elf_backend_dtrel_excludes_plt
[deliverable/binutils-gdb.git] / bfd / elflink.c
1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2016 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 }
123
124 bh = &h->root;
125 bed = get_elf_backend_data (abfd);
126 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
127 sec, 0, NULL, FALSE, bed->collect,
128 &bh))
129 return NULL;
130 h = (struct elf_link_hash_entry *) bh;
131 h->def_regular = 1;
132 h->non_elf = 0;
133 h->root.linker_def = 1;
134 h->type = STT_OBJECT;
135 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
136 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
137
138 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
139 return h;
140 }
141
142 bfd_boolean
143 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
144 {
145 flagword flags;
146 asection *s;
147 struct elf_link_hash_entry *h;
148 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
149 struct elf_link_hash_table *htab = elf_hash_table (info);
150
151 /* This function may be called more than once. */
152 if (htab->sgot != NULL)
153 return TRUE;
154
155 flags = bed->dynamic_sec_flags;
156
157 s = bfd_make_section_anyway_with_flags (abfd,
158 (bed->rela_plts_and_copies_p
159 ? ".rela.got" : ".rel.got"),
160 (bed->dynamic_sec_flags
161 | SEC_READONLY));
162 if (s == NULL
163 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
164 return FALSE;
165 htab->srelgot = s;
166
167 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
168 if (s == NULL
169 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
170 return FALSE;
171 htab->sgot = s;
172
173 if (bed->want_got_plt)
174 {
175 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
176 if (s == NULL
177 || !bfd_set_section_alignment (abfd, s,
178 bed->s->log_file_align))
179 return FALSE;
180 htab->sgotplt = s;
181 }
182
183 /* The first bit of the global offset table is the header. */
184 s->size += bed->got_header_size;
185
186 if (bed->want_got_sym)
187 {
188 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
189 (or .got.plt) section. We don't do this in the linker script
190 because we don't want to define the symbol if we are not creating
191 a global offset table. */
192 h = _bfd_elf_define_linkage_sym (abfd, info, s,
193 "_GLOBAL_OFFSET_TABLE_");
194 elf_hash_table (info)->hgot = h;
195 if (h == NULL)
196 return FALSE;
197 }
198
199 return TRUE;
200 }
201 \f
202 /* Create a strtab to hold the dynamic symbol names. */
203 static bfd_boolean
204 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
205 {
206 struct elf_link_hash_table *hash_table;
207
208 hash_table = elf_hash_table (info);
209 if (hash_table->dynobj == NULL)
210 {
211 /* We may not set dynobj, an input file holding linker created
212 dynamic sections to abfd, which may be a dynamic object with
213 its own dynamic sections. We need to find a normal input file
214 to hold linker created sections if possible. */
215 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
216 {
217 bfd *ibfd;
218 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
219 if ((ibfd->flags
220 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0)
221 {
222 abfd = ibfd;
223 break;
224 }
225 }
226 hash_table->dynobj = abfd;
227 }
228
229 if (hash_table->dynstr == NULL)
230 {
231 hash_table->dynstr = _bfd_elf_strtab_init ();
232 if (hash_table->dynstr == NULL)
233 return FALSE;
234 }
235 return TRUE;
236 }
237
238 /* Create some sections which will be filled in with dynamic linking
239 information. ABFD is an input file which requires dynamic sections
240 to be created. The dynamic sections take up virtual memory space
241 when the final executable is run, so we need to create them before
242 addresses are assigned to the output sections. We work out the
243 actual contents and size of these sections later. */
244
245 bfd_boolean
246 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
247 {
248 flagword flags;
249 asection *s;
250 const struct elf_backend_data *bed;
251 struct elf_link_hash_entry *h;
252
253 if (! is_elf_hash_table (info->hash))
254 return FALSE;
255
256 if (elf_hash_table (info)->dynamic_sections_created)
257 return TRUE;
258
259 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
260 return FALSE;
261
262 abfd = elf_hash_table (info)->dynobj;
263 bed = get_elf_backend_data (abfd);
264
265 flags = bed->dynamic_sec_flags;
266
267 /* A dynamically linked executable has a .interp section, but a
268 shared library does not. */
269 if (bfd_link_executable (info) && !info->nointerp)
270 {
271 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
272 flags | SEC_READONLY);
273 if (s == NULL)
274 return FALSE;
275 }
276
277 /* Create sections to hold version informations. These are removed
278 if they are not needed. */
279 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
280 flags | SEC_READONLY);
281 if (s == NULL
282 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
283 return FALSE;
284
285 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
286 flags | SEC_READONLY);
287 if (s == NULL
288 || ! bfd_set_section_alignment (abfd, s, 1))
289 return FALSE;
290
291 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
292 flags | SEC_READONLY);
293 if (s == NULL
294 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
295 return FALSE;
296
297 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
298 flags | SEC_READONLY);
299 if (s == NULL
300 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
301 return FALSE;
302 elf_hash_table (info)->dynsym = s;
303
304 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
305 flags | SEC_READONLY);
306 if (s == NULL)
307 return FALSE;
308
309 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
310 if (s == NULL
311 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
312 return FALSE;
313
314 /* The special symbol _DYNAMIC is always set to the start of the
315 .dynamic section. We could set _DYNAMIC in a linker script, but we
316 only want to define it if we are, in fact, creating a .dynamic
317 section. We don't want to define it if there is no .dynamic
318 section, since on some ELF platforms the start up code examines it
319 to decide how to initialize the process. */
320 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
321 elf_hash_table (info)->hdynamic = h;
322 if (h == NULL)
323 return FALSE;
324
325 if (info->emit_hash)
326 {
327 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
328 flags | SEC_READONLY);
329 if (s == NULL
330 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
331 return FALSE;
332 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
333 }
334
335 if (info->emit_gnu_hash)
336 {
337 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
338 flags | SEC_READONLY);
339 if (s == NULL
340 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
341 return FALSE;
342 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
343 4 32-bit words followed by variable count of 64-bit words, then
344 variable count of 32-bit words. */
345 if (bed->s->arch_size == 64)
346 elf_section_data (s)->this_hdr.sh_entsize = 0;
347 else
348 elf_section_data (s)->this_hdr.sh_entsize = 4;
349 }
350
351 /* Let the backend create the rest of the sections. This lets the
352 backend set the right flags. The backend will normally create
353 the .got and .plt sections. */
354 if (bed->elf_backend_create_dynamic_sections == NULL
355 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
356 return FALSE;
357
358 elf_hash_table (info)->dynamic_sections_created = TRUE;
359
360 return TRUE;
361 }
362
363 /* Create dynamic sections when linking against a dynamic object. */
364
365 bfd_boolean
366 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
367 {
368 flagword flags, pltflags;
369 struct elf_link_hash_entry *h;
370 asection *s;
371 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
372 struct elf_link_hash_table *htab = elf_hash_table (info);
373
374 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
375 .rel[a].bss sections. */
376 flags = bed->dynamic_sec_flags;
377
378 pltflags = flags;
379 if (bed->plt_not_loaded)
380 /* We do not clear SEC_ALLOC here because we still want the OS to
381 allocate space for the section; it's just that there's nothing
382 to read in from the object file. */
383 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
384 else
385 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
386 if (bed->plt_readonly)
387 pltflags |= SEC_READONLY;
388
389 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
390 if (s == NULL
391 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
392 return FALSE;
393 htab->splt = s;
394
395 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
396 .plt section. */
397 if (bed->want_plt_sym)
398 {
399 h = _bfd_elf_define_linkage_sym (abfd, info, s,
400 "_PROCEDURE_LINKAGE_TABLE_");
401 elf_hash_table (info)->hplt = h;
402 if (h == NULL)
403 return FALSE;
404 }
405
406 s = bfd_make_section_anyway_with_flags (abfd,
407 (bed->rela_plts_and_copies_p
408 ? ".rela.plt" : ".rel.plt"),
409 flags | SEC_READONLY);
410 if (s == NULL
411 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
412 return FALSE;
413 htab->srelplt = s;
414
415 if (! _bfd_elf_create_got_section (abfd, info))
416 return FALSE;
417
418 if (bed->want_dynbss)
419 {
420 /* The .dynbss section is a place to put symbols which are defined
421 by dynamic objects, are referenced by regular objects, and are
422 not functions. We must allocate space for them in the process
423 image and use a R_*_COPY reloc to tell the dynamic linker to
424 initialize them at run time. The linker script puts the .dynbss
425 section into the .bss section of the final image. */
426 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
427 (SEC_ALLOC | SEC_LINKER_CREATED));
428 if (s == NULL)
429 return FALSE;
430
431 /* The .rel[a].bss section holds copy relocs. This section is not
432 normally needed. We need to create it here, though, so that the
433 linker will map it to an output section. We can't just create it
434 only if we need it, because we will not know whether we need it
435 until we have seen all the input files, and the first time the
436 main linker code calls BFD after examining all the input files
437 (size_dynamic_sections) the input sections have already been
438 mapped to the output sections. If the section turns out not to
439 be needed, we can discard it later. We will never need this
440 section when generating a shared object, since they do not use
441 copy relocs. */
442 if (! bfd_link_pic (info))
443 {
444 s = bfd_make_section_anyway_with_flags (abfd,
445 (bed->rela_plts_and_copies_p
446 ? ".rela.bss" : ".rel.bss"),
447 flags | SEC_READONLY);
448 if (s == NULL
449 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
450 return FALSE;
451 }
452 }
453
454 return TRUE;
455 }
456 \f
457 /* Record a new dynamic symbol. We record the dynamic symbols as we
458 read the input files, since we need to have a list of all of them
459 before we can determine the final sizes of the output sections.
460 Note that we may actually call this function even though we are not
461 going to output any dynamic symbols; in some cases we know that a
462 symbol should be in the dynamic symbol table, but only if there is
463 one. */
464
465 bfd_boolean
466 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
467 struct elf_link_hash_entry *h)
468 {
469 if (h->dynindx == -1)
470 {
471 struct elf_strtab_hash *dynstr;
472 char *p;
473 const char *name;
474 size_t indx;
475
476 /* XXX: The ABI draft says the linker must turn hidden and
477 internal symbols into STB_LOCAL symbols when producing the
478 DSO. However, if ld.so honors st_other in the dynamic table,
479 this would not be necessary. */
480 switch (ELF_ST_VISIBILITY (h->other))
481 {
482 case STV_INTERNAL:
483 case STV_HIDDEN:
484 if (h->root.type != bfd_link_hash_undefined
485 && h->root.type != bfd_link_hash_undefweak)
486 {
487 h->forced_local = 1;
488 if (!elf_hash_table (info)->is_relocatable_executable)
489 return TRUE;
490 }
491
492 default:
493 break;
494 }
495
496 h->dynindx = elf_hash_table (info)->dynsymcount;
497 ++elf_hash_table (info)->dynsymcount;
498
499 dynstr = elf_hash_table (info)->dynstr;
500 if (dynstr == NULL)
501 {
502 /* Create a strtab to hold the dynamic symbol names. */
503 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
504 if (dynstr == NULL)
505 return FALSE;
506 }
507
508 /* We don't put any version information in the dynamic string
509 table. */
510 name = h->root.root.string;
511 p = strchr (name, ELF_VER_CHR);
512 if (p != NULL)
513 /* We know that the p points into writable memory. In fact,
514 there are only a few symbols that have read-only names, being
515 those like _GLOBAL_OFFSET_TABLE_ that are created specially
516 by the backends. Most symbols will have names pointing into
517 an ELF string table read from a file, or to objalloc memory. */
518 *p = 0;
519
520 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
521
522 if (p != NULL)
523 *p = ELF_VER_CHR;
524
525 if (indx == (size_t) -1)
526 return FALSE;
527 h->dynstr_index = indx;
528 }
529
530 return TRUE;
531 }
532 \f
533 /* Mark a symbol dynamic. */
534
535 static void
536 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
537 struct elf_link_hash_entry *h,
538 Elf_Internal_Sym *sym)
539 {
540 struct bfd_elf_dynamic_list *d = info->dynamic_list;
541
542 /* It may be called more than once on the same H. */
543 if(h->dynamic || bfd_link_relocatable (info))
544 return;
545
546 if ((info->dynamic_data
547 && (h->type == STT_OBJECT
548 || h->type == STT_COMMON
549 || (sym != NULL
550 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
551 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
552 || (d != NULL
553 && h->root.type == bfd_link_hash_new
554 && (*d->match) (&d->head, NULL, h->root.root.string)))
555 h->dynamic = 1;
556 }
557
558 /* Record an assignment to a symbol made by a linker script. We need
559 this in case some dynamic object refers to this symbol. */
560
561 bfd_boolean
562 bfd_elf_record_link_assignment (bfd *output_bfd,
563 struct bfd_link_info *info,
564 const char *name,
565 bfd_boolean provide,
566 bfd_boolean hidden)
567 {
568 struct elf_link_hash_entry *h, *hv;
569 struct elf_link_hash_table *htab;
570 const struct elf_backend_data *bed;
571
572 if (!is_elf_hash_table (info->hash))
573 return TRUE;
574
575 htab = elf_hash_table (info);
576 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
577 if (h == NULL)
578 return provide;
579
580 if (h->versioned == unknown)
581 {
582 /* Set versioned if symbol version is unknown. */
583 char *version = strrchr (name, ELF_VER_CHR);
584 if (version)
585 {
586 if (version > name && version[-1] != ELF_VER_CHR)
587 h->versioned = versioned_hidden;
588 else
589 h->versioned = versioned;
590 }
591 }
592
593 switch (h->root.type)
594 {
595 case bfd_link_hash_defined:
596 case bfd_link_hash_defweak:
597 case bfd_link_hash_common:
598 break;
599 case bfd_link_hash_undefweak:
600 case bfd_link_hash_undefined:
601 /* Since we're defining the symbol, don't let it seem to have not
602 been defined. record_dynamic_symbol and size_dynamic_sections
603 may depend on this. */
604 h->root.type = bfd_link_hash_new;
605 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
606 bfd_link_repair_undef_list (&htab->root);
607 break;
608 case bfd_link_hash_new:
609 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
610 h->non_elf = 0;
611 break;
612 case bfd_link_hash_indirect:
613 /* We had a versioned symbol in a dynamic library. We make the
614 the versioned symbol point to this one. */
615 bed = get_elf_backend_data (output_bfd);
616 hv = h;
617 while (hv->root.type == bfd_link_hash_indirect
618 || hv->root.type == bfd_link_hash_warning)
619 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
620 /* We don't need to update h->root.u since linker will set them
621 later. */
622 h->root.type = bfd_link_hash_undefined;
623 hv->root.type = bfd_link_hash_indirect;
624 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
625 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
626 break;
627 case bfd_link_hash_warning:
628 abort ();
629 break;
630 }
631
632 /* If this symbol is being provided by the linker script, and it is
633 currently defined by a dynamic object, but not by a regular
634 object, then mark it as undefined so that the generic linker will
635 force the correct value. */
636 if (provide
637 && h->def_dynamic
638 && !h->def_regular)
639 h->root.type = bfd_link_hash_undefined;
640
641 /* If this symbol is not being provided by the linker script, and it is
642 currently defined by a dynamic object, but not by a regular object,
643 then clear out any version information because the symbol will not be
644 associated with the dynamic object any more. */
645 if (!provide
646 && h->def_dynamic
647 && !h->def_regular)
648 h->verinfo.verdef = NULL;
649
650 h->def_regular = 1;
651
652 if (hidden)
653 {
654 bed = get_elf_backend_data (output_bfd);
655 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
656 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
657 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
658 }
659
660 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
661 and executables. */
662 if (!bfd_link_relocatable (info)
663 && h->dynindx != -1
664 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
665 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
666 h->forced_local = 1;
667
668 if ((h->def_dynamic
669 || h->ref_dynamic
670 || bfd_link_dll (info)
671 || elf_hash_table (info)->is_relocatable_executable)
672 && h->dynindx == -1)
673 {
674 if (! bfd_elf_link_record_dynamic_symbol (info, h))
675 return FALSE;
676
677 /* If this is a weak defined symbol, and we know a corresponding
678 real symbol from the same dynamic object, make sure the real
679 symbol is also made into a dynamic symbol. */
680 if (h->u.weakdef != NULL
681 && h->u.weakdef->dynindx == -1)
682 {
683 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
684 return FALSE;
685 }
686 }
687
688 return TRUE;
689 }
690
691 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
692 success, and 2 on a failure caused by attempting to record a symbol
693 in a discarded section, eg. a discarded link-once section symbol. */
694
695 int
696 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
697 bfd *input_bfd,
698 long input_indx)
699 {
700 bfd_size_type amt;
701 struct elf_link_local_dynamic_entry *entry;
702 struct elf_link_hash_table *eht;
703 struct elf_strtab_hash *dynstr;
704 size_t dynstr_index;
705 char *name;
706 Elf_External_Sym_Shndx eshndx;
707 char esym[sizeof (Elf64_External_Sym)];
708
709 if (! is_elf_hash_table (info->hash))
710 return 0;
711
712 /* See if the entry exists already. */
713 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
714 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
715 return 1;
716
717 amt = sizeof (*entry);
718 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
719 if (entry == NULL)
720 return 0;
721
722 /* Go find the symbol, so that we can find it's name. */
723 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
724 1, input_indx, &entry->isym, esym, &eshndx))
725 {
726 bfd_release (input_bfd, entry);
727 return 0;
728 }
729
730 if (entry->isym.st_shndx != SHN_UNDEF
731 && entry->isym.st_shndx < SHN_LORESERVE)
732 {
733 asection *s;
734
735 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
736 if (s == NULL || bfd_is_abs_section (s->output_section))
737 {
738 /* We can still bfd_release here as nothing has done another
739 bfd_alloc. We can't do this later in this function. */
740 bfd_release (input_bfd, entry);
741 return 2;
742 }
743 }
744
745 name = (bfd_elf_string_from_elf_section
746 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
747 entry->isym.st_name));
748
749 dynstr = elf_hash_table (info)->dynstr;
750 if (dynstr == NULL)
751 {
752 /* Create a strtab to hold the dynamic symbol names. */
753 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
754 if (dynstr == NULL)
755 return 0;
756 }
757
758 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
759 if (dynstr_index == (size_t) -1)
760 return 0;
761 entry->isym.st_name = dynstr_index;
762
763 eht = elf_hash_table (info);
764
765 entry->next = eht->dynlocal;
766 eht->dynlocal = entry;
767 entry->input_bfd = input_bfd;
768 entry->input_indx = input_indx;
769 eht->dynsymcount++;
770
771 /* Whatever binding the symbol had before, it's now local. */
772 entry->isym.st_info
773 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
774
775 /* The dynindx will be set at the end of size_dynamic_sections. */
776
777 return 1;
778 }
779
780 /* Return the dynindex of a local dynamic symbol. */
781
782 long
783 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
784 bfd *input_bfd,
785 long input_indx)
786 {
787 struct elf_link_local_dynamic_entry *e;
788
789 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
790 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
791 return e->dynindx;
792 return -1;
793 }
794
795 /* This function is used to renumber the dynamic symbols, if some of
796 them are removed because they are marked as local. This is called
797 via elf_link_hash_traverse. */
798
799 static bfd_boolean
800 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
801 void *data)
802 {
803 size_t *count = (size_t *) data;
804
805 if (h->forced_local)
806 return TRUE;
807
808 if (h->dynindx != -1)
809 h->dynindx = ++(*count);
810
811 return TRUE;
812 }
813
814
815 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
816 STB_LOCAL binding. */
817
818 static bfd_boolean
819 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
820 void *data)
821 {
822 size_t *count = (size_t *) data;
823
824 if (!h->forced_local)
825 return TRUE;
826
827 if (h->dynindx != -1)
828 h->dynindx = ++(*count);
829
830 return TRUE;
831 }
832
833 /* Return true if the dynamic symbol for a given section should be
834 omitted when creating a shared library. */
835 bfd_boolean
836 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
837 struct bfd_link_info *info,
838 asection *p)
839 {
840 struct elf_link_hash_table *htab;
841 asection *ip;
842
843 switch (elf_section_data (p)->this_hdr.sh_type)
844 {
845 case SHT_PROGBITS:
846 case SHT_NOBITS:
847 /* If sh_type is yet undecided, assume it could be
848 SHT_PROGBITS/SHT_NOBITS. */
849 case SHT_NULL:
850 htab = elf_hash_table (info);
851 if (p == htab->tls_sec)
852 return FALSE;
853
854 if (htab->text_index_section != NULL)
855 return p != htab->text_index_section && p != htab->data_index_section;
856
857 return (htab->dynobj != NULL
858 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
859 && ip->output_section == p);
860
861 /* There shouldn't be section relative relocations
862 against any other section. */
863 default:
864 return TRUE;
865 }
866 }
867
868 /* Assign dynsym indices. In a shared library we generate a section
869 symbol for each output section, which come first. Next come symbols
870 which have been forced to local binding. Then all of the back-end
871 allocated local dynamic syms, followed by the rest of the global
872 symbols. */
873
874 static unsigned long
875 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
876 struct bfd_link_info *info,
877 unsigned long *section_sym_count)
878 {
879 unsigned long dynsymcount = 0;
880
881 if (bfd_link_pic (info)
882 || elf_hash_table (info)->is_relocatable_executable)
883 {
884 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
885 asection *p;
886 for (p = output_bfd->sections; p ; p = p->next)
887 if ((p->flags & SEC_EXCLUDE) == 0
888 && (p->flags & SEC_ALLOC) != 0
889 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
890 elf_section_data (p)->dynindx = ++dynsymcount;
891 else
892 elf_section_data (p)->dynindx = 0;
893 }
894 *section_sym_count = dynsymcount;
895
896 elf_link_hash_traverse (elf_hash_table (info),
897 elf_link_renumber_local_hash_table_dynsyms,
898 &dynsymcount);
899
900 if (elf_hash_table (info)->dynlocal)
901 {
902 struct elf_link_local_dynamic_entry *p;
903 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
904 p->dynindx = ++dynsymcount;
905 }
906 elf_hash_table (info)->local_dynsymcount = dynsymcount;
907
908 elf_link_hash_traverse (elf_hash_table (info),
909 elf_link_renumber_hash_table_dynsyms,
910 &dynsymcount);
911
912 /* There is an unused NULL entry at the head of the table which we
913 must account for in our count even if the table is empty since it
914 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
915 .dynamic section. */
916 dynsymcount++;
917
918 elf_hash_table (info)->dynsymcount = dynsymcount;
919 return dynsymcount;
920 }
921
922 /* Merge st_other field. */
923
924 static void
925 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
926 const Elf_Internal_Sym *isym, asection *sec,
927 bfd_boolean definition, bfd_boolean dynamic)
928 {
929 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
930
931 /* If st_other has a processor-specific meaning, specific
932 code might be needed here. */
933 if (bed->elf_backend_merge_symbol_attribute)
934 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
935 dynamic);
936
937 if (!dynamic)
938 {
939 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
940 unsigned hvis = ELF_ST_VISIBILITY (h->other);
941
942 /* Keep the most constraining visibility. Leave the remainder
943 of the st_other field to elf_backend_merge_symbol_attribute. */
944 if (symvis - 1 < hvis - 1)
945 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
946 }
947 else if (definition
948 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
949 && (sec->flags & SEC_READONLY) == 0)
950 h->protected_def = 1;
951 }
952
953 /* This function is called when we want to merge a new symbol with an
954 existing symbol. It handles the various cases which arise when we
955 find a definition in a dynamic object, or when there is already a
956 definition in a dynamic object. The new symbol is described by
957 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
958 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
959 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
960 of an old common symbol. We set OVERRIDE if the old symbol is
961 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
962 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
963 to change. By OK to change, we mean that we shouldn't warn if the
964 type or size does change. */
965
966 static bfd_boolean
967 _bfd_elf_merge_symbol (bfd *abfd,
968 struct bfd_link_info *info,
969 const char *name,
970 Elf_Internal_Sym *sym,
971 asection **psec,
972 bfd_vma *pvalue,
973 struct elf_link_hash_entry **sym_hash,
974 bfd **poldbfd,
975 bfd_boolean *pold_weak,
976 unsigned int *pold_alignment,
977 bfd_boolean *skip,
978 bfd_boolean *override,
979 bfd_boolean *type_change_ok,
980 bfd_boolean *size_change_ok,
981 bfd_boolean *matched)
982 {
983 asection *sec, *oldsec;
984 struct elf_link_hash_entry *h;
985 struct elf_link_hash_entry *hi;
986 struct elf_link_hash_entry *flip;
987 int bind;
988 bfd *oldbfd;
989 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
990 bfd_boolean newweak, oldweak, newfunc, oldfunc;
991 const struct elf_backend_data *bed;
992 char *new_version;
993
994 *skip = FALSE;
995 *override = FALSE;
996
997 sec = *psec;
998 bind = ELF_ST_BIND (sym->st_info);
999
1000 if (! bfd_is_und_section (sec))
1001 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1002 else
1003 h = ((struct elf_link_hash_entry *)
1004 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1005 if (h == NULL)
1006 return FALSE;
1007 *sym_hash = h;
1008
1009 bed = get_elf_backend_data (abfd);
1010
1011 /* NEW_VERSION is the symbol version of the new symbol. */
1012 if (h->versioned != unversioned)
1013 {
1014 /* Symbol version is unknown or versioned. */
1015 new_version = strrchr (name, ELF_VER_CHR);
1016 if (new_version)
1017 {
1018 if (h->versioned == unknown)
1019 {
1020 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1021 h->versioned = versioned_hidden;
1022 else
1023 h->versioned = versioned;
1024 }
1025 new_version += 1;
1026 if (new_version[0] == '\0')
1027 new_version = NULL;
1028 }
1029 else
1030 h->versioned = unversioned;
1031 }
1032 else
1033 new_version = NULL;
1034
1035 /* For merging, we only care about real symbols. But we need to make
1036 sure that indirect symbol dynamic flags are updated. */
1037 hi = h;
1038 while (h->root.type == bfd_link_hash_indirect
1039 || h->root.type == bfd_link_hash_warning)
1040 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1041
1042 if (!*matched)
1043 {
1044 if (hi == h || h->root.type == bfd_link_hash_new)
1045 *matched = TRUE;
1046 else
1047 {
1048 /* OLD_HIDDEN is true if the existing symbol is only visible
1049 to the symbol with the same symbol version. NEW_HIDDEN is
1050 true if the new symbol is only visible to the symbol with
1051 the same symbol version. */
1052 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1053 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1054 if (!old_hidden && !new_hidden)
1055 /* The new symbol matches the existing symbol if both
1056 aren't hidden. */
1057 *matched = TRUE;
1058 else
1059 {
1060 /* OLD_VERSION is the symbol version of the existing
1061 symbol. */
1062 char *old_version;
1063
1064 if (h->versioned >= versioned)
1065 old_version = strrchr (h->root.root.string,
1066 ELF_VER_CHR) + 1;
1067 else
1068 old_version = NULL;
1069
1070 /* The new symbol matches the existing symbol if they
1071 have the same symbol version. */
1072 *matched = (old_version == new_version
1073 || (old_version != NULL
1074 && new_version != NULL
1075 && strcmp (old_version, new_version) == 0));
1076 }
1077 }
1078 }
1079
1080 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1081 existing symbol. */
1082
1083 oldbfd = NULL;
1084 oldsec = NULL;
1085 switch (h->root.type)
1086 {
1087 default:
1088 break;
1089
1090 case bfd_link_hash_undefined:
1091 case bfd_link_hash_undefweak:
1092 oldbfd = h->root.u.undef.abfd;
1093 break;
1094
1095 case bfd_link_hash_defined:
1096 case bfd_link_hash_defweak:
1097 oldbfd = h->root.u.def.section->owner;
1098 oldsec = h->root.u.def.section;
1099 break;
1100
1101 case bfd_link_hash_common:
1102 oldbfd = h->root.u.c.p->section->owner;
1103 oldsec = h->root.u.c.p->section;
1104 if (pold_alignment)
1105 *pold_alignment = h->root.u.c.p->alignment_power;
1106 break;
1107 }
1108 if (poldbfd && *poldbfd == NULL)
1109 *poldbfd = oldbfd;
1110
1111 /* Differentiate strong and weak symbols. */
1112 newweak = bind == STB_WEAK;
1113 oldweak = (h->root.type == bfd_link_hash_defweak
1114 || h->root.type == bfd_link_hash_undefweak);
1115 if (pold_weak)
1116 *pold_weak = oldweak;
1117
1118 /* This code is for coping with dynamic objects, and is only useful
1119 if we are doing an ELF link. */
1120 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1121 return TRUE;
1122
1123 /* We have to check it for every instance since the first few may be
1124 references and not all compilers emit symbol type for undefined
1125 symbols. */
1126 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1127
1128 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1129 respectively, is from a dynamic object. */
1130
1131 newdyn = (abfd->flags & DYNAMIC) != 0;
1132
1133 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1134 syms and defined syms in dynamic libraries respectively.
1135 ref_dynamic on the other hand can be set for a symbol defined in
1136 a dynamic library, and def_dynamic may not be set; When the
1137 definition in a dynamic lib is overridden by a definition in the
1138 executable use of the symbol in the dynamic lib becomes a
1139 reference to the executable symbol. */
1140 if (newdyn)
1141 {
1142 if (bfd_is_und_section (sec))
1143 {
1144 if (bind != STB_WEAK)
1145 {
1146 h->ref_dynamic_nonweak = 1;
1147 hi->ref_dynamic_nonweak = 1;
1148 }
1149 }
1150 else
1151 {
1152 /* Update the existing symbol only if they match. */
1153 if (*matched)
1154 h->dynamic_def = 1;
1155 hi->dynamic_def = 1;
1156 }
1157 }
1158
1159 /* If we just created the symbol, mark it as being an ELF symbol.
1160 Other than that, there is nothing to do--there is no merge issue
1161 with a newly defined symbol--so we just return. */
1162
1163 if (h->root.type == bfd_link_hash_new)
1164 {
1165 h->non_elf = 0;
1166 return TRUE;
1167 }
1168
1169 /* In cases involving weak versioned symbols, we may wind up trying
1170 to merge a symbol with itself. Catch that here, to avoid the
1171 confusion that results if we try to override a symbol with
1172 itself. The additional tests catch cases like
1173 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1174 dynamic object, which we do want to handle here. */
1175 if (abfd == oldbfd
1176 && (newweak || oldweak)
1177 && ((abfd->flags & DYNAMIC) == 0
1178 || !h->def_regular))
1179 return TRUE;
1180
1181 olddyn = FALSE;
1182 if (oldbfd != NULL)
1183 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1184 else if (oldsec != NULL)
1185 {
1186 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1187 indices used by MIPS ELF. */
1188 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1189 }
1190
1191 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1192 respectively, appear to be a definition rather than reference. */
1193
1194 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1195
1196 olddef = (h->root.type != bfd_link_hash_undefined
1197 && h->root.type != bfd_link_hash_undefweak
1198 && h->root.type != bfd_link_hash_common);
1199
1200 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1201 respectively, appear to be a function. */
1202
1203 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1204 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1205
1206 oldfunc = (h->type != STT_NOTYPE
1207 && bed->is_function_type (h->type));
1208
1209 /* If creating a default indirect symbol ("foo" or "foo@") from a
1210 dynamic versioned definition ("foo@@") skip doing so if there is
1211 an existing regular definition with a different type. We don't
1212 want, for example, a "time" variable in the executable overriding
1213 a "time" function in a shared library. */
1214 if (pold_alignment == NULL
1215 && newdyn
1216 && newdef
1217 && !olddyn
1218 && (olddef || h->root.type == bfd_link_hash_common)
1219 && ELF_ST_TYPE (sym->st_info) != h->type
1220 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1221 && h->type != STT_NOTYPE
1222 && !(newfunc && oldfunc))
1223 {
1224 *skip = TRUE;
1225 return TRUE;
1226 }
1227
1228 /* Check TLS symbols. We don't check undefined symbols introduced
1229 by "ld -u" which have no type (and oldbfd NULL), and we don't
1230 check symbols from plugins because they also have no type. */
1231 if (oldbfd != NULL
1232 && (oldbfd->flags & BFD_PLUGIN) == 0
1233 && (abfd->flags & BFD_PLUGIN) == 0
1234 && ELF_ST_TYPE (sym->st_info) != h->type
1235 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1236 {
1237 bfd *ntbfd, *tbfd;
1238 bfd_boolean ntdef, tdef;
1239 asection *ntsec, *tsec;
1240
1241 if (h->type == STT_TLS)
1242 {
1243 ntbfd = abfd;
1244 ntsec = sec;
1245 ntdef = newdef;
1246 tbfd = oldbfd;
1247 tsec = oldsec;
1248 tdef = olddef;
1249 }
1250 else
1251 {
1252 ntbfd = oldbfd;
1253 ntsec = oldsec;
1254 ntdef = olddef;
1255 tbfd = abfd;
1256 tsec = sec;
1257 tdef = newdef;
1258 }
1259
1260 if (tdef && ntdef)
1261 _bfd_error_handler
1262 /* xgettext:c-format */
1263 (_("%s: TLS definition in %B section %A "
1264 "mismatches non-TLS definition in %B section %A"),
1265 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1266 else if (!tdef && !ntdef)
1267 _bfd_error_handler
1268 /* xgettext:c-format */
1269 (_("%s: TLS reference in %B "
1270 "mismatches non-TLS reference in %B"),
1271 tbfd, ntbfd, h->root.root.string);
1272 else if (tdef)
1273 _bfd_error_handler
1274 /* xgettext:c-format */
1275 (_("%s: TLS definition in %B section %A "
1276 "mismatches non-TLS reference in %B"),
1277 tbfd, tsec, ntbfd, h->root.root.string);
1278 else
1279 _bfd_error_handler
1280 /* xgettext:c-format */
1281 (_("%s: TLS reference in %B "
1282 "mismatches non-TLS definition in %B section %A"),
1283 tbfd, ntbfd, ntsec, h->root.root.string);
1284
1285 bfd_set_error (bfd_error_bad_value);
1286 return FALSE;
1287 }
1288
1289 /* If the old symbol has non-default visibility, we ignore the new
1290 definition from a dynamic object. */
1291 if (newdyn
1292 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1293 && !bfd_is_und_section (sec))
1294 {
1295 *skip = TRUE;
1296 /* Make sure this symbol is dynamic. */
1297 h->ref_dynamic = 1;
1298 hi->ref_dynamic = 1;
1299 /* A protected symbol has external availability. Make sure it is
1300 recorded as dynamic.
1301
1302 FIXME: Should we check type and size for protected symbol? */
1303 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1304 return bfd_elf_link_record_dynamic_symbol (info, h);
1305 else
1306 return TRUE;
1307 }
1308 else if (!newdyn
1309 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1310 && h->def_dynamic)
1311 {
1312 /* If the new symbol with non-default visibility comes from a
1313 relocatable file and the old definition comes from a dynamic
1314 object, we remove the old definition. */
1315 if (hi->root.type == bfd_link_hash_indirect)
1316 {
1317 /* Handle the case where the old dynamic definition is
1318 default versioned. We need to copy the symbol info from
1319 the symbol with default version to the normal one if it
1320 was referenced before. */
1321 if (h->ref_regular)
1322 {
1323 hi->root.type = h->root.type;
1324 h->root.type = bfd_link_hash_indirect;
1325 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1326
1327 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1328 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1329 {
1330 /* If the new symbol is hidden or internal, completely undo
1331 any dynamic link state. */
1332 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1333 h->forced_local = 0;
1334 h->ref_dynamic = 0;
1335 }
1336 else
1337 h->ref_dynamic = 1;
1338
1339 h->def_dynamic = 0;
1340 /* FIXME: Should we check type and size for protected symbol? */
1341 h->size = 0;
1342 h->type = 0;
1343
1344 h = hi;
1345 }
1346 else
1347 h = hi;
1348 }
1349
1350 /* If the old symbol was undefined before, then it will still be
1351 on the undefs list. If the new symbol is undefined or
1352 common, we can't make it bfd_link_hash_new here, because new
1353 undefined or common symbols will be added to the undefs list
1354 by _bfd_generic_link_add_one_symbol. Symbols may not be
1355 added twice to the undefs list. Also, if the new symbol is
1356 undefweak then we don't want to lose the strong undef. */
1357 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1358 {
1359 h->root.type = bfd_link_hash_undefined;
1360 h->root.u.undef.abfd = abfd;
1361 }
1362 else
1363 {
1364 h->root.type = bfd_link_hash_new;
1365 h->root.u.undef.abfd = NULL;
1366 }
1367
1368 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1369 {
1370 /* If the new symbol is hidden or internal, completely undo
1371 any dynamic link state. */
1372 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1373 h->forced_local = 0;
1374 h->ref_dynamic = 0;
1375 }
1376 else
1377 h->ref_dynamic = 1;
1378 h->def_dynamic = 0;
1379 /* FIXME: Should we check type and size for protected symbol? */
1380 h->size = 0;
1381 h->type = 0;
1382 return TRUE;
1383 }
1384
1385 /* If a new weak symbol definition comes from a regular file and the
1386 old symbol comes from a dynamic library, we treat the new one as
1387 strong. Similarly, an old weak symbol definition from a regular
1388 file is treated as strong when the new symbol comes from a dynamic
1389 library. Further, an old weak symbol from a dynamic library is
1390 treated as strong if the new symbol is from a dynamic library.
1391 This reflects the way glibc's ld.so works.
1392
1393 Do this before setting *type_change_ok or *size_change_ok so that
1394 we warn properly when dynamic library symbols are overridden. */
1395
1396 if (newdef && !newdyn && olddyn)
1397 newweak = FALSE;
1398 if (olddef && newdyn)
1399 oldweak = FALSE;
1400
1401 /* Allow changes between different types of function symbol. */
1402 if (newfunc && oldfunc)
1403 *type_change_ok = TRUE;
1404
1405 /* It's OK to change the type if either the existing symbol or the
1406 new symbol is weak. A type change is also OK if the old symbol
1407 is undefined and the new symbol is defined. */
1408
1409 if (oldweak
1410 || newweak
1411 || (newdef
1412 && h->root.type == bfd_link_hash_undefined))
1413 *type_change_ok = TRUE;
1414
1415 /* It's OK to change the size if either the existing symbol or the
1416 new symbol is weak, or if the old symbol is undefined. */
1417
1418 if (*type_change_ok
1419 || h->root.type == bfd_link_hash_undefined)
1420 *size_change_ok = TRUE;
1421
1422 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1423 symbol, respectively, appears to be a common symbol in a dynamic
1424 object. If a symbol appears in an uninitialized section, and is
1425 not weak, and is not a function, then it may be a common symbol
1426 which was resolved when the dynamic object was created. We want
1427 to treat such symbols specially, because they raise special
1428 considerations when setting the symbol size: if the symbol
1429 appears as a common symbol in a regular object, and the size in
1430 the regular object is larger, we must make sure that we use the
1431 larger size. This problematic case can always be avoided in C,
1432 but it must be handled correctly when using Fortran shared
1433 libraries.
1434
1435 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1436 likewise for OLDDYNCOMMON and OLDDEF.
1437
1438 Note that this test is just a heuristic, and that it is quite
1439 possible to have an uninitialized symbol in a shared object which
1440 is really a definition, rather than a common symbol. This could
1441 lead to some minor confusion when the symbol really is a common
1442 symbol in some regular object. However, I think it will be
1443 harmless. */
1444
1445 if (newdyn
1446 && newdef
1447 && !newweak
1448 && (sec->flags & SEC_ALLOC) != 0
1449 && (sec->flags & SEC_LOAD) == 0
1450 && sym->st_size > 0
1451 && !newfunc)
1452 newdyncommon = TRUE;
1453 else
1454 newdyncommon = FALSE;
1455
1456 if (olddyn
1457 && olddef
1458 && h->root.type == bfd_link_hash_defined
1459 && h->def_dynamic
1460 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1461 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1462 && h->size > 0
1463 && !oldfunc)
1464 olddyncommon = TRUE;
1465 else
1466 olddyncommon = FALSE;
1467
1468 /* We now know everything about the old and new symbols. We ask the
1469 backend to check if we can merge them. */
1470 if (bed->merge_symbol != NULL)
1471 {
1472 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1473 return FALSE;
1474 sec = *psec;
1475 }
1476
1477 /* If both the old and the new symbols look like common symbols in a
1478 dynamic object, set the size of the symbol to the larger of the
1479 two. */
1480
1481 if (olddyncommon
1482 && newdyncommon
1483 && sym->st_size != h->size)
1484 {
1485 /* Since we think we have two common symbols, issue a multiple
1486 common warning if desired. Note that we only warn if the
1487 size is different. If the size is the same, we simply let
1488 the old symbol override the new one as normally happens with
1489 symbols defined in dynamic objects. */
1490
1491 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1492 bfd_link_hash_common, sym->st_size);
1493 if (sym->st_size > h->size)
1494 h->size = sym->st_size;
1495
1496 *size_change_ok = TRUE;
1497 }
1498
1499 /* If we are looking at a dynamic object, and we have found a
1500 definition, we need to see if the symbol was already defined by
1501 some other object. If so, we want to use the existing
1502 definition, and we do not want to report a multiple symbol
1503 definition error; we do this by clobbering *PSEC to be
1504 bfd_und_section_ptr.
1505
1506 We treat a common symbol as a definition if the symbol in the
1507 shared library is a function, since common symbols always
1508 represent variables; this can cause confusion in principle, but
1509 any such confusion would seem to indicate an erroneous program or
1510 shared library. We also permit a common symbol in a regular
1511 object to override a weak symbol in a shared object. A common
1512 symbol in executable also overrides a symbol in a shared object. */
1513
1514 if (newdyn
1515 && newdef
1516 && (olddef
1517 || (h->root.type == bfd_link_hash_common
1518 && (newweak
1519 || newfunc
1520 || (!olddyn && bfd_link_executable (info))))))
1521 {
1522 *override = TRUE;
1523 newdef = FALSE;
1524 newdyncommon = FALSE;
1525
1526 *psec = sec = bfd_und_section_ptr;
1527 *size_change_ok = TRUE;
1528
1529 /* If we get here when the old symbol is a common symbol, then
1530 we are explicitly letting it override a weak symbol or
1531 function in a dynamic object, and we don't want to warn about
1532 a type change. If the old symbol is a defined symbol, a type
1533 change warning may still be appropriate. */
1534
1535 if (h->root.type == bfd_link_hash_common)
1536 *type_change_ok = TRUE;
1537 }
1538
1539 /* Handle the special case of an old common symbol merging with a
1540 new symbol which looks like a common symbol in a shared object.
1541 We change *PSEC and *PVALUE to make the new symbol look like a
1542 common symbol, and let _bfd_generic_link_add_one_symbol do the
1543 right thing. */
1544
1545 if (newdyncommon
1546 && h->root.type == bfd_link_hash_common)
1547 {
1548 *override = TRUE;
1549 newdef = FALSE;
1550 newdyncommon = FALSE;
1551 *pvalue = sym->st_size;
1552 *psec = sec = bed->common_section (oldsec);
1553 *size_change_ok = TRUE;
1554 }
1555
1556 /* Skip weak definitions of symbols that are already defined. */
1557 if (newdef && olddef && newweak)
1558 {
1559 /* Don't skip new non-IR weak syms. */
1560 if (!(oldbfd != NULL
1561 && (oldbfd->flags & BFD_PLUGIN) != 0
1562 && (abfd->flags & BFD_PLUGIN) == 0))
1563 {
1564 newdef = FALSE;
1565 *skip = TRUE;
1566 }
1567
1568 /* Merge st_other. If the symbol already has a dynamic index,
1569 but visibility says it should not be visible, turn it into a
1570 local symbol. */
1571 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1572 if (h->dynindx != -1)
1573 switch (ELF_ST_VISIBILITY (h->other))
1574 {
1575 case STV_INTERNAL:
1576 case STV_HIDDEN:
1577 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1578 break;
1579 }
1580 }
1581
1582 /* If the old symbol is from a dynamic object, and the new symbol is
1583 a definition which is not from a dynamic object, then the new
1584 symbol overrides the old symbol. Symbols from regular files
1585 always take precedence over symbols from dynamic objects, even if
1586 they are defined after the dynamic object in the link.
1587
1588 As above, we again permit a common symbol in a regular object to
1589 override a definition in a shared object if the shared object
1590 symbol is a function or is weak. */
1591
1592 flip = NULL;
1593 if (!newdyn
1594 && (newdef
1595 || (bfd_is_com_section (sec)
1596 && (oldweak || oldfunc)))
1597 && olddyn
1598 && olddef
1599 && h->def_dynamic)
1600 {
1601 /* Change the hash table entry to undefined, and let
1602 _bfd_generic_link_add_one_symbol do the right thing with the
1603 new definition. */
1604
1605 h->root.type = bfd_link_hash_undefined;
1606 h->root.u.undef.abfd = h->root.u.def.section->owner;
1607 *size_change_ok = TRUE;
1608
1609 olddef = FALSE;
1610 olddyncommon = FALSE;
1611
1612 /* We again permit a type change when a common symbol may be
1613 overriding a function. */
1614
1615 if (bfd_is_com_section (sec))
1616 {
1617 if (oldfunc)
1618 {
1619 /* If a common symbol overrides a function, make sure
1620 that it isn't defined dynamically nor has type
1621 function. */
1622 h->def_dynamic = 0;
1623 h->type = STT_NOTYPE;
1624 }
1625 *type_change_ok = TRUE;
1626 }
1627
1628 if (hi->root.type == bfd_link_hash_indirect)
1629 flip = hi;
1630 else
1631 /* This union may have been set to be non-NULL when this symbol
1632 was seen in a dynamic object. We must force the union to be
1633 NULL, so that it is correct for a regular symbol. */
1634 h->verinfo.vertree = NULL;
1635 }
1636
1637 /* Handle the special case of a new common symbol merging with an
1638 old symbol that looks like it might be a common symbol defined in
1639 a shared object. Note that we have already handled the case in
1640 which a new common symbol should simply override the definition
1641 in the shared library. */
1642
1643 if (! newdyn
1644 && bfd_is_com_section (sec)
1645 && olddyncommon)
1646 {
1647 /* It would be best if we could set the hash table entry to a
1648 common symbol, but we don't know what to use for the section
1649 or the alignment. */
1650 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1651 bfd_link_hash_common, sym->st_size);
1652
1653 /* If the presumed common symbol in the dynamic object is
1654 larger, pretend that the new symbol has its size. */
1655
1656 if (h->size > *pvalue)
1657 *pvalue = h->size;
1658
1659 /* We need to remember the alignment required by the symbol
1660 in the dynamic object. */
1661 BFD_ASSERT (pold_alignment);
1662 *pold_alignment = h->root.u.def.section->alignment_power;
1663
1664 olddef = FALSE;
1665 olddyncommon = FALSE;
1666
1667 h->root.type = bfd_link_hash_undefined;
1668 h->root.u.undef.abfd = h->root.u.def.section->owner;
1669
1670 *size_change_ok = TRUE;
1671 *type_change_ok = TRUE;
1672
1673 if (hi->root.type == bfd_link_hash_indirect)
1674 flip = hi;
1675 else
1676 h->verinfo.vertree = NULL;
1677 }
1678
1679 if (flip != NULL)
1680 {
1681 /* Handle the case where we had a versioned symbol in a dynamic
1682 library and now find a definition in a normal object. In this
1683 case, we make the versioned symbol point to the normal one. */
1684 flip->root.type = h->root.type;
1685 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1686 h->root.type = bfd_link_hash_indirect;
1687 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1688 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1689 if (h->def_dynamic)
1690 {
1691 h->def_dynamic = 0;
1692 flip->ref_dynamic = 1;
1693 }
1694 }
1695
1696 return TRUE;
1697 }
1698
1699 /* This function is called to create an indirect symbol from the
1700 default for the symbol with the default version if needed. The
1701 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1702 set DYNSYM if the new indirect symbol is dynamic. */
1703
1704 static bfd_boolean
1705 _bfd_elf_add_default_symbol (bfd *abfd,
1706 struct bfd_link_info *info,
1707 struct elf_link_hash_entry *h,
1708 const char *name,
1709 Elf_Internal_Sym *sym,
1710 asection *sec,
1711 bfd_vma value,
1712 bfd **poldbfd,
1713 bfd_boolean *dynsym)
1714 {
1715 bfd_boolean type_change_ok;
1716 bfd_boolean size_change_ok;
1717 bfd_boolean skip;
1718 char *shortname;
1719 struct elf_link_hash_entry *hi;
1720 struct bfd_link_hash_entry *bh;
1721 const struct elf_backend_data *bed;
1722 bfd_boolean collect;
1723 bfd_boolean dynamic;
1724 bfd_boolean override;
1725 char *p;
1726 size_t len, shortlen;
1727 asection *tmp_sec;
1728 bfd_boolean matched;
1729
1730 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1731 return TRUE;
1732
1733 /* If this symbol has a version, and it is the default version, we
1734 create an indirect symbol from the default name to the fully
1735 decorated name. This will cause external references which do not
1736 specify a version to be bound to this version of the symbol. */
1737 p = strchr (name, ELF_VER_CHR);
1738 if (h->versioned == unknown)
1739 {
1740 if (p == NULL)
1741 {
1742 h->versioned = unversioned;
1743 return TRUE;
1744 }
1745 else
1746 {
1747 if (p[1] != ELF_VER_CHR)
1748 {
1749 h->versioned = versioned_hidden;
1750 return TRUE;
1751 }
1752 else
1753 h->versioned = versioned;
1754 }
1755 }
1756 else
1757 {
1758 /* PR ld/19073: We may see an unversioned definition after the
1759 default version. */
1760 if (p == NULL)
1761 return TRUE;
1762 }
1763
1764 bed = get_elf_backend_data (abfd);
1765 collect = bed->collect;
1766 dynamic = (abfd->flags & DYNAMIC) != 0;
1767
1768 shortlen = p - name;
1769 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1770 if (shortname == NULL)
1771 return FALSE;
1772 memcpy (shortname, name, shortlen);
1773 shortname[shortlen] = '\0';
1774
1775 /* We are going to create a new symbol. Merge it with any existing
1776 symbol with this name. For the purposes of the merge, act as
1777 though we were defining the symbol we just defined, although we
1778 actually going to define an indirect symbol. */
1779 type_change_ok = FALSE;
1780 size_change_ok = FALSE;
1781 matched = TRUE;
1782 tmp_sec = sec;
1783 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1784 &hi, poldbfd, NULL, NULL, &skip, &override,
1785 &type_change_ok, &size_change_ok, &matched))
1786 return FALSE;
1787
1788 if (skip)
1789 goto nondefault;
1790
1791 if (hi->def_regular)
1792 {
1793 /* If the undecorated symbol will have a version added by a
1794 script different to H, then don't indirect to/from the
1795 undecorated symbol. This isn't ideal because we may not yet
1796 have seen symbol versions, if given by a script on the
1797 command line rather than via --version-script. */
1798 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1799 {
1800 bfd_boolean hide;
1801
1802 hi->verinfo.vertree
1803 = bfd_find_version_for_sym (info->version_info,
1804 hi->root.root.string, &hide);
1805 if (hi->verinfo.vertree != NULL && hide)
1806 {
1807 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1808 goto nondefault;
1809 }
1810 }
1811 if (hi->verinfo.vertree != NULL
1812 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1813 goto nondefault;
1814 }
1815
1816 if (! override)
1817 {
1818 /* Add the default symbol if not performing a relocatable link. */
1819 if (! bfd_link_relocatable (info))
1820 {
1821 bh = &hi->root;
1822 if (! (_bfd_generic_link_add_one_symbol
1823 (info, abfd, shortname, BSF_INDIRECT,
1824 bfd_ind_section_ptr,
1825 0, name, FALSE, collect, &bh)))
1826 return FALSE;
1827 hi = (struct elf_link_hash_entry *) bh;
1828 }
1829 }
1830 else
1831 {
1832 /* In this case the symbol named SHORTNAME is overriding the
1833 indirect symbol we want to add. We were planning on making
1834 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1835 is the name without a version. NAME is the fully versioned
1836 name, and it is the default version.
1837
1838 Overriding means that we already saw a definition for the
1839 symbol SHORTNAME in a regular object, and it is overriding
1840 the symbol defined in the dynamic object.
1841
1842 When this happens, we actually want to change NAME, the
1843 symbol we just added, to refer to SHORTNAME. This will cause
1844 references to NAME in the shared object to become references
1845 to SHORTNAME in the regular object. This is what we expect
1846 when we override a function in a shared object: that the
1847 references in the shared object will be mapped to the
1848 definition in the regular object. */
1849
1850 while (hi->root.type == bfd_link_hash_indirect
1851 || hi->root.type == bfd_link_hash_warning)
1852 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1853
1854 h->root.type = bfd_link_hash_indirect;
1855 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1856 if (h->def_dynamic)
1857 {
1858 h->def_dynamic = 0;
1859 hi->ref_dynamic = 1;
1860 if (hi->ref_regular
1861 || hi->def_regular)
1862 {
1863 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1864 return FALSE;
1865 }
1866 }
1867
1868 /* Now set HI to H, so that the following code will set the
1869 other fields correctly. */
1870 hi = h;
1871 }
1872
1873 /* Check if HI is a warning symbol. */
1874 if (hi->root.type == bfd_link_hash_warning)
1875 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1876
1877 /* If there is a duplicate definition somewhere, then HI may not
1878 point to an indirect symbol. We will have reported an error to
1879 the user in that case. */
1880
1881 if (hi->root.type == bfd_link_hash_indirect)
1882 {
1883 struct elf_link_hash_entry *ht;
1884
1885 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1886 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1887
1888 /* A reference to the SHORTNAME symbol from a dynamic library
1889 will be satisfied by the versioned symbol at runtime. In
1890 effect, we have a reference to the versioned symbol. */
1891 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1892 hi->dynamic_def |= ht->dynamic_def;
1893
1894 /* See if the new flags lead us to realize that the symbol must
1895 be dynamic. */
1896 if (! *dynsym)
1897 {
1898 if (! dynamic)
1899 {
1900 if (! bfd_link_executable (info)
1901 || hi->def_dynamic
1902 || hi->ref_dynamic)
1903 *dynsym = TRUE;
1904 }
1905 else
1906 {
1907 if (hi->ref_regular)
1908 *dynsym = TRUE;
1909 }
1910 }
1911 }
1912
1913 /* We also need to define an indirection from the nondefault version
1914 of the symbol. */
1915
1916 nondefault:
1917 len = strlen (name);
1918 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
1919 if (shortname == NULL)
1920 return FALSE;
1921 memcpy (shortname, name, shortlen);
1922 memcpy (shortname + shortlen, p + 1, len - shortlen);
1923
1924 /* Once again, merge with any existing symbol. */
1925 type_change_ok = FALSE;
1926 size_change_ok = FALSE;
1927 tmp_sec = sec;
1928 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1929 &hi, poldbfd, NULL, NULL, &skip, &override,
1930 &type_change_ok, &size_change_ok, &matched))
1931 return FALSE;
1932
1933 if (skip)
1934 return TRUE;
1935
1936 if (override)
1937 {
1938 /* Here SHORTNAME is a versioned name, so we don't expect to see
1939 the type of override we do in the case above unless it is
1940 overridden by a versioned definition. */
1941 if (hi->root.type != bfd_link_hash_defined
1942 && hi->root.type != bfd_link_hash_defweak)
1943 _bfd_error_handler
1944 /* xgettext:c-format */
1945 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1946 abfd, shortname);
1947 }
1948 else
1949 {
1950 bh = &hi->root;
1951 if (! (_bfd_generic_link_add_one_symbol
1952 (info, abfd, shortname, BSF_INDIRECT,
1953 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1954 return FALSE;
1955 hi = (struct elf_link_hash_entry *) bh;
1956
1957 /* If there is a duplicate definition somewhere, then HI may not
1958 point to an indirect symbol. We will have reported an error
1959 to the user in that case. */
1960
1961 if (hi->root.type == bfd_link_hash_indirect)
1962 {
1963 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1964 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1965 hi->dynamic_def |= h->dynamic_def;
1966
1967 /* See if the new flags lead us to realize that the symbol
1968 must be dynamic. */
1969 if (! *dynsym)
1970 {
1971 if (! dynamic)
1972 {
1973 if (! bfd_link_executable (info)
1974 || hi->ref_dynamic)
1975 *dynsym = TRUE;
1976 }
1977 else
1978 {
1979 if (hi->ref_regular)
1980 *dynsym = TRUE;
1981 }
1982 }
1983 }
1984 }
1985
1986 return TRUE;
1987 }
1988 \f
1989 /* This routine is used to export all defined symbols into the dynamic
1990 symbol table. It is called via elf_link_hash_traverse. */
1991
1992 static bfd_boolean
1993 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1994 {
1995 struct elf_info_failed *eif = (struct elf_info_failed *) data;
1996
1997 /* Ignore indirect symbols. These are added by the versioning code. */
1998 if (h->root.type == bfd_link_hash_indirect)
1999 return TRUE;
2000
2001 /* Ignore this if we won't export it. */
2002 if (!eif->info->export_dynamic && !h->dynamic)
2003 return TRUE;
2004
2005 if (h->dynindx == -1
2006 && (h->def_regular || h->ref_regular)
2007 && ! bfd_hide_sym_by_version (eif->info->version_info,
2008 h->root.root.string))
2009 {
2010 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2011 {
2012 eif->failed = TRUE;
2013 return FALSE;
2014 }
2015 }
2016
2017 return TRUE;
2018 }
2019 \f
2020 /* Look through the symbols which are defined in other shared
2021 libraries and referenced here. Update the list of version
2022 dependencies. This will be put into the .gnu.version_r section.
2023 This function is called via elf_link_hash_traverse. */
2024
2025 static bfd_boolean
2026 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2027 void *data)
2028 {
2029 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2030 Elf_Internal_Verneed *t;
2031 Elf_Internal_Vernaux *a;
2032 bfd_size_type amt;
2033
2034 /* We only care about symbols defined in shared objects with version
2035 information. */
2036 if (!h->def_dynamic
2037 || h->def_regular
2038 || h->dynindx == -1
2039 || h->verinfo.verdef == NULL
2040 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2041 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2042 return TRUE;
2043
2044 /* See if we already know about this version. */
2045 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2046 t != NULL;
2047 t = t->vn_nextref)
2048 {
2049 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2050 continue;
2051
2052 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2053 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2054 return TRUE;
2055
2056 break;
2057 }
2058
2059 /* This is a new version. Add it to tree we are building. */
2060
2061 if (t == NULL)
2062 {
2063 amt = sizeof *t;
2064 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2065 if (t == NULL)
2066 {
2067 rinfo->failed = TRUE;
2068 return FALSE;
2069 }
2070
2071 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2072 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2073 elf_tdata (rinfo->info->output_bfd)->verref = t;
2074 }
2075
2076 amt = sizeof *a;
2077 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2078 if (a == NULL)
2079 {
2080 rinfo->failed = TRUE;
2081 return FALSE;
2082 }
2083
2084 /* Note that we are copying a string pointer here, and testing it
2085 above. If bfd_elf_string_from_elf_section is ever changed to
2086 discard the string data when low in memory, this will have to be
2087 fixed. */
2088 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2089
2090 a->vna_flags = h->verinfo.verdef->vd_flags;
2091 a->vna_nextptr = t->vn_auxptr;
2092
2093 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2094 ++rinfo->vers;
2095
2096 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2097
2098 t->vn_auxptr = a;
2099
2100 return TRUE;
2101 }
2102
2103 /* Figure out appropriate versions for all the symbols. We may not
2104 have the version number script until we have read all of the input
2105 files, so until that point we don't know which symbols should be
2106 local. This function is called via elf_link_hash_traverse. */
2107
2108 static bfd_boolean
2109 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2110 {
2111 struct elf_info_failed *sinfo;
2112 struct bfd_link_info *info;
2113 const struct elf_backend_data *bed;
2114 struct elf_info_failed eif;
2115 char *p;
2116
2117 sinfo = (struct elf_info_failed *) data;
2118 info = sinfo->info;
2119
2120 /* Fix the symbol flags. */
2121 eif.failed = FALSE;
2122 eif.info = info;
2123 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2124 {
2125 if (eif.failed)
2126 sinfo->failed = TRUE;
2127 return FALSE;
2128 }
2129
2130 /* We only need version numbers for symbols defined in regular
2131 objects. */
2132 if (!h->def_regular)
2133 return TRUE;
2134
2135 bed = get_elf_backend_data (info->output_bfd);
2136 p = strchr (h->root.root.string, ELF_VER_CHR);
2137 if (p != NULL && h->verinfo.vertree == NULL)
2138 {
2139 struct bfd_elf_version_tree *t;
2140
2141 ++p;
2142 if (*p == ELF_VER_CHR)
2143 ++p;
2144
2145 /* If there is no version string, we can just return out. */
2146 if (*p == '\0')
2147 return TRUE;
2148
2149 /* Look for the version. If we find it, it is no longer weak. */
2150 for (t = sinfo->info->version_info; t != NULL; t = t->next)
2151 {
2152 if (strcmp (t->name, p) == 0)
2153 {
2154 size_t len;
2155 char *alc;
2156 struct bfd_elf_version_expr *d;
2157
2158 len = p - h->root.root.string;
2159 alc = (char *) bfd_malloc (len);
2160 if (alc == NULL)
2161 {
2162 sinfo->failed = TRUE;
2163 return FALSE;
2164 }
2165 memcpy (alc, h->root.root.string, len - 1);
2166 alc[len - 1] = '\0';
2167 if (alc[len - 2] == ELF_VER_CHR)
2168 alc[len - 2] = '\0';
2169
2170 h->verinfo.vertree = t;
2171 t->used = TRUE;
2172 d = NULL;
2173
2174 if (t->globals.list != NULL)
2175 d = (*t->match) (&t->globals, NULL, alc);
2176
2177 /* See if there is anything to force this symbol to
2178 local scope. */
2179 if (d == NULL && t->locals.list != NULL)
2180 {
2181 d = (*t->match) (&t->locals, NULL, alc);
2182 if (d != NULL
2183 && h->dynindx != -1
2184 && ! info->export_dynamic)
2185 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2186 }
2187
2188 free (alc);
2189 break;
2190 }
2191 }
2192
2193 /* If we are building an application, we need to create a
2194 version node for this version. */
2195 if (t == NULL && bfd_link_executable (info))
2196 {
2197 struct bfd_elf_version_tree **pp;
2198 int version_index;
2199
2200 /* If we aren't going to export this symbol, we don't need
2201 to worry about it. */
2202 if (h->dynindx == -1)
2203 return TRUE;
2204
2205 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2206 sizeof *t);
2207 if (t == NULL)
2208 {
2209 sinfo->failed = TRUE;
2210 return FALSE;
2211 }
2212
2213 t->name = p;
2214 t->name_indx = (unsigned int) -1;
2215 t->used = TRUE;
2216
2217 version_index = 1;
2218 /* Don't count anonymous version tag. */
2219 if (sinfo->info->version_info != NULL
2220 && sinfo->info->version_info->vernum == 0)
2221 version_index = 0;
2222 for (pp = &sinfo->info->version_info;
2223 *pp != NULL;
2224 pp = &(*pp)->next)
2225 ++version_index;
2226 t->vernum = version_index;
2227
2228 *pp = t;
2229
2230 h->verinfo.vertree = t;
2231 }
2232 else if (t == NULL)
2233 {
2234 /* We could not find the version for a symbol when
2235 generating a shared archive. Return an error. */
2236 _bfd_error_handler
2237 /* xgettext:c-format */
2238 (_("%B: version node not found for symbol %s"),
2239 info->output_bfd, h->root.root.string);
2240 bfd_set_error (bfd_error_bad_value);
2241 sinfo->failed = TRUE;
2242 return FALSE;
2243 }
2244 }
2245
2246 /* If we don't have a version for this symbol, see if we can find
2247 something. */
2248 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
2249 {
2250 bfd_boolean hide;
2251
2252 h->verinfo.vertree
2253 = bfd_find_version_for_sym (sinfo->info->version_info,
2254 h->root.root.string, &hide);
2255 if (h->verinfo.vertree != NULL && hide)
2256 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2257 }
2258
2259 return TRUE;
2260 }
2261 \f
2262 /* Read and swap the relocs from the section indicated by SHDR. This
2263 may be either a REL or a RELA section. The relocations are
2264 translated into RELA relocations and stored in INTERNAL_RELOCS,
2265 which should have already been allocated to contain enough space.
2266 The EXTERNAL_RELOCS are a buffer where the external form of the
2267 relocations should be stored.
2268
2269 Returns FALSE if something goes wrong. */
2270
2271 static bfd_boolean
2272 elf_link_read_relocs_from_section (bfd *abfd,
2273 asection *sec,
2274 Elf_Internal_Shdr *shdr,
2275 void *external_relocs,
2276 Elf_Internal_Rela *internal_relocs)
2277 {
2278 const struct elf_backend_data *bed;
2279 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2280 const bfd_byte *erela;
2281 const bfd_byte *erelaend;
2282 Elf_Internal_Rela *irela;
2283 Elf_Internal_Shdr *symtab_hdr;
2284 size_t nsyms;
2285
2286 /* Position ourselves at the start of the section. */
2287 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2288 return FALSE;
2289
2290 /* Read the relocations. */
2291 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2292 return FALSE;
2293
2294 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2295 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2296
2297 bed = get_elf_backend_data (abfd);
2298
2299 /* Convert the external relocations to the internal format. */
2300 if (shdr->sh_entsize == bed->s->sizeof_rel)
2301 swap_in = bed->s->swap_reloc_in;
2302 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2303 swap_in = bed->s->swap_reloca_in;
2304 else
2305 {
2306 bfd_set_error (bfd_error_wrong_format);
2307 return FALSE;
2308 }
2309
2310 erela = (const bfd_byte *) external_relocs;
2311 erelaend = erela + shdr->sh_size;
2312 irela = internal_relocs;
2313 while (erela < erelaend)
2314 {
2315 bfd_vma r_symndx;
2316
2317 (*swap_in) (abfd, erela, irela);
2318 r_symndx = ELF32_R_SYM (irela->r_info);
2319 if (bed->s->arch_size == 64)
2320 r_symndx >>= 24;
2321 if (nsyms > 0)
2322 {
2323 if ((size_t) r_symndx >= nsyms)
2324 {
2325 _bfd_error_handler
2326 /* xgettext:c-format */
2327 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2328 " for offset 0x%lx in section `%A'"),
2329 abfd, sec,
2330 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2331 bfd_set_error (bfd_error_bad_value);
2332 return FALSE;
2333 }
2334 }
2335 else if (r_symndx != STN_UNDEF)
2336 {
2337 _bfd_error_handler
2338 /* xgettext:c-format */
2339 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2340 " when the object file has no symbol table"),
2341 abfd, sec,
2342 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2343 bfd_set_error (bfd_error_bad_value);
2344 return FALSE;
2345 }
2346 irela += bed->s->int_rels_per_ext_rel;
2347 erela += shdr->sh_entsize;
2348 }
2349
2350 return TRUE;
2351 }
2352
2353 /* Read and swap the relocs for a section O. They may have been
2354 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2355 not NULL, they are used as buffers to read into. They are known to
2356 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2357 the return value is allocated using either malloc or bfd_alloc,
2358 according to the KEEP_MEMORY argument. If O has two relocation
2359 sections (both REL and RELA relocations), then the REL_HDR
2360 relocations will appear first in INTERNAL_RELOCS, followed by the
2361 RELA_HDR relocations. */
2362
2363 Elf_Internal_Rela *
2364 _bfd_elf_link_read_relocs (bfd *abfd,
2365 asection *o,
2366 void *external_relocs,
2367 Elf_Internal_Rela *internal_relocs,
2368 bfd_boolean keep_memory)
2369 {
2370 void *alloc1 = NULL;
2371 Elf_Internal_Rela *alloc2 = NULL;
2372 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2373 struct bfd_elf_section_data *esdo = elf_section_data (o);
2374 Elf_Internal_Rela *internal_rela_relocs;
2375
2376 if (esdo->relocs != NULL)
2377 return esdo->relocs;
2378
2379 if (o->reloc_count == 0)
2380 return NULL;
2381
2382 if (internal_relocs == NULL)
2383 {
2384 bfd_size_type size;
2385
2386 size = o->reloc_count;
2387 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2388 if (keep_memory)
2389 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2390 else
2391 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2392 if (internal_relocs == NULL)
2393 goto error_return;
2394 }
2395
2396 if (external_relocs == NULL)
2397 {
2398 bfd_size_type size = 0;
2399
2400 if (esdo->rel.hdr)
2401 size += esdo->rel.hdr->sh_size;
2402 if (esdo->rela.hdr)
2403 size += esdo->rela.hdr->sh_size;
2404
2405 alloc1 = bfd_malloc (size);
2406 if (alloc1 == NULL)
2407 goto error_return;
2408 external_relocs = alloc1;
2409 }
2410
2411 internal_rela_relocs = internal_relocs;
2412 if (esdo->rel.hdr)
2413 {
2414 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2415 external_relocs,
2416 internal_relocs))
2417 goto error_return;
2418 external_relocs = (((bfd_byte *) external_relocs)
2419 + esdo->rel.hdr->sh_size);
2420 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2421 * bed->s->int_rels_per_ext_rel);
2422 }
2423
2424 if (esdo->rela.hdr
2425 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2426 external_relocs,
2427 internal_rela_relocs)))
2428 goto error_return;
2429
2430 /* Cache the results for next time, if we can. */
2431 if (keep_memory)
2432 esdo->relocs = internal_relocs;
2433
2434 if (alloc1 != NULL)
2435 free (alloc1);
2436
2437 /* Don't free alloc2, since if it was allocated we are passing it
2438 back (under the name of internal_relocs). */
2439
2440 return internal_relocs;
2441
2442 error_return:
2443 if (alloc1 != NULL)
2444 free (alloc1);
2445 if (alloc2 != NULL)
2446 {
2447 if (keep_memory)
2448 bfd_release (abfd, alloc2);
2449 else
2450 free (alloc2);
2451 }
2452 return NULL;
2453 }
2454
2455 /* Compute the size of, and allocate space for, REL_HDR which is the
2456 section header for a section containing relocations for O. */
2457
2458 static bfd_boolean
2459 _bfd_elf_link_size_reloc_section (bfd *abfd,
2460 struct bfd_elf_section_reloc_data *reldata)
2461 {
2462 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2463
2464 /* That allows us to calculate the size of the section. */
2465 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2466
2467 /* The contents field must last into write_object_contents, so we
2468 allocate it with bfd_alloc rather than malloc. Also since we
2469 cannot be sure that the contents will actually be filled in,
2470 we zero the allocated space. */
2471 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2472 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2473 return FALSE;
2474
2475 if (reldata->hashes == NULL && reldata->count)
2476 {
2477 struct elf_link_hash_entry **p;
2478
2479 p = ((struct elf_link_hash_entry **)
2480 bfd_zmalloc (reldata->count * sizeof (*p)));
2481 if (p == NULL)
2482 return FALSE;
2483
2484 reldata->hashes = p;
2485 }
2486
2487 return TRUE;
2488 }
2489
2490 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2491 originated from the section given by INPUT_REL_HDR) to the
2492 OUTPUT_BFD. */
2493
2494 bfd_boolean
2495 _bfd_elf_link_output_relocs (bfd *output_bfd,
2496 asection *input_section,
2497 Elf_Internal_Shdr *input_rel_hdr,
2498 Elf_Internal_Rela *internal_relocs,
2499 struct elf_link_hash_entry **rel_hash
2500 ATTRIBUTE_UNUSED)
2501 {
2502 Elf_Internal_Rela *irela;
2503 Elf_Internal_Rela *irelaend;
2504 bfd_byte *erel;
2505 struct bfd_elf_section_reloc_data *output_reldata;
2506 asection *output_section;
2507 const struct elf_backend_data *bed;
2508 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2509 struct bfd_elf_section_data *esdo;
2510
2511 output_section = input_section->output_section;
2512
2513 bed = get_elf_backend_data (output_bfd);
2514 esdo = elf_section_data (output_section);
2515 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2516 {
2517 output_reldata = &esdo->rel;
2518 swap_out = bed->s->swap_reloc_out;
2519 }
2520 else if (esdo->rela.hdr
2521 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2522 {
2523 output_reldata = &esdo->rela;
2524 swap_out = bed->s->swap_reloca_out;
2525 }
2526 else
2527 {
2528 _bfd_error_handler
2529 /* xgettext:c-format */
2530 (_("%B: relocation size mismatch in %B section %A"),
2531 output_bfd, input_section->owner, input_section);
2532 bfd_set_error (bfd_error_wrong_format);
2533 return FALSE;
2534 }
2535
2536 erel = output_reldata->hdr->contents;
2537 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2538 irela = internal_relocs;
2539 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2540 * bed->s->int_rels_per_ext_rel);
2541 while (irela < irelaend)
2542 {
2543 (*swap_out) (output_bfd, irela, erel);
2544 irela += bed->s->int_rels_per_ext_rel;
2545 erel += input_rel_hdr->sh_entsize;
2546 }
2547
2548 /* Bump the counter, so that we know where to add the next set of
2549 relocations. */
2550 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2551
2552 return TRUE;
2553 }
2554 \f
2555 /* Make weak undefined symbols in PIE dynamic. */
2556
2557 bfd_boolean
2558 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2559 struct elf_link_hash_entry *h)
2560 {
2561 if (bfd_link_pie (info)
2562 && h->dynindx == -1
2563 && h->root.type == bfd_link_hash_undefweak)
2564 return bfd_elf_link_record_dynamic_symbol (info, h);
2565
2566 return TRUE;
2567 }
2568
2569 /* Fix up the flags for a symbol. This handles various cases which
2570 can only be fixed after all the input files are seen. This is
2571 currently called by both adjust_dynamic_symbol and
2572 assign_sym_version, which is unnecessary but perhaps more robust in
2573 the face of future changes. */
2574
2575 static bfd_boolean
2576 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2577 struct elf_info_failed *eif)
2578 {
2579 const struct elf_backend_data *bed;
2580
2581 /* If this symbol was mentioned in a non-ELF file, try to set
2582 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2583 permit a non-ELF file to correctly refer to a symbol defined in
2584 an ELF dynamic object. */
2585 if (h->non_elf)
2586 {
2587 while (h->root.type == bfd_link_hash_indirect)
2588 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2589
2590 if (h->root.type != bfd_link_hash_defined
2591 && h->root.type != bfd_link_hash_defweak)
2592 {
2593 h->ref_regular = 1;
2594 h->ref_regular_nonweak = 1;
2595 }
2596 else
2597 {
2598 if (h->root.u.def.section->owner != NULL
2599 && (bfd_get_flavour (h->root.u.def.section->owner)
2600 == bfd_target_elf_flavour))
2601 {
2602 h->ref_regular = 1;
2603 h->ref_regular_nonweak = 1;
2604 }
2605 else
2606 h->def_regular = 1;
2607 }
2608
2609 if (h->dynindx == -1
2610 && (h->def_dynamic
2611 || h->ref_dynamic))
2612 {
2613 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2614 {
2615 eif->failed = TRUE;
2616 return FALSE;
2617 }
2618 }
2619 }
2620 else
2621 {
2622 /* Unfortunately, NON_ELF is only correct if the symbol
2623 was first seen in a non-ELF file. Fortunately, if the symbol
2624 was first seen in an ELF file, we're probably OK unless the
2625 symbol was defined in a non-ELF file. Catch that case here.
2626 FIXME: We're still in trouble if the symbol was first seen in
2627 a dynamic object, and then later in a non-ELF regular object. */
2628 if ((h->root.type == bfd_link_hash_defined
2629 || h->root.type == bfd_link_hash_defweak)
2630 && !h->def_regular
2631 && (h->root.u.def.section->owner != NULL
2632 ? (bfd_get_flavour (h->root.u.def.section->owner)
2633 != bfd_target_elf_flavour)
2634 : (bfd_is_abs_section (h->root.u.def.section)
2635 && !h->def_dynamic)))
2636 h->def_regular = 1;
2637 }
2638
2639 /* Backend specific symbol fixup. */
2640 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2641 if (bed->elf_backend_fixup_symbol
2642 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2643 return FALSE;
2644
2645 /* If this is a final link, and the symbol was defined as a common
2646 symbol in a regular object file, and there was no definition in
2647 any dynamic object, then the linker will have allocated space for
2648 the symbol in a common section but the DEF_REGULAR
2649 flag will not have been set. */
2650 if (h->root.type == bfd_link_hash_defined
2651 && !h->def_regular
2652 && h->ref_regular
2653 && !h->def_dynamic
2654 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2655 h->def_regular = 1;
2656
2657 /* If -Bsymbolic was used (which means to bind references to global
2658 symbols to the definition within the shared object), and this
2659 symbol was defined in a regular object, then it actually doesn't
2660 need a PLT entry. Likewise, if the symbol has non-default
2661 visibility. If the symbol has hidden or internal visibility, we
2662 will force it local. */
2663 if (h->needs_plt
2664 && bfd_link_pic (eif->info)
2665 && is_elf_hash_table (eif->info->hash)
2666 && (SYMBOLIC_BIND (eif->info, h)
2667 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2668 && h->def_regular)
2669 {
2670 bfd_boolean force_local;
2671
2672 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2673 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2674 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2675 }
2676
2677 /* If a weak undefined symbol has non-default visibility, we also
2678 hide it from the dynamic linker. */
2679 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2680 && h->root.type == bfd_link_hash_undefweak)
2681 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2682
2683 /* If this is a weak defined symbol in a dynamic object, and we know
2684 the real definition in the dynamic object, copy interesting flags
2685 over to the real definition. */
2686 if (h->u.weakdef != NULL)
2687 {
2688 /* If the real definition is defined by a regular object file,
2689 don't do anything special. See the longer description in
2690 _bfd_elf_adjust_dynamic_symbol, below. */
2691 if (h->u.weakdef->def_regular)
2692 h->u.weakdef = NULL;
2693 else
2694 {
2695 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2696
2697 while (h->root.type == bfd_link_hash_indirect)
2698 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2699
2700 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2701 || h->root.type == bfd_link_hash_defweak);
2702 BFD_ASSERT (weakdef->def_dynamic);
2703 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2704 || weakdef->root.type == bfd_link_hash_defweak);
2705 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2706 }
2707 }
2708
2709 return TRUE;
2710 }
2711
2712 /* Make the backend pick a good value for a dynamic symbol. This is
2713 called via elf_link_hash_traverse, and also calls itself
2714 recursively. */
2715
2716 static bfd_boolean
2717 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2718 {
2719 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2720 bfd *dynobj;
2721 const struct elf_backend_data *bed;
2722
2723 if (! is_elf_hash_table (eif->info->hash))
2724 return FALSE;
2725
2726 /* Ignore indirect symbols. These are added by the versioning code. */
2727 if (h->root.type == bfd_link_hash_indirect)
2728 return TRUE;
2729
2730 /* Fix the symbol flags. */
2731 if (! _bfd_elf_fix_symbol_flags (h, eif))
2732 return FALSE;
2733
2734 /* If this symbol does not require a PLT entry, and it is not
2735 defined by a dynamic object, or is not referenced by a regular
2736 object, ignore it. We do have to handle a weak defined symbol,
2737 even if no regular object refers to it, if we decided to add it
2738 to the dynamic symbol table. FIXME: Do we normally need to worry
2739 about symbols which are defined by one dynamic object and
2740 referenced by another one? */
2741 if (!h->needs_plt
2742 && h->type != STT_GNU_IFUNC
2743 && (h->def_regular
2744 || !h->def_dynamic
2745 || (!h->ref_regular
2746 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2747 {
2748 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2749 return TRUE;
2750 }
2751
2752 /* If we've already adjusted this symbol, don't do it again. This
2753 can happen via a recursive call. */
2754 if (h->dynamic_adjusted)
2755 return TRUE;
2756
2757 /* Don't look at this symbol again. Note that we must set this
2758 after checking the above conditions, because we may look at a
2759 symbol once, decide not to do anything, and then get called
2760 recursively later after REF_REGULAR is set below. */
2761 h->dynamic_adjusted = 1;
2762
2763 /* If this is a weak definition, and we know a real definition, and
2764 the real symbol is not itself defined by a regular object file,
2765 then get a good value for the real definition. We handle the
2766 real symbol first, for the convenience of the backend routine.
2767
2768 Note that there is a confusing case here. If the real definition
2769 is defined by a regular object file, we don't get the real symbol
2770 from the dynamic object, but we do get the weak symbol. If the
2771 processor backend uses a COPY reloc, then if some routine in the
2772 dynamic object changes the real symbol, we will not see that
2773 change in the corresponding weak symbol. This is the way other
2774 ELF linkers work as well, and seems to be a result of the shared
2775 library model.
2776
2777 I will clarify this issue. Most SVR4 shared libraries define the
2778 variable _timezone and define timezone as a weak synonym. The
2779 tzset call changes _timezone. If you write
2780 extern int timezone;
2781 int _timezone = 5;
2782 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2783 you might expect that, since timezone is a synonym for _timezone,
2784 the same number will print both times. However, if the processor
2785 backend uses a COPY reloc, then actually timezone will be copied
2786 into your process image, and, since you define _timezone
2787 yourself, _timezone will not. Thus timezone and _timezone will
2788 wind up at different memory locations. The tzset call will set
2789 _timezone, leaving timezone unchanged. */
2790
2791 if (h->u.weakdef != NULL)
2792 {
2793 /* If we get to this point, there is an implicit reference to
2794 H->U.WEAKDEF by a regular object file via the weak symbol H. */
2795 h->u.weakdef->ref_regular = 1;
2796
2797 /* Ensure that the backend adjust_dynamic_symbol function sees
2798 H->U.WEAKDEF before H by recursively calling ourselves. */
2799 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2800 return FALSE;
2801 }
2802
2803 /* If a symbol has no type and no size and does not require a PLT
2804 entry, then we are probably about to do the wrong thing here: we
2805 are probably going to create a COPY reloc for an empty object.
2806 This case can arise when a shared object is built with assembly
2807 code, and the assembly code fails to set the symbol type. */
2808 if (h->size == 0
2809 && h->type == STT_NOTYPE
2810 && !h->needs_plt)
2811 _bfd_error_handler
2812 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2813 h->root.root.string);
2814
2815 dynobj = elf_hash_table (eif->info)->dynobj;
2816 bed = get_elf_backend_data (dynobj);
2817
2818 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2819 {
2820 eif->failed = TRUE;
2821 return FALSE;
2822 }
2823
2824 return TRUE;
2825 }
2826
2827 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2828 DYNBSS. */
2829
2830 bfd_boolean
2831 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2832 struct elf_link_hash_entry *h,
2833 asection *dynbss)
2834 {
2835 unsigned int power_of_two;
2836 bfd_vma mask;
2837 asection *sec = h->root.u.def.section;
2838
2839 /* The section aligment of definition is the maximum alignment
2840 requirement of symbols defined in the section. Since we don't
2841 know the symbol alignment requirement, we start with the
2842 maximum alignment and check low bits of the symbol address
2843 for the minimum alignment. */
2844 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2845 mask = ((bfd_vma) 1 << power_of_two) - 1;
2846 while ((h->root.u.def.value & mask) != 0)
2847 {
2848 mask >>= 1;
2849 --power_of_two;
2850 }
2851
2852 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2853 dynbss))
2854 {
2855 /* Adjust the section alignment if needed. */
2856 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2857 power_of_two))
2858 return FALSE;
2859 }
2860
2861 /* We make sure that the symbol will be aligned properly. */
2862 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2863
2864 /* Define the symbol as being at this point in DYNBSS. */
2865 h->root.u.def.section = dynbss;
2866 h->root.u.def.value = dynbss->size;
2867
2868 /* Increment the size of DYNBSS to make room for the symbol. */
2869 dynbss->size += h->size;
2870
2871 /* No error if extern_protected_data is true. */
2872 if (h->protected_def
2873 && (!info->extern_protected_data
2874 || (info->extern_protected_data < 0
2875 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
2876 info->callbacks->einfo
2877 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2878 h->root.root.string);
2879
2880 return TRUE;
2881 }
2882
2883 /* Adjust all external symbols pointing into SEC_MERGE sections
2884 to reflect the object merging within the sections. */
2885
2886 static bfd_boolean
2887 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2888 {
2889 asection *sec;
2890
2891 if ((h->root.type == bfd_link_hash_defined
2892 || h->root.type == bfd_link_hash_defweak)
2893 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2894 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
2895 {
2896 bfd *output_bfd = (bfd *) data;
2897
2898 h->root.u.def.value =
2899 _bfd_merged_section_offset (output_bfd,
2900 &h->root.u.def.section,
2901 elf_section_data (sec)->sec_info,
2902 h->root.u.def.value);
2903 }
2904
2905 return TRUE;
2906 }
2907
2908 /* Returns false if the symbol referred to by H should be considered
2909 to resolve local to the current module, and true if it should be
2910 considered to bind dynamically. */
2911
2912 bfd_boolean
2913 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2914 struct bfd_link_info *info,
2915 bfd_boolean not_local_protected)
2916 {
2917 bfd_boolean binding_stays_local_p;
2918 const struct elf_backend_data *bed;
2919 struct elf_link_hash_table *hash_table;
2920
2921 if (h == NULL)
2922 return FALSE;
2923
2924 while (h->root.type == bfd_link_hash_indirect
2925 || h->root.type == bfd_link_hash_warning)
2926 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2927
2928 /* If it was forced local, then clearly it's not dynamic. */
2929 if (h->dynindx == -1)
2930 return FALSE;
2931 if (h->forced_local)
2932 return FALSE;
2933
2934 /* Identify the cases where name binding rules say that a
2935 visible symbol resolves locally. */
2936 binding_stays_local_p = (bfd_link_executable (info)
2937 || SYMBOLIC_BIND (info, h));
2938
2939 switch (ELF_ST_VISIBILITY (h->other))
2940 {
2941 case STV_INTERNAL:
2942 case STV_HIDDEN:
2943 return FALSE;
2944
2945 case STV_PROTECTED:
2946 hash_table = elf_hash_table (info);
2947 if (!is_elf_hash_table (hash_table))
2948 return FALSE;
2949
2950 bed = get_elf_backend_data (hash_table->dynobj);
2951
2952 /* Proper resolution for function pointer equality may require
2953 that these symbols perhaps be resolved dynamically, even though
2954 we should be resolving them to the current module. */
2955 if (!not_local_protected || !bed->is_function_type (h->type))
2956 binding_stays_local_p = TRUE;
2957 break;
2958
2959 default:
2960 break;
2961 }
2962
2963 /* If it isn't defined locally, then clearly it's dynamic. */
2964 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2965 return TRUE;
2966
2967 /* Otherwise, the symbol is dynamic if binding rules don't tell
2968 us that it remains local. */
2969 return !binding_stays_local_p;
2970 }
2971
2972 /* Return true if the symbol referred to by H should be considered
2973 to resolve local to the current module, and false otherwise. Differs
2974 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2975 undefined symbols. The two functions are virtually identical except
2976 for the place where forced_local and dynindx == -1 are tested. If
2977 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2978 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2979 the symbol is local only for defined symbols.
2980 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2981 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2982 treatment of undefined weak symbols. For those that do not make
2983 undefined weak symbols dynamic, both functions may return false. */
2984
2985 bfd_boolean
2986 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2987 struct bfd_link_info *info,
2988 bfd_boolean local_protected)
2989 {
2990 const struct elf_backend_data *bed;
2991 struct elf_link_hash_table *hash_table;
2992
2993 /* If it's a local sym, of course we resolve locally. */
2994 if (h == NULL)
2995 return TRUE;
2996
2997 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2998 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2999 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3000 return TRUE;
3001
3002 /* Common symbols that become definitions don't get the DEF_REGULAR
3003 flag set, so test it first, and don't bail out. */
3004 if (ELF_COMMON_DEF_P (h))
3005 /* Do nothing. */;
3006 /* If we don't have a definition in a regular file, then we can't
3007 resolve locally. The sym is either undefined or dynamic. */
3008 else if (!h->def_regular)
3009 return FALSE;
3010
3011 /* Forced local symbols resolve locally. */
3012 if (h->forced_local)
3013 return TRUE;
3014
3015 /* As do non-dynamic symbols. */
3016 if (h->dynindx == -1)
3017 return TRUE;
3018
3019 /* At this point, we know the symbol is defined and dynamic. In an
3020 executable it must resolve locally, likewise when building symbolic
3021 shared libraries. */
3022 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3023 return TRUE;
3024
3025 /* Now deal with defined dynamic symbols in shared libraries. Ones
3026 with default visibility might not resolve locally. */
3027 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3028 return FALSE;
3029
3030 hash_table = elf_hash_table (info);
3031 if (!is_elf_hash_table (hash_table))
3032 return TRUE;
3033
3034 bed = get_elf_backend_data (hash_table->dynobj);
3035
3036 /* If extern_protected_data is false, STV_PROTECTED non-function
3037 symbols are local. */
3038 if ((!info->extern_protected_data
3039 || (info->extern_protected_data < 0
3040 && !bed->extern_protected_data))
3041 && !bed->is_function_type (h->type))
3042 return TRUE;
3043
3044 /* Function pointer equality tests may require that STV_PROTECTED
3045 symbols be treated as dynamic symbols. If the address of a
3046 function not defined in an executable is set to that function's
3047 plt entry in the executable, then the address of the function in
3048 a shared library must also be the plt entry in the executable. */
3049 return local_protected;
3050 }
3051
3052 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3053 aligned. Returns the first TLS output section. */
3054
3055 struct bfd_section *
3056 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3057 {
3058 struct bfd_section *sec, *tls;
3059 unsigned int align = 0;
3060
3061 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3062 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3063 break;
3064 tls = sec;
3065
3066 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3067 if (sec->alignment_power > align)
3068 align = sec->alignment_power;
3069
3070 elf_hash_table (info)->tls_sec = tls;
3071
3072 /* Ensure the alignment of the first section is the largest alignment,
3073 so that the tls segment starts aligned. */
3074 if (tls != NULL)
3075 tls->alignment_power = align;
3076
3077 return tls;
3078 }
3079
3080 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3081 static bfd_boolean
3082 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3083 Elf_Internal_Sym *sym)
3084 {
3085 const struct elf_backend_data *bed;
3086
3087 /* Local symbols do not count, but target specific ones might. */
3088 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3089 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3090 return FALSE;
3091
3092 bed = get_elf_backend_data (abfd);
3093 /* Function symbols do not count. */
3094 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3095 return FALSE;
3096
3097 /* If the section is undefined, then so is the symbol. */
3098 if (sym->st_shndx == SHN_UNDEF)
3099 return FALSE;
3100
3101 /* If the symbol is defined in the common section, then
3102 it is a common definition and so does not count. */
3103 if (bed->common_definition (sym))
3104 return FALSE;
3105
3106 /* If the symbol is in a target specific section then we
3107 must rely upon the backend to tell us what it is. */
3108 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3109 /* FIXME - this function is not coded yet:
3110
3111 return _bfd_is_global_symbol_definition (abfd, sym);
3112
3113 Instead for now assume that the definition is not global,
3114 Even if this is wrong, at least the linker will behave
3115 in the same way that it used to do. */
3116 return FALSE;
3117
3118 return TRUE;
3119 }
3120
3121 /* Search the symbol table of the archive element of the archive ABFD
3122 whose archive map contains a mention of SYMDEF, and determine if
3123 the symbol is defined in this element. */
3124 static bfd_boolean
3125 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3126 {
3127 Elf_Internal_Shdr * hdr;
3128 size_t symcount;
3129 size_t extsymcount;
3130 size_t extsymoff;
3131 Elf_Internal_Sym *isymbuf;
3132 Elf_Internal_Sym *isym;
3133 Elf_Internal_Sym *isymend;
3134 bfd_boolean result;
3135
3136 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3137 if (abfd == NULL)
3138 return FALSE;
3139
3140 if (! bfd_check_format (abfd, bfd_object))
3141 return FALSE;
3142
3143 /* Select the appropriate symbol table. If we don't know if the
3144 object file is an IR object, give linker LTO plugin a chance to
3145 get the correct symbol table. */
3146 if (abfd->plugin_format == bfd_plugin_yes
3147 #if BFD_SUPPORTS_PLUGINS
3148 || (abfd->plugin_format == bfd_plugin_unknown
3149 && bfd_link_plugin_object_p (abfd))
3150 #endif
3151 )
3152 {
3153 /* Use the IR symbol table if the object has been claimed by
3154 plugin. */
3155 abfd = abfd->plugin_dummy_bfd;
3156 hdr = &elf_tdata (abfd)->symtab_hdr;
3157 }
3158 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3159 hdr = &elf_tdata (abfd)->symtab_hdr;
3160 else
3161 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3162
3163 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3164
3165 /* The sh_info field of the symtab header tells us where the
3166 external symbols start. We don't care about the local symbols. */
3167 if (elf_bad_symtab (abfd))
3168 {
3169 extsymcount = symcount;
3170 extsymoff = 0;
3171 }
3172 else
3173 {
3174 extsymcount = symcount - hdr->sh_info;
3175 extsymoff = hdr->sh_info;
3176 }
3177
3178 if (extsymcount == 0)
3179 return FALSE;
3180
3181 /* Read in the symbol table. */
3182 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3183 NULL, NULL, NULL);
3184 if (isymbuf == NULL)
3185 return FALSE;
3186
3187 /* Scan the symbol table looking for SYMDEF. */
3188 result = FALSE;
3189 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3190 {
3191 const char *name;
3192
3193 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3194 isym->st_name);
3195 if (name == NULL)
3196 break;
3197
3198 if (strcmp (name, symdef->name) == 0)
3199 {
3200 result = is_global_data_symbol_definition (abfd, isym);
3201 break;
3202 }
3203 }
3204
3205 free (isymbuf);
3206
3207 return result;
3208 }
3209 \f
3210 /* Add an entry to the .dynamic table. */
3211
3212 bfd_boolean
3213 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3214 bfd_vma tag,
3215 bfd_vma val)
3216 {
3217 struct elf_link_hash_table *hash_table;
3218 const struct elf_backend_data *bed;
3219 asection *s;
3220 bfd_size_type newsize;
3221 bfd_byte *newcontents;
3222 Elf_Internal_Dyn dyn;
3223
3224 hash_table = elf_hash_table (info);
3225 if (! is_elf_hash_table (hash_table))
3226 return FALSE;
3227
3228 bed = get_elf_backend_data (hash_table->dynobj);
3229 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3230 BFD_ASSERT (s != NULL);
3231
3232 newsize = s->size + bed->s->sizeof_dyn;
3233 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3234 if (newcontents == NULL)
3235 return FALSE;
3236
3237 dyn.d_tag = tag;
3238 dyn.d_un.d_val = val;
3239 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3240
3241 s->size = newsize;
3242 s->contents = newcontents;
3243
3244 return TRUE;
3245 }
3246
3247 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3248 otherwise just check whether one already exists. Returns -1 on error,
3249 1 if a DT_NEEDED tag already exists, and 0 on success. */
3250
3251 static int
3252 elf_add_dt_needed_tag (bfd *abfd,
3253 struct bfd_link_info *info,
3254 const char *soname,
3255 bfd_boolean do_it)
3256 {
3257 struct elf_link_hash_table *hash_table;
3258 size_t strindex;
3259
3260 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3261 return -1;
3262
3263 hash_table = elf_hash_table (info);
3264 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3265 if (strindex == (size_t) -1)
3266 return -1;
3267
3268 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3269 {
3270 asection *sdyn;
3271 const struct elf_backend_data *bed;
3272 bfd_byte *extdyn;
3273
3274 bed = get_elf_backend_data (hash_table->dynobj);
3275 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3276 if (sdyn != NULL)
3277 for (extdyn = sdyn->contents;
3278 extdyn < sdyn->contents + sdyn->size;
3279 extdyn += bed->s->sizeof_dyn)
3280 {
3281 Elf_Internal_Dyn dyn;
3282
3283 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3284 if (dyn.d_tag == DT_NEEDED
3285 && dyn.d_un.d_val == strindex)
3286 {
3287 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3288 return 1;
3289 }
3290 }
3291 }
3292
3293 if (do_it)
3294 {
3295 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3296 return -1;
3297
3298 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3299 return -1;
3300 }
3301 else
3302 /* We were just checking for existence of the tag. */
3303 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3304
3305 return 0;
3306 }
3307
3308 /* Return true if SONAME is on the needed list between NEEDED and STOP
3309 (or the end of list if STOP is NULL), and needed by a library that
3310 will be loaded. */
3311
3312 static bfd_boolean
3313 on_needed_list (const char *soname,
3314 struct bfd_link_needed_list *needed,
3315 struct bfd_link_needed_list *stop)
3316 {
3317 struct bfd_link_needed_list *look;
3318 for (look = needed; look != stop; look = look->next)
3319 if (strcmp (soname, look->name) == 0
3320 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3321 /* If needed by a library that itself is not directly
3322 needed, recursively check whether that library is
3323 indirectly needed. Since we add DT_NEEDED entries to
3324 the end of the list, library dependencies appear after
3325 the library. Therefore search prior to the current
3326 LOOK, preventing possible infinite recursion. */
3327 || on_needed_list (elf_dt_name (look->by), needed, look)))
3328 return TRUE;
3329
3330 return FALSE;
3331 }
3332
3333 /* Sort symbol by value, section, and size. */
3334 static int
3335 elf_sort_symbol (const void *arg1, const void *arg2)
3336 {
3337 const struct elf_link_hash_entry *h1;
3338 const struct elf_link_hash_entry *h2;
3339 bfd_signed_vma vdiff;
3340
3341 h1 = *(const struct elf_link_hash_entry **) arg1;
3342 h2 = *(const struct elf_link_hash_entry **) arg2;
3343 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3344 if (vdiff != 0)
3345 return vdiff > 0 ? 1 : -1;
3346 else
3347 {
3348 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3349 if (sdiff != 0)
3350 return sdiff > 0 ? 1 : -1;
3351 }
3352 vdiff = h1->size - h2->size;
3353 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3354 }
3355
3356 /* This function is used to adjust offsets into .dynstr for
3357 dynamic symbols. This is called via elf_link_hash_traverse. */
3358
3359 static bfd_boolean
3360 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3361 {
3362 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3363
3364 if (h->dynindx != -1)
3365 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3366 return TRUE;
3367 }
3368
3369 /* Assign string offsets in .dynstr, update all structures referencing
3370 them. */
3371
3372 static bfd_boolean
3373 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3374 {
3375 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3376 struct elf_link_local_dynamic_entry *entry;
3377 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3378 bfd *dynobj = hash_table->dynobj;
3379 asection *sdyn;
3380 bfd_size_type size;
3381 const struct elf_backend_data *bed;
3382 bfd_byte *extdyn;
3383
3384 _bfd_elf_strtab_finalize (dynstr);
3385 size = _bfd_elf_strtab_size (dynstr);
3386
3387 bed = get_elf_backend_data (dynobj);
3388 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3389 BFD_ASSERT (sdyn != NULL);
3390
3391 /* Update all .dynamic entries referencing .dynstr strings. */
3392 for (extdyn = sdyn->contents;
3393 extdyn < sdyn->contents + sdyn->size;
3394 extdyn += bed->s->sizeof_dyn)
3395 {
3396 Elf_Internal_Dyn dyn;
3397
3398 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3399 switch (dyn.d_tag)
3400 {
3401 case DT_STRSZ:
3402 dyn.d_un.d_val = size;
3403 break;
3404 case DT_NEEDED:
3405 case DT_SONAME:
3406 case DT_RPATH:
3407 case DT_RUNPATH:
3408 case DT_FILTER:
3409 case DT_AUXILIARY:
3410 case DT_AUDIT:
3411 case DT_DEPAUDIT:
3412 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3413 break;
3414 default:
3415 continue;
3416 }
3417 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3418 }
3419
3420 /* Now update local dynamic symbols. */
3421 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3422 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3423 entry->isym.st_name);
3424
3425 /* And the rest of dynamic symbols. */
3426 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3427
3428 /* Adjust version definitions. */
3429 if (elf_tdata (output_bfd)->cverdefs)
3430 {
3431 asection *s;
3432 bfd_byte *p;
3433 size_t i;
3434 Elf_Internal_Verdef def;
3435 Elf_Internal_Verdaux defaux;
3436
3437 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3438 p = s->contents;
3439 do
3440 {
3441 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3442 &def);
3443 p += sizeof (Elf_External_Verdef);
3444 if (def.vd_aux != sizeof (Elf_External_Verdef))
3445 continue;
3446 for (i = 0; i < def.vd_cnt; ++i)
3447 {
3448 _bfd_elf_swap_verdaux_in (output_bfd,
3449 (Elf_External_Verdaux *) p, &defaux);
3450 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3451 defaux.vda_name);
3452 _bfd_elf_swap_verdaux_out (output_bfd,
3453 &defaux, (Elf_External_Verdaux *) p);
3454 p += sizeof (Elf_External_Verdaux);
3455 }
3456 }
3457 while (def.vd_next);
3458 }
3459
3460 /* Adjust version references. */
3461 if (elf_tdata (output_bfd)->verref)
3462 {
3463 asection *s;
3464 bfd_byte *p;
3465 size_t i;
3466 Elf_Internal_Verneed need;
3467 Elf_Internal_Vernaux needaux;
3468
3469 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3470 p = s->contents;
3471 do
3472 {
3473 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3474 &need);
3475 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3476 _bfd_elf_swap_verneed_out (output_bfd, &need,
3477 (Elf_External_Verneed *) p);
3478 p += sizeof (Elf_External_Verneed);
3479 for (i = 0; i < need.vn_cnt; ++i)
3480 {
3481 _bfd_elf_swap_vernaux_in (output_bfd,
3482 (Elf_External_Vernaux *) p, &needaux);
3483 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3484 needaux.vna_name);
3485 _bfd_elf_swap_vernaux_out (output_bfd,
3486 &needaux,
3487 (Elf_External_Vernaux *) p);
3488 p += sizeof (Elf_External_Vernaux);
3489 }
3490 }
3491 while (need.vn_next);
3492 }
3493
3494 return TRUE;
3495 }
3496 \f
3497 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3498 The default is to only match when the INPUT and OUTPUT are exactly
3499 the same target. */
3500
3501 bfd_boolean
3502 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3503 const bfd_target *output)
3504 {
3505 return input == output;
3506 }
3507
3508 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3509 This version is used when different targets for the same architecture
3510 are virtually identical. */
3511
3512 bfd_boolean
3513 _bfd_elf_relocs_compatible (const bfd_target *input,
3514 const bfd_target *output)
3515 {
3516 const struct elf_backend_data *obed, *ibed;
3517
3518 if (input == output)
3519 return TRUE;
3520
3521 ibed = xvec_get_elf_backend_data (input);
3522 obed = xvec_get_elf_backend_data (output);
3523
3524 if (ibed->arch != obed->arch)
3525 return FALSE;
3526
3527 /* If both backends are using this function, deem them compatible. */
3528 return ibed->relocs_compatible == obed->relocs_compatible;
3529 }
3530
3531 /* Make a special call to the linker "notice" function to tell it that
3532 we are about to handle an as-needed lib, or have finished
3533 processing the lib. */
3534
3535 bfd_boolean
3536 _bfd_elf_notice_as_needed (bfd *ibfd,
3537 struct bfd_link_info *info,
3538 enum notice_asneeded_action act)
3539 {
3540 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3541 }
3542
3543 /* Check relocations an ELF object file. */
3544
3545 bfd_boolean
3546 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3547 {
3548 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3549 struct elf_link_hash_table *htab = elf_hash_table (info);
3550
3551 /* If this object is the same format as the output object, and it is
3552 not a shared library, then let the backend look through the
3553 relocs.
3554
3555 This is required to build global offset table entries and to
3556 arrange for dynamic relocs. It is not required for the
3557 particular common case of linking non PIC code, even when linking
3558 against shared libraries, but unfortunately there is no way of
3559 knowing whether an object file has been compiled PIC or not.
3560 Looking through the relocs is not particularly time consuming.
3561 The problem is that we must either (1) keep the relocs in memory,
3562 which causes the linker to require additional runtime memory or
3563 (2) read the relocs twice from the input file, which wastes time.
3564 This would be a good case for using mmap.
3565
3566 I have no idea how to handle linking PIC code into a file of a
3567 different format. It probably can't be done. */
3568 if ((abfd->flags & DYNAMIC) == 0
3569 && is_elf_hash_table (htab)
3570 && bed->check_relocs != NULL
3571 && elf_object_id (abfd) == elf_hash_table_id (htab)
3572 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3573 {
3574 asection *o;
3575
3576 for (o = abfd->sections; o != NULL; o = o->next)
3577 {
3578 Elf_Internal_Rela *internal_relocs;
3579 bfd_boolean ok;
3580
3581 /* Don't check relocations in excluded sections. */
3582 if ((o->flags & SEC_RELOC) == 0
3583 || (o->flags & SEC_EXCLUDE) != 0
3584 || o->reloc_count == 0
3585 || ((info->strip == strip_all || info->strip == strip_debugger)
3586 && (o->flags & SEC_DEBUGGING) != 0)
3587 || bfd_is_abs_section (o->output_section))
3588 continue;
3589
3590 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3591 info->keep_memory);
3592 if (internal_relocs == NULL)
3593 return FALSE;
3594
3595 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3596
3597 if (elf_section_data (o)->relocs != internal_relocs)
3598 free (internal_relocs);
3599
3600 if (! ok)
3601 return FALSE;
3602 }
3603 }
3604
3605 return TRUE;
3606 }
3607
3608 /* Add symbols from an ELF object file to the linker hash table. */
3609
3610 static bfd_boolean
3611 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3612 {
3613 Elf_Internal_Ehdr *ehdr;
3614 Elf_Internal_Shdr *hdr;
3615 size_t symcount;
3616 size_t extsymcount;
3617 size_t extsymoff;
3618 struct elf_link_hash_entry **sym_hash;
3619 bfd_boolean dynamic;
3620 Elf_External_Versym *extversym = NULL;
3621 Elf_External_Versym *ever;
3622 struct elf_link_hash_entry *weaks;
3623 struct elf_link_hash_entry **nondeflt_vers = NULL;
3624 size_t nondeflt_vers_cnt = 0;
3625 Elf_Internal_Sym *isymbuf = NULL;
3626 Elf_Internal_Sym *isym;
3627 Elf_Internal_Sym *isymend;
3628 const struct elf_backend_data *bed;
3629 bfd_boolean add_needed;
3630 struct elf_link_hash_table *htab;
3631 bfd_size_type amt;
3632 void *alloc_mark = NULL;
3633 struct bfd_hash_entry **old_table = NULL;
3634 unsigned int old_size = 0;
3635 unsigned int old_count = 0;
3636 void *old_tab = NULL;
3637 void *old_ent;
3638 struct bfd_link_hash_entry *old_undefs = NULL;
3639 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3640 void *old_strtab = NULL;
3641 size_t tabsize = 0;
3642 asection *s;
3643 bfd_boolean just_syms;
3644
3645 htab = elf_hash_table (info);
3646 bed = get_elf_backend_data (abfd);
3647
3648 if ((abfd->flags & DYNAMIC) == 0)
3649 dynamic = FALSE;
3650 else
3651 {
3652 dynamic = TRUE;
3653
3654 /* You can't use -r against a dynamic object. Also, there's no
3655 hope of using a dynamic object which does not exactly match
3656 the format of the output file. */
3657 if (bfd_link_relocatable (info)
3658 || !is_elf_hash_table (htab)
3659 || info->output_bfd->xvec != abfd->xvec)
3660 {
3661 if (bfd_link_relocatable (info))
3662 bfd_set_error (bfd_error_invalid_operation);
3663 else
3664 bfd_set_error (bfd_error_wrong_format);
3665 goto error_return;
3666 }
3667 }
3668
3669 ehdr = elf_elfheader (abfd);
3670 if (info->warn_alternate_em
3671 && bed->elf_machine_code != ehdr->e_machine
3672 && ((bed->elf_machine_alt1 != 0
3673 && ehdr->e_machine == bed->elf_machine_alt1)
3674 || (bed->elf_machine_alt2 != 0
3675 && ehdr->e_machine == bed->elf_machine_alt2)))
3676 info->callbacks->einfo
3677 /* xgettext:c-format */
3678 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3679 ehdr->e_machine, abfd, bed->elf_machine_code);
3680
3681 /* As a GNU extension, any input sections which are named
3682 .gnu.warning.SYMBOL are treated as warning symbols for the given
3683 symbol. This differs from .gnu.warning sections, which generate
3684 warnings when they are included in an output file. */
3685 /* PR 12761: Also generate this warning when building shared libraries. */
3686 for (s = abfd->sections; s != NULL; s = s->next)
3687 {
3688 const char *name;
3689
3690 name = bfd_get_section_name (abfd, s);
3691 if (CONST_STRNEQ (name, ".gnu.warning."))
3692 {
3693 char *msg;
3694 bfd_size_type sz;
3695
3696 name += sizeof ".gnu.warning." - 1;
3697
3698 /* If this is a shared object, then look up the symbol
3699 in the hash table. If it is there, and it is already
3700 been defined, then we will not be using the entry
3701 from this shared object, so we don't need to warn.
3702 FIXME: If we see the definition in a regular object
3703 later on, we will warn, but we shouldn't. The only
3704 fix is to keep track of what warnings we are supposed
3705 to emit, and then handle them all at the end of the
3706 link. */
3707 if (dynamic)
3708 {
3709 struct elf_link_hash_entry *h;
3710
3711 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3712
3713 /* FIXME: What about bfd_link_hash_common? */
3714 if (h != NULL
3715 && (h->root.type == bfd_link_hash_defined
3716 || h->root.type == bfd_link_hash_defweak))
3717 continue;
3718 }
3719
3720 sz = s->size;
3721 msg = (char *) bfd_alloc (abfd, sz + 1);
3722 if (msg == NULL)
3723 goto error_return;
3724
3725 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3726 goto error_return;
3727
3728 msg[sz] = '\0';
3729
3730 if (! (_bfd_generic_link_add_one_symbol
3731 (info, abfd, name, BSF_WARNING, s, 0, msg,
3732 FALSE, bed->collect, NULL)))
3733 goto error_return;
3734
3735 if (bfd_link_executable (info))
3736 {
3737 /* Clobber the section size so that the warning does
3738 not get copied into the output file. */
3739 s->size = 0;
3740
3741 /* Also set SEC_EXCLUDE, so that symbols defined in
3742 the warning section don't get copied to the output. */
3743 s->flags |= SEC_EXCLUDE;
3744 }
3745 }
3746 }
3747
3748 just_syms = ((s = abfd->sections) != NULL
3749 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3750
3751 add_needed = TRUE;
3752 if (! dynamic)
3753 {
3754 /* If we are creating a shared library, create all the dynamic
3755 sections immediately. We need to attach them to something,
3756 so we attach them to this BFD, provided it is the right
3757 format and is not from ld --just-symbols. Always create the
3758 dynamic sections for -E/--dynamic-list. FIXME: If there
3759 are no input BFD's of the same format as the output, we can't
3760 make a shared library. */
3761 if (!just_syms
3762 && (bfd_link_pic (info)
3763 || (!bfd_link_relocatable (info)
3764 && (info->export_dynamic || info->dynamic)))
3765 && is_elf_hash_table (htab)
3766 && info->output_bfd->xvec == abfd->xvec
3767 && !htab->dynamic_sections_created)
3768 {
3769 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3770 goto error_return;
3771 }
3772 }
3773 else if (!is_elf_hash_table (htab))
3774 goto error_return;
3775 else
3776 {
3777 const char *soname = NULL;
3778 char *audit = NULL;
3779 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3780 int ret;
3781
3782 /* ld --just-symbols and dynamic objects don't mix very well.
3783 ld shouldn't allow it. */
3784 if (just_syms)
3785 abort ();
3786
3787 /* If this dynamic lib was specified on the command line with
3788 --as-needed in effect, then we don't want to add a DT_NEEDED
3789 tag unless the lib is actually used. Similary for libs brought
3790 in by another lib's DT_NEEDED. When --no-add-needed is used
3791 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3792 any dynamic library in DT_NEEDED tags in the dynamic lib at
3793 all. */
3794 add_needed = (elf_dyn_lib_class (abfd)
3795 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3796 | DYN_NO_NEEDED)) == 0;
3797
3798 s = bfd_get_section_by_name (abfd, ".dynamic");
3799 if (s != NULL)
3800 {
3801 bfd_byte *dynbuf;
3802 bfd_byte *extdyn;
3803 unsigned int elfsec;
3804 unsigned long shlink;
3805
3806 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3807 {
3808 error_free_dyn:
3809 free (dynbuf);
3810 goto error_return;
3811 }
3812
3813 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3814 if (elfsec == SHN_BAD)
3815 goto error_free_dyn;
3816 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3817
3818 for (extdyn = dynbuf;
3819 extdyn < dynbuf + s->size;
3820 extdyn += bed->s->sizeof_dyn)
3821 {
3822 Elf_Internal_Dyn dyn;
3823
3824 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3825 if (dyn.d_tag == DT_SONAME)
3826 {
3827 unsigned int tagv = dyn.d_un.d_val;
3828 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3829 if (soname == NULL)
3830 goto error_free_dyn;
3831 }
3832 if (dyn.d_tag == DT_NEEDED)
3833 {
3834 struct bfd_link_needed_list *n, **pn;
3835 char *fnm, *anm;
3836 unsigned int tagv = dyn.d_un.d_val;
3837
3838 amt = sizeof (struct bfd_link_needed_list);
3839 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3840 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3841 if (n == NULL || fnm == NULL)
3842 goto error_free_dyn;
3843 amt = strlen (fnm) + 1;
3844 anm = (char *) bfd_alloc (abfd, amt);
3845 if (anm == NULL)
3846 goto error_free_dyn;
3847 memcpy (anm, fnm, amt);
3848 n->name = anm;
3849 n->by = abfd;
3850 n->next = NULL;
3851 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3852 ;
3853 *pn = n;
3854 }
3855 if (dyn.d_tag == DT_RUNPATH)
3856 {
3857 struct bfd_link_needed_list *n, **pn;
3858 char *fnm, *anm;
3859 unsigned int tagv = dyn.d_un.d_val;
3860
3861 amt = sizeof (struct bfd_link_needed_list);
3862 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3863 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3864 if (n == NULL || fnm == NULL)
3865 goto error_free_dyn;
3866 amt = strlen (fnm) + 1;
3867 anm = (char *) bfd_alloc (abfd, amt);
3868 if (anm == NULL)
3869 goto error_free_dyn;
3870 memcpy (anm, fnm, amt);
3871 n->name = anm;
3872 n->by = abfd;
3873 n->next = NULL;
3874 for (pn = & runpath;
3875 *pn != NULL;
3876 pn = &(*pn)->next)
3877 ;
3878 *pn = n;
3879 }
3880 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3881 if (!runpath && dyn.d_tag == DT_RPATH)
3882 {
3883 struct bfd_link_needed_list *n, **pn;
3884 char *fnm, *anm;
3885 unsigned int tagv = dyn.d_un.d_val;
3886
3887 amt = sizeof (struct bfd_link_needed_list);
3888 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3889 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3890 if (n == NULL || fnm == NULL)
3891 goto error_free_dyn;
3892 amt = strlen (fnm) + 1;
3893 anm = (char *) bfd_alloc (abfd, amt);
3894 if (anm == NULL)
3895 goto error_free_dyn;
3896 memcpy (anm, fnm, amt);
3897 n->name = anm;
3898 n->by = abfd;
3899 n->next = NULL;
3900 for (pn = & rpath;
3901 *pn != NULL;
3902 pn = &(*pn)->next)
3903 ;
3904 *pn = n;
3905 }
3906 if (dyn.d_tag == DT_AUDIT)
3907 {
3908 unsigned int tagv = dyn.d_un.d_val;
3909 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3910 }
3911 }
3912
3913 free (dynbuf);
3914 }
3915
3916 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3917 frees all more recently bfd_alloc'd blocks as well. */
3918 if (runpath)
3919 rpath = runpath;
3920
3921 if (rpath)
3922 {
3923 struct bfd_link_needed_list **pn;
3924 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3925 ;
3926 *pn = rpath;
3927 }
3928
3929 /* We do not want to include any of the sections in a dynamic
3930 object in the output file. We hack by simply clobbering the
3931 list of sections in the BFD. This could be handled more
3932 cleanly by, say, a new section flag; the existing
3933 SEC_NEVER_LOAD flag is not the one we want, because that one
3934 still implies that the section takes up space in the output
3935 file. */
3936 bfd_section_list_clear (abfd);
3937
3938 /* Find the name to use in a DT_NEEDED entry that refers to this
3939 object. If the object has a DT_SONAME entry, we use it.
3940 Otherwise, if the generic linker stuck something in
3941 elf_dt_name, we use that. Otherwise, we just use the file
3942 name. */
3943 if (soname == NULL || *soname == '\0')
3944 {
3945 soname = elf_dt_name (abfd);
3946 if (soname == NULL || *soname == '\0')
3947 soname = bfd_get_filename (abfd);
3948 }
3949
3950 /* Save the SONAME because sometimes the linker emulation code
3951 will need to know it. */
3952 elf_dt_name (abfd) = soname;
3953
3954 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3955 if (ret < 0)
3956 goto error_return;
3957
3958 /* If we have already included this dynamic object in the
3959 link, just ignore it. There is no reason to include a
3960 particular dynamic object more than once. */
3961 if (ret > 0)
3962 return TRUE;
3963
3964 /* Save the DT_AUDIT entry for the linker emulation code. */
3965 elf_dt_audit (abfd) = audit;
3966 }
3967
3968 /* If this is a dynamic object, we always link against the .dynsym
3969 symbol table, not the .symtab symbol table. The dynamic linker
3970 will only see the .dynsym symbol table, so there is no reason to
3971 look at .symtab for a dynamic object. */
3972
3973 if (! dynamic || elf_dynsymtab (abfd) == 0)
3974 hdr = &elf_tdata (abfd)->symtab_hdr;
3975 else
3976 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3977
3978 symcount = hdr->sh_size / bed->s->sizeof_sym;
3979
3980 /* The sh_info field of the symtab header tells us where the
3981 external symbols start. We don't care about the local symbols at
3982 this point. */
3983 if (elf_bad_symtab (abfd))
3984 {
3985 extsymcount = symcount;
3986 extsymoff = 0;
3987 }
3988 else
3989 {
3990 extsymcount = symcount - hdr->sh_info;
3991 extsymoff = hdr->sh_info;
3992 }
3993
3994 sym_hash = elf_sym_hashes (abfd);
3995 if (extsymcount != 0)
3996 {
3997 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3998 NULL, NULL, NULL);
3999 if (isymbuf == NULL)
4000 goto error_return;
4001
4002 if (sym_hash == NULL)
4003 {
4004 /* We store a pointer to the hash table entry for each
4005 external symbol. */
4006 amt = extsymcount;
4007 amt *= sizeof (struct elf_link_hash_entry *);
4008 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4009 if (sym_hash == NULL)
4010 goto error_free_sym;
4011 elf_sym_hashes (abfd) = sym_hash;
4012 }
4013 }
4014
4015 if (dynamic)
4016 {
4017 /* Read in any version definitions. */
4018 if (!_bfd_elf_slurp_version_tables (abfd,
4019 info->default_imported_symver))
4020 goto error_free_sym;
4021
4022 /* Read in the symbol versions, but don't bother to convert them
4023 to internal format. */
4024 if (elf_dynversym (abfd) != 0)
4025 {
4026 Elf_Internal_Shdr *versymhdr;
4027
4028 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4029 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4030 if (extversym == NULL)
4031 goto error_free_sym;
4032 amt = versymhdr->sh_size;
4033 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4034 || bfd_bread (extversym, amt, abfd) != amt)
4035 goto error_free_vers;
4036 }
4037 }
4038
4039 /* If we are loading an as-needed shared lib, save the symbol table
4040 state before we start adding symbols. If the lib turns out
4041 to be unneeded, restore the state. */
4042 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4043 {
4044 unsigned int i;
4045 size_t entsize;
4046
4047 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4048 {
4049 struct bfd_hash_entry *p;
4050 struct elf_link_hash_entry *h;
4051
4052 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4053 {
4054 h = (struct elf_link_hash_entry *) p;
4055 entsize += htab->root.table.entsize;
4056 if (h->root.type == bfd_link_hash_warning)
4057 entsize += htab->root.table.entsize;
4058 }
4059 }
4060
4061 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4062 old_tab = bfd_malloc (tabsize + entsize);
4063 if (old_tab == NULL)
4064 goto error_free_vers;
4065
4066 /* Remember the current objalloc pointer, so that all mem for
4067 symbols added can later be reclaimed. */
4068 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4069 if (alloc_mark == NULL)
4070 goto error_free_vers;
4071
4072 /* Make a special call to the linker "notice" function to
4073 tell it that we are about to handle an as-needed lib. */
4074 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4075 goto error_free_vers;
4076
4077 /* Clone the symbol table. Remember some pointers into the
4078 symbol table, and dynamic symbol count. */
4079 old_ent = (char *) old_tab + tabsize;
4080 memcpy (old_tab, htab->root.table.table, tabsize);
4081 old_undefs = htab->root.undefs;
4082 old_undefs_tail = htab->root.undefs_tail;
4083 old_table = htab->root.table.table;
4084 old_size = htab->root.table.size;
4085 old_count = htab->root.table.count;
4086 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4087 if (old_strtab == NULL)
4088 goto error_free_vers;
4089
4090 for (i = 0; i < htab->root.table.size; i++)
4091 {
4092 struct bfd_hash_entry *p;
4093 struct elf_link_hash_entry *h;
4094
4095 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4096 {
4097 memcpy (old_ent, p, htab->root.table.entsize);
4098 old_ent = (char *) old_ent + htab->root.table.entsize;
4099 h = (struct elf_link_hash_entry *) p;
4100 if (h->root.type == bfd_link_hash_warning)
4101 {
4102 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4103 old_ent = (char *) old_ent + htab->root.table.entsize;
4104 }
4105 }
4106 }
4107 }
4108
4109 weaks = NULL;
4110 ever = extversym != NULL ? extversym + extsymoff : NULL;
4111 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4112 isym < isymend;
4113 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4114 {
4115 int bind;
4116 bfd_vma value;
4117 asection *sec, *new_sec;
4118 flagword flags;
4119 const char *name;
4120 struct elf_link_hash_entry *h;
4121 struct elf_link_hash_entry *hi;
4122 bfd_boolean definition;
4123 bfd_boolean size_change_ok;
4124 bfd_boolean type_change_ok;
4125 bfd_boolean new_weakdef;
4126 bfd_boolean new_weak;
4127 bfd_boolean old_weak;
4128 bfd_boolean override;
4129 bfd_boolean common;
4130 bfd_boolean discarded;
4131 unsigned int old_alignment;
4132 bfd *old_bfd;
4133 bfd_boolean matched;
4134
4135 override = FALSE;
4136
4137 flags = BSF_NO_FLAGS;
4138 sec = NULL;
4139 value = isym->st_value;
4140 common = bed->common_definition (isym);
4141 discarded = FALSE;
4142
4143 bind = ELF_ST_BIND (isym->st_info);
4144 switch (bind)
4145 {
4146 case STB_LOCAL:
4147 /* This should be impossible, since ELF requires that all
4148 global symbols follow all local symbols, and that sh_info
4149 point to the first global symbol. Unfortunately, Irix 5
4150 screws this up. */
4151 continue;
4152
4153 case STB_GLOBAL:
4154 if (isym->st_shndx != SHN_UNDEF && !common)
4155 flags = BSF_GLOBAL;
4156 break;
4157
4158 case STB_WEAK:
4159 flags = BSF_WEAK;
4160 break;
4161
4162 case STB_GNU_UNIQUE:
4163 flags = BSF_GNU_UNIQUE;
4164 break;
4165
4166 default:
4167 /* Leave it up to the processor backend. */
4168 break;
4169 }
4170
4171 if (isym->st_shndx == SHN_UNDEF)
4172 sec = bfd_und_section_ptr;
4173 else if (isym->st_shndx == SHN_ABS)
4174 sec = bfd_abs_section_ptr;
4175 else if (isym->st_shndx == SHN_COMMON)
4176 {
4177 sec = bfd_com_section_ptr;
4178 /* What ELF calls the size we call the value. What ELF
4179 calls the value we call the alignment. */
4180 value = isym->st_size;
4181 }
4182 else
4183 {
4184 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4185 if (sec == NULL)
4186 sec = bfd_abs_section_ptr;
4187 else if (discarded_section (sec))
4188 {
4189 /* Symbols from discarded section are undefined. We keep
4190 its visibility. */
4191 sec = bfd_und_section_ptr;
4192 discarded = TRUE;
4193 isym->st_shndx = SHN_UNDEF;
4194 }
4195 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4196 value -= sec->vma;
4197 }
4198
4199 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4200 isym->st_name);
4201 if (name == NULL)
4202 goto error_free_vers;
4203
4204 if (isym->st_shndx == SHN_COMMON
4205 && (abfd->flags & BFD_PLUGIN) != 0)
4206 {
4207 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4208
4209 if (xc == NULL)
4210 {
4211 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4212 | SEC_EXCLUDE);
4213 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4214 if (xc == NULL)
4215 goto error_free_vers;
4216 }
4217 sec = xc;
4218 }
4219 else if (isym->st_shndx == SHN_COMMON
4220 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4221 && !bfd_link_relocatable (info))
4222 {
4223 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4224
4225 if (tcomm == NULL)
4226 {
4227 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4228 | SEC_LINKER_CREATED);
4229 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4230 if (tcomm == NULL)
4231 goto error_free_vers;
4232 }
4233 sec = tcomm;
4234 }
4235 else if (bed->elf_add_symbol_hook)
4236 {
4237 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4238 &sec, &value))
4239 goto error_free_vers;
4240
4241 /* The hook function sets the name to NULL if this symbol
4242 should be skipped for some reason. */
4243 if (name == NULL)
4244 continue;
4245 }
4246
4247 /* Sanity check that all possibilities were handled. */
4248 if (sec == NULL)
4249 {
4250 bfd_set_error (bfd_error_bad_value);
4251 goto error_free_vers;
4252 }
4253
4254 /* Silently discard TLS symbols from --just-syms. There's
4255 no way to combine a static TLS block with a new TLS block
4256 for this executable. */
4257 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4258 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4259 continue;
4260
4261 if (bfd_is_und_section (sec)
4262 || bfd_is_com_section (sec))
4263 definition = FALSE;
4264 else
4265 definition = TRUE;
4266
4267 size_change_ok = FALSE;
4268 type_change_ok = bed->type_change_ok;
4269 old_weak = FALSE;
4270 matched = FALSE;
4271 old_alignment = 0;
4272 old_bfd = NULL;
4273 new_sec = sec;
4274
4275 if (is_elf_hash_table (htab))
4276 {
4277 Elf_Internal_Versym iver;
4278 unsigned int vernum = 0;
4279 bfd_boolean skip;
4280
4281 if (ever == NULL)
4282 {
4283 if (info->default_imported_symver)
4284 /* Use the default symbol version created earlier. */
4285 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4286 else
4287 iver.vs_vers = 0;
4288 }
4289 else
4290 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4291
4292 vernum = iver.vs_vers & VERSYM_VERSION;
4293
4294 /* If this is a hidden symbol, or if it is not version
4295 1, we append the version name to the symbol name.
4296 However, we do not modify a non-hidden absolute symbol
4297 if it is not a function, because it might be the version
4298 symbol itself. FIXME: What if it isn't? */
4299 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4300 || (vernum > 1
4301 && (!bfd_is_abs_section (sec)
4302 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4303 {
4304 const char *verstr;
4305 size_t namelen, verlen, newlen;
4306 char *newname, *p;
4307
4308 if (isym->st_shndx != SHN_UNDEF)
4309 {
4310 if (vernum > elf_tdata (abfd)->cverdefs)
4311 verstr = NULL;
4312 else if (vernum > 1)
4313 verstr =
4314 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4315 else
4316 verstr = "";
4317
4318 if (verstr == NULL)
4319 {
4320 _bfd_error_handler
4321 /* xgettext:c-format */
4322 (_("%B: %s: invalid version %u (max %d)"),
4323 abfd, name, vernum,
4324 elf_tdata (abfd)->cverdefs);
4325 bfd_set_error (bfd_error_bad_value);
4326 goto error_free_vers;
4327 }
4328 }
4329 else
4330 {
4331 /* We cannot simply test for the number of
4332 entries in the VERNEED section since the
4333 numbers for the needed versions do not start
4334 at 0. */
4335 Elf_Internal_Verneed *t;
4336
4337 verstr = NULL;
4338 for (t = elf_tdata (abfd)->verref;
4339 t != NULL;
4340 t = t->vn_nextref)
4341 {
4342 Elf_Internal_Vernaux *a;
4343
4344 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4345 {
4346 if (a->vna_other == vernum)
4347 {
4348 verstr = a->vna_nodename;
4349 break;
4350 }
4351 }
4352 if (a != NULL)
4353 break;
4354 }
4355 if (verstr == NULL)
4356 {
4357 _bfd_error_handler
4358 /* xgettext:c-format */
4359 (_("%B: %s: invalid needed version %d"),
4360 abfd, name, vernum);
4361 bfd_set_error (bfd_error_bad_value);
4362 goto error_free_vers;
4363 }
4364 }
4365
4366 namelen = strlen (name);
4367 verlen = strlen (verstr);
4368 newlen = namelen + verlen + 2;
4369 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4370 && isym->st_shndx != SHN_UNDEF)
4371 ++newlen;
4372
4373 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4374 if (newname == NULL)
4375 goto error_free_vers;
4376 memcpy (newname, name, namelen);
4377 p = newname + namelen;
4378 *p++ = ELF_VER_CHR;
4379 /* If this is a defined non-hidden version symbol,
4380 we add another @ to the name. This indicates the
4381 default version of the symbol. */
4382 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4383 && isym->st_shndx != SHN_UNDEF)
4384 *p++ = ELF_VER_CHR;
4385 memcpy (p, verstr, verlen + 1);
4386
4387 name = newname;
4388 }
4389
4390 /* If this symbol has default visibility and the user has
4391 requested we not re-export it, then mark it as hidden. */
4392 if (!bfd_is_und_section (sec)
4393 && !dynamic
4394 && abfd->no_export
4395 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4396 isym->st_other = (STV_HIDDEN
4397 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4398
4399 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4400 sym_hash, &old_bfd, &old_weak,
4401 &old_alignment, &skip, &override,
4402 &type_change_ok, &size_change_ok,
4403 &matched))
4404 goto error_free_vers;
4405
4406 if (skip)
4407 continue;
4408
4409 /* Override a definition only if the new symbol matches the
4410 existing one. */
4411 if (override && matched)
4412 definition = FALSE;
4413
4414 h = *sym_hash;
4415 while (h->root.type == bfd_link_hash_indirect
4416 || h->root.type == bfd_link_hash_warning)
4417 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4418
4419 if (elf_tdata (abfd)->verdef != NULL
4420 && vernum > 1
4421 && definition)
4422 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4423 }
4424
4425 if (! (_bfd_generic_link_add_one_symbol
4426 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4427 (struct bfd_link_hash_entry **) sym_hash)))
4428 goto error_free_vers;
4429
4430 if ((flags & BSF_GNU_UNIQUE)
4431 && (abfd->flags & DYNAMIC) == 0
4432 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4433 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
4434
4435 h = *sym_hash;
4436 /* We need to make sure that indirect symbol dynamic flags are
4437 updated. */
4438 hi = h;
4439 while (h->root.type == bfd_link_hash_indirect
4440 || h->root.type == bfd_link_hash_warning)
4441 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4442
4443 /* Setting the index to -3 tells elf_link_output_extsym that
4444 this symbol is defined in a discarded section. */
4445 if (discarded)
4446 h->indx = -3;
4447
4448 *sym_hash = h;
4449
4450 new_weak = (flags & BSF_WEAK) != 0;
4451 new_weakdef = FALSE;
4452 if (dynamic
4453 && definition
4454 && new_weak
4455 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4456 && is_elf_hash_table (htab)
4457 && h->u.weakdef == NULL)
4458 {
4459 /* Keep a list of all weak defined non function symbols from
4460 a dynamic object, using the weakdef field. Later in this
4461 function we will set the weakdef field to the correct
4462 value. We only put non-function symbols from dynamic
4463 objects on this list, because that happens to be the only
4464 time we need to know the normal symbol corresponding to a
4465 weak symbol, and the information is time consuming to
4466 figure out. If the weakdef field is not already NULL,
4467 then this symbol was already defined by some previous
4468 dynamic object, and we will be using that previous
4469 definition anyhow. */
4470
4471 h->u.weakdef = weaks;
4472 weaks = h;
4473 new_weakdef = TRUE;
4474 }
4475
4476 /* Set the alignment of a common symbol. */
4477 if ((common || bfd_is_com_section (sec))
4478 && h->root.type == bfd_link_hash_common)
4479 {
4480 unsigned int align;
4481
4482 if (common)
4483 align = bfd_log2 (isym->st_value);
4484 else
4485 {
4486 /* The new symbol is a common symbol in a shared object.
4487 We need to get the alignment from the section. */
4488 align = new_sec->alignment_power;
4489 }
4490 if (align > old_alignment)
4491 h->root.u.c.p->alignment_power = align;
4492 else
4493 h->root.u.c.p->alignment_power = old_alignment;
4494 }
4495
4496 if (is_elf_hash_table (htab))
4497 {
4498 /* Set a flag in the hash table entry indicating the type of
4499 reference or definition we just found. A dynamic symbol
4500 is one which is referenced or defined by both a regular
4501 object and a shared object. */
4502 bfd_boolean dynsym = FALSE;
4503
4504 /* Plugin symbols aren't normal. Don't set def_regular or
4505 ref_regular for them, or make them dynamic. */
4506 if ((abfd->flags & BFD_PLUGIN) != 0)
4507 ;
4508 else if (! dynamic)
4509 {
4510 if (! definition)
4511 {
4512 h->ref_regular = 1;
4513 if (bind != STB_WEAK)
4514 h->ref_regular_nonweak = 1;
4515 }
4516 else
4517 {
4518 h->def_regular = 1;
4519 if (h->def_dynamic)
4520 {
4521 h->def_dynamic = 0;
4522 h->ref_dynamic = 1;
4523 }
4524 }
4525
4526 /* If the indirect symbol has been forced local, don't
4527 make the real symbol dynamic. */
4528 if ((h == hi || !hi->forced_local)
4529 && (bfd_link_dll (info)
4530 || h->def_dynamic
4531 || h->ref_dynamic))
4532 dynsym = TRUE;
4533 }
4534 else
4535 {
4536 if (! definition)
4537 {
4538 h->ref_dynamic = 1;
4539 hi->ref_dynamic = 1;
4540 }
4541 else
4542 {
4543 h->def_dynamic = 1;
4544 hi->def_dynamic = 1;
4545 }
4546
4547 /* If the indirect symbol has been forced local, don't
4548 make the real symbol dynamic. */
4549 if ((h == hi || !hi->forced_local)
4550 && (h->def_regular
4551 || h->ref_regular
4552 || (h->u.weakdef != NULL
4553 && ! new_weakdef
4554 && h->u.weakdef->dynindx != -1)))
4555 dynsym = TRUE;
4556 }
4557
4558 /* Check to see if we need to add an indirect symbol for
4559 the default name. */
4560 if (definition
4561 || (!override && h->root.type == bfd_link_hash_common))
4562 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4563 sec, value, &old_bfd, &dynsym))
4564 goto error_free_vers;
4565
4566 /* Check the alignment when a common symbol is involved. This
4567 can change when a common symbol is overridden by a normal
4568 definition or a common symbol is ignored due to the old
4569 normal definition. We need to make sure the maximum
4570 alignment is maintained. */
4571 if ((old_alignment || common)
4572 && h->root.type != bfd_link_hash_common)
4573 {
4574 unsigned int common_align;
4575 unsigned int normal_align;
4576 unsigned int symbol_align;
4577 bfd *normal_bfd;
4578 bfd *common_bfd;
4579
4580 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4581 || h->root.type == bfd_link_hash_defweak);
4582
4583 symbol_align = ffs (h->root.u.def.value) - 1;
4584 if (h->root.u.def.section->owner != NULL
4585 && (h->root.u.def.section->owner->flags
4586 & (DYNAMIC | BFD_PLUGIN)) == 0)
4587 {
4588 normal_align = h->root.u.def.section->alignment_power;
4589 if (normal_align > symbol_align)
4590 normal_align = symbol_align;
4591 }
4592 else
4593 normal_align = symbol_align;
4594
4595 if (old_alignment)
4596 {
4597 common_align = old_alignment;
4598 common_bfd = old_bfd;
4599 normal_bfd = abfd;
4600 }
4601 else
4602 {
4603 common_align = bfd_log2 (isym->st_value);
4604 common_bfd = abfd;
4605 normal_bfd = old_bfd;
4606 }
4607
4608 if (normal_align < common_align)
4609 {
4610 /* PR binutils/2735 */
4611 if (normal_bfd == NULL)
4612 _bfd_error_handler
4613 /* xgettext:c-format */
4614 (_("Warning: alignment %u of common symbol `%s' in %B is"
4615 " greater than the alignment (%u) of its section %A"),
4616 common_bfd, h->root.u.def.section,
4617 1 << common_align, name, 1 << normal_align);
4618 else
4619 _bfd_error_handler
4620 /* xgettext:c-format */
4621 (_("Warning: alignment %u of symbol `%s' in %B"
4622 " is smaller than %u in %B"),
4623 normal_bfd, common_bfd,
4624 1 << normal_align, name, 1 << common_align);
4625 }
4626 }
4627
4628 /* Remember the symbol size if it isn't undefined. */
4629 if (isym->st_size != 0
4630 && isym->st_shndx != SHN_UNDEF
4631 && (definition || h->size == 0))
4632 {
4633 if (h->size != 0
4634 && h->size != isym->st_size
4635 && ! size_change_ok)
4636 _bfd_error_handler
4637 /* xgettext:c-format */
4638 (_("Warning: size of symbol `%s' changed"
4639 " from %lu in %B to %lu in %B"),
4640 old_bfd, abfd,
4641 name, (unsigned long) h->size,
4642 (unsigned long) isym->st_size);
4643
4644 h->size = isym->st_size;
4645 }
4646
4647 /* If this is a common symbol, then we always want H->SIZE
4648 to be the size of the common symbol. The code just above
4649 won't fix the size if a common symbol becomes larger. We
4650 don't warn about a size change here, because that is
4651 covered by --warn-common. Allow changes between different
4652 function types. */
4653 if (h->root.type == bfd_link_hash_common)
4654 h->size = h->root.u.c.size;
4655
4656 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4657 && ((definition && !new_weak)
4658 || (old_weak && h->root.type == bfd_link_hash_common)
4659 || h->type == STT_NOTYPE))
4660 {
4661 unsigned int type = ELF_ST_TYPE (isym->st_info);
4662
4663 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4664 symbol. */
4665 if (type == STT_GNU_IFUNC
4666 && (abfd->flags & DYNAMIC) != 0)
4667 type = STT_FUNC;
4668
4669 if (h->type != type)
4670 {
4671 if (h->type != STT_NOTYPE && ! type_change_ok)
4672 /* xgettext:c-format */
4673 _bfd_error_handler
4674 (_("Warning: type of symbol `%s' changed"
4675 " from %d to %d in %B"),
4676 abfd, name, h->type, type);
4677
4678 h->type = type;
4679 }
4680 }
4681
4682 /* Merge st_other field. */
4683 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4684
4685 /* We don't want to make debug symbol dynamic. */
4686 if (definition
4687 && (sec->flags & SEC_DEBUGGING)
4688 && !bfd_link_relocatable (info))
4689 dynsym = FALSE;
4690
4691 /* Nor should we make plugin symbols dynamic. */
4692 if ((abfd->flags & BFD_PLUGIN) != 0)
4693 dynsym = FALSE;
4694
4695 if (definition)
4696 {
4697 h->target_internal = isym->st_target_internal;
4698 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4699 }
4700
4701 if (definition && !dynamic)
4702 {
4703 char *p = strchr (name, ELF_VER_CHR);
4704 if (p != NULL && p[1] != ELF_VER_CHR)
4705 {
4706 /* Queue non-default versions so that .symver x, x@FOO
4707 aliases can be checked. */
4708 if (!nondeflt_vers)
4709 {
4710 amt = ((isymend - isym + 1)
4711 * sizeof (struct elf_link_hash_entry *));
4712 nondeflt_vers
4713 = (struct elf_link_hash_entry **) bfd_malloc (amt);
4714 if (!nondeflt_vers)
4715 goto error_free_vers;
4716 }
4717 nondeflt_vers[nondeflt_vers_cnt++] = h;
4718 }
4719 }
4720
4721 if (dynsym && h->dynindx == -1)
4722 {
4723 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4724 goto error_free_vers;
4725 if (h->u.weakdef != NULL
4726 && ! new_weakdef
4727 && h->u.weakdef->dynindx == -1)
4728 {
4729 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4730 goto error_free_vers;
4731 }
4732 }
4733 else if (h->dynindx != -1)
4734 /* If the symbol already has a dynamic index, but
4735 visibility says it should not be visible, turn it into
4736 a local symbol. */
4737 switch (ELF_ST_VISIBILITY (h->other))
4738 {
4739 case STV_INTERNAL:
4740 case STV_HIDDEN:
4741 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4742 dynsym = FALSE;
4743 break;
4744 }
4745
4746 /* Don't add DT_NEEDED for references from the dummy bfd nor
4747 for unmatched symbol. */
4748 if (!add_needed
4749 && matched
4750 && definition
4751 && ((dynsym
4752 && h->ref_regular_nonweak
4753 && (old_bfd == NULL
4754 || (old_bfd->flags & BFD_PLUGIN) == 0))
4755 || (h->ref_dynamic_nonweak
4756 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4757 && !on_needed_list (elf_dt_name (abfd),
4758 htab->needed, NULL))))
4759 {
4760 int ret;
4761 const char *soname = elf_dt_name (abfd);
4762
4763 info->callbacks->minfo ("%!", soname, old_bfd,
4764 h->root.root.string);
4765
4766 /* A symbol from a library loaded via DT_NEEDED of some
4767 other library is referenced by a regular object.
4768 Add a DT_NEEDED entry for it. Issue an error if
4769 --no-add-needed is used and the reference was not
4770 a weak one. */
4771 if (old_bfd != NULL
4772 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4773 {
4774 _bfd_error_handler
4775 /* xgettext:c-format */
4776 (_("%B: undefined reference to symbol '%s'"),
4777 old_bfd, name);
4778 bfd_set_error (bfd_error_missing_dso);
4779 goto error_free_vers;
4780 }
4781
4782 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4783 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
4784
4785 add_needed = TRUE;
4786 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4787 if (ret < 0)
4788 goto error_free_vers;
4789
4790 BFD_ASSERT (ret == 0);
4791 }
4792 }
4793 }
4794
4795 if (extversym != NULL)
4796 {
4797 free (extversym);
4798 extversym = NULL;
4799 }
4800
4801 if (isymbuf != NULL)
4802 {
4803 free (isymbuf);
4804 isymbuf = NULL;
4805 }
4806
4807 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4808 {
4809 unsigned int i;
4810
4811 /* Restore the symbol table. */
4812 old_ent = (char *) old_tab + tabsize;
4813 memset (elf_sym_hashes (abfd), 0,
4814 extsymcount * sizeof (struct elf_link_hash_entry *));
4815 htab->root.table.table = old_table;
4816 htab->root.table.size = old_size;
4817 htab->root.table.count = old_count;
4818 memcpy (htab->root.table.table, old_tab, tabsize);
4819 htab->root.undefs = old_undefs;
4820 htab->root.undefs_tail = old_undefs_tail;
4821 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
4822 free (old_strtab);
4823 old_strtab = NULL;
4824 for (i = 0; i < htab->root.table.size; i++)
4825 {
4826 struct bfd_hash_entry *p;
4827 struct elf_link_hash_entry *h;
4828 bfd_size_type size;
4829 unsigned int alignment_power;
4830
4831 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4832 {
4833 h = (struct elf_link_hash_entry *) p;
4834 if (h->root.type == bfd_link_hash_warning)
4835 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4836
4837 /* Preserve the maximum alignment and size for common
4838 symbols even if this dynamic lib isn't on DT_NEEDED
4839 since it can still be loaded at run time by another
4840 dynamic lib. */
4841 if (h->root.type == bfd_link_hash_common)
4842 {
4843 size = h->root.u.c.size;
4844 alignment_power = h->root.u.c.p->alignment_power;
4845 }
4846 else
4847 {
4848 size = 0;
4849 alignment_power = 0;
4850 }
4851 memcpy (p, old_ent, htab->root.table.entsize);
4852 old_ent = (char *) old_ent + htab->root.table.entsize;
4853 h = (struct elf_link_hash_entry *) p;
4854 if (h->root.type == bfd_link_hash_warning)
4855 {
4856 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4857 old_ent = (char *) old_ent + htab->root.table.entsize;
4858 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4859 }
4860 if (h->root.type == bfd_link_hash_common)
4861 {
4862 if (size > h->root.u.c.size)
4863 h->root.u.c.size = size;
4864 if (alignment_power > h->root.u.c.p->alignment_power)
4865 h->root.u.c.p->alignment_power = alignment_power;
4866 }
4867 }
4868 }
4869
4870 /* Make a special call to the linker "notice" function to
4871 tell it that symbols added for crefs may need to be removed. */
4872 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
4873 goto error_free_vers;
4874
4875 free (old_tab);
4876 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4877 alloc_mark);
4878 if (nondeflt_vers != NULL)
4879 free (nondeflt_vers);
4880 return TRUE;
4881 }
4882
4883 if (old_tab != NULL)
4884 {
4885 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
4886 goto error_free_vers;
4887 free (old_tab);
4888 old_tab = NULL;
4889 }
4890
4891 /* Now that all the symbols from this input file are created, if
4892 not performing a relocatable link, handle .symver foo, foo@BAR
4893 such that any relocs against foo become foo@BAR. */
4894 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4895 {
4896 size_t cnt, symidx;
4897
4898 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4899 {
4900 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4901 char *shortname, *p;
4902
4903 p = strchr (h->root.root.string, ELF_VER_CHR);
4904 if (p == NULL
4905 || (h->root.type != bfd_link_hash_defined
4906 && h->root.type != bfd_link_hash_defweak))
4907 continue;
4908
4909 amt = p - h->root.root.string;
4910 shortname = (char *) bfd_malloc (amt + 1);
4911 if (!shortname)
4912 goto error_free_vers;
4913 memcpy (shortname, h->root.root.string, amt);
4914 shortname[amt] = '\0';
4915
4916 hi = (struct elf_link_hash_entry *)
4917 bfd_link_hash_lookup (&htab->root, shortname,
4918 FALSE, FALSE, FALSE);
4919 if (hi != NULL
4920 && hi->root.type == h->root.type
4921 && hi->root.u.def.value == h->root.u.def.value
4922 && hi->root.u.def.section == h->root.u.def.section)
4923 {
4924 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4925 hi->root.type = bfd_link_hash_indirect;
4926 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4927 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4928 sym_hash = elf_sym_hashes (abfd);
4929 if (sym_hash)
4930 for (symidx = 0; symidx < extsymcount; ++symidx)
4931 if (sym_hash[symidx] == hi)
4932 {
4933 sym_hash[symidx] = h;
4934 break;
4935 }
4936 }
4937 free (shortname);
4938 }
4939 free (nondeflt_vers);
4940 nondeflt_vers = NULL;
4941 }
4942
4943 /* Now set the weakdefs field correctly for all the weak defined
4944 symbols we found. The only way to do this is to search all the
4945 symbols. Since we only need the information for non functions in
4946 dynamic objects, that's the only time we actually put anything on
4947 the list WEAKS. We need this information so that if a regular
4948 object refers to a symbol defined weakly in a dynamic object, the
4949 real symbol in the dynamic object is also put in the dynamic
4950 symbols; we also must arrange for both symbols to point to the
4951 same memory location. We could handle the general case of symbol
4952 aliasing, but a general symbol alias can only be generated in
4953 assembler code, handling it correctly would be very time
4954 consuming, and other ELF linkers don't handle general aliasing
4955 either. */
4956 if (weaks != NULL)
4957 {
4958 struct elf_link_hash_entry **hpp;
4959 struct elf_link_hash_entry **hppend;
4960 struct elf_link_hash_entry **sorted_sym_hash;
4961 struct elf_link_hash_entry *h;
4962 size_t sym_count;
4963
4964 /* Since we have to search the whole symbol list for each weak
4965 defined symbol, search time for N weak defined symbols will be
4966 O(N^2). Binary search will cut it down to O(NlogN). */
4967 amt = extsymcount;
4968 amt *= sizeof (struct elf_link_hash_entry *);
4969 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4970 if (sorted_sym_hash == NULL)
4971 goto error_return;
4972 sym_hash = sorted_sym_hash;
4973 hpp = elf_sym_hashes (abfd);
4974 hppend = hpp + extsymcount;
4975 sym_count = 0;
4976 for (; hpp < hppend; hpp++)
4977 {
4978 h = *hpp;
4979 if (h != NULL
4980 && h->root.type == bfd_link_hash_defined
4981 && !bed->is_function_type (h->type))
4982 {
4983 *sym_hash = h;
4984 sym_hash++;
4985 sym_count++;
4986 }
4987 }
4988
4989 qsort (sorted_sym_hash, sym_count,
4990 sizeof (struct elf_link_hash_entry *),
4991 elf_sort_symbol);
4992
4993 while (weaks != NULL)
4994 {
4995 struct elf_link_hash_entry *hlook;
4996 asection *slook;
4997 bfd_vma vlook;
4998 size_t i, j, idx = 0;
4999
5000 hlook = weaks;
5001 weaks = hlook->u.weakdef;
5002 hlook->u.weakdef = NULL;
5003
5004 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
5005 || hlook->root.type == bfd_link_hash_defweak
5006 || hlook->root.type == bfd_link_hash_common
5007 || hlook->root.type == bfd_link_hash_indirect);
5008 slook = hlook->root.u.def.section;
5009 vlook = hlook->root.u.def.value;
5010
5011 i = 0;
5012 j = sym_count;
5013 while (i != j)
5014 {
5015 bfd_signed_vma vdiff;
5016 idx = (i + j) / 2;
5017 h = sorted_sym_hash[idx];
5018 vdiff = vlook - h->root.u.def.value;
5019 if (vdiff < 0)
5020 j = idx;
5021 else if (vdiff > 0)
5022 i = idx + 1;
5023 else
5024 {
5025 int sdiff = slook->id - h->root.u.def.section->id;
5026 if (sdiff < 0)
5027 j = idx;
5028 else if (sdiff > 0)
5029 i = idx + 1;
5030 else
5031 break;
5032 }
5033 }
5034
5035 /* We didn't find a value/section match. */
5036 if (i == j)
5037 continue;
5038
5039 /* With multiple aliases, or when the weak symbol is already
5040 strongly defined, we have multiple matching symbols and
5041 the binary search above may land on any of them. Step
5042 one past the matching symbol(s). */
5043 while (++idx != j)
5044 {
5045 h = sorted_sym_hash[idx];
5046 if (h->root.u.def.section != slook
5047 || h->root.u.def.value != vlook)
5048 break;
5049 }
5050
5051 /* Now look back over the aliases. Since we sorted by size
5052 as well as value and section, we'll choose the one with
5053 the largest size. */
5054 while (idx-- != i)
5055 {
5056 h = sorted_sym_hash[idx];
5057
5058 /* Stop if value or section doesn't match. */
5059 if (h->root.u.def.section != slook
5060 || h->root.u.def.value != vlook)
5061 break;
5062 else if (h != hlook)
5063 {
5064 hlook->u.weakdef = h;
5065
5066 /* If the weak definition is in the list of dynamic
5067 symbols, make sure the real definition is put
5068 there as well. */
5069 if (hlook->dynindx != -1 && h->dynindx == -1)
5070 {
5071 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5072 {
5073 err_free_sym_hash:
5074 free (sorted_sym_hash);
5075 goto error_return;
5076 }
5077 }
5078
5079 /* If the real definition is in the list of dynamic
5080 symbols, make sure the weak definition is put
5081 there as well. If we don't do this, then the
5082 dynamic loader might not merge the entries for the
5083 real definition and the weak definition. */
5084 if (h->dynindx != -1 && hlook->dynindx == -1)
5085 {
5086 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5087 goto err_free_sym_hash;
5088 }
5089 break;
5090 }
5091 }
5092 }
5093
5094 free (sorted_sym_hash);
5095 }
5096
5097 if (bed->check_directives
5098 && !(*bed->check_directives) (abfd, info))
5099 return FALSE;
5100
5101 if (!info->check_relocs_after_open_input
5102 && !_bfd_elf_link_check_relocs (abfd, info))
5103 return FALSE;
5104
5105 /* If this is a non-traditional link, try to optimize the handling
5106 of the .stab/.stabstr sections. */
5107 if (! dynamic
5108 && ! info->traditional_format
5109 && is_elf_hash_table (htab)
5110 && (info->strip != strip_all && info->strip != strip_debugger))
5111 {
5112 asection *stabstr;
5113
5114 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5115 if (stabstr != NULL)
5116 {
5117 bfd_size_type string_offset = 0;
5118 asection *stab;
5119
5120 for (stab = abfd->sections; stab; stab = stab->next)
5121 if (CONST_STRNEQ (stab->name, ".stab")
5122 && (!stab->name[5] ||
5123 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5124 && (stab->flags & SEC_MERGE) == 0
5125 && !bfd_is_abs_section (stab->output_section))
5126 {
5127 struct bfd_elf_section_data *secdata;
5128
5129 secdata = elf_section_data (stab);
5130 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5131 stabstr, &secdata->sec_info,
5132 &string_offset))
5133 goto error_return;
5134 if (secdata->sec_info)
5135 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5136 }
5137 }
5138 }
5139
5140 if (is_elf_hash_table (htab) && add_needed)
5141 {
5142 /* Add this bfd to the loaded list. */
5143 struct elf_link_loaded_list *n;
5144
5145 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5146 if (n == NULL)
5147 goto error_return;
5148 n->abfd = abfd;
5149 n->next = htab->loaded;
5150 htab->loaded = n;
5151 }
5152
5153 return TRUE;
5154
5155 error_free_vers:
5156 if (old_tab != NULL)
5157 free (old_tab);
5158 if (old_strtab != NULL)
5159 free (old_strtab);
5160 if (nondeflt_vers != NULL)
5161 free (nondeflt_vers);
5162 if (extversym != NULL)
5163 free (extversym);
5164 error_free_sym:
5165 if (isymbuf != NULL)
5166 free (isymbuf);
5167 error_return:
5168 return FALSE;
5169 }
5170
5171 /* Return the linker hash table entry of a symbol that might be
5172 satisfied by an archive symbol. Return -1 on error. */
5173
5174 struct elf_link_hash_entry *
5175 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5176 struct bfd_link_info *info,
5177 const char *name)
5178 {
5179 struct elf_link_hash_entry *h;
5180 char *p, *copy;
5181 size_t len, first;
5182
5183 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5184 if (h != NULL)
5185 return h;
5186
5187 /* If this is a default version (the name contains @@), look up the
5188 symbol again with only one `@' as well as without the version.
5189 The effect is that references to the symbol with and without the
5190 version will be matched by the default symbol in the archive. */
5191
5192 p = strchr (name, ELF_VER_CHR);
5193 if (p == NULL || p[1] != ELF_VER_CHR)
5194 return h;
5195
5196 /* First check with only one `@'. */
5197 len = strlen (name);
5198 copy = (char *) bfd_alloc (abfd, len);
5199 if (copy == NULL)
5200 return (struct elf_link_hash_entry *) 0 - 1;
5201
5202 first = p - name + 1;
5203 memcpy (copy, name, first);
5204 memcpy (copy + first, name + first + 1, len - first);
5205
5206 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5207 if (h == NULL)
5208 {
5209 /* We also need to check references to the symbol without the
5210 version. */
5211 copy[first - 1] = '\0';
5212 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5213 FALSE, FALSE, TRUE);
5214 }
5215
5216 bfd_release (abfd, copy);
5217 return h;
5218 }
5219
5220 /* Add symbols from an ELF archive file to the linker hash table. We
5221 don't use _bfd_generic_link_add_archive_symbols because we need to
5222 handle versioned symbols.
5223
5224 Fortunately, ELF archive handling is simpler than that done by
5225 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5226 oddities. In ELF, if we find a symbol in the archive map, and the
5227 symbol is currently undefined, we know that we must pull in that
5228 object file.
5229
5230 Unfortunately, we do have to make multiple passes over the symbol
5231 table until nothing further is resolved. */
5232
5233 static bfd_boolean
5234 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5235 {
5236 symindex c;
5237 unsigned char *included = NULL;
5238 carsym *symdefs;
5239 bfd_boolean loop;
5240 bfd_size_type amt;
5241 const struct elf_backend_data *bed;
5242 struct elf_link_hash_entry * (*archive_symbol_lookup)
5243 (bfd *, struct bfd_link_info *, const char *);
5244
5245 if (! bfd_has_map (abfd))
5246 {
5247 /* An empty archive is a special case. */
5248 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5249 return TRUE;
5250 bfd_set_error (bfd_error_no_armap);
5251 return FALSE;
5252 }
5253
5254 /* Keep track of all symbols we know to be already defined, and all
5255 files we know to be already included. This is to speed up the
5256 second and subsequent passes. */
5257 c = bfd_ardata (abfd)->symdef_count;
5258 if (c == 0)
5259 return TRUE;
5260 amt = c;
5261 amt *= sizeof (*included);
5262 included = (unsigned char *) bfd_zmalloc (amt);
5263 if (included == NULL)
5264 return FALSE;
5265
5266 symdefs = bfd_ardata (abfd)->symdefs;
5267 bed = get_elf_backend_data (abfd);
5268 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5269
5270 do
5271 {
5272 file_ptr last;
5273 symindex i;
5274 carsym *symdef;
5275 carsym *symdefend;
5276
5277 loop = FALSE;
5278 last = -1;
5279
5280 symdef = symdefs;
5281 symdefend = symdef + c;
5282 for (i = 0; symdef < symdefend; symdef++, i++)
5283 {
5284 struct elf_link_hash_entry *h;
5285 bfd *element;
5286 struct bfd_link_hash_entry *undefs_tail;
5287 symindex mark;
5288
5289 if (included[i])
5290 continue;
5291 if (symdef->file_offset == last)
5292 {
5293 included[i] = TRUE;
5294 continue;
5295 }
5296
5297 h = archive_symbol_lookup (abfd, info, symdef->name);
5298 if (h == (struct elf_link_hash_entry *) 0 - 1)
5299 goto error_return;
5300
5301 if (h == NULL)
5302 continue;
5303
5304 if (h->root.type == bfd_link_hash_common)
5305 {
5306 /* We currently have a common symbol. The archive map contains
5307 a reference to this symbol, so we may want to include it. We
5308 only want to include it however, if this archive element
5309 contains a definition of the symbol, not just another common
5310 declaration of it.
5311
5312 Unfortunately some archivers (including GNU ar) will put
5313 declarations of common symbols into their archive maps, as
5314 well as real definitions, so we cannot just go by the archive
5315 map alone. Instead we must read in the element's symbol
5316 table and check that to see what kind of symbol definition
5317 this is. */
5318 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5319 continue;
5320 }
5321 else if (h->root.type != bfd_link_hash_undefined)
5322 {
5323 if (h->root.type != bfd_link_hash_undefweak)
5324 /* Symbol must be defined. Don't check it again. */
5325 included[i] = TRUE;
5326 continue;
5327 }
5328
5329 /* We need to include this archive member. */
5330 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5331 if (element == NULL)
5332 goto error_return;
5333
5334 if (! bfd_check_format (element, bfd_object))
5335 goto error_return;
5336
5337 undefs_tail = info->hash->undefs_tail;
5338
5339 if (!(*info->callbacks
5340 ->add_archive_element) (info, element, symdef->name, &element))
5341 continue;
5342 if (!bfd_link_add_symbols (element, info))
5343 goto error_return;
5344
5345 /* If there are any new undefined symbols, we need to make
5346 another pass through the archive in order to see whether
5347 they can be defined. FIXME: This isn't perfect, because
5348 common symbols wind up on undefs_tail and because an
5349 undefined symbol which is defined later on in this pass
5350 does not require another pass. This isn't a bug, but it
5351 does make the code less efficient than it could be. */
5352 if (undefs_tail != info->hash->undefs_tail)
5353 loop = TRUE;
5354
5355 /* Look backward to mark all symbols from this object file
5356 which we have already seen in this pass. */
5357 mark = i;
5358 do
5359 {
5360 included[mark] = TRUE;
5361 if (mark == 0)
5362 break;
5363 --mark;
5364 }
5365 while (symdefs[mark].file_offset == symdef->file_offset);
5366
5367 /* We mark subsequent symbols from this object file as we go
5368 on through the loop. */
5369 last = symdef->file_offset;
5370 }
5371 }
5372 while (loop);
5373
5374 free (included);
5375
5376 return TRUE;
5377
5378 error_return:
5379 if (included != NULL)
5380 free (included);
5381 return FALSE;
5382 }
5383
5384 /* Given an ELF BFD, add symbols to the global hash table as
5385 appropriate. */
5386
5387 bfd_boolean
5388 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5389 {
5390 switch (bfd_get_format (abfd))
5391 {
5392 case bfd_object:
5393 return elf_link_add_object_symbols (abfd, info);
5394 case bfd_archive:
5395 return elf_link_add_archive_symbols (abfd, info);
5396 default:
5397 bfd_set_error (bfd_error_wrong_format);
5398 return FALSE;
5399 }
5400 }
5401 \f
5402 struct hash_codes_info
5403 {
5404 unsigned long *hashcodes;
5405 bfd_boolean error;
5406 };
5407
5408 /* This function will be called though elf_link_hash_traverse to store
5409 all hash value of the exported symbols in an array. */
5410
5411 static bfd_boolean
5412 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5413 {
5414 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5415 const char *name;
5416 unsigned long ha;
5417 char *alc = NULL;
5418
5419 /* Ignore indirect symbols. These are added by the versioning code. */
5420 if (h->dynindx == -1)
5421 return TRUE;
5422
5423 name = h->root.root.string;
5424 if (h->versioned >= versioned)
5425 {
5426 char *p = strchr (name, ELF_VER_CHR);
5427 if (p != NULL)
5428 {
5429 alc = (char *) bfd_malloc (p - name + 1);
5430 if (alc == NULL)
5431 {
5432 inf->error = TRUE;
5433 return FALSE;
5434 }
5435 memcpy (alc, name, p - name);
5436 alc[p - name] = '\0';
5437 name = alc;
5438 }
5439 }
5440
5441 /* Compute the hash value. */
5442 ha = bfd_elf_hash (name);
5443
5444 /* Store the found hash value in the array given as the argument. */
5445 *(inf->hashcodes)++ = ha;
5446
5447 /* And store it in the struct so that we can put it in the hash table
5448 later. */
5449 h->u.elf_hash_value = ha;
5450
5451 if (alc != NULL)
5452 free (alc);
5453
5454 return TRUE;
5455 }
5456
5457 struct collect_gnu_hash_codes
5458 {
5459 bfd *output_bfd;
5460 const struct elf_backend_data *bed;
5461 unsigned long int nsyms;
5462 unsigned long int maskbits;
5463 unsigned long int *hashcodes;
5464 unsigned long int *hashval;
5465 unsigned long int *indx;
5466 unsigned long int *counts;
5467 bfd_vma *bitmask;
5468 bfd_byte *contents;
5469 long int min_dynindx;
5470 unsigned long int bucketcount;
5471 unsigned long int symindx;
5472 long int local_indx;
5473 long int shift1, shift2;
5474 unsigned long int mask;
5475 bfd_boolean error;
5476 };
5477
5478 /* This function will be called though elf_link_hash_traverse to store
5479 all hash value of the exported symbols in an array. */
5480
5481 static bfd_boolean
5482 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5483 {
5484 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5485 const char *name;
5486 unsigned long ha;
5487 char *alc = NULL;
5488
5489 /* Ignore indirect symbols. These are added by the versioning code. */
5490 if (h->dynindx == -1)
5491 return TRUE;
5492
5493 /* Ignore also local symbols and undefined symbols. */
5494 if (! (*s->bed->elf_hash_symbol) (h))
5495 return TRUE;
5496
5497 name = h->root.root.string;
5498 if (h->versioned >= versioned)
5499 {
5500 char *p = strchr (name, ELF_VER_CHR);
5501 if (p != NULL)
5502 {
5503 alc = (char *) bfd_malloc (p - name + 1);
5504 if (alc == NULL)
5505 {
5506 s->error = TRUE;
5507 return FALSE;
5508 }
5509 memcpy (alc, name, p - name);
5510 alc[p - name] = '\0';
5511 name = alc;
5512 }
5513 }
5514
5515 /* Compute the hash value. */
5516 ha = bfd_elf_gnu_hash (name);
5517
5518 /* Store the found hash value in the array for compute_bucket_count,
5519 and also for .dynsym reordering purposes. */
5520 s->hashcodes[s->nsyms] = ha;
5521 s->hashval[h->dynindx] = ha;
5522 ++s->nsyms;
5523 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5524 s->min_dynindx = h->dynindx;
5525
5526 if (alc != NULL)
5527 free (alc);
5528
5529 return TRUE;
5530 }
5531
5532 /* This function will be called though elf_link_hash_traverse to do
5533 final dynaminc symbol renumbering. */
5534
5535 static bfd_boolean
5536 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5537 {
5538 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5539 unsigned long int bucket;
5540 unsigned long int val;
5541
5542 /* Ignore indirect symbols. */
5543 if (h->dynindx == -1)
5544 return TRUE;
5545
5546 /* Ignore also local symbols and undefined symbols. */
5547 if (! (*s->bed->elf_hash_symbol) (h))
5548 {
5549 if (h->dynindx >= s->min_dynindx)
5550 h->dynindx = s->local_indx++;
5551 return TRUE;
5552 }
5553
5554 bucket = s->hashval[h->dynindx] % s->bucketcount;
5555 val = (s->hashval[h->dynindx] >> s->shift1)
5556 & ((s->maskbits >> s->shift1) - 1);
5557 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5558 s->bitmask[val]
5559 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5560 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5561 if (s->counts[bucket] == 1)
5562 /* Last element terminates the chain. */
5563 val |= 1;
5564 bfd_put_32 (s->output_bfd, val,
5565 s->contents + (s->indx[bucket] - s->symindx) * 4);
5566 --s->counts[bucket];
5567 h->dynindx = s->indx[bucket]++;
5568 return TRUE;
5569 }
5570
5571 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5572
5573 bfd_boolean
5574 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5575 {
5576 return !(h->forced_local
5577 || h->root.type == bfd_link_hash_undefined
5578 || h->root.type == bfd_link_hash_undefweak
5579 || ((h->root.type == bfd_link_hash_defined
5580 || h->root.type == bfd_link_hash_defweak)
5581 && h->root.u.def.section->output_section == NULL));
5582 }
5583
5584 /* Array used to determine the number of hash table buckets to use
5585 based on the number of symbols there are. If there are fewer than
5586 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5587 fewer than 37 we use 17 buckets, and so forth. We never use more
5588 than 32771 buckets. */
5589
5590 static const size_t elf_buckets[] =
5591 {
5592 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5593 16411, 32771, 0
5594 };
5595
5596 /* Compute bucket count for hashing table. We do not use a static set
5597 of possible tables sizes anymore. Instead we determine for all
5598 possible reasonable sizes of the table the outcome (i.e., the
5599 number of collisions etc) and choose the best solution. The
5600 weighting functions are not too simple to allow the table to grow
5601 without bounds. Instead one of the weighting factors is the size.
5602 Therefore the result is always a good payoff between few collisions
5603 (= short chain lengths) and table size. */
5604 static size_t
5605 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5606 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5607 unsigned long int nsyms,
5608 int gnu_hash)
5609 {
5610 size_t best_size = 0;
5611 unsigned long int i;
5612
5613 /* We have a problem here. The following code to optimize the table
5614 size requires an integer type with more the 32 bits. If
5615 BFD_HOST_U_64_BIT is set we know about such a type. */
5616 #ifdef BFD_HOST_U_64_BIT
5617 if (info->optimize)
5618 {
5619 size_t minsize;
5620 size_t maxsize;
5621 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5622 bfd *dynobj = elf_hash_table (info)->dynobj;
5623 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5624 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5625 unsigned long int *counts;
5626 bfd_size_type amt;
5627 unsigned int no_improvement_count = 0;
5628
5629 /* Possible optimization parameters: if we have NSYMS symbols we say
5630 that the hashing table must at least have NSYMS/4 and at most
5631 2*NSYMS buckets. */
5632 minsize = nsyms / 4;
5633 if (minsize == 0)
5634 minsize = 1;
5635 best_size = maxsize = nsyms * 2;
5636 if (gnu_hash)
5637 {
5638 if (minsize < 2)
5639 minsize = 2;
5640 if ((best_size & 31) == 0)
5641 ++best_size;
5642 }
5643
5644 /* Create array where we count the collisions in. We must use bfd_malloc
5645 since the size could be large. */
5646 amt = maxsize;
5647 amt *= sizeof (unsigned long int);
5648 counts = (unsigned long int *) bfd_malloc (amt);
5649 if (counts == NULL)
5650 return 0;
5651
5652 /* Compute the "optimal" size for the hash table. The criteria is a
5653 minimal chain length. The minor criteria is (of course) the size
5654 of the table. */
5655 for (i = minsize; i < maxsize; ++i)
5656 {
5657 /* Walk through the array of hashcodes and count the collisions. */
5658 BFD_HOST_U_64_BIT max;
5659 unsigned long int j;
5660 unsigned long int fact;
5661
5662 if (gnu_hash && (i & 31) == 0)
5663 continue;
5664
5665 memset (counts, '\0', i * sizeof (unsigned long int));
5666
5667 /* Determine how often each hash bucket is used. */
5668 for (j = 0; j < nsyms; ++j)
5669 ++counts[hashcodes[j] % i];
5670
5671 /* For the weight function we need some information about the
5672 pagesize on the target. This is information need not be 100%
5673 accurate. Since this information is not available (so far) we
5674 define it here to a reasonable default value. If it is crucial
5675 to have a better value some day simply define this value. */
5676 # ifndef BFD_TARGET_PAGESIZE
5677 # define BFD_TARGET_PAGESIZE (4096)
5678 # endif
5679
5680 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5681 and the chains. */
5682 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5683
5684 # if 1
5685 /* Variant 1: optimize for short chains. We add the squares
5686 of all the chain lengths (which favors many small chain
5687 over a few long chains). */
5688 for (j = 0; j < i; ++j)
5689 max += counts[j] * counts[j];
5690
5691 /* This adds penalties for the overall size of the table. */
5692 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5693 max *= fact * fact;
5694 # else
5695 /* Variant 2: Optimize a lot more for small table. Here we
5696 also add squares of the size but we also add penalties for
5697 empty slots (the +1 term). */
5698 for (j = 0; j < i; ++j)
5699 max += (1 + counts[j]) * (1 + counts[j]);
5700
5701 /* The overall size of the table is considered, but not as
5702 strong as in variant 1, where it is squared. */
5703 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5704 max *= fact;
5705 # endif
5706
5707 /* Compare with current best results. */
5708 if (max < best_chlen)
5709 {
5710 best_chlen = max;
5711 best_size = i;
5712 no_improvement_count = 0;
5713 }
5714 /* PR 11843: Avoid futile long searches for the best bucket size
5715 when there are a large number of symbols. */
5716 else if (++no_improvement_count == 100)
5717 break;
5718 }
5719
5720 free (counts);
5721 }
5722 else
5723 #endif /* defined (BFD_HOST_U_64_BIT) */
5724 {
5725 /* This is the fallback solution if no 64bit type is available or if we
5726 are not supposed to spend much time on optimizations. We select the
5727 bucket count using a fixed set of numbers. */
5728 for (i = 0; elf_buckets[i] != 0; i++)
5729 {
5730 best_size = elf_buckets[i];
5731 if (nsyms < elf_buckets[i + 1])
5732 break;
5733 }
5734 if (gnu_hash && best_size < 2)
5735 best_size = 2;
5736 }
5737
5738 return best_size;
5739 }
5740
5741 /* Size any SHT_GROUP section for ld -r. */
5742
5743 bfd_boolean
5744 _bfd_elf_size_group_sections (struct bfd_link_info *info)
5745 {
5746 bfd *ibfd;
5747
5748 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5749 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5750 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5751 return FALSE;
5752 return TRUE;
5753 }
5754
5755 /* Set a default stack segment size. The value in INFO wins. If it
5756 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5757 undefined it is initialized. */
5758
5759 bfd_boolean
5760 bfd_elf_stack_segment_size (bfd *output_bfd,
5761 struct bfd_link_info *info,
5762 const char *legacy_symbol,
5763 bfd_vma default_size)
5764 {
5765 struct elf_link_hash_entry *h = NULL;
5766
5767 /* Look for legacy symbol. */
5768 if (legacy_symbol)
5769 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5770 FALSE, FALSE, FALSE);
5771 if (h && (h->root.type == bfd_link_hash_defined
5772 || h->root.type == bfd_link_hash_defweak)
5773 && h->def_regular
5774 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5775 {
5776 /* The symbol has no type if specified on the command line. */
5777 h->type = STT_OBJECT;
5778 if (info->stacksize)
5779 /* xgettext:c-format */
5780 _bfd_error_handler (_("%B: stack size specified and %s set"),
5781 output_bfd, legacy_symbol);
5782 else if (h->root.u.def.section != bfd_abs_section_ptr)
5783 /* xgettext:c-format */
5784 _bfd_error_handler (_("%B: %s not absolute"),
5785 output_bfd, legacy_symbol);
5786 else
5787 info->stacksize = h->root.u.def.value;
5788 }
5789
5790 if (!info->stacksize)
5791 /* If the user didn't set a size, or explicitly inhibit the
5792 size, set it now. */
5793 info->stacksize = default_size;
5794
5795 /* Provide the legacy symbol, if it is referenced. */
5796 if (h && (h->root.type == bfd_link_hash_undefined
5797 || h->root.type == bfd_link_hash_undefweak))
5798 {
5799 struct bfd_link_hash_entry *bh = NULL;
5800
5801 if (!(_bfd_generic_link_add_one_symbol
5802 (info, output_bfd, legacy_symbol,
5803 BSF_GLOBAL, bfd_abs_section_ptr,
5804 info->stacksize >= 0 ? info->stacksize : 0,
5805 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5806 return FALSE;
5807
5808 h = (struct elf_link_hash_entry *) bh;
5809 h->def_regular = 1;
5810 h->type = STT_OBJECT;
5811 }
5812
5813 return TRUE;
5814 }
5815
5816 /* Set up the sizes and contents of the ELF dynamic sections. This is
5817 called by the ELF linker emulation before_allocation routine. We
5818 must set the sizes of the sections before the linker sets the
5819 addresses of the various sections. */
5820
5821 bfd_boolean
5822 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5823 const char *soname,
5824 const char *rpath,
5825 const char *filter_shlib,
5826 const char *audit,
5827 const char *depaudit,
5828 const char * const *auxiliary_filters,
5829 struct bfd_link_info *info,
5830 asection **sinterpptr)
5831 {
5832 size_t soname_indx;
5833 bfd *dynobj;
5834 const struct elf_backend_data *bed;
5835 struct elf_info_failed asvinfo;
5836
5837 *sinterpptr = NULL;
5838
5839 soname_indx = (size_t) -1;
5840
5841 if (!is_elf_hash_table (info->hash))
5842 return TRUE;
5843
5844 bed = get_elf_backend_data (output_bfd);
5845
5846 /* Any syms created from now on start with -1 in
5847 got.refcount/offset and plt.refcount/offset. */
5848 elf_hash_table (info)->init_got_refcount
5849 = elf_hash_table (info)->init_got_offset;
5850 elf_hash_table (info)->init_plt_refcount
5851 = elf_hash_table (info)->init_plt_offset;
5852
5853 if (bfd_link_relocatable (info)
5854 && !_bfd_elf_size_group_sections (info))
5855 return FALSE;
5856
5857 /* The backend may have to create some sections regardless of whether
5858 we're dynamic or not. */
5859 if (bed->elf_backend_always_size_sections
5860 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5861 return FALSE;
5862
5863 /* Determine any GNU_STACK segment requirements, after the backend
5864 has had a chance to set a default segment size. */
5865 if (info->execstack)
5866 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
5867 else if (info->noexecstack)
5868 elf_stack_flags (output_bfd) = PF_R | PF_W;
5869 else
5870 {
5871 bfd *inputobj;
5872 asection *notesec = NULL;
5873 int exec = 0;
5874
5875 for (inputobj = info->input_bfds;
5876 inputobj;
5877 inputobj = inputobj->link.next)
5878 {
5879 asection *s;
5880
5881 if (inputobj->flags
5882 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5883 continue;
5884 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5885 if (s)
5886 {
5887 if (s->flags & SEC_CODE)
5888 exec = PF_X;
5889 notesec = s;
5890 }
5891 else if (bed->default_execstack)
5892 exec = PF_X;
5893 }
5894 if (notesec || info->stacksize > 0)
5895 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
5896 if (notesec && exec && bfd_link_relocatable (info)
5897 && notesec->output_section != bfd_abs_section_ptr)
5898 notesec->output_section->flags |= SEC_CODE;
5899 }
5900
5901 dynobj = elf_hash_table (info)->dynobj;
5902
5903 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5904 {
5905 struct elf_info_failed eif;
5906 struct elf_link_hash_entry *h;
5907 asection *dynstr;
5908 struct bfd_elf_version_tree *t;
5909 struct bfd_elf_version_expr *d;
5910 asection *s;
5911 bfd_boolean all_defined;
5912
5913 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
5914 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
5915
5916 if (soname != NULL)
5917 {
5918 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5919 soname, TRUE);
5920 if (soname_indx == (size_t) -1
5921 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5922 return FALSE;
5923 }
5924
5925 if (info->symbolic)
5926 {
5927 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5928 return FALSE;
5929 info->flags |= DF_SYMBOLIC;
5930 }
5931
5932 if (rpath != NULL)
5933 {
5934 size_t indx;
5935 bfd_vma tag;
5936
5937 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5938 TRUE);
5939 if (indx == (size_t) -1)
5940 return FALSE;
5941
5942 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
5943 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
5944 return FALSE;
5945 }
5946
5947 if (filter_shlib != NULL)
5948 {
5949 size_t indx;
5950
5951 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5952 filter_shlib, TRUE);
5953 if (indx == (size_t) -1
5954 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5955 return FALSE;
5956 }
5957
5958 if (auxiliary_filters != NULL)
5959 {
5960 const char * const *p;
5961
5962 for (p = auxiliary_filters; *p != NULL; p++)
5963 {
5964 size_t indx;
5965
5966 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5967 *p, TRUE);
5968 if (indx == (size_t) -1
5969 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5970 return FALSE;
5971 }
5972 }
5973
5974 if (audit != NULL)
5975 {
5976 size_t indx;
5977
5978 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5979 TRUE);
5980 if (indx == (size_t) -1
5981 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5982 return FALSE;
5983 }
5984
5985 if (depaudit != NULL)
5986 {
5987 size_t indx;
5988
5989 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
5990 TRUE);
5991 if (indx == (size_t) -1
5992 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
5993 return FALSE;
5994 }
5995
5996 eif.info = info;
5997 eif.failed = FALSE;
5998
5999 /* If we are supposed to export all symbols into the dynamic symbol
6000 table (this is not the normal case), then do so. */
6001 if (info->export_dynamic
6002 || (bfd_link_executable (info) && info->dynamic))
6003 {
6004 elf_link_hash_traverse (elf_hash_table (info),
6005 _bfd_elf_export_symbol,
6006 &eif);
6007 if (eif.failed)
6008 return FALSE;
6009 }
6010
6011 /* Make all global versions with definition. */
6012 for (t = info->version_info; t != NULL; t = t->next)
6013 for (d = t->globals.list; d != NULL; d = d->next)
6014 if (!d->symver && d->literal)
6015 {
6016 const char *verstr, *name;
6017 size_t namelen, verlen, newlen;
6018 char *newname, *p, leading_char;
6019 struct elf_link_hash_entry *newh;
6020
6021 leading_char = bfd_get_symbol_leading_char (output_bfd);
6022 name = d->pattern;
6023 namelen = strlen (name) + (leading_char != '\0');
6024 verstr = t->name;
6025 verlen = strlen (verstr);
6026 newlen = namelen + verlen + 3;
6027
6028 newname = (char *) bfd_malloc (newlen);
6029 if (newname == NULL)
6030 return FALSE;
6031 newname[0] = leading_char;
6032 memcpy (newname + (leading_char != '\0'), name, namelen);
6033
6034 /* Check the hidden versioned definition. */
6035 p = newname + namelen;
6036 *p++ = ELF_VER_CHR;
6037 memcpy (p, verstr, verlen + 1);
6038 newh = elf_link_hash_lookup (elf_hash_table (info),
6039 newname, FALSE, FALSE,
6040 FALSE);
6041 if (newh == NULL
6042 || (newh->root.type != bfd_link_hash_defined
6043 && newh->root.type != bfd_link_hash_defweak))
6044 {
6045 /* Check the default versioned definition. */
6046 *p++ = ELF_VER_CHR;
6047 memcpy (p, verstr, verlen + 1);
6048 newh = elf_link_hash_lookup (elf_hash_table (info),
6049 newname, FALSE, FALSE,
6050 FALSE);
6051 }
6052 free (newname);
6053
6054 /* Mark this version if there is a definition and it is
6055 not defined in a shared object. */
6056 if (newh != NULL
6057 && !newh->def_dynamic
6058 && (newh->root.type == bfd_link_hash_defined
6059 || newh->root.type == bfd_link_hash_defweak))
6060 d->symver = 1;
6061 }
6062
6063 /* Attach all the symbols to their version information. */
6064 asvinfo.info = info;
6065 asvinfo.failed = FALSE;
6066
6067 elf_link_hash_traverse (elf_hash_table (info),
6068 _bfd_elf_link_assign_sym_version,
6069 &asvinfo);
6070 if (asvinfo.failed)
6071 return FALSE;
6072
6073 if (!info->allow_undefined_version)
6074 {
6075 /* Check if all global versions have a definition. */
6076 all_defined = TRUE;
6077 for (t = info->version_info; t != NULL; t = t->next)
6078 for (d = t->globals.list; d != NULL; d = d->next)
6079 if (d->literal && !d->symver && !d->script)
6080 {
6081 _bfd_error_handler
6082 (_("%s: undefined version: %s"),
6083 d->pattern, t->name);
6084 all_defined = FALSE;
6085 }
6086
6087 if (!all_defined)
6088 {
6089 bfd_set_error (bfd_error_bad_value);
6090 return FALSE;
6091 }
6092 }
6093
6094 /* Find all symbols which were defined in a dynamic object and make
6095 the backend pick a reasonable value for them. */
6096 elf_link_hash_traverse (elf_hash_table (info),
6097 _bfd_elf_adjust_dynamic_symbol,
6098 &eif);
6099 if (eif.failed)
6100 return FALSE;
6101
6102 /* Add some entries to the .dynamic section. We fill in some of the
6103 values later, in bfd_elf_final_link, but we must add the entries
6104 now so that we know the final size of the .dynamic section. */
6105
6106 /* If there are initialization and/or finalization functions to
6107 call then add the corresponding DT_INIT/DT_FINI entries. */
6108 h = (info->init_function
6109 ? elf_link_hash_lookup (elf_hash_table (info),
6110 info->init_function, FALSE,
6111 FALSE, FALSE)
6112 : NULL);
6113 if (h != NULL
6114 && (h->ref_regular
6115 || h->def_regular))
6116 {
6117 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6118 return FALSE;
6119 }
6120 h = (info->fini_function
6121 ? elf_link_hash_lookup (elf_hash_table (info),
6122 info->fini_function, FALSE,
6123 FALSE, FALSE)
6124 : NULL);
6125 if (h != NULL
6126 && (h->ref_regular
6127 || h->def_regular))
6128 {
6129 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6130 return FALSE;
6131 }
6132
6133 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6134 if (s != NULL && s->linker_has_input)
6135 {
6136 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6137 if (! bfd_link_executable (info))
6138 {
6139 bfd *sub;
6140 asection *o;
6141
6142 for (sub = info->input_bfds; sub != NULL;
6143 sub = sub->link.next)
6144 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
6145 for (o = sub->sections; o != NULL; o = o->next)
6146 if (elf_section_data (o)->this_hdr.sh_type
6147 == SHT_PREINIT_ARRAY)
6148 {
6149 _bfd_error_handler
6150 (_("%B: .preinit_array section is not allowed in DSO"),
6151 sub);
6152 break;
6153 }
6154
6155 bfd_set_error (bfd_error_nonrepresentable_section);
6156 return FALSE;
6157 }
6158
6159 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6160 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6161 return FALSE;
6162 }
6163 s = bfd_get_section_by_name (output_bfd, ".init_array");
6164 if (s != NULL && s->linker_has_input)
6165 {
6166 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6167 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6168 return FALSE;
6169 }
6170 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6171 if (s != NULL && s->linker_has_input)
6172 {
6173 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6174 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6175 return FALSE;
6176 }
6177
6178 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6179 /* If .dynstr is excluded from the link, we don't want any of
6180 these tags. Strictly, we should be checking each section
6181 individually; This quick check covers for the case where
6182 someone does a /DISCARD/ : { *(*) }. */
6183 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6184 {
6185 bfd_size_type strsize;
6186
6187 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6188 if ((info->emit_hash
6189 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6190 || (info->emit_gnu_hash
6191 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6192 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6193 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6194 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6195 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6196 bed->s->sizeof_sym))
6197 return FALSE;
6198 }
6199 }
6200
6201 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6202 return FALSE;
6203
6204 /* The backend must work out the sizes of all the other dynamic
6205 sections. */
6206 if (dynobj != NULL
6207 && bed->elf_backend_size_dynamic_sections != NULL
6208 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6209 return FALSE;
6210
6211 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6212 {
6213 unsigned long section_sym_count;
6214 struct bfd_elf_version_tree *verdefs;
6215 asection *s;
6216
6217 /* Set up the version definition section. */
6218 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6219 BFD_ASSERT (s != NULL);
6220
6221 /* We may have created additional version definitions if we are
6222 just linking a regular application. */
6223 verdefs = info->version_info;
6224
6225 /* Skip anonymous version tag. */
6226 if (verdefs != NULL && verdefs->vernum == 0)
6227 verdefs = verdefs->next;
6228
6229 if (verdefs == NULL && !info->create_default_symver)
6230 s->flags |= SEC_EXCLUDE;
6231 else
6232 {
6233 unsigned int cdefs;
6234 bfd_size_type size;
6235 struct bfd_elf_version_tree *t;
6236 bfd_byte *p;
6237 Elf_Internal_Verdef def;
6238 Elf_Internal_Verdaux defaux;
6239 struct bfd_link_hash_entry *bh;
6240 struct elf_link_hash_entry *h;
6241 const char *name;
6242
6243 cdefs = 0;
6244 size = 0;
6245
6246 /* Make space for the base version. */
6247 size += sizeof (Elf_External_Verdef);
6248 size += sizeof (Elf_External_Verdaux);
6249 ++cdefs;
6250
6251 /* Make space for the default version. */
6252 if (info->create_default_symver)
6253 {
6254 size += sizeof (Elf_External_Verdef);
6255 ++cdefs;
6256 }
6257
6258 for (t = verdefs; t != NULL; t = t->next)
6259 {
6260 struct bfd_elf_version_deps *n;
6261
6262 /* Don't emit base version twice. */
6263 if (t->vernum == 0)
6264 continue;
6265
6266 size += sizeof (Elf_External_Verdef);
6267 size += sizeof (Elf_External_Verdaux);
6268 ++cdefs;
6269
6270 for (n = t->deps; n != NULL; n = n->next)
6271 size += sizeof (Elf_External_Verdaux);
6272 }
6273
6274 s->size = size;
6275 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6276 if (s->contents == NULL && s->size != 0)
6277 return FALSE;
6278
6279 /* Fill in the version definition section. */
6280
6281 p = s->contents;
6282
6283 def.vd_version = VER_DEF_CURRENT;
6284 def.vd_flags = VER_FLG_BASE;
6285 def.vd_ndx = 1;
6286 def.vd_cnt = 1;
6287 if (info->create_default_symver)
6288 {
6289 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6290 def.vd_next = sizeof (Elf_External_Verdef);
6291 }
6292 else
6293 {
6294 def.vd_aux = sizeof (Elf_External_Verdef);
6295 def.vd_next = (sizeof (Elf_External_Verdef)
6296 + sizeof (Elf_External_Verdaux));
6297 }
6298
6299 if (soname_indx != (size_t) -1)
6300 {
6301 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6302 soname_indx);
6303 def.vd_hash = bfd_elf_hash (soname);
6304 defaux.vda_name = soname_indx;
6305 name = soname;
6306 }
6307 else
6308 {
6309 size_t indx;
6310
6311 name = lbasename (output_bfd->filename);
6312 def.vd_hash = bfd_elf_hash (name);
6313 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6314 name, FALSE);
6315 if (indx == (size_t) -1)
6316 return FALSE;
6317 defaux.vda_name = indx;
6318 }
6319 defaux.vda_next = 0;
6320
6321 _bfd_elf_swap_verdef_out (output_bfd, &def,
6322 (Elf_External_Verdef *) p);
6323 p += sizeof (Elf_External_Verdef);
6324 if (info->create_default_symver)
6325 {
6326 /* Add a symbol representing this version. */
6327 bh = NULL;
6328 if (! (_bfd_generic_link_add_one_symbol
6329 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6330 0, NULL, FALSE,
6331 get_elf_backend_data (dynobj)->collect, &bh)))
6332 return FALSE;
6333 h = (struct elf_link_hash_entry *) bh;
6334 h->non_elf = 0;
6335 h->def_regular = 1;
6336 h->type = STT_OBJECT;
6337 h->verinfo.vertree = NULL;
6338
6339 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6340 return FALSE;
6341
6342 /* Create a duplicate of the base version with the same
6343 aux block, but different flags. */
6344 def.vd_flags = 0;
6345 def.vd_ndx = 2;
6346 def.vd_aux = sizeof (Elf_External_Verdef);
6347 if (verdefs)
6348 def.vd_next = (sizeof (Elf_External_Verdef)
6349 + sizeof (Elf_External_Verdaux));
6350 else
6351 def.vd_next = 0;
6352 _bfd_elf_swap_verdef_out (output_bfd, &def,
6353 (Elf_External_Verdef *) p);
6354 p += sizeof (Elf_External_Verdef);
6355 }
6356 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6357 (Elf_External_Verdaux *) p);
6358 p += sizeof (Elf_External_Verdaux);
6359
6360 for (t = verdefs; t != NULL; t = t->next)
6361 {
6362 unsigned int cdeps;
6363 struct bfd_elf_version_deps *n;
6364
6365 /* Don't emit the base version twice. */
6366 if (t->vernum == 0)
6367 continue;
6368
6369 cdeps = 0;
6370 for (n = t->deps; n != NULL; n = n->next)
6371 ++cdeps;
6372
6373 /* Add a symbol representing this version. */
6374 bh = NULL;
6375 if (! (_bfd_generic_link_add_one_symbol
6376 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6377 0, NULL, FALSE,
6378 get_elf_backend_data (dynobj)->collect, &bh)))
6379 return FALSE;
6380 h = (struct elf_link_hash_entry *) bh;
6381 h->non_elf = 0;
6382 h->def_regular = 1;
6383 h->type = STT_OBJECT;
6384 h->verinfo.vertree = t;
6385
6386 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6387 return FALSE;
6388
6389 def.vd_version = VER_DEF_CURRENT;
6390 def.vd_flags = 0;
6391 if (t->globals.list == NULL
6392 && t->locals.list == NULL
6393 && ! t->used)
6394 def.vd_flags |= VER_FLG_WEAK;
6395 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6396 def.vd_cnt = cdeps + 1;
6397 def.vd_hash = bfd_elf_hash (t->name);
6398 def.vd_aux = sizeof (Elf_External_Verdef);
6399 def.vd_next = 0;
6400
6401 /* If a basever node is next, it *must* be the last node in
6402 the chain, otherwise Verdef construction breaks. */
6403 if (t->next != NULL && t->next->vernum == 0)
6404 BFD_ASSERT (t->next->next == NULL);
6405
6406 if (t->next != NULL && t->next->vernum != 0)
6407 def.vd_next = (sizeof (Elf_External_Verdef)
6408 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6409
6410 _bfd_elf_swap_verdef_out (output_bfd, &def,
6411 (Elf_External_Verdef *) p);
6412 p += sizeof (Elf_External_Verdef);
6413
6414 defaux.vda_name = h->dynstr_index;
6415 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6416 h->dynstr_index);
6417 defaux.vda_next = 0;
6418 if (t->deps != NULL)
6419 defaux.vda_next = sizeof (Elf_External_Verdaux);
6420 t->name_indx = defaux.vda_name;
6421
6422 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6423 (Elf_External_Verdaux *) p);
6424 p += sizeof (Elf_External_Verdaux);
6425
6426 for (n = t->deps; n != NULL; n = n->next)
6427 {
6428 if (n->version_needed == NULL)
6429 {
6430 /* This can happen if there was an error in the
6431 version script. */
6432 defaux.vda_name = 0;
6433 }
6434 else
6435 {
6436 defaux.vda_name = n->version_needed->name_indx;
6437 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6438 defaux.vda_name);
6439 }
6440 if (n->next == NULL)
6441 defaux.vda_next = 0;
6442 else
6443 defaux.vda_next = sizeof (Elf_External_Verdaux);
6444
6445 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6446 (Elf_External_Verdaux *) p);
6447 p += sizeof (Elf_External_Verdaux);
6448 }
6449 }
6450
6451 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6452 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6453 return FALSE;
6454
6455 elf_tdata (output_bfd)->cverdefs = cdefs;
6456 }
6457
6458 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6459 {
6460 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6461 return FALSE;
6462 }
6463 else if (info->flags & DF_BIND_NOW)
6464 {
6465 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6466 return FALSE;
6467 }
6468
6469 if (info->flags_1)
6470 {
6471 if (bfd_link_executable (info))
6472 info->flags_1 &= ~ (DF_1_INITFIRST
6473 | DF_1_NODELETE
6474 | DF_1_NOOPEN);
6475 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6476 return FALSE;
6477 }
6478
6479 /* Work out the size of the version reference section. */
6480
6481 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6482 BFD_ASSERT (s != NULL);
6483 {
6484 struct elf_find_verdep_info sinfo;
6485
6486 sinfo.info = info;
6487 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6488 if (sinfo.vers == 0)
6489 sinfo.vers = 1;
6490 sinfo.failed = FALSE;
6491
6492 elf_link_hash_traverse (elf_hash_table (info),
6493 _bfd_elf_link_find_version_dependencies,
6494 &sinfo);
6495 if (sinfo.failed)
6496 return FALSE;
6497
6498 if (elf_tdata (output_bfd)->verref == NULL)
6499 s->flags |= SEC_EXCLUDE;
6500 else
6501 {
6502 Elf_Internal_Verneed *t;
6503 unsigned int size;
6504 unsigned int crefs;
6505 bfd_byte *p;
6506
6507 /* Build the version dependency section. */
6508 size = 0;
6509 crefs = 0;
6510 for (t = elf_tdata (output_bfd)->verref;
6511 t != NULL;
6512 t = t->vn_nextref)
6513 {
6514 Elf_Internal_Vernaux *a;
6515
6516 size += sizeof (Elf_External_Verneed);
6517 ++crefs;
6518 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6519 size += sizeof (Elf_External_Vernaux);
6520 }
6521
6522 s->size = size;
6523 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6524 if (s->contents == NULL)
6525 return FALSE;
6526
6527 p = s->contents;
6528 for (t = elf_tdata (output_bfd)->verref;
6529 t != NULL;
6530 t = t->vn_nextref)
6531 {
6532 unsigned int caux;
6533 Elf_Internal_Vernaux *a;
6534 size_t indx;
6535
6536 caux = 0;
6537 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6538 ++caux;
6539
6540 t->vn_version = VER_NEED_CURRENT;
6541 t->vn_cnt = caux;
6542 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6543 elf_dt_name (t->vn_bfd) != NULL
6544 ? elf_dt_name (t->vn_bfd)
6545 : lbasename (t->vn_bfd->filename),
6546 FALSE);
6547 if (indx == (size_t) -1)
6548 return FALSE;
6549 t->vn_file = indx;
6550 t->vn_aux = sizeof (Elf_External_Verneed);
6551 if (t->vn_nextref == NULL)
6552 t->vn_next = 0;
6553 else
6554 t->vn_next = (sizeof (Elf_External_Verneed)
6555 + caux * sizeof (Elf_External_Vernaux));
6556
6557 _bfd_elf_swap_verneed_out (output_bfd, t,
6558 (Elf_External_Verneed *) p);
6559 p += sizeof (Elf_External_Verneed);
6560
6561 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6562 {
6563 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6564 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6565 a->vna_nodename, FALSE);
6566 if (indx == (size_t) -1)
6567 return FALSE;
6568 a->vna_name = indx;
6569 if (a->vna_nextptr == NULL)
6570 a->vna_next = 0;
6571 else
6572 a->vna_next = sizeof (Elf_External_Vernaux);
6573
6574 _bfd_elf_swap_vernaux_out (output_bfd, a,
6575 (Elf_External_Vernaux *) p);
6576 p += sizeof (Elf_External_Vernaux);
6577 }
6578 }
6579
6580 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6581 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6582 return FALSE;
6583
6584 elf_tdata (output_bfd)->cverrefs = crefs;
6585 }
6586 }
6587
6588 if ((elf_tdata (output_bfd)->cverrefs == 0
6589 && elf_tdata (output_bfd)->cverdefs == 0)
6590 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6591 &section_sym_count) == 0)
6592 {
6593 s = bfd_get_linker_section (dynobj, ".gnu.version");
6594 s->flags |= SEC_EXCLUDE;
6595 }
6596 }
6597 return TRUE;
6598 }
6599
6600 /* Find the first non-excluded output section. We'll use its
6601 section symbol for some emitted relocs. */
6602 void
6603 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6604 {
6605 asection *s;
6606
6607 for (s = output_bfd->sections; s != NULL; s = s->next)
6608 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6609 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6610 {
6611 elf_hash_table (info)->text_index_section = s;
6612 break;
6613 }
6614 }
6615
6616 /* Find two non-excluded output sections, one for code, one for data.
6617 We'll use their section symbols for some emitted relocs. */
6618 void
6619 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6620 {
6621 asection *s;
6622
6623 /* Data first, since setting text_index_section changes
6624 _bfd_elf_link_omit_section_dynsym. */
6625 for (s = output_bfd->sections; s != NULL; s = s->next)
6626 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6627 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6628 {
6629 elf_hash_table (info)->data_index_section = s;
6630 break;
6631 }
6632
6633 for (s = output_bfd->sections; s != NULL; s = s->next)
6634 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6635 == (SEC_ALLOC | SEC_READONLY))
6636 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6637 {
6638 elf_hash_table (info)->text_index_section = s;
6639 break;
6640 }
6641
6642 if (elf_hash_table (info)->text_index_section == NULL)
6643 elf_hash_table (info)->text_index_section
6644 = elf_hash_table (info)->data_index_section;
6645 }
6646
6647 bfd_boolean
6648 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6649 {
6650 const struct elf_backend_data *bed;
6651
6652 if (!is_elf_hash_table (info->hash))
6653 return TRUE;
6654
6655 bed = get_elf_backend_data (output_bfd);
6656 (*bed->elf_backend_init_index_section) (output_bfd, info);
6657
6658 if (elf_hash_table (info)->dynamic_sections_created)
6659 {
6660 bfd *dynobj;
6661 asection *s;
6662 bfd_size_type dynsymcount;
6663 unsigned long section_sym_count;
6664 unsigned int dtagcount;
6665
6666 dynobj = elf_hash_table (info)->dynobj;
6667
6668 /* Assign dynsym indicies. In a shared library we generate a
6669 section symbol for each output section, which come first.
6670 Next come all of the back-end allocated local dynamic syms,
6671 followed by the rest of the global symbols. */
6672
6673 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6674 &section_sym_count);
6675
6676 /* Work out the size of the symbol version section. */
6677 s = bfd_get_linker_section (dynobj, ".gnu.version");
6678 BFD_ASSERT (s != NULL);
6679 if ((s->flags & SEC_EXCLUDE) == 0)
6680 {
6681 s->size = dynsymcount * sizeof (Elf_External_Versym);
6682 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6683 if (s->contents == NULL)
6684 return FALSE;
6685
6686 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6687 return FALSE;
6688 }
6689
6690 /* Set the size of the .dynsym and .hash sections. We counted
6691 the number of dynamic symbols in elf_link_add_object_symbols.
6692 We will build the contents of .dynsym and .hash when we build
6693 the final symbol table, because until then we do not know the
6694 correct value to give the symbols. We built the .dynstr
6695 section as we went along in elf_link_add_object_symbols. */
6696 s = elf_hash_table (info)->dynsym;
6697 BFD_ASSERT (s != NULL);
6698 s->size = dynsymcount * bed->s->sizeof_sym;
6699
6700 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6701 if (s->contents == NULL)
6702 return FALSE;
6703
6704 /* The first entry in .dynsym is a dummy symbol. Clear all the
6705 section syms, in case we don't output them all. */
6706 ++section_sym_count;
6707 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6708
6709 elf_hash_table (info)->bucketcount = 0;
6710
6711 /* Compute the size of the hashing table. As a side effect this
6712 computes the hash values for all the names we export. */
6713 if (info->emit_hash)
6714 {
6715 unsigned long int *hashcodes;
6716 struct hash_codes_info hashinf;
6717 bfd_size_type amt;
6718 unsigned long int nsyms;
6719 size_t bucketcount;
6720 size_t hash_entry_size;
6721
6722 /* Compute the hash values for all exported symbols. At the same
6723 time store the values in an array so that we could use them for
6724 optimizations. */
6725 amt = dynsymcount * sizeof (unsigned long int);
6726 hashcodes = (unsigned long int *) bfd_malloc (amt);
6727 if (hashcodes == NULL)
6728 return FALSE;
6729 hashinf.hashcodes = hashcodes;
6730 hashinf.error = FALSE;
6731
6732 /* Put all hash values in HASHCODES. */
6733 elf_link_hash_traverse (elf_hash_table (info),
6734 elf_collect_hash_codes, &hashinf);
6735 if (hashinf.error)
6736 {
6737 free (hashcodes);
6738 return FALSE;
6739 }
6740
6741 nsyms = hashinf.hashcodes - hashcodes;
6742 bucketcount
6743 = compute_bucket_count (info, hashcodes, nsyms, 0);
6744 free (hashcodes);
6745
6746 if (bucketcount == 0)
6747 return FALSE;
6748
6749 elf_hash_table (info)->bucketcount = bucketcount;
6750
6751 s = bfd_get_linker_section (dynobj, ".hash");
6752 BFD_ASSERT (s != NULL);
6753 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6754 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6755 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6756 if (s->contents == NULL)
6757 return FALSE;
6758
6759 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6760 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6761 s->contents + hash_entry_size);
6762 }
6763
6764 if (info->emit_gnu_hash)
6765 {
6766 size_t i, cnt;
6767 unsigned char *contents;
6768 struct collect_gnu_hash_codes cinfo;
6769 bfd_size_type amt;
6770 size_t bucketcount;
6771
6772 memset (&cinfo, 0, sizeof (cinfo));
6773
6774 /* Compute the hash values for all exported symbols. At the same
6775 time store the values in an array so that we could use them for
6776 optimizations. */
6777 amt = dynsymcount * 2 * sizeof (unsigned long int);
6778 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
6779 if (cinfo.hashcodes == NULL)
6780 return FALSE;
6781
6782 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6783 cinfo.min_dynindx = -1;
6784 cinfo.output_bfd = output_bfd;
6785 cinfo.bed = bed;
6786
6787 /* Put all hash values in HASHCODES. */
6788 elf_link_hash_traverse (elf_hash_table (info),
6789 elf_collect_gnu_hash_codes, &cinfo);
6790 if (cinfo.error)
6791 {
6792 free (cinfo.hashcodes);
6793 return FALSE;
6794 }
6795
6796 bucketcount
6797 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6798
6799 if (bucketcount == 0)
6800 {
6801 free (cinfo.hashcodes);
6802 return FALSE;
6803 }
6804
6805 s = bfd_get_linker_section (dynobj, ".gnu.hash");
6806 BFD_ASSERT (s != NULL);
6807
6808 if (cinfo.nsyms == 0)
6809 {
6810 /* Empty .gnu.hash section is special. */
6811 BFD_ASSERT (cinfo.min_dynindx == -1);
6812 free (cinfo.hashcodes);
6813 s->size = 5 * 4 + bed->s->arch_size / 8;
6814 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6815 if (contents == NULL)
6816 return FALSE;
6817 s->contents = contents;
6818 /* 1 empty bucket. */
6819 bfd_put_32 (output_bfd, 1, contents);
6820 /* SYMIDX above the special symbol 0. */
6821 bfd_put_32 (output_bfd, 1, contents + 4);
6822 /* Just one word for bitmask. */
6823 bfd_put_32 (output_bfd, 1, contents + 8);
6824 /* Only hash fn bloom filter. */
6825 bfd_put_32 (output_bfd, 0, contents + 12);
6826 /* No hashes are valid - empty bitmask. */
6827 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6828 /* No hashes in the only bucket. */
6829 bfd_put_32 (output_bfd, 0,
6830 contents + 16 + bed->s->arch_size / 8);
6831 }
6832 else
6833 {
6834 unsigned long int maskwords, maskbitslog2, x;
6835 BFD_ASSERT (cinfo.min_dynindx != -1);
6836
6837 x = cinfo.nsyms;
6838 maskbitslog2 = 1;
6839 while ((x >>= 1) != 0)
6840 ++maskbitslog2;
6841 if (maskbitslog2 < 3)
6842 maskbitslog2 = 5;
6843 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6844 maskbitslog2 = maskbitslog2 + 3;
6845 else
6846 maskbitslog2 = maskbitslog2 + 2;
6847 if (bed->s->arch_size == 64)
6848 {
6849 if (maskbitslog2 == 5)
6850 maskbitslog2 = 6;
6851 cinfo.shift1 = 6;
6852 }
6853 else
6854 cinfo.shift1 = 5;
6855 cinfo.mask = (1 << cinfo.shift1) - 1;
6856 cinfo.shift2 = maskbitslog2;
6857 cinfo.maskbits = 1 << maskbitslog2;
6858 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6859 amt = bucketcount * sizeof (unsigned long int) * 2;
6860 amt += maskwords * sizeof (bfd_vma);
6861 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
6862 if (cinfo.bitmask == NULL)
6863 {
6864 free (cinfo.hashcodes);
6865 return FALSE;
6866 }
6867
6868 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
6869 cinfo.indx = cinfo.counts + bucketcount;
6870 cinfo.symindx = dynsymcount - cinfo.nsyms;
6871 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6872
6873 /* Determine how often each hash bucket is used. */
6874 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6875 for (i = 0; i < cinfo.nsyms; ++i)
6876 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6877
6878 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6879 if (cinfo.counts[i] != 0)
6880 {
6881 cinfo.indx[i] = cnt;
6882 cnt += cinfo.counts[i];
6883 }
6884 BFD_ASSERT (cnt == dynsymcount);
6885 cinfo.bucketcount = bucketcount;
6886 cinfo.local_indx = cinfo.min_dynindx;
6887
6888 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6889 s->size += cinfo.maskbits / 8;
6890 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6891 if (contents == NULL)
6892 {
6893 free (cinfo.bitmask);
6894 free (cinfo.hashcodes);
6895 return FALSE;
6896 }
6897
6898 s->contents = contents;
6899 bfd_put_32 (output_bfd, bucketcount, contents);
6900 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6901 bfd_put_32 (output_bfd, maskwords, contents + 8);
6902 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6903 contents += 16 + cinfo.maskbits / 8;
6904
6905 for (i = 0; i < bucketcount; ++i)
6906 {
6907 if (cinfo.counts[i] == 0)
6908 bfd_put_32 (output_bfd, 0, contents);
6909 else
6910 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6911 contents += 4;
6912 }
6913
6914 cinfo.contents = contents;
6915
6916 /* Renumber dynamic symbols, populate .gnu.hash section. */
6917 elf_link_hash_traverse (elf_hash_table (info),
6918 elf_renumber_gnu_hash_syms, &cinfo);
6919
6920 contents = s->contents + 16;
6921 for (i = 0; i < maskwords; ++i)
6922 {
6923 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6924 contents);
6925 contents += bed->s->arch_size / 8;
6926 }
6927
6928 free (cinfo.bitmask);
6929 free (cinfo.hashcodes);
6930 }
6931 }
6932
6933 s = bfd_get_linker_section (dynobj, ".dynstr");
6934 BFD_ASSERT (s != NULL);
6935
6936 elf_finalize_dynstr (output_bfd, info);
6937
6938 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6939
6940 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6941 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6942 return FALSE;
6943 }
6944
6945 return TRUE;
6946 }
6947 \f
6948 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
6949
6950 static void
6951 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6952 asection *sec)
6953 {
6954 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
6955 sec->sec_info_type = SEC_INFO_TYPE_NONE;
6956 }
6957
6958 /* Finish SHF_MERGE section merging. */
6959
6960 bfd_boolean
6961 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
6962 {
6963 bfd *ibfd;
6964 asection *sec;
6965
6966 if (!is_elf_hash_table (info->hash))
6967 return FALSE;
6968
6969 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6970 if ((ibfd->flags & DYNAMIC) == 0
6971 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6972 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
6973 == get_elf_backend_data (obfd)->s->elfclass))
6974 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6975 if ((sec->flags & SEC_MERGE) != 0
6976 && !bfd_is_abs_section (sec->output_section))
6977 {
6978 struct bfd_elf_section_data *secdata;
6979
6980 secdata = elf_section_data (sec);
6981 if (! _bfd_add_merge_section (obfd,
6982 &elf_hash_table (info)->merge_info,
6983 sec, &secdata->sec_info))
6984 return FALSE;
6985 else if (secdata->sec_info)
6986 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
6987 }
6988
6989 if (elf_hash_table (info)->merge_info != NULL)
6990 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
6991 merge_sections_remove_hook);
6992 return TRUE;
6993 }
6994
6995 /* Create an entry in an ELF linker hash table. */
6996
6997 struct bfd_hash_entry *
6998 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6999 struct bfd_hash_table *table,
7000 const char *string)
7001 {
7002 /* Allocate the structure if it has not already been allocated by a
7003 subclass. */
7004 if (entry == NULL)
7005 {
7006 entry = (struct bfd_hash_entry *)
7007 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7008 if (entry == NULL)
7009 return entry;
7010 }
7011
7012 /* Call the allocation method of the superclass. */
7013 entry = _bfd_link_hash_newfunc (entry, table, string);
7014 if (entry != NULL)
7015 {
7016 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7017 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7018
7019 /* Set local fields. */
7020 ret->indx = -1;
7021 ret->dynindx = -1;
7022 ret->got = htab->init_got_refcount;
7023 ret->plt = htab->init_plt_refcount;
7024 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7025 - offsetof (struct elf_link_hash_entry, size)));
7026 /* Assume that we have been called by a non-ELF symbol reader.
7027 This flag is then reset by the code which reads an ELF input
7028 file. This ensures that a symbol created by a non-ELF symbol
7029 reader will have the flag set correctly. */
7030 ret->non_elf = 1;
7031 }
7032
7033 return entry;
7034 }
7035
7036 /* Copy data from an indirect symbol to its direct symbol, hiding the
7037 old indirect symbol. Also used for copying flags to a weakdef. */
7038
7039 void
7040 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7041 struct elf_link_hash_entry *dir,
7042 struct elf_link_hash_entry *ind)
7043 {
7044 struct elf_link_hash_table *htab;
7045
7046 /* Copy down any references that we may have already seen to the
7047 symbol which just became indirect if DIR isn't a hidden versioned
7048 symbol. */
7049
7050 if (dir->versioned != versioned_hidden)
7051 {
7052 dir->ref_dynamic |= ind->ref_dynamic;
7053 dir->ref_regular |= ind->ref_regular;
7054 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7055 dir->non_got_ref |= ind->non_got_ref;
7056 dir->needs_plt |= ind->needs_plt;
7057 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7058 }
7059
7060 if (ind->root.type != bfd_link_hash_indirect)
7061 return;
7062
7063 /* Copy over the global and procedure linkage table refcount entries.
7064 These may have been already set up by a check_relocs routine. */
7065 htab = elf_hash_table (info);
7066 if (ind->got.refcount > htab->init_got_refcount.refcount)
7067 {
7068 if (dir->got.refcount < 0)
7069 dir->got.refcount = 0;
7070 dir->got.refcount += ind->got.refcount;
7071 ind->got.refcount = htab->init_got_refcount.refcount;
7072 }
7073
7074 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7075 {
7076 if (dir->plt.refcount < 0)
7077 dir->plt.refcount = 0;
7078 dir->plt.refcount += ind->plt.refcount;
7079 ind->plt.refcount = htab->init_plt_refcount.refcount;
7080 }
7081
7082 if (ind->dynindx != -1)
7083 {
7084 if (dir->dynindx != -1)
7085 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7086 dir->dynindx = ind->dynindx;
7087 dir->dynstr_index = ind->dynstr_index;
7088 ind->dynindx = -1;
7089 ind->dynstr_index = 0;
7090 }
7091 }
7092
7093 void
7094 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7095 struct elf_link_hash_entry *h,
7096 bfd_boolean force_local)
7097 {
7098 /* STT_GNU_IFUNC symbol must go through PLT. */
7099 if (h->type != STT_GNU_IFUNC)
7100 {
7101 h->plt = elf_hash_table (info)->init_plt_offset;
7102 h->needs_plt = 0;
7103 }
7104 if (force_local)
7105 {
7106 h->forced_local = 1;
7107 if (h->dynindx != -1)
7108 {
7109 h->dynindx = -1;
7110 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7111 h->dynstr_index);
7112 }
7113 }
7114 }
7115
7116 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7117 caller. */
7118
7119 bfd_boolean
7120 _bfd_elf_link_hash_table_init
7121 (struct elf_link_hash_table *table,
7122 bfd *abfd,
7123 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7124 struct bfd_hash_table *,
7125 const char *),
7126 unsigned int entsize,
7127 enum elf_target_id target_id)
7128 {
7129 bfd_boolean ret;
7130 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7131
7132 table->init_got_refcount.refcount = can_refcount - 1;
7133 table->init_plt_refcount.refcount = can_refcount - 1;
7134 table->init_got_offset.offset = -(bfd_vma) 1;
7135 table->init_plt_offset.offset = -(bfd_vma) 1;
7136 /* The first dynamic symbol is a dummy. */
7137 table->dynsymcount = 1;
7138
7139 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7140
7141 table->root.type = bfd_link_elf_hash_table;
7142 table->hash_table_id = target_id;
7143
7144 return ret;
7145 }
7146
7147 /* Create an ELF linker hash table. */
7148
7149 struct bfd_link_hash_table *
7150 _bfd_elf_link_hash_table_create (bfd *abfd)
7151 {
7152 struct elf_link_hash_table *ret;
7153 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7154
7155 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7156 if (ret == NULL)
7157 return NULL;
7158
7159 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7160 sizeof (struct elf_link_hash_entry),
7161 GENERIC_ELF_DATA))
7162 {
7163 free (ret);
7164 return NULL;
7165 }
7166 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7167
7168 return &ret->root;
7169 }
7170
7171 /* Destroy an ELF linker hash table. */
7172
7173 void
7174 _bfd_elf_link_hash_table_free (bfd *obfd)
7175 {
7176 struct elf_link_hash_table *htab;
7177
7178 htab = (struct elf_link_hash_table *) obfd->link.hash;
7179 if (htab->dynstr != NULL)
7180 _bfd_elf_strtab_free (htab->dynstr);
7181 _bfd_merge_sections_free (htab->merge_info);
7182 _bfd_generic_link_hash_table_free (obfd);
7183 }
7184
7185 /* This is a hook for the ELF emulation code in the generic linker to
7186 tell the backend linker what file name to use for the DT_NEEDED
7187 entry for a dynamic object. */
7188
7189 void
7190 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7191 {
7192 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7193 && bfd_get_format (abfd) == bfd_object)
7194 elf_dt_name (abfd) = name;
7195 }
7196
7197 int
7198 bfd_elf_get_dyn_lib_class (bfd *abfd)
7199 {
7200 int lib_class;
7201 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7202 && bfd_get_format (abfd) == bfd_object)
7203 lib_class = elf_dyn_lib_class (abfd);
7204 else
7205 lib_class = 0;
7206 return lib_class;
7207 }
7208
7209 void
7210 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7211 {
7212 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7213 && bfd_get_format (abfd) == bfd_object)
7214 elf_dyn_lib_class (abfd) = lib_class;
7215 }
7216
7217 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7218 the linker ELF emulation code. */
7219
7220 struct bfd_link_needed_list *
7221 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7222 struct bfd_link_info *info)
7223 {
7224 if (! is_elf_hash_table (info->hash))
7225 return NULL;
7226 return elf_hash_table (info)->needed;
7227 }
7228
7229 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7230 hook for the linker ELF emulation code. */
7231
7232 struct bfd_link_needed_list *
7233 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7234 struct bfd_link_info *info)
7235 {
7236 if (! is_elf_hash_table (info->hash))
7237 return NULL;
7238 return elf_hash_table (info)->runpath;
7239 }
7240
7241 /* Get the name actually used for a dynamic object for a link. This
7242 is the SONAME entry if there is one. Otherwise, it is the string
7243 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7244
7245 const char *
7246 bfd_elf_get_dt_soname (bfd *abfd)
7247 {
7248 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7249 && bfd_get_format (abfd) == bfd_object)
7250 return elf_dt_name (abfd);
7251 return NULL;
7252 }
7253
7254 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7255 the ELF linker emulation code. */
7256
7257 bfd_boolean
7258 bfd_elf_get_bfd_needed_list (bfd *abfd,
7259 struct bfd_link_needed_list **pneeded)
7260 {
7261 asection *s;
7262 bfd_byte *dynbuf = NULL;
7263 unsigned int elfsec;
7264 unsigned long shlink;
7265 bfd_byte *extdyn, *extdynend;
7266 size_t extdynsize;
7267 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7268
7269 *pneeded = NULL;
7270
7271 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7272 || bfd_get_format (abfd) != bfd_object)
7273 return TRUE;
7274
7275 s = bfd_get_section_by_name (abfd, ".dynamic");
7276 if (s == NULL || s->size == 0)
7277 return TRUE;
7278
7279 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7280 goto error_return;
7281
7282 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7283 if (elfsec == SHN_BAD)
7284 goto error_return;
7285
7286 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7287
7288 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7289 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7290
7291 extdyn = dynbuf;
7292 extdynend = extdyn + s->size;
7293 for (; extdyn < extdynend; extdyn += extdynsize)
7294 {
7295 Elf_Internal_Dyn dyn;
7296
7297 (*swap_dyn_in) (abfd, extdyn, &dyn);
7298
7299 if (dyn.d_tag == DT_NULL)
7300 break;
7301
7302 if (dyn.d_tag == DT_NEEDED)
7303 {
7304 const char *string;
7305 struct bfd_link_needed_list *l;
7306 unsigned int tagv = dyn.d_un.d_val;
7307 bfd_size_type amt;
7308
7309 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7310 if (string == NULL)
7311 goto error_return;
7312
7313 amt = sizeof *l;
7314 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7315 if (l == NULL)
7316 goto error_return;
7317
7318 l->by = abfd;
7319 l->name = string;
7320 l->next = *pneeded;
7321 *pneeded = l;
7322 }
7323 }
7324
7325 free (dynbuf);
7326
7327 return TRUE;
7328
7329 error_return:
7330 if (dynbuf != NULL)
7331 free (dynbuf);
7332 return FALSE;
7333 }
7334
7335 struct elf_symbuf_symbol
7336 {
7337 unsigned long st_name; /* Symbol name, index in string tbl */
7338 unsigned char st_info; /* Type and binding attributes */
7339 unsigned char st_other; /* Visibilty, and target specific */
7340 };
7341
7342 struct elf_symbuf_head
7343 {
7344 struct elf_symbuf_symbol *ssym;
7345 size_t count;
7346 unsigned int st_shndx;
7347 };
7348
7349 struct elf_symbol
7350 {
7351 union
7352 {
7353 Elf_Internal_Sym *isym;
7354 struct elf_symbuf_symbol *ssym;
7355 } u;
7356 const char *name;
7357 };
7358
7359 /* Sort references to symbols by ascending section number. */
7360
7361 static int
7362 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7363 {
7364 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7365 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7366
7367 return s1->st_shndx - s2->st_shndx;
7368 }
7369
7370 static int
7371 elf_sym_name_compare (const void *arg1, const void *arg2)
7372 {
7373 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7374 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7375 return strcmp (s1->name, s2->name);
7376 }
7377
7378 static struct elf_symbuf_head *
7379 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
7380 {
7381 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7382 struct elf_symbuf_symbol *ssym;
7383 struct elf_symbuf_head *ssymbuf, *ssymhead;
7384 size_t i, shndx_count, total_size;
7385
7386 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7387 if (indbuf == NULL)
7388 return NULL;
7389
7390 for (ind = indbuf, i = 0; i < symcount; i++)
7391 if (isymbuf[i].st_shndx != SHN_UNDEF)
7392 *ind++ = &isymbuf[i];
7393 indbufend = ind;
7394
7395 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7396 elf_sort_elf_symbol);
7397
7398 shndx_count = 0;
7399 if (indbufend > indbuf)
7400 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7401 if (ind[0]->st_shndx != ind[1]->st_shndx)
7402 shndx_count++;
7403
7404 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7405 + (indbufend - indbuf) * sizeof (*ssym));
7406 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7407 if (ssymbuf == NULL)
7408 {
7409 free (indbuf);
7410 return NULL;
7411 }
7412
7413 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7414 ssymbuf->ssym = NULL;
7415 ssymbuf->count = shndx_count;
7416 ssymbuf->st_shndx = 0;
7417 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7418 {
7419 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7420 {
7421 ssymhead++;
7422 ssymhead->ssym = ssym;
7423 ssymhead->count = 0;
7424 ssymhead->st_shndx = (*ind)->st_shndx;
7425 }
7426 ssym->st_name = (*ind)->st_name;
7427 ssym->st_info = (*ind)->st_info;
7428 ssym->st_other = (*ind)->st_other;
7429 ssymhead->count++;
7430 }
7431 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
7432 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7433 == total_size));
7434
7435 free (indbuf);
7436 return ssymbuf;
7437 }
7438
7439 /* Check if 2 sections define the same set of local and global
7440 symbols. */
7441
7442 static bfd_boolean
7443 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7444 struct bfd_link_info *info)
7445 {
7446 bfd *bfd1, *bfd2;
7447 const struct elf_backend_data *bed1, *bed2;
7448 Elf_Internal_Shdr *hdr1, *hdr2;
7449 size_t symcount1, symcount2;
7450 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7451 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7452 Elf_Internal_Sym *isym, *isymend;
7453 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7454 size_t count1, count2, i;
7455 unsigned int shndx1, shndx2;
7456 bfd_boolean result;
7457
7458 bfd1 = sec1->owner;
7459 bfd2 = sec2->owner;
7460
7461 /* Both sections have to be in ELF. */
7462 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7463 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7464 return FALSE;
7465
7466 if (elf_section_type (sec1) != elf_section_type (sec2))
7467 return FALSE;
7468
7469 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7470 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7471 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7472 return FALSE;
7473
7474 bed1 = get_elf_backend_data (bfd1);
7475 bed2 = get_elf_backend_data (bfd2);
7476 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7477 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7478 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7479 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7480
7481 if (symcount1 == 0 || symcount2 == 0)
7482 return FALSE;
7483
7484 result = FALSE;
7485 isymbuf1 = NULL;
7486 isymbuf2 = NULL;
7487 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7488 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7489
7490 if (ssymbuf1 == NULL)
7491 {
7492 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7493 NULL, NULL, NULL);
7494 if (isymbuf1 == NULL)
7495 goto done;
7496
7497 if (!info->reduce_memory_overheads)
7498 elf_tdata (bfd1)->symbuf = ssymbuf1
7499 = elf_create_symbuf (symcount1, isymbuf1);
7500 }
7501
7502 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7503 {
7504 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7505 NULL, NULL, NULL);
7506 if (isymbuf2 == NULL)
7507 goto done;
7508
7509 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7510 elf_tdata (bfd2)->symbuf = ssymbuf2
7511 = elf_create_symbuf (symcount2, isymbuf2);
7512 }
7513
7514 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7515 {
7516 /* Optimized faster version. */
7517 size_t lo, hi, mid;
7518 struct elf_symbol *symp;
7519 struct elf_symbuf_symbol *ssym, *ssymend;
7520
7521 lo = 0;
7522 hi = ssymbuf1->count;
7523 ssymbuf1++;
7524 count1 = 0;
7525 while (lo < hi)
7526 {
7527 mid = (lo + hi) / 2;
7528 if (shndx1 < ssymbuf1[mid].st_shndx)
7529 hi = mid;
7530 else if (shndx1 > ssymbuf1[mid].st_shndx)
7531 lo = mid + 1;
7532 else
7533 {
7534 count1 = ssymbuf1[mid].count;
7535 ssymbuf1 += mid;
7536 break;
7537 }
7538 }
7539
7540 lo = 0;
7541 hi = ssymbuf2->count;
7542 ssymbuf2++;
7543 count2 = 0;
7544 while (lo < hi)
7545 {
7546 mid = (lo + hi) / 2;
7547 if (shndx2 < ssymbuf2[mid].st_shndx)
7548 hi = mid;
7549 else if (shndx2 > ssymbuf2[mid].st_shndx)
7550 lo = mid + 1;
7551 else
7552 {
7553 count2 = ssymbuf2[mid].count;
7554 ssymbuf2 += mid;
7555 break;
7556 }
7557 }
7558
7559 if (count1 == 0 || count2 == 0 || count1 != count2)
7560 goto done;
7561
7562 symtable1
7563 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7564 symtable2
7565 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
7566 if (symtable1 == NULL || symtable2 == NULL)
7567 goto done;
7568
7569 symp = symtable1;
7570 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7571 ssym < ssymend; ssym++, symp++)
7572 {
7573 symp->u.ssym = ssym;
7574 symp->name = bfd_elf_string_from_elf_section (bfd1,
7575 hdr1->sh_link,
7576 ssym->st_name);
7577 }
7578
7579 symp = symtable2;
7580 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7581 ssym < ssymend; ssym++, symp++)
7582 {
7583 symp->u.ssym = ssym;
7584 symp->name = bfd_elf_string_from_elf_section (bfd2,
7585 hdr2->sh_link,
7586 ssym->st_name);
7587 }
7588
7589 /* Sort symbol by name. */
7590 qsort (symtable1, count1, sizeof (struct elf_symbol),
7591 elf_sym_name_compare);
7592 qsort (symtable2, count1, sizeof (struct elf_symbol),
7593 elf_sym_name_compare);
7594
7595 for (i = 0; i < count1; i++)
7596 /* Two symbols must have the same binding, type and name. */
7597 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7598 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7599 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7600 goto done;
7601
7602 result = TRUE;
7603 goto done;
7604 }
7605
7606 symtable1 = (struct elf_symbol *)
7607 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7608 symtable2 = (struct elf_symbol *)
7609 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7610 if (symtable1 == NULL || symtable2 == NULL)
7611 goto done;
7612
7613 /* Count definitions in the section. */
7614 count1 = 0;
7615 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7616 if (isym->st_shndx == shndx1)
7617 symtable1[count1++].u.isym = isym;
7618
7619 count2 = 0;
7620 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7621 if (isym->st_shndx == shndx2)
7622 symtable2[count2++].u.isym = isym;
7623
7624 if (count1 == 0 || count2 == 0 || count1 != count2)
7625 goto done;
7626
7627 for (i = 0; i < count1; i++)
7628 symtable1[i].name
7629 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7630 symtable1[i].u.isym->st_name);
7631
7632 for (i = 0; i < count2; i++)
7633 symtable2[i].name
7634 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7635 symtable2[i].u.isym->st_name);
7636
7637 /* Sort symbol by name. */
7638 qsort (symtable1, count1, sizeof (struct elf_symbol),
7639 elf_sym_name_compare);
7640 qsort (symtable2, count1, sizeof (struct elf_symbol),
7641 elf_sym_name_compare);
7642
7643 for (i = 0; i < count1; i++)
7644 /* Two symbols must have the same binding, type and name. */
7645 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7646 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7647 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7648 goto done;
7649
7650 result = TRUE;
7651
7652 done:
7653 if (symtable1)
7654 free (symtable1);
7655 if (symtable2)
7656 free (symtable2);
7657 if (isymbuf1)
7658 free (isymbuf1);
7659 if (isymbuf2)
7660 free (isymbuf2);
7661
7662 return result;
7663 }
7664
7665 /* Return TRUE if 2 section types are compatible. */
7666
7667 bfd_boolean
7668 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7669 bfd *bbfd, const asection *bsec)
7670 {
7671 if (asec == NULL
7672 || bsec == NULL
7673 || abfd->xvec->flavour != bfd_target_elf_flavour
7674 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7675 return TRUE;
7676
7677 return elf_section_type (asec) == elf_section_type (bsec);
7678 }
7679 \f
7680 /* Final phase of ELF linker. */
7681
7682 /* A structure we use to avoid passing large numbers of arguments. */
7683
7684 struct elf_final_link_info
7685 {
7686 /* General link information. */
7687 struct bfd_link_info *info;
7688 /* Output BFD. */
7689 bfd *output_bfd;
7690 /* Symbol string table. */
7691 struct elf_strtab_hash *symstrtab;
7692 /* .hash section. */
7693 asection *hash_sec;
7694 /* symbol version section (.gnu.version). */
7695 asection *symver_sec;
7696 /* Buffer large enough to hold contents of any section. */
7697 bfd_byte *contents;
7698 /* Buffer large enough to hold external relocs of any section. */
7699 void *external_relocs;
7700 /* Buffer large enough to hold internal relocs of any section. */
7701 Elf_Internal_Rela *internal_relocs;
7702 /* Buffer large enough to hold external local symbols of any input
7703 BFD. */
7704 bfd_byte *external_syms;
7705 /* And a buffer for symbol section indices. */
7706 Elf_External_Sym_Shndx *locsym_shndx;
7707 /* Buffer large enough to hold internal local symbols of any input
7708 BFD. */
7709 Elf_Internal_Sym *internal_syms;
7710 /* Array large enough to hold a symbol index for each local symbol
7711 of any input BFD. */
7712 long *indices;
7713 /* Array large enough to hold a section pointer for each local
7714 symbol of any input BFD. */
7715 asection **sections;
7716 /* Buffer for SHT_SYMTAB_SHNDX section. */
7717 Elf_External_Sym_Shndx *symshndxbuf;
7718 /* Number of STT_FILE syms seen. */
7719 size_t filesym_count;
7720 };
7721
7722 /* This struct is used to pass information to elf_link_output_extsym. */
7723
7724 struct elf_outext_info
7725 {
7726 bfd_boolean failed;
7727 bfd_boolean localsyms;
7728 bfd_boolean file_sym_done;
7729 struct elf_final_link_info *flinfo;
7730 };
7731
7732
7733 /* Support for evaluating a complex relocation.
7734
7735 Complex relocations are generalized, self-describing relocations. The
7736 implementation of them consists of two parts: complex symbols, and the
7737 relocations themselves.
7738
7739 The relocations are use a reserved elf-wide relocation type code (R_RELC
7740 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7741 information (start bit, end bit, word width, etc) into the addend. This
7742 information is extracted from CGEN-generated operand tables within gas.
7743
7744 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7745 internal) representing prefix-notation expressions, including but not
7746 limited to those sorts of expressions normally encoded as addends in the
7747 addend field. The symbol mangling format is:
7748
7749 <node> := <literal>
7750 | <unary-operator> ':' <node>
7751 | <binary-operator> ':' <node> ':' <node>
7752 ;
7753
7754 <literal> := 's' <digits=N> ':' <N character symbol name>
7755 | 'S' <digits=N> ':' <N character section name>
7756 | '#' <hexdigits>
7757 ;
7758
7759 <binary-operator> := as in C
7760 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7761
7762 static void
7763 set_symbol_value (bfd *bfd_with_globals,
7764 Elf_Internal_Sym *isymbuf,
7765 size_t locsymcount,
7766 size_t symidx,
7767 bfd_vma val)
7768 {
7769 struct elf_link_hash_entry **sym_hashes;
7770 struct elf_link_hash_entry *h;
7771 size_t extsymoff = locsymcount;
7772
7773 if (symidx < locsymcount)
7774 {
7775 Elf_Internal_Sym *sym;
7776
7777 sym = isymbuf + symidx;
7778 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7779 {
7780 /* It is a local symbol: move it to the
7781 "absolute" section and give it a value. */
7782 sym->st_shndx = SHN_ABS;
7783 sym->st_value = val;
7784 return;
7785 }
7786 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7787 extsymoff = 0;
7788 }
7789
7790 /* It is a global symbol: set its link type
7791 to "defined" and give it a value. */
7792
7793 sym_hashes = elf_sym_hashes (bfd_with_globals);
7794 h = sym_hashes [symidx - extsymoff];
7795 while (h->root.type == bfd_link_hash_indirect
7796 || h->root.type == bfd_link_hash_warning)
7797 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7798 h->root.type = bfd_link_hash_defined;
7799 h->root.u.def.value = val;
7800 h->root.u.def.section = bfd_abs_section_ptr;
7801 }
7802
7803 static bfd_boolean
7804 resolve_symbol (const char *name,
7805 bfd *input_bfd,
7806 struct elf_final_link_info *flinfo,
7807 bfd_vma *result,
7808 Elf_Internal_Sym *isymbuf,
7809 size_t locsymcount)
7810 {
7811 Elf_Internal_Sym *sym;
7812 struct bfd_link_hash_entry *global_entry;
7813 const char *candidate = NULL;
7814 Elf_Internal_Shdr *symtab_hdr;
7815 size_t i;
7816
7817 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7818
7819 for (i = 0; i < locsymcount; ++ i)
7820 {
7821 sym = isymbuf + i;
7822
7823 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7824 continue;
7825
7826 candidate = bfd_elf_string_from_elf_section (input_bfd,
7827 symtab_hdr->sh_link,
7828 sym->st_name);
7829 #ifdef DEBUG
7830 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7831 name, candidate, (unsigned long) sym->st_value);
7832 #endif
7833 if (candidate && strcmp (candidate, name) == 0)
7834 {
7835 asection *sec = flinfo->sections [i];
7836
7837 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7838 *result += sec->output_offset + sec->output_section->vma;
7839 #ifdef DEBUG
7840 printf ("Found symbol with value %8.8lx\n",
7841 (unsigned long) *result);
7842 #endif
7843 return TRUE;
7844 }
7845 }
7846
7847 /* Hmm, haven't found it yet. perhaps it is a global. */
7848 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
7849 FALSE, FALSE, TRUE);
7850 if (!global_entry)
7851 return FALSE;
7852
7853 if (global_entry->type == bfd_link_hash_defined
7854 || global_entry->type == bfd_link_hash_defweak)
7855 {
7856 *result = (global_entry->u.def.value
7857 + global_entry->u.def.section->output_section->vma
7858 + global_entry->u.def.section->output_offset);
7859 #ifdef DEBUG
7860 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7861 global_entry->root.string, (unsigned long) *result);
7862 #endif
7863 return TRUE;
7864 }
7865
7866 return FALSE;
7867 }
7868
7869 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
7870 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
7871 names like "foo.end" which is the end address of section "foo". */
7872
7873 static bfd_boolean
7874 resolve_section (const char *name,
7875 asection *sections,
7876 bfd_vma *result,
7877 bfd * abfd)
7878 {
7879 asection *curr;
7880 unsigned int len;
7881
7882 for (curr = sections; curr; curr = curr->next)
7883 if (strcmp (curr->name, name) == 0)
7884 {
7885 *result = curr->vma;
7886 return TRUE;
7887 }
7888
7889 /* Hmm. still haven't found it. try pseudo-section names. */
7890 /* FIXME: This could be coded more efficiently... */
7891 for (curr = sections; curr; curr = curr->next)
7892 {
7893 len = strlen (curr->name);
7894 if (len > strlen (name))
7895 continue;
7896
7897 if (strncmp (curr->name, name, len) == 0)
7898 {
7899 if (strncmp (".end", name + len, 4) == 0)
7900 {
7901 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
7902 return TRUE;
7903 }
7904
7905 /* Insert more pseudo-section names here, if you like. */
7906 }
7907 }
7908
7909 return FALSE;
7910 }
7911
7912 static void
7913 undefined_reference (const char *reftype, const char *name)
7914 {
7915 /* xgettext:c-format */
7916 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7917 reftype, name);
7918 }
7919
7920 static bfd_boolean
7921 eval_symbol (bfd_vma *result,
7922 const char **symp,
7923 bfd *input_bfd,
7924 struct elf_final_link_info *flinfo,
7925 bfd_vma dot,
7926 Elf_Internal_Sym *isymbuf,
7927 size_t locsymcount,
7928 int signed_p)
7929 {
7930 size_t len;
7931 size_t symlen;
7932 bfd_vma a;
7933 bfd_vma b;
7934 char symbuf[4096];
7935 const char *sym = *symp;
7936 const char *symend;
7937 bfd_boolean symbol_is_section = FALSE;
7938
7939 len = strlen (sym);
7940 symend = sym + len;
7941
7942 if (len < 1 || len > sizeof (symbuf))
7943 {
7944 bfd_set_error (bfd_error_invalid_operation);
7945 return FALSE;
7946 }
7947
7948 switch (* sym)
7949 {
7950 case '.':
7951 *result = dot;
7952 *symp = sym + 1;
7953 return TRUE;
7954
7955 case '#':
7956 ++sym;
7957 *result = strtoul (sym, (char **) symp, 16);
7958 return TRUE;
7959
7960 case 'S':
7961 symbol_is_section = TRUE;
7962 /* Fall through. */
7963 case 's':
7964 ++sym;
7965 symlen = strtol (sym, (char **) symp, 10);
7966 sym = *symp + 1; /* Skip the trailing ':'. */
7967
7968 if (symend < sym || symlen + 1 > sizeof (symbuf))
7969 {
7970 bfd_set_error (bfd_error_invalid_operation);
7971 return FALSE;
7972 }
7973
7974 memcpy (symbuf, sym, symlen);
7975 symbuf[symlen] = '\0';
7976 *symp = sym + symlen;
7977
7978 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7979 the symbol as a section, or vice-versa. so we're pretty liberal in our
7980 interpretation here; section means "try section first", not "must be a
7981 section", and likewise with symbol. */
7982
7983 if (symbol_is_section)
7984 {
7985 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
7986 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
7987 isymbuf, locsymcount))
7988 {
7989 undefined_reference ("section", symbuf);
7990 return FALSE;
7991 }
7992 }
7993 else
7994 {
7995 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
7996 isymbuf, locsymcount)
7997 && !resolve_section (symbuf, flinfo->output_bfd->sections,
7998 result, input_bfd))
7999 {
8000 undefined_reference ("symbol", symbuf);
8001 return FALSE;
8002 }
8003 }
8004
8005 return TRUE;
8006
8007 /* All that remains are operators. */
8008
8009 #define UNARY_OP(op) \
8010 if (strncmp (sym, #op, strlen (#op)) == 0) \
8011 { \
8012 sym += strlen (#op); \
8013 if (*sym == ':') \
8014 ++sym; \
8015 *symp = sym; \
8016 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8017 isymbuf, locsymcount, signed_p)) \
8018 return FALSE; \
8019 if (signed_p) \
8020 *result = op ((bfd_signed_vma) a); \
8021 else \
8022 *result = op a; \
8023 return TRUE; \
8024 }
8025
8026 #define BINARY_OP(op) \
8027 if (strncmp (sym, #op, strlen (#op)) == 0) \
8028 { \
8029 sym += strlen (#op); \
8030 if (*sym == ':') \
8031 ++sym; \
8032 *symp = sym; \
8033 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8034 isymbuf, locsymcount, signed_p)) \
8035 return FALSE; \
8036 ++*symp; \
8037 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8038 isymbuf, locsymcount, signed_p)) \
8039 return FALSE; \
8040 if (signed_p) \
8041 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8042 else \
8043 *result = a op b; \
8044 return TRUE; \
8045 }
8046
8047 default:
8048 UNARY_OP (0-);
8049 BINARY_OP (<<);
8050 BINARY_OP (>>);
8051 BINARY_OP (==);
8052 BINARY_OP (!=);
8053 BINARY_OP (<=);
8054 BINARY_OP (>=);
8055 BINARY_OP (&&);
8056 BINARY_OP (||);
8057 UNARY_OP (~);
8058 UNARY_OP (!);
8059 BINARY_OP (*);
8060 BINARY_OP (/);
8061 BINARY_OP (%);
8062 BINARY_OP (^);
8063 BINARY_OP (|);
8064 BINARY_OP (&);
8065 BINARY_OP (+);
8066 BINARY_OP (-);
8067 BINARY_OP (<);
8068 BINARY_OP (>);
8069 #undef UNARY_OP
8070 #undef BINARY_OP
8071 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8072 bfd_set_error (bfd_error_invalid_operation);
8073 return FALSE;
8074 }
8075 }
8076
8077 static void
8078 put_value (bfd_vma size,
8079 unsigned long chunksz,
8080 bfd *input_bfd,
8081 bfd_vma x,
8082 bfd_byte *location)
8083 {
8084 location += (size - chunksz);
8085
8086 for (; size; size -= chunksz, location -= chunksz)
8087 {
8088 switch (chunksz)
8089 {
8090 case 1:
8091 bfd_put_8 (input_bfd, x, location);
8092 x >>= 8;
8093 break;
8094 case 2:
8095 bfd_put_16 (input_bfd, x, location);
8096 x >>= 16;
8097 break;
8098 case 4:
8099 bfd_put_32 (input_bfd, x, location);
8100 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8101 x >>= 16;
8102 x >>= 16;
8103 break;
8104 #ifdef BFD64
8105 case 8:
8106 bfd_put_64 (input_bfd, x, location);
8107 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8108 x >>= 32;
8109 x >>= 32;
8110 break;
8111 #endif
8112 default:
8113 abort ();
8114 break;
8115 }
8116 }
8117 }
8118
8119 static bfd_vma
8120 get_value (bfd_vma size,
8121 unsigned long chunksz,
8122 bfd *input_bfd,
8123 bfd_byte *location)
8124 {
8125 int shift;
8126 bfd_vma x = 0;
8127
8128 /* Sanity checks. */
8129 BFD_ASSERT (chunksz <= sizeof (x)
8130 && size >= chunksz
8131 && chunksz != 0
8132 && (size % chunksz) == 0
8133 && input_bfd != NULL
8134 && location != NULL);
8135
8136 if (chunksz == sizeof (x))
8137 {
8138 BFD_ASSERT (size == chunksz);
8139
8140 /* Make sure that we do not perform an undefined shift operation.
8141 We know that size == chunksz so there will only be one iteration
8142 of the loop below. */
8143 shift = 0;
8144 }
8145 else
8146 shift = 8 * chunksz;
8147
8148 for (; size; size -= chunksz, location += chunksz)
8149 {
8150 switch (chunksz)
8151 {
8152 case 1:
8153 x = (x << shift) | bfd_get_8 (input_bfd, location);
8154 break;
8155 case 2:
8156 x = (x << shift) | bfd_get_16 (input_bfd, location);
8157 break;
8158 case 4:
8159 x = (x << shift) | bfd_get_32 (input_bfd, location);
8160 break;
8161 #ifdef BFD64
8162 case 8:
8163 x = (x << shift) | bfd_get_64 (input_bfd, location);
8164 break;
8165 #endif
8166 default:
8167 abort ();
8168 }
8169 }
8170 return x;
8171 }
8172
8173 static void
8174 decode_complex_addend (unsigned long *start, /* in bits */
8175 unsigned long *oplen, /* in bits */
8176 unsigned long *len, /* in bits */
8177 unsigned long *wordsz, /* in bytes */
8178 unsigned long *chunksz, /* in bytes */
8179 unsigned long *lsb0_p,
8180 unsigned long *signed_p,
8181 unsigned long *trunc_p,
8182 unsigned long encoded)
8183 {
8184 * start = encoded & 0x3F;
8185 * len = (encoded >> 6) & 0x3F;
8186 * oplen = (encoded >> 12) & 0x3F;
8187 * wordsz = (encoded >> 18) & 0xF;
8188 * chunksz = (encoded >> 22) & 0xF;
8189 * lsb0_p = (encoded >> 27) & 1;
8190 * signed_p = (encoded >> 28) & 1;
8191 * trunc_p = (encoded >> 29) & 1;
8192 }
8193
8194 bfd_reloc_status_type
8195 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8196 asection *input_section ATTRIBUTE_UNUSED,
8197 bfd_byte *contents,
8198 Elf_Internal_Rela *rel,
8199 bfd_vma relocation)
8200 {
8201 bfd_vma shift, x, mask;
8202 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8203 bfd_reloc_status_type r;
8204
8205 /* Perform this reloc, since it is complex.
8206 (this is not to say that it necessarily refers to a complex
8207 symbol; merely that it is a self-describing CGEN based reloc.
8208 i.e. the addend has the complete reloc information (bit start, end,
8209 word size, etc) encoded within it.). */
8210
8211 decode_complex_addend (&start, &oplen, &len, &wordsz,
8212 &chunksz, &lsb0_p, &signed_p,
8213 &trunc_p, rel->r_addend);
8214
8215 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8216
8217 if (lsb0_p)
8218 shift = (start + 1) - len;
8219 else
8220 shift = (8 * wordsz) - (start + len);
8221
8222 x = get_value (wordsz, chunksz, input_bfd,
8223 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8224
8225 #ifdef DEBUG
8226 printf ("Doing complex reloc: "
8227 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8228 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8229 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8230 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8231 oplen, (unsigned long) x, (unsigned long) mask,
8232 (unsigned long) relocation);
8233 #endif
8234
8235 r = bfd_reloc_ok;
8236 if (! trunc_p)
8237 /* Now do an overflow check. */
8238 r = bfd_check_overflow ((signed_p
8239 ? complain_overflow_signed
8240 : complain_overflow_unsigned),
8241 len, 0, (8 * wordsz),
8242 relocation);
8243
8244 /* Do the deed. */
8245 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8246
8247 #ifdef DEBUG
8248 printf (" relocation: %8.8lx\n"
8249 " shifted mask: %8.8lx\n"
8250 " shifted/masked reloc: %8.8lx\n"
8251 " result: %8.8lx\n",
8252 (unsigned long) relocation, (unsigned long) (mask << shift),
8253 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8254 #endif
8255 put_value (wordsz, chunksz, input_bfd, x,
8256 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8257 return r;
8258 }
8259
8260 /* Functions to read r_offset from external (target order) reloc
8261 entry. Faster than bfd_getl32 et al, because we let the compiler
8262 know the value is aligned. */
8263
8264 static bfd_vma
8265 ext32l_r_offset (const void *p)
8266 {
8267 union aligned32
8268 {
8269 uint32_t v;
8270 unsigned char c[4];
8271 };
8272 const union aligned32 *a
8273 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8274
8275 uint32_t aval = ( (uint32_t) a->c[0]
8276 | (uint32_t) a->c[1] << 8
8277 | (uint32_t) a->c[2] << 16
8278 | (uint32_t) a->c[3] << 24);
8279 return aval;
8280 }
8281
8282 static bfd_vma
8283 ext32b_r_offset (const void *p)
8284 {
8285 union aligned32
8286 {
8287 uint32_t v;
8288 unsigned char c[4];
8289 };
8290 const union aligned32 *a
8291 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8292
8293 uint32_t aval = ( (uint32_t) a->c[0] << 24
8294 | (uint32_t) a->c[1] << 16
8295 | (uint32_t) a->c[2] << 8
8296 | (uint32_t) a->c[3]);
8297 return aval;
8298 }
8299
8300 #ifdef BFD_HOST_64_BIT
8301 static bfd_vma
8302 ext64l_r_offset (const void *p)
8303 {
8304 union aligned64
8305 {
8306 uint64_t v;
8307 unsigned char c[8];
8308 };
8309 const union aligned64 *a
8310 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8311
8312 uint64_t aval = ( (uint64_t) a->c[0]
8313 | (uint64_t) a->c[1] << 8
8314 | (uint64_t) a->c[2] << 16
8315 | (uint64_t) a->c[3] << 24
8316 | (uint64_t) a->c[4] << 32
8317 | (uint64_t) a->c[5] << 40
8318 | (uint64_t) a->c[6] << 48
8319 | (uint64_t) a->c[7] << 56);
8320 return aval;
8321 }
8322
8323 static bfd_vma
8324 ext64b_r_offset (const void *p)
8325 {
8326 union aligned64
8327 {
8328 uint64_t v;
8329 unsigned char c[8];
8330 };
8331 const union aligned64 *a
8332 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8333
8334 uint64_t aval = ( (uint64_t) a->c[0] << 56
8335 | (uint64_t) a->c[1] << 48
8336 | (uint64_t) a->c[2] << 40
8337 | (uint64_t) a->c[3] << 32
8338 | (uint64_t) a->c[4] << 24
8339 | (uint64_t) a->c[5] << 16
8340 | (uint64_t) a->c[6] << 8
8341 | (uint64_t) a->c[7]);
8342 return aval;
8343 }
8344 #endif
8345
8346 /* When performing a relocatable link, the input relocations are
8347 preserved. But, if they reference global symbols, the indices
8348 referenced must be updated. Update all the relocations found in
8349 RELDATA. */
8350
8351 static bfd_boolean
8352 elf_link_adjust_relocs (bfd *abfd,
8353 asection *sec,
8354 struct bfd_elf_section_reloc_data *reldata,
8355 bfd_boolean sort)
8356 {
8357 unsigned int i;
8358 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8359 bfd_byte *erela;
8360 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8361 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8362 bfd_vma r_type_mask;
8363 int r_sym_shift;
8364 unsigned int count = reldata->count;
8365 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8366
8367 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8368 {
8369 swap_in = bed->s->swap_reloc_in;
8370 swap_out = bed->s->swap_reloc_out;
8371 }
8372 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8373 {
8374 swap_in = bed->s->swap_reloca_in;
8375 swap_out = bed->s->swap_reloca_out;
8376 }
8377 else
8378 abort ();
8379
8380 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8381 abort ();
8382
8383 if (bed->s->arch_size == 32)
8384 {
8385 r_type_mask = 0xff;
8386 r_sym_shift = 8;
8387 }
8388 else
8389 {
8390 r_type_mask = 0xffffffff;
8391 r_sym_shift = 32;
8392 }
8393
8394 erela = reldata->hdr->contents;
8395 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8396 {
8397 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8398 unsigned int j;
8399
8400 if (*rel_hash == NULL)
8401 continue;
8402
8403 BFD_ASSERT ((*rel_hash)->indx >= 0);
8404
8405 (*swap_in) (abfd, erela, irela);
8406 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8407 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8408 | (irela[j].r_info & r_type_mask));
8409 (*swap_out) (abfd, irela, erela);
8410 }
8411
8412 if (bed->elf_backend_update_relocs)
8413 (*bed->elf_backend_update_relocs) (sec, reldata);
8414
8415 if (sort && count != 0)
8416 {
8417 bfd_vma (*ext_r_off) (const void *);
8418 bfd_vma r_off;
8419 size_t elt_size;
8420 bfd_byte *base, *end, *p, *loc;
8421 bfd_byte *buf = NULL;
8422
8423 if (bed->s->arch_size == 32)
8424 {
8425 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8426 ext_r_off = ext32l_r_offset;
8427 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8428 ext_r_off = ext32b_r_offset;
8429 else
8430 abort ();
8431 }
8432 else
8433 {
8434 #ifdef BFD_HOST_64_BIT
8435 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8436 ext_r_off = ext64l_r_offset;
8437 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8438 ext_r_off = ext64b_r_offset;
8439 else
8440 #endif
8441 abort ();
8442 }
8443
8444 /* Must use a stable sort here. A modified insertion sort,
8445 since the relocs are mostly sorted already. */
8446 elt_size = reldata->hdr->sh_entsize;
8447 base = reldata->hdr->contents;
8448 end = base + count * elt_size;
8449 if (elt_size > sizeof (Elf64_External_Rela))
8450 abort ();
8451
8452 /* Ensure the first element is lowest. This acts as a sentinel,
8453 speeding the main loop below. */
8454 r_off = (*ext_r_off) (base);
8455 for (p = loc = base; (p += elt_size) < end; )
8456 {
8457 bfd_vma r_off2 = (*ext_r_off) (p);
8458 if (r_off > r_off2)
8459 {
8460 r_off = r_off2;
8461 loc = p;
8462 }
8463 }
8464 if (loc != base)
8465 {
8466 /* Don't just swap *base and *loc as that changes the order
8467 of the original base[0] and base[1] if they happen to
8468 have the same r_offset. */
8469 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8470 memcpy (onebuf, loc, elt_size);
8471 memmove (base + elt_size, base, loc - base);
8472 memcpy (base, onebuf, elt_size);
8473 }
8474
8475 for (p = base + elt_size; (p += elt_size) < end; )
8476 {
8477 /* base to p is sorted, *p is next to insert. */
8478 r_off = (*ext_r_off) (p);
8479 /* Search the sorted region for location to insert. */
8480 loc = p - elt_size;
8481 while (r_off < (*ext_r_off) (loc))
8482 loc -= elt_size;
8483 loc += elt_size;
8484 if (loc != p)
8485 {
8486 /* Chances are there is a run of relocs to insert here,
8487 from one of more input files. Files are not always
8488 linked in order due to the way elf_link_input_bfd is
8489 called. See pr17666. */
8490 size_t sortlen = p - loc;
8491 bfd_vma r_off2 = (*ext_r_off) (loc);
8492 size_t runlen = elt_size;
8493 size_t buf_size = 96 * 1024;
8494 while (p + runlen < end
8495 && (sortlen <= buf_size
8496 || runlen + elt_size <= buf_size)
8497 && r_off2 > (*ext_r_off) (p + runlen))
8498 runlen += elt_size;
8499 if (buf == NULL)
8500 {
8501 buf = bfd_malloc (buf_size);
8502 if (buf == NULL)
8503 return FALSE;
8504 }
8505 if (runlen < sortlen)
8506 {
8507 memcpy (buf, p, runlen);
8508 memmove (loc + runlen, loc, sortlen);
8509 memcpy (loc, buf, runlen);
8510 }
8511 else
8512 {
8513 memcpy (buf, loc, sortlen);
8514 memmove (loc, p, runlen);
8515 memcpy (loc + runlen, buf, sortlen);
8516 }
8517 p += runlen - elt_size;
8518 }
8519 }
8520 /* Hashes are no longer valid. */
8521 free (reldata->hashes);
8522 reldata->hashes = NULL;
8523 free (buf);
8524 }
8525 return TRUE;
8526 }
8527
8528 struct elf_link_sort_rela
8529 {
8530 union {
8531 bfd_vma offset;
8532 bfd_vma sym_mask;
8533 } u;
8534 enum elf_reloc_type_class type;
8535 /* We use this as an array of size int_rels_per_ext_rel. */
8536 Elf_Internal_Rela rela[1];
8537 };
8538
8539 static int
8540 elf_link_sort_cmp1 (const void *A, const void *B)
8541 {
8542 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8543 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8544 int relativea, relativeb;
8545
8546 relativea = a->type == reloc_class_relative;
8547 relativeb = b->type == reloc_class_relative;
8548
8549 if (relativea < relativeb)
8550 return 1;
8551 if (relativea > relativeb)
8552 return -1;
8553 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8554 return -1;
8555 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8556 return 1;
8557 if (a->rela->r_offset < b->rela->r_offset)
8558 return -1;
8559 if (a->rela->r_offset > b->rela->r_offset)
8560 return 1;
8561 return 0;
8562 }
8563
8564 static int
8565 elf_link_sort_cmp2 (const void *A, const void *B)
8566 {
8567 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8568 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8569
8570 if (a->type < b->type)
8571 return -1;
8572 if (a->type > b->type)
8573 return 1;
8574 if (a->u.offset < b->u.offset)
8575 return -1;
8576 if (a->u.offset > b->u.offset)
8577 return 1;
8578 if (a->rela->r_offset < b->rela->r_offset)
8579 return -1;
8580 if (a->rela->r_offset > b->rela->r_offset)
8581 return 1;
8582 return 0;
8583 }
8584
8585 static size_t
8586 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8587 {
8588 asection *dynamic_relocs;
8589 asection *rela_dyn;
8590 asection *rel_dyn;
8591 bfd_size_type count, size;
8592 size_t i, ret, sort_elt, ext_size;
8593 bfd_byte *sort, *s_non_relative, *p;
8594 struct elf_link_sort_rela *sq;
8595 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8596 int i2e = bed->s->int_rels_per_ext_rel;
8597 unsigned int opb = bfd_octets_per_byte (abfd);
8598 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8599 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8600 struct bfd_link_order *lo;
8601 bfd_vma r_sym_mask;
8602 bfd_boolean use_rela;
8603
8604 /* Find a dynamic reloc section. */
8605 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8606 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8607 if (rela_dyn != NULL && rela_dyn->size > 0
8608 && rel_dyn != NULL && rel_dyn->size > 0)
8609 {
8610 bfd_boolean use_rela_initialised = FALSE;
8611
8612 /* This is just here to stop gcc from complaining.
8613 Its initialization checking code is not perfect. */
8614 use_rela = TRUE;
8615
8616 /* Both sections are present. Examine the sizes
8617 of the indirect sections to help us choose. */
8618 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8619 if (lo->type == bfd_indirect_link_order)
8620 {
8621 asection *o = lo->u.indirect.section;
8622
8623 if ((o->size % bed->s->sizeof_rela) == 0)
8624 {
8625 if ((o->size % bed->s->sizeof_rel) == 0)
8626 /* Section size is divisible by both rel and rela sizes.
8627 It is of no help to us. */
8628 ;
8629 else
8630 {
8631 /* Section size is only divisible by rela. */
8632 if (use_rela_initialised && (use_rela == FALSE))
8633 {
8634 _bfd_error_handler (_("%B: Unable to sort relocs - "
8635 "they are in more than one size"),
8636 abfd);
8637 bfd_set_error (bfd_error_invalid_operation);
8638 return 0;
8639 }
8640 else
8641 {
8642 use_rela = TRUE;
8643 use_rela_initialised = TRUE;
8644 }
8645 }
8646 }
8647 else if ((o->size % bed->s->sizeof_rel) == 0)
8648 {
8649 /* Section size is only divisible by rel. */
8650 if (use_rela_initialised && (use_rela == TRUE))
8651 {
8652 _bfd_error_handler (_("%B: Unable to sort relocs - "
8653 "they are in more than one size"),
8654 abfd);
8655 bfd_set_error (bfd_error_invalid_operation);
8656 return 0;
8657 }
8658 else
8659 {
8660 use_rela = FALSE;
8661 use_rela_initialised = TRUE;
8662 }
8663 }
8664 else
8665 {
8666 /* The section size is not divisible by either -
8667 something is wrong. */
8668 _bfd_error_handler (_("%B: Unable to sort relocs - "
8669 "they are of an unknown size"), abfd);
8670 bfd_set_error (bfd_error_invalid_operation);
8671 return 0;
8672 }
8673 }
8674
8675 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8676 if (lo->type == bfd_indirect_link_order)
8677 {
8678 asection *o = lo->u.indirect.section;
8679
8680 if ((o->size % bed->s->sizeof_rela) == 0)
8681 {
8682 if ((o->size % bed->s->sizeof_rel) == 0)
8683 /* Section size is divisible by both rel and rela sizes.
8684 It is of no help to us. */
8685 ;
8686 else
8687 {
8688 /* Section size is only divisible by rela. */
8689 if (use_rela_initialised && (use_rela == FALSE))
8690 {
8691 _bfd_error_handler (_("%B: Unable to sort relocs - "
8692 "they are in more than one size"),
8693 abfd);
8694 bfd_set_error (bfd_error_invalid_operation);
8695 return 0;
8696 }
8697 else
8698 {
8699 use_rela = TRUE;
8700 use_rela_initialised = TRUE;
8701 }
8702 }
8703 }
8704 else if ((o->size % bed->s->sizeof_rel) == 0)
8705 {
8706 /* Section size is only divisible by rel. */
8707 if (use_rela_initialised && (use_rela == TRUE))
8708 {
8709 _bfd_error_handler (_("%B: Unable to sort relocs - "
8710 "they are in more than one size"),
8711 abfd);
8712 bfd_set_error (bfd_error_invalid_operation);
8713 return 0;
8714 }
8715 else
8716 {
8717 use_rela = FALSE;
8718 use_rela_initialised = TRUE;
8719 }
8720 }
8721 else
8722 {
8723 /* The section size is not divisible by either -
8724 something is wrong. */
8725 _bfd_error_handler (_("%B: Unable to sort relocs - "
8726 "they are of an unknown size"), abfd);
8727 bfd_set_error (bfd_error_invalid_operation);
8728 return 0;
8729 }
8730 }
8731
8732 if (! use_rela_initialised)
8733 /* Make a guess. */
8734 use_rela = TRUE;
8735 }
8736 else if (rela_dyn != NULL && rela_dyn->size > 0)
8737 use_rela = TRUE;
8738 else if (rel_dyn != NULL && rel_dyn->size > 0)
8739 use_rela = FALSE;
8740 else
8741 return 0;
8742
8743 if (use_rela)
8744 {
8745 dynamic_relocs = rela_dyn;
8746 ext_size = bed->s->sizeof_rela;
8747 swap_in = bed->s->swap_reloca_in;
8748 swap_out = bed->s->swap_reloca_out;
8749 }
8750 else
8751 {
8752 dynamic_relocs = rel_dyn;
8753 ext_size = bed->s->sizeof_rel;
8754 swap_in = bed->s->swap_reloc_in;
8755 swap_out = bed->s->swap_reloc_out;
8756 }
8757
8758 size = 0;
8759 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8760 if (lo->type == bfd_indirect_link_order)
8761 size += lo->u.indirect.section->size;
8762
8763 if (size != dynamic_relocs->size)
8764 return 0;
8765
8766 sort_elt = (sizeof (struct elf_link_sort_rela)
8767 + (i2e - 1) * sizeof (Elf_Internal_Rela));
8768
8769 count = dynamic_relocs->size / ext_size;
8770 if (count == 0)
8771 return 0;
8772 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
8773
8774 if (sort == NULL)
8775 {
8776 (*info->callbacks->warning)
8777 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8778 return 0;
8779 }
8780
8781 if (bed->s->arch_size == 32)
8782 r_sym_mask = ~(bfd_vma) 0xff;
8783 else
8784 r_sym_mask = ~(bfd_vma) 0xffffffff;
8785
8786 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8787 if (lo->type == bfd_indirect_link_order)
8788 {
8789 bfd_byte *erel, *erelend;
8790 asection *o = lo->u.indirect.section;
8791
8792 if (o->contents == NULL && o->size != 0)
8793 {
8794 /* This is a reloc section that is being handled as a normal
8795 section. See bfd_section_from_shdr. We can't combine
8796 relocs in this case. */
8797 free (sort);
8798 return 0;
8799 }
8800 erel = o->contents;
8801 erelend = o->contents + o->size;
8802 p = sort + o->output_offset * opb / ext_size * sort_elt;
8803
8804 while (erel < erelend)
8805 {
8806 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8807
8808 (*swap_in) (abfd, erel, s->rela);
8809 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
8810 s->u.sym_mask = r_sym_mask;
8811 p += sort_elt;
8812 erel += ext_size;
8813 }
8814 }
8815
8816 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8817
8818 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8819 {
8820 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8821 if (s->type != reloc_class_relative)
8822 break;
8823 }
8824 ret = i;
8825 s_non_relative = p;
8826
8827 sq = (struct elf_link_sort_rela *) s_non_relative;
8828 for (; i < count; i++, p += sort_elt)
8829 {
8830 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8831 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8832 sq = sp;
8833 sp->u.offset = sq->rela->r_offset;
8834 }
8835
8836 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8837
8838 struct elf_link_hash_table *htab = elf_hash_table (info);
8839 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
8840 {
8841 /* We have plt relocs in .rela.dyn. */
8842 sq = (struct elf_link_sort_rela *) sort;
8843 for (i = 0; i < count; i++)
8844 if (sq[count - i - 1].type != reloc_class_plt)
8845 break;
8846 if (i != 0 && htab->srelplt->size == i * ext_size)
8847 {
8848 struct bfd_link_order **plo;
8849 /* Put srelplt link_order last. This is so the output_offset
8850 set in the next loop is correct for DT_JMPREL. */
8851 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
8852 if ((*plo)->type == bfd_indirect_link_order
8853 && (*plo)->u.indirect.section == htab->srelplt)
8854 {
8855 lo = *plo;
8856 *plo = lo->next;
8857 }
8858 else
8859 plo = &(*plo)->next;
8860 *plo = lo;
8861 lo->next = NULL;
8862 dynamic_relocs->map_tail.link_order = lo;
8863 }
8864 }
8865
8866 p = sort;
8867 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8868 if (lo->type == bfd_indirect_link_order)
8869 {
8870 bfd_byte *erel, *erelend;
8871 asection *o = lo->u.indirect.section;
8872
8873 erel = o->contents;
8874 erelend = o->contents + o->size;
8875 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
8876 while (erel < erelend)
8877 {
8878 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8879 (*swap_out) (abfd, s->rela, erel);
8880 p += sort_elt;
8881 erel += ext_size;
8882 }
8883 }
8884
8885 free (sort);
8886 *psec = dynamic_relocs;
8887 return ret;
8888 }
8889
8890 /* Add a symbol to the output symbol string table. */
8891
8892 static int
8893 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
8894 const char *name,
8895 Elf_Internal_Sym *elfsym,
8896 asection *input_sec,
8897 struct elf_link_hash_entry *h)
8898 {
8899 int (*output_symbol_hook)
8900 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8901 struct elf_link_hash_entry *);
8902 struct elf_link_hash_table *hash_table;
8903 const struct elf_backend_data *bed;
8904 bfd_size_type strtabsize;
8905
8906 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8907
8908 bed = get_elf_backend_data (flinfo->output_bfd);
8909 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8910 if (output_symbol_hook != NULL)
8911 {
8912 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
8913 if (ret != 1)
8914 return ret;
8915 }
8916
8917 if (name == NULL
8918 || *name == '\0'
8919 || (input_sec->flags & SEC_EXCLUDE))
8920 elfsym->st_name = (unsigned long) -1;
8921 else
8922 {
8923 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8924 to get the final offset for st_name. */
8925 elfsym->st_name
8926 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
8927 name, FALSE);
8928 if (elfsym->st_name == (unsigned long) -1)
8929 return 0;
8930 }
8931
8932 hash_table = elf_hash_table (flinfo->info);
8933 strtabsize = hash_table->strtabsize;
8934 if (strtabsize <= hash_table->strtabcount)
8935 {
8936 strtabsize += strtabsize;
8937 hash_table->strtabsize = strtabsize;
8938 strtabsize *= sizeof (*hash_table->strtab);
8939 hash_table->strtab
8940 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
8941 strtabsize);
8942 if (hash_table->strtab == NULL)
8943 return 0;
8944 }
8945 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
8946 hash_table->strtab[hash_table->strtabcount].dest_index
8947 = hash_table->strtabcount;
8948 hash_table->strtab[hash_table->strtabcount].destshndx_index
8949 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
8950
8951 bfd_get_symcount (flinfo->output_bfd) += 1;
8952 hash_table->strtabcount += 1;
8953
8954 return 1;
8955 }
8956
8957 /* Swap symbols out to the symbol table and flush the output symbols to
8958 the file. */
8959
8960 static bfd_boolean
8961 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
8962 {
8963 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
8964 bfd_size_type amt;
8965 size_t i;
8966 const struct elf_backend_data *bed;
8967 bfd_byte *symbuf;
8968 Elf_Internal_Shdr *hdr;
8969 file_ptr pos;
8970 bfd_boolean ret;
8971
8972 if (!hash_table->strtabcount)
8973 return TRUE;
8974
8975 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8976
8977 bed = get_elf_backend_data (flinfo->output_bfd);
8978
8979 amt = bed->s->sizeof_sym * hash_table->strtabcount;
8980 symbuf = (bfd_byte *) bfd_malloc (amt);
8981 if (symbuf == NULL)
8982 return FALSE;
8983
8984 if (flinfo->symshndxbuf)
8985 {
8986 amt = sizeof (Elf_External_Sym_Shndx);
8987 amt *= bfd_get_symcount (flinfo->output_bfd);
8988 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
8989 if (flinfo->symshndxbuf == NULL)
8990 {
8991 free (symbuf);
8992 return FALSE;
8993 }
8994 }
8995
8996 for (i = 0; i < hash_table->strtabcount; i++)
8997 {
8998 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
8999 if (elfsym->sym.st_name == (unsigned long) -1)
9000 elfsym->sym.st_name = 0;
9001 else
9002 elfsym->sym.st_name
9003 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9004 elfsym->sym.st_name);
9005 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9006 ((bfd_byte *) symbuf
9007 + (elfsym->dest_index
9008 * bed->s->sizeof_sym)),
9009 (flinfo->symshndxbuf
9010 + elfsym->destshndx_index));
9011 }
9012
9013 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9014 pos = hdr->sh_offset + hdr->sh_size;
9015 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9016 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9017 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9018 {
9019 hdr->sh_size += amt;
9020 ret = TRUE;
9021 }
9022 else
9023 ret = FALSE;
9024
9025 free (symbuf);
9026
9027 free (hash_table->strtab);
9028 hash_table->strtab = NULL;
9029
9030 return ret;
9031 }
9032
9033 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9034
9035 static bfd_boolean
9036 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9037 {
9038 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9039 && sym->st_shndx < SHN_LORESERVE)
9040 {
9041 /* The gABI doesn't support dynamic symbols in output sections
9042 beyond 64k. */
9043 _bfd_error_handler
9044 /* xgettext:c-format */
9045 (_("%B: Too many sections: %d (>= %d)"),
9046 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9047 bfd_set_error (bfd_error_nonrepresentable_section);
9048 return FALSE;
9049 }
9050 return TRUE;
9051 }
9052
9053 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9054 allowing an unsatisfied unversioned symbol in the DSO to match a
9055 versioned symbol that would normally require an explicit version.
9056 We also handle the case that a DSO references a hidden symbol
9057 which may be satisfied by a versioned symbol in another DSO. */
9058
9059 static bfd_boolean
9060 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9061 const struct elf_backend_data *bed,
9062 struct elf_link_hash_entry *h)
9063 {
9064 bfd *abfd;
9065 struct elf_link_loaded_list *loaded;
9066
9067 if (!is_elf_hash_table (info->hash))
9068 return FALSE;
9069
9070 /* Check indirect symbol. */
9071 while (h->root.type == bfd_link_hash_indirect)
9072 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9073
9074 switch (h->root.type)
9075 {
9076 default:
9077 abfd = NULL;
9078 break;
9079
9080 case bfd_link_hash_undefined:
9081 case bfd_link_hash_undefweak:
9082 abfd = h->root.u.undef.abfd;
9083 if (abfd == NULL
9084 || (abfd->flags & DYNAMIC) == 0
9085 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9086 return FALSE;
9087 break;
9088
9089 case bfd_link_hash_defined:
9090 case bfd_link_hash_defweak:
9091 abfd = h->root.u.def.section->owner;
9092 break;
9093
9094 case bfd_link_hash_common:
9095 abfd = h->root.u.c.p->section->owner;
9096 break;
9097 }
9098 BFD_ASSERT (abfd != NULL);
9099
9100 for (loaded = elf_hash_table (info)->loaded;
9101 loaded != NULL;
9102 loaded = loaded->next)
9103 {
9104 bfd *input;
9105 Elf_Internal_Shdr *hdr;
9106 size_t symcount;
9107 size_t extsymcount;
9108 size_t extsymoff;
9109 Elf_Internal_Shdr *versymhdr;
9110 Elf_Internal_Sym *isym;
9111 Elf_Internal_Sym *isymend;
9112 Elf_Internal_Sym *isymbuf;
9113 Elf_External_Versym *ever;
9114 Elf_External_Versym *extversym;
9115
9116 input = loaded->abfd;
9117
9118 /* We check each DSO for a possible hidden versioned definition. */
9119 if (input == abfd
9120 || (input->flags & DYNAMIC) == 0
9121 || elf_dynversym (input) == 0)
9122 continue;
9123
9124 hdr = &elf_tdata (input)->dynsymtab_hdr;
9125
9126 symcount = hdr->sh_size / bed->s->sizeof_sym;
9127 if (elf_bad_symtab (input))
9128 {
9129 extsymcount = symcount;
9130 extsymoff = 0;
9131 }
9132 else
9133 {
9134 extsymcount = symcount - hdr->sh_info;
9135 extsymoff = hdr->sh_info;
9136 }
9137
9138 if (extsymcount == 0)
9139 continue;
9140
9141 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9142 NULL, NULL, NULL);
9143 if (isymbuf == NULL)
9144 return FALSE;
9145
9146 /* Read in any version definitions. */
9147 versymhdr = &elf_tdata (input)->dynversym_hdr;
9148 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
9149 if (extversym == NULL)
9150 goto error_ret;
9151
9152 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9153 || (bfd_bread (extversym, versymhdr->sh_size, input)
9154 != versymhdr->sh_size))
9155 {
9156 free (extversym);
9157 error_ret:
9158 free (isymbuf);
9159 return FALSE;
9160 }
9161
9162 ever = extversym + extsymoff;
9163 isymend = isymbuf + extsymcount;
9164 for (isym = isymbuf; isym < isymend; isym++, ever++)
9165 {
9166 const char *name;
9167 Elf_Internal_Versym iver;
9168 unsigned short version_index;
9169
9170 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9171 || isym->st_shndx == SHN_UNDEF)
9172 continue;
9173
9174 name = bfd_elf_string_from_elf_section (input,
9175 hdr->sh_link,
9176 isym->st_name);
9177 if (strcmp (name, h->root.root.string) != 0)
9178 continue;
9179
9180 _bfd_elf_swap_versym_in (input, ever, &iver);
9181
9182 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9183 && !(h->def_regular
9184 && h->forced_local))
9185 {
9186 /* If we have a non-hidden versioned sym, then it should
9187 have provided a definition for the undefined sym unless
9188 it is defined in a non-shared object and forced local.
9189 */
9190 abort ();
9191 }
9192
9193 version_index = iver.vs_vers & VERSYM_VERSION;
9194 if (version_index == 1 || version_index == 2)
9195 {
9196 /* This is the base or first version. We can use it. */
9197 free (extversym);
9198 free (isymbuf);
9199 return TRUE;
9200 }
9201 }
9202
9203 free (extversym);
9204 free (isymbuf);
9205 }
9206
9207 return FALSE;
9208 }
9209
9210 /* Convert ELF common symbol TYPE. */
9211
9212 static int
9213 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9214 {
9215 /* Commom symbol can only appear in relocatable link. */
9216 if (!bfd_link_relocatable (info))
9217 abort ();
9218 switch (info->elf_stt_common)
9219 {
9220 case unchanged:
9221 break;
9222 case elf_stt_common:
9223 type = STT_COMMON;
9224 break;
9225 case no_elf_stt_common:
9226 type = STT_OBJECT;
9227 break;
9228 }
9229 return type;
9230 }
9231
9232 /* Add an external symbol to the symbol table. This is called from
9233 the hash table traversal routine. When generating a shared object,
9234 we go through the symbol table twice. The first time we output
9235 anything that might have been forced to local scope in a version
9236 script. The second time we output the symbols that are still
9237 global symbols. */
9238
9239 static bfd_boolean
9240 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9241 {
9242 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9243 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9244 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9245 bfd_boolean strip;
9246 Elf_Internal_Sym sym;
9247 asection *input_sec;
9248 const struct elf_backend_data *bed;
9249 long indx;
9250 int ret;
9251 unsigned int type;
9252 /* A symbol is bound locally if it is forced local or it is locally
9253 defined, hidden versioned, not referenced by shared library and
9254 not exported when linking executable. */
9255 bfd_boolean local_bind = (h->forced_local
9256 || (bfd_link_executable (flinfo->info)
9257 && !flinfo->info->export_dynamic
9258 && !h->dynamic
9259 && !h->ref_dynamic
9260 && h->def_regular
9261 && h->versioned == versioned_hidden));
9262
9263 if (h->root.type == bfd_link_hash_warning)
9264 {
9265 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9266 if (h->root.type == bfd_link_hash_new)
9267 return TRUE;
9268 }
9269
9270 /* Decide whether to output this symbol in this pass. */
9271 if (eoinfo->localsyms)
9272 {
9273 if (!local_bind)
9274 return TRUE;
9275 }
9276 else
9277 {
9278 if (local_bind)
9279 return TRUE;
9280 }
9281
9282 bed = get_elf_backend_data (flinfo->output_bfd);
9283
9284 if (h->root.type == bfd_link_hash_undefined)
9285 {
9286 /* If we have an undefined symbol reference here then it must have
9287 come from a shared library that is being linked in. (Undefined
9288 references in regular files have already been handled unless
9289 they are in unreferenced sections which are removed by garbage
9290 collection). */
9291 bfd_boolean ignore_undef = FALSE;
9292
9293 /* Some symbols may be special in that the fact that they're
9294 undefined can be safely ignored - let backend determine that. */
9295 if (bed->elf_backend_ignore_undef_symbol)
9296 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9297
9298 /* If we are reporting errors for this situation then do so now. */
9299 if (!ignore_undef
9300 && h->ref_dynamic
9301 && (!h->ref_regular || flinfo->info->gc_sections)
9302 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9303 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9304 (*flinfo->info->callbacks->undefined_symbol)
9305 (flinfo->info, h->root.root.string,
9306 h->ref_regular ? NULL : h->root.u.undef.abfd,
9307 NULL, 0,
9308 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
9309
9310 /* Strip a global symbol defined in a discarded section. */
9311 if (h->indx == -3)
9312 return TRUE;
9313 }
9314
9315 /* We should also warn if a forced local symbol is referenced from
9316 shared libraries. */
9317 if (bfd_link_executable (flinfo->info)
9318 && h->forced_local
9319 && h->ref_dynamic
9320 && h->def_regular
9321 && !h->dynamic_def
9322 && h->ref_dynamic_nonweak
9323 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9324 {
9325 bfd *def_bfd;
9326 const char *msg;
9327 struct elf_link_hash_entry *hi = h;
9328
9329 /* Check indirect symbol. */
9330 while (hi->root.type == bfd_link_hash_indirect)
9331 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9332
9333 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9334 /* xgettext:c-format */
9335 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9336 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9337 /* xgettext:c-format */
9338 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9339 else
9340 /* xgettext:c-format */
9341 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
9342 def_bfd = flinfo->output_bfd;
9343 if (hi->root.u.def.section != bfd_abs_section_ptr)
9344 def_bfd = hi->root.u.def.section->owner;
9345 _bfd_error_handler (msg, flinfo->output_bfd, def_bfd,
9346 h->root.root.string);
9347 bfd_set_error (bfd_error_bad_value);
9348 eoinfo->failed = TRUE;
9349 return FALSE;
9350 }
9351
9352 /* We don't want to output symbols that have never been mentioned by
9353 a regular file, or that we have been told to strip. However, if
9354 h->indx is set to -2, the symbol is used by a reloc and we must
9355 output it. */
9356 strip = FALSE;
9357 if (h->indx == -2)
9358 ;
9359 else if ((h->def_dynamic
9360 || h->ref_dynamic
9361 || h->root.type == bfd_link_hash_new)
9362 && !h->def_regular
9363 && !h->ref_regular)
9364 strip = TRUE;
9365 else if (flinfo->info->strip == strip_all)
9366 strip = TRUE;
9367 else if (flinfo->info->strip == strip_some
9368 && bfd_hash_lookup (flinfo->info->keep_hash,
9369 h->root.root.string, FALSE, FALSE) == NULL)
9370 strip = TRUE;
9371 else if ((h->root.type == bfd_link_hash_defined
9372 || h->root.type == bfd_link_hash_defweak)
9373 && ((flinfo->info->strip_discarded
9374 && discarded_section (h->root.u.def.section))
9375 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9376 && h->root.u.def.section->owner != NULL
9377 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9378 strip = TRUE;
9379 else if ((h->root.type == bfd_link_hash_undefined
9380 || h->root.type == bfd_link_hash_undefweak)
9381 && h->root.u.undef.abfd != NULL
9382 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9383 strip = TRUE;
9384
9385 type = h->type;
9386
9387 /* If we're stripping it, and it's not a dynamic symbol, there's
9388 nothing else to do. However, if it is a forced local symbol or
9389 an ifunc symbol we need to give the backend finish_dynamic_symbol
9390 function a chance to make it dynamic. */
9391 if (strip
9392 && h->dynindx == -1
9393 && type != STT_GNU_IFUNC
9394 && !h->forced_local)
9395 return TRUE;
9396
9397 sym.st_value = 0;
9398 sym.st_size = h->size;
9399 sym.st_other = h->other;
9400 switch (h->root.type)
9401 {
9402 default:
9403 case bfd_link_hash_new:
9404 case bfd_link_hash_warning:
9405 abort ();
9406 return FALSE;
9407
9408 case bfd_link_hash_undefined:
9409 case bfd_link_hash_undefweak:
9410 input_sec = bfd_und_section_ptr;
9411 sym.st_shndx = SHN_UNDEF;
9412 break;
9413
9414 case bfd_link_hash_defined:
9415 case bfd_link_hash_defweak:
9416 {
9417 input_sec = h->root.u.def.section;
9418 if (input_sec->output_section != NULL)
9419 {
9420 sym.st_shndx =
9421 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9422 input_sec->output_section);
9423 if (sym.st_shndx == SHN_BAD)
9424 {
9425 _bfd_error_handler
9426 /* xgettext:c-format */
9427 (_("%B: could not find output section %A for input section %A"),
9428 flinfo->output_bfd, input_sec->output_section, input_sec);
9429 bfd_set_error (bfd_error_nonrepresentable_section);
9430 eoinfo->failed = TRUE;
9431 return FALSE;
9432 }
9433
9434 /* ELF symbols in relocatable files are section relative,
9435 but in nonrelocatable files they are virtual
9436 addresses. */
9437 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9438 if (!bfd_link_relocatable (flinfo->info))
9439 {
9440 sym.st_value += input_sec->output_section->vma;
9441 if (h->type == STT_TLS)
9442 {
9443 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9444 if (tls_sec != NULL)
9445 sym.st_value -= tls_sec->vma;
9446 }
9447 }
9448 }
9449 else
9450 {
9451 BFD_ASSERT (input_sec->owner == NULL
9452 || (input_sec->owner->flags & DYNAMIC) != 0);
9453 sym.st_shndx = SHN_UNDEF;
9454 input_sec = bfd_und_section_ptr;
9455 }
9456 }
9457 break;
9458
9459 case bfd_link_hash_common:
9460 input_sec = h->root.u.c.p->section;
9461 sym.st_shndx = bed->common_section_index (input_sec);
9462 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9463 break;
9464
9465 case bfd_link_hash_indirect:
9466 /* These symbols are created by symbol versioning. They point
9467 to the decorated version of the name. For example, if the
9468 symbol foo@@GNU_1.2 is the default, which should be used when
9469 foo is used with no version, then we add an indirect symbol
9470 foo which points to foo@@GNU_1.2. We ignore these symbols,
9471 since the indirected symbol is already in the hash table. */
9472 return TRUE;
9473 }
9474
9475 if (type == STT_COMMON || type == STT_OBJECT)
9476 switch (h->root.type)
9477 {
9478 case bfd_link_hash_common:
9479 type = elf_link_convert_common_type (flinfo->info, type);
9480 break;
9481 case bfd_link_hash_defined:
9482 case bfd_link_hash_defweak:
9483 if (bed->common_definition (&sym))
9484 type = elf_link_convert_common_type (flinfo->info, type);
9485 else
9486 type = STT_OBJECT;
9487 break;
9488 case bfd_link_hash_undefined:
9489 case bfd_link_hash_undefweak:
9490 break;
9491 default:
9492 abort ();
9493 }
9494
9495 if (local_bind)
9496 {
9497 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9498 /* Turn off visibility on local symbol. */
9499 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9500 }
9501 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9502 else if (h->unique_global && h->def_regular)
9503 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9504 else if (h->root.type == bfd_link_hash_undefweak
9505 || h->root.type == bfd_link_hash_defweak)
9506 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9507 else
9508 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9509 sym.st_target_internal = h->target_internal;
9510
9511 /* Give the processor backend a chance to tweak the symbol value,
9512 and also to finish up anything that needs to be done for this
9513 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9514 forced local syms when non-shared is due to a historical quirk.
9515 STT_GNU_IFUNC symbol must go through PLT. */
9516 if ((h->type == STT_GNU_IFUNC
9517 && h->def_regular
9518 && !bfd_link_relocatable (flinfo->info))
9519 || ((h->dynindx != -1
9520 || h->forced_local)
9521 && ((bfd_link_pic (flinfo->info)
9522 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9523 || h->root.type != bfd_link_hash_undefweak))
9524 || !h->forced_local)
9525 && elf_hash_table (flinfo->info)->dynamic_sections_created))
9526 {
9527 if (! ((*bed->elf_backend_finish_dynamic_symbol)
9528 (flinfo->output_bfd, flinfo->info, h, &sym)))
9529 {
9530 eoinfo->failed = TRUE;
9531 return FALSE;
9532 }
9533 }
9534
9535 /* If we are marking the symbol as undefined, and there are no
9536 non-weak references to this symbol from a regular object, then
9537 mark the symbol as weak undefined; if there are non-weak
9538 references, mark the symbol as strong. We can't do this earlier,
9539 because it might not be marked as undefined until the
9540 finish_dynamic_symbol routine gets through with it. */
9541 if (sym.st_shndx == SHN_UNDEF
9542 && h->ref_regular
9543 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9544 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9545 {
9546 int bindtype;
9547 type = ELF_ST_TYPE (sym.st_info);
9548
9549 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9550 if (type == STT_GNU_IFUNC)
9551 type = STT_FUNC;
9552
9553 if (h->ref_regular_nonweak)
9554 bindtype = STB_GLOBAL;
9555 else
9556 bindtype = STB_WEAK;
9557 sym.st_info = ELF_ST_INFO (bindtype, type);
9558 }
9559
9560 /* If this is a symbol defined in a dynamic library, don't use the
9561 symbol size from the dynamic library. Relinking an executable
9562 against a new library may introduce gratuitous changes in the
9563 executable's symbols if we keep the size. */
9564 if (sym.st_shndx == SHN_UNDEF
9565 && !h->def_regular
9566 && h->def_dynamic)
9567 sym.st_size = 0;
9568
9569 /* If a non-weak symbol with non-default visibility is not defined
9570 locally, it is a fatal error. */
9571 if (!bfd_link_relocatable (flinfo->info)
9572 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9573 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9574 && h->root.type == bfd_link_hash_undefined
9575 && !h->def_regular)
9576 {
9577 const char *msg;
9578
9579 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9580 /* xgettext:c-format */
9581 msg = _("%B: protected symbol `%s' isn't defined");
9582 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9583 /* xgettext:c-format */
9584 msg = _("%B: internal symbol `%s' isn't defined");
9585 else
9586 /* xgettext:c-format */
9587 msg = _("%B: hidden symbol `%s' isn't defined");
9588 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
9589 bfd_set_error (bfd_error_bad_value);
9590 eoinfo->failed = TRUE;
9591 return FALSE;
9592 }
9593
9594 /* If this symbol should be put in the .dynsym section, then put it
9595 there now. We already know the symbol index. We also fill in
9596 the entry in the .hash section. */
9597 if (elf_hash_table (flinfo->info)->dynsym != NULL
9598 && h->dynindx != -1
9599 && elf_hash_table (flinfo->info)->dynamic_sections_created)
9600 {
9601 bfd_byte *esym;
9602
9603 /* Since there is no version information in the dynamic string,
9604 if there is no version info in symbol version section, we will
9605 have a run-time problem if not linking executable, referenced
9606 by shared library, not locally defined, or not bound locally.
9607 */
9608 if (h->verinfo.verdef == NULL
9609 && !local_bind
9610 && (!bfd_link_executable (flinfo->info)
9611 || h->ref_dynamic
9612 || !h->def_regular))
9613 {
9614 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9615
9616 if (p && p [1] != '\0')
9617 {
9618 _bfd_error_handler
9619 /* xgettext:c-format */
9620 (_("%B: No symbol version section for versioned symbol `%s'"),
9621 flinfo->output_bfd, h->root.root.string);
9622 eoinfo->failed = TRUE;
9623 return FALSE;
9624 }
9625 }
9626
9627 sym.st_name = h->dynstr_index;
9628 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9629 + h->dynindx * bed->s->sizeof_sym);
9630 if (!check_dynsym (flinfo->output_bfd, &sym))
9631 {
9632 eoinfo->failed = TRUE;
9633 return FALSE;
9634 }
9635 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
9636
9637 if (flinfo->hash_sec != NULL)
9638 {
9639 size_t hash_entry_size;
9640 bfd_byte *bucketpos;
9641 bfd_vma chain;
9642 size_t bucketcount;
9643 size_t bucket;
9644
9645 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
9646 bucket = h->u.elf_hash_value % bucketcount;
9647
9648 hash_entry_size
9649 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9650 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
9651 + (bucket + 2) * hash_entry_size);
9652 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9653 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9654 bucketpos);
9655 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9656 ((bfd_byte *) flinfo->hash_sec->contents
9657 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9658 }
9659
9660 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
9661 {
9662 Elf_Internal_Versym iversym;
9663 Elf_External_Versym *eversym;
9664
9665 if (!h->def_regular)
9666 {
9667 if (h->verinfo.verdef == NULL
9668 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9669 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
9670 iversym.vs_vers = 0;
9671 else
9672 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9673 }
9674 else
9675 {
9676 if (h->verinfo.vertree == NULL)
9677 iversym.vs_vers = 1;
9678 else
9679 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
9680 if (flinfo->info->create_default_symver)
9681 iversym.vs_vers++;
9682 }
9683
9684 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9685 defined locally. */
9686 if (h->versioned == versioned_hidden && h->def_regular)
9687 iversym.vs_vers |= VERSYM_HIDDEN;
9688
9689 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
9690 eversym += h->dynindx;
9691 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
9692 }
9693 }
9694
9695 /* If the symbol is undefined, and we didn't output it to .dynsym,
9696 strip it from .symtab too. Obviously we can't do this for
9697 relocatable output or when needed for --emit-relocs. */
9698 else if (input_sec == bfd_und_section_ptr
9699 && h->indx != -2
9700 && !bfd_link_relocatable (flinfo->info))
9701 return TRUE;
9702 /* Also strip others that we couldn't earlier due to dynamic symbol
9703 processing. */
9704 if (strip)
9705 return TRUE;
9706 if ((input_sec->flags & SEC_EXCLUDE) != 0)
9707 return TRUE;
9708
9709 /* Output a FILE symbol so that following locals are not associated
9710 with the wrong input file. We need one for forced local symbols
9711 if we've seen more than one FILE symbol or when we have exactly
9712 one FILE symbol but global symbols are present in a file other
9713 than the one with the FILE symbol. We also need one if linker
9714 defined symbols are present. In practice these conditions are
9715 always met, so just emit the FILE symbol unconditionally. */
9716 if (eoinfo->localsyms
9717 && !eoinfo->file_sym_done
9718 && eoinfo->flinfo->filesym_count != 0)
9719 {
9720 Elf_Internal_Sym fsym;
9721
9722 memset (&fsym, 0, sizeof (fsym));
9723 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9724 fsym.st_shndx = SHN_ABS;
9725 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9726 bfd_und_section_ptr, NULL))
9727 return FALSE;
9728
9729 eoinfo->file_sym_done = TRUE;
9730 }
9731
9732 indx = bfd_get_symcount (flinfo->output_bfd);
9733 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9734 input_sec, h);
9735 if (ret == 0)
9736 {
9737 eoinfo->failed = TRUE;
9738 return FALSE;
9739 }
9740 else if (ret == 1)
9741 h->indx = indx;
9742 else if (h->indx == -2)
9743 abort();
9744
9745 return TRUE;
9746 }
9747
9748 /* Return TRUE if special handling is done for relocs in SEC against
9749 symbols defined in discarded sections. */
9750
9751 static bfd_boolean
9752 elf_section_ignore_discarded_relocs (asection *sec)
9753 {
9754 const struct elf_backend_data *bed;
9755
9756 switch (sec->sec_info_type)
9757 {
9758 case SEC_INFO_TYPE_STABS:
9759 case SEC_INFO_TYPE_EH_FRAME:
9760 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
9761 return TRUE;
9762 default:
9763 break;
9764 }
9765
9766 bed = get_elf_backend_data (sec->owner);
9767 if (bed->elf_backend_ignore_discarded_relocs != NULL
9768 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9769 return TRUE;
9770
9771 return FALSE;
9772 }
9773
9774 /* Return a mask saying how ld should treat relocations in SEC against
9775 symbols defined in discarded sections. If this function returns
9776 COMPLAIN set, ld will issue a warning message. If this function
9777 returns PRETEND set, and the discarded section was link-once and the
9778 same size as the kept link-once section, ld will pretend that the
9779 symbol was actually defined in the kept section. Otherwise ld will
9780 zero the reloc (at least that is the intent, but some cooperation by
9781 the target dependent code is needed, particularly for REL targets). */
9782
9783 unsigned int
9784 _bfd_elf_default_action_discarded (asection *sec)
9785 {
9786 if (sec->flags & SEC_DEBUGGING)
9787 return PRETEND;
9788
9789 if (strcmp (".eh_frame", sec->name) == 0)
9790 return 0;
9791
9792 if (strcmp (".gcc_except_table", sec->name) == 0)
9793 return 0;
9794
9795 return COMPLAIN | PRETEND;
9796 }
9797
9798 /* Find a match between a section and a member of a section group. */
9799
9800 static asection *
9801 match_group_member (asection *sec, asection *group,
9802 struct bfd_link_info *info)
9803 {
9804 asection *first = elf_next_in_group (group);
9805 asection *s = first;
9806
9807 while (s != NULL)
9808 {
9809 if (bfd_elf_match_symbols_in_sections (s, sec, info))
9810 return s;
9811
9812 s = elf_next_in_group (s);
9813 if (s == first)
9814 break;
9815 }
9816
9817 return NULL;
9818 }
9819
9820 /* Check if the kept section of a discarded section SEC can be used
9821 to replace it. Return the replacement if it is OK. Otherwise return
9822 NULL. */
9823
9824 asection *
9825 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
9826 {
9827 asection *kept;
9828
9829 kept = sec->kept_section;
9830 if (kept != NULL)
9831 {
9832 if ((kept->flags & SEC_GROUP) != 0)
9833 kept = match_group_member (sec, kept, info);
9834 if (kept != NULL
9835 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9836 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
9837 kept = NULL;
9838 sec->kept_section = kept;
9839 }
9840 return kept;
9841 }
9842
9843 /* Link an input file into the linker output file. This function
9844 handles all the sections and relocations of the input file at once.
9845 This is so that we only have to read the local symbols once, and
9846 don't have to keep them in memory. */
9847
9848 static bfd_boolean
9849 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
9850 {
9851 int (*relocate_section)
9852 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9853 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9854 bfd *output_bfd;
9855 Elf_Internal_Shdr *symtab_hdr;
9856 size_t locsymcount;
9857 size_t extsymoff;
9858 Elf_Internal_Sym *isymbuf;
9859 Elf_Internal_Sym *isym;
9860 Elf_Internal_Sym *isymend;
9861 long *pindex;
9862 asection **ppsection;
9863 asection *o;
9864 const struct elf_backend_data *bed;
9865 struct elf_link_hash_entry **sym_hashes;
9866 bfd_size_type address_size;
9867 bfd_vma r_type_mask;
9868 int r_sym_shift;
9869 bfd_boolean have_file_sym = FALSE;
9870
9871 output_bfd = flinfo->output_bfd;
9872 bed = get_elf_backend_data (output_bfd);
9873 relocate_section = bed->elf_backend_relocate_section;
9874
9875 /* If this is a dynamic object, we don't want to do anything here:
9876 we don't want the local symbols, and we don't want the section
9877 contents. */
9878 if ((input_bfd->flags & DYNAMIC) != 0)
9879 return TRUE;
9880
9881 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9882 if (elf_bad_symtab (input_bfd))
9883 {
9884 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9885 extsymoff = 0;
9886 }
9887 else
9888 {
9889 locsymcount = symtab_hdr->sh_info;
9890 extsymoff = symtab_hdr->sh_info;
9891 }
9892
9893 /* Read the local symbols. */
9894 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9895 if (isymbuf == NULL && locsymcount != 0)
9896 {
9897 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
9898 flinfo->internal_syms,
9899 flinfo->external_syms,
9900 flinfo->locsym_shndx);
9901 if (isymbuf == NULL)
9902 return FALSE;
9903 }
9904
9905 /* Find local symbol sections and adjust values of symbols in
9906 SEC_MERGE sections. Write out those local symbols we know are
9907 going into the output file. */
9908 isymend = isymbuf + locsymcount;
9909 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
9910 isym < isymend;
9911 isym++, pindex++, ppsection++)
9912 {
9913 asection *isec;
9914 const char *name;
9915 Elf_Internal_Sym osym;
9916 long indx;
9917 int ret;
9918
9919 *pindex = -1;
9920
9921 if (elf_bad_symtab (input_bfd))
9922 {
9923 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9924 {
9925 *ppsection = NULL;
9926 continue;
9927 }
9928 }
9929
9930 if (isym->st_shndx == SHN_UNDEF)
9931 isec = bfd_und_section_ptr;
9932 else if (isym->st_shndx == SHN_ABS)
9933 isec = bfd_abs_section_ptr;
9934 else if (isym->st_shndx == SHN_COMMON)
9935 isec = bfd_com_section_ptr;
9936 else
9937 {
9938 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9939 if (isec == NULL)
9940 {
9941 /* Don't attempt to output symbols with st_shnx in the
9942 reserved range other than SHN_ABS and SHN_COMMON. */
9943 *ppsection = NULL;
9944 continue;
9945 }
9946 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
9947 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9948 isym->st_value =
9949 _bfd_merged_section_offset (output_bfd, &isec,
9950 elf_section_data (isec)->sec_info,
9951 isym->st_value);
9952 }
9953
9954 *ppsection = isec;
9955
9956 /* Don't output the first, undefined, symbol. In fact, don't
9957 output any undefined local symbol. */
9958 if (isec == bfd_und_section_ptr)
9959 continue;
9960
9961 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9962 {
9963 /* We never output section symbols. Instead, we use the
9964 section symbol of the corresponding section in the output
9965 file. */
9966 continue;
9967 }
9968
9969 /* If we are stripping all symbols, we don't want to output this
9970 one. */
9971 if (flinfo->info->strip == strip_all)
9972 continue;
9973
9974 /* If we are discarding all local symbols, we don't want to
9975 output this one. If we are generating a relocatable output
9976 file, then some of the local symbols may be required by
9977 relocs; we output them below as we discover that they are
9978 needed. */
9979 if (flinfo->info->discard == discard_all)
9980 continue;
9981
9982 /* If this symbol is defined in a section which we are
9983 discarding, we don't need to keep it. */
9984 if (isym->st_shndx != SHN_UNDEF
9985 && isym->st_shndx < SHN_LORESERVE
9986 && bfd_section_removed_from_list (output_bfd,
9987 isec->output_section))
9988 continue;
9989
9990 /* Get the name of the symbol. */
9991 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9992 isym->st_name);
9993 if (name == NULL)
9994 return FALSE;
9995
9996 /* See if we are discarding symbols with this name. */
9997 if ((flinfo->info->strip == strip_some
9998 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
9999 == NULL))
10000 || (((flinfo->info->discard == discard_sec_merge
10001 && (isec->flags & SEC_MERGE)
10002 && !bfd_link_relocatable (flinfo->info))
10003 || flinfo->info->discard == discard_l)
10004 && bfd_is_local_label_name (input_bfd, name)))
10005 continue;
10006
10007 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10008 {
10009 if (input_bfd->lto_output)
10010 /* -flto puts a temp file name here. This means builds
10011 are not reproducible. Discard the symbol. */
10012 continue;
10013 have_file_sym = TRUE;
10014 flinfo->filesym_count += 1;
10015 }
10016 if (!have_file_sym)
10017 {
10018 /* In the absence of debug info, bfd_find_nearest_line uses
10019 FILE symbols to determine the source file for local
10020 function symbols. Provide a FILE symbol here if input
10021 files lack such, so that their symbols won't be
10022 associated with a previous input file. It's not the
10023 source file, but the best we can do. */
10024 have_file_sym = TRUE;
10025 flinfo->filesym_count += 1;
10026 memset (&osym, 0, sizeof (osym));
10027 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10028 osym.st_shndx = SHN_ABS;
10029 if (!elf_link_output_symstrtab (flinfo,
10030 (input_bfd->lto_output ? NULL
10031 : input_bfd->filename),
10032 &osym, bfd_abs_section_ptr,
10033 NULL))
10034 return FALSE;
10035 }
10036
10037 osym = *isym;
10038
10039 /* Adjust the section index for the output file. */
10040 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10041 isec->output_section);
10042 if (osym.st_shndx == SHN_BAD)
10043 return FALSE;
10044
10045 /* ELF symbols in relocatable files are section relative, but
10046 in executable files they are virtual addresses. Note that
10047 this code assumes that all ELF sections have an associated
10048 BFD section with a reasonable value for output_offset; below
10049 we assume that they also have a reasonable value for
10050 output_section. Any special sections must be set up to meet
10051 these requirements. */
10052 osym.st_value += isec->output_offset;
10053 if (!bfd_link_relocatable (flinfo->info))
10054 {
10055 osym.st_value += isec->output_section->vma;
10056 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10057 {
10058 /* STT_TLS symbols are relative to PT_TLS segment base. */
10059 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10060 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10061 }
10062 }
10063
10064 indx = bfd_get_symcount (output_bfd);
10065 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10066 if (ret == 0)
10067 return FALSE;
10068 else if (ret == 1)
10069 *pindex = indx;
10070 }
10071
10072 if (bed->s->arch_size == 32)
10073 {
10074 r_type_mask = 0xff;
10075 r_sym_shift = 8;
10076 address_size = 4;
10077 }
10078 else
10079 {
10080 r_type_mask = 0xffffffff;
10081 r_sym_shift = 32;
10082 address_size = 8;
10083 }
10084
10085 /* Relocate the contents of each section. */
10086 sym_hashes = elf_sym_hashes (input_bfd);
10087 for (o = input_bfd->sections; o != NULL; o = o->next)
10088 {
10089 bfd_byte *contents;
10090
10091 if (! o->linker_mark)
10092 {
10093 /* This section was omitted from the link. */
10094 continue;
10095 }
10096
10097 if (bfd_link_relocatable (flinfo->info)
10098 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10099 {
10100 /* Deal with the group signature symbol. */
10101 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10102 unsigned long symndx = sec_data->this_hdr.sh_info;
10103 asection *osec = o->output_section;
10104
10105 if (symndx >= locsymcount
10106 || (elf_bad_symtab (input_bfd)
10107 && flinfo->sections[symndx] == NULL))
10108 {
10109 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10110 while (h->root.type == bfd_link_hash_indirect
10111 || h->root.type == bfd_link_hash_warning)
10112 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10113 /* Arrange for symbol to be output. */
10114 h->indx = -2;
10115 elf_section_data (osec)->this_hdr.sh_info = -2;
10116 }
10117 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10118 {
10119 /* We'll use the output section target_index. */
10120 asection *sec = flinfo->sections[symndx]->output_section;
10121 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10122 }
10123 else
10124 {
10125 if (flinfo->indices[symndx] == -1)
10126 {
10127 /* Otherwise output the local symbol now. */
10128 Elf_Internal_Sym sym = isymbuf[symndx];
10129 asection *sec = flinfo->sections[symndx]->output_section;
10130 const char *name;
10131 long indx;
10132 int ret;
10133
10134 name = bfd_elf_string_from_elf_section (input_bfd,
10135 symtab_hdr->sh_link,
10136 sym.st_name);
10137 if (name == NULL)
10138 return FALSE;
10139
10140 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10141 sec);
10142 if (sym.st_shndx == SHN_BAD)
10143 return FALSE;
10144
10145 sym.st_value += o->output_offset;
10146
10147 indx = bfd_get_symcount (output_bfd);
10148 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10149 NULL);
10150 if (ret == 0)
10151 return FALSE;
10152 else if (ret == 1)
10153 flinfo->indices[symndx] = indx;
10154 else
10155 abort ();
10156 }
10157 elf_section_data (osec)->this_hdr.sh_info
10158 = flinfo->indices[symndx];
10159 }
10160 }
10161
10162 if ((o->flags & SEC_HAS_CONTENTS) == 0
10163 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10164 continue;
10165
10166 if ((o->flags & SEC_LINKER_CREATED) != 0)
10167 {
10168 /* Section was created by _bfd_elf_link_create_dynamic_sections
10169 or somesuch. */
10170 continue;
10171 }
10172
10173 /* Get the contents of the section. They have been cached by a
10174 relaxation routine. Note that o is a section in an input
10175 file, so the contents field will not have been set by any of
10176 the routines which work on output files. */
10177 if (elf_section_data (o)->this_hdr.contents != NULL)
10178 {
10179 contents = elf_section_data (o)->this_hdr.contents;
10180 if (bed->caches_rawsize
10181 && o->rawsize != 0
10182 && o->rawsize < o->size)
10183 {
10184 memcpy (flinfo->contents, contents, o->rawsize);
10185 contents = flinfo->contents;
10186 }
10187 }
10188 else
10189 {
10190 contents = flinfo->contents;
10191 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10192 return FALSE;
10193 }
10194
10195 if ((o->flags & SEC_RELOC) != 0)
10196 {
10197 Elf_Internal_Rela *internal_relocs;
10198 Elf_Internal_Rela *rel, *relend;
10199 int action_discarded;
10200 int ret;
10201
10202 /* Get the swapped relocs. */
10203 internal_relocs
10204 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10205 flinfo->internal_relocs, FALSE);
10206 if (internal_relocs == NULL
10207 && o->reloc_count > 0)
10208 return FALSE;
10209
10210 /* We need to reverse-copy input .ctors/.dtors sections if
10211 they are placed in .init_array/.finit_array for output. */
10212 if (o->size > address_size
10213 && ((strncmp (o->name, ".ctors", 6) == 0
10214 && strcmp (o->output_section->name,
10215 ".init_array") == 0)
10216 || (strncmp (o->name, ".dtors", 6) == 0
10217 && strcmp (o->output_section->name,
10218 ".fini_array") == 0))
10219 && (o->name[6] == 0 || o->name[6] == '.'))
10220 {
10221 if (o->size != o->reloc_count * address_size)
10222 {
10223 _bfd_error_handler
10224 /* xgettext:c-format */
10225 (_("error: %B: size of section %A is not "
10226 "multiple of address size"),
10227 input_bfd, o);
10228 bfd_set_error (bfd_error_on_input);
10229 return FALSE;
10230 }
10231 o->flags |= SEC_ELF_REVERSE_COPY;
10232 }
10233
10234 action_discarded = -1;
10235 if (!elf_section_ignore_discarded_relocs (o))
10236 action_discarded = (*bed->action_discarded) (o);
10237
10238 /* Run through the relocs evaluating complex reloc symbols and
10239 looking for relocs against symbols from discarded sections
10240 or section symbols from removed link-once sections.
10241 Complain about relocs against discarded sections. Zero
10242 relocs against removed link-once sections. */
10243
10244 rel = internal_relocs;
10245 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
10246 for ( ; rel < relend; rel++)
10247 {
10248 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10249 unsigned int s_type;
10250 asection **ps, *sec;
10251 struct elf_link_hash_entry *h = NULL;
10252 const char *sym_name;
10253
10254 if (r_symndx == STN_UNDEF)
10255 continue;
10256
10257 if (r_symndx >= locsymcount
10258 || (elf_bad_symtab (input_bfd)
10259 && flinfo->sections[r_symndx] == NULL))
10260 {
10261 h = sym_hashes[r_symndx - extsymoff];
10262
10263 /* Badly formatted input files can contain relocs that
10264 reference non-existant symbols. Check here so that
10265 we do not seg fault. */
10266 if (h == NULL)
10267 {
10268 char buffer [32];
10269
10270 sprintf_vma (buffer, rel->r_info);
10271 _bfd_error_handler
10272 /* xgettext:c-format */
10273 (_("error: %B contains a reloc (0x%s) for section %A "
10274 "that references a non-existent global symbol"),
10275 input_bfd, o, buffer);
10276 bfd_set_error (bfd_error_bad_value);
10277 return FALSE;
10278 }
10279
10280 while (h->root.type == bfd_link_hash_indirect
10281 || h->root.type == bfd_link_hash_warning)
10282 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10283
10284 s_type = h->type;
10285
10286 /* If a plugin symbol is referenced from a non-IR file,
10287 mark the symbol as undefined. Note that the
10288 linker may attach linker created dynamic sections
10289 to the plugin bfd. Symbols defined in linker
10290 created sections are not plugin symbols. */
10291 if (h->root.non_ir_ref
10292 && (h->root.type == bfd_link_hash_defined
10293 || h->root.type == bfd_link_hash_defweak)
10294 && (h->root.u.def.section->flags
10295 & SEC_LINKER_CREATED) == 0
10296 && h->root.u.def.section->owner != NULL
10297 && (h->root.u.def.section->owner->flags
10298 & BFD_PLUGIN) != 0)
10299 {
10300 h->root.type = bfd_link_hash_undefined;
10301 h->root.u.undef.abfd = h->root.u.def.section->owner;
10302 }
10303
10304 ps = NULL;
10305 if (h->root.type == bfd_link_hash_defined
10306 || h->root.type == bfd_link_hash_defweak)
10307 ps = &h->root.u.def.section;
10308
10309 sym_name = h->root.root.string;
10310 }
10311 else
10312 {
10313 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10314
10315 s_type = ELF_ST_TYPE (sym->st_info);
10316 ps = &flinfo->sections[r_symndx];
10317 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10318 sym, *ps);
10319 }
10320
10321 if ((s_type == STT_RELC || s_type == STT_SRELC)
10322 && !bfd_link_relocatable (flinfo->info))
10323 {
10324 bfd_vma val;
10325 bfd_vma dot = (rel->r_offset
10326 + o->output_offset + o->output_section->vma);
10327 #ifdef DEBUG
10328 printf ("Encountered a complex symbol!");
10329 printf (" (input_bfd %s, section %s, reloc %ld\n",
10330 input_bfd->filename, o->name,
10331 (long) (rel - internal_relocs));
10332 printf (" symbol: idx %8.8lx, name %s\n",
10333 r_symndx, sym_name);
10334 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10335 (unsigned long) rel->r_info,
10336 (unsigned long) rel->r_offset);
10337 #endif
10338 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10339 isymbuf, locsymcount, s_type == STT_SRELC))
10340 return FALSE;
10341
10342 /* Symbol evaluated OK. Update to absolute value. */
10343 set_symbol_value (input_bfd, isymbuf, locsymcount,
10344 r_symndx, val);
10345 continue;
10346 }
10347
10348 if (action_discarded != -1 && ps != NULL)
10349 {
10350 /* Complain if the definition comes from a
10351 discarded section. */
10352 if ((sec = *ps) != NULL && discarded_section (sec))
10353 {
10354 BFD_ASSERT (r_symndx != STN_UNDEF);
10355 if (action_discarded & COMPLAIN)
10356 (*flinfo->info->callbacks->einfo)
10357 /* xgettext:c-format */
10358 (_("%X`%s' referenced in section `%A' of %B: "
10359 "defined in discarded section `%A' of %B\n"),
10360 sym_name, o, input_bfd, sec, sec->owner);
10361
10362 /* Try to do the best we can to support buggy old
10363 versions of gcc. Pretend that the symbol is
10364 really defined in the kept linkonce section.
10365 FIXME: This is quite broken. Modifying the
10366 symbol here means we will be changing all later
10367 uses of the symbol, not just in this section. */
10368 if (action_discarded & PRETEND)
10369 {
10370 asection *kept;
10371
10372 kept = _bfd_elf_check_kept_section (sec,
10373 flinfo->info);
10374 if (kept != NULL)
10375 {
10376 *ps = kept;
10377 continue;
10378 }
10379 }
10380 }
10381 }
10382 }
10383
10384 /* Relocate the section by invoking a back end routine.
10385
10386 The back end routine is responsible for adjusting the
10387 section contents as necessary, and (if using Rela relocs
10388 and generating a relocatable output file) adjusting the
10389 reloc addend as necessary.
10390
10391 The back end routine does not have to worry about setting
10392 the reloc address or the reloc symbol index.
10393
10394 The back end routine is given a pointer to the swapped in
10395 internal symbols, and can access the hash table entries
10396 for the external symbols via elf_sym_hashes (input_bfd).
10397
10398 When generating relocatable output, the back end routine
10399 must handle STB_LOCAL/STT_SECTION symbols specially. The
10400 output symbol is going to be a section symbol
10401 corresponding to the output section, which will require
10402 the addend to be adjusted. */
10403
10404 ret = (*relocate_section) (output_bfd, flinfo->info,
10405 input_bfd, o, contents,
10406 internal_relocs,
10407 isymbuf,
10408 flinfo->sections);
10409 if (!ret)
10410 return FALSE;
10411
10412 if (ret == 2
10413 || bfd_link_relocatable (flinfo->info)
10414 || flinfo->info->emitrelocations)
10415 {
10416 Elf_Internal_Rela *irela;
10417 Elf_Internal_Rela *irelaend, *irelamid;
10418 bfd_vma last_offset;
10419 struct elf_link_hash_entry **rel_hash;
10420 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10421 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10422 unsigned int next_erel;
10423 bfd_boolean rela_normal;
10424 struct bfd_elf_section_data *esdi, *esdo;
10425
10426 esdi = elf_section_data (o);
10427 esdo = elf_section_data (o->output_section);
10428 rela_normal = FALSE;
10429
10430 /* Adjust the reloc addresses and symbol indices. */
10431
10432 irela = internal_relocs;
10433 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
10434 rel_hash = esdo->rel.hashes + esdo->rel.count;
10435 /* We start processing the REL relocs, if any. When we reach
10436 IRELAMID in the loop, we switch to the RELA relocs. */
10437 irelamid = irela;
10438 if (esdi->rel.hdr != NULL)
10439 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10440 * bed->s->int_rels_per_ext_rel);
10441 rel_hash_list = rel_hash;
10442 rela_hash_list = NULL;
10443 last_offset = o->output_offset;
10444 if (!bfd_link_relocatable (flinfo->info))
10445 last_offset += o->output_section->vma;
10446 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10447 {
10448 unsigned long r_symndx;
10449 asection *sec;
10450 Elf_Internal_Sym sym;
10451
10452 if (next_erel == bed->s->int_rels_per_ext_rel)
10453 {
10454 rel_hash++;
10455 next_erel = 0;
10456 }
10457
10458 if (irela == irelamid)
10459 {
10460 rel_hash = esdo->rela.hashes + esdo->rela.count;
10461 rela_hash_list = rel_hash;
10462 rela_normal = bed->rela_normal;
10463 }
10464
10465 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10466 flinfo->info, o,
10467 irela->r_offset);
10468 if (irela->r_offset >= (bfd_vma) -2)
10469 {
10470 /* This is a reloc for a deleted entry or somesuch.
10471 Turn it into an R_*_NONE reloc, at the same
10472 offset as the last reloc. elf_eh_frame.c and
10473 bfd_elf_discard_info rely on reloc offsets
10474 being ordered. */
10475 irela->r_offset = last_offset;
10476 irela->r_info = 0;
10477 irela->r_addend = 0;
10478 continue;
10479 }
10480
10481 irela->r_offset += o->output_offset;
10482
10483 /* Relocs in an executable have to be virtual addresses. */
10484 if (!bfd_link_relocatable (flinfo->info))
10485 irela->r_offset += o->output_section->vma;
10486
10487 last_offset = irela->r_offset;
10488
10489 r_symndx = irela->r_info >> r_sym_shift;
10490 if (r_symndx == STN_UNDEF)
10491 continue;
10492
10493 if (r_symndx >= locsymcount
10494 || (elf_bad_symtab (input_bfd)
10495 && flinfo->sections[r_symndx] == NULL))
10496 {
10497 struct elf_link_hash_entry *rh;
10498 unsigned long indx;
10499
10500 /* This is a reloc against a global symbol. We
10501 have not yet output all the local symbols, so
10502 we do not know the symbol index of any global
10503 symbol. We set the rel_hash entry for this
10504 reloc to point to the global hash table entry
10505 for this symbol. The symbol index is then
10506 set at the end of bfd_elf_final_link. */
10507 indx = r_symndx - extsymoff;
10508 rh = elf_sym_hashes (input_bfd)[indx];
10509 while (rh->root.type == bfd_link_hash_indirect
10510 || rh->root.type == bfd_link_hash_warning)
10511 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10512
10513 /* Setting the index to -2 tells
10514 elf_link_output_extsym that this symbol is
10515 used by a reloc. */
10516 BFD_ASSERT (rh->indx < 0);
10517 rh->indx = -2;
10518
10519 *rel_hash = rh;
10520
10521 continue;
10522 }
10523
10524 /* This is a reloc against a local symbol. */
10525
10526 *rel_hash = NULL;
10527 sym = isymbuf[r_symndx];
10528 sec = flinfo->sections[r_symndx];
10529 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10530 {
10531 /* I suppose the backend ought to fill in the
10532 section of any STT_SECTION symbol against a
10533 processor specific section. */
10534 r_symndx = STN_UNDEF;
10535 if (bfd_is_abs_section (sec))
10536 ;
10537 else if (sec == NULL || sec->owner == NULL)
10538 {
10539 bfd_set_error (bfd_error_bad_value);
10540 return FALSE;
10541 }
10542 else
10543 {
10544 asection *osec = sec->output_section;
10545
10546 /* If we have discarded a section, the output
10547 section will be the absolute section. In
10548 case of discarded SEC_MERGE sections, use
10549 the kept section. relocate_section should
10550 have already handled discarded linkonce
10551 sections. */
10552 if (bfd_is_abs_section (osec)
10553 && sec->kept_section != NULL
10554 && sec->kept_section->output_section != NULL)
10555 {
10556 osec = sec->kept_section->output_section;
10557 irela->r_addend -= osec->vma;
10558 }
10559
10560 if (!bfd_is_abs_section (osec))
10561 {
10562 r_symndx = osec->target_index;
10563 if (r_symndx == STN_UNDEF)
10564 {
10565 irela->r_addend += osec->vma;
10566 osec = _bfd_nearby_section (output_bfd, osec,
10567 osec->vma);
10568 irela->r_addend -= osec->vma;
10569 r_symndx = osec->target_index;
10570 }
10571 }
10572 }
10573
10574 /* Adjust the addend according to where the
10575 section winds up in the output section. */
10576 if (rela_normal)
10577 irela->r_addend += sec->output_offset;
10578 }
10579 else
10580 {
10581 if (flinfo->indices[r_symndx] == -1)
10582 {
10583 unsigned long shlink;
10584 const char *name;
10585 asection *osec;
10586 long indx;
10587
10588 if (flinfo->info->strip == strip_all)
10589 {
10590 /* You can't do ld -r -s. */
10591 bfd_set_error (bfd_error_invalid_operation);
10592 return FALSE;
10593 }
10594
10595 /* This symbol was skipped earlier, but
10596 since it is needed by a reloc, we
10597 must output it now. */
10598 shlink = symtab_hdr->sh_link;
10599 name = (bfd_elf_string_from_elf_section
10600 (input_bfd, shlink, sym.st_name));
10601 if (name == NULL)
10602 return FALSE;
10603
10604 osec = sec->output_section;
10605 sym.st_shndx =
10606 _bfd_elf_section_from_bfd_section (output_bfd,
10607 osec);
10608 if (sym.st_shndx == SHN_BAD)
10609 return FALSE;
10610
10611 sym.st_value += sec->output_offset;
10612 if (!bfd_link_relocatable (flinfo->info))
10613 {
10614 sym.st_value += osec->vma;
10615 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10616 {
10617 /* STT_TLS symbols are relative to PT_TLS
10618 segment base. */
10619 BFD_ASSERT (elf_hash_table (flinfo->info)
10620 ->tls_sec != NULL);
10621 sym.st_value -= (elf_hash_table (flinfo->info)
10622 ->tls_sec->vma);
10623 }
10624 }
10625
10626 indx = bfd_get_symcount (output_bfd);
10627 ret = elf_link_output_symstrtab (flinfo, name,
10628 &sym, sec,
10629 NULL);
10630 if (ret == 0)
10631 return FALSE;
10632 else if (ret == 1)
10633 flinfo->indices[r_symndx] = indx;
10634 else
10635 abort ();
10636 }
10637
10638 r_symndx = flinfo->indices[r_symndx];
10639 }
10640
10641 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10642 | (irela->r_info & r_type_mask));
10643 }
10644
10645 /* Swap out the relocs. */
10646 input_rel_hdr = esdi->rel.hdr;
10647 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
10648 {
10649 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10650 input_rel_hdr,
10651 internal_relocs,
10652 rel_hash_list))
10653 return FALSE;
10654 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10655 * bed->s->int_rels_per_ext_rel);
10656 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
10657 }
10658
10659 input_rela_hdr = esdi->rela.hdr;
10660 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10661 {
10662 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10663 input_rela_hdr,
10664 internal_relocs,
10665 rela_hash_list))
10666 return FALSE;
10667 }
10668 }
10669 }
10670
10671 /* Write out the modified section contents. */
10672 if (bed->elf_backend_write_section
10673 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
10674 contents))
10675 {
10676 /* Section written out. */
10677 }
10678 else switch (o->sec_info_type)
10679 {
10680 case SEC_INFO_TYPE_STABS:
10681 if (! (_bfd_write_section_stabs
10682 (output_bfd,
10683 &elf_hash_table (flinfo->info)->stab_info,
10684 o, &elf_section_data (o)->sec_info, contents)))
10685 return FALSE;
10686 break;
10687 case SEC_INFO_TYPE_MERGE:
10688 if (! _bfd_write_merged_section (output_bfd, o,
10689 elf_section_data (o)->sec_info))
10690 return FALSE;
10691 break;
10692 case SEC_INFO_TYPE_EH_FRAME:
10693 {
10694 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
10695 o, contents))
10696 return FALSE;
10697 }
10698 break;
10699 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10700 {
10701 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10702 flinfo->info,
10703 o, contents))
10704 return FALSE;
10705 }
10706 break;
10707 default:
10708 {
10709 if (! (o->flags & SEC_EXCLUDE))
10710 {
10711 file_ptr offset = (file_ptr) o->output_offset;
10712 bfd_size_type todo = o->size;
10713
10714 offset *= bfd_octets_per_byte (output_bfd);
10715
10716 if ((o->flags & SEC_ELF_REVERSE_COPY))
10717 {
10718 /* Reverse-copy input section to output. */
10719 do
10720 {
10721 todo -= address_size;
10722 if (! bfd_set_section_contents (output_bfd,
10723 o->output_section,
10724 contents + todo,
10725 offset,
10726 address_size))
10727 return FALSE;
10728 if (todo == 0)
10729 break;
10730 offset += address_size;
10731 }
10732 while (1);
10733 }
10734 else if (! bfd_set_section_contents (output_bfd,
10735 o->output_section,
10736 contents,
10737 offset, todo))
10738 return FALSE;
10739 }
10740 }
10741 break;
10742 }
10743 }
10744
10745 return TRUE;
10746 }
10747
10748 /* Generate a reloc when linking an ELF file. This is a reloc
10749 requested by the linker, and does not come from any input file. This
10750 is used to build constructor and destructor tables when linking
10751 with -Ur. */
10752
10753 static bfd_boolean
10754 elf_reloc_link_order (bfd *output_bfd,
10755 struct bfd_link_info *info,
10756 asection *output_section,
10757 struct bfd_link_order *link_order)
10758 {
10759 reloc_howto_type *howto;
10760 long indx;
10761 bfd_vma offset;
10762 bfd_vma addend;
10763 struct bfd_elf_section_reloc_data *reldata;
10764 struct elf_link_hash_entry **rel_hash_ptr;
10765 Elf_Internal_Shdr *rel_hdr;
10766 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10767 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10768 bfd_byte *erel;
10769 unsigned int i;
10770 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
10771
10772 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10773 if (howto == NULL)
10774 {
10775 bfd_set_error (bfd_error_bad_value);
10776 return FALSE;
10777 }
10778
10779 addend = link_order->u.reloc.p->addend;
10780
10781 if (esdo->rel.hdr)
10782 reldata = &esdo->rel;
10783 else if (esdo->rela.hdr)
10784 reldata = &esdo->rela;
10785 else
10786 {
10787 reldata = NULL;
10788 BFD_ASSERT (0);
10789 }
10790
10791 /* Figure out the symbol index. */
10792 rel_hash_ptr = reldata->hashes + reldata->count;
10793 if (link_order->type == bfd_section_reloc_link_order)
10794 {
10795 indx = link_order->u.reloc.p->u.section->target_index;
10796 BFD_ASSERT (indx != 0);
10797 *rel_hash_ptr = NULL;
10798 }
10799 else
10800 {
10801 struct elf_link_hash_entry *h;
10802
10803 /* Treat a reloc against a defined symbol as though it were
10804 actually against the section. */
10805 h = ((struct elf_link_hash_entry *)
10806 bfd_wrapped_link_hash_lookup (output_bfd, info,
10807 link_order->u.reloc.p->u.name,
10808 FALSE, FALSE, TRUE));
10809 if (h != NULL
10810 && (h->root.type == bfd_link_hash_defined
10811 || h->root.type == bfd_link_hash_defweak))
10812 {
10813 asection *section;
10814
10815 section = h->root.u.def.section;
10816 indx = section->output_section->target_index;
10817 *rel_hash_ptr = NULL;
10818 /* It seems that we ought to add the symbol value to the
10819 addend here, but in practice it has already been added
10820 because it was passed to constructor_callback. */
10821 addend += section->output_section->vma + section->output_offset;
10822 }
10823 else if (h != NULL)
10824 {
10825 /* Setting the index to -2 tells elf_link_output_extsym that
10826 this symbol is used by a reloc. */
10827 h->indx = -2;
10828 *rel_hash_ptr = h;
10829 indx = 0;
10830 }
10831 else
10832 {
10833 (*info->callbacks->unattached_reloc)
10834 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
10835 indx = 0;
10836 }
10837 }
10838
10839 /* If this is an inplace reloc, we must write the addend into the
10840 object file. */
10841 if (howto->partial_inplace && addend != 0)
10842 {
10843 bfd_size_type size;
10844 bfd_reloc_status_type rstat;
10845 bfd_byte *buf;
10846 bfd_boolean ok;
10847 const char *sym_name;
10848
10849 size = (bfd_size_type) bfd_get_reloc_size (howto);
10850 buf = (bfd_byte *) bfd_zmalloc (size);
10851 if (buf == NULL && size != 0)
10852 return FALSE;
10853 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10854 switch (rstat)
10855 {
10856 case bfd_reloc_ok:
10857 break;
10858
10859 default:
10860 case bfd_reloc_outofrange:
10861 abort ();
10862
10863 case bfd_reloc_overflow:
10864 if (link_order->type == bfd_section_reloc_link_order)
10865 sym_name = bfd_section_name (output_bfd,
10866 link_order->u.reloc.p->u.section);
10867 else
10868 sym_name = link_order->u.reloc.p->u.name;
10869 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
10870 howto->name, addend, NULL, NULL,
10871 (bfd_vma) 0);
10872 break;
10873 }
10874
10875 ok = bfd_set_section_contents (output_bfd, output_section, buf,
10876 link_order->offset
10877 * bfd_octets_per_byte (output_bfd),
10878 size);
10879 free (buf);
10880 if (! ok)
10881 return FALSE;
10882 }
10883
10884 /* The address of a reloc is relative to the section in a
10885 relocatable file, and is a virtual address in an executable
10886 file. */
10887 offset = link_order->offset;
10888 if (! bfd_link_relocatable (info))
10889 offset += output_section->vma;
10890
10891 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10892 {
10893 irel[i].r_offset = offset;
10894 irel[i].r_info = 0;
10895 irel[i].r_addend = 0;
10896 }
10897 if (bed->s->arch_size == 32)
10898 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10899 else
10900 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10901
10902 rel_hdr = reldata->hdr;
10903 erel = rel_hdr->contents;
10904 if (rel_hdr->sh_type == SHT_REL)
10905 {
10906 erel += reldata->count * bed->s->sizeof_rel;
10907 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10908 }
10909 else
10910 {
10911 irel[0].r_addend = addend;
10912 erel += reldata->count * bed->s->sizeof_rela;
10913 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10914 }
10915
10916 ++reldata->count;
10917
10918 return TRUE;
10919 }
10920
10921
10922 /* Get the output vma of the section pointed to by the sh_link field. */
10923
10924 static bfd_vma
10925 elf_get_linked_section_vma (struct bfd_link_order *p)
10926 {
10927 Elf_Internal_Shdr **elf_shdrp;
10928 asection *s;
10929 int elfsec;
10930
10931 s = p->u.indirect.section;
10932 elf_shdrp = elf_elfsections (s->owner);
10933 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10934 elfsec = elf_shdrp[elfsec]->sh_link;
10935 /* PR 290:
10936 The Intel C compiler generates SHT_IA_64_UNWIND with
10937 SHF_LINK_ORDER. But it doesn't set the sh_link or
10938 sh_info fields. Hence we could get the situation
10939 where elfsec is 0. */
10940 if (elfsec == 0)
10941 {
10942 const struct elf_backend_data *bed
10943 = get_elf_backend_data (s->owner);
10944 if (bed->link_order_error_handler)
10945 bed->link_order_error_handler
10946 /* xgettext:c-format */
10947 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
10948 return 0;
10949 }
10950 else
10951 {
10952 s = elf_shdrp[elfsec]->bfd_section;
10953 return s->output_section->vma + s->output_offset;
10954 }
10955 }
10956
10957
10958 /* Compare two sections based on the locations of the sections they are
10959 linked to. Used by elf_fixup_link_order. */
10960
10961 static int
10962 compare_link_order (const void * a, const void * b)
10963 {
10964 bfd_vma apos;
10965 bfd_vma bpos;
10966
10967 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10968 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10969 if (apos < bpos)
10970 return -1;
10971 return apos > bpos;
10972 }
10973
10974
10975 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10976 order as their linked sections. Returns false if this could not be done
10977 because an output section includes both ordered and unordered
10978 sections. Ideally we'd do this in the linker proper. */
10979
10980 static bfd_boolean
10981 elf_fixup_link_order (bfd *abfd, asection *o)
10982 {
10983 int seen_linkorder;
10984 int seen_other;
10985 int n;
10986 struct bfd_link_order *p;
10987 bfd *sub;
10988 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10989 unsigned elfsec;
10990 struct bfd_link_order **sections;
10991 asection *s, *other_sec, *linkorder_sec;
10992 bfd_vma offset;
10993
10994 other_sec = NULL;
10995 linkorder_sec = NULL;
10996 seen_other = 0;
10997 seen_linkorder = 0;
10998 for (p = o->map_head.link_order; p != NULL; p = p->next)
10999 {
11000 if (p->type == bfd_indirect_link_order)
11001 {
11002 s = p->u.indirect.section;
11003 sub = s->owner;
11004 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11005 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
11006 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11007 && elfsec < elf_numsections (sub)
11008 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11009 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
11010 {
11011 seen_linkorder++;
11012 linkorder_sec = s;
11013 }
11014 else
11015 {
11016 seen_other++;
11017 other_sec = s;
11018 }
11019 }
11020 else
11021 seen_other++;
11022
11023 if (seen_other && seen_linkorder)
11024 {
11025 if (other_sec && linkorder_sec)
11026 _bfd_error_handler
11027 /* xgettext:c-format */
11028 (_("%A has both ordered [`%A' in %B] "
11029 "and unordered [`%A' in %B] sections"),
11030 o, linkorder_sec,
11031 linkorder_sec->owner, other_sec,
11032 other_sec->owner);
11033 else
11034 _bfd_error_handler
11035 (_("%A has both ordered and unordered sections"), o);
11036 bfd_set_error (bfd_error_bad_value);
11037 return FALSE;
11038 }
11039 }
11040
11041 if (!seen_linkorder)
11042 return TRUE;
11043
11044 sections = (struct bfd_link_order **)
11045 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11046 if (sections == NULL)
11047 return FALSE;
11048 seen_linkorder = 0;
11049
11050 for (p = o->map_head.link_order; p != NULL; p = p->next)
11051 {
11052 sections[seen_linkorder++] = p;
11053 }
11054 /* Sort the input sections in the order of their linked section. */
11055 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11056 compare_link_order);
11057
11058 /* Change the offsets of the sections. */
11059 offset = 0;
11060 for (n = 0; n < seen_linkorder; n++)
11061 {
11062 s = sections[n]->u.indirect.section;
11063 offset &= ~(bfd_vma) 0 << s->alignment_power;
11064 s->output_offset = offset / bfd_octets_per_byte (abfd);
11065 sections[n]->offset = offset;
11066 offset += sections[n]->size;
11067 }
11068
11069 free (sections);
11070 return TRUE;
11071 }
11072
11073 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11074 Returns TRUE upon success, FALSE otherwise. */
11075
11076 static bfd_boolean
11077 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11078 {
11079 bfd_boolean ret = FALSE;
11080 bfd *implib_bfd;
11081 const struct elf_backend_data *bed;
11082 flagword flags;
11083 enum bfd_architecture arch;
11084 unsigned int mach;
11085 asymbol **sympp = NULL;
11086 long symsize;
11087 long symcount;
11088 long src_count;
11089 elf_symbol_type *osymbuf;
11090
11091 implib_bfd = info->out_implib_bfd;
11092 bed = get_elf_backend_data (abfd);
11093
11094 if (!bfd_set_format (implib_bfd, bfd_object))
11095 return FALSE;
11096
11097 flags = bfd_get_file_flags (abfd);
11098 flags &= ~HAS_RELOC;
11099 if (!bfd_set_start_address (implib_bfd, 0)
11100 || !bfd_set_file_flags (implib_bfd, flags))
11101 return FALSE;
11102
11103 /* Copy architecture of output file to import library file. */
11104 arch = bfd_get_arch (abfd);
11105 mach = bfd_get_mach (abfd);
11106 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11107 && (abfd->target_defaulted
11108 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11109 return FALSE;
11110
11111 /* Get symbol table size. */
11112 symsize = bfd_get_symtab_upper_bound (abfd);
11113 if (symsize < 0)
11114 return FALSE;
11115
11116 /* Read in the symbol table. */
11117 sympp = (asymbol **) xmalloc (symsize);
11118 symcount = bfd_canonicalize_symtab (abfd, sympp);
11119 if (symcount < 0)
11120 goto free_sym_buf;
11121
11122 /* Allow the BFD backend to copy any private header data it
11123 understands from the output BFD to the import library BFD. */
11124 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11125 goto free_sym_buf;
11126
11127 /* Filter symbols to appear in the import library. */
11128 if (bed->elf_backend_filter_implib_symbols)
11129 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11130 symcount);
11131 else
11132 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11133 if (symcount == 0)
11134 {
11135 bfd_set_error (bfd_error_no_symbols);
11136 _bfd_error_handler (_("%B: no symbol found for import library"),
11137 implib_bfd);
11138 goto free_sym_buf;
11139 }
11140
11141
11142 /* Make symbols absolute. */
11143 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11144 sizeof (*osymbuf));
11145 for (src_count = 0; src_count < symcount; src_count++)
11146 {
11147 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11148 sizeof (*osymbuf));
11149 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11150 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11151 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11152 osymbuf[src_count].internal_elf_sym.st_value =
11153 osymbuf[src_count].symbol.value;
11154 sympp[src_count] = &osymbuf[src_count].symbol;
11155 }
11156
11157 bfd_set_symtab (implib_bfd, sympp, symcount);
11158
11159 /* Allow the BFD backend to copy any private data it understands
11160 from the output BFD to the import library BFD. This is done last
11161 to permit the routine to look at the filtered symbol table. */
11162 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11163 goto free_sym_buf;
11164
11165 if (!bfd_close (implib_bfd))
11166 goto free_sym_buf;
11167
11168 ret = TRUE;
11169
11170 free_sym_buf:
11171 free (sympp);
11172 return ret;
11173 }
11174
11175 static void
11176 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11177 {
11178 asection *o;
11179
11180 if (flinfo->symstrtab != NULL)
11181 _bfd_elf_strtab_free (flinfo->symstrtab);
11182 if (flinfo->contents != NULL)
11183 free (flinfo->contents);
11184 if (flinfo->external_relocs != NULL)
11185 free (flinfo->external_relocs);
11186 if (flinfo->internal_relocs != NULL)
11187 free (flinfo->internal_relocs);
11188 if (flinfo->external_syms != NULL)
11189 free (flinfo->external_syms);
11190 if (flinfo->locsym_shndx != NULL)
11191 free (flinfo->locsym_shndx);
11192 if (flinfo->internal_syms != NULL)
11193 free (flinfo->internal_syms);
11194 if (flinfo->indices != NULL)
11195 free (flinfo->indices);
11196 if (flinfo->sections != NULL)
11197 free (flinfo->sections);
11198 if (flinfo->symshndxbuf != NULL)
11199 free (flinfo->symshndxbuf);
11200 for (o = obfd->sections; o != NULL; o = o->next)
11201 {
11202 struct bfd_elf_section_data *esdo = elf_section_data (o);
11203 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11204 free (esdo->rel.hashes);
11205 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11206 free (esdo->rela.hashes);
11207 }
11208 }
11209
11210 /* Do the final step of an ELF link. */
11211
11212 bfd_boolean
11213 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11214 {
11215 bfd_boolean dynamic;
11216 bfd_boolean emit_relocs;
11217 bfd *dynobj;
11218 struct elf_final_link_info flinfo;
11219 asection *o;
11220 struct bfd_link_order *p;
11221 bfd *sub;
11222 bfd_size_type max_contents_size;
11223 bfd_size_type max_external_reloc_size;
11224 bfd_size_type max_internal_reloc_count;
11225 bfd_size_type max_sym_count;
11226 bfd_size_type max_sym_shndx_count;
11227 Elf_Internal_Sym elfsym;
11228 unsigned int i;
11229 Elf_Internal_Shdr *symtab_hdr;
11230 Elf_Internal_Shdr *symtab_shndx_hdr;
11231 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11232 struct elf_outext_info eoinfo;
11233 bfd_boolean merged;
11234 size_t relativecount = 0;
11235 asection *reldyn = 0;
11236 bfd_size_type amt;
11237 asection *attr_section = NULL;
11238 bfd_vma attr_size = 0;
11239 const char *std_attrs_section;
11240 struct elf_link_hash_table *htab = elf_hash_table (info);
11241
11242 if (!is_elf_hash_table (htab))
11243 return FALSE;
11244
11245 if (bfd_link_pic (info))
11246 abfd->flags |= DYNAMIC;
11247
11248 dynamic = htab->dynamic_sections_created;
11249 dynobj = htab->dynobj;
11250
11251 emit_relocs = (bfd_link_relocatable (info)
11252 || info->emitrelocations);
11253
11254 flinfo.info = info;
11255 flinfo.output_bfd = abfd;
11256 flinfo.symstrtab = _bfd_elf_strtab_init ();
11257 if (flinfo.symstrtab == NULL)
11258 return FALSE;
11259
11260 if (! dynamic)
11261 {
11262 flinfo.hash_sec = NULL;
11263 flinfo.symver_sec = NULL;
11264 }
11265 else
11266 {
11267 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11268 /* Note that dynsym_sec can be NULL (on VMS). */
11269 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11270 /* Note that it is OK if symver_sec is NULL. */
11271 }
11272
11273 flinfo.contents = NULL;
11274 flinfo.external_relocs = NULL;
11275 flinfo.internal_relocs = NULL;
11276 flinfo.external_syms = NULL;
11277 flinfo.locsym_shndx = NULL;
11278 flinfo.internal_syms = NULL;
11279 flinfo.indices = NULL;
11280 flinfo.sections = NULL;
11281 flinfo.symshndxbuf = NULL;
11282 flinfo.filesym_count = 0;
11283
11284 /* The object attributes have been merged. Remove the input
11285 sections from the link, and set the contents of the output
11286 secton. */
11287 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11288 for (o = abfd->sections; o != NULL; o = o->next)
11289 {
11290 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11291 || strcmp (o->name, ".gnu.attributes") == 0)
11292 {
11293 for (p = o->map_head.link_order; p != NULL; p = p->next)
11294 {
11295 asection *input_section;
11296
11297 if (p->type != bfd_indirect_link_order)
11298 continue;
11299 input_section = p->u.indirect.section;
11300 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11301 elf_link_input_bfd ignores this section. */
11302 input_section->flags &= ~SEC_HAS_CONTENTS;
11303 }
11304
11305 attr_size = bfd_elf_obj_attr_size (abfd);
11306 if (attr_size)
11307 {
11308 bfd_set_section_size (abfd, o, attr_size);
11309 attr_section = o;
11310 /* Skip this section later on. */
11311 o->map_head.link_order = NULL;
11312 }
11313 else
11314 o->flags |= SEC_EXCLUDE;
11315 }
11316 }
11317
11318 /* Count up the number of relocations we will output for each output
11319 section, so that we know the sizes of the reloc sections. We
11320 also figure out some maximum sizes. */
11321 max_contents_size = 0;
11322 max_external_reloc_size = 0;
11323 max_internal_reloc_count = 0;
11324 max_sym_count = 0;
11325 max_sym_shndx_count = 0;
11326 merged = FALSE;
11327 for (o = abfd->sections; o != NULL; o = o->next)
11328 {
11329 struct bfd_elf_section_data *esdo = elf_section_data (o);
11330 o->reloc_count = 0;
11331
11332 for (p = o->map_head.link_order; p != NULL; p = p->next)
11333 {
11334 unsigned int reloc_count = 0;
11335 unsigned int additional_reloc_count = 0;
11336 struct bfd_elf_section_data *esdi = NULL;
11337
11338 if (p->type == bfd_section_reloc_link_order
11339 || p->type == bfd_symbol_reloc_link_order)
11340 reloc_count = 1;
11341 else if (p->type == bfd_indirect_link_order)
11342 {
11343 asection *sec;
11344
11345 sec = p->u.indirect.section;
11346 esdi = elf_section_data (sec);
11347
11348 /* Mark all sections which are to be included in the
11349 link. This will normally be every section. We need
11350 to do this so that we can identify any sections which
11351 the linker has decided to not include. */
11352 sec->linker_mark = TRUE;
11353
11354 if (sec->flags & SEC_MERGE)
11355 merged = TRUE;
11356
11357 if (esdo->this_hdr.sh_type == SHT_REL
11358 || esdo->this_hdr.sh_type == SHT_RELA)
11359 /* Some backends use reloc_count in relocation sections
11360 to count particular types of relocs. Of course,
11361 reloc sections themselves can't have relocations. */
11362 reloc_count = 0;
11363 else if (emit_relocs)
11364 {
11365 reloc_count = sec->reloc_count;
11366 if (bed->elf_backend_count_additional_relocs)
11367 {
11368 int c;
11369 c = (*bed->elf_backend_count_additional_relocs) (sec);
11370 additional_reloc_count += c;
11371 }
11372 }
11373 else if (bed->elf_backend_count_relocs)
11374 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11375
11376 if (sec->rawsize > max_contents_size)
11377 max_contents_size = sec->rawsize;
11378 if (sec->size > max_contents_size)
11379 max_contents_size = sec->size;
11380
11381 /* We are interested in just local symbols, not all
11382 symbols. */
11383 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11384 && (sec->owner->flags & DYNAMIC) == 0)
11385 {
11386 size_t sym_count;
11387
11388 if (elf_bad_symtab (sec->owner))
11389 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11390 / bed->s->sizeof_sym);
11391 else
11392 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11393
11394 if (sym_count > max_sym_count)
11395 max_sym_count = sym_count;
11396
11397 if (sym_count > max_sym_shndx_count
11398 && elf_symtab_shndx_list (sec->owner) != NULL)
11399 max_sym_shndx_count = sym_count;
11400
11401 if ((sec->flags & SEC_RELOC) != 0)
11402 {
11403 size_t ext_size = 0;
11404
11405 if (esdi->rel.hdr != NULL)
11406 ext_size = esdi->rel.hdr->sh_size;
11407 if (esdi->rela.hdr != NULL)
11408 ext_size += esdi->rela.hdr->sh_size;
11409
11410 if (ext_size > max_external_reloc_size)
11411 max_external_reloc_size = ext_size;
11412 if (sec->reloc_count > max_internal_reloc_count)
11413 max_internal_reloc_count = sec->reloc_count;
11414 }
11415 }
11416 }
11417
11418 if (reloc_count == 0)
11419 continue;
11420
11421 reloc_count += additional_reloc_count;
11422 o->reloc_count += reloc_count;
11423
11424 if (p->type == bfd_indirect_link_order && emit_relocs)
11425 {
11426 if (esdi->rel.hdr)
11427 {
11428 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11429 esdo->rel.count += additional_reloc_count;
11430 }
11431 if (esdi->rela.hdr)
11432 {
11433 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11434 esdo->rela.count += additional_reloc_count;
11435 }
11436 }
11437 else
11438 {
11439 if (o->use_rela_p)
11440 esdo->rela.count += reloc_count;
11441 else
11442 esdo->rel.count += reloc_count;
11443 }
11444 }
11445
11446 if (o->reloc_count > 0)
11447 o->flags |= SEC_RELOC;
11448 else
11449 {
11450 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11451 set it (this is probably a bug) and if it is set
11452 assign_section_numbers will create a reloc section. */
11453 o->flags &=~ SEC_RELOC;
11454 }
11455
11456 /* If the SEC_ALLOC flag is not set, force the section VMA to
11457 zero. This is done in elf_fake_sections as well, but forcing
11458 the VMA to 0 here will ensure that relocs against these
11459 sections are handled correctly. */
11460 if ((o->flags & SEC_ALLOC) == 0
11461 && ! o->user_set_vma)
11462 o->vma = 0;
11463 }
11464
11465 if (! bfd_link_relocatable (info) && merged)
11466 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
11467
11468 /* Figure out the file positions for everything but the symbol table
11469 and the relocs. We set symcount to force assign_section_numbers
11470 to create a symbol table. */
11471 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
11472 BFD_ASSERT (! abfd->output_has_begun);
11473 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11474 goto error_return;
11475
11476 /* Set sizes, and assign file positions for reloc sections. */
11477 for (o = abfd->sections; o != NULL; o = o->next)
11478 {
11479 struct bfd_elf_section_data *esdo = elf_section_data (o);
11480 if ((o->flags & SEC_RELOC) != 0)
11481 {
11482 if (esdo->rel.hdr
11483 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
11484 goto error_return;
11485
11486 if (esdo->rela.hdr
11487 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
11488 goto error_return;
11489 }
11490
11491 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11492 to count upwards while actually outputting the relocations. */
11493 esdo->rel.count = 0;
11494 esdo->rela.count = 0;
11495
11496 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11497 {
11498 /* Cache the section contents so that they can be compressed
11499 later. Use bfd_malloc since it will be freed by
11500 bfd_compress_section_contents. */
11501 unsigned char *contents = esdo->this_hdr.contents;
11502 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11503 abort ();
11504 contents
11505 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11506 if (contents == NULL)
11507 goto error_return;
11508 esdo->this_hdr.contents = contents;
11509 }
11510 }
11511
11512 /* We have now assigned file positions for all the sections except
11513 .symtab, .strtab, and non-loaded reloc sections. We start the
11514 .symtab section at the current file position, and write directly
11515 to it. We build the .strtab section in memory. */
11516 bfd_get_symcount (abfd) = 0;
11517 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11518 /* sh_name is set in prep_headers. */
11519 symtab_hdr->sh_type = SHT_SYMTAB;
11520 /* sh_flags, sh_addr and sh_size all start off zero. */
11521 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11522 /* sh_link is set in assign_section_numbers. */
11523 /* sh_info is set below. */
11524 /* sh_offset is set just below. */
11525 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11526
11527 if (max_sym_count < 20)
11528 max_sym_count = 20;
11529 htab->strtabsize = max_sym_count;
11530 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11531 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
11532 if (htab->strtab == NULL)
11533 goto error_return;
11534 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11535 flinfo.symshndxbuf
11536 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11537 ? (Elf_External_Sym_Shndx *) -1 : NULL);
11538
11539 if (info->strip != strip_all || emit_relocs)
11540 {
11541 file_ptr off = elf_next_file_pos (abfd);
11542
11543 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11544
11545 /* Note that at this point elf_next_file_pos (abfd) is
11546 incorrect. We do not yet know the size of the .symtab section.
11547 We correct next_file_pos below, after we do know the size. */
11548
11549 /* Start writing out the symbol table. The first symbol is always a
11550 dummy symbol. */
11551 elfsym.st_value = 0;
11552 elfsym.st_size = 0;
11553 elfsym.st_info = 0;
11554 elfsym.st_other = 0;
11555 elfsym.st_shndx = SHN_UNDEF;
11556 elfsym.st_target_internal = 0;
11557 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11558 bfd_und_section_ptr, NULL) != 1)
11559 goto error_return;
11560
11561 /* Output a symbol for each section. We output these even if we are
11562 discarding local symbols, since they are used for relocs. These
11563 symbols have no names. We store the index of each one in the
11564 index field of the section, so that we can find it again when
11565 outputting relocs. */
11566
11567 elfsym.st_size = 0;
11568 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11569 elfsym.st_other = 0;
11570 elfsym.st_value = 0;
11571 elfsym.st_target_internal = 0;
11572 for (i = 1; i < elf_numsections (abfd); i++)
11573 {
11574 o = bfd_section_from_elf_index (abfd, i);
11575 if (o != NULL)
11576 {
11577 o->target_index = bfd_get_symcount (abfd);
11578 elfsym.st_shndx = i;
11579 if (!bfd_link_relocatable (info))
11580 elfsym.st_value = o->vma;
11581 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11582 NULL) != 1)
11583 goto error_return;
11584 }
11585 }
11586 }
11587
11588 /* Allocate some memory to hold information read in from the input
11589 files. */
11590 if (max_contents_size != 0)
11591 {
11592 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11593 if (flinfo.contents == NULL)
11594 goto error_return;
11595 }
11596
11597 if (max_external_reloc_size != 0)
11598 {
11599 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11600 if (flinfo.external_relocs == NULL)
11601 goto error_return;
11602 }
11603
11604 if (max_internal_reloc_count != 0)
11605 {
11606 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11607 amt *= sizeof (Elf_Internal_Rela);
11608 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11609 if (flinfo.internal_relocs == NULL)
11610 goto error_return;
11611 }
11612
11613 if (max_sym_count != 0)
11614 {
11615 amt = max_sym_count * bed->s->sizeof_sym;
11616 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11617 if (flinfo.external_syms == NULL)
11618 goto error_return;
11619
11620 amt = max_sym_count * sizeof (Elf_Internal_Sym);
11621 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11622 if (flinfo.internal_syms == NULL)
11623 goto error_return;
11624
11625 amt = max_sym_count * sizeof (long);
11626 flinfo.indices = (long int *) bfd_malloc (amt);
11627 if (flinfo.indices == NULL)
11628 goto error_return;
11629
11630 amt = max_sym_count * sizeof (asection *);
11631 flinfo.sections = (asection **) bfd_malloc (amt);
11632 if (flinfo.sections == NULL)
11633 goto error_return;
11634 }
11635
11636 if (max_sym_shndx_count != 0)
11637 {
11638 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
11639 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11640 if (flinfo.locsym_shndx == NULL)
11641 goto error_return;
11642 }
11643
11644 if (htab->tls_sec)
11645 {
11646 bfd_vma base, end = 0;
11647 asection *sec;
11648
11649 for (sec = htab->tls_sec;
11650 sec && (sec->flags & SEC_THREAD_LOCAL);
11651 sec = sec->next)
11652 {
11653 bfd_size_type size = sec->size;
11654
11655 if (size == 0
11656 && (sec->flags & SEC_HAS_CONTENTS) == 0)
11657 {
11658 struct bfd_link_order *ord = sec->map_tail.link_order;
11659
11660 if (ord != NULL)
11661 size = ord->offset + ord->size;
11662 }
11663 end = sec->vma + size;
11664 }
11665 base = htab->tls_sec->vma;
11666 /* Only align end of TLS section if static TLS doesn't have special
11667 alignment requirements. */
11668 if (bed->static_tls_alignment == 1)
11669 end = align_power (end, htab->tls_sec->alignment_power);
11670 htab->tls_size = end - base;
11671 }
11672
11673 /* Reorder SHF_LINK_ORDER sections. */
11674 for (o = abfd->sections; o != NULL; o = o->next)
11675 {
11676 if (!elf_fixup_link_order (abfd, o))
11677 return FALSE;
11678 }
11679
11680 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11681 return FALSE;
11682
11683 /* Since ELF permits relocations to be against local symbols, we
11684 must have the local symbols available when we do the relocations.
11685 Since we would rather only read the local symbols once, and we
11686 would rather not keep them in memory, we handle all the
11687 relocations for a single input file at the same time.
11688
11689 Unfortunately, there is no way to know the total number of local
11690 symbols until we have seen all of them, and the local symbol
11691 indices precede the global symbol indices. This means that when
11692 we are generating relocatable output, and we see a reloc against
11693 a global symbol, we can not know the symbol index until we have
11694 finished examining all the local symbols to see which ones we are
11695 going to output. To deal with this, we keep the relocations in
11696 memory, and don't output them until the end of the link. This is
11697 an unfortunate waste of memory, but I don't see a good way around
11698 it. Fortunately, it only happens when performing a relocatable
11699 link, which is not the common case. FIXME: If keep_memory is set
11700 we could write the relocs out and then read them again; I don't
11701 know how bad the memory loss will be. */
11702
11703 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11704 sub->output_has_begun = FALSE;
11705 for (o = abfd->sections; o != NULL; o = o->next)
11706 {
11707 for (p = o->map_head.link_order; p != NULL; p = p->next)
11708 {
11709 if (p->type == bfd_indirect_link_order
11710 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11711 == bfd_target_elf_flavour)
11712 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11713 {
11714 if (! sub->output_has_begun)
11715 {
11716 if (! elf_link_input_bfd (&flinfo, sub))
11717 goto error_return;
11718 sub->output_has_begun = TRUE;
11719 }
11720 }
11721 else if (p->type == bfd_section_reloc_link_order
11722 || p->type == bfd_symbol_reloc_link_order)
11723 {
11724 if (! elf_reloc_link_order (abfd, info, o, p))
11725 goto error_return;
11726 }
11727 else
11728 {
11729 if (! _bfd_default_link_order (abfd, info, o, p))
11730 {
11731 if (p->type == bfd_indirect_link_order
11732 && (bfd_get_flavour (sub)
11733 == bfd_target_elf_flavour)
11734 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11735 != bed->s->elfclass))
11736 {
11737 const char *iclass, *oclass;
11738
11739 switch (bed->s->elfclass)
11740 {
11741 case ELFCLASS64: oclass = "ELFCLASS64"; break;
11742 case ELFCLASS32: oclass = "ELFCLASS32"; break;
11743 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11744 default: abort ();
11745 }
11746
11747 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
11748 {
11749 case ELFCLASS64: iclass = "ELFCLASS64"; break;
11750 case ELFCLASS32: iclass = "ELFCLASS32"; break;
11751 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11752 default: abort ();
11753 }
11754
11755 bfd_set_error (bfd_error_wrong_format);
11756 _bfd_error_handler
11757 /* xgettext:c-format */
11758 (_("%B: file class %s incompatible with %s"),
11759 sub, iclass, oclass);
11760 }
11761
11762 goto error_return;
11763 }
11764 }
11765 }
11766 }
11767
11768 /* Free symbol buffer if needed. */
11769 if (!info->reduce_memory_overheads)
11770 {
11771 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11772 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11773 && elf_tdata (sub)->symbuf)
11774 {
11775 free (elf_tdata (sub)->symbuf);
11776 elf_tdata (sub)->symbuf = NULL;
11777 }
11778 }
11779
11780 /* Output any global symbols that got converted to local in a
11781 version script or due to symbol visibility. We do this in a
11782 separate step since ELF requires all local symbols to appear
11783 prior to any global symbols. FIXME: We should only do this if
11784 some global symbols were, in fact, converted to become local.
11785 FIXME: Will this work correctly with the Irix 5 linker? */
11786 eoinfo.failed = FALSE;
11787 eoinfo.flinfo = &flinfo;
11788 eoinfo.localsyms = TRUE;
11789 eoinfo.file_sym_done = FALSE;
11790 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11791 if (eoinfo.failed)
11792 return FALSE;
11793
11794 /* If backend needs to output some local symbols not present in the hash
11795 table, do it now. */
11796 if (bed->elf_backend_output_arch_local_syms
11797 && (info->strip != strip_all || emit_relocs))
11798 {
11799 typedef int (*out_sym_func)
11800 (void *, const char *, Elf_Internal_Sym *, asection *,
11801 struct elf_link_hash_entry *);
11802
11803 if (! ((*bed->elf_backend_output_arch_local_syms)
11804 (abfd, info, &flinfo,
11805 (out_sym_func) elf_link_output_symstrtab)))
11806 return FALSE;
11807 }
11808
11809 /* That wrote out all the local symbols. Finish up the symbol table
11810 with the global symbols. Even if we want to strip everything we
11811 can, we still need to deal with those global symbols that got
11812 converted to local in a version script. */
11813
11814 /* The sh_info field records the index of the first non local symbol. */
11815 symtab_hdr->sh_info = bfd_get_symcount (abfd);
11816
11817 if (dynamic
11818 && htab->dynsym != NULL
11819 && htab->dynsym->output_section != bfd_abs_section_ptr)
11820 {
11821 Elf_Internal_Sym sym;
11822 bfd_byte *dynsym = htab->dynsym->contents;
11823
11824 o = htab->dynsym->output_section;
11825 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
11826
11827 /* Write out the section symbols for the output sections. */
11828 if (bfd_link_pic (info)
11829 || htab->is_relocatable_executable)
11830 {
11831 asection *s;
11832
11833 sym.st_size = 0;
11834 sym.st_name = 0;
11835 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11836 sym.st_other = 0;
11837 sym.st_target_internal = 0;
11838
11839 for (s = abfd->sections; s != NULL; s = s->next)
11840 {
11841 int indx;
11842 bfd_byte *dest;
11843 long dynindx;
11844
11845 dynindx = elf_section_data (s)->dynindx;
11846 if (dynindx <= 0)
11847 continue;
11848 indx = elf_section_data (s)->this_idx;
11849 BFD_ASSERT (indx > 0);
11850 sym.st_shndx = indx;
11851 if (! check_dynsym (abfd, &sym))
11852 return FALSE;
11853 sym.st_value = s->vma;
11854 dest = dynsym + dynindx * bed->s->sizeof_sym;
11855 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11856 }
11857 }
11858
11859 /* Write out the local dynsyms. */
11860 if (htab->dynlocal)
11861 {
11862 struct elf_link_local_dynamic_entry *e;
11863 for (e = htab->dynlocal; e ; e = e->next)
11864 {
11865 asection *s;
11866 bfd_byte *dest;
11867
11868 /* Copy the internal symbol and turn off visibility.
11869 Note that we saved a word of storage and overwrote
11870 the original st_name with the dynstr_index. */
11871 sym = e->isym;
11872 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
11873
11874 s = bfd_section_from_elf_index (e->input_bfd,
11875 e->isym.st_shndx);
11876 if (s != NULL)
11877 {
11878 sym.st_shndx =
11879 elf_section_data (s->output_section)->this_idx;
11880 if (! check_dynsym (abfd, &sym))
11881 return FALSE;
11882 sym.st_value = (s->output_section->vma
11883 + s->output_offset
11884 + e->isym.st_value);
11885 }
11886
11887 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
11888 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11889 }
11890 }
11891 }
11892
11893 /* We get the global symbols from the hash table. */
11894 eoinfo.failed = FALSE;
11895 eoinfo.localsyms = FALSE;
11896 eoinfo.flinfo = &flinfo;
11897 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11898 if (eoinfo.failed)
11899 return FALSE;
11900
11901 /* If backend needs to output some symbols not present in the hash
11902 table, do it now. */
11903 if (bed->elf_backend_output_arch_syms
11904 && (info->strip != strip_all || emit_relocs))
11905 {
11906 typedef int (*out_sym_func)
11907 (void *, const char *, Elf_Internal_Sym *, asection *,
11908 struct elf_link_hash_entry *);
11909
11910 if (! ((*bed->elf_backend_output_arch_syms)
11911 (abfd, info, &flinfo,
11912 (out_sym_func) elf_link_output_symstrtab)))
11913 return FALSE;
11914 }
11915
11916 /* Finalize the .strtab section. */
11917 _bfd_elf_strtab_finalize (flinfo.symstrtab);
11918
11919 /* Swap out the .strtab section. */
11920 if (!elf_link_swap_symbols_out (&flinfo))
11921 return FALSE;
11922
11923 /* Now we know the size of the symtab section. */
11924 if (bfd_get_symcount (abfd) > 0)
11925 {
11926 /* Finish up and write out the symbol string table (.strtab)
11927 section. */
11928 Elf_Internal_Shdr *symstrtab_hdr;
11929 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
11930
11931 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
11932 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
11933 {
11934 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
11935 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
11936 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
11937 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
11938 symtab_shndx_hdr->sh_size = amt;
11939
11940 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
11941 off, TRUE);
11942
11943 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
11944 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
11945 return FALSE;
11946 }
11947
11948 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
11949 /* sh_name was set in prep_headers. */
11950 symstrtab_hdr->sh_type = SHT_STRTAB;
11951 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
11952 symstrtab_hdr->sh_addr = 0;
11953 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
11954 symstrtab_hdr->sh_entsize = 0;
11955 symstrtab_hdr->sh_link = 0;
11956 symstrtab_hdr->sh_info = 0;
11957 /* sh_offset is set just below. */
11958 symstrtab_hdr->sh_addralign = 1;
11959
11960 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
11961 off, TRUE);
11962 elf_next_file_pos (abfd) = off;
11963
11964 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
11965 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
11966 return FALSE;
11967 }
11968
11969 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
11970 {
11971 _bfd_error_handler (_("%B: failed to generate import library"),
11972 info->out_implib_bfd);
11973 return FALSE;
11974 }
11975
11976 /* Adjust the relocs to have the correct symbol indices. */
11977 for (o = abfd->sections; o != NULL; o = o->next)
11978 {
11979 struct bfd_elf_section_data *esdo = elf_section_data (o);
11980 bfd_boolean sort;
11981 if ((o->flags & SEC_RELOC) == 0)
11982 continue;
11983
11984 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
11985 if (esdo->rel.hdr != NULL
11986 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort))
11987 return FALSE;
11988 if (esdo->rela.hdr != NULL
11989 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort))
11990 return FALSE;
11991
11992 /* Set the reloc_count field to 0 to prevent write_relocs from
11993 trying to swap the relocs out itself. */
11994 o->reloc_count = 0;
11995 }
11996
11997 if (dynamic && info->combreloc && dynobj != NULL)
11998 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
11999
12000 /* If we are linking against a dynamic object, or generating a
12001 shared library, finish up the dynamic linking information. */
12002 if (dynamic)
12003 {
12004 bfd_byte *dyncon, *dynconend;
12005
12006 /* Fix up .dynamic entries. */
12007 o = bfd_get_linker_section (dynobj, ".dynamic");
12008 BFD_ASSERT (o != NULL);
12009
12010 dyncon = o->contents;
12011 dynconend = o->contents + o->size;
12012 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12013 {
12014 Elf_Internal_Dyn dyn;
12015 const char *name;
12016 unsigned int type;
12017
12018 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12019
12020 switch (dyn.d_tag)
12021 {
12022 default:
12023 continue;
12024 case DT_NULL:
12025 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12026 {
12027 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12028 {
12029 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12030 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12031 default: continue;
12032 }
12033 dyn.d_un.d_val = relativecount;
12034 relativecount = 0;
12035 break;
12036 }
12037 continue;
12038
12039 case DT_INIT:
12040 name = info->init_function;
12041 goto get_sym;
12042 case DT_FINI:
12043 name = info->fini_function;
12044 get_sym:
12045 {
12046 struct elf_link_hash_entry *h;
12047
12048 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
12049 if (h != NULL
12050 && (h->root.type == bfd_link_hash_defined
12051 || h->root.type == bfd_link_hash_defweak))
12052 {
12053 dyn.d_un.d_ptr = h->root.u.def.value;
12054 o = h->root.u.def.section;
12055 if (o->output_section != NULL)
12056 dyn.d_un.d_ptr += (o->output_section->vma
12057 + o->output_offset);
12058 else
12059 {
12060 /* The symbol is imported from another shared
12061 library and does not apply to this one. */
12062 dyn.d_un.d_ptr = 0;
12063 }
12064 break;
12065 }
12066 }
12067 continue;
12068
12069 case DT_PREINIT_ARRAYSZ:
12070 name = ".preinit_array";
12071 goto get_out_size;
12072 case DT_INIT_ARRAYSZ:
12073 name = ".init_array";
12074 goto get_out_size;
12075 case DT_FINI_ARRAYSZ:
12076 name = ".fini_array";
12077 get_out_size:
12078 o = bfd_get_section_by_name (abfd, name);
12079 if (o == NULL)
12080 {
12081 _bfd_error_handler
12082 (_("could not find section %s"), name);
12083 goto error_return;
12084 }
12085 if (o->size == 0)
12086 _bfd_error_handler
12087 (_("warning: %s section has zero size"), name);
12088 dyn.d_un.d_val = o->size;
12089 break;
12090
12091 case DT_PREINIT_ARRAY:
12092 name = ".preinit_array";
12093 goto get_out_vma;
12094 case DT_INIT_ARRAY:
12095 name = ".init_array";
12096 goto get_out_vma;
12097 case DT_FINI_ARRAY:
12098 name = ".fini_array";
12099 get_out_vma:
12100 o = bfd_get_section_by_name (abfd, name);
12101 goto do_vma;
12102
12103 case DT_HASH:
12104 name = ".hash";
12105 goto get_vma;
12106 case DT_GNU_HASH:
12107 name = ".gnu.hash";
12108 goto get_vma;
12109 case DT_STRTAB:
12110 name = ".dynstr";
12111 goto get_vma;
12112 case DT_SYMTAB:
12113 name = ".dynsym";
12114 goto get_vma;
12115 case DT_VERDEF:
12116 name = ".gnu.version_d";
12117 goto get_vma;
12118 case DT_VERNEED:
12119 name = ".gnu.version_r";
12120 goto get_vma;
12121 case DT_VERSYM:
12122 name = ".gnu.version";
12123 get_vma:
12124 o = bfd_get_linker_section (dynobj, name);
12125 do_vma:
12126 if (o == NULL)
12127 {
12128 _bfd_error_handler
12129 (_("could not find section %s"), name);
12130 goto error_return;
12131 }
12132 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12133 {
12134 _bfd_error_handler
12135 (_("warning: section '%s' is being made into a note"), name);
12136 bfd_set_error (bfd_error_nonrepresentable_section);
12137 goto error_return;
12138 }
12139 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
12140 break;
12141
12142 case DT_REL:
12143 case DT_RELA:
12144 case DT_RELSZ:
12145 case DT_RELASZ:
12146 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12147 type = SHT_REL;
12148 else
12149 type = SHT_RELA;
12150 dyn.d_un.d_val = 0;
12151 dyn.d_un.d_ptr = 0;
12152 for (i = 1; i < elf_numsections (abfd); i++)
12153 {
12154 Elf_Internal_Shdr *hdr;
12155
12156 hdr = elf_elfsections (abfd)[i];
12157 if (hdr->sh_type == type
12158 && (hdr->sh_flags & SHF_ALLOC) != 0)
12159 {
12160 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12161 dyn.d_un.d_val += hdr->sh_size;
12162 else
12163 {
12164 if (dyn.d_un.d_ptr == 0
12165 || hdr->sh_addr < dyn.d_un.d_ptr)
12166 dyn.d_un.d_ptr = hdr->sh_addr;
12167 }
12168 }
12169 }
12170 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12171 {
12172 /* Don't count procedure linkage table relocs in the
12173 overall reloc count. */
12174 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12175 dyn.d_un.d_val -= htab->srelplt->size;
12176 /* If .rela.plt is the first .rela section, exclude
12177 it from DT_RELA. */
12178 else if (dyn.d_un.d_ptr == (htab->srelplt->output_section->vma
12179 + htab->srelplt->output_offset))
12180 dyn.d_un.d_ptr += htab->srelplt->size;
12181 }
12182 break;
12183 }
12184 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12185 }
12186 }
12187
12188 /* If we have created any dynamic sections, then output them. */
12189 if (dynobj != NULL)
12190 {
12191 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12192 goto error_return;
12193
12194 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12195 if (((info->warn_shared_textrel && bfd_link_pic (info))
12196 || info->error_textrel)
12197 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
12198 {
12199 bfd_byte *dyncon, *dynconend;
12200
12201 dyncon = o->contents;
12202 dynconend = o->contents + o->size;
12203 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12204 {
12205 Elf_Internal_Dyn dyn;
12206
12207 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12208
12209 if (dyn.d_tag == DT_TEXTREL)
12210 {
12211 if (info->error_textrel)
12212 info->callbacks->einfo
12213 (_("%P%X: read-only segment has dynamic relocations.\n"));
12214 else
12215 info->callbacks->einfo
12216 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
12217 break;
12218 }
12219 }
12220 }
12221
12222 for (o = dynobj->sections; o != NULL; o = o->next)
12223 {
12224 if ((o->flags & SEC_HAS_CONTENTS) == 0
12225 || o->size == 0
12226 || o->output_section == bfd_abs_section_ptr)
12227 continue;
12228 if ((o->flags & SEC_LINKER_CREATED) == 0)
12229 {
12230 /* At this point, we are only interested in sections
12231 created by _bfd_elf_link_create_dynamic_sections. */
12232 continue;
12233 }
12234 if (htab->stab_info.stabstr == o)
12235 continue;
12236 if (htab->eh_info.hdr_sec == o)
12237 continue;
12238 if (strcmp (o->name, ".dynstr") != 0)
12239 {
12240 if (! bfd_set_section_contents (abfd, o->output_section,
12241 o->contents,
12242 (file_ptr) o->output_offset
12243 * bfd_octets_per_byte (abfd),
12244 o->size))
12245 goto error_return;
12246 }
12247 else
12248 {
12249 /* The contents of the .dynstr section are actually in a
12250 stringtab. */
12251 file_ptr off;
12252
12253 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12254 if (bfd_seek (abfd, off, SEEK_SET) != 0
12255 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
12256 goto error_return;
12257 }
12258 }
12259 }
12260
12261 if (bfd_link_relocatable (info))
12262 {
12263 bfd_boolean failed = FALSE;
12264
12265 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12266 if (failed)
12267 goto error_return;
12268 }
12269
12270 /* If we have optimized stabs strings, output them. */
12271 if (htab->stab_info.stabstr != NULL)
12272 {
12273 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
12274 goto error_return;
12275 }
12276
12277 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12278 goto error_return;
12279
12280 elf_final_link_free (abfd, &flinfo);
12281
12282 elf_linker (abfd) = TRUE;
12283
12284 if (attr_section)
12285 {
12286 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
12287 if (contents == NULL)
12288 return FALSE; /* Bail out and fail. */
12289 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12290 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12291 free (contents);
12292 }
12293
12294 return TRUE;
12295
12296 error_return:
12297 elf_final_link_free (abfd, &flinfo);
12298 return FALSE;
12299 }
12300 \f
12301 /* Initialize COOKIE for input bfd ABFD. */
12302
12303 static bfd_boolean
12304 init_reloc_cookie (struct elf_reloc_cookie *cookie,
12305 struct bfd_link_info *info, bfd *abfd)
12306 {
12307 Elf_Internal_Shdr *symtab_hdr;
12308 const struct elf_backend_data *bed;
12309
12310 bed = get_elf_backend_data (abfd);
12311 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12312
12313 cookie->abfd = abfd;
12314 cookie->sym_hashes = elf_sym_hashes (abfd);
12315 cookie->bad_symtab = elf_bad_symtab (abfd);
12316 if (cookie->bad_symtab)
12317 {
12318 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12319 cookie->extsymoff = 0;
12320 }
12321 else
12322 {
12323 cookie->locsymcount = symtab_hdr->sh_info;
12324 cookie->extsymoff = symtab_hdr->sh_info;
12325 }
12326
12327 if (bed->s->arch_size == 32)
12328 cookie->r_sym_shift = 8;
12329 else
12330 cookie->r_sym_shift = 32;
12331
12332 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12333 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12334 {
12335 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12336 cookie->locsymcount, 0,
12337 NULL, NULL, NULL);
12338 if (cookie->locsyms == NULL)
12339 {
12340 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12341 return FALSE;
12342 }
12343 if (info->keep_memory)
12344 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12345 }
12346 return TRUE;
12347 }
12348
12349 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12350
12351 static void
12352 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12353 {
12354 Elf_Internal_Shdr *symtab_hdr;
12355
12356 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12357 if (cookie->locsyms != NULL
12358 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12359 free (cookie->locsyms);
12360 }
12361
12362 /* Initialize the relocation information in COOKIE for input section SEC
12363 of input bfd ABFD. */
12364
12365 static bfd_boolean
12366 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12367 struct bfd_link_info *info, bfd *abfd,
12368 asection *sec)
12369 {
12370 const struct elf_backend_data *bed;
12371
12372 if (sec->reloc_count == 0)
12373 {
12374 cookie->rels = NULL;
12375 cookie->relend = NULL;
12376 }
12377 else
12378 {
12379 bed = get_elf_backend_data (abfd);
12380
12381 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12382 info->keep_memory);
12383 if (cookie->rels == NULL)
12384 return FALSE;
12385 cookie->rel = cookie->rels;
12386 cookie->relend = (cookie->rels
12387 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
12388 }
12389 cookie->rel = cookie->rels;
12390 return TRUE;
12391 }
12392
12393 /* Free the memory allocated by init_reloc_cookie_rels,
12394 if appropriate. */
12395
12396 static void
12397 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12398 asection *sec)
12399 {
12400 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12401 free (cookie->rels);
12402 }
12403
12404 /* Initialize the whole of COOKIE for input section SEC. */
12405
12406 static bfd_boolean
12407 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12408 struct bfd_link_info *info,
12409 asection *sec)
12410 {
12411 if (!init_reloc_cookie (cookie, info, sec->owner))
12412 goto error1;
12413 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12414 goto error2;
12415 return TRUE;
12416
12417 error2:
12418 fini_reloc_cookie (cookie, sec->owner);
12419 error1:
12420 return FALSE;
12421 }
12422
12423 /* Free the memory allocated by init_reloc_cookie_for_section,
12424 if appropriate. */
12425
12426 static void
12427 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12428 asection *sec)
12429 {
12430 fini_reloc_cookie_rels (cookie, sec);
12431 fini_reloc_cookie (cookie, sec->owner);
12432 }
12433 \f
12434 /* Garbage collect unused sections. */
12435
12436 /* Default gc_mark_hook. */
12437
12438 asection *
12439 _bfd_elf_gc_mark_hook (asection *sec,
12440 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12441 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12442 struct elf_link_hash_entry *h,
12443 Elf_Internal_Sym *sym)
12444 {
12445 if (h != NULL)
12446 {
12447 switch (h->root.type)
12448 {
12449 case bfd_link_hash_defined:
12450 case bfd_link_hash_defweak:
12451 return h->root.u.def.section;
12452
12453 case bfd_link_hash_common:
12454 return h->root.u.c.p->section;
12455
12456 default:
12457 break;
12458 }
12459 }
12460 else
12461 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12462
12463 return NULL;
12464 }
12465
12466 /* For undefined __start_<name> and __stop_<name> symbols, return the
12467 first input section matching <name>. Return NULL otherwise. */
12468
12469 asection *
12470 _bfd_elf_is_start_stop (const struct bfd_link_info *info,
12471 struct elf_link_hash_entry *h)
12472 {
12473 asection *s;
12474 const char *sec_name;
12475
12476 if (h->root.type != bfd_link_hash_undefined
12477 && h->root.type != bfd_link_hash_undefweak)
12478 return NULL;
12479
12480 s = h->root.u.undef.section;
12481 if (s != NULL)
12482 {
12483 if (s == (asection *) 0 - 1)
12484 return NULL;
12485 return s;
12486 }
12487
12488 sec_name = NULL;
12489 if (strncmp (h->root.root.string, "__start_", 8) == 0)
12490 sec_name = h->root.root.string + 8;
12491 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
12492 sec_name = h->root.root.string + 7;
12493
12494 if (sec_name != NULL && *sec_name != '\0')
12495 {
12496 bfd *i;
12497
12498 for (i = info->input_bfds; i != NULL; i = i->link.next)
12499 {
12500 s = bfd_get_section_by_name (i, sec_name);
12501 if (s != NULL)
12502 {
12503 h->root.u.undef.section = s;
12504 break;
12505 }
12506 }
12507 }
12508
12509 if (s == NULL)
12510 h->root.u.undef.section = (asection *) 0 - 1;
12511
12512 return s;
12513 }
12514
12515 /* COOKIE->rel describes a relocation against section SEC, which is
12516 a section we've decided to keep. Return the section that contains
12517 the relocation symbol, or NULL if no section contains it. */
12518
12519 asection *
12520 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12521 elf_gc_mark_hook_fn gc_mark_hook,
12522 struct elf_reloc_cookie *cookie,
12523 bfd_boolean *start_stop)
12524 {
12525 unsigned long r_symndx;
12526 struct elf_link_hash_entry *h;
12527
12528 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
12529 if (r_symndx == STN_UNDEF)
12530 return NULL;
12531
12532 if (r_symndx >= cookie->locsymcount
12533 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12534 {
12535 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
12536 if (h == NULL)
12537 {
12538 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12539 sec->owner);
12540 return NULL;
12541 }
12542 while (h->root.type == bfd_link_hash_indirect
12543 || h->root.type == bfd_link_hash_warning)
12544 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12545 h->mark = 1;
12546 /* If this symbol is weak and there is a non-weak definition, we
12547 keep the non-weak definition because many backends put
12548 dynamic reloc info on the non-weak definition for code
12549 handling copy relocs. */
12550 if (h->u.weakdef != NULL)
12551 h->u.weakdef->mark = 1;
12552
12553 if (start_stop != NULL)
12554 {
12555 /* To work around a glibc bug, mark all XXX input sections
12556 when there is an as yet undefined reference to __start_XXX
12557 or __stop_XXX symbols. The linker will later define such
12558 symbols for orphan input sections that have a name
12559 representable as a C identifier. */
12560 asection *s = _bfd_elf_is_start_stop (info, h);
12561
12562 if (s != NULL)
12563 {
12564 *start_stop = !s->gc_mark;
12565 return s;
12566 }
12567 }
12568
12569 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12570 }
12571
12572 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12573 &cookie->locsyms[r_symndx]);
12574 }
12575
12576 /* COOKIE->rel describes a relocation against section SEC, which is
12577 a section we've decided to keep. Mark the section that contains
12578 the relocation symbol. */
12579
12580 bfd_boolean
12581 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12582 asection *sec,
12583 elf_gc_mark_hook_fn gc_mark_hook,
12584 struct elf_reloc_cookie *cookie)
12585 {
12586 asection *rsec;
12587 bfd_boolean start_stop = FALSE;
12588
12589 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12590 while (rsec != NULL)
12591 {
12592 if (!rsec->gc_mark)
12593 {
12594 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12595 || (rsec->owner->flags & DYNAMIC) != 0)
12596 rsec->gc_mark = 1;
12597 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12598 return FALSE;
12599 }
12600 if (!start_stop)
12601 break;
12602 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
12603 }
12604 return TRUE;
12605 }
12606
12607 /* The mark phase of garbage collection. For a given section, mark
12608 it and any sections in this section's group, and all the sections
12609 which define symbols to which it refers. */
12610
12611 bfd_boolean
12612 _bfd_elf_gc_mark (struct bfd_link_info *info,
12613 asection *sec,
12614 elf_gc_mark_hook_fn gc_mark_hook)
12615 {
12616 bfd_boolean ret;
12617 asection *group_sec, *eh_frame;
12618
12619 sec->gc_mark = 1;
12620
12621 /* Mark all the sections in the group. */
12622 group_sec = elf_section_data (sec)->next_in_group;
12623 if (group_sec && !group_sec->gc_mark)
12624 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
12625 return FALSE;
12626
12627 /* Look through the section relocs. */
12628 ret = TRUE;
12629 eh_frame = elf_eh_frame_section (sec->owner);
12630 if ((sec->flags & SEC_RELOC) != 0
12631 && sec->reloc_count > 0
12632 && sec != eh_frame)
12633 {
12634 struct elf_reloc_cookie cookie;
12635
12636 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12637 ret = FALSE;
12638 else
12639 {
12640 for (; cookie.rel < cookie.relend; cookie.rel++)
12641 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
12642 {
12643 ret = FALSE;
12644 break;
12645 }
12646 fini_reloc_cookie_for_section (&cookie, sec);
12647 }
12648 }
12649
12650 if (ret && eh_frame && elf_fde_list (sec))
12651 {
12652 struct elf_reloc_cookie cookie;
12653
12654 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12655 ret = FALSE;
12656 else
12657 {
12658 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12659 gc_mark_hook, &cookie))
12660 ret = FALSE;
12661 fini_reloc_cookie_for_section (&cookie, eh_frame);
12662 }
12663 }
12664
12665 eh_frame = elf_section_eh_frame_entry (sec);
12666 if (ret && eh_frame && !eh_frame->gc_mark)
12667 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12668 ret = FALSE;
12669
12670 return ret;
12671 }
12672
12673 /* Scan and mark sections in a special or debug section group. */
12674
12675 static void
12676 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12677 {
12678 /* Point to first section of section group. */
12679 asection *ssec;
12680 /* Used to iterate the section group. */
12681 asection *msec;
12682
12683 bfd_boolean is_special_grp = TRUE;
12684 bfd_boolean is_debug_grp = TRUE;
12685
12686 /* First scan to see if group contains any section other than debug
12687 and special section. */
12688 ssec = msec = elf_next_in_group (grp);
12689 do
12690 {
12691 if ((msec->flags & SEC_DEBUGGING) == 0)
12692 is_debug_grp = FALSE;
12693
12694 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12695 is_special_grp = FALSE;
12696
12697 msec = elf_next_in_group (msec);
12698 }
12699 while (msec != ssec);
12700
12701 /* If this is a pure debug section group or pure special section group,
12702 keep all sections in this group. */
12703 if (is_debug_grp || is_special_grp)
12704 {
12705 do
12706 {
12707 msec->gc_mark = 1;
12708 msec = elf_next_in_group (msec);
12709 }
12710 while (msec != ssec);
12711 }
12712 }
12713
12714 /* Keep debug and special sections. */
12715
12716 bfd_boolean
12717 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12718 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12719 {
12720 bfd *ibfd;
12721
12722 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12723 {
12724 asection *isec;
12725 bfd_boolean some_kept;
12726 bfd_boolean debug_frag_seen;
12727
12728 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12729 continue;
12730
12731 /* Ensure all linker created sections are kept,
12732 see if any other section is already marked,
12733 and note if we have any fragmented debug sections. */
12734 debug_frag_seen = some_kept = FALSE;
12735 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12736 {
12737 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12738 isec->gc_mark = 1;
12739 else if (isec->gc_mark)
12740 some_kept = TRUE;
12741
12742 if (debug_frag_seen == FALSE
12743 && (isec->flags & SEC_DEBUGGING)
12744 && CONST_STRNEQ (isec->name, ".debug_line."))
12745 debug_frag_seen = TRUE;
12746 }
12747
12748 /* If no section in this file will be kept, then we can
12749 toss out the debug and special sections. */
12750 if (!some_kept)
12751 continue;
12752
12753 /* Keep debug and special sections like .comment when they are
12754 not part of a group. Also keep section groups that contain
12755 just debug sections or special sections. */
12756 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12757 {
12758 if ((isec->flags & SEC_GROUP) != 0)
12759 _bfd_elf_gc_mark_debug_special_section_group (isec);
12760 else if (((isec->flags & SEC_DEBUGGING) != 0
12761 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12762 && elf_next_in_group (isec) == NULL)
12763 isec->gc_mark = 1;
12764 }
12765
12766 if (! debug_frag_seen)
12767 continue;
12768
12769 /* Look for CODE sections which are going to be discarded,
12770 and find and discard any fragmented debug sections which
12771 are associated with that code section. */
12772 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12773 if ((isec->flags & SEC_CODE) != 0
12774 && isec->gc_mark == 0)
12775 {
12776 unsigned int ilen;
12777 asection *dsec;
12778
12779 ilen = strlen (isec->name);
12780
12781 /* Association is determined by the name of the debug section
12782 containing the name of the code section as a suffix. For
12783 example .debug_line.text.foo is a debug section associated
12784 with .text.foo. */
12785 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12786 {
12787 unsigned int dlen;
12788
12789 if (dsec->gc_mark == 0
12790 || (dsec->flags & SEC_DEBUGGING) == 0)
12791 continue;
12792
12793 dlen = strlen (dsec->name);
12794
12795 if (dlen > ilen
12796 && strncmp (dsec->name + (dlen - ilen),
12797 isec->name, ilen) == 0)
12798 {
12799 dsec->gc_mark = 0;
12800 }
12801 }
12802 }
12803 }
12804 return TRUE;
12805 }
12806
12807 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12808
12809 struct elf_gc_sweep_symbol_info
12810 {
12811 struct bfd_link_info *info;
12812 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
12813 bfd_boolean);
12814 };
12815
12816 static bfd_boolean
12817 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
12818 {
12819 if (!h->mark
12820 && (((h->root.type == bfd_link_hash_defined
12821 || h->root.type == bfd_link_hash_defweak)
12822 && !((h->def_regular || ELF_COMMON_DEF_P (h))
12823 && h->root.u.def.section->gc_mark))
12824 || h->root.type == bfd_link_hash_undefined
12825 || h->root.type == bfd_link_hash_undefweak))
12826 {
12827 struct elf_gc_sweep_symbol_info *inf;
12828
12829 inf = (struct elf_gc_sweep_symbol_info *) data;
12830 (*inf->hide_symbol) (inf->info, h, TRUE);
12831 h->def_regular = 0;
12832 h->ref_regular = 0;
12833 h->ref_regular_nonweak = 0;
12834 }
12835
12836 return TRUE;
12837 }
12838
12839 /* The sweep phase of garbage collection. Remove all garbage sections. */
12840
12841 typedef bfd_boolean (*gc_sweep_hook_fn)
12842 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
12843
12844 static bfd_boolean
12845 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
12846 {
12847 bfd *sub;
12848 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12849 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
12850 unsigned long section_sym_count;
12851 struct elf_gc_sweep_symbol_info sweep_info;
12852
12853 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12854 {
12855 asection *o;
12856
12857 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12858 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
12859 continue;
12860
12861 for (o = sub->sections; o != NULL; o = o->next)
12862 {
12863 /* When any section in a section group is kept, we keep all
12864 sections in the section group. If the first member of
12865 the section group is excluded, we will also exclude the
12866 group section. */
12867 if (o->flags & SEC_GROUP)
12868 {
12869 asection *first = elf_next_in_group (o);
12870 o->gc_mark = first->gc_mark;
12871 }
12872
12873 if (o->gc_mark)
12874 continue;
12875
12876 /* Skip sweeping sections already excluded. */
12877 if (o->flags & SEC_EXCLUDE)
12878 continue;
12879
12880 /* Since this is early in the link process, it is simple
12881 to remove a section from the output. */
12882 o->flags |= SEC_EXCLUDE;
12883
12884 if (info->print_gc_sections && o->size != 0)
12885 /* xgettext:c-format */
12886 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
12887
12888 /* But we also have to update some of the relocation
12889 info we collected before. */
12890 if (gc_sweep_hook
12891 && (o->flags & SEC_RELOC) != 0
12892 && o->reloc_count != 0
12893 && !((info->strip == strip_all || info->strip == strip_debugger)
12894 && (o->flags & SEC_DEBUGGING) != 0)
12895 && !bfd_is_abs_section (o->output_section))
12896 {
12897 Elf_Internal_Rela *internal_relocs;
12898 bfd_boolean r;
12899
12900 internal_relocs
12901 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
12902 info->keep_memory);
12903 if (internal_relocs == NULL)
12904 return FALSE;
12905
12906 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
12907
12908 if (elf_section_data (o)->relocs != internal_relocs)
12909 free (internal_relocs);
12910
12911 if (!r)
12912 return FALSE;
12913 }
12914 }
12915 }
12916
12917 /* Remove the symbols that were in the swept sections from the dynamic
12918 symbol table. GCFIXME: Anyone know how to get them out of the
12919 static symbol table as well? */
12920 sweep_info.info = info;
12921 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
12922 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
12923 &sweep_info);
12924
12925 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
12926 return TRUE;
12927 }
12928
12929 /* Propagate collected vtable information. This is called through
12930 elf_link_hash_traverse. */
12931
12932 static bfd_boolean
12933 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
12934 {
12935 /* Those that are not vtables. */
12936 if (h->vtable == NULL || h->vtable->parent == NULL)
12937 return TRUE;
12938
12939 /* Those vtables that do not have parents, we cannot merge. */
12940 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
12941 return TRUE;
12942
12943 /* If we've already been done, exit. */
12944 if (h->vtable->used && h->vtable->used[-1])
12945 return TRUE;
12946
12947 /* Make sure the parent's table is up to date. */
12948 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
12949
12950 if (h->vtable->used == NULL)
12951 {
12952 /* None of this table's entries were referenced. Re-use the
12953 parent's table. */
12954 h->vtable->used = h->vtable->parent->vtable->used;
12955 h->vtable->size = h->vtable->parent->vtable->size;
12956 }
12957 else
12958 {
12959 size_t n;
12960 bfd_boolean *cu, *pu;
12961
12962 /* Or the parent's entries into ours. */
12963 cu = h->vtable->used;
12964 cu[-1] = TRUE;
12965 pu = h->vtable->parent->vtable->used;
12966 if (pu != NULL)
12967 {
12968 const struct elf_backend_data *bed;
12969 unsigned int log_file_align;
12970
12971 bed = get_elf_backend_data (h->root.u.def.section->owner);
12972 log_file_align = bed->s->log_file_align;
12973 n = h->vtable->parent->vtable->size >> log_file_align;
12974 while (n--)
12975 {
12976 if (*pu)
12977 *cu = TRUE;
12978 pu++;
12979 cu++;
12980 }
12981 }
12982 }
12983
12984 return TRUE;
12985 }
12986
12987 static bfd_boolean
12988 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
12989 {
12990 asection *sec;
12991 bfd_vma hstart, hend;
12992 Elf_Internal_Rela *relstart, *relend, *rel;
12993 const struct elf_backend_data *bed;
12994 unsigned int log_file_align;
12995
12996 /* Take care of both those symbols that do not describe vtables as
12997 well as those that are not loaded. */
12998 if (h->vtable == NULL || h->vtable->parent == NULL)
12999 return TRUE;
13000
13001 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13002 || h->root.type == bfd_link_hash_defweak);
13003
13004 sec = h->root.u.def.section;
13005 hstart = h->root.u.def.value;
13006 hend = hstart + h->size;
13007
13008 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13009 if (!relstart)
13010 return *(bfd_boolean *) okp = FALSE;
13011 bed = get_elf_backend_data (sec->owner);
13012 log_file_align = bed->s->log_file_align;
13013
13014 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
13015
13016 for (rel = relstart; rel < relend; ++rel)
13017 if (rel->r_offset >= hstart && rel->r_offset < hend)
13018 {
13019 /* If the entry is in use, do nothing. */
13020 if (h->vtable->used
13021 && (rel->r_offset - hstart) < h->vtable->size)
13022 {
13023 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13024 if (h->vtable->used[entry])
13025 continue;
13026 }
13027 /* Otherwise, kill it. */
13028 rel->r_offset = rel->r_info = rel->r_addend = 0;
13029 }
13030
13031 return TRUE;
13032 }
13033
13034 /* Mark sections containing dynamically referenced symbols. When
13035 building shared libraries, we must assume that any visible symbol is
13036 referenced. */
13037
13038 bfd_boolean
13039 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13040 {
13041 struct bfd_link_info *info = (struct bfd_link_info *) inf;
13042 struct bfd_elf_dynamic_list *d = info->dynamic_list;
13043
13044 if ((h->root.type == bfd_link_hash_defined
13045 || h->root.type == bfd_link_hash_defweak)
13046 && (h->ref_dynamic
13047 || ((h->def_regular || ELF_COMMON_DEF_P (h))
13048 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
13049 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
13050 && (!bfd_link_executable (info)
13051 || info->export_dynamic
13052 || (h->dynamic
13053 && d != NULL
13054 && (*d->match) (&d->head, NULL, h->root.root.string)))
13055 && (h->versioned >= versioned
13056 || !bfd_hide_sym_by_version (info->version_info,
13057 h->root.root.string)))))
13058 h->root.u.def.section->flags |= SEC_KEEP;
13059
13060 return TRUE;
13061 }
13062
13063 /* Keep all sections containing symbols undefined on the command-line,
13064 and the section containing the entry symbol. */
13065
13066 void
13067 _bfd_elf_gc_keep (struct bfd_link_info *info)
13068 {
13069 struct bfd_sym_chain *sym;
13070
13071 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13072 {
13073 struct elf_link_hash_entry *h;
13074
13075 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13076 FALSE, FALSE, FALSE);
13077
13078 if (h != NULL
13079 && (h->root.type == bfd_link_hash_defined
13080 || h->root.type == bfd_link_hash_defweak)
13081 && !bfd_is_abs_section (h->root.u.def.section)
13082 && !bfd_is_und_section (h->root.u.def.section))
13083 h->root.u.def.section->flags |= SEC_KEEP;
13084 }
13085 }
13086
13087 bfd_boolean
13088 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13089 struct bfd_link_info *info)
13090 {
13091 bfd *ibfd = info->input_bfds;
13092
13093 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13094 {
13095 asection *sec;
13096 struct elf_reloc_cookie cookie;
13097
13098 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13099 continue;
13100
13101 if (!init_reloc_cookie (&cookie, info, ibfd))
13102 return FALSE;
13103
13104 for (sec = ibfd->sections; sec; sec = sec->next)
13105 {
13106 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13107 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13108 {
13109 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13110 fini_reloc_cookie_rels (&cookie, sec);
13111 }
13112 }
13113 }
13114 return TRUE;
13115 }
13116
13117 /* Do mark and sweep of unused sections. */
13118
13119 bfd_boolean
13120 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13121 {
13122 bfd_boolean ok = TRUE;
13123 bfd *sub;
13124 elf_gc_mark_hook_fn gc_mark_hook;
13125 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13126 struct elf_link_hash_table *htab;
13127
13128 if (!bed->can_gc_sections
13129 || !is_elf_hash_table (info->hash))
13130 {
13131 _bfd_error_handler(_("Warning: gc-sections option ignored"));
13132 return TRUE;
13133 }
13134
13135 bed->gc_keep (info);
13136 htab = elf_hash_table (info);
13137
13138 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13139 at the .eh_frame section if we can mark the FDEs individually. */
13140 for (sub = info->input_bfds;
13141 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13142 sub = sub->link.next)
13143 {
13144 asection *sec;
13145 struct elf_reloc_cookie cookie;
13146
13147 sec = bfd_get_section_by_name (sub, ".eh_frame");
13148 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
13149 {
13150 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
13151 if (elf_section_data (sec)->sec_info
13152 && (sec->flags & SEC_LINKER_CREATED) == 0)
13153 elf_eh_frame_section (sub) = sec;
13154 fini_reloc_cookie_for_section (&cookie, sec);
13155 sec = bfd_get_next_section_by_name (NULL, sec);
13156 }
13157 }
13158
13159 /* Apply transitive closure to the vtable entry usage info. */
13160 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13161 if (!ok)
13162 return FALSE;
13163
13164 /* Kill the vtable relocations that were not used. */
13165 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13166 if (!ok)
13167 return FALSE;
13168
13169 /* Mark dynamically referenced symbols. */
13170 if (htab->dynamic_sections_created)
13171 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13172
13173 /* Grovel through relocs to find out who stays ... */
13174 gc_mark_hook = bed->gc_mark_hook;
13175 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13176 {
13177 asection *o;
13178
13179 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13180 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13181 continue;
13182
13183 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13184 Also treat note sections as a root, if the section is not part
13185 of a group. */
13186 for (o = sub->sections; o != NULL; o = o->next)
13187 if (!o->gc_mark
13188 && (o->flags & SEC_EXCLUDE) == 0
13189 && ((o->flags & SEC_KEEP) != 0
13190 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13191 && elf_next_in_group (o) == NULL )))
13192 {
13193 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13194 return FALSE;
13195 }
13196 }
13197
13198 /* Allow the backend to mark additional target specific sections. */
13199 bed->gc_mark_extra_sections (info, gc_mark_hook);
13200
13201 /* ... and mark SEC_EXCLUDE for those that go. */
13202 return elf_gc_sweep (abfd, info);
13203 }
13204 \f
13205 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13206
13207 bfd_boolean
13208 bfd_elf_gc_record_vtinherit (bfd *abfd,
13209 asection *sec,
13210 struct elf_link_hash_entry *h,
13211 bfd_vma offset)
13212 {
13213 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13214 struct elf_link_hash_entry **search, *child;
13215 size_t extsymcount;
13216 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13217
13218 /* The sh_info field of the symtab header tells us where the
13219 external symbols start. We don't care about the local symbols at
13220 this point. */
13221 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13222 if (!elf_bad_symtab (abfd))
13223 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13224
13225 sym_hashes = elf_sym_hashes (abfd);
13226 sym_hashes_end = sym_hashes + extsymcount;
13227
13228 /* Hunt down the child symbol, which is in this section at the same
13229 offset as the relocation. */
13230 for (search = sym_hashes; search != sym_hashes_end; ++search)
13231 {
13232 if ((child = *search) != NULL
13233 && (child->root.type == bfd_link_hash_defined
13234 || child->root.type == bfd_link_hash_defweak)
13235 && child->root.u.def.section == sec
13236 && child->root.u.def.value == offset)
13237 goto win;
13238 }
13239
13240 /* xgettext:c-format */
13241 _bfd_error_handler (_("%B: %A+%lu: No symbol found for INHERIT"),
13242 abfd, sec, (unsigned long) offset);
13243 bfd_set_error (bfd_error_invalid_operation);
13244 return FALSE;
13245
13246 win:
13247 if (!child->vtable)
13248 {
13249 child->vtable = ((struct elf_link_virtual_table_entry *)
13250 bfd_zalloc (abfd, sizeof (*child->vtable)));
13251 if (!child->vtable)
13252 return FALSE;
13253 }
13254 if (!h)
13255 {
13256 /* This *should* only be the absolute section. It could potentially
13257 be that someone has defined a non-global vtable though, which
13258 would be bad. It isn't worth paging in the local symbols to be
13259 sure though; that case should simply be handled by the assembler. */
13260
13261 child->vtable->parent = (struct elf_link_hash_entry *) -1;
13262 }
13263 else
13264 child->vtable->parent = h;
13265
13266 return TRUE;
13267 }
13268
13269 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13270
13271 bfd_boolean
13272 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13273 asection *sec ATTRIBUTE_UNUSED,
13274 struct elf_link_hash_entry *h,
13275 bfd_vma addend)
13276 {
13277 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13278 unsigned int log_file_align = bed->s->log_file_align;
13279
13280 if (!h->vtable)
13281 {
13282 h->vtable = ((struct elf_link_virtual_table_entry *)
13283 bfd_zalloc (abfd, sizeof (*h->vtable)));
13284 if (!h->vtable)
13285 return FALSE;
13286 }
13287
13288 if (addend >= h->vtable->size)
13289 {
13290 size_t size, bytes, file_align;
13291 bfd_boolean *ptr = h->vtable->used;
13292
13293 /* While the symbol is undefined, we have to be prepared to handle
13294 a zero size. */
13295 file_align = 1 << log_file_align;
13296 if (h->root.type == bfd_link_hash_undefined)
13297 size = addend + file_align;
13298 else
13299 {
13300 size = h->size;
13301 if (addend >= size)
13302 {
13303 /* Oops! We've got a reference past the defined end of
13304 the table. This is probably a bug -- shall we warn? */
13305 size = addend + file_align;
13306 }
13307 }
13308 size = (size + file_align - 1) & -file_align;
13309
13310 /* Allocate one extra entry for use as a "done" flag for the
13311 consolidation pass. */
13312 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13313
13314 if (ptr)
13315 {
13316 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
13317
13318 if (ptr != NULL)
13319 {
13320 size_t oldbytes;
13321
13322 oldbytes = (((h->vtable->size >> log_file_align) + 1)
13323 * sizeof (bfd_boolean));
13324 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13325 }
13326 }
13327 else
13328 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13329
13330 if (ptr == NULL)
13331 return FALSE;
13332
13333 /* And arrange for that done flag to be at index -1. */
13334 h->vtable->used = ptr + 1;
13335 h->vtable->size = size;
13336 }
13337
13338 h->vtable->used[addend >> log_file_align] = TRUE;
13339
13340 return TRUE;
13341 }
13342
13343 /* Map an ELF section header flag to its corresponding string. */
13344 typedef struct
13345 {
13346 char *flag_name;
13347 flagword flag_value;
13348 } elf_flags_to_name_table;
13349
13350 static elf_flags_to_name_table elf_flags_to_names [] =
13351 {
13352 { "SHF_WRITE", SHF_WRITE },
13353 { "SHF_ALLOC", SHF_ALLOC },
13354 { "SHF_EXECINSTR", SHF_EXECINSTR },
13355 { "SHF_MERGE", SHF_MERGE },
13356 { "SHF_STRINGS", SHF_STRINGS },
13357 { "SHF_INFO_LINK", SHF_INFO_LINK},
13358 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13359 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13360 { "SHF_GROUP", SHF_GROUP },
13361 { "SHF_TLS", SHF_TLS },
13362 { "SHF_MASKOS", SHF_MASKOS },
13363 { "SHF_EXCLUDE", SHF_EXCLUDE },
13364 };
13365
13366 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13367 bfd_boolean
13368 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13369 struct flag_info *flaginfo,
13370 asection *section)
13371 {
13372 const bfd_vma sh_flags = elf_section_flags (section);
13373
13374 if (!flaginfo->flags_initialized)
13375 {
13376 bfd *obfd = info->output_bfd;
13377 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13378 struct flag_info_list *tf = flaginfo->flag_list;
13379 int with_hex = 0;
13380 int without_hex = 0;
13381
13382 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13383 {
13384 unsigned i;
13385 flagword (*lookup) (char *);
13386
13387 lookup = bed->elf_backend_lookup_section_flags_hook;
13388 if (lookup != NULL)
13389 {
13390 flagword hexval = (*lookup) ((char *) tf->name);
13391
13392 if (hexval != 0)
13393 {
13394 if (tf->with == with_flags)
13395 with_hex |= hexval;
13396 else if (tf->with == without_flags)
13397 without_hex |= hexval;
13398 tf->valid = TRUE;
13399 continue;
13400 }
13401 }
13402 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13403 {
13404 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13405 {
13406 if (tf->with == with_flags)
13407 with_hex |= elf_flags_to_names[i].flag_value;
13408 else if (tf->with == without_flags)
13409 without_hex |= elf_flags_to_names[i].flag_value;
13410 tf->valid = TRUE;
13411 break;
13412 }
13413 }
13414 if (!tf->valid)
13415 {
13416 info->callbacks->einfo
13417 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13418 return FALSE;
13419 }
13420 }
13421 flaginfo->flags_initialized = TRUE;
13422 flaginfo->only_with_flags |= with_hex;
13423 flaginfo->not_with_flags |= without_hex;
13424 }
13425
13426 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13427 return FALSE;
13428
13429 if ((flaginfo->not_with_flags & sh_flags) != 0)
13430 return FALSE;
13431
13432 return TRUE;
13433 }
13434
13435 struct alloc_got_off_arg {
13436 bfd_vma gotoff;
13437 struct bfd_link_info *info;
13438 };
13439
13440 /* We need a special top-level link routine to convert got reference counts
13441 to real got offsets. */
13442
13443 static bfd_boolean
13444 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13445 {
13446 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13447 bfd *obfd = gofarg->info->output_bfd;
13448 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13449
13450 if (h->got.refcount > 0)
13451 {
13452 h->got.offset = gofarg->gotoff;
13453 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13454 }
13455 else
13456 h->got.offset = (bfd_vma) -1;
13457
13458 return TRUE;
13459 }
13460
13461 /* And an accompanying bit to work out final got entry offsets once
13462 we're done. Should be called from final_link. */
13463
13464 bfd_boolean
13465 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13466 struct bfd_link_info *info)
13467 {
13468 bfd *i;
13469 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13470 bfd_vma gotoff;
13471 struct alloc_got_off_arg gofarg;
13472
13473 BFD_ASSERT (abfd == info->output_bfd);
13474
13475 if (! is_elf_hash_table (info->hash))
13476 return FALSE;
13477
13478 /* The GOT offset is relative to the .got section, but the GOT header is
13479 put into the .got.plt section, if the backend uses it. */
13480 if (bed->want_got_plt)
13481 gotoff = 0;
13482 else
13483 gotoff = bed->got_header_size;
13484
13485 /* Do the local .got entries first. */
13486 for (i = info->input_bfds; i; i = i->link.next)
13487 {
13488 bfd_signed_vma *local_got;
13489 size_t j, locsymcount;
13490 Elf_Internal_Shdr *symtab_hdr;
13491
13492 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13493 continue;
13494
13495 local_got = elf_local_got_refcounts (i);
13496 if (!local_got)
13497 continue;
13498
13499 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13500 if (elf_bad_symtab (i))
13501 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13502 else
13503 locsymcount = symtab_hdr->sh_info;
13504
13505 for (j = 0; j < locsymcount; ++j)
13506 {
13507 if (local_got[j] > 0)
13508 {
13509 local_got[j] = gotoff;
13510 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13511 }
13512 else
13513 local_got[j] = (bfd_vma) -1;
13514 }
13515 }
13516
13517 /* Then the global .got entries. .plt refcounts are handled by
13518 adjust_dynamic_symbol */
13519 gofarg.gotoff = gotoff;
13520 gofarg.info = info;
13521 elf_link_hash_traverse (elf_hash_table (info),
13522 elf_gc_allocate_got_offsets,
13523 &gofarg);
13524 return TRUE;
13525 }
13526
13527 /* Many folk need no more in the way of final link than this, once
13528 got entry reference counting is enabled. */
13529
13530 bfd_boolean
13531 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13532 {
13533 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13534 return FALSE;
13535
13536 /* Invoke the regular ELF backend linker to do all the work. */
13537 return bfd_elf_final_link (abfd, info);
13538 }
13539
13540 bfd_boolean
13541 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13542 {
13543 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13544
13545 if (rcookie->bad_symtab)
13546 rcookie->rel = rcookie->rels;
13547
13548 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13549 {
13550 unsigned long r_symndx;
13551
13552 if (! rcookie->bad_symtab)
13553 if (rcookie->rel->r_offset > offset)
13554 return FALSE;
13555 if (rcookie->rel->r_offset != offset)
13556 continue;
13557
13558 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
13559 if (r_symndx == STN_UNDEF)
13560 return TRUE;
13561
13562 if (r_symndx >= rcookie->locsymcount
13563 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13564 {
13565 struct elf_link_hash_entry *h;
13566
13567 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13568
13569 while (h->root.type == bfd_link_hash_indirect
13570 || h->root.type == bfd_link_hash_warning)
13571 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13572
13573 if ((h->root.type == bfd_link_hash_defined
13574 || h->root.type == bfd_link_hash_defweak)
13575 && (h->root.u.def.section->owner != rcookie->abfd
13576 || h->root.u.def.section->kept_section != NULL
13577 || discarded_section (h->root.u.def.section)))
13578 return TRUE;
13579 }
13580 else
13581 {
13582 /* It's not a relocation against a global symbol,
13583 but it could be a relocation against a local
13584 symbol for a discarded section. */
13585 asection *isec;
13586 Elf_Internal_Sym *isym;
13587
13588 /* Need to: get the symbol; get the section. */
13589 isym = &rcookie->locsyms[r_symndx];
13590 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
13591 if (isec != NULL
13592 && (isec->kept_section != NULL
13593 || discarded_section (isec)))
13594 return TRUE;
13595 }
13596 return FALSE;
13597 }
13598 return FALSE;
13599 }
13600
13601 /* Discard unneeded references to discarded sections.
13602 Returns -1 on error, 1 if any section's size was changed, 0 if
13603 nothing changed. This function assumes that the relocations are in
13604 sorted order, which is true for all known assemblers. */
13605
13606 int
13607 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13608 {
13609 struct elf_reloc_cookie cookie;
13610 asection *o;
13611 bfd *abfd;
13612 int changed = 0;
13613
13614 if (info->traditional_format
13615 || !is_elf_hash_table (info->hash))
13616 return 0;
13617
13618 o = bfd_get_section_by_name (output_bfd, ".stab");
13619 if (o != NULL)
13620 {
13621 asection *i;
13622
13623 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13624 {
13625 if (i->size == 0
13626 || i->reloc_count == 0
13627 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13628 continue;
13629
13630 abfd = i->owner;
13631 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13632 continue;
13633
13634 if (!init_reloc_cookie_for_section (&cookie, info, i))
13635 return -1;
13636
13637 if (_bfd_discard_section_stabs (abfd, i,
13638 elf_section_data (i)->sec_info,
13639 bfd_elf_reloc_symbol_deleted_p,
13640 &cookie))
13641 changed = 1;
13642
13643 fini_reloc_cookie_for_section (&cookie, i);
13644 }
13645 }
13646
13647 o = NULL;
13648 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13649 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
13650 if (o != NULL)
13651 {
13652 asection *i;
13653
13654 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13655 {
13656 if (i->size == 0)
13657 continue;
13658
13659 abfd = i->owner;
13660 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13661 continue;
13662
13663 if (!init_reloc_cookie_for_section (&cookie, info, i))
13664 return -1;
13665
13666 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13667 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
13668 bfd_elf_reloc_symbol_deleted_p,
13669 &cookie))
13670 changed = 1;
13671
13672 fini_reloc_cookie_for_section (&cookie, i);
13673 }
13674 }
13675
13676 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13677 {
13678 const struct elf_backend_data *bed;
13679
13680 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13681 continue;
13682
13683 bed = get_elf_backend_data (abfd);
13684
13685 if (bed->elf_backend_discard_info != NULL)
13686 {
13687 if (!init_reloc_cookie (&cookie, info, abfd))
13688 return -1;
13689
13690 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
13691 changed = 1;
13692
13693 fini_reloc_cookie (&cookie, abfd);
13694 }
13695 }
13696
13697 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13698 _bfd_elf_end_eh_frame_parsing (info);
13699
13700 if (info->eh_frame_hdr_type
13701 && !bfd_link_relocatable (info)
13702 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
13703 changed = 1;
13704
13705 return changed;
13706 }
13707
13708 bfd_boolean
13709 _bfd_elf_section_already_linked (bfd *abfd,
13710 asection *sec,
13711 struct bfd_link_info *info)
13712 {
13713 flagword flags;
13714 const char *name, *key;
13715 struct bfd_section_already_linked *l;
13716 struct bfd_section_already_linked_hash_entry *already_linked_list;
13717
13718 if (sec->output_section == bfd_abs_section_ptr)
13719 return FALSE;
13720
13721 flags = sec->flags;
13722
13723 /* Return if it isn't a linkonce section. A comdat group section
13724 also has SEC_LINK_ONCE set. */
13725 if ((flags & SEC_LINK_ONCE) == 0)
13726 return FALSE;
13727
13728 /* Don't put group member sections on our list of already linked
13729 sections. They are handled as a group via their group section. */
13730 if (elf_sec_group (sec) != NULL)
13731 return FALSE;
13732
13733 /* For a SHT_GROUP section, use the group signature as the key. */
13734 name = sec->name;
13735 if ((flags & SEC_GROUP) != 0
13736 && elf_next_in_group (sec) != NULL
13737 && elf_group_name (elf_next_in_group (sec)) != NULL)
13738 key = elf_group_name (elf_next_in_group (sec));
13739 else
13740 {
13741 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
13742 if (CONST_STRNEQ (name, ".gnu.linkonce.")
13743 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13744 key++;
13745 else
13746 /* Must be a user linkonce section that doesn't follow gcc's
13747 naming convention. In this case we won't be matching
13748 single member groups. */
13749 key = name;
13750 }
13751
13752 already_linked_list = bfd_section_already_linked_table_lookup (key);
13753
13754 for (l = already_linked_list->entry; l != NULL; l = l->next)
13755 {
13756 /* We may have 2 different types of sections on the list: group
13757 sections with a signature of <key> (<key> is some string),
13758 and linkonce sections named .gnu.linkonce.<type>.<key>.
13759 Match like sections. LTO plugin sections are an exception.
13760 They are always named .gnu.linkonce.t.<key> and match either
13761 type of section. */
13762 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13763 && ((flags & SEC_GROUP) != 0
13764 || strcmp (name, l->sec->name) == 0))
13765 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
13766 {
13767 /* The section has already been linked. See if we should
13768 issue a warning. */
13769 if (!_bfd_handle_already_linked (sec, l, info))
13770 return FALSE;
13771
13772 if (flags & SEC_GROUP)
13773 {
13774 asection *first = elf_next_in_group (sec);
13775 asection *s = first;
13776
13777 while (s != NULL)
13778 {
13779 s->output_section = bfd_abs_section_ptr;
13780 /* Record which group discards it. */
13781 s->kept_section = l->sec;
13782 s = elf_next_in_group (s);
13783 /* These lists are circular. */
13784 if (s == first)
13785 break;
13786 }
13787 }
13788
13789 return TRUE;
13790 }
13791 }
13792
13793 /* A single member comdat group section may be discarded by a
13794 linkonce section and vice versa. */
13795 if ((flags & SEC_GROUP) != 0)
13796 {
13797 asection *first = elf_next_in_group (sec);
13798
13799 if (first != NULL && elf_next_in_group (first) == first)
13800 /* Check this single member group against linkonce sections. */
13801 for (l = already_linked_list->entry; l != NULL; l = l->next)
13802 if ((l->sec->flags & SEC_GROUP) == 0
13803 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13804 {
13805 first->output_section = bfd_abs_section_ptr;
13806 first->kept_section = l->sec;
13807 sec->output_section = bfd_abs_section_ptr;
13808 break;
13809 }
13810 }
13811 else
13812 /* Check this linkonce section against single member groups. */
13813 for (l = already_linked_list->entry; l != NULL; l = l->next)
13814 if (l->sec->flags & SEC_GROUP)
13815 {
13816 asection *first = elf_next_in_group (l->sec);
13817
13818 if (first != NULL
13819 && elf_next_in_group (first) == first
13820 && bfd_elf_match_symbols_in_sections (first, sec, info))
13821 {
13822 sec->output_section = bfd_abs_section_ptr;
13823 sec->kept_section = first;
13824 break;
13825 }
13826 }
13827
13828 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13829 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13830 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13831 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13832 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13833 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13834 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13835 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13836 The reverse order cannot happen as there is never a bfd with only the
13837 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13838 matter as here were are looking only for cross-bfd sections. */
13839
13840 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13841 for (l = already_linked_list->entry; l != NULL; l = l->next)
13842 if ((l->sec->flags & SEC_GROUP) == 0
13843 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13844 {
13845 if (abfd != l->sec->owner)
13846 sec->output_section = bfd_abs_section_ptr;
13847 break;
13848 }
13849
13850 /* This is the first section with this name. Record it. */
13851 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
13852 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
13853 return sec->output_section == bfd_abs_section_ptr;
13854 }
13855
13856 bfd_boolean
13857 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
13858 {
13859 return sym->st_shndx == SHN_COMMON;
13860 }
13861
13862 unsigned int
13863 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13864 {
13865 return SHN_COMMON;
13866 }
13867
13868 asection *
13869 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13870 {
13871 return bfd_com_section_ptr;
13872 }
13873
13874 bfd_vma
13875 _bfd_elf_default_got_elt_size (bfd *abfd,
13876 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13877 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13878 bfd *ibfd ATTRIBUTE_UNUSED,
13879 unsigned long symndx ATTRIBUTE_UNUSED)
13880 {
13881 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13882 return bed->s->arch_size / 8;
13883 }
13884
13885 /* Routines to support the creation of dynamic relocs. */
13886
13887 /* Returns the name of the dynamic reloc section associated with SEC. */
13888
13889 static const char *
13890 get_dynamic_reloc_section_name (bfd * abfd,
13891 asection * sec,
13892 bfd_boolean is_rela)
13893 {
13894 char *name;
13895 const char *old_name = bfd_get_section_name (NULL, sec);
13896 const char *prefix = is_rela ? ".rela" : ".rel";
13897
13898 if (old_name == NULL)
13899 return NULL;
13900
13901 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
13902 sprintf (name, "%s%s", prefix, old_name);
13903
13904 return name;
13905 }
13906
13907 /* Returns the dynamic reloc section associated with SEC.
13908 If necessary compute the name of the dynamic reloc section based
13909 on SEC's name (looked up in ABFD's string table) and the setting
13910 of IS_RELA. */
13911
13912 asection *
13913 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
13914 asection * sec,
13915 bfd_boolean is_rela)
13916 {
13917 asection * reloc_sec = elf_section_data (sec)->sreloc;
13918
13919 if (reloc_sec == NULL)
13920 {
13921 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13922
13923 if (name != NULL)
13924 {
13925 reloc_sec = bfd_get_linker_section (abfd, name);
13926
13927 if (reloc_sec != NULL)
13928 elf_section_data (sec)->sreloc = reloc_sec;
13929 }
13930 }
13931
13932 return reloc_sec;
13933 }
13934
13935 /* Returns the dynamic reloc section associated with SEC. If the
13936 section does not exist it is created and attached to the DYNOBJ
13937 bfd and stored in the SRELOC field of SEC's elf_section_data
13938 structure.
13939
13940 ALIGNMENT is the alignment for the newly created section and
13941 IS_RELA defines whether the name should be .rela.<SEC's name>
13942 or .rel.<SEC's name>. The section name is looked up in the
13943 string table associated with ABFD. */
13944
13945 asection *
13946 _bfd_elf_make_dynamic_reloc_section (asection *sec,
13947 bfd *dynobj,
13948 unsigned int alignment,
13949 bfd *abfd,
13950 bfd_boolean is_rela)
13951 {
13952 asection * reloc_sec = elf_section_data (sec)->sreloc;
13953
13954 if (reloc_sec == NULL)
13955 {
13956 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13957
13958 if (name == NULL)
13959 return NULL;
13960
13961 reloc_sec = bfd_get_linker_section (dynobj, name);
13962
13963 if (reloc_sec == NULL)
13964 {
13965 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
13966 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
13967 if ((sec->flags & SEC_ALLOC) != 0)
13968 flags |= SEC_ALLOC | SEC_LOAD;
13969
13970 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
13971 if (reloc_sec != NULL)
13972 {
13973 /* _bfd_elf_get_sec_type_attr chooses a section type by
13974 name. Override as it may be wrong, eg. for a user
13975 section named "auto" we'll get ".relauto" which is
13976 seen to be a .rela section. */
13977 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
13978 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
13979 reloc_sec = NULL;
13980 }
13981 }
13982
13983 elf_section_data (sec)->sreloc = reloc_sec;
13984 }
13985
13986 return reloc_sec;
13987 }
13988
13989 /* Copy the ELF symbol type and other attributes for a linker script
13990 assignment from HSRC to HDEST. Generally this should be treated as
13991 if we found a strong non-dynamic definition for HDEST (except that
13992 ld ignores multiple definition errors). */
13993 void
13994 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
13995 struct bfd_link_hash_entry *hdest,
13996 struct bfd_link_hash_entry *hsrc)
13997 {
13998 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
13999 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14000 Elf_Internal_Sym isym;
14001
14002 ehdest->type = ehsrc->type;
14003 ehdest->target_internal = ehsrc->target_internal;
14004
14005 isym.st_other = ehsrc->other;
14006 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
14007 }
14008
14009 /* Append a RELA relocation REL to section S in BFD. */
14010
14011 void
14012 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14013 {
14014 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14015 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14016 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14017 bed->s->swap_reloca_out (abfd, rel, loc);
14018 }
14019
14020 /* Append a REL relocation REL to section S in BFD. */
14021
14022 void
14023 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14024 {
14025 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14026 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14027 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
14028 bed->s->swap_reloc_out (abfd, rel, loc);
14029 }
This page took 0.321956 seconds and 4 git commands to generate.