ld: Keep PREINIT_ARRAY/INIT_ARRAY/FINI_ARRAY sections for -r --gc-sections
[deliverable/binutils-gdb.git] / bfd / elflink.c
1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2018 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
20
21 #include "sysdep.h"
22 #include "bfd.h"
23 #include "bfd_stdint.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26 #define ARCH_SIZE 0
27 #include "elf-bfd.h"
28 #include "safe-ctype.h"
29 #include "libiberty.h"
30 #include "objalloc.h"
31 #if BFD_SUPPORTS_PLUGINS
32 #include "plugin-api.h"
33 #include "plugin.h"
34 #endif
35
36 /* This struct is used to pass information to routines called via
37 elf_link_hash_traverse which must return failure. */
38
39 struct elf_info_failed
40 {
41 struct bfd_link_info *info;
42 bfd_boolean failed;
43 };
44
45 /* This structure is used to pass information to
46 _bfd_elf_link_find_version_dependencies. */
47
48 struct elf_find_verdep_info
49 {
50 /* General link information. */
51 struct bfd_link_info *info;
52 /* The number of dependencies. */
53 unsigned int vers;
54 /* Whether we had a failure. */
55 bfd_boolean failed;
56 };
57
58 static bfd_boolean _bfd_elf_fix_symbol_flags
59 (struct elf_link_hash_entry *, struct elf_info_failed *);
60
61 asection *
62 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
63 unsigned long r_symndx,
64 bfd_boolean discard)
65 {
66 if (r_symndx >= cookie->locsymcount
67 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
68 {
69 struct elf_link_hash_entry *h;
70
71 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
72
73 while (h->root.type == bfd_link_hash_indirect
74 || h->root.type == bfd_link_hash_warning)
75 h = (struct elf_link_hash_entry *) h->root.u.i.link;
76
77 if ((h->root.type == bfd_link_hash_defined
78 || h->root.type == bfd_link_hash_defweak)
79 && discarded_section (h->root.u.def.section))
80 return h->root.u.def.section;
81 else
82 return NULL;
83 }
84 else
85 {
86 /* It's not a relocation against a global symbol,
87 but it could be a relocation against a local
88 symbol for a discarded section. */
89 asection *isec;
90 Elf_Internal_Sym *isym;
91
92 /* Need to: get the symbol; get the section. */
93 isym = &cookie->locsyms[r_symndx];
94 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
95 if (isec != NULL
96 && discard ? discarded_section (isec) : 1)
97 return isec;
98 }
99 return NULL;
100 }
101
102 /* Define a symbol in a dynamic linkage section. */
103
104 struct elf_link_hash_entry *
105 _bfd_elf_define_linkage_sym (bfd *abfd,
106 struct bfd_link_info *info,
107 asection *sec,
108 const char *name)
109 {
110 struct elf_link_hash_entry *h;
111 struct bfd_link_hash_entry *bh;
112 const struct elf_backend_data *bed;
113
114 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
115 if (h != NULL)
116 {
117 /* Zap symbol defined in an as-needed lib that wasn't linked.
118 This is a symptom of a larger problem: Absolute symbols
119 defined in shared libraries can't be overridden, because we
120 lose the link to the bfd which is via the symbol section. */
121 h->root.type = bfd_link_hash_new;
122 bh = &h->root;
123 }
124 else
125 bh = NULL;
126
127 bed = get_elf_backend_data (abfd);
128 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
129 sec, 0, NULL, FALSE, bed->collect,
130 &bh))
131 return NULL;
132 h = (struct elf_link_hash_entry *) bh;
133 BFD_ASSERT (h != NULL);
134 h->def_regular = 1;
135 h->non_elf = 0;
136 h->root.linker_def = 1;
137 h->type = STT_OBJECT;
138 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
139 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
140
141 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
142 return h;
143 }
144
145 bfd_boolean
146 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
147 {
148 flagword flags;
149 asection *s;
150 struct elf_link_hash_entry *h;
151 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
152 struct elf_link_hash_table *htab = elf_hash_table (info);
153
154 /* This function may be called more than once. */
155 if (htab->sgot != NULL)
156 return TRUE;
157
158 flags = bed->dynamic_sec_flags;
159
160 s = bfd_make_section_anyway_with_flags (abfd,
161 (bed->rela_plts_and_copies_p
162 ? ".rela.got" : ".rel.got"),
163 (bed->dynamic_sec_flags
164 | SEC_READONLY));
165 if (s == NULL
166 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
167 return FALSE;
168 htab->srelgot = s;
169
170 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
171 if (s == NULL
172 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
173 return FALSE;
174 htab->sgot = s;
175
176 if (bed->want_got_plt)
177 {
178 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
179 if (s == NULL
180 || !bfd_set_section_alignment (abfd, s,
181 bed->s->log_file_align))
182 return FALSE;
183 htab->sgotplt = s;
184 }
185
186 /* The first bit of the global offset table is the header. */
187 s->size += bed->got_header_size;
188
189 if (bed->want_got_sym)
190 {
191 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
192 (or .got.plt) section. We don't do this in the linker script
193 because we don't want to define the symbol if we are not creating
194 a global offset table. */
195 h = _bfd_elf_define_linkage_sym (abfd, info, s,
196 "_GLOBAL_OFFSET_TABLE_");
197 elf_hash_table (info)->hgot = h;
198 if (h == NULL)
199 return FALSE;
200 }
201
202 return TRUE;
203 }
204 \f
205 /* Create a strtab to hold the dynamic symbol names. */
206 static bfd_boolean
207 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
208 {
209 struct elf_link_hash_table *hash_table;
210
211 hash_table = elf_hash_table (info);
212 if (hash_table->dynobj == NULL)
213 {
214 /* We may not set dynobj, an input file holding linker created
215 dynamic sections to abfd, which may be a dynamic object with
216 its own dynamic sections. We need to find a normal input file
217 to hold linker created sections if possible. */
218 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
219 {
220 bfd *ibfd;
221 asection *s;
222 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
223 if ((ibfd->flags
224 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
225 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
226 && !((s = ibfd->sections) != NULL
227 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
228 {
229 abfd = ibfd;
230 break;
231 }
232 }
233 hash_table->dynobj = abfd;
234 }
235
236 if (hash_table->dynstr == NULL)
237 {
238 hash_table->dynstr = _bfd_elf_strtab_init ();
239 if (hash_table->dynstr == NULL)
240 return FALSE;
241 }
242 return TRUE;
243 }
244
245 /* Create some sections which will be filled in with dynamic linking
246 information. ABFD is an input file which requires dynamic sections
247 to be created. The dynamic sections take up virtual memory space
248 when the final executable is run, so we need to create them before
249 addresses are assigned to the output sections. We work out the
250 actual contents and size of these sections later. */
251
252 bfd_boolean
253 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
254 {
255 flagword flags;
256 asection *s;
257 const struct elf_backend_data *bed;
258 struct elf_link_hash_entry *h;
259
260 if (! is_elf_hash_table (info->hash))
261 return FALSE;
262
263 if (elf_hash_table (info)->dynamic_sections_created)
264 return TRUE;
265
266 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
267 return FALSE;
268
269 abfd = elf_hash_table (info)->dynobj;
270 bed = get_elf_backend_data (abfd);
271
272 flags = bed->dynamic_sec_flags;
273
274 /* A dynamically linked executable has a .interp section, but a
275 shared library does not. */
276 if (bfd_link_executable (info) && !info->nointerp)
277 {
278 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
279 flags | SEC_READONLY);
280 if (s == NULL)
281 return FALSE;
282 }
283
284 /* Create sections to hold version informations. These are removed
285 if they are not needed. */
286 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
287 flags | SEC_READONLY);
288 if (s == NULL
289 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
290 return FALSE;
291
292 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
293 flags | SEC_READONLY);
294 if (s == NULL
295 || ! bfd_set_section_alignment (abfd, s, 1))
296 return FALSE;
297
298 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
299 flags | SEC_READONLY);
300 if (s == NULL
301 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
302 return FALSE;
303
304 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
305 flags | SEC_READONLY);
306 if (s == NULL
307 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
308 return FALSE;
309 elf_hash_table (info)->dynsym = s;
310
311 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
312 flags | SEC_READONLY);
313 if (s == NULL)
314 return FALSE;
315
316 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
317 if (s == NULL
318 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
319 return FALSE;
320
321 /* The special symbol _DYNAMIC is always set to the start of the
322 .dynamic section. We could set _DYNAMIC in a linker script, but we
323 only want to define it if we are, in fact, creating a .dynamic
324 section. We don't want to define it if there is no .dynamic
325 section, since on some ELF platforms the start up code examines it
326 to decide how to initialize the process. */
327 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
328 elf_hash_table (info)->hdynamic = h;
329 if (h == NULL)
330 return FALSE;
331
332 if (info->emit_hash)
333 {
334 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
335 flags | SEC_READONLY);
336 if (s == NULL
337 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
338 return FALSE;
339 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
340 }
341
342 if (info->emit_gnu_hash)
343 {
344 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
345 flags | SEC_READONLY);
346 if (s == NULL
347 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
348 return FALSE;
349 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
350 4 32-bit words followed by variable count of 64-bit words, then
351 variable count of 32-bit words. */
352 if (bed->s->arch_size == 64)
353 elf_section_data (s)->this_hdr.sh_entsize = 0;
354 else
355 elf_section_data (s)->this_hdr.sh_entsize = 4;
356 }
357
358 /* Let the backend create the rest of the sections. This lets the
359 backend set the right flags. The backend will normally create
360 the .got and .plt sections. */
361 if (bed->elf_backend_create_dynamic_sections == NULL
362 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
363 return FALSE;
364
365 elf_hash_table (info)->dynamic_sections_created = TRUE;
366
367 return TRUE;
368 }
369
370 /* Create dynamic sections when linking against a dynamic object. */
371
372 bfd_boolean
373 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
374 {
375 flagword flags, pltflags;
376 struct elf_link_hash_entry *h;
377 asection *s;
378 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
379 struct elf_link_hash_table *htab = elf_hash_table (info);
380
381 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
382 .rel[a].bss sections. */
383 flags = bed->dynamic_sec_flags;
384
385 pltflags = flags;
386 if (bed->plt_not_loaded)
387 /* We do not clear SEC_ALLOC here because we still want the OS to
388 allocate space for the section; it's just that there's nothing
389 to read in from the object file. */
390 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
391 else
392 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
393 if (bed->plt_readonly)
394 pltflags |= SEC_READONLY;
395
396 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
397 if (s == NULL
398 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
399 return FALSE;
400 htab->splt = s;
401
402 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
403 .plt section. */
404 if (bed->want_plt_sym)
405 {
406 h = _bfd_elf_define_linkage_sym (abfd, info, s,
407 "_PROCEDURE_LINKAGE_TABLE_");
408 elf_hash_table (info)->hplt = h;
409 if (h == NULL)
410 return FALSE;
411 }
412
413 s = bfd_make_section_anyway_with_flags (abfd,
414 (bed->rela_plts_and_copies_p
415 ? ".rela.plt" : ".rel.plt"),
416 flags | SEC_READONLY);
417 if (s == NULL
418 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
419 return FALSE;
420 htab->srelplt = s;
421
422 if (! _bfd_elf_create_got_section (abfd, info))
423 return FALSE;
424
425 if (bed->want_dynbss)
426 {
427 /* The .dynbss section is a place to put symbols which are defined
428 by dynamic objects, are referenced by regular objects, and are
429 not functions. We must allocate space for them in the process
430 image and use a R_*_COPY reloc to tell the dynamic linker to
431 initialize them at run time. The linker script puts the .dynbss
432 section into the .bss section of the final image. */
433 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
434 SEC_ALLOC | SEC_LINKER_CREATED);
435 if (s == NULL)
436 return FALSE;
437 htab->sdynbss = s;
438
439 if (bed->want_dynrelro)
440 {
441 /* Similarly, but for symbols that were originally in read-only
442 sections. This section doesn't really need to have contents,
443 but make it like other .data.rel.ro sections. */
444 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
445 flags);
446 if (s == NULL)
447 return FALSE;
448 htab->sdynrelro = s;
449 }
450
451 /* The .rel[a].bss section holds copy relocs. This section is not
452 normally needed. We need to create it here, though, so that the
453 linker will map it to an output section. We can't just create it
454 only if we need it, because we will not know whether we need it
455 until we have seen all the input files, and the first time the
456 main linker code calls BFD after examining all the input files
457 (size_dynamic_sections) the input sections have already been
458 mapped to the output sections. If the section turns out not to
459 be needed, we can discard it later. We will never need this
460 section when generating a shared object, since they do not use
461 copy relocs. */
462 if (bfd_link_executable (info))
463 {
464 s = bfd_make_section_anyway_with_flags (abfd,
465 (bed->rela_plts_and_copies_p
466 ? ".rela.bss" : ".rel.bss"),
467 flags | SEC_READONLY);
468 if (s == NULL
469 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
470 return FALSE;
471 htab->srelbss = s;
472
473 if (bed->want_dynrelro)
474 {
475 s = (bfd_make_section_anyway_with_flags
476 (abfd, (bed->rela_plts_and_copies_p
477 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
478 flags | SEC_READONLY));
479 if (s == NULL
480 || ! bfd_set_section_alignment (abfd, s,
481 bed->s->log_file_align))
482 return FALSE;
483 htab->sreldynrelro = s;
484 }
485 }
486 }
487
488 return TRUE;
489 }
490 \f
491 /* Record a new dynamic symbol. We record the dynamic symbols as we
492 read the input files, since we need to have a list of all of them
493 before we can determine the final sizes of the output sections.
494 Note that we may actually call this function even though we are not
495 going to output any dynamic symbols; in some cases we know that a
496 symbol should be in the dynamic symbol table, but only if there is
497 one. */
498
499 bfd_boolean
500 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
501 struct elf_link_hash_entry *h)
502 {
503 if (h->dynindx == -1)
504 {
505 struct elf_strtab_hash *dynstr;
506 char *p;
507 const char *name;
508 size_t indx;
509
510 /* XXX: The ABI draft says the linker must turn hidden and
511 internal symbols into STB_LOCAL symbols when producing the
512 DSO. However, if ld.so honors st_other in the dynamic table,
513 this would not be necessary. */
514 switch (ELF_ST_VISIBILITY (h->other))
515 {
516 case STV_INTERNAL:
517 case STV_HIDDEN:
518 if (h->root.type != bfd_link_hash_undefined
519 && h->root.type != bfd_link_hash_undefweak)
520 {
521 h->forced_local = 1;
522 if (!elf_hash_table (info)->is_relocatable_executable)
523 return TRUE;
524 }
525
526 default:
527 break;
528 }
529
530 h->dynindx = elf_hash_table (info)->dynsymcount;
531 ++elf_hash_table (info)->dynsymcount;
532
533 dynstr = elf_hash_table (info)->dynstr;
534 if (dynstr == NULL)
535 {
536 /* Create a strtab to hold the dynamic symbol names. */
537 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
538 if (dynstr == NULL)
539 return FALSE;
540 }
541
542 /* We don't put any version information in the dynamic string
543 table. */
544 name = h->root.root.string;
545 p = strchr (name, ELF_VER_CHR);
546 if (p != NULL)
547 /* We know that the p points into writable memory. In fact,
548 there are only a few symbols that have read-only names, being
549 those like _GLOBAL_OFFSET_TABLE_ that are created specially
550 by the backends. Most symbols will have names pointing into
551 an ELF string table read from a file, or to objalloc memory. */
552 *p = 0;
553
554 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
555
556 if (p != NULL)
557 *p = ELF_VER_CHR;
558
559 if (indx == (size_t) -1)
560 return FALSE;
561 h->dynstr_index = indx;
562 }
563
564 return TRUE;
565 }
566 \f
567 /* Mark a symbol dynamic. */
568
569 static void
570 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
571 struct elf_link_hash_entry *h,
572 Elf_Internal_Sym *sym)
573 {
574 struct bfd_elf_dynamic_list *d = info->dynamic_list;
575
576 /* It may be called more than once on the same H. */
577 if(h->dynamic || bfd_link_relocatable (info))
578 return;
579
580 if ((info->dynamic_data
581 && (h->type == STT_OBJECT
582 || h->type == STT_COMMON
583 || (sym != NULL
584 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
585 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
586 || (d != NULL
587 && h->non_elf
588 && (*d->match) (&d->head, NULL, h->root.root.string)))
589 h->dynamic = 1;
590 }
591
592 /* Record an assignment to a symbol made by a linker script. We need
593 this in case some dynamic object refers to this symbol. */
594
595 bfd_boolean
596 bfd_elf_record_link_assignment (bfd *output_bfd,
597 struct bfd_link_info *info,
598 const char *name,
599 bfd_boolean provide,
600 bfd_boolean hidden)
601 {
602 struct elf_link_hash_entry *h, *hv;
603 struct elf_link_hash_table *htab;
604 const struct elf_backend_data *bed;
605
606 if (!is_elf_hash_table (info->hash))
607 return TRUE;
608
609 htab = elf_hash_table (info);
610 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
611 if (h == NULL)
612 return provide;
613
614 if (h->root.type == bfd_link_hash_warning)
615 h = (struct elf_link_hash_entry *) h->root.u.i.link;
616
617 if (h->versioned == unknown)
618 {
619 /* Set versioned if symbol version is unknown. */
620 char *version = strrchr (name, ELF_VER_CHR);
621 if (version)
622 {
623 if (version > name && version[-1] != ELF_VER_CHR)
624 h->versioned = versioned_hidden;
625 else
626 h->versioned = versioned;
627 }
628 }
629
630 /* Symbols defined in a linker script but not referenced anywhere
631 else will have non_elf set. */
632 if (h->non_elf)
633 {
634 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
635 h->non_elf = 0;
636 }
637
638 switch (h->root.type)
639 {
640 case bfd_link_hash_defined:
641 case bfd_link_hash_defweak:
642 case bfd_link_hash_common:
643 break;
644 case bfd_link_hash_undefweak:
645 case bfd_link_hash_undefined:
646 /* Since we're defining the symbol, don't let it seem to have not
647 been defined. record_dynamic_symbol and size_dynamic_sections
648 may depend on this. */
649 h->root.type = bfd_link_hash_new;
650 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
651 bfd_link_repair_undef_list (&htab->root);
652 break;
653 case bfd_link_hash_new:
654 break;
655 case bfd_link_hash_indirect:
656 /* We had a versioned symbol in a dynamic library. We make the
657 the versioned symbol point to this one. */
658 bed = get_elf_backend_data (output_bfd);
659 hv = h;
660 while (hv->root.type == bfd_link_hash_indirect
661 || hv->root.type == bfd_link_hash_warning)
662 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
663 /* We don't need to update h->root.u since linker will set them
664 later. */
665 h->root.type = bfd_link_hash_undefined;
666 hv->root.type = bfd_link_hash_indirect;
667 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
668 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
669 break;
670 default:
671 BFD_FAIL ();
672 return FALSE;
673 }
674
675 /* If this symbol is being provided by the linker script, and it is
676 currently defined by a dynamic object, but not by a regular
677 object, then mark it as undefined so that the generic linker will
678 force the correct value. */
679 if (provide
680 && h->def_dynamic
681 && !h->def_regular)
682 h->root.type = bfd_link_hash_undefined;
683
684 /* If this symbol is not being provided by the linker script, and it is
685 currently defined by a dynamic object, but not by a regular object,
686 then clear out any version information because the symbol will not be
687 associated with the dynamic object any more. */
688 if (!provide
689 && h->def_dynamic
690 && !h->def_regular)
691 h->verinfo.verdef = NULL;
692
693 /* Make sure this symbol is not garbage collected. */
694 h->mark = 1;
695
696 h->def_regular = 1;
697
698 if (hidden)
699 {
700 bed = get_elf_backend_data (output_bfd);
701 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
702 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
703 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
704 }
705
706 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
707 and executables. */
708 if (!bfd_link_relocatable (info)
709 && h->dynindx != -1
710 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
711 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
712 h->forced_local = 1;
713
714 if ((h->def_dynamic
715 || h->ref_dynamic
716 || bfd_link_dll (info)
717 || elf_hash_table (info)->is_relocatable_executable)
718 && h->dynindx == -1)
719 {
720 if (! bfd_elf_link_record_dynamic_symbol (info, h))
721 return FALSE;
722
723 /* If this is a weak defined symbol, and we know a corresponding
724 real symbol from the same dynamic object, make sure the real
725 symbol is also made into a dynamic symbol. */
726 if (h->is_weakalias)
727 {
728 struct elf_link_hash_entry *def = weakdef (h);
729
730 if (def->dynindx == -1
731 && !bfd_elf_link_record_dynamic_symbol (info, def))
732 return FALSE;
733 }
734 }
735
736 return TRUE;
737 }
738
739 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
740 success, and 2 on a failure caused by attempting to record a symbol
741 in a discarded section, eg. a discarded link-once section symbol. */
742
743 int
744 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
745 bfd *input_bfd,
746 long input_indx)
747 {
748 bfd_size_type amt;
749 struct elf_link_local_dynamic_entry *entry;
750 struct elf_link_hash_table *eht;
751 struct elf_strtab_hash *dynstr;
752 size_t dynstr_index;
753 char *name;
754 Elf_External_Sym_Shndx eshndx;
755 char esym[sizeof (Elf64_External_Sym)];
756
757 if (! is_elf_hash_table (info->hash))
758 return 0;
759
760 /* See if the entry exists already. */
761 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
762 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
763 return 1;
764
765 amt = sizeof (*entry);
766 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
767 if (entry == NULL)
768 return 0;
769
770 /* Go find the symbol, so that we can find it's name. */
771 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
772 1, input_indx, &entry->isym, esym, &eshndx))
773 {
774 bfd_release (input_bfd, entry);
775 return 0;
776 }
777
778 if (entry->isym.st_shndx != SHN_UNDEF
779 && entry->isym.st_shndx < SHN_LORESERVE)
780 {
781 asection *s;
782
783 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
784 if (s == NULL || bfd_is_abs_section (s->output_section))
785 {
786 /* We can still bfd_release here as nothing has done another
787 bfd_alloc. We can't do this later in this function. */
788 bfd_release (input_bfd, entry);
789 return 2;
790 }
791 }
792
793 name = (bfd_elf_string_from_elf_section
794 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
795 entry->isym.st_name));
796
797 dynstr = elf_hash_table (info)->dynstr;
798 if (dynstr == NULL)
799 {
800 /* Create a strtab to hold the dynamic symbol names. */
801 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
802 if (dynstr == NULL)
803 return 0;
804 }
805
806 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
807 if (dynstr_index == (size_t) -1)
808 return 0;
809 entry->isym.st_name = dynstr_index;
810
811 eht = elf_hash_table (info);
812
813 entry->next = eht->dynlocal;
814 eht->dynlocal = entry;
815 entry->input_bfd = input_bfd;
816 entry->input_indx = input_indx;
817 eht->dynsymcount++;
818
819 /* Whatever binding the symbol had before, it's now local. */
820 entry->isym.st_info
821 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
822
823 /* The dynindx will be set at the end of size_dynamic_sections. */
824
825 return 1;
826 }
827
828 /* Return the dynindex of a local dynamic symbol. */
829
830 long
831 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
832 bfd *input_bfd,
833 long input_indx)
834 {
835 struct elf_link_local_dynamic_entry *e;
836
837 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
838 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
839 return e->dynindx;
840 return -1;
841 }
842
843 /* This function is used to renumber the dynamic symbols, if some of
844 them are removed because they are marked as local. This is called
845 via elf_link_hash_traverse. */
846
847 static bfd_boolean
848 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
849 void *data)
850 {
851 size_t *count = (size_t *) data;
852
853 if (h->forced_local)
854 return TRUE;
855
856 if (h->dynindx != -1)
857 h->dynindx = ++(*count);
858
859 return TRUE;
860 }
861
862
863 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
864 STB_LOCAL binding. */
865
866 static bfd_boolean
867 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
868 void *data)
869 {
870 size_t *count = (size_t *) data;
871
872 if (!h->forced_local)
873 return TRUE;
874
875 if (h->dynindx != -1)
876 h->dynindx = ++(*count);
877
878 return TRUE;
879 }
880
881 /* Return true if the dynamic symbol for a given section should be
882 omitted when creating a shared library. */
883 bfd_boolean
884 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
885 struct bfd_link_info *info,
886 asection *p)
887 {
888 struct elf_link_hash_table *htab;
889 asection *ip;
890
891 switch (elf_section_data (p)->this_hdr.sh_type)
892 {
893 case SHT_PROGBITS:
894 case SHT_NOBITS:
895 /* If sh_type is yet undecided, assume it could be
896 SHT_PROGBITS/SHT_NOBITS. */
897 case SHT_NULL:
898 htab = elf_hash_table (info);
899 if (p == htab->tls_sec)
900 return FALSE;
901
902 if (htab->text_index_section != NULL)
903 return p != htab->text_index_section && p != htab->data_index_section;
904
905 return (htab->dynobj != NULL
906 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
907 && ip->output_section == p);
908
909 /* There shouldn't be section relative relocations
910 against any other section. */
911 default:
912 return TRUE;
913 }
914 }
915
916 /* Assign dynsym indices. In a shared library we generate a section
917 symbol for each output section, which come first. Next come symbols
918 which have been forced to local binding. Then all of the back-end
919 allocated local dynamic syms, followed by the rest of the global
920 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
921 (This prevents the early call before elf_backend_init_index_section
922 and strip_excluded_output_sections setting dynindx for sections
923 that are stripped.) */
924
925 static unsigned long
926 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
927 struct bfd_link_info *info,
928 unsigned long *section_sym_count)
929 {
930 unsigned long dynsymcount = 0;
931 bfd_boolean do_sec = section_sym_count != NULL;
932
933 if (bfd_link_pic (info)
934 || elf_hash_table (info)->is_relocatable_executable)
935 {
936 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
937 asection *p;
938 for (p = output_bfd->sections; p ; p = p->next)
939 if ((p->flags & SEC_EXCLUDE) == 0
940 && (p->flags & SEC_ALLOC) != 0
941 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
942 {
943 ++dynsymcount;
944 if (do_sec)
945 elf_section_data (p)->dynindx = dynsymcount;
946 }
947 else if (do_sec)
948 elf_section_data (p)->dynindx = 0;
949 }
950 if (do_sec)
951 *section_sym_count = dynsymcount;
952
953 elf_link_hash_traverse (elf_hash_table (info),
954 elf_link_renumber_local_hash_table_dynsyms,
955 &dynsymcount);
956
957 if (elf_hash_table (info)->dynlocal)
958 {
959 struct elf_link_local_dynamic_entry *p;
960 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
961 p->dynindx = ++dynsymcount;
962 }
963 elf_hash_table (info)->local_dynsymcount = dynsymcount;
964
965 elf_link_hash_traverse (elf_hash_table (info),
966 elf_link_renumber_hash_table_dynsyms,
967 &dynsymcount);
968
969 /* There is an unused NULL entry at the head of the table which we
970 must account for in our count even if the table is empty since it
971 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
972 .dynamic section. */
973 dynsymcount++;
974
975 elf_hash_table (info)->dynsymcount = dynsymcount;
976 return dynsymcount;
977 }
978
979 /* Merge st_other field. */
980
981 static void
982 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
983 const Elf_Internal_Sym *isym, asection *sec,
984 bfd_boolean definition, bfd_boolean dynamic)
985 {
986 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
987
988 /* If st_other has a processor-specific meaning, specific
989 code might be needed here. */
990 if (bed->elf_backend_merge_symbol_attribute)
991 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
992 dynamic);
993
994 if (!dynamic)
995 {
996 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
997 unsigned hvis = ELF_ST_VISIBILITY (h->other);
998
999 /* Keep the most constraining visibility. Leave the remainder
1000 of the st_other field to elf_backend_merge_symbol_attribute. */
1001 if (symvis - 1 < hvis - 1)
1002 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1003 }
1004 else if (definition
1005 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
1006 && (sec->flags & SEC_READONLY) == 0)
1007 h->protected_def = 1;
1008 }
1009
1010 /* This function is called when we want to merge a new symbol with an
1011 existing symbol. It handles the various cases which arise when we
1012 find a definition in a dynamic object, or when there is already a
1013 definition in a dynamic object. The new symbol is described by
1014 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1015 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1016 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1017 of an old common symbol. We set OVERRIDE if the old symbol is
1018 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1019 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1020 to change. By OK to change, we mean that we shouldn't warn if the
1021 type or size does change. */
1022
1023 static bfd_boolean
1024 _bfd_elf_merge_symbol (bfd *abfd,
1025 struct bfd_link_info *info,
1026 const char *name,
1027 Elf_Internal_Sym *sym,
1028 asection **psec,
1029 bfd_vma *pvalue,
1030 struct elf_link_hash_entry **sym_hash,
1031 bfd **poldbfd,
1032 bfd_boolean *pold_weak,
1033 unsigned int *pold_alignment,
1034 bfd_boolean *skip,
1035 bfd_boolean *override,
1036 bfd_boolean *type_change_ok,
1037 bfd_boolean *size_change_ok,
1038 bfd_boolean *matched)
1039 {
1040 asection *sec, *oldsec;
1041 struct elf_link_hash_entry *h;
1042 struct elf_link_hash_entry *hi;
1043 struct elf_link_hash_entry *flip;
1044 int bind;
1045 bfd *oldbfd;
1046 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1047 bfd_boolean newweak, oldweak, newfunc, oldfunc;
1048 const struct elf_backend_data *bed;
1049 char *new_version;
1050 bfd_boolean default_sym = *matched;
1051
1052 *skip = FALSE;
1053 *override = FALSE;
1054
1055 sec = *psec;
1056 bind = ELF_ST_BIND (sym->st_info);
1057
1058 if (! bfd_is_und_section (sec))
1059 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1060 else
1061 h = ((struct elf_link_hash_entry *)
1062 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1063 if (h == NULL)
1064 return FALSE;
1065 *sym_hash = h;
1066
1067 bed = get_elf_backend_data (abfd);
1068
1069 /* NEW_VERSION is the symbol version of the new symbol. */
1070 if (h->versioned != unversioned)
1071 {
1072 /* Symbol version is unknown or versioned. */
1073 new_version = strrchr (name, ELF_VER_CHR);
1074 if (new_version)
1075 {
1076 if (h->versioned == unknown)
1077 {
1078 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1079 h->versioned = versioned_hidden;
1080 else
1081 h->versioned = versioned;
1082 }
1083 new_version += 1;
1084 if (new_version[0] == '\0')
1085 new_version = NULL;
1086 }
1087 else
1088 h->versioned = unversioned;
1089 }
1090 else
1091 new_version = NULL;
1092
1093 /* For merging, we only care about real symbols. But we need to make
1094 sure that indirect symbol dynamic flags are updated. */
1095 hi = h;
1096 while (h->root.type == bfd_link_hash_indirect
1097 || h->root.type == bfd_link_hash_warning)
1098 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1099
1100 if (!*matched)
1101 {
1102 if (hi == h || h->root.type == bfd_link_hash_new)
1103 *matched = TRUE;
1104 else
1105 {
1106 /* OLD_HIDDEN is true if the existing symbol is only visible
1107 to the symbol with the same symbol version. NEW_HIDDEN is
1108 true if the new symbol is only visible to the symbol with
1109 the same symbol version. */
1110 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1111 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1112 if (!old_hidden && !new_hidden)
1113 /* The new symbol matches the existing symbol if both
1114 aren't hidden. */
1115 *matched = TRUE;
1116 else
1117 {
1118 /* OLD_VERSION is the symbol version of the existing
1119 symbol. */
1120 char *old_version;
1121
1122 if (h->versioned >= versioned)
1123 old_version = strrchr (h->root.root.string,
1124 ELF_VER_CHR) + 1;
1125 else
1126 old_version = NULL;
1127
1128 /* The new symbol matches the existing symbol if they
1129 have the same symbol version. */
1130 *matched = (old_version == new_version
1131 || (old_version != NULL
1132 && new_version != NULL
1133 && strcmp (old_version, new_version) == 0));
1134 }
1135 }
1136 }
1137
1138 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1139 existing symbol. */
1140
1141 oldbfd = NULL;
1142 oldsec = NULL;
1143 switch (h->root.type)
1144 {
1145 default:
1146 break;
1147
1148 case bfd_link_hash_undefined:
1149 case bfd_link_hash_undefweak:
1150 oldbfd = h->root.u.undef.abfd;
1151 break;
1152
1153 case bfd_link_hash_defined:
1154 case bfd_link_hash_defweak:
1155 oldbfd = h->root.u.def.section->owner;
1156 oldsec = h->root.u.def.section;
1157 break;
1158
1159 case bfd_link_hash_common:
1160 oldbfd = h->root.u.c.p->section->owner;
1161 oldsec = h->root.u.c.p->section;
1162 if (pold_alignment)
1163 *pold_alignment = h->root.u.c.p->alignment_power;
1164 break;
1165 }
1166 if (poldbfd && *poldbfd == NULL)
1167 *poldbfd = oldbfd;
1168
1169 /* Differentiate strong and weak symbols. */
1170 newweak = bind == STB_WEAK;
1171 oldweak = (h->root.type == bfd_link_hash_defweak
1172 || h->root.type == bfd_link_hash_undefweak);
1173 if (pold_weak)
1174 *pold_weak = oldweak;
1175
1176 /* We have to check it for every instance since the first few may be
1177 references and not all compilers emit symbol type for undefined
1178 symbols. */
1179 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1180
1181 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1182 respectively, is from a dynamic object. */
1183
1184 newdyn = (abfd->flags & DYNAMIC) != 0;
1185
1186 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1187 syms and defined syms in dynamic libraries respectively.
1188 ref_dynamic on the other hand can be set for a symbol defined in
1189 a dynamic library, and def_dynamic may not be set; When the
1190 definition in a dynamic lib is overridden by a definition in the
1191 executable use of the symbol in the dynamic lib becomes a
1192 reference to the executable symbol. */
1193 if (newdyn)
1194 {
1195 if (bfd_is_und_section (sec))
1196 {
1197 if (bind != STB_WEAK)
1198 {
1199 h->ref_dynamic_nonweak = 1;
1200 hi->ref_dynamic_nonweak = 1;
1201 }
1202 }
1203 else
1204 {
1205 /* Update the existing symbol only if they match. */
1206 if (*matched)
1207 h->dynamic_def = 1;
1208 hi->dynamic_def = 1;
1209 }
1210 }
1211
1212 /* If we just created the symbol, mark it as being an ELF symbol.
1213 Other than that, there is nothing to do--there is no merge issue
1214 with a newly defined symbol--so we just return. */
1215
1216 if (h->root.type == bfd_link_hash_new)
1217 {
1218 h->non_elf = 0;
1219 return TRUE;
1220 }
1221
1222 /* In cases involving weak versioned symbols, we may wind up trying
1223 to merge a symbol with itself. Catch that here, to avoid the
1224 confusion that results if we try to override a symbol with
1225 itself. The additional tests catch cases like
1226 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1227 dynamic object, which we do want to handle here. */
1228 if (abfd == oldbfd
1229 && (newweak || oldweak)
1230 && ((abfd->flags & DYNAMIC) == 0
1231 || !h->def_regular))
1232 return TRUE;
1233
1234 olddyn = FALSE;
1235 if (oldbfd != NULL)
1236 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1237 else if (oldsec != NULL)
1238 {
1239 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1240 indices used by MIPS ELF. */
1241 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1242 }
1243
1244 /* Handle a case where plugin_notice won't be called and thus won't
1245 set the non_ir_ref flags on the first pass over symbols. */
1246 if (oldbfd != NULL
1247 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1248 && newdyn != olddyn)
1249 {
1250 h->root.non_ir_ref_dynamic = TRUE;
1251 hi->root.non_ir_ref_dynamic = TRUE;
1252 }
1253
1254 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1255 respectively, appear to be a definition rather than reference. */
1256
1257 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1258
1259 olddef = (h->root.type != bfd_link_hash_undefined
1260 && h->root.type != bfd_link_hash_undefweak
1261 && h->root.type != bfd_link_hash_common);
1262
1263 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1264 respectively, appear to be a function. */
1265
1266 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1267 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1268
1269 oldfunc = (h->type != STT_NOTYPE
1270 && bed->is_function_type (h->type));
1271
1272 if (!(newfunc && oldfunc)
1273 && ELF_ST_TYPE (sym->st_info) != h->type
1274 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1275 && h->type != STT_NOTYPE
1276 && (newdef || bfd_is_com_section (sec))
1277 && (olddef || h->root.type == bfd_link_hash_common))
1278 {
1279 /* If creating a default indirect symbol ("foo" or "foo@") from
1280 a dynamic versioned definition ("foo@@") skip doing so if
1281 there is an existing regular definition with a different
1282 type. We don't want, for example, a "time" variable in the
1283 executable overriding a "time" function in a shared library. */
1284 if (newdyn
1285 && !olddyn)
1286 {
1287 *skip = TRUE;
1288 return TRUE;
1289 }
1290
1291 /* When adding a symbol from a regular object file after we have
1292 created indirect symbols, undo the indirection and any
1293 dynamic state. */
1294 if (hi != h
1295 && !newdyn
1296 && olddyn)
1297 {
1298 h = hi;
1299 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1300 h->forced_local = 0;
1301 h->ref_dynamic = 0;
1302 h->def_dynamic = 0;
1303 h->dynamic_def = 0;
1304 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1305 {
1306 h->root.type = bfd_link_hash_undefined;
1307 h->root.u.undef.abfd = abfd;
1308 }
1309 else
1310 {
1311 h->root.type = bfd_link_hash_new;
1312 h->root.u.undef.abfd = NULL;
1313 }
1314 return TRUE;
1315 }
1316 }
1317
1318 /* Check TLS symbols. We don't check undefined symbols introduced
1319 by "ld -u" which have no type (and oldbfd NULL), and we don't
1320 check symbols from plugins because they also have no type. */
1321 if (oldbfd != NULL
1322 && (oldbfd->flags & BFD_PLUGIN) == 0
1323 && (abfd->flags & BFD_PLUGIN) == 0
1324 && ELF_ST_TYPE (sym->st_info) != h->type
1325 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1326 {
1327 bfd *ntbfd, *tbfd;
1328 bfd_boolean ntdef, tdef;
1329 asection *ntsec, *tsec;
1330
1331 if (h->type == STT_TLS)
1332 {
1333 ntbfd = abfd;
1334 ntsec = sec;
1335 ntdef = newdef;
1336 tbfd = oldbfd;
1337 tsec = oldsec;
1338 tdef = olddef;
1339 }
1340 else
1341 {
1342 ntbfd = oldbfd;
1343 ntsec = oldsec;
1344 ntdef = olddef;
1345 tbfd = abfd;
1346 tsec = sec;
1347 tdef = newdef;
1348 }
1349
1350 if (tdef && ntdef)
1351 _bfd_error_handler
1352 /* xgettext:c-format */
1353 (_("%s: TLS definition in %B section %A "
1354 "mismatches non-TLS definition in %B section %A"),
1355 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1356 else if (!tdef && !ntdef)
1357 _bfd_error_handler
1358 /* xgettext:c-format */
1359 (_("%s: TLS reference in %B "
1360 "mismatches non-TLS reference in %B"),
1361 h->root.root.string, tbfd, ntbfd);
1362 else if (tdef)
1363 _bfd_error_handler
1364 /* xgettext:c-format */
1365 (_("%s: TLS definition in %B section %A "
1366 "mismatches non-TLS reference in %B"),
1367 h->root.root.string, tbfd, tsec, ntbfd);
1368 else
1369 _bfd_error_handler
1370 /* xgettext:c-format */
1371 (_("%s: TLS reference in %B "
1372 "mismatches non-TLS definition in %B section %A"),
1373 h->root.root.string, tbfd, ntbfd, ntsec);
1374
1375 bfd_set_error (bfd_error_bad_value);
1376 return FALSE;
1377 }
1378
1379 /* If the old symbol has non-default visibility, we ignore the new
1380 definition from a dynamic object. */
1381 if (newdyn
1382 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1383 && !bfd_is_und_section (sec))
1384 {
1385 *skip = TRUE;
1386 /* Make sure this symbol is dynamic. */
1387 h->ref_dynamic = 1;
1388 hi->ref_dynamic = 1;
1389 /* A protected symbol has external availability. Make sure it is
1390 recorded as dynamic.
1391
1392 FIXME: Should we check type and size for protected symbol? */
1393 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1394 return bfd_elf_link_record_dynamic_symbol (info, h);
1395 else
1396 return TRUE;
1397 }
1398 else if (!newdyn
1399 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1400 && h->def_dynamic)
1401 {
1402 /* If the new symbol with non-default visibility comes from a
1403 relocatable file and the old definition comes from a dynamic
1404 object, we remove the old definition. */
1405 if (hi->root.type == bfd_link_hash_indirect)
1406 {
1407 /* Handle the case where the old dynamic definition is
1408 default versioned. We need to copy the symbol info from
1409 the symbol with default version to the normal one if it
1410 was referenced before. */
1411 if (h->ref_regular)
1412 {
1413 hi->root.type = h->root.type;
1414 h->root.type = bfd_link_hash_indirect;
1415 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1416
1417 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1418 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1419 {
1420 /* If the new symbol is hidden or internal, completely undo
1421 any dynamic link state. */
1422 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1423 h->forced_local = 0;
1424 h->ref_dynamic = 0;
1425 }
1426 else
1427 h->ref_dynamic = 1;
1428
1429 h->def_dynamic = 0;
1430 /* FIXME: Should we check type and size for protected symbol? */
1431 h->size = 0;
1432 h->type = 0;
1433
1434 h = hi;
1435 }
1436 else
1437 h = hi;
1438 }
1439
1440 /* If the old symbol was undefined before, then it will still be
1441 on the undefs list. If the new symbol is undefined or
1442 common, we can't make it bfd_link_hash_new here, because new
1443 undefined or common symbols will be added to the undefs list
1444 by _bfd_generic_link_add_one_symbol. Symbols may not be
1445 added twice to the undefs list. Also, if the new symbol is
1446 undefweak then we don't want to lose the strong undef. */
1447 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1448 {
1449 h->root.type = bfd_link_hash_undefined;
1450 h->root.u.undef.abfd = abfd;
1451 }
1452 else
1453 {
1454 h->root.type = bfd_link_hash_new;
1455 h->root.u.undef.abfd = NULL;
1456 }
1457
1458 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1459 {
1460 /* If the new symbol is hidden or internal, completely undo
1461 any dynamic link state. */
1462 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1463 h->forced_local = 0;
1464 h->ref_dynamic = 0;
1465 }
1466 else
1467 h->ref_dynamic = 1;
1468 h->def_dynamic = 0;
1469 /* FIXME: Should we check type and size for protected symbol? */
1470 h->size = 0;
1471 h->type = 0;
1472 return TRUE;
1473 }
1474
1475 /* If a new weak symbol definition comes from a regular file and the
1476 old symbol comes from a dynamic library, we treat the new one as
1477 strong. Similarly, an old weak symbol definition from a regular
1478 file is treated as strong when the new symbol comes from a dynamic
1479 library. Further, an old weak symbol from a dynamic library is
1480 treated as strong if the new symbol is from a dynamic library.
1481 This reflects the way glibc's ld.so works.
1482
1483 Also allow a weak symbol to override a linker script symbol
1484 defined by an early pass over the script. This is done so the
1485 linker knows the symbol is defined in an object file, for the
1486 DEFINED script function.
1487
1488 Do this before setting *type_change_ok or *size_change_ok so that
1489 we warn properly when dynamic library symbols are overridden. */
1490
1491 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1492 newweak = FALSE;
1493 if (olddef && newdyn)
1494 oldweak = FALSE;
1495
1496 /* Allow changes between different types of function symbol. */
1497 if (newfunc && oldfunc)
1498 *type_change_ok = TRUE;
1499
1500 /* It's OK to change the type if either the existing symbol or the
1501 new symbol is weak. A type change is also OK if the old symbol
1502 is undefined and the new symbol is defined. */
1503
1504 if (oldweak
1505 || newweak
1506 || (newdef
1507 && h->root.type == bfd_link_hash_undefined))
1508 *type_change_ok = TRUE;
1509
1510 /* It's OK to change the size if either the existing symbol or the
1511 new symbol is weak, or if the old symbol is undefined. */
1512
1513 if (*type_change_ok
1514 || h->root.type == bfd_link_hash_undefined)
1515 *size_change_ok = TRUE;
1516
1517 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1518 symbol, respectively, appears to be a common symbol in a dynamic
1519 object. If a symbol appears in an uninitialized section, and is
1520 not weak, and is not a function, then it may be a common symbol
1521 which was resolved when the dynamic object was created. We want
1522 to treat such symbols specially, because they raise special
1523 considerations when setting the symbol size: if the symbol
1524 appears as a common symbol in a regular object, and the size in
1525 the regular object is larger, we must make sure that we use the
1526 larger size. This problematic case can always be avoided in C,
1527 but it must be handled correctly when using Fortran shared
1528 libraries.
1529
1530 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1531 likewise for OLDDYNCOMMON and OLDDEF.
1532
1533 Note that this test is just a heuristic, and that it is quite
1534 possible to have an uninitialized symbol in a shared object which
1535 is really a definition, rather than a common symbol. This could
1536 lead to some minor confusion when the symbol really is a common
1537 symbol in some regular object. However, I think it will be
1538 harmless. */
1539
1540 if (newdyn
1541 && newdef
1542 && !newweak
1543 && (sec->flags & SEC_ALLOC) != 0
1544 && (sec->flags & SEC_LOAD) == 0
1545 && sym->st_size > 0
1546 && !newfunc)
1547 newdyncommon = TRUE;
1548 else
1549 newdyncommon = FALSE;
1550
1551 if (olddyn
1552 && olddef
1553 && h->root.type == bfd_link_hash_defined
1554 && h->def_dynamic
1555 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1556 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1557 && h->size > 0
1558 && !oldfunc)
1559 olddyncommon = TRUE;
1560 else
1561 olddyncommon = FALSE;
1562
1563 /* We now know everything about the old and new symbols. We ask the
1564 backend to check if we can merge them. */
1565 if (bed->merge_symbol != NULL)
1566 {
1567 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1568 return FALSE;
1569 sec = *psec;
1570 }
1571
1572 /* There are multiple definitions of a normal symbol. Skip the
1573 default symbol as well as definition from an IR object. */
1574 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1575 && !default_sym && h->def_regular
1576 && !(oldbfd != NULL
1577 && (oldbfd->flags & BFD_PLUGIN) != 0
1578 && (abfd->flags & BFD_PLUGIN) == 0))
1579 {
1580 /* Handle a multiple definition. */
1581 (*info->callbacks->multiple_definition) (info, &h->root,
1582 abfd, sec, *pvalue);
1583 *skip = TRUE;
1584 return TRUE;
1585 }
1586
1587 /* If both the old and the new symbols look like common symbols in a
1588 dynamic object, set the size of the symbol to the larger of the
1589 two. */
1590
1591 if (olddyncommon
1592 && newdyncommon
1593 && sym->st_size != h->size)
1594 {
1595 /* Since we think we have two common symbols, issue a multiple
1596 common warning if desired. Note that we only warn if the
1597 size is different. If the size is the same, we simply let
1598 the old symbol override the new one as normally happens with
1599 symbols defined in dynamic objects. */
1600
1601 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1602 bfd_link_hash_common, sym->st_size);
1603 if (sym->st_size > h->size)
1604 h->size = sym->st_size;
1605
1606 *size_change_ok = TRUE;
1607 }
1608
1609 /* If we are looking at a dynamic object, and we have found a
1610 definition, we need to see if the symbol was already defined by
1611 some other object. If so, we want to use the existing
1612 definition, and we do not want to report a multiple symbol
1613 definition error; we do this by clobbering *PSEC to be
1614 bfd_und_section_ptr.
1615
1616 We treat a common symbol as a definition if the symbol in the
1617 shared library is a function, since common symbols always
1618 represent variables; this can cause confusion in principle, but
1619 any such confusion would seem to indicate an erroneous program or
1620 shared library. We also permit a common symbol in a regular
1621 object to override a weak symbol in a shared object. */
1622
1623 if (newdyn
1624 && newdef
1625 && (olddef
1626 || (h->root.type == bfd_link_hash_common
1627 && (newweak || newfunc))))
1628 {
1629 *override = TRUE;
1630 newdef = FALSE;
1631 newdyncommon = FALSE;
1632
1633 *psec = sec = bfd_und_section_ptr;
1634 *size_change_ok = TRUE;
1635
1636 /* If we get here when the old symbol is a common symbol, then
1637 we are explicitly letting it override a weak symbol or
1638 function in a dynamic object, and we don't want to warn about
1639 a type change. If the old symbol is a defined symbol, a type
1640 change warning may still be appropriate. */
1641
1642 if (h->root.type == bfd_link_hash_common)
1643 *type_change_ok = TRUE;
1644 }
1645
1646 /* Handle the special case of an old common symbol merging with a
1647 new symbol which looks like a common symbol in a shared object.
1648 We change *PSEC and *PVALUE to make the new symbol look like a
1649 common symbol, and let _bfd_generic_link_add_one_symbol do the
1650 right thing. */
1651
1652 if (newdyncommon
1653 && h->root.type == bfd_link_hash_common)
1654 {
1655 *override = TRUE;
1656 newdef = FALSE;
1657 newdyncommon = FALSE;
1658 *pvalue = sym->st_size;
1659 *psec = sec = bed->common_section (oldsec);
1660 *size_change_ok = TRUE;
1661 }
1662
1663 /* Skip weak definitions of symbols that are already defined. */
1664 if (newdef && olddef && newweak)
1665 {
1666 /* Don't skip new non-IR weak syms. */
1667 if (!(oldbfd != NULL
1668 && (oldbfd->flags & BFD_PLUGIN) != 0
1669 && (abfd->flags & BFD_PLUGIN) == 0))
1670 {
1671 newdef = FALSE;
1672 *skip = TRUE;
1673 }
1674
1675 /* Merge st_other. If the symbol already has a dynamic index,
1676 but visibility says it should not be visible, turn it into a
1677 local symbol. */
1678 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1679 if (h->dynindx != -1)
1680 switch (ELF_ST_VISIBILITY (h->other))
1681 {
1682 case STV_INTERNAL:
1683 case STV_HIDDEN:
1684 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1685 break;
1686 }
1687 }
1688
1689 /* If the old symbol is from a dynamic object, and the new symbol is
1690 a definition which is not from a dynamic object, then the new
1691 symbol overrides the old symbol. Symbols from regular files
1692 always take precedence over symbols from dynamic objects, even if
1693 they are defined after the dynamic object in the link.
1694
1695 As above, we again permit a common symbol in a regular object to
1696 override a definition in a shared object if the shared object
1697 symbol is a function or is weak. */
1698
1699 flip = NULL;
1700 if (!newdyn
1701 && (newdef
1702 || (bfd_is_com_section (sec)
1703 && (oldweak || oldfunc)))
1704 && olddyn
1705 && olddef
1706 && h->def_dynamic)
1707 {
1708 /* Change the hash table entry to undefined, and let
1709 _bfd_generic_link_add_one_symbol do the right thing with the
1710 new definition. */
1711
1712 h->root.type = bfd_link_hash_undefined;
1713 h->root.u.undef.abfd = h->root.u.def.section->owner;
1714 *size_change_ok = TRUE;
1715
1716 olddef = FALSE;
1717 olddyncommon = FALSE;
1718
1719 /* We again permit a type change when a common symbol may be
1720 overriding a function. */
1721
1722 if (bfd_is_com_section (sec))
1723 {
1724 if (oldfunc)
1725 {
1726 /* If a common symbol overrides a function, make sure
1727 that it isn't defined dynamically nor has type
1728 function. */
1729 h->def_dynamic = 0;
1730 h->type = STT_NOTYPE;
1731 }
1732 *type_change_ok = TRUE;
1733 }
1734
1735 if (hi->root.type == bfd_link_hash_indirect)
1736 flip = hi;
1737 else
1738 /* This union may have been set to be non-NULL when this symbol
1739 was seen in a dynamic object. We must force the union to be
1740 NULL, so that it is correct for a regular symbol. */
1741 h->verinfo.vertree = NULL;
1742 }
1743
1744 /* Handle the special case of a new common symbol merging with an
1745 old symbol that looks like it might be a common symbol defined in
1746 a shared object. Note that we have already handled the case in
1747 which a new common symbol should simply override the definition
1748 in the shared library. */
1749
1750 if (! newdyn
1751 && bfd_is_com_section (sec)
1752 && olddyncommon)
1753 {
1754 /* It would be best if we could set the hash table entry to a
1755 common symbol, but we don't know what to use for the section
1756 or the alignment. */
1757 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1758 bfd_link_hash_common, sym->st_size);
1759
1760 /* If the presumed common symbol in the dynamic object is
1761 larger, pretend that the new symbol has its size. */
1762
1763 if (h->size > *pvalue)
1764 *pvalue = h->size;
1765
1766 /* We need to remember the alignment required by the symbol
1767 in the dynamic object. */
1768 BFD_ASSERT (pold_alignment);
1769 *pold_alignment = h->root.u.def.section->alignment_power;
1770
1771 olddef = FALSE;
1772 olddyncommon = FALSE;
1773
1774 h->root.type = bfd_link_hash_undefined;
1775 h->root.u.undef.abfd = h->root.u.def.section->owner;
1776
1777 *size_change_ok = TRUE;
1778 *type_change_ok = TRUE;
1779
1780 if (hi->root.type == bfd_link_hash_indirect)
1781 flip = hi;
1782 else
1783 h->verinfo.vertree = NULL;
1784 }
1785
1786 if (flip != NULL)
1787 {
1788 /* Handle the case where we had a versioned symbol in a dynamic
1789 library and now find a definition in a normal object. In this
1790 case, we make the versioned symbol point to the normal one. */
1791 flip->root.type = h->root.type;
1792 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1793 h->root.type = bfd_link_hash_indirect;
1794 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1795 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1796 if (h->def_dynamic)
1797 {
1798 h->def_dynamic = 0;
1799 flip->ref_dynamic = 1;
1800 }
1801 }
1802
1803 return TRUE;
1804 }
1805
1806 /* This function is called to create an indirect symbol from the
1807 default for the symbol with the default version if needed. The
1808 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1809 set DYNSYM if the new indirect symbol is dynamic. */
1810
1811 static bfd_boolean
1812 _bfd_elf_add_default_symbol (bfd *abfd,
1813 struct bfd_link_info *info,
1814 struct elf_link_hash_entry *h,
1815 const char *name,
1816 Elf_Internal_Sym *sym,
1817 asection *sec,
1818 bfd_vma value,
1819 bfd **poldbfd,
1820 bfd_boolean *dynsym)
1821 {
1822 bfd_boolean type_change_ok;
1823 bfd_boolean size_change_ok;
1824 bfd_boolean skip;
1825 char *shortname;
1826 struct elf_link_hash_entry *hi;
1827 struct bfd_link_hash_entry *bh;
1828 const struct elf_backend_data *bed;
1829 bfd_boolean collect;
1830 bfd_boolean dynamic;
1831 bfd_boolean override;
1832 char *p;
1833 size_t len, shortlen;
1834 asection *tmp_sec;
1835 bfd_boolean matched;
1836
1837 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1838 return TRUE;
1839
1840 /* If this symbol has a version, and it is the default version, we
1841 create an indirect symbol from the default name to the fully
1842 decorated name. This will cause external references which do not
1843 specify a version to be bound to this version of the symbol. */
1844 p = strchr (name, ELF_VER_CHR);
1845 if (h->versioned == unknown)
1846 {
1847 if (p == NULL)
1848 {
1849 h->versioned = unversioned;
1850 return TRUE;
1851 }
1852 else
1853 {
1854 if (p[1] != ELF_VER_CHR)
1855 {
1856 h->versioned = versioned_hidden;
1857 return TRUE;
1858 }
1859 else
1860 h->versioned = versioned;
1861 }
1862 }
1863 else
1864 {
1865 /* PR ld/19073: We may see an unversioned definition after the
1866 default version. */
1867 if (p == NULL)
1868 return TRUE;
1869 }
1870
1871 bed = get_elf_backend_data (abfd);
1872 collect = bed->collect;
1873 dynamic = (abfd->flags & DYNAMIC) != 0;
1874
1875 shortlen = p - name;
1876 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1877 if (shortname == NULL)
1878 return FALSE;
1879 memcpy (shortname, name, shortlen);
1880 shortname[shortlen] = '\0';
1881
1882 /* We are going to create a new symbol. Merge it with any existing
1883 symbol with this name. For the purposes of the merge, act as
1884 though we were defining the symbol we just defined, although we
1885 actually going to define an indirect symbol. */
1886 type_change_ok = FALSE;
1887 size_change_ok = FALSE;
1888 matched = TRUE;
1889 tmp_sec = sec;
1890 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1891 &hi, poldbfd, NULL, NULL, &skip, &override,
1892 &type_change_ok, &size_change_ok, &matched))
1893 return FALSE;
1894
1895 if (skip)
1896 goto nondefault;
1897
1898 if (hi->def_regular)
1899 {
1900 /* If the undecorated symbol will have a version added by a
1901 script different to H, then don't indirect to/from the
1902 undecorated symbol. This isn't ideal because we may not yet
1903 have seen symbol versions, if given by a script on the
1904 command line rather than via --version-script. */
1905 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1906 {
1907 bfd_boolean hide;
1908
1909 hi->verinfo.vertree
1910 = bfd_find_version_for_sym (info->version_info,
1911 hi->root.root.string, &hide);
1912 if (hi->verinfo.vertree != NULL && hide)
1913 {
1914 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1915 goto nondefault;
1916 }
1917 }
1918 if (hi->verinfo.vertree != NULL
1919 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1920 goto nondefault;
1921 }
1922
1923 if (! override)
1924 {
1925 /* Add the default symbol if not performing a relocatable link. */
1926 if (! bfd_link_relocatable (info))
1927 {
1928 bh = &hi->root;
1929 if (! (_bfd_generic_link_add_one_symbol
1930 (info, abfd, shortname, BSF_INDIRECT,
1931 bfd_ind_section_ptr,
1932 0, name, FALSE, collect, &bh)))
1933 return FALSE;
1934 hi = (struct elf_link_hash_entry *) bh;
1935 }
1936 }
1937 else
1938 {
1939 /* In this case the symbol named SHORTNAME is overriding the
1940 indirect symbol we want to add. We were planning on making
1941 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1942 is the name without a version. NAME is the fully versioned
1943 name, and it is the default version.
1944
1945 Overriding means that we already saw a definition for the
1946 symbol SHORTNAME in a regular object, and it is overriding
1947 the symbol defined in the dynamic object.
1948
1949 When this happens, we actually want to change NAME, the
1950 symbol we just added, to refer to SHORTNAME. This will cause
1951 references to NAME in the shared object to become references
1952 to SHORTNAME in the regular object. This is what we expect
1953 when we override a function in a shared object: that the
1954 references in the shared object will be mapped to the
1955 definition in the regular object. */
1956
1957 while (hi->root.type == bfd_link_hash_indirect
1958 || hi->root.type == bfd_link_hash_warning)
1959 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1960
1961 h->root.type = bfd_link_hash_indirect;
1962 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1963 if (h->def_dynamic)
1964 {
1965 h->def_dynamic = 0;
1966 hi->ref_dynamic = 1;
1967 if (hi->ref_regular
1968 || hi->def_regular)
1969 {
1970 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1971 return FALSE;
1972 }
1973 }
1974
1975 /* Now set HI to H, so that the following code will set the
1976 other fields correctly. */
1977 hi = h;
1978 }
1979
1980 /* Check if HI is a warning symbol. */
1981 if (hi->root.type == bfd_link_hash_warning)
1982 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1983
1984 /* If there is a duplicate definition somewhere, then HI may not
1985 point to an indirect symbol. We will have reported an error to
1986 the user in that case. */
1987
1988 if (hi->root.type == bfd_link_hash_indirect)
1989 {
1990 struct elf_link_hash_entry *ht;
1991
1992 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1993 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1994
1995 /* A reference to the SHORTNAME symbol from a dynamic library
1996 will be satisfied by the versioned symbol at runtime. In
1997 effect, we have a reference to the versioned symbol. */
1998 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1999 hi->dynamic_def |= ht->dynamic_def;
2000
2001 /* See if the new flags lead us to realize that the symbol must
2002 be dynamic. */
2003 if (! *dynsym)
2004 {
2005 if (! dynamic)
2006 {
2007 if (! bfd_link_executable (info)
2008 || hi->def_dynamic
2009 || hi->ref_dynamic)
2010 *dynsym = TRUE;
2011 }
2012 else
2013 {
2014 if (hi->ref_regular)
2015 *dynsym = TRUE;
2016 }
2017 }
2018 }
2019
2020 /* We also need to define an indirection from the nondefault version
2021 of the symbol. */
2022
2023 nondefault:
2024 len = strlen (name);
2025 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2026 if (shortname == NULL)
2027 return FALSE;
2028 memcpy (shortname, name, shortlen);
2029 memcpy (shortname + shortlen, p + 1, len - shortlen);
2030
2031 /* Once again, merge with any existing symbol. */
2032 type_change_ok = FALSE;
2033 size_change_ok = FALSE;
2034 tmp_sec = sec;
2035 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2036 &hi, poldbfd, NULL, NULL, &skip, &override,
2037 &type_change_ok, &size_change_ok, &matched))
2038 return FALSE;
2039
2040 if (skip)
2041 return TRUE;
2042
2043 if (override)
2044 {
2045 /* Here SHORTNAME is a versioned name, so we don't expect to see
2046 the type of override we do in the case above unless it is
2047 overridden by a versioned definition. */
2048 if (hi->root.type != bfd_link_hash_defined
2049 && hi->root.type != bfd_link_hash_defweak)
2050 _bfd_error_handler
2051 /* xgettext:c-format */
2052 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
2053 abfd, shortname);
2054 }
2055 else
2056 {
2057 bh = &hi->root;
2058 if (! (_bfd_generic_link_add_one_symbol
2059 (info, abfd, shortname, BSF_INDIRECT,
2060 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
2061 return FALSE;
2062 hi = (struct elf_link_hash_entry *) bh;
2063
2064 /* If there is a duplicate definition somewhere, then HI may not
2065 point to an indirect symbol. We will have reported an error
2066 to the user in that case. */
2067
2068 if (hi->root.type == bfd_link_hash_indirect)
2069 {
2070 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2071 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2072 hi->dynamic_def |= h->dynamic_def;
2073
2074 /* See if the new flags lead us to realize that the symbol
2075 must be dynamic. */
2076 if (! *dynsym)
2077 {
2078 if (! dynamic)
2079 {
2080 if (! bfd_link_executable (info)
2081 || hi->ref_dynamic)
2082 *dynsym = TRUE;
2083 }
2084 else
2085 {
2086 if (hi->ref_regular)
2087 *dynsym = TRUE;
2088 }
2089 }
2090 }
2091 }
2092
2093 return TRUE;
2094 }
2095 \f
2096 /* This routine is used to export all defined symbols into the dynamic
2097 symbol table. It is called via elf_link_hash_traverse. */
2098
2099 static bfd_boolean
2100 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2101 {
2102 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2103
2104 /* Ignore indirect symbols. These are added by the versioning code. */
2105 if (h->root.type == bfd_link_hash_indirect)
2106 return TRUE;
2107
2108 /* Ignore this if we won't export it. */
2109 if (!eif->info->export_dynamic && !h->dynamic)
2110 return TRUE;
2111
2112 if (h->dynindx == -1
2113 && (h->def_regular || h->ref_regular)
2114 && ! bfd_hide_sym_by_version (eif->info->version_info,
2115 h->root.root.string))
2116 {
2117 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2118 {
2119 eif->failed = TRUE;
2120 return FALSE;
2121 }
2122 }
2123
2124 return TRUE;
2125 }
2126 \f
2127 /* Look through the symbols which are defined in other shared
2128 libraries and referenced here. Update the list of version
2129 dependencies. This will be put into the .gnu.version_r section.
2130 This function is called via elf_link_hash_traverse. */
2131
2132 static bfd_boolean
2133 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2134 void *data)
2135 {
2136 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2137 Elf_Internal_Verneed *t;
2138 Elf_Internal_Vernaux *a;
2139 bfd_size_type amt;
2140
2141 /* We only care about symbols defined in shared objects with version
2142 information. */
2143 if (!h->def_dynamic
2144 || h->def_regular
2145 || h->dynindx == -1
2146 || h->verinfo.verdef == NULL
2147 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2148 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2149 return TRUE;
2150
2151 /* See if we already know about this version. */
2152 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2153 t != NULL;
2154 t = t->vn_nextref)
2155 {
2156 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2157 continue;
2158
2159 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2160 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2161 return TRUE;
2162
2163 break;
2164 }
2165
2166 /* This is a new version. Add it to tree we are building. */
2167
2168 if (t == NULL)
2169 {
2170 amt = sizeof *t;
2171 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2172 if (t == NULL)
2173 {
2174 rinfo->failed = TRUE;
2175 return FALSE;
2176 }
2177
2178 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2179 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2180 elf_tdata (rinfo->info->output_bfd)->verref = t;
2181 }
2182
2183 amt = sizeof *a;
2184 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2185 if (a == NULL)
2186 {
2187 rinfo->failed = TRUE;
2188 return FALSE;
2189 }
2190
2191 /* Note that we are copying a string pointer here, and testing it
2192 above. If bfd_elf_string_from_elf_section is ever changed to
2193 discard the string data when low in memory, this will have to be
2194 fixed. */
2195 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2196
2197 a->vna_flags = h->verinfo.verdef->vd_flags;
2198 a->vna_nextptr = t->vn_auxptr;
2199
2200 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2201 ++rinfo->vers;
2202
2203 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2204
2205 t->vn_auxptr = a;
2206
2207 return TRUE;
2208 }
2209
2210 /* Figure out appropriate versions for all the symbols. We may not
2211 have the version number script until we have read all of the input
2212 files, so until that point we don't know which symbols should be
2213 local. This function is called via elf_link_hash_traverse. */
2214
2215 static bfd_boolean
2216 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2217 {
2218 struct elf_info_failed *sinfo;
2219 struct bfd_link_info *info;
2220 const struct elf_backend_data *bed;
2221 struct elf_info_failed eif;
2222 char *p;
2223
2224 sinfo = (struct elf_info_failed *) data;
2225 info = sinfo->info;
2226
2227 /* Fix the symbol flags. */
2228 eif.failed = FALSE;
2229 eif.info = info;
2230 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2231 {
2232 if (eif.failed)
2233 sinfo->failed = TRUE;
2234 return FALSE;
2235 }
2236
2237 /* We only need version numbers for symbols defined in regular
2238 objects. */
2239 if (!h->def_regular)
2240 return TRUE;
2241
2242 bed = get_elf_backend_data (info->output_bfd);
2243 p = strchr (h->root.root.string, ELF_VER_CHR);
2244 if (p != NULL && h->verinfo.vertree == NULL)
2245 {
2246 struct bfd_elf_version_tree *t;
2247
2248 ++p;
2249 if (*p == ELF_VER_CHR)
2250 ++p;
2251
2252 /* If there is no version string, we can just return out. */
2253 if (*p == '\0')
2254 return TRUE;
2255
2256 /* Look for the version. If we find it, it is no longer weak. */
2257 for (t = sinfo->info->version_info; t != NULL; t = t->next)
2258 {
2259 if (strcmp (t->name, p) == 0)
2260 {
2261 size_t len;
2262 char *alc;
2263 struct bfd_elf_version_expr *d;
2264
2265 len = p - h->root.root.string;
2266 alc = (char *) bfd_malloc (len);
2267 if (alc == NULL)
2268 {
2269 sinfo->failed = TRUE;
2270 return FALSE;
2271 }
2272 memcpy (alc, h->root.root.string, len - 1);
2273 alc[len - 1] = '\0';
2274 if (alc[len - 2] == ELF_VER_CHR)
2275 alc[len - 2] = '\0';
2276
2277 h->verinfo.vertree = t;
2278 t->used = TRUE;
2279 d = NULL;
2280
2281 if (t->globals.list != NULL)
2282 d = (*t->match) (&t->globals, NULL, alc);
2283
2284 /* See if there is anything to force this symbol to
2285 local scope. */
2286 if (d == NULL && t->locals.list != NULL)
2287 {
2288 d = (*t->match) (&t->locals, NULL, alc);
2289 if (d != NULL
2290 && h->dynindx != -1
2291 && ! info->export_dynamic)
2292 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2293 }
2294
2295 free (alc);
2296 break;
2297 }
2298 }
2299
2300 /* If we are building an application, we need to create a
2301 version node for this version. */
2302 if (t == NULL && bfd_link_executable (info))
2303 {
2304 struct bfd_elf_version_tree **pp;
2305 int version_index;
2306
2307 /* If we aren't going to export this symbol, we don't need
2308 to worry about it. */
2309 if (h->dynindx == -1)
2310 return TRUE;
2311
2312 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2313 sizeof *t);
2314 if (t == NULL)
2315 {
2316 sinfo->failed = TRUE;
2317 return FALSE;
2318 }
2319
2320 t->name = p;
2321 t->name_indx = (unsigned int) -1;
2322 t->used = TRUE;
2323
2324 version_index = 1;
2325 /* Don't count anonymous version tag. */
2326 if (sinfo->info->version_info != NULL
2327 && sinfo->info->version_info->vernum == 0)
2328 version_index = 0;
2329 for (pp = &sinfo->info->version_info;
2330 *pp != NULL;
2331 pp = &(*pp)->next)
2332 ++version_index;
2333 t->vernum = version_index;
2334
2335 *pp = t;
2336
2337 h->verinfo.vertree = t;
2338 }
2339 else if (t == NULL)
2340 {
2341 /* We could not find the version for a symbol when
2342 generating a shared archive. Return an error. */
2343 _bfd_error_handler
2344 /* xgettext:c-format */
2345 (_("%B: version node not found for symbol %s"),
2346 info->output_bfd, h->root.root.string);
2347 bfd_set_error (bfd_error_bad_value);
2348 sinfo->failed = TRUE;
2349 return FALSE;
2350 }
2351 }
2352
2353 /* If we don't have a version for this symbol, see if we can find
2354 something. */
2355 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
2356 {
2357 bfd_boolean hide;
2358
2359 h->verinfo.vertree
2360 = bfd_find_version_for_sym (sinfo->info->version_info,
2361 h->root.root.string, &hide);
2362 if (h->verinfo.vertree != NULL && hide)
2363 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2364 }
2365
2366 return TRUE;
2367 }
2368 \f
2369 /* Read and swap the relocs from the section indicated by SHDR. This
2370 may be either a REL or a RELA section. The relocations are
2371 translated into RELA relocations and stored in INTERNAL_RELOCS,
2372 which should have already been allocated to contain enough space.
2373 The EXTERNAL_RELOCS are a buffer where the external form of the
2374 relocations should be stored.
2375
2376 Returns FALSE if something goes wrong. */
2377
2378 static bfd_boolean
2379 elf_link_read_relocs_from_section (bfd *abfd,
2380 asection *sec,
2381 Elf_Internal_Shdr *shdr,
2382 void *external_relocs,
2383 Elf_Internal_Rela *internal_relocs)
2384 {
2385 const struct elf_backend_data *bed;
2386 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2387 const bfd_byte *erela;
2388 const bfd_byte *erelaend;
2389 Elf_Internal_Rela *irela;
2390 Elf_Internal_Shdr *symtab_hdr;
2391 size_t nsyms;
2392
2393 /* Position ourselves at the start of the section. */
2394 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2395 return FALSE;
2396
2397 /* Read the relocations. */
2398 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2399 return FALSE;
2400
2401 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2402 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2403
2404 bed = get_elf_backend_data (abfd);
2405
2406 /* Convert the external relocations to the internal format. */
2407 if (shdr->sh_entsize == bed->s->sizeof_rel)
2408 swap_in = bed->s->swap_reloc_in;
2409 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2410 swap_in = bed->s->swap_reloca_in;
2411 else
2412 {
2413 bfd_set_error (bfd_error_wrong_format);
2414 return FALSE;
2415 }
2416
2417 erela = (const bfd_byte *) external_relocs;
2418 erelaend = erela + shdr->sh_size;
2419 irela = internal_relocs;
2420 while (erela < erelaend)
2421 {
2422 bfd_vma r_symndx;
2423
2424 (*swap_in) (abfd, erela, irela);
2425 r_symndx = ELF32_R_SYM (irela->r_info);
2426 if (bed->s->arch_size == 64)
2427 r_symndx >>= 24;
2428 if (nsyms > 0)
2429 {
2430 if ((size_t) r_symndx >= nsyms)
2431 {
2432 _bfd_error_handler
2433 /* xgettext:c-format */
2434 (_("%B: bad reloc symbol index (%#Lx >= %#lx)"
2435 " for offset %#Lx in section `%A'"),
2436 abfd, r_symndx, (unsigned long) nsyms,
2437 irela->r_offset, sec);
2438 bfd_set_error (bfd_error_bad_value);
2439 return FALSE;
2440 }
2441 }
2442 else if (r_symndx != STN_UNDEF)
2443 {
2444 _bfd_error_handler
2445 /* xgettext:c-format */
2446 (_("%B: non-zero symbol index (%#Lx)"
2447 " for offset %#Lx in section `%A'"
2448 " when the object file has no symbol table"),
2449 abfd, r_symndx,
2450 irela->r_offset, sec);
2451 bfd_set_error (bfd_error_bad_value);
2452 return FALSE;
2453 }
2454 irela += bed->s->int_rels_per_ext_rel;
2455 erela += shdr->sh_entsize;
2456 }
2457
2458 return TRUE;
2459 }
2460
2461 /* Read and swap the relocs for a section O. They may have been
2462 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2463 not NULL, they are used as buffers to read into. They are known to
2464 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2465 the return value is allocated using either malloc or bfd_alloc,
2466 according to the KEEP_MEMORY argument. If O has two relocation
2467 sections (both REL and RELA relocations), then the REL_HDR
2468 relocations will appear first in INTERNAL_RELOCS, followed by the
2469 RELA_HDR relocations. */
2470
2471 Elf_Internal_Rela *
2472 _bfd_elf_link_read_relocs (bfd *abfd,
2473 asection *o,
2474 void *external_relocs,
2475 Elf_Internal_Rela *internal_relocs,
2476 bfd_boolean keep_memory)
2477 {
2478 void *alloc1 = NULL;
2479 Elf_Internal_Rela *alloc2 = NULL;
2480 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2481 struct bfd_elf_section_data *esdo = elf_section_data (o);
2482 Elf_Internal_Rela *internal_rela_relocs;
2483
2484 if (esdo->relocs != NULL)
2485 return esdo->relocs;
2486
2487 if (o->reloc_count == 0)
2488 return NULL;
2489
2490 if (internal_relocs == NULL)
2491 {
2492 bfd_size_type size;
2493
2494 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2495 if (keep_memory)
2496 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2497 else
2498 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2499 if (internal_relocs == NULL)
2500 goto error_return;
2501 }
2502
2503 if (external_relocs == NULL)
2504 {
2505 bfd_size_type size = 0;
2506
2507 if (esdo->rel.hdr)
2508 size += esdo->rel.hdr->sh_size;
2509 if (esdo->rela.hdr)
2510 size += esdo->rela.hdr->sh_size;
2511
2512 alloc1 = bfd_malloc (size);
2513 if (alloc1 == NULL)
2514 goto error_return;
2515 external_relocs = alloc1;
2516 }
2517
2518 internal_rela_relocs = internal_relocs;
2519 if (esdo->rel.hdr)
2520 {
2521 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2522 external_relocs,
2523 internal_relocs))
2524 goto error_return;
2525 external_relocs = (((bfd_byte *) external_relocs)
2526 + esdo->rel.hdr->sh_size);
2527 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2528 * bed->s->int_rels_per_ext_rel);
2529 }
2530
2531 if (esdo->rela.hdr
2532 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2533 external_relocs,
2534 internal_rela_relocs)))
2535 goto error_return;
2536
2537 /* Cache the results for next time, if we can. */
2538 if (keep_memory)
2539 esdo->relocs = internal_relocs;
2540
2541 if (alloc1 != NULL)
2542 free (alloc1);
2543
2544 /* Don't free alloc2, since if it was allocated we are passing it
2545 back (under the name of internal_relocs). */
2546
2547 return internal_relocs;
2548
2549 error_return:
2550 if (alloc1 != NULL)
2551 free (alloc1);
2552 if (alloc2 != NULL)
2553 {
2554 if (keep_memory)
2555 bfd_release (abfd, alloc2);
2556 else
2557 free (alloc2);
2558 }
2559 return NULL;
2560 }
2561
2562 /* Compute the size of, and allocate space for, REL_HDR which is the
2563 section header for a section containing relocations for O. */
2564
2565 static bfd_boolean
2566 _bfd_elf_link_size_reloc_section (bfd *abfd,
2567 struct bfd_elf_section_reloc_data *reldata)
2568 {
2569 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2570
2571 /* That allows us to calculate the size of the section. */
2572 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2573
2574 /* The contents field must last into write_object_contents, so we
2575 allocate it with bfd_alloc rather than malloc. Also since we
2576 cannot be sure that the contents will actually be filled in,
2577 we zero the allocated space. */
2578 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2579 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2580 return FALSE;
2581
2582 if (reldata->hashes == NULL && reldata->count)
2583 {
2584 struct elf_link_hash_entry **p;
2585
2586 p = ((struct elf_link_hash_entry **)
2587 bfd_zmalloc (reldata->count * sizeof (*p)));
2588 if (p == NULL)
2589 return FALSE;
2590
2591 reldata->hashes = p;
2592 }
2593
2594 return TRUE;
2595 }
2596
2597 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2598 originated from the section given by INPUT_REL_HDR) to the
2599 OUTPUT_BFD. */
2600
2601 bfd_boolean
2602 _bfd_elf_link_output_relocs (bfd *output_bfd,
2603 asection *input_section,
2604 Elf_Internal_Shdr *input_rel_hdr,
2605 Elf_Internal_Rela *internal_relocs,
2606 struct elf_link_hash_entry **rel_hash
2607 ATTRIBUTE_UNUSED)
2608 {
2609 Elf_Internal_Rela *irela;
2610 Elf_Internal_Rela *irelaend;
2611 bfd_byte *erel;
2612 struct bfd_elf_section_reloc_data *output_reldata;
2613 asection *output_section;
2614 const struct elf_backend_data *bed;
2615 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2616 struct bfd_elf_section_data *esdo;
2617
2618 output_section = input_section->output_section;
2619
2620 bed = get_elf_backend_data (output_bfd);
2621 esdo = elf_section_data (output_section);
2622 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2623 {
2624 output_reldata = &esdo->rel;
2625 swap_out = bed->s->swap_reloc_out;
2626 }
2627 else if (esdo->rela.hdr
2628 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2629 {
2630 output_reldata = &esdo->rela;
2631 swap_out = bed->s->swap_reloca_out;
2632 }
2633 else
2634 {
2635 _bfd_error_handler
2636 /* xgettext:c-format */
2637 (_("%B: relocation size mismatch in %B section %A"),
2638 output_bfd, input_section->owner, input_section);
2639 bfd_set_error (bfd_error_wrong_format);
2640 return FALSE;
2641 }
2642
2643 erel = output_reldata->hdr->contents;
2644 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2645 irela = internal_relocs;
2646 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2647 * bed->s->int_rels_per_ext_rel);
2648 while (irela < irelaend)
2649 {
2650 (*swap_out) (output_bfd, irela, erel);
2651 irela += bed->s->int_rels_per_ext_rel;
2652 erel += input_rel_hdr->sh_entsize;
2653 }
2654
2655 /* Bump the counter, so that we know where to add the next set of
2656 relocations. */
2657 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2658
2659 return TRUE;
2660 }
2661 \f
2662 /* Make weak undefined symbols in PIE dynamic. */
2663
2664 bfd_boolean
2665 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2666 struct elf_link_hash_entry *h)
2667 {
2668 if (bfd_link_pie (info)
2669 && h->dynindx == -1
2670 && h->root.type == bfd_link_hash_undefweak)
2671 return bfd_elf_link_record_dynamic_symbol (info, h);
2672
2673 return TRUE;
2674 }
2675
2676 /* Fix up the flags for a symbol. This handles various cases which
2677 can only be fixed after all the input files are seen. This is
2678 currently called by both adjust_dynamic_symbol and
2679 assign_sym_version, which is unnecessary but perhaps more robust in
2680 the face of future changes. */
2681
2682 static bfd_boolean
2683 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2684 struct elf_info_failed *eif)
2685 {
2686 const struct elf_backend_data *bed;
2687
2688 /* If this symbol was mentioned in a non-ELF file, try to set
2689 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2690 permit a non-ELF file to correctly refer to a symbol defined in
2691 an ELF dynamic object. */
2692 if (h->non_elf)
2693 {
2694 while (h->root.type == bfd_link_hash_indirect)
2695 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2696
2697 if (h->root.type != bfd_link_hash_defined
2698 && h->root.type != bfd_link_hash_defweak)
2699 {
2700 h->ref_regular = 1;
2701 h->ref_regular_nonweak = 1;
2702 }
2703 else
2704 {
2705 if (h->root.u.def.section->owner != NULL
2706 && (bfd_get_flavour (h->root.u.def.section->owner)
2707 == bfd_target_elf_flavour))
2708 {
2709 h->ref_regular = 1;
2710 h->ref_regular_nonweak = 1;
2711 }
2712 else
2713 h->def_regular = 1;
2714 }
2715
2716 if (h->dynindx == -1
2717 && (h->def_dynamic
2718 || h->ref_dynamic))
2719 {
2720 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2721 {
2722 eif->failed = TRUE;
2723 return FALSE;
2724 }
2725 }
2726 }
2727 else
2728 {
2729 /* Unfortunately, NON_ELF is only correct if the symbol
2730 was first seen in a non-ELF file. Fortunately, if the symbol
2731 was first seen in an ELF file, we're probably OK unless the
2732 symbol was defined in a non-ELF file. Catch that case here.
2733 FIXME: We're still in trouble if the symbol was first seen in
2734 a dynamic object, and then later in a non-ELF regular object. */
2735 if ((h->root.type == bfd_link_hash_defined
2736 || h->root.type == bfd_link_hash_defweak)
2737 && !h->def_regular
2738 && (h->root.u.def.section->owner != NULL
2739 ? (bfd_get_flavour (h->root.u.def.section->owner)
2740 != bfd_target_elf_flavour)
2741 : (bfd_is_abs_section (h->root.u.def.section)
2742 && !h->def_dynamic)))
2743 h->def_regular = 1;
2744 }
2745
2746 /* Backend specific symbol fixup. */
2747 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2748 if (bed->elf_backend_fixup_symbol
2749 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2750 return FALSE;
2751
2752 /* If this is a final link, and the symbol was defined as a common
2753 symbol in a regular object file, and there was no definition in
2754 any dynamic object, then the linker will have allocated space for
2755 the symbol in a common section but the DEF_REGULAR
2756 flag will not have been set. */
2757 if (h->root.type == bfd_link_hash_defined
2758 && !h->def_regular
2759 && h->ref_regular
2760 && !h->def_dynamic
2761 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2762 h->def_regular = 1;
2763
2764 /* If a weak undefined symbol has non-default visibility, we also
2765 hide it from the dynamic linker. */
2766 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2767 && h->root.type == bfd_link_hash_undefweak)
2768 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2769
2770 /* A hidden versioned symbol in executable should be forced local if
2771 it is is locally defined, not referenced by shared library and not
2772 exported. */
2773 else if (bfd_link_executable (eif->info)
2774 && h->versioned == versioned_hidden
2775 && !eif->info->export_dynamic
2776 && !h->dynamic
2777 && !h->ref_dynamic
2778 && h->def_regular)
2779 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2780
2781 /* If -Bsymbolic was used (which means to bind references to global
2782 symbols to the definition within the shared object), and this
2783 symbol was defined in a regular object, then it actually doesn't
2784 need a PLT entry. Likewise, if the symbol has non-default
2785 visibility. If the symbol has hidden or internal visibility, we
2786 will force it local. */
2787 else if (h->needs_plt
2788 && bfd_link_pic (eif->info)
2789 && is_elf_hash_table (eif->info->hash)
2790 && (SYMBOLIC_BIND (eif->info, h)
2791 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2792 && h->def_regular)
2793 {
2794 bfd_boolean force_local;
2795
2796 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2797 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2798 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2799 }
2800
2801 /* If this is a weak defined symbol in a dynamic object, and we know
2802 the real definition in the dynamic object, copy interesting flags
2803 over to the real definition. */
2804 if (h->is_weakalias)
2805 {
2806 struct elf_link_hash_entry *def = weakdef (h);
2807
2808 /* If the real definition is defined by a regular object file,
2809 don't do anything special. See the longer description in
2810 _bfd_elf_adjust_dynamic_symbol, below. */
2811 if (def->def_regular)
2812 {
2813 h = def;
2814 while ((h = h->u.alias) != def)
2815 h->is_weakalias = 0;
2816 }
2817 else
2818 {
2819 while (h->root.type == bfd_link_hash_indirect)
2820 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2821 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2822 || h->root.type == bfd_link_hash_defweak);
2823 BFD_ASSERT (def->def_dynamic);
2824 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
2825 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
2826 }
2827 }
2828
2829 return TRUE;
2830 }
2831
2832 /* Make the backend pick a good value for a dynamic symbol. This is
2833 called via elf_link_hash_traverse, and also calls itself
2834 recursively. */
2835
2836 static bfd_boolean
2837 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2838 {
2839 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2840 struct elf_link_hash_table *htab;
2841 const struct elf_backend_data *bed;
2842
2843 if (! is_elf_hash_table (eif->info->hash))
2844 return FALSE;
2845
2846 /* Ignore indirect symbols. These are added by the versioning code. */
2847 if (h->root.type == bfd_link_hash_indirect)
2848 return TRUE;
2849
2850 /* Fix the symbol flags. */
2851 if (! _bfd_elf_fix_symbol_flags (h, eif))
2852 return FALSE;
2853
2854 htab = elf_hash_table (eif->info);
2855 bed = get_elf_backend_data (htab->dynobj);
2856
2857 if (h->root.type == bfd_link_hash_undefweak)
2858 {
2859 if (eif->info->dynamic_undefined_weak == 0)
2860 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2861 else if (eif->info->dynamic_undefined_weak > 0
2862 && h->ref_regular
2863 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2864 && !bfd_hide_sym_by_version (eif->info->version_info,
2865 h->root.root.string))
2866 {
2867 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2868 {
2869 eif->failed = TRUE;
2870 return FALSE;
2871 }
2872 }
2873 }
2874
2875 /* If this symbol does not require a PLT entry, and it is not
2876 defined by a dynamic object, or is not referenced by a regular
2877 object, ignore it. We do have to handle a weak defined symbol,
2878 even if no regular object refers to it, if we decided to add it
2879 to the dynamic symbol table. FIXME: Do we normally need to worry
2880 about symbols which are defined by one dynamic object and
2881 referenced by another one? */
2882 if (!h->needs_plt
2883 && h->type != STT_GNU_IFUNC
2884 && (h->def_regular
2885 || !h->def_dynamic
2886 || (!h->ref_regular
2887 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
2888 {
2889 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2890 return TRUE;
2891 }
2892
2893 /* If we've already adjusted this symbol, don't do it again. This
2894 can happen via a recursive call. */
2895 if (h->dynamic_adjusted)
2896 return TRUE;
2897
2898 /* Don't look at this symbol again. Note that we must set this
2899 after checking the above conditions, because we may look at a
2900 symbol once, decide not to do anything, and then get called
2901 recursively later after REF_REGULAR is set below. */
2902 h->dynamic_adjusted = 1;
2903
2904 /* If this is a weak definition, and we know a real definition, and
2905 the real symbol is not itself defined by a regular object file,
2906 then get a good value for the real definition. We handle the
2907 real symbol first, for the convenience of the backend routine.
2908
2909 Note that there is a confusing case here. If the real definition
2910 is defined by a regular object file, we don't get the real symbol
2911 from the dynamic object, but we do get the weak symbol. If the
2912 processor backend uses a COPY reloc, then if some routine in the
2913 dynamic object changes the real symbol, we will not see that
2914 change in the corresponding weak symbol. This is the way other
2915 ELF linkers work as well, and seems to be a result of the shared
2916 library model.
2917
2918 I will clarify this issue. Most SVR4 shared libraries define the
2919 variable _timezone and define timezone as a weak synonym. The
2920 tzset call changes _timezone. If you write
2921 extern int timezone;
2922 int _timezone = 5;
2923 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2924 you might expect that, since timezone is a synonym for _timezone,
2925 the same number will print both times. However, if the processor
2926 backend uses a COPY reloc, then actually timezone will be copied
2927 into your process image, and, since you define _timezone
2928 yourself, _timezone will not. Thus timezone and _timezone will
2929 wind up at different memory locations. The tzset call will set
2930 _timezone, leaving timezone unchanged. */
2931
2932 if (h->is_weakalias)
2933 {
2934 struct elf_link_hash_entry *def = weakdef (h);
2935
2936 /* If we get to this point, there is an implicit reference to
2937 the alias by a regular object file via the weak symbol H. */
2938 def->ref_regular = 1;
2939
2940 /* Ensure that the backend adjust_dynamic_symbol function sees
2941 the strong alias before H by recursively calling ourselves. */
2942 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
2943 return FALSE;
2944 }
2945
2946 /* If a symbol has no type and no size and does not require a PLT
2947 entry, then we are probably about to do the wrong thing here: we
2948 are probably going to create a COPY reloc for an empty object.
2949 This case can arise when a shared object is built with assembly
2950 code, and the assembly code fails to set the symbol type. */
2951 if (h->size == 0
2952 && h->type == STT_NOTYPE
2953 && !h->needs_plt)
2954 _bfd_error_handler
2955 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2956 h->root.root.string);
2957
2958 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2959 {
2960 eif->failed = TRUE;
2961 return FALSE;
2962 }
2963
2964 return TRUE;
2965 }
2966
2967 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2968 DYNBSS. */
2969
2970 bfd_boolean
2971 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2972 struct elf_link_hash_entry *h,
2973 asection *dynbss)
2974 {
2975 unsigned int power_of_two;
2976 bfd_vma mask;
2977 asection *sec = h->root.u.def.section;
2978
2979 /* The section alignment of the definition is the maximum alignment
2980 requirement of symbols defined in the section. Since we don't
2981 know the symbol alignment requirement, we start with the
2982 maximum alignment and check low bits of the symbol address
2983 for the minimum alignment. */
2984 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2985 mask = ((bfd_vma) 1 << power_of_two) - 1;
2986 while ((h->root.u.def.value & mask) != 0)
2987 {
2988 mask >>= 1;
2989 --power_of_two;
2990 }
2991
2992 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2993 dynbss))
2994 {
2995 /* Adjust the section alignment if needed. */
2996 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2997 power_of_two))
2998 return FALSE;
2999 }
3000
3001 /* We make sure that the symbol will be aligned properly. */
3002 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3003
3004 /* Define the symbol as being at this point in DYNBSS. */
3005 h->root.u.def.section = dynbss;
3006 h->root.u.def.value = dynbss->size;
3007
3008 /* Increment the size of DYNBSS to make room for the symbol. */
3009 dynbss->size += h->size;
3010
3011 /* No error if extern_protected_data is true. */
3012 if (h->protected_def
3013 && (!info->extern_protected_data
3014 || (info->extern_protected_data < 0
3015 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3016 info->callbacks->einfo
3017 (_("%P: copy reloc against protected `%T' is dangerous\n"),
3018 h->root.root.string);
3019
3020 return TRUE;
3021 }
3022
3023 /* Adjust all external symbols pointing into SEC_MERGE sections
3024 to reflect the object merging within the sections. */
3025
3026 static bfd_boolean
3027 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3028 {
3029 asection *sec;
3030
3031 if ((h->root.type == bfd_link_hash_defined
3032 || h->root.type == bfd_link_hash_defweak)
3033 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3034 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3035 {
3036 bfd *output_bfd = (bfd *) data;
3037
3038 h->root.u.def.value =
3039 _bfd_merged_section_offset (output_bfd,
3040 &h->root.u.def.section,
3041 elf_section_data (sec)->sec_info,
3042 h->root.u.def.value);
3043 }
3044
3045 return TRUE;
3046 }
3047
3048 /* Returns false if the symbol referred to by H should be considered
3049 to resolve local to the current module, and true if it should be
3050 considered to bind dynamically. */
3051
3052 bfd_boolean
3053 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3054 struct bfd_link_info *info,
3055 bfd_boolean not_local_protected)
3056 {
3057 bfd_boolean binding_stays_local_p;
3058 const struct elf_backend_data *bed;
3059 struct elf_link_hash_table *hash_table;
3060
3061 if (h == NULL)
3062 return FALSE;
3063
3064 while (h->root.type == bfd_link_hash_indirect
3065 || h->root.type == bfd_link_hash_warning)
3066 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3067
3068 /* If it was forced local, then clearly it's not dynamic. */
3069 if (h->dynindx == -1)
3070 return FALSE;
3071 if (h->forced_local)
3072 return FALSE;
3073
3074 /* Identify the cases where name binding rules say that a
3075 visible symbol resolves locally. */
3076 binding_stays_local_p = (bfd_link_executable (info)
3077 || SYMBOLIC_BIND (info, h));
3078
3079 switch (ELF_ST_VISIBILITY (h->other))
3080 {
3081 case STV_INTERNAL:
3082 case STV_HIDDEN:
3083 return FALSE;
3084
3085 case STV_PROTECTED:
3086 hash_table = elf_hash_table (info);
3087 if (!is_elf_hash_table (hash_table))
3088 return FALSE;
3089
3090 bed = get_elf_backend_data (hash_table->dynobj);
3091
3092 /* Proper resolution for function pointer equality may require
3093 that these symbols perhaps be resolved dynamically, even though
3094 we should be resolving them to the current module. */
3095 if (!not_local_protected || !bed->is_function_type (h->type))
3096 binding_stays_local_p = TRUE;
3097 break;
3098
3099 default:
3100 break;
3101 }
3102
3103 /* If it isn't defined locally, then clearly it's dynamic. */
3104 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3105 return TRUE;
3106
3107 /* Otherwise, the symbol is dynamic if binding rules don't tell
3108 us that it remains local. */
3109 return !binding_stays_local_p;
3110 }
3111
3112 /* Return true if the symbol referred to by H should be considered
3113 to resolve local to the current module, and false otherwise. Differs
3114 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3115 undefined symbols. The two functions are virtually identical except
3116 for the place where dynindx == -1 is tested. If that test is true,
3117 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3118 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3119 defined symbols.
3120 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3121 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3122 treatment of undefined weak symbols. For those that do not make
3123 undefined weak symbols dynamic, both functions may return false. */
3124
3125 bfd_boolean
3126 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3127 struct bfd_link_info *info,
3128 bfd_boolean local_protected)
3129 {
3130 const struct elf_backend_data *bed;
3131 struct elf_link_hash_table *hash_table;
3132
3133 /* If it's a local sym, of course we resolve locally. */
3134 if (h == NULL)
3135 return TRUE;
3136
3137 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3138 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3139 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3140 return TRUE;
3141
3142 /* Forced local symbols resolve locally. */
3143 if (h->forced_local)
3144 return TRUE;
3145
3146 /* Common symbols that become definitions don't get the DEF_REGULAR
3147 flag set, so test it first, and don't bail out. */
3148 if (ELF_COMMON_DEF_P (h))
3149 /* Do nothing. */;
3150 /* If we don't have a definition in a regular file, then we can't
3151 resolve locally. The sym is either undefined or dynamic. */
3152 else if (!h->def_regular)
3153 return FALSE;
3154
3155 /* Non-dynamic symbols resolve locally. */
3156 if (h->dynindx == -1)
3157 return TRUE;
3158
3159 /* At this point, we know the symbol is defined and dynamic. In an
3160 executable it must resolve locally, likewise when building symbolic
3161 shared libraries. */
3162 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3163 return TRUE;
3164
3165 /* Now deal with defined dynamic symbols in shared libraries. Ones
3166 with default visibility might not resolve locally. */
3167 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3168 return FALSE;
3169
3170 hash_table = elf_hash_table (info);
3171 if (!is_elf_hash_table (hash_table))
3172 return TRUE;
3173
3174 bed = get_elf_backend_data (hash_table->dynobj);
3175
3176 /* If extern_protected_data is false, STV_PROTECTED non-function
3177 symbols are local. */
3178 if ((!info->extern_protected_data
3179 || (info->extern_protected_data < 0
3180 && !bed->extern_protected_data))
3181 && !bed->is_function_type (h->type))
3182 return TRUE;
3183
3184 /* Function pointer equality tests may require that STV_PROTECTED
3185 symbols be treated as dynamic symbols. If the address of a
3186 function not defined in an executable is set to that function's
3187 plt entry in the executable, then the address of the function in
3188 a shared library must also be the plt entry in the executable. */
3189 return local_protected;
3190 }
3191
3192 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3193 aligned. Returns the first TLS output section. */
3194
3195 struct bfd_section *
3196 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3197 {
3198 struct bfd_section *sec, *tls;
3199 unsigned int align = 0;
3200
3201 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3202 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3203 break;
3204 tls = sec;
3205
3206 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3207 if (sec->alignment_power > align)
3208 align = sec->alignment_power;
3209
3210 elf_hash_table (info)->tls_sec = tls;
3211
3212 /* Ensure the alignment of the first section is the largest alignment,
3213 so that the tls segment starts aligned. */
3214 if (tls != NULL)
3215 tls->alignment_power = align;
3216
3217 return tls;
3218 }
3219
3220 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3221 static bfd_boolean
3222 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3223 Elf_Internal_Sym *sym)
3224 {
3225 const struct elf_backend_data *bed;
3226
3227 /* Local symbols do not count, but target specific ones might. */
3228 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3229 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3230 return FALSE;
3231
3232 bed = get_elf_backend_data (abfd);
3233 /* Function symbols do not count. */
3234 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3235 return FALSE;
3236
3237 /* If the section is undefined, then so is the symbol. */
3238 if (sym->st_shndx == SHN_UNDEF)
3239 return FALSE;
3240
3241 /* If the symbol is defined in the common section, then
3242 it is a common definition and so does not count. */
3243 if (bed->common_definition (sym))
3244 return FALSE;
3245
3246 /* If the symbol is in a target specific section then we
3247 must rely upon the backend to tell us what it is. */
3248 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3249 /* FIXME - this function is not coded yet:
3250
3251 return _bfd_is_global_symbol_definition (abfd, sym);
3252
3253 Instead for now assume that the definition is not global,
3254 Even if this is wrong, at least the linker will behave
3255 in the same way that it used to do. */
3256 return FALSE;
3257
3258 return TRUE;
3259 }
3260
3261 /* Search the symbol table of the archive element of the archive ABFD
3262 whose archive map contains a mention of SYMDEF, and determine if
3263 the symbol is defined in this element. */
3264 static bfd_boolean
3265 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3266 {
3267 Elf_Internal_Shdr * hdr;
3268 size_t symcount;
3269 size_t extsymcount;
3270 size_t extsymoff;
3271 Elf_Internal_Sym *isymbuf;
3272 Elf_Internal_Sym *isym;
3273 Elf_Internal_Sym *isymend;
3274 bfd_boolean result;
3275
3276 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3277 if (abfd == NULL)
3278 return FALSE;
3279
3280 if (! bfd_check_format (abfd, bfd_object))
3281 return FALSE;
3282
3283 /* Select the appropriate symbol table. If we don't know if the
3284 object file is an IR object, give linker LTO plugin a chance to
3285 get the correct symbol table. */
3286 if (abfd->plugin_format == bfd_plugin_yes
3287 #if BFD_SUPPORTS_PLUGINS
3288 || (abfd->plugin_format == bfd_plugin_unknown
3289 && bfd_link_plugin_object_p (abfd))
3290 #endif
3291 )
3292 {
3293 /* Use the IR symbol table if the object has been claimed by
3294 plugin. */
3295 abfd = abfd->plugin_dummy_bfd;
3296 hdr = &elf_tdata (abfd)->symtab_hdr;
3297 }
3298 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3299 hdr = &elf_tdata (abfd)->symtab_hdr;
3300 else
3301 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3302
3303 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3304
3305 /* The sh_info field of the symtab header tells us where the
3306 external symbols start. We don't care about the local symbols. */
3307 if (elf_bad_symtab (abfd))
3308 {
3309 extsymcount = symcount;
3310 extsymoff = 0;
3311 }
3312 else
3313 {
3314 extsymcount = symcount - hdr->sh_info;
3315 extsymoff = hdr->sh_info;
3316 }
3317
3318 if (extsymcount == 0)
3319 return FALSE;
3320
3321 /* Read in the symbol table. */
3322 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3323 NULL, NULL, NULL);
3324 if (isymbuf == NULL)
3325 return FALSE;
3326
3327 /* Scan the symbol table looking for SYMDEF. */
3328 result = FALSE;
3329 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3330 {
3331 const char *name;
3332
3333 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3334 isym->st_name);
3335 if (name == NULL)
3336 break;
3337
3338 if (strcmp (name, symdef->name) == 0)
3339 {
3340 result = is_global_data_symbol_definition (abfd, isym);
3341 break;
3342 }
3343 }
3344
3345 free (isymbuf);
3346
3347 return result;
3348 }
3349 \f
3350 /* Add an entry to the .dynamic table. */
3351
3352 bfd_boolean
3353 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3354 bfd_vma tag,
3355 bfd_vma val)
3356 {
3357 struct elf_link_hash_table *hash_table;
3358 const struct elf_backend_data *bed;
3359 asection *s;
3360 bfd_size_type newsize;
3361 bfd_byte *newcontents;
3362 Elf_Internal_Dyn dyn;
3363
3364 hash_table = elf_hash_table (info);
3365 if (! is_elf_hash_table (hash_table))
3366 return FALSE;
3367
3368 bed = get_elf_backend_data (hash_table->dynobj);
3369 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3370 BFD_ASSERT (s != NULL);
3371
3372 newsize = s->size + bed->s->sizeof_dyn;
3373 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3374 if (newcontents == NULL)
3375 return FALSE;
3376
3377 dyn.d_tag = tag;
3378 dyn.d_un.d_val = val;
3379 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3380
3381 s->size = newsize;
3382 s->contents = newcontents;
3383
3384 return TRUE;
3385 }
3386
3387 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3388 otherwise just check whether one already exists. Returns -1 on error,
3389 1 if a DT_NEEDED tag already exists, and 0 on success. */
3390
3391 static int
3392 elf_add_dt_needed_tag (bfd *abfd,
3393 struct bfd_link_info *info,
3394 const char *soname,
3395 bfd_boolean do_it)
3396 {
3397 struct elf_link_hash_table *hash_table;
3398 size_t strindex;
3399
3400 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3401 return -1;
3402
3403 hash_table = elf_hash_table (info);
3404 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3405 if (strindex == (size_t) -1)
3406 return -1;
3407
3408 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3409 {
3410 asection *sdyn;
3411 const struct elf_backend_data *bed;
3412 bfd_byte *extdyn;
3413
3414 bed = get_elf_backend_data (hash_table->dynobj);
3415 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3416 if (sdyn != NULL)
3417 for (extdyn = sdyn->contents;
3418 extdyn < sdyn->contents + sdyn->size;
3419 extdyn += bed->s->sizeof_dyn)
3420 {
3421 Elf_Internal_Dyn dyn;
3422
3423 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3424 if (dyn.d_tag == DT_NEEDED
3425 && dyn.d_un.d_val == strindex)
3426 {
3427 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3428 return 1;
3429 }
3430 }
3431 }
3432
3433 if (do_it)
3434 {
3435 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3436 return -1;
3437
3438 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3439 return -1;
3440 }
3441 else
3442 /* We were just checking for existence of the tag. */
3443 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3444
3445 return 0;
3446 }
3447
3448 /* Return true if SONAME is on the needed list between NEEDED and STOP
3449 (or the end of list if STOP is NULL), and needed by a library that
3450 will be loaded. */
3451
3452 static bfd_boolean
3453 on_needed_list (const char *soname,
3454 struct bfd_link_needed_list *needed,
3455 struct bfd_link_needed_list *stop)
3456 {
3457 struct bfd_link_needed_list *look;
3458 for (look = needed; look != stop; look = look->next)
3459 if (strcmp (soname, look->name) == 0
3460 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3461 /* If needed by a library that itself is not directly
3462 needed, recursively check whether that library is
3463 indirectly needed. Since we add DT_NEEDED entries to
3464 the end of the list, library dependencies appear after
3465 the library. Therefore search prior to the current
3466 LOOK, preventing possible infinite recursion. */
3467 || on_needed_list (elf_dt_name (look->by), needed, look)))
3468 return TRUE;
3469
3470 return FALSE;
3471 }
3472
3473 /* Sort symbol by value, section, and size. */
3474 static int
3475 elf_sort_symbol (const void *arg1, const void *arg2)
3476 {
3477 const struct elf_link_hash_entry *h1;
3478 const struct elf_link_hash_entry *h2;
3479 bfd_signed_vma vdiff;
3480
3481 h1 = *(const struct elf_link_hash_entry **) arg1;
3482 h2 = *(const struct elf_link_hash_entry **) arg2;
3483 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3484 if (vdiff != 0)
3485 return vdiff > 0 ? 1 : -1;
3486 else
3487 {
3488 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3489 if (sdiff != 0)
3490 return sdiff > 0 ? 1 : -1;
3491 }
3492 vdiff = h1->size - h2->size;
3493 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3494 }
3495
3496 /* This function is used to adjust offsets into .dynstr for
3497 dynamic symbols. This is called via elf_link_hash_traverse. */
3498
3499 static bfd_boolean
3500 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3501 {
3502 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3503
3504 if (h->dynindx != -1)
3505 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3506 return TRUE;
3507 }
3508
3509 /* Assign string offsets in .dynstr, update all structures referencing
3510 them. */
3511
3512 static bfd_boolean
3513 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3514 {
3515 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3516 struct elf_link_local_dynamic_entry *entry;
3517 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3518 bfd *dynobj = hash_table->dynobj;
3519 asection *sdyn;
3520 bfd_size_type size;
3521 const struct elf_backend_data *bed;
3522 bfd_byte *extdyn;
3523
3524 _bfd_elf_strtab_finalize (dynstr);
3525 size = _bfd_elf_strtab_size (dynstr);
3526
3527 bed = get_elf_backend_data (dynobj);
3528 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3529 BFD_ASSERT (sdyn != NULL);
3530
3531 /* Update all .dynamic entries referencing .dynstr strings. */
3532 for (extdyn = sdyn->contents;
3533 extdyn < sdyn->contents + sdyn->size;
3534 extdyn += bed->s->sizeof_dyn)
3535 {
3536 Elf_Internal_Dyn dyn;
3537
3538 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3539 switch (dyn.d_tag)
3540 {
3541 case DT_STRSZ:
3542 dyn.d_un.d_val = size;
3543 break;
3544 case DT_NEEDED:
3545 case DT_SONAME:
3546 case DT_RPATH:
3547 case DT_RUNPATH:
3548 case DT_FILTER:
3549 case DT_AUXILIARY:
3550 case DT_AUDIT:
3551 case DT_DEPAUDIT:
3552 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3553 break;
3554 default:
3555 continue;
3556 }
3557 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3558 }
3559
3560 /* Now update local dynamic symbols. */
3561 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3562 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3563 entry->isym.st_name);
3564
3565 /* And the rest of dynamic symbols. */
3566 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3567
3568 /* Adjust version definitions. */
3569 if (elf_tdata (output_bfd)->cverdefs)
3570 {
3571 asection *s;
3572 bfd_byte *p;
3573 size_t i;
3574 Elf_Internal_Verdef def;
3575 Elf_Internal_Verdaux defaux;
3576
3577 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3578 p = s->contents;
3579 do
3580 {
3581 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3582 &def);
3583 p += sizeof (Elf_External_Verdef);
3584 if (def.vd_aux != sizeof (Elf_External_Verdef))
3585 continue;
3586 for (i = 0; i < def.vd_cnt; ++i)
3587 {
3588 _bfd_elf_swap_verdaux_in (output_bfd,
3589 (Elf_External_Verdaux *) p, &defaux);
3590 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3591 defaux.vda_name);
3592 _bfd_elf_swap_verdaux_out (output_bfd,
3593 &defaux, (Elf_External_Verdaux *) p);
3594 p += sizeof (Elf_External_Verdaux);
3595 }
3596 }
3597 while (def.vd_next);
3598 }
3599
3600 /* Adjust version references. */
3601 if (elf_tdata (output_bfd)->verref)
3602 {
3603 asection *s;
3604 bfd_byte *p;
3605 size_t i;
3606 Elf_Internal_Verneed need;
3607 Elf_Internal_Vernaux needaux;
3608
3609 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3610 p = s->contents;
3611 do
3612 {
3613 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3614 &need);
3615 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3616 _bfd_elf_swap_verneed_out (output_bfd, &need,
3617 (Elf_External_Verneed *) p);
3618 p += sizeof (Elf_External_Verneed);
3619 for (i = 0; i < need.vn_cnt; ++i)
3620 {
3621 _bfd_elf_swap_vernaux_in (output_bfd,
3622 (Elf_External_Vernaux *) p, &needaux);
3623 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3624 needaux.vna_name);
3625 _bfd_elf_swap_vernaux_out (output_bfd,
3626 &needaux,
3627 (Elf_External_Vernaux *) p);
3628 p += sizeof (Elf_External_Vernaux);
3629 }
3630 }
3631 while (need.vn_next);
3632 }
3633
3634 return TRUE;
3635 }
3636 \f
3637 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3638 The default is to only match when the INPUT and OUTPUT are exactly
3639 the same target. */
3640
3641 bfd_boolean
3642 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3643 const bfd_target *output)
3644 {
3645 return input == output;
3646 }
3647
3648 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3649 This version is used when different targets for the same architecture
3650 are virtually identical. */
3651
3652 bfd_boolean
3653 _bfd_elf_relocs_compatible (const bfd_target *input,
3654 const bfd_target *output)
3655 {
3656 const struct elf_backend_data *obed, *ibed;
3657
3658 if (input == output)
3659 return TRUE;
3660
3661 ibed = xvec_get_elf_backend_data (input);
3662 obed = xvec_get_elf_backend_data (output);
3663
3664 if (ibed->arch != obed->arch)
3665 return FALSE;
3666
3667 /* If both backends are using this function, deem them compatible. */
3668 return ibed->relocs_compatible == obed->relocs_compatible;
3669 }
3670
3671 /* Make a special call to the linker "notice" function to tell it that
3672 we are about to handle an as-needed lib, or have finished
3673 processing the lib. */
3674
3675 bfd_boolean
3676 _bfd_elf_notice_as_needed (bfd *ibfd,
3677 struct bfd_link_info *info,
3678 enum notice_asneeded_action act)
3679 {
3680 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3681 }
3682
3683 /* Check relocations an ELF object file. */
3684
3685 bfd_boolean
3686 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3687 {
3688 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3689 struct elf_link_hash_table *htab = elf_hash_table (info);
3690
3691 /* If this object is the same format as the output object, and it is
3692 not a shared library, then let the backend look through the
3693 relocs.
3694
3695 This is required to build global offset table entries and to
3696 arrange for dynamic relocs. It is not required for the
3697 particular common case of linking non PIC code, even when linking
3698 against shared libraries, but unfortunately there is no way of
3699 knowing whether an object file has been compiled PIC or not.
3700 Looking through the relocs is not particularly time consuming.
3701 The problem is that we must either (1) keep the relocs in memory,
3702 which causes the linker to require additional runtime memory or
3703 (2) read the relocs twice from the input file, which wastes time.
3704 This would be a good case for using mmap.
3705
3706 I have no idea how to handle linking PIC code into a file of a
3707 different format. It probably can't be done. */
3708 if ((abfd->flags & DYNAMIC) == 0
3709 && is_elf_hash_table (htab)
3710 && bed->check_relocs != NULL
3711 && elf_object_id (abfd) == elf_hash_table_id (htab)
3712 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3713 {
3714 asection *o;
3715
3716 for (o = abfd->sections; o != NULL; o = o->next)
3717 {
3718 Elf_Internal_Rela *internal_relocs;
3719 bfd_boolean ok;
3720
3721 /* Don't check relocations in excluded sections. */
3722 if ((o->flags & SEC_RELOC) == 0
3723 || (o->flags & SEC_EXCLUDE) != 0
3724 || o->reloc_count == 0
3725 || ((info->strip == strip_all || info->strip == strip_debugger)
3726 && (o->flags & SEC_DEBUGGING) != 0)
3727 || bfd_is_abs_section (o->output_section))
3728 continue;
3729
3730 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3731 info->keep_memory);
3732 if (internal_relocs == NULL)
3733 return FALSE;
3734
3735 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3736
3737 if (elf_section_data (o)->relocs != internal_relocs)
3738 free (internal_relocs);
3739
3740 if (! ok)
3741 return FALSE;
3742 }
3743 }
3744
3745 return TRUE;
3746 }
3747
3748 /* Add symbols from an ELF object file to the linker hash table. */
3749
3750 static bfd_boolean
3751 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3752 {
3753 Elf_Internal_Ehdr *ehdr;
3754 Elf_Internal_Shdr *hdr;
3755 size_t symcount;
3756 size_t extsymcount;
3757 size_t extsymoff;
3758 struct elf_link_hash_entry **sym_hash;
3759 bfd_boolean dynamic;
3760 Elf_External_Versym *extversym = NULL;
3761 Elf_External_Versym *ever;
3762 struct elf_link_hash_entry *weaks;
3763 struct elf_link_hash_entry **nondeflt_vers = NULL;
3764 size_t nondeflt_vers_cnt = 0;
3765 Elf_Internal_Sym *isymbuf = NULL;
3766 Elf_Internal_Sym *isym;
3767 Elf_Internal_Sym *isymend;
3768 const struct elf_backend_data *bed;
3769 bfd_boolean add_needed;
3770 struct elf_link_hash_table *htab;
3771 bfd_size_type amt;
3772 void *alloc_mark = NULL;
3773 struct bfd_hash_entry **old_table = NULL;
3774 unsigned int old_size = 0;
3775 unsigned int old_count = 0;
3776 void *old_tab = NULL;
3777 void *old_ent;
3778 struct bfd_link_hash_entry *old_undefs = NULL;
3779 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3780 void *old_strtab = NULL;
3781 size_t tabsize = 0;
3782 asection *s;
3783 bfd_boolean just_syms;
3784
3785 htab = elf_hash_table (info);
3786 bed = get_elf_backend_data (abfd);
3787
3788 if ((abfd->flags & DYNAMIC) == 0)
3789 dynamic = FALSE;
3790 else
3791 {
3792 dynamic = TRUE;
3793
3794 /* You can't use -r against a dynamic object. Also, there's no
3795 hope of using a dynamic object which does not exactly match
3796 the format of the output file. */
3797 if (bfd_link_relocatable (info)
3798 || !is_elf_hash_table (htab)
3799 || info->output_bfd->xvec != abfd->xvec)
3800 {
3801 if (bfd_link_relocatable (info))
3802 bfd_set_error (bfd_error_invalid_operation);
3803 else
3804 bfd_set_error (bfd_error_wrong_format);
3805 goto error_return;
3806 }
3807 }
3808
3809 ehdr = elf_elfheader (abfd);
3810 if (info->warn_alternate_em
3811 && bed->elf_machine_code != ehdr->e_machine
3812 && ((bed->elf_machine_alt1 != 0
3813 && ehdr->e_machine == bed->elf_machine_alt1)
3814 || (bed->elf_machine_alt2 != 0
3815 && ehdr->e_machine == bed->elf_machine_alt2)))
3816 info->callbacks->einfo
3817 /* xgettext:c-format */
3818 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3819 ehdr->e_machine, abfd, bed->elf_machine_code);
3820
3821 /* As a GNU extension, any input sections which are named
3822 .gnu.warning.SYMBOL are treated as warning symbols for the given
3823 symbol. This differs from .gnu.warning sections, which generate
3824 warnings when they are included in an output file. */
3825 /* PR 12761: Also generate this warning when building shared libraries. */
3826 for (s = abfd->sections; s != NULL; s = s->next)
3827 {
3828 const char *name;
3829
3830 name = bfd_get_section_name (abfd, s);
3831 if (CONST_STRNEQ (name, ".gnu.warning."))
3832 {
3833 char *msg;
3834 bfd_size_type sz;
3835
3836 name += sizeof ".gnu.warning." - 1;
3837
3838 /* If this is a shared object, then look up the symbol
3839 in the hash table. If it is there, and it is already
3840 been defined, then we will not be using the entry
3841 from this shared object, so we don't need to warn.
3842 FIXME: If we see the definition in a regular object
3843 later on, we will warn, but we shouldn't. The only
3844 fix is to keep track of what warnings we are supposed
3845 to emit, and then handle them all at the end of the
3846 link. */
3847 if (dynamic)
3848 {
3849 struct elf_link_hash_entry *h;
3850
3851 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3852
3853 /* FIXME: What about bfd_link_hash_common? */
3854 if (h != NULL
3855 && (h->root.type == bfd_link_hash_defined
3856 || h->root.type == bfd_link_hash_defweak))
3857 continue;
3858 }
3859
3860 sz = s->size;
3861 msg = (char *) bfd_alloc (abfd, sz + 1);
3862 if (msg == NULL)
3863 goto error_return;
3864
3865 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3866 goto error_return;
3867
3868 msg[sz] = '\0';
3869
3870 if (! (_bfd_generic_link_add_one_symbol
3871 (info, abfd, name, BSF_WARNING, s, 0, msg,
3872 FALSE, bed->collect, NULL)))
3873 goto error_return;
3874
3875 if (bfd_link_executable (info))
3876 {
3877 /* Clobber the section size so that the warning does
3878 not get copied into the output file. */
3879 s->size = 0;
3880
3881 /* Also set SEC_EXCLUDE, so that symbols defined in
3882 the warning section don't get copied to the output. */
3883 s->flags |= SEC_EXCLUDE;
3884 }
3885 }
3886 }
3887
3888 just_syms = ((s = abfd->sections) != NULL
3889 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3890
3891 add_needed = TRUE;
3892 if (! dynamic)
3893 {
3894 /* If we are creating a shared library, create all the dynamic
3895 sections immediately. We need to attach them to something,
3896 so we attach them to this BFD, provided it is the right
3897 format and is not from ld --just-symbols. Always create the
3898 dynamic sections for -E/--dynamic-list. FIXME: If there
3899 are no input BFD's of the same format as the output, we can't
3900 make a shared library. */
3901 if (!just_syms
3902 && (bfd_link_pic (info)
3903 || (!bfd_link_relocatable (info)
3904 && info->nointerp
3905 && (info->export_dynamic || info->dynamic)))
3906 && is_elf_hash_table (htab)
3907 && info->output_bfd->xvec == abfd->xvec
3908 && !htab->dynamic_sections_created)
3909 {
3910 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3911 goto error_return;
3912 }
3913 }
3914 else if (!is_elf_hash_table (htab))
3915 goto error_return;
3916 else
3917 {
3918 const char *soname = NULL;
3919 char *audit = NULL;
3920 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3921 const Elf_Internal_Phdr *phdr;
3922 int ret;
3923
3924 /* ld --just-symbols and dynamic objects don't mix very well.
3925 ld shouldn't allow it. */
3926 if (just_syms)
3927 abort ();
3928
3929 /* If this dynamic lib was specified on the command line with
3930 --as-needed in effect, then we don't want to add a DT_NEEDED
3931 tag unless the lib is actually used. Similary for libs brought
3932 in by another lib's DT_NEEDED. When --no-add-needed is used
3933 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3934 any dynamic library in DT_NEEDED tags in the dynamic lib at
3935 all. */
3936 add_needed = (elf_dyn_lib_class (abfd)
3937 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3938 | DYN_NO_NEEDED)) == 0;
3939
3940 s = bfd_get_section_by_name (abfd, ".dynamic");
3941 if (s != NULL)
3942 {
3943 bfd_byte *dynbuf;
3944 bfd_byte *extdyn;
3945 unsigned int elfsec;
3946 unsigned long shlink;
3947
3948 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3949 {
3950 error_free_dyn:
3951 free (dynbuf);
3952 goto error_return;
3953 }
3954
3955 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3956 if (elfsec == SHN_BAD)
3957 goto error_free_dyn;
3958 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3959
3960 for (extdyn = dynbuf;
3961 extdyn < dynbuf + s->size;
3962 extdyn += bed->s->sizeof_dyn)
3963 {
3964 Elf_Internal_Dyn dyn;
3965
3966 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3967 if (dyn.d_tag == DT_SONAME)
3968 {
3969 unsigned int tagv = dyn.d_un.d_val;
3970 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3971 if (soname == NULL)
3972 goto error_free_dyn;
3973 }
3974 if (dyn.d_tag == DT_NEEDED)
3975 {
3976 struct bfd_link_needed_list *n, **pn;
3977 char *fnm, *anm;
3978 unsigned int tagv = dyn.d_un.d_val;
3979
3980 amt = sizeof (struct bfd_link_needed_list);
3981 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3982 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3983 if (n == NULL || fnm == NULL)
3984 goto error_free_dyn;
3985 amt = strlen (fnm) + 1;
3986 anm = (char *) bfd_alloc (abfd, amt);
3987 if (anm == NULL)
3988 goto error_free_dyn;
3989 memcpy (anm, fnm, amt);
3990 n->name = anm;
3991 n->by = abfd;
3992 n->next = NULL;
3993 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3994 ;
3995 *pn = n;
3996 }
3997 if (dyn.d_tag == DT_RUNPATH)
3998 {
3999 struct bfd_link_needed_list *n, **pn;
4000 char *fnm, *anm;
4001 unsigned int tagv = dyn.d_un.d_val;
4002
4003 amt = sizeof (struct bfd_link_needed_list);
4004 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4005 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4006 if (n == NULL || fnm == NULL)
4007 goto error_free_dyn;
4008 amt = strlen (fnm) + 1;
4009 anm = (char *) bfd_alloc (abfd, amt);
4010 if (anm == NULL)
4011 goto error_free_dyn;
4012 memcpy (anm, fnm, amt);
4013 n->name = anm;
4014 n->by = abfd;
4015 n->next = NULL;
4016 for (pn = & runpath;
4017 *pn != NULL;
4018 pn = &(*pn)->next)
4019 ;
4020 *pn = n;
4021 }
4022 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4023 if (!runpath && dyn.d_tag == DT_RPATH)
4024 {
4025 struct bfd_link_needed_list *n, **pn;
4026 char *fnm, *anm;
4027 unsigned int tagv = dyn.d_un.d_val;
4028
4029 amt = sizeof (struct bfd_link_needed_list);
4030 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4031 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4032 if (n == NULL || fnm == NULL)
4033 goto error_free_dyn;
4034 amt = strlen (fnm) + 1;
4035 anm = (char *) bfd_alloc (abfd, amt);
4036 if (anm == NULL)
4037 goto error_free_dyn;
4038 memcpy (anm, fnm, amt);
4039 n->name = anm;
4040 n->by = abfd;
4041 n->next = NULL;
4042 for (pn = & rpath;
4043 *pn != NULL;
4044 pn = &(*pn)->next)
4045 ;
4046 *pn = n;
4047 }
4048 if (dyn.d_tag == DT_AUDIT)
4049 {
4050 unsigned int tagv = dyn.d_un.d_val;
4051 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4052 }
4053 }
4054
4055 free (dynbuf);
4056 }
4057
4058 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4059 frees all more recently bfd_alloc'd blocks as well. */
4060 if (runpath)
4061 rpath = runpath;
4062
4063 if (rpath)
4064 {
4065 struct bfd_link_needed_list **pn;
4066 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4067 ;
4068 *pn = rpath;
4069 }
4070
4071 /* If we have a PT_GNU_RELRO program header, mark as read-only
4072 all sections contained fully therein. This makes relro
4073 shared library sections appear as they will at run-time. */
4074 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4075 while (--phdr >= elf_tdata (abfd)->phdr)
4076 if (phdr->p_type == PT_GNU_RELRO)
4077 {
4078 for (s = abfd->sections; s != NULL; s = s->next)
4079 if ((s->flags & SEC_ALLOC) != 0
4080 && s->vma >= phdr->p_vaddr
4081 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4082 s->flags |= SEC_READONLY;
4083 break;
4084 }
4085
4086 /* We do not want to include any of the sections in a dynamic
4087 object in the output file. We hack by simply clobbering the
4088 list of sections in the BFD. This could be handled more
4089 cleanly by, say, a new section flag; the existing
4090 SEC_NEVER_LOAD flag is not the one we want, because that one
4091 still implies that the section takes up space in the output
4092 file. */
4093 bfd_section_list_clear (abfd);
4094
4095 /* Find the name to use in a DT_NEEDED entry that refers to this
4096 object. If the object has a DT_SONAME entry, we use it.
4097 Otherwise, if the generic linker stuck something in
4098 elf_dt_name, we use that. Otherwise, we just use the file
4099 name. */
4100 if (soname == NULL || *soname == '\0')
4101 {
4102 soname = elf_dt_name (abfd);
4103 if (soname == NULL || *soname == '\0')
4104 soname = bfd_get_filename (abfd);
4105 }
4106
4107 /* Save the SONAME because sometimes the linker emulation code
4108 will need to know it. */
4109 elf_dt_name (abfd) = soname;
4110
4111 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4112 if (ret < 0)
4113 goto error_return;
4114
4115 /* If we have already included this dynamic object in the
4116 link, just ignore it. There is no reason to include a
4117 particular dynamic object more than once. */
4118 if (ret > 0)
4119 return TRUE;
4120
4121 /* Save the DT_AUDIT entry for the linker emulation code. */
4122 elf_dt_audit (abfd) = audit;
4123 }
4124
4125 /* If this is a dynamic object, we always link against the .dynsym
4126 symbol table, not the .symtab symbol table. The dynamic linker
4127 will only see the .dynsym symbol table, so there is no reason to
4128 look at .symtab for a dynamic object. */
4129
4130 if (! dynamic || elf_dynsymtab (abfd) == 0)
4131 hdr = &elf_tdata (abfd)->symtab_hdr;
4132 else
4133 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4134
4135 symcount = hdr->sh_size / bed->s->sizeof_sym;
4136
4137 /* The sh_info field of the symtab header tells us where the
4138 external symbols start. We don't care about the local symbols at
4139 this point. */
4140 if (elf_bad_symtab (abfd))
4141 {
4142 extsymcount = symcount;
4143 extsymoff = 0;
4144 }
4145 else
4146 {
4147 extsymcount = symcount - hdr->sh_info;
4148 extsymoff = hdr->sh_info;
4149 }
4150
4151 sym_hash = elf_sym_hashes (abfd);
4152 if (extsymcount != 0)
4153 {
4154 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4155 NULL, NULL, NULL);
4156 if (isymbuf == NULL)
4157 goto error_return;
4158
4159 if (sym_hash == NULL)
4160 {
4161 /* We store a pointer to the hash table entry for each
4162 external symbol. */
4163 amt = extsymcount;
4164 amt *= sizeof (struct elf_link_hash_entry *);
4165 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4166 if (sym_hash == NULL)
4167 goto error_free_sym;
4168 elf_sym_hashes (abfd) = sym_hash;
4169 }
4170 }
4171
4172 if (dynamic)
4173 {
4174 /* Read in any version definitions. */
4175 if (!_bfd_elf_slurp_version_tables (abfd,
4176 info->default_imported_symver))
4177 goto error_free_sym;
4178
4179 /* Read in the symbol versions, but don't bother to convert them
4180 to internal format. */
4181 if (elf_dynversym (abfd) != 0)
4182 {
4183 Elf_Internal_Shdr *versymhdr;
4184
4185 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4186 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4187 if (extversym == NULL)
4188 goto error_free_sym;
4189 amt = versymhdr->sh_size;
4190 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4191 || bfd_bread (extversym, amt, abfd) != amt)
4192 goto error_free_vers;
4193 }
4194 }
4195
4196 /* If we are loading an as-needed shared lib, save the symbol table
4197 state before we start adding symbols. If the lib turns out
4198 to be unneeded, restore the state. */
4199 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4200 {
4201 unsigned int i;
4202 size_t entsize;
4203
4204 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4205 {
4206 struct bfd_hash_entry *p;
4207 struct elf_link_hash_entry *h;
4208
4209 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4210 {
4211 h = (struct elf_link_hash_entry *) p;
4212 entsize += htab->root.table.entsize;
4213 if (h->root.type == bfd_link_hash_warning)
4214 entsize += htab->root.table.entsize;
4215 }
4216 }
4217
4218 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4219 old_tab = bfd_malloc (tabsize + entsize);
4220 if (old_tab == NULL)
4221 goto error_free_vers;
4222
4223 /* Remember the current objalloc pointer, so that all mem for
4224 symbols added can later be reclaimed. */
4225 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4226 if (alloc_mark == NULL)
4227 goto error_free_vers;
4228
4229 /* Make a special call to the linker "notice" function to
4230 tell it that we are about to handle an as-needed lib. */
4231 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4232 goto error_free_vers;
4233
4234 /* Clone the symbol table. Remember some pointers into the
4235 symbol table, and dynamic symbol count. */
4236 old_ent = (char *) old_tab + tabsize;
4237 memcpy (old_tab, htab->root.table.table, tabsize);
4238 old_undefs = htab->root.undefs;
4239 old_undefs_tail = htab->root.undefs_tail;
4240 old_table = htab->root.table.table;
4241 old_size = htab->root.table.size;
4242 old_count = htab->root.table.count;
4243 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4244 if (old_strtab == NULL)
4245 goto error_free_vers;
4246
4247 for (i = 0; i < htab->root.table.size; i++)
4248 {
4249 struct bfd_hash_entry *p;
4250 struct elf_link_hash_entry *h;
4251
4252 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4253 {
4254 memcpy (old_ent, p, htab->root.table.entsize);
4255 old_ent = (char *) old_ent + htab->root.table.entsize;
4256 h = (struct elf_link_hash_entry *) p;
4257 if (h->root.type == bfd_link_hash_warning)
4258 {
4259 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4260 old_ent = (char *) old_ent + htab->root.table.entsize;
4261 }
4262 }
4263 }
4264 }
4265
4266 weaks = NULL;
4267 ever = extversym != NULL ? extversym + extsymoff : NULL;
4268 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4269 isym < isymend;
4270 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4271 {
4272 int bind;
4273 bfd_vma value;
4274 asection *sec, *new_sec;
4275 flagword flags;
4276 const char *name;
4277 struct elf_link_hash_entry *h;
4278 struct elf_link_hash_entry *hi;
4279 bfd_boolean definition;
4280 bfd_boolean size_change_ok;
4281 bfd_boolean type_change_ok;
4282 bfd_boolean new_weak;
4283 bfd_boolean old_weak;
4284 bfd_boolean override;
4285 bfd_boolean common;
4286 bfd_boolean discarded;
4287 unsigned int old_alignment;
4288 bfd *old_bfd;
4289 bfd_boolean matched;
4290
4291 override = FALSE;
4292
4293 flags = BSF_NO_FLAGS;
4294 sec = NULL;
4295 value = isym->st_value;
4296 common = bed->common_definition (isym);
4297 if (common && info->inhibit_common_definition)
4298 {
4299 /* Treat common symbol as undefined for --no-define-common. */
4300 isym->st_shndx = SHN_UNDEF;
4301 common = FALSE;
4302 }
4303 discarded = FALSE;
4304
4305 bind = ELF_ST_BIND (isym->st_info);
4306 switch (bind)
4307 {
4308 case STB_LOCAL:
4309 /* This should be impossible, since ELF requires that all
4310 global symbols follow all local symbols, and that sh_info
4311 point to the first global symbol. Unfortunately, Irix 5
4312 screws this up. */
4313 continue;
4314
4315 case STB_GLOBAL:
4316 if (isym->st_shndx != SHN_UNDEF && !common)
4317 flags = BSF_GLOBAL;
4318 break;
4319
4320 case STB_WEAK:
4321 flags = BSF_WEAK;
4322 break;
4323
4324 case STB_GNU_UNIQUE:
4325 flags = BSF_GNU_UNIQUE;
4326 break;
4327
4328 default:
4329 /* Leave it up to the processor backend. */
4330 break;
4331 }
4332
4333 if (isym->st_shndx == SHN_UNDEF)
4334 sec = bfd_und_section_ptr;
4335 else if (isym->st_shndx == SHN_ABS)
4336 sec = bfd_abs_section_ptr;
4337 else if (isym->st_shndx == SHN_COMMON)
4338 {
4339 sec = bfd_com_section_ptr;
4340 /* What ELF calls the size we call the value. What ELF
4341 calls the value we call the alignment. */
4342 value = isym->st_size;
4343 }
4344 else
4345 {
4346 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4347 if (sec == NULL)
4348 sec = bfd_abs_section_ptr;
4349 else if (discarded_section (sec))
4350 {
4351 /* Symbols from discarded section are undefined. We keep
4352 its visibility. */
4353 sec = bfd_und_section_ptr;
4354 discarded = TRUE;
4355 isym->st_shndx = SHN_UNDEF;
4356 }
4357 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4358 value -= sec->vma;
4359 }
4360
4361 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4362 isym->st_name);
4363 if (name == NULL)
4364 goto error_free_vers;
4365
4366 if (isym->st_shndx == SHN_COMMON
4367 && (abfd->flags & BFD_PLUGIN) != 0)
4368 {
4369 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4370
4371 if (xc == NULL)
4372 {
4373 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4374 | SEC_EXCLUDE);
4375 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4376 if (xc == NULL)
4377 goto error_free_vers;
4378 }
4379 sec = xc;
4380 }
4381 else if (isym->st_shndx == SHN_COMMON
4382 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4383 && !bfd_link_relocatable (info))
4384 {
4385 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4386
4387 if (tcomm == NULL)
4388 {
4389 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4390 | SEC_LINKER_CREATED);
4391 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4392 if (tcomm == NULL)
4393 goto error_free_vers;
4394 }
4395 sec = tcomm;
4396 }
4397 else if (bed->elf_add_symbol_hook)
4398 {
4399 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4400 &sec, &value))
4401 goto error_free_vers;
4402
4403 /* The hook function sets the name to NULL if this symbol
4404 should be skipped for some reason. */
4405 if (name == NULL)
4406 continue;
4407 }
4408
4409 /* Sanity check that all possibilities were handled. */
4410 if (sec == NULL)
4411 {
4412 bfd_set_error (bfd_error_bad_value);
4413 goto error_free_vers;
4414 }
4415
4416 /* Silently discard TLS symbols from --just-syms. There's
4417 no way to combine a static TLS block with a new TLS block
4418 for this executable. */
4419 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4420 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4421 continue;
4422
4423 if (bfd_is_und_section (sec)
4424 || bfd_is_com_section (sec))
4425 definition = FALSE;
4426 else
4427 definition = TRUE;
4428
4429 size_change_ok = FALSE;
4430 type_change_ok = bed->type_change_ok;
4431 old_weak = FALSE;
4432 matched = FALSE;
4433 old_alignment = 0;
4434 old_bfd = NULL;
4435 new_sec = sec;
4436
4437 if (is_elf_hash_table (htab))
4438 {
4439 Elf_Internal_Versym iver;
4440 unsigned int vernum = 0;
4441 bfd_boolean skip;
4442
4443 if (ever == NULL)
4444 {
4445 if (info->default_imported_symver)
4446 /* Use the default symbol version created earlier. */
4447 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4448 else
4449 iver.vs_vers = 0;
4450 }
4451 else
4452 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4453
4454 vernum = iver.vs_vers & VERSYM_VERSION;
4455
4456 /* If this is a hidden symbol, or if it is not version
4457 1, we append the version name to the symbol name.
4458 However, we do not modify a non-hidden absolute symbol
4459 if it is not a function, because it might be the version
4460 symbol itself. FIXME: What if it isn't? */
4461 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4462 || (vernum > 1
4463 && (!bfd_is_abs_section (sec)
4464 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4465 {
4466 const char *verstr;
4467 size_t namelen, verlen, newlen;
4468 char *newname, *p;
4469
4470 if (isym->st_shndx != SHN_UNDEF)
4471 {
4472 if (vernum > elf_tdata (abfd)->cverdefs)
4473 verstr = NULL;
4474 else if (vernum > 1)
4475 verstr =
4476 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4477 else
4478 verstr = "";
4479
4480 if (verstr == NULL)
4481 {
4482 _bfd_error_handler
4483 /* xgettext:c-format */
4484 (_("%B: %s: invalid version %u (max %d)"),
4485 abfd, name, vernum,
4486 elf_tdata (abfd)->cverdefs);
4487 bfd_set_error (bfd_error_bad_value);
4488 goto error_free_vers;
4489 }
4490 }
4491 else
4492 {
4493 /* We cannot simply test for the number of
4494 entries in the VERNEED section since the
4495 numbers for the needed versions do not start
4496 at 0. */
4497 Elf_Internal_Verneed *t;
4498
4499 verstr = NULL;
4500 for (t = elf_tdata (abfd)->verref;
4501 t != NULL;
4502 t = t->vn_nextref)
4503 {
4504 Elf_Internal_Vernaux *a;
4505
4506 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4507 {
4508 if (a->vna_other == vernum)
4509 {
4510 verstr = a->vna_nodename;
4511 break;
4512 }
4513 }
4514 if (a != NULL)
4515 break;
4516 }
4517 if (verstr == NULL)
4518 {
4519 _bfd_error_handler
4520 /* xgettext:c-format */
4521 (_("%B: %s: invalid needed version %d"),
4522 abfd, name, vernum);
4523 bfd_set_error (bfd_error_bad_value);
4524 goto error_free_vers;
4525 }
4526 }
4527
4528 namelen = strlen (name);
4529 verlen = strlen (verstr);
4530 newlen = namelen + verlen + 2;
4531 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4532 && isym->st_shndx != SHN_UNDEF)
4533 ++newlen;
4534
4535 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4536 if (newname == NULL)
4537 goto error_free_vers;
4538 memcpy (newname, name, namelen);
4539 p = newname + namelen;
4540 *p++ = ELF_VER_CHR;
4541 /* If this is a defined non-hidden version symbol,
4542 we add another @ to the name. This indicates the
4543 default version of the symbol. */
4544 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4545 && isym->st_shndx != SHN_UNDEF)
4546 *p++ = ELF_VER_CHR;
4547 memcpy (p, verstr, verlen + 1);
4548
4549 name = newname;
4550 }
4551
4552 /* If this symbol has default visibility and the user has
4553 requested we not re-export it, then mark it as hidden. */
4554 if (!bfd_is_und_section (sec)
4555 && !dynamic
4556 && abfd->no_export
4557 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4558 isym->st_other = (STV_HIDDEN
4559 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4560
4561 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4562 sym_hash, &old_bfd, &old_weak,
4563 &old_alignment, &skip, &override,
4564 &type_change_ok, &size_change_ok,
4565 &matched))
4566 goto error_free_vers;
4567
4568 if (skip)
4569 continue;
4570
4571 /* Override a definition only if the new symbol matches the
4572 existing one. */
4573 if (override && matched)
4574 definition = FALSE;
4575
4576 h = *sym_hash;
4577 while (h->root.type == bfd_link_hash_indirect
4578 || h->root.type == bfd_link_hash_warning)
4579 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4580
4581 if (elf_tdata (abfd)->verdef != NULL
4582 && vernum > 1
4583 && definition)
4584 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4585 }
4586
4587 if (! (_bfd_generic_link_add_one_symbol
4588 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4589 (struct bfd_link_hash_entry **) sym_hash)))
4590 goto error_free_vers;
4591
4592 if ((flags & BSF_GNU_UNIQUE)
4593 && (abfd->flags & DYNAMIC) == 0
4594 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4595 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
4596
4597 h = *sym_hash;
4598 /* We need to make sure that indirect symbol dynamic flags are
4599 updated. */
4600 hi = h;
4601 while (h->root.type == bfd_link_hash_indirect
4602 || h->root.type == bfd_link_hash_warning)
4603 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4604
4605 /* Setting the index to -3 tells elf_link_output_extsym that
4606 this symbol is defined in a discarded section. */
4607 if (discarded)
4608 h->indx = -3;
4609
4610 *sym_hash = h;
4611
4612 new_weak = (flags & BSF_WEAK) != 0;
4613 if (dynamic
4614 && definition
4615 && new_weak
4616 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4617 && is_elf_hash_table (htab)
4618 && h->u.alias == NULL)
4619 {
4620 /* Keep a list of all weak defined non function symbols from
4621 a dynamic object, using the alias field. Later in this
4622 function we will set the alias field to the correct
4623 value. We only put non-function symbols from dynamic
4624 objects on this list, because that happens to be the only
4625 time we need to know the normal symbol corresponding to a
4626 weak symbol, and the information is time consuming to
4627 figure out. If the alias field is not already NULL,
4628 then this symbol was already defined by some previous
4629 dynamic object, and we will be using that previous
4630 definition anyhow. */
4631
4632 h->u.alias = weaks;
4633 weaks = h;
4634 }
4635
4636 /* Set the alignment of a common symbol. */
4637 if ((common || bfd_is_com_section (sec))
4638 && h->root.type == bfd_link_hash_common)
4639 {
4640 unsigned int align;
4641
4642 if (common)
4643 align = bfd_log2 (isym->st_value);
4644 else
4645 {
4646 /* The new symbol is a common symbol in a shared object.
4647 We need to get the alignment from the section. */
4648 align = new_sec->alignment_power;
4649 }
4650 if (align > old_alignment)
4651 h->root.u.c.p->alignment_power = align;
4652 else
4653 h->root.u.c.p->alignment_power = old_alignment;
4654 }
4655
4656 if (is_elf_hash_table (htab))
4657 {
4658 /* Set a flag in the hash table entry indicating the type of
4659 reference or definition we just found. A dynamic symbol
4660 is one which is referenced or defined by both a regular
4661 object and a shared object. */
4662 bfd_boolean dynsym = FALSE;
4663
4664 /* Plugin symbols aren't normal. Don't set def_regular or
4665 ref_regular for them, or make them dynamic. */
4666 if ((abfd->flags & BFD_PLUGIN) != 0)
4667 ;
4668 else if (! dynamic)
4669 {
4670 if (! definition)
4671 {
4672 h->ref_regular = 1;
4673 if (bind != STB_WEAK)
4674 h->ref_regular_nonweak = 1;
4675 }
4676 else
4677 {
4678 h->def_regular = 1;
4679 if (h->def_dynamic)
4680 {
4681 h->def_dynamic = 0;
4682 h->ref_dynamic = 1;
4683 }
4684 }
4685
4686 /* If the indirect symbol has been forced local, don't
4687 make the real symbol dynamic. */
4688 if ((h == hi || !hi->forced_local)
4689 && (bfd_link_dll (info)
4690 || h->def_dynamic
4691 || h->ref_dynamic))
4692 dynsym = TRUE;
4693 }
4694 else
4695 {
4696 if (! definition)
4697 {
4698 h->ref_dynamic = 1;
4699 hi->ref_dynamic = 1;
4700 }
4701 else
4702 {
4703 h->def_dynamic = 1;
4704 hi->def_dynamic = 1;
4705 }
4706
4707 /* If the indirect symbol has been forced local, don't
4708 make the real symbol dynamic. */
4709 if ((h == hi || !hi->forced_local)
4710 && (h->def_regular
4711 || h->ref_regular
4712 || (h->is_weakalias
4713 && weakdef (h)->dynindx != -1)))
4714 dynsym = TRUE;
4715 }
4716
4717 /* Check to see if we need to add an indirect symbol for
4718 the default name. */
4719 if (definition
4720 || (!override && h->root.type == bfd_link_hash_common))
4721 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4722 sec, value, &old_bfd, &dynsym))
4723 goto error_free_vers;
4724
4725 /* Check the alignment when a common symbol is involved. This
4726 can change when a common symbol is overridden by a normal
4727 definition or a common symbol is ignored due to the old
4728 normal definition. We need to make sure the maximum
4729 alignment is maintained. */
4730 if ((old_alignment || common)
4731 && h->root.type != bfd_link_hash_common)
4732 {
4733 unsigned int common_align;
4734 unsigned int normal_align;
4735 unsigned int symbol_align;
4736 bfd *normal_bfd;
4737 bfd *common_bfd;
4738
4739 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4740 || h->root.type == bfd_link_hash_defweak);
4741
4742 symbol_align = ffs (h->root.u.def.value) - 1;
4743 if (h->root.u.def.section->owner != NULL
4744 && (h->root.u.def.section->owner->flags
4745 & (DYNAMIC | BFD_PLUGIN)) == 0)
4746 {
4747 normal_align = h->root.u.def.section->alignment_power;
4748 if (normal_align > symbol_align)
4749 normal_align = symbol_align;
4750 }
4751 else
4752 normal_align = symbol_align;
4753
4754 if (old_alignment)
4755 {
4756 common_align = old_alignment;
4757 common_bfd = old_bfd;
4758 normal_bfd = abfd;
4759 }
4760 else
4761 {
4762 common_align = bfd_log2 (isym->st_value);
4763 common_bfd = abfd;
4764 normal_bfd = old_bfd;
4765 }
4766
4767 if (normal_align < common_align)
4768 {
4769 /* PR binutils/2735 */
4770 if (normal_bfd == NULL)
4771 _bfd_error_handler
4772 /* xgettext:c-format */
4773 (_("Warning: alignment %u of common symbol `%s' in %B is"
4774 " greater than the alignment (%u) of its section %A"),
4775 1 << common_align, name, common_bfd,
4776 1 << normal_align, h->root.u.def.section);
4777 else
4778 _bfd_error_handler
4779 /* xgettext:c-format */
4780 (_("Warning: alignment %u of symbol `%s' in %B"
4781 " is smaller than %u in %B"),
4782 1 << normal_align, name, normal_bfd,
4783 1 << common_align, common_bfd);
4784 }
4785 }
4786
4787 /* Remember the symbol size if it isn't undefined. */
4788 if (isym->st_size != 0
4789 && isym->st_shndx != SHN_UNDEF
4790 && (definition || h->size == 0))
4791 {
4792 if (h->size != 0
4793 && h->size != isym->st_size
4794 && ! size_change_ok)
4795 _bfd_error_handler
4796 /* xgettext:c-format */
4797 (_("Warning: size of symbol `%s' changed"
4798 " from %Lu in %B to %Lu in %B"),
4799 name, h->size, old_bfd, isym->st_size, abfd);
4800
4801 h->size = isym->st_size;
4802 }
4803
4804 /* If this is a common symbol, then we always want H->SIZE
4805 to be the size of the common symbol. The code just above
4806 won't fix the size if a common symbol becomes larger. We
4807 don't warn about a size change here, because that is
4808 covered by --warn-common. Allow changes between different
4809 function types. */
4810 if (h->root.type == bfd_link_hash_common)
4811 h->size = h->root.u.c.size;
4812
4813 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4814 && ((definition && !new_weak)
4815 || (old_weak && h->root.type == bfd_link_hash_common)
4816 || h->type == STT_NOTYPE))
4817 {
4818 unsigned int type = ELF_ST_TYPE (isym->st_info);
4819
4820 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4821 symbol. */
4822 if (type == STT_GNU_IFUNC
4823 && (abfd->flags & DYNAMIC) != 0)
4824 type = STT_FUNC;
4825
4826 if (h->type != type)
4827 {
4828 if (h->type != STT_NOTYPE && ! type_change_ok)
4829 /* xgettext:c-format */
4830 _bfd_error_handler
4831 (_("Warning: type of symbol `%s' changed"
4832 " from %d to %d in %B"),
4833 name, h->type, type, abfd);
4834
4835 h->type = type;
4836 }
4837 }
4838
4839 /* Merge st_other field. */
4840 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4841
4842 /* We don't want to make debug symbol dynamic. */
4843 if (definition
4844 && (sec->flags & SEC_DEBUGGING)
4845 && !bfd_link_relocatable (info))
4846 dynsym = FALSE;
4847
4848 /* Nor should we make plugin symbols dynamic. */
4849 if ((abfd->flags & BFD_PLUGIN) != 0)
4850 dynsym = FALSE;
4851
4852 if (definition)
4853 {
4854 h->target_internal = isym->st_target_internal;
4855 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4856 }
4857
4858 if (definition && !dynamic)
4859 {
4860 char *p = strchr (name, ELF_VER_CHR);
4861 if (p != NULL && p[1] != ELF_VER_CHR)
4862 {
4863 /* Queue non-default versions so that .symver x, x@FOO
4864 aliases can be checked. */
4865 if (!nondeflt_vers)
4866 {
4867 amt = ((isymend - isym + 1)
4868 * sizeof (struct elf_link_hash_entry *));
4869 nondeflt_vers
4870 = (struct elf_link_hash_entry **) bfd_malloc (amt);
4871 if (!nondeflt_vers)
4872 goto error_free_vers;
4873 }
4874 nondeflt_vers[nondeflt_vers_cnt++] = h;
4875 }
4876 }
4877
4878 if (dynsym && h->dynindx == -1)
4879 {
4880 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4881 goto error_free_vers;
4882 if (h->is_weakalias
4883 && weakdef (h)->dynindx == -1)
4884 {
4885 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
4886 goto error_free_vers;
4887 }
4888 }
4889 else if (h->dynindx != -1)
4890 /* If the symbol already has a dynamic index, but
4891 visibility says it should not be visible, turn it into
4892 a local symbol. */
4893 switch (ELF_ST_VISIBILITY (h->other))
4894 {
4895 case STV_INTERNAL:
4896 case STV_HIDDEN:
4897 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4898 dynsym = FALSE;
4899 break;
4900 }
4901
4902 /* Don't add DT_NEEDED for references from the dummy bfd nor
4903 for unmatched symbol. */
4904 if (!add_needed
4905 && matched
4906 && definition
4907 && ((dynsym
4908 && h->ref_regular_nonweak
4909 && (old_bfd == NULL
4910 || (old_bfd->flags & BFD_PLUGIN) == 0))
4911 || (h->ref_dynamic_nonweak
4912 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4913 && !on_needed_list (elf_dt_name (abfd),
4914 htab->needed, NULL))))
4915 {
4916 int ret;
4917 const char *soname = elf_dt_name (abfd);
4918
4919 info->callbacks->minfo ("%!", soname, old_bfd,
4920 h->root.root.string);
4921
4922 /* A symbol from a library loaded via DT_NEEDED of some
4923 other library is referenced by a regular object.
4924 Add a DT_NEEDED entry for it. Issue an error if
4925 --no-add-needed is used and the reference was not
4926 a weak one. */
4927 if (old_bfd != NULL
4928 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4929 {
4930 _bfd_error_handler
4931 /* xgettext:c-format */
4932 (_("%B: undefined reference to symbol '%s'"),
4933 old_bfd, name);
4934 bfd_set_error (bfd_error_missing_dso);
4935 goto error_free_vers;
4936 }
4937
4938 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4939 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
4940
4941 add_needed = TRUE;
4942 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4943 if (ret < 0)
4944 goto error_free_vers;
4945
4946 BFD_ASSERT (ret == 0);
4947 }
4948 }
4949 }
4950
4951 if (info->lto_plugin_active
4952 && !bfd_link_relocatable (info)
4953 && (abfd->flags & BFD_PLUGIN) == 0
4954 && !just_syms
4955 && extsymcount)
4956 {
4957 int r_sym_shift;
4958
4959 if (bed->s->arch_size == 32)
4960 r_sym_shift = 8;
4961 else
4962 r_sym_shift = 32;
4963
4964 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
4965 referenced in regular objects so that linker plugin will get
4966 the correct symbol resolution. */
4967
4968 sym_hash = elf_sym_hashes (abfd);
4969 for (s = abfd->sections; s != NULL; s = s->next)
4970 {
4971 Elf_Internal_Rela *internal_relocs;
4972 Elf_Internal_Rela *rel, *relend;
4973
4974 /* Don't check relocations in excluded sections. */
4975 if ((s->flags & SEC_RELOC) == 0
4976 || s->reloc_count == 0
4977 || (s->flags & SEC_EXCLUDE) != 0
4978 || ((info->strip == strip_all
4979 || info->strip == strip_debugger)
4980 && (s->flags & SEC_DEBUGGING) != 0))
4981 continue;
4982
4983 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
4984 NULL,
4985 info->keep_memory);
4986 if (internal_relocs == NULL)
4987 goto error_free_vers;
4988
4989 rel = internal_relocs;
4990 relend = rel + s->reloc_count;
4991 for ( ; rel < relend; rel++)
4992 {
4993 unsigned long r_symndx = rel->r_info >> r_sym_shift;
4994 struct elf_link_hash_entry *h;
4995
4996 /* Skip local symbols. */
4997 if (r_symndx < extsymoff)
4998 continue;
4999
5000 h = sym_hash[r_symndx - extsymoff];
5001 if (h != NULL)
5002 h->root.non_ir_ref_regular = 1;
5003 }
5004
5005 if (elf_section_data (s)->relocs != internal_relocs)
5006 free (internal_relocs);
5007 }
5008 }
5009
5010 if (extversym != NULL)
5011 {
5012 free (extversym);
5013 extversym = NULL;
5014 }
5015
5016 if (isymbuf != NULL)
5017 {
5018 free (isymbuf);
5019 isymbuf = NULL;
5020 }
5021
5022 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5023 {
5024 unsigned int i;
5025
5026 /* Restore the symbol table. */
5027 old_ent = (char *) old_tab + tabsize;
5028 memset (elf_sym_hashes (abfd), 0,
5029 extsymcount * sizeof (struct elf_link_hash_entry *));
5030 htab->root.table.table = old_table;
5031 htab->root.table.size = old_size;
5032 htab->root.table.count = old_count;
5033 memcpy (htab->root.table.table, old_tab, tabsize);
5034 htab->root.undefs = old_undefs;
5035 htab->root.undefs_tail = old_undefs_tail;
5036 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5037 free (old_strtab);
5038 old_strtab = NULL;
5039 for (i = 0; i < htab->root.table.size; i++)
5040 {
5041 struct bfd_hash_entry *p;
5042 struct elf_link_hash_entry *h;
5043 bfd_size_type size;
5044 unsigned int alignment_power;
5045 unsigned int non_ir_ref_dynamic;
5046
5047 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5048 {
5049 h = (struct elf_link_hash_entry *) p;
5050 if (h->root.type == bfd_link_hash_warning)
5051 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5052
5053 /* Preserve the maximum alignment and size for common
5054 symbols even if this dynamic lib isn't on DT_NEEDED
5055 since it can still be loaded at run time by another
5056 dynamic lib. */
5057 if (h->root.type == bfd_link_hash_common)
5058 {
5059 size = h->root.u.c.size;
5060 alignment_power = h->root.u.c.p->alignment_power;
5061 }
5062 else
5063 {
5064 size = 0;
5065 alignment_power = 0;
5066 }
5067 /* Preserve non_ir_ref_dynamic so that this symbol
5068 will be exported when the dynamic lib becomes needed
5069 in the second pass. */
5070 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5071 memcpy (p, old_ent, htab->root.table.entsize);
5072 old_ent = (char *) old_ent + htab->root.table.entsize;
5073 h = (struct elf_link_hash_entry *) p;
5074 if (h->root.type == bfd_link_hash_warning)
5075 {
5076 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5077 old_ent = (char *) old_ent + htab->root.table.entsize;
5078 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5079 }
5080 if (h->root.type == bfd_link_hash_common)
5081 {
5082 if (size > h->root.u.c.size)
5083 h->root.u.c.size = size;
5084 if (alignment_power > h->root.u.c.p->alignment_power)
5085 h->root.u.c.p->alignment_power = alignment_power;
5086 }
5087 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5088 }
5089 }
5090
5091 /* Make a special call to the linker "notice" function to
5092 tell it that symbols added for crefs may need to be removed. */
5093 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5094 goto error_free_vers;
5095
5096 free (old_tab);
5097 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5098 alloc_mark);
5099 if (nondeflt_vers != NULL)
5100 free (nondeflt_vers);
5101 return TRUE;
5102 }
5103
5104 if (old_tab != NULL)
5105 {
5106 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5107 goto error_free_vers;
5108 free (old_tab);
5109 old_tab = NULL;
5110 }
5111
5112 /* Now that all the symbols from this input file are created, if
5113 not performing a relocatable link, handle .symver foo, foo@BAR
5114 such that any relocs against foo become foo@BAR. */
5115 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5116 {
5117 size_t cnt, symidx;
5118
5119 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5120 {
5121 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5122 char *shortname, *p;
5123
5124 p = strchr (h->root.root.string, ELF_VER_CHR);
5125 if (p == NULL
5126 || (h->root.type != bfd_link_hash_defined
5127 && h->root.type != bfd_link_hash_defweak))
5128 continue;
5129
5130 amt = p - h->root.root.string;
5131 shortname = (char *) bfd_malloc (amt + 1);
5132 if (!shortname)
5133 goto error_free_vers;
5134 memcpy (shortname, h->root.root.string, amt);
5135 shortname[amt] = '\0';
5136
5137 hi = (struct elf_link_hash_entry *)
5138 bfd_link_hash_lookup (&htab->root, shortname,
5139 FALSE, FALSE, FALSE);
5140 if (hi != NULL
5141 && hi->root.type == h->root.type
5142 && hi->root.u.def.value == h->root.u.def.value
5143 && hi->root.u.def.section == h->root.u.def.section)
5144 {
5145 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5146 hi->root.type = bfd_link_hash_indirect;
5147 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5148 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5149 sym_hash = elf_sym_hashes (abfd);
5150 if (sym_hash)
5151 for (symidx = 0; symidx < extsymcount; ++symidx)
5152 if (sym_hash[symidx] == hi)
5153 {
5154 sym_hash[symidx] = h;
5155 break;
5156 }
5157 }
5158 free (shortname);
5159 }
5160 free (nondeflt_vers);
5161 nondeflt_vers = NULL;
5162 }
5163
5164 /* Now set the alias field correctly for all the weak defined
5165 symbols we found. The only way to do this is to search all the
5166 symbols. Since we only need the information for non functions in
5167 dynamic objects, that's the only time we actually put anything on
5168 the list WEAKS. We need this information so that if a regular
5169 object refers to a symbol defined weakly in a dynamic object, the
5170 real symbol in the dynamic object is also put in the dynamic
5171 symbols; we also must arrange for both symbols to point to the
5172 same memory location. We could handle the general case of symbol
5173 aliasing, but a general symbol alias can only be generated in
5174 assembler code, handling it correctly would be very time
5175 consuming, and other ELF linkers don't handle general aliasing
5176 either. */
5177 if (weaks != NULL)
5178 {
5179 struct elf_link_hash_entry **hpp;
5180 struct elf_link_hash_entry **hppend;
5181 struct elf_link_hash_entry **sorted_sym_hash;
5182 struct elf_link_hash_entry *h;
5183 size_t sym_count;
5184
5185 /* Since we have to search the whole symbol list for each weak
5186 defined symbol, search time for N weak defined symbols will be
5187 O(N^2). Binary search will cut it down to O(NlogN). */
5188 amt = extsymcount;
5189 amt *= sizeof (struct elf_link_hash_entry *);
5190 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
5191 if (sorted_sym_hash == NULL)
5192 goto error_return;
5193 sym_hash = sorted_sym_hash;
5194 hpp = elf_sym_hashes (abfd);
5195 hppend = hpp + extsymcount;
5196 sym_count = 0;
5197 for (; hpp < hppend; hpp++)
5198 {
5199 h = *hpp;
5200 if (h != NULL
5201 && h->root.type == bfd_link_hash_defined
5202 && !bed->is_function_type (h->type))
5203 {
5204 *sym_hash = h;
5205 sym_hash++;
5206 sym_count++;
5207 }
5208 }
5209
5210 qsort (sorted_sym_hash, sym_count,
5211 sizeof (struct elf_link_hash_entry *),
5212 elf_sort_symbol);
5213
5214 while (weaks != NULL)
5215 {
5216 struct elf_link_hash_entry *hlook;
5217 asection *slook;
5218 bfd_vma vlook;
5219 size_t i, j, idx = 0;
5220
5221 hlook = weaks;
5222 weaks = hlook->u.alias;
5223 hlook->u.alias = NULL;
5224
5225 if (hlook->root.type != bfd_link_hash_defined
5226 && hlook->root.type != bfd_link_hash_defweak)
5227 continue;
5228
5229 slook = hlook->root.u.def.section;
5230 vlook = hlook->root.u.def.value;
5231
5232 i = 0;
5233 j = sym_count;
5234 while (i != j)
5235 {
5236 bfd_signed_vma vdiff;
5237 idx = (i + j) / 2;
5238 h = sorted_sym_hash[idx];
5239 vdiff = vlook - h->root.u.def.value;
5240 if (vdiff < 0)
5241 j = idx;
5242 else if (vdiff > 0)
5243 i = idx + 1;
5244 else
5245 {
5246 int sdiff = slook->id - h->root.u.def.section->id;
5247 if (sdiff < 0)
5248 j = idx;
5249 else if (sdiff > 0)
5250 i = idx + 1;
5251 else
5252 break;
5253 }
5254 }
5255
5256 /* We didn't find a value/section match. */
5257 if (i == j)
5258 continue;
5259
5260 /* With multiple aliases, or when the weak symbol is already
5261 strongly defined, we have multiple matching symbols and
5262 the binary search above may land on any of them. Step
5263 one past the matching symbol(s). */
5264 while (++idx != j)
5265 {
5266 h = sorted_sym_hash[idx];
5267 if (h->root.u.def.section != slook
5268 || h->root.u.def.value != vlook)
5269 break;
5270 }
5271
5272 /* Now look back over the aliases. Since we sorted by size
5273 as well as value and section, we'll choose the one with
5274 the largest size. */
5275 while (idx-- != i)
5276 {
5277 h = sorted_sym_hash[idx];
5278
5279 /* Stop if value or section doesn't match. */
5280 if (h->root.u.def.section != slook
5281 || h->root.u.def.value != vlook)
5282 break;
5283 else if (h != hlook)
5284 {
5285 struct elf_link_hash_entry *t;
5286
5287 hlook->u.alias = h;
5288 hlook->is_weakalias = 1;
5289 t = h;
5290 if (t->u.alias != NULL)
5291 while (t->u.alias != h)
5292 t = t->u.alias;
5293 t->u.alias = hlook;
5294
5295 /* If the weak definition is in the list of dynamic
5296 symbols, make sure the real definition is put
5297 there as well. */
5298 if (hlook->dynindx != -1 && h->dynindx == -1)
5299 {
5300 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5301 {
5302 err_free_sym_hash:
5303 free (sorted_sym_hash);
5304 goto error_return;
5305 }
5306 }
5307
5308 /* If the real definition is in the list of dynamic
5309 symbols, make sure the weak definition is put
5310 there as well. If we don't do this, then the
5311 dynamic loader might not merge the entries for the
5312 real definition and the weak definition. */
5313 if (h->dynindx != -1 && hlook->dynindx == -1)
5314 {
5315 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5316 goto err_free_sym_hash;
5317 }
5318 break;
5319 }
5320 }
5321 }
5322
5323 free (sorted_sym_hash);
5324 }
5325
5326 if (bed->check_directives
5327 && !(*bed->check_directives) (abfd, info))
5328 return FALSE;
5329
5330 /* If this is a non-traditional link, try to optimize the handling
5331 of the .stab/.stabstr sections. */
5332 if (! dynamic
5333 && ! info->traditional_format
5334 && is_elf_hash_table (htab)
5335 && (info->strip != strip_all && info->strip != strip_debugger))
5336 {
5337 asection *stabstr;
5338
5339 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5340 if (stabstr != NULL)
5341 {
5342 bfd_size_type string_offset = 0;
5343 asection *stab;
5344
5345 for (stab = abfd->sections; stab; stab = stab->next)
5346 if (CONST_STRNEQ (stab->name, ".stab")
5347 && (!stab->name[5] ||
5348 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5349 && (stab->flags & SEC_MERGE) == 0
5350 && !bfd_is_abs_section (stab->output_section))
5351 {
5352 struct bfd_elf_section_data *secdata;
5353
5354 secdata = elf_section_data (stab);
5355 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5356 stabstr, &secdata->sec_info,
5357 &string_offset))
5358 goto error_return;
5359 if (secdata->sec_info)
5360 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5361 }
5362 }
5363 }
5364
5365 if (is_elf_hash_table (htab) && add_needed)
5366 {
5367 /* Add this bfd to the loaded list. */
5368 struct elf_link_loaded_list *n;
5369
5370 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5371 if (n == NULL)
5372 goto error_return;
5373 n->abfd = abfd;
5374 n->next = htab->loaded;
5375 htab->loaded = n;
5376 }
5377
5378 return TRUE;
5379
5380 error_free_vers:
5381 if (old_tab != NULL)
5382 free (old_tab);
5383 if (old_strtab != NULL)
5384 free (old_strtab);
5385 if (nondeflt_vers != NULL)
5386 free (nondeflt_vers);
5387 if (extversym != NULL)
5388 free (extversym);
5389 error_free_sym:
5390 if (isymbuf != NULL)
5391 free (isymbuf);
5392 error_return:
5393 return FALSE;
5394 }
5395
5396 /* Return the linker hash table entry of a symbol that might be
5397 satisfied by an archive symbol. Return -1 on error. */
5398
5399 struct elf_link_hash_entry *
5400 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5401 struct bfd_link_info *info,
5402 const char *name)
5403 {
5404 struct elf_link_hash_entry *h;
5405 char *p, *copy;
5406 size_t len, first;
5407
5408 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5409 if (h != NULL)
5410 return h;
5411
5412 /* If this is a default version (the name contains @@), look up the
5413 symbol again with only one `@' as well as without the version.
5414 The effect is that references to the symbol with and without the
5415 version will be matched by the default symbol in the archive. */
5416
5417 p = strchr (name, ELF_VER_CHR);
5418 if (p == NULL || p[1] != ELF_VER_CHR)
5419 return h;
5420
5421 /* First check with only one `@'. */
5422 len = strlen (name);
5423 copy = (char *) bfd_alloc (abfd, len);
5424 if (copy == NULL)
5425 return (struct elf_link_hash_entry *) 0 - 1;
5426
5427 first = p - name + 1;
5428 memcpy (copy, name, first);
5429 memcpy (copy + first, name + first + 1, len - first);
5430
5431 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5432 if (h == NULL)
5433 {
5434 /* We also need to check references to the symbol without the
5435 version. */
5436 copy[first - 1] = '\0';
5437 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5438 FALSE, FALSE, TRUE);
5439 }
5440
5441 bfd_release (abfd, copy);
5442 return h;
5443 }
5444
5445 /* Add symbols from an ELF archive file to the linker hash table. We
5446 don't use _bfd_generic_link_add_archive_symbols because we need to
5447 handle versioned symbols.
5448
5449 Fortunately, ELF archive handling is simpler than that done by
5450 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5451 oddities. In ELF, if we find a symbol in the archive map, and the
5452 symbol is currently undefined, we know that we must pull in that
5453 object file.
5454
5455 Unfortunately, we do have to make multiple passes over the symbol
5456 table until nothing further is resolved. */
5457
5458 static bfd_boolean
5459 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5460 {
5461 symindex c;
5462 unsigned char *included = NULL;
5463 carsym *symdefs;
5464 bfd_boolean loop;
5465 bfd_size_type amt;
5466 const struct elf_backend_data *bed;
5467 struct elf_link_hash_entry * (*archive_symbol_lookup)
5468 (bfd *, struct bfd_link_info *, const char *);
5469
5470 if (! bfd_has_map (abfd))
5471 {
5472 /* An empty archive is a special case. */
5473 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5474 return TRUE;
5475 bfd_set_error (bfd_error_no_armap);
5476 return FALSE;
5477 }
5478
5479 /* Keep track of all symbols we know to be already defined, and all
5480 files we know to be already included. This is to speed up the
5481 second and subsequent passes. */
5482 c = bfd_ardata (abfd)->symdef_count;
5483 if (c == 0)
5484 return TRUE;
5485 amt = c;
5486 amt *= sizeof (*included);
5487 included = (unsigned char *) bfd_zmalloc (amt);
5488 if (included == NULL)
5489 return FALSE;
5490
5491 symdefs = bfd_ardata (abfd)->symdefs;
5492 bed = get_elf_backend_data (abfd);
5493 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5494
5495 do
5496 {
5497 file_ptr last;
5498 symindex i;
5499 carsym *symdef;
5500 carsym *symdefend;
5501
5502 loop = FALSE;
5503 last = -1;
5504
5505 symdef = symdefs;
5506 symdefend = symdef + c;
5507 for (i = 0; symdef < symdefend; symdef++, i++)
5508 {
5509 struct elf_link_hash_entry *h;
5510 bfd *element;
5511 struct bfd_link_hash_entry *undefs_tail;
5512 symindex mark;
5513
5514 if (included[i])
5515 continue;
5516 if (symdef->file_offset == last)
5517 {
5518 included[i] = TRUE;
5519 continue;
5520 }
5521
5522 h = archive_symbol_lookup (abfd, info, symdef->name);
5523 if (h == (struct elf_link_hash_entry *) 0 - 1)
5524 goto error_return;
5525
5526 if (h == NULL)
5527 continue;
5528
5529 if (h->root.type == bfd_link_hash_common)
5530 {
5531 /* We currently have a common symbol. The archive map contains
5532 a reference to this symbol, so we may want to include it. We
5533 only want to include it however, if this archive element
5534 contains a definition of the symbol, not just another common
5535 declaration of it.
5536
5537 Unfortunately some archivers (including GNU ar) will put
5538 declarations of common symbols into their archive maps, as
5539 well as real definitions, so we cannot just go by the archive
5540 map alone. Instead we must read in the element's symbol
5541 table and check that to see what kind of symbol definition
5542 this is. */
5543 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5544 continue;
5545 }
5546 else if (h->root.type != bfd_link_hash_undefined)
5547 {
5548 if (h->root.type != bfd_link_hash_undefweak)
5549 /* Symbol must be defined. Don't check it again. */
5550 included[i] = TRUE;
5551 continue;
5552 }
5553
5554 /* We need to include this archive member. */
5555 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5556 if (element == NULL)
5557 goto error_return;
5558
5559 if (! bfd_check_format (element, bfd_object))
5560 goto error_return;
5561
5562 undefs_tail = info->hash->undefs_tail;
5563
5564 if (!(*info->callbacks
5565 ->add_archive_element) (info, element, symdef->name, &element))
5566 continue;
5567 if (!bfd_link_add_symbols (element, info))
5568 goto error_return;
5569
5570 /* If there are any new undefined symbols, we need to make
5571 another pass through the archive in order to see whether
5572 they can be defined. FIXME: This isn't perfect, because
5573 common symbols wind up on undefs_tail and because an
5574 undefined symbol which is defined later on in this pass
5575 does not require another pass. This isn't a bug, but it
5576 does make the code less efficient than it could be. */
5577 if (undefs_tail != info->hash->undefs_tail)
5578 loop = TRUE;
5579
5580 /* Look backward to mark all symbols from this object file
5581 which we have already seen in this pass. */
5582 mark = i;
5583 do
5584 {
5585 included[mark] = TRUE;
5586 if (mark == 0)
5587 break;
5588 --mark;
5589 }
5590 while (symdefs[mark].file_offset == symdef->file_offset);
5591
5592 /* We mark subsequent symbols from this object file as we go
5593 on through the loop. */
5594 last = symdef->file_offset;
5595 }
5596 }
5597 while (loop);
5598
5599 free (included);
5600
5601 return TRUE;
5602
5603 error_return:
5604 if (included != NULL)
5605 free (included);
5606 return FALSE;
5607 }
5608
5609 /* Given an ELF BFD, add symbols to the global hash table as
5610 appropriate. */
5611
5612 bfd_boolean
5613 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5614 {
5615 switch (bfd_get_format (abfd))
5616 {
5617 case bfd_object:
5618 return elf_link_add_object_symbols (abfd, info);
5619 case bfd_archive:
5620 return elf_link_add_archive_symbols (abfd, info);
5621 default:
5622 bfd_set_error (bfd_error_wrong_format);
5623 return FALSE;
5624 }
5625 }
5626 \f
5627 struct hash_codes_info
5628 {
5629 unsigned long *hashcodes;
5630 bfd_boolean error;
5631 };
5632
5633 /* This function will be called though elf_link_hash_traverse to store
5634 all hash value of the exported symbols in an array. */
5635
5636 static bfd_boolean
5637 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5638 {
5639 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5640 const char *name;
5641 unsigned long ha;
5642 char *alc = NULL;
5643
5644 /* Ignore indirect symbols. These are added by the versioning code. */
5645 if (h->dynindx == -1)
5646 return TRUE;
5647
5648 name = h->root.root.string;
5649 if (h->versioned >= versioned)
5650 {
5651 char *p = strchr (name, ELF_VER_CHR);
5652 if (p != NULL)
5653 {
5654 alc = (char *) bfd_malloc (p - name + 1);
5655 if (alc == NULL)
5656 {
5657 inf->error = TRUE;
5658 return FALSE;
5659 }
5660 memcpy (alc, name, p - name);
5661 alc[p - name] = '\0';
5662 name = alc;
5663 }
5664 }
5665
5666 /* Compute the hash value. */
5667 ha = bfd_elf_hash (name);
5668
5669 /* Store the found hash value in the array given as the argument. */
5670 *(inf->hashcodes)++ = ha;
5671
5672 /* And store it in the struct so that we can put it in the hash table
5673 later. */
5674 h->u.elf_hash_value = ha;
5675
5676 if (alc != NULL)
5677 free (alc);
5678
5679 return TRUE;
5680 }
5681
5682 struct collect_gnu_hash_codes
5683 {
5684 bfd *output_bfd;
5685 const struct elf_backend_data *bed;
5686 unsigned long int nsyms;
5687 unsigned long int maskbits;
5688 unsigned long int *hashcodes;
5689 unsigned long int *hashval;
5690 unsigned long int *indx;
5691 unsigned long int *counts;
5692 bfd_vma *bitmask;
5693 bfd_byte *contents;
5694 long int min_dynindx;
5695 unsigned long int bucketcount;
5696 unsigned long int symindx;
5697 long int local_indx;
5698 long int shift1, shift2;
5699 unsigned long int mask;
5700 bfd_boolean error;
5701 };
5702
5703 /* This function will be called though elf_link_hash_traverse to store
5704 all hash value of the exported symbols in an array. */
5705
5706 static bfd_boolean
5707 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5708 {
5709 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5710 const char *name;
5711 unsigned long ha;
5712 char *alc = NULL;
5713
5714 /* Ignore indirect symbols. These are added by the versioning code. */
5715 if (h->dynindx == -1)
5716 return TRUE;
5717
5718 /* Ignore also local symbols and undefined symbols. */
5719 if (! (*s->bed->elf_hash_symbol) (h))
5720 return TRUE;
5721
5722 name = h->root.root.string;
5723 if (h->versioned >= versioned)
5724 {
5725 char *p = strchr (name, ELF_VER_CHR);
5726 if (p != NULL)
5727 {
5728 alc = (char *) bfd_malloc (p - name + 1);
5729 if (alc == NULL)
5730 {
5731 s->error = TRUE;
5732 return FALSE;
5733 }
5734 memcpy (alc, name, p - name);
5735 alc[p - name] = '\0';
5736 name = alc;
5737 }
5738 }
5739
5740 /* Compute the hash value. */
5741 ha = bfd_elf_gnu_hash (name);
5742
5743 /* Store the found hash value in the array for compute_bucket_count,
5744 and also for .dynsym reordering purposes. */
5745 s->hashcodes[s->nsyms] = ha;
5746 s->hashval[h->dynindx] = ha;
5747 ++s->nsyms;
5748 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5749 s->min_dynindx = h->dynindx;
5750
5751 if (alc != NULL)
5752 free (alc);
5753
5754 return TRUE;
5755 }
5756
5757 /* This function will be called though elf_link_hash_traverse to do
5758 final dynaminc symbol renumbering. */
5759
5760 static bfd_boolean
5761 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5762 {
5763 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5764 unsigned long int bucket;
5765 unsigned long int val;
5766
5767 /* Ignore indirect symbols. */
5768 if (h->dynindx == -1)
5769 return TRUE;
5770
5771 /* Ignore also local symbols and undefined symbols. */
5772 if (! (*s->bed->elf_hash_symbol) (h))
5773 {
5774 if (h->dynindx >= s->min_dynindx)
5775 h->dynindx = s->local_indx++;
5776 return TRUE;
5777 }
5778
5779 bucket = s->hashval[h->dynindx] % s->bucketcount;
5780 val = (s->hashval[h->dynindx] >> s->shift1)
5781 & ((s->maskbits >> s->shift1) - 1);
5782 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5783 s->bitmask[val]
5784 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5785 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5786 if (s->counts[bucket] == 1)
5787 /* Last element terminates the chain. */
5788 val |= 1;
5789 bfd_put_32 (s->output_bfd, val,
5790 s->contents + (s->indx[bucket] - s->symindx) * 4);
5791 --s->counts[bucket];
5792 h->dynindx = s->indx[bucket]++;
5793 return TRUE;
5794 }
5795
5796 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5797
5798 bfd_boolean
5799 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5800 {
5801 return !(h->forced_local
5802 || h->root.type == bfd_link_hash_undefined
5803 || h->root.type == bfd_link_hash_undefweak
5804 || ((h->root.type == bfd_link_hash_defined
5805 || h->root.type == bfd_link_hash_defweak)
5806 && h->root.u.def.section->output_section == NULL));
5807 }
5808
5809 /* Array used to determine the number of hash table buckets to use
5810 based on the number of symbols there are. If there are fewer than
5811 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5812 fewer than 37 we use 17 buckets, and so forth. We never use more
5813 than 32771 buckets. */
5814
5815 static const size_t elf_buckets[] =
5816 {
5817 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5818 16411, 32771, 0
5819 };
5820
5821 /* Compute bucket count for hashing table. We do not use a static set
5822 of possible tables sizes anymore. Instead we determine for all
5823 possible reasonable sizes of the table the outcome (i.e., the
5824 number of collisions etc) and choose the best solution. The
5825 weighting functions are not too simple to allow the table to grow
5826 without bounds. Instead one of the weighting factors is the size.
5827 Therefore the result is always a good payoff between few collisions
5828 (= short chain lengths) and table size. */
5829 static size_t
5830 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5831 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5832 unsigned long int nsyms,
5833 int gnu_hash)
5834 {
5835 size_t best_size = 0;
5836 unsigned long int i;
5837
5838 /* We have a problem here. The following code to optimize the table
5839 size requires an integer type with more the 32 bits. If
5840 BFD_HOST_U_64_BIT is set we know about such a type. */
5841 #ifdef BFD_HOST_U_64_BIT
5842 if (info->optimize)
5843 {
5844 size_t minsize;
5845 size_t maxsize;
5846 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5847 bfd *dynobj = elf_hash_table (info)->dynobj;
5848 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5849 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5850 unsigned long int *counts;
5851 bfd_size_type amt;
5852 unsigned int no_improvement_count = 0;
5853
5854 /* Possible optimization parameters: if we have NSYMS symbols we say
5855 that the hashing table must at least have NSYMS/4 and at most
5856 2*NSYMS buckets. */
5857 minsize = nsyms / 4;
5858 if (minsize == 0)
5859 minsize = 1;
5860 best_size = maxsize = nsyms * 2;
5861 if (gnu_hash)
5862 {
5863 if (minsize < 2)
5864 minsize = 2;
5865 if ((best_size & 31) == 0)
5866 ++best_size;
5867 }
5868
5869 /* Create array where we count the collisions in. We must use bfd_malloc
5870 since the size could be large. */
5871 amt = maxsize;
5872 amt *= sizeof (unsigned long int);
5873 counts = (unsigned long int *) bfd_malloc (amt);
5874 if (counts == NULL)
5875 return 0;
5876
5877 /* Compute the "optimal" size for the hash table. The criteria is a
5878 minimal chain length. The minor criteria is (of course) the size
5879 of the table. */
5880 for (i = minsize; i < maxsize; ++i)
5881 {
5882 /* Walk through the array of hashcodes and count the collisions. */
5883 BFD_HOST_U_64_BIT max;
5884 unsigned long int j;
5885 unsigned long int fact;
5886
5887 if (gnu_hash && (i & 31) == 0)
5888 continue;
5889
5890 memset (counts, '\0', i * sizeof (unsigned long int));
5891
5892 /* Determine how often each hash bucket is used. */
5893 for (j = 0; j < nsyms; ++j)
5894 ++counts[hashcodes[j] % i];
5895
5896 /* For the weight function we need some information about the
5897 pagesize on the target. This is information need not be 100%
5898 accurate. Since this information is not available (so far) we
5899 define it here to a reasonable default value. If it is crucial
5900 to have a better value some day simply define this value. */
5901 # ifndef BFD_TARGET_PAGESIZE
5902 # define BFD_TARGET_PAGESIZE (4096)
5903 # endif
5904
5905 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5906 and the chains. */
5907 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5908
5909 # if 1
5910 /* Variant 1: optimize for short chains. We add the squares
5911 of all the chain lengths (which favors many small chain
5912 over a few long chains). */
5913 for (j = 0; j < i; ++j)
5914 max += counts[j] * counts[j];
5915
5916 /* This adds penalties for the overall size of the table. */
5917 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5918 max *= fact * fact;
5919 # else
5920 /* Variant 2: Optimize a lot more for small table. Here we
5921 also add squares of the size but we also add penalties for
5922 empty slots (the +1 term). */
5923 for (j = 0; j < i; ++j)
5924 max += (1 + counts[j]) * (1 + counts[j]);
5925
5926 /* The overall size of the table is considered, but not as
5927 strong as in variant 1, where it is squared. */
5928 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5929 max *= fact;
5930 # endif
5931
5932 /* Compare with current best results. */
5933 if (max < best_chlen)
5934 {
5935 best_chlen = max;
5936 best_size = i;
5937 no_improvement_count = 0;
5938 }
5939 /* PR 11843: Avoid futile long searches for the best bucket size
5940 when there are a large number of symbols. */
5941 else if (++no_improvement_count == 100)
5942 break;
5943 }
5944
5945 free (counts);
5946 }
5947 else
5948 #endif /* defined (BFD_HOST_U_64_BIT) */
5949 {
5950 /* This is the fallback solution if no 64bit type is available or if we
5951 are not supposed to spend much time on optimizations. We select the
5952 bucket count using a fixed set of numbers. */
5953 for (i = 0; elf_buckets[i] != 0; i++)
5954 {
5955 best_size = elf_buckets[i];
5956 if (nsyms < elf_buckets[i + 1])
5957 break;
5958 }
5959 if (gnu_hash && best_size < 2)
5960 best_size = 2;
5961 }
5962
5963 return best_size;
5964 }
5965
5966 /* Size any SHT_GROUP section for ld -r. */
5967
5968 bfd_boolean
5969 _bfd_elf_size_group_sections (struct bfd_link_info *info)
5970 {
5971 bfd *ibfd;
5972 asection *s;
5973
5974 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5975 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5976 && (s = ibfd->sections) != NULL
5977 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
5978 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5979 return FALSE;
5980 return TRUE;
5981 }
5982
5983 /* Set a default stack segment size. The value in INFO wins. If it
5984 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5985 undefined it is initialized. */
5986
5987 bfd_boolean
5988 bfd_elf_stack_segment_size (bfd *output_bfd,
5989 struct bfd_link_info *info,
5990 const char *legacy_symbol,
5991 bfd_vma default_size)
5992 {
5993 struct elf_link_hash_entry *h = NULL;
5994
5995 /* Look for legacy symbol. */
5996 if (legacy_symbol)
5997 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5998 FALSE, FALSE, FALSE);
5999 if (h && (h->root.type == bfd_link_hash_defined
6000 || h->root.type == bfd_link_hash_defweak)
6001 && h->def_regular
6002 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6003 {
6004 /* The symbol has no type if specified on the command line. */
6005 h->type = STT_OBJECT;
6006 if (info->stacksize)
6007 /* xgettext:c-format */
6008 _bfd_error_handler (_("%B: stack size specified and %s set"),
6009 output_bfd, legacy_symbol);
6010 else if (h->root.u.def.section != bfd_abs_section_ptr)
6011 /* xgettext:c-format */
6012 _bfd_error_handler (_("%B: %s not absolute"),
6013 output_bfd, legacy_symbol);
6014 else
6015 info->stacksize = h->root.u.def.value;
6016 }
6017
6018 if (!info->stacksize)
6019 /* If the user didn't set a size, or explicitly inhibit the
6020 size, set it now. */
6021 info->stacksize = default_size;
6022
6023 /* Provide the legacy symbol, if it is referenced. */
6024 if (h && (h->root.type == bfd_link_hash_undefined
6025 || h->root.type == bfd_link_hash_undefweak))
6026 {
6027 struct bfd_link_hash_entry *bh = NULL;
6028
6029 if (!(_bfd_generic_link_add_one_symbol
6030 (info, output_bfd, legacy_symbol,
6031 BSF_GLOBAL, bfd_abs_section_ptr,
6032 info->stacksize >= 0 ? info->stacksize : 0,
6033 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6034 return FALSE;
6035
6036 h = (struct elf_link_hash_entry *) bh;
6037 h->def_regular = 1;
6038 h->type = STT_OBJECT;
6039 }
6040
6041 return TRUE;
6042 }
6043
6044 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6045
6046 struct elf_gc_sweep_symbol_info
6047 {
6048 struct bfd_link_info *info;
6049 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6050 bfd_boolean);
6051 };
6052
6053 static bfd_boolean
6054 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6055 {
6056 if (!h->mark
6057 && (((h->root.type == bfd_link_hash_defined
6058 || h->root.type == bfd_link_hash_defweak)
6059 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6060 && h->root.u.def.section->gc_mark))
6061 || h->root.type == bfd_link_hash_undefined
6062 || h->root.type == bfd_link_hash_undefweak))
6063 {
6064 struct elf_gc_sweep_symbol_info *inf;
6065
6066 inf = (struct elf_gc_sweep_symbol_info *) data;
6067 (*inf->hide_symbol) (inf->info, h, TRUE);
6068 h->def_regular = 0;
6069 h->ref_regular = 0;
6070 h->ref_regular_nonweak = 0;
6071 }
6072
6073 return TRUE;
6074 }
6075
6076 /* Set up the sizes and contents of the ELF dynamic sections. This is
6077 called by the ELF linker emulation before_allocation routine. We
6078 must set the sizes of the sections before the linker sets the
6079 addresses of the various sections. */
6080
6081 bfd_boolean
6082 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6083 const char *soname,
6084 const char *rpath,
6085 const char *filter_shlib,
6086 const char *audit,
6087 const char *depaudit,
6088 const char * const *auxiliary_filters,
6089 struct bfd_link_info *info,
6090 asection **sinterpptr)
6091 {
6092 bfd *dynobj;
6093 const struct elf_backend_data *bed;
6094
6095 *sinterpptr = NULL;
6096
6097 if (!is_elf_hash_table (info->hash))
6098 return TRUE;
6099
6100 dynobj = elf_hash_table (info)->dynobj;
6101
6102 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6103 {
6104 struct bfd_elf_version_tree *verdefs;
6105 struct elf_info_failed asvinfo;
6106 struct bfd_elf_version_tree *t;
6107 struct bfd_elf_version_expr *d;
6108 asection *s;
6109 size_t soname_indx;
6110
6111 /* If we are supposed to export all symbols into the dynamic symbol
6112 table (this is not the normal case), then do so. */
6113 if (info->export_dynamic
6114 || (bfd_link_executable (info) && info->dynamic))
6115 {
6116 struct elf_info_failed eif;
6117
6118 eif.info = info;
6119 eif.failed = FALSE;
6120 elf_link_hash_traverse (elf_hash_table (info),
6121 _bfd_elf_export_symbol,
6122 &eif);
6123 if (eif.failed)
6124 return FALSE;
6125 }
6126
6127 if (soname != NULL)
6128 {
6129 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6130 soname, TRUE);
6131 if (soname_indx == (size_t) -1
6132 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6133 return FALSE;
6134 }
6135 else
6136 soname_indx = (size_t) -1;
6137
6138 /* Make all global versions with definition. */
6139 for (t = info->version_info; t != NULL; t = t->next)
6140 for (d = t->globals.list; d != NULL; d = d->next)
6141 if (!d->symver && d->literal)
6142 {
6143 const char *verstr, *name;
6144 size_t namelen, verlen, newlen;
6145 char *newname, *p, leading_char;
6146 struct elf_link_hash_entry *newh;
6147
6148 leading_char = bfd_get_symbol_leading_char (output_bfd);
6149 name = d->pattern;
6150 namelen = strlen (name) + (leading_char != '\0');
6151 verstr = t->name;
6152 verlen = strlen (verstr);
6153 newlen = namelen + verlen + 3;
6154
6155 newname = (char *) bfd_malloc (newlen);
6156 if (newname == NULL)
6157 return FALSE;
6158 newname[0] = leading_char;
6159 memcpy (newname + (leading_char != '\0'), name, namelen);
6160
6161 /* Check the hidden versioned definition. */
6162 p = newname + namelen;
6163 *p++ = ELF_VER_CHR;
6164 memcpy (p, verstr, verlen + 1);
6165 newh = elf_link_hash_lookup (elf_hash_table (info),
6166 newname, FALSE, FALSE,
6167 FALSE);
6168 if (newh == NULL
6169 || (newh->root.type != bfd_link_hash_defined
6170 && newh->root.type != bfd_link_hash_defweak))
6171 {
6172 /* Check the default versioned definition. */
6173 *p++ = ELF_VER_CHR;
6174 memcpy (p, verstr, verlen + 1);
6175 newh = elf_link_hash_lookup (elf_hash_table (info),
6176 newname, FALSE, FALSE,
6177 FALSE);
6178 }
6179 free (newname);
6180
6181 /* Mark this version if there is a definition and it is
6182 not defined in a shared object. */
6183 if (newh != NULL
6184 && !newh->def_dynamic
6185 && (newh->root.type == bfd_link_hash_defined
6186 || newh->root.type == bfd_link_hash_defweak))
6187 d->symver = 1;
6188 }
6189
6190 /* Attach all the symbols to their version information. */
6191 asvinfo.info = info;
6192 asvinfo.failed = FALSE;
6193
6194 elf_link_hash_traverse (elf_hash_table (info),
6195 _bfd_elf_link_assign_sym_version,
6196 &asvinfo);
6197 if (asvinfo.failed)
6198 return FALSE;
6199
6200 if (!info->allow_undefined_version)
6201 {
6202 /* Check if all global versions have a definition. */
6203 bfd_boolean all_defined = TRUE;
6204 for (t = info->version_info; t != NULL; t = t->next)
6205 for (d = t->globals.list; d != NULL; d = d->next)
6206 if (d->literal && !d->symver && !d->script)
6207 {
6208 _bfd_error_handler
6209 (_("%s: undefined version: %s"),
6210 d->pattern, t->name);
6211 all_defined = FALSE;
6212 }
6213
6214 if (!all_defined)
6215 {
6216 bfd_set_error (bfd_error_bad_value);
6217 return FALSE;
6218 }
6219 }
6220
6221 /* Set up the version definition section. */
6222 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6223 BFD_ASSERT (s != NULL);
6224
6225 /* We may have created additional version definitions if we are
6226 just linking a regular application. */
6227 verdefs = info->version_info;
6228
6229 /* Skip anonymous version tag. */
6230 if (verdefs != NULL && verdefs->vernum == 0)
6231 verdefs = verdefs->next;
6232
6233 if (verdefs == NULL && !info->create_default_symver)
6234 s->flags |= SEC_EXCLUDE;
6235 else
6236 {
6237 unsigned int cdefs;
6238 bfd_size_type size;
6239 bfd_byte *p;
6240 Elf_Internal_Verdef def;
6241 Elf_Internal_Verdaux defaux;
6242 struct bfd_link_hash_entry *bh;
6243 struct elf_link_hash_entry *h;
6244 const char *name;
6245
6246 cdefs = 0;
6247 size = 0;
6248
6249 /* Make space for the base version. */
6250 size += sizeof (Elf_External_Verdef);
6251 size += sizeof (Elf_External_Verdaux);
6252 ++cdefs;
6253
6254 /* Make space for the default version. */
6255 if (info->create_default_symver)
6256 {
6257 size += sizeof (Elf_External_Verdef);
6258 ++cdefs;
6259 }
6260
6261 for (t = verdefs; t != NULL; t = t->next)
6262 {
6263 struct bfd_elf_version_deps *n;
6264
6265 /* Don't emit base version twice. */
6266 if (t->vernum == 0)
6267 continue;
6268
6269 size += sizeof (Elf_External_Verdef);
6270 size += sizeof (Elf_External_Verdaux);
6271 ++cdefs;
6272
6273 for (n = t->deps; n != NULL; n = n->next)
6274 size += sizeof (Elf_External_Verdaux);
6275 }
6276
6277 s->size = size;
6278 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6279 if (s->contents == NULL && s->size != 0)
6280 return FALSE;
6281
6282 /* Fill in the version definition section. */
6283
6284 p = s->contents;
6285
6286 def.vd_version = VER_DEF_CURRENT;
6287 def.vd_flags = VER_FLG_BASE;
6288 def.vd_ndx = 1;
6289 def.vd_cnt = 1;
6290 if (info->create_default_symver)
6291 {
6292 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6293 def.vd_next = sizeof (Elf_External_Verdef);
6294 }
6295 else
6296 {
6297 def.vd_aux = sizeof (Elf_External_Verdef);
6298 def.vd_next = (sizeof (Elf_External_Verdef)
6299 + sizeof (Elf_External_Verdaux));
6300 }
6301
6302 if (soname_indx != (size_t) -1)
6303 {
6304 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6305 soname_indx);
6306 def.vd_hash = bfd_elf_hash (soname);
6307 defaux.vda_name = soname_indx;
6308 name = soname;
6309 }
6310 else
6311 {
6312 size_t indx;
6313
6314 name = lbasename (output_bfd->filename);
6315 def.vd_hash = bfd_elf_hash (name);
6316 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6317 name, FALSE);
6318 if (indx == (size_t) -1)
6319 return FALSE;
6320 defaux.vda_name = indx;
6321 }
6322 defaux.vda_next = 0;
6323
6324 _bfd_elf_swap_verdef_out (output_bfd, &def,
6325 (Elf_External_Verdef *) p);
6326 p += sizeof (Elf_External_Verdef);
6327 if (info->create_default_symver)
6328 {
6329 /* Add a symbol representing this version. */
6330 bh = NULL;
6331 if (! (_bfd_generic_link_add_one_symbol
6332 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6333 0, NULL, FALSE,
6334 get_elf_backend_data (dynobj)->collect, &bh)))
6335 return FALSE;
6336 h = (struct elf_link_hash_entry *) bh;
6337 h->non_elf = 0;
6338 h->def_regular = 1;
6339 h->type = STT_OBJECT;
6340 h->verinfo.vertree = NULL;
6341
6342 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6343 return FALSE;
6344
6345 /* Create a duplicate of the base version with the same
6346 aux block, but different flags. */
6347 def.vd_flags = 0;
6348 def.vd_ndx = 2;
6349 def.vd_aux = sizeof (Elf_External_Verdef);
6350 if (verdefs)
6351 def.vd_next = (sizeof (Elf_External_Verdef)
6352 + sizeof (Elf_External_Verdaux));
6353 else
6354 def.vd_next = 0;
6355 _bfd_elf_swap_verdef_out (output_bfd, &def,
6356 (Elf_External_Verdef *) p);
6357 p += sizeof (Elf_External_Verdef);
6358 }
6359 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6360 (Elf_External_Verdaux *) p);
6361 p += sizeof (Elf_External_Verdaux);
6362
6363 for (t = verdefs; t != NULL; t = t->next)
6364 {
6365 unsigned int cdeps;
6366 struct bfd_elf_version_deps *n;
6367
6368 /* Don't emit the base version twice. */
6369 if (t->vernum == 0)
6370 continue;
6371
6372 cdeps = 0;
6373 for (n = t->deps; n != NULL; n = n->next)
6374 ++cdeps;
6375
6376 /* Add a symbol representing this version. */
6377 bh = NULL;
6378 if (! (_bfd_generic_link_add_one_symbol
6379 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6380 0, NULL, FALSE,
6381 get_elf_backend_data (dynobj)->collect, &bh)))
6382 return FALSE;
6383 h = (struct elf_link_hash_entry *) bh;
6384 h->non_elf = 0;
6385 h->def_regular = 1;
6386 h->type = STT_OBJECT;
6387 h->verinfo.vertree = t;
6388
6389 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6390 return FALSE;
6391
6392 def.vd_version = VER_DEF_CURRENT;
6393 def.vd_flags = 0;
6394 if (t->globals.list == NULL
6395 && t->locals.list == NULL
6396 && ! t->used)
6397 def.vd_flags |= VER_FLG_WEAK;
6398 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6399 def.vd_cnt = cdeps + 1;
6400 def.vd_hash = bfd_elf_hash (t->name);
6401 def.vd_aux = sizeof (Elf_External_Verdef);
6402 def.vd_next = 0;
6403
6404 /* If a basever node is next, it *must* be the last node in
6405 the chain, otherwise Verdef construction breaks. */
6406 if (t->next != NULL && t->next->vernum == 0)
6407 BFD_ASSERT (t->next->next == NULL);
6408
6409 if (t->next != NULL && t->next->vernum != 0)
6410 def.vd_next = (sizeof (Elf_External_Verdef)
6411 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6412
6413 _bfd_elf_swap_verdef_out (output_bfd, &def,
6414 (Elf_External_Verdef *) p);
6415 p += sizeof (Elf_External_Verdef);
6416
6417 defaux.vda_name = h->dynstr_index;
6418 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6419 h->dynstr_index);
6420 defaux.vda_next = 0;
6421 if (t->deps != NULL)
6422 defaux.vda_next = sizeof (Elf_External_Verdaux);
6423 t->name_indx = defaux.vda_name;
6424
6425 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6426 (Elf_External_Verdaux *) p);
6427 p += sizeof (Elf_External_Verdaux);
6428
6429 for (n = t->deps; n != NULL; n = n->next)
6430 {
6431 if (n->version_needed == NULL)
6432 {
6433 /* This can happen if there was an error in the
6434 version script. */
6435 defaux.vda_name = 0;
6436 }
6437 else
6438 {
6439 defaux.vda_name = n->version_needed->name_indx;
6440 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6441 defaux.vda_name);
6442 }
6443 if (n->next == NULL)
6444 defaux.vda_next = 0;
6445 else
6446 defaux.vda_next = sizeof (Elf_External_Verdaux);
6447
6448 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6449 (Elf_External_Verdaux *) p);
6450 p += sizeof (Elf_External_Verdaux);
6451 }
6452 }
6453
6454 elf_tdata (output_bfd)->cverdefs = cdefs;
6455 }
6456 }
6457
6458 bed = get_elf_backend_data (output_bfd);
6459
6460 if (info->gc_sections && bed->can_gc_sections)
6461 {
6462 struct elf_gc_sweep_symbol_info sweep_info;
6463
6464 /* Remove the symbols that were in the swept sections from the
6465 dynamic symbol table. */
6466 sweep_info.info = info;
6467 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6468 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6469 &sweep_info);
6470 }
6471
6472 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6473 {
6474 asection *s;
6475 struct elf_find_verdep_info sinfo;
6476
6477 /* Work out the size of the version reference section. */
6478
6479 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6480 BFD_ASSERT (s != NULL);
6481
6482 sinfo.info = info;
6483 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6484 if (sinfo.vers == 0)
6485 sinfo.vers = 1;
6486 sinfo.failed = FALSE;
6487
6488 elf_link_hash_traverse (elf_hash_table (info),
6489 _bfd_elf_link_find_version_dependencies,
6490 &sinfo);
6491 if (sinfo.failed)
6492 return FALSE;
6493
6494 if (elf_tdata (output_bfd)->verref == NULL)
6495 s->flags |= SEC_EXCLUDE;
6496 else
6497 {
6498 Elf_Internal_Verneed *vn;
6499 unsigned int size;
6500 unsigned int crefs;
6501 bfd_byte *p;
6502
6503 /* Build the version dependency section. */
6504 size = 0;
6505 crefs = 0;
6506 for (vn = elf_tdata (output_bfd)->verref;
6507 vn != NULL;
6508 vn = vn->vn_nextref)
6509 {
6510 Elf_Internal_Vernaux *a;
6511
6512 size += sizeof (Elf_External_Verneed);
6513 ++crefs;
6514 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6515 size += sizeof (Elf_External_Vernaux);
6516 }
6517
6518 s->size = size;
6519 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6520 if (s->contents == NULL)
6521 return FALSE;
6522
6523 p = s->contents;
6524 for (vn = elf_tdata (output_bfd)->verref;
6525 vn != NULL;
6526 vn = vn->vn_nextref)
6527 {
6528 unsigned int caux;
6529 Elf_Internal_Vernaux *a;
6530 size_t indx;
6531
6532 caux = 0;
6533 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6534 ++caux;
6535
6536 vn->vn_version = VER_NEED_CURRENT;
6537 vn->vn_cnt = caux;
6538 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6539 elf_dt_name (vn->vn_bfd) != NULL
6540 ? elf_dt_name (vn->vn_bfd)
6541 : lbasename (vn->vn_bfd->filename),
6542 FALSE);
6543 if (indx == (size_t) -1)
6544 return FALSE;
6545 vn->vn_file = indx;
6546 vn->vn_aux = sizeof (Elf_External_Verneed);
6547 if (vn->vn_nextref == NULL)
6548 vn->vn_next = 0;
6549 else
6550 vn->vn_next = (sizeof (Elf_External_Verneed)
6551 + caux * sizeof (Elf_External_Vernaux));
6552
6553 _bfd_elf_swap_verneed_out (output_bfd, vn,
6554 (Elf_External_Verneed *) p);
6555 p += sizeof (Elf_External_Verneed);
6556
6557 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6558 {
6559 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6560 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6561 a->vna_nodename, FALSE);
6562 if (indx == (size_t) -1)
6563 return FALSE;
6564 a->vna_name = indx;
6565 if (a->vna_nextptr == NULL)
6566 a->vna_next = 0;
6567 else
6568 a->vna_next = sizeof (Elf_External_Vernaux);
6569
6570 _bfd_elf_swap_vernaux_out (output_bfd, a,
6571 (Elf_External_Vernaux *) p);
6572 p += sizeof (Elf_External_Vernaux);
6573 }
6574 }
6575
6576 elf_tdata (output_bfd)->cverrefs = crefs;
6577 }
6578 }
6579
6580 /* Any syms created from now on start with -1 in
6581 got.refcount/offset and plt.refcount/offset. */
6582 elf_hash_table (info)->init_got_refcount
6583 = elf_hash_table (info)->init_got_offset;
6584 elf_hash_table (info)->init_plt_refcount
6585 = elf_hash_table (info)->init_plt_offset;
6586
6587 if (bfd_link_relocatable (info)
6588 && !_bfd_elf_size_group_sections (info))
6589 return FALSE;
6590
6591 /* The backend may have to create some sections regardless of whether
6592 we're dynamic or not. */
6593 if (bed->elf_backend_always_size_sections
6594 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6595 return FALSE;
6596
6597 /* Determine any GNU_STACK segment requirements, after the backend
6598 has had a chance to set a default segment size. */
6599 if (info->execstack)
6600 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6601 else if (info->noexecstack)
6602 elf_stack_flags (output_bfd) = PF_R | PF_W;
6603 else
6604 {
6605 bfd *inputobj;
6606 asection *notesec = NULL;
6607 int exec = 0;
6608
6609 for (inputobj = info->input_bfds;
6610 inputobj;
6611 inputobj = inputobj->link.next)
6612 {
6613 asection *s;
6614
6615 if (inputobj->flags
6616 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6617 continue;
6618 s = inputobj->sections;
6619 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6620 continue;
6621
6622 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6623 if (s)
6624 {
6625 if (s->flags & SEC_CODE)
6626 exec = PF_X;
6627 notesec = s;
6628 }
6629 else if (bed->default_execstack)
6630 exec = PF_X;
6631 }
6632 if (notesec || info->stacksize > 0)
6633 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6634 if (notesec && exec && bfd_link_relocatable (info)
6635 && notesec->output_section != bfd_abs_section_ptr)
6636 notesec->output_section->flags |= SEC_CODE;
6637 }
6638
6639 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6640 {
6641 struct elf_info_failed eif;
6642 struct elf_link_hash_entry *h;
6643 asection *dynstr;
6644 asection *s;
6645
6646 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6647 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6648
6649 if (info->symbolic)
6650 {
6651 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6652 return FALSE;
6653 info->flags |= DF_SYMBOLIC;
6654 }
6655
6656 if (rpath != NULL)
6657 {
6658 size_t indx;
6659 bfd_vma tag;
6660
6661 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6662 TRUE);
6663 if (indx == (size_t) -1)
6664 return FALSE;
6665
6666 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6667 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6668 return FALSE;
6669 }
6670
6671 if (filter_shlib != NULL)
6672 {
6673 size_t indx;
6674
6675 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6676 filter_shlib, TRUE);
6677 if (indx == (size_t) -1
6678 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6679 return FALSE;
6680 }
6681
6682 if (auxiliary_filters != NULL)
6683 {
6684 const char * const *p;
6685
6686 for (p = auxiliary_filters; *p != NULL; p++)
6687 {
6688 size_t indx;
6689
6690 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6691 *p, TRUE);
6692 if (indx == (size_t) -1
6693 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6694 return FALSE;
6695 }
6696 }
6697
6698 if (audit != NULL)
6699 {
6700 size_t indx;
6701
6702 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6703 TRUE);
6704 if (indx == (size_t) -1
6705 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6706 return FALSE;
6707 }
6708
6709 if (depaudit != NULL)
6710 {
6711 size_t indx;
6712
6713 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6714 TRUE);
6715 if (indx == (size_t) -1
6716 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6717 return FALSE;
6718 }
6719
6720 eif.info = info;
6721 eif.failed = FALSE;
6722
6723 /* Find all symbols which were defined in a dynamic object and make
6724 the backend pick a reasonable value for them. */
6725 elf_link_hash_traverse (elf_hash_table (info),
6726 _bfd_elf_adjust_dynamic_symbol,
6727 &eif);
6728 if (eif.failed)
6729 return FALSE;
6730
6731 /* Add some entries to the .dynamic section. We fill in some of the
6732 values later, in bfd_elf_final_link, but we must add the entries
6733 now so that we know the final size of the .dynamic section. */
6734
6735 /* If there are initialization and/or finalization functions to
6736 call then add the corresponding DT_INIT/DT_FINI entries. */
6737 h = (info->init_function
6738 ? elf_link_hash_lookup (elf_hash_table (info),
6739 info->init_function, FALSE,
6740 FALSE, FALSE)
6741 : NULL);
6742 if (h != NULL
6743 && (h->ref_regular
6744 || h->def_regular))
6745 {
6746 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6747 return FALSE;
6748 }
6749 h = (info->fini_function
6750 ? elf_link_hash_lookup (elf_hash_table (info),
6751 info->fini_function, FALSE,
6752 FALSE, FALSE)
6753 : NULL);
6754 if (h != NULL
6755 && (h->ref_regular
6756 || h->def_regular))
6757 {
6758 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6759 return FALSE;
6760 }
6761
6762 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6763 if (s != NULL && s->linker_has_input)
6764 {
6765 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6766 if (! bfd_link_executable (info))
6767 {
6768 bfd *sub;
6769 asection *o;
6770
6771 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6772 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
6773 && (o = sub->sections) != NULL
6774 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
6775 for (o = sub->sections; o != NULL; o = o->next)
6776 if (elf_section_data (o)->this_hdr.sh_type
6777 == SHT_PREINIT_ARRAY)
6778 {
6779 _bfd_error_handler
6780 (_("%B: .preinit_array section is not allowed in DSO"),
6781 sub);
6782 break;
6783 }
6784
6785 bfd_set_error (bfd_error_nonrepresentable_section);
6786 return FALSE;
6787 }
6788
6789 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6790 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6791 return FALSE;
6792 }
6793 s = bfd_get_section_by_name (output_bfd, ".init_array");
6794 if (s != NULL && s->linker_has_input)
6795 {
6796 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6797 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6798 return FALSE;
6799 }
6800 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6801 if (s != NULL && s->linker_has_input)
6802 {
6803 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6804 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6805 return FALSE;
6806 }
6807
6808 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6809 /* If .dynstr is excluded from the link, we don't want any of
6810 these tags. Strictly, we should be checking each section
6811 individually; This quick check covers for the case where
6812 someone does a /DISCARD/ : { *(*) }. */
6813 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6814 {
6815 bfd_size_type strsize;
6816
6817 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6818 if ((info->emit_hash
6819 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6820 || (info->emit_gnu_hash
6821 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6822 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6823 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6824 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6825 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6826 bed->s->sizeof_sym))
6827 return FALSE;
6828 }
6829 }
6830
6831 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6832 return FALSE;
6833
6834 /* The backend must work out the sizes of all the other dynamic
6835 sections. */
6836 if (dynobj != NULL
6837 && bed->elf_backend_size_dynamic_sections != NULL
6838 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6839 return FALSE;
6840
6841 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6842 {
6843 if (elf_tdata (output_bfd)->cverdefs)
6844 {
6845 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
6846
6847 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6848 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
6849 return FALSE;
6850 }
6851
6852 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6853 {
6854 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6855 return FALSE;
6856 }
6857 else if (info->flags & DF_BIND_NOW)
6858 {
6859 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6860 return FALSE;
6861 }
6862
6863 if (info->flags_1)
6864 {
6865 if (bfd_link_executable (info))
6866 info->flags_1 &= ~ (DF_1_INITFIRST
6867 | DF_1_NODELETE
6868 | DF_1_NOOPEN);
6869 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6870 return FALSE;
6871 }
6872
6873 if (elf_tdata (output_bfd)->cverrefs)
6874 {
6875 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
6876
6877 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6878 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6879 return FALSE;
6880 }
6881
6882 if ((elf_tdata (output_bfd)->cverrefs == 0
6883 && elf_tdata (output_bfd)->cverdefs == 0)
6884 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
6885 {
6886 asection *s;
6887
6888 s = bfd_get_linker_section (dynobj, ".gnu.version");
6889 s->flags |= SEC_EXCLUDE;
6890 }
6891 }
6892 return TRUE;
6893 }
6894
6895 /* Find the first non-excluded output section. We'll use its
6896 section symbol for some emitted relocs. */
6897 void
6898 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6899 {
6900 asection *s;
6901
6902 for (s = output_bfd->sections; s != NULL; s = s->next)
6903 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6904 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6905 {
6906 elf_hash_table (info)->text_index_section = s;
6907 break;
6908 }
6909 }
6910
6911 /* Find two non-excluded output sections, one for code, one for data.
6912 We'll use their section symbols for some emitted relocs. */
6913 void
6914 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6915 {
6916 asection *s;
6917
6918 /* Data first, since setting text_index_section changes
6919 _bfd_elf_link_omit_section_dynsym. */
6920 for (s = output_bfd->sections; s != NULL; s = s->next)
6921 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6922 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6923 {
6924 elf_hash_table (info)->data_index_section = s;
6925 break;
6926 }
6927
6928 for (s = output_bfd->sections; s != NULL; s = s->next)
6929 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6930 == (SEC_ALLOC | SEC_READONLY))
6931 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6932 {
6933 elf_hash_table (info)->text_index_section = s;
6934 break;
6935 }
6936
6937 if (elf_hash_table (info)->text_index_section == NULL)
6938 elf_hash_table (info)->text_index_section
6939 = elf_hash_table (info)->data_index_section;
6940 }
6941
6942 bfd_boolean
6943 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6944 {
6945 const struct elf_backend_data *bed;
6946 unsigned long section_sym_count;
6947 bfd_size_type dynsymcount = 0;
6948
6949 if (!is_elf_hash_table (info->hash))
6950 return TRUE;
6951
6952 bed = get_elf_backend_data (output_bfd);
6953 (*bed->elf_backend_init_index_section) (output_bfd, info);
6954
6955 /* Assign dynsym indices. In a shared library we generate a section
6956 symbol for each output section, which come first. Next come all
6957 of the back-end allocated local dynamic syms, followed by the rest
6958 of the global symbols.
6959
6960 This is usually not needed for static binaries, however backends
6961 can request to always do it, e.g. the MIPS backend uses dynamic
6962 symbol counts to lay out GOT, which will be produced in the
6963 presence of GOT relocations even in static binaries (holding fixed
6964 data in that case, to satisfy those relocations). */
6965
6966 if (elf_hash_table (info)->dynamic_sections_created
6967 || bed->always_renumber_dynsyms)
6968 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6969 &section_sym_count);
6970
6971 if (elf_hash_table (info)->dynamic_sections_created)
6972 {
6973 bfd *dynobj;
6974 asection *s;
6975 unsigned int dtagcount;
6976
6977 dynobj = elf_hash_table (info)->dynobj;
6978
6979 /* Work out the size of the symbol version section. */
6980 s = bfd_get_linker_section (dynobj, ".gnu.version");
6981 BFD_ASSERT (s != NULL);
6982 if ((s->flags & SEC_EXCLUDE) == 0)
6983 {
6984 s->size = dynsymcount * sizeof (Elf_External_Versym);
6985 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6986 if (s->contents == NULL)
6987 return FALSE;
6988
6989 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6990 return FALSE;
6991 }
6992
6993 /* Set the size of the .dynsym and .hash sections. We counted
6994 the number of dynamic symbols in elf_link_add_object_symbols.
6995 We will build the contents of .dynsym and .hash when we build
6996 the final symbol table, because until then we do not know the
6997 correct value to give the symbols. We built the .dynstr
6998 section as we went along in elf_link_add_object_symbols. */
6999 s = elf_hash_table (info)->dynsym;
7000 BFD_ASSERT (s != NULL);
7001 s->size = dynsymcount * bed->s->sizeof_sym;
7002
7003 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7004 if (s->contents == NULL)
7005 return FALSE;
7006
7007 /* The first entry in .dynsym is a dummy symbol. Clear all the
7008 section syms, in case we don't output them all. */
7009 ++section_sym_count;
7010 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7011
7012 elf_hash_table (info)->bucketcount = 0;
7013
7014 /* Compute the size of the hashing table. As a side effect this
7015 computes the hash values for all the names we export. */
7016 if (info->emit_hash)
7017 {
7018 unsigned long int *hashcodes;
7019 struct hash_codes_info hashinf;
7020 bfd_size_type amt;
7021 unsigned long int nsyms;
7022 size_t bucketcount;
7023 size_t hash_entry_size;
7024
7025 /* Compute the hash values for all exported symbols. At the same
7026 time store the values in an array so that we could use them for
7027 optimizations. */
7028 amt = dynsymcount * sizeof (unsigned long int);
7029 hashcodes = (unsigned long int *) bfd_malloc (amt);
7030 if (hashcodes == NULL)
7031 return FALSE;
7032 hashinf.hashcodes = hashcodes;
7033 hashinf.error = FALSE;
7034
7035 /* Put all hash values in HASHCODES. */
7036 elf_link_hash_traverse (elf_hash_table (info),
7037 elf_collect_hash_codes, &hashinf);
7038 if (hashinf.error)
7039 {
7040 free (hashcodes);
7041 return FALSE;
7042 }
7043
7044 nsyms = hashinf.hashcodes - hashcodes;
7045 bucketcount
7046 = compute_bucket_count (info, hashcodes, nsyms, 0);
7047 free (hashcodes);
7048
7049 if (bucketcount == 0 && nsyms > 0)
7050 return FALSE;
7051
7052 elf_hash_table (info)->bucketcount = bucketcount;
7053
7054 s = bfd_get_linker_section (dynobj, ".hash");
7055 BFD_ASSERT (s != NULL);
7056 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7057 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7058 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7059 if (s->contents == NULL)
7060 return FALSE;
7061
7062 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7063 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7064 s->contents + hash_entry_size);
7065 }
7066
7067 if (info->emit_gnu_hash)
7068 {
7069 size_t i, cnt;
7070 unsigned char *contents;
7071 struct collect_gnu_hash_codes cinfo;
7072 bfd_size_type amt;
7073 size_t bucketcount;
7074
7075 memset (&cinfo, 0, sizeof (cinfo));
7076
7077 /* Compute the hash values for all exported symbols. At the same
7078 time store the values in an array so that we could use them for
7079 optimizations. */
7080 amt = dynsymcount * 2 * sizeof (unsigned long int);
7081 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7082 if (cinfo.hashcodes == NULL)
7083 return FALSE;
7084
7085 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7086 cinfo.min_dynindx = -1;
7087 cinfo.output_bfd = output_bfd;
7088 cinfo.bed = bed;
7089
7090 /* Put all hash values in HASHCODES. */
7091 elf_link_hash_traverse (elf_hash_table (info),
7092 elf_collect_gnu_hash_codes, &cinfo);
7093 if (cinfo.error)
7094 {
7095 free (cinfo.hashcodes);
7096 return FALSE;
7097 }
7098
7099 bucketcount
7100 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7101
7102 if (bucketcount == 0)
7103 {
7104 free (cinfo.hashcodes);
7105 return FALSE;
7106 }
7107
7108 s = bfd_get_linker_section (dynobj, ".gnu.hash");
7109 BFD_ASSERT (s != NULL);
7110
7111 if (cinfo.nsyms == 0)
7112 {
7113 /* Empty .gnu.hash section is special. */
7114 BFD_ASSERT (cinfo.min_dynindx == -1);
7115 free (cinfo.hashcodes);
7116 s->size = 5 * 4 + bed->s->arch_size / 8;
7117 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7118 if (contents == NULL)
7119 return FALSE;
7120 s->contents = contents;
7121 /* 1 empty bucket. */
7122 bfd_put_32 (output_bfd, 1, contents);
7123 /* SYMIDX above the special symbol 0. */
7124 bfd_put_32 (output_bfd, 1, contents + 4);
7125 /* Just one word for bitmask. */
7126 bfd_put_32 (output_bfd, 1, contents + 8);
7127 /* Only hash fn bloom filter. */
7128 bfd_put_32 (output_bfd, 0, contents + 12);
7129 /* No hashes are valid - empty bitmask. */
7130 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7131 /* No hashes in the only bucket. */
7132 bfd_put_32 (output_bfd, 0,
7133 contents + 16 + bed->s->arch_size / 8);
7134 }
7135 else
7136 {
7137 unsigned long int maskwords, maskbitslog2, x;
7138 BFD_ASSERT (cinfo.min_dynindx != -1);
7139
7140 x = cinfo.nsyms;
7141 maskbitslog2 = 1;
7142 while ((x >>= 1) != 0)
7143 ++maskbitslog2;
7144 if (maskbitslog2 < 3)
7145 maskbitslog2 = 5;
7146 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7147 maskbitslog2 = maskbitslog2 + 3;
7148 else
7149 maskbitslog2 = maskbitslog2 + 2;
7150 if (bed->s->arch_size == 64)
7151 {
7152 if (maskbitslog2 == 5)
7153 maskbitslog2 = 6;
7154 cinfo.shift1 = 6;
7155 }
7156 else
7157 cinfo.shift1 = 5;
7158 cinfo.mask = (1 << cinfo.shift1) - 1;
7159 cinfo.shift2 = maskbitslog2;
7160 cinfo.maskbits = 1 << maskbitslog2;
7161 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7162 amt = bucketcount * sizeof (unsigned long int) * 2;
7163 amt += maskwords * sizeof (bfd_vma);
7164 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7165 if (cinfo.bitmask == NULL)
7166 {
7167 free (cinfo.hashcodes);
7168 return FALSE;
7169 }
7170
7171 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7172 cinfo.indx = cinfo.counts + bucketcount;
7173 cinfo.symindx = dynsymcount - cinfo.nsyms;
7174 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7175
7176 /* Determine how often each hash bucket is used. */
7177 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7178 for (i = 0; i < cinfo.nsyms; ++i)
7179 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7180
7181 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7182 if (cinfo.counts[i] != 0)
7183 {
7184 cinfo.indx[i] = cnt;
7185 cnt += cinfo.counts[i];
7186 }
7187 BFD_ASSERT (cnt == dynsymcount);
7188 cinfo.bucketcount = bucketcount;
7189 cinfo.local_indx = cinfo.min_dynindx;
7190
7191 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7192 s->size += cinfo.maskbits / 8;
7193 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7194 if (contents == NULL)
7195 {
7196 free (cinfo.bitmask);
7197 free (cinfo.hashcodes);
7198 return FALSE;
7199 }
7200
7201 s->contents = contents;
7202 bfd_put_32 (output_bfd, bucketcount, contents);
7203 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7204 bfd_put_32 (output_bfd, maskwords, contents + 8);
7205 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7206 contents += 16 + cinfo.maskbits / 8;
7207
7208 for (i = 0; i < bucketcount; ++i)
7209 {
7210 if (cinfo.counts[i] == 0)
7211 bfd_put_32 (output_bfd, 0, contents);
7212 else
7213 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7214 contents += 4;
7215 }
7216
7217 cinfo.contents = contents;
7218
7219 /* Renumber dynamic symbols, populate .gnu.hash section. */
7220 elf_link_hash_traverse (elf_hash_table (info),
7221 elf_renumber_gnu_hash_syms, &cinfo);
7222
7223 contents = s->contents + 16;
7224 for (i = 0; i < maskwords; ++i)
7225 {
7226 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7227 contents);
7228 contents += bed->s->arch_size / 8;
7229 }
7230
7231 free (cinfo.bitmask);
7232 free (cinfo.hashcodes);
7233 }
7234 }
7235
7236 s = bfd_get_linker_section (dynobj, ".dynstr");
7237 BFD_ASSERT (s != NULL);
7238
7239 elf_finalize_dynstr (output_bfd, info);
7240
7241 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7242
7243 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7244 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7245 return FALSE;
7246 }
7247
7248 return TRUE;
7249 }
7250 \f
7251 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7252
7253 static void
7254 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7255 asection *sec)
7256 {
7257 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7258 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7259 }
7260
7261 /* Finish SHF_MERGE section merging. */
7262
7263 bfd_boolean
7264 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7265 {
7266 bfd *ibfd;
7267 asection *sec;
7268
7269 if (!is_elf_hash_table (info->hash))
7270 return FALSE;
7271
7272 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7273 if ((ibfd->flags & DYNAMIC) == 0
7274 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7275 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7276 == get_elf_backend_data (obfd)->s->elfclass))
7277 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7278 if ((sec->flags & SEC_MERGE) != 0
7279 && !bfd_is_abs_section (sec->output_section))
7280 {
7281 struct bfd_elf_section_data *secdata;
7282
7283 secdata = elf_section_data (sec);
7284 if (! _bfd_add_merge_section (obfd,
7285 &elf_hash_table (info)->merge_info,
7286 sec, &secdata->sec_info))
7287 return FALSE;
7288 else if (secdata->sec_info)
7289 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7290 }
7291
7292 if (elf_hash_table (info)->merge_info != NULL)
7293 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7294 merge_sections_remove_hook);
7295 return TRUE;
7296 }
7297
7298 /* Create an entry in an ELF linker hash table. */
7299
7300 struct bfd_hash_entry *
7301 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7302 struct bfd_hash_table *table,
7303 const char *string)
7304 {
7305 /* Allocate the structure if it has not already been allocated by a
7306 subclass. */
7307 if (entry == NULL)
7308 {
7309 entry = (struct bfd_hash_entry *)
7310 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7311 if (entry == NULL)
7312 return entry;
7313 }
7314
7315 /* Call the allocation method of the superclass. */
7316 entry = _bfd_link_hash_newfunc (entry, table, string);
7317 if (entry != NULL)
7318 {
7319 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7320 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7321
7322 /* Set local fields. */
7323 ret->indx = -1;
7324 ret->dynindx = -1;
7325 ret->got = htab->init_got_refcount;
7326 ret->plt = htab->init_plt_refcount;
7327 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7328 - offsetof (struct elf_link_hash_entry, size)));
7329 /* Assume that we have been called by a non-ELF symbol reader.
7330 This flag is then reset by the code which reads an ELF input
7331 file. This ensures that a symbol created by a non-ELF symbol
7332 reader will have the flag set correctly. */
7333 ret->non_elf = 1;
7334 }
7335
7336 return entry;
7337 }
7338
7339 /* Copy data from an indirect symbol to its direct symbol, hiding the
7340 old indirect symbol. Also used for copying flags to a weakdef. */
7341
7342 void
7343 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7344 struct elf_link_hash_entry *dir,
7345 struct elf_link_hash_entry *ind)
7346 {
7347 struct elf_link_hash_table *htab;
7348
7349 /* Copy down any references that we may have already seen to the
7350 symbol which just became indirect. */
7351
7352 if (dir->versioned != versioned_hidden)
7353 dir->ref_dynamic |= ind->ref_dynamic;
7354 dir->ref_regular |= ind->ref_regular;
7355 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7356 dir->non_got_ref |= ind->non_got_ref;
7357 dir->needs_plt |= ind->needs_plt;
7358 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7359
7360 if (ind->root.type != bfd_link_hash_indirect)
7361 return;
7362
7363 /* Copy over the global and procedure linkage table refcount entries.
7364 These may have been already set up by a check_relocs routine. */
7365 htab = elf_hash_table (info);
7366 if (ind->got.refcount > htab->init_got_refcount.refcount)
7367 {
7368 if (dir->got.refcount < 0)
7369 dir->got.refcount = 0;
7370 dir->got.refcount += ind->got.refcount;
7371 ind->got.refcount = htab->init_got_refcount.refcount;
7372 }
7373
7374 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7375 {
7376 if (dir->plt.refcount < 0)
7377 dir->plt.refcount = 0;
7378 dir->plt.refcount += ind->plt.refcount;
7379 ind->plt.refcount = htab->init_plt_refcount.refcount;
7380 }
7381
7382 if (ind->dynindx != -1)
7383 {
7384 if (dir->dynindx != -1)
7385 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7386 dir->dynindx = ind->dynindx;
7387 dir->dynstr_index = ind->dynstr_index;
7388 ind->dynindx = -1;
7389 ind->dynstr_index = 0;
7390 }
7391 }
7392
7393 void
7394 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7395 struct elf_link_hash_entry *h,
7396 bfd_boolean force_local)
7397 {
7398 /* STT_GNU_IFUNC symbol must go through PLT. */
7399 if (h->type != STT_GNU_IFUNC)
7400 {
7401 h->plt = elf_hash_table (info)->init_plt_offset;
7402 h->needs_plt = 0;
7403 }
7404 if (force_local)
7405 {
7406 h->forced_local = 1;
7407 if (h->dynindx != -1)
7408 {
7409 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7410 h->dynstr_index);
7411 h->dynindx = -1;
7412 h->dynstr_index = 0;
7413 }
7414 }
7415 }
7416
7417 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7418 caller. */
7419
7420 bfd_boolean
7421 _bfd_elf_link_hash_table_init
7422 (struct elf_link_hash_table *table,
7423 bfd *abfd,
7424 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7425 struct bfd_hash_table *,
7426 const char *),
7427 unsigned int entsize,
7428 enum elf_target_id target_id)
7429 {
7430 bfd_boolean ret;
7431 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7432
7433 table->init_got_refcount.refcount = can_refcount - 1;
7434 table->init_plt_refcount.refcount = can_refcount - 1;
7435 table->init_got_offset.offset = -(bfd_vma) 1;
7436 table->init_plt_offset.offset = -(bfd_vma) 1;
7437 /* The first dynamic symbol is a dummy. */
7438 table->dynsymcount = 1;
7439
7440 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7441
7442 table->root.type = bfd_link_elf_hash_table;
7443 table->hash_table_id = target_id;
7444
7445 return ret;
7446 }
7447
7448 /* Create an ELF linker hash table. */
7449
7450 struct bfd_link_hash_table *
7451 _bfd_elf_link_hash_table_create (bfd *abfd)
7452 {
7453 struct elf_link_hash_table *ret;
7454 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7455
7456 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7457 if (ret == NULL)
7458 return NULL;
7459
7460 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7461 sizeof (struct elf_link_hash_entry),
7462 GENERIC_ELF_DATA))
7463 {
7464 free (ret);
7465 return NULL;
7466 }
7467 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7468
7469 return &ret->root;
7470 }
7471
7472 /* Destroy an ELF linker hash table. */
7473
7474 void
7475 _bfd_elf_link_hash_table_free (bfd *obfd)
7476 {
7477 struct elf_link_hash_table *htab;
7478
7479 htab = (struct elf_link_hash_table *) obfd->link.hash;
7480 if (htab->dynstr != NULL)
7481 _bfd_elf_strtab_free (htab->dynstr);
7482 _bfd_merge_sections_free (htab->merge_info);
7483 _bfd_generic_link_hash_table_free (obfd);
7484 }
7485
7486 /* This is a hook for the ELF emulation code in the generic linker to
7487 tell the backend linker what file name to use for the DT_NEEDED
7488 entry for a dynamic object. */
7489
7490 void
7491 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7492 {
7493 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7494 && bfd_get_format (abfd) == bfd_object)
7495 elf_dt_name (abfd) = name;
7496 }
7497
7498 int
7499 bfd_elf_get_dyn_lib_class (bfd *abfd)
7500 {
7501 int lib_class;
7502 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7503 && bfd_get_format (abfd) == bfd_object)
7504 lib_class = elf_dyn_lib_class (abfd);
7505 else
7506 lib_class = 0;
7507 return lib_class;
7508 }
7509
7510 void
7511 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7512 {
7513 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7514 && bfd_get_format (abfd) == bfd_object)
7515 elf_dyn_lib_class (abfd) = lib_class;
7516 }
7517
7518 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7519 the linker ELF emulation code. */
7520
7521 struct bfd_link_needed_list *
7522 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7523 struct bfd_link_info *info)
7524 {
7525 if (! is_elf_hash_table (info->hash))
7526 return NULL;
7527 return elf_hash_table (info)->needed;
7528 }
7529
7530 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7531 hook for the linker ELF emulation code. */
7532
7533 struct bfd_link_needed_list *
7534 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7535 struct bfd_link_info *info)
7536 {
7537 if (! is_elf_hash_table (info->hash))
7538 return NULL;
7539 return elf_hash_table (info)->runpath;
7540 }
7541
7542 /* Get the name actually used for a dynamic object for a link. This
7543 is the SONAME entry if there is one. Otherwise, it is the string
7544 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7545
7546 const char *
7547 bfd_elf_get_dt_soname (bfd *abfd)
7548 {
7549 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7550 && bfd_get_format (abfd) == bfd_object)
7551 return elf_dt_name (abfd);
7552 return NULL;
7553 }
7554
7555 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7556 the ELF linker emulation code. */
7557
7558 bfd_boolean
7559 bfd_elf_get_bfd_needed_list (bfd *abfd,
7560 struct bfd_link_needed_list **pneeded)
7561 {
7562 asection *s;
7563 bfd_byte *dynbuf = NULL;
7564 unsigned int elfsec;
7565 unsigned long shlink;
7566 bfd_byte *extdyn, *extdynend;
7567 size_t extdynsize;
7568 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7569
7570 *pneeded = NULL;
7571
7572 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7573 || bfd_get_format (abfd) != bfd_object)
7574 return TRUE;
7575
7576 s = bfd_get_section_by_name (abfd, ".dynamic");
7577 if (s == NULL || s->size == 0)
7578 return TRUE;
7579
7580 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7581 goto error_return;
7582
7583 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7584 if (elfsec == SHN_BAD)
7585 goto error_return;
7586
7587 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7588
7589 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7590 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7591
7592 extdyn = dynbuf;
7593 extdynend = extdyn + s->size;
7594 for (; extdyn < extdynend; extdyn += extdynsize)
7595 {
7596 Elf_Internal_Dyn dyn;
7597
7598 (*swap_dyn_in) (abfd, extdyn, &dyn);
7599
7600 if (dyn.d_tag == DT_NULL)
7601 break;
7602
7603 if (dyn.d_tag == DT_NEEDED)
7604 {
7605 const char *string;
7606 struct bfd_link_needed_list *l;
7607 unsigned int tagv = dyn.d_un.d_val;
7608 bfd_size_type amt;
7609
7610 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7611 if (string == NULL)
7612 goto error_return;
7613
7614 amt = sizeof *l;
7615 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7616 if (l == NULL)
7617 goto error_return;
7618
7619 l->by = abfd;
7620 l->name = string;
7621 l->next = *pneeded;
7622 *pneeded = l;
7623 }
7624 }
7625
7626 free (dynbuf);
7627
7628 return TRUE;
7629
7630 error_return:
7631 if (dynbuf != NULL)
7632 free (dynbuf);
7633 return FALSE;
7634 }
7635
7636 struct elf_symbuf_symbol
7637 {
7638 unsigned long st_name; /* Symbol name, index in string tbl */
7639 unsigned char st_info; /* Type and binding attributes */
7640 unsigned char st_other; /* Visibilty, and target specific */
7641 };
7642
7643 struct elf_symbuf_head
7644 {
7645 struct elf_symbuf_symbol *ssym;
7646 size_t count;
7647 unsigned int st_shndx;
7648 };
7649
7650 struct elf_symbol
7651 {
7652 union
7653 {
7654 Elf_Internal_Sym *isym;
7655 struct elf_symbuf_symbol *ssym;
7656 } u;
7657 const char *name;
7658 };
7659
7660 /* Sort references to symbols by ascending section number. */
7661
7662 static int
7663 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7664 {
7665 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7666 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7667
7668 return s1->st_shndx - s2->st_shndx;
7669 }
7670
7671 static int
7672 elf_sym_name_compare (const void *arg1, const void *arg2)
7673 {
7674 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7675 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7676 return strcmp (s1->name, s2->name);
7677 }
7678
7679 static struct elf_symbuf_head *
7680 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
7681 {
7682 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7683 struct elf_symbuf_symbol *ssym;
7684 struct elf_symbuf_head *ssymbuf, *ssymhead;
7685 size_t i, shndx_count, total_size;
7686
7687 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7688 if (indbuf == NULL)
7689 return NULL;
7690
7691 for (ind = indbuf, i = 0; i < symcount; i++)
7692 if (isymbuf[i].st_shndx != SHN_UNDEF)
7693 *ind++ = &isymbuf[i];
7694 indbufend = ind;
7695
7696 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7697 elf_sort_elf_symbol);
7698
7699 shndx_count = 0;
7700 if (indbufend > indbuf)
7701 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7702 if (ind[0]->st_shndx != ind[1]->st_shndx)
7703 shndx_count++;
7704
7705 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7706 + (indbufend - indbuf) * sizeof (*ssym));
7707 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7708 if (ssymbuf == NULL)
7709 {
7710 free (indbuf);
7711 return NULL;
7712 }
7713
7714 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7715 ssymbuf->ssym = NULL;
7716 ssymbuf->count = shndx_count;
7717 ssymbuf->st_shndx = 0;
7718 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7719 {
7720 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7721 {
7722 ssymhead++;
7723 ssymhead->ssym = ssym;
7724 ssymhead->count = 0;
7725 ssymhead->st_shndx = (*ind)->st_shndx;
7726 }
7727 ssym->st_name = (*ind)->st_name;
7728 ssym->st_info = (*ind)->st_info;
7729 ssym->st_other = (*ind)->st_other;
7730 ssymhead->count++;
7731 }
7732 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
7733 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7734 == total_size));
7735
7736 free (indbuf);
7737 return ssymbuf;
7738 }
7739
7740 /* Check if 2 sections define the same set of local and global
7741 symbols. */
7742
7743 static bfd_boolean
7744 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7745 struct bfd_link_info *info)
7746 {
7747 bfd *bfd1, *bfd2;
7748 const struct elf_backend_data *bed1, *bed2;
7749 Elf_Internal_Shdr *hdr1, *hdr2;
7750 size_t symcount1, symcount2;
7751 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7752 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7753 Elf_Internal_Sym *isym, *isymend;
7754 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7755 size_t count1, count2, i;
7756 unsigned int shndx1, shndx2;
7757 bfd_boolean result;
7758
7759 bfd1 = sec1->owner;
7760 bfd2 = sec2->owner;
7761
7762 /* Both sections have to be in ELF. */
7763 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7764 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7765 return FALSE;
7766
7767 if (elf_section_type (sec1) != elf_section_type (sec2))
7768 return FALSE;
7769
7770 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7771 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7772 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7773 return FALSE;
7774
7775 bed1 = get_elf_backend_data (bfd1);
7776 bed2 = get_elf_backend_data (bfd2);
7777 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7778 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7779 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7780 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7781
7782 if (symcount1 == 0 || symcount2 == 0)
7783 return FALSE;
7784
7785 result = FALSE;
7786 isymbuf1 = NULL;
7787 isymbuf2 = NULL;
7788 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7789 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7790
7791 if (ssymbuf1 == NULL)
7792 {
7793 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7794 NULL, NULL, NULL);
7795 if (isymbuf1 == NULL)
7796 goto done;
7797
7798 if (!info->reduce_memory_overheads)
7799 elf_tdata (bfd1)->symbuf = ssymbuf1
7800 = elf_create_symbuf (symcount1, isymbuf1);
7801 }
7802
7803 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7804 {
7805 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7806 NULL, NULL, NULL);
7807 if (isymbuf2 == NULL)
7808 goto done;
7809
7810 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7811 elf_tdata (bfd2)->symbuf = ssymbuf2
7812 = elf_create_symbuf (symcount2, isymbuf2);
7813 }
7814
7815 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7816 {
7817 /* Optimized faster version. */
7818 size_t lo, hi, mid;
7819 struct elf_symbol *symp;
7820 struct elf_symbuf_symbol *ssym, *ssymend;
7821
7822 lo = 0;
7823 hi = ssymbuf1->count;
7824 ssymbuf1++;
7825 count1 = 0;
7826 while (lo < hi)
7827 {
7828 mid = (lo + hi) / 2;
7829 if (shndx1 < ssymbuf1[mid].st_shndx)
7830 hi = mid;
7831 else if (shndx1 > ssymbuf1[mid].st_shndx)
7832 lo = mid + 1;
7833 else
7834 {
7835 count1 = ssymbuf1[mid].count;
7836 ssymbuf1 += mid;
7837 break;
7838 }
7839 }
7840
7841 lo = 0;
7842 hi = ssymbuf2->count;
7843 ssymbuf2++;
7844 count2 = 0;
7845 while (lo < hi)
7846 {
7847 mid = (lo + hi) / 2;
7848 if (shndx2 < ssymbuf2[mid].st_shndx)
7849 hi = mid;
7850 else if (shndx2 > ssymbuf2[mid].st_shndx)
7851 lo = mid + 1;
7852 else
7853 {
7854 count2 = ssymbuf2[mid].count;
7855 ssymbuf2 += mid;
7856 break;
7857 }
7858 }
7859
7860 if (count1 == 0 || count2 == 0 || count1 != count2)
7861 goto done;
7862
7863 symtable1
7864 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7865 symtable2
7866 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
7867 if (symtable1 == NULL || symtable2 == NULL)
7868 goto done;
7869
7870 symp = symtable1;
7871 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7872 ssym < ssymend; ssym++, symp++)
7873 {
7874 symp->u.ssym = ssym;
7875 symp->name = bfd_elf_string_from_elf_section (bfd1,
7876 hdr1->sh_link,
7877 ssym->st_name);
7878 }
7879
7880 symp = symtable2;
7881 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7882 ssym < ssymend; ssym++, symp++)
7883 {
7884 symp->u.ssym = ssym;
7885 symp->name = bfd_elf_string_from_elf_section (bfd2,
7886 hdr2->sh_link,
7887 ssym->st_name);
7888 }
7889
7890 /* Sort symbol by name. */
7891 qsort (symtable1, count1, sizeof (struct elf_symbol),
7892 elf_sym_name_compare);
7893 qsort (symtable2, count1, sizeof (struct elf_symbol),
7894 elf_sym_name_compare);
7895
7896 for (i = 0; i < count1; i++)
7897 /* Two symbols must have the same binding, type and name. */
7898 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7899 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7900 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7901 goto done;
7902
7903 result = TRUE;
7904 goto done;
7905 }
7906
7907 symtable1 = (struct elf_symbol *)
7908 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7909 symtable2 = (struct elf_symbol *)
7910 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7911 if (symtable1 == NULL || symtable2 == NULL)
7912 goto done;
7913
7914 /* Count definitions in the section. */
7915 count1 = 0;
7916 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7917 if (isym->st_shndx == shndx1)
7918 symtable1[count1++].u.isym = isym;
7919
7920 count2 = 0;
7921 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7922 if (isym->st_shndx == shndx2)
7923 symtable2[count2++].u.isym = isym;
7924
7925 if (count1 == 0 || count2 == 0 || count1 != count2)
7926 goto done;
7927
7928 for (i = 0; i < count1; i++)
7929 symtable1[i].name
7930 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7931 symtable1[i].u.isym->st_name);
7932
7933 for (i = 0; i < count2; i++)
7934 symtable2[i].name
7935 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7936 symtable2[i].u.isym->st_name);
7937
7938 /* Sort symbol by name. */
7939 qsort (symtable1, count1, sizeof (struct elf_symbol),
7940 elf_sym_name_compare);
7941 qsort (symtable2, count1, sizeof (struct elf_symbol),
7942 elf_sym_name_compare);
7943
7944 for (i = 0; i < count1; i++)
7945 /* Two symbols must have the same binding, type and name. */
7946 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7947 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7948 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7949 goto done;
7950
7951 result = TRUE;
7952
7953 done:
7954 if (symtable1)
7955 free (symtable1);
7956 if (symtable2)
7957 free (symtable2);
7958 if (isymbuf1)
7959 free (isymbuf1);
7960 if (isymbuf2)
7961 free (isymbuf2);
7962
7963 return result;
7964 }
7965
7966 /* Return TRUE if 2 section types are compatible. */
7967
7968 bfd_boolean
7969 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7970 bfd *bbfd, const asection *bsec)
7971 {
7972 if (asec == NULL
7973 || bsec == NULL
7974 || abfd->xvec->flavour != bfd_target_elf_flavour
7975 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7976 return TRUE;
7977
7978 return elf_section_type (asec) == elf_section_type (bsec);
7979 }
7980 \f
7981 /* Final phase of ELF linker. */
7982
7983 /* A structure we use to avoid passing large numbers of arguments. */
7984
7985 struct elf_final_link_info
7986 {
7987 /* General link information. */
7988 struct bfd_link_info *info;
7989 /* Output BFD. */
7990 bfd *output_bfd;
7991 /* Symbol string table. */
7992 struct elf_strtab_hash *symstrtab;
7993 /* .hash section. */
7994 asection *hash_sec;
7995 /* symbol version section (.gnu.version). */
7996 asection *symver_sec;
7997 /* Buffer large enough to hold contents of any section. */
7998 bfd_byte *contents;
7999 /* Buffer large enough to hold external relocs of any section. */
8000 void *external_relocs;
8001 /* Buffer large enough to hold internal relocs of any section. */
8002 Elf_Internal_Rela *internal_relocs;
8003 /* Buffer large enough to hold external local symbols of any input
8004 BFD. */
8005 bfd_byte *external_syms;
8006 /* And a buffer for symbol section indices. */
8007 Elf_External_Sym_Shndx *locsym_shndx;
8008 /* Buffer large enough to hold internal local symbols of any input
8009 BFD. */
8010 Elf_Internal_Sym *internal_syms;
8011 /* Array large enough to hold a symbol index for each local symbol
8012 of any input BFD. */
8013 long *indices;
8014 /* Array large enough to hold a section pointer for each local
8015 symbol of any input BFD. */
8016 asection **sections;
8017 /* Buffer for SHT_SYMTAB_SHNDX section. */
8018 Elf_External_Sym_Shndx *symshndxbuf;
8019 /* Number of STT_FILE syms seen. */
8020 size_t filesym_count;
8021 };
8022
8023 /* This struct is used to pass information to elf_link_output_extsym. */
8024
8025 struct elf_outext_info
8026 {
8027 bfd_boolean failed;
8028 bfd_boolean localsyms;
8029 bfd_boolean file_sym_done;
8030 struct elf_final_link_info *flinfo;
8031 };
8032
8033
8034 /* Support for evaluating a complex relocation.
8035
8036 Complex relocations are generalized, self-describing relocations. The
8037 implementation of them consists of two parts: complex symbols, and the
8038 relocations themselves.
8039
8040 The relocations are use a reserved elf-wide relocation type code (R_RELC
8041 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8042 information (start bit, end bit, word width, etc) into the addend. This
8043 information is extracted from CGEN-generated operand tables within gas.
8044
8045 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8046 internal) representing prefix-notation expressions, including but not
8047 limited to those sorts of expressions normally encoded as addends in the
8048 addend field. The symbol mangling format is:
8049
8050 <node> := <literal>
8051 | <unary-operator> ':' <node>
8052 | <binary-operator> ':' <node> ':' <node>
8053 ;
8054
8055 <literal> := 's' <digits=N> ':' <N character symbol name>
8056 | 'S' <digits=N> ':' <N character section name>
8057 | '#' <hexdigits>
8058 ;
8059
8060 <binary-operator> := as in C
8061 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8062
8063 static void
8064 set_symbol_value (bfd *bfd_with_globals,
8065 Elf_Internal_Sym *isymbuf,
8066 size_t locsymcount,
8067 size_t symidx,
8068 bfd_vma val)
8069 {
8070 struct elf_link_hash_entry **sym_hashes;
8071 struct elf_link_hash_entry *h;
8072 size_t extsymoff = locsymcount;
8073
8074 if (symidx < locsymcount)
8075 {
8076 Elf_Internal_Sym *sym;
8077
8078 sym = isymbuf + symidx;
8079 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8080 {
8081 /* It is a local symbol: move it to the
8082 "absolute" section and give it a value. */
8083 sym->st_shndx = SHN_ABS;
8084 sym->st_value = val;
8085 return;
8086 }
8087 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8088 extsymoff = 0;
8089 }
8090
8091 /* It is a global symbol: set its link type
8092 to "defined" and give it a value. */
8093
8094 sym_hashes = elf_sym_hashes (bfd_with_globals);
8095 h = sym_hashes [symidx - extsymoff];
8096 while (h->root.type == bfd_link_hash_indirect
8097 || h->root.type == bfd_link_hash_warning)
8098 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8099 h->root.type = bfd_link_hash_defined;
8100 h->root.u.def.value = val;
8101 h->root.u.def.section = bfd_abs_section_ptr;
8102 }
8103
8104 static bfd_boolean
8105 resolve_symbol (const char *name,
8106 bfd *input_bfd,
8107 struct elf_final_link_info *flinfo,
8108 bfd_vma *result,
8109 Elf_Internal_Sym *isymbuf,
8110 size_t locsymcount)
8111 {
8112 Elf_Internal_Sym *sym;
8113 struct bfd_link_hash_entry *global_entry;
8114 const char *candidate = NULL;
8115 Elf_Internal_Shdr *symtab_hdr;
8116 size_t i;
8117
8118 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8119
8120 for (i = 0; i < locsymcount; ++ i)
8121 {
8122 sym = isymbuf + i;
8123
8124 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8125 continue;
8126
8127 candidate = bfd_elf_string_from_elf_section (input_bfd,
8128 symtab_hdr->sh_link,
8129 sym->st_name);
8130 #ifdef DEBUG
8131 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8132 name, candidate, (unsigned long) sym->st_value);
8133 #endif
8134 if (candidate && strcmp (candidate, name) == 0)
8135 {
8136 asection *sec = flinfo->sections [i];
8137
8138 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8139 *result += sec->output_offset + sec->output_section->vma;
8140 #ifdef DEBUG
8141 printf ("Found symbol with value %8.8lx\n",
8142 (unsigned long) *result);
8143 #endif
8144 return TRUE;
8145 }
8146 }
8147
8148 /* Hmm, haven't found it yet. perhaps it is a global. */
8149 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8150 FALSE, FALSE, TRUE);
8151 if (!global_entry)
8152 return FALSE;
8153
8154 if (global_entry->type == bfd_link_hash_defined
8155 || global_entry->type == bfd_link_hash_defweak)
8156 {
8157 *result = (global_entry->u.def.value
8158 + global_entry->u.def.section->output_section->vma
8159 + global_entry->u.def.section->output_offset);
8160 #ifdef DEBUG
8161 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8162 global_entry->root.string, (unsigned long) *result);
8163 #endif
8164 return TRUE;
8165 }
8166
8167 return FALSE;
8168 }
8169
8170 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8171 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8172 names like "foo.end" which is the end address of section "foo". */
8173
8174 static bfd_boolean
8175 resolve_section (const char *name,
8176 asection *sections,
8177 bfd_vma *result,
8178 bfd * abfd)
8179 {
8180 asection *curr;
8181 unsigned int len;
8182
8183 for (curr = sections; curr; curr = curr->next)
8184 if (strcmp (curr->name, name) == 0)
8185 {
8186 *result = curr->vma;
8187 return TRUE;
8188 }
8189
8190 /* Hmm. still haven't found it. try pseudo-section names. */
8191 /* FIXME: This could be coded more efficiently... */
8192 for (curr = sections; curr; curr = curr->next)
8193 {
8194 len = strlen (curr->name);
8195 if (len > strlen (name))
8196 continue;
8197
8198 if (strncmp (curr->name, name, len) == 0)
8199 {
8200 if (strncmp (".end", name + len, 4) == 0)
8201 {
8202 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
8203 return TRUE;
8204 }
8205
8206 /* Insert more pseudo-section names here, if you like. */
8207 }
8208 }
8209
8210 return FALSE;
8211 }
8212
8213 static void
8214 undefined_reference (const char *reftype, const char *name)
8215 {
8216 /* xgettext:c-format */
8217 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8218 reftype, name);
8219 }
8220
8221 static bfd_boolean
8222 eval_symbol (bfd_vma *result,
8223 const char **symp,
8224 bfd *input_bfd,
8225 struct elf_final_link_info *flinfo,
8226 bfd_vma dot,
8227 Elf_Internal_Sym *isymbuf,
8228 size_t locsymcount,
8229 int signed_p)
8230 {
8231 size_t len;
8232 size_t symlen;
8233 bfd_vma a;
8234 bfd_vma b;
8235 char symbuf[4096];
8236 const char *sym = *symp;
8237 const char *symend;
8238 bfd_boolean symbol_is_section = FALSE;
8239
8240 len = strlen (sym);
8241 symend = sym + len;
8242
8243 if (len < 1 || len > sizeof (symbuf))
8244 {
8245 bfd_set_error (bfd_error_invalid_operation);
8246 return FALSE;
8247 }
8248
8249 switch (* sym)
8250 {
8251 case '.':
8252 *result = dot;
8253 *symp = sym + 1;
8254 return TRUE;
8255
8256 case '#':
8257 ++sym;
8258 *result = strtoul (sym, (char **) symp, 16);
8259 return TRUE;
8260
8261 case 'S':
8262 symbol_is_section = TRUE;
8263 /* Fall through. */
8264 case 's':
8265 ++sym;
8266 symlen = strtol (sym, (char **) symp, 10);
8267 sym = *symp + 1; /* Skip the trailing ':'. */
8268
8269 if (symend < sym || symlen + 1 > sizeof (symbuf))
8270 {
8271 bfd_set_error (bfd_error_invalid_operation);
8272 return FALSE;
8273 }
8274
8275 memcpy (symbuf, sym, symlen);
8276 symbuf[symlen] = '\0';
8277 *symp = sym + symlen;
8278
8279 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8280 the symbol as a section, or vice-versa. so we're pretty liberal in our
8281 interpretation here; section means "try section first", not "must be a
8282 section", and likewise with symbol. */
8283
8284 if (symbol_is_section)
8285 {
8286 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8287 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8288 isymbuf, locsymcount))
8289 {
8290 undefined_reference ("section", symbuf);
8291 return FALSE;
8292 }
8293 }
8294 else
8295 {
8296 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8297 isymbuf, locsymcount)
8298 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8299 result, input_bfd))
8300 {
8301 undefined_reference ("symbol", symbuf);
8302 return FALSE;
8303 }
8304 }
8305
8306 return TRUE;
8307
8308 /* All that remains are operators. */
8309
8310 #define UNARY_OP(op) \
8311 if (strncmp (sym, #op, strlen (#op)) == 0) \
8312 { \
8313 sym += strlen (#op); \
8314 if (*sym == ':') \
8315 ++sym; \
8316 *symp = sym; \
8317 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8318 isymbuf, locsymcount, signed_p)) \
8319 return FALSE; \
8320 if (signed_p) \
8321 *result = op ((bfd_signed_vma) a); \
8322 else \
8323 *result = op a; \
8324 return TRUE; \
8325 }
8326
8327 #define BINARY_OP(op) \
8328 if (strncmp (sym, #op, strlen (#op)) == 0) \
8329 { \
8330 sym += strlen (#op); \
8331 if (*sym == ':') \
8332 ++sym; \
8333 *symp = sym; \
8334 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8335 isymbuf, locsymcount, signed_p)) \
8336 return FALSE; \
8337 ++*symp; \
8338 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8339 isymbuf, locsymcount, signed_p)) \
8340 return FALSE; \
8341 if (signed_p) \
8342 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8343 else \
8344 *result = a op b; \
8345 return TRUE; \
8346 }
8347
8348 default:
8349 UNARY_OP (0-);
8350 BINARY_OP (<<);
8351 BINARY_OP (>>);
8352 BINARY_OP (==);
8353 BINARY_OP (!=);
8354 BINARY_OP (<=);
8355 BINARY_OP (>=);
8356 BINARY_OP (&&);
8357 BINARY_OP (||);
8358 UNARY_OP (~);
8359 UNARY_OP (!);
8360 BINARY_OP (*);
8361 BINARY_OP (/);
8362 BINARY_OP (%);
8363 BINARY_OP (^);
8364 BINARY_OP (|);
8365 BINARY_OP (&);
8366 BINARY_OP (+);
8367 BINARY_OP (-);
8368 BINARY_OP (<);
8369 BINARY_OP (>);
8370 #undef UNARY_OP
8371 #undef BINARY_OP
8372 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8373 bfd_set_error (bfd_error_invalid_operation);
8374 return FALSE;
8375 }
8376 }
8377
8378 static void
8379 put_value (bfd_vma size,
8380 unsigned long chunksz,
8381 bfd *input_bfd,
8382 bfd_vma x,
8383 bfd_byte *location)
8384 {
8385 location += (size - chunksz);
8386
8387 for (; size; size -= chunksz, location -= chunksz)
8388 {
8389 switch (chunksz)
8390 {
8391 case 1:
8392 bfd_put_8 (input_bfd, x, location);
8393 x >>= 8;
8394 break;
8395 case 2:
8396 bfd_put_16 (input_bfd, x, location);
8397 x >>= 16;
8398 break;
8399 case 4:
8400 bfd_put_32 (input_bfd, x, location);
8401 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8402 x >>= 16;
8403 x >>= 16;
8404 break;
8405 #ifdef BFD64
8406 case 8:
8407 bfd_put_64 (input_bfd, x, location);
8408 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8409 x >>= 32;
8410 x >>= 32;
8411 break;
8412 #endif
8413 default:
8414 abort ();
8415 break;
8416 }
8417 }
8418 }
8419
8420 static bfd_vma
8421 get_value (bfd_vma size,
8422 unsigned long chunksz,
8423 bfd *input_bfd,
8424 bfd_byte *location)
8425 {
8426 int shift;
8427 bfd_vma x = 0;
8428
8429 /* Sanity checks. */
8430 BFD_ASSERT (chunksz <= sizeof (x)
8431 && size >= chunksz
8432 && chunksz != 0
8433 && (size % chunksz) == 0
8434 && input_bfd != NULL
8435 && location != NULL);
8436
8437 if (chunksz == sizeof (x))
8438 {
8439 BFD_ASSERT (size == chunksz);
8440
8441 /* Make sure that we do not perform an undefined shift operation.
8442 We know that size == chunksz so there will only be one iteration
8443 of the loop below. */
8444 shift = 0;
8445 }
8446 else
8447 shift = 8 * chunksz;
8448
8449 for (; size; size -= chunksz, location += chunksz)
8450 {
8451 switch (chunksz)
8452 {
8453 case 1:
8454 x = (x << shift) | bfd_get_8 (input_bfd, location);
8455 break;
8456 case 2:
8457 x = (x << shift) | bfd_get_16 (input_bfd, location);
8458 break;
8459 case 4:
8460 x = (x << shift) | bfd_get_32 (input_bfd, location);
8461 break;
8462 #ifdef BFD64
8463 case 8:
8464 x = (x << shift) | bfd_get_64 (input_bfd, location);
8465 break;
8466 #endif
8467 default:
8468 abort ();
8469 }
8470 }
8471 return x;
8472 }
8473
8474 static void
8475 decode_complex_addend (unsigned long *start, /* in bits */
8476 unsigned long *oplen, /* in bits */
8477 unsigned long *len, /* in bits */
8478 unsigned long *wordsz, /* in bytes */
8479 unsigned long *chunksz, /* in bytes */
8480 unsigned long *lsb0_p,
8481 unsigned long *signed_p,
8482 unsigned long *trunc_p,
8483 unsigned long encoded)
8484 {
8485 * start = encoded & 0x3F;
8486 * len = (encoded >> 6) & 0x3F;
8487 * oplen = (encoded >> 12) & 0x3F;
8488 * wordsz = (encoded >> 18) & 0xF;
8489 * chunksz = (encoded >> 22) & 0xF;
8490 * lsb0_p = (encoded >> 27) & 1;
8491 * signed_p = (encoded >> 28) & 1;
8492 * trunc_p = (encoded >> 29) & 1;
8493 }
8494
8495 bfd_reloc_status_type
8496 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8497 asection *input_section ATTRIBUTE_UNUSED,
8498 bfd_byte *contents,
8499 Elf_Internal_Rela *rel,
8500 bfd_vma relocation)
8501 {
8502 bfd_vma shift, x, mask;
8503 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8504 bfd_reloc_status_type r;
8505
8506 /* Perform this reloc, since it is complex.
8507 (this is not to say that it necessarily refers to a complex
8508 symbol; merely that it is a self-describing CGEN based reloc.
8509 i.e. the addend has the complete reloc information (bit start, end,
8510 word size, etc) encoded within it.). */
8511
8512 decode_complex_addend (&start, &oplen, &len, &wordsz,
8513 &chunksz, &lsb0_p, &signed_p,
8514 &trunc_p, rel->r_addend);
8515
8516 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8517
8518 if (lsb0_p)
8519 shift = (start + 1) - len;
8520 else
8521 shift = (8 * wordsz) - (start + len);
8522
8523 x = get_value (wordsz, chunksz, input_bfd,
8524 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8525
8526 #ifdef DEBUG
8527 printf ("Doing complex reloc: "
8528 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8529 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8530 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8531 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8532 oplen, (unsigned long) x, (unsigned long) mask,
8533 (unsigned long) relocation);
8534 #endif
8535
8536 r = bfd_reloc_ok;
8537 if (! trunc_p)
8538 /* Now do an overflow check. */
8539 r = bfd_check_overflow ((signed_p
8540 ? complain_overflow_signed
8541 : complain_overflow_unsigned),
8542 len, 0, (8 * wordsz),
8543 relocation);
8544
8545 /* Do the deed. */
8546 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8547
8548 #ifdef DEBUG
8549 printf (" relocation: %8.8lx\n"
8550 " shifted mask: %8.8lx\n"
8551 " shifted/masked reloc: %8.8lx\n"
8552 " result: %8.8lx\n",
8553 (unsigned long) relocation, (unsigned long) (mask << shift),
8554 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8555 #endif
8556 put_value (wordsz, chunksz, input_bfd, x,
8557 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8558 return r;
8559 }
8560
8561 /* Functions to read r_offset from external (target order) reloc
8562 entry. Faster than bfd_getl32 et al, because we let the compiler
8563 know the value is aligned. */
8564
8565 static bfd_vma
8566 ext32l_r_offset (const void *p)
8567 {
8568 union aligned32
8569 {
8570 uint32_t v;
8571 unsigned char c[4];
8572 };
8573 const union aligned32 *a
8574 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8575
8576 uint32_t aval = ( (uint32_t) a->c[0]
8577 | (uint32_t) a->c[1] << 8
8578 | (uint32_t) a->c[2] << 16
8579 | (uint32_t) a->c[3] << 24);
8580 return aval;
8581 }
8582
8583 static bfd_vma
8584 ext32b_r_offset (const void *p)
8585 {
8586 union aligned32
8587 {
8588 uint32_t v;
8589 unsigned char c[4];
8590 };
8591 const union aligned32 *a
8592 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8593
8594 uint32_t aval = ( (uint32_t) a->c[0] << 24
8595 | (uint32_t) a->c[1] << 16
8596 | (uint32_t) a->c[2] << 8
8597 | (uint32_t) a->c[3]);
8598 return aval;
8599 }
8600
8601 #ifdef BFD_HOST_64_BIT
8602 static bfd_vma
8603 ext64l_r_offset (const void *p)
8604 {
8605 union aligned64
8606 {
8607 uint64_t v;
8608 unsigned char c[8];
8609 };
8610 const union aligned64 *a
8611 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8612
8613 uint64_t aval = ( (uint64_t) a->c[0]
8614 | (uint64_t) a->c[1] << 8
8615 | (uint64_t) a->c[2] << 16
8616 | (uint64_t) a->c[3] << 24
8617 | (uint64_t) a->c[4] << 32
8618 | (uint64_t) a->c[5] << 40
8619 | (uint64_t) a->c[6] << 48
8620 | (uint64_t) a->c[7] << 56);
8621 return aval;
8622 }
8623
8624 static bfd_vma
8625 ext64b_r_offset (const void *p)
8626 {
8627 union aligned64
8628 {
8629 uint64_t v;
8630 unsigned char c[8];
8631 };
8632 const union aligned64 *a
8633 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8634
8635 uint64_t aval = ( (uint64_t) a->c[0] << 56
8636 | (uint64_t) a->c[1] << 48
8637 | (uint64_t) a->c[2] << 40
8638 | (uint64_t) a->c[3] << 32
8639 | (uint64_t) a->c[4] << 24
8640 | (uint64_t) a->c[5] << 16
8641 | (uint64_t) a->c[6] << 8
8642 | (uint64_t) a->c[7]);
8643 return aval;
8644 }
8645 #endif
8646
8647 /* When performing a relocatable link, the input relocations are
8648 preserved. But, if they reference global symbols, the indices
8649 referenced must be updated. Update all the relocations found in
8650 RELDATA. */
8651
8652 static bfd_boolean
8653 elf_link_adjust_relocs (bfd *abfd,
8654 asection *sec,
8655 struct bfd_elf_section_reloc_data *reldata,
8656 bfd_boolean sort,
8657 struct bfd_link_info *info)
8658 {
8659 unsigned int i;
8660 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8661 bfd_byte *erela;
8662 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8663 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8664 bfd_vma r_type_mask;
8665 int r_sym_shift;
8666 unsigned int count = reldata->count;
8667 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8668
8669 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8670 {
8671 swap_in = bed->s->swap_reloc_in;
8672 swap_out = bed->s->swap_reloc_out;
8673 }
8674 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8675 {
8676 swap_in = bed->s->swap_reloca_in;
8677 swap_out = bed->s->swap_reloca_out;
8678 }
8679 else
8680 abort ();
8681
8682 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8683 abort ();
8684
8685 if (bed->s->arch_size == 32)
8686 {
8687 r_type_mask = 0xff;
8688 r_sym_shift = 8;
8689 }
8690 else
8691 {
8692 r_type_mask = 0xffffffff;
8693 r_sym_shift = 32;
8694 }
8695
8696 erela = reldata->hdr->contents;
8697 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8698 {
8699 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8700 unsigned int j;
8701
8702 if (*rel_hash == NULL)
8703 continue;
8704
8705 if ((*rel_hash)->indx == -2
8706 && info->gc_sections
8707 && ! info->gc_keep_exported)
8708 {
8709 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
8710 _bfd_error_handler (_("%B:%A: error: relocation references symbol %s which was removed by garbage collection."),
8711 abfd, sec,
8712 (*rel_hash)->root.root.string);
8713 _bfd_error_handler (_("%B:%A: error: try relinking with --gc-keep-exported enabled."),
8714 abfd, sec);
8715 bfd_set_error (bfd_error_invalid_operation);
8716 return FALSE;
8717 }
8718 BFD_ASSERT ((*rel_hash)->indx >= 0);
8719
8720 (*swap_in) (abfd, erela, irela);
8721 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8722 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8723 | (irela[j].r_info & r_type_mask));
8724 (*swap_out) (abfd, irela, erela);
8725 }
8726
8727 if (bed->elf_backend_update_relocs)
8728 (*bed->elf_backend_update_relocs) (sec, reldata);
8729
8730 if (sort && count != 0)
8731 {
8732 bfd_vma (*ext_r_off) (const void *);
8733 bfd_vma r_off;
8734 size_t elt_size;
8735 bfd_byte *base, *end, *p, *loc;
8736 bfd_byte *buf = NULL;
8737
8738 if (bed->s->arch_size == 32)
8739 {
8740 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8741 ext_r_off = ext32l_r_offset;
8742 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8743 ext_r_off = ext32b_r_offset;
8744 else
8745 abort ();
8746 }
8747 else
8748 {
8749 #ifdef BFD_HOST_64_BIT
8750 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8751 ext_r_off = ext64l_r_offset;
8752 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8753 ext_r_off = ext64b_r_offset;
8754 else
8755 #endif
8756 abort ();
8757 }
8758
8759 /* Must use a stable sort here. A modified insertion sort,
8760 since the relocs are mostly sorted already. */
8761 elt_size = reldata->hdr->sh_entsize;
8762 base = reldata->hdr->contents;
8763 end = base + count * elt_size;
8764 if (elt_size > sizeof (Elf64_External_Rela))
8765 abort ();
8766
8767 /* Ensure the first element is lowest. This acts as a sentinel,
8768 speeding the main loop below. */
8769 r_off = (*ext_r_off) (base);
8770 for (p = loc = base; (p += elt_size) < end; )
8771 {
8772 bfd_vma r_off2 = (*ext_r_off) (p);
8773 if (r_off > r_off2)
8774 {
8775 r_off = r_off2;
8776 loc = p;
8777 }
8778 }
8779 if (loc != base)
8780 {
8781 /* Don't just swap *base and *loc as that changes the order
8782 of the original base[0] and base[1] if they happen to
8783 have the same r_offset. */
8784 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8785 memcpy (onebuf, loc, elt_size);
8786 memmove (base + elt_size, base, loc - base);
8787 memcpy (base, onebuf, elt_size);
8788 }
8789
8790 for (p = base + elt_size; (p += elt_size) < end; )
8791 {
8792 /* base to p is sorted, *p is next to insert. */
8793 r_off = (*ext_r_off) (p);
8794 /* Search the sorted region for location to insert. */
8795 loc = p - elt_size;
8796 while (r_off < (*ext_r_off) (loc))
8797 loc -= elt_size;
8798 loc += elt_size;
8799 if (loc != p)
8800 {
8801 /* Chances are there is a run of relocs to insert here,
8802 from one of more input files. Files are not always
8803 linked in order due to the way elf_link_input_bfd is
8804 called. See pr17666. */
8805 size_t sortlen = p - loc;
8806 bfd_vma r_off2 = (*ext_r_off) (loc);
8807 size_t runlen = elt_size;
8808 size_t buf_size = 96 * 1024;
8809 while (p + runlen < end
8810 && (sortlen <= buf_size
8811 || runlen + elt_size <= buf_size)
8812 && r_off2 > (*ext_r_off) (p + runlen))
8813 runlen += elt_size;
8814 if (buf == NULL)
8815 {
8816 buf = bfd_malloc (buf_size);
8817 if (buf == NULL)
8818 return FALSE;
8819 }
8820 if (runlen < sortlen)
8821 {
8822 memcpy (buf, p, runlen);
8823 memmove (loc + runlen, loc, sortlen);
8824 memcpy (loc, buf, runlen);
8825 }
8826 else
8827 {
8828 memcpy (buf, loc, sortlen);
8829 memmove (loc, p, runlen);
8830 memcpy (loc + runlen, buf, sortlen);
8831 }
8832 p += runlen - elt_size;
8833 }
8834 }
8835 /* Hashes are no longer valid. */
8836 free (reldata->hashes);
8837 reldata->hashes = NULL;
8838 free (buf);
8839 }
8840 return TRUE;
8841 }
8842
8843 struct elf_link_sort_rela
8844 {
8845 union {
8846 bfd_vma offset;
8847 bfd_vma sym_mask;
8848 } u;
8849 enum elf_reloc_type_class type;
8850 /* We use this as an array of size int_rels_per_ext_rel. */
8851 Elf_Internal_Rela rela[1];
8852 };
8853
8854 static int
8855 elf_link_sort_cmp1 (const void *A, const void *B)
8856 {
8857 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8858 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8859 int relativea, relativeb;
8860
8861 relativea = a->type == reloc_class_relative;
8862 relativeb = b->type == reloc_class_relative;
8863
8864 if (relativea < relativeb)
8865 return 1;
8866 if (relativea > relativeb)
8867 return -1;
8868 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8869 return -1;
8870 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8871 return 1;
8872 if (a->rela->r_offset < b->rela->r_offset)
8873 return -1;
8874 if (a->rela->r_offset > b->rela->r_offset)
8875 return 1;
8876 return 0;
8877 }
8878
8879 static int
8880 elf_link_sort_cmp2 (const void *A, const void *B)
8881 {
8882 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8883 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8884
8885 if (a->type < b->type)
8886 return -1;
8887 if (a->type > b->type)
8888 return 1;
8889 if (a->u.offset < b->u.offset)
8890 return -1;
8891 if (a->u.offset > b->u.offset)
8892 return 1;
8893 if (a->rela->r_offset < b->rela->r_offset)
8894 return -1;
8895 if (a->rela->r_offset > b->rela->r_offset)
8896 return 1;
8897 return 0;
8898 }
8899
8900 static size_t
8901 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8902 {
8903 asection *dynamic_relocs;
8904 asection *rela_dyn;
8905 asection *rel_dyn;
8906 bfd_size_type count, size;
8907 size_t i, ret, sort_elt, ext_size;
8908 bfd_byte *sort, *s_non_relative, *p;
8909 struct elf_link_sort_rela *sq;
8910 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8911 int i2e = bed->s->int_rels_per_ext_rel;
8912 unsigned int opb = bfd_octets_per_byte (abfd);
8913 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8914 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8915 struct bfd_link_order *lo;
8916 bfd_vma r_sym_mask;
8917 bfd_boolean use_rela;
8918
8919 /* Find a dynamic reloc section. */
8920 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8921 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8922 if (rela_dyn != NULL && rela_dyn->size > 0
8923 && rel_dyn != NULL && rel_dyn->size > 0)
8924 {
8925 bfd_boolean use_rela_initialised = FALSE;
8926
8927 /* This is just here to stop gcc from complaining.
8928 Its initialization checking code is not perfect. */
8929 use_rela = TRUE;
8930
8931 /* Both sections are present. Examine the sizes
8932 of the indirect sections to help us choose. */
8933 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8934 if (lo->type == bfd_indirect_link_order)
8935 {
8936 asection *o = lo->u.indirect.section;
8937
8938 if ((o->size % bed->s->sizeof_rela) == 0)
8939 {
8940 if ((o->size % bed->s->sizeof_rel) == 0)
8941 /* Section size is divisible by both rel and rela sizes.
8942 It is of no help to us. */
8943 ;
8944 else
8945 {
8946 /* Section size is only divisible by rela. */
8947 if (use_rela_initialised && !use_rela)
8948 {
8949 _bfd_error_handler (_("%B: Unable to sort relocs - "
8950 "they are in more than one size"),
8951 abfd);
8952 bfd_set_error (bfd_error_invalid_operation);
8953 return 0;
8954 }
8955 else
8956 {
8957 use_rela = TRUE;
8958 use_rela_initialised = TRUE;
8959 }
8960 }
8961 }
8962 else if ((o->size % bed->s->sizeof_rel) == 0)
8963 {
8964 /* Section size is only divisible by rel. */
8965 if (use_rela_initialised && use_rela)
8966 {
8967 _bfd_error_handler (_("%B: Unable to sort relocs - "
8968 "they are in more than one size"),
8969 abfd);
8970 bfd_set_error (bfd_error_invalid_operation);
8971 return 0;
8972 }
8973 else
8974 {
8975 use_rela = FALSE;
8976 use_rela_initialised = TRUE;
8977 }
8978 }
8979 else
8980 {
8981 /* The section size is not divisible by either -
8982 something is wrong. */
8983 _bfd_error_handler (_("%B: Unable to sort relocs - "
8984 "they are of an unknown size"), abfd);
8985 bfd_set_error (bfd_error_invalid_operation);
8986 return 0;
8987 }
8988 }
8989
8990 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8991 if (lo->type == bfd_indirect_link_order)
8992 {
8993 asection *o = lo->u.indirect.section;
8994
8995 if ((o->size % bed->s->sizeof_rela) == 0)
8996 {
8997 if ((o->size % bed->s->sizeof_rel) == 0)
8998 /* Section size is divisible by both rel and rela sizes.
8999 It is of no help to us. */
9000 ;
9001 else
9002 {
9003 /* Section size is only divisible by rela. */
9004 if (use_rela_initialised && !use_rela)
9005 {
9006 _bfd_error_handler (_("%B: Unable to sort relocs - "
9007 "they are in more than one size"),
9008 abfd);
9009 bfd_set_error (bfd_error_invalid_operation);
9010 return 0;
9011 }
9012 else
9013 {
9014 use_rela = TRUE;
9015 use_rela_initialised = TRUE;
9016 }
9017 }
9018 }
9019 else if ((o->size % bed->s->sizeof_rel) == 0)
9020 {
9021 /* Section size is only divisible by rel. */
9022 if (use_rela_initialised && use_rela)
9023 {
9024 _bfd_error_handler (_("%B: Unable to sort relocs - "
9025 "they are in more than one size"),
9026 abfd);
9027 bfd_set_error (bfd_error_invalid_operation);
9028 return 0;
9029 }
9030 else
9031 {
9032 use_rela = FALSE;
9033 use_rela_initialised = TRUE;
9034 }
9035 }
9036 else
9037 {
9038 /* The section size is not divisible by either -
9039 something is wrong. */
9040 _bfd_error_handler (_("%B: Unable to sort relocs - "
9041 "they are of an unknown size"), abfd);
9042 bfd_set_error (bfd_error_invalid_operation);
9043 return 0;
9044 }
9045 }
9046
9047 if (! use_rela_initialised)
9048 /* Make a guess. */
9049 use_rela = TRUE;
9050 }
9051 else if (rela_dyn != NULL && rela_dyn->size > 0)
9052 use_rela = TRUE;
9053 else if (rel_dyn != NULL && rel_dyn->size > 0)
9054 use_rela = FALSE;
9055 else
9056 return 0;
9057
9058 if (use_rela)
9059 {
9060 dynamic_relocs = rela_dyn;
9061 ext_size = bed->s->sizeof_rela;
9062 swap_in = bed->s->swap_reloca_in;
9063 swap_out = bed->s->swap_reloca_out;
9064 }
9065 else
9066 {
9067 dynamic_relocs = rel_dyn;
9068 ext_size = bed->s->sizeof_rel;
9069 swap_in = bed->s->swap_reloc_in;
9070 swap_out = bed->s->swap_reloc_out;
9071 }
9072
9073 size = 0;
9074 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9075 if (lo->type == bfd_indirect_link_order)
9076 size += lo->u.indirect.section->size;
9077
9078 if (size != dynamic_relocs->size)
9079 return 0;
9080
9081 sort_elt = (sizeof (struct elf_link_sort_rela)
9082 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9083
9084 count = dynamic_relocs->size / ext_size;
9085 if (count == 0)
9086 return 0;
9087 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9088
9089 if (sort == NULL)
9090 {
9091 (*info->callbacks->warning)
9092 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
9093 return 0;
9094 }
9095
9096 if (bed->s->arch_size == 32)
9097 r_sym_mask = ~(bfd_vma) 0xff;
9098 else
9099 r_sym_mask = ~(bfd_vma) 0xffffffff;
9100
9101 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9102 if (lo->type == bfd_indirect_link_order)
9103 {
9104 bfd_byte *erel, *erelend;
9105 asection *o = lo->u.indirect.section;
9106
9107 if (o->contents == NULL && o->size != 0)
9108 {
9109 /* This is a reloc section that is being handled as a normal
9110 section. See bfd_section_from_shdr. We can't combine
9111 relocs in this case. */
9112 free (sort);
9113 return 0;
9114 }
9115 erel = o->contents;
9116 erelend = o->contents + o->size;
9117 p = sort + o->output_offset * opb / ext_size * sort_elt;
9118
9119 while (erel < erelend)
9120 {
9121 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9122
9123 (*swap_in) (abfd, erel, s->rela);
9124 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9125 s->u.sym_mask = r_sym_mask;
9126 p += sort_elt;
9127 erel += ext_size;
9128 }
9129 }
9130
9131 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9132
9133 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9134 {
9135 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9136 if (s->type != reloc_class_relative)
9137 break;
9138 }
9139 ret = i;
9140 s_non_relative = p;
9141
9142 sq = (struct elf_link_sort_rela *) s_non_relative;
9143 for (; i < count; i++, p += sort_elt)
9144 {
9145 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9146 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9147 sq = sp;
9148 sp->u.offset = sq->rela->r_offset;
9149 }
9150
9151 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9152
9153 struct elf_link_hash_table *htab = elf_hash_table (info);
9154 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9155 {
9156 /* We have plt relocs in .rela.dyn. */
9157 sq = (struct elf_link_sort_rela *) sort;
9158 for (i = 0; i < count; i++)
9159 if (sq[count - i - 1].type != reloc_class_plt)
9160 break;
9161 if (i != 0 && htab->srelplt->size == i * ext_size)
9162 {
9163 struct bfd_link_order **plo;
9164 /* Put srelplt link_order last. This is so the output_offset
9165 set in the next loop is correct for DT_JMPREL. */
9166 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9167 if ((*plo)->type == bfd_indirect_link_order
9168 && (*plo)->u.indirect.section == htab->srelplt)
9169 {
9170 lo = *plo;
9171 *plo = lo->next;
9172 }
9173 else
9174 plo = &(*plo)->next;
9175 *plo = lo;
9176 lo->next = NULL;
9177 dynamic_relocs->map_tail.link_order = lo;
9178 }
9179 }
9180
9181 p = sort;
9182 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9183 if (lo->type == bfd_indirect_link_order)
9184 {
9185 bfd_byte *erel, *erelend;
9186 asection *o = lo->u.indirect.section;
9187
9188 erel = o->contents;
9189 erelend = o->contents + o->size;
9190 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9191 while (erel < erelend)
9192 {
9193 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9194 (*swap_out) (abfd, s->rela, erel);
9195 p += sort_elt;
9196 erel += ext_size;
9197 }
9198 }
9199
9200 free (sort);
9201 *psec = dynamic_relocs;
9202 return ret;
9203 }
9204
9205 /* Add a symbol to the output symbol string table. */
9206
9207 static int
9208 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9209 const char *name,
9210 Elf_Internal_Sym *elfsym,
9211 asection *input_sec,
9212 struct elf_link_hash_entry *h)
9213 {
9214 int (*output_symbol_hook)
9215 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9216 struct elf_link_hash_entry *);
9217 struct elf_link_hash_table *hash_table;
9218 const struct elf_backend_data *bed;
9219 bfd_size_type strtabsize;
9220
9221 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9222
9223 bed = get_elf_backend_data (flinfo->output_bfd);
9224 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9225 if (output_symbol_hook != NULL)
9226 {
9227 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9228 if (ret != 1)
9229 return ret;
9230 }
9231
9232 if (name == NULL
9233 || *name == '\0'
9234 || (input_sec->flags & SEC_EXCLUDE))
9235 elfsym->st_name = (unsigned long) -1;
9236 else
9237 {
9238 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9239 to get the final offset for st_name. */
9240 elfsym->st_name
9241 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9242 name, FALSE);
9243 if (elfsym->st_name == (unsigned long) -1)
9244 return 0;
9245 }
9246
9247 hash_table = elf_hash_table (flinfo->info);
9248 strtabsize = hash_table->strtabsize;
9249 if (strtabsize <= hash_table->strtabcount)
9250 {
9251 strtabsize += strtabsize;
9252 hash_table->strtabsize = strtabsize;
9253 strtabsize *= sizeof (*hash_table->strtab);
9254 hash_table->strtab
9255 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9256 strtabsize);
9257 if (hash_table->strtab == NULL)
9258 return 0;
9259 }
9260 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9261 hash_table->strtab[hash_table->strtabcount].dest_index
9262 = hash_table->strtabcount;
9263 hash_table->strtab[hash_table->strtabcount].destshndx_index
9264 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9265
9266 bfd_get_symcount (flinfo->output_bfd) += 1;
9267 hash_table->strtabcount += 1;
9268
9269 return 1;
9270 }
9271
9272 /* Swap symbols out to the symbol table and flush the output symbols to
9273 the file. */
9274
9275 static bfd_boolean
9276 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9277 {
9278 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9279 bfd_size_type amt;
9280 size_t i;
9281 const struct elf_backend_data *bed;
9282 bfd_byte *symbuf;
9283 Elf_Internal_Shdr *hdr;
9284 file_ptr pos;
9285 bfd_boolean ret;
9286
9287 if (!hash_table->strtabcount)
9288 return TRUE;
9289
9290 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9291
9292 bed = get_elf_backend_data (flinfo->output_bfd);
9293
9294 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9295 symbuf = (bfd_byte *) bfd_malloc (amt);
9296 if (symbuf == NULL)
9297 return FALSE;
9298
9299 if (flinfo->symshndxbuf)
9300 {
9301 amt = sizeof (Elf_External_Sym_Shndx);
9302 amt *= bfd_get_symcount (flinfo->output_bfd);
9303 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9304 if (flinfo->symshndxbuf == NULL)
9305 {
9306 free (symbuf);
9307 return FALSE;
9308 }
9309 }
9310
9311 for (i = 0; i < hash_table->strtabcount; i++)
9312 {
9313 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9314 if (elfsym->sym.st_name == (unsigned long) -1)
9315 elfsym->sym.st_name = 0;
9316 else
9317 elfsym->sym.st_name
9318 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9319 elfsym->sym.st_name);
9320 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9321 ((bfd_byte *) symbuf
9322 + (elfsym->dest_index
9323 * bed->s->sizeof_sym)),
9324 (flinfo->symshndxbuf
9325 + elfsym->destshndx_index));
9326 }
9327
9328 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9329 pos = hdr->sh_offset + hdr->sh_size;
9330 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9331 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9332 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9333 {
9334 hdr->sh_size += amt;
9335 ret = TRUE;
9336 }
9337 else
9338 ret = FALSE;
9339
9340 free (symbuf);
9341
9342 free (hash_table->strtab);
9343 hash_table->strtab = NULL;
9344
9345 return ret;
9346 }
9347
9348 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9349
9350 static bfd_boolean
9351 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9352 {
9353 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9354 && sym->st_shndx < SHN_LORESERVE)
9355 {
9356 /* The gABI doesn't support dynamic symbols in output sections
9357 beyond 64k. */
9358 _bfd_error_handler
9359 /* xgettext:c-format */
9360 (_("%B: Too many sections: %d (>= %d)"),
9361 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9362 bfd_set_error (bfd_error_nonrepresentable_section);
9363 return FALSE;
9364 }
9365 return TRUE;
9366 }
9367
9368 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9369 allowing an unsatisfied unversioned symbol in the DSO to match a
9370 versioned symbol that would normally require an explicit version.
9371 We also handle the case that a DSO references a hidden symbol
9372 which may be satisfied by a versioned symbol in another DSO. */
9373
9374 static bfd_boolean
9375 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9376 const struct elf_backend_data *bed,
9377 struct elf_link_hash_entry *h)
9378 {
9379 bfd *abfd;
9380 struct elf_link_loaded_list *loaded;
9381
9382 if (!is_elf_hash_table (info->hash))
9383 return FALSE;
9384
9385 /* Check indirect symbol. */
9386 while (h->root.type == bfd_link_hash_indirect)
9387 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9388
9389 switch (h->root.type)
9390 {
9391 default:
9392 abfd = NULL;
9393 break;
9394
9395 case bfd_link_hash_undefined:
9396 case bfd_link_hash_undefweak:
9397 abfd = h->root.u.undef.abfd;
9398 if (abfd == NULL
9399 || (abfd->flags & DYNAMIC) == 0
9400 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9401 return FALSE;
9402 break;
9403
9404 case bfd_link_hash_defined:
9405 case bfd_link_hash_defweak:
9406 abfd = h->root.u.def.section->owner;
9407 break;
9408
9409 case bfd_link_hash_common:
9410 abfd = h->root.u.c.p->section->owner;
9411 break;
9412 }
9413 BFD_ASSERT (abfd != NULL);
9414
9415 for (loaded = elf_hash_table (info)->loaded;
9416 loaded != NULL;
9417 loaded = loaded->next)
9418 {
9419 bfd *input;
9420 Elf_Internal_Shdr *hdr;
9421 size_t symcount;
9422 size_t extsymcount;
9423 size_t extsymoff;
9424 Elf_Internal_Shdr *versymhdr;
9425 Elf_Internal_Sym *isym;
9426 Elf_Internal_Sym *isymend;
9427 Elf_Internal_Sym *isymbuf;
9428 Elf_External_Versym *ever;
9429 Elf_External_Versym *extversym;
9430
9431 input = loaded->abfd;
9432
9433 /* We check each DSO for a possible hidden versioned definition. */
9434 if (input == abfd
9435 || (input->flags & DYNAMIC) == 0
9436 || elf_dynversym (input) == 0)
9437 continue;
9438
9439 hdr = &elf_tdata (input)->dynsymtab_hdr;
9440
9441 symcount = hdr->sh_size / bed->s->sizeof_sym;
9442 if (elf_bad_symtab (input))
9443 {
9444 extsymcount = symcount;
9445 extsymoff = 0;
9446 }
9447 else
9448 {
9449 extsymcount = symcount - hdr->sh_info;
9450 extsymoff = hdr->sh_info;
9451 }
9452
9453 if (extsymcount == 0)
9454 continue;
9455
9456 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9457 NULL, NULL, NULL);
9458 if (isymbuf == NULL)
9459 return FALSE;
9460
9461 /* Read in any version definitions. */
9462 versymhdr = &elf_tdata (input)->dynversym_hdr;
9463 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
9464 if (extversym == NULL)
9465 goto error_ret;
9466
9467 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9468 || (bfd_bread (extversym, versymhdr->sh_size, input)
9469 != versymhdr->sh_size))
9470 {
9471 free (extversym);
9472 error_ret:
9473 free (isymbuf);
9474 return FALSE;
9475 }
9476
9477 ever = extversym + extsymoff;
9478 isymend = isymbuf + extsymcount;
9479 for (isym = isymbuf; isym < isymend; isym++, ever++)
9480 {
9481 const char *name;
9482 Elf_Internal_Versym iver;
9483 unsigned short version_index;
9484
9485 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9486 || isym->st_shndx == SHN_UNDEF)
9487 continue;
9488
9489 name = bfd_elf_string_from_elf_section (input,
9490 hdr->sh_link,
9491 isym->st_name);
9492 if (strcmp (name, h->root.root.string) != 0)
9493 continue;
9494
9495 _bfd_elf_swap_versym_in (input, ever, &iver);
9496
9497 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9498 && !(h->def_regular
9499 && h->forced_local))
9500 {
9501 /* If we have a non-hidden versioned sym, then it should
9502 have provided a definition for the undefined sym unless
9503 it is defined in a non-shared object and forced local.
9504 */
9505 abort ();
9506 }
9507
9508 version_index = iver.vs_vers & VERSYM_VERSION;
9509 if (version_index == 1 || version_index == 2)
9510 {
9511 /* This is the base or first version. We can use it. */
9512 free (extversym);
9513 free (isymbuf);
9514 return TRUE;
9515 }
9516 }
9517
9518 free (extversym);
9519 free (isymbuf);
9520 }
9521
9522 return FALSE;
9523 }
9524
9525 /* Convert ELF common symbol TYPE. */
9526
9527 static int
9528 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9529 {
9530 /* Commom symbol can only appear in relocatable link. */
9531 if (!bfd_link_relocatable (info))
9532 abort ();
9533 switch (info->elf_stt_common)
9534 {
9535 case unchanged:
9536 break;
9537 case elf_stt_common:
9538 type = STT_COMMON;
9539 break;
9540 case no_elf_stt_common:
9541 type = STT_OBJECT;
9542 break;
9543 }
9544 return type;
9545 }
9546
9547 /* Add an external symbol to the symbol table. This is called from
9548 the hash table traversal routine. When generating a shared object,
9549 we go through the symbol table twice. The first time we output
9550 anything that might have been forced to local scope in a version
9551 script. The second time we output the symbols that are still
9552 global symbols. */
9553
9554 static bfd_boolean
9555 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9556 {
9557 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9558 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9559 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9560 bfd_boolean strip;
9561 Elf_Internal_Sym sym;
9562 asection *input_sec;
9563 const struct elf_backend_data *bed;
9564 long indx;
9565 int ret;
9566 unsigned int type;
9567
9568 if (h->root.type == bfd_link_hash_warning)
9569 {
9570 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9571 if (h->root.type == bfd_link_hash_new)
9572 return TRUE;
9573 }
9574
9575 /* Decide whether to output this symbol in this pass. */
9576 if (eoinfo->localsyms)
9577 {
9578 if (!h->forced_local)
9579 return TRUE;
9580 }
9581 else
9582 {
9583 if (h->forced_local)
9584 return TRUE;
9585 }
9586
9587 bed = get_elf_backend_data (flinfo->output_bfd);
9588
9589 if (h->root.type == bfd_link_hash_undefined)
9590 {
9591 /* If we have an undefined symbol reference here then it must have
9592 come from a shared library that is being linked in. (Undefined
9593 references in regular files have already been handled unless
9594 they are in unreferenced sections which are removed by garbage
9595 collection). */
9596 bfd_boolean ignore_undef = FALSE;
9597
9598 /* Some symbols may be special in that the fact that they're
9599 undefined can be safely ignored - let backend determine that. */
9600 if (bed->elf_backend_ignore_undef_symbol)
9601 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9602
9603 /* If we are reporting errors for this situation then do so now. */
9604 if (!ignore_undef
9605 && h->ref_dynamic
9606 && (!h->ref_regular || flinfo->info->gc_sections)
9607 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9608 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9609 (*flinfo->info->callbacks->undefined_symbol)
9610 (flinfo->info, h->root.root.string,
9611 h->ref_regular ? NULL : h->root.u.undef.abfd,
9612 NULL, 0,
9613 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
9614
9615 /* Strip a global symbol defined in a discarded section. */
9616 if (h->indx == -3)
9617 return TRUE;
9618 }
9619
9620 /* We should also warn if a forced local symbol is referenced from
9621 shared libraries. */
9622 if (bfd_link_executable (flinfo->info)
9623 && h->forced_local
9624 && h->ref_dynamic
9625 && h->def_regular
9626 && !h->dynamic_def
9627 && h->ref_dynamic_nonweak
9628 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9629 {
9630 bfd *def_bfd;
9631 const char *msg;
9632 struct elf_link_hash_entry *hi = h;
9633
9634 /* Check indirect symbol. */
9635 while (hi->root.type == bfd_link_hash_indirect)
9636 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9637
9638 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9639 /* xgettext:c-format */
9640 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9641 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9642 /* xgettext:c-format */
9643 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9644 else
9645 /* xgettext:c-format */
9646 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
9647 def_bfd = flinfo->output_bfd;
9648 if (hi->root.u.def.section != bfd_abs_section_ptr)
9649 def_bfd = hi->root.u.def.section->owner;
9650 _bfd_error_handler (msg, flinfo->output_bfd,
9651 h->root.root.string, def_bfd);
9652 bfd_set_error (bfd_error_bad_value);
9653 eoinfo->failed = TRUE;
9654 return FALSE;
9655 }
9656
9657 /* We don't want to output symbols that have never been mentioned by
9658 a regular file, or that we have been told to strip. However, if
9659 h->indx is set to -2, the symbol is used by a reloc and we must
9660 output it. */
9661 strip = FALSE;
9662 if (h->indx == -2)
9663 ;
9664 else if ((h->def_dynamic
9665 || h->ref_dynamic
9666 || h->root.type == bfd_link_hash_new)
9667 && !h->def_regular
9668 && !h->ref_regular)
9669 strip = TRUE;
9670 else if (flinfo->info->strip == strip_all)
9671 strip = TRUE;
9672 else if (flinfo->info->strip == strip_some
9673 && bfd_hash_lookup (flinfo->info->keep_hash,
9674 h->root.root.string, FALSE, FALSE) == NULL)
9675 strip = TRUE;
9676 else if ((h->root.type == bfd_link_hash_defined
9677 || h->root.type == bfd_link_hash_defweak)
9678 && ((flinfo->info->strip_discarded
9679 && discarded_section (h->root.u.def.section))
9680 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9681 && h->root.u.def.section->owner != NULL
9682 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9683 strip = TRUE;
9684 else if ((h->root.type == bfd_link_hash_undefined
9685 || h->root.type == bfd_link_hash_undefweak)
9686 && h->root.u.undef.abfd != NULL
9687 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9688 strip = TRUE;
9689
9690 type = h->type;
9691
9692 /* If we're stripping it, and it's not a dynamic symbol, there's
9693 nothing else to do. However, if it is a forced local symbol or
9694 an ifunc symbol we need to give the backend finish_dynamic_symbol
9695 function a chance to make it dynamic. */
9696 if (strip
9697 && h->dynindx == -1
9698 && type != STT_GNU_IFUNC
9699 && !h->forced_local)
9700 return TRUE;
9701
9702 sym.st_value = 0;
9703 sym.st_size = h->size;
9704 sym.st_other = h->other;
9705 switch (h->root.type)
9706 {
9707 default:
9708 case bfd_link_hash_new:
9709 case bfd_link_hash_warning:
9710 abort ();
9711 return FALSE;
9712
9713 case bfd_link_hash_undefined:
9714 case bfd_link_hash_undefweak:
9715 input_sec = bfd_und_section_ptr;
9716 sym.st_shndx = SHN_UNDEF;
9717 break;
9718
9719 case bfd_link_hash_defined:
9720 case bfd_link_hash_defweak:
9721 {
9722 input_sec = h->root.u.def.section;
9723 if (input_sec->output_section != NULL)
9724 {
9725 sym.st_shndx =
9726 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9727 input_sec->output_section);
9728 if (sym.st_shndx == SHN_BAD)
9729 {
9730 _bfd_error_handler
9731 /* xgettext:c-format */
9732 (_("%B: could not find output section %A for input section %A"),
9733 flinfo->output_bfd, input_sec->output_section, input_sec);
9734 bfd_set_error (bfd_error_nonrepresentable_section);
9735 eoinfo->failed = TRUE;
9736 return FALSE;
9737 }
9738
9739 /* ELF symbols in relocatable files are section relative,
9740 but in nonrelocatable files they are virtual
9741 addresses. */
9742 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9743 if (!bfd_link_relocatable (flinfo->info))
9744 {
9745 sym.st_value += input_sec->output_section->vma;
9746 if (h->type == STT_TLS)
9747 {
9748 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9749 if (tls_sec != NULL)
9750 sym.st_value -= tls_sec->vma;
9751 }
9752 }
9753 }
9754 else
9755 {
9756 BFD_ASSERT (input_sec->owner == NULL
9757 || (input_sec->owner->flags & DYNAMIC) != 0);
9758 sym.st_shndx = SHN_UNDEF;
9759 input_sec = bfd_und_section_ptr;
9760 }
9761 }
9762 break;
9763
9764 case bfd_link_hash_common:
9765 input_sec = h->root.u.c.p->section;
9766 sym.st_shndx = bed->common_section_index (input_sec);
9767 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9768 break;
9769
9770 case bfd_link_hash_indirect:
9771 /* These symbols are created by symbol versioning. They point
9772 to the decorated version of the name. For example, if the
9773 symbol foo@@GNU_1.2 is the default, which should be used when
9774 foo is used with no version, then we add an indirect symbol
9775 foo which points to foo@@GNU_1.2. We ignore these symbols,
9776 since the indirected symbol is already in the hash table. */
9777 return TRUE;
9778 }
9779
9780 if (type == STT_COMMON || type == STT_OBJECT)
9781 switch (h->root.type)
9782 {
9783 case bfd_link_hash_common:
9784 type = elf_link_convert_common_type (flinfo->info, type);
9785 break;
9786 case bfd_link_hash_defined:
9787 case bfd_link_hash_defweak:
9788 if (bed->common_definition (&sym))
9789 type = elf_link_convert_common_type (flinfo->info, type);
9790 else
9791 type = STT_OBJECT;
9792 break;
9793 case bfd_link_hash_undefined:
9794 case bfd_link_hash_undefweak:
9795 break;
9796 default:
9797 abort ();
9798 }
9799
9800 if (h->forced_local)
9801 {
9802 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9803 /* Turn off visibility on local symbol. */
9804 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9805 }
9806 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9807 else if (h->unique_global && h->def_regular)
9808 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9809 else if (h->root.type == bfd_link_hash_undefweak
9810 || h->root.type == bfd_link_hash_defweak)
9811 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9812 else
9813 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9814 sym.st_target_internal = h->target_internal;
9815
9816 /* Give the processor backend a chance to tweak the symbol value,
9817 and also to finish up anything that needs to be done for this
9818 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9819 forced local syms when non-shared is due to a historical quirk.
9820 STT_GNU_IFUNC symbol must go through PLT. */
9821 if ((h->type == STT_GNU_IFUNC
9822 && h->def_regular
9823 && !bfd_link_relocatable (flinfo->info))
9824 || ((h->dynindx != -1
9825 || h->forced_local)
9826 && ((bfd_link_pic (flinfo->info)
9827 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9828 || h->root.type != bfd_link_hash_undefweak))
9829 || !h->forced_local)
9830 && elf_hash_table (flinfo->info)->dynamic_sections_created))
9831 {
9832 if (! ((*bed->elf_backend_finish_dynamic_symbol)
9833 (flinfo->output_bfd, flinfo->info, h, &sym)))
9834 {
9835 eoinfo->failed = TRUE;
9836 return FALSE;
9837 }
9838 }
9839
9840 /* If we are marking the symbol as undefined, and there are no
9841 non-weak references to this symbol from a regular object, then
9842 mark the symbol as weak undefined; if there are non-weak
9843 references, mark the symbol as strong. We can't do this earlier,
9844 because it might not be marked as undefined until the
9845 finish_dynamic_symbol routine gets through with it. */
9846 if (sym.st_shndx == SHN_UNDEF
9847 && h->ref_regular
9848 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9849 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9850 {
9851 int bindtype;
9852 type = ELF_ST_TYPE (sym.st_info);
9853
9854 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9855 if (type == STT_GNU_IFUNC)
9856 type = STT_FUNC;
9857
9858 if (h->ref_regular_nonweak)
9859 bindtype = STB_GLOBAL;
9860 else
9861 bindtype = STB_WEAK;
9862 sym.st_info = ELF_ST_INFO (bindtype, type);
9863 }
9864
9865 /* If this is a symbol defined in a dynamic library, don't use the
9866 symbol size from the dynamic library. Relinking an executable
9867 against a new library may introduce gratuitous changes in the
9868 executable's symbols if we keep the size. */
9869 if (sym.st_shndx == SHN_UNDEF
9870 && !h->def_regular
9871 && h->def_dynamic)
9872 sym.st_size = 0;
9873
9874 /* If a non-weak symbol with non-default visibility is not defined
9875 locally, it is a fatal error. */
9876 if (!bfd_link_relocatable (flinfo->info)
9877 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9878 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9879 && h->root.type == bfd_link_hash_undefined
9880 && !h->def_regular)
9881 {
9882 const char *msg;
9883
9884 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9885 /* xgettext:c-format */
9886 msg = _("%B: protected symbol `%s' isn't defined");
9887 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9888 /* xgettext:c-format */
9889 msg = _("%B: internal symbol `%s' isn't defined");
9890 else
9891 /* xgettext:c-format */
9892 msg = _("%B: hidden symbol `%s' isn't defined");
9893 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
9894 bfd_set_error (bfd_error_bad_value);
9895 eoinfo->failed = TRUE;
9896 return FALSE;
9897 }
9898
9899 /* If this symbol should be put in the .dynsym section, then put it
9900 there now. We already know the symbol index. We also fill in
9901 the entry in the .hash section. */
9902 if (elf_hash_table (flinfo->info)->dynsym != NULL
9903 && h->dynindx != -1
9904 && elf_hash_table (flinfo->info)->dynamic_sections_created)
9905 {
9906 bfd_byte *esym;
9907
9908 /* Since there is no version information in the dynamic string,
9909 if there is no version info in symbol version section, we will
9910 have a run-time problem if not linking executable, referenced
9911 by shared library, or not bound locally. */
9912 if (h->verinfo.verdef == NULL
9913 && (!bfd_link_executable (flinfo->info)
9914 || h->ref_dynamic
9915 || !h->def_regular))
9916 {
9917 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9918
9919 if (p && p [1] != '\0')
9920 {
9921 _bfd_error_handler
9922 /* xgettext:c-format */
9923 (_("%B: No symbol version section for versioned symbol `%s'"),
9924 flinfo->output_bfd, h->root.root.string);
9925 eoinfo->failed = TRUE;
9926 return FALSE;
9927 }
9928 }
9929
9930 sym.st_name = h->dynstr_index;
9931 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9932 + h->dynindx * bed->s->sizeof_sym);
9933 if (!check_dynsym (flinfo->output_bfd, &sym))
9934 {
9935 eoinfo->failed = TRUE;
9936 return FALSE;
9937 }
9938 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
9939
9940 if (flinfo->hash_sec != NULL)
9941 {
9942 size_t hash_entry_size;
9943 bfd_byte *bucketpos;
9944 bfd_vma chain;
9945 size_t bucketcount;
9946 size_t bucket;
9947
9948 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
9949 bucket = h->u.elf_hash_value % bucketcount;
9950
9951 hash_entry_size
9952 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9953 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
9954 + (bucket + 2) * hash_entry_size);
9955 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9956 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9957 bucketpos);
9958 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9959 ((bfd_byte *) flinfo->hash_sec->contents
9960 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9961 }
9962
9963 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
9964 {
9965 Elf_Internal_Versym iversym;
9966 Elf_External_Versym *eversym;
9967
9968 if (!h->def_regular)
9969 {
9970 if (h->verinfo.verdef == NULL
9971 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9972 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
9973 iversym.vs_vers = 0;
9974 else
9975 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9976 }
9977 else
9978 {
9979 if (h->verinfo.vertree == NULL)
9980 iversym.vs_vers = 1;
9981 else
9982 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
9983 if (flinfo->info->create_default_symver)
9984 iversym.vs_vers++;
9985 }
9986
9987 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9988 defined locally. */
9989 if (h->versioned == versioned_hidden && h->def_regular)
9990 iversym.vs_vers |= VERSYM_HIDDEN;
9991
9992 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
9993 eversym += h->dynindx;
9994 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
9995 }
9996 }
9997
9998 /* If the symbol is undefined, and we didn't output it to .dynsym,
9999 strip it from .symtab too. Obviously we can't do this for
10000 relocatable output or when needed for --emit-relocs. */
10001 else if (input_sec == bfd_und_section_ptr
10002 && h->indx != -2
10003 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10004 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10005 && !bfd_link_relocatable (flinfo->info))
10006 return TRUE;
10007
10008 /* Also strip others that we couldn't earlier due to dynamic symbol
10009 processing. */
10010 if (strip)
10011 return TRUE;
10012 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10013 return TRUE;
10014
10015 /* Output a FILE symbol so that following locals are not associated
10016 with the wrong input file. We need one for forced local symbols
10017 if we've seen more than one FILE symbol or when we have exactly
10018 one FILE symbol but global symbols are present in a file other
10019 than the one with the FILE symbol. We also need one if linker
10020 defined symbols are present. In practice these conditions are
10021 always met, so just emit the FILE symbol unconditionally. */
10022 if (eoinfo->localsyms
10023 && !eoinfo->file_sym_done
10024 && eoinfo->flinfo->filesym_count != 0)
10025 {
10026 Elf_Internal_Sym fsym;
10027
10028 memset (&fsym, 0, sizeof (fsym));
10029 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10030 fsym.st_shndx = SHN_ABS;
10031 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10032 bfd_und_section_ptr, NULL))
10033 return FALSE;
10034
10035 eoinfo->file_sym_done = TRUE;
10036 }
10037
10038 indx = bfd_get_symcount (flinfo->output_bfd);
10039 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10040 input_sec, h);
10041 if (ret == 0)
10042 {
10043 eoinfo->failed = TRUE;
10044 return FALSE;
10045 }
10046 else if (ret == 1)
10047 h->indx = indx;
10048 else if (h->indx == -2)
10049 abort();
10050
10051 return TRUE;
10052 }
10053
10054 /* Return TRUE if special handling is done for relocs in SEC against
10055 symbols defined in discarded sections. */
10056
10057 static bfd_boolean
10058 elf_section_ignore_discarded_relocs (asection *sec)
10059 {
10060 const struct elf_backend_data *bed;
10061
10062 switch (sec->sec_info_type)
10063 {
10064 case SEC_INFO_TYPE_STABS:
10065 case SEC_INFO_TYPE_EH_FRAME:
10066 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10067 return TRUE;
10068 default:
10069 break;
10070 }
10071
10072 bed = get_elf_backend_data (sec->owner);
10073 if (bed->elf_backend_ignore_discarded_relocs != NULL
10074 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10075 return TRUE;
10076
10077 return FALSE;
10078 }
10079
10080 /* Return a mask saying how ld should treat relocations in SEC against
10081 symbols defined in discarded sections. If this function returns
10082 COMPLAIN set, ld will issue a warning message. If this function
10083 returns PRETEND set, and the discarded section was link-once and the
10084 same size as the kept link-once section, ld will pretend that the
10085 symbol was actually defined in the kept section. Otherwise ld will
10086 zero the reloc (at least that is the intent, but some cooperation by
10087 the target dependent code is needed, particularly for REL targets). */
10088
10089 unsigned int
10090 _bfd_elf_default_action_discarded (asection *sec)
10091 {
10092 if (sec->flags & SEC_DEBUGGING)
10093 return PRETEND;
10094
10095 if (strcmp (".eh_frame", sec->name) == 0)
10096 return 0;
10097
10098 if (strcmp (".gcc_except_table", sec->name) == 0)
10099 return 0;
10100
10101 return COMPLAIN | PRETEND;
10102 }
10103
10104 /* Find a match between a section and a member of a section group. */
10105
10106 static asection *
10107 match_group_member (asection *sec, asection *group,
10108 struct bfd_link_info *info)
10109 {
10110 asection *first = elf_next_in_group (group);
10111 asection *s = first;
10112
10113 while (s != NULL)
10114 {
10115 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10116 return s;
10117
10118 s = elf_next_in_group (s);
10119 if (s == first)
10120 break;
10121 }
10122
10123 return NULL;
10124 }
10125
10126 /* Check if the kept section of a discarded section SEC can be used
10127 to replace it. Return the replacement if it is OK. Otherwise return
10128 NULL. */
10129
10130 asection *
10131 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10132 {
10133 asection *kept;
10134
10135 kept = sec->kept_section;
10136 if (kept != NULL)
10137 {
10138 if ((kept->flags & SEC_GROUP) != 0)
10139 kept = match_group_member (sec, kept, info);
10140 if (kept != NULL
10141 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10142 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
10143 kept = NULL;
10144 sec->kept_section = kept;
10145 }
10146 return kept;
10147 }
10148
10149 /* Link an input file into the linker output file. This function
10150 handles all the sections and relocations of the input file at once.
10151 This is so that we only have to read the local symbols once, and
10152 don't have to keep them in memory. */
10153
10154 static bfd_boolean
10155 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10156 {
10157 int (*relocate_section)
10158 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10159 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10160 bfd *output_bfd;
10161 Elf_Internal_Shdr *symtab_hdr;
10162 size_t locsymcount;
10163 size_t extsymoff;
10164 Elf_Internal_Sym *isymbuf;
10165 Elf_Internal_Sym *isym;
10166 Elf_Internal_Sym *isymend;
10167 long *pindex;
10168 asection **ppsection;
10169 asection *o;
10170 const struct elf_backend_data *bed;
10171 struct elf_link_hash_entry **sym_hashes;
10172 bfd_size_type address_size;
10173 bfd_vma r_type_mask;
10174 int r_sym_shift;
10175 bfd_boolean have_file_sym = FALSE;
10176
10177 output_bfd = flinfo->output_bfd;
10178 bed = get_elf_backend_data (output_bfd);
10179 relocate_section = bed->elf_backend_relocate_section;
10180
10181 /* If this is a dynamic object, we don't want to do anything here:
10182 we don't want the local symbols, and we don't want the section
10183 contents. */
10184 if ((input_bfd->flags & DYNAMIC) != 0)
10185 return TRUE;
10186
10187 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10188 if (elf_bad_symtab (input_bfd))
10189 {
10190 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10191 extsymoff = 0;
10192 }
10193 else
10194 {
10195 locsymcount = symtab_hdr->sh_info;
10196 extsymoff = symtab_hdr->sh_info;
10197 }
10198
10199 /* Read the local symbols. */
10200 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10201 if (isymbuf == NULL && locsymcount != 0)
10202 {
10203 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10204 flinfo->internal_syms,
10205 flinfo->external_syms,
10206 flinfo->locsym_shndx);
10207 if (isymbuf == NULL)
10208 return FALSE;
10209 }
10210
10211 /* Find local symbol sections and adjust values of symbols in
10212 SEC_MERGE sections. Write out those local symbols we know are
10213 going into the output file. */
10214 isymend = isymbuf + locsymcount;
10215 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10216 isym < isymend;
10217 isym++, pindex++, ppsection++)
10218 {
10219 asection *isec;
10220 const char *name;
10221 Elf_Internal_Sym osym;
10222 long indx;
10223 int ret;
10224
10225 *pindex = -1;
10226
10227 if (elf_bad_symtab (input_bfd))
10228 {
10229 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10230 {
10231 *ppsection = NULL;
10232 continue;
10233 }
10234 }
10235
10236 if (isym->st_shndx == SHN_UNDEF)
10237 isec = bfd_und_section_ptr;
10238 else if (isym->st_shndx == SHN_ABS)
10239 isec = bfd_abs_section_ptr;
10240 else if (isym->st_shndx == SHN_COMMON)
10241 isec = bfd_com_section_ptr;
10242 else
10243 {
10244 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10245 if (isec == NULL)
10246 {
10247 /* Don't attempt to output symbols with st_shnx in the
10248 reserved range other than SHN_ABS and SHN_COMMON. */
10249 *ppsection = NULL;
10250 continue;
10251 }
10252 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10253 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10254 isym->st_value =
10255 _bfd_merged_section_offset (output_bfd, &isec,
10256 elf_section_data (isec)->sec_info,
10257 isym->st_value);
10258 }
10259
10260 *ppsection = isec;
10261
10262 /* Don't output the first, undefined, symbol. In fact, don't
10263 output any undefined local symbol. */
10264 if (isec == bfd_und_section_ptr)
10265 continue;
10266
10267 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10268 {
10269 /* We never output section symbols. Instead, we use the
10270 section symbol of the corresponding section in the output
10271 file. */
10272 continue;
10273 }
10274
10275 /* If we are stripping all symbols, we don't want to output this
10276 one. */
10277 if (flinfo->info->strip == strip_all)
10278 continue;
10279
10280 /* If we are discarding all local symbols, we don't want to
10281 output this one. If we are generating a relocatable output
10282 file, then some of the local symbols may be required by
10283 relocs; we output them below as we discover that they are
10284 needed. */
10285 if (flinfo->info->discard == discard_all)
10286 continue;
10287
10288 /* If this symbol is defined in a section which we are
10289 discarding, we don't need to keep it. */
10290 if (isym->st_shndx != SHN_UNDEF
10291 && isym->st_shndx < SHN_LORESERVE
10292 && bfd_section_removed_from_list (output_bfd,
10293 isec->output_section))
10294 continue;
10295
10296 /* Get the name of the symbol. */
10297 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10298 isym->st_name);
10299 if (name == NULL)
10300 return FALSE;
10301
10302 /* See if we are discarding symbols with this name. */
10303 if ((flinfo->info->strip == strip_some
10304 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
10305 == NULL))
10306 || (((flinfo->info->discard == discard_sec_merge
10307 && (isec->flags & SEC_MERGE)
10308 && !bfd_link_relocatable (flinfo->info))
10309 || flinfo->info->discard == discard_l)
10310 && bfd_is_local_label_name (input_bfd, name)))
10311 continue;
10312
10313 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10314 {
10315 if (input_bfd->lto_output)
10316 /* -flto puts a temp file name here. This means builds
10317 are not reproducible. Discard the symbol. */
10318 continue;
10319 have_file_sym = TRUE;
10320 flinfo->filesym_count += 1;
10321 }
10322 if (!have_file_sym)
10323 {
10324 /* In the absence of debug info, bfd_find_nearest_line uses
10325 FILE symbols to determine the source file for local
10326 function symbols. Provide a FILE symbol here if input
10327 files lack such, so that their symbols won't be
10328 associated with a previous input file. It's not the
10329 source file, but the best we can do. */
10330 have_file_sym = TRUE;
10331 flinfo->filesym_count += 1;
10332 memset (&osym, 0, sizeof (osym));
10333 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10334 osym.st_shndx = SHN_ABS;
10335 if (!elf_link_output_symstrtab (flinfo,
10336 (input_bfd->lto_output ? NULL
10337 : input_bfd->filename),
10338 &osym, bfd_abs_section_ptr,
10339 NULL))
10340 return FALSE;
10341 }
10342
10343 osym = *isym;
10344
10345 /* Adjust the section index for the output file. */
10346 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10347 isec->output_section);
10348 if (osym.st_shndx == SHN_BAD)
10349 return FALSE;
10350
10351 /* ELF symbols in relocatable files are section relative, but
10352 in executable files they are virtual addresses. Note that
10353 this code assumes that all ELF sections have an associated
10354 BFD section with a reasonable value for output_offset; below
10355 we assume that they also have a reasonable value for
10356 output_section. Any special sections must be set up to meet
10357 these requirements. */
10358 osym.st_value += isec->output_offset;
10359 if (!bfd_link_relocatable (flinfo->info))
10360 {
10361 osym.st_value += isec->output_section->vma;
10362 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10363 {
10364 /* STT_TLS symbols are relative to PT_TLS segment base. */
10365 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10366 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10367 }
10368 }
10369
10370 indx = bfd_get_symcount (output_bfd);
10371 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10372 if (ret == 0)
10373 return FALSE;
10374 else if (ret == 1)
10375 *pindex = indx;
10376 }
10377
10378 if (bed->s->arch_size == 32)
10379 {
10380 r_type_mask = 0xff;
10381 r_sym_shift = 8;
10382 address_size = 4;
10383 }
10384 else
10385 {
10386 r_type_mask = 0xffffffff;
10387 r_sym_shift = 32;
10388 address_size = 8;
10389 }
10390
10391 /* Relocate the contents of each section. */
10392 sym_hashes = elf_sym_hashes (input_bfd);
10393 for (o = input_bfd->sections; o != NULL; o = o->next)
10394 {
10395 bfd_byte *contents;
10396
10397 if (! o->linker_mark)
10398 {
10399 /* This section was omitted from the link. */
10400 continue;
10401 }
10402
10403 if (!flinfo->info->resolve_section_groups
10404 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10405 {
10406 /* Deal with the group signature symbol. */
10407 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10408 unsigned long symndx = sec_data->this_hdr.sh_info;
10409 asection *osec = o->output_section;
10410
10411 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
10412 if (symndx >= locsymcount
10413 || (elf_bad_symtab (input_bfd)
10414 && flinfo->sections[symndx] == NULL))
10415 {
10416 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10417 while (h->root.type == bfd_link_hash_indirect
10418 || h->root.type == bfd_link_hash_warning)
10419 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10420 /* Arrange for symbol to be output. */
10421 h->indx = -2;
10422 elf_section_data (osec)->this_hdr.sh_info = -2;
10423 }
10424 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10425 {
10426 /* We'll use the output section target_index. */
10427 asection *sec = flinfo->sections[symndx]->output_section;
10428 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10429 }
10430 else
10431 {
10432 if (flinfo->indices[symndx] == -1)
10433 {
10434 /* Otherwise output the local symbol now. */
10435 Elf_Internal_Sym sym = isymbuf[symndx];
10436 asection *sec = flinfo->sections[symndx]->output_section;
10437 const char *name;
10438 long indx;
10439 int ret;
10440
10441 name = bfd_elf_string_from_elf_section (input_bfd,
10442 symtab_hdr->sh_link,
10443 sym.st_name);
10444 if (name == NULL)
10445 return FALSE;
10446
10447 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10448 sec);
10449 if (sym.st_shndx == SHN_BAD)
10450 return FALSE;
10451
10452 sym.st_value += o->output_offset;
10453
10454 indx = bfd_get_symcount (output_bfd);
10455 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10456 NULL);
10457 if (ret == 0)
10458 return FALSE;
10459 else if (ret == 1)
10460 flinfo->indices[symndx] = indx;
10461 else
10462 abort ();
10463 }
10464 elf_section_data (osec)->this_hdr.sh_info
10465 = flinfo->indices[symndx];
10466 }
10467 }
10468
10469 if ((o->flags & SEC_HAS_CONTENTS) == 0
10470 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10471 continue;
10472
10473 if ((o->flags & SEC_LINKER_CREATED) != 0)
10474 {
10475 /* Section was created by _bfd_elf_link_create_dynamic_sections
10476 or somesuch. */
10477 continue;
10478 }
10479
10480 /* Get the contents of the section. They have been cached by a
10481 relaxation routine. Note that o is a section in an input
10482 file, so the contents field will not have been set by any of
10483 the routines which work on output files. */
10484 if (elf_section_data (o)->this_hdr.contents != NULL)
10485 {
10486 contents = elf_section_data (o)->this_hdr.contents;
10487 if (bed->caches_rawsize
10488 && o->rawsize != 0
10489 && o->rawsize < o->size)
10490 {
10491 memcpy (flinfo->contents, contents, o->rawsize);
10492 contents = flinfo->contents;
10493 }
10494 }
10495 else
10496 {
10497 contents = flinfo->contents;
10498 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10499 return FALSE;
10500 }
10501
10502 if ((o->flags & SEC_RELOC) != 0)
10503 {
10504 Elf_Internal_Rela *internal_relocs;
10505 Elf_Internal_Rela *rel, *relend;
10506 int action_discarded;
10507 int ret;
10508
10509 /* Get the swapped relocs. */
10510 internal_relocs
10511 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10512 flinfo->internal_relocs, FALSE);
10513 if (internal_relocs == NULL
10514 && o->reloc_count > 0)
10515 return FALSE;
10516
10517 /* We need to reverse-copy input .ctors/.dtors sections if
10518 they are placed in .init_array/.finit_array for output. */
10519 if (o->size > address_size
10520 && ((strncmp (o->name, ".ctors", 6) == 0
10521 && strcmp (o->output_section->name,
10522 ".init_array") == 0)
10523 || (strncmp (o->name, ".dtors", 6) == 0
10524 && strcmp (o->output_section->name,
10525 ".fini_array") == 0))
10526 && (o->name[6] == 0 || o->name[6] == '.'))
10527 {
10528 if (o->size * bed->s->int_rels_per_ext_rel
10529 != o->reloc_count * address_size)
10530 {
10531 _bfd_error_handler
10532 /* xgettext:c-format */
10533 (_("error: %B: size of section %A is not "
10534 "multiple of address size"),
10535 input_bfd, o);
10536 bfd_set_error (bfd_error_bad_value);
10537 return FALSE;
10538 }
10539 o->flags |= SEC_ELF_REVERSE_COPY;
10540 }
10541
10542 action_discarded = -1;
10543 if (!elf_section_ignore_discarded_relocs (o))
10544 action_discarded = (*bed->action_discarded) (o);
10545
10546 /* Run through the relocs evaluating complex reloc symbols and
10547 looking for relocs against symbols from discarded sections
10548 or section symbols from removed link-once sections.
10549 Complain about relocs against discarded sections. Zero
10550 relocs against removed link-once sections. */
10551
10552 rel = internal_relocs;
10553 relend = rel + o->reloc_count;
10554 for ( ; rel < relend; rel++)
10555 {
10556 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10557 unsigned int s_type;
10558 asection **ps, *sec;
10559 struct elf_link_hash_entry *h = NULL;
10560 const char *sym_name;
10561
10562 if (r_symndx == STN_UNDEF)
10563 continue;
10564
10565 if (r_symndx >= locsymcount
10566 || (elf_bad_symtab (input_bfd)
10567 && flinfo->sections[r_symndx] == NULL))
10568 {
10569 h = sym_hashes[r_symndx - extsymoff];
10570
10571 /* Badly formatted input files can contain relocs that
10572 reference non-existant symbols. Check here so that
10573 we do not seg fault. */
10574 if (h == NULL)
10575 {
10576 _bfd_error_handler
10577 /* xgettext:c-format */
10578 (_("error: %B contains a reloc (%#Lx) for section %A "
10579 "that references a non-existent global symbol"),
10580 input_bfd, rel->r_info, o);
10581 bfd_set_error (bfd_error_bad_value);
10582 return FALSE;
10583 }
10584
10585 while (h->root.type == bfd_link_hash_indirect
10586 || h->root.type == bfd_link_hash_warning)
10587 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10588
10589 s_type = h->type;
10590
10591 /* If a plugin symbol is referenced from a non-IR file,
10592 mark the symbol as undefined. Note that the
10593 linker may attach linker created dynamic sections
10594 to the plugin bfd. Symbols defined in linker
10595 created sections are not plugin symbols. */
10596 if ((h->root.non_ir_ref_regular
10597 || h->root.non_ir_ref_dynamic)
10598 && (h->root.type == bfd_link_hash_defined
10599 || h->root.type == bfd_link_hash_defweak)
10600 && (h->root.u.def.section->flags
10601 & SEC_LINKER_CREATED) == 0
10602 && h->root.u.def.section->owner != NULL
10603 && (h->root.u.def.section->owner->flags
10604 & BFD_PLUGIN) != 0)
10605 {
10606 h->root.type = bfd_link_hash_undefined;
10607 h->root.u.undef.abfd = h->root.u.def.section->owner;
10608 }
10609
10610 ps = NULL;
10611 if (h->root.type == bfd_link_hash_defined
10612 || h->root.type == bfd_link_hash_defweak)
10613 ps = &h->root.u.def.section;
10614
10615 sym_name = h->root.root.string;
10616 }
10617 else
10618 {
10619 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10620
10621 s_type = ELF_ST_TYPE (sym->st_info);
10622 ps = &flinfo->sections[r_symndx];
10623 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10624 sym, *ps);
10625 }
10626
10627 if ((s_type == STT_RELC || s_type == STT_SRELC)
10628 && !bfd_link_relocatable (flinfo->info))
10629 {
10630 bfd_vma val;
10631 bfd_vma dot = (rel->r_offset
10632 + o->output_offset + o->output_section->vma);
10633 #ifdef DEBUG
10634 printf ("Encountered a complex symbol!");
10635 printf (" (input_bfd %s, section %s, reloc %ld\n",
10636 input_bfd->filename, o->name,
10637 (long) (rel - internal_relocs));
10638 printf (" symbol: idx %8.8lx, name %s\n",
10639 r_symndx, sym_name);
10640 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10641 (unsigned long) rel->r_info,
10642 (unsigned long) rel->r_offset);
10643 #endif
10644 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10645 isymbuf, locsymcount, s_type == STT_SRELC))
10646 return FALSE;
10647
10648 /* Symbol evaluated OK. Update to absolute value. */
10649 set_symbol_value (input_bfd, isymbuf, locsymcount,
10650 r_symndx, val);
10651 continue;
10652 }
10653
10654 if (action_discarded != -1 && ps != NULL)
10655 {
10656 /* Complain if the definition comes from a
10657 discarded section. */
10658 if ((sec = *ps) != NULL && discarded_section (sec))
10659 {
10660 BFD_ASSERT (r_symndx != STN_UNDEF);
10661 if (action_discarded & COMPLAIN)
10662 (*flinfo->info->callbacks->einfo)
10663 /* xgettext:c-format */
10664 (_("%X`%s' referenced in section `%A' of %B: "
10665 "defined in discarded section `%A' of %B\n"),
10666 sym_name, o, input_bfd, sec, sec->owner);
10667
10668 /* Try to do the best we can to support buggy old
10669 versions of gcc. Pretend that the symbol is
10670 really defined in the kept linkonce section.
10671 FIXME: This is quite broken. Modifying the
10672 symbol here means we will be changing all later
10673 uses of the symbol, not just in this section. */
10674 if (action_discarded & PRETEND)
10675 {
10676 asection *kept;
10677
10678 kept = _bfd_elf_check_kept_section (sec,
10679 flinfo->info);
10680 if (kept != NULL)
10681 {
10682 *ps = kept;
10683 continue;
10684 }
10685 }
10686 }
10687 }
10688 }
10689
10690 /* Relocate the section by invoking a back end routine.
10691
10692 The back end routine is responsible for adjusting the
10693 section contents as necessary, and (if using Rela relocs
10694 and generating a relocatable output file) adjusting the
10695 reloc addend as necessary.
10696
10697 The back end routine does not have to worry about setting
10698 the reloc address or the reloc symbol index.
10699
10700 The back end routine is given a pointer to the swapped in
10701 internal symbols, and can access the hash table entries
10702 for the external symbols via elf_sym_hashes (input_bfd).
10703
10704 When generating relocatable output, the back end routine
10705 must handle STB_LOCAL/STT_SECTION symbols specially. The
10706 output symbol is going to be a section symbol
10707 corresponding to the output section, which will require
10708 the addend to be adjusted. */
10709
10710 ret = (*relocate_section) (output_bfd, flinfo->info,
10711 input_bfd, o, contents,
10712 internal_relocs,
10713 isymbuf,
10714 flinfo->sections);
10715 if (!ret)
10716 return FALSE;
10717
10718 if (ret == 2
10719 || bfd_link_relocatable (flinfo->info)
10720 || flinfo->info->emitrelocations)
10721 {
10722 Elf_Internal_Rela *irela;
10723 Elf_Internal_Rela *irelaend, *irelamid;
10724 bfd_vma last_offset;
10725 struct elf_link_hash_entry **rel_hash;
10726 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10727 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10728 unsigned int next_erel;
10729 bfd_boolean rela_normal;
10730 struct bfd_elf_section_data *esdi, *esdo;
10731
10732 esdi = elf_section_data (o);
10733 esdo = elf_section_data (o->output_section);
10734 rela_normal = FALSE;
10735
10736 /* Adjust the reloc addresses and symbol indices. */
10737
10738 irela = internal_relocs;
10739 irelaend = irela + o->reloc_count;
10740 rel_hash = esdo->rel.hashes + esdo->rel.count;
10741 /* We start processing the REL relocs, if any. When we reach
10742 IRELAMID in the loop, we switch to the RELA relocs. */
10743 irelamid = irela;
10744 if (esdi->rel.hdr != NULL)
10745 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10746 * bed->s->int_rels_per_ext_rel);
10747 rel_hash_list = rel_hash;
10748 rela_hash_list = NULL;
10749 last_offset = o->output_offset;
10750 if (!bfd_link_relocatable (flinfo->info))
10751 last_offset += o->output_section->vma;
10752 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10753 {
10754 unsigned long r_symndx;
10755 asection *sec;
10756 Elf_Internal_Sym sym;
10757
10758 if (next_erel == bed->s->int_rels_per_ext_rel)
10759 {
10760 rel_hash++;
10761 next_erel = 0;
10762 }
10763
10764 if (irela == irelamid)
10765 {
10766 rel_hash = esdo->rela.hashes + esdo->rela.count;
10767 rela_hash_list = rel_hash;
10768 rela_normal = bed->rela_normal;
10769 }
10770
10771 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10772 flinfo->info, o,
10773 irela->r_offset);
10774 if (irela->r_offset >= (bfd_vma) -2)
10775 {
10776 /* This is a reloc for a deleted entry or somesuch.
10777 Turn it into an R_*_NONE reloc, at the same
10778 offset as the last reloc. elf_eh_frame.c and
10779 bfd_elf_discard_info rely on reloc offsets
10780 being ordered. */
10781 irela->r_offset = last_offset;
10782 irela->r_info = 0;
10783 irela->r_addend = 0;
10784 continue;
10785 }
10786
10787 irela->r_offset += o->output_offset;
10788
10789 /* Relocs in an executable have to be virtual addresses. */
10790 if (!bfd_link_relocatable (flinfo->info))
10791 irela->r_offset += o->output_section->vma;
10792
10793 last_offset = irela->r_offset;
10794
10795 r_symndx = irela->r_info >> r_sym_shift;
10796 if (r_symndx == STN_UNDEF)
10797 continue;
10798
10799 if (r_symndx >= locsymcount
10800 || (elf_bad_symtab (input_bfd)
10801 && flinfo->sections[r_symndx] == NULL))
10802 {
10803 struct elf_link_hash_entry *rh;
10804 unsigned long indx;
10805
10806 /* This is a reloc against a global symbol. We
10807 have not yet output all the local symbols, so
10808 we do not know the symbol index of any global
10809 symbol. We set the rel_hash entry for this
10810 reloc to point to the global hash table entry
10811 for this symbol. The symbol index is then
10812 set at the end of bfd_elf_final_link. */
10813 indx = r_symndx - extsymoff;
10814 rh = elf_sym_hashes (input_bfd)[indx];
10815 while (rh->root.type == bfd_link_hash_indirect
10816 || rh->root.type == bfd_link_hash_warning)
10817 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10818
10819 /* Setting the index to -2 tells
10820 elf_link_output_extsym that this symbol is
10821 used by a reloc. */
10822 BFD_ASSERT (rh->indx < 0);
10823 rh->indx = -2;
10824 *rel_hash = rh;
10825
10826 continue;
10827 }
10828
10829 /* This is a reloc against a local symbol. */
10830
10831 *rel_hash = NULL;
10832 sym = isymbuf[r_symndx];
10833 sec = flinfo->sections[r_symndx];
10834 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10835 {
10836 /* I suppose the backend ought to fill in the
10837 section of any STT_SECTION symbol against a
10838 processor specific section. */
10839 r_symndx = STN_UNDEF;
10840 if (bfd_is_abs_section (sec))
10841 ;
10842 else if (sec == NULL || sec->owner == NULL)
10843 {
10844 bfd_set_error (bfd_error_bad_value);
10845 return FALSE;
10846 }
10847 else
10848 {
10849 asection *osec = sec->output_section;
10850
10851 /* If we have discarded a section, the output
10852 section will be the absolute section. In
10853 case of discarded SEC_MERGE sections, use
10854 the kept section. relocate_section should
10855 have already handled discarded linkonce
10856 sections. */
10857 if (bfd_is_abs_section (osec)
10858 && sec->kept_section != NULL
10859 && sec->kept_section->output_section != NULL)
10860 {
10861 osec = sec->kept_section->output_section;
10862 irela->r_addend -= osec->vma;
10863 }
10864
10865 if (!bfd_is_abs_section (osec))
10866 {
10867 r_symndx = osec->target_index;
10868 if (r_symndx == STN_UNDEF)
10869 {
10870 irela->r_addend += osec->vma;
10871 osec = _bfd_nearby_section (output_bfd, osec,
10872 osec->vma);
10873 irela->r_addend -= osec->vma;
10874 r_symndx = osec->target_index;
10875 }
10876 }
10877 }
10878
10879 /* Adjust the addend according to where the
10880 section winds up in the output section. */
10881 if (rela_normal)
10882 irela->r_addend += sec->output_offset;
10883 }
10884 else
10885 {
10886 if (flinfo->indices[r_symndx] == -1)
10887 {
10888 unsigned long shlink;
10889 const char *name;
10890 asection *osec;
10891 long indx;
10892
10893 if (flinfo->info->strip == strip_all)
10894 {
10895 /* You can't do ld -r -s. */
10896 bfd_set_error (bfd_error_invalid_operation);
10897 return FALSE;
10898 }
10899
10900 /* This symbol was skipped earlier, but
10901 since it is needed by a reloc, we
10902 must output it now. */
10903 shlink = symtab_hdr->sh_link;
10904 name = (bfd_elf_string_from_elf_section
10905 (input_bfd, shlink, sym.st_name));
10906 if (name == NULL)
10907 return FALSE;
10908
10909 osec = sec->output_section;
10910 sym.st_shndx =
10911 _bfd_elf_section_from_bfd_section (output_bfd,
10912 osec);
10913 if (sym.st_shndx == SHN_BAD)
10914 return FALSE;
10915
10916 sym.st_value += sec->output_offset;
10917 if (!bfd_link_relocatable (flinfo->info))
10918 {
10919 sym.st_value += osec->vma;
10920 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10921 {
10922 /* STT_TLS symbols are relative to PT_TLS
10923 segment base. */
10924 BFD_ASSERT (elf_hash_table (flinfo->info)
10925 ->tls_sec != NULL);
10926 sym.st_value -= (elf_hash_table (flinfo->info)
10927 ->tls_sec->vma);
10928 }
10929 }
10930
10931 indx = bfd_get_symcount (output_bfd);
10932 ret = elf_link_output_symstrtab (flinfo, name,
10933 &sym, sec,
10934 NULL);
10935 if (ret == 0)
10936 return FALSE;
10937 else if (ret == 1)
10938 flinfo->indices[r_symndx] = indx;
10939 else
10940 abort ();
10941 }
10942
10943 r_symndx = flinfo->indices[r_symndx];
10944 }
10945
10946 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10947 | (irela->r_info & r_type_mask));
10948 }
10949
10950 /* Swap out the relocs. */
10951 input_rel_hdr = esdi->rel.hdr;
10952 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
10953 {
10954 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10955 input_rel_hdr,
10956 internal_relocs,
10957 rel_hash_list))
10958 return FALSE;
10959 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10960 * bed->s->int_rels_per_ext_rel);
10961 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
10962 }
10963
10964 input_rela_hdr = esdi->rela.hdr;
10965 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10966 {
10967 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10968 input_rela_hdr,
10969 internal_relocs,
10970 rela_hash_list))
10971 return FALSE;
10972 }
10973 }
10974 }
10975
10976 /* Write out the modified section contents. */
10977 if (bed->elf_backend_write_section
10978 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
10979 contents))
10980 {
10981 /* Section written out. */
10982 }
10983 else switch (o->sec_info_type)
10984 {
10985 case SEC_INFO_TYPE_STABS:
10986 if (! (_bfd_write_section_stabs
10987 (output_bfd,
10988 &elf_hash_table (flinfo->info)->stab_info,
10989 o, &elf_section_data (o)->sec_info, contents)))
10990 return FALSE;
10991 break;
10992 case SEC_INFO_TYPE_MERGE:
10993 if (! _bfd_write_merged_section (output_bfd, o,
10994 elf_section_data (o)->sec_info))
10995 return FALSE;
10996 break;
10997 case SEC_INFO_TYPE_EH_FRAME:
10998 {
10999 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11000 o, contents))
11001 return FALSE;
11002 }
11003 break;
11004 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11005 {
11006 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11007 flinfo->info,
11008 o, contents))
11009 return FALSE;
11010 }
11011 break;
11012 default:
11013 {
11014 if (! (o->flags & SEC_EXCLUDE))
11015 {
11016 file_ptr offset = (file_ptr) o->output_offset;
11017 bfd_size_type todo = o->size;
11018
11019 offset *= bfd_octets_per_byte (output_bfd);
11020
11021 if ((o->flags & SEC_ELF_REVERSE_COPY))
11022 {
11023 /* Reverse-copy input section to output. */
11024 do
11025 {
11026 todo -= address_size;
11027 if (! bfd_set_section_contents (output_bfd,
11028 o->output_section,
11029 contents + todo,
11030 offset,
11031 address_size))
11032 return FALSE;
11033 if (todo == 0)
11034 break;
11035 offset += address_size;
11036 }
11037 while (1);
11038 }
11039 else if (! bfd_set_section_contents (output_bfd,
11040 o->output_section,
11041 contents,
11042 offset, todo))
11043 return FALSE;
11044 }
11045 }
11046 break;
11047 }
11048 }
11049
11050 return TRUE;
11051 }
11052
11053 /* Generate a reloc when linking an ELF file. This is a reloc
11054 requested by the linker, and does not come from any input file. This
11055 is used to build constructor and destructor tables when linking
11056 with -Ur. */
11057
11058 static bfd_boolean
11059 elf_reloc_link_order (bfd *output_bfd,
11060 struct bfd_link_info *info,
11061 asection *output_section,
11062 struct bfd_link_order *link_order)
11063 {
11064 reloc_howto_type *howto;
11065 long indx;
11066 bfd_vma offset;
11067 bfd_vma addend;
11068 struct bfd_elf_section_reloc_data *reldata;
11069 struct elf_link_hash_entry **rel_hash_ptr;
11070 Elf_Internal_Shdr *rel_hdr;
11071 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11072 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11073 bfd_byte *erel;
11074 unsigned int i;
11075 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11076
11077 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11078 if (howto == NULL)
11079 {
11080 bfd_set_error (bfd_error_bad_value);
11081 return FALSE;
11082 }
11083
11084 addend = link_order->u.reloc.p->addend;
11085
11086 if (esdo->rel.hdr)
11087 reldata = &esdo->rel;
11088 else if (esdo->rela.hdr)
11089 reldata = &esdo->rela;
11090 else
11091 {
11092 reldata = NULL;
11093 BFD_ASSERT (0);
11094 }
11095
11096 /* Figure out the symbol index. */
11097 rel_hash_ptr = reldata->hashes + reldata->count;
11098 if (link_order->type == bfd_section_reloc_link_order)
11099 {
11100 indx = link_order->u.reloc.p->u.section->target_index;
11101 BFD_ASSERT (indx != 0);
11102 *rel_hash_ptr = NULL;
11103 }
11104 else
11105 {
11106 struct elf_link_hash_entry *h;
11107
11108 /* Treat a reloc against a defined symbol as though it were
11109 actually against the section. */
11110 h = ((struct elf_link_hash_entry *)
11111 bfd_wrapped_link_hash_lookup (output_bfd, info,
11112 link_order->u.reloc.p->u.name,
11113 FALSE, FALSE, TRUE));
11114 if (h != NULL
11115 && (h->root.type == bfd_link_hash_defined
11116 || h->root.type == bfd_link_hash_defweak))
11117 {
11118 asection *section;
11119
11120 section = h->root.u.def.section;
11121 indx = section->output_section->target_index;
11122 *rel_hash_ptr = NULL;
11123 /* It seems that we ought to add the symbol value to the
11124 addend here, but in practice it has already been added
11125 because it was passed to constructor_callback. */
11126 addend += section->output_section->vma + section->output_offset;
11127 }
11128 else if (h != NULL)
11129 {
11130 /* Setting the index to -2 tells elf_link_output_extsym that
11131 this symbol is used by a reloc. */
11132 h->indx = -2;
11133 *rel_hash_ptr = h;
11134 indx = 0;
11135 }
11136 else
11137 {
11138 (*info->callbacks->unattached_reloc)
11139 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11140 indx = 0;
11141 }
11142 }
11143
11144 /* If this is an inplace reloc, we must write the addend into the
11145 object file. */
11146 if (howto->partial_inplace && addend != 0)
11147 {
11148 bfd_size_type size;
11149 bfd_reloc_status_type rstat;
11150 bfd_byte *buf;
11151 bfd_boolean ok;
11152 const char *sym_name;
11153
11154 size = (bfd_size_type) bfd_get_reloc_size (howto);
11155 buf = (bfd_byte *) bfd_zmalloc (size);
11156 if (buf == NULL && size != 0)
11157 return FALSE;
11158 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11159 switch (rstat)
11160 {
11161 case bfd_reloc_ok:
11162 break;
11163
11164 default:
11165 case bfd_reloc_outofrange:
11166 abort ();
11167
11168 case bfd_reloc_overflow:
11169 if (link_order->type == bfd_section_reloc_link_order)
11170 sym_name = bfd_section_name (output_bfd,
11171 link_order->u.reloc.p->u.section);
11172 else
11173 sym_name = link_order->u.reloc.p->u.name;
11174 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11175 howto->name, addend, NULL, NULL,
11176 (bfd_vma) 0);
11177 break;
11178 }
11179
11180 ok = bfd_set_section_contents (output_bfd, output_section, buf,
11181 link_order->offset
11182 * bfd_octets_per_byte (output_bfd),
11183 size);
11184 free (buf);
11185 if (! ok)
11186 return FALSE;
11187 }
11188
11189 /* The address of a reloc is relative to the section in a
11190 relocatable file, and is a virtual address in an executable
11191 file. */
11192 offset = link_order->offset;
11193 if (! bfd_link_relocatable (info))
11194 offset += output_section->vma;
11195
11196 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11197 {
11198 irel[i].r_offset = offset;
11199 irel[i].r_info = 0;
11200 irel[i].r_addend = 0;
11201 }
11202 if (bed->s->arch_size == 32)
11203 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11204 else
11205 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11206
11207 rel_hdr = reldata->hdr;
11208 erel = rel_hdr->contents;
11209 if (rel_hdr->sh_type == SHT_REL)
11210 {
11211 erel += reldata->count * bed->s->sizeof_rel;
11212 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11213 }
11214 else
11215 {
11216 irel[0].r_addend = addend;
11217 erel += reldata->count * bed->s->sizeof_rela;
11218 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11219 }
11220
11221 ++reldata->count;
11222
11223 return TRUE;
11224 }
11225
11226
11227 /* Get the output vma of the section pointed to by the sh_link field. */
11228
11229 static bfd_vma
11230 elf_get_linked_section_vma (struct bfd_link_order *p)
11231 {
11232 Elf_Internal_Shdr **elf_shdrp;
11233 asection *s;
11234 int elfsec;
11235
11236 s = p->u.indirect.section;
11237 elf_shdrp = elf_elfsections (s->owner);
11238 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
11239 elfsec = elf_shdrp[elfsec]->sh_link;
11240 /* PR 290:
11241 The Intel C compiler generates SHT_IA_64_UNWIND with
11242 SHF_LINK_ORDER. But it doesn't set the sh_link or
11243 sh_info fields. Hence we could get the situation
11244 where elfsec is 0. */
11245 if (elfsec == 0)
11246 {
11247 const struct elf_backend_data *bed
11248 = get_elf_backend_data (s->owner);
11249 if (bed->link_order_error_handler)
11250 bed->link_order_error_handler
11251 /* xgettext:c-format */
11252 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
11253 return 0;
11254 }
11255 else
11256 {
11257 s = elf_shdrp[elfsec]->bfd_section;
11258 return s->output_section->vma + s->output_offset;
11259 }
11260 }
11261
11262
11263 /* Compare two sections based on the locations of the sections they are
11264 linked to. Used by elf_fixup_link_order. */
11265
11266 static int
11267 compare_link_order (const void * a, const void * b)
11268 {
11269 bfd_vma apos;
11270 bfd_vma bpos;
11271
11272 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11273 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11274 if (apos < bpos)
11275 return -1;
11276 return apos > bpos;
11277 }
11278
11279
11280 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11281 order as their linked sections. Returns false if this could not be done
11282 because an output section includes both ordered and unordered
11283 sections. Ideally we'd do this in the linker proper. */
11284
11285 static bfd_boolean
11286 elf_fixup_link_order (bfd *abfd, asection *o)
11287 {
11288 int seen_linkorder;
11289 int seen_other;
11290 int n;
11291 struct bfd_link_order *p;
11292 bfd *sub;
11293 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11294 unsigned elfsec;
11295 struct bfd_link_order **sections;
11296 asection *s, *other_sec, *linkorder_sec;
11297 bfd_vma offset;
11298
11299 other_sec = NULL;
11300 linkorder_sec = NULL;
11301 seen_other = 0;
11302 seen_linkorder = 0;
11303 for (p = o->map_head.link_order; p != NULL; p = p->next)
11304 {
11305 if (p->type == bfd_indirect_link_order)
11306 {
11307 s = p->u.indirect.section;
11308 sub = s->owner;
11309 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11310 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
11311 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11312 && elfsec < elf_numsections (sub)
11313 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11314 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
11315 {
11316 seen_linkorder++;
11317 linkorder_sec = s;
11318 }
11319 else
11320 {
11321 seen_other++;
11322 other_sec = s;
11323 }
11324 }
11325 else
11326 seen_other++;
11327
11328 if (seen_other && seen_linkorder)
11329 {
11330 if (other_sec && linkorder_sec)
11331 _bfd_error_handler
11332 /* xgettext:c-format */
11333 (_("%A has both ordered [`%A' in %B] "
11334 "and unordered [`%A' in %B] sections"),
11335 o, linkorder_sec, linkorder_sec->owner,
11336 other_sec, other_sec->owner);
11337 else
11338 _bfd_error_handler
11339 (_("%A has both ordered and unordered sections"), o);
11340 bfd_set_error (bfd_error_bad_value);
11341 return FALSE;
11342 }
11343 }
11344
11345 if (!seen_linkorder)
11346 return TRUE;
11347
11348 sections = (struct bfd_link_order **)
11349 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11350 if (sections == NULL)
11351 return FALSE;
11352 seen_linkorder = 0;
11353
11354 for (p = o->map_head.link_order; p != NULL; p = p->next)
11355 {
11356 sections[seen_linkorder++] = p;
11357 }
11358 /* Sort the input sections in the order of their linked section. */
11359 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11360 compare_link_order);
11361
11362 /* Change the offsets of the sections. */
11363 offset = 0;
11364 for (n = 0; n < seen_linkorder; n++)
11365 {
11366 s = sections[n]->u.indirect.section;
11367 offset &= ~(bfd_vma) 0 << s->alignment_power;
11368 s->output_offset = offset / bfd_octets_per_byte (abfd);
11369 sections[n]->offset = offset;
11370 offset += sections[n]->size;
11371 }
11372
11373 free (sections);
11374 return TRUE;
11375 }
11376
11377 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11378 Returns TRUE upon success, FALSE otherwise. */
11379
11380 static bfd_boolean
11381 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11382 {
11383 bfd_boolean ret = FALSE;
11384 bfd *implib_bfd;
11385 const struct elf_backend_data *bed;
11386 flagword flags;
11387 enum bfd_architecture arch;
11388 unsigned int mach;
11389 asymbol **sympp = NULL;
11390 long symsize;
11391 long symcount;
11392 long src_count;
11393 elf_symbol_type *osymbuf;
11394
11395 implib_bfd = info->out_implib_bfd;
11396 bed = get_elf_backend_data (abfd);
11397
11398 if (!bfd_set_format (implib_bfd, bfd_object))
11399 return FALSE;
11400
11401 /* Use flag from executable but make it a relocatable object. */
11402 flags = bfd_get_file_flags (abfd);
11403 flags &= ~HAS_RELOC;
11404 if (!bfd_set_start_address (implib_bfd, 0)
11405 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
11406 return FALSE;
11407
11408 /* Copy architecture of output file to import library file. */
11409 arch = bfd_get_arch (abfd);
11410 mach = bfd_get_mach (abfd);
11411 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11412 && (abfd->target_defaulted
11413 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11414 return FALSE;
11415
11416 /* Get symbol table size. */
11417 symsize = bfd_get_symtab_upper_bound (abfd);
11418 if (symsize < 0)
11419 return FALSE;
11420
11421 /* Read in the symbol table. */
11422 sympp = (asymbol **) xmalloc (symsize);
11423 symcount = bfd_canonicalize_symtab (abfd, sympp);
11424 if (symcount < 0)
11425 goto free_sym_buf;
11426
11427 /* Allow the BFD backend to copy any private header data it
11428 understands from the output BFD to the import library BFD. */
11429 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11430 goto free_sym_buf;
11431
11432 /* Filter symbols to appear in the import library. */
11433 if (bed->elf_backend_filter_implib_symbols)
11434 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11435 symcount);
11436 else
11437 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11438 if (symcount == 0)
11439 {
11440 bfd_set_error (bfd_error_no_symbols);
11441 _bfd_error_handler (_("%B: no symbol found for import library"),
11442 implib_bfd);
11443 goto free_sym_buf;
11444 }
11445
11446
11447 /* Make symbols absolute. */
11448 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11449 sizeof (*osymbuf));
11450 for (src_count = 0; src_count < symcount; src_count++)
11451 {
11452 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11453 sizeof (*osymbuf));
11454 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11455 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11456 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11457 osymbuf[src_count].internal_elf_sym.st_value =
11458 osymbuf[src_count].symbol.value;
11459 sympp[src_count] = &osymbuf[src_count].symbol;
11460 }
11461
11462 bfd_set_symtab (implib_bfd, sympp, symcount);
11463
11464 /* Allow the BFD backend to copy any private data it understands
11465 from the output BFD to the import library BFD. This is done last
11466 to permit the routine to look at the filtered symbol table. */
11467 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11468 goto free_sym_buf;
11469
11470 if (!bfd_close (implib_bfd))
11471 goto free_sym_buf;
11472
11473 ret = TRUE;
11474
11475 free_sym_buf:
11476 free (sympp);
11477 return ret;
11478 }
11479
11480 static void
11481 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11482 {
11483 asection *o;
11484
11485 if (flinfo->symstrtab != NULL)
11486 _bfd_elf_strtab_free (flinfo->symstrtab);
11487 if (flinfo->contents != NULL)
11488 free (flinfo->contents);
11489 if (flinfo->external_relocs != NULL)
11490 free (flinfo->external_relocs);
11491 if (flinfo->internal_relocs != NULL)
11492 free (flinfo->internal_relocs);
11493 if (flinfo->external_syms != NULL)
11494 free (flinfo->external_syms);
11495 if (flinfo->locsym_shndx != NULL)
11496 free (flinfo->locsym_shndx);
11497 if (flinfo->internal_syms != NULL)
11498 free (flinfo->internal_syms);
11499 if (flinfo->indices != NULL)
11500 free (flinfo->indices);
11501 if (flinfo->sections != NULL)
11502 free (flinfo->sections);
11503 if (flinfo->symshndxbuf != NULL)
11504 free (flinfo->symshndxbuf);
11505 for (o = obfd->sections; o != NULL; o = o->next)
11506 {
11507 struct bfd_elf_section_data *esdo = elf_section_data (o);
11508 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11509 free (esdo->rel.hashes);
11510 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11511 free (esdo->rela.hashes);
11512 }
11513 }
11514
11515 /* Do the final step of an ELF link. */
11516
11517 bfd_boolean
11518 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11519 {
11520 bfd_boolean dynamic;
11521 bfd_boolean emit_relocs;
11522 bfd *dynobj;
11523 struct elf_final_link_info flinfo;
11524 asection *o;
11525 struct bfd_link_order *p;
11526 bfd *sub;
11527 bfd_size_type max_contents_size;
11528 bfd_size_type max_external_reloc_size;
11529 bfd_size_type max_internal_reloc_count;
11530 bfd_size_type max_sym_count;
11531 bfd_size_type max_sym_shndx_count;
11532 Elf_Internal_Sym elfsym;
11533 unsigned int i;
11534 Elf_Internal_Shdr *symtab_hdr;
11535 Elf_Internal_Shdr *symtab_shndx_hdr;
11536 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11537 struct elf_outext_info eoinfo;
11538 bfd_boolean merged;
11539 size_t relativecount = 0;
11540 asection *reldyn = 0;
11541 bfd_size_type amt;
11542 asection *attr_section = NULL;
11543 bfd_vma attr_size = 0;
11544 const char *std_attrs_section;
11545 struct elf_link_hash_table *htab = elf_hash_table (info);
11546
11547 if (!is_elf_hash_table (htab))
11548 return FALSE;
11549
11550 if (bfd_link_pic (info))
11551 abfd->flags |= DYNAMIC;
11552
11553 dynamic = htab->dynamic_sections_created;
11554 dynobj = htab->dynobj;
11555
11556 emit_relocs = (bfd_link_relocatable (info)
11557 || info->emitrelocations);
11558
11559 flinfo.info = info;
11560 flinfo.output_bfd = abfd;
11561 flinfo.symstrtab = _bfd_elf_strtab_init ();
11562 if (flinfo.symstrtab == NULL)
11563 return FALSE;
11564
11565 if (! dynamic)
11566 {
11567 flinfo.hash_sec = NULL;
11568 flinfo.symver_sec = NULL;
11569 }
11570 else
11571 {
11572 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11573 /* Note that dynsym_sec can be NULL (on VMS). */
11574 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11575 /* Note that it is OK if symver_sec is NULL. */
11576 }
11577
11578 flinfo.contents = NULL;
11579 flinfo.external_relocs = NULL;
11580 flinfo.internal_relocs = NULL;
11581 flinfo.external_syms = NULL;
11582 flinfo.locsym_shndx = NULL;
11583 flinfo.internal_syms = NULL;
11584 flinfo.indices = NULL;
11585 flinfo.sections = NULL;
11586 flinfo.symshndxbuf = NULL;
11587 flinfo.filesym_count = 0;
11588
11589 /* The object attributes have been merged. Remove the input
11590 sections from the link, and set the contents of the output
11591 secton. */
11592 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11593 for (o = abfd->sections; o != NULL; o = o->next)
11594 {
11595 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11596 || strcmp (o->name, ".gnu.attributes") == 0)
11597 {
11598 for (p = o->map_head.link_order; p != NULL; p = p->next)
11599 {
11600 asection *input_section;
11601
11602 if (p->type != bfd_indirect_link_order)
11603 continue;
11604 input_section = p->u.indirect.section;
11605 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11606 elf_link_input_bfd ignores this section. */
11607 input_section->flags &= ~SEC_HAS_CONTENTS;
11608 }
11609
11610 attr_size = bfd_elf_obj_attr_size (abfd);
11611 if (attr_size)
11612 {
11613 bfd_set_section_size (abfd, o, attr_size);
11614 attr_section = o;
11615 /* Skip this section later on. */
11616 o->map_head.link_order = NULL;
11617 }
11618 else
11619 o->flags |= SEC_EXCLUDE;
11620 }
11621 }
11622
11623 /* Count up the number of relocations we will output for each output
11624 section, so that we know the sizes of the reloc sections. We
11625 also figure out some maximum sizes. */
11626 max_contents_size = 0;
11627 max_external_reloc_size = 0;
11628 max_internal_reloc_count = 0;
11629 max_sym_count = 0;
11630 max_sym_shndx_count = 0;
11631 merged = FALSE;
11632 for (o = abfd->sections; o != NULL; o = o->next)
11633 {
11634 struct bfd_elf_section_data *esdo = elf_section_data (o);
11635 o->reloc_count = 0;
11636
11637 for (p = o->map_head.link_order; p != NULL; p = p->next)
11638 {
11639 unsigned int reloc_count = 0;
11640 unsigned int additional_reloc_count = 0;
11641 struct bfd_elf_section_data *esdi = NULL;
11642
11643 if (p->type == bfd_section_reloc_link_order
11644 || p->type == bfd_symbol_reloc_link_order)
11645 reloc_count = 1;
11646 else if (p->type == bfd_indirect_link_order)
11647 {
11648 asection *sec;
11649
11650 sec = p->u.indirect.section;
11651
11652 /* Mark all sections which are to be included in the
11653 link. This will normally be every section. We need
11654 to do this so that we can identify any sections which
11655 the linker has decided to not include. */
11656 sec->linker_mark = TRUE;
11657
11658 if (sec->flags & SEC_MERGE)
11659 merged = TRUE;
11660
11661 if (sec->rawsize > max_contents_size)
11662 max_contents_size = sec->rawsize;
11663 if (sec->size > max_contents_size)
11664 max_contents_size = sec->size;
11665
11666 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11667 && (sec->owner->flags & DYNAMIC) == 0)
11668 {
11669 size_t sym_count;
11670
11671 /* We are interested in just local symbols, not all
11672 symbols. */
11673 if (elf_bad_symtab (sec->owner))
11674 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11675 / bed->s->sizeof_sym);
11676 else
11677 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11678
11679 if (sym_count > max_sym_count)
11680 max_sym_count = sym_count;
11681
11682 if (sym_count > max_sym_shndx_count
11683 && elf_symtab_shndx_list (sec->owner) != NULL)
11684 max_sym_shndx_count = sym_count;
11685
11686 if (esdo->this_hdr.sh_type == SHT_REL
11687 || esdo->this_hdr.sh_type == SHT_RELA)
11688 /* Some backends use reloc_count in relocation sections
11689 to count particular types of relocs. Of course,
11690 reloc sections themselves can't have relocations. */
11691 ;
11692 else if (emit_relocs)
11693 {
11694 reloc_count = sec->reloc_count;
11695 if (bed->elf_backend_count_additional_relocs)
11696 {
11697 int c;
11698 c = (*bed->elf_backend_count_additional_relocs) (sec);
11699 additional_reloc_count += c;
11700 }
11701 }
11702 else if (bed->elf_backend_count_relocs)
11703 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11704
11705 esdi = elf_section_data (sec);
11706
11707 if ((sec->flags & SEC_RELOC) != 0)
11708 {
11709 size_t ext_size = 0;
11710
11711 if (esdi->rel.hdr != NULL)
11712 ext_size = esdi->rel.hdr->sh_size;
11713 if (esdi->rela.hdr != NULL)
11714 ext_size += esdi->rela.hdr->sh_size;
11715
11716 if (ext_size > max_external_reloc_size)
11717 max_external_reloc_size = ext_size;
11718 if (sec->reloc_count > max_internal_reloc_count)
11719 max_internal_reloc_count = sec->reloc_count;
11720 }
11721 }
11722 }
11723
11724 if (reloc_count == 0)
11725 continue;
11726
11727 reloc_count += additional_reloc_count;
11728 o->reloc_count += reloc_count;
11729
11730 if (p->type == bfd_indirect_link_order && emit_relocs)
11731 {
11732 if (esdi->rel.hdr)
11733 {
11734 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11735 esdo->rel.count += additional_reloc_count;
11736 }
11737 if (esdi->rela.hdr)
11738 {
11739 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11740 esdo->rela.count += additional_reloc_count;
11741 }
11742 }
11743 else
11744 {
11745 if (o->use_rela_p)
11746 esdo->rela.count += reloc_count;
11747 else
11748 esdo->rel.count += reloc_count;
11749 }
11750 }
11751
11752 if (o->reloc_count > 0)
11753 o->flags |= SEC_RELOC;
11754 else
11755 {
11756 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11757 set it (this is probably a bug) and if it is set
11758 assign_section_numbers will create a reloc section. */
11759 o->flags &=~ SEC_RELOC;
11760 }
11761
11762 /* If the SEC_ALLOC flag is not set, force the section VMA to
11763 zero. This is done in elf_fake_sections as well, but forcing
11764 the VMA to 0 here will ensure that relocs against these
11765 sections are handled correctly. */
11766 if ((o->flags & SEC_ALLOC) == 0
11767 && ! o->user_set_vma)
11768 o->vma = 0;
11769 }
11770
11771 if (! bfd_link_relocatable (info) && merged)
11772 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
11773
11774 /* Figure out the file positions for everything but the symbol table
11775 and the relocs. We set symcount to force assign_section_numbers
11776 to create a symbol table. */
11777 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
11778 BFD_ASSERT (! abfd->output_has_begun);
11779 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11780 goto error_return;
11781
11782 /* Set sizes, and assign file positions for reloc sections. */
11783 for (o = abfd->sections; o != NULL; o = o->next)
11784 {
11785 struct bfd_elf_section_data *esdo = elf_section_data (o);
11786 if ((o->flags & SEC_RELOC) != 0)
11787 {
11788 if (esdo->rel.hdr
11789 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
11790 goto error_return;
11791
11792 if (esdo->rela.hdr
11793 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
11794 goto error_return;
11795 }
11796
11797 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11798 to count upwards while actually outputting the relocations. */
11799 esdo->rel.count = 0;
11800 esdo->rela.count = 0;
11801
11802 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11803 {
11804 /* Cache the section contents so that they can be compressed
11805 later. Use bfd_malloc since it will be freed by
11806 bfd_compress_section_contents. */
11807 unsigned char *contents = esdo->this_hdr.contents;
11808 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11809 abort ();
11810 contents
11811 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11812 if (contents == NULL)
11813 goto error_return;
11814 esdo->this_hdr.contents = contents;
11815 }
11816 }
11817
11818 /* We have now assigned file positions for all the sections except
11819 .symtab, .strtab, and non-loaded reloc sections. We start the
11820 .symtab section at the current file position, and write directly
11821 to it. We build the .strtab section in memory. */
11822 bfd_get_symcount (abfd) = 0;
11823 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11824 /* sh_name is set in prep_headers. */
11825 symtab_hdr->sh_type = SHT_SYMTAB;
11826 /* sh_flags, sh_addr and sh_size all start off zero. */
11827 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11828 /* sh_link is set in assign_section_numbers. */
11829 /* sh_info is set below. */
11830 /* sh_offset is set just below. */
11831 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11832
11833 if (max_sym_count < 20)
11834 max_sym_count = 20;
11835 htab->strtabsize = max_sym_count;
11836 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11837 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
11838 if (htab->strtab == NULL)
11839 goto error_return;
11840 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11841 flinfo.symshndxbuf
11842 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11843 ? (Elf_External_Sym_Shndx *) -1 : NULL);
11844
11845 if (info->strip != strip_all || emit_relocs)
11846 {
11847 file_ptr off = elf_next_file_pos (abfd);
11848
11849 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11850
11851 /* Note that at this point elf_next_file_pos (abfd) is
11852 incorrect. We do not yet know the size of the .symtab section.
11853 We correct next_file_pos below, after we do know the size. */
11854
11855 /* Start writing out the symbol table. The first symbol is always a
11856 dummy symbol. */
11857 elfsym.st_value = 0;
11858 elfsym.st_size = 0;
11859 elfsym.st_info = 0;
11860 elfsym.st_other = 0;
11861 elfsym.st_shndx = SHN_UNDEF;
11862 elfsym.st_target_internal = 0;
11863 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11864 bfd_und_section_ptr, NULL) != 1)
11865 goto error_return;
11866
11867 /* Output a symbol for each section. We output these even if we are
11868 discarding local symbols, since they are used for relocs. These
11869 symbols have no names. We store the index of each one in the
11870 index field of the section, so that we can find it again when
11871 outputting relocs. */
11872
11873 elfsym.st_size = 0;
11874 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11875 elfsym.st_other = 0;
11876 elfsym.st_value = 0;
11877 elfsym.st_target_internal = 0;
11878 for (i = 1; i < elf_numsections (abfd); i++)
11879 {
11880 o = bfd_section_from_elf_index (abfd, i);
11881 if (o != NULL)
11882 {
11883 o->target_index = bfd_get_symcount (abfd);
11884 elfsym.st_shndx = i;
11885 if (!bfd_link_relocatable (info))
11886 elfsym.st_value = o->vma;
11887 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11888 NULL) != 1)
11889 goto error_return;
11890 }
11891 }
11892 }
11893
11894 /* Allocate some memory to hold information read in from the input
11895 files. */
11896 if (max_contents_size != 0)
11897 {
11898 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11899 if (flinfo.contents == NULL)
11900 goto error_return;
11901 }
11902
11903 if (max_external_reloc_size != 0)
11904 {
11905 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11906 if (flinfo.external_relocs == NULL)
11907 goto error_return;
11908 }
11909
11910 if (max_internal_reloc_count != 0)
11911 {
11912 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
11913 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11914 if (flinfo.internal_relocs == NULL)
11915 goto error_return;
11916 }
11917
11918 if (max_sym_count != 0)
11919 {
11920 amt = max_sym_count * bed->s->sizeof_sym;
11921 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11922 if (flinfo.external_syms == NULL)
11923 goto error_return;
11924
11925 amt = max_sym_count * sizeof (Elf_Internal_Sym);
11926 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11927 if (flinfo.internal_syms == NULL)
11928 goto error_return;
11929
11930 amt = max_sym_count * sizeof (long);
11931 flinfo.indices = (long int *) bfd_malloc (amt);
11932 if (flinfo.indices == NULL)
11933 goto error_return;
11934
11935 amt = max_sym_count * sizeof (asection *);
11936 flinfo.sections = (asection **) bfd_malloc (amt);
11937 if (flinfo.sections == NULL)
11938 goto error_return;
11939 }
11940
11941 if (max_sym_shndx_count != 0)
11942 {
11943 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
11944 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11945 if (flinfo.locsym_shndx == NULL)
11946 goto error_return;
11947 }
11948
11949 if (htab->tls_sec)
11950 {
11951 bfd_vma base, end = 0;
11952 asection *sec;
11953
11954 for (sec = htab->tls_sec;
11955 sec && (sec->flags & SEC_THREAD_LOCAL);
11956 sec = sec->next)
11957 {
11958 bfd_size_type size = sec->size;
11959
11960 if (size == 0
11961 && (sec->flags & SEC_HAS_CONTENTS) == 0)
11962 {
11963 struct bfd_link_order *ord = sec->map_tail.link_order;
11964
11965 if (ord != NULL)
11966 size = ord->offset + ord->size;
11967 }
11968 end = sec->vma + size;
11969 }
11970 base = htab->tls_sec->vma;
11971 /* Only align end of TLS section if static TLS doesn't have special
11972 alignment requirements. */
11973 if (bed->static_tls_alignment == 1)
11974 end = align_power (end, htab->tls_sec->alignment_power);
11975 htab->tls_size = end - base;
11976 }
11977
11978 /* Reorder SHF_LINK_ORDER sections. */
11979 for (o = abfd->sections; o != NULL; o = o->next)
11980 {
11981 if (!elf_fixup_link_order (abfd, o))
11982 return FALSE;
11983 }
11984
11985 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11986 return FALSE;
11987
11988 /* Since ELF permits relocations to be against local symbols, we
11989 must have the local symbols available when we do the relocations.
11990 Since we would rather only read the local symbols once, and we
11991 would rather not keep them in memory, we handle all the
11992 relocations for a single input file at the same time.
11993
11994 Unfortunately, there is no way to know the total number of local
11995 symbols until we have seen all of them, and the local symbol
11996 indices precede the global symbol indices. This means that when
11997 we are generating relocatable output, and we see a reloc against
11998 a global symbol, we can not know the symbol index until we have
11999 finished examining all the local symbols to see which ones we are
12000 going to output. To deal with this, we keep the relocations in
12001 memory, and don't output them until the end of the link. This is
12002 an unfortunate waste of memory, but I don't see a good way around
12003 it. Fortunately, it only happens when performing a relocatable
12004 link, which is not the common case. FIXME: If keep_memory is set
12005 we could write the relocs out and then read them again; I don't
12006 know how bad the memory loss will be. */
12007
12008 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12009 sub->output_has_begun = FALSE;
12010 for (o = abfd->sections; o != NULL; o = o->next)
12011 {
12012 for (p = o->map_head.link_order; p != NULL; p = p->next)
12013 {
12014 if (p->type == bfd_indirect_link_order
12015 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12016 == bfd_target_elf_flavour)
12017 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12018 {
12019 if (! sub->output_has_begun)
12020 {
12021 if (! elf_link_input_bfd (&flinfo, sub))
12022 goto error_return;
12023 sub->output_has_begun = TRUE;
12024 }
12025 }
12026 else if (p->type == bfd_section_reloc_link_order
12027 || p->type == bfd_symbol_reloc_link_order)
12028 {
12029 if (! elf_reloc_link_order (abfd, info, o, p))
12030 goto error_return;
12031 }
12032 else
12033 {
12034 if (! _bfd_default_link_order (abfd, info, o, p))
12035 {
12036 if (p->type == bfd_indirect_link_order
12037 && (bfd_get_flavour (sub)
12038 == bfd_target_elf_flavour)
12039 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12040 != bed->s->elfclass))
12041 {
12042 const char *iclass, *oclass;
12043
12044 switch (bed->s->elfclass)
12045 {
12046 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12047 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12048 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12049 default: abort ();
12050 }
12051
12052 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12053 {
12054 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12055 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12056 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12057 default: abort ();
12058 }
12059
12060 bfd_set_error (bfd_error_wrong_format);
12061 _bfd_error_handler
12062 /* xgettext:c-format */
12063 (_("%B: file class %s incompatible with %s"),
12064 sub, iclass, oclass);
12065 }
12066
12067 goto error_return;
12068 }
12069 }
12070 }
12071 }
12072
12073 /* Free symbol buffer if needed. */
12074 if (!info->reduce_memory_overheads)
12075 {
12076 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12077 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
12078 && elf_tdata (sub)->symbuf)
12079 {
12080 free (elf_tdata (sub)->symbuf);
12081 elf_tdata (sub)->symbuf = NULL;
12082 }
12083 }
12084
12085 /* Output any global symbols that got converted to local in a
12086 version script or due to symbol visibility. We do this in a
12087 separate step since ELF requires all local symbols to appear
12088 prior to any global symbols. FIXME: We should only do this if
12089 some global symbols were, in fact, converted to become local.
12090 FIXME: Will this work correctly with the Irix 5 linker? */
12091 eoinfo.failed = FALSE;
12092 eoinfo.flinfo = &flinfo;
12093 eoinfo.localsyms = TRUE;
12094 eoinfo.file_sym_done = FALSE;
12095 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12096 if (eoinfo.failed)
12097 return FALSE;
12098
12099 /* If backend needs to output some local symbols not present in the hash
12100 table, do it now. */
12101 if (bed->elf_backend_output_arch_local_syms
12102 && (info->strip != strip_all || emit_relocs))
12103 {
12104 typedef int (*out_sym_func)
12105 (void *, const char *, Elf_Internal_Sym *, asection *,
12106 struct elf_link_hash_entry *);
12107
12108 if (! ((*bed->elf_backend_output_arch_local_syms)
12109 (abfd, info, &flinfo,
12110 (out_sym_func) elf_link_output_symstrtab)))
12111 return FALSE;
12112 }
12113
12114 /* That wrote out all the local symbols. Finish up the symbol table
12115 with the global symbols. Even if we want to strip everything we
12116 can, we still need to deal with those global symbols that got
12117 converted to local in a version script. */
12118
12119 /* The sh_info field records the index of the first non local symbol. */
12120 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12121
12122 if (dynamic
12123 && htab->dynsym != NULL
12124 && htab->dynsym->output_section != bfd_abs_section_ptr)
12125 {
12126 Elf_Internal_Sym sym;
12127 bfd_byte *dynsym = htab->dynsym->contents;
12128
12129 o = htab->dynsym->output_section;
12130 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12131
12132 /* Write out the section symbols for the output sections. */
12133 if (bfd_link_pic (info)
12134 || htab->is_relocatable_executable)
12135 {
12136 asection *s;
12137
12138 sym.st_size = 0;
12139 sym.st_name = 0;
12140 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12141 sym.st_other = 0;
12142 sym.st_target_internal = 0;
12143
12144 for (s = abfd->sections; s != NULL; s = s->next)
12145 {
12146 int indx;
12147 bfd_byte *dest;
12148 long dynindx;
12149
12150 dynindx = elf_section_data (s)->dynindx;
12151 if (dynindx <= 0)
12152 continue;
12153 indx = elf_section_data (s)->this_idx;
12154 BFD_ASSERT (indx > 0);
12155 sym.st_shndx = indx;
12156 if (! check_dynsym (abfd, &sym))
12157 return FALSE;
12158 sym.st_value = s->vma;
12159 dest = dynsym + dynindx * bed->s->sizeof_sym;
12160 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12161 }
12162 }
12163
12164 /* Write out the local dynsyms. */
12165 if (htab->dynlocal)
12166 {
12167 struct elf_link_local_dynamic_entry *e;
12168 for (e = htab->dynlocal; e ; e = e->next)
12169 {
12170 asection *s;
12171 bfd_byte *dest;
12172
12173 /* Copy the internal symbol and turn off visibility.
12174 Note that we saved a word of storage and overwrote
12175 the original st_name with the dynstr_index. */
12176 sym = e->isym;
12177 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12178
12179 s = bfd_section_from_elf_index (e->input_bfd,
12180 e->isym.st_shndx);
12181 if (s != NULL)
12182 {
12183 sym.st_shndx =
12184 elf_section_data (s->output_section)->this_idx;
12185 if (! check_dynsym (abfd, &sym))
12186 return FALSE;
12187 sym.st_value = (s->output_section->vma
12188 + s->output_offset
12189 + e->isym.st_value);
12190 }
12191
12192 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12193 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12194 }
12195 }
12196 }
12197
12198 /* We get the global symbols from the hash table. */
12199 eoinfo.failed = FALSE;
12200 eoinfo.localsyms = FALSE;
12201 eoinfo.flinfo = &flinfo;
12202 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12203 if (eoinfo.failed)
12204 return FALSE;
12205
12206 /* If backend needs to output some symbols not present in the hash
12207 table, do it now. */
12208 if (bed->elf_backend_output_arch_syms
12209 && (info->strip != strip_all || emit_relocs))
12210 {
12211 typedef int (*out_sym_func)
12212 (void *, const char *, Elf_Internal_Sym *, asection *,
12213 struct elf_link_hash_entry *);
12214
12215 if (! ((*bed->elf_backend_output_arch_syms)
12216 (abfd, info, &flinfo,
12217 (out_sym_func) elf_link_output_symstrtab)))
12218 return FALSE;
12219 }
12220
12221 /* Finalize the .strtab section. */
12222 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12223
12224 /* Swap out the .strtab section. */
12225 if (!elf_link_swap_symbols_out (&flinfo))
12226 return FALSE;
12227
12228 /* Now we know the size of the symtab section. */
12229 if (bfd_get_symcount (abfd) > 0)
12230 {
12231 /* Finish up and write out the symbol string table (.strtab)
12232 section. */
12233 Elf_Internal_Shdr *symstrtab_hdr = NULL;
12234 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12235
12236 if (elf_symtab_shndx_list (abfd))
12237 {
12238 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12239
12240 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12241 {
12242 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12243 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12244 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12245 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12246 symtab_shndx_hdr->sh_size = amt;
12247
12248 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12249 off, TRUE);
12250
12251 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12252 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12253 return FALSE;
12254 }
12255 }
12256
12257 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12258 /* sh_name was set in prep_headers. */
12259 symstrtab_hdr->sh_type = SHT_STRTAB;
12260 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12261 symstrtab_hdr->sh_addr = 0;
12262 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12263 symstrtab_hdr->sh_entsize = 0;
12264 symstrtab_hdr->sh_link = 0;
12265 symstrtab_hdr->sh_info = 0;
12266 /* sh_offset is set just below. */
12267 symstrtab_hdr->sh_addralign = 1;
12268
12269 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12270 off, TRUE);
12271 elf_next_file_pos (abfd) = off;
12272
12273 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12274 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12275 return FALSE;
12276 }
12277
12278 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12279 {
12280 _bfd_error_handler (_("%B: failed to generate import library"),
12281 info->out_implib_bfd);
12282 return FALSE;
12283 }
12284
12285 /* Adjust the relocs to have the correct symbol indices. */
12286 for (o = abfd->sections; o != NULL; o = o->next)
12287 {
12288 struct bfd_elf_section_data *esdo = elf_section_data (o);
12289 bfd_boolean sort;
12290
12291 if ((o->flags & SEC_RELOC) == 0)
12292 continue;
12293
12294 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12295 if (esdo->rel.hdr != NULL
12296 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
12297 return FALSE;
12298 if (esdo->rela.hdr != NULL
12299 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
12300 return FALSE;
12301
12302 /* Set the reloc_count field to 0 to prevent write_relocs from
12303 trying to swap the relocs out itself. */
12304 o->reloc_count = 0;
12305 }
12306
12307 if (dynamic && info->combreloc && dynobj != NULL)
12308 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12309
12310 /* If we are linking against a dynamic object, or generating a
12311 shared library, finish up the dynamic linking information. */
12312 if (dynamic)
12313 {
12314 bfd_byte *dyncon, *dynconend;
12315
12316 /* Fix up .dynamic entries. */
12317 o = bfd_get_linker_section (dynobj, ".dynamic");
12318 BFD_ASSERT (o != NULL);
12319
12320 dyncon = o->contents;
12321 dynconend = o->contents + o->size;
12322 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12323 {
12324 Elf_Internal_Dyn dyn;
12325 const char *name;
12326 unsigned int type;
12327 bfd_size_type sh_size;
12328 bfd_vma sh_addr;
12329
12330 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12331
12332 switch (dyn.d_tag)
12333 {
12334 default:
12335 continue;
12336 case DT_NULL:
12337 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12338 {
12339 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12340 {
12341 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12342 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12343 default: continue;
12344 }
12345 dyn.d_un.d_val = relativecount;
12346 relativecount = 0;
12347 break;
12348 }
12349 continue;
12350
12351 case DT_INIT:
12352 name = info->init_function;
12353 goto get_sym;
12354 case DT_FINI:
12355 name = info->fini_function;
12356 get_sym:
12357 {
12358 struct elf_link_hash_entry *h;
12359
12360 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
12361 if (h != NULL
12362 && (h->root.type == bfd_link_hash_defined
12363 || h->root.type == bfd_link_hash_defweak))
12364 {
12365 dyn.d_un.d_ptr = h->root.u.def.value;
12366 o = h->root.u.def.section;
12367 if (o->output_section != NULL)
12368 dyn.d_un.d_ptr += (o->output_section->vma
12369 + o->output_offset);
12370 else
12371 {
12372 /* The symbol is imported from another shared
12373 library and does not apply to this one. */
12374 dyn.d_un.d_ptr = 0;
12375 }
12376 break;
12377 }
12378 }
12379 continue;
12380
12381 case DT_PREINIT_ARRAYSZ:
12382 name = ".preinit_array";
12383 goto get_out_size;
12384 case DT_INIT_ARRAYSZ:
12385 name = ".init_array";
12386 goto get_out_size;
12387 case DT_FINI_ARRAYSZ:
12388 name = ".fini_array";
12389 get_out_size:
12390 o = bfd_get_section_by_name (abfd, name);
12391 if (o == NULL)
12392 {
12393 _bfd_error_handler
12394 (_("could not find section %s"), name);
12395 goto error_return;
12396 }
12397 if (o->size == 0)
12398 _bfd_error_handler
12399 (_("warning: %s section has zero size"), name);
12400 dyn.d_un.d_val = o->size;
12401 break;
12402
12403 case DT_PREINIT_ARRAY:
12404 name = ".preinit_array";
12405 goto get_out_vma;
12406 case DT_INIT_ARRAY:
12407 name = ".init_array";
12408 goto get_out_vma;
12409 case DT_FINI_ARRAY:
12410 name = ".fini_array";
12411 get_out_vma:
12412 o = bfd_get_section_by_name (abfd, name);
12413 goto do_vma;
12414
12415 case DT_HASH:
12416 name = ".hash";
12417 goto get_vma;
12418 case DT_GNU_HASH:
12419 name = ".gnu.hash";
12420 goto get_vma;
12421 case DT_STRTAB:
12422 name = ".dynstr";
12423 goto get_vma;
12424 case DT_SYMTAB:
12425 name = ".dynsym";
12426 goto get_vma;
12427 case DT_VERDEF:
12428 name = ".gnu.version_d";
12429 goto get_vma;
12430 case DT_VERNEED:
12431 name = ".gnu.version_r";
12432 goto get_vma;
12433 case DT_VERSYM:
12434 name = ".gnu.version";
12435 get_vma:
12436 o = bfd_get_linker_section (dynobj, name);
12437 do_vma:
12438 if (o == NULL || bfd_is_abs_section (o->output_section))
12439 {
12440 _bfd_error_handler
12441 (_("could not find section %s"), name);
12442 goto error_return;
12443 }
12444 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12445 {
12446 _bfd_error_handler
12447 (_("warning: section '%s' is being made into a note"), name);
12448 bfd_set_error (bfd_error_nonrepresentable_section);
12449 goto error_return;
12450 }
12451 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
12452 break;
12453
12454 case DT_REL:
12455 case DT_RELA:
12456 case DT_RELSZ:
12457 case DT_RELASZ:
12458 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12459 type = SHT_REL;
12460 else
12461 type = SHT_RELA;
12462 sh_size = 0;
12463 sh_addr = 0;
12464 for (i = 1; i < elf_numsections (abfd); i++)
12465 {
12466 Elf_Internal_Shdr *hdr;
12467
12468 hdr = elf_elfsections (abfd)[i];
12469 if (hdr->sh_type == type
12470 && (hdr->sh_flags & SHF_ALLOC) != 0)
12471 {
12472 sh_size += hdr->sh_size;
12473 if (sh_addr == 0
12474 || sh_addr > hdr->sh_addr)
12475 sh_addr = hdr->sh_addr;
12476 }
12477 }
12478
12479 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12480 {
12481 /* Don't count procedure linkage table relocs in the
12482 overall reloc count. */
12483 sh_size -= htab->srelplt->size;
12484 if (sh_size == 0)
12485 /* If the size is zero, make the address zero too.
12486 This is to avoid a glibc bug. If the backend
12487 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12488 zero, then we'll put DT_RELA at the end of
12489 DT_JMPREL. glibc will interpret the end of
12490 DT_RELA matching the end of DT_JMPREL as the
12491 case where DT_RELA includes DT_JMPREL, and for
12492 LD_BIND_NOW will decide that processing DT_RELA
12493 will process the PLT relocs too. Net result:
12494 No PLT relocs applied. */
12495 sh_addr = 0;
12496
12497 /* If .rela.plt is the first .rela section, exclude
12498 it from DT_RELA. */
12499 else if (sh_addr == (htab->srelplt->output_section->vma
12500 + htab->srelplt->output_offset))
12501 sh_addr += htab->srelplt->size;
12502 }
12503
12504 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12505 dyn.d_un.d_val = sh_size;
12506 else
12507 dyn.d_un.d_ptr = sh_addr;
12508 break;
12509 }
12510 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12511 }
12512 }
12513
12514 /* If we have created any dynamic sections, then output them. */
12515 if (dynobj != NULL)
12516 {
12517 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12518 goto error_return;
12519
12520 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12521 if (((info->warn_shared_textrel && bfd_link_pic (info))
12522 || info->error_textrel)
12523 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
12524 {
12525 bfd_byte *dyncon, *dynconend;
12526
12527 dyncon = o->contents;
12528 dynconend = o->contents + o->size;
12529 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12530 {
12531 Elf_Internal_Dyn dyn;
12532
12533 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12534
12535 if (dyn.d_tag == DT_TEXTREL)
12536 {
12537 if (info->error_textrel)
12538 info->callbacks->einfo
12539 (_("%P%X: read-only segment has dynamic relocations.\n"));
12540 else
12541 info->callbacks->einfo
12542 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
12543 break;
12544 }
12545 }
12546 }
12547
12548 for (o = dynobj->sections; o != NULL; o = o->next)
12549 {
12550 if ((o->flags & SEC_HAS_CONTENTS) == 0
12551 || o->size == 0
12552 || o->output_section == bfd_abs_section_ptr)
12553 continue;
12554 if ((o->flags & SEC_LINKER_CREATED) == 0)
12555 {
12556 /* At this point, we are only interested in sections
12557 created by _bfd_elf_link_create_dynamic_sections. */
12558 continue;
12559 }
12560 if (htab->stab_info.stabstr == o)
12561 continue;
12562 if (htab->eh_info.hdr_sec == o)
12563 continue;
12564 if (strcmp (o->name, ".dynstr") != 0)
12565 {
12566 if (! bfd_set_section_contents (abfd, o->output_section,
12567 o->contents,
12568 (file_ptr) o->output_offset
12569 * bfd_octets_per_byte (abfd),
12570 o->size))
12571 goto error_return;
12572 }
12573 else
12574 {
12575 /* The contents of the .dynstr section are actually in a
12576 stringtab. */
12577 file_ptr off;
12578
12579 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12580 if (bfd_seek (abfd, off, SEEK_SET) != 0
12581 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
12582 goto error_return;
12583 }
12584 }
12585 }
12586
12587 if (!info->resolve_section_groups)
12588 {
12589 bfd_boolean failed = FALSE;
12590
12591 BFD_ASSERT (bfd_link_relocatable (info));
12592 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12593 if (failed)
12594 goto error_return;
12595 }
12596
12597 /* If we have optimized stabs strings, output them. */
12598 if (htab->stab_info.stabstr != NULL)
12599 {
12600 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
12601 goto error_return;
12602 }
12603
12604 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12605 goto error_return;
12606
12607 elf_final_link_free (abfd, &flinfo);
12608
12609 elf_linker (abfd) = TRUE;
12610
12611 if (attr_section)
12612 {
12613 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
12614 if (contents == NULL)
12615 return FALSE; /* Bail out and fail. */
12616 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12617 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12618 free (contents);
12619 }
12620
12621 return TRUE;
12622
12623 error_return:
12624 elf_final_link_free (abfd, &flinfo);
12625 return FALSE;
12626 }
12627 \f
12628 /* Initialize COOKIE for input bfd ABFD. */
12629
12630 static bfd_boolean
12631 init_reloc_cookie (struct elf_reloc_cookie *cookie,
12632 struct bfd_link_info *info, bfd *abfd)
12633 {
12634 Elf_Internal_Shdr *symtab_hdr;
12635 const struct elf_backend_data *bed;
12636
12637 bed = get_elf_backend_data (abfd);
12638 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12639
12640 cookie->abfd = abfd;
12641 cookie->sym_hashes = elf_sym_hashes (abfd);
12642 cookie->bad_symtab = elf_bad_symtab (abfd);
12643 if (cookie->bad_symtab)
12644 {
12645 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12646 cookie->extsymoff = 0;
12647 }
12648 else
12649 {
12650 cookie->locsymcount = symtab_hdr->sh_info;
12651 cookie->extsymoff = symtab_hdr->sh_info;
12652 }
12653
12654 if (bed->s->arch_size == 32)
12655 cookie->r_sym_shift = 8;
12656 else
12657 cookie->r_sym_shift = 32;
12658
12659 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12660 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12661 {
12662 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12663 cookie->locsymcount, 0,
12664 NULL, NULL, NULL);
12665 if (cookie->locsyms == NULL)
12666 {
12667 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12668 return FALSE;
12669 }
12670 if (info->keep_memory)
12671 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12672 }
12673 return TRUE;
12674 }
12675
12676 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12677
12678 static void
12679 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12680 {
12681 Elf_Internal_Shdr *symtab_hdr;
12682
12683 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12684 if (cookie->locsyms != NULL
12685 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12686 free (cookie->locsyms);
12687 }
12688
12689 /* Initialize the relocation information in COOKIE for input section SEC
12690 of input bfd ABFD. */
12691
12692 static bfd_boolean
12693 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12694 struct bfd_link_info *info, bfd *abfd,
12695 asection *sec)
12696 {
12697 if (sec->reloc_count == 0)
12698 {
12699 cookie->rels = NULL;
12700 cookie->relend = NULL;
12701 }
12702 else
12703 {
12704 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12705 info->keep_memory);
12706 if (cookie->rels == NULL)
12707 return FALSE;
12708 cookie->rel = cookie->rels;
12709 cookie->relend = cookie->rels + sec->reloc_count;
12710 }
12711 cookie->rel = cookie->rels;
12712 return TRUE;
12713 }
12714
12715 /* Free the memory allocated by init_reloc_cookie_rels,
12716 if appropriate. */
12717
12718 static void
12719 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12720 asection *sec)
12721 {
12722 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12723 free (cookie->rels);
12724 }
12725
12726 /* Initialize the whole of COOKIE for input section SEC. */
12727
12728 static bfd_boolean
12729 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12730 struct bfd_link_info *info,
12731 asection *sec)
12732 {
12733 if (!init_reloc_cookie (cookie, info, sec->owner))
12734 goto error1;
12735 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12736 goto error2;
12737 return TRUE;
12738
12739 error2:
12740 fini_reloc_cookie (cookie, sec->owner);
12741 error1:
12742 return FALSE;
12743 }
12744
12745 /* Free the memory allocated by init_reloc_cookie_for_section,
12746 if appropriate. */
12747
12748 static void
12749 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12750 asection *sec)
12751 {
12752 fini_reloc_cookie_rels (cookie, sec);
12753 fini_reloc_cookie (cookie, sec->owner);
12754 }
12755 \f
12756 /* Garbage collect unused sections. */
12757
12758 /* Default gc_mark_hook. */
12759
12760 asection *
12761 _bfd_elf_gc_mark_hook (asection *sec,
12762 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12763 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12764 struct elf_link_hash_entry *h,
12765 Elf_Internal_Sym *sym)
12766 {
12767 if (h != NULL)
12768 {
12769 switch (h->root.type)
12770 {
12771 case bfd_link_hash_defined:
12772 case bfd_link_hash_defweak:
12773 return h->root.u.def.section;
12774
12775 case bfd_link_hash_common:
12776 return h->root.u.c.p->section;
12777
12778 default:
12779 break;
12780 }
12781 }
12782 else
12783 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12784
12785 return NULL;
12786 }
12787
12788 /* Return the global debug definition section. */
12789
12790 static asection *
12791 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
12792 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12793 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12794 struct elf_link_hash_entry *h,
12795 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
12796 {
12797 if (h != NULL
12798 && (h->root.type == bfd_link_hash_defined
12799 || h->root.type == bfd_link_hash_defweak)
12800 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
12801 return h->root.u.def.section;
12802
12803 return NULL;
12804 }
12805
12806 /* COOKIE->rel describes a relocation against section SEC, which is
12807 a section we've decided to keep. Return the section that contains
12808 the relocation symbol, or NULL if no section contains it. */
12809
12810 asection *
12811 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12812 elf_gc_mark_hook_fn gc_mark_hook,
12813 struct elf_reloc_cookie *cookie,
12814 bfd_boolean *start_stop)
12815 {
12816 unsigned long r_symndx;
12817 struct elf_link_hash_entry *h;
12818
12819 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
12820 if (r_symndx == STN_UNDEF)
12821 return NULL;
12822
12823 if (r_symndx >= cookie->locsymcount
12824 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12825 {
12826 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
12827 if (h == NULL)
12828 {
12829 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12830 sec->owner);
12831 return NULL;
12832 }
12833 while (h->root.type == bfd_link_hash_indirect
12834 || h->root.type == bfd_link_hash_warning)
12835 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12836 h->mark = 1;
12837 /* If this symbol is weak and there is a non-weak definition, we
12838 keep the non-weak definition because many backends put
12839 dynamic reloc info on the non-weak definition for code
12840 handling copy relocs. */
12841 if (h->is_weakalias)
12842 weakdef (h)->mark = 1;
12843
12844 if (start_stop != NULL)
12845 {
12846 /* To work around a glibc bug, mark XXX input sections
12847 when there is a reference to __start_XXX or __stop_XXX
12848 symbols. */
12849 if (h->start_stop)
12850 {
12851 asection *s = h->u2.start_stop_section;
12852 *start_stop = !s->gc_mark;
12853 return s;
12854 }
12855 }
12856
12857 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12858 }
12859
12860 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12861 &cookie->locsyms[r_symndx]);
12862 }
12863
12864 /* COOKIE->rel describes a relocation against section SEC, which is
12865 a section we've decided to keep. Mark the section that contains
12866 the relocation symbol. */
12867
12868 bfd_boolean
12869 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12870 asection *sec,
12871 elf_gc_mark_hook_fn gc_mark_hook,
12872 struct elf_reloc_cookie *cookie)
12873 {
12874 asection *rsec;
12875 bfd_boolean start_stop = FALSE;
12876
12877 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12878 while (rsec != NULL)
12879 {
12880 if (!rsec->gc_mark)
12881 {
12882 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12883 || (rsec->owner->flags & DYNAMIC) != 0)
12884 rsec->gc_mark = 1;
12885 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12886 return FALSE;
12887 }
12888 if (!start_stop)
12889 break;
12890 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
12891 }
12892 return TRUE;
12893 }
12894
12895 /* The mark phase of garbage collection. For a given section, mark
12896 it and any sections in this section's group, and all the sections
12897 which define symbols to which it refers. */
12898
12899 bfd_boolean
12900 _bfd_elf_gc_mark (struct bfd_link_info *info,
12901 asection *sec,
12902 elf_gc_mark_hook_fn gc_mark_hook)
12903 {
12904 bfd_boolean ret;
12905 asection *group_sec, *eh_frame;
12906
12907 sec->gc_mark = 1;
12908
12909 /* Mark all the sections in the group. */
12910 group_sec = elf_section_data (sec)->next_in_group;
12911 if (group_sec && !group_sec->gc_mark)
12912 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
12913 return FALSE;
12914
12915 /* Look through the section relocs. */
12916 ret = TRUE;
12917 eh_frame = elf_eh_frame_section (sec->owner);
12918 if ((sec->flags & SEC_RELOC) != 0
12919 && sec->reloc_count > 0
12920 && sec != eh_frame)
12921 {
12922 struct elf_reloc_cookie cookie;
12923
12924 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12925 ret = FALSE;
12926 else
12927 {
12928 for (; cookie.rel < cookie.relend; cookie.rel++)
12929 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
12930 {
12931 ret = FALSE;
12932 break;
12933 }
12934 fini_reloc_cookie_for_section (&cookie, sec);
12935 }
12936 }
12937
12938 if (ret && eh_frame && elf_fde_list (sec))
12939 {
12940 struct elf_reloc_cookie cookie;
12941
12942 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12943 ret = FALSE;
12944 else
12945 {
12946 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12947 gc_mark_hook, &cookie))
12948 ret = FALSE;
12949 fini_reloc_cookie_for_section (&cookie, eh_frame);
12950 }
12951 }
12952
12953 eh_frame = elf_section_eh_frame_entry (sec);
12954 if (ret && eh_frame && !eh_frame->gc_mark)
12955 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12956 ret = FALSE;
12957
12958 return ret;
12959 }
12960
12961 /* Scan and mark sections in a special or debug section group. */
12962
12963 static void
12964 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12965 {
12966 /* Point to first section of section group. */
12967 asection *ssec;
12968 /* Used to iterate the section group. */
12969 asection *msec;
12970
12971 bfd_boolean is_special_grp = TRUE;
12972 bfd_boolean is_debug_grp = TRUE;
12973
12974 /* First scan to see if group contains any section other than debug
12975 and special section. */
12976 ssec = msec = elf_next_in_group (grp);
12977 do
12978 {
12979 if ((msec->flags & SEC_DEBUGGING) == 0)
12980 is_debug_grp = FALSE;
12981
12982 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12983 is_special_grp = FALSE;
12984
12985 msec = elf_next_in_group (msec);
12986 }
12987 while (msec != ssec);
12988
12989 /* If this is a pure debug section group or pure special section group,
12990 keep all sections in this group. */
12991 if (is_debug_grp || is_special_grp)
12992 {
12993 do
12994 {
12995 msec->gc_mark = 1;
12996 msec = elf_next_in_group (msec);
12997 }
12998 while (msec != ssec);
12999 }
13000 }
13001
13002 /* Keep debug and special sections. */
13003
13004 bfd_boolean
13005 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13006 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
13007 {
13008 bfd *ibfd;
13009
13010 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13011 {
13012 asection *isec;
13013 bfd_boolean some_kept;
13014 bfd_boolean debug_frag_seen;
13015 bfd_boolean has_kept_debug_info;
13016
13017 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13018 continue;
13019 isec = ibfd->sections;
13020 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13021 continue;
13022
13023 /* Ensure all linker created sections are kept,
13024 see if any other section is already marked,
13025 and note if we have any fragmented debug sections. */
13026 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
13027 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13028 {
13029 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13030 isec->gc_mark = 1;
13031 else if (isec->gc_mark
13032 && (isec->flags & SEC_ALLOC) != 0
13033 && elf_section_type (isec) != SHT_NOTE)
13034 some_kept = TRUE;
13035
13036 if (!debug_frag_seen
13037 && (isec->flags & SEC_DEBUGGING)
13038 && CONST_STRNEQ (isec->name, ".debug_line."))
13039 debug_frag_seen = TRUE;
13040 }
13041
13042 /* If no non-note alloc section in this file will be kept, then
13043 we can toss out the debug and special sections. */
13044 if (!some_kept)
13045 continue;
13046
13047 /* Keep debug and special sections like .comment when they are
13048 not part of a group. Also keep section groups that contain
13049 just debug sections or special sections. */
13050 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13051 {
13052 if ((isec->flags & SEC_GROUP) != 0)
13053 _bfd_elf_gc_mark_debug_special_section_group (isec);
13054 else if (((isec->flags & SEC_DEBUGGING) != 0
13055 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13056 && elf_next_in_group (isec) == NULL)
13057 isec->gc_mark = 1;
13058 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13059 has_kept_debug_info = TRUE;
13060 }
13061
13062 /* Look for CODE sections which are going to be discarded,
13063 and find and discard any fragmented debug sections which
13064 are associated with that code section. */
13065 if (debug_frag_seen)
13066 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13067 if ((isec->flags & SEC_CODE) != 0
13068 && isec->gc_mark == 0)
13069 {
13070 unsigned int ilen;
13071 asection *dsec;
13072
13073 ilen = strlen (isec->name);
13074
13075 /* Association is determined by the name of the debug
13076 section containing the name of the code section as
13077 a suffix. For example .debug_line.text.foo is a
13078 debug section associated with .text.foo. */
13079 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13080 {
13081 unsigned int dlen;
13082
13083 if (dsec->gc_mark == 0
13084 || (dsec->flags & SEC_DEBUGGING) == 0)
13085 continue;
13086
13087 dlen = strlen (dsec->name);
13088
13089 if (dlen > ilen
13090 && strncmp (dsec->name + (dlen - ilen),
13091 isec->name, ilen) == 0)
13092 dsec->gc_mark = 0;
13093 }
13094 }
13095
13096 /* Mark debug sections referenced by kept debug sections. */
13097 if (has_kept_debug_info)
13098 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13099 if (isec->gc_mark
13100 && (isec->flags & SEC_DEBUGGING) != 0)
13101 if (!_bfd_elf_gc_mark (info, isec,
13102 elf_gc_mark_debug_section))
13103 return FALSE;
13104 }
13105 return TRUE;
13106 }
13107
13108 static bfd_boolean
13109 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13110 {
13111 bfd *sub;
13112 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13113
13114 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13115 {
13116 asection *o;
13117
13118 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13119 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
13120 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13121 continue;
13122 o = sub->sections;
13123 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13124 continue;
13125
13126 for (o = sub->sections; o != NULL; o = o->next)
13127 {
13128 /* When any section in a section group is kept, we keep all
13129 sections in the section group. If the first member of
13130 the section group is excluded, we will also exclude the
13131 group section. */
13132 if (o->flags & SEC_GROUP)
13133 {
13134 asection *first = elf_next_in_group (o);
13135 o->gc_mark = first->gc_mark;
13136 }
13137
13138 if (o->gc_mark)
13139 continue;
13140
13141 /* Skip sweeping sections already excluded. */
13142 if (o->flags & SEC_EXCLUDE)
13143 continue;
13144
13145 /* Since this is early in the link process, it is simple
13146 to remove a section from the output. */
13147 o->flags |= SEC_EXCLUDE;
13148
13149 if (info->print_gc_sections && o->size != 0)
13150 /* xgettext:c-format */
13151 _bfd_error_handler (_("Removing unused section '%A' in file '%B'"),
13152 o, sub);
13153 }
13154 }
13155
13156 return TRUE;
13157 }
13158
13159 /* Propagate collected vtable information. This is called through
13160 elf_link_hash_traverse. */
13161
13162 static bfd_boolean
13163 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13164 {
13165 /* Those that are not vtables. */
13166 if (h->start_stop
13167 || h->u2.vtable == NULL
13168 || h->u2.vtable->parent == NULL)
13169 return TRUE;
13170
13171 /* Those vtables that do not have parents, we cannot merge. */
13172 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
13173 return TRUE;
13174
13175 /* If we've already been done, exit. */
13176 if (h->u2.vtable->used && h->u2.vtable->used[-1])
13177 return TRUE;
13178
13179 /* Make sure the parent's table is up to date. */
13180 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
13181
13182 if (h->u2.vtable->used == NULL)
13183 {
13184 /* None of this table's entries were referenced. Re-use the
13185 parent's table. */
13186 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13187 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
13188 }
13189 else
13190 {
13191 size_t n;
13192 bfd_boolean *cu, *pu;
13193
13194 /* Or the parent's entries into ours. */
13195 cu = h->u2.vtable->used;
13196 cu[-1] = TRUE;
13197 pu = h->u2.vtable->parent->u2.vtable->used;
13198 if (pu != NULL)
13199 {
13200 const struct elf_backend_data *bed;
13201 unsigned int log_file_align;
13202
13203 bed = get_elf_backend_data (h->root.u.def.section->owner);
13204 log_file_align = bed->s->log_file_align;
13205 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
13206 while (n--)
13207 {
13208 if (*pu)
13209 *cu = TRUE;
13210 pu++;
13211 cu++;
13212 }
13213 }
13214 }
13215
13216 return TRUE;
13217 }
13218
13219 static bfd_boolean
13220 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13221 {
13222 asection *sec;
13223 bfd_vma hstart, hend;
13224 Elf_Internal_Rela *relstart, *relend, *rel;
13225 const struct elf_backend_data *bed;
13226 unsigned int log_file_align;
13227
13228 /* Take care of both those symbols that do not describe vtables as
13229 well as those that are not loaded. */
13230 if (h->start_stop
13231 || h->u2.vtable == NULL
13232 || h->u2.vtable->parent == NULL)
13233 return TRUE;
13234
13235 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13236 || h->root.type == bfd_link_hash_defweak);
13237
13238 sec = h->root.u.def.section;
13239 hstart = h->root.u.def.value;
13240 hend = hstart + h->size;
13241
13242 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13243 if (!relstart)
13244 return *(bfd_boolean *) okp = FALSE;
13245 bed = get_elf_backend_data (sec->owner);
13246 log_file_align = bed->s->log_file_align;
13247
13248 relend = relstart + sec->reloc_count;
13249
13250 for (rel = relstart; rel < relend; ++rel)
13251 if (rel->r_offset >= hstart && rel->r_offset < hend)
13252 {
13253 /* If the entry is in use, do nothing. */
13254 if (h->u2.vtable->used
13255 && (rel->r_offset - hstart) < h->u2.vtable->size)
13256 {
13257 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13258 if (h->u2.vtable->used[entry])
13259 continue;
13260 }
13261 /* Otherwise, kill it. */
13262 rel->r_offset = rel->r_info = rel->r_addend = 0;
13263 }
13264
13265 return TRUE;
13266 }
13267
13268 /* Mark sections containing dynamically referenced symbols. When
13269 building shared libraries, we must assume that any visible symbol is
13270 referenced. */
13271
13272 bfd_boolean
13273 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13274 {
13275 struct bfd_link_info *info = (struct bfd_link_info *) inf;
13276 struct bfd_elf_dynamic_list *d = info->dynamic_list;
13277
13278 if ((h->root.type == bfd_link_hash_defined
13279 || h->root.type == bfd_link_hash_defweak)
13280 && (h->ref_dynamic
13281 || ((h->def_regular || ELF_COMMON_DEF_P (h))
13282 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
13283 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
13284 && (!bfd_link_executable (info)
13285 || info->gc_keep_exported
13286 || info->export_dynamic
13287 || (h->dynamic
13288 && d != NULL
13289 && (*d->match) (&d->head, NULL, h->root.root.string)))
13290 && (h->versioned >= versioned
13291 || !bfd_hide_sym_by_version (info->version_info,
13292 h->root.root.string)))))
13293 h->root.u.def.section->flags |= SEC_KEEP;
13294
13295 return TRUE;
13296 }
13297
13298 /* Keep all sections containing symbols undefined on the command-line,
13299 and the section containing the entry symbol. */
13300
13301 void
13302 _bfd_elf_gc_keep (struct bfd_link_info *info)
13303 {
13304 struct bfd_sym_chain *sym;
13305
13306 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13307 {
13308 struct elf_link_hash_entry *h;
13309
13310 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13311 FALSE, FALSE, FALSE);
13312
13313 if (h != NULL
13314 && (h->root.type == bfd_link_hash_defined
13315 || h->root.type == bfd_link_hash_defweak)
13316 && !bfd_is_abs_section (h->root.u.def.section)
13317 && !bfd_is_und_section (h->root.u.def.section))
13318 h->root.u.def.section->flags |= SEC_KEEP;
13319 }
13320 }
13321
13322 bfd_boolean
13323 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13324 struct bfd_link_info *info)
13325 {
13326 bfd *ibfd = info->input_bfds;
13327
13328 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13329 {
13330 asection *sec;
13331 struct elf_reloc_cookie cookie;
13332
13333 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13334 continue;
13335 sec = ibfd->sections;
13336 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13337 continue;
13338
13339 if (!init_reloc_cookie (&cookie, info, ibfd))
13340 return FALSE;
13341
13342 for (sec = ibfd->sections; sec; sec = sec->next)
13343 {
13344 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13345 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13346 {
13347 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13348 fini_reloc_cookie_rels (&cookie, sec);
13349 }
13350 }
13351 }
13352 return TRUE;
13353 }
13354
13355 /* Do mark and sweep of unused sections. */
13356
13357 bfd_boolean
13358 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13359 {
13360 bfd_boolean ok = TRUE;
13361 bfd *sub;
13362 elf_gc_mark_hook_fn gc_mark_hook;
13363 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13364 struct elf_link_hash_table *htab;
13365
13366 if (!bed->can_gc_sections
13367 || !is_elf_hash_table (info->hash))
13368 {
13369 _bfd_error_handler(_("Warning: gc-sections option ignored"));
13370 return TRUE;
13371 }
13372
13373 bed->gc_keep (info);
13374 htab = elf_hash_table (info);
13375
13376 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13377 at the .eh_frame section if we can mark the FDEs individually. */
13378 for (sub = info->input_bfds;
13379 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13380 sub = sub->link.next)
13381 {
13382 asection *sec;
13383 struct elf_reloc_cookie cookie;
13384
13385 sec = sub->sections;
13386 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13387 continue;
13388 sec = bfd_get_section_by_name (sub, ".eh_frame");
13389 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
13390 {
13391 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
13392 if (elf_section_data (sec)->sec_info
13393 && (sec->flags & SEC_LINKER_CREATED) == 0)
13394 elf_eh_frame_section (sub) = sec;
13395 fini_reloc_cookie_for_section (&cookie, sec);
13396 sec = bfd_get_next_section_by_name (NULL, sec);
13397 }
13398 }
13399
13400 /* Apply transitive closure to the vtable entry usage info. */
13401 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13402 if (!ok)
13403 return FALSE;
13404
13405 /* Kill the vtable relocations that were not used. */
13406 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13407 if (!ok)
13408 return FALSE;
13409
13410 /* Mark dynamically referenced symbols. */
13411 if (htab->dynamic_sections_created || info->gc_keep_exported)
13412 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13413
13414 /* Grovel through relocs to find out who stays ... */
13415 gc_mark_hook = bed->gc_mark_hook;
13416 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13417 {
13418 asection *o;
13419
13420 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13421 || elf_object_id (sub) != elf_hash_table_id (htab)
13422 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13423 continue;
13424
13425 o = sub->sections;
13426 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13427 continue;
13428
13429 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13430 Also treat note sections as a root, if the section is not part
13431 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13432 well as FINI_ARRAY sections for ld -r. */
13433 for (o = sub->sections; o != NULL; o = o->next)
13434 if (!o->gc_mark
13435 && (o->flags & SEC_EXCLUDE) == 0
13436 && ((o->flags & SEC_KEEP) != 0
13437 || (bfd_link_relocatable (info)
13438 && ((elf_section_data (o)->this_hdr.sh_type
13439 == SHT_PREINIT_ARRAY)
13440 || (elf_section_data (o)->this_hdr.sh_type
13441 == SHT_INIT_ARRAY)
13442 || (elf_section_data (o)->this_hdr.sh_type
13443 == SHT_FINI_ARRAY)))
13444 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13445 && elf_next_in_group (o) == NULL )))
13446 {
13447 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13448 return FALSE;
13449 }
13450 }
13451
13452 /* Allow the backend to mark additional target specific sections. */
13453 bed->gc_mark_extra_sections (info, gc_mark_hook);
13454
13455 /* ... and mark SEC_EXCLUDE for those that go. */
13456 return elf_gc_sweep (abfd, info);
13457 }
13458 \f
13459 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13460
13461 bfd_boolean
13462 bfd_elf_gc_record_vtinherit (bfd *abfd,
13463 asection *sec,
13464 struct elf_link_hash_entry *h,
13465 bfd_vma offset)
13466 {
13467 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13468 struct elf_link_hash_entry **search, *child;
13469 size_t extsymcount;
13470 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13471
13472 /* The sh_info field of the symtab header tells us where the
13473 external symbols start. We don't care about the local symbols at
13474 this point. */
13475 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13476 if (!elf_bad_symtab (abfd))
13477 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13478
13479 sym_hashes = elf_sym_hashes (abfd);
13480 sym_hashes_end = sym_hashes + extsymcount;
13481
13482 /* Hunt down the child symbol, which is in this section at the same
13483 offset as the relocation. */
13484 for (search = sym_hashes; search != sym_hashes_end; ++search)
13485 {
13486 if ((child = *search) != NULL
13487 && (child->root.type == bfd_link_hash_defined
13488 || child->root.type == bfd_link_hash_defweak)
13489 && child->root.u.def.section == sec
13490 && child->root.u.def.value == offset)
13491 goto win;
13492 }
13493
13494 /* xgettext:c-format */
13495 _bfd_error_handler (_("%B: %A+%#Lx: No symbol found for INHERIT"),
13496 abfd, sec, offset);
13497 bfd_set_error (bfd_error_invalid_operation);
13498 return FALSE;
13499
13500 win:
13501 if (!child->u2.vtable)
13502 {
13503 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13504 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13505 if (!child->u2.vtable)
13506 return FALSE;
13507 }
13508 if (!h)
13509 {
13510 /* This *should* only be the absolute section. It could potentially
13511 be that someone has defined a non-global vtable though, which
13512 would be bad. It isn't worth paging in the local symbols to be
13513 sure though; that case should simply be handled by the assembler. */
13514
13515 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
13516 }
13517 else
13518 child->u2.vtable->parent = h;
13519
13520 return TRUE;
13521 }
13522
13523 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13524
13525 bfd_boolean
13526 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13527 asection *sec ATTRIBUTE_UNUSED,
13528 struct elf_link_hash_entry *h,
13529 bfd_vma addend)
13530 {
13531 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13532 unsigned int log_file_align = bed->s->log_file_align;
13533
13534 if (!h->u2.vtable)
13535 {
13536 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13537 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13538 if (!h->u2.vtable)
13539 return FALSE;
13540 }
13541
13542 if (addend >= h->u2.vtable->size)
13543 {
13544 size_t size, bytes, file_align;
13545 bfd_boolean *ptr = h->u2.vtable->used;
13546
13547 /* While the symbol is undefined, we have to be prepared to handle
13548 a zero size. */
13549 file_align = 1 << log_file_align;
13550 if (h->root.type == bfd_link_hash_undefined)
13551 size = addend + file_align;
13552 else
13553 {
13554 size = h->size;
13555 if (addend >= size)
13556 {
13557 /* Oops! We've got a reference past the defined end of
13558 the table. This is probably a bug -- shall we warn? */
13559 size = addend + file_align;
13560 }
13561 }
13562 size = (size + file_align - 1) & -file_align;
13563
13564 /* Allocate one extra entry for use as a "done" flag for the
13565 consolidation pass. */
13566 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13567
13568 if (ptr)
13569 {
13570 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
13571
13572 if (ptr != NULL)
13573 {
13574 size_t oldbytes;
13575
13576 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
13577 * sizeof (bfd_boolean));
13578 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13579 }
13580 }
13581 else
13582 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13583
13584 if (ptr == NULL)
13585 return FALSE;
13586
13587 /* And arrange for that done flag to be at index -1. */
13588 h->u2.vtable->used = ptr + 1;
13589 h->u2.vtable->size = size;
13590 }
13591
13592 h->u2.vtable->used[addend >> log_file_align] = TRUE;
13593
13594 return TRUE;
13595 }
13596
13597 /* Map an ELF section header flag to its corresponding string. */
13598 typedef struct
13599 {
13600 char *flag_name;
13601 flagword flag_value;
13602 } elf_flags_to_name_table;
13603
13604 static elf_flags_to_name_table elf_flags_to_names [] =
13605 {
13606 { "SHF_WRITE", SHF_WRITE },
13607 { "SHF_ALLOC", SHF_ALLOC },
13608 { "SHF_EXECINSTR", SHF_EXECINSTR },
13609 { "SHF_MERGE", SHF_MERGE },
13610 { "SHF_STRINGS", SHF_STRINGS },
13611 { "SHF_INFO_LINK", SHF_INFO_LINK},
13612 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13613 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13614 { "SHF_GROUP", SHF_GROUP },
13615 { "SHF_TLS", SHF_TLS },
13616 { "SHF_MASKOS", SHF_MASKOS },
13617 { "SHF_EXCLUDE", SHF_EXCLUDE },
13618 };
13619
13620 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13621 bfd_boolean
13622 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13623 struct flag_info *flaginfo,
13624 asection *section)
13625 {
13626 const bfd_vma sh_flags = elf_section_flags (section);
13627
13628 if (!flaginfo->flags_initialized)
13629 {
13630 bfd *obfd = info->output_bfd;
13631 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13632 struct flag_info_list *tf = flaginfo->flag_list;
13633 int with_hex = 0;
13634 int without_hex = 0;
13635
13636 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13637 {
13638 unsigned i;
13639 flagword (*lookup) (char *);
13640
13641 lookup = bed->elf_backend_lookup_section_flags_hook;
13642 if (lookup != NULL)
13643 {
13644 flagword hexval = (*lookup) ((char *) tf->name);
13645
13646 if (hexval != 0)
13647 {
13648 if (tf->with == with_flags)
13649 with_hex |= hexval;
13650 else if (tf->with == without_flags)
13651 without_hex |= hexval;
13652 tf->valid = TRUE;
13653 continue;
13654 }
13655 }
13656 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13657 {
13658 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13659 {
13660 if (tf->with == with_flags)
13661 with_hex |= elf_flags_to_names[i].flag_value;
13662 else if (tf->with == without_flags)
13663 without_hex |= elf_flags_to_names[i].flag_value;
13664 tf->valid = TRUE;
13665 break;
13666 }
13667 }
13668 if (!tf->valid)
13669 {
13670 info->callbacks->einfo
13671 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13672 return FALSE;
13673 }
13674 }
13675 flaginfo->flags_initialized = TRUE;
13676 flaginfo->only_with_flags |= with_hex;
13677 flaginfo->not_with_flags |= without_hex;
13678 }
13679
13680 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13681 return FALSE;
13682
13683 if ((flaginfo->not_with_flags & sh_flags) != 0)
13684 return FALSE;
13685
13686 return TRUE;
13687 }
13688
13689 struct alloc_got_off_arg {
13690 bfd_vma gotoff;
13691 struct bfd_link_info *info;
13692 };
13693
13694 /* We need a special top-level link routine to convert got reference counts
13695 to real got offsets. */
13696
13697 static bfd_boolean
13698 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13699 {
13700 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13701 bfd *obfd = gofarg->info->output_bfd;
13702 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13703
13704 if (h->got.refcount > 0)
13705 {
13706 h->got.offset = gofarg->gotoff;
13707 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13708 }
13709 else
13710 h->got.offset = (bfd_vma) -1;
13711
13712 return TRUE;
13713 }
13714
13715 /* And an accompanying bit to work out final got entry offsets once
13716 we're done. Should be called from final_link. */
13717
13718 bfd_boolean
13719 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13720 struct bfd_link_info *info)
13721 {
13722 bfd *i;
13723 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13724 bfd_vma gotoff;
13725 struct alloc_got_off_arg gofarg;
13726
13727 BFD_ASSERT (abfd == info->output_bfd);
13728
13729 if (! is_elf_hash_table (info->hash))
13730 return FALSE;
13731
13732 /* The GOT offset is relative to the .got section, but the GOT header is
13733 put into the .got.plt section, if the backend uses it. */
13734 if (bed->want_got_plt)
13735 gotoff = 0;
13736 else
13737 gotoff = bed->got_header_size;
13738
13739 /* Do the local .got entries first. */
13740 for (i = info->input_bfds; i; i = i->link.next)
13741 {
13742 bfd_signed_vma *local_got;
13743 size_t j, locsymcount;
13744 Elf_Internal_Shdr *symtab_hdr;
13745
13746 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13747 continue;
13748
13749 local_got = elf_local_got_refcounts (i);
13750 if (!local_got)
13751 continue;
13752
13753 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13754 if (elf_bad_symtab (i))
13755 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13756 else
13757 locsymcount = symtab_hdr->sh_info;
13758
13759 for (j = 0; j < locsymcount; ++j)
13760 {
13761 if (local_got[j] > 0)
13762 {
13763 local_got[j] = gotoff;
13764 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13765 }
13766 else
13767 local_got[j] = (bfd_vma) -1;
13768 }
13769 }
13770
13771 /* Then the global .got entries. .plt refcounts are handled by
13772 adjust_dynamic_symbol */
13773 gofarg.gotoff = gotoff;
13774 gofarg.info = info;
13775 elf_link_hash_traverse (elf_hash_table (info),
13776 elf_gc_allocate_got_offsets,
13777 &gofarg);
13778 return TRUE;
13779 }
13780
13781 /* Many folk need no more in the way of final link than this, once
13782 got entry reference counting is enabled. */
13783
13784 bfd_boolean
13785 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13786 {
13787 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13788 return FALSE;
13789
13790 /* Invoke the regular ELF backend linker to do all the work. */
13791 return bfd_elf_final_link (abfd, info);
13792 }
13793
13794 bfd_boolean
13795 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13796 {
13797 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13798
13799 if (rcookie->bad_symtab)
13800 rcookie->rel = rcookie->rels;
13801
13802 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13803 {
13804 unsigned long r_symndx;
13805
13806 if (! rcookie->bad_symtab)
13807 if (rcookie->rel->r_offset > offset)
13808 return FALSE;
13809 if (rcookie->rel->r_offset != offset)
13810 continue;
13811
13812 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
13813 if (r_symndx == STN_UNDEF)
13814 return TRUE;
13815
13816 if (r_symndx >= rcookie->locsymcount
13817 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13818 {
13819 struct elf_link_hash_entry *h;
13820
13821 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13822
13823 while (h->root.type == bfd_link_hash_indirect
13824 || h->root.type == bfd_link_hash_warning)
13825 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13826
13827 if ((h->root.type == bfd_link_hash_defined
13828 || h->root.type == bfd_link_hash_defweak)
13829 && (h->root.u.def.section->owner != rcookie->abfd
13830 || h->root.u.def.section->kept_section != NULL
13831 || discarded_section (h->root.u.def.section)))
13832 return TRUE;
13833 }
13834 else
13835 {
13836 /* It's not a relocation against a global symbol,
13837 but it could be a relocation against a local
13838 symbol for a discarded section. */
13839 asection *isec;
13840 Elf_Internal_Sym *isym;
13841
13842 /* Need to: get the symbol; get the section. */
13843 isym = &rcookie->locsyms[r_symndx];
13844 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
13845 if (isec != NULL
13846 && (isec->kept_section != NULL
13847 || discarded_section (isec)))
13848 return TRUE;
13849 }
13850 return FALSE;
13851 }
13852 return FALSE;
13853 }
13854
13855 /* Discard unneeded references to discarded sections.
13856 Returns -1 on error, 1 if any section's size was changed, 0 if
13857 nothing changed. This function assumes that the relocations are in
13858 sorted order, which is true for all known assemblers. */
13859
13860 int
13861 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13862 {
13863 struct elf_reloc_cookie cookie;
13864 asection *o;
13865 bfd *abfd;
13866 int changed = 0;
13867
13868 if (info->traditional_format
13869 || !is_elf_hash_table (info->hash))
13870 return 0;
13871
13872 o = bfd_get_section_by_name (output_bfd, ".stab");
13873 if (o != NULL)
13874 {
13875 asection *i;
13876
13877 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13878 {
13879 if (i->size == 0
13880 || i->reloc_count == 0
13881 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13882 continue;
13883
13884 abfd = i->owner;
13885 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13886 continue;
13887
13888 if (!init_reloc_cookie_for_section (&cookie, info, i))
13889 return -1;
13890
13891 if (_bfd_discard_section_stabs (abfd, i,
13892 elf_section_data (i)->sec_info,
13893 bfd_elf_reloc_symbol_deleted_p,
13894 &cookie))
13895 changed = 1;
13896
13897 fini_reloc_cookie_for_section (&cookie, i);
13898 }
13899 }
13900
13901 o = NULL;
13902 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13903 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
13904 if (o != NULL)
13905 {
13906 asection *i;
13907 int eh_changed = 0;
13908 unsigned int eh_alignment;
13909
13910 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13911 {
13912 if (i->size == 0)
13913 continue;
13914
13915 abfd = i->owner;
13916 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13917 continue;
13918
13919 if (!init_reloc_cookie_for_section (&cookie, info, i))
13920 return -1;
13921
13922 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13923 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
13924 bfd_elf_reloc_symbol_deleted_p,
13925 &cookie))
13926 {
13927 eh_changed = 1;
13928 if (i->size != i->rawsize)
13929 changed = 1;
13930 }
13931
13932 fini_reloc_cookie_for_section (&cookie, i);
13933 }
13934
13935 eh_alignment = 1 << o->alignment_power;
13936 /* Skip over zero terminator, and prevent empty sections from
13937 adding alignment padding at the end. */
13938 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
13939 if (i->size == 0)
13940 i->flags |= SEC_EXCLUDE;
13941 else if (i->size > 4)
13942 break;
13943 /* The last non-empty eh_frame section doesn't need padding. */
13944 if (i != NULL)
13945 i = i->map_tail.s;
13946 /* Any prior sections must pad the last FDE out to the output
13947 section alignment. Otherwise we might have zero padding
13948 between sections, which would be seen as a terminator. */
13949 for (; i != NULL; i = i->map_tail.s)
13950 if (i->size == 4)
13951 /* All but the last zero terminator should have been removed. */
13952 BFD_FAIL ();
13953 else
13954 {
13955 bfd_size_type size
13956 = (i->size + eh_alignment - 1) & -eh_alignment;
13957 if (i->size != size)
13958 {
13959 i->size = size;
13960 changed = 1;
13961 eh_changed = 1;
13962 }
13963 }
13964 if (eh_changed)
13965 elf_link_hash_traverse (elf_hash_table (info),
13966 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
13967 }
13968
13969 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13970 {
13971 const struct elf_backend_data *bed;
13972 asection *s;
13973
13974 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13975 continue;
13976 s = abfd->sections;
13977 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13978 continue;
13979
13980 bed = get_elf_backend_data (abfd);
13981
13982 if (bed->elf_backend_discard_info != NULL)
13983 {
13984 if (!init_reloc_cookie (&cookie, info, abfd))
13985 return -1;
13986
13987 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
13988 changed = 1;
13989
13990 fini_reloc_cookie (&cookie, abfd);
13991 }
13992 }
13993
13994 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13995 _bfd_elf_end_eh_frame_parsing (info);
13996
13997 if (info->eh_frame_hdr_type
13998 && !bfd_link_relocatable (info)
13999 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
14000 changed = 1;
14001
14002 return changed;
14003 }
14004
14005 bfd_boolean
14006 _bfd_elf_section_already_linked (bfd *abfd,
14007 asection *sec,
14008 struct bfd_link_info *info)
14009 {
14010 flagword flags;
14011 const char *name, *key;
14012 struct bfd_section_already_linked *l;
14013 struct bfd_section_already_linked_hash_entry *already_linked_list;
14014
14015 if (sec->output_section == bfd_abs_section_ptr)
14016 return FALSE;
14017
14018 flags = sec->flags;
14019
14020 /* Return if it isn't a linkonce section. A comdat group section
14021 also has SEC_LINK_ONCE set. */
14022 if ((flags & SEC_LINK_ONCE) == 0)
14023 return FALSE;
14024
14025 /* Don't put group member sections on our list of already linked
14026 sections. They are handled as a group via their group section. */
14027 if (elf_sec_group (sec) != NULL)
14028 return FALSE;
14029
14030 /* For a SHT_GROUP section, use the group signature as the key. */
14031 name = sec->name;
14032 if ((flags & SEC_GROUP) != 0
14033 && elf_next_in_group (sec) != NULL
14034 && elf_group_name (elf_next_in_group (sec)) != NULL)
14035 key = elf_group_name (elf_next_in_group (sec));
14036 else
14037 {
14038 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14039 if (CONST_STRNEQ (name, ".gnu.linkonce.")
14040 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14041 key++;
14042 else
14043 /* Must be a user linkonce section that doesn't follow gcc's
14044 naming convention. In this case we won't be matching
14045 single member groups. */
14046 key = name;
14047 }
14048
14049 already_linked_list = bfd_section_already_linked_table_lookup (key);
14050
14051 for (l = already_linked_list->entry; l != NULL; l = l->next)
14052 {
14053 /* We may have 2 different types of sections on the list: group
14054 sections with a signature of <key> (<key> is some string),
14055 and linkonce sections named .gnu.linkonce.<type>.<key>.
14056 Match like sections. LTO plugin sections are an exception.
14057 They are always named .gnu.linkonce.t.<key> and match either
14058 type of section. */
14059 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14060 && ((flags & SEC_GROUP) != 0
14061 || strcmp (name, l->sec->name) == 0))
14062 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
14063 {
14064 /* The section has already been linked. See if we should
14065 issue a warning. */
14066 if (!_bfd_handle_already_linked (sec, l, info))
14067 return FALSE;
14068
14069 if (flags & SEC_GROUP)
14070 {
14071 asection *first = elf_next_in_group (sec);
14072 asection *s = first;
14073
14074 while (s != NULL)
14075 {
14076 s->output_section = bfd_abs_section_ptr;
14077 /* Record which group discards it. */
14078 s->kept_section = l->sec;
14079 s = elf_next_in_group (s);
14080 /* These lists are circular. */
14081 if (s == first)
14082 break;
14083 }
14084 }
14085
14086 return TRUE;
14087 }
14088 }
14089
14090 /* A single member comdat group section may be discarded by a
14091 linkonce section and vice versa. */
14092 if ((flags & SEC_GROUP) != 0)
14093 {
14094 asection *first = elf_next_in_group (sec);
14095
14096 if (first != NULL && elf_next_in_group (first) == first)
14097 /* Check this single member group against linkonce sections. */
14098 for (l = already_linked_list->entry; l != NULL; l = l->next)
14099 if ((l->sec->flags & SEC_GROUP) == 0
14100 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14101 {
14102 first->output_section = bfd_abs_section_ptr;
14103 first->kept_section = l->sec;
14104 sec->output_section = bfd_abs_section_ptr;
14105 break;
14106 }
14107 }
14108 else
14109 /* Check this linkonce section against single member groups. */
14110 for (l = already_linked_list->entry; l != NULL; l = l->next)
14111 if (l->sec->flags & SEC_GROUP)
14112 {
14113 asection *first = elf_next_in_group (l->sec);
14114
14115 if (first != NULL
14116 && elf_next_in_group (first) == first
14117 && bfd_elf_match_symbols_in_sections (first, sec, info))
14118 {
14119 sec->output_section = bfd_abs_section_ptr;
14120 sec->kept_section = first;
14121 break;
14122 }
14123 }
14124
14125 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14126 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14127 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14128 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14129 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14130 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14131 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14132 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14133 The reverse order cannot happen as there is never a bfd with only the
14134 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14135 matter as here were are looking only for cross-bfd sections. */
14136
14137 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14138 for (l = already_linked_list->entry; l != NULL; l = l->next)
14139 if ((l->sec->flags & SEC_GROUP) == 0
14140 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14141 {
14142 if (abfd != l->sec->owner)
14143 sec->output_section = bfd_abs_section_ptr;
14144 break;
14145 }
14146
14147 /* This is the first section with this name. Record it. */
14148 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
14149 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
14150 return sec->output_section == bfd_abs_section_ptr;
14151 }
14152
14153 bfd_boolean
14154 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
14155 {
14156 return sym->st_shndx == SHN_COMMON;
14157 }
14158
14159 unsigned int
14160 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14161 {
14162 return SHN_COMMON;
14163 }
14164
14165 asection *
14166 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14167 {
14168 return bfd_com_section_ptr;
14169 }
14170
14171 bfd_vma
14172 _bfd_elf_default_got_elt_size (bfd *abfd,
14173 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14174 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14175 bfd *ibfd ATTRIBUTE_UNUSED,
14176 unsigned long symndx ATTRIBUTE_UNUSED)
14177 {
14178 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14179 return bed->s->arch_size / 8;
14180 }
14181
14182 /* Routines to support the creation of dynamic relocs. */
14183
14184 /* Returns the name of the dynamic reloc section associated with SEC. */
14185
14186 static const char *
14187 get_dynamic_reloc_section_name (bfd * abfd,
14188 asection * sec,
14189 bfd_boolean is_rela)
14190 {
14191 char *name;
14192 const char *old_name = bfd_get_section_name (NULL, sec);
14193 const char *prefix = is_rela ? ".rela" : ".rel";
14194
14195 if (old_name == NULL)
14196 return NULL;
14197
14198 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
14199 sprintf (name, "%s%s", prefix, old_name);
14200
14201 return name;
14202 }
14203
14204 /* Returns the dynamic reloc section associated with SEC.
14205 If necessary compute the name of the dynamic reloc section based
14206 on SEC's name (looked up in ABFD's string table) and the setting
14207 of IS_RELA. */
14208
14209 asection *
14210 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14211 asection * sec,
14212 bfd_boolean is_rela)
14213 {
14214 asection * reloc_sec = elf_section_data (sec)->sreloc;
14215
14216 if (reloc_sec == NULL)
14217 {
14218 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14219
14220 if (name != NULL)
14221 {
14222 reloc_sec = bfd_get_linker_section (abfd, name);
14223
14224 if (reloc_sec != NULL)
14225 elf_section_data (sec)->sreloc = reloc_sec;
14226 }
14227 }
14228
14229 return reloc_sec;
14230 }
14231
14232 /* Returns the dynamic reloc section associated with SEC. If the
14233 section does not exist it is created and attached to the DYNOBJ
14234 bfd and stored in the SRELOC field of SEC's elf_section_data
14235 structure.
14236
14237 ALIGNMENT is the alignment for the newly created section and
14238 IS_RELA defines whether the name should be .rela.<SEC's name>
14239 or .rel.<SEC's name>. The section name is looked up in the
14240 string table associated with ABFD. */
14241
14242 asection *
14243 _bfd_elf_make_dynamic_reloc_section (asection *sec,
14244 bfd *dynobj,
14245 unsigned int alignment,
14246 bfd *abfd,
14247 bfd_boolean is_rela)
14248 {
14249 asection * reloc_sec = elf_section_data (sec)->sreloc;
14250
14251 if (reloc_sec == NULL)
14252 {
14253 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14254
14255 if (name == NULL)
14256 return NULL;
14257
14258 reloc_sec = bfd_get_linker_section (dynobj, name);
14259
14260 if (reloc_sec == NULL)
14261 {
14262 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14263 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
14264 if ((sec->flags & SEC_ALLOC) != 0)
14265 flags |= SEC_ALLOC | SEC_LOAD;
14266
14267 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
14268 if (reloc_sec != NULL)
14269 {
14270 /* _bfd_elf_get_sec_type_attr chooses a section type by
14271 name. Override as it may be wrong, eg. for a user
14272 section named "auto" we'll get ".relauto" which is
14273 seen to be a .rela section. */
14274 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
14275 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14276 reloc_sec = NULL;
14277 }
14278 }
14279
14280 elf_section_data (sec)->sreloc = reloc_sec;
14281 }
14282
14283 return reloc_sec;
14284 }
14285
14286 /* Copy the ELF symbol type and other attributes for a linker script
14287 assignment from HSRC to HDEST. Generally this should be treated as
14288 if we found a strong non-dynamic definition for HDEST (except that
14289 ld ignores multiple definition errors). */
14290 void
14291 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14292 struct bfd_link_hash_entry *hdest,
14293 struct bfd_link_hash_entry *hsrc)
14294 {
14295 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14296 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14297 Elf_Internal_Sym isym;
14298
14299 ehdest->type = ehsrc->type;
14300 ehdest->target_internal = ehsrc->target_internal;
14301
14302 isym.st_other = ehsrc->other;
14303 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
14304 }
14305
14306 /* Append a RELA relocation REL to section S in BFD. */
14307
14308 void
14309 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14310 {
14311 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14312 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14313 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14314 bed->s->swap_reloca_out (abfd, rel, loc);
14315 }
14316
14317 /* Append a REL relocation REL to section S in BFD. */
14318
14319 void
14320 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14321 {
14322 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14323 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14324 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
14325 bed->s->swap_reloc_out (abfd, rel, loc);
14326 }
14327
14328 /* Define __start, __stop, .startof. or .sizeof. symbol. */
14329
14330 struct bfd_link_hash_entry *
14331 bfd_elf_define_start_stop (struct bfd_link_info *info,
14332 const char *symbol, asection *sec)
14333 {
14334 struct elf_link_hash_entry *h;
14335
14336 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14337 FALSE, FALSE, TRUE);
14338 if (h != NULL
14339 && (h->root.type == bfd_link_hash_undefined
14340 || h->root.type == bfd_link_hash_undefweak
14341 || (h->ref_regular && !h->def_regular)))
14342 {
14343 h->root.type = bfd_link_hash_defined;
14344 h->root.u.def.section = sec;
14345 h->root.u.def.value = 0;
14346 h->def_regular = 1;
14347 h->def_dynamic = 0;
14348 h->start_stop = 1;
14349 h->u2.start_stop_section = sec;
14350 if (symbol[0] == '.')
14351 {
14352 /* .startof. and .sizeof. symbols are local. */
14353 const struct elf_backend_data *bed;
14354 bed = get_elf_backend_data (info->output_bfd);
14355 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
14356 }
14357 else if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14358 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
14359 return &h->root;
14360 }
14361 return NULL;
14362 }
This page took 0.344398 seconds and 5 git commands to generate.