bfd_size_type to size_t
[deliverable/binutils-gdb.git] / bfd / elflink.c
1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2020 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
20
21 #include "sysdep.h"
22 #include "bfd.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #define ARCH_SIZE 0
26 #include "elf-bfd.h"
27 #include "safe-ctype.h"
28 #include "libiberty.h"
29 #include "objalloc.h"
30 #if BFD_SUPPORTS_PLUGINS
31 #include "plugin-api.h"
32 #include "plugin.h"
33 #endif
34
35 /* This struct is used to pass information to routines called via
36 elf_link_hash_traverse which must return failure. */
37
38 struct elf_info_failed
39 {
40 struct bfd_link_info *info;
41 bfd_boolean failed;
42 };
43
44 /* This structure is used to pass information to
45 _bfd_elf_link_find_version_dependencies. */
46
47 struct elf_find_verdep_info
48 {
49 /* General link information. */
50 struct bfd_link_info *info;
51 /* The number of dependencies. */
52 unsigned int vers;
53 /* Whether we had a failure. */
54 bfd_boolean failed;
55 };
56
57 static bfd_boolean _bfd_elf_fix_symbol_flags
58 (struct elf_link_hash_entry *, struct elf_info_failed *);
59
60 asection *
61 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
62 unsigned long r_symndx,
63 bfd_boolean discard)
64 {
65 if (r_symndx >= cookie->locsymcount
66 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
67 {
68 struct elf_link_hash_entry *h;
69
70 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
71
72 while (h->root.type == bfd_link_hash_indirect
73 || h->root.type == bfd_link_hash_warning)
74 h = (struct elf_link_hash_entry *) h->root.u.i.link;
75
76 if ((h->root.type == bfd_link_hash_defined
77 || h->root.type == bfd_link_hash_defweak)
78 && discarded_section (h->root.u.def.section))
79 return h->root.u.def.section;
80 else
81 return NULL;
82 }
83 else
84 {
85 /* It's not a relocation against a global symbol,
86 but it could be a relocation against a local
87 symbol for a discarded section. */
88 asection *isec;
89 Elf_Internal_Sym *isym;
90
91 /* Need to: get the symbol; get the section. */
92 isym = &cookie->locsyms[r_symndx];
93 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
94 if (isec != NULL
95 && discard ? discarded_section (isec) : 1)
96 return isec;
97 }
98 return NULL;
99 }
100
101 /* Define a symbol in a dynamic linkage section. */
102
103 struct elf_link_hash_entry *
104 _bfd_elf_define_linkage_sym (bfd *abfd,
105 struct bfd_link_info *info,
106 asection *sec,
107 const char *name)
108 {
109 struct elf_link_hash_entry *h;
110 struct bfd_link_hash_entry *bh;
111 const struct elf_backend_data *bed;
112
113 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
114 if (h != NULL)
115 {
116 /* Zap symbol defined in an as-needed lib that wasn't linked.
117 This is a symptom of a larger problem: Absolute symbols
118 defined in shared libraries can't be overridden, because we
119 lose the link to the bfd which is via the symbol section. */
120 h->root.type = bfd_link_hash_new;
121 bh = &h->root;
122 }
123 else
124 bh = NULL;
125
126 bed = get_elf_backend_data (abfd);
127 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
128 sec, 0, NULL, FALSE, bed->collect,
129 &bh))
130 return NULL;
131 h = (struct elf_link_hash_entry *) bh;
132 BFD_ASSERT (h != NULL);
133 h->def_regular = 1;
134 h->non_elf = 0;
135 h->root.linker_def = 1;
136 h->type = STT_OBJECT;
137 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
138 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
139
140 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
141 return h;
142 }
143
144 bfd_boolean
145 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
146 {
147 flagword flags;
148 asection *s;
149 struct elf_link_hash_entry *h;
150 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
151 struct elf_link_hash_table *htab = elf_hash_table (info);
152
153 /* This function may be called more than once. */
154 if (htab->sgot != NULL)
155 return TRUE;
156
157 flags = bed->dynamic_sec_flags;
158
159 s = bfd_make_section_anyway_with_flags (abfd,
160 (bed->rela_plts_and_copies_p
161 ? ".rela.got" : ".rel.got"),
162 (bed->dynamic_sec_flags
163 | SEC_READONLY));
164 if (s == NULL
165 || !bfd_set_section_alignment (s, bed->s->log_file_align))
166 return FALSE;
167 htab->srelgot = s;
168
169 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
170 if (s == NULL
171 || !bfd_set_section_alignment (s, bed->s->log_file_align))
172 return FALSE;
173 htab->sgot = s;
174
175 if (bed->want_got_plt)
176 {
177 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
178 if (s == NULL
179 || !bfd_set_section_alignment (s, bed->s->log_file_align))
180 return FALSE;
181 htab->sgotplt = s;
182 }
183
184 /* The first bit of the global offset table is the header. */
185 s->size += bed->got_header_size;
186
187 if (bed->want_got_sym)
188 {
189 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
190 (or .got.plt) section. We don't do this in the linker script
191 because we don't want to define the symbol if we are not creating
192 a global offset table. */
193 h = _bfd_elf_define_linkage_sym (abfd, info, s,
194 "_GLOBAL_OFFSET_TABLE_");
195 elf_hash_table (info)->hgot = h;
196 if (h == NULL)
197 return FALSE;
198 }
199
200 return TRUE;
201 }
202 \f
203 /* Create a strtab to hold the dynamic symbol names. */
204 static bfd_boolean
205 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
206 {
207 struct elf_link_hash_table *hash_table;
208
209 hash_table = elf_hash_table (info);
210 if (hash_table->dynobj == NULL)
211 {
212 /* We may not set dynobj, an input file holding linker created
213 dynamic sections to abfd, which may be a dynamic object with
214 its own dynamic sections. We need to find a normal input file
215 to hold linker created sections if possible. */
216 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
217 {
218 bfd *ibfd;
219 asection *s;
220 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
221 if ((ibfd->flags
222 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
223 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
224 && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
225 && !((s = ibfd->sections) != NULL
226 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
227 {
228 abfd = ibfd;
229 break;
230 }
231 }
232 hash_table->dynobj = abfd;
233 }
234
235 if (hash_table->dynstr == NULL)
236 {
237 hash_table->dynstr = _bfd_elf_strtab_init ();
238 if (hash_table->dynstr == NULL)
239 return FALSE;
240 }
241 return TRUE;
242 }
243
244 /* Create some sections which will be filled in with dynamic linking
245 information. ABFD is an input file which requires dynamic sections
246 to be created. The dynamic sections take up virtual memory space
247 when the final executable is run, so we need to create them before
248 addresses are assigned to the output sections. We work out the
249 actual contents and size of these sections later. */
250
251 bfd_boolean
252 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
253 {
254 flagword flags;
255 asection *s;
256 const struct elf_backend_data *bed;
257 struct elf_link_hash_entry *h;
258
259 if (! is_elf_hash_table (info->hash))
260 return FALSE;
261
262 if (elf_hash_table (info)->dynamic_sections_created)
263 return TRUE;
264
265 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
266 return FALSE;
267
268 abfd = elf_hash_table (info)->dynobj;
269 bed = get_elf_backend_data (abfd);
270
271 flags = bed->dynamic_sec_flags;
272
273 /* A dynamically linked executable has a .interp section, but a
274 shared library does not. */
275 if (bfd_link_executable (info) && !info->nointerp)
276 {
277 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
278 flags | SEC_READONLY);
279 if (s == NULL)
280 return FALSE;
281 }
282
283 /* Create sections to hold version informations. These are removed
284 if they are not needed. */
285 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
286 flags | SEC_READONLY);
287 if (s == NULL
288 || !bfd_set_section_alignment (s, bed->s->log_file_align))
289 return FALSE;
290
291 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
292 flags | SEC_READONLY);
293 if (s == NULL
294 || !bfd_set_section_alignment (s, 1))
295 return FALSE;
296
297 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
298 flags | SEC_READONLY);
299 if (s == NULL
300 || !bfd_set_section_alignment (s, bed->s->log_file_align))
301 return FALSE;
302
303 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
304 flags | SEC_READONLY);
305 if (s == NULL
306 || !bfd_set_section_alignment (s, bed->s->log_file_align))
307 return FALSE;
308 elf_hash_table (info)->dynsym = s;
309
310 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
311 flags | SEC_READONLY);
312 if (s == NULL)
313 return FALSE;
314
315 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
316 if (s == NULL
317 || !bfd_set_section_alignment (s, bed->s->log_file_align))
318 return FALSE;
319
320 /* The special symbol _DYNAMIC is always set to the start of the
321 .dynamic section. We could set _DYNAMIC in a linker script, but we
322 only want to define it if we are, in fact, creating a .dynamic
323 section. We don't want to define it if there is no .dynamic
324 section, since on some ELF platforms the start up code examines it
325 to decide how to initialize the process. */
326 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
327 elf_hash_table (info)->hdynamic = h;
328 if (h == NULL)
329 return FALSE;
330
331 if (info->emit_hash)
332 {
333 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
334 flags | SEC_READONLY);
335 if (s == NULL
336 || !bfd_set_section_alignment (s, bed->s->log_file_align))
337 return FALSE;
338 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
339 }
340
341 if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL)
342 {
343 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
344 flags | SEC_READONLY);
345 if (s == NULL
346 || !bfd_set_section_alignment (s, bed->s->log_file_align))
347 return FALSE;
348 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
349 4 32-bit words followed by variable count of 64-bit words, then
350 variable count of 32-bit words. */
351 if (bed->s->arch_size == 64)
352 elf_section_data (s)->this_hdr.sh_entsize = 0;
353 else
354 elf_section_data (s)->this_hdr.sh_entsize = 4;
355 }
356
357 /* Let the backend create the rest of the sections. This lets the
358 backend set the right flags. The backend will normally create
359 the .got and .plt sections. */
360 if (bed->elf_backend_create_dynamic_sections == NULL
361 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
362 return FALSE;
363
364 elf_hash_table (info)->dynamic_sections_created = TRUE;
365
366 return TRUE;
367 }
368
369 /* Create dynamic sections when linking against a dynamic object. */
370
371 bfd_boolean
372 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
373 {
374 flagword flags, pltflags;
375 struct elf_link_hash_entry *h;
376 asection *s;
377 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
378 struct elf_link_hash_table *htab = elf_hash_table (info);
379
380 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
381 .rel[a].bss sections. */
382 flags = bed->dynamic_sec_flags;
383
384 pltflags = flags;
385 if (bed->plt_not_loaded)
386 /* We do not clear SEC_ALLOC here because we still want the OS to
387 allocate space for the section; it's just that there's nothing
388 to read in from the object file. */
389 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
390 else
391 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
392 if (bed->plt_readonly)
393 pltflags |= SEC_READONLY;
394
395 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
396 if (s == NULL
397 || !bfd_set_section_alignment (s, bed->plt_alignment))
398 return FALSE;
399 htab->splt = s;
400
401 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
402 .plt section. */
403 if (bed->want_plt_sym)
404 {
405 h = _bfd_elf_define_linkage_sym (abfd, info, s,
406 "_PROCEDURE_LINKAGE_TABLE_");
407 elf_hash_table (info)->hplt = h;
408 if (h == NULL)
409 return FALSE;
410 }
411
412 s = bfd_make_section_anyway_with_flags (abfd,
413 (bed->rela_plts_and_copies_p
414 ? ".rela.plt" : ".rel.plt"),
415 flags | SEC_READONLY);
416 if (s == NULL
417 || !bfd_set_section_alignment (s, bed->s->log_file_align))
418 return FALSE;
419 htab->srelplt = s;
420
421 if (! _bfd_elf_create_got_section (abfd, info))
422 return FALSE;
423
424 if (bed->want_dynbss)
425 {
426 /* The .dynbss section is a place to put symbols which are defined
427 by dynamic objects, are referenced by regular objects, and are
428 not functions. We must allocate space for them in the process
429 image and use a R_*_COPY reloc to tell the dynamic linker to
430 initialize them at run time. The linker script puts the .dynbss
431 section into the .bss section of the final image. */
432 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
433 SEC_ALLOC | SEC_LINKER_CREATED);
434 if (s == NULL)
435 return FALSE;
436 htab->sdynbss = s;
437
438 if (bed->want_dynrelro)
439 {
440 /* Similarly, but for symbols that were originally in read-only
441 sections. This section doesn't really need to have contents,
442 but make it like other .data.rel.ro sections. */
443 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
444 flags);
445 if (s == NULL)
446 return FALSE;
447 htab->sdynrelro = s;
448 }
449
450 /* The .rel[a].bss section holds copy relocs. This section is not
451 normally needed. We need to create it here, though, so that the
452 linker will map it to an output section. We can't just create it
453 only if we need it, because we will not know whether we need it
454 until we have seen all the input files, and the first time the
455 main linker code calls BFD after examining all the input files
456 (size_dynamic_sections) the input sections have already been
457 mapped to the output sections. If the section turns out not to
458 be needed, we can discard it later. We will never need this
459 section when generating a shared object, since they do not use
460 copy relocs. */
461 if (bfd_link_executable (info))
462 {
463 s = bfd_make_section_anyway_with_flags (abfd,
464 (bed->rela_plts_and_copies_p
465 ? ".rela.bss" : ".rel.bss"),
466 flags | SEC_READONLY);
467 if (s == NULL
468 || !bfd_set_section_alignment (s, bed->s->log_file_align))
469 return FALSE;
470 htab->srelbss = s;
471
472 if (bed->want_dynrelro)
473 {
474 s = (bfd_make_section_anyway_with_flags
475 (abfd, (bed->rela_plts_and_copies_p
476 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
477 flags | SEC_READONLY));
478 if (s == NULL
479 || !bfd_set_section_alignment (s, bed->s->log_file_align))
480 return FALSE;
481 htab->sreldynrelro = s;
482 }
483 }
484 }
485
486 return TRUE;
487 }
488 \f
489 /* Record a new dynamic symbol. We record the dynamic symbols as we
490 read the input files, since we need to have a list of all of them
491 before we can determine the final sizes of the output sections.
492 Note that we may actually call this function even though we are not
493 going to output any dynamic symbols; in some cases we know that a
494 symbol should be in the dynamic symbol table, but only if there is
495 one. */
496
497 bfd_boolean
498 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
499 struct elf_link_hash_entry *h)
500 {
501 if (h->dynindx == -1)
502 {
503 struct elf_strtab_hash *dynstr;
504 char *p;
505 const char *name;
506 size_t indx;
507
508 /* XXX: The ABI draft says the linker must turn hidden and
509 internal symbols into STB_LOCAL symbols when producing the
510 DSO. However, if ld.so honors st_other in the dynamic table,
511 this would not be necessary. */
512 switch (ELF_ST_VISIBILITY (h->other))
513 {
514 case STV_INTERNAL:
515 case STV_HIDDEN:
516 if (h->root.type != bfd_link_hash_undefined
517 && h->root.type != bfd_link_hash_undefweak)
518 {
519 h->forced_local = 1;
520 if (!elf_hash_table (info)->is_relocatable_executable)
521 return TRUE;
522 }
523
524 default:
525 break;
526 }
527
528 h->dynindx = elf_hash_table (info)->dynsymcount;
529 ++elf_hash_table (info)->dynsymcount;
530
531 dynstr = elf_hash_table (info)->dynstr;
532 if (dynstr == NULL)
533 {
534 /* Create a strtab to hold the dynamic symbol names. */
535 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
536 if (dynstr == NULL)
537 return FALSE;
538 }
539
540 /* We don't put any version information in the dynamic string
541 table. */
542 name = h->root.root.string;
543 p = strchr (name, ELF_VER_CHR);
544 if (p != NULL)
545 /* We know that the p points into writable memory. In fact,
546 there are only a few symbols that have read-only names, being
547 those like _GLOBAL_OFFSET_TABLE_ that are created specially
548 by the backends. Most symbols will have names pointing into
549 an ELF string table read from a file, or to objalloc memory. */
550 *p = 0;
551
552 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
553
554 if (p != NULL)
555 *p = ELF_VER_CHR;
556
557 if (indx == (size_t) -1)
558 return FALSE;
559 h->dynstr_index = indx;
560 }
561
562 return TRUE;
563 }
564 \f
565 /* Mark a symbol dynamic. */
566
567 static void
568 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
569 struct elf_link_hash_entry *h,
570 Elf_Internal_Sym *sym)
571 {
572 struct bfd_elf_dynamic_list *d = info->dynamic_list;
573
574 /* It may be called more than once on the same H. */
575 if(h->dynamic || bfd_link_relocatable (info))
576 return;
577
578 if ((info->dynamic_data
579 && (h->type == STT_OBJECT
580 || h->type == STT_COMMON
581 || (sym != NULL
582 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
583 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
584 || (d != NULL
585 && h->non_elf
586 && (*d->match) (&d->head, NULL, h->root.root.string)))
587 {
588 h->dynamic = 1;
589 /* NB: If a symbol is made dynamic by --dynamic-list, it has
590 non-IR reference. */
591 h->root.non_ir_ref_dynamic = 1;
592 }
593 }
594
595 /* Record an assignment to a symbol made by a linker script. We need
596 this in case some dynamic object refers to this symbol. */
597
598 bfd_boolean
599 bfd_elf_record_link_assignment (bfd *output_bfd,
600 struct bfd_link_info *info,
601 const char *name,
602 bfd_boolean provide,
603 bfd_boolean hidden)
604 {
605 struct elf_link_hash_entry *h, *hv;
606 struct elf_link_hash_table *htab;
607 const struct elf_backend_data *bed;
608
609 if (!is_elf_hash_table (info->hash))
610 return TRUE;
611
612 htab = elf_hash_table (info);
613 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
614 if (h == NULL)
615 return provide;
616
617 if (h->root.type == bfd_link_hash_warning)
618 h = (struct elf_link_hash_entry *) h->root.u.i.link;
619
620 if (h->versioned == unknown)
621 {
622 /* Set versioned if symbol version is unknown. */
623 char *version = strrchr (name, ELF_VER_CHR);
624 if (version)
625 {
626 if (version > name && version[-1] != ELF_VER_CHR)
627 h->versioned = versioned_hidden;
628 else
629 h->versioned = versioned;
630 }
631 }
632
633 /* Symbols defined in a linker script but not referenced anywhere
634 else will have non_elf set. */
635 if (h->non_elf)
636 {
637 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
638 h->non_elf = 0;
639 }
640
641 switch (h->root.type)
642 {
643 case bfd_link_hash_defined:
644 case bfd_link_hash_defweak:
645 case bfd_link_hash_common:
646 break;
647 case bfd_link_hash_undefweak:
648 case bfd_link_hash_undefined:
649 /* Since we're defining the symbol, don't let it seem to have not
650 been defined. record_dynamic_symbol and size_dynamic_sections
651 may depend on this. */
652 h->root.type = bfd_link_hash_new;
653 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
654 bfd_link_repair_undef_list (&htab->root);
655 break;
656 case bfd_link_hash_new:
657 break;
658 case bfd_link_hash_indirect:
659 /* We had a versioned symbol in a dynamic library. We make the
660 the versioned symbol point to this one. */
661 bed = get_elf_backend_data (output_bfd);
662 hv = h;
663 while (hv->root.type == bfd_link_hash_indirect
664 || hv->root.type == bfd_link_hash_warning)
665 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
666 /* We don't need to update h->root.u since linker will set them
667 later. */
668 h->root.type = bfd_link_hash_undefined;
669 hv->root.type = bfd_link_hash_indirect;
670 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
671 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
672 break;
673 default:
674 BFD_FAIL ();
675 return FALSE;
676 }
677
678 /* If this symbol is being provided by the linker script, and it is
679 currently defined by a dynamic object, but not by a regular
680 object, then mark it as undefined so that the generic linker will
681 force the correct value. */
682 if (provide
683 && h->def_dynamic
684 && !h->def_regular)
685 h->root.type = bfd_link_hash_undefined;
686
687 /* If this symbol is currently defined by a dynamic object, but not
688 by a regular object, then clear out any version information because
689 the symbol will not be associated with the dynamic object any
690 more. */
691 if (h->def_dynamic && !h->def_regular)
692 h->verinfo.verdef = NULL;
693
694 /* Make sure this symbol is not garbage collected. */
695 h->mark = 1;
696
697 h->def_regular = 1;
698
699 if (hidden)
700 {
701 bed = get_elf_backend_data (output_bfd);
702 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
703 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
704 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
705 }
706
707 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
708 and executables. */
709 if (!bfd_link_relocatable (info)
710 && h->dynindx != -1
711 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
712 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
713 h->forced_local = 1;
714
715 if ((h->def_dynamic
716 || h->ref_dynamic
717 || bfd_link_dll (info)
718 || elf_hash_table (info)->is_relocatable_executable)
719 && !h->forced_local
720 && h->dynindx == -1)
721 {
722 if (! bfd_elf_link_record_dynamic_symbol (info, h))
723 return FALSE;
724
725 /* If this is a weak defined symbol, and we know a corresponding
726 real symbol from the same dynamic object, make sure the real
727 symbol is also made into a dynamic symbol. */
728 if (h->is_weakalias)
729 {
730 struct elf_link_hash_entry *def = weakdef (h);
731
732 if (def->dynindx == -1
733 && !bfd_elf_link_record_dynamic_symbol (info, def))
734 return FALSE;
735 }
736 }
737
738 return TRUE;
739 }
740
741 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
742 success, and 2 on a failure caused by attempting to record a symbol
743 in a discarded section, eg. a discarded link-once section symbol. */
744
745 int
746 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
747 bfd *input_bfd,
748 long input_indx)
749 {
750 size_t amt;
751 struct elf_link_local_dynamic_entry *entry;
752 struct elf_link_hash_table *eht;
753 struct elf_strtab_hash *dynstr;
754 size_t dynstr_index;
755 char *name;
756 Elf_External_Sym_Shndx eshndx;
757 char esym[sizeof (Elf64_External_Sym)];
758
759 if (! is_elf_hash_table (info->hash))
760 return 0;
761
762 /* See if the entry exists already. */
763 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
764 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
765 return 1;
766
767 amt = sizeof (*entry);
768 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
769 if (entry == NULL)
770 return 0;
771
772 /* Go find the symbol, so that we can find it's name. */
773 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
774 1, input_indx, &entry->isym, esym, &eshndx))
775 {
776 bfd_release (input_bfd, entry);
777 return 0;
778 }
779
780 if (entry->isym.st_shndx != SHN_UNDEF
781 && entry->isym.st_shndx < SHN_LORESERVE)
782 {
783 asection *s;
784
785 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
786 if (s == NULL || bfd_is_abs_section (s->output_section))
787 {
788 /* We can still bfd_release here as nothing has done another
789 bfd_alloc. We can't do this later in this function. */
790 bfd_release (input_bfd, entry);
791 return 2;
792 }
793 }
794
795 name = (bfd_elf_string_from_elf_section
796 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
797 entry->isym.st_name));
798
799 dynstr = elf_hash_table (info)->dynstr;
800 if (dynstr == NULL)
801 {
802 /* Create a strtab to hold the dynamic symbol names. */
803 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
804 if (dynstr == NULL)
805 return 0;
806 }
807
808 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
809 if (dynstr_index == (size_t) -1)
810 return 0;
811 entry->isym.st_name = dynstr_index;
812
813 eht = elf_hash_table (info);
814
815 entry->next = eht->dynlocal;
816 eht->dynlocal = entry;
817 entry->input_bfd = input_bfd;
818 entry->input_indx = input_indx;
819 eht->dynsymcount++;
820
821 /* Whatever binding the symbol had before, it's now local. */
822 entry->isym.st_info
823 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
824
825 /* The dynindx will be set at the end of size_dynamic_sections. */
826
827 return 1;
828 }
829
830 /* Return the dynindex of a local dynamic symbol. */
831
832 long
833 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
834 bfd *input_bfd,
835 long input_indx)
836 {
837 struct elf_link_local_dynamic_entry *e;
838
839 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
840 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
841 return e->dynindx;
842 return -1;
843 }
844
845 /* This function is used to renumber the dynamic symbols, if some of
846 them are removed because they are marked as local. This is called
847 via elf_link_hash_traverse. */
848
849 static bfd_boolean
850 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
851 void *data)
852 {
853 size_t *count = (size_t *) data;
854
855 if (h->forced_local)
856 return TRUE;
857
858 if (h->dynindx != -1)
859 h->dynindx = ++(*count);
860
861 return TRUE;
862 }
863
864
865 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
866 STB_LOCAL binding. */
867
868 static bfd_boolean
869 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
870 void *data)
871 {
872 size_t *count = (size_t *) data;
873
874 if (!h->forced_local)
875 return TRUE;
876
877 if (h->dynindx != -1)
878 h->dynindx = ++(*count);
879
880 return TRUE;
881 }
882
883 /* Return true if the dynamic symbol for a given section should be
884 omitted when creating a shared library. */
885 bfd_boolean
886 _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
887 struct bfd_link_info *info,
888 asection *p)
889 {
890 struct elf_link_hash_table *htab;
891 asection *ip;
892
893 switch (elf_section_data (p)->this_hdr.sh_type)
894 {
895 case SHT_PROGBITS:
896 case SHT_NOBITS:
897 /* If sh_type is yet undecided, assume it could be
898 SHT_PROGBITS/SHT_NOBITS. */
899 case SHT_NULL:
900 htab = elf_hash_table (info);
901 if (htab->text_index_section != NULL)
902 return p != htab->text_index_section && p != htab->data_index_section;
903
904 return (htab->dynobj != NULL
905 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
906 && ip->output_section == p);
907
908 /* There shouldn't be section relative relocations
909 against any other section. */
910 default:
911 return TRUE;
912 }
913 }
914
915 bfd_boolean
916 _bfd_elf_omit_section_dynsym_all
917 (bfd *output_bfd ATTRIBUTE_UNUSED,
918 struct bfd_link_info *info ATTRIBUTE_UNUSED,
919 asection *p ATTRIBUTE_UNUSED)
920 {
921 return TRUE;
922 }
923
924 /* Assign dynsym indices. In a shared library we generate a section
925 symbol for each output section, which come first. Next come symbols
926 which have been forced to local binding. Then all of the back-end
927 allocated local dynamic syms, followed by the rest of the global
928 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
929 (This prevents the early call before elf_backend_init_index_section
930 and strip_excluded_output_sections setting dynindx for sections
931 that are stripped.) */
932
933 static unsigned long
934 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
935 struct bfd_link_info *info,
936 unsigned long *section_sym_count)
937 {
938 unsigned long dynsymcount = 0;
939 bfd_boolean do_sec = section_sym_count != NULL;
940
941 if (bfd_link_pic (info)
942 || elf_hash_table (info)->is_relocatable_executable)
943 {
944 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
945 asection *p;
946 for (p = output_bfd->sections; p ; p = p->next)
947 if ((p->flags & SEC_EXCLUDE) == 0
948 && (p->flags & SEC_ALLOC) != 0
949 && elf_hash_table (info)->dynamic_relocs
950 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
951 {
952 ++dynsymcount;
953 if (do_sec)
954 elf_section_data (p)->dynindx = dynsymcount;
955 }
956 else if (do_sec)
957 elf_section_data (p)->dynindx = 0;
958 }
959 if (do_sec)
960 *section_sym_count = dynsymcount;
961
962 elf_link_hash_traverse (elf_hash_table (info),
963 elf_link_renumber_local_hash_table_dynsyms,
964 &dynsymcount);
965
966 if (elf_hash_table (info)->dynlocal)
967 {
968 struct elf_link_local_dynamic_entry *p;
969 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
970 p->dynindx = ++dynsymcount;
971 }
972 elf_hash_table (info)->local_dynsymcount = dynsymcount;
973
974 elf_link_hash_traverse (elf_hash_table (info),
975 elf_link_renumber_hash_table_dynsyms,
976 &dynsymcount);
977
978 /* There is an unused NULL entry at the head of the table which we
979 must account for in our count even if the table is empty since it
980 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
981 .dynamic section. */
982 dynsymcount++;
983
984 elf_hash_table (info)->dynsymcount = dynsymcount;
985 return dynsymcount;
986 }
987
988 /* Merge st_other field. */
989
990 static void
991 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
992 const Elf_Internal_Sym *isym, asection *sec,
993 bfd_boolean definition, bfd_boolean dynamic)
994 {
995 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
996
997 /* If st_other has a processor-specific meaning, specific
998 code might be needed here. */
999 if (bed->elf_backend_merge_symbol_attribute)
1000 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
1001 dynamic);
1002
1003 if (!dynamic)
1004 {
1005 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
1006 unsigned hvis = ELF_ST_VISIBILITY (h->other);
1007
1008 /* Keep the most constraining visibility. Leave the remainder
1009 of the st_other field to elf_backend_merge_symbol_attribute. */
1010 if (symvis - 1 < hvis - 1)
1011 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1012 }
1013 else if (definition
1014 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
1015 && (sec->flags & SEC_READONLY) == 0)
1016 h->protected_def = 1;
1017 }
1018
1019 /* This function is called when we want to merge a new symbol with an
1020 existing symbol. It handles the various cases which arise when we
1021 find a definition in a dynamic object, or when there is already a
1022 definition in a dynamic object. The new symbol is described by
1023 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1024 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1025 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1026 of an old common symbol. We set OVERRIDE if the old symbol is
1027 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1028 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1029 to change. By OK to change, we mean that we shouldn't warn if the
1030 type or size does change. */
1031
1032 static bfd_boolean
1033 _bfd_elf_merge_symbol (bfd *abfd,
1034 struct bfd_link_info *info,
1035 const char *name,
1036 Elf_Internal_Sym *sym,
1037 asection **psec,
1038 bfd_vma *pvalue,
1039 struct elf_link_hash_entry **sym_hash,
1040 bfd **poldbfd,
1041 bfd_boolean *pold_weak,
1042 unsigned int *pold_alignment,
1043 bfd_boolean *skip,
1044 bfd_boolean *override,
1045 bfd_boolean *type_change_ok,
1046 bfd_boolean *size_change_ok,
1047 bfd_boolean *matched)
1048 {
1049 asection *sec, *oldsec;
1050 struct elf_link_hash_entry *h;
1051 struct elf_link_hash_entry *hi;
1052 struct elf_link_hash_entry *flip;
1053 int bind;
1054 bfd *oldbfd;
1055 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1056 bfd_boolean newweak, oldweak, newfunc, oldfunc;
1057 const struct elf_backend_data *bed;
1058 char *new_version;
1059 bfd_boolean default_sym = *matched;
1060
1061 *skip = FALSE;
1062 *override = FALSE;
1063
1064 sec = *psec;
1065 bind = ELF_ST_BIND (sym->st_info);
1066
1067 if (! bfd_is_und_section (sec))
1068 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1069 else
1070 h = ((struct elf_link_hash_entry *)
1071 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1072 if (h == NULL)
1073 return FALSE;
1074 *sym_hash = h;
1075
1076 bed = get_elf_backend_data (abfd);
1077
1078 /* NEW_VERSION is the symbol version of the new symbol. */
1079 if (h->versioned != unversioned)
1080 {
1081 /* Symbol version is unknown or versioned. */
1082 new_version = strrchr (name, ELF_VER_CHR);
1083 if (new_version)
1084 {
1085 if (h->versioned == unknown)
1086 {
1087 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1088 h->versioned = versioned_hidden;
1089 else
1090 h->versioned = versioned;
1091 }
1092 new_version += 1;
1093 if (new_version[0] == '\0')
1094 new_version = NULL;
1095 }
1096 else
1097 h->versioned = unversioned;
1098 }
1099 else
1100 new_version = NULL;
1101
1102 /* For merging, we only care about real symbols. But we need to make
1103 sure that indirect symbol dynamic flags are updated. */
1104 hi = h;
1105 while (h->root.type == bfd_link_hash_indirect
1106 || h->root.type == bfd_link_hash_warning)
1107 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1108
1109 if (!*matched)
1110 {
1111 if (hi == h || h->root.type == bfd_link_hash_new)
1112 *matched = TRUE;
1113 else
1114 {
1115 /* OLD_HIDDEN is true if the existing symbol is only visible
1116 to the symbol with the same symbol version. NEW_HIDDEN is
1117 true if the new symbol is only visible to the symbol with
1118 the same symbol version. */
1119 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1120 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1121 if (!old_hidden && !new_hidden)
1122 /* The new symbol matches the existing symbol if both
1123 aren't hidden. */
1124 *matched = TRUE;
1125 else
1126 {
1127 /* OLD_VERSION is the symbol version of the existing
1128 symbol. */
1129 char *old_version;
1130
1131 if (h->versioned >= versioned)
1132 old_version = strrchr (h->root.root.string,
1133 ELF_VER_CHR) + 1;
1134 else
1135 old_version = NULL;
1136
1137 /* The new symbol matches the existing symbol if they
1138 have the same symbol version. */
1139 *matched = (old_version == new_version
1140 || (old_version != NULL
1141 && new_version != NULL
1142 && strcmp (old_version, new_version) == 0));
1143 }
1144 }
1145 }
1146
1147 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1148 existing symbol. */
1149
1150 oldbfd = NULL;
1151 oldsec = NULL;
1152 switch (h->root.type)
1153 {
1154 default:
1155 break;
1156
1157 case bfd_link_hash_undefined:
1158 case bfd_link_hash_undefweak:
1159 oldbfd = h->root.u.undef.abfd;
1160 break;
1161
1162 case bfd_link_hash_defined:
1163 case bfd_link_hash_defweak:
1164 oldbfd = h->root.u.def.section->owner;
1165 oldsec = h->root.u.def.section;
1166 break;
1167
1168 case bfd_link_hash_common:
1169 oldbfd = h->root.u.c.p->section->owner;
1170 oldsec = h->root.u.c.p->section;
1171 if (pold_alignment)
1172 *pold_alignment = h->root.u.c.p->alignment_power;
1173 break;
1174 }
1175 if (poldbfd && *poldbfd == NULL)
1176 *poldbfd = oldbfd;
1177
1178 /* Differentiate strong and weak symbols. */
1179 newweak = bind == STB_WEAK;
1180 oldweak = (h->root.type == bfd_link_hash_defweak
1181 || h->root.type == bfd_link_hash_undefweak);
1182 if (pold_weak)
1183 *pold_weak = oldweak;
1184
1185 /* We have to check it for every instance since the first few may be
1186 references and not all compilers emit symbol type for undefined
1187 symbols. */
1188 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1189
1190 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1191 respectively, is from a dynamic object. */
1192
1193 newdyn = (abfd->flags & DYNAMIC) != 0;
1194
1195 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1196 syms and defined syms in dynamic libraries respectively.
1197 ref_dynamic on the other hand can be set for a symbol defined in
1198 a dynamic library, and def_dynamic may not be set; When the
1199 definition in a dynamic lib is overridden by a definition in the
1200 executable use of the symbol in the dynamic lib becomes a
1201 reference to the executable symbol. */
1202 if (newdyn)
1203 {
1204 if (bfd_is_und_section (sec))
1205 {
1206 if (bind != STB_WEAK)
1207 {
1208 h->ref_dynamic_nonweak = 1;
1209 hi->ref_dynamic_nonweak = 1;
1210 }
1211 }
1212 else
1213 {
1214 /* Update the existing symbol only if they match. */
1215 if (*matched)
1216 h->dynamic_def = 1;
1217 hi->dynamic_def = 1;
1218 }
1219 }
1220
1221 /* If we just created the symbol, mark it as being an ELF symbol.
1222 Other than that, there is nothing to do--there is no merge issue
1223 with a newly defined symbol--so we just return. */
1224
1225 if (h->root.type == bfd_link_hash_new)
1226 {
1227 h->non_elf = 0;
1228 return TRUE;
1229 }
1230
1231 /* In cases involving weak versioned symbols, we may wind up trying
1232 to merge a symbol with itself. Catch that here, to avoid the
1233 confusion that results if we try to override a symbol with
1234 itself. The additional tests catch cases like
1235 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1236 dynamic object, which we do want to handle here. */
1237 if (abfd == oldbfd
1238 && (newweak || oldweak)
1239 && ((abfd->flags & DYNAMIC) == 0
1240 || !h->def_regular))
1241 return TRUE;
1242
1243 olddyn = FALSE;
1244 if (oldbfd != NULL)
1245 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1246 else if (oldsec != NULL)
1247 {
1248 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1249 indices used by MIPS ELF. */
1250 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1251 }
1252
1253 /* Handle a case where plugin_notice won't be called and thus won't
1254 set the non_ir_ref flags on the first pass over symbols. */
1255 if (oldbfd != NULL
1256 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1257 && newdyn != olddyn)
1258 {
1259 h->root.non_ir_ref_dynamic = TRUE;
1260 hi->root.non_ir_ref_dynamic = TRUE;
1261 }
1262
1263 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1264 respectively, appear to be a definition rather than reference. */
1265
1266 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1267
1268 olddef = (h->root.type != bfd_link_hash_undefined
1269 && h->root.type != bfd_link_hash_undefweak
1270 && h->root.type != bfd_link_hash_common);
1271
1272 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1273 respectively, appear to be a function. */
1274
1275 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1276 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1277
1278 oldfunc = (h->type != STT_NOTYPE
1279 && bed->is_function_type (h->type));
1280
1281 if (!(newfunc && oldfunc)
1282 && ELF_ST_TYPE (sym->st_info) != h->type
1283 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1284 && h->type != STT_NOTYPE
1285 && (newdef || bfd_is_com_section (sec))
1286 && (olddef || h->root.type == bfd_link_hash_common))
1287 {
1288 /* If creating a default indirect symbol ("foo" or "foo@") from
1289 a dynamic versioned definition ("foo@@") skip doing so if
1290 there is an existing regular definition with a different
1291 type. We don't want, for example, a "time" variable in the
1292 executable overriding a "time" function in a shared library. */
1293 if (newdyn
1294 && !olddyn)
1295 {
1296 *skip = TRUE;
1297 return TRUE;
1298 }
1299
1300 /* When adding a symbol from a regular object file after we have
1301 created indirect symbols, undo the indirection and any
1302 dynamic state. */
1303 if (hi != h
1304 && !newdyn
1305 && olddyn)
1306 {
1307 h = hi;
1308 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1309 h->forced_local = 0;
1310 h->ref_dynamic = 0;
1311 h->def_dynamic = 0;
1312 h->dynamic_def = 0;
1313 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1314 {
1315 h->root.type = bfd_link_hash_undefined;
1316 h->root.u.undef.abfd = abfd;
1317 }
1318 else
1319 {
1320 h->root.type = bfd_link_hash_new;
1321 h->root.u.undef.abfd = NULL;
1322 }
1323 return TRUE;
1324 }
1325 }
1326
1327 /* Check TLS symbols. We don't check undefined symbols introduced
1328 by "ld -u" which have no type (and oldbfd NULL), and we don't
1329 check symbols from plugins because they also have no type. */
1330 if (oldbfd != NULL
1331 && (oldbfd->flags & BFD_PLUGIN) == 0
1332 && (abfd->flags & BFD_PLUGIN) == 0
1333 && ELF_ST_TYPE (sym->st_info) != h->type
1334 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1335 {
1336 bfd *ntbfd, *tbfd;
1337 bfd_boolean ntdef, tdef;
1338 asection *ntsec, *tsec;
1339
1340 if (h->type == STT_TLS)
1341 {
1342 ntbfd = abfd;
1343 ntsec = sec;
1344 ntdef = newdef;
1345 tbfd = oldbfd;
1346 tsec = oldsec;
1347 tdef = olddef;
1348 }
1349 else
1350 {
1351 ntbfd = oldbfd;
1352 ntsec = oldsec;
1353 ntdef = olddef;
1354 tbfd = abfd;
1355 tsec = sec;
1356 tdef = newdef;
1357 }
1358
1359 if (tdef && ntdef)
1360 _bfd_error_handler
1361 /* xgettext:c-format */
1362 (_("%s: TLS definition in %pB section %pA "
1363 "mismatches non-TLS definition in %pB section %pA"),
1364 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1365 else if (!tdef && !ntdef)
1366 _bfd_error_handler
1367 /* xgettext:c-format */
1368 (_("%s: TLS reference in %pB "
1369 "mismatches non-TLS reference in %pB"),
1370 h->root.root.string, tbfd, ntbfd);
1371 else if (tdef)
1372 _bfd_error_handler
1373 /* xgettext:c-format */
1374 (_("%s: TLS definition in %pB section %pA "
1375 "mismatches non-TLS reference in %pB"),
1376 h->root.root.string, tbfd, tsec, ntbfd);
1377 else
1378 _bfd_error_handler
1379 /* xgettext:c-format */
1380 (_("%s: TLS reference in %pB "
1381 "mismatches non-TLS definition in %pB section %pA"),
1382 h->root.root.string, tbfd, ntbfd, ntsec);
1383
1384 bfd_set_error (bfd_error_bad_value);
1385 return FALSE;
1386 }
1387
1388 /* If the old symbol has non-default visibility, we ignore the new
1389 definition from a dynamic object. */
1390 if (newdyn
1391 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1392 && !bfd_is_und_section (sec))
1393 {
1394 *skip = TRUE;
1395 /* Make sure this symbol is dynamic. */
1396 h->ref_dynamic = 1;
1397 hi->ref_dynamic = 1;
1398 /* A protected symbol has external availability. Make sure it is
1399 recorded as dynamic.
1400
1401 FIXME: Should we check type and size for protected symbol? */
1402 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1403 return bfd_elf_link_record_dynamic_symbol (info, h);
1404 else
1405 return TRUE;
1406 }
1407 else if (!newdyn
1408 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1409 && h->def_dynamic)
1410 {
1411 /* If the new symbol with non-default visibility comes from a
1412 relocatable file and the old definition comes from a dynamic
1413 object, we remove the old definition. */
1414 if (hi->root.type == bfd_link_hash_indirect)
1415 {
1416 /* Handle the case where the old dynamic definition is
1417 default versioned. We need to copy the symbol info from
1418 the symbol with default version to the normal one if it
1419 was referenced before. */
1420 if (h->ref_regular)
1421 {
1422 hi->root.type = h->root.type;
1423 h->root.type = bfd_link_hash_indirect;
1424 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1425
1426 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1427 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1428 {
1429 /* If the new symbol is hidden or internal, completely undo
1430 any dynamic link state. */
1431 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1432 h->forced_local = 0;
1433 h->ref_dynamic = 0;
1434 }
1435 else
1436 h->ref_dynamic = 1;
1437
1438 h->def_dynamic = 0;
1439 /* FIXME: Should we check type and size for protected symbol? */
1440 h->size = 0;
1441 h->type = 0;
1442
1443 h = hi;
1444 }
1445 else
1446 h = hi;
1447 }
1448
1449 /* If the old symbol was undefined before, then it will still be
1450 on the undefs list. If the new symbol is undefined or
1451 common, we can't make it bfd_link_hash_new here, because new
1452 undefined or common symbols will be added to the undefs list
1453 by _bfd_generic_link_add_one_symbol. Symbols may not be
1454 added twice to the undefs list. Also, if the new symbol is
1455 undefweak then we don't want to lose the strong undef. */
1456 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1457 {
1458 h->root.type = bfd_link_hash_undefined;
1459 h->root.u.undef.abfd = abfd;
1460 }
1461 else
1462 {
1463 h->root.type = bfd_link_hash_new;
1464 h->root.u.undef.abfd = NULL;
1465 }
1466
1467 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1468 {
1469 /* If the new symbol is hidden or internal, completely undo
1470 any dynamic link state. */
1471 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1472 h->forced_local = 0;
1473 h->ref_dynamic = 0;
1474 }
1475 else
1476 h->ref_dynamic = 1;
1477 h->def_dynamic = 0;
1478 /* FIXME: Should we check type and size for protected symbol? */
1479 h->size = 0;
1480 h->type = 0;
1481 return TRUE;
1482 }
1483
1484 /* If a new weak symbol definition comes from a regular file and the
1485 old symbol comes from a dynamic library, we treat the new one as
1486 strong. Similarly, an old weak symbol definition from a regular
1487 file is treated as strong when the new symbol comes from a dynamic
1488 library. Further, an old weak symbol from a dynamic library is
1489 treated as strong if the new symbol is from a dynamic library.
1490 This reflects the way glibc's ld.so works.
1491
1492 Also allow a weak symbol to override a linker script symbol
1493 defined by an early pass over the script. This is done so the
1494 linker knows the symbol is defined in an object file, for the
1495 DEFINED script function.
1496
1497 Do this before setting *type_change_ok or *size_change_ok so that
1498 we warn properly when dynamic library symbols are overridden. */
1499
1500 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1501 newweak = FALSE;
1502 if (olddef && newdyn)
1503 oldweak = FALSE;
1504
1505 /* Allow changes between different types of function symbol. */
1506 if (newfunc && oldfunc)
1507 *type_change_ok = TRUE;
1508
1509 /* It's OK to change the type if either the existing symbol or the
1510 new symbol is weak. A type change is also OK if the old symbol
1511 is undefined and the new symbol is defined. */
1512
1513 if (oldweak
1514 || newweak
1515 || (newdef
1516 && h->root.type == bfd_link_hash_undefined))
1517 *type_change_ok = TRUE;
1518
1519 /* It's OK to change the size if either the existing symbol or the
1520 new symbol is weak, or if the old symbol is undefined. */
1521
1522 if (*type_change_ok
1523 || h->root.type == bfd_link_hash_undefined)
1524 *size_change_ok = TRUE;
1525
1526 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1527 symbol, respectively, appears to be a common symbol in a dynamic
1528 object. If a symbol appears in an uninitialized section, and is
1529 not weak, and is not a function, then it may be a common symbol
1530 which was resolved when the dynamic object was created. We want
1531 to treat such symbols specially, because they raise special
1532 considerations when setting the symbol size: if the symbol
1533 appears as a common symbol in a regular object, and the size in
1534 the regular object is larger, we must make sure that we use the
1535 larger size. This problematic case can always be avoided in C,
1536 but it must be handled correctly when using Fortran shared
1537 libraries.
1538
1539 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1540 likewise for OLDDYNCOMMON and OLDDEF.
1541
1542 Note that this test is just a heuristic, and that it is quite
1543 possible to have an uninitialized symbol in a shared object which
1544 is really a definition, rather than a common symbol. This could
1545 lead to some minor confusion when the symbol really is a common
1546 symbol in some regular object. However, I think it will be
1547 harmless. */
1548
1549 if (newdyn
1550 && newdef
1551 && !newweak
1552 && (sec->flags & SEC_ALLOC) != 0
1553 && (sec->flags & SEC_LOAD) == 0
1554 && sym->st_size > 0
1555 && !newfunc)
1556 newdyncommon = TRUE;
1557 else
1558 newdyncommon = FALSE;
1559
1560 if (olddyn
1561 && olddef
1562 && h->root.type == bfd_link_hash_defined
1563 && h->def_dynamic
1564 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1565 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1566 && h->size > 0
1567 && !oldfunc)
1568 olddyncommon = TRUE;
1569 else
1570 olddyncommon = FALSE;
1571
1572 /* We now know everything about the old and new symbols. We ask the
1573 backend to check if we can merge them. */
1574 if (bed->merge_symbol != NULL)
1575 {
1576 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1577 return FALSE;
1578 sec = *psec;
1579 }
1580
1581 /* There are multiple definitions of a normal symbol. Skip the
1582 default symbol as well as definition from an IR object. */
1583 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1584 && !default_sym && h->def_regular
1585 && !(oldbfd != NULL
1586 && (oldbfd->flags & BFD_PLUGIN) != 0
1587 && (abfd->flags & BFD_PLUGIN) == 0))
1588 {
1589 /* Handle a multiple definition. */
1590 (*info->callbacks->multiple_definition) (info, &h->root,
1591 abfd, sec, *pvalue);
1592 *skip = TRUE;
1593 return TRUE;
1594 }
1595
1596 /* If both the old and the new symbols look like common symbols in a
1597 dynamic object, set the size of the symbol to the larger of the
1598 two. */
1599
1600 if (olddyncommon
1601 && newdyncommon
1602 && sym->st_size != h->size)
1603 {
1604 /* Since we think we have two common symbols, issue a multiple
1605 common warning if desired. Note that we only warn if the
1606 size is different. If the size is the same, we simply let
1607 the old symbol override the new one as normally happens with
1608 symbols defined in dynamic objects. */
1609
1610 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1611 bfd_link_hash_common, sym->st_size);
1612 if (sym->st_size > h->size)
1613 h->size = sym->st_size;
1614
1615 *size_change_ok = TRUE;
1616 }
1617
1618 /* If we are looking at a dynamic object, and we have found a
1619 definition, we need to see if the symbol was already defined by
1620 some other object. If so, we want to use the existing
1621 definition, and we do not want to report a multiple symbol
1622 definition error; we do this by clobbering *PSEC to be
1623 bfd_und_section_ptr.
1624
1625 We treat a common symbol as a definition if the symbol in the
1626 shared library is a function, since common symbols always
1627 represent variables; this can cause confusion in principle, but
1628 any such confusion would seem to indicate an erroneous program or
1629 shared library. We also permit a common symbol in a regular
1630 object to override a weak symbol in a shared object. */
1631
1632 if (newdyn
1633 && newdef
1634 && (olddef
1635 || (h->root.type == bfd_link_hash_common
1636 && (newweak || newfunc))))
1637 {
1638 *override = TRUE;
1639 newdef = FALSE;
1640 newdyncommon = FALSE;
1641
1642 *psec = sec = bfd_und_section_ptr;
1643 *size_change_ok = TRUE;
1644
1645 /* If we get here when the old symbol is a common symbol, then
1646 we are explicitly letting it override a weak symbol or
1647 function in a dynamic object, and we don't want to warn about
1648 a type change. If the old symbol is a defined symbol, a type
1649 change warning may still be appropriate. */
1650
1651 if (h->root.type == bfd_link_hash_common)
1652 *type_change_ok = TRUE;
1653 }
1654
1655 /* Handle the special case of an old common symbol merging with a
1656 new symbol which looks like a common symbol in a shared object.
1657 We change *PSEC and *PVALUE to make the new symbol look like a
1658 common symbol, and let _bfd_generic_link_add_one_symbol do the
1659 right thing. */
1660
1661 if (newdyncommon
1662 && h->root.type == bfd_link_hash_common)
1663 {
1664 *override = TRUE;
1665 newdef = FALSE;
1666 newdyncommon = FALSE;
1667 *pvalue = sym->st_size;
1668 *psec = sec = bed->common_section (oldsec);
1669 *size_change_ok = TRUE;
1670 }
1671
1672 /* Skip weak definitions of symbols that are already defined. */
1673 if (newdef && olddef && newweak)
1674 {
1675 /* Don't skip new non-IR weak syms. */
1676 if (!(oldbfd != NULL
1677 && (oldbfd->flags & BFD_PLUGIN) != 0
1678 && (abfd->flags & BFD_PLUGIN) == 0))
1679 {
1680 newdef = FALSE;
1681 *skip = TRUE;
1682 }
1683
1684 /* Merge st_other. If the symbol already has a dynamic index,
1685 but visibility says it should not be visible, turn it into a
1686 local symbol. */
1687 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1688 if (h->dynindx != -1)
1689 switch (ELF_ST_VISIBILITY (h->other))
1690 {
1691 case STV_INTERNAL:
1692 case STV_HIDDEN:
1693 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1694 break;
1695 }
1696 }
1697
1698 /* If the old symbol is from a dynamic object, and the new symbol is
1699 a definition which is not from a dynamic object, then the new
1700 symbol overrides the old symbol. Symbols from regular files
1701 always take precedence over symbols from dynamic objects, even if
1702 they are defined after the dynamic object in the link.
1703
1704 As above, we again permit a common symbol in a regular object to
1705 override a definition in a shared object if the shared object
1706 symbol is a function or is weak. */
1707
1708 flip = NULL;
1709 if (!newdyn
1710 && (newdef
1711 || (bfd_is_com_section (sec)
1712 && (oldweak || oldfunc)))
1713 && olddyn
1714 && olddef
1715 && h->def_dynamic)
1716 {
1717 /* Change the hash table entry to undefined, and let
1718 _bfd_generic_link_add_one_symbol do the right thing with the
1719 new definition. */
1720
1721 h->root.type = bfd_link_hash_undefined;
1722 h->root.u.undef.abfd = h->root.u.def.section->owner;
1723 *size_change_ok = TRUE;
1724
1725 olddef = FALSE;
1726 olddyncommon = FALSE;
1727
1728 /* We again permit a type change when a common symbol may be
1729 overriding a function. */
1730
1731 if (bfd_is_com_section (sec))
1732 {
1733 if (oldfunc)
1734 {
1735 /* If a common symbol overrides a function, make sure
1736 that it isn't defined dynamically nor has type
1737 function. */
1738 h->def_dynamic = 0;
1739 h->type = STT_NOTYPE;
1740 }
1741 *type_change_ok = TRUE;
1742 }
1743
1744 if (hi->root.type == bfd_link_hash_indirect)
1745 flip = hi;
1746 else
1747 /* This union may have been set to be non-NULL when this symbol
1748 was seen in a dynamic object. We must force the union to be
1749 NULL, so that it is correct for a regular symbol. */
1750 h->verinfo.vertree = NULL;
1751 }
1752
1753 /* Handle the special case of a new common symbol merging with an
1754 old symbol that looks like it might be a common symbol defined in
1755 a shared object. Note that we have already handled the case in
1756 which a new common symbol should simply override the definition
1757 in the shared library. */
1758
1759 if (! newdyn
1760 && bfd_is_com_section (sec)
1761 && olddyncommon)
1762 {
1763 /* It would be best if we could set the hash table entry to a
1764 common symbol, but we don't know what to use for the section
1765 or the alignment. */
1766 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1767 bfd_link_hash_common, sym->st_size);
1768
1769 /* If the presumed common symbol in the dynamic object is
1770 larger, pretend that the new symbol has its size. */
1771
1772 if (h->size > *pvalue)
1773 *pvalue = h->size;
1774
1775 /* We need to remember the alignment required by the symbol
1776 in the dynamic object. */
1777 BFD_ASSERT (pold_alignment);
1778 *pold_alignment = h->root.u.def.section->alignment_power;
1779
1780 olddef = FALSE;
1781 olddyncommon = FALSE;
1782
1783 h->root.type = bfd_link_hash_undefined;
1784 h->root.u.undef.abfd = h->root.u.def.section->owner;
1785
1786 *size_change_ok = TRUE;
1787 *type_change_ok = TRUE;
1788
1789 if (hi->root.type == bfd_link_hash_indirect)
1790 flip = hi;
1791 else
1792 h->verinfo.vertree = NULL;
1793 }
1794
1795 if (flip != NULL)
1796 {
1797 /* Handle the case where we had a versioned symbol in a dynamic
1798 library and now find a definition in a normal object. In this
1799 case, we make the versioned symbol point to the normal one. */
1800 flip->root.type = h->root.type;
1801 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1802 h->root.type = bfd_link_hash_indirect;
1803 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1804 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1805 if (h->def_dynamic)
1806 {
1807 h->def_dynamic = 0;
1808 flip->ref_dynamic = 1;
1809 }
1810 }
1811
1812 return TRUE;
1813 }
1814
1815 /* This function is called to create an indirect symbol from the
1816 default for the symbol with the default version if needed. The
1817 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1818 set DYNSYM if the new indirect symbol is dynamic. */
1819
1820 static bfd_boolean
1821 _bfd_elf_add_default_symbol (bfd *abfd,
1822 struct bfd_link_info *info,
1823 struct elf_link_hash_entry *h,
1824 const char *name,
1825 Elf_Internal_Sym *sym,
1826 asection *sec,
1827 bfd_vma value,
1828 bfd **poldbfd,
1829 bfd_boolean *dynsym)
1830 {
1831 bfd_boolean type_change_ok;
1832 bfd_boolean size_change_ok;
1833 bfd_boolean skip;
1834 char *shortname;
1835 struct elf_link_hash_entry *hi;
1836 struct bfd_link_hash_entry *bh;
1837 const struct elf_backend_data *bed;
1838 bfd_boolean collect;
1839 bfd_boolean dynamic;
1840 bfd_boolean override;
1841 char *p;
1842 size_t len, shortlen;
1843 asection *tmp_sec;
1844 bfd_boolean matched;
1845
1846 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1847 return TRUE;
1848
1849 /* If this symbol has a version, and it is the default version, we
1850 create an indirect symbol from the default name to the fully
1851 decorated name. This will cause external references which do not
1852 specify a version to be bound to this version of the symbol. */
1853 p = strchr (name, ELF_VER_CHR);
1854 if (h->versioned == unknown)
1855 {
1856 if (p == NULL)
1857 {
1858 h->versioned = unversioned;
1859 return TRUE;
1860 }
1861 else
1862 {
1863 if (p[1] != ELF_VER_CHR)
1864 {
1865 h->versioned = versioned_hidden;
1866 return TRUE;
1867 }
1868 else
1869 h->versioned = versioned;
1870 }
1871 }
1872 else
1873 {
1874 /* PR ld/19073: We may see an unversioned definition after the
1875 default version. */
1876 if (p == NULL)
1877 return TRUE;
1878 }
1879
1880 bed = get_elf_backend_data (abfd);
1881 collect = bed->collect;
1882 dynamic = (abfd->flags & DYNAMIC) != 0;
1883
1884 shortlen = p - name;
1885 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1886 if (shortname == NULL)
1887 return FALSE;
1888 memcpy (shortname, name, shortlen);
1889 shortname[shortlen] = '\0';
1890
1891 /* We are going to create a new symbol. Merge it with any existing
1892 symbol with this name. For the purposes of the merge, act as
1893 though we were defining the symbol we just defined, although we
1894 actually going to define an indirect symbol. */
1895 type_change_ok = FALSE;
1896 size_change_ok = FALSE;
1897 matched = TRUE;
1898 tmp_sec = sec;
1899 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1900 &hi, poldbfd, NULL, NULL, &skip, &override,
1901 &type_change_ok, &size_change_ok, &matched))
1902 return FALSE;
1903
1904 if (skip)
1905 goto nondefault;
1906
1907 if (hi->def_regular || ELF_COMMON_DEF_P (hi))
1908 {
1909 /* If the undecorated symbol will have a version added by a
1910 script different to H, then don't indirect to/from the
1911 undecorated symbol. This isn't ideal because we may not yet
1912 have seen symbol versions, if given by a script on the
1913 command line rather than via --version-script. */
1914 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1915 {
1916 bfd_boolean hide;
1917
1918 hi->verinfo.vertree
1919 = bfd_find_version_for_sym (info->version_info,
1920 hi->root.root.string, &hide);
1921 if (hi->verinfo.vertree != NULL && hide)
1922 {
1923 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1924 goto nondefault;
1925 }
1926 }
1927 if (hi->verinfo.vertree != NULL
1928 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1929 goto nondefault;
1930 }
1931
1932 if (! override)
1933 {
1934 /* Add the default symbol if not performing a relocatable link. */
1935 if (! bfd_link_relocatable (info))
1936 {
1937 bh = &hi->root;
1938 if (bh->type == bfd_link_hash_defined
1939 && bh->u.def.section->owner != NULL
1940 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1941 {
1942 /* Mark the previous definition from IR object as
1943 undefined so that the generic linker will override
1944 it. */
1945 bh->type = bfd_link_hash_undefined;
1946 bh->u.undef.abfd = bh->u.def.section->owner;
1947 }
1948 if (! (_bfd_generic_link_add_one_symbol
1949 (info, abfd, shortname, BSF_INDIRECT,
1950 bfd_ind_section_ptr,
1951 0, name, FALSE, collect, &bh)))
1952 return FALSE;
1953 hi = (struct elf_link_hash_entry *) bh;
1954 }
1955 }
1956 else
1957 {
1958 /* In this case the symbol named SHORTNAME is overriding the
1959 indirect symbol we want to add. We were planning on making
1960 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1961 is the name without a version. NAME is the fully versioned
1962 name, and it is the default version.
1963
1964 Overriding means that we already saw a definition for the
1965 symbol SHORTNAME in a regular object, and it is overriding
1966 the symbol defined in the dynamic object.
1967
1968 When this happens, we actually want to change NAME, the
1969 symbol we just added, to refer to SHORTNAME. This will cause
1970 references to NAME in the shared object to become references
1971 to SHORTNAME in the regular object. This is what we expect
1972 when we override a function in a shared object: that the
1973 references in the shared object will be mapped to the
1974 definition in the regular object. */
1975
1976 while (hi->root.type == bfd_link_hash_indirect
1977 || hi->root.type == bfd_link_hash_warning)
1978 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1979
1980 h->root.type = bfd_link_hash_indirect;
1981 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1982 if (h->def_dynamic)
1983 {
1984 h->def_dynamic = 0;
1985 hi->ref_dynamic = 1;
1986 if (hi->ref_regular
1987 || hi->def_regular)
1988 {
1989 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1990 return FALSE;
1991 }
1992 }
1993
1994 /* Now set HI to H, so that the following code will set the
1995 other fields correctly. */
1996 hi = h;
1997 }
1998
1999 /* Check if HI is a warning symbol. */
2000 if (hi->root.type == bfd_link_hash_warning)
2001 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2002
2003 /* If there is a duplicate definition somewhere, then HI may not
2004 point to an indirect symbol. We will have reported an error to
2005 the user in that case. */
2006
2007 if (hi->root.type == bfd_link_hash_indirect)
2008 {
2009 struct elf_link_hash_entry *ht;
2010
2011 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2012 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2013
2014 /* A reference to the SHORTNAME symbol from a dynamic library
2015 will be satisfied by the versioned symbol at runtime. In
2016 effect, we have a reference to the versioned symbol. */
2017 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2018 hi->dynamic_def |= ht->dynamic_def;
2019
2020 /* See if the new flags lead us to realize that the symbol must
2021 be dynamic. */
2022 if (! *dynsym)
2023 {
2024 if (! dynamic)
2025 {
2026 if (! bfd_link_executable (info)
2027 || hi->def_dynamic
2028 || hi->ref_dynamic)
2029 *dynsym = TRUE;
2030 }
2031 else
2032 {
2033 if (hi->ref_regular)
2034 *dynsym = TRUE;
2035 }
2036 }
2037 }
2038
2039 /* We also need to define an indirection from the nondefault version
2040 of the symbol. */
2041
2042 nondefault:
2043 len = strlen (name);
2044 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2045 if (shortname == NULL)
2046 return FALSE;
2047 memcpy (shortname, name, shortlen);
2048 memcpy (shortname + shortlen, p + 1, len - shortlen);
2049
2050 /* Once again, merge with any existing symbol. */
2051 type_change_ok = FALSE;
2052 size_change_ok = FALSE;
2053 tmp_sec = sec;
2054 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2055 &hi, poldbfd, NULL, NULL, &skip, &override,
2056 &type_change_ok, &size_change_ok, &matched))
2057 return FALSE;
2058
2059 if (skip)
2060 return TRUE;
2061
2062 if (override)
2063 {
2064 /* Here SHORTNAME is a versioned name, so we don't expect to see
2065 the type of override we do in the case above unless it is
2066 overridden by a versioned definition. */
2067 if (hi->root.type != bfd_link_hash_defined
2068 && hi->root.type != bfd_link_hash_defweak)
2069 _bfd_error_handler
2070 /* xgettext:c-format */
2071 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2072 abfd, shortname);
2073 }
2074 else
2075 {
2076 bh = &hi->root;
2077 if (! (_bfd_generic_link_add_one_symbol
2078 (info, abfd, shortname, BSF_INDIRECT,
2079 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
2080 return FALSE;
2081 hi = (struct elf_link_hash_entry *) bh;
2082
2083 /* If there is a duplicate definition somewhere, then HI may not
2084 point to an indirect symbol. We will have reported an error
2085 to the user in that case. */
2086
2087 if (hi->root.type == bfd_link_hash_indirect)
2088 {
2089 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2090 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2091 hi->dynamic_def |= h->dynamic_def;
2092
2093 /* See if the new flags lead us to realize that the symbol
2094 must be dynamic. */
2095 if (! *dynsym)
2096 {
2097 if (! dynamic)
2098 {
2099 if (! bfd_link_executable (info)
2100 || hi->ref_dynamic)
2101 *dynsym = TRUE;
2102 }
2103 else
2104 {
2105 if (hi->ref_regular)
2106 *dynsym = TRUE;
2107 }
2108 }
2109 }
2110 }
2111
2112 return TRUE;
2113 }
2114 \f
2115 /* This routine is used to export all defined symbols into the dynamic
2116 symbol table. It is called via elf_link_hash_traverse. */
2117
2118 static bfd_boolean
2119 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2120 {
2121 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2122
2123 /* Ignore indirect symbols. These are added by the versioning code. */
2124 if (h->root.type == bfd_link_hash_indirect)
2125 return TRUE;
2126
2127 /* Ignore this if we won't export it. */
2128 if (!eif->info->export_dynamic && !h->dynamic)
2129 return TRUE;
2130
2131 if (h->dynindx == -1
2132 && (h->def_regular || h->ref_regular)
2133 && ! bfd_hide_sym_by_version (eif->info->version_info,
2134 h->root.root.string))
2135 {
2136 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2137 {
2138 eif->failed = TRUE;
2139 return FALSE;
2140 }
2141 }
2142
2143 return TRUE;
2144 }
2145 \f
2146 /* Look through the symbols which are defined in other shared
2147 libraries and referenced here. Update the list of version
2148 dependencies. This will be put into the .gnu.version_r section.
2149 This function is called via elf_link_hash_traverse. */
2150
2151 static bfd_boolean
2152 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2153 void *data)
2154 {
2155 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2156 Elf_Internal_Verneed *t;
2157 Elf_Internal_Vernaux *a;
2158 size_t amt;
2159
2160 /* We only care about symbols defined in shared objects with version
2161 information. */
2162 if (!h->def_dynamic
2163 || h->def_regular
2164 || h->dynindx == -1
2165 || h->verinfo.verdef == NULL
2166 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2167 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2168 return TRUE;
2169
2170 /* See if we already know about this version. */
2171 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2172 t != NULL;
2173 t = t->vn_nextref)
2174 {
2175 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2176 continue;
2177
2178 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2179 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2180 return TRUE;
2181
2182 break;
2183 }
2184
2185 /* This is a new version. Add it to tree we are building. */
2186
2187 if (t == NULL)
2188 {
2189 amt = sizeof *t;
2190 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2191 if (t == NULL)
2192 {
2193 rinfo->failed = TRUE;
2194 return FALSE;
2195 }
2196
2197 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2198 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2199 elf_tdata (rinfo->info->output_bfd)->verref = t;
2200 }
2201
2202 amt = sizeof *a;
2203 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2204 if (a == NULL)
2205 {
2206 rinfo->failed = TRUE;
2207 return FALSE;
2208 }
2209
2210 /* Note that we are copying a string pointer here, and testing it
2211 above. If bfd_elf_string_from_elf_section is ever changed to
2212 discard the string data when low in memory, this will have to be
2213 fixed. */
2214 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2215
2216 a->vna_flags = h->verinfo.verdef->vd_flags;
2217 a->vna_nextptr = t->vn_auxptr;
2218
2219 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2220 ++rinfo->vers;
2221
2222 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2223
2224 t->vn_auxptr = a;
2225
2226 return TRUE;
2227 }
2228
2229 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2230 hidden. Set *T_P to NULL if there is no match. */
2231
2232 static bfd_boolean
2233 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2234 struct elf_link_hash_entry *h,
2235 const char *version_p,
2236 struct bfd_elf_version_tree **t_p,
2237 bfd_boolean *hide)
2238 {
2239 struct bfd_elf_version_tree *t;
2240
2241 /* Look for the version. If we find it, it is no longer weak. */
2242 for (t = info->version_info; t != NULL; t = t->next)
2243 {
2244 if (strcmp (t->name, version_p) == 0)
2245 {
2246 size_t len;
2247 char *alc;
2248 struct bfd_elf_version_expr *d;
2249
2250 len = version_p - h->root.root.string;
2251 alc = (char *) bfd_malloc (len);
2252 if (alc == NULL)
2253 return FALSE;
2254 memcpy (alc, h->root.root.string, len - 1);
2255 alc[len - 1] = '\0';
2256 if (alc[len - 2] == ELF_VER_CHR)
2257 alc[len - 2] = '\0';
2258
2259 h->verinfo.vertree = t;
2260 t->used = TRUE;
2261 d = NULL;
2262
2263 if (t->globals.list != NULL)
2264 d = (*t->match) (&t->globals, NULL, alc);
2265
2266 /* See if there is anything to force this symbol to
2267 local scope. */
2268 if (d == NULL && t->locals.list != NULL)
2269 {
2270 d = (*t->match) (&t->locals, NULL, alc);
2271 if (d != NULL
2272 && h->dynindx != -1
2273 && ! info->export_dynamic)
2274 *hide = TRUE;
2275 }
2276
2277 free (alc);
2278 break;
2279 }
2280 }
2281
2282 *t_p = t;
2283
2284 return TRUE;
2285 }
2286
2287 /* Return TRUE if the symbol H is hidden by version script. */
2288
2289 bfd_boolean
2290 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2291 struct elf_link_hash_entry *h)
2292 {
2293 const char *p;
2294 bfd_boolean hide = FALSE;
2295 const struct elf_backend_data *bed
2296 = get_elf_backend_data (info->output_bfd);
2297
2298 /* Version script only hides symbols defined in regular objects. */
2299 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2300 return TRUE;
2301
2302 p = strchr (h->root.root.string, ELF_VER_CHR);
2303 if (p != NULL && h->verinfo.vertree == NULL)
2304 {
2305 struct bfd_elf_version_tree *t;
2306
2307 ++p;
2308 if (*p == ELF_VER_CHR)
2309 ++p;
2310
2311 if (*p != '\0'
2312 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2313 && hide)
2314 {
2315 if (hide)
2316 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2317 return TRUE;
2318 }
2319 }
2320
2321 /* If we don't have a version for this symbol, see if we can find
2322 something. */
2323 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2324 {
2325 h->verinfo.vertree
2326 = bfd_find_version_for_sym (info->version_info,
2327 h->root.root.string, &hide);
2328 if (h->verinfo.vertree != NULL && hide)
2329 {
2330 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2331 return TRUE;
2332 }
2333 }
2334
2335 return FALSE;
2336 }
2337
2338 /* Figure out appropriate versions for all the symbols. We may not
2339 have the version number script until we have read all of the input
2340 files, so until that point we don't know which symbols should be
2341 local. This function is called via elf_link_hash_traverse. */
2342
2343 static bfd_boolean
2344 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2345 {
2346 struct elf_info_failed *sinfo;
2347 struct bfd_link_info *info;
2348 const struct elf_backend_data *bed;
2349 struct elf_info_failed eif;
2350 char *p;
2351 bfd_boolean hide;
2352
2353 sinfo = (struct elf_info_failed *) data;
2354 info = sinfo->info;
2355
2356 /* Fix the symbol flags. */
2357 eif.failed = FALSE;
2358 eif.info = info;
2359 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2360 {
2361 if (eif.failed)
2362 sinfo->failed = TRUE;
2363 return FALSE;
2364 }
2365
2366 bed = get_elf_backend_data (info->output_bfd);
2367
2368 /* We only need version numbers for symbols defined in regular
2369 objects. */
2370 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2371 {
2372 /* Hide symbols defined in discarded input sections. */
2373 if ((h->root.type == bfd_link_hash_defined
2374 || h->root.type == bfd_link_hash_defweak)
2375 && discarded_section (h->root.u.def.section))
2376 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2377 return TRUE;
2378 }
2379
2380 hide = FALSE;
2381 p = strchr (h->root.root.string, ELF_VER_CHR);
2382 if (p != NULL && h->verinfo.vertree == NULL)
2383 {
2384 struct bfd_elf_version_tree *t;
2385
2386 ++p;
2387 if (*p == ELF_VER_CHR)
2388 ++p;
2389
2390 /* If there is no version string, we can just return out. */
2391 if (*p == '\0')
2392 return TRUE;
2393
2394 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2395 {
2396 sinfo->failed = TRUE;
2397 return FALSE;
2398 }
2399
2400 if (hide)
2401 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2402
2403 /* If we are building an application, we need to create a
2404 version node for this version. */
2405 if (t == NULL && bfd_link_executable (info))
2406 {
2407 struct bfd_elf_version_tree **pp;
2408 int version_index;
2409
2410 /* If we aren't going to export this symbol, we don't need
2411 to worry about it. */
2412 if (h->dynindx == -1)
2413 return TRUE;
2414
2415 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2416 sizeof *t);
2417 if (t == NULL)
2418 {
2419 sinfo->failed = TRUE;
2420 return FALSE;
2421 }
2422
2423 t->name = p;
2424 t->name_indx = (unsigned int) -1;
2425 t->used = TRUE;
2426
2427 version_index = 1;
2428 /* Don't count anonymous version tag. */
2429 if (sinfo->info->version_info != NULL
2430 && sinfo->info->version_info->vernum == 0)
2431 version_index = 0;
2432 for (pp = &sinfo->info->version_info;
2433 *pp != NULL;
2434 pp = &(*pp)->next)
2435 ++version_index;
2436 t->vernum = version_index;
2437
2438 *pp = t;
2439
2440 h->verinfo.vertree = t;
2441 }
2442 else if (t == NULL)
2443 {
2444 /* We could not find the version for a symbol when
2445 generating a shared archive. Return an error. */
2446 _bfd_error_handler
2447 /* xgettext:c-format */
2448 (_("%pB: version node not found for symbol %s"),
2449 info->output_bfd, h->root.root.string);
2450 bfd_set_error (bfd_error_bad_value);
2451 sinfo->failed = TRUE;
2452 return FALSE;
2453 }
2454 }
2455
2456 /* If we don't have a version for this symbol, see if we can find
2457 something. */
2458 if (!hide
2459 && h->verinfo.vertree == NULL
2460 && sinfo->info->version_info != NULL)
2461 {
2462 h->verinfo.vertree
2463 = bfd_find_version_for_sym (sinfo->info->version_info,
2464 h->root.root.string, &hide);
2465 if (h->verinfo.vertree != NULL && hide)
2466 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2467 }
2468
2469 return TRUE;
2470 }
2471 \f
2472 /* Read and swap the relocs from the section indicated by SHDR. This
2473 may be either a REL or a RELA section. The relocations are
2474 translated into RELA relocations and stored in INTERNAL_RELOCS,
2475 which should have already been allocated to contain enough space.
2476 The EXTERNAL_RELOCS are a buffer where the external form of the
2477 relocations should be stored.
2478
2479 Returns FALSE if something goes wrong. */
2480
2481 static bfd_boolean
2482 elf_link_read_relocs_from_section (bfd *abfd,
2483 asection *sec,
2484 Elf_Internal_Shdr *shdr,
2485 void *external_relocs,
2486 Elf_Internal_Rela *internal_relocs)
2487 {
2488 const struct elf_backend_data *bed;
2489 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2490 const bfd_byte *erela;
2491 const bfd_byte *erelaend;
2492 Elf_Internal_Rela *irela;
2493 Elf_Internal_Shdr *symtab_hdr;
2494 size_t nsyms;
2495
2496 /* Position ourselves at the start of the section. */
2497 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2498 return FALSE;
2499
2500 /* Read the relocations. */
2501 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2502 return FALSE;
2503
2504 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2505 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2506
2507 bed = get_elf_backend_data (abfd);
2508
2509 /* Convert the external relocations to the internal format. */
2510 if (shdr->sh_entsize == bed->s->sizeof_rel)
2511 swap_in = bed->s->swap_reloc_in;
2512 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2513 swap_in = bed->s->swap_reloca_in;
2514 else
2515 {
2516 bfd_set_error (bfd_error_wrong_format);
2517 return FALSE;
2518 }
2519
2520 erela = (const bfd_byte *) external_relocs;
2521 /* Setting erelaend like this and comparing with <= handles case of
2522 a fuzzed object with sh_size not a multiple of sh_entsize. */
2523 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2524 irela = internal_relocs;
2525 while (erela <= erelaend)
2526 {
2527 bfd_vma r_symndx;
2528
2529 (*swap_in) (abfd, erela, irela);
2530 r_symndx = ELF32_R_SYM (irela->r_info);
2531 if (bed->s->arch_size == 64)
2532 r_symndx >>= 24;
2533 if (nsyms > 0)
2534 {
2535 if ((size_t) r_symndx >= nsyms)
2536 {
2537 _bfd_error_handler
2538 /* xgettext:c-format */
2539 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2540 " for offset %#" PRIx64 " in section `%pA'"),
2541 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2542 (uint64_t) irela->r_offset, sec);
2543 bfd_set_error (bfd_error_bad_value);
2544 return FALSE;
2545 }
2546 }
2547 else if (r_symndx != STN_UNDEF)
2548 {
2549 _bfd_error_handler
2550 /* xgettext:c-format */
2551 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2552 " for offset %#" PRIx64 " in section `%pA'"
2553 " when the object file has no symbol table"),
2554 abfd, (uint64_t) r_symndx,
2555 (uint64_t) irela->r_offset, sec);
2556 bfd_set_error (bfd_error_bad_value);
2557 return FALSE;
2558 }
2559 irela += bed->s->int_rels_per_ext_rel;
2560 erela += shdr->sh_entsize;
2561 }
2562
2563 return TRUE;
2564 }
2565
2566 /* Read and swap the relocs for a section O. They may have been
2567 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2568 not NULL, they are used as buffers to read into. They are known to
2569 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2570 the return value is allocated using either malloc or bfd_alloc,
2571 according to the KEEP_MEMORY argument. If O has two relocation
2572 sections (both REL and RELA relocations), then the REL_HDR
2573 relocations will appear first in INTERNAL_RELOCS, followed by the
2574 RELA_HDR relocations. */
2575
2576 Elf_Internal_Rela *
2577 _bfd_elf_link_read_relocs (bfd *abfd,
2578 asection *o,
2579 void *external_relocs,
2580 Elf_Internal_Rela *internal_relocs,
2581 bfd_boolean keep_memory)
2582 {
2583 void *alloc1 = NULL;
2584 Elf_Internal_Rela *alloc2 = NULL;
2585 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2586 struct bfd_elf_section_data *esdo = elf_section_data (o);
2587 Elf_Internal_Rela *internal_rela_relocs;
2588
2589 if (esdo->relocs != NULL)
2590 return esdo->relocs;
2591
2592 if (o->reloc_count == 0)
2593 return NULL;
2594
2595 if (internal_relocs == NULL)
2596 {
2597 bfd_size_type size;
2598
2599 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2600 if (keep_memory)
2601 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2602 else
2603 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2604 if (internal_relocs == NULL)
2605 goto error_return;
2606 }
2607
2608 if (external_relocs == NULL)
2609 {
2610 bfd_size_type size = 0;
2611
2612 if (esdo->rel.hdr)
2613 size += esdo->rel.hdr->sh_size;
2614 if (esdo->rela.hdr)
2615 size += esdo->rela.hdr->sh_size;
2616
2617 alloc1 = bfd_malloc (size);
2618 if (alloc1 == NULL)
2619 goto error_return;
2620 external_relocs = alloc1;
2621 }
2622
2623 internal_rela_relocs = internal_relocs;
2624 if (esdo->rel.hdr)
2625 {
2626 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2627 external_relocs,
2628 internal_relocs))
2629 goto error_return;
2630 external_relocs = (((bfd_byte *) external_relocs)
2631 + esdo->rel.hdr->sh_size);
2632 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2633 * bed->s->int_rels_per_ext_rel);
2634 }
2635
2636 if (esdo->rela.hdr
2637 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2638 external_relocs,
2639 internal_rela_relocs)))
2640 goto error_return;
2641
2642 /* Cache the results for next time, if we can. */
2643 if (keep_memory)
2644 esdo->relocs = internal_relocs;
2645
2646 if (alloc1 != NULL)
2647 free (alloc1);
2648
2649 /* Don't free alloc2, since if it was allocated we are passing it
2650 back (under the name of internal_relocs). */
2651
2652 return internal_relocs;
2653
2654 error_return:
2655 if (alloc1 != NULL)
2656 free (alloc1);
2657 if (alloc2 != NULL)
2658 {
2659 if (keep_memory)
2660 bfd_release (abfd, alloc2);
2661 else
2662 free (alloc2);
2663 }
2664 return NULL;
2665 }
2666
2667 /* Compute the size of, and allocate space for, REL_HDR which is the
2668 section header for a section containing relocations for O. */
2669
2670 static bfd_boolean
2671 _bfd_elf_link_size_reloc_section (bfd *abfd,
2672 struct bfd_elf_section_reloc_data *reldata)
2673 {
2674 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2675
2676 /* That allows us to calculate the size of the section. */
2677 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2678
2679 /* The contents field must last into write_object_contents, so we
2680 allocate it with bfd_alloc rather than malloc. Also since we
2681 cannot be sure that the contents will actually be filled in,
2682 we zero the allocated space. */
2683 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2684 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2685 return FALSE;
2686
2687 if (reldata->hashes == NULL && reldata->count)
2688 {
2689 struct elf_link_hash_entry **p;
2690
2691 p = ((struct elf_link_hash_entry **)
2692 bfd_zmalloc (reldata->count * sizeof (*p)));
2693 if (p == NULL)
2694 return FALSE;
2695
2696 reldata->hashes = p;
2697 }
2698
2699 return TRUE;
2700 }
2701
2702 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2703 originated from the section given by INPUT_REL_HDR) to the
2704 OUTPUT_BFD. */
2705
2706 bfd_boolean
2707 _bfd_elf_link_output_relocs (bfd *output_bfd,
2708 asection *input_section,
2709 Elf_Internal_Shdr *input_rel_hdr,
2710 Elf_Internal_Rela *internal_relocs,
2711 struct elf_link_hash_entry **rel_hash
2712 ATTRIBUTE_UNUSED)
2713 {
2714 Elf_Internal_Rela *irela;
2715 Elf_Internal_Rela *irelaend;
2716 bfd_byte *erel;
2717 struct bfd_elf_section_reloc_data *output_reldata;
2718 asection *output_section;
2719 const struct elf_backend_data *bed;
2720 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2721 struct bfd_elf_section_data *esdo;
2722
2723 output_section = input_section->output_section;
2724
2725 bed = get_elf_backend_data (output_bfd);
2726 esdo = elf_section_data (output_section);
2727 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2728 {
2729 output_reldata = &esdo->rel;
2730 swap_out = bed->s->swap_reloc_out;
2731 }
2732 else if (esdo->rela.hdr
2733 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2734 {
2735 output_reldata = &esdo->rela;
2736 swap_out = bed->s->swap_reloca_out;
2737 }
2738 else
2739 {
2740 _bfd_error_handler
2741 /* xgettext:c-format */
2742 (_("%pB: relocation size mismatch in %pB section %pA"),
2743 output_bfd, input_section->owner, input_section);
2744 bfd_set_error (bfd_error_wrong_format);
2745 return FALSE;
2746 }
2747
2748 erel = output_reldata->hdr->contents;
2749 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2750 irela = internal_relocs;
2751 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2752 * bed->s->int_rels_per_ext_rel);
2753 while (irela < irelaend)
2754 {
2755 (*swap_out) (output_bfd, irela, erel);
2756 irela += bed->s->int_rels_per_ext_rel;
2757 erel += input_rel_hdr->sh_entsize;
2758 }
2759
2760 /* Bump the counter, so that we know where to add the next set of
2761 relocations. */
2762 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2763
2764 return TRUE;
2765 }
2766 \f
2767 /* Make weak undefined symbols in PIE dynamic. */
2768
2769 bfd_boolean
2770 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2771 struct elf_link_hash_entry *h)
2772 {
2773 if (bfd_link_pie (info)
2774 && h->dynindx == -1
2775 && h->root.type == bfd_link_hash_undefweak)
2776 return bfd_elf_link_record_dynamic_symbol (info, h);
2777
2778 return TRUE;
2779 }
2780
2781 /* Fix up the flags for a symbol. This handles various cases which
2782 can only be fixed after all the input files are seen. This is
2783 currently called by both adjust_dynamic_symbol and
2784 assign_sym_version, which is unnecessary but perhaps more robust in
2785 the face of future changes. */
2786
2787 static bfd_boolean
2788 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2789 struct elf_info_failed *eif)
2790 {
2791 const struct elf_backend_data *bed;
2792
2793 /* If this symbol was mentioned in a non-ELF file, try to set
2794 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2795 permit a non-ELF file to correctly refer to a symbol defined in
2796 an ELF dynamic object. */
2797 if (h->non_elf)
2798 {
2799 while (h->root.type == bfd_link_hash_indirect)
2800 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2801
2802 if (h->root.type != bfd_link_hash_defined
2803 && h->root.type != bfd_link_hash_defweak)
2804 {
2805 h->ref_regular = 1;
2806 h->ref_regular_nonweak = 1;
2807 }
2808 else
2809 {
2810 if (h->root.u.def.section->owner != NULL
2811 && (bfd_get_flavour (h->root.u.def.section->owner)
2812 == bfd_target_elf_flavour))
2813 {
2814 h->ref_regular = 1;
2815 h->ref_regular_nonweak = 1;
2816 }
2817 else
2818 h->def_regular = 1;
2819 }
2820
2821 if (h->dynindx == -1
2822 && (h->def_dynamic
2823 || h->ref_dynamic))
2824 {
2825 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2826 {
2827 eif->failed = TRUE;
2828 return FALSE;
2829 }
2830 }
2831 }
2832 else
2833 {
2834 /* Unfortunately, NON_ELF is only correct if the symbol
2835 was first seen in a non-ELF file. Fortunately, if the symbol
2836 was first seen in an ELF file, we're probably OK unless the
2837 symbol was defined in a non-ELF file. Catch that case here.
2838 FIXME: We're still in trouble if the symbol was first seen in
2839 a dynamic object, and then later in a non-ELF regular object. */
2840 if ((h->root.type == bfd_link_hash_defined
2841 || h->root.type == bfd_link_hash_defweak)
2842 && !h->def_regular
2843 && (h->root.u.def.section->owner != NULL
2844 ? (bfd_get_flavour (h->root.u.def.section->owner)
2845 != bfd_target_elf_flavour)
2846 : (bfd_is_abs_section (h->root.u.def.section)
2847 && !h->def_dynamic)))
2848 h->def_regular = 1;
2849 }
2850
2851 /* Backend specific symbol fixup. */
2852 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2853 if (bed->elf_backend_fixup_symbol
2854 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2855 return FALSE;
2856
2857 /* If this is a final link, and the symbol was defined as a common
2858 symbol in a regular object file, and there was no definition in
2859 any dynamic object, then the linker will have allocated space for
2860 the symbol in a common section but the DEF_REGULAR
2861 flag will not have been set. */
2862 if (h->root.type == bfd_link_hash_defined
2863 && !h->def_regular
2864 && h->ref_regular
2865 && !h->def_dynamic
2866 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2867 h->def_regular = 1;
2868
2869 /* Symbols defined in discarded sections shouldn't be dynamic. */
2870 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
2871 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2872
2873 /* If a weak undefined symbol has non-default visibility, we also
2874 hide it from the dynamic linker. */
2875 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2876 && h->root.type == bfd_link_hash_undefweak)
2877 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2878
2879 /* A hidden versioned symbol in executable should be forced local if
2880 it is is locally defined, not referenced by shared library and not
2881 exported. */
2882 else if (bfd_link_executable (eif->info)
2883 && h->versioned == versioned_hidden
2884 && !eif->info->export_dynamic
2885 && !h->dynamic
2886 && !h->ref_dynamic
2887 && h->def_regular)
2888 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2889
2890 /* If -Bsymbolic was used (which means to bind references to global
2891 symbols to the definition within the shared object), and this
2892 symbol was defined in a regular object, then it actually doesn't
2893 need a PLT entry. Likewise, if the symbol has non-default
2894 visibility. If the symbol has hidden or internal visibility, we
2895 will force it local. */
2896 else if (h->needs_plt
2897 && bfd_link_pic (eif->info)
2898 && is_elf_hash_table (eif->info->hash)
2899 && (SYMBOLIC_BIND (eif->info, h)
2900 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2901 && h->def_regular)
2902 {
2903 bfd_boolean force_local;
2904
2905 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2906 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2907 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2908 }
2909
2910 /* If this is a weak defined symbol in a dynamic object, and we know
2911 the real definition in the dynamic object, copy interesting flags
2912 over to the real definition. */
2913 if (h->is_weakalias)
2914 {
2915 struct elf_link_hash_entry *def = weakdef (h);
2916
2917 /* If the real definition is defined by a regular object file,
2918 don't do anything special. See the longer description in
2919 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
2920 bfd_link_hash_defined as it was when put on the alias list
2921 then it must have originally been a versioned symbol (for
2922 which a non-versioned indirect symbol is created) and later
2923 a definition for the non-versioned symbol is found. In that
2924 case the indirection is flipped with the versioned symbol
2925 becoming an indirect pointing at the non-versioned symbol.
2926 Thus, not an alias any more. */
2927 if (def->def_regular
2928 || def->root.type != bfd_link_hash_defined)
2929 {
2930 h = def;
2931 while ((h = h->u.alias) != def)
2932 h->is_weakalias = 0;
2933 }
2934 else
2935 {
2936 while (h->root.type == bfd_link_hash_indirect)
2937 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2938 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2939 || h->root.type == bfd_link_hash_defweak);
2940 BFD_ASSERT (def->def_dynamic);
2941 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
2942 }
2943 }
2944
2945 return TRUE;
2946 }
2947
2948 /* Make the backend pick a good value for a dynamic symbol. This is
2949 called via elf_link_hash_traverse, and also calls itself
2950 recursively. */
2951
2952 static bfd_boolean
2953 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2954 {
2955 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2956 struct elf_link_hash_table *htab;
2957 const struct elf_backend_data *bed;
2958
2959 if (! is_elf_hash_table (eif->info->hash))
2960 return FALSE;
2961
2962 /* Ignore indirect symbols. These are added by the versioning code. */
2963 if (h->root.type == bfd_link_hash_indirect)
2964 return TRUE;
2965
2966 /* Fix the symbol flags. */
2967 if (! _bfd_elf_fix_symbol_flags (h, eif))
2968 return FALSE;
2969
2970 htab = elf_hash_table (eif->info);
2971 bed = get_elf_backend_data (htab->dynobj);
2972
2973 if (h->root.type == bfd_link_hash_undefweak)
2974 {
2975 if (eif->info->dynamic_undefined_weak == 0)
2976 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2977 else if (eif->info->dynamic_undefined_weak > 0
2978 && h->ref_regular
2979 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2980 && !bfd_hide_sym_by_version (eif->info->version_info,
2981 h->root.root.string))
2982 {
2983 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2984 {
2985 eif->failed = TRUE;
2986 return FALSE;
2987 }
2988 }
2989 }
2990
2991 /* If this symbol does not require a PLT entry, and it is not
2992 defined by a dynamic object, or is not referenced by a regular
2993 object, ignore it. We do have to handle a weak defined symbol,
2994 even if no regular object refers to it, if we decided to add it
2995 to the dynamic symbol table. FIXME: Do we normally need to worry
2996 about symbols which are defined by one dynamic object and
2997 referenced by another one? */
2998 if (!h->needs_plt
2999 && h->type != STT_GNU_IFUNC
3000 && (h->def_regular
3001 || !h->def_dynamic
3002 || (!h->ref_regular
3003 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3004 {
3005 h->plt = elf_hash_table (eif->info)->init_plt_offset;
3006 return TRUE;
3007 }
3008
3009 /* If we've already adjusted this symbol, don't do it again. This
3010 can happen via a recursive call. */
3011 if (h->dynamic_adjusted)
3012 return TRUE;
3013
3014 /* Don't look at this symbol again. Note that we must set this
3015 after checking the above conditions, because we may look at a
3016 symbol once, decide not to do anything, and then get called
3017 recursively later after REF_REGULAR is set below. */
3018 h->dynamic_adjusted = 1;
3019
3020 /* If this is a weak definition, and we know a real definition, and
3021 the real symbol is not itself defined by a regular object file,
3022 then get a good value for the real definition. We handle the
3023 real symbol first, for the convenience of the backend routine.
3024
3025 Note that there is a confusing case here. If the real definition
3026 is defined by a regular object file, we don't get the real symbol
3027 from the dynamic object, but we do get the weak symbol. If the
3028 processor backend uses a COPY reloc, then if some routine in the
3029 dynamic object changes the real symbol, we will not see that
3030 change in the corresponding weak symbol. This is the way other
3031 ELF linkers work as well, and seems to be a result of the shared
3032 library model.
3033
3034 I will clarify this issue. Most SVR4 shared libraries define the
3035 variable _timezone and define timezone as a weak synonym. The
3036 tzset call changes _timezone. If you write
3037 extern int timezone;
3038 int _timezone = 5;
3039 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3040 you might expect that, since timezone is a synonym for _timezone,
3041 the same number will print both times. However, if the processor
3042 backend uses a COPY reloc, then actually timezone will be copied
3043 into your process image, and, since you define _timezone
3044 yourself, _timezone will not. Thus timezone and _timezone will
3045 wind up at different memory locations. The tzset call will set
3046 _timezone, leaving timezone unchanged. */
3047
3048 if (h->is_weakalias)
3049 {
3050 struct elf_link_hash_entry *def = weakdef (h);
3051
3052 /* If we get to this point, there is an implicit reference to
3053 the alias by a regular object file via the weak symbol H. */
3054 def->ref_regular = 1;
3055
3056 /* Ensure that the backend adjust_dynamic_symbol function sees
3057 the strong alias before H by recursively calling ourselves. */
3058 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3059 return FALSE;
3060 }
3061
3062 /* If a symbol has no type and no size and does not require a PLT
3063 entry, then we are probably about to do the wrong thing here: we
3064 are probably going to create a COPY reloc for an empty object.
3065 This case can arise when a shared object is built with assembly
3066 code, and the assembly code fails to set the symbol type. */
3067 if (h->size == 0
3068 && h->type == STT_NOTYPE
3069 && !h->needs_plt)
3070 _bfd_error_handler
3071 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3072 h->root.root.string);
3073
3074 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3075 {
3076 eif->failed = TRUE;
3077 return FALSE;
3078 }
3079
3080 return TRUE;
3081 }
3082
3083 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3084 DYNBSS. */
3085
3086 bfd_boolean
3087 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3088 struct elf_link_hash_entry *h,
3089 asection *dynbss)
3090 {
3091 unsigned int power_of_two;
3092 bfd_vma mask;
3093 asection *sec = h->root.u.def.section;
3094
3095 /* The section alignment of the definition is the maximum alignment
3096 requirement of symbols defined in the section. Since we don't
3097 know the symbol alignment requirement, we start with the
3098 maximum alignment and check low bits of the symbol address
3099 for the minimum alignment. */
3100 power_of_two = bfd_section_alignment (sec);
3101 mask = ((bfd_vma) 1 << power_of_two) - 1;
3102 while ((h->root.u.def.value & mask) != 0)
3103 {
3104 mask >>= 1;
3105 --power_of_two;
3106 }
3107
3108 if (power_of_two > bfd_section_alignment (dynbss))
3109 {
3110 /* Adjust the section alignment if needed. */
3111 if (!bfd_set_section_alignment (dynbss, power_of_two))
3112 return FALSE;
3113 }
3114
3115 /* We make sure that the symbol will be aligned properly. */
3116 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3117
3118 /* Define the symbol as being at this point in DYNBSS. */
3119 h->root.u.def.section = dynbss;
3120 h->root.u.def.value = dynbss->size;
3121
3122 /* Increment the size of DYNBSS to make room for the symbol. */
3123 dynbss->size += h->size;
3124
3125 /* No error if extern_protected_data is true. */
3126 if (h->protected_def
3127 && (!info->extern_protected_data
3128 || (info->extern_protected_data < 0
3129 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3130 info->callbacks->einfo
3131 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3132 h->root.root.string);
3133
3134 return TRUE;
3135 }
3136
3137 /* Adjust all external symbols pointing into SEC_MERGE sections
3138 to reflect the object merging within the sections. */
3139
3140 static bfd_boolean
3141 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3142 {
3143 asection *sec;
3144
3145 if ((h->root.type == bfd_link_hash_defined
3146 || h->root.type == bfd_link_hash_defweak)
3147 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3148 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3149 {
3150 bfd *output_bfd = (bfd *) data;
3151
3152 h->root.u.def.value =
3153 _bfd_merged_section_offset (output_bfd,
3154 &h->root.u.def.section,
3155 elf_section_data (sec)->sec_info,
3156 h->root.u.def.value);
3157 }
3158
3159 return TRUE;
3160 }
3161
3162 /* Returns false if the symbol referred to by H should be considered
3163 to resolve local to the current module, and true if it should be
3164 considered to bind dynamically. */
3165
3166 bfd_boolean
3167 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3168 struct bfd_link_info *info,
3169 bfd_boolean not_local_protected)
3170 {
3171 bfd_boolean binding_stays_local_p;
3172 const struct elf_backend_data *bed;
3173 struct elf_link_hash_table *hash_table;
3174
3175 if (h == NULL)
3176 return FALSE;
3177
3178 while (h->root.type == bfd_link_hash_indirect
3179 || h->root.type == bfd_link_hash_warning)
3180 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3181
3182 /* If it was forced local, then clearly it's not dynamic. */
3183 if (h->dynindx == -1)
3184 return FALSE;
3185 if (h->forced_local)
3186 return FALSE;
3187
3188 /* Identify the cases where name binding rules say that a
3189 visible symbol resolves locally. */
3190 binding_stays_local_p = (bfd_link_executable (info)
3191 || SYMBOLIC_BIND (info, h));
3192
3193 switch (ELF_ST_VISIBILITY (h->other))
3194 {
3195 case STV_INTERNAL:
3196 case STV_HIDDEN:
3197 return FALSE;
3198
3199 case STV_PROTECTED:
3200 hash_table = elf_hash_table (info);
3201 if (!is_elf_hash_table (hash_table))
3202 return FALSE;
3203
3204 bed = get_elf_backend_data (hash_table->dynobj);
3205
3206 /* Proper resolution for function pointer equality may require
3207 that these symbols perhaps be resolved dynamically, even though
3208 we should be resolving them to the current module. */
3209 if (!not_local_protected || !bed->is_function_type (h->type))
3210 binding_stays_local_p = TRUE;
3211 break;
3212
3213 default:
3214 break;
3215 }
3216
3217 /* If it isn't defined locally, then clearly it's dynamic. */
3218 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3219 return TRUE;
3220
3221 /* Otherwise, the symbol is dynamic if binding rules don't tell
3222 us that it remains local. */
3223 return !binding_stays_local_p;
3224 }
3225
3226 /* Return true if the symbol referred to by H should be considered
3227 to resolve local to the current module, and false otherwise. Differs
3228 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3229 undefined symbols. The two functions are virtually identical except
3230 for the place where dynindx == -1 is tested. If that test is true,
3231 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3232 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3233 defined symbols.
3234 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3235 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3236 treatment of undefined weak symbols. For those that do not make
3237 undefined weak symbols dynamic, both functions may return false. */
3238
3239 bfd_boolean
3240 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3241 struct bfd_link_info *info,
3242 bfd_boolean local_protected)
3243 {
3244 const struct elf_backend_data *bed;
3245 struct elf_link_hash_table *hash_table;
3246
3247 /* If it's a local sym, of course we resolve locally. */
3248 if (h == NULL)
3249 return TRUE;
3250
3251 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3252 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3253 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3254 return TRUE;
3255
3256 /* Forced local symbols resolve locally. */
3257 if (h->forced_local)
3258 return TRUE;
3259
3260 /* Common symbols that become definitions don't get the DEF_REGULAR
3261 flag set, so test it first, and don't bail out. */
3262 if (ELF_COMMON_DEF_P (h))
3263 /* Do nothing. */;
3264 /* If we don't have a definition in a regular file, then we can't
3265 resolve locally. The sym is either undefined or dynamic. */
3266 else if (!h->def_regular)
3267 return FALSE;
3268
3269 /* Non-dynamic symbols resolve locally. */
3270 if (h->dynindx == -1)
3271 return TRUE;
3272
3273 /* At this point, we know the symbol is defined and dynamic. In an
3274 executable it must resolve locally, likewise when building symbolic
3275 shared libraries. */
3276 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3277 return TRUE;
3278
3279 /* Now deal with defined dynamic symbols in shared libraries. Ones
3280 with default visibility might not resolve locally. */
3281 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3282 return FALSE;
3283
3284 hash_table = elf_hash_table (info);
3285 if (!is_elf_hash_table (hash_table))
3286 return TRUE;
3287
3288 bed = get_elf_backend_data (hash_table->dynobj);
3289
3290 /* If extern_protected_data is false, STV_PROTECTED non-function
3291 symbols are local. */
3292 if ((!info->extern_protected_data
3293 || (info->extern_protected_data < 0
3294 && !bed->extern_protected_data))
3295 && !bed->is_function_type (h->type))
3296 return TRUE;
3297
3298 /* Function pointer equality tests may require that STV_PROTECTED
3299 symbols be treated as dynamic symbols. If the address of a
3300 function not defined in an executable is set to that function's
3301 plt entry in the executable, then the address of the function in
3302 a shared library must also be the plt entry in the executable. */
3303 return local_protected;
3304 }
3305
3306 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3307 aligned. Returns the first TLS output section. */
3308
3309 struct bfd_section *
3310 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3311 {
3312 struct bfd_section *sec, *tls;
3313 unsigned int align = 0;
3314
3315 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3316 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3317 break;
3318 tls = sec;
3319
3320 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3321 if (sec->alignment_power > align)
3322 align = sec->alignment_power;
3323
3324 elf_hash_table (info)->tls_sec = tls;
3325
3326 /* Ensure the alignment of the first section is the largest alignment,
3327 so that the tls segment starts aligned. */
3328 if (tls != NULL)
3329 tls->alignment_power = align;
3330
3331 return tls;
3332 }
3333
3334 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3335 static bfd_boolean
3336 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3337 Elf_Internal_Sym *sym)
3338 {
3339 const struct elf_backend_data *bed;
3340
3341 /* Local symbols do not count, but target specific ones might. */
3342 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3343 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3344 return FALSE;
3345
3346 bed = get_elf_backend_data (abfd);
3347 /* Function symbols do not count. */
3348 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3349 return FALSE;
3350
3351 /* If the section is undefined, then so is the symbol. */
3352 if (sym->st_shndx == SHN_UNDEF)
3353 return FALSE;
3354
3355 /* If the symbol is defined in the common section, then
3356 it is a common definition and so does not count. */
3357 if (bed->common_definition (sym))
3358 return FALSE;
3359
3360 /* If the symbol is in a target specific section then we
3361 must rely upon the backend to tell us what it is. */
3362 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3363 /* FIXME - this function is not coded yet:
3364
3365 return _bfd_is_global_symbol_definition (abfd, sym);
3366
3367 Instead for now assume that the definition is not global,
3368 Even if this is wrong, at least the linker will behave
3369 in the same way that it used to do. */
3370 return FALSE;
3371
3372 return TRUE;
3373 }
3374
3375 /* Search the symbol table of the archive element of the archive ABFD
3376 whose archive map contains a mention of SYMDEF, and determine if
3377 the symbol is defined in this element. */
3378 static bfd_boolean
3379 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3380 {
3381 Elf_Internal_Shdr * hdr;
3382 size_t symcount;
3383 size_t extsymcount;
3384 size_t extsymoff;
3385 Elf_Internal_Sym *isymbuf;
3386 Elf_Internal_Sym *isym;
3387 Elf_Internal_Sym *isymend;
3388 bfd_boolean result;
3389
3390 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3391 if (abfd == NULL)
3392 return FALSE;
3393
3394 if (! bfd_check_format (abfd, bfd_object))
3395 return FALSE;
3396
3397 /* Select the appropriate symbol table. If we don't know if the
3398 object file is an IR object, give linker LTO plugin a chance to
3399 get the correct symbol table. */
3400 if (abfd->plugin_format == bfd_plugin_yes
3401 #if BFD_SUPPORTS_PLUGINS
3402 || (abfd->plugin_format == bfd_plugin_unknown
3403 && bfd_link_plugin_object_p (abfd))
3404 #endif
3405 )
3406 {
3407 /* Use the IR symbol table if the object has been claimed by
3408 plugin. */
3409 abfd = abfd->plugin_dummy_bfd;
3410 hdr = &elf_tdata (abfd)->symtab_hdr;
3411 }
3412 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3413 hdr = &elf_tdata (abfd)->symtab_hdr;
3414 else
3415 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3416
3417 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3418
3419 /* The sh_info field of the symtab header tells us where the
3420 external symbols start. We don't care about the local symbols. */
3421 if (elf_bad_symtab (abfd))
3422 {
3423 extsymcount = symcount;
3424 extsymoff = 0;
3425 }
3426 else
3427 {
3428 extsymcount = symcount - hdr->sh_info;
3429 extsymoff = hdr->sh_info;
3430 }
3431
3432 if (extsymcount == 0)
3433 return FALSE;
3434
3435 /* Read in the symbol table. */
3436 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3437 NULL, NULL, NULL);
3438 if (isymbuf == NULL)
3439 return FALSE;
3440
3441 /* Scan the symbol table looking for SYMDEF. */
3442 result = FALSE;
3443 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3444 {
3445 const char *name;
3446
3447 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3448 isym->st_name);
3449 if (name == NULL)
3450 break;
3451
3452 if (strcmp (name, symdef->name) == 0)
3453 {
3454 result = is_global_data_symbol_definition (abfd, isym);
3455 break;
3456 }
3457 }
3458
3459 free (isymbuf);
3460
3461 return result;
3462 }
3463 \f
3464 /* Add an entry to the .dynamic table. */
3465
3466 bfd_boolean
3467 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3468 bfd_vma tag,
3469 bfd_vma val)
3470 {
3471 struct elf_link_hash_table *hash_table;
3472 const struct elf_backend_data *bed;
3473 asection *s;
3474 bfd_size_type newsize;
3475 bfd_byte *newcontents;
3476 Elf_Internal_Dyn dyn;
3477
3478 hash_table = elf_hash_table (info);
3479 if (! is_elf_hash_table (hash_table))
3480 return FALSE;
3481
3482 if (tag == DT_RELA || tag == DT_REL)
3483 hash_table->dynamic_relocs = TRUE;
3484
3485 bed = get_elf_backend_data (hash_table->dynobj);
3486 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3487 BFD_ASSERT (s != NULL);
3488
3489 newsize = s->size + bed->s->sizeof_dyn;
3490 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3491 if (newcontents == NULL)
3492 return FALSE;
3493
3494 dyn.d_tag = tag;
3495 dyn.d_un.d_val = val;
3496 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3497
3498 s->size = newsize;
3499 s->contents = newcontents;
3500
3501 return TRUE;
3502 }
3503
3504 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3505 otherwise just check whether one already exists. Returns -1 on error,
3506 1 if a DT_NEEDED tag already exists, and 0 on success. */
3507
3508 static int
3509 elf_add_dt_needed_tag (bfd *abfd,
3510 struct bfd_link_info *info,
3511 const char *soname,
3512 bfd_boolean do_it)
3513 {
3514 struct elf_link_hash_table *hash_table;
3515 size_t strindex;
3516
3517 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3518 return -1;
3519
3520 hash_table = elf_hash_table (info);
3521 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3522 if (strindex == (size_t) -1)
3523 return -1;
3524
3525 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3526 {
3527 asection *sdyn;
3528 const struct elf_backend_data *bed;
3529 bfd_byte *extdyn;
3530
3531 bed = get_elf_backend_data (hash_table->dynobj);
3532 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3533 if (sdyn != NULL)
3534 for (extdyn = sdyn->contents;
3535 extdyn < sdyn->contents + sdyn->size;
3536 extdyn += bed->s->sizeof_dyn)
3537 {
3538 Elf_Internal_Dyn dyn;
3539
3540 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3541 if (dyn.d_tag == DT_NEEDED
3542 && dyn.d_un.d_val == strindex)
3543 {
3544 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3545 return 1;
3546 }
3547 }
3548 }
3549
3550 if (do_it)
3551 {
3552 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3553 return -1;
3554
3555 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3556 return -1;
3557 }
3558 else
3559 /* We were just checking for existence of the tag. */
3560 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3561
3562 return 0;
3563 }
3564
3565 /* Return true if SONAME is on the needed list between NEEDED and STOP
3566 (or the end of list if STOP is NULL), and needed by a library that
3567 will be loaded. */
3568
3569 static bfd_boolean
3570 on_needed_list (const char *soname,
3571 struct bfd_link_needed_list *needed,
3572 struct bfd_link_needed_list *stop)
3573 {
3574 struct bfd_link_needed_list *look;
3575 for (look = needed; look != stop; look = look->next)
3576 if (strcmp (soname, look->name) == 0
3577 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3578 /* If needed by a library that itself is not directly
3579 needed, recursively check whether that library is
3580 indirectly needed. Since we add DT_NEEDED entries to
3581 the end of the list, library dependencies appear after
3582 the library. Therefore search prior to the current
3583 LOOK, preventing possible infinite recursion. */
3584 || on_needed_list (elf_dt_name (look->by), needed, look)))
3585 return TRUE;
3586
3587 return FALSE;
3588 }
3589
3590 /* Sort symbol by value, section, size, and type. */
3591 static int
3592 elf_sort_symbol (const void *arg1, const void *arg2)
3593 {
3594 const struct elf_link_hash_entry *h1;
3595 const struct elf_link_hash_entry *h2;
3596 bfd_signed_vma vdiff;
3597 int sdiff;
3598 const char *n1;
3599 const char *n2;
3600
3601 h1 = *(const struct elf_link_hash_entry **) arg1;
3602 h2 = *(const struct elf_link_hash_entry **) arg2;
3603 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3604 if (vdiff != 0)
3605 return vdiff > 0 ? 1 : -1;
3606
3607 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3608 if (sdiff != 0)
3609 return sdiff;
3610
3611 /* Sort so that sized symbols are selected over zero size symbols. */
3612 vdiff = h1->size - h2->size;
3613 if (vdiff != 0)
3614 return vdiff > 0 ? 1 : -1;
3615
3616 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3617 if (h1->type != h2->type)
3618 return h1->type - h2->type;
3619
3620 /* If symbols are properly sized and typed, and multiple strong
3621 aliases are not defined in a shared library by the user we
3622 shouldn't get here. Unfortunately linker script symbols like
3623 __bss_start sometimes match a user symbol defined at the start of
3624 .bss without proper size and type. We'd like to preference the
3625 user symbol over reserved system symbols. Sort on leading
3626 underscores. */
3627 n1 = h1->root.root.string;
3628 n2 = h2->root.root.string;
3629 while (*n1 == *n2)
3630 {
3631 if (*n1 == 0)
3632 break;
3633 ++n1;
3634 ++n2;
3635 }
3636 if (*n1 == '_')
3637 return -1;
3638 if (*n2 == '_')
3639 return 1;
3640
3641 /* Final sort on name selects user symbols like '_u' over reserved
3642 system symbols like '_Z' and also will avoid qsort instability. */
3643 return *n1 - *n2;
3644 }
3645
3646 /* This function is used to adjust offsets into .dynstr for
3647 dynamic symbols. This is called via elf_link_hash_traverse. */
3648
3649 static bfd_boolean
3650 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3651 {
3652 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3653
3654 if (h->dynindx != -1)
3655 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3656 return TRUE;
3657 }
3658
3659 /* Assign string offsets in .dynstr, update all structures referencing
3660 them. */
3661
3662 static bfd_boolean
3663 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3664 {
3665 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3666 struct elf_link_local_dynamic_entry *entry;
3667 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3668 bfd *dynobj = hash_table->dynobj;
3669 asection *sdyn;
3670 bfd_size_type size;
3671 const struct elf_backend_data *bed;
3672 bfd_byte *extdyn;
3673
3674 _bfd_elf_strtab_finalize (dynstr);
3675 size = _bfd_elf_strtab_size (dynstr);
3676
3677 bed = get_elf_backend_data (dynobj);
3678 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3679 BFD_ASSERT (sdyn != NULL);
3680
3681 /* Update all .dynamic entries referencing .dynstr strings. */
3682 for (extdyn = sdyn->contents;
3683 extdyn < sdyn->contents + sdyn->size;
3684 extdyn += bed->s->sizeof_dyn)
3685 {
3686 Elf_Internal_Dyn dyn;
3687
3688 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3689 switch (dyn.d_tag)
3690 {
3691 case DT_STRSZ:
3692 dyn.d_un.d_val = size;
3693 break;
3694 case DT_NEEDED:
3695 case DT_SONAME:
3696 case DT_RPATH:
3697 case DT_RUNPATH:
3698 case DT_FILTER:
3699 case DT_AUXILIARY:
3700 case DT_AUDIT:
3701 case DT_DEPAUDIT:
3702 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3703 break;
3704 default:
3705 continue;
3706 }
3707 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3708 }
3709
3710 /* Now update local dynamic symbols. */
3711 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3712 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3713 entry->isym.st_name);
3714
3715 /* And the rest of dynamic symbols. */
3716 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3717
3718 /* Adjust version definitions. */
3719 if (elf_tdata (output_bfd)->cverdefs)
3720 {
3721 asection *s;
3722 bfd_byte *p;
3723 size_t i;
3724 Elf_Internal_Verdef def;
3725 Elf_Internal_Verdaux defaux;
3726
3727 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3728 p = s->contents;
3729 do
3730 {
3731 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3732 &def);
3733 p += sizeof (Elf_External_Verdef);
3734 if (def.vd_aux != sizeof (Elf_External_Verdef))
3735 continue;
3736 for (i = 0; i < def.vd_cnt; ++i)
3737 {
3738 _bfd_elf_swap_verdaux_in (output_bfd,
3739 (Elf_External_Verdaux *) p, &defaux);
3740 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3741 defaux.vda_name);
3742 _bfd_elf_swap_verdaux_out (output_bfd,
3743 &defaux, (Elf_External_Verdaux *) p);
3744 p += sizeof (Elf_External_Verdaux);
3745 }
3746 }
3747 while (def.vd_next);
3748 }
3749
3750 /* Adjust version references. */
3751 if (elf_tdata (output_bfd)->verref)
3752 {
3753 asection *s;
3754 bfd_byte *p;
3755 size_t i;
3756 Elf_Internal_Verneed need;
3757 Elf_Internal_Vernaux needaux;
3758
3759 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3760 p = s->contents;
3761 do
3762 {
3763 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3764 &need);
3765 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3766 _bfd_elf_swap_verneed_out (output_bfd, &need,
3767 (Elf_External_Verneed *) p);
3768 p += sizeof (Elf_External_Verneed);
3769 for (i = 0; i < need.vn_cnt; ++i)
3770 {
3771 _bfd_elf_swap_vernaux_in (output_bfd,
3772 (Elf_External_Vernaux *) p, &needaux);
3773 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3774 needaux.vna_name);
3775 _bfd_elf_swap_vernaux_out (output_bfd,
3776 &needaux,
3777 (Elf_External_Vernaux *) p);
3778 p += sizeof (Elf_External_Vernaux);
3779 }
3780 }
3781 while (need.vn_next);
3782 }
3783
3784 return TRUE;
3785 }
3786 \f
3787 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3788 The default is to only match when the INPUT and OUTPUT are exactly
3789 the same target. */
3790
3791 bfd_boolean
3792 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3793 const bfd_target *output)
3794 {
3795 return input == output;
3796 }
3797
3798 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3799 This version is used when different targets for the same architecture
3800 are virtually identical. */
3801
3802 bfd_boolean
3803 _bfd_elf_relocs_compatible (const bfd_target *input,
3804 const bfd_target *output)
3805 {
3806 const struct elf_backend_data *obed, *ibed;
3807
3808 if (input == output)
3809 return TRUE;
3810
3811 ibed = xvec_get_elf_backend_data (input);
3812 obed = xvec_get_elf_backend_data (output);
3813
3814 if (ibed->arch != obed->arch)
3815 return FALSE;
3816
3817 /* If both backends are using this function, deem them compatible. */
3818 return ibed->relocs_compatible == obed->relocs_compatible;
3819 }
3820
3821 /* Make a special call to the linker "notice" function to tell it that
3822 we are about to handle an as-needed lib, or have finished
3823 processing the lib. */
3824
3825 bfd_boolean
3826 _bfd_elf_notice_as_needed (bfd *ibfd,
3827 struct bfd_link_info *info,
3828 enum notice_asneeded_action act)
3829 {
3830 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3831 }
3832
3833 /* Check relocations an ELF object file. */
3834
3835 bfd_boolean
3836 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3837 {
3838 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3839 struct elf_link_hash_table *htab = elf_hash_table (info);
3840
3841 /* If this object is the same format as the output object, and it is
3842 not a shared library, then let the backend look through the
3843 relocs.
3844
3845 This is required to build global offset table entries and to
3846 arrange for dynamic relocs. It is not required for the
3847 particular common case of linking non PIC code, even when linking
3848 against shared libraries, but unfortunately there is no way of
3849 knowing whether an object file has been compiled PIC or not.
3850 Looking through the relocs is not particularly time consuming.
3851 The problem is that we must either (1) keep the relocs in memory,
3852 which causes the linker to require additional runtime memory or
3853 (2) read the relocs twice from the input file, which wastes time.
3854 This would be a good case for using mmap.
3855
3856 I have no idea how to handle linking PIC code into a file of a
3857 different format. It probably can't be done. */
3858 if ((abfd->flags & DYNAMIC) == 0
3859 && is_elf_hash_table (htab)
3860 && bed->check_relocs != NULL
3861 && elf_object_id (abfd) == elf_hash_table_id (htab)
3862 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3863 {
3864 asection *o;
3865
3866 for (o = abfd->sections; o != NULL; o = o->next)
3867 {
3868 Elf_Internal_Rela *internal_relocs;
3869 bfd_boolean ok;
3870
3871 /* Don't check relocations in excluded sections. */
3872 if ((o->flags & SEC_RELOC) == 0
3873 || (o->flags & SEC_EXCLUDE) != 0
3874 || o->reloc_count == 0
3875 || ((info->strip == strip_all || info->strip == strip_debugger)
3876 && (o->flags & SEC_DEBUGGING) != 0)
3877 || bfd_is_abs_section (o->output_section))
3878 continue;
3879
3880 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3881 info->keep_memory);
3882 if (internal_relocs == NULL)
3883 return FALSE;
3884
3885 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3886
3887 if (elf_section_data (o)->relocs != internal_relocs)
3888 free (internal_relocs);
3889
3890 if (! ok)
3891 return FALSE;
3892 }
3893 }
3894
3895 return TRUE;
3896 }
3897
3898 /* Add symbols from an ELF object file to the linker hash table. */
3899
3900 static bfd_boolean
3901 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3902 {
3903 Elf_Internal_Ehdr *ehdr;
3904 Elf_Internal_Shdr *hdr;
3905 size_t symcount;
3906 size_t extsymcount;
3907 size_t extsymoff;
3908 struct elf_link_hash_entry **sym_hash;
3909 bfd_boolean dynamic;
3910 Elf_External_Versym *extversym = NULL;
3911 Elf_External_Versym *extversym_end = NULL;
3912 Elf_External_Versym *ever;
3913 struct elf_link_hash_entry *weaks;
3914 struct elf_link_hash_entry **nondeflt_vers = NULL;
3915 size_t nondeflt_vers_cnt = 0;
3916 Elf_Internal_Sym *isymbuf = NULL;
3917 Elf_Internal_Sym *isym;
3918 Elf_Internal_Sym *isymend;
3919 const struct elf_backend_data *bed;
3920 bfd_boolean add_needed;
3921 struct elf_link_hash_table *htab;
3922 void *alloc_mark = NULL;
3923 struct bfd_hash_entry **old_table = NULL;
3924 unsigned int old_size = 0;
3925 unsigned int old_count = 0;
3926 void *old_tab = NULL;
3927 void *old_ent;
3928 struct bfd_link_hash_entry *old_undefs = NULL;
3929 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3930 void *old_strtab = NULL;
3931 size_t tabsize = 0;
3932 asection *s;
3933 bfd_boolean just_syms;
3934
3935 htab = elf_hash_table (info);
3936 bed = get_elf_backend_data (abfd);
3937
3938 if ((abfd->flags & DYNAMIC) == 0)
3939 dynamic = FALSE;
3940 else
3941 {
3942 dynamic = TRUE;
3943
3944 /* You can't use -r against a dynamic object. Also, there's no
3945 hope of using a dynamic object which does not exactly match
3946 the format of the output file. */
3947 if (bfd_link_relocatable (info)
3948 || !is_elf_hash_table (htab)
3949 || info->output_bfd->xvec != abfd->xvec)
3950 {
3951 if (bfd_link_relocatable (info))
3952 bfd_set_error (bfd_error_invalid_operation);
3953 else
3954 bfd_set_error (bfd_error_wrong_format);
3955 goto error_return;
3956 }
3957 }
3958
3959 ehdr = elf_elfheader (abfd);
3960 if (info->warn_alternate_em
3961 && bed->elf_machine_code != ehdr->e_machine
3962 && ((bed->elf_machine_alt1 != 0
3963 && ehdr->e_machine == bed->elf_machine_alt1)
3964 || (bed->elf_machine_alt2 != 0
3965 && ehdr->e_machine == bed->elf_machine_alt2)))
3966 _bfd_error_handler
3967 /* xgettext:c-format */
3968 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
3969 ehdr->e_machine, abfd, bed->elf_machine_code);
3970
3971 /* As a GNU extension, any input sections which are named
3972 .gnu.warning.SYMBOL are treated as warning symbols for the given
3973 symbol. This differs from .gnu.warning sections, which generate
3974 warnings when they are included in an output file. */
3975 /* PR 12761: Also generate this warning when building shared libraries. */
3976 for (s = abfd->sections; s != NULL; s = s->next)
3977 {
3978 const char *name;
3979
3980 name = bfd_section_name (s);
3981 if (CONST_STRNEQ (name, ".gnu.warning."))
3982 {
3983 char *msg;
3984 bfd_size_type sz;
3985
3986 name += sizeof ".gnu.warning." - 1;
3987
3988 /* If this is a shared object, then look up the symbol
3989 in the hash table. If it is there, and it is already
3990 been defined, then we will not be using the entry
3991 from this shared object, so we don't need to warn.
3992 FIXME: If we see the definition in a regular object
3993 later on, we will warn, but we shouldn't. The only
3994 fix is to keep track of what warnings we are supposed
3995 to emit, and then handle them all at the end of the
3996 link. */
3997 if (dynamic)
3998 {
3999 struct elf_link_hash_entry *h;
4000
4001 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
4002
4003 /* FIXME: What about bfd_link_hash_common? */
4004 if (h != NULL
4005 && (h->root.type == bfd_link_hash_defined
4006 || h->root.type == bfd_link_hash_defweak))
4007 continue;
4008 }
4009
4010 sz = s->size;
4011 msg = (char *) bfd_alloc (abfd, sz + 1);
4012 if (msg == NULL)
4013 goto error_return;
4014
4015 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4016 goto error_return;
4017
4018 msg[sz] = '\0';
4019
4020 if (! (_bfd_generic_link_add_one_symbol
4021 (info, abfd, name, BSF_WARNING, s, 0, msg,
4022 FALSE, bed->collect, NULL)))
4023 goto error_return;
4024
4025 if (bfd_link_executable (info))
4026 {
4027 /* Clobber the section size so that the warning does
4028 not get copied into the output file. */
4029 s->size = 0;
4030
4031 /* Also set SEC_EXCLUDE, so that symbols defined in
4032 the warning section don't get copied to the output. */
4033 s->flags |= SEC_EXCLUDE;
4034 }
4035 }
4036 }
4037
4038 just_syms = ((s = abfd->sections) != NULL
4039 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4040
4041 add_needed = TRUE;
4042 if (! dynamic)
4043 {
4044 /* If we are creating a shared library, create all the dynamic
4045 sections immediately. We need to attach them to something,
4046 so we attach them to this BFD, provided it is the right
4047 format and is not from ld --just-symbols. Always create the
4048 dynamic sections for -E/--dynamic-list. FIXME: If there
4049 are no input BFD's of the same format as the output, we can't
4050 make a shared library. */
4051 if (!just_syms
4052 && (bfd_link_pic (info)
4053 || (!bfd_link_relocatable (info)
4054 && info->nointerp
4055 && (info->export_dynamic || info->dynamic)))
4056 && is_elf_hash_table (htab)
4057 && info->output_bfd->xvec == abfd->xvec
4058 && !htab->dynamic_sections_created)
4059 {
4060 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4061 goto error_return;
4062 }
4063 }
4064 else if (!is_elf_hash_table (htab))
4065 goto error_return;
4066 else
4067 {
4068 const char *soname = NULL;
4069 char *audit = NULL;
4070 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4071 const Elf_Internal_Phdr *phdr;
4072 int ret;
4073
4074 /* ld --just-symbols and dynamic objects don't mix very well.
4075 ld shouldn't allow it. */
4076 if (just_syms)
4077 abort ();
4078
4079 /* If this dynamic lib was specified on the command line with
4080 --as-needed in effect, then we don't want to add a DT_NEEDED
4081 tag unless the lib is actually used. Similary for libs brought
4082 in by another lib's DT_NEEDED. When --no-add-needed is used
4083 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4084 any dynamic library in DT_NEEDED tags in the dynamic lib at
4085 all. */
4086 add_needed = (elf_dyn_lib_class (abfd)
4087 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4088 | DYN_NO_NEEDED)) == 0;
4089
4090 s = bfd_get_section_by_name (abfd, ".dynamic");
4091 if (s != NULL)
4092 {
4093 bfd_byte *dynbuf;
4094 bfd_byte *extdyn;
4095 unsigned int elfsec;
4096 unsigned long shlink;
4097
4098 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4099 {
4100 error_free_dyn:
4101 free (dynbuf);
4102 goto error_return;
4103 }
4104
4105 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4106 if (elfsec == SHN_BAD)
4107 goto error_free_dyn;
4108 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4109
4110 for (extdyn = dynbuf;
4111 extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
4112 extdyn += bed->s->sizeof_dyn)
4113 {
4114 Elf_Internal_Dyn dyn;
4115
4116 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4117 if (dyn.d_tag == DT_SONAME)
4118 {
4119 unsigned int tagv = dyn.d_un.d_val;
4120 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4121 if (soname == NULL)
4122 goto error_free_dyn;
4123 }
4124 if (dyn.d_tag == DT_NEEDED)
4125 {
4126 struct bfd_link_needed_list *n, **pn;
4127 char *fnm, *anm;
4128 unsigned int tagv = dyn.d_un.d_val;
4129 size_t amt = sizeof (struct bfd_link_needed_list);
4130
4131 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4132 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4133 if (n == NULL || fnm == NULL)
4134 goto error_free_dyn;
4135 amt = strlen (fnm) + 1;
4136 anm = (char *) bfd_alloc (abfd, amt);
4137 if (anm == NULL)
4138 goto error_free_dyn;
4139 memcpy (anm, fnm, amt);
4140 n->name = anm;
4141 n->by = abfd;
4142 n->next = NULL;
4143 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4144 ;
4145 *pn = n;
4146 }
4147 if (dyn.d_tag == DT_RUNPATH)
4148 {
4149 struct bfd_link_needed_list *n, **pn;
4150 char *fnm, *anm;
4151 unsigned int tagv = dyn.d_un.d_val;
4152 size_t amt = sizeof (struct bfd_link_needed_list);
4153
4154 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4155 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4156 if (n == NULL || fnm == NULL)
4157 goto error_free_dyn;
4158 amt = strlen (fnm) + 1;
4159 anm = (char *) bfd_alloc (abfd, amt);
4160 if (anm == NULL)
4161 goto error_free_dyn;
4162 memcpy (anm, fnm, amt);
4163 n->name = anm;
4164 n->by = abfd;
4165 n->next = NULL;
4166 for (pn = & runpath;
4167 *pn != NULL;
4168 pn = &(*pn)->next)
4169 ;
4170 *pn = n;
4171 }
4172 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4173 if (!runpath && dyn.d_tag == DT_RPATH)
4174 {
4175 struct bfd_link_needed_list *n, **pn;
4176 char *fnm, *anm;
4177 unsigned int tagv = dyn.d_un.d_val;
4178 size_t amt = sizeof (struct bfd_link_needed_list);
4179
4180 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4181 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4182 if (n == NULL || fnm == NULL)
4183 goto error_free_dyn;
4184 amt = strlen (fnm) + 1;
4185 anm = (char *) bfd_alloc (abfd, amt);
4186 if (anm == NULL)
4187 goto error_free_dyn;
4188 memcpy (anm, fnm, amt);
4189 n->name = anm;
4190 n->by = abfd;
4191 n->next = NULL;
4192 for (pn = & rpath;
4193 *pn != NULL;
4194 pn = &(*pn)->next)
4195 ;
4196 *pn = n;
4197 }
4198 if (dyn.d_tag == DT_AUDIT)
4199 {
4200 unsigned int tagv = dyn.d_un.d_val;
4201 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4202 }
4203 }
4204
4205 free (dynbuf);
4206 }
4207
4208 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4209 frees all more recently bfd_alloc'd blocks as well. */
4210 if (runpath)
4211 rpath = runpath;
4212
4213 if (rpath)
4214 {
4215 struct bfd_link_needed_list **pn;
4216 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4217 ;
4218 *pn = rpath;
4219 }
4220
4221 /* If we have a PT_GNU_RELRO program header, mark as read-only
4222 all sections contained fully therein. This makes relro
4223 shared library sections appear as they will at run-time. */
4224 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4225 while (phdr-- > elf_tdata (abfd)->phdr)
4226 if (phdr->p_type == PT_GNU_RELRO)
4227 {
4228 for (s = abfd->sections; s != NULL; s = s->next)
4229 if ((s->flags & SEC_ALLOC) != 0
4230 && s->vma >= phdr->p_vaddr
4231 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4232 s->flags |= SEC_READONLY;
4233 break;
4234 }
4235
4236 /* We do not want to include any of the sections in a dynamic
4237 object in the output file. We hack by simply clobbering the
4238 list of sections in the BFD. This could be handled more
4239 cleanly by, say, a new section flag; the existing
4240 SEC_NEVER_LOAD flag is not the one we want, because that one
4241 still implies that the section takes up space in the output
4242 file. */
4243 bfd_section_list_clear (abfd);
4244
4245 /* Find the name to use in a DT_NEEDED entry that refers to this
4246 object. If the object has a DT_SONAME entry, we use it.
4247 Otherwise, if the generic linker stuck something in
4248 elf_dt_name, we use that. Otherwise, we just use the file
4249 name. */
4250 if (soname == NULL || *soname == '\0')
4251 {
4252 soname = elf_dt_name (abfd);
4253 if (soname == NULL || *soname == '\0')
4254 soname = bfd_get_filename (abfd);
4255 }
4256
4257 /* Save the SONAME because sometimes the linker emulation code
4258 will need to know it. */
4259 elf_dt_name (abfd) = soname;
4260
4261 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4262 if (ret < 0)
4263 goto error_return;
4264
4265 /* If we have already included this dynamic object in the
4266 link, just ignore it. There is no reason to include a
4267 particular dynamic object more than once. */
4268 if (ret > 0)
4269 return TRUE;
4270
4271 /* Save the DT_AUDIT entry for the linker emulation code. */
4272 elf_dt_audit (abfd) = audit;
4273 }
4274
4275 /* If this is a dynamic object, we always link against the .dynsym
4276 symbol table, not the .symtab symbol table. The dynamic linker
4277 will only see the .dynsym symbol table, so there is no reason to
4278 look at .symtab for a dynamic object. */
4279
4280 if (! dynamic || elf_dynsymtab (abfd) == 0)
4281 hdr = &elf_tdata (abfd)->symtab_hdr;
4282 else
4283 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4284
4285 symcount = hdr->sh_size / bed->s->sizeof_sym;
4286
4287 /* The sh_info field of the symtab header tells us where the
4288 external symbols start. We don't care about the local symbols at
4289 this point. */
4290 if (elf_bad_symtab (abfd))
4291 {
4292 extsymcount = symcount;
4293 extsymoff = 0;
4294 }
4295 else
4296 {
4297 extsymcount = symcount - hdr->sh_info;
4298 extsymoff = hdr->sh_info;
4299 }
4300
4301 sym_hash = elf_sym_hashes (abfd);
4302 if (extsymcount != 0)
4303 {
4304 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4305 NULL, NULL, NULL);
4306 if (isymbuf == NULL)
4307 goto error_return;
4308
4309 if (sym_hash == NULL)
4310 {
4311 /* We store a pointer to the hash table entry for each
4312 external symbol. */
4313 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4314 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4315 if (sym_hash == NULL)
4316 goto error_free_sym;
4317 elf_sym_hashes (abfd) = sym_hash;
4318 }
4319 }
4320
4321 if (dynamic)
4322 {
4323 /* Read in any version definitions. */
4324 if (!_bfd_elf_slurp_version_tables (abfd,
4325 info->default_imported_symver))
4326 goto error_free_sym;
4327
4328 /* Read in the symbol versions, but don't bother to convert them
4329 to internal format. */
4330 if (elf_dynversym (abfd) != 0)
4331 {
4332 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4333 bfd_size_type amt = versymhdr->sh_size;
4334
4335 extversym = (Elf_External_Versym *) bfd_malloc (amt);
4336 if (extversym == NULL)
4337 goto error_free_sym;
4338 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4339 || bfd_bread (extversym, amt, abfd) != amt)
4340 goto error_free_vers;
4341 extversym_end = extversym + amt / sizeof (*extversym);
4342 }
4343 }
4344
4345 /* If we are loading an as-needed shared lib, save the symbol table
4346 state before we start adding symbols. If the lib turns out
4347 to be unneeded, restore the state. */
4348 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4349 {
4350 unsigned int i;
4351 size_t entsize;
4352
4353 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4354 {
4355 struct bfd_hash_entry *p;
4356 struct elf_link_hash_entry *h;
4357
4358 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4359 {
4360 h = (struct elf_link_hash_entry *) p;
4361 entsize += htab->root.table.entsize;
4362 if (h->root.type == bfd_link_hash_warning)
4363 entsize += htab->root.table.entsize;
4364 }
4365 }
4366
4367 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4368 old_tab = bfd_malloc (tabsize + entsize);
4369 if (old_tab == NULL)
4370 goto error_free_vers;
4371
4372 /* Remember the current objalloc pointer, so that all mem for
4373 symbols added can later be reclaimed. */
4374 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4375 if (alloc_mark == NULL)
4376 goto error_free_vers;
4377
4378 /* Make a special call to the linker "notice" function to
4379 tell it that we are about to handle an as-needed lib. */
4380 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4381 goto error_free_vers;
4382
4383 /* Clone the symbol table. Remember some pointers into the
4384 symbol table, and dynamic symbol count. */
4385 old_ent = (char *) old_tab + tabsize;
4386 memcpy (old_tab, htab->root.table.table, tabsize);
4387 old_undefs = htab->root.undefs;
4388 old_undefs_tail = htab->root.undefs_tail;
4389 old_table = htab->root.table.table;
4390 old_size = htab->root.table.size;
4391 old_count = htab->root.table.count;
4392 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4393 if (old_strtab == NULL)
4394 goto error_free_vers;
4395
4396 for (i = 0; i < htab->root.table.size; i++)
4397 {
4398 struct bfd_hash_entry *p;
4399 struct elf_link_hash_entry *h;
4400
4401 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4402 {
4403 memcpy (old_ent, p, htab->root.table.entsize);
4404 old_ent = (char *) old_ent + htab->root.table.entsize;
4405 h = (struct elf_link_hash_entry *) p;
4406 if (h->root.type == bfd_link_hash_warning)
4407 {
4408 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4409 old_ent = (char *) old_ent + htab->root.table.entsize;
4410 }
4411 }
4412 }
4413 }
4414
4415 weaks = NULL;
4416 if (extversym == NULL)
4417 ever = NULL;
4418 else if (extversym + extsymoff < extversym_end)
4419 ever = extversym + extsymoff;
4420 else
4421 {
4422 /* xgettext:c-format */
4423 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4424 abfd, (long) extsymoff,
4425 (long) (extversym_end - extversym) / sizeof (* extversym));
4426 bfd_set_error (bfd_error_bad_value);
4427 goto error_free_vers;
4428 }
4429
4430 if (!bfd_link_relocatable (info)
4431 && abfd->lto_slim_object)
4432 {
4433 _bfd_error_handler
4434 (_("%pB: plugin needed to handle lto object"), abfd);
4435 }
4436
4437 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4438 isym < isymend;
4439 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4440 {
4441 int bind;
4442 bfd_vma value;
4443 asection *sec, *new_sec;
4444 flagword flags;
4445 const char *name;
4446 struct elf_link_hash_entry *h;
4447 struct elf_link_hash_entry *hi;
4448 bfd_boolean definition;
4449 bfd_boolean size_change_ok;
4450 bfd_boolean type_change_ok;
4451 bfd_boolean new_weak;
4452 bfd_boolean old_weak;
4453 bfd_boolean override;
4454 bfd_boolean common;
4455 bfd_boolean discarded;
4456 unsigned int old_alignment;
4457 unsigned int shindex;
4458 bfd *old_bfd;
4459 bfd_boolean matched;
4460
4461 override = FALSE;
4462
4463 flags = BSF_NO_FLAGS;
4464 sec = NULL;
4465 value = isym->st_value;
4466 common = bed->common_definition (isym);
4467 if (common && info->inhibit_common_definition)
4468 {
4469 /* Treat common symbol as undefined for --no-define-common. */
4470 isym->st_shndx = SHN_UNDEF;
4471 common = FALSE;
4472 }
4473 discarded = FALSE;
4474
4475 bind = ELF_ST_BIND (isym->st_info);
4476 switch (bind)
4477 {
4478 case STB_LOCAL:
4479 /* This should be impossible, since ELF requires that all
4480 global symbols follow all local symbols, and that sh_info
4481 point to the first global symbol. Unfortunately, Irix 5
4482 screws this up. */
4483 if (elf_bad_symtab (abfd))
4484 continue;
4485
4486 /* If we aren't prepared to handle locals within the globals
4487 then we'll likely segfault on a NULL symbol hash if the
4488 symbol is ever referenced in relocations. */
4489 shindex = elf_elfheader (abfd)->e_shstrndx;
4490 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4491 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4492 " (>= sh_info of %lu)"),
4493 abfd, name, (long) (isym - isymbuf + extsymoff),
4494 (long) extsymoff);
4495
4496 /* Dynamic object relocations are not processed by ld, so
4497 ld won't run into the problem mentioned above. */
4498 if (dynamic)
4499 continue;
4500 bfd_set_error (bfd_error_bad_value);
4501 goto error_free_vers;
4502
4503 case STB_GLOBAL:
4504 if (isym->st_shndx != SHN_UNDEF && !common)
4505 flags = BSF_GLOBAL;
4506 break;
4507
4508 case STB_WEAK:
4509 flags = BSF_WEAK;
4510 break;
4511
4512 case STB_GNU_UNIQUE:
4513 flags = BSF_GNU_UNIQUE;
4514 break;
4515
4516 default:
4517 /* Leave it up to the processor backend. */
4518 break;
4519 }
4520
4521 if (isym->st_shndx == SHN_UNDEF)
4522 sec = bfd_und_section_ptr;
4523 else if (isym->st_shndx == SHN_ABS)
4524 sec = bfd_abs_section_ptr;
4525 else if (isym->st_shndx == SHN_COMMON)
4526 {
4527 sec = bfd_com_section_ptr;
4528 /* What ELF calls the size we call the value. What ELF
4529 calls the value we call the alignment. */
4530 value = isym->st_size;
4531 }
4532 else
4533 {
4534 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4535 if (sec == NULL)
4536 sec = bfd_abs_section_ptr;
4537 else if (discarded_section (sec))
4538 {
4539 /* Symbols from discarded section are undefined. We keep
4540 its visibility. */
4541 sec = bfd_und_section_ptr;
4542 discarded = TRUE;
4543 isym->st_shndx = SHN_UNDEF;
4544 }
4545 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4546 value -= sec->vma;
4547 }
4548
4549 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4550 isym->st_name);
4551 if (name == NULL)
4552 goto error_free_vers;
4553
4554 if (isym->st_shndx == SHN_COMMON
4555 && (abfd->flags & BFD_PLUGIN) != 0)
4556 {
4557 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4558
4559 if (xc == NULL)
4560 {
4561 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4562 | SEC_EXCLUDE);
4563 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4564 if (xc == NULL)
4565 goto error_free_vers;
4566 }
4567 sec = xc;
4568 }
4569 else if (isym->st_shndx == SHN_COMMON
4570 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4571 && !bfd_link_relocatable (info))
4572 {
4573 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4574
4575 if (tcomm == NULL)
4576 {
4577 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4578 | SEC_LINKER_CREATED);
4579 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4580 if (tcomm == NULL)
4581 goto error_free_vers;
4582 }
4583 sec = tcomm;
4584 }
4585 else if (bed->elf_add_symbol_hook)
4586 {
4587 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4588 &sec, &value))
4589 goto error_free_vers;
4590
4591 /* The hook function sets the name to NULL if this symbol
4592 should be skipped for some reason. */
4593 if (name == NULL)
4594 continue;
4595 }
4596
4597 /* Sanity check that all possibilities were handled. */
4598 if (sec == NULL)
4599 abort ();
4600
4601 /* Silently discard TLS symbols from --just-syms. There's
4602 no way to combine a static TLS block with a new TLS block
4603 for this executable. */
4604 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4605 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4606 continue;
4607
4608 if (bfd_is_und_section (sec)
4609 || bfd_is_com_section (sec))
4610 definition = FALSE;
4611 else
4612 definition = TRUE;
4613
4614 size_change_ok = FALSE;
4615 type_change_ok = bed->type_change_ok;
4616 old_weak = FALSE;
4617 matched = FALSE;
4618 old_alignment = 0;
4619 old_bfd = NULL;
4620 new_sec = sec;
4621
4622 if (is_elf_hash_table (htab))
4623 {
4624 Elf_Internal_Versym iver;
4625 unsigned int vernum = 0;
4626 bfd_boolean skip;
4627
4628 if (ever == NULL)
4629 {
4630 if (info->default_imported_symver)
4631 /* Use the default symbol version created earlier. */
4632 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4633 else
4634 iver.vs_vers = 0;
4635 }
4636 else if (ever >= extversym_end)
4637 {
4638 /* xgettext:c-format */
4639 _bfd_error_handler (_("%pB: not enough version information"),
4640 abfd);
4641 bfd_set_error (bfd_error_bad_value);
4642 goto error_free_vers;
4643 }
4644 else
4645 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4646
4647 vernum = iver.vs_vers & VERSYM_VERSION;
4648
4649 /* If this is a hidden symbol, or if it is not version
4650 1, we append the version name to the symbol name.
4651 However, we do not modify a non-hidden absolute symbol
4652 if it is not a function, because it might be the version
4653 symbol itself. FIXME: What if it isn't? */
4654 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4655 || (vernum > 1
4656 && (!bfd_is_abs_section (sec)
4657 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4658 {
4659 const char *verstr;
4660 size_t namelen, verlen, newlen;
4661 char *newname, *p;
4662
4663 if (isym->st_shndx != SHN_UNDEF)
4664 {
4665 if (vernum > elf_tdata (abfd)->cverdefs)
4666 verstr = NULL;
4667 else if (vernum > 1)
4668 verstr =
4669 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4670 else
4671 verstr = "";
4672
4673 if (verstr == NULL)
4674 {
4675 _bfd_error_handler
4676 /* xgettext:c-format */
4677 (_("%pB: %s: invalid version %u (max %d)"),
4678 abfd, name, vernum,
4679 elf_tdata (abfd)->cverdefs);
4680 bfd_set_error (bfd_error_bad_value);
4681 goto error_free_vers;
4682 }
4683 }
4684 else
4685 {
4686 /* We cannot simply test for the number of
4687 entries in the VERNEED section since the
4688 numbers for the needed versions do not start
4689 at 0. */
4690 Elf_Internal_Verneed *t;
4691
4692 verstr = NULL;
4693 for (t = elf_tdata (abfd)->verref;
4694 t != NULL;
4695 t = t->vn_nextref)
4696 {
4697 Elf_Internal_Vernaux *a;
4698
4699 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4700 {
4701 if (a->vna_other == vernum)
4702 {
4703 verstr = a->vna_nodename;
4704 break;
4705 }
4706 }
4707 if (a != NULL)
4708 break;
4709 }
4710 if (verstr == NULL)
4711 {
4712 _bfd_error_handler
4713 /* xgettext:c-format */
4714 (_("%pB: %s: invalid needed version %d"),
4715 abfd, name, vernum);
4716 bfd_set_error (bfd_error_bad_value);
4717 goto error_free_vers;
4718 }
4719 }
4720
4721 namelen = strlen (name);
4722 verlen = strlen (verstr);
4723 newlen = namelen + verlen + 2;
4724 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4725 && isym->st_shndx != SHN_UNDEF)
4726 ++newlen;
4727
4728 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4729 if (newname == NULL)
4730 goto error_free_vers;
4731 memcpy (newname, name, namelen);
4732 p = newname + namelen;
4733 *p++ = ELF_VER_CHR;
4734 /* If this is a defined non-hidden version symbol,
4735 we add another @ to the name. This indicates the
4736 default version of the symbol. */
4737 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4738 && isym->st_shndx != SHN_UNDEF)
4739 *p++ = ELF_VER_CHR;
4740 memcpy (p, verstr, verlen + 1);
4741
4742 name = newname;
4743 }
4744
4745 /* If this symbol has default visibility and the user has
4746 requested we not re-export it, then mark it as hidden. */
4747 if (!bfd_is_und_section (sec)
4748 && !dynamic
4749 && abfd->no_export
4750 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4751 isym->st_other = (STV_HIDDEN
4752 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4753
4754 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4755 sym_hash, &old_bfd, &old_weak,
4756 &old_alignment, &skip, &override,
4757 &type_change_ok, &size_change_ok,
4758 &matched))
4759 goto error_free_vers;
4760
4761 if (skip)
4762 continue;
4763
4764 /* Override a definition only if the new symbol matches the
4765 existing one. */
4766 if (override && matched)
4767 definition = FALSE;
4768
4769 h = *sym_hash;
4770 while (h->root.type == bfd_link_hash_indirect
4771 || h->root.type == bfd_link_hash_warning)
4772 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4773
4774 if (elf_tdata (abfd)->verdef != NULL
4775 && vernum > 1
4776 && definition)
4777 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4778 }
4779
4780 if (! (_bfd_generic_link_add_one_symbol
4781 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4782 (struct bfd_link_hash_entry **) sym_hash)))
4783 goto error_free_vers;
4784
4785 h = *sym_hash;
4786 /* We need to make sure that indirect symbol dynamic flags are
4787 updated. */
4788 hi = h;
4789 while (h->root.type == bfd_link_hash_indirect
4790 || h->root.type == bfd_link_hash_warning)
4791 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4792
4793 /* Setting the index to -3 tells elf_link_output_extsym that
4794 this symbol is defined in a discarded section. */
4795 if (discarded)
4796 h->indx = -3;
4797
4798 *sym_hash = h;
4799
4800 new_weak = (flags & BSF_WEAK) != 0;
4801 if (dynamic
4802 && definition
4803 && new_weak
4804 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4805 && is_elf_hash_table (htab)
4806 && h->u.alias == NULL)
4807 {
4808 /* Keep a list of all weak defined non function symbols from
4809 a dynamic object, using the alias field. Later in this
4810 function we will set the alias field to the correct
4811 value. We only put non-function symbols from dynamic
4812 objects on this list, because that happens to be the only
4813 time we need to know the normal symbol corresponding to a
4814 weak symbol, and the information is time consuming to
4815 figure out. If the alias field is not already NULL,
4816 then this symbol was already defined by some previous
4817 dynamic object, and we will be using that previous
4818 definition anyhow. */
4819
4820 h->u.alias = weaks;
4821 weaks = h;
4822 }
4823
4824 /* Set the alignment of a common symbol. */
4825 if ((common || bfd_is_com_section (sec))
4826 && h->root.type == bfd_link_hash_common)
4827 {
4828 unsigned int align;
4829
4830 if (common)
4831 align = bfd_log2 (isym->st_value);
4832 else
4833 {
4834 /* The new symbol is a common symbol in a shared object.
4835 We need to get the alignment from the section. */
4836 align = new_sec->alignment_power;
4837 }
4838 if (align > old_alignment)
4839 h->root.u.c.p->alignment_power = align;
4840 else
4841 h->root.u.c.p->alignment_power = old_alignment;
4842 }
4843
4844 if (is_elf_hash_table (htab))
4845 {
4846 /* Set a flag in the hash table entry indicating the type of
4847 reference or definition we just found. A dynamic symbol
4848 is one which is referenced or defined by both a regular
4849 object and a shared object. */
4850 bfd_boolean dynsym = FALSE;
4851
4852 /* Plugin symbols aren't normal. Don't set def_regular or
4853 ref_regular for them, or make them dynamic. */
4854 if ((abfd->flags & BFD_PLUGIN) != 0)
4855 ;
4856 else if (! dynamic)
4857 {
4858 if (! definition)
4859 {
4860 h->ref_regular = 1;
4861 if (bind != STB_WEAK)
4862 h->ref_regular_nonweak = 1;
4863 }
4864 else
4865 {
4866 h->def_regular = 1;
4867 if (h->def_dynamic)
4868 {
4869 h->def_dynamic = 0;
4870 h->ref_dynamic = 1;
4871 }
4872 }
4873
4874 /* If the indirect symbol has been forced local, don't
4875 make the real symbol dynamic. */
4876 if ((h == hi || !hi->forced_local)
4877 && (bfd_link_dll (info)
4878 || h->def_dynamic
4879 || h->ref_dynamic))
4880 dynsym = TRUE;
4881 }
4882 else
4883 {
4884 if (! definition)
4885 {
4886 h->ref_dynamic = 1;
4887 hi->ref_dynamic = 1;
4888 }
4889 else
4890 {
4891 h->def_dynamic = 1;
4892 hi->def_dynamic = 1;
4893 }
4894
4895 /* If the indirect symbol has been forced local, don't
4896 make the real symbol dynamic. */
4897 if ((h == hi || !hi->forced_local)
4898 && (h->def_regular
4899 || h->ref_regular
4900 || (h->is_weakalias
4901 && weakdef (h)->dynindx != -1)))
4902 dynsym = TRUE;
4903 }
4904
4905 /* Check to see if we need to add an indirect symbol for
4906 the default name. */
4907 if (definition
4908 || (!override && h->root.type == bfd_link_hash_common))
4909 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4910 sec, value, &old_bfd, &dynsym))
4911 goto error_free_vers;
4912
4913 /* Check the alignment when a common symbol is involved. This
4914 can change when a common symbol is overridden by a normal
4915 definition or a common symbol is ignored due to the old
4916 normal definition. We need to make sure the maximum
4917 alignment is maintained. */
4918 if ((old_alignment || common)
4919 && h->root.type != bfd_link_hash_common)
4920 {
4921 unsigned int common_align;
4922 unsigned int normal_align;
4923 unsigned int symbol_align;
4924 bfd *normal_bfd;
4925 bfd *common_bfd;
4926
4927 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4928 || h->root.type == bfd_link_hash_defweak);
4929
4930 symbol_align = ffs (h->root.u.def.value) - 1;
4931 if (h->root.u.def.section->owner != NULL
4932 && (h->root.u.def.section->owner->flags
4933 & (DYNAMIC | BFD_PLUGIN)) == 0)
4934 {
4935 normal_align = h->root.u.def.section->alignment_power;
4936 if (normal_align > symbol_align)
4937 normal_align = symbol_align;
4938 }
4939 else
4940 normal_align = symbol_align;
4941
4942 if (old_alignment)
4943 {
4944 common_align = old_alignment;
4945 common_bfd = old_bfd;
4946 normal_bfd = abfd;
4947 }
4948 else
4949 {
4950 common_align = bfd_log2 (isym->st_value);
4951 common_bfd = abfd;
4952 normal_bfd = old_bfd;
4953 }
4954
4955 if (normal_align < common_align)
4956 {
4957 /* PR binutils/2735 */
4958 if (normal_bfd == NULL)
4959 _bfd_error_handler
4960 /* xgettext:c-format */
4961 (_("warning: alignment %u of common symbol `%s' in %pB is"
4962 " greater than the alignment (%u) of its section %pA"),
4963 1 << common_align, name, common_bfd,
4964 1 << normal_align, h->root.u.def.section);
4965 else
4966 _bfd_error_handler
4967 /* xgettext:c-format */
4968 (_("warning: alignment %u of symbol `%s' in %pB"
4969 " is smaller than %u in %pB"),
4970 1 << normal_align, name, normal_bfd,
4971 1 << common_align, common_bfd);
4972 }
4973 }
4974
4975 /* Remember the symbol size if it isn't undefined. */
4976 if (isym->st_size != 0
4977 && isym->st_shndx != SHN_UNDEF
4978 && (definition || h->size == 0))
4979 {
4980 if (h->size != 0
4981 && h->size != isym->st_size
4982 && ! size_change_ok)
4983 _bfd_error_handler
4984 /* xgettext:c-format */
4985 (_("warning: size of symbol `%s' changed"
4986 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
4987 name, (uint64_t) h->size, old_bfd,
4988 (uint64_t) isym->st_size, abfd);
4989
4990 h->size = isym->st_size;
4991 }
4992
4993 /* If this is a common symbol, then we always want H->SIZE
4994 to be the size of the common symbol. The code just above
4995 won't fix the size if a common symbol becomes larger. We
4996 don't warn about a size change here, because that is
4997 covered by --warn-common. Allow changes between different
4998 function types. */
4999 if (h->root.type == bfd_link_hash_common)
5000 h->size = h->root.u.c.size;
5001
5002 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
5003 && ((definition && !new_weak)
5004 || (old_weak && h->root.type == bfd_link_hash_common)
5005 || h->type == STT_NOTYPE))
5006 {
5007 unsigned int type = ELF_ST_TYPE (isym->st_info);
5008
5009 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5010 symbol. */
5011 if (type == STT_GNU_IFUNC
5012 && (abfd->flags & DYNAMIC) != 0)
5013 type = STT_FUNC;
5014
5015 if (h->type != type)
5016 {
5017 if (h->type != STT_NOTYPE && ! type_change_ok)
5018 /* xgettext:c-format */
5019 _bfd_error_handler
5020 (_("warning: type of symbol `%s' changed"
5021 " from %d to %d in %pB"),
5022 name, h->type, type, abfd);
5023
5024 h->type = type;
5025 }
5026 }
5027
5028 /* Merge st_other field. */
5029 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
5030
5031 /* We don't want to make debug symbol dynamic. */
5032 if (definition
5033 && (sec->flags & SEC_DEBUGGING)
5034 && !bfd_link_relocatable (info))
5035 dynsym = FALSE;
5036
5037 /* Nor should we make plugin symbols dynamic. */
5038 if ((abfd->flags & BFD_PLUGIN) != 0)
5039 dynsym = FALSE;
5040
5041 if (definition)
5042 {
5043 h->target_internal = isym->st_target_internal;
5044 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5045 }
5046
5047 if (definition && !dynamic)
5048 {
5049 char *p = strchr (name, ELF_VER_CHR);
5050 if (p != NULL && p[1] != ELF_VER_CHR)
5051 {
5052 /* Queue non-default versions so that .symver x, x@FOO
5053 aliases can be checked. */
5054 if (!nondeflt_vers)
5055 {
5056 size_t amt = ((isymend - isym + 1)
5057 * sizeof (struct elf_link_hash_entry *));
5058 nondeflt_vers
5059 = (struct elf_link_hash_entry **) bfd_malloc (amt);
5060 if (!nondeflt_vers)
5061 goto error_free_vers;
5062 }
5063 nondeflt_vers[nondeflt_vers_cnt++] = h;
5064 }
5065 }
5066
5067 if (dynsym && h->dynindx == -1)
5068 {
5069 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5070 goto error_free_vers;
5071 if (h->is_weakalias
5072 && weakdef (h)->dynindx == -1)
5073 {
5074 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5075 goto error_free_vers;
5076 }
5077 }
5078 else if (h->dynindx != -1)
5079 /* If the symbol already has a dynamic index, but
5080 visibility says it should not be visible, turn it into
5081 a local symbol. */
5082 switch (ELF_ST_VISIBILITY (h->other))
5083 {
5084 case STV_INTERNAL:
5085 case STV_HIDDEN:
5086 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5087 dynsym = FALSE;
5088 break;
5089 }
5090
5091 /* Don't add DT_NEEDED for references from the dummy bfd nor
5092 for unmatched symbol. */
5093 if (!add_needed
5094 && matched
5095 && definition
5096 && ((dynsym
5097 && h->ref_regular_nonweak
5098 && (old_bfd == NULL
5099 || (old_bfd->flags & BFD_PLUGIN) == 0))
5100 || (h->ref_dynamic_nonweak
5101 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5102 && !on_needed_list (elf_dt_name (abfd),
5103 htab->needed, NULL))))
5104 {
5105 int ret;
5106 const char *soname = elf_dt_name (abfd);
5107
5108 info->callbacks->minfo ("%!", soname, old_bfd,
5109 h->root.root.string);
5110
5111 /* A symbol from a library loaded via DT_NEEDED of some
5112 other library is referenced by a regular object.
5113 Add a DT_NEEDED entry for it. Issue an error if
5114 --no-add-needed is used and the reference was not
5115 a weak one. */
5116 if (old_bfd != NULL
5117 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5118 {
5119 _bfd_error_handler
5120 /* xgettext:c-format */
5121 (_("%pB: undefined reference to symbol '%s'"),
5122 old_bfd, name);
5123 bfd_set_error (bfd_error_missing_dso);
5124 goto error_free_vers;
5125 }
5126
5127 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5128 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5129
5130 add_needed = TRUE;
5131 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
5132 if (ret < 0)
5133 goto error_free_vers;
5134
5135 BFD_ASSERT (ret == 0);
5136 }
5137 }
5138 }
5139
5140 if (info->lto_plugin_active
5141 && !bfd_link_relocatable (info)
5142 && (abfd->flags & BFD_PLUGIN) == 0
5143 && !just_syms
5144 && extsymcount)
5145 {
5146 int r_sym_shift;
5147
5148 if (bed->s->arch_size == 32)
5149 r_sym_shift = 8;
5150 else
5151 r_sym_shift = 32;
5152
5153 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5154 referenced in regular objects so that linker plugin will get
5155 the correct symbol resolution. */
5156
5157 sym_hash = elf_sym_hashes (abfd);
5158 for (s = abfd->sections; s != NULL; s = s->next)
5159 {
5160 Elf_Internal_Rela *internal_relocs;
5161 Elf_Internal_Rela *rel, *relend;
5162
5163 /* Don't check relocations in excluded sections. */
5164 if ((s->flags & SEC_RELOC) == 0
5165 || s->reloc_count == 0
5166 || (s->flags & SEC_EXCLUDE) != 0
5167 || ((info->strip == strip_all
5168 || info->strip == strip_debugger)
5169 && (s->flags & SEC_DEBUGGING) != 0))
5170 continue;
5171
5172 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5173 NULL,
5174 info->keep_memory);
5175 if (internal_relocs == NULL)
5176 goto error_free_vers;
5177
5178 rel = internal_relocs;
5179 relend = rel + s->reloc_count;
5180 for ( ; rel < relend; rel++)
5181 {
5182 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5183 struct elf_link_hash_entry *h;
5184
5185 /* Skip local symbols. */
5186 if (r_symndx < extsymoff)
5187 continue;
5188
5189 h = sym_hash[r_symndx - extsymoff];
5190 if (h != NULL)
5191 h->root.non_ir_ref_regular = 1;
5192 }
5193
5194 if (elf_section_data (s)->relocs != internal_relocs)
5195 free (internal_relocs);
5196 }
5197 }
5198
5199 if (extversym != NULL)
5200 {
5201 free (extversym);
5202 extversym = NULL;
5203 }
5204
5205 if (isymbuf != NULL)
5206 {
5207 free (isymbuf);
5208 isymbuf = NULL;
5209 }
5210
5211 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5212 {
5213 unsigned int i;
5214
5215 /* Restore the symbol table. */
5216 old_ent = (char *) old_tab + tabsize;
5217 memset (elf_sym_hashes (abfd), 0,
5218 extsymcount * sizeof (struct elf_link_hash_entry *));
5219 htab->root.table.table = old_table;
5220 htab->root.table.size = old_size;
5221 htab->root.table.count = old_count;
5222 memcpy (htab->root.table.table, old_tab, tabsize);
5223 htab->root.undefs = old_undefs;
5224 htab->root.undefs_tail = old_undefs_tail;
5225 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5226 free (old_strtab);
5227 old_strtab = NULL;
5228 for (i = 0; i < htab->root.table.size; i++)
5229 {
5230 struct bfd_hash_entry *p;
5231 struct elf_link_hash_entry *h;
5232 bfd_size_type size;
5233 unsigned int alignment_power;
5234 unsigned int non_ir_ref_dynamic;
5235
5236 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5237 {
5238 h = (struct elf_link_hash_entry *) p;
5239 if (h->root.type == bfd_link_hash_warning)
5240 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5241
5242 /* Preserve the maximum alignment and size for common
5243 symbols even if this dynamic lib isn't on DT_NEEDED
5244 since it can still be loaded at run time by another
5245 dynamic lib. */
5246 if (h->root.type == bfd_link_hash_common)
5247 {
5248 size = h->root.u.c.size;
5249 alignment_power = h->root.u.c.p->alignment_power;
5250 }
5251 else
5252 {
5253 size = 0;
5254 alignment_power = 0;
5255 }
5256 /* Preserve non_ir_ref_dynamic so that this symbol
5257 will be exported when the dynamic lib becomes needed
5258 in the second pass. */
5259 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5260 memcpy (p, old_ent, htab->root.table.entsize);
5261 old_ent = (char *) old_ent + htab->root.table.entsize;
5262 h = (struct elf_link_hash_entry *) p;
5263 if (h->root.type == bfd_link_hash_warning)
5264 {
5265 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5266 old_ent = (char *) old_ent + htab->root.table.entsize;
5267 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5268 }
5269 if (h->root.type == bfd_link_hash_common)
5270 {
5271 if (size > h->root.u.c.size)
5272 h->root.u.c.size = size;
5273 if (alignment_power > h->root.u.c.p->alignment_power)
5274 h->root.u.c.p->alignment_power = alignment_power;
5275 }
5276 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5277 }
5278 }
5279
5280 /* Make a special call to the linker "notice" function to
5281 tell it that symbols added for crefs may need to be removed. */
5282 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5283 goto error_free_vers;
5284
5285 free (old_tab);
5286 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5287 alloc_mark);
5288 if (nondeflt_vers != NULL)
5289 free (nondeflt_vers);
5290 return TRUE;
5291 }
5292
5293 if (old_tab != NULL)
5294 {
5295 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5296 goto error_free_vers;
5297 free (old_tab);
5298 old_tab = NULL;
5299 }
5300
5301 /* Now that all the symbols from this input file are created, if
5302 not performing a relocatable link, handle .symver foo, foo@BAR
5303 such that any relocs against foo become foo@BAR. */
5304 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5305 {
5306 size_t cnt, symidx;
5307
5308 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5309 {
5310 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5311 char *shortname, *p;
5312 size_t amt;
5313
5314 p = strchr (h->root.root.string, ELF_VER_CHR);
5315 if (p == NULL
5316 || (h->root.type != bfd_link_hash_defined
5317 && h->root.type != bfd_link_hash_defweak))
5318 continue;
5319
5320 amt = p - h->root.root.string;
5321 shortname = (char *) bfd_malloc (amt + 1);
5322 if (!shortname)
5323 goto error_free_vers;
5324 memcpy (shortname, h->root.root.string, amt);
5325 shortname[amt] = '\0';
5326
5327 hi = (struct elf_link_hash_entry *)
5328 bfd_link_hash_lookup (&htab->root, shortname,
5329 FALSE, FALSE, FALSE);
5330 if (hi != NULL
5331 && hi->root.type == h->root.type
5332 && hi->root.u.def.value == h->root.u.def.value
5333 && hi->root.u.def.section == h->root.u.def.section)
5334 {
5335 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5336 hi->root.type = bfd_link_hash_indirect;
5337 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5338 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5339 sym_hash = elf_sym_hashes (abfd);
5340 if (sym_hash)
5341 for (symidx = 0; symidx < extsymcount; ++symidx)
5342 if (sym_hash[symidx] == hi)
5343 {
5344 sym_hash[symidx] = h;
5345 break;
5346 }
5347 }
5348 free (shortname);
5349 }
5350 free (nondeflt_vers);
5351 nondeflt_vers = NULL;
5352 }
5353
5354 /* Now set the alias field correctly for all the weak defined
5355 symbols we found. The only way to do this is to search all the
5356 symbols. Since we only need the information for non functions in
5357 dynamic objects, that's the only time we actually put anything on
5358 the list WEAKS. We need this information so that if a regular
5359 object refers to a symbol defined weakly in a dynamic object, the
5360 real symbol in the dynamic object is also put in the dynamic
5361 symbols; we also must arrange for both symbols to point to the
5362 same memory location. We could handle the general case of symbol
5363 aliasing, but a general symbol alias can only be generated in
5364 assembler code, handling it correctly would be very time
5365 consuming, and other ELF linkers don't handle general aliasing
5366 either. */
5367 if (weaks != NULL)
5368 {
5369 struct elf_link_hash_entry **hpp;
5370 struct elf_link_hash_entry **hppend;
5371 struct elf_link_hash_entry **sorted_sym_hash;
5372 struct elf_link_hash_entry *h;
5373 size_t sym_count, amt;
5374
5375 /* Since we have to search the whole symbol list for each weak
5376 defined symbol, search time for N weak defined symbols will be
5377 O(N^2). Binary search will cut it down to O(NlogN). */
5378 amt = extsymcount * sizeof (*sorted_sym_hash);
5379 sorted_sym_hash = bfd_malloc (amt);
5380 if (sorted_sym_hash == NULL)
5381 goto error_return;
5382 sym_hash = sorted_sym_hash;
5383 hpp = elf_sym_hashes (abfd);
5384 hppend = hpp + extsymcount;
5385 sym_count = 0;
5386 for (; hpp < hppend; hpp++)
5387 {
5388 h = *hpp;
5389 if (h != NULL
5390 && h->root.type == bfd_link_hash_defined
5391 && !bed->is_function_type (h->type))
5392 {
5393 *sym_hash = h;
5394 sym_hash++;
5395 sym_count++;
5396 }
5397 }
5398
5399 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
5400 elf_sort_symbol);
5401
5402 while (weaks != NULL)
5403 {
5404 struct elf_link_hash_entry *hlook;
5405 asection *slook;
5406 bfd_vma vlook;
5407 size_t i, j, idx = 0;
5408
5409 hlook = weaks;
5410 weaks = hlook->u.alias;
5411 hlook->u.alias = NULL;
5412
5413 if (hlook->root.type != bfd_link_hash_defined
5414 && hlook->root.type != bfd_link_hash_defweak)
5415 continue;
5416
5417 slook = hlook->root.u.def.section;
5418 vlook = hlook->root.u.def.value;
5419
5420 i = 0;
5421 j = sym_count;
5422 while (i != j)
5423 {
5424 bfd_signed_vma vdiff;
5425 idx = (i + j) / 2;
5426 h = sorted_sym_hash[idx];
5427 vdiff = vlook - h->root.u.def.value;
5428 if (vdiff < 0)
5429 j = idx;
5430 else if (vdiff > 0)
5431 i = idx + 1;
5432 else
5433 {
5434 int sdiff = slook->id - h->root.u.def.section->id;
5435 if (sdiff < 0)
5436 j = idx;
5437 else if (sdiff > 0)
5438 i = idx + 1;
5439 else
5440 break;
5441 }
5442 }
5443
5444 /* We didn't find a value/section match. */
5445 if (i == j)
5446 continue;
5447
5448 /* With multiple aliases, or when the weak symbol is already
5449 strongly defined, we have multiple matching symbols and
5450 the binary search above may land on any of them. Step
5451 one past the matching symbol(s). */
5452 while (++idx != j)
5453 {
5454 h = sorted_sym_hash[idx];
5455 if (h->root.u.def.section != slook
5456 || h->root.u.def.value != vlook)
5457 break;
5458 }
5459
5460 /* Now look back over the aliases. Since we sorted by size
5461 as well as value and section, we'll choose the one with
5462 the largest size. */
5463 while (idx-- != i)
5464 {
5465 h = sorted_sym_hash[idx];
5466
5467 /* Stop if value or section doesn't match. */
5468 if (h->root.u.def.section != slook
5469 || h->root.u.def.value != vlook)
5470 break;
5471 else if (h != hlook)
5472 {
5473 struct elf_link_hash_entry *t;
5474
5475 hlook->u.alias = h;
5476 hlook->is_weakalias = 1;
5477 t = h;
5478 if (t->u.alias != NULL)
5479 while (t->u.alias != h)
5480 t = t->u.alias;
5481 t->u.alias = hlook;
5482
5483 /* If the weak definition is in the list of dynamic
5484 symbols, make sure the real definition is put
5485 there as well. */
5486 if (hlook->dynindx != -1 && h->dynindx == -1)
5487 {
5488 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5489 {
5490 err_free_sym_hash:
5491 free (sorted_sym_hash);
5492 goto error_return;
5493 }
5494 }
5495
5496 /* If the real definition is in the list of dynamic
5497 symbols, make sure the weak definition is put
5498 there as well. If we don't do this, then the
5499 dynamic loader might not merge the entries for the
5500 real definition and the weak definition. */
5501 if (h->dynindx != -1 && hlook->dynindx == -1)
5502 {
5503 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5504 goto err_free_sym_hash;
5505 }
5506 break;
5507 }
5508 }
5509 }
5510
5511 free (sorted_sym_hash);
5512 }
5513
5514 if (bed->check_directives
5515 && !(*bed->check_directives) (abfd, info))
5516 return FALSE;
5517
5518 /* If this is a non-traditional link, try to optimize the handling
5519 of the .stab/.stabstr sections. */
5520 if (! dynamic
5521 && ! info->traditional_format
5522 && is_elf_hash_table (htab)
5523 && (info->strip != strip_all && info->strip != strip_debugger))
5524 {
5525 asection *stabstr;
5526
5527 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5528 if (stabstr != NULL)
5529 {
5530 bfd_size_type string_offset = 0;
5531 asection *stab;
5532
5533 for (stab = abfd->sections; stab; stab = stab->next)
5534 if (CONST_STRNEQ (stab->name, ".stab")
5535 && (!stab->name[5] ||
5536 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5537 && (stab->flags & SEC_MERGE) == 0
5538 && !bfd_is_abs_section (stab->output_section))
5539 {
5540 struct bfd_elf_section_data *secdata;
5541
5542 secdata = elf_section_data (stab);
5543 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5544 stabstr, &secdata->sec_info,
5545 &string_offset))
5546 goto error_return;
5547 if (secdata->sec_info)
5548 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5549 }
5550 }
5551 }
5552
5553 if (is_elf_hash_table (htab) && add_needed)
5554 {
5555 /* Add this bfd to the loaded list. */
5556 struct elf_link_loaded_list *n;
5557
5558 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5559 if (n == NULL)
5560 goto error_return;
5561 n->abfd = abfd;
5562 n->next = htab->loaded;
5563 htab->loaded = n;
5564 }
5565
5566 return TRUE;
5567
5568 error_free_vers:
5569 if (old_tab != NULL)
5570 free (old_tab);
5571 if (old_strtab != NULL)
5572 free (old_strtab);
5573 if (nondeflt_vers != NULL)
5574 free (nondeflt_vers);
5575 if (extversym != NULL)
5576 free (extversym);
5577 error_free_sym:
5578 if (isymbuf != NULL)
5579 free (isymbuf);
5580 error_return:
5581 return FALSE;
5582 }
5583
5584 /* Return the linker hash table entry of a symbol that might be
5585 satisfied by an archive symbol. Return -1 on error. */
5586
5587 struct elf_link_hash_entry *
5588 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5589 struct bfd_link_info *info,
5590 const char *name)
5591 {
5592 struct elf_link_hash_entry *h;
5593 char *p, *copy;
5594 size_t len, first;
5595
5596 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5597 if (h != NULL)
5598 return h;
5599
5600 /* If this is a default version (the name contains @@), look up the
5601 symbol again with only one `@' as well as without the version.
5602 The effect is that references to the symbol with and without the
5603 version will be matched by the default symbol in the archive. */
5604
5605 p = strchr (name, ELF_VER_CHR);
5606 if (p == NULL || p[1] != ELF_VER_CHR)
5607 return h;
5608
5609 /* First check with only one `@'. */
5610 len = strlen (name);
5611 copy = (char *) bfd_alloc (abfd, len);
5612 if (copy == NULL)
5613 return (struct elf_link_hash_entry *) -1;
5614
5615 first = p - name + 1;
5616 memcpy (copy, name, first);
5617 memcpy (copy + first, name + first + 1, len - first);
5618
5619 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5620 if (h == NULL)
5621 {
5622 /* We also need to check references to the symbol without the
5623 version. */
5624 copy[first - 1] = '\0';
5625 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5626 FALSE, FALSE, TRUE);
5627 }
5628
5629 bfd_release (abfd, copy);
5630 return h;
5631 }
5632
5633 /* Add symbols from an ELF archive file to the linker hash table. We
5634 don't use _bfd_generic_link_add_archive_symbols because we need to
5635 handle versioned symbols.
5636
5637 Fortunately, ELF archive handling is simpler than that done by
5638 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5639 oddities. In ELF, if we find a symbol in the archive map, and the
5640 symbol is currently undefined, we know that we must pull in that
5641 object file.
5642
5643 Unfortunately, we do have to make multiple passes over the symbol
5644 table until nothing further is resolved. */
5645
5646 static bfd_boolean
5647 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5648 {
5649 symindex c;
5650 unsigned char *included = NULL;
5651 carsym *symdefs;
5652 bfd_boolean loop;
5653 size_t amt;
5654 const struct elf_backend_data *bed;
5655 struct elf_link_hash_entry * (*archive_symbol_lookup)
5656 (bfd *, struct bfd_link_info *, const char *);
5657
5658 if (! bfd_has_map (abfd))
5659 {
5660 /* An empty archive is a special case. */
5661 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5662 return TRUE;
5663 bfd_set_error (bfd_error_no_armap);
5664 return FALSE;
5665 }
5666
5667 /* Keep track of all symbols we know to be already defined, and all
5668 files we know to be already included. This is to speed up the
5669 second and subsequent passes. */
5670 c = bfd_ardata (abfd)->symdef_count;
5671 if (c == 0)
5672 return TRUE;
5673 amt = c * sizeof (*included);
5674 included = (unsigned char *) bfd_zmalloc (amt);
5675 if (included == NULL)
5676 return FALSE;
5677
5678 symdefs = bfd_ardata (abfd)->symdefs;
5679 bed = get_elf_backend_data (abfd);
5680 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5681
5682 do
5683 {
5684 file_ptr last;
5685 symindex i;
5686 carsym *symdef;
5687 carsym *symdefend;
5688
5689 loop = FALSE;
5690 last = -1;
5691
5692 symdef = symdefs;
5693 symdefend = symdef + c;
5694 for (i = 0; symdef < symdefend; symdef++, i++)
5695 {
5696 struct elf_link_hash_entry *h;
5697 bfd *element;
5698 struct bfd_link_hash_entry *undefs_tail;
5699 symindex mark;
5700
5701 if (included[i])
5702 continue;
5703 if (symdef->file_offset == last)
5704 {
5705 included[i] = TRUE;
5706 continue;
5707 }
5708
5709 h = archive_symbol_lookup (abfd, info, symdef->name);
5710 if (h == (struct elf_link_hash_entry *) -1)
5711 goto error_return;
5712
5713 if (h == NULL)
5714 continue;
5715
5716 if (h->root.type == bfd_link_hash_common)
5717 {
5718 /* We currently have a common symbol. The archive map contains
5719 a reference to this symbol, so we may want to include it. We
5720 only want to include it however, if this archive element
5721 contains a definition of the symbol, not just another common
5722 declaration of it.
5723
5724 Unfortunately some archivers (including GNU ar) will put
5725 declarations of common symbols into their archive maps, as
5726 well as real definitions, so we cannot just go by the archive
5727 map alone. Instead we must read in the element's symbol
5728 table and check that to see what kind of symbol definition
5729 this is. */
5730 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5731 continue;
5732 }
5733 else if (h->root.type != bfd_link_hash_undefined)
5734 {
5735 if (h->root.type != bfd_link_hash_undefweak)
5736 /* Symbol must be defined. Don't check it again. */
5737 included[i] = TRUE;
5738 continue;
5739 }
5740
5741 /* We need to include this archive member. */
5742 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5743 if (element == NULL)
5744 goto error_return;
5745
5746 if (! bfd_check_format (element, bfd_object))
5747 goto error_return;
5748
5749 undefs_tail = info->hash->undefs_tail;
5750
5751 if (!(*info->callbacks
5752 ->add_archive_element) (info, element, symdef->name, &element))
5753 continue;
5754 if (!bfd_link_add_symbols (element, info))
5755 goto error_return;
5756
5757 /* If there are any new undefined symbols, we need to make
5758 another pass through the archive in order to see whether
5759 they can be defined. FIXME: This isn't perfect, because
5760 common symbols wind up on undefs_tail and because an
5761 undefined symbol which is defined later on in this pass
5762 does not require another pass. This isn't a bug, but it
5763 does make the code less efficient than it could be. */
5764 if (undefs_tail != info->hash->undefs_tail)
5765 loop = TRUE;
5766
5767 /* Look backward to mark all symbols from this object file
5768 which we have already seen in this pass. */
5769 mark = i;
5770 do
5771 {
5772 included[mark] = TRUE;
5773 if (mark == 0)
5774 break;
5775 --mark;
5776 }
5777 while (symdefs[mark].file_offset == symdef->file_offset);
5778
5779 /* We mark subsequent symbols from this object file as we go
5780 on through the loop. */
5781 last = symdef->file_offset;
5782 }
5783 }
5784 while (loop);
5785
5786 free (included);
5787
5788 return TRUE;
5789
5790 error_return:
5791 if (included != NULL)
5792 free (included);
5793 return FALSE;
5794 }
5795
5796 /* Given an ELF BFD, add symbols to the global hash table as
5797 appropriate. */
5798
5799 bfd_boolean
5800 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5801 {
5802 switch (bfd_get_format (abfd))
5803 {
5804 case bfd_object:
5805 return elf_link_add_object_symbols (abfd, info);
5806 case bfd_archive:
5807 return elf_link_add_archive_symbols (abfd, info);
5808 default:
5809 bfd_set_error (bfd_error_wrong_format);
5810 return FALSE;
5811 }
5812 }
5813 \f
5814 struct hash_codes_info
5815 {
5816 unsigned long *hashcodes;
5817 bfd_boolean error;
5818 };
5819
5820 /* This function will be called though elf_link_hash_traverse to store
5821 all hash value of the exported symbols in an array. */
5822
5823 static bfd_boolean
5824 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5825 {
5826 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5827 const char *name;
5828 unsigned long ha;
5829 char *alc = NULL;
5830
5831 /* Ignore indirect symbols. These are added by the versioning code. */
5832 if (h->dynindx == -1)
5833 return TRUE;
5834
5835 name = h->root.root.string;
5836 if (h->versioned >= versioned)
5837 {
5838 char *p = strchr (name, ELF_VER_CHR);
5839 if (p != NULL)
5840 {
5841 alc = (char *) bfd_malloc (p - name + 1);
5842 if (alc == NULL)
5843 {
5844 inf->error = TRUE;
5845 return FALSE;
5846 }
5847 memcpy (alc, name, p - name);
5848 alc[p - name] = '\0';
5849 name = alc;
5850 }
5851 }
5852
5853 /* Compute the hash value. */
5854 ha = bfd_elf_hash (name);
5855
5856 /* Store the found hash value in the array given as the argument. */
5857 *(inf->hashcodes)++ = ha;
5858
5859 /* And store it in the struct so that we can put it in the hash table
5860 later. */
5861 h->u.elf_hash_value = ha;
5862
5863 if (alc != NULL)
5864 free (alc);
5865
5866 return TRUE;
5867 }
5868
5869 struct collect_gnu_hash_codes
5870 {
5871 bfd *output_bfd;
5872 const struct elf_backend_data *bed;
5873 unsigned long int nsyms;
5874 unsigned long int maskbits;
5875 unsigned long int *hashcodes;
5876 unsigned long int *hashval;
5877 unsigned long int *indx;
5878 unsigned long int *counts;
5879 bfd_vma *bitmask;
5880 bfd_byte *contents;
5881 bfd_size_type xlat;
5882 long int min_dynindx;
5883 unsigned long int bucketcount;
5884 unsigned long int symindx;
5885 long int local_indx;
5886 long int shift1, shift2;
5887 unsigned long int mask;
5888 bfd_boolean error;
5889 };
5890
5891 /* This function will be called though elf_link_hash_traverse to store
5892 all hash value of the exported symbols in an array. */
5893
5894 static bfd_boolean
5895 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5896 {
5897 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5898 const char *name;
5899 unsigned long ha;
5900 char *alc = NULL;
5901
5902 /* Ignore indirect symbols. These are added by the versioning code. */
5903 if (h->dynindx == -1)
5904 return TRUE;
5905
5906 /* Ignore also local symbols and undefined symbols. */
5907 if (! (*s->bed->elf_hash_symbol) (h))
5908 return TRUE;
5909
5910 name = h->root.root.string;
5911 if (h->versioned >= versioned)
5912 {
5913 char *p = strchr (name, ELF_VER_CHR);
5914 if (p != NULL)
5915 {
5916 alc = (char *) bfd_malloc (p - name + 1);
5917 if (alc == NULL)
5918 {
5919 s->error = TRUE;
5920 return FALSE;
5921 }
5922 memcpy (alc, name, p - name);
5923 alc[p - name] = '\0';
5924 name = alc;
5925 }
5926 }
5927
5928 /* Compute the hash value. */
5929 ha = bfd_elf_gnu_hash (name);
5930
5931 /* Store the found hash value in the array for compute_bucket_count,
5932 and also for .dynsym reordering purposes. */
5933 s->hashcodes[s->nsyms] = ha;
5934 s->hashval[h->dynindx] = ha;
5935 ++s->nsyms;
5936 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5937 s->min_dynindx = h->dynindx;
5938
5939 if (alc != NULL)
5940 free (alc);
5941
5942 return TRUE;
5943 }
5944
5945 /* This function will be called though elf_link_hash_traverse to do
5946 final dynamic symbol renumbering in case of .gnu.hash.
5947 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
5948 to the translation table. */
5949
5950 static bfd_boolean
5951 elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
5952 {
5953 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5954 unsigned long int bucket;
5955 unsigned long int val;
5956
5957 /* Ignore indirect symbols. */
5958 if (h->dynindx == -1)
5959 return TRUE;
5960
5961 /* Ignore also local symbols and undefined symbols. */
5962 if (! (*s->bed->elf_hash_symbol) (h))
5963 {
5964 if (h->dynindx >= s->min_dynindx)
5965 {
5966 if (s->bed->record_xhash_symbol != NULL)
5967 {
5968 (*s->bed->record_xhash_symbol) (h, 0);
5969 s->local_indx++;
5970 }
5971 else
5972 h->dynindx = s->local_indx++;
5973 }
5974 return TRUE;
5975 }
5976
5977 bucket = s->hashval[h->dynindx] % s->bucketcount;
5978 val = (s->hashval[h->dynindx] >> s->shift1)
5979 & ((s->maskbits >> s->shift1) - 1);
5980 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5981 s->bitmask[val]
5982 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5983 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5984 if (s->counts[bucket] == 1)
5985 /* Last element terminates the chain. */
5986 val |= 1;
5987 bfd_put_32 (s->output_bfd, val,
5988 s->contents + (s->indx[bucket] - s->symindx) * 4);
5989 --s->counts[bucket];
5990 if (s->bed->record_xhash_symbol != NULL)
5991 {
5992 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
5993
5994 (*s->bed->record_xhash_symbol) (h, xlat_loc);
5995 }
5996 else
5997 h->dynindx = s->indx[bucket]++;
5998 return TRUE;
5999 }
6000
6001 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6002
6003 bfd_boolean
6004 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6005 {
6006 return !(h->forced_local
6007 || h->root.type == bfd_link_hash_undefined
6008 || h->root.type == bfd_link_hash_undefweak
6009 || ((h->root.type == bfd_link_hash_defined
6010 || h->root.type == bfd_link_hash_defweak)
6011 && h->root.u.def.section->output_section == NULL));
6012 }
6013
6014 /* Array used to determine the number of hash table buckets to use
6015 based on the number of symbols there are. If there are fewer than
6016 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6017 fewer than 37 we use 17 buckets, and so forth. We never use more
6018 than 32771 buckets. */
6019
6020 static const size_t elf_buckets[] =
6021 {
6022 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6023 16411, 32771, 0
6024 };
6025
6026 /* Compute bucket count for hashing table. We do not use a static set
6027 of possible tables sizes anymore. Instead we determine for all
6028 possible reasonable sizes of the table the outcome (i.e., the
6029 number of collisions etc) and choose the best solution. The
6030 weighting functions are not too simple to allow the table to grow
6031 without bounds. Instead one of the weighting factors is the size.
6032 Therefore the result is always a good payoff between few collisions
6033 (= short chain lengths) and table size. */
6034 static size_t
6035 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6036 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6037 unsigned long int nsyms,
6038 int gnu_hash)
6039 {
6040 size_t best_size = 0;
6041 unsigned long int i;
6042
6043 /* We have a problem here. The following code to optimize the table
6044 size requires an integer type with more the 32 bits. If
6045 BFD_HOST_U_64_BIT is set we know about such a type. */
6046 #ifdef BFD_HOST_U_64_BIT
6047 if (info->optimize)
6048 {
6049 size_t minsize;
6050 size_t maxsize;
6051 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
6052 bfd *dynobj = elf_hash_table (info)->dynobj;
6053 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6054 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
6055 unsigned long int *counts;
6056 bfd_size_type amt;
6057 unsigned int no_improvement_count = 0;
6058
6059 /* Possible optimization parameters: if we have NSYMS symbols we say
6060 that the hashing table must at least have NSYMS/4 and at most
6061 2*NSYMS buckets. */
6062 minsize = nsyms / 4;
6063 if (minsize == 0)
6064 minsize = 1;
6065 best_size = maxsize = nsyms * 2;
6066 if (gnu_hash)
6067 {
6068 if (minsize < 2)
6069 minsize = 2;
6070 if ((best_size & 31) == 0)
6071 ++best_size;
6072 }
6073
6074 /* Create array where we count the collisions in. We must use bfd_malloc
6075 since the size could be large. */
6076 amt = maxsize;
6077 amt *= sizeof (unsigned long int);
6078 counts = (unsigned long int *) bfd_malloc (amt);
6079 if (counts == NULL)
6080 return 0;
6081
6082 /* Compute the "optimal" size for the hash table. The criteria is a
6083 minimal chain length. The minor criteria is (of course) the size
6084 of the table. */
6085 for (i = minsize; i < maxsize; ++i)
6086 {
6087 /* Walk through the array of hashcodes and count the collisions. */
6088 BFD_HOST_U_64_BIT max;
6089 unsigned long int j;
6090 unsigned long int fact;
6091
6092 if (gnu_hash && (i & 31) == 0)
6093 continue;
6094
6095 memset (counts, '\0', i * sizeof (unsigned long int));
6096
6097 /* Determine how often each hash bucket is used. */
6098 for (j = 0; j < nsyms; ++j)
6099 ++counts[hashcodes[j] % i];
6100
6101 /* For the weight function we need some information about the
6102 pagesize on the target. This is information need not be 100%
6103 accurate. Since this information is not available (so far) we
6104 define it here to a reasonable default value. If it is crucial
6105 to have a better value some day simply define this value. */
6106 # ifndef BFD_TARGET_PAGESIZE
6107 # define BFD_TARGET_PAGESIZE (4096)
6108 # endif
6109
6110 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6111 and the chains. */
6112 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6113
6114 # if 1
6115 /* Variant 1: optimize for short chains. We add the squares
6116 of all the chain lengths (which favors many small chain
6117 over a few long chains). */
6118 for (j = 0; j < i; ++j)
6119 max += counts[j] * counts[j];
6120
6121 /* This adds penalties for the overall size of the table. */
6122 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6123 max *= fact * fact;
6124 # else
6125 /* Variant 2: Optimize a lot more for small table. Here we
6126 also add squares of the size but we also add penalties for
6127 empty slots (the +1 term). */
6128 for (j = 0; j < i; ++j)
6129 max += (1 + counts[j]) * (1 + counts[j]);
6130
6131 /* The overall size of the table is considered, but not as
6132 strong as in variant 1, where it is squared. */
6133 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6134 max *= fact;
6135 # endif
6136
6137 /* Compare with current best results. */
6138 if (max < best_chlen)
6139 {
6140 best_chlen = max;
6141 best_size = i;
6142 no_improvement_count = 0;
6143 }
6144 /* PR 11843: Avoid futile long searches for the best bucket size
6145 when there are a large number of symbols. */
6146 else if (++no_improvement_count == 100)
6147 break;
6148 }
6149
6150 free (counts);
6151 }
6152 else
6153 #endif /* defined (BFD_HOST_U_64_BIT) */
6154 {
6155 /* This is the fallback solution if no 64bit type is available or if we
6156 are not supposed to spend much time on optimizations. We select the
6157 bucket count using a fixed set of numbers. */
6158 for (i = 0; elf_buckets[i] != 0; i++)
6159 {
6160 best_size = elf_buckets[i];
6161 if (nsyms < elf_buckets[i + 1])
6162 break;
6163 }
6164 if (gnu_hash && best_size < 2)
6165 best_size = 2;
6166 }
6167
6168 return best_size;
6169 }
6170
6171 /* Size any SHT_GROUP section for ld -r. */
6172
6173 bfd_boolean
6174 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6175 {
6176 bfd *ibfd;
6177 asection *s;
6178
6179 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6180 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6181 && (s = ibfd->sections) != NULL
6182 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6183 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6184 return FALSE;
6185 return TRUE;
6186 }
6187
6188 /* Set a default stack segment size. The value in INFO wins. If it
6189 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6190 undefined it is initialized. */
6191
6192 bfd_boolean
6193 bfd_elf_stack_segment_size (bfd *output_bfd,
6194 struct bfd_link_info *info,
6195 const char *legacy_symbol,
6196 bfd_vma default_size)
6197 {
6198 struct elf_link_hash_entry *h = NULL;
6199
6200 /* Look for legacy symbol. */
6201 if (legacy_symbol)
6202 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6203 FALSE, FALSE, FALSE);
6204 if (h && (h->root.type == bfd_link_hash_defined
6205 || h->root.type == bfd_link_hash_defweak)
6206 && h->def_regular
6207 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6208 {
6209 /* The symbol has no type if specified on the command line. */
6210 h->type = STT_OBJECT;
6211 if (info->stacksize)
6212 /* xgettext:c-format */
6213 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6214 output_bfd, legacy_symbol);
6215 else if (h->root.u.def.section != bfd_abs_section_ptr)
6216 /* xgettext:c-format */
6217 _bfd_error_handler (_("%pB: %s not absolute"),
6218 output_bfd, legacy_symbol);
6219 else
6220 info->stacksize = h->root.u.def.value;
6221 }
6222
6223 if (!info->stacksize)
6224 /* If the user didn't set a size, or explicitly inhibit the
6225 size, set it now. */
6226 info->stacksize = default_size;
6227
6228 /* Provide the legacy symbol, if it is referenced. */
6229 if (h && (h->root.type == bfd_link_hash_undefined
6230 || h->root.type == bfd_link_hash_undefweak))
6231 {
6232 struct bfd_link_hash_entry *bh = NULL;
6233
6234 if (!(_bfd_generic_link_add_one_symbol
6235 (info, output_bfd, legacy_symbol,
6236 BSF_GLOBAL, bfd_abs_section_ptr,
6237 info->stacksize >= 0 ? info->stacksize : 0,
6238 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6239 return FALSE;
6240
6241 h = (struct elf_link_hash_entry *) bh;
6242 h->def_regular = 1;
6243 h->type = STT_OBJECT;
6244 }
6245
6246 return TRUE;
6247 }
6248
6249 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6250
6251 struct elf_gc_sweep_symbol_info
6252 {
6253 struct bfd_link_info *info;
6254 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6255 bfd_boolean);
6256 };
6257
6258 static bfd_boolean
6259 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6260 {
6261 if (!h->mark
6262 && (((h->root.type == bfd_link_hash_defined
6263 || h->root.type == bfd_link_hash_defweak)
6264 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6265 && h->root.u.def.section->gc_mark))
6266 || h->root.type == bfd_link_hash_undefined
6267 || h->root.type == bfd_link_hash_undefweak))
6268 {
6269 struct elf_gc_sweep_symbol_info *inf;
6270
6271 inf = (struct elf_gc_sweep_symbol_info *) data;
6272 (*inf->hide_symbol) (inf->info, h, TRUE);
6273 h->def_regular = 0;
6274 h->ref_regular = 0;
6275 h->ref_regular_nonweak = 0;
6276 }
6277
6278 return TRUE;
6279 }
6280
6281 /* Set up the sizes and contents of the ELF dynamic sections. This is
6282 called by the ELF linker emulation before_allocation routine. We
6283 must set the sizes of the sections before the linker sets the
6284 addresses of the various sections. */
6285
6286 bfd_boolean
6287 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6288 const char *soname,
6289 const char *rpath,
6290 const char *filter_shlib,
6291 const char *audit,
6292 const char *depaudit,
6293 const char * const *auxiliary_filters,
6294 struct bfd_link_info *info,
6295 asection **sinterpptr)
6296 {
6297 bfd *dynobj;
6298 const struct elf_backend_data *bed;
6299
6300 *sinterpptr = NULL;
6301
6302 if (!is_elf_hash_table (info->hash))
6303 return TRUE;
6304
6305 dynobj = elf_hash_table (info)->dynobj;
6306
6307 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6308 {
6309 struct bfd_elf_version_tree *verdefs;
6310 struct elf_info_failed asvinfo;
6311 struct bfd_elf_version_tree *t;
6312 struct bfd_elf_version_expr *d;
6313 asection *s;
6314 size_t soname_indx;
6315
6316 /* If we are supposed to export all symbols into the dynamic symbol
6317 table (this is not the normal case), then do so. */
6318 if (info->export_dynamic
6319 || (bfd_link_executable (info) && info->dynamic))
6320 {
6321 struct elf_info_failed eif;
6322
6323 eif.info = info;
6324 eif.failed = FALSE;
6325 elf_link_hash_traverse (elf_hash_table (info),
6326 _bfd_elf_export_symbol,
6327 &eif);
6328 if (eif.failed)
6329 return FALSE;
6330 }
6331
6332 if (soname != NULL)
6333 {
6334 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6335 soname, TRUE);
6336 if (soname_indx == (size_t) -1
6337 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6338 return FALSE;
6339 }
6340 else
6341 soname_indx = (size_t) -1;
6342
6343 /* Make all global versions with definition. */
6344 for (t = info->version_info; t != NULL; t = t->next)
6345 for (d = t->globals.list; d != NULL; d = d->next)
6346 if (!d->symver && d->literal)
6347 {
6348 const char *verstr, *name;
6349 size_t namelen, verlen, newlen;
6350 char *newname, *p, leading_char;
6351 struct elf_link_hash_entry *newh;
6352
6353 leading_char = bfd_get_symbol_leading_char (output_bfd);
6354 name = d->pattern;
6355 namelen = strlen (name) + (leading_char != '\0');
6356 verstr = t->name;
6357 verlen = strlen (verstr);
6358 newlen = namelen + verlen + 3;
6359
6360 newname = (char *) bfd_malloc (newlen);
6361 if (newname == NULL)
6362 return FALSE;
6363 newname[0] = leading_char;
6364 memcpy (newname + (leading_char != '\0'), name, namelen);
6365
6366 /* Check the hidden versioned definition. */
6367 p = newname + namelen;
6368 *p++ = ELF_VER_CHR;
6369 memcpy (p, verstr, verlen + 1);
6370 newh = elf_link_hash_lookup (elf_hash_table (info),
6371 newname, FALSE, FALSE,
6372 FALSE);
6373 if (newh == NULL
6374 || (newh->root.type != bfd_link_hash_defined
6375 && newh->root.type != bfd_link_hash_defweak))
6376 {
6377 /* Check the default versioned definition. */
6378 *p++ = ELF_VER_CHR;
6379 memcpy (p, verstr, verlen + 1);
6380 newh = elf_link_hash_lookup (elf_hash_table (info),
6381 newname, FALSE, FALSE,
6382 FALSE);
6383 }
6384 free (newname);
6385
6386 /* Mark this version if there is a definition and it is
6387 not defined in a shared object. */
6388 if (newh != NULL
6389 && !newh->def_dynamic
6390 && (newh->root.type == bfd_link_hash_defined
6391 || newh->root.type == bfd_link_hash_defweak))
6392 d->symver = 1;
6393 }
6394
6395 /* Attach all the symbols to their version information. */
6396 asvinfo.info = info;
6397 asvinfo.failed = FALSE;
6398
6399 elf_link_hash_traverse (elf_hash_table (info),
6400 _bfd_elf_link_assign_sym_version,
6401 &asvinfo);
6402 if (asvinfo.failed)
6403 return FALSE;
6404
6405 if (!info->allow_undefined_version)
6406 {
6407 /* Check if all global versions have a definition. */
6408 bfd_boolean all_defined = TRUE;
6409 for (t = info->version_info; t != NULL; t = t->next)
6410 for (d = t->globals.list; d != NULL; d = d->next)
6411 if (d->literal && !d->symver && !d->script)
6412 {
6413 _bfd_error_handler
6414 (_("%s: undefined version: %s"),
6415 d->pattern, t->name);
6416 all_defined = FALSE;
6417 }
6418
6419 if (!all_defined)
6420 {
6421 bfd_set_error (bfd_error_bad_value);
6422 return FALSE;
6423 }
6424 }
6425
6426 /* Set up the version definition section. */
6427 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6428 BFD_ASSERT (s != NULL);
6429
6430 /* We may have created additional version definitions if we are
6431 just linking a regular application. */
6432 verdefs = info->version_info;
6433
6434 /* Skip anonymous version tag. */
6435 if (verdefs != NULL && verdefs->vernum == 0)
6436 verdefs = verdefs->next;
6437
6438 if (verdefs == NULL && !info->create_default_symver)
6439 s->flags |= SEC_EXCLUDE;
6440 else
6441 {
6442 unsigned int cdefs;
6443 bfd_size_type size;
6444 bfd_byte *p;
6445 Elf_Internal_Verdef def;
6446 Elf_Internal_Verdaux defaux;
6447 struct bfd_link_hash_entry *bh;
6448 struct elf_link_hash_entry *h;
6449 const char *name;
6450
6451 cdefs = 0;
6452 size = 0;
6453
6454 /* Make space for the base version. */
6455 size += sizeof (Elf_External_Verdef);
6456 size += sizeof (Elf_External_Verdaux);
6457 ++cdefs;
6458
6459 /* Make space for the default version. */
6460 if (info->create_default_symver)
6461 {
6462 size += sizeof (Elf_External_Verdef);
6463 ++cdefs;
6464 }
6465
6466 for (t = verdefs; t != NULL; t = t->next)
6467 {
6468 struct bfd_elf_version_deps *n;
6469
6470 /* Don't emit base version twice. */
6471 if (t->vernum == 0)
6472 continue;
6473
6474 size += sizeof (Elf_External_Verdef);
6475 size += sizeof (Elf_External_Verdaux);
6476 ++cdefs;
6477
6478 for (n = t->deps; n != NULL; n = n->next)
6479 size += sizeof (Elf_External_Verdaux);
6480 }
6481
6482 s->size = size;
6483 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6484 if (s->contents == NULL && s->size != 0)
6485 return FALSE;
6486
6487 /* Fill in the version definition section. */
6488
6489 p = s->contents;
6490
6491 def.vd_version = VER_DEF_CURRENT;
6492 def.vd_flags = VER_FLG_BASE;
6493 def.vd_ndx = 1;
6494 def.vd_cnt = 1;
6495 if (info->create_default_symver)
6496 {
6497 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6498 def.vd_next = sizeof (Elf_External_Verdef);
6499 }
6500 else
6501 {
6502 def.vd_aux = sizeof (Elf_External_Verdef);
6503 def.vd_next = (sizeof (Elf_External_Verdef)
6504 + sizeof (Elf_External_Verdaux));
6505 }
6506
6507 if (soname_indx != (size_t) -1)
6508 {
6509 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6510 soname_indx);
6511 def.vd_hash = bfd_elf_hash (soname);
6512 defaux.vda_name = soname_indx;
6513 name = soname;
6514 }
6515 else
6516 {
6517 size_t indx;
6518
6519 name = lbasename (output_bfd->filename);
6520 def.vd_hash = bfd_elf_hash (name);
6521 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6522 name, FALSE);
6523 if (indx == (size_t) -1)
6524 return FALSE;
6525 defaux.vda_name = indx;
6526 }
6527 defaux.vda_next = 0;
6528
6529 _bfd_elf_swap_verdef_out (output_bfd, &def,
6530 (Elf_External_Verdef *) p);
6531 p += sizeof (Elf_External_Verdef);
6532 if (info->create_default_symver)
6533 {
6534 /* Add a symbol representing this version. */
6535 bh = NULL;
6536 if (! (_bfd_generic_link_add_one_symbol
6537 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6538 0, NULL, FALSE,
6539 get_elf_backend_data (dynobj)->collect, &bh)))
6540 return FALSE;
6541 h = (struct elf_link_hash_entry *) bh;
6542 h->non_elf = 0;
6543 h->def_regular = 1;
6544 h->type = STT_OBJECT;
6545 h->verinfo.vertree = NULL;
6546
6547 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6548 return FALSE;
6549
6550 /* Create a duplicate of the base version with the same
6551 aux block, but different flags. */
6552 def.vd_flags = 0;
6553 def.vd_ndx = 2;
6554 def.vd_aux = sizeof (Elf_External_Verdef);
6555 if (verdefs)
6556 def.vd_next = (sizeof (Elf_External_Verdef)
6557 + sizeof (Elf_External_Verdaux));
6558 else
6559 def.vd_next = 0;
6560 _bfd_elf_swap_verdef_out (output_bfd, &def,
6561 (Elf_External_Verdef *) p);
6562 p += sizeof (Elf_External_Verdef);
6563 }
6564 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6565 (Elf_External_Verdaux *) p);
6566 p += sizeof (Elf_External_Verdaux);
6567
6568 for (t = verdefs; t != NULL; t = t->next)
6569 {
6570 unsigned int cdeps;
6571 struct bfd_elf_version_deps *n;
6572
6573 /* Don't emit the base version twice. */
6574 if (t->vernum == 0)
6575 continue;
6576
6577 cdeps = 0;
6578 for (n = t->deps; n != NULL; n = n->next)
6579 ++cdeps;
6580
6581 /* Add a symbol representing this version. */
6582 bh = NULL;
6583 if (! (_bfd_generic_link_add_one_symbol
6584 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6585 0, NULL, FALSE,
6586 get_elf_backend_data (dynobj)->collect, &bh)))
6587 return FALSE;
6588 h = (struct elf_link_hash_entry *) bh;
6589 h->non_elf = 0;
6590 h->def_regular = 1;
6591 h->type = STT_OBJECT;
6592 h->verinfo.vertree = t;
6593
6594 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6595 return FALSE;
6596
6597 def.vd_version = VER_DEF_CURRENT;
6598 def.vd_flags = 0;
6599 if (t->globals.list == NULL
6600 && t->locals.list == NULL
6601 && ! t->used)
6602 def.vd_flags |= VER_FLG_WEAK;
6603 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6604 def.vd_cnt = cdeps + 1;
6605 def.vd_hash = bfd_elf_hash (t->name);
6606 def.vd_aux = sizeof (Elf_External_Verdef);
6607 def.vd_next = 0;
6608
6609 /* If a basever node is next, it *must* be the last node in
6610 the chain, otherwise Verdef construction breaks. */
6611 if (t->next != NULL && t->next->vernum == 0)
6612 BFD_ASSERT (t->next->next == NULL);
6613
6614 if (t->next != NULL && t->next->vernum != 0)
6615 def.vd_next = (sizeof (Elf_External_Verdef)
6616 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6617
6618 _bfd_elf_swap_verdef_out (output_bfd, &def,
6619 (Elf_External_Verdef *) p);
6620 p += sizeof (Elf_External_Verdef);
6621
6622 defaux.vda_name = h->dynstr_index;
6623 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6624 h->dynstr_index);
6625 defaux.vda_next = 0;
6626 if (t->deps != NULL)
6627 defaux.vda_next = sizeof (Elf_External_Verdaux);
6628 t->name_indx = defaux.vda_name;
6629
6630 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6631 (Elf_External_Verdaux *) p);
6632 p += sizeof (Elf_External_Verdaux);
6633
6634 for (n = t->deps; n != NULL; n = n->next)
6635 {
6636 if (n->version_needed == NULL)
6637 {
6638 /* This can happen if there was an error in the
6639 version script. */
6640 defaux.vda_name = 0;
6641 }
6642 else
6643 {
6644 defaux.vda_name = n->version_needed->name_indx;
6645 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6646 defaux.vda_name);
6647 }
6648 if (n->next == NULL)
6649 defaux.vda_next = 0;
6650 else
6651 defaux.vda_next = sizeof (Elf_External_Verdaux);
6652
6653 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6654 (Elf_External_Verdaux *) p);
6655 p += sizeof (Elf_External_Verdaux);
6656 }
6657 }
6658
6659 elf_tdata (output_bfd)->cverdefs = cdefs;
6660 }
6661 }
6662
6663 bed = get_elf_backend_data (output_bfd);
6664
6665 if (info->gc_sections && bed->can_gc_sections)
6666 {
6667 struct elf_gc_sweep_symbol_info sweep_info;
6668
6669 /* Remove the symbols that were in the swept sections from the
6670 dynamic symbol table. */
6671 sweep_info.info = info;
6672 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6673 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6674 &sweep_info);
6675 }
6676
6677 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6678 {
6679 asection *s;
6680 struct elf_find_verdep_info sinfo;
6681
6682 /* Work out the size of the version reference section. */
6683
6684 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6685 BFD_ASSERT (s != NULL);
6686
6687 sinfo.info = info;
6688 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6689 if (sinfo.vers == 0)
6690 sinfo.vers = 1;
6691 sinfo.failed = FALSE;
6692
6693 elf_link_hash_traverse (elf_hash_table (info),
6694 _bfd_elf_link_find_version_dependencies,
6695 &sinfo);
6696 if (sinfo.failed)
6697 return FALSE;
6698
6699 if (elf_tdata (output_bfd)->verref == NULL)
6700 s->flags |= SEC_EXCLUDE;
6701 else
6702 {
6703 Elf_Internal_Verneed *vn;
6704 unsigned int size;
6705 unsigned int crefs;
6706 bfd_byte *p;
6707
6708 /* Build the version dependency section. */
6709 size = 0;
6710 crefs = 0;
6711 for (vn = elf_tdata (output_bfd)->verref;
6712 vn != NULL;
6713 vn = vn->vn_nextref)
6714 {
6715 Elf_Internal_Vernaux *a;
6716
6717 size += sizeof (Elf_External_Verneed);
6718 ++crefs;
6719 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6720 size += sizeof (Elf_External_Vernaux);
6721 }
6722
6723 s->size = size;
6724 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6725 if (s->contents == NULL)
6726 return FALSE;
6727
6728 p = s->contents;
6729 for (vn = elf_tdata (output_bfd)->verref;
6730 vn != NULL;
6731 vn = vn->vn_nextref)
6732 {
6733 unsigned int caux;
6734 Elf_Internal_Vernaux *a;
6735 size_t indx;
6736
6737 caux = 0;
6738 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6739 ++caux;
6740
6741 vn->vn_version = VER_NEED_CURRENT;
6742 vn->vn_cnt = caux;
6743 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6744 elf_dt_name (vn->vn_bfd) != NULL
6745 ? elf_dt_name (vn->vn_bfd)
6746 : lbasename (vn->vn_bfd->filename),
6747 FALSE);
6748 if (indx == (size_t) -1)
6749 return FALSE;
6750 vn->vn_file = indx;
6751 vn->vn_aux = sizeof (Elf_External_Verneed);
6752 if (vn->vn_nextref == NULL)
6753 vn->vn_next = 0;
6754 else
6755 vn->vn_next = (sizeof (Elf_External_Verneed)
6756 + caux * sizeof (Elf_External_Vernaux));
6757
6758 _bfd_elf_swap_verneed_out (output_bfd, vn,
6759 (Elf_External_Verneed *) p);
6760 p += sizeof (Elf_External_Verneed);
6761
6762 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6763 {
6764 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6765 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6766 a->vna_nodename, FALSE);
6767 if (indx == (size_t) -1)
6768 return FALSE;
6769 a->vna_name = indx;
6770 if (a->vna_nextptr == NULL)
6771 a->vna_next = 0;
6772 else
6773 a->vna_next = sizeof (Elf_External_Vernaux);
6774
6775 _bfd_elf_swap_vernaux_out (output_bfd, a,
6776 (Elf_External_Vernaux *) p);
6777 p += sizeof (Elf_External_Vernaux);
6778 }
6779 }
6780
6781 elf_tdata (output_bfd)->cverrefs = crefs;
6782 }
6783 }
6784
6785 /* Any syms created from now on start with -1 in
6786 got.refcount/offset and plt.refcount/offset. */
6787 elf_hash_table (info)->init_got_refcount
6788 = elf_hash_table (info)->init_got_offset;
6789 elf_hash_table (info)->init_plt_refcount
6790 = elf_hash_table (info)->init_plt_offset;
6791
6792 if (bfd_link_relocatable (info)
6793 && !_bfd_elf_size_group_sections (info))
6794 return FALSE;
6795
6796 /* The backend may have to create some sections regardless of whether
6797 we're dynamic or not. */
6798 if (bed->elf_backend_always_size_sections
6799 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6800 return FALSE;
6801
6802 /* Determine any GNU_STACK segment requirements, after the backend
6803 has had a chance to set a default segment size. */
6804 if (info->execstack)
6805 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6806 else if (info->noexecstack)
6807 elf_stack_flags (output_bfd) = PF_R | PF_W;
6808 else
6809 {
6810 bfd *inputobj;
6811 asection *notesec = NULL;
6812 int exec = 0;
6813
6814 for (inputobj = info->input_bfds;
6815 inputobj;
6816 inputobj = inputobj->link.next)
6817 {
6818 asection *s;
6819
6820 if (inputobj->flags
6821 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6822 continue;
6823 s = inputobj->sections;
6824 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6825 continue;
6826
6827 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6828 if (s)
6829 {
6830 if (s->flags & SEC_CODE)
6831 exec = PF_X;
6832 notesec = s;
6833 }
6834 else if (bed->default_execstack)
6835 exec = PF_X;
6836 }
6837 if (notesec || info->stacksize > 0)
6838 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6839 if (notesec && exec && bfd_link_relocatable (info)
6840 && notesec->output_section != bfd_abs_section_ptr)
6841 notesec->output_section->flags |= SEC_CODE;
6842 }
6843
6844 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6845 {
6846 struct elf_info_failed eif;
6847 struct elf_link_hash_entry *h;
6848 asection *dynstr;
6849 asection *s;
6850
6851 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6852 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6853
6854 if (info->symbolic)
6855 {
6856 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6857 return FALSE;
6858 info->flags |= DF_SYMBOLIC;
6859 }
6860
6861 if (rpath != NULL)
6862 {
6863 size_t indx;
6864 bfd_vma tag;
6865
6866 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6867 TRUE);
6868 if (indx == (size_t) -1)
6869 return FALSE;
6870
6871 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6872 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6873 return FALSE;
6874 }
6875
6876 if (filter_shlib != NULL)
6877 {
6878 size_t indx;
6879
6880 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6881 filter_shlib, TRUE);
6882 if (indx == (size_t) -1
6883 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6884 return FALSE;
6885 }
6886
6887 if (auxiliary_filters != NULL)
6888 {
6889 const char * const *p;
6890
6891 for (p = auxiliary_filters; *p != NULL; p++)
6892 {
6893 size_t indx;
6894
6895 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6896 *p, TRUE);
6897 if (indx == (size_t) -1
6898 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6899 return FALSE;
6900 }
6901 }
6902
6903 if (audit != NULL)
6904 {
6905 size_t indx;
6906
6907 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6908 TRUE);
6909 if (indx == (size_t) -1
6910 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6911 return FALSE;
6912 }
6913
6914 if (depaudit != NULL)
6915 {
6916 size_t indx;
6917
6918 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6919 TRUE);
6920 if (indx == (size_t) -1
6921 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6922 return FALSE;
6923 }
6924
6925 eif.info = info;
6926 eif.failed = FALSE;
6927
6928 /* Find all symbols which were defined in a dynamic object and make
6929 the backend pick a reasonable value for them. */
6930 elf_link_hash_traverse (elf_hash_table (info),
6931 _bfd_elf_adjust_dynamic_symbol,
6932 &eif);
6933 if (eif.failed)
6934 return FALSE;
6935
6936 /* Add some entries to the .dynamic section. We fill in some of the
6937 values later, in bfd_elf_final_link, but we must add the entries
6938 now so that we know the final size of the .dynamic section. */
6939
6940 /* If there are initialization and/or finalization functions to
6941 call then add the corresponding DT_INIT/DT_FINI entries. */
6942 h = (info->init_function
6943 ? elf_link_hash_lookup (elf_hash_table (info),
6944 info->init_function, FALSE,
6945 FALSE, FALSE)
6946 : NULL);
6947 if (h != NULL
6948 && (h->ref_regular
6949 || h->def_regular))
6950 {
6951 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6952 return FALSE;
6953 }
6954 h = (info->fini_function
6955 ? elf_link_hash_lookup (elf_hash_table (info),
6956 info->fini_function, FALSE,
6957 FALSE, FALSE)
6958 : NULL);
6959 if (h != NULL
6960 && (h->ref_regular
6961 || h->def_regular))
6962 {
6963 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6964 return FALSE;
6965 }
6966
6967 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6968 if (s != NULL && s->linker_has_input)
6969 {
6970 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6971 if (! bfd_link_executable (info))
6972 {
6973 bfd *sub;
6974 asection *o;
6975
6976 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6977 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
6978 && (o = sub->sections) != NULL
6979 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
6980 for (o = sub->sections; o != NULL; o = o->next)
6981 if (elf_section_data (o)->this_hdr.sh_type
6982 == SHT_PREINIT_ARRAY)
6983 {
6984 _bfd_error_handler
6985 (_("%pB: .preinit_array section is not allowed in DSO"),
6986 sub);
6987 break;
6988 }
6989
6990 bfd_set_error (bfd_error_nonrepresentable_section);
6991 return FALSE;
6992 }
6993
6994 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6995 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6996 return FALSE;
6997 }
6998 s = bfd_get_section_by_name (output_bfd, ".init_array");
6999 if (s != NULL && s->linker_has_input)
7000 {
7001 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7002 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7003 return FALSE;
7004 }
7005 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7006 if (s != NULL && s->linker_has_input)
7007 {
7008 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7009 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7010 return FALSE;
7011 }
7012
7013 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7014 /* If .dynstr is excluded from the link, we don't want any of
7015 these tags. Strictly, we should be checking each section
7016 individually; This quick check covers for the case where
7017 someone does a /DISCARD/ : { *(*) }. */
7018 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7019 {
7020 bfd_size_type strsize;
7021
7022 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7023 if ((info->emit_hash
7024 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7025 || (info->emit_gnu_hash
7026 && (bed->record_xhash_symbol == NULL
7027 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7028 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7029 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7030 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7031 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7032 bed->s->sizeof_sym))
7033 return FALSE;
7034 }
7035 }
7036
7037 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7038 return FALSE;
7039
7040 /* The backend must work out the sizes of all the other dynamic
7041 sections. */
7042 if (dynobj != NULL
7043 && bed->elf_backend_size_dynamic_sections != NULL
7044 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7045 return FALSE;
7046
7047 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7048 {
7049 if (elf_tdata (output_bfd)->cverdefs)
7050 {
7051 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7052
7053 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7054 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7055 return FALSE;
7056 }
7057
7058 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7059 {
7060 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7061 return FALSE;
7062 }
7063 else if (info->flags & DF_BIND_NOW)
7064 {
7065 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7066 return FALSE;
7067 }
7068
7069 if (info->flags_1)
7070 {
7071 if (bfd_link_executable (info))
7072 info->flags_1 &= ~ (DF_1_INITFIRST
7073 | DF_1_NODELETE
7074 | DF_1_NOOPEN);
7075 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7076 return FALSE;
7077 }
7078
7079 if (elf_tdata (output_bfd)->cverrefs)
7080 {
7081 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7082
7083 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7084 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7085 return FALSE;
7086 }
7087
7088 if ((elf_tdata (output_bfd)->cverrefs == 0
7089 && elf_tdata (output_bfd)->cverdefs == 0)
7090 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7091 {
7092 asection *s;
7093
7094 s = bfd_get_linker_section (dynobj, ".gnu.version");
7095 s->flags |= SEC_EXCLUDE;
7096 }
7097 }
7098 return TRUE;
7099 }
7100
7101 /* Find the first non-excluded output section. We'll use its
7102 section symbol for some emitted relocs. */
7103 void
7104 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7105 {
7106 asection *s;
7107 asection *found = NULL;
7108
7109 for (s = output_bfd->sections; s != NULL; s = s->next)
7110 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7111 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7112 {
7113 found = s;
7114 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7115 break;
7116 }
7117 elf_hash_table (info)->text_index_section = found;
7118 }
7119
7120 /* Find two non-excluded output sections, one for code, one for data.
7121 We'll use their section symbols for some emitted relocs. */
7122 void
7123 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7124 {
7125 asection *s;
7126 asection *found = NULL;
7127
7128 /* Data first, since setting text_index_section changes
7129 _bfd_elf_omit_section_dynsym_default. */
7130 for (s = output_bfd->sections; s != NULL; s = s->next)
7131 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7132 && !(s->flags & SEC_READONLY)
7133 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7134 {
7135 found = s;
7136 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7137 break;
7138 }
7139 elf_hash_table (info)->data_index_section = found;
7140
7141 for (s = output_bfd->sections; s != NULL; s = s->next)
7142 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7143 && (s->flags & SEC_READONLY)
7144 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7145 {
7146 found = s;
7147 break;
7148 }
7149 elf_hash_table (info)->text_index_section = found;
7150 }
7151
7152 #define GNU_HASH_SECTION_NAME(bed) \
7153 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7154
7155 bfd_boolean
7156 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7157 {
7158 const struct elf_backend_data *bed;
7159 unsigned long section_sym_count;
7160 bfd_size_type dynsymcount = 0;
7161
7162 if (!is_elf_hash_table (info->hash))
7163 return TRUE;
7164
7165 bed = get_elf_backend_data (output_bfd);
7166 (*bed->elf_backend_init_index_section) (output_bfd, info);
7167
7168 /* Assign dynsym indices. In a shared library we generate a section
7169 symbol for each output section, which come first. Next come all
7170 of the back-end allocated local dynamic syms, followed by the rest
7171 of the global symbols.
7172
7173 This is usually not needed for static binaries, however backends
7174 can request to always do it, e.g. the MIPS backend uses dynamic
7175 symbol counts to lay out GOT, which will be produced in the
7176 presence of GOT relocations even in static binaries (holding fixed
7177 data in that case, to satisfy those relocations). */
7178
7179 if (elf_hash_table (info)->dynamic_sections_created
7180 || bed->always_renumber_dynsyms)
7181 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7182 &section_sym_count);
7183
7184 if (elf_hash_table (info)->dynamic_sections_created)
7185 {
7186 bfd *dynobj;
7187 asection *s;
7188 unsigned int dtagcount;
7189
7190 dynobj = elf_hash_table (info)->dynobj;
7191
7192 /* Work out the size of the symbol version section. */
7193 s = bfd_get_linker_section (dynobj, ".gnu.version");
7194 BFD_ASSERT (s != NULL);
7195 if ((s->flags & SEC_EXCLUDE) == 0)
7196 {
7197 s->size = dynsymcount * sizeof (Elf_External_Versym);
7198 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7199 if (s->contents == NULL)
7200 return FALSE;
7201
7202 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7203 return FALSE;
7204 }
7205
7206 /* Set the size of the .dynsym and .hash sections. We counted
7207 the number of dynamic symbols in elf_link_add_object_symbols.
7208 We will build the contents of .dynsym and .hash when we build
7209 the final symbol table, because until then we do not know the
7210 correct value to give the symbols. We built the .dynstr
7211 section as we went along in elf_link_add_object_symbols. */
7212 s = elf_hash_table (info)->dynsym;
7213 BFD_ASSERT (s != NULL);
7214 s->size = dynsymcount * bed->s->sizeof_sym;
7215
7216 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7217 if (s->contents == NULL)
7218 return FALSE;
7219
7220 /* The first entry in .dynsym is a dummy symbol. Clear all the
7221 section syms, in case we don't output them all. */
7222 ++section_sym_count;
7223 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7224
7225 elf_hash_table (info)->bucketcount = 0;
7226
7227 /* Compute the size of the hashing table. As a side effect this
7228 computes the hash values for all the names we export. */
7229 if (info->emit_hash)
7230 {
7231 unsigned long int *hashcodes;
7232 struct hash_codes_info hashinf;
7233 bfd_size_type amt;
7234 unsigned long int nsyms;
7235 size_t bucketcount;
7236 size_t hash_entry_size;
7237
7238 /* Compute the hash values for all exported symbols. At the same
7239 time store the values in an array so that we could use them for
7240 optimizations. */
7241 amt = dynsymcount * sizeof (unsigned long int);
7242 hashcodes = (unsigned long int *) bfd_malloc (amt);
7243 if (hashcodes == NULL)
7244 return FALSE;
7245 hashinf.hashcodes = hashcodes;
7246 hashinf.error = FALSE;
7247
7248 /* Put all hash values in HASHCODES. */
7249 elf_link_hash_traverse (elf_hash_table (info),
7250 elf_collect_hash_codes, &hashinf);
7251 if (hashinf.error)
7252 {
7253 free (hashcodes);
7254 return FALSE;
7255 }
7256
7257 nsyms = hashinf.hashcodes - hashcodes;
7258 bucketcount
7259 = compute_bucket_count (info, hashcodes, nsyms, 0);
7260 free (hashcodes);
7261
7262 if (bucketcount == 0 && nsyms > 0)
7263 return FALSE;
7264
7265 elf_hash_table (info)->bucketcount = bucketcount;
7266
7267 s = bfd_get_linker_section (dynobj, ".hash");
7268 BFD_ASSERT (s != NULL);
7269 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7270 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7271 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7272 if (s->contents == NULL)
7273 return FALSE;
7274
7275 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7276 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7277 s->contents + hash_entry_size);
7278 }
7279
7280 if (info->emit_gnu_hash)
7281 {
7282 size_t i, cnt;
7283 unsigned char *contents;
7284 struct collect_gnu_hash_codes cinfo;
7285 bfd_size_type amt;
7286 size_t bucketcount;
7287
7288 memset (&cinfo, 0, sizeof (cinfo));
7289
7290 /* Compute the hash values for all exported symbols. At the same
7291 time store the values in an array so that we could use them for
7292 optimizations. */
7293 amt = dynsymcount * 2 * sizeof (unsigned long int);
7294 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7295 if (cinfo.hashcodes == NULL)
7296 return FALSE;
7297
7298 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7299 cinfo.min_dynindx = -1;
7300 cinfo.output_bfd = output_bfd;
7301 cinfo.bed = bed;
7302
7303 /* Put all hash values in HASHCODES. */
7304 elf_link_hash_traverse (elf_hash_table (info),
7305 elf_collect_gnu_hash_codes, &cinfo);
7306 if (cinfo.error)
7307 {
7308 free (cinfo.hashcodes);
7309 return FALSE;
7310 }
7311
7312 bucketcount
7313 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7314
7315 if (bucketcount == 0)
7316 {
7317 free (cinfo.hashcodes);
7318 return FALSE;
7319 }
7320
7321 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
7322 BFD_ASSERT (s != NULL);
7323
7324 if (cinfo.nsyms == 0)
7325 {
7326 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7327 BFD_ASSERT (cinfo.min_dynindx == -1);
7328 free (cinfo.hashcodes);
7329 s->size = 5 * 4 + bed->s->arch_size / 8;
7330 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7331 if (contents == NULL)
7332 return FALSE;
7333 s->contents = contents;
7334 /* 1 empty bucket. */
7335 bfd_put_32 (output_bfd, 1, contents);
7336 /* SYMIDX above the special symbol 0. */
7337 bfd_put_32 (output_bfd, 1, contents + 4);
7338 /* Just one word for bitmask. */
7339 bfd_put_32 (output_bfd, 1, contents + 8);
7340 /* Only hash fn bloom filter. */
7341 bfd_put_32 (output_bfd, 0, contents + 12);
7342 /* No hashes are valid - empty bitmask. */
7343 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7344 /* No hashes in the only bucket. */
7345 bfd_put_32 (output_bfd, 0,
7346 contents + 16 + bed->s->arch_size / 8);
7347 }
7348 else
7349 {
7350 unsigned long int maskwords, maskbitslog2, x;
7351 BFD_ASSERT (cinfo.min_dynindx != -1);
7352
7353 x = cinfo.nsyms;
7354 maskbitslog2 = 1;
7355 while ((x >>= 1) != 0)
7356 ++maskbitslog2;
7357 if (maskbitslog2 < 3)
7358 maskbitslog2 = 5;
7359 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7360 maskbitslog2 = maskbitslog2 + 3;
7361 else
7362 maskbitslog2 = maskbitslog2 + 2;
7363 if (bed->s->arch_size == 64)
7364 {
7365 if (maskbitslog2 == 5)
7366 maskbitslog2 = 6;
7367 cinfo.shift1 = 6;
7368 }
7369 else
7370 cinfo.shift1 = 5;
7371 cinfo.mask = (1 << cinfo.shift1) - 1;
7372 cinfo.shift2 = maskbitslog2;
7373 cinfo.maskbits = 1 << maskbitslog2;
7374 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7375 amt = bucketcount * sizeof (unsigned long int) * 2;
7376 amt += maskwords * sizeof (bfd_vma);
7377 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7378 if (cinfo.bitmask == NULL)
7379 {
7380 free (cinfo.hashcodes);
7381 return FALSE;
7382 }
7383
7384 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7385 cinfo.indx = cinfo.counts + bucketcount;
7386 cinfo.symindx = dynsymcount - cinfo.nsyms;
7387 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7388
7389 /* Determine how often each hash bucket is used. */
7390 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7391 for (i = 0; i < cinfo.nsyms; ++i)
7392 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7393
7394 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7395 if (cinfo.counts[i] != 0)
7396 {
7397 cinfo.indx[i] = cnt;
7398 cnt += cinfo.counts[i];
7399 }
7400 BFD_ASSERT (cnt == dynsymcount);
7401 cinfo.bucketcount = bucketcount;
7402 cinfo.local_indx = cinfo.min_dynindx;
7403
7404 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7405 s->size += cinfo.maskbits / 8;
7406 if (bed->record_xhash_symbol != NULL)
7407 s->size += cinfo.nsyms * 4;
7408 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7409 if (contents == NULL)
7410 {
7411 free (cinfo.bitmask);
7412 free (cinfo.hashcodes);
7413 return FALSE;
7414 }
7415
7416 s->contents = contents;
7417 bfd_put_32 (output_bfd, bucketcount, contents);
7418 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7419 bfd_put_32 (output_bfd, maskwords, contents + 8);
7420 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7421 contents += 16 + cinfo.maskbits / 8;
7422
7423 for (i = 0; i < bucketcount; ++i)
7424 {
7425 if (cinfo.counts[i] == 0)
7426 bfd_put_32 (output_bfd, 0, contents);
7427 else
7428 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7429 contents += 4;
7430 }
7431
7432 cinfo.contents = contents;
7433
7434 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7435 /* Renumber dynamic symbols, if populating .gnu.hash section.
7436 If using .MIPS.xhash, populate the translation table. */
7437 elf_link_hash_traverse (elf_hash_table (info),
7438 elf_gnu_hash_process_symidx, &cinfo);
7439
7440 contents = s->contents + 16;
7441 for (i = 0; i < maskwords; ++i)
7442 {
7443 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7444 contents);
7445 contents += bed->s->arch_size / 8;
7446 }
7447
7448 free (cinfo.bitmask);
7449 free (cinfo.hashcodes);
7450 }
7451 }
7452
7453 s = bfd_get_linker_section (dynobj, ".dynstr");
7454 BFD_ASSERT (s != NULL);
7455
7456 elf_finalize_dynstr (output_bfd, info);
7457
7458 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7459
7460 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7461 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7462 return FALSE;
7463 }
7464
7465 return TRUE;
7466 }
7467 \f
7468 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7469
7470 static void
7471 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7472 asection *sec)
7473 {
7474 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7475 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7476 }
7477
7478 /* Finish SHF_MERGE section merging. */
7479
7480 bfd_boolean
7481 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7482 {
7483 bfd *ibfd;
7484 asection *sec;
7485
7486 if (!is_elf_hash_table (info->hash))
7487 return FALSE;
7488
7489 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7490 if ((ibfd->flags & DYNAMIC) == 0
7491 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7492 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7493 == get_elf_backend_data (obfd)->s->elfclass))
7494 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7495 if ((sec->flags & SEC_MERGE) != 0
7496 && !bfd_is_abs_section (sec->output_section))
7497 {
7498 struct bfd_elf_section_data *secdata;
7499
7500 secdata = elf_section_data (sec);
7501 if (! _bfd_add_merge_section (obfd,
7502 &elf_hash_table (info)->merge_info,
7503 sec, &secdata->sec_info))
7504 return FALSE;
7505 else if (secdata->sec_info)
7506 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7507 }
7508
7509 if (elf_hash_table (info)->merge_info != NULL)
7510 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7511 merge_sections_remove_hook);
7512 return TRUE;
7513 }
7514
7515 /* Create an entry in an ELF linker hash table. */
7516
7517 struct bfd_hash_entry *
7518 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7519 struct bfd_hash_table *table,
7520 const char *string)
7521 {
7522 /* Allocate the structure if it has not already been allocated by a
7523 subclass. */
7524 if (entry == NULL)
7525 {
7526 entry = (struct bfd_hash_entry *)
7527 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7528 if (entry == NULL)
7529 return entry;
7530 }
7531
7532 /* Call the allocation method of the superclass. */
7533 entry = _bfd_link_hash_newfunc (entry, table, string);
7534 if (entry != NULL)
7535 {
7536 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7537 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7538
7539 /* Set local fields. */
7540 ret->indx = -1;
7541 ret->dynindx = -1;
7542 ret->got = htab->init_got_refcount;
7543 ret->plt = htab->init_plt_refcount;
7544 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7545 - offsetof (struct elf_link_hash_entry, size)));
7546 /* Assume that we have been called by a non-ELF symbol reader.
7547 This flag is then reset by the code which reads an ELF input
7548 file. This ensures that a symbol created by a non-ELF symbol
7549 reader will have the flag set correctly. */
7550 ret->non_elf = 1;
7551 }
7552
7553 return entry;
7554 }
7555
7556 /* Copy data from an indirect symbol to its direct symbol, hiding the
7557 old indirect symbol. Also used for copying flags to a weakdef. */
7558
7559 void
7560 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7561 struct elf_link_hash_entry *dir,
7562 struct elf_link_hash_entry *ind)
7563 {
7564 struct elf_link_hash_table *htab;
7565
7566 /* Copy down any references that we may have already seen to the
7567 symbol which just became indirect. */
7568
7569 if (dir->versioned != versioned_hidden)
7570 dir->ref_dynamic |= ind->ref_dynamic;
7571 dir->ref_regular |= ind->ref_regular;
7572 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7573 dir->non_got_ref |= ind->non_got_ref;
7574 dir->needs_plt |= ind->needs_plt;
7575 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7576
7577 if (ind->root.type != bfd_link_hash_indirect)
7578 return;
7579
7580 /* Copy over the global and procedure linkage table refcount entries.
7581 These may have been already set up by a check_relocs routine. */
7582 htab = elf_hash_table (info);
7583 if (ind->got.refcount > htab->init_got_refcount.refcount)
7584 {
7585 if (dir->got.refcount < 0)
7586 dir->got.refcount = 0;
7587 dir->got.refcount += ind->got.refcount;
7588 ind->got.refcount = htab->init_got_refcount.refcount;
7589 }
7590
7591 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7592 {
7593 if (dir->plt.refcount < 0)
7594 dir->plt.refcount = 0;
7595 dir->plt.refcount += ind->plt.refcount;
7596 ind->plt.refcount = htab->init_plt_refcount.refcount;
7597 }
7598
7599 if (ind->dynindx != -1)
7600 {
7601 if (dir->dynindx != -1)
7602 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7603 dir->dynindx = ind->dynindx;
7604 dir->dynstr_index = ind->dynstr_index;
7605 ind->dynindx = -1;
7606 ind->dynstr_index = 0;
7607 }
7608 }
7609
7610 void
7611 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7612 struct elf_link_hash_entry *h,
7613 bfd_boolean force_local)
7614 {
7615 /* STT_GNU_IFUNC symbol must go through PLT. */
7616 if (h->type != STT_GNU_IFUNC)
7617 {
7618 h->plt = elf_hash_table (info)->init_plt_offset;
7619 h->needs_plt = 0;
7620 }
7621 if (force_local)
7622 {
7623 h->forced_local = 1;
7624 if (h->dynindx != -1)
7625 {
7626 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7627 h->dynstr_index);
7628 h->dynindx = -1;
7629 h->dynstr_index = 0;
7630 }
7631 }
7632 }
7633
7634 /* Hide a symbol. */
7635
7636 void
7637 _bfd_elf_link_hide_symbol (bfd *output_bfd,
7638 struct bfd_link_info *info,
7639 struct bfd_link_hash_entry *h)
7640 {
7641 if (is_elf_hash_table (info->hash))
7642 {
7643 const struct elf_backend_data *bed
7644 = get_elf_backend_data (output_bfd);
7645 struct elf_link_hash_entry *eh
7646 = (struct elf_link_hash_entry *) h;
7647 bed->elf_backend_hide_symbol (info, eh, TRUE);
7648 eh->def_dynamic = 0;
7649 eh->ref_dynamic = 0;
7650 eh->dynamic_def = 0;
7651 }
7652 }
7653
7654 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7655 caller. */
7656
7657 bfd_boolean
7658 _bfd_elf_link_hash_table_init
7659 (struct elf_link_hash_table *table,
7660 bfd *abfd,
7661 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7662 struct bfd_hash_table *,
7663 const char *),
7664 unsigned int entsize,
7665 enum elf_target_id target_id)
7666 {
7667 bfd_boolean ret;
7668 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7669
7670 table->init_got_refcount.refcount = can_refcount - 1;
7671 table->init_plt_refcount.refcount = can_refcount - 1;
7672 table->init_got_offset.offset = -(bfd_vma) 1;
7673 table->init_plt_offset.offset = -(bfd_vma) 1;
7674 /* The first dynamic symbol is a dummy. */
7675 table->dynsymcount = 1;
7676
7677 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7678
7679 table->root.type = bfd_link_elf_hash_table;
7680 table->hash_table_id = target_id;
7681
7682 return ret;
7683 }
7684
7685 /* Create an ELF linker hash table. */
7686
7687 struct bfd_link_hash_table *
7688 _bfd_elf_link_hash_table_create (bfd *abfd)
7689 {
7690 struct elf_link_hash_table *ret;
7691 size_t amt = sizeof (struct elf_link_hash_table);
7692
7693 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7694 if (ret == NULL)
7695 return NULL;
7696
7697 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7698 sizeof (struct elf_link_hash_entry),
7699 GENERIC_ELF_DATA))
7700 {
7701 free (ret);
7702 return NULL;
7703 }
7704 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7705
7706 return &ret->root;
7707 }
7708
7709 /* Destroy an ELF linker hash table. */
7710
7711 void
7712 _bfd_elf_link_hash_table_free (bfd *obfd)
7713 {
7714 struct elf_link_hash_table *htab;
7715
7716 htab = (struct elf_link_hash_table *) obfd->link.hash;
7717 if (htab->dynstr != NULL)
7718 _bfd_elf_strtab_free (htab->dynstr);
7719 _bfd_merge_sections_free (htab->merge_info);
7720 _bfd_generic_link_hash_table_free (obfd);
7721 }
7722
7723 /* This is a hook for the ELF emulation code in the generic linker to
7724 tell the backend linker what file name to use for the DT_NEEDED
7725 entry for a dynamic object. */
7726
7727 void
7728 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7729 {
7730 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7731 && bfd_get_format (abfd) == bfd_object)
7732 elf_dt_name (abfd) = name;
7733 }
7734
7735 int
7736 bfd_elf_get_dyn_lib_class (bfd *abfd)
7737 {
7738 int lib_class;
7739 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7740 && bfd_get_format (abfd) == bfd_object)
7741 lib_class = elf_dyn_lib_class (abfd);
7742 else
7743 lib_class = 0;
7744 return lib_class;
7745 }
7746
7747 void
7748 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7749 {
7750 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7751 && bfd_get_format (abfd) == bfd_object)
7752 elf_dyn_lib_class (abfd) = lib_class;
7753 }
7754
7755 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7756 the linker ELF emulation code. */
7757
7758 struct bfd_link_needed_list *
7759 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7760 struct bfd_link_info *info)
7761 {
7762 if (! is_elf_hash_table (info->hash))
7763 return NULL;
7764 return elf_hash_table (info)->needed;
7765 }
7766
7767 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7768 hook for the linker ELF emulation code. */
7769
7770 struct bfd_link_needed_list *
7771 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7772 struct bfd_link_info *info)
7773 {
7774 if (! is_elf_hash_table (info->hash))
7775 return NULL;
7776 return elf_hash_table (info)->runpath;
7777 }
7778
7779 /* Get the name actually used for a dynamic object for a link. This
7780 is the SONAME entry if there is one. Otherwise, it is the string
7781 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7782
7783 const char *
7784 bfd_elf_get_dt_soname (bfd *abfd)
7785 {
7786 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7787 && bfd_get_format (abfd) == bfd_object)
7788 return elf_dt_name (abfd);
7789 return NULL;
7790 }
7791
7792 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7793 the ELF linker emulation code. */
7794
7795 bfd_boolean
7796 bfd_elf_get_bfd_needed_list (bfd *abfd,
7797 struct bfd_link_needed_list **pneeded)
7798 {
7799 asection *s;
7800 bfd_byte *dynbuf = NULL;
7801 unsigned int elfsec;
7802 unsigned long shlink;
7803 bfd_byte *extdyn, *extdynend;
7804 size_t extdynsize;
7805 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7806
7807 *pneeded = NULL;
7808
7809 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7810 || bfd_get_format (abfd) != bfd_object)
7811 return TRUE;
7812
7813 s = bfd_get_section_by_name (abfd, ".dynamic");
7814 if (s == NULL || s->size == 0)
7815 return TRUE;
7816
7817 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7818 goto error_return;
7819
7820 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7821 if (elfsec == SHN_BAD)
7822 goto error_return;
7823
7824 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7825
7826 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7827 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7828
7829 extdyn = dynbuf;
7830 extdynend = extdyn + s->size;
7831 for (; extdyn < extdynend; extdyn += extdynsize)
7832 {
7833 Elf_Internal_Dyn dyn;
7834
7835 (*swap_dyn_in) (abfd, extdyn, &dyn);
7836
7837 if (dyn.d_tag == DT_NULL)
7838 break;
7839
7840 if (dyn.d_tag == DT_NEEDED)
7841 {
7842 const char *string;
7843 struct bfd_link_needed_list *l;
7844 unsigned int tagv = dyn.d_un.d_val;
7845 size_t amt;
7846
7847 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7848 if (string == NULL)
7849 goto error_return;
7850
7851 amt = sizeof *l;
7852 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7853 if (l == NULL)
7854 goto error_return;
7855
7856 l->by = abfd;
7857 l->name = string;
7858 l->next = *pneeded;
7859 *pneeded = l;
7860 }
7861 }
7862
7863 free (dynbuf);
7864
7865 return TRUE;
7866
7867 error_return:
7868 if (dynbuf != NULL)
7869 free (dynbuf);
7870 return FALSE;
7871 }
7872
7873 struct elf_symbuf_symbol
7874 {
7875 unsigned long st_name; /* Symbol name, index in string tbl */
7876 unsigned char st_info; /* Type and binding attributes */
7877 unsigned char st_other; /* Visibilty, and target specific */
7878 };
7879
7880 struct elf_symbuf_head
7881 {
7882 struct elf_symbuf_symbol *ssym;
7883 size_t count;
7884 unsigned int st_shndx;
7885 };
7886
7887 struct elf_symbol
7888 {
7889 union
7890 {
7891 Elf_Internal_Sym *isym;
7892 struct elf_symbuf_symbol *ssym;
7893 void *p;
7894 } u;
7895 const char *name;
7896 };
7897
7898 /* Sort references to symbols by ascending section number. */
7899
7900 static int
7901 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7902 {
7903 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7904 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7905
7906 if (s1->st_shndx != s2->st_shndx)
7907 return s1->st_shndx > s2->st_shndx ? 1 : -1;
7908 /* Final sort by the address of the sym in the symbuf ensures
7909 a stable sort. */
7910 if (s1 != s2)
7911 return s1 > s2 ? 1 : -1;
7912 return 0;
7913 }
7914
7915 static int
7916 elf_sym_name_compare (const void *arg1, const void *arg2)
7917 {
7918 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7919 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7920 int ret = strcmp (s1->name, s2->name);
7921 if (ret != 0)
7922 return ret;
7923 if (s1->u.p != s2->u.p)
7924 return s1->u.p > s2->u.p ? 1 : -1;
7925 return 0;
7926 }
7927
7928 static struct elf_symbuf_head *
7929 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
7930 {
7931 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7932 struct elf_symbuf_symbol *ssym;
7933 struct elf_symbuf_head *ssymbuf, *ssymhead;
7934 size_t i, shndx_count, total_size;
7935
7936 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7937 if (indbuf == NULL)
7938 return NULL;
7939
7940 for (ind = indbuf, i = 0; i < symcount; i++)
7941 if (isymbuf[i].st_shndx != SHN_UNDEF)
7942 *ind++ = &isymbuf[i];
7943 indbufend = ind;
7944
7945 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7946 elf_sort_elf_symbol);
7947
7948 shndx_count = 0;
7949 if (indbufend > indbuf)
7950 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7951 if (ind[0]->st_shndx != ind[1]->st_shndx)
7952 shndx_count++;
7953
7954 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7955 + (indbufend - indbuf) * sizeof (*ssym));
7956 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7957 if (ssymbuf == NULL)
7958 {
7959 free (indbuf);
7960 return NULL;
7961 }
7962
7963 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7964 ssymbuf->ssym = NULL;
7965 ssymbuf->count = shndx_count;
7966 ssymbuf->st_shndx = 0;
7967 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7968 {
7969 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7970 {
7971 ssymhead++;
7972 ssymhead->ssym = ssym;
7973 ssymhead->count = 0;
7974 ssymhead->st_shndx = (*ind)->st_shndx;
7975 }
7976 ssym->st_name = (*ind)->st_name;
7977 ssym->st_info = (*ind)->st_info;
7978 ssym->st_other = (*ind)->st_other;
7979 ssymhead->count++;
7980 }
7981 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
7982 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7983 == total_size));
7984
7985 free (indbuf);
7986 return ssymbuf;
7987 }
7988
7989 /* Check if 2 sections define the same set of local and global
7990 symbols. */
7991
7992 static bfd_boolean
7993 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7994 struct bfd_link_info *info)
7995 {
7996 bfd *bfd1, *bfd2;
7997 const struct elf_backend_data *bed1, *bed2;
7998 Elf_Internal_Shdr *hdr1, *hdr2;
7999 size_t symcount1, symcount2;
8000 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8001 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8002 Elf_Internal_Sym *isym, *isymend;
8003 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
8004 size_t count1, count2, i;
8005 unsigned int shndx1, shndx2;
8006 bfd_boolean result;
8007
8008 bfd1 = sec1->owner;
8009 bfd2 = sec2->owner;
8010
8011 /* Both sections have to be in ELF. */
8012 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8013 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8014 return FALSE;
8015
8016 if (elf_section_type (sec1) != elf_section_type (sec2))
8017 return FALSE;
8018
8019 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8020 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8021 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
8022 return FALSE;
8023
8024 bed1 = get_elf_backend_data (bfd1);
8025 bed2 = get_elf_backend_data (bfd2);
8026 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8027 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8028 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8029 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8030
8031 if (symcount1 == 0 || symcount2 == 0)
8032 return FALSE;
8033
8034 result = FALSE;
8035 isymbuf1 = NULL;
8036 isymbuf2 = NULL;
8037 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8038 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8039
8040 if (ssymbuf1 == NULL)
8041 {
8042 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8043 NULL, NULL, NULL);
8044 if (isymbuf1 == NULL)
8045 goto done;
8046
8047 if (!info->reduce_memory_overheads)
8048 {
8049 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8050 elf_tdata (bfd1)->symbuf = ssymbuf1;
8051 }
8052 }
8053
8054 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8055 {
8056 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8057 NULL, NULL, NULL);
8058 if (isymbuf2 == NULL)
8059 goto done;
8060
8061 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
8062 {
8063 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8064 elf_tdata (bfd2)->symbuf = ssymbuf2;
8065 }
8066 }
8067
8068 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8069 {
8070 /* Optimized faster version. */
8071 size_t lo, hi, mid;
8072 struct elf_symbol *symp;
8073 struct elf_symbuf_symbol *ssym, *ssymend;
8074
8075 lo = 0;
8076 hi = ssymbuf1->count;
8077 ssymbuf1++;
8078 count1 = 0;
8079 while (lo < hi)
8080 {
8081 mid = (lo + hi) / 2;
8082 if (shndx1 < ssymbuf1[mid].st_shndx)
8083 hi = mid;
8084 else if (shndx1 > ssymbuf1[mid].st_shndx)
8085 lo = mid + 1;
8086 else
8087 {
8088 count1 = ssymbuf1[mid].count;
8089 ssymbuf1 += mid;
8090 break;
8091 }
8092 }
8093
8094 lo = 0;
8095 hi = ssymbuf2->count;
8096 ssymbuf2++;
8097 count2 = 0;
8098 while (lo < hi)
8099 {
8100 mid = (lo + hi) / 2;
8101 if (shndx2 < ssymbuf2[mid].st_shndx)
8102 hi = mid;
8103 else if (shndx2 > ssymbuf2[mid].st_shndx)
8104 lo = mid + 1;
8105 else
8106 {
8107 count2 = ssymbuf2[mid].count;
8108 ssymbuf2 += mid;
8109 break;
8110 }
8111 }
8112
8113 if (count1 == 0 || count2 == 0 || count1 != count2)
8114 goto done;
8115
8116 symtable1
8117 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8118 symtable2
8119 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8120 if (symtable1 == NULL || symtable2 == NULL)
8121 goto done;
8122
8123 symp = symtable1;
8124 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
8125 ssym < ssymend; ssym++, symp++)
8126 {
8127 symp->u.ssym = ssym;
8128 symp->name = bfd_elf_string_from_elf_section (bfd1,
8129 hdr1->sh_link,
8130 ssym->st_name);
8131 }
8132
8133 symp = symtable2;
8134 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8135 ssym < ssymend; ssym++, symp++)
8136 {
8137 symp->u.ssym = ssym;
8138 symp->name = bfd_elf_string_from_elf_section (bfd2,
8139 hdr2->sh_link,
8140 ssym->st_name);
8141 }
8142
8143 /* Sort symbol by name. */
8144 qsort (symtable1, count1, sizeof (struct elf_symbol),
8145 elf_sym_name_compare);
8146 qsort (symtable2, count1, sizeof (struct elf_symbol),
8147 elf_sym_name_compare);
8148
8149 for (i = 0; i < count1; i++)
8150 /* Two symbols must have the same binding, type and name. */
8151 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8152 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8153 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8154 goto done;
8155
8156 result = TRUE;
8157 goto done;
8158 }
8159
8160 symtable1 = (struct elf_symbol *)
8161 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8162 symtable2 = (struct elf_symbol *)
8163 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8164 if (symtable1 == NULL || symtable2 == NULL)
8165 goto done;
8166
8167 /* Count definitions in the section. */
8168 count1 = 0;
8169 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8170 if (isym->st_shndx == shndx1)
8171 symtable1[count1++].u.isym = isym;
8172
8173 count2 = 0;
8174 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8175 if (isym->st_shndx == shndx2)
8176 symtable2[count2++].u.isym = isym;
8177
8178 if (count1 == 0 || count2 == 0 || count1 != count2)
8179 goto done;
8180
8181 for (i = 0; i < count1; i++)
8182 symtable1[i].name
8183 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8184 symtable1[i].u.isym->st_name);
8185
8186 for (i = 0; i < count2; i++)
8187 symtable2[i].name
8188 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8189 symtable2[i].u.isym->st_name);
8190
8191 /* Sort symbol by name. */
8192 qsort (symtable1, count1, sizeof (struct elf_symbol),
8193 elf_sym_name_compare);
8194 qsort (symtable2, count1, sizeof (struct elf_symbol),
8195 elf_sym_name_compare);
8196
8197 for (i = 0; i < count1; i++)
8198 /* Two symbols must have the same binding, type and name. */
8199 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8200 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8201 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8202 goto done;
8203
8204 result = TRUE;
8205
8206 done:
8207 if (symtable1)
8208 free (symtable1);
8209 if (symtable2)
8210 free (symtable2);
8211 if (isymbuf1)
8212 free (isymbuf1);
8213 if (isymbuf2)
8214 free (isymbuf2);
8215
8216 return result;
8217 }
8218
8219 /* Return TRUE if 2 section types are compatible. */
8220
8221 bfd_boolean
8222 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8223 bfd *bbfd, const asection *bsec)
8224 {
8225 if (asec == NULL
8226 || bsec == NULL
8227 || abfd->xvec->flavour != bfd_target_elf_flavour
8228 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8229 return TRUE;
8230
8231 return elf_section_type (asec) == elf_section_type (bsec);
8232 }
8233 \f
8234 /* Final phase of ELF linker. */
8235
8236 /* A structure we use to avoid passing large numbers of arguments. */
8237
8238 struct elf_final_link_info
8239 {
8240 /* General link information. */
8241 struct bfd_link_info *info;
8242 /* Output BFD. */
8243 bfd *output_bfd;
8244 /* Symbol string table. */
8245 struct elf_strtab_hash *symstrtab;
8246 /* .hash section. */
8247 asection *hash_sec;
8248 /* symbol version section (.gnu.version). */
8249 asection *symver_sec;
8250 /* Buffer large enough to hold contents of any section. */
8251 bfd_byte *contents;
8252 /* Buffer large enough to hold external relocs of any section. */
8253 void *external_relocs;
8254 /* Buffer large enough to hold internal relocs of any section. */
8255 Elf_Internal_Rela *internal_relocs;
8256 /* Buffer large enough to hold external local symbols of any input
8257 BFD. */
8258 bfd_byte *external_syms;
8259 /* And a buffer for symbol section indices. */
8260 Elf_External_Sym_Shndx *locsym_shndx;
8261 /* Buffer large enough to hold internal local symbols of any input
8262 BFD. */
8263 Elf_Internal_Sym *internal_syms;
8264 /* Array large enough to hold a symbol index for each local symbol
8265 of any input BFD. */
8266 long *indices;
8267 /* Array large enough to hold a section pointer for each local
8268 symbol of any input BFD. */
8269 asection **sections;
8270 /* Buffer for SHT_SYMTAB_SHNDX section. */
8271 Elf_External_Sym_Shndx *symshndxbuf;
8272 /* Number of STT_FILE syms seen. */
8273 size_t filesym_count;
8274 };
8275
8276 /* This struct is used to pass information to elf_link_output_extsym. */
8277
8278 struct elf_outext_info
8279 {
8280 bfd_boolean failed;
8281 bfd_boolean localsyms;
8282 bfd_boolean file_sym_done;
8283 struct elf_final_link_info *flinfo;
8284 };
8285
8286
8287 /* Support for evaluating a complex relocation.
8288
8289 Complex relocations are generalized, self-describing relocations. The
8290 implementation of them consists of two parts: complex symbols, and the
8291 relocations themselves.
8292
8293 The relocations are use a reserved elf-wide relocation type code (R_RELC
8294 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8295 information (start bit, end bit, word width, etc) into the addend. This
8296 information is extracted from CGEN-generated operand tables within gas.
8297
8298 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8299 internal) representing prefix-notation expressions, including but not
8300 limited to those sorts of expressions normally encoded as addends in the
8301 addend field. The symbol mangling format is:
8302
8303 <node> := <literal>
8304 | <unary-operator> ':' <node>
8305 | <binary-operator> ':' <node> ':' <node>
8306 ;
8307
8308 <literal> := 's' <digits=N> ':' <N character symbol name>
8309 | 'S' <digits=N> ':' <N character section name>
8310 | '#' <hexdigits>
8311 ;
8312
8313 <binary-operator> := as in C
8314 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8315
8316 static void
8317 set_symbol_value (bfd *bfd_with_globals,
8318 Elf_Internal_Sym *isymbuf,
8319 size_t locsymcount,
8320 size_t symidx,
8321 bfd_vma val)
8322 {
8323 struct elf_link_hash_entry **sym_hashes;
8324 struct elf_link_hash_entry *h;
8325 size_t extsymoff = locsymcount;
8326
8327 if (symidx < locsymcount)
8328 {
8329 Elf_Internal_Sym *sym;
8330
8331 sym = isymbuf + symidx;
8332 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8333 {
8334 /* It is a local symbol: move it to the
8335 "absolute" section and give it a value. */
8336 sym->st_shndx = SHN_ABS;
8337 sym->st_value = val;
8338 return;
8339 }
8340 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8341 extsymoff = 0;
8342 }
8343
8344 /* It is a global symbol: set its link type
8345 to "defined" and give it a value. */
8346
8347 sym_hashes = elf_sym_hashes (bfd_with_globals);
8348 h = sym_hashes [symidx - extsymoff];
8349 while (h->root.type == bfd_link_hash_indirect
8350 || h->root.type == bfd_link_hash_warning)
8351 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8352 h->root.type = bfd_link_hash_defined;
8353 h->root.u.def.value = val;
8354 h->root.u.def.section = bfd_abs_section_ptr;
8355 }
8356
8357 static bfd_boolean
8358 resolve_symbol (const char *name,
8359 bfd *input_bfd,
8360 struct elf_final_link_info *flinfo,
8361 bfd_vma *result,
8362 Elf_Internal_Sym *isymbuf,
8363 size_t locsymcount)
8364 {
8365 Elf_Internal_Sym *sym;
8366 struct bfd_link_hash_entry *global_entry;
8367 const char *candidate = NULL;
8368 Elf_Internal_Shdr *symtab_hdr;
8369 size_t i;
8370
8371 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8372
8373 for (i = 0; i < locsymcount; ++ i)
8374 {
8375 sym = isymbuf + i;
8376
8377 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8378 continue;
8379
8380 candidate = bfd_elf_string_from_elf_section (input_bfd,
8381 symtab_hdr->sh_link,
8382 sym->st_name);
8383 #ifdef DEBUG
8384 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8385 name, candidate, (unsigned long) sym->st_value);
8386 #endif
8387 if (candidate && strcmp (candidate, name) == 0)
8388 {
8389 asection *sec = flinfo->sections [i];
8390
8391 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8392 *result += sec->output_offset + sec->output_section->vma;
8393 #ifdef DEBUG
8394 printf ("Found symbol with value %8.8lx\n",
8395 (unsigned long) *result);
8396 #endif
8397 return TRUE;
8398 }
8399 }
8400
8401 /* Hmm, haven't found it yet. perhaps it is a global. */
8402 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8403 FALSE, FALSE, TRUE);
8404 if (!global_entry)
8405 return FALSE;
8406
8407 if (global_entry->type == bfd_link_hash_defined
8408 || global_entry->type == bfd_link_hash_defweak)
8409 {
8410 *result = (global_entry->u.def.value
8411 + global_entry->u.def.section->output_section->vma
8412 + global_entry->u.def.section->output_offset);
8413 #ifdef DEBUG
8414 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8415 global_entry->root.string, (unsigned long) *result);
8416 #endif
8417 return TRUE;
8418 }
8419
8420 return FALSE;
8421 }
8422
8423 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8424 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8425 names like "foo.end" which is the end address of section "foo". */
8426
8427 static bfd_boolean
8428 resolve_section (const char *name,
8429 asection *sections,
8430 bfd_vma *result,
8431 bfd * abfd)
8432 {
8433 asection *curr;
8434 unsigned int len;
8435
8436 for (curr = sections; curr; curr = curr->next)
8437 if (strcmp (curr->name, name) == 0)
8438 {
8439 *result = curr->vma;
8440 return TRUE;
8441 }
8442
8443 /* Hmm. still haven't found it. try pseudo-section names. */
8444 /* FIXME: This could be coded more efficiently... */
8445 for (curr = sections; curr; curr = curr->next)
8446 {
8447 len = strlen (curr->name);
8448 if (len > strlen (name))
8449 continue;
8450
8451 if (strncmp (curr->name, name, len) == 0)
8452 {
8453 if (strncmp (".end", name + len, 4) == 0)
8454 {
8455 *result = (curr->vma
8456 + curr->size / bfd_octets_per_byte (abfd, curr));
8457 return TRUE;
8458 }
8459
8460 /* Insert more pseudo-section names here, if you like. */
8461 }
8462 }
8463
8464 return FALSE;
8465 }
8466
8467 static void
8468 undefined_reference (const char *reftype, const char *name)
8469 {
8470 /* xgettext:c-format */
8471 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8472 reftype, name);
8473 }
8474
8475 static bfd_boolean
8476 eval_symbol (bfd_vma *result,
8477 const char **symp,
8478 bfd *input_bfd,
8479 struct elf_final_link_info *flinfo,
8480 bfd_vma dot,
8481 Elf_Internal_Sym *isymbuf,
8482 size_t locsymcount,
8483 int signed_p)
8484 {
8485 size_t len;
8486 size_t symlen;
8487 bfd_vma a;
8488 bfd_vma b;
8489 char symbuf[4096];
8490 const char *sym = *symp;
8491 const char *symend;
8492 bfd_boolean symbol_is_section = FALSE;
8493
8494 len = strlen (sym);
8495 symend = sym + len;
8496
8497 if (len < 1 || len > sizeof (symbuf))
8498 {
8499 bfd_set_error (bfd_error_invalid_operation);
8500 return FALSE;
8501 }
8502
8503 switch (* sym)
8504 {
8505 case '.':
8506 *result = dot;
8507 *symp = sym + 1;
8508 return TRUE;
8509
8510 case '#':
8511 ++sym;
8512 *result = strtoul (sym, (char **) symp, 16);
8513 return TRUE;
8514
8515 case 'S':
8516 symbol_is_section = TRUE;
8517 /* Fall through. */
8518 case 's':
8519 ++sym;
8520 symlen = strtol (sym, (char **) symp, 10);
8521 sym = *symp + 1; /* Skip the trailing ':'. */
8522
8523 if (symend < sym || symlen + 1 > sizeof (symbuf))
8524 {
8525 bfd_set_error (bfd_error_invalid_operation);
8526 return FALSE;
8527 }
8528
8529 memcpy (symbuf, sym, symlen);
8530 symbuf[symlen] = '\0';
8531 *symp = sym + symlen;
8532
8533 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8534 the symbol as a section, or vice-versa. so we're pretty liberal in our
8535 interpretation here; section means "try section first", not "must be a
8536 section", and likewise with symbol. */
8537
8538 if (symbol_is_section)
8539 {
8540 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8541 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8542 isymbuf, locsymcount))
8543 {
8544 undefined_reference ("section", symbuf);
8545 return FALSE;
8546 }
8547 }
8548 else
8549 {
8550 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8551 isymbuf, locsymcount)
8552 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8553 result, input_bfd))
8554 {
8555 undefined_reference ("symbol", symbuf);
8556 return FALSE;
8557 }
8558 }
8559
8560 return TRUE;
8561
8562 /* All that remains are operators. */
8563
8564 #define UNARY_OP(op) \
8565 if (strncmp (sym, #op, strlen (#op)) == 0) \
8566 { \
8567 sym += strlen (#op); \
8568 if (*sym == ':') \
8569 ++sym; \
8570 *symp = sym; \
8571 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8572 isymbuf, locsymcount, signed_p)) \
8573 return FALSE; \
8574 if (signed_p) \
8575 *result = op ((bfd_signed_vma) a); \
8576 else \
8577 *result = op a; \
8578 return TRUE; \
8579 }
8580
8581 #define BINARY_OP(op) \
8582 if (strncmp (sym, #op, strlen (#op)) == 0) \
8583 { \
8584 sym += strlen (#op); \
8585 if (*sym == ':') \
8586 ++sym; \
8587 *symp = sym; \
8588 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8589 isymbuf, locsymcount, signed_p)) \
8590 return FALSE; \
8591 ++*symp; \
8592 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8593 isymbuf, locsymcount, signed_p)) \
8594 return FALSE; \
8595 if (signed_p) \
8596 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8597 else \
8598 *result = a op b; \
8599 return TRUE; \
8600 }
8601
8602 default:
8603 UNARY_OP (0-);
8604 BINARY_OP (<<);
8605 BINARY_OP (>>);
8606 BINARY_OP (==);
8607 BINARY_OP (!=);
8608 BINARY_OP (<=);
8609 BINARY_OP (>=);
8610 BINARY_OP (&&);
8611 BINARY_OP (||);
8612 UNARY_OP (~);
8613 UNARY_OP (!);
8614 BINARY_OP (*);
8615 BINARY_OP (/);
8616 BINARY_OP (%);
8617 BINARY_OP (^);
8618 BINARY_OP (|);
8619 BINARY_OP (&);
8620 BINARY_OP (+);
8621 BINARY_OP (-);
8622 BINARY_OP (<);
8623 BINARY_OP (>);
8624 #undef UNARY_OP
8625 #undef BINARY_OP
8626 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8627 bfd_set_error (bfd_error_invalid_operation);
8628 return FALSE;
8629 }
8630 }
8631
8632 static void
8633 put_value (bfd_vma size,
8634 unsigned long chunksz,
8635 bfd *input_bfd,
8636 bfd_vma x,
8637 bfd_byte *location)
8638 {
8639 location += (size - chunksz);
8640
8641 for (; size; size -= chunksz, location -= chunksz)
8642 {
8643 switch (chunksz)
8644 {
8645 case 1:
8646 bfd_put_8 (input_bfd, x, location);
8647 x >>= 8;
8648 break;
8649 case 2:
8650 bfd_put_16 (input_bfd, x, location);
8651 x >>= 16;
8652 break;
8653 case 4:
8654 bfd_put_32 (input_bfd, x, location);
8655 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8656 x >>= 16;
8657 x >>= 16;
8658 break;
8659 #ifdef BFD64
8660 case 8:
8661 bfd_put_64 (input_bfd, x, location);
8662 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8663 x >>= 32;
8664 x >>= 32;
8665 break;
8666 #endif
8667 default:
8668 abort ();
8669 break;
8670 }
8671 }
8672 }
8673
8674 static bfd_vma
8675 get_value (bfd_vma size,
8676 unsigned long chunksz,
8677 bfd *input_bfd,
8678 bfd_byte *location)
8679 {
8680 int shift;
8681 bfd_vma x = 0;
8682
8683 /* Sanity checks. */
8684 BFD_ASSERT (chunksz <= sizeof (x)
8685 && size >= chunksz
8686 && chunksz != 0
8687 && (size % chunksz) == 0
8688 && input_bfd != NULL
8689 && location != NULL);
8690
8691 if (chunksz == sizeof (x))
8692 {
8693 BFD_ASSERT (size == chunksz);
8694
8695 /* Make sure that we do not perform an undefined shift operation.
8696 We know that size == chunksz so there will only be one iteration
8697 of the loop below. */
8698 shift = 0;
8699 }
8700 else
8701 shift = 8 * chunksz;
8702
8703 for (; size; size -= chunksz, location += chunksz)
8704 {
8705 switch (chunksz)
8706 {
8707 case 1:
8708 x = (x << shift) | bfd_get_8 (input_bfd, location);
8709 break;
8710 case 2:
8711 x = (x << shift) | bfd_get_16 (input_bfd, location);
8712 break;
8713 case 4:
8714 x = (x << shift) | bfd_get_32 (input_bfd, location);
8715 break;
8716 #ifdef BFD64
8717 case 8:
8718 x = (x << shift) | bfd_get_64 (input_bfd, location);
8719 break;
8720 #endif
8721 default:
8722 abort ();
8723 }
8724 }
8725 return x;
8726 }
8727
8728 static void
8729 decode_complex_addend (unsigned long *start, /* in bits */
8730 unsigned long *oplen, /* in bits */
8731 unsigned long *len, /* in bits */
8732 unsigned long *wordsz, /* in bytes */
8733 unsigned long *chunksz, /* in bytes */
8734 unsigned long *lsb0_p,
8735 unsigned long *signed_p,
8736 unsigned long *trunc_p,
8737 unsigned long encoded)
8738 {
8739 * start = encoded & 0x3F;
8740 * len = (encoded >> 6) & 0x3F;
8741 * oplen = (encoded >> 12) & 0x3F;
8742 * wordsz = (encoded >> 18) & 0xF;
8743 * chunksz = (encoded >> 22) & 0xF;
8744 * lsb0_p = (encoded >> 27) & 1;
8745 * signed_p = (encoded >> 28) & 1;
8746 * trunc_p = (encoded >> 29) & 1;
8747 }
8748
8749 bfd_reloc_status_type
8750 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8751 asection *input_section,
8752 bfd_byte *contents,
8753 Elf_Internal_Rela *rel,
8754 bfd_vma relocation)
8755 {
8756 bfd_vma shift, x, mask;
8757 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8758 bfd_reloc_status_type r;
8759 bfd_size_type octets;
8760
8761 /* Perform this reloc, since it is complex.
8762 (this is not to say that it necessarily refers to a complex
8763 symbol; merely that it is a self-describing CGEN based reloc.
8764 i.e. the addend has the complete reloc information (bit start, end,
8765 word size, etc) encoded within it.). */
8766
8767 decode_complex_addend (&start, &oplen, &len, &wordsz,
8768 &chunksz, &lsb0_p, &signed_p,
8769 &trunc_p, rel->r_addend);
8770
8771 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8772
8773 if (lsb0_p)
8774 shift = (start + 1) - len;
8775 else
8776 shift = (8 * wordsz) - (start + len);
8777
8778 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
8779 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
8780
8781 #ifdef DEBUG
8782 printf ("Doing complex reloc: "
8783 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8784 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8785 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8786 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8787 oplen, (unsigned long) x, (unsigned long) mask,
8788 (unsigned long) relocation);
8789 #endif
8790
8791 r = bfd_reloc_ok;
8792 if (! trunc_p)
8793 /* Now do an overflow check. */
8794 r = bfd_check_overflow ((signed_p
8795 ? complain_overflow_signed
8796 : complain_overflow_unsigned),
8797 len, 0, (8 * wordsz),
8798 relocation);
8799
8800 /* Do the deed. */
8801 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8802
8803 #ifdef DEBUG
8804 printf (" relocation: %8.8lx\n"
8805 " shifted mask: %8.8lx\n"
8806 " shifted/masked reloc: %8.8lx\n"
8807 " result: %8.8lx\n",
8808 (unsigned long) relocation, (unsigned long) (mask << shift),
8809 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8810 #endif
8811 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
8812 return r;
8813 }
8814
8815 /* Functions to read r_offset from external (target order) reloc
8816 entry. Faster than bfd_getl32 et al, because we let the compiler
8817 know the value is aligned. */
8818
8819 static bfd_vma
8820 ext32l_r_offset (const void *p)
8821 {
8822 union aligned32
8823 {
8824 uint32_t v;
8825 unsigned char c[4];
8826 };
8827 const union aligned32 *a
8828 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8829
8830 uint32_t aval = ( (uint32_t) a->c[0]
8831 | (uint32_t) a->c[1] << 8
8832 | (uint32_t) a->c[2] << 16
8833 | (uint32_t) a->c[3] << 24);
8834 return aval;
8835 }
8836
8837 static bfd_vma
8838 ext32b_r_offset (const void *p)
8839 {
8840 union aligned32
8841 {
8842 uint32_t v;
8843 unsigned char c[4];
8844 };
8845 const union aligned32 *a
8846 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8847
8848 uint32_t aval = ( (uint32_t) a->c[0] << 24
8849 | (uint32_t) a->c[1] << 16
8850 | (uint32_t) a->c[2] << 8
8851 | (uint32_t) a->c[3]);
8852 return aval;
8853 }
8854
8855 #ifdef BFD_HOST_64_BIT
8856 static bfd_vma
8857 ext64l_r_offset (const void *p)
8858 {
8859 union aligned64
8860 {
8861 uint64_t v;
8862 unsigned char c[8];
8863 };
8864 const union aligned64 *a
8865 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8866
8867 uint64_t aval = ( (uint64_t) a->c[0]
8868 | (uint64_t) a->c[1] << 8
8869 | (uint64_t) a->c[2] << 16
8870 | (uint64_t) a->c[3] << 24
8871 | (uint64_t) a->c[4] << 32
8872 | (uint64_t) a->c[5] << 40
8873 | (uint64_t) a->c[6] << 48
8874 | (uint64_t) a->c[7] << 56);
8875 return aval;
8876 }
8877
8878 static bfd_vma
8879 ext64b_r_offset (const void *p)
8880 {
8881 union aligned64
8882 {
8883 uint64_t v;
8884 unsigned char c[8];
8885 };
8886 const union aligned64 *a
8887 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8888
8889 uint64_t aval = ( (uint64_t) a->c[0] << 56
8890 | (uint64_t) a->c[1] << 48
8891 | (uint64_t) a->c[2] << 40
8892 | (uint64_t) a->c[3] << 32
8893 | (uint64_t) a->c[4] << 24
8894 | (uint64_t) a->c[5] << 16
8895 | (uint64_t) a->c[6] << 8
8896 | (uint64_t) a->c[7]);
8897 return aval;
8898 }
8899 #endif
8900
8901 /* When performing a relocatable link, the input relocations are
8902 preserved. But, if they reference global symbols, the indices
8903 referenced must be updated. Update all the relocations found in
8904 RELDATA. */
8905
8906 static bfd_boolean
8907 elf_link_adjust_relocs (bfd *abfd,
8908 asection *sec,
8909 struct bfd_elf_section_reloc_data *reldata,
8910 bfd_boolean sort,
8911 struct bfd_link_info *info)
8912 {
8913 unsigned int i;
8914 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8915 bfd_byte *erela;
8916 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8917 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8918 bfd_vma r_type_mask;
8919 int r_sym_shift;
8920 unsigned int count = reldata->count;
8921 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8922
8923 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8924 {
8925 swap_in = bed->s->swap_reloc_in;
8926 swap_out = bed->s->swap_reloc_out;
8927 }
8928 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8929 {
8930 swap_in = bed->s->swap_reloca_in;
8931 swap_out = bed->s->swap_reloca_out;
8932 }
8933 else
8934 abort ();
8935
8936 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8937 abort ();
8938
8939 if (bed->s->arch_size == 32)
8940 {
8941 r_type_mask = 0xff;
8942 r_sym_shift = 8;
8943 }
8944 else
8945 {
8946 r_type_mask = 0xffffffff;
8947 r_sym_shift = 32;
8948 }
8949
8950 erela = reldata->hdr->contents;
8951 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8952 {
8953 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8954 unsigned int j;
8955
8956 if (*rel_hash == NULL)
8957 continue;
8958
8959 if ((*rel_hash)->indx == -2
8960 && info->gc_sections
8961 && ! info->gc_keep_exported)
8962 {
8963 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
8964 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
8965 abfd, sec,
8966 (*rel_hash)->root.root.string);
8967 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
8968 abfd, sec);
8969 bfd_set_error (bfd_error_invalid_operation);
8970 return FALSE;
8971 }
8972 BFD_ASSERT ((*rel_hash)->indx >= 0);
8973
8974 (*swap_in) (abfd, erela, irela);
8975 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8976 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8977 | (irela[j].r_info & r_type_mask));
8978 (*swap_out) (abfd, irela, erela);
8979 }
8980
8981 if (bed->elf_backend_update_relocs)
8982 (*bed->elf_backend_update_relocs) (sec, reldata);
8983
8984 if (sort && count != 0)
8985 {
8986 bfd_vma (*ext_r_off) (const void *);
8987 bfd_vma r_off;
8988 size_t elt_size;
8989 bfd_byte *base, *end, *p, *loc;
8990 bfd_byte *buf = NULL;
8991
8992 if (bed->s->arch_size == 32)
8993 {
8994 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8995 ext_r_off = ext32l_r_offset;
8996 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8997 ext_r_off = ext32b_r_offset;
8998 else
8999 abort ();
9000 }
9001 else
9002 {
9003 #ifdef BFD_HOST_64_BIT
9004 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
9005 ext_r_off = ext64l_r_offset;
9006 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
9007 ext_r_off = ext64b_r_offset;
9008 else
9009 #endif
9010 abort ();
9011 }
9012
9013 /* Must use a stable sort here. A modified insertion sort,
9014 since the relocs are mostly sorted already. */
9015 elt_size = reldata->hdr->sh_entsize;
9016 base = reldata->hdr->contents;
9017 end = base + count * elt_size;
9018 if (elt_size > sizeof (Elf64_External_Rela))
9019 abort ();
9020
9021 /* Ensure the first element is lowest. This acts as a sentinel,
9022 speeding the main loop below. */
9023 r_off = (*ext_r_off) (base);
9024 for (p = loc = base; (p += elt_size) < end; )
9025 {
9026 bfd_vma r_off2 = (*ext_r_off) (p);
9027 if (r_off > r_off2)
9028 {
9029 r_off = r_off2;
9030 loc = p;
9031 }
9032 }
9033 if (loc != base)
9034 {
9035 /* Don't just swap *base and *loc as that changes the order
9036 of the original base[0] and base[1] if they happen to
9037 have the same r_offset. */
9038 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9039 memcpy (onebuf, loc, elt_size);
9040 memmove (base + elt_size, base, loc - base);
9041 memcpy (base, onebuf, elt_size);
9042 }
9043
9044 for (p = base + elt_size; (p += elt_size) < end; )
9045 {
9046 /* base to p is sorted, *p is next to insert. */
9047 r_off = (*ext_r_off) (p);
9048 /* Search the sorted region for location to insert. */
9049 loc = p - elt_size;
9050 while (r_off < (*ext_r_off) (loc))
9051 loc -= elt_size;
9052 loc += elt_size;
9053 if (loc != p)
9054 {
9055 /* Chances are there is a run of relocs to insert here,
9056 from one of more input files. Files are not always
9057 linked in order due to the way elf_link_input_bfd is
9058 called. See pr17666. */
9059 size_t sortlen = p - loc;
9060 bfd_vma r_off2 = (*ext_r_off) (loc);
9061 size_t runlen = elt_size;
9062 size_t buf_size = 96 * 1024;
9063 while (p + runlen < end
9064 && (sortlen <= buf_size
9065 || runlen + elt_size <= buf_size)
9066 && r_off2 > (*ext_r_off) (p + runlen))
9067 runlen += elt_size;
9068 if (buf == NULL)
9069 {
9070 buf = bfd_malloc (buf_size);
9071 if (buf == NULL)
9072 return FALSE;
9073 }
9074 if (runlen < sortlen)
9075 {
9076 memcpy (buf, p, runlen);
9077 memmove (loc + runlen, loc, sortlen);
9078 memcpy (loc, buf, runlen);
9079 }
9080 else
9081 {
9082 memcpy (buf, loc, sortlen);
9083 memmove (loc, p, runlen);
9084 memcpy (loc + runlen, buf, sortlen);
9085 }
9086 p += runlen - elt_size;
9087 }
9088 }
9089 /* Hashes are no longer valid. */
9090 free (reldata->hashes);
9091 reldata->hashes = NULL;
9092 free (buf);
9093 }
9094 return TRUE;
9095 }
9096
9097 struct elf_link_sort_rela
9098 {
9099 union {
9100 bfd_vma offset;
9101 bfd_vma sym_mask;
9102 } u;
9103 enum elf_reloc_type_class type;
9104 /* We use this as an array of size int_rels_per_ext_rel. */
9105 Elf_Internal_Rela rela[1];
9106 };
9107
9108 /* qsort stability here and for cmp2 is only an issue if multiple
9109 dynamic relocations are emitted at the same address. But targets
9110 that apply a series of dynamic relocations each operating on the
9111 result of the prior relocation can't use -z combreloc as
9112 implemented anyway. Such schemes tend to be broken by sorting on
9113 symbol index. That leaves dynamic NONE relocs as the only other
9114 case where ld might emit multiple relocs at the same address, and
9115 those are only emitted due to target bugs. */
9116
9117 static int
9118 elf_link_sort_cmp1 (const void *A, const void *B)
9119 {
9120 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9121 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9122 int relativea, relativeb;
9123
9124 relativea = a->type == reloc_class_relative;
9125 relativeb = b->type == reloc_class_relative;
9126
9127 if (relativea < relativeb)
9128 return 1;
9129 if (relativea > relativeb)
9130 return -1;
9131 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9132 return -1;
9133 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9134 return 1;
9135 if (a->rela->r_offset < b->rela->r_offset)
9136 return -1;
9137 if (a->rela->r_offset > b->rela->r_offset)
9138 return 1;
9139 return 0;
9140 }
9141
9142 static int
9143 elf_link_sort_cmp2 (const void *A, const void *B)
9144 {
9145 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9146 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9147
9148 if (a->type < b->type)
9149 return -1;
9150 if (a->type > b->type)
9151 return 1;
9152 if (a->u.offset < b->u.offset)
9153 return -1;
9154 if (a->u.offset > b->u.offset)
9155 return 1;
9156 if (a->rela->r_offset < b->rela->r_offset)
9157 return -1;
9158 if (a->rela->r_offset > b->rela->r_offset)
9159 return 1;
9160 return 0;
9161 }
9162
9163 static size_t
9164 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9165 {
9166 asection *dynamic_relocs;
9167 asection *rela_dyn;
9168 asection *rel_dyn;
9169 bfd_size_type count, size;
9170 size_t i, ret, sort_elt, ext_size;
9171 bfd_byte *sort, *s_non_relative, *p;
9172 struct elf_link_sort_rela *sq;
9173 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9174 int i2e = bed->s->int_rels_per_ext_rel;
9175 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
9176 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9177 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9178 struct bfd_link_order *lo;
9179 bfd_vma r_sym_mask;
9180 bfd_boolean use_rela;
9181
9182 /* Find a dynamic reloc section. */
9183 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9184 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9185 if (rela_dyn != NULL && rela_dyn->size > 0
9186 && rel_dyn != NULL && rel_dyn->size > 0)
9187 {
9188 bfd_boolean use_rela_initialised = FALSE;
9189
9190 /* This is just here to stop gcc from complaining.
9191 Its initialization checking code is not perfect. */
9192 use_rela = TRUE;
9193
9194 /* Both sections are present. Examine the sizes
9195 of the indirect sections to help us choose. */
9196 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9197 if (lo->type == bfd_indirect_link_order)
9198 {
9199 asection *o = lo->u.indirect.section;
9200
9201 if ((o->size % bed->s->sizeof_rela) == 0)
9202 {
9203 if ((o->size % bed->s->sizeof_rel) == 0)
9204 /* Section size is divisible by both rel and rela sizes.
9205 It is of no help to us. */
9206 ;
9207 else
9208 {
9209 /* Section size is only divisible by rela. */
9210 if (use_rela_initialised && !use_rela)
9211 {
9212 _bfd_error_handler (_("%pB: unable to sort relocs - "
9213 "they are in more than one size"),
9214 abfd);
9215 bfd_set_error (bfd_error_invalid_operation);
9216 return 0;
9217 }
9218 else
9219 {
9220 use_rela = TRUE;
9221 use_rela_initialised = TRUE;
9222 }
9223 }
9224 }
9225 else if ((o->size % bed->s->sizeof_rel) == 0)
9226 {
9227 /* Section size is only divisible by rel. */
9228 if (use_rela_initialised && use_rela)
9229 {
9230 _bfd_error_handler (_("%pB: unable to sort relocs - "
9231 "they are in more than one size"),
9232 abfd);
9233 bfd_set_error (bfd_error_invalid_operation);
9234 return 0;
9235 }
9236 else
9237 {
9238 use_rela = FALSE;
9239 use_rela_initialised = TRUE;
9240 }
9241 }
9242 else
9243 {
9244 /* The section size is not divisible by either -
9245 something is wrong. */
9246 _bfd_error_handler (_("%pB: unable to sort relocs - "
9247 "they are of an unknown size"), abfd);
9248 bfd_set_error (bfd_error_invalid_operation);
9249 return 0;
9250 }
9251 }
9252
9253 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9254 if (lo->type == bfd_indirect_link_order)
9255 {
9256 asection *o = lo->u.indirect.section;
9257
9258 if ((o->size % bed->s->sizeof_rela) == 0)
9259 {
9260 if ((o->size % bed->s->sizeof_rel) == 0)
9261 /* Section size is divisible by both rel and rela sizes.
9262 It is of no help to us. */
9263 ;
9264 else
9265 {
9266 /* Section size is only divisible by rela. */
9267 if (use_rela_initialised && !use_rela)
9268 {
9269 _bfd_error_handler (_("%pB: unable to sort relocs - "
9270 "they are in more than one size"),
9271 abfd);
9272 bfd_set_error (bfd_error_invalid_operation);
9273 return 0;
9274 }
9275 else
9276 {
9277 use_rela = TRUE;
9278 use_rela_initialised = TRUE;
9279 }
9280 }
9281 }
9282 else if ((o->size % bed->s->sizeof_rel) == 0)
9283 {
9284 /* Section size is only divisible by rel. */
9285 if (use_rela_initialised && use_rela)
9286 {
9287 _bfd_error_handler (_("%pB: unable to sort relocs - "
9288 "they are in more than one size"),
9289 abfd);
9290 bfd_set_error (bfd_error_invalid_operation);
9291 return 0;
9292 }
9293 else
9294 {
9295 use_rela = FALSE;
9296 use_rela_initialised = TRUE;
9297 }
9298 }
9299 else
9300 {
9301 /* The section size is not divisible by either -
9302 something is wrong. */
9303 _bfd_error_handler (_("%pB: unable to sort relocs - "
9304 "they are of an unknown size"), abfd);
9305 bfd_set_error (bfd_error_invalid_operation);
9306 return 0;
9307 }
9308 }
9309
9310 if (! use_rela_initialised)
9311 /* Make a guess. */
9312 use_rela = TRUE;
9313 }
9314 else if (rela_dyn != NULL && rela_dyn->size > 0)
9315 use_rela = TRUE;
9316 else if (rel_dyn != NULL && rel_dyn->size > 0)
9317 use_rela = FALSE;
9318 else
9319 return 0;
9320
9321 if (use_rela)
9322 {
9323 dynamic_relocs = rela_dyn;
9324 ext_size = bed->s->sizeof_rela;
9325 swap_in = bed->s->swap_reloca_in;
9326 swap_out = bed->s->swap_reloca_out;
9327 }
9328 else
9329 {
9330 dynamic_relocs = rel_dyn;
9331 ext_size = bed->s->sizeof_rel;
9332 swap_in = bed->s->swap_reloc_in;
9333 swap_out = bed->s->swap_reloc_out;
9334 }
9335
9336 size = 0;
9337 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9338 if (lo->type == bfd_indirect_link_order)
9339 size += lo->u.indirect.section->size;
9340
9341 if (size != dynamic_relocs->size)
9342 return 0;
9343
9344 sort_elt = (sizeof (struct elf_link_sort_rela)
9345 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9346
9347 count = dynamic_relocs->size / ext_size;
9348 if (count == 0)
9349 return 0;
9350 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9351
9352 if (sort == NULL)
9353 {
9354 (*info->callbacks->warning)
9355 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
9356 return 0;
9357 }
9358
9359 if (bed->s->arch_size == 32)
9360 r_sym_mask = ~(bfd_vma) 0xff;
9361 else
9362 r_sym_mask = ~(bfd_vma) 0xffffffff;
9363
9364 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9365 if (lo->type == bfd_indirect_link_order)
9366 {
9367 bfd_byte *erel, *erelend;
9368 asection *o = lo->u.indirect.section;
9369
9370 if (o->contents == NULL && o->size != 0)
9371 {
9372 /* This is a reloc section that is being handled as a normal
9373 section. See bfd_section_from_shdr. We can't combine
9374 relocs in this case. */
9375 free (sort);
9376 return 0;
9377 }
9378 erel = o->contents;
9379 erelend = o->contents + o->size;
9380 p = sort + o->output_offset * opb / ext_size * sort_elt;
9381
9382 while (erel < erelend)
9383 {
9384 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9385
9386 (*swap_in) (abfd, erel, s->rela);
9387 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9388 s->u.sym_mask = r_sym_mask;
9389 p += sort_elt;
9390 erel += ext_size;
9391 }
9392 }
9393
9394 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9395
9396 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9397 {
9398 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9399 if (s->type != reloc_class_relative)
9400 break;
9401 }
9402 ret = i;
9403 s_non_relative = p;
9404
9405 sq = (struct elf_link_sort_rela *) s_non_relative;
9406 for (; i < count; i++, p += sort_elt)
9407 {
9408 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9409 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9410 sq = sp;
9411 sp->u.offset = sq->rela->r_offset;
9412 }
9413
9414 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9415
9416 struct elf_link_hash_table *htab = elf_hash_table (info);
9417 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9418 {
9419 /* We have plt relocs in .rela.dyn. */
9420 sq = (struct elf_link_sort_rela *) sort;
9421 for (i = 0; i < count; i++)
9422 if (sq[count - i - 1].type != reloc_class_plt)
9423 break;
9424 if (i != 0 && htab->srelplt->size == i * ext_size)
9425 {
9426 struct bfd_link_order **plo;
9427 /* Put srelplt link_order last. This is so the output_offset
9428 set in the next loop is correct for DT_JMPREL. */
9429 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9430 if ((*plo)->type == bfd_indirect_link_order
9431 && (*plo)->u.indirect.section == htab->srelplt)
9432 {
9433 lo = *plo;
9434 *plo = lo->next;
9435 }
9436 else
9437 plo = &(*plo)->next;
9438 *plo = lo;
9439 lo->next = NULL;
9440 dynamic_relocs->map_tail.link_order = lo;
9441 }
9442 }
9443
9444 p = sort;
9445 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9446 if (lo->type == bfd_indirect_link_order)
9447 {
9448 bfd_byte *erel, *erelend;
9449 asection *o = lo->u.indirect.section;
9450
9451 erel = o->contents;
9452 erelend = o->contents + o->size;
9453 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9454 while (erel < erelend)
9455 {
9456 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9457 (*swap_out) (abfd, s->rela, erel);
9458 p += sort_elt;
9459 erel += ext_size;
9460 }
9461 }
9462
9463 free (sort);
9464 *psec = dynamic_relocs;
9465 return ret;
9466 }
9467
9468 /* Add a symbol to the output symbol string table. */
9469
9470 static int
9471 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9472 const char *name,
9473 Elf_Internal_Sym *elfsym,
9474 asection *input_sec,
9475 struct elf_link_hash_entry *h)
9476 {
9477 int (*output_symbol_hook)
9478 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9479 struct elf_link_hash_entry *);
9480 struct elf_link_hash_table *hash_table;
9481 const struct elf_backend_data *bed;
9482 bfd_size_type strtabsize;
9483
9484 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9485
9486 bed = get_elf_backend_data (flinfo->output_bfd);
9487 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9488 if (output_symbol_hook != NULL)
9489 {
9490 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9491 if (ret != 1)
9492 return ret;
9493 }
9494
9495 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
9496 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
9497 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
9498 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
9499
9500 if (name == NULL
9501 || *name == '\0'
9502 || (input_sec->flags & SEC_EXCLUDE))
9503 elfsym->st_name = (unsigned long) -1;
9504 else
9505 {
9506 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9507 to get the final offset for st_name. */
9508 elfsym->st_name
9509 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9510 name, FALSE);
9511 if (elfsym->st_name == (unsigned long) -1)
9512 return 0;
9513 }
9514
9515 hash_table = elf_hash_table (flinfo->info);
9516 strtabsize = hash_table->strtabsize;
9517 if (strtabsize <= hash_table->strtabcount)
9518 {
9519 strtabsize += strtabsize;
9520 hash_table->strtabsize = strtabsize;
9521 strtabsize *= sizeof (*hash_table->strtab);
9522 hash_table->strtab
9523 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9524 strtabsize);
9525 if (hash_table->strtab == NULL)
9526 return 0;
9527 }
9528 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9529 hash_table->strtab[hash_table->strtabcount].dest_index
9530 = hash_table->strtabcount;
9531 hash_table->strtab[hash_table->strtabcount].destshndx_index
9532 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9533
9534 flinfo->output_bfd->symcount += 1;
9535 hash_table->strtabcount += 1;
9536
9537 return 1;
9538 }
9539
9540 /* Swap symbols out to the symbol table and flush the output symbols to
9541 the file. */
9542
9543 static bfd_boolean
9544 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9545 {
9546 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9547 size_t amt;
9548 size_t i;
9549 const struct elf_backend_data *bed;
9550 bfd_byte *symbuf;
9551 Elf_Internal_Shdr *hdr;
9552 file_ptr pos;
9553 bfd_boolean ret;
9554
9555 if (!hash_table->strtabcount)
9556 return TRUE;
9557
9558 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9559
9560 bed = get_elf_backend_data (flinfo->output_bfd);
9561
9562 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9563 symbuf = (bfd_byte *) bfd_malloc (amt);
9564 if (symbuf == NULL)
9565 return FALSE;
9566
9567 if (flinfo->symshndxbuf)
9568 {
9569 amt = sizeof (Elf_External_Sym_Shndx);
9570 amt *= bfd_get_symcount (flinfo->output_bfd);
9571 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9572 if (flinfo->symshndxbuf == NULL)
9573 {
9574 free (symbuf);
9575 return FALSE;
9576 }
9577 }
9578
9579 for (i = 0; i < hash_table->strtabcount; i++)
9580 {
9581 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9582 if (elfsym->sym.st_name == (unsigned long) -1)
9583 elfsym->sym.st_name = 0;
9584 else
9585 elfsym->sym.st_name
9586 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9587 elfsym->sym.st_name);
9588 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9589 ((bfd_byte *) symbuf
9590 + (elfsym->dest_index
9591 * bed->s->sizeof_sym)),
9592 (flinfo->symshndxbuf
9593 + elfsym->destshndx_index));
9594 }
9595
9596 /* Allow the linker to examine the strtab and symtab now they are
9597 populated. */
9598
9599 if (flinfo->info->callbacks->examine_strtab)
9600 flinfo->info->callbacks->examine_strtab (hash_table->strtab,
9601 hash_table->strtabcount,
9602 flinfo->symstrtab);
9603
9604 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9605 pos = hdr->sh_offset + hdr->sh_size;
9606 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9607 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9608 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9609 {
9610 hdr->sh_size += amt;
9611 ret = TRUE;
9612 }
9613 else
9614 ret = FALSE;
9615
9616 free (symbuf);
9617
9618 free (hash_table->strtab);
9619 hash_table->strtab = NULL;
9620
9621 return ret;
9622 }
9623
9624 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9625
9626 static bfd_boolean
9627 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9628 {
9629 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9630 && sym->st_shndx < SHN_LORESERVE)
9631 {
9632 /* The gABI doesn't support dynamic symbols in output sections
9633 beyond 64k. */
9634 _bfd_error_handler
9635 /* xgettext:c-format */
9636 (_("%pB: too many sections: %d (>= %d)"),
9637 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9638 bfd_set_error (bfd_error_nonrepresentable_section);
9639 return FALSE;
9640 }
9641 return TRUE;
9642 }
9643
9644 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9645 allowing an unsatisfied unversioned symbol in the DSO to match a
9646 versioned symbol that would normally require an explicit version.
9647 We also handle the case that a DSO references a hidden symbol
9648 which may be satisfied by a versioned symbol in another DSO. */
9649
9650 static bfd_boolean
9651 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9652 const struct elf_backend_data *bed,
9653 struct elf_link_hash_entry *h)
9654 {
9655 bfd *abfd;
9656 struct elf_link_loaded_list *loaded;
9657
9658 if (!is_elf_hash_table (info->hash))
9659 return FALSE;
9660
9661 /* Check indirect symbol. */
9662 while (h->root.type == bfd_link_hash_indirect)
9663 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9664
9665 switch (h->root.type)
9666 {
9667 default:
9668 abfd = NULL;
9669 break;
9670
9671 case bfd_link_hash_undefined:
9672 case bfd_link_hash_undefweak:
9673 abfd = h->root.u.undef.abfd;
9674 if (abfd == NULL
9675 || (abfd->flags & DYNAMIC) == 0
9676 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9677 return FALSE;
9678 break;
9679
9680 case bfd_link_hash_defined:
9681 case bfd_link_hash_defweak:
9682 abfd = h->root.u.def.section->owner;
9683 break;
9684
9685 case bfd_link_hash_common:
9686 abfd = h->root.u.c.p->section->owner;
9687 break;
9688 }
9689 BFD_ASSERT (abfd != NULL);
9690
9691 for (loaded = elf_hash_table (info)->loaded;
9692 loaded != NULL;
9693 loaded = loaded->next)
9694 {
9695 bfd *input;
9696 Elf_Internal_Shdr *hdr;
9697 size_t symcount;
9698 size_t extsymcount;
9699 size_t extsymoff;
9700 Elf_Internal_Shdr *versymhdr;
9701 Elf_Internal_Sym *isym;
9702 Elf_Internal_Sym *isymend;
9703 Elf_Internal_Sym *isymbuf;
9704 Elf_External_Versym *ever;
9705 Elf_External_Versym *extversym;
9706
9707 input = loaded->abfd;
9708
9709 /* We check each DSO for a possible hidden versioned definition. */
9710 if (input == abfd
9711 || (input->flags & DYNAMIC) == 0
9712 || elf_dynversym (input) == 0)
9713 continue;
9714
9715 hdr = &elf_tdata (input)->dynsymtab_hdr;
9716
9717 symcount = hdr->sh_size / bed->s->sizeof_sym;
9718 if (elf_bad_symtab (input))
9719 {
9720 extsymcount = symcount;
9721 extsymoff = 0;
9722 }
9723 else
9724 {
9725 extsymcount = symcount - hdr->sh_info;
9726 extsymoff = hdr->sh_info;
9727 }
9728
9729 if (extsymcount == 0)
9730 continue;
9731
9732 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9733 NULL, NULL, NULL);
9734 if (isymbuf == NULL)
9735 return FALSE;
9736
9737 /* Read in any version definitions. */
9738 versymhdr = &elf_tdata (input)->dynversym_hdr;
9739 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
9740 if (extversym == NULL)
9741 goto error_ret;
9742
9743 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9744 || (bfd_bread (extversym, versymhdr->sh_size, input)
9745 != versymhdr->sh_size))
9746 {
9747 free (extversym);
9748 error_ret:
9749 free (isymbuf);
9750 return FALSE;
9751 }
9752
9753 ever = extversym + extsymoff;
9754 isymend = isymbuf + extsymcount;
9755 for (isym = isymbuf; isym < isymend; isym++, ever++)
9756 {
9757 const char *name;
9758 Elf_Internal_Versym iver;
9759 unsigned short version_index;
9760
9761 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9762 || isym->st_shndx == SHN_UNDEF)
9763 continue;
9764
9765 name = bfd_elf_string_from_elf_section (input,
9766 hdr->sh_link,
9767 isym->st_name);
9768 if (strcmp (name, h->root.root.string) != 0)
9769 continue;
9770
9771 _bfd_elf_swap_versym_in (input, ever, &iver);
9772
9773 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9774 && !(h->def_regular
9775 && h->forced_local))
9776 {
9777 /* If we have a non-hidden versioned sym, then it should
9778 have provided a definition for the undefined sym unless
9779 it is defined in a non-shared object and forced local.
9780 */
9781 abort ();
9782 }
9783
9784 version_index = iver.vs_vers & VERSYM_VERSION;
9785 if (version_index == 1 || version_index == 2)
9786 {
9787 /* This is the base or first version. We can use it. */
9788 free (extversym);
9789 free (isymbuf);
9790 return TRUE;
9791 }
9792 }
9793
9794 free (extversym);
9795 free (isymbuf);
9796 }
9797
9798 return FALSE;
9799 }
9800
9801 /* Convert ELF common symbol TYPE. */
9802
9803 static int
9804 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9805 {
9806 /* Commom symbol can only appear in relocatable link. */
9807 if (!bfd_link_relocatable (info))
9808 abort ();
9809 switch (info->elf_stt_common)
9810 {
9811 case unchanged:
9812 break;
9813 case elf_stt_common:
9814 type = STT_COMMON;
9815 break;
9816 case no_elf_stt_common:
9817 type = STT_OBJECT;
9818 break;
9819 }
9820 return type;
9821 }
9822
9823 /* Add an external symbol to the symbol table. This is called from
9824 the hash table traversal routine. When generating a shared object,
9825 we go through the symbol table twice. The first time we output
9826 anything that might have been forced to local scope in a version
9827 script. The second time we output the symbols that are still
9828 global symbols. */
9829
9830 static bfd_boolean
9831 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9832 {
9833 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9834 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9835 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9836 bfd_boolean strip;
9837 Elf_Internal_Sym sym;
9838 asection *input_sec;
9839 const struct elf_backend_data *bed;
9840 long indx;
9841 int ret;
9842 unsigned int type;
9843
9844 if (h->root.type == bfd_link_hash_warning)
9845 {
9846 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9847 if (h->root.type == bfd_link_hash_new)
9848 return TRUE;
9849 }
9850
9851 /* Decide whether to output this symbol in this pass. */
9852 if (eoinfo->localsyms)
9853 {
9854 if (!h->forced_local)
9855 return TRUE;
9856 }
9857 else
9858 {
9859 if (h->forced_local)
9860 return TRUE;
9861 }
9862
9863 bed = get_elf_backend_data (flinfo->output_bfd);
9864
9865 if (h->root.type == bfd_link_hash_undefined)
9866 {
9867 /* If we have an undefined symbol reference here then it must have
9868 come from a shared library that is being linked in. (Undefined
9869 references in regular files have already been handled unless
9870 they are in unreferenced sections which are removed by garbage
9871 collection). */
9872 bfd_boolean ignore_undef = FALSE;
9873
9874 /* Some symbols may be special in that the fact that they're
9875 undefined can be safely ignored - let backend determine that. */
9876 if (bed->elf_backend_ignore_undef_symbol)
9877 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9878
9879 /* If we are reporting errors for this situation then do so now. */
9880 if (!ignore_undef
9881 && h->ref_dynamic_nonweak
9882 && (!h->ref_regular || flinfo->info->gc_sections)
9883 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9884 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9885 (*flinfo->info->callbacks->undefined_symbol)
9886 (flinfo->info, h->root.root.string,
9887 h->ref_regular ? NULL : h->root.u.undef.abfd,
9888 NULL, 0,
9889 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
9890
9891 /* Strip a global symbol defined in a discarded section. */
9892 if (h->indx == -3)
9893 return TRUE;
9894 }
9895
9896 /* We should also warn if a forced local symbol is referenced from
9897 shared libraries. */
9898 if (bfd_link_executable (flinfo->info)
9899 && h->forced_local
9900 && h->ref_dynamic
9901 && h->def_regular
9902 && !h->dynamic_def
9903 && h->ref_dynamic_nonweak
9904 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9905 {
9906 bfd *def_bfd;
9907 const char *msg;
9908 struct elf_link_hash_entry *hi = h;
9909
9910 /* Check indirect symbol. */
9911 while (hi->root.type == bfd_link_hash_indirect)
9912 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9913
9914 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9915 /* xgettext:c-format */
9916 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
9917 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9918 /* xgettext:c-format */
9919 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
9920 else
9921 /* xgettext:c-format */
9922 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
9923 def_bfd = flinfo->output_bfd;
9924 if (hi->root.u.def.section != bfd_abs_section_ptr)
9925 def_bfd = hi->root.u.def.section->owner;
9926 _bfd_error_handler (msg, flinfo->output_bfd,
9927 h->root.root.string, def_bfd);
9928 bfd_set_error (bfd_error_bad_value);
9929 eoinfo->failed = TRUE;
9930 return FALSE;
9931 }
9932
9933 /* We don't want to output symbols that have never been mentioned by
9934 a regular file, or that we have been told to strip. However, if
9935 h->indx is set to -2, the symbol is used by a reloc and we must
9936 output it. */
9937 strip = FALSE;
9938 if (h->indx == -2)
9939 ;
9940 else if ((h->def_dynamic
9941 || h->ref_dynamic
9942 || h->root.type == bfd_link_hash_new)
9943 && !h->def_regular
9944 && !h->ref_regular)
9945 strip = TRUE;
9946 else if (flinfo->info->strip == strip_all)
9947 strip = TRUE;
9948 else if (flinfo->info->strip == strip_some
9949 && bfd_hash_lookup (flinfo->info->keep_hash,
9950 h->root.root.string, FALSE, FALSE) == NULL)
9951 strip = TRUE;
9952 else if ((h->root.type == bfd_link_hash_defined
9953 || h->root.type == bfd_link_hash_defweak)
9954 && ((flinfo->info->strip_discarded
9955 && discarded_section (h->root.u.def.section))
9956 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9957 && h->root.u.def.section->owner != NULL
9958 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9959 strip = TRUE;
9960 else if ((h->root.type == bfd_link_hash_undefined
9961 || h->root.type == bfd_link_hash_undefweak)
9962 && h->root.u.undef.abfd != NULL
9963 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9964 strip = TRUE;
9965
9966 type = h->type;
9967
9968 /* If we're stripping it, and it's not a dynamic symbol, there's
9969 nothing else to do. However, if it is a forced local symbol or
9970 an ifunc symbol we need to give the backend finish_dynamic_symbol
9971 function a chance to make it dynamic. */
9972 if (strip
9973 && h->dynindx == -1
9974 && type != STT_GNU_IFUNC
9975 && !h->forced_local)
9976 return TRUE;
9977
9978 sym.st_value = 0;
9979 sym.st_size = h->size;
9980 sym.st_other = h->other;
9981 switch (h->root.type)
9982 {
9983 default:
9984 case bfd_link_hash_new:
9985 case bfd_link_hash_warning:
9986 abort ();
9987 return FALSE;
9988
9989 case bfd_link_hash_undefined:
9990 case bfd_link_hash_undefweak:
9991 input_sec = bfd_und_section_ptr;
9992 sym.st_shndx = SHN_UNDEF;
9993 break;
9994
9995 case bfd_link_hash_defined:
9996 case bfd_link_hash_defweak:
9997 {
9998 input_sec = h->root.u.def.section;
9999 if (input_sec->output_section != NULL)
10000 {
10001 sym.st_shndx =
10002 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
10003 input_sec->output_section);
10004 if (sym.st_shndx == SHN_BAD)
10005 {
10006 _bfd_error_handler
10007 /* xgettext:c-format */
10008 (_("%pB: could not find output section %pA for input section %pA"),
10009 flinfo->output_bfd, input_sec->output_section, input_sec);
10010 bfd_set_error (bfd_error_nonrepresentable_section);
10011 eoinfo->failed = TRUE;
10012 return FALSE;
10013 }
10014
10015 /* ELF symbols in relocatable files are section relative,
10016 but in nonrelocatable files they are virtual
10017 addresses. */
10018 sym.st_value = h->root.u.def.value + input_sec->output_offset;
10019 if (!bfd_link_relocatable (flinfo->info))
10020 {
10021 sym.st_value += input_sec->output_section->vma;
10022 if (h->type == STT_TLS)
10023 {
10024 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
10025 if (tls_sec != NULL)
10026 sym.st_value -= tls_sec->vma;
10027 }
10028 }
10029 }
10030 else
10031 {
10032 BFD_ASSERT (input_sec->owner == NULL
10033 || (input_sec->owner->flags & DYNAMIC) != 0);
10034 sym.st_shndx = SHN_UNDEF;
10035 input_sec = bfd_und_section_ptr;
10036 }
10037 }
10038 break;
10039
10040 case bfd_link_hash_common:
10041 input_sec = h->root.u.c.p->section;
10042 sym.st_shndx = bed->common_section_index (input_sec);
10043 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10044 break;
10045
10046 case bfd_link_hash_indirect:
10047 /* These symbols are created by symbol versioning. They point
10048 to the decorated version of the name. For example, if the
10049 symbol foo@@GNU_1.2 is the default, which should be used when
10050 foo is used with no version, then we add an indirect symbol
10051 foo which points to foo@@GNU_1.2. We ignore these symbols,
10052 since the indirected symbol is already in the hash table. */
10053 return TRUE;
10054 }
10055
10056 if (type == STT_COMMON || type == STT_OBJECT)
10057 switch (h->root.type)
10058 {
10059 case bfd_link_hash_common:
10060 type = elf_link_convert_common_type (flinfo->info, type);
10061 break;
10062 case bfd_link_hash_defined:
10063 case bfd_link_hash_defweak:
10064 if (bed->common_definition (&sym))
10065 type = elf_link_convert_common_type (flinfo->info, type);
10066 else
10067 type = STT_OBJECT;
10068 break;
10069 case bfd_link_hash_undefined:
10070 case bfd_link_hash_undefweak:
10071 break;
10072 default:
10073 abort ();
10074 }
10075
10076 if (h->forced_local)
10077 {
10078 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10079 /* Turn off visibility on local symbol. */
10080 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10081 }
10082 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10083 else if (h->unique_global && h->def_regular)
10084 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10085 else if (h->root.type == bfd_link_hash_undefweak
10086 || h->root.type == bfd_link_hash_defweak)
10087 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10088 else
10089 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10090 sym.st_target_internal = h->target_internal;
10091
10092 /* Give the processor backend a chance to tweak the symbol value,
10093 and also to finish up anything that needs to be done for this
10094 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10095 forced local syms when non-shared is due to a historical quirk.
10096 STT_GNU_IFUNC symbol must go through PLT. */
10097 if ((h->type == STT_GNU_IFUNC
10098 && h->def_regular
10099 && !bfd_link_relocatable (flinfo->info))
10100 || ((h->dynindx != -1
10101 || h->forced_local)
10102 && ((bfd_link_pic (flinfo->info)
10103 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10104 || h->root.type != bfd_link_hash_undefweak))
10105 || !h->forced_local)
10106 && elf_hash_table (flinfo->info)->dynamic_sections_created))
10107 {
10108 if (! ((*bed->elf_backend_finish_dynamic_symbol)
10109 (flinfo->output_bfd, flinfo->info, h, &sym)))
10110 {
10111 eoinfo->failed = TRUE;
10112 return FALSE;
10113 }
10114 }
10115
10116 /* If we are marking the symbol as undefined, and there are no
10117 non-weak references to this symbol from a regular object, then
10118 mark the symbol as weak undefined; if there are non-weak
10119 references, mark the symbol as strong. We can't do this earlier,
10120 because it might not be marked as undefined until the
10121 finish_dynamic_symbol routine gets through with it. */
10122 if (sym.st_shndx == SHN_UNDEF
10123 && h->ref_regular
10124 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10125 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10126 {
10127 int bindtype;
10128 type = ELF_ST_TYPE (sym.st_info);
10129
10130 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10131 if (type == STT_GNU_IFUNC)
10132 type = STT_FUNC;
10133
10134 if (h->ref_regular_nonweak)
10135 bindtype = STB_GLOBAL;
10136 else
10137 bindtype = STB_WEAK;
10138 sym.st_info = ELF_ST_INFO (bindtype, type);
10139 }
10140
10141 /* If this is a symbol defined in a dynamic library, don't use the
10142 symbol size from the dynamic library. Relinking an executable
10143 against a new library may introduce gratuitous changes in the
10144 executable's symbols if we keep the size. */
10145 if (sym.st_shndx == SHN_UNDEF
10146 && !h->def_regular
10147 && h->def_dynamic)
10148 sym.st_size = 0;
10149
10150 /* If a non-weak symbol with non-default visibility is not defined
10151 locally, it is a fatal error. */
10152 if (!bfd_link_relocatable (flinfo->info)
10153 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10154 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10155 && h->root.type == bfd_link_hash_undefined
10156 && !h->def_regular)
10157 {
10158 const char *msg;
10159
10160 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10161 /* xgettext:c-format */
10162 msg = _("%pB: protected symbol `%s' isn't defined");
10163 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10164 /* xgettext:c-format */
10165 msg = _("%pB: internal symbol `%s' isn't defined");
10166 else
10167 /* xgettext:c-format */
10168 msg = _("%pB: hidden symbol `%s' isn't defined");
10169 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10170 bfd_set_error (bfd_error_bad_value);
10171 eoinfo->failed = TRUE;
10172 return FALSE;
10173 }
10174
10175 /* If this symbol should be put in the .dynsym section, then put it
10176 there now. We already know the symbol index. We also fill in
10177 the entry in the .hash section. */
10178 if (h->dynindx != -1
10179 && elf_hash_table (flinfo->info)->dynamic_sections_created
10180 && elf_hash_table (flinfo->info)->dynsym != NULL
10181 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
10182 {
10183 bfd_byte *esym;
10184
10185 /* Since there is no version information in the dynamic string,
10186 if there is no version info in symbol version section, we will
10187 have a run-time problem if not linking executable, referenced
10188 by shared library, or not bound locally. */
10189 if (h->verinfo.verdef == NULL
10190 && (!bfd_link_executable (flinfo->info)
10191 || h->ref_dynamic
10192 || !h->def_regular))
10193 {
10194 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10195
10196 if (p && p [1] != '\0')
10197 {
10198 _bfd_error_handler
10199 /* xgettext:c-format */
10200 (_("%pB: no symbol version section for versioned symbol `%s'"),
10201 flinfo->output_bfd, h->root.root.string);
10202 eoinfo->failed = TRUE;
10203 return FALSE;
10204 }
10205 }
10206
10207 sym.st_name = h->dynstr_index;
10208 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10209 + h->dynindx * bed->s->sizeof_sym);
10210 if (!check_dynsym (flinfo->output_bfd, &sym))
10211 {
10212 eoinfo->failed = TRUE;
10213 return FALSE;
10214 }
10215 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
10216
10217 if (flinfo->hash_sec != NULL)
10218 {
10219 size_t hash_entry_size;
10220 bfd_byte *bucketpos;
10221 bfd_vma chain;
10222 size_t bucketcount;
10223 size_t bucket;
10224
10225 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
10226 bucket = h->u.elf_hash_value % bucketcount;
10227
10228 hash_entry_size
10229 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10230 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
10231 + (bucket + 2) * hash_entry_size);
10232 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10233 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10234 bucketpos);
10235 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10236 ((bfd_byte *) flinfo->hash_sec->contents
10237 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10238 }
10239
10240 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
10241 {
10242 Elf_Internal_Versym iversym;
10243 Elf_External_Versym *eversym;
10244
10245 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
10246 {
10247 if (h->verinfo.verdef == NULL
10248 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10249 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
10250 iversym.vs_vers = 0;
10251 else
10252 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10253 }
10254 else
10255 {
10256 if (h->verinfo.vertree == NULL)
10257 iversym.vs_vers = 1;
10258 else
10259 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
10260 if (flinfo->info->create_default_symver)
10261 iversym.vs_vers++;
10262 }
10263
10264 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10265 defined locally. */
10266 if (h->versioned == versioned_hidden && h->def_regular)
10267 iversym.vs_vers |= VERSYM_HIDDEN;
10268
10269 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10270 eversym += h->dynindx;
10271 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10272 }
10273 }
10274
10275 /* If the symbol is undefined, and we didn't output it to .dynsym,
10276 strip it from .symtab too. Obviously we can't do this for
10277 relocatable output or when needed for --emit-relocs. */
10278 else if (input_sec == bfd_und_section_ptr
10279 && h->indx != -2
10280 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10281 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10282 && !bfd_link_relocatable (flinfo->info))
10283 return TRUE;
10284
10285 /* Also strip others that we couldn't earlier due to dynamic symbol
10286 processing. */
10287 if (strip)
10288 return TRUE;
10289 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10290 return TRUE;
10291
10292 /* Output a FILE symbol so that following locals are not associated
10293 with the wrong input file. We need one for forced local symbols
10294 if we've seen more than one FILE symbol or when we have exactly
10295 one FILE symbol but global symbols are present in a file other
10296 than the one with the FILE symbol. We also need one if linker
10297 defined symbols are present. In practice these conditions are
10298 always met, so just emit the FILE symbol unconditionally. */
10299 if (eoinfo->localsyms
10300 && !eoinfo->file_sym_done
10301 && eoinfo->flinfo->filesym_count != 0)
10302 {
10303 Elf_Internal_Sym fsym;
10304
10305 memset (&fsym, 0, sizeof (fsym));
10306 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10307 fsym.st_shndx = SHN_ABS;
10308 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10309 bfd_und_section_ptr, NULL))
10310 return FALSE;
10311
10312 eoinfo->file_sym_done = TRUE;
10313 }
10314
10315 indx = bfd_get_symcount (flinfo->output_bfd);
10316 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10317 input_sec, h);
10318 if (ret == 0)
10319 {
10320 eoinfo->failed = TRUE;
10321 return FALSE;
10322 }
10323 else if (ret == 1)
10324 h->indx = indx;
10325 else if (h->indx == -2)
10326 abort();
10327
10328 return TRUE;
10329 }
10330
10331 /* Return TRUE if special handling is done for relocs in SEC against
10332 symbols defined in discarded sections. */
10333
10334 static bfd_boolean
10335 elf_section_ignore_discarded_relocs (asection *sec)
10336 {
10337 const struct elf_backend_data *bed;
10338
10339 switch (sec->sec_info_type)
10340 {
10341 case SEC_INFO_TYPE_STABS:
10342 case SEC_INFO_TYPE_EH_FRAME:
10343 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10344 return TRUE;
10345 default:
10346 break;
10347 }
10348
10349 bed = get_elf_backend_data (sec->owner);
10350 if (bed->elf_backend_ignore_discarded_relocs != NULL
10351 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10352 return TRUE;
10353
10354 return FALSE;
10355 }
10356
10357 /* Return a mask saying how ld should treat relocations in SEC against
10358 symbols defined in discarded sections. If this function returns
10359 COMPLAIN set, ld will issue a warning message. If this function
10360 returns PRETEND set, and the discarded section was link-once and the
10361 same size as the kept link-once section, ld will pretend that the
10362 symbol was actually defined in the kept section. Otherwise ld will
10363 zero the reloc (at least that is the intent, but some cooperation by
10364 the target dependent code is needed, particularly for REL targets). */
10365
10366 unsigned int
10367 _bfd_elf_default_action_discarded (asection *sec)
10368 {
10369 if (sec->flags & SEC_DEBUGGING)
10370 return PRETEND;
10371
10372 if (strcmp (".eh_frame", sec->name) == 0)
10373 return 0;
10374
10375 if (strcmp (".gcc_except_table", sec->name) == 0)
10376 return 0;
10377
10378 return COMPLAIN | PRETEND;
10379 }
10380
10381 /* Find a match between a section and a member of a section group. */
10382
10383 static asection *
10384 match_group_member (asection *sec, asection *group,
10385 struct bfd_link_info *info)
10386 {
10387 asection *first = elf_next_in_group (group);
10388 asection *s = first;
10389
10390 while (s != NULL)
10391 {
10392 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10393 return s;
10394
10395 s = elf_next_in_group (s);
10396 if (s == first)
10397 break;
10398 }
10399
10400 return NULL;
10401 }
10402
10403 /* Check if the kept section of a discarded section SEC can be used
10404 to replace it. Return the replacement if it is OK. Otherwise return
10405 NULL. */
10406
10407 asection *
10408 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10409 {
10410 asection *kept;
10411
10412 kept = sec->kept_section;
10413 if (kept != NULL)
10414 {
10415 if ((kept->flags & SEC_GROUP) != 0)
10416 kept = match_group_member (sec, kept, info);
10417 if (kept != NULL
10418 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10419 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
10420 kept = NULL;
10421 sec->kept_section = kept;
10422 }
10423 return kept;
10424 }
10425
10426 /* Link an input file into the linker output file. This function
10427 handles all the sections and relocations of the input file at once.
10428 This is so that we only have to read the local symbols once, and
10429 don't have to keep them in memory. */
10430
10431 static bfd_boolean
10432 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10433 {
10434 int (*relocate_section)
10435 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10436 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10437 bfd *output_bfd;
10438 Elf_Internal_Shdr *symtab_hdr;
10439 size_t locsymcount;
10440 size_t extsymoff;
10441 Elf_Internal_Sym *isymbuf;
10442 Elf_Internal_Sym *isym;
10443 Elf_Internal_Sym *isymend;
10444 long *pindex;
10445 asection **ppsection;
10446 asection *o;
10447 const struct elf_backend_data *bed;
10448 struct elf_link_hash_entry **sym_hashes;
10449 bfd_size_type address_size;
10450 bfd_vma r_type_mask;
10451 int r_sym_shift;
10452 bfd_boolean have_file_sym = FALSE;
10453
10454 output_bfd = flinfo->output_bfd;
10455 bed = get_elf_backend_data (output_bfd);
10456 relocate_section = bed->elf_backend_relocate_section;
10457
10458 /* If this is a dynamic object, we don't want to do anything here:
10459 we don't want the local symbols, and we don't want the section
10460 contents. */
10461 if ((input_bfd->flags & DYNAMIC) != 0)
10462 return TRUE;
10463
10464 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10465 if (elf_bad_symtab (input_bfd))
10466 {
10467 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10468 extsymoff = 0;
10469 }
10470 else
10471 {
10472 locsymcount = symtab_hdr->sh_info;
10473 extsymoff = symtab_hdr->sh_info;
10474 }
10475
10476 /* Read the local symbols. */
10477 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10478 if (isymbuf == NULL && locsymcount != 0)
10479 {
10480 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10481 flinfo->internal_syms,
10482 flinfo->external_syms,
10483 flinfo->locsym_shndx);
10484 if (isymbuf == NULL)
10485 return FALSE;
10486 }
10487
10488 /* Find local symbol sections and adjust values of symbols in
10489 SEC_MERGE sections. Write out those local symbols we know are
10490 going into the output file. */
10491 isymend = isymbuf + locsymcount;
10492 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10493 isym < isymend;
10494 isym++, pindex++, ppsection++)
10495 {
10496 asection *isec;
10497 const char *name;
10498 Elf_Internal_Sym osym;
10499 long indx;
10500 int ret;
10501
10502 *pindex = -1;
10503
10504 if (elf_bad_symtab (input_bfd))
10505 {
10506 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10507 {
10508 *ppsection = NULL;
10509 continue;
10510 }
10511 }
10512
10513 if (isym->st_shndx == SHN_UNDEF)
10514 isec = bfd_und_section_ptr;
10515 else if (isym->st_shndx == SHN_ABS)
10516 isec = bfd_abs_section_ptr;
10517 else if (isym->st_shndx == SHN_COMMON)
10518 isec = bfd_com_section_ptr;
10519 else
10520 {
10521 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10522 if (isec == NULL)
10523 {
10524 /* Don't attempt to output symbols with st_shnx in the
10525 reserved range other than SHN_ABS and SHN_COMMON. */
10526 isec = bfd_und_section_ptr;
10527 }
10528 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10529 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10530 isym->st_value =
10531 _bfd_merged_section_offset (output_bfd, &isec,
10532 elf_section_data (isec)->sec_info,
10533 isym->st_value);
10534 }
10535
10536 *ppsection = isec;
10537
10538 /* Don't output the first, undefined, symbol. In fact, don't
10539 output any undefined local symbol. */
10540 if (isec == bfd_und_section_ptr)
10541 continue;
10542
10543 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10544 {
10545 /* We never output section symbols. Instead, we use the
10546 section symbol of the corresponding section in the output
10547 file. */
10548 continue;
10549 }
10550
10551 /* If we are stripping all symbols, we don't want to output this
10552 one. */
10553 if (flinfo->info->strip == strip_all)
10554 continue;
10555
10556 /* If we are discarding all local symbols, we don't want to
10557 output this one. If we are generating a relocatable output
10558 file, then some of the local symbols may be required by
10559 relocs; we output them below as we discover that they are
10560 needed. */
10561 if (flinfo->info->discard == discard_all)
10562 continue;
10563
10564 /* If this symbol is defined in a section which we are
10565 discarding, we don't need to keep it. */
10566 if (isym->st_shndx != SHN_UNDEF
10567 && isym->st_shndx < SHN_LORESERVE
10568 && bfd_section_removed_from_list (output_bfd,
10569 isec->output_section))
10570 continue;
10571
10572 /* Get the name of the symbol. */
10573 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10574 isym->st_name);
10575 if (name == NULL)
10576 return FALSE;
10577
10578 /* See if we are discarding symbols with this name. */
10579 if ((flinfo->info->strip == strip_some
10580 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
10581 == NULL))
10582 || (((flinfo->info->discard == discard_sec_merge
10583 && (isec->flags & SEC_MERGE)
10584 && !bfd_link_relocatable (flinfo->info))
10585 || flinfo->info->discard == discard_l)
10586 && bfd_is_local_label_name (input_bfd, name)))
10587 continue;
10588
10589 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10590 {
10591 if (input_bfd->lto_output)
10592 /* -flto puts a temp file name here. This means builds
10593 are not reproducible. Discard the symbol. */
10594 continue;
10595 have_file_sym = TRUE;
10596 flinfo->filesym_count += 1;
10597 }
10598 if (!have_file_sym)
10599 {
10600 /* In the absence of debug info, bfd_find_nearest_line uses
10601 FILE symbols to determine the source file for local
10602 function symbols. Provide a FILE symbol here if input
10603 files lack such, so that their symbols won't be
10604 associated with a previous input file. It's not the
10605 source file, but the best we can do. */
10606 have_file_sym = TRUE;
10607 flinfo->filesym_count += 1;
10608 memset (&osym, 0, sizeof (osym));
10609 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10610 osym.st_shndx = SHN_ABS;
10611 if (!elf_link_output_symstrtab (flinfo,
10612 (input_bfd->lto_output ? NULL
10613 : input_bfd->filename),
10614 &osym, bfd_abs_section_ptr,
10615 NULL))
10616 return FALSE;
10617 }
10618
10619 osym = *isym;
10620
10621 /* Adjust the section index for the output file. */
10622 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10623 isec->output_section);
10624 if (osym.st_shndx == SHN_BAD)
10625 return FALSE;
10626
10627 /* ELF symbols in relocatable files are section relative, but
10628 in executable files they are virtual addresses. Note that
10629 this code assumes that all ELF sections have an associated
10630 BFD section with a reasonable value for output_offset; below
10631 we assume that they also have a reasonable value for
10632 output_section. Any special sections must be set up to meet
10633 these requirements. */
10634 osym.st_value += isec->output_offset;
10635 if (!bfd_link_relocatable (flinfo->info))
10636 {
10637 osym.st_value += isec->output_section->vma;
10638 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10639 {
10640 /* STT_TLS symbols are relative to PT_TLS segment base. */
10641 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
10642 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10643 else
10644 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
10645 STT_NOTYPE);
10646 }
10647 }
10648
10649 indx = bfd_get_symcount (output_bfd);
10650 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10651 if (ret == 0)
10652 return FALSE;
10653 else if (ret == 1)
10654 *pindex = indx;
10655 }
10656
10657 if (bed->s->arch_size == 32)
10658 {
10659 r_type_mask = 0xff;
10660 r_sym_shift = 8;
10661 address_size = 4;
10662 }
10663 else
10664 {
10665 r_type_mask = 0xffffffff;
10666 r_sym_shift = 32;
10667 address_size = 8;
10668 }
10669
10670 /* Relocate the contents of each section. */
10671 sym_hashes = elf_sym_hashes (input_bfd);
10672 for (o = input_bfd->sections; o != NULL; o = o->next)
10673 {
10674 bfd_byte *contents;
10675
10676 if (! o->linker_mark)
10677 {
10678 /* This section was omitted from the link. */
10679 continue;
10680 }
10681
10682 if (!flinfo->info->resolve_section_groups
10683 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10684 {
10685 /* Deal with the group signature symbol. */
10686 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10687 unsigned long symndx = sec_data->this_hdr.sh_info;
10688 asection *osec = o->output_section;
10689
10690 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
10691 if (symndx >= locsymcount
10692 || (elf_bad_symtab (input_bfd)
10693 && flinfo->sections[symndx] == NULL))
10694 {
10695 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10696 while (h->root.type == bfd_link_hash_indirect
10697 || h->root.type == bfd_link_hash_warning)
10698 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10699 /* Arrange for symbol to be output. */
10700 h->indx = -2;
10701 elf_section_data (osec)->this_hdr.sh_info = -2;
10702 }
10703 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10704 {
10705 /* We'll use the output section target_index. */
10706 asection *sec = flinfo->sections[symndx]->output_section;
10707 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10708 }
10709 else
10710 {
10711 if (flinfo->indices[symndx] == -1)
10712 {
10713 /* Otherwise output the local symbol now. */
10714 Elf_Internal_Sym sym = isymbuf[symndx];
10715 asection *sec = flinfo->sections[symndx]->output_section;
10716 const char *name;
10717 long indx;
10718 int ret;
10719
10720 name = bfd_elf_string_from_elf_section (input_bfd,
10721 symtab_hdr->sh_link,
10722 sym.st_name);
10723 if (name == NULL)
10724 return FALSE;
10725
10726 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10727 sec);
10728 if (sym.st_shndx == SHN_BAD)
10729 return FALSE;
10730
10731 sym.st_value += o->output_offset;
10732
10733 indx = bfd_get_symcount (output_bfd);
10734 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10735 NULL);
10736 if (ret == 0)
10737 return FALSE;
10738 else if (ret == 1)
10739 flinfo->indices[symndx] = indx;
10740 else
10741 abort ();
10742 }
10743 elf_section_data (osec)->this_hdr.sh_info
10744 = flinfo->indices[symndx];
10745 }
10746 }
10747
10748 if ((o->flags & SEC_HAS_CONTENTS) == 0
10749 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10750 continue;
10751
10752 if ((o->flags & SEC_LINKER_CREATED) != 0)
10753 {
10754 /* Section was created by _bfd_elf_link_create_dynamic_sections
10755 or somesuch. */
10756 continue;
10757 }
10758
10759 /* Get the contents of the section. They have been cached by a
10760 relaxation routine. Note that o is a section in an input
10761 file, so the contents field will not have been set by any of
10762 the routines which work on output files. */
10763 if (elf_section_data (o)->this_hdr.contents != NULL)
10764 {
10765 contents = elf_section_data (o)->this_hdr.contents;
10766 if (bed->caches_rawsize
10767 && o->rawsize != 0
10768 && o->rawsize < o->size)
10769 {
10770 memcpy (flinfo->contents, contents, o->rawsize);
10771 contents = flinfo->contents;
10772 }
10773 }
10774 else
10775 {
10776 contents = flinfo->contents;
10777 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10778 return FALSE;
10779 }
10780
10781 if ((o->flags & SEC_RELOC) != 0)
10782 {
10783 Elf_Internal_Rela *internal_relocs;
10784 Elf_Internal_Rela *rel, *relend;
10785 int action_discarded;
10786 int ret;
10787
10788 /* Get the swapped relocs. */
10789 internal_relocs
10790 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10791 flinfo->internal_relocs, FALSE);
10792 if (internal_relocs == NULL
10793 && o->reloc_count > 0)
10794 return FALSE;
10795
10796 /* We need to reverse-copy input .ctors/.dtors sections if
10797 they are placed in .init_array/.finit_array for output. */
10798 if (o->size > address_size
10799 && ((strncmp (o->name, ".ctors", 6) == 0
10800 && strcmp (o->output_section->name,
10801 ".init_array") == 0)
10802 || (strncmp (o->name, ".dtors", 6) == 0
10803 && strcmp (o->output_section->name,
10804 ".fini_array") == 0))
10805 && (o->name[6] == 0 || o->name[6] == '.'))
10806 {
10807 if (o->size * bed->s->int_rels_per_ext_rel
10808 != o->reloc_count * address_size)
10809 {
10810 _bfd_error_handler
10811 /* xgettext:c-format */
10812 (_("error: %pB: size of section %pA is not "
10813 "multiple of address size"),
10814 input_bfd, o);
10815 bfd_set_error (bfd_error_bad_value);
10816 return FALSE;
10817 }
10818 o->flags |= SEC_ELF_REVERSE_COPY;
10819 }
10820
10821 action_discarded = -1;
10822 if (!elf_section_ignore_discarded_relocs (o))
10823 action_discarded = (*bed->action_discarded) (o);
10824
10825 /* Run through the relocs evaluating complex reloc symbols and
10826 looking for relocs against symbols from discarded sections
10827 or section symbols from removed link-once sections.
10828 Complain about relocs against discarded sections. Zero
10829 relocs against removed link-once sections. */
10830
10831 rel = internal_relocs;
10832 relend = rel + o->reloc_count;
10833 for ( ; rel < relend; rel++)
10834 {
10835 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10836 unsigned int s_type;
10837 asection **ps, *sec;
10838 struct elf_link_hash_entry *h = NULL;
10839 const char *sym_name;
10840
10841 if (r_symndx == STN_UNDEF)
10842 continue;
10843
10844 if (r_symndx >= locsymcount
10845 || (elf_bad_symtab (input_bfd)
10846 && flinfo->sections[r_symndx] == NULL))
10847 {
10848 h = sym_hashes[r_symndx - extsymoff];
10849
10850 /* Badly formatted input files can contain relocs that
10851 reference non-existant symbols. Check here so that
10852 we do not seg fault. */
10853 if (h == NULL)
10854 {
10855 _bfd_error_handler
10856 /* xgettext:c-format */
10857 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
10858 "that references a non-existent global symbol"),
10859 input_bfd, (uint64_t) rel->r_info, o);
10860 bfd_set_error (bfd_error_bad_value);
10861 return FALSE;
10862 }
10863
10864 while (h->root.type == bfd_link_hash_indirect
10865 || h->root.type == bfd_link_hash_warning)
10866 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10867
10868 s_type = h->type;
10869
10870 /* If a plugin symbol is referenced from a non-IR file,
10871 mark the symbol as undefined. Note that the
10872 linker may attach linker created dynamic sections
10873 to the plugin bfd. Symbols defined in linker
10874 created sections are not plugin symbols. */
10875 if ((h->root.non_ir_ref_regular
10876 || h->root.non_ir_ref_dynamic)
10877 && (h->root.type == bfd_link_hash_defined
10878 || h->root.type == bfd_link_hash_defweak)
10879 && (h->root.u.def.section->flags
10880 & SEC_LINKER_CREATED) == 0
10881 && h->root.u.def.section->owner != NULL
10882 && (h->root.u.def.section->owner->flags
10883 & BFD_PLUGIN) != 0)
10884 {
10885 h->root.type = bfd_link_hash_undefined;
10886 h->root.u.undef.abfd = h->root.u.def.section->owner;
10887 }
10888
10889 ps = NULL;
10890 if (h->root.type == bfd_link_hash_defined
10891 || h->root.type == bfd_link_hash_defweak)
10892 ps = &h->root.u.def.section;
10893
10894 sym_name = h->root.root.string;
10895 }
10896 else
10897 {
10898 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10899
10900 s_type = ELF_ST_TYPE (sym->st_info);
10901 ps = &flinfo->sections[r_symndx];
10902 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10903 sym, *ps);
10904 }
10905
10906 if ((s_type == STT_RELC || s_type == STT_SRELC)
10907 && !bfd_link_relocatable (flinfo->info))
10908 {
10909 bfd_vma val;
10910 bfd_vma dot = (rel->r_offset
10911 + o->output_offset + o->output_section->vma);
10912 #ifdef DEBUG
10913 printf ("Encountered a complex symbol!");
10914 printf (" (input_bfd %s, section %s, reloc %ld\n",
10915 input_bfd->filename, o->name,
10916 (long) (rel - internal_relocs));
10917 printf (" symbol: idx %8.8lx, name %s\n",
10918 r_symndx, sym_name);
10919 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10920 (unsigned long) rel->r_info,
10921 (unsigned long) rel->r_offset);
10922 #endif
10923 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10924 isymbuf, locsymcount, s_type == STT_SRELC))
10925 return FALSE;
10926
10927 /* Symbol evaluated OK. Update to absolute value. */
10928 set_symbol_value (input_bfd, isymbuf, locsymcount,
10929 r_symndx, val);
10930 continue;
10931 }
10932
10933 if (action_discarded != -1 && ps != NULL)
10934 {
10935 /* Complain if the definition comes from a
10936 discarded section. */
10937 if ((sec = *ps) != NULL && discarded_section (sec))
10938 {
10939 BFD_ASSERT (r_symndx != STN_UNDEF);
10940 if (action_discarded & COMPLAIN)
10941 (*flinfo->info->callbacks->einfo)
10942 /* xgettext:c-format */
10943 (_("%X`%s' referenced in section `%pA' of %pB: "
10944 "defined in discarded section `%pA' of %pB\n"),
10945 sym_name, o, input_bfd, sec, sec->owner);
10946
10947 /* Try to do the best we can to support buggy old
10948 versions of gcc. Pretend that the symbol is
10949 really defined in the kept linkonce section.
10950 FIXME: This is quite broken. Modifying the
10951 symbol here means we will be changing all later
10952 uses of the symbol, not just in this section. */
10953 if (action_discarded & PRETEND)
10954 {
10955 asection *kept;
10956
10957 kept = _bfd_elf_check_kept_section (sec,
10958 flinfo->info);
10959 if (kept != NULL)
10960 {
10961 *ps = kept;
10962 continue;
10963 }
10964 }
10965 }
10966 }
10967 }
10968
10969 /* Relocate the section by invoking a back end routine.
10970
10971 The back end routine is responsible for adjusting the
10972 section contents as necessary, and (if using Rela relocs
10973 and generating a relocatable output file) adjusting the
10974 reloc addend as necessary.
10975
10976 The back end routine does not have to worry about setting
10977 the reloc address or the reloc symbol index.
10978
10979 The back end routine is given a pointer to the swapped in
10980 internal symbols, and can access the hash table entries
10981 for the external symbols via elf_sym_hashes (input_bfd).
10982
10983 When generating relocatable output, the back end routine
10984 must handle STB_LOCAL/STT_SECTION symbols specially. The
10985 output symbol is going to be a section symbol
10986 corresponding to the output section, which will require
10987 the addend to be adjusted. */
10988
10989 ret = (*relocate_section) (output_bfd, flinfo->info,
10990 input_bfd, o, contents,
10991 internal_relocs,
10992 isymbuf,
10993 flinfo->sections);
10994 if (!ret)
10995 return FALSE;
10996
10997 if (ret == 2
10998 || bfd_link_relocatable (flinfo->info)
10999 || flinfo->info->emitrelocations)
11000 {
11001 Elf_Internal_Rela *irela;
11002 Elf_Internal_Rela *irelaend, *irelamid;
11003 bfd_vma last_offset;
11004 struct elf_link_hash_entry **rel_hash;
11005 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11006 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
11007 unsigned int next_erel;
11008 bfd_boolean rela_normal;
11009 struct bfd_elf_section_data *esdi, *esdo;
11010
11011 esdi = elf_section_data (o);
11012 esdo = elf_section_data (o->output_section);
11013 rela_normal = FALSE;
11014
11015 /* Adjust the reloc addresses and symbol indices. */
11016
11017 irela = internal_relocs;
11018 irelaend = irela + o->reloc_count;
11019 rel_hash = esdo->rel.hashes + esdo->rel.count;
11020 /* We start processing the REL relocs, if any. When we reach
11021 IRELAMID in the loop, we switch to the RELA relocs. */
11022 irelamid = irela;
11023 if (esdi->rel.hdr != NULL)
11024 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11025 * bed->s->int_rels_per_ext_rel);
11026 rel_hash_list = rel_hash;
11027 rela_hash_list = NULL;
11028 last_offset = o->output_offset;
11029 if (!bfd_link_relocatable (flinfo->info))
11030 last_offset += o->output_section->vma;
11031 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11032 {
11033 unsigned long r_symndx;
11034 asection *sec;
11035 Elf_Internal_Sym sym;
11036
11037 if (next_erel == bed->s->int_rels_per_ext_rel)
11038 {
11039 rel_hash++;
11040 next_erel = 0;
11041 }
11042
11043 if (irela == irelamid)
11044 {
11045 rel_hash = esdo->rela.hashes + esdo->rela.count;
11046 rela_hash_list = rel_hash;
11047 rela_normal = bed->rela_normal;
11048 }
11049
11050 irela->r_offset = _bfd_elf_section_offset (output_bfd,
11051 flinfo->info, o,
11052 irela->r_offset);
11053 if (irela->r_offset >= (bfd_vma) -2)
11054 {
11055 /* This is a reloc for a deleted entry or somesuch.
11056 Turn it into an R_*_NONE reloc, at the same
11057 offset as the last reloc. elf_eh_frame.c and
11058 bfd_elf_discard_info rely on reloc offsets
11059 being ordered. */
11060 irela->r_offset = last_offset;
11061 irela->r_info = 0;
11062 irela->r_addend = 0;
11063 continue;
11064 }
11065
11066 irela->r_offset += o->output_offset;
11067
11068 /* Relocs in an executable have to be virtual addresses. */
11069 if (!bfd_link_relocatable (flinfo->info))
11070 irela->r_offset += o->output_section->vma;
11071
11072 last_offset = irela->r_offset;
11073
11074 r_symndx = irela->r_info >> r_sym_shift;
11075 if (r_symndx == STN_UNDEF)
11076 continue;
11077
11078 if (r_symndx >= locsymcount
11079 || (elf_bad_symtab (input_bfd)
11080 && flinfo->sections[r_symndx] == NULL))
11081 {
11082 struct elf_link_hash_entry *rh;
11083 unsigned long indx;
11084
11085 /* This is a reloc against a global symbol. We
11086 have not yet output all the local symbols, so
11087 we do not know the symbol index of any global
11088 symbol. We set the rel_hash entry for this
11089 reloc to point to the global hash table entry
11090 for this symbol. The symbol index is then
11091 set at the end of bfd_elf_final_link. */
11092 indx = r_symndx - extsymoff;
11093 rh = elf_sym_hashes (input_bfd)[indx];
11094 while (rh->root.type == bfd_link_hash_indirect
11095 || rh->root.type == bfd_link_hash_warning)
11096 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11097
11098 /* Setting the index to -2 tells
11099 elf_link_output_extsym that this symbol is
11100 used by a reloc. */
11101 BFD_ASSERT (rh->indx < 0);
11102 rh->indx = -2;
11103 *rel_hash = rh;
11104
11105 continue;
11106 }
11107
11108 /* This is a reloc against a local symbol. */
11109
11110 *rel_hash = NULL;
11111 sym = isymbuf[r_symndx];
11112 sec = flinfo->sections[r_symndx];
11113 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11114 {
11115 /* I suppose the backend ought to fill in the
11116 section of any STT_SECTION symbol against a
11117 processor specific section. */
11118 r_symndx = STN_UNDEF;
11119 if (bfd_is_abs_section (sec))
11120 ;
11121 else if (sec == NULL || sec->owner == NULL)
11122 {
11123 bfd_set_error (bfd_error_bad_value);
11124 return FALSE;
11125 }
11126 else
11127 {
11128 asection *osec = sec->output_section;
11129
11130 /* If we have discarded a section, the output
11131 section will be the absolute section. In
11132 case of discarded SEC_MERGE sections, use
11133 the kept section. relocate_section should
11134 have already handled discarded linkonce
11135 sections. */
11136 if (bfd_is_abs_section (osec)
11137 && sec->kept_section != NULL
11138 && sec->kept_section->output_section != NULL)
11139 {
11140 osec = sec->kept_section->output_section;
11141 irela->r_addend -= osec->vma;
11142 }
11143
11144 if (!bfd_is_abs_section (osec))
11145 {
11146 r_symndx = osec->target_index;
11147 if (r_symndx == STN_UNDEF)
11148 {
11149 irela->r_addend += osec->vma;
11150 osec = _bfd_nearby_section (output_bfd, osec,
11151 osec->vma);
11152 irela->r_addend -= osec->vma;
11153 r_symndx = osec->target_index;
11154 }
11155 }
11156 }
11157
11158 /* Adjust the addend according to where the
11159 section winds up in the output section. */
11160 if (rela_normal)
11161 irela->r_addend += sec->output_offset;
11162 }
11163 else
11164 {
11165 if (flinfo->indices[r_symndx] == -1)
11166 {
11167 unsigned long shlink;
11168 const char *name;
11169 asection *osec;
11170 long indx;
11171
11172 if (flinfo->info->strip == strip_all)
11173 {
11174 /* You can't do ld -r -s. */
11175 bfd_set_error (bfd_error_invalid_operation);
11176 return FALSE;
11177 }
11178
11179 /* This symbol was skipped earlier, but
11180 since it is needed by a reloc, we
11181 must output it now. */
11182 shlink = symtab_hdr->sh_link;
11183 name = (bfd_elf_string_from_elf_section
11184 (input_bfd, shlink, sym.st_name));
11185 if (name == NULL)
11186 return FALSE;
11187
11188 osec = sec->output_section;
11189 sym.st_shndx =
11190 _bfd_elf_section_from_bfd_section (output_bfd,
11191 osec);
11192 if (sym.st_shndx == SHN_BAD)
11193 return FALSE;
11194
11195 sym.st_value += sec->output_offset;
11196 if (!bfd_link_relocatable (flinfo->info))
11197 {
11198 sym.st_value += osec->vma;
11199 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11200 {
11201 struct elf_link_hash_table *htab
11202 = elf_hash_table (flinfo->info);
11203
11204 /* STT_TLS symbols are relative to PT_TLS
11205 segment base. */
11206 if (htab->tls_sec != NULL)
11207 sym.st_value -= htab->tls_sec->vma;
11208 else
11209 sym.st_info
11210 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11211 STT_NOTYPE);
11212 }
11213 }
11214
11215 indx = bfd_get_symcount (output_bfd);
11216 ret = elf_link_output_symstrtab (flinfo, name,
11217 &sym, sec,
11218 NULL);
11219 if (ret == 0)
11220 return FALSE;
11221 else if (ret == 1)
11222 flinfo->indices[r_symndx] = indx;
11223 else
11224 abort ();
11225 }
11226
11227 r_symndx = flinfo->indices[r_symndx];
11228 }
11229
11230 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11231 | (irela->r_info & r_type_mask));
11232 }
11233
11234 /* Swap out the relocs. */
11235 input_rel_hdr = esdi->rel.hdr;
11236 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
11237 {
11238 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11239 input_rel_hdr,
11240 internal_relocs,
11241 rel_hash_list))
11242 return FALSE;
11243 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11244 * bed->s->int_rels_per_ext_rel);
11245 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
11246 }
11247
11248 input_rela_hdr = esdi->rela.hdr;
11249 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11250 {
11251 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11252 input_rela_hdr,
11253 internal_relocs,
11254 rela_hash_list))
11255 return FALSE;
11256 }
11257 }
11258 }
11259
11260 /* Write out the modified section contents. */
11261 if (bed->elf_backend_write_section
11262 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
11263 contents))
11264 {
11265 /* Section written out. */
11266 }
11267 else switch (o->sec_info_type)
11268 {
11269 case SEC_INFO_TYPE_STABS:
11270 if (! (_bfd_write_section_stabs
11271 (output_bfd,
11272 &elf_hash_table (flinfo->info)->stab_info,
11273 o, &elf_section_data (o)->sec_info, contents)))
11274 return FALSE;
11275 break;
11276 case SEC_INFO_TYPE_MERGE:
11277 if (! _bfd_write_merged_section (output_bfd, o,
11278 elf_section_data (o)->sec_info))
11279 return FALSE;
11280 break;
11281 case SEC_INFO_TYPE_EH_FRAME:
11282 {
11283 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11284 o, contents))
11285 return FALSE;
11286 }
11287 break;
11288 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11289 {
11290 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11291 flinfo->info,
11292 o, contents))
11293 return FALSE;
11294 }
11295 break;
11296 default:
11297 {
11298 if (! (o->flags & SEC_EXCLUDE))
11299 {
11300 file_ptr offset = (file_ptr) o->output_offset;
11301 bfd_size_type todo = o->size;
11302
11303 offset *= bfd_octets_per_byte (output_bfd, o);
11304
11305 if ((o->flags & SEC_ELF_REVERSE_COPY))
11306 {
11307 /* Reverse-copy input section to output. */
11308 do
11309 {
11310 todo -= address_size;
11311 if (! bfd_set_section_contents (output_bfd,
11312 o->output_section,
11313 contents + todo,
11314 offset,
11315 address_size))
11316 return FALSE;
11317 if (todo == 0)
11318 break;
11319 offset += address_size;
11320 }
11321 while (1);
11322 }
11323 else if (! bfd_set_section_contents (output_bfd,
11324 o->output_section,
11325 contents,
11326 offset, todo))
11327 return FALSE;
11328 }
11329 }
11330 break;
11331 }
11332 }
11333
11334 return TRUE;
11335 }
11336
11337 /* Generate a reloc when linking an ELF file. This is a reloc
11338 requested by the linker, and does not come from any input file. This
11339 is used to build constructor and destructor tables when linking
11340 with -Ur. */
11341
11342 static bfd_boolean
11343 elf_reloc_link_order (bfd *output_bfd,
11344 struct bfd_link_info *info,
11345 asection *output_section,
11346 struct bfd_link_order *link_order)
11347 {
11348 reloc_howto_type *howto;
11349 long indx;
11350 bfd_vma offset;
11351 bfd_vma addend;
11352 struct bfd_elf_section_reloc_data *reldata;
11353 struct elf_link_hash_entry **rel_hash_ptr;
11354 Elf_Internal_Shdr *rel_hdr;
11355 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11356 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11357 bfd_byte *erel;
11358 unsigned int i;
11359 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11360
11361 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11362 if (howto == NULL)
11363 {
11364 bfd_set_error (bfd_error_bad_value);
11365 return FALSE;
11366 }
11367
11368 addend = link_order->u.reloc.p->addend;
11369
11370 if (esdo->rel.hdr)
11371 reldata = &esdo->rel;
11372 else if (esdo->rela.hdr)
11373 reldata = &esdo->rela;
11374 else
11375 {
11376 reldata = NULL;
11377 BFD_ASSERT (0);
11378 }
11379
11380 /* Figure out the symbol index. */
11381 rel_hash_ptr = reldata->hashes + reldata->count;
11382 if (link_order->type == bfd_section_reloc_link_order)
11383 {
11384 indx = link_order->u.reloc.p->u.section->target_index;
11385 BFD_ASSERT (indx != 0);
11386 *rel_hash_ptr = NULL;
11387 }
11388 else
11389 {
11390 struct elf_link_hash_entry *h;
11391
11392 /* Treat a reloc against a defined symbol as though it were
11393 actually against the section. */
11394 h = ((struct elf_link_hash_entry *)
11395 bfd_wrapped_link_hash_lookup (output_bfd, info,
11396 link_order->u.reloc.p->u.name,
11397 FALSE, FALSE, TRUE));
11398 if (h != NULL
11399 && (h->root.type == bfd_link_hash_defined
11400 || h->root.type == bfd_link_hash_defweak))
11401 {
11402 asection *section;
11403
11404 section = h->root.u.def.section;
11405 indx = section->output_section->target_index;
11406 *rel_hash_ptr = NULL;
11407 /* It seems that we ought to add the symbol value to the
11408 addend here, but in practice it has already been added
11409 because it was passed to constructor_callback. */
11410 addend += section->output_section->vma + section->output_offset;
11411 }
11412 else if (h != NULL)
11413 {
11414 /* Setting the index to -2 tells elf_link_output_extsym that
11415 this symbol is used by a reloc. */
11416 h->indx = -2;
11417 *rel_hash_ptr = h;
11418 indx = 0;
11419 }
11420 else
11421 {
11422 (*info->callbacks->unattached_reloc)
11423 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11424 indx = 0;
11425 }
11426 }
11427
11428 /* If this is an inplace reloc, we must write the addend into the
11429 object file. */
11430 if (howto->partial_inplace && addend != 0)
11431 {
11432 bfd_size_type size;
11433 bfd_reloc_status_type rstat;
11434 bfd_byte *buf;
11435 bfd_boolean ok;
11436 const char *sym_name;
11437 bfd_size_type octets;
11438
11439 size = (bfd_size_type) bfd_get_reloc_size (howto);
11440 buf = (bfd_byte *) bfd_zmalloc (size);
11441 if (buf == NULL && size != 0)
11442 return FALSE;
11443 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11444 switch (rstat)
11445 {
11446 case bfd_reloc_ok:
11447 break;
11448
11449 default:
11450 case bfd_reloc_outofrange:
11451 abort ();
11452
11453 case bfd_reloc_overflow:
11454 if (link_order->type == bfd_section_reloc_link_order)
11455 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
11456 else
11457 sym_name = link_order->u.reloc.p->u.name;
11458 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11459 howto->name, addend, NULL, NULL,
11460 (bfd_vma) 0);
11461 break;
11462 }
11463
11464 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
11465 output_section);
11466 ok = bfd_set_section_contents (output_bfd, output_section, buf,
11467 octets, size);
11468 free (buf);
11469 if (! ok)
11470 return FALSE;
11471 }
11472
11473 /* The address of a reloc is relative to the section in a
11474 relocatable file, and is a virtual address in an executable
11475 file. */
11476 offset = link_order->offset;
11477 if (! bfd_link_relocatable (info))
11478 offset += output_section->vma;
11479
11480 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11481 {
11482 irel[i].r_offset = offset;
11483 irel[i].r_info = 0;
11484 irel[i].r_addend = 0;
11485 }
11486 if (bed->s->arch_size == 32)
11487 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11488 else
11489 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11490
11491 rel_hdr = reldata->hdr;
11492 erel = rel_hdr->contents;
11493 if (rel_hdr->sh_type == SHT_REL)
11494 {
11495 erel += reldata->count * bed->s->sizeof_rel;
11496 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11497 }
11498 else
11499 {
11500 irel[0].r_addend = addend;
11501 erel += reldata->count * bed->s->sizeof_rela;
11502 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11503 }
11504
11505 ++reldata->count;
11506
11507 return TRUE;
11508 }
11509
11510
11511 /* Compare two sections based on the locations of the sections they are
11512 linked to. Used by elf_fixup_link_order. */
11513
11514 static int
11515 compare_link_order (const void *a, const void *b)
11516 {
11517 const struct bfd_link_order *alo = *(const struct bfd_link_order **) a;
11518 const struct bfd_link_order *blo = *(const struct bfd_link_order **) b;
11519 asection *asec = elf_linked_to_section (alo->u.indirect.section);
11520 asection *bsec = elf_linked_to_section (blo->u.indirect.section);
11521 bfd_vma apos = asec->output_section->lma + asec->output_offset;
11522 bfd_vma bpos = bsec->output_section->lma + bsec->output_offset;
11523
11524 if (apos < bpos)
11525 return -1;
11526 if (apos > bpos)
11527 return 1;
11528
11529 /* The only way we should get matching LMAs is when the first of two
11530 sections has zero size. */
11531 if (asec->size < bsec->size)
11532 return -1;
11533 if (asec->size > bsec->size)
11534 return 1;
11535
11536 /* If they are both zero size then they almost certainly have the same
11537 VMA and thus are not ordered with respect to each other. Test VMA
11538 anyway, and fall back to id to make the result reproducible across
11539 qsort implementations. */
11540 apos = asec->output_section->vma + asec->output_offset;
11541 bpos = bsec->output_section->vma + bsec->output_offset;
11542 if (apos < bpos)
11543 return -1;
11544 if (apos > bpos)
11545 return 1;
11546
11547 return asec->id - bsec->id;
11548 }
11549
11550
11551 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11552 order as their linked sections. Returns false if this could not be done
11553 because an output section includes both ordered and unordered
11554 sections. Ideally we'd do this in the linker proper. */
11555
11556 static bfd_boolean
11557 elf_fixup_link_order (bfd *abfd, asection *o)
11558 {
11559 size_t seen_linkorder;
11560 size_t seen_other;
11561 size_t n;
11562 struct bfd_link_order *p;
11563 bfd *sub;
11564 struct bfd_link_order **sections;
11565 asection *s, *other_sec, *linkorder_sec;
11566 bfd_vma offset;
11567
11568 other_sec = NULL;
11569 linkorder_sec = NULL;
11570 seen_other = 0;
11571 seen_linkorder = 0;
11572 for (p = o->map_head.link_order; p != NULL; p = p->next)
11573 {
11574 if (p->type == bfd_indirect_link_order)
11575 {
11576 s = p->u.indirect.section;
11577 sub = s->owner;
11578 if ((s->flags & SEC_LINKER_CREATED) == 0
11579 && bfd_get_flavour (sub) == bfd_target_elf_flavour
11580 && elf_section_data (s) != NULL
11581 && elf_linked_to_section (s) != NULL)
11582 {
11583 seen_linkorder++;
11584 linkorder_sec = s;
11585 }
11586 else
11587 {
11588 seen_other++;
11589 other_sec = s;
11590 }
11591 }
11592 else
11593 seen_other++;
11594
11595 if (seen_other && seen_linkorder)
11596 {
11597 if (other_sec && linkorder_sec)
11598 _bfd_error_handler
11599 /* xgettext:c-format */
11600 (_("%pA has both ordered [`%pA' in %pB] "
11601 "and unordered [`%pA' in %pB] sections"),
11602 o, linkorder_sec, linkorder_sec->owner,
11603 other_sec, other_sec->owner);
11604 else
11605 _bfd_error_handler
11606 (_("%pA has both ordered and unordered sections"), o);
11607 bfd_set_error (bfd_error_bad_value);
11608 return FALSE;
11609 }
11610 }
11611
11612 if (!seen_linkorder)
11613 return TRUE;
11614
11615 sections = bfd_malloc (seen_linkorder * sizeof (*sections));
11616 if (sections == NULL)
11617 return FALSE;
11618
11619 seen_linkorder = 0;
11620 for (p = o->map_head.link_order; p != NULL; p = p->next)
11621 sections[seen_linkorder++] = p;
11622
11623 /* Sort the input sections in the order of their linked section. */
11624 qsort (sections, seen_linkorder, sizeof (*sections), compare_link_order);
11625
11626 /* Change the offsets of the sections. */
11627 offset = 0;
11628 for (n = 0; n < seen_linkorder; n++)
11629 {
11630 bfd_vma mask;
11631 s = sections[n]->u.indirect.section;
11632 mask = ~(bfd_vma) 0 << s->alignment_power;
11633 offset = (offset + ~mask) & mask;
11634 s->output_offset = offset / bfd_octets_per_byte (abfd, s);
11635 sections[n]->offset = offset;
11636 offset += sections[n]->size;
11637 }
11638
11639 free (sections);
11640 return TRUE;
11641 }
11642
11643 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11644 Returns TRUE upon success, FALSE otherwise. */
11645
11646 static bfd_boolean
11647 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11648 {
11649 bfd_boolean ret = FALSE;
11650 bfd *implib_bfd;
11651 const struct elf_backend_data *bed;
11652 flagword flags;
11653 enum bfd_architecture arch;
11654 unsigned int mach;
11655 asymbol **sympp = NULL;
11656 long symsize;
11657 long symcount;
11658 long src_count;
11659 elf_symbol_type *osymbuf;
11660
11661 implib_bfd = info->out_implib_bfd;
11662 bed = get_elf_backend_data (abfd);
11663
11664 if (!bfd_set_format (implib_bfd, bfd_object))
11665 return FALSE;
11666
11667 /* Use flag from executable but make it a relocatable object. */
11668 flags = bfd_get_file_flags (abfd);
11669 flags &= ~HAS_RELOC;
11670 if (!bfd_set_start_address (implib_bfd, 0)
11671 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
11672 return FALSE;
11673
11674 /* Copy architecture of output file to import library file. */
11675 arch = bfd_get_arch (abfd);
11676 mach = bfd_get_mach (abfd);
11677 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11678 && (abfd->target_defaulted
11679 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11680 return FALSE;
11681
11682 /* Get symbol table size. */
11683 symsize = bfd_get_symtab_upper_bound (abfd);
11684 if (symsize < 0)
11685 return FALSE;
11686
11687 /* Read in the symbol table. */
11688 sympp = (asymbol **) bfd_malloc (symsize);
11689 if (sympp == NULL)
11690 return FALSE;
11691
11692 symcount = bfd_canonicalize_symtab (abfd, sympp);
11693 if (symcount < 0)
11694 goto free_sym_buf;
11695
11696 /* Allow the BFD backend to copy any private header data it
11697 understands from the output BFD to the import library BFD. */
11698 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11699 goto free_sym_buf;
11700
11701 /* Filter symbols to appear in the import library. */
11702 if (bed->elf_backend_filter_implib_symbols)
11703 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11704 symcount);
11705 else
11706 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11707 if (symcount == 0)
11708 {
11709 bfd_set_error (bfd_error_no_symbols);
11710 _bfd_error_handler (_("%pB: no symbol found for import library"),
11711 implib_bfd);
11712 goto free_sym_buf;
11713 }
11714
11715
11716 /* Make symbols absolute. */
11717 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11718 sizeof (*osymbuf));
11719 if (osymbuf == NULL)
11720 goto free_sym_buf;
11721
11722 for (src_count = 0; src_count < symcount; src_count++)
11723 {
11724 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11725 sizeof (*osymbuf));
11726 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11727 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11728 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11729 osymbuf[src_count].internal_elf_sym.st_value =
11730 osymbuf[src_count].symbol.value;
11731 sympp[src_count] = &osymbuf[src_count].symbol;
11732 }
11733
11734 bfd_set_symtab (implib_bfd, sympp, symcount);
11735
11736 /* Allow the BFD backend to copy any private data it understands
11737 from the output BFD to the import library BFD. This is done last
11738 to permit the routine to look at the filtered symbol table. */
11739 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11740 goto free_sym_buf;
11741
11742 if (!bfd_close (implib_bfd))
11743 goto free_sym_buf;
11744
11745 ret = TRUE;
11746
11747 free_sym_buf:
11748 free (sympp);
11749 return ret;
11750 }
11751
11752 static void
11753 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11754 {
11755 asection *o;
11756
11757 if (flinfo->symstrtab != NULL)
11758 _bfd_elf_strtab_free (flinfo->symstrtab);
11759 if (flinfo->contents != NULL)
11760 free (flinfo->contents);
11761 if (flinfo->external_relocs != NULL)
11762 free (flinfo->external_relocs);
11763 if (flinfo->internal_relocs != NULL)
11764 free (flinfo->internal_relocs);
11765 if (flinfo->external_syms != NULL)
11766 free (flinfo->external_syms);
11767 if (flinfo->locsym_shndx != NULL)
11768 free (flinfo->locsym_shndx);
11769 if (flinfo->internal_syms != NULL)
11770 free (flinfo->internal_syms);
11771 if (flinfo->indices != NULL)
11772 free (flinfo->indices);
11773 if (flinfo->sections != NULL)
11774 free (flinfo->sections);
11775 if (flinfo->symshndxbuf != NULL
11776 && flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
11777 free (flinfo->symshndxbuf);
11778 for (o = obfd->sections; o != NULL; o = o->next)
11779 {
11780 struct bfd_elf_section_data *esdo = elf_section_data (o);
11781 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11782 free (esdo->rel.hashes);
11783 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11784 free (esdo->rela.hashes);
11785 }
11786 }
11787
11788 /* Do the final step of an ELF link. */
11789
11790 bfd_boolean
11791 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11792 {
11793 bfd_boolean dynamic;
11794 bfd_boolean emit_relocs;
11795 bfd *dynobj;
11796 struct elf_final_link_info flinfo;
11797 asection *o;
11798 struct bfd_link_order *p;
11799 bfd *sub;
11800 bfd_size_type max_contents_size;
11801 bfd_size_type max_external_reloc_size;
11802 bfd_size_type max_internal_reloc_count;
11803 bfd_size_type max_sym_count;
11804 bfd_size_type max_sym_shndx_count;
11805 Elf_Internal_Sym elfsym;
11806 unsigned int i;
11807 Elf_Internal_Shdr *symtab_hdr;
11808 Elf_Internal_Shdr *symtab_shndx_hdr;
11809 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11810 struct elf_outext_info eoinfo;
11811 bfd_boolean merged;
11812 size_t relativecount = 0;
11813 asection *reldyn = 0;
11814 bfd_size_type amt;
11815 asection *attr_section = NULL;
11816 bfd_vma attr_size = 0;
11817 const char *std_attrs_section;
11818 struct elf_link_hash_table *htab = elf_hash_table (info);
11819 bfd_boolean sections_removed;
11820
11821 if (!is_elf_hash_table (htab))
11822 return FALSE;
11823
11824 if (bfd_link_pic (info))
11825 abfd->flags |= DYNAMIC;
11826
11827 dynamic = htab->dynamic_sections_created;
11828 dynobj = htab->dynobj;
11829
11830 emit_relocs = (bfd_link_relocatable (info)
11831 || info->emitrelocations);
11832
11833 flinfo.info = info;
11834 flinfo.output_bfd = abfd;
11835 flinfo.symstrtab = _bfd_elf_strtab_init ();
11836 if (flinfo.symstrtab == NULL)
11837 return FALSE;
11838
11839 if (! dynamic)
11840 {
11841 flinfo.hash_sec = NULL;
11842 flinfo.symver_sec = NULL;
11843 }
11844 else
11845 {
11846 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11847 /* Note that dynsym_sec can be NULL (on VMS). */
11848 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11849 /* Note that it is OK if symver_sec is NULL. */
11850 }
11851
11852 flinfo.contents = NULL;
11853 flinfo.external_relocs = NULL;
11854 flinfo.internal_relocs = NULL;
11855 flinfo.external_syms = NULL;
11856 flinfo.locsym_shndx = NULL;
11857 flinfo.internal_syms = NULL;
11858 flinfo.indices = NULL;
11859 flinfo.sections = NULL;
11860 flinfo.symshndxbuf = NULL;
11861 flinfo.filesym_count = 0;
11862
11863 /* The object attributes have been merged. Remove the input
11864 sections from the link, and set the contents of the output
11865 section. */
11866 sections_removed = FALSE;
11867 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11868 for (o = abfd->sections; o != NULL; o = o->next)
11869 {
11870 bfd_boolean remove_section = FALSE;
11871
11872 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11873 || strcmp (o->name, ".gnu.attributes") == 0)
11874 {
11875 for (p = o->map_head.link_order; p != NULL; p = p->next)
11876 {
11877 asection *input_section;
11878
11879 if (p->type != bfd_indirect_link_order)
11880 continue;
11881 input_section = p->u.indirect.section;
11882 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11883 elf_link_input_bfd ignores this section. */
11884 input_section->flags &= ~SEC_HAS_CONTENTS;
11885 }
11886
11887 attr_size = bfd_elf_obj_attr_size (abfd);
11888 bfd_set_section_size (o, attr_size);
11889 /* Skip this section later on. */
11890 o->map_head.link_order = NULL;
11891 if (attr_size)
11892 attr_section = o;
11893 else
11894 remove_section = TRUE;
11895 }
11896 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
11897 {
11898 /* Remove empty group section from linker output. */
11899 remove_section = TRUE;
11900 }
11901 if (remove_section)
11902 {
11903 o->flags |= SEC_EXCLUDE;
11904 bfd_section_list_remove (abfd, o);
11905 abfd->section_count--;
11906 sections_removed = TRUE;
11907 }
11908 }
11909 if (sections_removed)
11910 _bfd_fix_excluded_sec_syms (abfd, info);
11911
11912 /* Count up the number of relocations we will output for each output
11913 section, so that we know the sizes of the reloc sections. We
11914 also figure out some maximum sizes. */
11915 max_contents_size = 0;
11916 max_external_reloc_size = 0;
11917 max_internal_reloc_count = 0;
11918 max_sym_count = 0;
11919 max_sym_shndx_count = 0;
11920 merged = FALSE;
11921 for (o = abfd->sections; o != NULL; o = o->next)
11922 {
11923 struct bfd_elf_section_data *esdo = elf_section_data (o);
11924 o->reloc_count = 0;
11925
11926 for (p = o->map_head.link_order; p != NULL; p = p->next)
11927 {
11928 unsigned int reloc_count = 0;
11929 unsigned int additional_reloc_count = 0;
11930 struct bfd_elf_section_data *esdi = NULL;
11931
11932 if (p->type == bfd_section_reloc_link_order
11933 || p->type == bfd_symbol_reloc_link_order)
11934 reloc_count = 1;
11935 else if (p->type == bfd_indirect_link_order)
11936 {
11937 asection *sec;
11938
11939 sec = p->u.indirect.section;
11940
11941 /* Mark all sections which are to be included in the
11942 link. This will normally be every section. We need
11943 to do this so that we can identify any sections which
11944 the linker has decided to not include. */
11945 sec->linker_mark = TRUE;
11946
11947 if (sec->flags & SEC_MERGE)
11948 merged = TRUE;
11949
11950 if (sec->rawsize > max_contents_size)
11951 max_contents_size = sec->rawsize;
11952 if (sec->size > max_contents_size)
11953 max_contents_size = sec->size;
11954
11955 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11956 && (sec->owner->flags & DYNAMIC) == 0)
11957 {
11958 size_t sym_count;
11959
11960 /* We are interested in just local symbols, not all
11961 symbols. */
11962 if (elf_bad_symtab (sec->owner))
11963 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11964 / bed->s->sizeof_sym);
11965 else
11966 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11967
11968 if (sym_count > max_sym_count)
11969 max_sym_count = sym_count;
11970
11971 if (sym_count > max_sym_shndx_count
11972 && elf_symtab_shndx_list (sec->owner) != NULL)
11973 max_sym_shndx_count = sym_count;
11974
11975 if (esdo->this_hdr.sh_type == SHT_REL
11976 || esdo->this_hdr.sh_type == SHT_RELA)
11977 /* Some backends use reloc_count in relocation sections
11978 to count particular types of relocs. Of course,
11979 reloc sections themselves can't have relocations. */
11980 ;
11981 else if (emit_relocs)
11982 {
11983 reloc_count = sec->reloc_count;
11984 if (bed->elf_backend_count_additional_relocs)
11985 {
11986 int c;
11987 c = (*bed->elf_backend_count_additional_relocs) (sec);
11988 additional_reloc_count += c;
11989 }
11990 }
11991 else if (bed->elf_backend_count_relocs)
11992 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11993
11994 esdi = elf_section_data (sec);
11995
11996 if ((sec->flags & SEC_RELOC) != 0)
11997 {
11998 size_t ext_size = 0;
11999
12000 if (esdi->rel.hdr != NULL)
12001 ext_size = esdi->rel.hdr->sh_size;
12002 if (esdi->rela.hdr != NULL)
12003 ext_size += esdi->rela.hdr->sh_size;
12004
12005 if (ext_size > max_external_reloc_size)
12006 max_external_reloc_size = ext_size;
12007 if (sec->reloc_count > max_internal_reloc_count)
12008 max_internal_reloc_count = sec->reloc_count;
12009 }
12010 }
12011 }
12012
12013 if (reloc_count == 0)
12014 continue;
12015
12016 reloc_count += additional_reloc_count;
12017 o->reloc_count += reloc_count;
12018
12019 if (p->type == bfd_indirect_link_order && emit_relocs)
12020 {
12021 if (esdi->rel.hdr)
12022 {
12023 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
12024 esdo->rel.count += additional_reloc_count;
12025 }
12026 if (esdi->rela.hdr)
12027 {
12028 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
12029 esdo->rela.count += additional_reloc_count;
12030 }
12031 }
12032 else
12033 {
12034 if (o->use_rela_p)
12035 esdo->rela.count += reloc_count;
12036 else
12037 esdo->rel.count += reloc_count;
12038 }
12039 }
12040
12041 if (o->reloc_count > 0)
12042 o->flags |= SEC_RELOC;
12043 else
12044 {
12045 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12046 set it (this is probably a bug) and if it is set
12047 assign_section_numbers will create a reloc section. */
12048 o->flags &=~ SEC_RELOC;
12049 }
12050
12051 /* If the SEC_ALLOC flag is not set, force the section VMA to
12052 zero. This is done in elf_fake_sections as well, but forcing
12053 the VMA to 0 here will ensure that relocs against these
12054 sections are handled correctly. */
12055 if ((o->flags & SEC_ALLOC) == 0
12056 && ! o->user_set_vma)
12057 o->vma = 0;
12058 }
12059
12060 if (! bfd_link_relocatable (info) && merged)
12061 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
12062
12063 /* Figure out the file positions for everything but the symbol table
12064 and the relocs. We set symcount to force assign_section_numbers
12065 to create a symbol table. */
12066 abfd->symcount = info->strip != strip_all || emit_relocs;
12067 BFD_ASSERT (! abfd->output_has_begun);
12068 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12069 goto error_return;
12070
12071 /* Set sizes, and assign file positions for reloc sections. */
12072 for (o = abfd->sections; o != NULL; o = o->next)
12073 {
12074 struct bfd_elf_section_data *esdo = elf_section_data (o);
12075 if ((o->flags & SEC_RELOC) != 0)
12076 {
12077 if (esdo->rel.hdr
12078 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
12079 goto error_return;
12080
12081 if (esdo->rela.hdr
12082 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
12083 goto error_return;
12084 }
12085
12086 /* _bfd_elf_compute_section_file_positions makes temporary use
12087 of target_index. Reset it. */
12088 o->target_index = 0;
12089
12090 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12091 to count upwards while actually outputting the relocations. */
12092 esdo->rel.count = 0;
12093 esdo->rela.count = 0;
12094
12095 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12096 && !bfd_section_is_ctf (o))
12097 {
12098 /* Cache the section contents so that they can be compressed
12099 later. Use bfd_malloc since it will be freed by
12100 bfd_compress_section_contents. */
12101 unsigned char *contents = esdo->this_hdr.contents;
12102 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
12103 abort ();
12104 contents
12105 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12106 if (contents == NULL)
12107 goto error_return;
12108 esdo->this_hdr.contents = contents;
12109 }
12110 }
12111
12112 /* We have now assigned file positions for all the sections except .symtab,
12113 .strtab, and non-loaded reloc and compressed debugging sections. We start
12114 the .symtab section at the current file position, and write directly to it.
12115 We build the .strtab section in memory. */
12116 abfd->symcount = 0;
12117 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12118 /* sh_name is set in prep_headers. */
12119 symtab_hdr->sh_type = SHT_SYMTAB;
12120 /* sh_flags, sh_addr and sh_size all start off zero. */
12121 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12122 /* sh_link is set in assign_section_numbers. */
12123 /* sh_info is set below. */
12124 /* sh_offset is set just below. */
12125 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
12126
12127 if (max_sym_count < 20)
12128 max_sym_count = 20;
12129 htab->strtabsize = max_sym_count;
12130 amt = max_sym_count * sizeof (struct elf_sym_strtab);
12131 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12132 if (htab->strtab == NULL)
12133 goto error_return;
12134 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12135 flinfo.symshndxbuf
12136 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12137 ? (Elf_External_Sym_Shndx *) -1 : NULL);
12138
12139 if (info->strip != strip_all || emit_relocs)
12140 {
12141 file_ptr off = elf_next_file_pos (abfd);
12142
12143 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
12144
12145 /* Note that at this point elf_next_file_pos (abfd) is
12146 incorrect. We do not yet know the size of the .symtab section.
12147 We correct next_file_pos below, after we do know the size. */
12148
12149 /* Start writing out the symbol table. The first symbol is always a
12150 dummy symbol. */
12151 elfsym.st_value = 0;
12152 elfsym.st_size = 0;
12153 elfsym.st_info = 0;
12154 elfsym.st_other = 0;
12155 elfsym.st_shndx = SHN_UNDEF;
12156 elfsym.st_target_internal = 0;
12157 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12158 bfd_und_section_ptr, NULL) != 1)
12159 goto error_return;
12160
12161 /* Output a symbol for each section. We output these even if we are
12162 discarding local symbols, since they are used for relocs. These
12163 symbols have no names. We store the index of each one in the
12164 index field of the section, so that we can find it again when
12165 outputting relocs. */
12166
12167 elfsym.st_size = 0;
12168 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12169 elfsym.st_other = 0;
12170 elfsym.st_value = 0;
12171 elfsym.st_target_internal = 0;
12172 for (i = 1; i < elf_numsections (abfd); i++)
12173 {
12174 o = bfd_section_from_elf_index (abfd, i);
12175 if (o != NULL)
12176 {
12177 o->target_index = bfd_get_symcount (abfd);
12178 elfsym.st_shndx = i;
12179 if (!bfd_link_relocatable (info))
12180 elfsym.st_value = o->vma;
12181 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
12182 NULL) != 1)
12183 goto error_return;
12184 }
12185 }
12186 }
12187
12188 /* Allocate some memory to hold information read in from the input
12189 files. */
12190 if (max_contents_size != 0)
12191 {
12192 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12193 if (flinfo.contents == NULL)
12194 goto error_return;
12195 }
12196
12197 if (max_external_reloc_size != 0)
12198 {
12199 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12200 if (flinfo.external_relocs == NULL)
12201 goto error_return;
12202 }
12203
12204 if (max_internal_reloc_count != 0)
12205 {
12206 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12207 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12208 if (flinfo.internal_relocs == NULL)
12209 goto error_return;
12210 }
12211
12212 if (max_sym_count != 0)
12213 {
12214 amt = max_sym_count * bed->s->sizeof_sym;
12215 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12216 if (flinfo.external_syms == NULL)
12217 goto error_return;
12218
12219 amt = max_sym_count * sizeof (Elf_Internal_Sym);
12220 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12221 if (flinfo.internal_syms == NULL)
12222 goto error_return;
12223
12224 amt = max_sym_count * sizeof (long);
12225 flinfo.indices = (long int *) bfd_malloc (amt);
12226 if (flinfo.indices == NULL)
12227 goto error_return;
12228
12229 amt = max_sym_count * sizeof (asection *);
12230 flinfo.sections = (asection **) bfd_malloc (amt);
12231 if (flinfo.sections == NULL)
12232 goto error_return;
12233 }
12234
12235 if (max_sym_shndx_count != 0)
12236 {
12237 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
12238 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12239 if (flinfo.locsym_shndx == NULL)
12240 goto error_return;
12241 }
12242
12243 if (htab->tls_sec)
12244 {
12245 bfd_vma base, end = 0;
12246 asection *sec;
12247
12248 for (sec = htab->tls_sec;
12249 sec && (sec->flags & SEC_THREAD_LOCAL);
12250 sec = sec->next)
12251 {
12252 bfd_size_type size = sec->size;
12253
12254 if (size == 0
12255 && (sec->flags & SEC_HAS_CONTENTS) == 0)
12256 {
12257 struct bfd_link_order *ord = sec->map_tail.link_order;
12258
12259 if (ord != NULL)
12260 size = ord->offset + ord->size;
12261 }
12262 end = sec->vma + size;
12263 }
12264 base = htab->tls_sec->vma;
12265 /* Only align end of TLS section if static TLS doesn't have special
12266 alignment requirements. */
12267 if (bed->static_tls_alignment == 1)
12268 end = align_power (end, htab->tls_sec->alignment_power);
12269 htab->tls_size = end - base;
12270 }
12271
12272 /* Reorder SHF_LINK_ORDER sections. */
12273 for (o = abfd->sections; o != NULL; o = o->next)
12274 {
12275 if (!elf_fixup_link_order (abfd, o))
12276 return FALSE;
12277 }
12278
12279 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12280 return FALSE;
12281
12282 /* Since ELF permits relocations to be against local symbols, we
12283 must have the local symbols available when we do the relocations.
12284 Since we would rather only read the local symbols once, and we
12285 would rather not keep them in memory, we handle all the
12286 relocations for a single input file at the same time.
12287
12288 Unfortunately, there is no way to know the total number of local
12289 symbols until we have seen all of them, and the local symbol
12290 indices precede the global symbol indices. This means that when
12291 we are generating relocatable output, and we see a reloc against
12292 a global symbol, we can not know the symbol index until we have
12293 finished examining all the local symbols to see which ones we are
12294 going to output. To deal with this, we keep the relocations in
12295 memory, and don't output them until the end of the link. This is
12296 an unfortunate waste of memory, but I don't see a good way around
12297 it. Fortunately, it only happens when performing a relocatable
12298 link, which is not the common case. FIXME: If keep_memory is set
12299 we could write the relocs out and then read them again; I don't
12300 know how bad the memory loss will be. */
12301
12302 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12303 sub->output_has_begun = FALSE;
12304 for (o = abfd->sections; o != NULL; o = o->next)
12305 {
12306 for (p = o->map_head.link_order; p != NULL; p = p->next)
12307 {
12308 if (p->type == bfd_indirect_link_order
12309 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12310 == bfd_target_elf_flavour)
12311 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12312 {
12313 if (! sub->output_has_begun)
12314 {
12315 if (! elf_link_input_bfd (&flinfo, sub))
12316 goto error_return;
12317 sub->output_has_begun = TRUE;
12318 }
12319 }
12320 else if (p->type == bfd_section_reloc_link_order
12321 || p->type == bfd_symbol_reloc_link_order)
12322 {
12323 if (! elf_reloc_link_order (abfd, info, o, p))
12324 goto error_return;
12325 }
12326 else
12327 {
12328 if (! _bfd_default_link_order (abfd, info, o, p))
12329 {
12330 if (p->type == bfd_indirect_link_order
12331 && (bfd_get_flavour (sub)
12332 == bfd_target_elf_flavour)
12333 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12334 != bed->s->elfclass))
12335 {
12336 const char *iclass, *oclass;
12337
12338 switch (bed->s->elfclass)
12339 {
12340 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12341 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12342 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12343 default: abort ();
12344 }
12345
12346 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12347 {
12348 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12349 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12350 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12351 default: abort ();
12352 }
12353
12354 bfd_set_error (bfd_error_wrong_format);
12355 _bfd_error_handler
12356 /* xgettext:c-format */
12357 (_("%pB: file class %s incompatible with %s"),
12358 sub, iclass, oclass);
12359 }
12360
12361 goto error_return;
12362 }
12363 }
12364 }
12365 }
12366
12367 /* Free symbol buffer if needed. */
12368 if (!info->reduce_memory_overheads)
12369 {
12370 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12371 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
12372 && elf_tdata (sub)->symbuf)
12373 {
12374 free (elf_tdata (sub)->symbuf);
12375 elf_tdata (sub)->symbuf = NULL;
12376 }
12377 }
12378
12379 /* Output any global symbols that got converted to local in a
12380 version script or due to symbol visibility. We do this in a
12381 separate step since ELF requires all local symbols to appear
12382 prior to any global symbols. FIXME: We should only do this if
12383 some global symbols were, in fact, converted to become local.
12384 FIXME: Will this work correctly with the Irix 5 linker? */
12385 eoinfo.failed = FALSE;
12386 eoinfo.flinfo = &flinfo;
12387 eoinfo.localsyms = TRUE;
12388 eoinfo.file_sym_done = FALSE;
12389 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12390 if (eoinfo.failed)
12391 return FALSE;
12392
12393 /* If backend needs to output some local symbols not present in the hash
12394 table, do it now. */
12395 if (bed->elf_backend_output_arch_local_syms
12396 && (info->strip != strip_all || emit_relocs))
12397 {
12398 typedef int (*out_sym_func)
12399 (void *, const char *, Elf_Internal_Sym *, asection *,
12400 struct elf_link_hash_entry *);
12401
12402 if (! ((*bed->elf_backend_output_arch_local_syms)
12403 (abfd, info, &flinfo,
12404 (out_sym_func) elf_link_output_symstrtab)))
12405 return FALSE;
12406 }
12407
12408 /* That wrote out all the local symbols. Finish up the symbol table
12409 with the global symbols. Even if we want to strip everything we
12410 can, we still need to deal with those global symbols that got
12411 converted to local in a version script. */
12412
12413 /* The sh_info field records the index of the first non local symbol. */
12414 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12415
12416 if (dynamic
12417 && htab->dynsym != NULL
12418 && htab->dynsym->output_section != bfd_abs_section_ptr)
12419 {
12420 Elf_Internal_Sym sym;
12421 bfd_byte *dynsym = htab->dynsym->contents;
12422
12423 o = htab->dynsym->output_section;
12424 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12425
12426 /* Write out the section symbols for the output sections. */
12427 if (bfd_link_pic (info)
12428 || htab->is_relocatable_executable)
12429 {
12430 asection *s;
12431
12432 sym.st_size = 0;
12433 sym.st_name = 0;
12434 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12435 sym.st_other = 0;
12436 sym.st_target_internal = 0;
12437
12438 for (s = abfd->sections; s != NULL; s = s->next)
12439 {
12440 int indx;
12441 bfd_byte *dest;
12442 long dynindx;
12443
12444 dynindx = elf_section_data (s)->dynindx;
12445 if (dynindx <= 0)
12446 continue;
12447 indx = elf_section_data (s)->this_idx;
12448 BFD_ASSERT (indx > 0);
12449 sym.st_shndx = indx;
12450 if (! check_dynsym (abfd, &sym))
12451 return FALSE;
12452 sym.st_value = s->vma;
12453 dest = dynsym + dynindx * bed->s->sizeof_sym;
12454 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12455 }
12456 }
12457
12458 /* Write out the local dynsyms. */
12459 if (htab->dynlocal)
12460 {
12461 struct elf_link_local_dynamic_entry *e;
12462 for (e = htab->dynlocal; e ; e = e->next)
12463 {
12464 asection *s;
12465 bfd_byte *dest;
12466
12467 /* Copy the internal symbol and turn off visibility.
12468 Note that we saved a word of storage and overwrote
12469 the original st_name with the dynstr_index. */
12470 sym = e->isym;
12471 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12472
12473 s = bfd_section_from_elf_index (e->input_bfd,
12474 e->isym.st_shndx);
12475 if (s != NULL)
12476 {
12477 sym.st_shndx =
12478 elf_section_data (s->output_section)->this_idx;
12479 if (! check_dynsym (abfd, &sym))
12480 return FALSE;
12481 sym.st_value = (s->output_section->vma
12482 + s->output_offset
12483 + e->isym.st_value);
12484 }
12485
12486 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12487 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12488 }
12489 }
12490 }
12491
12492 /* We get the global symbols from the hash table. */
12493 eoinfo.failed = FALSE;
12494 eoinfo.localsyms = FALSE;
12495 eoinfo.flinfo = &flinfo;
12496 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12497 if (eoinfo.failed)
12498 return FALSE;
12499
12500 /* If backend needs to output some symbols not present in the hash
12501 table, do it now. */
12502 if (bed->elf_backend_output_arch_syms
12503 && (info->strip != strip_all || emit_relocs))
12504 {
12505 typedef int (*out_sym_func)
12506 (void *, const char *, Elf_Internal_Sym *, asection *,
12507 struct elf_link_hash_entry *);
12508
12509 if (! ((*bed->elf_backend_output_arch_syms)
12510 (abfd, info, &flinfo,
12511 (out_sym_func) elf_link_output_symstrtab)))
12512 return FALSE;
12513 }
12514
12515 /* Finalize the .strtab section. */
12516 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12517
12518 /* Swap out the .strtab section. */
12519 if (!elf_link_swap_symbols_out (&flinfo))
12520 return FALSE;
12521
12522 /* Now we know the size of the symtab section. */
12523 if (bfd_get_symcount (abfd) > 0)
12524 {
12525 /* Finish up and write out the symbol string table (.strtab)
12526 section. */
12527 Elf_Internal_Shdr *symstrtab_hdr = NULL;
12528 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12529
12530 if (elf_symtab_shndx_list (abfd))
12531 {
12532 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12533
12534 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12535 {
12536 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12537 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12538 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12539 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12540 symtab_shndx_hdr->sh_size = amt;
12541
12542 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12543 off, TRUE);
12544
12545 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12546 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12547 return FALSE;
12548 }
12549 }
12550
12551 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12552 /* sh_name was set in prep_headers. */
12553 symstrtab_hdr->sh_type = SHT_STRTAB;
12554 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12555 symstrtab_hdr->sh_addr = 0;
12556 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12557 symstrtab_hdr->sh_entsize = 0;
12558 symstrtab_hdr->sh_link = 0;
12559 symstrtab_hdr->sh_info = 0;
12560 /* sh_offset is set just below. */
12561 symstrtab_hdr->sh_addralign = 1;
12562
12563 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12564 off, TRUE);
12565 elf_next_file_pos (abfd) = off;
12566
12567 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12568 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12569 return FALSE;
12570 }
12571
12572 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12573 {
12574 _bfd_error_handler (_("%pB: failed to generate import library"),
12575 info->out_implib_bfd);
12576 return FALSE;
12577 }
12578
12579 /* Adjust the relocs to have the correct symbol indices. */
12580 for (o = abfd->sections; o != NULL; o = o->next)
12581 {
12582 struct bfd_elf_section_data *esdo = elf_section_data (o);
12583 bfd_boolean sort;
12584
12585 if ((o->flags & SEC_RELOC) == 0)
12586 continue;
12587
12588 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12589 if (esdo->rel.hdr != NULL
12590 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
12591 return FALSE;
12592 if (esdo->rela.hdr != NULL
12593 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
12594 return FALSE;
12595
12596 /* Set the reloc_count field to 0 to prevent write_relocs from
12597 trying to swap the relocs out itself. */
12598 o->reloc_count = 0;
12599 }
12600
12601 if (dynamic && info->combreloc && dynobj != NULL)
12602 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12603
12604 /* If we are linking against a dynamic object, or generating a
12605 shared library, finish up the dynamic linking information. */
12606 if (dynamic)
12607 {
12608 bfd_byte *dyncon, *dynconend;
12609
12610 /* Fix up .dynamic entries. */
12611 o = bfd_get_linker_section (dynobj, ".dynamic");
12612 BFD_ASSERT (o != NULL);
12613
12614 dyncon = o->contents;
12615 dynconend = o->contents + o->size;
12616 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12617 {
12618 Elf_Internal_Dyn dyn;
12619 const char *name;
12620 unsigned int type;
12621 bfd_size_type sh_size;
12622 bfd_vma sh_addr;
12623
12624 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12625
12626 switch (dyn.d_tag)
12627 {
12628 default:
12629 continue;
12630 case DT_NULL:
12631 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12632 {
12633 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12634 {
12635 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12636 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12637 default: continue;
12638 }
12639 dyn.d_un.d_val = relativecount;
12640 relativecount = 0;
12641 break;
12642 }
12643 continue;
12644
12645 case DT_INIT:
12646 name = info->init_function;
12647 goto get_sym;
12648 case DT_FINI:
12649 name = info->fini_function;
12650 get_sym:
12651 {
12652 struct elf_link_hash_entry *h;
12653
12654 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
12655 if (h != NULL
12656 && (h->root.type == bfd_link_hash_defined
12657 || h->root.type == bfd_link_hash_defweak))
12658 {
12659 dyn.d_un.d_ptr = h->root.u.def.value;
12660 o = h->root.u.def.section;
12661 if (o->output_section != NULL)
12662 dyn.d_un.d_ptr += (o->output_section->vma
12663 + o->output_offset);
12664 else
12665 {
12666 /* The symbol is imported from another shared
12667 library and does not apply to this one. */
12668 dyn.d_un.d_ptr = 0;
12669 }
12670 break;
12671 }
12672 }
12673 continue;
12674
12675 case DT_PREINIT_ARRAYSZ:
12676 name = ".preinit_array";
12677 goto get_out_size;
12678 case DT_INIT_ARRAYSZ:
12679 name = ".init_array";
12680 goto get_out_size;
12681 case DT_FINI_ARRAYSZ:
12682 name = ".fini_array";
12683 get_out_size:
12684 o = bfd_get_section_by_name (abfd, name);
12685 if (o == NULL)
12686 {
12687 _bfd_error_handler
12688 (_("could not find section %s"), name);
12689 goto error_return;
12690 }
12691 if (o->size == 0)
12692 _bfd_error_handler
12693 (_("warning: %s section has zero size"), name);
12694 dyn.d_un.d_val = o->size;
12695 break;
12696
12697 case DT_PREINIT_ARRAY:
12698 name = ".preinit_array";
12699 goto get_out_vma;
12700 case DT_INIT_ARRAY:
12701 name = ".init_array";
12702 goto get_out_vma;
12703 case DT_FINI_ARRAY:
12704 name = ".fini_array";
12705 get_out_vma:
12706 o = bfd_get_section_by_name (abfd, name);
12707 goto do_vma;
12708
12709 case DT_HASH:
12710 name = ".hash";
12711 goto get_vma;
12712 case DT_GNU_HASH:
12713 name = ".gnu.hash";
12714 goto get_vma;
12715 case DT_STRTAB:
12716 name = ".dynstr";
12717 goto get_vma;
12718 case DT_SYMTAB:
12719 name = ".dynsym";
12720 goto get_vma;
12721 case DT_VERDEF:
12722 name = ".gnu.version_d";
12723 goto get_vma;
12724 case DT_VERNEED:
12725 name = ".gnu.version_r";
12726 goto get_vma;
12727 case DT_VERSYM:
12728 name = ".gnu.version";
12729 get_vma:
12730 o = bfd_get_linker_section (dynobj, name);
12731 do_vma:
12732 if (o == NULL || bfd_is_abs_section (o->output_section))
12733 {
12734 _bfd_error_handler
12735 (_("could not find section %s"), name);
12736 goto error_return;
12737 }
12738 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12739 {
12740 _bfd_error_handler
12741 (_("warning: section '%s' is being made into a note"), name);
12742 bfd_set_error (bfd_error_nonrepresentable_section);
12743 goto error_return;
12744 }
12745 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
12746 break;
12747
12748 case DT_REL:
12749 case DT_RELA:
12750 case DT_RELSZ:
12751 case DT_RELASZ:
12752 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12753 type = SHT_REL;
12754 else
12755 type = SHT_RELA;
12756 sh_size = 0;
12757 sh_addr = 0;
12758 for (i = 1; i < elf_numsections (abfd); i++)
12759 {
12760 Elf_Internal_Shdr *hdr;
12761
12762 hdr = elf_elfsections (abfd)[i];
12763 if (hdr->sh_type == type
12764 && (hdr->sh_flags & SHF_ALLOC) != 0)
12765 {
12766 sh_size += hdr->sh_size;
12767 if (sh_addr == 0
12768 || sh_addr > hdr->sh_addr)
12769 sh_addr = hdr->sh_addr;
12770 }
12771 }
12772
12773 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12774 {
12775 /* Don't count procedure linkage table relocs in the
12776 overall reloc count. */
12777 sh_size -= htab->srelplt->size;
12778 if (sh_size == 0)
12779 /* If the size is zero, make the address zero too.
12780 This is to avoid a glibc bug. If the backend
12781 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12782 zero, then we'll put DT_RELA at the end of
12783 DT_JMPREL. glibc will interpret the end of
12784 DT_RELA matching the end of DT_JMPREL as the
12785 case where DT_RELA includes DT_JMPREL, and for
12786 LD_BIND_NOW will decide that processing DT_RELA
12787 will process the PLT relocs too. Net result:
12788 No PLT relocs applied. */
12789 sh_addr = 0;
12790
12791 /* If .rela.plt is the first .rela section, exclude
12792 it from DT_RELA. */
12793 else if (sh_addr == (htab->srelplt->output_section->vma
12794 + htab->srelplt->output_offset))
12795 sh_addr += htab->srelplt->size;
12796 }
12797
12798 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12799 dyn.d_un.d_val = sh_size;
12800 else
12801 dyn.d_un.d_ptr = sh_addr;
12802 break;
12803 }
12804 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12805 }
12806 }
12807
12808 /* If we have created any dynamic sections, then output them. */
12809 if (dynobj != NULL)
12810 {
12811 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12812 goto error_return;
12813
12814 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12815 if (((info->warn_shared_textrel && bfd_link_pic (info))
12816 || info->error_textrel)
12817 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
12818 {
12819 bfd_byte *dyncon, *dynconend;
12820
12821 dyncon = o->contents;
12822 dynconend = o->contents + o->size;
12823 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12824 {
12825 Elf_Internal_Dyn dyn;
12826
12827 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12828
12829 if (dyn.d_tag == DT_TEXTREL)
12830 {
12831 if (info->error_textrel)
12832 info->callbacks->einfo
12833 (_("%P%X: read-only segment has dynamic relocations\n"));
12834 else
12835 info->callbacks->einfo
12836 (_("%P: warning: creating a DT_TEXTREL in a shared object\n"));
12837 break;
12838 }
12839 }
12840 }
12841
12842 for (o = dynobj->sections; o != NULL; o = o->next)
12843 {
12844 if ((o->flags & SEC_HAS_CONTENTS) == 0
12845 || o->size == 0
12846 || o->output_section == bfd_abs_section_ptr)
12847 continue;
12848 if ((o->flags & SEC_LINKER_CREATED) == 0)
12849 {
12850 /* At this point, we are only interested in sections
12851 created by _bfd_elf_link_create_dynamic_sections. */
12852 continue;
12853 }
12854 if (htab->stab_info.stabstr == o)
12855 continue;
12856 if (htab->eh_info.hdr_sec == o)
12857 continue;
12858 if (strcmp (o->name, ".dynstr") != 0)
12859 {
12860 bfd_size_type octets = ((file_ptr) o->output_offset
12861 * bfd_octets_per_byte (abfd, o));
12862 if (!bfd_set_section_contents (abfd, o->output_section,
12863 o->contents, octets, o->size))
12864 goto error_return;
12865 }
12866 else
12867 {
12868 /* The contents of the .dynstr section are actually in a
12869 stringtab. */
12870 file_ptr off;
12871
12872 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12873 if (bfd_seek (abfd, off, SEEK_SET) != 0
12874 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
12875 goto error_return;
12876 }
12877 }
12878 }
12879
12880 if (!info->resolve_section_groups)
12881 {
12882 bfd_boolean failed = FALSE;
12883
12884 BFD_ASSERT (bfd_link_relocatable (info));
12885 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12886 if (failed)
12887 goto error_return;
12888 }
12889
12890 /* If we have optimized stabs strings, output them. */
12891 if (htab->stab_info.stabstr != NULL)
12892 {
12893 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
12894 goto error_return;
12895 }
12896
12897 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12898 goto error_return;
12899
12900 if (info->callbacks->emit_ctf)
12901 info->callbacks->emit_ctf ();
12902
12903 elf_final_link_free (abfd, &flinfo);
12904
12905 if (attr_section)
12906 {
12907 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
12908 if (contents == NULL)
12909 return FALSE; /* Bail out and fail. */
12910 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12911 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12912 free (contents);
12913 }
12914
12915 return TRUE;
12916
12917 error_return:
12918 elf_final_link_free (abfd, &flinfo);
12919 return FALSE;
12920 }
12921 \f
12922 /* Initialize COOKIE for input bfd ABFD. */
12923
12924 static bfd_boolean
12925 init_reloc_cookie (struct elf_reloc_cookie *cookie,
12926 struct bfd_link_info *info, bfd *abfd)
12927 {
12928 Elf_Internal_Shdr *symtab_hdr;
12929 const struct elf_backend_data *bed;
12930
12931 bed = get_elf_backend_data (abfd);
12932 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12933
12934 cookie->abfd = abfd;
12935 cookie->sym_hashes = elf_sym_hashes (abfd);
12936 cookie->bad_symtab = elf_bad_symtab (abfd);
12937 if (cookie->bad_symtab)
12938 {
12939 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12940 cookie->extsymoff = 0;
12941 }
12942 else
12943 {
12944 cookie->locsymcount = symtab_hdr->sh_info;
12945 cookie->extsymoff = symtab_hdr->sh_info;
12946 }
12947
12948 if (bed->s->arch_size == 32)
12949 cookie->r_sym_shift = 8;
12950 else
12951 cookie->r_sym_shift = 32;
12952
12953 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12954 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12955 {
12956 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12957 cookie->locsymcount, 0,
12958 NULL, NULL, NULL);
12959 if (cookie->locsyms == NULL)
12960 {
12961 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12962 return FALSE;
12963 }
12964 if (info->keep_memory)
12965 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12966 }
12967 return TRUE;
12968 }
12969
12970 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12971
12972 static void
12973 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12974 {
12975 Elf_Internal_Shdr *symtab_hdr;
12976
12977 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12978 if (cookie->locsyms != NULL
12979 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12980 free (cookie->locsyms);
12981 }
12982
12983 /* Initialize the relocation information in COOKIE for input section SEC
12984 of input bfd ABFD. */
12985
12986 static bfd_boolean
12987 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12988 struct bfd_link_info *info, bfd *abfd,
12989 asection *sec)
12990 {
12991 if (sec->reloc_count == 0)
12992 {
12993 cookie->rels = NULL;
12994 cookie->relend = NULL;
12995 }
12996 else
12997 {
12998 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12999 info->keep_memory);
13000 if (cookie->rels == NULL)
13001 return FALSE;
13002 cookie->rel = cookie->rels;
13003 cookie->relend = cookie->rels + sec->reloc_count;
13004 }
13005 cookie->rel = cookie->rels;
13006 return TRUE;
13007 }
13008
13009 /* Free the memory allocated by init_reloc_cookie_rels,
13010 if appropriate. */
13011
13012 static void
13013 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13014 asection *sec)
13015 {
13016 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
13017 free (cookie->rels);
13018 }
13019
13020 /* Initialize the whole of COOKIE for input section SEC. */
13021
13022 static bfd_boolean
13023 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13024 struct bfd_link_info *info,
13025 asection *sec)
13026 {
13027 if (!init_reloc_cookie (cookie, info, sec->owner))
13028 goto error1;
13029 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13030 goto error2;
13031 return TRUE;
13032
13033 error2:
13034 fini_reloc_cookie (cookie, sec->owner);
13035 error1:
13036 return FALSE;
13037 }
13038
13039 /* Free the memory allocated by init_reloc_cookie_for_section,
13040 if appropriate. */
13041
13042 static void
13043 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13044 asection *sec)
13045 {
13046 fini_reloc_cookie_rels (cookie, sec);
13047 fini_reloc_cookie (cookie, sec->owner);
13048 }
13049 \f
13050 /* Garbage collect unused sections. */
13051
13052 /* Default gc_mark_hook. */
13053
13054 asection *
13055 _bfd_elf_gc_mark_hook (asection *sec,
13056 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13057 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13058 struct elf_link_hash_entry *h,
13059 Elf_Internal_Sym *sym)
13060 {
13061 if (h != NULL)
13062 {
13063 switch (h->root.type)
13064 {
13065 case bfd_link_hash_defined:
13066 case bfd_link_hash_defweak:
13067 return h->root.u.def.section;
13068
13069 case bfd_link_hash_common:
13070 return h->root.u.c.p->section;
13071
13072 default:
13073 break;
13074 }
13075 }
13076 else
13077 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13078
13079 return NULL;
13080 }
13081
13082 /* Return the debug definition section. */
13083
13084 static asection *
13085 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13086 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13087 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13088 struct elf_link_hash_entry *h,
13089 Elf_Internal_Sym *sym)
13090 {
13091 if (h != NULL)
13092 {
13093 /* Return the global debug definition section. */
13094 if ((h->root.type == bfd_link_hash_defined
13095 || h->root.type == bfd_link_hash_defweak)
13096 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13097 return h->root.u.def.section;
13098 }
13099 else
13100 {
13101 /* Return the local debug definition section. */
13102 asection *isec = bfd_section_from_elf_index (sec->owner,
13103 sym->st_shndx);
13104 if ((isec->flags & SEC_DEBUGGING) != 0)
13105 return isec;
13106 }
13107
13108 return NULL;
13109 }
13110
13111 /* COOKIE->rel describes a relocation against section SEC, which is
13112 a section we've decided to keep. Return the section that contains
13113 the relocation symbol, or NULL if no section contains it. */
13114
13115 asection *
13116 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13117 elf_gc_mark_hook_fn gc_mark_hook,
13118 struct elf_reloc_cookie *cookie,
13119 bfd_boolean *start_stop)
13120 {
13121 unsigned long r_symndx;
13122 struct elf_link_hash_entry *h, *hw;
13123
13124 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
13125 if (r_symndx == STN_UNDEF)
13126 return NULL;
13127
13128 if (r_symndx >= cookie->locsymcount
13129 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13130 {
13131 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
13132 if (h == NULL)
13133 {
13134 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
13135 sec->owner);
13136 return NULL;
13137 }
13138 while (h->root.type == bfd_link_hash_indirect
13139 || h->root.type == bfd_link_hash_warning)
13140 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13141 h->mark = 1;
13142 /* Keep all aliases of the symbol too. If an object symbol
13143 needs to be copied into .dynbss then all of its aliases
13144 should be present as dynamic symbols, not just the one used
13145 on the copy relocation. */
13146 hw = h;
13147 while (hw->is_weakalias)
13148 {
13149 hw = hw->u.alias;
13150 hw->mark = 1;
13151 }
13152
13153 if (start_stop != NULL)
13154 {
13155 /* To work around a glibc bug, mark XXX input sections
13156 when there is a reference to __start_XXX or __stop_XXX
13157 symbols. */
13158 if (h->start_stop)
13159 {
13160 asection *s = h->u2.start_stop_section;
13161 *start_stop = !s->gc_mark;
13162 return s;
13163 }
13164 }
13165
13166 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13167 }
13168
13169 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13170 &cookie->locsyms[r_symndx]);
13171 }
13172
13173 /* COOKIE->rel describes a relocation against section SEC, which is
13174 a section we've decided to keep. Mark the section that contains
13175 the relocation symbol. */
13176
13177 bfd_boolean
13178 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13179 asection *sec,
13180 elf_gc_mark_hook_fn gc_mark_hook,
13181 struct elf_reloc_cookie *cookie)
13182 {
13183 asection *rsec;
13184 bfd_boolean start_stop = FALSE;
13185
13186 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13187 while (rsec != NULL)
13188 {
13189 if (!rsec->gc_mark)
13190 {
13191 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13192 || (rsec->owner->flags & DYNAMIC) != 0)
13193 rsec->gc_mark = 1;
13194 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13195 return FALSE;
13196 }
13197 if (!start_stop)
13198 break;
13199 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
13200 }
13201 return TRUE;
13202 }
13203
13204 /* The mark phase of garbage collection. For a given section, mark
13205 it and any sections in this section's group, and all the sections
13206 which define symbols to which it refers. */
13207
13208 bfd_boolean
13209 _bfd_elf_gc_mark (struct bfd_link_info *info,
13210 asection *sec,
13211 elf_gc_mark_hook_fn gc_mark_hook)
13212 {
13213 bfd_boolean ret;
13214 asection *group_sec, *eh_frame;
13215
13216 sec->gc_mark = 1;
13217
13218 /* Mark all the sections in the group. */
13219 group_sec = elf_section_data (sec)->next_in_group;
13220 if (group_sec && !group_sec->gc_mark)
13221 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
13222 return FALSE;
13223
13224 /* Look through the section relocs. */
13225 ret = TRUE;
13226 eh_frame = elf_eh_frame_section (sec->owner);
13227 if ((sec->flags & SEC_RELOC) != 0
13228 && sec->reloc_count > 0
13229 && sec != eh_frame)
13230 {
13231 struct elf_reloc_cookie cookie;
13232
13233 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13234 ret = FALSE;
13235 else
13236 {
13237 for (; cookie.rel < cookie.relend; cookie.rel++)
13238 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
13239 {
13240 ret = FALSE;
13241 break;
13242 }
13243 fini_reloc_cookie_for_section (&cookie, sec);
13244 }
13245 }
13246
13247 if (ret && eh_frame && elf_fde_list (sec))
13248 {
13249 struct elf_reloc_cookie cookie;
13250
13251 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13252 ret = FALSE;
13253 else
13254 {
13255 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13256 gc_mark_hook, &cookie))
13257 ret = FALSE;
13258 fini_reloc_cookie_for_section (&cookie, eh_frame);
13259 }
13260 }
13261
13262 eh_frame = elf_section_eh_frame_entry (sec);
13263 if (ret && eh_frame && !eh_frame->gc_mark)
13264 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13265 ret = FALSE;
13266
13267 return ret;
13268 }
13269
13270 /* Scan and mark sections in a special or debug section group. */
13271
13272 static void
13273 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13274 {
13275 /* Point to first section of section group. */
13276 asection *ssec;
13277 /* Used to iterate the section group. */
13278 asection *msec;
13279
13280 bfd_boolean is_special_grp = TRUE;
13281 bfd_boolean is_debug_grp = TRUE;
13282
13283 /* First scan to see if group contains any section other than debug
13284 and special section. */
13285 ssec = msec = elf_next_in_group (grp);
13286 do
13287 {
13288 if ((msec->flags & SEC_DEBUGGING) == 0)
13289 is_debug_grp = FALSE;
13290
13291 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13292 is_special_grp = FALSE;
13293
13294 msec = elf_next_in_group (msec);
13295 }
13296 while (msec != ssec);
13297
13298 /* If this is a pure debug section group or pure special section group,
13299 keep all sections in this group. */
13300 if (is_debug_grp || is_special_grp)
13301 {
13302 do
13303 {
13304 msec->gc_mark = 1;
13305 msec = elf_next_in_group (msec);
13306 }
13307 while (msec != ssec);
13308 }
13309 }
13310
13311 /* Keep debug and special sections. */
13312
13313 bfd_boolean
13314 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13315 elf_gc_mark_hook_fn mark_hook)
13316 {
13317 bfd *ibfd;
13318
13319 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13320 {
13321 asection *isec;
13322 bfd_boolean some_kept;
13323 bfd_boolean debug_frag_seen;
13324 bfd_boolean has_kept_debug_info;
13325
13326 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13327 continue;
13328 isec = ibfd->sections;
13329 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13330 continue;
13331
13332 /* Ensure all linker created sections are kept,
13333 see if any other section is already marked,
13334 and note if we have any fragmented debug sections. */
13335 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
13336 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13337 {
13338 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13339 isec->gc_mark = 1;
13340 else if (isec->gc_mark
13341 && (isec->flags & SEC_ALLOC) != 0
13342 && elf_section_type (isec) != SHT_NOTE)
13343 some_kept = TRUE;
13344 else
13345 {
13346 /* Since all sections, except for backend specific ones,
13347 have been garbage collected, call mark_hook on this
13348 section if any of its linked-to sections is marked. */
13349 asection *linked_to_sec = elf_linked_to_section (isec);
13350 for (; linked_to_sec != NULL;
13351 linked_to_sec = elf_linked_to_section (linked_to_sec))
13352 if (linked_to_sec->gc_mark)
13353 {
13354 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
13355 return FALSE;
13356 break;
13357 }
13358 }
13359
13360 if (!debug_frag_seen
13361 && (isec->flags & SEC_DEBUGGING)
13362 && CONST_STRNEQ (isec->name, ".debug_line."))
13363 debug_frag_seen = TRUE;
13364 else if (strcmp (bfd_section_name (isec),
13365 "__patchable_function_entries") == 0
13366 && elf_linked_to_section (isec) == NULL)
13367 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
13368 "need linked-to section "
13369 "for --gc-sections\n"),
13370 isec->owner, isec);
13371 }
13372
13373 /* If no non-note alloc section in this file will be kept, then
13374 we can toss out the debug and special sections. */
13375 if (!some_kept)
13376 continue;
13377
13378 /* Keep debug and special sections like .comment when they are
13379 not part of a group. Also keep section groups that contain
13380 just debug sections or special sections. NB: Sections with
13381 linked-to section has been handled above. */
13382 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13383 {
13384 if ((isec->flags & SEC_GROUP) != 0)
13385 _bfd_elf_gc_mark_debug_special_section_group (isec);
13386 else if (((isec->flags & SEC_DEBUGGING) != 0
13387 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13388 && elf_next_in_group (isec) == NULL
13389 && elf_linked_to_section (isec) == NULL)
13390 isec->gc_mark = 1;
13391 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13392 has_kept_debug_info = TRUE;
13393 }
13394
13395 /* Look for CODE sections which are going to be discarded,
13396 and find and discard any fragmented debug sections which
13397 are associated with that code section. */
13398 if (debug_frag_seen)
13399 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13400 if ((isec->flags & SEC_CODE) != 0
13401 && isec->gc_mark == 0)
13402 {
13403 unsigned int ilen;
13404 asection *dsec;
13405
13406 ilen = strlen (isec->name);
13407
13408 /* Association is determined by the name of the debug
13409 section containing the name of the code section as
13410 a suffix. For example .debug_line.text.foo is a
13411 debug section associated with .text.foo. */
13412 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13413 {
13414 unsigned int dlen;
13415
13416 if (dsec->gc_mark == 0
13417 || (dsec->flags & SEC_DEBUGGING) == 0)
13418 continue;
13419
13420 dlen = strlen (dsec->name);
13421
13422 if (dlen > ilen
13423 && strncmp (dsec->name + (dlen - ilen),
13424 isec->name, ilen) == 0)
13425 dsec->gc_mark = 0;
13426 }
13427 }
13428
13429 /* Mark debug sections referenced by kept debug sections. */
13430 if (has_kept_debug_info)
13431 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13432 if (isec->gc_mark
13433 && (isec->flags & SEC_DEBUGGING) != 0)
13434 if (!_bfd_elf_gc_mark (info, isec,
13435 elf_gc_mark_debug_section))
13436 return FALSE;
13437 }
13438 return TRUE;
13439 }
13440
13441 static bfd_boolean
13442 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13443 {
13444 bfd *sub;
13445 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13446
13447 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13448 {
13449 asection *o;
13450
13451 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13452 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
13453 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13454 continue;
13455 o = sub->sections;
13456 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13457 continue;
13458
13459 for (o = sub->sections; o != NULL; o = o->next)
13460 {
13461 /* When any section in a section group is kept, we keep all
13462 sections in the section group. If the first member of
13463 the section group is excluded, we will also exclude the
13464 group section. */
13465 if (o->flags & SEC_GROUP)
13466 {
13467 asection *first = elf_next_in_group (o);
13468 o->gc_mark = first->gc_mark;
13469 }
13470
13471 if (o->gc_mark)
13472 continue;
13473
13474 /* Skip sweeping sections already excluded. */
13475 if (o->flags & SEC_EXCLUDE)
13476 continue;
13477
13478 /* Since this is early in the link process, it is simple
13479 to remove a section from the output. */
13480 o->flags |= SEC_EXCLUDE;
13481
13482 if (info->print_gc_sections && o->size != 0)
13483 /* xgettext:c-format */
13484 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13485 o, sub);
13486 }
13487 }
13488
13489 return TRUE;
13490 }
13491
13492 /* Propagate collected vtable information. This is called through
13493 elf_link_hash_traverse. */
13494
13495 static bfd_boolean
13496 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13497 {
13498 /* Those that are not vtables. */
13499 if (h->start_stop
13500 || h->u2.vtable == NULL
13501 || h->u2.vtable->parent == NULL)
13502 return TRUE;
13503
13504 /* Those vtables that do not have parents, we cannot merge. */
13505 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
13506 return TRUE;
13507
13508 /* If we've already been done, exit. */
13509 if (h->u2.vtable->used && h->u2.vtable->used[-1])
13510 return TRUE;
13511
13512 /* Make sure the parent's table is up to date. */
13513 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
13514
13515 if (h->u2.vtable->used == NULL)
13516 {
13517 /* None of this table's entries were referenced. Re-use the
13518 parent's table. */
13519 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13520 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
13521 }
13522 else
13523 {
13524 size_t n;
13525 bfd_boolean *cu, *pu;
13526
13527 /* Or the parent's entries into ours. */
13528 cu = h->u2.vtable->used;
13529 cu[-1] = TRUE;
13530 pu = h->u2.vtable->parent->u2.vtable->used;
13531 if (pu != NULL)
13532 {
13533 const struct elf_backend_data *bed;
13534 unsigned int log_file_align;
13535
13536 bed = get_elf_backend_data (h->root.u.def.section->owner);
13537 log_file_align = bed->s->log_file_align;
13538 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
13539 while (n--)
13540 {
13541 if (*pu)
13542 *cu = TRUE;
13543 pu++;
13544 cu++;
13545 }
13546 }
13547 }
13548
13549 return TRUE;
13550 }
13551
13552 static bfd_boolean
13553 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13554 {
13555 asection *sec;
13556 bfd_vma hstart, hend;
13557 Elf_Internal_Rela *relstart, *relend, *rel;
13558 const struct elf_backend_data *bed;
13559 unsigned int log_file_align;
13560
13561 /* Take care of both those symbols that do not describe vtables as
13562 well as those that are not loaded. */
13563 if (h->start_stop
13564 || h->u2.vtable == NULL
13565 || h->u2.vtable->parent == NULL)
13566 return TRUE;
13567
13568 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13569 || h->root.type == bfd_link_hash_defweak);
13570
13571 sec = h->root.u.def.section;
13572 hstart = h->root.u.def.value;
13573 hend = hstart + h->size;
13574
13575 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13576 if (!relstart)
13577 return *(bfd_boolean *) okp = FALSE;
13578 bed = get_elf_backend_data (sec->owner);
13579 log_file_align = bed->s->log_file_align;
13580
13581 relend = relstart + sec->reloc_count;
13582
13583 for (rel = relstart; rel < relend; ++rel)
13584 if (rel->r_offset >= hstart && rel->r_offset < hend)
13585 {
13586 /* If the entry is in use, do nothing. */
13587 if (h->u2.vtable->used
13588 && (rel->r_offset - hstart) < h->u2.vtable->size)
13589 {
13590 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13591 if (h->u2.vtable->used[entry])
13592 continue;
13593 }
13594 /* Otherwise, kill it. */
13595 rel->r_offset = rel->r_info = rel->r_addend = 0;
13596 }
13597
13598 return TRUE;
13599 }
13600
13601 /* Mark sections containing dynamically referenced symbols. When
13602 building shared libraries, we must assume that any visible symbol is
13603 referenced. */
13604
13605 bfd_boolean
13606 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13607 {
13608 struct bfd_link_info *info = (struct bfd_link_info *) inf;
13609 struct bfd_elf_dynamic_list *d = info->dynamic_list;
13610
13611 if ((h->root.type == bfd_link_hash_defined
13612 || h->root.type == bfd_link_hash_defweak)
13613 && ((h->ref_dynamic && !h->forced_local)
13614 || ((h->def_regular || ELF_COMMON_DEF_P (h))
13615 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
13616 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
13617 && (!bfd_link_executable (info)
13618 || info->gc_keep_exported
13619 || info->export_dynamic
13620 || (h->dynamic
13621 && d != NULL
13622 && (*d->match) (&d->head, NULL, h->root.root.string)))
13623 && (h->versioned >= versioned
13624 || !bfd_hide_sym_by_version (info->version_info,
13625 h->root.root.string)))))
13626 h->root.u.def.section->flags |= SEC_KEEP;
13627
13628 return TRUE;
13629 }
13630
13631 /* Keep all sections containing symbols undefined on the command-line,
13632 and the section containing the entry symbol. */
13633
13634 void
13635 _bfd_elf_gc_keep (struct bfd_link_info *info)
13636 {
13637 struct bfd_sym_chain *sym;
13638
13639 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13640 {
13641 struct elf_link_hash_entry *h;
13642
13643 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13644 FALSE, FALSE, FALSE);
13645
13646 if (h != NULL
13647 && (h->root.type == bfd_link_hash_defined
13648 || h->root.type == bfd_link_hash_defweak)
13649 && !bfd_is_abs_section (h->root.u.def.section)
13650 && !bfd_is_und_section (h->root.u.def.section))
13651 h->root.u.def.section->flags |= SEC_KEEP;
13652 }
13653 }
13654
13655 bfd_boolean
13656 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13657 struct bfd_link_info *info)
13658 {
13659 bfd *ibfd = info->input_bfds;
13660
13661 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13662 {
13663 asection *sec;
13664 struct elf_reloc_cookie cookie;
13665
13666 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13667 continue;
13668 sec = ibfd->sections;
13669 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13670 continue;
13671
13672 if (!init_reloc_cookie (&cookie, info, ibfd))
13673 return FALSE;
13674
13675 for (sec = ibfd->sections; sec; sec = sec->next)
13676 {
13677 if (CONST_STRNEQ (bfd_section_name (sec), ".eh_frame_entry")
13678 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13679 {
13680 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13681 fini_reloc_cookie_rels (&cookie, sec);
13682 }
13683 }
13684 }
13685 return TRUE;
13686 }
13687
13688 /* Do mark and sweep of unused sections. */
13689
13690 bfd_boolean
13691 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13692 {
13693 bfd_boolean ok = TRUE;
13694 bfd *sub;
13695 elf_gc_mark_hook_fn gc_mark_hook;
13696 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13697 struct elf_link_hash_table *htab;
13698
13699 if (!bed->can_gc_sections
13700 || !is_elf_hash_table (info->hash))
13701 {
13702 _bfd_error_handler(_("warning: gc-sections option ignored"));
13703 return TRUE;
13704 }
13705
13706 bed->gc_keep (info);
13707 htab = elf_hash_table (info);
13708
13709 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13710 at the .eh_frame section if we can mark the FDEs individually. */
13711 for (sub = info->input_bfds;
13712 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13713 sub = sub->link.next)
13714 {
13715 asection *sec;
13716 struct elf_reloc_cookie cookie;
13717
13718 sec = sub->sections;
13719 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13720 continue;
13721 sec = bfd_get_section_by_name (sub, ".eh_frame");
13722 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
13723 {
13724 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
13725 if (elf_section_data (sec)->sec_info
13726 && (sec->flags & SEC_LINKER_CREATED) == 0)
13727 elf_eh_frame_section (sub) = sec;
13728 fini_reloc_cookie_for_section (&cookie, sec);
13729 sec = bfd_get_next_section_by_name (NULL, sec);
13730 }
13731 }
13732
13733 /* Apply transitive closure to the vtable entry usage info. */
13734 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13735 if (!ok)
13736 return FALSE;
13737
13738 /* Kill the vtable relocations that were not used. */
13739 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13740 if (!ok)
13741 return FALSE;
13742
13743 /* Mark dynamically referenced symbols. */
13744 if (htab->dynamic_sections_created || info->gc_keep_exported)
13745 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13746
13747 /* Grovel through relocs to find out who stays ... */
13748 gc_mark_hook = bed->gc_mark_hook;
13749 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13750 {
13751 asection *o;
13752
13753 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13754 || elf_object_id (sub) != elf_hash_table_id (htab)
13755 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13756 continue;
13757
13758 o = sub->sections;
13759 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13760 continue;
13761
13762 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13763 Also treat note sections as a root, if the section is not part
13764 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13765 well as FINI_ARRAY sections for ld -r. */
13766 for (o = sub->sections; o != NULL; o = o->next)
13767 if (!o->gc_mark
13768 && (o->flags & SEC_EXCLUDE) == 0
13769 && ((o->flags & SEC_KEEP) != 0
13770 || (bfd_link_relocatable (info)
13771 && ((elf_section_data (o)->this_hdr.sh_type
13772 == SHT_PREINIT_ARRAY)
13773 || (elf_section_data (o)->this_hdr.sh_type
13774 == SHT_INIT_ARRAY)
13775 || (elf_section_data (o)->this_hdr.sh_type
13776 == SHT_FINI_ARRAY)))
13777 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13778 && elf_next_in_group (o) == NULL )))
13779 {
13780 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13781 return FALSE;
13782 }
13783 }
13784
13785 /* Allow the backend to mark additional target specific sections. */
13786 bed->gc_mark_extra_sections (info, gc_mark_hook);
13787
13788 /* ... and mark SEC_EXCLUDE for those that go. */
13789 return elf_gc_sweep (abfd, info);
13790 }
13791 \f
13792 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13793
13794 bfd_boolean
13795 bfd_elf_gc_record_vtinherit (bfd *abfd,
13796 asection *sec,
13797 struct elf_link_hash_entry *h,
13798 bfd_vma offset)
13799 {
13800 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13801 struct elf_link_hash_entry **search, *child;
13802 size_t extsymcount;
13803 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13804
13805 /* The sh_info field of the symtab header tells us where the
13806 external symbols start. We don't care about the local symbols at
13807 this point. */
13808 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13809 if (!elf_bad_symtab (abfd))
13810 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13811
13812 sym_hashes = elf_sym_hashes (abfd);
13813 sym_hashes_end = sym_hashes + extsymcount;
13814
13815 /* Hunt down the child symbol, which is in this section at the same
13816 offset as the relocation. */
13817 for (search = sym_hashes; search != sym_hashes_end; ++search)
13818 {
13819 if ((child = *search) != NULL
13820 && (child->root.type == bfd_link_hash_defined
13821 || child->root.type == bfd_link_hash_defweak)
13822 && child->root.u.def.section == sec
13823 && child->root.u.def.value == offset)
13824 goto win;
13825 }
13826
13827 /* xgettext:c-format */
13828 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
13829 abfd, sec, (uint64_t) offset);
13830 bfd_set_error (bfd_error_invalid_operation);
13831 return FALSE;
13832
13833 win:
13834 if (!child->u2.vtable)
13835 {
13836 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13837 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13838 if (!child->u2.vtable)
13839 return FALSE;
13840 }
13841 if (!h)
13842 {
13843 /* This *should* only be the absolute section. It could potentially
13844 be that someone has defined a non-global vtable though, which
13845 would be bad. It isn't worth paging in the local symbols to be
13846 sure though; that case should simply be handled by the assembler. */
13847
13848 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
13849 }
13850 else
13851 child->u2.vtable->parent = h;
13852
13853 return TRUE;
13854 }
13855
13856 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13857
13858 bfd_boolean
13859 bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
13860 struct elf_link_hash_entry *h,
13861 bfd_vma addend)
13862 {
13863 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13864 unsigned int log_file_align = bed->s->log_file_align;
13865
13866 if (!h)
13867 {
13868 /* xgettext:c-format */
13869 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
13870 abfd, sec);
13871 bfd_set_error (bfd_error_bad_value);
13872 return FALSE;
13873 }
13874
13875 if (!h->u2.vtable)
13876 {
13877 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13878 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13879 if (!h->u2.vtable)
13880 return FALSE;
13881 }
13882
13883 if (addend >= h->u2.vtable->size)
13884 {
13885 size_t size, bytes, file_align;
13886 bfd_boolean *ptr = h->u2.vtable->used;
13887
13888 /* While the symbol is undefined, we have to be prepared to handle
13889 a zero size. */
13890 file_align = 1 << log_file_align;
13891 if (h->root.type == bfd_link_hash_undefined)
13892 size = addend + file_align;
13893 else
13894 {
13895 size = h->size;
13896 if (addend >= size)
13897 {
13898 /* Oops! We've got a reference past the defined end of
13899 the table. This is probably a bug -- shall we warn? */
13900 size = addend + file_align;
13901 }
13902 }
13903 size = (size + file_align - 1) & -file_align;
13904
13905 /* Allocate one extra entry for use as a "done" flag for the
13906 consolidation pass. */
13907 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13908
13909 if (ptr)
13910 {
13911 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
13912
13913 if (ptr != NULL)
13914 {
13915 size_t oldbytes;
13916
13917 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
13918 * sizeof (bfd_boolean));
13919 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13920 }
13921 }
13922 else
13923 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13924
13925 if (ptr == NULL)
13926 return FALSE;
13927
13928 /* And arrange for that done flag to be at index -1. */
13929 h->u2.vtable->used = ptr + 1;
13930 h->u2.vtable->size = size;
13931 }
13932
13933 h->u2.vtable->used[addend >> log_file_align] = TRUE;
13934
13935 return TRUE;
13936 }
13937
13938 /* Map an ELF section header flag to its corresponding string. */
13939 typedef struct
13940 {
13941 char *flag_name;
13942 flagword flag_value;
13943 } elf_flags_to_name_table;
13944
13945 static elf_flags_to_name_table elf_flags_to_names [] =
13946 {
13947 { "SHF_WRITE", SHF_WRITE },
13948 { "SHF_ALLOC", SHF_ALLOC },
13949 { "SHF_EXECINSTR", SHF_EXECINSTR },
13950 { "SHF_MERGE", SHF_MERGE },
13951 { "SHF_STRINGS", SHF_STRINGS },
13952 { "SHF_INFO_LINK", SHF_INFO_LINK},
13953 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13954 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13955 { "SHF_GROUP", SHF_GROUP },
13956 { "SHF_TLS", SHF_TLS },
13957 { "SHF_MASKOS", SHF_MASKOS },
13958 { "SHF_EXCLUDE", SHF_EXCLUDE },
13959 };
13960
13961 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13962 bfd_boolean
13963 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13964 struct flag_info *flaginfo,
13965 asection *section)
13966 {
13967 const bfd_vma sh_flags = elf_section_flags (section);
13968
13969 if (!flaginfo->flags_initialized)
13970 {
13971 bfd *obfd = info->output_bfd;
13972 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13973 struct flag_info_list *tf = flaginfo->flag_list;
13974 int with_hex = 0;
13975 int without_hex = 0;
13976
13977 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13978 {
13979 unsigned i;
13980 flagword (*lookup) (char *);
13981
13982 lookup = bed->elf_backend_lookup_section_flags_hook;
13983 if (lookup != NULL)
13984 {
13985 flagword hexval = (*lookup) ((char *) tf->name);
13986
13987 if (hexval != 0)
13988 {
13989 if (tf->with == with_flags)
13990 with_hex |= hexval;
13991 else if (tf->with == without_flags)
13992 without_hex |= hexval;
13993 tf->valid = TRUE;
13994 continue;
13995 }
13996 }
13997 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13998 {
13999 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
14000 {
14001 if (tf->with == with_flags)
14002 with_hex |= elf_flags_to_names[i].flag_value;
14003 else if (tf->with == without_flags)
14004 without_hex |= elf_flags_to_names[i].flag_value;
14005 tf->valid = TRUE;
14006 break;
14007 }
14008 }
14009 if (!tf->valid)
14010 {
14011 info->callbacks->einfo
14012 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
14013 return FALSE;
14014 }
14015 }
14016 flaginfo->flags_initialized = TRUE;
14017 flaginfo->only_with_flags |= with_hex;
14018 flaginfo->not_with_flags |= without_hex;
14019 }
14020
14021 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
14022 return FALSE;
14023
14024 if ((flaginfo->not_with_flags & sh_flags) != 0)
14025 return FALSE;
14026
14027 return TRUE;
14028 }
14029
14030 struct alloc_got_off_arg {
14031 bfd_vma gotoff;
14032 struct bfd_link_info *info;
14033 };
14034
14035 /* We need a special top-level link routine to convert got reference counts
14036 to real got offsets. */
14037
14038 static bfd_boolean
14039 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14040 {
14041 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
14042 bfd *obfd = gofarg->info->output_bfd;
14043 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14044
14045 if (h->got.refcount > 0)
14046 {
14047 h->got.offset = gofarg->gotoff;
14048 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
14049 }
14050 else
14051 h->got.offset = (bfd_vma) -1;
14052
14053 return TRUE;
14054 }
14055
14056 /* And an accompanying bit to work out final got entry offsets once
14057 we're done. Should be called from final_link. */
14058
14059 bfd_boolean
14060 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14061 struct bfd_link_info *info)
14062 {
14063 bfd *i;
14064 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14065 bfd_vma gotoff;
14066 struct alloc_got_off_arg gofarg;
14067
14068 BFD_ASSERT (abfd == info->output_bfd);
14069
14070 if (! is_elf_hash_table (info->hash))
14071 return FALSE;
14072
14073 /* The GOT offset is relative to the .got section, but the GOT header is
14074 put into the .got.plt section, if the backend uses it. */
14075 if (bed->want_got_plt)
14076 gotoff = 0;
14077 else
14078 gotoff = bed->got_header_size;
14079
14080 /* Do the local .got entries first. */
14081 for (i = info->input_bfds; i; i = i->link.next)
14082 {
14083 bfd_signed_vma *local_got;
14084 size_t j, locsymcount;
14085 Elf_Internal_Shdr *symtab_hdr;
14086
14087 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14088 continue;
14089
14090 local_got = elf_local_got_refcounts (i);
14091 if (!local_got)
14092 continue;
14093
14094 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14095 if (elf_bad_symtab (i))
14096 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14097 else
14098 locsymcount = symtab_hdr->sh_info;
14099
14100 for (j = 0; j < locsymcount; ++j)
14101 {
14102 if (local_got[j] > 0)
14103 {
14104 local_got[j] = gotoff;
14105 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
14106 }
14107 else
14108 local_got[j] = (bfd_vma) -1;
14109 }
14110 }
14111
14112 /* Then the global .got entries. .plt refcounts are handled by
14113 adjust_dynamic_symbol */
14114 gofarg.gotoff = gotoff;
14115 gofarg.info = info;
14116 elf_link_hash_traverse (elf_hash_table (info),
14117 elf_gc_allocate_got_offsets,
14118 &gofarg);
14119 return TRUE;
14120 }
14121
14122 /* Many folk need no more in the way of final link than this, once
14123 got entry reference counting is enabled. */
14124
14125 bfd_boolean
14126 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14127 {
14128 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14129 return FALSE;
14130
14131 /* Invoke the regular ELF backend linker to do all the work. */
14132 return bfd_elf_final_link (abfd, info);
14133 }
14134
14135 bfd_boolean
14136 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14137 {
14138 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
14139
14140 if (rcookie->bad_symtab)
14141 rcookie->rel = rcookie->rels;
14142
14143 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14144 {
14145 unsigned long r_symndx;
14146
14147 if (! rcookie->bad_symtab)
14148 if (rcookie->rel->r_offset > offset)
14149 return FALSE;
14150 if (rcookie->rel->r_offset != offset)
14151 continue;
14152
14153 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
14154 if (r_symndx == STN_UNDEF)
14155 return TRUE;
14156
14157 if (r_symndx >= rcookie->locsymcount
14158 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14159 {
14160 struct elf_link_hash_entry *h;
14161
14162 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14163
14164 while (h->root.type == bfd_link_hash_indirect
14165 || h->root.type == bfd_link_hash_warning)
14166 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14167
14168 if ((h->root.type == bfd_link_hash_defined
14169 || h->root.type == bfd_link_hash_defweak)
14170 && (h->root.u.def.section->owner != rcookie->abfd
14171 || h->root.u.def.section->kept_section != NULL
14172 || discarded_section (h->root.u.def.section)))
14173 return TRUE;
14174 }
14175 else
14176 {
14177 /* It's not a relocation against a global symbol,
14178 but it could be a relocation against a local
14179 symbol for a discarded section. */
14180 asection *isec;
14181 Elf_Internal_Sym *isym;
14182
14183 /* Need to: get the symbol; get the section. */
14184 isym = &rcookie->locsyms[r_symndx];
14185 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
14186 if (isec != NULL
14187 && (isec->kept_section != NULL
14188 || discarded_section (isec)))
14189 return TRUE;
14190 }
14191 return FALSE;
14192 }
14193 return FALSE;
14194 }
14195
14196 /* Discard unneeded references to discarded sections.
14197 Returns -1 on error, 1 if any section's size was changed, 0 if
14198 nothing changed. This function assumes that the relocations are in
14199 sorted order, which is true for all known assemblers. */
14200
14201 int
14202 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14203 {
14204 struct elf_reloc_cookie cookie;
14205 asection *o;
14206 bfd *abfd;
14207 int changed = 0;
14208
14209 if (info->traditional_format
14210 || !is_elf_hash_table (info->hash))
14211 return 0;
14212
14213 o = bfd_get_section_by_name (output_bfd, ".stab");
14214 if (o != NULL)
14215 {
14216 asection *i;
14217
14218 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14219 {
14220 if (i->size == 0
14221 || i->reloc_count == 0
14222 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14223 continue;
14224
14225 abfd = i->owner;
14226 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14227 continue;
14228
14229 if (!init_reloc_cookie_for_section (&cookie, info, i))
14230 return -1;
14231
14232 if (_bfd_discard_section_stabs (abfd, i,
14233 elf_section_data (i)->sec_info,
14234 bfd_elf_reloc_symbol_deleted_p,
14235 &cookie))
14236 changed = 1;
14237
14238 fini_reloc_cookie_for_section (&cookie, i);
14239 }
14240 }
14241
14242 o = NULL;
14243 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14244 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
14245 if (o != NULL)
14246 {
14247 asection *i;
14248 int eh_changed = 0;
14249 unsigned int eh_alignment;
14250
14251 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14252 {
14253 if (i->size == 0)
14254 continue;
14255
14256 abfd = i->owner;
14257 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14258 continue;
14259
14260 if (!init_reloc_cookie_for_section (&cookie, info, i))
14261 return -1;
14262
14263 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14264 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
14265 bfd_elf_reloc_symbol_deleted_p,
14266 &cookie))
14267 {
14268 eh_changed = 1;
14269 if (i->size != i->rawsize)
14270 changed = 1;
14271 }
14272
14273 fini_reloc_cookie_for_section (&cookie, i);
14274 }
14275
14276 eh_alignment = 1 << o->alignment_power;
14277 /* Skip over zero terminator, and prevent empty sections from
14278 adding alignment padding at the end. */
14279 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14280 if (i->size == 0)
14281 i->flags |= SEC_EXCLUDE;
14282 else if (i->size > 4)
14283 break;
14284 /* The last non-empty eh_frame section doesn't need padding. */
14285 if (i != NULL)
14286 i = i->map_tail.s;
14287 /* Any prior sections must pad the last FDE out to the output
14288 section alignment. Otherwise we might have zero padding
14289 between sections, which would be seen as a terminator. */
14290 for (; i != NULL; i = i->map_tail.s)
14291 if (i->size == 4)
14292 /* All but the last zero terminator should have been removed. */
14293 BFD_FAIL ();
14294 else
14295 {
14296 bfd_size_type size
14297 = (i->size + eh_alignment - 1) & -eh_alignment;
14298 if (i->size != size)
14299 {
14300 i->size = size;
14301 changed = 1;
14302 eh_changed = 1;
14303 }
14304 }
14305 if (eh_changed)
14306 elf_link_hash_traverse (elf_hash_table (info),
14307 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
14308 }
14309
14310 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14311 {
14312 const struct elf_backend_data *bed;
14313 asection *s;
14314
14315 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14316 continue;
14317 s = abfd->sections;
14318 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14319 continue;
14320
14321 bed = get_elf_backend_data (abfd);
14322
14323 if (bed->elf_backend_discard_info != NULL)
14324 {
14325 if (!init_reloc_cookie (&cookie, info, abfd))
14326 return -1;
14327
14328 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
14329 changed = 1;
14330
14331 fini_reloc_cookie (&cookie, abfd);
14332 }
14333 }
14334
14335 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14336 _bfd_elf_end_eh_frame_parsing (info);
14337
14338 if (info->eh_frame_hdr_type
14339 && !bfd_link_relocatable (info)
14340 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
14341 changed = 1;
14342
14343 return changed;
14344 }
14345
14346 bfd_boolean
14347 _bfd_elf_section_already_linked (bfd *abfd,
14348 asection *sec,
14349 struct bfd_link_info *info)
14350 {
14351 flagword flags;
14352 const char *name, *key;
14353 struct bfd_section_already_linked *l;
14354 struct bfd_section_already_linked_hash_entry *already_linked_list;
14355
14356 if (sec->output_section == bfd_abs_section_ptr)
14357 return FALSE;
14358
14359 flags = sec->flags;
14360
14361 /* Return if it isn't a linkonce section. A comdat group section
14362 also has SEC_LINK_ONCE set. */
14363 if ((flags & SEC_LINK_ONCE) == 0)
14364 return FALSE;
14365
14366 /* Don't put group member sections on our list of already linked
14367 sections. They are handled as a group via their group section. */
14368 if (elf_sec_group (sec) != NULL)
14369 return FALSE;
14370
14371 /* For a SHT_GROUP section, use the group signature as the key. */
14372 name = sec->name;
14373 if ((flags & SEC_GROUP) != 0
14374 && elf_next_in_group (sec) != NULL
14375 && elf_group_name (elf_next_in_group (sec)) != NULL)
14376 key = elf_group_name (elf_next_in_group (sec));
14377 else
14378 {
14379 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14380 if (CONST_STRNEQ (name, ".gnu.linkonce.")
14381 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14382 key++;
14383 else
14384 /* Must be a user linkonce section that doesn't follow gcc's
14385 naming convention. In this case we won't be matching
14386 single member groups. */
14387 key = name;
14388 }
14389
14390 already_linked_list = bfd_section_already_linked_table_lookup (key);
14391
14392 for (l = already_linked_list->entry; l != NULL; l = l->next)
14393 {
14394 /* We may have 2 different types of sections on the list: group
14395 sections with a signature of <key> (<key> is some string),
14396 and linkonce sections named .gnu.linkonce.<type>.<key>.
14397 Match like sections. LTO plugin sections are an exception.
14398 They are always named .gnu.linkonce.t.<key> and match either
14399 type of section. */
14400 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14401 && ((flags & SEC_GROUP) != 0
14402 || strcmp (name, l->sec->name) == 0))
14403 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
14404 {
14405 /* The section has already been linked. See if we should
14406 issue a warning. */
14407 if (!_bfd_handle_already_linked (sec, l, info))
14408 return FALSE;
14409
14410 if (flags & SEC_GROUP)
14411 {
14412 asection *first = elf_next_in_group (sec);
14413 asection *s = first;
14414
14415 while (s != NULL)
14416 {
14417 s->output_section = bfd_abs_section_ptr;
14418 /* Record which group discards it. */
14419 s->kept_section = l->sec;
14420 s = elf_next_in_group (s);
14421 /* These lists are circular. */
14422 if (s == first)
14423 break;
14424 }
14425 }
14426
14427 return TRUE;
14428 }
14429 }
14430
14431 /* A single member comdat group section may be discarded by a
14432 linkonce section and vice versa. */
14433 if ((flags & SEC_GROUP) != 0)
14434 {
14435 asection *first = elf_next_in_group (sec);
14436
14437 if (first != NULL && elf_next_in_group (first) == first)
14438 /* Check this single member group against linkonce sections. */
14439 for (l = already_linked_list->entry; l != NULL; l = l->next)
14440 if ((l->sec->flags & SEC_GROUP) == 0
14441 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14442 {
14443 first->output_section = bfd_abs_section_ptr;
14444 first->kept_section = l->sec;
14445 sec->output_section = bfd_abs_section_ptr;
14446 break;
14447 }
14448 }
14449 else
14450 /* Check this linkonce section against single member groups. */
14451 for (l = already_linked_list->entry; l != NULL; l = l->next)
14452 if (l->sec->flags & SEC_GROUP)
14453 {
14454 asection *first = elf_next_in_group (l->sec);
14455
14456 if (first != NULL
14457 && elf_next_in_group (first) == first
14458 && bfd_elf_match_symbols_in_sections (first, sec, info))
14459 {
14460 sec->output_section = bfd_abs_section_ptr;
14461 sec->kept_section = first;
14462 break;
14463 }
14464 }
14465
14466 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14467 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14468 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14469 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14470 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14471 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14472 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14473 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14474 The reverse order cannot happen as there is never a bfd with only the
14475 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14476 matter as here were are looking only for cross-bfd sections. */
14477
14478 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14479 for (l = already_linked_list->entry; l != NULL; l = l->next)
14480 if ((l->sec->flags & SEC_GROUP) == 0
14481 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14482 {
14483 if (abfd != l->sec->owner)
14484 sec->output_section = bfd_abs_section_ptr;
14485 break;
14486 }
14487
14488 /* This is the first section with this name. Record it. */
14489 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
14490 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
14491 return sec->output_section == bfd_abs_section_ptr;
14492 }
14493
14494 bfd_boolean
14495 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
14496 {
14497 return sym->st_shndx == SHN_COMMON;
14498 }
14499
14500 unsigned int
14501 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14502 {
14503 return SHN_COMMON;
14504 }
14505
14506 asection *
14507 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14508 {
14509 return bfd_com_section_ptr;
14510 }
14511
14512 bfd_vma
14513 _bfd_elf_default_got_elt_size (bfd *abfd,
14514 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14515 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14516 bfd *ibfd ATTRIBUTE_UNUSED,
14517 unsigned long symndx ATTRIBUTE_UNUSED)
14518 {
14519 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14520 return bed->s->arch_size / 8;
14521 }
14522
14523 /* Routines to support the creation of dynamic relocs. */
14524
14525 /* Returns the name of the dynamic reloc section associated with SEC. */
14526
14527 static const char *
14528 get_dynamic_reloc_section_name (bfd * abfd,
14529 asection * sec,
14530 bfd_boolean is_rela)
14531 {
14532 char *name;
14533 const char *old_name = bfd_section_name (sec);
14534 const char *prefix = is_rela ? ".rela" : ".rel";
14535
14536 if (old_name == NULL)
14537 return NULL;
14538
14539 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
14540 sprintf (name, "%s%s", prefix, old_name);
14541
14542 return name;
14543 }
14544
14545 /* Returns the dynamic reloc section associated with SEC.
14546 If necessary compute the name of the dynamic reloc section based
14547 on SEC's name (looked up in ABFD's string table) and the setting
14548 of IS_RELA. */
14549
14550 asection *
14551 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14552 asection * sec,
14553 bfd_boolean is_rela)
14554 {
14555 asection * reloc_sec = elf_section_data (sec)->sreloc;
14556
14557 if (reloc_sec == NULL)
14558 {
14559 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14560
14561 if (name != NULL)
14562 {
14563 reloc_sec = bfd_get_linker_section (abfd, name);
14564
14565 if (reloc_sec != NULL)
14566 elf_section_data (sec)->sreloc = reloc_sec;
14567 }
14568 }
14569
14570 return reloc_sec;
14571 }
14572
14573 /* Returns the dynamic reloc section associated with SEC. If the
14574 section does not exist it is created and attached to the DYNOBJ
14575 bfd and stored in the SRELOC field of SEC's elf_section_data
14576 structure.
14577
14578 ALIGNMENT is the alignment for the newly created section and
14579 IS_RELA defines whether the name should be .rela.<SEC's name>
14580 or .rel.<SEC's name>. The section name is looked up in the
14581 string table associated with ABFD. */
14582
14583 asection *
14584 _bfd_elf_make_dynamic_reloc_section (asection *sec,
14585 bfd *dynobj,
14586 unsigned int alignment,
14587 bfd *abfd,
14588 bfd_boolean is_rela)
14589 {
14590 asection * reloc_sec = elf_section_data (sec)->sreloc;
14591
14592 if (reloc_sec == NULL)
14593 {
14594 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14595
14596 if (name == NULL)
14597 return NULL;
14598
14599 reloc_sec = bfd_get_linker_section (dynobj, name);
14600
14601 if (reloc_sec == NULL)
14602 {
14603 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14604 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
14605 if ((sec->flags & SEC_ALLOC) != 0)
14606 flags |= SEC_ALLOC | SEC_LOAD;
14607
14608 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
14609 if (reloc_sec != NULL)
14610 {
14611 /* _bfd_elf_get_sec_type_attr chooses a section type by
14612 name. Override as it may be wrong, eg. for a user
14613 section named "auto" we'll get ".relauto" which is
14614 seen to be a .rela section. */
14615 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
14616 if (!bfd_set_section_alignment (reloc_sec, alignment))
14617 reloc_sec = NULL;
14618 }
14619 }
14620
14621 elf_section_data (sec)->sreloc = reloc_sec;
14622 }
14623
14624 return reloc_sec;
14625 }
14626
14627 /* Copy the ELF symbol type and other attributes for a linker script
14628 assignment from HSRC to HDEST. Generally this should be treated as
14629 if we found a strong non-dynamic definition for HDEST (except that
14630 ld ignores multiple definition errors). */
14631 void
14632 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14633 struct bfd_link_hash_entry *hdest,
14634 struct bfd_link_hash_entry *hsrc)
14635 {
14636 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14637 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14638 Elf_Internal_Sym isym;
14639
14640 ehdest->type = ehsrc->type;
14641 ehdest->target_internal = ehsrc->target_internal;
14642
14643 isym.st_other = ehsrc->other;
14644 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
14645 }
14646
14647 /* Append a RELA relocation REL to section S in BFD. */
14648
14649 void
14650 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14651 {
14652 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14653 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14654 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14655 bed->s->swap_reloca_out (abfd, rel, loc);
14656 }
14657
14658 /* Append a REL relocation REL to section S in BFD. */
14659
14660 void
14661 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14662 {
14663 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14664 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14665 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
14666 bed->s->swap_reloc_out (abfd, rel, loc);
14667 }
14668
14669 /* Define __start, __stop, .startof. or .sizeof. symbol. */
14670
14671 struct bfd_link_hash_entry *
14672 bfd_elf_define_start_stop (struct bfd_link_info *info,
14673 const char *symbol, asection *sec)
14674 {
14675 struct elf_link_hash_entry *h;
14676
14677 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14678 FALSE, FALSE, TRUE);
14679 if (h != NULL
14680 && (h->root.type == bfd_link_hash_undefined
14681 || h->root.type == bfd_link_hash_undefweak
14682 || ((h->ref_regular || h->def_dynamic) && !h->def_regular)))
14683 {
14684 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
14685 h->root.type = bfd_link_hash_defined;
14686 h->root.u.def.section = sec;
14687 h->root.u.def.value = 0;
14688 h->def_regular = 1;
14689 h->def_dynamic = 0;
14690 h->start_stop = 1;
14691 h->u2.start_stop_section = sec;
14692 if (symbol[0] == '.')
14693 {
14694 /* .startof. and .sizeof. symbols are local. */
14695 const struct elf_backend_data *bed;
14696 bed = get_elf_backend_data (info->output_bfd);
14697 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
14698 }
14699 else
14700 {
14701 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14702 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
14703 if (was_dynamic)
14704 bfd_elf_link_record_dynamic_symbol (info, h);
14705 }
14706 return &h->root;
14707 }
14708 return NULL;
14709 }
This page took 0.322684 seconds and 5 git commands to generate.