Remove bfd stub function casts.
[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_omit_section_dynsym_default (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 bfd_boolean
917 _bfd_elf_omit_section_dynsym_all
918 (bfd *output_bfd ATTRIBUTE_UNUSED,
919 struct bfd_link_info *info ATTRIBUTE_UNUSED,
920 asection *p ATTRIBUTE_UNUSED)
921 {
922 return TRUE;
923 }
924
925 /* Assign dynsym indices. In a shared library we generate a section
926 symbol for each output section, which come first. Next come symbols
927 which have been forced to local binding. Then all of the back-end
928 allocated local dynamic syms, followed by the rest of the global
929 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
930 (This prevents the early call before elf_backend_init_index_section
931 and strip_excluded_output_sections setting dynindx for sections
932 that are stripped.) */
933
934 static unsigned long
935 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
936 struct bfd_link_info *info,
937 unsigned long *section_sym_count)
938 {
939 unsigned long dynsymcount = 0;
940 bfd_boolean do_sec = section_sym_count != NULL;
941
942 if (bfd_link_pic (info)
943 || elf_hash_table (info)->is_relocatable_executable)
944 {
945 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
946 asection *p;
947 for (p = output_bfd->sections; p ; p = p->next)
948 if ((p->flags & SEC_EXCLUDE) == 0
949 && (p->flags & SEC_ALLOC) != 0
950 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
951 {
952 ++dynsymcount;
953 if (do_sec)
954 elf_section_data (p)->dynindx = dynsymcount;
955 }
956 else if (do_sec)
957 elf_section_data (p)->dynindx = 0;
958 }
959 if (do_sec)
960 *section_sym_count = dynsymcount;
961
962 elf_link_hash_traverse (elf_hash_table (info),
963 elf_link_renumber_local_hash_table_dynsyms,
964 &dynsymcount);
965
966 if (elf_hash_table (info)->dynlocal)
967 {
968 struct elf_link_local_dynamic_entry *p;
969 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
970 p->dynindx = ++dynsymcount;
971 }
972 elf_hash_table (info)->local_dynsymcount = dynsymcount;
973
974 elf_link_hash_traverse (elf_hash_table (info),
975 elf_link_renumber_hash_table_dynsyms,
976 &dynsymcount);
977
978 /* There is an unused NULL entry at the head of the table which we
979 must account for in our count even if the table is empty since it
980 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
981 .dynamic section. */
982 dynsymcount++;
983
984 elf_hash_table (info)->dynsymcount = dynsymcount;
985 return dynsymcount;
986 }
987
988 /* Merge st_other field. */
989
990 static void
991 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
992 const Elf_Internal_Sym *isym, asection *sec,
993 bfd_boolean definition, bfd_boolean dynamic)
994 {
995 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
996
997 /* If st_other has a processor-specific meaning, specific
998 code might be needed here. */
999 if (bed->elf_backend_merge_symbol_attribute)
1000 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
1001 dynamic);
1002
1003 if (!dynamic)
1004 {
1005 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
1006 unsigned hvis = ELF_ST_VISIBILITY (h->other);
1007
1008 /* Keep the most constraining visibility. Leave the remainder
1009 of the st_other field to elf_backend_merge_symbol_attribute. */
1010 if (symvis - 1 < hvis - 1)
1011 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1012 }
1013 else if (definition
1014 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
1015 && (sec->flags & SEC_READONLY) == 0)
1016 h->protected_def = 1;
1017 }
1018
1019 /* This function is called when we want to merge a new symbol with an
1020 existing symbol. It handles the various cases which arise when we
1021 find a definition in a dynamic object, or when there is already a
1022 definition in a dynamic object. The new symbol is described by
1023 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1024 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1025 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1026 of an old common symbol. We set OVERRIDE if the old symbol is
1027 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1028 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1029 to change. By OK to change, we mean that we shouldn't warn if the
1030 type or size does change. */
1031
1032 static bfd_boolean
1033 _bfd_elf_merge_symbol (bfd *abfd,
1034 struct bfd_link_info *info,
1035 const char *name,
1036 Elf_Internal_Sym *sym,
1037 asection **psec,
1038 bfd_vma *pvalue,
1039 struct elf_link_hash_entry **sym_hash,
1040 bfd **poldbfd,
1041 bfd_boolean *pold_weak,
1042 unsigned int *pold_alignment,
1043 bfd_boolean *skip,
1044 bfd_boolean *override,
1045 bfd_boolean *type_change_ok,
1046 bfd_boolean *size_change_ok,
1047 bfd_boolean *matched)
1048 {
1049 asection *sec, *oldsec;
1050 struct elf_link_hash_entry *h;
1051 struct elf_link_hash_entry *hi;
1052 struct elf_link_hash_entry *flip;
1053 int bind;
1054 bfd *oldbfd;
1055 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1056 bfd_boolean newweak, oldweak, newfunc, oldfunc;
1057 const struct elf_backend_data *bed;
1058 char *new_version;
1059 bfd_boolean default_sym = *matched;
1060
1061 *skip = FALSE;
1062 *override = FALSE;
1063
1064 sec = *psec;
1065 bind = ELF_ST_BIND (sym->st_info);
1066
1067 if (! bfd_is_und_section (sec))
1068 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1069 else
1070 h = ((struct elf_link_hash_entry *)
1071 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1072 if (h == NULL)
1073 return FALSE;
1074 *sym_hash = h;
1075
1076 bed = get_elf_backend_data (abfd);
1077
1078 /* NEW_VERSION is the symbol version of the new symbol. */
1079 if (h->versioned != unversioned)
1080 {
1081 /* Symbol version is unknown or versioned. */
1082 new_version = strrchr (name, ELF_VER_CHR);
1083 if (new_version)
1084 {
1085 if (h->versioned == unknown)
1086 {
1087 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1088 h->versioned = versioned_hidden;
1089 else
1090 h->versioned = versioned;
1091 }
1092 new_version += 1;
1093 if (new_version[0] == '\0')
1094 new_version = NULL;
1095 }
1096 else
1097 h->versioned = unversioned;
1098 }
1099 else
1100 new_version = NULL;
1101
1102 /* For merging, we only care about real symbols. But we need to make
1103 sure that indirect symbol dynamic flags are updated. */
1104 hi = h;
1105 while (h->root.type == bfd_link_hash_indirect
1106 || h->root.type == bfd_link_hash_warning)
1107 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1108
1109 if (!*matched)
1110 {
1111 if (hi == h || h->root.type == bfd_link_hash_new)
1112 *matched = TRUE;
1113 else
1114 {
1115 /* OLD_HIDDEN is true if the existing symbol is only visible
1116 to the symbol with the same symbol version. NEW_HIDDEN is
1117 true if the new symbol is only visible to the symbol with
1118 the same symbol version. */
1119 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1120 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1121 if (!old_hidden && !new_hidden)
1122 /* The new symbol matches the existing symbol if both
1123 aren't hidden. */
1124 *matched = TRUE;
1125 else
1126 {
1127 /* OLD_VERSION is the symbol version of the existing
1128 symbol. */
1129 char *old_version;
1130
1131 if (h->versioned >= versioned)
1132 old_version = strrchr (h->root.root.string,
1133 ELF_VER_CHR) + 1;
1134 else
1135 old_version = NULL;
1136
1137 /* The new symbol matches the existing symbol if they
1138 have the same symbol version. */
1139 *matched = (old_version == new_version
1140 || (old_version != NULL
1141 && new_version != NULL
1142 && strcmp (old_version, new_version) == 0));
1143 }
1144 }
1145 }
1146
1147 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1148 existing symbol. */
1149
1150 oldbfd = NULL;
1151 oldsec = NULL;
1152 switch (h->root.type)
1153 {
1154 default:
1155 break;
1156
1157 case bfd_link_hash_undefined:
1158 case bfd_link_hash_undefweak:
1159 oldbfd = h->root.u.undef.abfd;
1160 break;
1161
1162 case bfd_link_hash_defined:
1163 case bfd_link_hash_defweak:
1164 oldbfd = h->root.u.def.section->owner;
1165 oldsec = h->root.u.def.section;
1166 break;
1167
1168 case bfd_link_hash_common:
1169 oldbfd = h->root.u.c.p->section->owner;
1170 oldsec = h->root.u.c.p->section;
1171 if (pold_alignment)
1172 *pold_alignment = h->root.u.c.p->alignment_power;
1173 break;
1174 }
1175 if (poldbfd && *poldbfd == NULL)
1176 *poldbfd = oldbfd;
1177
1178 /* Differentiate strong and weak symbols. */
1179 newweak = bind == STB_WEAK;
1180 oldweak = (h->root.type == bfd_link_hash_defweak
1181 || h->root.type == bfd_link_hash_undefweak);
1182 if (pold_weak)
1183 *pold_weak = oldweak;
1184
1185 /* We have to check it for every instance since the first few may be
1186 references and not all compilers emit symbol type for undefined
1187 symbols. */
1188 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1189
1190 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1191 respectively, is from a dynamic object. */
1192
1193 newdyn = (abfd->flags & DYNAMIC) != 0;
1194
1195 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1196 syms and defined syms in dynamic libraries respectively.
1197 ref_dynamic on the other hand can be set for a symbol defined in
1198 a dynamic library, and def_dynamic may not be set; When the
1199 definition in a dynamic lib is overridden by a definition in the
1200 executable use of the symbol in the dynamic lib becomes a
1201 reference to the executable symbol. */
1202 if (newdyn)
1203 {
1204 if (bfd_is_und_section (sec))
1205 {
1206 if (bind != STB_WEAK)
1207 {
1208 h->ref_dynamic_nonweak = 1;
1209 hi->ref_dynamic_nonweak = 1;
1210 }
1211 }
1212 else
1213 {
1214 /* Update the existing symbol only if they match. */
1215 if (*matched)
1216 h->dynamic_def = 1;
1217 hi->dynamic_def = 1;
1218 }
1219 }
1220
1221 /* If we just created the symbol, mark it as being an ELF symbol.
1222 Other than that, there is nothing to do--there is no merge issue
1223 with a newly defined symbol--so we just return. */
1224
1225 if (h->root.type == bfd_link_hash_new)
1226 {
1227 h->non_elf = 0;
1228 return TRUE;
1229 }
1230
1231 /* In cases involving weak versioned symbols, we may wind up trying
1232 to merge a symbol with itself. Catch that here, to avoid the
1233 confusion that results if we try to override a symbol with
1234 itself. The additional tests catch cases like
1235 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1236 dynamic object, which we do want to handle here. */
1237 if (abfd == oldbfd
1238 && (newweak || oldweak)
1239 && ((abfd->flags & DYNAMIC) == 0
1240 || !h->def_regular))
1241 return TRUE;
1242
1243 olddyn = FALSE;
1244 if (oldbfd != NULL)
1245 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1246 else if (oldsec != NULL)
1247 {
1248 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1249 indices used by MIPS ELF. */
1250 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1251 }
1252
1253 /* Handle a case where plugin_notice won't be called and thus won't
1254 set the non_ir_ref flags on the first pass over symbols. */
1255 if (oldbfd != NULL
1256 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1257 && newdyn != olddyn)
1258 {
1259 h->root.non_ir_ref_dynamic = TRUE;
1260 hi->root.non_ir_ref_dynamic = TRUE;
1261 }
1262
1263 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1264 respectively, appear to be a definition rather than reference. */
1265
1266 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1267
1268 olddef = (h->root.type != bfd_link_hash_undefined
1269 && h->root.type != bfd_link_hash_undefweak
1270 && h->root.type != bfd_link_hash_common);
1271
1272 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1273 respectively, appear to be a function. */
1274
1275 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1276 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1277
1278 oldfunc = (h->type != STT_NOTYPE
1279 && bed->is_function_type (h->type));
1280
1281 if (!(newfunc && oldfunc)
1282 && ELF_ST_TYPE (sym->st_info) != h->type
1283 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1284 && h->type != STT_NOTYPE
1285 && (newdef || bfd_is_com_section (sec))
1286 && (olddef || h->root.type == bfd_link_hash_common))
1287 {
1288 /* If creating a default indirect symbol ("foo" or "foo@") from
1289 a dynamic versioned definition ("foo@@") skip doing so if
1290 there is an existing regular definition with a different
1291 type. We don't want, for example, a "time" variable in the
1292 executable overriding a "time" function in a shared library. */
1293 if (newdyn
1294 && !olddyn)
1295 {
1296 *skip = TRUE;
1297 return TRUE;
1298 }
1299
1300 /* When adding a symbol from a regular object file after we have
1301 created indirect symbols, undo the indirection and any
1302 dynamic state. */
1303 if (hi != h
1304 && !newdyn
1305 && olddyn)
1306 {
1307 h = hi;
1308 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1309 h->forced_local = 0;
1310 h->ref_dynamic = 0;
1311 h->def_dynamic = 0;
1312 h->dynamic_def = 0;
1313 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1314 {
1315 h->root.type = bfd_link_hash_undefined;
1316 h->root.u.undef.abfd = abfd;
1317 }
1318 else
1319 {
1320 h->root.type = bfd_link_hash_new;
1321 h->root.u.undef.abfd = NULL;
1322 }
1323 return TRUE;
1324 }
1325 }
1326
1327 /* Check TLS symbols. We don't check undefined symbols introduced
1328 by "ld -u" which have no type (and oldbfd NULL), and we don't
1329 check symbols from plugins because they also have no type. */
1330 if (oldbfd != NULL
1331 && (oldbfd->flags & BFD_PLUGIN) == 0
1332 && (abfd->flags & BFD_PLUGIN) == 0
1333 && ELF_ST_TYPE (sym->st_info) != h->type
1334 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1335 {
1336 bfd *ntbfd, *tbfd;
1337 bfd_boolean ntdef, tdef;
1338 asection *ntsec, *tsec;
1339
1340 if (h->type == STT_TLS)
1341 {
1342 ntbfd = abfd;
1343 ntsec = sec;
1344 ntdef = newdef;
1345 tbfd = oldbfd;
1346 tsec = oldsec;
1347 tdef = olddef;
1348 }
1349 else
1350 {
1351 ntbfd = oldbfd;
1352 ntsec = oldsec;
1353 ntdef = olddef;
1354 tbfd = abfd;
1355 tsec = sec;
1356 tdef = newdef;
1357 }
1358
1359 if (tdef && ntdef)
1360 _bfd_error_handler
1361 /* xgettext:c-format */
1362 (_("%s: TLS definition in %B section %A "
1363 "mismatches non-TLS definition in %B section %A"),
1364 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1365 else if (!tdef && !ntdef)
1366 _bfd_error_handler
1367 /* xgettext:c-format */
1368 (_("%s: TLS reference in %B "
1369 "mismatches non-TLS reference in %B"),
1370 h->root.root.string, tbfd, ntbfd);
1371 else if (tdef)
1372 _bfd_error_handler
1373 /* xgettext:c-format */
1374 (_("%s: TLS definition in %B section %A "
1375 "mismatches non-TLS reference in %B"),
1376 h->root.root.string, tbfd, tsec, ntbfd);
1377 else
1378 _bfd_error_handler
1379 /* xgettext:c-format */
1380 (_("%s: TLS reference in %B "
1381 "mismatches non-TLS definition in %B section %A"),
1382 h->root.root.string, tbfd, ntbfd, ntsec);
1383
1384 bfd_set_error (bfd_error_bad_value);
1385 return FALSE;
1386 }
1387
1388 /* If the old symbol has non-default visibility, we ignore the new
1389 definition from a dynamic object. */
1390 if (newdyn
1391 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1392 && !bfd_is_und_section (sec))
1393 {
1394 *skip = TRUE;
1395 /* Make sure this symbol is dynamic. */
1396 h->ref_dynamic = 1;
1397 hi->ref_dynamic = 1;
1398 /* A protected symbol has external availability. Make sure it is
1399 recorded as dynamic.
1400
1401 FIXME: Should we check type and size for protected symbol? */
1402 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1403 return bfd_elf_link_record_dynamic_symbol (info, h);
1404 else
1405 return TRUE;
1406 }
1407 else if (!newdyn
1408 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1409 && h->def_dynamic)
1410 {
1411 /* If the new symbol with non-default visibility comes from a
1412 relocatable file and the old definition comes from a dynamic
1413 object, we remove the old definition. */
1414 if (hi->root.type == bfd_link_hash_indirect)
1415 {
1416 /* Handle the case where the old dynamic definition is
1417 default versioned. We need to copy the symbol info from
1418 the symbol with default version to the normal one if it
1419 was referenced before. */
1420 if (h->ref_regular)
1421 {
1422 hi->root.type = h->root.type;
1423 h->root.type = bfd_link_hash_indirect;
1424 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1425
1426 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1427 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1428 {
1429 /* If the new symbol is hidden or internal, completely undo
1430 any dynamic link state. */
1431 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1432 h->forced_local = 0;
1433 h->ref_dynamic = 0;
1434 }
1435 else
1436 h->ref_dynamic = 1;
1437
1438 h->def_dynamic = 0;
1439 /* FIXME: Should we check type and size for protected symbol? */
1440 h->size = 0;
1441 h->type = 0;
1442
1443 h = hi;
1444 }
1445 else
1446 h = hi;
1447 }
1448
1449 /* If the old symbol was undefined before, then it will still be
1450 on the undefs list. If the new symbol is undefined or
1451 common, we can't make it bfd_link_hash_new here, because new
1452 undefined or common symbols will be added to the undefs list
1453 by _bfd_generic_link_add_one_symbol. Symbols may not be
1454 added twice to the undefs list. Also, if the new symbol is
1455 undefweak then we don't want to lose the strong undef. */
1456 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1457 {
1458 h->root.type = bfd_link_hash_undefined;
1459 h->root.u.undef.abfd = abfd;
1460 }
1461 else
1462 {
1463 h->root.type = bfd_link_hash_new;
1464 h->root.u.undef.abfd = NULL;
1465 }
1466
1467 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1468 {
1469 /* If the new symbol is hidden or internal, completely undo
1470 any dynamic link state. */
1471 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1472 h->forced_local = 0;
1473 h->ref_dynamic = 0;
1474 }
1475 else
1476 h->ref_dynamic = 1;
1477 h->def_dynamic = 0;
1478 /* FIXME: Should we check type and size for protected symbol? */
1479 h->size = 0;
1480 h->type = 0;
1481 return TRUE;
1482 }
1483
1484 /* If a new weak symbol definition comes from a regular file and the
1485 old symbol comes from a dynamic library, we treat the new one as
1486 strong. Similarly, an old weak symbol definition from a regular
1487 file is treated as strong when the new symbol comes from a dynamic
1488 library. Further, an old weak symbol from a dynamic library is
1489 treated as strong if the new symbol is from a dynamic library.
1490 This reflects the way glibc's ld.so works.
1491
1492 Also allow a weak symbol to override a linker script symbol
1493 defined by an early pass over the script. This is done so the
1494 linker knows the symbol is defined in an object file, for the
1495 DEFINED script function.
1496
1497 Do this before setting *type_change_ok or *size_change_ok so that
1498 we warn properly when dynamic library symbols are overridden. */
1499
1500 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1501 newweak = FALSE;
1502 if (olddef && newdyn)
1503 oldweak = FALSE;
1504
1505 /* Allow changes between different types of function symbol. */
1506 if (newfunc && oldfunc)
1507 *type_change_ok = TRUE;
1508
1509 /* It's OK to change the type if either the existing symbol or the
1510 new symbol is weak. A type change is also OK if the old symbol
1511 is undefined and the new symbol is defined. */
1512
1513 if (oldweak
1514 || newweak
1515 || (newdef
1516 && h->root.type == bfd_link_hash_undefined))
1517 *type_change_ok = TRUE;
1518
1519 /* It's OK to change the size if either the existing symbol or the
1520 new symbol is weak, or if the old symbol is undefined. */
1521
1522 if (*type_change_ok
1523 || h->root.type == bfd_link_hash_undefined)
1524 *size_change_ok = TRUE;
1525
1526 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1527 symbol, respectively, appears to be a common symbol in a dynamic
1528 object. If a symbol appears in an uninitialized section, and is
1529 not weak, and is not a function, then it may be a common symbol
1530 which was resolved when the dynamic object was created. We want
1531 to treat such symbols specially, because they raise special
1532 considerations when setting the symbol size: if the symbol
1533 appears as a common symbol in a regular object, and the size in
1534 the regular object is larger, we must make sure that we use the
1535 larger size. This problematic case can always be avoided in C,
1536 but it must be handled correctly when using Fortran shared
1537 libraries.
1538
1539 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1540 likewise for OLDDYNCOMMON and OLDDEF.
1541
1542 Note that this test is just a heuristic, and that it is quite
1543 possible to have an uninitialized symbol in a shared object which
1544 is really a definition, rather than a common symbol. This could
1545 lead to some minor confusion when the symbol really is a common
1546 symbol in some regular object. However, I think it will be
1547 harmless. */
1548
1549 if (newdyn
1550 && newdef
1551 && !newweak
1552 && (sec->flags & SEC_ALLOC) != 0
1553 && (sec->flags & SEC_LOAD) == 0
1554 && sym->st_size > 0
1555 && !newfunc)
1556 newdyncommon = TRUE;
1557 else
1558 newdyncommon = FALSE;
1559
1560 if (olddyn
1561 && olddef
1562 && h->root.type == bfd_link_hash_defined
1563 && h->def_dynamic
1564 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1565 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1566 && h->size > 0
1567 && !oldfunc)
1568 olddyncommon = TRUE;
1569 else
1570 olddyncommon = FALSE;
1571
1572 /* We now know everything about the old and new symbols. We ask the
1573 backend to check if we can merge them. */
1574 if (bed->merge_symbol != NULL)
1575 {
1576 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1577 return FALSE;
1578 sec = *psec;
1579 }
1580
1581 /* There are multiple definitions of a normal symbol. Skip the
1582 default symbol as well as definition from an IR object. */
1583 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1584 && !default_sym && h->def_regular
1585 && !(oldbfd != NULL
1586 && (oldbfd->flags & BFD_PLUGIN) != 0
1587 && (abfd->flags & BFD_PLUGIN) == 0))
1588 {
1589 /* Handle a multiple definition. */
1590 (*info->callbacks->multiple_definition) (info, &h->root,
1591 abfd, sec, *pvalue);
1592 *skip = TRUE;
1593 return TRUE;
1594 }
1595
1596 /* If both the old and the new symbols look like common symbols in a
1597 dynamic object, set the size of the symbol to the larger of the
1598 two. */
1599
1600 if (olddyncommon
1601 && newdyncommon
1602 && sym->st_size != h->size)
1603 {
1604 /* Since we think we have two common symbols, issue a multiple
1605 common warning if desired. Note that we only warn if the
1606 size is different. If the size is the same, we simply let
1607 the old symbol override the new one as normally happens with
1608 symbols defined in dynamic objects. */
1609
1610 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1611 bfd_link_hash_common, sym->st_size);
1612 if (sym->st_size > h->size)
1613 h->size = sym->st_size;
1614
1615 *size_change_ok = TRUE;
1616 }
1617
1618 /* If we are looking at a dynamic object, and we have found a
1619 definition, we need to see if the symbol was already defined by
1620 some other object. If so, we want to use the existing
1621 definition, and we do not want to report a multiple symbol
1622 definition error; we do this by clobbering *PSEC to be
1623 bfd_und_section_ptr.
1624
1625 We treat a common symbol as a definition if the symbol in the
1626 shared library is a function, since common symbols always
1627 represent variables; this can cause confusion in principle, but
1628 any such confusion would seem to indicate an erroneous program or
1629 shared library. We also permit a common symbol in a regular
1630 object to override a weak symbol in a shared object. */
1631
1632 if (newdyn
1633 && newdef
1634 && (olddef
1635 || (h->root.type == bfd_link_hash_common
1636 && (newweak || newfunc))))
1637 {
1638 *override = TRUE;
1639 newdef = FALSE;
1640 newdyncommon = FALSE;
1641
1642 *psec = sec = bfd_und_section_ptr;
1643 *size_change_ok = TRUE;
1644
1645 /* If we get here when the old symbol is a common symbol, then
1646 we are explicitly letting it override a weak symbol or
1647 function in a dynamic object, and we don't want to warn about
1648 a type change. If the old symbol is a defined symbol, a type
1649 change warning may still be appropriate. */
1650
1651 if (h->root.type == bfd_link_hash_common)
1652 *type_change_ok = TRUE;
1653 }
1654
1655 /* Handle the special case of an old common symbol merging with a
1656 new symbol which looks like a common symbol in a shared object.
1657 We change *PSEC and *PVALUE to make the new symbol look like a
1658 common symbol, and let _bfd_generic_link_add_one_symbol do the
1659 right thing. */
1660
1661 if (newdyncommon
1662 && h->root.type == bfd_link_hash_common)
1663 {
1664 *override = TRUE;
1665 newdef = FALSE;
1666 newdyncommon = FALSE;
1667 *pvalue = sym->st_size;
1668 *psec = sec = bed->common_section (oldsec);
1669 *size_change_ok = TRUE;
1670 }
1671
1672 /* Skip weak definitions of symbols that are already defined. */
1673 if (newdef && olddef && newweak)
1674 {
1675 /* Don't skip new non-IR weak syms. */
1676 if (!(oldbfd != NULL
1677 && (oldbfd->flags & BFD_PLUGIN) != 0
1678 && (abfd->flags & BFD_PLUGIN) == 0))
1679 {
1680 newdef = FALSE;
1681 *skip = TRUE;
1682 }
1683
1684 /* Merge st_other. If the symbol already has a dynamic index,
1685 but visibility says it should not be visible, turn it into a
1686 local symbol. */
1687 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1688 if (h->dynindx != -1)
1689 switch (ELF_ST_VISIBILITY (h->other))
1690 {
1691 case STV_INTERNAL:
1692 case STV_HIDDEN:
1693 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1694 break;
1695 }
1696 }
1697
1698 /* If the old symbol is from a dynamic object, and the new symbol is
1699 a definition which is not from a dynamic object, then the new
1700 symbol overrides the old symbol. Symbols from regular files
1701 always take precedence over symbols from dynamic objects, even if
1702 they are defined after the dynamic object in the link.
1703
1704 As above, we again permit a common symbol in a regular object to
1705 override a definition in a shared object if the shared object
1706 symbol is a function or is weak. */
1707
1708 flip = NULL;
1709 if (!newdyn
1710 && (newdef
1711 || (bfd_is_com_section (sec)
1712 && (oldweak || oldfunc)))
1713 && olddyn
1714 && olddef
1715 && h->def_dynamic)
1716 {
1717 /* Change the hash table entry to undefined, and let
1718 _bfd_generic_link_add_one_symbol do the right thing with the
1719 new definition. */
1720
1721 h->root.type = bfd_link_hash_undefined;
1722 h->root.u.undef.abfd = h->root.u.def.section->owner;
1723 *size_change_ok = TRUE;
1724
1725 olddef = FALSE;
1726 olddyncommon = FALSE;
1727
1728 /* We again permit a type change when a common symbol may be
1729 overriding a function. */
1730
1731 if (bfd_is_com_section (sec))
1732 {
1733 if (oldfunc)
1734 {
1735 /* If a common symbol overrides a function, make sure
1736 that it isn't defined dynamically nor has type
1737 function. */
1738 h->def_dynamic = 0;
1739 h->type = STT_NOTYPE;
1740 }
1741 *type_change_ok = TRUE;
1742 }
1743
1744 if (hi->root.type == bfd_link_hash_indirect)
1745 flip = hi;
1746 else
1747 /* This union may have been set to be non-NULL when this symbol
1748 was seen in a dynamic object. We must force the union to be
1749 NULL, so that it is correct for a regular symbol. */
1750 h->verinfo.vertree = NULL;
1751 }
1752
1753 /* Handle the special case of a new common symbol merging with an
1754 old symbol that looks like it might be a common symbol defined in
1755 a shared object. Note that we have already handled the case in
1756 which a new common symbol should simply override the definition
1757 in the shared library. */
1758
1759 if (! newdyn
1760 && bfd_is_com_section (sec)
1761 && olddyncommon)
1762 {
1763 /* It would be best if we could set the hash table entry to a
1764 common symbol, but we don't know what to use for the section
1765 or the alignment. */
1766 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1767 bfd_link_hash_common, sym->st_size);
1768
1769 /* If the presumed common symbol in the dynamic object is
1770 larger, pretend that the new symbol has its size. */
1771
1772 if (h->size > *pvalue)
1773 *pvalue = h->size;
1774
1775 /* We need to remember the alignment required by the symbol
1776 in the dynamic object. */
1777 BFD_ASSERT (pold_alignment);
1778 *pold_alignment = h->root.u.def.section->alignment_power;
1779
1780 olddef = FALSE;
1781 olddyncommon = FALSE;
1782
1783 h->root.type = bfd_link_hash_undefined;
1784 h->root.u.undef.abfd = h->root.u.def.section->owner;
1785
1786 *size_change_ok = TRUE;
1787 *type_change_ok = TRUE;
1788
1789 if (hi->root.type == bfd_link_hash_indirect)
1790 flip = hi;
1791 else
1792 h->verinfo.vertree = NULL;
1793 }
1794
1795 if (flip != NULL)
1796 {
1797 /* Handle the case where we had a versioned symbol in a dynamic
1798 library and now find a definition in a normal object. In this
1799 case, we make the versioned symbol point to the normal one. */
1800 flip->root.type = h->root.type;
1801 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1802 h->root.type = bfd_link_hash_indirect;
1803 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1804 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1805 if (h->def_dynamic)
1806 {
1807 h->def_dynamic = 0;
1808 flip->ref_dynamic = 1;
1809 }
1810 }
1811
1812 return TRUE;
1813 }
1814
1815 /* This function is called to create an indirect symbol from the
1816 default for the symbol with the default version if needed. The
1817 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1818 set DYNSYM if the new indirect symbol is dynamic. */
1819
1820 static bfd_boolean
1821 _bfd_elf_add_default_symbol (bfd *abfd,
1822 struct bfd_link_info *info,
1823 struct elf_link_hash_entry *h,
1824 const char *name,
1825 Elf_Internal_Sym *sym,
1826 asection *sec,
1827 bfd_vma value,
1828 bfd **poldbfd,
1829 bfd_boolean *dynsym)
1830 {
1831 bfd_boolean type_change_ok;
1832 bfd_boolean size_change_ok;
1833 bfd_boolean skip;
1834 char *shortname;
1835 struct elf_link_hash_entry *hi;
1836 struct bfd_link_hash_entry *bh;
1837 const struct elf_backend_data *bed;
1838 bfd_boolean collect;
1839 bfd_boolean dynamic;
1840 bfd_boolean override;
1841 char *p;
1842 size_t len, shortlen;
1843 asection *tmp_sec;
1844 bfd_boolean matched;
1845
1846 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1847 return TRUE;
1848
1849 /* If this symbol has a version, and it is the default version, we
1850 create an indirect symbol from the default name to the fully
1851 decorated name. This will cause external references which do not
1852 specify a version to be bound to this version of the symbol. */
1853 p = strchr (name, ELF_VER_CHR);
1854 if (h->versioned == unknown)
1855 {
1856 if (p == NULL)
1857 {
1858 h->versioned = unversioned;
1859 return TRUE;
1860 }
1861 else
1862 {
1863 if (p[1] != ELF_VER_CHR)
1864 {
1865 h->versioned = versioned_hidden;
1866 return TRUE;
1867 }
1868 else
1869 h->versioned = versioned;
1870 }
1871 }
1872 else
1873 {
1874 /* PR ld/19073: We may see an unversioned definition after the
1875 default version. */
1876 if (p == NULL)
1877 return TRUE;
1878 }
1879
1880 bed = get_elf_backend_data (abfd);
1881 collect = bed->collect;
1882 dynamic = (abfd->flags & DYNAMIC) != 0;
1883
1884 shortlen = p - name;
1885 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1886 if (shortname == NULL)
1887 return FALSE;
1888 memcpy (shortname, name, shortlen);
1889 shortname[shortlen] = '\0';
1890
1891 /* We are going to create a new symbol. Merge it with any existing
1892 symbol with this name. For the purposes of the merge, act as
1893 though we were defining the symbol we just defined, although we
1894 actually going to define an indirect symbol. */
1895 type_change_ok = FALSE;
1896 size_change_ok = FALSE;
1897 matched = TRUE;
1898 tmp_sec = sec;
1899 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1900 &hi, poldbfd, NULL, NULL, &skip, &override,
1901 &type_change_ok, &size_change_ok, &matched))
1902 return FALSE;
1903
1904 if (skip)
1905 goto nondefault;
1906
1907 if (hi->def_regular)
1908 {
1909 /* If the undecorated symbol will have a version added by a
1910 script different to H, then don't indirect to/from the
1911 undecorated symbol. This isn't ideal because we may not yet
1912 have seen symbol versions, if given by a script on the
1913 command line rather than via --version-script. */
1914 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1915 {
1916 bfd_boolean hide;
1917
1918 hi->verinfo.vertree
1919 = bfd_find_version_for_sym (info->version_info,
1920 hi->root.root.string, &hide);
1921 if (hi->verinfo.vertree != NULL && hide)
1922 {
1923 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1924 goto nondefault;
1925 }
1926 }
1927 if (hi->verinfo.vertree != NULL
1928 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1929 goto nondefault;
1930 }
1931
1932 if (! override)
1933 {
1934 /* Add the default symbol if not performing a relocatable link. */
1935 if (! bfd_link_relocatable (info))
1936 {
1937 bh = &hi->root;
1938 if (! (_bfd_generic_link_add_one_symbol
1939 (info, abfd, shortname, BSF_INDIRECT,
1940 bfd_ind_section_ptr,
1941 0, name, FALSE, collect, &bh)))
1942 return FALSE;
1943 hi = (struct elf_link_hash_entry *) bh;
1944 }
1945 }
1946 else
1947 {
1948 /* In this case the symbol named SHORTNAME is overriding the
1949 indirect symbol we want to add. We were planning on making
1950 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1951 is the name without a version. NAME is the fully versioned
1952 name, and it is the default version.
1953
1954 Overriding means that we already saw a definition for the
1955 symbol SHORTNAME in a regular object, and it is overriding
1956 the symbol defined in the dynamic object.
1957
1958 When this happens, we actually want to change NAME, the
1959 symbol we just added, to refer to SHORTNAME. This will cause
1960 references to NAME in the shared object to become references
1961 to SHORTNAME in the regular object. This is what we expect
1962 when we override a function in a shared object: that the
1963 references in the shared object will be mapped to the
1964 definition in the regular object. */
1965
1966 while (hi->root.type == bfd_link_hash_indirect
1967 || hi->root.type == bfd_link_hash_warning)
1968 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1969
1970 h->root.type = bfd_link_hash_indirect;
1971 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1972 if (h->def_dynamic)
1973 {
1974 h->def_dynamic = 0;
1975 hi->ref_dynamic = 1;
1976 if (hi->ref_regular
1977 || hi->def_regular)
1978 {
1979 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1980 return FALSE;
1981 }
1982 }
1983
1984 /* Now set HI to H, so that the following code will set the
1985 other fields correctly. */
1986 hi = h;
1987 }
1988
1989 /* Check if HI is a warning symbol. */
1990 if (hi->root.type == bfd_link_hash_warning)
1991 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1992
1993 /* If there is a duplicate definition somewhere, then HI may not
1994 point to an indirect symbol. We will have reported an error to
1995 the user in that case. */
1996
1997 if (hi->root.type == bfd_link_hash_indirect)
1998 {
1999 struct elf_link_hash_entry *ht;
2000
2001 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2002 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2003
2004 /* A reference to the SHORTNAME symbol from a dynamic library
2005 will be satisfied by the versioned symbol at runtime. In
2006 effect, we have a reference to the versioned symbol. */
2007 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2008 hi->dynamic_def |= ht->dynamic_def;
2009
2010 /* See if the new flags lead us to realize that the symbol must
2011 be dynamic. */
2012 if (! *dynsym)
2013 {
2014 if (! dynamic)
2015 {
2016 if (! bfd_link_executable (info)
2017 || hi->def_dynamic
2018 || hi->ref_dynamic)
2019 *dynsym = TRUE;
2020 }
2021 else
2022 {
2023 if (hi->ref_regular)
2024 *dynsym = TRUE;
2025 }
2026 }
2027 }
2028
2029 /* We also need to define an indirection from the nondefault version
2030 of the symbol. */
2031
2032 nondefault:
2033 len = strlen (name);
2034 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2035 if (shortname == NULL)
2036 return FALSE;
2037 memcpy (shortname, name, shortlen);
2038 memcpy (shortname + shortlen, p + 1, len - shortlen);
2039
2040 /* Once again, merge with any existing symbol. */
2041 type_change_ok = FALSE;
2042 size_change_ok = FALSE;
2043 tmp_sec = sec;
2044 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2045 &hi, poldbfd, NULL, NULL, &skip, &override,
2046 &type_change_ok, &size_change_ok, &matched))
2047 return FALSE;
2048
2049 if (skip)
2050 return TRUE;
2051
2052 if (override)
2053 {
2054 /* Here SHORTNAME is a versioned name, so we don't expect to see
2055 the type of override we do in the case above unless it is
2056 overridden by a versioned definition. */
2057 if (hi->root.type != bfd_link_hash_defined
2058 && hi->root.type != bfd_link_hash_defweak)
2059 _bfd_error_handler
2060 /* xgettext:c-format */
2061 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
2062 abfd, shortname);
2063 }
2064 else
2065 {
2066 bh = &hi->root;
2067 if (! (_bfd_generic_link_add_one_symbol
2068 (info, abfd, shortname, BSF_INDIRECT,
2069 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
2070 return FALSE;
2071 hi = (struct elf_link_hash_entry *) bh;
2072
2073 /* If there is a duplicate definition somewhere, then HI may not
2074 point to an indirect symbol. We will have reported an error
2075 to the user in that case. */
2076
2077 if (hi->root.type == bfd_link_hash_indirect)
2078 {
2079 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2080 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2081 hi->dynamic_def |= h->dynamic_def;
2082
2083 /* See if the new flags lead us to realize that the symbol
2084 must be dynamic. */
2085 if (! *dynsym)
2086 {
2087 if (! dynamic)
2088 {
2089 if (! bfd_link_executable (info)
2090 || hi->ref_dynamic)
2091 *dynsym = TRUE;
2092 }
2093 else
2094 {
2095 if (hi->ref_regular)
2096 *dynsym = TRUE;
2097 }
2098 }
2099 }
2100 }
2101
2102 return TRUE;
2103 }
2104 \f
2105 /* This routine is used to export all defined symbols into the dynamic
2106 symbol table. It is called via elf_link_hash_traverse. */
2107
2108 static bfd_boolean
2109 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2110 {
2111 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2112
2113 /* Ignore indirect symbols. These are added by the versioning code. */
2114 if (h->root.type == bfd_link_hash_indirect)
2115 return TRUE;
2116
2117 /* Ignore this if we won't export it. */
2118 if (!eif->info->export_dynamic && !h->dynamic)
2119 return TRUE;
2120
2121 if (h->dynindx == -1
2122 && (h->def_regular || h->ref_regular)
2123 && ! bfd_hide_sym_by_version (eif->info->version_info,
2124 h->root.root.string))
2125 {
2126 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2127 {
2128 eif->failed = TRUE;
2129 return FALSE;
2130 }
2131 }
2132
2133 return TRUE;
2134 }
2135 \f
2136 /* Look through the symbols which are defined in other shared
2137 libraries and referenced here. Update the list of version
2138 dependencies. This will be put into the .gnu.version_r section.
2139 This function is called via elf_link_hash_traverse. */
2140
2141 static bfd_boolean
2142 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2143 void *data)
2144 {
2145 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2146 Elf_Internal_Verneed *t;
2147 Elf_Internal_Vernaux *a;
2148 bfd_size_type amt;
2149
2150 /* We only care about symbols defined in shared objects with version
2151 information. */
2152 if (!h->def_dynamic
2153 || h->def_regular
2154 || h->dynindx == -1
2155 || h->verinfo.verdef == NULL
2156 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2157 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2158 return TRUE;
2159
2160 /* See if we already know about this version. */
2161 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2162 t != NULL;
2163 t = t->vn_nextref)
2164 {
2165 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2166 continue;
2167
2168 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2169 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2170 return TRUE;
2171
2172 break;
2173 }
2174
2175 /* This is a new version. Add it to tree we are building. */
2176
2177 if (t == NULL)
2178 {
2179 amt = sizeof *t;
2180 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2181 if (t == NULL)
2182 {
2183 rinfo->failed = TRUE;
2184 return FALSE;
2185 }
2186
2187 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2188 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2189 elf_tdata (rinfo->info->output_bfd)->verref = t;
2190 }
2191
2192 amt = sizeof *a;
2193 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2194 if (a == NULL)
2195 {
2196 rinfo->failed = TRUE;
2197 return FALSE;
2198 }
2199
2200 /* Note that we are copying a string pointer here, and testing it
2201 above. If bfd_elf_string_from_elf_section is ever changed to
2202 discard the string data when low in memory, this will have to be
2203 fixed. */
2204 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2205
2206 a->vna_flags = h->verinfo.verdef->vd_flags;
2207 a->vna_nextptr = t->vn_auxptr;
2208
2209 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2210 ++rinfo->vers;
2211
2212 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2213
2214 t->vn_auxptr = a;
2215
2216 return TRUE;
2217 }
2218
2219 /* Figure out appropriate versions for all the symbols. We may not
2220 have the version number script until we have read all of the input
2221 files, so until that point we don't know which symbols should be
2222 local. This function is called via elf_link_hash_traverse. */
2223
2224 static bfd_boolean
2225 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2226 {
2227 struct elf_info_failed *sinfo;
2228 struct bfd_link_info *info;
2229 const struct elf_backend_data *bed;
2230 struct elf_info_failed eif;
2231 char *p;
2232
2233 sinfo = (struct elf_info_failed *) data;
2234 info = sinfo->info;
2235
2236 /* Fix the symbol flags. */
2237 eif.failed = FALSE;
2238 eif.info = info;
2239 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2240 {
2241 if (eif.failed)
2242 sinfo->failed = TRUE;
2243 return FALSE;
2244 }
2245
2246 /* We only need version numbers for symbols defined in regular
2247 objects. */
2248 if (!h->def_regular)
2249 return TRUE;
2250
2251 bed = get_elf_backend_data (info->output_bfd);
2252 p = strchr (h->root.root.string, ELF_VER_CHR);
2253 if (p != NULL && h->verinfo.vertree == NULL)
2254 {
2255 struct bfd_elf_version_tree *t;
2256
2257 ++p;
2258 if (*p == ELF_VER_CHR)
2259 ++p;
2260
2261 /* If there is no version string, we can just return out. */
2262 if (*p == '\0')
2263 return TRUE;
2264
2265 /* Look for the version. If we find it, it is no longer weak. */
2266 for (t = sinfo->info->version_info; t != NULL; t = t->next)
2267 {
2268 if (strcmp (t->name, p) == 0)
2269 {
2270 size_t len;
2271 char *alc;
2272 struct bfd_elf_version_expr *d;
2273
2274 len = p - h->root.root.string;
2275 alc = (char *) bfd_malloc (len);
2276 if (alc == NULL)
2277 {
2278 sinfo->failed = TRUE;
2279 return FALSE;
2280 }
2281 memcpy (alc, h->root.root.string, len - 1);
2282 alc[len - 1] = '\0';
2283 if (alc[len - 2] == ELF_VER_CHR)
2284 alc[len - 2] = '\0';
2285
2286 h->verinfo.vertree = t;
2287 t->used = TRUE;
2288 d = NULL;
2289
2290 if (t->globals.list != NULL)
2291 d = (*t->match) (&t->globals, NULL, alc);
2292
2293 /* See if there is anything to force this symbol to
2294 local scope. */
2295 if (d == NULL && t->locals.list != NULL)
2296 {
2297 d = (*t->match) (&t->locals, NULL, alc);
2298 if (d != NULL
2299 && h->dynindx != -1
2300 && ! info->export_dynamic)
2301 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2302 }
2303
2304 free (alc);
2305 break;
2306 }
2307 }
2308
2309 /* If we are building an application, we need to create a
2310 version node for this version. */
2311 if (t == NULL && bfd_link_executable (info))
2312 {
2313 struct bfd_elf_version_tree **pp;
2314 int version_index;
2315
2316 /* If we aren't going to export this symbol, we don't need
2317 to worry about it. */
2318 if (h->dynindx == -1)
2319 return TRUE;
2320
2321 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2322 sizeof *t);
2323 if (t == NULL)
2324 {
2325 sinfo->failed = TRUE;
2326 return FALSE;
2327 }
2328
2329 t->name = p;
2330 t->name_indx = (unsigned int) -1;
2331 t->used = TRUE;
2332
2333 version_index = 1;
2334 /* Don't count anonymous version tag. */
2335 if (sinfo->info->version_info != NULL
2336 && sinfo->info->version_info->vernum == 0)
2337 version_index = 0;
2338 for (pp = &sinfo->info->version_info;
2339 *pp != NULL;
2340 pp = &(*pp)->next)
2341 ++version_index;
2342 t->vernum = version_index;
2343
2344 *pp = t;
2345
2346 h->verinfo.vertree = t;
2347 }
2348 else if (t == NULL)
2349 {
2350 /* We could not find the version for a symbol when
2351 generating a shared archive. Return an error. */
2352 _bfd_error_handler
2353 /* xgettext:c-format */
2354 (_("%B: version node not found for symbol %s"),
2355 info->output_bfd, h->root.root.string);
2356 bfd_set_error (bfd_error_bad_value);
2357 sinfo->failed = TRUE;
2358 return FALSE;
2359 }
2360 }
2361
2362 /* If we don't have a version for this symbol, see if we can find
2363 something. */
2364 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
2365 {
2366 bfd_boolean hide;
2367
2368 h->verinfo.vertree
2369 = bfd_find_version_for_sym (sinfo->info->version_info,
2370 h->root.root.string, &hide);
2371 if (h->verinfo.vertree != NULL && hide)
2372 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2373 }
2374
2375 return TRUE;
2376 }
2377 \f
2378 /* Read and swap the relocs from the section indicated by SHDR. This
2379 may be either a REL or a RELA section. The relocations are
2380 translated into RELA relocations and stored in INTERNAL_RELOCS,
2381 which should have already been allocated to contain enough space.
2382 The EXTERNAL_RELOCS are a buffer where the external form of the
2383 relocations should be stored.
2384
2385 Returns FALSE if something goes wrong. */
2386
2387 static bfd_boolean
2388 elf_link_read_relocs_from_section (bfd *abfd,
2389 asection *sec,
2390 Elf_Internal_Shdr *shdr,
2391 void *external_relocs,
2392 Elf_Internal_Rela *internal_relocs)
2393 {
2394 const struct elf_backend_data *bed;
2395 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2396 const bfd_byte *erela;
2397 const bfd_byte *erelaend;
2398 Elf_Internal_Rela *irela;
2399 Elf_Internal_Shdr *symtab_hdr;
2400 size_t nsyms;
2401
2402 /* Position ourselves at the start of the section. */
2403 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2404 return FALSE;
2405
2406 /* Read the relocations. */
2407 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2408 return FALSE;
2409
2410 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2411 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2412
2413 bed = get_elf_backend_data (abfd);
2414
2415 /* Convert the external relocations to the internal format. */
2416 if (shdr->sh_entsize == bed->s->sizeof_rel)
2417 swap_in = bed->s->swap_reloc_in;
2418 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2419 swap_in = bed->s->swap_reloca_in;
2420 else
2421 {
2422 bfd_set_error (bfd_error_wrong_format);
2423 return FALSE;
2424 }
2425
2426 erela = (const bfd_byte *) external_relocs;
2427 erelaend = erela + shdr->sh_size;
2428 irela = internal_relocs;
2429 while (erela < erelaend)
2430 {
2431 bfd_vma r_symndx;
2432
2433 (*swap_in) (abfd, erela, irela);
2434 r_symndx = ELF32_R_SYM (irela->r_info);
2435 if (bed->s->arch_size == 64)
2436 r_symndx >>= 24;
2437 if (nsyms > 0)
2438 {
2439 if ((size_t) r_symndx >= nsyms)
2440 {
2441 _bfd_error_handler
2442 /* xgettext:c-format */
2443 (_("%B: bad reloc symbol index (%#Lx >= %#lx)"
2444 " for offset %#Lx in section `%A'"),
2445 abfd, r_symndx, (unsigned long) nsyms,
2446 irela->r_offset, sec);
2447 bfd_set_error (bfd_error_bad_value);
2448 return FALSE;
2449 }
2450 }
2451 else if (r_symndx != STN_UNDEF)
2452 {
2453 _bfd_error_handler
2454 /* xgettext:c-format */
2455 (_("%B: non-zero symbol index (%#Lx)"
2456 " for offset %#Lx in section `%A'"
2457 " when the object file has no symbol table"),
2458 abfd, r_symndx,
2459 irela->r_offset, sec);
2460 bfd_set_error (bfd_error_bad_value);
2461 return FALSE;
2462 }
2463 irela += bed->s->int_rels_per_ext_rel;
2464 erela += shdr->sh_entsize;
2465 }
2466
2467 return TRUE;
2468 }
2469
2470 /* Read and swap the relocs for a section O. They may have been
2471 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2472 not NULL, they are used as buffers to read into. They are known to
2473 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2474 the return value is allocated using either malloc or bfd_alloc,
2475 according to the KEEP_MEMORY argument. If O has two relocation
2476 sections (both REL and RELA relocations), then the REL_HDR
2477 relocations will appear first in INTERNAL_RELOCS, followed by the
2478 RELA_HDR relocations. */
2479
2480 Elf_Internal_Rela *
2481 _bfd_elf_link_read_relocs (bfd *abfd,
2482 asection *o,
2483 void *external_relocs,
2484 Elf_Internal_Rela *internal_relocs,
2485 bfd_boolean keep_memory)
2486 {
2487 void *alloc1 = NULL;
2488 Elf_Internal_Rela *alloc2 = NULL;
2489 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2490 struct bfd_elf_section_data *esdo = elf_section_data (o);
2491 Elf_Internal_Rela *internal_rela_relocs;
2492
2493 if (esdo->relocs != NULL)
2494 return esdo->relocs;
2495
2496 if (o->reloc_count == 0)
2497 return NULL;
2498
2499 if (internal_relocs == NULL)
2500 {
2501 bfd_size_type size;
2502
2503 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2504 if (keep_memory)
2505 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2506 else
2507 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2508 if (internal_relocs == NULL)
2509 goto error_return;
2510 }
2511
2512 if (external_relocs == NULL)
2513 {
2514 bfd_size_type size = 0;
2515
2516 if (esdo->rel.hdr)
2517 size += esdo->rel.hdr->sh_size;
2518 if (esdo->rela.hdr)
2519 size += esdo->rela.hdr->sh_size;
2520
2521 alloc1 = bfd_malloc (size);
2522 if (alloc1 == NULL)
2523 goto error_return;
2524 external_relocs = alloc1;
2525 }
2526
2527 internal_rela_relocs = internal_relocs;
2528 if (esdo->rel.hdr)
2529 {
2530 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2531 external_relocs,
2532 internal_relocs))
2533 goto error_return;
2534 external_relocs = (((bfd_byte *) external_relocs)
2535 + esdo->rel.hdr->sh_size);
2536 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2537 * bed->s->int_rels_per_ext_rel);
2538 }
2539
2540 if (esdo->rela.hdr
2541 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2542 external_relocs,
2543 internal_rela_relocs)))
2544 goto error_return;
2545
2546 /* Cache the results for next time, if we can. */
2547 if (keep_memory)
2548 esdo->relocs = internal_relocs;
2549
2550 if (alloc1 != NULL)
2551 free (alloc1);
2552
2553 /* Don't free alloc2, since if it was allocated we are passing it
2554 back (under the name of internal_relocs). */
2555
2556 return internal_relocs;
2557
2558 error_return:
2559 if (alloc1 != NULL)
2560 free (alloc1);
2561 if (alloc2 != NULL)
2562 {
2563 if (keep_memory)
2564 bfd_release (abfd, alloc2);
2565 else
2566 free (alloc2);
2567 }
2568 return NULL;
2569 }
2570
2571 /* Compute the size of, and allocate space for, REL_HDR which is the
2572 section header for a section containing relocations for O. */
2573
2574 static bfd_boolean
2575 _bfd_elf_link_size_reloc_section (bfd *abfd,
2576 struct bfd_elf_section_reloc_data *reldata)
2577 {
2578 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2579
2580 /* That allows us to calculate the size of the section. */
2581 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2582
2583 /* The contents field must last into write_object_contents, so we
2584 allocate it with bfd_alloc rather than malloc. Also since we
2585 cannot be sure that the contents will actually be filled in,
2586 we zero the allocated space. */
2587 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2588 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2589 return FALSE;
2590
2591 if (reldata->hashes == NULL && reldata->count)
2592 {
2593 struct elf_link_hash_entry **p;
2594
2595 p = ((struct elf_link_hash_entry **)
2596 bfd_zmalloc (reldata->count * sizeof (*p)));
2597 if (p == NULL)
2598 return FALSE;
2599
2600 reldata->hashes = p;
2601 }
2602
2603 return TRUE;
2604 }
2605
2606 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2607 originated from the section given by INPUT_REL_HDR) to the
2608 OUTPUT_BFD. */
2609
2610 bfd_boolean
2611 _bfd_elf_link_output_relocs (bfd *output_bfd,
2612 asection *input_section,
2613 Elf_Internal_Shdr *input_rel_hdr,
2614 Elf_Internal_Rela *internal_relocs,
2615 struct elf_link_hash_entry **rel_hash
2616 ATTRIBUTE_UNUSED)
2617 {
2618 Elf_Internal_Rela *irela;
2619 Elf_Internal_Rela *irelaend;
2620 bfd_byte *erel;
2621 struct bfd_elf_section_reloc_data *output_reldata;
2622 asection *output_section;
2623 const struct elf_backend_data *bed;
2624 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2625 struct bfd_elf_section_data *esdo;
2626
2627 output_section = input_section->output_section;
2628
2629 bed = get_elf_backend_data (output_bfd);
2630 esdo = elf_section_data (output_section);
2631 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2632 {
2633 output_reldata = &esdo->rel;
2634 swap_out = bed->s->swap_reloc_out;
2635 }
2636 else if (esdo->rela.hdr
2637 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2638 {
2639 output_reldata = &esdo->rela;
2640 swap_out = bed->s->swap_reloca_out;
2641 }
2642 else
2643 {
2644 _bfd_error_handler
2645 /* xgettext:c-format */
2646 (_("%B: relocation size mismatch in %B section %A"),
2647 output_bfd, input_section->owner, input_section);
2648 bfd_set_error (bfd_error_wrong_format);
2649 return FALSE;
2650 }
2651
2652 erel = output_reldata->hdr->contents;
2653 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2654 irela = internal_relocs;
2655 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2656 * bed->s->int_rels_per_ext_rel);
2657 while (irela < irelaend)
2658 {
2659 (*swap_out) (output_bfd, irela, erel);
2660 irela += bed->s->int_rels_per_ext_rel;
2661 erel += input_rel_hdr->sh_entsize;
2662 }
2663
2664 /* Bump the counter, so that we know where to add the next set of
2665 relocations. */
2666 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2667
2668 return TRUE;
2669 }
2670 \f
2671 /* Make weak undefined symbols in PIE dynamic. */
2672
2673 bfd_boolean
2674 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2675 struct elf_link_hash_entry *h)
2676 {
2677 if (bfd_link_pie (info)
2678 && h->dynindx == -1
2679 && h->root.type == bfd_link_hash_undefweak)
2680 return bfd_elf_link_record_dynamic_symbol (info, h);
2681
2682 return TRUE;
2683 }
2684
2685 /* Fix up the flags for a symbol. This handles various cases which
2686 can only be fixed after all the input files are seen. This is
2687 currently called by both adjust_dynamic_symbol and
2688 assign_sym_version, which is unnecessary but perhaps more robust in
2689 the face of future changes. */
2690
2691 static bfd_boolean
2692 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2693 struct elf_info_failed *eif)
2694 {
2695 const struct elf_backend_data *bed;
2696
2697 /* If this symbol was mentioned in a non-ELF file, try to set
2698 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2699 permit a non-ELF file to correctly refer to a symbol defined in
2700 an ELF dynamic object. */
2701 if (h->non_elf)
2702 {
2703 while (h->root.type == bfd_link_hash_indirect)
2704 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2705
2706 if (h->root.type != bfd_link_hash_defined
2707 && h->root.type != bfd_link_hash_defweak)
2708 {
2709 h->ref_regular = 1;
2710 h->ref_regular_nonweak = 1;
2711 }
2712 else
2713 {
2714 if (h->root.u.def.section->owner != NULL
2715 && (bfd_get_flavour (h->root.u.def.section->owner)
2716 == bfd_target_elf_flavour))
2717 {
2718 h->ref_regular = 1;
2719 h->ref_regular_nonweak = 1;
2720 }
2721 else
2722 h->def_regular = 1;
2723 }
2724
2725 if (h->dynindx == -1
2726 && (h->def_dynamic
2727 || h->ref_dynamic))
2728 {
2729 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2730 {
2731 eif->failed = TRUE;
2732 return FALSE;
2733 }
2734 }
2735 }
2736 else
2737 {
2738 /* Unfortunately, NON_ELF is only correct if the symbol
2739 was first seen in a non-ELF file. Fortunately, if the symbol
2740 was first seen in an ELF file, we're probably OK unless the
2741 symbol was defined in a non-ELF file. Catch that case here.
2742 FIXME: We're still in trouble if the symbol was first seen in
2743 a dynamic object, and then later in a non-ELF regular object. */
2744 if ((h->root.type == bfd_link_hash_defined
2745 || h->root.type == bfd_link_hash_defweak)
2746 && !h->def_regular
2747 && (h->root.u.def.section->owner != NULL
2748 ? (bfd_get_flavour (h->root.u.def.section->owner)
2749 != bfd_target_elf_flavour)
2750 : (bfd_is_abs_section (h->root.u.def.section)
2751 && !h->def_dynamic)))
2752 h->def_regular = 1;
2753 }
2754
2755 /* Backend specific symbol fixup. */
2756 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2757 if (bed->elf_backend_fixup_symbol
2758 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2759 return FALSE;
2760
2761 /* If this is a final link, and the symbol was defined as a common
2762 symbol in a regular object file, and there was no definition in
2763 any dynamic object, then the linker will have allocated space for
2764 the symbol in a common section but the DEF_REGULAR
2765 flag will not have been set. */
2766 if (h->root.type == bfd_link_hash_defined
2767 && !h->def_regular
2768 && h->ref_regular
2769 && !h->def_dynamic
2770 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2771 h->def_regular = 1;
2772
2773 /* If a weak undefined symbol has non-default visibility, we also
2774 hide it from the dynamic linker. */
2775 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2776 && h->root.type == bfd_link_hash_undefweak)
2777 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2778
2779 /* A hidden versioned symbol in executable should be forced local if
2780 it is is locally defined, not referenced by shared library and not
2781 exported. */
2782 else if (bfd_link_executable (eif->info)
2783 && h->versioned == versioned_hidden
2784 && !eif->info->export_dynamic
2785 && !h->dynamic
2786 && !h->ref_dynamic
2787 && h->def_regular)
2788 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2789
2790 /* If -Bsymbolic was used (which means to bind references to global
2791 symbols to the definition within the shared object), and this
2792 symbol was defined in a regular object, then it actually doesn't
2793 need a PLT entry. Likewise, if the symbol has non-default
2794 visibility. If the symbol has hidden or internal visibility, we
2795 will force it local. */
2796 else if (h->needs_plt
2797 && bfd_link_pic (eif->info)
2798 && is_elf_hash_table (eif->info->hash)
2799 && (SYMBOLIC_BIND (eif->info, h)
2800 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2801 && h->def_regular)
2802 {
2803 bfd_boolean force_local;
2804
2805 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2806 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2807 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2808 }
2809
2810 /* If this is a weak defined symbol in a dynamic object, and we know
2811 the real definition in the dynamic object, copy interesting flags
2812 over to the real definition. */
2813 if (h->is_weakalias)
2814 {
2815 struct elf_link_hash_entry *def = weakdef (h);
2816
2817 /* If the real definition is defined by a regular object file,
2818 don't do anything special. See the longer description in
2819 _bfd_elf_adjust_dynamic_symbol, below. */
2820 if (def->def_regular)
2821 {
2822 h = def;
2823 while ((h = h->u.alias) != def)
2824 h->is_weakalias = 0;
2825 }
2826 else
2827 {
2828 while (h->root.type == bfd_link_hash_indirect)
2829 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2830 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2831 || h->root.type == bfd_link_hash_defweak);
2832 BFD_ASSERT (def->def_dynamic);
2833 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
2834 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
2835 }
2836 }
2837
2838 return TRUE;
2839 }
2840
2841 /* Make the backend pick a good value for a dynamic symbol. This is
2842 called via elf_link_hash_traverse, and also calls itself
2843 recursively. */
2844
2845 static bfd_boolean
2846 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2847 {
2848 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2849 struct elf_link_hash_table *htab;
2850 const struct elf_backend_data *bed;
2851
2852 if (! is_elf_hash_table (eif->info->hash))
2853 return FALSE;
2854
2855 /* Ignore indirect symbols. These are added by the versioning code. */
2856 if (h->root.type == bfd_link_hash_indirect)
2857 return TRUE;
2858
2859 /* Fix the symbol flags. */
2860 if (! _bfd_elf_fix_symbol_flags (h, eif))
2861 return FALSE;
2862
2863 htab = elf_hash_table (eif->info);
2864 bed = get_elf_backend_data (htab->dynobj);
2865
2866 if (h->root.type == bfd_link_hash_undefweak)
2867 {
2868 if (eif->info->dynamic_undefined_weak == 0)
2869 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2870 else if (eif->info->dynamic_undefined_weak > 0
2871 && h->ref_regular
2872 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2873 && !bfd_hide_sym_by_version (eif->info->version_info,
2874 h->root.root.string))
2875 {
2876 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2877 {
2878 eif->failed = TRUE;
2879 return FALSE;
2880 }
2881 }
2882 }
2883
2884 /* If this symbol does not require a PLT entry, and it is not
2885 defined by a dynamic object, or is not referenced by a regular
2886 object, ignore it. We do have to handle a weak defined symbol,
2887 even if no regular object refers to it, if we decided to add it
2888 to the dynamic symbol table. FIXME: Do we normally need to worry
2889 about symbols which are defined by one dynamic object and
2890 referenced by another one? */
2891 if (!h->needs_plt
2892 && h->type != STT_GNU_IFUNC
2893 && (h->def_regular
2894 || !h->def_dynamic
2895 || (!h->ref_regular
2896 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
2897 {
2898 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2899 return TRUE;
2900 }
2901
2902 /* If we've already adjusted this symbol, don't do it again. This
2903 can happen via a recursive call. */
2904 if (h->dynamic_adjusted)
2905 return TRUE;
2906
2907 /* Don't look at this symbol again. Note that we must set this
2908 after checking the above conditions, because we may look at a
2909 symbol once, decide not to do anything, and then get called
2910 recursively later after REF_REGULAR is set below. */
2911 h->dynamic_adjusted = 1;
2912
2913 /* If this is a weak definition, and we know a real definition, and
2914 the real symbol is not itself defined by a regular object file,
2915 then get a good value for the real definition. We handle the
2916 real symbol first, for the convenience of the backend routine.
2917
2918 Note that there is a confusing case here. If the real definition
2919 is defined by a regular object file, we don't get the real symbol
2920 from the dynamic object, but we do get the weak symbol. If the
2921 processor backend uses a COPY reloc, then if some routine in the
2922 dynamic object changes the real symbol, we will not see that
2923 change in the corresponding weak symbol. This is the way other
2924 ELF linkers work as well, and seems to be a result of the shared
2925 library model.
2926
2927 I will clarify this issue. Most SVR4 shared libraries define the
2928 variable _timezone and define timezone as a weak synonym. The
2929 tzset call changes _timezone. If you write
2930 extern int timezone;
2931 int _timezone = 5;
2932 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2933 you might expect that, since timezone is a synonym for _timezone,
2934 the same number will print both times. However, if the processor
2935 backend uses a COPY reloc, then actually timezone will be copied
2936 into your process image, and, since you define _timezone
2937 yourself, _timezone will not. Thus timezone and _timezone will
2938 wind up at different memory locations. The tzset call will set
2939 _timezone, leaving timezone unchanged. */
2940
2941 if (h->is_weakalias)
2942 {
2943 struct elf_link_hash_entry *def = weakdef (h);
2944
2945 /* If we get to this point, there is an implicit reference to
2946 the alias by a regular object file via the weak symbol H. */
2947 def->ref_regular = 1;
2948
2949 /* Ensure that the backend adjust_dynamic_symbol function sees
2950 the strong alias before H by recursively calling ourselves. */
2951 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
2952 return FALSE;
2953 }
2954
2955 /* If a symbol has no type and no size and does not require a PLT
2956 entry, then we are probably about to do the wrong thing here: we
2957 are probably going to create a COPY reloc for an empty object.
2958 This case can arise when a shared object is built with assembly
2959 code, and the assembly code fails to set the symbol type. */
2960 if (h->size == 0
2961 && h->type == STT_NOTYPE
2962 && !h->needs_plt)
2963 _bfd_error_handler
2964 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2965 h->root.root.string);
2966
2967 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2968 {
2969 eif->failed = TRUE;
2970 return FALSE;
2971 }
2972
2973 return TRUE;
2974 }
2975
2976 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2977 DYNBSS. */
2978
2979 bfd_boolean
2980 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2981 struct elf_link_hash_entry *h,
2982 asection *dynbss)
2983 {
2984 unsigned int power_of_two;
2985 bfd_vma mask;
2986 asection *sec = h->root.u.def.section;
2987
2988 /* The section alignment of the definition is the maximum alignment
2989 requirement of symbols defined in the section. Since we don't
2990 know the symbol alignment requirement, we start with the
2991 maximum alignment and check low bits of the symbol address
2992 for the minimum alignment. */
2993 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2994 mask = ((bfd_vma) 1 << power_of_two) - 1;
2995 while ((h->root.u.def.value & mask) != 0)
2996 {
2997 mask >>= 1;
2998 --power_of_two;
2999 }
3000
3001 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
3002 dynbss))
3003 {
3004 /* Adjust the section alignment if needed. */
3005 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
3006 power_of_two))
3007 return FALSE;
3008 }
3009
3010 /* We make sure that the symbol will be aligned properly. */
3011 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3012
3013 /* Define the symbol as being at this point in DYNBSS. */
3014 h->root.u.def.section = dynbss;
3015 h->root.u.def.value = dynbss->size;
3016
3017 /* Increment the size of DYNBSS to make room for the symbol. */
3018 dynbss->size += h->size;
3019
3020 /* No error if extern_protected_data is true. */
3021 if (h->protected_def
3022 && (!info->extern_protected_data
3023 || (info->extern_protected_data < 0
3024 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3025 info->callbacks->einfo
3026 (_("%P: copy reloc against protected `%T' is dangerous\n"),
3027 h->root.root.string);
3028
3029 return TRUE;
3030 }
3031
3032 /* Adjust all external symbols pointing into SEC_MERGE sections
3033 to reflect the object merging within the sections. */
3034
3035 static bfd_boolean
3036 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3037 {
3038 asection *sec;
3039
3040 if ((h->root.type == bfd_link_hash_defined
3041 || h->root.type == bfd_link_hash_defweak)
3042 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3043 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3044 {
3045 bfd *output_bfd = (bfd *) data;
3046
3047 h->root.u.def.value =
3048 _bfd_merged_section_offset (output_bfd,
3049 &h->root.u.def.section,
3050 elf_section_data (sec)->sec_info,
3051 h->root.u.def.value);
3052 }
3053
3054 return TRUE;
3055 }
3056
3057 /* Returns false if the symbol referred to by H should be considered
3058 to resolve local to the current module, and true if it should be
3059 considered to bind dynamically. */
3060
3061 bfd_boolean
3062 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3063 struct bfd_link_info *info,
3064 bfd_boolean not_local_protected)
3065 {
3066 bfd_boolean binding_stays_local_p;
3067 const struct elf_backend_data *bed;
3068 struct elf_link_hash_table *hash_table;
3069
3070 if (h == NULL)
3071 return FALSE;
3072
3073 while (h->root.type == bfd_link_hash_indirect
3074 || h->root.type == bfd_link_hash_warning)
3075 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3076
3077 /* If it was forced local, then clearly it's not dynamic. */
3078 if (h->dynindx == -1)
3079 return FALSE;
3080 if (h->forced_local)
3081 return FALSE;
3082
3083 /* Identify the cases where name binding rules say that a
3084 visible symbol resolves locally. */
3085 binding_stays_local_p = (bfd_link_executable (info)
3086 || SYMBOLIC_BIND (info, h));
3087
3088 switch (ELF_ST_VISIBILITY (h->other))
3089 {
3090 case STV_INTERNAL:
3091 case STV_HIDDEN:
3092 return FALSE;
3093
3094 case STV_PROTECTED:
3095 hash_table = elf_hash_table (info);
3096 if (!is_elf_hash_table (hash_table))
3097 return FALSE;
3098
3099 bed = get_elf_backend_data (hash_table->dynobj);
3100
3101 /* Proper resolution for function pointer equality may require
3102 that these symbols perhaps be resolved dynamically, even though
3103 we should be resolving them to the current module. */
3104 if (!not_local_protected || !bed->is_function_type (h->type))
3105 binding_stays_local_p = TRUE;
3106 break;
3107
3108 default:
3109 break;
3110 }
3111
3112 /* If it isn't defined locally, then clearly it's dynamic. */
3113 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3114 return TRUE;
3115
3116 /* Otherwise, the symbol is dynamic if binding rules don't tell
3117 us that it remains local. */
3118 return !binding_stays_local_p;
3119 }
3120
3121 /* Return true if the symbol referred to by H should be considered
3122 to resolve local to the current module, and false otherwise. Differs
3123 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3124 undefined symbols. The two functions are virtually identical except
3125 for the place where dynindx == -1 is tested. If that test is true,
3126 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3127 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3128 defined symbols.
3129 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3130 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3131 treatment of undefined weak symbols. For those that do not make
3132 undefined weak symbols dynamic, both functions may return false. */
3133
3134 bfd_boolean
3135 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3136 struct bfd_link_info *info,
3137 bfd_boolean local_protected)
3138 {
3139 const struct elf_backend_data *bed;
3140 struct elf_link_hash_table *hash_table;
3141
3142 /* If it's a local sym, of course we resolve locally. */
3143 if (h == NULL)
3144 return TRUE;
3145
3146 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3147 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3148 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3149 return TRUE;
3150
3151 /* Forced local symbols resolve locally. */
3152 if (h->forced_local)
3153 return TRUE;
3154
3155 /* Common symbols that become definitions don't get the DEF_REGULAR
3156 flag set, so test it first, and don't bail out. */
3157 if (ELF_COMMON_DEF_P (h))
3158 /* Do nothing. */;
3159 /* If we don't have a definition in a regular file, then we can't
3160 resolve locally. The sym is either undefined or dynamic. */
3161 else if (!h->def_regular)
3162 return FALSE;
3163
3164 /* Non-dynamic symbols resolve locally. */
3165 if (h->dynindx == -1)
3166 return TRUE;
3167
3168 /* At this point, we know the symbol is defined and dynamic. In an
3169 executable it must resolve locally, likewise when building symbolic
3170 shared libraries. */
3171 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3172 return TRUE;
3173
3174 /* Now deal with defined dynamic symbols in shared libraries. Ones
3175 with default visibility might not resolve locally. */
3176 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3177 return FALSE;
3178
3179 hash_table = elf_hash_table (info);
3180 if (!is_elf_hash_table (hash_table))
3181 return TRUE;
3182
3183 bed = get_elf_backend_data (hash_table->dynobj);
3184
3185 /* If extern_protected_data is false, STV_PROTECTED non-function
3186 symbols are local. */
3187 if ((!info->extern_protected_data
3188 || (info->extern_protected_data < 0
3189 && !bed->extern_protected_data))
3190 && !bed->is_function_type (h->type))
3191 return TRUE;
3192
3193 /* Function pointer equality tests may require that STV_PROTECTED
3194 symbols be treated as dynamic symbols. If the address of a
3195 function not defined in an executable is set to that function's
3196 plt entry in the executable, then the address of the function in
3197 a shared library must also be the plt entry in the executable. */
3198 return local_protected;
3199 }
3200
3201 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3202 aligned. Returns the first TLS output section. */
3203
3204 struct bfd_section *
3205 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3206 {
3207 struct bfd_section *sec, *tls;
3208 unsigned int align = 0;
3209
3210 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3211 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3212 break;
3213 tls = sec;
3214
3215 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3216 if (sec->alignment_power > align)
3217 align = sec->alignment_power;
3218
3219 elf_hash_table (info)->tls_sec = tls;
3220
3221 /* Ensure the alignment of the first section is the largest alignment,
3222 so that the tls segment starts aligned. */
3223 if (tls != NULL)
3224 tls->alignment_power = align;
3225
3226 return tls;
3227 }
3228
3229 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3230 static bfd_boolean
3231 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3232 Elf_Internal_Sym *sym)
3233 {
3234 const struct elf_backend_data *bed;
3235
3236 /* Local symbols do not count, but target specific ones might. */
3237 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3238 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3239 return FALSE;
3240
3241 bed = get_elf_backend_data (abfd);
3242 /* Function symbols do not count. */
3243 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3244 return FALSE;
3245
3246 /* If the section is undefined, then so is the symbol. */
3247 if (sym->st_shndx == SHN_UNDEF)
3248 return FALSE;
3249
3250 /* If the symbol is defined in the common section, then
3251 it is a common definition and so does not count. */
3252 if (bed->common_definition (sym))
3253 return FALSE;
3254
3255 /* If the symbol is in a target specific section then we
3256 must rely upon the backend to tell us what it is. */
3257 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3258 /* FIXME - this function is not coded yet:
3259
3260 return _bfd_is_global_symbol_definition (abfd, sym);
3261
3262 Instead for now assume that the definition is not global,
3263 Even if this is wrong, at least the linker will behave
3264 in the same way that it used to do. */
3265 return FALSE;
3266
3267 return TRUE;
3268 }
3269
3270 /* Search the symbol table of the archive element of the archive ABFD
3271 whose archive map contains a mention of SYMDEF, and determine if
3272 the symbol is defined in this element. */
3273 static bfd_boolean
3274 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3275 {
3276 Elf_Internal_Shdr * hdr;
3277 size_t symcount;
3278 size_t extsymcount;
3279 size_t extsymoff;
3280 Elf_Internal_Sym *isymbuf;
3281 Elf_Internal_Sym *isym;
3282 Elf_Internal_Sym *isymend;
3283 bfd_boolean result;
3284
3285 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3286 if (abfd == NULL)
3287 return FALSE;
3288
3289 if (! bfd_check_format (abfd, bfd_object))
3290 return FALSE;
3291
3292 /* Select the appropriate symbol table. If we don't know if the
3293 object file is an IR object, give linker LTO plugin a chance to
3294 get the correct symbol table. */
3295 if (abfd->plugin_format == bfd_plugin_yes
3296 #if BFD_SUPPORTS_PLUGINS
3297 || (abfd->plugin_format == bfd_plugin_unknown
3298 && bfd_link_plugin_object_p (abfd))
3299 #endif
3300 )
3301 {
3302 /* Use the IR symbol table if the object has been claimed by
3303 plugin. */
3304 abfd = abfd->plugin_dummy_bfd;
3305 hdr = &elf_tdata (abfd)->symtab_hdr;
3306 }
3307 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3308 hdr = &elf_tdata (abfd)->symtab_hdr;
3309 else
3310 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3311
3312 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3313
3314 /* The sh_info field of the symtab header tells us where the
3315 external symbols start. We don't care about the local symbols. */
3316 if (elf_bad_symtab (abfd))
3317 {
3318 extsymcount = symcount;
3319 extsymoff = 0;
3320 }
3321 else
3322 {
3323 extsymcount = symcount - hdr->sh_info;
3324 extsymoff = hdr->sh_info;
3325 }
3326
3327 if (extsymcount == 0)
3328 return FALSE;
3329
3330 /* Read in the symbol table. */
3331 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3332 NULL, NULL, NULL);
3333 if (isymbuf == NULL)
3334 return FALSE;
3335
3336 /* Scan the symbol table looking for SYMDEF. */
3337 result = FALSE;
3338 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3339 {
3340 const char *name;
3341
3342 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3343 isym->st_name);
3344 if (name == NULL)
3345 break;
3346
3347 if (strcmp (name, symdef->name) == 0)
3348 {
3349 result = is_global_data_symbol_definition (abfd, isym);
3350 break;
3351 }
3352 }
3353
3354 free (isymbuf);
3355
3356 return result;
3357 }
3358 \f
3359 /* Add an entry to the .dynamic table. */
3360
3361 bfd_boolean
3362 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3363 bfd_vma tag,
3364 bfd_vma val)
3365 {
3366 struct elf_link_hash_table *hash_table;
3367 const struct elf_backend_data *bed;
3368 asection *s;
3369 bfd_size_type newsize;
3370 bfd_byte *newcontents;
3371 Elf_Internal_Dyn dyn;
3372
3373 hash_table = elf_hash_table (info);
3374 if (! is_elf_hash_table (hash_table))
3375 return FALSE;
3376
3377 bed = get_elf_backend_data (hash_table->dynobj);
3378 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3379 BFD_ASSERT (s != NULL);
3380
3381 newsize = s->size + bed->s->sizeof_dyn;
3382 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3383 if (newcontents == NULL)
3384 return FALSE;
3385
3386 dyn.d_tag = tag;
3387 dyn.d_un.d_val = val;
3388 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3389
3390 s->size = newsize;
3391 s->contents = newcontents;
3392
3393 return TRUE;
3394 }
3395
3396 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3397 otherwise just check whether one already exists. Returns -1 on error,
3398 1 if a DT_NEEDED tag already exists, and 0 on success. */
3399
3400 static int
3401 elf_add_dt_needed_tag (bfd *abfd,
3402 struct bfd_link_info *info,
3403 const char *soname,
3404 bfd_boolean do_it)
3405 {
3406 struct elf_link_hash_table *hash_table;
3407 size_t strindex;
3408
3409 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3410 return -1;
3411
3412 hash_table = elf_hash_table (info);
3413 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3414 if (strindex == (size_t) -1)
3415 return -1;
3416
3417 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3418 {
3419 asection *sdyn;
3420 const struct elf_backend_data *bed;
3421 bfd_byte *extdyn;
3422
3423 bed = get_elf_backend_data (hash_table->dynobj);
3424 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3425 if (sdyn != NULL)
3426 for (extdyn = sdyn->contents;
3427 extdyn < sdyn->contents + sdyn->size;
3428 extdyn += bed->s->sizeof_dyn)
3429 {
3430 Elf_Internal_Dyn dyn;
3431
3432 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3433 if (dyn.d_tag == DT_NEEDED
3434 && dyn.d_un.d_val == strindex)
3435 {
3436 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3437 return 1;
3438 }
3439 }
3440 }
3441
3442 if (do_it)
3443 {
3444 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3445 return -1;
3446
3447 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3448 return -1;
3449 }
3450 else
3451 /* We were just checking for existence of the tag. */
3452 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3453
3454 return 0;
3455 }
3456
3457 /* Return true if SONAME is on the needed list between NEEDED and STOP
3458 (or the end of list if STOP is NULL), and needed by a library that
3459 will be loaded. */
3460
3461 static bfd_boolean
3462 on_needed_list (const char *soname,
3463 struct bfd_link_needed_list *needed,
3464 struct bfd_link_needed_list *stop)
3465 {
3466 struct bfd_link_needed_list *look;
3467 for (look = needed; look != stop; look = look->next)
3468 if (strcmp (soname, look->name) == 0
3469 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3470 /* If needed by a library that itself is not directly
3471 needed, recursively check whether that library is
3472 indirectly needed. Since we add DT_NEEDED entries to
3473 the end of the list, library dependencies appear after
3474 the library. Therefore search prior to the current
3475 LOOK, preventing possible infinite recursion. */
3476 || on_needed_list (elf_dt_name (look->by), needed, look)))
3477 return TRUE;
3478
3479 return FALSE;
3480 }
3481
3482 /* Sort symbol by value, section, and size. */
3483 static int
3484 elf_sort_symbol (const void *arg1, const void *arg2)
3485 {
3486 const struct elf_link_hash_entry *h1;
3487 const struct elf_link_hash_entry *h2;
3488 bfd_signed_vma vdiff;
3489
3490 h1 = *(const struct elf_link_hash_entry **) arg1;
3491 h2 = *(const struct elf_link_hash_entry **) arg2;
3492 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3493 if (vdiff != 0)
3494 return vdiff > 0 ? 1 : -1;
3495 else
3496 {
3497 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3498 if (sdiff != 0)
3499 return sdiff > 0 ? 1 : -1;
3500 }
3501 vdiff = h1->size - h2->size;
3502 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3503 }
3504
3505 /* This function is used to adjust offsets into .dynstr for
3506 dynamic symbols. This is called via elf_link_hash_traverse. */
3507
3508 static bfd_boolean
3509 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3510 {
3511 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3512
3513 if (h->dynindx != -1)
3514 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3515 return TRUE;
3516 }
3517
3518 /* Assign string offsets in .dynstr, update all structures referencing
3519 them. */
3520
3521 static bfd_boolean
3522 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3523 {
3524 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3525 struct elf_link_local_dynamic_entry *entry;
3526 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3527 bfd *dynobj = hash_table->dynobj;
3528 asection *sdyn;
3529 bfd_size_type size;
3530 const struct elf_backend_data *bed;
3531 bfd_byte *extdyn;
3532
3533 _bfd_elf_strtab_finalize (dynstr);
3534 size = _bfd_elf_strtab_size (dynstr);
3535
3536 bed = get_elf_backend_data (dynobj);
3537 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3538 BFD_ASSERT (sdyn != NULL);
3539
3540 /* Update all .dynamic entries referencing .dynstr strings. */
3541 for (extdyn = sdyn->contents;
3542 extdyn < sdyn->contents + sdyn->size;
3543 extdyn += bed->s->sizeof_dyn)
3544 {
3545 Elf_Internal_Dyn dyn;
3546
3547 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3548 switch (dyn.d_tag)
3549 {
3550 case DT_STRSZ:
3551 dyn.d_un.d_val = size;
3552 break;
3553 case DT_NEEDED:
3554 case DT_SONAME:
3555 case DT_RPATH:
3556 case DT_RUNPATH:
3557 case DT_FILTER:
3558 case DT_AUXILIARY:
3559 case DT_AUDIT:
3560 case DT_DEPAUDIT:
3561 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3562 break;
3563 default:
3564 continue;
3565 }
3566 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3567 }
3568
3569 /* Now update local dynamic symbols. */
3570 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3571 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3572 entry->isym.st_name);
3573
3574 /* And the rest of dynamic symbols. */
3575 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3576
3577 /* Adjust version definitions. */
3578 if (elf_tdata (output_bfd)->cverdefs)
3579 {
3580 asection *s;
3581 bfd_byte *p;
3582 size_t i;
3583 Elf_Internal_Verdef def;
3584 Elf_Internal_Verdaux defaux;
3585
3586 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3587 p = s->contents;
3588 do
3589 {
3590 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3591 &def);
3592 p += sizeof (Elf_External_Verdef);
3593 if (def.vd_aux != sizeof (Elf_External_Verdef))
3594 continue;
3595 for (i = 0; i < def.vd_cnt; ++i)
3596 {
3597 _bfd_elf_swap_verdaux_in (output_bfd,
3598 (Elf_External_Verdaux *) p, &defaux);
3599 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3600 defaux.vda_name);
3601 _bfd_elf_swap_verdaux_out (output_bfd,
3602 &defaux, (Elf_External_Verdaux *) p);
3603 p += sizeof (Elf_External_Verdaux);
3604 }
3605 }
3606 while (def.vd_next);
3607 }
3608
3609 /* Adjust version references. */
3610 if (elf_tdata (output_bfd)->verref)
3611 {
3612 asection *s;
3613 bfd_byte *p;
3614 size_t i;
3615 Elf_Internal_Verneed need;
3616 Elf_Internal_Vernaux needaux;
3617
3618 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3619 p = s->contents;
3620 do
3621 {
3622 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3623 &need);
3624 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3625 _bfd_elf_swap_verneed_out (output_bfd, &need,
3626 (Elf_External_Verneed *) p);
3627 p += sizeof (Elf_External_Verneed);
3628 for (i = 0; i < need.vn_cnt; ++i)
3629 {
3630 _bfd_elf_swap_vernaux_in (output_bfd,
3631 (Elf_External_Vernaux *) p, &needaux);
3632 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3633 needaux.vna_name);
3634 _bfd_elf_swap_vernaux_out (output_bfd,
3635 &needaux,
3636 (Elf_External_Vernaux *) p);
3637 p += sizeof (Elf_External_Vernaux);
3638 }
3639 }
3640 while (need.vn_next);
3641 }
3642
3643 return TRUE;
3644 }
3645 \f
3646 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3647 The default is to only match when the INPUT and OUTPUT are exactly
3648 the same target. */
3649
3650 bfd_boolean
3651 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3652 const bfd_target *output)
3653 {
3654 return input == output;
3655 }
3656
3657 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3658 This version is used when different targets for the same architecture
3659 are virtually identical. */
3660
3661 bfd_boolean
3662 _bfd_elf_relocs_compatible (const bfd_target *input,
3663 const bfd_target *output)
3664 {
3665 const struct elf_backend_data *obed, *ibed;
3666
3667 if (input == output)
3668 return TRUE;
3669
3670 ibed = xvec_get_elf_backend_data (input);
3671 obed = xvec_get_elf_backend_data (output);
3672
3673 if (ibed->arch != obed->arch)
3674 return FALSE;
3675
3676 /* If both backends are using this function, deem them compatible. */
3677 return ibed->relocs_compatible == obed->relocs_compatible;
3678 }
3679
3680 /* Make a special call to the linker "notice" function to tell it that
3681 we are about to handle an as-needed lib, or have finished
3682 processing the lib. */
3683
3684 bfd_boolean
3685 _bfd_elf_notice_as_needed (bfd *ibfd,
3686 struct bfd_link_info *info,
3687 enum notice_asneeded_action act)
3688 {
3689 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3690 }
3691
3692 /* Check relocations an ELF object file. */
3693
3694 bfd_boolean
3695 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3696 {
3697 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3698 struct elf_link_hash_table *htab = elf_hash_table (info);
3699
3700 /* If this object is the same format as the output object, and it is
3701 not a shared library, then let the backend look through the
3702 relocs.
3703
3704 This is required to build global offset table entries and to
3705 arrange for dynamic relocs. It is not required for the
3706 particular common case of linking non PIC code, even when linking
3707 against shared libraries, but unfortunately there is no way of
3708 knowing whether an object file has been compiled PIC or not.
3709 Looking through the relocs is not particularly time consuming.
3710 The problem is that we must either (1) keep the relocs in memory,
3711 which causes the linker to require additional runtime memory or
3712 (2) read the relocs twice from the input file, which wastes time.
3713 This would be a good case for using mmap.
3714
3715 I have no idea how to handle linking PIC code into a file of a
3716 different format. It probably can't be done. */
3717 if ((abfd->flags & DYNAMIC) == 0
3718 && is_elf_hash_table (htab)
3719 && bed->check_relocs != NULL
3720 && elf_object_id (abfd) == elf_hash_table_id (htab)
3721 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3722 {
3723 asection *o;
3724
3725 for (o = abfd->sections; o != NULL; o = o->next)
3726 {
3727 Elf_Internal_Rela *internal_relocs;
3728 bfd_boolean ok;
3729
3730 /* Don't check relocations in excluded sections. */
3731 if ((o->flags & SEC_RELOC) == 0
3732 || (o->flags & SEC_EXCLUDE) != 0
3733 || o->reloc_count == 0
3734 || ((info->strip == strip_all || info->strip == strip_debugger)
3735 && (o->flags & SEC_DEBUGGING) != 0)
3736 || bfd_is_abs_section (o->output_section))
3737 continue;
3738
3739 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3740 info->keep_memory);
3741 if (internal_relocs == NULL)
3742 return FALSE;
3743
3744 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3745
3746 if (elf_section_data (o)->relocs != internal_relocs)
3747 free (internal_relocs);
3748
3749 if (! ok)
3750 return FALSE;
3751 }
3752 }
3753
3754 return TRUE;
3755 }
3756
3757 /* Add symbols from an ELF object file to the linker hash table. */
3758
3759 static bfd_boolean
3760 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3761 {
3762 Elf_Internal_Ehdr *ehdr;
3763 Elf_Internal_Shdr *hdr;
3764 size_t symcount;
3765 size_t extsymcount;
3766 size_t extsymoff;
3767 struct elf_link_hash_entry **sym_hash;
3768 bfd_boolean dynamic;
3769 Elf_External_Versym *extversym = NULL;
3770 Elf_External_Versym *ever;
3771 struct elf_link_hash_entry *weaks;
3772 struct elf_link_hash_entry **nondeflt_vers = NULL;
3773 size_t nondeflt_vers_cnt = 0;
3774 Elf_Internal_Sym *isymbuf = NULL;
3775 Elf_Internal_Sym *isym;
3776 Elf_Internal_Sym *isymend;
3777 const struct elf_backend_data *bed;
3778 bfd_boolean add_needed;
3779 struct elf_link_hash_table *htab;
3780 bfd_size_type amt;
3781 void *alloc_mark = NULL;
3782 struct bfd_hash_entry **old_table = NULL;
3783 unsigned int old_size = 0;
3784 unsigned int old_count = 0;
3785 void *old_tab = NULL;
3786 void *old_ent;
3787 struct bfd_link_hash_entry *old_undefs = NULL;
3788 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3789 void *old_strtab = NULL;
3790 size_t tabsize = 0;
3791 asection *s;
3792 bfd_boolean just_syms;
3793
3794 htab = elf_hash_table (info);
3795 bed = get_elf_backend_data (abfd);
3796
3797 if ((abfd->flags & DYNAMIC) == 0)
3798 dynamic = FALSE;
3799 else
3800 {
3801 dynamic = TRUE;
3802
3803 /* You can't use -r against a dynamic object. Also, there's no
3804 hope of using a dynamic object which does not exactly match
3805 the format of the output file. */
3806 if (bfd_link_relocatable (info)
3807 || !is_elf_hash_table (htab)
3808 || info->output_bfd->xvec != abfd->xvec)
3809 {
3810 if (bfd_link_relocatable (info))
3811 bfd_set_error (bfd_error_invalid_operation);
3812 else
3813 bfd_set_error (bfd_error_wrong_format);
3814 goto error_return;
3815 }
3816 }
3817
3818 ehdr = elf_elfheader (abfd);
3819 if (info->warn_alternate_em
3820 && bed->elf_machine_code != ehdr->e_machine
3821 && ((bed->elf_machine_alt1 != 0
3822 && ehdr->e_machine == bed->elf_machine_alt1)
3823 || (bed->elf_machine_alt2 != 0
3824 && ehdr->e_machine == bed->elf_machine_alt2)))
3825 info->callbacks->einfo
3826 /* xgettext:c-format */
3827 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3828 ehdr->e_machine, abfd, bed->elf_machine_code);
3829
3830 /* As a GNU extension, any input sections which are named
3831 .gnu.warning.SYMBOL are treated as warning symbols for the given
3832 symbol. This differs from .gnu.warning sections, which generate
3833 warnings when they are included in an output file. */
3834 /* PR 12761: Also generate this warning when building shared libraries. */
3835 for (s = abfd->sections; s != NULL; s = s->next)
3836 {
3837 const char *name;
3838
3839 name = bfd_get_section_name (abfd, s);
3840 if (CONST_STRNEQ (name, ".gnu.warning."))
3841 {
3842 char *msg;
3843 bfd_size_type sz;
3844
3845 name += sizeof ".gnu.warning." - 1;
3846
3847 /* If this is a shared object, then look up the symbol
3848 in the hash table. If it is there, and it is already
3849 been defined, then we will not be using the entry
3850 from this shared object, so we don't need to warn.
3851 FIXME: If we see the definition in a regular object
3852 later on, we will warn, but we shouldn't. The only
3853 fix is to keep track of what warnings we are supposed
3854 to emit, and then handle them all at the end of the
3855 link. */
3856 if (dynamic)
3857 {
3858 struct elf_link_hash_entry *h;
3859
3860 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3861
3862 /* FIXME: What about bfd_link_hash_common? */
3863 if (h != NULL
3864 && (h->root.type == bfd_link_hash_defined
3865 || h->root.type == bfd_link_hash_defweak))
3866 continue;
3867 }
3868
3869 sz = s->size;
3870 msg = (char *) bfd_alloc (abfd, sz + 1);
3871 if (msg == NULL)
3872 goto error_return;
3873
3874 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3875 goto error_return;
3876
3877 msg[sz] = '\0';
3878
3879 if (! (_bfd_generic_link_add_one_symbol
3880 (info, abfd, name, BSF_WARNING, s, 0, msg,
3881 FALSE, bed->collect, NULL)))
3882 goto error_return;
3883
3884 if (bfd_link_executable (info))
3885 {
3886 /* Clobber the section size so that the warning does
3887 not get copied into the output file. */
3888 s->size = 0;
3889
3890 /* Also set SEC_EXCLUDE, so that symbols defined in
3891 the warning section don't get copied to the output. */
3892 s->flags |= SEC_EXCLUDE;
3893 }
3894 }
3895 }
3896
3897 just_syms = ((s = abfd->sections) != NULL
3898 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3899
3900 add_needed = TRUE;
3901 if (! dynamic)
3902 {
3903 /* If we are creating a shared library, create all the dynamic
3904 sections immediately. We need to attach them to something,
3905 so we attach them to this BFD, provided it is the right
3906 format and is not from ld --just-symbols. Always create the
3907 dynamic sections for -E/--dynamic-list. FIXME: If there
3908 are no input BFD's of the same format as the output, we can't
3909 make a shared library. */
3910 if (!just_syms
3911 && (bfd_link_pic (info)
3912 || (!bfd_link_relocatable (info)
3913 && info->nointerp
3914 && (info->export_dynamic || info->dynamic)))
3915 && is_elf_hash_table (htab)
3916 && info->output_bfd->xvec == abfd->xvec
3917 && !htab->dynamic_sections_created)
3918 {
3919 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3920 goto error_return;
3921 }
3922 }
3923 else if (!is_elf_hash_table (htab))
3924 goto error_return;
3925 else
3926 {
3927 const char *soname = NULL;
3928 char *audit = NULL;
3929 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3930 const Elf_Internal_Phdr *phdr;
3931 int ret;
3932
3933 /* ld --just-symbols and dynamic objects don't mix very well.
3934 ld shouldn't allow it. */
3935 if (just_syms)
3936 abort ();
3937
3938 /* If this dynamic lib was specified on the command line with
3939 --as-needed in effect, then we don't want to add a DT_NEEDED
3940 tag unless the lib is actually used. Similary for libs brought
3941 in by another lib's DT_NEEDED. When --no-add-needed is used
3942 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3943 any dynamic library in DT_NEEDED tags in the dynamic lib at
3944 all. */
3945 add_needed = (elf_dyn_lib_class (abfd)
3946 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3947 | DYN_NO_NEEDED)) == 0;
3948
3949 s = bfd_get_section_by_name (abfd, ".dynamic");
3950 if (s != NULL)
3951 {
3952 bfd_byte *dynbuf;
3953 bfd_byte *extdyn;
3954 unsigned int elfsec;
3955 unsigned long shlink;
3956
3957 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3958 {
3959 error_free_dyn:
3960 free (dynbuf);
3961 goto error_return;
3962 }
3963
3964 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3965 if (elfsec == SHN_BAD)
3966 goto error_free_dyn;
3967 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3968
3969 for (extdyn = dynbuf;
3970 extdyn < dynbuf + s->size;
3971 extdyn += bed->s->sizeof_dyn)
3972 {
3973 Elf_Internal_Dyn dyn;
3974
3975 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3976 if (dyn.d_tag == DT_SONAME)
3977 {
3978 unsigned int tagv = dyn.d_un.d_val;
3979 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3980 if (soname == NULL)
3981 goto error_free_dyn;
3982 }
3983 if (dyn.d_tag == DT_NEEDED)
3984 {
3985 struct bfd_link_needed_list *n, **pn;
3986 char *fnm, *anm;
3987 unsigned int tagv = dyn.d_un.d_val;
3988
3989 amt = sizeof (struct bfd_link_needed_list);
3990 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3991 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3992 if (n == NULL || fnm == NULL)
3993 goto error_free_dyn;
3994 amt = strlen (fnm) + 1;
3995 anm = (char *) bfd_alloc (abfd, amt);
3996 if (anm == NULL)
3997 goto error_free_dyn;
3998 memcpy (anm, fnm, amt);
3999 n->name = anm;
4000 n->by = abfd;
4001 n->next = NULL;
4002 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4003 ;
4004 *pn = n;
4005 }
4006 if (dyn.d_tag == DT_RUNPATH)
4007 {
4008 struct bfd_link_needed_list *n, **pn;
4009 char *fnm, *anm;
4010 unsigned int tagv = dyn.d_un.d_val;
4011
4012 amt = sizeof (struct bfd_link_needed_list);
4013 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4014 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4015 if (n == NULL || fnm == NULL)
4016 goto error_free_dyn;
4017 amt = strlen (fnm) + 1;
4018 anm = (char *) bfd_alloc (abfd, amt);
4019 if (anm == NULL)
4020 goto error_free_dyn;
4021 memcpy (anm, fnm, amt);
4022 n->name = anm;
4023 n->by = abfd;
4024 n->next = NULL;
4025 for (pn = & runpath;
4026 *pn != NULL;
4027 pn = &(*pn)->next)
4028 ;
4029 *pn = n;
4030 }
4031 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4032 if (!runpath && dyn.d_tag == DT_RPATH)
4033 {
4034 struct bfd_link_needed_list *n, **pn;
4035 char *fnm, *anm;
4036 unsigned int tagv = dyn.d_un.d_val;
4037
4038 amt = sizeof (struct bfd_link_needed_list);
4039 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4040 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4041 if (n == NULL || fnm == NULL)
4042 goto error_free_dyn;
4043 amt = strlen (fnm) + 1;
4044 anm = (char *) bfd_alloc (abfd, amt);
4045 if (anm == NULL)
4046 goto error_free_dyn;
4047 memcpy (anm, fnm, amt);
4048 n->name = anm;
4049 n->by = abfd;
4050 n->next = NULL;
4051 for (pn = & rpath;
4052 *pn != NULL;
4053 pn = &(*pn)->next)
4054 ;
4055 *pn = n;
4056 }
4057 if (dyn.d_tag == DT_AUDIT)
4058 {
4059 unsigned int tagv = dyn.d_un.d_val;
4060 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4061 }
4062 }
4063
4064 free (dynbuf);
4065 }
4066
4067 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4068 frees all more recently bfd_alloc'd blocks as well. */
4069 if (runpath)
4070 rpath = runpath;
4071
4072 if (rpath)
4073 {
4074 struct bfd_link_needed_list **pn;
4075 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4076 ;
4077 *pn = rpath;
4078 }
4079
4080 /* If we have a PT_GNU_RELRO program header, mark as read-only
4081 all sections contained fully therein. This makes relro
4082 shared library sections appear as they will at run-time. */
4083 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4084 while (--phdr >= elf_tdata (abfd)->phdr)
4085 if (phdr->p_type == PT_GNU_RELRO)
4086 {
4087 for (s = abfd->sections; s != NULL; s = s->next)
4088 if ((s->flags & SEC_ALLOC) != 0
4089 && s->vma >= phdr->p_vaddr
4090 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4091 s->flags |= SEC_READONLY;
4092 break;
4093 }
4094
4095 /* We do not want to include any of the sections in a dynamic
4096 object in the output file. We hack by simply clobbering the
4097 list of sections in the BFD. This could be handled more
4098 cleanly by, say, a new section flag; the existing
4099 SEC_NEVER_LOAD flag is not the one we want, because that one
4100 still implies that the section takes up space in the output
4101 file. */
4102 bfd_section_list_clear (abfd);
4103
4104 /* Find the name to use in a DT_NEEDED entry that refers to this
4105 object. If the object has a DT_SONAME entry, we use it.
4106 Otherwise, if the generic linker stuck something in
4107 elf_dt_name, we use that. Otherwise, we just use the file
4108 name. */
4109 if (soname == NULL || *soname == '\0')
4110 {
4111 soname = elf_dt_name (abfd);
4112 if (soname == NULL || *soname == '\0')
4113 soname = bfd_get_filename (abfd);
4114 }
4115
4116 /* Save the SONAME because sometimes the linker emulation code
4117 will need to know it. */
4118 elf_dt_name (abfd) = soname;
4119
4120 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4121 if (ret < 0)
4122 goto error_return;
4123
4124 /* If we have already included this dynamic object in the
4125 link, just ignore it. There is no reason to include a
4126 particular dynamic object more than once. */
4127 if (ret > 0)
4128 return TRUE;
4129
4130 /* Save the DT_AUDIT entry for the linker emulation code. */
4131 elf_dt_audit (abfd) = audit;
4132 }
4133
4134 /* If this is a dynamic object, we always link against the .dynsym
4135 symbol table, not the .symtab symbol table. The dynamic linker
4136 will only see the .dynsym symbol table, so there is no reason to
4137 look at .symtab for a dynamic object. */
4138
4139 if (! dynamic || elf_dynsymtab (abfd) == 0)
4140 hdr = &elf_tdata (abfd)->symtab_hdr;
4141 else
4142 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4143
4144 symcount = hdr->sh_size / bed->s->sizeof_sym;
4145
4146 /* The sh_info field of the symtab header tells us where the
4147 external symbols start. We don't care about the local symbols at
4148 this point. */
4149 if (elf_bad_symtab (abfd))
4150 {
4151 extsymcount = symcount;
4152 extsymoff = 0;
4153 }
4154 else
4155 {
4156 extsymcount = symcount - hdr->sh_info;
4157 extsymoff = hdr->sh_info;
4158 }
4159
4160 sym_hash = elf_sym_hashes (abfd);
4161 if (extsymcount != 0)
4162 {
4163 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4164 NULL, NULL, NULL);
4165 if (isymbuf == NULL)
4166 goto error_return;
4167
4168 if (sym_hash == NULL)
4169 {
4170 /* We store a pointer to the hash table entry for each
4171 external symbol. */
4172 amt = extsymcount;
4173 amt *= sizeof (struct elf_link_hash_entry *);
4174 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4175 if (sym_hash == NULL)
4176 goto error_free_sym;
4177 elf_sym_hashes (abfd) = sym_hash;
4178 }
4179 }
4180
4181 if (dynamic)
4182 {
4183 /* Read in any version definitions. */
4184 if (!_bfd_elf_slurp_version_tables (abfd,
4185 info->default_imported_symver))
4186 goto error_free_sym;
4187
4188 /* Read in the symbol versions, but don't bother to convert them
4189 to internal format. */
4190 if (elf_dynversym (abfd) != 0)
4191 {
4192 Elf_Internal_Shdr *versymhdr;
4193
4194 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4195 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4196 if (extversym == NULL)
4197 goto error_free_sym;
4198 amt = versymhdr->sh_size;
4199 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4200 || bfd_bread (extversym, amt, abfd) != amt)
4201 goto error_free_vers;
4202 }
4203 }
4204
4205 /* If we are loading an as-needed shared lib, save the symbol table
4206 state before we start adding symbols. If the lib turns out
4207 to be unneeded, restore the state. */
4208 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4209 {
4210 unsigned int i;
4211 size_t entsize;
4212
4213 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4214 {
4215 struct bfd_hash_entry *p;
4216 struct elf_link_hash_entry *h;
4217
4218 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4219 {
4220 h = (struct elf_link_hash_entry *) p;
4221 entsize += htab->root.table.entsize;
4222 if (h->root.type == bfd_link_hash_warning)
4223 entsize += htab->root.table.entsize;
4224 }
4225 }
4226
4227 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4228 old_tab = bfd_malloc (tabsize + entsize);
4229 if (old_tab == NULL)
4230 goto error_free_vers;
4231
4232 /* Remember the current objalloc pointer, so that all mem for
4233 symbols added can later be reclaimed. */
4234 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4235 if (alloc_mark == NULL)
4236 goto error_free_vers;
4237
4238 /* Make a special call to the linker "notice" function to
4239 tell it that we are about to handle an as-needed lib. */
4240 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4241 goto error_free_vers;
4242
4243 /* Clone the symbol table. Remember some pointers into the
4244 symbol table, and dynamic symbol count. */
4245 old_ent = (char *) old_tab + tabsize;
4246 memcpy (old_tab, htab->root.table.table, tabsize);
4247 old_undefs = htab->root.undefs;
4248 old_undefs_tail = htab->root.undefs_tail;
4249 old_table = htab->root.table.table;
4250 old_size = htab->root.table.size;
4251 old_count = htab->root.table.count;
4252 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4253 if (old_strtab == NULL)
4254 goto error_free_vers;
4255
4256 for (i = 0; i < htab->root.table.size; i++)
4257 {
4258 struct bfd_hash_entry *p;
4259 struct elf_link_hash_entry *h;
4260
4261 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4262 {
4263 memcpy (old_ent, p, htab->root.table.entsize);
4264 old_ent = (char *) old_ent + htab->root.table.entsize;
4265 h = (struct elf_link_hash_entry *) p;
4266 if (h->root.type == bfd_link_hash_warning)
4267 {
4268 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4269 old_ent = (char *) old_ent + htab->root.table.entsize;
4270 }
4271 }
4272 }
4273 }
4274
4275 weaks = NULL;
4276 ever = extversym != NULL ? extversym + extsymoff : NULL;
4277 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4278 isym < isymend;
4279 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4280 {
4281 int bind;
4282 bfd_vma value;
4283 asection *sec, *new_sec;
4284 flagword flags;
4285 const char *name;
4286 struct elf_link_hash_entry *h;
4287 struct elf_link_hash_entry *hi;
4288 bfd_boolean definition;
4289 bfd_boolean size_change_ok;
4290 bfd_boolean type_change_ok;
4291 bfd_boolean new_weak;
4292 bfd_boolean old_weak;
4293 bfd_boolean override;
4294 bfd_boolean common;
4295 bfd_boolean discarded;
4296 unsigned int old_alignment;
4297 bfd *old_bfd;
4298 bfd_boolean matched;
4299
4300 override = FALSE;
4301
4302 flags = BSF_NO_FLAGS;
4303 sec = NULL;
4304 value = isym->st_value;
4305 common = bed->common_definition (isym);
4306 if (common && info->inhibit_common_definition)
4307 {
4308 /* Treat common symbol as undefined for --no-define-common. */
4309 isym->st_shndx = SHN_UNDEF;
4310 common = FALSE;
4311 }
4312 discarded = FALSE;
4313
4314 bind = ELF_ST_BIND (isym->st_info);
4315 switch (bind)
4316 {
4317 case STB_LOCAL:
4318 /* This should be impossible, since ELF requires that all
4319 global symbols follow all local symbols, and that sh_info
4320 point to the first global symbol. Unfortunately, Irix 5
4321 screws this up. */
4322 continue;
4323
4324 case STB_GLOBAL:
4325 if (isym->st_shndx != SHN_UNDEF && !common)
4326 flags = BSF_GLOBAL;
4327 break;
4328
4329 case STB_WEAK:
4330 flags = BSF_WEAK;
4331 break;
4332
4333 case STB_GNU_UNIQUE:
4334 flags = BSF_GNU_UNIQUE;
4335 break;
4336
4337 default:
4338 /* Leave it up to the processor backend. */
4339 break;
4340 }
4341
4342 if (isym->st_shndx == SHN_UNDEF)
4343 sec = bfd_und_section_ptr;
4344 else if (isym->st_shndx == SHN_ABS)
4345 sec = bfd_abs_section_ptr;
4346 else if (isym->st_shndx == SHN_COMMON)
4347 {
4348 sec = bfd_com_section_ptr;
4349 /* What ELF calls the size we call the value. What ELF
4350 calls the value we call the alignment. */
4351 value = isym->st_size;
4352 }
4353 else
4354 {
4355 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4356 if (sec == NULL)
4357 sec = bfd_abs_section_ptr;
4358 else if (discarded_section (sec))
4359 {
4360 /* Symbols from discarded section are undefined. We keep
4361 its visibility. */
4362 sec = bfd_und_section_ptr;
4363 discarded = TRUE;
4364 isym->st_shndx = SHN_UNDEF;
4365 }
4366 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4367 value -= sec->vma;
4368 }
4369
4370 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4371 isym->st_name);
4372 if (name == NULL)
4373 goto error_free_vers;
4374
4375 if (isym->st_shndx == SHN_COMMON
4376 && (abfd->flags & BFD_PLUGIN) != 0)
4377 {
4378 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4379
4380 if (xc == NULL)
4381 {
4382 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4383 | SEC_EXCLUDE);
4384 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4385 if (xc == NULL)
4386 goto error_free_vers;
4387 }
4388 sec = xc;
4389 }
4390 else if (isym->st_shndx == SHN_COMMON
4391 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4392 && !bfd_link_relocatable (info))
4393 {
4394 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4395
4396 if (tcomm == NULL)
4397 {
4398 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4399 | SEC_LINKER_CREATED);
4400 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4401 if (tcomm == NULL)
4402 goto error_free_vers;
4403 }
4404 sec = tcomm;
4405 }
4406 else if (bed->elf_add_symbol_hook)
4407 {
4408 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4409 &sec, &value))
4410 goto error_free_vers;
4411
4412 /* The hook function sets the name to NULL if this symbol
4413 should be skipped for some reason. */
4414 if (name == NULL)
4415 continue;
4416 }
4417
4418 /* Sanity check that all possibilities were handled. */
4419 if (sec == NULL)
4420 {
4421 bfd_set_error (bfd_error_bad_value);
4422 goto error_free_vers;
4423 }
4424
4425 /* Silently discard TLS symbols from --just-syms. There's
4426 no way to combine a static TLS block with a new TLS block
4427 for this executable. */
4428 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4429 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4430 continue;
4431
4432 if (bfd_is_und_section (sec)
4433 || bfd_is_com_section (sec))
4434 definition = FALSE;
4435 else
4436 definition = TRUE;
4437
4438 size_change_ok = FALSE;
4439 type_change_ok = bed->type_change_ok;
4440 old_weak = FALSE;
4441 matched = FALSE;
4442 old_alignment = 0;
4443 old_bfd = NULL;
4444 new_sec = sec;
4445
4446 if (is_elf_hash_table (htab))
4447 {
4448 Elf_Internal_Versym iver;
4449 unsigned int vernum = 0;
4450 bfd_boolean skip;
4451
4452 if (ever == NULL)
4453 {
4454 if (info->default_imported_symver)
4455 /* Use the default symbol version created earlier. */
4456 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4457 else
4458 iver.vs_vers = 0;
4459 }
4460 else
4461 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4462
4463 vernum = iver.vs_vers & VERSYM_VERSION;
4464
4465 /* If this is a hidden symbol, or if it is not version
4466 1, we append the version name to the symbol name.
4467 However, we do not modify a non-hidden absolute symbol
4468 if it is not a function, because it might be the version
4469 symbol itself. FIXME: What if it isn't? */
4470 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4471 || (vernum > 1
4472 && (!bfd_is_abs_section (sec)
4473 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4474 {
4475 const char *verstr;
4476 size_t namelen, verlen, newlen;
4477 char *newname, *p;
4478
4479 if (isym->st_shndx != SHN_UNDEF)
4480 {
4481 if (vernum > elf_tdata (abfd)->cverdefs)
4482 verstr = NULL;
4483 else if (vernum > 1)
4484 verstr =
4485 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4486 else
4487 verstr = "";
4488
4489 if (verstr == NULL)
4490 {
4491 _bfd_error_handler
4492 /* xgettext:c-format */
4493 (_("%B: %s: invalid version %u (max %d)"),
4494 abfd, name, vernum,
4495 elf_tdata (abfd)->cverdefs);
4496 bfd_set_error (bfd_error_bad_value);
4497 goto error_free_vers;
4498 }
4499 }
4500 else
4501 {
4502 /* We cannot simply test for the number of
4503 entries in the VERNEED section since the
4504 numbers for the needed versions do not start
4505 at 0. */
4506 Elf_Internal_Verneed *t;
4507
4508 verstr = NULL;
4509 for (t = elf_tdata (abfd)->verref;
4510 t != NULL;
4511 t = t->vn_nextref)
4512 {
4513 Elf_Internal_Vernaux *a;
4514
4515 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4516 {
4517 if (a->vna_other == vernum)
4518 {
4519 verstr = a->vna_nodename;
4520 break;
4521 }
4522 }
4523 if (a != NULL)
4524 break;
4525 }
4526 if (verstr == NULL)
4527 {
4528 _bfd_error_handler
4529 /* xgettext:c-format */
4530 (_("%B: %s: invalid needed version %d"),
4531 abfd, name, vernum);
4532 bfd_set_error (bfd_error_bad_value);
4533 goto error_free_vers;
4534 }
4535 }
4536
4537 namelen = strlen (name);
4538 verlen = strlen (verstr);
4539 newlen = namelen + verlen + 2;
4540 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4541 && isym->st_shndx != SHN_UNDEF)
4542 ++newlen;
4543
4544 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4545 if (newname == NULL)
4546 goto error_free_vers;
4547 memcpy (newname, name, namelen);
4548 p = newname + namelen;
4549 *p++ = ELF_VER_CHR;
4550 /* If this is a defined non-hidden version symbol,
4551 we add another @ to the name. This indicates the
4552 default version of the symbol. */
4553 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4554 && isym->st_shndx != SHN_UNDEF)
4555 *p++ = ELF_VER_CHR;
4556 memcpy (p, verstr, verlen + 1);
4557
4558 name = newname;
4559 }
4560
4561 /* If this symbol has default visibility and the user has
4562 requested we not re-export it, then mark it as hidden. */
4563 if (!bfd_is_und_section (sec)
4564 && !dynamic
4565 && abfd->no_export
4566 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4567 isym->st_other = (STV_HIDDEN
4568 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4569
4570 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4571 sym_hash, &old_bfd, &old_weak,
4572 &old_alignment, &skip, &override,
4573 &type_change_ok, &size_change_ok,
4574 &matched))
4575 goto error_free_vers;
4576
4577 if (skip)
4578 continue;
4579
4580 /* Override a definition only if the new symbol matches the
4581 existing one. */
4582 if (override && matched)
4583 definition = FALSE;
4584
4585 h = *sym_hash;
4586 while (h->root.type == bfd_link_hash_indirect
4587 || h->root.type == bfd_link_hash_warning)
4588 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4589
4590 if (elf_tdata (abfd)->verdef != NULL
4591 && vernum > 1
4592 && definition)
4593 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4594 }
4595
4596 if (! (_bfd_generic_link_add_one_symbol
4597 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4598 (struct bfd_link_hash_entry **) sym_hash)))
4599 goto error_free_vers;
4600
4601 if ((flags & BSF_GNU_UNIQUE)
4602 && (abfd->flags & DYNAMIC) == 0
4603 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4604 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
4605
4606 h = *sym_hash;
4607 /* We need to make sure that indirect symbol dynamic flags are
4608 updated. */
4609 hi = h;
4610 while (h->root.type == bfd_link_hash_indirect
4611 || h->root.type == bfd_link_hash_warning)
4612 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4613
4614 /* Setting the index to -3 tells elf_link_output_extsym that
4615 this symbol is defined in a discarded section. */
4616 if (discarded)
4617 h->indx = -3;
4618
4619 *sym_hash = h;
4620
4621 new_weak = (flags & BSF_WEAK) != 0;
4622 if (dynamic
4623 && definition
4624 && new_weak
4625 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4626 && is_elf_hash_table (htab)
4627 && h->u.alias == NULL)
4628 {
4629 /* Keep a list of all weak defined non function symbols from
4630 a dynamic object, using the alias field. Later in this
4631 function we will set the alias field to the correct
4632 value. We only put non-function symbols from dynamic
4633 objects on this list, because that happens to be the only
4634 time we need to know the normal symbol corresponding to a
4635 weak symbol, and the information is time consuming to
4636 figure out. If the alias field is not already NULL,
4637 then this symbol was already defined by some previous
4638 dynamic object, and we will be using that previous
4639 definition anyhow. */
4640
4641 h->u.alias = weaks;
4642 weaks = h;
4643 }
4644
4645 /* Set the alignment of a common symbol. */
4646 if ((common || bfd_is_com_section (sec))
4647 && h->root.type == bfd_link_hash_common)
4648 {
4649 unsigned int align;
4650
4651 if (common)
4652 align = bfd_log2 (isym->st_value);
4653 else
4654 {
4655 /* The new symbol is a common symbol in a shared object.
4656 We need to get the alignment from the section. */
4657 align = new_sec->alignment_power;
4658 }
4659 if (align > old_alignment)
4660 h->root.u.c.p->alignment_power = align;
4661 else
4662 h->root.u.c.p->alignment_power = old_alignment;
4663 }
4664
4665 if (is_elf_hash_table (htab))
4666 {
4667 /* Set a flag in the hash table entry indicating the type of
4668 reference or definition we just found. A dynamic symbol
4669 is one which is referenced or defined by both a regular
4670 object and a shared object. */
4671 bfd_boolean dynsym = FALSE;
4672
4673 /* Plugin symbols aren't normal. Don't set def_regular or
4674 ref_regular for them, or make them dynamic. */
4675 if ((abfd->flags & BFD_PLUGIN) != 0)
4676 ;
4677 else if (! dynamic)
4678 {
4679 if (! definition)
4680 {
4681 h->ref_regular = 1;
4682 if (bind != STB_WEAK)
4683 h->ref_regular_nonweak = 1;
4684 }
4685 else
4686 {
4687 h->def_regular = 1;
4688 if (h->def_dynamic)
4689 {
4690 h->def_dynamic = 0;
4691 h->ref_dynamic = 1;
4692 }
4693 }
4694
4695 /* If the indirect symbol has been forced local, don't
4696 make the real symbol dynamic. */
4697 if ((h == hi || !hi->forced_local)
4698 && (bfd_link_dll (info)
4699 || h->def_dynamic
4700 || h->ref_dynamic))
4701 dynsym = TRUE;
4702 }
4703 else
4704 {
4705 if (! definition)
4706 {
4707 h->ref_dynamic = 1;
4708 hi->ref_dynamic = 1;
4709 }
4710 else
4711 {
4712 h->def_dynamic = 1;
4713 hi->def_dynamic = 1;
4714 }
4715
4716 /* If the indirect symbol has been forced local, don't
4717 make the real symbol dynamic. */
4718 if ((h == hi || !hi->forced_local)
4719 && (h->def_regular
4720 || h->ref_regular
4721 || (h->is_weakalias
4722 && weakdef (h)->dynindx != -1)))
4723 dynsym = TRUE;
4724 }
4725
4726 /* Check to see if we need to add an indirect symbol for
4727 the default name. */
4728 if (definition
4729 || (!override && h->root.type == bfd_link_hash_common))
4730 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4731 sec, value, &old_bfd, &dynsym))
4732 goto error_free_vers;
4733
4734 /* Check the alignment when a common symbol is involved. This
4735 can change when a common symbol is overridden by a normal
4736 definition or a common symbol is ignored due to the old
4737 normal definition. We need to make sure the maximum
4738 alignment is maintained. */
4739 if ((old_alignment || common)
4740 && h->root.type != bfd_link_hash_common)
4741 {
4742 unsigned int common_align;
4743 unsigned int normal_align;
4744 unsigned int symbol_align;
4745 bfd *normal_bfd;
4746 bfd *common_bfd;
4747
4748 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4749 || h->root.type == bfd_link_hash_defweak);
4750
4751 symbol_align = ffs (h->root.u.def.value) - 1;
4752 if (h->root.u.def.section->owner != NULL
4753 && (h->root.u.def.section->owner->flags
4754 & (DYNAMIC | BFD_PLUGIN)) == 0)
4755 {
4756 normal_align = h->root.u.def.section->alignment_power;
4757 if (normal_align > symbol_align)
4758 normal_align = symbol_align;
4759 }
4760 else
4761 normal_align = symbol_align;
4762
4763 if (old_alignment)
4764 {
4765 common_align = old_alignment;
4766 common_bfd = old_bfd;
4767 normal_bfd = abfd;
4768 }
4769 else
4770 {
4771 common_align = bfd_log2 (isym->st_value);
4772 common_bfd = abfd;
4773 normal_bfd = old_bfd;
4774 }
4775
4776 if (normal_align < common_align)
4777 {
4778 /* PR binutils/2735 */
4779 if (normal_bfd == NULL)
4780 _bfd_error_handler
4781 /* xgettext:c-format */
4782 (_("Warning: alignment %u of common symbol `%s' in %B is"
4783 " greater than the alignment (%u) of its section %A"),
4784 1 << common_align, name, common_bfd,
4785 1 << normal_align, h->root.u.def.section);
4786 else
4787 _bfd_error_handler
4788 /* xgettext:c-format */
4789 (_("Warning: alignment %u of symbol `%s' in %B"
4790 " is smaller than %u in %B"),
4791 1 << normal_align, name, normal_bfd,
4792 1 << common_align, common_bfd);
4793 }
4794 }
4795
4796 /* Remember the symbol size if it isn't undefined. */
4797 if (isym->st_size != 0
4798 && isym->st_shndx != SHN_UNDEF
4799 && (definition || h->size == 0))
4800 {
4801 if (h->size != 0
4802 && h->size != isym->st_size
4803 && ! size_change_ok)
4804 _bfd_error_handler
4805 /* xgettext:c-format */
4806 (_("Warning: size of symbol `%s' changed"
4807 " from %Lu in %B to %Lu in %B"),
4808 name, h->size, old_bfd, isym->st_size, abfd);
4809
4810 h->size = isym->st_size;
4811 }
4812
4813 /* If this is a common symbol, then we always want H->SIZE
4814 to be the size of the common symbol. The code just above
4815 won't fix the size if a common symbol becomes larger. We
4816 don't warn about a size change here, because that is
4817 covered by --warn-common. Allow changes between different
4818 function types. */
4819 if (h->root.type == bfd_link_hash_common)
4820 h->size = h->root.u.c.size;
4821
4822 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4823 && ((definition && !new_weak)
4824 || (old_weak && h->root.type == bfd_link_hash_common)
4825 || h->type == STT_NOTYPE))
4826 {
4827 unsigned int type = ELF_ST_TYPE (isym->st_info);
4828
4829 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4830 symbol. */
4831 if (type == STT_GNU_IFUNC
4832 && (abfd->flags & DYNAMIC) != 0)
4833 type = STT_FUNC;
4834
4835 if (h->type != type)
4836 {
4837 if (h->type != STT_NOTYPE && ! type_change_ok)
4838 /* xgettext:c-format */
4839 _bfd_error_handler
4840 (_("Warning: type of symbol `%s' changed"
4841 " from %d to %d in %B"),
4842 name, h->type, type, abfd);
4843
4844 h->type = type;
4845 }
4846 }
4847
4848 /* Merge st_other field. */
4849 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4850
4851 /* We don't want to make debug symbol dynamic. */
4852 if (definition
4853 && (sec->flags & SEC_DEBUGGING)
4854 && !bfd_link_relocatable (info))
4855 dynsym = FALSE;
4856
4857 /* Nor should we make plugin symbols dynamic. */
4858 if ((abfd->flags & BFD_PLUGIN) != 0)
4859 dynsym = FALSE;
4860
4861 if (definition)
4862 {
4863 h->target_internal = isym->st_target_internal;
4864 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4865 }
4866
4867 if (definition && !dynamic)
4868 {
4869 char *p = strchr (name, ELF_VER_CHR);
4870 if (p != NULL && p[1] != ELF_VER_CHR)
4871 {
4872 /* Queue non-default versions so that .symver x, x@FOO
4873 aliases can be checked. */
4874 if (!nondeflt_vers)
4875 {
4876 amt = ((isymend - isym + 1)
4877 * sizeof (struct elf_link_hash_entry *));
4878 nondeflt_vers
4879 = (struct elf_link_hash_entry **) bfd_malloc (amt);
4880 if (!nondeflt_vers)
4881 goto error_free_vers;
4882 }
4883 nondeflt_vers[nondeflt_vers_cnt++] = h;
4884 }
4885 }
4886
4887 if (dynsym && h->dynindx == -1)
4888 {
4889 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4890 goto error_free_vers;
4891 if (h->is_weakalias
4892 && weakdef (h)->dynindx == -1)
4893 {
4894 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
4895 goto error_free_vers;
4896 }
4897 }
4898 else if (h->dynindx != -1)
4899 /* If the symbol already has a dynamic index, but
4900 visibility says it should not be visible, turn it into
4901 a local symbol. */
4902 switch (ELF_ST_VISIBILITY (h->other))
4903 {
4904 case STV_INTERNAL:
4905 case STV_HIDDEN:
4906 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4907 dynsym = FALSE;
4908 break;
4909 }
4910
4911 /* Don't add DT_NEEDED for references from the dummy bfd nor
4912 for unmatched symbol. */
4913 if (!add_needed
4914 && matched
4915 && definition
4916 && ((dynsym
4917 && h->ref_regular_nonweak
4918 && (old_bfd == NULL
4919 || (old_bfd->flags & BFD_PLUGIN) == 0))
4920 || (h->ref_dynamic_nonweak
4921 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4922 && !on_needed_list (elf_dt_name (abfd),
4923 htab->needed, NULL))))
4924 {
4925 int ret;
4926 const char *soname = elf_dt_name (abfd);
4927
4928 info->callbacks->minfo ("%!", soname, old_bfd,
4929 h->root.root.string);
4930
4931 /* A symbol from a library loaded via DT_NEEDED of some
4932 other library is referenced by a regular object.
4933 Add a DT_NEEDED entry for it. Issue an error if
4934 --no-add-needed is used and the reference was not
4935 a weak one. */
4936 if (old_bfd != NULL
4937 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4938 {
4939 _bfd_error_handler
4940 /* xgettext:c-format */
4941 (_("%B: undefined reference to symbol '%s'"),
4942 old_bfd, name);
4943 bfd_set_error (bfd_error_missing_dso);
4944 goto error_free_vers;
4945 }
4946
4947 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4948 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
4949
4950 add_needed = TRUE;
4951 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4952 if (ret < 0)
4953 goto error_free_vers;
4954
4955 BFD_ASSERT (ret == 0);
4956 }
4957 }
4958 }
4959
4960 if (info->lto_plugin_active
4961 && !bfd_link_relocatable (info)
4962 && (abfd->flags & BFD_PLUGIN) == 0
4963 && !just_syms
4964 && extsymcount)
4965 {
4966 int r_sym_shift;
4967
4968 if (bed->s->arch_size == 32)
4969 r_sym_shift = 8;
4970 else
4971 r_sym_shift = 32;
4972
4973 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
4974 referenced in regular objects so that linker plugin will get
4975 the correct symbol resolution. */
4976
4977 sym_hash = elf_sym_hashes (abfd);
4978 for (s = abfd->sections; s != NULL; s = s->next)
4979 {
4980 Elf_Internal_Rela *internal_relocs;
4981 Elf_Internal_Rela *rel, *relend;
4982
4983 /* Don't check relocations in excluded sections. */
4984 if ((s->flags & SEC_RELOC) == 0
4985 || s->reloc_count == 0
4986 || (s->flags & SEC_EXCLUDE) != 0
4987 || ((info->strip == strip_all
4988 || info->strip == strip_debugger)
4989 && (s->flags & SEC_DEBUGGING) != 0))
4990 continue;
4991
4992 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
4993 NULL,
4994 info->keep_memory);
4995 if (internal_relocs == NULL)
4996 goto error_free_vers;
4997
4998 rel = internal_relocs;
4999 relend = rel + s->reloc_count;
5000 for ( ; rel < relend; rel++)
5001 {
5002 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5003 struct elf_link_hash_entry *h;
5004
5005 /* Skip local symbols. */
5006 if (r_symndx < extsymoff)
5007 continue;
5008
5009 h = sym_hash[r_symndx - extsymoff];
5010 if (h != NULL)
5011 h->root.non_ir_ref_regular = 1;
5012 }
5013
5014 if (elf_section_data (s)->relocs != internal_relocs)
5015 free (internal_relocs);
5016 }
5017 }
5018
5019 if (extversym != NULL)
5020 {
5021 free (extversym);
5022 extversym = NULL;
5023 }
5024
5025 if (isymbuf != NULL)
5026 {
5027 free (isymbuf);
5028 isymbuf = NULL;
5029 }
5030
5031 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5032 {
5033 unsigned int i;
5034
5035 /* Restore the symbol table. */
5036 old_ent = (char *) old_tab + tabsize;
5037 memset (elf_sym_hashes (abfd), 0,
5038 extsymcount * sizeof (struct elf_link_hash_entry *));
5039 htab->root.table.table = old_table;
5040 htab->root.table.size = old_size;
5041 htab->root.table.count = old_count;
5042 memcpy (htab->root.table.table, old_tab, tabsize);
5043 htab->root.undefs = old_undefs;
5044 htab->root.undefs_tail = old_undefs_tail;
5045 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5046 free (old_strtab);
5047 old_strtab = NULL;
5048 for (i = 0; i < htab->root.table.size; i++)
5049 {
5050 struct bfd_hash_entry *p;
5051 struct elf_link_hash_entry *h;
5052 bfd_size_type size;
5053 unsigned int alignment_power;
5054 unsigned int non_ir_ref_dynamic;
5055
5056 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5057 {
5058 h = (struct elf_link_hash_entry *) p;
5059 if (h->root.type == bfd_link_hash_warning)
5060 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5061
5062 /* Preserve the maximum alignment and size for common
5063 symbols even if this dynamic lib isn't on DT_NEEDED
5064 since it can still be loaded at run time by another
5065 dynamic lib. */
5066 if (h->root.type == bfd_link_hash_common)
5067 {
5068 size = h->root.u.c.size;
5069 alignment_power = h->root.u.c.p->alignment_power;
5070 }
5071 else
5072 {
5073 size = 0;
5074 alignment_power = 0;
5075 }
5076 /* Preserve non_ir_ref_dynamic so that this symbol
5077 will be exported when the dynamic lib becomes needed
5078 in the second pass. */
5079 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5080 memcpy (p, old_ent, htab->root.table.entsize);
5081 old_ent = (char *) old_ent + htab->root.table.entsize;
5082 h = (struct elf_link_hash_entry *) p;
5083 if (h->root.type == bfd_link_hash_warning)
5084 {
5085 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5086 old_ent = (char *) old_ent + htab->root.table.entsize;
5087 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5088 }
5089 if (h->root.type == bfd_link_hash_common)
5090 {
5091 if (size > h->root.u.c.size)
5092 h->root.u.c.size = size;
5093 if (alignment_power > h->root.u.c.p->alignment_power)
5094 h->root.u.c.p->alignment_power = alignment_power;
5095 }
5096 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5097 }
5098 }
5099
5100 /* Make a special call to the linker "notice" function to
5101 tell it that symbols added for crefs may need to be removed. */
5102 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5103 goto error_free_vers;
5104
5105 free (old_tab);
5106 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5107 alloc_mark);
5108 if (nondeflt_vers != NULL)
5109 free (nondeflt_vers);
5110 return TRUE;
5111 }
5112
5113 if (old_tab != NULL)
5114 {
5115 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5116 goto error_free_vers;
5117 free (old_tab);
5118 old_tab = NULL;
5119 }
5120
5121 /* Now that all the symbols from this input file are created, if
5122 not performing a relocatable link, handle .symver foo, foo@BAR
5123 such that any relocs against foo become foo@BAR. */
5124 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5125 {
5126 size_t cnt, symidx;
5127
5128 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5129 {
5130 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5131 char *shortname, *p;
5132
5133 p = strchr (h->root.root.string, ELF_VER_CHR);
5134 if (p == NULL
5135 || (h->root.type != bfd_link_hash_defined
5136 && h->root.type != bfd_link_hash_defweak))
5137 continue;
5138
5139 amt = p - h->root.root.string;
5140 shortname = (char *) bfd_malloc (amt + 1);
5141 if (!shortname)
5142 goto error_free_vers;
5143 memcpy (shortname, h->root.root.string, amt);
5144 shortname[amt] = '\0';
5145
5146 hi = (struct elf_link_hash_entry *)
5147 bfd_link_hash_lookup (&htab->root, shortname,
5148 FALSE, FALSE, FALSE);
5149 if (hi != NULL
5150 && hi->root.type == h->root.type
5151 && hi->root.u.def.value == h->root.u.def.value
5152 && hi->root.u.def.section == h->root.u.def.section)
5153 {
5154 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5155 hi->root.type = bfd_link_hash_indirect;
5156 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5157 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5158 sym_hash = elf_sym_hashes (abfd);
5159 if (sym_hash)
5160 for (symidx = 0; symidx < extsymcount; ++symidx)
5161 if (sym_hash[symidx] == hi)
5162 {
5163 sym_hash[symidx] = h;
5164 break;
5165 }
5166 }
5167 free (shortname);
5168 }
5169 free (nondeflt_vers);
5170 nondeflt_vers = NULL;
5171 }
5172
5173 /* Now set the alias field correctly for all the weak defined
5174 symbols we found. The only way to do this is to search all the
5175 symbols. Since we only need the information for non functions in
5176 dynamic objects, that's the only time we actually put anything on
5177 the list WEAKS. We need this information so that if a regular
5178 object refers to a symbol defined weakly in a dynamic object, the
5179 real symbol in the dynamic object is also put in the dynamic
5180 symbols; we also must arrange for both symbols to point to the
5181 same memory location. We could handle the general case of symbol
5182 aliasing, but a general symbol alias can only be generated in
5183 assembler code, handling it correctly would be very time
5184 consuming, and other ELF linkers don't handle general aliasing
5185 either. */
5186 if (weaks != NULL)
5187 {
5188 struct elf_link_hash_entry **hpp;
5189 struct elf_link_hash_entry **hppend;
5190 struct elf_link_hash_entry **sorted_sym_hash;
5191 struct elf_link_hash_entry *h;
5192 size_t sym_count;
5193
5194 /* Since we have to search the whole symbol list for each weak
5195 defined symbol, search time for N weak defined symbols will be
5196 O(N^2). Binary search will cut it down to O(NlogN). */
5197 amt = extsymcount;
5198 amt *= sizeof (struct elf_link_hash_entry *);
5199 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
5200 if (sorted_sym_hash == NULL)
5201 goto error_return;
5202 sym_hash = sorted_sym_hash;
5203 hpp = elf_sym_hashes (abfd);
5204 hppend = hpp + extsymcount;
5205 sym_count = 0;
5206 for (; hpp < hppend; hpp++)
5207 {
5208 h = *hpp;
5209 if (h != NULL
5210 && h->root.type == bfd_link_hash_defined
5211 && !bed->is_function_type (h->type))
5212 {
5213 *sym_hash = h;
5214 sym_hash++;
5215 sym_count++;
5216 }
5217 }
5218
5219 qsort (sorted_sym_hash, sym_count,
5220 sizeof (struct elf_link_hash_entry *),
5221 elf_sort_symbol);
5222
5223 while (weaks != NULL)
5224 {
5225 struct elf_link_hash_entry *hlook;
5226 asection *slook;
5227 bfd_vma vlook;
5228 size_t i, j, idx = 0;
5229
5230 hlook = weaks;
5231 weaks = hlook->u.alias;
5232 hlook->u.alias = NULL;
5233
5234 if (hlook->root.type != bfd_link_hash_defined
5235 && hlook->root.type != bfd_link_hash_defweak)
5236 continue;
5237
5238 slook = hlook->root.u.def.section;
5239 vlook = hlook->root.u.def.value;
5240
5241 i = 0;
5242 j = sym_count;
5243 while (i != j)
5244 {
5245 bfd_signed_vma vdiff;
5246 idx = (i + j) / 2;
5247 h = sorted_sym_hash[idx];
5248 vdiff = vlook - h->root.u.def.value;
5249 if (vdiff < 0)
5250 j = idx;
5251 else if (vdiff > 0)
5252 i = idx + 1;
5253 else
5254 {
5255 int sdiff = slook->id - h->root.u.def.section->id;
5256 if (sdiff < 0)
5257 j = idx;
5258 else if (sdiff > 0)
5259 i = idx + 1;
5260 else
5261 break;
5262 }
5263 }
5264
5265 /* We didn't find a value/section match. */
5266 if (i == j)
5267 continue;
5268
5269 /* With multiple aliases, or when the weak symbol is already
5270 strongly defined, we have multiple matching symbols and
5271 the binary search above may land on any of them. Step
5272 one past the matching symbol(s). */
5273 while (++idx != j)
5274 {
5275 h = sorted_sym_hash[idx];
5276 if (h->root.u.def.section != slook
5277 || h->root.u.def.value != vlook)
5278 break;
5279 }
5280
5281 /* Now look back over the aliases. Since we sorted by size
5282 as well as value and section, we'll choose the one with
5283 the largest size. */
5284 while (idx-- != i)
5285 {
5286 h = sorted_sym_hash[idx];
5287
5288 /* Stop if value or section doesn't match. */
5289 if (h->root.u.def.section != slook
5290 || h->root.u.def.value != vlook)
5291 break;
5292 else if (h != hlook)
5293 {
5294 struct elf_link_hash_entry *t;
5295
5296 hlook->u.alias = h;
5297 hlook->is_weakalias = 1;
5298 t = h;
5299 if (t->u.alias != NULL)
5300 while (t->u.alias != h)
5301 t = t->u.alias;
5302 t->u.alias = hlook;
5303
5304 /* If the weak definition is in the list of dynamic
5305 symbols, make sure the real definition is put
5306 there as well. */
5307 if (hlook->dynindx != -1 && h->dynindx == -1)
5308 {
5309 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5310 {
5311 err_free_sym_hash:
5312 free (sorted_sym_hash);
5313 goto error_return;
5314 }
5315 }
5316
5317 /* If the real definition is in the list of dynamic
5318 symbols, make sure the weak definition is put
5319 there as well. If we don't do this, then the
5320 dynamic loader might not merge the entries for the
5321 real definition and the weak definition. */
5322 if (h->dynindx != -1 && hlook->dynindx == -1)
5323 {
5324 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5325 goto err_free_sym_hash;
5326 }
5327 break;
5328 }
5329 }
5330 }
5331
5332 free (sorted_sym_hash);
5333 }
5334
5335 if (bed->check_directives
5336 && !(*bed->check_directives) (abfd, info))
5337 return FALSE;
5338
5339 /* If this is a non-traditional link, try to optimize the handling
5340 of the .stab/.stabstr sections. */
5341 if (! dynamic
5342 && ! info->traditional_format
5343 && is_elf_hash_table (htab)
5344 && (info->strip != strip_all && info->strip != strip_debugger))
5345 {
5346 asection *stabstr;
5347
5348 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5349 if (stabstr != NULL)
5350 {
5351 bfd_size_type string_offset = 0;
5352 asection *stab;
5353
5354 for (stab = abfd->sections; stab; stab = stab->next)
5355 if (CONST_STRNEQ (stab->name, ".stab")
5356 && (!stab->name[5] ||
5357 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5358 && (stab->flags & SEC_MERGE) == 0
5359 && !bfd_is_abs_section (stab->output_section))
5360 {
5361 struct bfd_elf_section_data *secdata;
5362
5363 secdata = elf_section_data (stab);
5364 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5365 stabstr, &secdata->sec_info,
5366 &string_offset))
5367 goto error_return;
5368 if (secdata->sec_info)
5369 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5370 }
5371 }
5372 }
5373
5374 if (is_elf_hash_table (htab) && add_needed)
5375 {
5376 /* Add this bfd to the loaded list. */
5377 struct elf_link_loaded_list *n;
5378
5379 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5380 if (n == NULL)
5381 goto error_return;
5382 n->abfd = abfd;
5383 n->next = htab->loaded;
5384 htab->loaded = n;
5385 }
5386
5387 return TRUE;
5388
5389 error_free_vers:
5390 if (old_tab != NULL)
5391 free (old_tab);
5392 if (old_strtab != NULL)
5393 free (old_strtab);
5394 if (nondeflt_vers != NULL)
5395 free (nondeflt_vers);
5396 if (extversym != NULL)
5397 free (extversym);
5398 error_free_sym:
5399 if (isymbuf != NULL)
5400 free (isymbuf);
5401 error_return:
5402 return FALSE;
5403 }
5404
5405 /* Return the linker hash table entry of a symbol that might be
5406 satisfied by an archive symbol. Return -1 on error. */
5407
5408 struct elf_link_hash_entry *
5409 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5410 struct bfd_link_info *info,
5411 const char *name)
5412 {
5413 struct elf_link_hash_entry *h;
5414 char *p, *copy;
5415 size_t len, first;
5416
5417 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5418 if (h != NULL)
5419 return h;
5420
5421 /* If this is a default version (the name contains @@), look up the
5422 symbol again with only one `@' as well as without the version.
5423 The effect is that references to the symbol with and without the
5424 version will be matched by the default symbol in the archive. */
5425
5426 p = strchr (name, ELF_VER_CHR);
5427 if (p == NULL || p[1] != ELF_VER_CHR)
5428 return h;
5429
5430 /* First check with only one `@'. */
5431 len = strlen (name);
5432 copy = (char *) bfd_alloc (abfd, len);
5433 if (copy == NULL)
5434 return (struct elf_link_hash_entry *) -1;
5435
5436 first = p - name + 1;
5437 memcpy (copy, name, first);
5438 memcpy (copy + first, name + first + 1, len - first);
5439
5440 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5441 if (h == NULL)
5442 {
5443 /* We also need to check references to the symbol without the
5444 version. */
5445 copy[first - 1] = '\0';
5446 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5447 FALSE, FALSE, TRUE);
5448 }
5449
5450 bfd_release (abfd, copy);
5451 return h;
5452 }
5453
5454 /* Add symbols from an ELF archive file to the linker hash table. We
5455 don't use _bfd_generic_link_add_archive_symbols because we need to
5456 handle versioned symbols.
5457
5458 Fortunately, ELF archive handling is simpler than that done by
5459 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5460 oddities. In ELF, if we find a symbol in the archive map, and the
5461 symbol is currently undefined, we know that we must pull in that
5462 object file.
5463
5464 Unfortunately, we do have to make multiple passes over the symbol
5465 table until nothing further is resolved. */
5466
5467 static bfd_boolean
5468 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5469 {
5470 symindex c;
5471 unsigned char *included = NULL;
5472 carsym *symdefs;
5473 bfd_boolean loop;
5474 bfd_size_type amt;
5475 const struct elf_backend_data *bed;
5476 struct elf_link_hash_entry * (*archive_symbol_lookup)
5477 (bfd *, struct bfd_link_info *, const char *);
5478
5479 if (! bfd_has_map (abfd))
5480 {
5481 /* An empty archive is a special case. */
5482 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5483 return TRUE;
5484 bfd_set_error (bfd_error_no_armap);
5485 return FALSE;
5486 }
5487
5488 /* Keep track of all symbols we know to be already defined, and all
5489 files we know to be already included. This is to speed up the
5490 second and subsequent passes. */
5491 c = bfd_ardata (abfd)->symdef_count;
5492 if (c == 0)
5493 return TRUE;
5494 amt = c;
5495 amt *= sizeof (*included);
5496 included = (unsigned char *) bfd_zmalloc (amt);
5497 if (included == NULL)
5498 return FALSE;
5499
5500 symdefs = bfd_ardata (abfd)->symdefs;
5501 bed = get_elf_backend_data (abfd);
5502 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5503
5504 do
5505 {
5506 file_ptr last;
5507 symindex i;
5508 carsym *symdef;
5509 carsym *symdefend;
5510
5511 loop = FALSE;
5512 last = -1;
5513
5514 symdef = symdefs;
5515 symdefend = symdef + c;
5516 for (i = 0; symdef < symdefend; symdef++, i++)
5517 {
5518 struct elf_link_hash_entry *h;
5519 bfd *element;
5520 struct bfd_link_hash_entry *undefs_tail;
5521 symindex mark;
5522
5523 if (included[i])
5524 continue;
5525 if (symdef->file_offset == last)
5526 {
5527 included[i] = TRUE;
5528 continue;
5529 }
5530
5531 h = archive_symbol_lookup (abfd, info, symdef->name);
5532 if (h == (struct elf_link_hash_entry *) -1)
5533 goto error_return;
5534
5535 if (h == NULL)
5536 continue;
5537
5538 if (h->root.type == bfd_link_hash_common)
5539 {
5540 /* We currently have a common symbol. The archive map contains
5541 a reference to this symbol, so we may want to include it. We
5542 only want to include it however, if this archive element
5543 contains a definition of the symbol, not just another common
5544 declaration of it.
5545
5546 Unfortunately some archivers (including GNU ar) will put
5547 declarations of common symbols into their archive maps, as
5548 well as real definitions, so we cannot just go by the archive
5549 map alone. Instead we must read in the element's symbol
5550 table and check that to see what kind of symbol definition
5551 this is. */
5552 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5553 continue;
5554 }
5555 else if (h->root.type != bfd_link_hash_undefined)
5556 {
5557 if (h->root.type != bfd_link_hash_undefweak)
5558 /* Symbol must be defined. Don't check it again. */
5559 included[i] = TRUE;
5560 continue;
5561 }
5562
5563 /* We need to include this archive member. */
5564 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5565 if (element == NULL)
5566 goto error_return;
5567
5568 if (! bfd_check_format (element, bfd_object))
5569 goto error_return;
5570
5571 undefs_tail = info->hash->undefs_tail;
5572
5573 if (!(*info->callbacks
5574 ->add_archive_element) (info, element, symdef->name, &element))
5575 continue;
5576 if (!bfd_link_add_symbols (element, info))
5577 goto error_return;
5578
5579 /* If there are any new undefined symbols, we need to make
5580 another pass through the archive in order to see whether
5581 they can be defined. FIXME: This isn't perfect, because
5582 common symbols wind up on undefs_tail and because an
5583 undefined symbol which is defined later on in this pass
5584 does not require another pass. This isn't a bug, but it
5585 does make the code less efficient than it could be. */
5586 if (undefs_tail != info->hash->undefs_tail)
5587 loop = TRUE;
5588
5589 /* Look backward to mark all symbols from this object file
5590 which we have already seen in this pass. */
5591 mark = i;
5592 do
5593 {
5594 included[mark] = TRUE;
5595 if (mark == 0)
5596 break;
5597 --mark;
5598 }
5599 while (symdefs[mark].file_offset == symdef->file_offset);
5600
5601 /* We mark subsequent symbols from this object file as we go
5602 on through the loop. */
5603 last = symdef->file_offset;
5604 }
5605 }
5606 while (loop);
5607
5608 free (included);
5609
5610 return TRUE;
5611
5612 error_return:
5613 if (included != NULL)
5614 free (included);
5615 return FALSE;
5616 }
5617
5618 /* Given an ELF BFD, add symbols to the global hash table as
5619 appropriate. */
5620
5621 bfd_boolean
5622 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5623 {
5624 switch (bfd_get_format (abfd))
5625 {
5626 case bfd_object:
5627 return elf_link_add_object_symbols (abfd, info);
5628 case bfd_archive:
5629 return elf_link_add_archive_symbols (abfd, info);
5630 default:
5631 bfd_set_error (bfd_error_wrong_format);
5632 return FALSE;
5633 }
5634 }
5635 \f
5636 struct hash_codes_info
5637 {
5638 unsigned long *hashcodes;
5639 bfd_boolean error;
5640 };
5641
5642 /* This function will be called though elf_link_hash_traverse to store
5643 all hash value of the exported symbols in an array. */
5644
5645 static bfd_boolean
5646 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5647 {
5648 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5649 const char *name;
5650 unsigned long ha;
5651 char *alc = NULL;
5652
5653 /* Ignore indirect symbols. These are added by the versioning code. */
5654 if (h->dynindx == -1)
5655 return TRUE;
5656
5657 name = h->root.root.string;
5658 if (h->versioned >= versioned)
5659 {
5660 char *p = strchr (name, ELF_VER_CHR);
5661 if (p != NULL)
5662 {
5663 alc = (char *) bfd_malloc (p - name + 1);
5664 if (alc == NULL)
5665 {
5666 inf->error = TRUE;
5667 return FALSE;
5668 }
5669 memcpy (alc, name, p - name);
5670 alc[p - name] = '\0';
5671 name = alc;
5672 }
5673 }
5674
5675 /* Compute the hash value. */
5676 ha = bfd_elf_hash (name);
5677
5678 /* Store the found hash value in the array given as the argument. */
5679 *(inf->hashcodes)++ = ha;
5680
5681 /* And store it in the struct so that we can put it in the hash table
5682 later. */
5683 h->u.elf_hash_value = ha;
5684
5685 if (alc != NULL)
5686 free (alc);
5687
5688 return TRUE;
5689 }
5690
5691 struct collect_gnu_hash_codes
5692 {
5693 bfd *output_bfd;
5694 const struct elf_backend_data *bed;
5695 unsigned long int nsyms;
5696 unsigned long int maskbits;
5697 unsigned long int *hashcodes;
5698 unsigned long int *hashval;
5699 unsigned long int *indx;
5700 unsigned long int *counts;
5701 bfd_vma *bitmask;
5702 bfd_byte *contents;
5703 long int min_dynindx;
5704 unsigned long int bucketcount;
5705 unsigned long int symindx;
5706 long int local_indx;
5707 long int shift1, shift2;
5708 unsigned long int mask;
5709 bfd_boolean error;
5710 };
5711
5712 /* This function will be called though elf_link_hash_traverse to store
5713 all hash value of the exported symbols in an array. */
5714
5715 static bfd_boolean
5716 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5717 {
5718 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5719 const char *name;
5720 unsigned long ha;
5721 char *alc = NULL;
5722
5723 /* Ignore indirect symbols. These are added by the versioning code. */
5724 if (h->dynindx == -1)
5725 return TRUE;
5726
5727 /* Ignore also local symbols and undefined symbols. */
5728 if (! (*s->bed->elf_hash_symbol) (h))
5729 return TRUE;
5730
5731 name = h->root.root.string;
5732 if (h->versioned >= versioned)
5733 {
5734 char *p = strchr (name, ELF_VER_CHR);
5735 if (p != NULL)
5736 {
5737 alc = (char *) bfd_malloc (p - name + 1);
5738 if (alc == NULL)
5739 {
5740 s->error = TRUE;
5741 return FALSE;
5742 }
5743 memcpy (alc, name, p - name);
5744 alc[p - name] = '\0';
5745 name = alc;
5746 }
5747 }
5748
5749 /* Compute the hash value. */
5750 ha = bfd_elf_gnu_hash (name);
5751
5752 /* Store the found hash value in the array for compute_bucket_count,
5753 and also for .dynsym reordering purposes. */
5754 s->hashcodes[s->nsyms] = ha;
5755 s->hashval[h->dynindx] = ha;
5756 ++s->nsyms;
5757 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5758 s->min_dynindx = h->dynindx;
5759
5760 if (alc != NULL)
5761 free (alc);
5762
5763 return TRUE;
5764 }
5765
5766 /* This function will be called though elf_link_hash_traverse to do
5767 final dynaminc symbol renumbering. */
5768
5769 static bfd_boolean
5770 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5771 {
5772 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5773 unsigned long int bucket;
5774 unsigned long int val;
5775
5776 /* Ignore indirect symbols. */
5777 if (h->dynindx == -1)
5778 return TRUE;
5779
5780 /* Ignore also local symbols and undefined symbols. */
5781 if (! (*s->bed->elf_hash_symbol) (h))
5782 {
5783 if (h->dynindx >= s->min_dynindx)
5784 h->dynindx = s->local_indx++;
5785 return TRUE;
5786 }
5787
5788 bucket = s->hashval[h->dynindx] % s->bucketcount;
5789 val = (s->hashval[h->dynindx] >> s->shift1)
5790 & ((s->maskbits >> s->shift1) - 1);
5791 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5792 s->bitmask[val]
5793 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5794 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5795 if (s->counts[bucket] == 1)
5796 /* Last element terminates the chain. */
5797 val |= 1;
5798 bfd_put_32 (s->output_bfd, val,
5799 s->contents + (s->indx[bucket] - s->symindx) * 4);
5800 --s->counts[bucket];
5801 h->dynindx = s->indx[bucket]++;
5802 return TRUE;
5803 }
5804
5805 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5806
5807 bfd_boolean
5808 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5809 {
5810 return !(h->forced_local
5811 || h->root.type == bfd_link_hash_undefined
5812 || h->root.type == bfd_link_hash_undefweak
5813 || ((h->root.type == bfd_link_hash_defined
5814 || h->root.type == bfd_link_hash_defweak)
5815 && h->root.u.def.section->output_section == NULL));
5816 }
5817
5818 /* Array used to determine the number of hash table buckets to use
5819 based on the number of symbols there are. If there are fewer than
5820 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5821 fewer than 37 we use 17 buckets, and so forth. We never use more
5822 than 32771 buckets. */
5823
5824 static const size_t elf_buckets[] =
5825 {
5826 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5827 16411, 32771, 0
5828 };
5829
5830 /* Compute bucket count for hashing table. We do not use a static set
5831 of possible tables sizes anymore. Instead we determine for all
5832 possible reasonable sizes of the table the outcome (i.e., the
5833 number of collisions etc) and choose the best solution. The
5834 weighting functions are not too simple to allow the table to grow
5835 without bounds. Instead one of the weighting factors is the size.
5836 Therefore the result is always a good payoff between few collisions
5837 (= short chain lengths) and table size. */
5838 static size_t
5839 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5840 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5841 unsigned long int nsyms,
5842 int gnu_hash)
5843 {
5844 size_t best_size = 0;
5845 unsigned long int i;
5846
5847 /* We have a problem here. The following code to optimize the table
5848 size requires an integer type with more the 32 bits. If
5849 BFD_HOST_U_64_BIT is set we know about such a type. */
5850 #ifdef BFD_HOST_U_64_BIT
5851 if (info->optimize)
5852 {
5853 size_t minsize;
5854 size_t maxsize;
5855 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5856 bfd *dynobj = elf_hash_table (info)->dynobj;
5857 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5858 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5859 unsigned long int *counts;
5860 bfd_size_type amt;
5861 unsigned int no_improvement_count = 0;
5862
5863 /* Possible optimization parameters: if we have NSYMS symbols we say
5864 that the hashing table must at least have NSYMS/4 and at most
5865 2*NSYMS buckets. */
5866 minsize = nsyms / 4;
5867 if (minsize == 0)
5868 minsize = 1;
5869 best_size = maxsize = nsyms * 2;
5870 if (gnu_hash)
5871 {
5872 if (minsize < 2)
5873 minsize = 2;
5874 if ((best_size & 31) == 0)
5875 ++best_size;
5876 }
5877
5878 /* Create array where we count the collisions in. We must use bfd_malloc
5879 since the size could be large. */
5880 amt = maxsize;
5881 amt *= sizeof (unsigned long int);
5882 counts = (unsigned long int *) bfd_malloc (amt);
5883 if (counts == NULL)
5884 return 0;
5885
5886 /* Compute the "optimal" size for the hash table. The criteria is a
5887 minimal chain length. The minor criteria is (of course) the size
5888 of the table. */
5889 for (i = minsize; i < maxsize; ++i)
5890 {
5891 /* Walk through the array of hashcodes and count the collisions. */
5892 BFD_HOST_U_64_BIT max;
5893 unsigned long int j;
5894 unsigned long int fact;
5895
5896 if (gnu_hash && (i & 31) == 0)
5897 continue;
5898
5899 memset (counts, '\0', i * sizeof (unsigned long int));
5900
5901 /* Determine how often each hash bucket is used. */
5902 for (j = 0; j < nsyms; ++j)
5903 ++counts[hashcodes[j] % i];
5904
5905 /* For the weight function we need some information about the
5906 pagesize on the target. This is information need not be 100%
5907 accurate. Since this information is not available (so far) we
5908 define it here to a reasonable default value. If it is crucial
5909 to have a better value some day simply define this value. */
5910 # ifndef BFD_TARGET_PAGESIZE
5911 # define BFD_TARGET_PAGESIZE (4096)
5912 # endif
5913
5914 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5915 and the chains. */
5916 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5917
5918 # if 1
5919 /* Variant 1: optimize for short chains. We add the squares
5920 of all the chain lengths (which favors many small chain
5921 over a few long chains). */
5922 for (j = 0; j < i; ++j)
5923 max += counts[j] * counts[j];
5924
5925 /* This adds penalties for the overall size of the table. */
5926 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5927 max *= fact * fact;
5928 # else
5929 /* Variant 2: Optimize a lot more for small table. Here we
5930 also add squares of the size but we also add penalties for
5931 empty slots (the +1 term). */
5932 for (j = 0; j < i; ++j)
5933 max += (1 + counts[j]) * (1 + counts[j]);
5934
5935 /* The overall size of the table is considered, but not as
5936 strong as in variant 1, where it is squared. */
5937 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5938 max *= fact;
5939 # endif
5940
5941 /* Compare with current best results. */
5942 if (max < best_chlen)
5943 {
5944 best_chlen = max;
5945 best_size = i;
5946 no_improvement_count = 0;
5947 }
5948 /* PR 11843: Avoid futile long searches for the best bucket size
5949 when there are a large number of symbols. */
5950 else if (++no_improvement_count == 100)
5951 break;
5952 }
5953
5954 free (counts);
5955 }
5956 else
5957 #endif /* defined (BFD_HOST_U_64_BIT) */
5958 {
5959 /* This is the fallback solution if no 64bit type is available or if we
5960 are not supposed to spend much time on optimizations. We select the
5961 bucket count using a fixed set of numbers. */
5962 for (i = 0; elf_buckets[i] != 0; i++)
5963 {
5964 best_size = elf_buckets[i];
5965 if (nsyms < elf_buckets[i + 1])
5966 break;
5967 }
5968 if (gnu_hash && best_size < 2)
5969 best_size = 2;
5970 }
5971
5972 return best_size;
5973 }
5974
5975 /* Size any SHT_GROUP section for ld -r. */
5976
5977 bfd_boolean
5978 _bfd_elf_size_group_sections (struct bfd_link_info *info)
5979 {
5980 bfd *ibfd;
5981 asection *s;
5982
5983 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5984 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5985 && (s = ibfd->sections) != NULL
5986 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
5987 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5988 return FALSE;
5989 return TRUE;
5990 }
5991
5992 /* Set a default stack segment size. The value in INFO wins. If it
5993 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5994 undefined it is initialized. */
5995
5996 bfd_boolean
5997 bfd_elf_stack_segment_size (bfd *output_bfd,
5998 struct bfd_link_info *info,
5999 const char *legacy_symbol,
6000 bfd_vma default_size)
6001 {
6002 struct elf_link_hash_entry *h = NULL;
6003
6004 /* Look for legacy symbol. */
6005 if (legacy_symbol)
6006 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6007 FALSE, FALSE, FALSE);
6008 if (h && (h->root.type == bfd_link_hash_defined
6009 || h->root.type == bfd_link_hash_defweak)
6010 && h->def_regular
6011 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6012 {
6013 /* The symbol has no type if specified on the command line. */
6014 h->type = STT_OBJECT;
6015 if (info->stacksize)
6016 /* xgettext:c-format */
6017 _bfd_error_handler (_("%B: stack size specified and %s set"),
6018 output_bfd, legacy_symbol);
6019 else if (h->root.u.def.section != bfd_abs_section_ptr)
6020 /* xgettext:c-format */
6021 _bfd_error_handler (_("%B: %s not absolute"),
6022 output_bfd, legacy_symbol);
6023 else
6024 info->stacksize = h->root.u.def.value;
6025 }
6026
6027 if (!info->stacksize)
6028 /* If the user didn't set a size, or explicitly inhibit the
6029 size, set it now. */
6030 info->stacksize = default_size;
6031
6032 /* Provide the legacy symbol, if it is referenced. */
6033 if (h && (h->root.type == bfd_link_hash_undefined
6034 || h->root.type == bfd_link_hash_undefweak))
6035 {
6036 struct bfd_link_hash_entry *bh = NULL;
6037
6038 if (!(_bfd_generic_link_add_one_symbol
6039 (info, output_bfd, legacy_symbol,
6040 BSF_GLOBAL, bfd_abs_section_ptr,
6041 info->stacksize >= 0 ? info->stacksize : 0,
6042 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6043 return FALSE;
6044
6045 h = (struct elf_link_hash_entry *) bh;
6046 h->def_regular = 1;
6047 h->type = STT_OBJECT;
6048 }
6049
6050 return TRUE;
6051 }
6052
6053 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6054
6055 struct elf_gc_sweep_symbol_info
6056 {
6057 struct bfd_link_info *info;
6058 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6059 bfd_boolean);
6060 };
6061
6062 static bfd_boolean
6063 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6064 {
6065 if (!h->mark
6066 && (((h->root.type == bfd_link_hash_defined
6067 || h->root.type == bfd_link_hash_defweak)
6068 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6069 && h->root.u.def.section->gc_mark))
6070 || h->root.type == bfd_link_hash_undefined
6071 || h->root.type == bfd_link_hash_undefweak))
6072 {
6073 struct elf_gc_sweep_symbol_info *inf;
6074
6075 inf = (struct elf_gc_sweep_symbol_info *) data;
6076 (*inf->hide_symbol) (inf->info, h, TRUE);
6077 h->def_regular = 0;
6078 h->ref_regular = 0;
6079 h->ref_regular_nonweak = 0;
6080 }
6081
6082 return TRUE;
6083 }
6084
6085 /* Set up the sizes and contents of the ELF dynamic sections. This is
6086 called by the ELF linker emulation before_allocation routine. We
6087 must set the sizes of the sections before the linker sets the
6088 addresses of the various sections. */
6089
6090 bfd_boolean
6091 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6092 const char *soname,
6093 const char *rpath,
6094 const char *filter_shlib,
6095 const char *audit,
6096 const char *depaudit,
6097 const char * const *auxiliary_filters,
6098 struct bfd_link_info *info,
6099 asection **sinterpptr)
6100 {
6101 bfd *dynobj;
6102 const struct elf_backend_data *bed;
6103
6104 *sinterpptr = NULL;
6105
6106 if (!is_elf_hash_table (info->hash))
6107 return TRUE;
6108
6109 dynobj = elf_hash_table (info)->dynobj;
6110
6111 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6112 {
6113 struct bfd_elf_version_tree *verdefs;
6114 struct elf_info_failed asvinfo;
6115 struct bfd_elf_version_tree *t;
6116 struct bfd_elf_version_expr *d;
6117 asection *s;
6118 size_t soname_indx;
6119
6120 /* If we are supposed to export all symbols into the dynamic symbol
6121 table (this is not the normal case), then do so. */
6122 if (info->export_dynamic
6123 || (bfd_link_executable (info) && info->dynamic))
6124 {
6125 struct elf_info_failed eif;
6126
6127 eif.info = info;
6128 eif.failed = FALSE;
6129 elf_link_hash_traverse (elf_hash_table (info),
6130 _bfd_elf_export_symbol,
6131 &eif);
6132 if (eif.failed)
6133 return FALSE;
6134 }
6135
6136 if (soname != NULL)
6137 {
6138 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6139 soname, TRUE);
6140 if (soname_indx == (size_t) -1
6141 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6142 return FALSE;
6143 }
6144 else
6145 soname_indx = (size_t) -1;
6146
6147 /* Make all global versions with definition. */
6148 for (t = info->version_info; t != NULL; t = t->next)
6149 for (d = t->globals.list; d != NULL; d = d->next)
6150 if (!d->symver && d->literal)
6151 {
6152 const char *verstr, *name;
6153 size_t namelen, verlen, newlen;
6154 char *newname, *p, leading_char;
6155 struct elf_link_hash_entry *newh;
6156
6157 leading_char = bfd_get_symbol_leading_char (output_bfd);
6158 name = d->pattern;
6159 namelen = strlen (name) + (leading_char != '\0');
6160 verstr = t->name;
6161 verlen = strlen (verstr);
6162 newlen = namelen + verlen + 3;
6163
6164 newname = (char *) bfd_malloc (newlen);
6165 if (newname == NULL)
6166 return FALSE;
6167 newname[0] = leading_char;
6168 memcpy (newname + (leading_char != '\0'), name, namelen);
6169
6170 /* Check the hidden versioned definition. */
6171 p = newname + namelen;
6172 *p++ = ELF_VER_CHR;
6173 memcpy (p, verstr, verlen + 1);
6174 newh = elf_link_hash_lookup (elf_hash_table (info),
6175 newname, FALSE, FALSE,
6176 FALSE);
6177 if (newh == NULL
6178 || (newh->root.type != bfd_link_hash_defined
6179 && newh->root.type != bfd_link_hash_defweak))
6180 {
6181 /* Check the default versioned definition. */
6182 *p++ = ELF_VER_CHR;
6183 memcpy (p, verstr, verlen + 1);
6184 newh = elf_link_hash_lookup (elf_hash_table (info),
6185 newname, FALSE, FALSE,
6186 FALSE);
6187 }
6188 free (newname);
6189
6190 /* Mark this version if there is a definition and it is
6191 not defined in a shared object. */
6192 if (newh != NULL
6193 && !newh->def_dynamic
6194 && (newh->root.type == bfd_link_hash_defined
6195 || newh->root.type == bfd_link_hash_defweak))
6196 d->symver = 1;
6197 }
6198
6199 /* Attach all the symbols to their version information. */
6200 asvinfo.info = info;
6201 asvinfo.failed = FALSE;
6202
6203 elf_link_hash_traverse (elf_hash_table (info),
6204 _bfd_elf_link_assign_sym_version,
6205 &asvinfo);
6206 if (asvinfo.failed)
6207 return FALSE;
6208
6209 if (!info->allow_undefined_version)
6210 {
6211 /* Check if all global versions have a definition. */
6212 bfd_boolean all_defined = TRUE;
6213 for (t = info->version_info; t != NULL; t = t->next)
6214 for (d = t->globals.list; d != NULL; d = d->next)
6215 if (d->literal && !d->symver && !d->script)
6216 {
6217 _bfd_error_handler
6218 (_("%s: undefined version: %s"),
6219 d->pattern, t->name);
6220 all_defined = FALSE;
6221 }
6222
6223 if (!all_defined)
6224 {
6225 bfd_set_error (bfd_error_bad_value);
6226 return FALSE;
6227 }
6228 }
6229
6230 /* Set up the version definition section. */
6231 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6232 BFD_ASSERT (s != NULL);
6233
6234 /* We may have created additional version definitions if we are
6235 just linking a regular application. */
6236 verdefs = info->version_info;
6237
6238 /* Skip anonymous version tag. */
6239 if (verdefs != NULL && verdefs->vernum == 0)
6240 verdefs = verdefs->next;
6241
6242 if (verdefs == NULL && !info->create_default_symver)
6243 s->flags |= SEC_EXCLUDE;
6244 else
6245 {
6246 unsigned int cdefs;
6247 bfd_size_type size;
6248 bfd_byte *p;
6249 Elf_Internal_Verdef def;
6250 Elf_Internal_Verdaux defaux;
6251 struct bfd_link_hash_entry *bh;
6252 struct elf_link_hash_entry *h;
6253 const char *name;
6254
6255 cdefs = 0;
6256 size = 0;
6257
6258 /* Make space for the base version. */
6259 size += sizeof (Elf_External_Verdef);
6260 size += sizeof (Elf_External_Verdaux);
6261 ++cdefs;
6262
6263 /* Make space for the default version. */
6264 if (info->create_default_symver)
6265 {
6266 size += sizeof (Elf_External_Verdef);
6267 ++cdefs;
6268 }
6269
6270 for (t = verdefs; t != NULL; t = t->next)
6271 {
6272 struct bfd_elf_version_deps *n;
6273
6274 /* Don't emit base version twice. */
6275 if (t->vernum == 0)
6276 continue;
6277
6278 size += sizeof (Elf_External_Verdef);
6279 size += sizeof (Elf_External_Verdaux);
6280 ++cdefs;
6281
6282 for (n = t->deps; n != NULL; n = n->next)
6283 size += sizeof (Elf_External_Verdaux);
6284 }
6285
6286 s->size = size;
6287 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6288 if (s->contents == NULL && s->size != 0)
6289 return FALSE;
6290
6291 /* Fill in the version definition section. */
6292
6293 p = s->contents;
6294
6295 def.vd_version = VER_DEF_CURRENT;
6296 def.vd_flags = VER_FLG_BASE;
6297 def.vd_ndx = 1;
6298 def.vd_cnt = 1;
6299 if (info->create_default_symver)
6300 {
6301 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6302 def.vd_next = sizeof (Elf_External_Verdef);
6303 }
6304 else
6305 {
6306 def.vd_aux = sizeof (Elf_External_Verdef);
6307 def.vd_next = (sizeof (Elf_External_Verdef)
6308 + sizeof (Elf_External_Verdaux));
6309 }
6310
6311 if (soname_indx != (size_t) -1)
6312 {
6313 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6314 soname_indx);
6315 def.vd_hash = bfd_elf_hash (soname);
6316 defaux.vda_name = soname_indx;
6317 name = soname;
6318 }
6319 else
6320 {
6321 size_t indx;
6322
6323 name = lbasename (output_bfd->filename);
6324 def.vd_hash = bfd_elf_hash (name);
6325 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6326 name, FALSE);
6327 if (indx == (size_t) -1)
6328 return FALSE;
6329 defaux.vda_name = indx;
6330 }
6331 defaux.vda_next = 0;
6332
6333 _bfd_elf_swap_verdef_out (output_bfd, &def,
6334 (Elf_External_Verdef *) p);
6335 p += sizeof (Elf_External_Verdef);
6336 if (info->create_default_symver)
6337 {
6338 /* Add a symbol representing this version. */
6339 bh = NULL;
6340 if (! (_bfd_generic_link_add_one_symbol
6341 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6342 0, NULL, FALSE,
6343 get_elf_backend_data (dynobj)->collect, &bh)))
6344 return FALSE;
6345 h = (struct elf_link_hash_entry *) bh;
6346 h->non_elf = 0;
6347 h->def_regular = 1;
6348 h->type = STT_OBJECT;
6349 h->verinfo.vertree = NULL;
6350
6351 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6352 return FALSE;
6353
6354 /* Create a duplicate of the base version with the same
6355 aux block, but different flags. */
6356 def.vd_flags = 0;
6357 def.vd_ndx = 2;
6358 def.vd_aux = sizeof (Elf_External_Verdef);
6359 if (verdefs)
6360 def.vd_next = (sizeof (Elf_External_Verdef)
6361 + sizeof (Elf_External_Verdaux));
6362 else
6363 def.vd_next = 0;
6364 _bfd_elf_swap_verdef_out (output_bfd, &def,
6365 (Elf_External_Verdef *) p);
6366 p += sizeof (Elf_External_Verdef);
6367 }
6368 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6369 (Elf_External_Verdaux *) p);
6370 p += sizeof (Elf_External_Verdaux);
6371
6372 for (t = verdefs; t != NULL; t = t->next)
6373 {
6374 unsigned int cdeps;
6375 struct bfd_elf_version_deps *n;
6376
6377 /* Don't emit the base version twice. */
6378 if (t->vernum == 0)
6379 continue;
6380
6381 cdeps = 0;
6382 for (n = t->deps; n != NULL; n = n->next)
6383 ++cdeps;
6384
6385 /* Add a symbol representing this version. */
6386 bh = NULL;
6387 if (! (_bfd_generic_link_add_one_symbol
6388 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6389 0, NULL, FALSE,
6390 get_elf_backend_data (dynobj)->collect, &bh)))
6391 return FALSE;
6392 h = (struct elf_link_hash_entry *) bh;
6393 h->non_elf = 0;
6394 h->def_regular = 1;
6395 h->type = STT_OBJECT;
6396 h->verinfo.vertree = t;
6397
6398 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6399 return FALSE;
6400
6401 def.vd_version = VER_DEF_CURRENT;
6402 def.vd_flags = 0;
6403 if (t->globals.list == NULL
6404 && t->locals.list == NULL
6405 && ! t->used)
6406 def.vd_flags |= VER_FLG_WEAK;
6407 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6408 def.vd_cnt = cdeps + 1;
6409 def.vd_hash = bfd_elf_hash (t->name);
6410 def.vd_aux = sizeof (Elf_External_Verdef);
6411 def.vd_next = 0;
6412
6413 /* If a basever node is next, it *must* be the last node in
6414 the chain, otherwise Verdef construction breaks. */
6415 if (t->next != NULL && t->next->vernum == 0)
6416 BFD_ASSERT (t->next->next == NULL);
6417
6418 if (t->next != NULL && t->next->vernum != 0)
6419 def.vd_next = (sizeof (Elf_External_Verdef)
6420 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6421
6422 _bfd_elf_swap_verdef_out (output_bfd, &def,
6423 (Elf_External_Verdef *) p);
6424 p += sizeof (Elf_External_Verdef);
6425
6426 defaux.vda_name = h->dynstr_index;
6427 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6428 h->dynstr_index);
6429 defaux.vda_next = 0;
6430 if (t->deps != NULL)
6431 defaux.vda_next = sizeof (Elf_External_Verdaux);
6432 t->name_indx = defaux.vda_name;
6433
6434 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6435 (Elf_External_Verdaux *) p);
6436 p += sizeof (Elf_External_Verdaux);
6437
6438 for (n = t->deps; n != NULL; n = n->next)
6439 {
6440 if (n->version_needed == NULL)
6441 {
6442 /* This can happen if there was an error in the
6443 version script. */
6444 defaux.vda_name = 0;
6445 }
6446 else
6447 {
6448 defaux.vda_name = n->version_needed->name_indx;
6449 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6450 defaux.vda_name);
6451 }
6452 if (n->next == NULL)
6453 defaux.vda_next = 0;
6454 else
6455 defaux.vda_next = sizeof (Elf_External_Verdaux);
6456
6457 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6458 (Elf_External_Verdaux *) p);
6459 p += sizeof (Elf_External_Verdaux);
6460 }
6461 }
6462
6463 elf_tdata (output_bfd)->cverdefs = cdefs;
6464 }
6465 }
6466
6467 bed = get_elf_backend_data (output_bfd);
6468
6469 if (info->gc_sections && bed->can_gc_sections)
6470 {
6471 struct elf_gc_sweep_symbol_info sweep_info;
6472
6473 /* Remove the symbols that were in the swept sections from the
6474 dynamic symbol table. */
6475 sweep_info.info = info;
6476 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6477 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6478 &sweep_info);
6479 }
6480
6481 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6482 {
6483 asection *s;
6484 struct elf_find_verdep_info sinfo;
6485
6486 /* Work out the size of the version reference section. */
6487
6488 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6489 BFD_ASSERT (s != NULL);
6490
6491 sinfo.info = info;
6492 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6493 if (sinfo.vers == 0)
6494 sinfo.vers = 1;
6495 sinfo.failed = FALSE;
6496
6497 elf_link_hash_traverse (elf_hash_table (info),
6498 _bfd_elf_link_find_version_dependencies,
6499 &sinfo);
6500 if (sinfo.failed)
6501 return FALSE;
6502
6503 if (elf_tdata (output_bfd)->verref == NULL)
6504 s->flags |= SEC_EXCLUDE;
6505 else
6506 {
6507 Elf_Internal_Verneed *vn;
6508 unsigned int size;
6509 unsigned int crefs;
6510 bfd_byte *p;
6511
6512 /* Build the version dependency section. */
6513 size = 0;
6514 crefs = 0;
6515 for (vn = elf_tdata (output_bfd)->verref;
6516 vn != NULL;
6517 vn = vn->vn_nextref)
6518 {
6519 Elf_Internal_Vernaux *a;
6520
6521 size += sizeof (Elf_External_Verneed);
6522 ++crefs;
6523 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6524 size += sizeof (Elf_External_Vernaux);
6525 }
6526
6527 s->size = size;
6528 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6529 if (s->contents == NULL)
6530 return FALSE;
6531
6532 p = s->contents;
6533 for (vn = elf_tdata (output_bfd)->verref;
6534 vn != NULL;
6535 vn = vn->vn_nextref)
6536 {
6537 unsigned int caux;
6538 Elf_Internal_Vernaux *a;
6539 size_t indx;
6540
6541 caux = 0;
6542 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6543 ++caux;
6544
6545 vn->vn_version = VER_NEED_CURRENT;
6546 vn->vn_cnt = caux;
6547 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6548 elf_dt_name (vn->vn_bfd) != NULL
6549 ? elf_dt_name (vn->vn_bfd)
6550 : lbasename (vn->vn_bfd->filename),
6551 FALSE);
6552 if (indx == (size_t) -1)
6553 return FALSE;
6554 vn->vn_file = indx;
6555 vn->vn_aux = sizeof (Elf_External_Verneed);
6556 if (vn->vn_nextref == NULL)
6557 vn->vn_next = 0;
6558 else
6559 vn->vn_next = (sizeof (Elf_External_Verneed)
6560 + caux * sizeof (Elf_External_Vernaux));
6561
6562 _bfd_elf_swap_verneed_out (output_bfd, vn,
6563 (Elf_External_Verneed *) p);
6564 p += sizeof (Elf_External_Verneed);
6565
6566 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6567 {
6568 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6569 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6570 a->vna_nodename, FALSE);
6571 if (indx == (size_t) -1)
6572 return FALSE;
6573 a->vna_name = indx;
6574 if (a->vna_nextptr == NULL)
6575 a->vna_next = 0;
6576 else
6577 a->vna_next = sizeof (Elf_External_Vernaux);
6578
6579 _bfd_elf_swap_vernaux_out (output_bfd, a,
6580 (Elf_External_Vernaux *) p);
6581 p += sizeof (Elf_External_Vernaux);
6582 }
6583 }
6584
6585 elf_tdata (output_bfd)->cverrefs = crefs;
6586 }
6587 }
6588
6589 /* Any syms created from now on start with -1 in
6590 got.refcount/offset and plt.refcount/offset. */
6591 elf_hash_table (info)->init_got_refcount
6592 = elf_hash_table (info)->init_got_offset;
6593 elf_hash_table (info)->init_plt_refcount
6594 = elf_hash_table (info)->init_plt_offset;
6595
6596 if (bfd_link_relocatable (info)
6597 && !_bfd_elf_size_group_sections (info))
6598 return FALSE;
6599
6600 /* The backend may have to create some sections regardless of whether
6601 we're dynamic or not. */
6602 if (bed->elf_backend_always_size_sections
6603 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6604 return FALSE;
6605
6606 /* Determine any GNU_STACK segment requirements, after the backend
6607 has had a chance to set a default segment size. */
6608 if (info->execstack)
6609 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6610 else if (info->noexecstack)
6611 elf_stack_flags (output_bfd) = PF_R | PF_W;
6612 else
6613 {
6614 bfd *inputobj;
6615 asection *notesec = NULL;
6616 int exec = 0;
6617
6618 for (inputobj = info->input_bfds;
6619 inputobj;
6620 inputobj = inputobj->link.next)
6621 {
6622 asection *s;
6623
6624 if (inputobj->flags
6625 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6626 continue;
6627 s = inputobj->sections;
6628 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6629 continue;
6630
6631 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6632 if (s)
6633 {
6634 if (s->flags & SEC_CODE)
6635 exec = PF_X;
6636 notesec = s;
6637 }
6638 else if (bed->default_execstack)
6639 exec = PF_X;
6640 }
6641 if (notesec || info->stacksize > 0)
6642 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6643 if (notesec && exec && bfd_link_relocatable (info)
6644 && notesec->output_section != bfd_abs_section_ptr)
6645 notesec->output_section->flags |= SEC_CODE;
6646 }
6647
6648 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6649 {
6650 struct elf_info_failed eif;
6651 struct elf_link_hash_entry *h;
6652 asection *dynstr;
6653 asection *s;
6654
6655 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6656 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6657
6658 if (info->symbolic)
6659 {
6660 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6661 return FALSE;
6662 info->flags |= DF_SYMBOLIC;
6663 }
6664
6665 if (rpath != NULL)
6666 {
6667 size_t indx;
6668 bfd_vma tag;
6669
6670 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6671 TRUE);
6672 if (indx == (size_t) -1)
6673 return FALSE;
6674
6675 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6676 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6677 return FALSE;
6678 }
6679
6680 if (filter_shlib != NULL)
6681 {
6682 size_t indx;
6683
6684 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6685 filter_shlib, TRUE);
6686 if (indx == (size_t) -1
6687 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6688 return FALSE;
6689 }
6690
6691 if (auxiliary_filters != NULL)
6692 {
6693 const char * const *p;
6694
6695 for (p = auxiliary_filters; *p != NULL; p++)
6696 {
6697 size_t indx;
6698
6699 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6700 *p, TRUE);
6701 if (indx == (size_t) -1
6702 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6703 return FALSE;
6704 }
6705 }
6706
6707 if (audit != NULL)
6708 {
6709 size_t indx;
6710
6711 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6712 TRUE);
6713 if (indx == (size_t) -1
6714 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6715 return FALSE;
6716 }
6717
6718 if (depaudit != NULL)
6719 {
6720 size_t indx;
6721
6722 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6723 TRUE);
6724 if (indx == (size_t) -1
6725 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6726 return FALSE;
6727 }
6728
6729 eif.info = info;
6730 eif.failed = FALSE;
6731
6732 /* Find all symbols which were defined in a dynamic object and make
6733 the backend pick a reasonable value for them. */
6734 elf_link_hash_traverse (elf_hash_table (info),
6735 _bfd_elf_adjust_dynamic_symbol,
6736 &eif);
6737 if (eif.failed)
6738 return FALSE;
6739
6740 /* Add some entries to the .dynamic section. We fill in some of the
6741 values later, in bfd_elf_final_link, but we must add the entries
6742 now so that we know the final size of the .dynamic section. */
6743
6744 /* If there are initialization and/or finalization functions to
6745 call then add the corresponding DT_INIT/DT_FINI entries. */
6746 h = (info->init_function
6747 ? elf_link_hash_lookup (elf_hash_table (info),
6748 info->init_function, FALSE,
6749 FALSE, FALSE)
6750 : NULL);
6751 if (h != NULL
6752 && (h->ref_regular
6753 || h->def_regular))
6754 {
6755 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6756 return FALSE;
6757 }
6758 h = (info->fini_function
6759 ? elf_link_hash_lookup (elf_hash_table (info),
6760 info->fini_function, FALSE,
6761 FALSE, FALSE)
6762 : NULL);
6763 if (h != NULL
6764 && (h->ref_regular
6765 || h->def_regular))
6766 {
6767 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6768 return FALSE;
6769 }
6770
6771 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6772 if (s != NULL && s->linker_has_input)
6773 {
6774 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6775 if (! bfd_link_executable (info))
6776 {
6777 bfd *sub;
6778 asection *o;
6779
6780 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6781 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
6782 && (o = sub->sections) != NULL
6783 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
6784 for (o = sub->sections; o != NULL; o = o->next)
6785 if (elf_section_data (o)->this_hdr.sh_type
6786 == SHT_PREINIT_ARRAY)
6787 {
6788 _bfd_error_handler
6789 (_("%B: .preinit_array section is not allowed in DSO"),
6790 sub);
6791 break;
6792 }
6793
6794 bfd_set_error (bfd_error_nonrepresentable_section);
6795 return FALSE;
6796 }
6797
6798 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6799 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6800 return FALSE;
6801 }
6802 s = bfd_get_section_by_name (output_bfd, ".init_array");
6803 if (s != NULL && s->linker_has_input)
6804 {
6805 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6806 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6807 return FALSE;
6808 }
6809 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6810 if (s != NULL && s->linker_has_input)
6811 {
6812 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6813 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6814 return FALSE;
6815 }
6816
6817 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6818 /* If .dynstr is excluded from the link, we don't want any of
6819 these tags. Strictly, we should be checking each section
6820 individually; This quick check covers for the case where
6821 someone does a /DISCARD/ : { *(*) }. */
6822 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6823 {
6824 bfd_size_type strsize;
6825
6826 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6827 if ((info->emit_hash
6828 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6829 || (info->emit_gnu_hash
6830 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6831 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6832 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6833 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6834 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6835 bed->s->sizeof_sym))
6836 return FALSE;
6837 }
6838 }
6839
6840 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6841 return FALSE;
6842
6843 /* The backend must work out the sizes of all the other dynamic
6844 sections. */
6845 if (dynobj != NULL
6846 && bed->elf_backend_size_dynamic_sections != NULL
6847 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6848 return FALSE;
6849
6850 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6851 {
6852 if (elf_tdata (output_bfd)->cverdefs)
6853 {
6854 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
6855
6856 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6857 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
6858 return FALSE;
6859 }
6860
6861 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6862 {
6863 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6864 return FALSE;
6865 }
6866 else if (info->flags & DF_BIND_NOW)
6867 {
6868 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6869 return FALSE;
6870 }
6871
6872 if (info->flags_1)
6873 {
6874 if (bfd_link_executable (info))
6875 info->flags_1 &= ~ (DF_1_INITFIRST
6876 | DF_1_NODELETE
6877 | DF_1_NOOPEN);
6878 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6879 return FALSE;
6880 }
6881
6882 if (elf_tdata (output_bfd)->cverrefs)
6883 {
6884 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
6885
6886 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6887 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6888 return FALSE;
6889 }
6890
6891 if ((elf_tdata (output_bfd)->cverrefs == 0
6892 && elf_tdata (output_bfd)->cverdefs == 0)
6893 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
6894 {
6895 asection *s;
6896
6897 s = bfd_get_linker_section (dynobj, ".gnu.version");
6898 s->flags |= SEC_EXCLUDE;
6899 }
6900 }
6901 return TRUE;
6902 }
6903
6904 /* Find the first non-excluded output section. We'll use its
6905 section symbol for some emitted relocs. */
6906 void
6907 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6908 {
6909 asection *s;
6910
6911 for (s = output_bfd->sections; s != NULL; s = s->next)
6912 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6913 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
6914 {
6915 elf_hash_table (info)->text_index_section = s;
6916 break;
6917 }
6918 }
6919
6920 /* Find two non-excluded output sections, one for code, one for data.
6921 We'll use their section symbols for some emitted relocs. */
6922 void
6923 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6924 {
6925 asection *s;
6926
6927 /* Data first, since setting text_index_section changes
6928 _bfd_elf_link_omit_section_dynsym. */
6929 for (s = output_bfd->sections; s != NULL; s = s->next)
6930 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6931 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
6932 {
6933 elf_hash_table (info)->data_index_section = s;
6934 break;
6935 }
6936
6937 for (s = output_bfd->sections; s != NULL; s = s->next)
6938 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6939 == (SEC_ALLOC | SEC_READONLY))
6940 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
6941 {
6942 elf_hash_table (info)->text_index_section = s;
6943 break;
6944 }
6945
6946 if (elf_hash_table (info)->text_index_section == NULL)
6947 elf_hash_table (info)->text_index_section
6948 = elf_hash_table (info)->data_index_section;
6949 }
6950
6951 bfd_boolean
6952 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6953 {
6954 const struct elf_backend_data *bed;
6955 unsigned long section_sym_count;
6956 bfd_size_type dynsymcount = 0;
6957
6958 if (!is_elf_hash_table (info->hash))
6959 return TRUE;
6960
6961 bed = get_elf_backend_data (output_bfd);
6962 (*bed->elf_backend_init_index_section) (output_bfd, info);
6963
6964 /* Assign dynsym indices. In a shared library we generate a section
6965 symbol for each output section, which come first. Next come all
6966 of the back-end allocated local dynamic syms, followed by the rest
6967 of the global symbols.
6968
6969 This is usually not needed for static binaries, however backends
6970 can request to always do it, e.g. the MIPS backend uses dynamic
6971 symbol counts to lay out GOT, which will be produced in the
6972 presence of GOT relocations even in static binaries (holding fixed
6973 data in that case, to satisfy those relocations). */
6974
6975 if (elf_hash_table (info)->dynamic_sections_created
6976 || bed->always_renumber_dynsyms)
6977 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6978 &section_sym_count);
6979
6980 if (elf_hash_table (info)->dynamic_sections_created)
6981 {
6982 bfd *dynobj;
6983 asection *s;
6984 unsigned int dtagcount;
6985
6986 dynobj = elf_hash_table (info)->dynobj;
6987
6988 /* Work out the size of the symbol version section. */
6989 s = bfd_get_linker_section (dynobj, ".gnu.version");
6990 BFD_ASSERT (s != NULL);
6991 if ((s->flags & SEC_EXCLUDE) == 0)
6992 {
6993 s->size = dynsymcount * sizeof (Elf_External_Versym);
6994 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6995 if (s->contents == NULL)
6996 return FALSE;
6997
6998 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6999 return FALSE;
7000 }
7001
7002 /* Set the size of the .dynsym and .hash sections. We counted
7003 the number of dynamic symbols in elf_link_add_object_symbols.
7004 We will build the contents of .dynsym and .hash when we build
7005 the final symbol table, because until then we do not know the
7006 correct value to give the symbols. We built the .dynstr
7007 section as we went along in elf_link_add_object_symbols. */
7008 s = elf_hash_table (info)->dynsym;
7009 BFD_ASSERT (s != NULL);
7010 s->size = dynsymcount * bed->s->sizeof_sym;
7011
7012 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7013 if (s->contents == NULL)
7014 return FALSE;
7015
7016 /* The first entry in .dynsym is a dummy symbol. Clear all the
7017 section syms, in case we don't output them all. */
7018 ++section_sym_count;
7019 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7020
7021 elf_hash_table (info)->bucketcount = 0;
7022
7023 /* Compute the size of the hashing table. As a side effect this
7024 computes the hash values for all the names we export. */
7025 if (info->emit_hash)
7026 {
7027 unsigned long int *hashcodes;
7028 struct hash_codes_info hashinf;
7029 bfd_size_type amt;
7030 unsigned long int nsyms;
7031 size_t bucketcount;
7032 size_t hash_entry_size;
7033
7034 /* Compute the hash values for all exported symbols. At the same
7035 time store the values in an array so that we could use them for
7036 optimizations. */
7037 amt = dynsymcount * sizeof (unsigned long int);
7038 hashcodes = (unsigned long int *) bfd_malloc (amt);
7039 if (hashcodes == NULL)
7040 return FALSE;
7041 hashinf.hashcodes = hashcodes;
7042 hashinf.error = FALSE;
7043
7044 /* Put all hash values in HASHCODES. */
7045 elf_link_hash_traverse (elf_hash_table (info),
7046 elf_collect_hash_codes, &hashinf);
7047 if (hashinf.error)
7048 {
7049 free (hashcodes);
7050 return FALSE;
7051 }
7052
7053 nsyms = hashinf.hashcodes - hashcodes;
7054 bucketcount
7055 = compute_bucket_count (info, hashcodes, nsyms, 0);
7056 free (hashcodes);
7057
7058 if (bucketcount == 0 && nsyms > 0)
7059 return FALSE;
7060
7061 elf_hash_table (info)->bucketcount = bucketcount;
7062
7063 s = bfd_get_linker_section (dynobj, ".hash");
7064 BFD_ASSERT (s != NULL);
7065 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7066 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7067 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7068 if (s->contents == NULL)
7069 return FALSE;
7070
7071 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7072 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7073 s->contents + hash_entry_size);
7074 }
7075
7076 if (info->emit_gnu_hash)
7077 {
7078 size_t i, cnt;
7079 unsigned char *contents;
7080 struct collect_gnu_hash_codes cinfo;
7081 bfd_size_type amt;
7082 size_t bucketcount;
7083
7084 memset (&cinfo, 0, sizeof (cinfo));
7085
7086 /* Compute the hash values for all exported symbols. At the same
7087 time store the values in an array so that we could use them for
7088 optimizations. */
7089 amt = dynsymcount * 2 * sizeof (unsigned long int);
7090 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7091 if (cinfo.hashcodes == NULL)
7092 return FALSE;
7093
7094 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7095 cinfo.min_dynindx = -1;
7096 cinfo.output_bfd = output_bfd;
7097 cinfo.bed = bed;
7098
7099 /* Put all hash values in HASHCODES. */
7100 elf_link_hash_traverse (elf_hash_table (info),
7101 elf_collect_gnu_hash_codes, &cinfo);
7102 if (cinfo.error)
7103 {
7104 free (cinfo.hashcodes);
7105 return FALSE;
7106 }
7107
7108 bucketcount
7109 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7110
7111 if (bucketcount == 0)
7112 {
7113 free (cinfo.hashcodes);
7114 return FALSE;
7115 }
7116
7117 s = bfd_get_linker_section (dynobj, ".gnu.hash");
7118 BFD_ASSERT (s != NULL);
7119
7120 if (cinfo.nsyms == 0)
7121 {
7122 /* Empty .gnu.hash section is special. */
7123 BFD_ASSERT (cinfo.min_dynindx == -1);
7124 free (cinfo.hashcodes);
7125 s->size = 5 * 4 + bed->s->arch_size / 8;
7126 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7127 if (contents == NULL)
7128 return FALSE;
7129 s->contents = contents;
7130 /* 1 empty bucket. */
7131 bfd_put_32 (output_bfd, 1, contents);
7132 /* SYMIDX above the special symbol 0. */
7133 bfd_put_32 (output_bfd, 1, contents + 4);
7134 /* Just one word for bitmask. */
7135 bfd_put_32 (output_bfd, 1, contents + 8);
7136 /* Only hash fn bloom filter. */
7137 bfd_put_32 (output_bfd, 0, contents + 12);
7138 /* No hashes are valid - empty bitmask. */
7139 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7140 /* No hashes in the only bucket. */
7141 bfd_put_32 (output_bfd, 0,
7142 contents + 16 + bed->s->arch_size / 8);
7143 }
7144 else
7145 {
7146 unsigned long int maskwords, maskbitslog2, x;
7147 BFD_ASSERT (cinfo.min_dynindx != -1);
7148
7149 x = cinfo.nsyms;
7150 maskbitslog2 = 1;
7151 while ((x >>= 1) != 0)
7152 ++maskbitslog2;
7153 if (maskbitslog2 < 3)
7154 maskbitslog2 = 5;
7155 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7156 maskbitslog2 = maskbitslog2 + 3;
7157 else
7158 maskbitslog2 = maskbitslog2 + 2;
7159 if (bed->s->arch_size == 64)
7160 {
7161 if (maskbitslog2 == 5)
7162 maskbitslog2 = 6;
7163 cinfo.shift1 = 6;
7164 }
7165 else
7166 cinfo.shift1 = 5;
7167 cinfo.mask = (1 << cinfo.shift1) - 1;
7168 cinfo.shift2 = maskbitslog2;
7169 cinfo.maskbits = 1 << maskbitslog2;
7170 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7171 amt = bucketcount * sizeof (unsigned long int) * 2;
7172 amt += maskwords * sizeof (bfd_vma);
7173 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7174 if (cinfo.bitmask == NULL)
7175 {
7176 free (cinfo.hashcodes);
7177 return FALSE;
7178 }
7179
7180 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7181 cinfo.indx = cinfo.counts + bucketcount;
7182 cinfo.symindx = dynsymcount - cinfo.nsyms;
7183 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7184
7185 /* Determine how often each hash bucket is used. */
7186 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7187 for (i = 0; i < cinfo.nsyms; ++i)
7188 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7189
7190 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7191 if (cinfo.counts[i] != 0)
7192 {
7193 cinfo.indx[i] = cnt;
7194 cnt += cinfo.counts[i];
7195 }
7196 BFD_ASSERT (cnt == dynsymcount);
7197 cinfo.bucketcount = bucketcount;
7198 cinfo.local_indx = cinfo.min_dynindx;
7199
7200 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7201 s->size += cinfo.maskbits / 8;
7202 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7203 if (contents == NULL)
7204 {
7205 free (cinfo.bitmask);
7206 free (cinfo.hashcodes);
7207 return FALSE;
7208 }
7209
7210 s->contents = contents;
7211 bfd_put_32 (output_bfd, bucketcount, contents);
7212 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7213 bfd_put_32 (output_bfd, maskwords, contents + 8);
7214 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7215 contents += 16 + cinfo.maskbits / 8;
7216
7217 for (i = 0; i < bucketcount; ++i)
7218 {
7219 if (cinfo.counts[i] == 0)
7220 bfd_put_32 (output_bfd, 0, contents);
7221 else
7222 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7223 contents += 4;
7224 }
7225
7226 cinfo.contents = contents;
7227
7228 /* Renumber dynamic symbols, populate .gnu.hash section. */
7229 elf_link_hash_traverse (elf_hash_table (info),
7230 elf_renumber_gnu_hash_syms, &cinfo);
7231
7232 contents = s->contents + 16;
7233 for (i = 0; i < maskwords; ++i)
7234 {
7235 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7236 contents);
7237 contents += bed->s->arch_size / 8;
7238 }
7239
7240 free (cinfo.bitmask);
7241 free (cinfo.hashcodes);
7242 }
7243 }
7244
7245 s = bfd_get_linker_section (dynobj, ".dynstr");
7246 BFD_ASSERT (s != NULL);
7247
7248 elf_finalize_dynstr (output_bfd, info);
7249
7250 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7251
7252 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7253 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7254 return FALSE;
7255 }
7256
7257 return TRUE;
7258 }
7259 \f
7260 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7261
7262 static void
7263 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7264 asection *sec)
7265 {
7266 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7267 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7268 }
7269
7270 /* Finish SHF_MERGE section merging. */
7271
7272 bfd_boolean
7273 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7274 {
7275 bfd *ibfd;
7276 asection *sec;
7277
7278 if (!is_elf_hash_table (info->hash))
7279 return FALSE;
7280
7281 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7282 if ((ibfd->flags & DYNAMIC) == 0
7283 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7284 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7285 == get_elf_backend_data (obfd)->s->elfclass))
7286 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7287 if ((sec->flags & SEC_MERGE) != 0
7288 && !bfd_is_abs_section (sec->output_section))
7289 {
7290 struct bfd_elf_section_data *secdata;
7291
7292 secdata = elf_section_data (sec);
7293 if (! _bfd_add_merge_section (obfd,
7294 &elf_hash_table (info)->merge_info,
7295 sec, &secdata->sec_info))
7296 return FALSE;
7297 else if (secdata->sec_info)
7298 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7299 }
7300
7301 if (elf_hash_table (info)->merge_info != NULL)
7302 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7303 merge_sections_remove_hook);
7304 return TRUE;
7305 }
7306
7307 /* Create an entry in an ELF linker hash table. */
7308
7309 struct bfd_hash_entry *
7310 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7311 struct bfd_hash_table *table,
7312 const char *string)
7313 {
7314 /* Allocate the structure if it has not already been allocated by a
7315 subclass. */
7316 if (entry == NULL)
7317 {
7318 entry = (struct bfd_hash_entry *)
7319 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7320 if (entry == NULL)
7321 return entry;
7322 }
7323
7324 /* Call the allocation method of the superclass. */
7325 entry = _bfd_link_hash_newfunc (entry, table, string);
7326 if (entry != NULL)
7327 {
7328 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7329 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7330
7331 /* Set local fields. */
7332 ret->indx = -1;
7333 ret->dynindx = -1;
7334 ret->got = htab->init_got_refcount;
7335 ret->plt = htab->init_plt_refcount;
7336 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7337 - offsetof (struct elf_link_hash_entry, size)));
7338 /* Assume that we have been called by a non-ELF symbol reader.
7339 This flag is then reset by the code which reads an ELF input
7340 file. This ensures that a symbol created by a non-ELF symbol
7341 reader will have the flag set correctly. */
7342 ret->non_elf = 1;
7343 }
7344
7345 return entry;
7346 }
7347
7348 /* Copy data from an indirect symbol to its direct symbol, hiding the
7349 old indirect symbol. Also used for copying flags to a weakdef. */
7350
7351 void
7352 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7353 struct elf_link_hash_entry *dir,
7354 struct elf_link_hash_entry *ind)
7355 {
7356 struct elf_link_hash_table *htab;
7357
7358 /* Copy down any references that we may have already seen to the
7359 symbol which just became indirect. */
7360
7361 if (dir->versioned != versioned_hidden)
7362 dir->ref_dynamic |= ind->ref_dynamic;
7363 dir->ref_regular |= ind->ref_regular;
7364 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7365 dir->non_got_ref |= ind->non_got_ref;
7366 dir->needs_plt |= ind->needs_plt;
7367 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7368
7369 if (ind->root.type != bfd_link_hash_indirect)
7370 return;
7371
7372 /* Copy over the global and procedure linkage table refcount entries.
7373 These may have been already set up by a check_relocs routine. */
7374 htab = elf_hash_table (info);
7375 if (ind->got.refcount > htab->init_got_refcount.refcount)
7376 {
7377 if (dir->got.refcount < 0)
7378 dir->got.refcount = 0;
7379 dir->got.refcount += ind->got.refcount;
7380 ind->got.refcount = htab->init_got_refcount.refcount;
7381 }
7382
7383 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7384 {
7385 if (dir->plt.refcount < 0)
7386 dir->plt.refcount = 0;
7387 dir->plt.refcount += ind->plt.refcount;
7388 ind->plt.refcount = htab->init_plt_refcount.refcount;
7389 }
7390
7391 if (ind->dynindx != -1)
7392 {
7393 if (dir->dynindx != -1)
7394 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7395 dir->dynindx = ind->dynindx;
7396 dir->dynstr_index = ind->dynstr_index;
7397 ind->dynindx = -1;
7398 ind->dynstr_index = 0;
7399 }
7400 }
7401
7402 void
7403 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7404 struct elf_link_hash_entry *h,
7405 bfd_boolean force_local)
7406 {
7407 /* STT_GNU_IFUNC symbol must go through PLT. */
7408 if (h->type != STT_GNU_IFUNC)
7409 {
7410 h->plt = elf_hash_table (info)->init_plt_offset;
7411 h->needs_plt = 0;
7412 }
7413 if (force_local)
7414 {
7415 h->forced_local = 1;
7416 if (h->dynindx != -1)
7417 {
7418 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7419 h->dynstr_index);
7420 h->dynindx = -1;
7421 h->dynstr_index = 0;
7422 }
7423 }
7424 }
7425
7426 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7427 caller. */
7428
7429 bfd_boolean
7430 _bfd_elf_link_hash_table_init
7431 (struct elf_link_hash_table *table,
7432 bfd *abfd,
7433 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7434 struct bfd_hash_table *,
7435 const char *),
7436 unsigned int entsize,
7437 enum elf_target_id target_id)
7438 {
7439 bfd_boolean ret;
7440 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7441
7442 table->init_got_refcount.refcount = can_refcount - 1;
7443 table->init_plt_refcount.refcount = can_refcount - 1;
7444 table->init_got_offset.offset = -(bfd_vma) 1;
7445 table->init_plt_offset.offset = -(bfd_vma) 1;
7446 /* The first dynamic symbol is a dummy. */
7447 table->dynsymcount = 1;
7448
7449 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7450
7451 table->root.type = bfd_link_elf_hash_table;
7452 table->hash_table_id = target_id;
7453
7454 return ret;
7455 }
7456
7457 /* Create an ELF linker hash table. */
7458
7459 struct bfd_link_hash_table *
7460 _bfd_elf_link_hash_table_create (bfd *abfd)
7461 {
7462 struct elf_link_hash_table *ret;
7463 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7464
7465 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7466 if (ret == NULL)
7467 return NULL;
7468
7469 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7470 sizeof (struct elf_link_hash_entry),
7471 GENERIC_ELF_DATA))
7472 {
7473 free (ret);
7474 return NULL;
7475 }
7476 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7477
7478 return &ret->root;
7479 }
7480
7481 /* Destroy an ELF linker hash table. */
7482
7483 void
7484 _bfd_elf_link_hash_table_free (bfd *obfd)
7485 {
7486 struct elf_link_hash_table *htab;
7487
7488 htab = (struct elf_link_hash_table *) obfd->link.hash;
7489 if (htab->dynstr != NULL)
7490 _bfd_elf_strtab_free (htab->dynstr);
7491 _bfd_merge_sections_free (htab->merge_info);
7492 _bfd_generic_link_hash_table_free (obfd);
7493 }
7494
7495 /* This is a hook for the ELF emulation code in the generic linker to
7496 tell the backend linker what file name to use for the DT_NEEDED
7497 entry for a dynamic object. */
7498
7499 void
7500 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7501 {
7502 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7503 && bfd_get_format (abfd) == bfd_object)
7504 elf_dt_name (abfd) = name;
7505 }
7506
7507 int
7508 bfd_elf_get_dyn_lib_class (bfd *abfd)
7509 {
7510 int lib_class;
7511 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7512 && bfd_get_format (abfd) == bfd_object)
7513 lib_class = elf_dyn_lib_class (abfd);
7514 else
7515 lib_class = 0;
7516 return lib_class;
7517 }
7518
7519 void
7520 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7521 {
7522 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7523 && bfd_get_format (abfd) == bfd_object)
7524 elf_dyn_lib_class (abfd) = lib_class;
7525 }
7526
7527 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7528 the linker ELF emulation code. */
7529
7530 struct bfd_link_needed_list *
7531 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7532 struct bfd_link_info *info)
7533 {
7534 if (! is_elf_hash_table (info->hash))
7535 return NULL;
7536 return elf_hash_table (info)->needed;
7537 }
7538
7539 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7540 hook for the linker ELF emulation code. */
7541
7542 struct bfd_link_needed_list *
7543 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7544 struct bfd_link_info *info)
7545 {
7546 if (! is_elf_hash_table (info->hash))
7547 return NULL;
7548 return elf_hash_table (info)->runpath;
7549 }
7550
7551 /* Get the name actually used for a dynamic object for a link. This
7552 is the SONAME entry if there is one. Otherwise, it is the string
7553 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7554
7555 const char *
7556 bfd_elf_get_dt_soname (bfd *abfd)
7557 {
7558 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7559 && bfd_get_format (abfd) == bfd_object)
7560 return elf_dt_name (abfd);
7561 return NULL;
7562 }
7563
7564 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7565 the ELF linker emulation code. */
7566
7567 bfd_boolean
7568 bfd_elf_get_bfd_needed_list (bfd *abfd,
7569 struct bfd_link_needed_list **pneeded)
7570 {
7571 asection *s;
7572 bfd_byte *dynbuf = NULL;
7573 unsigned int elfsec;
7574 unsigned long shlink;
7575 bfd_byte *extdyn, *extdynend;
7576 size_t extdynsize;
7577 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7578
7579 *pneeded = NULL;
7580
7581 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7582 || bfd_get_format (abfd) != bfd_object)
7583 return TRUE;
7584
7585 s = bfd_get_section_by_name (abfd, ".dynamic");
7586 if (s == NULL || s->size == 0)
7587 return TRUE;
7588
7589 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7590 goto error_return;
7591
7592 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7593 if (elfsec == SHN_BAD)
7594 goto error_return;
7595
7596 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7597
7598 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7599 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7600
7601 extdyn = dynbuf;
7602 extdynend = extdyn + s->size;
7603 for (; extdyn < extdynend; extdyn += extdynsize)
7604 {
7605 Elf_Internal_Dyn dyn;
7606
7607 (*swap_dyn_in) (abfd, extdyn, &dyn);
7608
7609 if (dyn.d_tag == DT_NULL)
7610 break;
7611
7612 if (dyn.d_tag == DT_NEEDED)
7613 {
7614 const char *string;
7615 struct bfd_link_needed_list *l;
7616 unsigned int tagv = dyn.d_un.d_val;
7617 bfd_size_type amt;
7618
7619 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7620 if (string == NULL)
7621 goto error_return;
7622
7623 amt = sizeof *l;
7624 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7625 if (l == NULL)
7626 goto error_return;
7627
7628 l->by = abfd;
7629 l->name = string;
7630 l->next = *pneeded;
7631 *pneeded = l;
7632 }
7633 }
7634
7635 free (dynbuf);
7636
7637 return TRUE;
7638
7639 error_return:
7640 if (dynbuf != NULL)
7641 free (dynbuf);
7642 return FALSE;
7643 }
7644
7645 struct elf_symbuf_symbol
7646 {
7647 unsigned long st_name; /* Symbol name, index in string tbl */
7648 unsigned char st_info; /* Type and binding attributes */
7649 unsigned char st_other; /* Visibilty, and target specific */
7650 };
7651
7652 struct elf_symbuf_head
7653 {
7654 struct elf_symbuf_symbol *ssym;
7655 size_t count;
7656 unsigned int st_shndx;
7657 };
7658
7659 struct elf_symbol
7660 {
7661 union
7662 {
7663 Elf_Internal_Sym *isym;
7664 struct elf_symbuf_symbol *ssym;
7665 } u;
7666 const char *name;
7667 };
7668
7669 /* Sort references to symbols by ascending section number. */
7670
7671 static int
7672 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7673 {
7674 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7675 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7676
7677 return s1->st_shndx - s2->st_shndx;
7678 }
7679
7680 static int
7681 elf_sym_name_compare (const void *arg1, const void *arg2)
7682 {
7683 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7684 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7685 return strcmp (s1->name, s2->name);
7686 }
7687
7688 static struct elf_symbuf_head *
7689 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
7690 {
7691 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7692 struct elf_symbuf_symbol *ssym;
7693 struct elf_symbuf_head *ssymbuf, *ssymhead;
7694 size_t i, shndx_count, total_size;
7695
7696 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7697 if (indbuf == NULL)
7698 return NULL;
7699
7700 for (ind = indbuf, i = 0; i < symcount; i++)
7701 if (isymbuf[i].st_shndx != SHN_UNDEF)
7702 *ind++ = &isymbuf[i];
7703 indbufend = ind;
7704
7705 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7706 elf_sort_elf_symbol);
7707
7708 shndx_count = 0;
7709 if (indbufend > indbuf)
7710 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7711 if (ind[0]->st_shndx != ind[1]->st_shndx)
7712 shndx_count++;
7713
7714 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7715 + (indbufend - indbuf) * sizeof (*ssym));
7716 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7717 if (ssymbuf == NULL)
7718 {
7719 free (indbuf);
7720 return NULL;
7721 }
7722
7723 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7724 ssymbuf->ssym = NULL;
7725 ssymbuf->count = shndx_count;
7726 ssymbuf->st_shndx = 0;
7727 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7728 {
7729 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7730 {
7731 ssymhead++;
7732 ssymhead->ssym = ssym;
7733 ssymhead->count = 0;
7734 ssymhead->st_shndx = (*ind)->st_shndx;
7735 }
7736 ssym->st_name = (*ind)->st_name;
7737 ssym->st_info = (*ind)->st_info;
7738 ssym->st_other = (*ind)->st_other;
7739 ssymhead->count++;
7740 }
7741 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
7742 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7743 == total_size));
7744
7745 free (indbuf);
7746 return ssymbuf;
7747 }
7748
7749 /* Check if 2 sections define the same set of local and global
7750 symbols. */
7751
7752 static bfd_boolean
7753 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7754 struct bfd_link_info *info)
7755 {
7756 bfd *bfd1, *bfd2;
7757 const struct elf_backend_data *bed1, *bed2;
7758 Elf_Internal_Shdr *hdr1, *hdr2;
7759 size_t symcount1, symcount2;
7760 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7761 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7762 Elf_Internal_Sym *isym, *isymend;
7763 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7764 size_t count1, count2, i;
7765 unsigned int shndx1, shndx2;
7766 bfd_boolean result;
7767
7768 bfd1 = sec1->owner;
7769 bfd2 = sec2->owner;
7770
7771 /* Both sections have to be in ELF. */
7772 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7773 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7774 return FALSE;
7775
7776 if (elf_section_type (sec1) != elf_section_type (sec2))
7777 return FALSE;
7778
7779 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7780 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7781 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7782 return FALSE;
7783
7784 bed1 = get_elf_backend_data (bfd1);
7785 bed2 = get_elf_backend_data (bfd2);
7786 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7787 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7788 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7789 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7790
7791 if (symcount1 == 0 || symcount2 == 0)
7792 return FALSE;
7793
7794 result = FALSE;
7795 isymbuf1 = NULL;
7796 isymbuf2 = NULL;
7797 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7798 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7799
7800 if (ssymbuf1 == NULL)
7801 {
7802 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7803 NULL, NULL, NULL);
7804 if (isymbuf1 == NULL)
7805 goto done;
7806
7807 if (!info->reduce_memory_overheads)
7808 elf_tdata (bfd1)->symbuf = ssymbuf1
7809 = elf_create_symbuf (symcount1, isymbuf1);
7810 }
7811
7812 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7813 {
7814 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7815 NULL, NULL, NULL);
7816 if (isymbuf2 == NULL)
7817 goto done;
7818
7819 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7820 elf_tdata (bfd2)->symbuf = ssymbuf2
7821 = elf_create_symbuf (symcount2, isymbuf2);
7822 }
7823
7824 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7825 {
7826 /* Optimized faster version. */
7827 size_t lo, hi, mid;
7828 struct elf_symbol *symp;
7829 struct elf_symbuf_symbol *ssym, *ssymend;
7830
7831 lo = 0;
7832 hi = ssymbuf1->count;
7833 ssymbuf1++;
7834 count1 = 0;
7835 while (lo < hi)
7836 {
7837 mid = (lo + hi) / 2;
7838 if (shndx1 < ssymbuf1[mid].st_shndx)
7839 hi = mid;
7840 else if (shndx1 > ssymbuf1[mid].st_shndx)
7841 lo = mid + 1;
7842 else
7843 {
7844 count1 = ssymbuf1[mid].count;
7845 ssymbuf1 += mid;
7846 break;
7847 }
7848 }
7849
7850 lo = 0;
7851 hi = ssymbuf2->count;
7852 ssymbuf2++;
7853 count2 = 0;
7854 while (lo < hi)
7855 {
7856 mid = (lo + hi) / 2;
7857 if (shndx2 < ssymbuf2[mid].st_shndx)
7858 hi = mid;
7859 else if (shndx2 > ssymbuf2[mid].st_shndx)
7860 lo = mid + 1;
7861 else
7862 {
7863 count2 = ssymbuf2[mid].count;
7864 ssymbuf2 += mid;
7865 break;
7866 }
7867 }
7868
7869 if (count1 == 0 || count2 == 0 || count1 != count2)
7870 goto done;
7871
7872 symtable1
7873 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7874 symtable2
7875 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
7876 if (symtable1 == NULL || symtable2 == NULL)
7877 goto done;
7878
7879 symp = symtable1;
7880 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7881 ssym < ssymend; ssym++, symp++)
7882 {
7883 symp->u.ssym = ssym;
7884 symp->name = bfd_elf_string_from_elf_section (bfd1,
7885 hdr1->sh_link,
7886 ssym->st_name);
7887 }
7888
7889 symp = symtable2;
7890 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7891 ssym < ssymend; ssym++, symp++)
7892 {
7893 symp->u.ssym = ssym;
7894 symp->name = bfd_elf_string_from_elf_section (bfd2,
7895 hdr2->sh_link,
7896 ssym->st_name);
7897 }
7898
7899 /* Sort symbol by name. */
7900 qsort (symtable1, count1, sizeof (struct elf_symbol),
7901 elf_sym_name_compare);
7902 qsort (symtable2, count1, sizeof (struct elf_symbol),
7903 elf_sym_name_compare);
7904
7905 for (i = 0; i < count1; i++)
7906 /* Two symbols must have the same binding, type and name. */
7907 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7908 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7909 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7910 goto done;
7911
7912 result = TRUE;
7913 goto done;
7914 }
7915
7916 symtable1 = (struct elf_symbol *)
7917 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7918 symtable2 = (struct elf_symbol *)
7919 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7920 if (symtable1 == NULL || symtable2 == NULL)
7921 goto done;
7922
7923 /* Count definitions in the section. */
7924 count1 = 0;
7925 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7926 if (isym->st_shndx == shndx1)
7927 symtable1[count1++].u.isym = isym;
7928
7929 count2 = 0;
7930 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7931 if (isym->st_shndx == shndx2)
7932 symtable2[count2++].u.isym = isym;
7933
7934 if (count1 == 0 || count2 == 0 || count1 != count2)
7935 goto done;
7936
7937 for (i = 0; i < count1; i++)
7938 symtable1[i].name
7939 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7940 symtable1[i].u.isym->st_name);
7941
7942 for (i = 0; i < count2; i++)
7943 symtable2[i].name
7944 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7945 symtable2[i].u.isym->st_name);
7946
7947 /* Sort symbol by name. */
7948 qsort (symtable1, count1, sizeof (struct elf_symbol),
7949 elf_sym_name_compare);
7950 qsort (symtable2, count1, sizeof (struct elf_symbol),
7951 elf_sym_name_compare);
7952
7953 for (i = 0; i < count1; i++)
7954 /* Two symbols must have the same binding, type and name. */
7955 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7956 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7957 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7958 goto done;
7959
7960 result = TRUE;
7961
7962 done:
7963 if (symtable1)
7964 free (symtable1);
7965 if (symtable2)
7966 free (symtable2);
7967 if (isymbuf1)
7968 free (isymbuf1);
7969 if (isymbuf2)
7970 free (isymbuf2);
7971
7972 return result;
7973 }
7974
7975 /* Return TRUE if 2 section types are compatible. */
7976
7977 bfd_boolean
7978 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7979 bfd *bbfd, const asection *bsec)
7980 {
7981 if (asec == NULL
7982 || bsec == NULL
7983 || abfd->xvec->flavour != bfd_target_elf_flavour
7984 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7985 return TRUE;
7986
7987 return elf_section_type (asec) == elf_section_type (bsec);
7988 }
7989 \f
7990 /* Final phase of ELF linker. */
7991
7992 /* A structure we use to avoid passing large numbers of arguments. */
7993
7994 struct elf_final_link_info
7995 {
7996 /* General link information. */
7997 struct bfd_link_info *info;
7998 /* Output BFD. */
7999 bfd *output_bfd;
8000 /* Symbol string table. */
8001 struct elf_strtab_hash *symstrtab;
8002 /* .hash section. */
8003 asection *hash_sec;
8004 /* symbol version section (.gnu.version). */
8005 asection *symver_sec;
8006 /* Buffer large enough to hold contents of any section. */
8007 bfd_byte *contents;
8008 /* Buffer large enough to hold external relocs of any section. */
8009 void *external_relocs;
8010 /* Buffer large enough to hold internal relocs of any section. */
8011 Elf_Internal_Rela *internal_relocs;
8012 /* Buffer large enough to hold external local symbols of any input
8013 BFD. */
8014 bfd_byte *external_syms;
8015 /* And a buffer for symbol section indices. */
8016 Elf_External_Sym_Shndx *locsym_shndx;
8017 /* Buffer large enough to hold internal local symbols of any input
8018 BFD. */
8019 Elf_Internal_Sym *internal_syms;
8020 /* Array large enough to hold a symbol index for each local symbol
8021 of any input BFD. */
8022 long *indices;
8023 /* Array large enough to hold a section pointer for each local
8024 symbol of any input BFD. */
8025 asection **sections;
8026 /* Buffer for SHT_SYMTAB_SHNDX section. */
8027 Elf_External_Sym_Shndx *symshndxbuf;
8028 /* Number of STT_FILE syms seen. */
8029 size_t filesym_count;
8030 };
8031
8032 /* This struct is used to pass information to elf_link_output_extsym. */
8033
8034 struct elf_outext_info
8035 {
8036 bfd_boolean failed;
8037 bfd_boolean localsyms;
8038 bfd_boolean file_sym_done;
8039 struct elf_final_link_info *flinfo;
8040 };
8041
8042
8043 /* Support for evaluating a complex relocation.
8044
8045 Complex relocations are generalized, self-describing relocations. The
8046 implementation of them consists of two parts: complex symbols, and the
8047 relocations themselves.
8048
8049 The relocations are use a reserved elf-wide relocation type code (R_RELC
8050 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8051 information (start bit, end bit, word width, etc) into the addend. This
8052 information is extracted from CGEN-generated operand tables within gas.
8053
8054 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8055 internal) representing prefix-notation expressions, including but not
8056 limited to those sorts of expressions normally encoded as addends in the
8057 addend field. The symbol mangling format is:
8058
8059 <node> := <literal>
8060 | <unary-operator> ':' <node>
8061 | <binary-operator> ':' <node> ':' <node>
8062 ;
8063
8064 <literal> := 's' <digits=N> ':' <N character symbol name>
8065 | 'S' <digits=N> ':' <N character section name>
8066 | '#' <hexdigits>
8067 ;
8068
8069 <binary-operator> := as in C
8070 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8071
8072 static void
8073 set_symbol_value (bfd *bfd_with_globals,
8074 Elf_Internal_Sym *isymbuf,
8075 size_t locsymcount,
8076 size_t symidx,
8077 bfd_vma val)
8078 {
8079 struct elf_link_hash_entry **sym_hashes;
8080 struct elf_link_hash_entry *h;
8081 size_t extsymoff = locsymcount;
8082
8083 if (symidx < locsymcount)
8084 {
8085 Elf_Internal_Sym *sym;
8086
8087 sym = isymbuf + symidx;
8088 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8089 {
8090 /* It is a local symbol: move it to the
8091 "absolute" section and give it a value. */
8092 sym->st_shndx = SHN_ABS;
8093 sym->st_value = val;
8094 return;
8095 }
8096 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8097 extsymoff = 0;
8098 }
8099
8100 /* It is a global symbol: set its link type
8101 to "defined" and give it a value. */
8102
8103 sym_hashes = elf_sym_hashes (bfd_with_globals);
8104 h = sym_hashes [symidx - extsymoff];
8105 while (h->root.type == bfd_link_hash_indirect
8106 || h->root.type == bfd_link_hash_warning)
8107 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8108 h->root.type = bfd_link_hash_defined;
8109 h->root.u.def.value = val;
8110 h->root.u.def.section = bfd_abs_section_ptr;
8111 }
8112
8113 static bfd_boolean
8114 resolve_symbol (const char *name,
8115 bfd *input_bfd,
8116 struct elf_final_link_info *flinfo,
8117 bfd_vma *result,
8118 Elf_Internal_Sym *isymbuf,
8119 size_t locsymcount)
8120 {
8121 Elf_Internal_Sym *sym;
8122 struct bfd_link_hash_entry *global_entry;
8123 const char *candidate = NULL;
8124 Elf_Internal_Shdr *symtab_hdr;
8125 size_t i;
8126
8127 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8128
8129 for (i = 0; i < locsymcount; ++ i)
8130 {
8131 sym = isymbuf + i;
8132
8133 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8134 continue;
8135
8136 candidate = bfd_elf_string_from_elf_section (input_bfd,
8137 symtab_hdr->sh_link,
8138 sym->st_name);
8139 #ifdef DEBUG
8140 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8141 name, candidate, (unsigned long) sym->st_value);
8142 #endif
8143 if (candidate && strcmp (candidate, name) == 0)
8144 {
8145 asection *sec = flinfo->sections [i];
8146
8147 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8148 *result += sec->output_offset + sec->output_section->vma;
8149 #ifdef DEBUG
8150 printf ("Found symbol with value %8.8lx\n",
8151 (unsigned long) *result);
8152 #endif
8153 return TRUE;
8154 }
8155 }
8156
8157 /* Hmm, haven't found it yet. perhaps it is a global. */
8158 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8159 FALSE, FALSE, TRUE);
8160 if (!global_entry)
8161 return FALSE;
8162
8163 if (global_entry->type == bfd_link_hash_defined
8164 || global_entry->type == bfd_link_hash_defweak)
8165 {
8166 *result = (global_entry->u.def.value
8167 + global_entry->u.def.section->output_section->vma
8168 + global_entry->u.def.section->output_offset);
8169 #ifdef DEBUG
8170 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8171 global_entry->root.string, (unsigned long) *result);
8172 #endif
8173 return TRUE;
8174 }
8175
8176 return FALSE;
8177 }
8178
8179 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8180 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8181 names like "foo.end" which is the end address of section "foo". */
8182
8183 static bfd_boolean
8184 resolve_section (const char *name,
8185 asection *sections,
8186 bfd_vma *result,
8187 bfd * abfd)
8188 {
8189 asection *curr;
8190 unsigned int len;
8191
8192 for (curr = sections; curr; curr = curr->next)
8193 if (strcmp (curr->name, name) == 0)
8194 {
8195 *result = curr->vma;
8196 return TRUE;
8197 }
8198
8199 /* Hmm. still haven't found it. try pseudo-section names. */
8200 /* FIXME: This could be coded more efficiently... */
8201 for (curr = sections; curr; curr = curr->next)
8202 {
8203 len = strlen (curr->name);
8204 if (len > strlen (name))
8205 continue;
8206
8207 if (strncmp (curr->name, name, len) == 0)
8208 {
8209 if (strncmp (".end", name + len, 4) == 0)
8210 {
8211 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
8212 return TRUE;
8213 }
8214
8215 /* Insert more pseudo-section names here, if you like. */
8216 }
8217 }
8218
8219 return FALSE;
8220 }
8221
8222 static void
8223 undefined_reference (const char *reftype, const char *name)
8224 {
8225 /* xgettext:c-format */
8226 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8227 reftype, name);
8228 }
8229
8230 static bfd_boolean
8231 eval_symbol (bfd_vma *result,
8232 const char **symp,
8233 bfd *input_bfd,
8234 struct elf_final_link_info *flinfo,
8235 bfd_vma dot,
8236 Elf_Internal_Sym *isymbuf,
8237 size_t locsymcount,
8238 int signed_p)
8239 {
8240 size_t len;
8241 size_t symlen;
8242 bfd_vma a;
8243 bfd_vma b;
8244 char symbuf[4096];
8245 const char *sym = *symp;
8246 const char *symend;
8247 bfd_boolean symbol_is_section = FALSE;
8248
8249 len = strlen (sym);
8250 symend = sym + len;
8251
8252 if (len < 1 || len > sizeof (symbuf))
8253 {
8254 bfd_set_error (bfd_error_invalid_operation);
8255 return FALSE;
8256 }
8257
8258 switch (* sym)
8259 {
8260 case '.':
8261 *result = dot;
8262 *symp = sym + 1;
8263 return TRUE;
8264
8265 case '#':
8266 ++sym;
8267 *result = strtoul (sym, (char **) symp, 16);
8268 return TRUE;
8269
8270 case 'S':
8271 symbol_is_section = TRUE;
8272 /* Fall through. */
8273 case 's':
8274 ++sym;
8275 symlen = strtol (sym, (char **) symp, 10);
8276 sym = *symp + 1; /* Skip the trailing ':'. */
8277
8278 if (symend < sym || symlen + 1 > sizeof (symbuf))
8279 {
8280 bfd_set_error (bfd_error_invalid_operation);
8281 return FALSE;
8282 }
8283
8284 memcpy (symbuf, sym, symlen);
8285 symbuf[symlen] = '\0';
8286 *symp = sym + symlen;
8287
8288 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8289 the symbol as a section, or vice-versa. so we're pretty liberal in our
8290 interpretation here; section means "try section first", not "must be a
8291 section", and likewise with symbol. */
8292
8293 if (symbol_is_section)
8294 {
8295 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8296 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8297 isymbuf, locsymcount))
8298 {
8299 undefined_reference ("section", symbuf);
8300 return FALSE;
8301 }
8302 }
8303 else
8304 {
8305 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8306 isymbuf, locsymcount)
8307 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8308 result, input_bfd))
8309 {
8310 undefined_reference ("symbol", symbuf);
8311 return FALSE;
8312 }
8313 }
8314
8315 return TRUE;
8316
8317 /* All that remains are operators. */
8318
8319 #define UNARY_OP(op) \
8320 if (strncmp (sym, #op, strlen (#op)) == 0) \
8321 { \
8322 sym += strlen (#op); \
8323 if (*sym == ':') \
8324 ++sym; \
8325 *symp = sym; \
8326 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8327 isymbuf, locsymcount, signed_p)) \
8328 return FALSE; \
8329 if (signed_p) \
8330 *result = op ((bfd_signed_vma) a); \
8331 else \
8332 *result = op a; \
8333 return TRUE; \
8334 }
8335
8336 #define BINARY_OP(op) \
8337 if (strncmp (sym, #op, strlen (#op)) == 0) \
8338 { \
8339 sym += strlen (#op); \
8340 if (*sym == ':') \
8341 ++sym; \
8342 *symp = sym; \
8343 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8344 isymbuf, locsymcount, signed_p)) \
8345 return FALSE; \
8346 ++*symp; \
8347 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8348 isymbuf, locsymcount, signed_p)) \
8349 return FALSE; \
8350 if (signed_p) \
8351 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8352 else \
8353 *result = a op b; \
8354 return TRUE; \
8355 }
8356
8357 default:
8358 UNARY_OP (0-);
8359 BINARY_OP (<<);
8360 BINARY_OP (>>);
8361 BINARY_OP (==);
8362 BINARY_OP (!=);
8363 BINARY_OP (<=);
8364 BINARY_OP (>=);
8365 BINARY_OP (&&);
8366 BINARY_OP (||);
8367 UNARY_OP (~);
8368 UNARY_OP (!);
8369 BINARY_OP (*);
8370 BINARY_OP (/);
8371 BINARY_OP (%);
8372 BINARY_OP (^);
8373 BINARY_OP (|);
8374 BINARY_OP (&);
8375 BINARY_OP (+);
8376 BINARY_OP (-);
8377 BINARY_OP (<);
8378 BINARY_OP (>);
8379 #undef UNARY_OP
8380 #undef BINARY_OP
8381 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8382 bfd_set_error (bfd_error_invalid_operation);
8383 return FALSE;
8384 }
8385 }
8386
8387 static void
8388 put_value (bfd_vma size,
8389 unsigned long chunksz,
8390 bfd *input_bfd,
8391 bfd_vma x,
8392 bfd_byte *location)
8393 {
8394 location += (size - chunksz);
8395
8396 for (; size; size -= chunksz, location -= chunksz)
8397 {
8398 switch (chunksz)
8399 {
8400 case 1:
8401 bfd_put_8 (input_bfd, x, location);
8402 x >>= 8;
8403 break;
8404 case 2:
8405 bfd_put_16 (input_bfd, x, location);
8406 x >>= 16;
8407 break;
8408 case 4:
8409 bfd_put_32 (input_bfd, x, location);
8410 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8411 x >>= 16;
8412 x >>= 16;
8413 break;
8414 #ifdef BFD64
8415 case 8:
8416 bfd_put_64 (input_bfd, x, location);
8417 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8418 x >>= 32;
8419 x >>= 32;
8420 break;
8421 #endif
8422 default:
8423 abort ();
8424 break;
8425 }
8426 }
8427 }
8428
8429 static bfd_vma
8430 get_value (bfd_vma size,
8431 unsigned long chunksz,
8432 bfd *input_bfd,
8433 bfd_byte *location)
8434 {
8435 int shift;
8436 bfd_vma x = 0;
8437
8438 /* Sanity checks. */
8439 BFD_ASSERT (chunksz <= sizeof (x)
8440 && size >= chunksz
8441 && chunksz != 0
8442 && (size % chunksz) == 0
8443 && input_bfd != NULL
8444 && location != NULL);
8445
8446 if (chunksz == sizeof (x))
8447 {
8448 BFD_ASSERT (size == chunksz);
8449
8450 /* Make sure that we do not perform an undefined shift operation.
8451 We know that size == chunksz so there will only be one iteration
8452 of the loop below. */
8453 shift = 0;
8454 }
8455 else
8456 shift = 8 * chunksz;
8457
8458 for (; size; size -= chunksz, location += chunksz)
8459 {
8460 switch (chunksz)
8461 {
8462 case 1:
8463 x = (x << shift) | bfd_get_8 (input_bfd, location);
8464 break;
8465 case 2:
8466 x = (x << shift) | bfd_get_16 (input_bfd, location);
8467 break;
8468 case 4:
8469 x = (x << shift) | bfd_get_32 (input_bfd, location);
8470 break;
8471 #ifdef BFD64
8472 case 8:
8473 x = (x << shift) | bfd_get_64 (input_bfd, location);
8474 break;
8475 #endif
8476 default:
8477 abort ();
8478 }
8479 }
8480 return x;
8481 }
8482
8483 static void
8484 decode_complex_addend (unsigned long *start, /* in bits */
8485 unsigned long *oplen, /* in bits */
8486 unsigned long *len, /* in bits */
8487 unsigned long *wordsz, /* in bytes */
8488 unsigned long *chunksz, /* in bytes */
8489 unsigned long *lsb0_p,
8490 unsigned long *signed_p,
8491 unsigned long *trunc_p,
8492 unsigned long encoded)
8493 {
8494 * start = encoded & 0x3F;
8495 * len = (encoded >> 6) & 0x3F;
8496 * oplen = (encoded >> 12) & 0x3F;
8497 * wordsz = (encoded >> 18) & 0xF;
8498 * chunksz = (encoded >> 22) & 0xF;
8499 * lsb0_p = (encoded >> 27) & 1;
8500 * signed_p = (encoded >> 28) & 1;
8501 * trunc_p = (encoded >> 29) & 1;
8502 }
8503
8504 bfd_reloc_status_type
8505 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8506 asection *input_section ATTRIBUTE_UNUSED,
8507 bfd_byte *contents,
8508 Elf_Internal_Rela *rel,
8509 bfd_vma relocation)
8510 {
8511 bfd_vma shift, x, mask;
8512 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8513 bfd_reloc_status_type r;
8514
8515 /* Perform this reloc, since it is complex.
8516 (this is not to say that it necessarily refers to a complex
8517 symbol; merely that it is a self-describing CGEN based reloc.
8518 i.e. the addend has the complete reloc information (bit start, end,
8519 word size, etc) encoded within it.). */
8520
8521 decode_complex_addend (&start, &oplen, &len, &wordsz,
8522 &chunksz, &lsb0_p, &signed_p,
8523 &trunc_p, rel->r_addend);
8524
8525 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8526
8527 if (lsb0_p)
8528 shift = (start + 1) - len;
8529 else
8530 shift = (8 * wordsz) - (start + len);
8531
8532 x = get_value (wordsz, chunksz, input_bfd,
8533 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8534
8535 #ifdef DEBUG
8536 printf ("Doing complex reloc: "
8537 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8538 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8539 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8540 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8541 oplen, (unsigned long) x, (unsigned long) mask,
8542 (unsigned long) relocation);
8543 #endif
8544
8545 r = bfd_reloc_ok;
8546 if (! trunc_p)
8547 /* Now do an overflow check. */
8548 r = bfd_check_overflow ((signed_p
8549 ? complain_overflow_signed
8550 : complain_overflow_unsigned),
8551 len, 0, (8 * wordsz),
8552 relocation);
8553
8554 /* Do the deed. */
8555 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8556
8557 #ifdef DEBUG
8558 printf (" relocation: %8.8lx\n"
8559 " shifted mask: %8.8lx\n"
8560 " shifted/masked reloc: %8.8lx\n"
8561 " result: %8.8lx\n",
8562 (unsigned long) relocation, (unsigned long) (mask << shift),
8563 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8564 #endif
8565 put_value (wordsz, chunksz, input_bfd, x,
8566 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8567 return r;
8568 }
8569
8570 /* Functions to read r_offset from external (target order) reloc
8571 entry. Faster than bfd_getl32 et al, because we let the compiler
8572 know the value is aligned. */
8573
8574 static bfd_vma
8575 ext32l_r_offset (const void *p)
8576 {
8577 union aligned32
8578 {
8579 uint32_t v;
8580 unsigned char c[4];
8581 };
8582 const union aligned32 *a
8583 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8584
8585 uint32_t aval = ( (uint32_t) a->c[0]
8586 | (uint32_t) a->c[1] << 8
8587 | (uint32_t) a->c[2] << 16
8588 | (uint32_t) a->c[3] << 24);
8589 return aval;
8590 }
8591
8592 static bfd_vma
8593 ext32b_r_offset (const void *p)
8594 {
8595 union aligned32
8596 {
8597 uint32_t v;
8598 unsigned char c[4];
8599 };
8600 const union aligned32 *a
8601 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8602
8603 uint32_t aval = ( (uint32_t) a->c[0] << 24
8604 | (uint32_t) a->c[1] << 16
8605 | (uint32_t) a->c[2] << 8
8606 | (uint32_t) a->c[3]);
8607 return aval;
8608 }
8609
8610 #ifdef BFD_HOST_64_BIT
8611 static bfd_vma
8612 ext64l_r_offset (const void *p)
8613 {
8614 union aligned64
8615 {
8616 uint64_t v;
8617 unsigned char c[8];
8618 };
8619 const union aligned64 *a
8620 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8621
8622 uint64_t aval = ( (uint64_t) a->c[0]
8623 | (uint64_t) a->c[1] << 8
8624 | (uint64_t) a->c[2] << 16
8625 | (uint64_t) a->c[3] << 24
8626 | (uint64_t) a->c[4] << 32
8627 | (uint64_t) a->c[5] << 40
8628 | (uint64_t) a->c[6] << 48
8629 | (uint64_t) a->c[7] << 56);
8630 return aval;
8631 }
8632
8633 static bfd_vma
8634 ext64b_r_offset (const void *p)
8635 {
8636 union aligned64
8637 {
8638 uint64_t v;
8639 unsigned char c[8];
8640 };
8641 const union aligned64 *a
8642 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8643
8644 uint64_t aval = ( (uint64_t) a->c[0] << 56
8645 | (uint64_t) a->c[1] << 48
8646 | (uint64_t) a->c[2] << 40
8647 | (uint64_t) a->c[3] << 32
8648 | (uint64_t) a->c[4] << 24
8649 | (uint64_t) a->c[5] << 16
8650 | (uint64_t) a->c[6] << 8
8651 | (uint64_t) a->c[7]);
8652 return aval;
8653 }
8654 #endif
8655
8656 /* When performing a relocatable link, the input relocations are
8657 preserved. But, if they reference global symbols, the indices
8658 referenced must be updated. Update all the relocations found in
8659 RELDATA. */
8660
8661 static bfd_boolean
8662 elf_link_adjust_relocs (bfd *abfd,
8663 asection *sec,
8664 struct bfd_elf_section_reloc_data *reldata,
8665 bfd_boolean sort,
8666 struct bfd_link_info *info)
8667 {
8668 unsigned int i;
8669 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8670 bfd_byte *erela;
8671 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8672 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8673 bfd_vma r_type_mask;
8674 int r_sym_shift;
8675 unsigned int count = reldata->count;
8676 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8677
8678 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8679 {
8680 swap_in = bed->s->swap_reloc_in;
8681 swap_out = bed->s->swap_reloc_out;
8682 }
8683 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8684 {
8685 swap_in = bed->s->swap_reloca_in;
8686 swap_out = bed->s->swap_reloca_out;
8687 }
8688 else
8689 abort ();
8690
8691 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8692 abort ();
8693
8694 if (bed->s->arch_size == 32)
8695 {
8696 r_type_mask = 0xff;
8697 r_sym_shift = 8;
8698 }
8699 else
8700 {
8701 r_type_mask = 0xffffffff;
8702 r_sym_shift = 32;
8703 }
8704
8705 erela = reldata->hdr->contents;
8706 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8707 {
8708 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8709 unsigned int j;
8710
8711 if (*rel_hash == NULL)
8712 continue;
8713
8714 if ((*rel_hash)->indx == -2
8715 && info->gc_sections
8716 && ! info->gc_keep_exported)
8717 {
8718 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
8719 _bfd_error_handler (_("%B:%A: error: relocation references symbol %s which was removed by garbage collection."),
8720 abfd, sec,
8721 (*rel_hash)->root.root.string);
8722 _bfd_error_handler (_("%B:%A: error: try relinking with --gc-keep-exported enabled."),
8723 abfd, sec);
8724 bfd_set_error (bfd_error_invalid_operation);
8725 return FALSE;
8726 }
8727 BFD_ASSERT ((*rel_hash)->indx >= 0);
8728
8729 (*swap_in) (abfd, erela, irela);
8730 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8731 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8732 | (irela[j].r_info & r_type_mask));
8733 (*swap_out) (abfd, irela, erela);
8734 }
8735
8736 if (bed->elf_backend_update_relocs)
8737 (*bed->elf_backend_update_relocs) (sec, reldata);
8738
8739 if (sort && count != 0)
8740 {
8741 bfd_vma (*ext_r_off) (const void *);
8742 bfd_vma r_off;
8743 size_t elt_size;
8744 bfd_byte *base, *end, *p, *loc;
8745 bfd_byte *buf = NULL;
8746
8747 if (bed->s->arch_size == 32)
8748 {
8749 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8750 ext_r_off = ext32l_r_offset;
8751 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8752 ext_r_off = ext32b_r_offset;
8753 else
8754 abort ();
8755 }
8756 else
8757 {
8758 #ifdef BFD_HOST_64_BIT
8759 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8760 ext_r_off = ext64l_r_offset;
8761 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8762 ext_r_off = ext64b_r_offset;
8763 else
8764 #endif
8765 abort ();
8766 }
8767
8768 /* Must use a stable sort here. A modified insertion sort,
8769 since the relocs are mostly sorted already. */
8770 elt_size = reldata->hdr->sh_entsize;
8771 base = reldata->hdr->contents;
8772 end = base + count * elt_size;
8773 if (elt_size > sizeof (Elf64_External_Rela))
8774 abort ();
8775
8776 /* Ensure the first element is lowest. This acts as a sentinel,
8777 speeding the main loop below. */
8778 r_off = (*ext_r_off) (base);
8779 for (p = loc = base; (p += elt_size) < end; )
8780 {
8781 bfd_vma r_off2 = (*ext_r_off) (p);
8782 if (r_off > r_off2)
8783 {
8784 r_off = r_off2;
8785 loc = p;
8786 }
8787 }
8788 if (loc != base)
8789 {
8790 /* Don't just swap *base and *loc as that changes the order
8791 of the original base[0] and base[1] if they happen to
8792 have the same r_offset. */
8793 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8794 memcpy (onebuf, loc, elt_size);
8795 memmove (base + elt_size, base, loc - base);
8796 memcpy (base, onebuf, elt_size);
8797 }
8798
8799 for (p = base + elt_size; (p += elt_size) < end; )
8800 {
8801 /* base to p is sorted, *p is next to insert. */
8802 r_off = (*ext_r_off) (p);
8803 /* Search the sorted region for location to insert. */
8804 loc = p - elt_size;
8805 while (r_off < (*ext_r_off) (loc))
8806 loc -= elt_size;
8807 loc += elt_size;
8808 if (loc != p)
8809 {
8810 /* Chances are there is a run of relocs to insert here,
8811 from one of more input files. Files are not always
8812 linked in order due to the way elf_link_input_bfd is
8813 called. See pr17666. */
8814 size_t sortlen = p - loc;
8815 bfd_vma r_off2 = (*ext_r_off) (loc);
8816 size_t runlen = elt_size;
8817 size_t buf_size = 96 * 1024;
8818 while (p + runlen < end
8819 && (sortlen <= buf_size
8820 || runlen + elt_size <= buf_size)
8821 && r_off2 > (*ext_r_off) (p + runlen))
8822 runlen += elt_size;
8823 if (buf == NULL)
8824 {
8825 buf = bfd_malloc (buf_size);
8826 if (buf == NULL)
8827 return FALSE;
8828 }
8829 if (runlen < sortlen)
8830 {
8831 memcpy (buf, p, runlen);
8832 memmove (loc + runlen, loc, sortlen);
8833 memcpy (loc, buf, runlen);
8834 }
8835 else
8836 {
8837 memcpy (buf, loc, sortlen);
8838 memmove (loc, p, runlen);
8839 memcpy (loc + runlen, buf, sortlen);
8840 }
8841 p += runlen - elt_size;
8842 }
8843 }
8844 /* Hashes are no longer valid. */
8845 free (reldata->hashes);
8846 reldata->hashes = NULL;
8847 free (buf);
8848 }
8849 return TRUE;
8850 }
8851
8852 struct elf_link_sort_rela
8853 {
8854 union {
8855 bfd_vma offset;
8856 bfd_vma sym_mask;
8857 } u;
8858 enum elf_reloc_type_class type;
8859 /* We use this as an array of size int_rels_per_ext_rel. */
8860 Elf_Internal_Rela rela[1];
8861 };
8862
8863 static int
8864 elf_link_sort_cmp1 (const void *A, const void *B)
8865 {
8866 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8867 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8868 int relativea, relativeb;
8869
8870 relativea = a->type == reloc_class_relative;
8871 relativeb = b->type == reloc_class_relative;
8872
8873 if (relativea < relativeb)
8874 return 1;
8875 if (relativea > relativeb)
8876 return -1;
8877 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8878 return -1;
8879 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8880 return 1;
8881 if (a->rela->r_offset < b->rela->r_offset)
8882 return -1;
8883 if (a->rela->r_offset > b->rela->r_offset)
8884 return 1;
8885 return 0;
8886 }
8887
8888 static int
8889 elf_link_sort_cmp2 (const void *A, const void *B)
8890 {
8891 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8892 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8893
8894 if (a->type < b->type)
8895 return -1;
8896 if (a->type > b->type)
8897 return 1;
8898 if (a->u.offset < b->u.offset)
8899 return -1;
8900 if (a->u.offset > b->u.offset)
8901 return 1;
8902 if (a->rela->r_offset < b->rela->r_offset)
8903 return -1;
8904 if (a->rela->r_offset > b->rela->r_offset)
8905 return 1;
8906 return 0;
8907 }
8908
8909 static size_t
8910 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8911 {
8912 asection *dynamic_relocs;
8913 asection *rela_dyn;
8914 asection *rel_dyn;
8915 bfd_size_type count, size;
8916 size_t i, ret, sort_elt, ext_size;
8917 bfd_byte *sort, *s_non_relative, *p;
8918 struct elf_link_sort_rela *sq;
8919 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8920 int i2e = bed->s->int_rels_per_ext_rel;
8921 unsigned int opb = bfd_octets_per_byte (abfd);
8922 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8923 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8924 struct bfd_link_order *lo;
8925 bfd_vma r_sym_mask;
8926 bfd_boolean use_rela;
8927
8928 /* Find a dynamic reloc section. */
8929 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8930 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8931 if (rela_dyn != NULL && rela_dyn->size > 0
8932 && rel_dyn != NULL && rel_dyn->size > 0)
8933 {
8934 bfd_boolean use_rela_initialised = FALSE;
8935
8936 /* This is just here to stop gcc from complaining.
8937 Its initialization checking code is not perfect. */
8938 use_rela = TRUE;
8939
8940 /* Both sections are present. Examine the sizes
8941 of the indirect sections to help us choose. */
8942 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8943 if (lo->type == bfd_indirect_link_order)
8944 {
8945 asection *o = lo->u.indirect.section;
8946
8947 if ((o->size % bed->s->sizeof_rela) == 0)
8948 {
8949 if ((o->size % bed->s->sizeof_rel) == 0)
8950 /* Section size is divisible by both rel and rela sizes.
8951 It is of no help to us. */
8952 ;
8953 else
8954 {
8955 /* Section size is only divisible by rela. */
8956 if (use_rela_initialised && !use_rela)
8957 {
8958 _bfd_error_handler (_("%B: Unable to sort relocs - "
8959 "they are in more than one size"),
8960 abfd);
8961 bfd_set_error (bfd_error_invalid_operation);
8962 return 0;
8963 }
8964 else
8965 {
8966 use_rela = TRUE;
8967 use_rela_initialised = TRUE;
8968 }
8969 }
8970 }
8971 else if ((o->size % bed->s->sizeof_rel) == 0)
8972 {
8973 /* Section size is only divisible by rel. */
8974 if (use_rela_initialised && use_rela)
8975 {
8976 _bfd_error_handler (_("%B: Unable to sort relocs - "
8977 "they are in more than one size"),
8978 abfd);
8979 bfd_set_error (bfd_error_invalid_operation);
8980 return 0;
8981 }
8982 else
8983 {
8984 use_rela = FALSE;
8985 use_rela_initialised = TRUE;
8986 }
8987 }
8988 else
8989 {
8990 /* The section size is not divisible by either -
8991 something is wrong. */
8992 _bfd_error_handler (_("%B: Unable to sort relocs - "
8993 "they are of an unknown size"), abfd);
8994 bfd_set_error (bfd_error_invalid_operation);
8995 return 0;
8996 }
8997 }
8998
8999 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9000 if (lo->type == bfd_indirect_link_order)
9001 {
9002 asection *o = lo->u.indirect.section;
9003
9004 if ((o->size % bed->s->sizeof_rela) == 0)
9005 {
9006 if ((o->size % bed->s->sizeof_rel) == 0)
9007 /* Section size is divisible by both rel and rela sizes.
9008 It is of no help to us. */
9009 ;
9010 else
9011 {
9012 /* Section size is only divisible by rela. */
9013 if (use_rela_initialised && !use_rela)
9014 {
9015 _bfd_error_handler (_("%B: Unable to sort relocs - "
9016 "they are in more than one size"),
9017 abfd);
9018 bfd_set_error (bfd_error_invalid_operation);
9019 return 0;
9020 }
9021 else
9022 {
9023 use_rela = TRUE;
9024 use_rela_initialised = TRUE;
9025 }
9026 }
9027 }
9028 else if ((o->size % bed->s->sizeof_rel) == 0)
9029 {
9030 /* Section size is only divisible by rel. */
9031 if (use_rela_initialised && use_rela)
9032 {
9033 _bfd_error_handler (_("%B: Unable to sort relocs - "
9034 "they are in more than one size"),
9035 abfd);
9036 bfd_set_error (bfd_error_invalid_operation);
9037 return 0;
9038 }
9039 else
9040 {
9041 use_rela = FALSE;
9042 use_rela_initialised = TRUE;
9043 }
9044 }
9045 else
9046 {
9047 /* The section size is not divisible by either -
9048 something is wrong. */
9049 _bfd_error_handler (_("%B: Unable to sort relocs - "
9050 "they are of an unknown size"), abfd);
9051 bfd_set_error (bfd_error_invalid_operation);
9052 return 0;
9053 }
9054 }
9055
9056 if (! use_rela_initialised)
9057 /* Make a guess. */
9058 use_rela = TRUE;
9059 }
9060 else if (rela_dyn != NULL && rela_dyn->size > 0)
9061 use_rela = TRUE;
9062 else if (rel_dyn != NULL && rel_dyn->size > 0)
9063 use_rela = FALSE;
9064 else
9065 return 0;
9066
9067 if (use_rela)
9068 {
9069 dynamic_relocs = rela_dyn;
9070 ext_size = bed->s->sizeof_rela;
9071 swap_in = bed->s->swap_reloca_in;
9072 swap_out = bed->s->swap_reloca_out;
9073 }
9074 else
9075 {
9076 dynamic_relocs = rel_dyn;
9077 ext_size = bed->s->sizeof_rel;
9078 swap_in = bed->s->swap_reloc_in;
9079 swap_out = bed->s->swap_reloc_out;
9080 }
9081
9082 size = 0;
9083 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9084 if (lo->type == bfd_indirect_link_order)
9085 size += lo->u.indirect.section->size;
9086
9087 if (size != dynamic_relocs->size)
9088 return 0;
9089
9090 sort_elt = (sizeof (struct elf_link_sort_rela)
9091 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9092
9093 count = dynamic_relocs->size / ext_size;
9094 if (count == 0)
9095 return 0;
9096 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9097
9098 if (sort == NULL)
9099 {
9100 (*info->callbacks->warning)
9101 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
9102 return 0;
9103 }
9104
9105 if (bed->s->arch_size == 32)
9106 r_sym_mask = ~(bfd_vma) 0xff;
9107 else
9108 r_sym_mask = ~(bfd_vma) 0xffffffff;
9109
9110 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9111 if (lo->type == bfd_indirect_link_order)
9112 {
9113 bfd_byte *erel, *erelend;
9114 asection *o = lo->u.indirect.section;
9115
9116 if (o->contents == NULL && o->size != 0)
9117 {
9118 /* This is a reloc section that is being handled as a normal
9119 section. See bfd_section_from_shdr. We can't combine
9120 relocs in this case. */
9121 free (sort);
9122 return 0;
9123 }
9124 erel = o->contents;
9125 erelend = o->contents + o->size;
9126 p = sort + o->output_offset * opb / ext_size * sort_elt;
9127
9128 while (erel < erelend)
9129 {
9130 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9131
9132 (*swap_in) (abfd, erel, s->rela);
9133 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9134 s->u.sym_mask = r_sym_mask;
9135 p += sort_elt;
9136 erel += ext_size;
9137 }
9138 }
9139
9140 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9141
9142 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9143 {
9144 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9145 if (s->type != reloc_class_relative)
9146 break;
9147 }
9148 ret = i;
9149 s_non_relative = p;
9150
9151 sq = (struct elf_link_sort_rela *) s_non_relative;
9152 for (; i < count; i++, p += sort_elt)
9153 {
9154 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9155 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9156 sq = sp;
9157 sp->u.offset = sq->rela->r_offset;
9158 }
9159
9160 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9161
9162 struct elf_link_hash_table *htab = elf_hash_table (info);
9163 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9164 {
9165 /* We have plt relocs in .rela.dyn. */
9166 sq = (struct elf_link_sort_rela *) sort;
9167 for (i = 0; i < count; i++)
9168 if (sq[count - i - 1].type != reloc_class_plt)
9169 break;
9170 if (i != 0 && htab->srelplt->size == i * ext_size)
9171 {
9172 struct bfd_link_order **plo;
9173 /* Put srelplt link_order last. This is so the output_offset
9174 set in the next loop is correct for DT_JMPREL. */
9175 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9176 if ((*plo)->type == bfd_indirect_link_order
9177 && (*plo)->u.indirect.section == htab->srelplt)
9178 {
9179 lo = *plo;
9180 *plo = lo->next;
9181 }
9182 else
9183 plo = &(*plo)->next;
9184 *plo = lo;
9185 lo->next = NULL;
9186 dynamic_relocs->map_tail.link_order = lo;
9187 }
9188 }
9189
9190 p = sort;
9191 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9192 if (lo->type == bfd_indirect_link_order)
9193 {
9194 bfd_byte *erel, *erelend;
9195 asection *o = lo->u.indirect.section;
9196
9197 erel = o->contents;
9198 erelend = o->contents + o->size;
9199 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9200 while (erel < erelend)
9201 {
9202 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9203 (*swap_out) (abfd, s->rela, erel);
9204 p += sort_elt;
9205 erel += ext_size;
9206 }
9207 }
9208
9209 free (sort);
9210 *psec = dynamic_relocs;
9211 return ret;
9212 }
9213
9214 /* Add a symbol to the output symbol string table. */
9215
9216 static int
9217 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9218 const char *name,
9219 Elf_Internal_Sym *elfsym,
9220 asection *input_sec,
9221 struct elf_link_hash_entry *h)
9222 {
9223 int (*output_symbol_hook)
9224 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9225 struct elf_link_hash_entry *);
9226 struct elf_link_hash_table *hash_table;
9227 const struct elf_backend_data *bed;
9228 bfd_size_type strtabsize;
9229
9230 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9231
9232 bed = get_elf_backend_data (flinfo->output_bfd);
9233 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9234 if (output_symbol_hook != NULL)
9235 {
9236 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9237 if (ret != 1)
9238 return ret;
9239 }
9240
9241 if (name == NULL
9242 || *name == '\0'
9243 || (input_sec->flags & SEC_EXCLUDE))
9244 elfsym->st_name = (unsigned long) -1;
9245 else
9246 {
9247 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9248 to get the final offset for st_name. */
9249 elfsym->st_name
9250 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9251 name, FALSE);
9252 if (elfsym->st_name == (unsigned long) -1)
9253 return 0;
9254 }
9255
9256 hash_table = elf_hash_table (flinfo->info);
9257 strtabsize = hash_table->strtabsize;
9258 if (strtabsize <= hash_table->strtabcount)
9259 {
9260 strtabsize += strtabsize;
9261 hash_table->strtabsize = strtabsize;
9262 strtabsize *= sizeof (*hash_table->strtab);
9263 hash_table->strtab
9264 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9265 strtabsize);
9266 if (hash_table->strtab == NULL)
9267 return 0;
9268 }
9269 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9270 hash_table->strtab[hash_table->strtabcount].dest_index
9271 = hash_table->strtabcount;
9272 hash_table->strtab[hash_table->strtabcount].destshndx_index
9273 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9274
9275 bfd_get_symcount (flinfo->output_bfd) += 1;
9276 hash_table->strtabcount += 1;
9277
9278 return 1;
9279 }
9280
9281 /* Swap symbols out to the symbol table and flush the output symbols to
9282 the file. */
9283
9284 static bfd_boolean
9285 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9286 {
9287 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9288 bfd_size_type amt;
9289 size_t i;
9290 const struct elf_backend_data *bed;
9291 bfd_byte *symbuf;
9292 Elf_Internal_Shdr *hdr;
9293 file_ptr pos;
9294 bfd_boolean ret;
9295
9296 if (!hash_table->strtabcount)
9297 return TRUE;
9298
9299 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9300
9301 bed = get_elf_backend_data (flinfo->output_bfd);
9302
9303 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9304 symbuf = (bfd_byte *) bfd_malloc (amt);
9305 if (symbuf == NULL)
9306 return FALSE;
9307
9308 if (flinfo->symshndxbuf)
9309 {
9310 amt = sizeof (Elf_External_Sym_Shndx);
9311 amt *= bfd_get_symcount (flinfo->output_bfd);
9312 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9313 if (flinfo->symshndxbuf == NULL)
9314 {
9315 free (symbuf);
9316 return FALSE;
9317 }
9318 }
9319
9320 for (i = 0; i < hash_table->strtabcount; i++)
9321 {
9322 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9323 if (elfsym->sym.st_name == (unsigned long) -1)
9324 elfsym->sym.st_name = 0;
9325 else
9326 elfsym->sym.st_name
9327 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9328 elfsym->sym.st_name);
9329 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9330 ((bfd_byte *) symbuf
9331 + (elfsym->dest_index
9332 * bed->s->sizeof_sym)),
9333 (flinfo->symshndxbuf
9334 + elfsym->destshndx_index));
9335 }
9336
9337 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9338 pos = hdr->sh_offset + hdr->sh_size;
9339 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9340 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9341 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9342 {
9343 hdr->sh_size += amt;
9344 ret = TRUE;
9345 }
9346 else
9347 ret = FALSE;
9348
9349 free (symbuf);
9350
9351 free (hash_table->strtab);
9352 hash_table->strtab = NULL;
9353
9354 return ret;
9355 }
9356
9357 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9358
9359 static bfd_boolean
9360 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9361 {
9362 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9363 && sym->st_shndx < SHN_LORESERVE)
9364 {
9365 /* The gABI doesn't support dynamic symbols in output sections
9366 beyond 64k. */
9367 _bfd_error_handler
9368 /* xgettext:c-format */
9369 (_("%B: Too many sections: %d (>= %d)"),
9370 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9371 bfd_set_error (bfd_error_nonrepresentable_section);
9372 return FALSE;
9373 }
9374 return TRUE;
9375 }
9376
9377 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9378 allowing an unsatisfied unversioned symbol in the DSO to match a
9379 versioned symbol that would normally require an explicit version.
9380 We also handle the case that a DSO references a hidden symbol
9381 which may be satisfied by a versioned symbol in another DSO. */
9382
9383 static bfd_boolean
9384 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9385 const struct elf_backend_data *bed,
9386 struct elf_link_hash_entry *h)
9387 {
9388 bfd *abfd;
9389 struct elf_link_loaded_list *loaded;
9390
9391 if (!is_elf_hash_table (info->hash))
9392 return FALSE;
9393
9394 /* Check indirect symbol. */
9395 while (h->root.type == bfd_link_hash_indirect)
9396 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9397
9398 switch (h->root.type)
9399 {
9400 default:
9401 abfd = NULL;
9402 break;
9403
9404 case bfd_link_hash_undefined:
9405 case bfd_link_hash_undefweak:
9406 abfd = h->root.u.undef.abfd;
9407 if (abfd == NULL
9408 || (abfd->flags & DYNAMIC) == 0
9409 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9410 return FALSE;
9411 break;
9412
9413 case bfd_link_hash_defined:
9414 case bfd_link_hash_defweak:
9415 abfd = h->root.u.def.section->owner;
9416 break;
9417
9418 case bfd_link_hash_common:
9419 abfd = h->root.u.c.p->section->owner;
9420 break;
9421 }
9422 BFD_ASSERT (abfd != NULL);
9423
9424 for (loaded = elf_hash_table (info)->loaded;
9425 loaded != NULL;
9426 loaded = loaded->next)
9427 {
9428 bfd *input;
9429 Elf_Internal_Shdr *hdr;
9430 size_t symcount;
9431 size_t extsymcount;
9432 size_t extsymoff;
9433 Elf_Internal_Shdr *versymhdr;
9434 Elf_Internal_Sym *isym;
9435 Elf_Internal_Sym *isymend;
9436 Elf_Internal_Sym *isymbuf;
9437 Elf_External_Versym *ever;
9438 Elf_External_Versym *extversym;
9439
9440 input = loaded->abfd;
9441
9442 /* We check each DSO for a possible hidden versioned definition. */
9443 if (input == abfd
9444 || (input->flags & DYNAMIC) == 0
9445 || elf_dynversym (input) == 0)
9446 continue;
9447
9448 hdr = &elf_tdata (input)->dynsymtab_hdr;
9449
9450 symcount = hdr->sh_size / bed->s->sizeof_sym;
9451 if (elf_bad_symtab (input))
9452 {
9453 extsymcount = symcount;
9454 extsymoff = 0;
9455 }
9456 else
9457 {
9458 extsymcount = symcount - hdr->sh_info;
9459 extsymoff = hdr->sh_info;
9460 }
9461
9462 if (extsymcount == 0)
9463 continue;
9464
9465 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9466 NULL, NULL, NULL);
9467 if (isymbuf == NULL)
9468 return FALSE;
9469
9470 /* Read in any version definitions. */
9471 versymhdr = &elf_tdata (input)->dynversym_hdr;
9472 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
9473 if (extversym == NULL)
9474 goto error_ret;
9475
9476 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9477 || (bfd_bread (extversym, versymhdr->sh_size, input)
9478 != versymhdr->sh_size))
9479 {
9480 free (extversym);
9481 error_ret:
9482 free (isymbuf);
9483 return FALSE;
9484 }
9485
9486 ever = extversym + extsymoff;
9487 isymend = isymbuf + extsymcount;
9488 for (isym = isymbuf; isym < isymend; isym++, ever++)
9489 {
9490 const char *name;
9491 Elf_Internal_Versym iver;
9492 unsigned short version_index;
9493
9494 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9495 || isym->st_shndx == SHN_UNDEF)
9496 continue;
9497
9498 name = bfd_elf_string_from_elf_section (input,
9499 hdr->sh_link,
9500 isym->st_name);
9501 if (strcmp (name, h->root.root.string) != 0)
9502 continue;
9503
9504 _bfd_elf_swap_versym_in (input, ever, &iver);
9505
9506 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9507 && !(h->def_regular
9508 && h->forced_local))
9509 {
9510 /* If we have a non-hidden versioned sym, then it should
9511 have provided a definition for the undefined sym unless
9512 it is defined in a non-shared object and forced local.
9513 */
9514 abort ();
9515 }
9516
9517 version_index = iver.vs_vers & VERSYM_VERSION;
9518 if (version_index == 1 || version_index == 2)
9519 {
9520 /* This is the base or first version. We can use it. */
9521 free (extversym);
9522 free (isymbuf);
9523 return TRUE;
9524 }
9525 }
9526
9527 free (extversym);
9528 free (isymbuf);
9529 }
9530
9531 return FALSE;
9532 }
9533
9534 /* Convert ELF common symbol TYPE. */
9535
9536 static int
9537 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9538 {
9539 /* Commom symbol can only appear in relocatable link. */
9540 if (!bfd_link_relocatable (info))
9541 abort ();
9542 switch (info->elf_stt_common)
9543 {
9544 case unchanged:
9545 break;
9546 case elf_stt_common:
9547 type = STT_COMMON;
9548 break;
9549 case no_elf_stt_common:
9550 type = STT_OBJECT;
9551 break;
9552 }
9553 return type;
9554 }
9555
9556 /* Add an external symbol to the symbol table. This is called from
9557 the hash table traversal routine. When generating a shared object,
9558 we go through the symbol table twice. The first time we output
9559 anything that might have been forced to local scope in a version
9560 script. The second time we output the symbols that are still
9561 global symbols. */
9562
9563 static bfd_boolean
9564 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9565 {
9566 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9567 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9568 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9569 bfd_boolean strip;
9570 Elf_Internal_Sym sym;
9571 asection *input_sec;
9572 const struct elf_backend_data *bed;
9573 long indx;
9574 int ret;
9575 unsigned int type;
9576
9577 if (h->root.type == bfd_link_hash_warning)
9578 {
9579 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9580 if (h->root.type == bfd_link_hash_new)
9581 return TRUE;
9582 }
9583
9584 /* Decide whether to output this symbol in this pass. */
9585 if (eoinfo->localsyms)
9586 {
9587 if (!h->forced_local)
9588 return TRUE;
9589 }
9590 else
9591 {
9592 if (h->forced_local)
9593 return TRUE;
9594 }
9595
9596 bed = get_elf_backend_data (flinfo->output_bfd);
9597
9598 if (h->root.type == bfd_link_hash_undefined)
9599 {
9600 /* If we have an undefined symbol reference here then it must have
9601 come from a shared library that is being linked in. (Undefined
9602 references in regular files have already been handled unless
9603 they are in unreferenced sections which are removed by garbage
9604 collection). */
9605 bfd_boolean ignore_undef = FALSE;
9606
9607 /* Some symbols may be special in that the fact that they're
9608 undefined can be safely ignored - let backend determine that. */
9609 if (bed->elf_backend_ignore_undef_symbol)
9610 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9611
9612 /* If we are reporting errors for this situation then do so now. */
9613 if (!ignore_undef
9614 && h->ref_dynamic
9615 && (!h->ref_regular || flinfo->info->gc_sections)
9616 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9617 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9618 (*flinfo->info->callbacks->undefined_symbol)
9619 (flinfo->info, h->root.root.string,
9620 h->ref_regular ? NULL : h->root.u.undef.abfd,
9621 NULL, 0,
9622 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
9623
9624 /* Strip a global symbol defined in a discarded section. */
9625 if (h->indx == -3)
9626 return TRUE;
9627 }
9628
9629 /* We should also warn if a forced local symbol is referenced from
9630 shared libraries. */
9631 if (bfd_link_executable (flinfo->info)
9632 && h->forced_local
9633 && h->ref_dynamic
9634 && h->def_regular
9635 && !h->dynamic_def
9636 && h->ref_dynamic_nonweak
9637 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9638 {
9639 bfd *def_bfd;
9640 const char *msg;
9641 struct elf_link_hash_entry *hi = h;
9642
9643 /* Check indirect symbol. */
9644 while (hi->root.type == bfd_link_hash_indirect)
9645 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9646
9647 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9648 /* xgettext:c-format */
9649 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9650 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9651 /* xgettext:c-format */
9652 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9653 else
9654 /* xgettext:c-format */
9655 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
9656 def_bfd = flinfo->output_bfd;
9657 if (hi->root.u.def.section != bfd_abs_section_ptr)
9658 def_bfd = hi->root.u.def.section->owner;
9659 _bfd_error_handler (msg, flinfo->output_bfd,
9660 h->root.root.string, def_bfd);
9661 bfd_set_error (bfd_error_bad_value);
9662 eoinfo->failed = TRUE;
9663 return FALSE;
9664 }
9665
9666 /* We don't want to output symbols that have never been mentioned by
9667 a regular file, or that we have been told to strip. However, if
9668 h->indx is set to -2, the symbol is used by a reloc and we must
9669 output it. */
9670 strip = FALSE;
9671 if (h->indx == -2)
9672 ;
9673 else if ((h->def_dynamic
9674 || h->ref_dynamic
9675 || h->root.type == bfd_link_hash_new)
9676 && !h->def_regular
9677 && !h->ref_regular)
9678 strip = TRUE;
9679 else if (flinfo->info->strip == strip_all)
9680 strip = TRUE;
9681 else if (flinfo->info->strip == strip_some
9682 && bfd_hash_lookup (flinfo->info->keep_hash,
9683 h->root.root.string, FALSE, FALSE) == NULL)
9684 strip = TRUE;
9685 else if ((h->root.type == bfd_link_hash_defined
9686 || h->root.type == bfd_link_hash_defweak)
9687 && ((flinfo->info->strip_discarded
9688 && discarded_section (h->root.u.def.section))
9689 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9690 && h->root.u.def.section->owner != NULL
9691 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9692 strip = TRUE;
9693 else if ((h->root.type == bfd_link_hash_undefined
9694 || h->root.type == bfd_link_hash_undefweak)
9695 && h->root.u.undef.abfd != NULL
9696 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9697 strip = TRUE;
9698
9699 type = h->type;
9700
9701 /* If we're stripping it, and it's not a dynamic symbol, there's
9702 nothing else to do. However, if it is a forced local symbol or
9703 an ifunc symbol we need to give the backend finish_dynamic_symbol
9704 function a chance to make it dynamic. */
9705 if (strip
9706 && h->dynindx == -1
9707 && type != STT_GNU_IFUNC
9708 && !h->forced_local)
9709 return TRUE;
9710
9711 sym.st_value = 0;
9712 sym.st_size = h->size;
9713 sym.st_other = h->other;
9714 switch (h->root.type)
9715 {
9716 default:
9717 case bfd_link_hash_new:
9718 case bfd_link_hash_warning:
9719 abort ();
9720 return FALSE;
9721
9722 case bfd_link_hash_undefined:
9723 case bfd_link_hash_undefweak:
9724 input_sec = bfd_und_section_ptr;
9725 sym.st_shndx = SHN_UNDEF;
9726 break;
9727
9728 case bfd_link_hash_defined:
9729 case bfd_link_hash_defweak:
9730 {
9731 input_sec = h->root.u.def.section;
9732 if (input_sec->output_section != NULL)
9733 {
9734 sym.st_shndx =
9735 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9736 input_sec->output_section);
9737 if (sym.st_shndx == SHN_BAD)
9738 {
9739 _bfd_error_handler
9740 /* xgettext:c-format */
9741 (_("%B: could not find output section %A for input section %A"),
9742 flinfo->output_bfd, input_sec->output_section, input_sec);
9743 bfd_set_error (bfd_error_nonrepresentable_section);
9744 eoinfo->failed = TRUE;
9745 return FALSE;
9746 }
9747
9748 /* ELF symbols in relocatable files are section relative,
9749 but in nonrelocatable files they are virtual
9750 addresses. */
9751 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9752 if (!bfd_link_relocatable (flinfo->info))
9753 {
9754 sym.st_value += input_sec->output_section->vma;
9755 if (h->type == STT_TLS)
9756 {
9757 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9758 if (tls_sec != NULL)
9759 sym.st_value -= tls_sec->vma;
9760 }
9761 }
9762 }
9763 else
9764 {
9765 BFD_ASSERT (input_sec->owner == NULL
9766 || (input_sec->owner->flags & DYNAMIC) != 0);
9767 sym.st_shndx = SHN_UNDEF;
9768 input_sec = bfd_und_section_ptr;
9769 }
9770 }
9771 break;
9772
9773 case bfd_link_hash_common:
9774 input_sec = h->root.u.c.p->section;
9775 sym.st_shndx = bed->common_section_index (input_sec);
9776 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9777 break;
9778
9779 case bfd_link_hash_indirect:
9780 /* These symbols are created by symbol versioning. They point
9781 to the decorated version of the name. For example, if the
9782 symbol foo@@GNU_1.2 is the default, which should be used when
9783 foo is used with no version, then we add an indirect symbol
9784 foo which points to foo@@GNU_1.2. We ignore these symbols,
9785 since the indirected symbol is already in the hash table. */
9786 return TRUE;
9787 }
9788
9789 if (type == STT_COMMON || type == STT_OBJECT)
9790 switch (h->root.type)
9791 {
9792 case bfd_link_hash_common:
9793 type = elf_link_convert_common_type (flinfo->info, type);
9794 break;
9795 case bfd_link_hash_defined:
9796 case bfd_link_hash_defweak:
9797 if (bed->common_definition (&sym))
9798 type = elf_link_convert_common_type (flinfo->info, type);
9799 else
9800 type = STT_OBJECT;
9801 break;
9802 case bfd_link_hash_undefined:
9803 case bfd_link_hash_undefweak:
9804 break;
9805 default:
9806 abort ();
9807 }
9808
9809 if (h->forced_local)
9810 {
9811 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9812 /* Turn off visibility on local symbol. */
9813 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9814 }
9815 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9816 else if (h->unique_global && h->def_regular)
9817 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9818 else if (h->root.type == bfd_link_hash_undefweak
9819 || h->root.type == bfd_link_hash_defweak)
9820 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9821 else
9822 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9823 sym.st_target_internal = h->target_internal;
9824
9825 /* Give the processor backend a chance to tweak the symbol value,
9826 and also to finish up anything that needs to be done for this
9827 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9828 forced local syms when non-shared is due to a historical quirk.
9829 STT_GNU_IFUNC symbol must go through PLT. */
9830 if ((h->type == STT_GNU_IFUNC
9831 && h->def_regular
9832 && !bfd_link_relocatable (flinfo->info))
9833 || ((h->dynindx != -1
9834 || h->forced_local)
9835 && ((bfd_link_pic (flinfo->info)
9836 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9837 || h->root.type != bfd_link_hash_undefweak))
9838 || !h->forced_local)
9839 && elf_hash_table (flinfo->info)->dynamic_sections_created))
9840 {
9841 if (! ((*bed->elf_backend_finish_dynamic_symbol)
9842 (flinfo->output_bfd, flinfo->info, h, &sym)))
9843 {
9844 eoinfo->failed = TRUE;
9845 return FALSE;
9846 }
9847 }
9848
9849 /* If we are marking the symbol as undefined, and there are no
9850 non-weak references to this symbol from a regular object, then
9851 mark the symbol as weak undefined; if there are non-weak
9852 references, mark the symbol as strong. We can't do this earlier,
9853 because it might not be marked as undefined until the
9854 finish_dynamic_symbol routine gets through with it. */
9855 if (sym.st_shndx == SHN_UNDEF
9856 && h->ref_regular
9857 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9858 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9859 {
9860 int bindtype;
9861 type = ELF_ST_TYPE (sym.st_info);
9862
9863 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9864 if (type == STT_GNU_IFUNC)
9865 type = STT_FUNC;
9866
9867 if (h->ref_regular_nonweak)
9868 bindtype = STB_GLOBAL;
9869 else
9870 bindtype = STB_WEAK;
9871 sym.st_info = ELF_ST_INFO (bindtype, type);
9872 }
9873
9874 /* If this is a symbol defined in a dynamic library, don't use the
9875 symbol size from the dynamic library. Relinking an executable
9876 against a new library may introduce gratuitous changes in the
9877 executable's symbols if we keep the size. */
9878 if (sym.st_shndx == SHN_UNDEF
9879 && !h->def_regular
9880 && h->def_dynamic)
9881 sym.st_size = 0;
9882
9883 /* If a non-weak symbol with non-default visibility is not defined
9884 locally, it is a fatal error. */
9885 if (!bfd_link_relocatable (flinfo->info)
9886 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9887 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9888 && h->root.type == bfd_link_hash_undefined
9889 && !h->def_regular)
9890 {
9891 const char *msg;
9892
9893 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9894 /* xgettext:c-format */
9895 msg = _("%B: protected symbol `%s' isn't defined");
9896 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9897 /* xgettext:c-format */
9898 msg = _("%B: internal symbol `%s' isn't defined");
9899 else
9900 /* xgettext:c-format */
9901 msg = _("%B: hidden symbol `%s' isn't defined");
9902 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
9903 bfd_set_error (bfd_error_bad_value);
9904 eoinfo->failed = TRUE;
9905 return FALSE;
9906 }
9907
9908 /* If this symbol should be put in the .dynsym section, then put it
9909 there now. We already know the symbol index. We also fill in
9910 the entry in the .hash section. */
9911 if (elf_hash_table (flinfo->info)->dynsym != NULL
9912 && h->dynindx != -1
9913 && elf_hash_table (flinfo->info)->dynamic_sections_created)
9914 {
9915 bfd_byte *esym;
9916
9917 /* Since there is no version information in the dynamic string,
9918 if there is no version info in symbol version section, we will
9919 have a run-time problem if not linking executable, referenced
9920 by shared library, or not bound locally. */
9921 if (h->verinfo.verdef == NULL
9922 && (!bfd_link_executable (flinfo->info)
9923 || h->ref_dynamic
9924 || !h->def_regular))
9925 {
9926 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9927
9928 if (p && p [1] != '\0')
9929 {
9930 _bfd_error_handler
9931 /* xgettext:c-format */
9932 (_("%B: No symbol version section for versioned symbol `%s'"),
9933 flinfo->output_bfd, h->root.root.string);
9934 eoinfo->failed = TRUE;
9935 return FALSE;
9936 }
9937 }
9938
9939 sym.st_name = h->dynstr_index;
9940 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9941 + h->dynindx * bed->s->sizeof_sym);
9942 if (!check_dynsym (flinfo->output_bfd, &sym))
9943 {
9944 eoinfo->failed = TRUE;
9945 return FALSE;
9946 }
9947 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
9948
9949 if (flinfo->hash_sec != NULL)
9950 {
9951 size_t hash_entry_size;
9952 bfd_byte *bucketpos;
9953 bfd_vma chain;
9954 size_t bucketcount;
9955 size_t bucket;
9956
9957 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
9958 bucket = h->u.elf_hash_value % bucketcount;
9959
9960 hash_entry_size
9961 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9962 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
9963 + (bucket + 2) * hash_entry_size);
9964 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9965 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9966 bucketpos);
9967 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9968 ((bfd_byte *) flinfo->hash_sec->contents
9969 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9970 }
9971
9972 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
9973 {
9974 Elf_Internal_Versym iversym;
9975 Elf_External_Versym *eversym;
9976
9977 if (!h->def_regular)
9978 {
9979 if (h->verinfo.verdef == NULL
9980 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9981 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
9982 iversym.vs_vers = 0;
9983 else
9984 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9985 }
9986 else
9987 {
9988 if (h->verinfo.vertree == NULL)
9989 iversym.vs_vers = 1;
9990 else
9991 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
9992 if (flinfo->info->create_default_symver)
9993 iversym.vs_vers++;
9994 }
9995
9996 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9997 defined locally. */
9998 if (h->versioned == versioned_hidden && h->def_regular)
9999 iversym.vs_vers |= VERSYM_HIDDEN;
10000
10001 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10002 eversym += h->dynindx;
10003 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10004 }
10005 }
10006
10007 /* If the symbol is undefined, and we didn't output it to .dynsym,
10008 strip it from .symtab too. Obviously we can't do this for
10009 relocatable output or when needed for --emit-relocs. */
10010 else if (input_sec == bfd_und_section_ptr
10011 && h->indx != -2
10012 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10013 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10014 && !bfd_link_relocatable (flinfo->info))
10015 return TRUE;
10016
10017 /* Also strip others that we couldn't earlier due to dynamic symbol
10018 processing. */
10019 if (strip)
10020 return TRUE;
10021 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10022 return TRUE;
10023
10024 /* Output a FILE symbol so that following locals are not associated
10025 with the wrong input file. We need one for forced local symbols
10026 if we've seen more than one FILE symbol or when we have exactly
10027 one FILE symbol but global symbols are present in a file other
10028 than the one with the FILE symbol. We also need one if linker
10029 defined symbols are present. In practice these conditions are
10030 always met, so just emit the FILE symbol unconditionally. */
10031 if (eoinfo->localsyms
10032 && !eoinfo->file_sym_done
10033 && eoinfo->flinfo->filesym_count != 0)
10034 {
10035 Elf_Internal_Sym fsym;
10036
10037 memset (&fsym, 0, sizeof (fsym));
10038 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10039 fsym.st_shndx = SHN_ABS;
10040 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10041 bfd_und_section_ptr, NULL))
10042 return FALSE;
10043
10044 eoinfo->file_sym_done = TRUE;
10045 }
10046
10047 indx = bfd_get_symcount (flinfo->output_bfd);
10048 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10049 input_sec, h);
10050 if (ret == 0)
10051 {
10052 eoinfo->failed = TRUE;
10053 return FALSE;
10054 }
10055 else if (ret == 1)
10056 h->indx = indx;
10057 else if (h->indx == -2)
10058 abort();
10059
10060 return TRUE;
10061 }
10062
10063 /* Return TRUE if special handling is done for relocs in SEC against
10064 symbols defined in discarded sections. */
10065
10066 static bfd_boolean
10067 elf_section_ignore_discarded_relocs (asection *sec)
10068 {
10069 const struct elf_backend_data *bed;
10070
10071 switch (sec->sec_info_type)
10072 {
10073 case SEC_INFO_TYPE_STABS:
10074 case SEC_INFO_TYPE_EH_FRAME:
10075 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10076 return TRUE;
10077 default:
10078 break;
10079 }
10080
10081 bed = get_elf_backend_data (sec->owner);
10082 if (bed->elf_backend_ignore_discarded_relocs != NULL
10083 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10084 return TRUE;
10085
10086 return FALSE;
10087 }
10088
10089 /* Return a mask saying how ld should treat relocations in SEC against
10090 symbols defined in discarded sections. If this function returns
10091 COMPLAIN set, ld will issue a warning message. If this function
10092 returns PRETEND set, and the discarded section was link-once and the
10093 same size as the kept link-once section, ld will pretend that the
10094 symbol was actually defined in the kept section. Otherwise ld will
10095 zero the reloc (at least that is the intent, but some cooperation by
10096 the target dependent code is needed, particularly for REL targets). */
10097
10098 unsigned int
10099 _bfd_elf_default_action_discarded (asection *sec)
10100 {
10101 if (sec->flags & SEC_DEBUGGING)
10102 return PRETEND;
10103
10104 if (strcmp (".eh_frame", sec->name) == 0)
10105 return 0;
10106
10107 if (strcmp (".gcc_except_table", sec->name) == 0)
10108 return 0;
10109
10110 return COMPLAIN | PRETEND;
10111 }
10112
10113 /* Find a match between a section and a member of a section group. */
10114
10115 static asection *
10116 match_group_member (asection *sec, asection *group,
10117 struct bfd_link_info *info)
10118 {
10119 asection *first = elf_next_in_group (group);
10120 asection *s = first;
10121
10122 while (s != NULL)
10123 {
10124 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10125 return s;
10126
10127 s = elf_next_in_group (s);
10128 if (s == first)
10129 break;
10130 }
10131
10132 return NULL;
10133 }
10134
10135 /* Check if the kept section of a discarded section SEC can be used
10136 to replace it. Return the replacement if it is OK. Otherwise return
10137 NULL. */
10138
10139 asection *
10140 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10141 {
10142 asection *kept;
10143
10144 kept = sec->kept_section;
10145 if (kept != NULL)
10146 {
10147 if ((kept->flags & SEC_GROUP) != 0)
10148 kept = match_group_member (sec, kept, info);
10149 if (kept != NULL
10150 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10151 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
10152 kept = NULL;
10153 sec->kept_section = kept;
10154 }
10155 return kept;
10156 }
10157
10158 /* Link an input file into the linker output file. This function
10159 handles all the sections and relocations of the input file at once.
10160 This is so that we only have to read the local symbols once, and
10161 don't have to keep them in memory. */
10162
10163 static bfd_boolean
10164 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10165 {
10166 int (*relocate_section)
10167 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10168 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10169 bfd *output_bfd;
10170 Elf_Internal_Shdr *symtab_hdr;
10171 size_t locsymcount;
10172 size_t extsymoff;
10173 Elf_Internal_Sym *isymbuf;
10174 Elf_Internal_Sym *isym;
10175 Elf_Internal_Sym *isymend;
10176 long *pindex;
10177 asection **ppsection;
10178 asection *o;
10179 const struct elf_backend_data *bed;
10180 struct elf_link_hash_entry **sym_hashes;
10181 bfd_size_type address_size;
10182 bfd_vma r_type_mask;
10183 int r_sym_shift;
10184 bfd_boolean have_file_sym = FALSE;
10185
10186 output_bfd = flinfo->output_bfd;
10187 bed = get_elf_backend_data (output_bfd);
10188 relocate_section = bed->elf_backend_relocate_section;
10189
10190 /* If this is a dynamic object, we don't want to do anything here:
10191 we don't want the local symbols, and we don't want the section
10192 contents. */
10193 if ((input_bfd->flags & DYNAMIC) != 0)
10194 return TRUE;
10195
10196 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10197 if (elf_bad_symtab (input_bfd))
10198 {
10199 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10200 extsymoff = 0;
10201 }
10202 else
10203 {
10204 locsymcount = symtab_hdr->sh_info;
10205 extsymoff = symtab_hdr->sh_info;
10206 }
10207
10208 /* Read the local symbols. */
10209 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10210 if (isymbuf == NULL && locsymcount != 0)
10211 {
10212 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10213 flinfo->internal_syms,
10214 flinfo->external_syms,
10215 flinfo->locsym_shndx);
10216 if (isymbuf == NULL)
10217 return FALSE;
10218 }
10219
10220 /* Find local symbol sections and adjust values of symbols in
10221 SEC_MERGE sections. Write out those local symbols we know are
10222 going into the output file. */
10223 isymend = isymbuf + locsymcount;
10224 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10225 isym < isymend;
10226 isym++, pindex++, ppsection++)
10227 {
10228 asection *isec;
10229 const char *name;
10230 Elf_Internal_Sym osym;
10231 long indx;
10232 int ret;
10233
10234 *pindex = -1;
10235
10236 if (elf_bad_symtab (input_bfd))
10237 {
10238 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10239 {
10240 *ppsection = NULL;
10241 continue;
10242 }
10243 }
10244
10245 if (isym->st_shndx == SHN_UNDEF)
10246 isec = bfd_und_section_ptr;
10247 else if (isym->st_shndx == SHN_ABS)
10248 isec = bfd_abs_section_ptr;
10249 else if (isym->st_shndx == SHN_COMMON)
10250 isec = bfd_com_section_ptr;
10251 else
10252 {
10253 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10254 if (isec == NULL)
10255 {
10256 /* Don't attempt to output symbols with st_shnx in the
10257 reserved range other than SHN_ABS and SHN_COMMON. */
10258 *ppsection = NULL;
10259 continue;
10260 }
10261 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10262 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10263 isym->st_value =
10264 _bfd_merged_section_offset (output_bfd, &isec,
10265 elf_section_data (isec)->sec_info,
10266 isym->st_value);
10267 }
10268
10269 *ppsection = isec;
10270
10271 /* Don't output the first, undefined, symbol. In fact, don't
10272 output any undefined local symbol. */
10273 if (isec == bfd_und_section_ptr)
10274 continue;
10275
10276 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10277 {
10278 /* We never output section symbols. Instead, we use the
10279 section symbol of the corresponding section in the output
10280 file. */
10281 continue;
10282 }
10283
10284 /* If we are stripping all symbols, we don't want to output this
10285 one. */
10286 if (flinfo->info->strip == strip_all)
10287 continue;
10288
10289 /* If we are discarding all local symbols, we don't want to
10290 output this one. If we are generating a relocatable output
10291 file, then some of the local symbols may be required by
10292 relocs; we output them below as we discover that they are
10293 needed. */
10294 if (flinfo->info->discard == discard_all)
10295 continue;
10296
10297 /* If this symbol is defined in a section which we are
10298 discarding, we don't need to keep it. */
10299 if (isym->st_shndx != SHN_UNDEF
10300 && isym->st_shndx < SHN_LORESERVE
10301 && bfd_section_removed_from_list (output_bfd,
10302 isec->output_section))
10303 continue;
10304
10305 /* Get the name of the symbol. */
10306 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10307 isym->st_name);
10308 if (name == NULL)
10309 return FALSE;
10310
10311 /* See if we are discarding symbols with this name. */
10312 if ((flinfo->info->strip == strip_some
10313 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
10314 == NULL))
10315 || (((flinfo->info->discard == discard_sec_merge
10316 && (isec->flags & SEC_MERGE)
10317 && !bfd_link_relocatable (flinfo->info))
10318 || flinfo->info->discard == discard_l)
10319 && bfd_is_local_label_name (input_bfd, name)))
10320 continue;
10321
10322 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10323 {
10324 if (input_bfd->lto_output)
10325 /* -flto puts a temp file name here. This means builds
10326 are not reproducible. Discard the symbol. */
10327 continue;
10328 have_file_sym = TRUE;
10329 flinfo->filesym_count += 1;
10330 }
10331 if (!have_file_sym)
10332 {
10333 /* In the absence of debug info, bfd_find_nearest_line uses
10334 FILE symbols to determine the source file for local
10335 function symbols. Provide a FILE symbol here if input
10336 files lack such, so that their symbols won't be
10337 associated with a previous input file. It's not the
10338 source file, but the best we can do. */
10339 have_file_sym = TRUE;
10340 flinfo->filesym_count += 1;
10341 memset (&osym, 0, sizeof (osym));
10342 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10343 osym.st_shndx = SHN_ABS;
10344 if (!elf_link_output_symstrtab (flinfo,
10345 (input_bfd->lto_output ? NULL
10346 : input_bfd->filename),
10347 &osym, bfd_abs_section_ptr,
10348 NULL))
10349 return FALSE;
10350 }
10351
10352 osym = *isym;
10353
10354 /* Adjust the section index for the output file. */
10355 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10356 isec->output_section);
10357 if (osym.st_shndx == SHN_BAD)
10358 return FALSE;
10359
10360 /* ELF symbols in relocatable files are section relative, but
10361 in executable files they are virtual addresses. Note that
10362 this code assumes that all ELF sections have an associated
10363 BFD section with a reasonable value for output_offset; below
10364 we assume that they also have a reasonable value for
10365 output_section. Any special sections must be set up to meet
10366 these requirements. */
10367 osym.st_value += isec->output_offset;
10368 if (!bfd_link_relocatable (flinfo->info))
10369 {
10370 osym.st_value += isec->output_section->vma;
10371 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10372 {
10373 /* STT_TLS symbols are relative to PT_TLS segment base. */
10374 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10375 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10376 }
10377 }
10378
10379 indx = bfd_get_symcount (output_bfd);
10380 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10381 if (ret == 0)
10382 return FALSE;
10383 else if (ret == 1)
10384 *pindex = indx;
10385 }
10386
10387 if (bed->s->arch_size == 32)
10388 {
10389 r_type_mask = 0xff;
10390 r_sym_shift = 8;
10391 address_size = 4;
10392 }
10393 else
10394 {
10395 r_type_mask = 0xffffffff;
10396 r_sym_shift = 32;
10397 address_size = 8;
10398 }
10399
10400 /* Relocate the contents of each section. */
10401 sym_hashes = elf_sym_hashes (input_bfd);
10402 for (o = input_bfd->sections; o != NULL; o = o->next)
10403 {
10404 bfd_byte *contents;
10405
10406 if (! o->linker_mark)
10407 {
10408 /* This section was omitted from the link. */
10409 continue;
10410 }
10411
10412 if (!flinfo->info->resolve_section_groups
10413 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10414 {
10415 /* Deal with the group signature symbol. */
10416 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10417 unsigned long symndx = sec_data->this_hdr.sh_info;
10418 asection *osec = o->output_section;
10419
10420 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
10421 if (symndx >= locsymcount
10422 || (elf_bad_symtab (input_bfd)
10423 && flinfo->sections[symndx] == NULL))
10424 {
10425 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10426 while (h->root.type == bfd_link_hash_indirect
10427 || h->root.type == bfd_link_hash_warning)
10428 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10429 /* Arrange for symbol to be output. */
10430 h->indx = -2;
10431 elf_section_data (osec)->this_hdr.sh_info = -2;
10432 }
10433 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10434 {
10435 /* We'll use the output section target_index. */
10436 asection *sec = flinfo->sections[symndx]->output_section;
10437 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10438 }
10439 else
10440 {
10441 if (flinfo->indices[symndx] == -1)
10442 {
10443 /* Otherwise output the local symbol now. */
10444 Elf_Internal_Sym sym = isymbuf[symndx];
10445 asection *sec = flinfo->sections[symndx]->output_section;
10446 const char *name;
10447 long indx;
10448 int ret;
10449
10450 name = bfd_elf_string_from_elf_section (input_bfd,
10451 symtab_hdr->sh_link,
10452 sym.st_name);
10453 if (name == NULL)
10454 return FALSE;
10455
10456 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10457 sec);
10458 if (sym.st_shndx == SHN_BAD)
10459 return FALSE;
10460
10461 sym.st_value += o->output_offset;
10462
10463 indx = bfd_get_symcount (output_bfd);
10464 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10465 NULL);
10466 if (ret == 0)
10467 return FALSE;
10468 else if (ret == 1)
10469 flinfo->indices[symndx] = indx;
10470 else
10471 abort ();
10472 }
10473 elf_section_data (osec)->this_hdr.sh_info
10474 = flinfo->indices[symndx];
10475 }
10476 }
10477
10478 if ((o->flags & SEC_HAS_CONTENTS) == 0
10479 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10480 continue;
10481
10482 if ((o->flags & SEC_LINKER_CREATED) != 0)
10483 {
10484 /* Section was created by _bfd_elf_link_create_dynamic_sections
10485 or somesuch. */
10486 continue;
10487 }
10488
10489 /* Get the contents of the section. They have been cached by a
10490 relaxation routine. Note that o is a section in an input
10491 file, so the contents field will not have been set by any of
10492 the routines which work on output files. */
10493 if (elf_section_data (o)->this_hdr.contents != NULL)
10494 {
10495 contents = elf_section_data (o)->this_hdr.contents;
10496 if (bed->caches_rawsize
10497 && o->rawsize != 0
10498 && o->rawsize < o->size)
10499 {
10500 memcpy (flinfo->contents, contents, o->rawsize);
10501 contents = flinfo->contents;
10502 }
10503 }
10504 else
10505 {
10506 contents = flinfo->contents;
10507 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10508 return FALSE;
10509 }
10510
10511 if ((o->flags & SEC_RELOC) != 0)
10512 {
10513 Elf_Internal_Rela *internal_relocs;
10514 Elf_Internal_Rela *rel, *relend;
10515 int action_discarded;
10516 int ret;
10517
10518 /* Get the swapped relocs. */
10519 internal_relocs
10520 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10521 flinfo->internal_relocs, FALSE);
10522 if (internal_relocs == NULL
10523 && o->reloc_count > 0)
10524 return FALSE;
10525
10526 /* We need to reverse-copy input .ctors/.dtors sections if
10527 they are placed in .init_array/.finit_array for output. */
10528 if (o->size > address_size
10529 && ((strncmp (o->name, ".ctors", 6) == 0
10530 && strcmp (o->output_section->name,
10531 ".init_array") == 0)
10532 || (strncmp (o->name, ".dtors", 6) == 0
10533 && strcmp (o->output_section->name,
10534 ".fini_array") == 0))
10535 && (o->name[6] == 0 || o->name[6] == '.'))
10536 {
10537 if (o->size * bed->s->int_rels_per_ext_rel
10538 != o->reloc_count * address_size)
10539 {
10540 _bfd_error_handler
10541 /* xgettext:c-format */
10542 (_("error: %B: size of section %A is not "
10543 "multiple of address size"),
10544 input_bfd, o);
10545 bfd_set_error (bfd_error_bad_value);
10546 return FALSE;
10547 }
10548 o->flags |= SEC_ELF_REVERSE_COPY;
10549 }
10550
10551 action_discarded = -1;
10552 if (!elf_section_ignore_discarded_relocs (o))
10553 action_discarded = (*bed->action_discarded) (o);
10554
10555 /* Run through the relocs evaluating complex reloc symbols and
10556 looking for relocs against symbols from discarded sections
10557 or section symbols from removed link-once sections.
10558 Complain about relocs against discarded sections. Zero
10559 relocs against removed link-once sections. */
10560
10561 rel = internal_relocs;
10562 relend = rel + o->reloc_count;
10563 for ( ; rel < relend; rel++)
10564 {
10565 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10566 unsigned int s_type;
10567 asection **ps, *sec;
10568 struct elf_link_hash_entry *h = NULL;
10569 const char *sym_name;
10570
10571 if (r_symndx == STN_UNDEF)
10572 continue;
10573
10574 if (r_symndx >= locsymcount
10575 || (elf_bad_symtab (input_bfd)
10576 && flinfo->sections[r_symndx] == NULL))
10577 {
10578 h = sym_hashes[r_symndx - extsymoff];
10579
10580 /* Badly formatted input files can contain relocs that
10581 reference non-existant symbols. Check here so that
10582 we do not seg fault. */
10583 if (h == NULL)
10584 {
10585 _bfd_error_handler
10586 /* xgettext:c-format */
10587 (_("error: %B contains a reloc (%#Lx) for section %A "
10588 "that references a non-existent global symbol"),
10589 input_bfd, rel->r_info, o);
10590 bfd_set_error (bfd_error_bad_value);
10591 return FALSE;
10592 }
10593
10594 while (h->root.type == bfd_link_hash_indirect
10595 || h->root.type == bfd_link_hash_warning)
10596 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10597
10598 s_type = h->type;
10599
10600 /* If a plugin symbol is referenced from a non-IR file,
10601 mark the symbol as undefined. Note that the
10602 linker may attach linker created dynamic sections
10603 to the plugin bfd. Symbols defined in linker
10604 created sections are not plugin symbols. */
10605 if ((h->root.non_ir_ref_regular
10606 || h->root.non_ir_ref_dynamic)
10607 && (h->root.type == bfd_link_hash_defined
10608 || h->root.type == bfd_link_hash_defweak)
10609 && (h->root.u.def.section->flags
10610 & SEC_LINKER_CREATED) == 0
10611 && h->root.u.def.section->owner != NULL
10612 && (h->root.u.def.section->owner->flags
10613 & BFD_PLUGIN) != 0)
10614 {
10615 h->root.type = bfd_link_hash_undefined;
10616 h->root.u.undef.abfd = h->root.u.def.section->owner;
10617 }
10618
10619 ps = NULL;
10620 if (h->root.type == bfd_link_hash_defined
10621 || h->root.type == bfd_link_hash_defweak)
10622 ps = &h->root.u.def.section;
10623
10624 sym_name = h->root.root.string;
10625 }
10626 else
10627 {
10628 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10629
10630 s_type = ELF_ST_TYPE (sym->st_info);
10631 ps = &flinfo->sections[r_symndx];
10632 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10633 sym, *ps);
10634 }
10635
10636 if ((s_type == STT_RELC || s_type == STT_SRELC)
10637 && !bfd_link_relocatable (flinfo->info))
10638 {
10639 bfd_vma val;
10640 bfd_vma dot = (rel->r_offset
10641 + o->output_offset + o->output_section->vma);
10642 #ifdef DEBUG
10643 printf ("Encountered a complex symbol!");
10644 printf (" (input_bfd %s, section %s, reloc %ld\n",
10645 input_bfd->filename, o->name,
10646 (long) (rel - internal_relocs));
10647 printf (" symbol: idx %8.8lx, name %s\n",
10648 r_symndx, sym_name);
10649 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10650 (unsigned long) rel->r_info,
10651 (unsigned long) rel->r_offset);
10652 #endif
10653 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10654 isymbuf, locsymcount, s_type == STT_SRELC))
10655 return FALSE;
10656
10657 /* Symbol evaluated OK. Update to absolute value. */
10658 set_symbol_value (input_bfd, isymbuf, locsymcount,
10659 r_symndx, val);
10660 continue;
10661 }
10662
10663 if (action_discarded != -1 && ps != NULL)
10664 {
10665 /* Complain if the definition comes from a
10666 discarded section. */
10667 if ((sec = *ps) != NULL && discarded_section (sec))
10668 {
10669 BFD_ASSERT (r_symndx != STN_UNDEF);
10670 if (action_discarded & COMPLAIN)
10671 (*flinfo->info->callbacks->einfo)
10672 /* xgettext:c-format */
10673 (_("%X`%s' referenced in section `%A' of %B: "
10674 "defined in discarded section `%A' of %B\n"),
10675 sym_name, o, input_bfd, sec, sec->owner);
10676
10677 /* Try to do the best we can to support buggy old
10678 versions of gcc. Pretend that the symbol is
10679 really defined in the kept linkonce section.
10680 FIXME: This is quite broken. Modifying the
10681 symbol here means we will be changing all later
10682 uses of the symbol, not just in this section. */
10683 if (action_discarded & PRETEND)
10684 {
10685 asection *kept;
10686
10687 kept = _bfd_elf_check_kept_section (sec,
10688 flinfo->info);
10689 if (kept != NULL)
10690 {
10691 *ps = kept;
10692 continue;
10693 }
10694 }
10695 }
10696 }
10697 }
10698
10699 /* Relocate the section by invoking a back end routine.
10700
10701 The back end routine is responsible for adjusting the
10702 section contents as necessary, and (if using Rela relocs
10703 and generating a relocatable output file) adjusting the
10704 reloc addend as necessary.
10705
10706 The back end routine does not have to worry about setting
10707 the reloc address or the reloc symbol index.
10708
10709 The back end routine is given a pointer to the swapped in
10710 internal symbols, and can access the hash table entries
10711 for the external symbols via elf_sym_hashes (input_bfd).
10712
10713 When generating relocatable output, the back end routine
10714 must handle STB_LOCAL/STT_SECTION symbols specially. The
10715 output symbol is going to be a section symbol
10716 corresponding to the output section, which will require
10717 the addend to be adjusted. */
10718
10719 ret = (*relocate_section) (output_bfd, flinfo->info,
10720 input_bfd, o, contents,
10721 internal_relocs,
10722 isymbuf,
10723 flinfo->sections);
10724 if (!ret)
10725 return FALSE;
10726
10727 if (ret == 2
10728 || bfd_link_relocatable (flinfo->info)
10729 || flinfo->info->emitrelocations)
10730 {
10731 Elf_Internal_Rela *irela;
10732 Elf_Internal_Rela *irelaend, *irelamid;
10733 bfd_vma last_offset;
10734 struct elf_link_hash_entry **rel_hash;
10735 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10736 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10737 unsigned int next_erel;
10738 bfd_boolean rela_normal;
10739 struct bfd_elf_section_data *esdi, *esdo;
10740
10741 esdi = elf_section_data (o);
10742 esdo = elf_section_data (o->output_section);
10743 rela_normal = FALSE;
10744
10745 /* Adjust the reloc addresses and symbol indices. */
10746
10747 irela = internal_relocs;
10748 irelaend = irela + o->reloc_count;
10749 rel_hash = esdo->rel.hashes + esdo->rel.count;
10750 /* We start processing the REL relocs, if any. When we reach
10751 IRELAMID in the loop, we switch to the RELA relocs. */
10752 irelamid = irela;
10753 if (esdi->rel.hdr != NULL)
10754 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10755 * bed->s->int_rels_per_ext_rel);
10756 rel_hash_list = rel_hash;
10757 rela_hash_list = NULL;
10758 last_offset = o->output_offset;
10759 if (!bfd_link_relocatable (flinfo->info))
10760 last_offset += o->output_section->vma;
10761 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10762 {
10763 unsigned long r_symndx;
10764 asection *sec;
10765 Elf_Internal_Sym sym;
10766
10767 if (next_erel == bed->s->int_rels_per_ext_rel)
10768 {
10769 rel_hash++;
10770 next_erel = 0;
10771 }
10772
10773 if (irela == irelamid)
10774 {
10775 rel_hash = esdo->rela.hashes + esdo->rela.count;
10776 rela_hash_list = rel_hash;
10777 rela_normal = bed->rela_normal;
10778 }
10779
10780 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10781 flinfo->info, o,
10782 irela->r_offset);
10783 if (irela->r_offset >= (bfd_vma) -2)
10784 {
10785 /* This is a reloc for a deleted entry or somesuch.
10786 Turn it into an R_*_NONE reloc, at the same
10787 offset as the last reloc. elf_eh_frame.c and
10788 bfd_elf_discard_info rely on reloc offsets
10789 being ordered. */
10790 irela->r_offset = last_offset;
10791 irela->r_info = 0;
10792 irela->r_addend = 0;
10793 continue;
10794 }
10795
10796 irela->r_offset += o->output_offset;
10797
10798 /* Relocs in an executable have to be virtual addresses. */
10799 if (!bfd_link_relocatable (flinfo->info))
10800 irela->r_offset += o->output_section->vma;
10801
10802 last_offset = irela->r_offset;
10803
10804 r_symndx = irela->r_info >> r_sym_shift;
10805 if (r_symndx == STN_UNDEF)
10806 continue;
10807
10808 if (r_symndx >= locsymcount
10809 || (elf_bad_symtab (input_bfd)
10810 && flinfo->sections[r_symndx] == NULL))
10811 {
10812 struct elf_link_hash_entry *rh;
10813 unsigned long indx;
10814
10815 /* This is a reloc against a global symbol. We
10816 have not yet output all the local symbols, so
10817 we do not know the symbol index of any global
10818 symbol. We set the rel_hash entry for this
10819 reloc to point to the global hash table entry
10820 for this symbol. The symbol index is then
10821 set at the end of bfd_elf_final_link. */
10822 indx = r_symndx - extsymoff;
10823 rh = elf_sym_hashes (input_bfd)[indx];
10824 while (rh->root.type == bfd_link_hash_indirect
10825 || rh->root.type == bfd_link_hash_warning)
10826 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10827
10828 /* Setting the index to -2 tells
10829 elf_link_output_extsym that this symbol is
10830 used by a reloc. */
10831 BFD_ASSERT (rh->indx < 0);
10832 rh->indx = -2;
10833 *rel_hash = rh;
10834
10835 continue;
10836 }
10837
10838 /* This is a reloc against a local symbol. */
10839
10840 *rel_hash = NULL;
10841 sym = isymbuf[r_symndx];
10842 sec = flinfo->sections[r_symndx];
10843 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10844 {
10845 /* I suppose the backend ought to fill in the
10846 section of any STT_SECTION symbol against a
10847 processor specific section. */
10848 r_symndx = STN_UNDEF;
10849 if (bfd_is_abs_section (sec))
10850 ;
10851 else if (sec == NULL || sec->owner == NULL)
10852 {
10853 bfd_set_error (bfd_error_bad_value);
10854 return FALSE;
10855 }
10856 else
10857 {
10858 asection *osec = sec->output_section;
10859
10860 /* If we have discarded a section, the output
10861 section will be the absolute section. In
10862 case of discarded SEC_MERGE sections, use
10863 the kept section. relocate_section should
10864 have already handled discarded linkonce
10865 sections. */
10866 if (bfd_is_abs_section (osec)
10867 && sec->kept_section != NULL
10868 && sec->kept_section->output_section != NULL)
10869 {
10870 osec = sec->kept_section->output_section;
10871 irela->r_addend -= osec->vma;
10872 }
10873
10874 if (!bfd_is_abs_section (osec))
10875 {
10876 r_symndx = osec->target_index;
10877 if (r_symndx == STN_UNDEF)
10878 {
10879 irela->r_addend += osec->vma;
10880 osec = _bfd_nearby_section (output_bfd, osec,
10881 osec->vma);
10882 irela->r_addend -= osec->vma;
10883 r_symndx = osec->target_index;
10884 }
10885 }
10886 }
10887
10888 /* Adjust the addend according to where the
10889 section winds up in the output section. */
10890 if (rela_normal)
10891 irela->r_addend += sec->output_offset;
10892 }
10893 else
10894 {
10895 if (flinfo->indices[r_symndx] == -1)
10896 {
10897 unsigned long shlink;
10898 const char *name;
10899 asection *osec;
10900 long indx;
10901
10902 if (flinfo->info->strip == strip_all)
10903 {
10904 /* You can't do ld -r -s. */
10905 bfd_set_error (bfd_error_invalid_operation);
10906 return FALSE;
10907 }
10908
10909 /* This symbol was skipped earlier, but
10910 since it is needed by a reloc, we
10911 must output it now. */
10912 shlink = symtab_hdr->sh_link;
10913 name = (bfd_elf_string_from_elf_section
10914 (input_bfd, shlink, sym.st_name));
10915 if (name == NULL)
10916 return FALSE;
10917
10918 osec = sec->output_section;
10919 sym.st_shndx =
10920 _bfd_elf_section_from_bfd_section (output_bfd,
10921 osec);
10922 if (sym.st_shndx == SHN_BAD)
10923 return FALSE;
10924
10925 sym.st_value += sec->output_offset;
10926 if (!bfd_link_relocatable (flinfo->info))
10927 {
10928 sym.st_value += osec->vma;
10929 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10930 {
10931 /* STT_TLS symbols are relative to PT_TLS
10932 segment base. */
10933 BFD_ASSERT (elf_hash_table (flinfo->info)
10934 ->tls_sec != NULL);
10935 sym.st_value -= (elf_hash_table (flinfo->info)
10936 ->tls_sec->vma);
10937 }
10938 }
10939
10940 indx = bfd_get_symcount (output_bfd);
10941 ret = elf_link_output_symstrtab (flinfo, name,
10942 &sym, sec,
10943 NULL);
10944 if (ret == 0)
10945 return FALSE;
10946 else if (ret == 1)
10947 flinfo->indices[r_symndx] = indx;
10948 else
10949 abort ();
10950 }
10951
10952 r_symndx = flinfo->indices[r_symndx];
10953 }
10954
10955 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10956 | (irela->r_info & r_type_mask));
10957 }
10958
10959 /* Swap out the relocs. */
10960 input_rel_hdr = esdi->rel.hdr;
10961 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
10962 {
10963 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10964 input_rel_hdr,
10965 internal_relocs,
10966 rel_hash_list))
10967 return FALSE;
10968 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10969 * bed->s->int_rels_per_ext_rel);
10970 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
10971 }
10972
10973 input_rela_hdr = esdi->rela.hdr;
10974 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10975 {
10976 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10977 input_rela_hdr,
10978 internal_relocs,
10979 rela_hash_list))
10980 return FALSE;
10981 }
10982 }
10983 }
10984
10985 /* Write out the modified section contents. */
10986 if (bed->elf_backend_write_section
10987 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
10988 contents))
10989 {
10990 /* Section written out. */
10991 }
10992 else switch (o->sec_info_type)
10993 {
10994 case SEC_INFO_TYPE_STABS:
10995 if (! (_bfd_write_section_stabs
10996 (output_bfd,
10997 &elf_hash_table (flinfo->info)->stab_info,
10998 o, &elf_section_data (o)->sec_info, contents)))
10999 return FALSE;
11000 break;
11001 case SEC_INFO_TYPE_MERGE:
11002 if (! _bfd_write_merged_section (output_bfd, o,
11003 elf_section_data (o)->sec_info))
11004 return FALSE;
11005 break;
11006 case SEC_INFO_TYPE_EH_FRAME:
11007 {
11008 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11009 o, contents))
11010 return FALSE;
11011 }
11012 break;
11013 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11014 {
11015 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11016 flinfo->info,
11017 o, contents))
11018 return FALSE;
11019 }
11020 break;
11021 default:
11022 {
11023 if (! (o->flags & SEC_EXCLUDE))
11024 {
11025 file_ptr offset = (file_ptr) o->output_offset;
11026 bfd_size_type todo = o->size;
11027
11028 offset *= bfd_octets_per_byte (output_bfd);
11029
11030 if ((o->flags & SEC_ELF_REVERSE_COPY))
11031 {
11032 /* Reverse-copy input section to output. */
11033 do
11034 {
11035 todo -= address_size;
11036 if (! bfd_set_section_contents (output_bfd,
11037 o->output_section,
11038 contents + todo,
11039 offset,
11040 address_size))
11041 return FALSE;
11042 if (todo == 0)
11043 break;
11044 offset += address_size;
11045 }
11046 while (1);
11047 }
11048 else if (! bfd_set_section_contents (output_bfd,
11049 o->output_section,
11050 contents,
11051 offset, todo))
11052 return FALSE;
11053 }
11054 }
11055 break;
11056 }
11057 }
11058
11059 return TRUE;
11060 }
11061
11062 /* Generate a reloc when linking an ELF file. This is a reloc
11063 requested by the linker, and does not come from any input file. This
11064 is used to build constructor and destructor tables when linking
11065 with -Ur. */
11066
11067 static bfd_boolean
11068 elf_reloc_link_order (bfd *output_bfd,
11069 struct bfd_link_info *info,
11070 asection *output_section,
11071 struct bfd_link_order *link_order)
11072 {
11073 reloc_howto_type *howto;
11074 long indx;
11075 bfd_vma offset;
11076 bfd_vma addend;
11077 struct bfd_elf_section_reloc_data *reldata;
11078 struct elf_link_hash_entry **rel_hash_ptr;
11079 Elf_Internal_Shdr *rel_hdr;
11080 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11081 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11082 bfd_byte *erel;
11083 unsigned int i;
11084 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11085
11086 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11087 if (howto == NULL)
11088 {
11089 bfd_set_error (bfd_error_bad_value);
11090 return FALSE;
11091 }
11092
11093 addend = link_order->u.reloc.p->addend;
11094
11095 if (esdo->rel.hdr)
11096 reldata = &esdo->rel;
11097 else if (esdo->rela.hdr)
11098 reldata = &esdo->rela;
11099 else
11100 {
11101 reldata = NULL;
11102 BFD_ASSERT (0);
11103 }
11104
11105 /* Figure out the symbol index. */
11106 rel_hash_ptr = reldata->hashes + reldata->count;
11107 if (link_order->type == bfd_section_reloc_link_order)
11108 {
11109 indx = link_order->u.reloc.p->u.section->target_index;
11110 BFD_ASSERT (indx != 0);
11111 *rel_hash_ptr = NULL;
11112 }
11113 else
11114 {
11115 struct elf_link_hash_entry *h;
11116
11117 /* Treat a reloc against a defined symbol as though it were
11118 actually against the section. */
11119 h = ((struct elf_link_hash_entry *)
11120 bfd_wrapped_link_hash_lookup (output_bfd, info,
11121 link_order->u.reloc.p->u.name,
11122 FALSE, FALSE, TRUE));
11123 if (h != NULL
11124 && (h->root.type == bfd_link_hash_defined
11125 || h->root.type == bfd_link_hash_defweak))
11126 {
11127 asection *section;
11128
11129 section = h->root.u.def.section;
11130 indx = section->output_section->target_index;
11131 *rel_hash_ptr = NULL;
11132 /* It seems that we ought to add the symbol value to the
11133 addend here, but in practice it has already been added
11134 because it was passed to constructor_callback. */
11135 addend += section->output_section->vma + section->output_offset;
11136 }
11137 else if (h != NULL)
11138 {
11139 /* Setting the index to -2 tells elf_link_output_extsym that
11140 this symbol is used by a reloc. */
11141 h->indx = -2;
11142 *rel_hash_ptr = h;
11143 indx = 0;
11144 }
11145 else
11146 {
11147 (*info->callbacks->unattached_reloc)
11148 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11149 indx = 0;
11150 }
11151 }
11152
11153 /* If this is an inplace reloc, we must write the addend into the
11154 object file. */
11155 if (howto->partial_inplace && addend != 0)
11156 {
11157 bfd_size_type size;
11158 bfd_reloc_status_type rstat;
11159 bfd_byte *buf;
11160 bfd_boolean ok;
11161 const char *sym_name;
11162
11163 size = (bfd_size_type) bfd_get_reloc_size (howto);
11164 buf = (bfd_byte *) bfd_zmalloc (size);
11165 if (buf == NULL && size != 0)
11166 return FALSE;
11167 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11168 switch (rstat)
11169 {
11170 case bfd_reloc_ok:
11171 break;
11172
11173 default:
11174 case bfd_reloc_outofrange:
11175 abort ();
11176
11177 case bfd_reloc_overflow:
11178 if (link_order->type == bfd_section_reloc_link_order)
11179 sym_name = bfd_section_name (output_bfd,
11180 link_order->u.reloc.p->u.section);
11181 else
11182 sym_name = link_order->u.reloc.p->u.name;
11183 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11184 howto->name, addend, NULL, NULL,
11185 (bfd_vma) 0);
11186 break;
11187 }
11188
11189 ok = bfd_set_section_contents (output_bfd, output_section, buf,
11190 link_order->offset
11191 * bfd_octets_per_byte (output_bfd),
11192 size);
11193 free (buf);
11194 if (! ok)
11195 return FALSE;
11196 }
11197
11198 /* The address of a reloc is relative to the section in a
11199 relocatable file, and is a virtual address in an executable
11200 file. */
11201 offset = link_order->offset;
11202 if (! bfd_link_relocatable (info))
11203 offset += output_section->vma;
11204
11205 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11206 {
11207 irel[i].r_offset = offset;
11208 irel[i].r_info = 0;
11209 irel[i].r_addend = 0;
11210 }
11211 if (bed->s->arch_size == 32)
11212 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11213 else
11214 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11215
11216 rel_hdr = reldata->hdr;
11217 erel = rel_hdr->contents;
11218 if (rel_hdr->sh_type == SHT_REL)
11219 {
11220 erel += reldata->count * bed->s->sizeof_rel;
11221 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11222 }
11223 else
11224 {
11225 irel[0].r_addend = addend;
11226 erel += reldata->count * bed->s->sizeof_rela;
11227 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11228 }
11229
11230 ++reldata->count;
11231
11232 return TRUE;
11233 }
11234
11235
11236 /* Get the output vma of the section pointed to by the sh_link field. */
11237
11238 static bfd_vma
11239 elf_get_linked_section_vma (struct bfd_link_order *p)
11240 {
11241 Elf_Internal_Shdr **elf_shdrp;
11242 asection *s;
11243 int elfsec;
11244
11245 s = p->u.indirect.section;
11246 elf_shdrp = elf_elfsections (s->owner);
11247 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
11248 elfsec = elf_shdrp[elfsec]->sh_link;
11249 /* PR 290:
11250 The Intel C compiler generates SHT_IA_64_UNWIND with
11251 SHF_LINK_ORDER. But it doesn't set the sh_link or
11252 sh_info fields. Hence we could get the situation
11253 where elfsec is 0. */
11254 if (elfsec == 0)
11255 {
11256 const struct elf_backend_data *bed
11257 = get_elf_backend_data (s->owner);
11258 if (bed->link_order_error_handler)
11259 bed->link_order_error_handler
11260 /* xgettext:c-format */
11261 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
11262 return 0;
11263 }
11264 else
11265 {
11266 s = elf_shdrp[elfsec]->bfd_section;
11267 return s->output_section->vma + s->output_offset;
11268 }
11269 }
11270
11271
11272 /* Compare two sections based on the locations of the sections they are
11273 linked to. Used by elf_fixup_link_order. */
11274
11275 static int
11276 compare_link_order (const void * a, const void * b)
11277 {
11278 bfd_vma apos;
11279 bfd_vma bpos;
11280
11281 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11282 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11283 if (apos < bpos)
11284 return -1;
11285 return apos > bpos;
11286 }
11287
11288
11289 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11290 order as their linked sections. Returns false if this could not be done
11291 because an output section includes both ordered and unordered
11292 sections. Ideally we'd do this in the linker proper. */
11293
11294 static bfd_boolean
11295 elf_fixup_link_order (bfd *abfd, asection *o)
11296 {
11297 int seen_linkorder;
11298 int seen_other;
11299 int n;
11300 struct bfd_link_order *p;
11301 bfd *sub;
11302 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11303 unsigned elfsec;
11304 struct bfd_link_order **sections;
11305 asection *s, *other_sec, *linkorder_sec;
11306 bfd_vma offset;
11307
11308 other_sec = NULL;
11309 linkorder_sec = NULL;
11310 seen_other = 0;
11311 seen_linkorder = 0;
11312 for (p = o->map_head.link_order; p != NULL; p = p->next)
11313 {
11314 if (p->type == bfd_indirect_link_order)
11315 {
11316 s = p->u.indirect.section;
11317 sub = s->owner;
11318 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11319 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
11320 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11321 && elfsec < elf_numsections (sub)
11322 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11323 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
11324 {
11325 seen_linkorder++;
11326 linkorder_sec = s;
11327 }
11328 else
11329 {
11330 seen_other++;
11331 other_sec = s;
11332 }
11333 }
11334 else
11335 seen_other++;
11336
11337 if (seen_other && seen_linkorder)
11338 {
11339 if (other_sec && linkorder_sec)
11340 _bfd_error_handler
11341 /* xgettext:c-format */
11342 (_("%A has both ordered [`%A' in %B] "
11343 "and unordered [`%A' in %B] sections"),
11344 o, linkorder_sec, linkorder_sec->owner,
11345 other_sec, other_sec->owner);
11346 else
11347 _bfd_error_handler
11348 (_("%A has both ordered and unordered sections"), o);
11349 bfd_set_error (bfd_error_bad_value);
11350 return FALSE;
11351 }
11352 }
11353
11354 if (!seen_linkorder)
11355 return TRUE;
11356
11357 sections = (struct bfd_link_order **)
11358 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11359 if (sections == NULL)
11360 return FALSE;
11361 seen_linkorder = 0;
11362
11363 for (p = o->map_head.link_order; p != NULL; p = p->next)
11364 {
11365 sections[seen_linkorder++] = p;
11366 }
11367 /* Sort the input sections in the order of their linked section. */
11368 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11369 compare_link_order);
11370
11371 /* Change the offsets of the sections. */
11372 offset = 0;
11373 for (n = 0; n < seen_linkorder; n++)
11374 {
11375 s = sections[n]->u.indirect.section;
11376 offset &= ~(bfd_vma) 0 << s->alignment_power;
11377 s->output_offset = offset / bfd_octets_per_byte (abfd);
11378 sections[n]->offset = offset;
11379 offset += sections[n]->size;
11380 }
11381
11382 free (sections);
11383 return TRUE;
11384 }
11385
11386 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11387 Returns TRUE upon success, FALSE otherwise. */
11388
11389 static bfd_boolean
11390 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11391 {
11392 bfd_boolean ret = FALSE;
11393 bfd *implib_bfd;
11394 const struct elf_backend_data *bed;
11395 flagword flags;
11396 enum bfd_architecture arch;
11397 unsigned int mach;
11398 asymbol **sympp = NULL;
11399 long symsize;
11400 long symcount;
11401 long src_count;
11402 elf_symbol_type *osymbuf;
11403
11404 implib_bfd = info->out_implib_bfd;
11405 bed = get_elf_backend_data (abfd);
11406
11407 if (!bfd_set_format (implib_bfd, bfd_object))
11408 return FALSE;
11409
11410 /* Use flag from executable but make it a relocatable object. */
11411 flags = bfd_get_file_flags (abfd);
11412 flags &= ~HAS_RELOC;
11413 if (!bfd_set_start_address (implib_bfd, 0)
11414 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
11415 return FALSE;
11416
11417 /* Copy architecture of output file to import library file. */
11418 arch = bfd_get_arch (abfd);
11419 mach = bfd_get_mach (abfd);
11420 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11421 && (abfd->target_defaulted
11422 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11423 return FALSE;
11424
11425 /* Get symbol table size. */
11426 symsize = bfd_get_symtab_upper_bound (abfd);
11427 if (symsize < 0)
11428 return FALSE;
11429
11430 /* Read in the symbol table. */
11431 sympp = (asymbol **) xmalloc (symsize);
11432 symcount = bfd_canonicalize_symtab (abfd, sympp);
11433 if (symcount < 0)
11434 goto free_sym_buf;
11435
11436 /* Allow the BFD backend to copy any private header data it
11437 understands from the output BFD to the import library BFD. */
11438 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11439 goto free_sym_buf;
11440
11441 /* Filter symbols to appear in the import library. */
11442 if (bed->elf_backend_filter_implib_symbols)
11443 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11444 symcount);
11445 else
11446 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11447 if (symcount == 0)
11448 {
11449 bfd_set_error (bfd_error_no_symbols);
11450 _bfd_error_handler (_("%B: no symbol found for import library"),
11451 implib_bfd);
11452 goto free_sym_buf;
11453 }
11454
11455
11456 /* Make symbols absolute. */
11457 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11458 sizeof (*osymbuf));
11459 for (src_count = 0; src_count < symcount; src_count++)
11460 {
11461 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11462 sizeof (*osymbuf));
11463 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11464 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11465 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11466 osymbuf[src_count].internal_elf_sym.st_value =
11467 osymbuf[src_count].symbol.value;
11468 sympp[src_count] = &osymbuf[src_count].symbol;
11469 }
11470
11471 bfd_set_symtab (implib_bfd, sympp, symcount);
11472
11473 /* Allow the BFD backend to copy any private data it understands
11474 from the output BFD to the import library BFD. This is done last
11475 to permit the routine to look at the filtered symbol table. */
11476 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11477 goto free_sym_buf;
11478
11479 if (!bfd_close (implib_bfd))
11480 goto free_sym_buf;
11481
11482 ret = TRUE;
11483
11484 free_sym_buf:
11485 free (sympp);
11486 return ret;
11487 }
11488
11489 static void
11490 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11491 {
11492 asection *o;
11493
11494 if (flinfo->symstrtab != NULL)
11495 _bfd_elf_strtab_free (flinfo->symstrtab);
11496 if (flinfo->contents != NULL)
11497 free (flinfo->contents);
11498 if (flinfo->external_relocs != NULL)
11499 free (flinfo->external_relocs);
11500 if (flinfo->internal_relocs != NULL)
11501 free (flinfo->internal_relocs);
11502 if (flinfo->external_syms != NULL)
11503 free (flinfo->external_syms);
11504 if (flinfo->locsym_shndx != NULL)
11505 free (flinfo->locsym_shndx);
11506 if (flinfo->internal_syms != NULL)
11507 free (flinfo->internal_syms);
11508 if (flinfo->indices != NULL)
11509 free (flinfo->indices);
11510 if (flinfo->sections != NULL)
11511 free (flinfo->sections);
11512 if (flinfo->symshndxbuf != NULL)
11513 free (flinfo->symshndxbuf);
11514 for (o = obfd->sections; o != NULL; o = o->next)
11515 {
11516 struct bfd_elf_section_data *esdo = elf_section_data (o);
11517 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11518 free (esdo->rel.hashes);
11519 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11520 free (esdo->rela.hashes);
11521 }
11522 }
11523
11524 /* Do the final step of an ELF link. */
11525
11526 bfd_boolean
11527 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11528 {
11529 bfd_boolean dynamic;
11530 bfd_boolean emit_relocs;
11531 bfd *dynobj;
11532 struct elf_final_link_info flinfo;
11533 asection *o;
11534 struct bfd_link_order *p;
11535 bfd *sub;
11536 bfd_size_type max_contents_size;
11537 bfd_size_type max_external_reloc_size;
11538 bfd_size_type max_internal_reloc_count;
11539 bfd_size_type max_sym_count;
11540 bfd_size_type max_sym_shndx_count;
11541 Elf_Internal_Sym elfsym;
11542 unsigned int i;
11543 Elf_Internal_Shdr *symtab_hdr;
11544 Elf_Internal_Shdr *symtab_shndx_hdr;
11545 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11546 struct elf_outext_info eoinfo;
11547 bfd_boolean merged;
11548 size_t relativecount = 0;
11549 asection *reldyn = 0;
11550 bfd_size_type amt;
11551 asection *attr_section = NULL;
11552 bfd_vma attr_size = 0;
11553 const char *std_attrs_section;
11554 struct elf_link_hash_table *htab = elf_hash_table (info);
11555
11556 if (!is_elf_hash_table (htab))
11557 return FALSE;
11558
11559 if (bfd_link_pic (info))
11560 abfd->flags |= DYNAMIC;
11561
11562 dynamic = htab->dynamic_sections_created;
11563 dynobj = htab->dynobj;
11564
11565 emit_relocs = (bfd_link_relocatable (info)
11566 || info->emitrelocations);
11567
11568 flinfo.info = info;
11569 flinfo.output_bfd = abfd;
11570 flinfo.symstrtab = _bfd_elf_strtab_init ();
11571 if (flinfo.symstrtab == NULL)
11572 return FALSE;
11573
11574 if (! dynamic)
11575 {
11576 flinfo.hash_sec = NULL;
11577 flinfo.symver_sec = NULL;
11578 }
11579 else
11580 {
11581 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11582 /* Note that dynsym_sec can be NULL (on VMS). */
11583 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11584 /* Note that it is OK if symver_sec is NULL. */
11585 }
11586
11587 flinfo.contents = NULL;
11588 flinfo.external_relocs = NULL;
11589 flinfo.internal_relocs = NULL;
11590 flinfo.external_syms = NULL;
11591 flinfo.locsym_shndx = NULL;
11592 flinfo.internal_syms = NULL;
11593 flinfo.indices = NULL;
11594 flinfo.sections = NULL;
11595 flinfo.symshndxbuf = NULL;
11596 flinfo.filesym_count = 0;
11597
11598 /* The object attributes have been merged. Remove the input
11599 sections from the link, and set the contents of the output
11600 secton. */
11601 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11602 for (o = abfd->sections; o != NULL; o = o->next)
11603 {
11604 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11605 || strcmp (o->name, ".gnu.attributes") == 0)
11606 {
11607 for (p = o->map_head.link_order; p != NULL; p = p->next)
11608 {
11609 asection *input_section;
11610
11611 if (p->type != bfd_indirect_link_order)
11612 continue;
11613 input_section = p->u.indirect.section;
11614 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11615 elf_link_input_bfd ignores this section. */
11616 input_section->flags &= ~SEC_HAS_CONTENTS;
11617 }
11618
11619 attr_size = bfd_elf_obj_attr_size (abfd);
11620 if (attr_size)
11621 {
11622 bfd_set_section_size (abfd, o, attr_size);
11623 attr_section = o;
11624 /* Skip this section later on. */
11625 o->map_head.link_order = NULL;
11626 }
11627 else
11628 o->flags |= SEC_EXCLUDE;
11629 }
11630 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
11631 {
11632 /* Remove empty group section from linker output. */
11633 o->flags |= SEC_EXCLUDE;
11634 bfd_section_list_remove (abfd, o);
11635 abfd->section_count--;
11636 }
11637 }
11638
11639 /* Count up the number of relocations we will output for each output
11640 section, so that we know the sizes of the reloc sections. We
11641 also figure out some maximum sizes. */
11642 max_contents_size = 0;
11643 max_external_reloc_size = 0;
11644 max_internal_reloc_count = 0;
11645 max_sym_count = 0;
11646 max_sym_shndx_count = 0;
11647 merged = FALSE;
11648 for (o = abfd->sections; o != NULL; o = o->next)
11649 {
11650 struct bfd_elf_section_data *esdo = elf_section_data (o);
11651 o->reloc_count = 0;
11652
11653 for (p = o->map_head.link_order; p != NULL; p = p->next)
11654 {
11655 unsigned int reloc_count = 0;
11656 unsigned int additional_reloc_count = 0;
11657 struct bfd_elf_section_data *esdi = NULL;
11658
11659 if (p->type == bfd_section_reloc_link_order
11660 || p->type == bfd_symbol_reloc_link_order)
11661 reloc_count = 1;
11662 else if (p->type == bfd_indirect_link_order)
11663 {
11664 asection *sec;
11665
11666 sec = p->u.indirect.section;
11667
11668 /* Mark all sections which are to be included in the
11669 link. This will normally be every section. We need
11670 to do this so that we can identify any sections which
11671 the linker has decided to not include. */
11672 sec->linker_mark = TRUE;
11673
11674 if (sec->flags & SEC_MERGE)
11675 merged = TRUE;
11676
11677 if (sec->rawsize > max_contents_size)
11678 max_contents_size = sec->rawsize;
11679 if (sec->size > max_contents_size)
11680 max_contents_size = sec->size;
11681
11682 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11683 && (sec->owner->flags & DYNAMIC) == 0)
11684 {
11685 size_t sym_count;
11686
11687 /* We are interested in just local symbols, not all
11688 symbols. */
11689 if (elf_bad_symtab (sec->owner))
11690 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11691 / bed->s->sizeof_sym);
11692 else
11693 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11694
11695 if (sym_count > max_sym_count)
11696 max_sym_count = sym_count;
11697
11698 if (sym_count > max_sym_shndx_count
11699 && elf_symtab_shndx_list (sec->owner) != NULL)
11700 max_sym_shndx_count = sym_count;
11701
11702 if (esdo->this_hdr.sh_type == SHT_REL
11703 || esdo->this_hdr.sh_type == SHT_RELA)
11704 /* Some backends use reloc_count in relocation sections
11705 to count particular types of relocs. Of course,
11706 reloc sections themselves can't have relocations. */
11707 ;
11708 else if (emit_relocs)
11709 {
11710 reloc_count = sec->reloc_count;
11711 if (bed->elf_backend_count_additional_relocs)
11712 {
11713 int c;
11714 c = (*bed->elf_backend_count_additional_relocs) (sec);
11715 additional_reloc_count += c;
11716 }
11717 }
11718 else if (bed->elf_backend_count_relocs)
11719 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11720
11721 esdi = elf_section_data (sec);
11722
11723 if ((sec->flags & SEC_RELOC) != 0)
11724 {
11725 size_t ext_size = 0;
11726
11727 if (esdi->rel.hdr != NULL)
11728 ext_size = esdi->rel.hdr->sh_size;
11729 if (esdi->rela.hdr != NULL)
11730 ext_size += esdi->rela.hdr->sh_size;
11731
11732 if (ext_size > max_external_reloc_size)
11733 max_external_reloc_size = ext_size;
11734 if (sec->reloc_count > max_internal_reloc_count)
11735 max_internal_reloc_count = sec->reloc_count;
11736 }
11737 }
11738 }
11739
11740 if (reloc_count == 0)
11741 continue;
11742
11743 reloc_count += additional_reloc_count;
11744 o->reloc_count += reloc_count;
11745
11746 if (p->type == bfd_indirect_link_order && emit_relocs)
11747 {
11748 if (esdi->rel.hdr)
11749 {
11750 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11751 esdo->rel.count += additional_reloc_count;
11752 }
11753 if (esdi->rela.hdr)
11754 {
11755 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11756 esdo->rela.count += additional_reloc_count;
11757 }
11758 }
11759 else
11760 {
11761 if (o->use_rela_p)
11762 esdo->rela.count += reloc_count;
11763 else
11764 esdo->rel.count += reloc_count;
11765 }
11766 }
11767
11768 if (o->reloc_count > 0)
11769 o->flags |= SEC_RELOC;
11770 else
11771 {
11772 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11773 set it (this is probably a bug) and if it is set
11774 assign_section_numbers will create a reloc section. */
11775 o->flags &=~ SEC_RELOC;
11776 }
11777
11778 /* If the SEC_ALLOC flag is not set, force the section VMA to
11779 zero. This is done in elf_fake_sections as well, but forcing
11780 the VMA to 0 here will ensure that relocs against these
11781 sections are handled correctly. */
11782 if ((o->flags & SEC_ALLOC) == 0
11783 && ! o->user_set_vma)
11784 o->vma = 0;
11785 }
11786
11787 if (! bfd_link_relocatable (info) && merged)
11788 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
11789
11790 /* Figure out the file positions for everything but the symbol table
11791 and the relocs. We set symcount to force assign_section_numbers
11792 to create a symbol table. */
11793 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
11794 BFD_ASSERT (! abfd->output_has_begun);
11795 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11796 goto error_return;
11797
11798 /* Set sizes, and assign file positions for reloc sections. */
11799 for (o = abfd->sections; o != NULL; o = o->next)
11800 {
11801 struct bfd_elf_section_data *esdo = elf_section_data (o);
11802 if ((o->flags & SEC_RELOC) != 0)
11803 {
11804 if (esdo->rel.hdr
11805 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
11806 goto error_return;
11807
11808 if (esdo->rela.hdr
11809 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
11810 goto error_return;
11811 }
11812
11813 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11814 to count upwards while actually outputting the relocations. */
11815 esdo->rel.count = 0;
11816 esdo->rela.count = 0;
11817
11818 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11819 {
11820 /* Cache the section contents so that they can be compressed
11821 later. Use bfd_malloc since it will be freed by
11822 bfd_compress_section_contents. */
11823 unsigned char *contents = esdo->this_hdr.contents;
11824 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11825 abort ();
11826 contents
11827 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11828 if (contents == NULL)
11829 goto error_return;
11830 esdo->this_hdr.contents = contents;
11831 }
11832 }
11833
11834 /* We have now assigned file positions for all the sections except
11835 .symtab, .strtab, and non-loaded reloc sections. We start the
11836 .symtab section at the current file position, and write directly
11837 to it. We build the .strtab section in memory. */
11838 bfd_get_symcount (abfd) = 0;
11839 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11840 /* sh_name is set in prep_headers. */
11841 symtab_hdr->sh_type = SHT_SYMTAB;
11842 /* sh_flags, sh_addr and sh_size all start off zero. */
11843 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11844 /* sh_link is set in assign_section_numbers. */
11845 /* sh_info is set below. */
11846 /* sh_offset is set just below. */
11847 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11848
11849 if (max_sym_count < 20)
11850 max_sym_count = 20;
11851 htab->strtabsize = max_sym_count;
11852 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11853 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
11854 if (htab->strtab == NULL)
11855 goto error_return;
11856 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11857 flinfo.symshndxbuf
11858 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11859 ? (Elf_External_Sym_Shndx *) -1 : NULL);
11860
11861 if (info->strip != strip_all || emit_relocs)
11862 {
11863 file_ptr off = elf_next_file_pos (abfd);
11864
11865 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11866
11867 /* Note that at this point elf_next_file_pos (abfd) is
11868 incorrect. We do not yet know the size of the .symtab section.
11869 We correct next_file_pos below, after we do know the size. */
11870
11871 /* Start writing out the symbol table. The first symbol is always a
11872 dummy symbol. */
11873 elfsym.st_value = 0;
11874 elfsym.st_size = 0;
11875 elfsym.st_info = 0;
11876 elfsym.st_other = 0;
11877 elfsym.st_shndx = SHN_UNDEF;
11878 elfsym.st_target_internal = 0;
11879 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11880 bfd_und_section_ptr, NULL) != 1)
11881 goto error_return;
11882
11883 /* Output a symbol for each section. We output these even if we are
11884 discarding local symbols, since they are used for relocs. These
11885 symbols have no names. We store the index of each one in the
11886 index field of the section, so that we can find it again when
11887 outputting relocs. */
11888
11889 elfsym.st_size = 0;
11890 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11891 elfsym.st_other = 0;
11892 elfsym.st_value = 0;
11893 elfsym.st_target_internal = 0;
11894 for (i = 1; i < elf_numsections (abfd); i++)
11895 {
11896 o = bfd_section_from_elf_index (abfd, i);
11897 if (o != NULL)
11898 {
11899 o->target_index = bfd_get_symcount (abfd);
11900 elfsym.st_shndx = i;
11901 if (!bfd_link_relocatable (info))
11902 elfsym.st_value = o->vma;
11903 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11904 NULL) != 1)
11905 goto error_return;
11906 }
11907 }
11908 }
11909
11910 /* Allocate some memory to hold information read in from the input
11911 files. */
11912 if (max_contents_size != 0)
11913 {
11914 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11915 if (flinfo.contents == NULL)
11916 goto error_return;
11917 }
11918
11919 if (max_external_reloc_size != 0)
11920 {
11921 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11922 if (flinfo.external_relocs == NULL)
11923 goto error_return;
11924 }
11925
11926 if (max_internal_reloc_count != 0)
11927 {
11928 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
11929 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11930 if (flinfo.internal_relocs == NULL)
11931 goto error_return;
11932 }
11933
11934 if (max_sym_count != 0)
11935 {
11936 amt = max_sym_count * bed->s->sizeof_sym;
11937 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11938 if (flinfo.external_syms == NULL)
11939 goto error_return;
11940
11941 amt = max_sym_count * sizeof (Elf_Internal_Sym);
11942 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11943 if (flinfo.internal_syms == NULL)
11944 goto error_return;
11945
11946 amt = max_sym_count * sizeof (long);
11947 flinfo.indices = (long int *) bfd_malloc (amt);
11948 if (flinfo.indices == NULL)
11949 goto error_return;
11950
11951 amt = max_sym_count * sizeof (asection *);
11952 flinfo.sections = (asection **) bfd_malloc (amt);
11953 if (flinfo.sections == NULL)
11954 goto error_return;
11955 }
11956
11957 if (max_sym_shndx_count != 0)
11958 {
11959 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
11960 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11961 if (flinfo.locsym_shndx == NULL)
11962 goto error_return;
11963 }
11964
11965 if (htab->tls_sec)
11966 {
11967 bfd_vma base, end = 0;
11968 asection *sec;
11969
11970 for (sec = htab->tls_sec;
11971 sec && (sec->flags & SEC_THREAD_LOCAL);
11972 sec = sec->next)
11973 {
11974 bfd_size_type size = sec->size;
11975
11976 if (size == 0
11977 && (sec->flags & SEC_HAS_CONTENTS) == 0)
11978 {
11979 struct bfd_link_order *ord = sec->map_tail.link_order;
11980
11981 if (ord != NULL)
11982 size = ord->offset + ord->size;
11983 }
11984 end = sec->vma + size;
11985 }
11986 base = htab->tls_sec->vma;
11987 /* Only align end of TLS section if static TLS doesn't have special
11988 alignment requirements. */
11989 if (bed->static_tls_alignment == 1)
11990 end = align_power (end, htab->tls_sec->alignment_power);
11991 htab->tls_size = end - base;
11992 }
11993
11994 /* Reorder SHF_LINK_ORDER sections. */
11995 for (o = abfd->sections; o != NULL; o = o->next)
11996 {
11997 if (!elf_fixup_link_order (abfd, o))
11998 return FALSE;
11999 }
12000
12001 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12002 return FALSE;
12003
12004 /* Since ELF permits relocations to be against local symbols, we
12005 must have the local symbols available when we do the relocations.
12006 Since we would rather only read the local symbols once, and we
12007 would rather not keep them in memory, we handle all the
12008 relocations for a single input file at the same time.
12009
12010 Unfortunately, there is no way to know the total number of local
12011 symbols until we have seen all of them, and the local symbol
12012 indices precede the global symbol indices. This means that when
12013 we are generating relocatable output, and we see a reloc against
12014 a global symbol, we can not know the symbol index until we have
12015 finished examining all the local symbols to see which ones we are
12016 going to output. To deal with this, we keep the relocations in
12017 memory, and don't output them until the end of the link. This is
12018 an unfortunate waste of memory, but I don't see a good way around
12019 it. Fortunately, it only happens when performing a relocatable
12020 link, which is not the common case. FIXME: If keep_memory is set
12021 we could write the relocs out and then read them again; I don't
12022 know how bad the memory loss will be. */
12023
12024 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12025 sub->output_has_begun = FALSE;
12026 for (o = abfd->sections; o != NULL; o = o->next)
12027 {
12028 for (p = o->map_head.link_order; p != NULL; p = p->next)
12029 {
12030 if (p->type == bfd_indirect_link_order
12031 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12032 == bfd_target_elf_flavour)
12033 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12034 {
12035 if (! sub->output_has_begun)
12036 {
12037 if (! elf_link_input_bfd (&flinfo, sub))
12038 goto error_return;
12039 sub->output_has_begun = TRUE;
12040 }
12041 }
12042 else if (p->type == bfd_section_reloc_link_order
12043 || p->type == bfd_symbol_reloc_link_order)
12044 {
12045 if (! elf_reloc_link_order (abfd, info, o, p))
12046 goto error_return;
12047 }
12048 else
12049 {
12050 if (! _bfd_default_link_order (abfd, info, o, p))
12051 {
12052 if (p->type == bfd_indirect_link_order
12053 && (bfd_get_flavour (sub)
12054 == bfd_target_elf_flavour)
12055 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12056 != bed->s->elfclass))
12057 {
12058 const char *iclass, *oclass;
12059
12060 switch (bed->s->elfclass)
12061 {
12062 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12063 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12064 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12065 default: abort ();
12066 }
12067
12068 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12069 {
12070 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12071 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12072 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12073 default: abort ();
12074 }
12075
12076 bfd_set_error (bfd_error_wrong_format);
12077 _bfd_error_handler
12078 /* xgettext:c-format */
12079 (_("%B: file class %s incompatible with %s"),
12080 sub, iclass, oclass);
12081 }
12082
12083 goto error_return;
12084 }
12085 }
12086 }
12087 }
12088
12089 /* Free symbol buffer if needed. */
12090 if (!info->reduce_memory_overheads)
12091 {
12092 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12093 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
12094 && elf_tdata (sub)->symbuf)
12095 {
12096 free (elf_tdata (sub)->symbuf);
12097 elf_tdata (sub)->symbuf = NULL;
12098 }
12099 }
12100
12101 /* Output any global symbols that got converted to local in a
12102 version script or due to symbol visibility. We do this in a
12103 separate step since ELF requires all local symbols to appear
12104 prior to any global symbols. FIXME: We should only do this if
12105 some global symbols were, in fact, converted to become local.
12106 FIXME: Will this work correctly with the Irix 5 linker? */
12107 eoinfo.failed = FALSE;
12108 eoinfo.flinfo = &flinfo;
12109 eoinfo.localsyms = TRUE;
12110 eoinfo.file_sym_done = FALSE;
12111 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12112 if (eoinfo.failed)
12113 return FALSE;
12114
12115 /* If backend needs to output some local symbols not present in the hash
12116 table, do it now. */
12117 if (bed->elf_backend_output_arch_local_syms
12118 && (info->strip != strip_all || emit_relocs))
12119 {
12120 typedef int (*out_sym_func)
12121 (void *, const char *, Elf_Internal_Sym *, asection *,
12122 struct elf_link_hash_entry *);
12123
12124 if (! ((*bed->elf_backend_output_arch_local_syms)
12125 (abfd, info, &flinfo,
12126 (out_sym_func) elf_link_output_symstrtab)))
12127 return FALSE;
12128 }
12129
12130 /* That wrote out all the local symbols. Finish up the symbol table
12131 with the global symbols. Even if we want to strip everything we
12132 can, we still need to deal with those global symbols that got
12133 converted to local in a version script. */
12134
12135 /* The sh_info field records the index of the first non local symbol. */
12136 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12137
12138 if (dynamic
12139 && htab->dynsym != NULL
12140 && htab->dynsym->output_section != bfd_abs_section_ptr)
12141 {
12142 Elf_Internal_Sym sym;
12143 bfd_byte *dynsym = htab->dynsym->contents;
12144
12145 o = htab->dynsym->output_section;
12146 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12147
12148 /* Write out the section symbols for the output sections. */
12149 if (bfd_link_pic (info)
12150 || htab->is_relocatable_executable)
12151 {
12152 asection *s;
12153
12154 sym.st_size = 0;
12155 sym.st_name = 0;
12156 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12157 sym.st_other = 0;
12158 sym.st_target_internal = 0;
12159
12160 for (s = abfd->sections; s != NULL; s = s->next)
12161 {
12162 int indx;
12163 bfd_byte *dest;
12164 long dynindx;
12165
12166 dynindx = elf_section_data (s)->dynindx;
12167 if (dynindx <= 0)
12168 continue;
12169 indx = elf_section_data (s)->this_idx;
12170 BFD_ASSERT (indx > 0);
12171 sym.st_shndx = indx;
12172 if (! check_dynsym (abfd, &sym))
12173 return FALSE;
12174 sym.st_value = s->vma;
12175 dest = dynsym + dynindx * bed->s->sizeof_sym;
12176 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12177 }
12178 }
12179
12180 /* Write out the local dynsyms. */
12181 if (htab->dynlocal)
12182 {
12183 struct elf_link_local_dynamic_entry *e;
12184 for (e = htab->dynlocal; e ; e = e->next)
12185 {
12186 asection *s;
12187 bfd_byte *dest;
12188
12189 /* Copy the internal symbol and turn off visibility.
12190 Note that we saved a word of storage and overwrote
12191 the original st_name with the dynstr_index. */
12192 sym = e->isym;
12193 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12194
12195 s = bfd_section_from_elf_index (e->input_bfd,
12196 e->isym.st_shndx);
12197 if (s != NULL)
12198 {
12199 sym.st_shndx =
12200 elf_section_data (s->output_section)->this_idx;
12201 if (! check_dynsym (abfd, &sym))
12202 return FALSE;
12203 sym.st_value = (s->output_section->vma
12204 + s->output_offset
12205 + e->isym.st_value);
12206 }
12207
12208 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12209 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12210 }
12211 }
12212 }
12213
12214 /* We get the global symbols from the hash table. */
12215 eoinfo.failed = FALSE;
12216 eoinfo.localsyms = FALSE;
12217 eoinfo.flinfo = &flinfo;
12218 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12219 if (eoinfo.failed)
12220 return FALSE;
12221
12222 /* If backend needs to output some symbols not present in the hash
12223 table, do it now. */
12224 if (bed->elf_backend_output_arch_syms
12225 && (info->strip != strip_all || emit_relocs))
12226 {
12227 typedef int (*out_sym_func)
12228 (void *, const char *, Elf_Internal_Sym *, asection *,
12229 struct elf_link_hash_entry *);
12230
12231 if (! ((*bed->elf_backend_output_arch_syms)
12232 (abfd, info, &flinfo,
12233 (out_sym_func) elf_link_output_symstrtab)))
12234 return FALSE;
12235 }
12236
12237 /* Finalize the .strtab section. */
12238 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12239
12240 /* Swap out the .strtab section. */
12241 if (!elf_link_swap_symbols_out (&flinfo))
12242 return FALSE;
12243
12244 /* Now we know the size of the symtab section. */
12245 if (bfd_get_symcount (abfd) > 0)
12246 {
12247 /* Finish up and write out the symbol string table (.strtab)
12248 section. */
12249 Elf_Internal_Shdr *symstrtab_hdr = NULL;
12250 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12251
12252 if (elf_symtab_shndx_list (abfd))
12253 {
12254 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12255
12256 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12257 {
12258 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12259 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12260 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12261 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12262 symtab_shndx_hdr->sh_size = amt;
12263
12264 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12265 off, TRUE);
12266
12267 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12268 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12269 return FALSE;
12270 }
12271 }
12272
12273 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12274 /* sh_name was set in prep_headers. */
12275 symstrtab_hdr->sh_type = SHT_STRTAB;
12276 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12277 symstrtab_hdr->sh_addr = 0;
12278 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12279 symstrtab_hdr->sh_entsize = 0;
12280 symstrtab_hdr->sh_link = 0;
12281 symstrtab_hdr->sh_info = 0;
12282 /* sh_offset is set just below. */
12283 symstrtab_hdr->sh_addralign = 1;
12284
12285 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12286 off, TRUE);
12287 elf_next_file_pos (abfd) = off;
12288
12289 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12290 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12291 return FALSE;
12292 }
12293
12294 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12295 {
12296 _bfd_error_handler (_("%B: failed to generate import library"),
12297 info->out_implib_bfd);
12298 return FALSE;
12299 }
12300
12301 /* Adjust the relocs to have the correct symbol indices. */
12302 for (o = abfd->sections; o != NULL; o = o->next)
12303 {
12304 struct bfd_elf_section_data *esdo = elf_section_data (o);
12305 bfd_boolean sort;
12306
12307 if ((o->flags & SEC_RELOC) == 0)
12308 continue;
12309
12310 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12311 if (esdo->rel.hdr != NULL
12312 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
12313 return FALSE;
12314 if (esdo->rela.hdr != NULL
12315 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
12316 return FALSE;
12317
12318 /* Set the reloc_count field to 0 to prevent write_relocs from
12319 trying to swap the relocs out itself. */
12320 o->reloc_count = 0;
12321 }
12322
12323 if (dynamic && info->combreloc && dynobj != NULL)
12324 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12325
12326 /* If we are linking against a dynamic object, or generating a
12327 shared library, finish up the dynamic linking information. */
12328 if (dynamic)
12329 {
12330 bfd_byte *dyncon, *dynconend;
12331
12332 /* Fix up .dynamic entries. */
12333 o = bfd_get_linker_section (dynobj, ".dynamic");
12334 BFD_ASSERT (o != NULL);
12335
12336 dyncon = o->contents;
12337 dynconend = o->contents + o->size;
12338 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12339 {
12340 Elf_Internal_Dyn dyn;
12341 const char *name;
12342 unsigned int type;
12343 bfd_size_type sh_size;
12344 bfd_vma sh_addr;
12345
12346 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12347
12348 switch (dyn.d_tag)
12349 {
12350 default:
12351 continue;
12352 case DT_NULL:
12353 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12354 {
12355 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12356 {
12357 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12358 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12359 default: continue;
12360 }
12361 dyn.d_un.d_val = relativecount;
12362 relativecount = 0;
12363 break;
12364 }
12365 continue;
12366
12367 case DT_INIT:
12368 name = info->init_function;
12369 goto get_sym;
12370 case DT_FINI:
12371 name = info->fini_function;
12372 get_sym:
12373 {
12374 struct elf_link_hash_entry *h;
12375
12376 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
12377 if (h != NULL
12378 && (h->root.type == bfd_link_hash_defined
12379 || h->root.type == bfd_link_hash_defweak))
12380 {
12381 dyn.d_un.d_ptr = h->root.u.def.value;
12382 o = h->root.u.def.section;
12383 if (o->output_section != NULL)
12384 dyn.d_un.d_ptr += (o->output_section->vma
12385 + o->output_offset);
12386 else
12387 {
12388 /* The symbol is imported from another shared
12389 library and does not apply to this one. */
12390 dyn.d_un.d_ptr = 0;
12391 }
12392 break;
12393 }
12394 }
12395 continue;
12396
12397 case DT_PREINIT_ARRAYSZ:
12398 name = ".preinit_array";
12399 goto get_out_size;
12400 case DT_INIT_ARRAYSZ:
12401 name = ".init_array";
12402 goto get_out_size;
12403 case DT_FINI_ARRAYSZ:
12404 name = ".fini_array";
12405 get_out_size:
12406 o = bfd_get_section_by_name (abfd, name);
12407 if (o == NULL)
12408 {
12409 _bfd_error_handler
12410 (_("could not find section %s"), name);
12411 goto error_return;
12412 }
12413 if (o->size == 0)
12414 _bfd_error_handler
12415 (_("warning: %s section has zero size"), name);
12416 dyn.d_un.d_val = o->size;
12417 break;
12418
12419 case DT_PREINIT_ARRAY:
12420 name = ".preinit_array";
12421 goto get_out_vma;
12422 case DT_INIT_ARRAY:
12423 name = ".init_array";
12424 goto get_out_vma;
12425 case DT_FINI_ARRAY:
12426 name = ".fini_array";
12427 get_out_vma:
12428 o = bfd_get_section_by_name (abfd, name);
12429 goto do_vma;
12430
12431 case DT_HASH:
12432 name = ".hash";
12433 goto get_vma;
12434 case DT_GNU_HASH:
12435 name = ".gnu.hash";
12436 goto get_vma;
12437 case DT_STRTAB:
12438 name = ".dynstr";
12439 goto get_vma;
12440 case DT_SYMTAB:
12441 name = ".dynsym";
12442 goto get_vma;
12443 case DT_VERDEF:
12444 name = ".gnu.version_d";
12445 goto get_vma;
12446 case DT_VERNEED:
12447 name = ".gnu.version_r";
12448 goto get_vma;
12449 case DT_VERSYM:
12450 name = ".gnu.version";
12451 get_vma:
12452 o = bfd_get_linker_section (dynobj, name);
12453 do_vma:
12454 if (o == NULL || bfd_is_abs_section (o->output_section))
12455 {
12456 _bfd_error_handler
12457 (_("could not find section %s"), name);
12458 goto error_return;
12459 }
12460 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12461 {
12462 _bfd_error_handler
12463 (_("warning: section '%s' is being made into a note"), name);
12464 bfd_set_error (bfd_error_nonrepresentable_section);
12465 goto error_return;
12466 }
12467 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
12468 break;
12469
12470 case DT_REL:
12471 case DT_RELA:
12472 case DT_RELSZ:
12473 case DT_RELASZ:
12474 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12475 type = SHT_REL;
12476 else
12477 type = SHT_RELA;
12478 sh_size = 0;
12479 sh_addr = 0;
12480 for (i = 1; i < elf_numsections (abfd); i++)
12481 {
12482 Elf_Internal_Shdr *hdr;
12483
12484 hdr = elf_elfsections (abfd)[i];
12485 if (hdr->sh_type == type
12486 && (hdr->sh_flags & SHF_ALLOC) != 0)
12487 {
12488 sh_size += hdr->sh_size;
12489 if (sh_addr == 0
12490 || sh_addr > hdr->sh_addr)
12491 sh_addr = hdr->sh_addr;
12492 }
12493 }
12494
12495 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12496 {
12497 /* Don't count procedure linkage table relocs in the
12498 overall reloc count. */
12499 sh_size -= htab->srelplt->size;
12500 if (sh_size == 0)
12501 /* If the size is zero, make the address zero too.
12502 This is to avoid a glibc bug. If the backend
12503 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12504 zero, then we'll put DT_RELA at the end of
12505 DT_JMPREL. glibc will interpret the end of
12506 DT_RELA matching the end of DT_JMPREL as the
12507 case where DT_RELA includes DT_JMPREL, and for
12508 LD_BIND_NOW will decide that processing DT_RELA
12509 will process the PLT relocs too. Net result:
12510 No PLT relocs applied. */
12511 sh_addr = 0;
12512
12513 /* If .rela.plt is the first .rela section, exclude
12514 it from DT_RELA. */
12515 else if (sh_addr == (htab->srelplt->output_section->vma
12516 + htab->srelplt->output_offset))
12517 sh_addr += htab->srelplt->size;
12518 }
12519
12520 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12521 dyn.d_un.d_val = sh_size;
12522 else
12523 dyn.d_un.d_ptr = sh_addr;
12524 break;
12525 }
12526 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12527 }
12528 }
12529
12530 /* If we have created any dynamic sections, then output them. */
12531 if (dynobj != NULL)
12532 {
12533 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12534 goto error_return;
12535
12536 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12537 if (((info->warn_shared_textrel && bfd_link_pic (info))
12538 || info->error_textrel)
12539 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
12540 {
12541 bfd_byte *dyncon, *dynconend;
12542
12543 dyncon = o->contents;
12544 dynconend = o->contents + o->size;
12545 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12546 {
12547 Elf_Internal_Dyn dyn;
12548
12549 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12550
12551 if (dyn.d_tag == DT_TEXTREL)
12552 {
12553 if (info->error_textrel)
12554 info->callbacks->einfo
12555 (_("%P%X: read-only segment has dynamic relocations.\n"));
12556 else
12557 info->callbacks->einfo
12558 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
12559 break;
12560 }
12561 }
12562 }
12563
12564 for (o = dynobj->sections; o != NULL; o = o->next)
12565 {
12566 if ((o->flags & SEC_HAS_CONTENTS) == 0
12567 || o->size == 0
12568 || o->output_section == bfd_abs_section_ptr)
12569 continue;
12570 if ((o->flags & SEC_LINKER_CREATED) == 0)
12571 {
12572 /* At this point, we are only interested in sections
12573 created by _bfd_elf_link_create_dynamic_sections. */
12574 continue;
12575 }
12576 if (htab->stab_info.stabstr == o)
12577 continue;
12578 if (htab->eh_info.hdr_sec == o)
12579 continue;
12580 if (strcmp (o->name, ".dynstr") != 0)
12581 {
12582 if (! bfd_set_section_contents (abfd, o->output_section,
12583 o->contents,
12584 (file_ptr) o->output_offset
12585 * bfd_octets_per_byte (abfd),
12586 o->size))
12587 goto error_return;
12588 }
12589 else
12590 {
12591 /* The contents of the .dynstr section are actually in a
12592 stringtab. */
12593 file_ptr off;
12594
12595 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12596 if (bfd_seek (abfd, off, SEEK_SET) != 0
12597 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
12598 goto error_return;
12599 }
12600 }
12601 }
12602
12603 if (!info->resolve_section_groups)
12604 {
12605 bfd_boolean failed = FALSE;
12606
12607 BFD_ASSERT (bfd_link_relocatable (info));
12608 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12609 if (failed)
12610 goto error_return;
12611 }
12612
12613 /* If we have optimized stabs strings, output them. */
12614 if (htab->stab_info.stabstr != NULL)
12615 {
12616 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
12617 goto error_return;
12618 }
12619
12620 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12621 goto error_return;
12622
12623 elf_final_link_free (abfd, &flinfo);
12624
12625 elf_linker (abfd) = TRUE;
12626
12627 if (attr_section)
12628 {
12629 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
12630 if (contents == NULL)
12631 return FALSE; /* Bail out and fail. */
12632 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12633 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12634 free (contents);
12635 }
12636
12637 return TRUE;
12638
12639 error_return:
12640 elf_final_link_free (abfd, &flinfo);
12641 return FALSE;
12642 }
12643 \f
12644 /* Initialize COOKIE for input bfd ABFD. */
12645
12646 static bfd_boolean
12647 init_reloc_cookie (struct elf_reloc_cookie *cookie,
12648 struct bfd_link_info *info, bfd *abfd)
12649 {
12650 Elf_Internal_Shdr *symtab_hdr;
12651 const struct elf_backend_data *bed;
12652
12653 bed = get_elf_backend_data (abfd);
12654 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12655
12656 cookie->abfd = abfd;
12657 cookie->sym_hashes = elf_sym_hashes (abfd);
12658 cookie->bad_symtab = elf_bad_symtab (abfd);
12659 if (cookie->bad_symtab)
12660 {
12661 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12662 cookie->extsymoff = 0;
12663 }
12664 else
12665 {
12666 cookie->locsymcount = symtab_hdr->sh_info;
12667 cookie->extsymoff = symtab_hdr->sh_info;
12668 }
12669
12670 if (bed->s->arch_size == 32)
12671 cookie->r_sym_shift = 8;
12672 else
12673 cookie->r_sym_shift = 32;
12674
12675 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12676 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12677 {
12678 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12679 cookie->locsymcount, 0,
12680 NULL, NULL, NULL);
12681 if (cookie->locsyms == NULL)
12682 {
12683 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12684 return FALSE;
12685 }
12686 if (info->keep_memory)
12687 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12688 }
12689 return TRUE;
12690 }
12691
12692 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12693
12694 static void
12695 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12696 {
12697 Elf_Internal_Shdr *symtab_hdr;
12698
12699 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12700 if (cookie->locsyms != NULL
12701 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12702 free (cookie->locsyms);
12703 }
12704
12705 /* Initialize the relocation information in COOKIE for input section SEC
12706 of input bfd ABFD. */
12707
12708 static bfd_boolean
12709 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12710 struct bfd_link_info *info, bfd *abfd,
12711 asection *sec)
12712 {
12713 if (sec->reloc_count == 0)
12714 {
12715 cookie->rels = NULL;
12716 cookie->relend = NULL;
12717 }
12718 else
12719 {
12720 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12721 info->keep_memory);
12722 if (cookie->rels == NULL)
12723 return FALSE;
12724 cookie->rel = cookie->rels;
12725 cookie->relend = cookie->rels + sec->reloc_count;
12726 }
12727 cookie->rel = cookie->rels;
12728 return TRUE;
12729 }
12730
12731 /* Free the memory allocated by init_reloc_cookie_rels,
12732 if appropriate. */
12733
12734 static void
12735 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12736 asection *sec)
12737 {
12738 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12739 free (cookie->rels);
12740 }
12741
12742 /* Initialize the whole of COOKIE for input section SEC. */
12743
12744 static bfd_boolean
12745 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12746 struct bfd_link_info *info,
12747 asection *sec)
12748 {
12749 if (!init_reloc_cookie (cookie, info, sec->owner))
12750 goto error1;
12751 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12752 goto error2;
12753 return TRUE;
12754
12755 error2:
12756 fini_reloc_cookie (cookie, sec->owner);
12757 error1:
12758 return FALSE;
12759 }
12760
12761 /* Free the memory allocated by init_reloc_cookie_for_section,
12762 if appropriate. */
12763
12764 static void
12765 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12766 asection *sec)
12767 {
12768 fini_reloc_cookie_rels (cookie, sec);
12769 fini_reloc_cookie (cookie, sec->owner);
12770 }
12771 \f
12772 /* Garbage collect unused sections. */
12773
12774 /* Default gc_mark_hook. */
12775
12776 asection *
12777 _bfd_elf_gc_mark_hook (asection *sec,
12778 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12779 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12780 struct elf_link_hash_entry *h,
12781 Elf_Internal_Sym *sym)
12782 {
12783 if (h != NULL)
12784 {
12785 switch (h->root.type)
12786 {
12787 case bfd_link_hash_defined:
12788 case bfd_link_hash_defweak:
12789 return h->root.u.def.section;
12790
12791 case bfd_link_hash_common:
12792 return h->root.u.c.p->section;
12793
12794 default:
12795 break;
12796 }
12797 }
12798 else
12799 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12800
12801 return NULL;
12802 }
12803
12804 /* Return the global debug definition section. */
12805
12806 static asection *
12807 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
12808 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12809 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12810 struct elf_link_hash_entry *h,
12811 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
12812 {
12813 if (h != NULL
12814 && (h->root.type == bfd_link_hash_defined
12815 || h->root.type == bfd_link_hash_defweak)
12816 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
12817 return h->root.u.def.section;
12818
12819 return NULL;
12820 }
12821
12822 /* COOKIE->rel describes a relocation against section SEC, which is
12823 a section we've decided to keep. Return the section that contains
12824 the relocation symbol, or NULL if no section contains it. */
12825
12826 asection *
12827 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12828 elf_gc_mark_hook_fn gc_mark_hook,
12829 struct elf_reloc_cookie *cookie,
12830 bfd_boolean *start_stop)
12831 {
12832 unsigned long r_symndx;
12833 struct elf_link_hash_entry *h;
12834
12835 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
12836 if (r_symndx == STN_UNDEF)
12837 return NULL;
12838
12839 if (r_symndx >= cookie->locsymcount
12840 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12841 {
12842 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
12843 if (h == NULL)
12844 {
12845 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12846 sec->owner);
12847 return NULL;
12848 }
12849 while (h->root.type == bfd_link_hash_indirect
12850 || h->root.type == bfd_link_hash_warning)
12851 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12852 h->mark = 1;
12853 /* If this symbol is weak and there is a non-weak definition, we
12854 keep the non-weak definition because many backends put
12855 dynamic reloc info on the non-weak definition for code
12856 handling copy relocs. */
12857 if (h->is_weakalias)
12858 weakdef (h)->mark = 1;
12859
12860 if (start_stop != NULL)
12861 {
12862 /* To work around a glibc bug, mark XXX input sections
12863 when there is a reference to __start_XXX or __stop_XXX
12864 symbols. */
12865 if (h->start_stop)
12866 {
12867 asection *s = h->u2.start_stop_section;
12868 *start_stop = !s->gc_mark;
12869 return s;
12870 }
12871 }
12872
12873 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12874 }
12875
12876 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12877 &cookie->locsyms[r_symndx]);
12878 }
12879
12880 /* COOKIE->rel describes a relocation against section SEC, which is
12881 a section we've decided to keep. Mark the section that contains
12882 the relocation symbol. */
12883
12884 bfd_boolean
12885 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12886 asection *sec,
12887 elf_gc_mark_hook_fn gc_mark_hook,
12888 struct elf_reloc_cookie *cookie)
12889 {
12890 asection *rsec;
12891 bfd_boolean start_stop = FALSE;
12892
12893 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12894 while (rsec != NULL)
12895 {
12896 if (!rsec->gc_mark)
12897 {
12898 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12899 || (rsec->owner->flags & DYNAMIC) != 0)
12900 rsec->gc_mark = 1;
12901 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12902 return FALSE;
12903 }
12904 if (!start_stop)
12905 break;
12906 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
12907 }
12908 return TRUE;
12909 }
12910
12911 /* The mark phase of garbage collection. For a given section, mark
12912 it and any sections in this section's group, and all the sections
12913 which define symbols to which it refers. */
12914
12915 bfd_boolean
12916 _bfd_elf_gc_mark (struct bfd_link_info *info,
12917 asection *sec,
12918 elf_gc_mark_hook_fn gc_mark_hook)
12919 {
12920 bfd_boolean ret;
12921 asection *group_sec, *eh_frame;
12922
12923 sec->gc_mark = 1;
12924
12925 /* Mark all the sections in the group. */
12926 group_sec = elf_section_data (sec)->next_in_group;
12927 if (group_sec && !group_sec->gc_mark)
12928 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
12929 return FALSE;
12930
12931 /* Look through the section relocs. */
12932 ret = TRUE;
12933 eh_frame = elf_eh_frame_section (sec->owner);
12934 if ((sec->flags & SEC_RELOC) != 0
12935 && sec->reloc_count > 0
12936 && sec != eh_frame)
12937 {
12938 struct elf_reloc_cookie cookie;
12939
12940 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12941 ret = FALSE;
12942 else
12943 {
12944 for (; cookie.rel < cookie.relend; cookie.rel++)
12945 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
12946 {
12947 ret = FALSE;
12948 break;
12949 }
12950 fini_reloc_cookie_for_section (&cookie, sec);
12951 }
12952 }
12953
12954 if (ret && eh_frame && elf_fde_list (sec))
12955 {
12956 struct elf_reloc_cookie cookie;
12957
12958 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12959 ret = FALSE;
12960 else
12961 {
12962 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12963 gc_mark_hook, &cookie))
12964 ret = FALSE;
12965 fini_reloc_cookie_for_section (&cookie, eh_frame);
12966 }
12967 }
12968
12969 eh_frame = elf_section_eh_frame_entry (sec);
12970 if (ret && eh_frame && !eh_frame->gc_mark)
12971 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12972 ret = FALSE;
12973
12974 return ret;
12975 }
12976
12977 /* Scan and mark sections in a special or debug section group. */
12978
12979 static void
12980 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12981 {
12982 /* Point to first section of section group. */
12983 asection *ssec;
12984 /* Used to iterate the section group. */
12985 asection *msec;
12986
12987 bfd_boolean is_special_grp = TRUE;
12988 bfd_boolean is_debug_grp = TRUE;
12989
12990 /* First scan to see if group contains any section other than debug
12991 and special section. */
12992 ssec = msec = elf_next_in_group (grp);
12993 do
12994 {
12995 if ((msec->flags & SEC_DEBUGGING) == 0)
12996 is_debug_grp = FALSE;
12997
12998 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12999 is_special_grp = FALSE;
13000
13001 msec = elf_next_in_group (msec);
13002 }
13003 while (msec != ssec);
13004
13005 /* If this is a pure debug section group or pure special section group,
13006 keep all sections in this group. */
13007 if (is_debug_grp || is_special_grp)
13008 {
13009 do
13010 {
13011 msec->gc_mark = 1;
13012 msec = elf_next_in_group (msec);
13013 }
13014 while (msec != ssec);
13015 }
13016 }
13017
13018 /* Keep debug and special sections. */
13019
13020 bfd_boolean
13021 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13022 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
13023 {
13024 bfd *ibfd;
13025
13026 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13027 {
13028 asection *isec;
13029 bfd_boolean some_kept;
13030 bfd_boolean debug_frag_seen;
13031 bfd_boolean has_kept_debug_info;
13032
13033 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13034 continue;
13035 isec = ibfd->sections;
13036 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13037 continue;
13038
13039 /* Ensure all linker created sections are kept,
13040 see if any other section is already marked,
13041 and note if we have any fragmented debug sections. */
13042 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
13043 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13044 {
13045 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13046 isec->gc_mark = 1;
13047 else if (isec->gc_mark
13048 && (isec->flags & SEC_ALLOC) != 0
13049 && elf_section_type (isec) != SHT_NOTE)
13050 some_kept = TRUE;
13051
13052 if (!debug_frag_seen
13053 && (isec->flags & SEC_DEBUGGING)
13054 && CONST_STRNEQ (isec->name, ".debug_line."))
13055 debug_frag_seen = TRUE;
13056 }
13057
13058 /* If no non-note alloc section in this file will be kept, then
13059 we can toss out the debug and special sections. */
13060 if (!some_kept)
13061 continue;
13062
13063 /* Keep debug and special sections like .comment when they are
13064 not part of a group. Also keep section groups that contain
13065 just debug sections or special sections. */
13066 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13067 {
13068 if ((isec->flags & SEC_GROUP) != 0)
13069 _bfd_elf_gc_mark_debug_special_section_group (isec);
13070 else if (((isec->flags & SEC_DEBUGGING) != 0
13071 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13072 && elf_next_in_group (isec) == NULL)
13073 isec->gc_mark = 1;
13074 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13075 has_kept_debug_info = TRUE;
13076 }
13077
13078 /* Look for CODE sections which are going to be discarded,
13079 and find and discard any fragmented debug sections which
13080 are associated with that code section. */
13081 if (debug_frag_seen)
13082 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13083 if ((isec->flags & SEC_CODE) != 0
13084 && isec->gc_mark == 0)
13085 {
13086 unsigned int ilen;
13087 asection *dsec;
13088
13089 ilen = strlen (isec->name);
13090
13091 /* Association is determined by the name of the debug
13092 section containing the name of the code section as
13093 a suffix. For example .debug_line.text.foo is a
13094 debug section associated with .text.foo. */
13095 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13096 {
13097 unsigned int dlen;
13098
13099 if (dsec->gc_mark == 0
13100 || (dsec->flags & SEC_DEBUGGING) == 0)
13101 continue;
13102
13103 dlen = strlen (dsec->name);
13104
13105 if (dlen > ilen
13106 && strncmp (dsec->name + (dlen - ilen),
13107 isec->name, ilen) == 0)
13108 dsec->gc_mark = 0;
13109 }
13110 }
13111
13112 /* Mark debug sections referenced by kept debug sections. */
13113 if (has_kept_debug_info)
13114 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13115 if (isec->gc_mark
13116 && (isec->flags & SEC_DEBUGGING) != 0)
13117 if (!_bfd_elf_gc_mark (info, isec,
13118 elf_gc_mark_debug_section))
13119 return FALSE;
13120 }
13121 return TRUE;
13122 }
13123
13124 static bfd_boolean
13125 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13126 {
13127 bfd *sub;
13128 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13129
13130 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13131 {
13132 asection *o;
13133
13134 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13135 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
13136 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13137 continue;
13138 o = sub->sections;
13139 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13140 continue;
13141
13142 for (o = sub->sections; o != NULL; o = o->next)
13143 {
13144 /* When any section in a section group is kept, we keep all
13145 sections in the section group. If the first member of
13146 the section group is excluded, we will also exclude the
13147 group section. */
13148 if (o->flags & SEC_GROUP)
13149 {
13150 asection *first = elf_next_in_group (o);
13151 o->gc_mark = first->gc_mark;
13152 }
13153
13154 if (o->gc_mark)
13155 continue;
13156
13157 /* Skip sweeping sections already excluded. */
13158 if (o->flags & SEC_EXCLUDE)
13159 continue;
13160
13161 /* Since this is early in the link process, it is simple
13162 to remove a section from the output. */
13163 o->flags |= SEC_EXCLUDE;
13164
13165 if (info->print_gc_sections && o->size != 0)
13166 /* xgettext:c-format */
13167 _bfd_error_handler (_("Removing unused section '%A' in file '%B'"),
13168 o, sub);
13169 }
13170 }
13171
13172 return TRUE;
13173 }
13174
13175 /* Propagate collected vtable information. This is called through
13176 elf_link_hash_traverse. */
13177
13178 static bfd_boolean
13179 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13180 {
13181 /* Those that are not vtables. */
13182 if (h->start_stop
13183 || h->u2.vtable == NULL
13184 || h->u2.vtable->parent == NULL)
13185 return TRUE;
13186
13187 /* Those vtables that do not have parents, we cannot merge. */
13188 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
13189 return TRUE;
13190
13191 /* If we've already been done, exit. */
13192 if (h->u2.vtable->used && h->u2.vtable->used[-1])
13193 return TRUE;
13194
13195 /* Make sure the parent's table is up to date. */
13196 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
13197
13198 if (h->u2.vtable->used == NULL)
13199 {
13200 /* None of this table's entries were referenced. Re-use the
13201 parent's table. */
13202 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13203 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
13204 }
13205 else
13206 {
13207 size_t n;
13208 bfd_boolean *cu, *pu;
13209
13210 /* Or the parent's entries into ours. */
13211 cu = h->u2.vtable->used;
13212 cu[-1] = TRUE;
13213 pu = h->u2.vtable->parent->u2.vtable->used;
13214 if (pu != NULL)
13215 {
13216 const struct elf_backend_data *bed;
13217 unsigned int log_file_align;
13218
13219 bed = get_elf_backend_data (h->root.u.def.section->owner);
13220 log_file_align = bed->s->log_file_align;
13221 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
13222 while (n--)
13223 {
13224 if (*pu)
13225 *cu = TRUE;
13226 pu++;
13227 cu++;
13228 }
13229 }
13230 }
13231
13232 return TRUE;
13233 }
13234
13235 static bfd_boolean
13236 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13237 {
13238 asection *sec;
13239 bfd_vma hstart, hend;
13240 Elf_Internal_Rela *relstart, *relend, *rel;
13241 const struct elf_backend_data *bed;
13242 unsigned int log_file_align;
13243
13244 /* Take care of both those symbols that do not describe vtables as
13245 well as those that are not loaded. */
13246 if (h->start_stop
13247 || h->u2.vtable == NULL
13248 || h->u2.vtable->parent == NULL)
13249 return TRUE;
13250
13251 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13252 || h->root.type == bfd_link_hash_defweak);
13253
13254 sec = h->root.u.def.section;
13255 hstart = h->root.u.def.value;
13256 hend = hstart + h->size;
13257
13258 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13259 if (!relstart)
13260 return *(bfd_boolean *) okp = FALSE;
13261 bed = get_elf_backend_data (sec->owner);
13262 log_file_align = bed->s->log_file_align;
13263
13264 relend = relstart + sec->reloc_count;
13265
13266 for (rel = relstart; rel < relend; ++rel)
13267 if (rel->r_offset >= hstart && rel->r_offset < hend)
13268 {
13269 /* If the entry is in use, do nothing. */
13270 if (h->u2.vtable->used
13271 && (rel->r_offset - hstart) < h->u2.vtable->size)
13272 {
13273 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13274 if (h->u2.vtable->used[entry])
13275 continue;
13276 }
13277 /* Otherwise, kill it. */
13278 rel->r_offset = rel->r_info = rel->r_addend = 0;
13279 }
13280
13281 return TRUE;
13282 }
13283
13284 /* Mark sections containing dynamically referenced symbols. When
13285 building shared libraries, we must assume that any visible symbol is
13286 referenced. */
13287
13288 bfd_boolean
13289 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13290 {
13291 struct bfd_link_info *info = (struct bfd_link_info *) inf;
13292 struct bfd_elf_dynamic_list *d = info->dynamic_list;
13293
13294 if ((h->root.type == bfd_link_hash_defined
13295 || h->root.type == bfd_link_hash_defweak)
13296 && ((h->ref_dynamic && !h->forced_local)
13297 || ((h->def_regular || ELF_COMMON_DEF_P (h))
13298 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
13299 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
13300 && (!bfd_link_executable (info)
13301 || info->gc_keep_exported
13302 || info->export_dynamic
13303 || (h->dynamic
13304 && d != NULL
13305 && (*d->match) (&d->head, NULL, h->root.root.string)))
13306 && (h->versioned >= versioned
13307 || !bfd_hide_sym_by_version (info->version_info,
13308 h->root.root.string)))))
13309 h->root.u.def.section->flags |= SEC_KEEP;
13310
13311 return TRUE;
13312 }
13313
13314 /* Keep all sections containing symbols undefined on the command-line,
13315 and the section containing the entry symbol. */
13316
13317 void
13318 _bfd_elf_gc_keep (struct bfd_link_info *info)
13319 {
13320 struct bfd_sym_chain *sym;
13321
13322 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13323 {
13324 struct elf_link_hash_entry *h;
13325
13326 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13327 FALSE, FALSE, FALSE);
13328
13329 if (h != NULL
13330 && (h->root.type == bfd_link_hash_defined
13331 || h->root.type == bfd_link_hash_defweak)
13332 && !bfd_is_abs_section (h->root.u.def.section)
13333 && !bfd_is_und_section (h->root.u.def.section))
13334 h->root.u.def.section->flags |= SEC_KEEP;
13335 }
13336 }
13337
13338 bfd_boolean
13339 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13340 struct bfd_link_info *info)
13341 {
13342 bfd *ibfd = info->input_bfds;
13343
13344 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13345 {
13346 asection *sec;
13347 struct elf_reloc_cookie cookie;
13348
13349 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13350 continue;
13351 sec = ibfd->sections;
13352 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13353 continue;
13354
13355 if (!init_reloc_cookie (&cookie, info, ibfd))
13356 return FALSE;
13357
13358 for (sec = ibfd->sections; sec; sec = sec->next)
13359 {
13360 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13361 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13362 {
13363 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13364 fini_reloc_cookie_rels (&cookie, sec);
13365 }
13366 }
13367 }
13368 return TRUE;
13369 }
13370
13371 /* Do mark and sweep of unused sections. */
13372
13373 bfd_boolean
13374 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13375 {
13376 bfd_boolean ok = TRUE;
13377 bfd *sub;
13378 elf_gc_mark_hook_fn gc_mark_hook;
13379 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13380 struct elf_link_hash_table *htab;
13381
13382 if (!bed->can_gc_sections
13383 || !is_elf_hash_table (info->hash))
13384 {
13385 _bfd_error_handler(_("Warning: gc-sections option ignored"));
13386 return TRUE;
13387 }
13388
13389 bed->gc_keep (info);
13390 htab = elf_hash_table (info);
13391
13392 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13393 at the .eh_frame section if we can mark the FDEs individually. */
13394 for (sub = info->input_bfds;
13395 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13396 sub = sub->link.next)
13397 {
13398 asection *sec;
13399 struct elf_reloc_cookie cookie;
13400
13401 sec = sub->sections;
13402 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13403 continue;
13404 sec = bfd_get_section_by_name (sub, ".eh_frame");
13405 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
13406 {
13407 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
13408 if (elf_section_data (sec)->sec_info
13409 && (sec->flags & SEC_LINKER_CREATED) == 0)
13410 elf_eh_frame_section (sub) = sec;
13411 fini_reloc_cookie_for_section (&cookie, sec);
13412 sec = bfd_get_next_section_by_name (NULL, sec);
13413 }
13414 }
13415
13416 /* Apply transitive closure to the vtable entry usage info. */
13417 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13418 if (!ok)
13419 return FALSE;
13420
13421 /* Kill the vtable relocations that were not used. */
13422 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13423 if (!ok)
13424 return FALSE;
13425
13426 /* Mark dynamically referenced symbols. */
13427 if (htab->dynamic_sections_created || info->gc_keep_exported)
13428 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13429
13430 /* Grovel through relocs to find out who stays ... */
13431 gc_mark_hook = bed->gc_mark_hook;
13432 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13433 {
13434 asection *o;
13435
13436 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13437 || elf_object_id (sub) != elf_hash_table_id (htab)
13438 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13439 continue;
13440
13441 o = sub->sections;
13442 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13443 continue;
13444
13445 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13446 Also treat note sections as a root, if the section is not part
13447 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13448 well as FINI_ARRAY sections for ld -r. */
13449 for (o = sub->sections; o != NULL; o = o->next)
13450 if (!o->gc_mark
13451 && (o->flags & SEC_EXCLUDE) == 0
13452 && ((o->flags & SEC_KEEP) != 0
13453 || (bfd_link_relocatable (info)
13454 && ((elf_section_data (o)->this_hdr.sh_type
13455 == SHT_PREINIT_ARRAY)
13456 || (elf_section_data (o)->this_hdr.sh_type
13457 == SHT_INIT_ARRAY)
13458 || (elf_section_data (o)->this_hdr.sh_type
13459 == SHT_FINI_ARRAY)))
13460 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13461 && elf_next_in_group (o) == NULL )))
13462 {
13463 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13464 return FALSE;
13465 }
13466 }
13467
13468 /* Allow the backend to mark additional target specific sections. */
13469 bed->gc_mark_extra_sections (info, gc_mark_hook);
13470
13471 /* ... and mark SEC_EXCLUDE for those that go. */
13472 return elf_gc_sweep (abfd, info);
13473 }
13474 \f
13475 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13476
13477 bfd_boolean
13478 bfd_elf_gc_record_vtinherit (bfd *abfd,
13479 asection *sec,
13480 struct elf_link_hash_entry *h,
13481 bfd_vma offset)
13482 {
13483 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13484 struct elf_link_hash_entry **search, *child;
13485 size_t extsymcount;
13486 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13487
13488 /* The sh_info field of the symtab header tells us where the
13489 external symbols start. We don't care about the local symbols at
13490 this point. */
13491 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13492 if (!elf_bad_symtab (abfd))
13493 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13494
13495 sym_hashes = elf_sym_hashes (abfd);
13496 sym_hashes_end = sym_hashes + extsymcount;
13497
13498 /* Hunt down the child symbol, which is in this section at the same
13499 offset as the relocation. */
13500 for (search = sym_hashes; search != sym_hashes_end; ++search)
13501 {
13502 if ((child = *search) != NULL
13503 && (child->root.type == bfd_link_hash_defined
13504 || child->root.type == bfd_link_hash_defweak)
13505 && child->root.u.def.section == sec
13506 && child->root.u.def.value == offset)
13507 goto win;
13508 }
13509
13510 /* xgettext:c-format */
13511 _bfd_error_handler (_("%B: %A+%#Lx: No symbol found for INHERIT"),
13512 abfd, sec, offset);
13513 bfd_set_error (bfd_error_invalid_operation);
13514 return FALSE;
13515
13516 win:
13517 if (!child->u2.vtable)
13518 {
13519 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13520 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13521 if (!child->u2.vtable)
13522 return FALSE;
13523 }
13524 if (!h)
13525 {
13526 /* This *should* only be the absolute section. It could potentially
13527 be that someone has defined a non-global vtable though, which
13528 would be bad. It isn't worth paging in the local symbols to be
13529 sure though; that case should simply be handled by the assembler. */
13530
13531 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
13532 }
13533 else
13534 child->u2.vtable->parent = h;
13535
13536 return TRUE;
13537 }
13538
13539 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13540
13541 bfd_boolean
13542 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13543 asection *sec ATTRIBUTE_UNUSED,
13544 struct elf_link_hash_entry *h,
13545 bfd_vma addend)
13546 {
13547 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13548 unsigned int log_file_align = bed->s->log_file_align;
13549
13550 if (!h->u2.vtable)
13551 {
13552 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13553 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13554 if (!h->u2.vtable)
13555 return FALSE;
13556 }
13557
13558 if (addend >= h->u2.vtable->size)
13559 {
13560 size_t size, bytes, file_align;
13561 bfd_boolean *ptr = h->u2.vtable->used;
13562
13563 /* While the symbol is undefined, we have to be prepared to handle
13564 a zero size. */
13565 file_align = 1 << log_file_align;
13566 if (h->root.type == bfd_link_hash_undefined)
13567 size = addend + file_align;
13568 else
13569 {
13570 size = h->size;
13571 if (addend >= size)
13572 {
13573 /* Oops! We've got a reference past the defined end of
13574 the table. This is probably a bug -- shall we warn? */
13575 size = addend + file_align;
13576 }
13577 }
13578 size = (size + file_align - 1) & -file_align;
13579
13580 /* Allocate one extra entry for use as a "done" flag for the
13581 consolidation pass. */
13582 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13583
13584 if (ptr)
13585 {
13586 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
13587
13588 if (ptr != NULL)
13589 {
13590 size_t oldbytes;
13591
13592 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
13593 * sizeof (bfd_boolean));
13594 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13595 }
13596 }
13597 else
13598 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13599
13600 if (ptr == NULL)
13601 return FALSE;
13602
13603 /* And arrange for that done flag to be at index -1. */
13604 h->u2.vtable->used = ptr + 1;
13605 h->u2.vtable->size = size;
13606 }
13607
13608 h->u2.vtable->used[addend >> log_file_align] = TRUE;
13609
13610 return TRUE;
13611 }
13612
13613 /* Map an ELF section header flag to its corresponding string. */
13614 typedef struct
13615 {
13616 char *flag_name;
13617 flagword flag_value;
13618 } elf_flags_to_name_table;
13619
13620 static elf_flags_to_name_table elf_flags_to_names [] =
13621 {
13622 { "SHF_WRITE", SHF_WRITE },
13623 { "SHF_ALLOC", SHF_ALLOC },
13624 { "SHF_EXECINSTR", SHF_EXECINSTR },
13625 { "SHF_MERGE", SHF_MERGE },
13626 { "SHF_STRINGS", SHF_STRINGS },
13627 { "SHF_INFO_LINK", SHF_INFO_LINK},
13628 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13629 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13630 { "SHF_GROUP", SHF_GROUP },
13631 { "SHF_TLS", SHF_TLS },
13632 { "SHF_MASKOS", SHF_MASKOS },
13633 { "SHF_EXCLUDE", SHF_EXCLUDE },
13634 };
13635
13636 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13637 bfd_boolean
13638 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13639 struct flag_info *flaginfo,
13640 asection *section)
13641 {
13642 const bfd_vma sh_flags = elf_section_flags (section);
13643
13644 if (!flaginfo->flags_initialized)
13645 {
13646 bfd *obfd = info->output_bfd;
13647 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13648 struct flag_info_list *tf = flaginfo->flag_list;
13649 int with_hex = 0;
13650 int without_hex = 0;
13651
13652 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13653 {
13654 unsigned i;
13655 flagword (*lookup) (char *);
13656
13657 lookup = bed->elf_backend_lookup_section_flags_hook;
13658 if (lookup != NULL)
13659 {
13660 flagword hexval = (*lookup) ((char *) tf->name);
13661
13662 if (hexval != 0)
13663 {
13664 if (tf->with == with_flags)
13665 with_hex |= hexval;
13666 else if (tf->with == without_flags)
13667 without_hex |= hexval;
13668 tf->valid = TRUE;
13669 continue;
13670 }
13671 }
13672 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13673 {
13674 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13675 {
13676 if (tf->with == with_flags)
13677 with_hex |= elf_flags_to_names[i].flag_value;
13678 else if (tf->with == without_flags)
13679 without_hex |= elf_flags_to_names[i].flag_value;
13680 tf->valid = TRUE;
13681 break;
13682 }
13683 }
13684 if (!tf->valid)
13685 {
13686 info->callbacks->einfo
13687 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13688 return FALSE;
13689 }
13690 }
13691 flaginfo->flags_initialized = TRUE;
13692 flaginfo->only_with_flags |= with_hex;
13693 flaginfo->not_with_flags |= without_hex;
13694 }
13695
13696 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13697 return FALSE;
13698
13699 if ((flaginfo->not_with_flags & sh_flags) != 0)
13700 return FALSE;
13701
13702 return TRUE;
13703 }
13704
13705 struct alloc_got_off_arg {
13706 bfd_vma gotoff;
13707 struct bfd_link_info *info;
13708 };
13709
13710 /* We need a special top-level link routine to convert got reference counts
13711 to real got offsets. */
13712
13713 static bfd_boolean
13714 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13715 {
13716 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13717 bfd *obfd = gofarg->info->output_bfd;
13718 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13719
13720 if (h->got.refcount > 0)
13721 {
13722 h->got.offset = gofarg->gotoff;
13723 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13724 }
13725 else
13726 h->got.offset = (bfd_vma) -1;
13727
13728 return TRUE;
13729 }
13730
13731 /* And an accompanying bit to work out final got entry offsets once
13732 we're done. Should be called from final_link. */
13733
13734 bfd_boolean
13735 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13736 struct bfd_link_info *info)
13737 {
13738 bfd *i;
13739 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13740 bfd_vma gotoff;
13741 struct alloc_got_off_arg gofarg;
13742
13743 BFD_ASSERT (abfd == info->output_bfd);
13744
13745 if (! is_elf_hash_table (info->hash))
13746 return FALSE;
13747
13748 /* The GOT offset is relative to the .got section, but the GOT header is
13749 put into the .got.plt section, if the backend uses it. */
13750 if (bed->want_got_plt)
13751 gotoff = 0;
13752 else
13753 gotoff = bed->got_header_size;
13754
13755 /* Do the local .got entries first. */
13756 for (i = info->input_bfds; i; i = i->link.next)
13757 {
13758 bfd_signed_vma *local_got;
13759 size_t j, locsymcount;
13760 Elf_Internal_Shdr *symtab_hdr;
13761
13762 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13763 continue;
13764
13765 local_got = elf_local_got_refcounts (i);
13766 if (!local_got)
13767 continue;
13768
13769 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13770 if (elf_bad_symtab (i))
13771 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13772 else
13773 locsymcount = symtab_hdr->sh_info;
13774
13775 for (j = 0; j < locsymcount; ++j)
13776 {
13777 if (local_got[j] > 0)
13778 {
13779 local_got[j] = gotoff;
13780 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13781 }
13782 else
13783 local_got[j] = (bfd_vma) -1;
13784 }
13785 }
13786
13787 /* Then the global .got entries. .plt refcounts are handled by
13788 adjust_dynamic_symbol */
13789 gofarg.gotoff = gotoff;
13790 gofarg.info = info;
13791 elf_link_hash_traverse (elf_hash_table (info),
13792 elf_gc_allocate_got_offsets,
13793 &gofarg);
13794 return TRUE;
13795 }
13796
13797 /* Many folk need no more in the way of final link than this, once
13798 got entry reference counting is enabled. */
13799
13800 bfd_boolean
13801 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13802 {
13803 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13804 return FALSE;
13805
13806 /* Invoke the regular ELF backend linker to do all the work. */
13807 return bfd_elf_final_link (abfd, info);
13808 }
13809
13810 bfd_boolean
13811 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13812 {
13813 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13814
13815 if (rcookie->bad_symtab)
13816 rcookie->rel = rcookie->rels;
13817
13818 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13819 {
13820 unsigned long r_symndx;
13821
13822 if (! rcookie->bad_symtab)
13823 if (rcookie->rel->r_offset > offset)
13824 return FALSE;
13825 if (rcookie->rel->r_offset != offset)
13826 continue;
13827
13828 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
13829 if (r_symndx == STN_UNDEF)
13830 return TRUE;
13831
13832 if (r_symndx >= rcookie->locsymcount
13833 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13834 {
13835 struct elf_link_hash_entry *h;
13836
13837 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13838
13839 while (h->root.type == bfd_link_hash_indirect
13840 || h->root.type == bfd_link_hash_warning)
13841 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13842
13843 if ((h->root.type == bfd_link_hash_defined
13844 || h->root.type == bfd_link_hash_defweak)
13845 && (h->root.u.def.section->owner != rcookie->abfd
13846 || h->root.u.def.section->kept_section != NULL
13847 || discarded_section (h->root.u.def.section)))
13848 return TRUE;
13849 }
13850 else
13851 {
13852 /* It's not a relocation against a global symbol,
13853 but it could be a relocation against a local
13854 symbol for a discarded section. */
13855 asection *isec;
13856 Elf_Internal_Sym *isym;
13857
13858 /* Need to: get the symbol; get the section. */
13859 isym = &rcookie->locsyms[r_symndx];
13860 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
13861 if (isec != NULL
13862 && (isec->kept_section != NULL
13863 || discarded_section (isec)))
13864 return TRUE;
13865 }
13866 return FALSE;
13867 }
13868 return FALSE;
13869 }
13870
13871 /* Discard unneeded references to discarded sections.
13872 Returns -1 on error, 1 if any section's size was changed, 0 if
13873 nothing changed. This function assumes that the relocations are in
13874 sorted order, which is true for all known assemblers. */
13875
13876 int
13877 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13878 {
13879 struct elf_reloc_cookie cookie;
13880 asection *o;
13881 bfd *abfd;
13882 int changed = 0;
13883
13884 if (info->traditional_format
13885 || !is_elf_hash_table (info->hash))
13886 return 0;
13887
13888 o = bfd_get_section_by_name (output_bfd, ".stab");
13889 if (o != NULL)
13890 {
13891 asection *i;
13892
13893 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13894 {
13895 if (i->size == 0
13896 || i->reloc_count == 0
13897 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13898 continue;
13899
13900 abfd = i->owner;
13901 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13902 continue;
13903
13904 if (!init_reloc_cookie_for_section (&cookie, info, i))
13905 return -1;
13906
13907 if (_bfd_discard_section_stabs (abfd, i,
13908 elf_section_data (i)->sec_info,
13909 bfd_elf_reloc_symbol_deleted_p,
13910 &cookie))
13911 changed = 1;
13912
13913 fini_reloc_cookie_for_section (&cookie, i);
13914 }
13915 }
13916
13917 o = NULL;
13918 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13919 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
13920 if (o != NULL)
13921 {
13922 asection *i;
13923 int eh_changed = 0;
13924 unsigned int eh_alignment;
13925
13926 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13927 {
13928 if (i->size == 0)
13929 continue;
13930
13931 abfd = i->owner;
13932 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13933 continue;
13934
13935 if (!init_reloc_cookie_for_section (&cookie, info, i))
13936 return -1;
13937
13938 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13939 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
13940 bfd_elf_reloc_symbol_deleted_p,
13941 &cookie))
13942 {
13943 eh_changed = 1;
13944 if (i->size != i->rawsize)
13945 changed = 1;
13946 }
13947
13948 fini_reloc_cookie_for_section (&cookie, i);
13949 }
13950
13951 eh_alignment = 1 << o->alignment_power;
13952 /* Skip over zero terminator, and prevent empty sections from
13953 adding alignment padding at the end. */
13954 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
13955 if (i->size == 0)
13956 i->flags |= SEC_EXCLUDE;
13957 else if (i->size > 4)
13958 break;
13959 /* The last non-empty eh_frame section doesn't need padding. */
13960 if (i != NULL)
13961 i = i->map_tail.s;
13962 /* Any prior sections must pad the last FDE out to the output
13963 section alignment. Otherwise we might have zero padding
13964 between sections, which would be seen as a terminator. */
13965 for (; i != NULL; i = i->map_tail.s)
13966 if (i->size == 4)
13967 /* All but the last zero terminator should have been removed. */
13968 BFD_FAIL ();
13969 else
13970 {
13971 bfd_size_type size
13972 = (i->size + eh_alignment - 1) & -eh_alignment;
13973 if (i->size != size)
13974 {
13975 i->size = size;
13976 changed = 1;
13977 eh_changed = 1;
13978 }
13979 }
13980 if (eh_changed)
13981 elf_link_hash_traverse (elf_hash_table (info),
13982 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
13983 }
13984
13985 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13986 {
13987 const struct elf_backend_data *bed;
13988 asection *s;
13989
13990 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13991 continue;
13992 s = abfd->sections;
13993 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13994 continue;
13995
13996 bed = get_elf_backend_data (abfd);
13997
13998 if (bed->elf_backend_discard_info != NULL)
13999 {
14000 if (!init_reloc_cookie (&cookie, info, abfd))
14001 return -1;
14002
14003 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
14004 changed = 1;
14005
14006 fini_reloc_cookie (&cookie, abfd);
14007 }
14008 }
14009
14010 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14011 _bfd_elf_end_eh_frame_parsing (info);
14012
14013 if (info->eh_frame_hdr_type
14014 && !bfd_link_relocatable (info)
14015 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
14016 changed = 1;
14017
14018 return changed;
14019 }
14020
14021 bfd_boolean
14022 _bfd_elf_section_already_linked (bfd *abfd,
14023 asection *sec,
14024 struct bfd_link_info *info)
14025 {
14026 flagword flags;
14027 const char *name, *key;
14028 struct bfd_section_already_linked *l;
14029 struct bfd_section_already_linked_hash_entry *already_linked_list;
14030
14031 if (sec->output_section == bfd_abs_section_ptr)
14032 return FALSE;
14033
14034 flags = sec->flags;
14035
14036 /* Return if it isn't a linkonce section. A comdat group section
14037 also has SEC_LINK_ONCE set. */
14038 if ((flags & SEC_LINK_ONCE) == 0)
14039 return FALSE;
14040
14041 /* Don't put group member sections on our list of already linked
14042 sections. They are handled as a group via their group section. */
14043 if (elf_sec_group (sec) != NULL)
14044 return FALSE;
14045
14046 /* For a SHT_GROUP section, use the group signature as the key. */
14047 name = sec->name;
14048 if ((flags & SEC_GROUP) != 0
14049 && elf_next_in_group (sec) != NULL
14050 && elf_group_name (elf_next_in_group (sec)) != NULL)
14051 key = elf_group_name (elf_next_in_group (sec));
14052 else
14053 {
14054 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14055 if (CONST_STRNEQ (name, ".gnu.linkonce.")
14056 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14057 key++;
14058 else
14059 /* Must be a user linkonce section that doesn't follow gcc's
14060 naming convention. In this case we won't be matching
14061 single member groups. */
14062 key = name;
14063 }
14064
14065 already_linked_list = bfd_section_already_linked_table_lookup (key);
14066
14067 for (l = already_linked_list->entry; l != NULL; l = l->next)
14068 {
14069 /* We may have 2 different types of sections on the list: group
14070 sections with a signature of <key> (<key> is some string),
14071 and linkonce sections named .gnu.linkonce.<type>.<key>.
14072 Match like sections. LTO plugin sections are an exception.
14073 They are always named .gnu.linkonce.t.<key> and match either
14074 type of section. */
14075 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14076 && ((flags & SEC_GROUP) != 0
14077 || strcmp (name, l->sec->name) == 0))
14078 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
14079 {
14080 /* The section has already been linked. See if we should
14081 issue a warning. */
14082 if (!_bfd_handle_already_linked (sec, l, info))
14083 return FALSE;
14084
14085 if (flags & SEC_GROUP)
14086 {
14087 asection *first = elf_next_in_group (sec);
14088 asection *s = first;
14089
14090 while (s != NULL)
14091 {
14092 s->output_section = bfd_abs_section_ptr;
14093 /* Record which group discards it. */
14094 s->kept_section = l->sec;
14095 s = elf_next_in_group (s);
14096 /* These lists are circular. */
14097 if (s == first)
14098 break;
14099 }
14100 }
14101
14102 return TRUE;
14103 }
14104 }
14105
14106 /* A single member comdat group section may be discarded by a
14107 linkonce section and vice versa. */
14108 if ((flags & SEC_GROUP) != 0)
14109 {
14110 asection *first = elf_next_in_group (sec);
14111
14112 if (first != NULL && elf_next_in_group (first) == first)
14113 /* Check this single member group against linkonce sections. */
14114 for (l = already_linked_list->entry; l != NULL; l = l->next)
14115 if ((l->sec->flags & SEC_GROUP) == 0
14116 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14117 {
14118 first->output_section = bfd_abs_section_ptr;
14119 first->kept_section = l->sec;
14120 sec->output_section = bfd_abs_section_ptr;
14121 break;
14122 }
14123 }
14124 else
14125 /* Check this linkonce section against single member groups. */
14126 for (l = already_linked_list->entry; l != NULL; l = l->next)
14127 if (l->sec->flags & SEC_GROUP)
14128 {
14129 asection *first = elf_next_in_group (l->sec);
14130
14131 if (first != NULL
14132 && elf_next_in_group (first) == first
14133 && bfd_elf_match_symbols_in_sections (first, sec, info))
14134 {
14135 sec->output_section = bfd_abs_section_ptr;
14136 sec->kept_section = first;
14137 break;
14138 }
14139 }
14140
14141 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14142 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14143 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14144 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14145 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14146 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14147 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14148 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14149 The reverse order cannot happen as there is never a bfd with only the
14150 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14151 matter as here were are looking only for cross-bfd sections. */
14152
14153 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14154 for (l = already_linked_list->entry; l != NULL; l = l->next)
14155 if ((l->sec->flags & SEC_GROUP) == 0
14156 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14157 {
14158 if (abfd != l->sec->owner)
14159 sec->output_section = bfd_abs_section_ptr;
14160 break;
14161 }
14162
14163 /* This is the first section with this name. Record it. */
14164 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
14165 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
14166 return sec->output_section == bfd_abs_section_ptr;
14167 }
14168
14169 bfd_boolean
14170 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
14171 {
14172 return sym->st_shndx == SHN_COMMON;
14173 }
14174
14175 unsigned int
14176 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14177 {
14178 return SHN_COMMON;
14179 }
14180
14181 asection *
14182 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14183 {
14184 return bfd_com_section_ptr;
14185 }
14186
14187 bfd_vma
14188 _bfd_elf_default_got_elt_size (bfd *abfd,
14189 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14190 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14191 bfd *ibfd ATTRIBUTE_UNUSED,
14192 unsigned long symndx ATTRIBUTE_UNUSED)
14193 {
14194 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14195 return bed->s->arch_size / 8;
14196 }
14197
14198 /* Routines to support the creation of dynamic relocs. */
14199
14200 /* Returns the name of the dynamic reloc section associated with SEC. */
14201
14202 static const char *
14203 get_dynamic_reloc_section_name (bfd * abfd,
14204 asection * sec,
14205 bfd_boolean is_rela)
14206 {
14207 char *name;
14208 const char *old_name = bfd_get_section_name (NULL, sec);
14209 const char *prefix = is_rela ? ".rela" : ".rel";
14210
14211 if (old_name == NULL)
14212 return NULL;
14213
14214 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
14215 sprintf (name, "%s%s", prefix, old_name);
14216
14217 return name;
14218 }
14219
14220 /* Returns the dynamic reloc section associated with SEC.
14221 If necessary compute the name of the dynamic reloc section based
14222 on SEC's name (looked up in ABFD's string table) and the setting
14223 of IS_RELA. */
14224
14225 asection *
14226 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14227 asection * sec,
14228 bfd_boolean is_rela)
14229 {
14230 asection * reloc_sec = elf_section_data (sec)->sreloc;
14231
14232 if (reloc_sec == NULL)
14233 {
14234 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14235
14236 if (name != NULL)
14237 {
14238 reloc_sec = bfd_get_linker_section (abfd, name);
14239
14240 if (reloc_sec != NULL)
14241 elf_section_data (sec)->sreloc = reloc_sec;
14242 }
14243 }
14244
14245 return reloc_sec;
14246 }
14247
14248 /* Returns the dynamic reloc section associated with SEC. If the
14249 section does not exist it is created and attached to the DYNOBJ
14250 bfd and stored in the SRELOC field of SEC's elf_section_data
14251 structure.
14252
14253 ALIGNMENT is the alignment for the newly created section and
14254 IS_RELA defines whether the name should be .rela.<SEC's name>
14255 or .rel.<SEC's name>. The section name is looked up in the
14256 string table associated with ABFD. */
14257
14258 asection *
14259 _bfd_elf_make_dynamic_reloc_section (asection *sec,
14260 bfd *dynobj,
14261 unsigned int alignment,
14262 bfd *abfd,
14263 bfd_boolean is_rela)
14264 {
14265 asection * reloc_sec = elf_section_data (sec)->sreloc;
14266
14267 if (reloc_sec == NULL)
14268 {
14269 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14270
14271 if (name == NULL)
14272 return NULL;
14273
14274 reloc_sec = bfd_get_linker_section (dynobj, name);
14275
14276 if (reloc_sec == NULL)
14277 {
14278 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14279 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
14280 if ((sec->flags & SEC_ALLOC) != 0)
14281 flags |= SEC_ALLOC | SEC_LOAD;
14282
14283 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
14284 if (reloc_sec != NULL)
14285 {
14286 /* _bfd_elf_get_sec_type_attr chooses a section type by
14287 name. Override as it may be wrong, eg. for a user
14288 section named "auto" we'll get ".relauto" which is
14289 seen to be a .rela section. */
14290 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
14291 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14292 reloc_sec = NULL;
14293 }
14294 }
14295
14296 elf_section_data (sec)->sreloc = reloc_sec;
14297 }
14298
14299 return reloc_sec;
14300 }
14301
14302 /* Copy the ELF symbol type and other attributes for a linker script
14303 assignment from HSRC to HDEST. Generally this should be treated as
14304 if we found a strong non-dynamic definition for HDEST (except that
14305 ld ignores multiple definition errors). */
14306 void
14307 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14308 struct bfd_link_hash_entry *hdest,
14309 struct bfd_link_hash_entry *hsrc)
14310 {
14311 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14312 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14313 Elf_Internal_Sym isym;
14314
14315 ehdest->type = ehsrc->type;
14316 ehdest->target_internal = ehsrc->target_internal;
14317
14318 isym.st_other = ehsrc->other;
14319 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
14320 }
14321
14322 /* Append a RELA relocation REL to section S in BFD. */
14323
14324 void
14325 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14326 {
14327 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14328 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14329 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14330 bed->s->swap_reloca_out (abfd, rel, loc);
14331 }
14332
14333 /* Append a REL relocation REL to section S in BFD. */
14334
14335 void
14336 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14337 {
14338 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14339 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14340 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
14341 bed->s->swap_reloc_out (abfd, rel, loc);
14342 }
14343
14344 /* Define __start, __stop, .startof. or .sizeof. symbol. */
14345
14346 struct bfd_link_hash_entry *
14347 bfd_elf_define_start_stop (struct bfd_link_info *info,
14348 const char *symbol, asection *sec)
14349 {
14350 struct elf_link_hash_entry *h;
14351
14352 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14353 FALSE, FALSE, TRUE);
14354 if (h != NULL
14355 && (h->root.type == bfd_link_hash_undefined
14356 || h->root.type == bfd_link_hash_undefweak
14357 || ((h->ref_regular || h->def_dynamic) && !h->def_regular)))
14358 {
14359 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
14360 h->root.type = bfd_link_hash_defined;
14361 h->root.u.def.section = sec;
14362 h->root.u.def.value = 0;
14363 h->def_regular = 1;
14364 h->def_dynamic = 0;
14365 h->start_stop = 1;
14366 h->u2.start_stop_section = sec;
14367 if (symbol[0] == '.')
14368 {
14369 /* .startof. and .sizeof. symbols are local. */
14370 const struct elf_backend_data *bed;
14371 bed = get_elf_backend_data (info->output_bfd);
14372 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
14373 }
14374 else
14375 {
14376 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14377 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
14378 if (was_dynamic)
14379 bfd_elf_link_record_dynamic_symbol (info, h);
14380 }
14381 return &h->root;
14382 }
14383 return NULL;
14384 }
This page took 0.353708 seconds and 5 git commands to generate.