elf: Don't merge .note.gnu.property section in IR
[deliverable/binutils-gdb.git] / bfd / elflink.c
1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2018 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 "bfd_stdint.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26 #define ARCH_SIZE 0
27 #include "elf-bfd.h"
28 #include "safe-ctype.h"
29 #include "libiberty.h"
30 #include "objalloc.h"
31 #if BFD_SUPPORTS_PLUGINS
32 #include "plugin-api.h"
33 #include "plugin.h"
34 #endif
35
36 /* This struct is used to pass information to routines called via
37 elf_link_hash_traverse which must return failure. */
38
39 struct elf_info_failed
40 {
41 struct bfd_link_info *info;
42 bfd_boolean failed;
43 };
44
45 /* This structure is used to pass information to
46 _bfd_elf_link_find_version_dependencies. */
47
48 struct elf_find_verdep_info
49 {
50 /* General link information. */
51 struct bfd_link_info *info;
52 /* The number of dependencies. */
53 unsigned int vers;
54 /* Whether we had a failure. */
55 bfd_boolean failed;
56 };
57
58 static bfd_boolean _bfd_elf_fix_symbol_flags
59 (struct elf_link_hash_entry *, struct elf_info_failed *);
60
61 asection *
62 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
63 unsigned long r_symndx,
64 bfd_boolean discard)
65 {
66 if (r_symndx >= cookie->locsymcount
67 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
68 {
69 struct elf_link_hash_entry *h;
70
71 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
72
73 while (h->root.type == bfd_link_hash_indirect
74 || h->root.type == bfd_link_hash_warning)
75 h = (struct elf_link_hash_entry *) h->root.u.i.link;
76
77 if ((h->root.type == bfd_link_hash_defined
78 || h->root.type == bfd_link_hash_defweak)
79 && discarded_section (h->root.u.def.section))
80 return h->root.u.def.section;
81 else
82 return NULL;
83 }
84 else
85 {
86 /* It's not a relocation against a global symbol,
87 but it could be a relocation against a local
88 symbol for a discarded section. */
89 asection *isec;
90 Elf_Internal_Sym *isym;
91
92 /* Need to: get the symbol; get the section. */
93 isym = &cookie->locsyms[r_symndx];
94 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
95 if (isec != NULL
96 && discard ? discarded_section (isec) : 1)
97 return isec;
98 }
99 return NULL;
100 }
101
102 /* Define a symbol in a dynamic linkage section. */
103
104 struct elf_link_hash_entry *
105 _bfd_elf_define_linkage_sym (bfd *abfd,
106 struct bfd_link_info *info,
107 asection *sec,
108 const char *name)
109 {
110 struct elf_link_hash_entry *h;
111 struct bfd_link_hash_entry *bh;
112 const struct elf_backend_data *bed;
113
114 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
115 if (h != NULL)
116 {
117 /* Zap symbol defined in an as-needed lib that wasn't linked.
118 This is a symptom of a larger problem: Absolute symbols
119 defined in shared libraries can't be overridden, because we
120 lose the link to the bfd which is via the symbol section. */
121 h->root.type = bfd_link_hash_new;
122 bh = &h->root;
123 }
124 else
125 bh = NULL;
126
127 bed = get_elf_backend_data (abfd);
128 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
129 sec, 0, NULL, FALSE, bed->collect,
130 &bh))
131 return NULL;
132 h = (struct elf_link_hash_entry *) bh;
133 BFD_ASSERT (h != NULL);
134 h->def_regular = 1;
135 h->non_elf = 0;
136 h->root.linker_def = 1;
137 h->type = STT_OBJECT;
138 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
139 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
140
141 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
142 return h;
143 }
144
145 bfd_boolean
146 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
147 {
148 flagword flags;
149 asection *s;
150 struct elf_link_hash_entry *h;
151 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
152 struct elf_link_hash_table *htab = elf_hash_table (info);
153
154 /* This function may be called more than once. */
155 if (htab->sgot != NULL)
156 return TRUE;
157
158 flags = bed->dynamic_sec_flags;
159
160 s = bfd_make_section_anyway_with_flags (abfd,
161 (bed->rela_plts_and_copies_p
162 ? ".rela.got" : ".rel.got"),
163 (bed->dynamic_sec_flags
164 | SEC_READONLY));
165 if (s == NULL
166 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
167 return FALSE;
168 htab->srelgot = s;
169
170 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
171 if (s == NULL
172 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
173 return FALSE;
174 htab->sgot = s;
175
176 if (bed->want_got_plt)
177 {
178 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
179 if (s == NULL
180 || !bfd_set_section_alignment (abfd, s,
181 bed->s->log_file_align))
182 return FALSE;
183 htab->sgotplt = s;
184 }
185
186 /* The first bit of the global offset table is the header. */
187 s->size += bed->got_header_size;
188
189 if (bed->want_got_sym)
190 {
191 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
192 (or .got.plt) section. We don't do this in the linker script
193 because we don't want to define the symbol if we are not creating
194 a global offset table. */
195 h = _bfd_elf_define_linkage_sym (abfd, info, s,
196 "_GLOBAL_OFFSET_TABLE_");
197 elf_hash_table (info)->hgot = h;
198 if (h == NULL)
199 return FALSE;
200 }
201
202 return TRUE;
203 }
204 \f
205 /* Create a strtab to hold the dynamic symbol names. */
206 static bfd_boolean
207 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
208 {
209 struct elf_link_hash_table *hash_table;
210
211 hash_table = elf_hash_table (info);
212 if (hash_table->dynobj == NULL)
213 {
214 /* We may not set dynobj, an input file holding linker created
215 dynamic sections to abfd, which may be a dynamic object with
216 its own dynamic sections. We need to find a normal input file
217 to hold linker created sections if possible. */
218 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
219 {
220 bfd *ibfd;
221 asection *s;
222 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
223 if ((ibfd->flags
224 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
225 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
226 && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
227 && !((s = ibfd->sections) != NULL
228 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
229 {
230 abfd = ibfd;
231 break;
232 }
233 }
234 hash_table->dynobj = abfd;
235 }
236
237 if (hash_table->dynstr == NULL)
238 {
239 hash_table->dynstr = _bfd_elf_strtab_init ();
240 if (hash_table->dynstr == NULL)
241 return FALSE;
242 }
243 return TRUE;
244 }
245
246 /* Create some sections which will be filled in with dynamic linking
247 information. ABFD is an input file which requires dynamic sections
248 to be created. The dynamic sections take up virtual memory space
249 when the final executable is run, so we need to create them before
250 addresses are assigned to the output sections. We work out the
251 actual contents and size of these sections later. */
252
253 bfd_boolean
254 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
255 {
256 flagword flags;
257 asection *s;
258 const struct elf_backend_data *bed;
259 struct elf_link_hash_entry *h;
260
261 if (! is_elf_hash_table (info->hash))
262 return FALSE;
263
264 if (elf_hash_table (info)->dynamic_sections_created)
265 return TRUE;
266
267 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
268 return FALSE;
269
270 abfd = elf_hash_table (info)->dynobj;
271 bed = get_elf_backend_data (abfd);
272
273 flags = bed->dynamic_sec_flags;
274
275 /* A dynamically linked executable has a .interp section, but a
276 shared library does not. */
277 if (bfd_link_executable (info) && !info->nointerp)
278 {
279 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
280 flags | SEC_READONLY);
281 if (s == NULL)
282 return FALSE;
283 }
284
285 /* Create sections to hold version informations. These are removed
286 if they are not needed. */
287 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
288 flags | SEC_READONLY);
289 if (s == NULL
290 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
291 return FALSE;
292
293 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
294 flags | SEC_READONLY);
295 if (s == NULL
296 || ! bfd_set_section_alignment (abfd, s, 1))
297 return FALSE;
298
299 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
300 flags | SEC_READONLY);
301 if (s == NULL
302 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
303 return FALSE;
304
305 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
306 flags | SEC_READONLY);
307 if (s == NULL
308 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
309 return FALSE;
310 elf_hash_table (info)->dynsym = s;
311
312 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
313 flags | SEC_READONLY);
314 if (s == NULL)
315 return FALSE;
316
317 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
318 if (s == NULL
319 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
320 return FALSE;
321
322 /* The special symbol _DYNAMIC is always set to the start of the
323 .dynamic section. We could set _DYNAMIC in a linker script, but we
324 only want to define it if we are, in fact, creating a .dynamic
325 section. We don't want to define it if there is no .dynamic
326 section, since on some ELF platforms the start up code examines it
327 to decide how to initialize the process. */
328 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
329 elf_hash_table (info)->hdynamic = h;
330 if (h == NULL)
331 return FALSE;
332
333 if (info->emit_hash)
334 {
335 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
336 flags | SEC_READONLY);
337 if (s == NULL
338 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
339 return FALSE;
340 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
341 }
342
343 if (info->emit_gnu_hash)
344 {
345 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
346 flags | SEC_READONLY);
347 if (s == NULL
348 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
349 return FALSE;
350 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
351 4 32-bit words followed by variable count of 64-bit words, then
352 variable count of 32-bit words. */
353 if (bed->s->arch_size == 64)
354 elf_section_data (s)->this_hdr.sh_entsize = 0;
355 else
356 elf_section_data (s)->this_hdr.sh_entsize = 4;
357 }
358
359 /* Let the backend create the rest of the sections. This lets the
360 backend set the right flags. The backend will normally create
361 the .got and .plt sections. */
362 if (bed->elf_backend_create_dynamic_sections == NULL
363 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
364 return FALSE;
365
366 elf_hash_table (info)->dynamic_sections_created = TRUE;
367
368 return TRUE;
369 }
370
371 /* Create dynamic sections when linking against a dynamic object. */
372
373 bfd_boolean
374 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
375 {
376 flagword flags, pltflags;
377 struct elf_link_hash_entry *h;
378 asection *s;
379 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
380 struct elf_link_hash_table *htab = elf_hash_table (info);
381
382 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
383 .rel[a].bss sections. */
384 flags = bed->dynamic_sec_flags;
385
386 pltflags = flags;
387 if (bed->plt_not_loaded)
388 /* We do not clear SEC_ALLOC here because we still want the OS to
389 allocate space for the section; it's just that there's nothing
390 to read in from the object file. */
391 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
392 else
393 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
394 if (bed->plt_readonly)
395 pltflags |= SEC_READONLY;
396
397 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
398 if (s == NULL
399 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
400 return FALSE;
401 htab->splt = s;
402
403 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
404 .plt section. */
405 if (bed->want_plt_sym)
406 {
407 h = _bfd_elf_define_linkage_sym (abfd, info, s,
408 "_PROCEDURE_LINKAGE_TABLE_");
409 elf_hash_table (info)->hplt = h;
410 if (h == NULL)
411 return FALSE;
412 }
413
414 s = bfd_make_section_anyway_with_flags (abfd,
415 (bed->rela_plts_and_copies_p
416 ? ".rela.plt" : ".rel.plt"),
417 flags | SEC_READONLY);
418 if (s == NULL
419 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
420 return FALSE;
421 htab->srelplt = s;
422
423 if (! _bfd_elf_create_got_section (abfd, info))
424 return FALSE;
425
426 if (bed->want_dynbss)
427 {
428 /* The .dynbss section is a place to put symbols which are defined
429 by dynamic objects, are referenced by regular objects, and are
430 not functions. We must allocate space for them in the process
431 image and use a R_*_COPY reloc to tell the dynamic linker to
432 initialize them at run time. The linker script puts the .dynbss
433 section into the .bss section of the final image. */
434 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
435 SEC_ALLOC | SEC_LINKER_CREATED);
436 if (s == NULL)
437 return FALSE;
438 htab->sdynbss = s;
439
440 if (bed->want_dynrelro)
441 {
442 /* Similarly, but for symbols that were originally in read-only
443 sections. This section doesn't really need to have contents,
444 but make it like other .data.rel.ro sections. */
445 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
446 flags);
447 if (s == NULL)
448 return FALSE;
449 htab->sdynrelro = s;
450 }
451
452 /* The .rel[a].bss section holds copy relocs. This section is not
453 normally needed. We need to create it here, though, so that the
454 linker will map it to an output section. We can't just create it
455 only if we need it, because we will not know whether we need it
456 until we have seen all the input files, and the first time the
457 main linker code calls BFD after examining all the input files
458 (size_dynamic_sections) the input sections have already been
459 mapped to the output sections. If the section turns out not to
460 be needed, we can discard it later. We will never need this
461 section when generating a shared object, since they do not use
462 copy relocs. */
463 if (bfd_link_executable (info))
464 {
465 s = bfd_make_section_anyway_with_flags (abfd,
466 (bed->rela_plts_and_copies_p
467 ? ".rela.bss" : ".rel.bss"),
468 flags | SEC_READONLY);
469 if (s == NULL
470 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
471 return FALSE;
472 htab->srelbss = s;
473
474 if (bed->want_dynrelro)
475 {
476 s = (bfd_make_section_anyway_with_flags
477 (abfd, (bed->rela_plts_and_copies_p
478 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
479 flags | SEC_READONLY));
480 if (s == NULL
481 || ! bfd_set_section_alignment (abfd, s,
482 bed->s->log_file_align))
483 return FALSE;
484 htab->sreldynrelro = s;
485 }
486 }
487 }
488
489 return TRUE;
490 }
491 \f
492 /* Record a new dynamic symbol. We record the dynamic symbols as we
493 read the input files, since we need to have a list of all of them
494 before we can determine the final sizes of the output sections.
495 Note that we may actually call this function even though we are not
496 going to output any dynamic symbols; in some cases we know that a
497 symbol should be in the dynamic symbol table, but only if there is
498 one. */
499
500 bfd_boolean
501 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
502 struct elf_link_hash_entry *h)
503 {
504 if (h->dynindx == -1)
505 {
506 struct elf_strtab_hash *dynstr;
507 char *p;
508 const char *name;
509 size_t indx;
510
511 /* XXX: The ABI draft says the linker must turn hidden and
512 internal symbols into STB_LOCAL symbols when producing the
513 DSO. However, if ld.so honors st_other in the dynamic table,
514 this would not be necessary. */
515 switch (ELF_ST_VISIBILITY (h->other))
516 {
517 case STV_INTERNAL:
518 case STV_HIDDEN:
519 if (h->root.type != bfd_link_hash_undefined
520 && h->root.type != bfd_link_hash_undefweak)
521 {
522 h->forced_local = 1;
523 if (!elf_hash_table (info)->is_relocatable_executable)
524 return TRUE;
525 }
526
527 default:
528 break;
529 }
530
531 h->dynindx = elf_hash_table (info)->dynsymcount;
532 ++elf_hash_table (info)->dynsymcount;
533
534 dynstr = elf_hash_table (info)->dynstr;
535 if (dynstr == NULL)
536 {
537 /* Create a strtab to hold the dynamic symbol names. */
538 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
539 if (dynstr == NULL)
540 return FALSE;
541 }
542
543 /* We don't put any version information in the dynamic string
544 table. */
545 name = h->root.root.string;
546 p = strchr (name, ELF_VER_CHR);
547 if (p != NULL)
548 /* We know that the p points into writable memory. In fact,
549 there are only a few symbols that have read-only names, being
550 those like _GLOBAL_OFFSET_TABLE_ that are created specially
551 by the backends. Most symbols will have names pointing into
552 an ELF string table read from a file, or to objalloc memory. */
553 *p = 0;
554
555 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
556
557 if (p != NULL)
558 *p = ELF_VER_CHR;
559
560 if (indx == (size_t) -1)
561 return FALSE;
562 h->dynstr_index = indx;
563 }
564
565 return TRUE;
566 }
567 \f
568 /* Mark a symbol dynamic. */
569
570 static void
571 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
572 struct elf_link_hash_entry *h,
573 Elf_Internal_Sym *sym)
574 {
575 struct bfd_elf_dynamic_list *d = info->dynamic_list;
576
577 /* It may be called more than once on the same H. */
578 if(h->dynamic || bfd_link_relocatable (info))
579 return;
580
581 if ((info->dynamic_data
582 && (h->type == STT_OBJECT
583 || h->type == STT_COMMON
584 || (sym != NULL
585 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
586 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
587 || (d != NULL
588 && h->non_elf
589 && (*d->match) (&d->head, NULL, h->root.root.string)))
590 {
591 h->dynamic = 1;
592 /* NB: If a symbol is made dynamic by --dynamic-list, it has
593 non-IR reference. */
594 h->root.non_ir_ref_dynamic = 1;
595 }
596 }
597
598 /* Record an assignment to a symbol made by a linker script. We need
599 this in case some dynamic object refers to this symbol. */
600
601 bfd_boolean
602 bfd_elf_record_link_assignment (bfd *output_bfd,
603 struct bfd_link_info *info,
604 const char *name,
605 bfd_boolean provide,
606 bfd_boolean hidden)
607 {
608 struct elf_link_hash_entry *h, *hv;
609 struct elf_link_hash_table *htab;
610 const struct elf_backend_data *bed;
611
612 if (!is_elf_hash_table (info->hash))
613 return TRUE;
614
615 htab = elf_hash_table (info);
616 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
617 if (h == NULL)
618 return provide;
619
620 if (h->root.type == bfd_link_hash_warning)
621 h = (struct elf_link_hash_entry *) h->root.u.i.link;
622
623 if (h->versioned == unknown)
624 {
625 /* Set versioned if symbol version is unknown. */
626 char *version = strrchr (name, ELF_VER_CHR);
627 if (version)
628 {
629 if (version > name && version[-1] != ELF_VER_CHR)
630 h->versioned = versioned_hidden;
631 else
632 h->versioned = versioned;
633 }
634 }
635
636 /* Symbols defined in a linker script but not referenced anywhere
637 else will have non_elf set. */
638 if (h->non_elf)
639 {
640 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
641 h->non_elf = 0;
642 }
643
644 switch (h->root.type)
645 {
646 case bfd_link_hash_defined:
647 case bfd_link_hash_defweak:
648 case bfd_link_hash_common:
649 break;
650 case bfd_link_hash_undefweak:
651 case bfd_link_hash_undefined:
652 /* Since we're defining the symbol, don't let it seem to have not
653 been defined. record_dynamic_symbol and size_dynamic_sections
654 may depend on this. */
655 h->root.type = bfd_link_hash_new;
656 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
657 bfd_link_repair_undef_list (&htab->root);
658 break;
659 case bfd_link_hash_new:
660 break;
661 case bfd_link_hash_indirect:
662 /* We had a versioned symbol in a dynamic library. We make the
663 the versioned symbol point to this one. */
664 bed = get_elf_backend_data (output_bfd);
665 hv = h;
666 while (hv->root.type == bfd_link_hash_indirect
667 || hv->root.type == bfd_link_hash_warning)
668 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
669 /* We don't need to update h->root.u since linker will set them
670 later. */
671 h->root.type = bfd_link_hash_undefined;
672 hv->root.type = bfd_link_hash_indirect;
673 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
674 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
675 break;
676 default:
677 BFD_FAIL ();
678 return FALSE;
679 }
680
681 /* If this symbol is being provided by the linker script, and it is
682 currently defined by a dynamic object, but not by a regular
683 object, then mark it as undefined so that the generic linker will
684 force the correct value. */
685 if (provide
686 && h->def_dynamic
687 && !h->def_regular)
688 h->root.type = bfd_link_hash_undefined;
689
690 /* If this symbol is currently defined by a dynamic object, but not
691 by a regular object, then clear out any version information because
692 the symbol will not be associated with the dynamic object any
693 more. */
694 if (h->def_dynamic && !h->def_regular)
695 h->verinfo.verdef = NULL;
696
697 /* Make sure this symbol is not garbage collected. */
698 h->mark = 1;
699
700 h->def_regular = 1;
701
702 if (hidden)
703 {
704 bed = get_elf_backend_data (output_bfd);
705 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
706 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
707 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
708 }
709
710 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
711 and executables. */
712 if (!bfd_link_relocatable (info)
713 && h->dynindx != -1
714 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
715 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
716 h->forced_local = 1;
717
718 if ((h->def_dynamic
719 || h->ref_dynamic
720 || bfd_link_dll (info)
721 || elf_hash_table (info)->is_relocatable_executable)
722 && !h->forced_local
723 && h->dynindx == -1)
724 {
725 if (! bfd_elf_link_record_dynamic_symbol (info, h))
726 return FALSE;
727
728 /* If this is a weak defined symbol, and we know a corresponding
729 real symbol from the same dynamic object, make sure the real
730 symbol is also made into a dynamic symbol. */
731 if (h->is_weakalias)
732 {
733 struct elf_link_hash_entry *def = weakdef (h);
734
735 if (def->dynindx == -1
736 && !bfd_elf_link_record_dynamic_symbol (info, def))
737 return FALSE;
738 }
739 }
740
741 return TRUE;
742 }
743
744 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
745 success, and 2 on a failure caused by attempting to record a symbol
746 in a discarded section, eg. a discarded link-once section symbol. */
747
748 int
749 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
750 bfd *input_bfd,
751 long input_indx)
752 {
753 bfd_size_type amt;
754 struct elf_link_local_dynamic_entry *entry;
755 struct elf_link_hash_table *eht;
756 struct elf_strtab_hash *dynstr;
757 size_t dynstr_index;
758 char *name;
759 Elf_External_Sym_Shndx eshndx;
760 char esym[sizeof (Elf64_External_Sym)];
761
762 if (! is_elf_hash_table (info->hash))
763 return 0;
764
765 /* See if the entry exists already. */
766 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
767 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
768 return 1;
769
770 amt = sizeof (*entry);
771 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
772 if (entry == NULL)
773 return 0;
774
775 /* Go find the symbol, so that we can find it's name. */
776 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
777 1, input_indx, &entry->isym, esym, &eshndx))
778 {
779 bfd_release (input_bfd, entry);
780 return 0;
781 }
782
783 if (entry->isym.st_shndx != SHN_UNDEF
784 && entry->isym.st_shndx < SHN_LORESERVE)
785 {
786 asection *s;
787
788 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
789 if (s == NULL || bfd_is_abs_section (s->output_section))
790 {
791 /* We can still bfd_release here as nothing has done another
792 bfd_alloc. We can't do this later in this function. */
793 bfd_release (input_bfd, entry);
794 return 2;
795 }
796 }
797
798 name = (bfd_elf_string_from_elf_section
799 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
800 entry->isym.st_name));
801
802 dynstr = elf_hash_table (info)->dynstr;
803 if (dynstr == NULL)
804 {
805 /* Create a strtab to hold the dynamic symbol names. */
806 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
807 if (dynstr == NULL)
808 return 0;
809 }
810
811 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
812 if (dynstr_index == (size_t) -1)
813 return 0;
814 entry->isym.st_name = dynstr_index;
815
816 eht = elf_hash_table (info);
817
818 entry->next = eht->dynlocal;
819 eht->dynlocal = entry;
820 entry->input_bfd = input_bfd;
821 entry->input_indx = input_indx;
822 eht->dynsymcount++;
823
824 /* Whatever binding the symbol had before, it's now local. */
825 entry->isym.st_info
826 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
827
828 /* The dynindx will be set at the end of size_dynamic_sections. */
829
830 return 1;
831 }
832
833 /* Return the dynindex of a local dynamic symbol. */
834
835 long
836 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
837 bfd *input_bfd,
838 long input_indx)
839 {
840 struct elf_link_local_dynamic_entry *e;
841
842 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
843 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
844 return e->dynindx;
845 return -1;
846 }
847
848 /* This function is used to renumber the dynamic symbols, if some of
849 them are removed because they are marked as local. This is called
850 via elf_link_hash_traverse. */
851
852 static bfd_boolean
853 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
854 void *data)
855 {
856 size_t *count = (size_t *) data;
857
858 if (h->forced_local)
859 return TRUE;
860
861 if (h->dynindx != -1)
862 h->dynindx = ++(*count);
863
864 return TRUE;
865 }
866
867
868 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
869 STB_LOCAL binding. */
870
871 static bfd_boolean
872 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
873 void *data)
874 {
875 size_t *count = (size_t *) data;
876
877 if (!h->forced_local)
878 return TRUE;
879
880 if (h->dynindx != -1)
881 h->dynindx = ++(*count);
882
883 return TRUE;
884 }
885
886 /* Return true if the dynamic symbol for a given section should be
887 omitted when creating a shared library. */
888 bfd_boolean
889 _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
890 struct bfd_link_info *info,
891 asection *p)
892 {
893 struct elf_link_hash_table *htab;
894 asection *ip;
895
896 switch (elf_section_data (p)->this_hdr.sh_type)
897 {
898 case SHT_PROGBITS:
899 case SHT_NOBITS:
900 /* If sh_type is yet undecided, assume it could be
901 SHT_PROGBITS/SHT_NOBITS. */
902 case SHT_NULL:
903 htab = elf_hash_table (info);
904 if (p == htab->tls_sec)
905 return FALSE;
906
907 if (htab->text_index_section != NULL)
908 return p != htab->text_index_section && p != htab->data_index_section;
909
910 return (htab->dynobj != NULL
911 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
912 && ip->output_section == p);
913
914 /* There shouldn't be section relative relocations
915 against any other section. */
916 default:
917 return TRUE;
918 }
919 }
920
921 bfd_boolean
922 _bfd_elf_omit_section_dynsym_all
923 (bfd *output_bfd ATTRIBUTE_UNUSED,
924 struct bfd_link_info *info ATTRIBUTE_UNUSED,
925 asection *p ATTRIBUTE_UNUSED)
926 {
927 return TRUE;
928 }
929
930 /* Assign dynsym indices. In a shared library we generate a section
931 symbol for each output section, which come first. Next come symbols
932 which have been forced to local binding. Then all of the back-end
933 allocated local dynamic syms, followed by the rest of the global
934 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
935 (This prevents the early call before elf_backend_init_index_section
936 and strip_excluded_output_sections setting dynindx for sections
937 that are stripped.) */
938
939 static unsigned long
940 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
941 struct bfd_link_info *info,
942 unsigned long *section_sym_count)
943 {
944 unsigned long dynsymcount = 0;
945 bfd_boolean do_sec = section_sym_count != NULL;
946
947 if (bfd_link_pic (info)
948 || elf_hash_table (info)->is_relocatable_executable)
949 {
950 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
951 asection *p;
952 for (p = output_bfd->sections; p ; p = p->next)
953 if ((p->flags & SEC_EXCLUDE) == 0
954 && (p->flags & SEC_ALLOC) != 0
955 && elf_hash_table (info)->dynamic_relocs
956 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
957 {
958 ++dynsymcount;
959 if (do_sec)
960 elf_section_data (p)->dynindx = dynsymcount;
961 }
962 else if (do_sec)
963 elf_section_data (p)->dynindx = 0;
964 }
965 if (do_sec)
966 *section_sym_count = dynsymcount;
967
968 elf_link_hash_traverse (elf_hash_table (info),
969 elf_link_renumber_local_hash_table_dynsyms,
970 &dynsymcount);
971
972 if (elf_hash_table (info)->dynlocal)
973 {
974 struct elf_link_local_dynamic_entry *p;
975 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
976 p->dynindx = ++dynsymcount;
977 }
978 elf_hash_table (info)->local_dynsymcount = dynsymcount;
979
980 elf_link_hash_traverse (elf_hash_table (info),
981 elf_link_renumber_hash_table_dynsyms,
982 &dynsymcount);
983
984 /* There is an unused NULL entry at the head of the table which we
985 must account for in our count even if the table is empty since it
986 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
987 .dynamic section. */
988 dynsymcount++;
989
990 elf_hash_table (info)->dynsymcount = dynsymcount;
991 return dynsymcount;
992 }
993
994 /* Merge st_other field. */
995
996 static void
997 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
998 const Elf_Internal_Sym *isym, asection *sec,
999 bfd_boolean definition, bfd_boolean dynamic)
1000 {
1001 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1002
1003 /* If st_other has a processor-specific meaning, specific
1004 code might be needed here. */
1005 if (bed->elf_backend_merge_symbol_attribute)
1006 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
1007 dynamic);
1008
1009 if (!dynamic)
1010 {
1011 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
1012 unsigned hvis = ELF_ST_VISIBILITY (h->other);
1013
1014 /* Keep the most constraining visibility. Leave the remainder
1015 of the st_other field to elf_backend_merge_symbol_attribute. */
1016 if (symvis - 1 < hvis - 1)
1017 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1018 }
1019 else if (definition
1020 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
1021 && (sec->flags & SEC_READONLY) == 0)
1022 h->protected_def = 1;
1023 }
1024
1025 /* This function is called when we want to merge a new symbol with an
1026 existing symbol. It handles the various cases which arise when we
1027 find a definition in a dynamic object, or when there is already a
1028 definition in a dynamic object. The new symbol is described by
1029 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1030 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1031 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1032 of an old common symbol. We set OVERRIDE if the old symbol is
1033 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1034 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1035 to change. By OK to change, we mean that we shouldn't warn if the
1036 type or size does change. */
1037
1038 static bfd_boolean
1039 _bfd_elf_merge_symbol (bfd *abfd,
1040 struct bfd_link_info *info,
1041 const char *name,
1042 Elf_Internal_Sym *sym,
1043 asection **psec,
1044 bfd_vma *pvalue,
1045 struct elf_link_hash_entry **sym_hash,
1046 bfd **poldbfd,
1047 bfd_boolean *pold_weak,
1048 unsigned int *pold_alignment,
1049 bfd_boolean *skip,
1050 bfd_boolean *override,
1051 bfd_boolean *type_change_ok,
1052 bfd_boolean *size_change_ok,
1053 bfd_boolean *matched)
1054 {
1055 asection *sec, *oldsec;
1056 struct elf_link_hash_entry *h;
1057 struct elf_link_hash_entry *hi;
1058 struct elf_link_hash_entry *flip;
1059 int bind;
1060 bfd *oldbfd;
1061 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1062 bfd_boolean newweak, oldweak, newfunc, oldfunc;
1063 const struct elf_backend_data *bed;
1064 char *new_version;
1065 bfd_boolean default_sym = *matched;
1066
1067 *skip = FALSE;
1068 *override = FALSE;
1069
1070 sec = *psec;
1071 bind = ELF_ST_BIND (sym->st_info);
1072
1073 if (! bfd_is_und_section (sec))
1074 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1075 else
1076 h = ((struct elf_link_hash_entry *)
1077 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1078 if (h == NULL)
1079 return FALSE;
1080 *sym_hash = h;
1081
1082 bed = get_elf_backend_data (abfd);
1083
1084 /* NEW_VERSION is the symbol version of the new symbol. */
1085 if (h->versioned != unversioned)
1086 {
1087 /* Symbol version is unknown or versioned. */
1088 new_version = strrchr (name, ELF_VER_CHR);
1089 if (new_version)
1090 {
1091 if (h->versioned == unknown)
1092 {
1093 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1094 h->versioned = versioned_hidden;
1095 else
1096 h->versioned = versioned;
1097 }
1098 new_version += 1;
1099 if (new_version[0] == '\0')
1100 new_version = NULL;
1101 }
1102 else
1103 h->versioned = unversioned;
1104 }
1105 else
1106 new_version = NULL;
1107
1108 /* For merging, we only care about real symbols. But we need to make
1109 sure that indirect symbol dynamic flags are updated. */
1110 hi = h;
1111 while (h->root.type == bfd_link_hash_indirect
1112 || h->root.type == bfd_link_hash_warning)
1113 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1114
1115 if (!*matched)
1116 {
1117 if (hi == h || h->root.type == bfd_link_hash_new)
1118 *matched = TRUE;
1119 else
1120 {
1121 /* OLD_HIDDEN is true if the existing symbol is only visible
1122 to the symbol with the same symbol version. NEW_HIDDEN is
1123 true if the new symbol is only visible to the symbol with
1124 the same symbol version. */
1125 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1126 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1127 if (!old_hidden && !new_hidden)
1128 /* The new symbol matches the existing symbol if both
1129 aren't hidden. */
1130 *matched = TRUE;
1131 else
1132 {
1133 /* OLD_VERSION is the symbol version of the existing
1134 symbol. */
1135 char *old_version;
1136
1137 if (h->versioned >= versioned)
1138 old_version = strrchr (h->root.root.string,
1139 ELF_VER_CHR) + 1;
1140 else
1141 old_version = NULL;
1142
1143 /* The new symbol matches the existing symbol if they
1144 have the same symbol version. */
1145 *matched = (old_version == new_version
1146 || (old_version != NULL
1147 && new_version != NULL
1148 && strcmp (old_version, new_version) == 0));
1149 }
1150 }
1151 }
1152
1153 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1154 existing symbol. */
1155
1156 oldbfd = NULL;
1157 oldsec = NULL;
1158 switch (h->root.type)
1159 {
1160 default:
1161 break;
1162
1163 case bfd_link_hash_undefined:
1164 case bfd_link_hash_undefweak:
1165 oldbfd = h->root.u.undef.abfd;
1166 break;
1167
1168 case bfd_link_hash_defined:
1169 case bfd_link_hash_defweak:
1170 oldbfd = h->root.u.def.section->owner;
1171 oldsec = h->root.u.def.section;
1172 break;
1173
1174 case bfd_link_hash_common:
1175 oldbfd = h->root.u.c.p->section->owner;
1176 oldsec = h->root.u.c.p->section;
1177 if (pold_alignment)
1178 *pold_alignment = h->root.u.c.p->alignment_power;
1179 break;
1180 }
1181 if (poldbfd && *poldbfd == NULL)
1182 *poldbfd = oldbfd;
1183
1184 /* Differentiate strong and weak symbols. */
1185 newweak = bind == STB_WEAK;
1186 oldweak = (h->root.type == bfd_link_hash_defweak
1187 || h->root.type == bfd_link_hash_undefweak);
1188 if (pold_weak)
1189 *pold_weak = oldweak;
1190
1191 /* We have to check it for every instance since the first few may be
1192 references and not all compilers emit symbol type for undefined
1193 symbols. */
1194 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1195
1196 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1197 respectively, is from a dynamic object. */
1198
1199 newdyn = (abfd->flags & DYNAMIC) != 0;
1200
1201 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1202 syms and defined syms in dynamic libraries respectively.
1203 ref_dynamic on the other hand can be set for a symbol defined in
1204 a dynamic library, and def_dynamic may not be set; When the
1205 definition in a dynamic lib is overridden by a definition in the
1206 executable use of the symbol in the dynamic lib becomes a
1207 reference to the executable symbol. */
1208 if (newdyn)
1209 {
1210 if (bfd_is_und_section (sec))
1211 {
1212 if (bind != STB_WEAK)
1213 {
1214 h->ref_dynamic_nonweak = 1;
1215 hi->ref_dynamic_nonweak = 1;
1216 }
1217 }
1218 else
1219 {
1220 /* Update the existing symbol only if they match. */
1221 if (*matched)
1222 h->dynamic_def = 1;
1223 hi->dynamic_def = 1;
1224 }
1225 }
1226
1227 /* If we just created the symbol, mark it as being an ELF symbol.
1228 Other than that, there is nothing to do--there is no merge issue
1229 with a newly defined symbol--so we just return. */
1230
1231 if (h->root.type == bfd_link_hash_new)
1232 {
1233 h->non_elf = 0;
1234 return TRUE;
1235 }
1236
1237 /* In cases involving weak versioned symbols, we may wind up trying
1238 to merge a symbol with itself. Catch that here, to avoid the
1239 confusion that results if we try to override a symbol with
1240 itself. The additional tests catch cases like
1241 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1242 dynamic object, which we do want to handle here. */
1243 if (abfd == oldbfd
1244 && (newweak || oldweak)
1245 && ((abfd->flags & DYNAMIC) == 0
1246 || !h->def_regular))
1247 return TRUE;
1248
1249 olddyn = FALSE;
1250 if (oldbfd != NULL)
1251 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1252 else if (oldsec != NULL)
1253 {
1254 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1255 indices used by MIPS ELF. */
1256 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1257 }
1258
1259 /* Handle a case where plugin_notice won't be called and thus won't
1260 set the non_ir_ref flags on the first pass over symbols. */
1261 if (oldbfd != NULL
1262 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1263 && newdyn != olddyn)
1264 {
1265 h->root.non_ir_ref_dynamic = TRUE;
1266 hi->root.non_ir_ref_dynamic = TRUE;
1267 }
1268
1269 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1270 respectively, appear to be a definition rather than reference. */
1271
1272 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1273
1274 olddef = (h->root.type != bfd_link_hash_undefined
1275 && h->root.type != bfd_link_hash_undefweak
1276 && h->root.type != bfd_link_hash_common);
1277
1278 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1279 respectively, appear to be a function. */
1280
1281 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1282 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1283
1284 oldfunc = (h->type != STT_NOTYPE
1285 && bed->is_function_type (h->type));
1286
1287 if (!(newfunc && oldfunc)
1288 && ELF_ST_TYPE (sym->st_info) != h->type
1289 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1290 && h->type != STT_NOTYPE
1291 && (newdef || bfd_is_com_section (sec))
1292 && (olddef || h->root.type == bfd_link_hash_common))
1293 {
1294 /* If creating a default indirect symbol ("foo" or "foo@") from
1295 a dynamic versioned definition ("foo@@") skip doing so if
1296 there is an existing regular definition with a different
1297 type. We don't want, for example, a "time" variable in the
1298 executable overriding a "time" function in a shared library. */
1299 if (newdyn
1300 && !olddyn)
1301 {
1302 *skip = TRUE;
1303 return TRUE;
1304 }
1305
1306 /* When adding a symbol from a regular object file after we have
1307 created indirect symbols, undo the indirection and any
1308 dynamic state. */
1309 if (hi != h
1310 && !newdyn
1311 && olddyn)
1312 {
1313 h = hi;
1314 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1315 h->forced_local = 0;
1316 h->ref_dynamic = 0;
1317 h->def_dynamic = 0;
1318 h->dynamic_def = 0;
1319 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1320 {
1321 h->root.type = bfd_link_hash_undefined;
1322 h->root.u.undef.abfd = abfd;
1323 }
1324 else
1325 {
1326 h->root.type = bfd_link_hash_new;
1327 h->root.u.undef.abfd = NULL;
1328 }
1329 return TRUE;
1330 }
1331 }
1332
1333 /* Check TLS symbols. We don't check undefined symbols introduced
1334 by "ld -u" which have no type (and oldbfd NULL), and we don't
1335 check symbols from plugins because they also have no type. */
1336 if (oldbfd != NULL
1337 && (oldbfd->flags & BFD_PLUGIN) == 0
1338 && (abfd->flags & BFD_PLUGIN) == 0
1339 && ELF_ST_TYPE (sym->st_info) != h->type
1340 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1341 {
1342 bfd *ntbfd, *tbfd;
1343 bfd_boolean ntdef, tdef;
1344 asection *ntsec, *tsec;
1345
1346 if (h->type == STT_TLS)
1347 {
1348 ntbfd = abfd;
1349 ntsec = sec;
1350 ntdef = newdef;
1351 tbfd = oldbfd;
1352 tsec = oldsec;
1353 tdef = olddef;
1354 }
1355 else
1356 {
1357 ntbfd = oldbfd;
1358 ntsec = oldsec;
1359 ntdef = olddef;
1360 tbfd = abfd;
1361 tsec = sec;
1362 tdef = newdef;
1363 }
1364
1365 if (tdef && ntdef)
1366 _bfd_error_handler
1367 /* xgettext:c-format */
1368 (_("%s: TLS definition in %pB section %pA "
1369 "mismatches non-TLS definition in %pB section %pA"),
1370 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1371 else if (!tdef && !ntdef)
1372 _bfd_error_handler
1373 /* xgettext:c-format */
1374 (_("%s: TLS reference in %pB "
1375 "mismatches non-TLS reference in %pB"),
1376 h->root.root.string, tbfd, ntbfd);
1377 else if (tdef)
1378 _bfd_error_handler
1379 /* xgettext:c-format */
1380 (_("%s: TLS definition in %pB section %pA "
1381 "mismatches non-TLS reference in %pB"),
1382 h->root.root.string, tbfd, tsec, ntbfd);
1383 else
1384 _bfd_error_handler
1385 /* xgettext:c-format */
1386 (_("%s: TLS reference in %pB "
1387 "mismatches non-TLS definition in %pB section %pA"),
1388 h->root.root.string, tbfd, ntbfd, ntsec);
1389
1390 bfd_set_error (bfd_error_bad_value);
1391 return FALSE;
1392 }
1393
1394 /* If the old symbol has non-default visibility, we ignore the new
1395 definition from a dynamic object. */
1396 if (newdyn
1397 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1398 && !bfd_is_und_section (sec))
1399 {
1400 *skip = TRUE;
1401 /* Make sure this symbol is dynamic. */
1402 h->ref_dynamic = 1;
1403 hi->ref_dynamic = 1;
1404 /* A protected symbol has external availability. Make sure it is
1405 recorded as dynamic.
1406
1407 FIXME: Should we check type and size for protected symbol? */
1408 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1409 return bfd_elf_link_record_dynamic_symbol (info, h);
1410 else
1411 return TRUE;
1412 }
1413 else if (!newdyn
1414 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1415 && h->def_dynamic)
1416 {
1417 /* If the new symbol with non-default visibility comes from a
1418 relocatable file and the old definition comes from a dynamic
1419 object, we remove the old definition. */
1420 if (hi->root.type == bfd_link_hash_indirect)
1421 {
1422 /* Handle the case where the old dynamic definition is
1423 default versioned. We need to copy the symbol info from
1424 the symbol with default version to the normal one if it
1425 was referenced before. */
1426 if (h->ref_regular)
1427 {
1428 hi->root.type = h->root.type;
1429 h->root.type = bfd_link_hash_indirect;
1430 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1431
1432 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1433 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1434 {
1435 /* If the new symbol is hidden or internal, completely undo
1436 any dynamic link state. */
1437 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1438 h->forced_local = 0;
1439 h->ref_dynamic = 0;
1440 }
1441 else
1442 h->ref_dynamic = 1;
1443
1444 h->def_dynamic = 0;
1445 /* FIXME: Should we check type and size for protected symbol? */
1446 h->size = 0;
1447 h->type = 0;
1448
1449 h = hi;
1450 }
1451 else
1452 h = hi;
1453 }
1454
1455 /* If the old symbol was undefined before, then it will still be
1456 on the undefs list. If the new symbol is undefined or
1457 common, we can't make it bfd_link_hash_new here, because new
1458 undefined or common symbols will be added to the undefs list
1459 by _bfd_generic_link_add_one_symbol. Symbols may not be
1460 added twice to the undefs list. Also, if the new symbol is
1461 undefweak then we don't want to lose the strong undef. */
1462 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1463 {
1464 h->root.type = bfd_link_hash_undefined;
1465 h->root.u.undef.abfd = abfd;
1466 }
1467 else
1468 {
1469 h->root.type = bfd_link_hash_new;
1470 h->root.u.undef.abfd = NULL;
1471 }
1472
1473 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1474 {
1475 /* If the new symbol is hidden or internal, completely undo
1476 any dynamic link state. */
1477 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1478 h->forced_local = 0;
1479 h->ref_dynamic = 0;
1480 }
1481 else
1482 h->ref_dynamic = 1;
1483 h->def_dynamic = 0;
1484 /* FIXME: Should we check type and size for protected symbol? */
1485 h->size = 0;
1486 h->type = 0;
1487 return TRUE;
1488 }
1489
1490 /* If a new weak symbol definition comes from a regular file and the
1491 old symbol comes from a dynamic library, we treat the new one as
1492 strong. Similarly, an old weak symbol definition from a regular
1493 file is treated as strong when the new symbol comes from a dynamic
1494 library. Further, an old weak symbol from a dynamic library is
1495 treated as strong if the new symbol is from a dynamic library.
1496 This reflects the way glibc's ld.so works.
1497
1498 Also allow a weak symbol to override a linker script symbol
1499 defined by an early pass over the script. This is done so the
1500 linker knows the symbol is defined in an object file, for the
1501 DEFINED script function.
1502
1503 Do this before setting *type_change_ok or *size_change_ok so that
1504 we warn properly when dynamic library symbols are overridden. */
1505
1506 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1507 newweak = FALSE;
1508 if (olddef && newdyn)
1509 oldweak = FALSE;
1510
1511 /* Allow changes between different types of function symbol. */
1512 if (newfunc && oldfunc)
1513 *type_change_ok = TRUE;
1514
1515 /* It's OK to change the type if either the existing symbol or the
1516 new symbol is weak. A type change is also OK if the old symbol
1517 is undefined and the new symbol is defined. */
1518
1519 if (oldweak
1520 || newweak
1521 || (newdef
1522 && h->root.type == bfd_link_hash_undefined))
1523 *type_change_ok = TRUE;
1524
1525 /* It's OK to change the size if either the existing symbol or the
1526 new symbol is weak, or if the old symbol is undefined. */
1527
1528 if (*type_change_ok
1529 || h->root.type == bfd_link_hash_undefined)
1530 *size_change_ok = TRUE;
1531
1532 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1533 symbol, respectively, appears to be a common symbol in a dynamic
1534 object. If a symbol appears in an uninitialized section, and is
1535 not weak, and is not a function, then it may be a common symbol
1536 which was resolved when the dynamic object was created. We want
1537 to treat such symbols specially, because they raise special
1538 considerations when setting the symbol size: if the symbol
1539 appears as a common symbol in a regular object, and the size in
1540 the regular object is larger, we must make sure that we use the
1541 larger size. This problematic case can always be avoided in C,
1542 but it must be handled correctly when using Fortran shared
1543 libraries.
1544
1545 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1546 likewise for OLDDYNCOMMON and OLDDEF.
1547
1548 Note that this test is just a heuristic, and that it is quite
1549 possible to have an uninitialized symbol in a shared object which
1550 is really a definition, rather than a common symbol. This could
1551 lead to some minor confusion when the symbol really is a common
1552 symbol in some regular object. However, I think it will be
1553 harmless. */
1554
1555 if (newdyn
1556 && newdef
1557 && !newweak
1558 && (sec->flags & SEC_ALLOC) != 0
1559 && (sec->flags & SEC_LOAD) == 0
1560 && sym->st_size > 0
1561 && !newfunc)
1562 newdyncommon = TRUE;
1563 else
1564 newdyncommon = FALSE;
1565
1566 if (olddyn
1567 && olddef
1568 && h->root.type == bfd_link_hash_defined
1569 && h->def_dynamic
1570 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1571 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1572 && h->size > 0
1573 && !oldfunc)
1574 olddyncommon = TRUE;
1575 else
1576 olddyncommon = FALSE;
1577
1578 /* We now know everything about the old and new symbols. We ask the
1579 backend to check if we can merge them. */
1580 if (bed->merge_symbol != NULL)
1581 {
1582 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1583 return FALSE;
1584 sec = *psec;
1585 }
1586
1587 /* There are multiple definitions of a normal symbol. Skip the
1588 default symbol as well as definition from an IR object. */
1589 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1590 && !default_sym && h->def_regular
1591 && !(oldbfd != NULL
1592 && (oldbfd->flags & BFD_PLUGIN) != 0
1593 && (abfd->flags & BFD_PLUGIN) == 0))
1594 {
1595 /* Handle a multiple definition. */
1596 (*info->callbacks->multiple_definition) (info, &h->root,
1597 abfd, sec, *pvalue);
1598 *skip = TRUE;
1599 return TRUE;
1600 }
1601
1602 /* If both the old and the new symbols look like common symbols in a
1603 dynamic object, set the size of the symbol to the larger of the
1604 two. */
1605
1606 if (olddyncommon
1607 && newdyncommon
1608 && sym->st_size != h->size)
1609 {
1610 /* Since we think we have two common symbols, issue a multiple
1611 common warning if desired. Note that we only warn if the
1612 size is different. If the size is the same, we simply let
1613 the old symbol override the new one as normally happens with
1614 symbols defined in dynamic objects. */
1615
1616 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1617 bfd_link_hash_common, sym->st_size);
1618 if (sym->st_size > h->size)
1619 h->size = sym->st_size;
1620
1621 *size_change_ok = TRUE;
1622 }
1623
1624 /* If we are looking at a dynamic object, and we have found a
1625 definition, we need to see if the symbol was already defined by
1626 some other object. If so, we want to use the existing
1627 definition, and we do not want to report a multiple symbol
1628 definition error; we do this by clobbering *PSEC to be
1629 bfd_und_section_ptr.
1630
1631 We treat a common symbol as a definition if the symbol in the
1632 shared library is a function, since common symbols always
1633 represent variables; this can cause confusion in principle, but
1634 any such confusion would seem to indicate an erroneous program or
1635 shared library. We also permit a common symbol in a regular
1636 object to override a weak symbol in a shared object. */
1637
1638 if (newdyn
1639 && newdef
1640 && (olddef
1641 || (h->root.type == bfd_link_hash_common
1642 && (newweak || newfunc))))
1643 {
1644 *override = TRUE;
1645 newdef = FALSE;
1646 newdyncommon = FALSE;
1647
1648 *psec = sec = bfd_und_section_ptr;
1649 *size_change_ok = TRUE;
1650
1651 /* If we get here when the old symbol is a common symbol, then
1652 we are explicitly letting it override a weak symbol or
1653 function in a dynamic object, and we don't want to warn about
1654 a type change. If the old symbol is a defined symbol, a type
1655 change warning may still be appropriate. */
1656
1657 if (h->root.type == bfd_link_hash_common)
1658 *type_change_ok = TRUE;
1659 }
1660
1661 /* Handle the special case of an old common symbol merging with a
1662 new symbol which looks like a common symbol in a shared object.
1663 We change *PSEC and *PVALUE to make the new symbol look like a
1664 common symbol, and let _bfd_generic_link_add_one_symbol do the
1665 right thing. */
1666
1667 if (newdyncommon
1668 && h->root.type == bfd_link_hash_common)
1669 {
1670 *override = TRUE;
1671 newdef = FALSE;
1672 newdyncommon = FALSE;
1673 *pvalue = sym->st_size;
1674 *psec = sec = bed->common_section (oldsec);
1675 *size_change_ok = TRUE;
1676 }
1677
1678 /* Skip weak definitions of symbols that are already defined. */
1679 if (newdef && olddef && newweak)
1680 {
1681 /* Don't skip new non-IR weak syms. */
1682 if (!(oldbfd != NULL
1683 && (oldbfd->flags & BFD_PLUGIN) != 0
1684 && (abfd->flags & BFD_PLUGIN) == 0))
1685 {
1686 newdef = FALSE;
1687 *skip = TRUE;
1688 }
1689
1690 /* Merge st_other. If the symbol already has a dynamic index,
1691 but visibility says it should not be visible, turn it into a
1692 local symbol. */
1693 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1694 if (h->dynindx != -1)
1695 switch (ELF_ST_VISIBILITY (h->other))
1696 {
1697 case STV_INTERNAL:
1698 case STV_HIDDEN:
1699 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1700 break;
1701 }
1702 }
1703
1704 /* If the old symbol is from a dynamic object, and the new symbol is
1705 a definition which is not from a dynamic object, then the new
1706 symbol overrides the old symbol. Symbols from regular files
1707 always take precedence over symbols from dynamic objects, even if
1708 they are defined after the dynamic object in the link.
1709
1710 As above, we again permit a common symbol in a regular object to
1711 override a definition in a shared object if the shared object
1712 symbol is a function or is weak. */
1713
1714 flip = NULL;
1715 if (!newdyn
1716 && (newdef
1717 || (bfd_is_com_section (sec)
1718 && (oldweak || oldfunc)))
1719 && olddyn
1720 && olddef
1721 && h->def_dynamic)
1722 {
1723 /* Change the hash table entry to undefined, and let
1724 _bfd_generic_link_add_one_symbol do the right thing with the
1725 new definition. */
1726
1727 h->root.type = bfd_link_hash_undefined;
1728 h->root.u.undef.abfd = h->root.u.def.section->owner;
1729 *size_change_ok = TRUE;
1730
1731 olddef = FALSE;
1732 olddyncommon = FALSE;
1733
1734 /* We again permit a type change when a common symbol may be
1735 overriding a function. */
1736
1737 if (bfd_is_com_section (sec))
1738 {
1739 if (oldfunc)
1740 {
1741 /* If a common symbol overrides a function, make sure
1742 that it isn't defined dynamically nor has type
1743 function. */
1744 h->def_dynamic = 0;
1745 h->type = STT_NOTYPE;
1746 }
1747 *type_change_ok = TRUE;
1748 }
1749
1750 if (hi->root.type == bfd_link_hash_indirect)
1751 flip = hi;
1752 else
1753 /* This union may have been set to be non-NULL when this symbol
1754 was seen in a dynamic object. We must force the union to be
1755 NULL, so that it is correct for a regular symbol. */
1756 h->verinfo.vertree = NULL;
1757 }
1758
1759 /* Handle the special case of a new common symbol merging with an
1760 old symbol that looks like it might be a common symbol defined in
1761 a shared object. Note that we have already handled the case in
1762 which a new common symbol should simply override the definition
1763 in the shared library. */
1764
1765 if (! newdyn
1766 && bfd_is_com_section (sec)
1767 && olddyncommon)
1768 {
1769 /* It would be best if we could set the hash table entry to a
1770 common symbol, but we don't know what to use for the section
1771 or the alignment. */
1772 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1773 bfd_link_hash_common, sym->st_size);
1774
1775 /* If the presumed common symbol in the dynamic object is
1776 larger, pretend that the new symbol has its size. */
1777
1778 if (h->size > *pvalue)
1779 *pvalue = h->size;
1780
1781 /* We need to remember the alignment required by the symbol
1782 in the dynamic object. */
1783 BFD_ASSERT (pold_alignment);
1784 *pold_alignment = h->root.u.def.section->alignment_power;
1785
1786 olddef = FALSE;
1787 olddyncommon = FALSE;
1788
1789 h->root.type = bfd_link_hash_undefined;
1790 h->root.u.undef.abfd = h->root.u.def.section->owner;
1791
1792 *size_change_ok = TRUE;
1793 *type_change_ok = TRUE;
1794
1795 if (hi->root.type == bfd_link_hash_indirect)
1796 flip = hi;
1797 else
1798 h->verinfo.vertree = NULL;
1799 }
1800
1801 if (flip != NULL)
1802 {
1803 /* Handle the case where we had a versioned symbol in a dynamic
1804 library and now find a definition in a normal object. In this
1805 case, we make the versioned symbol point to the normal one. */
1806 flip->root.type = h->root.type;
1807 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1808 h->root.type = bfd_link_hash_indirect;
1809 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1810 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1811 if (h->def_dynamic)
1812 {
1813 h->def_dynamic = 0;
1814 flip->ref_dynamic = 1;
1815 }
1816 }
1817
1818 return TRUE;
1819 }
1820
1821 /* This function is called to create an indirect symbol from the
1822 default for the symbol with the default version if needed. The
1823 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1824 set DYNSYM if the new indirect symbol is dynamic. */
1825
1826 static bfd_boolean
1827 _bfd_elf_add_default_symbol (bfd *abfd,
1828 struct bfd_link_info *info,
1829 struct elf_link_hash_entry *h,
1830 const char *name,
1831 Elf_Internal_Sym *sym,
1832 asection *sec,
1833 bfd_vma value,
1834 bfd **poldbfd,
1835 bfd_boolean *dynsym)
1836 {
1837 bfd_boolean type_change_ok;
1838 bfd_boolean size_change_ok;
1839 bfd_boolean skip;
1840 char *shortname;
1841 struct elf_link_hash_entry *hi;
1842 struct bfd_link_hash_entry *bh;
1843 const struct elf_backend_data *bed;
1844 bfd_boolean collect;
1845 bfd_boolean dynamic;
1846 bfd_boolean override;
1847 char *p;
1848 size_t len, shortlen;
1849 asection *tmp_sec;
1850 bfd_boolean matched;
1851
1852 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1853 return TRUE;
1854
1855 /* If this symbol has a version, and it is the default version, we
1856 create an indirect symbol from the default name to the fully
1857 decorated name. This will cause external references which do not
1858 specify a version to be bound to this version of the symbol. */
1859 p = strchr (name, ELF_VER_CHR);
1860 if (h->versioned == unknown)
1861 {
1862 if (p == NULL)
1863 {
1864 h->versioned = unversioned;
1865 return TRUE;
1866 }
1867 else
1868 {
1869 if (p[1] != ELF_VER_CHR)
1870 {
1871 h->versioned = versioned_hidden;
1872 return TRUE;
1873 }
1874 else
1875 h->versioned = versioned;
1876 }
1877 }
1878 else
1879 {
1880 /* PR ld/19073: We may see an unversioned definition after the
1881 default version. */
1882 if (p == NULL)
1883 return TRUE;
1884 }
1885
1886 bed = get_elf_backend_data (abfd);
1887 collect = bed->collect;
1888 dynamic = (abfd->flags & DYNAMIC) != 0;
1889
1890 shortlen = p - name;
1891 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1892 if (shortname == NULL)
1893 return FALSE;
1894 memcpy (shortname, name, shortlen);
1895 shortname[shortlen] = '\0';
1896
1897 /* We are going to create a new symbol. Merge it with any existing
1898 symbol with this name. For the purposes of the merge, act as
1899 though we were defining the symbol we just defined, although we
1900 actually going to define an indirect symbol. */
1901 type_change_ok = FALSE;
1902 size_change_ok = FALSE;
1903 matched = TRUE;
1904 tmp_sec = sec;
1905 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1906 &hi, poldbfd, NULL, NULL, &skip, &override,
1907 &type_change_ok, &size_change_ok, &matched))
1908 return FALSE;
1909
1910 if (skip)
1911 goto nondefault;
1912
1913 if (hi->def_regular)
1914 {
1915 /* If the undecorated symbol will have a version added by a
1916 script different to H, then don't indirect to/from the
1917 undecorated symbol. This isn't ideal because we may not yet
1918 have seen symbol versions, if given by a script on the
1919 command line rather than via --version-script. */
1920 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1921 {
1922 bfd_boolean hide;
1923
1924 hi->verinfo.vertree
1925 = bfd_find_version_for_sym (info->version_info,
1926 hi->root.root.string, &hide);
1927 if (hi->verinfo.vertree != NULL && hide)
1928 {
1929 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1930 goto nondefault;
1931 }
1932 }
1933 if (hi->verinfo.vertree != NULL
1934 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1935 goto nondefault;
1936 }
1937
1938 if (! override)
1939 {
1940 /* Add the default symbol if not performing a relocatable link. */
1941 if (! bfd_link_relocatable (info))
1942 {
1943 bh = &hi->root;
1944 if (! (_bfd_generic_link_add_one_symbol
1945 (info, abfd, shortname, BSF_INDIRECT,
1946 bfd_ind_section_ptr,
1947 0, name, FALSE, collect, &bh)))
1948 return FALSE;
1949 hi = (struct elf_link_hash_entry *) bh;
1950 }
1951 }
1952 else
1953 {
1954 /* In this case the symbol named SHORTNAME is overriding the
1955 indirect symbol we want to add. We were planning on making
1956 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1957 is the name without a version. NAME is the fully versioned
1958 name, and it is the default version.
1959
1960 Overriding means that we already saw a definition for the
1961 symbol SHORTNAME in a regular object, and it is overriding
1962 the symbol defined in the dynamic object.
1963
1964 When this happens, we actually want to change NAME, the
1965 symbol we just added, to refer to SHORTNAME. This will cause
1966 references to NAME in the shared object to become references
1967 to SHORTNAME in the regular object. This is what we expect
1968 when we override a function in a shared object: that the
1969 references in the shared object will be mapped to the
1970 definition in the regular object. */
1971
1972 while (hi->root.type == bfd_link_hash_indirect
1973 || hi->root.type == bfd_link_hash_warning)
1974 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1975
1976 h->root.type = bfd_link_hash_indirect;
1977 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1978 if (h->def_dynamic)
1979 {
1980 h->def_dynamic = 0;
1981 hi->ref_dynamic = 1;
1982 if (hi->ref_regular
1983 || hi->def_regular)
1984 {
1985 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1986 return FALSE;
1987 }
1988 }
1989
1990 /* Now set HI to H, so that the following code will set the
1991 other fields correctly. */
1992 hi = h;
1993 }
1994
1995 /* Check if HI is a warning symbol. */
1996 if (hi->root.type == bfd_link_hash_warning)
1997 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1998
1999 /* If there is a duplicate definition somewhere, then HI may not
2000 point to an indirect symbol. We will have reported an error to
2001 the user in that case. */
2002
2003 if (hi->root.type == bfd_link_hash_indirect)
2004 {
2005 struct elf_link_hash_entry *ht;
2006
2007 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2008 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2009
2010 /* A reference to the SHORTNAME symbol from a dynamic library
2011 will be satisfied by the versioned symbol at runtime. In
2012 effect, we have a reference to the versioned symbol. */
2013 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2014 hi->dynamic_def |= ht->dynamic_def;
2015
2016 /* See if the new flags lead us to realize that the symbol must
2017 be dynamic. */
2018 if (! *dynsym)
2019 {
2020 if (! dynamic)
2021 {
2022 if (! bfd_link_executable (info)
2023 || hi->def_dynamic
2024 || hi->ref_dynamic)
2025 *dynsym = TRUE;
2026 }
2027 else
2028 {
2029 if (hi->ref_regular)
2030 *dynsym = TRUE;
2031 }
2032 }
2033 }
2034
2035 /* We also need to define an indirection from the nondefault version
2036 of the symbol. */
2037
2038 nondefault:
2039 len = strlen (name);
2040 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2041 if (shortname == NULL)
2042 return FALSE;
2043 memcpy (shortname, name, shortlen);
2044 memcpy (shortname + shortlen, p + 1, len - shortlen);
2045
2046 /* Once again, merge with any existing symbol. */
2047 type_change_ok = FALSE;
2048 size_change_ok = FALSE;
2049 tmp_sec = sec;
2050 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2051 &hi, poldbfd, NULL, NULL, &skip, &override,
2052 &type_change_ok, &size_change_ok, &matched))
2053 return FALSE;
2054
2055 if (skip)
2056 return TRUE;
2057
2058 if (override)
2059 {
2060 /* Here SHORTNAME is a versioned name, so we don't expect to see
2061 the type of override we do in the case above unless it is
2062 overridden by a versioned definition. */
2063 if (hi->root.type != bfd_link_hash_defined
2064 && hi->root.type != bfd_link_hash_defweak)
2065 _bfd_error_handler
2066 /* xgettext:c-format */
2067 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2068 abfd, shortname);
2069 }
2070 else
2071 {
2072 bh = &hi->root;
2073 if (! (_bfd_generic_link_add_one_symbol
2074 (info, abfd, shortname, BSF_INDIRECT,
2075 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
2076 return FALSE;
2077 hi = (struct elf_link_hash_entry *) bh;
2078
2079 /* If there is a duplicate definition somewhere, then HI may not
2080 point to an indirect symbol. We will have reported an error
2081 to the user in that case. */
2082
2083 if (hi->root.type == bfd_link_hash_indirect)
2084 {
2085 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2086 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2087 hi->dynamic_def |= h->dynamic_def;
2088
2089 /* See if the new flags lead us to realize that the symbol
2090 must be dynamic. */
2091 if (! *dynsym)
2092 {
2093 if (! dynamic)
2094 {
2095 if (! bfd_link_executable (info)
2096 || hi->ref_dynamic)
2097 *dynsym = TRUE;
2098 }
2099 else
2100 {
2101 if (hi->ref_regular)
2102 *dynsym = TRUE;
2103 }
2104 }
2105 }
2106 }
2107
2108 return TRUE;
2109 }
2110 \f
2111 /* This routine is used to export all defined symbols into the dynamic
2112 symbol table. It is called via elf_link_hash_traverse. */
2113
2114 static bfd_boolean
2115 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2116 {
2117 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2118
2119 /* Ignore indirect symbols. These are added by the versioning code. */
2120 if (h->root.type == bfd_link_hash_indirect)
2121 return TRUE;
2122
2123 /* Ignore this if we won't export it. */
2124 if (!eif->info->export_dynamic && !h->dynamic)
2125 return TRUE;
2126
2127 if (h->dynindx == -1
2128 && (h->def_regular || h->ref_regular)
2129 && ! bfd_hide_sym_by_version (eif->info->version_info,
2130 h->root.root.string))
2131 {
2132 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2133 {
2134 eif->failed = TRUE;
2135 return FALSE;
2136 }
2137 }
2138
2139 return TRUE;
2140 }
2141 \f
2142 /* Look through the symbols which are defined in other shared
2143 libraries and referenced here. Update the list of version
2144 dependencies. This will be put into the .gnu.version_r section.
2145 This function is called via elf_link_hash_traverse. */
2146
2147 static bfd_boolean
2148 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2149 void *data)
2150 {
2151 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2152 Elf_Internal_Verneed *t;
2153 Elf_Internal_Vernaux *a;
2154 bfd_size_type amt;
2155
2156 /* We only care about symbols defined in shared objects with version
2157 information. */
2158 if (!h->def_dynamic
2159 || h->def_regular
2160 || h->dynindx == -1
2161 || h->verinfo.verdef == NULL
2162 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2163 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2164 return TRUE;
2165
2166 /* See if we already know about this version. */
2167 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2168 t != NULL;
2169 t = t->vn_nextref)
2170 {
2171 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2172 continue;
2173
2174 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2175 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2176 return TRUE;
2177
2178 break;
2179 }
2180
2181 /* This is a new version. Add it to tree we are building. */
2182
2183 if (t == NULL)
2184 {
2185 amt = sizeof *t;
2186 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2187 if (t == NULL)
2188 {
2189 rinfo->failed = TRUE;
2190 return FALSE;
2191 }
2192
2193 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2194 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2195 elf_tdata (rinfo->info->output_bfd)->verref = t;
2196 }
2197
2198 amt = sizeof *a;
2199 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2200 if (a == NULL)
2201 {
2202 rinfo->failed = TRUE;
2203 return FALSE;
2204 }
2205
2206 /* Note that we are copying a string pointer here, and testing it
2207 above. If bfd_elf_string_from_elf_section is ever changed to
2208 discard the string data when low in memory, this will have to be
2209 fixed. */
2210 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2211
2212 a->vna_flags = h->verinfo.verdef->vd_flags;
2213 a->vna_nextptr = t->vn_auxptr;
2214
2215 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2216 ++rinfo->vers;
2217
2218 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2219
2220 t->vn_auxptr = a;
2221
2222 return TRUE;
2223 }
2224
2225 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2226 hidden. Set *T_P to NULL if there is no match. */
2227
2228 static bfd_boolean
2229 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2230 struct elf_link_hash_entry *h,
2231 const char *version_p,
2232 struct bfd_elf_version_tree **t_p,
2233 bfd_boolean *hide)
2234 {
2235 struct bfd_elf_version_tree *t;
2236
2237 /* Look for the version. If we find it, it is no longer weak. */
2238 for (t = info->version_info; t != NULL; t = t->next)
2239 {
2240 if (strcmp (t->name, version_p) == 0)
2241 {
2242 size_t len;
2243 char *alc;
2244 struct bfd_elf_version_expr *d;
2245
2246 len = version_p - h->root.root.string;
2247 alc = (char *) bfd_malloc (len);
2248 if (alc == NULL)
2249 return FALSE;
2250 memcpy (alc, h->root.root.string, len - 1);
2251 alc[len - 1] = '\0';
2252 if (alc[len - 2] == ELF_VER_CHR)
2253 alc[len - 2] = '\0';
2254
2255 h->verinfo.vertree = t;
2256 t->used = TRUE;
2257 d = NULL;
2258
2259 if (t->globals.list != NULL)
2260 d = (*t->match) (&t->globals, NULL, alc);
2261
2262 /* See if there is anything to force this symbol to
2263 local scope. */
2264 if (d == NULL && t->locals.list != NULL)
2265 {
2266 d = (*t->match) (&t->locals, NULL, alc);
2267 if (d != NULL
2268 && h->dynindx != -1
2269 && ! info->export_dynamic)
2270 *hide = TRUE;
2271 }
2272
2273 free (alc);
2274 break;
2275 }
2276 }
2277
2278 *t_p = t;
2279
2280 return TRUE;
2281 }
2282
2283 /* Return TRUE if the symbol H is hidden by version script. */
2284
2285 bfd_boolean
2286 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2287 struct elf_link_hash_entry *h)
2288 {
2289 const char *p;
2290 bfd_boolean hide = FALSE;
2291 const struct elf_backend_data *bed
2292 = get_elf_backend_data (info->output_bfd);
2293
2294 /* Version script only hides symbols defined in regular objects. */
2295 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2296 return TRUE;
2297
2298 p = strchr (h->root.root.string, ELF_VER_CHR);
2299 if (p != NULL && h->verinfo.vertree == NULL)
2300 {
2301 struct bfd_elf_version_tree *t;
2302
2303 ++p;
2304 if (*p == ELF_VER_CHR)
2305 ++p;
2306
2307 if (*p != '\0'
2308 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2309 && hide)
2310 {
2311 if (hide)
2312 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2313 return TRUE;
2314 }
2315 }
2316
2317 /* If we don't have a version for this symbol, see if we can find
2318 something. */
2319 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2320 {
2321 h->verinfo.vertree
2322 = bfd_find_version_for_sym (info->version_info,
2323 h->root.root.string, &hide);
2324 if (h->verinfo.vertree != NULL && hide)
2325 {
2326 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2327 return TRUE;
2328 }
2329 }
2330
2331 return FALSE;
2332 }
2333
2334 /* Figure out appropriate versions for all the symbols. We may not
2335 have the version number script until we have read all of the input
2336 files, so until that point we don't know which symbols should be
2337 local. This function is called via elf_link_hash_traverse. */
2338
2339 static bfd_boolean
2340 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2341 {
2342 struct elf_info_failed *sinfo;
2343 struct bfd_link_info *info;
2344 const struct elf_backend_data *bed;
2345 struct elf_info_failed eif;
2346 char *p;
2347 bfd_boolean hide;
2348
2349 sinfo = (struct elf_info_failed *) data;
2350 info = sinfo->info;
2351
2352 /* Fix the symbol flags. */
2353 eif.failed = FALSE;
2354 eif.info = info;
2355 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2356 {
2357 if (eif.failed)
2358 sinfo->failed = TRUE;
2359 return FALSE;
2360 }
2361
2362 bed = get_elf_backend_data (info->output_bfd);
2363
2364 /* We only need version numbers for symbols defined in regular
2365 objects. */
2366 if (!h->def_regular)
2367 {
2368 /* Hide symbols defined in discarded input sections. */
2369 if ((h->root.type == bfd_link_hash_defined
2370 || h->root.type == bfd_link_hash_defweak)
2371 && discarded_section (h->root.u.def.section))
2372 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2373 return TRUE;
2374 }
2375
2376 hide = FALSE;
2377 p = strchr (h->root.root.string, ELF_VER_CHR);
2378 if (p != NULL && h->verinfo.vertree == NULL)
2379 {
2380 struct bfd_elf_version_tree *t;
2381
2382 ++p;
2383 if (*p == ELF_VER_CHR)
2384 ++p;
2385
2386 /* If there is no version string, we can just return out. */
2387 if (*p == '\0')
2388 return TRUE;
2389
2390 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2391 {
2392 sinfo->failed = TRUE;
2393 return FALSE;
2394 }
2395
2396 if (hide)
2397 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2398
2399 /* If we are building an application, we need to create a
2400 version node for this version. */
2401 if (t == NULL && bfd_link_executable (info))
2402 {
2403 struct bfd_elf_version_tree **pp;
2404 int version_index;
2405
2406 /* If we aren't going to export this symbol, we don't need
2407 to worry about it. */
2408 if (h->dynindx == -1)
2409 return TRUE;
2410
2411 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2412 sizeof *t);
2413 if (t == NULL)
2414 {
2415 sinfo->failed = TRUE;
2416 return FALSE;
2417 }
2418
2419 t->name = p;
2420 t->name_indx = (unsigned int) -1;
2421 t->used = TRUE;
2422
2423 version_index = 1;
2424 /* Don't count anonymous version tag. */
2425 if (sinfo->info->version_info != NULL
2426 && sinfo->info->version_info->vernum == 0)
2427 version_index = 0;
2428 for (pp = &sinfo->info->version_info;
2429 *pp != NULL;
2430 pp = &(*pp)->next)
2431 ++version_index;
2432 t->vernum = version_index;
2433
2434 *pp = t;
2435
2436 h->verinfo.vertree = t;
2437 }
2438 else if (t == NULL)
2439 {
2440 /* We could not find the version for a symbol when
2441 generating a shared archive. Return an error. */
2442 _bfd_error_handler
2443 /* xgettext:c-format */
2444 (_("%pB: version node not found for symbol %s"),
2445 info->output_bfd, h->root.root.string);
2446 bfd_set_error (bfd_error_bad_value);
2447 sinfo->failed = TRUE;
2448 return FALSE;
2449 }
2450 }
2451
2452 /* If we don't have a version for this symbol, see if we can find
2453 something. */
2454 if (!hide
2455 && h->verinfo.vertree == NULL
2456 && sinfo->info->version_info != NULL)
2457 {
2458 h->verinfo.vertree
2459 = bfd_find_version_for_sym (sinfo->info->version_info,
2460 h->root.root.string, &hide);
2461 if (h->verinfo.vertree != NULL && hide)
2462 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2463 }
2464
2465 return TRUE;
2466 }
2467 \f
2468 /* Read and swap the relocs from the section indicated by SHDR. This
2469 may be either a REL or a RELA section. The relocations are
2470 translated into RELA relocations and stored in INTERNAL_RELOCS,
2471 which should have already been allocated to contain enough space.
2472 The EXTERNAL_RELOCS are a buffer where the external form of the
2473 relocations should be stored.
2474
2475 Returns FALSE if something goes wrong. */
2476
2477 static bfd_boolean
2478 elf_link_read_relocs_from_section (bfd *abfd,
2479 asection *sec,
2480 Elf_Internal_Shdr *shdr,
2481 void *external_relocs,
2482 Elf_Internal_Rela *internal_relocs)
2483 {
2484 const struct elf_backend_data *bed;
2485 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2486 const bfd_byte *erela;
2487 const bfd_byte *erelaend;
2488 Elf_Internal_Rela *irela;
2489 Elf_Internal_Shdr *symtab_hdr;
2490 size_t nsyms;
2491
2492 /* Position ourselves at the start of the section. */
2493 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2494 return FALSE;
2495
2496 /* Read the relocations. */
2497 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2498 return FALSE;
2499
2500 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2501 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2502
2503 bed = get_elf_backend_data (abfd);
2504
2505 /* Convert the external relocations to the internal format. */
2506 if (shdr->sh_entsize == bed->s->sizeof_rel)
2507 swap_in = bed->s->swap_reloc_in;
2508 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2509 swap_in = bed->s->swap_reloca_in;
2510 else
2511 {
2512 bfd_set_error (bfd_error_wrong_format);
2513 return FALSE;
2514 }
2515
2516 erela = (const bfd_byte *) external_relocs;
2517 erelaend = erela + shdr->sh_size;
2518 irela = internal_relocs;
2519 while (erela < erelaend)
2520 {
2521 bfd_vma r_symndx;
2522
2523 (*swap_in) (abfd, erela, irela);
2524 r_symndx = ELF32_R_SYM (irela->r_info);
2525 if (bed->s->arch_size == 64)
2526 r_symndx >>= 24;
2527 if (nsyms > 0)
2528 {
2529 if ((size_t) r_symndx >= nsyms)
2530 {
2531 _bfd_error_handler
2532 /* xgettext:c-format */
2533 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2534 " for offset %#" PRIx64 " in section `%pA'"),
2535 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2536 (uint64_t) irela->r_offset, sec);
2537 bfd_set_error (bfd_error_bad_value);
2538 return FALSE;
2539 }
2540 }
2541 else if (r_symndx != STN_UNDEF)
2542 {
2543 _bfd_error_handler
2544 /* xgettext:c-format */
2545 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2546 " for offset %#" PRIx64 " in section `%pA'"
2547 " when the object file has no symbol table"),
2548 abfd, (uint64_t) r_symndx,
2549 (uint64_t) irela->r_offset, sec);
2550 bfd_set_error (bfd_error_bad_value);
2551 return FALSE;
2552 }
2553 irela += bed->s->int_rels_per_ext_rel;
2554 erela += shdr->sh_entsize;
2555 }
2556
2557 return TRUE;
2558 }
2559
2560 /* Read and swap the relocs for a section O. They may have been
2561 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2562 not NULL, they are used as buffers to read into. They are known to
2563 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2564 the return value is allocated using either malloc or bfd_alloc,
2565 according to the KEEP_MEMORY argument. If O has two relocation
2566 sections (both REL and RELA relocations), then the REL_HDR
2567 relocations will appear first in INTERNAL_RELOCS, followed by the
2568 RELA_HDR relocations. */
2569
2570 Elf_Internal_Rela *
2571 _bfd_elf_link_read_relocs (bfd *abfd,
2572 asection *o,
2573 void *external_relocs,
2574 Elf_Internal_Rela *internal_relocs,
2575 bfd_boolean keep_memory)
2576 {
2577 void *alloc1 = NULL;
2578 Elf_Internal_Rela *alloc2 = NULL;
2579 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2580 struct bfd_elf_section_data *esdo = elf_section_data (o);
2581 Elf_Internal_Rela *internal_rela_relocs;
2582
2583 if (esdo->relocs != NULL)
2584 return esdo->relocs;
2585
2586 if (o->reloc_count == 0)
2587 return NULL;
2588
2589 if (internal_relocs == NULL)
2590 {
2591 bfd_size_type size;
2592
2593 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2594 if (keep_memory)
2595 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2596 else
2597 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2598 if (internal_relocs == NULL)
2599 goto error_return;
2600 }
2601
2602 if (external_relocs == NULL)
2603 {
2604 bfd_size_type size = 0;
2605
2606 if (esdo->rel.hdr)
2607 size += esdo->rel.hdr->sh_size;
2608 if (esdo->rela.hdr)
2609 size += esdo->rela.hdr->sh_size;
2610
2611 alloc1 = bfd_malloc (size);
2612 if (alloc1 == NULL)
2613 goto error_return;
2614 external_relocs = alloc1;
2615 }
2616
2617 internal_rela_relocs = internal_relocs;
2618 if (esdo->rel.hdr)
2619 {
2620 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2621 external_relocs,
2622 internal_relocs))
2623 goto error_return;
2624 external_relocs = (((bfd_byte *) external_relocs)
2625 + esdo->rel.hdr->sh_size);
2626 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2627 * bed->s->int_rels_per_ext_rel);
2628 }
2629
2630 if (esdo->rela.hdr
2631 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2632 external_relocs,
2633 internal_rela_relocs)))
2634 goto error_return;
2635
2636 /* Cache the results for next time, if we can. */
2637 if (keep_memory)
2638 esdo->relocs = internal_relocs;
2639
2640 if (alloc1 != NULL)
2641 free (alloc1);
2642
2643 /* Don't free alloc2, since if it was allocated we are passing it
2644 back (under the name of internal_relocs). */
2645
2646 return internal_relocs;
2647
2648 error_return:
2649 if (alloc1 != NULL)
2650 free (alloc1);
2651 if (alloc2 != NULL)
2652 {
2653 if (keep_memory)
2654 bfd_release (abfd, alloc2);
2655 else
2656 free (alloc2);
2657 }
2658 return NULL;
2659 }
2660
2661 /* Compute the size of, and allocate space for, REL_HDR which is the
2662 section header for a section containing relocations for O. */
2663
2664 static bfd_boolean
2665 _bfd_elf_link_size_reloc_section (bfd *abfd,
2666 struct bfd_elf_section_reloc_data *reldata)
2667 {
2668 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2669
2670 /* That allows us to calculate the size of the section. */
2671 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2672
2673 /* The contents field must last into write_object_contents, so we
2674 allocate it with bfd_alloc rather than malloc. Also since we
2675 cannot be sure that the contents will actually be filled in,
2676 we zero the allocated space. */
2677 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2678 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2679 return FALSE;
2680
2681 if (reldata->hashes == NULL && reldata->count)
2682 {
2683 struct elf_link_hash_entry **p;
2684
2685 p = ((struct elf_link_hash_entry **)
2686 bfd_zmalloc (reldata->count * sizeof (*p)));
2687 if (p == NULL)
2688 return FALSE;
2689
2690 reldata->hashes = p;
2691 }
2692
2693 return TRUE;
2694 }
2695
2696 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2697 originated from the section given by INPUT_REL_HDR) to the
2698 OUTPUT_BFD. */
2699
2700 bfd_boolean
2701 _bfd_elf_link_output_relocs (bfd *output_bfd,
2702 asection *input_section,
2703 Elf_Internal_Shdr *input_rel_hdr,
2704 Elf_Internal_Rela *internal_relocs,
2705 struct elf_link_hash_entry **rel_hash
2706 ATTRIBUTE_UNUSED)
2707 {
2708 Elf_Internal_Rela *irela;
2709 Elf_Internal_Rela *irelaend;
2710 bfd_byte *erel;
2711 struct bfd_elf_section_reloc_data *output_reldata;
2712 asection *output_section;
2713 const struct elf_backend_data *bed;
2714 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2715 struct bfd_elf_section_data *esdo;
2716
2717 output_section = input_section->output_section;
2718
2719 bed = get_elf_backend_data (output_bfd);
2720 esdo = elf_section_data (output_section);
2721 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2722 {
2723 output_reldata = &esdo->rel;
2724 swap_out = bed->s->swap_reloc_out;
2725 }
2726 else if (esdo->rela.hdr
2727 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2728 {
2729 output_reldata = &esdo->rela;
2730 swap_out = bed->s->swap_reloca_out;
2731 }
2732 else
2733 {
2734 _bfd_error_handler
2735 /* xgettext:c-format */
2736 (_("%pB: relocation size mismatch in %pB section %pA"),
2737 output_bfd, input_section->owner, input_section);
2738 bfd_set_error (bfd_error_wrong_format);
2739 return FALSE;
2740 }
2741
2742 erel = output_reldata->hdr->contents;
2743 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2744 irela = internal_relocs;
2745 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2746 * bed->s->int_rels_per_ext_rel);
2747 while (irela < irelaend)
2748 {
2749 (*swap_out) (output_bfd, irela, erel);
2750 irela += bed->s->int_rels_per_ext_rel;
2751 erel += input_rel_hdr->sh_entsize;
2752 }
2753
2754 /* Bump the counter, so that we know where to add the next set of
2755 relocations. */
2756 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2757
2758 return TRUE;
2759 }
2760 \f
2761 /* Make weak undefined symbols in PIE dynamic. */
2762
2763 bfd_boolean
2764 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2765 struct elf_link_hash_entry *h)
2766 {
2767 if (bfd_link_pie (info)
2768 && h->dynindx == -1
2769 && h->root.type == bfd_link_hash_undefweak)
2770 return bfd_elf_link_record_dynamic_symbol (info, h);
2771
2772 return TRUE;
2773 }
2774
2775 /* Fix up the flags for a symbol. This handles various cases which
2776 can only be fixed after all the input files are seen. This is
2777 currently called by both adjust_dynamic_symbol and
2778 assign_sym_version, which is unnecessary but perhaps more robust in
2779 the face of future changes. */
2780
2781 static bfd_boolean
2782 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2783 struct elf_info_failed *eif)
2784 {
2785 const struct elf_backend_data *bed;
2786
2787 /* If this symbol was mentioned in a non-ELF file, try to set
2788 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2789 permit a non-ELF file to correctly refer to a symbol defined in
2790 an ELF dynamic object. */
2791 if (h->non_elf)
2792 {
2793 while (h->root.type == bfd_link_hash_indirect)
2794 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2795
2796 if (h->root.type != bfd_link_hash_defined
2797 && h->root.type != bfd_link_hash_defweak)
2798 {
2799 h->ref_regular = 1;
2800 h->ref_regular_nonweak = 1;
2801 }
2802 else
2803 {
2804 if (h->root.u.def.section->owner != NULL
2805 && (bfd_get_flavour (h->root.u.def.section->owner)
2806 == bfd_target_elf_flavour))
2807 {
2808 h->ref_regular = 1;
2809 h->ref_regular_nonweak = 1;
2810 }
2811 else
2812 h->def_regular = 1;
2813 }
2814
2815 if (h->dynindx == -1
2816 && (h->def_dynamic
2817 || h->ref_dynamic))
2818 {
2819 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2820 {
2821 eif->failed = TRUE;
2822 return FALSE;
2823 }
2824 }
2825 }
2826 else
2827 {
2828 /* Unfortunately, NON_ELF is only correct if the symbol
2829 was first seen in a non-ELF file. Fortunately, if the symbol
2830 was first seen in an ELF file, we're probably OK unless the
2831 symbol was defined in a non-ELF file. Catch that case here.
2832 FIXME: We're still in trouble if the symbol was first seen in
2833 a dynamic object, and then later in a non-ELF regular object. */
2834 if ((h->root.type == bfd_link_hash_defined
2835 || h->root.type == bfd_link_hash_defweak)
2836 && !h->def_regular
2837 && (h->root.u.def.section->owner != NULL
2838 ? (bfd_get_flavour (h->root.u.def.section->owner)
2839 != bfd_target_elf_flavour)
2840 : (bfd_is_abs_section (h->root.u.def.section)
2841 && !h->def_dynamic)))
2842 h->def_regular = 1;
2843 }
2844
2845 /* Backend specific symbol fixup. */
2846 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2847 if (bed->elf_backend_fixup_symbol
2848 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2849 return FALSE;
2850
2851 /* If this is a final link, and the symbol was defined as a common
2852 symbol in a regular object file, and there was no definition in
2853 any dynamic object, then the linker will have allocated space for
2854 the symbol in a common section but the DEF_REGULAR
2855 flag will not have been set. */
2856 if (h->root.type == bfd_link_hash_defined
2857 && !h->def_regular
2858 && h->ref_regular
2859 && !h->def_dynamic
2860 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2861 h->def_regular = 1;
2862
2863 /* Symbols defined in discarded sections shouldn't be dynamic. */
2864 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
2865 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2866
2867 /* If a weak undefined symbol has non-default visibility, we also
2868 hide it from the dynamic linker. */
2869 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2870 && h->root.type == bfd_link_hash_undefweak)
2871 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2872
2873 /* A hidden versioned symbol in executable should be forced local if
2874 it is is locally defined, not referenced by shared library and not
2875 exported. */
2876 else if (bfd_link_executable (eif->info)
2877 && h->versioned == versioned_hidden
2878 && !eif->info->export_dynamic
2879 && !h->dynamic
2880 && !h->ref_dynamic
2881 && h->def_regular)
2882 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2883
2884 /* If -Bsymbolic was used (which means to bind references to global
2885 symbols to the definition within the shared object), and this
2886 symbol was defined in a regular object, then it actually doesn't
2887 need a PLT entry. Likewise, if the symbol has non-default
2888 visibility. If the symbol has hidden or internal visibility, we
2889 will force it local. */
2890 else if (h->needs_plt
2891 && bfd_link_pic (eif->info)
2892 && is_elf_hash_table (eif->info->hash)
2893 && (SYMBOLIC_BIND (eif->info, h)
2894 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2895 && h->def_regular)
2896 {
2897 bfd_boolean force_local;
2898
2899 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2900 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2901 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2902 }
2903
2904 /* If this is a weak defined symbol in a dynamic object, and we know
2905 the real definition in the dynamic object, copy interesting flags
2906 over to the real definition. */
2907 if (h->is_weakalias)
2908 {
2909 struct elf_link_hash_entry *def = weakdef (h);
2910
2911 /* If the real definition is defined by a regular object file,
2912 don't do anything special. See the longer description in
2913 _bfd_elf_adjust_dynamic_symbol, below. */
2914 if (def->def_regular)
2915 {
2916 h = def;
2917 while ((h = h->u.alias) != def)
2918 h->is_weakalias = 0;
2919 }
2920 else
2921 {
2922 while (h->root.type == bfd_link_hash_indirect)
2923 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2924 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2925 || h->root.type == bfd_link_hash_defweak);
2926 BFD_ASSERT (def->def_dynamic);
2927 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
2928 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
2929 }
2930 }
2931
2932 return TRUE;
2933 }
2934
2935 /* Make the backend pick a good value for a dynamic symbol. This is
2936 called via elf_link_hash_traverse, and also calls itself
2937 recursively. */
2938
2939 static bfd_boolean
2940 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2941 {
2942 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2943 struct elf_link_hash_table *htab;
2944 const struct elf_backend_data *bed;
2945
2946 if (! is_elf_hash_table (eif->info->hash))
2947 return FALSE;
2948
2949 /* Ignore indirect symbols. These are added by the versioning code. */
2950 if (h->root.type == bfd_link_hash_indirect)
2951 return TRUE;
2952
2953 /* Fix the symbol flags. */
2954 if (! _bfd_elf_fix_symbol_flags (h, eif))
2955 return FALSE;
2956
2957 htab = elf_hash_table (eif->info);
2958 bed = get_elf_backend_data (htab->dynobj);
2959
2960 if (h->root.type == bfd_link_hash_undefweak)
2961 {
2962 if (eif->info->dynamic_undefined_weak == 0)
2963 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2964 else if (eif->info->dynamic_undefined_weak > 0
2965 && h->ref_regular
2966 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2967 && !bfd_hide_sym_by_version (eif->info->version_info,
2968 h->root.root.string))
2969 {
2970 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2971 {
2972 eif->failed = TRUE;
2973 return FALSE;
2974 }
2975 }
2976 }
2977
2978 /* If this symbol does not require a PLT entry, and it is not
2979 defined by a dynamic object, or is not referenced by a regular
2980 object, ignore it. We do have to handle a weak defined symbol,
2981 even if no regular object refers to it, if we decided to add it
2982 to the dynamic symbol table. FIXME: Do we normally need to worry
2983 about symbols which are defined by one dynamic object and
2984 referenced by another one? */
2985 if (!h->needs_plt
2986 && h->type != STT_GNU_IFUNC
2987 && (h->def_regular
2988 || !h->def_dynamic
2989 || (!h->ref_regular
2990 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
2991 {
2992 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2993 return TRUE;
2994 }
2995
2996 /* If we've already adjusted this symbol, don't do it again. This
2997 can happen via a recursive call. */
2998 if (h->dynamic_adjusted)
2999 return TRUE;
3000
3001 /* Don't look at this symbol again. Note that we must set this
3002 after checking the above conditions, because we may look at a
3003 symbol once, decide not to do anything, and then get called
3004 recursively later after REF_REGULAR is set below. */
3005 h->dynamic_adjusted = 1;
3006
3007 /* If this is a weak definition, and we know a real definition, and
3008 the real symbol is not itself defined by a regular object file,
3009 then get a good value for the real definition. We handle the
3010 real symbol first, for the convenience of the backend routine.
3011
3012 Note that there is a confusing case here. If the real definition
3013 is defined by a regular object file, we don't get the real symbol
3014 from the dynamic object, but we do get the weak symbol. If the
3015 processor backend uses a COPY reloc, then if some routine in the
3016 dynamic object changes the real symbol, we will not see that
3017 change in the corresponding weak symbol. This is the way other
3018 ELF linkers work as well, and seems to be a result of the shared
3019 library model.
3020
3021 I will clarify this issue. Most SVR4 shared libraries define the
3022 variable _timezone and define timezone as a weak synonym. The
3023 tzset call changes _timezone. If you write
3024 extern int timezone;
3025 int _timezone = 5;
3026 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3027 you might expect that, since timezone is a synonym for _timezone,
3028 the same number will print both times. However, if the processor
3029 backend uses a COPY reloc, then actually timezone will be copied
3030 into your process image, and, since you define _timezone
3031 yourself, _timezone will not. Thus timezone and _timezone will
3032 wind up at different memory locations. The tzset call will set
3033 _timezone, leaving timezone unchanged. */
3034
3035 if (h->is_weakalias)
3036 {
3037 struct elf_link_hash_entry *def = weakdef (h);
3038
3039 /* If we get to this point, there is an implicit reference to
3040 the alias by a regular object file via the weak symbol H. */
3041 def->ref_regular = 1;
3042
3043 /* Ensure that the backend adjust_dynamic_symbol function sees
3044 the strong alias before H by recursively calling ourselves. */
3045 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3046 return FALSE;
3047 }
3048
3049 /* If a symbol has no type and no size and does not require a PLT
3050 entry, then we are probably about to do the wrong thing here: we
3051 are probably going to create a COPY reloc for an empty object.
3052 This case can arise when a shared object is built with assembly
3053 code, and the assembly code fails to set the symbol type. */
3054 if (h->size == 0
3055 && h->type == STT_NOTYPE
3056 && !h->needs_plt)
3057 _bfd_error_handler
3058 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3059 h->root.root.string);
3060
3061 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3062 {
3063 eif->failed = TRUE;
3064 return FALSE;
3065 }
3066
3067 return TRUE;
3068 }
3069
3070 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3071 DYNBSS. */
3072
3073 bfd_boolean
3074 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3075 struct elf_link_hash_entry *h,
3076 asection *dynbss)
3077 {
3078 unsigned int power_of_two;
3079 bfd_vma mask;
3080 asection *sec = h->root.u.def.section;
3081
3082 /* The section alignment of the definition is the maximum alignment
3083 requirement of symbols defined in the section. Since we don't
3084 know the symbol alignment requirement, we start with the
3085 maximum alignment and check low bits of the symbol address
3086 for the minimum alignment. */
3087 power_of_two = bfd_get_section_alignment (sec->owner, sec);
3088 mask = ((bfd_vma) 1 << power_of_two) - 1;
3089 while ((h->root.u.def.value & mask) != 0)
3090 {
3091 mask >>= 1;
3092 --power_of_two;
3093 }
3094
3095 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
3096 dynbss))
3097 {
3098 /* Adjust the section alignment if needed. */
3099 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
3100 power_of_two))
3101 return FALSE;
3102 }
3103
3104 /* We make sure that the symbol will be aligned properly. */
3105 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3106
3107 /* Define the symbol as being at this point in DYNBSS. */
3108 h->root.u.def.section = dynbss;
3109 h->root.u.def.value = dynbss->size;
3110
3111 /* Increment the size of DYNBSS to make room for the symbol. */
3112 dynbss->size += h->size;
3113
3114 /* No error if extern_protected_data is true. */
3115 if (h->protected_def
3116 && (!info->extern_protected_data
3117 || (info->extern_protected_data < 0
3118 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3119 info->callbacks->einfo
3120 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3121 h->root.root.string);
3122
3123 return TRUE;
3124 }
3125
3126 /* Adjust all external symbols pointing into SEC_MERGE sections
3127 to reflect the object merging within the sections. */
3128
3129 static bfd_boolean
3130 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3131 {
3132 asection *sec;
3133
3134 if ((h->root.type == bfd_link_hash_defined
3135 || h->root.type == bfd_link_hash_defweak)
3136 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3137 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3138 {
3139 bfd *output_bfd = (bfd *) data;
3140
3141 h->root.u.def.value =
3142 _bfd_merged_section_offset (output_bfd,
3143 &h->root.u.def.section,
3144 elf_section_data (sec)->sec_info,
3145 h->root.u.def.value);
3146 }
3147
3148 return TRUE;
3149 }
3150
3151 /* Returns false if the symbol referred to by H should be considered
3152 to resolve local to the current module, and true if it should be
3153 considered to bind dynamically. */
3154
3155 bfd_boolean
3156 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3157 struct bfd_link_info *info,
3158 bfd_boolean not_local_protected)
3159 {
3160 bfd_boolean binding_stays_local_p;
3161 const struct elf_backend_data *bed;
3162 struct elf_link_hash_table *hash_table;
3163
3164 if (h == NULL)
3165 return FALSE;
3166
3167 while (h->root.type == bfd_link_hash_indirect
3168 || h->root.type == bfd_link_hash_warning)
3169 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3170
3171 /* If it was forced local, then clearly it's not dynamic. */
3172 if (h->dynindx == -1)
3173 return FALSE;
3174 if (h->forced_local)
3175 return FALSE;
3176
3177 /* Identify the cases where name binding rules say that a
3178 visible symbol resolves locally. */
3179 binding_stays_local_p = (bfd_link_executable (info)
3180 || SYMBOLIC_BIND (info, h));
3181
3182 switch (ELF_ST_VISIBILITY (h->other))
3183 {
3184 case STV_INTERNAL:
3185 case STV_HIDDEN:
3186 return FALSE;
3187
3188 case STV_PROTECTED:
3189 hash_table = elf_hash_table (info);
3190 if (!is_elf_hash_table (hash_table))
3191 return FALSE;
3192
3193 bed = get_elf_backend_data (hash_table->dynobj);
3194
3195 /* Proper resolution for function pointer equality may require
3196 that these symbols perhaps be resolved dynamically, even though
3197 we should be resolving them to the current module. */
3198 if (!not_local_protected || !bed->is_function_type (h->type))
3199 binding_stays_local_p = TRUE;
3200 break;
3201
3202 default:
3203 break;
3204 }
3205
3206 /* If it isn't defined locally, then clearly it's dynamic. */
3207 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3208 return TRUE;
3209
3210 /* Otherwise, the symbol is dynamic if binding rules don't tell
3211 us that it remains local. */
3212 return !binding_stays_local_p;
3213 }
3214
3215 /* Return true if the symbol referred to by H should be considered
3216 to resolve local to the current module, and false otherwise. Differs
3217 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3218 undefined symbols. The two functions are virtually identical except
3219 for the place where dynindx == -1 is tested. If that test is true,
3220 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3221 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3222 defined symbols.
3223 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3224 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3225 treatment of undefined weak symbols. For those that do not make
3226 undefined weak symbols dynamic, both functions may return false. */
3227
3228 bfd_boolean
3229 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3230 struct bfd_link_info *info,
3231 bfd_boolean local_protected)
3232 {
3233 const struct elf_backend_data *bed;
3234 struct elf_link_hash_table *hash_table;
3235
3236 /* If it's a local sym, of course we resolve locally. */
3237 if (h == NULL)
3238 return TRUE;
3239
3240 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3241 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3242 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3243 return TRUE;
3244
3245 /* Forced local symbols resolve locally. */
3246 if (h->forced_local)
3247 return TRUE;
3248
3249 /* Common symbols that become definitions don't get the DEF_REGULAR
3250 flag set, so test it first, and don't bail out. */
3251 if (ELF_COMMON_DEF_P (h))
3252 /* Do nothing. */;
3253 /* If we don't have a definition in a regular file, then we can't
3254 resolve locally. The sym is either undefined or dynamic. */
3255 else if (!h->def_regular)
3256 return FALSE;
3257
3258 /* Non-dynamic symbols resolve locally. */
3259 if (h->dynindx == -1)
3260 return TRUE;
3261
3262 /* At this point, we know the symbol is defined and dynamic. In an
3263 executable it must resolve locally, likewise when building symbolic
3264 shared libraries. */
3265 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3266 return TRUE;
3267
3268 /* Now deal with defined dynamic symbols in shared libraries. Ones
3269 with default visibility might not resolve locally. */
3270 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3271 return FALSE;
3272
3273 hash_table = elf_hash_table (info);
3274 if (!is_elf_hash_table (hash_table))
3275 return TRUE;
3276
3277 bed = get_elf_backend_data (hash_table->dynobj);
3278
3279 /* If extern_protected_data is false, STV_PROTECTED non-function
3280 symbols are local. */
3281 if ((!info->extern_protected_data
3282 || (info->extern_protected_data < 0
3283 && !bed->extern_protected_data))
3284 && !bed->is_function_type (h->type))
3285 return TRUE;
3286
3287 /* Function pointer equality tests may require that STV_PROTECTED
3288 symbols be treated as dynamic symbols. If the address of a
3289 function not defined in an executable is set to that function's
3290 plt entry in the executable, then the address of the function in
3291 a shared library must also be the plt entry in the executable. */
3292 return local_protected;
3293 }
3294
3295 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3296 aligned. Returns the first TLS output section. */
3297
3298 struct bfd_section *
3299 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3300 {
3301 struct bfd_section *sec, *tls;
3302 unsigned int align = 0;
3303
3304 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3305 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3306 break;
3307 tls = sec;
3308
3309 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3310 if (sec->alignment_power > align)
3311 align = sec->alignment_power;
3312
3313 elf_hash_table (info)->tls_sec = tls;
3314
3315 /* Ensure the alignment of the first section is the largest alignment,
3316 so that the tls segment starts aligned. */
3317 if (tls != NULL)
3318 tls->alignment_power = align;
3319
3320 return tls;
3321 }
3322
3323 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3324 static bfd_boolean
3325 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3326 Elf_Internal_Sym *sym)
3327 {
3328 const struct elf_backend_data *bed;
3329
3330 /* Local symbols do not count, but target specific ones might. */
3331 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3332 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3333 return FALSE;
3334
3335 bed = get_elf_backend_data (abfd);
3336 /* Function symbols do not count. */
3337 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3338 return FALSE;
3339
3340 /* If the section is undefined, then so is the symbol. */
3341 if (sym->st_shndx == SHN_UNDEF)
3342 return FALSE;
3343
3344 /* If the symbol is defined in the common section, then
3345 it is a common definition and so does not count. */
3346 if (bed->common_definition (sym))
3347 return FALSE;
3348
3349 /* If the symbol is in a target specific section then we
3350 must rely upon the backend to tell us what it is. */
3351 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3352 /* FIXME - this function is not coded yet:
3353
3354 return _bfd_is_global_symbol_definition (abfd, sym);
3355
3356 Instead for now assume that the definition is not global,
3357 Even if this is wrong, at least the linker will behave
3358 in the same way that it used to do. */
3359 return FALSE;
3360
3361 return TRUE;
3362 }
3363
3364 /* Search the symbol table of the archive element of the archive ABFD
3365 whose archive map contains a mention of SYMDEF, and determine if
3366 the symbol is defined in this element. */
3367 static bfd_boolean
3368 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3369 {
3370 Elf_Internal_Shdr * hdr;
3371 size_t symcount;
3372 size_t extsymcount;
3373 size_t extsymoff;
3374 Elf_Internal_Sym *isymbuf;
3375 Elf_Internal_Sym *isym;
3376 Elf_Internal_Sym *isymend;
3377 bfd_boolean result;
3378
3379 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3380 if (abfd == NULL)
3381 return FALSE;
3382
3383 if (! bfd_check_format (abfd, bfd_object))
3384 return FALSE;
3385
3386 /* Select the appropriate symbol table. If we don't know if the
3387 object file is an IR object, give linker LTO plugin a chance to
3388 get the correct symbol table. */
3389 if (abfd->plugin_format == bfd_plugin_yes
3390 #if BFD_SUPPORTS_PLUGINS
3391 || (abfd->plugin_format == bfd_plugin_unknown
3392 && bfd_link_plugin_object_p (abfd))
3393 #endif
3394 )
3395 {
3396 /* Use the IR symbol table if the object has been claimed by
3397 plugin. */
3398 abfd = abfd->plugin_dummy_bfd;
3399 hdr = &elf_tdata (abfd)->symtab_hdr;
3400 }
3401 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3402 hdr = &elf_tdata (abfd)->symtab_hdr;
3403 else
3404 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3405
3406 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3407
3408 /* The sh_info field of the symtab header tells us where the
3409 external symbols start. We don't care about the local symbols. */
3410 if (elf_bad_symtab (abfd))
3411 {
3412 extsymcount = symcount;
3413 extsymoff = 0;
3414 }
3415 else
3416 {
3417 extsymcount = symcount - hdr->sh_info;
3418 extsymoff = hdr->sh_info;
3419 }
3420
3421 if (extsymcount == 0)
3422 return FALSE;
3423
3424 /* Read in the symbol table. */
3425 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3426 NULL, NULL, NULL);
3427 if (isymbuf == NULL)
3428 return FALSE;
3429
3430 /* Scan the symbol table looking for SYMDEF. */
3431 result = FALSE;
3432 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3433 {
3434 const char *name;
3435
3436 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3437 isym->st_name);
3438 if (name == NULL)
3439 break;
3440
3441 if (strcmp (name, symdef->name) == 0)
3442 {
3443 result = is_global_data_symbol_definition (abfd, isym);
3444 break;
3445 }
3446 }
3447
3448 free (isymbuf);
3449
3450 return result;
3451 }
3452 \f
3453 /* Add an entry to the .dynamic table. */
3454
3455 bfd_boolean
3456 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3457 bfd_vma tag,
3458 bfd_vma val)
3459 {
3460 struct elf_link_hash_table *hash_table;
3461 const struct elf_backend_data *bed;
3462 asection *s;
3463 bfd_size_type newsize;
3464 bfd_byte *newcontents;
3465 Elf_Internal_Dyn dyn;
3466
3467 hash_table = elf_hash_table (info);
3468 if (! is_elf_hash_table (hash_table))
3469 return FALSE;
3470
3471 if (tag == DT_RELA || tag == DT_REL)
3472 hash_table->dynamic_relocs = TRUE;
3473
3474 bed = get_elf_backend_data (hash_table->dynobj);
3475 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3476 BFD_ASSERT (s != NULL);
3477
3478 newsize = s->size + bed->s->sizeof_dyn;
3479 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3480 if (newcontents == NULL)
3481 return FALSE;
3482
3483 dyn.d_tag = tag;
3484 dyn.d_un.d_val = val;
3485 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3486
3487 s->size = newsize;
3488 s->contents = newcontents;
3489
3490 return TRUE;
3491 }
3492
3493 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3494 otherwise just check whether one already exists. Returns -1 on error,
3495 1 if a DT_NEEDED tag already exists, and 0 on success. */
3496
3497 static int
3498 elf_add_dt_needed_tag (bfd *abfd,
3499 struct bfd_link_info *info,
3500 const char *soname,
3501 bfd_boolean do_it)
3502 {
3503 struct elf_link_hash_table *hash_table;
3504 size_t strindex;
3505
3506 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3507 return -1;
3508
3509 hash_table = elf_hash_table (info);
3510 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3511 if (strindex == (size_t) -1)
3512 return -1;
3513
3514 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3515 {
3516 asection *sdyn;
3517 const struct elf_backend_data *bed;
3518 bfd_byte *extdyn;
3519
3520 bed = get_elf_backend_data (hash_table->dynobj);
3521 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3522 if (sdyn != NULL)
3523 for (extdyn = sdyn->contents;
3524 extdyn < sdyn->contents + sdyn->size;
3525 extdyn += bed->s->sizeof_dyn)
3526 {
3527 Elf_Internal_Dyn dyn;
3528
3529 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3530 if (dyn.d_tag == DT_NEEDED
3531 && dyn.d_un.d_val == strindex)
3532 {
3533 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3534 return 1;
3535 }
3536 }
3537 }
3538
3539 if (do_it)
3540 {
3541 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3542 return -1;
3543
3544 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3545 return -1;
3546 }
3547 else
3548 /* We were just checking for existence of the tag. */
3549 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3550
3551 return 0;
3552 }
3553
3554 /* Return true if SONAME is on the needed list between NEEDED and STOP
3555 (or the end of list if STOP is NULL), and needed by a library that
3556 will be loaded. */
3557
3558 static bfd_boolean
3559 on_needed_list (const char *soname,
3560 struct bfd_link_needed_list *needed,
3561 struct bfd_link_needed_list *stop)
3562 {
3563 struct bfd_link_needed_list *look;
3564 for (look = needed; look != stop; look = look->next)
3565 if (strcmp (soname, look->name) == 0
3566 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3567 /* If needed by a library that itself is not directly
3568 needed, recursively check whether that library is
3569 indirectly needed. Since we add DT_NEEDED entries to
3570 the end of the list, library dependencies appear after
3571 the library. Therefore search prior to the current
3572 LOOK, preventing possible infinite recursion. */
3573 || on_needed_list (elf_dt_name (look->by), needed, look)))
3574 return TRUE;
3575
3576 return FALSE;
3577 }
3578
3579 /* Sort symbol by value, section, and size. */
3580 static int
3581 elf_sort_symbol (const void *arg1, const void *arg2)
3582 {
3583 const struct elf_link_hash_entry *h1;
3584 const struct elf_link_hash_entry *h2;
3585 bfd_signed_vma vdiff;
3586
3587 h1 = *(const struct elf_link_hash_entry **) arg1;
3588 h2 = *(const struct elf_link_hash_entry **) arg2;
3589 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3590 if (vdiff != 0)
3591 return vdiff > 0 ? 1 : -1;
3592 else
3593 {
3594 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3595 if (sdiff != 0)
3596 return sdiff > 0 ? 1 : -1;
3597 }
3598 vdiff = h1->size - h2->size;
3599 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3600 }
3601
3602 /* This function is used to adjust offsets into .dynstr for
3603 dynamic symbols. This is called via elf_link_hash_traverse. */
3604
3605 static bfd_boolean
3606 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3607 {
3608 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3609
3610 if (h->dynindx != -1)
3611 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3612 return TRUE;
3613 }
3614
3615 /* Assign string offsets in .dynstr, update all structures referencing
3616 them. */
3617
3618 static bfd_boolean
3619 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3620 {
3621 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3622 struct elf_link_local_dynamic_entry *entry;
3623 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3624 bfd *dynobj = hash_table->dynobj;
3625 asection *sdyn;
3626 bfd_size_type size;
3627 const struct elf_backend_data *bed;
3628 bfd_byte *extdyn;
3629
3630 _bfd_elf_strtab_finalize (dynstr);
3631 size = _bfd_elf_strtab_size (dynstr);
3632
3633 bed = get_elf_backend_data (dynobj);
3634 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3635 BFD_ASSERT (sdyn != NULL);
3636
3637 /* Update all .dynamic entries referencing .dynstr strings. */
3638 for (extdyn = sdyn->contents;
3639 extdyn < sdyn->contents + sdyn->size;
3640 extdyn += bed->s->sizeof_dyn)
3641 {
3642 Elf_Internal_Dyn dyn;
3643
3644 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3645 switch (dyn.d_tag)
3646 {
3647 case DT_STRSZ:
3648 dyn.d_un.d_val = size;
3649 break;
3650 case DT_NEEDED:
3651 case DT_SONAME:
3652 case DT_RPATH:
3653 case DT_RUNPATH:
3654 case DT_FILTER:
3655 case DT_AUXILIARY:
3656 case DT_AUDIT:
3657 case DT_DEPAUDIT:
3658 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3659 break;
3660 default:
3661 continue;
3662 }
3663 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3664 }
3665
3666 /* Now update local dynamic symbols. */
3667 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3668 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3669 entry->isym.st_name);
3670
3671 /* And the rest of dynamic symbols. */
3672 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3673
3674 /* Adjust version definitions. */
3675 if (elf_tdata (output_bfd)->cverdefs)
3676 {
3677 asection *s;
3678 bfd_byte *p;
3679 size_t i;
3680 Elf_Internal_Verdef def;
3681 Elf_Internal_Verdaux defaux;
3682
3683 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3684 p = s->contents;
3685 do
3686 {
3687 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3688 &def);
3689 p += sizeof (Elf_External_Verdef);
3690 if (def.vd_aux != sizeof (Elf_External_Verdef))
3691 continue;
3692 for (i = 0; i < def.vd_cnt; ++i)
3693 {
3694 _bfd_elf_swap_verdaux_in (output_bfd,
3695 (Elf_External_Verdaux *) p, &defaux);
3696 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3697 defaux.vda_name);
3698 _bfd_elf_swap_verdaux_out (output_bfd,
3699 &defaux, (Elf_External_Verdaux *) p);
3700 p += sizeof (Elf_External_Verdaux);
3701 }
3702 }
3703 while (def.vd_next);
3704 }
3705
3706 /* Adjust version references. */
3707 if (elf_tdata (output_bfd)->verref)
3708 {
3709 asection *s;
3710 bfd_byte *p;
3711 size_t i;
3712 Elf_Internal_Verneed need;
3713 Elf_Internal_Vernaux needaux;
3714
3715 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3716 p = s->contents;
3717 do
3718 {
3719 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3720 &need);
3721 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3722 _bfd_elf_swap_verneed_out (output_bfd, &need,
3723 (Elf_External_Verneed *) p);
3724 p += sizeof (Elf_External_Verneed);
3725 for (i = 0; i < need.vn_cnt; ++i)
3726 {
3727 _bfd_elf_swap_vernaux_in (output_bfd,
3728 (Elf_External_Vernaux *) p, &needaux);
3729 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3730 needaux.vna_name);
3731 _bfd_elf_swap_vernaux_out (output_bfd,
3732 &needaux,
3733 (Elf_External_Vernaux *) p);
3734 p += sizeof (Elf_External_Vernaux);
3735 }
3736 }
3737 while (need.vn_next);
3738 }
3739
3740 return TRUE;
3741 }
3742 \f
3743 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3744 The default is to only match when the INPUT and OUTPUT are exactly
3745 the same target. */
3746
3747 bfd_boolean
3748 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3749 const bfd_target *output)
3750 {
3751 return input == output;
3752 }
3753
3754 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3755 This version is used when different targets for the same architecture
3756 are virtually identical. */
3757
3758 bfd_boolean
3759 _bfd_elf_relocs_compatible (const bfd_target *input,
3760 const bfd_target *output)
3761 {
3762 const struct elf_backend_data *obed, *ibed;
3763
3764 if (input == output)
3765 return TRUE;
3766
3767 ibed = xvec_get_elf_backend_data (input);
3768 obed = xvec_get_elf_backend_data (output);
3769
3770 if (ibed->arch != obed->arch)
3771 return FALSE;
3772
3773 /* If both backends are using this function, deem them compatible. */
3774 return ibed->relocs_compatible == obed->relocs_compatible;
3775 }
3776
3777 /* Make a special call to the linker "notice" function to tell it that
3778 we are about to handle an as-needed lib, or have finished
3779 processing the lib. */
3780
3781 bfd_boolean
3782 _bfd_elf_notice_as_needed (bfd *ibfd,
3783 struct bfd_link_info *info,
3784 enum notice_asneeded_action act)
3785 {
3786 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3787 }
3788
3789 /* Check relocations an ELF object file. */
3790
3791 bfd_boolean
3792 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3793 {
3794 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3795 struct elf_link_hash_table *htab = elf_hash_table (info);
3796
3797 /* If this object is the same format as the output object, and it is
3798 not a shared library, then let the backend look through the
3799 relocs.
3800
3801 This is required to build global offset table entries and to
3802 arrange for dynamic relocs. It is not required for the
3803 particular common case of linking non PIC code, even when linking
3804 against shared libraries, but unfortunately there is no way of
3805 knowing whether an object file has been compiled PIC or not.
3806 Looking through the relocs is not particularly time consuming.
3807 The problem is that we must either (1) keep the relocs in memory,
3808 which causes the linker to require additional runtime memory or
3809 (2) read the relocs twice from the input file, which wastes time.
3810 This would be a good case for using mmap.
3811
3812 I have no idea how to handle linking PIC code into a file of a
3813 different format. It probably can't be done. */
3814 if ((abfd->flags & DYNAMIC) == 0
3815 && is_elf_hash_table (htab)
3816 && bed->check_relocs != NULL
3817 && elf_object_id (abfd) == elf_hash_table_id (htab)
3818 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3819 {
3820 asection *o;
3821
3822 for (o = abfd->sections; o != NULL; o = o->next)
3823 {
3824 Elf_Internal_Rela *internal_relocs;
3825 bfd_boolean ok;
3826
3827 /* Don't check relocations in excluded sections. */
3828 if ((o->flags & SEC_RELOC) == 0
3829 || (o->flags & SEC_EXCLUDE) != 0
3830 || o->reloc_count == 0
3831 || ((info->strip == strip_all || info->strip == strip_debugger)
3832 && (o->flags & SEC_DEBUGGING) != 0)
3833 || bfd_is_abs_section (o->output_section))
3834 continue;
3835
3836 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3837 info->keep_memory);
3838 if (internal_relocs == NULL)
3839 return FALSE;
3840
3841 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3842
3843 if (elf_section_data (o)->relocs != internal_relocs)
3844 free (internal_relocs);
3845
3846 if (! ok)
3847 return FALSE;
3848 }
3849 }
3850
3851 return TRUE;
3852 }
3853
3854 /* Add symbols from an ELF object file to the linker hash table. */
3855
3856 static bfd_boolean
3857 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3858 {
3859 Elf_Internal_Ehdr *ehdr;
3860 Elf_Internal_Shdr *hdr;
3861 size_t symcount;
3862 size_t extsymcount;
3863 size_t extsymoff;
3864 struct elf_link_hash_entry **sym_hash;
3865 bfd_boolean dynamic;
3866 Elf_External_Versym *extversym = NULL;
3867 Elf_External_Versym *ever;
3868 struct elf_link_hash_entry *weaks;
3869 struct elf_link_hash_entry **nondeflt_vers = NULL;
3870 size_t nondeflt_vers_cnt = 0;
3871 Elf_Internal_Sym *isymbuf = NULL;
3872 Elf_Internal_Sym *isym;
3873 Elf_Internal_Sym *isymend;
3874 const struct elf_backend_data *bed;
3875 bfd_boolean add_needed;
3876 struct elf_link_hash_table *htab;
3877 bfd_size_type amt;
3878 void *alloc_mark = NULL;
3879 struct bfd_hash_entry **old_table = NULL;
3880 unsigned int old_size = 0;
3881 unsigned int old_count = 0;
3882 void *old_tab = NULL;
3883 void *old_ent;
3884 struct bfd_link_hash_entry *old_undefs = NULL;
3885 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3886 void *old_strtab = NULL;
3887 size_t tabsize = 0;
3888 asection *s;
3889 bfd_boolean just_syms;
3890
3891 htab = elf_hash_table (info);
3892 bed = get_elf_backend_data (abfd);
3893
3894 if ((abfd->flags & DYNAMIC) == 0)
3895 dynamic = FALSE;
3896 else
3897 {
3898 dynamic = TRUE;
3899
3900 /* You can't use -r against a dynamic object. Also, there's no
3901 hope of using a dynamic object which does not exactly match
3902 the format of the output file. */
3903 if (bfd_link_relocatable (info)
3904 || !is_elf_hash_table (htab)
3905 || info->output_bfd->xvec != abfd->xvec)
3906 {
3907 if (bfd_link_relocatable (info))
3908 bfd_set_error (bfd_error_invalid_operation);
3909 else
3910 bfd_set_error (bfd_error_wrong_format);
3911 goto error_return;
3912 }
3913 }
3914
3915 ehdr = elf_elfheader (abfd);
3916 if (info->warn_alternate_em
3917 && bed->elf_machine_code != ehdr->e_machine
3918 && ((bed->elf_machine_alt1 != 0
3919 && ehdr->e_machine == bed->elf_machine_alt1)
3920 || (bed->elf_machine_alt2 != 0
3921 && ehdr->e_machine == bed->elf_machine_alt2)))
3922 _bfd_error_handler
3923 /* xgettext:c-format */
3924 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
3925 ehdr->e_machine, abfd, bed->elf_machine_code);
3926
3927 /* As a GNU extension, any input sections which are named
3928 .gnu.warning.SYMBOL are treated as warning symbols for the given
3929 symbol. This differs from .gnu.warning sections, which generate
3930 warnings when they are included in an output file. */
3931 /* PR 12761: Also generate this warning when building shared libraries. */
3932 for (s = abfd->sections; s != NULL; s = s->next)
3933 {
3934 const char *name;
3935
3936 name = bfd_get_section_name (abfd, s);
3937 if (CONST_STRNEQ (name, ".gnu.warning."))
3938 {
3939 char *msg;
3940 bfd_size_type sz;
3941
3942 name += sizeof ".gnu.warning." - 1;
3943
3944 /* If this is a shared object, then look up the symbol
3945 in the hash table. If it is there, and it is already
3946 been defined, then we will not be using the entry
3947 from this shared object, so we don't need to warn.
3948 FIXME: If we see the definition in a regular object
3949 later on, we will warn, but we shouldn't. The only
3950 fix is to keep track of what warnings we are supposed
3951 to emit, and then handle them all at the end of the
3952 link. */
3953 if (dynamic)
3954 {
3955 struct elf_link_hash_entry *h;
3956
3957 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3958
3959 /* FIXME: What about bfd_link_hash_common? */
3960 if (h != NULL
3961 && (h->root.type == bfd_link_hash_defined
3962 || h->root.type == bfd_link_hash_defweak))
3963 continue;
3964 }
3965
3966 sz = s->size;
3967 msg = (char *) bfd_alloc (abfd, sz + 1);
3968 if (msg == NULL)
3969 goto error_return;
3970
3971 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3972 goto error_return;
3973
3974 msg[sz] = '\0';
3975
3976 if (! (_bfd_generic_link_add_one_symbol
3977 (info, abfd, name, BSF_WARNING, s, 0, msg,
3978 FALSE, bed->collect, NULL)))
3979 goto error_return;
3980
3981 if (bfd_link_executable (info))
3982 {
3983 /* Clobber the section size so that the warning does
3984 not get copied into the output file. */
3985 s->size = 0;
3986
3987 /* Also set SEC_EXCLUDE, so that symbols defined in
3988 the warning section don't get copied to the output. */
3989 s->flags |= SEC_EXCLUDE;
3990 }
3991 }
3992 }
3993
3994 just_syms = ((s = abfd->sections) != NULL
3995 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3996
3997 add_needed = TRUE;
3998 if (! dynamic)
3999 {
4000 /* If we are creating a shared library, create all the dynamic
4001 sections immediately. We need to attach them to something,
4002 so we attach them to this BFD, provided it is the right
4003 format and is not from ld --just-symbols. Always create the
4004 dynamic sections for -E/--dynamic-list. FIXME: If there
4005 are no input BFD's of the same format as the output, we can't
4006 make a shared library. */
4007 if (!just_syms
4008 && (bfd_link_pic (info)
4009 || (!bfd_link_relocatable (info)
4010 && info->nointerp
4011 && (info->export_dynamic || info->dynamic)))
4012 && is_elf_hash_table (htab)
4013 && info->output_bfd->xvec == abfd->xvec
4014 && !htab->dynamic_sections_created)
4015 {
4016 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4017 goto error_return;
4018 }
4019 }
4020 else if (!is_elf_hash_table (htab))
4021 goto error_return;
4022 else
4023 {
4024 const char *soname = NULL;
4025 char *audit = NULL;
4026 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4027 const Elf_Internal_Phdr *phdr;
4028 int ret;
4029
4030 /* ld --just-symbols and dynamic objects don't mix very well.
4031 ld shouldn't allow it. */
4032 if (just_syms)
4033 abort ();
4034
4035 /* If this dynamic lib was specified on the command line with
4036 --as-needed in effect, then we don't want to add a DT_NEEDED
4037 tag unless the lib is actually used. Similary for libs brought
4038 in by another lib's DT_NEEDED. When --no-add-needed is used
4039 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4040 any dynamic library in DT_NEEDED tags in the dynamic lib at
4041 all. */
4042 add_needed = (elf_dyn_lib_class (abfd)
4043 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4044 | DYN_NO_NEEDED)) == 0;
4045
4046 s = bfd_get_section_by_name (abfd, ".dynamic");
4047 if (s != NULL)
4048 {
4049 bfd_byte *dynbuf;
4050 bfd_byte *extdyn;
4051 unsigned int elfsec;
4052 unsigned long shlink;
4053
4054 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4055 {
4056 error_free_dyn:
4057 free (dynbuf);
4058 goto error_return;
4059 }
4060
4061 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4062 if (elfsec == SHN_BAD)
4063 goto error_free_dyn;
4064 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4065
4066 for (extdyn = dynbuf;
4067 extdyn < dynbuf + s->size;
4068 extdyn += bed->s->sizeof_dyn)
4069 {
4070 Elf_Internal_Dyn dyn;
4071
4072 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4073 if (dyn.d_tag == DT_SONAME)
4074 {
4075 unsigned int tagv = dyn.d_un.d_val;
4076 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4077 if (soname == NULL)
4078 goto error_free_dyn;
4079 }
4080 if (dyn.d_tag == DT_NEEDED)
4081 {
4082 struct bfd_link_needed_list *n, **pn;
4083 char *fnm, *anm;
4084 unsigned int tagv = dyn.d_un.d_val;
4085
4086 amt = sizeof (struct bfd_link_needed_list);
4087 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4088 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4089 if (n == NULL || fnm == NULL)
4090 goto error_free_dyn;
4091 amt = strlen (fnm) + 1;
4092 anm = (char *) bfd_alloc (abfd, amt);
4093 if (anm == NULL)
4094 goto error_free_dyn;
4095 memcpy (anm, fnm, amt);
4096 n->name = anm;
4097 n->by = abfd;
4098 n->next = NULL;
4099 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4100 ;
4101 *pn = n;
4102 }
4103 if (dyn.d_tag == DT_RUNPATH)
4104 {
4105 struct bfd_link_needed_list *n, **pn;
4106 char *fnm, *anm;
4107 unsigned int tagv = dyn.d_un.d_val;
4108
4109 amt = sizeof (struct bfd_link_needed_list);
4110 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4111 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4112 if (n == NULL || fnm == NULL)
4113 goto error_free_dyn;
4114 amt = strlen (fnm) + 1;
4115 anm = (char *) bfd_alloc (abfd, amt);
4116 if (anm == NULL)
4117 goto error_free_dyn;
4118 memcpy (anm, fnm, amt);
4119 n->name = anm;
4120 n->by = abfd;
4121 n->next = NULL;
4122 for (pn = & runpath;
4123 *pn != NULL;
4124 pn = &(*pn)->next)
4125 ;
4126 *pn = n;
4127 }
4128 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4129 if (!runpath && dyn.d_tag == DT_RPATH)
4130 {
4131 struct bfd_link_needed_list *n, **pn;
4132 char *fnm, *anm;
4133 unsigned int tagv = dyn.d_un.d_val;
4134
4135 amt = sizeof (struct bfd_link_needed_list);
4136 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4137 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4138 if (n == NULL || fnm == NULL)
4139 goto error_free_dyn;
4140 amt = strlen (fnm) + 1;
4141 anm = (char *) bfd_alloc (abfd, amt);
4142 if (anm == NULL)
4143 goto error_free_dyn;
4144 memcpy (anm, fnm, amt);
4145 n->name = anm;
4146 n->by = abfd;
4147 n->next = NULL;
4148 for (pn = & rpath;
4149 *pn != NULL;
4150 pn = &(*pn)->next)
4151 ;
4152 *pn = n;
4153 }
4154 if (dyn.d_tag == DT_AUDIT)
4155 {
4156 unsigned int tagv = dyn.d_un.d_val;
4157 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4158 }
4159 }
4160
4161 free (dynbuf);
4162 }
4163
4164 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4165 frees all more recently bfd_alloc'd blocks as well. */
4166 if (runpath)
4167 rpath = runpath;
4168
4169 if (rpath)
4170 {
4171 struct bfd_link_needed_list **pn;
4172 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4173 ;
4174 *pn = rpath;
4175 }
4176
4177 /* If we have a PT_GNU_RELRO program header, mark as read-only
4178 all sections contained fully therein. This makes relro
4179 shared library sections appear as they will at run-time. */
4180 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4181 while (--phdr >= elf_tdata (abfd)->phdr)
4182 if (phdr->p_type == PT_GNU_RELRO)
4183 {
4184 for (s = abfd->sections; s != NULL; s = s->next)
4185 if ((s->flags & SEC_ALLOC) != 0
4186 && s->vma >= phdr->p_vaddr
4187 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4188 s->flags |= SEC_READONLY;
4189 break;
4190 }
4191
4192 /* We do not want to include any of the sections in a dynamic
4193 object in the output file. We hack by simply clobbering the
4194 list of sections in the BFD. This could be handled more
4195 cleanly by, say, a new section flag; the existing
4196 SEC_NEVER_LOAD flag is not the one we want, because that one
4197 still implies that the section takes up space in the output
4198 file. */
4199 bfd_section_list_clear (abfd);
4200
4201 /* Find the name to use in a DT_NEEDED entry that refers to this
4202 object. If the object has a DT_SONAME entry, we use it.
4203 Otherwise, if the generic linker stuck something in
4204 elf_dt_name, we use that. Otherwise, we just use the file
4205 name. */
4206 if (soname == NULL || *soname == '\0')
4207 {
4208 soname = elf_dt_name (abfd);
4209 if (soname == NULL || *soname == '\0')
4210 soname = bfd_get_filename (abfd);
4211 }
4212
4213 /* Save the SONAME because sometimes the linker emulation code
4214 will need to know it. */
4215 elf_dt_name (abfd) = soname;
4216
4217 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4218 if (ret < 0)
4219 goto error_return;
4220
4221 /* If we have already included this dynamic object in the
4222 link, just ignore it. There is no reason to include a
4223 particular dynamic object more than once. */
4224 if (ret > 0)
4225 return TRUE;
4226
4227 /* Save the DT_AUDIT entry for the linker emulation code. */
4228 elf_dt_audit (abfd) = audit;
4229 }
4230
4231 /* If this is a dynamic object, we always link against the .dynsym
4232 symbol table, not the .symtab symbol table. The dynamic linker
4233 will only see the .dynsym symbol table, so there is no reason to
4234 look at .symtab for a dynamic object. */
4235
4236 if (! dynamic || elf_dynsymtab (abfd) == 0)
4237 hdr = &elf_tdata (abfd)->symtab_hdr;
4238 else
4239 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4240
4241 symcount = hdr->sh_size / bed->s->sizeof_sym;
4242
4243 /* The sh_info field of the symtab header tells us where the
4244 external symbols start. We don't care about the local symbols at
4245 this point. */
4246 if (elf_bad_symtab (abfd))
4247 {
4248 extsymcount = symcount;
4249 extsymoff = 0;
4250 }
4251 else
4252 {
4253 extsymcount = symcount - hdr->sh_info;
4254 extsymoff = hdr->sh_info;
4255 }
4256
4257 sym_hash = elf_sym_hashes (abfd);
4258 if (extsymcount != 0)
4259 {
4260 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4261 NULL, NULL, NULL);
4262 if (isymbuf == NULL)
4263 goto error_return;
4264
4265 if (sym_hash == NULL)
4266 {
4267 /* We store a pointer to the hash table entry for each
4268 external symbol. */
4269 amt = extsymcount;
4270 amt *= sizeof (struct elf_link_hash_entry *);
4271 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4272 if (sym_hash == NULL)
4273 goto error_free_sym;
4274 elf_sym_hashes (abfd) = sym_hash;
4275 }
4276 }
4277
4278 if (dynamic)
4279 {
4280 /* Read in any version definitions. */
4281 if (!_bfd_elf_slurp_version_tables (abfd,
4282 info->default_imported_symver))
4283 goto error_free_sym;
4284
4285 /* Read in the symbol versions, but don't bother to convert them
4286 to internal format. */
4287 if (elf_dynversym (abfd) != 0)
4288 {
4289 Elf_Internal_Shdr *versymhdr;
4290
4291 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4292 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4293 if (extversym == NULL)
4294 goto error_free_sym;
4295 amt = versymhdr->sh_size;
4296 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4297 || bfd_bread (extversym, amt, abfd) != amt)
4298 goto error_free_vers;
4299 }
4300 }
4301
4302 /* If we are loading an as-needed shared lib, save the symbol table
4303 state before we start adding symbols. If the lib turns out
4304 to be unneeded, restore the state. */
4305 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4306 {
4307 unsigned int i;
4308 size_t entsize;
4309
4310 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4311 {
4312 struct bfd_hash_entry *p;
4313 struct elf_link_hash_entry *h;
4314
4315 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4316 {
4317 h = (struct elf_link_hash_entry *) p;
4318 entsize += htab->root.table.entsize;
4319 if (h->root.type == bfd_link_hash_warning)
4320 entsize += htab->root.table.entsize;
4321 }
4322 }
4323
4324 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4325 old_tab = bfd_malloc (tabsize + entsize);
4326 if (old_tab == NULL)
4327 goto error_free_vers;
4328
4329 /* Remember the current objalloc pointer, so that all mem for
4330 symbols added can later be reclaimed. */
4331 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4332 if (alloc_mark == NULL)
4333 goto error_free_vers;
4334
4335 /* Make a special call to the linker "notice" function to
4336 tell it that we are about to handle an as-needed lib. */
4337 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4338 goto error_free_vers;
4339
4340 /* Clone the symbol table. Remember some pointers into the
4341 symbol table, and dynamic symbol count. */
4342 old_ent = (char *) old_tab + tabsize;
4343 memcpy (old_tab, htab->root.table.table, tabsize);
4344 old_undefs = htab->root.undefs;
4345 old_undefs_tail = htab->root.undefs_tail;
4346 old_table = htab->root.table.table;
4347 old_size = htab->root.table.size;
4348 old_count = htab->root.table.count;
4349 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4350 if (old_strtab == NULL)
4351 goto error_free_vers;
4352
4353 for (i = 0; i < htab->root.table.size; i++)
4354 {
4355 struct bfd_hash_entry *p;
4356 struct elf_link_hash_entry *h;
4357
4358 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4359 {
4360 memcpy (old_ent, p, htab->root.table.entsize);
4361 old_ent = (char *) old_ent + htab->root.table.entsize;
4362 h = (struct elf_link_hash_entry *) p;
4363 if (h->root.type == bfd_link_hash_warning)
4364 {
4365 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4366 old_ent = (char *) old_ent + htab->root.table.entsize;
4367 }
4368 }
4369 }
4370 }
4371
4372 weaks = NULL;
4373 ever = extversym != NULL ? extversym + extsymoff : NULL;
4374 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4375 isym < isymend;
4376 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4377 {
4378 int bind;
4379 bfd_vma value;
4380 asection *sec, *new_sec;
4381 flagword flags;
4382 const char *name;
4383 struct elf_link_hash_entry *h;
4384 struct elf_link_hash_entry *hi;
4385 bfd_boolean definition;
4386 bfd_boolean size_change_ok;
4387 bfd_boolean type_change_ok;
4388 bfd_boolean new_weak;
4389 bfd_boolean old_weak;
4390 bfd_boolean override;
4391 bfd_boolean common;
4392 bfd_boolean discarded;
4393 unsigned int old_alignment;
4394 bfd *old_bfd;
4395 bfd_boolean matched;
4396
4397 override = FALSE;
4398
4399 flags = BSF_NO_FLAGS;
4400 sec = NULL;
4401 value = isym->st_value;
4402 common = bed->common_definition (isym);
4403 if (common && info->inhibit_common_definition)
4404 {
4405 /* Treat common symbol as undefined for --no-define-common. */
4406 isym->st_shndx = SHN_UNDEF;
4407 common = FALSE;
4408 }
4409 discarded = FALSE;
4410
4411 bind = ELF_ST_BIND (isym->st_info);
4412 switch (bind)
4413 {
4414 case STB_LOCAL:
4415 /* This should be impossible, since ELF requires that all
4416 global symbols follow all local symbols, and that sh_info
4417 point to the first global symbol. Unfortunately, Irix 5
4418 screws this up. */
4419 continue;
4420
4421 case STB_GLOBAL:
4422 if (isym->st_shndx != SHN_UNDEF && !common)
4423 flags = BSF_GLOBAL;
4424 break;
4425
4426 case STB_WEAK:
4427 flags = BSF_WEAK;
4428 break;
4429
4430 case STB_GNU_UNIQUE:
4431 flags = BSF_GNU_UNIQUE;
4432 break;
4433
4434 default:
4435 /* Leave it up to the processor backend. */
4436 break;
4437 }
4438
4439 if (isym->st_shndx == SHN_UNDEF)
4440 sec = bfd_und_section_ptr;
4441 else if (isym->st_shndx == SHN_ABS)
4442 sec = bfd_abs_section_ptr;
4443 else if (isym->st_shndx == SHN_COMMON)
4444 {
4445 sec = bfd_com_section_ptr;
4446 /* What ELF calls the size we call the value. What ELF
4447 calls the value we call the alignment. */
4448 value = isym->st_size;
4449 }
4450 else
4451 {
4452 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4453 if (sec == NULL)
4454 sec = bfd_abs_section_ptr;
4455 else if (discarded_section (sec))
4456 {
4457 /* Symbols from discarded section are undefined. We keep
4458 its visibility. */
4459 sec = bfd_und_section_ptr;
4460 discarded = TRUE;
4461 isym->st_shndx = SHN_UNDEF;
4462 }
4463 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4464 value -= sec->vma;
4465 }
4466
4467 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4468 isym->st_name);
4469 if (name == NULL)
4470 goto error_free_vers;
4471
4472 if (isym->st_shndx == SHN_COMMON
4473 && (abfd->flags & BFD_PLUGIN) != 0)
4474 {
4475 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4476
4477 if (xc == NULL)
4478 {
4479 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4480 | SEC_EXCLUDE);
4481 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4482 if (xc == NULL)
4483 goto error_free_vers;
4484 }
4485 sec = xc;
4486 }
4487 else if (isym->st_shndx == SHN_COMMON
4488 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4489 && !bfd_link_relocatable (info))
4490 {
4491 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4492
4493 if (tcomm == NULL)
4494 {
4495 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4496 | SEC_LINKER_CREATED);
4497 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4498 if (tcomm == NULL)
4499 goto error_free_vers;
4500 }
4501 sec = tcomm;
4502 }
4503 else if (bed->elf_add_symbol_hook)
4504 {
4505 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4506 &sec, &value))
4507 goto error_free_vers;
4508
4509 /* The hook function sets the name to NULL if this symbol
4510 should be skipped for some reason. */
4511 if (name == NULL)
4512 continue;
4513 }
4514
4515 /* Sanity check that all possibilities were handled. */
4516 if (sec == NULL)
4517 {
4518 bfd_set_error (bfd_error_bad_value);
4519 goto error_free_vers;
4520 }
4521
4522 /* Silently discard TLS symbols from --just-syms. There's
4523 no way to combine a static TLS block with a new TLS block
4524 for this executable. */
4525 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4526 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4527 continue;
4528
4529 if (bfd_is_und_section (sec)
4530 || bfd_is_com_section (sec))
4531 definition = FALSE;
4532 else
4533 definition = TRUE;
4534
4535 size_change_ok = FALSE;
4536 type_change_ok = bed->type_change_ok;
4537 old_weak = FALSE;
4538 matched = FALSE;
4539 old_alignment = 0;
4540 old_bfd = NULL;
4541 new_sec = sec;
4542
4543 if (is_elf_hash_table (htab))
4544 {
4545 Elf_Internal_Versym iver;
4546 unsigned int vernum = 0;
4547 bfd_boolean skip;
4548
4549 if (ever == NULL)
4550 {
4551 if (info->default_imported_symver)
4552 /* Use the default symbol version created earlier. */
4553 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4554 else
4555 iver.vs_vers = 0;
4556 }
4557 else
4558 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4559
4560 vernum = iver.vs_vers & VERSYM_VERSION;
4561
4562 /* If this is a hidden symbol, or if it is not version
4563 1, we append the version name to the symbol name.
4564 However, we do not modify a non-hidden absolute symbol
4565 if it is not a function, because it might be the version
4566 symbol itself. FIXME: What if it isn't? */
4567 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4568 || (vernum > 1
4569 && (!bfd_is_abs_section (sec)
4570 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4571 {
4572 const char *verstr;
4573 size_t namelen, verlen, newlen;
4574 char *newname, *p;
4575
4576 if (isym->st_shndx != SHN_UNDEF)
4577 {
4578 if (vernum > elf_tdata (abfd)->cverdefs)
4579 verstr = NULL;
4580 else if (vernum > 1)
4581 verstr =
4582 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4583 else
4584 verstr = "";
4585
4586 if (verstr == NULL)
4587 {
4588 _bfd_error_handler
4589 /* xgettext:c-format */
4590 (_("%pB: %s: invalid version %u (max %d)"),
4591 abfd, name, vernum,
4592 elf_tdata (abfd)->cverdefs);
4593 bfd_set_error (bfd_error_bad_value);
4594 goto error_free_vers;
4595 }
4596 }
4597 else
4598 {
4599 /* We cannot simply test for the number of
4600 entries in the VERNEED section since the
4601 numbers for the needed versions do not start
4602 at 0. */
4603 Elf_Internal_Verneed *t;
4604
4605 verstr = NULL;
4606 for (t = elf_tdata (abfd)->verref;
4607 t != NULL;
4608 t = t->vn_nextref)
4609 {
4610 Elf_Internal_Vernaux *a;
4611
4612 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4613 {
4614 if (a->vna_other == vernum)
4615 {
4616 verstr = a->vna_nodename;
4617 break;
4618 }
4619 }
4620 if (a != NULL)
4621 break;
4622 }
4623 if (verstr == NULL)
4624 {
4625 _bfd_error_handler
4626 /* xgettext:c-format */
4627 (_("%pB: %s: invalid needed version %d"),
4628 abfd, name, vernum);
4629 bfd_set_error (bfd_error_bad_value);
4630 goto error_free_vers;
4631 }
4632 }
4633
4634 namelen = strlen (name);
4635 verlen = strlen (verstr);
4636 newlen = namelen + verlen + 2;
4637 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4638 && isym->st_shndx != SHN_UNDEF)
4639 ++newlen;
4640
4641 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4642 if (newname == NULL)
4643 goto error_free_vers;
4644 memcpy (newname, name, namelen);
4645 p = newname + namelen;
4646 *p++ = ELF_VER_CHR;
4647 /* If this is a defined non-hidden version symbol,
4648 we add another @ to the name. This indicates the
4649 default version of the symbol. */
4650 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4651 && isym->st_shndx != SHN_UNDEF)
4652 *p++ = ELF_VER_CHR;
4653 memcpy (p, verstr, verlen + 1);
4654
4655 name = newname;
4656 }
4657
4658 /* If this symbol has default visibility and the user has
4659 requested we not re-export it, then mark it as hidden. */
4660 if (!bfd_is_und_section (sec)
4661 && !dynamic
4662 && abfd->no_export
4663 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4664 isym->st_other = (STV_HIDDEN
4665 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4666
4667 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4668 sym_hash, &old_bfd, &old_weak,
4669 &old_alignment, &skip, &override,
4670 &type_change_ok, &size_change_ok,
4671 &matched))
4672 goto error_free_vers;
4673
4674 if (skip)
4675 continue;
4676
4677 /* Override a definition only if the new symbol matches the
4678 existing one. */
4679 if (override && matched)
4680 definition = FALSE;
4681
4682 h = *sym_hash;
4683 while (h->root.type == bfd_link_hash_indirect
4684 || h->root.type == bfd_link_hash_warning)
4685 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4686
4687 if (elf_tdata (abfd)->verdef != NULL
4688 && vernum > 1
4689 && definition)
4690 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4691 }
4692
4693 if (! (_bfd_generic_link_add_one_symbol
4694 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4695 (struct bfd_link_hash_entry **) sym_hash)))
4696 goto error_free_vers;
4697
4698 if ((abfd->flags & DYNAMIC) == 0
4699 && (bfd_get_flavour (info->output_bfd)
4700 == bfd_target_elf_flavour))
4701 {
4702 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4703 elf_tdata (info->output_bfd)->has_gnu_symbols
4704 |= elf_gnu_symbol_ifunc;
4705 if ((flags & BSF_GNU_UNIQUE))
4706 elf_tdata (info->output_bfd)->has_gnu_symbols
4707 |= elf_gnu_symbol_unique;
4708 }
4709
4710 h = *sym_hash;
4711 /* We need to make sure that indirect symbol dynamic flags are
4712 updated. */
4713 hi = h;
4714 while (h->root.type == bfd_link_hash_indirect
4715 || h->root.type == bfd_link_hash_warning)
4716 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4717
4718 /* Setting the index to -3 tells elf_link_output_extsym that
4719 this symbol is defined in a discarded section. */
4720 if (discarded)
4721 h->indx = -3;
4722
4723 *sym_hash = h;
4724
4725 new_weak = (flags & BSF_WEAK) != 0;
4726 if (dynamic
4727 && definition
4728 && new_weak
4729 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4730 && is_elf_hash_table (htab)
4731 && h->u.alias == NULL)
4732 {
4733 /* Keep a list of all weak defined non function symbols from
4734 a dynamic object, using the alias field. Later in this
4735 function we will set the alias field to the correct
4736 value. We only put non-function symbols from dynamic
4737 objects on this list, because that happens to be the only
4738 time we need to know the normal symbol corresponding to a
4739 weak symbol, and the information is time consuming to
4740 figure out. If the alias field is not already NULL,
4741 then this symbol was already defined by some previous
4742 dynamic object, and we will be using that previous
4743 definition anyhow. */
4744
4745 h->u.alias = weaks;
4746 weaks = h;
4747 }
4748
4749 /* Set the alignment of a common symbol. */
4750 if ((common || bfd_is_com_section (sec))
4751 && h->root.type == bfd_link_hash_common)
4752 {
4753 unsigned int align;
4754
4755 if (common)
4756 align = bfd_log2 (isym->st_value);
4757 else
4758 {
4759 /* The new symbol is a common symbol in a shared object.
4760 We need to get the alignment from the section. */
4761 align = new_sec->alignment_power;
4762 }
4763 if (align > old_alignment)
4764 h->root.u.c.p->alignment_power = align;
4765 else
4766 h->root.u.c.p->alignment_power = old_alignment;
4767 }
4768
4769 if (is_elf_hash_table (htab))
4770 {
4771 /* Set a flag in the hash table entry indicating the type of
4772 reference or definition we just found. A dynamic symbol
4773 is one which is referenced or defined by both a regular
4774 object and a shared object. */
4775 bfd_boolean dynsym = FALSE;
4776
4777 /* Plugin symbols aren't normal. Don't set def_regular or
4778 ref_regular for them, or make them dynamic. */
4779 if ((abfd->flags & BFD_PLUGIN) != 0)
4780 ;
4781 else if (! dynamic)
4782 {
4783 if (! definition)
4784 {
4785 h->ref_regular = 1;
4786 if (bind != STB_WEAK)
4787 h->ref_regular_nonweak = 1;
4788 }
4789 else
4790 {
4791 h->def_regular = 1;
4792 if (h->def_dynamic)
4793 {
4794 h->def_dynamic = 0;
4795 h->ref_dynamic = 1;
4796 }
4797 }
4798
4799 /* If the indirect symbol has been forced local, don't
4800 make the real symbol dynamic. */
4801 if ((h == hi || !hi->forced_local)
4802 && (bfd_link_dll (info)
4803 || h->def_dynamic
4804 || h->ref_dynamic))
4805 dynsym = TRUE;
4806 }
4807 else
4808 {
4809 if (! definition)
4810 {
4811 h->ref_dynamic = 1;
4812 hi->ref_dynamic = 1;
4813 }
4814 else
4815 {
4816 h->def_dynamic = 1;
4817 hi->def_dynamic = 1;
4818 }
4819
4820 /* If the indirect symbol has been forced local, don't
4821 make the real symbol dynamic. */
4822 if ((h == hi || !hi->forced_local)
4823 && (h->def_regular
4824 || h->ref_regular
4825 || (h->is_weakalias
4826 && weakdef (h)->dynindx != -1)))
4827 dynsym = TRUE;
4828 }
4829
4830 /* Check to see if we need to add an indirect symbol for
4831 the default name. */
4832 if (definition
4833 || (!override && h->root.type == bfd_link_hash_common))
4834 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4835 sec, value, &old_bfd, &dynsym))
4836 goto error_free_vers;
4837
4838 /* Check the alignment when a common symbol is involved. This
4839 can change when a common symbol is overridden by a normal
4840 definition or a common symbol is ignored due to the old
4841 normal definition. We need to make sure the maximum
4842 alignment is maintained. */
4843 if ((old_alignment || common)
4844 && h->root.type != bfd_link_hash_common)
4845 {
4846 unsigned int common_align;
4847 unsigned int normal_align;
4848 unsigned int symbol_align;
4849 bfd *normal_bfd;
4850 bfd *common_bfd;
4851
4852 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4853 || h->root.type == bfd_link_hash_defweak);
4854
4855 symbol_align = ffs (h->root.u.def.value) - 1;
4856 if (h->root.u.def.section->owner != NULL
4857 && (h->root.u.def.section->owner->flags
4858 & (DYNAMIC | BFD_PLUGIN)) == 0)
4859 {
4860 normal_align = h->root.u.def.section->alignment_power;
4861 if (normal_align > symbol_align)
4862 normal_align = symbol_align;
4863 }
4864 else
4865 normal_align = symbol_align;
4866
4867 if (old_alignment)
4868 {
4869 common_align = old_alignment;
4870 common_bfd = old_bfd;
4871 normal_bfd = abfd;
4872 }
4873 else
4874 {
4875 common_align = bfd_log2 (isym->st_value);
4876 common_bfd = abfd;
4877 normal_bfd = old_bfd;
4878 }
4879
4880 if (normal_align < common_align)
4881 {
4882 /* PR binutils/2735 */
4883 if (normal_bfd == NULL)
4884 _bfd_error_handler
4885 /* xgettext:c-format */
4886 (_("warning: alignment %u of common symbol `%s' in %pB is"
4887 " greater than the alignment (%u) of its section %pA"),
4888 1 << common_align, name, common_bfd,
4889 1 << normal_align, h->root.u.def.section);
4890 else
4891 _bfd_error_handler
4892 /* xgettext:c-format */
4893 (_("warning: alignment %u of symbol `%s' in %pB"
4894 " is smaller than %u in %pB"),
4895 1 << normal_align, name, normal_bfd,
4896 1 << common_align, common_bfd);
4897 }
4898 }
4899
4900 /* Remember the symbol size if it isn't undefined. */
4901 if (isym->st_size != 0
4902 && isym->st_shndx != SHN_UNDEF
4903 && (definition || h->size == 0))
4904 {
4905 if (h->size != 0
4906 && h->size != isym->st_size
4907 && ! size_change_ok)
4908 _bfd_error_handler
4909 /* xgettext:c-format */
4910 (_("warning: size of symbol `%s' changed"
4911 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
4912 name, (uint64_t) h->size, old_bfd,
4913 (uint64_t) isym->st_size, abfd);
4914
4915 h->size = isym->st_size;
4916 }
4917
4918 /* If this is a common symbol, then we always want H->SIZE
4919 to be the size of the common symbol. The code just above
4920 won't fix the size if a common symbol becomes larger. We
4921 don't warn about a size change here, because that is
4922 covered by --warn-common. Allow changes between different
4923 function types. */
4924 if (h->root.type == bfd_link_hash_common)
4925 h->size = h->root.u.c.size;
4926
4927 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4928 && ((definition && !new_weak)
4929 || (old_weak && h->root.type == bfd_link_hash_common)
4930 || h->type == STT_NOTYPE))
4931 {
4932 unsigned int type = ELF_ST_TYPE (isym->st_info);
4933
4934 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4935 symbol. */
4936 if (type == STT_GNU_IFUNC
4937 && (abfd->flags & DYNAMIC) != 0)
4938 type = STT_FUNC;
4939
4940 if (h->type != type)
4941 {
4942 if (h->type != STT_NOTYPE && ! type_change_ok)
4943 /* xgettext:c-format */
4944 _bfd_error_handler
4945 (_("warning: type of symbol `%s' changed"
4946 " from %d to %d in %pB"),
4947 name, h->type, type, abfd);
4948
4949 h->type = type;
4950 }
4951 }
4952
4953 /* Merge st_other field. */
4954 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4955
4956 /* We don't want to make debug symbol dynamic. */
4957 if (definition
4958 && (sec->flags & SEC_DEBUGGING)
4959 && !bfd_link_relocatable (info))
4960 dynsym = FALSE;
4961
4962 /* Nor should we make plugin symbols dynamic. */
4963 if ((abfd->flags & BFD_PLUGIN) != 0)
4964 dynsym = FALSE;
4965
4966 if (definition)
4967 {
4968 h->target_internal = isym->st_target_internal;
4969 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4970 }
4971
4972 if (definition && !dynamic)
4973 {
4974 char *p = strchr (name, ELF_VER_CHR);
4975 if (p != NULL && p[1] != ELF_VER_CHR)
4976 {
4977 /* Queue non-default versions so that .symver x, x@FOO
4978 aliases can be checked. */
4979 if (!nondeflt_vers)
4980 {
4981 amt = ((isymend - isym + 1)
4982 * sizeof (struct elf_link_hash_entry *));
4983 nondeflt_vers
4984 = (struct elf_link_hash_entry **) bfd_malloc (amt);
4985 if (!nondeflt_vers)
4986 goto error_free_vers;
4987 }
4988 nondeflt_vers[nondeflt_vers_cnt++] = h;
4989 }
4990 }
4991
4992 if (dynsym && h->dynindx == -1)
4993 {
4994 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4995 goto error_free_vers;
4996 if (h->is_weakalias
4997 && weakdef (h)->dynindx == -1)
4998 {
4999 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5000 goto error_free_vers;
5001 }
5002 }
5003 else if (h->dynindx != -1)
5004 /* If the symbol already has a dynamic index, but
5005 visibility says it should not be visible, turn it into
5006 a local symbol. */
5007 switch (ELF_ST_VISIBILITY (h->other))
5008 {
5009 case STV_INTERNAL:
5010 case STV_HIDDEN:
5011 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5012 dynsym = FALSE;
5013 break;
5014 }
5015
5016 /* Don't add DT_NEEDED for references from the dummy bfd nor
5017 for unmatched symbol. */
5018 if (!add_needed
5019 && matched
5020 && definition
5021 && ((dynsym
5022 && h->ref_regular_nonweak
5023 && (old_bfd == NULL
5024 || (old_bfd->flags & BFD_PLUGIN) == 0))
5025 || (h->ref_dynamic_nonweak
5026 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5027 && !on_needed_list (elf_dt_name (abfd),
5028 htab->needed, NULL))))
5029 {
5030 int ret;
5031 const char *soname = elf_dt_name (abfd);
5032
5033 info->callbacks->minfo ("%!", soname, old_bfd,
5034 h->root.root.string);
5035
5036 /* A symbol from a library loaded via DT_NEEDED of some
5037 other library is referenced by a regular object.
5038 Add a DT_NEEDED entry for it. Issue an error if
5039 --no-add-needed is used and the reference was not
5040 a weak one. */
5041 if (old_bfd != NULL
5042 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5043 {
5044 _bfd_error_handler
5045 /* xgettext:c-format */
5046 (_("%pB: undefined reference to symbol '%s'"),
5047 old_bfd, name);
5048 bfd_set_error (bfd_error_missing_dso);
5049 goto error_free_vers;
5050 }
5051
5052 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5053 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5054
5055 add_needed = TRUE;
5056 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
5057 if (ret < 0)
5058 goto error_free_vers;
5059
5060 BFD_ASSERT (ret == 0);
5061 }
5062 }
5063 }
5064
5065 if (info->lto_plugin_active
5066 && !bfd_link_relocatable (info)
5067 && (abfd->flags & BFD_PLUGIN) == 0
5068 && !just_syms
5069 && extsymcount)
5070 {
5071 int r_sym_shift;
5072
5073 if (bed->s->arch_size == 32)
5074 r_sym_shift = 8;
5075 else
5076 r_sym_shift = 32;
5077
5078 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5079 referenced in regular objects so that linker plugin will get
5080 the correct symbol resolution. */
5081
5082 sym_hash = elf_sym_hashes (abfd);
5083 for (s = abfd->sections; s != NULL; s = s->next)
5084 {
5085 Elf_Internal_Rela *internal_relocs;
5086 Elf_Internal_Rela *rel, *relend;
5087
5088 /* Don't check relocations in excluded sections. */
5089 if ((s->flags & SEC_RELOC) == 0
5090 || s->reloc_count == 0
5091 || (s->flags & SEC_EXCLUDE) != 0
5092 || ((info->strip == strip_all
5093 || info->strip == strip_debugger)
5094 && (s->flags & SEC_DEBUGGING) != 0))
5095 continue;
5096
5097 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5098 NULL,
5099 info->keep_memory);
5100 if (internal_relocs == NULL)
5101 goto error_free_vers;
5102
5103 rel = internal_relocs;
5104 relend = rel + s->reloc_count;
5105 for ( ; rel < relend; rel++)
5106 {
5107 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5108 struct elf_link_hash_entry *h;
5109
5110 /* Skip local symbols. */
5111 if (r_symndx < extsymoff)
5112 continue;
5113
5114 h = sym_hash[r_symndx - extsymoff];
5115 if (h != NULL)
5116 h->root.non_ir_ref_regular = 1;
5117 }
5118
5119 if (elf_section_data (s)->relocs != internal_relocs)
5120 free (internal_relocs);
5121 }
5122 }
5123
5124 if (extversym != NULL)
5125 {
5126 free (extversym);
5127 extversym = NULL;
5128 }
5129
5130 if (isymbuf != NULL)
5131 {
5132 free (isymbuf);
5133 isymbuf = NULL;
5134 }
5135
5136 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5137 {
5138 unsigned int i;
5139
5140 /* Restore the symbol table. */
5141 old_ent = (char *) old_tab + tabsize;
5142 memset (elf_sym_hashes (abfd), 0,
5143 extsymcount * sizeof (struct elf_link_hash_entry *));
5144 htab->root.table.table = old_table;
5145 htab->root.table.size = old_size;
5146 htab->root.table.count = old_count;
5147 memcpy (htab->root.table.table, old_tab, tabsize);
5148 htab->root.undefs = old_undefs;
5149 htab->root.undefs_tail = old_undefs_tail;
5150 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5151 free (old_strtab);
5152 old_strtab = NULL;
5153 for (i = 0; i < htab->root.table.size; i++)
5154 {
5155 struct bfd_hash_entry *p;
5156 struct elf_link_hash_entry *h;
5157 bfd_size_type size;
5158 unsigned int alignment_power;
5159 unsigned int non_ir_ref_dynamic;
5160
5161 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5162 {
5163 h = (struct elf_link_hash_entry *) p;
5164 if (h->root.type == bfd_link_hash_warning)
5165 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5166
5167 /* Preserve the maximum alignment and size for common
5168 symbols even if this dynamic lib isn't on DT_NEEDED
5169 since it can still be loaded at run time by another
5170 dynamic lib. */
5171 if (h->root.type == bfd_link_hash_common)
5172 {
5173 size = h->root.u.c.size;
5174 alignment_power = h->root.u.c.p->alignment_power;
5175 }
5176 else
5177 {
5178 size = 0;
5179 alignment_power = 0;
5180 }
5181 /* Preserve non_ir_ref_dynamic so that this symbol
5182 will be exported when the dynamic lib becomes needed
5183 in the second pass. */
5184 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5185 memcpy (p, old_ent, htab->root.table.entsize);
5186 old_ent = (char *) old_ent + htab->root.table.entsize;
5187 h = (struct elf_link_hash_entry *) p;
5188 if (h->root.type == bfd_link_hash_warning)
5189 {
5190 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5191 old_ent = (char *) old_ent + htab->root.table.entsize;
5192 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5193 }
5194 if (h->root.type == bfd_link_hash_common)
5195 {
5196 if (size > h->root.u.c.size)
5197 h->root.u.c.size = size;
5198 if (alignment_power > h->root.u.c.p->alignment_power)
5199 h->root.u.c.p->alignment_power = alignment_power;
5200 }
5201 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5202 }
5203 }
5204
5205 /* Make a special call to the linker "notice" function to
5206 tell it that symbols added for crefs may need to be removed. */
5207 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5208 goto error_free_vers;
5209
5210 free (old_tab);
5211 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5212 alloc_mark);
5213 if (nondeflt_vers != NULL)
5214 free (nondeflt_vers);
5215 return TRUE;
5216 }
5217
5218 if (old_tab != NULL)
5219 {
5220 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5221 goto error_free_vers;
5222 free (old_tab);
5223 old_tab = NULL;
5224 }
5225
5226 /* Now that all the symbols from this input file are created, if
5227 not performing a relocatable link, handle .symver foo, foo@BAR
5228 such that any relocs against foo become foo@BAR. */
5229 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5230 {
5231 size_t cnt, symidx;
5232
5233 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5234 {
5235 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5236 char *shortname, *p;
5237
5238 p = strchr (h->root.root.string, ELF_VER_CHR);
5239 if (p == NULL
5240 || (h->root.type != bfd_link_hash_defined
5241 && h->root.type != bfd_link_hash_defweak))
5242 continue;
5243
5244 amt = p - h->root.root.string;
5245 shortname = (char *) bfd_malloc (amt + 1);
5246 if (!shortname)
5247 goto error_free_vers;
5248 memcpy (shortname, h->root.root.string, amt);
5249 shortname[amt] = '\0';
5250
5251 hi = (struct elf_link_hash_entry *)
5252 bfd_link_hash_lookup (&htab->root, shortname,
5253 FALSE, FALSE, FALSE);
5254 if (hi != NULL
5255 && hi->root.type == h->root.type
5256 && hi->root.u.def.value == h->root.u.def.value
5257 && hi->root.u.def.section == h->root.u.def.section)
5258 {
5259 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5260 hi->root.type = bfd_link_hash_indirect;
5261 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5262 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5263 sym_hash = elf_sym_hashes (abfd);
5264 if (sym_hash)
5265 for (symidx = 0; symidx < extsymcount; ++symidx)
5266 if (sym_hash[symidx] == hi)
5267 {
5268 sym_hash[symidx] = h;
5269 break;
5270 }
5271 }
5272 free (shortname);
5273 }
5274 free (nondeflt_vers);
5275 nondeflt_vers = NULL;
5276 }
5277
5278 /* Now set the alias field correctly for all the weak defined
5279 symbols we found. The only way to do this is to search all the
5280 symbols. Since we only need the information for non functions in
5281 dynamic objects, that's the only time we actually put anything on
5282 the list WEAKS. We need this information so that if a regular
5283 object refers to a symbol defined weakly in a dynamic object, the
5284 real symbol in the dynamic object is also put in the dynamic
5285 symbols; we also must arrange for both symbols to point to the
5286 same memory location. We could handle the general case of symbol
5287 aliasing, but a general symbol alias can only be generated in
5288 assembler code, handling it correctly would be very time
5289 consuming, and other ELF linkers don't handle general aliasing
5290 either. */
5291 if (weaks != NULL)
5292 {
5293 struct elf_link_hash_entry **hpp;
5294 struct elf_link_hash_entry **hppend;
5295 struct elf_link_hash_entry **sorted_sym_hash;
5296 struct elf_link_hash_entry *h;
5297 size_t sym_count;
5298
5299 /* Since we have to search the whole symbol list for each weak
5300 defined symbol, search time for N weak defined symbols will be
5301 O(N^2). Binary search will cut it down to O(NlogN). */
5302 amt = extsymcount;
5303 amt *= sizeof (struct elf_link_hash_entry *);
5304 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
5305 if (sorted_sym_hash == NULL)
5306 goto error_return;
5307 sym_hash = sorted_sym_hash;
5308 hpp = elf_sym_hashes (abfd);
5309 hppend = hpp + extsymcount;
5310 sym_count = 0;
5311 for (; hpp < hppend; hpp++)
5312 {
5313 h = *hpp;
5314 if (h != NULL
5315 && h->root.type == bfd_link_hash_defined
5316 && !bed->is_function_type (h->type))
5317 {
5318 *sym_hash = h;
5319 sym_hash++;
5320 sym_count++;
5321 }
5322 }
5323
5324 qsort (sorted_sym_hash, sym_count,
5325 sizeof (struct elf_link_hash_entry *),
5326 elf_sort_symbol);
5327
5328 while (weaks != NULL)
5329 {
5330 struct elf_link_hash_entry *hlook;
5331 asection *slook;
5332 bfd_vma vlook;
5333 size_t i, j, idx = 0;
5334
5335 hlook = weaks;
5336 weaks = hlook->u.alias;
5337 hlook->u.alias = NULL;
5338
5339 if (hlook->root.type != bfd_link_hash_defined
5340 && hlook->root.type != bfd_link_hash_defweak)
5341 continue;
5342
5343 slook = hlook->root.u.def.section;
5344 vlook = hlook->root.u.def.value;
5345
5346 i = 0;
5347 j = sym_count;
5348 while (i != j)
5349 {
5350 bfd_signed_vma vdiff;
5351 idx = (i + j) / 2;
5352 h = sorted_sym_hash[idx];
5353 vdiff = vlook - h->root.u.def.value;
5354 if (vdiff < 0)
5355 j = idx;
5356 else if (vdiff > 0)
5357 i = idx + 1;
5358 else
5359 {
5360 int sdiff = slook->id - h->root.u.def.section->id;
5361 if (sdiff < 0)
5362 j = idx;
5363 else if (sdiff > 0)
5364 i = idx + 1;
5365 else
5366 break;
5367 }
5368 }
5369
5370 /* We didn't find a value/section match. */
5371 if (i == j)
5372 continue;
5373
5374 /* With multiple aliases, or when the weak symbol is already
5375 strongly defined, we have multiple matching symbols and
5376 the binary search above may land on any of them. Step
5377 one past the matching symbol(s). */
5378 while (++idx != j)
5379 {
5380 h = sorted_sym_hash[idx];
5381 if (h->root.u.def.section != slook
5382 || h->root.u.def.value != vlook)
5383 break;
5384 }
5385
5386 /* Now look back over the aliases. Since we sorted by size
5387 as well as value and section, we'll choose the one with
5388 the largest size. */
5389 while (idx-- != i)
5390 {
5391 h = sorted_sym_hash[idx];
5392
5393 /* Stop if value or section doesn't match. */
5394 if (h->root.u.def.section != slook
5395 || h->root.u.def.value != vlook)
5396 break;
5397 else if (h != hlook)
5398 {
5399 struct elf_link_hash_entry *t;
5400
5401 hlook->u.alias = h;
5402 hlook->is_weakalias = 1;
5403 t = h;
5404 if (t->u.alias != NULL)
5405 while (t->u.alias != h)
5406 t = t->u.alias;
5407 t->u.alias = hlook;
5408
5409 /* If the weak definition is in the list of dynamic
5410 symbols, make sure the real definition is put
5411 there as well. */
5412 if (hlook->dynindx != -1 && h->dynindx == -1)
5413 {
5414 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5415 {
5416 err_free_sym_hash:
5417 free (sorted_sym_hash);
5418 goto error_return;
5419 }
5420 }
5421
5422 /* If the real definition is in the list of dynamic
5423 symbols, make sure the weak definition is put
5424 there as well. If we don't do this, then the
5425 dynamic loader might not merge the entries for the
5426 real definition and the weak definition. */
5427 if (h->dynindx != -1 && hlook->dynindx == -1)
5428 {
5429 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5430 goto err_free_sym_hash;
5431 }
5432 break;
5433 }
5434 }
5435 }
5436
5437 free (sorted_sym_hash);
5438 }
5439
5440 if (bed->check_directives
5441 && !(*bed->check_directives) (abfd, info))
5442 return FALSE;
5443
5444 /* If this is a non-traditional link, try to optimize the handling
5445 of the .stab/.stabstr sections. */
5446 if (! dynamic
5447 && ! info->traditional_format
5448 && is_elf_hash_table (htab)
5449 && (info->strip != strip_all && info->strip != strip_debugger))
5450 {
5451 asection *stabstr;
5452
5453 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5454 if (stabstr != NULL)
5455 {
5456 bfd_size_type string_offset = 0;
5457 asection *stab;
5458
5459 for (stab = abfd->sections; stab; stab = stab->next)
5460 if (CONST_STRNEQ (stab->name, ".stab")
5461 && (!stab->name[5] ||
5462 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5463 && (stab->flags & SEC_MERGE) == 0
5464 && !bfd_is_abs_section (stab->output_section))
5465 {
5466 struct bfd_elf_section_data *secdata;
5467
5468 secdata = elf_section_data (stab);
5469 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5470 stabstr, &secdata->sec_info,
5471 &string_offset))
5472 goto error_return;
5473 if (secdata->sec_info)
5474 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5475 }
5476 }
5477 }
5478
5479 if (is_elf_hash_table (htab) && add_needed)
5480 {
5481 /* Add this bfd to the loaded list. */
5482 struct elf_link_loaded_list *n;
5483
5484 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5485 if (n == NULL)
5486 goto error_return;
5487 n->abfd = abfd;
5488 n->next = htab->loaded;
5489 htab->loaded = n;
5490 }
5491
5492 return TRUE;
5493
5494 error_free_vers:
5495 if (old_tab != NULL)
5496 free (old_tab);
5497 if (old_strtab != NULL)
5498 free (old_strtab);
5499 if (nondeflt_vers != NULL)
5500 free (nondeflt_vers);
5501 if (extversym != NULL)
5502 free (extversym);
5503 error_free_sym:
5504 if (isymbuf != NULL)
5505 free (isymbuf);
5506 error_return:
5507 return FALSE;
5508 }
5509
5510 /* Return the linker hash table entry of a symbol that might be
5511 satisfied by an archive symbol. Return -1 on error. */
5512
5513 struct elf_link_hash_entry *
5514 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5515 struct bfd_link_info *info,
5516 const char *name)
5517 {
5518 struct elf_link_hash_entry *h;
5519 char *p, *copy;
5520 size_t len, first;
5521
5522 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5523 if (h != NULL)
5524 return h;
5525
5526 /* If this is a default version (the name contains @@), look up the
5527 symbol again with only one `@' as well as without the version.
5528 The effect is that references to the symbol with and without the
5529 version will be matched by the default symbol in the archive. */
5530
5531 p = strchr (name, ELF_VER_CHR);
5532 if (p == NULL || p[1] != ELF_VER_CHR)
5533 return h;
5534
5535 /* First check with only one `@'. */
5536 len = strlen (name);
5537 copy = (char *) bfd_alloc (abfd, len);
5538 if (copy == NULL)
5539 return (struct elf_link_hash_entry *) -1;
5540
5541 first = p - name + 1;
5542 memcpy (copy, name, first);
5543 memcpy (copy + first, name + first + 1, len - first);
5544
5545 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5546 if (h == NULL)
5547 {
5548 /* We also need to check references to the symbol without the
5549 version. */
5550 copy[first - 1] = '\0';
5551 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5552 FALSE, FALSE, TRUE);
5553 }
5554
5555 bfd_release (abfd, copy);
5556 return h;
5557 }
5558
5559 /* Add symbols from an ELF archive file to the linker hash table. We
5560 don't use _bfd_generic_link_add_archive_symbols because we need to
5561 handle versioned symbols.
5562
5563 Fortunately, ELF archive handling is simpler than that done by
5564 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5565 oddities. In ELF, if we find a symbol in the archive map, and the
5566 symbol is currently undefined, we know that we must pull in that
5567 object file.
5568
5569 Unfortunately, we do have to make multiple passes over the symbol
5570 table until nothing further is resolved. */
5571
5572 static bfd_boolean
5573 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5574 {
5575 symindex c;
5576 unsigned char *included = NULL;
5577 carsym *symdefs;
5578 bfd_boolean loop;
5579 bfd_size_type amt;
5580 const struct elf_backend_data *bed;
5581 struct elf_link_hash_entry * (*archive_symbol_lookup)
5582 (bfd *, struct bfd_link_info *, const char *);
5583
5584 if (! bfd_has_map (abfd))
5585 {
5586 /* An empty archive is a special case. */
5587 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5588 return TRUE;
5589 bfd_set_error (bfd_error_no_armap);
5590 return FALSE;
5591 }
5592
5593 /* Keep track of all symbols we know to be already defined, and all
5594 files we know to be already included. This is to speed up the
5595 second and subsequent passes. */
5596 c = bfd_ardata (abfd)->symdef_count;
5597 if (c == 0)
5598 return TRUE;
5599 amt = c;
5600 amt *= sizeof (*included);
5601 included = (unsigned char *) bfd_zmalloc (amt);
5602 if (included == NULL)
5603 return FALSE;
5604
5605 symdefs = bfd_ardata (abfd)->symdefs;
5606 bed = get_elf_backend_data (abfd);
5607 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5608
5609 do
5610 {
5611 file_ptr last;
5612 symindex i;
5613 carsym *symdef;
5614 carsym *symdefend;
5615
5616 loop = FALSE;
5617 last = -1;
5618
5619 symdef = symdefs;
5620 symdefend = symdef + c;
5621 for (i = 0; symdef < symdefend; symdef++, i++)
5622 {
5623 struct elf_link_hash_entry *h;
5624 bfd *element;
5625 struct bfd_link_hash_entry *undefs_tail;
5626 symindex mark;
5627
5628 if (included[i])
5629 continue;
5630 if (symdef->file_offset == last)
5631 {
5632 included[i] = TRUE;
5633 continue;
5634 }
5635
5636 h = archive_symbol_lookup (abfd, info, symdef->name);
5637 if (h == (struct elf_link_hash_entry *) -1)
5638 goto error_return;
5639
5640 if (h == NULL)
5641 continue;
5642
5643 if (h->root.type == bfd_link_hash_common)
5644 {
5645 /* We currently have a common symbol. The archive map contains
5646 a reference to this symbol, so we may want to include it. We
5647 only want to include it however, if this archive element
5648 contains a definition of the symbol, not just another common
5649 declaration of it.
5650
5651 Unfortunately some archivers (including GNU ar) will put
5652 declarations of common symbols into their archive maps, as
5653 well as real definitions, so we cannot just go by the archive
5654 map alone. Instead we must read in the element's symbol
5655 table and check that to see what kind of symbol definition
5656 this is. */
5657 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5658 continue;
5659 }
5660 else if (h->root.type != bfd_link_hash_undefined)
5661 {
5662 if (h->root.type != bfd_link_hash_undefweak)
5663 /* Symbol must be defined. Don't check it again. */
5664 included[i] = TRUE;
5665 continue;
5666 }
5667
5668 /* We need to include this archive member. */
5669 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5670 if (element == NULL)
5671 goto error_return;
5672
5673 if (! bfd_check_format (element, bfd_object))
5674 goto error_return;
5675
5676 undefs_tail = info->hash->undefs_tail;
5677
5678 if (!(*info->callbacks
5679 ->add_archive_element) (info, element, symdef->name, &element))
5680 continue;
5681 if (!bfd_link_add_symbols (element, info))
5682 goto error_return;
5683
5684 /* If there are any new undefined symbols, we need to make
5685 another pass through the archive in order to see whether
5686 they can be defined. FIXME: This isn't perfect, because
5687 common symbols wind up on undefs_tail and because an
5688 undefined symbol which is defined later on in this pass
5689 does not require another pass. This isn't a bug, but it
5690 does make the code less efficient than it could be. */
5691 if (undefs_tail != info->hash->undefs_tail)
5692 loop = TRUE;
5693
5694 /* Look backward to mark all symbols from this object file
5695 which we have already seen in this pass. */
5696 mark = i;
5697 do
5698 {
5699 included[mark] = TRUE;
5700 if (mark == 0)
5701 break;
5702 --mark;
5703 }
5704 while (symdefs[mark].file_offset == symdef->file_offset);
5705
5706 /* We mark subsequent symbols from this object file as we go
5707 on through the loop. */
5708 last = symdef->file_offset;
5709 }
5710 }
5711 while (loop);
5712
5713 free (included);
5714
5715 return TRUE;
5716
5717 error_return:
5718 if (included != NULL)
5719 free (included);
5720 return FALSE;
5721 }
5722
5723 /* Given an ELF BFD, add symbols to the global hash table as
5724 appropriate. */
5725
5726 bfd_boolean
5727 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5728 {
5729 switch (bfd_get_format (abfd))
5730 {
5731 case bfd_object:
5732 return elf_link_add_object_symbols (abfd, info);
5733 case bfd_archive:
5734 return elf_link_add_archive_symbols (abfd, info);
5735 default:
5736 bfd_set_error (bfd_error_wrong_format);
5737 return FALSE;
5738 }
5739 }
5740 \f
5741 struct hash_codes_info
5742 {
5743 unsigned long *hashcodes;
5744 bfd_boolean error;
5745 };
5746
5747 /* This function will be called though elf_link_hash_traverse to store
5748 all hash value of the exported symbols in an array. */
5749
5750 static bfd_boolean
5751 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5752 {
5753 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5754 const char *name;
5755 unsigned long ha;
5756 char *alc = NULL;
5757
5758 /* Ignore indirect symbols. These are added by the versioning code. */
5759 if (h->dynindx == -1)
5760 return TRUE;
5761
5762 name = h->root.root.string;
5763 if (h->versioned >= versioned)
5764 {
5765 char *p = strchr (name, ELF_VER_CHR);
5766 if (p != NULL)
5767 {
5768 alc = (char *) bfd_malloc (p - name + 1);
5769 if (alc == NULL)
5770 {
5771 inf->error = TRUE;
5772 return FALSE;
5773 }
5774 memcpy (alc, name, p - name);
5775 alc[p - name] = '\0';
5776 name = alc;
5777 }
5778 }
5779
5780 /* Compute the hash value. */
5781 ha = bfd_elf_hash (name);
5782
5783 /* Store the found hash value in the array given as the argument. */
5784 *(inf->hashcodes)++ = ha;
5785
5786 /* And store it in the struct so that we can put it in the hash table
5787 later. */
5788 h->u.elf_hash_value = ha;
5789
5790 if (alc != NULL)
5791 free (alc);
5792
5793 return TRUE;
5794 }
5795
5796 struct collect_gnu_hash_codes
5797 {
5798 bfd *output_bfd;
5799 const struct elf_backend_data *bed;
5800 unsigned long int nsyms;
5801 unsigned long int maskbits;
5802 unsigned long int *hashcodes;
5803 unsigned long int *hashval;
5804 unsigned long int *indx;
5805 unsigned long int *counts;
5806 bfd_vma *bitmask;
5807 bfd_byte *contents;
5808 long int min_dynindx;
5809 unsigned long int bucketcount;
5810 unsigned long int symindx;
5811 long int local_indx;
5812 long int shift1, shift2;
5813 unsigned long int mask;
5814 bfd_boolean error;
5815 };
5816
5817 /* This function will be called though elf_link_hash_traverse to store
5818 all hash value of the exported symbols in an array. */
5819
5820 static bfd_boolean
5821 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5822 {
5823 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5824 const char *name;
5825 unsigned long ha;
5826 char *alc = NULL;
5827
5828 /* Ignore indirect symbols. These are added by the versioning code. */
5829 if (h->dynindx == -1)
5830 return TRUE;
5831
5832 /* Ignore also local symbols and undefined symbols. */
5833 if (! (*s->bed->elf_hash_symbol) (h))
5834 return TRUE;
5835
5836 name = h->root.root.string;
5837 if (h->versioned >= versioned)
5838 {
5839 char *p = strchr (name, ELF_VER_CHR);
5840 if (p != NULL)
5841 {
5842 alc = (char *) bfd_malloc (p - name + 1);
5843 if (alc == NULL)
5844 {
5845 s->error = TRUE;
5846 return FALSE;
5847 }
5848 memcpy (alc, name, p - name);
5849 alc[p - name] = '\0';
5850 name = alc;
5851 }
5852 }
5853
5854 /* Compute the hash value. */
5855 ha = bfd_elf_gnu_hash (name);
5856
5857 /* Store the found hash value in the array for compute_bucket_count,
5858 and also for .dynsym reordering purposes. */
5859 s->hashcodes[s->nsyms] = ha;
5860 s->hashval[h->dynindx] = ha;
5861 ++s->nsyms;
5862 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5863 s->min_dynindx = h->dynindx;
5864
5865 if (alc != NULL)
5866 free (alc);
5867
5868 return TRUE;
5869 }
5870
5871 /* This function will be called though elf_link_hash_traverse to do
5872 final dynaminc symbol renumbering. */
5873
5874 static bfd_boolean
5875 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5876 {
5877 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5878 unsigned long int bucket;
5879 unsigned long int val;
5880
5881 /* Ignore indirect symbols. */
5882 if (h->dynindx == -1)
5883 return TRUE;
5884
5885 /* Ignore also local symbols and undefined symbols. */
5886 if (! (*s->bed->elf_hash_symbol) (h))
5887 {
5888 if (h->dynindx >= s->min_dynindx)
5889 h->dynindx = s->local_indx++;
5890 return TRUE;
5891 }
5892
5893 bucket = s->hashval[h->dynindx] % s->bucketcount;
5894 val = (s->hashval[h->dynindx] >> s->shift1)
5895 & ((s->maskbits >> s->shift1) - 1);
5896 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5897 s->bitmask[val]
5898 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5899 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5900 if (s->counts[bucket] == 1)
5901 /* Last element terminates the chain. */
5902 val |= 1;
5903 bfd_put_32 (s->output_bfd, val,
5904 s->contents + (s->indx[bucket] - s->symindx) * 4);
5905 --s->counts[bucket];
5906 h->dynindx = s->indx[bucket]++;
5907 return TRUE;
5908 }
5909
5910 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5911
5912 bfd_boolean
5913 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5914 {
5915 return !(h->forced_local
5916 || h->root.type == bfd_link_hash_undefined
5917 || h->root.type == bfd_link_hash_undefweak
5918 || ((h->root.type == bfd_link_hash_defined
5919 || h->root.type == bfd_link_hash_defweak)
5920 && h->root.u.def.section->output_section == NULL));
5921 }
5922
5923 /* Array used to determine the number of hash table buckets to use
5924 based on the number of symbols there are. If there are fewer than
5925 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5926 fewer than 37 we use 17 buckets, and so forth. We never use more
5927 than 32771 buckets. */
5928
5929 static const size_t elf_buckets[] =
5930 {
5931 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5932 16411, 32771, 0
5933 };
5934
5935 /* Compute bucket count for hashing table. We do not use a static set
5936 of possible tables sizes anymore. Instead we determine for all
5937 possible reasonable sizes of the table the outcome (i.e., the
5938 number of collisions etc) and choose the best solution. The
5939 weighting functions are not too simple to allow the table to grow
5940 without bounds. Instead one of the weighting factors is the size.
5941 Therefore the result is always a good payoff between few collisions
5942 (= short chain lengths) and table size. */
5943 static size_t
5944 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5945 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5946 unsigned long int nsyms,
5947 int gnu_hash)
5948 {
5949 size_t best_size = 0;
5950 unsigned long int i;
5951
5952 /* We have a problem here. The following code to optimize the table
5953 size requires an integer type with more the 32 bits. If
5954 BFD_HOST_U_64_BIT is set we know about such a type. */
5955 #ifdef BFD_HOST_U_64_BIT
5956 if (info->optimize)
5957 {
5958 size_t minsize;
5959 size_t maxsize;
5960 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5961 bfd *dynobj = elf_hash_table (info)->dynobj;
5962 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5963 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5964 unsigned long int *counts;
5965 bfd_size_type amt;
5966 unsigned int no_improvement_count = 0;
5967
5968 /* Possible optimization parameters: if we have NSYMS symbols we say
5969 that the hashing table must at least have NSYMS/4 and at most
5970 2*NSYMS buckets. */
5971 minsize = nsyms / 4;
5972 if (minsize == 0)
5973 minsize = 1;
5974 best_size = maxsize = nsyms * 2;
5975 if (gnu_hash)
5976 {
5977 if (minsize < 2)
5978 minsize = 2;
5979 if ((best_size & 31) == 0)
5980 ++best_size;
5981 }
5982
5983 /* Create array where we count the collisions in. We must use bfd_malloc
5984 since the size could be large. */
5985 amt = maxsize;
5986 amt *= sizeof (unsigned long int);
5987 counts = (unsigned long int *) bfd_malloc (amt);
5988 if (counts == NULL)
5989 return 0;
5990
5991 /* Compute the "optimal" size for the hash table. The criteria is a
5992 minimal chain length. The minor criteria is (of course) the size
5993 of the table. */
5994 for (i = minsize; i < maxsize; ++i)
5995 {
5996 /* Walk through the array of hashcodes and count the collisions. */
5997 BFD_HOST_U_64_BIT max;
5998 unsigned long int j;
5999 unsigned long int fact;
6000
6001 if (gnu_hash && (i & 31) == 0)
6002 continue;
6003
6004 memset (counts, '\0', i * sizeof (unsigned long int));
6005
6006 /* Determine how often each hash bucket is used. */
6007 for (j = 0; j < nsyms; ++j)
6008 ++counts[hashcodes[j] % i];
6009
6010 /* For the weight function we need some information about the
6011 pagesize on the target. This is information need not be 100%
6012 accurate. Since this information is not available (so far) we
6013 define it here to a reasonable default value. If it is crucial
6014 to have a better value some day simply define this value. */
6015 # ifndef BFD_TARGET_PAGESIZE
6016 # define BFD_TARGET_PAGESIZE (4096)
6017 # endif
6018
6019 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6020 and the chains. */
6021 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6022
6023 # if 1
6024 /* Variant 1: optimize for short chains. We add the squares
6025 of all the chain lengths (which favors many small chain
6026 over a few long chains). */
6027 for (j = 0; j < i; ++j)
6028 max += counts[j] * counts[j];
6029
6030 /* This adds penalties for the overall size of the table. */
6031 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6032 max *= fact * fact;
6033 # else
6034 /* Variant 2: Optimize a lot more for small table. Here we
6035 also add squares of the size but we also add penalties for
6036 empty slots (the +1 term). */
6037 for (j = 0; j < i; ++j)
6038 max += (1 + counts[j]) * (1 + counts[j]);
6039
6040 /* The overall size of the table is considered, but not as
6041 strong as in variant 1, where it is squared. */
6042 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6043 max *= fact;
6044 # endif
6045
6046 /* Compare with current best results. */
6047 if (max < best_chlen)
6048 {
6049 best_chlen = max;
6050 best_size = i;
6051 no_improvement_count = 0;
6052 }
6053 /* PR 11843: Avoid futile long searches for the best bucket size
6054 when there are a large number of symbols. */
6055 else if (++no_improvement_count == 100)
6056 break;
6057 }
6058
6059 free (counts);
6060 }
6061 else
6062 #endif /* defined (BFD_HOST_U_64_BIT) */
6063 {
6064 /* This is the fallback solution if no 64bit type is available or if we
6065 are not supposed to spend much time on optimizations. We select the
6066 bucket count using a fixed set of numbers. */
6067 for (i = 0; elf_buckets[i] != 0; i++)
6068 {
6069 best_size = elf_buckets[i];
6070 if (nsyms < elf_buckets[i + 1])
6071 break;
6072 }
6073 if (gnu_hash && best_size < 2)
6074 best_size = 2;
6075 }
6076
6077 return best_size;
6078 }
6079
6080 /* Size any SHT_GROUP section for ld -r. */
6081
6082 bfd_boolean
6083 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6084 {
6085 bfd *ibfd;
6086 asection *s;
6087
6088 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6089 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6090 && (s = ibfd->sections) != NULL
6091 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6092 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6093 return FALSE;
6094 return TRUE;
6095 }
6096
6097 /* Set a default stack segment size. The value in INFO wins. If it
6098 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6099 undefined it is initialized. */
6100
6101 bfd_boolean
6102 bfd_elf_stack_segment_size (bfd *output_bfd,
6103 struct bfd_link_info *info,
6104 const char *legacy_symbol,
6105 bfd_vma default_size)
6106 {
6107 struct elf_link_hash_entry *h = NULL;
6108
6109 /* Look for legacy symbol. */
6110 if (legacy_symbol)
6111 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6112 FALSE, FALSE, FALSE);
6113 if (h && (h->root.type == bfd_link_hash_defined
6114 || h->root.type == bfd_link_hash_defweak)
6115 && h->def_regular
6116 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6117 {
6118 /* The symbol has no type if specified on the command line. */
6119 h->type = STT_OBJECT;
6120 if (info->stacksize)
6121 /* xgettext:c-format */
6122 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6123 output_bfd, legacy_symbol);
6124 else if (h->root.u.def.section != bfd_abs_section_ptr)
6125 /* xgettext:c-format */
6126 _bfd_error_handler (_("%pB: %s not absolute"),
6127 output_bfd, legacy_symbol);
6128 else
6129 info->stacksize = h->root.u.def.value;
6130 }
6131
6132 if (!info->stacksize)
6133 /* If the user didn't set a size, or explicitly inhibit the
6134 size, set it now. */
6135 info->stacksize = default_size;
6136
6137 /* Provide the legacy symbol, if it is referenced. */
6138 if (h && (h->root.type == bfd_link_hash_undefined
6139 || h->root.type == bfd_link_hash_undefweak))
6140 {
6141 struct bfd_link_hash_entry *bh = NULL;
6142
6143 if (!(_bfd_generic_link_add_one_symbol
6144 (info, output_bfd, legacy_symbol,
6145 BSF_GLOBAL, bfd_abs_section_ptr,
6146 info->stacksize >= 0 ? info->stacksize : 0,
6147 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6148 return FALSE;
6149
6150 h = (struct elf_link_hash_entry *) bh;
6151 h->def_regular = 1;
6152 h->type = STT_OBJECT;
6153 }
6154
6155 return TRUE;
6156 }
6157
6158 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6159
6160 struct elf_gc_sweep_symbol_info
6161 {
6162 struct bfd_link_info *info;
6163 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6164 bfd_boolean);
6165 };
6166
6167 static bfd_boolean
6168 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6169 {
6170 if (!h->mark
6171 && (((h->root.type == bfd_link_hash_defined
6172 || h->root.type == bfd_link_hash_defweak)
6173 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6174 && h->root.u.def.section->gc_mark))
6175 || h->root.type == bfd_link_hash_undefined
6176 || h->root.type == bfd_link_hash_undefweak))
6177 {
6178 struct elf_gc_sweep_symbol_info *inf;
6179
6180 inf = (struct elf_gc_sweep_symbol_info *) data;
6181 (*inf->hide_symbol) (inf->info, h, TRUE);
6182 h->def_regular = 0;
6183 h->ref_regular = 0;
6184 h->ref_regular_nonweak = 0;
6185 }
6186
6187 return TRUE;
6188 }
6189
6190 /* Set up the sizes and contents of the ELF dynamic sections. This is
6191 called by the ELF linker emulation before_allocation routine. We
6192 must set the sizes of the sections before the linker sets the
6193 addresses of the various sections. */
6194
6195 bfd_boolean
6196 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6197 const char *soname,
6198 const char *rpath,
6199 const char *filter_shlib,
6200 const char *audit,
6201 const char *depaudit,
6202 const char * const *auxiliary_filters,
6203 struct bfd_link_info *info,
6204 asection **sinterpptr)
6205 {
6206 bfd *dynobj;
6207 const struct elf_backend_data *bed;
6208
6209 *sinterpptr = NULL;
6210
6211 if (!is_elf_hash_table (info->hash))
6212 return TRUE;
6213
6214 dynobj = elf_hash_table (info)->dynobj;
6215
6216 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6217 {
6218 struct bfd_elf_version_tree *verdefs;
6219 struct elf_info_failed asvinfo;
6220 struct bfd_elf_version_tree *t;
6221 struct bfd_elf_version_expr *d;
6222 asection *s;
6223 size_t soname_indx;
6224
6225 /* If we are supposed to export all symbols into the dynamic symbol
6226 table (this is not the normal case), then do so. */
6227 if (info->export_dynamic
6228 || (bfd_link_executable (info) && info->dynamic))
6229 {
6230 struct elf_info_failed eif;
6231
6232 eif.info = info;
6233 eif.failed = FALSE;
6234 elf_link_hash_traverse (elf_hash_table (info),
6235 _bfd_elf_export_symbol,
6236 &eif);
6237 if (eif.failed)
6238 return FALSE;
6239 }
6240
6241 if (soname != NULL)
6242 {
6243 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6244 soname, TRUE);
6245 if (soname_indx == (size_t) -1
6246 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6247 return FALSE;
6248 }
6249 else
6250 soname_indx = (size_t) -1;
6251
6252 /* Make all global versions with definition. */
6253 for (t = info->version_info; t != NULL; t = t->next)
6254 for (d = t->globals.list; d != NULL; d = d->next)
6255 if (!d->symver && d->literal)
6256 {
6257 const char *verstr, *name;
6258 size_t namelen, verlen, newlen;
6259 char *newname, *p, leading_char;
6260 struct elf_link_hash_entry *newh;
6261
6262 leading_char = bfd_get_symbol_leading_char (output_bfd);
6263 name = d->pattern;
6264 namelen = strlen (name) + (leading_char != '\0');
6265 verstr = t->name;
6266 verlen = strlen (verstr);
6267 newlen = namelen + verlen + 3;
6268
6269 newname = (char *) bfd_malloc (newlen);
6270 if (newname == NULL)
6271 return FALSE;
6272 newname[0] = leading_char;
6273 memcpy (newname + (leading_char != '\0'), name, namelen);
6274
6275 /* Check the hidden versioned definition. */
6276 p = newname + namelen;
6277 *p++ = ELF_VER_CHR;
6278 memcpy (p, verstr, verlen + 1);
6279 newh = elf_link_hash_lookup (elf_hash_table (info),
6280 newname, FALSE, FALSE,
6281 FALSE);
6282 if (newh == NULL
6283 || (newh->root.type != bfd_link_hash_defined
6284 && newh->root.type != bfd_link_hash_defweak))
6285 {
6286 /* Check the default versioned definition. */
6287 *p++ = ELF_VER_CHR;
6288 memcpy (p, verstr, verlen + 1);
6289 newh = elf_link_hash_lookup (elf_hash_table (info),
6290 newname, FALSE, FALSE,
6291 FALSE);
6292 }
6293 free (newname);
6294
6295 /* Mark this version if there is a definition and it is
6296 not defined in a shared object. */
6297 if (newh != NULL
6298 && !newh->def_dynamic
6299 && (newh->root.type == bfd_link_hash_defined
6300 || newh->root.type == bfd_link_hash_defweak))
6301 d->symver = 1;
6302 }
6303
6304 /* Attach all the symbols to their version information. */
6305 asvinfo.info = info;
6306 asvinfo.failed = FALSE;
6307
6308 elf_link_hash_traverse (elf_hash_table (info),
6309 _bfd_elf_link_assign_sym_version,
6310 &asvinfo);
6311 if (asvinfo.failed)
6312 return FALSE;
6313
6314 if (!info->allow_undefined_version)
6315 {
6316 /* Check if all global versions have a definition. */
6317 bfd_boolean all_defined = TRUE;
6318 for (t = info->version_info; t != NULL; t = t->next)
6319 for (d = t->globals.list; d != NULL; d = d->next)
6320 if (d->literal && !d->symver && !d->script)
6321 {
6322 _bfd_error_handler
6323 (_("%s: undefined version: %s"),
6324 d->pattern, t->name);
6325 all_defined = FALSE;
6326 }
6327
6328 if (!all_defined)
6329 {
6330 bfd_set_error (bfd_error_bad_value);
6331 return FALSE;
6332 }
6333 }
6334
6335 /* Set up the version definition section. */
6336 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6337 BFD_ASSERT (s != NULL);
6338
6339 /* We may have created additional version definitions if we are
6340 just linking a regular application. */
6341 verdefs = info->version_info;
6342
6343 /* Skip anonymous version tag. */
6344 if (verdefs != NULL && verdefs->vernum == 0)
6345 verdefs = verdefs->next;
6346
6347 if (verdefs == NULL && !info->create_default_symver)
6348 s->flags |= SEC_EXCLUDE;
6349 else
6350 {
6351 unsigned int cdefs;
6352 bfd_size_type size;
6353 bfd_byte *p;
6354 Elf_Internal_Verdef def;
6355 Elf_Internal_Verdaux defaux;
6356 struct bfd_link_hash_entry *bh;
6357 struct elf_link_hash_entry *h;
6358 const char *name;
6359
6360 cdefs = 0;
6361 size = 0;
6362
6363 /* Make space for the base version. */
6364 size += sizeof (Elf_External_Verdef);
6365 size += sizeof (Elf_External_Verdaux);
6366 ++cdefs;
6367
6368 /* Make space for the default version. */
6369 if (info->create_default_symver)
6370 {
6371 size += sizeof (Elf_External_Verdef);
6372 ++cdefs;
6373 }
6374
6375 for (t = verdefs; t != NULL; t = t->next)
6376 {
6377 struct bfd_elf_version_deps *n;
6378
6379 /* Don't emit base version twice. */
6380 if (t->vernum == 0)
6381 continue;
6382
6383 size += sizeof (Elf_External_Verdef);
6384 size += sizeof (Elf_External_Verdaux);
6385 ++cdefs;
6386
6387 for (n = t->deps; n != NULL; n = n->next)
6388 size += sizeof (Elf_External_Verdaux);
6389 }
6390
6391 s->size = size;
6392 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6393 if (s->contents == NULL && s->size != 0)
6394 return FALSE;
6395
6396 /* Fill in the version definition section. */
6397
6398 p = s->contents;
6399
6400 def.vd_version = VER_DEF_CURRENT;
6401 def.vd_flags = VER_FLG_BASE;
6402 def.vd_ndx = 1;
6403 def.vd_cnt = 1;
6404 if (info->create_default_symver)
6405 {
6406 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6407 def.vd_next = sizeof (Elf_External_Verdef);
6408 }
6409 else
6410 {
6411 def.vd_aux = sizeof (Elf_External_Verdef);
6412 def.vd_next = (sizeof (Elf_External_Verdef)
6413 + sizeof (Elf_External_Verdaux));
6414 }
6415
6416 if (soname_indx != (size_t) -1)
6417 {
6418 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6419 soname_indx);
6420 def.vd_hash = bfd_elf_hash (soname);
6421 defaux.vda_name = soname_indx;
6422 name = soname;
6423 }
6424 else
6425 {
6426 size_t indx;
6427
6428 name = lbasename (output_bfd->filename);
6429 def.vd_hash = bfd_elf_hash (name);
6430 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6431 name, FALSE);
6432 if (indx == (size_t) -1)
6433 return FALSE;
6434 defaux.vda_name = indx;
6435 }
6436 defaux.vda_next = 0;
6437
6438 _bfd_elf_swap_verdef_out (output_bfd, &def,
6439 (Elf_External_Verdef *) p);
6440 p += sizeof (Elf_External_Verdef);
6441 if (info->create_default_symver)
6442 {
6443 /* Add a symbol representing this version. */
6444 bh = NULL;
6445 if (! (_bfd_generic_link_add_one_symbol
6446 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6447 0, NULL, FALSE,
6448 get_elf_backend_data (dynobj)->collect, &bh)))
6449 return FALSE;
6450 h = (struct elf_link_hash_entry *) bh;
6451 h->non_elf = 0;
6452 h->def_regular = 1;
6453 h->type = STT_OBJECT;
6454 h->verinfo.vertree = NULL;
6455
6456 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6457 return FALSE;
6458
6459 /* Create a duplicate of the base version with the same
6460 aux block, but different flags. */
6461 def.vd_flags = 0;
6462 def.vd_ndx = 2;
6463 def.vd_aux = sizeof (Elf_External_Verdef);
6464 if (verdefs)
6465 def.vd_next = (sizeof (Elf_External_Verdef)
6466 + sizeof (Elf_External_Verdaux));
6467 else
6468 def.vd_next = 0;
6469 _bfd_elf_swap_verdef_out (output_bfd, &def,
6470 (Elf_External_Verdef *) p);
6471 p += sizeof (Elf_External_Verdef);
6472 }
6473 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6474 (Elf_External_Verdaux *) p);
6475 p += sizeof (Elf_External_Verdaux);
6476
6477 for (t = verdefs; t != NULL; t = t->next)
6478 {
6479 unsigned int cdeps;
6480 struct bfd_elf_version_deps *n;
6481
6482 /* Don't emit the base version twice. */
6483 if (t->vernum == 0)
6484 continue;
6485
6486 cdeps = 0;
6487 for (n = t->deps; n != NULL; n = n->next)
6488 ++cdeps;
6489
6490 /* Add a symbol representing this version. */
6491 bh = NULL;
6492 if (! (_bfd_generic_link_add_one_symbol
6493 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6494 0, NULL, FALSE,
6495 get_elf_backend_data (dynobj)->collect, &bh)))
6496 return FALSE;
6497 h = (struct elf_link_hash_entry *) bh;
6498 h->non_elf = 0;
6499 h->def_regular = 1;
6500 h->type = STT_OBJECT;
6501 h->verinfo.vertree = t;
6502
6503 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6504 return FALSE;
6505
6506 def.vd_version = VER_DEF_CURRENT;
6507 def.vd_flags = 0;
6508 if (t->globals.list == NULL
6509 && t->locals.list == NULL
6510 && ! t->used)
6511 def.vd_flags |= VER_FLG_WEAK;
6512 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6513 def.vd_cnt = cdeps + 1;
6514 def.vd_hash = bfd_elf_hash (t->name);
6515 def.vd_aux = sizeof (Elf_External_Verdef);
6516 def.vd_next = 0;
6517
6518 /* If a basever node is next, it *must* be the last node in
6519 the chain, otherwise Verdef construction breaks. */
6520 if (t->next != NULL && t->next->vernum == 0)
6521 BFD_ASSERT (t->next->next == NULL);
6522
6523 if (t->next != NULL && t->next->vernum != 0)
6524 def.vd_next = (sizeof (Elf_External_Verdef)
6525 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6526
6527 _bfd_elf_swap_verdef_out (output_bfd, &def,
6528 (Elf_External_Verdef *) p);
6529 p += sizeof (Elf_External_Verdef);
6530
6531 defaux.vda_name = h->dynstr_index;
6532 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6533 h->dynstr_index);
6534 defaux.vda_next = 0;
6535 if (t->deps != NULL)
6536 defaux.vda_next = sizeof (Elf_External_Verdaux);
6537 t->name_indx = defaux.vda_name;
6538
6539 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6540 (Elf_External_Verdaux *) p);
6541 p += sizeof (Elf_External_Verdaux);
6542
6543 for (n = t->deps; n != NULL; n = n->next)
6544 {
6545 if (n->version_needed == NULL)
6546 {
6547 /* This can happen if there was an error in the
6548 version script. */
6549 defaux.vda_name = 0;
6550 }
6551 else
6552 {
6553 defaux.vda_name = n->version_needed->name_indx;
6554 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6555 defaux.vda_name);
6556 }
6557 if (n->next == NULL)
6558 defaux.vda_next = 0;
6559 else
6560 defaux.vda_next = sizeof (Elf_External_Verdaux);
6561
6562 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6563 (Elf_External_Verdaux *) p);
6564 p += sizeof (Elf_External_Verdaux);
6565 }
6566 }
6567
6568 elf_tdata (output_bfd)->cverdefs = cdefs;
6569 }
6570 }
6571
6572 bed = get_elf_backend_data (output_bfd);
6573
6574 if (info->gc_sections && bed->can_gc_sections)
6575 {
6576 struct elf_gc_sweep_symbol_info sweep_info;
6577
6578 /* Remove the symbols that were in the swept sections from the
6579 dynamic symbol table. */
6580 sweep_info.info = info;
6581 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6582 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6583 &sweep_info);
6584 }
6585
6586 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6587 {
6588 asection *s;
6589 struct elf_find_verdep_info sinfo;
6590
6591 /* Work out the size of the version reference section. */
6592
6593 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6594 BFD_ASSERT (s != NULL);
6595
6596 sinfo.info = info;
6597 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6598 if (sinfo.vers == 0)
6599 sinfo.vers = 1;
6600 sinfo.failed = FALSE;
6601
6602 elf_link_hash_traverse (elf_hash_table (info),
6603 _bfd_elf_link_find_version_dependencies,
6604 &sinfo);
6605 if (sinfo.failed)
6606 return FALSE;
6607
6608 if (elf_tdata (output_bfd)->verref == NULL)
6609 s->flags |= SEC_EXCLUDE;
6610 else
6611 {
6612 Elf_Internal_Verneed *vn;
6613 unsigned int size;
6614 unsigned int crefs;
6615 bfd_byte *p;
6616
6617 /* Build the version dependency section. */
6618 size = 0;
6619 crefs = 0;
6620 for (vn = elf_tdata (output_bfd)->verref;
6621 vn != NULL;
6622 vn = vn->vn_nextref)
6623 {
6624 Elf_Internal_Vernaux *a;
6625
6626 size += sizeof (Elf_External_Verneed);
6627 ++crefs;
6628 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6629 size += sizeof (Elf_External_Vernaux);
6630 }
6631
6632 s->size = size;
6633 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6634 if (s->contents == NULL)
6635 return FALSE;
6636
6637 p = s->contents;
6638 for (vn = elf_tdata (output_bfd)->verref;
6639 vn != NULL;
6640 vn = vn->vn_nextref)
6641 {
6642 unsigned int caux;
6643 Elf_Internal_Vernaux *a;
6644 size_t indx;
6645
6646 caux = 0;
6647 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6648 ++caux;
6649
6650 vn->vn_version = VER_NEED_CURRENT;
6651 vn->vn_cnt = caux;
6652 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6653 elf_dt_name (vn->vn_bfd) != NULL
6654 ? elf_dt_name (vn->vn_bfd)
6655 : lbasename (vn->vn_bfd->filename),
6656 FALSE);
6657 if (indx == (size_t) -1)
6658 return FALSE;
6659 vn->vn_file = indx;
6660 vn->vn_aux = sizeof (Elf_External_Verneed);
6661 if (vn->vn_nextref == NULL)
6662 vn->vn_next = 0;
6663 else
6664 vn->vn_next = (sizeof (Elf_External_Verneed)
6665 + caux * sizeof (Elf_External_Vernaux));
6666
6667 _bfd_elf_swap_verneed_out (output_bfd, vn,
6668 (Elf_External_Verneed *) p);
6669 p += sizeof (Elf_External_Verneed);
6670
6671 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6672 {
6673 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6674 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6675 a->vna_nodename, FALSE);
6676 if (indx == (size_t) -1)
6677 return FALSE;
6678 a->vna_name = indx;
6679 if (a->vna_nextptr == NULL)
6680 a->vna_next = 0;
6681 else
6682 a->vna_next = sizeof (Elf_External_Vernaux);
6683
6684 _bfd_elf_swap_vernaux_out (output_bfd, a,
6685 (Elf_External_Vernaux *) p);
6686 p += sizeof (Elf_External_Vernaux);
6687 }
6688 }
6689
6690 elf_tdata (output_bfd)->cverrefs = crefs;
6691 }
6692 }
6693
6694 /* Any syms created from now on start with -1 in
6695 got.refcount/offset and plt.refcount/offset. */
6696 elf_hash_table (info)->init_got_refcount
6697 = elf_hash_table (info)->init_got_offset;
6698 elf_hash_table (info)->init_plt_refcount
6699 = elf_hash_table (info)->init_plt_offset;
6700
6701 if (bfd_link_relocatable (info)
6702 && !_bfd_elf_size_group_sections (info))
6703 return FALSE;
6704
6705 /* The backend may have to create some sections regardless of whether
6706 we're dynamic or not. */
6707 if (bed->elf_backend_always_size_sections
6708 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6709 return FALSE;
6710
6711 /* Determine any GNU_STACK segment requirements, after the backend
6712 has had a chance to set a default segment size. */
6713 if (info->execstack)
6714 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6715 else if (info->noexecstack)
6716 elf_stack_flags (output_bfd) = PF_R | PF_W;
6717 else
6718 {
6719 bfd *inputobj;
6720 asection *notesec = NULL;
6721 int exec = 0;
6722
6723 for (inputobj = info->input_bfds;
6724 inputobj;
6725 inputobj = inputobj->link.next)
6726 {
6727 asection *s;
6728
6729 if (inputobj->flags
6730 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6731 continue;
6732 s = inputobj->sections;
6733 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6734 continue;
6735
6736 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6737 if (s)
6738 {
6739 if (s->flags & SEC_CODE)
6740 exec = PF_X;
6741 notesec = s;
6742 }
6743 else if (bed->default_execstack)
6744 exec = PF_X;
6745 }
6746 if (notesec || info->stacksize > 0)
6747 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6748 if (notesec && exec && bfd_link_relocatable (info)
6749 && notesec->output_section != bfd_abs_section_ptr)
6750 notesec->output_section->flags |= SEC_CODE;
6751 }
6752
6753 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6754 {
6755 struct elf_info_failed eif;
6756 struct elf_link_hash_entry *h;
6757 asection *dynstr;
6758 asection *s;
6759
6760 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6761 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6762
6763 if (info->symbolic)
6764 {
6765 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6766 return FALSE;
6767 info->flags |= DF_SYMBOLIC;
6768 }
6769
6770 if (rpath != NULL)
6771 {
6772 size_t indx;
6773 bfd_vma tag;
6774
6775 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6776 TRUE);
6777 if (indx == (size_t) -1)
6778 return FALSE;
6779
6780 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6781 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6782 return FALSE;
6783 }
6784
6785 if (filter_shlib != NULL)
6786 {
6787 size_t indx;
6788
6789 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6790 filter_shlib, TRUE);
6791 if (indx == (size_t) -1
6792 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6793 return FALSE;
6794 }
6795
6796 if (auxiliary_filters != NULL)
6797 {
6798 const char * const *p;
6799
6800 for (p = auxiliary_filters; *p != NULL; p++)
6801 {
6802 size_t indx;
6803
6804 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6805 *p, TRUE);
6806 if (indx == (size_t) -1
6807 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6808 return FALSE;
6809 }
6810 }
6811
6812 if (audit != NULL)
6813 {
6814 size_t indx;
6815
6816 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6817 TRUE);
6818 if (indx == (size_t) -1
6819 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6820 return FALSE;
6821 }
6822
6823 if (depaudit != NULL)
6824 {
6825 size_t indx;
6826
6827 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6828 TRUE);
6829 if (indx == (size_t) -1
6830 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6831 return FALSE;
6832 }
6833
6834 eif.info = info;
6835 eif.failed = FALSE;
6836
6837 /* Find all symbols which were defined in a dynamic object and make
6838 the backend pick a reasonable value for them. */
6839 elf_link_hash_traverse (elf_hash_table (info),
6840 _bfd_elf_adjust_dynamic_symbol,
6841 &eif);
6842 if (eif.failed)
6843 return FALSE;
6844
6845 /* Add some entries to the .dynamic section. We fill in some of the
6846 values later, in bfd_elf_final_link, but we must add the entries
6847 now so that we know the final size of the .dynamic section. */
6848
6849 /* If there are initialization and/or finalization functions to
6850 call then add the corresponding DT_INIT/DT_FINI entries. */
6851 h = (info->init_function
6852 ? elf_link_hash_lookup (elf_hash_table (info),
6853 info->init_function, FALSE,
6854 FALSE, FALSE)
6855 : NULL);
6856 if (h != NULL
6857 && (h->ref_regular
6858 || h->def_regular))
6859 {
6860 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6861 return FALSE;
6862 }
6863 h = (info->fini_function
6864 ? elf_link_hash_lookup (elf_hash_table (info),
6865 info->fini_function, FALSE,
6866 FALSE, FALSE)
6867 : NULL);
6868 if (h != NULL
6869 && (h->ref_regular
6870 || h->def_regular))
6871 {
6872 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6873 return FALSE;
6874 }
6875
6876 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6877 if (s != NULL && s->linker_has_input)
6878 {
6879 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6880 if (! bfd_link_executable (info))
6881 {
6882 bfd *sub;
6883 asection *o;
6884
6885 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6886 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
6887 && (o = sub->sections) != NULL
6888 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
6889 for (o = sub->sections; o != NULL; o = o->next)
6890 if (elf_section_data (o)->this_hdr.sh_type
6891 == SHT_PREINIT_ARRAY)
6892 {
6893 _bfd_error_handler
6894 (_("%pB: .preinit_array section is not allowed in DSO"),
6895 sub);
6896 break;
6897 }
6898
6899 bfd_set_error (bfd_error_nonrepresentable_section);
6900 return FALSE;
6901 }
6902
6903 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6904 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6905 return FALSE;
6906 }
6907 s = bfd_get_section_by_name (output_bfd, ".init_array");
6908 if (s != NULL && s->linker_has_input)
6909 {
6910 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6911 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6912 return FALSE;
6913 }
6914 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6915 if (s != NULL && s->linker_has_input)
6916 {
6917 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6918 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6919 return FALSE;
6920 }
6921
6922 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6923 /* If .dynstr is excluded from the link, we don't want any of
6924 these tags. Strictly, we should be checking each section
6925 individually; This quick check covers for the case where
6926 someone does a /DISCARD/ : { *(*) }. */
6927 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6928 {
6929 bfd_size_type strsize;
6930
6931 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6932 if ((info->emit_hash
6933 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6934 || (info->emit_gnu_hash
6935 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6936 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6937 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6938 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6939 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6940 bed->s->sizeof_sym))
6941 return FALSE;
6942 }
6943 }
6944
6945 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6946 return FALSE;
6947
6948 /* The backend must work out the sizes of all the other dynamic
6949 sections. */
6950 if (dynobj != NULL
6951 && bed->elf_backend_size_dynamic_sections != NULL
6952 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6953 return FALSE;
6954
6955 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6956 {
6957 if (elf_tdata (output_bfd)->cverdefs)
6958 {
6959 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
6960
6961 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6962 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
6963 return FALSE;
6964 }
6965
6966 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6967 {
6968 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6969 return FALSE;
6970 }
6971 else if (info->flags & DF_BIND_NOW)
6972 {
6973 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6974 return FALSE;
6975 }
6976
6977 if (info->flags_1)
6978 {
6979 if (bfd_link_executable (info))
6980 info->flags_1 &= ~ (DF_1_INITFIRST
6981 | DF_1_NODELETE
6982 | DF_1_NOOPEN);
6983 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6984 return FALSE;
6985 }
6986
6987 if (elf_tdata (output_bfd)->cverrefs)
6988 {
6989 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
6990
6991 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6992 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6993 return FALSE;
6994 }
6995
6996 if ((elf_tdata (output_bfd)->cverrefs == 0
6997 && elf_tdata (output_bfd)->cverdefs == 0)
6998 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
6999 {
7000 asection *s;
7001
7002 s = bfd_get_linker_section (dynobj, ".gnu.version");
7003 s->flags |= SEC_EXCLUDE;
7004 }
7005 }
7006 return TRUE;
7007 }
7008
7009 /* Find the first non-excluded output section. We'll use its
7010 section symbol for some emitted relocs. */
7011 void
7012 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7013 {
7014 asection *s;
7015
7016 for (s = output_bfd->sections; s != NULL; s = s->next)
7017 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7018 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7019 {
7020 elf_hash_table (info)->text_index_section = s;
7021 break;
7022 }
7023 }
7024
7025 /* Find two non-excluded output sections, one for code, one for data.
7026 We'll use their section symbols for some emitted relocs. */
7027 void
7028 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7029 {
7030 asection *s;
7031
7032 /* Data first, since setting text_index_section changes
7033 _bfd_elf_omit_section_dynsym_default. */
7034 for (s = output_bfd->sections; s != NULL; s = s->next)
7035 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
7036 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7037 {
7038 elf_hash_table (info)->data_index_section = s;
7039 break;
7040 }
7041
7042 for (s = output_bfd->sections; s != NULL; s = s->next)
7043 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
7044 == (SEC_ALLOC | SEC_READONLY))
7045 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7046 {
7047 elf_hash_table (info)->text_index_section = s;
7048 break;
7049 }
7050
7051 if (elf_hash_table (info)->text_index_section == NULL)
7052 elf_hash_table (info)->text_index_section
7053 = elf_hash_table (info)->data_index_section;
7054 }
7055
7056 bfd_boolean
7057 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7058 {
7059 const struct elf_backend_data *bed;
7060 unsigned long section_sym_count;
7061 bfd_size_type dynsymcount = 0;
7062
7063 if (!is_elf_hash_table (info->hash))
7064 return TRUE;
7065
7066 bed = get_elf_backend_data (output_bfd);
7067 (*bed->elf_backend_init_index_section) (output_bfd, info);
7068
7069 /* Assign dynsym indices. In a shared library we generate a section
7070 symbol for each output section, which come first. Next come all
7071 of the back-end allocated local dynamic syms, followed by the rest
7072 of the global symbols.
7073
7074 This is usually not needed for static binaries, however backends
7075 can request to always do it, e.g. the MIPS backend uses dynamic
7076 symbol counts to lay out GOT, which will be produced in the
7077 presence of GOT relocations even in static binaries (holding fixed
7078 data in that case, to satisfy those relocations). */
7079
7080 if (elf_hash_table (info)->dynamic_sections_created
7081 || bed->always_renumber_dynsyms)
7082 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7083 &section_sym_count);
7084
7085 if (elf_hash_table (info)->dynamic_sections_created)
7086 {
7087 bfd *dynobj;
7088 asection *s;
7089 unsigned int dtagcount;
7090
7091 dynobj = elf_hash_table (info)->dynobj;
7092
7093 /* Work out the size of the symbol version section. */
7094 s = bfd_get_linker_section (dynobj, ".gnu.version");
7095 BFD_ASSERT (s != NULL);
7096 if ((s->flags & SEC_EXCLUDE) == 0)
7097 {
7098 s->size = dynsymcount * sizeof (Elf_External_Versym);
7099 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7100 if (s->contents == NULL)
7101 return FALSE;
7102
7103 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7104 return FALSE;
7105 }
7106
7107 /* Set the size of the .dynsym and .hash sections. We counted
7108 the number of dynamic symbols in elf_link_add_object_symbols.
7109 We will build the contents of .dynsym and .hash when we build
7110 the final symbol table, because until then we do not know the
7111 correct value to give the symbols. We built the .dynstr
7112 section as we went along in elf_link_add_object_symbols. */
7113 s = elf_hash_table (info)->dynsym;
7114 BFD_ASSERT (s != NULL);
7115 s->size = dynsymcount * bed->s->sizeof_sym;
7116
7117 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7118 if (s->contents == NULL)
7119 return FALSE;
7120
7121 /* The first entry in .dynsym is a dummy symbol. Clear all the
7122 section syms, in case we don't output them all. */
7123 ++section_sym_count;
7124 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7125
7126 elf_hash_table (info)->bucketcount = 0;
7127
7128 /* Compute the size of the hashing table. As a side effect this
7129 computes the hash values for all the names we export. */
7130 if (info->emit_hash)
7131 {
7132 unsigned long int *hashcodes;
7133 struct hash_codes_info hashinf;
7134 bfd_size_type amt;
7135 unsigned long int nsyms;
7136 size_t bucketcount;
7137 size_t hash_entry_size;
7138
7139 /* Compute the hash values for all exported symbols. At the same
7140 time store the values in an array so that we could use them for
7141 optimizations. */
7142 amt = dynsymcount * sizeof (unsigned long int);
7143 hashcodes = (unsigned long int *) bfd_malloc (amt);
7144 if (hashcodes == NULL)
7145 return FALSE;
7146 hashinf.hashcodes = hashcodes;
7147 hashinf.error = FALSE;
7148
7149 /* Put all hash values in HASHCODES. */
7150 elf_link_hash_traverse (elf_hash_table (info),
7151 elf_collect_hash_codes, &hashinf);
7152 if (hashinf.error)
7153 {
7154 free (hashcodes);
7155 return FALSE;
7156 }
7157
7158 nsyms = hashinf.hashcodes - hashcodes;
7159 bucketcount
7160 = compute_bucket_count (info, hashcodes, nsyms, 0);
7161 free (hashcodes);
7162
7163 if (bucketcount == 0 && nsyms > 0)
7164 return FALSE;
7165
7166 elf_hash_table (info)->bucketcount = bucketcount;
7167
7168 s = bfd_get_linker_section (dynobj, ".hash");
7169 BFD_ASSERT (s != NULL);
7170 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7171 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7172 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7173 if (s->contents == NULL)
7174 return FALSE;
7175
7176 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7177 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7178 s->contents + hash_entry_size);
7179 }
7180
7181 if (info->emit_gnu_hash)
7182 {
7183 size_t i, cnt;
7184 unsigned char *contents;
7185 struct collect_gnu_hash_codes cinfo;
7186 bfd_size_type amt;
7187 size_t bucketcount;
7188
7189 memset (&cinfo, 0, sizeof (cinfo));
7190
7191 /* Compute the hash values for all exported symbols. At the same
7192 time store the values in an array so that we could use them for
7193 optimizations. */
7194 amt = dynsymcount * 2 * sizeof (unsigned long int);
7195 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7196 if (cinfo.hashcodes == NULL)
7197 return FALSE;
7198
7199 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7200 cinfo.min_dynindx = -1;
7201 cinfo.output_bfd = output_bfd;
7202 cinfo.bed = bed;
7203
7204 /* Put all hash values in HASHCODES. */
7205 elf_link_hash_traverse (elf_hash_table (info),
7206 elf_collect_gnu_hash_codes, &cinfo);
7207 if (cinfo.error)
7208 {
7209 free (cinfo.hashcodes);
7210 return FALSE;
7211 }
7212
7213 bucketcount
7214 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7215
7216 if (bucketcount == 0)
7217 {
7218 free (cinfo.hashcodes);
7219 return FALSE;
7220 }
7221
7222 s = bfd_get_linker_section (dynobj, ".gnu.hash");
7223 BFD_ASSERT (s != NULL);
7224
7225 if (cinfo.nsyms == 0)
7226 {
7227 /* Empty .gnu.hash section is special. */
7228 BFD_ASSERT (cinfo.min_dynindx == -1);
7229 free (cinfo.hashcodes);
7230 s->size = 5 * 4 + bed->s->arch_size / 8;
7231 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7232 if (contents == NULL)
7233 return FALSE;
7234 s->contents = contents;
7235 /* 1 empty bucket. */
7236 bfd_put_32 (output_bfd, 1, contents);
7237 /* SYMIDX above the special symbol 0. */
7238 bfd_put_32 (output_bfd, 1, contents + 4);
7239 /* Just one word for bitmask. */
7240 bfd_put_32 (output_bfd, 1, contents + 8);
7241 /* Only hash fn bloom filter. */
7242 bfd_put_32 (output_bfd, 0, contents + 12);
7243 /* No hashes are valid - empty bitmask. */
7244 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7245 /* No hashes in the only bucket. */
7246 bfd_put_32 (output_bfd, 0,
7247 contents + 16 + bed->s->arch_size / 8);
7248 }
7249 else
7250 {
7251 unsigned long int maskwords, maskbitslog2, x;
7252 BFD_ASSERT (cinfo.min_dynindx != -1);
7253
7254 x = cinfo.nsyms;
7255 maskbitslog2 = 1;
7256 while ((x >>= 1) != 0)
7257 ++maskbitslog2;
7258 if (maskbitslog2 < 3)
7259 maskbitslog2 = 5;
7260 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7261 maskbitslog2 = maskbitslog2 + 3;
7262 else
7263 maskbitslog2 = maskbitslog2 + 2;
7264 if (bed->s->arch_size == 64)
7265 {
7266 if (maskbitslog2 == 5)
7267 maskbitslog2 = 6;
7268 cinfo.shift1 = 6;
7269 }
7270 else
7271 cinfo.shift1 = 5;
7272 cinfo.mask = (1 << cinfo.shift1) - 1;
7273 cinfo.shift2 = maskbitslog2;
7274 cinfo.maskbits = 1 << maskbitslog2;
7275 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7276 amt = bucketcount * sizeof (unsigned long int) * 2;
7277 amt += maskwords * sizeof (bfd_vma);
7278 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7279 if (cinfo.bitmask == NULL)
7280 {
7281 free (cinfo.hashcodes);
7282 return FALSE;
7283 }
7284
7285 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7286 cinfo.indx = cinfo.counts + bucketcount;
7287 cinfo.symindx = dynsymcount - cinfo.nsyms;
7288 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7289
7290 /* Determine how often each hash bucket is used. */
7291 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7292 for (i = 0; i < cinfo.nsyms; ++i)
7293 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7294
7295 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7296 if (cinfo.counts[i] != 0)
7297 {
7298 cinfo.indx[i] = cnt;
7299 cnt += cinfo.counts[i];
7300 }
7301 BFD_ASSERT (cnt == dynsymcount);
7302 cinfo.bucketcount = bucketcount;
7303 cinfo.local_indx = cinfo.min_dynindx;
7304
7305 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7306 s->size += cinfo.maskbits / 8;
7307 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7308 if (contents == NULL)
7309 {
7310 free (cinfo.bitmask);
7311 free (cinfo.hashcodes);
7312 return FALSE;
7313 }
7314
7315 s->contents = contents;
7316 bfd_put_32 (output_bfd, bucketcount, contents);
7317 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7318 bfd_put_32 (output_bfd, maskwords, contents + 8);
7319 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7320 contents += 16 + cinfo.maskbits / 8;
7321
7322 for (i = 0; i < bucketcount; ++i)
7323 {
7324 if (cinfo.counts[i] == 0)
7325 bfd_put_32 (output_bfd, 0, contents);
7326 else
7327 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7328 contents += 4;
7329 }
7330
7331 cinfo.contents = contents;
7332
7333 /* Renumber dynamic symbols, populate .gnu.hash section. */
7334 elf_link_hash_traverse (elf_hash_table (info),
7335 elf_renumber_gnu_hash_syms, &cinfo);
7336
7337 contents = s->contents + 16;
7338 for (i = 0; i < maskwords; ++i)
7339 {
7340 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7341 contents);
7342 contents += bed->s->arch_size / 8;
7343 }
7344
7345 free (cinfo.bitmask);
7346 free (cinfo.hashcodes);
7347 }
7348 }
7349
7350 s = bfd_get_linker_section (dynobj, ".dynstr");
7351 BFD_ASSERT (s != NULL);
7352
7353 elf_finalize_dynstr (output_bfd, info);
7354
7355 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7356
7357 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7358 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7359 return FALSE;
7360 }
7361
7362 return TRUE;
7363 }
7364 \f
7365 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7366
7367 static void
7368 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7369 asection *sec)
7370 {
7371 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7372 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7373 }
7374
7375 /* Finish SHF_MERGE section merging. */
7376
7377 bfd_boolean
7378 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7379 {
7380 bfd *ibfd;
7381 asection *sec;
7382
7383 if (!is_elf_hash_table (info->hash))
7384 return FALSE;
7385
7386 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7387 if ((ibfd->flags & DYNAMIC) == 0
7388 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7389 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7390 == get_elf_backend_data (obfd)->s->elfclass))
7391 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7392 if ((sec->flags & SEC_MERGE) != 0
7393 && !bfd_is_abs_section (sec->output_section))
7394 {
7395 struct bfd_elf_section_data *secdata;
7396
7397 secdata = elf_section_data (sec);
7398 if (! _bfd_add_merge_section (obfd,
7399 &elf_hash_table (info)->merge_info,
7400 sec, &secdata->sec_info))
7401 return FALSE;
7402 else if (secdata->sec_info)
7403 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7404 }
7405
7406 if (elf_hash_table (info)->merge_info != NULL)
7407 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7408 merge_sections_remove_hook);
7409 return TRUE;
7410 }
7411
7412 /* Create an entry in an ELF linker hash table. */
7413
7414 struct bfd_hash_entry *
7415 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7416 struct bfd_hash_table *table,
7417 const char *string)
7418 {
7419 /* Allocate the structure if it has not already been allocated by a
7420 subclass. */
7421 if (entry == NULL)
7422 {
7423 entry = (struct bfd_hash_entry *)
7424 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7425 if (entry == NULL)
7426 return entry;
7427 }
7428
7429 /* Call the allocation method of the superclass. */
7430 entry = _bfd_link_hash_newfunc (entry, table, string);
7431 if (entry != NULL)
7432 {
7433 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7434 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7435
7436 /* Set local fields. */
7437 ret->indx = -1;
7438 ret->dynindx = -1;
7439 ret->got = htab->init_got_refcount;
7440 ret->plt = htab->init_plt_refcount;
7441 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7442 - offsetof (struct elf_link_hash_entry, size)));
7443 /* Assume that we have been called by a non-ELF symbol reader.
7444 This flag is then reset by the code which reads an ELF input
7445 file. This ensures that a symbol created by a non-ELF symbol
7446 reader will have the flag set correctly. */
7447 ret->non_elf = 1;
7448 }
7449
7450 return entry;
7451 }
7452
7453 /* Copy data from an indirect symbol to its direct symbol, hiding the
7454 old indirect symbol. Also used for copying flags to a weakdef. */
7455
7456 void
7457 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7458 struct elf_link_hash_entry *dir,
7459 struct elf_link_hash_entry *ind)
7460 {
7461 struct elf_link_hash_table *htab;
7462
7463 /* Copy down any references that we may have already seen to the
7464 symbol which just became indirect. */
7465
7466 if (dir->versioned != versioned_hidden)
7467 dir->ref_dynamic |= ind->ref_dynamic;
7468 dir->ref_regular |= ind->ref_regular;
7469 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7470 dir->non_got_ref |= ind->non_got_ref;
7471 dir->needs_plt |= ind->needs_plt;
7472 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7473
7474 if (ind->root.type != bfd_link_hash_indirect)
7475 return;
7476
7477 /* Copy over the global and procedure linkage table refcount entries.
7478 These may have been already set up by a check_relocs routine. */
7479 htab = elf_hash_table (info);
7480 if (ind->got.refcount > htab->init_got_refcount.refcount)
7481 {
7482 if (dir->got.refcount < 0)
7483 dir->got.refcount = 0;
7484 dir->got.refcount += ind->got.refcount;
7485 ind->got.refcount = htab->init_got_refcount.refcount;
7486 }
7487
7488 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7489 {
7490 if (dir->plt.refcount < 0)
7491 dir->plt.refcount = 0;
7492 dir->plt.refcount += ind->plt.refcount;
7493 ind->plt.refcount = htab->init_plt_refcount.refcount;
7494 }
7495
7496 if (ind->dynindx != -1)
7497 {
7498 if (dir->dynindx != -1)
7499 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7500 dir->dynindx = ind->dynindx;
7501 dir->dynstr_index = ind->dynstr_index;
7502 ind->dynindx = -1;
7503 ind->dynstr_index = 0;
7504 }
7505 }
7506
7507 void
7508 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7509 struct elf_link_hash_entry *h,
7510 bfd_boolean force_local)
7511 {
7512 /* STT_GNU_IFUNC symbol must go through PLT. */
7513 if (h->type != STT_GNU_IFUNC)
7514 {
7515 h->plt = elf_hash_table (info)->init_plt_offset;
7516 h->needs_plt = 0;
7517 }
7518 if (force_local)
7519 {
7520 h->forced_local = 1;
7521 if (h->dynindx != -1)
7522 {
7523 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7524 h->dynstr_index);
7525 h->dynindx = -1;
7526 h->dynstr_index = 0;
7527 }
7528 }
7529 }
7530
7531 /* Hide a symbol. */
7532
7533 void
7534 _bfd_elf_link_hide_symbol (bfd *output_bfd,
7535 struct bfd_link_info *info,
7536 struct bfd_link_hash_entry *h)
7537 {
7538 if (is_elf_hash_table (info->hash))
7539 {
7540 const struct elf_backend_data *bed
7541 = get_elf_backend_data (output_bfd);
7542 struct elf_link_hash_entry *eh
7543 = (struct elf_link_hash_entry *) h;
7544 bed->elf_backend_hide_symbol (info, eh, TRUE);
7545 eh->def_dynamic = 0;
7546 eh->ref_dynamic = 0;
7547 eh->dynamic_def = 0;
7548 }
7549 }
7550
7551 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7552 caller. */
7553
7554 bfd_boolean
7555 _bfd_elf_link_hash_table_init
7556 (struct elf_link_hash_table *table,
7557 bfd *abfd,
7558 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7559 struct bfd_hash_table *,
7560 const char *),
7561 unsigned int entsize,
7562 enum elf_target_id target_id)
7563 {
7564 bfd_boolean ret;
7565 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7566
7567 table->init_got_refcount.refcount = can_refcount - 1;
7568 table->init_plt_refcount.refcount = can_refcount - 1;
7569 table->init_got_offset.offset = -(bfd_vma) 1;
7570 table->init_plt_offset.offset = -(bfd_vma) 1;
7571 /* The first dynamic symbol is a dummy. */
7572 table->dynsymcount = 1;
7573
7574 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7575
7576 table->root.type = bfd_link_elf_hash_table;
7577 table->hash_table_id = target_id;
7578
7579 return ret;
7580 }
7581
7582 /* Create an ELF linker hash table. */
7583
7584 struct bfd_link_hash_table *
7585 _bfd_elf_link_hash_table_create (bfd *abfd)
7586 {
7587 struct elf_link_hash_table *ret;
7588 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7589
7590 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7591 if (ret == NULL)
7592 return NULL;
7593
7594 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7595 sizeof (struct elf_link_hash_entry),
7596 GENERIC_ELF_DATA))
7597 {
7598 free (ret);
7599 return NULL;
7600 }
7601 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7602
7603 return &ret->root;
7604 }
7605
7606 /* Destroy an ELF linker hash table. */
7607
7608 void
7609 _bfd_elf_link_hash_table_free (bfd *obfd)
7610 {
7611 struct elf_link_hash_table *htab;
7612
7613 htab = (struct elf_link_hash_table *) obfd->link.hash;
7614 if (htab->dynstr != NULL)
7615 _bfd_elf_strtab_free (htab->dynstr);
7616 _bfd_merge_sections_free (htab->merge_info);
7617 _bfd_generic_link_hash_table_free (obfd);
7618 }
7619
7620 /* This is a hook for the ELF emulation code in the generic linker to
7621 tell the backend linker what file name to use for the DT_NEEDED
7622 entry for a dynamic object. */
7623
7624 void
7625 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7626 {
7627 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7628 && bfd_get_format (abfd) == bfd_object)
7629 elf_dt_name (abfd) = name;
7630 }
7631
7632 int
7633 bfd_elf_get_dyn_lib_class (bfd *abfd)
7634 {
7635 int lib_class;
7636 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7637 && bfd_get_format (abfd) == bfd_object)
7638 lib_class = elf_dyn_lib_class (abfd);
7639 else
7640 lib_class = 0;
7641 return lib_class;
7642 }
7643
7644 void
7645 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7646 {
7647 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7648 && bfd_get_format (abfd) == bfd_object)
7649 elf_dyn_lib_class (abfd) = lib_class;
7650 }
7651
7652 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7653 the linker ELF emulation code. */
7654
7655 struct bfd_link_needed_list *
7656 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7657 struct bfd_link_info *info)
7658 {
7659 if (! is_elf_hash_table (info->hash))
7660 return NULL;
7661 return elf_hash_table (info)->needed;
7662 }
7663
7664 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7665 hook for the linker ELF emulation code. */
7666
7667 struct bfd_link_needed_list *
7668 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7669 struct bfd_link_info *info)
7670 {
7671 if (! is_elf_hash_table (info->hash))
7672 return NULL;
7673 return elf_hash_table (info)->runpath;
7674 }
7675
7676 /* Get the name actually used for a dynamic object for a link. This
7677 is the SONAME entry if there is one. Otherwise, it is the string
7678 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7679
7680 const char *
7681 bfd_elf_get_dt_soname (bfd *abfd)
7682 {
7683 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7684 && bfd_get_format (abfd) == bfd_object)
7685 return elf_dt_name (abfd);
7686 return NULL;
7687 }
7688
7689 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7690 the ELF linker emulation code. */
7691
7692 bfd_boolean
7693 bfd_elf_get_bfd_needed_list (bfd *abfd,
7694 struct bfd_link_needed_list **pneeded)
7695 {
7696 asection *s;
7697 bfd_byte *dynbuf = NULL;
7698 unsigned int elfsec;
7699 unsigned long shlink;
7700 bfd_byte *extdyn, *extdynend;
7701 size_t extdynsize;
7702 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7703
7704 *pneeded = NULL;
7705
7706 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7707 || bfd_get_format (abfd) != bfd_object)
7708 return TRUE;
7709
7710 s = bfd_get_section_by_name (abfd, ".dynamic");
7711 if (s == NULL || s->size == 0)
7712 return TRUE;
7713
7714 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7715 goto error_return;
7716
7717 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7718 if (elfsec == SHN_BAD)
7719 goto error_return;
7720
7721 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7722
7723 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7724 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7725
7726 extdyn = dynbuf;
7727 extdynend = extdyn + s->size;
7728 for (; extdyn < extdynend; extdyn += extdynsize)
7729 {
7730 Elf_Internal_Dyn dyn;
7731
7732 (*swap_dyn_in) (abfd, extdyn, &dyn);
7733
7734 if (dyn.d_tag == DT_NULL)
7735 break;
7736
7737 if (dyn.d_tag == DT_NEEDED)
7738 {
7739 const char *string;
7740 struct bfd_link_needed_list *l;
7741 unsigned int tagv = dyn.d_un.d_val;
7742 bfd_size_type amt;
7743
7744 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7745 if (string == NULL)
7746 goto error_return;
7747
7748 amt = sizeof *l;
7749 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7750 if (l == NULL)
7751 goto error_return;
7752
7753 l->by = abfd;
7754 l->name = string;
7755 l->next = *pneeded;
7756 *pneeded = l;
7757 }
7758 }
7759
7760 free (dynbuf);
7761
7762 return TRUE;
7763
7764 error_return:
7765 if (dynbuf != NULL)
7766 free (dynbuf);
7767 return FALSE;
7768 }
7769
7770 struct elf_symbuf_symbol
7771 {
7772 unsigned long st_name; /* Symbol name, index in string tbl */
7773 unsigned char st_info; /* Type and binding attributes */
7774 unsigned char st_other; /* Visibilty, and target specific */
7775 };
7776
7777 struct elf_symbuf_head
7778 {
7779 struct elf_symbuf_symbol *ssym;
7780 size_t count;
7781 unsigned int st_shndx;
7782 };
7783
7784 struct elf_symbol
7785 {
7786 union
7787 {
7788 Elf_Internal_Sym *isym;
7789 struct elf_symbuf_symbol *ssym;
7790 } u;
7791 const char *name;
7792 };
7793
7794 /* Sort references to symbols by ascending section number. */
7795
7796 static int
7797 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7798 {
7799 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7800 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7801
7802 return s1->st_shndx - s2->st_shndx;
7803 }
7804
7805 static int
7806 elf_sym_name_compare (const void *arg1, const void *arg2)
7807 {
7808 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7809 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7810 return strcmp (s1->name, s2->name);
7811 }
7812
7813 static struct elf_symbuf_head *
7814 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
7815 {
7816 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7817 struct elf_symbuf_symbol *ssym;
7818 struct elf_symbuf_head *ssymbuf, *ssymhead;
7819 size_t i, shndx_count, total_size;
7820
7821 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7822 if (indbuf == NULL)
7823 return NULL;
7824
7825 for (ind = indbuf, i = 0; i < symcount; i++)
7826 if (isymbuf[i].st_shndx != SHN_UNDEF)
7827 *ind++ = &isymbuf[i];
7828 indbufend = ind;
7829
7830 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7831 elf_sort_elf_symbol);
7832
7833 shndx_count = 0;
7834 if (indbufend > indbuf)
7835 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7836 if (ind[0]->st_shndx != ind[1]->st_shndx)
7837 shndx_count++;
7838
7839 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7840 + (indbufend - indbuf) * sizeof (*ssym));
7841 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7842 if (ssymbuf == NULL)
7843 {
7844 free (indbuf);
7845 return NULL;
7846 }
7847
7848 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7849 ssymbuf->ssym = NULL;
7850 ssymbuf->count = shndx_count;
7851 ssymbuf->st_shndx = 0;
7852 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7853 {
7854 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7855 {
7856 ssymhead++;
7857 ssymhead->ssym = ssym;
7858 ssymhead->count = 0;
7859 ssymhead->st_shndx = (*ind)->st_shndx;
7860 }
7861 ssym->st_name = (*ind)->st_name;
7862 ssym->st_info = (*ind)->st_info;
7863 ssym->st_other = (*ind)->st_other;
7864 ssymhead->count++;
7865 }
7866 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
7867 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7868 == total_size));
7869
7870 free (indbuf);
7871 return ssymbuf;
7872 }
7873
7874 /* Check if 2 sections define the same set of local and global
7875 symbols. */
7876
7877 static bfd_boolean
7878 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7879 struct bfd_link_info *info)
7880 {
7881 bfd *bfd1, *bfd2;
7882 const struct elf_backend_data *bed1, *bed2;
7883 Elf_Internal_Shdr *hdr1, *hdr2;
7884 size_t symcount1, symcount2;
7885 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7886 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7887 Elf_Internal_Sym *isym, *isymend;
7888 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7889 size_t count1, count2, i;
7890 unsigned int shndx1, shndx2;
7891 bfd_boolean result;
7892
7893 bfd1 = sec1->owner;
7894 bfd2 = sec2->owner;
7895
7896 /* Both sections have to be in ELF. */
7897 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7898 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7899 return FALSE;
7900
7901 if (elf_section_type (sec1) != elf_section_type (sec2))
7902 return FALSE;
7903
7904 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7905 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7906 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7907 return FALSE;
7908
7909 bed1 = get_elf_backend_data (bfd1);
7910 bed2 = get_elf_backend_data (bfd2);
7911 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7912 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7913 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7914 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7915
7916 if (symcount1 == 0 || symcount2 == 0)
7917 return FALSE;
7918
7919 result = FALSE;
7920 isymbuf1 = NULL;
7921 isymbuf2 = NULL;
7922 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7923 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7924
7925 if (ssymbuf1 == NULL)
7926 {
7927 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7928 NULL, NULL, NULL);
7929 if (isymbuf1 == NULL)
7930 goto done;
7931
7932 if (!info->reduce_memory_overheads)
7933 elf_tdata (bfd1)->symbuf = ssymbuf1
7934 = elf_create_symbuf (symcount1, isymbuf1);
7935 }
7936
7937 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7938 {
7939 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7940 NULL, NULL, NULL);
7941 if (isymbuf2 == NULL)
7942 goto done;
7943
7944 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7945 elf_tdata (bfd2)->symbuf = ssymbuf2
7946 = elf_create_symbuf (symcount2, isymbuf2);
7947 }
7948
7949 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7950 {
7951 /* Optimized faster version. */
7952 size_t lo, hi, mid;
7953 struct elf_symbol *symp;
7954 struct elf_symbuf_symbol *ssym, *ssymend;
7955
7956 lo = 0;
7957 hi = ssymbuf1->count;
7958 ssymbuf1++;
7959 count1 = 0;
7960 while (lo < hi)
7961 {
7962 mid = (lo + hi) / 2;
7963 if (shndx1 < ssymbuf1[mid].st_shndx)
7964 hi = mid;
7965 else if (shndx1 > ssymbuf1[mid].st_shndx)
7966 lo = mid + 1;
7967 else
7968 {
7969 count1 = ssymbuf1[mid].count;
7970 ssymbuf1 += mid;
7971 break;
7972 }
7973 }
7974
7975 lo = 0;
7976 hi = ssymbuf2->count;
7977 ssymbuf2++;
7978 count2 = 0;
7979 while (lo < hi)
7980 {
7981 mid = (lo + hi) / 2;
7982 if (shndx2 < ssymbuf2[mid].st_shndx)
7983 hi = mid;
7984 else if (shndx2 > ssymbuf2[mid].st_shndx)
7985 lo = mid + 1;
7986 else
7987 {
7988 count2 = ssymbuf2[mid].count;
7989 ssymbuf2 += mid;
7990 break;
7991 }
7992 }
7993
7994 if (count1 == 0 || count2 == 0 || count1 != count2)
7995 goto done;
7996
7997 symtable1
7998 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7999 symtable2
8000 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8001 if (symtable1 == NULL || symtable2 == NULL)
8002 goto done;
8003
8004 symp = symtable1;
8005 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
8006 ssym < ssymend; ssym++, symp++)
8007 {
8008 symp->u.ssym = ssym;
8009 symp->name = bfd_elf_string_from_elf_section (bfd1,
8010 hdr1->sh_link,
8011 ssym->st_name);
8012 }
8013
8014 symp = symtable2;
8015 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8016 ssym < ssymend; ssym++, symp++)
8017 {
8018 symp->u.ssym = ssym;
8019 symp->name = bfd_elf_string_from_elf_section (bfd2,
8020 hdr2->sh_link,
8021 ssym->st_name);
8022 }
8023
8024 /* Sort symbol by name. */
8025 qsort (symtable1, count1, sizeof (struct elf_symbol),
8026 elf_sym_name_compare);
8027 qsort (symtable2, count1, sizeof (struct elf_symbol),
8028 elf_sym_name_compare);
8029
8030 for (i = 0; i < count1; i++)
8031 /* Two symbols must have the same binding, type and name. */
8032 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8033 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8034 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8035 goto done;
8036
8037 result = TRUE;
8038 goto done;
8039 }
8040
8041 symtable1 = (struct elf_symbol *)
8042 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8043 symtable2 = (struct elf_symbol *)
8044 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8045 if (symtable1 == NULL || symtable2 == NULL)
8046 goto done;
8047
8048 /* Count definitions in the section. */
8049 count1 = 0;
8050 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8051 if (isym->st_shndx == shndx1)
8052 symtable1[count1++].u.isym = isym;
8053
8054 count2 = 0;
8055 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8056 if (isym->st_shndx == shndx2)
8057 symtable2[count2++].u.isym = isym;
8058
8059 if (count1 == 0 || count2 == 0 || count1 != count2)
8060 goto done;
8061
8062 for (i = 0; i < count1; i++)
8063 symtable1[i].name
8064 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8065 symtable1[i].u.isym->st_name);
8066
8067 for (i = 0; i < count2; i++)
8068 symtable2[i].name
8069 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8070 symtable2[i].u.isym->st_name);
8071
8072 /* Sort symbol by name. */
8073 qsort (symtable1, count1, sizeof (struct elf_symbol),
8074 elf_sym_name_compare);
8075 qsort (symtable2, count1, sizeof (struct elf_symbol),
8076 elf_sym_name_compare);
8077
8078 for (i = 0; i < count1; i++)
8079 /* Two symbols must have the same binding, type and name. */
8080 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8081 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8082 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8083 goto done;
8084
8085 result = TRUE;
8086
8087 done:
8088 if (symtable1)
8089 free (symtable1);
8090 if (symtable2)
8091 free (symtable2);
8092 if (isymbuf1)
8093 free (isymbuf1);
8094 if (isymbuf2)
8095 free (isymbuf2);
8096
8097 return result;
8098 }
8099
8100 /* Return TRUE if 2 section types are compatible. */
8101
8102 bfd_boolean
8103 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8104 bfd *bbfd, const asection *bsec)
8105 {
8106 if (asec == NULL
8107 || bsec == NULL
8108 || abfd->xvec->flavour != bfd_target_elf_flavour
8109 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8110 return TRUE;
8111
8112 return elf_section_type (asec) == elf_section_type (bsec);
8113 }
8114 \f
8115 /* Final phase of ELF linker. */
8116
8117 /* A structure we use to avoid passing large numbers of arguments. */
8118
8119 struct elf_final_link_info
8120 {
8121 /* General link information. */
8122 struct bfd_link_info *info;
8123 /* Output BFD. */
8124 bfd *output_bfd;
8125 /* Symbol string table. */
8126 struct elf_strtab_hash *symstrtab;
8127 /* .hash section. */
8128 asection *hash_sec;
8129 /* symbol version section (.gnu.version). */
8130 asection *symver_sec;
8131 /* Buffer large enough to hold contents of any section. */
8132 bfd_byte *contents;
8133 /* Buffer large enough to hold external relocs of any section. */
8134 void *external_relocs;
8135 /* Buffer large enough to hold internal relocs of any section. */
8136 Elf_Internal_Rela *internal_relocs;
8137 /* Buffer large enough to hold external local symbols of any input
8138 BFD. */
8139 bfd_byte *external_syms;
8140 /* And a buffer for symbol section indices. */
8141 Elf_External_Sym_Shndx *locsym_shndx;
8142 /* Buffer large enough to hold internal local symbols of any input
8143 BFD. */
8144 Elf_Internal_Sym *internal_syms;
8145 /* Array large enough to hold a symbol index for each local symbol
8146 of any input BFD. */
8147 long *indices;
8148 /* Array large enough to hold a section pointer for each local
8149 symbol of any input BFD. */
8150 asection **sections;
8151 /* Buffer for SHT_SYMTAB_SHNDX section. */
8152 Elf_External_Sym_Shndx *symshndxbuf;
8153 /* Number of STT_FILE syms seen. */
8154 size_t filesym_count;
8155 };
8156
8157 /* This struct is used to pass information to elf_link_output_extsym. */
8158
8159 struct elf_outext_info
8160 {
8161 bfd_boolean failed;
8162 bfd_boolean localsyms;
8163 bfd_boolean file_sym_done;
8164 struct elf_final_link_info *flinfo;
8165 };
8166
8167
8168 /* Support for evaluating a complex relocation.
8169
8170 Complex relocations are generalized, self-describing relocations. The
8171 implementation of them consists of two parts: complex symbols, and the
8172 relocations themselves.
8173
8174 The relocations are use a reserved elf-wide relocation type code (R_RELC
8175 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8176 information (start bit, end bit, word width, etc) into the addend. This
8177 information is extracted from CGEN-generated operand tables within gas.
8178
8179 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8180 internal) representing prefix-notation expressions, including but not
8181 limited to those sorts of expressions normally encoded as addends in the
8182 addend field. The symbol mangling format is:
8183
8184 <node> := <literal>
8185 | <unary-operator> ':' <node>
8186 | <binary-operator> ':' <node> ':' <node>
8187 ;
8188
8189 <literal> := 's' <digits=N> ':' <N character symbol name>
8190 | 'S' <digits=N> ':' <N character section name>
8191 | '#' <hexdigits>
8192 ;
8193
8194 <binary-operator> := as in C
8195 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8196
8197 static void
8198 set_symbol_value (bfd *bfd_with_globals,
8199 Elf_Internal_Sym *isymbuf,
8200 size_t locsymcount,
8201 size_t symidx,
8202 bfd_vma val)
8203 {
8204 struct elf_link_hash_entry **sym_hashes;
8205 struct elf_link_hash_entry *h;
8206 size_t extsymoff = locsymcount;
8207
8208 if (symidx < locsymcount)
8209 {
8210 Elf_Internal_Sym *sym;
8211
8212 sym = isymbuf + symidx;
8213 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8214 {
8215 /* It is a local symbol: move it to the
8216 "absolute" section and give it a value. */
8217 sym->st_shndx = SHN_ABS;
8218 sym->st_value = val;
8219 return;
8220 }
8221 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8222 extsymoff = 0;
8223 }
8224
8225 /* It is a global symbol: set its link type
8226 to "defined" and give it a value. */
8227
8228 sym_hashes = elf_sym_hashes (bfd_with_globals);
8229 h = sym_hashes [symidx - extsymoff];
8230 while (h->root.type == bfd_link_hash_indirect
8231 || h->root.type == bfd_link_hash_warning)
8232 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8233 h->root.type = bfd_link_hash_defined;
8234 h->root.u.def.value = val;
8235 h->root.u.def.section = bfd_abs_section_ptr;
8236 }
8237
8238 static bfd_boolean
8239 resolve_symbol (const char *name,
8240 bfd *input_bfd,
8241 struct elf_final_link_info *flinfo,
8242 bfd_vma *result,
8243 Elf_Internal_Sym *isymbuf,
8244 size_t locsymcount)
8245 {
8246 Elf_Internal_Sym *sym;
8247 struct bfd_link_hash_entry *global_entry;
8248 const char *candidate = NULL;
8249 Elf_Internal_Shdr *symtab_hdr;
8250 size_t i;
8251
8252 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8253
8254 for (i = 0; i < locsymcount; ++ i)
8255 {
8256 sym = isymbuf + i;
8257
8258 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8259 continue;
8260
8261 candidate = bfd_elf_string_from_elf_section (input_bfd,
8262 symtab_hdr->sh_link,
8263 sym->st_name);
8264 #ifdef DEBUG
8265 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8266 name, candidate, (unsigned long) sym->st_value);
8267 #endif
8268 if (candidate && strcmp (candidate, name) == 0)
8269 {
8270 asection *sec = flinfo->sections [i];
8271
8272 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8273 *result += sec->output_offset + sec->output_section->vma;
8274 #ifdef DEBUG
8275 printf ("Found symbol with value %8.8lx\n",
8276 (unsigned long) *result);
8277 #endif
8278 return TRUE;
8279 }
8280 }
8281
8282 /* Hmm, haven't found it yet. perhaps it is a global. */
8283 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8284 FALSE, FALSE, TRUE);
8285 if (!global_entry)
8286 return FALSE;
8287
8288 if (global_entry->type == bfd_link_hash_defined
8289 || global_entry->type == bfd_link_hash_defweak)
8290 {
8291 *result = (global_entry->u.def.value
8292 + global_entry->u.def.section->output_section->vma
8293 + global_entry->u.def.section->output_offset);
8294 #ifdef DEBUG
8295 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8296 global_entry->root.string, (unsigned long) *result);
8297 #endif
8298 return TRUE;
8299 }
8300
8301 return FALSE;
8302 }
8303
8304 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8305 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8306 names like "foo.end" which is the end address of section "foo". */
8307
8308 static bfd_boolean
8309 resolve_section (const char *name,
8310 asection *sections,
8311 bfd_vma *result,
8312 bfd * abfd)
8313 {
8314 asection *curr;
8315 unsigned int len;
8316
8317 for (curr = sections; curr; curr = curr->next)
8318 if (strcmp (curr->name, name) == 0)
8319 {
8320 *result = curr->vma;
8321 return TRUE;
8322 }
8323
8324 /* Hmm. still haven't found it. try pseudo-section names. */
8325 /* FIXME: This could be coded more efficiently... */
8326 for (curr = sections; curr; curr = curr->next)
8327 {
8328 len = strlen (curr->name);
8329 if (len > strlen (name))
8330 continue;
8331
8332 if (strncmp (curr->name, name, len) == 0)
8333 {
8334 if (strncmp (".end", name + len, 4) == 0)
8335 {
8336 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
8337 return TRUE;
8338 }
8339
8340 /* Insert more pseudo-section names here, if you like. */
8341 }
8342 }
8343
8344 return FALSE;
8345 }
8346
8347 static void
8348 undefined_reference (const char *reftype, const char *name)
8349 {
8350 /* xgettext:c-format */
8351 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8352 reftype, name);
8353 }
8354
8355 static bfd_boolean
8356 eval_symbol (bfd_vma *result,
8357 const char **symp,
8358 bfd *input_bfd,
8359 struct elf_final_link_info *flinfo,
8360 bfd_vma dot,
8361 Elf_Internal_Sym *isymbuf,
8362 size_t locsymcount,
8363 int signed_p)
8364 {
8365 size_t len;
8366 size_t symlen;
8367 bfd_vma a;
8368 bfd_vma b;
8369 char symbuf[4096];
8370 const char *sym = *symp;
8371 const char *symend;
8372 bfd_boolean symbol_is_section = FALSE;
8373
8374 len = strlen (sym);
8375 symend = sym + len;
8376
8377 if (len < 1 || len > sizeof (symbuf))
8378 {
8379 bfd_set_error (bfd_error_invalid_operation);
8380 return FALSE;
8381 }
8382
8383 switch (* sym)
8384 {
8385 case '.':
8386 *result = dot;
8387 *symp = sym + 1;
8388 return TRUE;
8389
8390 case '#':
8391 ++sym;
8392 *result = strtoul (sym, (char **) symp, 16);
8393 return TRUE;
8394
8395 case 'S':
8396 symbol_is_section = TRUE;
8397 /* Fall through. */
8398 case 's':
8399 ++sym;
8400 symlen = strtol (sym, (char **) symp, 10);
8401 sym = *symp + 1; /* Skip the trailing ':'. */
8402
8403 if (symend < sym || symlen + 1 > sizeof (symbuf))
8404 {
8405 bfd_set_error (bfd_error_invalid_operation);
8406 return FALSE;
8407 }
8408
8409 memcpy (symbuf, sym, symlen);
8410 symbuf[symlen] = '\0';
8411 *symp = sym + symlen;
8412
8413 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8414 the symbol as a section, or vice-versa. so we're pretty liberal in our
8415 interpretation here; section means "try section first", not "must be a
8416 section", and likewise with symbol. */
8417
8418 if (symbol_is_section)
8419 {
8420 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8421 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8422 isymbuf, locsymcount))
8423 {
8424 undefined_reference ("section", symbuf);
8425 return FALSE;
8426 }
8427 }
8428 else
8429 {
8430 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8431 isymbuf, locsymcount)
8432 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8433 result, input_bfd))
8434 {
8435 undefined_reference ("symbol", symbuf);
8436 return FALSE;
8437 }
8438 }
8439
8440 return TRUE;
8441
8442 /* All that remains are operators. */
8443
8444 #define UNARY_OP(op) \
8445 if (strncmp (sym, #op, strlen (#op)) == 0) \
8446 { \
8447 sym += strlen (#op); \
8448 if (*sym == ':') \
8449 ++sym; \
8450 *symp = sym; \
8451 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8452 isymbuf, locsymcount, signed_p)) \
8453 return FALSE; \
8454 if (signed_p) \
8455 *result = op ((bfd_signed_vma) a); \
8456 else \
8457 *result = op a; \
8458 return TRUE; \
8459 }
8460
8461 #define BINARY_OP(op) \
8462 if (strncmp (sym, #op, strlen (#op)) == 0) \
8463 { \
8464 sym += strlen (#op); \
8465 if (*sym == ':') \
8466 ++sym; \
8467 *symp = sym; \
8468 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8469 isymbuf, locsymcount, signed_p)) \
8470 return FALSE; \
8471 ++*symp; \
8472 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8473 isymbuf, locsymcount, signed_p)) \
8474 return FALSE; \
8475 if (signed_p) \
8476 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8477 else \
8478 *result = a op b; \
8479 return TRUE; \
8480 }
8481
8482 default:
8483 UNARY_OP (0-);
8484 BINARY_OP (<<);
8485 BINARY_OP (>>);
8486 BINARY_OP (==);
8487 BINARY_OP (!=);
8488 BINARY_OP (<=);
8489 BINARY_OP (>=);
8490 BINARY_OP (&&);
8491 BINARY_OP (||);
8492 UNARY_OP (~);
8493 UNARY_OP (!);
8494 BINARY_OP (*);
8495 BINARY_OP (/);
8496 BINARY_OP (%);
8497 BINARY_OP (^);
8498 BINARY_OP (|);
8499 BINARY_OP (&);
8500 BINARY_OP (+);
8501 BINARY_OP (-);
8502 BINARY_OP (<);
8503 BINARY_OP (>);
8504 #undef UNARY_OP
8505 #undef BINARY_OP
8506 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8507 bfd_set_error (bfd_error_invalid_operation);
8508 return FALSE;
8509 }
8510 }
8511
8512 static void
8513 put_value (bfd_vma size,
8514 unsigned long chunksz,
8515 bfd *input_bfd,
8516 bfd_vma x,
8517 bfd_byte *location)
8518 {
8519 location += (size - chunksz);
8520
8521 for (; size; size -= chunksz, location -= chunksz)
8522 {
8523 switch (chunksz)
8524 {
8525 case 1:
8526 bfd_put_8 (input_bfd, x, location);
8527 x >>= 8;
8528 break;
8529 case 2:
8530 bfd_put_16 (input_bfd, x, location);
8531 x >>= 16;
8532 break;
8533 case 4:
8534 bfd_put_32 (input_bfd, x, location);
8535 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8536 x >>= 16;
8537 x >>= 16;
8538 break;
8539 #ifdef BFD64
8540 case 8:
8541 bfd_put_64 (input_bfd, x, location);
8542 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8543 x >>= 32;
8544 x >>= 32;
8545 break;
8546 #endif
8547 default:
8548 abort ();
8549 break;
8550 }
8551 }
8552 }
8553
8554 static bfd_vma
8555 get_value (bfd_vma size,
8556 unsigned long chunksz,
8557 bfd *input_bfd,
8558 bfd_byte *location)
8559 {
8560 int shift;
8561 bfd_vma x = 0;
8562
8563 /* Sanity checks. */
8564 BFD_ASSERT (chunksz <= sizeof (x)
8565 && size >= chunksz
8566 && chunksz != 0
8567 && (size % chunksz) == 0
8568 && input_bfd != NULL
8569 && location != NULL);
8570
8571 if (chunksz == sizeof (x))
8572 {
8573 BFD_ASSERT (size == chunksz);
8574
8575 /* Make sure that we do not perform an undefined shift operation.
8576 We know that size == chunksz so there will only be one iteration
8577 of the loop below. */
8578 shift = 0;
8579 }
8580 else
8581 shift = 8 * chunksz;
8582
8583 for (; size; size -= chunksz, location += chunksz)
8584 {
8585 switch (chunksz)
8586 {
8587 case 1:
8588 x = (x << shift) | bfd_get_8 (input_bfd, location);
8589 break;
8590 case 2:
8591 x = (x << shift) | bfd_get_16 (input_bfd, location);
8592 break;
8593 case 4:
8594 x = (x << shift) | bfd_get_32 (input_bfd, location);
8595 break;
8596 #ifdef BFD64
8597 case 8:
8598 x = (x << shift) | bfd_get_64 (input_bfd, location);
8599 break;
8600 #endif
8601 default:
8602 abort ();
8603 }
8604 }
8605 return x;
8606 }
8607
8608 static void
8609 decode_complex_addend (unsigned long *start, /* in bits */
8610 unsigned long *oplen, /* in bits */
8611 unsigned long *len, /* in bits */
8612 unsigned long *wordsz, /* in bytes */
8613 unsigned long *chunksz, /* in bytes */
8614 unsigned long *lsb0_p,
8615 unsigned long *signed_p,
8616 unsigned long *trunc_p,
8617 unsigned long encoded)
8618 {
8619 * start = encoded & 0x3F;
8620 * len = (encoded >> 6) & 0x3F;
8621 * oplen = (encoded >> 12) & 0x3F;
8622 * wordsz = (encoded >> 18) & 0xF;
8623 * chunksz = (encoded >> 22) & 0xF;
8624 * lsb0_p = (encoded >> 27) & 1;
8625 * signed_p = (encoded >> 28) & 1;
8626 * trunc_p = (encoded >> 29) & 1;
8627 }
8628
8629 bfd_reloc_status_type
8630 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8631 asection *input_section ATTRIBUTE_UNUSED,
8632 bfd_byte *contents,
8633 Elf_Internal_Rela *rel,
8634 bfd_vma relocation)
8635 {
8636 bfd_vma shift, x, mask;
8637 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8638 bfd_reloc_status_type r;
8639
8640 /* Perform this reloc, since it is complex.
8641 (this is not to say that it necessarily refers to a complex
8642 symbol; merely that it is a self-describing CGEN based reloc.
8643 i.e. the addend has the complete reloc information (bit start, end,
8644 word size, etc) encoded within it.). */
8645
8646 decode_complex_addend (&start, &oplen, &len, &wordsz,
8647 &chunksz, &lsb0_p, &signed_p,
8648 &trunc_p, rel->r_addend);
8649
8650 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8651
8652 if (lsb0_p)
8653 shift = (start + 1) - len;
8654 else
8655 shift = (8 * wordsz) - (start + len);
8656
8657 x = get_value (wordsz, chunksz, input_bfd,
8658 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8659
8660 #ifdef DEBUG
8661 printf ("Doing complex reloc: "
8662 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8663 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8664 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8665 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8666 oplen, (unsigned long) x, (unsigned long) mask,
8667 (unsigned long) relocation);
8668 #endif
8669
8670 r = bfd_reloc_ok;
8671 if (! trunc_p)
8672 /* Now do an overflow check. */
8673 r = bfd_check_overflow ((signed_p
8674 ? complain_overflow_signed
8675 : complain_overflow_unsigned),
8676 len, 0, (8 * wordsz),
8677 relocation);
8678
8679 /* Do the deed. */
8680 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8681
8682 #ifdef DEBUG
8683 printf (" relocation: %8.8lx\n"
8684 " shifted mask: %8.8lx\n"
8685 " shifted/masked reloc: %8.8lx\n"
8686 " result: %8.8lx\n",
8687 (unsigned long) relocation, (unsigned long) (mask << shift),
8688 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8689 #endif
8690 put_value (wordsz, chunksz, input_bfd, x,
8691 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8692 return r;
8693 }
8694
8695 /* Functions to read r_offset from external (target order) reloc
8696 entry. Faster than bfd_getl32 et al, because we let the compiler
8697 know the value is aligned. */
8698
8699 static bfd_vma
8700 ext32l_r_offset (const void *p)
8701 {
8702 union aligned32
8703 {
8704 uint32_t v;
8705 unsigned char c[4];
8706 };
8707 const union aligned32 *a
8708 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8709
8710 uint32_t aval = ( (uint32_t) a->c[0]
8711 | (uint32_t) a->c[1] << 8
8712 | (uint32_t) a->c[2] << 16
8713 | (uint32_t) a->c[3] << 24);
8714 return aval;
8715 }
8716
8717 static bfd_vma
8718 ext32b_r_offset (const void *p)
8719 {
8720 union aligned32
8721 {
8722 uint32_t v;
8723 unsigned char c[4];
8724 };
8725 const union aligned32 *a
8726 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8727
8728 uint32_t aval = ( (uint32_t) a->c[0] << 24
8729 | (uint32_t) a->c[1] << 16
8730 | (uint32_t) a->c[2] << 8
8731 | (uint32_t) a->c[3]);
8732 return aval;
8733 }
8734
8735 #ifdef BFD_HOST_64_BIT
8736 static bfd_vma
8737 ext64l_r_offset (const void *p)
8738 {
8739 union aligned64
8740 {
8741 uint64_t v;
8742 unsigned char c[8];
8743 };
8744 const union aligned64 *a
8745 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8746
8747 uint64_t aval = ( (uint64_t) a->c[0]
8748 | (uint64_t) a->c[1] << 8
8749 | (uint64_t) a->c[2] << 16
8750 | (uint64_t) a->c[3] << 24
8751 | (uint64_t) a->c[4] << 32
8752 | (uint64_t) a->c[5] << 40
8753 | (uint64_t) a->c[6] << 48
8754 | (uint64_t) a->c[7] << 56);
8755 return aval;
8756 }
8757
8758 static bfd_vma
8759 ext64b_r_offset (const void *p)
8760 {
8761 union aligned64
8762 {
8763 uint64_t v;
8764 unsigned char c[8];
8765 };
8766 const union aligned64 *a
8767 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8768
8769 uint64_t aval = ( (uint64_t) a->c[0] << 56
8770 | (uint64_t) a->c[1] << 48
8771 | (uint64_t) a->c[2] << 40
8772 | (uint64_t) a->c[3] << 32
8773 | (uint64_t) a->c[4] << 24
8774 | (uint64_t) a->c[5] << 16
8775 | (uint64_t) a->c[6] << 8
8776 | (uint64_t) a->c[7]);
8777 return aval;
8778 }
8779 #endif
8780
8781 /* When performing a relocatable link, the input relocations are
8782 preserved. But, if they reference global symbols, the indices
8783 referenced must be updated. Update all the relocations found in
8784 RELDATA. */
8785
8786 static bfd_boolean
8787 elf_link_adjust_relocs (bfd *abfd,
8788 asection *sec,
8789 struct bfd_elf_section_reloc_data *reldata,
8790 bfd_boolean sort,
8791 struct bfd_link_info *info)
8792 {
8793 unsigned int i;
8794 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8795 bfd_byte *erela;
8796 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8797 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8798 bfd_vma r_type_mask;
8799 int r_sym_shift;
8800 unsigned int count = reldata->count;
8801 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8802
8803 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8804 {
8805 swap_in = bed->s->swap_reloc_in;
8806 swap_out = bed->s->swap_reloc_out;
8807 }
8808 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8809 {
8810 swap_in = bed->s->swap_reloca_in;
8811 swap_out = bed->s->swap_reloca_out;
8812 }
8813 else
8814 abort ();
8815
8816 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8817 abort ();
8818
8819 if (bed->s->arch_size == 32)
8820 {
8821 r_type_mask = 0xff;
8822 r_sym_shift = 8;
8823 }
8824 else
8825 {
8826 r_type_mask = 0xffffffff;
8827 r_sym_shift = 32;
8828 }
8829
8830 erela = reldata->hdr->contents;
8831 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8832 {
8833 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8834 unsigned int j;
8835
8836 if (*rel_hash == NULL)
8837 continue;
8838
8839 if ((*rel_hash)->indx == -2
8840 && info->gc_sections
8841 && ! info->gc_keep_exported)
8842 {
8843 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
8844 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
8845 abfd, sec,
8846 (*rel_hash)->root.root.string);
8847 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
8848 abfd, sec);
8849 bfd_set_error (bfd_error_invalid_operation);
8850 return FALSE;
8851 }
8852 BFD_ASSERT ((*rel_hash)->indx >= 0);
8853
8854 (*swap_in) (abfd, erela, irela);
8855 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8856 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8857 | (irela[j].r_info & r_type_mask));
8858 (*swap_out) (abfd, irela, erela);
8859 }
8860
8861 if (bed->elf_backend_update_relocs)
8862 (*bed->elf_backend_update_relocs) (sec, reldata);
8863
8864 if (sort && count != 0)
8865 {
8866 bfd_vma (*ext_r_off) (const void *);
8867 bfd_vma r_off;
8868 size_t elt_size;
8869 bfd_byte *base, *end, *p, *loc;
8870 bfd_byte *buf = NULL;
8871
8872 if (bed->s->arch_size == 32)
8873 {
8874 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8875 ext_r_off = ext32l_r_offset;
8876 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8877 ext_r_off = ext32b_r_offset;
8878 else
8879 abort ();
8880 }
8881 else
8882 {
8883 #ifdef BFD_HOST_64_BIT
8884 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8885 ext_r_off = ext64l_r_offset;
8886 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8887 ext_r_off = ext64b_r_offset;
8888 else
8889 #endif
8890 abort ();
8891 }
8892
8893 /* Must use a stable sort here. A modified insertion sort,
8894 since the relocs are mostly sorted already. */
8895 elt_size = reldata->hdr->sh_entsize;
8896 base = reldata->hdr->contents;
8897 end = base + count * elt_size;
8898 if (elt_size > sizeof (Elf64_External_Rela))
8899 abort ();
8900
8901 /* Ensure the first element is lowest. This acts as a sentinel,
8902 speeding the main loop below. */
8903 r_off = (*ext_r_off) (base);
8904 for (p = loc = base; (p += elt_size) < end; )
8905 {
8906 bfd_vma r_off2 = (*ext_r_off) (p);
8907 if (r_off > r_off2)
8908 {
8909 r_off = r_off2;
8910 loc = p;
8911 }
8912 }
8913 if (loc != base)
8914 {
8915 /* Don't just swap *base and *loc as that changes the order
8916 of the original base[0] and base[1] if they happen to
8917 have the same r_offset. */
8918 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8919 memcpy (onebuf, loc, elt_size);
8920 memmove (base + elt_size, base, loc - base);
8921 memcpy (base, onebuf, elt_size);
8922 }
8923
8924 for (p = base + elt_size; (p += elt_size) < end; )
8925 {
8926 /* base to p is sorted, *p is next to insert. */
8927 r_off = (*ext_r_off) (p);
8928 /* Search the sorted region for location to insert. */
8929 loc = p - elt_size;
8930 while (r_off < (*ext_r_off) (loc))
8931 loc -= elt_size;
8932 loc += elt_size;
8933 if (loc != p)
8934 {
8935 /* Chances are there is a run of relocs to insert here,
8936 from one of more input files. Files are not always
8937 linked in order due to the way elf_link_input_bfd is
8938 called. See pr17666. */
8939 size_t sortlen = p - loc;
8940 bfd_vma r_off2 = (*ext_r_off) (loc);
8941 size_t runlen = elt_size;
8942 size_t buf_size = 96 * 1024;
8943 while (p + runlen < end
8944 && (sortlen <= buf_size
8945 || runlen + elt_size <= buf_size)
8946 && r_off2 > (*ext_r_off) (p + runlen))
8947 runlen += elt_size;
8948 if (buf == NULL)
8949 {
8950 buf = bfd_malloc (buf_size);
8951 if (buf == NULL)
8952 return FALSE;
8953 }
8954 if (runlen < sortlen)
8955 {
8956 memcpy (buf, p, runlen);
8957 memmove (loc + runlen, loc, sortlen);
8958 memcpy (loc, buf, runlen);
8959 }
8960 else
8961 {
8962 memcpy (buf, loc, sortlen);
8963 memmove (loc, p, runlen);
8964 memcpy (loc + runlen, buf, sortlen);
8965 }
8966 p += runlen - elt_size;
8967 }
8968 }
8969 /* Hashes are no longer valid. */
8970 free (reldata->hashes);
8971 reldata->hashes = NULL;
8972 free (buf);
8973 }
8974 return TRUE;
8975 }
8976
8977 struct elf_link_sort_rela
8978 {
8979 union {
8980 bfd_vma offset;
8981 bfd_vma sym_mask;
8982 } u;
8983 enum elf_reloc_type_class type;
8984 /* We use this as an array of size int_rels_per_ext_rel. */
8985 Elf_Internal_Rela rela[1];
8986 };
8987
8988 static int
8989 elf_link_sort_cmp1 (const void *A, const void *B)
8990 {
8991 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8992 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8993 int relativea, relativeb;
8994
8995 relativea = a->type == reloc_class_relative;
8996 relativeb = b->type == reloc_class_relative;
8997
8998 if (relativea < relativeb)
8999 return 1;
9000 if (relativea > relativeb)
9001 return -1;
9002 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9003 return -1;
9004 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9005 return 1;
9006 if (a->rela->r_offset < b->rela->r_offset)
9007 return -1;
9008 if (a->rela->r_offset > b->rela->r_offset)
9009 return 1;
9010 return 0;
9011 }
9012
9013 static int
9014 elf_link_sort_cmp2 (const void *A, const void *B)
9015 {
9016 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9017 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9018
9019 if (a->type < b->type)
9020 return -1;
9021 if (a->type > b->type)
9022 return 1;
9023 if (a->u.offset < b->u.offset)
9024 return -1;
9025 if (a->u.offset > b->u.offset)
9026 return 1;
9027 if (a->rela->r_offset < b->rela->r_offset)
9028 return -1;
9029 if (a->rela->r_offset > b->rela->r_offset)
9030 return 1;
9031 return 0;
9032 }
9033
9034 static size_t
9035 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9036 {
9037 asection *dynamic_relocs;
9038 asection *rela_dyn;
9039 asection *rel_dyn;
9040 bfd_size_type count, size;
9041 size_t i, ret, sort_elt, ext_size;
9042 bfd_byte *sort, *s_non_relative, *p;
9043 struct elf_link_sort_rela *sq;
9044 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9045 int i2e = bed->s->int_rels_per_ext_rel;
9046 unsigned int opb = bfd_octets_per_byte (abfd);
9047 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9048 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9049 struct bfd_link_order *lo;
9050 bfd_vma r_sym_mask;
9051 bfd_boolean use_rela;
9052
9053 /* Find a dynamic reloc section. */
9054 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9055 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9056 if (rela_dyn != NULL && rela_dyn->size > 0
9057 && rel_dyn != NULL && rel_dyn->size > 0)
9058 {
9059 bfd_boolean use_rela_initialised = FALSE;
9060
9061 /* This is just here to stop gcc from complaining.
9062 Its initialization checking code is not perfect. */
9063 use_rela = TRUE;
9064
9065 /* Both sections are present. Examine the sizes
9066 of the indirect sections to help us choose. */
9067 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9068 if (lo->type == bfd_indirect_link_order)
9069 {
9070 asection *o = lo->u.indirect.section;
9071
9072 if ((o->size % bed->s->sizeof_rela) == 0)
9073 {
9074 if ((o->size % bed->s->sizeof_rel) == 0)
9075 /* Section size is divisible by both rel and rela sizes.
9076 It is of no help to us. */
9077 ;
9078 else
9079 {
9080 /* Section size is only divisible by rela. */
9081 if (use_rela_initialised && !use_rela)
9082 {
9083 _bfd_error_handler (_("%pB: unable to sort relocs - "
9084 "they are in more than one size"),
9085 abfd);
9086 bfd_set_error (bfd_error_invalid_operation);
9087 return 0;
9088 }
9089 else
9090 {
9091 use_rela = TRUE;
9092 use_rela_initialised = TRUE;
9093 }
9094 }
9095 }
9096 else if ((o->size % bed->s->sizeof_rel) == 0)
9097 {
9098 /* Section size is only divisible by rel. */
9099 if (use_rela_initialised && use_rela)
9100 {
9101 _bfd_error_handler (_("%pB: unable to sort relocs - "
9102 "they are in more than one size"),
9103 abfd);
9104 bfd_set_error (bfd_error_invalid_operation);
9105 return 0;
9106 }
9107 else
9108 {
9109 use_rela = FALSE;
9110 use_rela_initialised = TRUE;
9111 }
9112 }
9113 else
9114 {
9115 /* The section size is not divisible by either -
9116 something is wrong. */
9117 _bfd_error_handler (_("%pB: unable to sort relocs - "
9118 "they are of an unknown size"), abfd);
9119 bfd_set_error (bfd_error_invalid_operation);
9120 return 0;
9121 }
9122 }
9123
9124 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9125 if (lo->type == bfd_indirect_link_order)
9126 {
9127 asection *o = lo->u.indirect.section;
9128
9129 if ((o->size % bed->s->sizeof_rela) == 0)
9130 {
9131 if ((o->size % bed->s->sizeof_rel) == 0)
9132 /* Section size is divisible by both rel and rela sizes.
9133 It is of no help to us. */
9134 ;
9135 else
9136 {
9137 /* Section size is only divisible by rela. */
9138 if (use_rela_initialised && !use_rela)
9139 {
9140 _bfd_error_handler (_("%pB: unable to sort relocs - "
9141 "they are in more than one size"),
9142 abfd);
9143 bfd_set_error (bfd_error_invalid_operation);
9144 return 0;
9145 }
9146 else
9147 {
9148 use_rela = TRUE;
9149 use_rela_initialised = TRUE;
9150 }
9151 }
9152 }
9153 else if ((o->size % bed->s->sizeof_rel) == 0)
9154 {
9155 /* Section size is only divisible by rel. */
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 = FALSE;
9167 use_rela_initialised = TRUE;
9168 }
9169 }
9170 else
9171 {
9172 /* The section size is not divisible by either -
9173 something is wrong. */
9174 _bfd_error_handler (_("%pB: unable to sort relocs - "
9175 "they are of an unknown size"), abfd);
9176 bfd_set_error (bfd_error_invalid_operation);
9177 return 0;
9178 }
9179 }
9180
9181 if (! use_rela_initialised)
9182 /* Make a guess. */
9183 use_rela = TRUE;
9184 }
9185 else if (rela_dyn != NULL && rela_dyn->size > 0)
9186 use_rela = TRUE;
9187 else if (rel_dyn != NULL && rel_dyn->size > 0)
9188 use_rela = FALSE;
9189 else
9190 return 0;
9191
9192 if (use_rela)
9193 {
9194 dynamic_relocs = rela_dyn;
9195 ext_size = bed->s->sizeof_rela;
9196 swap_in = bed->s->swap_reloca_in;
9197 swap_out = bed->s->swap_reloca_out;
9198 }
9199 else
9200 {
9201 dynamic_relocs = rel_dyn;
9202 ext_size = bed->s->sizeof_rel;
9203 swap_in = bed->s->swap_reloc_in;
9204 swap_out = bed->s->swap_reloc_out;
9205 }
9206
9207 size = 0;
9208 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9209 if (lo->type == bfd_indirect_link_order)
9210 size += lo->u.indirect.section->size;
9211
9212 if (size != dynamic_relocs->size)
9213 return 0;
9214
9215 sort_elt = (sizeof (struct elf_link_sort_rela)
9216 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9217
9218 count = dynamic_relocs->size / ext_size;
9219 if (count == 0)
9220 return 0;
9221 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9222
9223 if (sort == NULL)
9224 {
9225 (*info->callbacks->warning)
9226 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
9227 return 0;
9228 }
9229
9230 if (bed->s->arch_size == 32)
9231 r_sym_mask = ~(bfd_vma) 0xff;
9232 else
9233 r_sym_mask = ~(bfd_vma) 0xffffffff;
9234
9235 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9236 if (lo->type == bfd_indirect_link_order)
9237 {
9238 bfd_byte *erel, *erelend;
9239 asection *o = lo->u.indirect.section;
9240
9241 if (o->contents == NULL && o->size != 0)
9242 {
9243 /* This is a reloc section that is being handled as a normal
9244 section. See bfd_section_from_shdr. We can't combine
9245 relocs in this case. */
9246 free (sort);
9247 return 0;
9248 }
9249 erel = o->contents;
9250 erelend = o->contents + o->size;
9251 p = sort + o->output_offset * opb / ext_size * sort_elt;
9252
9253 while (erel < erelend)
9254 {
9255 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9256
9257 (*swap_in) (abfd, erel, s->rela);
9258 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9259 s->u.sym_mask = r_sym_mask;
9260 p += sort_elt;
9261 erel += ext_size;
9262 }
9263 }
9264
9265 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9266
9267 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9268 {
9269 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9270 if (s->type != reloc_class_relative)
9271 break;
9272 }
9273 ret = i;
9274 s_non_relative = p;
9275
9276 sq = (struct elf_link_sort_rela *) s_non_relative;
9277 for (; i < count; i++, p += sort_elt)
9278 {
9279 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9280 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9281 sq = sp;
9282 sp->u.offset = sq->rela->r_offset;
9283 }
9284
9285 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9286
9287 struct elf_link_hash_table *htab = elf_hash_table (info);
9288 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9289 {
9290 /* We have plt relocs in .rela.dyn. */
9291 sq = (struct elf_link_sort_rela *) sort;
9292 for (i = 0; i < count; i++)
9293 if (sq[count - i - 1].type != reloc_class_plt)
9294 break;
9295 if (i != 0 && htab->srelplt->size == i * ext_size)
9296 {
9297 struct bfd_link_order **plo;
9298 /* Put srelplt link_order last. This is so the output_offset
9299 set in the next loop is correct for DT_JMPREL. */
9300 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9301 if ((*plo)->type == bfd_indirect_link_order
9302 && (*plo)->u.indirect.section == htab->srelplt)
9303 {
9304 lo = *plo;
9305 *plo = lo->next;
9306 }
9307 else
9308 plo = &(*plo)->next;
9309 *plo = lo;
9310 lo->next = NULL;
9311 dynamic_relocs->map_tail.link_order = lo;
9312 }
9313 }
9314
9315 p = sort;
9316 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9317 if (lo->type == bfd_indirect_link_order)
9318 {
9319 bfd_byte *erel, *erelend;
9320 asection *o = lo->u.indirect.section;
9321
9322 erel = o->contents;
9323 erelend = o->contents + o->size;
9324 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9325 while (erel < erelend)
9326 {
9327 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9328 (*swap_out) (abfd, s->rela, erel);
9329 p += sort_elt;
9330 erel += ext_size;
9331 }
9332 }
9333
9334 free (sort);
9335 *psec = dynamic_relocs;
9336 return ret;
9337 }
9338
9339 /* Add a symbol to the output symbol string table. */
9340
9341 static int
9342 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9343 const char *name,
9344 Elf_Internal_Sym *elfsym,
9345 asection *input_sec,
9346 struct elf_link_hash_entry *h)
9347 {
9348 int (*output_symbol_hook)
9349 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9350 struct elf_link_hash_entry *);
9351 struct elf_link_hash_table *hash_table;
9352 const struct elf_backend_data *bed;
9353 bfd_size_type strtabsize;
9354
9355 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9356
9357 bed = get_elf_backend_data (flinfo->output_bfd);
9358 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9359 if (output_symbol_hook != NULL)
9360 {
9361 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9362 if (ret != 1)
9363 return ret;
9364 }
9365
9366 if (name == NULL
9367 || *name == '\0'
9368 || (input_sec->flags & SEC_EXCLUDE))
9369 elfsym->st_name = (unsigned long) -1;
9370 else
9371 {
9372 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9373 to get the final offset for st_name. */
9374 elfsym->st_name
9375 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9376 name, FALSE);
9377 if (elfsym->st_name == (unsigned long) -1)
9378 return 0;
9379 }
9380
9381 hash_table = elf_hash_table (flinfo->info);
9382 strtabsize = hash_table->strtabsize;
9383 if (strtabsize <= hash_table->strtabcount)
9384 {
9385 strtabsize += strtabsize;
9386 hash_table->strtabsize = strtabsize;
9387 strtabsize *= sizeof (*hash_table->strtab);
9388 hash_table->strtab
9389 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9390 strtabsize);
9391 if (hash_table->strtab == NULL)
9392 return 0;
9393 }
9394 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9395 hash_table->strtab[hash_table->strtabcount].dest_index
9396 = hash_table->strtabcount;
9397 hash_table->strtab[hash_table->strtabcount].destshndx_index
9398 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9399
9400 bfd_get_symcount (flinfo->output_bfd) += 1;
9401 hash_table->strtabcount += 1;
9402
9403 return 1;
9404 }
9405
9406 /* Swap symbols out to the symbol table and flush the output symbols to
9407 the file. */
9408
9409 static bfd_boolean
9410 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9411 {
9412 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9413 bfd_size_type amt;
9414 size_t i;
9415 const struct elf_backend_data *bed;
9416 bfd_byte *symbuf;
9417 Elf_Internal_Shdr *hdr;
9418 file_ptr pos;
9419 bfd_boolean ret;
9420
9421 if (!hash_table->strtabcount)
9422 return TRUE;
9423
9424 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9425
9426 bed = get_elf_backend_data (flinfo->output_bfd);
9427
9428 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9429 symbuf = (bfd_byte *) bfd_malloc (amt);
9430 if (symbuf == NULL)
9431 return FALSE;
9432
9433 if (flinfo->symshndxbuf)
9434 {
9435 amt = sizeof (Elf_External_Sym_Shndx);
9436 amt *= bfd_get_symcount (flinfo->output_bfd);
9437 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9438 if (flinfo->symshndxbuf == NULL)
9439 {
9440 free (symbuf);
9441 return FALSE;
9442 }
9443 }
9444
9445 for (i = 0; i < hash_table->strtabcount; i++)
9446 {
9447 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9448 if (elfsym->sym.st_name == (unsigned long) -1)
9449 elfsym->sym.st_name = 0;
9450 else
9451 elfsym->sym.st_name
9452 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9453 elfsym->sym.st_name);
9454 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9455 ((bfd_byte *) symbuf
9456 + (elfsym->dest_index
9457 * bed->s->sizeof_sym)),
9458 (flinfo->symshndxbuf
9459 + elfsym->destshndx_index));
9460 }
9461
9462 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9463 pos = hdr->sh_offset + hdr->sh_size;
9464 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9465 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9466 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9467 {
9468 hdr->sh_size += amt;
9469 ret = TRUE;
9470 }
9471 else
9472 ret = FALSE;
9473
9474 free (symbuf);
9475
9476 free (hash_table->strtab);
9477 hash_table->strtab = NULL;
9478
9479 return ret;
9480 }
9481
9482 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9483
9484 static bfd_boolean
9485 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9486 {
9487 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9488 && sym->st_shndx < SHN_LORESERVE)
9489 {
9490 /* The gABI doesn't support dynamic symbols in output sections
9491 beyond 64k. */
9492 _bfd_error_handler
9493 /* xgettext:c-format */
9494 (_("%pB: too many sections: %d (>= %d)"),
9495 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9496 bfd_set_error (bfd_error_nonrepresentable_section);
9497 return FALSE;
9498 }
9499 return TRUE;
9500 }
9501
9502 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9503 allowing an unsatisfied unversioned symbol in the DSO to match a
9504 versioned symbol that would normally require an explicit version.
9505 We also handle the case that a DSO references a hidden symbol
9506 which may be satisfied by a versioned symbol in another DSO. */
9507
9508 static bfd_boolean
9509 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9510 const struct elf_backend_data *bed,
9511 struct elf_link_hash_entry *h)
9512 {
9513 bfd *abfd;
9514 struct elf_link_loaded_list *loaded;
9515
9516 if (!is_elf_hash_table (info->hash))
9517 return FALSE;
9518
9519 /* Check indirect symbol. */
9520 while (h->root.type == bfd_link_hash_indirect)
9521 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9522
9523 switch (h->root.type)
9524 {
9525 default:
9526 abfd = NULL;
9527 break;
9528
9529 case bfd_link_hash_undefined:
9530 case bfd_link_hash_undefweak:
9531 abfd = h->root.u.undef.abfd;
9532 if (abfd == NULL
9533 || (abfd->flags & DYNAMIC) == 0
9534 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9535 return FALSE;
9536 break;
9537
9538 case bfd_link_hash_defined:
9539 case bfd_link_hash_defweak:
9540 abfd = h->root.u.def.section->owner;
9541 break;
9542
9543 case bfd_link_hash_common:
9544 abfd = h->root.u.c.p->section->owner;
9545 break;
9546 }
9547 BFD_ASSERT (abfd != NULL);
9548
9549 for (loaded = elf_hash_table (info)->loaded;
9550 loaded != NULL;
9551 loaded = loaded->next)
9552 {
9553 bfd *input;
9554 Elf_Internal_Shdr *hdr;
9555 size_t symcount;
9556 size_t extsymcount;
9557 size_t extsymoff;
9558 Elf_Internal_Shdr *versymhdr;
9559 Elf_Internal_Sym *isym;
9560 Elf_Internal_Sym *isymend;
9561 Elf_Internal_Sym *isymbuf;
9562 Elf_External_Versym *ever;
9563 Elf_External_Versym *extversym;
9564
9565 input = loaded->abfd;
9566
9567 /* We check each DSO for a possible hidden versioned definition. */
9568 if (input == abfd
9569 || (input->flags & DYNAMIC) == 0
9570 || elf_dynversym (input) == 0)
9571 continue;
9572
9573 hdr = &elf_tdata (input)->dynsymtab_hdr;
9574
9575 symcount = hdr->sh_size / bed->s->sizeof_sym;
9576 if (elf_bad_symtab (input))
9577 {
9578 extsymcount = symcount;
9579 extsymoff = 0;
9580 }
9581 else
9582 {
9583 extsymcount = symcount - hdr->sh_info;
9584 extsymoff = hdr->sh_info;
9585 }
9586
9587 if (extsymcount == 0)
9588 continue;
9589
9590 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9591 NULL, NULL, NULL);
9592 if (isymbuf == NULL)
9593 return FALSE;
9594
9595 /* Read in any version definitions. */
9596 versymhdr = &elf_tdata (input)->dynversym_hdr;
9597 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
9598 if (extversym == NULL)
9599 goto error_ret;
9600
9601 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9602 || (bfd_bread (extversym, versymhdr->sh_size, input)
9603 != versymhdr->sh_size))
9604 {
9605 free (extversym);
9606 error_ret:
9607 free (isymbuf);
9608 return FALSE;
9609 }
9610
9611 ever = extversym + extsymoff;
9612 isymend = isymbuf + extsymcount;
9613 for (isym = isymbuf; isym < isymend; isym++, ever++)
9614 {
9615 const char *name;
9616 Elf_Internal_Versym iver;
9617 unsigned short version_index;
9618
9619 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9620 || isym->st_shndx == SHN_UNDEF)
9621 continue;
9622
9623 name = bfd_elf_string_from_elf_section (input,
9624 hdr->sh_link,
9625 isym->st_name);
9626 if (strcmp (name, h->root.root.string) != 0)
9627 continue;
9628
9629 _bfd_elf_swap_versym_in (input, ever, &iver);
9630
9631 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9632 && !(h->def_regular
9633 && h->forced_local))
9634 {
9635 /* If we have a non-hidden versioned sym, then it should
9636 have provided a definition for the undefined sym unless
9637 it is defined in a non-shared object and forced local.
9638 */
9639 abort ();
9640 }
9641
9642 version_index = iver.vs_vers & VERSYM_VERSION;
9643 if (version_index == 1 || version_index == 2)
9644 {
9645 /* This is the base or first version. We can use it. */
9646 free (extversym);
9647 free (isymbuf);
9648 return TRUE;
9649 }
9650 }
9651
9652 free (extversym);
9653 free (isymbuf);
9654 }
9655
9656 return FALSE;
9657 }
9658
9659 /* Convert ELF common symbol TYPE. */
9660
9661 static int
9662 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9663 {
9664 /* Commom symbol can only appear in relocatable link. */
9665 if (!bfd_link_relocatable (info))
9666 abort ();
9667 switch (info->elf_stt_common)
9668 {
9669 case unchanged:
9670 break;
9671 case elf_stt_common:
9672 type = STT_COMMON;
9673 break;
9674 case no_elf_stt_common:
9675 type = STT_OBJECT;
9676 break;
9677 }
9678 return type;
9679 }
9680
9681 /* Add an external symbol to the symbol table. This is called from
9682 the hash table traversal routine. When generating a shared object,
9683 we go through the symbol table twice. The first time we output
9684 anything that might have been forced to local scope in a version
9685 script. The second time we output the symbols that are still
9686 global symbols. */
9687
9688 static bfd_boolean
9689 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9690 {
9691 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9692 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9693 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9694 bfd_boolean strip;
9695 Elf_Internal_Sym sym;
9696 asection *input_sec;
9697 const struct elf_backend_data *bed;
9698 long indx;
9699 int ret;
9700 unsigned int type;
9701
9702 if (h->root.type == bfd_link_hash_warning)
9703 {
9704 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9705 if (h->root.type == bfd_link_hash_new)
9706 return TRUE;
9707 }
9708
9709 /* Decide whether to output this symbol in this pass. */
9710 if (eoinfo->localsyms)
9711 {
9712 if (!h->forced_local)
9713 return TRUE;
9714 }
9715 else
9716 {
9717 if (h->forced_local)
9718 return TRUE;
9719 }
9720
9721 bed = get_elf_backend_data (flinfo->output_bfd);
9722
9723 if (h->root.type == bfd_link_hash_undefined)
9724 {
9725 /* If we have an undefined symbol reference here then it must have
9726 come from a shared library that is being linked in. (Undefined
9727 references in regular files have already been handled unless
9728 they are in unreferenced sections which are removed by garbage
9729 collection). */
9730 bfd_boolean ignore_undef = FALSE;
9731
9732 /* Some symbols may be special in that the fact that they're
9733 undefined can be safely ignored - let backend determine that. */
9734 if (bed->elf_backend_ignore_undef_symbol)
9735 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9736
9737 /* If we are reporting errors for this situation then do so now. */
9738 if (!ignore_undef
9739 && h->ref_dynamic
9740 && (!h->ref_regular || flinfo->info->gc_sections)
9741 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9742 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9743 (*flinfo->info->callbacks->undefined_symbol)
9744 (flinfo->info, h->root.root.string,
9745 h->ref_regular ? NULL : h->root.u.undef.abfd,
9746 NULL, 0,
9747 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
9748
9749 /* Strip a global symbol defined in a discarded section. */
9750 if (h->indx == -3)
9751 return TRUE;
9752 }
9753
9754 /* We should also warn if a forced local symbol is referenced from
9755 shared libraries. */
9756 if (bfd_link_executable (flinfo->info)
9757 && h->forced_local
9758 && h->ref_dynamic
9759 && h->def_regular
9760 && !h->dynamic_def
9761 && h->ref_dynamic_nonweak
9762 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9763 {
9764 bfd *def_bfd;
9765 const char *msg;
9766 struct elf_link_hash_entry *hi = h;
9767
9768 /* Check indirect symbol. */
9769 while (hi->root.type == bfd_link_hash_indirect)
9770 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9771
9772 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9773 /* xgettext:c-format */
9774 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
9775 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9776 /* xgettext:c-format */
9777 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
9778 else
9779 /* xgettext:c-format */
9780 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
9781 def_bfd = flinfo->output_bfd;
9782 if (hi->root.u.def.section != bfd_abs_section_ptr)
9783 def_bfd = hi->root.u.def.section->owner;
9784 _bfd_error_handler (msg, flinfo->output_bfd,
9785 h->root.root.string, def_bfd);
9786 bfd_set_error (bfd_error_bad_value);
9787 eoinfo->failed = TRUE;
9788 return FALSE;
9789 }
9790
9791 /* We don't want to output symbols that have never been mentioned by
9792 a regular file, or that we have been told to strip. However, if
9793 h->indx is set to -2, the symbol is used by a reloc and we must
9794 output it. */
9795 strip = FALSE;
9796 if (h->indx == -2)
9797 ;
9798 else if ((h->def_dynamic
9799 || h->ref_dynamic
9800 || h->root.type == bfd_link_hash_new)
9801 && !h->def_regular
9802 && !h->ref_regular)
9803 strip = TRUE;
9804 else if (flinfo->info->strip == strip_all)
9805 strip = TRUE;
9806 else if (flinfo->info->strip == strip_some
9807 && bfd_hash_lookup (flinfo->info->keep_hash,
9808 h->root.root.string, FALSE, FALSE) == NULL)
9809 strip = TRUE;
9810 else if ((h->root.type == bfd_link_hash_defined
9811 || h->root.type == bfd_link_hash_defweak)
9812 && ((flinfo->info->strip_discarded
9813 && discarded_section (h->root.u.def.section))
9814 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9815 && h->root.u.def.section->owner != NULL
9816 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9817 strip = TRUE;
9818 else if ((h->root.type == bfd_link_hash_undefined
9819 || h->root.type == bfd_link_hash_undefweak)
9820 && h->root.u.undef.abfd != NULL
9821 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9822 strip = TRUE;
9823
9824 type = h->type;
9825
9826 /* If we're stripping it, and it's not a dynamic symbol, there's
9827 nothing else to do. However, if it is a forced local symbol or
9828 an ifunc symbol we need to give the backend finish_dynamic_symbol
9829 function a chance to make it dynamic. */
9830 if (strip
9831 && h->dynindx == -1
9832 && type != STT_GNU_IFUNC
9833 && !h->forced_local)
9834 return TRUE;
9835
9836 sym.st_value = 0;
9837 sym.st_size = h->size;
9838 sym.st_other = h->other;
9839 switch (h->root.type)
9840 {
9841 default:
9842 case bfd_link_hash_new:
9843 case bfd_link_hash_warning:
9844 abort ();
9845 return FALSE;
9846
9847 case bfd_link_hash_undefined:
9848 case bfd_link_hash_undefweak:
9849 input_sec = bfd_und_section_ptr;
9850 sym.st_shndx = SHN_UNDEF;
9851 break;
9852
9853 case bfd_link_hash_defined:
9854 case bfd_link_hash_defweak:
9855 {
9856 input_sec = h->root.u.def.section;
9857 if (input_sec->output_section != NULL)
9858 {
9859 sym.st_shndx =
9860 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9861 input_sec->output_section);
9862 if (sym.st_shndx == SHN_BAD)
9863 {
9864 _bfd_error_handler
9865 /* xgettext:c-format */
9866 (_("%pB: could not find output section %pA for input section %pA"),
9867 flinfo->output_bfd, input_sec->output_section, input_sec);
9868 bfd_set_error (bfd_error_nonrepresentable_section);
9869 eoinfo->failed = TRUE;
9870 return FALSE;
9871 }
9872
9873 /* ELF symbols in relocatable files are section relative,
9874 but in nonrelocatable files they are virtual
9875 addresses. */
9876 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9877 if (!bfd_link_relocatable (flinfo->info))
9878 {
9879 sym.st_value += input_sec->output_section->vma;
9880 if (h->type == STT_TLS)
9881 {
9882 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9883 if (tls_sec != NULL)
9884 sym.st_value -= tls_sec->vma;
9885 }
9886 }
9887 }
9888 else
9889 {
9890 BFD_ASSERT (input_sec->owner == NULL
9891 || (input_sec->owner->flags & DYNAMIC) != 0);
9892 sym.st_shndx = SHN_UNDEF;
9893 input_sec = bfd_und_section_ptr;
9894 }
9895 }
9896 break;
9897
9898 case bfd_link_hash_common:
9899 input_sec = h->root.u.c.p->section;
9900 sym.st_shndx = bed->common_section_index (input_sec);
9901 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9902 break;
9903
9904 case bfd_link_hash_indirect:
9905 /* These symbols are created by symbol versioning. They point
9906 to the decorated version of the name. For example, if the
9907 symbol foo@@GNU_1.2 is the default, which should be used when
9908 foo is used with no version, then we add an indirect symbol
9909 foo which points to foo@@GNU_1.2. We ignore these symbols,
9910 since the indirected symbol is already in the hash table. */
9911 return TRUE;
9912 }
9913
9914 if (type == STT_COMMON || type == STT_OBJECT)
9915 switch (h->root.type)
9916 {
9917 case bfd_link_hash_common:
9918 type = elf_link_convert_common_type (flinfo->info, type);
9919 break;
9920 case bfd_link_hash_defined:
9921 case bfd_link_hash_defweak:
9922 if (bed->common_definition (&sym))
9923 type = elf_link_convert_common_type (flinfo->info, type);
9924 else
9925 type = STT_OBJECT;
9926 break;
9927 case bfd_link_hash_undefined:
9928 case bfd_link_hash_undefweak:
9929 break;
9930 default:
9931 abort ();
9932 }
9933
9934 if (h->forced_local)
9935 {
9936 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9937 /* Turn off visibility on local symbol. */
9938 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9939 }
9940 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9941 else if (h->unique_global && h->def_regular)
9942 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9943 else if (h->root.type == bfd_link_hash_undefweak
9944 || h->root.type == bfd_link_hash_defweak)
9945 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9946 else
9947 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9948 sym.st_target_internal = h->target_internal;
9949
9950 /* Give the processor backend a chance to tweak the symbol value,
9951 and also to finish up anything that needs to be done for this
9952 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9953 forced local syms when non-shared is due to a historical quirk.
9954 STT_GNU_IFUNC symbol must go through PLT. */
9955 if ((h->type == STT_GNU_IFUNC
9956 && h->def_regular
9957 && !bfd_link_relocatable (flinfo->info))
9958 || ((h->dynindx != -1
9959 || h->forced_local)
9960 && ((bfd_link_pic (flinfo->info)
9961 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9962 || h->root.type != bfd_link_hash_undefweak))
9963 || !h->forced_local)
9964 && elf_hash_table (flinfo->info)->dynamic_sections_created))
9965 {
9966 if (! ((*bed->elf_backend_finish_dynamic_symbol)
9967 (flinfo->output_bfd, flinfo->info, h, &sym)))
9968 {
9969 eoinfo->failed = TRUE;
9970 return FALSE;
9971 }
9972 }
9973
9974 /* If we are marking the symbol as undefined, and there are no
9975 non-weak references to this symbol from a regular object, then
9976 mark the symbol as weak undefined; if there are non-weak
9977 references, mark the symbol as strong. We can't do this earlier,
9978 because it might not be marked as undefined until the
9979 finish_dynamic_symbol routine gets through with it. */
9980 if (sym.st_shndx == SHN_UNDEF
9981 && h->ref_regular
9982 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9983 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9984 {
9985 int bindtype;
9986 type = ELF_ST_TYPE (sym.st_info);
9987
9988 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9989 if (type == STT_GNU_IFUNC)
9990 type = STT_FUNC;
9991
9992 if (h->ref_regular_nonweak)
9993 bindtype = STB_GLOBAL;
9994 else
9995 bindtype = STB_WEAK;
9996 sym.st_info = ELF_ST_INFO (bindtype, type);
9997 }
9998
9999 /* If this is a symbol defined in a dynamic library, don't use the
10000 symbol size from the dynamic library. Relinking an executable
10001 against a new library may introduce gratuitous changes in the
10002 executable's symbols if we keep the size. */
10003 if (sym.st_shndx == SHN_UNDEF
10004 && !h->def_regular
10005 && h->def_dynamic)
10006 sym.st_size = 0;
10007
10008 /* If a non-weak symbol with non-default visibility is not defined
10009 locally, it is a fatal error. */
10010 if (!bfd_link_relocatable (flinfo->info)
10011 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10012 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10013 && h->root.type == bfd_link_hash_undefined
10014 && !h->def_regular)
10015 {
10016 const char *msg;
10017
10018 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10019 /* xgettext:c-format */
10020 msg = _("%pB: protected symbol `%s' isn't defined");
10021 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10022 /* xgettext:c-format */
10023 msg = _("%pB: internal symbol `%s' isn't defined");
10024 else
10025 /* xgettext:c-format */
10026 msg = _("%pB: hidden symbol `%s' isn't defined");
10027 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10028 bfd_set_error (bfd_error_bad_value);
10029 eoinfo->failed = TRUE;
10030 return FALSE;
10031 }
10032
10033 /* If this symbol should be put in the .dynsym section, then put it
10034 there now. We already know the symbol index. We also fill in
10035 the entry in the .hash section. */
10036 if (h->dynindx != -1
10037 && elf_hash_table (flinfo->info)->dynamic_sections_created
10038 && elf_hash_table (flinfo->info)->dynsym != NULL
10039 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
10040 {
10041 bfd_byte *esym;
10042
10043 /* Since there is no version information in the dynamic string,
10044 if there is no version info in symbol version section, we will
10045 have a run-time problem if not linking executable, referenced
10046 by shared library, or not bound locally. */
10047 if (h->verinfo.verdef == NULL
10048 && (!bfd_link_executable (flinfo->info)
10049 || h->ref_dynamic
10050 || !h->def_regular))
10051 {
10052 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10053
10054 if (p && p [1] != '\0')
10055 {
10056 _bfd_error_handler
10057 /* xgettext:c-format */
10058 (_("%pB: no symbol version section for versioned symbol `%s'"),
10059 flinfo->output_bfd, h->root.root.string);
10060 eoinfo->failed = TRUE;
10061 return FALSE;
10062 }
10063 }
10064
10065 sym.st_name = h->dynstr_index;
10066 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10067 + h->dynindx * bed->s->sizeof_sym);
10068 if (!check_dynsym (flinfo->output_bfd, &sym))
10069 {
10070 eoinfo->failed = TRUE;
10071 return FALSE;
10072 }
10073 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
10074
10075 if (flinfo->hash_sec != NULL)
10076 {
10077 size_t hash_entry_size;
10078 bfd_byte *bucketpos;
10079 bfd_vma chain;
10080 size_t bucketcount;
10081 size_t bucket;
10082
10083 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
10084 bucket = h->u.elf_hash_value % bucketcount;
10085
10086 hash_entry_size
10087 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10088 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
10089 + (bucket + 2) * hash_entry_size);
10090 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10091 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10092 bucketpos);
10093 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10094 ((bfd_byte *) flinfo->hash_sec->contents
10095 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10096 }
10097
10098 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
10099 {
10100 Elf_Internal_Versym iversym;
10101 Elf_External_Versym *eversym;
10102
10103 if (!h->def_regular)
10104 {
10105 if (h->verinfo.verdef == NULL
10106 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10107 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
10108 iversym.vs_vers = 0;
10109 else
10110 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10111 }
10112 else
10113 {
10114 if (h->verinfo.vertree == NULL)
10115 iversym.vs_vers = 1;
10116 else
10117 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
10118 if (flinfo->info->create_default_symver)
10119 iversym.vs_vers++;
10120 }
10121
10122 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10123 defined locally. */
10124 if (h->versioned == versioned_hidden && h->def_regular)
10125 iversym.vs_vers |= VERSYM_HIDDEN;
10126
10127 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10128 eversym += h->dynindx;
10129 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10130 }
10131 }
10132
10133 /* If the symbol is undefined, and we didn't output it to .dynsym,
10134 strip it from .symtab too. Obviously we can't do this for
10135 relocatable output or when needed for --emit-relocs. */
10136 else if (input_sec == bfd_und_section_ptr
10137 && h->indx != -2
10138 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10139 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10140 && !bfd_link_relocatable (flinfo->info))
10141 return TRUE;
10142
10143 /* Also strip others that we couldn't earlier due to dynamic symbol
10144 processing. */
10145 if (strip)
10146 return TRUE;
10147 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10148 return TRUE;
10149
10150 /* Output a FILE symbol so that following locals are not associated
10151 with the wrong input file. We need one for forced local symbols
10152 if we've seen more than one FILE symbol or when we have exactly
10153 one FILE symbol but global symbols are present in a file other
10154 than the one with the FILE symbol. We also need one if linker
10155 defined symbols are present. In practice these conditions are
10156 always met, so just emit the FILE symbol unconditionally. */
10157 if (eoinfo->localsyms
10158 && !eoinfo->file_sym_done
10159 && eoinfo->flinfo->filesym_count != 0)
10160 {
10161 Elf_Internal_Sym fsym;
10162
10163 memset (&fsym, 0, sizeof (fsym));
10164 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10165 fsym.st_shndx = SHN_ABS;
10166 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10167 bfd_und_section_ptr, NULL))
10168 return FALSE;
10169
10170 eoinfo->file_sym_done = TRUE;
10171 }
10172
10173 indx = bfd_get_symcount (flinfo->output_bfd);
10174 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10175 input_sec, h);
10176 if (ret == 0)
10177 {
10178 eoinfo->failed = TRUE;
10179 return FALSE;
10180 }
10181 else if (ret == 1)
10182 h->indx = indx;
10183 else if (h->indx == -2)
10184 abort();
10185
10186 return TRUE;
10187 }
10188
10189 /* Return TRUE if special handling is done for relocs in SEC against
10190 symbols defined in discarded sections. */
10191
10192 static bfd_boolean
10193 elf_section_ignore_discarded_relocs (asection *sec)
10194 {
10195 const struct elf_backend_data *bed;
10196
10197 switch (sec->sec_info_type)
10198 {
10199 case SEC_INFO_TYPE_STABS:
10200 case SEC_INFO_TYPE_EH_FRAME:
10201 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10202 return TRUE;
10203 default:
10204 break;
10205 }
10206
10207 bed = get_elf_backend_data (sec->owner);
10208 if (bed->elf_backend_ignore_discarded_relocs != NULL
10209 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10210 return TRUE;
10211
10212 return FALSE;
10213 }
10214
10215 /* Return a mask saying how ld should treat relocations in SEC against
10216 symbols defined in discarded sections. If this function returns
10217 COMPLAIN set, ld will issue a warning message. If this function
10218 returns PRETEND set, and the discarded section was link-once and the
10219 same size as the kept link-once section, ld will pretend that the
10220 symbol was actually defined in the kept section. Otherwise ld will
10221 zero the reloc (at least that is the intent, but some cooperation by
10222 the target dependent code is needed, particularly for REL targets). */
10223
10224 unsigned int
10225 _bfd_elf_default_action_discarded (asection *sec)
10226 {
10227 if (sec->flags & SEC_DEBUGGING)
10228 return PRETEND;
10229
10230 if (strcmp (".eh_frame", sec->name) == 0)
10231 return 0;
10232
10233 if (strcmp (".gcc_except_table", sec->name) == 0)
10234 return 0;
10235
10236 return COMPLAIN | PRETEND;
10237 }
10238
10239 /* Find a match between a section and a member of a section group. */
10240
10241 static asection *
10242 match_group_member (asection *sec, asection *group,
10243 struct bfd_link_info *info)
10244 {
10245 asection *first = elf_next_in_group (group);
10246 asection *s = first;
10247
10248 while (s != NULL)
10249 {
10250 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10251 return s;
10252
10253 s = elf_next_in_group (s);
10254 if (s == first)
10255 break;
10256 }
10257
10258 return NULL;
10259 }
10260
10261 /* Check if the kept section of a discarded section SEC can be used
10262 to replace it. Return the replacement if it is OK. Otherwise return
10263 NULL. */
10264
10265 asection *
10266 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10267 {
10268 asection *kept;
10269
10270 kept = sec->kept_section;
10271 if (kept != NULL)
10272 {
10273 if ((kept->flags & SEC_GROUP) != 0)
10274 kept = match_group_member (sec, kept, info);
10275 if (kept != NULL
10276 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10277 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
10278 kept = NULL;
10279 sec->kept_section = kept;
10280 }
10281 return kept;
10282 }
10283
10284 /* Link an input file into the linker output file. This function
10285 handles all the sections and relocations of the input file at once.
10286 This is so that we only have to read the local symbols once, and
10287 don't have to keep them in memory. */
10288
10289 static bfd_boolean
10290 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10291 {
10292 int (*relocate_section)
10293 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10294 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10295 bfd *output_bfd;
10296 Elf_Internal_Shdr *symtab_hdr;
10297 size_t locsymcount;
10298 size_t extsymoff;
10299 Elf_Internal_Sym *isymbuf;
10300 Elf_Internal_Sym *isym;
10301 Elf_Internal_Sym *isymend;
10302 long *pindex;
10303 asection **ppsection;
10304 asection *o;
10305 const struct elf_backend_data *bed;
10306 struct elf_link_hash_entry **sym_hashes;
10307 bfd_size_type address_size;
10308 bfd_vma r_type_mask;
10309 int r_sym_shift;
10310 bfd_boolean have_file_sym = FALSE;
10311
10312 output_bfd = flinfo->output_bfd;
10313 bed = get_elf_backend_data (output_bfd);
10314 relocate_section = bed->elf_backend_relocate_section;
10315
10316 /* If this is a dynamic object, we don't want to do anything here:
10317 we don't want the local symbols, and we don't want the section
10318 contents. */
10319 if ((input_bfd->flags & DYNAMIC) != 0)
10320 return TRUE;
10321
10322 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10323 if (elf_bad_symtab (input_bfd))
10324 {
10325 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10326 extsymoff = 0;
10327 }
10328 else
10329 {
10330 locsymcount = symtab_hdr->sh_info;
10331 extsymoff = symtab_hdr->sh_info;
10332 }
10333
10334 /* Read the local symbols. */
10335 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10336 if (isymbuf == NULL && locsymcount != 0)
10337 {
10338 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10339 flinfo->internal_syms,
10340 flinfo->external_syms,
10341 flinfo->locsym_shndx);
10342 if (isymbuf == NULL)
10343 return FALSE;
10344 }
10345
10346 /* Find local symbol sections and adjust values of symbols in
10347 SEC_MERGE sections. Write out those local symbols we know are
10348 going into the output file. */
10349 isymend = isymbuf + locsymcount;
10350 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10351 isym < isymend;
10352 isym++, pindex++, ppsection++)
10353 {
10354 asection *isec;
10355 const char *name;
10356 Elf_Internal_Sym osym;
10357 long indx;
10358 int ret;
10359
10360 *pindex = -1;
10361
10362 if (elf_bad_symtab (input_bfd))
10363 {
10364 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10365 {
10366 *ppsection = NULL;
10367 continue;
10368 }
10369 }
10370
10371 if (isym->st_shndx == SHN_UNDEF)
10372 isec = bfd_und_section_ptr;
10373 else if (isym->st_shndx == SHN_ABS)
10374 isec = bfd_abs_section_ptr;
10375 else if (isym->st_shndx == SHN_COMMON)
10376 isec = bfd_com_section_ptr;
10377 else
10378 {
10379 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10380 if (isec == NULL)
10381 {
10382 /* Don't attempt to output symbols with st_shnx in the
10383 reserved range other than SHN_ABS and SHN_COMMON. */
10384 *ppsection = NULL;
10385 continue;
10386 }
10387 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10388 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10389 isym->st_value =
10390 _bfd_merged_section_offset (output_bfd, &isec,
10391 elf_section_data (isec)->sec_info,
10392 isym->st_value);
10393 }
10394
10395 *ppsection = isec;
10396
10397 /* Don't output the first, undefined, symbol. In fact, don't
10398 output any undefined local symbol. */
10399 if (isec == bfd_und_section_ptr)
10400 continue;
10401
10402 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10403 {
10404 /* We never output section symbols. Instead, we use the
10405 section symbol of the corresponding section in the output
10406 file. */
10407 continue;
10408 }
10409
10410 /* If we are stripping all symbols, we don't want to output this
10411 one. */
10412 if (flinfo->info->strip == strip_all)
10413 continue;
10414
10415 /* If we are discarding all local symbols, we don't want to
10416 output this one. If we are generating a relocatable output
10417 file, then some of the local symbols may be required by
10418 relocs; we output them below as we discover that they are
10419 needed. */
10420 if (flinfo->info->discard == discard_all)
10421 continue;
10422
10423 /* If this symbol is defined in a section which we are
10424 discarding, we don't need to keep it. */
10425 if (isym->st_shndx != SHN_UNDEF
10426 && isym->st_shndx < SHN_LORESERVE
10427 && bfd_section_removed_from_list (output_bfd,
10428 isec->output_section))
10429 continue;
10430
10431 /* Get the name of the symbol. */
10432 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10433 isym->st_name);
10434 if (name == NULL)
10435 return FALSE;
10436
10437 /* See if we are discarding symbols with this name. */
10438 if ((flinfo->info->strip == strip_some
10439 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
10440 == NULL))
10441 || (((flinfo->info->discard == discard_sec_merge
10442 && (isec->flags & SEC_MERGE)
10443 && !bfd_link_relocatable (flinfo->info))
10444 || flinfo->info->discard == discard_l)
10445 && bfd_is_local_label_name (input_bfd, name)))
10446 continue;
10447
10448 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10449 {
10450 if (input_bfd->lto_output)
10451 /* -flto puts a temp file name here. This means builds
10452 are not reproducible. Discard the symbol. */
10453 continue;
10454 have_file_sym = TRUE;
10455 flinfo->filesym_count += 1;
10456 }
10457 if (!have_file_sym)
10458 {
10459 /* In the absence of debug info, bfd_find_nearest_line uses
10460 FILE symbols to determine the source file for local
10461 function symbols. Provide a FILE symbol here if input
10462 files lack such, so that their symbols won't be
10463 associated with a previous input file. It's not the
10464 source file, but the best we can do. */
10465 have_file_sym = TRUE;
10466 flinfo->filesym_count += 1;
10467 memset (&osym, 0, sizeof (osym));
10468 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10469 osym.st_shndx = SHN_ABS;
10470 if (!elf_link_output_symstrtab (flinfo,
10471 (input_bfd->lto_output ? NULL
10472 : input_bfd->filename),
10473 &osym, bfd_abs_section_ptr,
10474 NULL))
10475 return FALSE;
10476 }
10477
10478 osym = *isym;
10479
10480 /* Adjust the section index for the output file. */
10481 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10482 isec->output_section);
10483 if (osym.st_shndx == SHN_BAD)
10484 return FALSE;
10485
10486 /* ELF symbols in relocatable files are section relative, but
10487 in executable files they are virtual addresses. Note that
10488 this code assumes that all ELF sections have an associated
10489 BFD section with a reasonable value for output_offset; below
10490 we assume that they also have a reasonable value for
10491 output_section. Any special sections must be set up to meet
10492 these requirements. */
10493 osym.st_value += isec->output_offset;
10494 if (!bfd_link_relocatable (flinfo->info))
10495 {
10496 osym.st_value += isec->output_section->vma;
10497 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10498 {
10499 /* STT_TLS symbols are relative to PT_TLS segment base. */
10500 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
10501 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10502 else
10503 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
10504 STT_NOTYPE);
10505 }
10506 }
10507
10508 indx = bfd_get_symcount (output_bfd);
10509 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10510 if (ret == 0)
10511 return FALSE;
10512 else if (ret == 1)
10513 *pindex = indx;
10514 }
10515
10516 if (bed->s->arch_size == 32)
10517 {
10518 r_type_mask = 0xff;
10519 r_sym_shift = 8;
10520 address_size = 4;
10521 }
10522 else
10523 {
10524 r_type_mask = 0xffffffff;
10525 r_sym_shift = 32;
10526 address_size = 8;
10527 }
10528
10529 /* Relocate the contents of each section. */
10530 sym_hashes = elf_sym_hashes (input_bfd);
10531 for (o = input_bfd->sections; o != NULL; o = o->next)
10532 {
10533 bfd_byte *contents;
10534
10535 if (! o->linker_mark)
10536 {
10537 /* This section was omitted from the link. */
10538 continue;
10539 }
10540
10541 if (!flinfo->info->resolve_section_groups
10542 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10543 {
10544 /* Deal with the group signature symbol. */
10545 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10546 unsigned long symndx = sec_data->this_hdr.sh_info;
10547 asection *osec = o->output_section;
10548
10549 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
10550 if (symndx >= locsymcount
10551 || (elf_bad_symtab (input_bfd)
10552 && flinfo->sections[symndx] == NULL))
10553 {
10554 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10555 while (h->root.type == bfd_link_hash_indirect
10556 || h->root.type == bfd_link_hash_warning)
10557 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10558 /* Arrange for symbol to be output. */
10559 h->indx = -2;
10560 elf_section_data (osec)->this_hdr.sh_info = -2;
10561 }
10562 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10563 {
10564 /* We'll use the output section target_index. */
10565 asection *sec = flinfo->sections[symndx]->output_section;
10566 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10567 }
10568 else
10569 {
10570 if (flinfo->indices[symndx] == -1)
10571 {
10572 /* Otherwise output the local symbol now. */
10573 Elf_Internal_Sym sym = isymbuf[symndx];
10574 asection *sec = flinfo->sections[symndx]->output_section;
10575 const char *name;
10576 long indx;
10577 int ret;
10578
10579 name = bfd_elf_string_from_elf_section (input_bfd,
10580 symtab_hdr->sh_link,
10581 sym.st_name);
10582 if (name == NULL)
10583 return FALSE;
10584
10585 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10586 sec);
10587 if (sym.st_shndx == SHN_BAD)
10588 return FALSE;
10589
10590 sym.st_value += o->output_offset;
10591
10592 indx = bfd_get_symcount (output_bfd);
10593 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10594 NULL);
10595 if (ret == 0)
10596 return FALSE;
10597 else if (ret == 1)
10598 flinfo->indices[symndx] = indx;
10599 else
10600 abort ();
10601 }
10602 elf_section_data (osec)->this_hdr.sh_info
10603 = flinfo->indices[symndx];
10604 }
10605 }
10606
10607 if ((o->flags & SEC_HAS_CONTENTS) == 0
10608 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10609 continue;
10610
10611 if ((o->flags & SEC_LINKER_CREATED) != 0)
10612 {
10613 /* Section was created by _bfd_elf_link_create_dynamic_sections
10614 or somesuch. */
10615 continue;
10616 }
10617
10618 /* Get the contents of the section. They have been cached by a
10619 relaxation routine. Note that o is a section in an input
10620 file, so the contents field will not have been set by any of
10621 the routines which work on output files. */
10622 if (elf_section_data (o)->this_hdr.contents != NULL)
10623 {
10624 contents = elf_section_data (o)->this_hdr.contents;
10625 if (bed->caches_rawsize
10626 && o->rawsize != 0
10627 && o->rawsize < o->size)
10628 {
10629 memcpy (flinfo->contents, contents, o->rawsize);
10630 contents = flinfo->contents;
10631 }
10632 }
10633 else
10634 {
10635 contents = flinfo->contents;
10636 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10637 return FALSE;
10638 }
10639
10640 if ((o->flags & SEC_RELOC) != 0)
10641 {
10642 Elf_Internal_Rela *internal_relocs;
10643 Elf_Internal_Rela *rel, *relend;
10644 int action_discarded;
10645 int ret;
10646
10647 /* Get the swapped relocs. */
10648 internal_relocs
10649 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10650 flinfo->internal_relocs, FALSE);
10651 if (internal_relocs == NULL
10652 && o->reloc_count > 0)
10653 return FALSE;
10654
10655 /* We need to reverse-copy input .ctors/.dtors sections if
10656 they are placed in .init_array/.finit_array for output. */
10657 if (o->size > address_size
10658 && ((strncmp (o->name, ".ctors", 6) == 0
10659 && strcmp (o->output_section->name,
10660 ".init_array") == 0)
10661 || (strncmp (o->name, ".dtors", 6) == 0
10662 && strcmp (o->output_section->name,
10663 ".fini_array") == 0))
10664 && (o->name[6] == 0 || o->name[6] == '.'))
10665 {
10666 if (o->size * bed->s->int_rels_per_ext_rel
10667 != o->reloc_count * address_size)
10668 {
10669 _bfd_error_handler
10670 /* xgettext:c-format */
10671 (_("error: %pB: size of section %pA is not "
10672 "multiple of address size"),
10673 input_bfd, o);
10674 bfd_set_error (bfd_error_bad_value);
10675 return FALSE;
10676 }
10677 o->flags |= SEC_ELF_REVERSE_COPY;
10678 }
10679
10680 action_discarded = -1;
10681 if (!elf_section_ignore_discarded_relocs (o))
10682 action_discarded = (*bed->action_discarded) (o);
10683
10684 /* Run through the relocs evaluating complex reloc symbols and
10685 looking for relocs against symbols from discarded sections
10686 or section symbols from removed link-once sections.
10687 Complain about relocs against discarded sections. Zero
10688 relocs against removed link-once sections. */
10689
10690 rel = internal_relocs;
10691 relend = rel + o->reloc_count;
10692 for ( ; rel < relend; rel++)
10693 {
10694 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10695 unsigned int s_type;
10696 asection **ps, *sec;
10697 struct elf_link_hash_entry *h = NULL;
10698 const char *sym_name;
10699
10700 if (r_symndx == STN_UNDEF)
10701 continue;
10702
10703 if (r_symndx >= locsymcount
10704 || (elf_bad_symtab (input_bfd)
10705 && flinfo->sections[r_symndx] == NULL))
10706 {
10707 h = sym_hashes[r_symndx - extsymoff];
10708
10709 /* Badly formatted input files can contain relocs that
10710 reference non-existant symbols. Check here so that
10711 we do not seg fault. */
10712 if (h == NULL)
10713 {
10714 _bfd_error_handler
10715 /* xgettext:c-format */
10716 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
10717 "that references a non-existent global symbol"),
10718 input_bfd, (uint64_t) rel->r_info, o);
10719 bfd_set_error (bfd_error_bad_value);
10720 return FALSE;
10721 }
10722
10723 while (h->root.type == bfd_link_hash_indirect
10724 || h->root.type == bfd_link_hash_warning)
10725 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10726
10727 s_type = h->type;
10728
10729 /* If a plugin symbol is referenced from a non-IR file,
10730 mark the symbol as undefined. Note that the
10731 linker may attach linker created dynamic sections
10732 to the plugin bfd. Symbols defined in linker
10733 created sections are not plugin symbols. */
10734 if ((h->root.non_ir_ref_regular
10735 || h->root.non_ir_ref_dynamic)
10736 && (h->root.type == bfd_link_hash_defined
10737 || h->root.type == bfd_link_hash_defweak)
10738 && (h->root.u.def.section->flags
10739 & SEC_LINKER_CREATED) == 0
10740 && h->root.u.def.section->owner != NULL
10741 && (h->root.u.def.section->owner->flags
10742 & BFD_PLUGIN) != 0)
10743 {
10744 h->root.type = bfd_link_hash_undefined;
10745 h->root.u.undef.abfd = h->root.u.def.section->owner;
10746 }
10747
10748 ps = NULL;
10749 if (h->root.type == bfd_link_hash_defined
10750 || h->root.type == bfd_link_hash_defweak)
10751 ps = &h->root.u.def.section;
10752
10753 sym_name = h->root.root.string;
10754 }
10755 else
10756 {
10757 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10758
10759 s_type = ELF_ST_TYPE (sym->st_info);
10760 ps = &flinfo->sections[r_symndx];
10761 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10762 sym, *ps);
10763 }
10764
10765 if ((s_type == STT_RELC || s_type == STT_SRELC)
10766 && !bfd_link_relocatable (flinfo->info))
10767 {
10768 bfd_vma val;
10769 bfd_vma dot = (rel->r_offset
10770 + o->output_offset + o->output_section->vma);
10771 #ifdef DEBUG
10772 printf ("Encountered a complex symbol!");
10773 printf (" (input_bfd %s, section %s, reloc %ld\n",
10774 input_bfd->filename, o->name,
10775 (long) (rel - internal_relocs));
10776 printf (" symbol: idx %8.8lx, name %s\n",
10777 r_symndx, sym_name);
10778 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10779 (unsigned long) rel->r_info,
10780 (unsigned long) rel->r_offset);
10781 #endif
10782 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10783 isymbuf, locsymcount, s_type == STT_SRELC))
10784 return FALSE;
10785
10786 /* Symbol evaluated OK. Update to absolute value. */
10787 set_symbol_value (input_bfd, isymbuf, locsymcount,
10788 r_symndx, val);
10789 continue;
10790 }
10791
10792 if (action_discarded != -1 && ps != NULL)
10793 {
10794 /* Complain if the definition comes from a
10795 discarded section. */
10796 if ((sec = *ps) != NULL && discarded_section (sec))
10797 {
10798 BFD_ASSERT (r_symndx != STN_UNDEF);
10799 if (action_discarded & COMPLAIN)
10800 (*flinfo->info->callbacks->einfo)
10801 /* xgettext:c-format */
10802 (_("%X`%s' referenced in section `%pA' of %pB: "
10803 "defined in discarded section `%pA' of %pB\n"),
10804 sym_name, o, input_bfd, sec, sec->owner);
10805
10806 /* Try to do the best we can to support buggy old
10807 versions of gcc. Pretend that the symbol is
10808 really defined in the kept linkonce section.
10809 FIXME: This is quite broken. Modifying the
10810 symbol here means we will be changing all later
10811 uses of the symbol, not just in this section. */
10812 if (action_discarded & PRETEND)
10813 {
10814 asection *kept;
10815
10816 kept = _bfd_elf_check_kept_section (sec,
10817 flinfo->info);
10818 if (kept != NULL)
10819 {
10820 *ps = kept;
10821 continue;
10822 }
10823 }
10824 }
10825 }
10826 }
10827
10828 /* Relocate the section by invoking a back end routine.
10829
10830 The back end routine is responsible for adjusting the
10831 section contents as necessary, and (if using Rela relocs
10832 and generating a relocatable output file) adjusting the
10833 reloc addend as necessary.
10834
10835 The back end routine does not have to worry about setting
10836 the reloc address or the reloc symbol index.
10837
10838 The back end routine is given a pointer to the swapped in
10839 internal symbols, and can access the hash table entries
10840 for the external symbols via elf_sym_hashes (input_bfd).
10841
10842 When generating relocatable output, the back end routine
10843 must handle STB_LOCAL/STT_SECTION symbols specially. The
10844 output symbol is going to be a section symbol
10845 corresponding to the output section, which will require
10846 the addend to be adjusted. */
10847
10848 ret = (*relocate_section) (output_bfd, flinfo->info,
10849 input_bfd, o, contents,
10850 internal_relocs,
10851 isymbuf,
10852 flinfo->sections);
10853 if (!ret)
10854 return FALSE;
10855
10856 if (ret == 2
10857 || bfd_link_relocatable (flinfo->info)
10858 || flinfo->info->emitrelocations)
10859 {
10860 Elf_Internal_Rela *irela;
10861 Elf_Internal_Rela *irelaend, *irelamid;
10862 bfd_vma last_offset;
10863 struct elf_link_hash_entry **rel_hash;
10864 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10865 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10866 unsigned int next_erel;
10867 bfd_boolean rela_normal;
10868 struct bfd_elf_section_data *esdi, *esdo;
10869
10870 esdi = elf_section_data (o);
10871 esdo = elf_section_data (o->output_section);
10872 rela_normal = FALSE;
10873
10874 /* Adjust the reloc addresses and symbol indices. */
10875
10876 irela = internal_relocs;
10877 irelaend = irela + o->reloc_count;
10878 rel_hash = esdo->rel.hashes + esdo->rel.count;
10879 /* We start processing the REL relocs, if any. When we reach
10880 IRELAMID in the loop, we switch to the RELA relocs. */
10881 irelamid = irela;
10882 if (esdi->rel.hdr != NULL)
10883 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10884 * bed->s->int_rels_per_ext_rel);
10885 rel_hash_list = rel_hash;
10886 rela_hash_list = NULL;
10887 last_offset = o->output_offset;
10888 if (!bfd_link_relocatable (flinfo->info))
10889 last_offset += o->output_section->vma;
10890 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10891 {
10892 unsigned long r_symndx;
10893 asection *sec;
10894 Elf_Internal_Sym sym;
10895
10896 if (next_erel == bed->s->int_rels_per_ext_rel)
10897 {
10898 rel_hash++;
10899 next_erel = 0;
10900 }
10901
10902 if (irela == irelamid)
10903 {
10904 rel_hash = esdo->rela.hashes + esdo->rela.count;
10905 rela_hash_list = rel_hash;
10906 rela_normal = bed->rela_normal;
10907 }
10908
10909 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10910 flinfo->info, o,
10911 irela->r_offset);
10912 if (irela->r_offset >= (bfd_vma) -2)
10913 {
10914 /* This is a reloc for a deleted entry or somesuch.
10915 Turn it into an R_*_NONE reloc, at the same
10916 offset as the last reloc. elf_eh_frame.c and
10917 bfd_elf_discard_info rely on reloc offsets
10918 being ordered. */
10919 irela->r_offset = last_offset;
10920 irela->r_info = 0;
10921 irela->r_addend = 0;
10922 continue;
10923 }
10924
10925 irela->r_offset += o->output_offset;
10926
10927 /* Relocs in an executable have to be virtual addresses. */
10928 if (!bfd_link_relocatable (flinfo->info))
10929 irela->r_offset += o->output_section->vma;
10930
10931 last_offset = irela->r_offset;
10932
10933 r_symndx = irela->r_info >> r_sym_shift;
10934 if (r_symndx == STN_UNDEF)
10935 continue;
10936
10937 if (r_symndx >= locsymcount
10938 || (elf_bad_symtab (input_bfd)
10939 && flinfo->sections[r_symndx] == NULL))
10940 {
10941 struct elf_link_hash_entry *rh;
10942 unsigned long indx;
10943
10944 /* This is a reloc against a global symbol. We
10945 have not yet output all the local symbols, so
10946 we do not know the symbol index of any global
10947 symbol. We set the rel_hash entry for this
10948 reloc to point to the global hash table entry
10949 for this symbol. The symbol index is then
10950 set at the end of bfd_elf_final_link. */
10951 indx = r_symndx - extsymoff;
10952 rh = elf_sym_hashes (input_bfd)[indx];
10953 while (rh->root.type == bfd_link_hash_indirect
10954 || rh->root.type == bfd_link_hash_warning)
10955 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10956
10957 /* Setting the index to -2 tells
10958 elf_link_output_extsym that this symbol is
10959 used by a reloc. */
10960 BFD_ASSERT (rh->indx < 0);
10961 rh->indx = -2;
10962 *rel_hash = rh;
10963
10964 continue;
10965 }
10966
10967 /* This is a reloc against a local symbol. */
10968
10969 *rel_hash = NULL;
10970 sym = isymbuf[r_symndx];
10971 sec = flinfo->sections[r_symndx];
10972 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10973 {
10974 /* I suppose the backend ought to fill in the
10975 section of any STT_SECTION symbol against a
10976 processor specific section. */
10977 r_symndx = STN_UNDEF;
10978 if (bfd_is_abs_section (sec))
10979 ;
10980 else if (sec == NULL || sec->owner == NULL)
10981 {
10982 bfd_set_error (bfd_error_bad_value);
10983 return FALSE;
10984 }
10985 else
10986 {
10987 asection *osec = sec->output_section;
10988
10989 /* If we have discarded a section, the output
10990 section will be the absolute section. In
10991 case of discarded SEC_MERGE sections, use
10992 the kept section. relocate_section should
10993 have already handled discarded linkonce
10994 sections. */
10995 if (bfd_is_abs_section (osec)
10996 && sec->kept_section != NULL
10997 && sec->kept_section->output_section != NULL)
10998 {
10999 osec = sec->kept_section->output_section;
11000 irela->r_addend -= osec->vma;
11001 }
11002
11003 if (!bfd_is_abs_section (osec))
11004 {
11005 r_symndx = osec->target_index;
11006 if (r_symndx == STN_UNDEF)
11007 {
11008 irela->r_addend += osec->vma;
11009 osec = _bfd_nearby_section (output_bfd, osec,
11010 osec->vma);
11011 irela->r_addend -= osec->vma;
11012 r_symndx = osec->target_index;
11013 }
11014 }
11015 }
11016
11017 /* Adjust the addend according to where the
11018 section winds up in the output section. */
11019 if (rela_normal)
11020 irela->r_addend += sec->output_offset;
11021 }
11022 else
11023 {
11024 if (flinfo->indices[r_symndx] == -1)
11025 {
11026 unsigned long shlink;
11027 const char *name;
11028 asection *osec;
11029 long indx;
11030
11031 if (flinfo->info->strip == strip_all)
11032 {
11033 /* You can't do ld -r -s. */
11034 bfd_set_error (bfd_error_invalid_operation);
11035 return FALSE;
11036 }
11037
11038 /* This symbol was skipped earlier, but
11039 since it is needed by a reloc, we
11040 must output it now. */
11041 shlink = symtab_hdr->sh_link;
11042 name = (bfd_elf_string_from_elf_section
11043 (input_bfd, shlink, sym.st_name));
11044 if (name == NULL)
11045 return FALSE;
11046
11047 osec = sec->output_section;
11048 sym.st_shndx =
11049 _bfd_elf_section_from_bfd_section (output_bfd,
11050 osec);
11051 if (sym.st_shndx == SHN_BAD)
11052 return FALSE;
11053
11054 sym.st_value += sec->output_offset;
11055 if (!bfd_link_relocatable (flinfo->info))
11056 {
11057 sym.st_value += osec->vma;
11058 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11059 {
11060 struct elf_link_hash_table *htab
11061 = elf_hash_table (flinfo->info);
11062
11063 /* STT_TLS symbols are relative to PT_TLS
11064 segment base. */
11065 if (htab->tls_sec != NULL)
11066 sym.st_value -= htab->tls_sec->vma;
11067 else
11068 sym.st_info
11069 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11070 STT_NOTYPE);
11071 }
11072 }
11073
11074 indx = bfd_get_symcount (output_bfd);
11075 ret = elf_link_output_symstrtab (flinfo, name,
11076 &sym, sec,
11077 NULL);
11078 if (ret == 0)
11079 return FALSE;
11080 else if (ret == 1)
11081 flinfo->indices[r_symndx] = indx;
11082 else
11083 abort ();
11084 }
11085
11086 r_symndx = flinfo->indices[r_symndx];
11087 }
11088
11089 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11090 | (irela->r_info & r_type_mask));
11091 }
11092
11093 /* Swap out the relocs. */
11094 input_rel_hdr = esdi->rel.hdr;
11095 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
11096 {
11097 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11098 input_rel_hdr,
11099 internal_relocs,
11100 rel_hash_list))
11101 return FALSE;
11102 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11103 * bed->s->int_rels_per_ext_rel);
11104 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
11105 }
11106
11107 input_rela_hdr = esdi->rela.hdr;
11108 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11109 {
11110 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11111 input_rela_hdr,
11112 internal_relocs,
11113 rela_hash_list))
11114 return FALSE;
11115 }
11116 }
11117 }
11118
11119 /* Write out the modified section contents. */
11120 if (bed->elf_backend_write_section
11121 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
11122 contents))
11123 {
11124 /* Section written out. */
11125 }
11126 else switch (o->sec_info_type)
11127 {
11128 case SEC_INFO_TYPE_STABS:
11129 if (! (_bfd_write_section_stabs
11130 (output_bfd,
11131 &elf_hash_table (flinfo->info)->stab_info,
11132 o, &elf_section_data (o)->sec_info, contents)))
11133 return FALSE;
11134 break;
11135 case SEC_INFO_TYPE_MERGE:
11136 if (! _bfd_write_merged_section (output_bfd, o,
11137 elf_section_data (o)->sec_info))
11138 return FALSE;
11139 break;
11140 case SEC_INFO_TYPE_EH_FRAME:
11141 {
11142 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11143 o, contents))
11144 return FALSE;
11145 }
11146 break;
11147 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11148 {
11149 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11150 flinfo->info,
11151 o, contents))
11152 return FALSE;
11153 }
11154 break;
11155 default:
11156 {
11157 if (! (o->flags & SEC_EXCLUDE))
11158 {
11159 file_ptr offset = (file_ptr) o->output_offset;
11160 bfd_size_type todo = o->size;
11161
11162 offset *= bfd_octets_per_byte (output_bfd);
11163
11164 if ((o->flags & SEC_ELF_REVERSE_COPY))
11165 {
11166 /* Reverse-copy input section to output. */
11167 do
11168 {
11169 todo -= address_size;
11170 if (! bfd_set_section_contents (output_bfd,
11171 o->output_section,
11172 contents + todo,
11173 offset,
11174 address_size))
11175 return FALSE;
11176 if (todo == 0)
11177 break;
11178 offset += address_size;
11179 }
11180 while (1);
11181 }
11182 else if (! bfd_set_section_contents (output_bfd,
11183 o->output_section,
11184 contents,
11185 offset, todo))
11186 return FALSE;
11187 }
11188 }
11189 break;
11190 }
11191 }
11192
11193 return TRUE;
11194 }
11195
11196 /* Generate a reloc when linking an ELF file. This is a reloc
11197 requested by the linker, and does not come from any input file. This
11198 is used to build constructor and destructor tables when linking
11199 with -Ur. */
11200
11201 static bfd_boolean
11202 elf_reloc_link_order (bfd *output_bfd,
11203 struct bfd_link_info *info,
11204 asection *output_section,
11205 struct bfd_link_order *link_order)
11206 {
11207 reloc_howto_type *howto;
11208 long indx;
11209 bfd_vma offset;
11210 bfd_vma addend;
11211 struct bfd_elf_section_reloc_data *reldata;
11212 struct elf_link_hash_entry **rel_hash_ptr;
11213 Elf_Internal_Shdr *rel_hdr;
11214 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11215 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11216 bfd_byte *erel;
11217 unsigned int i;
11218 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11219
11220 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11221 if (howto == NULL)
11222 {
11223 bfd_set_error (bfd_error_bad_value);
11224 return FALSE;
11225 }
11226
11227 addend = link_order->u.reloc.p->addend;
11228
11229 if (esdo->rel.hdr)
11230 reldata = &esdo->rel;
11231 else if (esdo->rela.hdr)
11232 reldata = &esdo->rela;
11233 else
11234 {
11235 reldata = NULL;
11236 BFD_ASSERT (0);
11237 }
11238
11239 /* Figure out the symbol index. */
11240 rel_hash_ptr = reldata->hashes + reldata->count;
11241 if (link_order->type == bfd_section_reloc_link_order)
11242 {
11243 indx = link_order->u.reloc.p->u.section->target_index;
11244 BFD_ASSERT (indx != 0);
11245 *rel_hash_ptr = NULL;
11246 }
11247 else
11248 {
11249 struct elf_link_hash_entry *h;
11250
11251 /* Treat a reloc against a defined symbol as though it were
11252 actually against the section. */
11253 h = ((struct elf_link_hash_entry *)
11254 bfd_wrapped_link_hash_lookup (output_bfd, info,
11255 link_order->u.reloc.p->u.name,
11256 FALSE, FALSE, TRUE));
11257 if (h != NULL
11258 && (h->root.type == bfd_link_hash_defined
11259 || h->root.type == bfd_link_hash_defweak))
11260 {
11261 asection *section;
11262
11263 section = h->root.u.def.section;
11264 indx = section->output_section->target_index;
11265 *rel_hash_ptr = NULL;
11266 /* It seems that we ought to add the symbol value to the
11267 addend here, but in practice it has already been added
11268 because it was passed to constructor_callback. */
11269 addend += section->output_section->vma + section->output_offset;
11270 }
11271 else if (h != NULL)
11272 {
11273 /* Setting the index to -2 tells elf_link_output_extsym that
11274 this symbol is used by a reloc. */
11275 h->indx = -2;
11276 *rel_hash_ptr = h;
11277 indx = 0;
11278 }
11279 else
11280 {
11281 (*info->callbacks->unattached_reloc)
11282 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11283 indx = 0;
11284 }
11285 }
11286
11287 /* If this is an inplace reloc, we must write the addend into the
11288 object file. */
11289 if (howto->partial_inplace && addend != 0)
11290 {
11291 bfd_size_type size;
11292 bfd_reloc_status_type rstat;
11293 bfd_byte *buf;
11294 bfd_boolean ok;
11295 const char *sym_name;
11296
11297 size = (bfd_size_type) bfd_get_reloc_size (howto);
11298 buf = (bfd_byte *) bfd_zmalloc (size);
11299 if (buf == NULL && size != 0)
11300 return FALSE;
11301 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11302 switch (rstat)
11303 {
11304 case bfd_reloc_ok:
11305 break;
11306
11307 default:
11308 case bfd_reloc_outofrange:
11309 abort ();
11310
11311 case bfd_reloc_overflow:
11312 if (link_order->type == bfd_section_reloc_link_order)
11313 sym_name = bfd_section_name (output_bfd,
11314 link_order->u.reloc.p->u.section);
11315 else
11316 sym_name = link_order->u.reloc.p->u.name;
11317 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11318 howto->name, addend, NULL, NULL,
11319 (bfd_vma) 0);
11320 break;
11321 }
11322
11323 ok = bfd_set_section_contents (output_bfd, output_section, buf,
11324 link_order->offset
11325 * bfd_octets_per_byte (output_bfd),
11326 size);
11327 free (buf);
11328 if (! ok)
11329 return FALSE;
11330 }
11331
11332 /* The address of a reloc is relative to the section in a
11333 relocatable file, and is a virtual address in an executable
11334 file. */
11335 offset = link_order->offset;
11336 if (! bfd_link_relocatable (info))
11337 offset += output_section->vma;
11338
11339 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11340 {
11341 irel[i].r_offset = offset;
11342 irel[i].r_info = 0;
11343 irel[i].r_addend = 0;
11344 }
11345 if (bed->s->arch_size == 32)
11346 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11347 else
11348 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11349
11350 rel_hdr = reldata->hdr;
11351 erel = rel_hdr->contents;
11352 if (rel_hdr->sh_type == SHT_REL)
11353 {
11354 erel += reldata->count * bed->s->sizeof_rel;
11355 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11356 }
11357 else
11358 {
11359 irel[0].r_addend = addend;
11360 erel += reldata->count * bed->s->sizeof_rela;
11361 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11362 }
11363
11364 ++reldata->count;
11365
11366 return TRUE;
11367 }
11368
11369
11370 /* Get the output vma of the section pointed to by the sh_link field. */
11371
11372 static bfd_vma
11373 elf_get_linked_section_vma (struct bfd_link_order *p)
11374 {
11375 Elf_Internal_Shdr **elf_shdrp;
11376 asection *s;
11377 int elfsec;
11378
11379 s = p->u.indirect.section;
11380 elf_shdrp = elf_elfsections (s->owner);
11381 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
11382 elfsec = elf_shdrp[elfsec]->sh_link;
11383 /* PR 290:
11384 The Intel C compiler generates SHT_IA_64_UNWIND with
11385 SHF_LINK_ORDER. But it doesn't set the sh_link or
11386 sh_info fields. Hence we could get the situation
11387 where elfsec is 0. */
11388 if (elfsec == 0)
11389 {
11390 const struct elf_backend_data *bed
11391 = get_elf_backend_data (s->owner);
11392 if (bed->link_order_error_handler)
11393 bed->link_order_error_handler
11394 /* xgettext:c-format */
11395 (_("%pB: warning: sh_link not set for section `%pA'"), s->owner, s);
11396 return 0;
11397 }
11398 else
11399 {
11400 s = elf_shdrp[elfsec]->bfd_section;
11401 return s->output_section->vma + s->output_offset;
11402 }
11403 }
11404
11405
11406 /* Compare two sections based on the locations of the sections they are
11407 linked to. Used by elf_fixup_link_order. */
11408
11409 static int
11410 compare_link_order (const void * a, const void * b)
11411 {
11412 bfd_vma apos;
11413 bfd_vma bpos;
11414
11415 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11416 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11417 if (apos < bpos)
11418 return -1;
11419 return apos > bpos;
11420 }
11421
11422
11423 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11424 order as their linked sections. Returns false if this could not be done
11425 because an output section includes both ordered and unordered
11426 sections. Ideally we'd do this in the linker proper. */
11427
11428 static bfd_boolean
11429 elf_fixup_link_order (bfd *abfd, asection *o)
11430 {
11431 int seen_linkorder;
11432 int seen_other;
11433 int n;
11434 struct bfd_link_order *p;
11435 bfd *sub;
11436 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11437 unsigned elfsec;
11438 struct bfd_link_order **sections;
11439 asection *s, *other_sec, *linkorder_sec;
11440 bfd_vma offset;
11441
11442 other_sec = NULL;
11443 linkorder_sec = NULL;
11444 seen_other = 0;
11445 seen_linkorder = 0;
11446 for (p = o->map_head.link_order; p != NULL; p = p->next)
11447 {
11448 if (p->type == bfd_indirect_link_order)
11449 {
11450 s = p->u.indirect.section;
11451 sub = s->owner;
11452 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11453 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
11454 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11455 && elfsec < elf_numsections (sub)
11456 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11457 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
11458 {
11459 seen_linkorder++;
11460 linkorder_sec = s;
11461 }
11462 else
11463 {
11464 seen_other++;
11465 other_sec = s;
11466 }
11467 }
11468 else
11469 seen_other++;
11470
11471 if (seen_other && seen_linkorder)
11472 {
11473 if (other_sec && linkorder_sec)
11474 _bfd_error_handler
11475 /* xgettext:c-format */
11476 (_("%pA has both ordered [`%pA' in %pB] "
11477 "and unordered [`%pA' in %pB] sections"),
11478 o, linkorder_sec, linkorder_sec->owner,
11479 other_sec, other_sec->owner);
11480 else
11481 _bfd_error_handler
11482 (_("%pA has both ordered and unordered sections"), o);
11483 bfd_set_error (bfd_error_bad_value);
11484 return FALSE;
11485 }
11486 }
11487
11488 if (!seen_linkorder)
11489 return TRUE;
11490
11491 sections = (struct bfd_link_order **)
11492 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11493 if (sections == NULL)
11494 return FALSE;
11495 seen_linkorder = 0;
11496
11497 for (p = o->map_head.link_order; p != NULL; p = p->next)
11498 {
11499 sections[seen_linkorder++] = p;
11500 }
11501 /* Sort the input sections in the order of their linked section. */
11502 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11503 compare_link_order);
11504
11505 /* Change the offsets of the sections. */
11506 offset = 0;
11507 for (n = 0; n < seen_linkorder; n++)
11508 {
11509 s = sections[n]->u.indirect.section;
11510 offset &= ~(bfd_vma) 0 << s->alignment_power;
11511 s->output_offset = offset / bfd_octets_per_byte (abfd);
11512 sections[n]->offset = offset;
11513 offset += sections[n]->size;
11514 }
11515
11516 free (sections);
11517 return TRUE;
11518 }
11519
11520 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11521 Returns TRUE upon success, FALSE otherwise. */
11522
11523 static bfd_boolean
11524 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11525 {
11526 bfd_boolean ret = FALSE;
11527 bfd *implib_bfd;
11528 const struct elf_backend_data *bed;
11529 flagword flags;
11530 enum bfd_architecture arch;
11531 unsigned int mach;
11532 asymbol **sympp = NULL;
11533 long symsize;
11534 long symcount;
11535 long src_count;
11536 elf_symbol_type *osymbuf;
11537
11538 implib_bfd = info->out_implib_bfd;
11539 bed = get_elf_backend_data (abfd);
11540
11541 if (!bfd_set_format (implib_bfd, bfd_object))
11542 return FALSE;
11543
11544 /* Use flag from executable but make it a relocatable object. */
11545 flags = bfd_get_file_flags (abfd);
11546 flags &= ~HAS_RELOC;
11547 if (!bfd_set_start_address (implib_bfd, 0)
11548 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
11549 return FALSE;
11550
11551 /* Copy architecture of output file to import library file. */
11552 arch = bfd_get_arch (abfd);
11553 mach = bfd_get_mach (abfd);
11554 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11555 && (abfd->target_defaulted
11556 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11557 return FALSE;
11558
11559 /* Get symbol table size. */
11560 symsize = bfd_get_symtab_upper_bound (abfd);
11561 if (symsize < 0)
11562 return FALSE;
11563
11564 /* Read in the symbol table. */
11565 sympp = (asymbol **) xmalloc (symsize);
11566 symcount = bfd_canonicalize_symtab (abfd, sympp);
11567 if (symcount < 0)
11568 goto free_sym_buf;
11569
11570 /* Allow the BFD backend to copy any private header data it
11571 understands from the output BFD to the import library BFD. */
11572 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11573 goto free_sym_buf;
11574
11575 /* Filter symbols to appear in the import library. */
11576 if (bed->elf_backend_filter_implib_symbols)
11577 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11578 symcount);
11579 else
11580 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11581 if (symcount == 0)
11582 {
11583 bfd_set_error (bfd_error_no_symbols);
11584 _bfd_error_handler (_("%pB: no symbol found for import library"),
11585 implib_bfd);
11586 goto free_sym_buf;
11587 }
11588
11589
11590 /* Make symbols absolute. */
11591 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11592 sizeof (*osymbuf));
11593 for (src_count = 0; src_count < symcount; src_count++)
11594 {
11595 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11596 sizeof (*osymbuf));
11597 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11598 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11599 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11600 osymbuf[src_count].internal_elf_sym.st_value =
11601 osymbuf[src_count].symbol.value;
11602 sympp[src_count] = &osymbuf[src_count].symbol;
11603 }
11604
11605 bfd_set_symtab (implib_bfd, sympp, symcount);
11606
11607 /* Allow the BFD backend to copy any private data it understands
11608 from the output BFD to the import library BFD. This is done last
11609 to permit the routine to look at the filtered symbol table. */
11610 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11611 goto free_sym_buf;
11612
11613 if (!bfd_close (implib_bfd))
11614 goto free_sym_buf;
11615
11616 ret = TRUE;
11617
11618 free_sym_buf:
11619 free (sympp);
11620 return ret;
11621 }
11622
11623 static void
11624 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11625 {
11626 asection *o;
11627
11628 if (flinfo->symstrtab != NULL)
11629 _bfd_elf_strtab_free (flinfo->symstrtab);
11630 if (flinfo->contents != NULL)
11631 free (flinfo->contents);
11632 if (flinfo->external_relocs != NULL)
11633 free (flinfo->external_relocs);
11634 if (flinfo->internal_relocs != NULL)
11635 free (flinfo->internal_relocs);
11636 if (flinfo->external_syms != NULL)
11637 free (flinfo->external_syms);
11638 if (flinfo->locsym_shndx != NULL)
11639 free (flinfo->locsym_shndx);
11640 if (flinfo->internal_syms != NULL)
11641 free (flinfo->internal_syms);
11642 if (flinfo->indices != NULL)
11643 free (flinfo->indices);
11644 if (flinfo->sections != NULL)
11645 free (flinfo->sections);
11646 if (flinfo->symshndxbuf != NULL)
11647 free (flinfo->symshndxbuf);
11648 for (o = obfd->sections; o != NULL; o = o->next)
11649 {
11650 struct bfd_elf_section_data *esdo = elf_section_data (o);
11651 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11652 free (esdo->rel.hashes);
11653 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11654 free (esdo->rela.hashes);
11655 }
11656 }
11657
11658 /* Do the final step of an ELF link. */
11659
11660 bfd_boolean
11661 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11662 {
11663 bfd_boolean dynamic;
11664 bfd_boolean emit_relocs;
11665 bfd *dynobj;
11666 struct elf_final_link_info flinfo;
11667 asection *o;
11668 struct bfd_link_order *p;
11669 bfd *sub;
11670 bfd_size_type max_contents_size;
11671 bfd_size_type max_external_reloc_size;
11672 bfd_size_type max_internal_reloc_count;
11673 bfd_size_type max_sym_count;
11674 bfd_size_type max_sym_shndx_count;
11675 Elf_Internal_Sym elfsym;
11676 unsigned int i;
11677 Elf_Internal_Shdr *symtab_hdr;
11678 Elf_Internal_Shdr *symtab_shndx_hdr;
11679 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11680 struct elf_outext_info eoinfo;
11681 bfd_boolean merged;
11682 size_t relativecount = 0;
11683 asection *reldyn = 0;
11684 bfd_size_type amt;
11685 asection *attr_section = NULL;
11686 bfd_vma attr_size = 0;
11687 const char *std_attrs_section;
11688 struct elf_link_hash_table *htab = elf_hash_table (info);
11689
11690 if (!is_elf_hash_table (htab))
11691 return FALSE;
11692
11693 if (bfd_link_pic (info))
11694 abfd->flags |= DYNAMIC;
11695
11696 dynamic = htab->dynamic_sections_created;
11697 dynobj = htab->dynobj;
11698
11699 emit_relocs = (bfd_link_relocatable (info)
11700 || info->emitrelocations);
11701
11702 flinfo.info = info;
11703 flinfo.output_bfd = abfd;
11704 flinfo.symstrtab = _bfd_elf_strtab_init ();
11705 if (flinfo.symstrtab == NULL)
11706 return FALSE;
11707
11708 if (! dynamic)
11709 {
11710 flinfo.hash_sec = NULL;
11711 flinfo.symver_sec = NULL;
11712 }
11713 else
11714 {
11715 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11716 /* Note that dynsym_sec can be NULL (on VMS). */
11717 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11718 /* Note that it is OK if symver_sec is NULL. */
11719 }
11720
11721 flinfo.contents = NULL;
11722 flinfo.external_relocs = NULL;
11723 flinfo.internal_relocs = NULL;
11724 flinfo.external_syms = NULL;
11725 flinfo.locsym_shndx = NULL;
11726 flinfo.internal_syms = NULL;
11727 flinfo.indices = NULL;
11728 flinfo.sections = NULL;
11729 flinfo.symshndxbuf = NULL;
11730 flinfo.filesym_count = 0;
11731
11732 /* The object attributes have been merged. Remove the input
11733 sections from the link, and set the contents of the output
11734 secton. */
11735 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11736 for (o = abfd->sections; o != NULL; o = o->next)
11737 {
11738 bfd_boolean remove_section = FALSE;
11739
11740 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11741 || strcmp (o->name, ".gnu.attributes") == 0)
11742 {
11743 for (p = o->map_head.link_order; p != NULL; p = p->next)
11744 {
11745 asection *input_section;
11746
11747 if (p->type != bfd_indirect_link_order)
11748 continue;
11749 input_section = p->u.indirect.section;
11750 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11751 elf_link_input_bfd ignores this section. */
11752 input_section->flags &= ~SEC_HAS_CONTENTS;
11753 }
11754
11755 attr_size = bfd_elf_obj_attr_size (abfd);
11756 bfd_set_section_size (abfd, o, attr_size);
11757 /* Skip this section later on. */
11758 o->map_head.link_order = NULL;
11759 if (attr_size)
11760 attr_section = o;
11761 else
11762 remove_section = TRUE;
11763 }
11764 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
11765 {
11766 /* Remove empty group section from linker output. */
11767 remove_section = TRUE;
11768 }
11769 if (remove_section)
11770 {
11771 o->flags |= SEC_EXCLUDE;
11772 bfd_section_list_remove (abfd, o);
11773 abfd->section_count--;
11774 }
11775 }
11776
11777 /* Count up the number of relocations we will output for each output
11778 section, so that we know the sizes of the reloc sections. We
11779 also figure out some maximum sizes. */
11780 max_contents_size = 0;
11781 max_external_reloc_size = 0;
11782 max_internal_reloc_count = 0;
11783 max_sym_count = 0;
11784 max_sym_shndx_count = 0;
11785 merged = FALSE;
11786 for (o = abfd->sections; o != NULL; o = o->next)
11787 {
11788 struct bfd_elf_section_data *esdo = elf_section_data (o);
11789 o->reloc_count = 0;
11790
11791 for (p = o->map_head.link_order; p != NULL; p = p->next)
11792 {
11793 unsigned int reloc_count = 0;
11794 unsigned int additional_reloc_count = 0;
11795 struct bfd_elf_section_data *esdi = NULL;
11796
11797 if (p->type == bfd_section_reloc_link_order
11798 || p->type == bfd_symbol_reloc_link_order)
11799 reloc_count = 1;
11800 else if (p->type == bfd_indirect_link_order)
11801 {
11802 asection *sec;
11803
11804 sec = p->u.indirect.section;
11805
11806 /* Mark all sections which are to be included in the
11807 link. This will normally be every section. We need
11808 to do this so that we can identify any sections which
11809 the linker has decided to not include. */
11810 sec->linker_mark = TRUE;
11811
11812 if (sec->flags & SEC_MERGE)
11813 merged = TRUE;
11814
11815 if (sec->rawsize > max_contents_size)
11816 max_contents_size = sec->rawsize;
11817 if (sec->size > max_contents_size)
11818 max_contents_size = sec->size;
11819
11820 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11821 && (sec->owner->flags & DYNAMIC) == 0)
11822 {
11823 size_t sym_count;
11824
11825 /* We are interested in just local symbols, not all
11826 symbols. */
11827 if (elf_bad_symtab (sec->owner))
11828 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11829 / bed->s->sizeof_sym);
11830 else
11831 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11832
11833 if (sym_count > max_sym_count)
11834 max_sym_count = sym_count;
11835
11836 if (sym_count > max_sym_shndx_count
11837 && elf_symtab_shndx_list (sec->owner) != NULL)
11838 max_sym_shndx_count = sym_count;
11839
11840 if (esdo->this_hdr.sh_type == SHT_REL
11841 || esdo->this_hdr.sh_type == SHT_RELA)
11842 /* Some backends use reloc_count in relocation sections
11843 to count particular types of relocs. Of course,
11844 reloc sections themselves can't have relocations. */
11845 ;
11846 else if (emit_relocs)
11847 {
11848 reloc_count = sec->reloc_count;
11849 if (bed->elf_backend_count_additional_relocs)
11850 {
11851 int c;
11852 c = (*bed->elf_backend_count_additional_relocs) (sec);
11853 additional_reloc_count += c;
11854 }
11855 }
11856 else if (bed->elf_backend_count_relocs)
11857 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11858
11859 esdi = elf_section_data (sec);
11860
11861 if ((sec->flags & SEC_RELOC) != 0)
11862 {
11863 size_t ext_size = 0;
11864
11865 if (esdi->rel.hdr != NULL)
11866 ext_size = esdi->rel.hdr->sh_size;
11867 if (esdi->rela.hdr != NULL)
11868 ext_size += esdi->rela.hdr->sh_size;
11869
11870 if (ext_size > max_external_reloc_size)
11871 max_external_reloc_size = ext_size;
11872 if (sec->reloc_count > max_internal_reloc_count)
11873 max_internal_reloc_count = sec->reloc_count;
11874 }
11875 }
11876 }
11877
11878 if (reloc_count == 0)
11879 continue;
11880
11881 reloc_count += additional_reloc_count;
11882 o->reloc_count += reloc_count;
11883
11884 if (p->type == bfd_indirect_link_order && emit_relocs)
11885 {
11886 if (esdi->rel.hdr)
11887 {
11888 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11889 esdo->rel.count += additional_reloc_count;
11890 }
11891 if (esdi->rela.hdr)
11892 {
11893 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11894 esdo->rela.count += additional_reloc_count;
11895 }
11896 }
11897 else
11898 {
11899 if (o->use_rela_p)
11900 esdo->rela.count += reloc_count;
11901 else
11902 esdo->rel.count += reloc_count;
11903 }
11904 }
11905
11906 if (o->reloc_count > 0)
11907 o->flags |= SEC_RELOC;
11908 else
11909 {
11910 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11911 set it (this is probably a bug) and if it is set
11912 assign_section_numbers will create a reloc section. */
11913 o->flags &=~ SEC_RELOC;
11914 }
11915
11916 /* If the SEC_ALLOC flag is not set, force the section VMA to
11917 zero. This is done in elf_fake_sections as well, but forcing
11918 the VMA to 0 here will ensure that relocs against these
11919 sections are handled correctly. */
11920 if ((o->flags & SEC_ALLOC) == 0
11921 && ! o->user_set_vma)
11922 o->vma = 0;
11923 }
11924
11925 if (! bfd_link_relocatable (info) && merged)
11926 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
11927
11928 /* Figure out the file positions for everything but the symbol table
11929 and the relocs. We set symcount to force assign_section_numbers
11930 to create a symbol table. */
11931 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
11932 BFD_ASSERT (! abfd->output_has_begun);
11933 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11934 goto error_return;
11935
11936 /* Set sizes, and assign file positions for reloc sections. */
11937 for (o = abfd->sections; o != NULL; o = o->next)
11938 {
11939 struct bfd_elf_section_data *esdo = elf_section_data (o);
11940 if ((o->flags & SEC_RELOC) != 0)
11941 {
11942 if (esdo->rel.hdr
11943 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
11944 goto error_return;
11945
11946 if (esdo->rela.hdr
11947 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
11948 goto error_return;
11949 }
11950
11951 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11952 to count upwards while actually outputting the relocations. */
11953 esdo->rel.count = 0;
11954 esdo->rela.count = 0;
11955
11956 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11957 {
11958 /* Cache the section contents so that they can be compressed
11959 later. Use bfd_malloc since it will be freed by
11960 bfd_compress_section_contents. */
11961 unsigned char *contents = esdo->this_hdr.contents;
11962 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11963 abort ();
11964 contents
11965 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11966 if (contents == NULL)
11967 goto error_return;
11968 esdo->this_hdr.contents = contents;
11969 }
11970 }
11971
11972 /* We have now assigned file positions for all the sections except
11973 .symtab, .strtab, and non-loaded reloc sections. We start the
11974 .symtab section at the current file position, and write directly
11975 to it. We build the .strtab section in memory. */
11976 bfd_get_symcount (abfd) = 0;
11977 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11978 /* sh_name is set in prep_headers. */
11979 symtab_hdr->sh_type = SHT_SYMTAB;
11980 /* sh_flags, sh_addr and sh_size all start off zero. */
11981 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11982 /* sh_link is set in assign_section_numbers. */
11983 /* sh_info is set below. */
11984 /* sh_offset is set just below. */
11985 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11986
11987 if (max_sym_count < 20)
11988 max_sym_count = 20;
11989 htab->strtabsize = max_sym_count;
11990 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11991 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
11992 if (htab->strtab == NULL)
11993 goto error_return;
11994 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11995 flinfo.symshndxbuf
11996 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11997 ? (Elf_External_Sym_Shndx *) -1 : NULL);
11998
11999 if (info->strip != strip_all || emit_relocs)
12000 {
12001 file_ptr off = elf_next_file_pos (abfd);
12002
12003 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
12004
12005 /* Note that at this point elf_next_file_pos (abfd) is
12006 incorrect. We do not yet know the size of the .symtab section.
12007 We correct next_file_pos below, after we do know the size. */
12008
12009 /* Start writing out the symbol table. The first symbol is always a
12010 dummy symbol. */
12011 elfsym.st_value = 0;
12012 elfsym.st_size = 0;
12013 elfsym.st_info = 0;
12014 elfsym.st_other = 0;
12015 elfsym.st_shndx = SHN_UNDEF;
12016 elfsym.st_target_internal = 0;
12017 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12018 bfd_und_section_ptr, NULL) != 1)
12019 goto error_return;
12020
12021 /* Output a symbol for each section. We output these even if we are
12022 discarding local symbols, since they are used for relocs. These
12023 symbols have no names. We store the index of each one in the
12024 index field of the section, so that we can find it again when
12025 outputting relocs. */
12026
12027 elfsym.st_size = 0;
12028 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12029 elfsym.st_other = 0;
12030 elfsym.st_value = 0;
12031 elfsym.st_target_internal = 0;
12032 for (i = 1; i < elf_numsections (abfd); i++)
12033 {
12034 o = bfd_section_from_elf_index (abfd, i);
12035 if (o != NULL)
12036 {
12037 o->target_index = bfd_get_symcount (abfd);
12038 elfsym.st_shndx = i;
12039 if (!bfd_link_relocatable (info))
12040 elfsym.st_value = o->vma;
12041 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
12042 NULL) != 1)
12043 goto error_return;
12044 }
12045 }
12046 }
12047
12048 /* Allocate some memory to hold information read in from the input
12049 files. */
12050 if (max_contents_size != 0)
12051 {
12052 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12053 if (flinfo.contents == NULL)
12054 goto error_return;
12055 }
12056
12057 if (max_external_reloc_size != 0)
12058 {
12059 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12060 if (flinfo.external_relocs == NULL)
12061 goto error_return;
12062 }
12063
12064 if (max_internal_reloc_count != 0)
12065 {
12066 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12067 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12068 if (flinfo.internal_relocs == NULL)
12069 goto error_return;
12070 }
12071
12072 if (max_sym_count != 0)
12073 {
12074 amt = max_sym_count * bed->s->sizeof_sym;
12075 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12076 if (flinfo.external_syms == NULL)
12077 goto error_return;
12078
12079 amt = max_sym_count * sizeof (Elf_Internal_Sym);
12080 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12081 if (flinfo.internal_syms == NULL)
12082 goto error_return;
12083
12084 amt = max_sym_count * sizeof (long);
12085 flinfo.indices = (long int *) bfd_malloc (amt);
12086 if (flinfo.indices == NULL)
12087 goto error_return;
12088
12089 amt = max_sym_count * sizeof (asection *);
12090 flinfo.sections = (asection **) bfd_malloc (amt);
12091 if (flinfo.sections == NULL)
12092 goto error_return;
12093 }
12094
12095 if (max_sym_shndx_count != 0)
12096 {
12097 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
12098 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12099 if (flinfo.locsym_shndx == NULL)
12100 goto error_return;
12101 }
12102
12103 if (htab->tls_sec)
12104 {
12105 bfd_vma base, end = 0;
12106 asection *sec;
12107
12108 for (sec = htab->tls_sec;
12109 sec && (sec->flags & SEC_THREAD_LOCAL);
12110 sec = sec->next)
12111 {
12112 bfd_size_type size = sec->size;
12113
12114 if (size == 0
12115 && (sec->flags & SEC_HAS_CONTENTS) == 0)
12116 {
12117 struct bfd_link_order *ord = sec->map_tail.link_order;
12118
12119 if (ord != NULL)
12120 size = ord->offset + ord->size;
12121 }
12122 end = sec->vma + size;
12123 }
12124 base = htab->tls_sec->vma;
12125 /* Only align end of TLS section if static TLS doesn't have special
12126 alignment requirements. */
12127 if (bed->static_tls_alignment == 1)
12128 end = align_power (end, htab->tls_sec->alignment_power);
12129 htab->tls_size = end - base;
12130 }
12131
12132 /* Reorder SHF_LINK_ORDER sections. */
12133 for (o = abfd->sections; o != NULL; o = o->next)
12134 {
12135 if (!elf_fixup_link_order (abfd, o))
12136 return FALSE;
12137 }
12138
12139 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12140 return FALSE;
12141
12142 /* Since ELF permits relocations to be against local symbols, we
12143 must have the local symbols available when we do the relocations.
12144 Since we would rather only read the local symbols once, and we
12145 would rather not keep them in memory, we handle all the
12146 relocations for a single input file at the same time.
12147
12148 Unfortunately, there is no way to know the total number of local
12149 symbols until we have seen all of them, and the local symbol
12150 indices precede the global symbol indices. This means that when
12151 we are generating relocatable output, and we see a reloc against
12152 a global symbol, we can not know the symbol index until we have
12153 finished examining all the local symbols to see which ones we are
12154 going to output. To deal with this, we keep the relocations in
12155 memory, and don't output them until the end of the link. This is
12156 an unfortunate waste of memory, but I don't see a good way around
12157 it. Fortunately, it only happens when performing a relocatable
12158 link, which is not the common case. FIXME: If keep_memory is set
12159 we could write the relocs out and then read them again; I don't
12160 know how bad the memory loss will be. */
12161
12162 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12163 sub->output_has_begun = FALSE;
12164 for (o = abfd->sections; o != NULL; o = o->next)
12165 {
12166 for (p = o->map_head.link_order; p != NULL; p = p->next)
12167 {
12168 if (p->type == bfd_indirect_link_order
12169 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12170 == bfd_target_elf_flavour)
12171 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12172 {
12173 if (! sub->output_has_begun)
12174 {
12175 if (! elf_link_input_bfd (&flinfo, sub))
12176 goto error_return;
12177 sub->output_has_begun = TRUE;
12178 }
12179 }
12180 else if (p->type == bfd_section_reloc_link_order
12181 || p->type == bfd_symbol_reloc_link_order)
12182 {
12183 if (! elf_reloc_link_order (abfd, info, o, p))
12184 goto error_return;
12185 }
12186 else
12187 {
12188 if (! _bfd_default_link_order (abfd, info, o, p))
12189 {
12190 if (p->type == bfd_indirect_link_order
12191 && (bfd_get_flavour (sub)
12192 == bfd_target_elf_flavour)
12193 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12194 != bed->s->elfclass))
12195 {
12196 const char *iclass, *oclass;
12197
12198 switch (bed->s->elfclass)
12199 {
12200 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12201 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12202 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12203 default: abort ();
12204 }
12205
12206 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12207 {
12208 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12209 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12210 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12211 default: abort ();
12212 }
12213
12214 bfd_set_error (bfd_error_wrong_format);
12215 _bfd_error_handler
12216 /* xgettext:c-format */
12217 (_("%pB: file class %s incompatible with %s"),
12218 sub, iclass, oclass);
12219 }
12220
12221 goto error_return;
12222 }
12223 }
12224 }
12225 }
12226
12227 /* Free symbol buffer if needed. */
12228 if (!info->reduce_memory_overheads)
12229 {
12230 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12231 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
12232 && elf_tdata (sub)->symbuf)
12233 {
12234 free (elf_tdata (sub)->symbuf);
12235 elf_tdata (sub)->symbuf = NULL;
12236 }
12237 }
12238
12239 /* Output any global symbols that got converted to local in a
12240 version script or due to symbol visibility. We do this in a
12241 separate step since ELF requires all local symbols to appear
12242 prior to any global symbols. FIXME: We should only do this if
12243 some global symbols were, in fact, converted to become local.
12244 FIXME: Will this work correctly with the Irix 5 linker? */
12245 eoinfo.failed = FALSE;
12246 eoinfo.flinfo = &flinfo;
12247 eoinfo.localsyms = TRUE;
12248 eoinfo.file_sym_done = FALSE;
12249 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12250 if (eoinfo.failed)
12251 return FALSE;
12252
12253 /* If backend needs to output some local symbols not present in the hash
12254 table, do it now. */
12255 if (bed->elf_backend_output_arch_local_syms
12256 && (info->strip != strip_all || emit_relocs))
12257 {
12258 typedef int (*out_sym_func)
12259 (void *, const char *, Elf_Internal_Sym *, asection *,
12260 struct elf_link_hash_entry *);
12261
12262 if (! ((*bed->elf_backend_output_arch_local_syms)
12263 (abfd, info, &flinfo,
12264 (out_sym_func) elf_link_output_symstrtab)))
12265 return FALSE;
12266 }
12267
12268 /* That wrote out all the local symbols. Finish up the symbol table
12269 with the global symbols. Even if we want to strip everything we
12270 can, we still need to deal with those global symbols that got
12271 converted to local in a version script. */
12272
12273 /* The sh_info field records the index of the first non local symbol. */
12274 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12275
12276 if (dynamic
12277 && htab->dynsym != NULL
12278 && htab->dynsym->output_section != bfd_abs_section_ptr)
12279 {
12280 Elf_Internal_Sym sym;
12281 bfd_byte *dynsym = htab->dynsym->contents;
12282
12283 o = htab->dynsym->output_section;
12284 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12285
12286 /* Write out the section symbols for the output sections. */
12287 if (bfd_link_pic (info)
12288 || htab->is_relocatable_executable)
12289 {
12290 asection *s;
12291
12292 sym.st_size = 0;
12293 sym.st_name = 0;
12294 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12295 sym.st_other = 0;
12296 sym.st_target_internal = 0;
12297
12298 for (s = abfd->sections; s != NULL; s = s->next)
12299 {
12300 int indx;
12301 bfd_byte *dest;
12302 long dynindx;
12303
12304 dynindx = elf_section_data (s)->dynindx;
12305 if (dynindx <= 0)
12306 continue;
12307 indx = elf_section_data (s)->this_idx;
12308 BFD_ASSERT (indx > 0);
12309 sym.st_shndx = indx;
12310 if (! check_dynsym (abfd, &sym))
12311 return FALSE;
12312 sym.st_value = s->vma;
12313 dest = dynsym + dynindx * bed->s->sizeof_sym;
12314 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12315 }
12316 }
12317
12318 /* Write out the local dynsyms. */
12319 if (htab->dynlocal)
12320 {
12321 struct elf_link_local_dynamic_entry *e;
12322 for (e = htab->dynlocal; e ; e = e->next)
12323 {
12324 asection *s;
12325 bfd_byte *dest;
12326
12327 /* Copy the internal symbol and turn off visibility.
12328 Note that we saved a word of storage and overwrote
12329 the original st_name with the dynstr_index. */
12330 sym = e->isym;
12331 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12332
12333 s = bfd_section_from_elf_index (e->input_bfd,
12334 e->isym.st_shndx);
12335 if (s != NULL)
12336 {
12337 sym.st_shndx =
12338 elf_section_data (s->output_section)->this_idx;
12339 if (! check_dynsym (abfd, &sym))
12340 return FALSE;
12341 sym.st_value = (s->output_section->vma
12342 + s->output_offset
12343 + e->isym.st_value);
12344 }
12345
12346 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12347 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12348 }
12349 }
12350 }
12351
12352 /* We get the global symbols from the hash table. */
12353 eoinfo.failed = FALSE;
12354 eoinfo.localsyms = FALSE;
12355 eoinfo.flinfo = &flinfo;
12356 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12357 if (eoinfo.failed)
12358 return FALSE;
12359
12360 /* If backend needs to output some symbols not present in the hash
12361 table, do it now. */
12362 if (bed->elf_backend_output_arch_syms
12363 && (info->strip != strip_all || emit_relocs))
12364 {
12365 typedef int (*out_sym_func)
12366 (void *, const char *, Elf_Internal_Sym *, asection *,
12367 struct elf_link_hash_entry *);
12368
12369 if (! ((*bed->elf_backend_output_arch_syms)
12370 (abfd, info, &flinfo,
12371 (out_sym_func) elf_link_output_symstrtab)))
12372 return FALSE;
12373 }
12374
12375 /* Finalize the .strtab section. */
12376 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12377
12378 /* Swap out the .strtab section. */
12379 if (!elf_link_swap_symbols_out (&flinfo))
12380 return FALSE;
12381
12382 /* Now we know the size of the symtab section. */
12383 if (bfd_get_symcount (abfd) > 0)
12384 {
12385 /* Finish up and write out the symbol string table (.strtab)
12386 section. */
12387 Elf_Internal_Shdr *symstrtab_hdr = NULL;
12388 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12389
12390 if (elf_symtab_shndx_list (abfd))
12391 {
12392 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12393
12394 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12395 {
12396 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12397 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12398 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12399 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12400 symtab_shndx_hdr->sh_size = amt;
12401
12402 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12403 off, TRUE);
12404
12405 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12406 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12407 return FALSE;
12408 }
12409 }
12410
12411 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12412 /* sh_name was set in prep_headers. */
12413 symstrtab_hdr->sh_type = SHT_STRTAB;
12414 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12415 symstrtab_hdr->sh_addr = 0;
12416 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12417 symstrtab_hdr->sh_entsize = 0;
12418 symstrtab_hdr->sh_link = 0;
12419 symstrtab_hdr->sh_info = 0;
12420 /* sh_offset is set just below. */
12421 symstrtab_hdr->sh_addralign = 1;
12422
12423 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12424 off, TRUE);
12425 elf_next_file_pos (abfd) = off;
12426
12427 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12428 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12429 return FALSE;
12430 }
12431
12432 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12433 {
12434 _bfd_error_handler (_("%pB: failed to generate import library"),
12435 info->out_implib_bfd);
12436 return FALSE;
12437 }
12438
12439 /* Adjust the relocs to have the correct symbol indices. */
12440 for (o = abfd->sections; o != NULL; o = o->next)
12441 {
12442 struct bfd_elf_section_data *esdo = elf_section_data (o);
12443 bfd_boolean sort;
12444
12445 if ((o->flags & SEC_RELOC) == 0)
12446 continue;
12447
12448 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12449 if (esdo->rel.hdr != NULL
12450 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
12451 return FALSE;
12452 if (esdo->rela.hdr != NULL
12453 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
12454 return FALSE;
12455
12456 /* Set the reloc_count field to 0 to prevent write_relocs from
12457 trying to swap the relocs out itself. */
12458 o->reloc_count = 0;
12459 }
12460
12461 if (dynamic && info->combreloc && dynobj != NULL)
12462 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12463
12464 /* If we are linking against a dynamic object, or generating a
12465 shared library, finish up the dynamic linking information. */
12466 if (dynamic)
12467 {
12468 bfd_byte *dyncon, *dynconend;
12469
12470 /* Fix up .dynamic entries. */
12471 o = bfd_get_linker_section (dynobj, ".dynamic");
12472 BFD_ASSERT (o != NULL);
12473
12474 dyncon = o->contents;
12475 dynconend = o->contents + o->size;
12476 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12477 {
12478 Elf_Internal_Dyn dyn;
12479 const char *name;
12480 unsigned int type;
12481 bfd_size_type sh_size;
12482 bfd_vma sh_addr;
12483
12484 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12485
12486 switch (dyn.d_tag)
12487 {
12488 default:
12489 continue;
12490 case DT_NULL:
12491 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12492 {
12493 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12494 {
12495 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12496 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12497 default: continue;
12498 }
12499 dyn.d_un.d_val = relativecount;
12500 relativecount = 0;
12501 break;
12502 }
12503 continue;
12504
12505 case DT_INIT:
12506 name = info->init_function;
12507 goto get_sym;
12508 case DT_FINI:
12509 name = info->fini_function;
12510 get_sym:
12511 {
12512 struct elf_link_hash_entry *h;
12513
12514 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
12515 if (h != NULL
12516 && (h->root.type == bfd_link_hash_defined
12517 || h->root.type == bfd_link_hash_defweak))
12518 {
12519 dyn.d_un.d_ptr = h->root.u.def.value;
12520 o = h->root.u.def.section;
12521 if (o->output_section != NULL)
12522 dyn.d_un.d_ptr += (o->output_section->vma
12523 + o->output_offset);
12524 else
12525 {
12526 /* The symbol is imported from another shared
12527 library and does not apply to this one. */
12528 dyn.d_un.d_ptr = 0;
12529 }
12530 break;
12531 }
12532 }
12533 continue;
12534
12535 case DT_PREINIT_ARRAYSZ:
12536 name = ".preinit_array";
12537 goto get_out_size;
12538 case DT_INIT_ARRAYSZ:
12539 name = ".init_array";
12540 goto get_out_size;
12541 case DT_FINI_ARRAYSZ:
12542 name = ".fini_array";
12543 get_out_size:
12544 o = bfd_get_section_by_name (abfd, name);
12545 if (o == NULL)
12546 {
12547 _bfd_error_handler
12548 (_("could not find section %s"), name);
12549 goto error_return;
12550 }
12551 if (o->size == 0)
12552 _bfd_error_handler
12553 (_("warning: %s section has zero size"), name);
12554 dyn.d_un.d_val = o->size;
12555 break;
12556
12557 case DT_PREINIT_ARRAY:
12558 name = ".preinit_array";
12559 goto get_out_vma;
12560 case DT_INIT_ARRAY:
12561 name = ".init_array";
12562 goto get_out_vma;
12563 case DT_FINI_ARRAY:
12564 name = ".fini_array";
12565 get_out_vma:
12566 o = bfd_get_section_by_name (abfd, name);
12567 goto do_vma;
12568
12569 case DT_HASH:
12570 name = ".hash";
12571 goto get_vma;
12572 case DT_GNU_HASH:
12573 name = ".gnu.hash";
12574 goto get_vma;
12575 case DT_STRTAB:
12576 name = ".dynstr";
12577 goto get_vma;
12578 case DT_SYMTAB:
12579 name = ".dynsym";
12580 goto get_vma;
12581 case DT_VERDEF:
12582 name = ".gnu.version_d";
12583 goto get_vma;
12584 case DT_VERNEED:
12585 name = ".gnu.version_r";
12586 goto get_vma;
12587 case DT_VERSYM:
12588 name = ".gnu.version";
12589 get_vma:
12590 o = bfd_get_linker_section (dynobj, name);
12591 do_vma:
12592 if (o == NULL || bfd_is_abs_section (o->output_section))
12593 {
12594 _bfd_error_handler
12595 (_("could not find section %s"), name);
12596 goto error_return;
12597 }
12598 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12599 {
12600 _bfd_error_handler
12601 (_("warning: section '%s' is being made into a note"), name);
12602 bfd_set_error (bfd_error_nonrepresentable_section);
12603 goto error_return;
12604 }
12605 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
12606 break;
12607
12608 case DT_REL:
12609 case DT_RELA:
12610 case DT_RELSZ:
12611 case DT_RELASZ:
12612 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12613 type = SHT_REL;
12614 else
12615 type = SHT_RELA;
12616 sh_size = 0;
12617 sh_addr = 0;
12618 for (i = 1; i < elf_numsections (abfd); i++)
12619 {
12620 Elf_Internal_Shdr *hdr;
12621
12622 hdr = elf_elfsections (abfd)[i];
12623 if (hdr->sh_type == type
12624 && (hdr->sh_flags & SHF_ALLOC) != 0)
12625 {
12626 sh_size += hdr->sh_size;
12627 if (sh_addr == 0
12628 || sh_addr > hdr->sh_addr)
12629 sh_addr = hdr->sh_addr;
12630 }
12631 }
12632
12633 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12634 {
12635 /* Don't count procedure linkage table relocs in the
12636 overall reloc count. */
12637 sh_size -= htab->srelplt->size;
12638 if (sh_size == 0)
12639 /* If the size is zero, make the address zero too.
12640 This is to avoid a glibc bug. If the backend
12641 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12642 zero, then we'll put DT_RELA at the end of
12643 DT_JMPREL. glibc will interpret the end of
12644 DT_RELA matching the end of DT_JMPREL as the
12645 case where DT_RELA includes DT_JMPREL, and for
12646 LD_BIND_NOW will decide that processing DT_RELA
12647 will process the PLT relocs too. Net result:
12648 No PLT relocs applied. */
12649 sh_addr = 0;
12650
12651 /* If .rela.plt is the first .rela section, exclude
12652 it from DT_RELA. */
12653 else if (sh_addr == (htab->srelplt->output_section->vma
12654 + htab->srelplt->output_offset))
12655 sh_addr += htab->srelplt->size;
12656 }
12657
12658 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12659 dyn.d_un.d_val = sh_size;
12660 else
12661 dyn.d_un.d_ptr = sh_addr;
12662 break;
12663 }
12664 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12665 }
12666 }
12667
12668 /* If we have created any dynamic sections, then output them. */
12669 if (dynobj != NULL)
12670 {
12671 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12672 goto error_return;
12673
12674 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12675 if (((info->warn_shared_textrel && bfd_link_pic (info))
12676 || info->error_textrel)
12677 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
12678 {
12679 bfd_byte *dyncon, *dynconend;
12680
12681 dyncon = o->contents;
12682 dynconend = o->contents + o->size;
12683 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12684 {
12685 Elf_Internal_Dyn dyn;
12686
12687 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12688
12689 if (dyn.d_tag == DT_TEXTREL)
12690 {
12691 if (info->error_textrel)
12692 info->callbacks->einfo
12693 (_("%P%X: read-only segment has dynamic relocations\n"));
12694 else
12695 info->callbacks->einfo
12696 (_("%P: warning: creating a DT_TEXTREL in a shared object\n"));
12697 break;
12698 }
12699 }
12700 }
12701
12702 for (o = dynobj->sections; o != NULL; o = o->next)
12703 {
12704 if ((o->flags & SEC_HAS_CONTENTS) == 0
12705 || o->size == 0
12706 || o->output_section == bfd_abs_section_ptr)
12707 continue;
12708 if ((o->flags & SEC_LINKER_CREATED) == 0)
12709 {
12710 /* At this point, we are only interested in sections
12711 created by _bfd_elf_link_create_dynamic_sections. */
12712 continue;
12713 }
12714 if (htab->stab_info.stabstr == o)
12715 continue;
12716 if (htab->eh_info.hdr_sec == o)
12717 continue;
12718 if (strcmp (o->name, ".dynstr") != 0)
12719 {
12720 if (! bfd_set_section_contents (abfd, o->output_section,
12721 o->contents,
12722 (file_ptr) o->output_offset
12723 * bfd_octets_per_byte (abfd),
12724 o->size))
12725 goto error_return;
12726 }
12727 else
12728 {
12729 /* The contents of the .dynstr section are actually in a
12730 stringtab. */
12731 file_ptr off;
12732
12733 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12734 if (bfd_seek (abfd, off, SEEK_SET) != 0
12735 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
12736 goto error_return;
12737 }
12738 }
12739 }
12740
12741 if (!info->resolve_section_groups)
12742 {
12743 bfd_boolean failed = FALSE;
12744
12745 BFD_ASSERT (bfd_link_relocatable (info));
12746 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12747 if (failed)
12748 goto error_return;
12749 }
12750
12751 /* If we have optimized stabs strings, output them. */
12752 if (htab->stab_info.stabstr != NULL)
12753 {
12754 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
12755 goto error_return;
12756 }
12757
12758 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12759 goto error_return;
12760
12761 elf_final_link_free (abfd, &flinfo);
12762
12763 elf_linker (abfd) = TRUE;
12764
12765 if (attr_section)
12766 {
12767 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
12768 if (contents == NULL)
12769 return FALSE; /* Bail out and fail. */
12770 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12771 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12772 free (contents);
12773 }
12774
12775 return TRUE;
12776
12777 error_return:
12778 elf_final_link_free (abfd, &flinfo);
12779 return FALSE;
12780 }
12781 \f
12782 /* Initialize COOKIE for input bfd ABFD. */
12783
12784 static bfd_boolean
12785 init_reloc_cookie (struct elf_reloc_cookie *cookie,
12786 struct bfd_link_info *info, bfd *abfd)
12787 {
12788 Elf_Internal_Shdr *symtab_hdr;
12789 const struct elf_backend_data *bed;
12790
12791 bed = get_elf_backend_data (abfd);
12792 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12793
12794 cookie->abfd = abfd;
12795 cookie->sym_hashes = elf_sym_hashes (abfd);
12796 cookie->bad_symtab = elf_bad_symtab (abfd);
12797 if (cookie->bad_symtab)
12798 {
12799 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12800 cookie->extsymoff = 0;
12801 }
12802 else
12803 {
12804 cookie->locsymcount = symtab_hdr->sh_info;
12805 cookie->extsymoff = symtab_hdr->sh_info;
12806 }
12807
12808 if (bed->s->arch_size == 32)
12809 cookie->r_sym_shift = 8;
12810 else
12811 cookie->r_sym_shift = 32;
12812
12813 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12814 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12815 {
12816 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12817 cookie->locsymcount, 0,
12818 NULL, NULL, NULL);
12819 if (cookie->locsyms == NULL)
12820 {
12821 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12822 return FALSE;
12823 }
12824 if (info->keep_memory)
12825 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12826 }
12827 return TRUE;
12828 }
12829
12830 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12831
12832 static void
12833 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12834 {
12835 Elf_Internal_Shdr *symtab_hdr;
12836
12837 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12838 if (cookie->locsyms != NULL
12839 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12840 free (cookie->locsyms);
12841 }
12842
12843 /* Initialize the relocation information in COOKIE for input section SEC
12844 of input bfd ABFD. */
12845
12846 static bfd_boolean
12847 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12848 struct bfd_link_info *info, bfd *abfd,
12849 asection *sec)
12850 {
12851 if (sec->reloc_count == 0)
12852 {
12853 cookie->rels = NULL;
12854 cookie->relend = NULL;
12855 }
12856 else
12857 {
12858 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12859 info->keep_memory);
12860 if (cookie->rels == NULL)
12861 return FALSE;
12862 cookie->rel = cookie->rels;
12863 cookie->relend = cookie->rels + sec->reloc_count;
12864 }
12865 cookie->rel = cookie->rels;
12866 return TRUE;
12867 }
12868
12869 /* Free the memory allocated by init_reloc_cookie_rels,
12870 if appropriate. */
12871
12872 static void
12873 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12874 asection *sec)
12875 {
12876 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12877 free (cookie->rels);
12878 }
12879
12880 /* Initialize the whole of COOKIE for input section SEC. */
12881
12882 static bfd_boolean
12883 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12884 struct bfd_link_info *info,
12885 asection *sec)
12886 {
12887 if (!init_reloc_cookie (cookie, info, sec->owner))
12888 goto error1;
12889 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12890 goto error2;
12891 return TRUE;
12892
12893 error2:
12894 fini_reloc_cookie (cookie, sec->owner);
12895 error1:
12896 return FALSE;
12897 }
12898
12899 /* Free the memory allocated by init_reloc_cookie_for_section,
12900 if appropriate. */
12901
12902 static void
12903 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12904 asection *sec)
12905 {
12906 fini_reloc_cookie_rels (cookie, sec);
12907 fini_reloc_cookie (cookie, sec->owner);
12908 }
12909 \f
12910 /* Garbage collect unused sections. */
12911
12912 /* Default gc_mark_hook. */
12913
12914 asection *
12915 _bfd_elf_gc_mark_hook (asection *sec,
12916 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12917 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12918 struct elf_link_hash_entry *h,
12919 Elf_Internal_Sym *sym)
12920 {
12921 if (h != NULL)
12922 {
12923 switch (h->root.type)
12924 {
12925 case bfd_link_hash_defined:
12926 case bfd_link_hash_defweak:
12927 return h->root.u.def.section;
12928
12929 case bfd_link_hash_common:
12930 return h->root.u.c.p->section;
12931
12932 default:
12933 break;
12934 }
12935 }
12936 else
12937 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12938
12939 return NULL;
12940 }
12941
12942 /* Return the debug definition section. */
12943
12944 static asection *
12945 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
12946 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12947 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12948 struct elf_link_hash_entry *h,
12949 Elf_Internal_Sym *sym)
12950 {
12951 if (h != NULL)
12952 {
12953 /* Return the global debug definition section. */
12954 if ((h->root.type == bfd_link_hash_defined
12955 || h->root.type == bfd_link_hash_defweak)
12956 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
12957 return h->root.u.def.section;
12958 }
12959 else
12960 {
12961 /* Return the local debug definition section. */
12962 asection *isec = bfd_section_from_elf_index (sec->owner,
12963 sym->st_shndx);
12964 if ((isec->flags & SEC_DEBUGGING) != 0)
12965 return isec;
12966 }
12967
12968 return NULL;
12969 }
12970
12971 /* COOKIE->rel describes a relocation against section SEC, which is
12972 a section we've decided to keep. Return the section that contains
12973 the relocation symbol, or NULL if no section contains it. */
12974
12975 asection *
12976 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12977 elf_gc_mark_hook_fn gc_mark_hook,
12978 struct elf_reloc_cookie *cookie,
12979 bfd_boolean *start_stop)
12980 {
12981 unsigned long r_symndx;
12982 struct elf_link_hash_entry *h;
12983
12984 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
12985 if (r_symndx == STN_UNDEF)
12986 return NULL;
12987
12988 if (r_symndx >= cookie->locsymcount
12989 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12990 {
12991 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
12992 if (h == NULL)
12993 {
12994 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
12995 sec->owner);
12996 return NULL;
12997 }
12998 while (h->root.type == bfd_link_hash_indirect
12999 || h->root.type == bfd_link_hash_warning)
13000 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13001 h->mark = 1;
13002 /* If this symbol is weak and there is a non-weak definition, we
13003 keep the non-weak definition because many backends put
13004 dynamic reloc info on the non-weak definition for code
13005 handling copy relocs. */
13006 if (h->is_weakalias)
13007 weakdef (h)->mark = 1;
13008
13009 if (start_stop != NULL)
13010 {
13011 /* To work around a glibc bug, mark XXX input sections
13012 when there is a reference to __start_XXX or __stop_XXX
13013 symbols. */
13014 if (h->start_stop)
13015 {
13016 asection *s = h->u2.start_stop_section;
13017 *start_stop = !s->gc_mark;
13018 return s;
13019 }
13020 }
13021
13022 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13023 }
13024
13025 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13026 &cookie->locsyms[r_symndx]);
13027 }
13028
13029 /* COOKIE->rel describes a relocation against section SEC, which is
13030 a section we've decided to keep. Mark the section that contains
13031 the relocation symbol. */
13032
13033 bfd_boolean
13034 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13035 asection *sec,
13036 elf_gc_mark_hook_fn gc_mark_hook,
13037 struct elf_reloc_cookie *cookie)
13038 {
13039 asection *rsec;
13040 bfd_boolean start_stop = FALSE;
13041
13042 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13043 while (rsec != NULL)
13044 {
13045 if (!rsec->gc_mark)
13046 {
13047 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13048 || (rsec->owner->flags & DYNAMIC) != 0)
13049 rsec->gc_mark = 1;
13050 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13051 return FALSE;
13052 }
13053 if (!start_stop)
13054 break;
13055 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
13056 }
13057 return TRUE;
13058 }
13059
13060 /* The mark phase of garbage collection. For a given section, mark
13061 it and any sections in this section's group, and all the sections
13062 which define symbols to which it refers. */
13063
13064 bfd_boolean
13065 _bfd_elf_gc_mark (struct bfd_link_info *info,
13066 asection *sec,
13067 elf_gc_mark_hook_fn gc_mark_hook)
13068 {
13069 bfd_boolean ret;
13070 asection *group_sec, *eh_frame;
13071
13072 sec->gc_mark = 1;
13073
13074 /* Mark all the sections in the group. */
13075 group_sec = elf_section_data (sec)->next_in_group;
13076 if (group_sec && !group_sec->gc_mark)
13077 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
13078 return FALSE;
13079
13080 /* Look through the section relocs. */
13081 ret = TRUE;
13082 eh_frame = elf_eh_frame_section (sec->owner);
13083 if ((sec->flags & SEC_RELOC) != 0
13084 && sec->reloc_count > 0
13085 && sec != eh_frame)
13086 {
13087 struct elf_reloc_cookie cookie;
13088
13089 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13090 ret = FALSE;
13091 else
13092 {
13093 for (; cookie.rel < cookie.relend; cookie.rel++)
13094 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
13095 {
13096 ret = FALSE;
13097 break;
13098 }
13099 fini_reloc_cookie_for_section (&cookie, sec);
13100 }
13101 }
13102
13103 if (ret && eh_frame && elf_fde_list (sec))
13104 {
13105 struct elf_reloc_cookie cookie;
13106
13107 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13108 ret = FALSE;
13109 else
13110 {
13111 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13112 gc_mark_hook, &cookie))
13113 ret = FALSE;
13114 fini_reloc_cookie_for_section (&cookie, eh_frame);
13115 }
13116 }
13117
13118 eh_frame = elf_section_eh_frame_entry (sec);
13119 if (ret && eh_frame && !eh_frame->gc_mark)
13120 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13121 ret = FALSE;
13122
13123 return ret;
13124 }
13125
13126 /* Scan and mark sections in a special or debug section group. */
13127
13128 static void
13129 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13130 {
13131 /* Point to first section of section group. */
13132 asection *ssec;
13133 /* Used to iterate the section group. */
13134 asection *msec;
13135
13136 bfd_boolean is_special_grp = TRUE;
13137 bfd_boolean is_debug_grp = TRUE;
13138
13139 /* First scan to see if group contains any section other than debug
13140 and special section. */
13141 ssec = msec = elf_next_in_group (grp);
13142 do
13143 {
13144 if ((msec->flags & SEC_DEBUGGING) == 0)
13145 is_debug_grp = FALSE;
13146
13147 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13148 is_special_grp = FALSE;
13149
13150 msec = elf_next_in_group (msec);
13151 }
13152 while (msec != ssec);
13153
13154 /* If this is a pure debug section group or pure special section group,
13155 keep all sections in this group. */
13156 if (is_debug_grp || is_special_grp)
13157 {
13158 do
13159 {
13160 msec->gc_mark = 1;
13161 msec = elf_next_in_group (msec);
13162 }
13163 while (msec != ssec);
13164 }
13165 }
13166
13167 /* Keep debug and special sections. */
13168
13169 bfd_boolean
13170 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13171 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
13172 {
13173 bfd *ibfd;
13174
13175 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13176 {
13177 asection *isec;
13178 bfd_boolean some_kept;
13179 bfd_boolean debug_frag_seen;
13180 bfd_boolean has_kept_debug_info;
13181
13182 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13183 continue;
13184 isec = ibfd->sections;
13185 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13186 continue;
13187
13188 /* Ensure all linker created sections are kept,
13189 see if any other section is already marked,
13190 and note if we have any fragmented debug sections. */
13191 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
13192 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13193 {
13194 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13195 isec->gc_mark = 1;
13196 else if (isec->gc_mark
13197 && (isec->flags & SEC_ALLOC) != 0
13198 && elf_section_type (isec) != SHT_NOTE)
13199 some_kept = TRUE;
13200
13201 if (!debug_frag_seen
13202 && (isec->flags & SEC_DEBUGGING)
13203 && CONST_STRNEQ (isec->name, ".debug_line."))
13204 debug_frag_seen = TRUE;
13205 }
13206
13207 /* If no non-note alloc section in this file will be kept, then
13208 we can toss out the debug and special sections. */
13209 if (!some_kept)
13210 continue;
13211
13212 /* Keep debug and special sections like .comment when they are
13213 not part of a group. Also keep section groups that contain
13214 just debug sections or special sections. */
13215 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13216 {
13217 if ((isec->flags & SEC_GROUP) != 0)
13218 _bfd_elf_gc_mark_debug_special_section_group (isec);
13219 else if (((isec->flags & SEC_DEBUGGING) != 0
13220 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13221 && elf_next_in_group (isec) == NULL)
13222 isec->gc_mark = 1;
13223 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13224 has_kept_debug_info = TRUE;
13225 }
13226
13227 /* Look for CODE sections which are going to be discarded,
13228 and find and discard any fragmented debug sections which
13229 are associated with that code section. */
13230 if (debug_frag_seen)
13231 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13232 if ((isec->flags & SEC_CODE) != 0
13233 && isec->gc_mark == 0)
13234 {
13235 unsigned int ilen;
13236 asection *dsec;
13237
13238 ilen = strlen (isec->name);
13239
13240 /* Association is determined by the name of the debug
13241 section containing the name of the code section as
13242 a suffix. For example .debug_line.text.foo is a
13243 debug section associated with .text.foo. */
13244 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13245 {
13246 unsigned int dlen;
13247
13248 if (dsec->gc_mark == 0
13249 || (dsec->flags & SEC_DEBUGGING) == 0)
13250 continue;
13251
13252 dlen = strlen (dsec->name);
13253
13254 if (dlen > ilen
13255 && strncmp (dsec->name + (dlen - ilen),
13256 isec->name, ilen) == 0)
13257 dsec->gc_mark = 0;
13258 }
13259 }
13260
13261 /* Mark debug sections referenced by kept debug sections. */
13262 if (has_kept_debug_info)
13263 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13264 if (isec->gc_mark
13265 && (isec->flags & SEC_DEBUGGING) != 0)
13266 if (!_bfd_elf_gc_mark (info, isec,
13267 elf_gc_mark_debug_section))
13268 return FALSE;
13269 }
13270 return TRUE;
13271 }
13272
13273 static bfd_boolean
13274 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13275 {
13276 bfd *sub;
13277 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13278
13279 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13280 {
13281 asection *o;
13282
13283 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13284 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
13285 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13286 continue;
13287 o = sub->sections;
13288 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13289 continue;
13290
13291 for (o = sub->sections; o != NULL; o = o->next)
13292 {
13293 /* When any section in a section group is kept, we keep all
13294 sections in the section group. If the first member of
13295 the section group is excluded, we will also exclude the
13296 group section. */
13297 if (o->flags & SEC_GROUP)
13298 {
13299 asection *first = elf_next_in_group (o);
13300 o->gc_mark = first->gc_mark;
13301 }
13302
13303 if (o->gc_mark)
13304 continue;
13305
13306 /* Skip sweeping sections already excluded. */
13307 if (o->flags & SEC_EXCLUDE)
13308 continue;
13309
13310 /* Since this is early in the link process, it is simple
13311 to remove a section from the output. */
13312 o->flags |= SEC_EXCLUDE;
13313
13314 if (info->print_gc_sections && o->size != 0)
13315 /* xgettext:c-format */
13316 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13317 o, sub);
13318 }
13319 }
13320
13321 return TRUE;
13322 }
13323
13324 /* Propagate collected vtable information. This is called through
13325 elf_link_hash_traverse. */
13326
13327 static bfd_boolean
13328 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13329 {
13330 /* Those that are not vtables. */
13331 if (h->start_stop
13332 || h->u2.vtable == NULL
13333 || h->u2.vtable->parent == NULL)
13334 return TRUE;
13335
13336 /* Those vtables that do not have parents, we cannot merge. */
13337 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
13338 return TRUE;
13339
13340 /* If we've already been done, exit. */
13341 if (h->u2.vtable->used && h->u2.vtable->used[-1])
13342 return TRUE;
13343
13344 /* Make sure the parent's table is up to date. */
13345 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
13346
13347 if (h->u2.vtable->used == NULL)
13348 {
13349 /* None of this table's entries were referenced. Re-use the
13350 parent's table. */
13351 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13352 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
13353 }
13354 else
13355 {
13356 size_t n;
13357 bfd_boolean *cu, *pu;
13358
13359 /* Or the parent's entries into ours. */
13360 cu = h->u2.vtable->used;
13361 cu[-1] = TRUE;
13362 pu = h->u2.vtable->parent->u2.vtable->used;
13363 if (pu != NULL)
13364 {
13365 const struct elf_backend_data *bed;
13366 unsigned int log_file_align;
13367
13368 bed = get_elf_backend_data (h->root.u.def.section->owner);
13369 log_file_align = bed->s->log_file_align;
13370 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
13371 while (n--)
13372 {
13373 if (*pu)
13374 *cu = TRUE;
13375 pu++;
13376 cu++;
13377 }
13378 }
13379 }
13380
13381 return TRUE;
13382 }
13383
13384 static bfd_boolean
13385 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13386 {
13387 asection *sec;
13388 bfd_vma hstart, hend;
13389 Elf_Internal_Rela *relstart, *relend, *rel;
13390 const struct elf_backend_data *bed;
13391 unsigned int log_file_align;
13392
13393 /* Take care of both those symbols that do not describe vtables as
13394 well as those that are not loaded. */
13395 if (h->start_stop
13396 || h->u2.vtable == NULL
13397 || h->u2.vtable->parent == NULL)
13398 return TRUE;
13399
13400 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13401 || h->root.type == bfd_link_hash_defweak);
13402
13403 sec = h->root.u.def.section;
13404 hstart = h->root.u.def.value;
13405 hend = hstart + h->size;
13406
13407 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13408 if (!relstart)
13409 return *(bfd_boolean *) okp = FALSE;
13410 bed = get_elf_backend_data (sec->owner);
13411 log_file_align = bed->s->log_file_align;
13412
13413 relend = relstart + sec->reloc_count;
13414
13415 for (rel = relstart; rel < relend; ++rel)
13416 if (rel->r_offset >= hstart && rel->r_offset < hend)
13417 {
13418 /* If the entry is in use, do nothing. */
13419 if (h->u2.vtable->used
13420 && (rel->r_offset - hstart) < h->u2.vtable->size)
13421 {
13422 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13423 if (h->u2.vtable->used[entry])
13424 continue;
13425 }
13426 /* Otherwise, kill it. */
13427 rel->r_offset = rel->r_info = rel->r_addend = 0;
13428 }
13429
13430 return TRUE;
13431 }
13432
13433 /* Mark sections containing dynamically referenced symbols. When
13434 building shared libraries, we must assume that any visible symbol is
13435 referenced. */
13436
13437 bfd_boolean
13438 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13439 {
13440 struct bfd_link_info *info = (struct bfd_link_info *) inf;
13441 struct bfd_elf_dynamic_list *d = info->dynamic_list;
13442
13443 if ((h->root.type == bfd_link_hash_defined
13444 || h->root.type == bfd_link_hash_defweak)
13445 && ((h->ref_dynamic && !h->forced_local)
13446 || ((h->def_regular || ELF_COMMON_DEF_P (h))
13447 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
13448 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
13449 && (!bfd_link_executable (info)
13450 || info->gc_keep_exported
13451 || info->export_dynamic
13452 || (h->dynamic
13453 && d != NULL
13454 && (*d->match) (&d->head, NULL, h->root.root.string)))
13455 && (h->versioned >= versioned
13456 || !bfd_hide_sym_by_version (info->version_info,
13457 h->root.root.string)))))
13458 h->root.u.def.section->flags |= SEC_KEEP;
13459
13460 return TRUE;
13461 }
13462
13463 /* Keep all sections containing symbols undefined on the command-line,
13464 and the section containing the entry symbol. */
13465
13466 void
13467 _bfd_elf_gc_keep (struct bfd_link_info *info)
13468 {
13469 struct bfd_sym_chain *sym;
13470
13471 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13472 {
13473 struct elf_link_hash_entry *h;
13474
13475 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13476 FALSE, FALSE, FALSE);
13477
13478 if (h != NULL
13479 && (h->root.type == bfd_link_hash_defined
13480 || h->root.type == bfd_link_hash_defweak)
13481 && !bfd_is_abs_section (h->root.u.def.section)
13482 && !bfd_is_und_section (h->root.u.def.section))
13483 h->root.u.def.section->flags |= SEC_KEEP;
13484 }
13485 }
13486
13487 bfd_boolean
13488 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13489 struct bfd_link_info *info)
13490 {
13491 bfd *ibfd = info->input_bfds;
13492
13493 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13494 {
13495 asection *sec;
13496 struct elf_reloc_cookie cookie;
13497
13498 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13499 continue;
13500 sec = ibfd->sections;
13501 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13502 continue;
13503
13504 if (!init_reloc_cookie (&cookie, info, ibfd))
13505 return FALSE;
13506
13507 for (sec = ibfd->sections; sec; sec = sec->next)
13508 {
13509 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13510 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13511 {
13512 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13513 fini_reloc_cookie_rels (&cookie, sec);
13514 }
13515 }
13516 }
13517 return TRUE;
13518 }
13519
13520 /* Do mark and sweep of unused sections. */
13521
13522 bfd_boolean
13523 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13524 {
13525 bfd_boolean ok = TRUE;
13526 bfd *sub;
13527 elf_gc_mark_hook_fn gc_mark_hook;
13528 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13529 struct elf_link_hash_table *htab;
13530
13531 if (!bed->can_gc_sections
13532 || !is_elf_hash_table (info->hash))
13533 {
13534 _bfd_error_handler(_("warning: gc-sections option ignored"));
13535 return TRUE;
13536 }
13537
13538 bed->gc_keep (info);
13539 htab = elf_hash_table (info);
13540
13541 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13542 at the .eh_frame section if we can mark the FDEs individually. */
13543 for (sub = info->input_bfds;
13544 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13545 sub = sub->link.next)
13546 {
13547 asection *sec;
13548 struct elf_reloc_cookie cookie;
13549
13550 sec = sub->sections;
13551 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13552 continue;
13553 sec = bfd_get_section_by_name (sub, ".eh_frame");
13554 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
13555 {
13556 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
13557 if (elf_section_data (sec)->sec_info
13558 && (sec->flags & SEC_LINKER_CREATED) == 0)
13559 elf_eh_frame_section (sub) = sec;
13560 fini_reloc_cookie_for_section (&cookie, sec);
13561 sec = bfd_get_next_section_by_name (NULL, sec);
13562 }
13563 }
13564
13565 /* Apply transitive closure to the vtable entry usage info. */
13566 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13567 if (!ok)
13568 return FALSE;
13569
13570 /* Kill the vtable relocations that were not used. */
13571 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13572 if (!ok)
13573 return FALSE;
13574
13575 /* Mark dynamically referenced symbols. */
13576 if (htab->dynamic_sections_created || info->gc_keep_exported)
13577 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13578
13579 /* Grovel through relocs to find out who stays ... */
13580 gc_mark_hook = bed->gc_mark_hook;
13581 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13582 {
13583 asection *o;
13584
13585 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13586 || elf_object_id (sub) != elf_hash_table_id (htab)
13587 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13588 continue;
13589
13590 o = sub->sections;
13591 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13592 continue;
13593
13594 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13595 Also treat note sections as a root, if the section is not part
13596 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13597 well as FINI_ARRAY sections for ld -r. */
13598 for (o = sub->sections; o != NULL; o = o->next)
13599 if (!o->gc_mark
13600 && (o->flags & SEC_EXCLUDE) == 0
13601 && ((o->flags & SEC_KEEP) != 0
13602 || (bfd_link_relocatable (info)
13603 && ((elf_section_data (o)->this_hdr.sh_type
13604 == SHT_PREINIT_ARRAY)
13605 || (elf_section_data (o)->this_hdr.sh_type
13606 == SHT_INIT_ARRAY)
13607 || (elf_section_data (o)->this_hdr.sh_type
13608 == SHT_FINI_ARRAY)))
13609 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13610 && elf_next_in_group (o) == NULL )))
13611 {
13612 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13613 return FALSE;
13614 }
13615 }
13616
13617 /* Allow the backend to mark additional target specific sections. */
13618 bed->gc_mark_extra_sections (info, gc_mark_hook);
13619
13620 /* ... and mark SEC_EXCLUDE for those that go. */
13621 return elf_gc_sweep (abfd, info);
13622 }
13623 \f
13624 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13625
13626 bfd_boolean
13627 bfd_elf_gc_record_vtinherit (bfd *abfd,
13628 asection *sec,
13629 struct elf_link_hash_entry *h,
13630 bfd_vma offset)
13631 {
13632 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13633 struct elf_link_hash_entry **search, *child;
13634 size_t extsymcount;
13635 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13636
13637 /* The sh_info field of the symtab header tells us where the
13638 external symbols start. We don't care about the local symbols at
13639 this point. */
13640 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13641 if (!elf_bad_symtab (abfd))
13642 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13643
13644 sym_hashes = elf_sym_hashes (abfd);
13645 sym_hashes_end = sym_hashes + extsymcount;
13646
13647 /* Hunt down the child symbol, which is in this section at the same
13648 offset as the relocation. */
13649 for (search = sym_hashes; search != sym_hashes_end; ++search)
13650 {
13651 if ((child = *search) != NULL
13652 && (child->root.type == bfd_link_hash_defined
13653 || child->root.type == bfd_link_hash_defweak)
13654 && child->root.u.def.section == sec
13655 && child->root.u.def.value == offset)
13656 goto win;
13657 }
13658
13659 /* xgettext:c-format */
13660 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
13661 abfd, sec, (uint64_t) offset);
13662 bfd_set_error (bfd_error_invalid_operation);
13663 return FALSE;
13664
13665 win:
13666 if (!child->u2.vtable)
13667 {
13668 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13669 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13670 if (!child->u2.vtable)
13671 return FALSE;
13672 }
13673 if (!h)
13674 {
13675 /* This *should* only be the absolute section. It could potentially
13676 be that someone has defined a non-global vtable though, which
13677 would be bad. It isn't worth paging in the local symbols to be
13678 sure though; that case should simply be handled by the assembler. */
13679
13680 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
13681 }
13682 else
13683 child->u2.vtable->parent = h;
13684
13685 return TRUE;
13686 }
13687
13688 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13689
13690 bfd_boolean
13691 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13692 asection *sec ATTRIBUTE_UNUSED,
13693 struct elf_link_hash_entry *h,
13694 bfd_vma addend)
13695 {
13696 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13697 unsigned int log_file_align = bed->s->log_file_align;
13698
13699 if (!h->u2.vtable)
13700 {
13701 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13702 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13703 if (!h->u2.vtable)
13704 return FALSE;
13705 }
13706
13707 if (addend >= h->u2.vtable->size)
13708 {
13709 size_t size, bytes, file_align;
13710 bfd_boolean *ptr = h->u2.vtable->used;
13711
13712 /* While the symbol is undefined, we have to be prepared to handle
13713 a zero size. */
13714 file_align = 1 << log_file_align;
13715 if (h->root.type == bfd_link_hash_undefined)
13716 size = addend + file_align;
13717 else
13718 {
13719 size = h->size;
13720 if (addend >= size)
13721 {
13722 /* Oops! We've got a reference past the defined end of
13723 the table. This is probably a bug -- shall we warn? */
13724 size = addend + file_align;
13725 }
13726 }
13727 size = (size + file_align - 1) & -file_align;
13728
13729 /* Allocate one extra entry for use as a "done" flag for the
13730 consolidation pass. */
13731 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13732
13733 if (ptr)
13734 {
13735 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
13736
13737 if (ptr != NULL)
13738 {
13739 size_t oldbytes;
13740
13741 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
13742 * sizeof (bfd_boolean));
13743 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13744 }
13745 }
13746 else
13747 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13748
13749 if (ptr == NULL)
13750 return FALSE;
13751
13752 /* And arrange for that done flag to be at index -1. */
13753 h->u2.vtable->used = ptr + 1;
13754 h->u2.vtable->size = size;
13755 }
13756
13757 h->u2.vtable->used[addend >> log_file_align] = TRUE;
13758
13759 return TRUE;
13760 }
13761
13762 /* Map an ELF section header flag to its corresponding string. */
13763 typedef struct
13764 {
13765 char *flag_name;
13766 flagword flag_value;
13767 } elf_flags_to_name_table;
13768
13769 static elf_flags_to_name_table elf_flags_to_names [] =
13770 {
13771 { "SHF_WRITE", SHF_WRITE },
13772 { "SHF_ALLOC", SHF_ALLOC },
13773 { "SHF_EXECINSTR", SHF_EXECINSTR },
13774 { "SHF_MERGE", SHF_MERGE },
13775 { "SHF_STRINGS", SHF_STRINGS },
13776 { "SHF_INFO_LINK", SHF_INFO_LINK},
13777 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13778 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13779 { "SHF_GROUP", SHF_GROUP },
13780 { "SHF_TLS", SHF_TLS },
13781 { "SHF_MASKOS", SHF_MASKOS },
13782 { "SHF_EXCLUDE", SHF_EXCLUDE },
13783 };
13784
13785 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13786 bfd_boolean
13787 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13788 struct flag_info *flaginfo,
13789 asection *section)
13790 {
13791 const bfd_vma sh_flags = elf_section_flags (section);
13792
13793 if (!flaginfo->flags_initialized)
13794 {
13795 bfd *obfd = info->output_bfd;
13796 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13797 struct flag_info_list *tf = flaginfo->flag_list;
13798 int with_hex = 0;
13799 int without_hex = 0;
13800
13801 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13802 {
13803 unsigned i;
13804 flagword (*lookup) (char *);
13805
13806 lookup = bed->elf_backend_lookup_section_flags_hook;
13807 if (lookup != NULL)
13808 {
13809 flagword hexval = (*lookup) ((char *) tf->name);
13810
13811 if (hexval != 0)
13812 {
13813 if (tf->with == with_flags)
13814 with_hex |= hexval;
13815 else if (tf->with == without_flags)
13816 without_hex |= hexval;
13817 tf->valid = TRUE;
13818 continue;
13819 }
13820 }
13821 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13822 {
13823 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13824 {
13825 if (tf->with == with_flags)
13826 with_hex |= elf_flags_to_names[i].flag_value;
13827 else if (tf->with == without_flags)
13828 without_hex |= elf_flags_to_names[i].flag_value;
13829 tf->valid = TRUE;
13830 break;
13831 }
13832 }
13833 if (!tf->valid)
13834 {
13835 info->callbacks->einfo
13836 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13837 return FALSE;
13838 }
13839 }
13840 flaginfo->flags_initialized = TRUE;
13841 flaginfo->only_with_flags |= with_hex;
13842 flaginfo->not_with_flags |= without_hex;
13843 }
13844
13845 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13846 return FALSE;
13847
13848 if ((flaginfo->not_with_flags & sh_flags) != 0)
13849 return FALSE;
13850
13851 return TRUE;
13852 }
13853
13854 struct alloc_got_off_arg {
13855 bfd_vma gotoff;
13856 struct bfd_link_info *info;
13857 };
13858
13859 /* We need a special top-level link routine to convert got reference counts
13860 to real got offsets. */
13861
13862 static bfd_boolean
13863 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13864 {
13865 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13866 bfd *obfd = gofarg->info->output_bfd;
13867 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13868
13869 if (h->got.refcount > 0)
13870 {
13871 h->got.offset = gofarg->gotoff;
13872 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13873 }
13874 else
13875 h->got.offset = (bfd_vma) -1;
13876
13877 return TRUE;
13878 }
13879
13880 /* And an accompanying bit to work out final got entry offsets once
13881 we're done. Should be called from final_link. */
13882
13883 bfd_boolean
13884 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13885 struct bfd_link_info *info)
13886 {
13887 bfd *i;
13888 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13889 bfd_vma gotoff;
13890 struct alloc_got_off_arg gofarg;
13891
13892 BFD_ASSERT (abfd == info->output_bfd);
13893
13894 if (! is_elf_hash_table (info->hash))
13895 return FALSE;
13896
13897 /* The GOT offset is relative to the .got section, but the GOT header is
13898 put into the .got.plt section, if the backend uses it. */
13899 if (bed->want_got_plt)
13900 gotoff = 0;
13901 else
13902 gotoff = bed->got_header_size;
13903
13904 /* Do the local .got entries first. */
13905 for (i = info->input_bfds; i; i = i->link.next)
13906 {
13907 bfd_signed_vma *local_got;
13908 size_t j, locsymcount;
13909 Elf_Internal_Shdr *symtab_hdr;
13910
13911 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13912 continue;
13913
13914 local_got = elf_local_got_refcounts (i);
13915 if (!local_got)
13916 continue;
13917
13918 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13919 if (elf_bad_symtab (i))
13920 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13921 else
13922 locsymcount = symtab_hdr->sh_info;
13923
13924 for (j = 0; j < locsymcount; ++j)
13925 {
13926 if (local_got[j] > 0)
13927 {
13928 local_got[j] = gotoff;
13929 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13930 }
13931 else
13932 local_got[j] = (bfd_vma) -1;
13933 }
13934 }
13935
13936 /* Then the global .got entries. .plt refcounts are handled by
13937 adjust_dynamic_symbol */
13938 gofarg.gotoff = gotoff;
13939 gofarg.info = info;
13940 elf_link_hash_traverse (elf_hash_table (info),
13941 elf_gc_allocate_got_offsets,
13942 &gofarg);
13943 return TRUE;
13944 }
13945
13946 /* Many folk need no more in the way of final link than this, once
13947 got entry reference counting is enabled. */
13948
13949 bfd_boolean
13950 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13951 {
13952 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13953 return FALSE;
13954
13955 /* Invoke the regular ELF backend linker to do all the work. */
13956 return bfd_elf_final_link (abfd, info);
13957 }
13958
13959 bfd_boolean
13960 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13961 {
13962 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13963
13964 if (rcookie->bad_symtab)
13965 rcookie->rel = rcookie->rels;
13966
13967 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13968 {
13969 unsigned long r_symndx;
13970
13971 if (! rcookie->bad_symtab)
13972 if (rcookie->rel->r_offset > offset)
13973 return FALSE;
13974 if (rcookie->rel->r_offset != offset)
13975 continue;
13976
13977 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
13978 if (r_symndx == STN_UNDEF)
13979 return TRUE;
13980
13981 if (r_symndx >= rcookie->locsymcount
13982 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13983 {
13984 struct elf_link_hash_entry *h;
13985
13986 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13987
13988 while (h->root.type == bfd_link_hash_indirect
13989 || h->root.type == bfd_link_hash_warning)
13990 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13991
13992 if ((h->root.type == bfd_link_hash_defined
13993 || h->root.type == bfd_link_hash_defweak)
13994 && (h->root.u.def.section->owner != rcookie->abfd
13995 || h->root.u.def.section->kept_section != NULL
13996 || discarded_section (h->root.u.def.section)))
13997 return TRUE;
13998 }
13999 else
14000 {
14001 /* It's not a relocation against a global symbol,
14002 but it could be a relocation against a local
14003 symbol for a discarded section. */
14004 asection *isec;
14005 Elf_Internal_Sym *isym;
14006
14007 /* Need to: get the symbol; get the section. */
14008 isym = &rcookie->locsyms[r_symndx];
14009 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
14010 if (isec != NULL
14011 && (isec->kept_section != NULL
14012 || discarded_section (isec)))
14013 return TRUE;
14014 }
14015 return FALSE;
14016 }
14017 return FALSE;
14018 }
14019
14020 /* Discard unneeded references to discarded sections.
14021 Returns -1 on error, 1 if any section's size was changed, 0 if
14022 nothing changed. This function assumes that the relocations are in
14023 sorted order, which is true for all known assemblers. */
14024
14025 int
14026 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14027 {
14028 struct elf_reloc_cookie cookie;
14029 asection *o;
14030 bfd *abfd;
14031 int changed = 0;
14032
14033 if (info->traditional_format
14034 || !is_elf_hash_table (info->hash))
14035 return 0;
14036
14037 o = bfd_get_section_by_name (output_bfd, ".stab");
14038 if (o != NULL)
14039 {
14040 asection *i;
14041
14042 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14043 {
14044 if (i->size == 0
14045 || i->reloc_count == 0
14046 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14047 continue;
14048
14049 abfd = i->owner;
14050 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14051 continue;
14052
14053 if (!init_reloc_cookie_for_section (&cookie, info, i))
14054 return -1;
14055
14056 if (_bfd_discard_section_stabs (abfd, i,
14057 elf_section_data (i)->sec_info,
14058 bfd_elf_reloc_symbol_deleted_p,
14059 &cookie))
14060 changed = 1;
14061
14062 fini_reloc_cookie_for_section (&cookie, i);
14063 }
14064 }
14065
14066 o = NULL;
14067 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14068 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
14069 if (o != NULL)
14070 {
14071 asection *i;
14072 int eh_changed = 0;
14073 unsigned int eh_alignment;
14074
14075 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14076 {
14077 if (i->size == 0)
14078 continue;
14079
14080 abfd = i->owner;
14081 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14082 continue;
14083
14084 if (!init_reloc_cookie_for_section (&cookie, info, i))
14085 return -1;
14086
14087 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14088 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
14089 bfd_elf_reloc_symbol_deleted_p,
14090 &cookie))
14091 {
14092 eh_changed = 1;
14093 if (i->size != i->rawsize)
14094 changed = 1;
14095 }
14096
14097 fini_reloc_cookie_for_section (&cookie, i);
14098 }
14099
14100 eh_alignment = 1 << o->alignment_power;
14101 /* Skip over zero terminator, and prevent empty sections from
14102 adding alignment padding at the end. */
14103 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14104 if (i->size == 0)
14105 i->flags |= SEC_EXCLUDE;
14106 else if (i->size > 4)
14107 break;
14108 /* The last non-empty eh_frame section doesn't need padding. */
14109 if (i != NULL)
14110 i = i->map_tail.s;
14111 /* Any prior sections must pad the last FDE out to the output
14112 section alignment. Otherwise we might have zero padding
14113 between sections, which would be seen as a terminator. */
14114 for (; i != NULL; i = i->map_tail.s)
14115 if (i->size == 4)
14116 /* All but the last zero terminator should have been removed. */
14117 BFD_FAIL ();
14118 else
14119 {
14120 bfd_size_type size
14121 = (i->size + eh_alignment - 1) & -eh_alignment;
14122 if (i->size != size)
14123 {
14124 i->size = size;
14125 changed = 1;
14126 eh_changed = 1;
14127 }
14128 }
14129 if (eh_changed)
14130 elf_link_hash_traverse (elf_hash_table (info),
14131 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
14132 }
14133
14134 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14135 {
14136 const struct elf_backend_data *bed;
14137 asection *s;
14138
14139 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14140 continue;
14141 s = abfd->sections;
14142 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14143 continue;
14144
14145 bed = get_elf_backend_data (abfd);
14146
14147 if (bed->elf_backend_discard_info != NULL)
14148 {
14149 if (!init_reloc_cookie (&cookie, info, abfd))
14150 return -1;
14151
14152 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
14153 changed = 1;
14154
14155 fini_reloc_cookie (&cookie, abfd);
14156 }
14157 }
14158
14159 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14160 _bfd_elf_end_eh_frame_parsing (info);
14161
14162 if (info->eh_frame_hdr_type
14163 && !bfd_link_relocatable (info)
14164 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
14165 changed = 1;
14166
14167 return changed;
14168 }
14169
14170 bfd_boolean
14171 _bfd_elf_section_already_linked (bfd *abfd,
14172 asection *sec,
14173 struct bfd_link_info *info)
14174 {
14175 flagword flags;
14176 const char *name, *key;
14177 struct bfd_section_already_linked *l;
14178 struct bfd_section_already_linked_hash_entry *already_linked_list;
14179
14180 if (sec->output_section == bfd_abs_section_ptr)
14181 return FALSE;
14182
14183 flags = sec->flags;
14184
14185 /* Return if it isn't a linkonce section. A comdat group section
14186 also has SEC_LINK_ONCE set. */
14187 if ((flags & SEC_LINK_ONCE) == 0)
14188 return FALSE;
14189
14190 /* Don't put group member sections on our list of already linked
14191 sections. They are handled as a group via their group section. */
14192 if (elf_sec_group (sec) != NULL)
14193 return FALSE;
14194
14195 /* For a SHT_GROUP section, use the group signature as the key. */
14196 name = sec->name;
14197 if ((flags & SEC_GROUP) != 0
14198 && elf_next_in_group (sec) != NULL
14199 && elf_group_name (elf_next_in_group (sec)) != NULL)
14200 key = elf_group_name (elf_next_in_group (sec));
14201 else
14202 {
14203 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14204 if (CONST_STRNEQ (name, ".gnu.linkonce.")
14205 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14206 key++;
14207 else
14208 /* Must be a user linkonce section that doesn't follow gcc's
14209 naming convention. In this case we won't be matching
14210 single member groups. */
14211 key = name;
14212 }
14213
14214 already_linked_list = bfd_section_already_linked_table_lookup (key);
14215
14216 for (l = already_linked_list->entry; l != NULL; l = l->next)
14217 {
14218 /* We may have 2 different types of sections on the list: group
14219 sections with a signature of <key> (<key> is some string),
14220 and linkonce sections named .gnu.linkonce.<type>.<key>.
14221 Match like sections. LTO plugin sections are an exception.
14222 They are always named .gnu.linkonce.t.<key> and match either
14223 type of section. */
14224 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14225 && ((flags & SEC_GROUP) != 0
14226 || strcmp (name, l->sec->name) == 0))
14227 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
14228 {
14229 /* The section has already been linked. See if we should
14230 issue a warning. */
14231 if (!_bfd_handle_already_linked (sec, l, info))
14232 return FALSE;
14233
14234 if (flags & SEC_GROUP)
14235 {
14236 asection *first = elf_next_in_group (sec);
14237 asection *s = first;
14238
14239 while (s != NULL)
14240 {
14241 s->output_section = bfd_abs_section_ptr;
14242 /* Record which group discards it. */
14243 s->kept_section = l->sec;
14244 s = elf_next_in_group (s);
14245 /* These lists are circular. */
14246 if (s == first)
14247 break;
14248 }
14249 }
14250
14251 return TRUE;
14252 }
14253 }
14254
14255 /* A single member comdat group section may be discarded by a
14256 linkonce section and vice versa. */
14257 if ((flags & SEC_GROUP) != 0)
14258 {
14259 asection *first = elf_next_in_group (sec);
14260
14261 if (first != NULL && elf_next_in_group (first) == first)
14262 /* Check this single member group against linkonce sections. */
14263 for (l = already_linked_list->entry; l != NULL; l = l->next)
14264 if ((l->sec->flags & SEC_GROUP) == 0
14265 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14266 {
14267 first->output_section = bfd_abs_section_ptr;
14268 first->kept_section = l->sec;
14269 sec->output_section = bfd_abs_section_ptr;
14270 break;
14271 }
14272 }
14273 else
14274 /* Check this linkonce section against single member groups. */
14275 for (l = already_linked_list->entry; l != NULL; l = l->next)
14276 if (l->sec->flags & SEC_GROUP)
14277 {
14278 asection *first = elf_next_in_group (l->sec);
14279
14280 if (first != NULL
14281 && elf_next_in_group (first) == first
14282 && bfd_elf_match_symbols_in_sections (first, sec, info))
14283 {
14284 sec->output_section = bfd_abs_section_ptr;
14285 sec->kept_section = first;
14286 break;
14287 }
14288 }
14289
14290 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14291 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14292 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14293 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14294 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14295 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14296 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14297 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14298 The reverse order cannot happen as there is never a bfd with only the
14299 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14300 matter as here were are looking only for cross-bfd sections. */
14301
14302 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14303 for (l = already_linked_list->entry; l != NULL; l = l->next)
14304 if ((l->sec->flags & SEC_GROUP) == 0
14305 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14306 {
14307 if (abfd != l->sec->owner)
14308 sec->output_section = bfd_abs_section_ptr;
14309 break;
14310 }
14311
14312 /* This is the first section with this name. Record it. */
14313 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
14314 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
14315 return sec->output_section == bfd_abs_section_ptr;
14316 }
14317
14318 bfd_boolean
14319 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
14320 {
14321 return sym->st_shndx == SHN_COMMON;
14322 }
14323
14324 unsigned int
14325 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14326 {
14327 return SHN_COMMON;
14328 }
14329
14330 asection *
14331 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14332 {
14333 return bfd_com_section_ptr;
14334 }
14335
14336 bfd_vma
14337 _bfd_elf_default_got_elt_size (bfd *abfd,
14338 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14339 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14340 bfd *ibfd ATTRIBUTE_UNUSED,
14341 unsigned long symndx ATTRIBUTE_UNUSED)
14342 {
14343 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14344 return bed->s->arch_size / 8;
14345 }
14346
14347 /* Routines to support the creation of dynamic relocs. */
14348
14349 /* Returns the name of the dynamic reloc section associated with SEC. */
14350
14351 static const char *
14352 get_dynamic_reloc_section_name (bfd * abfd,
14353 asection * sec,
14354 bfd_boolean is_rela)
14355 {
14356 char *name;
14357 const char *old_name = bfd_get_section_name (NULL, sec);
14358 const char *prefix = is_rela ? ".rela" : ".rel";
14359
14360 if (old_name == NULL)
14361 return NULL;
14362
14363 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
14364 sprintf (name, "%s%s", prefix, old_name);
14365
14366 return name;
14367 }
14368
14369 /* Returns the dynamic reloc section associated with SEC.
14370 If necessary compute the name of the dynamic reloc section based
14371 on SEC's name (looked up in ABFD's string table) and the setting
14372 of IS_RELA. */
14373
14374 asection *
14375 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14376 asection * sec,
14377 bfd_boolean is_rela)
14378 {
14379 asection * reloc_sec = elf_section_data (sec)->sreloc;
14380
14381 if (reloc_sec == NULL)
14382 {
14383 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14384
14385 if (name != NULL)
14386 {
14387 reloc_sec = bfd_get_linker_section (abfd, name);
14388
14389 if (reloc_sec != NULL)
14390 elf_section_data (sec)->sreloc = reloc_sec;
14391 }
14392 }
14393
14394 return reloc_sec;
14395 }
14396
14397 /* Returns the dynamic reloc section associated with SEC. If the
14398 section does not exist it is created and attached to the DYNOBJ
14399 bfd and stored in the SRELOC field of SEC's elf_section_data
14400 structure.
14401
14402 ALIGNMENT is the alignment for the newly created section and
14403 IS_RELA defines whether the name should be .rela.<SEC's name>
14404 or .rel.<SEC's name>. The section name is looked up in the
14405 string table associated with ABFD. */
14406
14407 asection *
14408 _bfd_elf_make_dynamic_reloc_section (asection *sec,
14409 bfd *dynobj,
14410 unsigned int alignment,
14411 bfd *abfd,
14412 bfd_boolean is_rela)
14413 {
14414 asection * reloc_sec = elf_section_data (sec)->sreloc;
14415
14416 if (reloc_sec == NULL)
14417 {
14418 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14419
14420 if (name == NULL)
14421 return NULL;
14422
14423 reloc_sec = bfd_get_linker_section (dynobj, name);
14424
14425 if (reloc_sec == NULL)
14426 {
14427 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14428 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
14429 if ((sec->flags & SEC_ALLOC) != 0)
14430 flags |= SEC_ALLOC | SEC_LOAD;
14431
14432 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
14433 if (reloc_sec != NULL)
14434 {
14435 /* _bfd_elf_get_sec_type_attr chooses a section type by
14436 name. Override as it may be wrong, eg. for a user
14437 section named "auto" we'll get ".relauto" which is
14438 seen to be a .rela section. */
14439 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
14440 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14441 reloc_sec = NULL;
14442 }
14443 }
14444
14445 elf_section_data (sec)->sreloc = reloc_sec;
14446 }
14447
14448 return reloc_sec;
14449 }
14450
14451 /* Copy the ELF symbol type and other attributes for a linker script
14452 assignment from HSRC to HDEST. Generally this should be treated as
14453 if we found a strong non-dynamic definition for HDEST (except that
14454 ld ignores multiple definition errors). */
14455 void
14456 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14457 struct bfd_link_hash_entry *hdest,
14458 struct bfd_link_hash_entry *hsrc)
14459 {
14460 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14461 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14462 Elf_Internal_Sym isym;
14463
14464 ehdest->type = ehsrc->type;
14465 ehdest->target_internal = ehsrc->target_internal;
14466
14467 isym.st_other = ehsrc->other;
14468 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
14469 }
14470
14471 /* Append a RELA relocation REL to section S in BFD. */
14472
14473 void
14474 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14475 {
14476 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14477 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14478 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14479 bed->s->swap_reloca_out (abfd, rel, loc);
14480 }
14481
14482 /* Append a REL relocation REL to section S in BFD. */
14483
14484 void
14485 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14486 {
14487 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14488 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14489 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
14490 bed->s->swap_reloc_out (abfd, rel, loc);
14491 }
14492
14493 /* Define __start, __stop, .startof. or .sizeof. symbol. */
14494
14495 struct bfd_link_hash_entry *
14496 bfd_elf_define_start_stop (struct bfd_link_info *info,
14497 const char *symbol, asection *sec)
14498 {
14499 struct elf_link_hash_entry *h;
14500
14501 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14502 FALSE, FALSE, TRUE);
14503 if (h != NULL
14504 && (h->root.type == bfd_link_hash_undefined
14505 || h->root.type == bfd_link_hash_undefweak
14506 || ((h->ref_regular || h->def_dynamic) && !h->def_regular)))
14507 {
14508 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
14509 h->root.type = bfd_link_hash_defined;
14510 h->root.u.def.section = sec;
14511 h->root.u.def.value = 0;
14512 h->def_regular = 1;
14513 h->def_dynamic = 0;
14514 h->start_stop = 1;
14515 h->u2.start_stop_section = sec;
14516 if (symbol[0] == '.')
14517 {
14518 /* .startof. and .sizeof. symbols are local. */
14519 const struct elf_backend_data *bed;
14520 bed = get_elf_backend_data (info->output_bfd);
14521 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
14522 }
14523 else
14524 {
14525 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14526 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
14527 if (was_dynamic)
14528 bfd_elf_link_record_dynamic_symbol (info, h);
14529 }
14530 return &h->root;
14531 }
14532 return NULL;
14533 }
This page took 0.522312 seconds and 4 git commands to generate.