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