ELF dynsyms
[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 {
590 h->dynamic = 1;
591 /* NB: If a symbol is made dynamic by --dynamic-list, it has
592 non-IR reference. */
593 h->root.non_ir_ref_dynamic = 1;
594 }
595 }
596
597 /* Record an assignment to a symbol made by a linker script. We need
598 this in case some dynamic object refers to this symbol. */
599
600 bfd_boolean
601 bfd_elf_record_link_assignment (bfd *output_bfd,
602 struct bfd_link_info *info,
603 const char *name,
604 bfd_boolean provide,
605 bfd_boolean hidden)
606 {
607 struct elf_link_hash_entry *h, *hv;
608 struct elf_link_hash_table *htab;
609 const struct elf_backend_data *bed;
610
611 if (!is_elf_hash_table (info->hash))
612 return TRUE;
613
614 htab = elf_hash_table (info);
615 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
616 if (h == NULL)
617 return provide;
618
619 if (h->root.type == bfd_link_hash_warning)
620 h = (struct elf_link_hash_entry *) h->root.u.i.link;
621
622 if (h->versioned == unknown)
623 {
624 /* Set versioned if symbol version is unknown. */
625 char *version = strrchr (name, ELF_VER_CHR);
626 if (version)
627 {
628 if (version > name && version[-1] != ELF_VER_CHR)
629 h->versioned = versioned_hidden;
630 else
631 h->versioned = versioned;
632 }
633 }
634
635 /* Symbols defined in a linker script but not referenced anywhere
636 else will have non_elf set. */
637 if (h->non_elf)
638 {
639 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
640 h->non_elf = 0;
641 }
642
643 switch (h->root.type)
644 {
645 case bfd_link_hash_defined:
646 case bfd_link_hash_defweak:
647 case bfd_link_hash_common:
648 break;
649 case bfd_link_hash_undefweak:
650 case bfd_link_hash_undefined:
651 /* Since we're defining the symbol, don't let it seem to have not
652 been defined. record_dynamic_symbol and size_dynamic_sections
653 may depend on this. */
654 h->root.type = bfd_link_hash_new;
655 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
656 bfd_link_repair_undef_list (&htab->root);
657 break;
658 case bfd_link_hash_new:
659 break;
660 case bfd_link_hash_indirect:
661 /* We had a versioned symbol in a dynamic library. We make the
662 the versioned symbol point to this one. */
663 bed = get_elf_backend_data (output_bfd);
664 hv = h;
665 while (hv->root.type == bfd_link_hash_indirect
666 || hv->root.type == bfd_link_hash_warning)
667 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
668 /* We don't need to update h->root.u since linker will set them
669 later. */
670 h->root.type = bfd_link_hash_undefined;
671 hv->root.type = bfd_link_hash_indirect;
672 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
673 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
674 break;
675 default:
676 BFD_FAIL ();
677 return FALSE;
678 }
679
680 /* If this symbol is being provided by the linker script, and it is
681 currently defined by a dynamic object, but not by a regular
682 object, then mark it as undefined so that the generic linker will
683 force the correct value. */
684 if (provide
685 && h->def_dynamic
686 && !h->def_regular)
687 h->root.type = bfd_link_hash_undefined;
688
689 /* If this symbol is not being provided by the linker script, and it is
690 currently defined by a dynamic object, but not by a regular object,
691 then clear out any version information because the symbol will not be
692 associated with the dynamic object any more. */
693 if (!provide
694 && h->def_dynamic
695 && !h->def_regular)
696 h->verinfo.verdef = NULL;
697
698 /* Make sure this symbol is not garbage collected. */
699 h->mark = 1;
700
701 h->def_regular = 1;
702
703 if (hidden)
704 {
705 bed = get_elf_backend_data (output_bfd);
706 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
707 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
708 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
709 }
710
711 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
712 and executables. */
713 if (!bfd_link_relocatable (info)
714 && h->dynindx != -1
715 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
716 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
717 h->forced_local = 1;
718
719 if ((h->def_dynamic
720 || h->ref_dynamic
721 || bfd_link_dll (info)
722 || elf_hash_table (info)->is_relocatable_executable)
723 && !h->forced_local
724 && h->dynindx == -1)
725 {
726 if (! bfd_elf_link_record_dynamic_symbol (info, h))
727 return FALSE;
728
729 /* If this is a weak defined symbol, and we know a corresponding
730 real symbol from the same dynamic object, make sure the real
731 symbol is also made into a dynamic symbol. */
732 if (h->is_weakalias)
733 {
734 struct elf_link_hash_entry *def = weakdef (h);
735
736 if (def->dynindx == -1
737 && !bfd_elf_link_record_dynamic_symbol (info, def))
738 return FALSE;
739 }
740 }
741
742 return TRUE;
743 }
744
745 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
746 success, and 2 on a failure caused by attempting to record a symbol
747 in a discarded section, eg. a discarded link-once section symbol. */
748
749 int
750 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
751 bfd *input_bfd,
752 long input_indx)
753 {
754 bfd_size_type amt;
755 struct elf_link_local_dynamic_entry *entry;
756 struct elf_link_hash_table *eht;
757 struct elf_strtab_hash *dynstr;
758 size_t dynstr_index;
759 char *name;
760 Elf_External_Sym_Shndx eshndx;
761 char esym[sizeof (Elf64_External_Sym)];
762
763 if (! is_elf_hash_table (info->hash))
764 return 0;
765
766 /* See if the entry exists already. */
767 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
768 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
769 return 1;
770
771 amt = sizeof (*entry);
772 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
773 if (entry == NULL)
774 return 0;
775
776 /* Go find the symbol, so that we can find it's name. */
777 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
778 1, input_indx, &entry->isym, esym, &eshndx))
779 {
780 bfd_release (input_bfd, entry);
781 return 0;
782 }
783
784 if (entry->isym.st_shndx != SHN_UNDEF
785 && entry->isym.st_shndx < SHN_LORESERVE)
786 {
787 asection *s;
788
789 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
790 if (s == NULL || bfd_is_abs_section (s->output_section))
791 {
792 /* We can still bfd_release here as nothing has done another
793 bfd_alloc. We can't do this later in this function. */
794 bfd_release (input_bfd, entry);
795 return 2;
796 }
797 }
798
799 name = (bfd_elf_string_from_elf_section
800 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
801 entry->isym.st_name));
802
803 dynstr = elf_hash_table (info)->dynstr;
804 if (dynstr == NULL)
805 {
806 /* Create a strtab to hold the dynamic symbol names. */
807 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
808 if (dynstr == NULL)
809 return 0;
810 }
811
812 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
813 if (dynstr_index == (size_t) -1)
814 return 0;
815 entry->isym.st_name = dynstr_index;
816
817 eht = elf_hash_table (info);
818
819 entry->next = eht->dynlocal;
820 eht->dynlocal = entry;
821 entry->input_bfd = input_bfd;
822 entry->input_indx = input_indx;
823 eht->dynsymcount++;
824
825 /* Whatever binding the symbol had before, it's now local. */
826 entry->isym.st_info
827 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
828
829 /* The dynindx will be set at the end of size_dynamic_sections. */
830
831 return 1;
832 }
833
834 /* Return the dynindex of a local dynamic symbol. */
835
836 long
837 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
838 bfd *input_bfd,
839 long input_indx)
840 {
841 struct elf_link_local_dynamic_entry *e;
842
843 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
844 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
845 return e->dynindx;
846 return -1;
847 }
848
849 /* This function is used to renumber the dynamic symbols, if some of
850 them are removed because they are marked as local. This is called
851 via elf_link_hash_traverse. */
852
853 static bfd_boolean
854 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
855 void *data)
856 {
857 size_t *count = (size_t *) data;
858
859 if (h->forced_local)
860 return TRUE;
861
862 if (h->dynindx != -1)
863 h->dynindx = ++(*count);
864
865 return TRUE;
866 }
867
868
869 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
870 STB_LOCAL binding. */
871
872 static bfd_boolean
873 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
874 void *data)
875 {
876 size_t *count = (size_t *) data;
877
878 if (!h->forced_local)
879 return TRUE;
880
881 if (h->dynindx != -1)
882 h->dynindx = ++(*count);
883
884 return TRUE;
885 }
886
887 /* Return true if the dynamic symbol for a given section should be
888 omitted when creating a shared library. */
889 bfd_boolean
890 _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
891 struct bfd_link_info *info,
892 asection *p)
893 {
894 struct elf_link_hash_table *htab;
895 asection *ip;
896
897 switch (elf_section_data (p)->this_hdr.sh_type)
898 {
899 case SHT_PROGBITS:
900 case SHT_NOBITS:
901 /* If sh_type is yet undecided, assume it could be
902 SHT_PROGBITS/SHT_NOBITS. */
903 case SHT_NULL:
904 htab = elf_hash_table (info);
905 if (p == htab->tls_sec)
906 return FALSE;
907
908 if (htab->text_index_section != NULL)
909 return p != htab->text_index_section && p != htab->data_index_section;
910
911 return (htab->dynobj != NULL
912 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
913 && ip->output_section == p);
914
915 /* There shouldn't be section relative relocations
916 against any other section. */
917 default:
918 return TRUE;
919 }
920 }
921
922 bfd_boolean
923 _bfd_elf_omit_section_dynsym_all
924 (bfd *output_bfd ATTRIBUTE_UNUSED,
925 struct bfd_link_info *info ATTRIBUTE_UNUSED,
926 asection *p ATTRIBUTE_UNUSED)
927 {
928 return TRUE;
929 }
930
931 /* Assign dynsym indices. In a shared library we generate a section
932 symbol for each output section, which come first. Next come symbols
933 which have been forced to local binding. Then all of the back-end
934 allocated local dynamic syms, followed by the rest of the global
935 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
936 (This prevents the early call before elf_backend_init_index_section
937 and strip_excluded_output_sections setting dynindx for sections
938 that are stripped.) */
939
940 static unsigned long
941 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
942 struct bfd_link_info *info,
943 unsigned long *section_sym_count)
944 {
945 unsigned long dynsymcount = 0;
946 bfd_boolean do_sec = section_sym_count != NULL;
947
948 if (bfd_link_pic (info)
949 || elf_hash_table (info)->is_relocatable_executable)
950 {
951 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
952 asection *p;
953 for (p = output_bfd->sections; p ; p = p->next)
954 if ((p->flags & SEC_EXCLUDE) == 0
955 && (p->flags & SEC_ALLOC) != 0
956 && elf_hash_table (info)->dynamic_relocs
957 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
958 {
959 ++dynsymcount;
960 if (do_sec)
961 elf_section_data (p)->dynindx = dynsymcount;
962 }
963 else if (do_sec)
964 elf_section_data (p)->dynindx = 0;
965 }
966 if (do_sec)
967 *section_sym_count = dynsymcount;
968
969 elf_link_hash_traverse (elf_hash_table (info),
970 elf_link_renumber_local_hash_table_dynsyms,
971 &dynsymcount);
972
973 if (elf_hash_table (info)->dynlocal)
974 {
975 struct elf_link_local_dynamic_entry *p;
976 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
977 p->dynindx = ++dynsymcount;
978 }
979 elf_hash_table (info)->local_dynsymcount = dynsymcount;
980
981 elf_link_hash_traverse (elf_hash_table (info),
982 elf_link_renumber_hash_table_dynsyms,
983 &dynsymcount);
984
985 /* There is an unused NULL entry at the head of the table which we
986 must account for in our count even if the table is empty since it
987 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
988 .dynamic section. */
989 dynsymcount++;
990
991 elf_hash_table (info)->dynsymcount = dynsymcount;
992 return dynsymcount;
993 }
994
995 /* Merge st_other field. */
996
997 static void
998 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
999 const Elf_Internal_Sym *isym, asection *sec,
1000 bfd_boolean definition, bfd_boolean dynamic)
1001 {
1002 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1003
1004 /* If st_other has a processor-specific meaning, specific
1005 code might be needed here. */
1006 if (bed->elf_backend_merge_symbol_attribute)
1007 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
1008 dynamic);
1009
1010 if (!dynamic)
1011 {
1012 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
1013 unsigned hvis = ELF_ST_VISIBILITY (h->other);
1014
1015 /* Keep the most constraining visibility. Leave the remainder
1016 of the st_other field to elf_backend_merge_symbol_attribute. */
1017 if (symvis - 1 < hvis - 1)
1018 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1019 }
1020 else if (definition
1021 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
1022 && (sec->flags & SEC_READONLY) == 0)
1023 h->protected_def = 1;
1024 }
1025
1026 /* This function is called when we want to merge a new symbol with an
1027 existing symbol. It handles the various cases which arise when we
1028 find a definition in a dynamic object, or when there is already a
1029 definition in a dynamic object. The new symbol is described by
1030 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1031 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1032 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1033 of an old common symbol. We set OVERRIDE if the old symbol is
1034 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1035 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1036 to change. By OK to change, we mean that we shouldn't warn if the
1037 type or size does change. */
1038
1039 static bfd_boolean
1040 _bfd_elf_merge_symbol (bfd *abfd,
1041 struct bfd_link_info *info,
1042 const char *name,
1043 Elf_Internal_Sym *sym,
1044 asection **psec,
1045 bfd_vma *pvalue,
1046 struct elf_link_hash_entry **sym_hash,
1047 bfd **poldbfd,
1048 bfd_boolean *pold_weak,
1049 unsigned int *pold_alignment,
1050 bfd_boolean *skip,
1051 bfd_boolean *override,
1052 bfd_boolean *type_change_ok,
1053 bfd_boolean *size_change_ok,
1054 bfd_boolean *matched)
1055 {
1056 asection *sec, *oldsec;
1057 struct elf_link_hash_entry *h;
1058 struct elf_link_hash_entry *hi;
1059 struct elf_link_hash_entry *flip;
1060 int bind;
1061 bfd *oldbfd;
1062 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1063 bfd_boolean newweak, oldweak, newfunc, oldfunc;
1064 const struct elf_backend_data *bed;
1065 char *new_version;
1066 bfd_boolean default_sym = *matched;
1067
1068 *skip = FALSE;
1069 *override = FALSE;
1070
1071 sec = *psec;
1072 bind = ELF_ST_BIND (sym->st_info);
1073
1074 if (! bfd_is_und_section (sec))
1075 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1076 else
1077 h = ((struct elf_link_hash_entry *)
1078 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1079 if (h == NULL)
1080 return FALSE;
1081 *sym_hash = h;
1082
1083 bed = get_elf_backend_data (abfd);
1084
1085 /* NEW_VERSION is the symbol version of the new symbol. */
1086 if (h->versioned != unversioned)
1087 {
1088 /* Symbol version is unknown or versioned. */
1089 new_version = strrchr (name, ELF_VER_CHR);
1090 if (new_version)
1091 {
1092 if (h->versioned == unknown)
1093 {
1094 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1095 h->versioned = versioned_hidden;
1096 else
1097 h->versioned = versioned;
1098 }
1099 new_version += 1;
1100 if (new_version[0] == '\0')
1101 new_version = NULL;
1102 }
1103 else
1104 h->versioned = unversioned;
1105 }
1106 else
1107 new_version = NULL;
1108
1109 /* For merging, we only care about real symbols. But we need to make
1110 sure that indirect symbol dynamic flags are updated. */
1111 hi = h;
1112 while (h->root.type == bfd_link_hash_indirect
1113 || h->root.type == bfd_link_hash_warning)
1114 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1115
1116 if (!*matched)
1117 {
1118 if (hi == h || h->root.type == bfd_link_hash_new)
1119 *matched = TRUE;
1120 else
1121 {
1122 /* OLD_HIDDEN is true if the existing symbol is only visible
1123 to the symbol with the same symbol version. NEW_HIDDEN is
1124 true if the new symbol is only visible to the symbol with
1125 the same symbol version. */
1126 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1127 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1128 if (!old_hidden && !new_hidden)
1129 /* The new symbol matches the existing symbol if both
1130 aren't hidden. */
1131 *matched = TRUE;
1132 else
1133 {
1134 /* OLD_VERSION is the symbol version of the existing
1135 symbol. */
1136 char *old_version;
1137
1138 if (h->versioned >= versioned)
1139 old_version = strrchr (h->root.root.string,
1140 ELF_VER_CHR) + 1;
1141 else
1142 old_version = NULL;
1143
1144 /* The new symbol matches the existing symbol if they
1145 have the same symbol version. */
1146 *matched = (old_version == new_version
1147 || (old_version != NULL
1148 && new_version != NULL
1149 && strcmp (old_version, new_version) == 0));
1150 }
1151 }
1152 }
1153
1154 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1155 existing symbol. */
1156
1157 oldbfd = NULL;
1158 oldsec = NULL;
1159 switch (h->root.type)
1160 {
1161 default:
1162 break;
1163
1164 case bfd_link_hash_undefined:
1165 case bfd_link_hash_undefweak:
1166 oldbfd = h->root.u.undef.abfd;
1167 break;
1168
1169 case bfd_link_hash_defined:
1170 case bfd_link_hash_defweak:
1171 oldbfd = h->root.u.def.section->owner;
1172 oldsec = h->root.u.def.section;
1173 break;
1174
1175 case bfd_link_hash_common:
1176 oldbfd = h->root.u.c.p->section->owner;
1177 oldsec = h->root.u.c.p->section;
1178 if (pold_alignment)
1179 *pold_alignment = h->root.u.c.p->alignment_power;
1180 break;
1181 }
1182 if (poldbfd && *poldbfd == NULL)
1183 *poldbfd = oldbfd;
1184
1185 /* Differentiate strong and weak symbols. */
1186 newweak = bind == STB_WEAK;
1187 oldweak = (h->root.type == bfd_link_hash_defweak
1188 || h->root.type == bfd_link_hash_undefweak);
1189 if (pold_weak)
1190 *pold_weak = oldweak;
1191
1192 /* We have to check it for every instance since the first few may be
1193 references and not all compilers emit symbol type for undefined
1194 symbols. */
1195 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1196
1197 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1198 respectively, is from a dynamic object. */
1199
1200 newdyn = (abfd->flags & DYNAMIC) != 0;
1201
1202 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1203 syms and defined syms in dynamic libraries respectively.
1204 ref_dynamic on the other hand can be set for a symbol defined in
1205 a dynamic library, and def_dynamic may not be set; When the
1206 definition in a dynamic lib is overridden by a definition in the
1207 executable use of the symbol in the dynamic lib becomes a
1208 reference to the executable symbol. */
1209 if (newdyn)
1210 {
1211 if (bfd_is_und_section (sec))
1212 {
1213 if (bind != STB_WEAK)
1214 {
1215 h->ref_dynamic_nonweak = 1;
1216 hi->ref_dynamic_nonweak = 1;
1217 }
1218 }
1219 else
1220 {
1221 /* Update the existing symbol only if they match. */
1222 if (*matched)
1223 h->dynamic_def = 1;
1224 hi->dynamic_def = 1;
1225 }
1226 }
1227
1228 /* If we just created the symbol, mark it as being an ELF symbol.
1229 Other than that, there is nothing to do--there is no merge issue
1230 with a newly defined symbol--so we just return. */
1231
1232 if (h->root.type == bfd_link_hash_new)
1233 {
1234 h->non_elf = 0;
1235 return TRUE;
1236 }
1237
1238 /* In cases involving weak versioned symbols, we may wind up trying
1239 to merge a symbol with itself. Catch that here, to avoid the
1240 confusion that results if we try to override a symbol with
1241 itself. The additional tests catch cases like
1242 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1243 dynamic object, which we do want to handle here. */
1244 if (abfd == oldbfd
1245 && (newweak || oldweak)
1246 && ((abfd->flags & DYNAMIC) == 0
1247 || !h->def_regular))
1248 return TRUE;
1249
1250 olddyn = FALSE;
1251 if (oldbfd != NULL)
1252 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1253 else if (oldsec != NULL)
1254 {
1255 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1256 indices used by MIPS ELF. */
1257 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1258 }
1259
1260 /* Handle a case where plugin_notice won't be called and thus won't
1261 set the non_ir_ref flags on the first pass over symbols. */
1262 if (oldbfd != NULL
1263 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1264 && newdyn != olddyn)
1265 {
1266 h->root.non_ir_ref_dynamic = TRUE;
1267 hi->root.non_ir_ref_dynamic = TRUE;
1268 }
1269
1270 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1271 respectively, appear to be a definition rather than reference. */
1272
1273 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1274
1275 olddef = (h->root.type != bfd_link_hash_undefined
1276 && h->root.type != bfd_link_hash_undefweak
1277 && h->root.type != bfd_link_hash_common);
1278
1279 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1280 respectively, appear to be a function. */
1281
1282 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1283 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1284
1285 oldfunc = (h->type != STT_NOTYPE
1286 && bed->is_function_type (h->type));
1287
1288 if (!(newfunc && oldfunc)
1289 && ELF_ST_TYPE (sym->st_info) != h->type
1290 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1291 && h->type != STT_NOTYPE
1292 && (newdef || bfd_is_com_section (sec))
1293 && (olddef || h->root.type == bfd_link_hash_common))
1294 {
1295 /* If creating a default indirect symbol ("foo" or "foo@") from
1296 a dynamic versioned definition ("foo@@") skip doing so if
1297 there is an existing regular definition with a different
1298 type. We don't want, for example, a "time" variable in the
1299 executable overriding a "time" function in a shared library. */
1300 if (newdyn
1301 && !olddyn)
1302 {
1303 *skip = TRUE;
1304 return TRUE;
1305 }
1306
1307 /* When adding a symbol from a regular object file after we have
1308 created indirect symbols, undo the indirection and any
1309 dynamic state. */
1310 if (hi != h
1311 && !newdyn
1312 && olddyn)
1313 {
1314 h = hi;
1315 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1316 h->forced_local = 0;
1317 h->ref_dynamic = 0;
1318 h->def_dynamic = 0;
1319 h->dynamic_def = 0;
1320 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1321 {
1322 h->root.type = bfd_link_hash_undefined;
1323 h->root.u.undef.abfd = abfd;
1324 }
1325 else
1326 {
1327 h->root.type = bfd_link_hash_new;
1328 h->root.u.undef.abfd = NULL;
1329 }
1330 return TRUE;
1331 }
1332 }
1333
1334 /* Check TLS symbols. We don't check undefined symbols introduced
1335 by "ld -u" which have no type (and oldbfd NULL), and we don't
1336 check symbols from plugins because they also have no type. */
1337 if (oldbfd != NULL
1338 && (oldbfd->flags & BFD_PLUGIN) == 0
1339 && (abfd->flags & BFD_PLUGIN) == 0
1340 && ELF_ST_TYPE (sym->st_info) != h->type
1341 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1342 {
1343 bfd *ntbfd, *tbfd;
1344 bfd_boolean ntdef, tdef;
1345 asection *ntsec, *tsec;
1346
1347 if (h->type == STT_TLS)
1348 {
1349 ntbfd = abfd;
1350 ntsec = sec;
1351 ntdef = newdef;
1352 tbfd = oldbfd;
1353 tsec = oldsec;
1354 tdef = olddef;
1355 }
1356 else
1357 {
1358 ntbfd = oldbfd;
1359 ntsec = oldsec;
1360 ntdef = olddef;
1361 tbfd = abfd;
1362 tsec = sec;
1363 tdef = newdef;
1364 }
1365
1366 if (tdef && ntdef)
1367 _bfd_error_handler
1368 /* xgettext:c-format */
1369 (_("%s: TLS definition in %pB section %pA "
1370 "mismatches non-TLS definition in %pB section %pA"),
1371 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1372 else if (!tdef && !ntdef)
1373 _bfd_error_handler
1374 /* xgettext:c-format */
1375 (_("%s: TLS reference in %pB "
1376 "mismatches non-TLS reference in %pB"),
1377 h->root.root.string, tbfd, ntbfd);
1378 else if (tdef)
1379 _bfd_error_handler
1380 /* xgettext:c-format */
1381 (_("%s: TLS definition in %pB section %pA "
1382 "mismatches non-TLS reference in %pB"),
1383 h->root.root.string, tbfd, tsec, ntbfd);
1384 else
1385 _bfd_error_handler
1386 /* xgettext:c-format */
1387 (_("%s: TLS reference in %pB "
1388 "mismatches non-TLS definition in %pB section %pA"),
1389 h->root.root.string, tbfd, ntbfd, ntsec);
1390
1391 bfd_set_error (bfd_error_bad_value);
1392 return FALSE;
1393 }
1394
1395 /* If the old symbol has non-default visibility, we ignore the new
1396 definition from a dynamic object. */
1397 if (newdyn
1398 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1399 && !bfd_is_und_section (sec))
1400 {
1401 *skip = TRUE;
1402 /* Make sure this symbol is dynamic. */
1403 h->ref_dynamic = 1;
1404 hi->ref_dynamic = 1;
1405 /* A protected symbol has external availability. Make sure it is
1406 recorded as dynamic.
1407
1408 FIXME: Should we check type and size for protected symbol? */
1409 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1410 return bfd_elf_link_record_dynamic_symbol (info, h);
1411 else
1412 return TRUE;
1413 }
1414 else if (!newdyn
1415 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1416 && h->def_dynamic)
1417 {
1418 /* If the new symbol with non-default visibility comes from a
1419 relocatable file and the old definition comes from a dynamic
1420 object, we remove the old definition. */
1421 if (hi->root.type == bfd_link_hash_indirect)
1422 {
1423 /* Handle the case where the old dynamic definition is
1424 default versioned. We need to copy the symbol info from
1425 the symbol with default version to the normal one if it
1426 was referenced before. */
1427 if (h->ref_regular)
1428 {
1429 hi->root.type = h->root.type;
1430 h->root.type = bfd_link_hash_indirect;
1431 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1432
1433 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1434 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1435 {
1436 /* If the new symbol is hidden or internal, completely undo
1437 any dynamic link state. */
1438 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1439 h->forced_local = 0;
1440 h->ref_dynamic = 0;
1441 }
1442 else
1443 h->ref_dynamic = 1;
1444
1445 h->def_dynamic = 0;
1446 /* FIXME: Should we check type and size for protected symbol? */
1447 h->size = 0;
1448 h->type = 0;
1449
1450 h = hi;
1451 }
1452 else
1453 h = hi;
1454 }
1455
1456 /* If the old symbol was undefined before, then it will still be
1457 on the undefs list. If the new symbol is undefined or
1458 common, we can't make it bfd_link_hash_new here, because new
1459 undefined or common symbols will be added to the undefs list
1460 by _bfd_generic_link_add_one_symbol. Symbols may not be
1461 added twice to the undefs list. Also, if the new symbol is
1462 undefweak then we don't want to lose the strong undef. */
1463 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1464 {
1465 h->root.type = bfd_link_hash_undefined;
1466 h->root.u.undef.abfd = abfd;
1467 }
1468 else
1469 {
1470 h->root.type = bfd_link_hash_new;
1471 h->root.u.undef.abfd = NULL;
1472 }
1473
1474 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1475 {
1476 /* If the new symbol is hidden or internal, completely undo
1477 any dynamic link state. */
1478 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1479 h->forced_local = 0;
1480 h->ref_dynamic = 0;
1481 }
1482 else
1483 h->ref_dynamic = 1;
1484 h->def_dynamic = 0;
1485 /* FIXME: Should we check type and size for protected symbol? */
1486 h->size = 0;
1487 h->type = 0;
1488 return TRUE;
1489 }
1490
1491 /* If a new weak symbol definition comes from a regular file and the
1492 old symbol comes from a dynamic library, we treat the new one as
1493 strong. Similarly, an old weak symbol definition from a regular
1494 file is treated as strong when the new symbol comes from a dynamic
1495 library. Further, an old weak symbol from a dynamic library is
1496 treated as strong if the new symbol is from a dynamic library.
1497 This reflects the way glibc's ld.so works.
1498
1499 Also allow a weak symbol to override a linker script symbol
1500 defined by an early pass over the script. This is done so the
1501 linker knows the symbol is defined in an object file, for the
1502 DEFINED script function.
1503
1504 Do this before setting *type_change_ok or *size_change_ok so that
1505 we warn properly when dynamic library symbols are overridden. */
1506
1507 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1508 newweak = FALSE;
1509 if (olddef && newdyn)
1510 oldweak = FALSE;
1511
1512 /* Allow changes between different types of function symbol. */
1513 if (newfunc && oldfunc)
1514 *type_change_ok = TRUE;
1515
1516 /* It's OK to change the type if either the existing symbol or the
1517 new symbol is weak. A type change is also OK if the old symbol
1518 is undefined and the new symbol is defined. */
1519
1520 if (oldweak
1521 || newweak
1522 || (newdef
1523 && h->root.type == bfd_link_hash_undefined))
1524 *type_change_ok = TRUE;
1525
1526 /* It's OK to change the size if either the existing symbol or the
1527 new symbol is weak, or if the old symbol is undefined. */
1528
1529 if (*type_change_ok
1530 || h->root.type == bfd_link_hash_undefined)
1531 *size_change_ok = TRUE;
1532
1533 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1534 symbol, respectively, appears to be a common symbol in a dynamic
1535 object. If a symbol appears in an uninitialized section, and is
1536 not weak, and is not a function, then it may be a common symbol
1537 which was resolved when the dynamic object was created. We want
1538 to treat such symbols specially, because they raise special
1539 considerations when setting the symbol size: if the symbol
1540 appears as a common symbol in a regular object, and the size in
1541 the regular object is larger, we must make sure that we use the
1542 larger size. This problematic case can always be avoided in C,
1543 but it must be handled correctly when using Fortran shared
1544 libraries.
1545
1546 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1547 likewise for OLDDYNCOMMON and OLDDEF.
1548
1549 Note that this test is just a heuristic, and that it is quite
1550 possible to have an uninitialized symbol in a shared object which
1551 is really a definition, rather than a common symbol. This could
1552 lead to some minor confusion when the symbol really is a common
1553 symbol in some regular object. However, I think it will be
1554 harmless. */
1555
1556 if (newdyn
1557 && newdef
1558 && !newweak
1559 && (sec->flags & SEC_ALLOC) != 0
1560 && (sec->flags & SEC_LOAD) == 0
1561 && sym->st_size > 0
1562 && !newfunc)
1563 newdyncommon = TRUE;
1564 else
1565 newdyncommon = FALSE;
1566
1567 if (olddyn
1568 && olddef
1569 && h->root.type == bfd_link_hash_defined
1570 && h->def_dynamic
1571 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1572 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1573 && h->size > 0
1574 && !oldfunc)
1575 olddyncommon = TRUE;
1576 else
1577 olddyncommon = FALSE;
1578
1579 /* We now know everything about the old and new symbols. We ask the
1580 backend to check if we can merge them. */
1581 if (bed->merge_symbol != NULL)
1582 {
1583 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1584 return FALSE;
1585 sec = *psec;
1586 }
1587
1588 /* There are multiple definitions of a normal symbol. Skip the
1589 default symbol as well as definition from an IR object. */
1590 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1591 && !default_sym && h->def_regular
1592 && !(oldbfd != NULL
1593 && (oldbfd->flags & BFD_PLUGIN) != 0
1594 && (abfd->flags & BFD_PLUGIN) == 0))
1595 {
1596 /* Handle a multiple definition. */
1597 (*info->callbacks->multiple_definition) (info, &h->root,
1598 abfd, sec, *pvalue);
1599 *skip = TRUE;
1600 return TRUE;
1601 }
1602
1603 /* If both the old and the new symbols look like common symbols in a
1604 dynamic object, set the size of the symbol to the larger of the
1605 two. */
1606
1607 if (olddyncommon
1608 && newdyncommon
1609 && sym->st_size != h->size)
1610 {
1611 /* Since we think we have two common symbols, issue a multiple
1612 common warning if desired. Note that we only warn if the
1613 size is different. If the size is the same, we simply let
1614 the old symbol override the new one as normally happens with
1615 symbols defined in dynamic objects. */
1616
1617 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1618 bfd_link_hash_common, sym->st_size);
1619 if (sym->st_size > h->size)
1620 h->size = sym->st_size;
1621
1622 *size_change_ok = TRUE;
1623 }
1624
1625 /* If we are looking at a dynamic object, and we have found a
1626 definition, we need to see if the symbol was already defined by
1627 some other object. If so, we want to use the existing
1628 definition, and we do not want to report a multiple symbol
1629 definition error; we do this by clobbering *PSEC to be
1630 bfd_und_section_ptr.
1631
1632 We treat a common symbol as a definition if the symbol in the
1633 shared library is a function, since common symbols always
1634 represent variables; this can cause confusion in principle, but
1635 any such confusion would seem to indicate an erroneous program or
1636 shared library. We also permit a common symbol in a regular
1637 object to override a weak symbol in a shared object. */
1638
1639 if (newdyn
1640 && newdef
1641 && (olddef
1642 || (h->root.type == bfd_link_hash_common
1643 && (newweak || newfunc))))
1644 {
1645 *override = TRUE;
1646 newdef = FALSE;
1647 newdyncommon = FALSE;
1648
1649 *psec = sec = bfd_und_section_ptr;
1650 *size_change_ok = TRUE;
1651
1652 /* If we get here when the old symbol is a common symbol, then
1653 we are explicitly letting it override a weak symbol or
1654 function in a dynamic object, and we don't want to warn about
1655 a type change. If the old symbol is a defined symbol, a type
1656 change warning may still be appropriate. */
1657
1658 if (h->root.type == bfd_link_hash_common)
1659 *type_change_ok = TRUE;
1660 }
1661
1662 /* Handle the special case of an old common symbol merging with a
1663 new symbol which looks like a common symbol in a shared object.
1664 We change *PSEC and *PVALUE to make the new symbol look like a
1665 common symbol, and let _bfd_generic_link_add_one_symbol do the
1666 right thing. */
1667
1668 if (newdyncommon
1669 && h->root.type == bfd_link_hash_common)
1670 {
1671 *override = TRUE;
1672 newdef = FALSE;
1673 newdyncommon = FALSE;
1674 *pvalue = sym->st_size;
1675 *psec = sec = bed->common_section (oldsec);
1676 *size_change_ok = TRUE;
1677 }
1678
1679 /* Skip weak definitions of symbols that are already defined. */
1680 if (newdef && olddef && newweak)
1681 {
1682 /* Don't skip new non-IR weak syms. */
1683 if (!(oldbfd != NULL
1684 && (oldbfd->flags & BFD_PLUGIN) != 0
1685 && (abfd->flags & BFD_PLUGIN) == 0))
1686 {
1687 newdef = FALSE;
1688 *skip = TRUE;
1689 }
1690
1691 /* Merge st_other. If the symbol already has a dynamic index,
1692 but visibility says it should not be visible, turn it into a
1693 local symbol. */
1694 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1695 if (h->dynindx != -1)
1696 switch (ELF_ST_VISIBILITY (h->other))
1697 {
1698 case STV_INTERNAL:
1699 case STV_HIDDEN:
1700 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1701 break;
1702 }
1703 }
1704
1705 /* If the old symbol is from a dynamic object, and the new symbol is
1706 a definition which is not from a dynamic object, then the new
1707 symbol overrides the old symbol. Symbols from regular files
1708 always take precedence over symbols from dynamic objects, even if
1709 they are defined after the dynamic object in the link.
1710
1711 As above, we again permit a common symbol in a regular object to
1712 override a definition in a shared object if the shared object
1713 symbol is a function or is weak. */
1714
1715 flip = NULL;
1716 if (!newdyn
1717 && (newdef
1718 || (bfd_is_com_section (sec)
1719 && (oldweak || oldfunc)))
1720 && olddyn
1721 && olddef
1722 && h->def_dynamic)
1723 {
1724 /* Change the hash table entry to undefined, and let
1725 _bfd_generic_link_add_one_symbol do the right thing with the
1726 new definition. */
1727
1728 h->root.type = bfd_link_hash_undefined;
1729 h->root.u.undef.abfd = h->root.u.def.section->owner;
1730 *size_change_ok = TRUE;
1731
1732 olddef = FALSE;
1733 olddyncommon = FALSE;
1734
1735 /* We again permit a type change when a common symbol may be
1736 overriding a function. */
1737
1738 if (bfd_is_com_section (sec))
1739 {
1740 if (oldfunc)
1741 {
1742 /* If a common symbol overrides a function, make sure
1743 that it isn't defined dynamically nor has type
1744 function. */
1745 h->def_dynamic = 0;
1746 h->type = STT_NOTYPE;
1747 }
1748 *type_change_ok = TRUE;
1749 }
1750
1751 if (hi->root.type == bfd_link_hash_indirect)
1752 flip = hi;
1753 else
1754 /* This union may have been set to be non-NULL when this symbol
1755 was seen in a dynamic object. We must force the union to be
1756 NULL, so that it is correct for a regular symbol. */
1757 h->verinfo.vertree = NULL;
1758 }
1759
1760 /* Handle the special case of a new common symbol merging with an
1761 old symbol that looks like it might be a common symbol defined in
1762 a shared object. Note that we have already handled the case in
1763 which a new common symbol should simply override the definition
1764 in the shared library. */
1765
1766 if (! newdyn
1767 && bfd_is_com_section (sec)
1768 && olddyncommon)
1769 {
1770 /* It would be best if we could set the hash table entry to a
1771 common symbol, but we don't know what to use for the section
1772 or the alignment. */
1773 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1774 bfd_link_hash_common, sym->st_size);
1775
1776 /* If the presumed common symbol in the dynamic object is
1777 larger, pretend that the new symbol has its size. */
1778
1779 if (h->size > *pvalue)
1780 *pvalue = h->size;
1781
1782 /* We need to remember the alignment required by the symbol
1783 in the dynamic object. */
1784 BFD_ASSERT (pold_alignment);
1785 *pold_alignment = h->root.u.def.section->alignment_power;
1786
1787 olddef = FALSE;
1788 olddyncommon = FALSE;
1789
1790 h->root.type = bfd_link_hash_undefined;
1791 h->root.u.undef.abfd = h->root.u.def.section->owner;
1792
1793 *size_change_ok = TRUE;
1794 *type_change_ok = TRUE;
1795
1796 if (hi->root.type == bfd_link_hash_indirect)
1797 flip = hi;
1798 else
1799 h->verinfo.vertree = NULL;
1800 }
1801
1802 if (flip != NULL)
1803 {
1804 /* Handle the case where we had a versioned symbol in a dynamic
1805 library and now find a definition in a normal object. In this
1806 case, we make the versioned symbol point to the normal one. */
1807 flip->root.type = h->root.type;
1808 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1809 h->root.type = bfd_link_hash_indirect;
1810 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1811 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1812 if (h->def_dynamic)
1813 {
1814 h->def_dynamic = 0;
1815 flip->ref_dynamic = 1;
1816 }
1817 }
1818
1819 return TRUE;
1820 }
1821
1822 /* This function is called to create an indirect symbol from the
1823 default for the symbol with the default version if needed. The
1824 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1825 set DYNSYM if the new indirect symbol is dynamic. */
1826
1827 static bfd_boolean
1828 _bfd_elf_add_default_symbol (bfd *abfd,
1829 struct bfd_link_info *info,
1830 struct elf_link_hash_entry *h,
1831 const char *name,
1832 Elf_Internal_Sym *sym,
1833 asection *sec,
1834 bfd_vma value,
1835 bfd **poldbfd,
1836 bfd_boolean *dynsym)
1837 {
1838 bfd_boolean type_change_ok;
1839 bfd_boolean size_change_ok;
1840 bfd_boolean skip;
1841 char *shortname;
1842 struct elf_link_hash_entry *hi;
1843 struct bfd_link_hash_entry *bh;
1844 const struct elf_backend_data *bed;
1845 bfd_boolean collect;
1846 bfd_boolean dynamic;
1847 bfd_boolean override;
1848 char *p;
1849 size_t len, shortlen;
1850 asection *tmp_sec;
1851 bfd_boolean matched;
1852
1853 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1854 return TRUE;
1855
1856 /* If this symbol has a version, and it is the default version, we
1857 create an indirect symbol from the default name to the fully
1858 decorated name. This will cause external references which do not
1859 specify a version to be bound to this version of the symbol. */
1860 p = strchr (name, ELF_VER_CHR);
1861 if (h->versioned == unknown)
1862 {
1863 if (p == NULL)
1864 {
1865 h->versioned = unversioned;
1866 return TRUE;
1867 }
1868 else
1869 {
1870 if (p[1] != ELF_VER_CHR)
1871 {
1872 h->versioned = versioned_hidden;
1873 return TRUE;
1874 }
1875 else
1876 h->versioned = versioned;
1877 }
1878 }
1879 else
1880 {
1881 /* PR ld/19073: We may see an unversioned definition after the
1882 default version. */
1883 if (p == NULL)
1884 return TRUE;
1885 }
1886
1887 bed = get_elf_backend_data (abfd);
1888 collect = bed->collect;
1889 dynamic = (abfd->flags & DYNAMIC) != 0;
1890
1891 shortlen = p - name;
1892 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1893 if (shortname == NULL)
1894 return FALSE;
1895 memcpy (shortname, name, shortlen);
1896 shortname[shortlen] = '\0';
1897
1898 /* We are going to create a new symbol. Merge it with any existing
1899 symbol with this name. For the purposes of the merge, act as
1900 though we were defining the symbol we just defined, although we
1901 actually going to define an indirect symbol. */
1902 type_change_ok = FALSE;
1903 size_change_ok = FALSE;
1904 matched = TRUE;
1905 tmp_sec = sec;
1906 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1907 &hi, poldbfd, NULL, NULL, &skip, &override,
1908 &type_change_ok, &size_change_ok, &matched))
1909 return FALSE;
1910
1911 if (skip)
1912 goto nondefault;
1913
1914 if (hi->def_regular)
1915 {
1916 /* If the undecorated symbol will have a version added by a
1917 script different to H, then don't indirect to/from the
1918 undecorated symbol. This isn't ideal because we may not yet
1919 have seen symbol versions, if given by a script on the
1920 command line rather than via --version-script. */
1921 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1922 {
1923 bfd_boolean hide;
1924
1925 hi->verinfo.vertree
1926 = bfd_find_version_for_sym (info->version_info,
1927 hi->root.root.string, &hide);
1928 if (hi->verinfo.vertree != NULL && hide)
1929 {
1930 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1931 goto nondefault;
1932 }
1933 }
1934 if (hi->verinfo.vertree != NULL
1935 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1936 goto nondefault;
1937 }
1938
1939 if (! override)
1940 {
1941 /* Add the default symbol if not performing a relocatable link. */
1942 if (! bfd_link_relocatable (info))
1943 {
1944 bh = &hi->root;
1945 if (! (_bfd_generic_link_add_one_symbol
1946 (info, abfd, shortname, BSF_INDIRECT,
1947 bfd_ind_section_ptr,
1948 0, name, FALSE, collect, &bh)))
1949 return FALSE;
1950 hi = (struct elf_link_hash_entry *) bh;
1951 }
1952 }
1953 else
1954 {
1955 /* In this case the symbol named SHORTNAME is overriding the
1956 indirect symbol we want to add. We were planning on making
1957 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1958 is the name without a version. NAME is the fully versioned
1959 name, and it is the default version.
1960
1961 Overriding means that we already saw a definition for the
1962 symbol SHORTNAME in a regular object, and it is overriding
1963 the symbol defined in the dynamic object.
1964
1965 When this happens, we actually want to change NAME, the
1966 symbol we just added, to refer to SHORTNAME. This will cause
1967 references to NAME in the shared object to become references
1968 to SHORTNAME in the regular object. This is what we expect
1969 when we override a function in a shared object: that the
1970 references in the shared object will be mapped to the
1971 definition in the regular object. */
1972
1973 while (hi->root.type == bfd_link_hash_indirect
1974 || hi->root.type == bfd_link_hash_warning)
1975 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1976
1977 h->root.type = bfd_link_hash_indirect;
1978 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1979 if (h->def_dynamic)
1980 {
1981 h->def_dynamic = 0;
1982 hi->ref_dynamic = 1;
1983 if (hi->ref_regular
1984 || hi->def_regular)
1985 {
1986 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1987 return FALSE;
1988 }
1989 }
1990
1991 /* Now set HI to H, so that the following code will set the
1992 other fields correctly. */
1993 hi = h;
1994 }
1995
1996 /* Check if HI is a warning symbol. */
1997 if (hi->root.type == bfd_link_hash_warning)
1998 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1999
2000 /* If there is a duplicate definition somewhere, then HI may not
2001 point to an indirect symbol. We will have reported an error to
2002 the user in that case. */
2003
2004 if (hi->root.type == bfd_link_hash_indirect)
2005 {
2006 struct elf_link_hash_entry *ht;
2007
2008 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2009 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2010
2011 /* A reference to the SHORTNAME symbol from a dynamic library
2012 will be satisfied by the versioned symbol at runtime. In
2013 effect, we have a reference to the versioned symbol. */
2014 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2015 hi->dynamic_def |= ht->dynamic_def;
2016
2017 /* See if the new flags lead us to realize that the symbol must
2018 be dynamic. */
2019 if (! *dynsym)
2020 {
2021 if (! dynamic)
2022 {
2023 if (! bfd_link_executable (info)
2024 || hi->def_dynamic
2025 || hi->ref_dynamic)
2026 *dynsym = TRUE;
2027 }
2028 else
2029 {
2030 if (hi->ref_regular)
2031 *dynsym = TRUE;
2032 }
2033 }
2034 }
2035
2036 /* We also need to define an indirection from the nondefault version
2037 of the symbol. */
2038
2039 nondefault:
2040 len = strlen (name);
2041 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2042 if (shortname == NULL)
2043 return FALSE;
2044 memcpy (shortname, name, shortlen);
2045 memcpy (shortname + shortlen, p + 1, len - shortlen);
2046
2047 /* Once again, merge with any existing symbol. */
2048 type_change_ok = FALSE;
2049 size_change_ok = FALSE;
2050 tmp_sec = sec;
2051 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2052 &hi, poldbfd, NULL, NULL, &skip, &override,
2053 &type_change_ok, &size_change_ok, &matched))
2054 return FALSE;
2055
2056 if (skip)
2057 return TRUE;
2058
2059 if (override)
2060 {
2061 /* Here SHORTNAME is a versioned name, so we don't expect to see
2062 the type of override we do in the case above unless it is
2063 overridden by a versioned definition. */
2064 if (hi->root.type != bfd_link_hash_defined
2065 && hi->root.type != bfd_link_hash_defweak)
2066 _bfd_error_handler
2067 /* xgettext:c-format */
2068 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2069 abfd, shortname);
2070 }
2071 else
2072 {
2073 bh = &hi->root;
2074 if (! (_bfd_generic_link_add_one_symbol
2075 (info, abfd, shortname, BSF_INDIRECT,
2076 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
2077 return FALSE;
2078 hi = (struct elf_link_hash_entry *) bh;
2079
2080 /* If there is a duplicate definition somewhere, then HI may not
2081 point to an indirect symbol. We will have reported an error
2082 to the user in that case. */
2083
2084 if (hi->root.type == bfd_link_hash_indirect)
2085 {
2086 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2087 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2088 hi->dynamic_def |= h->dynamic_def;
2089
2090 /* See if the new flags lead us to realize that the symbol
2091 must be dynamic. */
2092 if (! *dynsym)
2093 {
2094 if (! dynamic)
2095 {
2096 if (! bfd_link_executable (info)
2097 || hi->ref_dynamic)
2098 *dynsym = TRUE;
2099 }
2100 else
2101 {
2102 if (hi->ref_regular)
2103 *dynsym = TRUE;
2104 }
2105 }
2106 }
2107 }
2108
2109 return TRUE;
2110 }
2111 \f
2112 /* This routine is used to export all defined symbols into the dynamic
2113 symbol table. It is called via elf_link_hash_traverse. */
2114
2115 static bfd_boolean
2116 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2117 {
2118 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2119
2120 /* Ignore indirect symbols. These are added by the versioning code. */
2121 if (h->root.type == bfd_link_hash_indirect)
2122 return TRUE;
2123
2124 /* Ignore this if we won't export it. */
2125 if (!eif->info->export_dynamic && !h->dynamic)
2126 return TRUE;
2127
2128 if (h->dynindx == -1
2129 && (h->def_regular || h->ref_regular)
2130 && ! bfd_hide_sym_by_version (eif->info->version_info,
2131 h->root.root.string))
2132 {
2133 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2134 {
2135 eif->failed = TRUE;
2136 return FALSE;
2137 }
2138 }
2139
2140 return TRUE;
2141 }
2142 \f
2143 /* Look through the symbols which are defined in other shared
2144 libraries and referenced here. Update the list of version
2145 dependencies. This will be put into the .gnu.version_r section.
2146 This function is called via elf_link_hash_traverse. */
2147
2148 static bfd_boolean
2149 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2150 void *data)
2151 {
2152 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2153 Elf_Internal_Verneed *t;
2154 Elf_Internal_Vernaux *a;
2155 bfd_size_type amt;
2156
2157 /* We only care about symbols defined in shared objects with version
2158 information. */
2159 if (!h->def_dynamic
2160 || h->def_regular
2161 || h->dynindx == -1
2162 || h->verinfo.verdef == NULL
2163 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2164 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2165 return TRUE;
2166
2167 /* See if we already know about this version. */
2168 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2169 t != NULL;
2170 t = t->vn_nextref)
2171 {
2172 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2173 continue;
2174
2175 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2176 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2177 return TRUE;
2178
2179 break;
2180 }
2181
2182 /* This is a new version. Add it to tree we are building. */
2183
2184 if (t == NULL)
2185 {
2186 amt = sizeof *t;
2187 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2188 if (t == NULL)
2189 {
2190 rinfo->failed = TRUE;
2191 return FALSE;
2192 }
2193
2194 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2195 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2196 elf_tdata (rinfo->info->output_bfd)->verref = t;
2197 }
2198
2199 amt = sizeof *a;
2200 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2201 if (a == NULL)
2202 {
2203 rinfo->failed = TRUE;
2204 return FALSE;
2205 }
2206
2207 /* Note that we are copying a string pointer here, and testing it
2208 above. If bfd_elf_string_from_elf_section is ever changed to
2209 discard the string data when low in memory, this will have to be
2210 fixed. */
2211 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2212
2213 a->vna_flags = h->verinfo.verdef->vd_flags;
2214 a->vna_nextptr = t->vn_auxptr;
2215
2216 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2217 ++rinfo->vers;
2218
2219 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2220
2221 t->vn_auxptr = a;
2222
2223 return TRUE;
2224 }
2225
2226 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2227 hidden. Set *T_P to NULL if there is no match. */
2228
2229 static bfd_boolean
2230 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2231 struct elf_link_hash_entry *h,
2232 const char *version_p,
2233 struct bfd_elf_version_tree **t_p,
2234 bfd_boolean *hide)
2235 {
2236 struct bfd_elf_version_tree *t;
2237
2238 /* Look for the version. If we find it, it is no longer weak. */
2239 for (t = info->version_info; t != NULL; t = t->next)
2240 {
2241 if (strcmp (t->name, version_p) == 0)
2242 {
2243 size_t len;
2244 char *alc;
2245 struct bfd_elf_version_expr *d;
2246
2247 len = version_p - h->root.root.string;
2248 alc = (char *) bfd_malloc (len);
2249 if (alc == NULL)
2250 return FALSE;
2251 memcpy (alc, h->root.root.string, len - 1);
2252 alc[len - 1] = '\0';
2253 if (alc[len - 2] == ELF_VER_CHR)
2254 alc[len - 2] = '\0';
2255
2256 h->verinfo.vertree = t;
2257 t->used = TRUE;
2258 d = NULL;
2259
2260 if (t->globals.list != NULL)
2261 d = (*t->match) (&t->globals, NULL, alc);
2262
2263 /* See if there is anything to force this symbol to
2264 local scope. */
2265 if (d == NULL && t->locals.list != NULL)
2266 {
2267 d = (*t->match) (&t->locals, NULL, alc);
2268 if (d != NULL
2269 && h->dynindx != -1
2270 && ! info->export_dynamic)
2271 *hide = TRUE;
2272 }
2273
2274 free (alc);
2275 break;
2276 }
2277 }
2278
2279 *t_p = t;
2280
2281 return TRUE;
2282 }
2283
2284 /* Return TRUE if the symbol H is hidden by version script. */
2285
2286 bfd_boolean
2287 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2288 struct elf_link_hash_entry *h)
2289 {
2290 const char *p;
2291 bfd_boolean hide = FALSE;
2292 const struct elf_backend_data *bed
2293 = get_elf_backend_data (info->output_bfd);
2294
2295 /* Version script only hides symbols defined in regular objects. */
2296 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2297 return TRUE;
2298
2299 p = strchr (h->root.root.string, ELF_VER_CHR);
2300 if (p != NULL && h->verinfo.vertree == NULL)
2301 {
2302 struct bfd_elf_version_tree *t;
2303
2304 ++p;
2305 if (*p == ELF_VER_CHR)
2306 ++p;
2307
2308 if (*p != '\0'
2309 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2310 && hide)
2311 {
2312 if (hide)
2313 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2314 return TRUE;
2315 }
2316 }
2317
2318 /* If we don't have a version for this symbol, see if we can find
2319 something. */
2320 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2321 {
2322 h->verinfo.vertree
2323 = bfd_find_version_for_sym (info->version_info,
2324 h->root.root.string, &hide);
2325 if (h->verinfo.vertree != NULL && hide)
2326 {
2327 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2328 return TRUE;
2329 }
2330 }
2331
2332 return FALSE;
2333 }
2334
2335 /* Figure out appropriate versions for all the symbols. We may not
2336 have the version number script until we have read all of the input
2337 files, so until that point we don't know which symbols should be
2338 local. This function is called via elf_link_hash_traverse. */
2339
2340 static bfd_boolean
2341 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2342 {
2343 struct elf_info_failed *sinfo;
2344 struct bfd_link_info *info;
2345 const struct elf_backend_data *bed;
2346 struct elf_info_failed eif;
2347 char *p;
2348 bfd_boolean hide;
2349
2350 sinfo = (struct elf_info_failed *) data;
2351 info = sinfo->info;
2352
2353 /* Fix the symbol flags. */
2354 eif.failed = FALSE;
2355 eif.info = info;
2356 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2357 {
2358 if (eif.failed)
2359 sinfo->failed = TRUE;
2360 return FALSE;
2361 }
2362
2363 /* We only need version numbers for symbols defined in regular
2364 objects. */
2365 if (!h->def_regular)
2366 return TRUE;
2367
2368 hide = FALSE;
2369 bed = get_elf_backend_data (info->output_bfd);
2370 p = strchr (h->root.root.string, ELF_VER_CHR);
2371 if (p != NULL && h->verinfo.vertree == NULL)
2372 {
2373 struct bfd_elf_version_tree *t;
2374
2375 ++p;
2376 if (*p == ELF_VER_CHR)
2377 ++p;
2378
2379 /* If there is no version string, we can just return out. */
2380 if (*p == '\0')
2381 return TRUE;
2382
2383 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2384 {
2385 sinfo->failed = TRUE;
2386 return FALSE;
2387 }
2388
2389 if (hide)
2390 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2391
2392 /* If we are building an application, we need to create a
2393 version node for this version. */
2394 if (t == NULL && bfd_link_executable (info))
2395 {
2396 struct bfd_elf_version_tree **pp;
2397 int version_index;
2398
2399 /* If we aren't going to export this symbol, we don't need
2400 to worry about it. */
2401 if (h->dynindx == -1)
2402 return TRUE;
2403
2404 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2405 sizeof *t);
2406 if (t == NULL)
2407 {
2408 sinfo->failed = TRUE;
2409 return FALSE;
2410 }
2411
2412 t->name = p;
2413 t->name_indx = (unsigned int) -1;
2414 t->used = TRUE;
2415
2416 version_index = 1;
2417 /* Don't count anonymous version tag. */
2418 if (sinfo->info->version_info != NULL
2419 && sinfo->info->version_info->vernum == 0)
2420 version_index = 0;
2421 for (pp = &sinfo->info->version_info;
2422 *pp != NULL;
2423 pp = &(*pp)->next)
2424 ++version_index;
2425 t->vernum = version_index;
2426
2427 *pp = t;
2428
2429 h->verinfo.vertree = t;
2430 }
2431 else if (t == NULL)
2432 {
2433 /* We could not find the version for a symbol when
2434 generating a shared archive. Return an error. */
2435 _bfd_error_handler
2436 /* xgettext:c-format */
2437 (_("%pB: version node not found for symbol %s"),
2438 info->output_bfd, h->root.root.string);
2439 bfd_set_error (bfd_error_bad_value);
2440 sinfo->failed = TRUE;
2441 return FALSE;
2442 }
2443 }
2444
2445 /* If we don't have a version for this symbol, see if we can find
2446 something. */
2447 if (!hide
2448 && h->verinfo.vertree == NULL
2449 && sinfo->info->version_info != NULL)
2450 {
2451 h->verinfo.vertree
2452 = bfd_find_version_for_sym (sinfo->info->version_info,
2453 h->root.root.string, &hide);
2454 if (h->verinfo.vertree != NULL && hide)
2455 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2456 }
2457
2458 return TRUE;
2459 }
2460 \f
2461 /* Read and swap the relocs from the section indicated by SHDR. This
2462 may be either a REL or a RELA section. The relocations are
2463 translated into RELA relocations and stored in INTERNAL_RELOCS,
2464 which should have already been allocated to contain enough space.
2465 The EXTERNAL_RELOCS are a buffer where the external form of the
2466 relocations should be stored.
2467
2468 Returns FALSE if something goes wrong. */
2469
2470 static bfd_boolean
2471 elf_link_read_relocs_from_section (bfd *abfd,
2472 asection *sec,
2473 Elf_Internal_Shdr *shdr,
2474 void *external_relocs,
2475 Elf_Internal_Rela *internal_relocs)
2476 {
2477 const struct elf_backend_data *bed;
2478 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2479 const bfd_byte *erela;
2480 const bfd_byte *erelaend;
2481 Elf_Internal_Rela *irela;
2482 Elf_Internal_Shdr *symtab_hdr;
2483 size_t nsyms;
2484
2485 /* Position ourselves at the start of the section. */
2486 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2487 return FALSE;
2488
2489 /* Read the relocations. */
2490 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2491 return FALSE;
2492
2493 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2494 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2495
2496 bed = get_elf_backend_data (abfd);
2497
2498 /* Convert the external relocations to the internal format. */
2499 if (shdr->sh_entsize == bed->s->sizeof_rel)
2500 swap_in = bed->s->swap_reloc_in;
2501 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2502 swap_in = bed->s->swap_reloca_in;
2503 else
2504 {
2505 bfd_set_error (bfd_error_wrong_format);
2506 return FALSE;
2507 }
2508
2509 erela = (const bfd_byte *) external_relocs;
2510 erelaend = erela + shdr->sh_size;
2511 irela = internal_relocs;
2512 while (erela < erelaend)
2513 {
2514 bfd_vma r_symndx;
2515
2516 (*swap_in) (abfd, erela, irela);
2517 r_symndx = ELF32_R_SYM (irela->r_info);
2518 if (bed->s->arch_size == 64)
2519 r_symndx >>= 24;
2520 if (nsyms > 0)
2521 {
2522 if ((size_t) r_symndx >= nsyms)
2523 {
2524 _bfd_error_handler
2525 /* xgettext:c-format */
2526 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2527 " for offset %#" PRIx64 " in section `%pA'"),
2528 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2529 (uint64_t) irela->r_offset, sec);
2530 bfd_set_error (bfd_error_bad_value);
2531 return FALSE;
2532 }
2533 }
2534 else if (r_symndx != STN_UNDEF)
2535 {
2536 _bfd_error_handler
2537 /* xgettext:c-format */
2538 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2539 " for offset %#" PRIx64 " in section `%pA'"
2540 " when the object file has no symbol table"),
2541 abfd, (uint64_t) r_symndx,
2542 (uint64_t) irela->r_offset, sec);
2543 bfd_set_error (bfd_error_bad_value);
2544 return FALSE;
2545 }
2546 irela += bed->s->int_rels_per_ext_rel;
2547 erela += shdr->sh_entsize;
2548 }
2549
2550 return TRUE;
2551 }
2552
2553 /* Read and swap the relocs for a section O. They may have been
2554 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2555 not NULL, they are used as buffers to read into. They are known to
2556 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2557 the return value is allocated using either malloc or bfd_alloc,
2558 according to the KEEP_MEMORY argument. If O has two relocation
2559 sections (both REL and RELA relocations), then the REL_HDR
2560 relocations will appear first in INTERNAL_RELOCS, followed by the
2561 RELA_HDR relocations. */
2562
2563 Elf_Internal_Rela *
2564 _bfd_elf_link_read_relocs (bfd *abfd,
2565 asection *o,
2566 void *external_relocs,
2567 Elf_Internal_Rela *internal_relocs,
2568 bfd_boolean keep_memory)
2569 {
2570 void *alloc1 = NULL;
2571 Elf_Internal_Rela *alloc2 = NULL;
2572 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2573 struct bfd_elf_section_data *esdo = elf_section_data (o);
2574 Elf_Internal_Rela *internal_rela_relocs;
2575
2576 if (esdo->relocs != NULL)
2577 return esdo->relocs;
2578
2579 if (o->reloc_count == 0)
2580 return NULL;
2581
2582 if (internal_relocs == NULL)
2583 {
2584 bfd_size_type size;
2585
2586 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2587 if (keep_memory)
2588 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2589 else
2590 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2591 if (internal_relocs == NULL)
2592 goto error_return;
2593 }
2594
2595 if (external_relocs == NULL)
2596 {
2597 bfd_size_type size = 0;
2598
2599 if (esdo->rel.hdr)
2600 size += esdo->rel.hdr->sh_size;
2601 if (esdo->rela.hdr)
2602 size += esdo->rela.hdr->sh_size;
2603
2604 alloc1 = bfd_malloc (size);
2605 if (alloc1 == NULL)
2606 goto error_return;
2607 external_relocs = alloc1;
2608 }
2609
2610 internal_rela_relocs = internal_relocs;
2611 if (esdo->rel.hdr)
2612 {
2613 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2614 external_relocs,
2615 internal_relocs))
2616 goto error_return;
2617 external_relocs = (((bfd_byte *) external_relocs)
2618 + esdo->rel.hdr->sh_size);
2619 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2620 * bed->s->int_rels_per_ext_rel);
2621 }
2622
2623 if (esdo->rela.hdr
2624 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2625 external_relocs,
2626 internal_rela_relocs)))
2627 goto error_return;
2628
2629 /* Cache the results for next time, if we can. */
2630 if (keep_memory)
2631 esdo->relocs = internal_relocs;
2632
2633 if (alloc1 != NULL)
2634 free (alloc1);
2635
2636 /* Don't free alloc2, since if it was allocated we are passing it
2637 back (under the name of internal_relocs). */
2638
2639 return internal_relocs;
2640
2641 error_return:
2642 if (alloc1 != NULL)
2643 free (alloc1);
2644 if (alloc2 != NULL)
2645 {
2646 if (keep_memory)
2647 bfd_release (abfd, alloc2);
2648 else
2649 free (alloc2);
2650 }
2651 return NULL;
2652 }
2653
2654 /* Compute the size of, and allocate space for, REL_HDR which is the
2655 section header for a section containing relocations for O. */
2656
2657 static bfd_boolean
2658 _bfd_elf_link_size_reloc_section (bfd *abfd,
2659 struct bfd_elf_section_reloc_data *reldata)
2660 {
2661 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2662
2663 /* That allows us to calculate the size of the section. */
2664 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2665
2666 /* The contents field must last into write_object_contents, so we
2667 allocate it with bfd_alloc rather than malloc. Also since we
2668 cannot be sure that the contents will actually be filled in,
2669 we zero the allocated space. */
2670 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2671 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2672 return FALSE;
2673
2674 if (reldata->hashes == NULL && reldata->count)
2675 {
2676 struct elf_link_hash_entry **p;
2677
2678 p = ((struct elf_link_hash_entry **)
2679 bfd_zmalloc (reldata->count * sizeof (*p)));
2680 if (p == NULL)
2681 return FALSE;
2682
2683 reldata->hashes = p;
2684 }
2685
2686 return TRUE;
2687 }
2688
2689 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2690 originated from the section given by INPUT_REL_HDR) to the
2691 OUTPUT_BFD. */
2692
2693 bfd_boolean
2694 _bfd_elf_link_output_relocs (bfd *output_bfd,
2695 asection *input_section,
2696 Elf_Internal_Shdr *input_rel_hdr,
2697 Elf_Internal_Rela *internal_relocs,
2698 struct elf_link_hash_entry **rel_hash
2699 ATTRIBUTE_UNUSED)
2700 {
2701 Elf_Internal_Rela *irela;
2702 Elf_Internal_Rela *irelaend;
2703 bfd_byte *erel;
2704 struct bfd_elf_section_reloc_data *output_reldata;
2705 asection *output_section;
2706 const struct elf_backend_data *bed;
2707 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2708 struct bfd_elf_section_data *esdo;
2709
2710 output_section = input_section->output_section;
2711
2712 bed = get_elf_backend_data (output_bfd);
2713 esdo = elf_section_data (output_section);
2714 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2715 {
2716 output_reldata = &esdo->rel;
2717 swap_out = bed->s->swap_reloc_out;
2718 }
2719 else if (esdo->rela.hdr
2720 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2721 {
2722 output_reldata = &esdo->rela;
2723 swap_out = bed->s->swap_reloca_out;
2724 }
2725 else
2726 {
2727 _bfd_error_handler
2728 /* xgettext:c-format */
2729 (_("%pB: relocation size mismatch in %pB section %pA"),
2730 output_bfd, input_section->owner, input_section);
2731 bfd_set_error (bfd_error_wrong_format);
2732 return FALSE;
2733 }
2734
2735 erel = output_reldata->hdr->contents;
2736 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2737 irela = internal_relocs;
2738 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2739 * bed->s->int_rels_per_ext_rel);
2740 while (irela < irelaend)
2741 {
2742 (*swap_out) (output_bfd, irela, erel);
2743 irela += bed->s->int_rels_per_ext_rel;
2744 erel += input_rel_hdr->sh_entsize;
2745 }
2746
2747 /* Bump the counter, so that we know where to add the next set of
2748 relocations. */
2749 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2750
2751 return TRUE;
2752 }
2753 \f
2754 /* Make weak undefined symbols in PIE dynamic. */
2755
2756 bfd_boolean
2757 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2758 struct elf_link_hash_entry *h)
2759 {
2760 if (bfd_link_pie (info)
2761 && h->dynindx == -1
2762 && h->root.type == bfd_link_hash_undefweak)
2763 return bfd_elf_link_record_dynamic_symbol (info, h);
2764
2765 return TRUE;
2766 }
2767
2768 /* Fix up the flags for a symbol. This handles various cases which
2769 can only be fixed after all the input files are seen. This is
2770 currently called by both adjust_dynamic_symbol and
2771 assign_sym_version, which is unnecessary but perhaps more robust in
2772 the face of future changes. */
2773
2774 static bfd_boolean
2775 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2776 struct elf_info_failed *eif)
2777 {
2778 const struct elf_backend_data *bed;
2779
2780 /* If this symbol was mentioned in a non-ELF file, try to set
2781 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2782 permit a non-ELF file to correctly refer to a symbol defined in
2783 an ELF dynamic object. */
2784 if (h->non_elf)
2785 {
2786 while (h->root.type == bfd_link_hash_indirect)
2787 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2788
2789 if (h->root.type != bfd_link_hash_defined
2790 && h->root.type != bfd_link_hash_defweak)
2791 {
2792 h->ref_regular = 1;
2793 h->ref_regular_nonweak = 1;
2794 }
2795 else
2796 {
2797 if (h->root.u.def.section->owner != NULL
2798 && (bfd_get_flavour (h->root.u.def.section->owner)
2799 == bfd_target_elf_flavour))
2800 {
2801 h->ref_regular = 1;
2802 h->ref_regular_nonweak = 1;
2803 }
2804 else
2805 h->def_regular = 1;
2806 }
2807
2808 if (h->dynindx == -1
2809 && (h->def_dynamic
2810 || h->ref_dynamic))
2811 {
2812 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2813 {
2814 eif->failed = TRUE;
2815 return FALSE;
2816 }
2817 }
2818 }
2819 else
2820 {
2821 /* Unfortunately, NON_ELF is only correct if the symbol
2822 was first seen in a non-ELF file. Fortunately, if the symbol
2823 was first seen in an ELF file, we're probably OK unless the
2824 symbol was defined in a non-ELF file. Catch that case here.
2825 FIXME: We're still in trouble if the symbol was first seen in
2826 a dynamic object, and then later in a non-ELF regular object. */
2827 if ((h->root.type == bfd_link_hash_defined
2828 || h->root.type == bfd_link_hash_defweak)
2829 && !h->def_regular
2830 && (h->root.u.def.section->owner != NULL
2831 ? (bfd_get_flavour (h->root.u.def.section->owner)
2832 != bfd_target_elf_flavour)
2833 : (bfd_is_abs_section (h->root.u.def.section)
2834 && !h->def_dynamic)))
2835 h->def_regular = 1;
2836 }
2837
2838 /* Backend specific symbol fixup. */
2839 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2840 if (bed->elf_backend_fixup_symbol
2841 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2842 return FALSE;
2843
2844 /* If this is a final link, and the symbol was defined as a common
2845 symbol in a regular object file, and there was no definition in
2846 any dynamic object, then the linker will have allocated space for
2847 the symbol in a common section but the DEF_REGULAR
2848 flag will not have been set. */
2849 if (h->root.type == bfd_link_hash_defined
2850 && !h->def_regular
2851 && h->ref_regular
2852 && !h->def_dynamic
2853 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2854 h->def_regular = 1;
2855
2856 /* If a weak undefined symbol has non-default visibility, we also
2857 hide it from the dynamic linker. */
2858 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2859 && h->root.type == bfd_link_hash_undefweak)
2860 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2861
2862 /* A hidden versioned symbol in executable should be forced local if
2863 it is is locally defined, not referenced by shared library and not
2864 exported. */
2865 else if (bfd_link_executable (eif->info)
2866 && h->versioned == versioned_hidden
2867 && !eif->info->export_dynamic
2868 && !h->dynamic
2869 && !h->ref_dynamic
2870 && h->def_regular)
2871 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2872
2873 /* If -Bsymbolic was used (which means to bind references to global
2874 symbols to the definition within the shared object), and this
2875 symbol was defined in a regular object, then it actually doesn't
2876 need a PLT entry. Likewise, if the symbol has non-default
2877 visibility. If the symbol has hidden or internal visibility, we
2878 will force it local. */
2879 else if (h->needs_plt
2880 && bfd_link_pic (eif->info)
2881 && is_elf_hash_table (eif->info->hash)
2882 && (SYMBOLIC_BIND (eif->info, h)
2883 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2884 && h->def_regular)
2885 {
2886 bfd_boolean force_local;
2887
2888 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2889 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2890 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2891 }
2892
2893 /* If this is a weak defined symbol in a dynamic object, and we know
2894 the real definition in the dynamic object, copy interesting flags
2895 over to the real definition. */
2896 if (h->is_weakalias)
2897 {
2898 struct elf_link_hash_entry *def = weakdef (h);
2899
2900 /* If the real definition is defined by a regular object file,
2901 don't do anything special. See the longer description in
2902 _bfd_elf_adjust_dynamic_symbol, below. */
2903 if (def->def_regular)
2904 {
2905 h = def;
2906 while ((h = h->u.alias) != def)
2907 h->is_weakalias = 0;
2908 }
2909 else
2910 {
2911 while (h->root.type == bfd_link_hash_indirect)
2912 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2913 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2914 || h->root.type == bfd_link_hash_defweak);
2915 BFD_ASSERT (def->def_dynamic);
2916 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
2917 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
2918 }
2919 }
2920
2921 return TRUE;
2922 }
2923
2924 /* Make the backend pick a good value for a dynamic symbol. This is
2925 called via elf_link_hash_traverse, and also calls itself
2926 recursively. */
2927
2928 static bfd_boolean
2929 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2930 {
2931 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2932 struct elf_link_hash_table *htab;
2933 const struct elf_backend_data *bed;
2934
2935 if (! is_elf_hash_table (eif->info->hash))
2936 return FALSE;
2937
2938 /* Ignore indirect symbols. These are added by the versioning code. */
2939 if (h->root.type == bfd_link_hash_indirect)
2940 return TRUE;
2941
2942 /* Fix the symbol flags. */
2943 if (! _bfd_elf_fix_symbol_flags (h, eif))
2944 return FALSE;
2945
2946 htab = elf_hash_table (eif->info);
2947 bed = get_elf_backend_data (htab->dynobj);
2948
2949 if (h->root.type == bfd_link_hash_undefweak)
2950 {
2951 if (eif->info->dynamic_undefined_weak == 0)
2952 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2953 else if (eif->info->dynamic_undefined_weak > 0
2954 && h->ref_regular
2955 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2956 && !bfd_hide_sym_by_version (eif->info->version_info,
2957 h->root.root.string))
2958 {
2959 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2960 {
2961 eif->failed = TRUE;
2962 return FALSE;
2963 }
2964 }
2965 }
2966
2967 /* If this symbol does not require a PLT entry, and it is not
2968 defined by a dynamic object, or is not referenced by a regular
2969 object, ignore it. We do have to handle a weak defined symbol,
2970 even if no regular object refers to it, if we decided to add it
2971 to the dynamic symbol table. FIXME: Do we normally need to worry
2972 about symbols which are defined by one dynamic object and
2973 referenced by another one? */
2974 if (!h->needs_plt
2975 && h->type != STT_GNU_IFUNC
2976 && (h->def_regular
2977 || !h->def_dynamic
2978 || (!h->ref_regular
2979 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
2980 {
2981 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2982 return TRUE;
2983 }
2984
2985 /* If we've already adjusted this symbol, don't do it again. This
2986 can happen via a recursive call. */
2987 if (h->dynamic_adjusted)
2988 return TRUE;
2989
2990 /* Don't look at this symbol again. Note that we must set this
2991 after checking the above conditions, because we may look at a
2992 symbol once, decide not to do anything, and then get called
2993 recursively later after REF_REGULAR is set below. */
2994 h->dynamic_adjusted = 1;
2995
2996 /* If this is a weak definition, and we know a real definition, and
2997 the real symbol is not itself defined by a regular object file,
2998 then get a good value for the real definition. We handle the
2999 real symbol first, for the convenience of the backend routine.
3000
3001 Note that there is a confusing case here. If the real definition
3002 is defined by a regular object file, we don't get the real symbol
3003 from the dynamic object, but we do get the weak symbol. If the
3004 processor backend uses a COPY reloc, then if some routine in the
3005 dynamic object changes the real symbol, we will not see that
3006 change in the corresponding weak symbol. This is the way other
3007 ELF linkers work as well, and seems to be a result of the shared
3008 library model.
3009
3010 I will clarify this issue. Most SVR4 shared libraries define the
3011 variable _timezone and define timezone as a weak synonym. The
3012 tzset call changes _timezone. If you write
3013 extern int timezone;
3014 int _timezone = 5;
3015 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3016 you might expect that, since timezone is a synonym for _timezone,
3017 the same number will print both times. However, if the processor
3018 backend uses a COPY reloc, then actually timezone will be copied
3019 into your process image, and, since you define _timezone
3020 yourself, _timezone will not. Thus timezone and _timezone will
3021 wind up at different memory locations. The tzset call will set
3022 _timezone, leaving timezone unchanged. */
3023
3024 if (h->is_weakalias)
3025 {
3026 struct elf_link_hash_entry *def = weakdef (h);
3027
3028 /* If we get to this point, there is an implicit reference to
3029 the alias by a regular object file via the weak symbol H. */
3030 def->ref_regular = 1;
3031
3032 /* Ensure that the backend adjust_dynamic_symbol function sees
3033 the strong alias before H by recursively calling ourselves. */
3034 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3035 return FALSE;
3036 }
3037
3038 /* If a symbol has no type and no size and does not require a PLT
3039 entry, then we are probably about to do the wrong thing here: we
3040 are probably going to create a COPY reloc for an empty object.
3041 This case can arise when a shared object is built with assembly
3042 code, and the assembly code fails to set the symbol type. */
3043 if (h->size == 0
3044 && h->type == STT_NOTYPE
3045 && !h->needs_plt)
3046 _bfd_error_handler
3047 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3048 h->root.root.string);
3049
3050 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3051 {
3052 eif->failed = TRUE;
3053 return FALSE;
3054 }
3055
3056 return TRUE;
3057 }
3058
3059 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3060 DYNBSS. */
3061
3062 bfd_boolean
3063 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3064 struct elf_link_hash_entry *h,
3065 asection *dynbss)
3066 {
3067 unsigned int power_of_two;
3068 bfd_vma mask;
3069 asection *sec = h->root.u.def.section;
3070
3071 /* The section alignment of the definition is the maximum alignment
3072 requirement of symbols defined in the section. Since we don't
3073 know the symbol alignment requirement, we start with the
3074 maximum alignment and check low bits of the symbol address
3075 for the minimum alignment. */
3076 power_of_two = bfd_get_section_alignment (sec->owner, sec);
3077 mask = ((bfd_vma) 1 << power_of_two) - 1;
3078 while ((h->root.u.def.value & mask) != 0)
3079 {
3080 mask >>= 1;
3081 --power_of_two;
3082 }
3083
3084 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
3085 dynbss))
3086 {
3087 /* Adjust the section alignment if needed. */
3088 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
3089 power_of_two))
3090 return FALSE;
3091 }
3092
3093 /* We make sure that the symbol will be aligned properly. */
3094 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3095
3096 /* Define the symbol as being at this point in DYNBSS. */
3097 h->root.u.def.section = dynbss;
3098 h->root.u.def.value = dynbss->size;
3099
3100 /* Increment the size of DYNBSS to make room for the symbol. */
3101 dynbss->size += h->size;
3102
3103 /* No error if extern_protected_data is true. */
3104 if (h->protected_def
3105 && (!info->extern_protected_data
3106 || (info->extern_protected_data < 0
3107 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3108 info->callbacks->einfo
3109 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3110 h->root.root.string);
3111
3112 return TRUE;
3113 }
3114
3115 /* Adjust all external symbols pointing into SEC_MERGE sections
3116 to reflect the object merging within the sections. */
3117
3118 static bfd_boolean
3119 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3120 {
3121 asection *sec;
3122
3123 if ((h->root.type == bfd_link_hash_defined
3124 || h->root.type == bfd_link_hash_defweak)
3125 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3126 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3127 {
3128 bfd *output_bfd = (bfd *) data;
3129
3130 h->root.u.def.value =
3131 _bfd_merged_section_offset (output_bfd,
3132 &h->root.u.def.section,
3133 elf_section_data (sec)->sec_info,
3134 h->root.u.def.value);
3135 }
3136
3137 return TRUE;
3138 }
3139
3140 /* Returns false if the symbol referred to by H should be considered
3141 to resolve local to the current module, and true if it should be
3142 considered to bind dynamically. */
3143
3144 bfd_boolean
3145 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3146 struct bfd_link_info *info,
3147 bfd_boolean not_local_protected)
3148 {
3149 bfd_boolean binding_stays_local_p;
3150 const struct elf_backend_data *bed;
3151 struct elf_link_hash_table *hash_table;
3152
3153 if (h == NULL)
3154 return FALSE;
3155
3156 while (h->root.type == bfd_link_hash_indirect
3157 || h->root.type == bfd_link_hash_warning)
3158 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3159
3160 /* If it was forced local, then clearly it's not dynamic. */
3161 if (h->dynindx == -1)
3162 return FALSE;
3163 if (h->forced_local)
3164 return FALSE;
3165
3166 /* Identify the cases where name binding rules say that a
3167 visible symbol resolves locally. */
3168 binding_stays_local_p = (bfd_link_executable (info)
3169 || SYMBOLIC_BIND (info, h));
3170
3171 switch (ELF_ST_VISIBILITY (h->other))
3172 {
3173 case STV_INTERNAL:
3174 case STV_HIDDEN:
3175 return FALSE;
3176
3177 case STV_PROTECTED:
3178 hash_table = elf_hash_table (info);
3179 if (!is_elf_hash_table (hash_table))
3180 return FALSE;
3181
3182 bed = get_elf_backend_data (hash_table->dynobj);
3183
3184 /* Proper resolution for function pointer equality may require
3185 that these symbols perhaps be resolved dynamically, even though
3186 we should be resolving them to the current module. */
3187 if (!not_local_protected || !bed->is_function_type (h->type))
3188 binding_stays_local_p = TRUE;
3189 break;
3190
3191 default:
3192 break;
3193 }
3194
3195 /* If it isn't defined locally, then clearly it's dynamic. */
3196 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3197 return TRUE;
3198
3199 /* Otherwise, the symbol is dynamic if binding rules don't tell
3200 us that it remains local. */
3201 return !binding_stays_local_p;
3202 }
3203
3204 /* Return true if the symbol referred to by H should be considered
3205 to resolve local to the current module, and false otherwise. Differs
3206 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3207 undefined symbols. The two functions are virtually identical except
3208 for the place where dynindx == -1 is tested. If that test is true,
3209 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3210 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3211 defined symbols.
3212 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3213 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3214 treatment of undefined weak symbols. For those that do not make
3215 undefined weak symbols dynamic, both functions may return false. */
3216
3217 bfd_boolean
3218 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3219 struct bfd_link_info *info,
3220 bfd_boolean local_protected)
3221 {
3222 const struct elf_backend_data *bed;
3223 struct elf_link_hash_table *hash_table;
3224
3225 /* If it's a local sym, of course we resolve locally. */
3226 if (h == NULL)
3227 return TRUE;
3228
3229 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3230 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3231 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3232 return TRUE;
3233
3234 /* Forced local symbols resolve locally. */
3235 if (h->forced_local)
3236 return TRUE;
3237
3238 /* Common symbols that become definitions don't get the DEF_REGULAR
3239 flag set, so test it first, and don't bail out. */
3240 if (ELF_COMMON_DEF_P (h))
3241 /* Do nothing. */;
3242 /* If we don't have a definition in a regular file, then we can't
3243 resolve locally. The sym is either undefined or dynamic. */
3244 else if (!h->def_regular)
3245 return FALSE;
3246
3247 /* Non-dynamic symbols resolve locally. */
3248 if (h->dynindx == -1)
3249 return TRUE;
3250
3251 /* At this point, we know the symbol is defined and dynamic. In an
3252 executable it must resolve locally, likewise when building symbolic
3253 shared libraries. */
3254 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3255 return TRUE;
3256
3257 /* Now deal with defined dynamic symbols in shared libraries. Ones
3258 with default visibility might not resolve locally. */
3259 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3260 return FALSE;
3261
3262 hash_table = elf_hash_table (info);
3263 if (!is_elf_hash_table (hash_table))
3264 return TRUE;
3265
3266 bed = get_elf_backend_data (hash_table->dynobj);
3267
3268 /* If extern_protected_data is false, STV_PROTECTED non-function
3269 symbols are local. */
3270 if ((!info->extern_protected_data
3271 || (info->extern_protected_data < 0
3272 && !bed->extern_protected_data))
3273 && !bed->is_function_type (h->type))
3274 return TRUE;
3275
3276 /* Function pointer equality tests may require that STV_PROTECTED
3277 symbols be treated as dynamic symbols. If the address of a
3278 function not defined in an executable is set to that function's
3279 plt entry in the executable, then the address of the function in
3280 a shared library must also be the plt entry in the executable. */
3281 return local_protected;
3282 }
3283
3284 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3285 aligned. Returns the first TLS output section. */
3286
3287 struct bfd_section *
3288 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3289 {
3290 struct bfd_section *sec, *tls;
3291 unsigned int align = 0;
3292
3293 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3294 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3295 break;
3296 tls = sec;
3297
3298 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3299 if (sec->alignment_power > align)
3300 align = sec->alignment_power;
3301
3302 elf_hash_table (info)->tls_sec = tls;
3303
3304 /* Ensure the alignment of the first section is the largest alignment,
3305 so that the tls segment starts aligned. */
3306 if (tls != NULL)
3307 tls->alignment_power = align;
3308
3309 return tls;
3310 }
3311
3312 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3313 static bfd_boolean
3314 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3315 Elf_Internal_Sym *sym)
3316 {
3317 const struct elf_backend_data *bed;
3318
3319 /* Local symbols do not count, but target specific ones might. */
3320 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3321 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3322 return FALSE;
3323
3324 bed = get_elf_backend_data (abfd);
3325 /* Function symbols do not count. */
3326 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3327 return FALSE;
3328
3329 /* If the section is undefined, then so is the symbol. */
3330 if (sym->st_shndx == SHN_UNDEF)
3331 return FALSE;
3332
3333 /* If the symbol is defined in the common section, then
3334 it is a common definition and so does not count. */
3335 if (bed->common_definition (sym))
3336 return FALSE;
3337
3338 /* If the symbol is in a target specific section then we
3339 must rely upon the backend to tell us what it is. */
3340 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3341 /* FIXME - this function is not coded yet:
3342
3343 return _bfd_is_global_symbol_definition (abfd, sym);
3344
3345 Instead for now assume that the definition is not global,
3346 Even if this is wrong, at least the linker will behave
3347 in the same way that it used to do. */
3348 return FALSE;
3349
3350 return TRUE;
3351 }
3352
3353 /* Search the symbol table of the archive element of the archive ABFD
3354 whose archive map contains a mention of SYMDEF, and determine if
3355 the symbol is defined in this element. */
3356 static bfd_boolean
3357 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3358 {
3359 Elf_Internal_Shdr * hdr;
3360 size_t symcount;
3361 size_t extsymcount;
3362 size_t extsymoff;
3363 Elf_Internal_Sym *isymbuf;
3364 Elf_Internal_Sym *isym;
3365 Elf_Internal_Sym *isymend;
3366 bfd_boolean result;
3367
3368 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3369 if (abfd == NULL)
3370 return FALSE;
3371
3372 if (! bfd_check_format (abfd, bfd_object))
3373 return FALSE;
3374
3375 /* Select the appropriate symbol table. If we don't know if the
3376 object file is an IR object, give linker LTO plugin a chance to
3377 get the correct symbol table. */
3378 if (abfd->plugin_format == bfd_plugin_yes
3379 #if BFD_SUPPORTS_PLUGINS
3380 || (abfd->plugin_format == bfd_plugin_unknown
3381 && bfd_link_plugin_object_p (abfd))
3382 #endif
3383 )
3384 {
3385 /* Use the IR symbol table if the object has been claimed by
3386 plugin. */
3387 abfd = abfd->plugin_dummy_bfd;
3388 hdr = &elf_tdata (abfd)->symtab_hdr;
3389 }
3390 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3391 hdr = &elf_tdata (abfd)->symtab_hdr;
3392 else
3393 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3394
3395 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3396
3397 /* The sh_info field of the symtab header tells us where the
3398 external symbols start. We don't care about the local symbols. */
3399 if (elf_bad_symtab (abfd))
3400 {
3401 extsymcount = symcount;
3402 extsymoff = 0;
3403 }
3404 else
3405 {
3406 extsymcount = symcount - hdr->sh_info;
3407 extsymoff = hdr->sh_info;
3408 }
3409
3410 if (extsymcount == 0)
3411 return FALSE;
3412
3413 /* Read in the symbol table. */
3414 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3415 NULL, NULL, NULL);
3416 if (isymbuf == NULL)
3417 return FALSE;
3418
3419 /* Scan the symbol table looking for SYMDEF. */
3420 result = FALSE;
3421 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3422 {
3423 const char *name;
3424
3425 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3426 isym->st_name);
3427 if (name == NULL)
3428 break;
3429
3430 if (strcmp (name, symdef->name) == 0)
3431 {
3432 result = is_global_data_symbol_definition (abfd, isym);
3433 break;
3434 }
3435 }
3436
3437 free (isymbuf);
3438
3439 return result;
3440 }
3441 \f
3442 /* Add an entry to the .dynamic table. */
3443
3444 bfd_boolean
3445 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3446 bfd_vma tag,
3447 bfd_vma val)
3448 {
3449 struct elf_link_hash_table *hash_table;
3450 const struct elf_backend_data *bed;
3451 asection *s;
3452 bfd_size_type newsize;
3453 bfd_byte *newcontents;
3454 Elf_Internal_Dyn dyn;
3455
3456 hash_table = elf_hash_table (info);
3457 if (! is_elf_hash_table (hash_table))
3458 return FALSE;
3459
3460 if (tag == DT_RELA || tag == DT_REL)
3461 hash_table->dynamic_relocs = TRUE;
3462
3463 bed = get_elf_backend_data (hash_table->dynobj);
3464 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3465 BFD_ASSERT (s != NULL);
3466
3467 newsize = s->size + bed->s->sizeof_dyn;
3468 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3469 if (newcontents == NULL)
3470 return FALSE;
3471
3472 dyn.d_tag = tag;
3473 dyn.d_un.d_val = val;
3474 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3475
3476 s->size = newsize;
3477 s->contents = newcontents;
3478
3479 return TRUE;
3480 }
3481
3482 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3483 otherwise just check whether one already exists. Returns -1 on error,
3484 1 if a DT_NEEDED tag already exists, and 0 on success. */
3485
3486 static int
3487 elf_add_dt_needed_tag (bfd *abfd,
3488 struct bfd_link_info *info,
3489 const char *soname,
3490 bfd_boolean do_it)
3491 {
3492 struct elf_link_hash_table *hash_table;
3493 size_t strindex;
3494
3495 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3496 return -1;
3497
3498 hash_table = elf_hash_table (info);
3499 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3500 if (strindex == (size_t) -1)
3501 return -1;
3502
3503 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3504 {
3505 asection *sdyn;
3506 const struct elf_backend_data *bed;
3507 bfd_byte *extdyn;
3508
3509 bed = get_elf_backend_data (hash_table->dynobj);
3510 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3511 if (sdyn != NULL)
3512 for (extdyn = sdyn->contents;
3513 extdyn < sdyn->contents + sdyn->size;
3514 extdyn += bed->s->sizeof_dyn)
3515 {
3516 Elf_Internal_Dyn dyn;
3517
3518 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3519 if (dyn.d_tag == DT_NEEDED
3520 && dyn.d_un.d_val == strindex)
3521 {
3522 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3523 return 1;
3524 }
3525 }
3526 }
3527
3528 if (do_it)
3529 {
3530 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3531 return -1;
3532
3533 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3534 return -1;
3535 }
3536 else
3537 /* We were just checking for existence of the tag. */
3538 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3539
3540 return 0;
3541 }
3542
3543 /* Return true if SONAME is on the needed list between NEEDED and STOP
3544 (or the end of list if STOP is NULL), and needed by a library that
3545 will be loaded. */
3546
3547 static bfd_boolean
3548 on_needed_list (const char *soname,
3549 struct bfd_link_needed_list *needed,
3550 struct bfd_link_needed_list *stop)
3551 {
3552 struct bfd_link_needed_list *look;
3553 for (look = needed; look != stop; look = look->next)
3554 if (strcmp (soname, look->name) == 0
3555 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3556 /* If needed by a library that itself is not directly
3557 needed, recursively check whether that library is
3558 indirectly needed. Since we add DT_NEEDED entries to
3559 the end of the list, library dependencies appear after
3560 the library. Therefore search prior to the current
3561 LOOK, preventing possible infinite recursion. */
3562 || on_needed_list (elf_dt_name (look->by), needed, look)))
3563 return TRUE;
3564
3565 return FALSE;
3566 }
3567
3568 /* Sort symbol by value, section, and size. */
3569 static int
3570 elf_sort_symbol (const void *arg1, const void *arg2)
3571 {
3572 const struct elf_link_hash_entry *h1;
3573 const struct elf_link_hash_entry *h2;
3574 bfd_signed_vma vdiff;
3575
3576 h1 = *(const struct elf_link_hash_entry **) arg1;
3577 h2 = *(const struct elf_link_hash_entry **) arg2;
3578 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3579 if (vdiff != 0)
3580 return vdiff > 0 ? 1 : -1;
3581 else
3582 {
3583 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3584 if (sdiff != 0)
3585 return sdiff > 0 ? 1 : -1;
3586 }
3587 vdiff = h1->size - h2->size;
3588 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3589 }
3590
3591 /* This function is used to adjust offsets into .dynstr for
3592 dynamic symbols. This is called via elf_link_hash_traverse. */
3593
3594 static bfd_boolean
3595 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3596 {
3597 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3598
3599 if (h->dynindx != -1)
3600 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3601 return TRUE;
3602 }
3603
3604 /* Assign string offsets in .dynstr, update all structures referencing
3605 them. */
3606
3607 static bfd_boolean
3608 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3609 {
3610 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3611 struct elf_link_local_dynamic_entry *entry;
3612 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3613 bfd *dynobj = hash_table->dynobj;
3614 asection *sdyn;
3615 bfd_size_type size;
3616 const struct elf_backend_data *bed;
3617 bfd_byte *extdyn;
3618
3619 _bfd_elf_strtab_finalize (dynstr);
3620 size = _bfd_elf_strtab_size (dynstr);
3621
3622 bed = get_elf_backend_data (dynobj);
3623 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3624 BFD_ASSERT (sdyn != NULL);
3625
3626 /* Update all .dynamic entries referencing .dynstr strings. */
3627 for (extdyn = sdyn->contents;
3628 extdyn < sdyn->contents + sdyn->size;
3629 extdyn += bed->s->sizeof_dyn)
3630 {
3631 Elf_Internal_Dyn dyn;
3632
3633 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3634 switch (dyn.d_tag)
3635 {
3636 case DT_STRSZ:
3637 dyn.d_un.d_val = size;
3638 break;
3639 case DT_NEEDED:
3640 case DT_SONAME:
3641 case DT_RPATH:
3642 case DT_RUNPATH:
3643 case DT_FILTER:
3644 case DT_AUXILIARY:
3645 case DT_AUDIT:
3646 case DT_DEPAUDIT:
3647 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3648 break;
3649 default:
3650 continue;
3651 }
3652 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3653 }
3654
3655 /* Now update local dynamic symbols. */
3656 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3657 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3658 entry->isym.st_name);
3659
3660 /* And the rest of dynamic symbols. */
3661 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3662
3663 /* Adjust version definitions. */
3664 if (elf_tdata (output_bfd)->cverdefs)
3665 {
3666 asection *s;
3667 bfd_byte *p;
3668 size_t i;
3669 Elf_Internal_Verdef def;
3670 Elf_Internal_Verdaux defaux;
3671
3672 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3673 p = s->contents;
3674 do
3675 {
3676 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3677 &def);
3678 p += sizeof (Elf_External_Verdef);
3679 if (def.vd_aux != sizeof (Elf_External_Verdef))
3680 continue;
3681 for (i = 0; i < def.vd_cnt; ++i)
3682 {
3683 _bfd_elf_swap_verdaux_in (output_bfd,
3684 (Elf_External_Verdaux *) p, &defaux);
3685 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3686 defaux.vda_name);
3687 _bfd_elf_swap_verdaux_out (output_bfd,
3688 &defaux, (Elf_External_Verdaux *) p);
3689 p += sizeof (Elf_External_Verdaux);
3690 }
3691 }
3692 while (def.vd_next);
3693 }
3694
3695 /* Adjust version references. */
3696 if (elf_tdata (output_bfd)->verref)
3697 {
3698 asection *s;
3699 bfd_byte *p;
3700 size_t i;
3701 Elf_Internal_Verneed need;
3702 Elf_Internal_Vernaux needaux;
3703
3704 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3705 p = s->contents;
3706 do
3707 {
3708 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3709 &need);
3710 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3711 _bfd_elf_swap_verneed_out (output_bfd, &need,
3712 (Elf_External_Verneed *) p);
3713 p += sizeof (Elf_External_Verneed);
3714 for (i = 0; i < need.vn_cnt; ++i)
3715 {
3716 _bfd_elf_swap_vernaux_in (output_bfd,
3717 (Elf_External_Vernaux *) p, &needaux);
3718 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3719 needaux.vna_name);
3720 _bfd_elf_swap_vernaux_out (output_bfd,
3721 &needaux,
3722 (Elf_External_Vernaux *) p);
3723 p += sizeof (Elf_External_Vernaux);
3724 }
3725 }
3726 while (need.vn_next);
3727 }
3728
3729 return TRUE;
3730 }
3731 \f
3732 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3733 The default is to only match when the INPUT and OUTPUT are exactly
3734 the same target. */
3735
3736 bfd_boolean
3737 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3738 const bfd_target *output)
3739 {
3740 return input == output;
3741 }
3742
3743 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3744 This version is used when different targets for the same architecture
3745 are virtually identical. */
3746
3747 bfd_boolean
3748 _bfd_elf_relocs_compatible (const bfd_target *input,
3749 const bfd_target *output)
3750 {
3751 const struct elf_backend_data *obed, *ibed;
3752
3753 if (input == output)
3754 return TRUE;
3755
3756 ibed = xvec_get_elf_backend_data (input);
3757 obed = xvec_get_elf_backend_data (output);
3758
3759 if (ibed->arch != obed->arch)
3760 return FALSE;
3761
3762 /* If both backends are using this function, deem them compatible. */
3763 return ibed->relocs_compatible == obed->relocs_compatible;
3764 }
3765
3766 /* Make a special call to the linker "notice" function to tell it that
3767 we are about to handle an as-needed lib, or have finished
3768 processing the lib. */
3769
3770 bfd_boolean
3771 _bfd_elf_notice_as_needed (bfd *ibfd,
3772 struct bfd_link_info *info,
3773 enum notice_asneeded_action act)
3774 {
3775 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3776 }
3777
3778 /* Check relocations an ELF object file. */
3779
3780 bfd_boolean
3781 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3782 {
3783 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3784 struct elf_link_hash_table *htab = elf_hash_table (info);
3785
3786 /* If this object is the same format as the output object, and it is
3787 not a shared library, then let the backend look through the
3788 relocs.
3789
3790 This is required to build global offset table entries and to
3791 arrange for dynamic relocs. It is not required for the
3792 particular common case of linking non PIC code, even when linking
3793 against shared libraries, but unfortunately there is no way of
3794 knowing whether an object file has been compiled PIC or not.
3795 Looking through the relocs is not particularly time consuming.
3796 The problem is that we must either (1) keep the relocs in memory,
3797 which causes the linker to require additional runtime memory or
3798 (2) read the relocs twice from the input file, which wastes time.
3799 This would be a good case for using mmap.
3800
3801 I have no idea how to handle linking PIC code into a file of a
3802 different format. It probably can't be done. */
3803 if ((abfd->flags & DYNAMIC) == 0
3804 && is_elf_hash_table (htab)
3805 && bed->check_relocs != NULL
3806 && elf_object_id (abfd) == elf_hash_table_id (htab)
3807 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3808 {
3809 asection *o;
3810
3811 for (o = abfd->sections; o != NULL; o = o->next)
3812 {
3813 Elf_Internal_Rela *internal_relocs;
3814 bfd_boolean ok;
3815
3816 /* Don't check relocations in excluded sections. */
3817 if ((o->flags & SEC_RELOC) == 0
3818 || (o->flags & SEC_EXCLUDE) != 0
3819 || o->reloc_count == 0
3820 || ((info->strip == strip_all || info->strip == strip_debugger)
3821 && (o->flags & SEC_DEBUGGING) != 0)
3822 || bfd_is_abs_section (o->output_section))
3823 continue;
3824
3825 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3826 info->keep_memory);
3827 if (internal_relocs == NULL)
3828 return FALSE;
3829
3830 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3831
3832 if (elf_section_data (o)->relocs != internal_relocs)
3833 free (internal_relocs);
3834
3835 if (! ok)
3836 return FALSE;
3837 }
3838 }
3839
3840 return TRUE;
3841 }
3842
3843 /* Add symbols from an ELF object file to the linker hash table. */
3844
3845 static bfd_boolean
3846 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3847 {
3848 Elf_Internal_Ehdr *ehdr;
3849 Elf_Internal_Shdr *hdr;
3850 size_t symcount;
3851 size_t extsymcount;
3852 size_t extsymoff;
3853 struct elf_link_hash_entry **sym_hash;
3854 bfd_boolean dynamic;
3855 Elf_External_Versym *extversym = NULL;
3856 Elf_External_Versym *ever;
3857 struct elf_link_hash_entry *weaks;
3858 struct elf_link_hash_entry **nondeflt_vers = NULL;
3859 size_t nondeflt_vers_cnt = 0;
3860 Elf_Internal_Sym *isymbuf = NULL;
3861 Elf_Internal_Sym *isym;
3862 Elf_Internal_Sym *isymend;
3863 const struct elf_backend_data *bed;
3864 bfd_boolean add_needed;
3865 struct elf_link_hash_table *htab;
3866 bfd_size_type amt;
3867 void *alloc_mark = NULL;
3868 struct bfd_hash_entry **old_table = NULL;
3869 unsigned int old_size = 0;
3870 unsigned int old_count = 0;
3871 void *old_tab = NULL;
3872 void *old_ent;
3873 struct bfd_link_hash_entry *old_undefs = NULL;
3874 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3875 void *old_strtab = NULL;
3876 size_t tabsize = 0;
3877 asection *s;
3878 bfd_boolean just_syms;
3879
3880 htab = elf_hash_table (info);
3881 bed = get_elf_backend_data (abfd);
3882
3883 if ((abfd->flags & DYNAMIC) == 0)
3884 dynamic = FALSE;
3885 else
3886 {
3887 dynamic = TRUE;
3888
3889 /* You can't use -r against a dynamic object. Also, there's no
3890 hope of using a dynamic object which does not exactly match
3891 the format of the output file. */
3892 if (bfd_link_relocatable (info)
3893 || !is_elf_hash_table (htab)
3894 || info->output_bfd->xvec != abfd->xvec)
3895 {
3896 if (bfd_link_relocatable (info))
3897 bfd_set_error (bfd_error_invalid_operation);
3898 else
3899 bfd_set_error (bfd_error_wrong_format);
3900 goto error_return;
3901 }
3902 }
3903
3904 ehdr = elf_elfheader (abfd);
3905 if (info->warn_alternate_em
3906 && bed->elf_machine_code != ehdr->e_machine
3907 && ((bed->elf_machine_alt1 != 0
3908 && ehdr->e_machine == bed->elf_machine_alt1)
3909 || (bed->elf_machine_alt2 != 0
3910 && ehdr->e_machine == bed->elf_machine_alt2)))
3911 _bfd_error_handler
3912 /* xgettext:c-format */
3913 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
3914 ehdr->e_machine, abfd, bed->elf_machine_code);
3915
3916 /* As a GNU extension, any input sections which are named
3917 .gnu.warning.SYMBOL are treated as warning symbols for the given
3918 symbol. This differs from .gnu.warning sections, which generate
3919 warnings when they are included in an output file. */
3920 /* PR 12761: Also generate this warning when building shared libraries. */
3921 for (s = abfd->sections; s != NULL; s = s->next)
3922 {
3923 const char *name;
3924
3925 name = bfd_get_section_name (abfd, s);
3926 if (CONST_STRNEQ (name, ".gnu.warning."))
3927 {
3928 char *msg;
3929 bfd_size_type sz;
3930
3931 name += sizeof ".gnu.warning." - 1;
3932
3933 /* If this is a shared object, then look up the symbol
3934 in the hash table. If it is there, and it is already
3935 been defined, then we will not be using the entry
3936 from this shared object, so we don't need to warn.
3937 FIXME: If we see the definition in a regular object
3938 later on, we will warn, but we shouldn't. The only
3939 fix is to keep track of what warnings we are supposed
3940 to emit, and then handle them all at the end of the
3941 link. */
3942 if (dynamic)
3943 {
3944 struct elf_link_hash_entry *h;
3945
3946 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3947
3948 /* FIXME: What about bfd_link_hash_common? */
3949 if (h != NULL
3950 && (h->root.type == bfd_link_hash_defined
3951 || h->root.type == bfd_link_hash_defweak))
3952 continue;
3953 }
3954
3955 sz = s->size;
3956 msg = (char *) bfd_alloc (abfd, sz + 1);
3957 if (msg == NULL)
3958 goto error_return;
3959
3960 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3961 goto error_return;
3962
3963 msg[sz] = '\0';
3964
3965 if (! (_bfd_generic_link_add_one_symbol
3966 (info, abfd, name, BSF_WARNING, s, 0, msg,
3967 FALSE, bed->collect, NULL)))
3968 goto error_return;
3969
3970 if (bfd_link_executable (info))
3971 {
3972 /* Clobber the section size so that the warning does
3973 not get copied into the output file. */
3974 s->size = 0;
3975
3976 /* Also set SEC_EXCLUDE, so that symbols defined in
3977 the warning section don't get copied to the output. */
3978 s->flags |= SEC_EXCLUDE;
3979 }
3980 }
3981 }
3982
3983 just_syms = ((s = abfd->sections) != NULL
3984 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3985
3986 add_needed = TRUE;
3987 if (! dynamic)
3988 {
3989 /* If we are creating a shared library, create all the dynamic
3990 sections immediately. We need to attach them to something,
3991 so we attach them to this BFD, provided it is the right
3992 format and is not from ld --just-symbols. Always create the
3993 dynamic sections for -E/--dynamic-list. FIXME: If there
3994 are no input BFD's of the same format as the output, we can't
3995 make a shared library. */
3996 if (!just_syms
3997 && (bfd_link_pic (info)
3998 || (!bfd_link_relocatable (info)
3999 && info->nointerp
4000 && (info->export_dynamic || info->dynamic)))
4001 && is_elf_hash_table (htab)
4002 && info->output_bfd->xvec == abfd->xvec
4003 && !htab->dynamic_sections_created)
4004 {
4005 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4006 goto error_return;
4007 }
4008 }
4009 else if (!is_elf_hash_table (htab))
4010 goto error_return;
4011 else
4012 {
4013 const char *soname = NULL;
4014 char *audit = NULL;
4015 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4016 const Elf_Internal_Phdr *phdr;
4017 int ret;
4018
4019 /* ld --just-symbols and dynamic objects don't mix very well.
4020 ld shouldn't allow it. */
4021 if (just_syms)
4022 abort ();
4023
4024 /* If this dynamic lib was specified on the command line with
4025 --as-needed in effect, then we don't want to add a DT_NEEDED
4026 tag unless the lib is actually used. Similary for libs brought
4027 in by another lib's DT_NEEDED. When --no-add-needed is used
4028 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4029 any dynamic library in DT_NEEDED tags in the dynamic lib at
4030 all. */
4031 add_needed = (elf_dyn_lib_class (abfd)
4032 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4033 | DYN_NO_NEEDED)) == 0;
4034
4035 s = bfd_get_section_by_name (abfd, ".dynamic");
4036 if (s != NULL)
4037 {
4038 bfd_byte *dynbuf;
4039 bfd_byte *extdyn;
4040 unsigned int elfsec;
4041 unsigned long shlink;
4042
4043 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4044 {
4045 error_free_dyn:
4046 free (dynbuf);
4047 goto error_return;
4048 }
4049
4050 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4051 if (elfsec == SHN_BAD)
4052 goto error_free_dyn;
4053 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4054
4055 for (extdyn = dynbuf;
4056 extdyn < dynbuf + s->size;
4057 extdyn += bed->s->sizeof_dyn)
4058 {
4059 Elf_Internal_Dyn dyn;
4060
4061 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4062 if (dyn.d_tag == DT_SONAME)
4063 {
4064 unsigned int tagv = dyn.d_un.d_val;
4065 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4066 if (soname == NULL)
4067 goto error_free_dyn;
4068 }
4069 if (dyn.d_tag == DT_NEEDED)
4070 {
4071 struct bfd_link_needed_list *n, **pn;
4072 char *fnm, *anm;
4073 unsigned int tagv = dyn.d_un.d_val;
4074
4075 amt = sizeof (struct bfd_link_needed_list);
4076 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4077 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4078 if (n == NULL || fnm == NULL)
4079 goto error_free_dyn;
4080 amt = strlen (fnm) + 1;
4081 anm = (char *) bfd_alloc (abfd, amt);
4082 if (anm == NULL)
4083 goto error_free_dyn;
4084 memcpy (anm, fnm, amt);
4085 n->name = anm;
4086 n->by = abfd;
4087 n->next = NULL;
4088 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4089 ;
4090 *pn = n;
4091 }
4092 if (dyn.d_tag == DT_RUNPATH)
4093 {
4094 struct bfd_link_needed_list *n, **pn;
4095 char *fnm, *anm;
4096 unsigned int tagv = dyn.d_un.d_val;
4097
4098 amt = sizeof (struct bfd_link_needed_list);
4099 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4100 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4101 if (n == NULL || fnm == NULL)
4102 goto error_free_dyn;
4103 amt = strlen (fnm) + 1;
4104 anm = (char *) bfd_alloc (abfd, amt);
4105 if (anm == NULL)
4106 goto error_free_dyn;
4107 memcpy (anm, fnm, amt);
4108 n->name = anm;
4109 n->by = abfd;
4110 n->next = NULL;
4111 for (pn = & runpath;
4112 *pn != NULL;
4113 pn = &(*pn)->next)
4114 ;
4115 *pn = n;
4116 }
4117 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4118 if (!runpath && dyn.d_tag == DT_RPATH)
4119 {
4120 struct bfd_link_needed_list *n, **pn;
4121 char *fnm, *anm;
4122 unsigned int tagv = dyn.d_un.d_val;
4123
4124 amt = sizeof (struct bfd_link_needed_list);
4125 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4126 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4127 if (n == NULL || fnm == NULL)
4128 goto error_free_dyn;
4129 amt = strlen (fnm) + 1;
4130 anm = (char *) bfd_alloc (abfd, amt);
4131 if (anm == NULL)
4132 goto error_free_dyn;
4133 memcpy (anm, fnm, amt);
4134 n->name = anm;
4135 n->by = abfd;
4136 n->next = NULL;
4137 for (pn = & rpath;
4138 *pn != NULL;
4139 pn = &(*pn)->next)
4140 ;
4141 *pn = n;
4142 }
4143 if (dyn.d_tag == DT_AUDIT)
4144 {
4145 unsigned int tagv = dyn.d_un.d_val;
4146 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4147 }
4148 }
4149
4150 free (dynbuf);
4151 }
4152
4153 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4154 frees all more recently bfd_alloc'd blocks as well. */
4155 if (runpath)
4156 rpath = runpath;
4157
4158 if (rpath)
4159 {
4160 struct bfd_link_needed_list **pn;
4161 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4162 ;
4163 *pn = rpath;
4164 }
4165
4166 /* If we have a PT_GNU_RELRO program header, mark as read-only
4167 all sections contained fully therein. This makes relro
4168 shared library sections appear as they will at run-time. */
4169 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4170 while (--phdr >= elf_tdata (abfd)->phdr)
4171 if (phdr->p_type == PT_GNU_RELRO)
4172 {
4173 for (s = abfd->sections; s != NULL; s = s->next)
4174 if ((s->flags & SEC_ALLOC) != 0
4175 && s->vma >= phdr->p_vaddr
4176 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4177 s->flags |= SEC_READONLY;
4178 break;
4179 }
4180
4181 /* We do not want to include any of the sections in a dynamic
4182 object in the output file. We hack by simply clobbering the
4183 list of sections in the BFD. This could be handled more
4184 cleanly by, say, a new section flag; the existing
4185 SEC_NEVER_LOAD flag is not the one we want, because that one
4186 still implies that the section takes up space in the output
4187 file. */
4188 bfd_section_list_clear (abfd);
4189
4190 /* Find the name to use in a DT_NEEDED entry that refers to this
4191 object. If the object has a DT_SONAME entry, we use it.
4192 Otherwise, if the generic linker stuck something in
4193 elf_dt_name, we use that. Otherwise, we just use the file
4194 name. */
4195 if (soname == NULL || *soname == '\0')
4196 {
4197 soname = elf_dt_name (abfd);
4198 if (soname == NULL || *soname == '\0')
4199 soname = bfd_get_filename (abfd);
4200 }
4201
4202 /* Save the SONAME because sometimes the linker emulation code
4203 will need to know it. */
4204 elf_dt_name (abfd) = soname;
4205
4206 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4207 if (ret < 0)
4208 goto error_return;
4209
4210 /* If we have already included this dynamic object in the
4211 link, just ignore it. There is no reason to include a
4212 particular dynamic object more than once. */
4213 if (ret > 0)
4214 return TRUE;
4215
4216 /* Save the DT_AUDIT entry for the linker emulation code. */
4217 elf_dt_audit (abfd) = audit;
4218 }
4219
4220 /* If this is a dynamic object, we always link against the .dynsym
4221 symbol table, not the .symtab symbol table. The dynamic linker
4222 will only see the .dynsym symbol table, so there is no reason to
4223 look at .symtab for a dynamic object. */
4224
4225 if (! dynamic || elf_dynsymtab (abfd) == 0)
4226 hdr = &elf_tdata (abfd)->symtab_hdr;
4227 else
4228 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4229
4230 symcount = hdr->sh_size / bed->s->sizeof_sym;
4231
4232 /* The sh_info field of the symtab header tells us where the
4233 external symbols start. We don't care about the local symbols at
4234 this point. */
4235 if (elf_bad_symtab (abfd))
4236 {
4237 extsymcount = symcount;
4238 extsymoff = 0;
4239 }
4240 else
4241 {
4242 extsymcount = symcount - hdr->sh_info;
4243 extsymoff = hdr->sh_info;
4244 }
4245
4246 sym_hash = elf_sym_hashes (abfd);
4247 if (extsymcount != 0)
4248 {
4249 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4250 NULL, NULL, NULL);
4251 if (isymbuf == NULL)
4252 goto error_return;
4253
4254 if (sym_hash == NULL)
4255 {
4256 /* We store a pointer to the hash table entry for each
4257 external symbol. */
4258 amt = extsymcount;
4259 amt *= sizeof (struct elf_link_hash_entry *);
4260 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4261 if (sym_hash == NULL)
4262 goto error_free_sym;
4263 elf_sym_hashes (abfd) = sym_hash;
4264 }
4265 }
4266
4267 if (dynamic)
4268 {
4269 /* Read in any version definitions. */
4270 if (!_bfd_elf_slurp_version_tables (abfd,
4271 info->default_imported_symver))
4272 goto error_free_sym;
4273
4274 /* Read in the symbol versions, but don't bother to convert them
4275 to internal format. */
4276 if (elf_dynversym (abfd) != 0)
4277 {
4278 Elf_Internal_Shdr *versymhdr;
4279
4280 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4281 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4282 if (extversym == NULL)
4283 goto error_free_sym;
4284 amt = versymhdr->sh_size;
4285 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4286 || bfd_bread (extversym, amt, abfd) != amt)
4287 goto error_free_vers;
4288 }
4289 }
4290
4291 /* If we are loading an as-needed shared lib, save the symbol table
4292 state before we start adding symbols. If the lib turns out
4293 to be unneeded, restore the state. */
4294 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4295 {
4296 unsigned int i;
4297 size_t entsize;
4298
4299 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4300 {
4301 struct bfd_hash_entry *p;
4302 struct elf_link_hash_entry *h;
4303
4304 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4305 {
4306 h = (struct elf_link_hash_entry *) p;
4307 entsize += htab->root.table.entsize;
4308 if (h->root.type == bfd_link_hash_warning)
4309 entsize += htab->root.table.entsize;
4310 }
4311 }
4312
4313 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4314 old_tab = bfd_malloc (tabsize + entsize);
4315 if (old_tab == NULL)
4316 goto error_free_vers;
4317
4318 /* Remember the current objalloc pointer, so that all mem for
4319 symbols added can later be reclaimed. */
4320 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4321 if (alloc_mark == NULL)
4322 goto error_free_vers;
4323
4324 /* Make a special call to the linker "notice" function to
4325 tell it that we are about to handle an as-needed lib. */
4326 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4327 goto error_free_vers;
4328
4329 /* Clone the symbol table. Remember some pointers into the
4330 symbol table, and dynamic symbol count. */
4331 old_ent = (char *) old_tab + tabsize;
4332 memcpy (old_tab, htab->root.table.table, tabsize);
4333 old_undefs = htab->root.undefs;
4334 old_undefs_tail = htab->root.undefs_tail;
4335 old_table = htab->root.table.table;
4336 old_size = htab->root.table.size;
4337 old_count = htab->root.table.count;
4338 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4339 if (old_strtab == NULL)
4340 goto error_free_vers;
4341
4342 for (i = 0; i < htab->root.table.size; i++)
4343 {
4344 struct bfd_hash_entry *p;
4345 struct elf_link_hash_entry *h;
4346
4347 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4348 {
4349 memcpy (old_ent, p, htab->root.table.entsize);
4350 old_ent = (char *) old_ent + htab->root.table.entsize;
4351 h = (struct elf_link_hash_entry *) p;
4352 if (h->root.type == bfd_link_hash_warning)
4353 {
4354 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4355 old_ent = (char *) old_ent + htab->root.table.entsize;
4356 }
4357 }
4358 }
4359 }
4360
4361 weaks = NULL;
4362 ever = extversym != NULL ? extversym + extsymoff : NULL;
4363 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4364 isym < isymend;
4365 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4366 {
4367 int bind;
4368 bfd_vma value;
4369 asection *sec, *new_sec;
4370 flagword flags;
4371 const char *name;
4372 struct elf_link_hash_entry *h;
4373 struct elf_link_hash_entry *hi;
4374 bfd_boolean definition;
4375 bfd_boolean size_change_ok;
4376 bfd_boolean type_change_ok;
4377 bfd_boolean new_weak;
4378 bfd_boolean old_weak;
4379 bfd_boolean override;
4380 bfd_boolean common;
4381 bfd_boolean discarded;
4382 unsigned int old_alignment;
4383 bfd *old_bfd;
4384 bfd_boolean matched;
4385
4386 override = FALSE;
4387
4388 flags = BSF_NO_FLAGS;
4389 sec = NULL;
4390 value = isym->st_value;
4391 common = bed->common_definition (isym);
4392 if (common && info->inhibit_common_definition)
4393 {
4394 /* Treat common symbol as undefined for --no-define-common. */
4395 isym->st_shndx = SHN_UNDEF;
4396 common = FALSE;
4397 }
4398 discarded = FALSE;
4399
4400 bind = ELF_ST_BIND (isym->st_info);
4401 switch (bind)
4402 {
4403 case STB_LOCAL:
4404 /* This should be impossible, since ELF requires that all
4405 global symbols follow all local symbols, and that sh_info
4406 point to the first global symbol. Unfortunately, Irix 5
4407 screws this up. */
4408 continue;
4409
4410 case STB_GLOBAL:
4411 if (isym->st_shndx != SHN_UNDEF && !common)
4412 flags = BSF_GLOBAL;
4413 break;
4414
4415 case STB_WEAK:
4416 flags = BSF_WEAK;
4417 break;
4418
4419 case STB_GNU_UNIQUE:
4420 flags = BSF_GNU_UNIQUE;
4421 break;
4422
4423 default:
4424 /* Leave it up to the processor backend. */
4425 break;
4426 }
4427
4428 if (isym->st_shndx == SHN_UNDEF)
4429 sec = bfd_und_section_ptr;
4430 else if (isym->st_shndx == SHN_ABS)
4431 sec = bfd_abs_section_ptr;
4432 else if (isym->st_shndx == SHN_COMMON)
4433 {
4434 sec = bfd_com_section_ptr;
4435 /* What ELF calls the size we call the value. What ELF
4436 calls the value we call the alignment. */
4437 value = isym->st_size;
4438 }
4439 else
4440 {
4441 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4442 if (sec == NULL)
4443 sec = bfd_abs_section_ptr;
4444 else if (discarded_section (sec))
4445 {
4446 /* Symbols from discarded section are undefined. We keep
4447 its visibility. */
4448 sec = bfd_und_section_ptr;
4449 discarded = TRUE;
4450 isym->st_shndx = SHN_UNDEF;
4451 }
4452 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4453 value -= sec->vma;
4454 }
4455
4456 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4457 isym->st_name);
4458 if (name == NULL)
4459 goto error_free_vers;
4460
4461 if (isym->st_shndx == SHN_COMMON
4462 && (abfd->flags & BFD_PLUGIN) != 0)
4463 {
4464 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4465
4466 if (xc == NULL)
4467 {
4468 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4469 | SEC_EXCLUDE);
4470 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4471 if (xc == NULL)
4472 goto error_free_vers;
4473 }
4474 sec = xc;
4475 }
4476 else if (isym->st_shndx == SHN_COMMON
4477 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4478 && !bfd_link_relocatable (info))
4479 {
4480 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4481
4482 if (tcomm == NULL)
4483 {
4484 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4485 | SEC_LINKER_CREATED);
4486 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4487 if (tcomm == NULL)
4488 goto error_free_vers;
4489 }
4490 sec = tcomm;
4491 }
4492 else if (bed->elf_add_symbol_hook)
4493 {
4494 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4495 &sec, &value))
4496 goto error_free_vers;
4497
4498 /* The hook function sets the name to NULL if this symbol
4499 should be skipped for some reason. */
4500 if (name == NULL)
4501 continue;
4502 }
4503
4504 /* Sanity check that all possibilities were handled. */
4505 if (sec == NULL)
4506 {
4507 bfd_set_error (bfd_error_bad_value);
4508 goto error_free_vers;
4509 }
4510
4511 /* Silently discard TLS symbols from --just-syms. There's
4512 no way to combine a static TLS block with a new TLS block
4513 for this executable. */
4514 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4515 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4516 continue;
4517
4518 if (bfd_is_und_section (sec)
4519 || bfd_is_com_section (sec))
4520 definition = FALSE;
4521 else
4522 definition = TRUE;
4523
4524 size_change_ok = FALSE;
4525 type_change_ok = bed->type_change_ok;
4526 old_weak = FALSE;
4527 matched = FALSE;
4528 old_alignment = 0;
4529 old_bfd = NULL;
4530 new_sec = sec;
4531
4532 if (is_elf_hash_table (htab))
4533 {
4534 Elf_Internal_Versym iver;
4535 unsigned int vernum = 0;
4536 bfd_boolean skip;
4537
4538 if (ever == NULL)
4539 {
4540 if (info->default_imported_symver)
4541 /* Use the default symbol version created earlier. */
4542 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4543 else
4544 iver.vs_vers = 0;
4545 }
4546 else
4547 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4548
4549 vernum = iver.vs_vers & VERSYM_VERSION;
4550
4551 /* If this is a hidden symbol, or if it is not version
4552 1, we append the version name to the symbol name.
4553 However, we do not modify a non-hidden absolute symbol
4554 if it is not a function, because it might be the version
4555 symbol itself. FIXME: What if it isn't? */
4556 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4557 || (vernum > 1
4558 && (!bfd_is_abs_section (sec)
4559 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4560 {
4561 const char *verstr;
4562 size_t namelen, verlen, newlen;
4563 char *newname, *p;
4564
4565 if (isym->st_shndx != SHN_UNDEF)
4566 {
4567 if (vernum > elf_tdata (abfd)->cverdefs)
4568 verstr = NULL;
4569 else if (vernum > 1)
4570 verstr =
4571 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4572 else
4573 verstr = "";
4574
4575 if (verstr == NULL)
4576 {
4577 _bfd_error_handler
4578 /* xgettext:c-format */
4579 (_("%pB: %s: invalid version %u (max %d)"),
4580 abfd, name, vernum,
4581 elf_tdata (abfd)->cverdefs);
4582 bfd_set_error (bfd_error_bad_value);
4583 goto error_free_vers;
4584 }
4585 }
4586 else
4587 {
4588 /* We cannot simply test for the number of
4589 entries in the VERNEED section since the
4590 numbers for the needed versions do not start
4591 at 0. */
4592 Elf_Internal_Verneed *t;
4593
4594 verstr = NULL;
4595 for (t = elf_tdata (abfd)->verref;
4596 t != NULL;
4597 t = t->vn_nextref)
4598 {
4599 Elf_Internal_Vernaux *a;
4600
4601 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4602 {
4603 if (a->vna_other == vernum)
4604 {
4605 verstr = a->vna_nodename;
4606 break;
4607 }
4608 }
4609 if (a != NULL)
4610 break;
4611 }
4612 if (verstr == NULL)
4613 {
4614 _bfd_error_handler
4615 /* xgettext:c-format */
4616 (_("%pB: %s: invalid needed version %d"),
4617 abfd, name, vernum);
4618 bfd_set_error (bfd_error_bad_value);
4619 goto error_free_vers;
4620 }
4621 }
4622
4623 namelen = strlen (name);
4624 verlen = strlen (verstr);
4625 newlen = namelen + verlen + 2;
4626 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4627 && isym->st_shndx != SHN_UNDEF)
4628 ++newlen;
4629
4630 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4631 if (newname == NULL)
4632 goto error_free_vers;
4633 memcpy (newname, name, namelen);
4634 p = newname + namelen;
4635 *p++ = ELF_VER_CHR;
4636 /* If this is a defined non-hidden version symbol,
4637 we add another @ to the name. This indicates the
4638 default version of the symbol. */
4639 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4640 && isym->st_shndx != SHN_UNDEF)
4641 *p++ = ELF_VER_CHR;
4642 memcpy (p, verstr, verlen + 1);
4643
4644 name = newname;
4645 }
4646
4647 /* If this symbol has default visibility and the user has
4648 requested we not re-export it, then mark it as hidden. */
4649 if (!bfd_is_und_section (sec)
4650 && !dynamic
4651 && abfd->no_export
4652 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4653 isym->st_other = (STV_HIDDEN
4654 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4655
4656 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4657 sym_hash, &old_bfd, &old_weak,
4658 &old_alignment, &skip, &override,
4659 &type_change_ok, &size_change_ok,
4660 &matched))
4661 goto error_free_vers;
4662
4663 if (skip)
4664 continue;
4665
4666 /* Override a definition only if the new symbol matches the
4667 existing one. */
4668 if (override && matched)
4669 definition = FALSE;
4670
4671 h = *sym_hash;
4672 while (h->root.type == bfd_link_hash_indirect
4673 || h->root.type == bfd_link_hash_warning)
4674 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4675
4676 if (elf_tdata (abfd)->verdef != NULL
4677 && vernum > 1
4678 && definition)
4679 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4680 }
4681
4682 if (! (_bfd_generic_link_add_one_symbol
4683 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4684 (struct bfd_link_hash_entry **) sym_hash)))
4685 goto error_free_vers;
4686
4687 if ((abfd->flags & DYNAMIC) == 0
4688 && (bfd_get_flavour (info->output_bfd)
4689 == bfd_target_elf_flavour))
4690 {
4691 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4692 elf_tdata (info->output_bfd)->has_gnu_symbols
4693 |= elf_gnu_symbol_ifunc;
4694 if ((flags & BSF_GNU_UNIQUE))
4695 elf_tdata (info->output_bfd)->has_gnu_symbols
4696 |= elf_gnu_symbol_unique;
4697 }
4698
4699 h = *sym_hash;
4700 /* We need to make sure that indirect symbol dynamic flags are
4701 updated. */
4702 hi = h;
4703 while (h->root.type == bfd_link_hash_indirect
4704 || h->root.type == bfd_link_hash_warning)
4705 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4706
4707 /* Setting the index to -3 tells elf_link_output_extsym that
4708 this symbol is defined in a discarded section. */
4709 if (discarded)
4710 h->indx = -3;
4711
4712 *sym_hash = h;
4713
4714 new_weak = (flags & BSF_WEAK) != 0;
4715 if (dynamic
4716 && definition
4717 && new_weak
4718 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4719 && is_elf_hash_table (htab)
4720 && h->u.alias == NULL)
4721 {
4722 /* Keep a list of all weak defined non function symbols from
4723 a dynamic object, using the alias field. Later in this
4724 function we will set the alias field to the correct
4725 value. We only put non-function symbols from dynamic
4726 objects on this list, because that happens to be the only
4727 time we need to know the normal symbol corresponding to a
4728 weak symbol, and the information is time consuming to
4729 figure out. If the alias field is not already NULL,
4730 then this symbol was already defined by some previous
4731 dynamic object, and we will be using that previous
4732 definition anyhow. */
4733
4734 h->u.alias = weaks;
4735 weaks = h;
4736 }
4737
4738 /* Set the alignment of a common symbol. */
4739 if ((common || bfd_is_com_section (sec))
4740 && h->root.type == bfd_link_hash_common)
4741 {
4742 unsigned int align;
4743
4744 if (common)
4745 align = bfd_log2 (isym->st_value);
4746 else
4747 {
4748 /* The new symbol is a common symbol in a shared object.
4749 We need to get the alignment from the section. */
4750 align = new_sec->alignment_power;
4751 }
4752 if (align > old_alignment)
4753 h->root.u.c.p->alignment_power = align;
4754 else
4755 h->root.u.c.p->alignment_power = old_alignment;
4756 }
4757
4758 if (is_elf_hash_table (htab))
4759 {
4760 /* Set a flag in the hash table entry indicating the type of
4761 reference or definition we just found. A dynamic symbol
4762 is one which is referenced or defined by both a regular
4763 object and a shared object. */
4764 bfd_boolean dynsym = FALSE;
4765
4766 /* Plugin symbols aren't normal. Don't set def_regular or
4767 ref_regular for them, or make them dynamic. */
4768 if ((abfd->flags & BFD_PLUGIN) != 0)
4769 ;
4770 else if (! dynamic)
4771 {
4772 if (! definition)
4773 {
4774 h->ref_regular = 1;
4775 if (bind != STB_WEAK)
4776 h->ref_regular_nonweak = 1;
4777 }
4778 else
4779 {
4780 h->def_regular = 1;
4781 if (h->def_dynamic)
4782 {
4783 h->def_dynamic = 0;
4784 h->ref_dynamic = 1;
4785 }
4786 }
4787
4788 /* If the indirect symbol has been forced local, don't
4789 make the real symbol dynamic. */
4790 if ((h == hi || !hi->forced_local)
4791 && (bfd_link_dll (info)
4792 || h->def_dynamic
4793 || h->ref_dynamic))
4794 dynsym = TRUE;
4795 }
4796 else
4797 {
4798 if (! definition)
4799 {
4800 h->ref_dynamic = 1;
4801 hi->ref_dynamic = 1;
4802 }
4803 else
4804 {
4805 h->def_dynamic = 1;
4806 hi->def_dynamic = 1;
4807 }
4808
4809 /* If the indirect symbol has been forced local, don't
4810 make the real symbol dynamic. */
4811 if ((h == hi || !hi->forced_local)
4812 && (h->def_regular
4813 || h->ref_regular
4814 || (h->is_weakalias
4815 && weakdef (h)->dynindx != -1)))
4816 dynsym = TRUE;
4817 }
4818
4819 /* Check to see if we need to add an indirect symbol for
4820 the default name. */
4821 if (definition
4822 || (!override && h->root.type == bfd_link_hash_common))
4823 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4824 sec, value, &old_bfd, &dynsym))
4825 goto error_free_vers;
4826
4827 /* Check the alignment when a common symbol is involved. This
4828 can change when a common symbol is overridden by a normal
4829 definition or a common symbol is ignored due to the old
4830 normal definition. We need to make sure the maximum
4831 alignment is maintained. */
4832 if ((old_alignment || common)
4833 && h->root.type != bfd_link_hash_common)
4834 {
4835 unsigned int common_align;
4836 unsigned int normal_align;
4837 unsigned int symbol_align;
4838 bfd *normal_bfd;
4839 bfd *common_bfd;
4840
4841 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4842 || h->root.type == bfd_link_hash_defweak);
4843
4844 symbol_align = ffs (h->root.u.def.value) - 1;
4845 if (h->root.u.def.section->owner != NULL
4846 && (h->root.u.def.section->owner->flags
4847 & (DYNAMIC | BFD_PLUGIN)) == 0)
4848 {
4849 normal_align = h->root.u.def.section->alignment_power;
4850 if (normal_align > symbol_align)
4851 normal_align = symbol_align;
4852 }
4853 else
4854 normal_align = symbol_align;
4855
4856 if (old_alignment)
4857 {
4858 common_align = old_alignment;
4859 common_bfd = old_bfd;
4860 normal_bfd = abfd;
4861 }
4862 else
4863 {
4864 common_align = bfd_log2 (isym->st_value);
4865 common_bfd = abfd;
4866 normal_bfd = old_bfd;
4867 }
4868
4869 if (normal_align < common_align)
4870 {
4871 /* PR binutils/2735 */
4872 if (normal_bfd == NULL)
4873 _bfd_error_handler
4874 /* xgettext:c-format */
4875 (_("warning: alignment %u of common symbol `%s' in %pB is"
4876 " greater than the alignment (%u) of its section %pA"),
4877 1 << common_align, name, common_bfd,
4878 1 << normal_align, h->root.u.def.section);
4879 else
4880 _bfd_error_handler
4881 /* xgettext:c-format */
4882 (_("warning: alignment %u of symbol `%s' in %pB"
4883 " is smaller than %u in %pB"),
4884 1 << normal_align, name, normal_bfd,
4885 1 << common_align, common_bfd);
4886 }
4887 }
4888
4889 /* Remember the symbol size if it isn't undefined. */
4890 if (isym->st_size != 0
4891 && isym->st_shndx != SHN_UNDEF
4892 && (definition || h->size == 0))
4893 {
4894 if (h->size != 0
4895 && h->size != isym->st_size
4896 && ! size_change_ok)
4897 _bfd_error_handler
4898 /* xgettext:c-format */
4899 (_("warning: size of symbol `%s' changed"
4900 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
4901 name, (uint64_t) h->size, old_bfd,
4902 (uint64_t) isym->st_size, abfd);
4903
4904 h->size = isym->st_size;
4905 }
4906
4907 /* If this is a common symbol, then we always want H->SIZE
4908 to be the size of the common symbol. The code just above
4909 won't fix the size if a common symbol becomes larger. We
4910 don't warn about a size change here, because that is
4911 covered by --warn-common. Allow changes between different
4912 function types. */
4913 if (h->root.type == bfd_link_hash_common)
4914 h->size = h->root.u.c.size;
4915
4916 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4917 && ((definition && !new_weak)
4918 || (old_weak && h->root.type == bfd_link_hash_common)
4919 || h->type == STT_NOTYPE))
4920 {
4921 unsigned int type = ELF_ST_TYPE (isym->st_info);
4922
4923 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4924 symbol. */
4925 if (type == STT_GNU_IFUNC
4926 && (abfd->flags & DYNAMIC) != 0)
4927 type = STT_FUNC;
4928
4929 if (h->type != type)
4930 {
4931 if (h->type != STT_NOTYPE && ! type_change_ok)
4932 /* xgettext:c-format */
4933 _bfd_error_handler
4934 (_("warning: type of symbol `%s' changed"
4935 " from %d to %d in %pB"),
4936 name, h->type, type, abfd);
4937
4938 h->type = type;
4939 }
4940 }
4941
4942 /* Merge st_other field. */
4943 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4944
4945 /* We don't want to make debug symbol dynamic. */
4946 if (definition
4947 && (sec->flags & SEC_DEBUGGING)
4948 && !bfd_link_relocatable (info))
4949 dynsym = FALSE;
4950
4951 /* Nor should we make plugin symbols dynamic. */
4952 if ((abfd->flags & BFD_PLUGIN) != 0)
4953 dynsym = FALSE;
4954
4955 if (definition)
4956 {
4957 h->target_internal = isym->st_target_internal;
4958 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4959 }
4960
4961 if (definition && !dynamic)
4962 {
4963 char *p = strchr (name, ELF_VER_CHR);
4964 if (p != NULL && p[1] != ELF_VER_CHR)
4965 {
4966 /* Queue non-default versions so that .symver x, x@FOO
4967 aliases can be checked. */
4968 if (!nondeflt_vers)
4969 {
4970 amt = ((isymend - isym + 1)
4971 * sizeof (struct elf_link_hash_entry *));
4972 nondeflt_vers
4973 = (struct elf_link_hash_entry **) bfd_malloc (amt);
4974 if (!nondeflt_vers)
4975 goto error_free_vers;
4976 }
4977 nondeflt_vers[nondeflt_vers_cnt++] = h;
4978 }
4979 }
4980
4981 if (dynsym && h->dynindx == -1)
4982 {
4983 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4984 goto error_free_vers;
4985 if (h->is_weakalias
4986 && weakdef (h)->dynindx == -1)
4987 {
4988 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
4989 goto error_free_vers;
4990 }
4991 }
4992 else if (h->dynindx != -1)
4993 /* If the symbol already has a dynamic index, but
4994 visibility says it should not be visible, turn it into
4995 a local symbol. */
4996 switch (ELF_ST_VISIBILITY (h->other))
4997 {
4998 case STV_INTERNAL:
4999 case STV_HIDDEN:
5000 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5001 dynsym = FALSE;
5002 break;
5003 }
5004
5005 /* Don't add DT_NEEDED for references from the dummy bfd nor
5006 for unmatched symbol. */
5007 if (!add_needed
5008 && matched
5009 && definition
5010 && ((dynsym
5011 && h->ref_regular_nonweak
5012 && (old_bfd == NULL
5013 || (old_bfd->flags & BFD_PLUGIN) == 0))
5014 || (h->ref_dynamic_nonweak
5015 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5016 && !on_needed_list (elf_dt_name (abfd),
5017 htab->needed, NULL))))
5018 {
5019 int ret;
5020 const char *soname = elf_dt_name (abfd);
5021
5022 info->callbacks->minfo ("%!", soname, old_bfd,
5023 h->root.root.string);
5024
5025 /* A symbol from a library loaded via DT_NEEDED of some
5026 other library is referenced by a regular object.
5027 Add a DT_NEEDED entry for it. Issue an error if
5028 --no-add-needed is used and the reference was not
5029 a weak one. */
5030 if (old_bfd != NULL
5031 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5032 {
5033 _bfd_error_handler
5034 /* xgettext:c-format */
5035 (_("%pB: undefined reference to symbol '%s'"),
5036 old_bfd, name);
5037 bfd_set_error (bfd_error_missing_dso);
5038 goto error_free_vers;
5039 }
5040
5041 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5042 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5043
5044 add_needed = TRUE;
5045 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
5046 if (ret < 0)
5047 goto error_free_vers;
5048
5049 BFD_ASSERT (ret == 0);
5050 }
5051 }
5052 }
5053
5054 if (info->lto_plugin_active
5055 && !bfd_link_relocatable (info)
5056 && (abfd->flags & BFD_PLUGIN) == 0
5057 && !just_syms
5058 && extsymcount)
5059 {
5060 int r_sym_shift;
5061
5062 if (bed->s->arch_size == 32)
5063 r_sym_shift = 8;
5064 else
5065 r_sym_shift = 32;
5066
5067 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5068 referenced in regular objects so that linker plugin will get
5069 the correct symbol resolution. */
5070
5071 sym_hash = elf_sym_hashes (abfd);
5072 for (s = abfd->sections; s != NULL; s = s->next)
5073 {
5074 Elf_Internal_Rela *internal_relocs;
5075 Elf_Internal_Rela *rel, *relend;
5076
5077 /* Don't check relocations in excluded sections. */
5078 if ((s->flags & SEC_RELOC) == 0
5079 || s->reloc_count == 0
5080 || (s->flags & SEC_EXCLUDE) != 0
5081 || ((info->strip == strip_all
5082 || info->strip == strip_debugger)
5083 && (s->flags & SEC_DEBUGGING) != 0))
5084 continue;
5085
5086 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5087 NULL,
5088 info->keep_memory);
5089 if (internal_relocs == NULL)
5090 goto error_free_vers;
5091
5092 rel = internal_relocs;
5093 relend = rel + s->reloc_count;
5094 for ( ; rel < relend; rel++)
5095 {
5096 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5097 struct elf_link_hash_entry *h;
5098
5099 /* Skip local symbols. */
5100 if (r_symndx < extsymoff)
5101 continue;
5102
5103 h = sym_hash[r_symndx - extsymoff];
5104 if (h != NULL)
5105 h->root.non_ir_ref_regular = 1;
5106 }
5107
5108 if (elf_section_data (s)->relocs != internal_relocs)
5109 free (internal_relocs);
5110 }
5111 }
5112
5113 if (extversym != NULL)
5114 {
5115 free (extversym);
5116 extversym = NULL;
5117 }
5118
5119 if (isymbuf != NULL)
5120 {
5121 free (isymbuf);
5122 isymbuf = NULL;
5123 }
5124
5125 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5126 {
5127 unsigned int i;
5128
5129 /* Restore the symbol table. */
5130 old_ent = (char *) old_tab + tabsize;
5131 memset (elf_sym_hashes (abfd), 0,
5132 extsymcount * sizeof (struct elf_link_hash_entry *));
5133 htab->root.table.table = old_table;
5134 htab->root.table.size = old_size;
5135 htab->root.table.count = old_count;
5136 memcpy (htab->root.table.table, old_tab, tabsize);
5137 htab->root.undefs = old_undefs;
5138 htab->root.undefs_tail = old_undefs_tail;
5139 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5140 free (old_strtab);
5141 old_strtab = NULL;
5142 for (i = 0; i < htab->root.table.size; i++)
5143 {
5144 struct bfd_hash_entry *p;
5145 struct elf_link_hash_entry *h;
5146 bfd_size_type size;
5147 unsigned int alignment_power;
5148 unsigned int non_ir_ref_dynamic;
5149
5150 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5151 {
5152 h = (struct elf_link_hash_entry *) p;
5153 if (h->root.type == bfd_link_hash_warning)
5154 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5155
5156 /* Preserve the maximum alignment and size for common
5157 symbols even if this dynamic lib isn't on DT_NEEDED
5158 since it can still be loaded at run time by another
5159 dynamic lib. */
5160 if (h->root.type == bfd_link_hash_common)
5161 {
5162 size = h->root.u.c.size;
5163 alignment_power = h->root.u.c.p->alignment_power;
5164 }
5165 else
5166 {
5167 size = 0;
5168 alignment_power = 0;
5169 }
5170 /* Preserve non_ir_ref_dynamic so that this symbol
5171 will be exported when the dynamic lib becomes needed
5172 in the second pass. */
5173 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5174 memcpy (p, old_ent, htab->root.table.entsize);
5175 old_ent = (char *) old_ent + htab->root.table.entsize;
5176 h = (struct elf_link_hash_entry *) p;
5177 if (h->root.type == bfd_link_hash_warning)
5178 {
5179 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5180 old_ent = (char *) old_ent + htab->root.table.entsize;
5181 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5182 }
5183 if (h->root.type == bfd_link_hash_common)
5184 {
5185 if (size > h->root.u.c.size)
5186 h->root.u.c.size = size;
5187 if (alignment_power > h->root.u.c.p->alignment_power)
5188 h->root.u.c.p->alignment_power = alignment_power;
5189 }
5190 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5191 }
5192 }
5193
5194 /* Make a special call to the linker "notice" function to
5195 tell it that symbols added for crefs may need to be removed. */
5196 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5197 goto error_free_vers;
5198
5199 free (old_tab);
5200 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5201 alloc_mark);
5202 if (nondeflt_vers != NULL)
5203 free (nondeflt_vers);
5204 return TRUE;
5205 }
5206
5207 if (old_tab != NULL)
5208 {
5209 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5210 goto error_free_vers;
5211 free (old_tab);
5212 old_tab = NULL;
5213 }
5214
5215 /* Now that all the symbols from this input file are created, if
5216 not performing a relocatable link, handle .symver foo, foo@BAR
5217 such that any relocs against foo become foo@BAR. */
5218 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5219 {
5220 size_t cnt, symidx;
5221
5222 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5223 {
5224 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5225 char *shortname, *p;
5226
5227 p = strchr (h->root.root.string, ELF_VER_CHR);
5228 if (p == NULL
5229 || (h->root.type != bfd_link_hash_defined
5230 && h->root.type != bfd_link_hash_defweak))
5231 continue;
5232
5233 amt = p - h->root.root.string;
5234 shortname = (char *) bfd_malloc (amt + 1);
5235 if (!shortname)
5236 goto error_free_vers;
5237 memcpy (shortname, h->root.root.string, amt);
5238 shortname[amt] = '\0';
5239
5240 hi = (struct elf_link_hash_entry *)
5241 bfd_link_hash_lookup (&htab->root, shortname,
5242 FALSE, FALSE, FALSE);
5243 if (hi != NULL
5244 && hi->root.type == h->root.type
5245 && hi->root.u.def.value == h->root.u.def.value
5246 && hi->root.u.def.section == h->root.u.def.section)
5247 {
5248 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5249 hi->root.type = bfd_link_hash_indirect;
5250 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5251 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5252 sym_hash = elf_sym_hashes (abfd);
5253 if (sym_hash)
5254 for (symidx = 0; symidx < extsymcount; ++symidx)
5255 if (sym_hash[symidx] == hi)
5256 {
5257 sym_hash[symidx] = h;
5258 break;
5259 }
5260 }
5261 free (shortname);
5262 }
5263 free (nondeflt_vers);
5264 nondeflt_vers = NULL;
5265 }
5266
5267 /* Now set the alias field correctly for all the weak defined
5268 symbols we found. The only way to do this is to search all the
5269 symbols. Since we only need the information for non functions in
5270 dynamic objects, that's the only time we actually put anything on
5271 the list WEAKS. We need this information so that if a regular
5272 object refers to a symbol defined weakly in a dynamic object, the
5273 real symbol in the dynamic object is also put in the dynamic
5274 symbols; we also must arrange for both symbols to point to the
5275 same memory location. We could handle the general case of symbol
5276 aliasing, but a general symbol alias can only be generated in
5277 assembler code, handling it correctly would be very time
5278 consuming, and other ELF linkers don't handle general aliasing
5279 either. */
5280 if (weaks != NULL)
5281 {
5282 struct elf_link_hash_entry **hpp;
5283 struct elf_link_hash_entry **hppend;
5284 struct elf_link_hash_entry **sorted_sym_hash;
5285 struct elf_link_hash_entry *h;
5286 size_t sym_count;
5287
5288 /* Since we have to search the whole symbol list for each weak
5289 defined symbol, search time for N weak defined symbols will be
5290 O(N^2). Binary search will cut it down to O(NlogN). */
5291 amt = extsymcount;
5292 amt *= sizeof (struct elf_link_hash_entry *);
5293 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
5294 if (sorted_sym_hash == NULL)
5295 goto error_return;
5296 sym_hash = sorted_sym_hash;
5297 hpp = elf_sym_hashes (abfd);
5298 hppend = hpp + extsymcount;
5299 sym_count = 0;
5300 for (; hpp < hppend; hpp++)
5301 {
5302 h = *hpp;
5303 if (h != NULL
5304 && h->root.type == bfd_link_hash_defined
5305 && !bed->is_function_type (h->type))
5306 {
5307 *sym_hash = h;
5308 sym_hash++;
5309 sym_count++;
5310 }
5311 }
5312
5313 qsort (sorted_sym_hash, sym_count,
5314 sizeof (struct elf_link_hash_entry *),
5315 elf_sort_symbol);
5316
5317 while (weaks != NULL)
5318 {
5319 struct elf_link_hash_entry *hlook;
5320 asection *slook;
5321 bfd_vma vlook;
5322 size_t i, j, idx = 0;
5323
5324 hlook = weaks;
5325 weaks = hlook->u.alias;
5326 hlook->u.alias = NULL;
5327
5328 if (hlook->root.type != bfd_link_hash_defined
5329 && hlook->root.type != bfd_link_hash_defweak)
5330 continue;
5331
5332 slook = hlook->root.u.def.section;
5333 vlook = hlook->root.u.def.value;
5334
5335 i = 0;
5336 j = sym_count;
5337 while (i != j)
5338 {
5339 bfd_signed_vma vdiff;
5340 idx = (i + j) / 2;
5341 h = sorted_sym_hash[idx];
5342 vdiff = vlook - h->root.u.def.value;
5343 if (vdiff < 0)
5344 j = idx;
5345 else if (vdiff > 0)
5346 i = idx + 1;
5347 else
5348 {
5349 int sdiff = slook->id - h->root.u.def.section->id;
5350 if (sdiff < 0)
5351 j = idx;
5352 else if (sdiff > 0)
5353 i = idx + 1;
5354 else
5355 break;
5356 }
5357 }
5358
5359 /* We didn't find a value/section match. */
5360 if (i == j)
5361 continue;
5362
5363 /* With multiple aliases, or when the weak symbol is already
5364 strongly defined, we have multiple matching symbols and
5365 the binary search above may land on any of them. Step
5366 one past the matching symbol(s). */
5367 while (++idx != j)
5368 {
5369 h = sorted_sym_hash[idx];
5370 if (h->root.u.def.section != slook
5371 || h->root.u.def.value != vlook)
5372 break;
5373 }
5374
5375 /* Now look back over the aliases. Since we sorted by size
5376 as well as value and section, we'll choose the one with
5377 the largest size. */
5378 while (idx-- != i)
5379 {
5380 h = sorted_sym_hash[idx];
5381
5382 /* Stop if value or section doesn't match. */
5383 if (h->root.u.def.section != slook
5384 || h->root.u.def.value != vlook)
5385 break;
5386 else if (h != hlook)
5387 {
5388 struct elf_link_hash_entry *t;
5389
5390 hlook->u.alias = h;
5391 hlook->is_weakalias = 1;
5392 t = h;
5393 if (t->u.alias != NULL)
5394 while (t->u.alias != h)
5395 t = t->u.alias;
5396 t->u.alias = hlook;
5397
5398 /* If the weak definition is in the list of dynamic
5399 symbols, make sure the real definition is put
5400 there as well. */
5401 if (hlook->dynindx != -1 && h->dynindx == -1)
5402 {
5403 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5404 {
5405 err_free_sym_hash:
5406 free (sorted_sym_hash);
5407 goto error_return;
5408 }
5409 }
5410
5411 /* If the real definition is in the list of dynamic
5412 symbols, make sure the weak definition is put
5413 there as well. If we don't do this, then the
5414 dynamic loader might not merge the entries for the
5415 real definition and the weak definition. */
5416 if (h->dynindx != -1 && hlook->dynindx == -1)
5417 {
5418 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5419 goto err_free_sym_hash;
5420 }
5421 break;
5422 }
5423 }
5424 }
5425
5426 free (sorted_sym_hash);
5427 }
5428
5429 if (bed->check_directives
5430 && !(*bed->check_directives) (abfd, info))
5431 return FALSE;
5432
5433 /* If this is a non-traditional link, try to optimize the handling
5434 of the .stab/.stabstr sections. */
5435 if (! dynamic
5436 && ! info->traditional_format
5437 && is_elf_hash_table (htab)
5438 && (info->strip != strip_all && info->strip != strip_debugger))
5439 {
5440 asection *stabstr;
5441
5442 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5443 if (stabstr != NULL)
5444 {
5445 bfd_size_type string_offset = 0;
5446 asection *stab;
5447
5448 for (stab = abfd->sections; stab; stab = stab->next)
5449 if (CONST_STRNEQ (stab->name, ".stab")
5450 && (!stab->name[5] ||
5451 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5452 && (stab->flags & SEC_MERGE) == 0
5453 && !bfd_is_abs_section (stab->output_section))
5454 {
5455 struct bfd_elf_section_data *secdata;
5456
5457 secdata = elf_section_data (stab);
5458 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5459 stabstr, &secdata->sec_info,
5460 &string_offset))
5461 goto error_return;
5462 if (secdata->sec_info)
5463 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5464 }
5465 }
5466 }
5467
5468 if (is_elf_hash_table (htab) && add_needed)
5469 {
5470 /* Add this bfd to the loaded list. */
5471 struct elf_link_loaded_list *n;
5472
5473 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5474 if (n == NULL)
5475 goto error_return;
5476 n->abfd = abfd;
5477 n->next = htab->loaded;
5478 htab->loaded = n;
5479 }
5480
5481 return TRUE;
5482
5483 error_free_vers:
5484 if (old_tab != NULL)
5485 free (old_tab);
5486 if (old_strtab != NULL)
5487 free (old_strtab);
5488 if (nondeflt_vers != NULL)
5489 free (nondeflt_vers);
5490 if (extversym != NULL)
5491 free (extversym);
5492 error_free_sym:
5493 if (isymbuf != NULL)
5494 free (isymbuf);
5495 error_return:
5496 return FALSE;
5497 }
5498
5499 /* Return the linker hash table entry of a symbol that might be
5500 satisfied by an archive symbol. Return -1 on error. */
5501
5502 struct elf_link_hash_entry *
5503 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5504 struct bfd_link_info *info,
5505 const char *name)
5506 {
5507 struct elf_link_hash_entry *h;
5508 char *p, *copy;
5509 size_t len, first;
5510
5511 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5512 if (h != NULL)
5513 return h;
5514
5515 /* If this is a default version (the name contains @@), look up the
5516 symbol again with only one `@' as well as without the version.
5517 The effect is that references to the symbol with and without the
5518 version will be matched by the default symbol in the archive. */
5519
5520 p = strchr (name, ELF_VER_CHR);
5521 if (p == NULL || p[1] != ELF_VER_CHR)
5522 return h;
5523
5524 /* First check with only one `@'. */
5525 len = strlen (name);
5526 copy = (char *) bfd_alloc (abfd, len);
5527 if (copy == NULL)
5528 return (struct elf_link_hash_entry *) -1;
5529
5530 first = p - name + 1;
5531 memcpy (copy, name, first);
5532 memcpy (copy + first, name + first + 1, len - first);
5533
5534 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5535 if (h == NULL)
5536 {
5537 /* We also need to check references to the symbol without the
5538 version. */
5539 copy[first - 1] = '\0';
5540 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5541 FALSE, FALSE, TRUE);
5542 }
5543
5544 bfd_release (abfd, copy);
5545 return h;
5546 }
5547
5548 /* Add symbols from an ELF archive file to the linker hash table. We
5549 don't use _bfd_generic_link_add_archive_symbols because we need to
5550 handle versioned symbols.
5551
5552 Fortunately, ELF archive handling is simpler than that done by
5553 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5554 oddities. In ELF, if we find a symbol in the archive map, and the
5555 symbol is currently undefined, we know that we must pull in that
5556 object file.
5557
5558 Unfortunately, we do have to make multiple passes over the symbol
5559 table until nothing further is resolved. */
5560
5561 static bfd_boolean
5562 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5563 {
5564 symindex c;
5565 unsigned char *included = NULL;
5566 carsym *symdefs;
5567 bfd_boolean loop;
5568 bfd_size_type amt;
5569 const struct elf_backend_data *bed;
5570 struct elf_link_hash_entry * (*archive_symbol_lookup)
5571 (bfd *, struct bfd_link_info *, const char *);
5572
5573 if (! bfd_has_map (abfd))
5574 {
5575 /* An empty archive is a special case. */
5576 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5577 return TRUE;
5578 bfd_set_error (bfd_error_no_armap);
5579 return FALSE;
5580 }
5581
5582 /* Keep track of all symbols we know to be already defined, and all
5583 files we know to be already included. This is to speed up the
5584 second and subsequent passes. */
5585 c = bfd_ardata (abfd)->symdef_count;
5586 if (c == 0)
5587 return TRUE;
5588 amt = c;
5589 amt *= sizeof (*included);
5590 included = (unsigned char *) bfd_zmalloc (amt);
5591 if (included == NULL)
5592 return FALSE;
5593
5594 symdefs = bfd_ardata (abfd)->symdefs;
5595 bed = get_elf_backend_data (abfd);
5596 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5597
5598 do
5599 {
5600 file_ptr last;
5601 symindex i;
5602 carsym *symdef;
5603 carsym *symdefend;
5604
5605 loop = FALSE;
5606 last = -1;
5607
5608 symdef = symdefs;
5609 symdefend = symdef + c;
5610 for (i = 0; symdef < symdefend; symdef++, i++)
5611 {
5612 struct elf_link_hash_entry *h;
5613 bfd *element;
5614 struct bfd_link_hash_entry *undefs_tail;
5615 symindex mark;
5616
5617 if (included[i])
5618 continue;
5619 if (symdef->file_offset == last)
5620 {
5621 included[i] = TRUE;
5622 continue;
5623 }
5624
5625 h = archive_symbol_lookup (abfd, info, symdef->name);
5626 if (h == (struct elf_link_hash_entry *) -1)
5627 goto error_return;
5628
5629 if (h == NULL)
5630 continue;
5631
5632 if (h->root.type == bfd_link_hash_common)
5633 {
5634 /* We currently have a common symbol. The archive map contains
5635 a reference to this symbol, so we may want to include it. We
5636 only want to include it however, if this archive element
5637 contains a definition of the symbol, not just another common
5638 declaration of it.
5639
5640 Unfortunately some archivers (including GNU ar) will put
5641 declarations of common symbols into their archive maps, as
5642 well as real definitions, so we cannot just go by the archive
5643 map alone. Instead we must read in the element's symbol
5644 table and check that to see what kind of symbol definition
5645 this is. */
5646 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5647 continue;
5648 }
5649 else if (h->root.type != bfd_link_hash_undefined)
5650 {
5651 if (h->root.type != bfd_link_hash_undefweak)
5652 /* Symbol must be defined. Don't check it again. */
5653 included[i] = TRUE;
5654 continue;
5655 }
5656
5657 /* We need to include this archive member. */
5658 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5659 if (element == NULL)
5660 goto error_return;
5661
5662 if (! bfd_check_format (element, bfd_object))
5663 goto error_return;
5664
5665 undefs_tail = info->hash->undefs_tail;
5666
5667 if (!(*info->callbacks
5668 ->add_archive_element) (info, element, symdef->name, &element))
5669 continue;
5670 if (!bfd_link_add_symbols (element, info))
5671 goto error_return;
5672
5673 /* If there are any new undefined symbols, we need to make
5674 another pass through the archive in order to see whether
5675 they can be defined. FIXME: This isn't perfect, because
5676 common symbols wind up on undefs_tail and because an
5677 undefined symbol which is defined later on in this pass
5678 does not require another pass. This isn't a bug, but it
5679 does make the code less efficient than it could be. */
5680 if (undefs_tail != info->hash->undefs_tail)
5681 loop = TRUE;
5682
5683 /* Look backward to mark all symbols from this object file
5684 which we have already seen in this pass. */
5685 mark = i;
5686 do
5687 {
5688 included[mark] = TRUE;
5689 if (mark == 0)
5690 break;
5691 --mark;
5692 }
5693 while (symdefs[mark].file_offset == symdef->file_offset);
5694
5695 /* We mark subsequent symbols from this object file as we go
5696 on through the loop. */
5697 last = symdef->file_offset;
5698 }
5699 }
5700 while (loop);
5701
5702 free (included);
5703
5704 return TRUE;
5705
5706 error_return:
5707 if (included != NULL)
5708 free (included);
5709 return FALSE;
5710 }
5711
5712 /* Given an ELF BFD, add symbols to the global hash table as
5713 appropriate. */
5714
5715 bfd_boolean
5716 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5717 {
5718 switch (bfd_get_format (abfd))
5719 {
5720 case bfd_object:
5721 return elf_link_add_object_symbols (abfd, info);
5722 case bfd_archive:
5723 return elf_link_add_archive_symbols (abfd, info);
5724 default:
5725 bfd_set_error (bfd_error_wrong_format);
5726 return FALSE;
5727 }
5728 }
5729 \f
5730 struct hash_codes_info
5731 {
5732 unsigned long *hashcodes;
5733 bfd_boolean error;
5734 };
5735
5736 /* This function will be called though elf_link_hash_traverse to store
5737 all hash value of the exported symbols in an array. */
5738
5739 static bfd_boolean
5740 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5741 {
5742 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5743 const char *name;
5744 unsigned long ha;
5745 char *alc = NULL;
5746
5747 /* Ignore indirect symbols. These are added by the versioning code. */
5748 if (h->dynindx == -1)
5749 return TRUE;
5750
5751 name = h->root.root.string;
5752 if (h->versioned >= versioned)
5753 {
5754 char *p = strchr (name, ELF_VER_CHR);
5755 if (p != NULL)
5756 {
5757 alc = (char *) bfd_malloc (p - name + 1);
5758 if (alc == NULL)
5759 {
5760 inf->error = TRUE;
5761 return FALSE;
5762 }
5763 memcpy (alc, name, p - name);
5764 alc[p - name] = '\0';
5765 name = alc;
5766 }
5767 }
5768
5769 /* Compute the hash value. */
5770 ha = bfd_elf_hash (name);
5771
5772 /* Store the found hash value in the array given as the argument. */
5773 *(inf->hashcodes)++ = ha;
5774
5775 /* And store it in the struct so that we can put it in the hash table
5776 later. */
5777 h->u.elf_hash_value = ha;
5778
5779 if (alc != NULL)
5780 free (alc);
5781
5782 return TRUE;
5783 }
5784
5785 struct collect_gnu_hash_codes
5786 {
5787 bfd *output_bfd;
5788 const struct elf_backend_data *bed;
5789 unsigned long int nsyms;
5790 unsigned long int maskbits;
5791 unsigned long int *hashcodes;
5792 unsigned long int *hashval;
5793 unsigned long int *indx;
5794 unsigned long int *counts;
5795 bfd_vma *bitmask;
5796 bfd_byte *contents;
5797 long int min_dynindx;
5798 unsigned long int bucketcount;
5799 unsigned long int symindx;
5800 long int local_indx;
5801 long int shift1, shift2;
5802 unsigned long int mask;
5803 bfd_boolean error;
5804 };
5805
5806 /* This function will be called though elf_link_hash_traverse to store
5807 all hash value of the exported symbols in an array. */
5808
5809 static bfd_boolean
5810 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5811 {
5812 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5813 const char *name;
5814 unsigned long ha;
5815 char *alc = NULL;
5816
5817 /* Ignore indirect symbols. These are added by the versioning code. */
5818 if (h->dynindx == -1)
5819 return TRUE;
5820
5821 /* Ignore also local symbols and undefined symbols. */
5822 if (! (*s->bed->elf_hash_symbol) (h))
5823 return TRUE;
5824
5825 name = h->root.root.string;
5826 if (h->versioned >= versioned)
5827 {
5828 char *p = strchr (name, ELF_VER_CHR);
5829 if (p != NULL)
5830 {
5831 alc = (char *) bfd_malloc (p - name + 1);
5832 if (alc == NULL)
5833 {
5834 s->error = TRUE;
5835 return FALSE;
5836 }
5837 memcpy (alc, name, p - name);
5838 alc[p - name] = '\0';
5839 name = alc;
5840 }
5841 }
5842
5843 /* Compute the hash value. */
5844 ha = bfd_elf_gnu_hash (name);
5845
5846 /* Store the found hash value in the array for compute_bucket_count,
5847 and also for .dynsym reordering purposes. */
5848 s->hashcodes[s->nsyms] = ha;
5849 s->hashval[h->dynindx] = ha;
5850 ++s->nsyms;
5851 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5852 s->min_dynindx = h->dynindx;
5853
5854 if (alc != NULL)
5855 free (alc);
5856
5857 return TRUE;
5858 }
5859
5860 /* This function will be called though elf_link_hash_traverse to do
5861 final dynaminc symbol renumbering. */
5862
5863 static bfd_boolean
5864 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5865 {
5866 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5867 unsigned long int bucket;
5868 unsigned long int val;
5869
5870 /* Ignore indirect symbols. */
5871 if (h->dynindx == -1)
5872 return TRUE;
5873
5874 /* Ignore also local symbols and undefined symbols. */
5875 if (! (*s->bed->elf_hash_symbol) (h))
5876 {
5877 if (h->dynindx >= s->min_dynindx)
5878 h->dynindx = s->local_indx++;
5879 return TRUE;
5880 }
5881
5882 bucket = s->hashval[h->dynindx] % s->bucketcount;
5883 val = (s->hashval[h->dynindx] >> s->shift1)
5884 & ((s->maskbits >> s->shift1) - 1);
5885 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5886 s->bitmask[val]
5887 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5888 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5889 if (s->counts[bucket] == 1)
5890 /* Last element terminates the chain. */
5891 val |= 1;
5892 bfd_put_32 (s->output_bfd, val,
5893 s->contents + (s->indx[bucket] - s->symindx) * 4);
5894 --s->counts[bucket];
5895 h->dynindx = s->indx[bucket]++;
5896 return TRUE;
5897 }
5898
5899 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5900
5901 bfd_boolean
5902 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5903 {
5904 return !(h->forced_local
5905 || h->root.type == bfd_link_hash_undefined
5906 || h->root.type == bfd_link_hash_undefweak
5907 || ((h->root.type == bfd_link_hash_defined
5908 || h->root.type == bfd_link_hash_defweak)
5909 && h->root.u.def.section->output_section == NULL));
5910 }
5911
5912 /* Array used to determine the number of hash table buckets to use
5913 based on the number of symbols there are. If there are fewer than
5914 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5915 fewer than 37 we use 17 buckets, and so forth. We never use more
5916 than 32771 buckets. */
5917
5918 static const size_t elf_buckets[] =
5919 {
5920 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5921 16411, 32771, 0
5922 };
5923
5924 /* Compute bucket count for hashing table. We do not use a static set
5925 of possible tables sizes anymore. Instead we determine for all
5926 possible reasonable sizes of the table the outcome (i.e., the
5927 number of collisions etc) and choose the best solution. The
5928 weighting functions are not too simple to allow the table to grow
5929 without bounds. Instead one of the weighting factors is the size.
5930 Therefore the result is always a good payoff between few collisions
5931 (= short chain lengths) and table size. */
5932 static size_t
5933 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5934 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5935 unsigned long int nsyms,
5936 int gnu_hash)
5937 {
5938 size_t best_size = 0;
5939 unsigned long int i;
5940
5941 /* We have a problem here. The following code to optimize the table
5942 size requires an integer type with more the 32 bits. If
5943 BFD_HOST_U_64_BIT is set we know about such a type. */
5944 #ifdef BFD_HOST_U_64_BIT
5945 if (info->optimize)
5946 {
5947 size_t minsize;
5948 size_t maxsize;
5949 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5950 bfd *dynobj = elf_hash_table (info)->dynobj;
5951 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5952 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5953 unsigned long int *counts;
5954 bfd_size_type amt;
5955 unsigned int no_improvement_count = 0;
5956
5957 /* Possible optimization parameters: if we have NSYMS symbols we say
5958 that the hashing table must at least have NSYMS/4 and at most
5959 2*NSYMS buckets. */
5960 minsize = nsyms / 4;
5961 if (minsize == 0)
5962 minsize = 1;
5963 best_size = maxsize = nsyms * 2;
5964 if (gnu_hash)
5965 {
5966 if (minsize < 2)
5967 minsize = 2;
5968 if ((best_size & 31) == 0)
5969 ++best_size;
5970 }
5971
5972 /* Create array where we count the collisions in. We must use bfd_malloc
5973 since the size could be large. */
5974 amt = maxsize;
5975 amt *= sizeof (unsigned long int);
5976 counts = (unsigned long int *) bfd_malloc (amt);
5977 if (counts == NULL)
5978 return 0;
5979
5980 /* Compute the "optimal" size for the hash table. The criteria is a
5981 minimal chain length. The minor criteria is (of course) the size
5982 of the table. */
5983 for (i = minsize; i < maxsize; ++i)
5984 {
5985 /* Walk through the array of hashcodes and count the collisions. */
5986 BFD_HOST_U_64_BIT max;
5987 unsigned long int j;
5988 unsigned long int fact;
5989
5990 if (gnu_hash && (i & 31) == 0)
5991 continue;
5992
5993 memset (counts, '\0', i * sizeof (unsigned long int));
5994
5995 /* Determine how often each hash bucket is used. */
5996 for (j = 0; j < nsyms; ++j)
5997 ++counts[hashcodes[j] % i];
5998
5999 /* For the weight function we need some information about the
6000 pagesize on the target. This is information need not be 100%
6001 accurate. Since this information is not available (so far) we
6002 define it here to a reasonable default value. If it is crucial
6003 to have a better value some day simply define this value. */
6004 # ifndef BFD_TARGET_PAGESIZE
6005 # define BFD_TARGET_PAGESIZE (4096)
6006 # endif
6007
6008 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6009 and the chains. */
6010 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6011
6012 # if 1
6013 /* Variant 1: optimize for short chains. We add the squares
6014 of all the chain lengths (which favors many small chain
6015 over a few long chains). */
6016 for (j = 0; j < i; ++j)
6017 max += counts[j] * counts[j];
6018
6019 /* This adds penalties for the overall size of the table. */
6020 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6021 max *= fact * fact;
6022 # else
6023 /* Variant 2: Optimize a lot more for small table. Here we
6024 also add squares of the size but we also add penalties for
6025 empty slots (the +1 term). */
6026 for (j = 0; j < i; ++j)
6027 max += (1 + counts[j]) * (1 + counts[j]);
6028
6029 /* The overall size of the table is considered, but not as
6030 strong as in variant 1, where it is squared. */
6031 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6032 max *= fact;
6033 # endif
6034
6035 /* Compare with current best results. */
6036 if (max < best_chlen)
6037 {
6038 best_chlen = max;
6039 best_size = i;
6040 no_improvement_count = 0;
6041 }
6042 /* PR 11843: Avoid futile long searches for the best bucket size
6043 when there are a large number of symbols. */
6044 else if (++no_improvement_count == 100)
6045 break;
6046 }
6047
6048 free (counts);
6049 }
6050 else
6051 #endif /* defined (BFD_HOST_U_64_BIT) */
6052 {
6053 /* This is the fallback solution if no 64bit type is available or if we
6054 are not supposed to spend much time on optimizations. We select the
6055 bucket count using a fixed set of numbers. */
6056 for (i = 0; elf_buckets[i] != 0; i++)
6057 {
6058 best_size = elf_buckets[i];
6059 if (nsyms < elf_buckets[i + 1])
6060 break;
6061 }
6062 if (gnu_hash && best_size < 2)
6063 best_size = 2;
6064 }
6065
6066 return best_size;
6067 }
6068
6069 /* Size any SHT_GROUP section for ld -r. */
6070
6071 bfd_boolean
6072 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6073 {
6074 bfd *ibfd;
6075 asection *s;
6076
6077 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6078 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6079 && (s = ibfd->sections) != NULL
6080 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6081 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6082 return FALSE;
6083 return TRUE;
6084 }
6085
6086 /* Set a default stack segment size. The value in INFO wins. If it
6087 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6088 undefined it is initialized. */
6089
6090 bfd_boolean
6091 bfd_elf_stack_segment_size (bfd *output_bfd,
6092 struct bfd_link_info *info,
6093 const char *legacy_symbol,
6094 bfd_vma default_size)
6095 {
6096 struct elf_link_hash_entry *h = NULL;
6097
6098 /* Look for legacy symbol. */
6099 if (legacy_symbol)
6100 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6101 FALSE, FALSE, FALSE);
6102 if (h && (h->root.type == bfd_link_hash_defined
6103 || h->root.type == bfd_link_hash_defweak)
6104 && h->def_regular
6105 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6106 {
6107 /* The symbol has no type if specified on the command line. */
6108 h->type = STT_OBJECT;
6109 if (info->stacksize)
6110 /* xgettext:c-format */
6111 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6112 output_bfd, legacy_symbol);
6113 else if (h->root.u.def.section != bfd_abs_section_ptr)
6114 /* xgettext:c-format */
6115 _bfd_error_handler (_("%pB: %s not absolute"),
6116 output_bfd, legacy_symbol);
6117 else
6118 info->stacksize = h->root.u.def.value;
6119 }
6120
6121 if (!info->stacksize)
6122 /* If the user didn't set a size, or explicitly inhibit the
6123 size, set it now. */
6124 info->stacksize = default_size;
6125
6126 /* Provide the legacy symbol, if it is referenced. */
6127 if (h && (h->root.type == bfd_link_hash_undefined
6128 || h->root.type == bfd_link_hash_undefweak))
6129 {
6130 struct bfd_link_hash_entry *bh = NULL;
6131
6132 if (!(_bfd_generic_link_add_one_symbol
6133 (info, output_bfd, legacy_symbol,
6134 BSF_GLOBAL, bfd_abs_section_ptr,
6135 info->stacksize >= 0 ? info->stacksize : 0,
6136 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6137 return FALSE;
6138
6139 h = (struct elf_link_hash_entry *) bh;
6140 h->def_regular = 1;
6141 h->type = STT_OBJECT;
6142 }
6143
6144 return TRUE;
6145 }
6146
6147 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6148
6149 struct elf_gc_sweep_symbol_info
6150 {
6151 struct bfd_link_info *info;
6152 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6153 bfd_boolean);
6154 };
6155
6156 static bfd_boolean
6157 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6158 {
6159 if (!h->mark
6160 && (((h->root.type == bfd_link_hash_defined
6161 || h->root.type == bfd_link_hash_defweak)
6162 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6163 && h->root.u.def.section->gc_mark))
6164 || h->root.type == bfd_link_hash_undefined
6165 || h->root.type == bfd_link_hash_undefweak))
6166 {
6167 struct elf_gc_sweep_symbol_info *inf;
6168
6169 inf = (struct elf_gc_sweep_symbol_info *) data;
6170 (*inf->hide_symbol) (inf->info, h, TRUE);
6171 h->def_regular = 0;
6172 h->ref_regular = 0;
6173 h->ref_regular_nonweak = 0;
6174 }
6175
6176 return TRUE;
6177 }
6178
6179 /* Set up the sizes and contents of the ELF dynamic sections. This is
6180 called by the ELF linker emulation before_allocation routine. We
6181 must set the sizes of the sections before the linker sets the
6182 addresses of the various sections. */
6183
6184 bfd_boolean
6185 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6186 const char *soname,
6187 const char *rpath,
6188 const char *filter_shlib,
6189 const char *audit,
6190 const char *depaudit,
6191 const char * const *auxiliary_filters,
6192 struct bfd_link_info *info,
6193 asection **sinterpptr)
6194 {
6195 bfd *dynobj;
6196 const struct elf_backend_data *bed;
6197
6198 *sinterpptr = NULL;
6199
6200 if (!is_elf_hash_table (info->hash))
6201 return TRUE;
6202
6203 dynobj = elf_hash_table (info)->dynobj;
6204
6205 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6206 {
6207 struct bfd_elf_version_tree *verdefs;
6208 struct elf_info_failed asvinfo;
6209 struct bfd_elf_version_tree *t;
6210 struct bfd_elf_version_expr *d;
6211 asection *s;
6212 size_t soname_indx;
6213
6214 /* If we are supposed to export all symbols into the dynamic symbol
6215 table (this is not the normal case), then do so. */
6216 if (info->export_dynamic
6217 || (bfd_link_executable (info) && info->dynamic))
6218 {
6219 struct elf_info_failed eif;
6220
6221 eif.info = info;
6222 eif.failed = FALSE;
6223 elf_link_hash_traverse (elf_hash_table (info),
6224 _bfd_elf_export_symbol,
6225 &eif);
6226 if (eif.failed)
6227 return FALSE;
6228 }
6229
6230 if (soname != NULL)
6231 {
6232 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6233 soname, TRUE);
6234 if (soname_indx == (size_t) -1
6235 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6236 return FALSE;
6237 }
6238 else
6239 soname_indx = (size_t) -1;
6240
6241 /* Make all global versions with definition. */
6242 for (t = info->version_info; t != NULL; t = t->next)
6243 for (d = t->globals.list; d != NULL; d = d->next)
6244 if (!d->symver && d->literal)
6245 {
6246 const char *verstr, *name;
6247 size_t namelen, verlen, newlen;
6248 char *newname, *p, leading_char;
6249 struct elf_link_hash_entry *newh;
6250
6251 leading_char = bfd_get_symbol_leading_char (output_bfd);
6252 name = d->pattern;
6253 namelen = strlen (name) + (leading_char != '\0');
6254 verstr = t->name;
6255 verlen = strlen (verstr);
6256 newlen = namelen + verlen + 3;
6257
6258 newname = (char *) bfd_malloc (newlen);
6259 if (newname == NULL)
6260 return FALSE;
6261 newname[0] = leading_char;
6262 memcpy (newname + (leading_char != '\0'), name, namelen);
6263
6264 /* Check the hidden versioned definition. */
6265 p = newname + namelen;
6266 *p++ = ELF_VER_CHR;
6267 memcpy (p, verstr, verlen + 1);
6268 newh = elf_link_hash_lookup (elf_hash_table (info),
6269 newname, FALSE, FALSE,
6270 FALSE);
6271 if (newh == NULL
6272 || (newh->root.type != bfd_link_hash_defined
6273 && newh->root.type != bfd_link_hash_defweak))
6274 {
6275 /* Check the default versioned definition. */
6276 *p++ = ELF_VER_CHR;
6277 memcpy (p, verstr, verlen + 1);
6278 newh = elf_link_hash_lookup (elf_hash_table (info),
6279 newname, FALSE, FALSE,
6280 FALSE);
6281 }
6282 free (newname);
6283
6284 /* Mark this version if there is a definition and it is
6285 not defined in a shared object. */
6286 if (newh != NULL
6287 && !newh->def_dynamic
6288 && (newh->root.type == bfd_link_hash_defined
6289 || newh->root.type == bfd_link_hash_defweak))
6290 d->symver = 1;
6291 }
6292
6293 /* Attach all the symbols to their version information. */
6294 asvinfo.info = info;
6295 asvinfo.failed = FALSE;
6296
6297 elf_link_hash_traverse (elf_hash_table (info),
6298 _bfd_elf_link_assign_sym_version,
6299 &asvinfo);
6300 if (asvinfo.failed)
6301 return FALSE;
6302
6303 if (!info->allow_undefined_version)
6304 {
6305 /* Check if all global versions have a definition. */
6306 bfd_boolean all_defined = TRUE;
6307 for (t = info->version_info; t != NULL; t = t->next)
6308 for (d = t->globals.list; d != NULL; d = d->next)
6309 if (d->literal && !d->symver && !d->script)
6310 {
6311 _bfd_error_handler
6312 (_("%s: undefined version: %s"),
6313 d->pattern, t->name);
6314 all_defined = FALSE;
6315 }
6316
6317 if (!all_defined)
6318 {
6319 bfd_set_error (bfd_error_bad_value);
6320 return FALSE;
6321 }
6322 }
6323
6324 /* Set up the version definition section. */
6325 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6326 BFD_ASSERT (s != NULL);
6327
6328 /* We may have created additional version definitions if we are
6329 just linking a regular application. */
6330 verdefs = info->version_info;
6331
6332 /* Skip anonymous version tag. */
6333 if (verdefs != NULL && verdefs->vernum == 0)
6334 verdefs = verdefs->next;
6335
6336 if (verdefs == NULL && !info->create_default_symver)
6337 s->flags |= SEC_EXCLUDE;
6338 else
6339 {
6340 unsigned int cdefs;
6341 bfd_size_type size;
6342 bfd_byte *p;
6343 Elf_Internal_Verdef def;
6344 Elf_Internal_Verdaux defaux;
6345 struct bfd_link_hash_entry *bh;
6346 struct elf_link_hash_entry *h;
6347 const char *name;
6348
6349 cdefs = 0;
6350 size = 0;
6351
6352 /* Make space for the base version. */
6353 size += sizeof (Elf_External_Verdef);
6354 size += sizeof (Elf_External_Verdaux);
6355 ++cdefs;
6356
6357 /* Make space for the default version. */
6358 if (info->create_default_symver)
6359 {
6360 size += sizeof (Elf_External_Verdef);
6361 ++cdefs;
6362 }
6363
6364 for (t = verdefs; t != NULL; t = t->next)
6365 {
6366 struct bfd_elf_version_deps *n;
6367
6368 /* Don't emit base version twice. */
6369 if (t->vernum == 0)
6370 continue;
6371
6372 size += sizeof (Elf_External_Verdef);
6373 size += sizeof (Elf_External_Verdaux);
6374 ++cdefs;
6375
6376 for (n = t->deps; n != NULL; n = n->next)
6377 size += sizeof (Elf_External_Verdaux);
6378 }
6379
6380 s->size = size;
6381 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6382 if (s->contents == NULL && s->size != 0)
6383 return FALSE;
6384
6385 /* Fill in the version definition section. */
6386
6387 p = s->contents;
6388
6389 def.vd_version = VER_DEF_CURRENT;
6390 def.vd_flags = VER_FLG_BASE;
6391 def.vd_ndx = 1;
6392 def.vd_cnt = 1;
6393 if (info->create_default_symver)
6394 {
6395 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6396 def.vd_next = sizeof (Elf_External_Verdef);
6397 }
6398 else
6399 {
6400 def.vd_aux = sizeof (Elf_External_Verdef);
6401 def.vd_next = (sizeof (Elf_External_Verdef)
6402 + sizeof (Elf_External_Verdaux));
6403 }
6404
6405 if (soname_indx != (size_t) -1)
6406 {
6407 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6408 soname_indx);
6409 def.vd_hash = bfd_elf_hash (soname);
6410 defaux.vda_name = soname_indx;
6411 name = soname;
6412 }
6413 else
6414 {
6415 size_t indx;
6416
6417 name = lbasename (output_bfd->filename);
6418 def.vd_hash = bfd_elf_hash (name);
6419 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6420 name, FALSE);
6421 if (indx == (size_t) -1)
6422 return FALSE;
6423 defaux.vda_name = indx;
6424 }
6425 defaux.vda_next = 0;
6426
6427 _bfd_elf_swap_verdef_out (output_bfd, &def,
6428 (Elf_External_Verdef *) p);
6429 p += sizeof (Elf_External_Verdef);
6430 if (info->create_default_symver)
6431 {
6432 /* Add a symbol representing this version. */
6433 bh = NULL;
6434 if (! (_bfd_generic_link_add_one_symbol
6435 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6436 0, NULL, FALSE,
6437 get_elf_backend_data (dynobj)->collect, &bh)))
6438 return FALSE;
6439 h = (struct elf_link_hash_entry *) bh;
6440 h->non_elf = 0;
6441 h->def_regular = 1;
6442 h->type = STT_OBJECT;
6443 h->verinfo.vertree = NULL;
6444
6445 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6446 return FALSE;
6447
6448 /* Create a duplicate of the base version with the same
6449 aux block, but different flags. */
6450 def.vd_flags = 0;
6451 def.vd_ndx = 2;
6452 def.vd_aux = sizeof (Elf_External_Verdef);
6453 if (verdefs)
6454 def.vd_next = (sizeof (Elf_External_Verdef)
6455 + sizeof (Elf_External_Verdaux));
6456 else
6457 def.vd_next = 0;
6458 _bfd_elf_swap_verdef_out (output_bfd, &def,
6459 (Elf_External_Verdef *) p);
6460 p += sizeof (Elf_External_Verdef);
6461 }
6462 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6463 (Elf_External_Verdaux *) p);
6464 p += sizeof (Elf_External_Verdaux);
6465
6466 for (t = verdefs; t != NULL; t = t->next)
6467 {
6468 unsigned int cdeps;
6469 struct bfd_elf_version_deps *n;
6470
6471 /* Don't emit the base version twice. */
6472 if (t->vernum == 0)
6473 continue;
6474
6475 cdeps = 0;
6476 for (n = t->deps; n != NULL; n = n->next)
6477 ++cdeps;
6478
6479 /* Add a symbol representing this version. */
6480 bh = NULL;
6481 if (! (_bfd_generic_link_add_one_symbol
6482 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6483 0, NULL, FALSE,
6484 get_elf_backend_data (dynobj)->collect, &bh)))
6485 return FALSE;
6486 h = (struct elf_link_hash_entry *) bh;
6487 h->non_elf = 0;
6488 h->def_regular = 1;
6489 h->type = STT_OBJECT;
6490 h->verinfo.vertree = t;
6491
6492 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6493 return FALSE;
6494
6495 def.vd_version = VER_DEF_CURRENT;
6496 def.vd_flags = 0;
6497 if (t->globals.list == NULL
6498 && t->locals.list == NULL
6499 && ! t->used)
6500 def.vd_flags |= VER_FLG_WEAK;
6501 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6502 def.vd_cnt = cdeps + 1;
6503 def.vd_hash = bfd_elf_hash (t->name);
6504 def.vd_aux = sizeof (Elf_External_Verdef);
6505 def.vd_next = 0;
6506
6507 /* If a basever node is next, it *must* be the last node in
6508 the chain, otherwise Verdef construction breaks. */
6509 if (t->next != NULL && t->next->vernum == 0)
6510 BFD_ASSERT (t->next->next == NULL);
6511
6512 if (t->next != NULL && t->next->vernum != 0)
6513 def.vd_next = (sizeof (Elf_External_Verdef)
6514 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6515
6516 _bfd_elf_swap_verdef_out (output_bfd, &def,
6517 (Elf_External_Verdef *) p);
6518 p += sizeof (Elf_External_Verdef);
6519
6520 defaux.vda_name = h->dynstr_index;
6521 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6522 h->dynstr_index);
6523 defaux.vda_next = 0;
6524 if (t->deps != NULL)
6525 defaux.vda_next = sizeof (Elf_External_Verdaux);
6526 t->name_indx = defaux.vda_name;
6527
6528 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6529 (Elf_External_Verdaux *) p);
6530 p += sizeof (Elf_External_Verdaux);
6531
6532 for (n = t->deps; n != NULL; n = n->next)
6533 {
6534 if (n->version_needed == NULL)
6535 {
6536 /* This can happen if there was an error in the
6537 version script. */
6538 defaux.vda_name = 0;
6539 }
6540 else
6541 {
6542 defaux.vda_name = n->version_needed->name_indx;
6543 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6544 defaux.vda_name);
6545 }
6546 if (n->next == NULL)
6547 defaux.vda_next = 0;
6548 else
6549 defaux.vda_next = sizeof (Elf_External_Verdaux);
6550
6551 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6552 (Elf_External_Verdaux *) p);
6553 p += sizeof (Elf_External_Verdaux);
6554 }
6555 }
6556
6557 elf_tdata (output_bfd)->cverdefs = cdefs;
6558 }
6559 }
6560
6561 bed = get_elf_backend_data (output_bfd);
6562
6563 if (info->gc_sections && bed->can_gc_sections)
6564 {
6565 struct elf_gc_sweep_symbol_info sweep_info;
6566
6567 /* Remove the symbols that were in the swept sections from the
6568 dynamic symbol table. */
6569 sweep_info.info = info;
6570 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6571 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6572 &sweep_info);
6573 }
6574
6575 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6576 {
6577 asection *s;
6578 struct elf_find_verdep_info sinfo;
6579
6580 /* Work out the size of the version reference section. */
6581
6582 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6583 BFD_ASSERT (s != NULL);
6584
6585 sinfo.info = info;
6586 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6587 if (sinfo.vers == 0)
6588 sinfo.vers = 1;
6589 sinfo.failed = FALSE;
6590
6591 elf_link_hash_traverse (elf_hash_table (info),
6592 _bfd_elf_link_find_version_dependencies,
6593 &sinfo);
6594 if (sinfo.failed)
6595 return FALSE;
6596
6597 if (elf_tdata (output_bfd)->verref == NULL)
6598 s->flags |= SEC_EXCLUDE;
6599 else
6600 {
6601 Elf_Internal_Verneed *vn;
6602 unsigned int size;
6603 unsigned int crefs;
6604 bfd_byte *p;
6605
6606 /* Build the version dependency section. */
6607 size = 0;
6608 crefs = 0;
6609 for (vn = elf_tdata (output_bfd)->verref;
6610 vn != NULL;
6611 vn = vn->vn_nextref)
6612 {
6613 Elf_Internal_Vernaux *a;
6614
6615 size += sizeof (Elf_External_Verneed);
6616 ++crefs;
6617 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6618 size += sizeof (Elf_External_Vernaux);
6619 }
6620
6621 s->size = size;
6622 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6623 if (s->contents == NULL)
6624 return FALSE;
6625
6626 p = s->contents;
6627 for (vn = elf_tdata (output_bfd)->verref;
6628 vn != NULL;
6629 vn = vn->vn_nextref)
6630 {
6631 unsigned int caux;
6632 Elf_Internal_Vernaux *a;
6633 size_t indx;
6634
6635 caux = 0;
6636 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6637 ++caux;
6638
6639 vn->vn_version = VER_NEED_CURRENT;
6640 vn->vn_cnt = caux;
6641 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6642 elf_dt_name (vn->vn_bfd) != NULL
6643 ? elf_dt_name (vn->vn_bfd)
6644 : lbasename (vn->vn_bfd->filename),
6645 FALSE);
6646 if (indx == (size_t) -1)
6647 return FALSE;
6648 vn->vn_file = indx;
6649 vn->vn_aux = sizeof (Elf_External_Verneed);
6650 if (vn->vn_nextref == NULL)
6651 vn->vn_next = 0;
6652 else
6653 vn->vn_next = (sizeof (Elf_External_Verneed)
6654 + caux * sizeof (Elf_External_Vernaux));
6655
6656 _bfd_elf_swap_verneed_out (output_bfd, vn,
6657 (Elf_External_Verneed *) p);
6658 p += sizeof (Elf_External_Verneed);
6659
6660 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6661 {
6662 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6663 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6664 a->vna_nodename, FALSE);
6665 if (indx == (size_t) -1)
6666 return FALSE;
6667 a->vna_name = indx;
6668 if (a->vna_nextptr == NULL)
6669 a->vna_next = 0;
6670 else
6671 a->vna_next = sizeof (Elf_External_Vernaux);
6672
6673 _bfd_elf_swap_vernaux_out (output_bfd, a,
6674 (Elf_External_Vernaux *) p);
6675 p += sizeof (Elf_External_Vernaux);
6676 }
6677 }
6678
6679 elf_tdata (output_bfd)->cverrefs = crefs;
6680 }
6681 }
6682
6683 /* Any syms created from now on start with -1 in
6684 got.refcount/offset and plt.refcount/offset. */
6685 elf_hash_table (info)->init_got_refcount
6686 = elf_hash_table (info)->init_got_offset;
6687 elf_hash_table (info)->init_plt_refcount
6688 = elf_hash_table (info)->init_plt_offset;
6689
6690 if (bfd_link_relocatable (info)
6691 && !_bfd_elf_size_group_sections (info))
6692 return FALSE;
6693
6694 /* The backend may have to create some sections regardless of whether
6695 we're dynamic or not. */
6696 if (bed->elf_backend_always_size_sections
6697 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6698 return FALSE;
6699
6700 /* Determine any GNU_STACK segment requirements, after the backend
6701 has had a chance to set a default segment size. */
6702 if (info->execstack)
6703 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6704 else if (info->noexecstack)
6705 elf_stack_flags (output_bfd) = PF_R | PF_W;
6706 else
6707 {
6708 bfd *inputobj;
6709 asection *notesec = NULL;
6710 int exec = 0;
6711
6712 for (inputobj = info->input_bfds;
6713 inputobj;
6714 inputobj = inputobj->link.next)
6715 {
6716 asection *s;
6717
6718 if (inputobj->flags
6719 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6720 continue;
6721 s = inputobj->sections;
6722 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6723 continue;
6724
6725 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6726 if (s)
6727 {
6728 if (s->flags & SEC_CODE)
6729 exec = PF_X;
6730 notesec = s;
6731 }
6732 else if (bed->default_execstack)
6733 exec = PF_X;
6734 }
6735 if (notesec || info->stacksize > 0)
6736 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6737 if (notesec && exec && bfd_link_relocatable (info)
6738 && notesec->output_section != bfd_abs_section_ptr)
6739 notesec->output_section->flags |= SEC_CODE;
6740 }
6741
6742 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6743 {
6744 struct elf_info_failed eif;
6745 struct elf_link_hash_entry *h;
6746 asection *dynstr;
6747 asection *s;
6748
6749 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6750 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6751
6752 if (info->symbolic)
6753 {
6754 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6755 return FALSE;
6756 info->flags |= DF_SYMBOLIC;
6757 }
6758
6759 if (rpath != NULL)
6760 {
6761 size_t indx;
6762 bfd_vma tag;
6763
6764 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6765 TRUE);
6766 if (indx == (size_t) -1)
6767 return FALSE;
6768
6769 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6770 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6771 return FALSE;
6772 }
6773
6774 if (filter_shlib != NULL)
6775 {
6776 size_t indx;
6777
6778 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6779 filter_shlib, TRUE);
6780 if (indx == (size_t) -1
6781 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6782 return FALSE;
6783 }
6784
6785 if (auxiliary_filters != NULL)
6786 {
6787 const char * const *p;
6788
6789 for (p = auxiliary_filters; *p != NULL; p++)
6790 {
6791 size_t indx;
6792
6793 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6794 *p, TRUE);
6795 if (indx == (size_t) -1
6796 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6797 return FALSE;
6798 }
6799 }
6800
6801 if (audit != NULL)
6802 {
6803 size_t indx;
6804
6805 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6806 TRUE);
6807 if (indx == (size_t) -1
6808 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6809 return FALSE;
6810 }
6811
6812 if (depaudit != NULL)
6813 {
6814 size_t indx;
6815
6816 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6817 TRUE);
6818 if (indx == (size_t) -1
6819 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6820 return FALSE;
6821 }
6822
6823 eif.info = info;
6824 eif.failed = FALSE;
6825
6826 /* Find all symbols which were defined in a dynamic object and make
6827 the backend pick a reasonable value for them. */
6828 elf_link_hash_traverse (elf_hash_table (info),
6829 _bfd_elf_adjust_dynamic_symbol,
6830 &eif);
6831 if (eif.failed)
6832 return FALSE;
6833
6834 /* Add some entries to the .dynamic section. We fill in some of the
6835 values later, in bfd_elf_final_link, but we must add the entries
6836 now so that we know the final size of the .dynamic section. */
6837
6838 /* If there are initialization and/or finalization functions to
6839 call then add the corresponding DT_INIT/DT_FINI entries. */
6840 h = (info->init_function
6841 ? elf_link_hash_lookup (elf_hash_table (info),
6842 info->init_function, FALSE,
6843 FALSE, FALSE)
6844 : NULL);
6845 if (h != NULL
6846 && (h->ref_regular
6847 || h->def_regular))
6848 {
6849 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6850 return FALSE;
6851 }
6852 h = (info->fini_function
6853 ? elf_link_hash_lookup (elf_hash_table (info),
6854 info->fini_function, FALSE,
6855 FALSE, FALSE)
6856 : NULL);
6857 if (h != NULL
6858 && (h->ref_regular
6859 || h->def_regular))
6860 {
6861 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6862 return FALSE;
6863 }
6864
6865 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6866 if (s != NULL && s->linker_has_input)
6867 {
6868 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6869 if (! bfd_link_executable (info))
6870 {
6871 bfd *sub;
6872 asection *o;
6873
6874 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6875 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
6876 && (o = sub->sections) != NULL
6877 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
6878 for (o = sub->sections; o != NULL; o = o->next)
6879 if (elf_section_data (o)->this_hdr.sh_type
6880 == SHT_PREINIT_ARRAY)
6881 {
6882 _bfd_error_handler
6883 (_("%pB: .preinit_array section is not allowed in DSO"),
6884 sub);
6885 break;
6886 }
6887
6888 bfd_set_error (bfd_error_nonrepresentable_section);
6889 return FALSE;
6890 }
6891
6892 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6893 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6894 return FALSE;
6895 }
6896 s = bfd_get_section_by_name (output_bfd, ".init_array");
6897 if (s != NULL && s->linker_has_input)
6898 {
6899 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6900 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6901 return FALSE;
6902 }
6903 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6904 if (s != NULL && s->linker_has_input)
6905 {
6906 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6907 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6908 return FALSE;
6909 }
6910
6911 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6912 /* If .dynstr is excluded from the link, we don't want any of
6913 these tags. Strictly, we should be checking each section
6914 individually; This quick check covers for the case where
6915 someone does a /DISCARD/ : { *(*) }. */
6916 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6917 {
6918 bfd_size_type strsize;
6919
6920 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6921 if ((info->emit_hash
6922 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6923 || (info->emit_gnu_hash
6924 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6925 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6926 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6927 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6928 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6929 bed->s->sizeof_sym))
6930 return FALSE;
6931 }
6932 }
6933
6934 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6935 return FALSE;
6936
6937 /* The backend must work out the sizes of all the other dynamic
6938 sections. */
6939 if (dynobj != NULL
6940 && bed->elf_backend_size_dynamic_sections != NULL
6941 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6942 return FALSE;
6943
6944 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6945 {
6946 if (elf_tdata (output_bfd)->cverdefs)
6947 {
6948 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
6949
6950 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6951 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
6952 return FALSE;
6953 }
6954
6955 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6956 {
6957 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6958 return FALSE;
6959 }
6960 else if (info->flags & DF_BIND_NOW)
6961 {
6962 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6963 return FALSE;
6964 }
6965
6966 if (info->flags_1)
6967 {
6968 if (bfd_link_executable (info))
6969 info->flags_1 &= ~ (DF_1_INITFIRST
6970 | DF_1_NODELETE
6971 | DF_1_NOOPEN);
6972 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6973 return FALSE;
6974 }
6975
6976 if (elf_tdata (output_bfd)->cverrefs)
6977 {
6978 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
6979
6980 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6981 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6982 return FALSE;
6983 }
6984
6985 if ((elf_tdata (output_bfd)->cverrefs == 0
6986 && elf_tdata (output_bfd)->cverdefs == 0)
6987 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
6988 {
6989 asection *s;
6990
6991 s = bfd_get_linker_section (dynobj, ".gnu.version");
6992 s->flags |= SEC_EXCLUDE;
6993 }
6994 }
6995 return TRUE;
6996 }
6997
6998 /* Find the first non-excluded output section. We'll use its
6999 section symbol for some emitted relocs. */
7000 void
7001 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7002 {
7003 asection *s;
7004
7005 for (s = output_bfd->sections; s != NULL; s = s->next)
7006 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7007 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7008 {
7009 elf_hash_table (info)->text_index_section = s;
7010 break;
7011 }
7012 }
7013
7014 /* Find two non-excluded output sections, one for code, one for data.
7015 We'll use their section symbols for some emitted relocs. */
7016 void
7017 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7018 {
7019 asection *s;
7020
7021 /* Data first, since setting text_index_section changes
7022 _bfd_elf_omit_section_dynsym_default. */
7023 for (s = output_bfd->sections; s != NULL; s = s->next)
7024 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
7025 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7026 {
7027 elf_hash_table (info)->data_index_section = s;
7028 break;
7029 }
7030
7031 for (s = output_bfd->sections; s != NULL; s = s->next)
7032 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
7033 == (SEC_ALLOC | SEC_READONLY))
7034 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7035 {
7036 elf_hash_table (info)->text_index_section = s;
7037 break;
7038 }
7039
7040 if (elf_hash_table (info)->text_index_section == NULL)
7041 elf_hash_table (info)->text_index_section
7042 = elf_hash_table (info)->data_index_section;
7043 }
7044
7045 bfd_boolean
7046 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7047 {
7048 const struct elf_backend_data *bed;
7049 unsigned long section_sym_count;
7050 bfd_size_type dynsymcount = 0;
7051
7052 if (!is_elf_hash_table (info->hash))
7053 return TRUE;
7054
7055 bed = get_elf_backend_data (output_bfd);
7056 (*bed->elf_backend_init_index_section) (output_bfd, info);
7057
7058 /* Assign dynsym indices. In a shared library we generate a section
7059 symbol for each output section, which come first. Next come all
7060 of the back-end allocated local dynamic syms, followed by the rest
7061 of the global symbols.
7062
7063 This is usually not needed for static binaries, however backends
7064 can request to always do it, e.g. the MIPS backend uses dynamic
7065 symbol counts to lay out GOT, which will be produced in the
7066 presence of GOT relocations even in static binaries (holding fixed
7067 data in that case, to satisfy those relocations). */
7068
7069 if (elf_hash_table (info)->dynamic_sections_created
7070 || bed->always_renumber_dynsyms)
7071 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7072 &section_sym_count);
7073
7074 if (elf_hash_table (info)->dynamic_sections_created)
7075 {
7076 bfd *dynobj;
7077 asection *s;
7078 unsigned int dtagcount;
7079
7080 dynobj = elf_hash_table (info)->dynobj;
7081
7082 /* Work out the size of the symbol version section. */
7083 s = bfd_get_linker_section (dynobj, ".gnu.version");
7084 BFD_ASSERT (s != NULL);
7085 if ((s->flags & SEC_EXCLUDE) == 0)
7086 {
7087 s->size = dynsymcount * sizeof (Elf_External_Versym);
7088 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7089 if (s->contents == NULL)
7090 return FALSE;
7091
7092 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7093 return FALSE;
7094 }
7095
7096 /* Set the size of the .dynsym and .hash sections. We counted
7097 the number of dynamic symbols in elf_link_add_object_symbols.
7098 We will build the contents of .dynsym and .hash when we build
7099 the final symbol table, because until then we do not know the
7100 correct value to give the symbols. We built the .dynstr
7101 section as we went along in elf_link_add_object_symbols. */
7102 s = elf_hash_table (info)->dynsym;
7103 BFD_ASSERT (s != NULL);
7104 s->size = dynsymcount * bed->s->sizeof_sym;
7105
7106 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7107 if (s->contents == NULL)
7108 return FALSE;
7109
7110 /* The first entry in .dynsym is a dummy symbol. Clear all the
7111 section syms, in case we don't output them all. */
7112 ++section_sym_count;
7113 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7114
7115 elf_hash_table (info)->bucketcount = 0;
7116
7117 /* Compute the size of the hashing table. As a side effect this
7118 computes the hash values for all the names we export. */
7119 if (info->emit_hash)
7120 {
7121 unsigned long int *hashcodes;
7122 struct hash_codes_info hashinf;
7123 bfd_size_type amt;
7124 unsigned long int nsyms;
7125 size_t bucketcount;
7126 size_t hash_entry_size;
7127
7128 /* Compute the hash values for all exported symbols. At the same
7129 time store the values in an array so that we could use them for
7130 optimizations. */
7131 amt = dynsymcount * sizeof (unsigned long int);
7132 hashcodes = (unsigned long int *) bfd_malloc (amt);
7133 if (hashcodes == NULL)
7134 return FALSE;
7135 hashinf.hashcodes = hashcodes;
7136 hashinf.error = FALSE;
7137
7138 /* Put all hash values in HASHCODES. */
7139 elf_link_hash_traverse (elf_hash_table (info),
7140 elf_collect_hash_codes, &hashinf);
7141 if (hashinf.error)
7142 {
7143 free (hashcodes);
7144 return FALSE;
7145 }
7146
7147 nsyms = hashinf.hashcodes - hashcodes;
7148 bucketcount
7149 = compute_bucket_count (info, hashcodes, nsyms, 0);
7150 free (hashcodes);
7151
7152 if (bucketcount == 0 && nsyms > 0)
7153 return FALSE;
7154
7155 elf_hash_table (info)->bucketcount = bucketcount;
7156
7157 s = bfd_get_linker_section (dynobj, ".hash");
7158 BFD_ASSERT (s != NULL);
7159 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7160 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7161 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7162 if (s->contents == NULL)
7163 return FALSE;
7164
7165 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7166 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7167 s->contents + hash_entry_size);
7168 }
7169
7170 if (info->emit_gnu_hash)
7171 {
7172 size_t i, cnt;
7173 unsigned char *contents;
7174 struct collect_gnu_hash_codes cinfo;
7175 bfd_size_type amt;
7176 size_t bucketcount;
7177
7178 memset (&cinfo, 0, sizeof (cinfo));
7179
7180 /* Compute the hash values for all exported symbols. At the same
7181 time store the values in an array so that we could use them for
7182 optimizations. */
7183 amt = dynsymcount * 2 * sizeof (unsigned long int);
7184 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7185 if (cinfo.hashcodes == NULL)
7186 return FALSE;
7187
7188 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7189 cinfo.min_dynindx = -1;
7190 cinfo.output_bfd = output_bfd;
7191 cinfo.bed = bed;
7192
7193 /* Put all hash values in HASHCODES. */
7194 elf_link_hash_traverse (elf_hash_table (info),
7195 elf_collect_gnu_hash_codes, &cinfo);
7196 if (cinfo.error)
7197 {
7198 free (cinfo.hashcodes);
7199 return FALSE;
7200 }
7201
7202 bucketcount
7203 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7204
7205 if (bucketcount == 0)
7206 {
7207 free (cinfo.hashcodes);
7208 return FALSE;
7209 }
7210
7211 s = bfd_get_linker_section (dynobj, ".gnu.hash");
7212 BFD_ASSERT (s != NULL);
7213
7214 if (cinfo.nsyms == 0)
7215 {
7216 /* Empty .gnu.hash section is special. */
7217 BFD_ASSERT (cinfo.min_dynindx == -1);
7218 free (cinfo.hashcodes);
7219 s->size = 5 * 4 + bed->s->arch_size / 8;
7220 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7221 if (contents == NULL)
7222 return FALSE;
7223 s->contents = contents;
7224 /* 1 empty bucket. */
7225 bfd_put_32 (output_bfd, 1, contents);
7226 /* SYMIDX above the special symbol 0. */
7227 bfd_put_32 (output_bfd, 1, contents + 4);
7228 /* Just one word for bitmask. */
7229 bfd_put_32 (output_bfd, 1, contents + 8);
7230 /* Only hash fn bloom filter. */
7231 bfd_put_32 (output_bfd, 0, contents + 12);
7232 /* No hashes are valid - empty bitmask. */
7233 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7234 /* No hashes in the only bucket. */
7235 bfd_put_32 (output_bfd, 0,
7236 contents + 16 + bed->s->arch_size / 8);
7237 }
7238 else
7239 {
7240 unsigned long int maskwords, maskbitslog2, x;
7241 BFD_ASSERT (cinfo.min_dynindx != -1);
7242
7243 x = cinfo.nsyms;
7244 maskbitslog2 = 1;
7245 while ((x >>= 1) != 0)
7246 ++maskbitslog2;
7247 if (maskbitslog2 < 3)
7248 maskbitslog2 = 5;
7249 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7250 maskbitslog2 = maskbitslog2 + 3;
7251 else
7252 maskbitslog2 = maskbitslog2 + 2;
7253 if (bed->s->arch_size == 64)
7254 {
7255 if (maskbitslog2 == 5)
7256 maskbitslog2 = 6;
7257 cinfo.shift1 = 6;
7258 }
7259 else
7260 cinfo.shift1 = 5;
7261 cinfo.mask = (1 << cinfo.shift1) - 1;
7262 cinfo.shift2 = maskbitslog2;
7263 cinfo.maskbits = 1 << maskbitslog2;
7264 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7265 amt = bucketcount * sizeof (unsigned long int) * 2;
7266 amt += maskwords * sizeof (bfd_vma);
7267 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7268 if (cinfo.bitmask == NULL)
7269 {
7270 free (cinfo.hashcodes);
7271 return FALSE;
7272 }
7273
7274 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7275 cinfo.indx = cinfo.counts + bucketcount;
7276 cinfo.symindx = dynsymcount - cinfo.nsyms;
7277 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7278
7279 /* Determine how often each hash bucket is used. */
7280 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7281 for (i = 0; i < cinfo.nsyms; ++i)
7282 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7283
7284 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7285 if (cinfo.counts[i] != 0)
7286 {
7287 cinfo.indx[i] = cnt;
7288 cnt += cinfo.counts[i];
7289 }
7290 BFD_ASSERT (cnt == dynsymcount);
7291 cinfo.bucketcount = bucketcount;
7292 cinfo.local_indx = cinfo.min_dynindx;
7293
7294 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7295 s->size += cinfo.maskbits / 8;
7296 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7297 if (contents == NULL)
7298 {
7299 free (cinfo.bitmask);
7300 free (cinfo.hashcodes);
7301 return FALSE;
7302 }
7303
7304 s->contents = contents;
7305 bfd_put_32 (output_bfd, bucketcount, contents);
7306 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7307 bfd_put_32 (output_bfd, maskwords, contents + 8);
7308 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7309 contents += 16 + cinfo.maskbits / 8;
7310
7311 for (i = 0; i < bucketcount; ++i)
7312 {
7313 if (cinfo.counts[i] == 0)
7314 bfd_put_32 (output_bfd, 0, contents);
7315 else
7316 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7317 contents += 4;
7318 }
7319
7320 cinfo.contents = contents;
7321
7322 /* Renumber dynamic symbols, populate .gnu.hash section. */
7323 elf_link_hash_traverse (elf_hash_table (info),
7324 elf_renumber_gnu_hash_syms, &cinfo);
7325
7326 contents = s->contents + 16;
7327 for (i = 0; i < maskwords; ++i)
7328 {
7329 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7330 contents);
7331 contents += bed->s->arch_size / 8;
7332 }
7333
7334 free (cinfo.bitmask);
7335 free (cinfo.hashcodes);
7336 }
7337 }
7338
7339 s = bfd_get_linker_section (dynobj, ".dynstr");
7340 BFD_ASSERT (s != NULL);
7341
7342 elf_finalize_dynstr (output_bfd, info);
7343
7344 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7345
7346 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7347 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7348 return FALSE;
7349 }
7350
7351 return TRUE;
7352 }
7353 \f
7354 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7355
7356 static void
7357 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7358 asection *sec)
7359 {
7360 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7361 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7362 }
7363
7364 /* Finish SHF_MERGE section merging. */
7365
7366 bfd_boolean
7367 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7368 {
7369 bfd *ibfd;
7370 asection *sec;
7371
7372 if (!is_elf_hash_table (info->hash))
7373 return FALSE;
7374
7375 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7376 if ((ibfd->flags & DYNAMIC) == 0
7377 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7378 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7379 == get_elf_backend_data (obfd)->s->elfclass))
7380 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7381 if ((sec->flags & SEC_MERGE) != 0
7382 && !bfd_is_abs_section (sec->output_section))
7383 {
7384 struct bfd_elf_section_data *secdata;
7385
7386 secdata = elf_section_data (sec);
7387 if (! _bfd_add_merge_section (obfd,
7388 &elf_hash_table (info)->merge_info,
7389 sec, &secdata->sec_info))
7390 return FALSE;
7391 else if (secdata->sec_info)
7392 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7393 }
7394
7395 if (elf_hash_table (info)->merge_info != NULL)
7396 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7397 merge_sections_remove_hook);
7398 return TRUE;
7399 }
7400
7401 /* Create an entry in an ELF linker hash table. */
7402
7403 struct bfd_hash_entry *
7404 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7405 struct bfd_hash_table *table,
7406 const char *string)
7407 {
7408 /* Allocate the structure if it has not already been allocated by a
7409 subclass. */
7410 if (entry == NULL)
7411 {
7412 entry = (struct bfd_hash_entry *)
7413 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7414 if (entry == NULL)
7415 return entry;
7416 }
7417
7418 /* Call the allocation method of the superclass. */
7419 entry = _bfd_link_hash_newfunc (entry, table, string);
7420 if (entry != NULL)
7421 {
7422 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7423 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7424
7425 /* Set local fields. */
7426 ret->indx = -1;
7427 ret->dynindx = -1;
7428 ret->got = htab->init_got_refcount;
7429 ret->plt = htab->init_plt_refcount;
7430 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7431 - offsetof (struct elf_link_hash_entry, size)));
7432 /* Assume that we have been called by a non-ELF symbol reader.
7433 This flag is then reset by the code which reads an ELF input
7434 file. This ensures that a symbol created by a non-ELF symbol
7435 reader will have the flag set correctly. */
7436 ret->non_elf = 1;
7437 }
7438
7439 return entry;
7440 }
7441
7442 /* Copy data from an indirect symbol to its direct symbol, hiding the
7443 old indirect symbol. Also used for copying flags to a weakdef. */
7444
7445 void
7446 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7447 struct elf_link_hash_entry *dir,
7448 struct elf_link_hash_entry *ind)
7449 {
7450 struct elf_link_hash_table *htab;
7451
7452 /* Copy down any references that we may have already seen to the
7453 symbol which just became indirect. */
7454
7455 if (dir->versioned != versioned_hidden)
7456 dir->ref_dynamic |= ind->ref_dynamic;
7457 dir->ref_regular |= ind->ref_regular;
7458 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7459 dir->non_got_ref |= ind->non_got_ref;
7460 dir->needs_plt |= ind->needs_plt;
7461 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7462
7463 if (ind->root.type != bfd_link_hash_indirect)
7464 return;
7465
7466 /* Copy over the global and procedure linkage table refcount entries.
7467 These may have been already set up by a check_relocs routine. */
7468 htab = elf_hash_table (info);
7469 if (ind->got.refcount > htab->init_got_refcount.refcount)
7470 {
7471 if (dir->got.refcount < 0)
7472 dir->got.refcount = 0;
7473 dir->got.refcount += ind->got.refcount;
7474 ind->got.refcount = htab->init_got_refcount.refcount;
7475 }
7476
7477 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7478 {
7479 if (dir->plt.refcount < 0)
7480 dir->plt.refcount = 0;
7481 dir->plt.refcount += ind->plt.refcount;
7482 ind->plt.refcount = htab->init_plt_refcount.refcount;
7483 }
7484
7485 if (ind->dynindx != -1)
7486 {
7487 if (dir->dynindx != -1)
7488 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7489 dir->dynindx = ind->dynindx;
7490 dir->dynstr_index = ind->dynstr_index;
7491 ind->dynindx = -1;
7492 ind->dynstr_index = 0;
7493 }
7494 }
7495
7496 void
7497 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7498 struct elf_link_hash_entry *h,
7499 bfd_boolean force_local)
7500 {
7501 /* STT_GNU_IFUNC symbol must go through PLT. */
7502 if (h->type != STT_GNU_IFUNC)
7503 {
7504 h->plt = elf_hash_table (info)->init_plt_offset;
7505 h->needs_plt = 0;
7506 }
7507 if (force_local)
7508 {
7509 h->forced_local = 1;
7510 if (h->dynindx != -1)
7511 {
7512 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7513 h->dynstr_index);
7514 h->dynindx = -1;
7515 h->dynstr_index = 0;
7516 }
7517 }
7518 }
7519
7520 /* Hide a symbol. */
7521
7522 void
7523 _bfd_elf_link_hide_symbol (bfd *output_bfd,
7524 struct bfd_link_info *info,
7525 struct bfd_link_hash_entry *h)
7526 {
7527 if (is_elf_hash_table (info->hash))
7528 {
7529 const struct elf_backend_data *bed
7530 = get_elf_backend_data (output_bfd);
7531 struct elf_link_hash_entry *eh
7532 = (struct elf_link_hash_entry *) h;
7533 bed->elf_backend_hide_symbol (info, eh, TRUE);
7534 eh->def_dynamic = 0;
7535 eh->ref_dynamic = 0;
7536 eh->dynamic_def = 0;
7537 }
7538 }
7539
7540 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7541 caller. */
7542
7543 bfd_boolean
7544 _bfd_elf_link_hash_table_init
7545 (struct elf_link_hash_table *table,
7546 bfd *abfd,
7547 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7548 struct bfd_hash_table *,
7549 const char *),
7550 unsigned int entsize,
7551 enum elf_target_id target_id)
7552 {
7553 bfd_boolean ret;
7554 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7555
7556 table->init_got_refcount.refcount = can_refcount - 1;
7557 table->init_plt_refcount.refcount = can_refcount - 1;
7558 table->init_got_offset.offset = -(bfd_vma) 1;
7559 table->init_plt_offset.offset = -(bfd_vma) 1;
7560 /* The first dynamic symbol is a dummy. */
7561 table->dynsymcount = 1;
7562
7563 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7564
7565 table->root.type = bfd_link_elf_hash_table;
7566 table->hash_table_id = target_id;
7567
7568 return ret;
7569 }
7570
7571 /* Create an ELF linker hash table. */
7572
7573 struct bfd_link_hash_table *
7574 _bfd_elf_link_hash_table_create (bfd *abfd)
7575 {
7576 struct elf_link_hash_table *ret;
7577 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7578
7579 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7580 if (ret == NULL)
7581 return NULL;
7582
7583 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7584 sizeof (struct elf_link_hash_entry),
7585 GENERIC_ELF_DATA))
7586 {
7587 free (ret);
7588 return NULL;
7589 }
7590 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7591
7592 return &ret->root;
7593 }
7594
7595 /* Destroy an ELF linker hash table. */
7596
7597 void
7598 _bfd_elf_link_hash_table_free (bfd *obfd)
7599 {
7600 struct elf_link_hash_table *htab;
7601
7602 htab = (struct elf_link_hash_table *) obfd->link.hash;
7603 if (htab->dynstr != NULL)
7604 _bfd_elf_strtab_free (htab->dynstr);
7605 _bfd_merge_sections_free (htab->merge_info);
7606 _bfd_generic_link_hash_table_free (obfd);
7607 }
7608
7609 /* This is a hook for the ELF emulation code in the generic linker to
7610 tell the backend linker what file name to use for the DT_NEEDED
7611 entry for a dynamic object. */
7612
7613 void
7614 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7615 {
7616 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7617 && bfd_get_format (abfd) == bfd_object)
7618 elf_dt_name (abfd) = name;
7619 }
7620
7621 int
7622 bfd_elf_get_dyn_lib_class (bfd *abfd)
7623 {
7624 int lib_class;
7625 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7626 && bfd_get_format (abfd) == bfd_object)
7627 lib_class = elf_dyn_lib_class (abfd);
7628 else
7629 lib_class = 0;
7630 return lib_class;
7631 }
7632
7633 void
7634 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7635 {
7636 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7637 && bfd_get_format (abfd) == bfd_object)
7638 elf_dyn_lib_class (abfd) = lib_class;
7639 }
7640
7641 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7642 the linker ELF emulation code. */
7643
7644 struct bfd_link_needed_list *
7645 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7646 struct bfd_link_info *info)
7647 {
7648 if (! is_elf_hash_table (info->hash))
7649 return NULL;
7650 return elf_hash_table (info)->needed;
7651 }
7652
7653 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7654 hook for the linker ELF emulation code. */
7655
7656 struct bfd_link_needed_list *
7657 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7658 struct bfd_link_info *info)
7659 {
7660 if (! is_elf_hash_table (info->hash))
7661 return NULL;
7662 return elf_hash_table (info)->runpath;
7663 }
7664
7665 /* Get the name actually used for a dynamic object for a link. This
7666 is the SONAME entry if there is one. Otherwise, it is the string
7667 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7668
7669 const char *
7670 bfd_elf_get_dt_soname (bfd *abfd)
7671 {
7672 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7673 && bfd_get_format (abfd) == bfd_object)
7674 return elf_dt_name (abfd);
7675 return NULL;
7676 }
7677
7678 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7679 the ELF linker emulation code. */
7680
7681 bfd_boolean
7682 bfd_elf_get_bfd_needed_list (bfd *abfd,
7683 struct bfd_link_needed_list **pneeded)
7684 {
7685 asection *s;
7686 bfd_byte *dynbuf = NULL;
7687 unsigned int elfsec;
7688 unsigned long shlink;
7689 bfd_byte *extdyn, *extdynend;
7690 size_t extdynsize;
7691 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7692
7693 *pneeded = NULL;
7694
7695 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7696 || bfd_get_format (abfd) != bfd_object)
7697 return TRUE;
7698
7699 s = bfd_get_section_by_name (abfd, ".dynamic");
7700 if (s == NULL || s->size == 0)
7701 return TRUE;
7702
7703 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7704 goto error_return;
7705
7706 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7707 if (elfsec == SHN_BAD)
7708 goto error_return;
7709
7710 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7711
7712 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7713 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7714
7715 extdyn = dynbuf;
7716 extdynend = extdyn + s->size;
7717 for (; extdyn < extdynend; extdyn += extdynsize)
7718 {
7719 Elf_Internal_Dyn dyn;
7720
7721 (*swap_dyn_in) (abfd, extdyn, &dyn);
7722
7723 if (dyn.d_tag == DT_NULL)
7724 break;
7725
7726 if (dyn.d_tag == DT_NEEDED)
7727 {
7728 const char *string;
7729 struct bfd_link_needed_list *l;
7730 unsigned int tagv = dyn.d_un.d_val;
7731 bfd_size_type amt;
7732
7733 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7734 if (string == NULL)
7735 goto error_return;
7736
7737 amt = sizeof *l;
7738 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7739 if (l == NULL)
7740 goto error_return;
7741
7742 l->by = abfd;
7743 l->name = string;
7744 l->next = *pneeded;
7745 *pneeded = l;
7746 }
7747 }
7748
7749 free (dynbuf);
7750
7751 return TRUE;
7752
7753 error_return:
7754 if (dynbuf != NULL)
7755 free (dynbuf);
7756 return FALSE;
7757 }
7758
7759 struct elf_symbuf_symbol
7760 {
7761 unsigned long st_name; /* Symbol name, index in string tbl */
7762 unsigned char st_info; /* Type and binding attributes */
7763 unsigned char st_other; /* Visibilty, and target specific */
7764 };
7765
7766 struct elf_symbuf_head
7767 {
7768 struct elf_symbuf_symbol *ssym;
7769 size_t count;
7770 unsigned int st_shndx;
7771 };
7772
7773 struct elf_symbol
7774 {
7775 union
7776 {
7777 Elf_Internal_Sym *isym;
7778 struct elf_symbuf_symbol *ssym;
7779 } u;
7780 const char *name;
7781 };
7782
7783 /* Sort references to symbols by ascending section number. */
7784
7785 static int
7786 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7787 {
7788 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7789 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7790
7791 return s1->st_shndx - s2->st_shndx;
7792 }
7793
7794 static int
7795 elf_sym_name_compare (const void *arg1, const void *arg2)
7796 {
7797 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7798 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7799 return strcmp (s1->name, s2->name);
7800 }
7801
7802 static struct elf_symbuf_head *
7803 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
7804 {
7805 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7806 struct elf_symbuf_symbol *ssym;
7807 struct elf_symbuf_head *ssymbuf, *ssymhead;
7808 size_t i, shndx_count, total_size;
7809
7810 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7811 if (indbuf == NULL)
7812 return NULL;
7813
7814 for (ind = indbuf, i = 0; i < symcount; i++)
7815 if (isymbuf[i].st_shndx != SHN_UNDEF)
7816 *ind++ = &isymbuf[i];
7817 indbufend = ind;
7818
7819 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7820 elf_sort_elf_symbol);
7821
7822 shndx_count = 0;
7823 if (indbufend > indbuf)
7824 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7825 if (ind[0]->st_shndx != ind[1]->st_shndx)
7826 shndx_count++;
7827
7828 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7829 + (indbufend - indbuf) * sizeof (*ssym));
7830 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7831 if (ssymbuf == NULL)
7832 {
7833 free (indbuf);
7834 return NULL;
7835 }
7836
7837 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7838 ssymbuf->ssym = NULL;
7839 ssymbuf->count = shndx_count;
7840 ssymbuf->st_shndx = 0;
7841 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7842 {
7843 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7844 {
7845 ssymhead++;
7846 ssymhead->ssym = ssym;
7847 ssymhead->count = 0;
7848 ssymhead->st_shndx = (*ind)->st_shndx;
7849 }
7850 ssym->st_name = (*ind)->st_name;
7851 ssym->st_info = (*ind)->st_info;
7852 ssym->st_other = (*ind)->st_other;
7853 ssymhead->count++;
7854 }
7855 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
7856 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7857 == total_size));
7858
7859 free (indbuf);
7860 return ssymbuf;
7861 }
7862
7863 /* Check if 2 sections define the same set of local and global
7864 symbols. */
7865
7866 static bfd_boolean
7867 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7868 struct bfd_link_info *info)
7869 {
7870 bfd *bfd1, *bfd2;
7871 const struct elf_backend_data *bed1, *bed2;
7872 Elf_Internal_Shdr *hdr1, *hdr2;
7873 size_t symcount1, symcount2;
7874 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7875 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7876 Elf_Internal_Sym *isym, *isymend;
7877 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7878 size_t count1, count2, i;
7879 unsigned int shndx1, shndx2;
7880 bfd_boolean result;
7881
7882 bfd1 = sec1->owner;
7883 bfd2 = sec2->owner;
7884
7885 /* Both sections have to be in ELF. */
7886 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7887 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7888 return FALSE;
7889
7890 if (elf_section_type (sec1) != elf_section_type (sec2))
7891 return FALSE;
7892
7893 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7894 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7895 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7896 return FALSE;
7897
7898 bed1 = get_elf_backend_data (bfd1);
7899 bed2 = get_elf_backend_data (bfd2);
7900 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7901 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7902 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7903 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7904
7905 if (symcount1 == 0 || symcount2 == 0)
7906 return FALSE;
7907
7908 result = FALSE;
7909 isymbuf1 = NULL;
7910 isymbuf2 = NULL;
7911 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7912 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7913
7914 if (ssymbuf1 == NULL)
7915 {
7916 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7917 NULL, NULL, NULL);
7918 if (isymbuf1 == NULL)
7919 goto done;
7920
7921 if (!info->reduce_memory_overheads)
7922 elf_tdata (bfd1)->symbuf = ssymbuf1
7923 = elf_create_symbuf (symcount1, isymbuf1);
7924 }
7925
7926 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7927 {
7928 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7929 NULL, NULL, NULL);
7930 if (isymbuf2 == NULL)
7931 goto done;
7932
7933 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7934 elf_tdata (bfd2)->symbuf = ssymbuf2
7935 = elf_create_symbuf (symcount2, isymbuf2);
7936 }
7937
7938 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7939 {
7940 /* Optimized faster version. */
7941 size_t lo, hi, mid;
7942 struct elf_symbol *symp;
7943 struct elf_symbuf_symbol *ssym, *ssymend;
7944
7945 lo = 0;
7946 hi = ssymbuf1->count;
7947 ssymbuf1++;
7948 count1 = 0;
7949 while (lo < hi)
7950 {
7951 mid = (lo + hi) / 2;
7952 if (shndx1 < ssymbuf1[mid].st_shndx)
7953 hi = mid;
7954 else if (shndx1 > ssymbuf1[mid].st_shndx)
7955 lo = mid + 1;
7956 else
7957 {
7958 count1 = ssymbuf1[mid].count;
7959 ssymbuf1 += mid;
7960 break;
7961 }
7962 }
7963
7964 lo = 0;
7965 hi = ssymbuf2->count;
7966 ssymbuf2++;
7967 count2 = 0;
7968 while (lo < hi)
7969 {
7970 mid = (lo + hi) / 2;
7971 if (shndx2 < ssymbuf2[mid].st_shndx)
7972 hi = mid;
7973 else if (shndx2 > ssymbuf2[mid].st_shndx)
7974 lo = mid + 1;
7975 else
7976 {
7977 count2 = ssymbuf2[mid].count;
7978 ssymbuf2 += mid;
7979 break;
7980 }
7981 }
7982
7983 if (count1 == 0 || count2 == 0 || count1 != count2)
7984 goto done;
7985
7986 symtable1
7987 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7988 symtable2
7989 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
7990 if (symtable1 == NULL || symtable2 == NULL)
7991 goto done;
7992
7993 symp = symtable1;
7994 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7995 ssym < ssymend; ssym++, symp++)
7996 {
7997 symp->u.ssym = ssym;
7998 symp->name = bfd_elf_string_from_elf_section (bfd1,
7999 hdr1->sh_link,
8000 ssym->st_name);
8001 }
8002
8003 symp = symtable2;
8004 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8005 ssym < ssymend; ssym++, symp++)
8006 {
8007 symp->u.ssym = ssym;
8008 symp->name = bfd_elf_string_from_elf_section (bfd2,
8009 hdr2->sh_link,
8010 ssym->st_name);
8011 }
8012
8013 /* Sort symbol by name. */
8014 qsort (symtable1, count1, sizeof (struct elf_symbol),
8015 elf_sym_name_compare);
8016 qsort (symtable2, count1, sizeof (struct elf_symbol),
8017 elf_sym_name_compare);
8018
8019 for (i = 0; i < count1; i++)
8020 /* Two symbols must have the same binding, type and name. */
8021 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8022 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8023 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8024 goto done;
8025
8026 result = TRUE;
8027 goto done;
8028 }
8029
8030 symtable1 = (struct elf_symbol *)
8031 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8032 symtable2 = (struct elf_symbol *)
8033 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8034 if (symtable1 == NULL || symtable2 == NULL)
8035 goto done;
8036
8037 /* Count definitions in the section. */
8038 count1 = 0;
8039 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8040 if (isym->st_shndx == shndx1)
8041 symtable1[count1++].u.isym = isym;
8042
8043 count2 = 0;
8044 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8045 if (isym->st_shndx == shndx2)
8046 symtable2[count2++].u.isym = isym;
8047
8048 if (count1 == 0 || count2 == 0 || count1 != count2)
8049 goto done;
8050
8051 for (i = 0; i < count1; i++)
8052 symtable1[i].name
8053 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8054 symtable1[i].u.isym->st_name);
8055
8056 for (i = 0; i < count2; i++)
8057 symtable2[i].name
8058 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8059 symtable2[i].u.isym->st_name);
8060
8061 /* Sort symbol by name. */
8062 qsort (symtable1, count1, sizeof (struct elf_symbol),
8063 elf_sym_name_compare);
8064 qsort (symtable2, count1, sizeof (struct elf_symbol),
8065 elf_sym_name_compare);
8066
8067 for (i = 0; i < count1; i++)
8068 /* Two symbols must have the same binding, type and name. */
8069 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8070 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8071 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8072 goto done;
8073
8074 result = TRUE;
8075
8076 done:
8077 if (symtable1)
8078 free (symtable1);
8079 if (symtable2)
8080 free (symtable2);
8081 if (isymbuf1)
8082 free (isymbuf1);
8083 if (isymbuf2)
8084 free (isymbuf2);
8085
8086 return result;
8087 }
8088
8089 /* Return TRUE if 2 section types are compatible. */
8090
8091 bfd_boolean
8092 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8093 bfd *bbfd, const asection *bsec)
8094 {
8095 if (asec == NULL
8096 || bsec == NULL
8097 || abfd->xvec->flavour != bfd_target_elf_flavour
8098 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8099 return TRUE;
8100
8101 return elf_section_type (asec) == elf_section_type (bsec);
8102 }
8103 \f
8104 /* Final phase of ELF linker. */
8105
8106 /* A structure we use to avoid passing large numbers of arguments. */
8107
8108 struct elf_final_link_info
8109 {
8110 /* General link information. */
8111 struct bfd_link_info *info;
8112 /* Output BFD. */
8113 bfd *output_bfd;
8114 /* Symbol string table. */
8115 struct elf_strtab_hash *symstrtab;
8116 /* .hash section. */
8117 asection *hash_sec;
8118 /* symbol version section (.gnu.version). */
8119 asection *symver_sec;
8120 /* Buffer large enough to hold contents of any section. */
8121 bfd_byte *contents;
8122 /* Buffer large enough to hold external relocs of any section. */
8123 void *external_relocs;
8124 /* Buffer large enough to hold internal relocs of any section. */
8125 Elf_Internal_Rela *internal_relocs;
8126 /* Buffer large enough to hold external local symbols of any input
8127 BFD. */
8128 bfd_byte *external_syms;
8129 /* And a buffer for symbol section indices. */
8130 Elf_External_Sym_Shndx *locsym_shndx;
8131 /* Buffer large enough to hold internal local symbols of any input
8132 BFD. */
8133 Elf_Internal_Sym *internal_syms;
8134 /* Array large enough to hold a symbol index for each local symbol
8135 of any input BFD. */
8136 long *indices;
8137 /* Array large enough to hold a section pointer for each local
8138 symbol of any input BFD. */
8139 asection **sections;
8140 /* Buffer for SHT_SYMTAB_SHNDX section. */
8141 Elf_External_Sym_Shndx *symshndxbuf;
8142 /* Number of STT_FILE syms seen. */
8143 size_t filesym_count;
8144 };
8145
8146 /* This struct is used to pass information to elf_link_output_extsym. */
8147
8148 struct elf_outext_info
8149 {
8150 bfd_boolean failed;
8151 bfd_boolean localsyms;
8152 bfd_boolean file_sym_done;
8153 struct elf_final_link_info *flinfo;
8154 };
8155
8156
8157 /* Support for evaluating a complex relocation.
8158
8159 Complex relocations are generalized, self-describing relocations. The
8160 implementation of them consists of two parts: complex symbols, and the
8161 relocations themselves.
8162
8163 The relocations are use a reserved elf-wide relocation type code (R_RELC
8164 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8165 information (start bit, end bit, word width, etc) into the addend. This
8166 information is extracted from CGEN-generated operand tables within gas.
8167
8168 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8169 internal) representing prefix-notation expressions, including but not
8170 limited to those sorts of expressions normally encoded as addends in the
8171 addend field. The symbol mangling format is:
8172
8173 <node> := <literal>
8174 | <unary-operator> ':' <node>
8175 | <binary-operator> ':' <node> ':' <node>
8176 ;
8177
8178 <literal> := 's' <digits=N> ':' <N character symbol name>
8179 | 'S' <digits=N> ':' <N character section name>
8180 | '#' <hexdigits>
8181 ;
8182
8183 <binary-operator> := as in C
8184 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8185
8186 static void
8187 set_symbol_value (bfd *bfd_with_globals,
8188 Elf_Internal_Sym *isymbuf,
8189 size_t locsymcount,
8190 size_t symidx,
8191 bfd_vma val)
8192 {
8193 struct elf_link_hash_entry **sym_hashes;
8194 struct elf_link_hash_entry *h;
8195 size_t extsymoff = locsymcount;
8196
8197 if (symidx < locsymcount)
8198 {
8199 Elf_Internal_Sym *sym;
8200
8201 sym = isymbuf + symidx;
8202 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8203 {
8204 /* It is a local symbol: move it to the
8205 "absolute" section and give it a value. */
8206 sym->st_shndx = SHN_ABS;
8207 sym->st_value = val;
8208 return;
8209 }
8210 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8211 extsymoff = 0;
8212 }
8213
8214 /* It is a global symbol: set its link type
8215 to "defined" and give it a value. */
8216
8217 sym_hashes = elf_sym_hashes (bfd_with_globals);
8218 h = sym_hashes [symidx - extsymoff];
8219 while (h->root.type == bfd_link_hash_indirect
8220 || h->root.type == bfd_link_hash_warning)
8221 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8222 h->root.type = bfd_link_hash_defined;
8223 h->root.u.def.value = val;
8224 h->root.u.def.section = bfd_abs_section_ptr;
8225 }
8226
8227 static bfd_boolean
8228 resolve_symbol (const char *name,
8229 bfd *input_bfd,
8230 struct elf_final_link_info *flinfo,
8231 bfd_vma *result,
8232 Elf_Internal_Sym *isymbuf,
8233 size_t locsymcount)
8234 {
8235 Elf_Internal_Sym *sym;
8236 struct bfd_link_hash_entry *global_entry;
8237 const char *candidate = NULL;
8238 Elf_Internal_Shdr *symtab_hdr;
8239 size_t i;
8240
8241 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8242
8243 for (i = 0; i < locsymcount; ++ i)
8244 {
8245 sym = isymbuf + i;
8246
8247 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8248 continue;
8249
8250 candidate = bfd_elf_string_from_elf_section (input_bfd,
8251 symtab_hdr->sh_link,
8252 sym->st_name);
8253 #ifdef DEBUG
8254 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8255 name, candidate, (unsigned long) sym->st_value);
8256 #endif
8257 if (candidate && strcmp (candidate, name) == 0)
8258 {
8259 asection *sec = flinfo->sections [i];
8260
8261 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8262 *result += sec->output_offset + sec->output_section->vma;
8263 #ifdef DEBUG
8264 printf ("Found symbol with value %8.8lx\n",
8265 (unsigned long) *result);
8266 #endif
8267 return TRUE;
8268 }
8269 }
8270
8271 /* Hmm, haven't found it yet. perhaps it is a global. */
8272 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8273 FALSE, FALSE, TRUE);
8274 if (!global_entry)
8275 return FALSE;
8276
8277 if (global_entry->type == bfd_link_hash_defined
8278 || global_entry->type == bfd_link_hash_defweak)
8279 {
8280 *result = (global_entry->u.def.value
8281 + global_entry->u.def.section->output_section->vma
8282 + global_entry->u.def.section->output_offset);
8283 #ifdef DEBUG
8284 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8285 global_entry->root.string, (unsigned long) *result);
8286 #endif
8287 return TRUE;
8288 }
8289
8290 return FALSE;
8291 }
8292
8293 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8294 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8295 names like "foo.end" which is the end address of section "foo". */
8296
8297 static bfd_boolean
8298 resolve_section (const char *name,
8299 asection *sections,
8300 bfd_vma *result,
8301 bfd * abfd)
8302 {
8303 asection *curr;
8304 unsigned int len;
8305
8306 for (curr = sections; curr; curr = curr->next)
8307 if (strcmp (curr->name, name) == 0)
8308 {
8309 *result = curr->vma;
8310 return TRUE;
8311 }
8312
8313 /* Hmm. still haven't found it. try pseudo-section names. */
8314 /* FIXME: This could be coded more efficiently... */
8315 for (curr = sections; curr; curr = curr->next)
8316 {
8317 len = strlen (curr->name);
8318 if (len > strlen (name))
8319 continue;
8320
8321 if (strncmp (curr->name, name, len) == 0)
8322 {
8323 if (strncmp (".end", name + len, 4) == 0)
8324 {
8325 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
8326 return TRUE;
8327 }
8328
8329 /* Insert more pseudo-section names here, if you like. */
8330 }
8331 }
8332
8333 return FALSE;
8334 }
8335
8336 static void
8337 undefined_reference (const char *reftype, const char *name)
8338 {
8339 /* xgettext:c-format */
8340 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8341 reftype, name);
8342 }
8343
8344 static bfd_boolean
8345 eval_symbol (bfd_vma *result,
8346 const char **symp,
8347 bfd *input_bfd,
8348 struct elf_final_link_info *flinfo,
8349 bfd_vma dot,
8350 Elf_Internal_Sym *isymbuf,
8351 size_t locsymcount,
8352 int signed_p)
8353 {
8354 size_t len;
8355 size_t symlen;
8356 bfd_vma a;
8357 bfd_vma b;
8358 char symbuf[4096];
8359 const char *sym = *symp;
8360 const char *symend;
8361 bfd_boolean symbol_is_section = FALSE;
8362
8363 len = strlen (sym);
8364 symend = sym + len;
8365
8366 if (len < 1 || len > sizeof (symbuf))
8367 {
8368 bfd_set_error (bfd_error_invalid_operation);
8369 return FALSE;
8370 }
8371
8372 switch (* sym)
8373 {
8374 case '.':
8375 *result = dot;
8376 *symp = sym + 1;
8377 return TRUE;
8378
8379 case '#':
8380 ++sym;
8381 *result = strtoul (sym, (char **) symp, 16);
8382 return TRUE;
8383
8384 case 'S':
8385 symbol_is_section = TRUE;
8386 /* Fall through. */
8387 case 's':
8388 ++sym;
8389 symlen = strtol (sym, (char **) symp, 10);
8390 sym = *symp + 1; /* Skip the trailing ':'. */
8391
8392 if (symend < sym || symlen + 1 > sizeof (symbuf))
8393 {
8394 bfd_set_error (bfd_error_invalid_operation);
8395 return FALSE;
8396 }
8397
8398 memcpy (symbuf, sym, symlen);
8399 symbuf[symlen] = '\0';
8400 *symp = sym + symlen;
8401
8402 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8403 the symbol as a section, or vice-versa. so we're pretty liberal in our
8404 interpretation here; section means "try section first", not "must be a
8405 section", and likewise with symbol. */
8406
8407 if (symbol_is_section)
8408 {
8409 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8410 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8411 isymbuf, locsymcount))
8412 {
8413 undefined_reference ("section", symbuf);
8414 return FALSE;
8415 }
8416 }
8417 else
8418 {
8419 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8420 isymbuf, locsymcount)
8421 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8422 result, input_bfd))
8423 {
8424 undefined_reference ("symbol", symbuf);
8425 return FALSE;
8426 }
8427 }
8428
8429 return TRUE;
8430
8431 /* All that remains are operators. */
8432
8433 #define UNARY_OP(op) \
8434 if (strncmp (sym, #op, strlen (#op)) == 0) \
8435 { \
8436 sym += strlen (#op); \
8437 if (*sym == ':') \
8438 ++sym; \
8439 *symp = sym; \
8440 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8441 isymbuf, locsymcount, signed_p)) \
8442 return FALSE; \
8443 if (signed_p) \
8444 *result = op ((bfd_signed_vma) a); \
8445 else \
8446 *result = op a; \
8447 return TRUE; \
8448 }
8449
8450 #define BINARY_OP(op) \
8451 if (strncmp (sym, #op, strlen (#op)) == 0) \
8452 { \
8453 sym += strlen (#op); \
8454 if (*sym == ':') \
8455 ++sym; \
8456 *symp = sym; \
8457 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8458 isymbuf, locsymcount, signed_p)) \
8459 return FALSE; \
8460 ++*symp; \
8461 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8462 isymbuf, locsymcount, signed_p)) \
8463 return FALSE; \
8464 if (signed_p) \
8465 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8466 else \
8467 *result = a op b; \
8468 return TRUE; \
8469 }
8470
8471 default:
8472 UNARY_OP (0-);
8473 BINARY_OP (<<);
8474 BINARY_OP (>>);
8475 BINARY_OP (==);
8476 BINARY_OP (!=);
8477 BINARY_OP (<=);
8478 BINARY_OP (>=);
8479 BINARY_OP (&&);
8480 BINARY_OP (||);
8481 UNARY_OP (~);
8482 UNARY_OP (!);
8483 BINARY_OP (*);
8484 BINARY_OP (/);
8485 BINARY_OP (%);
8486 BINARY_OP (^);
8487 BINARY_OP (|);
8488 BINARY_OP (&);
8489 BINARY_OP (+);
8490 BINARY_OP (-);
8491 BINARY_OP (<);
8492 BINARY_OP (>);
8493 #undef UNARY_OP
8494 #undef BINARY_OP
8495 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8496 bfd_set_error (bfd_error_invalid_operation);
8497 return FALSE;
8498 }
8499 }
8500
8501 static void
8502 put_value (bfd_vma size,
8503 unsigned long chunksz,
8504 bfd *input_bfd,
8505 bfd_vma x,
8506 bfd_byte *location)
8507 {
8508 location += (size - chunksz);
8509
8510 for (; size; size -= chunksz, location -= chunksz)
8511 {
8512 switch (chunksz)
8513 {
8514 case 1:
8515 bfd_put_8 (input_bfd, x, location);
8516 x >>= 8;
8517 break;
8518 case 2:
8519 bfd_put_16 (input_bfd, x, location);
8520 x >>= 16;
8521 break;
8522 case 4:
8523 bfd_put_32 (input_bfd, x, location);
8524 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8525 x >>= 16;
8526 x >>= 16;
8527 break;
8528 #ifdef BFD64
8529 case 8:
8530 bfd_put_64 (input_bfd, x, location);
8531 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8532 x >>= 32;
8533 x >>= 32;
8534 break;
8535 #endif
8536 default:
8537 abort ();
8538 break;
8539 }
8540 }
8541 }
8542
8543 static bfd_vma
8544 get_value (bfd_vma size,
8545 unsigned long chunksz,
8546 bfd *input_bfd,
8547 bfd_byte *location)
8548 {
8549 int shift;
8550 bfd_vma x = 0;
8551
8552 /* Sanity checks. */
8553 BFD_ASSERT (chunksz <= sizeof (x)
8554 && size >= chunksz
8555 && chunksz != 0
8556 && (size % chunksz) == 0
8557 && input_bfd != NULL
8558 && location != NULL);
8559
8560 if (chunksz == sizeof (x))
8561 {
8562 BFD_ASSERT (size == chunksz);
8563
8564 /* Make sure that we do not perform an undefined shift operation.
8565 We know that size == chunksz so there will only be one iteration
8566 of the loop below. */
8567 shift = 0;
8568 }
8569 else
8570 shift = 8 * chunksz;
8571
8572 for (; size; size -= chunksz, location += chunksz)
8573 {
8574 switch (chunksz)
8575 {
8576 case 1:
8577 x = (x << shift) | bfd_get_8 (input_bfd, location);
8578 break;
8579 case 2:
8580 x = (x << shift) | bfd_get_16 (input_bfd, location);
8581 break;
8582 case 4:
8583 x = (x << shift) | bfd_get_32 (input_bfd, location);
8584 break;
8585 #ifdef BFD64
8586 case 8:
8587 x = (x << shift) | bfd_get_64 (input_bfd, location);
8588 break;
8589 #endif
8590 default:
8591 abort ();
8592 }
8593 }
8594 return x;
8595 }
8596
8597 static void
8598 decode_complex_addend (unsigned long *start, /* in bits */
8599 unsigned long *oplen, /* in bits */
8600 unsigned long *len, /* in bits */
8601 unsigned long *wordsz, /* in bytes */
8602 unsigned long *chunksz, /* in bytes */
8603 unsigned long *lsb0_p,
8604 unsigned long *signed_p,
8605 unsigned long *trunc_p,
8606 unsigned long encoded)
8607 {
8608 * start = encoded & 0x3F;
8609 * len = (encoded >> 6) & 0x3F;
8610 * oplen = (encoded >> 12) & 0x3F;
8611 * wordsz = (encoded >> 18) & 0xF;
8612 * chunksz = (encoded >> 22) & 0xF;
8613 * lsb0_p = (encoded >> 27) & 1;
8614 * signed_p = (encoded >> 28) & 1;
8615 * trunc_p = (encoded >> 29) & 1;
8616 }
8617
8618 bfd_reloc_status_type
8619 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8620 asection *input_section ATTRIBUTE_UNUSED,
8621 bfd_byte *contents,
8622 Elf_Internal_Rela *rel,
8623 bfd_vma relocation)
8624 {
8625 bfd_vma shift, x, mask;
8626 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8627 bfd_reloc_status_type r;
8628
8629 /* Perform this reloc, since it is complex.
8630 (this is not to say that it necessarily refers to a complex
8631 symbol; merely that it is a self-describing CGEN based reloc.
8632 i.e. the addend has the complete reloc information (bit start, end,
8633 word size, etc) encoded within it.). */
8634
8635 decode_complex_addend (&start, &oplen, &len, &wordsz,
8636 &chunksz, &lsb0_p, &signed_p,
8637 &trunc_p, rel->r_addend);
8638
8639 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8640
8641 if (lsb0_p)
8642 shift = (start + 1) - len;
8643 else
8644 shift = (8 * wordsz) - (start + len);
8645
8646 x = get_value (wordsz, chunksz, input_bfd,
8647 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8648
8649 #ifdef DEBUG
8650 printf ("Doing complex reloc: "
8651 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8652 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8653 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8654 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8655 oplen, (unsigned long) x, (unsigned long) mask,
8656 (unsigned long) relocation);
8657 #endif
8658
8659 r = bfd_reloc_ok;
8660 if (! trunc_p)
8661 /* Now do an overflow check. */
8662 r = bfd_check_overflow ((signed_p
8663 ? complain_overflow_signed
8664 : complain_overflow_unsigned),
8665 len, 0, (8 * wordsz),
8666 relocation);
8667
8668 /* Do the deed. */
8669 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8670
8671 #ifdef DEBUG
8672 printf (" relocation: %8.8lx\n"
8673 " shifted mask: %8.8lx\n"
8674 " shifted/masked reloc: %8.8lx\n"
8675 " result: %8.8lx\n",
8676 (unsigned long) relocation, (unsigned long) (mask << shift),
8677 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8678 #endif
8679 put_value (wordsz, chunksz, input_bfd, x,
8680 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8681 return r;
8682 }
8683
8684 /* Functions to read r_offset from external (target order) reloc
8685 entry. Faster than bfd_getl32 et al, because we let the compiler
8686 know the value is aligned. */
8687
8688 static bfd_vma
8689 ext32l_r_offset (const void *p)
8690 {
8691 union aligned32
8692 {
8693 uint32_t v;
8694 unsigned char c[4];
8695 };
8696 const union aligned32 *a
8697 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8698
8699 uint32_t aval = ( (uint32_t) a->c[0]
8700 | (uint32_t) a->c[1] << 8
8701 | (uint32_t) a->c[2] << 16
8702 | (uint32_t) a->c[3] << 24);
8703 return aval;
8704 }
8705
8706 static bfd_vma
8707 ext32b_r_offset (const void *p)
8708 {
8709 union aligned32
8710 {
8711 uint32_t v;
8712 unsigned char c[4];
8713 };
8714 const union aligned32 *a
8715 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8716
8717 uint32_t aval = ( (uint32_t) a->c[0] << 24
8718 | (uint32_t) a->c[1] << 16
8719 | (uint32_t) a->c[2] << 8
8720 | (uint32_t) a->c[3]);
8721 return aval;
8722 }
8723
8724 #ifdef BFD_HOST_64_BIT
8725 static bfd_vma
8726 ext64l_r_offset (const void *p)
8727 {
8728 union aligned64
8729 {
8730 uint64_t v;
8731 unsigned char c[8];
8732 };
8733 const union aligned64 *a
8734 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8735
8736 uint64_t aval = ( (uint64_t) a->c[0]
8737 | (uint64_t) a->c[1] << 8
8738 | (uint64_t) a->c[2] << 16
8739 | (uint64_t) a->c[3] << 24
8740 | (uint64_t) a->c[4] << 32
8741 | (uint64_t) a->c[5] << 40
8742 | (uint64_t) a->c[6] << 48
8743 | (uint64_t) a->c[7] << 56);
8744 return aval;
8745 }
8746
8747 static bfd_vma
8748 ext64b_r_offset (const void *p)
8749 {
8750 union aligned64
8751 {
8752 uint64_t v;
8753 unsigned char c[8];
8754 };
8755 const union aligned64 *a
8756 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8757
8758 uint64_t aval = ( (uint64_t) a->c[0] << 56
8759 | (uint64_t) a->c[1] << 48
8760 | (uint64_t) a->c[2] << 40
8761 | (uint64_t) a->c[3] << 32
8762 | (uint64_t) a->c[4] << 24
8763 | (uint64_t) a->c[5] << 16
8764 | (uint64_t) a->c[6] << 8
8765 | (uint64_t) a->c[7]);
8766 return aval;
8767 }
8768 #endif
8769
8770 /* When performing a relocatable link, the input relocations are
8771 preserved. But, if they reference global symbols, the indices
8772 referenced must be updated. Update all the relocations found in
8773 RELDATA. */
8774
8775 static bfd_boolean
8776 elf_link_adjust_relocs (bfd *abfd,
8777 asection *sec,
8778 struct bfd_elf_section_reloc_data *reldata,
8779 bfd_boolean sort,
8780 struct bfd_link_info *info)
8781 {
8782 unsigned int i;
8783 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8784 bfd_byte *erela;
8785 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8786 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8787 bfd_vma r_type_mask;
8788 int r_sym_shift;
8789 unsigned int count = reldata->count;
8790 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8791
8792 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8793 {
8794 swap_in = bed->s->swap_reloc_in;
8795 swap_out = bed->s->swap_reloc_out;
8796 }
8797 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8798 {
8799 swap_in = bed->s->swap_reloca_in;
8800 swap_out = bed->s->swap_reloca_out;
8801 }
8802 else
8803 abort ();
8804
8805 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8806 abort ();
8807
8808 if (bed->s->arch_size == 32)
8809 {
8810 r_type_mask = 0xff;
8811 r_sym_shift = 8;
8812 }
8813 else
8814 {
8815 r_type_mask = 0xffffffff;
8816 r_sym_shift = 32;
8817 }
8818
8819 erela = reldata->hdr->contents;
8820 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8821 {
8822 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8823 unsigned int j;
8824
8825 if (*rel_hash == NULL)
8826 continue;
8827
8828 if ((*rel_hash)->indx == -2
8829 && info->gc_sections
8830 && ! info->gc_keep_exported)
8831 {
8832 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
8833 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
8834 abfd, sec,
8835 (*rel_hash)->root.root.string);
8836 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
8837 abfd, sec);
8838 bfd_set_error (bfd_error_invalid_operation);
8839 return FALSE;
8840 }
8841 BFD_ASSERT ((*rel_hash)->indx >= 0);
8842
8843 (*swap_in) (abfd, erela, irela);
8844 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8845 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8846 | (irela[j].r_info & r_type_mask));
8847 (*swap_out) (abfd, irela, erela);
8848 }
8849
8850 if (bed->elf_backend_update_relocs)
8851 (*bed->elf_backend_update_relocs) (sec, reldata);
8852
8853 if (sort && count != 0)
8854 {
8855 bfd_vma (*ext_r_off) (const void *);
8856 bfd_vma r_off;
8857 size_t elt_size;
8858 bfd_byte *base, *end, *p, *loc;
8859 bfd_byte *buf = NULL;
8860
8861 if (bed->s->arch_size == 32)
8862 {
8863 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8864 ext_r_off = ext32l_r_offset;
8865 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8866 ext_r_off = ext32b_r_offset;
8867 else
8868 abort ();
8869 }
8870 else
8871 {
8872 #ifdef BFD_HOST_64_BIT
8873 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8874 ext_r_off = ext64l_r_offset;
8875 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8876 ext_r_off = ext64b_r_offset;
8877 else
8878 #endif
8879 abort ();
8880 }
8881
8882 /* Must use a stable sort here. A modified insertion sort,
8883 since the relocs are mostly sorted already. */
8884 elt_size = reldata->hdr->sh_entsize;
8885 base = reldata->hdr->contents;
8886 end = base + count * elt_size;
8887 if (elt_size > sizeof (Elf64_External_Rela))
8888 abort ();
8889
8890 /* Ensure the first element is lowest. This acts as a sentinel,
8891 speeding the main loop below. */
8892 r_off = (*ext_r_off) (base);
8893 for (p = loc = base; (p += elt_size) < end; )
8894 {
8895 bfd_vma r_off2 = (*ext_r_off) (p);
8896 if (r_off > r_off2)
8897 {
8898 r_off = r_off2;
8899 loc = p;
8900 }
8901 }
8902 if (loc != base)
8903 {
8904 /* Don't just swap *base and *loc as that changes the order
8905 of the original base[0] and base[1] if they happen to
8906 have the same r_offset. */
8907 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8908 memcpy (onebuf, loc, elt_size);
8909 memmove (base + elt_size, base, loc - base);
8910 memcpy (base, onebuf, elt_size);
8911 }
8912
8913 for (p = base + elt_size; (p += elt_size) < end; )
8914 {
8915 /* base to p is sorted, *p is next to insert. */
8916 r_off = (*ext_r_off) (p);
8917 /* Search the sorted region for location to insert. */
8918 loc = p - elt_size;
8919 while (r_off < (*ext_r_off) (loc))
8920 loc -= elt_size;
8921 loc += elt_size;
8922 if (loc != p)
8923 {
8924 /* Chances are there is a run of relocs to insert here,
8925 from one of more input files. Files are not always
8926 linked in order due to the way elf_link_input_bfd is
8927 called. See pr17666. */
8928 size_t sortlen = p - loc;
8929 bfd_vma r_off2 = (*ext_r_off) (loc);
8930 size_t runlen = elt_size;
8931 size_t buf_size = 96 * 1024;
8932 while (p + runlen < end
8933 && (sortlen <= buf_size
8934 || runlen + elt_size <= buf_size)
8935 && r_off2 > (*ext_r_off) (p + runlen))
8936 runlen += elt_size;
8937 if (buf == NULL)
8938 {
8939 buf = bfd_malloc (buf_size);
8940 if (buf == NULL)
8941 return FALSE;
8942 }
8943 if (runlen < sortlen)
8944 {
8945 memcpy (buf, p, runlen);
8946 memmove (loc + runlen, loc, sortlen);
8947 memcpy (loc, buf, runlen);
8948 }
8949 else
8950 {
8951 memcpy (buf, loc, sortlen);
8952 memmove (loc, p, runlen);
8953 memcpy (loc + runlen, buf, sortlen);
8954 }
8955 p += runlen - elt_size;
8956 }
8957 }
8958 /* Hashes are no longer valid. */
8959 free (reldata->hashes);
8960 reldata->hashes = NULL;
8961 free (buf);
8962 }
8963 return TRUE;
8964 }
8965
8966 struct elf_link_sort_rela
8967 {
8968 union {
8969 bfd_vma offset;
8970 bfd_vma sym_mask;
8971 } u;
8972 enum elf_reloc_type_class type;
8973 /* We use this as an array of size int_rels_per_ext_rel. */
8974 Elf_Internal_Rela rela[1];
8975 };
8976
8977 static int
8978 elf_link_sort_cmp1 (const void *A, const void *B)
8979 {
8980 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8981 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8982 int relativea, relativeb;
8983
8984 relativea = a->type == reloc_class_relative;
8985 relativeb = b->type == reloc_class_relative;
8986
8987 if (relativea < relativeb)
8988 return 1;
8989 if (relativea > relativeb)
8990 return -1;
8991 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8992 return -1;
8993 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8994 return 1;
8995 if (a->rela->r_offset < b->rela->r_offset)
8996 return -1;
8997 if (a->rela->r_offset > b->rela->r_offset)
8998 return 1;
8999 return 0;
9000 }
9001
9002 static int
9003 elf_link_sort_cmp2 (const void *A, const void *B)
9004 {
9005 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9006 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9007
9008 if (a->type < b->type)
9009 return -1;
9010 if (a->type > b->type)
9011 return 1;
9012 if (a->u.offset < b->u.offset)
9013 return -1;
9014 if (a->u.offset > b->u.offset)
9015 return 1;
9016 if (a->rela->r_offset < b->rela->r_offset)
9017 return -1;
9018 if (a->rela->r_offset > b->rela->r_offset)
9019 return 1;
9020 return 0;
9021 }
9022
9023 static size_t
9024 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9025 {
9026 asection *dynamic_relocs;
9027 asection *rela_dyn;
9028 asection *rel_dyn;
9029 bfd_size_type count, size;
9030 size_t i, ret, sort_elt, ext_size;
9031 bfd_byte *sort, *s_non_relative, *p;
9032 struct elf_link_sort_rela *sq;
9033 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9034 int i2e = bed->s->int_rels_per_ext_rel;
9035 unsigned int opb = bfd_octets_per_byte (abfd);
9036 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9037 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9038 struct bfd_link_order *lo;
9039 bfd_vma r_sym_mask;
9040 bfd_boolean use_rela;
9041
9042 /* Find a dynamic reloc section. */
9043 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9044 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9045 if (rela_dyn != NULL && rela_dyn->size > 0
9046 && rel_dyn != NULL && rel_dyn->size > 0)
9047 {
9048 bfd_boolean use_rela_initialised = FALSE;
9049
9050 /* This is just here to stop gcc from complaining.
9051 Its initialization checking code is not perfect. */
9052 use_rela = TRUE;
9053
9054 /* Both sections are present. Examine the sizes
9055 of the indirect sections to help us choose. */
9056 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9057 if (lo->type == bfd_indirect_link_order)
9058 {
9059 asection *o = lo->u.indirect.section;
9060
9061 if ((o->size % bed->s->sizeof_rela) == 0)
9062 {
9063 if ((o->size % bed->s->sizeof_rel) == 0)
9064 /* Section size is divisible by both rel and rela sizes.
9065 It is of no help to us. */
9066 ;
9067 else
9068 {
9069 /* Section size is only divisible by rela. */
9070 if (use_rela_initialised && !use_rela)
9071 {
9072 _bfd_error_handler (_("%pB: unable to sort relocs - "
9073 "they are in more than one size"),
9074 abfd);
9075 bfd_set_error (bfd_error_invalid_operation);
9076 return 0;
9077 }
9078 else
9079 {
9080 use_rela = TRUE;
9081 use_rela_initialised = TRUE;
9082 }
9083 }
9084 }
9085 else if ((o->size % bed->s->sizeof_rel) == 0)
9086 {
9087 /* Section size is only divisible by rel. */
9088 if (use_rela_initialised && use_rela)
9089 {
9090 _bfd_error_handler (_("%pB: unable to sort relocs - "
9091 "they are in more than one size"),
9092 abfd);
9093 bfd_set_error (bfd_error_invalid_operation);
9094 return 0;
9095 }
9096 else
9097 {
9098 use_rela = FALSE;
9099 use_rela_initialised = TRUE;
9100 }
9101 }
9102 else
9103 {
9104 /* The section size is not divisible by either -
9105 something is wrong. */
9106 _bfd_error_handler (_("%pB: unable to sort relocs - "
9107 "they are of an unknown size"), abfd);
9108 bfd_set_error (bfd_error_invalid_operation);
9109 return 0;
9110 }
9111 }
9112
9113 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9114 if (lo->type == bfd_indirect_link_order)
9115 {
9116 asection *o = lo->u.indirect.section;
9117
9118 if ((o->size % bed->s->sizeof_rela) == 0)
9119 {
9120 if ((o->size % bed->s->sizeof_rel) == 0)
9121 /* Section size is divisible by both rel and rela sizes.
9122 It is of no help to us. */
9123 ;
9124 else
9125 {
9126 /* Section size is only divisible by rela. */
9127 if (use_rela_initialised && !use_rela)
9128 {
9129 _bfd_error_handler (_("%pB: unable to sort relocs - "
9130 "they are in more than one size"),
9131 abfd);
9132 bfd_set_error (bfd_error_invalid_operation);
9133 return 0;
9134 }
9135 else
9136 {
9137 use_rela = TRUE;
9138 use_rela_initialised = TRUE;
9139 }
9140 }
9141 }
9142 else if ((o->size % bed->s->sizeof_rel) == 0)
9143 {
9144 /* Section size is only divisible by rel. */
9145 if (use_rela_initialised && use_rela)
9146 {
9147 _bfd_error_handler (_("%pB: unable to sort relocs - "
9148 "they are in more than one size"),
9149 abfd);
9150 bfd_set_error (bfd_error_invalid_operation);
9151 return 0;
9152 }
9153 else
9154 {
9155 use_rela = FALSE;
9156 use_rela_initialised = TRUE;
9157 }
9158 }
9159 else
9160 {
9161 /* The section size is not divisible by either -
9162 something is wrong. */
9163 _bfd_error_handler (_("%pB: unable to sort relocs - "
9164 "they are of an unknown size"), abfd);
9165 bfd_set_error (bfd_error_invalid_operation);
9166 return 0;
9167 }
9168 }
9169
9170 if (! use_rela_initialised)
9171 /* Make a guess. */
9172 use_rela = TRUE;
9173 }
9174 else if (rela_dyn != NULL && rela_dyn->size > 0)
9175 use_rela = TRUE;
9176 else if (rel_dyn != NULL && rel_dyn->size > 0)
9177 use_rela = FALSE;
9178 else
9179 return 0;
9180
9181 if (use_rela)
9182 {
9183 dynamic_relocs = rela_dyn;
9184 ext_size = bed->s->sizeof_rela;
9185 swap_in = bed->s->swap_reloca_in;
9186 swap_out = bed->s->swap_reloca_out;
9187 }
9188 else
9189 {
9190 dynamic_relocs = rel_dyn;
9191 ext_size = bed->s->sizeof_rel;
9192 swap_in = bed->s->swap_reloc_in;
9193 swap_out = bed->s->swap_reloc_out;
9194 }
9195
9196 size = 0;
9197 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9198 if (lo->type == bfd_indirect_link_order)
9199 size += lo->u.indirect.section->size;
9200
9201 if (size != dynamic_relocs->size)
9202 return 0;
9203
9204 sort_elt = (sizeof (struct elf_link_sort_rela)
9205 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9206
9207 count = dynamic_relocs->size / ext_size;
9208 if (count == 0)
9209 return 0;
9210 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9211
9212 if (sort == NULL)
9213 {
9214 (*info->callbacks->warning)
9215 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
9216 return 0;
9217 }
9218
9219 if (bed->s->arch_size == 32)
9220 r_sym_mask = ~(bfd_vma) 0xff;
9221 else
9222 r_sym_mask = ~(bfd_vma) 0xffffffff;
9223
9224 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9225 if (lo->type == bfd_indirect_link_order)
9226 {
9227 bfd_byte *erel, *erelend;
9228 asection *o = lo->u.indirect.section;
9229
9230 if (o->contents == NULL && o->size != 0)
9231 {
9232 /* This is a reloc section that is being handled as a normal
9233 section. See bfd_section_from_shdr. We can't combine
9234 relocs in this case. */
9235 free (sort);
9236 return 0;
9237 }
9238 erel = o->contents;
9239 erelend = o->contents + o->size;
9240 p = sort + o->output_offset * opb / ext_size * sort_elt;
9241
9242 while (erel < erelend)
9243 {
9244 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9245
9246 (*swap_in) (abfd, erel, s->rela);
9247 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9248 s->u.sym_mask = r_sym_mask;
9249 p += sort_elt;
9250 erel += ext_size;
9251 }
9252 }
9253
9254 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9255
9256 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9257 {
9258 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9259 if (s->type != reloc_class_relative)
9260 break;
9261 }
9262 ret = i;
9263 s_non_relative = p;
9264
9265 sq = (struct elf_link_sort_rela *) s_non_relative;
9266 for (; i < count; i++, p += sort_elt)
9267 {
9268 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9269 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9270 sq = sp;
9271 sp->u.offset = sq->rela->r_offset;
9272 }
9273
9274 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9275
9276 struct elf_link_hash_table *htab = elf_hash_table (info);
9277 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9278 {
9279 /* We have plt relocs in .rela.dyn. */
9280 sq = (struct elf_link_sort_rela *) sort;
9281 for (i = 0; i < count; i++)
9282 if (sq[count - i - 1].type != reloc_class_plt)
9283 break;
9284 if (i != 0 && htab->srelplt->size == i * ext_size)
9285 {
9286 struct bfd_link_order **plo;
9287 /* Put srelplt link_order last. This is so the output_offset
9288 set in the next loop is correct for DT_JMPREL. */
9289 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9290 if ((*plo)->type == bfd_indirect_link_order
9291 && (*plo)->u.indirect.section == htab->srelplt)
9292 {
9293 lo = *plo;
9294 *plo = lo->next;
9295 }
9296 else
9297 plo = &(*plo)->next;
9298 *plo = lo;
9299 lo->next = NULL;
9300 dynamic_relocs->map_tail.link_order = lo;
9301 }
9302 }
9303
9304 p = sort;
9305 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9306 if (lo->type == bfd_indirect_link_order)
9307 {
9308 bfd_byte *erel, *erelend;
9309 asection *o = lo->u.indirect.section;
9310
9311 erel = o->contents;
9312 erelend = o->contents + o->size;
9313 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9314 while (erel < erelend)
9315 {
9316 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9317 (*swap_out) (abfd, s->rela, erel);
9318 p += sort_elt;
9319 erel += ext_size;
9320 }
9321 }
9322
9323 free (sort);
9324 *psec = dynamic_relocs;
9325 return ret;
9326 }
9327
9328 /* Add a symbol to the output symbol string table. */
9329
9330 static int
9331 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9332 const char *name,
9333 Elf_Internal_Sym *elfsym,
9334 asection *input_sec,
9335 struct elf_link_hash_entry *h)
9336 {
9337 int (*output_symbol_hook)
9338 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9339 struct elf_link_hash_entry *);
9340 struct elf_link_hash_table *hash_table;
9341 const struct elf_backend_data *bed;
9342 bfd_size_type strtabsize;
9343
9344 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9345
9346 bed = get_elf_backend_data (flinfo->output_bfd);
9347 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9348 if (output_symbol_hook != NULL)
9349 {
9350 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9351 if (ret != 1)
9352 return ret;
9353 }
9354
9355 if (name == NULL
9356 || *name == '\0'
9357 || (input_sec->flags & SEC_EXCLUDE))
9358 elfsym->st_name = (unsigned long) -1;
9359 else
9360 {
9361 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9362 to get the final offset for st_name. */
9363 elfsym->st_name
9364 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9365 name, FALSE);
9366 if (elfsym->st_name == (unsigned long) -1)
9367 return 0;
9368 }
9369
9370 hash_table = elf_hash_table (flinfo->info);
9371 strtabsize = hash_table->strtabsize;
9372 if (strtabsize <= hash_table->strtabcount)
9373 {
9374 strtabsize += strtabsize;
9375 hash_table->strtabsize = strtabsize;
9376 strtabsize *= sizeof (*hash_table->strtab);
9377 hash_table->strtab
9378 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9379 strtabsize);
9380 if (hash_table->strtab == NULL)
9381 return 0;
9382 }
9383 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9384 hash_table->strtab[hash_table->strtabcount].dest_index
9385 = hash_table->strtabcount;
9386 hash_table->strtab[hash_table->strtabcount].destshndx_index
9387 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9388
9389 bfd_get_symcount (flinfo->output_bfd) += 1;
9390 hash_table->strtabcount += 1;
9391
9392 return 1;
9393 }
9394
9395 /* Swap symbols out to the symbol table and flush the output symbols to
9396 the file. */
9397
9398 static bfd_boolean
9399 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9400 {
9401 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9402 bfd_size_type amt;
9403 size_t i;
9404 const struct elf_backend_data *bed;
9405 bfd_byte *symbuf;
9406 Elf_Internal_Shdr *hdr;
9407 file_ptr pos;
9408 bfd_boolean ret;
9409
9410 if (!hash_table->strtabcount)
9411 return TRUE;
9412
9413 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9414
9415 bed = get_elf_backend_data (flinfo->output_bfd);
9416
9417 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9418 symbuf = (bfd_byte *) bfd_malloc (amt);
9419 if (symbuf == NULL)
9420 return FALSE;
9421
9422 if (flinfo->symshndxbuf)
9423 {
9424 amt = sizeof (Elf_External_Sym_Shndx);
9425 amt *= bfd_get_symcount (flinfo->output_bfd);
9426 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9427 if (flinfo->symshndxbuf == NULL)
9428 {
9429 free (symbuf);
9430 return FALSE;
9431 }
9432 }
9433
9434 for (i = 0; i < hash_table->strtabcount; i++)
9435 {
9436 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9437 if (elfsym->sym.st_name == (unsigned long) -1)
9438 elfsym->sym.st_name = 0;
9439 else
9440 elfsym->sym.st_name
9441 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9442 elfsym->sym.st_name);
9443 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9444 ((bfd_byte *) symbuf
9445 + (elfsym->dest_index
9446 * bed->s->sizeof_sym)),
9447 (flinfo->symshndxbuf
9448 + elfsym->destshndx_index));
9449 }
9450
9451 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9452 pos = hdr->sh_offset + hdr->sh_size;
9453 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9454 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9455 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9456 {
9457 hdr->sh_size += amt;
9458 ret = TRUE;
9459 }
9460 else
9461 ret = FALSE;
9462
9463 free (symbuf);
9464
9465 free (hash_table->strtab);
9466 hash_table->strtab = NULL;
9467
9468 return ret;
9469 }
9470
9471 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9472
9473 static bfd_boolean
9474 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9475 {
9476 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9477 && sym->st_shndx < SHN_LORESERVE)
9478 {
9479 /* The gABI doesn't support dynamic symbols in output sections
9480 beyond 64k. */
9481 _bfd_error_handler
9482 /* xgettext:c-format */
9483 (_("%pB: too many sections: %d (>= %d)"),
9484 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9485 bfd_set_error (bfd_error_nonrepresentable_section);
9486 return FALSE;
9487 }
9488 return TRUE;
9489 }
9490
9491 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9492 allowing an unsatisfied unversioned symbol in the DSO to match a
9493 versioned symbol that would normally require an explicit version.
9494 We also handle the case that a DSO references a hidden symbol
9495 which may be satisfied by a versioned symbol in another DSO. */
9496
9497 static bfd_boolean
9498 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9499 const struct elf_backend_data *bed,
9500 struct elf_link_hash_entry *h)
9501 {
9502 bfd *abfd;
9503 struct elf_link_loaded_list *loaded;
9504
9505 if (!is_elf_hash_table (info->hash))
9506 return FALSE;
9507
9508 /* Check indirect symbol. */
9509 while (h->root.type == bfd_link_hash_indirect)
9510 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9511
9512 switch (h->root.type)
9513 {
9514 default:
9515 abfd = NULL;
9516 break;
9517
9518 case bfd_link_hash_undefined:
9519 case bfd_link_hash_undefweak:
9520 abfd = h->root.u.undef.abfd;
9521 if (abfd == NULL
9522 || (abfd->flags & DYNAMIC) == 0
9523 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9524 return FALSE;
9525 break;
9526
9527 case bfd_link_hash_defined:
9528 case bfd_link_hash_defweak:
9529 abfd = h->root.u.def.section->owner;
9530 break;
9531
9532 case bfd_link_hash_common:
9533 abfd = h->root.u.c.p->section->owner;
9534 break;
9535 }
9536 BFD_ASSERT (abfd != NULL);
9537
9538 for (loaded = elf_hash_table (info)->loaded;
9539 loaded != NULL;
9540 loaded = loaded->next)
9541 {
9542 bfd *input;
9543 Elf_Internal_Shdr *hdr;
9544 size_t symcount;
9545 size_t extsymcount;
9546 size_t extsymoff;
9547 Elf_Internal_Shdr *versymhdr;
9548 Elf_Internal_Sym *isym;
9549 Elf_Internal_Sym *isymend;
9550 Elf_Internal_Sym *isymbuf;
9551 Elf_External_Versym *ever;
9552 Elf_External_Versym *extversym;
9553
9554 input = loaded->abfd;
9555
9556 /* We check each DSO for a possible hidden versioned definition. */
9557 if (input == abfd
9558 || (input->flags & DYNAMIC) == 0
9559 || elf_dynversym (input) == 0)
9560 continue;
9561
9562 hdr = &elf_tdata (input)->dynsymtab_hdr;
9563
9564 symcount = hdr->sh_size / bed->s->sizeof_sym;
9565 if (elf_bad_symtab (input))
9566 {
9567 extsymcount = symcount;
9568 extsymoff = 0;
9569 }
9570 else
9571 {
9572 extsymcount = symcount - hdr->sh_info;
9573 extsymoff = hdr->sh_info;
9574 }
9575
9576 if (extsymcount == 0)
9577 continue;
9578
9579 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9580 NULL, NULL, NULL);
9581 if (isymbuf == NULL)
9582 return FALSE;
9583
9584 /* Read in any version definitions. */
9585 versymhdr = &elf_tdata (input)->dynversym_hdr;
9586 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
9587 if (extversym == NULL)
9588 goto error_ret;
9589
9590 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9591 || (bfd_bread (extversym, versymhdr->sh_size, input)
9592 != versymhdr->sh_size))
9593 {
9594 free (extversym);
9595 error_ret:
9596 free (isymbuf);
9597 return FALSE;
9598 }
9599
9600 ever = extversym + extsymoff;
9601 isymend = isymbuf + extsymcount;
9602 for (isym = isymbuf; isym < isymend; isym++, ever++)
9603 {
9604 const char *name;
9605 Elf_Internal_Versym iver;
9606 unsigned short version_index;
9607
9608 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9609 || isym->st_shndx == SHN_UNDEF)
9610 continue;
9611
9612 name = bfd_elf_string_from_elf_section (input,
9613 hdr->sh_link,
9614 isym->st_name);
9615 if (strcmp (name, h->root.root.string) != 0)
9616 continue;
9617
9618 _bfd_elf_swap_versym_in (input, ever, &iver);
9619
9620 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9621 && !(h->def_regular
9622 && h->forced_local))
9623 {
9624 /* If we have a non-hidden versioned sym, then it should
9625 have provided a definition for the undefined sym unless
9626 it is defined in a non-shared object and forced local.
9627 */
9628 abort ();
9629 }
9630
9631 version_index = iver.vs_vers & VERSYM_VERSION;
9632 if (version_index == 1 || version_index == 2)
9633 {
9634 /* This is the base or first version. We can use it. */
9635 free (extversym);
9636 free (isymbuf);
9637 return TRUE;
9638 }
9639 }
9640
9641 free (extversym);
9642 free (isymbuf);
9643 }
9644
9645 return FALSE;
9646 }
9647
9648 /* Convert ELF common symbol TYPE. */
9649
9650 static int
9651 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9652 {
9653 /* Commom symbol can only appear in relocatable link. */
9654 if (!bfd_link_relocatable (info))
9655 abort ();
9656 switch (info->elf_stt_common)
9657 {
9658 case unchanged:
9659 break;
9660 case elf_stt_common:
9661 type = STT_COMMON;
9662 break;
9663 case no_elf_stt_common:
9664 type = STT_OBJECT;
9665 break;
9666 }
9667 return type;
9668 }
9669
9670 /* Add an external symbol to the symbol table. This is called from
9671 the hash table traversal routine. When generating a shared object,
9672 we go through the symbol table twice. The first time we output
9673 anything that might have been forced to local scope in a version
9674 script. The second time we output the symbols that are still
9675 global symbols. */
9676
9677 static bfd_boolean
9678 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9679 {
9680 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9681 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9682 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9683 bfd_boolean strip;
9684 Elf_Internal_Sym sym;
9685 asection *input_sec;
9686 const struct elf_backend_data *bed;
9687 long indx;
9688 int ret;
9689 unsigned int type;
9690
9691 if (h->root.type == bfd_link_hash_warning)
9692 {
9693 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9694 if (h->root.type == bfd_link_hash_new)
9695 return TRUE;
9696 }
9697
9698 /* Decide whether to output this symbol in this pass. */
9699 if (eoinfo->localsyms)
9700 {
9701 if (!h->forced_local)
9702 return TRUE;
9703 }
9704 else
9705 {
9706 if (h->forced_local)
9707 return TRUE;
9708 }
9709
9710 bed = get_elf_backend_data (flinfo->output_bfd);
9711
9712 if (h->root.type == bfd_link_hash_undefined)
9713 {
9714 /* If we have an undefined symbol reference here then it must have
9715 come from a shared library that is being linked in. (Undefined
9716 references in regular files have already been handled unless
9717 they are in unreferenced sections which are removed by garbage
9718 collection). */
9719 bfd_boolean ignore_undef = FALSE;
9720
9721 /* Some symbols may be special in that the fact that they're
9722 undefined can be safely ignored - let backend determine that. */
9723 if (bed->elf_backend_ignore_undef_symbol)
9724 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9725
9726 /* If we are reporting errors for this situation then do so now. */
9727 if (!ignore_undef
9728 && h->ref_dynamic
9729 && (!h->ref_regular || flinfo->info->gc_sections)
9730 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9731 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9732 (*flinfo->info->callbacks->undefined_symbol)
9733 (flinfo->info, h->root.root.string,
9734 h->ref_regular ? NULL : h->root.u.undef.abfd,
9735 NULL, 0,
9736 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
9737
9738 /* Strip a global symbol defined in a discarded section. */
9739 if (h->indx == -3)
9740 return TRUE;
9741 }
9742
9743 /* We should also warn if a forced local symbol is referenced from
9744 shared libraries. */
9745 if (bfd_link_executable (flinfo->info)
9746 && h->forced_local
9747 && h->ref_dynamic
9748 && h->def_regular
9749 && !h->dynamic_def
9750 && h->ref_dynamic_nonweak
9751 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9752 {
9753 bfd *def_bfd;
9754 const char *msg;
9755 struct elf_link_hash_entry *hi = h;
9756
9757 /* Check indirect symbol. */
9758 while (hi->root.type == bfd_link_hash_indirect)
9759 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9760
9761 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9762 /* xgettext:c-format */
9763 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
9764 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9765 /* xgettext:c-format */
9766 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
9767 else
9768 /* xgettext:c-format */
9769 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
9770 def_bfd = flinfo->output_bfd;
9771 if (hi->root.u.def.section != bfd_abs_section_ptr)
9772 def_bfd = hi->root.u.def.section->owner;
9773 _bfd_error_handler (msg, flinfo->output_bfd,
9774 h->root.root.string, def_bfd);
9775 bfd_set_error (bfd_error_bad_value);
9776 eoinfo->failed = TRUE;
9777 return FALSE;
9778 }
9779
9780 /* We don't want to output symbols that have never been mentioned by
9781 a regular file, or that we have been told to strip. However, if
9782 h->indx is set to -2, the symbol is used by a reloc and we must
9783 output it. */
9784 strip = FALSE;
9785 if (h->indx == -2)
9786 ;
9787 else if ((h->def_dynamic
9788 || h->ref_dynamic
9789 || h->root.type == bfd_link_hash_new)
9790 && !h->def_regular
9791 && !h->ref_regular)
9792 strip = TRUE;
9793 else if (flinfo->info->strip == strip_all)
9794 strip = TRUE;
9795 else if (flinfo->info->strip == strip_some
9796 && bfd_hash_lookup (flinfo->info->keep_hash,
9797 h->root.root.string, FALSE, FALSE) == NULL)
9798 strip = TRUE;
9799 else if ((h->root.type == bfd_link_hash_defined
9800 || h->root.type == bfd_link_hash_defweak)
9801 && ((flinfo->info->strip_discarded
9802 && discarded_section (h->root.u.def.section))
9803 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9804 && h->root.u.def.section->owner != NULL
9805 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9806 strip = TRUE;
9807 else if ((h->root.type == bfd_link_hash_undefined
9808 || h->root.type == bfd_link_hash_undefweak)
9809 && h->root.u.undef.abfd != NULL
9810 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9811 strip = TRUE;
9812
9813 type = h->type;
9814
9815 /* If we're stripping it, and it's not a dynamic symbol, there's
9816 nothing else to do. However, if it is a forced local symbol or
9817 an ifunc symbol we need to give the backend finish_dynamic_symbol
9818 function a chance to make it dynamic. */
9819 if (strip
9820 && h->dynindx == -1
9821 && type != STT_GNU_IFUNC
9822 && !h->forced_local)
9823 return TRUE;
9824
9825 sym.st_value = 0;
9826 sym.st_size = h->size;
9827 sym.st_other = h->other;
9828 switch (h->root.type)
9829 {
9830 default:
9831 case bfd_link_hash_new:
9832 case bfd_link_hash_warning:
9833 abort ();
9834 return FALSE;
9835
9836 case bfd_link_hash_undefined:
9837 case bfd_link_hash_undefweak:
9838 input_sec = bfd_und_section_ptr;
9839 sym.st_shndx = SHN_UNDEF;
9840 break;
9841
9842 case bfd_link_hash_defined:
9843 case bfd_link_hash_defweak:
9844 {
9845 input_sec = h->root.u.def.section;
9846 if (input_sec->output_section != NULL)
9847 {
9848 sym.st_shndx =
9849 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9850 input_sec->output_section);
9851 if (sym.st_shndx == SHN_BAD)
9852 {
9853 _bfd_error_handler
9854 /* xgettext:c-format */
9855 (_("%pB: could not find output section %pA for input section %pA"),
9856 flinfo->output_bfd, input_sec->output_section, input_sec);
9857 bfd_set_error (bfd_error_nonrepresentable_section);
9858 eoinfo->failed = TRUE;
9859 return FALSE;
9860 }
9861
9862 /* ELF symbols in relocatable files are section relative,
9863 but in nonrelocatable files they are virtual
9864 addresses. */
9865 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9866 if (!bfd_link_relocatable (flinfo->info))
9867 {
9868 sym.st_value += input_sec->output_section->vma;
9869 if (h->type == STT_TLS)
9870 {
9871 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9872 if (tls_sec != NULL)
9873 sym.st_value -= tls_sec->vma;
9874 }
9875 }
9876 }
9877 else
9878 {
9879 BFD_ASSERT (input_sec->owner == NULL
9880 || (input_sec->owner->flags & DYNAMIC) != 0);
9881 sym.st_shndx = SHN_UNDEF;
9882 input_sec = bfd_und_section_ptr;
9883 }
9884 }
9885 break;
9886
9887 case bfd_link_hash_common:
9888 input_sec = h->root.u.c.p->section;
9889 sym.st_shndx = bed->common_section_index (input_sec);
9890 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9891 break;
9892
9893 case bfd_link_hash_indirect:
9894 /* These symbols are created by symbol versioning. They point
9895 to the decorated version of the name. For example, if the
9896 symbol foo@@GNU_1.2 is the default, which should be used when
9897 foo is used with no version, then we add an indirect symbol
9898 foo which points to foo@@GNU_1.2. We ignore these symbols,
9899 since the indirected symbol is already in the hash table. */
9900 return TRUE;
9901 }
9902
9903 if (type == STT_COMMON || type == STT_OBJECT)
9904 switch (h->root.type)
9905 {
9906 case bfd_link_hash_common:
9907 type = elf_link_convert_common_type (flinfo->info, type);
9908 break;
9909 case bfd_link_hash_defined:
9910 case bfd_link_hash_defweak:
9911 if (bed->common_definition (&sym))
9912 type = elf_link_convert_common_type (flinfo->info, type);
9913 else
9914 type = STT_OBJECT;
9915 break;
9916 case bfd_link_hash_undefined:
9917 case bfd_link_hash_undefweak:
9918 break;
9919 default:
9920 abort ();
9921 }
9922
9923 if (h->forced_local)
9924 {
9925 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9926 /* Turn off visibility on local symbol. */
9927 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9928 }
9929 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9930 else if (h->unique_global && h->def_regular)
9931 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9932 else if (h->root.type == bfd_link_hash_undefweak
9933 || h->root.type == bfd_link_hash_defweak)
9934 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9935 else
9936 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9937 sym.st_target_internal = h->target_internal;
9938
9939 /* Give the processor backend a chance to tweak the symbol value,
9940 and also to finish up anything that needs to be done for this
9941 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9942 forced local syms when non-shared is due to a historical quirk.
9943 STT_GNU_IFUNC symbol must go through PLT. */
9944 if ((h->type == STT_GNU_IFUNC
9945 && h->def_regular
9946 && !bfd_link_relocatable (flinfo->info))
9947 || ((h->dynindx != -1
9948 || h->forced_local)
9949 && ((bfd_link_pic (flinfo->info)
9950 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9951 || h->root.type != bfd_link_hash_undefweak))
9952 || !h->forced_local)
9953 && elf_hash_table (flinfo->info)->dynamic_sections_created))
9954 {
9955 if (! ((*bed->elf_backend_finish_dynamic_symbol)
9956 (flinfo->output_bfd, flinfo->info, h, &sym)))
9957 {
9958 eoinfo->failed = TRUE;
9959 return FALSE;
9960 }
9961 }
9962
9963 /* If we are marking the symbol as undefined, and there are no
9964 non-weak references to this symbol from a regular object, then
9965 mark the symbol as weak undefined; if there are non-weak
9966 references, mark the symbol as strong. We can't do this earlier,
9967 because it might not be marked as undefined until the
9968 finish_dynamic_symbol routine gets through with it. */
9969 if (sym.st_shndx == SHN_UNDEF
9970 && h->ref_regular
9971 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9972 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9973 {
9974 int bindtype;
9975 type = ELF_ST_TYPE (sym.st_info);
9976
9977 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9978 if (type == STT_GNU_IFUNC)
9979 type = STT_FUNC;
9980
9981 if (h->ref_regular_nonweak)
9982 bindtype = STB_GLOBAL;
9983 else
9984 bindtype = STB_WEAK;
9985 sym.st_info = ELF_ST_INFO (bindtype, type);
9986 }
9987
9988 /* If this is a symbol defined in a dynamic library, don't use the
9989 symbol size from the dynamic library. Relinking an executable
9990 against a new library may introduce gratuitous changes in the
9991 executable's symbols if we keep the size. */
9992 if (sym.st_shndx == SHN_UNDEF
9993 && !h->def_regular
9994 && h->def_dynamic)
9995 sym.st_size = 0;
9996
9997 /* If a non-weak symbol with non-default visibility is not defined
9998 locally, it is a fatal error. */
9999 if (!bfd_link_relocatable (flinfo->info)
10000 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10001 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10002 && h->root.type == bfd_link_hash_undefined
10003 && !h->def_regular)
10004 {
10005 const char *msg;
10006
10007 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10008 /* xgettext:c-format */
10009 msg = _("%pB: protected symbol `%s' isn't defined");
10010 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10011 /* xgettext:c-format */
10012 msg = _("%pB: internal symbol `%s' isn't defined");
10013 else
10014 /* xgettext:c-format */
10015 msg = _("%pB: hidden symbol `%s' isn't defined");
10016 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10017 bfd_set_error (bfd_error_bad_value);
10018 eoinfo->failed = TRUE;
10019 return FALSE;
10020 }
10021
10022 /* If this symbol should be put in the .dynsym section, then put it
10023 there now. We already know the symbol index. We also fill in
10024 the entry in the .hash section. */
10025 if (elf_hash_table (flinfo->info)->dynsym != NULL
10026 && h->dynindx != -1
10027 && elf_hash_table (flinfo->info)->dynamic_sections_created)
10028 {
10029 bfd_byte *esym;
10030
10031 /* Since there is no version information in the dynamic string,
10032 if there is no version info in symbol version section, we will
10033 have a run-time problem if not linking executable, referenced
10034 by shared library, or not bound locally. */
10035 if (h->verinfo.verdef == NULL
10036 && (!bfd_link_executable (flinfo->info)
10037 || h->ref_dynamic
10038 || !h->def_regular))
10039 {
10040 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10041
10042 if (p && p [1] != '\0')
10043 {
10044 _bfd_error_handler
10045 /* xgettext:c-format */
10046 (_("%pB: no symbol version section for versioned symbol `%s'"),
10047 flinfo->output_bfd, h->root.root.string);
10048 eoinfo->failed = TRUE;
10049 return FALSE;
10050 }
10051 }
10052
10053 sym.st_name = h->dynstr_index;
10054 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10055 + h->dynindx * bed->s->sizeof_sym);
10056 if (!check_dynsym (flinfo->output_bfd, &sym))
10057 {
10058 eoinfo->failed = TRUE;
10059 return FALSE;
10060 }
10061 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
10062
10063 if (flinfo->hash_sec != NULL)
10064 {
10065 size_t hash_entry_size;
10066 bfd_byte *bucketpos;
10067 bfd_vma chain;
10068 size_t bucketcount;
10069 size_t bucket;
10070
10071 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
10072 bucket = h->u.elf_hash_value % bucketcount;
10073
10074 hash_entry_size
10075 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10076 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
10077 + (bucket + 2) * hash_entry_size);
10078 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10079 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10080 bucketpos);
10081 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10082 ((bfd_byte *) flinfo->hash_sec->contents
10083 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10084 }
10085
10086 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
10087 {
10088 Elf_Internal_Versym iversym;
10089 Elf_External_Versym *eversym;
10090
10091 if (!h->def_regular)
10092 {
10093 if (h->verinfo.verdef == NULL
10094 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10095 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
10096 iversym.vs_vers = 0;
10097 else
10098 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10099 }
10100 else
10101 {
10102 if (h->verinfo.vertree == NULL)
10103 iversym.vs_vers = 1;
10104 else
10105 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
10106 if (flinfo->info->create_default_symver)
10107 iversym.vs_vers++;
10108 }
10109
10110 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10111 defined locally. */
10112 if (h->versioned == versioned_hidden && h->def_regular)
10113 iversym.vs_vers |= VERSYM_HIDDEN;
10114
10115 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10116 eversym += h->dynindx;
10117 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10118 }
10119 }
10120
10121 /* If the symbol is undefined, and we didn't output it to .dynsym,
10122 strip it from .symtab too. Obviously we can't do this for
10123 relocatable output or when needed for --emit-relocs. */
10124 else if (input_sec == bfd_und_section_ptr
10125 && h->indx != -2
10126 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10127 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10128 && !bfd_link_relocatable (flinfo->info))
10129 return TRUE;
10130
10131 /* Also strip others that we couldn't earlier due to dynamic symbol
10132 processing. */
10133 if (strip)
10134 return TRUE;
10135 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10136 return TRUE;
10137
10138 /* Output a FILE symbol so that following locals are not associated
10139 with the wrong input file. We need one for forced local symbols
10140 if we've seen more than one FILE symbol or when we have exactly
10141 one FILE symbol but global symbols are present in a file other
10142 than the one with the FILE symbol. We also need one if linker
10143 defined symbols are present. In practice these conditions are
10144 always met, so just emit the FILE symbol unconditionally. */
10145 if (eoinfo->localsyms
10146 && !eoinfo->file_sym_done
10147 && eoinfo->flinfo->filesym_count != 0)
10148 {
10149 Elf_Internal_Sym fsym;
10150
10151 memset (&fsym, 0, sizeof (fsym));
10152 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10153 fsym.st_shndx = SHN_ABS;
10154 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10155 bfd_und_section_ptr, NULL))
10156 return FALSE;
10157
10158 eoinfo->file_sym_done = TRUE;
10159 }
10160
10161 indx = bfd_get_symcount (flinfo->output_bfd);
10162 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10163 input_sec, h);
10164 if (ret == 0)
10165 {
10166 eoinfo->failed = TRUE;
10167 return FALSE;
10168 }
10169 else if (ret == 1)
10170 h->indx = indx;
10171 else if (h->indx == -2)
10172 abort();
10173
10174 return TRUE;
10175 }
10176
10177 /* Return TRUE if special handling is done for relocs in SEC against
10178 symbols defined in discarded sections. */
10179
10180 static bfd_boolean
10181 elf_section_ignore_discarded_relocs (asection *sec)
10182 {
10183 const struct elf_backend_data *bed;
10184
10185 switch (sec->sec_info_type)
10186 {
10187 case SEC_INFO_TYPE_STABS:
10188 case SEC_INFO_TYPE_EH_FRAME:
10189 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10190 return TRUE;
10191 default:
10192 break;
10193 }
10194
10195 bed = get_elf_backend_data (sec->owner);
10196 if (bed->elf_backend_ignore_discarded_relocs != NULL
10197 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10198 return TRUE;
10199
10200 return FALSE;
10201 }
10202
10203 /* Return a mask saying how ld should treat relocations in SEC against
10204 symbols defined in discarded sections. If this function returns
10205 COMPLAIN set, ld will issue a warning message. If this function
10206 returns PRETEND set, and the discarded section was link-once and the
10207 same size as the kept link-once section, ld will pretend that the
10208 symbol was actually defined in the kept section. Otherwise ld will
10209 zero the reloc (at least that is the intent, but some cooperation by
10210 the target dependent code is needed, particularly for REL targets). */
10211
10212 unsigned int
10213 _bfd_elf_default_action_discarded (asection *sec)
10214 {
10215 if (sec->flags & SEC_DEBUGGING)
10216 return PRETEND;
10217
10218 if (strcmp (".eh_frame", sec->name) == 0)
10219 return 0;
10220
10221 if (strcmp (".gcc_except_table", sec->name) == 0)
10222 return 0;
10223
10224 return COMPLAIN | PRETEND;
10225 }
10226
10227 /* Find a match between a section and a member of a section group. */
10228
10229 static asection *
10230 match_group_member (asection *sec, asection *group,
10231 struct bfd_link_info *info)
10232 {
10233 asection *first = elf_next_in_group (group);
10234 asection *s = first;
10235
10236 while (s != NULL)
10237 {
10238 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10239 return s;
10240
10241 s = elf_next_in_group (s);
10242 if (s == first)
10243 break;
10244 }
10245
10246 return NULL;
10247 }
10248
10249 /* Check if the kept section of a discarded section SEC can be used
10250 to replace it. Return the replacement if it is OK. Otherwise return
10251 NULL. */
10252
10253 asection *
10254 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10255 {
10256 asection *kept;
10257
10258 kept = sec->kept_section;
10259 if (kept != NULL)
10260 {
10261 if ((kept->flags & SEC_GROUP) != 0)
10262 kept = match_group_member (sec, kept, info);
10263 if (kept != NULL
10264 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10265 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
10266 kept = NULL;
10267 sec->kept_section = kept;
10268 }
10269 return kept;
10270 }
10271
10272 /* Link an input file into the linker output file. This function
10273 handles all the sections and relocations of the input file at once.
10274 This is so that we only have to read the local symbols once, and
10275 don't have to keep them in memory. */
10276
10277 static bfd_boolean
10278 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10279 {
10280 int (*relocate_section)
10281 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10282 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10283 bfd *output_bfd;
10284 Elf_Internal_Shdr *symtab_hdr;
10285 size_t locsymcount;
10286 size_t extsymoff;
10287 Elf_Internal_Sym *isymbuf;
10288 Elf_Internal_Sym *isym;
10289 Elf_Internal_Sym *isymend;
10290 long *pindex;
10291 asection **ppsection;
10292 asection *o;
10293 const struct elf_backend_data *bed;
10294 struct elf_link_hash_entry **sym_hashes;
10295 bfd_size_type address_size;
10296 bfd_vma r_type_mask;
10297 int r_sym_shift;
10298 bfd_boolean have_file_sym = FALSE;
10299
10300 output_bfd = flinfo->output_bfd;
10301 bed = get_elf_backend_data (output_bfd);
10302 relocate_section = bed->elf_backend_relocate_section;
10303
10304 /* If this is a dynamic object, we don't want to do anything here:
10305 we don't want the local symbols, and we don't want the section
10306 contents. */
10307 if ((input_bfd->flags & DYNAMIC) != 0)
10308 return TRUE;
10309
10310 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10311 if (elf_bad_symtab (input_bfd))
10312 {
10313 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10314 extsymoff = 0;
10315 }
10316 else
10317 {
10318 locsymcount = symtab_hdr->sh_info;
10319 extsymoff = symtab_hdr->sh_info;
10320 }
10321
10322 /* Read the local symbols. */
10323 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10324 if (isymbuf == NULL && locsymcount != 0)
10325 {
10326 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10327 flinfo->internal_syms,
10328 flinfo->external_syms,
10329 flinfo->locsym_shndx);
10330 if (isymbuf == NULL)
10331 return FALSE;
10332 }
10333
10334 /* Find local symbol sections and adjust values of symbols in
10335 SEC_MERGE sections. Write out those local symbols we know are
10336 going into the output file. */
10337 isymend = isymbuf + locsymcount;
10338 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10339 isym < isymend;
10340 isym++, pindex++, ppsection++)
10341 {
10342 asection *isec;
10343 const char *name;
10344 Elf_Internal_Sym osym;
10345 long indx;
10346 int ret;
10347
10348 *pindex = -1;
10349
10350 if (elf_bad_symtab (input_bfd))
10351 {
10352 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10353 {
10354 *ppsection = NULL;
10355 continue;
10356 }
10357 }
10358
10359 if (isym->st_shndx == SHN_UNDEF)
10360 isec = bfd_und_section_ptr;
10361 else if (isym->st_shndx == SHN_ABS)
10362 isec = bfd_abs_section_ptr;
10363 else if (isym->st_shndx == SHN_COMMON)
10364 isec = bfd_com_section_ptr;
10365 else
10366 {
10367 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10368 if (isec == NULL)
10369 {
10370 /* Don't attempt to output symbols with st_shnx in the
10371 reserved range other than SHN_ABS and SHN_COMMON. */
10372 *ppsection = NULL;
10373 continue;
10374 }
10375 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10376 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10377 isym->st_value =
10378 _bfd_merged_section_offset (output_bfd, &isec,
10379 elf_section_data (isec)->sec_info,
10380 isym->st_value);
10381 }
10382
10383 *ppsection = isec;
10384
10385 /* Don't output the first, undefined, symbol. In fact, don't
10386 output any undefined local symbol. */
10387 if (isec == bfd_und_section_ptr)
10388 continue;
10389
10390 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10391 {
10392 /* We never output section symbols. Instead, we use the
10393 section symbol of the corresponding section in the output
10394 file. */
10395 continue;
10396 }
10397
10398 /* If we are stripping all symbols, we don't want to output this
10399 one. */
10400 if (flinfo->info->strip == strip_all)
10401 continue;
10402
10403 /* If we are discarding all local symbols, we don't want to
10404 output this one. If we are generating a relocatable output
10405 file, then some of the local symbols may be required by
10406 relocs; we output them below as we discover that they are
10407 needed. */
10408 if (flinfo->info->discard == discard_all)
10409 continue;
10410
10411 /* If this symbol is defined in a section which we are
10412 discarding, we don't need to keep it. */
10413 if (isym->st_shndx != SHN_UNDEF
10414 && isym->st_shndx < SHN_LORESERVE
10415 && bfd_section_removed_from_list (output_bfd,
10416 isec->output_section))
10417 continue;
10418
10419 /* Get the name of the symbol. */
10420 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10421 isym->st_name);
10422 if (name == NULL)
10423 return FALSE;
10424
10425 /* See if we are discarding symbols with this name. */
10426 if ((flinfo->info->strip == strip_some
10427 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
10428 == NULL))
10429 || (((flinfo->info->discard == discard_sec_merge
10430 && (isec->flags & SEC_MERGE)
10431 && !bfd_link_relocatable (flinfo->info))
10432 || flinfo->info->discard == discard_l)
10433 && bfd_is_local_label_name (input_bfd, name)))
10434 continue;
10435
10436 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10437 {
10438 if (input_bfd->lto_output)
10439 /* -flto puts a temp file name here. This means builds
10440 are not reproducible. Discard the symbol. */
10441 continue;
10442 have_file_sym = TRUE;
10443 flinfo->filesym_count += 1;
10444 }
10445 if (!have_file_sym)
10446 {
10447 /* In the absence of debug info, bfd_find_nearest_line uses
10448 FILE symbols to determine the source file for local
10449 function symbols. Provide a FILE symbol here if input
10450 files lack such, so that their symbols won't be
10451 associated with a previous input file. It's not the
10452 source file, but the best we can do. */
10453 have_file_sym = TRUE;
10454 flinfo->filesym_count += 1;
10455 memset (&osym, 0, sizeof (osym));
10456 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10457 osym.st_shndx = SHN_ABS;
10458 if (!elf_link_output_symstrtab (flinfo,
10459 (input_bfd->lto_output ? NULL
10460 : input_bfd->filename),
10461 &osym, bfd_abs_section_ptr,
10462 NULL))
10463 return FALSE;
10464 }
10465
10466 osym = *isym;
10467
10468 /* Adjust the section index for the output file. */
10469 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10470 isec->output_section);
10471 if (osym.st_shndx == SHN_BAD)
10472 return FALSE;
10473
10474 /* ELF symbols in relocatable files are section relative, but
10475 in executable files they are virtual addresses. Note that
10476 this code assumes that all ELF sections have an associated
10477 BFD section with a reasonable value for output_offset; below
10478 we assume that they also have a reasonable value for
10479 output_section. Any special sections must be set up to meet
10480 these requirements. */
10481 osym.st_value += isec->output_offset;
10482 if (!bfd_link_relocatable (flinfo->info))
10483 {
10484 osym.st_value += isec->output_section->vma;
10485 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10486 {
10487 /* STT_TLS symbols are relative to PT_TLS segment base. */
10488 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10489 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10490 }
10491 }
10492
10493 indx = bfd_get_symcount (output_bfd);
10494 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10495 if (ret == 0)
10496 return FALSE;
10497 else if (ret == 1)
10498 *pindex = indx;
10499 }
10500
10501 if (bed->s->arch_size == 32)
10502 {
10503 r_type_mask = 0xff;
10504 r_sym_shift = 8;
10505 address_size = 4;
10506 }
10507 else
10508 {
10509 r_type_mask = 0xffffffff;
10510 r_sym_shift = 32;
10511 address_size = 8;
10512 }
10513
10514 /* Relocate the contents of each section. */
10515 sym_hashes = elf_sym_hashes (input_bfd);
10516 for (o = input_bfd->sections; o != NULL; o = o->next)
10517 {
10518 bfd_byte *contents;
10519
10520 if (! o->linker_mark)
10521 {
10522 /* This section was omitted from the link. */
10523 continue;
10524 }
10525
10526 if (!flinfo->info->resolve_section_groups
10527 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10528 {
10529 /* Deal with the group signature symbol. */
10530 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10531 unsigned long symndx = sec_data->this_hdr.sh_info;
10532 asection *osec = o->output_section;
10533
10534 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
10535 if (symndx >= locsymcount
10536 || (elf_bad_symtab (input_bfd)
10537 && flinfo->sections[symndx] == NULL))
10538 {
10539 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10540 while (h->root.type == bfd_link_hash_indirect
10541 || h->root.type == bfd_link_hash_warning)
10542 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10543 /* Arrange for symbol to be output. */
10544 h->indx = -2;
10545 elf_section_data (osec)->this_hdr.sh_info = -2;
10546 }
10547 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10548 {
10549 /* We'll use the output section target_index. */
10550 asection *sec = flinfo->sections[symndx]->output_section;
10551 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10552 }
10553 else
10554 {
10555 if (flinfo->indices[symndx] == -1)
10556 {
10557 /* Otherwise output the local symbol now. */
10558 Elf_Internal_Sym sym = isymbuf[symndx];
10559 asection *sec = flinfo->sections[symndx]->output_section;
10560 const char *name;
10561 long indx;
10562 int ret;
10563
10564 name = bfd_elf_string_from_elf_section (input_bfd,
10565 symtab_hdr->sh_link,
10566 sym.st_name);
10567 if (name == NULL)
10568 return FALSE;
10569
10570 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10571 sec);
10572 if (sym.st_shndx == SHN_BAD)
10573 return FALSE;
10574
10575 sym.st_value += o->output_offset;
10576
10577 indx = bfd_get_symcount (output_bfd);
10578 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10579 NULL);
10580 if (ret == 0)
10581 return FALSE;
10582 else if (ret == 1)
10583 flinfo->indices[symndx] = indx;
10584 else
10585 abort ();
10586 }
10587 elf_section_data (osec)->this_hdr.sh_info
10588 = flinfo->indices[symndx];
10589 }
10590 }
10591
10592 if ((o->flags & SEC_HAS_CONTENTS) == 0
10593 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10594 continue;
10595
10596 if ((o->flags & SEC_LINKER_CREATED) != 0)
10597 {
10598 /* Section was created by _bfd_elf_link_create_dynamic_sections
10599 or somesuch. */
10600 continue;
10601 }
10602
10603 /* Get the contents of the section. They have been cached by a
10604 relaxation routine. Note that o is a section in an input
10605 file, so the contents field will not have been set by any of
10606 the routines which work on output files. */
10607 if (elf_section_data (o)->this_hdr.contents != NULL)
10608 {
10609 contents = elf_section_data (o)->this_hdr.contents;
10610 if (bed->caches_rawsize
10611 && o->rawsize != 0
10612 && o->rawsize < o->size)
10613 {
10614 memcpy (flinfo->contents, contents, o->rawsize);
10615 contents = flinfo->contents;
10616 }
10617 }
10618 else
10619 {
10620 contents = flinfo->contents;
10621 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10622 return FALSE;
10623 }
10624
10625 if ((o->flags & SEC_RELOC) != 0)
10626 {
10627 Elf_Internal_Rela *internal_relocs;
10628 Elf_Internal_Rela *rel, *relend;
10629 int action_discarded;
10630 int ret;
10631
10632 /* Get the swapped relocs. */
10633 internal_relocs
10634 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10635 flinfo->internal_relocs, FALSE);
10636 if (internal_relocs == NULL
10637 && o->reloc_count > 0)
10638 return FALSE;
10639
10640 /* We need to reverse-copy input .ctors/.dtors sections if
10641 they are placed in .init_array/.finit_array for output. */
10642 if (o->size > address_size
10643 && ((strncmp (o->name, ".ctors", 6) == 0
10644 && strcmp (o->output_section->name,
10645 ".init_array") == 0)
10646 || (strncmp (o->name, ".dtors", 6) == 0
10647 && strcmp (o->output_section->name,
10648 ".fini_array") == 0))
10649 && (o->name[6] == 0 || o->name[6] == '.'))
10650 {
10651 if (o->size * bed->s->int_rels_per_ext_rel
10652 != o->reloc_count * address_size)
10653 {
10654 _bfd_error_handler
10655 /* xgettext:c-format */
10656 (_("error: %pB: size of section %pA is not "
10657 "multiple of address size"),
10658 input_bfd, o);
10659 bfd_set_error (bfd_error_bad_value);
10660 return FALSE;
10661 }
10662 o->flags |= SEC_ELF_REVERSE_COPY;
10663 }
10664
10665 action_discarded = -1;
10666 if (!elf_section_ignore_discarded_relocs (o))
10667 action_discarded = (*bed->action_discarded) (o);
10668
10669 /* Run through the relocs evaluating complex reloc symbols and
10670 looking for relocs against symbols from discarded sections
10671 or section symbols from removed link-once sections.
10672 Complain about relocs against discarded sections. Zero
10673 relocs against removed link-once sections. */
10674
10675 rel = internal_relocs;
10676 relend = rel + o->reloc_count;
10677 for ( ; rel < relend; rel++)
10678 {
10679 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10680 unsigned int s_type;
10681 asection **ps, *sec;
10682 struct elf_link_hash_entry *h = NULL;
10683 const char *sym_name;
10684
10685 if (r_symndx == STN_UNDEF)
10686 continue;
10687
10688 if (r_symndx >= locsymcount
10689 || (elf_bad_symtab (input_bfd)
10690 && flinfo->sections[r_symndx] == NULL))
10691 {
10692 h = sym_hashes[r_symndx - extsymoff];
10693
10694 /* Badly formatted input files can contain relocs that
10695 reference non-existant symbols. Check here so that
10696 we do not seg fault. */
10697 if (h == NULL)
10698 {
10699 _bfd_error_handler
10700 /* xgettext:c-format */
10701 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
10702 "that references a non-existent global symbol"),
10703 input_bfd, (uint64_t) rel->r_info, o);
10704 bfd_set_error (bfd_error_bad_value);
10705 return FALSE;
10706 }
10707
10708 while (h->root.type == bfd_link_hash_indirect
10709 || h->root.type == bfd_link_hash_warning)
10710 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10711
10712 s_type = h->type;
10713
10714 /* If a plugin symbol is referenced from a non-IR file,
10715 mark the symbol as undefined. Note that the
10716 linker may attach linker created dynamic sections
10717 to the plugin bfd. Symbols defined in linker
10718 created sections are not plugin symbols. */
10719 if ((h->root.non_ir_ref_regular
10720 || h->root.non_ir_ref_dynamic)
10721 && (h->root.type == bfd_link_hash_defined
10722 || h->root.type == bfd_link_hash_defweak)
10723 && (h->root.u.def.section->flags
10724 & SEC_LINKER_CREATED) == 0
10725 && h->root.u.def.section->owner != NULL
10726 && (h->root.u.def.section->owner->flags
10727 & BFD_PLUGIN) != 0)
10728 {
10729 h->root.type = bfd_link_hash_undefined;
10730 h->root.u.undef.abfd = h->root.u.def.section->owner;
10731 }
10732
10733 ps = NULL;
10734 if (h->root.type == bfd_link_hash_defined
10735 || h->root.type == bfd_link_hash_defweak)
10736 ps = &h->root.u.def.section;
10737
10738 sym_name = h->root.root.string;
10739 }
10740 else
10741 {
10742 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10743
10744 s_type = ELF_ST_TYPE (sym->st_info);
10745 ps = &flinfo->sections[r_symndx];
10746 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10747 sym, *ps);
10748 }
10749
10750 if ((s_type == STT_RELC || s_type == STT_SRELC)
10751 && !bfd_link_relocatable (flinfo->info))
10752 {
10753 bfd_vma val;
10754 bfd_vma dot = (rel->r_offset
10755 + o->output_offset + o->output_section->vma);
10756 #ifdef DEBUG
10757 printf ("Encountered a complex symbol!");
10758 printf (" (input_bfd %s, section %s, reloc %ld\n",
10759 input_bfd->filename, o->name,
10760 (long) (rel - internal_relocs));
10761 printf (" symbol: idx %8.8lx, name %s\n",
10762 r_symndx, sym_name);
10763 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10764 (unsigned long) rel->r_info,
10765 (unsigned long) rel->r_offset);
10766 #endif
10767 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10768 isymbuf, locsymcount, s_type == STT_SRELC))
10769 return FALSE;
10770
10771 /* Symbol evaluated OK. Update to absolute value. */
10772 set_symbol_value (input_bfd, isymbuf, locsymcount,
10773 r_symndx, val);
10774 continue;
10775 }
10776
10777 if (action_discarded != -1 && ps != NULL)
10778 {
10779 /* Complain if the definition comes from a
10780 discarded section. */
10781 if ((sec = *ps) != NULL && discarded_section (sec))
10782 {
10783 BFD_ASSERT (r_symndx != STN_UNDEF);
10784 if (action_discarded & COMPLAIN)
10785 (*flinfo->info->callbacks->einfo)
10786 /* xgettext:c-format */
10787 (_("%X`%s' referenced in section `%pA' of %pB: "
10788 "defined in discarded section `%pA' of %pB\n"),
10789 sym_name, o, input_bfd, sec, sec->owner);
10790
10791 /* Try to do the best we can to support buggy old
10792 versions of gcc. Pretend that the symbol is
10793 really defined in the kept linkonce section.
10794 FIXME: This is quite broken. Modifying the
10795 symbol here means we will be changing all later
10796 uses of the symbol, not just in this section. */
10797 if (action_discarded & PRETEND)
10798 {
10799 asection *kept;
10800
10801 kept = _bfd_elf_check_kept_section (sec,
10802 flinfo->info);
10803 if (kept != NULL)
10804 {
10805 *ps = kept;
10806 continue;
10807 }
10808 }
10809 }
10810 }
10811 }
10812
10813 /* Relocate the section by invoking a back end routine.
10814
10815 The back end routine is responsible for adjusting the
10816 section contents as necessary, and (if using Rela relocs
10817 and generating a relocatable output file) adjusting the
10818 reloc addend as necessary.
10819
10820 The back end routine does not have to worry about setting
10821 the reloc address or the reloc symbol index.
10822
10823 The back end routine is given a pointer to the swapped in
10824 internal symbols, and can access the hash table entries
10825 for the external symbols via elf_sym_hashes (input_bfd).
10826
10827 When generating relocatable output, the back end routine
10828 must handle STB_LOCAL/STT_SECTION symbols specially. The
10829 output symbol is going to be a section symbol
10830 corresponding to the output section, which will require
10831 the addend to be adjusted. */
10832
10833 ret = (*relocate_section) (output_bfd, flinfo->info,
10834 input_bfd, o, contents,
10835 internal_relocs,
10836 isymbuf,
10837 flinfo->sections);
10838 if (!ret)
10839 return FALSE;
10840
10841 if (ret == 2
10842 || bfd_link_relocatable (flinfo->info)
10843 || flinfo->info->emitrelocations)
10844 {
10845 Elf_Internal_Rela *irela;
10846 Elf_Internal_Rela *irelaend, *irelamid;
10847 bfd_vma last_offset;
10848 struct elf_link_hash_entry **rel_hash;
10849 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10850 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10851 unsigned int next_erel;
10852 bfd_boolean rela_normal;
10853 struct bfd_elf_section_data *esdi, *esdo;
10854
10855 esdi = elf_section_data (o);
10856 esdo = elf_section_data (o->output_section);
10857 rela_normal = FALSE;
10858
10859 /* Adjust the reloc addresses and symbol indices. */
10860
10861 irela = internal_relocs;
10862 irelaend = irela + o->reloc_count;
10863 rel_hash = esdo->rel.hashes + esdo->rel.count;
10864 /* We start processing the REL relocs, if any. When we reach
10865 IRELAMID in the loop, we switch to the RELA relocs. */
10866 irelamid = irela;
10867 if (esdi->rel.hdr != NULL)
10868 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10869 * bed->s->int_rels_per_ext_rel);
10870 rel_hash_list = rel_hash;
10871 rela_hash_list = NULL;
10872 last_offset = o->output_offset;
10873 if (!bfd_link_relocatable (flinfo->info))
10874 last_offset += o->output_section->vma;
10875 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10876 {
10877 unsigned long r_symndx;
10878 asection *sec;
10879 Elf_Internal_Sym sym;
10880
10881 if (next_erel == bed->s->int_rels_per_ext_rel)
10882 {
10883 rel_hash++;
10884 next_erel = 0;
10885 }
10886
10887 if (irela == irelamid)
10888 {
10889 rel_hash = esdo->rela.hashes + esdo->rela.count;
10890 rela_hash_list = rel_hash;
10891 rela_normal = bed->rela_normal;
10892 }
10893
10894 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10895 flinfo->info, o,
10896 irela->r_offset);
10897 if (irela->r_offset >= (bfd_vma) -2)
10898 {
10899 /* This is a reloc for a deleted entry or somesuch.
10900 Turn it into an R_*_NONE reloc, at the same
10901 offset as the last reloc. elf_eh_frame.c and
10902 bfd_elf_discard_info rely on reloc offsets
10903 being ordered. */
10904 irela->r_offset = last_offset;
10905 irela->r_info = 0;
10906 irela->r_addend = 0;
10907 continue;
10908 }
10909
10910 irela->r_offset += o->output_offset;
10911
10912 /* Relocs in an executable have to be virtual addresses. */
10913 if (!bfd_link_relocatable (flinfo->info))
10914 irela->r_offset += o->output_section->vma;
10915
10916 last_offset = irela->r_offset;
10917
10918 r_symndx = irela->r_info >> r_sym_shift;
10919 if (r_symndx == STN_UNDEF)
10920 continue;
10921
10922 if (r_symndx >= locsymcount
10923 || (elf_bad_symtab (input_bfd)
10924 && flinfo->sections[r_symndx] == NULL))
10925 {
10926 struct elf_link_hash_entry *rh;
10927 unsigned long indx;
10928
10929 /* This is a reloc against a global symbol. We
10930 have not yet output all the local symbols, so
10931 we do not know the symbol index of any global
10932 symbol. We set the rel_hash entry for this
10933 reloc to point to the global hash table entry
10934 for this symbol. The symbol index is then
10935 set at the end of bfd_elf_final_link. */
10936 indx = r_symndx - extsymoff;
10937 rh = elf_sym_hashes (input_bfd)[indx];
10938 while (rh->root.type == bfd_link_hash_indirect
10939 || rh->root.type == bfd_link_hash_warning)
10940 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10941
10942 /* Setting the index to -2 tells
10943 elf_link_output_extsym that this symbol is
10944 used by a reloc. */
10945 BFD_ASSERT (rh->indx < 0);
10946 rh->indx = -2;
10947 *rel_hash = rh;
10948
10949 continue;
10950 }
10951
10952 /* This is a reloc against a local symbol. */
10953
10954 *rel_hash = NULL;
10955 sym = isymbuf[r_symndx];
10956 sec = flinfo->sections[r_symndx];
10957 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10958 {
10959 /* I suppose the backend ought to fill in the
10960 section of any STT_SECTION symbol against a
10961 processor specific section. */
10962 r_symndx = STN_UNDEF;
10963 if (bfd_is_abs_section (sec))
10964 ;
10965 else if (sec == NULL || sec->owner == NULL)
10966 {
10967 bfd_set_error (bfd_error_bad_value);
10968 return FALSE;
10969 }
10970 else
10971 {
10972 asection *osec = sec->output_section;
10973
10974 /* If we have discarded a section, the output
10975 section will be the absolute section. In
10976 case of discarded SEC_MERGE sections, use
10977 the kept section. relocate_section should
10978 have already handled discarded linkonce
10979 sections. */
10980 if (bfd_is_abs_section (osec)
10981 && sec->kept_section != NULL
10982 && sec->kept_section->output_section != NULL)
10983 {
10984 osec = sec->kept_section->output_section;
10985 irela->r_addend -= osec->vma;
10986 }
10987
10988 if (!bfd_is_abs_section (osec))
10989 {
10990 r_symndx = osec->target_index;
10991 if (r_symndx == STN_UNDEF)
10992 {
10993 irela->r_addend += osec->vma;
10994 osec = _bfd_nearby_section (output_bfd, osec,
10995 osec->vma);
10996 irela->r_addend -= osec->vma;
10997 r_symndx = osec->target_index;
10998 }
10999 }
11000 }
11001
11002 /* Adjust the addend according to where the
11003 section winds up in the output section. */
11004 if (rela_normal)
11005 irela->r_addend += sec->output_offset;
11006 }
11007 else
11008 {
11009 if (flinfo->indices[r_symndx] == -1)
11010 {
11011 unsigned long shlink;
11012 const char *name;
11013 asection *osec;
11014 long indx;
11015
11016 if (flinfo->info->strip == strip_all)
11017 {
11018 /* You can't do ld -r -s. */
11019 bfd_set_error (bfd_error_invalid_operation);
11020 return FALSE;
11021 }
11022
11023 /* This symbol was skipped earlier, but
11024 since it is needed by a reloc, we
11025 must output it now. */
11026 shlink = symtab_hdr->sh_link;
11027 name = (bfd_elf_string_from_elf_section
11028 (input_bfd, shlink, sym.st_name));
11029 if (name == NULL)
11030 return FALSE;
11031
11032 osec = sec->output_section;
11033 sym.st_shndx =
11034 _bfd_elf_section_from_bfd_section (output_bfd,
11035 osec);
11036 if (sym.st_shndx == SHN_BAD)
11037 return FALSE;
11038
11039 sym.st_value += sec->output_offset;
11040 if (!bfd_link_relocatable (flinfo->info))
11041 {
11042 sym.st_value += osec->vma;
11043 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11044 {
11045 /* STT_TLS symbols are relative to PT_TLS
11046 segment base. */
11047 BFD_ASSERT (elf_hash_table (flinfo->info)
11048 ->tls_sec != NULL);
11049 sym.st_value -= (elf_hash_table (flinfo->info)
11050 ->tls_sec->vma);
11051 }
11052 }
11053
11054 indx = bfd_get_symcount (output_bfd);
11055 ret = elf_link_output_symstrtab (flinfo, name,
11056 &sym, sec,
11057 NULL);
11058 if (ret == 0)
11059 return FALSE;
11060 else if (ret == 1)
11061 flinfo->indices[r_symndx] = indx;
11062 else
11063 abort ();
11064 }
11065
11066 r_symndx = flinfo->indices[r_symndx];
11067 }
11068
11069 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11070 | (irela->r_info & r_type_mask));
11071 }
11072
11073 /* Swap out the relocs. */
11074 input_rel_hdr = esdi->rel.hdr;
11075 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
11076 {
11077 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11078 input_rel_hdr,
11079 internal_relocs,
11080 rel_hash_list))
11081 return FALSE;
11082 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11083 * bed->s->int_rels_per_ext_rel);
11084 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
11085 }
11086
11087 input_rela_hdr = esdi->rela.hdr;
11088 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11089 {
11090 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11091 input_rela_hdr,
11092 internal_relocs,
11093 rela_hash_list))
11094 return FALSE;
11095 }
11096 }
11097 }
11098
11099 /* Write out the modified section contents. */
11100 if (bed->elf_backend_write_section
11101 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
11102 contents))
11103 {
11104 /* Section written out. */
11105 }
11106 else switch (o->sec_info_type)
11107 {
11108 case SEC_INFO_TYPE_STABS:
11109 if (! (_bfd_write_section_stabs
11110 (output_bfd,
11111 &elf_hash_table (flinfo->info)->stab_info,
11112 o, &elf_section_data (o)->sec_info, contents)))
11113 return FALSE;
11114 break;
11115 case SEC_INFO_TYPE_MERGE:
11116 if (! _bfd_write_merged_section (output_bfd, o,
11117 elf_section_data (o)->sec_info))
11118 return FALSE;
11119 break;
11120 case SEC_INFO_TYPE_EH_FRAME:
11121 {
11122 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11123 o, contents))
11124 return FALSE;
11125 }
11126 break;
11127 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11128 {
11129 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11130 flinfo->info,
11131 o, contents))
11132 return FALSE;
11133 }
11134 break;
11135 default:
11136 {
11137 if (! (o->flags & SEC_EXCLUDE))
11138 {
11139 file_ptr offset = (file_ptr) o->output_offset;
11140 bfd_size_type todo = o->size;
11141
11142 offset *= bfd_octets_per_byte (output_bfd);
11143
11144 if ((o->flags & SEC_ELF_REVERSE_COPY))
11145 {
11146 /* Reverse-copy input section to output. */
11147 do
11148 {
11149 todo -= address_size;
11150 if (! bfd_set_section_contents (output_bfd,
11151 o->output_section,
11152 contents + todo,
11153 offset,
11154 address_size))
11155 return FALSE;
11156 if (todo == 0)
11157 break;
11158 offset += address_size;
11159 }
11160 while (1);
11161 }
11162 else if (! bfd_set_section_contents (output_bfd,
11163 o->output_section,
11164 contents,
11165 offset, todo))
11166 return FALSE;
11167 }
11168 }
11169 break;
11170 }
11171 }
11172
11173 return TRUE;
11174 }
11175
11176 /* Generate a reloc when linking an ELF file. This is a reloc
11177 requested by the linker, and does not come from any input file. This
11178 is used to build constructor and destructor tables when linking
11179 with -Ur. */
11180
11181 static bfd_boolean
11182 elf_reloc_link_order (bfd *output_bfd,
11183 struct bfd_link_info *info,
11184 asection *output_section,
11185 struct bfd_link_order *link_order)
11186 {
11187 reloc_howto_type *howto;
11188 long indx;
11189 bfd_vma offset;
11190 bfd_vma addend;
11191 struct bfd_elf_section_reloc_data *reldata;
11192 struct elf_link_hash_entry **rel_hash_ptr;
11193 Elf_Internal_Shdr *rel_hdr;
11194 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11195 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11196 bfd_byte *erel;
11197 unsigned int i;
11198 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11199
11200 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11201 if (howto == NULL)
11202 {
11203 bfd_set_error (bfd_error_bad_value);
11204 return FALSE;
11205 }
11206
11207 addend = link_order->u.reloc.p->addend;
11208
11209 if (esdo->rel.hdr)
11210 reldata = &esdo->rel;
11211 else if (esdo->rela.hdr)
11212 reldata = &esdo->rela;
11213 else
11214 {
11215 reldata = NULL;
11216 BFD_ASSERT (0);
11217 }
11218
11219 /* Figure out the symbol index. */
11220 rel_hash_ptr = reldata->hashes + reldata->count;
11221 if (link_order->type == bfd_section_reloc_link_order)
11222 {
11223 indx = link_order->u.reloc.p->u.section->target_index;
11224 BFD_ASSERT (indx != 0);
11225 *rel_hash_ptr = NULL;
11226 }
11227 else
11228 {
11229 struct elf_link_hash_entry *h;
11230
11231 /* Treat a reloc against a defined symbol as though it were
11232 actually against the section. */
11233 h = ((struct elf_link_hash_entry *)
11234 bfd_wrapped_link_hash_lookup (output_bfd, info,
11235 link_order->u.reloc.p->u.name,
11236 FALSE, FALSE, TRUE));
11237 if (h != NULL
11238 && (h->root.type == bfd_link_hash_defined
11239 || h->root.type == bfd_link_hash_defweak))
11240 {
11241 asection *section;
11242
11243 section = h->root.u.def.section;
11244 indx = section->output_section->target_index;
11245 *rel_hash_ptr = NULL;
11246 /* It seems that we ought to add the symbol value to the
11247 addend here, but in practice it has already been added
11248 because it was passed to constructor_callback. */
11249 addend += section->output_section->vma + section->output_offset;
11250 }
11251 else if (h != NULL)
11252 {
11253 /* Setting the index to -2 tells elf_link_output_extsym that
11254 this symbol is used by a reloc. */
11255 h->indx = -2;
11256 *rel_hash_ptr = h;
11257 indx = 0;
11258 }
11259 else
11260 {
11261 (*info->callbacks->unattached_reloc)
11262 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11263 indx = 0;
11264 }
11265 }
11266
11267 /* If this is an inplace reloc, we must write the addend into the
11268 object file. */
11269 if (howto->partial_inplace && addend != 0)
11270 {
11271 bfd_size_type size;
11272 bfd_reloc_status_type rstat;
11273 bfd_byte *buf;
11274 bfd_boolean ok;
11275 const char *sym_name;
11276
11277 size = (bfd_size_type) bfd_get_reloc_size (howto);
11278 buf = (bfd_byte *) bfd_zmalloc (size);
11279 if (buf == NULL && size != 0)
11280 return FALSE;
11281 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11282 switch (rstat)
11283 {
11284 case bfd_reloc_ok:
11285 break;
11286
11287 default:
11288 case bfd_reloc_outofrange:
11289 abort ();
11290
11291 case bfd_reloc_overflow:
11292 if (link_order->type == bfd_section_reloc_link_order)
11293 sym_name = bfd_section_name (output_bfd,
11294 link_order->u.reloc.p->u.section);
11295 else
11296 sym_name = link_order->u.reloc.p->u.name;
11297 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11298 howto->name, addend, NULL, NULL,
11299 (bfd_vma) 0);
11300 break;
11301 }
11302
11303 ok = bfd_set_section_contents (output_bfd, output_section, buf,
11304 link_order->offset
11305 * bfd_octets_per_byte (output_bfd),
11306 size);
11307 free (buf);
11308 if (! ok)
11309 return FALSE;
11310 }
11311
11312 /* The address of a reloc is relative to the section in a
11313 relocatable file, and is a virtual address in an executable
11314 file. */
11315 offset = link_order->offset;
11316 if (! bfd_link_relocatable (info))
11317 offset += output_section->vma;
11318
11319 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11320 {
11321 irel[i].r_offset = offset;
11322 irel[i].r_info = 0;
11323 irel[i].r_addend = 0;
11324 }
11325 if (bed->s->arch_size == 32)
11326 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11327 else
11328 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11329
11330 rel_hdr = reldata->hdr;
11331 erel = rel_hdr->contents;
11332 if (rel_hdr->sh_type == SHT_REL)
11333 {
11334 erel += reldata->count * bed->s->sizeof_rel;
11335 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11336 }
11337 else
11338 {
11339 irel[0].r_addend = addend;
11340 erel += reldata->count * bed->s->sizeof_rela;
11341 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11342 }
11343
11344 ++reldata->count;
11345
11346 return TRUE;
11347 }
11348
11349
11350 /* Get the output vma of the section pointed to by the sh_link field. */
11351
11352 static bfd_vma
11353 elf_get_linked_section_vma (struct bfd_link_order *p)
11354 {
11355 Elf_Internal_Shdr **elf_shdrp;
11356 asection *s;
11357 int elfsec;
11358
11359 s = p->u.indirect.section;
11360 elf_shdrp = elf_elfsections (s->owner);
11361 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
11362 elfsec = elf_shdrp[elfsec]->sh_link;
11363 /* PR 290:
11364 The Intel C compiler generates SHT_IA_64_UNWIND with
11365 SHF_LINK_ORDER. But it doesn't set the sh_link or
11366 sh_info fields. Hence we could get the situation
11367 where elfsec is 0. */
11368 if (elfsec == 0)
11369 {
11370 const struct elf_backend_data *bed
11371 = get_elf_backend_data (s->owner);
11372 if (bed->link_order_error_handler)
11373 bed->link_order_error_handler
11374 /* xgettext:c-format */
11375 (_("%pB: warning: sh_link not set for section `%pA'"), s->owner, s);
11376 return 0;
11377 }
11378 else
11379 {
11380 s = elf_shdrp[elfsec]->bfd_section;
11381 return s->output_section->vma + s->output_offset;
11382 }
11383 }
11384
11385
11386 /* Compare two sections based on the locations of the sections they are
11387 linked to. Used by elf_fixup_link_order. */
11388
11389 static int
11390 compare_link_order (const void * a, const void * b)
11391 {
11392 bfd_vma apos;
11393 bfd_vma bpos;
11394
11395 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11396 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11397 if (apos < bpos)
11398 return -1;
11399 return apos > bpos;
11400 }
11401
11402
11403 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11404 order as their linked sections. Returns false if this could not be done
11405 because an output section includes both ordered and unordered
11406 sections. Ideally we'd do this in the linker proper. */
11407
11408 static bfd_boolean
11409 elf_fixup_link_order (bfd *abfd, asection *o)
11410 {
11411 int seen_linkorder;
11412 int seen_other;
11413 int n;
11414 struct bfd_link_order *p;
11415 bfd *sub;
11416 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11417 unsigned elfsec;
11418 struct bfd_link_order **sections;
11419 asection *s, *other_sec, *linkorder_sec;
11420 bfd_vma offset;
11421
11422 other_sec = NULL;
11423 linkorder_sec = NULL;
11424 seen_other = 0;
11425 seen_linkorder = 0;
11426 for (p = o->map_head.link_order; p != NULL; p = p->next)
11427 {
11428 if (p->type == bfd_indirect_link_order)
11429 {
11430 s = p->u.indirect.section;
11431 sub = s->owner;
11432 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11433 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
11434 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11435 && elfsec < elf_numsections (sub)
11436 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11437 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
11438 {
11439 seen_linkorder++;
11440 linkorder_sec = s;
11441 }
11442 else
11443 {
11444 seen_other++;
11445 other_sec = s;
11446 }
11447 }
11448 else
11449 seen_other++;
11450
11451 if (seen_other && seen_linkorder)
11452 {
11453 if (other_sec && linkorder_sec)
11454 _bfd_error_handler
11455 /* xgettext:c-format */
11456 (_("%pA has both ordered [`%pA' in %pB] "
11457 "and unordered [`%pA' in %pB] sections"),
11458 o, linkorder_sec, linkorder_sec->owner,
11459 other_sec, other_sec->owner);
11460 else
11461 _bfd_error_handler
11462 (_("%pA has both ordered and unordered sections"), o);
11463 bfd_set_error (bfd_error_bad_value);
11464 return FALSE;
11465 }
11466 }
11467
11468 if (!seen_linkorder)
11469 return TRUE;
11470
11471 sections = (struct bfd_link_order **)
11472 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11473 if (sections == NULL)
11474 return FALSE;
11475 seen_linkorder = 0;
11476
11477 for (p = o->map_head.link_order; p != NULL; p = p->next)
11478 {
11479 sections[seen_linkorder++] = p;
11480 }
11481 /* Sort the input sections in the order of their linked section. */
11482 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11483 compare_link_order);
11484
11485 /* Change the offsets of the sections. */
11486 offset = 0;
11487 for (n = 0; n < seen_linkorder; n++)
11488 {
11489 s = sections[n]->u.indirect.section;
11490 offset &= ~(bfd_vma) 0 << s->alignment_power;
11491 s->output_offset = offset / bfd_octets_per_byte (abfd);
11492 sections[n]->offset = offset;
11493 offset += sections[n]->size;
11494 }
11495
11496 free (sections);
11497 return TRUE;
11498 }
11499
11500 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11501 Returns TRUE upon success, FALSE otherwise. */
11502
11503 static bfd_boolean
11504 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11505 {
11506 bfd_boolean ret = FALSE;
11507 bfd *implib_bfd;
11508 const struct elf_backend_data *bed;
11509 flagword flags;
11510 enum bfd_architecture arch;
11511 unsigned int mach;
11512 asymbol **sympp = NULL;
11513 long symsize;
11514 long symcount;
11515 long src_count;
11516 elf_symbol_type *osymbuf;
11517
11518 implib_bfd = info->out_implib_bfd;
11519 bed = get_elf_backend_data (abfd);
11520
11521 if (!bfd_set_format (implib_bfd, bfd_object))
11522 return FALSE;
11523
11524 /* Use flag from executable but make it a relocatable object. */
11525 flags = bfd_get_file_flags (abfd);
11526 flags &= ~HAS_RELOC;
11527 if (!bfd_set_start_address (implib_bfd, 0)
11528 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
11529 return FALSE;
11530
11531 /* Copy architecture of output file to import library file. */
11532 arch = bfd_get_arch (abfd);
11533 mach = bfd_get_mach (abfd);
11534 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11535 && (abfd->target_defaulted
11536 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11537 return FALSE;
11538
11539 /* Get symbol table size. */
11540 symsize = bfd_get_symtab_upper_bound (abfd);
11541 if (symsize < 0)
11542 return FALSE;
11543
11544 /* Read in the symbol table. */
11545 sympp = (asymbol **) xmalloc (symsize);
11546 symcount = bfd_canonicalize_symtab (abfd, sympp);
11547 if (symcount < 0)
11548 goto free_sym_buf;
11549
11550 /* Allow the BFD backend to copy any private header data it
11551 understands from the output BFD to the import library BFD. */
11552 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11553 goto free_sym_buf;
11554
11555 /* Filter symbols to appear in the import library. */
11556 if (bed->elf_backend_filter_implib_symbols)
11557 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11558 symcount);
11559 else
11560 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11561 if (symcount == 0)
11562 {
11563 bfd_set_error (bfd_error_no_symbols);
11564 _bfd_error_handler (_("%pB: no symbol found for import library"),
11565 implib_bfd);
11566 goto free_sym_buf;
11567 }
11568
11569
11570 /* Make symbols absolute. */
11571 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11572 sizeof (*osymbuf));
11573 for (src_count = 0; src_count < symcount; src_count++)
11574 {
11575 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11576 sizeof (*osymbuf));
11577 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11578 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11579 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11580 osymbuf[src_count].internal_elf_sym.st_value =
11581 osymbuf[src_count].symbol.value;
11582 sympp[src_count] = &osymbuf[src_count].symbol;
11583 }
11584
11585 bfd_set_symtab (implib_bfd, sympp, symcount);
11586
11587 /* Allow the BFD backend to copy any private data it understands
11588 from the output BFD to the import library BFD. This is done last
11589 to permit the routine to look at the filtered symbol table. */
11590 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11591 goto free_sym_buf;
11592
11593 if (!bfd_close (implib_bfd))
11594 goto free_sym_buf;
11595
11596 ret = TRUE;
11597
11598 free_sym_buf:
11599 free (sympp);
11600 return ret;
11601 }
11602
11603 static void
11604 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11605 {
11606 asection *o;
11607
11608 if (flinfo->symstrtab != NULL)
11609 _bfd_elf_strtab_free (flinfo->symstrtab);
11610 if (flinfo->contents != NULL)
11611 free (flinfo->contents);
11612 if (flinfo->external_relocs != NULL)
11613 free (flinfo->external_relocs);
11614 if (flinfo->internal_relocs != NULL)
11615 free (flinfo->internal_relocs);
11616 if (flinfo->external_syms != NULL)
11617 free (flinfo->external_syms);
11618 if (flinfo->locsym_shndx != NULL)
11619 free (flinfo->locsym_shndx);
11620 if (flinfo->internal_syms != NULL)
11621 free (flinfo->internal_syms);
11622 if (flinfo->indices != NULL)
11623 free (flinfo->indices);
11624 if (flinfo->sections != NULL)
11625 free (flinfo->sections);
11626 if (flinfo->symshndxbuf != NULL)
11627 free (flinfo->symshndxbuf);
11628 for (o = obfd->sections; o != NULL; o = o->next)
11629 {
11630 struct bfd_elf_section_data *esdo = elf_section_data (o);
11631 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11632 free (esdo->rel.hashes);
11633 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11634 free (esdo->rela.hashes);
11635 }
11636 }
11637
11638 /* Do the final step of an ELF link. */
11639
11640 bfd_boolean
11641 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11642 {
11643 bfd_boolean dynamic;
11644 bfd_boolean emit_relocs;
11645 bfd *dynobj;
11646 struct elf_final_link_info flinfo;
11647 asection *o;
11648 struct bfd_link_order *p;
11649 bfd *sub;
11650 bfd_size_type max_contents_size;
11651 bfd_size_type max_external_reloc_size;
11652 bfd_size_type max_internal_reloc_count;
11653 bfd_size_type max_sym_count;
11654 bfd_size_type max_sym_shndx_count;
11655 Elf_Internal_Sym elfsym;
11656 unsigned int i;
11657 Elf_Internal_Shdr *symtab_hdr;
11658 Elf_Internal_Shdr *symtab_shndx_hdr;
11659 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11660 struct elf_outext_info eoinfo;
11661 bfd_boolean merged;
11662 size_t relativecount = 0;
11663 asection *reldyn = 0;
11664 bfd_size_type amt;
11665 asection *attr_section = NULL;
11666 bfd_vma attr_size = 0;
11667 const char *std_attrs_section;
11668 struct elf_link_hash_table *htab = elf_hash_table (info);
11669
11670 if (!is_elf_hash_table (htab))
11671 return FALSE;
11672
11673 if (bfd_link_pic (info))
11674 abfd->flags |= DYNAMIC;
11675
11676 dynamic = htab->dynamic_sections_created;
11677 dynobj = htab->dynobj;
11678
11679 emit_relocs = (bfd_link_relocatable (info)
11680 || info->emitrelocations);
11681
11682 flinfo.info = info;
11683 flinfo.output_bfd = abfd;
11684 flinfo.symstrtab = _bfd_elf_strtab_init ();
11685 if (flinfo.symstrtab == NULL)
11686 return FALSE;
11687
11688 if (! dynamic)
11689 {
11690 flinfo.hash_sec = NULL;
11691 flinfo.symver_sec = NULL;
11692 }
11693 else
11694 {
11695 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11696 /* Note that dynsym_sec can be NULL (on VMS). */
11697 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11698 /* Note that it is OK if symver_sec is NULL. */
11699 }
11700
11701 flinfo.contents = NULL;
11702 flinfo.external_relocs = NULL;
11703 flinfo.internal_relocs = NULL;
11704 flinfo.external_syms = NULL;
11705 flinfo.locsym_shndx = NULL;
11706 flinfo.internal_syms = NULL;
11707 flinfo.indices = NULL;
11708 flinfo.sections = NULL;
11709 flinfo.symshndxbuf = NULL;
11710 flinfo.filesym_count = 0;
11711
11712 /* The object attributes have been merged. Remove the input
11713 sections from the link, and set the contents of the output
11714 secton. */
11715 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11716 for (o = abfd->sections; o != NULL; o = o->next)
11717 {
11718 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11719 || strcmp (o->name, ".gnu.attributes") == 0)
11720 {
11721 for (p = o->map_head.link_order; p != NULL; p = p->next)
11722 {
11723 asection *input_section;
11724
11725 if (p->type != bfd_indirect_link_order)
11726 continue;
11727 input_section = p->u.indirect.section;
11728 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11729 elf_link_input_bfd ignores this section. */
11730 input_section->flags &= ~SEC_HAS_CONTENTS;
11731 }
11732
11733 attr_size = bfd_elf_obj_attr_size (abfd);
11734 if (attr_size)
11735 {
11736 bfd_set_section_size (abfd, o, attr_size);
11737 attr_section = o;
11738 /* Skip this section later on. */
11739 o->map_head.link_order = NULL;
11740 }
11741 else
11742 o->flags |= SEC_EXCLUDE;
11743 }
11744 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
11745 {
11746 /* Remove empty group section from linker output. */
11747 o->flags |= SEC_EXCLUDE;
11748 bfd_section_list_remove (abfd, o);
11749 abfd->section_count--;
11750 }
11751 }
11752
11753 /* Count up the number of relocations we will output for each output
11754 section, so that we know the sizes of the reloc sections. We
11755 also figure out some maximum sizes. */
11756 max_contents_size = 0;
11757 max_external_reloc_size = 0;
11758 max_internal_reloc_count = 0;
11759 max_sym_count = 0;
11760 max_sym_shndx_count = 0;
11761 merged = FALSE;
11762 for (o = abfd->sections; o != NULL; o = o->next)
11763 {
11764 struct bfd_elf_section_data *esdo = elf_section_data (o);
11765 o->reloc_count = 0;
11766
11767 for (p = o->map_head.link_order; p != NULL; p = p->next)
11768 {
11769 unsigned int reloc_count = 0;
11770 unsigned int additional_reloc_count = 0;
11771 struct bfd_elf_section_data *esdi = NULL;
11772
11773 if (p->type == bfd_section_reloc_link_order
11774 || p->type == bfd_symbol_reloc_link_order)
11775 reloc_count = 1;
11776 else if (p->type == bfd_indirect_link_order)
11777 {
11778 asection *sec;
11779
11780 sec = p->u.indirect.section;
11781
11782 /* Mark all sections which are to be included in the
11783 link. This will normally be every section. We need
11784 to do this so that we can identify any sections which
11785 the linker has decided to not include. */
11786 sec->linker_mark = TRUE;
11787
11788 if (sec->flags & SEC_MERGE)
11789 merged = TRUE;
11790
11791 if (sec->rawsize > max_contents_size)
11792 max_contents_size = sec->rawsize;
11793 if (sec->size > max_contents_size)
11794 max_contents_size = sec->size;
11795
11796 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11797 && (sec->owner->flags & DYNAMIC) == 0)
11798 {
11799 size_t sym_count;
11800
11801 /* We are interested in just local symbols, not all
11802 symbols. */
11803 if (elf_bad_symtab (sec->owner))
11804 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11805 / bed->s->sizeof_sym);
11806 else
11807 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11808
11809 if (sym_count > max_sym_count)
11810 max_sym_count = sym_count;
11811
11812 if (sym_count > max_sym_shndx_count
11813 && elf_symtab_shndx_list (sec->owner) != NULL)
11814 max_sym_shndx_count = sym_count;
11815
11816 if (esdo->this_hdr.sh_type == SHT_REL
11817 || esdo->this_hdr.sh_type == SHT_RELA)
11818 /* Some backends use reloc_count in relocation sections
11819 to count particular types of relocs. Of course,
11820 reloc sections themselves can't have relocations. */
11821 ;
11822 else if (emit_relocs)
11823 {
11824 reloc_count = sec->reloc_count;
11825 if (bed->elf_backend_count_additional_relocs)
11826 {
11827 int c;
11828 c = (*bed->elf_backend_count_additional_relocs) (sec);
11829 additional_reloc_count += c;
11830 }
11831 }
11832 else if (bed->elf_backend_count_relocs)
11833 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11834
11835 esdi = elf_section_data (sec);
11836
11837 if ((sec->flags & SEC_RELOC) != 0)
11838 {
11839 size_t ext_size = 0;
11840
11841 if (esdi->rel.hdr != NULL)
11842 ext_size = esdi->rel.hdr->sh_size;
11843 if (esdi->rela.hdr != NULL)
11844 ext_size += esdi->rela.hdr->sh_size;
11845
11846 if (ext_size > max_external_reloc_size)
11847 max_external_reloc_size = ext_size;
11848 if (sec->reloc_count > max_internal_reloc_count)
11849 max_internal_reloc_count = sec->reloc_count;
11850 }
11851 }
11852 }
11853
11854 if (reloc_count == 0)
11855 continue;
11856
11857 reloc_count += additional_reloc_count;
11858 o->reloc_count += reloc_count;
11859
11860 if (p->type == bfd_indirect_link_order && emit_relocs)
11861 {
11862 if (esdi->rel.hdr)
11863 {
11864 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11865 esdo->rel.count += additional_reloc_count;
11866 }
11867 if (esdi->rela.hdr)
11868 {
11869 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11870 esdo->rela.count += additional_reloc_count;
11871 }
11872 }
11873 else
11874 {
11875 if (o->use_rela_p)
11876 esdo->rela.count += reloc_count;
11877 else
11878 esdo->rel.count += reloc_count;
11879 }
11880 }
11881
11882 if (o->reloc_count > 0)
11883 o->flags |= SEC_RELOC;
11884 else
11885 {
11886 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11887 set it (this is probably a bug) and if it is set
11888 assign_section_numbers will create a reloc section. */
11889 o->flags &=~ SEC_RELOC;
11890 }
11891
11892 /* If the SEC_ALLOC flag is not set, force the section VMA to
11893 zero. This is done in elf_fake_sections as well, but forcing
11894 the VMA to 0 here will ensure that relocs against these
11895 sections are handled correctly. */
11896 if ((o->flags & SEC_ALLOC) == 0
11897 && ! o->user_set_vma)
11898 o->vma = 0;
11899 }
11900
11901 if (! bfd_link_relocatable (info) && merged)
11902 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
11903
11904 /* Figure out the file positions for everything but the symbol table
11905 and the relocs. We set symcount to force assign_section_numbers
11906 to create a symbol table. */
11907 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
11908 BFD_ASSERT (! abfd->output_has_begun);
11909 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11910 goto error_return;
11911
11912 /* Set sizes, and assign file positions for reloc sections. */
11913 for (o = abfd->sections; o != NULL; o = o->next)
11914 {
11915 struct bfd_elf_section_data *esdo = elf_section_data (o);
11916 if ((o->flags & SEC_RELOC) != 0)
11917 {
11918 if (esdo->rel.hdr
11919 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
11920 goto error_return;
11921
11922 if (esdo->rela.hdr
11923 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
11924 goto error_return;
11925 }
11926
11927 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11928 to count upwards while actually outputting the relocations. */
11929 esdo->rel.count = 0;
11930 esdo->rela.count = 0;
11931
11932 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11933 {
11934 /* Cache the section contents so that they can be compressed
11935 later. Use bfd_malloc since it will be freed by
11936 bfd_compress_section_contents. */
11937 unsigned char *contents = esdo->this_hdr.contents;
11938 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11939 abort ();
11940 contents
11941 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11942 if (contents == NULL)
11943 goto error_return;
11944 esdo->this_hdr.contents = contents;
11945 }
11946 }
11947
11948 /* We have now assigned file positions for all the sections except
11949 .symtab, .strtab, and non-loaded reloc sections. We start the
11950 .symtab section at the current file position, and write directly
11951 to it. We build the .strtab section in memory. */
11952 bfd_get_symcount (abfd) = 0;
11953 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11954 /* sh_name is set in prep_headers. */
11955 symtab_hdr->sh_type = SHT_SYMTAB;
11956 /* sh_flags, sh_addr and sh_size all start off zero. */
11957 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11958 /* sh_link is set in assign_section_numbers. */
11959 /* sh_info is set below. */
11960 /* sh_offset is set just below. */
11961 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11962
11963 if (max_sym_count < 20)
11964 max_sym_count = 20;
11965 htab->strtabsize = max_sym_count;
11966 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11967 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
11968 if (htab->strtab == NULL)
11969 goto error_return;
11970 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11971 flinfo.symshndxbuf
11972 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11973 ? (Elf_External_Sym_Shndx *) -1 : NULL);
11974
11975 if (info->strip != strip_all || emit_relocs)
11976 {
11977 file_ptr off = elf_next_file_pos (abfd);
11978
11979 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11980
11981 /* Note that at this point elf_next_file_pos (abfd) is
11982 incorrect. We do not yet know the size of the .symtab section.
11983 We correct next_file_pos below, after we do know the size. */
11984
11985 /* Start writing out the symbol table. The first symbol is always a
11986 dummy symbol. */
11987 elfsym.st_value = 0;
11988 elfsym.st_size = 0;
11989 elfsym.st_info = 0;
11990 elfsym.st_other = 0;
11991 elfsym.st_shndx = SHN_UNDEF;
11992 elfsym.st_target_internal = 0;
11993 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11994 bfd_und_section_ptr, NULL) != 1)
11995 goto error_return;
11996
11997 /* Output a symbol for each section. We output these even if we are
11998 discarding local symbols, since they are used for relocs. These
11999 symbols have no names. We store the index of each one in the
12000 index field of the section, so that we can find it again when
12001 outputting relocs. */
12002
12003 elfsym.st_size = 0;
12004 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12005 elfsym.st_other = 0;
12006 elfsym.st_value = 0;
12007 elfsym.st_target_internal = 0;
12008 for (i = 1; i < elf_numsections (abfd); i++)
12009 {
12010 o = bfd_section_from_elf_index (abfd, i);
12011 if (o != NULL)
12012 {
12013 o->target_index = bfd_get_symcount (abfd);
12014 elfsym.st_shndx = i;
12015 if (!bfd_link_relocatable (info))
12016 elfsym.st_value = o->vma;
12017 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
12018 NULL) != 1)
12019 goto error_return;
12020 }
12021 }
12022 }
12023
12024 /* Allocate some memory to hold information read in from the input
12025 files. */
12026 if (max_contents_size != 0)
12027 {
12028 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12029 if (flinfo.contents == NULL)
12030 goto error_return;
12031 }
12032
12033 if (max_external_reloc_size != 0)
12034 {
12035 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12036 if (flinfo.external_relocs == NULL)
12037 goto error_return;
12038 }
12039
12040 if (max_internal_reloc_count != 0)
12041 {
12042 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12043 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12044 if (flinfo.internal_relocs == NULL)
12045 goto error_return;
12046 }
12047
12048 if (max_sym_count != 0)
12049 {
12050 amt = max_sym_count * bed->s->sizeof_sym;
12051 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12052 if (flinfo.external_syms == NULL)
12053 goto error_return;
12054
12055 amt = max_sym_count * sizeof (Elf_Internal_Sym);
12056 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12057 if (flinfo.internal_syms == NULL)
12058 goto error_return;
12059
12060 amt = max_sym_count * sizeof (long);
12061 flinfo.indices = (long int *) bfd_malloc (amt);
12062 if (flinfo.indices == NULL)
12063 goto error_return;
12064
12065 amt = max_sym_count * sizeof (asection *);
12066 flinfo.sections = (asection **) bfd_malloc (amt);
12067 if (flinfo.sections == NULL)
12068 goto error_return;
12069 }
12070
12071 if (max_sym_shndx_count != 0)
12072 {
12073 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
12074 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12075 if (flinfo.locsym_shndx == NULL)
12076 goto error_return;
12077 }
12078
12079 if (htab->tls_sec)
12080 {
12081 bfd_vma base, end = 0;
12082 asection *sec;
12083
12084 for (sec = htab->tls_sec;
12085 sec && (sec->flags & SEC_THREAD_LOCAL);
12086 sec = sec->next)
12087 {
12088 bfd_size_type size = sec->size;
12089
12090 if (size == 0
12091 && (sec->flags & SEC_HAS_CONTENTS) == 0)
12092 {
12093 struct bfd_link_order *ord = sec->map_tail.link_order;
12094
12095 if (ord != NULL)
12096 size = ord->offset + ord->size;
12097 }
12098 end = sec->vma + size;
12099 }
12100 base = htab->tls_sec->vma;
12101 /* Only align end of TLS section if static TLS doesn't have special
12102 alignment requirements. */
12103 if (bed->static_tls_alignment == 1)
12104 end = align_power (end, htab->tls_sec->alignment_power);
12105 htab->tls_size = end - base;
12106 }
12107
12108 /* Reorder SHF_LINK_ORDER sections. */
12109 for (o = abfd->sections; o != NULL; o = o->next)
12110 {
12111 if (!elf_fixup_link_order (abfd, o))
12112 return FALSE;
12113 }
12114
12115 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12116 return FALSE;
12117
12118 /* Since ELF permits relocations to be against local symbols, we
12119 must have the local symbols available when we do the relocations.
12120 Since we would rather only read the local symbols once, and we
12121 would rather not keep them in memory, we handle all the
12122 relocations for a single input file at the same time.
12123
12124 Unfortunately, there is no way to know the total number of local
12125 symbols until we have seen all of them, and the local symbol
12126 indices precede the global symbol indices. This means that when
12127 we are generating relocatable output, and we see a reloc against
12128 a global symbol, we can not know the symbol index until we have
12129 finished examining all the local symbols to see which ones we are
12130 going to output. To deal with this, we keep the relocations in
12131 memory, and don't output them until the end of the link. This is
12132 an unfortunate waste of memory, but I don't see a good way around
12133 it. Fortunately, it only happens when performing a relocatable
12134 link, which is not the common case. FIXME: If keep_memory is set
12135 we could write the relocs out and then read them again; I don't
12136 know how bad the memory loss will be. */
12137
12138 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12139 sub->output_has_begun = FALSE;
12140 for (o = abfd->sections; o != NULL; o = o->next)
12141 {
12142 for (p = o->map_head.link_order; p != NULL; p = p->next)
12143 {
12144 if (p->type == bfd_indirect_link_order
12145 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12146 == bfd_target_elf_flavour)
12147 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12148 {
12149 if (! sub->output_has_begun)
12150 {
12151 if (! elf_link_input_bfd (&flinfo, sub))
12152 goto error_return;
12153 sub->output_has_begun = TRUE;
12154 }
12155 }
12156 else if (p->type == bfd_section_reloc_link_order
12157 || p->type == bfd_symbol_reloc_link_order)
12158 {
12159 if (! elf_reloc_link_order (abfd, info, o, p))
12160 goto error_return;
12161 }
12162 else
12163 {
12164 if (! _bfd_default_link_order (abfd, info, o, p))
12165 {
12166 if (p->type == bfd_indirect_link_order
12167 && (bfd_get_flavour (sub)
12168 == bfd_target_elf_flavour)
12169 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12170 != bed->s->elfclass))
12171 {
12172 const char *iclass, *oclass;
12173
12174 switch (bed->s->elfclass)
12175 {
12176 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12177 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12178 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12179 default: abort ();
12180 }
12181
12182 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12183 {
12184 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12185 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12186 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12187 default: abort ();
12188 }
12189
12190 bfd_set_error (bfd_error_wrong_format);
12191 _bfd_error_handler
12192 /* xgettext:c-format */
12193 (_("%pB: file class %s incompatible with %s"),
12194 sub, iclass, oclass);
12195 }
12196
12197 goto error_return;
12198 }
12199 }
12200 }
12201 }
12202
12203 /* Free symbol buffer if needed. */
12204 if (!info->reduce_memory_overheads)
12205 {
12206 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12207 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
12208 && elf_tdata (sub)->symbuf)
12209 {
12210 free (elf_tdata (sub)->symbuf);
12211 elf_tdata (sub)->symbuf = NULL;
12212 }
12213 }
12214
12215 /* Output any global symbols that got converted to local in a
12216 version script or due to symbol visibility. We do this in a
12217 separate step since ELF requires all local symbols to appear
12218 prior to any global symbols. FIXME: We should only do this if
12219 some global symbols were, in fact, converted to become local.
12220 FIXME: Will this work correctly with the Irix 5 linker? */
12221 eoinfo.failed = FALSE;
12222 eoinfo.flinfo = &flinfo;
12223 eoinfo.localsyms = TRUE;
12224 eoinfo.file_sym_done = FALSE;
12225 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12226 if (eoinfo.failed)
12227 return FALSE;
12228
12229 /* If backend needs to output some local symbols not present in the hash
12230 table, do it now. */
12231 if (bed->elf_backend_output_arch_local_syms
12232 && (info->strip != strip_all || emit_relocs))
12233 {
12234 typedef int (*out_sym_func)
12235 (void *, const char *, Elf_Internal_Sym *, asection *,
12236 struct elf_link_hash_entry *);
12237
12238 if (! ((*bed->elf_backend_output_arch_local_syms)
12239 (abfd, info, &flinfo,
12240 (out_sym_func) elf_link_output_symstrtab)))
12241 return FALSE;
12242 }
12243
12244 /* That wrote out all the local symbols. Finish up the symbol table
12245 with the global symbols. Even if we want to strip everything we
12246 can, we still need to deal with those global symbols that got
12247 converted to local in a version script. */
12248
12249 /* The sh_info field records the index of the first non local symbol. */
12250 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12251
12252 if (dynamic
12253 && htab->dynsym != NULL
12254 && htab->dynsym->output_section != bfd_abs_section_ptr)
12255 {
12256 Elf_Internal_Sym sym;
12257 bfd_byte *dynsym = htab->dynsym->contents;
12258
12259 o = htab->dynsym->output_section;
12260 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12261
12262 /* Write out the section symbols for the output sections. */
12263 if (bfd_link_pic (info)
12264 || htab->is_relocatable_executable)
12265 {
12266 asection *s;
12267
12268 sym.st_size = 0;
12269 sym.st_name = 0;
12270 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12271 sym.st_other = 0;
12272 sym.st_target_internal = 0;
12273
12274 for (s = abfd->sections; s != NULL; s = s->next)
12275 {
12276 int indx;
12277 bfd_byte *dest;
12278 long dynindx;
12279
12280 dynindx = elf_section_data (s)->dynindx;
12281 if (dynindx <= 0)
12282 continue;
12283 indx = elf_section_data (s)->this_idx;
12284 BFD_ASSERT (indx > 0);
12285 sym.st_shndx = indx;
12286 if (! check_dynsym (abfd, &sym))
12287 return FALSE;
12288 sym.st_value = s->vma;
12289 dest = dynsym + dynindx * bed->s->sizeof_sym;
12290 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12291 }
12292 }
12293
12294 /* Write out the local dynsyms. */
12295 if (htab->dynlocal)
12296 {
12297 struct elf_link_local_dynamic_entry *e;
12298 for (e = htab->dynlocal; e ; e = e->next)
12299 {
12300 asection *s;
12301 bfd_byte *dest;
12302
12303 /* Copy the internal symbol and turn off visibility.
12304 Note that we saved a word of storage and overwrote
12305 the original st_name with the dynstr_index. */
12306 sym = e->isym;
12307 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12308
12309 s = bfd_section_from_elf_index (e->input_bfd,
12310 e->isym.st_shndx);
12311 if (s != NULL)
12312 {
12313 sym.st_shndx =
12314 elf_section_data (s->output_section)->this_idx;
12315 if (! check_dynsym (abfd, &sym))
12316 return FALSE;
12317 sym.st_value = (s->output_section->vma
12318 + s->output_offset
12319 + e->isym.st_value);
12320 }
12321
12322 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12323 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12324 }
12325 }
12326 }
12327
12328 /* We get the global symbols from the hash table. */
12329 eoinfo.failed = FALSE;
12330 eoinfo.localsyms = FALSE;
12331 eoinfo.flinfo = &flinfo;
12332 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12333 if (eoinfo.failed)
12334 return FALSE;
12335
12336 /* If backend needs to output some symbols not present in the hash
12337 table, do it now. */
12338 if (bed->elf_backend_output_arch_syms
12339 && (info->strip != strip_all || emit_relocs))
12340 {
12341 typedef int (*out_sym_func)
12342 (void *, const char *, Elf_Internal_Sym *, asection *,
12343 struct elf_link_hash_entry *);
12344
12345 if (! ((*bed->elf_backend_output_arch_syms)
12346 (abfd, info, &flinfo,
12347 (out_sym_func) elf_link_output_symstrtab)))
12348 return FALSE;
12349 }
12350
12351 /* Finalize the .strtab section. */
12352 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12353
12354 /* Swap out the .strtab section. */
12355 if (!elf_link_swap_symbols_out (&flinfo))
12356 return FALSE;
12357
12358 /* Now we know the size of the symtab section. */
12359 if (bfd_get_symcount (abfd) > 0)
12360 {
12361 /* Finish up and write out the symbol string table (.strtab)
12362 section. */
12363 Elf_Internal_Shdr *symstrtab_hdr = NULL;
12364 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12365
12366 if (elf_symtab_shndx_list (abfd))
12367 {
12368 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12369
12370 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12371 {
12372 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12373 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12374 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12375 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12376 symtab_shndx_hdr->sh_size = amt;
12377
12378 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12379 off, TRUE);
12380
12381 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12382 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12383 return FALSE;
12384 }
12385 }
12386
12387 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12388 /* sh_name was set in prep_headers. */
12389 symstrtab_hdr->sh_type = SHT_STRTAB;
12390 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12391 symstrtab_hdr->sh_addr = 0;
12392 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12393 symstrtab_hdr->sh_entsize = 0;
12394 symstrtab_hdr->sh_link = 0;
12395 symstrtab_hdr->sh_info = 0;
12396 /* sh_offset is set just below. */
12397 symstrtab_hdr->sh_addralign = 1;
12398
12399 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12400 off, TRUE);
12401 elf_next_file_pos (abfd) = off;
12402
12403 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12404 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12405 return FALSE;
12406 }
12407
12408 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12409 {
12410 _bfd_error_handler (_("%pB: failed to generate import library"),
12411 info->out_implib_bfd);
12412 return FALSE;
12413 }
12414
12415 /* Adjust the relocs to have the correct symbol indices. */
12416 for (o = abfd->sections; o != NULL; o = o->next)
12417 {
12418 struct bfd_elf_section_data *esdo = elf_section_data (o);
12419 bfd_boolean sort;
12420
12421 if ((o->flags & SEC_RELOC) == 0)
12422 continue;
12423
12424 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12425 if (esdo->rel.hdr != NULL
12426 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
12427 return FALSE;
12428 if (esdo->rela.hdr != NULL
12429 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
12430 return FALSE;
12431
12432 /* Set the reloc_count field to 0 to prevent write_relocs from
12433 trying to swap the relocs out itself. */
12434 o->reloc_count = 0;
12435 }
12436
12437 if (dynamic && info->combreloc && dynobj != NULL)
12438 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12439
12440 /* If we are linking against a dynamic object, or generating a
12441 shared library, finish up the dynamic linking information. */
12442 if (dynamic)
12443 {
12444 bfd_byte *dyncon, *dynconend;
12445
12446 /* Fix up .dynamic entries. */
12447 o = bfd_get_linker_section (dynobj, ".dynamic");
12448 BFD_ASSERT (o != NULL);
12449
12450 dyncon = o->contents;
12451 dynconend = o->contents + o->size;
12452 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12453 {
12454 Elf_Internal_Dyn dyn;
12455 const char *name;
12456 unsigned int type;
12457 bfd_size_type sh_size;
12458 bfd_vma sh_addr;
12459
12460 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12461
12462 switch (dyn.d_tag)
12463 {
12464 default:
12465 continue;
12466 case DT_NULL:
12467 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12468 {
12469 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12470 {
12471 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12472 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12473 default: continue;
12474 }
12475 dyn.d_un.d_val = relativecount;
12476 relativecount = 0;
12477 break;
12478 }
12479 continue;
12480
12481 case DT_INIT:
12482 name = info->init_function;
12483 goto get_sym;
12484 case DT_FINI:
12485 name = info->fini_function;
12486 get_sym:
12487 {
12488 struct elf_link_hash_entry *h;
12489
12490 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
12491 if (h != NULL
12492 && (h->root.type == bfd_link_hash_defined
12493 || h->root.type == bfd_link_hash_defweak))
12494 {
12495 dyn.d_un.d_ptr = h->root.u.def.value;
12496 o = h->root.u.def.section;
12497 if (o->output_section != NULL)
12498 dyn.d_un.d_ptr += (o->output_section->vma
12499 + o->output_offset);
12500 else
12501 {
12502 /* The symbol is imported from another shared
12503 library and does not apply to this one. */
12504 dyn.d_un.d_ptr = 0;
12505 }
12506 break;
12507 }
12508 }
12509 continue;
12510
12511 case DT_PREINIT_ARRAYSZ:
12512 name = ".preinit_array";
12513 goto get_out_size;
12514 case DT_INIT_ARRAYSZ:
12515 name = ".init_array";
12516 goto get_out_size;
12517 case DT_FINI_ARRAYSZ:
12518 name = ".fini_array";
12519 get_out_size:
12520 o = bfd_get_section_by_name (abfd, name);
12521 if (o == NULL)
12522 {
12523 _bfd_error_handler
12524 (_("could not find section %s"), name);
12525 goto error_return;
12526 }
12527 if (o->size == 0)
12528 _bfd_error_handler
12529 (_("warning: %s section has zero size"), name);
12530 dyn.d_un.d_val = o->size;
12531 break;
12532
12533 case DT_PREINIT_ARRAY:
12534 name = ".preinit_array";
12535 goto get_out_vma;
12536 case DT_INIT_ARRAY:
12537 name = ".init_array";
12538 goto get_out_vma;
12539 case DT_FINI_ARRAY:
12540 name = ".fini_array";
12541 get_out_vma:
12542 o = bfd_get_section_by_name (abfd, name);
12543 goto do_vma;
12544
12545 case DT_HASH:
12546 name = ".hash";
12547 goto get_vma;
12548 case DT_GNU_HASH:
12549 name = ".gnu.hash";
12550 goto get_vma;
12551 case DT_STRTAB:
12552 name = ".dynstr";
12553 goto get_vma;
12554 case DT_SYMTAB:
12555 name = ".dynsym";
12556 goto get_vma;
12557 case DT_VERDEF:
12558 name = ".gnu.version_d";
12559 goto get_vma;
12560 case DT_VERNEED:
12561 name = ".gnu.version_r";
12562 goto get_vma;
12563 case DT_VERSYM:
12564 name = ".gnu.version";
12565 get_vma:
12566 o = bfd_get_linker_section (dynobj, name);
12567 do_vma:
12568 if (o == NULL || bfd_is_abs_section (o->output_section))
12569 {
12570 _bfd_error_handler
12571 (_("could not find section %s"), name);
12572 goto error_return;
12573 }
12574 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12575 {
12576 _bfd_error_handler
12577 (_("warning: section '%s' is being made into a note"), name);
12578 bfd_set_error (bfd_error_nonrepresentable_section);
12579 goto error_return;
12580 }
12581 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
12582 break;
12583
12584 case DT_REL:
12585 case DT_RELA:
12586 case DT_RELSZ:
12587 case DT_RELASZ:
12588 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12589 type = SHT_REL;
12590 else
12591 type = SHT_RELA;
12592 sh_size = 0;
12593 sh_addr = 0;
12594 for (i = 1; i < elf_numsections (abfd); i++)
12595 {
12596 Elf_Internal_Shdr *hdr;
12597
12598 hdr = elf_elfsections (abfd)[i];
12599 if (hdr->sh_type == type
12600 && (hdr->sh_flags & SHF_ALLOC) != 0)
12601 {
12602 sh_size += hdr->sh_size;
12603 if (sh_addr == 0
12604 || sh_addr > hdr->sh_addr)
12605 sh_addr = hdr->sh_addr;
12606 }
12607 }
12608
12609 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12610 {
12611 /* Don't count procedure linkage table relocs in the
12612 overall reloc count. */
12613 sh_size -= htab->srelplt->size;
12614 if (sh_size == 0)
12615 /* If the size is zero, make the address zero too.
12616 This is to avoid a glibc bug. If the backend
12617 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12618 zero, then we'll put DT_RELA at the end of
12619 DT_JMPREL. glibc will interpret the end of
12620 DT_RELA matching the end of DT_JMPREL as the
12621 case where DT_RELA includes DT_JMPREL, and for
12622 LD_BIND_NOW will decide that processing DT_RELA
12623 will process the PLT relocs too. Net result:
12624 No PLT relocs applied. */
12625 sh_addr = 0;
12626
12627 /* If .rela.plt is the first .rela section, exclude
12628 it from DT_RELA. */
12629 else if (sh_addr == (htab->srelplt->output_section->vma
12630 + htab->srelplt->output_offset))
12631 sh_addr += htab->srelplt->size;
12632 }
12633
12634 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12635 dyn.d_un.d_val = sh_size;
12636 else
12637 dyn.d_un.d_ptr = sh_addr;
12638 break;
12639 }
12640 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12641 }
12642 }
12643
12644 /* If we have created any dynamic sections, then output them. */
12645 if (dynobj != NULL)
12646 {
12647 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12648 goto error_return;
12649
12650 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12651 if (((info->warn_shared_textrel && bfd_link_pic (info))
12652 || info->error_textrel)
12653 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
12654 {
12655 bfd_byte *dyncon, *dynconend;
12656
12657 dyncon = o->contents;
12658 dynconend = o->contents + o->size;
12659 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12660 {
12661 Elf_Internal_Dyn dyn;
12662
12663 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12664
12665 if (dyn.d_tag == DT_TEXTREL)
12666 {
12667 if (info->error_textrel)
12668 info->callbacks->einfo
12669 (_("%P%X: read-only segment has dynamic relocations\n"));
12670 else
12671 info->callbacks->einfo
12672 (_("%P: warning: creating a DT_TEXTREL in a shared object\n"));
12673 break;
12674 }
12675 }
12676 }
12677
12678 for (o = dynobj->sections; o != NULL; o = o->next)
12679 {
12680 if ((o->flags & SEC_HAS_CONTENTS) == 0
12681 || o->size == 0
12682 || o->output_section == bfd_abs_section_ptr)
12683 continue;
12684 if ((o->flags & SEC_LINKER_CREATED) == 0)
12685 {
12686 /* At this point, we are only interested in sections
12687 created by _bfd_elf_link_create_dynamic_sections. */
12688 continue;
12689 }
12690 if (htab->stab_info.stabstr == o)
12691 continue;
12692 if (htab->eh_info.hdr_sec == o)
12693 continue;
12694 if (strcmp (o->name, ".dynstr") != 0)
12695 {
12696 if (! bfd_set_section_contents (abfd, o->output_section,
12697 o->contents,
12698 (file_ptr) o->output_offset
12699 * bfd_octets_per_byte (abfd),
12700 o->size))
12701 goto error_return;
12702 }
12703 else
12704 {
12705 /* The contents of the .dynstr section are actually in a
12706 stringtab. */
12707 file_ptr off;
12708
12709 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12710 if (bfd_seek (abfd, off, SEEK_SET) != 0
12711 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
12712 goto error_return;
12713 }
12714 }
12715 }
12716
12717 if (!info->resolve_section_groups)
12718 {
12719 bfd_boolean failed = FALSE;
12720
12721 BFD_ASSERT (bfd_link_relocatable (info));
12722 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12723 if (failed)
12724 goto error_return;
12725 }
12726
12727 /* If we have optimized stabs strings, output them. */
12728 if (htab->stab_info.stabstr != NULL)
12729 {
12730 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
12731 goto error_return;
12732 }
12733
12734 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12735 goto error_return;
12736
12737 elf_final_link_free (abfd, &flinfo);
12738
12739 elf_linker (abfd) = TRUE;
12740
12741 if (attr_section)
12742 {
12743 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
12744 if (contents == NULL)
12745 return FALSE; /* Bail out and fail. */
12746 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12747 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12748 free (contents);
12749 }
12750
12751 return TRUE;
12752
12753 error_return:
12754 elf_final_link_free (abfd, &flinfo);
12755 return FALSE;
12756 }
12757 \f
12758 /* Initialize COOKIE for input bfd ABFD. */
12759
12760 static bfd_boolean
12761 init_reloc_cookie (struct elf_reloc_cookie *cookie,
12762 struct bfd_link_info *info, bfd *abfd)
12763 {
12764 Elf_Internal_Shdr *symtab_hdr;
12765 const struct elf_backend_data *bed;
12766
12767 bed = get_elf_backend_data (abfd);
12768 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12769
12770 cookie->abfd = abfd;
12771 cookie->sym_hashes = elf_sym_hashes (abfd);
12772 cookie->bad_symtab = elf_bad_symtab (abfd);
12773 if (cookie->bad_symtab)
12774 {
12775 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12776 cookie->extsymoff = 0;
12777 }
12778 else
12779 {
12780 cookie->locsymcount = symtab_hdr->sh_info;
12781 cookie->extsymoff = symtab_hdr->sh_info;
12782 }
12783
12784 if (bed->s->arch_size == 32)
12785 cookie->r_sym_shift = 8;
12786 else
12787 cookie->r_sym_shift = 32;
12788
12789 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12790 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12791 {
12792 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12793 cookie->locsymcount, 0,
12794 NULL, NULL, NULL);
12795 if (cookie->locsyms == NULL)
12796 {
12797 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12798 return FALSE;
12799 }
12800 if (info->keep_memory)
12801 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12802 }
12803 return TRUE;
12804 }
12805
12806 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12807
12808 static void
12809 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12810 {
12811 Elf_Internal_Shdr *symtab_hdr;
12812
12813 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12814 if (cookie->locsyms != NULL
12815 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12816 free (cookie->locsyms);
12817 }
12818
12819 /* Initialize the relocation information in COOKIE for input section SEC
12820 of input bfd ABFD. */
12821
12822 static bfd_boolean
12823 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12824 struct bfd_link_info *info, bfd *abfd,
12825 asection *sec)
12826 {
12827 if (sec->reloc_count == 0)
12828 {
12829 cookie->rels = NULL;
12830 cookie->relend = NULL;
12831 }
12832 else
12833 {
12834 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12835 info->keep_memory);
12836 if (cookie->rels == NULL)
12837 return FALSE;
12838 cookie->rel = cookie->rels;
12839 cookie->relend = cookie->rels + sec->reloc_count;
12840 }
12841 cookie->rel = cookie->rels;
12842 return TRUE;
12843 }
12844
12845 /* Free the memory allocated by init_reloc_cookie_rels,
12846 if appropriate. */
12847
12848 static void
12849 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12850 asection *sec)
12851 {
12852 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12853 free (cookie->rels);
12854 }
12855
12856 /* Initialize the whole of COOKIE for input section SEC. */
12857
12858 static bfd_boolean
12859 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12860 struct bfd_link_info *info,
12861 asection *sec)
12862 {
12863 if (!init_reloc_cookie (cookie, info, sec->owner))
12864 goto error1;
12865 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12866 goto error2;
12867 return TRUE;
12868
12869 error2:
12870 fini_reloc_cookie (cookie, sec->owner);
12871 error1:
12872 return FALSE;
12873 }
12874
12875 /* Free the memory allocated by init_reloc_cookie_for_section,
12876 if appropriate. */
12877
12878 static void
12879 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12880 asection *sec)
12881 {
12882 fini_reloc_cookie_rels (cookie, sec);
12883 fini_reloc_cookie (cookie, sec->owner);
12884 }
12885 \f
12886 /* Garbage collect unused sections. */
12887
12888 /* Default gc_mark_hook. */
12889
12890 asection *
12891 _bfd_elf_gc_mark_hook (asection *sec,
12892 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12893 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12894 struct elf_link_hash_entry *h,
12895 Elf_Internal_Sym *sym)
12896 {
12897 if (h != NULL)
12898 {
12899 switch (h->root.type)
12900 {
12901 case bfd_link_hash_defined:
12902 case bfd_link_hash_defweak:
12903 return h->root.u.def.section;
12904
12905 case bfd_link_hash_common:
12906 return h->root.u.c.p->section;
12907
12908 default:
12909 break;
12910 }
12911 }
12912 else
12913 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12914
12915 return NULL;
12916 }
12917
12918 /* Return the debug definition section. */
12919
12920 static asection *
12921 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
12922 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12923 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12924 struct elf_link_hash_entry *h,
12925 Elf_Internal_Sym *sym)
12926 {
12927 if (h != NULL)
12928 {
12929 /* Return the global debug definition section. */
12930 if ((h->root.type == bfd_link_hash_defined
12931 || h->root.type == bfd_link_hash_defweak)
12932 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
12933 return h->root.u.def.section;
12934 }
12935 else
12936 {
12937 /* Return the local debug definition section. */
12938 asection *isec = bfd_section_from_elf_index (sec->owner,
12939 sym->st_shndx);
12940 if ((isec->flags & SEC_DEBUGGING) != 0)
12941 return isec;
12942 }
12943
12944 return NULL;
12945 }
12946
12947 /* COOKIE->rel describes a relocation against section SEC, which is
12948 a section we've decided to keep. Return the section that contains
12949 the relocation symbol, or NULL if no section contains it. */
12950
12951 asection *
12952 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12953 elf_gc_mark_hook_fn gc_mark_hook,
12954 struct elf_reloc_cookie *cookie,
12955 bfd_boolean *start_stop)
12956 {
12957 unsigned long r_symndx;
12958 struct elf_link_hash_entry *h;
12959
12960 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
12961 if (r_symndx == STN_UNDEF)
12962 return NULL;
12963
12964 if (r_symndx >= cookie->locsymcount
12965 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12966 {
12967 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
12968 if (h == NULL)
12969 {
12970 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
12971 sec->owner);
12972 return NULL;
12973 }
12974 while (h->root.type == bfd_link_hash_indirect
12975 || h->root.type == bfd_link_hash_warning)
12976 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12977 h->mark = 1;
12978 /* If this symbol is weak and there is a non-weak definition, we
12979 keep the non-weak definition because many backends put
12980 dynamic reloc info on the non-weak definition for code
12981 handling copy relocs. */
12982 if (h->is_weakalias)
12983 weakdef (h)->mark = 1;
12984
12985 if (start_stop != NULL)
12986 {
12987 /* To work around a glibc bug, mark XXX input sections
12988 when there is a reference to __start_XXX or __stop_XXX
12989 symbols. */
12990 if (h->start_stop)
12991 {
12992 asection *s = h->u2.start_stop_section;
12993 *start_stop = !s->gc_mark;
12994 return s;
12995 }
12996 }
12997
12998 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12999 }
13000
13001 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13002 &cookie->locsyms[r_symndx]);
13003 }
13004
13005 /* COOKIE->rel describes a relocation against section SEC, which is
13006 a section we've decided to keep. Mark the section that contains
13007 the relocation symbol. */
13008
13009 bfd_boolean
13010 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13011 asection *sec,
13012 elf_gc_mark_hook_fn gc_mark_hook,
13013 struct elf_reloc_cookie *cookie)
13014 {
13015 asection *rsec;
13016 bfd_boolean start_stop = FALSE;
13017
13018 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13019 while (rsec != NULL)
13020 {
13021 if (!rsec->gc_mark)
13022 {
13023 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13024 || (rsec->owner->flags & DYNAMIC) != 0)
13025 rsec->gc_mark = 1;
13026 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13027 return FALSE;
13028 }
13029 if (!start_stop)
13030 break;
13031 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
13032 }
13033 return TRUE;
13034 }
13035
13036 /* The mark phase of garbage collection. For a given section, mark
13037 it and any sections in this section's group, and all the sections
13038 which define symbols to which it refers. */
13039
13040 bfd_boolean
13041 _bfd_elf_gc_mark (struct bfd_link_info *info,
13042 asection *sec,
13043 elf_gc_mark_hook_fn gc_mark_hook)
13044 {
13045 bfd_boolean ret;
13046 asection *group_sec, *eh_frame;
13047
13048 sec->gc_mark = 1;
13049
13050 /* Mark all the sections in the group. */
13051 group_sec = elf_section_data (sec)->next_in_group;
13052 if (group_sec && !group_sec->gc_mark)
13053 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
13054 return FALSE;
13055
13056 /* Look through the section relocs. */
13057 ret = TRUE;
13058 eh_frame = elf_eh_frame_section (sec->owner);
13059 if ((sec->flags & SEC_RELOC) != 0
13060 && sec->reloc_count > 0
13061 && sec != eh_frame)
13062 {
13063 struct elf_reloc_cookie cookie;
13064
13065 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13066 ret = FALSE;
13067 else
13068 {
13069 for (; cookie.rel < cookie.relend; cookie.rel++)
13070 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
13071 {
13072 ret = FALSE;
13073 break;
13074 }
13075 fini_reloc_cookie_for_section (&cookie, sec);
13076 }
13077 }
13078
13079 if (ret && eh_frame && elf_fde_list (sec))
13080 {
13081 struct elf_reloc_cookie cookie;
13082
13083 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13084 ret = FALSE;
13085 else
13086 {
13087 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13088 gc_mark_hook, &cookie))
13089 ret = FALSE;
13090 fini_reloc_cookie_for_section (&cookie, eh_frame);
13091 }
13092 }
13093
13094 eh_frame = elf_section_eh_frame_entry (sec);
13095 if (ret && eh_frame && !eh_frame->gc_mark)
13096 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13097 ret = FALSE;
13098
13099 return ret;
13100 }
13101
13102 /* Scan and mark sections in a special or debug section group. */
13103
13104 static void
13105 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13106 {
13107 /* Point to first section of section group. */
13108 asection *ssec;
13109 /* Used to iterate the section group. */
13110 asection *msec;
13111
13112 bfd_boolean is_special_grp = TRUE;
13113 bfd_boolean is_debug_grp = TRUE;
13114
13115 /* First scan to see if group contains any section other than debug
13116 and special section. */
13117 ssec = msec = elf_next_in_group (grp);
13118 do
13119 {
13120 if ((msec->flags & SEC_DEBUGGING) == 0)
13121 is_debug_grp = FALSE;
13122
13123 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13124 is_special_grp = FALSE;
13125
13126 msec = elf_next_in_group (msec);
13127 }
13128 while (msec != ssec);
13129
13130 /* If this is a pure debug section group or pure special section group,
13131 keep all sections in this group. */
13132 if (is_debug_grp || is_special_grp)
13133 {
13134 do
13135 {
13136 msec->gc_mark = 1;
13137 msec = elf_next_in_group (msec);
13138 }
13139 while (msec != ssec);
13140 }
13141 }
13142
13143 /* Keep debug and special sections. */
13144
13145 bfd_boolean
13146 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13147 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
13148 {
13149 bfd *ibfd;
13150
13151 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13152 {
13153 asection *isec;
13154 bfd_boolean some_kept;
13155 bfd_boolean debug_frag_seen;
13156 bfd_boolean has_kept_debug_info;
13157
13158 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13159 continue;
13160 isec = ibfd->sections;
13161 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13162 continue;
13163
13164 /* Ensure all linker created sections are kept,
13165 see if any other section is already marked,
13166 and note if we have any fragmented debug sections. */
13167 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
13168 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13169 {
13170 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13171 isec->gc_mark = 1;
13172 else if (isec->gc_mark
13173 && (isec->flags & SEC_ALLOC) != 0
13174 && elf_section_type (isec) != SHT_NOTE)
13175 some_kept = TRUE;
13176
13177 if (!debug_frag_seen
13178 && (isec->flags & SEC_DEBUGGING)
13179 && CONST_STRNEQ (isec->name, ".debug_line."))
13180 debug_frag_seen = TRUE;
13181 }
13182
13183 /* If no non-note alloc section in this file will be kept, then
13184 we can toss out the debug and special sections. */
13185 if (!some_kept)
13186 continue;
13187
13188 /* Keep debug and special sections like .comment when they are
13189 not part of a group. Also keep section groups that contain
13190 just debug sections or special sections. */
13191 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13192 {
13193 if ((isec->flags & SEC_GROUP) != 0)
13194 _bfd_elf_gc_mark_debug_special_section_group (isec);
13195 else if (((isec->flags & SEC_DEBUGGING) != 0
13196 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13197 && elf_next_in_group (isec) == NULL)
13198 isec->gc_mark = 1;
13199 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13200 has_kept_debug_info = TRUE;
13201 }
13202
13203 /* Look for CODE sections which are going to be discarded,
13204 and find and discard any fragmented debug sections which
13205 are associated with that code section. */
13206 if (debug_frag_seen)
13207 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13208 if ((isec->flags & SEC_CODE) != 0
13209 && isec->gc_mark == 0)
13210 {
13211 unsigned int ilen;
13212 asection *dsec;
13213
13214 ilen = strlen (isec->name);
13215
13216 /* Association is determined by the name of the debug
13217 section containing the name of the code section as
13218 a suffix. For example .debug_line.text.foo is a
13219 debug section associated with .text.foo. */
13220 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13221 {
13222 unsigned int dlen;
13223
13224 if (dsec->gc_mark == 0
13225 || (dsec->flags & SEC_DEBUGGING) == 0)
13226 continue;
13227
13228 dlen = strlen (dsec->name);
13229
13230 if (dlen > ilen
13231 && strncmp (dsec->name + (dlen - ilen),
13232 isec->name, ilen) == 0)
13233 dsec->gc_mark = 0;
13234 }
13235 }
13236
13237 /* Mark debug sections referenced by kept debug sections. */
13238 if (has_kept_debug_info)
13239 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13240 if (isec->gc_mark
13241 && (isec->flags & SEC_DEBUGGING) != 0)
13242 if (!_bfd_elf_gc_mark (info, isec,
13243 elf_gc_mark_debug_section))
13244 return FALSE;
13245 }
13246 return TRUE;
13247 }
13248
13249 static bfd_boolean
13250 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13251 {
13252 bfd *sub;
13253 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13254
13255 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13256 {
13257 asection *o;
13258
13259 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13260 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
13261 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13262 continue;
13263 o = sub->sections;
13264 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13265 continue;
13266
13267 for (o = sub->sections; o != NULL; o = o->next)
13268 {
13269 /* When any section in a section group is kept, we keep all
13270 sections in the section group. If the first member of
13271 the section group is excluded, we will also exclude the
13272 group section. */
13273 if (o->flags & SEC_GROUP)
13274 {
13275 asection *first = elf_next_in_group (o);
13276 o->gc_mark = first->gc_mark;
13277 }
13278
13279 if (o->gc_mark)
13280 continue;
13281
13282 /* Skip sweeping sections already excluded. */
13283 if (o->flags & SEC_EXCLUDE)
13284 continue;
13285
13286 /* Since this is early in the link process, it is simple
13287 to remove a section from the output. */
13288 o->flags |= SEC_EXCLUDE;
13289
13290 if (info->print_gc_sections && o->size != 0)
13291 /* xgettext:c-format */
13292 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13293 o, sub);
13294 }
13295 }
13296
13297 return TRUE;
13298 }
13299
13300 /* Propagate collected vtable information. This is called through
13301 elf_link_hash_traverse. */
13302
13303 static bfd_boolean
13304 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13305 {
13306 /* Those that are not vtables. */
13307 if (h->start_stop
13308 || h->u2.vtable == NULL
13309 || h->u2.vtable->parent == NULL)
13310 return TRUE;
13311
13312 /* Those vtables that do not have parents, we cannot merge. */
13313 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
13314 return TRUE;
13315
13316 /* If we've already been done, exit. */
13317 if (h->u2.vtable->used && h->u2.vtable->used[-1])
13318 return TRUE;
13319
13320 /* Make sure the parent's table is up to date. */
13321 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
13322
13323 if (h->u2.vtable->used == NULL)
13324 {
13325 /* None of this table's entries were referenced. Re-use the
13326 parent's table. */
13327 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13328 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
13329 }
13330 else
13331 {
13332 size_t n;
13333 bfd_boolean *cu, *pu;
13334
13335 /* Or the parent's entries into ours. */
13336 cu = h->u2.vtable->used;
13337 cu[-1] = TRUE;
13338 pu = h->u2.vtable->parent->u2.vtable->used;
13339 if (pu != NULL)
13340 {
13341 const struct elf_backend_data *bed;
13342 unsigned int log_file_align;
13343
13344 bed = get_elf_backend_data (h->root.u.def.section->owner);
13345 log_file_align = bed->s->log_file_align;
13346 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
13347 while (n--)
13348 {
13349 if (*pu)
13350 *cu = TRUE;
13351 pu++;
13352 cu++;
13353 }
13354 }
13355 }
13356
13357 return TRUE;
13358 }
13359
13360 static bfd_boolean
13361 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13362 {
13363 asection *sec;
13364 bfd_vma hstart, hend;
13365 Elf_Internal_Rela *relstart, *relend, *rel;
13366 const struct elf_backend_data *bed;
13367 unsigned int log_file_align;
13368
13369 /* Take care of both those symbols that do not describe vtables as
13370 well as those that are not loaded. */
13371 if (h->start_stop
13372 || h->u2.vtable == NULL
13373 || h->u2.vtable->parent == NULL)
13374 return TRUE;
13375
13376 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13377 || h->root.type == bfd_link_hash_defweak);
13378
13379 sec = h->root.u.def.section;
13380 hstart = h->root.u.def.value;
13381 hend = hstart + h->size;
13382
13383 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13384 if (!relstart)
13385 return *(bfd_boolean *) okp = FALSE;
13386 bed = get_elf_backend_data (sec->owner);
13387 log_file_align = bed->s->log_file_align;
13388
13389 relend = relstart + sec->reloc_count;
13390
13391 for (rel = relstart; rel < relend; ++rel)
13392 if (rel->r_offset >= hstart && rel->r_offset < hend)
13393 {
13394 /* If the entry is in use, do nothing. */
13395 if (h->u2.vtable->used
13396 && (rel->r_offset - hstart) < h->u2.vtable->size)
13397 {
13398 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13399 if (h->u2.vtable->used[entry])
13400 continue;
13401 }
13402 /* Otherwise, kill it. */
13403 rel->r_offset = rel->r_info = rel->r_addend = 0;
13404 }
13405
13406 return TRUE;
13407 }
13408
13409 /* Mark sections containing dynamically referenced symbols. When
13410 building shared libraries, we must assume that any visible symbol is
13411 referenced. */
13412
13413 bfd_boolean
13414 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13415 {
13416 struct bfd_link_info *info = (struct bfd_link_info *) inf;
13417 struct bfd_elf_dynamic_list *d = info->dynamic_list;
13418
13419 if ((h->root.type == bfd_link_hash_defined
13420 || h->root.type == bfd_link_hash_defweak)
13421 && ((h->ref_dynamic && !h->forced_local)
13422 || ((h->def_regular || ELF_COMMON_DEF_P (h))
13423 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
13424 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
13425 && (!bfd_link_executable (info)
13426 || info->gc_keep_exported
13427 || info->export_dynamic
13428 || (h->dynamic
13429 && d != NULL
13430 && (*d->match) (&d->head, NULL, h->root.root.string)))
13431 && (h->versioned >= versioned
13432 || !bfd_hide_sym_by_version (info->version_info,
13433 h->root.root.string)))))
13434 h->root.u.def.section->flags |= SEC_KEEP;
13435
13436 return TRUE;
13437 }
13438
13439 /* Keep all sections containing symbols undefined on the command-line,
13440 and the section containing the entry symbol. */
13441
13442 void
13443 _bfd_elf_gc_keep (struct bfd_link_info *info)
13444 {
13445 struct bfd_sym_chain *sym;
13446
13447 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13448 {
13449 struct elf_link_hash_entry *h;
13450
13451 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13452 FALSE, FALSE, FALSE);
13453
13454 if (h != NULL
13455 && (h->root.type == bfd_link_hash_defined
13456 || h->root.type == bfd_link_hash_defweak)
13457 && !bfd_is_abs_section (h->root.u.def.section)
13458 && !bfd_is_und_section (h->root.u.def.section))
13459 h->root.u.def.section->flags |= SEC_KEEP;
13460 }
13461 }
13462
13463 bfd_boolean
13464 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13465 struct bfd_link_info *info)
13466 {
13467 bfd *ibfd = info->input_bfds;
13468
13469 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13470 {
13471 asection *sec;
13472 struct elf_reloc_cookie cookie;
13473
13474 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13475 continue;
13476 sec = ibfd->sections;
13477 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13478 continue;
13479
13480 if (!init_reloc_cookie (&cookie, info, ibfd))
13481 return FALSE;
13482
13483 for (sec = ibfd->sections; sec; sec = sec->next)
13484 {
13485 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13486 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13487 {
13488 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13489 fini_reloc_cookie_rels (&cookie, sec);
13490 }
13491 }
13492 }
13493 return TRUE;
13494 }
13495
13496 /* Do mark and sweep of unused sections. */
13497
13498 bfd_boolean
13499 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13500 {
13501 bfd_boolean ok = TRUE;
13502 bfd *sub;
13503 elf_gc_mark_hook_fn gc_mark_hook;
13504 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13505 struct elf_link_hash_table *htab;
13506
13507 if (!bed->can_gc_sections
13508 || !is_elf_hash_table (info->hash))
13509 {
13510 _bfd_error_handler(_("warning: gc-sections option ignored"));
13511 return TRUE;
13512 }
13513
13514 bed->gc_keep (info);
13515 htab = elf_hash_table (info);
13516
13517 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13518 at the .eh_frame section if we can mark the FDEs individually. */
13519 for (sub = info->input_bfds;
13520 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13521 sub = sub->link.next)
13522 {
13523 asection *sec;
13524 struct elf_reloc_cookie cookie;
13525
13526 sec = sub->sections;
13527 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13528 continue;
13529 sec = bfd_get_section_by_name (sub, ".eh_frame");
13530 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
13531 {
13532 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
13533 if (elf_section_data (sec)->sec_info
13534 && (sec->flags & SEC_LINKER_CREATED) == 0)
13535 elf_eh_frame_section (sub) = sec;
13536 fini_reloc_cookie_for_section (&cookie, sec);
13537 sec = bfd_get_next_section_by_name (NULL, sec);
13538 }
13539 }
13540
13541 /* Apply transitive closure to the vtable entry usage info. */
13542 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13543 if (!ok)
13544 return FALSE;
13545
13546 /* Kill the vtable relocations that were not used. */
13547 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13548 if (!ok)
13549 return FALSE;
13550
13551 /* Mark dynamically referenced symbols. */
13552 if (htab->dynamic_sections_created || info->gc_keep_exported)
13553 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13554
13555 /* Grovel through relocs to find out who stays ... */
13556 gc_mark_hook = bed->gc_mark_hook;
13557 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13558 {
13559 asection *o;
13560
13561 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13562 || elf_object_id (sub) != elf_hash_table_id (htab)
13563 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13564 continue;
13565
13566 o = sub->sections;
13567 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13568 continue;
13569
13570 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13571 Also treat note sections as a root, if the section is not part
13572 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13573 well as FINI_ARRAY sections for ld -r. */
13574 for (o = sub->sections; o != NULL; o = o->next)
13575 if (!o->gc_mark
13576 && (o->flags & SEC_EXCLUDE) == 0
13577 && ((o->flags & SEC_KEEP) != 0
13578 || (bfd_link_relocatable (info)
13579 && ((elf_section_data (o)->this_hdr.sh_type
13580 == SHT_PREINIT_ARRAY)
13581 || (elf_section_data (o)->this_hdr.sh_type
13582 == SHT_INIT_ARRAY)
13583 || (elf_section_data (o)->this_hdr.sh_type
13584 == SHT_FINI_ARRAY)))
13585 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13586 && elf_next_in_group (o) == NULL )))
13587 {
13588 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13589 return FALSE;
13590 }
13591 }
13592
13593 /* Allow the backend to mark additional target specific sections. */
13594 bed->gc_mark_extra_sections (info, gc_mark_hook);
13595
13596 /* ... and mark SEC_EXCLUDE for those that go. */
13597 return elf_gc_sweep (abfd, info);
13598 }
13599 \f
13600 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13601
13602 bfd_boolean
13603 bfd_elf_gc_record_vtinherit (bfd *abfd,
13604 asection *sec,
13605 struct elf_link_hash_entry *h,
13606 bfd_vma offset)
13607 {
13608 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13609 struct elf_link_hash_entry **search, *child;
13610 size_t extsymcount;
13611 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13612
13613 /* The sh_info field of the symtab header tells us where the
13614 external symbols start. We don't care about the local symbols at
13615 this point. */
13616 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13617 if (!elf_bad_symtab (abfd))
13618 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13619
13620 sym_hashes = elf_sym_hashes (abfd);
13621 sym_hashes_end = sym_hashes + extsymcount;
13622
13623 /* Hunt down the child symbol, which is in this section at the same
13624 offset as the relocation. */
13625 for (search = sym_hashes; search != sym_hashes_end; ++search)
13626 {
13627 if ((child = *search) != NULL
13628 && (child->root.type == bfd_link_hash_defined
13629 || child->root.type == bfd_link_hash_defweak)
13630 && child->root.u.def.section == sec
13631 && child->root.u.def.value == offset)
13632 goto win;
13633 }
13634
13635 /* xgettext:c-format */
13636 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
13637 abfd, sec, (uint64_t) offset);
13638 bfd_set_error (bfd_error_invalid_operation);
13639 return FALSE;
13640
13641 win:
13642 if (!child->u2.vtable)
13643 {
13644 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13645 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13646 if (!child->u2.vtable)
13647 return FALSE;
13648 }
13649 if (!h)
13650 {
13651 /* This *should* only be the absolute section. It could potentially
13652 be that someone has defined a non-global vtable though, which
13653 would be bad. It isn't worth paging in the local symbols to be
13654 sure though; that case should simply be handled by the assembler. */
13655
13656 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
13657 }
13658 else
13659 child->u2.vtable->parent = h;
13660
13661 return TRUE;
13662 }
13663
13664 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13665
13666 bfd_boolean
13667 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13668 asection *sec ATTRIBUTE_UNUSED,
13669 struct elf_link_hash_entry *h,
13670 bfd_vma addend)
13671 {
13672 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13673 unsigned int log_file_align = bed->s->log_file_align;
13674
13675 if (!h->u2.vtable)
13676 {
13677 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13678 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13679 if (!h->u2.vtable)
13680 return FALSE;
13681 }
13682
13683 if (addend >= h->u2.vtable->size)
13684 {
13685 size_t size, bytes, file_align;
13686 bfd_boolean *ptr = h->u2.vtable->used;
13687
13688 /* While the symbol is undefined, we have to be prepared to handle
13689 a zero size. */
13690 file_align = 1 << log_file_align;
13691 if (h->root.type == bfd_link_hash_undefined)
13692 size = addend + file_align;
13693 else
13694 {
13695 size = h->size;
13696 if (addend >= size)
13697 {
13698 /* Oops! We've got a reference past the defined end of
13699 the table. This is probably a bug -- shall we warn? */
13700 size = addend + file_align;
13701 }
13702 }
13703 size = (size + file_align - 1) & -file_align;
13704
13705 /* Allocate one extra entry for use as a "done" flag for the
13706 consolidation pass. */
13707 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13708
13709 if (ptr)
13710 {
13711 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
13712
13713 if (ptr != NULL)
13714 {
13715 size_t oldbytes;
13716
13717 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
13718 * sizeof (bfd_boolean));
13719 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13720 }
13721 }
13722 else
13723 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13724
13725 if (ptr == NULL)
13726 return FALSE;
13727
13728 /* And arrange for that done flag to be at index -1. */
13729 h->u2.vtable->used = ptr + 1;
13730 h->u2.vtable->size = size;
13731 }
13732
13733 h->u2.vtable->used[addend >> log_file_align] = TRUE;
13734
13735 return TRUE;
13736 }
13737
13738 /* Map an ELF section header flag to its corresponding string. */
13739 typedef struct
13740 {
13741 char *flag_name;
13742 flagword flag_value;
13743 } elf_flags_to_name_table;
13744
13745 static elf_flags_to_name_table elf_flags_to_names [] =
13746 {
13747 { "SHF_WRITE", SHF_WRITE },
13748 { "SHF_ALLOC", SHF_ALLOC },
13749 { "SHF_EXECINSTR", SHF_EXECINSTR },
13750 { "SHF_MERGE", SHF_MERGE },
13751 { "SHF_STRINGS", SHF_STRINGS },
13752 { "SHF_INFO_LINK", SHF_INFO_LINK},
13753 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13754 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13755 { "SHF_GROUP", SHF_GROUP },
13756 { "SHF_TLS", SHF_TLS },
13757 { "SHF_MASKOS", SHF_MASKOS },
13758 { "SHF_EXCLUDE", SHF_EXCLUDE },
13759 };
13760
13761 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13762 bfd_boolean
13763 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13764 struct flag_info *flaginfo,
13765 asection *section)
13766 {
13767 const bfd_vma sh_flags = elf_section_flags (section);
13768
13769 if (!flaginfo->flags_initialized)
13770 {
13771 bfd *obfd = info->output_bfd;
13772 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13773 struct flag_info_list *tf = flaginfo->flag_list;
13774 int with_hex = 0;
13775 int without_hex = 0;
13776
13777 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13778 {
13779 unsigned i;
13780 flagword (*lookup) (char *);
13781
13782 lookup = bed->elf_backend_lookup_section_flags_hook;
13783 if (lookup != NULL)
13784 {
13785 flagword hexval = (*lookup) ((char *) tf->name);
13786
13787 if (hexval != 0)
13788 {
13789 if (tf->with == with_flags)
13790 with_hex |= hexval;
13791 else if (tf->with == without_flags)
13792 without_hex |= hexval;
13793 tf->valid = TRUE;
13794 continue;
13795 }
13796 }
13797 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13798 {
13799 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13800 {
13801 if (tf->with == with_flags)
13802 with_hex |= elf_flags_to_names[i].flag_value;
13803 else if (tf->with == without_flags)
13804 without_hex |= elf_flags_to_names[i].flag_value;
13805 tf->valid = TRUE;
13806 break;
13807 }
13808 }
13809 if (!tf->valid)
13810 {
13811 info->callbacks->einfo
13812 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13813 return FALSE;
13814 }
13815 }
13816 flaginfo->flags_initialized = TRUE;
13817 flaginfo->only_with_flags |= with_hex;
13818 flaginfo->not_with_flags |= without_hex;
13819 }
13820
13821 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13822 return FALSE;
13823
13824 if ((flaginfo->not_with_flags & sh_flags) != 0)
13825 return FALSE;
13826
13827 return TRUE;
13828 }
13829
13830 struct alloc_got_off_arg {
13831 bfd_vma gotoff;
13832 struct bfd_link_info *info;
13833 };
13834
13835 /* We need a special top-level link routine to convert got reference counts
13836 to real got offsets. */
13837
13838 static bfd_boolean
13839 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13840 {
13841 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13842 bfd *obfd = gofarg->info->output_bfd;
13843 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13844
13845 if (h->got.refcount > 0)
13846 {
13847 h->got.offset = gofarg->gotoff;
13848 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13849 }
13850 else
13851 h->got.offset = (bfd_vma) -1;
13852
13853 return TRUE;
13854 }
13855
13856 /* And an accompanying bit to work out final got entry offsets once
13857 we're done. Should be called from final_link. */
13858
13859 bfd_boolean
13860 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13861 struct bfd_link_info *info)
13862 {
13863 bfd *i;
13864 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13865 bfd_vma gotoff;
13866 struct alloc_got_off_arg gofarg;
13867
13868 BFD_ASSERT (abfd == info->output_bfd);
13869
13870 if (! is_elf_hash_table (info->hash))
13871 return FALSE;
13872
13873 /* The GOT offset is relative to the .got section, but the GOT header is
13874 put into the .got.plt section, if the backend uses it. */
13875 if (bed->want_got_plt)
13876 gotoff = 0;
13877 else
13878 gotoff = bed->got_header_size;
13879
13880 /* Do the local .got entries first. */
13881 for (i = info->input_bfds; i; i = i->link.next)
13882 {
13883 bfd_signed_vma *local_got;
13884 size_t j, locsymcount;
13885 Elf_Internal_Shdr *symtab_hdr;
13886
13887 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13888 continue;
13889
13890 local_got = elf_local_got_refcounts (i);
13891 if (!local_got)
13892 continue;
13893
13894 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13895 if (elf_bad_symtab (i))
13896 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13897 else
13898 locsymcount = symtab_hdr->sh_info;
13899
13900 for (j = 0; j < locsymcount; ++j)
13901 {
13902 if (local_got[j] > 0)
13903 {
13904 local_got[j] = gotoff;
13905 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13906 }
13907 else
13908 local_got[j] = (bfd_vma) -1;
13909 }
13910 }
13911
13912 /* Then the global .got entries. .plt refcounts are handled by
13913 adjust_dynamic_symbol */
13914 gofarg.gotoff = gotoff;
13915 gofarg.info = info;
13916 elf_link_hash_traverse (elf_hash_table (info),
13917 elf_gc_allocate_got_offsets,
13918 &gofarg);
13919 return TRUE;
13920 }
13921
13922 /* Many folk need no more in the way of final link than this, once
13923 got entry reference counting is enabled. */
13924
13925 bfd_boolean
13926 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13927 {
13928 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13929 return FALSE;
13930
13931 /* Invoke the regular ELF backend linker to do all the work. */
13932 return bfd_elf_final_link (abfd, info);
13933 }
13934
13935 bfd_boolean
13936 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13937 {
13938 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13939
13940 if (rcookie->bad_symtab)
13941 rcookie->rel = rcookie->rels;
13942
13943 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13944 {
13945 unsigned long r_symndx;
13946
13947 if (! rcookie->bad_symtab)
13948 if (rcookie->rel->r_offset > offset)
13949 return FALSE;
13950 if (rcookie->rel->r_offset != offset)
13951 continue;
13952
13953 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
13954 if (r_symndx == STN_UNDEF)
13955 return TRUE;
13956
13957 if (r_symndx >= rcookie->locsymcount
13958 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13959 {
13960 struct elf_link_hash_entry *h;
13961
13962 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13963
13964 while (h->root.type == bfd_link_hash_indirect
13965 || h->root.type == bfd_link_hash_warning)
13966 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13967
13968 if ((h->root.type == bfd_link_hash_defined
13969 || h->root.type == bfd_link_hash_defweak)
13970 && (h->root.u.def.section->owner != rcookie->abfd
13971 || h->root.u.def.section->kept_section != NULL
13972 || discarded_section (h->root.u.def.section)))
13973 return TRUE;
13974 }
13975 else
13976 {
13977 /* It's not a relocation against a global symbol,
13978 but it could be a relocation against a local
13979 symbol for a discarded section. */
13980 asection *isec;
13981 Elf_Internal_Sym *isym;
13982
13983 /* Need to: get the symbol; get the section. */
13984 isym = &rcookie->locsyms[r_symndx];
13985 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
13986 if (isec != NULL
13987 && (isec->kept_section != NULL
13988 || discarded_section (isec)))
13989 return TRUE;
13990 }
13991 return FALSE;
13992 }
13993 return FALSE;
13994 }
13995
13996 /* Discard unneeded references to discarded sections.
13997 Returns -1 on error, 1 if any section's size was changed, 0 if
13998 nothing changed. This function assumes that the relocations are in
13999 sorted order, which is true for all known assemblers. */
14000
14001 int
14002 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14003 {
14004 struct elf_reloc_cookie cookie;
14005 asection *o;
14006 bfd *abfd;
14007 int changed = 0;
14008
14009 if (info->traditional_format
14010 || !is_elf_hash_table (info->hash))
14011 return 0;
14012
14013 o = bfd_get_section_by_name (output_bfd, ".stab");
14014 if (o != NULL)
14015 {
14016 asection *i;
14017
14018 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14019 {
14020 if (i->size == 0
14021 || i->reloc_count == 0
14022 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14023 continue;
14024
14025 abfd = i->owner;
14026 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14027 continue;
14028
14029 if (!init_reloc_cookie_for_section (&cookie, info, i))
14030 return -1;
14031
14032 if (_bfd_discard_section_stabs (abfd, i,
14033 elf_section_data (i)->sec_info,
14034 bfd_elf_reloc_symbol_deleted_p,
14035 &cookie))
14036 changed = 1;
14037
14038 fini_reloc_cookie_for_section (&cookie, i);
14039 }
14040 }
14041
14042 o = NULL;
14043 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14044 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
14045 if (o != NULL)
14046 {
14047 asection *i;
14048 int eh_changed = 0;
14049 unsigned int eh_alignment;
14050
14051 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14052 {
14053 if (i->size == 0)
14054 continue;
14055
14056 abfd = i->owner;
14057 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14058 continue;
14059
14060 if (!init_reloc_cookie_for_section (&cookie, info, i))
14061 return -1;
14062
14063 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14064 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
14065 bfd_elf_reloc_symbol_deleted_p,
14066 &cookie))
14067 {
14068 eh_changed = 1;
14069 if (i->size != i->rawsize)
14070 changed = 1;
14071 }
14072
14073 fini_reloc_cookie_for_section (&cookie, i);
14074 }
14075
14076 eh_alignment = 1 << o->alignment_power;
14077 /* Skip over zero terminator, and prevent empty sections from
14078 adding alignment padding at the end. */
14079 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14080 if (i->size == 0)
14081 i->flags |= SEC_EXCLUDE;
14082 else if (i->size > 4)
14083 break;
14084 /* The last non-empty eh_frame section doesn't need padding. */
14085 if (i != NULL)
14086 i = i->map_tail.s;
14087 /* Any prior sections must pad the last FDE out to the output
14088 section alignment. Otherwise we might have zero padding
14089 between sections, which would be seen as a terminator. */
14090 for (; i != NULL; i = i->map_tail.s)
14091 if (i->size == 4)
14092 /* All but the last zero terminator should have been removed. */
14093 BFD_FAIL ();
14094 else
14095 {
14096 bfd_size_type size
14097 = (i->size + eh_alignment - 1) & -eh_alignment;
14098 if (i->size != size)
14099 {
14100 i->size = size;
14101 changed = 1;
14102 eh_changed = 1;
14103 }
14104 }
14105 if (eh_changed)
14106 elf_link_hash_traverse (elf_hash_table (info),
14107 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
14108 }
14109
14110 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14111 {
14112 const struct elf_backend_data *bed;
14113 asection *s;
14114
14115 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14116 continue;
14117 s = abfd->sections;
14118 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14119 continue;
14120
14121 bed = get_elf_backend_data (abfd);
14122
14123 if (bed->elf_backend_discard_info != NULL)
14124 {
14125 if (!init_reloc_cookie (&cookie, info, abfd))
14126 return -1;
14127
14128 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
14129 changed = 1;
14130
14131 fini_reloc_cookie (&cookie, abfd);
14132 }
14133 }
14134
14135 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14136 _bfd_elf_end_eh_frame_parsing (info);
14137
14138 if (info->eh_frame_hdr_type
14139 && !bfd_link_relocatable (info)
14140 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
14141 changed = 1;
14142
14143 return changed;
14144 }
14145
14146 bfd_boolean
14147 _bfd_elf_section_already_linked (bfd *abfd,
14148 asection *sec,
14149 struct bfd_link_info *info)
14150 {
14151 flagword flags;
14152 const char *name, *key;
14153 struct bfd_section_already_linked *l;
14154 struct bfd_section_already_linked_hash_entry *already_linked_list;
14155
14156 if (sec->output_section == bfd_abs_section_ptr)
14157 return FALSE;
14158
14159 flags = sec->flags;
14160
14161 /* Return if it isn't a linkonce section. A comdat group section
14162 also has SEC_LINK_ONCE set. */
14163 if ((flags & SEC_LINK_ONCE) == 0)
14164 return FALSE;
14165
14166 /* Don't put group member sections on our list of already linked
14167 sections. They are handled as a group via their group section. */
14168 if (elf_sec_group (sec) != NULL)
14169 return FALSE;
14170
14171 /* For a SHT_GROUP section, use the group signature as the key. */
14172 name = sec->name;
14173 if ((flags & SEC_GROUP) != 0
14174 && elf_next_in_group (sec) != NULL
14175 && elf_group_name (elf_next_in_group (sec)) != NULL)
14176 key = elf_group_name (elf_next_in_group (sec));
14177 else
14178 {
14179 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14180 if (CONST_STRNEQ (name, ".gnu.linkonce.")
14181 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14182 key++;
14183 else
14184 /* Must be a user linkonce section that doesn't follow gcc's
14185 naming convention. In this case we won't be matching
14186 single member groups. */
14187 key = name;
14188 }
14189
14190 already_linked_list = bfd_section_already_linked_table_lookup (key);
14191
14192 for (l = already_linked_list->entry; l != NULL; l = l->next)
14193 {
14194 /* We may have 2 different types of sections on the list: group
14195 sections with a signature of <key> (<key> is some string),
14196 and linkonce sections named .gnu.linkonce.<type>.<key>.
14197 Match like sections. LTO plugin sections are an exception.
14198 They are always named .gnu.linkonce.t.<key> and match either
14199 type of section. */
14200 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14201 && ((flags & SEC_GROUP) != 0
14202 || strcmp (name, l->sec->name) == 0))
14203 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
14204 {
14205 /* The section has already been linked. See if we should
14206 issue a warning. */
14207 if (!_bfd_handle_already_linked (sec, l, info))
14208 return FALSE;
14209
14210 if (flags & SEC_GROUP)
14211 {
14212 asection *first = elf_next_in_group (sec);
14213 asection *s = first;
14214
14215 while (s != NULL)
14216 {
14217 s->output_section = bfd_abs_section_ptr;
14218 /* Record which group discards it. */
14219 s->kept_section = l->sec;
14220 s = elf_next_in_group (s);
14221 /* These lists are circular. */
14222 if (s == first)
14223 break;
14224 }
14225 }
14226
14227 return TRUE;
14228 }
14229 }
14230
14231 /* A single member comdat group section may be discarded by a
14232 linkonce section and vice versa. */
14233 if ((flags & SEC_GROUP) != 0)
14234 {
14235 asection *first = elf_next_in_group (sec);
14236
14237 if (first != NULL && elf_next_in_group (first) == first)
14238 /* Check this single member group against linkonce sections. */
14239 for (l = already_linked_list->entry; l != NULL; l = l->next)
14240 if ((l->sec->flags & SEC_GROUP) == 0
14241 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14242 {
14243 first->output_section = bfd_abs_section_ptr;
14244 first->kept_section = l->sec;
14245 sec->output_section = bfd_abs_section_ptr;
14246 break;
14247 }
14248 }
14249 else
14250 /* Check this linkonce section against single member groups. */
14251 for (l = already_linked_list->entry; l != NULL; l = l->next)
14252 if (l->sec->flags & SEC_GROUP)
14253 {
14254 asection *first = elf_next_in_group (l->sec);
14255
14256 if (first != NULL
14257 && elf_next_in_group (first) == first
14258 && bfd_elf_match_symbols_in_sections (first, sec, info))
14259 {
14260 sec->output_section = bfd_abs_section_ptr;
14261 sec->kept_section = first;
14262 break;
14263 }
14264 }
14265
14266 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14267 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14268 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14269 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14270 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14271 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14272 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14273 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14274 The reverse order cannot happen as there is never a bfd with only the
14275 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14276 matter as here were are looking only for cross-bfd sections. */
14277
14278 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14279 for (l = already_linked_list->entry; l != NULL; l = l->next)
14280 if ((l->sec->flags & SEC_GROUP) == 0
14281 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14282 {
14283 if (abfd != l->sec->owner)
14284 sec->output_section = bfd_abs_section_ptr;
14285 break;
14286 }
14287
14288 /* This is the first section with this name. Record it. */
14289 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
14290 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
14291 return sec->output_section == bfd_abs_section_ptr;
14292 }
14293
14294 bfd_boolean
14295 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
14296 {
14297 return sym->st_shndx == SHN_COMMON;
14298 }
14299
14300 unsigned int
14301 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14302 {
14303 return SHN_COMMON;
14304 }
14305
14306 asection *
14307 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14308 {
14309 return bfd_com_section_ptr;
14310 }
14311
14312 bfd_vma
14313 _bfd_elf_default_got_elt_size (bfd *abfd,
14314 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14315 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14316 bfd *ibfd ATTRIBUTE_UNUSED,
14317 unsigned long symndx ATTRIBUTE_UNUSED)
14318 {
14319 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14320 return bed->s->arch_size / 8;
14321 }
14322
14323 /* Routines to support the creation of dynamic relocs. */
14324
14325 /* Returns the name of the dynamic reloc section associated with SEC. */
14326
14327 static const char *
14328 get_dynamic_reloc_section_name (bfd * abfd,
14329 asection * sec,
14330 bfd_boolean is_rela)
14331 {
14332 char *name;
14333 const char *old_name = bfd_get_section_name (NULL, sec);
14334 const char *prefix = is_rela ? ".rela" : ".rel";
14335
14336 if (old_name == NULL)
14337 return NULL;
14338
14339 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
14340 sprintf (name, "%s%s", prefix, old_name);
14341
14342 return name;
14343 }
14344
14345 /* Returns the dynamic reloc section associated with SEC.
14346 If necessary compute the name of the dynamic reloc section based
14347 on SEC's name (looked up in ABFD's string table) and the setting
14348 of IS_RELA. */
14349
14350 asection *
14351 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14352 asection * sec,
14353 bfd_boolean is_rela)
14354 {
14355 asection * reloc_sec = elf_section_data (sec)->sreloc;
14356
14357 if (reloc_sec == NULL)
14358 {
14359 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14360
14361 if (name != NULL)
14362 {
14363 reloc_sec = bfd_get_linker_section (abfd, name);
14364
14365 if (reloc_sec != NULL)
14366 elf_section_data (sec)->sreloc = reloc_sec;
14367 }
14368 }
14369
14370 return reloc_sec;
14371 }
14372
14373 /* Returns the dynamic reloc section associated with SEC. If the
14374 section does not exist it is created and attached to the DYNOBJ
14375 bfd and stored in the SRELOC field of SEC's elf_section_data
14376 structure.
14377
14378 ALIGNMENT is the alignment for the newly created section and
14379 IS_RELA defines whether the name should be .rela.<SEC's name>
14380 or .rel.<SEC's name>. The section name is looked up in the
14381 string table associated with ABFD. */
14382
14383 asection *
14384 _bfd_elf_make_dynamic_reloc_section (asection *sec,
14385 bfd *dynobj,
14386 unsigned int alignment,
14387 bfd *abfd,
14388 bfd_boolean is_rela)
14389 {
14390 asection * reloc_sec = elf_section_data (sec)->sreloc;
14391
14392 if (reloc_sec == NULL)
14393 {
14394 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14395
14396 if (name == NULL)
14397 return NULL;
14398
14399 reloc_sec = bfd_get_linker_section (dynobj, name);
14400
14401 if (reloc_sec == NULL)
14402 {
14403 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14404 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
14405 if ((sec->flags & SEC_ALLOC) != 0)
14406 flags |= SEC_ALLOC | SEC_LOAD;
14407
14408 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
14409 if (reloc_sec != NULL)
14410 {
14411 /* _bfd_elf_get_sec_type_attr chooses a section type by
14412 name. Override as it may be wrong, eg. for a user
14413 section named "auto" we'll get ".relauto" which is
14414 seen to be a .rela section. */
14415 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
14416 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14417 reloc_sec = NULL;
14418 }
14419 }
14420
14421 elf_section_data (sec)->sreloc = reloc_sec;
14422 }
14423
14424 return reloc_sec;
14425 }
14426
14427 /* Copy the ELF symbol type and other attributes for a linker script
14428 assignment from HSRC to HDEST. Generally this should be treated as
14429 if we found a strong non-dynamic definition for HDEST (except that
14430 ld ignores multiple definition errors). */
14431 void
14432 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14433 struct bfd_link_hash_entry *hdest,
14434 struct bfd_link_hash_entry *hsrc)
14435 {
14436 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14437 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14438 Elf_Internal_Sym isym;
14439
14440 ehdest->type = ehsrc->type;
14441 ehdest->target_internal = ehsrc->target_internal;
14442
14443 isym.st_other = ehsrc->other;
14444 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
14445 }
14446
14447 /* Append a RELA relocation REL to section S in BFD. */
14448
14449 void
14450 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14451 {
14452 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14453 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14454 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14455 bed->s->swap_reloca_out (abfd, rel, loc);
14456 }
14457
14458 /* Append a REL relocation REL to section S in BFD. */
14459
14460 void
14461 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14462 {
14463 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14464 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14465 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
14466 bed->s->swap_reloc_out (abfd, rel, loc);
14467 }
14468
14469 /* Define __start, __stop, .startof. or .sizeof. symbol. */
14470
14471 struct bfd_link_hash_entry *
14472 bfd_elf_define_start_stop (struct bfd_link_info *info,
14473 const char *symbol, asection *sec)
14474 {
14475 struct elf_link_hash_entry *h;
14476
14477 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14478 FALSE, FALSE, TRUE);
14479 if (h != NULL
14480 && (h->root.type == bfd_link_hash_undefined
14481 || h->root.type == bfd_link_hash_undefweak
14482 || ((h->ref_regular || h->def_dynamic) && !h->def_regular)))
14483 {
14484 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
14485 h->root.type = bfd_link_hash_defined;
14486 h->root.u.def.section = sec;
14487 h->root.u.def.value = 0;
14488 h->def_regular = 1;
14489 h->def_dynamic = 0;
14490 h->start_stop = 1;
14491 h->u2.start_stop_section = sec;
14492 if (symbol[0] == '.')
14493 {
14494 /* .startof. and .sizeof. symbols are local. */
14495 const struct elf_backend_data *bed;
14496 bed = get_elf_backend_data (info->output_bfd);
14497 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
14498 }
14499 else
14500 {
14501 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14502 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
14503 if (was_dynamic)
14504 bfd_elf_link_record_dynamic_symbol (info, h);
14505 }
14506 return &h->root;
14507 }
14508 return NULL;
14509 }
This page took 0.731034 seconds and 4 git commands to generate.