dwarf2read.c (setup_type_unit_groups): Add comment.
[deliverable/binutils-gdb.git] / bfd / elflink.c
1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2015 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
32 /* This struct is used to pass information to routines called via
33 elf_link_hash_traverse which must return failure. */
34
35 struct elf_info_failed
36 {
37 struct bfd_link_info *info;
38 bfd_boolean failed;
39 };
40
41 /* This structure is used to pass information to
42 _bfd_elf_link_find_version_dependencies. */
43
44 struct elf_find_verdep_info
45 {
46 /* General link information. */
47 struct bfd_link_info *info;
48 /* The number of dependencies. */
49 unsigned int vers;
50 /* Whether we had a failure. */
51 bfd_boolean failed;
52 };
53
54 static bfd_boolean _bfd_elf_fix_symbol_flags
55 (struct elf_link_hash_entry *, struct elf_info_failed *);
56
57 asection *
58 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
59 unsigned long r_symndx,
60 bfd_boolean discard)
61 {
62 if (r_symndx >= cookie->locsymcount
63 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
64 {
65 struct elf_link_hash_entry *h;
66
67 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
68
69 while (h->root.type == bfd_link_hash_indirect
70 || h->root.type == bfd_link_hash_warning)
71 h = (struct elf_link_hash_entry *) h->root.u.i.link;
72
73 if ((h->root.type == bfd_link_hash_defined
74 || h->root.type == bfd_link_hash_defweak)
75 && discarded_section (h->root.u.def.section))
76 return h->root.u.def.section;
77 else
78 return NULL;
79 }
80 else
81 {
82 /* It's not a relocation against a global symbol,
83 but it could be a relocation against a local
84 symbol for a discarded section. */
85 asection *isec;
86 Elf_Internal_Sym *isym;
87
88 /* Need to: get the symbol; get the section. */
89 isym = &cookie->locsyms[r_symndx];
90 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
91 if (isec != NULL
92 && discard ? discarded_section (isec) : 1)
93 return isec;
94 }
95 return NULL;
96 }
97
98 /* Define a symbol in a dynamic linkage section. */
99
100 struct elf_link_hash_entry *
101 _bfd_elf_define_linkage_sym (bfd *abfd,
102 struct bfd_link_info *info,
103 asection *sec,
104 const char *name)
105 {
106 struct elf_link_hash_entry *h;
107 struct bfd_link_hash_entry *bh;
108 const struct elf_backend_data *bed;
109
110 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
111 if (h != NULL)
112 {
113 /* Zap symbol defined in an as-needed lib that wasn't linked.
114 This is a symptom of a larger problem: Absolute symbols
115 defined in shared libraries can't be overridden, because we
116 lose the link to the bfd which is via the symbol section. */
117 h->root.type = bfd_link_hash_new;
118 }
119
120 bh = &h->root;
121 bed = get_elf_backend_data (abfd);
122 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
123 sec, 0, NULL, FALSE, bed->collect,
124 &bh))
125 return NULL;
126 h = (struct elf_link_hash_entry *) bh;
127 h->def_regular = 1;
128 h->non_elf = 0;
129 h->root.linker_def = 1;
130 h->type = STT_OBJECT;
131 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
132 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
133
134 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
135 return h;
136 }
137
138 bfd_boolean
139 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
140 {
141 flagword flags;
142 asection *s;
143 struct elf_link_hash_entry *h;
144 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
145 struct elf_link_hash_table *htab = elf_hash_table (info);
146
147 /* This function may be called more than once. */
148 s = bfd_get_linker_section (abfd, ".got");
149 if (s != NULL)
150 return TRUE;
151
152 flags = bed->dynamic_sec_flags;
153
154 s = bfd_make_section_anyway_with_flags (abfd,
155 (bed->rela_plts_and_copies_p
156 ? ".rela.got" : ".rel.got"),
157 (bed->dynamic_sec_flags
158 | SEC_READONLY));
159 if (s == NULL
160 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
161 return FALSE;
162 htab->srelgot = s;
163
164 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
165 if (s == NULL
166 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
167 return FALSE;
168 htab->sgot = s;
169
170 if (bed->want_got_plt)
171 {
172 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
173 if (s == NULL
174 || !bfd_set_section_alignment (abfd, s,
175 bed->s->log_file_align))
176 return FALSE;
177 htab->sgotplt = s;
178 }
179
180 /* The first bit of the global offset table is the header. */
181 s->size += bed->got_header_size;
182
183 if (bed->want_got_sym)
184 {
185 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
186 (or .got.plt) section. We don't do this in the linker script
187 because we don't want to define the symbol if we are not creating
188 a global offset table. */
189 h = _bfd_elf_define_linkage_sym (abfd, info, s,
190 "_GLOBAL_OFFSET_TABLE_");
191 elf_hash_table (info)->hgot = h;
192 if (h == NULL)
193 return FALSE;
194 }
195
196 return TRUE;
197 }
198 \f
199 /* Create a strtab to hold the dynamic symbol names. */
200 static bfd_boolean
201 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
202 {
203 struct elf_link_hash_table *hash_table;
204
205 hash_table = elf_hash_table (info);
206 if (hash_table->dynobj == NULL)
207 hash_table->dynobj = abfd;
208
209 if (hash_table->dynstr == NULL)
210 {
211 hash_table->dynstr = _bfd_elf_strtab_init ();
212 if (hash_table->dynstr == NULL)
213 return FALSE;
214 }
215 return TRUE;
216 }
217
218 /* Create some sections which will be filled in with dynamic linking
219 information. ABFD is an input file which requires dynamic sections
220 to be created. The dynamic sections take up virtual memory space
221 when the final executable is run, so we need to create them before
222 addresses are assigned to the output sections. We work out the
223 actual contents and size of these sections later. */
224
225 bfd_boolean
226 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
227 {
228 flagword flags;
229 asection *s;
230 const struct elf_backend_data *bed;
231 struct elf_link_hash_entry *h;
232
233 if (! is_elf_hash_table (info->hash))
234 return FALSE;
235
236 if (elf_hash_table (info)->dynamic_sections_created)
237 return TRUE;
238
239 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
240 return FALSE;
241
242 abfd = elf_hash_table (info)->dynobj;
243 bed = get_elf_backend_data (abfd);
244
245 flags = bed->dynamic_sec_flags;
246
247 /* A dynamically linked executable has a .interp section, but a
248 shared library does not. */
249 if (bfd_link_executable (info) && !info->nointerp)
250 {
251 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
252 flags | SEC_READONLY);
253 if (s == NULL)
254 return FALSE;
255 }
256
257 /* Create sections to hold version informations. These are removed
258 if they are not needed. */
259 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
260 flags | SEC_READONLY);
261 if (s == NULL
262 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
263 return FALSE;
264
265 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
266 flags | SEC_READONLY);
267 if (s == NULL
268 || ! bfd_set_section_alignment (abfd, s, 1))
269 return FALSE;
270
271 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
272 flags | SEC_READONLY);
273 if (s == NULL
274 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
275 return FALSE;
276
277 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
278 flags | SEC_READONLY);
279 if (s == NULL
280 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
281 return FALSE;
282 elf_hash_table (info)->dynsym = s;
283
284 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
285 flags | SEC_READONLY);
286 if (s == NULL)
287 return FALSE;
288
289 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
290 if (s == NULL
291 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
292 return FALSE;
293
294 /* The special symbol _DYNAMIC is always set to the start of the
295 .dynamic section. We could set _DYNAMIC in a linker script, but we
296 only want to define it if we are, in fact, creating a .dynamic
297 section. We don't want to define it if there is no .dynamic
298 section, since on some ELF platforms the start up code examines it
299 to decide how to initialize the process. */
300 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
301 elf_hash_table (info)->hdynamic = h;
302 if (h == NULL)
303 return FALSE;
304
305 if (info->emit_hash)
306 {
307 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
308 flags | SEC_READONLY);
309 if (s == NULL
310 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
311 return FALSE;
312 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
313 }
314
315 if (info->emit_gnu_hash)
316 {
317 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
318 flags | SEC_READONLY);
319 if (s == NULL
320 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
321 return FALSE;
322 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
323 4 32-bit words followed by variable count of 64-bit words, then
324 variable count of 32-bit words. */
325 if (bed->s->arch_size == 64)
326 elf_section_data (s)->this_hdr.sh_entsize = 0;
327 else
328 elf_section_data (s)->this_hdr.sh_entsize = 4;
329 }
330
331 /* Let the backend create the rest of the sections. This lets the
332 backend set the right flags. The backend will normally create
333 the .got and .plt sections. */
334 if (bed->elf_backend_create_dynamic_sections == NULL
335 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
336 return FALSE;
337
338 elf_hash_table (info)->dynamic_sections_created = TRUE;
339
340 return TRUE;
341 }
342
343 /* Create dynamic sections when linking against a dynamic object. */
344
345 bfd_boolean
346 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
347 {
348 flagword flags, pltflags;
349 struct elf_link_hash_entry *h;
350 asection *s;
351 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
352 struct elf_link_hash_table *htab = elf_hash_table (info);
353
354 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
355 .rel[a].bss sections. */
356 flags = bed->dynamic_sec_flags;
357
358 pltflags = flags;
359 if (bed->plt_not_loaded)
360 /* We do not clear SEC_ALLOC here because we still want the OS to
361 allocate space for the section; it's just that there's nothing
362 to read in from the object file. */
363 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
364 else
365 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
366 if (bed->plt_readonly)
367 pltflags |= SEC_READONLY;
368
369 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
370 if (s == NULL
371 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
372 return FALSE;
373 htab->splt = s;
374
375 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
376 .plt section. */
377 if (bed->want_plt_sym)
378 {
379 h = _bfd_elf_define_linkage_sym (abfd, info, s,
380 "_PROCEDURE_LINKAGE_TABLE_");
381 elf_hash_table (info)->hplt = h;
382 if (h == NULL)
383 return FALSE;
384 }
385
386 s = bfd_make_section_anyway_with_flags (abfd,
387 (bed->rela_plts_and_copies_p
388 ? ".rela.plt" : ".rel.plt"),
389 flags | SEC_READONLY);
390 if (s == NULL
391 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
392 return FALSE;
393 htab->srelplt = s;
394
395 if (! _bfd_elf_create_got_section (abfd, info))
396 return FALSE;
397
398 if (bed->want_dynbss)
399 {
400 /* The .dynbss section is a place to put symbols which are defined
401 by dynamic objects, are referenced by regular objects, and are
402 not functions. We must allocate space for them in the process
403 image and use a R_*_COPY reloc to tell the dynamic linker to
404 initialize them at run time. The linker script puts the .dynbss
405 section into the .bss section of the final image. */
406 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
407 (SEC_ALLOC | SEC_LINKER_CREATED));
408 if (s == NULL)
409 return FALSE;
410
411 /* The .rel[a].bss section holds copy relocs. This section is not
412 normally needed. We need to create it here, though, so that the
413 linker will map it to an output section. We can't just create it
414 only if we need it, because we will not know whether we need it
415 until we have seen all the input files, and the first time the
416 main linker code calls BFD after examining all the input files
417 (size_dynamic_sections) the input sections have already been
418 mapped to the output sections. If the section turns out not to
419 be needed, we can discard it later. We will never need this
420 section when generating a shared object, since they do not use
421 copy relocs. */
422 if (! bfd_link_pic (info))
423 {
424 s = bfd_make_section_anyway_with_flags (abfd,
425 (bed->rela_plts_and_copies_p
426 ? ".rela.bss" : ".rel.bss"),
427 flags | SEC_READONLY);
428 if (s == NULL
429 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
430 return FALSE;
431 }
432 }
433
434 return TRUE;
435 }
436 \f
437 /* Record a new dynamic symbol. We record the dynamic symbols as we
438 read the input files, since we need to have a list of all of them
439 before we can determine the final sizes of the output sections.
440 Note that we may actually call this function even though we are not
441 going to output any dynamic symbols; in some cases we know that a
442 symbol should be in the dynamic symbol table, but only if there is
443 one. */
444
445 bfd_boolean
446 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
447 struct elf_link_hash_entry *h)
448 {
449 if (h->dynindx == -1)
450 {
451 struct elf_strtab_hash *dynstr;
452 char *p;
453 const char *name;
454 bfd_size_type indx;
455
456 /* XXX: The ABI draft says the linker must turn hidden and
457 internal symbols into STB_LOCAL symbols when producing the
458 DSO. However, if ld.so honors st_other in the dynamic table,
459 this would not be necessary. */
460 switch (ELF_ST_VISIBILITY (h->other))
461 {
462 case STV_INTERNAL:
463 case STV_HIDDEN:
464 if (h->root.type != bfd_link_hash_undefined
465 && h->root.type != bfd_link_hash_undefweak)
466 {
467 h->forced_local = 1;
468 if (!elf_hash_table (info)->is_relocatable_executable)
469 return TRUE;
470 }
471
472 default:
473 break;
474 }
475
476 h->dynindx = elf_hash_table (info)->dynsymcount;
477 ++elf_hash_table (info)->dynsymcount;
478
479 dynstr = elf_hash_table (info)->dynstr;
480 if (dynstr == NULL)
481 {
482 /* Create a strtab to hold the dynamic symbol names. */
483 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
484 if (dynstr == NULL)
485 return FALSE;
486 }
487
488 /* We don't put any version information in the dynamic string
489 table. */
490 name = h->root.root.string;
491 p = strchr (name, ELF_VER_CHR);
492 if (p != NULL)
493 /* We know that the p points into writable memory. In fact,
494 there are only a few symbols that have read-only names, being
495 those like _GLOBAL_OFFSET_TABLE_ that are created specially
496 by the backends. Most symbols will have names pointing into
497 an ELF string table read from a file, or to objalloc memory. */
498 *p = 0;
499
500 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
501
502 if (p != NULL)
503 *p = ELF_VER_CHR;
504
505 if (indx == (bfd_size_type) -1)
506 return FALSE;
507 h->dynstr_index = indx;
508 }
509
510 return TRUE;
511 }
512 \f
513 /* Mark a symbol dynamic. */
514
515 static void
516 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
517 struct elf_link_hash_entry *h,
518 Elf_Internal_Sym *sym)
519 {
520 struct bfd_elf_dynamic_list *d = info->dynamic_list;
521
522 /* It may be called more than once on the same H. */
523 if(h->dynamic || bfd_link_relocatable (info))
524 return;
525
526 if ((info->dynamic_data
527 && (h->type == STT_OBJECT
528 || (sym != NULL
529 && ELF_ST_TYPE (sym->st_info) == STT_OBJECT)))
530 || (d != NULL
531 && h->root.type == bfd_link_hash_new
532 && (*d->match) (&d->head, NULL, h->root.root.string)))
533 h->dynamic = 1;
534 }
535
536 /* Record an assignment to a symbol made by a linker script. We need
537 this in case some dynamic object refers to this symbol. */
538
539 bfd_boolean
540 bfd_elf_record_link_assignment (bfd *output_bfd,
541 struct bfd_link_info *info,
542 const char *name,
543 bfd_boolean provide,
544 bfd_boolean hidden)
545 {
546 struct elf_link_hash_entry *h, *hv;
547 struct elf_link_hash_table *htab;
548 const struct elf_backend_data *bed;
549
550 if (!is_elf_hash_table (info->hash))
551 return TRUE;
552
553 htab = elf_hash_table (info);
554 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
555 if (h == NULL)
556 return provide;
557
558 switch (h->root.type)
559 {
560 case bfd_link_hash_defined:
561 case bfd_link_hash_defweak:
562 case bfd_link_hash_common:
563 break;
564 case bfd_link_hash_undefweak:
565 case bfd_link_hash_undefined:
566 /* Since we're defining the symbol, don't let it seem to have not
567 been defined. record_dynamic_symbol and size_dynamic_sections
568 may depend on this. */
569 h->root.type = bfd_link_hash_new;
570 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
571 bfd_link_repair_undef_list (&htab->root);
572 break;
573 case bfd_link_hash_new:
574 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
575 h->non_elf = 0;
576 break;
577 case bfd_link_hash_indirect:
578 /* We had a versioned symbol in a dynamic library. We make the
579 the versioned symbol point to this one. */
580 bed = get_elf_backend_data (output_bfd);
581 hv = h;
582 while (hv->root.type == bfd_link_hash_indirect
583 || hv->root.type == bfd_link_hash_warning)
584 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
585 /* We don't need to update h->root.u since linker will set them
586 later. */
587 h->root.type = bfd_link_hash_undefined;
588 hv->root.type = bfd_link_hash_indirect;
589 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
590 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
591 break;
592 case bfd_link_hash_warning:
593 abort ();
594 break;
595 }
596
597 /* If this symbol is being provided by the linker script, and it is
598 currently defined by a dynamic object, but not by a regular
599 object, then mark it as undefined so that the generic linker will
600 force the correct value. */
601 if (provide
602 && h->def_dynamic
603 && !h->def_regular)
604 h->root.type = bfd_link_hash_undefined;
605
606 /* If this symbol is not being provided by the linker script, and it is
607 currently defined by a dynamic object, but not by a regular object,
608 then clear out any version information because the symbol will not be
609 associated with the dynamic object any more. */
610 if (!provide
611 && h->def_dynamic
612 && !h->def_regular)
613 h->verinfo.verdef = NULL;
614
615 h->def_regular = 1;
616
617 if (hidden)
618 {
619 bed = get_elf_backend_data (output_bfd);
620 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
621 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
622 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
623 }
624
625 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
626 and executables. */
627 if (!bfd_link_relocatable (info)
628 && h->dynindx != -1
629 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
630 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
631 h->forced_local = 1;
632
633 if ((h->def_dynamic
634 || h->ref_dynamic
635 || bfd_link_pic (info)
636 || (bfd_link_pde (info)
637 && elf_hash_table (info)->is_relocatable_executable))
638 && h->dynindx == -1)
639 {
640 if (! bfd_elf_link_record_dynamic_symbol (info, h))
641 return FALSE;
642
643 /* If this is a weak defined symbol, and we know a corresponding
644 real symbol from the same dynamic object, make sure the real
645 symbol is also made into a dynamic symbol. */
646 if (h->u.weakdef != NULL
647 && h->u.weakdef->dynindx == -1)
648 {
649 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
650 return FALSE;
651 }
652 }
653
654 return TRUE;
655 }
656
657 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
658 success, and 2 on a failure caused by attempting to record a symbol
659 in a discarded section, eg. a discarded link-once section symbol. */
660
661 int
662 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
663 bfd *input_bfd,
664 long input_indx)
665 {
666 bfd_size_type amt;
667 struct elf_link_local_dynamic_entry *entry;
668 struct elf_link_hash_table *eht;
669 struct elf_strtab_hash *dynstr;
670 unsigned long dynstr_index;
671 char *name;
672 Elf_External_Sym_Shndx eshndx;
673 char esym[sizeof (Elf64_External_Sym)];
674
675 if (! is_elf_hash_table (info->hash))
676 return 0;
677
678 /* See if the entry exists already. */
679 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
680 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
681 return 1;
682
683 amt = sizeof (*entry);
684 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
685 if (entry == NULL)
686 return 0;
687
688 /* Go find the symbol, so that we can find it's name. */
689 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
690 1, input_indx, &entry->isym, esym, &eshndx))
691 {
692 bfd_release (input_bfd, entry);
693 return 0;
694 }
695
696 if (entry->isym.st_shndx != SHN_UNDEF
697 && entry->isym.st_shndx < SHN_LORESERVE)
698 {
699 asection *s;
700
701 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
702 if (s == NULL || bfd_is_abs_section (s->output_section))
703 {
704 /* We can still bfd_release here as nothing has done another
705 bfd_alloc. We can't do this later in this function. */
706 bfd_release (input_bfd, entry);
707 return 2;
708 }
709 }
710
711 name = (bfd_elf_string_from_elf_section
712 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
713 entry->isym.st_name));
714
715 dynstr = elf_hash_table (info)->dynstr;
716 if (dynstr == NULL)
717 {
718 /* Create a strtab to hold the dynamic symbol names. */
719 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
720 if (dynstr == NULL)
721 return 0;
722 }
723
724 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
725 if (dynstr_index == (unsigned long) -1)
726 return 0;
727 entry->isym.st_name = dynstr_index;
728
729 eht = elf_hash_table (info);
730
731 entry->next = eht->dynlocal;
732 eht->dynlocal = entry;
733 entry->input_bfd = input_bfd;
734 entry->input_indx = input_indx;
735 eht->dynsymcount++;
736
737 /* Whatever binding the symbol had before, it's now local. */
738 entry->isym.st_info
739 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
740
741 /* The dynindx will be set at the end of size_dynamic_sections. */
742
743 return 1;
744 }
745
746 /* Return the dynindex of a local dynamic symbol. */
747
748 long
749 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
750 bfd *input_bfd,
751 long input_indx)
752 {
753 struct elf_link_local_dynamic_entry *e;
754
755 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
756 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
757 return e->dynindx;
758 return -1;
759 }
760
761 /* This function is used to renumber the dynamic symbols, if some of
762 them are removed because they are marked as local. This is called
763 via elf_link_hash_traverse. */
764
765 static bfd_boolean
766 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
767 void *data)
768 {
769 size_t *count = (size_t *) data;
770
771 if (h->forced_local)
772 return TRUE;
773
774 if (h->dynindx != -1)
775 h->dynindx = ++(*count);
776
777 return TRUE;
778 }
779
780
781 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
782 STB_LOCAL binding. */
783
784 static bfd_boolean
785 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
786 void *data)
787 {
788 size_t *count = (size_t *) data;
789
790 if (!h->forced_local)
791 return TRUE;
792
793 if (h->dynindx != -1)
794 h->dynindx = ++(*count);
795
796 return TRUE;
797 }
798
799 /* Return true if the dynamic symbol for a given section should be
800 omitted when creating a shared library. */
801 bfd_boolean
802 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
803 struct bfd_link_info *info,
804 asection *p)
805 {
806 struct elf_link_hash_table *htab;
807 asection *ip;
808
809 switch (elf_section_data (p)->this_hdr.sh_type)
810 {
811 case SHT_PROGBITS:
812 case SHT_NOBITS:
813 /* If sh_type is yet undecided, assume it could be
814 SHT_PROGBITS/SHT_NOBITS. */
815 case SHT_NULL:
816 htab = elf_hash_table (info);
817 if (p == htab->tls_sec)
818 return FALSE;
819
820 if (htab->text_index_section != NULL)
821 return p != htab->text_index_section && p != htab->data_index_section;
822
823 return (htab->dynobj != NULL
824 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
825 && ip->output_section == p);
826
827 /* There shouldn't be section relative relocations
828 against any other section. */
829 default:
830 return TRUE;
831 }
832 }
833
834 /* Assign dynsym indices. In a shared library we generate a section
835 symbol for each output section, which come first. Next come symbols
836 which have been forced to local binding. Then all of the back-end
837 allocated local dynamic syms, followed by the rest of the global
838 symbols. */
839
840 static unsigned long
841 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
842 struct bfd_link_info *info,
843 unsigned long *section_sym_count)
844 {
845 unsigned long dynsymcount = 0;
846
847 if (bfd_link_pic (info)
848 || elf_hash_table (info)->is_relocatable_executable)
849 {
850 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
851 asection *p;
852 for (p = output_bfd->sections; p ; p = p->next)
853 if ((p->flags & SEC_EXCLUDE) == 0
854 && (p->flags & SEC_ALLOC) != 0
855 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
856 elf_section_data (p)->dynindx = ++dynsymcount;
857 else
858 elf_section_data (p)->dynindx = 0;
859 }
860 *section_sym_count = dynsymcount;
861
862 elf_link_hash_traverse (elf_hash_table (info),
863 elf_link_renumber_local_hash_table_dynsyms,
864 &dynsymcount);
865
866 if (elf_hash_table (info)->dynlocal)
867 {
868 struct elf_link_local_dynamic_entry *p;
869 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
870 p->dynindx = ++dynsymcount;
871 }
872
873 elf_link_hash_traverse (elf_hash_table (info),
874 elf_link_renumber_hash_table_dynsyms,
875 &dynsymcount);
876
877 /* There is an unused NULL entry at the head of the table which
878 we must account for in our count. Unless there weren't any
879 symbols, which means we'll have no table at all. */
880 if (dynsymcount != 0)
881 ++dynsymcount;
882
883 elf_hash_table (info)->dynsymcount = dynsymcount;
884 return dynsymcount;
885 }
886
887 /* Merge st_other field. */
888
889 static void
890 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
891 const Elf_Internal_Sym *isym, asection *sec,
892 bfd_boolean definition, bfd_boolean dynamic)
893 {
894 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
895
896 /* If st_other has a processor-specific meaning, specific
897 code might be needed here. */
898 if (bed->elf_backend_merge_symbol_attribute)
899 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
900 dynamic);
901
902 if (!dynamic)
903 {
904 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
905 unsigned hvis = ELF_ST_VISIBILITY (h->other);
906
907 /* Keep the most constraining visibility. Leave the remainder
908 of the st_other field to elf_backend_merge_symbol_attribute. */
909 if (symvis - 1 < hvis - 1)
910 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
911 }
912 else if (definition
913 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
914 && (sec->flags & SEC_READONLY) == 0)
915 h->protected_def = 1;
916 }
917
918 /* This function is called when we want to merge a new symbol with an
919 existing symbol. It handles the various cases which arise when we
920 find a definition in a dynamic object, or when there is already a
921 definition in a dynamic object. The new symbol is described by
922 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
923 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
924 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
925 of an old common symbol. We set OVERRIDE if the old symbol is
926 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
927 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
928 to change. By OK to change, we mean that we shouldn't warn if the
929 type or size does change. */
930
931 static bfd_boolean
932 _bfd_elf_merge_symbol (bfd *abfd,
933 struct bfd_link_info *info,
934 const char *name,
935 Elf_Internal_Sym *sym,
936 asection **psec,
937 bfd_vma *pvalue,
938 struct elf_link_hash_entry **sym_hash,
939 bfd **poldbfd,
940 bfd_boolean *pold_weak,
941 unsigned int *pold_alignment,
942 bfd_boolean *skip,
943 bfd_boolean *override,
944 bfd_boolean *type_change_ok,
945 bfd_boolean *size_change_ok,
946 bfd_boolean *matched)
947 {
948 asection *sec, *oldsec;
949 struct elf_link_hash_entry *h;
950 struct elf_link_hash_entry *hi;
951 struct elf_link_hash_entry *flip;
952 int bind;
953 bfd *oldbfd;
954 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
955 bfd_boolean newweak, oldweak, newfunc, oldfunc;
956 const struct elf_backend_data *bed;
957 char *new_version;
958
959 *skip = FALSE;
960 *override = FALSE;
961
962 sec = *psec;
963 bind = ELF_ST_BIND (sym->st_info);
964
965 if (! bfd_is_und_section (sec))
966 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
967 else
968 h = ((struct elf_link_hash_entry *)
969 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
970 if (h == NULL)
971 return FALSE;
972 *sym_hash = h;
973
974 bed = get_elf_backend_data (abfd);
975
976 /* NEW_VERSION is the symbol version of the new symbol. */
977 if (h->versioned != unversioned)
978 {
979 /* Symbol version is unknown or versioned. */
980 new_version = strrchr (name, ELF_VER_CHR);
981 if (new_version)
982 {
983 if (h->versioned == unknown)
984 {
985 if (new_version > name && new_version[-1] != ELF_VER_CHR)
986 h->versioned = versioned_hidden;
987 else
988 h->versioned = versioned;
989 }
990 new_version += 1;
991 if (new_version[0] == '\0')
992 new_version = NULL;
993 }
994 else
995 h->versioned = unversioned;
996 }
997 else
998 new_version = NULL;
999
1000 /* For merging, we only care about real symbols. But we need to make
1001 sure that indirect symbol dynamic flags are updated. */
1002 hi = h;
1003 while (h->root.type == bfd_link_hash_indirect
1004 || h->root.type == bfd_link_hash_warning)
1005 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1006
1007 if (!*matched)
1008 {
1009 if (hi == h || h->root.type == bfd_link_hash_new)
1010 *matched = TRUE;
1011 else
1012 {
1013 /* OLD_HIDDEN is true if the existing symbol is only visibile
1014 to the symbol with the same symbol version. NEW_HIDDEN is
1015 true if the new symbol is only visibile to the symbol with
1016 the same symbol version. */
1017 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1018 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1019 if (!old_hidden && !new_hidden)
1020 /* The new symbol matches the existing symbol if both
1021 aren't hidden. */
1022 *matched = TRUE;
1023 else
1024 {
1025 /* OLD_VERSION is the symbol version of the existing
1026 symbol. */
1027 char *old_version;
1028
1029 if (h->versioned >= versioned)
1030 old_version = strrchr (h->root.root.string,
1031 ELF_VER_CHR) + 1;
1032 else
1033 old_version = NULL;
1034
1035 /* The new symbol matches the existing symbol if they
1036 have the same symbol version. */
1037 *matched = (old_version == new_version
1038 || (old_version != NULL
1039 && new_version != NULL
1040 && strcmp (old_version, new_version) == 0));
1041 }
1042 }
1043 }
1044
1045 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1046 existing symbol. */
1047
1048 oldbfd = NULL;
1049 oldsec = NULL;
1050 switch (h->root.type)
1051 {
1052 default:
1053 break;
1054
1055 case bfd_link_hash_undefined:
1056 case bfd_link_hash_undefweak:
1057 oldbfd = h->root.u.undef.abfd;
1058 break;
1059
1060 case bfd_link_hash_defined:
1061 case bfd_link_hash_defweak:
1062 oldbfd = h->root.u.def.section->owner;
1063 oldsec = h->root.u.def.section;
1064 break;
1065
1066 case bfd_link_hash_common:
1067 oldbfd = h->root.u.c.p->section->owner;
1068 oldsec = h->root.u.c.p->section;
1069 if (pold_alignment)
1070 *pold_alignment = h->root.u.c.p->alignment_power;
1071 break;
1072 }
1073 if (poldbfd && *poldbfd == NULL)
1074 *poldbfd = oldbfd;
1075
1076 /* Differentiate strong and weak symbols. */
1077 newweak = bind == STB_WEAK;
1078 oldweak = (h->root.type == bfd_link_hash_defweak
1079 || h->root.type == bfd_link_hash_undefweak);
1080 if (pold_weak)
1081 *pold_weak = oldweak;
1082
1083 /* This code is for coping with dynamic objects, and is only useful
1084 if we are doing an ELF link. */
1085 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1086 return TRUE;
1087
1088 /* We have to check it for every instance since the first few may be
1089 references and not all compilers emit symbol type for undefined
1090 symbols. */
1091 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1092
1093 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1094 respectively, is from a dynamic object. */
1095
1096 newdyn = (abfd->flags & DYNAMIC) != 0;
1097
1098 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1099 syms and defined syms in dynamic libraries respectively.
1100 ref_dynamic on the other hand can be set for a symbol defined in
1101 a dynamic library, and def_dynamic may not be set; When the
1102 definition in a dynamic lib is overridden by a definition in the
1103 executable use of the symbol in the dynamic lib becomes a
1104 reference to the executable symbol. */
1105 if (newdyn)
1106 {
1107 if (bfd_is_und_section (sec))
1108 {
1109 if (bind != STB_WEAK)
1110 {
1111 h->ref_dynamic_nonweak = 1;
1112 hi->ref_dynamic_nonweak = 1;
1113 }
1114 }
1115 else
1116 {
1117 /* Update the existing symbol only if they match. */
1118 if (*matched)
1119 h->dynamic_def = 1;
1120 hi->dynamic_def = 1;
1121 }
1122 }
1123
1124 /* If we just created the symbol, mark it as being an ELF symbol.
1125 Other than that, there is nothing to do--there is no merge issue
1126 with a newly defined symbol--so we just return. */
1127
1128 if (h->root.type == bfd_link_hash_new)
1129 {
1130 h->non_elf = 0;
1131 return TRUE;
1132 }
1133
1134 /* In cases involving weak versioned symbols, we may wind up trying
1135 to merge a symbol with itself. Catch that here, to avoid the
1136 confusion that results if we try to override a symbol with
1137 itself. The additional tests catch cases like
1138 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1139 dynamic object, which we do want to handle here. */
1140 if (abfd == oldbfd
1141 && (newweak || oldweak)
1142 && ((abfd->flags & DYNAMIC) == 0
1143 || !h->def_regular))
1144 return TRUE;
1145
1146 olddyn = FALSE;
1147 if (oldbfd != NULL)
1148 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1149 else if (oldsec != NULL)
1150 {
1151 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1152 indices used by MIPS ELF. */
1153 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1154 }
1155
1156 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1157 respectively, appear to be a definition rather than reference. */
1158
1159 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1160
1161 olddef = (h->root.type != bfd_link_hash_undefined
1162 && h->root.type != bfd_link_hash_undefweak
1163 && h->root.type != bfd_link_hash_common);
1164
1165 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1166 respectively, appear to be a function. */
1167
1168 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1169 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1170
1171 oldfunc = (h->type != STT_NOTYPE
1172 && bed->is_function_type (h->type));
1173
1174 /* When we try to create a default indirect symbol from the dynamic
1175 definition with the default version, we skip it if its type and
1176 the type of existing regular definition mismatch. */
1177 if (pold_alignment == NULL
1178 && newdyn
1179 && newdef
1180 && !olddyn
1181 && (((olddef || h->root.type == bfd_link_hash_common)
1182 && ELF_ST_TYPE (sym->st_info) != h->type
1183 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1184 && h->type != STT_NOTYPE
1185 && !(newfunc && oldfunc))
1186 || (olddef
1187 && ((h->type == STT_GNU_IFUNC)
1188 != (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))))
1189 {
1190 *skip = TRUE;
1191 return TRUE;
1192 }
1193
1194 /* Check TLS symbols. We don't check undefined symbols introduced
1195 by "ld -u" which have no type (and oldbfd NULL), and we don't
1196 check symbols from plugins because they also have no type. */
1197 if (oldbfd != NULL
1198 && (oldbfd->flags & BFD_PLUGIN) == 0
1199 && (abfd->flags & BFD_PLUGIN) == 0
1200 && ELF_ST_TYPE (sym->st_info) != h->type
1201 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1202 {
1203 bfd *ntbfd, *tbfd;
1204 bfd_boolean ntdef, tdef;
1205 asection *ntsec, *tsec;
1206
1207 if (h->type == STT_TLS)
1208 {
1209 ntbfd = abfd;
1210 ntsec = sec;
1211 ntdef = newdef;
1212 tbfd = oldbfd;
1213 tsec = oldsec;
1214 tdef = olddef;
1215 }
1216 else
1217 {
1218 ntbfd = oldbfd;
1219 ntsec = oldsec;
1220 ntdef = olddef;
1221 tbfd = abfd;
1222 tsec = sec;
1223 tdef = newdef;
1224 }
1225
1226 if (tdef && ntdef)
1227 (*_bfd_error_handler)
1228 (_("%s: TLS definition in %B section %A "
1229 "mismatches non-TLS definition in %B section %A"),
1230 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1231 else if (!tdef && !ntdef)
1232 (*_bfd_error_handler)
1233 (_("%s: TLS reference in %B "
1234 "mismatches non-TLS reference in %B"),
1235 tbfd, ntbfd, h->root.root.string);
1236 else if (tdef)
1237 (*_bfd_error_handler)
1238 (_("%s: TLS definition in %B section %A "
1239 "mismatches non-TLS reference in %B"),
1240 tbfd, tsec, ntbfd, h->root.root.string);
1241 else
1242 (*_bfd_error_handler)
1243 (_("%s: TLS reference in %B "
1244 "mismatches non-TLS definition in %B section %A"),
1245 tbfd, ntbfd, ntsec, h->root.root.string);
1246
1247 bfd_set_error (bfd_error_bad_value);
1248 return FALSE;
1249 }
1250
1251 /* If the old symbol has non-default visibility, we ignore the new
1252 definition from a dynamic object. */
1253 if (newdyn
1254 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1255 && !bfd_is_und_section (sec))
1256 {
1257 *skip = TRUE;
1258 /* Make sure this symbol is dynamic. */
1259 h->ref_dynamic = 1;
1260 hi->ref_dynamic = 1;
1261 /* A protected symbol has external availability. Make sure it is
1262 recorded as dynamic.
1263
1264 FIXME: Should we check type and size for protected symbol? */
1265 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1266 return bfd_elf_link_record_dynamic_symbol (info, h);
1267 else
1268 return TRUE;
1269 }
1270 else if (!newdyn
1271 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1272 && h->def_dynamic)
1273 {
1274 /* If the new symbol with non-default visibility comes from a
1275 relocatable file and the old definition comes from a dynamic
1276 object, we remove the old definition. */
1277 if (hi->root.type == bfd_link_hash_indirect)
1278 {
1279 /* Handle the case where the old dynamic definition is
1280 default versioned. We need to copy the symbol info from
1281 the symbol with default version to the normal one if it
1282 was referenced before. */
1283 if (h->ref_regular)
1284 {
1285 hi->root.type = h->root.type;
1286 h->root.type = bfd_link_hash_indirect;
1287 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1288
1289 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1290 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1291 {
1292 /* If the new symbol is hidden or internal, completely undo
1293 any dynamic link state. */
1294 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1295 h->forced_local = 0;
1296 h->ref_dynamic = 0;
1297 }
1298 else
1299 h->ref_dynamic = 1;
1300
1301 h->def_dynamic = 0;
1302 /* FIXME: Should we check type and size for protected symbol? */
1303 h->size = 0;
1304 h->type = 0;
1305
1306 h = hi;
1307 }
1308 else
1309 h = hi;
1310 }
1311
1312 /* If the old symbol was undefined before, then it will still be
1313 on the undefs list. If the new symbol is undefined or
1314 common, we can't make it bfd_link_hash_new here, because new
1315 undefined or common symbols will be added to the undefs list
1316 by _bfd_generic_link_add_one_symbol. Symbols may not be
1317 added twice to the undefs list. Also, if the new symbol is
1318 undefweak then we don't want to lose the strong undef. */
1319 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1320 {
1321 h->root.type = bfd_link_hash_undefined;
1322 h->root.u.undef.abfd = abfd;
1323 }
1324 else
1325 {
1326 h->root.type = bfd_link_hash_new;
1327 h->root.u.undef.abfd = NULL;
1328 }
1329
1330 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1331 {
1332 /* If the new symbol is hidden or internal, completely undo
1333 any dynamic link state. */
1334 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1335 h->forced_local = 0;
1336 h->ref_dynamic = 0;
1337 }
1338 else
1339 h->ref_dynamic = 1;
1340 h->def_dynamic = 0;
1341 /* FIXME: Should we check type and size for protected symbol? */
1342 h->size = 0;
1343 h->type = 0;
1344 return TRUE;
1345 }
1346
1347 /* If a new weak symbol definition comes from a regular file and the
1348 old symbol comes from a dynamic library, we treat the new one as
1349 strong. Similarly, an old weak symbol definition from a regular
1350 file is treated as strong when the new symbol comes from a dynamic
1351 library. Further, an old weak symbol from a dynamic library is
1352 treated as strong if the new symbol is from a dynamic library.
1353 This reflects the way glibc's ld.so works.
1354
1355 Do this before setting *type_change_ok or *size_change_ok so that
1356 we warn properly when dynamic library symbols are overridden. */
1357
1358 if (newdef && !newdyn && olddyn)
1359 newweak = FALSE;
1360 if (olddef && newdyn)
1361 oldweak = FALSE;
1362
1363 /* Allow changes between different types of function symbol. */
1364 if (newfunc && oldfunc)
1365 *type_change_ok = TRUE;
1366
1367 /* It's OK to change the type if either the existing symbol or the
1368 new symbol is weak. A type change is also OK if the old symbol
1369 is undefined and the new symbol is defined. */
1370
1371 if (oldweak
1372 || newweak
1373 || (newdef
1374 && h->root.type == bfd_link_hash_undefined))
1375 *type_change_ok = TRUE;
1376
1377 /* It's OK to change the size if either the existing symbol or the
1378 new symbol is weak, or if the old symbol is undefined. */
1379
1380 if (*type_change_ok
1381 || h->root.type == bfd_link_hash_undefined)
1382 *size_change_ok = TRUE;
1383
1384 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1385 symbol, respectively, appears to be a common symbol in a dynamic
1386 object. If a symbol appears in an uninitialized section, and is
1387 not weak, and is not a function, then it may be a common symbol
1388 which was resolved when the dynamic object was created. We want
1389 to treat such symbols specially, because they raise special
1390 considerations when setting the symbol size: if the symbol
1391 appears as a common symbol in a regular object, and the size in
1392 the regular object is larger, we must make sure that we use the
1393 larger size. This problematic case can always be avoided in C,
1394 but it must be handled correctly when using Fortran shared
1395 libraries.
1396
1397 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1398 likewise for OLDDYNCOMMON and OLDDEF.
1399
1400 Note that this test is just a heuristic, and that it is quite
1401 possible to have an uninitialized symbol in a shared object which
1402 is really a definition, rather than a common symbol. This could
1403 lead to some minor confusion when the symbol really is a common
1404 symbol in some regular object. However, I think it will be
1405 harmless. */
1406
1407 if (newdyn
1408 && newdef
1409 && !newweak
1410 && (sec->flags & SEC_ALLOC) != 0
1411 && (sec->flags & SEC_LOAD) == 0
1412 && sym->st_size > 0
1413 && !newfunc)
1414 newdyncommon = TRUE;
1415 else
1416 newdyncommon = FALSE;
1417
1418 if (olddyn
1419 && olddef
1420 && h->root.type == bfd_link_hash_defined
1421 && h->def_dynamic
1422 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1423 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1424 && h->size > 0
1425 && !oldfunc)
1426 olddyncommon = TRUE;
1427 else
1428 olddyncommon = FALSE;
1429
1430 /* We now know everything about the old and new symbols. We ask the
1431 backend to check if we can merge them. */
1432 if (bed->merge_symbol != NULL)
1433 {
1434 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1435 return FALSE;
1436 sec = *psec;
1437 }
1438
1439 /* If both the old and the new symbols look like common symbols in a
1440 dynamic object, set the size of the symbol to the larger of the
1441 two. */
1442
1443 if (olddyncommon
1444 && newdyncommon
1445 && sym->st_size != h->size)
1446 {
1447 /* Since we think we have two common symbols, issue a multiple
1448 common warning if desired. Note that we only warn if the
1449 size is different. If the size is the same, we simply let
1450 the old symbol override the new one as normally happens with
1451 symbols defined in dynamic objects. */
1452
1453 if (! ((*info->callbacks->multiple_common)
1454 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
1455 return FALSE;
1456
1457 if (sym->st_size > h->size)
1458 h->size = sym->st_size;
1459
1460 *size_change_ok = TRUE;
1461 }
1462
1463 /* If we are looking at a dynamic object, and we have found a
1464 definition, we need to see if the symbol was already defined by
1465 some other object. If so, we want to use the existing
1466 definition, and we do not want to report a multiple symbol
1467 definition error; we do this by clobbering *PSEC to be
1468 bfd_und_section_ptr.
1469
1470 We treat a common symbol as a definition if the symbol in the
1471 shared library is a function, since common symbols always
1472 represent variables; this can cause confusion in principle, but
1473 any such confusion would seem to indicate an erroneous program or
1474 shared library. We also permit a common symbol in a regular
1475 object to override a weak symbol in a shared object. */
1476
1477 if (newdyn
1478 && newdef
1479 && (olddef
1480 || (h->root.type == bfd_link_hash_common
1481 && (newweak || newfunc))))
1482 {
1483 *override = TRUE;
1484 newdef = FALSE;
1485 newdyncommon = FALSE;
1486
1487 *psec = sec = bfd_und_section_ptr;
1488 *size_change_ok = TRUE;
1489
1490 /* If we get here when the old symbol is a common symbol, then
1491 we are explicitly letting it override a weak symbol or
1492 function in a dynamic object, and we don't want to warn about
1493 a type change. If the old symbol is a defined symbol, a type
1494 change warning may still be appropriate. */
1495
1496 if (h->root.type == bfd_link_hash_common)
1497 *type_change_ok = TRUE;
1498 }
1499
1500 /* Handle the special case of an old common symbol merging with a
1501 new symbol which looks like a common symbol in a shared object.
1502 We change *PSEC and *PVALUE to make the new symbol look like a
1503 common symbol, and let _bfd_generic_link_add_one_symbol do the
1504 right thing. */
1505
1506 if (newdyncommon
1507 && h->root.type == bfd_link_hash_common)
1508 {
1509 *override = TRUE;
1510 newdef = FALSE;
1511 newdyncommon = FALSE;
1512 *pvalue = sym->st_size;
1513 *psec = sec = bed->common_section (oldsec);
1514 *size_change_ok = TRUE;
1515 }
1516
1517 /* Skip weak definitions of symbols that are already defined. */
1518 if (newdef && olddef && newweak)
1519 {
1520 /* Don't skip new non-IR weak syms. */
1521 if (!(oldbfd != NULL
1522 && (oldbfd->flags & BFD_PLUGIN) != 0
1523 && (abfd->flags & BFD_PLUGIN) == 0))
1524 {
1525 newdef = FALSE;
1526 *skip = TRUE;
1527 }
1528
1529 /* Merge st_other. If the symbol already has a dynamic index,
1530 but visibility says it should not be visible, turn it into a
1531 local symbol. */
1532 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1533 if (h->dynindx != -1)
1534 switch (ELF_ST_VISIBILITY (h->other))
1535 {
1536 case STV_INTERNAL:
1537 case STV_HIDDEN:
1538 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1539 break;
1540 }
1541 }
1542
1543 /* If the old symbol is from a dynamic object, and the new symbol is
1544 a definition which is not from a dynamic object, then the new
1545 symbol overrides the old symbol. Symbols from regular files
1546 always take precedence over symbols from dynamic objects, even if
1547 they are defined after the dynamic object in the link.
1548
1549 As above, we again permit a common symbol in a regular object to
1550 override a definition in a shared object if the shared object
1551 symbol is a function or is weak. */
1552
1553 flip = NULL;
1554 if (!newdyn
1555 && (newdef
1556 || (bfd_is_com_section (sec)
1557 && (oldweak || oldfunc)))
1558 && olddyn
1559 && olddef
1560 && h->def_dynamic)
1561 {
1562 /* Change the hash table entry to undefined, and let
1563 _bfd_generic_link_add_one_symbol do the right thing with the
1564 new definition. */
1565
1566 h->root.type = bfd_link_hash_undefined;
1567 h->root.u.undef.abfd = h->root.u.def.section->owner;
1568 *size_change_ok = TRUE;
1569
1570 olddef = FALSE;
1571 olddyncommon = FALSE;
1572
1573 /* We again permit a type change when a common symbol may be
1574 overriding a function. */
1575
1576 if (bfd_is_com_section (sec))
1577 {
1578 if (oldfunc)
1579 {
1580 /* If a common symbol overrides a function, make sure
1581 that it isn't defined dynamically nor has type
1582 function. */
1583 h->def_dynamic = 0;
1584 h->type = STT_NOTYPE;
1585 }
1586 *type_change_ok = TRUE;
1587 }
1588
1589 if (hi->root.type == bfd_link_hash_indirect)
1590 flip = hi;
1591 else
1592 /* This union may have been set to be non-NULL when this symbol
1593 was seen in a dynamic object. We must force the union to be
1594 NULL, so that it is correct for a regular symbol. */
1595 h->verinfo.vertree = NULL;
1596 }
1597
1598 /* Handle the special case of a new common symbol merging with an
1599 old symbol that looks like it might be a common symbol defined in
1600 a shared object. Note that we have already handled the case in
1601 which a new common symbol should simply override the definition
1602 in the shared library. */
1603
1604 if (! newdyn
1605 && bfd_is_com_section (sec)
1606 && olddyncommon)
1607 {
1608 /* It would be best if we could set the hash table entry to a
1609 common symbol, but we don't know what to use for the section
1610 or the alignment. */
1611 if (! ((*info->callbacks->multiple_common)
1612 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
1613 return FALSE;
1614
1615 /* If the presumed common symbol in the dynamic object is
1616 larger, pretend that the new symbol has its size. */
1617
1618 if (h->size > *pvalue)
1619 *pvalue = h->size;
1620
1621 /* We need to remember the alignment required by the symbol
1622 in the dynamic object. */
1623 BFD_ASSERT (pold_alignment);
1624 *pold_alignment = h->root.u.def.section->alignment_power;
1625
1626 olddef = FALSE;
1627 olddyncommon = FALSE;
1628
1629 h->root.type = bfd_link_hash_undefined;
1630 h->root.u.undef.abfd = h->root.u.def.section->owner;
1631
1632 *size_change_ok = TRUE;
1633 *type_change_ok = TRUE;
1634
1635 if (hi->root.type == bfd_link_hash_indirect)
1636 flip = hi;
1637 else
1638 h->verinfo.vertree = NULL;
1639 }
1640
1641 if (flip != NULL)
1642 {
1643 /* Handle the case where we had a versioned symbol in a dynamic
1644 library and now find a definition in a normal object. In this
1645 case, we make the versioned symbol point to the normal one. */
1646 flip->root.type = h->root.type;
1647 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1648 h->root.type = bfd_link_hash_indirect;
1649 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1650 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1651 if (h->def_dynamic)
1652 {
1653 h->def_dynamic = 0;
1654 flip->ref_dynamic = 1;
1655 }
1656 }
1657
1658 return TRUE;
1659 }
1660
1661 /* This function is called to create an indirect symbol from the
1662 default for the symbol with the default version if needed. The
1663 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1664 set DYNSYM if the new indirect symbol is dynamic. */
1665
1666 static bfd_boolean
1667 _bfd_elf_add_default_symbol (bfd *abfd,
1668 struct bfd_link_info *info,
1669 struct elf_link_hash_entry *h,
1670 const char *name,
1671 Elf_Internal_Sym *sym,
1672 asection *sec,
1673 bfd_vma value,
1674 bfd **poldbfd,
1675 bfd_boolean *dynsym)
1676 {
1677 bfd_boolean type_change_ok;
1678 bfd_boolean size_change_ok;
1679 bfd_boolean skip;
1680 char *shortname;
1681 struct elf_link_hash_entry *hi;
1682 struct bfd_link_hash_entry *bh;
1683 const struct elf_backend_data *bed;
1684 bfd_boolean collect;
1685 bfd_boolean dynamic;
1686 bfd_boolean override;
1687 char *p;
1688 size_t len, shortlen;
1689 asection *tmp_sec;
1690 bfd_boolean matched;
1691
1692 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1693 return TRUE;
1694
1695 /* If this symbol has a version, and it is the default version, we
1696 create an indirect symbol from the default name to the fully
1697 decorated name. This will cause external references which do not
1698 specify a version to be bound to this version of the symbol. */
1699 p = strchr (name, ELF_VER_CHR);
1700 if (h->versioned == unknown)
1701 {
1702 if (p == NULL)
1703 {
1704 h->versioned = unversioned;
1705 return TRUE;
1706 }
1707 else
1708 {
1709 if (p[1] != ELF_VER_CHR)
1710 {
1711 h->versioned = versioned_hidden;
1712 return TRUE;
1713 }
1714 else
1715 h->versioned = versioned;
1716 }
1717 }
1718
1719 bed = get_elf_backend_data (abfd);
1720 collect = bed->collect;
1721 dynamic = (abfd->flags & DYNAMIC) != 0;
1722
1723 shortlen = p - name;
1724 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1725 if (shortname == NULL)
1726 return FALSE;
1727 memcpy (shortname, name, shortlen);
1728 shortname[shortlen] = '\0';
1729
1730 /* We are going to create a new symbol. Merge it with any existing
1731 symbol with this name. For the purposes of the merge, act as
1732 though we were defining the symbol we just defined, although we
1733 actually going to define an indirect symbol. */
1734 type_change_ok = FALSE;
1735 size_change_ok = FALSE;
1736 matched = TRUE;
1737 tmp_sec = sec;
1738 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1739 &hi, poldbfd, NULL, NULL, &skip, &override,
1740 &type_change_ok, &size_change_ok, &matched))
1741 return FALSE;
1742
1743 if (skip)
1744 goto nondefault;
1745
1746 if (! override)
1747 {
1748 /* Add the default symbol if not performing a relocatable link. */
1749 if (! bfd_link_relocatable (info))
1750 {
1751 bh = &hi->root;
1752 if (! (_bfd_generic_link_add_one_symbol
1753 (info, abfd, shortname, BSF_INDIRECT,
1754 bfd_ind_section_ptr,
1755 0, name, FALSE, collect, &bh)))
1756 return FALSE;
1757 hi = (struct elf_link_hash_entry *) bh;
1758 }
1759 }
1760 else
1761 {
1762 /* In this case the symbol named SHORTNAME is overriding the
1763 indirect symbol we want to add. We were planning on making
1764 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1765 is the name without a version. NAME is the fully versioned
1766 name, and it is the default version.
1767
1768 Overriding means that we already saw a definition for the
1769 symbol SHORTNAME in a regular object, and it is overriding
1770 the symbol defined in the dynamic object.
1771
1772 When this happens, we actually want to change NAME, the
1773 symbol we just added, to refer to SHORTNAME. This will cause
1774 references to NAME in the shared object to become references
1775 to SHORTNAME in the regular object. This is what we expect
1776 when we override a function in a shared object: that the
1777 references in the shared object will be mapped to the
1778 definition in the regular object. */
1779
1780 while (hi->root.type == bfd_link_hash_indirect
1781 || hi->root.type == bfd_link_hash_warning)
1782 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1783
1784 h->root.type = bfd_link_hash_indirect;
1785 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1786 if (h->def_dynamic)
1787 {
1788 h->def_dynamic = 0;
1789 hi->ref_dynamic = 1;
1790 if (hi->ref_regular
1791 || hi->def_regular)
1792 {
1793 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1794 return FALSE;
1795 }
1796 }
1797
1798 /* Now set HI to H, so that the following code will set the
1799 other fields correctly. */
1800 hi = h;
1801 }
1802
1803 /* Check if HI is a warning symbol. */
1804 if (hi->root.type == bfd_link_hash_warning)
1805 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1806
1807 /* If there is a duplicate definition somewhere, then HI may not
1808 point to an indirect symbol. We will have reported an error to
1809 the user in that case. */
1810
1811 if (hi->root.type == bfd_link_hash_indirect)
1812 {
1813 struct elf_link_hash_entry *ht;
1814
1815 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1816 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1817
1818 /* A reference to the SHORTNAME symbol from a dynamic library
1819 will be satisfied by the versioned symbol at runtime. In
1820 effect, we have a reference to the versioned symbol. */
1821 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1822 hi->dynamic_def |= ht->dynamic_def;
1823
1824 /* See if the new flags lead us to realize that the symbol must
1825 be dynamic. */
1826 if (! *dynsym)
1827 {
1828 if (! dynamic)
1829 {
1830 if (! bfd_link_executable (info)
1831 || hi->def_dynamic
1832 || hi->ref_dynamic)
1833 *dynsym = TRUE;
1834 }
1835 else
1836 {
1837 if (hi->ref_regular)
1838 *dynsym = TRUE;
1839 }
1840 }
1841 }
1842
1843 /* We also need to define an indirection from the nondefault version
1844 of the symbol. */
1845
1846 nondefault:
1847 len = strlen (name);
1848 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
1849 if (shortname == NULL)
1850 return FALSE;
1851 memcpy (shortname, name, shortlen);
1852 memcpy (shortname + shortlen, p + 1, len - shortlen);
1853
1854 /* Once again, merge with any existing symbol. */
1855 type_change_ok = FALSE;
1856 size_change_ok = FALSE;
1857 tmp_sec = sec;
1858 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1859 &hi, poldbfd, NULL, NULL, &skip, &override,
1860 &type_change_ok, &size_change_ok, &matched))
1861 return FALSE;
1862
1863 if (skip)
1864 return TRUE;
1865
1866 if (override)
1867 {
1868 /* Here SHORTNAME is a versioned name, so we don't expect to see
1869 the type of override we do in the case above unless it is
1870 overridden by a versioned definition. */
1871 if (hi->root.type != bfd_link_hash_defined
1872 && hi->root.type != bfd_link_hash_defweak)
1873 (*_bfd_error_handler)
1874 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1875 abfd, shortname);
1876 }
1877 else
1878 {
1879 bh = &hi->root;
1880 if (! (_bfd_generic_link_add_one_symbol
1881 (info, abfd, shortname, BSF_INDIRECT,
1882 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1883 return FALSE;
1884 hi = (struct elf_link_hash_entry *) bh;
1885
1886 /* If there is a duplicate definition somewhere, then HI may not
1887 point to an indirect symbol. We will have reported an error
1888 to the user in that case. */
1889
1890 if (hi->root.type == bfd_link_hash_indirect)
1891 {
1892 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1893 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1894 hi->dynamic_def |= h->dynamic_def;
1895
1896 /* See if the new flags lead us to realize that the symbol
1897 must be dynamic. */
1898 if (! *dynsym)
1899 {
1900 if (! dynamic)
1901 {
1902 if (! bfd_link_executable (info)
1903 || hi->ref_dynamic)
1904 *dynsym = TRUE;
1905 }
1906 else
1907 {
1908 if (hi->ref_regular)
1909 *dynsym = TRUE;
1910 }
1911 }
1912 }
1913 }
1914
1915 return TRUE;
1916 }
1917 \f
1918 /* This routine is used to export all defined symbols into the dynamic
1919 symbol table. It is called via elf_link_hash_traverse. */
1920
1921 static bfd_boolean
1922 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1923 {
1924 struct elf_info_failed *eif = (struct elf_info_failed *) data;
1925
1926 /* Ignore indirect symbols. These are added by the versioning code. */
1927 if (h->root.type == bfd_link_hash_indirect)
1928 return TRUE;
1929
1930 /* Ignore this if we won't export it. */
1931 if (!eif->info->export_dynamic && !h->dynamic)
1932 return TRUE;
1933
1934 if (h->dynindx == -1
1935 && (h->def_regular || h->ref_regular)
1936 && ! bfd_hide_sym_by_version (eif->info->version_info,
1937 h->root.root.string))
1938 {
1939 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1940 {
1941 eif->failed = TRUE;
1942 return FALSE;
1943 }
1944 }
1945
1946 return TRUE;
1947 }
1948 \f
1949 /* Look through the symbols which are defined in other shared
1950 libraries and referenced here. Update the list of version
1951 dependencies. This will be put into the .gnu.version_r section.
1952 This function is called via elf_link_hash_traverse. */
1953
1954 static bfd_boolean
1955 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1956 void *data)
1957 {
1958 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
1959 Elf_Internal_Verneed *t;
1960 Elf_Internal_Vernaux *a;
1961 bfd_size_type amt;
1962
1963 /* We only care about symbols defined in shared objects with version
1964 information. */
1965 if (!h->def_dynamic
1966 || h->def_regular
1967 || h->dynindx == -1
1968 || h->verinfo.verdef == NULL
1969 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
1970 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
1971 return TRUE;
1972
1973 /* See if we already know about this version. */
1974 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
1975 t != NULL;
1976 t = t->vn_nextref)
1977 {
1978 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1979 continue;
1980
1981 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1982 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1983 return TRUE;
1984
1985 break;
1986 }
1987
1988 /* This is a new version. Add it to tree we are building. */
1989
1990 if (t == NULL)
1991 {
1992 amt = sizeof *t;
1993 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
1994 if (t == NULL)
1995 {
1996 rinfo->failed = TRUE;
1997 return FALSE;
1998 }
1999
2000 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2001 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2002 elf_tdata (rinfo->info->output_bfd)->verref = t;
2003 }
2004
2005 amt = sizeof *a;
2006 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2007 if (a == NULL)
2008 {
2009 rinfo->failed = TRUE;
2010 return FALSE;
2011 }
2012
2013 /* Note that we are copying a string pointer here, and testing it
2014 above. If bfd_elf_string_from_elf_section is ever changed to
2015 discard the string data when low in memory, this will have to be
2016 fixed. */
2017 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2018
2019 a->vna_flags = h->verinfo.verdef->vd_flags;
2020 a->vna_nextptr = t->vn_auxptr;
2021
2022 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2023 ++rinfo->vers;
2024
2025 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2026
2027 t->vn_auxptr = a;
2028
2029 return TRUE;
2030 }
2031
2032 /* Figure out appropriate versions for all the symbols. We may not
2033 have the version number script until we have read all of the input
2034 files, so until that point we don't know which symbols should be
2035 local. This function is called via elf_link_hash_traverse. */
2036
2037 static bfd_boolean
2038 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2039 {
2040 struct elf_info_failed *sinfo;
2041 struct bfd_link_info *info;
2042 const struct elf_backend_data *bed;
2043 struct elf_info_failed eif;
2044 char *p;
2045 bfd_size_type amt;
2046
2047 sinfo = (struct elf_info_failed *) data;
2048 info = sinfo->info;
2049
2050 /* Fix the symbol flags. */
2051 eif.failed = FALSE;
2052 eif.info = info;
2053 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2054 {
2055 if (eif.failed)
2056 sinfo->failed = TRUE;
2057 return FALSE;
2058 }
2059
2060 /* We only need version numbers for symbols defined in regular
2061 objects. */
2062 if (!h->def_regular)
2063 return TRUE;
2064
2065 bed = get_elf_backend_data (info->output_bfd);
2066 p = strchr (h->root.root.string, ELF_VER_CHR);
2067 if (p != NULL && h->verinfo.vertree == NULL)
2068 {
2069 struct bfd_elf_version_tree *t;
2070
2071 ++p;
2072 if (*p == ELF_VER_CHR)
2073 ++p;
2074
2075 /* If there is no version string, we can just return out. */
2076 if (*p == '\0')
2077 return TRUE;
2078
2079 /* Look for the version. If we find it, it is no longer weak. */
2080 for (t = sinfo->info->version_info; t != NULL; t = t->next)
2081 {
2082 if (strcmp (t->name, p) == 0)
2083 {
2084 size_t len;
2085 char *alc;
2086 struct bfd_elf_version_expr *d;
2087
2088 len = p - h->root.root.string;
2089 alc = (char *) bfd_malloc (len);
2090 if (alc == NULL)
2091 {
2092 sinfo->failed = TRUE;
2093 return FALSE;
2094 }
2095 memcpy (alc, h->root.root.string, len - 1);
2096 alc[len - 1] = '\0';
2097 if (alc[len - 2] == ELF_VER_CHR)
2098 alc[len - 2] = '\0';
2099
2100 h->verinfo.vertree = t;
2101 t->used = TRUE;
2102 d = NULL;
2103
2104 if (t->globals.list != NULL)
2105 d = (*t->match) (&t->globals, NULL, alc);
2106
2107 /* See if there is anything to force this symbol to
2108 local scope. */
2109 if (d == NULL && t->locals.list != NULL)
2110 {
2111 d = (*t->match) (&t->locals, NULL, alc);
2112 if (d != NULL
2113 && h->dynindx != -1
2114 && ! info->export_dynamic)
2115 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2116 }
2117
2118 free (alc);
2119 break;
2120 }
2121 }
2122
2123 /* If we are building an application, we need to create a
2124 version node for this version. */
2125 if (t == NULL && bfd_link_executable (info))
2126 {
2127 struct bfd_elf_version_tree **pp;
2128 int version_index;
2129
2130 /* If we aren't going to export this symbol, we don't need
2131 to worry about it. */
2132 if (h->dynindx == -1)
2133 return TRUE;
2134
2135 amt = sizeof *t;
2136 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, amt);
2137 if (t == NULL)
2138 {
2139 sinfo->failed = TRUE;
2140 return FALSE;
2141 }
2142
2143 t->name = p;
2144 t->name_indx = (unsigned int) -1;
2145 t->used = TRUE;
2146
2147 version_index = 1;
2148 /* Don't count anonymous version tag. */
2149 if (sinfo->info->version_info != NULL
2150 && sinfo->info->version_info->vernum == 0)
2151 version_index = 0;
2152 for (pp = &sinfo->info->version_info;
2153 *pp != NULL;
2154 pp = &(*pp)->next)
2155 ++version_index;
2156 t->vernum = version_index;
2157
2158 *pp = t;
2159
2160 h->verinfo.vertree = t;
2161 }
2162 else if (t == NULL)
2163 {
2164 /* We could not find the version for a symbol when
2165 generating a shared archive. Return an error. */
2166 (*_bfd_error_handler)
2167 (_("%B: version node not found for symbol %s"),
2168 info->output_bfd, h->root.root.string);
2169 bfd_set_error (bfd_error_bad_value);
2170 sinfo->failed = TRUE;
2171 return FALSE;
2172 }
2173 }
2174
2175 /* If we don't have a version for this symbol, see if we can find
2176 something. */
2177 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
2178 {
2179 bfd_boolean hide;
2180
2181 h->verinfo.vertree
2182 = bfd_find_version_for_sym (sinfo->info->version_info,
2183 h->root.root.string, &hide);
2184 if (h->verinfo.vertree != NULL && hide)
2185 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2186 }
2187
2188 return TRUE;
2189 }
2190 \f
2191 /* Read and swap the relocs from the section indicated by SHDR. This
2192 may be either a REL or a RELA section. The relocations are
2193 translated into RELA relocations and stored in INTERNAL_RELOCS,
2194 which should have already been allocated to contain enough space.
2195 The EXTERNAL_RELOCS are a buffer where the external form of the
2196 relocations should be stored.
2197
2198 Returns FALSE if something goes wrong. */
2199
2200 static bfd_boolean
2201 elf_link_read_relocs_from_section (bfd *abfd,
2202 asection *sec,
2203 Elf_Internal_Shdr *shdr,
2204 void *external_relocs,
2205 Elf_Internal_Rela *internal_relocs)
2206 {
2207 const struct elf_backend_data *bed;
2208 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2209 const bfd_byte *erela;
2210 const bfd_byte *erelaend;
2211 Elf_Internal_Rela *irela;
2212 Elf_Internal_Shdr *symtab_hdr;
2213 size_t nsyms;
2214
2215 /* Position ourselves at the start of the section. */
2216 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2217 return FALSE;
2218
2219 /* Read the relocations. */
2220 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2221 return FALSE;
2222
2223 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2224 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2225
2226 bed = get_elf_backend_data (abfd);
2227
2228 /* Convert the external relocations to the internal format. */
2229 if (shdr->sh_entsize == bed->s->sizeof_rel)
2230 swap_in = bed->s->swap_reloc_in;
2231 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2232 swap_in = bed->s->swap_reloca_in;
2233 else
2234 {
2235 bfd_set_error (bfd_error_wrong_format);
2236 return FALSE;
2237 }
2238
2239 erela = (const bfd_byte *) external_relocs;
2240 erelaend = erela + shdr->sh_size;
2241 irela = internal_relocs;
2242 while (erela < erelaend)
2243 {
2244 bfd_vma r_symndx;
2245
2246 (*swap_in) (abfd, erela, irela);
2247 r_symndx = ELF32_R_SYM (irela->r_info);
2248 if (bed->s->arch_size == 64)
2249 r_symndx >>= 24;
2250 if (nsyms > 0)
2251 {
2252 if ((size_t) r_symndx >= nsyms)
2253 {
2254 (*_bfd_error_handler)
2255 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2256 " for offset 0x%lx in section `%A'"),
2257 abfd, sec,
2258 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2259 bfd_set_error (bfd_error_bad_value);
2260 return FALSE;
2261 }
2262 }
2263 else if (r_symndx != STN_UNDEF)
2264 {
2265 (*_bfd_error_handler)
2266 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2267 " when the object file has no symbol table"),
2268 abfd, sec,
2269 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2270 bfd_set_error (bfd_error_bad_value);
2271 return FALSE;
2272 }
2273 irela += bed->s->int_rels_per_ext_rel;
2274 erela += shdr->sh_entsize;
2275 }
2276
2277 return TRUE;
2278 }
2279
2280 /* Read and swap the relocs for a section O. They may have been
2281 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2282 not NULL, they are used as buffers to read into. They are known to
2283 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2284 the return value is allocated using either malloc or bfd_alloc,
2285 according to the KEEP_MEMORY argument. If O has two relocation
2286 sections (both REL and RELA relocations), then the REL_HDR
2287 relocations will appear first in INTERNAL_RELOCS, followed by the
2288 RELA_HDR relocations. */
2289
2290 Elf_Internal_Rela *
2291 _bfd_elf_link_read_relocs (bfd *abfd,
2292 asection *o,
2293 void *external_relocs,
2294 Elf_Internal_Rela *internal_relocs,
2295 bfd_boolean keep_memory)
2296 {
2297 void *alloc1 = NULL;
2298 Elf_Internal_Rela *alloc2 = NULL;
2299 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2300 struct bfd_elf_section_data *esdo = elf_section_data (o);
2301 Elf_Internal_Rela *internal_rela_relocs;
2302
2303 if (esdo->relocs != NULL)
2304 return esdo->relocs;
2305
2306 if (o->reloc_count == 0)
2307 return NULL;
2308
2309 if (internal_relocs == NULL)
2310 {
2311 bfd_size_type size;
2312
2313 size = o->reloc_count;
2314 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2315 if (keep_memory)
2316 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2317 else
2318 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2319 if (internal_relocs == NULL)
2320 goto error_return;
2321 }
2322
2323 if (external_relocs == NULL)
2324 {
2325 bfd_size_type size = 0;
2326
2327 if (esdo->rel.hdr)
2328 size += esdo->rel.hdr->sh_size;
2329 if (esdo->rela.hdr)
2330 size += esdo->rela.hdr->sh_size;
2331
2332 alloc1 = bfd_malloc (size);
2333 if (alloc1 == NULL)
2334 goto error_return;
2335 external_relocs = alloc1;
2336 }
2337
2338 internal_rela_relocs = internal_relocs;
2339 if (esdo->rel.hdr)
2340 {
2341 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2342 external_relocs,
2343 internal_relocs))
2344 goto error_return;
2345 external_relocs = (((bfd_byte *) external_relocs)
2346 + esdo->rel.hdr->sh_size);
2347 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2348 * bed->s->int_rels_per_ext_rel);
2349 }
2350
2351 if (esdo->rela.hdr
2352 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2353 external_relocs,
2354 internal_rela_relocs)))
2355 goto error_return;
2356
2357 /* Cache the results for next time, if we can. */
2358 if (keep_memory)
2359 esdo->relocs = internal_relocs;
2360
2361 if (alloc1 != NULL)
2362 free (alloc1);
2363
2364 /* Don't free alloc2, since if it was allocated we are passing it
2365 back (under the name of internal_relocs). */
2366
2367 return internal_relocs;
2368
2369 error_return:
2370 if (alloc1 != NULL)
2371 free (alloc1);
2372 if (alloc2 != NULL)
2373 {
2374 if (keep_memory)
2375 bfd_release (abfd, alloc2);
2376 else
2377 free (alloc2);
2378 }
2379 return NULL;
2380 }
2381
2382 /* Compute the size of, and allocate space for, REL_HDR which is the
2383 section header for a section containing relocations for O. */
2384
2385 static bfd_boolean
2386 _bfd_elf_link_size_reloc_section (bfd *abfd,
2387 struct bfd_elf_section_reloc_data *reldata)
2388 {
2389 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2390
2391 /* That allows us to calculate the size of the section. */
2392 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2393
2394 /* The contents field must last into write_object_contents, so we
2395 allocate it with bfd_alloc rather than malloc. Also since we
2396 cannot be sure that the contents will actually be filled in,
2397 we zero the allocated space. */
2398 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2399 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2400 return FALSE;
2401
2402 if (reldata->hashes == NULL && reldata->count)
2403 {
2404 struct elf_link_hash_entry **p;
2405
2406 p = ((struct elf_link_hash_entry **)
2407 bfd_zmalloc (reldata->count * sizeof (*p)));
2408 if (p == NULL)
2409 return FALSE;
2410
2411 reldata->hashes = p;
2412 }
2413
2414 return TRUE;
2415 }
2416
2417 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2418 originated from the section given by INPUT_REL_HDR) to the
2419 OUTPUT_BFD. */
2420
2421 bfd_boolean
2422 _bfd_elf_link_output_relocs (bfd *output_bfd,
2423 asection *input_section,
2424 Elf_Internal_Shdr *input_rel_hdr,
2425 Elf_Internal_Rela *internal_relocs,
2426 struct elf_link_hash_entry **rel_hash
2427 ATTRIBUTE_UNUSED)
2428 {
2429 Elf_Internal_Rela *irela;
2430 Elf_Internal_Rela *irelaend;
2431 bfd_byte *erel;
2432 struct bfd_elf_section_reloc_data *output_reldata;
2433 asection *output_section;
2434 const struct elf_backend_data *bed;
2435 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2436 struct bfd_elf_section_data *esdo;
2437
2438 output_section = input_section->output_section;
2439
2440 bed = get_elf_backend_data (output_bfd);
2441 esdo = elf_section_data (output_section);
2442 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2443 {
2444 output_reldata = &esdo->rel;
2445 swap_out = bed->s->swap_reloc_out;
2446 }
2447 else if (esdo->rela.hdr
2448 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2449 {
2450 output_reldata = &esdo->rela;
2451 swap_out = bed->s->swap_reloca_out;
2452 }
2453 else
2454 {
2455 (*_bfd_error_handler)
2456 (_("%B: relocation size mismatch in %B section %A"),
2457 output_bfd, input_section->owner, input_section);
2458 bfd_set_error (bfd_error_wrong_format);
2459 return FALSE;
2460 }
2461
2462 erel = output_reldata->hdr->contents;
2463 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2464 irela = internal_relocs;
2465 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2466 * bed->s->int_rels_per_ext_rel);
2467 while (irela < irelaend)
2468 {
2469 (*swap_out) (output_bfd, irela, erel);
2470 irela += bed->s->int_rels_per_ext_rel;
2471 erel += input_rel_hdr->sh_entsize;
2472 }
2473
2474 /* Bump the counter, so that we know where to add the next set of
2475 relocations. */
2476 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2477
2478 return TRUE;
2479 }
2480 \f
2481 /* Make weak undefined symbols in PIE dynamic. */
2482
2483 bfd_boolean
2484 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2485 struct elf_link_hash_entry *h)
2486 {
2487 if (bfd_link_pie (info)
2488 && h->dynindx == -1
2489 && h->root.type == bfd_link_hash_undefweak)
2490 return bfd_elf_link_record_dynamic_symbol (info, h);
2491
2492 return TRUE;
2493 }
2494
2495 /* Fix up the flags for a symbol. This handles various cases which
2496 can only be fixed after all the input files are seen. This is
2497 currently called by both adjust_dynamic_symbol and
2498 assign_sym_version, which is unnecessary but perhaps more robust in
2499 the face of future changes. */
2500
2501 static bfd_boolean
2502 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2503 struct elf_info_failed *eif)
2504 {
2505 const struct elf_backend_data *bed;
2506
2507 /* If this symbol was mentioned in a non-ELF file, try to set
2508 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2509 permit a non-ELF file to correctly refer to a symbol defined in
2510 an ELF dynamic object. */
2511 if (h->non_elf)
2512 {
2513 while (h->root.type == bfd_link_hash_indirect)
2514 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2515
2516 if (h->root.type != bfd_link_hash_defined
2517 && h->root.type != bfd_link_hash_defweak)
2518 {
2519 h->ref_regular = 1;
2520 h->ref_regular_nonweak = 1;
2521 }
2522 else
2523 {
2524 if (h->root.u.def.section->owner != NULL
2525 && (bfd_get_flavour (h->root.u.def.section->owner)
2526 == bfd_target_elf_flavour))
2527 {
2528 h->ref_regular = 1;
2529 h->ref_regular_nonweak = 1;
2530 }
2531 else
2532 h->def_regular = 1;
2533 }
2534
2535 if (h->dynindx == -1
2536 && (h->def_dynamic
2537 || h->ref_dynamic))
2538 {
2539 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2540 {
2541 eif->failed = TRUE;
2542 return FALSE;
2543 }
2544 }
2545 }
2546 else
2547 {
2548 /* Unfortunately, NON_ELF is only correct if the symbol
2549 was first seen in a non-ELF file. Fortunately, if the symbol
2550 was first seen in an ELF file, we're probably OK unless the
2551 symbol was defined in a non-ELF file. Catch that case here.
2552 FIXME: We're still in trouble if the symbol was first seen in
2553 a dynamic object, and then later in a non-ELF regular object. */
2554 if ((h->root.type == bfd_link_hash_defined
2555 || h->root.type == bfd_link_hash_defweak)
2556 && !h->def_regular
2557 && (h->root.u.def.section->owner != NULL
2558 ? (bfd_get_flavour (h->root.u.def.section->owner)
2559 != bfd_target_elf_flavour)
2560 : (bfd_is_abs_section (h->root.u.def.section)
2561 && !h->def_dynamic)))
2562 h->def_regular = 1;
2563 }
2564
2565 /* Backend specific symbol fixup. */
2566 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2567 if (bed->elf_backend_fixup_symbol
2568 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2569 return FALSE;
2570
2571 /* If this is a final link, and the symbol was defined as a common
2572 symbol in a regular object file, and there was no definition in
2573 any dynamic object, then the linker will have allocated space for
2574 the symbol in a common section but the DEF_REGULAR
2575 flag will not have been set. */
2576 if (h->root.type == bfd_link_hash_defined
2577 && !h->def_regular
2578 && h->ref_regular
2579 && !h->def_dynamic
2580 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2581 h->def_regular = 1;
2582
2583 /* If -Bsymbolic was used (which means to bind references to global
2584 symbols to the definition within the shared object), and this
2585 symbol was defined in a regular object, then it actually doesn't
2586 need a PLT entry. Likewise, if the symbol has non-default
2587 visibility. If the symbol has hidden or internal visibility, we
2588 will force it local. */
2589 if (h->needs_plt
2590 && bfd_link_pic (eif->info)
2591 && is_elf_hash_table (eif->info->hash)
2592 && (SYMBOLIC_BIND (eif->info, h)
2593 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2594 && h->def_regular)
2595 {
2596 bfd_boolean force_local;
2597
2598 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2599 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2600 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2601 }
2602
2603 /* If a weak undefined symbol has non-default visibility, we also
2604 hide it from the dynamic linker. */
2605 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2606 && h->root.type == bfd_link_hash_undefweak)
2607 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2608
2609 /* If this is a weak defined symbol in a dynamic object, and we know
2610 the real definition in the dynamic object, copy interesting flags
2611 over to the real definition. */
2612 if (h->u.weakdef != NULL)
2613 {
2614 /* If the real definition is defined by a regular object file,
2615 don't do anything special. See the longer description in
2616 _bfd_elf_adjust_dynamic_symbol, below. */
2617 if (h->u.weakdef->def_regular)
2618 h->u.weakdef = NULL;
2619 else
2620 {
2621 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2622
2623 while (h->root.type == bfd_link_hash_indirect)
2624 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2625
2626 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2627 || h->root.type == bfd_link_hash_defweak);
2628 BFD_ASSERT (weakdef->def_dynamic);
2629 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2630 || weakdef->root.type == bfd_link_hash_defweak);
2631 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2632 }
2633 }
2634
2635 return TRUE;
2636 }
2637
2638 /* Make the backend pick a good value for a dynamic symbol. This is
2639 called via elf_link_hash_traverse, and also calls itself
2640 recursively. */
2641
2642 static bfd_boolean
2643 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2644 {
2645 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2646 bfd *dynobj;
2647 const struct elf_backend_data *bed;
2648
2649 if (! is_elf_hash_table (eif->info->hash))
2650 return FALSE;
2651
2652 /* Ignore indirect symbols. These are added by the versioning code. */
2653 if (h->root.type == bfd_link_hash_indirect)
2654 return TRUE;
2655
2656 /* Fix the symbol flags. */
2657 if (! _bfd_elf_fix_symbol_flags (h, eif))
2658 return FALSE;
2659
2660 /* If this symbol does not require a PLT entry, and it is not
2661 defined by a dynamic object, or is not referenced by a regular
2662 object, ignore it. We do have to handle a weak defined symbol,
2663 even if no regular object refers to it, if we decided to add it
2664 to the dynamic symbol table. FIXME: Do we normally need to worry
2665 about symbols which are defined by one dynamic object and
2666 referenced by another one? */
2667 if (!h->needs_plt
2668 && h->type != STT_GNU_IFUNC
2669 && (h->def_regular
2670 || !h->def_dynamic
2671 || (!h->ref_regular
2672 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2673 {
2674 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2675 return TRUE;
2676 }
2677
2678 /* If we've already adjusted this symbol, don't do it again. This
2679 can happen via a recursive call. */
2680 if (h->dynamic_adjusted)
2681 return TRUE;
2682
2683 /* Don't look at this symbol again. Note that we must set this
2684 after checking the above conditions, because we may look at a
2685 symbol once, decide not to do anything, and then get called
2686 recursively later after REF_REGULAR is set below. */
2687 h->dynamic_adjusted = 1;
2688
2689 /* If this is a weak definition, and we know a real definition, and
2690 the real symbol is not itself defined by a regular object file,
2691 then get a good value for the real definition. We handle the
2692 real symbol first, for the convenience of the backend routine.
2693
2694 Note that there is a confusing case here. If the real definition
2695 is defined by a regular object file, we don't get the real symbol
2696 from the dynamic object, but we do get the weak symbol. If the
2697 processor backend uses a COPY reloc, then if some routine in the
2698 dynamic object changes the real symbol, we will not see that
2699 change in the corresponding weak symbol. This is the way other
2700 ELF linkers work as well, and seems to be a result of the shared
2701 library model.
2702
2703 I will clarify this issue. Most SVR4 shared libraries define the
2704 variable _timezone and define timezone as a weak synonym. The
2705 tzset call changes _timezone. If you write
2706 extern int timezone;
2707 int _timezone = 5;
2708 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2709 you might expect that, since timezone is a synonym for _timezone,
2710 the same number will print both times. However, if the processor
2711 backend uses a COPY reloc, then actually timezone will be copied
2712 into your process image, and, since you define _timezone
2713 yourself, _timezone will not. Thus timezone and _timezone will
2714 wind up at different memory locations. The tzset call will set
2715 _timezone, leaving timezone unchanged. */
2716
2717 if (h->u.weakdef != NULL)
2718 {
2719 /* If we get to this point, there is an implicit reference to
2720 H->U.WEAKDEF by a regular object file via the weak symbol H. */
2721 h->u.weakdef->ref_regular = 1;
2722
2723 /* Ensure that the backend adjust_dynamic_symbol function sees
2724 H->U.WEAKDEF before H by recursively calling ourselves. */
2725 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2726 return FALSE;
2727 }
2728
2729 /* If a symbol has no type and no size and does not require a PLT
2730 entry, then we are probably about to do the wrong thing here: we
2731 are probably going to create a COPY reloc for an empty object.
2732 This case can arise when a shared object is built with assembly
2733 code, and the assembly code fails to set the symbol type. */
2734 if (h->size == 0
2735 && h->type == STT_NOTYPE
2736 && !h->needs_plt)
2737 (*_bfd_error_handler)
2738 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2739 h->root.root.string);
2740
2741 dynobj = elf_hash_table (eif->info)->dynobj;
2742 bed = get_elf_backend_data (dynobj);
2743
2744 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2745 {
2746 eif->failed = TRUE;
2747 return FALSE;
2748 }
2749
2750 return TRUE;
2751 }
2752
2753 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2754 DYNBSS. */
2755
2756 bfd_boolean
2757 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2758 struct elf_link_hash_entry *h,
2759 asection *dynbss)
2760 {
2761 unsigned int power_of_two;
2762 bfd_vma mask;
2763 asection *sec = h->root.u.def.section;
2764
2765 /* The section aligment of definition is the maximum alignment
2766 requirement of symbols defined in the section. Since we don't
2767 know the symbol alignment requirement, we start with the
2768 maximum alignment and check low bits of the symbol address
2769 for the minimum alignment. */
2770 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2771 mask = ((bfd_vma) 1 << power_of_two) - 1;
2772 while ((h->root.u.def.value & mask) != 0)
2773 {
2774 mask >>= 1;
2775 --power_of_two;
2776 }
2777
2778 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2779 dynbss))
2780 {
2781 /* Adjust the section alignment if needed. */
2782 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2783 power_of_two))
2784 return FALSE;
2785 }
2786
2787 /* We make sure that the symbol will be aligned properly. */
2788 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2789
2790 /* Define the symbol as being at this point in DYNBSS. */
2791 h->root.u.def.section = dynbss;
2792 h->root.u.def.value = dynbss->size;
2793
2794 /* Increment the size of DYNBSS to make room for the symbol. */
2795 dynbss->size += h->size;
2796
2797 /* No error if extern_protected_data is true. */
2798 if (h->protected_def
2799 && (!info->extern_protected_data
2800 || (info->extern_protected_data < 0
2801 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
2802 info->callbacks->einfo
2803 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2804 h->root.root.string);
2805
2806 return TRUE;
2807 }
2808
2809 /* Adjust all external symbols pointing into SEC_MERGE sections
2810 to reflect the object merging within the sections. */
2811
2812 static bfd_boolean
2813 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2814 {
2815 asection *sec;
2816
2817 if ((h->root.type == bfd_link_hash_defined
2818 || h->root.type == bfd_link_hash_defweak)
2819 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2820 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
2821 {
2822 bfd *output_bfd = (bfd *) data;
2823
2824 h->root.u.def.value =
2825 _bfd_merged_section_offset (output_bfd,
2826 &h->root.u.def.section,
2827 elf_section_data (sec)->sec_info,
2828 h->root.u.def.value);
2829 }
2830
2831 return TRUE;
2832 }
2833
2834 /* Returns false if the symbol referred to by H should be considered
2835 to resolve local to the current module, and true if it should be
2836 considered to bind dynamically. */
2837
2838 bfd_boolean
2839 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2840 struct bfd_link_info *info,
2841 bfd_boolean not_local_protected)
2842 {
2843 bfd_boolean binding_stays_local_p;
2844 const struct elf_backend_data *bed;
2845 struct elf_link_hash_table *hash_table;
2846
2847 if (h == NULL)
2848 return FALSE;
2849
2850 while (h->root.type == bfd_link_hash_indirect
2851 || h->root.type == bfd_link_hash_warning)
2852 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2853
2854 /* If it was forced local, then clearly it's not dynamic. */
2855 if (h->dynindx == -1)
2856 return FALSE;
2857 if (h->forced_local)
2858 return FALSE;
2859
2860 /* Identify the cases where name binding rules say that a
2861 visible symbol resolves locally. */
2862 binding_stays_local_p = (bfd_link_executable (info)
2863 || SYMBOLIC_BIND (info, h));
2864
2865 switch (ELF_ST_VISIBILITY (h->other))
2866 {
2867 case STV_INTERNAL:
2868 case STV_HIDDEN:
2869 return FALSE;
2870
2871 case STV_PROTECTED:
2872 hash_table = elf_hash_table (info);
2873 if (!is_elf_hash_table (hash_table))
2874 return FALSE;
2875
2876 bed = get_elf_backend_data (hash_table->dynobj);
2877
2878 /* Proper resolution for function pointer equality may require
2879 that these symbols perhaps be resolved dynamically, even though
2880 we should be resolving them to the current module. */
2881 if (!not_local_protected || !bed->is_function_type (h->type))
2882 binding_stays_local_p = TRUE;
2883 break;
2884
2885 default:
2886 break;
2887 }
2888
2889 /* If it isn't defined locally, then clearly it's dynamic. */
2890 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2891 return TRUE;
2892
2893 /* Otherwise, the symbol is dynamic if binding rules don't tell
2894 us that it remains local. */
2895 return !binding_stays_local_p;
2896 }
2897
2898 /* Return true if the symbol referred to by H should be considered
2899 to resolve local to the current module, and false otherwise. Differs
2900 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2901 undefined symbols. The two functions are virtually identical except
2902 for the place where forced_local and dynindx == -1 are tested. If
2903 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2904 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2905 the symbol is local only for defined symbols.
2906 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2907 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2908 treatment of undefined weak symbols. For those that do not make
2909 undefined weak symbols dynamic, both functions may return false. */
2910
2911 bfd_boolean
2912 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2913 struct bfd_link_info *info,
2914 bfd_boolean local_protected)
2915 {
2916 const struct elf_backend_data *bed;
2917 struct elf_link_hash_table *hash_table;
2918
2919 /* If it's a local sym, of course we resolve locally. */
2920 if (h == NULL)
2921 return TRUE;
2922
2923 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2924 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2925 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2926 return TRUE;
2927
2928 /* Common symbols that become definitions don't get the DEF_REGULAR
2929 flag set, so test it first, and don't bail out. */
2930 if (ELF_COMMON_DEF_P (h))
2931 /* Do nothing. */;
2932 /* If we don't have a definition in a regular file, then we can't
2933 resolve locally. The sym is either undefined or dynamic. */
2934 else if (!h->def_regular)
2935 return FALSE;
2936
2937 /* Forced local symbols resolve locally. */
2938 if (h->forced_local)
2939 return TRUE;
2940
2941 /* As do non-dynamic symbols. */
2942 if (h->dynindx == -1)
2943 return TRUE;
2944
2945 /* At this point, we know the symbol is defined and dynamic. In an
2946 executable it must resolve locally, likewise when building symbolic
2947 shared libraries. */
2948 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
2949 return TRUE;
2950
2951 /* Now deal with defined dynamic symbols in shared libraries. Ones
2952 with default visibility might not resolve locally. */
2953 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2954 return FALSE;
2955
2956 hash_table = elf_hash_table (info);
2957 if (!is_elf_hash_table (hash_table))
2958 return TRUE;
2959
2960 bed = get_elf_backend_data (hash_table->dynobj);
2961
2962 /* If extern_protected_data is false, STV_PROTECTED non-function
2963 symbols are local. */
2964 if ((!info->extern_protected_data
2965 || (info->extern_protected_data < 0
2966 && !bed->extern_protected_data))
2967 && !bed->is_function_type (h->type))
2968 return TRUE;
2969
2970 /* Function pointer equality tests may require that STV_PROTECTED
2971 symbols be treated as dynamic symbols. If the address of a
2972 function not defined in an executable is set to that function's
2973 plt entry in the executable, then the address of the function in
2974 a shared library must also be the plt entry in the executable. */
2975 return local_protected;
2976 }
2977
2978 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2979 aligned. Returns the first TLS output section. */
2980
2981 struct bfd_section *
2982 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2983 {
2984 struct bfd_section *sec, *tls;
2985 unsigned int align = 0;
2986
2987 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2988 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2989 break;
2990 tls = sec;
2991
2992 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2993 if (sec->alignment_power > align)
2994 align = sec->alignment_power;
2995
2996 elf_hash_table (info)->tls_sec = tls;
2997
2998 /* Ensure the alignment of the first section is the largest alignment,
2999 so that the tls segment starts aligned. */
3000 if (tls != NULL)
3001 tls->alignment_power = align;
3002
3003 return tls;
3004 }
3005
3006 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3007 static bfd_boolean
3008 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3009 Elf_Internal_Sym *sym)
3010 {
3011 const struct elf_backend_data *bed;
3012
3013 /* Local symbols do not count, but target specific ones might. */
3014 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3015 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3016 return FALSE;
3017
3018 bed = get_elf_backend_data (abfd);
3019 /* Function symbols do not count. */
3020 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3021 return FALSE;
3022
3023 /* If the section is undefined, then so is the symbol. */
3024 if (sym->st_shndx == SHN_UNDEF)
3025 return FALSE;
3026
3027 /* If the symbol is defined in the common section, then
3028 it is a common definition and so does not count. */
3029 if (bed->common_definition (sym))
3030 return FALSE;
3031
3032 /* If the symbol is in a target specific section then we
3033 must rely upon the backend to tell us what it is. */
3034 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3035 /* FIXME - this function is not coded yet:
3036
3037 return _bfd_is_global_symbol_definition (abfd, sym);
3038
3039 Instead for now assume that the definition is not global,
3040 Even if this is wrong, at least the linker will behave
3041 in the same way that it used to do. */
3042 return FALSE;
3043
3044 return TRUE;
3045 }
3046
3047 /* Search the symbol table of the archive element of the archive ABFD
3048 whose archive map contains a mention of SYMDEF, and determine if
3049 the symbol is defined in this element. */
3050 static bfd_boolean
3051 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3052 {
3053 Elf_Internal_Shdr * hdr;
3054 bfd_size_type symcount;
3055 bfd_size_type extsymcount;
3056 bfd_size_type extsymoff;
3057 Elf_Internal_Sym *isymbuf;
3058 Elf_Internal_Sym *isym;
3059 Elf_Internal_Sym *isymend;
3060 bfd_boolean result;
3061
3062 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3063 if (abfd == NULL)
3064 return FALSE;
3065
3066 /* Return FALSE if the object has been claimed by plugin. */
3067 if (abfd->plugin_format == bfd_plugin_yes)
3068 return FALSE;
3069
3070 if (! bfd_check_format (abfd, bfd_object))
3071 return FALSE;
3072
3073 /* Select the appropriate symbol table. */
3074 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3075 hdr = &elf_tdata (abfd)->symtab_hdr;
3076 else
3077 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3078
3079 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3080
3081 /* The sh_info field of the symtab header tells us where the
3082 external symbols start. We don't care about the local symbols. */
3083 if (elf_bad_symtab (abfd))
3084 {
3085 extsymcount = symcount;
3086 extsymoff = 0;
3087 }
3088 else
3089 {
3090 extsymcount = symcount - hdr->sh_info;
3091 extsymoff = hdr->sh_info;
3092 }
3093
3094 if (extsymcount == 0)
3095 return FALSE;
3096
3097 /* Read in the symbol table. */
3098 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3099 NULL, NULL, NULL);
3100 if (isymbuf == NULL)
3101 return FALSE;
3102
3103 /* Scan the symbol table looking for SYMDEF. */
3104 result = FALSE;
3105 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3106 {
3107 const char *name;
3108
3109 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3110 isym->st_name);
3111 if (name == NULL)
3112 break;
3113
3114 if (strcmp (name, symdef->name) == 0)
3115 {
3116 result = is_global_data_symbol_definition (abfd, isym);
3117 break;
3118 }
3119 }
3120
3121 free (isymbuf);
3122
3123 return result;
3124 }
3125 \f
3126 /* Add an entry to the .dynamic table. */
3127
3128 bfd_boolean
3129 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3130 bfd_vma tag,
3131 bfd_vma val)
3132 {
3133 struct elf_link_hash_table *hash_table;
3134 const struct elf_backend_data *bed;
3135 asection *s;
3136 bfd_size_type newsize;
3137 bfd_byte *newcontents;
3138 Elf_Internal_Dyn dyn;
3139
3140 hash_table = elf_hash_table (info);
3141 if (! is_elf_hash_table (hash_table))
3142 return FALSE;
3143
3144 bed = get_elf_backend_data (hash_table->dynobj);
3145 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3146 BFD_ASSERT (s != NULL);
3147
3148 newsize = s->size + bed->s->sizeof_dyn;
3149 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3150 if (newcontents == NULL)
3151 return FALSE;
3152
3153 dyn.d_tag = tag;
3154 dyn.d_un.d_val = val;
3155 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3156
3157 s->size = newsize;
3158 s->contents = newcontents;
3159
3160 return TRUE;
3161 }
3162
3163 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3164 otherwise just check whether one already exists. Returns -1 on error,
3165 1 if a DT_NEEDED tag already exists, and 0 on success. */
3166
3167 static int
3168 elf_add_dt_needed_tag (bfd *abfd,
3169 struct bfd_link_info *info,
3170 const char *soname,
3171 bfd_boolean do_it)
3172 {
3173 struct elf_link_hash_table *hash_table;
3174 bfd_size_type strindex;
3175
3176 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3177 return -1;
3178
3179 hash_table = elf_hash_table (info);
3180 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3181 if (strindex == (bfd_size_type) -1)
3182 return -1;
3183
3184 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3185 {
3186 asection *sdyn;
3187 const struct elf_backend_data *bed;
3188 bfd_byte *extdyn;
3189
3190 bed = get_elf_backend_data (hash_table->dynobj);
3191 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3192 if (sdyn != NULL)
3193 for (extdyn = sdyn->contents;
3194 extdyn < sdyn->contents + sdyn->size;
3195 extdyn += bed->s->sizeof_dyn)
3196 {
3197 Elf_Internal_Dyn dyn;
3198
3199 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3200 if (dyn.d_tag == DT_NEEDED
3201 && dyn.d_un.d_val == strindex)
3202 {
3203 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3204 return 1;
3205 }
3206 }
3207 }
3208
3209 if (do_it)
3210 {
3211 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3212 return -1;
3213
3214 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3215 return -1;
3216 }
3217 else
3218 /* We were just checking for existence of the tag. */
3219 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3220
3221 return 0;
3222 }
3223
3224 static bfd_boolean
3225 on_needed_list (const char *soname, struct bfd_link_needed_list *needed)
3226 {
3227 for (; needed != NULL; needed = needed->next)
3228 if ((elf_dyn_lib_class (needed->by) & DYN_AS_NEEDED) == 0
3229 && strcmp (soname, needed->name) == 0)
3230 return TRUE;
3231
3232 return FALSE;
3233 }
3234
3235 /* Sort symbol by value, section, and size. */
3236 static int
3237 elf_sort_symbol (const void *arg1, const void *arg2)
3238 {
3239 const struct elf_link_hash_entry *h1;
3240 const struct elf_link_hash_entry *h2;
3241 bfd_signed_vma vdiff;
3242
3243 h1 = *(const struct elf_link_hash_entry **) arg1;
3244 h2 = *(const struct elf_link_hash_entry **) arg2;
3245 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3246 if (vdiff != 0)
3247 return vdiff > 0 ? 1 : -1;
3248 else
3249 {
3250 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3251 if (sdiff != 0)
3252 return sdiff > 0 ? 1 : -1;
3253 }
3254 vdiff = h1->size - h2->size;
3255 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3256 }
3257
3258 /* This function is used to adjust offsets into .dynstr for
3259 dynamic symbols. This is called via elf_link_hash_traverse. */
3260
3261 static bfd_boolean
3262 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3263 {
3264 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3265
3266 if (h->dynindx != -1)
3267 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3268 return TRUE;
3269 }
3270
3271 /* Assign string offsets in .dynstr, update all structures referencing
3272 them. */
3273
3274 static bfd_boolean
3275 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3276 {
3277 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3278 struct elf_link_local_dynamic_entry *entry;
3279 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3280 bfd *dynobj = hash_table->dynobj;
3281 asection *sdyn;
3282 bfd_size_type size;
3283 const struct elf_backend_data *bed;
3284 bfd_byte *extdyn;
3285
3286 _bfd_elf_strtab_finalize (dynstr);
3287 size = _bfd_elf_strtab_size (dynstr);
3288
3289 bed = get_elf_backend_data (dynobj);
3290 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3291 BFD_ASSERT (sdyn != NULL);
3292
3293 /* Update all .dynamic entries referencing .dynstr strings. */
3294 for (extdyn = sdyn->contents;
3295 extdyn < sdyn->contents + sdyn->size;
3296 extdyn += bed->s->sizeof_dyn)
3297 {
3298 Elf_Internal_Dyn dyn;
3299
3300 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3301 switch (dyn.d_tag)
3302 {
3303 case DT_STRSZ:
3304 dyn.d_un.d_val = size;
3305 break;
3306 case DT_NEEDED:
3307 case DT_SONAME:
3308 case DT_RPATH:
3309 case DT_RUNPATH:
3310 case DT_FILTER:
3311 case DT_AUXILIARY:
3312 case DT_AUDIT:
3313 case DT_DEPAUDIT:
3314 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3315 break;
3316 default:
3317 continue;
3318 }
3319 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3320 }
3321
3322 /* Now update local dynamic symbols. */
3323 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3324 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3325 entry->isym.st_name);
3326
3327 /* And the rest of dynamic symbols. */
3328 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3329
3330 /* Adjust version definitions. */
3331 if (elf_tdata (output_bfd)->cverdefs)
3332 {
3333 asection *s;
3334 bfd_byte *p;
3335 bfd_size_type i;
3336 Elf_Internal_Verdef def;
3337 Elf_Internal_Verdaux defaux;
3338
3339 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3340 p = s->contents;
3341 do
3342 {
3343 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3344 &def);
3345 p += sizeof (Elf_External_Verdef);
3346 if (def.vd_aux != sizeof (Elf_External_Verdef))
3347 continue;
3348 for (i = 0; i < def.vd_cnt; ++i)
3349 {
3350 _bfd_elf_swap_verdaux_in (output_bfd,
3351 (Elf_External_Verdaux *) p, &defaux);
3352 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3353 defaux.vda_name);
3354 _bfd_elf_swap_verdaux_out (output_bfd,
3355 &defaux, (Elf_External_Verdaux *) p);
3356 p += sizeof (Elf_External_Verdaux);
3357 }
3358 }
3359 while (def.vd_next);
3360 }
3361
3362 /* Adjust version references. */
3363 if (elf_tdata (output_bfd)->verref)
3364 {
3365 asection *s;
3366 bfd_byte *p;
3367 bfd_size_type i;
3368 Elf_Internal_Verneed need;
3369 Elf_Internal_Vernaux needaux;
3370
3371 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3372 p = s->contents;
3373 do
3374 {
3375 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3376 &need);
3377 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3378 _bfd_elf_swap_verneed_out (output_bfd, &need,
3379 (Elf_External_Verneed *) p);
3380 p += sizeof (Elf_External_Verneed);
3381 for (i = 0; i < need.vn_cnt; ++i)
3382 {
3383 _bfd_elf_swap_vernaux_in (output_bfd,
3384 (Elf_External_Vernaux *) p, &needaux);
3385 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3386 needaux.vna_name);
3387 _bfd_elf_swap_vernaux_out (output_bfd,
3388 &needaux,
3389 (Elf_External_Vernaux *) p);
3390 p += sizeof (Elf_External_Vernaux);
3391 }
3392 }
3393 while (need.vn_next);
3394 }
3395
3396 return TRUE;
3397 }
3398 \f
3399 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3400 The default is to only match when the INPUT and OUTPUT are exactly
3401 the same target. */
3402
3403 bfd_boolean
3404 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3405 const bfd_target *output)
3406 {
3407 return input == output;
3408 }
3409
3410 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3411 This version is used when different targets for the same architecture
3412 are virtually identical. */
3413
3414 bfd_boolean
3415 _bfd_elf_relocs_compatible (const bfd_target *input,
3416 const bfd_target *output)
3417 {
3418 const struct elf_backend_data *obed, *ibed;
3419
3420 if (input == output)
3421 return TRUE;
3422
3423 ibed = xvec_get_elf_backend_data (input);
3424 obed = xvec_get_elf_backend_data (output);
3425
3426 if (ibed->arch != obed->arch)
3427 return FALSE;
3428
3429 /* If both backends are using this function, deem them compatible. */
3430 return ibed->relocs_compatible == obed->relocs_compatible;
3431 }
3432
3433 /* Make a special call to the linker "notice" function to tell it that
3434 we are about to handle an as-needed lib, or have finished
3435 processing the lib. */
3436
3437 bfd_boolean
3438 _bfd_elf_notice_as_needed (bfd *ibfd,
3439 struct bfd_link_info *info,
3440 enum notice_asneeded_action act)
3441 {
3442 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3443 }
3444
3445 /* Add symbols from an ELF object file to the linker hash table. */
3446
3447 static bfd_boolean
3448 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3449 {
3450 Elf_Internal_Ehdr *ehdr;
3451 Elf_Internal_Shdr *hdr;
3452 bfd_size_type symcount;
3453 bfd_size_type extsymcount;
3454 bfd_size_type extsymoff;
3455 struct elf_link_hash_entry **sym_hash;
3456 bfd_boolean dynamic;
3457 Elf_External_Versym *extversym = NULL;
3458 Elf_External_Versym *ever;
3459 struct elf_link_hash_entry *weaks;
3460 struct elf_link_hash_entry **nondeflt_vers = NULL;
3461 bfd_size_type nondeflt_vers_cnt = 0;
3462 Elf_Internal_Sym *isymbuf = NULL;
3463 Elf_Internal_Sym *isym;
3464 Elf_Internal_Sym *isymend;
3465 const struct elf_backend_data *bed;
3466 bfd_boolean add_needed;
3467 struct elf_link_hash_table *htab;
3468 bfd_size_type amt;
3469 void *alloc_mark = NULL;
3470 struct bfd_hash_entry **old_table = NULL;
3471 unsigned int old_size = 0;
3472 unsigned int old_count = 0;
3473 void *old_tab = NULL;
3474 void *old_ent;
3475 struct bfd_link_hash_entry *old_undefs = NULL;
3476 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3477 long old_dynsymcount = 0;
3478 bfd_size_type old_dynstr_size = 0;
3479 size_t tabsize = 0;
3480 asection *s;
3481 bfd_boolean just_syms;
3482
3483 htab = elf_hash_table (info);
3484 bed = get_elf_backend_data (abfd);
3485
3486 if ((abfd->flags & DYNAMIC) == 0)
3487 dynamic = FALSE;
3488 else
3489 {
3490 dynamic = TRUE;
3491
3492 /* You can't use -r against a dynamic object. Also, there's no
3493 hope of using a dynamic object which does not exactly match
3494 the format of the output file. */
3495 if (bfd_link_relocatable (info)
3496 || !is_elf_hash_table (htab)
3497 || info->output_bfd->xvec != abfd->xvec)
3498 {
3499 if (bfd_link_relocatable (info))
3500 bfd_set_error (bfd_error_invalid_operation);
3501 else
3502 bfd_set_error (bfd_error_wrong_format);
3503 goto error_return;
3504 }
3505 }
3506
3507 ehdr = elf_elfheader (abfd);
3508 if (info->warn_alternate_em
3509 && bed->elf_machine_code != ehdr->e_machine
3510 && ((bed->elf_machine_alt1 != 0
3511 && ehdr->e_machine == bed->elf_machine_alt1)
3512 || (bed->elf_machine_alt2 != 0
3513 && ehdr->e_machine == bed->elf_machine_alt2)))
3514 info->callbacks->einfo
3515 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3516 ehdr->e_machine, abfd, bed->elf_machine_code);
3517
3518 /* As a GNU extension, any input sections which are named
3519 .gnu.warning.SYMBOL are treated as warning symbols for the given
3520 symbol. This differs from .gnu.warning sections, which generate
3521 warnings when they are included in an output file. */
3522 /* PR 12761: Also generate this warning when building shared libraries. */
3523 for (s = abfd->sections; s != NULL; s = s->next)
3524 {
3525 const char *name;
3526
3527 name = bfd_get_section_name (abfd, s);
3528 if (CONST_STRNEQ (name, ".gnu.warning."))
3529 {
3530 char *msg;
3531 bfd_size_type sz;
3532
3533 name += sizeof ".gnu.warning." - 1;
3534
3535 /* If this is a shared object, then look up the symbol
3536 in the hash table. If it is there, and it is already
3537 been defined, then we will not be using the entry
3538 from this shared object, so we don't need to warn.
3539 FIXME: If we see the definition in a regular object
3540 later on, we will warn, but we shouldn't. The only
3541 fix is to keep track of what warnings we are supposed
3542 to emit, and then handle them all at the end of the
3543 link. */
3544 if (dynamic)
3545 {
3546 struct elf_link_hash_entry *h;
3547
3548 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3549
3550 /* FIXME: What about bfd_link_hash_common? */
3551 if (h != NULL
3552 && (h->root.type == bfd_link_hash_defined
3553 || h->root.type == bfd_link_hash_defweak))
3554 continue;
3555 }
3556
3557 sz = s->size;
3558 msg = (char *) bfd_alloc (abfd, sz + 1);
3559 if (msg == NULL)
3560 goto error_return;
3561
3562 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3563 goto error_return;
3564
3565 msg[sz] = '\0';
3566
3567 if (! (_bfd_generic_link_add_one_symbol
3568 (info, abfd, name, BSF_WARNING, s, 0, msg,
3569 FALSE, bed->collect, NULL)))
3570 goto error_return;
3571
3572 if (bfd_link_executable (info))
3573 {
3574 /* Clobber the section size so that the warning does
3575 not get copied into the output file. */
3576 s->size = 0;
3577
3578 /* Also set SEC_EXCLUDE, so that symbols defined in
3579 the warning section don't get copied to the output. */
3580 s->flags |= SEC_EXCLUDE;
3581 }
3582 }
3583 }
3584
3585 just_syms = ((s = abfd->sections) != NULL
3586 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3587
3588 add_needed = TRUE;
3589 if (! dynamic)
3590 {
3591 /* If we are creating a shared library, create all the dynamic
3592 sections immediately. We need to attach them to something,
3593 so we attach them to this BFD, provided it is the right
3594 format and is not from ld --just-symbols. FIXME: If there
3595 are no input BFD's of the same format as the output, we can't
3596 make a shared library. */
3597 if (!just_syms
3598 && bfd_link_pic (info)
3599 && is_elf_hash_table (htab)
3600 && info->output_bfd->xvec == abfd->xvec
3601 && !htab->dynamic_sections_created)
3602 {
3603 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3604 goto error_return;
3605 }
3606 }
3607 else if (!is_elf_hash_table (htab))
3608 goto error_return;
3609 else
3610 {
3611 const char *soname = NULL;
3612 char *audit = NULL;
3613 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3614 int ret;
3615
3616 /* ld --just-symbols and dynamic objects don't mix very well.
3617 ld shouldn't allow it. */
3618 if (just_syms)
3619 abort ();
3620
3621 /* If this dynamic lib was specified on the command line with
3622 --as-needed in effect, then we don't want to add a DT_NEEDED
3623 tag unless the lib is actually used. Similary for libs brought
3624 in by another lib's DT_NEEDED. When --no-add-needed is used
3625 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3626 any dynamic library in DT_NEEDED tags in the dynamic lib at
3627 all. */
3628 add_needed = (elf_dyn_lib_class (abfd)
3629 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3630 | DYN_NO_NEEDED)) == 0;
3631
3632 s = bfd_get_section_by_name (abfd, ".dynamic");
3633 if (s != NULL)
3634 {
3635 bfd_byte *dynbuf;
3636 bfd_byte *extdyn;
3637 unsigned int elfsec;
3638 unsigned long shlink;
3639
3640 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3641 {
3642 error_free_dyn:
3643 free (dynbuf);
3644 goto error_return;
3645 }
3646
3647 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3648 if (elfsec == SHN_BAD)
3649 goto error_free_dyn;
3650 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3651
3652 for (extdyn = dynbuf;
3653 extdyn < dynbuf + s->size;
3654 extdyn += bed->s->sizeof_dyn)
3655 {
3656 Elf_Internal_Dyn dyn;
3657
3658 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3659 if (dyn.d_tag == DT_SONAME)
3660 {
3661 unsigned int tagv = dyn.d_un.d_val;
3662 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3663 if (soname == NULL)
3664 goto error_free_dyn;
3665 }
3666 if (dyn.d_tag == DT_NEEDED)
3667 {
3668 struct bfd_link_needed_list *n, **pn;
3669 char *fnm, *anm;
3670 unsigned int tagv = dyn.d_un.d_val;
3671
3672 amt = sizeof (struct bfd_link_needed_list);
3673 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3674 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3675 if (n == NULL || fnm == NULL)
3676 goto error_free_dyn;
3677 amt = strlen (fnm) + 1;
3678 anm = (char *) bfd_alloc (abfd, amt);
3679 if (anm == NULL)
3680 goto error_free_dyn;
3681 memcpy (anm, fnm, amt);
3682 n->name = anm;
3683 n->by = abfd;
3684 n->next = NULL;
3685 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3686 ;
3687 *pn = n;
3688 }
3689 if (dyn.d_tag == DT_RUNPATH)
3690 {
3691 struct bfd_link_needed_list *n, **pn;
3692 char *fnm, *anm;
3693 unsigned int tagv = dyn.d_un.d_val;
3694
3695 amt = sizeof (struct bfd_link_needed_list);
3696 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3697 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3698 if (n == NULL || fnm == NULL)
3699 goto error_free_dyn;
3700 amt = strlen (fnm) + 1;
3701 anm = (char *) bfd_alloc (abfd, amt);
3702 if (anm == NULL)
3703 goto error_free_dyn;
3704 memcpy (anm, fnm, amt);
3705 n->name = anm;
3706 n->by = abfd;
3707 n->next = NULL;
3708 for (pn = & runpath;
3709 *pn != NULL;
3710 pn = &(*pn)->next)
3711 ;
3712 *pn = n;
3713 }
3714 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3715 if (!runpath && dyn.d_tag == DT_RPATH)
3716 {
3717 struct bfd_link_needed_list *n, **pn;
3718 char *fnm, *anm;
3719 unsigned int tagv = dyn.d_un.d_val;
3720
3721 amt = sizeof (struct bfd_link_needed_list);
3722 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3723 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3724 if (n == NULL || fnm == NULL)
3725 goto error_free_dyn;
3726 amt = strlen (fnm) + 1;
3727 anm = (char *) bfd_alloc (abfd, amt);
3728 if (anm == NULL)
3729 goto error_free_dyn;
3730 memcpy (anm, fnm, amt);
3731 n->name = anm;
3732 n->by = abfd;
3733 n->next = NULL;
3734 for (pn = & rpath;
3735 *pn != NULL;
3736 pn = &(*pn)->next)
3737 ;
3738 *pn = n;
3739 }
3740 if (dyn.d_tag == DT_AUDIT)
3741 {
3742 unsigned int tagv = dyn.d_un.d_val;
3743 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3744 }
3745 }
3746
3747 free (dynbuf);
3748 }
3749
3750 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3751 frees all more recently bfd_alloc'd blocks as well. */
3752 if (runpath)
3753 rpath = runpath;
3754
3755 if (rpath)
3756 {
3757 struct bfd_link_needed_list **pn;
3758 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3759 ;
3760 *pn = rpath;
3761 }
3762
3763 /* We do not want to include any of the sections in a dynamic
3764 object in the output file. We hack by simply clobbering the
3765 list of sections in the BFD. This could be handled more
3766 cleanly by, say, a new section flag; the existing
3767 SEC_NEVER_LOAD flag is not the one we want, because that one
3768 still implies that the section takes up space in the output
3769 file. */
3770 bfd_section_list_clear (abfd);
3771
3772 /* Find the name to use in a DT_NEEDED entry that refers to this
3773 object. If the object has a DT_SONAME entry, we use it.
3774 Otherwise, if the generic linker stuck something in
3775 elf_dt_name, we use that. Otherwise, we just use the file
3776 name. */
3777 if (soname == NULL || *soname == '\0')
3778 {
3779 soname = elf_dt_name (abfd);
3780 if (soname == NULL || *soname == '\0')
3781 soname = bfd_get_filename (abfd);
3782 }
3783
3784 /* Save the SONAME because sometimes the linker emulation code
3785 will need to know it. */
3786 elf_dt_name (abfd) = soname;
3787
3788 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3789 if (ret < 0)
3790 goto error_return;
3791
3792 /* If we have already included this dynamic object in the
3793 link, just ignore it. There is no reason to include a
3794 particular dynamic object more than once. */
3795 if (ret > 0)
3796 return TRUE;
3797
3798 /* Save the DT_AUDIT entry for the linker emulation code. */
3799 elf_dt_audit (abfd) = audit;
3800 }
3801
3802 /* If this is a dynamic object, we always link against the .dynsym
3803 symbol table, not the .symtab symbol table. The dynamic linker
3804 will only see the .dynsym symbol table, so there is no reason to
3805 look at .symtab for a dynamic object. */
3806
3807 if (! dynamic || elf_dynsymtab (abfd) == 0)
3808 hdr = &elf_tdata (abfd)->symtab_hdr;
3809 else
3810 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3811
3812 symcount = hdr->sh_size / bed->s->sizeof_sym;
3813
3814 /* The sh_info field of the symtab header tells us where the
3815 external symbols start. We don't care about the local symbols at
3816 this point. */
3817 if (elf_bad_symtab (abfd))
3818 {
3819 extsymcount = symcount;
3820 extsymoff = 0;
3821 }
3822 else
3823 {
3824 extsymcount = symcount - hdr->sh_info;
3825 extsymoff = hdr->sh_info;
3826 }
3827
3828 sym_hash = elf_sym_hashes (abfd);
3829 if (extsymcount != 0)
3830 {
3831 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3832 NULL, NULL, NULL);
3833 if (isymbuf == NULL)
3834 goto error_return;
3835
3836 if (sym_hash == NULL)
3837 {
3838 /* We store a pointer to the hash table entry for each
3839 external symbol. */
3840 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3841 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
3842 if (sym_hash == NULL)
3843 goto error_free_sym;
3844 elf_sym_hashes (abfd) = sym_hash;
3845 }
3846 }
3847
3848 if (dynamic)
3849 {
3850 /* Read in any version definitions. */
3851 if (!_bfd_elf_slurp_version_tables (abfd,
3852 info->default_imported_symver))
3853 goto error_free_sym;
3854
3855 /* Read in the symbol versions, but don't bother to convert them
3856 to internal format. */
3857 if (elf_dynversym (abfd) != 0)
3858 {
3859 Elf_Internal_Shdr *versymhdr;
3860
3861 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3862 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
3863 if (extversym == NULL)
3864 goto error_free_sym;
3865 amt = versymhdr->sh_size;
3866 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3867 || bfd_bread (extversym, amt, abfd) != amt)
3868 goto error_free_vers;
3869 }
3870 }
3871
3872 /* If we are loading an as-needed shared lib, save the symbol table
3873 state before we start adding symbols. If the lib turns out
3874 to be unneeded, restore the state. */
3875 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3876 {
3877 unsigned int i;
3878 size_t entsize;
3879
3880 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3881 {
3882 struct bfd_hash_entry *p;
3883 struct elf_link_hash_entry *h;
3884
3885 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3886 {
3887 h = (struct elf_link_hash_entry *) p;
3888 entsize += htab->root.table.entsize;
3889 if (h->root.type == bfd_link_hash_warning)
3890 entsize += htab->root.table.entsize;
3891 }
3892 }
3893
3894 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
3895 old_tab = bfd_malloc (tabsize + entsize);
3896 if (old_tab == NULL)
3897 goto error_free_vers;
3898
3899 /* Remember the current objalloc pointer, so that all mem for
3900 symbols added can later be reclaimed. */
3901 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3902 if (alloc_mark == NULL)
3903 goto error_free_vers;
3904
3905 /* Make a special call to the linker "notice" function to
3906 tell it that we are about to handle an as-needed lib. */
3907 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
3908 goto error_free_vers;
3909
3910 /* Clone the symbol table. Remember some pointers into the
3911 symbol table, and dynamic symbol count. */
3912 old_ent = (char *) old_tab + tabsize;
3913 memcpy (old_tab, htab->root.table.table, tabsize);
3914 old_undefs = htab->root.undefs;
3915 old_undefs_tail = htab->root.undefs_tail;
3916 old_table = htab->root.table.table;
3917 old_size = htab->root.table.size;
3918 old_count = htab->root.table.count;
3919 old_dynsymcount = htab->dynsymcount;
3920 old_dynstr_size = _bfd_elf_strtab_size (htab->dynstr);
3921
3922 for (i = 0; i < htab->root.table.size; i++)
3923 {
3924 struct bfd_hash_entry *p;
3925 struct elf_link_hash_entry *h;
3926
3927 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3928 {
3929 memcpy (old_ent, p, htab->root.table.entsize);
3930 old_ent = (char *) old_ent + htab->root.table.entsize;
3931 h = (struct elf_link_hash_entry *) p;
3932 if (h->root.type == bfd_link_hash_warning)
3933 {
3934 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3935 old_ent = (char *) old_ent + htab->root.table.entsize;
3936 }
3937 }
3938 }
3939 }
3940
3941 weaks = NULL;
3942 ever = extversym != NULL ? extversym + extsymoff : NULL;
3943 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3944 isym < isymend;
3945 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3946 {
3947 int bind;
3948 bfd_vma value;
3949 asection *sec, *new_sec;
3950 flagword flags;
3951 const char *name;
3952 struct elf_link_hash_entry *h;
3953 struct elf_link_hash_entry *hi;
3954 bfd_boolean definition;
3955 bfd_boolean size_change_ok;
3956 bfd_boolean type_change_ok;
3957 bfd_boolean new_weakdef;
3958 bfd_boolean new_weak;
3959 bfd_boolean old_weak;
3960 bfd_boolean override;
3961 bfd_boolean common;
3962 unsigned int old_alignment;
3963 bfd *old_bfd;
3964 bfd_boolean matched;
3965
3966 override = FALSE;
3967
3968 flags = BSF_NO_FLAGS;
3969 sec = NULL;
3970 value = isym->st_value;
3971 common = bed->common_definition (isym);
3972
3973 bind = ELF_ST_BIND (isym->st_info);
3974 switch (bind)
3975 {
3976 case STB_LOCAL:
3977 /* This should be impossible, since ELF requires that all
3978 global symbols follow all local symbols, and that sh_info
3979 point to the first global symbol. Unfortunately, Irix 5
3980 screws this up. */
3981 continue;
3982
3983 case STB_GLOBAL:
3984 if (isym->st_shndx != SHN_UNDEF && !common)
3985 flags = BSF_GLOBAL;
3986 break;
3987
3988 case STB_WEAK:
3989 flags = BSF_WEAK;
3990 break;
3991
3992 case STB_GNU_UNIQUE:
3993 flags = BSF_GNU_UNIQUE;
3994 break;
3995
3996 default:
3997 /* Leave it up to the processor backend. */
3998 break;
3999 }
4000
4001 if (isym->st_shndx == SHN_UNDEF)
4002 sec = bfd_und_section_ptr;
4003 else if (isym->st_shndx == SHN_ABS)
4004 sec = bfd_abs_section_ptr;
4005 else if (isym->st_shndx == SHN_COMMON)
4006 {
4007 sec = bfd_com_section_ptr;
4008 /* What ELF calls the size we call the value. What ELF
4009 calls the value we call the alignment. */
4010 value = isym->st_size;
4011 }
4012 else
4013 {
4014 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4015 if (sec == NULL)
4016 sec = bfd_abs_section_ptr;
4017 else if (discarded_section (sec))
4018 {
4019 /* Symbols from discarded section are undefined. We keep
4020 its visibility. */
4021 sec = bfd_und_section_ptr;
4022 isym->st_shndx = SHN_UNDEF;
4023 }
4024 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4025 value -= sec->vma;
4026 }
4027
4028 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4029 isym->st_name);
4030 if (name == NULL)
4031 goto error_free_vers;
4032
4033 if (isym->st_shndx == SHN_COMMON
4034 && (abfd->flags & BFD_PLUGIN) != 0)
4035 {
4036 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4037
4038 if (xc == NULL)
4039 {
4040 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4041 | SEC_EXCLUDE);
4042 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4043 if (xc == NULL)
4044 goto error_free_vers;
4045 }
4046 sec = xc;
4047 }
4048 else if (isym->st_shndx == SHN_COMMON
4049 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4050 && !bfd_link_relocatable (info))
4051 {
4052 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4053
4054 if (tcomm == NULL)
4055 {
4056 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4057 | SEC_LINKER_CREATED);
4058 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4059 if (tcomm == NULL)
4060 goto error_free_vers;
4061 }
4062 sec = tcomm;
4063 }
4064 else if (bed->elf_add_symbol_hook)
4065 {
4066 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4067 &sec, &value))
4068 goto error_free_vers;
4069
4070 /* The hook function sets the name to NULL if this symbol
4071 should be skipped for some reason. */
4072 if (name == NULL)
4073 continue;
4074 }
4075
4076 /* Sanity check that all possibilities were handled. */
4077 if (sec == NULL)
4078 {
4079 bfd_set_error (bfd_error_bad_value);
4080 goto error_free_vers;
4081 }
4082
4083 /* Silently discard TLS symbols from --just-syms. There's
4084 no way to combine a static TLS block with a new TLS block
4085 for this executable. */
4086 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4087 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4088 continue;
4089
4090 if (bfd_is_und_section (sec)
4091 || bfd_is_com_section (sec))
4092 definition = FALSE;
4093 else
4094 definition = TRUE;
4095
4096 size_change_ok = FALSE;
4097 type_change_ok = bed->type_change_ok;
4098 old_weak = FALSE;
4099 matched = FALSE;
4100 old_alignment = 0;
4101 old_bfd = NULL;
4102 new_sec = sec;
4103
4104 if (is_elf_hash_table (htab))
4105 {
4106 Elf_Internal_Versym iver;
4107 unsigned int vernum = 0;
4108 bfd_boolean skip;
4109
4110 if (ever == NULL)
4111 {
4112 if (info->default_imported_symver)
4113 /* Use the default symbol version created earlier. */
4114 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4115 else
4116 iver.vs_vers = 0;
4117 }
4118 else
4119 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4120
4121 vernum = iver.vs_vers & VERSYM_VERSION;
4122
4123 /* If this is a hidden symbol, or if it is not version
4124 1, we append the version name to the symbol name.
4125 However, we do not modify a non-hidden absolute symbol
4126 if it is not a function, because it might be the version
4127 symbol itself. FIXME: What if it isn't? */
4128 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4129 || (vernum > 1
4130 && (!bfd_is_abs_section (sec)
4131 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4132 {
4133 const char *verstr;
4134 size_t namelen, verlen, newlen;
4135 char *newname, *p;
4136
4137 if (isym->st_shndx != SHN_UNDEF)
4138 {
4139 if (vernum > elf_tdata (abfd)->cverdefs)
4140 verstr = NULL;
4141 else if (vernum > 1)
4142 verstr =
4143 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4144 else
4145 verstr = "";
4146
4147 if (verstr == NULL)
4148 {
4149 (*_bfd_error_handler)
4150 (_("%B: %s: invalid version %u (max %d)"),
4151 abfd, name, vernum,
4152 elf_tdata (abfd)->cverdefs);
4153 bfd_set_error (bfd_error_bad_value);
4154 goto error_free_vers;
4155 }
4156 }
4157 else
4158 {
4159 /* We cannot simply test for the number of
4160 entries in the VERNEED section since the
4161 numbers for the needed versions do not start
4162 at 0. */
4163 Elf_Internal_Verneed *t;
4164
4165 verstr = NULL;
4166 for (t = elf_tdata (abfd)->verref;
4167 t != NULL;
4168 t = t->vn_nextref)
4169 {
4170 Elf_Internal_Vernaux *a;
4171
4172 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4173 {
4174 if (a->vna_other == vernum)
4175 {
4176 verstr = a->vna_nodename;
4177 break;
4178 }
4179 }
4180 if (a != NULL)
4181 break;
4182 }
4183 if (verstr == NULL)
4184 {
4185 (*_bfd_error_handler)
4186 (_("%B: %s: invalid needed version %d"),
4187 abfd, name, vernum);
4188 bfd_set_error (bfd_error_bad_value);
4189 goto error_free_vers;
4190 }
4191 }
4192
4193 namelen = strlen (name);
4194 verlen = strlen (verstr);
4195 newlen = namelen + verlen + 2;
4196 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4197 && isym->st_shndx != SHN_UNDEF)
4198 ++newlen;
4199
4200 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4201 if (newname == NULL)
4202 goto error_free_vers;
4203 memcpy (newname, name, namelen);
4204 p = newname + namelen;
4205 *p++ = ELF_VER_CHR;
4206 /* If this is a defined non-hidden version symbol,
4207 we add another @ to the name. This indicates the
4208 default version of the symbol. */
4209 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4210 && isym->st_shndx != SHN_UNDEF)
4211 *p++ = ELF_VER_CHR;
4212 memcpy (p, verstr, verlen + 1);
4213
4214 name = newname;
4215 }
4216
4217 /* If this symbol has default visibility and the user has
4218 requested we not re-export it, then mark it as hidden. */
4219 if (definition
4220 && !dynamic
4221 && abfd->no_export
4222 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4223 isym->st_other = (STV_HIDDEN
4224 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4225
4226 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4227 sym_hash, &old_bfd, &old_weak,
4228 &old_alignment, &skip, &override,
4229 &type_change_ok, &size_change_ok,
4230 &matched))
4231 goto error_free_vers;
4232
4233 if (skip)
4234 continue;
4235
4236 /* Override a definition only if the new symbol matches the
4237 existing one. */
4238 if (override && matched)
4239 definition = FALSE;
4240
4241 h = *sym_hash;
4242 while (h->root.type == bfd_link_hash_indirect
4243 || h->root.type == bfd_link_hash_warning)
4244 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4245
4246 if (elf_tdata (abfd)->verdef != NULL
4247 && vernum > 1
4248 && definition)
4249 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4250 }
4251
4252 if (! (_bfd_generic_link_add_one_symbol
4253 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4254 (struct bfd_link_hash_entry **) sym_hash)))
4255 goto error_free_vers;
4256
4257 h = *sym_hash;
4258 /* We need to make sure that indirect symbol dynamic flags are
4259 updated. */
4260 hi = h;
4261 while (h->root.type == bfd_link_hash_indirect
4262 || h->root.type == bfd_link_hash_warning)
4263 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4264
4265 *sym_hash = h;
4266
4267 new_weak = (flags & BSF_WEAK) != 0;
4268 new_weakdef = FALSE;
4269 if (dynamic
4270 && definition
4271 && new_weak
4272 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4273 && is_elf_hash_table (htab)
4274 && h->u.weakdef == NULL)
4275 {
4276 /* Keep a list of all weak defined non function symbols from
4277 a dynamic object, using the weakdef field. Later in this
4278 function we will set the weakdef field to the correct
4279 value. We only put non-function symbols from dynamic
4280 objects on this list, because that happens to be the only
4281 time we need to know the normal symbol corresponding to a
4282 weak symbol, and the information is time consuming to
4283 figure out. If the weakdef field is not already NULL,
4284 then this symbol was already defined by some previous
4285 dynamic object, and we will be using that previous
4286 definition anyhow. */
4287
4288 h->u.weakdef = weaks;
4289 weaks = h;
4290 new_weakdef = TRUE;
4291 }
4292
4293 /* Set the alignment of a common symbol. */
4294 if ((common || bfd_is_com_section (sec))
4295 && h->root.type == bfd_link_hash_common)
4296 {
4297 unsigned int align;
4298
4299 if (common)
4300 align = bfd_log2 (isym->st_value);
4301 else
4302 {
4303 /* The new symbol is a common symbol in a shared object.
4304 We need to get the alignment from the section. */
4305 align = new_sec->alignment_power;
4306 }
4307 if (align > old_alignment)
4308 h->root.u.c.p->alignment_power = align;
4309 else
4310 h->root.u.c.p->alignment_power = old_alignment;
4311 }
4312
4313 if (is_elf_hash_table (htab))
4314 {
4315 /* Set a flag in the hash table entry indicating the type of
4316 reference or definition we just found. A dynamic symbol
4317 is one which is referenced or defined by both a regular
4318 object and a shared object. */
4319 bfd_boolean dynsym = FALSE;
4320
4321 /* Plugin symbols aren't normal. Don't set def_regular or
4322 ref_regular for them, or make them dynamic. */
4323 if ((abfd->flags & BFD_PLUGIN) != 0)
4324 ;
4325 else if (! dynamic)
4326 {
4327 if (! definition)
4328 {
4329 h->ref_regular = 1;
4330 if (bind != STB_WEAK)
4331 h->ref_regular_nonweak = 1;
4332 }
4333 else
4334 {
4335 h->def_regular = 1;
4336 if (h->def_dynamic)
4337 {
4338 h->def_dynamic = 0;
4339 h->ref_dynamic = 1;
4340 }
4341 }
4342
4343 /* If the indirect symbol has been forced local, don't
4344 make the real symbol dynamic. */
4345 if ((h == hi || !hi->forced_local)
4346 && (bfd_link_dll (info)
4347 || h->def_dynamic
4348 || h->ref_dynamic))
4349 dynsym = TRUE;
4350 }
4351 else
4352 {
4353 if (! definition)
4354 {
4355 h->ref_dynamic = 1;
4356 hi->ref_dynamic = 1;
4357 }
4358 else
4359 {
4360 h->def_dynamic = 1;
4361 hi->def_dynamic = 1;
4362 }
4363
4364 /* If the indirect symbol has been forced local, don't
4365 make the real symbol dynamic. */
4366 if ((h == hi || !hi->forced_local)
4367 && (h->def_regular
4368 || h->ref_regular
4369 || (h->u.weakdef != NULL
4370 && ! new_weakdef
4371 && h->u.weakdef->dynindx != -1)))
4372 dynsym = TRUE;
4373 }
4374
4375 /* Check to see if we need to add an indirect symbol for
4376 the default name. */
4377 if (definition
4378 || (!override && h->root.type == bfd_link_hash_common))
4379 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4380 sec, value, &old_bfd, &dynsym))
4381 goto error_free_vers;
4382
4383 /* Check the alignment when a common symbol is involved. This
4384 can change when a common symbol is overridden by a normal
4385 definition or a common symbol is ignored due to the old
4386 normal definition. We need to make sure the maximum
4387 alignment is maintained. */
4388 if ((old_alignment || common)
4389 && h->root.type != bfd_link_hash_common)
4390 {
4391 unsigned int common_align;
4392 unsigned int normal_align;
4393 unsigned int symbol_align;
4394 bfd *normal_bfd;
4395 bfd *common_bfd;
4396
4397 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4398 || h->root.type == bfd_link_hash_defweak);
4399
4400 symbol_align = ffs (h->root.u.def.value) - 1;
4401 if (h->root.u.def.section->owner != NULL
4402 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4403 {
4404 normal_align = h->root.u.def.section->alignment_power;
4405 if (normal_align > symbol_align)
4406 normal_align = symbol_align;
4407 }
4408 else
4409 normal_align = symbol_align;
4410
4411 if (old_alignment)
4412 {
4413 common_align = old_alignment;
4414 common_bfd = old_bfd;
4415 normal_bfd = abfd;
4416 }
4417 else
4418 {
4419 common_align = bfd_log2 (isym->st_value);
4420 common_bfd = abfd;
4421 normal_bfd = old_bfd;
4422 }
4423
4424 if (normal_align < common_align)
4425 {
4426 /* PR binutils/2735 */
4427 if (normal_bfd == NULL)
4428 (*_bfd_error_handler)
4429 (_("Warning: alignment %u of common symbol `%s' in %B is"
4430 " greater than the alignment (%u) of its section %A"),
4431 common_bfd, h->root.u.def.section,
4432 1 << common_align, name, 1 << normal_align);
4433 else
4434 (*_bfd_error_handler)
4435 (_("Warning: alignment %u of symbol `%s' in %B"
4436 " is smaller than %u in %B"),
4437 normal_bfd, common_bfd,
4438 1 << normal_align, name, 1 << common_align);
4439 }
4440 }
4441
4442 /* Remember the symbol size if it isn't undefined. */
4443 if (isym->st_size != 0
4444 && isym->st_shndx != SHN_UNDEF
4445 && (definition || h->size == 0))
4446 {
4447 if (h->size != 0
4448 && h->size != isym->st_size
4449 && ! size_change_ok)
4450 (*_bfd_error_handler)
4451 (_("Warning: size of symbol `%s' changed"
4452 " from %lu in %B to %lu in %B"),
4453 old_bfd, abfd,
4454 name, (unsigned long) h->size,
4455 (unsigned long) isym->st_size);
4456
4457 h->size = isym->st_size;
4458 }
4459
4460 /* If this is a common symbol, then we always want H->SIZE
4461 to be the size of the common symbol. The code just above
4462 won't fix the size if a common symbol becomes larger. We
4463 don't warn about a size change here, because that is
4464 covered by --warn-common. Allow changes between different
4465 function types. */
4466 if (h->root.type == bfd_link_hash_common)
4467 h->size = h->root.u.c.size;
4468
4469 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4470 && ((definition && !new_weak)
4471 || (old_weak && h->root.type == bfd_link_hash_common)
4472 || h->type == STT_NOTYPE))
4473 {
4474 unsigned int type = ELF_ST_TYPE (isym->st_info);
4475
4476 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4477 symbol. */
4478 if (type == STT_GNU_IFUNC
4479 && (abfd->flags & DYNAMIC) != 0)
4480 type = STT_FUNC;
4481
4482 if (h->type != type)
4483 {
4484 if (h->type != STT_NOTYPE && ! type_change_ok)
4485 (*_bfd_error_handler)
4486 (_("Warning: type of symbol `%s' changed"
4487 " from %d to %d in %B"),
4488 abfd, name, h->type, type);
4489
4490 h->type = type;
4491 }
4492 }
4493
4494 /* Merge st_other field. */
4495 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4496
4497 /* We don't want to make debug symbol dynamic. */
4498 if (definition
4499 && (sec->flags & SEC_DEBUGGING)
4500 && !bfd_link_relocatable (info))
4501 dynsym = FALSE;
4502
4503 /* Nor should we make plugin symbols dynamic. */
4504 if ((abfd->flags & BFD_PLUGIN) != 0)
4505 dynsym = FALSE;
4506
4507 if (definition)
4508 {
4509 h->target_internal = isym->st_target_internal;
4510 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4511 }
4512
4513 if (definition && !dynamic)
4514 {
4515 char *p = strchr (name, ELF_VER_CHR);
4516 if (p != NULL && p[1] != ELF_VER_CHR)
4517 {
4518 /* Queue non-default versions so that .symver x, x@FOO
4519 aliases can be checked. */
4520 if (!nondeflt_vers)
4521 {
4522 amt = ((isymend - isym + 1)
4523 * sizeof (struct elf_link_hash_entry *));
4524 nondeflt_vers
4525 = (struct elf_link_hash_entry **) bfd_malloc (amt);
4526 if (!nondeflt_vers)
4527 goto error_free_vers;
4528 }
4529 nondeflt_vers[nondeflt_vers_cnt++] = h;
4530 }
4531 }
4532
4533 if (dynsym && h->dynindx == -1)
4534 {
4535 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4536 goto error_free_vers;
4537 if (h->u.weakdef != NULL
4538 && ! new_weakdef
4539 && h->u.weakdef->dynindx == -1)
4540 {
4541 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4542 goto error_free_vers;
4543 }
4544 }
4545 else if (dynsym && h->dynindx != -1)
4546 /* If the symbol already has a dynamic index, but
4547 visibility says it should not be visible, turn it into
4548 a local symbol. */
4549 switch (ELF_ST_VISIBILITY (h->other))
4550 {
4551 case STV_INTERNAL:
4552 case STV_HIDDEN:
4553 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4554 dynsym = FALSE;
4555 break;
4556 }
4557
4558 /* Don't add DT_NEEDED for references from the dummy bfd. */
4559 if (!add_needed
4560 && definition
4561 && ((dynsym
4562 && h->ref_regular_nonweak
4563 && (old_bfd == NULL
4564 || (old_bfd->flags & BFD_PLUGIN) == 0))
4565 || (h->ref_dynamic_nonweak
4566 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4567 && !on_needed_list (elf_dt_name (abfd), htab->needed))))
4568 {
4569 int ret;
4570 const char *soname = elf_dt_name (abfd);
4571
4572 info->callbacks->minfo ("%!", soname, old_bfd,
4573 h->root.root.string);
4574
4575 /* A symbol from a library loaded via DT_NEEDED of some
4576 other library is referenced by a regular object.
4577 Add a DT_NEEDED entry for it. Issue an error if
4578 --no-add-needed is used and the reference was not
4579 a weak one. */
4580 if (old_bfd != NULL
4581 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4582 {
4583 (*_bfd_error_handler)
4584 (_("%B: undefined reference to symbol '%s'"),
4585 old_bfd, name);
4586 bfd_set_error (bfd_error_missing_dso);
4587 goto error_free_vers;
4588 }
4589
4590 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4591 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
4592
4593 add_needed = TRUE;
4594 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4595 if (ret < 0)
4596 goto error_free_vers;
4597
4598 BFD_ASSERT (ret == 0);
4599 }
4600 }
4601 }
4602
4603 if (extversym != NULL)
4604 {
4605 free (extversym);
4606 extversym = NULL;
4607 }
4608
4609 if (isymbuf != NULL)
4610 {
4611 free (isymbuf);
4612 isymbuf = NULL;
4613 }
4614
4615 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4616 {
4617 unsigned int i;
4618
4619 /* Restore the symbol table. */
4620 old_ent = (char *) old_tab + tabsize;
4621 memset (elf_sym_hashes (abfd), 0,
4622 extsymcount * sizeof (struct elf_link_hash_entry *));
4623 htab->root.table.table = old_table;
4624 htab->root.table.size = old_size;
4625 htab->root.table.count = old_count;
4626 memcpy (htab->root.table.table, old_tab, tabsize);
4627 htab->root.undefs = old_undefs;
4628 htab->root.undefs_tail = old_undefs_tail;
4629 _bfd_elf_strtab_restore_size (htab->dynstr, old_dynstr_size);
4630 for (i = 0; i < htab->root.table.size; i++)
4631 {
4632 struct bfd_hash_entry *p;
4633 struct elf_link_hash_entry *h;
4634 bfd_size_type size;
4635 unsigned int alignment_power;
4636
4637 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4638 {
4639 h = (struct elf_link_hash_entry *) p;
4640 if (h->root.type == bfd_link_hash_warning)
4641 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4642 if (h->dynindx >= old_dynsymcount
4643 && h->dynstr_index < old_dynstr_size)
4644 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
4645
4646 /* Preserve the maximum alignment and size for common
4647 symbols even if this dynamic lib isn't on DT_NEEDED
4648 since it can still be loaded at run time by another
4649 dynamic lib. */
4650 if (h->root.type == bfd_link_hash_common)
4651 {
4652 size = h->root.u.c.size;
4653 alignment_power = h->root.u.c.p->alignment_power;
4654 }
4655 else
4656 {
4657 size = 0;
4658 alignment_power = 0;
4659 }
4660 memcpy (p, old_ent, htab->root.table.entsize);
4661 old_ent = (char *) old_ent + htab->root.table.entsize;
4662 h = (struct elf_link_hash_entry *) p;
4663 if (h->root.type == bfd_link_hash_warning)
4664 {
4665 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4666 old_ent = (char *) old_ent + htab->root.table.entsize;
4667 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4668 }
4669 if (h->root.type == bfd_link_hash_common)
4670 {
4671 if (size > h->root.u.c.size)
4672 h->root.u.c.size = size;
4673 if (alignment_power > h->root.u.c.p->alignment_power)
4674 h->root.u.c.p->alignment_power = alignment_power;
4675 }
4676 }
4677 }
4678
4679 /* Make a special call to the linker "notice" function to
4680 tell it that symbols added for crefs may need to be removed. */
4681 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
4682 goto error_free_vers;
4683
4684 free (old_tab);
4685 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4686 alloc_mark);
4687 if (nondeflt_vers != NULL)
4688 free (nondeflt_vers);
4689 return TRUE;
4690 }
4691
4692 if (old_tab != NULL)
4693 {
4694 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
4695 goto error_free_vers;
4696 free (old_tab);
4697 old_tab = NULL;
4698 }
4699
4700 /* Now that all the symbols from this input file are created, if
4701 not performing a relocatable link, handle .symver foo, foo@BAR
4702 such that any relocs against foo become foo@BAR. */
4703 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4704 {
4705 bfd_size_type cnt, symidx;
4706
4707 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4708 {
4709 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4710 char *shortname, *p;
4711
4712 p = strchr (h->root.root.string, ELF_VER_CHR);
4713 if (p == NULL
4714 || (h->root.type != bfd_link_hash_defined
4715 && h->root.type != bfd_link_hash_defweak))
4716 continue;
4717
4718 amt = p - h->root.root.string;
4719 shortname = (char *) bfd_malloc (amt + 1);
4720 if (!shortname)
4721 goto error_free_vers;
4722 memcpy (shortname, h->root.root.string, amt);
4723 shortname[amt] = '\0';
4724
4725 hi = (struct elf_link_hash_entry *)
4726 bfd_link_hash_lookup (&htab->root, shortname,
4727 FALSE, FALSE, FALSE);
4728 if (hi != NULL
4729 && hi->root.type == h->root.type
4730 && hi->root.u.def.value == h->root.u.def.value
4731 && hi->root.u.def.section == h->root.u.def.section)
4732 {
4733 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4734 hi->root.type = bfd_link_hash_indirect;
4735 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4736 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4737 sym_hash = elf_sym_hashes (abfd);
4738 if (sym_hash)
4739 for (symidx = 0; symidx < extsymcount; ++symidx)
4740 if (sym_hash[symidx] == hi)
4741 {
4742 sym_hash[symidx] = h;
4743 break;
4744 }
4745 }
4746 free (shortname);
4747 }
4748 free (nondeflt_vers);
4749 nondeflt_vers = NULL;
4750 }
4751
4752 /* Now set the weakdefs field correctly for all the weak defined
4753 symbols we found. The only way to do this is to search all the
4754 symbols. Since we only need the information for non functions in
4755 dynamic objects, that's the only time we actually put anything on
4756 the list WEAKS. We need this information so that if a regular
4757 object refers to a symbol defined weakly in a dynamic object, the
4758 real symbol in the dynamic object is also put in the dynamic
4759 symbols; we also must arrange for both symbols to point to the
4760 same memory location. We could handle the general case of symbol
4761 aliasing, but a general symbol alias can only be generated in
4762 assembler code, handling it correctly would be very time
4763 consuming, and other ELF linkers don't handle general aliasing
4764 either. */
4765 if (weaks != NULL)
4766 {
4767 struct elf_link_hash_entry **hpp;
4768 struct elf_link_hash_entry **hppend;
4769 struct elf_link_hash_entry **sorted_sym_hash;
4770 struct elf_link_hash_entry *h;
4771 size_t sym_count;
4772
4773 /* Since we have to search the whole symbol list for each weak
4774 defined symbol, search time for N weak defined symbols will be
4775 O(N^2). Binary search will cut it down to O(NlogN). */
4776 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4777 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4778 if (sorted_sym_hash == NULL)
4779 goto error_return;
4780 sym_hash = sorted_sym_hash;
4781 hpp = elf_sym_hashes (abfd);
4782 hppend = hpp + extsymcount;
4783 sym_count = 0;
4784 for (; hpp < hppend; hpp++)
4785 {
4786 h = *hpp;
4787 if (h != NULL
4788 && h->root.type == bfd_link_hash_defined
4789 && !bed->is_function_type (h->type))
4790 {
4791 *sym_hash = h;
4792 sym_hash++;
4793 sym_count++;
4794 }
4795 }
4796
4797 qsort (sorted_sym_hash, sym_count,
4798 sizeof (struct elf_link_hash_entry *),
4799 elf_sort_symbol);
4800
4801 while (weaks != NULL)
4802 {
4803 struct elf_link_hash_entry *hlook;
4804 asection *slook;
4805 bfd_vma vlook;
4806 size_t i, j, idx = 0;
4807
4808 hlook = weaks;
4809 weaks = hlook->u.weakdef;
4810 hlook->u.weakdef = NULL;
4811
4812 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4813 || hlook->root.type == bfd_link_hash_defweak
4814 || hlook->root.type == bfd_link_hash_common
4815 || hlook->root.type == bfd_link_hash_indirect);
4816 slook = hlook->root.u.def.section;
4817 vlook = hlook->root.u.def.value;
4818
4819 i = 0;
4820 j = sym_count;
4821 while (i != j)
4822 {
4823 bfd_signed_vma vdiff;
4824 idx = (i + j) / 2;
4825 h = sorted_sym_hash[idx];
4826 vdiff = vlook - h->root.u.def.value;
4827 if (vdiff < 0)
4828 j = idx;
4829 else if (vdiff > 0)
4830 i = idx + 1;
4831 else
4832 {
4833 int sdiff = slook->id - h->root.u.def.section->id;
4834 if (sdiff < 0)
4835 j = idx;
4836 else if (sdiff > 0)
4837 i = idx + 1;
4838 else
4839 break;
4840 }
4841 }
4842
4843 /* We didn't find a value/section match. */
4844 if (i == j)
4845 continue;
4846
4847 /* With multiple aliases, or when the weak symbol is already
4848 strongly defined, we have multiple matching symbols and
4849 the binary search above may land on any of them. Step
4850 one past the matching symbol(s). */
4851 while (++idx != j)
4852 {
4853 h = sorted_sym_hash[idx];
4854 if (h->root.u.def.section != slook
4855 || h->root.u.def.value != vlook)
4856 break;
4857 }
4858
4859 /* Now look back over the aliases. Since we sorted by size
4860 as well as value and section, we'll choose the one with
4861 the largest size. */
4862 while (idx-- != i)
4863 {
4864 h = sorted_sym_hash[idx];
4865
4866 /* Stop if value or section doesn't match. */
4867 if (h->root.u.def.section != slook
4868 || h->root.u.def.value != vlook)
4869 break;
4870 else if (h != hlook)
4871 {
4872 hlook->u.weakdef = h;
4873
4874 /* If the weak definition is in the list of dynamic
4875 symbols, make sure the real definition is put
4876 there as well. */
4877 if (hlook->dynindx != -1 && h->dynindx == -1)
4878 {
4879 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4880 {
4881 err_free_sym_hash:
4882 free (sorted_sym_hash);
4883 goto error_return;
4884 }
4885 }
4886
4887 /* If the real definition is in the list of dynamic
4888 symbols, make sure the weak definition is put
4889 there as well. If we don't do this, then the
4890 dynamic loader might not merge the entries for the
4891 real definition and the weak definition. */
4892 if (h->dynindx != -1 && hlook->dynindx == -1)
4893 {
4894 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4895 goto err_free_sym_hash;
4896 }
4897 break;
4898 }
4899 }
4900 }
4901
4902 free (sorted_sym_hash);
4903 }
4904
4905 if (bed->check_directives
4906 && !(*bed->check_directives) (abfd, info))
4907 return FALSE;
4908
4909 /* If this object is the same format as the output object, and it is
4910 not a shared library, then let the backend look through the
4911 relocs.
4912
4913 This is required to build global offset table entries and to
4914 arrange for dynamic relocs. It is not required for the
4915 particular common case of linking non PIC code, even when linking
4916 against shared libraries, but unfortunately there is no way of
4917 knowing whether an object file has been compiled PIC or not.
4918 Looking through the relocs is not particularly time consuming.
4919 The problem is that we must either (1) keep the relocs in memory,
4920 which causes the linker to require additional runtime memory or
4921 (2) read the relocs twice from the input file, which wastes time.
4922 This would be a good case for using mmap.
4923
4924 I have no idea how to handle linking PIC code into a file of a
4925 different format. It probably can't be done. */
4926 if (! dynamic
4927 && is_elf_hash_table (htab)
4928 && bed->check_relocs != NULL
4929 && elf_object_id (abfd) == elf_hash_table_id (htab)
4930 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4931 {
4932 asection *o;
4933
4934 for (o = abfd->sections; o != NULL; o = o->next)
4935 {
4936 Elf_Internal_Rela *internal_relocs;
4937 bfd_boolean ok;
4938
4939 if ((o->flags & SEC_RELOC) == 0
4940 || o->reloc_count == 0
4941 || ((info->strip == strip_all || info->strip == strip_debugger)
4942 && (o->flags & SEC_DEBUGGING) != 0)
4943 || bfd_is_abs_section (o->output_section))
4944 continue;
4945
4946 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4947 info->keep_memory);
4948 if (internal_relocs == NULL)
4949 goto error_return;
4950
4951 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4952
4953 if (elf_section_data (o)->relocs != internal_relocs)
4954 free (internal_relocs);
4955
4956 if (! ok)
4957 goto error_return;
4958 }
4959 }
4960
4961 /* If this is a non-traditional link, try to optimize the handling
4962 of the .stab/.stabstr sections. */
4963 if (! dynamic
4964 && ! info->traditional_format
4965 && is_elf_hash_table (htab)
4966 && (info->strip != strip_all && info->strip != strip_debugger))
4967 {
4968 asection *stabstr;
4969
4970 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4971 if (stabstr != NULL)
4972 {
4973 bfd_size_type string_offset = 0;
4974 asection *stab;
4975
4976 for (stab = abfd->sections; stab; stab = stab->next)
4977 if (CONST_STRNEQ (stab->name, ".stab")
4978 && (!stab->name[5] ||
4979 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4980 && (stab->flags & SEC_MERGE) == 0
4981 && !bfd_is_abs_section (stab->output_section))
4982 {
4983 struct bfd_elf_section_data *secdata;
4984
4985 secdata = elf_section_data (stab);
4986 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
4987 stabstr, &secdata->sec_info,
4988 &string_offset))
4989 goto error_return;
4990 if (secdata->sec_info)
4991 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4992 }
4993 }
4994 }
4995
4996 if (is_elf_hash_table (htab) && add_needed)
4997 {
4998 /* Add this bfd to the loaded list. */
4999 struct elf_link_loaded_list *n;
5000
5001 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5002 if (n == NULL)
5003 goto error_return;
5004 n->abfd = abfd;
5005 n->next = htab->loaded;
5006 htab->loaded = n;
5007 }
5008
5009 return TRUE;
5010
5011 error_free_vers:
5012 if (old_tab != NULL)
5013 free (old_tab);
5014 if (nondeflt_vers != NULL)
5015 free (nondeflt_vers);
5016 if (extversym != NULL)
5017 free (extversym);
5018 error_free_sym:
5019 if (isymbuf != NULL)
5020 free (isymbuf);
5021 error_return:
5022 return FALSE;
5023 }
5024
5025 /* Return the linker hash table entry of a symbol that might be
5026 satisfied by an archive symbol. Return -1 on error. */
5027
5028 struct elf_link_hash_entry *
5029 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5030 struct bfd_link_info *info,
5031 const char *name)
5032 {
5033 struct elf_link_hash_entry *h;
5034 char *p, *copy;
5035 size_t len, first;
5036
5037 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5038 if (h != NULL)
5039 return h;
5040
5041 /* If this is a default version (the name contains @@), look up the
5042 symbol again with only one `@' as well as without the version.
5043 The effect is that references to the symbol with and without the
5044 version will be matched by the default symbol in the archive. */
5045
5046 p = strchr (name, ELF_VER_CHR);
5047 if (p == NULL || p[1] != ELF_VER_CHR)
5048 return h;
5049
5050 /* First check with only one `@'. */
5051 len = strlen (name);
5052 copy = (char *) bfd_alloc (abfd, len);
5053 if (copy == NULL)
5054 return (struct elf_link_hash_entry *) 0 - 1;
5055
5056 first = p - name + 1;
5057 memcpy (copy, name, first);
5058 memcpy (copy + first, name + first + 1, len - first);
5059
5060 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5061 if (h == NULL)
5062 {
5063 /* We also need to check references to the symbol without the
5064 version. */
5065 copy[first - 1] = '\0';
5066 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5067 FALSE, FALSE, TRUE);
5068 }
5069
5070 bfd_release (abfd, copy);
5071 return h;
5072 }
5073
5074 /* Add symbols from an ELF archive file to the linker hash table. We
5075 don't use _bfd_generic_link_add_archive_symbols because we need to
5076 handle versioned symbols.
5077
5078 Fortunately, ELF archive handling is simpler than that done by
5079 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5080 oddities. In ELF, if we find a symbol in the archive map, and the
5081 symbol is currently undefined, we know that we must pull in that
5082 object file.
5083
5084 Unfortunately, we do have to make multiple passes over the symbol
5085 table until nothing further is resolved. */
5086
5087 static bfd_boolean
5088 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5089 {
5090 symindex c;
5091 unsigned char *included = NULL;
5092 carsym *symdefs;
5093 bfd_boolean loop;
5094 bfd_size_type amt;
5095 const struct elf_backend_data *bed;
5096 struct elf_link_hash_entry * (*archive_symbol_lookup)
5097 (bfd *, struct bfd_link_info *, const char *);
5098
5099 if (! bfd_has_map (abfd))
5100 {
5101 /* An empty archive is a special case. */
5102 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5103 return TRUE;
5104 bfd_set_error (bfd_error_no_armap);
5105 return FALSE;
5106 }
5107
5108 /* Keep track of all symbols we know to be already defined, and all
5109 files we know to be already included. This is to speed up the
5110 second and subsequent passes. */
5111 c = bfd_ardata (abfd)->symdef_count;
5112 if (c == 0)
5113 return TRUE;
5114 amt = c;
5115 amt *= sizeof (*included);
5116 included = (unsigned char *) bfd_zmalloc (amt);
5117 if (included == NULL)
5118 return FALSE;
5119
5120 symdefs = bfd_ardata (abfd)->symdefs;
5121 bed = get_elf_backend_data (abfd);
5122 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5123
5124 do
5125 {
5126 file_ptr last;
5127 symindex i;
5128 carsym *symdef;
5129 carsym *symdefend;
5130
5131 loop = FALSE;
5132 last = -1;
5133
5134 symdef = symdefs;
5135 symdefend = symdef + c;
5136 for (i = 0; symdef < symdefend; symdef++, i++)
5137 {
5138 struct elf_link_hash_entry *h;
5139 bfd *element;
5140 struct bfd_link_hash_entry *undefs_tail;
5141 symindex mark;
5142
5143 if (included[i])
5144 continue;
5145 if (symdef->file_offset == last)
5146 {
5147 included[i] = TRUE;
5148 continue;
5149 }
5150
5151 h = archive_symbol_lookup (abfd, info, symdef->name);
5152 if (h == (struct elf_link_hash_entry *) 0 - 1)
5153 goto error_return;
5154
5155 if (h == NULL)
5156 continue;
5157
5158 if (h->root.type == bfd_link_hash_common)
5159 {
5160 /* We currently have a common symbol. The archive map contains
5161 a reference to this symbol, so we may want to include it. We
5162 only want to include it however, if this archive element
5163 contains a definition of the symbol, not just another common
5164 declaration of it.
5165
5166 Unfortunately some archivers (including GNU ar) will put
5167 declarations of common symbols into their archive maps, as
5168 well as real definitions, so we cannot just go by the archive
5169 map alone. Instead we must read in the element's symbol
5170 table and check that to see what kind of symbol definition
5171 this is. */
5172 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5173 continue;
5174 }
5175 else if (h->root.type != bfd_link_hash_undefined)
5176 {
5177 if (h->root.type != bfd_link_hash_undefweak)
5178 /* Symbol must be defined. Don't check it again. */
5179 included[i] = TRUE;
5180 continue;
5181 }
5182
5183 /* We need to include this archive member. */
5184 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5185 if (element == NULL)
5186 goto error_return;
5187
5188 if (! bfd_check_format (element, bfd_object))
5189 goto error_return;
5190
5191 undefs_tail = info->hash->undefs_tail;
5192
5193 if (!(*info->callbacks
5194 ->add_archive_element) (info, element, symdef->name, &element))
5195 goto error_return;
5196 if (!bfd_link_add_symbols (element, info))
5197 goto error_return;
5198
5199 /* If there are any new undefined symbols, we need to make
5200 another pass through the archive in order to see whether
5201 they can be defined. FIXME: This isn't perfect, because
5202 common symbols wind up on undefs_tail and because an
5203 undefined symbol which is defined later on in this pass
5204 does not require another pass. This isn't a bug, but it
5205 does make the code less efficient than it could be. */
5206 if (undefs_tail != info->hash->undefs_tail)
5207 loop = TRUE;
5208
5209 /* Look backward to mark all symbols from this object file
5210 which we have already seen in this pass. */
5211 mark = i;
5212 do
5213 {
5214 included[mark] = TRUE;
5215 if (mark == 0)
5216 break;
5217 --mark;
5218 }
5219 while (symdefs[mark].file_offset == symdef->file_offset);
5220
5221 /* We mark subsequent symbols from this object file as we go
5222 on through the loop. */
5223 last = symdef->file_offset;
5224 }
5225 }
5226 while (loop);
5227
5228 free (included);
5229
5230 return TRUE;
5231
5232 error_return:
5233 if (included != NULL)
5234 free (included);
5235 return FALSE;
5236 }
5237
5238 /* Given an ELF BFD, add symbols to the global hash table as
5239 appropriate. */
5240
5241 bfd_boolean
5242 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5243 {
5244 switch (bfd_get_format (abfd))
5245 {
5246 case bfd_object:
5247 return elf_link_add_object_symbols (abfd, info);
5248 case bfd_archive:
5249 return elf_link_add_archive_symbols (abfd, info);
5250 default:
5251 bfd_set_error (bfd_error_wrong_format);
5252 return FALSE;
5253 }
5254 }
5255 \f
5256 struct hash_codes_info
5257 {
5258 unsigned long *hashcodes;
5259 bfd_boolean error;
5260 };
5261
5262 /* This function will be called though elf_link_hash_traverse to store
5263 all hash value of the exported symbols in an array. */
5264
5265 static bfd_boolean
5266 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5267 {
5268 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5269 const char *name;
5270 unsigned long ha;
5271 char *alc = NULL;
5272
5273 /* Ignore indirect symbols. These are added by the versioning code. */
5274 if (h->dynindx == -1)
5275 return TRUE;
5276
5277 name = h->root.root.string;
5278 if (h->versioned >= versioned)
5279 {
5280 char *p = strchr (name, ELF_VER_CHR);
5281 if (p != NULL)
5282 {
5283 alc = (char *) bfd_malloc (p - name + 1);
5284 if (alc == NULL)
5285 {
5286 inf->error = TRUE;
5287 return FALSE;
5288 }
5289 memcpy (alc, name, p - name);
5290 alc[p - name] = '\0';
5291 name = alc;
5292 }
5293 }
5294
5295 /* Compute the hash value. */
5296 ha = bfd_elf_hash (name);
5297
5298 /* Store the found hash value in the array given as the argument. */
5299 *(inf->hashcodes)++ = ha;
5300
5301 /* And store it in the struct so that we can put it in the hash table
5302 later. */
5303 h->u.elf_hash_value = ha;
5304
5305 if (alc != NULL)
5306 free (alc);
5307
5308 return TRUE;
5309 }
5310
5311 struct collect_gnu_hash_codes
5312 {
5313 bfd *output_bfd;
5314 const struct elf_backend_data *bed;
5315 unsigned long int nsyms;
5316 unsigned long int maskbits;
5317 unsigned long int *hashcodes;
5318 unsigned long int *hashval;
5319 unsigned long int *indx;
5320 unsigned long int *counts;
5321 bfd_vma *bitmask;
5322 bfd_byte *contents;
5323 long int min_dynindx;
5324 unsigned long int bucketcount;
5325 unsigned long int symindx;
5326 long int local_indx;
5327 long int shift1, shift2;
5328 unsigned long int mask;
5329 bfd_boolean error;
5330 };
5331
5332 /* This function will be called though elf_link_hash_traverse to store
5333 all hash value of the exported symbols in an array. */
5334
5335 static bfd_boolean
5336 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5337 {
5338 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5339 const char *name;
5340 unsigned long ha;
5341 char *alc = NULL;
5342
5343 /* Ignore indirect symbols. These are added by the versioning code. */
5344 if (h->dynindx == -1)
5345 return TRUE;
5346
5347 /* Ignore also local symbols and undefined symbols. */
5348 if (! (*s->bed->elf_hash_symbol) (h))
5349 return TRUE;
5350
5351 name = h->root.root.string;
5352 if (h->versioned >= versioned)
5353 {
5354 char *p = strchr (name, ELF_VER_CHR);
5355 if (p != NULL)
5356 {
5357 alc = (char *) bfd_malloc (p - name + 1);
5358 if (alc == NULL)
5359 {
5360 s->error = TRUE;
5361 return FALSE;
5362 }
5363 memcpy (alc, name, p - name);
5364 alc[p - name] = '\0';
5365 name = alc;
5366 }
5367 }
5368
5369 /* Compute the hash value. */
5370 ha = bfd_elf_gnu_hash (name);
5371
5372 /* Store the found hash value in the array for compute_bucket_count,
5373 and also for .dynsym reordering purposes. */
5374 s->hashcodes[s->nsyms] = ha;
5375 s->hashval[h->dynindx] = ha;
5376 ++s->nsyms;
5377 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5378 s->min_dynindx = h->dynindx;
5379
5380 if (alc != NULL)
5381 free (alc);
5382
5383 return TRUE;
5384 }
5385
5386 /* This function will be called though elf_link_hash_traverse to do
5387 final dynaminc symbol renumbering. */
5388
5389 static bfd_boolean
5390 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5391 {
5392 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5393 unsigned long int bucket;
5394 unsigned long int val;
5395
5396 /* Ignore indirect symbols. */
5397 if (h->dynindx == -1)
5398 return TRUE;
5399
5400 /* Ignore also local symbols and undefined symbols. */
5401 if (! (*s->bed->elf_hash_symbol) (h))
5402 {
5403 if (h->dynindx >= s->min_dynindx)
5404 h->dynindx = s->local_indx++;
5405 return TRUE;
5406 }
5407
5408 bucket = s->hashval[h->dynindx] % s->bucketcount;
5409 val = (s->hashval[h->dynindx] >> s->shift1)
5410 & ((s->maskbits >> s->shift1) - 1);
5411 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5412 s->bitmask[val]
5413 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5414 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5415 if (s->counts[bucket] == 1)
5416 /* Last element terminates the chain. */
5417 val |= 1;
5418 bfd_put_32 (s->output_bfd, val,
5419 s->contents + (s->indx[bucket] - s->symindx) * 4);
5420 --s->counts[bucket];
5421 h->dynindx = s->indx[bucket]++;
5422 return TRUE;
5423 }
5424
5425 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5426
5427 bfd_boolean
5428 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5429 {
5430 return !(h->forced_local
5431 || h->root.type == bfd_link_hash_undefined
5432 || h->root.type == bfd_link_hash_undefweak
5433 || ((h->root.type == bfd_link_hash_defined
5434 || h->root.type == bfd_link_hash_defweak)
5435 && h->root.u.def.section->output_section == NULL));
5436 }
5437
5438 /* Array used to determine the number of hash table buckets to use
5439 based on the number of symbols there are. If there are fewer than
5440 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5441 fewer than 37 we use 17 buckets, and so forth. We never use more
5442 than 32771 buckets. */
5443
5444 static const size_t elf_buckets[] =
5445 {
5446 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5447 16411, 32771, 0
5448 };
5449
5450 /* Compute bucket count for hashing table. We do not use a static set
5451 of possible tables sizes anymore. Instead we determine for all
5452 possible reasonable sizes of the table the outcome (i.e., the
5453 number of collisions etc) and choose the best solution. The
5454 weighting functions are not too simple to allow the table to grow
5455 without bounds. Instead one of the weighting factors is the size.
5456 Therefore the result is always a good payoff between few collisions
5457 (= short chain lengths) and table size. */
5458 static size_t
5459 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5460 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5461 unsigned long int nsyms,
5462 int gnu_hash)
5463 {
5464 size_t best_size = 0;
5465 unsigned long int i;
5466
5467 /* We have a problem here. The following code to optimize the table
5468 size requires an integer type with more the 32 bits. If
5469 BFD_HOST_U_64_BIT is set we know about such a type. */
5470 #ifdef BFD_HOST_U_64_BIT
5471 if (info->optimize)
5472 {
5473 size_t minsize;
5474 size_t maxsize;
5475 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5476 bfd *dynobj = elf_hash_table (info)->dynobj;
5477 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5478 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5479 unsigned long int *counts;
5480 bfd_size_type amt;
5481 unsigned int no_improvement_count = 0;
5482
5483 /* Possible optimization parameters: if we have NSYMS symbols we say
5484 that the hashing table must at least have NSYMS/4 and at most
5485 2*NSYMS buckets. */
5486 minsize = nsyms / 4;
5487 if (minsize == 0)
5488 minsize = 1;
5489 best_size = maxsize = nsyms * 2;
5490 if (gnu_hash)
5491 {
5492 if (minsize < 2)
5493 minsize = 2;
5494 if ((best_size & 31) == 0)
5495 ++best_size;
5496 }
5497
5498 /* Create array where we count the collisions in. We must use bfd_malloc
5499 since the size could be large. */
5500 amt = maxsize;
5501 amt *= sizeof (unsigned long int);
5502 counts = (unsigned long int *) bfd_malloc (amt);
5503 if (counts == NULL)
5504 return 0;
5505
5506 /* Compute the "optimal" size for the hash table. The criteria is a
5507 minimal chain length. The minor criteria is (of course) the size
5508 of the table. */
5509 for (i = minsize; i < maxsize; ++i)
5510 {
5511 /* Walk through the array of hashcodes and count the collisions. */
5512 BFD_HOST_U_64_BIT max;
5513 unsigned long int j;
5514 unsigned long int fact;
5515
5516 if (gnu_hash && (i & 31) == 0)
5517 continue;
5518
5519 memset (counts, '\0', i * sizeof (unsigned long int));
5520
5521 /* Determine how often each hash bucket is used. */
5522 for (j = 0; j < nsyms; ++j)
5523 ++counts[hashcodes[j] % i];
5524
5525 /* For the weight function we need some information about the
5526 pagesize on the target. This is information need not be 100%
5527 accurate. Since this information is not available (so far) we
5528 define it here to a reasonable default value. If it is crucial
5529 to have a better value some day simply define this value. */
5530 # ifndef BFD_TARGET_PAGESIZE
5531 # define BFD_TARGET_PAGESIZE (4096)
5532 # endif
5533
5534 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5535 and the chains. */
5536 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5537
5538 # if 1
5539 /* Variant 1: optimize for short chains. We add the squares
5540 of all the chain lengths (which favors many small chain
5541 over a few long chains). */
5542 for (j = 0; j < i; ++j)
5543 max += counts[j] * counts[j];
5544
5545 /* This adds penalties for the overall size of the table. */
5546 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5547 max *= fact * fact;
5548 # else
5549 /* Variant 2: Optimize a lot more for small table. Here we
5550 also add squares of the size but we also add penalties for
5551 empty slots (the +1 term). */
5552 for (j = 0; j < i; ++j)
5553 max += (1 + counts[j]) * (1 + counts[j]);
5554
5555 /* The overall size of the table is considered, but not as
5556 strong as in variant 1, where it is squared. */
5557 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5558 max *= fact;
5559 # endif
5560
5561 /* Compare with current best results. */
5562 if (max < best_chlen)
5563 {
5564 best_chlen = max;
5565 best_size = i;
5566 no_improvement_count = 0;
5567 }
5568 /* PR 11843: Avoid futile long searches for the best bucket size
5569 when there are a large number of symbols. */
5570 else if (++no_improvement_count == 100)
5571 break;
5572 }
5573
5574 free (counts);
5575 }
5576 else
5577 #endif /* defined (BFD_HOST_U_64_BIT) */
5578 {
5579 /* This is the fallback solution if no 64bit type is available or if we
5580 are not supposed to spend much time on optimizations. We select the
5581 bucket count using a fixed set of numbers. */
5582 for (i = 0; elf_buckets[i] != 0; i++)
5583 {
5584 best_size = elf_buckets[i];
5585 if (nsyms < elf_buckets[i + 1])
5586 break;
5587 }
5588 if (gnu_hash && best_size < 2)
5589 best_size = 2;
5590 }
5591
5592 return best_size;
5593 }
5594
5595 /* Size any SHT_GROUP section for ld -r. */
5596
5597 bfd_boolean
5598 _bfd_elf_size_group_sections (struct bfd_link_info *info)
5599 {
5600 bfd *ibfd;
5601
5602 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5603 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5604 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5605 return FALSE;
5606 return TRUE;
5607 }
5608
5609 /* Set a default stack segment size. The value in INFO wins. If it
5610 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5611 undefined it is initialized. */
5612
5613 bfd_boolean
5614 bfd_elf_stack_segment_size (bfd *output_bfd,
5615 struct bfd_link_info *info,
5616 const char *legacy_symbol,
5617 bfd_vma default_size)
5618 {
5619 struct elf_link_hash_entry *h = NULL;
5620
5621 /* Look for legacy symbol. */
5622 if (legacy_symbol)
5623 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5624 FALSE, FALSE, FALSE);
5625 if (h && (h->root.type == bfd_link_hash_defined
5626 || h->root.type == bfd_link_hash_defweak)
5627 && h->def_regular
5628 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5629 {
5630 /* The symbol has no type if specified on the command line. */
5631 h->type = STT_OBJECT;
5632 if (info->stacksize)
5633 (*_bfd_error_handler) (_("%B: stack size specified and %s set"),
5634 output_bfd, legacy_symbol);
5635 else if (h->root.u.def.section != bfd_abs_section_ptr)
5636 (*_bfd_error_handler) (_("%B: %s not absolute"),
5637 output_bfd, legacy_symbol);
5638 else
5639 info->stacksize = h->root.u.def.value;
5640 }
5641
5642 if (!info->stacksize)
5643 /* If the user didn't set a size, or explicitly inhibit the
5644 size, set it now. */
5645 info->stacksize = default_size;
5646
5647 /* Provide the legacy symbol, if it is referenced. */
5648 if (h && (h->root.type == bfd_link_hash_undefined
5649 || h->root.type == bfd_link_hash_undefweak))
5650 {
5651 struct bfd_link_hash_entry *bh = NULL;
5652
5653 if (!(_bfd_generic_link_add_one_symbol
5654 (info, output_bfd, legacy_symbol,
5655 BSF_GLOBAL, bfd_abs_section_ptr,
5656 info->stacksize >= 0 ? info->stacksize : 0,
5657 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5658 return FALSE;
5659
5660 h = (struct elf_link_hash_entry *) bh;
5661 h->def_regular = 1;
5662 h->type = STT_OBJECT;
5663 }
5664
5665 return TRUE;
5666 }
5667
5668 /* Set up the sizes and contents of the ELF dynamic sections. This is
5669 called by the ELF linker emulation before_allocation routine. We
5670 must set the sizes of the sections before the linker sets the
5671 addresses of the various sections. */
5672
5673 bfd_boolean
5674 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5675 const char *soname,
5676 const char *rpath,
5677 const char *filter_shlib,
5678 const char *audit,
5679 const char *depaudit,
5680 const char * const *auxiliary_filters,
5681 struct bfd_link_info *info,
5682 asection **sinterpptr)
5683 {
5684 bfd_size_type soname_indx;
5685 bfd *dynobj;
5686 const struct elf_backend_data *bed;
5687 struct elf_info_failed asvinfo;
5688
5689 *sinterpptr = NULL;
5690
5691 soname_indx = (bfd_size_type) -1;
5692
5693 if (!is_elf_hash_table (info->hash))
5694 return TRUE;
5695
5696 bed = get_elf_backend_data (output_bfd);
5697
5698 /* Any syms created from now on start with -1 in
5699 got.refcount/offset and plt.refcount/offset. */
5700 elf_hash_table (info)->init_got_refcount
5701 = elf_hash_table (info)->init_got_offset;
5702 elf_hash_table (info)->init_plt_refcount
5703 = elf_hash_table (info)->init_plt_offset;
5704
5705 if (bfd_link_relocatable (info)
5706 && !_bfd_elf_size_group_sections (info))
5707 return FALSE;
5708
5709 /* The backend may have to create some sections regardless of whether
5710 we're dynamic or not. */
5711 if (bed->elf_backend_always_size_sections
5712 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5713 return FALSE;
5714
5715 /* Determine any GNU_STACK segment requirements, after the backend
5716 has had a chance to set a default segment size. */
5717 if (info->execstack)
5718 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
5719 else if (info->noexecstack)
5720 elf_stack_flags (output_bfd) = PF_R | PF_W;
5721 else
5722 {
5723 bfd *inputobj;
5724 asection *notesec = NULL;
5725 int exec = 0;
5726
5727 for (inputobj = info->input_bfds;
5728 inputobj;
5729 inputobj = inputobj->link.next)
5730 {
5731 asection *s;
5732
5733 if (inputobj->flags
5734 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5735 continue;
5736 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5737 if (s)
5738 {
5739 if (s->flags & SEC_CODE)
5740 exec = PF_X;
5741 notesec = s;
5742 }
5743 else if (bed->default_execstack)
5744 exec = PF_X;
5745 }
5746 if (notesec || info->stacksize > 0)
5747 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
5748 if (notesec && exec && bfd_link_relocatable (info)
5749 && notesec->output_section != bfd_abs_section_ptr)
5750 notesec->output_section->flags |= SEC_CODE;
5751 }
5752
5753 dynobj = elf_hash_table (info)->dynobj;
5754
5755 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5756 {
5757 struct elf_info_failed eif;
5758 struct elf_link_hash_entry *h;
5759 asection *dynstr;
5760 struct bfd_elf_version_tree *t;
5761 struct bfd_elf_version_expr *d;
5762 asection *s;
5763 bfd_boolean all_defined;
5764
5765 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
5766 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
5767
5768 if (soname != NULL)
5769 {
5770 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5771 soname, TRUE);
5772 if (soname_indx == (bfd_size_type) -1
5773 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5774 return FALSE;
5775 }
5776
5777 if (info->symbolic)
5778 {
5779 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5780 return FALSE;
5781 info->flags |= DF_SYMBOLIC;
5782 }
5783
5784 if (rpath != NULL)
5785 {
5786 bfd_size_type indx;
5787 bfd_vma tag;
5788
5789 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5790 TRUE);
5791 if (indx == (bfd_size_type) -1)
5792 return FALSE;
5793
5794 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
5795 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
5796 return FALSE;
5797 }
5798
5799 if (filter_shlib != NULL)
5800 {
5801 bfd_size_type indx;
5802
5803 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5804 filter_shlib, TRUE);
5805 if (indx == (bfd_size_type) -1
5806 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5807 return FALSE;
5808 }
5809
5810 if (auxiliary_filters != NULL)
5811 {
5812 const char * const *p;
5813
5814 for (p = auxiliary_filters; *p != NULL; p++)
5815 {
5816 bfd_size_type indx;
5817
5818 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5819 *p, TRUE);
5820 if (indx == (bfd_size_type) -1
5821 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5822 return FALSE;
5823 }
5824 }
5825
5826 if (audit != NULL)
5827 {
5828 bfd_size_type indx;
5829
5830 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5831 TRUE);
5832 if (indx == (bfd_size_type) -1
5833 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5834 return FALSE;
5835 }
5836
5837 if (depaudit != NULL)
5838 {
5839 bfd_size_type indx;
5840
5841 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
5842 TRUE);
5843 if (indx == (bfd_size_type) -1
5844 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
5845 return FALSE;
5846 }
5847
5848 eif.info = info;
5849 eif.failed = FALSE;
5850
5851 /* If we are supposed to export all symbols into the dynamic symbol
5852 table (this is not the normal case), then do so. */
5853 if (info->export_dynamic
5854 || (bfd_link_executable (info) && info->dynamic))
5855 {
5856 elf_link_hash_traverse (elf_hash_table (info),
5857 _bfd_elf_export_symbol,
5858 &eif);
5859 if (eif.failed)
5860 return FALSE;
5861 }
5862
5863 /* Make all global versions with definition. */
5864 for (t = info->version_info; t != NULL; t = t->next)
5865 for (d = t->globals.list; d != NULL; d = d->next)
5866 if (!d->symver && d->literal)
5867 {
5868 const char *verstr, *name;
5869 size_t namelen, verlen, newlen;
5870 char *newname, *p, leading_char;
5871 struct elf_link_hash_entry *newh;
5872
5873 leading_char = bfd_get_symbol_leading_char (output_bfd);
5874 name = d->pattern;
5875 namelen = strlen (name) + (leading_char != '\0');
5876 verstr = t->name;
5877 verlen = strlen (verstr);
5878 newlen = namelen + verlen + 3;
5879
5880 newname = (char *) bfd_malloc (newlen);
5881 if (newname == NULL)
5882 return FALSE;
5883 newname[0] = leading_char;
5884 memcpy (newname + (leading_char != '\0'), name, namelen);
5885
5886 /* Check the hidden versioned definition. */
5887 p = newname + namelen;
5888 *p++ = ELF_VER_CHR;
5889 memcpy (p, verstr, verlen + 1);
5890 newh = elf_link_hash_lookup (elf_hash_table (info),
5891 newname, FALSE, FALSE,
5892 FALSE);
5893 if (newh == NULL
5894 || (newh->root.type != bfd_link_hash_defined
5895 && newh->root.type != bfd_link_hash_defweak))
5896 {
5897 /* Check the default versioned definition. */
5898 *p++ = ELF_VER_CHR;
5899 memcpy (p, verstr, verlen + 1);
5900 newh = elf_link_hash_lookup (elf_hash_table (info),
5901 newname, FALSE, FALSE,
5902 FALSE);
5903 }
5904 free (newname);
5905
5906 /* Mark this version if there is a definition and it is
5907 not defined in a shared object. */
5908 if (newh != NULL
5909 && !newh->def_dynamic
5910 && (newh->root.type == bfd_link_hash_defined
5911 || newh->root.type == bfd_link_hash_defweak))
5912 d->symver = 1;
5913 }
5914
5915 /* Attach all the symbols to their version information. */
5916 asvinfo.info = info;
5917 asvinfo.failed = FALSE;
5918
5919 elf_link_hash_traverse (elf_hash_table (info),
5920 _bfd_elf_link_assign_sym_version,
5921 &asvinfo);
5922 if (asvinfo.failed)
5923 return FALSE;
5924
5925 if (!info->allow_undefined_version)
5926 {
5927 /* Check if all global versions have a definition. */
5928 all_defined = TRUE;
5929 for (t = info->version_info; t != NULL; t = t->next)
5930 for (d = t->globals.list; d != NULL; d = d->next)
5931 if (d->literal && !d->symver && !d->script)
5932 {
5933 (*_bfd_error_handler)
5934 (_("%s: undefined version: %s"),
5935 d->pattern, t->name);
5936 all_defined = FALSE;
5937 }
5938
5939 if (!all_defined)
5940 {
5941 bfd_set_error (bfd_error_bad_value);
5942 return FALSE;
5943 }
5944 }
5945
5946 /* Find all symbols which were defined in a dynamic object and make
5947 the backend pick a reasonable value for them. */
5948 elf_link_hash_traverse (elf_hash_table (info),
5949 _bfd_elf_adjust_dynamic_symbol,
5950 &eif);
5951 if (eif.failed)
5952 return FALSE;
5953
5954 /* Add some entries to the .dynamic section. We fill in some of the
5955 values later, in bfd_elf_final_link, but we must add the entries
5956 now so that we know the final size of the .dynamic section. */
5957
5958 /* If there are initialization and/or finalization functions to
5959 call then add the corresponding DT_INIT/DT_FINI entries. */
5960 h = (info->init_function
5961 ? elf_link_hash_lookup (elf_hash_table (info),
5962 info->init_function, FALSE,
5963 FALSE, FALSE)
5964 : NULL);
5965 if (h != NULL
5966 && (h->ref_regular
5967 || h->def_regular))
5968 {
5969 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5970 return FALSE;
5971 }
5972 h = (info->fini_function
5973 ? elf_link_hash_lookup (elf_hash_table (info),
5974 info->fini_function, FALSE,
5975 FALSE, FALSE)
5976 : NULL);
5977 if (h != NULL
5978 && (h->ref_regular
5979 || h->def_regular))
5980 {
5981 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5982 return FALSE;
5983 }
5984
5985 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5986 if (s != NULL && s->linker_has_input)
5987 {
5988 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5989 if (! bfd_link_executable (info))
5990 {
5991 bfd *sub;
5992 asection *o;
5993
5994 for (sub = info->input_bfds; sub != NULL;
5995 sub = sub->link.next)
5996 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
5997 for (o = sub->sections; o != NULL; o = o->next)
5998 if (elf_section_data (o)->this_hdr.sh_type
5999 == SHT_PREINIT_ARRAY)
6000 {
6001 (*_bfd_error_handler)
6002 (_("%B: .preinit_array section is not allowed in DSO"),
6003 sub);
6004 break;
6005 }
6006
6007 bfd_set_error (bfd_error_nonrepresentable_section);
6008 return FALSE;
6009 }
6010
6011 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6012 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6013 return FALSE;
6014 }
6015 s = bfd_get_section_by_name (output_bfd, ".init_array");
6016 if (s != NULL && s->linker_has_input)
6017 {
6018 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6019 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6020 return FALSE;
6021 }
6022 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6023 if (s != NULL && s->linker_has_input)
6024 {
6025 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6026 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6027 return FALSE;
6028 }
6029
6030 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6031 /* If .dynstr is excluded from the link, we don't want any of
6032 these tags. Strictly, we should be checking each section
6033 individually; This quick check covers for the case where
6034 someone does a /DISCARD/ : { *(*) }. */
6035 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6036 {
6037 bfd_size_type strsize;
6038
6039 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6040 if ((info->emit_hash
6041 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6042 || (info->emit_gnu_hash
6043 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6044 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6045 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6046 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6047 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6048 bed->s->sizeof_sym))
6049 return FALSE;
6050 }
6051 }
6052
6053 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6054 return FALSE;
6055
6056 /* The backend must work out the sizes of all the other dynamic
6057 sections. */
6058 if (dynobj != NULL
6059 && bed->elf_backend_size_dynamic_sections != NULL
6060 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6061 return FALSE;
6062
6063 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6064 {
6065 unsigned long section_sym_count;
6066 struct bfd_elf_version_tree *verdefs;
6067 asection *s;
6068
6069 /* Set up the version definition section. */
6070 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6071 BFD_ASSERT (s != NULL);
6072
6073 /* We may have created additional version definitions if we are
6074 just linking a regular application. */
6075 verdefs = info->version_info;
6076
6077 /* Skip anonymous version tag. */
6078 if (verdefs != NULL && verdefs->vernum == 0)
6079 verdefs = verdefs->next;
6080
6081 if (verdefs == NULL && !info->create_default_symver)
6082 s->flags |= SEC_EXCLUDE;
6083 else
6084 {
6085 unsigned int cdefs;
6086 bfd_size_type size;
6087 struct bfd_elf_version_tree *t;
6088 bfd_byte *p;
6089 Elf_Internal_Verdef def;
6090 Elf_Internal_Verdaux defaux;
6091 struct bfd_link_hash_entry *bh;
6092 struct elf_link_hash_entry *h;
6093 const char *name;
6094
6095 cdefs = 0;
6096 size = 0;
6097
6098 /* Make space for the base version. */
6099 size += sizeof (Elf_External_Verdef);
6100 size += sizeof (Elf_External_Verdaux);
6101 ++cdefs;
6102
6103 /* Make space for the default version. */
6104 if (info->create_default_symver)
6105 {
6106 size += sizeof (Elf_External_Verdef);
6107 ++cdefs;
6108 }
6109
6110 for (t = verdefs; t != NULL; t = t->next)
6111 {
6112 struct bfd_elf_version_deps *n;
6113
6114 /* Don't emit base version twice. */
6115 if (t->vernum == 0)
6116 continue;
6117
6118 size += sizeof (Elf_External_Verdef);
6119 size += sizeof (Elf_External_Verdaux);
6120 ++cdefs;
6121
6122 for (n = t->deps; n != NULL; n = n->next)
6123 size += sizeof (Elf_External_Verdaux);
6124 }
6125
6126 s->size = size;
6127 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6128 if (s->contents == NULL && s->size != 0)
6129 return FALSE;
6130
6131 /* Fill in the version definition section. */
6132
6133 p = s->contents;
6134
6135 def.vd_version = VER_DEF_CURRENT;
6136 def.vd_flags = VER_FLG_BASE;
6137 def.vd_ndx = 1;
6138 def.vd_cnt = 1;
6139 if (info->create_default_symver)
6140 {
6141 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6142 def.vd_next = sizeof (Elf_External_Verdef);
6143 }
6144 else
6145 {
6146 def.vd_aux = sizeof (Elf_External_Verdef);
6147 def.vd_next = (sizeof (Elf_External_Verdef)
6148 + sizeof (Elf_External_Verdaux));
6149 }
6150
6151 if (soname_indx != (bfd_size_type) -1)
6152 {
6153 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6154 soname_indx);
6155 def.vd_hash = bfd_elf_hash (soname);
6156 defaux.vda_name = soname_indx;
6157 name = soname;
6158 }
6159 else
6160 {
6161 bfd_size_type indx;
6162
6163 name = lbasename (output_bfd->filename);
6164 def.vd_hash = bfd_elf_hash (name);
6165 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6166 name, FALSE);
6167 if (indx == (bfd_size_type) -1)
6168 return FALSE;
6169 defaux.vda_name = indx;
6170 }
6171 defaux.vda_next = 0;
6172
6173 _bfd_elf_swap_verdef_out (output_bfd, &def,
6174 (Elf_External_Verdef *) p);
6175 p += sizeof (Elf_External_Verdef);
6176 if (info->create_default_symver)
6177 {
6178 /* Add a symbol representing this version. */
6179 bh = NULL;
6180 if (! (_bfd_generic_link_add_one_symbol
6181 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6182 0, NULL, FALSE,
6183 get_elf_backend_data (dynobj)->collect, &bh)))
6184 return FALSE;
6185 h = (struct elf_link_hash_entry *) bh;
6186 h->non_elf = 0;
6187 h->def_regular = 1;
6188 h->type = STT_OBJECT;
6189 h->verinfo.vertree = NULL;
6190
6191 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6192 return FALSE;
6193
6194 /* Create a duplicate of the base version with the same
6195 aux block, but different flags. */
6196 def.vd_flags = 0;
6197 def.vd_ndx = 2;
6198 def.vd_aux = sizeof (Elf_External_Verdef);
6199 if (verdefs)
6200 def.vd_next = (sizeof (Elf_External_Verdef)
6201 + sizeof (Elf_External_Verdaux));
6202 else
6203 def.vd_next = 0;
6204 _bfd_elf_swap_verdef_out (output_bfd, &def,
6205 (Elf_External_Verdef *) p);
6206 p += sizeof (Elf_External_Verdef);
6207 }
6208 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6209 (Elf_External_Verdaux *) p);
6210 p += sizeof (Elf_External_Verdaux);
6211
6212 for (t = verdefs; t != NULL; t = t->next)
6213 {
6214 unsigned int cdeps;
6215 struct bfd_elf_version_deps *n;
6216
6217 /* Don't emit the base version twice. */
6218 if (t->vernum == 0)
6219 continue;
6220
6221 cdeps = 0;
6222 for (n = t->deps; n != NULL; n = n->next)
6223 ++cdeps;
6224
6225 /* Add a symbol representing this version. */
6226 bh = NULL;
6227 if (! (_bfd_generic_link_add_one_symbol
6228 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6229 0, NULL, FALSE,
6230 get_elf_backend_data (dynobj)->collect, &bh)))
6231 return FALSE;
6232 h = (struct elf_link_hash_entry *) bh;
6233 h->non_elf = 0;
6234 h->def_regular = 1;
6235 h->type = STT_OBJECT;
6236 h->verinfo.vertree = t;
6237
6238 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6239 return FALSE;
6240
6241 def.vd_version = VER_DEF_CURRENT;
6242 def.vd_flags = 0;
6243 if (t->globals.list == NULL
6244 && t->locals.list == NULL
6245 && ! t->used)
6246 def.vd_flags |= VER_FLG_WEAK;
6247 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6248 def.vd_cnt = cdeps + 1;
6249 def.vd_hash = bfd_elf_hash (t->name);
6250 def.vd_aux = sizeof (Elf_External_Verdef);
6251 def.vd_next = 0;
6252
6253 /* If a basever node is next, it *must* be the last node in
6254 the chain, otherwise Verdef construction breaks. */
6255 if (t->next != NULL && t->next->vernum == 0)
6256 BFD_ASSERT (t->next->next == NULL);
6257
6258 if (t->next != NULL && t->next->vernum != 0)
6259 def.vd_next = (sizeof (Elf_External_Verdef)
6260 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6261
6262 _bfd_elf_swap_verdef_out (output_bfd, &def,
6263 (Elf_External_Verdef *) p);
6264 p += sizeof (Elf_External_Verdef);
6265
6266 defaux.vda_name = h->dynstr_index;
6267 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6268 h->dynstr_index);
6269 defaux.vda_next = 0;
6270 if (t->deps != NULL)
6271 defaux.vda_next = sizeof (Elf_External_Verdaux);
6272 t->name_indx = defaux.vda_name;
6273
6274 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6275 (Elf_External_Verdaux *) p);
6276 p += sizeof (Elf_External_Verdaux);
6277
6278 for (n = t->deps; n != NULL; n = n->next)
6279 {
6280 if (n->version_needed == NULL)
6281 {
6282 /* This can happen if there was an error in the
6283 version script. */
6284 defaux.vda_name = 0;
6285 }
6286 else
6287 {
6288 defaux.vda_name = n->version_needed->name_indx;
6289 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6290 defaux.vda_name);
6291 }
6292 if (n->next == NULL)
6293 defaux.vda_next = 0;
6294 else
6295 defaux.vda_next = sizeof (Elf_External_Verdaux);
6296
6297 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6298 (Elf_External_Verdaux *) p);
6299 p += sizeof (Elf_External_Verdaux);
6300 }
6301 }
6302
6303 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6304 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6305 return FALSE;
6306
6307 elf_tdata (output_bfd)->cverdefs = cdefs;
6308 }
6309
6310 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6311 {
6312 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6313 return FALSE;
6314 }
6315 else if (info->flags & DF_BIND_NOW)
6316 {
6317 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6318 return FALSE;
6319 }
6320
6321 if (info->flags_1)
6322 {
6323 if (bfd_link_executable (info))
6324 info->flags_1 &= ~ (DF_1_INITFIRST
6325 | DF_1_NODELETE
6326 | DF_1_NOOPEN);
6327 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6328 return FALSE;
6329 }
6330
6331 /* Work out the size of the version reference section. */
6332
6333 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6334 BFD_ASSERT (s != NULL);
6335 {
6336 struct elf_find_verdep_info sinfo;
6337
6338 sinfo.info = info;
6339 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6340 if (sinfo.vers == 0)
6341 sinfo.vers = 1;
6342 sinfo.failed = FALSE;
6343
6344 elf_link_hash_traverse (elf_hash_table (info),
6345 _bfd_elf_link_find_version_dependencies,
6346 &sinfo);
6347 if (sinfo.failed)
6348 return FALSE;
6349
6350 if (elf_tdata (output_bfd)->verref == NULL)
6351 s->flags |= SEC_EXCLUDE;
6352 else
6353 {
6354 Elf_Internal_Verneed *t;
6355 unsigned int size;
6356 unsigned int crefs;
6357 bfd_byte *p;
6358
6359 /* Build the version dependency section. */
6360 size = 0;
6361 crefs = 0;
6362 for (t = elf_tdata (output_bfd)->verref;
6363 t != NULL;
6364 t = t->vn_nextref)
6365 {
6366 Elf_Internal_Vernaux *a;
6367
6368 size += sizeof (Elf_External_Verneed);
6369 ++crefs;
6370 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6371 size += sizeof (Elf_External_Vernaux);
6372 }
6373
6374 s->size = size;
6375 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6376 if (s->contents == NULL)
6377 return FALSE;
6378
6379 p = s->contents;
6380 for (t = elf_tdata (output_bfd)->verref;
6381 t != NULL;
6382 t = t->vn_nextref)
6383 {
6384 unsigned int caux;
6385 Elf_Internal_Vernaux *a;
6386 bfd_size_type indx;
6387
6388 caux = 0;
6389 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6390 ++caux;
6391
6392 t->vn_version = VER_NEED_CURRENT;
6393 t->vn_cnt = caux;
6394 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6395 elf_dt_name (t->vn_bfd) != NULL
6396 ? elf_dt_name (t->vn_bfd)
6397 : lbasename (t->vn_bfd->filename),
6398 FALSE);
6399 if (indx == (bfd_size_type) -1)
6400 return FALSE;
6401 t->vn_file = indx;
6402 t->vn_aux = sizeof (Elf_External_Verneed);
6403 if (t->vn_nextref == NULL)
6404 t->vn_next = 0;
6405 else
6406 t->vn_next = (sizeof (Elf_External_Verneed)
6407 + caux * sizeof (Elf_External_Vernaux));
6408
6409 _bfd_elf_swap_verneed_out (output_bfd, t,
6410 (Elf_External_Verneed *) p);
6411 p += sizeof (Elf_External_Verneed);
6412
6413 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6414 {
6415 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6416 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6417 a->vna_nodename, FALSE);
6418 if (indx == (bfd_size_type) -1)
6419 return FALSE;
6420 a->vna_name = indx;
6421 if (a->vna_nextptr == NULL)
6422 a->vna_next = 0;
6423 else
6424 a->vna_next = sizeof (Elf_External_Vernaux);
6425
6426 _bfd_elf_swap_vernaux_out (output_bfd, a,
6427 (Elf_External_Vernaux *) p);
6428 p += sizeof (Elf_External_Vernaux);
6429 }
6430 }
6431
6432 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6433 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6434 return FALSE;
6435
6436 elf_tdata (output_bfd)->cverrefs = crefs;
6437 }
6438 }
6439
6440 if ((elf_tdata (output_bfd)->cverrefs == 0
6441 && elf_tdata (output_bfd)->cverdefs == 0)
6442 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6443 &section_sym_count) == 0)
6444 {
6445 s = bfd_get_linker_section (dynobj, ".gnu.version");
6446 s->flags |= SEC_EXCLUDE;
6447 }
6448 }
6449 return TRUE;
6450 }
6451
6452 /* Find the first non-excluded output section. We'll use its
6453 section symbol for some emitted relocs. */
6454 void
6455 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6456 {
6457 asection *s;
6458
6459 for (s = output_bfd->sections; s != NULL; s = s->next)
6460 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6461 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6462 {
6463 elf_hash_table (info)->text_index_section = s;
6464 break;
6465 }
6466 }
6467
6468 /* Find two non-excluded output sections, one for code, one for data.
6469 We'll use their section symbols for some emitted relocs. */
6470 void
6471 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6472 {
6473 asection *s;
6474
6475 /* Data first, since setting text_index_section changes
6476 _bfd_elf_link_omit_section_dynsym. */
6477 for (s = output_bfd->sections; s != NULL; s = s->next)
6478 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6479 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6480 {
6481 elf_hash_table (info)->data_index_section = s;
6482 break;
6483 }
6484
6485 for (s = output_bfd->sections; s != NULL; s = s->next)
6486 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6487 == (SEC_ALLOC | SEC_READONLY))
6488 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6489 {
6490 elf_hash_table (info)->text_index_section = s;
6491 break;
6492 }
6493
6494 if (elf_hash_table (info)->text_index_section == NULL)
6495 elf_hash_table (info)->text_index_section
6496 = elf_hash_table (info)->data_index_section;
6497 }
6498
6499 bfd_boolean
6500 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6501 {
6502 const struct elf_backend_data *bed;
6503
6504 if (!is_elf_hash_table (info->hash))
6505 return TRUE;
6506
6507 bed = get_elf_backend_data (output_bfd);
6508 (*bed->elf_backend_init_index_section) (output_bfd, info);
6509
6510 if (elf_hash_table (info)->dynamic_sections_created)
6511 {
6512 bfd *dynobj;
6513 asection *s;
6514 bfd_size_type dynsymcount;
6515 unsigned long section_sym_count;
6516 unsigned int dtagcount;
6517
6518 dynobj = elf_hash_table (info)->dynobj;
6519
6520 /* Assign dynsym indicies. In a shared library we generate a
6521 section symbol for each output section, which come first.
6522 Next come all of the back-end allocated local dynamic syms,
6523 followed by the rest of the global symbols. */
6524
6525 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6526 &section_sym_count);
6527
6528 /* Work out the size of the symbol version section. */
6529 s = bfd_get_linker_section (dynobj, ".gnu.version");
6530 BFD_ASSERT (s != NULL);
6531 if (dynsymcount != 0
6532 && (s->flags & SEC_EXCLUDE) == 0)
6533 {
6534 s->size = dynsymcount * sizeof (Elf_External_Versym);
6535 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6536 if (s->contents == NULL)
6537 return FALSE;
6538
6539 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6540 return FALSE;
6541 }
6542
6543 /* Set the size of the .dynsym and .hash sections. We counted
6544 the number of dynamic symbols in elf_link_add_object_symbols.
6545 We will build the contents of .dynsym and .hash when we build
6546 the final symbol table, because until then we do not know the
6547 correct value to give the symbols. We built the .dynstr
6548 section as we went along in elf_link_add_object_symbols. */
6549 s = elf_hash_table (info)->dynsym;
6550 BFD_ASSERT (s != NULL);
6551 s->size = dynsymcount * bed->s->sizeof_sym;
6552
6553 if (dynsymcount != 0)
6554 {
6555 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6556 if (s->contents == NULL)
6557 return FALSE;
6558
6559 /* The first entry in .dynsym is a dummy symbol.
6560 Clear all the section syms, in case we don't output them all. */
6561 ++section_sym_count;
6562 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6563 }
6564
6565 elf_hash_table (info)->bucketcount = 0;
6566
6567 /* Compute the size of the hashing table. As a side effect this
6568 computes the hash values for all the names we export. */
6569 if (info->emit_hash)
6570 {
6571 unsigned long int *hashcodes;
6572 struct hash_codes_info hashinf;
6573 bfd_size_type amt;
6574 unsigned long int nsyms;
6575 size_t bucketcount;
6576 size_t hash_entry_size;
6577
6578 /* Compute the hash values for all exported symbols. At the same
6579 time store the values in an array so that we could use them for
6580 optimizations. */
6581 amt = dynsymcount * sizeof (unsigned long int);
6582 hashcodes = (unsigned long int *) bfd_malloc (amt);
6583 if (hashcodes == NULL)
6584 return FALSE;
6585 hashinf.hashcodes = hashcodes;
6586 hashinf.error = FALSE;
6587
6588 /* Put all hash values in HASHCODES. */
6589 elf_link_hash_traverse (elf_hash_table (info),
6590 elf_collect_hash_codes, &hashinf);
6591 if (hashinf.error)
6592 {
6593 free (hashcodes);
6594 return FALSE;
6595 }
6596
6597 nsyms = hashinf.hashcodes - hashcodes;
6598 bucketcount
6599 = compute_bucket_count (info, hashcodes, nsyms, 0);
6600 free (hashcodes);
6601
6602 if (bucketcount == 0)
6603 return FALSE;
6604
6605 elf_hash_table (info)->bucketcount = bucketcount;
6606
6607 s = bfd_get_linker_section (dynobj, ".hash");
6608 BFD_ASSERT (s != NULL);
6609 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6610 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6611 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6612 if (s->contents == NULL)
6613 return FALSE;
6614
6615 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6616 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6617 s->contents + hash_entry_size);
6618 }
6619
6620 if (info->emit_gnu_hash)
6621 {
6622 size_t i, cnt;
6623 unsigned char *contents;
6624 struct collect_gnu_hash_codes cinfo;
6625 bfd_size_type amt;
6626 size_t bucketcount;
6627
6628 memset (&cinfo, 0, sizeof (cinfo));
6629
6630 /* Compute the hash values for all exported symbols. At the same
6631 time store the values in an array so that we could use them for
6632 optimizations. */
6633 amt = dynsymcount * 2 * sizeof (unsigned long int);
6634 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
6635 if (cinfo.hashcodes == NULL)
6636 return FALSE;
6637
6638 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6639 cinfo.min_dynindx = -1;
6640 cinfo.output_bfd = output_bfd;
6641 cinfo.bed = bed;
6642
6643 /* Put all hash values in HASHCODES. */
6644 elf_link_hash_traverse (elf_hash_table (info),
6645 elf_collect_gnu_hash_codes, &cinfo);
6646 if (cinfo.error)
6647 {
6648 free (cinfo.hashcodes);
6649 return FALSE;
6650 }
6651
6652 bucketcount
6653 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6654
6655 if (bucketcount == 0)
6656 {
6657 free (cinfo.hashcodes);
6658 return FALSE;
6659 }
6660
6661 s = bfd_get_linker_section (dynobj, ".gnu.hash");
6662 BFD_ASSERT (s != NULL);
6663
6664 if (cinfo.nsyms == 0)
6665 {
6666 /* Empty .gnu.hash section is special. */
6667 BFD_ASSERT (cinfo.min_dynindx == -1);
6668 free (cinfo.hashcodes);
6669 s->size = 5 * 4 + bed->s->arch_size / 8;
6670 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6671 if (contents == NULL)
6672 return FALSE;
6673 s->contents = contents;
6674 /* 1 empty bucket. */
6675 bfd_put_32 (output_bfd, 1, contents);
6676 /* SYMIDX above the special symbol 0. */
6677 bfd_put_32 (output_bfd, 1, contents + 4);
6678 /* Just one word for bitmask. */
6679 bfd_put_32 (output_bfd, 1, contents + 8);
6680 /* Only hash fn bloom filter. */
6681 bfd_put_32 (output_bfd, 0, contents + 12);
6682 /* No hashes are valid - empty bitmask. */
6683 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6684 /* No hashes in the only bucket. */
6685 bfd_put_32 (output_bfd, 0,
6686 contents + 16 + bed->s->arch_size / 8);
6687 }
6688 else
6689 {
6690 unsigned long int maskwords, maskbitslog2, x;
6691 BFD_ASSERT (cinfo.min_dynindx != -1);
6692
6693 x = cinfo.nsyms;
6694 maskbitslog2 = 1;
6695 while ((x >>= 1) != 0)
6696 ++maskbitslog2;
6697 if (maskbitslog2 < 3)
6698 maskbitslog2 = 5;
6699 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6700 maskbitslog2 = maskbitslog2 + 3;
6701 else
6702 maskbitslog2 = maskbitslog2 + 2;
6703 if (bed->s->arch_size == 64)
6704 {
6705 if (maskbitslog2 == 5)
6706 maskbitslog2 = 6;
6707 cinfo.shift1 = 6;
6708 }
6709 else
6710 cinfo.shift1 = 5;
6711 cinfo.mask = (1 << cinfo.shift1) - 1;
6712 cinfo.shift2 = maskbitslog2;
6713 cinfo.maskbits = 1 << maskbitslog2;
6714 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6715 amt = bucketcount * sizeof (unsigned long int) * 2;
6716 amt += maskwords * sizeof (bfd_vma);
6717 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
6718 if (cinfo.bitmask == NULL)
6719 {
6720 free (cinfo.hashcodes);
6721 return FALSE;
6722 }
6723
6724 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
6725 cinfo.indx = cinfo.counts + bucketcount;
6726 cinfo.symindx = dynsymcount - cinfo.nsyms;
6727 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6728
6729 /* Determine how often each hash bucket is used. */
6730 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6731 for (i = 0; i < cinfo.nsyms; ++i)
6732 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6733
6734 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6735 if (cinfo.counts[i] != 0)
6736 {
6737 cinfo.indx[i] = cnt;
6738 cnt += cinfo.counts[i];
6739 }
6740 BFD_ASSERT (cnt == dynsymcount);
6741 cinfo.bucketcount = bucketcount;
6742 cinfo.local_indx = cinfo.min_dynindx;
6743
6744 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6745 s->size += cinfo.maskbits / 8;
6746 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6747 if (contents == NULL)
6748 {
6749 free (cinfo.bitmask);
6750 free (cinfo.hashcodes);
6751 return FALSE;
6752 }
6753
6754 s->contents = contents;
6755 bfd_put_32 (output_bfd, bucketcount, contents);
6756 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6757 bfd_put_32 (output_bfd, maskwords, contents + 8);
6758 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6759 contents += 16 + cinfo.maskbits / 8;
6760
6761 for (i = 0; i < bucketcount; ++i)
6762 {
6763 if (cinfo.counts[i] == 0)
6764 bfd_put_32 (output_bfd, 0, contents);
6765 else
6766 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6767 contents += 4;
6768 }
6769
6770 cinfo.contents = contents;
6771
6772 /* Renumber dynamic symbols, populate .gnu.hash section. */
6773 elf_link_hash_traverse (elf_hash_table (info),
6774 elf_renumber_gnu_hash_syms, &cinfo);
6775
6776 contents = s->contents + 16;
6777 for (i = 0; i < maskwords; ++i)
6778 {
6779 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6780 contents);
6781 contents += bed->s->arch_size / 8;
6782 }
6783
6784 free (cinfo.bitmask);
6785 free (cinfo.hashcodes);
6786 }
6787 }
6788
6789 s = bfd_get_linker_section (dynobj, ".dynstr");
6790 BFD_ASSERT (s != NULL);
6791
6792 elf_finalize_dynstr (output_bfd, info);
6793
6794 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6795
6796 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6797 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6798 return FALSE;
6799 }
6800
6801 return TRUE;
6802 }
6803 \f
6804 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
6805
6806 static void
6807 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6808 asection *sec)
6809 {
6810 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
6811 sec->sec_info_type = SEC_INFO_TYPE_NONE;
6812 }
6813
6814 /* Finish SHF_MERGE section merging. */
6815
6816 bfd_boolean
6817 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
6818 {
6819 bfd *ibfd;
6820 asection *sec;
6821 const struct elf_backend_data *bed;
6822
6823 if (!is_elf_hash_table (info->hash))
6824 return FALSE;
6825
6826 bed = get_elf_backend_data (obfd);
6827 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6828 if ((ibfd->flags & DYNAMIC) == 0
6829 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6830 && (elf_elfheader (ibfd)->e_ident[EI_CLASS] == bed->s->elfclass)
6831 && (bed->elf_machine_code == elf_elfheader (ibfd)->e_machine
6832 || (bed->elf_machine_alt1 != 0
6833 && (bed->elf_machine_alt1
6834 == elf_elfheader (ibfd)->e_machine))
6835 || (bed->elf_machine_alt2 != 0
6836 && (bed->elf_machine_alt2
6837 == elf_elfheader (ibfd)->e_machine))))
6838 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6839 if ((sec->flags & SEC_MERGE) != 0
6840 && !bfd_is_abs_section (sec->output_section))
6841 {
6842 struct bfd_elf_section_data *secdata;
6843
6844 secdata = elf_section_data (sec);
6845 if (! _bfd_add_merge_section (obfd,
6846 &elf_hash_table (info)->merge_info,
6847 sec, &secdata->sec_info))
6848 return FALSE;
6849 else if (secdata->sec_info)
6850 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
6851 }
6852
6853 if (elf_hash_table (info)->merge_info != NULL)
6854 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
6855 merge_sections_remove_hook);
6856 return TRUE;
6857 }
6858
6859 /* Create an entry in an ELF linker hash table. */
6860
6861 struct bfd_hash_entry *
6862 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6863 struct bfd_hash_table *table,
6864 const char *string)
6865 {
6866 /* Allocate the structure if it has not already been allocated by a
6867 subclass. */
6868 if (entry == NULL)
6869 {
6870 entry = (struct bfd_hash_entry *)
6871 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
6872 if (entry == NULL)
6873 return entry;
6874 }
6875
6876 /* Call the allocation method of the superclass. */
6877 entry = _bfd_link_hash_newfunc (entry, table, string);
6878 if (entry != NULL)
6879 {
6880 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6881 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6882
6883 /* Set local fields. */
6884 ret->indx = -1;
6885 ret->dynindx = -1;
6886 ret->got = htab->init_got_refcount;
6887 ret->plt = htab->init_plt_refcount;
6888 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6889 - offsetof (struct elf_link_hash_entry, size)));
6890 /* Assume that we have been called by a non-ELF symbol reader.
6891 This flag is then reset by the code which reads an ELF input
6892 file. This ensures that a symbol created by a non-ELF symbol
6893 reader will have the flag set correctly. */
6894 ret->non_elf = 1;
6895 }
6896
6897 return entry;
6898 }
6899
6900 /* Copy data from an indirect symbol to its direct symbol, hiding the
6901 old indirect symbol. Also used for copying flags to a weakdef. */
6902
6903 void
6904 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
6905 struct elf_link_hash_entry *dir,
6906 struct elf_link_hash_entry *ind)
6907 {
6908 struct elf_link_hash_table *htab;
6909
6910 /* Copy down any references that we may have already seen to the
6911 symbol which just became indirect if DIR isn't a hidden versioned
6912 symbol. */
6913
6914 if (dir->versioned != versioned_hidden)
6915 {
6916 dir->ref_dynamic |= ind->ref_dynamic;
6917 dir->ref_regular |= ind->ref_regular;
6918 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
6919 dir->non_got_ref |= ind->non_got_ref;
6920 dir->needs_plt |= ind->needs_plt;
6921 dir->pointer_equality_needed |= ind->pointer_equality_needed;
6922 }
6923
6924 if (ind->root.type != bfd_link_hash_indirect)
6925 return;
6926
6927 /* Copy over the global and procedure linkage table refcount entries.
6928 These may have been already set up by a check_relocs routine. */
6929 htab = elf_hash_table (info);
6930 if (ind->got.refcount > htab->init_got_refcount.refcount)
6931 {
6932 if (dir->got.refcount < 0)
6933 dir->got.refcount = 0;
6934 dir->got.refcount += ind->got.refcount;
6935 ind->got.refcount = htab->init_got_refcount.refcount;
6936 }
6937
6938 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
6939 {
6940 if (dir->plt.refcount < 0)
6941 dir->plt.refcount = 0;
6942 dir->plt.refcount += ind->plt.refcount;
6943 ind->plt.refcount = htab->init_plt_refcount.refcount;
6944 }
6945
6946 if (ind->dynindx != -1)
6947 {
6948 if (dir->dynindx != -1)
6949 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
6950 dir->dynindx = ind->dynindx;
6951 dir->dynstr_index = ind->dynstr_index;
6952 ind->dynindx = -1;
6953 ind->dynstr_index = 0;
6954 }
6955 }
6956
6957 void
6958 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
6959 struct elf_link_hash_entry *h,
6960 bfd_boolean force_local)
6961 {
6962 /* STT_GNU_IFUNC symbol must go through PLT. */
6963 if (h->type != STT_GNU_IFUNC)
6964 {
6965 h->plt = elf_hash_table (info)->init_plt_offset;
6966 h->needs_plt = 0;
6967 }
6968 if (force_local)
6969 {
6970 h->forced_local = 1;
6971 if (h->dynindx != -1)
6972 {
6973 h->dynindx = -1;
6974 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
6975 h->dynstr_index);
6976 }
6977 }
6978 }
6979
6980 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
6981 caller. */
6982
6983 bfd_boolean
6984 _bfd_elf_link_hash_table_init
6985 (struct elf_link_hash_table *table,
6986 bfd *abfd,
6987 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
6988 struct bfd_hash_table *,
6989 const char *),
6990 unsigned int entsize,
6991 enum elf_target_id target_id)
6992 {
6993 bfd_boolean ret;
6994 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
6995
6996 table->init_got_refcount.refcount = can_refcount - 1;
6997 table->init_plt_refcount.refcount = can_refcount - 1;
6998 table->init_got_offset.offset = -(bfd_vma) 1;
6999 table->init_plt_offset.offset = -(bfd_vma) 1;
7000 /* The first dynamic symbol is a dummy. */
7001 table->dynsymcount = 1;
7002
7003 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7004
7005 table->root.type = bfd_link_elf_hash_table;
7006 table->hash_table_id = target_id;
7007
7008 return ret;
7009 }
7010
7011 /* Create an ELF linker hash table. */
7012
7013 struct bfd_link_hash_table *
7014 _bfd_elf_link_hash_table_create (bfd *abfd)
7015 {
7016 struct elf_link_hash_table *ret;
7017 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7018
7019 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7020 if (ret == NULL)
7021 return NULL;
7022
7023 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7024 sizeof (struct elf_link_hash_entry),
7025 GENERIC_ELF_DATA))
7026 {
7027 free (ret);
7028 return NULL;
7029 }
7030 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7031
7032 return &ret->root;
7033 }
7034
7035 /* Destroy an ELF linker hash table. */
7036
7037 void
7038 _bfd_elf_link_hash_table_free (bfd *obfd)
7039 {
7040 struct elf_link_hash_table *htab;
7041
7042 htab = (struct elf_link_hash_table *) obfd->link.hash;
7043 if (htab->dynstr != NULL)
7044 _bfd_elf_strtab_free (htab->dynstr);
7045 _bfd_merge_sections_free (htab->merge_info);
7046 _bfd_generic_link_hash_table_free (obfd);
7047 }
7048
7049 /* This is a hook for the ELF emulation code in the generic linker to
7050 tell the backend linker what file name to use for the DT_NEEDED
7051 entry for a dynamic object. */
7052
7053 void
7054 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7055 {
7056 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7057 && bfd_get_format (abfd) == bfd_object)
7058 elf_dt_name (abfd) = name;
7059 }
7060
7061 int
7062 bfd_elf_get_dyn_lib_class (bfd *abfd)
7063 {
7064 int lib_class;
7065 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7066 && bfd_get_format (abfd) == bfd_object)
7067 lib_class = elf_dyn_lib_class (abfd);
7068 else
7069 lib_class = 0;
7070 return lib_class;
7071 }
7072
7073 void
7074 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7075 {
7076 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7077 && bfd_get_format (abfd) == bfd_object)
7078 elf_dyn_lib_class (abfd) = lib_class;
7079 }
7080
7081 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7082 the linker ELF emulation code. */
7083
7084 struct bfd_link_needed_list *
7085 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7086 struct bfd_link_info *info)
7087 {
7088 if (! is_elf_hash_table (info->hash))
7089 return NULL;
7090 return elf_hash_table (info)->needed;
7091 }
7092
7093 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7094 hook for the linker ELF emulation code. */
7095
7096 struct bfd_link_needed_list *
7097 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7098 struct bfd_link_info *info)
7099 {
7100 if (! is_elf_hash_table (info->hash))
7101 return NULL;
7102 return elf_hash_table (info)->runpath;
7103 }
7104
7105 /* Get the name actually used for a dynamic object for a link. This
7106 is the SONAME entry if there is one. Otherwise, it is the string
7107 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7108
7109 const char *
7110 bfd_elf_get_dt_soname (bfd *abfd)
7111 {
7112 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7113 && bfd_get_format (abfd) == bfd_object)
7114 return elf_dt_name (abfd);
7115 return NULL;
7116 }
7117
7118 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7119 the ELF linker emulation code. */
7120
7121 bfd_boolean
7122 bfd_elf_get_bfd_needed_list (bfd *abfd,
7123 struct bfd_link_needed_list **pneeded)
7124 {
7125 asection *s;
7126 bfd_byte *dynbuf = NULL;
7127 unsigned int elfsec;
7128 unsigned long shlink;
7129 bfd_byte *extdyn, *extdynend;
7130 size_t extdynsize;
7131 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7132
7133 *pneeded = NULL;
7134
7135 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7136 || bfd_get_format (abfd) != bfd_object)
7137 return TRUE;
7138
7139 s = bfd_get_section_by_name (abfd, ".dynamic");
7140 if (s == NULL || s->size == 0)
7141 return TRUE;
7142
7143 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7144 goto error_return;
7145
7146 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7147 if (elfsec == SHN_BAD)
7148 goto error_return;
7149
7150 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7151
7152 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7153 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7154
7155 extdyn = dynbuf;
7156 extdynend = extdyn + s->size;
7157 for (; extdyn < extdynend; extdyn += extdynsize)
7158 {
7159 Elf_Internal_Dyn dyn;
7160
7161 (*swap_dyn_in) (abfd, extdyn, &dyn);
7162
7163 if (dyn.d_tag == DT_NULL)
7164 break;
7165
7166 if (dyn.d_tag == DT_NEEDED)
7167 {
7168 const char *string;
7169 struct bfd_link_needed_list *l;
7170 unsigned int tagv = dyn.d_un.d_val;
7171 bfd_size_type amt;
7172
7173 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7174 if (string == NULL)
7175 goto error_return;
7176
7177 amt = sizeof *l;
7178 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7179 if (l == NULL)
7180 goto error_return;
7181
7182 l->by = abfd;
7183 l->name = string;
7184 l->next = *pneeded;
7185 *pneeded = l;
7186 }
7187 }
7188
7189 free (dynbuf);
7190
7191 return TRUE;
7192
7193 error_return:
7194 if (dynbuf != NULL)
7195 free (dynbuf);
7196 return FALSE;
7197 }
7198
7199 struct elf_symbuf_symbol
7200 {
7201 unsigned long st_name; /* Symbol name, index in string tbl */
7202 unsigned char st_info; /* Type and binding attributes */
7203 unsigned char st_other; /* Visibilty, and target specific */
7204 };
7205
7206 struct elf_symbuf_head
7207 {
7208 struct elf_symbuf_symbol *ssym;
7209 bfd_size_type count;
7210 unsigned int st_shndx;
7211 };
7212
7213 struct elf_symbol
7214 {
7215 union
7216 {
7217 Elf_Internal_Sym *isym;
7218 struct elf_symbuf_symbol *ssym;
7219 } u;
7220 const char *name;
7221 };
7222
7223 /* Sort references to symbols by ascending section number. */
7224
7225 static int
7226 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7227 {
7228 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7229 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7230
7231 return s1->st_shndx - s2->st_shndx;
7232 }
7233
7234 static int
7235 elf_sym_name_compare (const void *arg1, const void *arg2)
7236 {
7237 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7238 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7239 return strcmp (s1->name, s2->name);
7240 }
7241
7242 static struct elf_symbuf_head *
7243 elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
7244 {
7245 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7246 struct elf_symbuf_symbol *ssym;
7247 struct elf_symbuf_head *ssymbuf, *ssymhead;
7248 bfd_size_type i, shndx_count, total_size;
7249
7250 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7251 if (indbuf == NULL)
7252 return NULL;
7253
7254 for (ind = indbuf, i = 0; i < symcount; i++)
7255 if (isymbuf[i].st_shndx != SHN_UNDEF)
7256 *ind++ = &isymbuf[i];
7257 indbufend = ind;
7258
7259 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7260 elf_sort_elf_symbol);
7261
7262 shndx_count = 0;
7263 if (indbufend > indbuf)
7264 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7265 if (ind[0]->st_shndx != ind[1]->st_shndx)
7266 shndx_count++;
7267
7268 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7269 + (indbufend - indbuf) * sizeof (*ssym));
7270 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7271 if (ssymbuf == NULL)
7272 {
7273 free (indbuf);
7274 return NULL;
7275 }
7276
7277 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7278 ssymbuf->ssym = NULL;
7279 ssymbuf->count = shndx_count;
7280 ssymbuf->st_shndx = 0;
7281 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7282 {
7283 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7284 {
7285 ssymhead++;
7286 ssymhead->ssym = ssym;
7287 ssymhead->count = 0;
7288 ssymhead->st_shndx = (*ind)->st_shndx;
7289 }
7290 ssym->st_name = (*ind)->st_name;
7291 ssym->st_info = (*ind)->st_info;
7292 ssym->st_other = (*ind)->st_other;
7293 ssymhead->count++;
7294 }
7295 BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
7296 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7297 == total_size));
7298
7299 free (indbuf);
7300 return ssymbuf;
7301 }
7302
7303 /* Check if 2 sections define the same set of local and global
7304 symbols. */
7305
7306 static bfd_boolean
7307 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7308 struct bfd_link_info *info)
7309 {
7310 bfd *bfd1, *bfd2;
7311 const struct elf_backend_data *bed1, *bed2;
7312 Elf_Internal_Shdr *hdr1, *hdr2;
7313 bfd_size_type symcount1, symcount2;
7314 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7315 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7316 Elf_Internal_Sym *isym, *isymend;
7317 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7318 bfd_size_type count1, count2, i;
7319 unsigned int shndx1, shndx2;
7320 bfd_boolean result;
7321
7322 bfd1 = sec1->owner;
7323 bfd2 = sec2->owner;
7324
7325 /* Both sections have to be in ELF. */
7326 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7327 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7328 return FALSE;
7329
7330 if (elf_section_type (sec1) != elf_section_type (sec2))
7331 return FALSE;
7332
7333 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7334 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7335 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7336 return FALSE;
7337
7338 bed1 = get_elf_backend_data (bfd1);
7339 bed2 = get_elf_backend_data (bfd2);
7340 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7341 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7342 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7343 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7344
7345 if (symcount1 == 0 || symcount2 == 0)
7346 return FALSE;
7347
7348 result = FALSE;
7349 isymbuf1 = NULL;
7350 isymbuf2 = NULL;
7351 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7352 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7353
7354 if (ssymbuf1 == NULL)
7355 {
7356 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7357 NULL, NULL, NULL);
7358 if (isymbuf1 == NULL)
7359 goto done;
7360
7361 if (!info->reduce_memory_overheads)
7362 elf_tdata (bfd1)->symbuf = ssymbuf1
7363 = elf_create_symbuf (symcount1, isymbuf1);
7364 }
7365
7366 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7367 {
7368 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7369 NULL, NULL, NULL);
7370 if (isymbuf2 == NULL)
7371 goto done;
7372
7373 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7374 elf_tdata (bfd2)->symbuf = ssymbuf2
7375 = elf_create_symbuf (symcount2, isymbuf2);
7376 }
7377
7378 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7379 {
7380 /* Optimized faster version. */
7381 bfd_size_type lo, hi, mid;
7382 struct elf_symbol *symp;
7383 struct elf_symbuf_symbol *ssym, *ssymend;
7384
7385 lo = 0;
7386 hi = ssymbuf1->count;
7387 ssymbuf1++;
7388 count1 = 0;
7389 while (lo < hi)
7390 {
7391 mid = (lo + hi) / 2;
7392 if (shndx1 < ssymbuf1[mid].st_shndx)
7393 hi = mid;
7394 else if (shndx1 > ssymbuf1[mid].st_shndx)
7395 lo = mid + 1;
7396 else
7397 {
7398 count1 = ssymbuf1[mid].count;
7399 ssymbuf1 += mid;
7400 break;
7401 }
7402 }
7403
7404 lo = 0;
7405 hi = ssymbuf2->count;
7406 ssymbuf2++;
7407 count2 = 0;
7408 while (lo < hi)
7409 {
7410 mid = (lo + hi) / 2;
7411 if (shndx2 < ssymbuf2[mid].st_shndx)
7412 hi = mid;
7413 else if (shndx2 > ssymbuf2[mid].st_shndx)
7414 lo = mid + 1;
7415 else
7416 {
7417 count2 = ssymbuf2[mid].count;
7418 ssymbuf2 += mid;
7419 break;
7420 }
7421 }
7422
7423 if (count1 == 0 || count2 == 0 || count1 != count2)
7424 goto done;
7425
7426 symtable1
7427 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7428 symtable2
7429 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
7430 if (symtable1 == NULL || symtable2 == NULL)
7431 goto done;
7432
7433 symp = symtable1;
7434 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7435 ssym < ssymend; ssym++, symp++)
7436 {
7437 symp->u.ssym = ssym;
7438 symp->name = bfd_elf_string_from_elf_section (bfd1,
7439 hdr1->sh_link,
7440 ssym->st_name);
7441 }
7442
7443 symp = symtable2;
7444 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7445 ssym < ssymend; ssym++, symp++)
7446 {
7447 symp->u.ssym = ssym;
7448 symp->name = bfd_elf_string_from_elf_section (bfd2,
7449 hdr2->sh_link,
7450 ssym->st_name);
7451 }
7452
7453 /* Sort symbol by name. */
7454 qsort (symtable1, count1, sizeof (struct elf_symbol),
7455 elf_sym_name_compare);
7456 qsort (symtable2, count1, sizeof (struct elf_symbol),
7457 elf_sym_name_compare);
7458
7459 for (i = 0; i < count1; i++)
7460 /* Two symbols must have the same binding, type and name. */
7461 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7462 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7463 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7464 goto done;
7465
7466 result = TRUE;
7467 goto done;
7468 }
7469
7470 symtable1 = (struct elf_symbol *)
7471 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7472 symtable2 = (struct elf_symbol *)
7473 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7474 if (symtable1 == NULL || symtable2 == NULL)
7475 goto done;
7476
7477 /* Count definitions in the section. */
7478 count1 = 0;
7479 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7480 if (isym->st_shndx == shndx1)
7481 symtable1[count1++].u.isym = isym;
7482
7483 count2 = 0;
7484 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7485 if (isym->st_shndx == shndx2)
7486 symtable2[count2++].u.isym = isym;
7487
7488 if (count1 == 0 || count2 == 0 || count1 != count2)
7489 goto done;
7490
7491 for (i = 0; i < count1; i++)
7492 symtable1[i].name
7493 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7494 symtable1[i].u.isym->st_name);
7495
7496 for (i = 0; i < count2; i++)
7497 symtable2[i].name
7498 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7499 symtable2[i].u.isym->st_name);
7500
7501 /* Sort symbol by name. */
7502 qsort (symtable1, count1, sizeof (struct elf_symbol),
7503 elf_sym_name_compare);
7504 qsort (symtable2, count1, sizeof (struct elf_symbol),
7505 elf_sym_name_compare);
7506
7507 for (i = 0; i < count1; i++)
7508 /* Two symbols must have the same binding, type and name. */
7509 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7510 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7511 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7512 goto done;
7513
7514 result = TRUE;
7515
7516 done:
7517 if (symtable1)
7518 free (symtable1);
7519 if (symtable2)
7520 free (symtable2);
7521 if (isymbuf1)
7522 free (isymbuf1);
7523 if (isymbuf2)
7524 free (isymbuf2);
7525
7526 return result;
7527 }
7528
7529 /* Return TRUE if 2 section types are compatible. */
7530
7531 bfd_boolean
7532 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7533 bfd *bbfd, const asection *bsec)
7534 {
7535 if (asec == NULL
7536 || bsec == NULL
7537 || abfd->xvec->flavour != bfd_target_elf_flavour
7538 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7539 return TRUE;
7540
7541 return elf_section_type (asec) == elf_section_type (bsec);
7542 }
7543 \f
7544 /* Final phase of ELF linker. */
7545
7546 /* A structure we use to avoid passing large numbers of arguments. */
7547
7548 struct elf_final_link_info
7549 {
7550 /* General link information. */
7551 struct bfd_link_info *info;
7552 /* Output BFD. */
7553 bfd *output_bfd;
7554 /* Symbol string table. */
7555 struct elf_strtab_hash *symstrtab;
7556 /* .hash section. */
7557 asection *hash_sec;
7558 /* symbol version section (.gnu.version). */
7559 asection *symver_sec;
7560 /* Buffer large enough to hold contents of any section. */
7561 bfd_byte *contents;
7562 /* Buffer large enough to hold external relocs of any section. */
7563 void *external_relocs;
7564 /* Buffer large enough to hold internal relocs of any section. */
7565 Elf_Internal_Rela *internal_relocs;
7566 /* Buffer large enough to hold external local symbols of any input
7567 BFD. */
7568 bfd_byte *external_syms;
7569 /* And a buffer for symbol section indices. */
7570 Elf_External_Sym_Shndx *locsym_shndx;
7571 /* Buffer large enough to hold internal local symbols of any input
7572 BFD. */
7573 Elf_Internal_Sym *internal_syms;
7574 /* Array large enough to hold a symbol index for each local symbol
7575 of any input BFD. */
7576 long *indices;
7577 /* Array large enough to hold a section pointer for each local
7578 symbol of any input BFD. */
7579 asection **sections;
7580 /* Buffer for SHT_SYMTAB_SHNDX section. */
7581 Elf_External_Sym_Shndx *symshndxbuf;
7582 /* Number of STT_FILE syms seen. */
7583 size_t filesym_count;
7584 };
7585
7586 /* This struct is used to pass information to elf_link_output_extsym. */
7587
7588 struct elf_outext_info
7589 {
7590 bfd_boolean failed;
7591 bfd_boolean localsyms;
7592 bfd_boolean file_sym_done;
7593 struct elf_final_link_info *flinfo;
7594 };
7595
7596
7597 /* Support for evaluating a complex relocation.
7598
7599 Complex relocations are generalized, self-describing relocations. The
7600 implementation of them consists of two parts: complex symbols, and the
7601 relocations themselves.
7602
7603 The relocations are use a reserved elf-wide relocation type code (R_RELC
7604 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7605 information (start bit, end bit, word width, etc) into the addend. This
7606 information is extracted from CGEN-generated operand tables within gas.
7607
7608 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7609 internal) representing prefix-notation expressions, including but not
7610 limited to those sorts of expressions normally encoded as addends in the
7611 addend field. The symbol mangling format is:
7612
7613 <node> := <literal>
7614 | <unary-operator> ':' <node>
7615 | <binary-operator> ':' <node> ':' <node>
7616 ;
7617
7618 <literal> := 's' <digits=N> ':' <N character symbol name>
7619 | 'S' <digits=N> ':' <N character section name>
7620 | '#' <hexdigits>
7621 ;
7622
7623 <binary-operator> := as in C
7624 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7625
7626 static void
7627 set_symbol_value (bfd *bfd_with_globals,
7628 Elf_Internal_Sym *isymbuf,
7629 size_t locsymcount,
7630 size_t symidx,
7631 bfd_vma val)
7632 {
7633 struct elf_link_hash_entry **sym_hashes;
7634 struct elf_link_hash_entry *h;
7635 size_t extsymoff = locsymcount;
7636
7637 if (symidx < locsymcount)
7638 {
7639 Elf_Internal_Sym *sym;
7640
7641 sym = isymbuf + symidx;
7642 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7643 {
7644 /* It is a local symbol: move it to the
7645 "absolute" section and give it a value. */
7646 sym->st_shndx = SHN_ABS;
7647 sym->st_value = val;
7648 return;
7649 }
7650 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7651 extsymoff = 0;
7652 }
7653
7654 /* It is a global symbol: set its link type
7655 to "defined" and give it a value. */
7656
7657 sym_hashes = elf_sym_hashes (bfd_with_globals);
7658 h = sym_hashes [symidx - extsymoff];
7659 while (h->root.type == bfd_link_hash_indirect
7660 || h->root.type == bfd_link_hash_warning)
7661 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7662 h->root.type = bfd_link_hash_defined;
7663 h->root.u.def.value = val;
7664 h->root.u.def.section = bfd_abs_section_ptr;
7665 }
7666
7667 static bfd_boolean
7668 resolve_symbol (const char *name,
7669 bfd *input_bfd,
7670 struct elf_final_link_info *flinfo,
7671 bfd_vma *result,
7672 Elf_Internal_Sym *isymbuf,
7673 size_t locsymcount)
7674 {
7675 Elf_Internal_Sym *sym;
7676 struct bfd_link_hash_entry *global_entry;
7677 const char *candidate = NULL;
7678 Elf_Internal_Shdr *symtab_hdr;
7679 size_t i;
7680
7681 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7682
7683 for (i = 0; i < locsymcount; ++ i)
7684 {
7685 sym = isymbuf + i;
7686
7687 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7688 continue;
7689
7690 candidate = bfd_elf_string_from_elf_section (input_bfd,
7691 symtab_hdr->sh_link,
7692 sym->st_name);
7693 #ifdef DEBUG
7694 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7695 name, candidate, (unsigned long) sym->st_value);
7696 #endif
7697 if (candidate && strcmp (candidate, name) == 0)
7698 {
7699 asection *sec = flinfo->sections [i];
7700
7701 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7702 *result += sec->output_offset + sec->output_section->vma;
7703 #ifdef DEBUG
7704 printf ("Found symbol with value %8.8lx\n",
7705 (unsigned long) *result);
7706 #endif
7707 return TRUE;
7708 }
7709 }
7710
7711 /* Hmm, haven't found it yet. perhaps it is a global. */
7712 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
7713 FALSE, FALSE, TRUE);
7714 if (!global_entry)
7715 return FALSE;
7716
7717 if (global_entry->type == bfd_link_hash_defined
7718 || global_entry->type == bfd_link_hash_defweak)
7719 {
7720 *result = (global_entry->u.def.value
7721 + global_entry->u.def.section->output_section->vma
7722 + global_entry->u.def.section->output_offset);
7723 #ifdef DEBUG
7724 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7725 global_entry->root.string, (unsigned long) *result);
7726 #endif
7727 return TRUE;
7728 }
7729
7730 return FALSE;
7731 }
7732
7733 static bfd_boolean
7734 resolve_section (const char *name,
7735 asection *sections,
7736 bfd_vma *result)
7737 {
7738 asection *curr;
7739 unsigned int len;
7740
7741 for (curr = sections; curr; curr = curr->next)
7742 if (strcmp (curr->name, name) == 0)
7743 {
7744 *result = curr->vma;
7745 return TRUE;
7746 }
7747
7748 /* Hmm. still haven't found it. try pseudo-section names. */
7749 for (curr = sections; curr; curr = curr->next)
7750 {
7751 len = strlen (curr->name);
7752 if (len > strlen (name))
7753 continue;
7754
7755 if (strncmp (curr->name, name, len) == 0)
7756 {
7757 if (strncmp (".end", name + len, 4) == 0)
7758 {
7759 *result = curr->vma + curr->size;
7760 return TRUE;
7761 }
7762
7763 /* Insert more pseudo-section names here, if you like. */
7764 }
7765 }
7766
7767 return FALSE;
7768 }
7769
7770 static void
7771 undefined_reference (const char *reftype, const char *name)
7772 {
7773 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7774 reftype, name);
7775 }
7776
7777 static bfd_boolean
7778 eval_symbol (bfd_vma *result,
7779 const char **symp,
7780 bfd *input_bfd,
7781 struct elf_final_link_info *flinfo,
7782 bfd_vma dot,
7783 Elf_Internal_Sym *isymbuf,
7784 size_t locsymcount,
7785 int signed_p)
7786 {
7787 size_t len;
7788 size_t symlen;
7789 bfd_vma a;
7790 bfd_vma b;
7791 char symbuf[4096];
7792 const char *sym = *symp;
7793 const char *symend;
7794 bfd_boolean symbol_is_section = FALSE;
7795
7796 len = strlen (sym);
7797 symend = sym + len;
7798
7799 if (len < 1 || len > sizeof (symbuf))
7800 {
7801 bfd_set_error (bfd_error_invalid_operation);
7802 return FALSE;
7803 }
7804
7805 switch (* sym)
7806 {
7807 case '.':
7808 *result = dot;
7809 *symp = sym + 1;
7810 return TRUE;
7811
7812 case '#':
7813 ++sym;
7814 *result = strtoul (sym, (char **) symp, 16);
7815 return TRUE;
7816
7817 case 'S':
7818 symbol_is_section = TRUE;
7819 case 's':
7820 ++sym;
7821 symlen = strtol (sym, (char **) symp, 10);
7822 sym = *symp + 1; /* Skip the trailing ':'. */
7823
7824 if (symend < sym || symlen + 1 > sizeof (symbuf))
7825 {
7826 bfd_set_error (bfd_error_invalid_operation);
7827 return FALSE;
7828 }
7829
7830 memcpy (symbuf, sym, symlen);
7831 symbuf[symlen] = '\0';
7832 *symp = sym + symlen;
7833
7834 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7835 the symbol as a section, or vice-versa. so we're pretty liberal in our
7836 interpretation here; section means "try section first", not "must be a
7837 section", and likewise with symbol. */
7838
7839 if (symbol_is_section)
7840 {
7841 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result)
7842 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
7843 isymbuf, locsymcount))
7844 {
7845 undefined_reference ("section", symbuf);
7846 return FALSE;
7847 }
7848 }
7849 else
7850 {
7851 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
7852 isymbuf, locsymcount)
7853 && !resolve_section (symbuf, flinfo->output_bfd->sections,
7854 result))
7855 {
7856 undefined_reference ("symbol", symbuf);
7857 return FALSE;
7858 }
7859 }
7860
7861 return TRUE;
7862
7863 /* All that remains are operators. */
7864
7865 #define UNARY_OP(op) \
7866 if (strncmp (sym, #op, strlen (#op)) == 0) \
7867 { \
7868 sym += strlen (#op); \
7869 if (*sym == ':') \
7870 ++sym; \
7871 *symp = sym; \
7872 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
7873 isymbuf, locsymcount, signed_p)) \
7874 return FALSE; \
7875 if (signed_p) \
7876 *result = op ((bfd_signed_vma) a); \
7877 else \
7878 *result = op a; \
7879 return TRUE; \
7880 }
7881
7882 #define BINARY_OP(op) \
7883 if (strncmp (sym, #op, strlen (#op)) == 0) \
7884 { \
7885 sym += strlen (#op); \
7886 if (*sym == ':') \
7887 ++sym; \
7888 *symp = sym; \
7889 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
7890 isymbuf, locsymcount, signed_p)) \
7891 return FALSE; \
7892 ++*symp; \
7893 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
7894 isymbuf, locsymcount, signed_p)) \
7895 return FALSE; \
7896 if (signed_p) \
7897 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
7898 else \
7899 *result = a op b; \
7900 return TRUE; \
7901 }
7902
7903 default:
7904 UNARY_OP (0-);
7905 BINARY_OP (<<);
7906 BINARY_OP (>>);
7907 BINARY_OP (==);
7908 BINARY_OP (!=);
7909 BINARY_OP (<=);
7910 BINARY_OP (>=);
7911 BINARY_OP (&&);
7912 BINARY_OP (||);
7913 UNARY_OP (~);
7914 UNARY_OP (!);
7915 BINARY_OP (*);
7916 BINARY_OP (/);
7917 BINARY_OP (%);
7918 BINARY_OP (^);
7919 BINARY_OP (|);
7920 BINARY_OP (&);
7921 BINARY_OP (+);
7922 BINARY_OP (-);
7923 BINARY_OP (<);
7924 BINARY_OP (>);
7925 #undef UNARY_OP
7926 #undef BINARY_OP
7927 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
7928 bfd_set_error (bfd_error_invalid_operation);
7929 return FALSE;
7930 }
7931 }
7932
7933 static void
7934 put_value (bfd_vma size,
7935 unsigned long chunksz,
7936 bfd *input_bfd,
7937 bfd_vma x,
7938 bfd_byte *location)
7939 {
7940 location += (size - chunksz);
7941
7942 for (; size; size -= chunksz, location -= chunksz)
7943 {
7944 switch (chunksz)
7945 {
7946 case 1:
7947 bfd_put_8 (input_bfd, x, location);
7948 x >>= 8;
7949 break;
7950 case 2:
7951 bfd_put_16 (input_bfd, x, location);
7952 x >>= 16;
7953 break;
7954 case 4:
7955 bfd_put_32 (input_bfd, x, location);
7956 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
7957 x >>= 16;
7958 x >>= 16;
7959 break;
7960 #ifdef BFD64
7961 case 8:
7962 bfd_put_64 (input_bfd, x, location);
7963 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
7964 x >>= 32;
7965 x >>= 32;
7966 break;
7967 #endif
7968 default:
7969 abort ();
7970 break;
7971 }
7972 }
7973 }
7974
7975 static bfd_vma
7976 get_value (bfd_vma size,
7977 unsigned long chunksz,
7978 bfd *input_bfd,
7979 bfd_byte *location)
7980 {
7981 int shift;
7982 bfd_vma x = 0;
7983
7984 /* Sanity checks. */
7985 BFD_ASSERT (chunksz <= sizeof (x)
7986 && size >= chunksz
7987 && chunksz != 0
7988 && (size % chunksz) == 0
7989 && input_bfd != NULL
7990 && location != NULL);
7991
7992 if (chunksz == sizeof (x))
7993 {
7994 BFD_ASSERT (size == chunksz);
7995
7996 /* Make sure that we do not perform an undefined shift operation.
7997 We know that size == chunksz so there will only be one iteration
7998 of the loop below. */
7999 shift = 0;
8000 }
8001 else
8002 shift = 8 * chunksz;
8003
8004 for (; size; size -= chunksz, location += chunksz)
8005 {
8006 switch (chunksz)
8007 {
8008 case 1:
8009 x = (x << shift) | bfd_get_8 (input_bfd, location);
8010 break;
8011 case 2:
8012 x = (x << shift) | bfd_get_16 (input_bfd, location);
8013 break;
8014 case 4:
8015 x = (x << shift) | bfd_get_32 (input_bfd, location);
8016 break;
8017 #ifdef BFD64
8018 case 8:
8019 x = (x << shift) | bfd_get_64 (input_bfd, location);
8020 break;
8021 #endif
8022 default:
8023 abort ();
8024 }
8025 }
8026 return x;
8027 }
8028
8029 static void
8030 decode_complex_addend (unsigned long *start, /* in bits */
8031 unsigned long *oplen, /* in bits */
8032 unsigned long *len, /* in bits */
8033 unsigned long *wordsz, /* in bytes */
8034 unsigned long *chunksz, /* in bytes */
8035 unsigned long *lsb0_p,
8036 unsigned long *signed_p,
8037 unsigned long *trunc_p,
8038 unsigned long encoded)
8039 {
8040 * start = encoded & 0x3F;
8041 * len = (encoded >> 6) & 0x3F;
8042 * oplen = (encoded >> 12) & 0x3F;
8043 * wordsz = (encoded >> 18) & 0xF;
8044 * chunksz = (encoded >> 22) & 0xF;
8045 * lsb0_p = (encoded >> 27) & 1;
8046 * signed_p = (encoded >> 28) & 1;
8047 * trunc_p = (encoded >> 29) & 1;
8048 }
8049
8050 bfd_reloc_status_type
8051 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8052 asection *input_section ATTRIBUTE_UNUSED,
8053 bfd_byte *contents,
8054 Elf_Internal_Rela *rel,
8055 bfd_vma relocation)
8056 {
8057 bfd_vma shift, x, mask;
8058 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8059 bfd_reloc_status_type r;
8060
8061 /* Perform this reloc, since it is complex.
8062 (this is not to say that it necessarily refers to a complex
8063 symbol; merely that it is a self-describing CGEN based reloc.
8064 i.e. the addend has the complete reloc information (bit start, end,
8065 word size, etc) encoded within it.). */
8066
8067 decode_complex_addend (&start, &oplen, &len, &wordsz,
8068 &chunksz, &lsb0_p, &signed_p,
8069 &trunc_p, rel->r_addend);
8070
8071 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8072
8073 if (lsb0_p)
8074 shift = (start + 1) - len;
8075 else
8076 shift = (8 * wordsz) - (start + len);
8077
8078 /* FIXME: octets_per_byte. */
8079 x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
8080
8081 #ifdef DEBUG
8082 printf ("Doing complex reloc: "
8083 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8084 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8085 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8086 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8087 oplen, (unsigned long) x, (unsigned long) mask,
8088 (unsigned long) relocation);
8089 #endif
8090
8091 r = bfd_reloc_ok;
8092 if (! trunc_p)
8093 /* Now do an overflow check. */
8094 r = bfd_check_overflow ((signed_p
8095 ? complain_overflow_signed
8096 : complain_overflow_unsigned),
8097 len, 0, (8 * wordsz),
8098 relocation);
8099
8100 /* Do the deed. */
8101 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8102
8103 #ifdef DEBUG
8104 printf (" relocation: %8.8lx\n"
8105 " shifted mask: %8.8lx\n"
8106 " shifted/masked reloc: %8.8lx\n"
8107 " result: %8.8lx\n",
8108 (unsigned long) relocation, (unsigned long) (mask << shift),
8109 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8110 #endif
8111 /* FIXME: octets_per_byte. */
8112 put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
8113 return r;
8114 }
8115
8116 /* Functions to read r_offset from external (target order) reloc
8117 entry. Faster than bfd_getl32 et al, because we let the compiler
8118 know the value is aligned. */
8119
8120 static bfd_vma
8121 ext32l_r_offset (const void *p)
8122 {
8123 union aligned32
8124 {
8125 uint32_t v;
8126 unsigned char c[4];
8127 };
8128 const union aligned32 *a
8129 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8130
8131 uint32_t aval = ( (uint32_t) a->c[0]
8132 | (uint32_t) a->c[1] << 8
8133 | (uint32_t) a->c[2] << 16
8134 | (uint32_t) a->c[3] << 24);
8135 return aval;
8136 }
8137
8138 static bfd_vma
8139 ext32b_r_offset (const void *p)
8140 {
8141 union aligned32
8142 {
8143 uint32_t v;
8144 unsigned char c[4];
8145 };
8146 const union aligned32 *a
8147 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8148
8149 uint32_t aval = ( (uint32_t) a->c[0] << 24
8150 | (uint32_t) a->c[1] << 16
8151 | (uint32_t) a->c[2] << 8
8152 | (uint32_t) a->c[3]);
8153 return aval;
8154 }
8155
8156 #ifdef BFD_HOST_64_BIT
8157 static bfd_vma
8158 ext64l_r_offset (const void *p)
8159 {
8160 union aligned64
8161 {
8162 uint64_t v;
8163 unsigned char c[8];
8164 };
8165 const union aligned64 *a
8166 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8167
8168 uint64_t aval = ( (uint64_t) a->c[0]
8169 | (uint64_t) a->c[1] << 8
8170 | (uint64_t) a->c[2] << 16
8171 | (uint64_t) a->c[3] << 24
8172 | (uint64_t) a->c[4] << 32
8173 | (uint64_t) a->c[5] << 40
8174 | (uint64_t) a->c[6] << 48
8175 | (uint64_t) a->c[7] << 56);
8176 return aval;
8177 }
8178
8179 static bfd_vma
8180 ext64b_r_offset (const void *p)
8181 {
8182 union aligned64
8183 {
8184 uint64_t v;
8185 unsigned char c[8];
8186 };
8187 const union aligned64 *a
8188 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8189
8190 uint64_t aval = ( (uint64_t) a->c[0] << 56
8191 | (uint64_t) a->c[1] << 48
8192 | (uint64_t) a->c[2] << 40
8193 | (uint64_t) a->c[3] << 32
8194 | (uint64_t) a->c[4] << 24
8195 | (uint64_t) a->c[5] << 16
8196 | (uint64_t) a->c[6] << 8
8197 | (uint64_t) a->c[7]);
8198 return aval;
8199 }
8200 #endif
8201
8202 /* When performing a relocatable link, the input relocations are
8203 preserved. But, if they reference global symbols, the indices
8204 referenced must be updated. Update all the relocations found in
8205 RELDATA. */
8206
8207 static bfd_boolean
8208 elf_link_adjust_relocs (bfd *abfd,
8209 struct bfd_elf_section_reloc_data *reldata,
8210 bfd_boolean sort)
8211 {
8212 unsigned int i;
8213 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8214 bfd_byte *erela;
8215 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8216 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8217 bfd_vma r_type_mask;
8218 int r_sym_shift;
8219 unsigned int count = reldata->count;
8220 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8221
8222 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8223 {
8224 swap_in = bed->s->swap_reloc_in;
8225 swap_out = bed->s->swap_reloc_out;
8226 }
8227 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8228 {
8229 swap_in = bed->s->swap_reloca_in;
8230 swap_out = bed->s->swap_reloca_out;
8231 }
8232 else
8233 abort ();
8234
8235 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8236 abort ();
8237
8238 if (bed->s->arch_size == 32)
8239 {
8240 r_type_mask = 0xff;
8241 r_sym_shift = 8;
8242 }
8243 else
8244 {
8245 r_type_mask = 0xffffffff;
8246 r_sym_shift = 32;
8247 }
8248
8249 erela = reldata->hdr->contents;
8250 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8251 {
8252 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8253 unsigned int j;
8254
8255 if (*rel_hash == NULL)
8256 continue;
8257
8258 BFD_ASSERT ((*rel_hash)->indx >= 0);
8259
8260 (*swap_in) (abfd, erela, irela);
8261 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8262 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8263 | (irela[j].r_info & r_type_mask));
8264 (*swap_out) (abfd, irela, erela);
8265 }
8266
8267 if (sort && count != 0)
8268 {
8269 bfd_vma (*ext_r_off) (const void *);
8270 bfd_vma r_off;
8271 size_t elt_size;
8272 bfd_byte *base, *end, *p, *loc;
8273 bfd_byte *buf = NULL;
8274
8275 if (bed->s->arch_size == 32)
8276 {
8277 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8278 ext_r_off = ext32l_r_offset;
8279 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8280 ext_r_off = ext32b_r_offset;
8281 else
8282 abort ();
8283 }
8284 else
8285 {
8286 #ifdef BFD_HOST_64_BIT
8287 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8288 ext_r_off = ext64l_r_offset;
8289 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8290 ext_r_off = ext64b_r_offset;
8291 else
8292 #endif
8293 abort ();
8294 }
8295
8296 /* Must use a stable sort here. A modified insertion sort,
8297 since the relocs are mostly sorted already. */
8298 elt_size = reldata->hdr->sh_entsize;
8299 base = reldata->hdr->contents;
8300 end = base + count * elt_size;
8301 if (elt_size > sizeof (Elf64_External_Rela))
8302 abort ();
8303
8304 /* Ensure the first element is lowest. This acts as a sentinel,
8305 speeding the main loop below. */
8306 r_off = (*ext_r_off) (base);
8307 for (p = loc = base; (p += elt_size) < end; )
8308 {
8309 bfd_vma r_off2 = (*ext_r_off) (p);
8310 if (r_off > r_off2)
8311 {
8312 r_off = r_off2;
8313 loc = p;
8314 }
8315 }
8316 if (loc != base)
8317 {
8318 /* Don't just swap *base and *loc as that changes the order
8319 of the original base[0] and base[1] if they happen to
8320 have the same r_offset. */
8321 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8322 memcpy (onebuf, loc, elt_size);
8323 memmove (base + elt_size, base, loc - base);
8324 memcpy (base, onebuf, elt_size);
8325 }
8326
8327 for (p = base + elt_size; (p += elt_size) < end; )
8328 {
8329 /* base to p is sorted, *p is next to insert. */
8330 r_off = (*ext_r_off) (p);
8331 /* Search the sorted region for location to insert. */
8332 loc = p - elt_size;
8333 while (r_off < (*ext_r_off) (loc))
8334 loc -= elt_size;
8335 loc += elt_size;
8336 if (loc != p)
8337 {
8338 /* Chances are there is a run of relocs to insert here,
8339 from one of more input files. Files are not always
8340 linked in order due to the way elf_link_input_bfd is
8341 called. See pr17666. */
8342 size_t sortlen = p - loc;
8343 bfd_vma r_off2 = (*ext_r_off) (loc);
8344 size_t runlen = elt_size;
8345 size_t buf_size = 96 * 1024;
8346 while (p + runlen < end
8347 && (sortlen <= buf_size
8348 || runlen + elt_size <= buf_size)
8349 && r_off2 > (*ext_r_off) (p + runlen))
8350 runlen += elt_size;
8351 if (buf == NULL)
8352 {
8353 buf = bfd_malloc (buf_size);
8354 if (buf == NULL)
8355 return FALSE;
8356 }
8357 if (runlen < sortlen)
8358 {
8359 memcpy (buf, p, runlen);
8360 memmove (loc + runlen, loc, sortlen);
8361 memcpy (loc, buf, runlen);
8362 }
8363 else
8364 {
8365 memcpy (buf, loc, sortlen);
8366 memmove (loc, p, runlen);
8367 memcpy (loc + runlen, buf, sortlen);
8368 }
8369 p += runlen - elt_size;
8370 }
8371 }
8372 /* Hashes are no longer valid. */
8373 free (reldata->hashes);
8374 reldata->hashes = NULL;
8375 free (buf);
8376 }
8377 return TRUE;
8378 }
8379
8380 struct elf_link_sort_rela
8381 {
8382 union {
8383 bfd_vma offset;
8384 bfd_vma sym_mask;
8385 } u;
8386 enum elf_reloc_type_class type;
8387 /* We use this as an array of size int_rels_per_ext_rel. */
8388 Elf_Internal_Rela rela[1];
8389 };
8390
8391 static int
8392 elf_link_sort_cmp1 (const void *A, const void *B)
8393 {
8394 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8395 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8396 int relativea, relativeb;
8397
8398 relativea = a->type == reloc_class_relative;
8399 relativeb = b->type == reloc_class_relative;
8400
8401 if (relativea < relativeb)
8402 return 1;
8403 if (relativea > relativeb)
8404 return -1;
8405 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8406 return -1;
8407 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8408 return 1;
8409 if (a->rela->r_offset < b->rela->r_offset)
8410 return -1;
8411 if (a->rela->r_offset > b->rela->r_offset)
8412 return 1;
8413 return 0;
8414 }
8415
8416 static int
8417 elf_link_sort_cmp2 (const void *A, const void *B)
8418 {
8419 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8420 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8421
8422 if (a->type < b->type)
8423 return -1;
8424 if (a->type > b->type)
8425 return 1;
8426 if (a->u.offset < b->u.offset)
8427 return -1;
8428 if (a->u.offset > b->u.offset)
8429 return 1;
8430 if (a->rela->r_offset < b->rela->r_offset)
8431 return -1;
8432 if (a->rela->r_offset > b->rela->r_offset)
8433 return 1;
8434 return 0;
8435 }
8436
8437 static size_t
8438 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8439 {
8440 asection *dynamic_relocs;
8441 asection *rela_dyn;
8442 asection *rel_dyn;
8443 bfd_size_type count, size;
8444 size_t i, ret, sort_elt, ext_size;
8445 bfd_byte *sort, *s_non_relative, *p;
8446 struct elf_link_sort_rela *sq;
8447 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8448 int i2e = bed->s->int_rels_per_ext_rel;
8449 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8450 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8451 struct bfd_link_order *lo;
8452 bfd_vma r_sym_mask;
8453 bfd_boolean use_rela;
8454
8455 /* Find a dynamic reloc section. */
8456 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8457 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8458 if (rela_dyn != NULL && rela_dyn->size > 0
8459 && rel_dyn != NULL && rel_dyn->size > 0)
8460 {
8461 bfd_boolean use_rela_initialised = FALSE;
8462
8463 /* This is just here to stop gcc from complaining.
8464 It's initialization checking code is not perfect. */
8465 use_rela = TRUE;
8466
8467 /* Both sections are present. Examine the sizes
8468 of the indirect sections to help us choose. */
8469 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8470 if (lo->type == bfd_indirect_link_order)
8471 {
8472 asection *o = lo->u.indirect.section;
8473
8474 if ((o->size % bed->s->sizeof_rela) == 0)
8475 {
8476 if ((o->size % bed->s->sizeof_rel) == 0)
8477 /* Section size is divisible by both rel and rela sizes.
8478 It is of no help to us. */
8479 ;
8480 else
8481 {
8482 /* Section size is only divisible by rela. */
8483 if (use_rela_initialised && (use_rela == FALSE))
8484 {
8485 _bfd_error_handler
8486 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8487 bfd_set_error (bfd_error_invalid_operation);
8488 return 0;
8489 }
8490 else
8491 {
8492 use_rela = TRUE;
8493 use_rela_initialised = TRUE;
8494 }
8495 }
8496 }
8497 else if ((o->size % bed->s->sizeof_rel) == 0)
8498 {
8499 /* Section size is only divisible by rel. */
8500 if (use_rela_initialised && (use_rela == TRUE))
8501 {
8502 _bfd_error_handler
8503 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8504 bfd_set_error (bfd_error_invalid_operation);
8505 return 0;
8506 }
8507 else
8508 {
8509 use_rela = FALSE;
8510 use_rela_initialised = TRUE;
8511 }
8512 }
8513 else
8514 {
8515 /* The section size is not divisible by either - something is wrong. */
8516 _bfd_error_handler
8517 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8518 bfd_set_error (bfd_error_invalid_operation);
8519 return 0;
8520 }
8521 }
8522
8523 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8524 if (lo->type == bfd_indirect_link_order)
8525 {
8526 asection *o = lo->u.indirect.section;
8527
8528 if ((o->size % bed->s->sizeof_rela) == 0)
8529 {
8530 if ((o->size % bed->s->sizeof_rel) == 0)
8531 /* Section size is divisible by both rel and rela sizes.
8532 It is of no help to us. */
8533 ;
8534 else
8535 {
8536 /* Section size is only divisible by rela. */
8537 if (use_rela_initialised && (use_rela == FALSE))
8538 {
8539 _bfd_error_handler
8540 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8541 bfd_set_error (bfd_error_invalid_operation);
8542 return 0;
8543 }
8544 else
8545 {
8546 use_rela = TRUE;
8547 use_rela_initialised = TRUE;
8548 }
8549 }
8550 }
8551 else if ((o->size % bed->s->sizeof_rel) == 0)
8552 {
8553 /* Section size is only divisible by rel. */
8554 if (use_rela_initialised && (use_rela == TRUE))
8555 {
8556 _bfd_error_handler
8557 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8558 bfd_set_error (bfd_error_invalid_operation);
8559 return 0;
8560 }
8561 else
8562 {
8563 use_rela = FALSE;
8564 use_rela_initialised = TRUE;
8565 }
8566 }
8567 else
8568 {
8569 /* The section size is not divisible by either - something is wrong. */
8570 _bfd_error_handler
8571 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8572 bfd_set_error (bfd_error_invalid_operation);
8573 return 0;
8574 }
8575 }
8576
8577 if (! use_rela_initialised)
8578 /* Make a guess. */
8579 use_rela = TRUE;
8580 }
8581 else if (rela_dyn != NULL && rela_dyn->size > 0)
8582 use_rela = TRUE;
8583 else if (rel_dyn != NULL && rel_dyn->size > 0)
8584 use_rela = FALSE;
8585 else
8586 return 0;
8587
8588 if (use_rela)
8589 {
8590 dynamic_relocs = rela_dyn;
8591 ext_size = bed->s->sizeof_rela;
8592 swap_in = bed->s->swap_reloca_in;
8593 swap_out = bed->s->swap_reloca_out;
8594 }
8595 else
8596 {
8597 dynamic_relocs = rel_dyn;
8598 ext_size = bed->s->sizeof_rel;
8599 swap_in = bed->s->swap_reloc_in;
8600 swap_out = bed->s->swap_reloc_out;
8601 }
8602
8603 size = 0;
8604 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8605 if (lo->type == bfd_indirect_link_order)
8606 size += lo->u.indirect.section->size;
8607
8608 if (size != dynamic_relocs->size)
8609 return 0;
8610
8611 sort_elt = (sizeof (struct elf_link_sort_rela)
8612 + (i2e - 1) * sizeof (Elf_Internal_Rela));
8613
8614 count = dynamic_relocs->size / ext_size;
8615 if (count == 0)
8616 return 0;
8617 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
8618
8619 if (sort == NULL)
8620 {
8621 (*info->callbacks->warning)
8622 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8623 return 0;
8624 }
8625
8626 if (bed->s->arch_size == 32)
8627 r_sym_mask = ~(bfd_vma) 0xff;
8628 else
8629 r_sym_mask = ~(bfd_vma) 0xffffffff;
8630
8631 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8632 if (lo->type == bfd_indirect_link_order)
8633 {
8634 bfd_byte *erel, *erelend;
8635 asection *o = lo->u.indirect.section;
8636
8637 if (o->contents == NULL && o->size != 0)
8638 {
8639 /* This is a reloc section that is being handled as a normal
8640 section. See bfd_section_from_shdr. We can't combine
8641 relocs in this case. */
8642 free (sort);
8643 return 0;
8644 }
8645 erel = o->contents;
8646 erelend = o->contents + o->size;
8647 /* FIXME: octets_per_byte. */
8648 p = sort + o->output_offset / ext_size * sort_elt;
8649
8650 while (erel < erelend)
8651 {
8652 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8653
8654 (*swap_in) (abfd, erel, s->rela);
8655 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
8656 s->u.sym_mask = r_sym_mask;
8657 p += sort_elt;
8658 erel += ext_size;
8659 }
8660 }
8661
8662 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8663
8664 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8665 {
8666 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8667 if (s->type != reloc_class_relative)
8668 break;
8669 }
8670 ret = i;
8671 s_non_relative = p;
8672
8673 sq = (struct elf_link_sort_rela *) s_non_relative;
8674 for (; i < count; i++, p += sort_elt)
8675 {
8676 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8677 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8678 sq = sp;
8679 sp->u.offset = sq->rela->r_offset;
8680 }
8681
8682 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8683
8684 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8685 if (lo->type == bfd_indirect_link_order)
8686 {
8687 bfd_byte *erel, *erelend;
8688 asection *o = lo->u.indirect.section;
8689
8690 erel = o->contents;
8691 erelend = o->contents + o->size;
8692 /* FIXME: octets_per_byte. */
8693 p = sort + o->output_offset / ext_size * sort_elt;
8694 while (erel < erelend)
8695 {
8696 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8697 (*swap_out) (abfd, s->rela, erel);
8698 p += sort_elt;
8699 erel += ext_size;
8700 }
8701 }
8702
8703 free (sort);
8704 *psec = dynamic_relocs;
8705 return ret;
8706 }
8707
8708 /* Add a symbol to the output symbol string table. */
8709
8710 static int
8711 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
8712 const char *name,
8713 Elf_Internal_Sym *elfsym,
8714 asection *input_sec,
8715 struct elf_link_hash_entry *h)
8716 {
8717 int (*output_symbol_hook)
8718 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8719 struct elf_link_hash_entry *);
8720 struct elf_link_hash_table *hash_table;
8721 const struct elf_backend_data *bed;
8722 bfd_size_type strtabsize;
8723
8724 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8725
8726 bed = get_elf_backend_data (flinfo->output_bfd);
8727 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8728 if (output_symbol_hook != NULL)
8729 {
8730 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
8731 if (ret != 1)
8732 return ret;
8733 }
8734
8735 if (name == NULL
8736 || *name == '\0'
8737 || (input_sec->flags & SEC_EXCLUDE))
8738 elfsym->st_name = (unsigned long) -1;
8739 else
8740 {
8741 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8742 to get the final offset for st_name. */
8743 elfsym->st_name
8744 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
8745 name, FALSE);
8746 if (elfsym->st_name == (unsigned long) -1)
8747 return 0;
8748 }
8749
8750 hash_table = elf_hash_table (flinfo->info);
8751 strtabsize = hash_table->strtabsize;
8752 if (strtabsize <= hash_table->strtabcount)
8753 {
8754 strtabsize += strtabsize;
8755 hash_table->strtabsize = strtabsize;
8756 strtabsize *= sizeof (*hash_table->strtab);
8757 hash_table->strtab
8758 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
8759 strtabsize);
8760 if (hash_table->strtab == NULL)
8761 return 0;
8762 }
8763 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
8764 hash_table->strtab[hash_table->strtabcount].dest_index
8765 = hash_table->strtabcount;
8766 hash_table->strtab[hash_table->strtabcount].destshndx_index
8767 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
8768
8769 bfd_get_symcount (flinfo->output_bfd) += 1;
8770 hash_table->strtabcount += 1;
8771
8772 return 1;
8773 }
8774
8775 /* Swap symbols out to the symbol table and flush the output symbols to
8776 the file. */
8777
8778 static bfd_boolean
8779 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
8780 {
8781 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
8782 bfd_size_type amt, i;
8783 const struct elf_backend_data *bed;
8784 bfd_byte *symbuf;
8785 Elf_Internal_Shdr *hdr;
8786 file_ptr pos;
8787 bfd_boolean ret;
8788
8789 if (!hash_table->strtabcount)
8790 return TRUE;
8791
8792 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8793
8794 bed = get_elf_backend_data (flinfo->output_bfd);
8795
8796 amt = bed->s->sizeof_sym * hash_table->strtabcount;
8797 symbuf = (bfd_byte *) bfd_malloc (amt);
8798 if (symbuf == NULL)
8799 return FALSE;
8800
8801 if (flinfo->symshndxbuf)
8802 {
8803 amt = (sizeof (Elf_External_Sym_Shndx)
8804 * (bfd_get_symcount (flinfo->output_bfd)));
8805 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
8806 if (flinfo->symshndxbuf == NULL)
8807 {
8808 free (symbuf);
8809 return FALSE;
8810 }
8811 }
8812
8813 for (i = 0; i < hash_table->strtabcount; i++)
8814 {
8815 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
8816 if (elfsym->sym.st_name == (unsigned long) -1)
8817 elfsym->sym.st_name = 0;
8818 else
8819 elfsym->sym.st_name
8820 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
8821 elfsym->sym.st_name);
8822 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
8823 ((bfd_byte *) symbuf
8824 + (elfsym->dest_index
8825 * bed->s->sizeof_sym)),
8826 (flinfo->symshndxbuf
8827 + elfsym->destshndx_index));
8828 }
8829
8830 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
8831 pos = hdr->sh_offset + hdr->sh_size;
8832 amt = hash_table->strtabcount * bed->s->sizeof_sym;
8833 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
8834 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
8835 {
8836 hdr->sh_size += amt;
8837 ret = TRUE;
8838 }
8839 else
8840 ret = FALSE;
8841
8842 free (symbuf);
8843
8844 free (hash_table->strtab);
8845 hash_table->strtab = NULL;
8846
8847 return ret;
8848 }
8849
8850 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8851
8852 static bfd_boolean
8853 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8854 {
8855 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
8856 && sym->st_shndx < SHN_LORESERVE)
8857 {
8858 /* The gABI doesn't support dynamic symbols in output sections
8859 beyond 64k. */
8860 (*_bfd_error_handler)
8861 (_("%B: Too many sections: %d (>= %d)"),
8862 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
8863 bfd_set_error (bfd_error_nonrepresentable_section);
8864 return FALSE;
8865 }
8866 return TRUE;
8867 }
8868
8869 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8870 allowing an unsatisfied unversioned symbol in the DSO to match a
8871 versioned symbol that would normally require an explicit version.
8872 We also handle the case that a DSO references a hidden symbol
8873 which may be satisfied by a versioned symbol in another DSO. */
8874
8875 static bfd_boolean
8876 elf_link_check_versioned_symbol (struct bfd_link_info *info,
8877 const struct elf_backend_data *bed,
8878 struct elf_link_hash_entry *h)
8879 {
8880 bfd *abfd;
8881 struct elf_link_loaded_list *loaded;
8882
8883 if (!is_elf_hash_table (info->hash))
8884 return FALSE;
8885
8886 /* Check indirect symbol. */
8887 while (h->root.type == bfd_link_hash_indirect)
8888 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8889
8890 switch (h->root.type)
8891 {
8892 default:
8893 abfd = NULL;
8894 break;
8895
8896 case bfd_link_hash_undefined:
8897 case bfd_link_hash_undefweak:
8898 abfd = h->root.u.undef.abfd;
8899 if ((abfd->flags & DYNAMIC) == 0
8900 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
8901 return FALSE;
8902 break;
8903
8904 case bfd_link_hash_defined:
8905 case bfd_link_hash_defweak:
8906 abfd = h->root.u.def.section->owner;
8907 break;
8908
8909 case bfd_link_hash_common:
8910 abfd = h->root.u.c.p->section->owner;
8911 break;
8912 }
8913 BFD_ASSERT (abfd != NULL);
8914
8915 for (loaded = elf_hash_table (info)->loaded;
8916 loaded != NULL;
8917 loaded = loaded->next)
8918 {
8919 bfd *input;
8920 Elf_Internal_Shdr *hdr;
8921 bfd_size_type symcount;
8922 bfd_size_type extsymcount;
8923 bfd_size_type extsymoff;
8924 Elf_Internal_Shdr *versymhdr;
8925 Elf_Internal_Sym *isym;
8926 Elf_Internal_Sym *isymend;
8927 Elf_Internal_Sym *isymbuf;
8928 Elf_External_Versym *ever;
8929 Elf_External_Versym *extversym;
8930
8931 input = loaded->abfd;
8932
8933 /* We check each DSO for a possible hidden versioned definition. */
8934 if (input == abfd
8935 || (input->flags & DYNAMIC) == 0
8936 || elf_dynversym (input) == 0)
8937 continue;
8938
8939 hdr = &elf_tdata (input)->dynsymtab_hdr;
8940
8941 symcount = hdr->sh_size / bed->s->sizeof_sym;
8942 if (elf_bad_symtab (input))
8943 {
8944 extsymcount = symcount;
8945 extsymoff = 0;
8946 }
8947 else
8948 {
8949 extsymcount = symcount - hdr->sh_info;
8950 extsymoff = hdr->sh_info;
8951 }
8952
8953 if (extsymcount == 0)
8954 continue;
8955
8956 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
8957 NULL, NULL, NULL);
8958 if (isymbuf == NULL)
8959 return FALSE;
8960
8961 /* Read in any version definitions. */
8962 versymhdr = &elf_tdata (input)->dynversym_hdr;
8963 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
8964 if (extversym == NULL)
8965 goto error_ret;
8966
8967 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
8968 || (bfd_bread (extversym, versymhdr->sh_size, input)
8969 != versymhdr->sh_size))
8970 {
8971 free (extversym);
8972 error_ret:
8973 free (isymbuf);
8974 return FALSE;
8975 }
8976
8977 ever = extversym + extsymoff;
8978 isymend = isymbuf + extsymcount;
8979 for (isym = isymbuf; isym < isymend; isym++, ever++)
8980 {
8981 const char *name;
8982 Elf_Internal_Versym iver;
8983 unsigned short version_index;
8984
8985 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
8986 || isym->st_shndx == SHN_UNDEF)
8987 continue;
8988
8989 name = bfd_elf_string_from_elf_section (input,
8990 hdr->sh_link,
8991 isym->st_name);
8992 if (strcmp (name, h->root.root.string) != 0)
8993 continue;
8994
8995 _bfd_elf_swap_versym_in (input, ever, &iver);
8996
8997 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
8998 && !(h->def_regular
8999 && h->forced_local))
9000 {
9001 /* If we have a non-hidden versioned sym, then it should
9002 have provided a definition for the undefined sym unless
9003 it is defined in a non-shared object and forced local.
9004 */
9005 abort ();
9006 }
9007
9008 version_index = iver.vs_vers & VERSYM_VERSION;
9009 if (version_index == 1 || version_index == 2)
9010 {
9011 /* This is the base or first version. We can use it. */
9012 free (extversym);
9013 free (isymbuf);
9014 return TRUE;
9015 }
9016 }
9017
9018 free (extversym);
9019 free (isymbuf);
9020 }
9021
9022 return FALSE;
9023 }
9024
9025 /* Add an external symbol to the symbol table. This is called from
9026 the hash table traversal routine. When generating a shared object,
9027 we go through the symbol table twice. The first time we output
9028 anything that might have been forced to local scope in a version
9029 script. The second time we output the symbols that are still
9030 global symbols. */
9031
9032 static bfd_boolean
9033 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9034 {
9035 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9036 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9037 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9038 bfd_boolean strip;
9039 Elf_Internal_Sym sym;
9040 asection *input_sec;
9041 const struct elf_backend_data *bed;
9042 long indx;
9043 int ret;
9044 /* A symbol is bound locally if it is forced local or it is locally
9045 defined, hidden versioned, not referenced by shared library and
9046 not exported when linking executable. */
9047 bfd_boolean local_bind = (h->forced_local
9048 || (bfd_link_executable (flinfo->info)
9049 && !flinfo->info->export_dynamic
9050 && !h->dynamic
9051 && !h->ref_dynamic
9052 && h->def_regular
9053 && h->versioned == versioned_hidden));
9054
9055 if (h->root.type == bfd_link_hash_warning)
9056 {
9057 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9058 if (h->root.type == bfd_link_hash_new)
9059 return TRUE;
9060 }
9061
9062 /* Decide whether to output this symbol in this pass. */
9063 if (eoinfo->localsyms)
9064 {
9065 if (!local_bind)
9066 return TRUE;
9067 }
9068 else
9069 {
9070 if (local_bind)
9071 return TRUE;
9072 }
9073
9074 bed = get_elf_backend_data (flinfo->output_bfd);
9075
9076 if (h->root.type == bfd_link_hash_undefined)
9077 {
9078 /* If we have an undefined symbol reference here then it must have
9079 come from a shared library that is being linked in. (Undefined
9080 references in regular files have already been handled unless
9081 they are in unreferenced sections which are removed by garbage
9082 collection). */
9083 bfd_boolean ignore_undef = FALSE;
9084
9085 /* Some symbols may be special in that the fact that they're
9086 undefined can be safely ignored - let backend determine that. */
9087 if (bed->elf_backend_ignore_undef_symbol)
9088 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9089
9090 /* If we are reporting errors for this situation then do so now. */
9091 if (!ignore_undef
9092 && h->ref_dynamic
9093 && (!h->ref_regular || flinfo->info->gc_sections)
9094 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9095 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9096 {
9097 if (!(flinfo->info->callbacks->undefined_symbol
9098 (flinfo->info, h->root.root.string,
9099 h->ref_regular ? NULL : h->root.u.undef.abfd,
9100 NULL, 0,
9101 (flinfo->info->unresolved_syms_in_shared_libs
9102 == RM_GENERATE_ERROR))))
9103 {
9104 bfd_set_error (bfd_error_bad_value);
9105 eoinfo->failed = TRUE;
9106 return FALSE;
9107 }
9108 }
9109 }
9110
9111 /* We should also warn if a forced local symbol is referenced from
9112 shared libraries. */
9113 if (bfd_link_executable (flinfo->info)
9114 && h->forced_local
9115 && h->ref_dynamic
9116 && h->def_regular
9117 && !h->dynamic_def
9118 && h->ref_dynamic_nonweak
9119 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9120 {
9121 bfd *def_bfd;
9122 const char *msg;
9123 struct elf_link_hash_entry *hi = h;
9124
9125 /* Check indirect symbol. */
9126 while (hi->root.type == bfd_link_hash_indirect)
9127 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9128
9129 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9130 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9131 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9132 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9133 else
9134 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
9135 def_bfd = flinfo->output_bfd;
9136 if (hi->root.u.def.section != bfd_abs_section_ptr)
9137 def_bfd = hi->root.u.def.section->owner;
9138 (*_bfd_error_handler) (msg, flinfo->output_bfd, def_bfd,
9139 h->root.root.string);
9140 bfd_set_error (bfd_error_bad_value);
9141 eoinfo->failed = TRUE;
9142 return FALSE;
9143 }
9144
9145 /* We don't want to output symbols that have never been mentioned by
9146 a regular file, or that we have been told to strip. However, if
9147 h->indx is set to -2, the symbol is used by a reloc and we must
9148 output it. */
9149 strip = FALSE;
9150 if (h->indx == -2)
9151 ;
9152 else if ((h->def_dynamic
9153 || h->ref_dynamic
9154 || h->root.type == bfd_link_hash_new)
9155 && !h->def_regular
9156 && !h->ref_regular)
9157 strip = TRUE;
9158 else if (flinfo->info->strip == strip_all)
9159 strip = TRUE;
9160 else if (flinfo->info->strip == strip_some
9161 && bfd_hash_lookup (flinfo->info->keep_hash,
9162 h->root.root.string, FALSE, FALSE) == NULL)
9163 strip = TRUE;
9164 else if ((h->root.type == bfd_link_hash_defined
9165 || h->root.type == bfd_link_hash_defweak)
9166 && ((flinfo->info->strip_discarded
9167 && discarded_section (h->root.u.def.section))
9168 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9169 && h->root.u.def.section->owner != NULL
9170 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9171 strip = TRUE;
9172 else if ((h->root.type == bfd_link_hash_undefined
9173 || h->root.type == bfd_link_hash_undefweak)
9174 && h->root.u.undef.abfd != NULL
9175 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9176 strip = TRUE;
9177
9178 /* If we're stripping it, and it's not a dynamic symbol, there's
9179 nothing else to do. However, if it is a forced local symbol or
9180 an ifunc symbol we need to give the backend finish_dynamic_symbol
9181 function a chance to make it dynamic. */
9182 if (strip
9183 && h->dynindx == -1
9184 && h->type != STT_GNU_IFUNC
9185 && !h->forced_local)
9186 return TRUE;
9187
9188 sym.st_value = 0;
9189 sym.st_size = h->size;
9190 sym.st_other = h->other;
9191 if (local_bind)
9192 {
9193 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
9194 /* Turn off visibility on local symbol. */
9195 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9196 }
9197 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9198 else if (h->unique_global && h->def_regular)
9199 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, h->type);
9200 else if (h->root.type == bfd_link_hash_undefweak
9201 || h->root.type == bfd_link_hash_defweak)
9202 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
9203 else
9204 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
9205 sym.st_target_internal = h->target_internal;
9206
9207 switch (h->root.type)
9208 {
9209 default:
9210 case bfd_link_hash_new:
9211 case bfd_link_hash_warning:
9212 abort ();
9213 return FALSE;
9214
9215 case bfd_link_hash_undefined:
9216 case bfd_link_hash_undefweak:
9217 input_sec = bfd_und_section_ptr;
9218 sym.st_shndx = SHN_UNDEF;
9219 break;
9220
9221 case bfd_link_hash_defined:
9222 case bfd_link_hash_defweak:
9223 {
9224 input_sec = h->root.u.def.section;
9225 if (input_sec->output_section != NULL)
9226 {
9227 sym.st_shndx =
9228 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9229 input_sec->output_section);
9230 if (sym.st_shndx == SHN_BAD)
9231 {
9232 (*_bfd_error_handler)
9233 (_("%B: could not find output section %A for input section %A"),
9234 flinfo->output_bfd, input_sec->output_section, input_sec);
9235 bfd_set_error (bfd_error_nonrepresentable_section);
9236 eoinfo->failed = TRUE;
9237 return FALSE;
9238 }
9239
9240 /* ELF symbols in relocatable files are section relative,
9241 but in nonrelocatable files they are virtual
9242 addresses. */
9243 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9244 if (!bfd_link_relocatable (flinfo->info))
9245 {
9246 sym.st_value += input_sec->output_section->vma;
9247 if (h->type == STT_TLS)
9248 {
9249 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9250 if (tls_sec != NULL)
9251 sym.st_value -= tls_sec->vma;
9252 }
9253 }
9254 }
9255 else
9256 {
9257 BFD_ASSERT (input_sec->owner == NULL
9258 || (input_sec->owner->flags & DYNAMIC) != 0);
9259 sym.st_shndx = SHN_UNDEF;
9260 input_sec = bfd_und_section_ptr;
9261 }
9262 }
9263 break;
9264
9265 case bfd_link_hash_common:
9266 input_sec = h->root.u.c.p->section;
9267 sym.st_shndx = bed->common_section_index (input_sec);
9268 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9269 break;
9270
9271 case bfd_link_hash_indirect:
9272 /* These symbols are created by symbol versioning. They point
9273 to the decorated version of the name. For example, if the
9274 symbol foo@@GNU_1.2 is the default, which should be used when
9275 foo is used with no version, then we add an indirect symbol
9276 foo which points to foo@@GNU_1.2. We ignore these symbols,
9277 since the indirected symbol is already in the hash table. */
9278 return TRUE;
9279 }
9280
9281 /* Give the processor backend a chance to tweak the symbol value,
9282 and also to finish up anything that needs to be done for this
9283 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9284 forced local syms when non-shared is due to a historical quirk.
9285 STT_GNU_IFUNC symbol must go through PLT. */
9286 if ((h->type == STT_GNU_IFUNC
9287 && h->def_regular
9288 && !bfd_link_relocatable (flinfo->info))
9289 || ((h->dynindx != -1
9290 || h->forced_local)
9291 && ((bfd_link_pic (flinfo->info)
9292 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9293 || h->root.type != bfd_link_hash_undefweak))
9294 || !h->forced_local)
9295 && elf_hash_table (flinfo->info)->dynamic_sections_created))
9296 {
9297 if (! ((*bed->elf_backend_finish_dynamic_symbol)
9298 (flinfo->output_bfd, flinfo->info, h, &sym)))
9299 {
9300 eoinfo->failed = TRUE;
9301 return FALSE;
9302 }
9303 }
9304
9305 /* If we are marking the symbol as undefined, and there are no
9306 non-weak references to this symbol from a regular object, then
9307 mark the symbol as weak undefined; if there are non-weak
9308 references, mark the symbol as strong. We can't do this earlier,
9309 because it might not be marked as undefined until the
9310 finish_dynamic_symbol routine gets through with it. */
9311 if (sym.st_shndx == SHN_UNDEF
9312 && h->ref_regular
9313 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9314 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9315 {
9316 int bindtype;
9317 unsigned int type = ELF_ST_TYPE (sym.st_info);
9318
9319 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9320 if (type == STT_GNU_IFUNC)
9321 type = STT_FUNC;
9322
9323 if (h->ref_regular_nonweak)
9324 bindtype = STB_GLOBAL;
9325 else
9326 bindtype = STB_WEAK;
9327 sym.st_info = ELF_ST_INFO (bindtype, type);
9328 }
9329
9330 /* If this is a symbol defined in a dynamic library, don't use the
9331 symbol size from the dynamic library. Relinking an executable
9332 against a new library may introduce gratuitous changes in the
9333 executable's symbols if we keep the size. */
9334 if (sym.st_shndx == SHN_UNDEF
9335 && !h->def_regular
9336 && h->def_dynamic)
9337 sym.st_size = 0;
9338
9339 /* If a non-weak symbol with non-default visibility is not defined
9340 locally, it is a fatal error. */
9341 if (!bfd_link_relocatable (flinfo->info)
9342 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9343 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9344 && h->root.type == bfd_link_hash_undefined
9345 && !h->def_regular)
9346 {
9347 const char *msg;
9348
9349 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9350 msg = _("%B: protected symbol `%s' isn't defined");
9351 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9352 msg = _("%B: internal symbol `%s' isn't defined");
9353 else
9354 msg = _("%B: hidden symbol `%s' isn't defined");
9355 (*_bfd_error_handler) (msg, flinfo->output_bfd, h->root.root.string);
9356 bfd_set_error (bfd_error_bad_value);
9357 eoinfo->failed = TRUE;
9358 return FALSE;
9359 }
9360
9361 /* If this symbol should be put in the .dynsym section, then put it
9362 there now. We already know the symbol index. We also fill in
9363 the entry in the .hash section. */
9364 if (elf_hash_table (flinfo->info)->dynsym != NULL
9365 && h->dynindx != -1
9366 && elf_hash_table (flinfo->info)->dynamic_sections_created)
9367 {
9368 bfd_byte *esym;
9369
9370 /* Since there is no version information in the dynamic string,
9371 if there is no version info in symbol version section, we will
9372 have a run-time problem if not linking executable, referenced
9373 by shared library, not locally defined, or not bound locally.
9374 */
9375 if (h->verinfo.verdef == NULL
9376 && !local_bind
9377 && (!bfd_link_executable (flinfo->info)
9378 || h->ref_dynamic
9379 || !h->def_regular))
9380 {
9381 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9382
9383 if (p && p [1] != '\0')
9384 {
9385 (*_bfd_error_handler)
9386 (_("%B: No symbol version section for versioned symbol `%s'"),
9387 flinfo->output_bfd, h->root.root.string);
9388 eoinfo->failed = TRUE;
9389 return FALSE;
9390 }
9391 }
9392
9393 sym.st_name = h->dynstr_index;
9394 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9395 + h->dynindx * bed->s->sizeof_sym);
9396 if (!check_dynsym (flinfo->output_bfd, &sym))
9397 {
9398 eoinfo->failed = TRUE;
9399 return FALSE;
9400 }
9401 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
9402
9403 if (flinfo->hash_sec != NULL)
9404 {
9405 size_t hash_entry_size;
9406 bfd_byte *bucketpos;
9407 bfd_vma chain;
9408 size_t bucketcount;
9409 size_t bucket;
9410
9411 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
9412 bucket = h->u.elf_hash_value % bucketcount;
9413
9414 hash_entry_size
9415 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9416 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
9417 + (bucket + 2) * hash_entry_size);
9418 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9419 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9420 bucketpos);
9421 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9422 ((bfd_byte *) flinfo->hash_sec->contents
9423 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9424 }
9425
9426 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
9427 {
9428 Elf_Internal_Versym iversym;
9429 Elf_External_Versym *eversym;
9430
9431 if (!h->def_regular)
9432 {
9433 if (h->verinfo.verdef == NULL
9434 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9435 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
9436 iversym.vs_vers = 0;
9437 else
9438 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9439 }
9440 else
9441 {
9442 if (h->verinfo.vertree == NULL)
9443 iversym.vs_vers = 1;
9444 else
9445 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
9446 if (flinfo->info->create_default_symver)
9447 iversym.vs_vers++;
9448 }
9449
9450 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9451 defined locally. */
9452 if (h->versioned == versioned_hidden && h->def_regular)
9453 iversym.vs_vers |= VERSYM_HIDDEN;
9454
9455 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
9456 eversym += h->dynindx;
9457 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
9458 }
9459 }
9460
9461 /* If the symbol is undefined, and we didn't output it to .dynsym,
9462 strip it from .symtab too. Obviously we can't do this for
9463 relocatable output or when needed for --emit-relocs. */
9464 else if (input_sec == bfd_und_section_ptr
9465 && h->indx != -2
9466 && !bfd_link_relocatable (flinfo->info))
9467 return TRUE;
9468 /* Also strip others that we couldn't earlier due to dynamic symbol
9469 processing. */
9470 if (strip)
9471 return TRUE;
9472 if ((input_sec->flags & SEC_EXCLUDE) != 0)
9473 return TRUE;
9474
9475 /* Output a FILE symbol so that following locals are not associated
9476 with the wrong input file. We need one for forced local symbols
9477 if we've seen more than one FILE symbol or when we have exactly
9478 one FILE symbol but global symbols are present in a file other
9479 than the one with the FILE symbol. We also need one if linker
9480 defined symbols are present. In practice these conditions are
9481 always met, so just emit the FILE symbol unconditionally. */
9482 if (eoinfo->localsyms
9483 && !eoinfo->file_sym_done
9484 && eoinfo->flinfo->filesym_count != 0)
9485 {
9486 Elf_Internal_Sym fsym;
9487
9488 memset (&fsym, 0, sizeof (fsym));
9489 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9490 fsym.st_shndx = SHN_ABS;
9491 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9492 bfd_und_section_ptr, NULL))
9493 return FALSE;
9494
9495 eoinfo->file_sym_done = TRUE;
9496 }
9497
9498 indx = bfd_get_symcount (flinfo->output_bfd);
9499 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9500 input_sec, h);
9501 if (ret == 0)
9502 {
9503 eoinfo->failed = TRUE;
9504 return FALSE;
9505 }
9506 else if (ret == 1)
9507 h->indx = indx;
9508 else if (h->indx == -2)
9509 abort();
9510
9511 return TRUE;
9512 }
9513
9514 /* Return TRUE if special handling is done for relocs in SEC against
9515 symbols defined in discarded sections. */
9516
9517 static bfd_boolean
9518 elf_section_ignore_discarded_relocs (asection *sec)
9519 {
9520 const struct elf_backend_data *bed;
9521
9522 switch (sec->sec_info_type)
9523 {
9524 case SEC_INFO_TYPE_STABS:
9525 case SEC_INFO_TYPE_EH_FRAME:
9526 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
9527 return TRUE;
9528 default:
9529 break;
9530 }
9531
9532 bed = get_elf_backend_data (sec->owner);
9533 if (bed->elf_backend_ignore_discarded_relocs != NULL
9534 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9535 return TRUE;
9536
9537 return FALSE;
9538 }
9539
9540 /* Return a mask saying how ld should treat relocations in SEC against
9541 symbols defined in discarded sections. If this function returns
9542 COMPLAIN set, ld will issue a warning message. If this function
9543 returns PRETEND set, and the discarded section was link-once and the
9544 same size as the kept link-once section, ld will pretend that the
9545 symbol was actually defined in the kept section. Otherwise ld will
9546 zero the reloc (at least that is the intent, but some cooperation by
9547 the target dependent code is needed, particularly for REL targets). */
9548
9549 unsigned int
9550 _bfd_elf_default_action_discarded (asection *sec)
9551 {
9552 if (sec->flags & SEC_DEBUGGING)
9553 return PRETEND;
9554
9555 if (strcmp (".eh_frame", sec->name) == 0)
9556 return 0;
9557
9558 if (strcmp (".gcc_except_table", sec->name) == 0)
9559 return 0;
9560
9561 return COMPLAIN | PRETEND;
9562 }
9563
9564 /* Find a match between a section and a member of a section group. */
9565
9566 static asection *
9567 match_group_member (asection *sec, asection *group,
9568 struct bfd_link_info *info)
9569 {
9570 asection *first = elf_next_in_group (group);
9571 asection *s = first;
9572
9573 while (s != NULL)
9574 {
9575 if (bfd_elf_match_symbols_in_sections (s, sec, info))
9576 return s;
9577
9578 s = elf_next_in_group (s);
9579 if (s == first)
9580 break;
9581 }
9582
9583 return NULL;
9584 }
9585
9586 /* Check if the kept section of a discarded section SEC can be used
9587 to replace it. Return the replacement if it is OK. Otherwise return
9588 NULL. */
9589
9590 asection *
9591 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
9592 {
9593 asection *kept;
9594
9595 kept = sec->kept_section;
9596 if (kept != NULL)
9597 {
9598 if ((kept->flags & SEC_GROUP) != 0)
9599 kept = match_group_member (sec, kept, info);
9600 if (kept != NULL
9601 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9602 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
9603 kept = NULL;
9604 sec->kept_section = kept;
9605 }
9606 return kept;
9607 }
9608
9609 /* Link an input file into the linker output file. This function
9610 handles all the sections and relocations of the input file at once.
9611 This is so that we only have to read the local symbols once, and
9612 don't have to keep them in memory. */
9613
9614 static bfd_boolean
9615 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
9616 {
9617 int (*relocate_section)
9618 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9619 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9620 bfd *output_bfd;
9621 Elf_Internal_Shdr *symtab_hdr;
9622 size_t locsymcount;
9623 size_t extsymoff;
9624 Elf_Internal_Sym *isymbuf;
9625 Elf_Internal_Sym *isym;
9626 Elf_Internal_Sym *isymend;
9627 long *pindex;
9628 asection **ppsection;
9629 asection *o;
9630 const struct elf_backend_data *bed;
9631 struct elf_link_hash_entry **sym_hashes;
9632 bfd_size_type address_size;
9633 bfd_vma r_type_mask;
9634 int r_sym_shift;
9635 bfd_boolean have_file_sym = FALSE;
9636
9637 output_bfd = flinfo->output_bfd;
9638 bed = get_elf_backend_data (output_bfd);
9639 relocate_section = bed->elf_backend_relocate_section;
9640
9641 /* If this is a dynamic object, we don't want to do anything here:
9642 we don't want the local symbols, and we don't want the section
9643 contents. */
9644 if ((input_bfd->flags & DYNAMIC) != 0)
9645 return TRUE;
9646
9647 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9648 if (elf_bad_symtab (input_bfd))
9649 {
9650 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9651 extsymoff = 0;
9652 }
9653 else
9654 {
9655 locsymcount = symtab_hdr->sh_info;
9656 extsymoff = symtab_hdr->sh_info;
9657 }
9658
9659 /* Read the local symbols. */
9660 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9661 if (isymbuf == NULL && locsymcount != 0)
9662 {
9663 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
9664 flinfo->internal_syms,
9665 flinfo->external_syms,
9666 flinfo->locsym_shndx);
9667 if (isymbuf == NULL)
9668 return FALSE;
9669 }
9670
9671 /* Find local symbol sections and adjust values of symbols in
9672 SEC_MERGE sections. Write out those local symbols we know are
9673 going into the output file. */
9674 isymend = isymbuf + locsymcount;
9675 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
9676 isym < isymend;
9677 isym++, pindex++, ppsection++)
9678 {
9679 asection *isec;
9680 const char *name;
9681 Elf_Internal_Sym osym;
9682 long indx;
9683 int ret;
9684
9685 *pindex = -1;
9686
9687 if (elf_bad_symtab (input_bfd))
9688 {
9689 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9690 {
9691 *ppsection = NULL;
9692 continue;
9693 }
9694 }
9695
9696 if (isym->st_shndx == SHN_UNDEF)
9697 isec = bfd_und_section_ptr;
9698 else if (isym->st_shndx == SHN_ABS)
9699 isec = bfd_abs_section_ptr;
9700 else if (isym->st_shndx == SHN_COMMON)
9701 isec = bfd_com_section_ptr;
9702 else
9703 {
9704 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9705 if (isec == NULL)
9706 {
9707 /* Don't attempt to output symbols with st_shnx in the
9708 reserved range other than SHN_ABS and SHN_COMMON. */
9709 *ppsection = NULL;
9710 continue;
9711 }
9712 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
9713 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9714 isym->st_value =
9715 _bfd_merged_section_offset (output_bfd, &isec,
9716 elf_section_data (isec)->sec_info,
9717 isym->st_value);
9718 }
9719
9720 *ppsection = isec;
9721
9722 /* Don't output the first, undefined, symbol. In fact, don't
9723 output any undefined local symbol. */
9724 if (isec == bfd_und_section_ptr)
9725 continue;
9726
9727 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9728 {
9729 /* We never output section symbols. Instead, we use the
9730 section symbol of the corresponding section in the output
9731 file. */
9732 continue;
9733 }
9734
9735 /* If we are stripping all symbols, we don't want to output this
9736 one. */
9737 if (flinfo->info->strip == strip_all)
9738 continue;
9739
9740 /* If we are discarding all local symbols, we don't want to
9741 output this one. If we are generating a relocatable output
9742 file, then some of the local symbols may be required by
9743 relocs; we output them below as we discover that they are
9744 needed. */
9745 if (flinfo->info->discard == discard_all)
9746 continue;
9747
9748 /* If this symbol is defined in a section which we are
9749 discarding, we don't need to keep it. */
9750 if (isym->st_shndx != SHN_UNDEF
9751 && isym->st_shndx < SHN_LORESERVE
9752 && bfd_section_removed_from_list (output_bfd,
9753 isec->output_section))
9754 continue;
9755
9756 /* Get the name of the symbol. */
9757 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9758 isym->st_name);
9759 if (name == NULL)
9760 return FALSE;
9761
9762 /* See if we are discarding symbols with this name. */
9763 if ((flinfo->info->strip == strip_some
9764 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
9765 == NULL))
9766 || (((flinfo->info->discard == discard_sec_merge
9767 && (isec->flags & SEC_MERGE)
9768 && !bfd_link_relocatable (flinfo->info))
9769 || flinfo->info->discard == discard_l)
9770 && bfd_is_local_label_name (input_bfd, name)))
9771 continue;
9772
9773 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
9774 {
9775 if (input_bfd->lto_output)
9776 /* -flto puts a temp file name here. This means builds
9777 are not reproducible. Discard the symbol. */
9778 continue;
9779 have_file_sym = TRUE;
9780 flinfo->filesym_count += 1;
9781 }
9782 if (!have_file_sym)
9783 {
9784 /* In the absence of debug info, bfd_find_nearest_line uses
9785 FILE symbols to determine the source file for local
9786 function symbols. Provide a FILE symbol here if input
9787 files lack such, so that their symbols won't be
9788 associated with a previous input file. It's not the
9789 source file, but the best we can do. */
9790 have_file_sym = TRUE;
9791 flinfo->filesym_count += 1;
9792 memset (&osym, 0, sizeof (osym));
9793 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9794 osym.st_shndx = SHN_ABS;
9795 if (!elf_link_output_symstrtab (flinfo,
9796 (input_bfd->lto_output ? NULL
9797 : input_bfd->filename),
9798 &osym, bfd_abs_section_ptr,
9799 NULL))
9800 return FALSE;
9801 }
9802
9803 osym = *isym;
9804
9805 /* Adjust the section index for the output file. */
9806 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9807 isec->output_section);
9808 if (osym.st_shndx == SHN_BAD)
9809 return FALSE;
9810
9811 /* ELF symbols in relocatable files are section relative, but
9812 in executable files they are virtual addresses. Note that
9813 this code assumes that all ELF sections have an associated
9814 BFD section with a reasonable value for output_offset; below
9815 we assume that they also have a reasonable value for
9816 output_section. Any special sections must be set up to meet
9817 these requirements. */
9818 osym.st_value += isec->output_offset;
9819 if (!bfd_link_relocatable (flinfo->info))
9820 {
9821 osym.st_value += isec->output_section->vma;
9822 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
9823 {
9824 /* STT_TLS symbols are relative to PT_TLS segment base. */
9825 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
9826 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
9827 }
9828 }
9829
9830 indx = bfd_get_symcount (output_bfd);
9831 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
9832 if (ret == 0)
9833 return FALSE;
9834 else if (ret == 1)
9835 *pindex = indx;
9836 }
9837
9838 if (bed->s->arch_size == 32)
9839 {
9840 r_type_mask = 0xff;
9841 r_sym_shift = 8;
9842 address_size = 4;
9843 }
9844 else
9845 {
9846 r_type_mask = 0xffffffff;
9847 r_sym_shift = 32;
9848 address_size = 8;
9849 }
9850
9851 /* Relocate the contents of each section. */
9852 sym_hashes = elf_sym_hashes (input_bfd);
9853 for (o = input_bfd->sections; o != NULL; o = o->next)
9854 {
9855 bfd_byte *contents;
9856
9857 if (! o->linker_mark)
9858 {
9859 /* This section was omitted from the link. */
9860 continue;
9861 }
9862
9863 if (bfd_link_relocatable (flinfo->info)
9864 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
9865 {
9866 /* Deal with the group signature symbol. */
9867 struct bfd_elf_section_data *sec_data = elf_section_data (o);
9868 unsigned long symndx = sec_data->this_hdr.sh_info;
9869 asection *osec = o->output_section;
9870
9871 if (symndx >= locsymcount
9872 || (elf_bad_symtab (input_bfd)
9873 && flinfo->sections[symndx] == NULL))
9874 {
9875 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
9876 while (h->root.type == bfd_link_hash_indirect
9877 || h->root.type == bfd_link_hash_warning)
9878 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9879 /* Arrange for symbol to be output. */
9880 h->indx = -2;
9881 elf_section_data (osec)->this_hdr.sh_info = -2;
9882 }
9883 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
9884 {
9885 /* We'll use the output section target_index. */
9886 asection *sec = flinfo->sections[symndx]->output_section;
9887 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
9888 }
9889 else
9890 {
9891 if (flinfo->indices[symndx] == -1)
9892 {
9893 /* Otherwise output the local symbol now. */
9894 Elf_Internal_Sym sym = isymbuf[symndx];
9895 asection *sec = flinfo->sections[symndx]->output_section;
9896 const char *name;
9897 long indx;
9898 int ret;
9899
9900 name = bfd_elf_string_from_elf_section (input_bfd,
9901 symtab_hdr->sh_link,
9902 sym.st_name);
9903 if (name == NULL)
9904 return FALSE;
9905
9906 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9907 sec);
9908 if (sym.st_shndx == SHN_BAD)
9909 return FALSE;
9910
9911 sym.st_value += o->output_offset;
9912
9913 indx = bfd_get_symcount (output_bfd);
9914 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
9915 NULL);
9916 if (ret == 0)
9917 return FALSE;
9918 else if (ret == 1)
9919 flinfo->indices[symndx] = indx;
9920 else
9921 abort ();
9922 }
9923 elf_section_data (osec)->this_hdr.sh_info
9924 = flinfo->indices[symndx];
9925 }
9926 }
9927
9928 if ((o->flags & SEC_HAS_CONTENTS) == 0
9929 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
9930 continue;
9931
9932 if ((o->flags & SEC_LINKER_CREATED) != 0)
9933 {
9934 /* Section was created by _bfd_elf_link_create_dynamic_sections
9935 or somesuch. */
9936 continue;
9937 }
9938
9939 /* Get the contents of the section. They have been cached by a
9940 relaxation routine. Note that o is a section in an input
9941 file, so the contents field will not have been set by any of
9942 the routines which work on output files. */
9943 if (elf_section_data (o)->this_hdr.contents != NULL)
9944 {
9945 contents = elf_section_data (o)->this_hdr.contents;
9946 if (bed->caches_rawsize
9947 && o->rawsize != 0
9948 && o->rawsize < o->size)
9949 {
9950 memcpy (flinfo->contents, contents, o->rawsize);
9951 contents = flinfo->contents;
9952 }
9953 }
9954 else
9955 {
9956 contents = flinfo->contents;
9957 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
9958 return FALSE;
9959 }
9960
9961 if ((o->flags & SEC_RELOC) != 0)
9962 {
9963 Elf_Internal_Rela *internal_relocs;
9964 Elf_Internal_Rela *rel, *relend;
9965 int action_discarded;
9966 int ret;
9967
9968 /* Get the swapped relocs. */
9969 internal_relocs
9970 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
9971 flinfo->internal_relocs, FALSE);
9972 if (internal_relocs == NULL
9973 && o->reloc_count > 0)
9974 return FALSE;
9975
9976 /* We need to reverse-copy input .ctors/.dtors sections if
9977 they are placed in .init_array/.finit_array for output. */
9978 if (o->size > address_size
9979 && ((strncmp (o->name, ".ctors", 6) == 0
9980 && strcmp (o->output_section->name,
9981 ".init_array") == 0)
9982 || (strncmp (o->name, ".dtors", 6) == 0
9983 && strcmp (o->output_section->name,
9984 ".fini_array") == 0))
9985 && (o->name[6] == 0 || o->name[6] == '.'))
9986 {
9987 if (o->size != o->reloc_count * address_size)
9988 {
9989 (*_bfd_error_handler)
9990 (_("error: %B: size of section %A is not "
9991 "multiple of address size"),
9992 input_bfd, o);
9993 bfd_set_error (bfd_error_on_input);
9994 return FALSE;
9995 }
9996 o->flags |= SEC_ELF_REVERSE_COPY;
9997 }
9998
9999 action_discarded = -1;
10000 if (!elf_section_ignore_discarded_relocs (o))
10001 action_discarded = (*bed->action_discarded) (o);
10002
10003 /* Run through the relocs evaluating complex reloc symbols and
10004 looking for relocs against symbols from discarded sections
10005 or section symbols from removed link-once sections.
10006 Complain about relocs against discarded sections. Zero
10007 relocs against removed link-once sections. */
10008
10009 rel = internal_relocs;
10010 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
10011 for ( ; rel < relend; rel++)
10012 {
10013 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10014 unsigned int s_type;
10015 asection **ps, *sec;
10016 struct elf_link_hash_entry *h = NULL;
10017 const char *sym_name;
10018
10019 if (r_symndx == STN_UNDEF)
10020 continue;
10021
10022 if (r_symndx >= locsymcount
10023 || (elf_bad_symtab (input_bfd)
10024 && flinfo->sections[r_symndx] == NULL))
10025 {
10026 h = sym_hashes[r_symndx - extsymoff];
10027
10028 /* Badly formatted input files can contain relocs that
10029 reference non-existant symbols. Check here so that
10030 we do not seg fault. */
10031 if (h == NULL)
10032 {
10033 char buffer [32];
10034
10035 sprintf_vma (buffer, rel->r_info);
10036 (*_bfd_error_handler)
10037 (_("error: %B contains a reloc (0x%s) for section %A "
10038 "that references a non-existent global symbol"),
10039 input_bfd, o, buffer);
10040 bfd_set_error (bfd_error_bad_value);
10041 return FALSE;
10042 }
10043
10044 while (h->root.type == bfd_link_hash_indirect
10045 || h->root.type == bfd_link_hash_warning)
10046 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10047
10048 s_type = h->type;
10049
10050 /* If a plugin symbol is referenced from a non-IR file,
10051 mark the symbol as undefined. Note that the
10052 linker may attach linker created dynamic sections
10053 to the plugin bfd. Symbols defined in linker
10054 created sections are not plugin symbols. */
10055 if (h->root.non_ir_ref
10056 && (h->root.type == bfd_link_hash_defined
10057 || h->root.type == bfd_link_hash_defweak)
10058 && (h->root.u.def.section->flags
10059 & SEC_LINKER_CREATED) == 0
10060 && h->root.u.def.section->owner != NULL
10061 && (h->root.u.def.section->owner->flags
10062 & BFD_PLUGIN) != 0)
10063 {
10064 h->root.type = bfd_link_hash_undefined;
10065 h->root.u.undef.abfd = h->root.u.def.section->owner;
10066 }
10067
10068 ps = NULL;
10069 if (h->root.type == bfd_link_hash_defined
10070 || h->root.type == bfd_link_hash_defweak)
10071 ps = &h->root.u.def.section;
10072
10073 sym_name = h->root.root.string;
10074 }
10075 else
10076 {
10077 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10078
10079 s_type = ELF_ST_TYPE (sym->st_info);
10080 ps = &flinfo->sections[r_symndx];
10081 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10082 sym, *ps);
10083 }
10084
10085 if ((s_type == STT_RELC || s_type == STT_SRELC)
10086 && !bfd_link_relocatable (flinfo->info))
10087 {
10088 bfd_vma val;
10089 bfd_vma dot = (rel->r_offset
10090 + o->output_offset + o->output_section->vma);
10091 #ifdef DEBUG
10092 printf ("Encountered a complex symbol!");
10093 printf (" (input_bfd %s, section %s, reloc %ld\n",
10094 input_bfd->filename, o->name,
10095 (long) (rel - internal_relocs));
10096 printf (" symbol: idx %8.8lx, name %s\n",
10097 r_symndx, sym_name);
10098 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10099 (unsigned long) rel->r_info,
10100 (unsigned long) rel->r_offset);
10101 #endif
10102 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10103 isymbuf, locsymcount, s_type == STT_SRELC))
10104 return FALSE;
10105
10106 /* Symbol evaluated OK. Update to absolute value. */
10107 set_symbol_value (input_bfd, isymbuf, locsymcount,
10108 r_symndx, val);
10109 continue;
10110 }
10111
10112 if (action_discarded != -1 && ps != NULL)
10113 {
10114 /* Complain if the definition comes from a
10115 discarded section. */
10116 if ((sec = *ps) != NULL && discarded_section (sec))
10117 {
10118 BFD_ASSERT (r_symndx != STN_UNDEF);
10119 if (action_discarded & COMPLAIN)
10120 (*flinfo->info->callbacks->einfo)
10121 (_("%X`%s' referenced in section `%A' of %B: "
10122 "defined in discarded section `%A' of %B\n"),
10123 sym_name, o, input_bfd, sec, sec->owner);
10124
10125 /* Try to do the best we can to support buggy old
10126 versions of gcc. Pretend that the symbol is
10127 really defined in the kept linkonce section.
10128 FIXME: This is quite broken. Modifying the
10129 symbol here means we will be changing all later
10130 uses of the symbol, not just in this section. */
10131 if (action_discarded & PRETEND)
10132 {
10133 asection *kept;
10134
10135 kept = _bfd_elf_check_kept_section (sec,
10136 flinfo->info);
10137 if (kept != NULL)
10138 {
10139 *ps = kept;
10140 continue;
10141 }
10142 }
10143 }
10144 }
10145 }
10146
10147 /* Relocate the section by invoking a back end routine.
10148
10149 The back end routine is responsible for adjusting the
10150 section contents as necessary, and (if using Rela relocs
10151 and generating a relocatable output file) adjusting the
10152 reloc addend as necessary.
10153
10154 The back end routine does not have to worry about setting
10155 the reloc address or the reloc symbol index.
10156
10157 The back end routine is given a pointer to the swapped in
10158 internal symbols, and can access the hash table entries
10159 for the external symbols via elf_sym_hashes (input_bfd).
10160
10161 When generating relocatable output, the back end routine
10162 must handle STB_LOCAL/STT_SECTION symbols specially. The
10163 output symbol is going to be a section symbol
10164 corresponding to the output section, which will require
10165 the addend to be adjusted. */
10166
10167 ret = (*relocate_section) (output_bfd, flinfo->info,
10168 input_bfd, o, contents,
10169 internal_relocs,
10170 isymbuf,
10171 flinfo->sections);
10172 if (!ret)
10173 return FALSE;
10174
10175 if (ret == 2
10176 || bfd_link_relocatable (flinfo->info)
10177 || flinfo->info->emitrelocations)
10178 {
10179 Elf_Internal_Rela *irela;
10180 Elf_Internal_Rela *irelaend, *irelamid;
10181 bfd_vma last_offset;
10182 struct elf_link_hash_entry **rel_hash;
10183 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10184 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10185 unsigned int next_erel;
10186 bfd_boolean rela_normal;
10187 struct bfd_elf_section_data *esdi, *esdo;
10188
10189 esdi = elf_section_data (o);
10190 esdo = elf_section_data (o->output_section);
10191 rela_normal = FALSE;
10192
10193 /* Adjust the reloc addresses and symbol indices. */
10194
10195 irela = internal_relocs;
10196 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
10197 rel_hash = esdo->rel.hashes + esdo->rel.count;
10198 /* We start processing the REL relocs, if any. When we reach
10199 IRELAMID in the loop, we switch to the RELA relocs. */
10200 irelamid = irela;
10201 if (esdi->rel.hdr != NULL)
10202 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10203 * bed->s->int_rels_per_ext_rel);
10204 rel_hash_list = rel_hash;
10205 rela_hash_list = NULL;
10206 last_offset = o->output_offset;
10207 if (!bfd_link_relocatable (flinfo->info))
10208 last_offset += o->output_section->vma;
10209 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10210 {
10211 unsigned long r_symndx;
10212 asection *sec;
10213 Elf_Internal_Sym sym;
10214
10215 if (next_erel == bed->s->int_rels_per_ext_rel)
10216 {
10217 rel_hash++;
10218 next_erel = 0;
10219 }
10220
10221 if (irela == irelamid)
10222 {
10223 rel_hash = esdo->rela.hashes + esdo->rela.count;
10224 rela_hash_list = rel_hash;
10225 rela_normal = bed->rela_normal;
10226 }
10227
10228 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10229 flinfo->info, o,
10230 irela->r_offset);
10231 if (irela->r_offset >= (bfd_vma) -2)
10232 {
10233 /* This is a reloc for a deleted entry or somesuch.
10234 Turn it into an R_*_NONE reloc, at the same
10235 offset as the last reloc. elf_eh_frame.c and
10236 bfd_elf_discard_info rely on reloc offsets
10237 being ordered. */
10238 irela->r_offset = last_offset;
10239 irela->r_info = 0;
10240 irela->r_addend = 0;
10241 continue;
10242 }
10243
10244 irela->r_offset += o->output_offset;
10245
10246 /* Relocs in an executable have to be virtual addresses. */
10247 if (!bfd_link_relocatable (flinfo->info))
10248 irela->r_offset += o->output_section->vma;
10249
10250 last_offset = irela->r_offset;
10251
10252 r_symndx = irela->r_info >> r_sym_shift;
10253 if (r_symndx == STN_UNDEF)
10254 continue;
10255
10256 if (r_symndx >= locsymcount
10257 || (elf_bad_symtab (input_bfd)
10258 && flinfo->sections[r_symndx] == NULL))
10259 {
10260 struct elf_link_hash_entry *rh;
10261 unsigned long indx;
10262
10263 /* This is a reloc against a global symbol. We
10264 have not yet output all the local symbols, so
10265 we do not know the symbol index of any global
10266 symbol. We set the rel_hash entry for this
10267 reloc to point to the global hash table entry
10268 for this symbol. The symbol index is then
10269 set at the end of bfd_elf_final_link. */
10270 indx = r_symndx - extsymoff;
10271 rh = elf_sym_hashes (input_bfd)[indx];
10272 while (rh->root.type == bfd_link_hash_indirect
10273 || rh->root.type == bfd_link_hash_warning)
10274 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10275
10276 /* Setting the index to -2 tells
10277 elf_link_output_extsym that this symbol is
10278 used by a reloc. */
10279 BFD_ASSERT (rh->indx < 0);
10280 rh->indx = -2;
10281
10282 *rel_hash = rh;
10283
10284 continue;
10285 }
10286
10287 /* This is a reloc against a local symbol. */
10288
10289 *rel_hash = NULL;
10290 sym = isymbuf[r_symndx];
10291 sec = flinfo->sections[r_symndx];
10292 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10293 {
10294 /* I suppose the backend ought to fill in the
10295 section of any STT_SECTION symbol against a
10296 processor specific section. */
10297 r_symndx = STN_UNDEF;
10298 if (bfd_is_abs_section (sec))
10299 ;
10300 else if (sec == NULL || sec->owner == NULL)
10301 {
10302 bfd_set_error (bfd_error_bad_value);
10303 return FALSE;
10304 }
10305 else
10306 {
10307 asection *osec = sec->output_section;
10308
10309 /* If we have discarded a section, the output
10310 section will be the absolute section. In
10311 case of discarded SEC_MERGE sections, use
10312 the kept section. relocate_section should
10313 have already handled discarded linkonce
10314 sections. */
10315 if (bfd_is_abs_section (osec)
10316 && sec->kept_section != NULL
10317 && sec->kept_section->output_section != NULL)
10318 {
10319 osec = sec->kept_section->output_section;
10320 irela->r_addend -= osec->vma;
10321 }
10322
10323 if (!bfd_is_abs_section (osec))
10324 {
10325 r_symndx = osec->target_index;
10326 if (r_symndx == STN_UNDEF)
10327 {
10328 irela->r_addend += osec->vma;
10329 osec = _bfd_nearby_section (output_bfd, osec,
10330 osec->vma);
10331 irela->r_addend -= osec->vma;
10332 r_symndx = osec->target_index;
10333 }
10334 }
10335 }
10336
10337 /* Adjust the addend according to where the
10338 section winds up in the output section. */
10339 if (rela_normal)
10340 irela->r_addend += sec->output_offset;
10341 }
10342 else
10343 {
10344 if (flinfo->indices[r_symndx] == -1)
10345 {
10346 unsigned long shlink;
10347 const char *name;
10348 asection *osec;
10349 long indx;
10350
10351 if (flinfo->info->strip == strip_all)
10352 {
10353 /* You can't do ld -r -s. */
10354 bfd_set_error (bfd_error_invalid_operation);
10355 return FALSE;
10356 }
10357
10358 /* This symbol was skipped earlier, but
10359 since it is needed by a reloc, we
10360 must output it now. */
10361 shlink = symtab_hdr->sh_link;
10362 name = (bfd_elf_string_from_elf_section
10363 (input_bfd, shlink, sym.st_name));
10364 if (name == NULL)
10365 return FALSE;
10366
10367 osec = sec->output_section;
10368 sym.st_shndx =
10369 _bfd_elf_section_from_bfd_section (output_bfd,
10370 osec);
10371 if (sym.st_shndx == SHN_BAD)
10372 return FALSE;
10373
10374 sym.st_value += sec->output_offset;
10375 if (!bfd_link_relocatable (flinfo->info))
10376 {
10377 sym.st_value += osec->vma;
10378 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10379 {
10380 /* STT_TLS symbols are relative to PT_TLS
10381 segment base. */
10382 BFD_ASSERT (elf_hash_table (flinfo->info)
10383 ->tls_sec != NULL);
10384 sym.st_value -= (elf_hash_table (flinfo->info)
10385 ->tls_sec->vma);
10386 }
10387 }
10388
10389 indx = bfd_get_symcount (output_bfd);
10390 ret = elf_link_output_symstrtab (flinfo, name,
10391 &sym, sec,
10392 NULL);
10393 if (ret == 0)
10394 return FALSE;
10395 else if (ret == 1)
10396 flinfo->indices[r_symndx] = indx;
10397 else
10398 abort ();
10399 }
10400
10401 r_symndx = flinfo->indices[r_symndx];
10402 }
10403
10404 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10405 | (irela->r_info & r_type_mask));
10406 }
10407
10408 /* Swap out the relocs. */
10409 input_rel_hdr = esdi->rel.hdr;
10410 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
10411 {
10412 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10413 input_rel_hdr,
10414 internal_relocs,
10415 rel_hash_list))
10416 return FALSE;
10417 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10418 * bed->s->int_rels_per_ext_rel);
10419 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
10420 }
10421
10422 input_rela_hdr = esdi->rela.hdr;
10423 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10424 {
10425 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10426 input_rela_hdr,
10427 internal_relocs,
10428 rela_hash_list))
10429 return FALSE;
10430 }
10431 }
10432 }
10433
10434 /* Write out the modified section contents. */
10435 if (bed->elf_backend_write_section
10436 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
10437 contents))
10438 {
10439 /* Section written out. */
10440 }
10441 else switch (o->sec_info_type)
10442 {
10443 case SEC_INFO_TYPE_STABS:
10444 if (! (_bfd_write_section_stabs
10445 (output_bfd,
10446 &elf_hash_table (flinfo->info)->stab_info,
10447 o, &elf_section_data (o)->sec_info, contents)))
10448 return FALSE;
10449 break;
10450 case SEC_INFO_TYPE_MERGE:
10451 if (! _bfd_write_merged_section (output_bfd, o,
10452 elf_section_data (o)->sec_info))
10453 return FALSE;
10454 break;
10455 case SEC_INFO_TYPE_EH_FRAME:
10456 {
10457 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
10458 o, contents))
10459 return FALSE;
10460 }
10461 break;
10462 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10463 {
10464 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10465 flinfo->info,
10466 o, contents))
10467 return FALSE;
10468 }
10469 break;
10470 default:
10471 {
10472 /* FIXME: octets_per_byte. */
10473 if (! (o->flags & SEC_EXCLUDE))
10474 {
10475 file_ptr offset = (file_ptr) o->output_offset;
10476 bfd_size_type todo = o->size;
10477 if ((o->flags & SEC_ELF_REVERSE_COPY))
10478 {
10479 /* Reverse-copy input section to output. */
10480 do
10481 {
10482 todo -= address_size;
10483 if (! bfd_set_section_contents (output_bfd,
10484 o->output_section,
10485 contents + todo,
10486 offset,
10487 address_size))
10488 return FALSE;
10489 if (todo == 0)
10490 break;
10491 offset += address_size;
10492 }
10493 while (1);
10494 }
10495 else if (! bfd_set_section_contents (output_bfd,
10496 o->output_section,
10497 contents,
10498 offset, todo))
10499 return FALSE;
10500 }
10501 }
10502 break;
10503 }
10504 }
10505
10506 return TRUE;
10507 }
10508
10509 /* Generate a reloc when linking an ELF file. This is a reloc
10510 requested by the linker, and does not come from any input file. This
10511 is used to build constructor and destructor tables when linking
10512 with -Ur. */
10513
10514 static bfd_boolean
10515 elf_reloc_link_order (bfd *output_bfd,
10516 struct bfd_link_info *info,
10517 asection *output_section,
10518 struct bfd_link_order *link_order)
10519 {
10520 reloc_howto_type *howto;
10521 long indx;
10522 bfd_vma offset;
10523 bfd_vma addend;
10524 struct bfd_elf_section_reloc_data *reldata;
10525 struct elf_link_hash_entry **rel_hash_ptr;
10526 Elf_Internal_Shdr *rel_hdr;
10527 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10528 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10529 bfd_byte *erel;
10530 unsigned int i;
10531 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
10532
10533 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10534 if (howto == NULL)
10535 {
10536 bfd_set_error (bfd_error_bad_value);
10537 return FALSE;
10538 }
10539
10540 addend = link_order->u.reloc.p->addend;
10541
10542 if (esdo->rel.hdr)
10543 reldata = &esdo->rel;
10544 else if (esdo->rela.hdr)
10545 reldata = &esdo->rela;
10546 else
10547 {
10548 reldata = NULL;
10549 BFD_ASSERT (0);
10550 }
10551
10552 /* Figure out the symbol index. */
10553 rel_hash_ptr = reldata->hashes + reldata->count;
10554 if (link_order->type == bfd_section_reloc_link_order)
10555 {
10556 indx = link_order->u.reloc.p->u.section->target_index;
10557 BFD_ASSERT (indx != 0);
10558 *rel_hash_ptr = NULL;
10559 }
10560 else
10561 {
10562 struct elf_link_hash_entry *h;
10563
10564 /* Treat a reloc against a defined symbol as though it were
10565 actually against the section. */
10566 h = ((struct elf_link_hash_entry *)
10567 bfd_wrapped_link_hash_lookup (output_bfd, info,
10568 link_order->u.reloc.p->u.name,
10569 FALSE, FALSE, TRUE));
10570 if (h != NULL
10571 && (h->root.type == bfd_link_hash_defined
10572 || h->root.type == bfd_link_hash_defweak))
10573 {
10574 asection *section;
10575
10576 section = h->root.u.def.section;
10577 indx = section->output_section->target_index;
10578 *rel_hash_ptr = NULL;
10579 /* It seems that we ought to add the symbol value to the
10580 addend here, but in practice it has already been added
10581 because it was passed to constructor_callback. */
10582 addend += section->output_section->vma + section->output_offset;
10583 }
10584 else if (h != NULL)
10585 {
10586 /* Setting the index to -2 tells elf_link_output_extsym that
10587 this symbol is used by a reloc. */
10588 h->indx = -2;
10589 *rel_hash_ptr = h;
10590 indx = 0;
10591 }
10592 else
10593 {
10594 if (! ((*info->callbacks->unattached_reloc)
10595 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
10596 return FALSE;
10597 indx = 0;
10598 }
10599 }
10600
10601 /* If this is an inplace reloc, we must write the addend into the
10602 object file. */
10603 if (howto->partial_inplace && addend != 0)
10604 {
10605 bfd_size_type size;
10606 bfd_reloc_status_type rstat;
10607 bfd_byte *buf;
10608 bfd_boolean ok;
10609 const char *sym_name;
10610
10611 size = (bfd_size_type) bfd_get_reloc_size (howto);
10612 buf = (bfd_byte *) bfd_zmalloc (size);
10613 if (buf == NULL && size != 0)
10614 return FALSE;
10615 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10616 switch (rstat)
10617 {
10618 case bfd_reloc_ok:
10619 break;
10620
10621 default:
10622 case bfd_reloc_outofrange:
10623 abort ();
10624
10625 case bfd_reloc_overflow:
10626 if (link_order->type == bfd_section_reloc_link_order)
10627 sym_name = bfd_section_name (output_bfd,
10628 link_order->u.reloc.p->u.section);
10629 else
10630 sym_name = link_order->u.reloc.p->u.name;
10631 if (! ((*info->callbacks->reloc_overflow)
10632 (info, NULL, sym_name, howto->name, addend, NULL,
10633 NULL, (bfd_vma) 0)))
10634 {
10635 free (buf);
10636 return FALSE;
10637 }
10638 break;
10639 }
10640 ok = bfd_set_section_contents (output_bfd, output_section, buf,
10641 link_order->offset, size);
10642 free (buf);
10643 if (! ok)
10644 return FALSE;
10645 }
10646
10647 /* The address of a reloc is relative to the section in a
10648 relocatable file, and is a virtual address in an executable
10649 file. */
10650 offset = link_order->offset;
10651 if (! bfd_link_relocatable (info))
10652 offset += output_section->vma;
10653
10654 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10655 {
10656 irel[i].r_offset = offset;
10657 irel[i].r_info = 0;
10658 irel[i].r_addend = 0;
10659 }
10660 if (bed->s->arch_size == 32)
10661 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10662 else
10663 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10664
10665 rel_hdr = reldata->hdr;
10666 erel = rel_hdr->contents;
10667 if (rel_hdr->sh_type == SHT_REL)
10668 {
10669 erel += reldata->count * bed->s->sizeof_rel;
10670 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10671 }
10672 else
10673 {
10674 irel[0].r_addend = addend;
10675 erel += reldata->count * bed->s->sizeof_rela;
10676 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10677 }
10678
10679 ++reldata->count;
10680
10681 return TRUE;
10682 }
10683
10684
10685 /* Get the output vma of the section pointed to by the sh_link field. */
10686
10687 static bfd_vma
10688 elf_get_linked_section_vma (struct bfd_link_order *p)
10689 {
10690 Elf_Internal_Shdr **elf_shdrp;
10691 asection *s;
10692 int elfsec;
10693
10694 s = p->u.indirect.section;
10695 elf_shdrp = elf_elfsections (s->owner);
10696 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10697 elfsec = elf_shdrp[elfsec]->sh_link;
10698 /* PR 290:
10699 The Intel C compiler generates SHT_IA_64_UNWIND with
10700 SHF_LINK_ORDER. But it doesn't set the sh_link or
10701 sh_info fields. Hence we could get the situation
10702 where elfsec is 0. */
10703 if (elfsec == 0)
10704 {
10705 const struct elf_backend_data *bed
10706 = get_elf_backend_data (s->owner);
10707 if (bed->link_order_error_handler)
10708 bed->link_order_error_handler
10709 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
10710 return 0;
10711 }
10712 else
10713 {
10714 s = elf_shdrp[elfsec]->bfd_section;
10715 return s->output_section->vma + s->output_offset;
10716 }
10717 }
10718
10719
10720 /* Compare two sections based on the locations of the sections they are
10721 linked to. Used by elf_fixup_link_order. */
10722
10723 static int
10724 compare_link_order (const void * a, const void * b)
10725 {
10726 bfd_vma apos;
10727 bfd_vma bpos;
10728
10729 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10730 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10731 if (apos < bpos)
10732 return -1;
10733 return apos > bpos;
10734 }
10735
10736
10737 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10738 order as their linked sections. Returns false if this could not be done
10739 because an output section includes both ordered and unordered
10740 sections. Ideally we'd do this in the linker proper. */
10741
10742 static bfd_boolean
10743 elf_fixup_link_order (bfd *abfd, asection *o)
10744 {
10745 int seen_linkorder;
10746 int seen_other;
10747 int n;
10748 struct bfd_link_order *p;
10749 bfd *sub;
10750 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10751 unsigned elfsec;
10752 struct bfd_link_order **sections;
10753 asection *s, *other_sec, *linkorder_sec;
10754 bfd_vma offset;
10755
10756 other_sec = NULL;
10757 linkorder_sec = NULL;
10758 seen_other = 0;
10759 seen_linkorder = 0;
10760 for (p = o->map_head.link_order; p != NULL; p = p->next)
10761 {
10762 if (p->type == bfd_indirect_link_order)
10763 {
10764 s = p->u.indirect.section;
10765 sub = s->owner;
10766 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10767 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
10768 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
10769 && elfsec < elf_numsections (sub)
10770 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
10771 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
10772 {
10773 seen_linkorder++;
10774 linkorder_sec = s;
10775 }
10776 else
10777 {
10778 seen_other++;
10779 other_sec = s;
10780 }
10781 }
10782 else
10783 seen_other++;
10784
10785 if (seen_other && seen_linkorder)
10786 {
10787 if (other_sec && linkorder_sec)
10788 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10789 o, linkorder_sec,
10790 linkorder_sec->owner, other_sec,
10791 other_sec->owner);
10792 else
10793 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
10794 o);
10795 bfd_set_error (bfd_error_bad_value);
10796 return FALSE;
10797 }
10798 }
10799
10800 if (!seen_linkorder)
10801 return TRUE;
10802
10803 sections = (struct bfd_link_order **)
10804 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
10805 if (sections == NULL)
10806 return FALSE;
10807 seen_linkorder = 0;
10808
10809 for (p = o->map_head.link_order; p != NULL; p = p->next)
10810 {
10811 sections[seen_linkorder++] = p;
10812 }
10813 /* Sort the input sections in the order of their linked section. */
10814 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
10815 compare_link_order);
10816
10817 /* Change the offsets of the sections. */
10818 offset = 0;
10819 for (n = 0; n < seen_linkorder; n++)
10820 {
10821 s = sections[n]->u.indirect.section;
10822 offset &= ~(bfd_vma) 0 << s->alignment_power;
10823 s->output_offset = offset;
10824 sections[n]->offset = offset;
10825 /* FIXME: octets_per_byte. */
10826 offset += sections[n]->size;
10827 }
10828
10829 free (sections);
10830 return TRUE;
10831 }
10832
10833 static void
10834 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
10835 {
10836 asection *o;
10837
10838 if (flinfo->symstrtab != NULL)
10839 _bfd_elf_strtab_free (flinfo->symstrtab);
10840 if (flinfo->contents != NULL)
10841 free (flinfo->contents);
10842 if (flinfo->external_relocs != NULL)
10843 free (flinfo->external_relocs);
10844 if (flinfo->internal_relocs != NULL)
10845 free (flinfo->internal_relocs);
10846 if (flinfo->external_syms != NULL)
10847 free (flinfo->external_syms);
10848 if (flinfo->locsym_shndx != NULL)
10849 free (flinfo->locsym_shndx);
10850 if (flinfo->internal_syms != NULL)
10851 free (flinfo->internal_syms);
10852 if (flinfo->indices != NULL)
10853 free (flinfo->indices);
10854 if (flinfo->sections != NULL)
10855 free (flinfo->sections);
10856 if (flinfo->symshndxbuf != NULL)
10857 free (flinfo->symshndxbuf);
10858 for (o = obfd->sections; o != NULL; o = o->next)
10859 {
10860 struct bfd_elf_section_data *esdo = elf_section_data (o);
10861 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
10862 free (esdo->rel.hashes);
10863 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
10864 free (esdo->rela.hashes);
10865 }
10866 }
10867
10868 /* Do the final step of an ELF link. */
10869
10870 bfd_boolean
10871 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
10872 {
10873 bfd_boolean dynamic;
10874 bfd_boolean emit_relocs;
10875 bfd *dynobj;
10876 struct elf_final_link_info flinfo;
10877 asection *o;
10878 struct bfd_link_order *p;
10879 bfd *sub;
10880 bfd_size_type max_contents_size;
10881 bfd_size_type max_external_reloc_size;
10882 bfd_size_type max_internal_reloc_count;
10883 bfd_size_type max_sym_count;
10884 bfd_size_type max_sym_shndx_count;
10885 Elf_Internal_Sym elfsym;
10886 unsigned int i;
10887 Elf_Internal_Shdr *symtab_hdr;
10888 Elf_Internal_Shdr *symtab_shndx_hdr;
10889 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10890 struct elf_outext_info eoinfo;
10891 bfd_boolean merged;
10892 size_t relativecount = 0;
10893 asection *reldyn = 0;
10894 bfd_size_type amt;
10895 asection *attr_section = NULL;
10896 bfd_vma attr_size = 0;
10897 const char *std_attrs_section;
10898
10899 if (! is_elf_hash_table (info->hash))
10900 return FALSE;
10901
10902 if (bfd_link_pic (info))
10903 abfd->flags |= DYNAMIC;
10904
10905 dynamic = elf_hash_table (info)->dynamic_sections_created;
10906 dynobj = elf_hash_table (info)->dynobj;
10907
10908 emit_relocs = (bfd_link_relocatable (info)
10909 || info->emitrelocations);
10910
10911 flinfo.info = info;
10912 flinfo.output_bfd = abfd;
10913 flinfo.symstrtab = _bfd_elf_strtab_init ();
10914 if (flinfo.symstrtab == NULL)
10915 return FALSE;
10916
10917 if (! dynamic)
10918 {
10919 flinfo.hash_sec = NULL;
10920 flinfo.symver_sec = NULL;
10921 }
10922 else
10923 {
10924 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
10925 /* Note that dynsym_sec can be NULL (on VMS). */
10926 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
10927 /* Note that it is OK if symver_sec is NULL. */
10928 }
10929
10930 flinfo.contents = NULL;
10931 flinfo.external_relocs = NULL;
10932 flinfo.internal_relocs = NULL;
10933 flinfo.external_syms = NULL;
10934 flinfo.locsym_shndx = NULL;
10935 flinfo.internal_syms = NULL;
10936 flinfo.indices = NULL;
10937 flinfo.sections = NULL;
10938 flinfo.symshndxbuf = NULL;
10939 flinfo.filesym_count = 0;
10940
10941 /* The object attributes have been merged. Remove the input
10942 sections from the link, and set the contents of the output
10943 secton. */
10944 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
10945 for (o = abfd->sections; o != NULL; o = o->next)
10946 {
10947 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
10948 || strcmp (o->name, ".gnu.attributes") == 0)
10949 {
10950 for (p = o->map_head.link_order; p != NULL; p = p->next)
10951 {
10952 asection *input_section;
10953
10954 if (p->type != bfd_indirect_link_order)
10955 continue;
10956 input_section = p->u.indirect.section;
10957 /* Hack: reset the SEC_HAS_CONTENTS flag so that
10958 elf_link_input_bfd ignores this section. */
10959 input_section->flags &= ~SEC_HAS_CONTENTS;
10960 }
10961
10962 attr_size = bfd_elf_obj_attr_size (abfd);
10963 if (attr_size)
10964 {
10965 bfd_set_section_size (abfd, o, attr_size);
10966 attr_section = o;
10967 /* Skip this section later on. */
10968 o->map_head.link_order = NULL;
10969 }
10970 else
10971 o->flags |= SEC_EXCLUDE;
10972 }
10973 }
10974
10975 /* Count up the number of relocations we will output for each output
10976 section, so that we know the sizes of the reloc sections. We
10977 also figure out some maximum sizes. */
10978 max_contents_size = 0;
10979 max_external_reloc_size = 0;
10980 max_internal_reloc_count = 0;
10981 max_sym_count = 0;
10982 max_sym_shndx_count = 0;
10983 merged = FALSE;
10984 for (o = abfd->sections; o != NULL; o = o->next)
10985 {
10986 struct bfd_elf_section_data *esdo = elf_section_data (o);
10987 o->reloc_count = 0;
10988
10989 for (p = o->map_head.link_order; p != NULL; p = p->next)
10990 {
10991 unsigned int reloc_count = 0;
10992 struct bfd_elf_section_data *esdi = NULL;
10993
10994 if (p->type == bfd_section_reloc_link_order
10995 || p->type == bfd_symbol_reloc_link_order)
10996 reloc_count = 1;
10997 else if (p->type == bfd_indirect_link_order)
10998 {
10999 asection *sec;
11000
11001 sec = p->u.indirect.section;
11002 esdi = elf_section_data (sec);
11003
11004 /* Mark all sections which are to be included in the
11005 link. This will normally be every section. We need
11006 to do this so that we can identify any sections which
11007 the linker has decided to not include. */
11008 sec->linker_mark = TRUE;
11009
11010 if (sec->flags & SEC_MERGE)
11011 merged = TRUE;
11012
11013 if (esdo->this_hdr.sh_type == SHT_REL
11014 || esdo->this_hdr.sh_type == SHT_RELA)
11015 /* Some backends use reloc_count in relocation sections
11016 to count particular types of relocs. Of course,
11017 reloc sections themselves can't have relocations. */
11018 reloc_count = 0;
11019 else if (emit_relocs)
11020 reloc_count = sec->reloc_count;
11021 else if (bed->elf_backend_count_relocs)
11022 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11023
11024 if (sec->rawsize > max_contents_size)
11025 max_contents_size = sec->rawsize;
11026 if (sec->size > max_contents_size)
11027 max_contents_size = sec->size;
11028
11029 /* We are interested in just local symbols, not all
11030 symbols. */
11031 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11032 && (sec->owner->flags & DYNAMIC) == 0)
11033 {
11034 size_t sym_count;
11035
11036 if (elf_bad_symtab (sec->owner))
11037 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11038 / bed->s->sizeof_sym);
11039 else
11040 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11041
11042 if (sym_count > max_sym_count)
11043 max_sym_count = sym_count;
11044
11045 if (sym_count > max_sym_shndx_count
11046 && elf_symtab_shndx_list (sec->owner) != NULL)
11047 max_sym_shndx_count = sym_count;
11048
11049 if ((sec->flags & SEC_RELOC) != 0)
11050 {
11051 size_t ext_size = 0;
11052
11053 if (esdi->rel.hdr != NULL)
11054 ext_size = esdi->rel.hdr->sh_size;
11055 if (esdi->rela.hdr != NULL)
11056 ext_size += esdi->rela.hdr->sh_size;
11057
11058 if (ext_size > max_external_reloc_size)
11059 max_external_reloc_size = ext_size;
11060 if (sec->reloc_count > max_internal_reloc_count)
11061 max_internal_reloc_count = sec->reloc_count;
11062 }
11063 }
11064 }
11065
11066 if (reloc_count == 0)
11067 continue;
11068
11069 o->reloc_count += reloc_count;
11070
11071 if (p->type == bfd_indirect_link_order && emit_relocs)
11072 {
11073 if (esdi->rel.hdr)
11074 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11075 if (esdi->rela.hdr)
11076 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11077 }
11078 else
11079 {
11080 if (o->use_rela_p)
11081 esdo->rela.count += reloc_count;
11082 else
11083 esdo->rel.count += reloc_count;
11084 }
11085 }
11086
11087 if (o->reloc_count > 0)
11088 o->flags |= SEC_RELOC;
11089 else
11090 {
11091 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11092 set it (this is probably a bug) and if it is set
11093 assign_section_numbers will create a reloc section. */
11094 o->flags &=~ SEC_RELOC;
11095 }
11096
11097 /* If the SEC_ALLOC flag is not set, force the section VMA to
11098 zero. This is done in elf_fake_sections as well, but forcing
11099 the VMA to 0 here will ensure that relocs against these
11100 sections are handled correctly. */
11101 if ((o->flags & SEC_ALLOC) == 0
11102 && ! o->user_set_vma)
11103 o->vma = 0;
11104 }
11105
11106 if (! bfd_link_relocatable (info) && merged)
11107 elf_link_hash_traverse (elf_hash_table (info),
11108 _bfd_elf_link_sec_merge_syms, abfd);
11109
11110 /* Figure out the file positions for everything but the symbol table
11111 and the relocs. We set symcount to force assign_section_numbers
11112 to create a symbol table. */
11113 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
11114 BFD_ASSERT (! abfd->output_has_begun);
11115 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11116 goto error_return;
11117
11118 /* Set sizes, and assign file positions for reloc sections. */
11119 for (o = abfd->sections; o != NULL; o = o->next)
11120 {
11121 struct bfd_elf_section_data *esdo = elf_section_data (o);
11122 if ((o->flags & SEC_RELOC) != 0)
11123 {
11124 if (esdo->rel.hdr
11125 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
11126 goto error_return;
11127
11128 if (esdo->rela.hdr
11129 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
11130 goto error_return;
11131 }
11132
11133 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11134 to count upwards while actually outputting the relocations. */
11135 esdo->rel.count = 0;
11136 esdo->rela.count = 0;
11137
11138 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11139 {
11140 /* Cache the section contents so that they can be compressed
11141 later. Use bfd_malloc since it will be freed by
11142 bfd_compress_section_contents. */
11143 unsigned char *contents = esdo->this_hdr.contents;
11144 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11145 abort ();
11146 contents
11147 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11148 if (contents == NULL)
11149 goto error_return;
11150 esdo->this_hdr.contents = contents;
11151 }
11152 }
11153
11154 /* We have now assigned file positions for all the sections except
11155 .symtab, .strtab, and non-loaded reloc sections. We start the
11156 .symtab section at the current file position, and write directly
11157 to it. We build the .strtab section in memory. */
11158 bfd_get_symcount (abfd) = 0;
11159 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11160 /* sh_name is set in prep_headers. */
11161 symtab_hdr->sh_type = SHT_SYMTAB;
11162 /* sh_flags, sh_addr and sh_size all start off zero. */
11163 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11164 /* sh_link is set in assign_section_numbers. */
11165 /* sh_info is set below. */
11166 /* sh_offset is set just below. */
11167 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11168
11169 if (max_sym_count < 20)
11170 max_sym_count = 20;
11171 elf_hash_table (info)->strtabsize = max_sym_count;
11172 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11173 elf_hash_table (info)->strtab
11174 = (struct elf_sym_strtab *) bfd_malloc (amt);
11175 if (elf_hash_table (info)->strtab == NULL)
11176 goto error_return;
11177 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11178 flinfo.symshndxbuf
11179 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11180 ? (Elf_External_Sym_Shndx *) -1 : NULL);
11181
11182 if (info->strip != strip_all || emit_relocs)
11183 {
11184 file_ptr off = elf_next_file_pos (abfd);
11185
11186 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11187
11188 /* Note that at this point elf_next_file_pos (abfd) is
11189 incorrect. We do not yet know the size of the .symtab section.
11190 We correct next_file_pos below, after we do know the size. */
11191
11192 /* Start writing out the symbol table. The first symbol is always a
11193 dummy symbol. */
11194 elfsym.st_value = 0;
11195 elfsym.st_size = 0;
11196 elfsym.st_info = 0;
11197 elfsym.st_other = 0;
11198 elfsym.st_shndx = SHN_UNDEF;
11199 elfsym.st_target_internal = 0;
11200 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11201 bfd_und_section_ptr, NULL) != 1)
11202 goto error_return;
11203
11204 /* Output a symbol for each section. We output these even if we are
11205 discarding local symbols, since they are used for relocs. These
11206 symbols have no names. We store the index of each one in the
11207 index field of the section, so that we can find it again when
11208 outputting relocs. */
11209
11210 elfsym.st_size = 0;
11211 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11212 elfsym.st_other = 0;
11213 elfsym.st_value = 0;
11214 elfsym.st_target_internal = 0;
11215 for (i = 1; i < elf_numsections (abfd); i++)
11216 {
11217 o = bfd_section_from_elf_index (abfd, i);
11218 if (o != NULL)
11219 {
11220 o->target_index = bfd_get_symcount (abfd);
11221 elfsym.st_shndx = i;
11222 if (!bfd_link_relocatable (info))
11223 elfsym.st_value = o->vma;
11224 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11225 NULL) != 1)
11226 goto error_return;
11227 }
11228 }
11229 }
11230
11231 /* Allocate some memory to hold information read in from the input
11232 files. */
11233 if (max_contents_size != 0)
11234 {
11235 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11236 if (flinfo.contents == NULL)
11237 goto error_return;
11238 }
11239
11240 if (max_external_reloc_size != 0)
11241 {
11242 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11243 if (flinfo.external_relocs == NULL)
11244 goto error_return;
11245 }
11246
11247 if (max_internal_reloc_count != 0)
11248 {
11249 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11250 amt *= sizeof (Elf_Internal_Rela);
11251 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11252 if (flinfo.internal_relocs == NULL)
11253 goto error_return;
11254 }
11255
11256 if (max_sym_count != 0)
11257 {
11258 amt = max_sym_count * bed->s->sizeof_sym;
11259 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11260 if (flinfo.external_syms == NULL)
11261 goto error_return;
11262
11263 amt = max_sym_count * sizeof (Elf_Internal_Sym);
11264 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11265 if (flinfo.internal_syms == NULL)
11266 goto error_return;
11267
11268 amt = max_sym_count * sizeof (long);
11269 flinfo.indices = (long int *) bfd_malloc (amt);
11270 if (flinfo.indices == NULL)
11271 goto error_return;
11272
11273 amt = max_sym_count * sizeof (asection *);
11274 flinfo.sections = (asection **) bfd_malloc (amt);
11275 if (flinfo.sections == NULL)
11276 goto error_return;
11277 }
11278
11279 if (max_sym_shndx_count != 0)
11280 {
11281 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
11282 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11283 if (flinfo.locsym_shndx == NULL)
11284 goto error_return;
11285 }
11286
11287 if (elf_hash_table (info)->tls_sec)
11288 {
11289 bfd_vma base, end = 0;
11290 asection *sec;
11291
11292 for (sec = elf_hash_table (info)->tls_sec;
11293 sec && (sec->flags & SEC_THREAD_LOCAL);
11294 sec = sec->next)
11295 {
11296 bfd_size_type size = sec->size;
11297
11298 if (size == 0
11299 && (sec->flags & SEC_HAS_CONTENTS) == 0)
11300 {
11301 struct bfd_link_order *ord = sec->map_tail.link_order;
11302
11303 if (ord != NULL)
11304 size = ord->offset + ord->size;
11305 }
11306 end = sec->vma + size;
11307 }
11308 base = elf_hash_table (info)->tls_sec->vma;
11309 /* Only align end of TLS section if static TLS doesn't have special
11310 alignment requirements. */
11311 if (bed->static_tls_alignment == 1)
11312 end = align_power (end,
11313 elf_hash_table (info)->tls_sec->alignment_power);
11314 elf_hash_table (info)->tls_size = end - base;
11315 }
11316
11317 /* Reorder SHF_LINK_ORDER sections. */
11318 for (o = abfd->sections; o != NULL; o = o->next)
11319 {
11320 if (!elf_fixup_link_order (abfd, o))
11321 return FALSE;
11322 }
11323
11324 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11325 return FALSE;
11326
11327 /* Since ELF permits relocations to be against local symbols, we
11328 must have the local symbols available when we do the relocations.
11329 Since we would rather only read the local symbols once, and we
11330 would rather not keep them in memory, we handle all the
11331 relocations for a single input file at the same time.
11332
11333 Unfortunately, there is no way to know the total number of local
11334 symbols until we have seen all of them, and the local symbol
11335 indices precede the global symbol indices. This means that when
11336 we are generating relocatable output, and we see a reloc against
11337 a global symbol, we can not know the symbol index until we have
11338 finished examining all the local symbols to see which ones we are
11339 going to output. To deal with this, we keep the relocations in
11340 memory, and don't output them until the end of the link. This is
11341 an unfortunate waste of memory, but I don't see a good way around
11342 it. Fortunately, it only happens when performing a relocatable
11343 link, which is not the common case. FIXME: If keep_memory is set
11344 we could write the relocs out and then read them again; I don't
11345 know how bad the memory loss will be. */
11346
11347 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11348 sub->output_has_begun = FALSE;
11349 for (o = abfd->sections; o != NULL; o = o->next)
11350 {
11351 for (p = o->map_head.link_order; p != NULL; p = p->next)
11352 {
11353 if (p->type == bfd_indirect_link_order
11354 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11355 == bfd_target_elf_flavour)
11356 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11357 {
11358 if (! sub->output_has_begun)
11359 {
11360 if (! elf_link_input_bfd (&flinfo, sub))
11361 goto error_return;
11362 sub->output_has_begun = TRUE;
11363 }
11364 }
11365 else if (p->type == bfd_section_reloc_link_order
11366 || p->type == bfd_symbol_reloc_link_order)
11367 {
11368 if (! elf_reloc_link_order (abfd, info, o, p))
11369 goto error_return;
11370 }
11371 else
11372 {
11373 if (! _bfd_default_link_order (abfd, info, o, p))
11374 {
11375 if (p->type == bfd_indirect_link_order
11376 && (bfd_get_flavour (sub)
11377 == bfd_target_elf_flavour)
11378 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11379 != bed->s->elfclass))
11380 {
11381 const char *iclass, *oclass;
11382
11383 if (bed->s->elfclass == ELFCLASS64)
11384 {
11385 iclass = "ELFCLASS32";
11386 oclass = "ELFCLASS64";
11387 }
11388 else
11389 {
11390 iclass = "ELFCLASS64";
11391 oclass = "ELFCLASS32";
11392 }
11393
11394 bfd_set_error (bfd_error_wrong_format);
11395 (*_bfd_error_handler)
11396 (_("%B: file class %s incompatible with %s"),
11397 sub, iclass, oclass);
11398 }
11399
11400 goto error_return;
11401 }
11402 }
11403 }
11404 }
11405
11406 /* Free symbol buffer if needed. */
11407 if (!info->reduce_memory_overheads)
11408 {
11409 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11410 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11411 && elf_tdata (sub)->symbuf)
11412 {
11413 free (elf_tdata (sub)->symbuf);
11414 elf_tdata (sub)->symbuf = NULL;
11415 }
11416 }
11417
11418 /* Output any global symbols that got converted to local in a
11419 version script or due to symbol visibility. We do this in a
11420 separate step since ELF requires all local symbols to appear
11421 prior to any global symbols. FIXME: We should only do this if
11422 some global symbols were, in fact, converted to become local.
11423 FIXME: Will this work correctly with the Irix 5 linker? */
11424 eoinfo.failed = FALSE;
11425 eoinfo.flinfo = &flinfo;
11426 eoinfo.localsyms = TRUE;
11427 eoinfo.file_sym_done = FALSE;
11428 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11429 if (eoinfo.failed)
11430 return FALSE;
11431
11432 /* If backend needs to output some local symbols not present in the hash
11433 table, do it now. */
11434 if (bed->elf_backend_output_arch_local_syms
11435 && (info->strip != strip_all || emit_relocs))
11436 {
11437 typedef int (*out_sym_func)
11438 (void *, const char *, Elf_Internal_Sym *, asection *,
11439 struct elf_link_hash_entry *);
11440
11441 if (! ((*bed->elf_backend_output_arch_local_syms)
11442 (abfd, info, &flinfo,
11443 (out_sym_func) elf_link_output_symstrtab)))
11444 return FALSE;
11445 }
11446
11447 /* That wrote out all the local symbols. Finish up the symbol table
11448 with the global symbols. Even if we want to strip everything we
11449 can, we still need to deal with those global symbols that got
11450 converted to local in a version script. */
11451
11452 /* The sh_info field records the index of the first non local symbol. */
11453 symtab_hdr->sh_info = bfd_get_symcount (abfd);
11454
11455 if (dynamic
11456 && elf_hash_table (info)->dynsym != NULL
11457 && (elf_hash_table (info)->dynsym->output_section
11458 != bfd_abs_section_ptr))
11459 {
11460 Elf_Internal_Sym sym;
11461 bfd_byte *dynsym = elf_hash_table (info)->dynsym->contents;
11462 long last_local = 0;
11463
11464 /* Write out the section symbols for the output sections. */
11465 if (bfd_link_pic (info)
11466 || elf_hash_table (info)->is_relocatable_executable)
11467 {
11468 asection *s;
11469
11470 sym.st_size = 0;
11471 sym.st_name = 0;
11472 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11473 sym.st_other = 0;
11474 sym.st_target_internal = 0;
11475
11476 for (s = abfd->sections; s != NULL; s = s->next)
11477 {
11478 int indx;
11479 bfd_byte *dest;
11480 long dynindx;
11481
11482 dynindx = elf_section_data (s)->dynindx;
11483 if (dynindx <= 0)
11484 continue;
11485 indx = elf_section_data (s)->this_idx;
11486 BFD_ASSERT (indx > 0);
11487 sym.st_shndx = indx;
11488 if (! check_dynsym (abfd, &sym))
11489 return FALSE;
11490 sym.st_value = s->vma;
11491 dest = dynsym + dynindx * bed->s->sizeof_sym;
11492 if (last_local < dynindx)
11493 last_local = dynindx;
11494 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11495 }
11496 }
11497
11498 /* Write out the local dynsyms. */
11499 if (elf_hash_table (info)->dynlocal)
11500 {
11501 struct elf_link_local_dynamic_entry *e;
11502 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
11503 {
11504 asection *s;
11505 bfd_byte *dest;
11506
11507 /* Copy the internal symbol and turn off visibility.
11508 Note that we saved a word of storage and overwrote
11509 the original st_name with the dynstr_index. */
11510 sym = e->isym;
11511 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
11512
11513 s = bfd_section_from_elf_index (e->input_bfd,
11514 e->isym.st_shndx);
11515 if (s != NULL)
11516 {
11517 sym.st_shndx =
11518 elf_section_data (s->output_section)->this_idx;
11519 if (! check_dynsym (abfd, &sym))
11520 return FALSE;
11521 sym.st_value = (s->output_section->vma
11522 + s->output_offset
11523 + e->isym.st_value);
11524 }
11525
11526 if (last_local < e->dynindx)
11527 last_local = e->dynindx;
11528
11529 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
11530 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11531 }
11532 }
11533
11534 elf_section_data (elf_hash_table (info)->dynsym->output_section)->this_hdr.sh_info =
11535 last_local + 1;
11536 }
11537
11538 /* We get the global symbols from the hash table. */
11539 eoinfo.failed = FALSE;
11540 eoinfo.localsyms = FALSE;
11541 eoinfo.flinfo = &flinfo;
11542 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11543 if (eoinfo.failed)
11544 return FALSE;
11545
11546 /* If backend needs to output some symbols not present in the hash
11547 table, do it now. */
11548 if (bed->elf_backend_output_arch_syms
11549 && (info->strip != strip_all || emit_relocs))
11550 {
11551 typedef int (*out_sym_func)
11552 (void *, const char *, Elf_Internal_Sym *, asection *,
11553 struct elf_link_hash_entry *);
11554
11555 if (! ((*bed->elf_backend_output_arch_syms)
11556 (abfd, info, &flinfo,
11557 (out_sym_func) elf_link_output_symstrtab)))
11558 return FALSE;
11559 }
11560
11561 /* Finalize the .strtab section. */
11562 _bfd_elf_strtab_finalize (flinfo.symstrtab);
11563
11564 /* Swap out the .strtab section. */
11565 if (!elf_link_swap_symbols_out (&flinfo))
11566 return FALSE;
11567
11568 /* Now we know the size of the symtab section. */
11569 if (bfd_get_symcount (abfd) > 0)
11570 {
11571 /* Finish up and write out the symbol string table (.strtab)
11572 section. */
11573 Elf_Internal_Shdr *symstrtab_hdr;
11574 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
11575
11576 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
11577 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
11578 {
11579 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
11580 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
11581 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
11582 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
11583 symtab_shndx_hdr->sh_size = amt;
11584
11585 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
11586 off, TRUE);
11587
11588 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
11589 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
11590 return FALSE;
11591 }
11592
11593 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
11594 /* sh_name was set in prep_headers. */
11595 symstrtab_hdr->sh_type = SHT_STRTAB;
11596 symstrtab_hdr->sh_flags = 0;
11597 symstrtab_hdr->sh_addr = 0;
11598 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
11599 symstrtab_hdr->sh_entsize = 0;
11600 symstrtab_hdr->sh_link = 0;
11601 symstrtab_hdr->sh_info = 0;
11602 /* sh_offset is set just below. */
11603 symstrtab_hdr->sh_addralign = 1;
11604
11605 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
11606 off, TRUE);
11607 elf_next_file_pos (abfd) = off;
11608
11609 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
11610 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
11611 return FALSE;
11612 }
11613
11614 /* Adjust the relocs to have the correct symbol indices. */
11615 for (o = abfd->sections; o != NULL; o = o->next)
11616 {
11617 struct bfd_elf_section_data *esdo = elf_section_data (o);
11618 bfd_boolean sort;
11619 if ((o->flags & SEC_RELOC) == 0)
11620 continue;
11621
11622 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
11623 if (esdo->rel.hdr != NULL
11624 && !elf_link_adjust_relocs (abfd, &esdo->rel, sort))
11625 return FALSE;
11626 if (esdo->rela.hdr != NULL
11627 && !elf_link_adjust_relocs (abfd, &esdo->rela, sort))
11628 return FALSE;
11629
11630 /* Set the reloc_count field to 0 to prevent write_relocs from
11631 trying to swap the relocs out itself. */
11632 o->reloc_count = 0;
11633 }
11634
11635 if (dynamic && info->combreloc && dynobj != NULL)
11636 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
11637
11638 /* If we are linking against a dynamic object, or generating a
11639 shared library, finish up the dynamic linking information. */
11640 if (dynamic)
11641 {
11642 bfd_byte *dyncon, *dynconend;
11643
11644 /* Fix up .dynamic entries. */
11645 o = bfd_get_linker_section (dynobj, ".dynamic");
11646 BFD_ASSERT (o != NULL);
11647
11648 dyncon = o->contents;
11649 dynconend = o->contents + o->size;
11650 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11651 {
11652 Elf_Internal_Dyn dyn;
11653 const char *name;
11654 unsigned int type;
11655
11656 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11657
11658 switch (dyn.d_tag)
11659 {
11660 default:
11661 continue;
11662 case DT_NULL:
11663 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
11664 {
11665 switch (elf_section_data (reldyn)->this_hdr.sh_type)
11666 {
11667 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
11668 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
11669 default: continue;
11670 }
11671 dyn.d_un.d_val = relativecount;
11672 relativecount = 0;
11673 break;
11674 }
11675 continue;
11676
11677 case DT_INIT:
11678 name = info->init_function;
11679 goto get_sym;
11680 case DT_FINI:
11681 name = info->fini_function;
11682 get_sym:
11683 {
11684 struct elf_link_hash_entry *h;
11685
11686 h = elf_link_hash_lookup (elf_hash_table (info), name,
11687 FALSE, FALSE, TRUE);
11688 if (h != NULL
11689 && (h->root.type == bfd_link_hash_defined
11690 || h->root.type == bfd_link_hash_defweak))
11691 {
11692 dyn.d_un.d_ptr = h->root.u.def.value;
11693 o = h->root.u.def.section;
11694 if (o->output_section != NULL)
11695 dyn.d_un.d_ptr += (o->output_section->vma
11696 + o->output_offset);
11697 else
11698 {
11699 /* The symbol is imported from another shared
11700 library and does not apply to this one. */
11701 dyn.d_un.d_ptr = 0;
11702 }
11703 break;
11704 }
11705 }
11706 continue;
11707
11708 case DT_PREINIT_ARRAYSZ:
11709 name = ".preinit_array";
11710 goto get_size;
11711 case DT_INIT_ARRAYSZ:
11712 name = ".init_array";
11713 goto get_size;
11714 case DT_FINI_ARRAYSZ:
11715 name = ".fini_array";
11716 get_size:
11717 o = bfd_get_section_by_name (abfd, name);
11718 if (o == NULL)
11719 {
11720 (*_bfd_error_handler)
11721 (_("%B: could not find output section %s"), abfd, name);
11722 goto error_return;
11723 }
11724 if (o->size == 0)
11725 (*_bfd_error_handler)
11726 (_("warning: %s section has zero size"), name);
11727 dyn.d_un.d_val = o->size;
11728 break;
11729
11730 case DT_PREINIT_ARRAY:
11731 name = ".preinit_array";
11732 goto get_vma;
11733 case DT_INIT_ARRAY:
11734 name = ".init_array";
11735 goto get_vma;
11736 case DT_FINI_ARRAY:
11737 name = ".fini_array";
11738 goto get_vma;
11739
11740 case DT_HASH:
11741 name = ".hash";
11742 goto get_vma;
11743 case DT_GNU_HASH:
11744 name = ".gnu.hash";
11745 goto get_vma;
11746 case DT_STRTAB:
11747 name = ".dynstr";
11748 goto get_vma;
11749 case DT_SYMTAB:
11750 name = ".dynsym";
11751 goto get_vma;
11752 case DT_VERDEF:
11753 name = ".gnu.version_d";
11754 goto get_vma;
11755 case DT_VERNEED:
11756 name = ".gnu.version_r";
11757 goto get_vma;
11758 case DT_VERSYM:
11759 name = ".gnu.version";
11760 get_vma:
11761 o = bfd_get_section_by_name (abfd, name);
11762 if (o == NULL)
11763 {
11764 (*_bfd_error_handler)
11765 (_("%B: could not find output section %s"), abfd, name);
11766 goto error_return;
11767 }
11768 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
11769 {
11770 (*_bfd_error_handler)
11771 (_("warning: section '%s' is being made into a note"), name);
11772 bfd_set_error (bfd_error_nonrepresentable_section);
11773 goto error_return;
11774 }
11775 dyn.d_un.d_ptr = o->vma;
11776 break;
11777
11778 case DT_REL:
11779 case DT_RELA:
11780 case DT_RELSZ:
11781 case DT_RELASZ:
11782 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
11783 type = SHT_REL;
11784 else
11785 type = SHT_RELA;
11786 dyn.d_un.d_val = 0;
11787 dyn.d_un.d_ptr = 0;
11788 for (i = 1; i < elf_numsections (abfd); i++)
11789 {
11790 Elf_Internal_Shdr *hdr;
11791
11792 hdr = elf_elfsections (abfd)[i];
11793 if (hdr->sh_type == type
11794 && (hdr->sh_flags & SHF_ALLOC) != 0)
11795 {
11796 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
11797 dyn.d_un.d_val += hdr->sh_size;
11798 else
11799 {
11800 if (dyn.d_un.d_ptr == 0
11801 || hdr->sh_addr < dyn.d_un.d_ptr)
11802 dyn.d_un.d_ptr = hdr->sh_addr;
11803 }
11804 }
11805 }
11806 break;
11807 }
11808 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
11809 }
11810 }
11811
11812 /* If we have created any dynamic sections, then output them. */
11813 if (dynobj != NULL)
11814 {
11815 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
11816 goto error_return;
11817
11818 /* Check for DT_TEXTREL (late, in case the backend removes it). */
11819 if (((info->warn_shared_textrel && bfd_link_pic (info))
11820 || info->error_textrel)
11821 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
11822 {
11823 bfd_byte *dyncon, *dynconend;
11824
11825 dyncon = o->contents;
11826 dynconend = o->contents + o->size;
11827 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11828 {
11829 Elf_Internal_Dyn dyn;
11830
11831 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11832
11833 if (dyn.d_tag == DT_TEXTREL)
11834 {
11835 if (info->error_textrel)
11836 info->callbacks->einfo
11837 (_("%P%X: read-only segment has dynamic relocations.\n"));
11838 else
11839 info->callbacks->einfo
11840 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
11841 break;
11842 }
11843 }
11844 }
11845
11846 for (o = dynobj->sections; o != NULL; o = o->next)
11847 {
11848 if ((o->flags & SEC_HAS_CONTENTS) == 0
11849 || o->size == 0
11850 || o->output_section == bfd_abs_section_ptr)
11851 continue;
11852 if ((o->flags & SEC_LINKER_CREATED) == 0)
11853 {
11854 /* At this point, we are only interested in sections
11855 created by _bfd_elf_link_create_dynamic_sections. */
11856 continue;
11857 }
11858 if (elf_hash_table (info)->stab_info.stabstr == o)
11859 continue;
11860 if (elf_hash_table (info)->eh_info.hdr_sec == o)
11861 continue;
11862 if (strcmp (o->name, ".dynstr") != 0)
11863 {
11864 /* FIXME: octets_per_byte. */
11865 if (! bfd_set_section_contents (abfd, o->output_section,
11866 o->contents,
11867 (file_ptr) o->output_offset,
11868 o->size))
11869 goto error_return;
11870 }
11871 else
11872 {
11873 /* The contents of the .dynstr section are actually in a
11874 stringtab. */
11875 file_ptr off;
11876
11877 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
11878 if (bfd_seek (abfd, off, SEEK_SET) != 0
11879 || ! _bfd_elf_strtab_emit (abfd,
11880 elf_hash_table (info)->dynstr))
11881 goto error_return;
11882 }
11883 }
11884 }
11885
11886 if (bfd_link_relocatable (info))
11887 {
11888 bfd_boolean failed = FALSE;
11889
11890 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
11891 if (failed)
11892 goto error_return;
11893 }
11894
11895 /* If we have optimized stabs strings, output them. */
11896 if (elf_hash_table (info)->stab_info.stabstr != NULL)
11897 {
11898 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
11899 goto error_return;
11900 }
11901
11902 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
11903 goto error_return;
11904
11905 elf_final_link_free (abfd, &flinfo);
11906
11907 elf_linker (abfd) = TRUE;
11908
11909 if (attr_section)
11910 {
11911 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
11912 if (contents == NULL)
11913 return FALSE; /* Bail out and fail. */
11914 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
11915 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
11916 free (contents);
11917 }
11918
11919 return TRUE;
11920
11921 error_return:
11922 elf_final_link_free (abfd, &flinfo);
11923 return FALSE;
11924 }
11925 \f
11926 /* Initialize COOKIE for input bfd ABFD. */
11927
11928 static bfd_boolean
11929 init_reloc_cookie (struct elf_reloc_cookie *cookie,
11930 struct bfd_link_info *info, bfd *abfd)
11931 {
11932 Elf_Internal_Shdr *symtab_hdr;
11933 const struct elf_backend_data *bed;
11934
11935 bed = get_elf_backend_data (abfd);
11936 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11937
11938 cookie->abfd = abfd;
11939 cookie->sym_hashes = elf_sym_hashes (abfd);
11940 cookie->bad_symtab = elf_bad_symtab (abfd);
11941 if (cookie->bad_symtab)
11942 {
11943 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11944 cookie->extsymoff = 0;
11945 }
11946 else
11947 {
11948 cookie->locsymcount = symtab_hdr->sh_info;
11949 cookie->extsymoff = symtab_hdr->sh_info;
11950 }
11951
11952 if (bed->s->arch_size == 32)
11953 cookie->r_sym_shift = 8;
11954 else
11955 cookie->r_sym_shift = 32;
11956
11957 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
11958 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
11959 {
11960 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
11961 cookie->locsymcount, 0,
11962 NULL, NULL, NULL);
11963 if (cookie->locsyms == NULL)
11964 {
11965 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
11966 return FALSE;
11967 }
11968 if (info->keep_memory)
11969 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
11970 }
11971 return TRUE;
11972 }
11973
11974 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
11975
11976 static void
11977 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
11978 {
11979 Elf_Internal_Shdr *symtab_hdr;
11980
11981 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11982 if (cookie->locsyms != NULL
11983 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
11984 free (cookie->locsyms);
11985 }
11986
11987 /* Initialize the relocation information in COOKIE for input section SEC
11988 of input bfd ABFD. */
11989
11990 static bfd_boolean
11991 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11992 struct bfd_link_info *info, bfd *abfd,
11993 asection *sec)
11994 {
11995 const struct elf_backend_data *bed;
11996
11997 if (sec->reloc_count == 0)
11998 {
11999 cookie->rels = NULL;
12000 cookie->relend = NULL;
12001 }
12002 else
12003 {
12004 bed = get_elf_backend_data (abfd);
12005
12006 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12007 info->keep_memory);
12008 if (cookie->rels == NULL)
12009 return FALSE;
12010 cookie->rel = cookie->rels;
12011 cookie->relend = (cookie->rels
12012 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
12013 }
12014 cookie->rel = cookie->rels;
12015 return TRUE;
12016 }
12017
12018 /* Free the memory allocated by init_reloc_cookie_rels,
12019 if appropriate. */
12020
12021 static void
12022 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12023 asection *sec)
12024 {
12025 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12026 free (cookie->rels);
12027 }
12028
12029 /* Initialize the whole of COOKIE for input section SEC. */
12030
12031 static bfd_boolean
12032 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12033 struct bfd_link_info *info,
12034 asection *sec)
12035 {
12036 if (!init_reloc_cookie (cookie, info, sec->owner))
12037 goto error1;
12038 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12039 goto error2;
12040 return TRUE;
12041
12042 error2:
12043 fini_reloc_cookie (cookie, sec->owner);
12044 error1:
12045 return FALSE;
12046 }
12047
12048 /* Free the memory allocated by init_reloc_cookie_for_section,
12049 if appropriate. */
12050
12051 static void
12052 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12053 asection *sec)
12054 {
12055 fini_reloc_cookie_rels (cookie, sec);
12056 fini_reloc_cookie (cookie, sec->owner);
12057 }
12058 \f
12059 /* Garbage collect unused sections. */
12060
12061 /* Default gc_mark_hook. */
12062
12063 asection *
12064 _bfd_elf_gc_mark_hook (asection *sec,
12065 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12066 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12067 struct elf_link_hash_entry *h,
12068 Elf_Internal_Sym *sym)
12069 {
12070 const char *sec_name;
12071
12072 if (h != NULL)
12073 {
12074 switch (h->root.type)
12075 {
12076 case bfd_link_hash_defined:
12077 case bfd_link_hash_defweak:
12078 return h->root.u.def.section;
12079
12080 case bfd_link_hash_common:
12081 return h->root.u.c.p->section;
12082
12083 case bfd_link_hash_undefined:
12084 case bfd_link_hash_undefweak:
12085 /* To work around a glibc bug, keep all XXX input sections
12086 when there is an as yet undefined reference to __start_XXX
12087 or __stop_XXX symbols. The linker will later define such
12088 symbols for orphan input sections that have a name
12089 representable as a C identifier. */
12090 if (strncmp (h->root.root.string, "__start_", 8) == 0)
12091 sec_name = h->root.root.string + 8;
12092 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
12093 sec_name = h->root.root.string + 7;
12094 else
12095 sec_name = NULL;
12096
12097 if (sec_name && *sec_name != '\0')
12098 {
12099 bfd *i;
12100
12101 for (i = info->input_bfds; i; i = i->link.next)
12102 {
12103 sec = bfd_get_section_by_name (i, sec_name);
12104 if (sec)
12105 sec->flags |= SEC_KEEP;
12106 }
12107 }
12108 break;
12109
12110 default:
12111 break;
12112 }
12113 }
12114 else
12115 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12116
12117 return NULL;
12118 }
12119
12120 /* COOKIE->rel describes a relocation against section SEC, which is
12121 a section we've decided to keep. Return the section that contains
12122 the relocation symbol, or NULL if no section contains it. */
12123
12124 asection *
12125 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12126 elf_gc_mark_hook_fn gc_mark_hook,
12127 struct elf_reloc_cookie *cookie)
12128 {
12129 unsigned long r_symndx;
12130 struct elf_link_hash_entry *h;
12131
12132 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
12133 if (r_symndx == STN_UNDEF)
12134 return NULL;
12135
12136 if (r_symndx >= cookie->locsymcount
12137 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12138 {
12139 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
12140 if (h == NULL)
12141 {
12142 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12143 sec->owner);
12144 return NULL;
12145 }
12146 while (h->root.type == bfd_link_hash_indirect
12147 || h->root.type == bfd_link_hash_warning)
12148 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12149 h->mark = 1;
12150 /* If this symbol is weak and there is a non-weak definition, we
12151 keep the non-weak definition because many backends put
12152 dynamic reloc info on the non-weak definition for code
12153 handling copy relocs. */
12154 if (h->u.weakdef != NULL)
12155 h->u.weakdef->mark = 1;
12156 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12157 }
12158
12159 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12160 &cookie->locsyms[r_symndx]);
12161 }
12162
12163 /* COOKIE->rel describes a relocation against section SEC, which is
12164 a section we've decided to keep. Mark the section that contains
12165 the relocation symbol. */
12166
12167 bfd_boolean
12168 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12169 asection *sec,
12170 elf_gc_mark_hook_fn gc_mark_hook,
12171 struct elf_reloc_cookie *cookie)
12172 {
12173 asection *rsec;
12174
12175 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie);
12176 if (rsec && !rsec->gc_mark)
12177 {
12178 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12179 || (rsec->owner->flags & DYNAMIC) != 0)
12180 rsec->gc_mark = 1;
12181 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12182 return FALSE;
12183 }
12184 return TRUE;
12185 }
12186
12187 /* The mark phase of garbage collection. For a given section, mark
12188 it and any sections in this section's group, and all the sections
12189 which define symbols to which it refers. */
12190
12191 bfd_boolean
12192 _bfd_elf_gc_mark (struct bfd_link_info *info,
12193 asection *sec,
12194 elf_gc_mark_hook_fn gc_mark_hook)
12195 {
12196 bfd_boolean ret;
12197 asection *group_sec, *eh_frame;
12198
12199 sec->gc_mark = 1;
12200
12201 /* Mark all the sections in the group. */
12202 group_sec = elf_section_data (sec)->next_in_group;
12203 if (group_sec && !group_sec->gc_mark)
12204 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
12205 return FALSE;
12206
12207 /* Look through the section relocs. */
12208 ret = TRUE;
12209 eh_frame = elf_eh_frame_section (sec->owner);
12210 if ((sec->flags & SEC_RELOC) != 0
12211 && sec->reloc_count > 0
12212 && sec != eh_frame)
12213 {
12214 struct elf_reloc_cookie cookie;
12215
12216 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12217 ret = FALSE;
12218 else
12219 {
12220 for (; cookie.rel < cookie.relend; cookie.rel++)
12221 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
12222 {
12223 ret = FALSE;
12224 break;
12225 }
12226 fini_reloc_cookie_for_section (&cookie, sec);
12227 }
12228 }
12229
12230 if (ret && eh_frame && elf_fde_list (sec))
12231 {
12232 struct elf_reloc_cookie cookie;
12233
12234 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12235 ret = FALSE;
12236 else
12237 {
12238 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12239 gc_mark_hook, &cookie))
12240 ret = FALSE;
12241 fini_reloc_cookie_for_section (&cookie, eh_frame);
12242 }
12243 }
12244
12245 eh_frame = elf_section_eh_frame_entry (sec);
12246 if (ret && eh_frame && !eh_frame->gc_mark)
12247 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12248 ret = FALSE;
12249
12250 return ret;
12251 }
12252
12253 /* Scan and mark sections in a special or debug section group. */
12254
12255 static void
12256 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12257 {
12258 /* Point to first section of section group. */
12259 asection *ssec;
12260 /* Used to iterate the section group. */
12261 asection *msec;
12262
12263 bfd_boolean is_special_grp = TRUE;
12264 bfd_boolean is_debug_grp = TRUE;
12265
12266 /* First scan to see if group contains any section other than debug
12267 and special section. */
12268 ssec = msec = elf_next_in_group (grp);
12269 do
12270 {
12271 if ((msec->flags & SEC_DEBUGGING) == 0)
12272 is_debug_grp = FALSE;
12273
12274 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12275 is_special_grp = FALSE;
12276
12277 msec = elf_next_in_group (msec);
12278 }
12279 while (msec != ssec);
12280
12281 /* If this is a pure debug section group or pure special section group,
12282 keep all sections in this group. */
12283 if (is_debug_grp || is_special_grp)
12284 {
12285 do
12286 {
12287 msec->gc_mark = 1;
12288 msec = elf_next_in_group (msec);
12289 }
12290 while (msec != ssec);
12291 }
12292 }
12293
12294 /* Keep debug and special sections. */
12295
12296 bfd_boolean
12297 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12298 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12299 {
12300 bfd *ibfd;
12301
12302 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12303 {
12304 asection *isec;
12305 bfd_boolean some_kept;
12306 bfd_boolean debug_frag_seen;
12307
12308 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12309 continue;
12310
12311 /* Ensure all linker created sections are kept,
12312 see if any other section is already marked,
12313 and note if we have any fragmented debug sections. */
12314 debug_frag_seen = some_kept = FALSE;
12315 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12316 {
12317 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12318 isec->gc_mark = 1;
12319 else if (isec->gc_mark)
12320 some_kept = TRUE;
12321
12322 if (debug_frag_seen == FALSE
12323 && (isec->flags & SEC_DEBUGGING)
12324 && CONST_STRNEQ (isec->name, ".debug_line."))
12325 debug_frag_seen = TRUE;
12326 }
12327
12328 /* If no section in this file will be kept, then we can
12329 toss out the debug and special sections. */
12330 if (!some_kept)
12331 continue;
12332
12333 /* Keep debug and special sections like .comment when they are
12334 not part of a group. Also keep section groups that contain
12335 just debug sections or special sections. */
12336 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12337 {
12338 if ((isec->flags & SEC_GROUP) != 0)
12339 _bfd_elf_gc_mark_debug_special_section_group (isec);
12340 else if (((isec->flags & SEC_DEBUGGING) != 0
12341 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12342 && elf_next_in_group (isec) == NULL)
12343 isec->gc_mark = 1;
12344 }
12345
12346 if (! debug_frag_seen)
12347 continue;
12348
12349 /* Look for CODE sections which are going to be discarded,
12350 and find and discard any fragmented debug sections which
12351 are associated with that code section. */
12352 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12353 if ((isec->flags & SEC_CODE) != 0
12354 && isec->gc_mark == 0)
12355 {
12356 unsigned int ilen;
12357 asection *dsec;
12358
12359 ilen = strlen (isec->name);
12360
12361 /* Association is determined by the name of the debug section
12362 containing the name of the code section as a suffix. For
12363 example .debug_line.text.foo is a debug section associated
12364 with .text.foo. */
12365 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12366 {
12367 unsigned int dlen;
12368
12369 if (dsec->gc_mark == 0
12370 || (dsec->flags & SEC_DEBUGGING) == 0)
12371 continue;
12372
12373 dlen = strlen (dsec->name);
12374
12375 if (dlen > ilen
12376 && strncmp (dsec->name + (dlen - ilen),
12377 isec->name, ilen) == 0)
12378 {
12379 dsec->gc_mark = 0;
12380 }
12381 }
12382 }
12383 }
12384 return TRUE;
12385 }
12386
12387 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12388
12389 struct elf_gc_sweep_symbol_info
12390 {
12391 struct bfd_link_info *info;
12392 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
12393 bfd_boolean);
12394 };
12395
12396 static bfd_boolean
12397 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
12398 {
12399 if (!h->mark
12400 && (((h->root.type == bfd_link_hash_defined
12401 || h->root.type == bfd_link_hash_defweak)
12402 && !((h->def_regular || ELF_COMMON_DEF_P (h))
12403 && h->root.u.def.section->gc_mark))
12404 || h->root.type == bfd_link_hash_undefined
12405 || h->root.type == bfd_link_hash_undefweak))
12406 {
12407 struct elf_gc_sweep_symbol_info *inf;
12408
12409 inf = (struct elf_gc_sweep_symbol_info *) data;
12410 (*inf->hide_symbol) (inf->info, h, TRUE);
12411 h->def_regular = 0;
12412 h->ref_regular = 0;
12413 h->ref_regular_nonweak = 0;
12414 }
12415
12416 return TRUE;
12417 }
12418
12419 /* The sweep phase of garbage collection. Remove all garbage sections. */
12420
12421 typedef bfd_boolean (*gc_sweep_hook_fn)
12422 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
12423
12424 static bfd_boolean
12425 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
12426 {
12427 bfd *sub;
12428 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12429 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
12430 unsigned long section_sym_count;
12431 struct elf_gc_sweep_symbol_info sweep_info;
12432
12433 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12434 {
12435 asection *o;
12436
12437 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12438 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
12439 continue;
12440
12441 for (o = sub->sections; o != NULL; o = o->next)
12442 {
12443 /* When any section in a section group is kept, we keep all
12444 sections in the section group. If the first member of
12445 the section group is excluded, we will also exclude the
12446 group section. */
12447 if (o->flags & SEC_GROUP)
12448 {
12449 asection *first = elf_next_in_group (o);
12450 o->gc_mark = first->gc_mark;
12451 }
12452
12453 if (o->gc_mark)
12454 continue;
12455
12456 /* Skip sweeping sections already excluded. */
12457 if (o->flags & SEC_EXCLUDE)
12458 continue;
12459
12460 /* Since this is early in the link process, it is simple
12461 to remove a section from the output. */
12462 o->flags |= SEC_EXCLUDE;
12463
12464 if (info->print_gc_sections && o->size != 0)
12465 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
12466
12467 /* But we also have to update some of the relocation
12468 info we collected before. */
12469 if (gc_sweep_hook
12470 && (o->flags & SEC_RELOC) != 0
12471 && o->reloc_count != 0
12472 && !((info->strip == strip_all || info->strip == strip_debugger)
12473 && (o->flags & SEC_DEBUGGING) != 0)
12474 && !bfd_is_abs_section (o->output_section))
12475 {
12476 Elf_Internal_Rela *internal_relocs;
12477 bfd_boolean r;
12478
12479 internal_relocs
12480 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
12481 info->keep_memory);
12482 if (internal_relocs == NULL)
12483 return FALSE;
12484
12485 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
12486
12487 if (elf_section_data (o)->relocs != internal_relocs)
12488 free (internal_relocs);
12489
12490 if (!r)
12491 return FALSE;
12492 }
12493 }
12494 }
12495
12496 /* Remove the symbols that were in the swept sections from the dynamic
12497 symbol table. GCFIXME: Anyone know how to get them out of the
12498 static symbol table as well? */
12499 sweep_info.info = info;
12500 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
12501 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
12502 &sweep_info);
12503
12504 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
12505 return TRUE;
12506 }
12507
12508 /* Propagate collected vtable information. This is called through
12509 elf_link_hash_traverse. */
12510
12511 static bfd_boolean
12512 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
12513 {
12514 /* Those that are not vtables. */
12515 if (h->vtable == NULL || h->vtable->parent == NULL)
12516 return TRUE;
12517
12518 /* Those vtables that do not have parents, we cannot merge. */
12519 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
12520 return TRUE;
12521
12522 /* If we've already been done, exit. */
12523 if (h->vtable->used && h->vtable->used[-1])
12524 return TRUE;
12525
12526 /* Make sure the parent's table is up to date. */
12527 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
12528
12529 if (h->vtable->used == NULL)
12530 {
12531 /* None of this table's entries were referenced. Re-use the
12532 parent's table. */
12533 h->vtable->used = h->vtable->parent->vtable->used;
12534 h->vtable->size = h->vtable->parent->vtable->size;
12535 }
12536 else
12537 {
12538 size_t n;
12539 bfd_boolean *cu, *pu;
12540
12541 /* Or the parent's entries into ours. */
12542 cu = h->vtable->used;
12543 cu[-1] = TRUE;
12544 pu = h->vtable->parent->vtable->used;
12545 if (pu != NULL)
12546 {
12547 const struct elf_backend_data *bed;
12548 unsigned int log_file_align;
12549
12550 bed = get_elf_backend_data (h->root.u.def.section->owner);
12551 log_file_align = bed->s->log_file_align;
12552 n = h->vtable->parent->vtable->size >> log_file_align;
12553 while (n--)
12554 {
12555 if (*pu)
12556 *cu = TRUE;
12557 pu++;
12558 cu++;
12559 }
12560 }
12561 }
12562
12563 return TRUE;
12564 }
12565
12566 static bfd_boolean
12567 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
12568 {
12569 asection *sec;
12570 bfd_vma hstart, hend;
12571 Elf_Internal_Rela *relstart, *relend, *rel;
12572 const struct elf_backend_data *bed;
12573 unsigned int log_file_align;
12574
12575 /* Take care of both those symbols that do not describe vtables as
12576 well as those that are not loaded. */
12577 if (h->vtable == NULL || h->vtable->parent == NULL)
12578 return TRUE;
12579
12580 BFD_ASSERT (h->root.type == bfd_link_hash_defined
12581 || h->root.type == bfd_link_hash_defweak);
12582
12583 sec = h->root.u.def.section;
12584 hstart = h->root.u.def.value;
12585 hend = hstart + h->size;
12586
12587 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
12588 if (!relstart)
12589 return *(bfd_boolean *) okp = FALSE;
12590 bed = get_elf_backend_data (sec->owner);
12591 log_file_align = bed->s->log_file_align;
12592
12593 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
12594
12595 for (rel = relstart; rel < relend; ++rel)
12596 if (rel->r_offset >= hstart && rel->r_offset < hend)
12597 {
12598 /* If the entry is in use, do nothing. */
12599 if (h->vtable->used
12600 && (rel->r_offset - hstart) < h->vtable->size)
12601 {
12602 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
12603 if (h->vtable->used[entry])
12604 continue;
12605 }
12606 /* Otherwise, kill it. */
12607 rel->r_offset = rel->r_info = rel->r_addend = 0;
12608 }
12609
12610 return TRUE;
12611 }
12612
12613 /* Mark sections containing dynamically referenced symbols. When
12614 building shared libraries, we must assume that any visible symbol is
12615 referenced. */
12616
12617 bfd_boolean
12618 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
12619 {
12620 struct bfd_link_info *info = (struct bfd_link_info *) inf;
12621 struct bfd_elf_dynamic_list *d = info->dynamic_list;
12622
12623 if ((h->root.type == bfd_link_hash_defined
12624 || h->root.type == bfd_link_hash_defweak)
12625 && (h->ref_dynamic
12626 || ((h->def_regular || ELF_COMMON_DEF_P (h))
12627 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
12628 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
12629 && (!bfd_link_executable (info)
12630 || info->export_dynamic
12631 || (h->dynamic
12632 && d != NULL
12633 && (*d->match) (&d->head, NULL, h->root.root.string)))
12634 && (h->versioned >= versioned
12635 || !bfd_hide_sym_by_version (info->version_info,
12636 h->root.root.string)))))
12637 h->root.u.def.section->flags |= SEC_KEEP;
12638
12639 return TRUE;
12640 }
12641
12642 /* Keep all sections containing symbols undefined on the command-line,
12643 and the section containing the entry symbol. */
12644
12645 void
12646 _bfd_elf_gc_keep (struct bfd_link_info *info)
12647 {
12648 struct bfd_sym_chain *sym;
12649
12650 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
12651 {
12652 struct elf_link_hash_entry *h;
12653
12654 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
12655 FALSE, FALSE, FALSE);
12656
12657 if (h != NULL
12658 && (h->root.type == bfd_link_hash_defined
12659 || h->root.type == bfd_link_hash_defweak)
12660 && !bfd_is_abs_section (h->root.u.def.section))
12661 h->root.u.def.section->flags |= SEC_KEEP;
12662 }
12663 }
12664
12665 bfd_boolean
12666 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
12667 struct bfd_link_info *info)
12668 {
12669 bfd *ibfd = info->input_bfds;
12670
12671 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12672 {
12673 asection *sec;
12674 struct elf_reloc_cookie cookie;
12675
12676 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12677 continue;
12678
12679 if (!init_reloc_cookie (&cookie, info, ibfd))
12680 return FALSE;
12681
12682 for (sec = ibfd->sections; sec; sec = sec->next)
12683 {
12684 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
12685 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
12686 {
12687 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
12688 fini_reloc_cookie_rels (&cookie, sec);
12689 }
12690 }
12691 }
12692 return TRUE;
12693 }
12694
12695 /* Do mark and sweep of unused sections. */
12696
12697 bfd_boolean
12698 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
12699 {
12700 bfd_boolean ok = TRUE;
12701 bfd *sub;
12702 elf_gc_mark_hook_fn gc_mark_hook;
12703 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12704 struct elf_link_hash_table *htab;
12705
12706 if (!bed->can_gc_sections
12707 || !is_elf_hash_table (info->hash))
12708 {
12709 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
12710 return TRUE;
12711 }
12712
12713 bed->gc_keep (info);
12714 htab = elf_hash_table (info);
12715
12716 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
12717 at the .eh_frame section if we can mark the FDEs individually. */
12718 for (sub = info->input_bfds;
12719 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
12720 sub = sub->link.next)
12721 {
12722 asection *sec;
12723 struct elf_reloc_cookie cookie;
12724
12725 sec = bfd_get_section_by_name (sub, ".eh_frame");
12726 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
12727 {
12728 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
12729 if (elf_section_data (sec)->sec_info
12730 && (sec->flags & SEC_LINKER_CREATED) == 0)
12731 elf_eh_frame_section (sub) = sec;
12732 fini_reloc_cookie_for_section (&cookie, sec);
12733 sec = bfd_get_next_section_by_name (sec);
12734 }
12735 }
12736
12737 /* Apply transitive closure to the vtable entry usage info. */
12738 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
12739 if (!ok)
12740 return FALSE;
12741
12742 /* Kill the vtable relocations that were not used. */
12743 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
12744 if (!ok)
12745 return FALSE;
12746
12747 /* Mark dynamically referenced symbols. */
12748 if (htab->dynamic_sections_created)
12749 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
12750
12751 /* Grovel through relocs to find out who stays ... */
12752 gc_mark_hook = bed->gc_mark_hook;
12753 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12754 {
12755 asection *o;
12756
12757 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12758 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
12759 continue;
12760
12761 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
12762 Also treat note sections as a root, if the section is not part
12763 of a group. */
12764 for (o = sub->sections; o != NULL; o = o->next)
12765 if (!o->gc_mark
12766 && (o->flags & SEC_EXCLUDE) == 0
12767 && ((o->flags & SEC_KEEP) != 0
12768 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
12769 && elf_next_in_group (o) == NULL )))
12770 {
12771 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12772 return FALSE;
12773 }
12774 }
12775
12776 /* Allow the backend to mark additional target specific sections. */
12777 bed->gc_mark_extra_sections (info, gc_mark_hook);
12778
12779 /* ... and mark SEC_EXCLUDE for those that go. */
12780 return elf_gc_sweep (abfd, info);
12781 }
12782 \f
12783 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
12784
12785 bfd_boolean
12786 bfd_elf_gc_record_vtinherit (bfd *abfd,
12787 asection *sec,
12788 struct elf_link_hash_entry *h,
12789 bfd_vma offset)
12790 {
12791 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
12792 struct elf_link_hash_entry **search, *child;
12793 bfd_size_type extsymcount;
12794 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12795
12796 /* The sh_info field of the symtab header tells us where the
12797 external symbols start. We don't care about the local symbols at
12798 this point. */
12799 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
12800 if (!elf_bad_symtab (abfd))
12801 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
12802
12803 sym_hashes = elf_sym_hashes (abfd);
12804 sym_hashes_end = sym_hashes + extsymcount;
12805
12806 /* Hunt down the child symbol, which is in this section at the same
12807 offset as the relocation. */
12808 for (search = sym_hashes; search != sym_hashes_end; ++search)
12809 {
12810 if ((child = *search) != NULL
12811 && (child->root.type == bfd_link_hash_defined
12812 || child->root.type == bfd_link_hash_defweak)
12813 && child->root.u.def.section == sec
12814 && child->root.u.def.value == offset)
12815 goto win;
12816 }
12817
12818 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
12819 abfd, sec, (unsigned long) offset);
12820 bfd_set_error (bfd_error_invalid_operation);
12821 return FALSE;
12822
12823 win:
12824 if (!child->vtable)
12825 {
12826 child->vtable = ((struct elf_link_virtual_table_entry *)
12827 bfd_zalloc (abfd, sizeof (*child->vtable)));
12828 if (!child->vtable)
12829 return FALSE;
12830 }
12831 if (!h)
12832 {
12833 /* This *should* only be the absolute section. It could potentially
12834 be that someone has defined a non-global vtable though, which
12835 would be bad. It isn't worth paging in the local symbols to be
12836 sure though; that case should simply be handled by the assembler. */
12837
12838 child->vtable->parent = (struct elf_link_hash_entry *) -1;
12839 }
12840 else
12841 child->vtable->parent = h;
12842
12843 return TRUE;
12844 }
12845
12846 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
12847
12848 bfd_boolean
12849 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
12850 asection *sec ATTRIBUTE_UNUSED,
12851 struct elf_link_hash_entry *h,
12852 bfd_vma addend)
12853 {
12854 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12855 unsigned int log_file_align = bed->s->log_file_align;
12856
12857 if (!h->vtable)
12858 {
12859 h->vtable = ((struct elf_link_virtual_table_entry *)
12860 bfd_zalloc (abfd, sizeof (*h->vtable)));
12861 if (!h->vtable)
12862 return FALSE;
12863 }
12864
12865 if (addend >= h->vtable->size)
12866 {
12867 size_t size, bytes, file_align;
12868 bfd_boolean *ptr = h->vtable->used;
12869
12870 /* While the symbol is undefined, we have to be prepared to handle
12871 a zero size. */
12872 file_align = 1 << log_file_align;
12873 if (h->root.type == bfd_link_hash_undefined)
12874 size = addend + file_align;
12875 else
12876 {
12877 size = h->size;
12878 if (addend >= size)
12879 {
12880 /* Oops! We've got a reference past the defined end of
12881 the table. This is probably a bug -- shall we warn? */
12882 size = addend + file_align;
12883 }
12884 }
12885 size = (size + file_align - 1) & -file_align;
12886
12887 /* Allocate one extra entry for use as a "done" flag for the
12888 consolidation pass. */
12889 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
12890
12891 if (ptr)
12892 {
12893 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
12894
12895 if (ptr != NULL)
12896 {
12897 size_t oldbytes;
12898
12899 oldbytes = (((h->vtable->size >> log_file_align) + 1)
12900 * sizeof (bfd_boolean));
12901 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
12902 }
12903 }
12904 else
12905 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
12906
12907 if (ptr == NULL)
12908 return FALSE;
12909
12910 /* And arrange for that done flag to be at index -1. */
12911 h->vtable->used = ptr + 1;
12912 h->vtable->size = size;
12913 }
12914
12915 h->vtable->used[addend >> log_file_align] = TRUE;
12916
12917 return TRUE;
12918 }
12919
12920 /* Map an ELF section header flag to its corresponding string. */
12921 typedef struct
12922 {
12923 char *flag_name;
12924 flagword flag_value;
12925 } elf_flags_to_name_table;
12926
12927 static elf_flags_to_name_table elf_flags_to_names [] =
12928 {
12929 { "SHF_WRITE", SHF_WRITE },
12930 { "SHF_ALLOC", SHF_ALLOC },
12931 { "SHF_EXECINSTR", SHF_EXECINSTR },
12932 { "SHF_MERGE", SHF_MERGE },
12933 { "SHF_STRINGS", SHF_STRINGS },
12934 { "SHF_INFO_LINK", SHF_INFO_LINK},
12935 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
12936 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
12937 { "SHF_GROUP", SHF_GROUP },
12938 { "SHF_TLS", SHF_TLS },
12939 { "SHF_MASKOS", SHF_MASKOS },
12940 { "SHF_EXCLUDE", SHF_EXCLUDE },
12941 };
12942
12943 /* Returns TRUE if the section is to be included, otherwise FALSE. */
12944 bfd_boolean
12945 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
12946 struct flag_info *flaginfo,
12947 asection *section)
12948 {
12949 const bfd_vma sh_flags = elf_section_flags (section);
12950
12951 if (!flaginfo->flags_initialized)
12952 {
12953 bfd *obfd = info->output_bfd;
12954 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
12955 struct flag_info_list *tf = flaginfo->flag_list;
12956 int with_hex = 0;
12957 int without_hex = 0;
12958
12959 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
12960 {
12961 unsigned i;
12962 flagword (*lookup) (char *);
12963
12964 lookup = bed->elf_backend_lookup_section_flags_hook;
12965 if (lookup != NULL)
12966 {
12967 flagword hexval = (*lookup) ((char *) tf->name);
12968
12969 if (hexval != 0)
12970 {
12971 if (tf->with == with_flags)
12972 with_hex |= hexval;
12973 else if (tf->with == without_flags)
12974 without_hex |= hexval;
12975 tf->valid = TRUE;
12976 continue;
12977 }
12978 }
12979 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
12980 {
12981 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
12982 {
12983 if (tf->with == with_flags)
12984 with_hex |= elf_flags_to_names[i].flag_value;
12985 else if (tf->with == without_flags)
12986 without_hex |= elf_flags_to_names[i].flag_value;
12987 tf->valid = TRUE;
12988 break;
12989 }
12990 }
12991 if (!tf->valid)
12992 {
12993 info->callbacks->einfo
12994 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
12995 return FALSE;
12996 }
12997 }
12998 flaginfo->flags_initialized = TRUE;
12999 flaginfo->only_with_flags |= with_hex;
13000 flaginfo->not_with_flags |= without_hex;
13001 }
13002
13003 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13004 return FALSE;
13005
13006 if ((flaginfo->not_with_flags & sh_flags) != 0)
13007 return FALSE;
13008
13009 return TRUE;
13010 }
13011
13012 struct alloc_got_off_arg {
13013 bfd_vma gotoff;
13014 struct bfd_link_info *info;
13015 };
13016
13017 /* We need a special top-level link routine to convert got reference counts
13018 to real got offsets. */
13019
13020 static bfd_boolean
13021 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13022 {
13023 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13024 bfd *obfd = gofarg->info->output_bfd;
13025 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13026
13027 if (h->got.refcount > 0)
13028 {
13029 h->got.offset = gofarg->gotoff;
13030 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13031 }
13032 else
13033 h->got.offset = (bfd_vma) -1;
13034
13035 return TRUE;
13036 }
13037
13038 /* And an accompanying bit to work out final got entry offsets once
13039 we're done. Should be called from final_link. */
13040
13041 bfd_boolean
13042 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13043 struct bfd_link_info *info)
13044 {
13045 bfd *i;
13046 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13047 bfd_vma gotoff;
13048 struct alloc_got_off_arg gofarg;
13049
13050 BFD_ASSERT (abfd == info->output_bfd);
13051
13052 if (! is_elf_hash_table (info->hash))
13053 return FALSE;
13054
13055 /* The GOT offset is relative to the .got section, but the GOT header is
13056 put into the .got.plt section, if the backend uses it. */
13057 if (bed->want_got_plt)
13058 gotoff = 0;
13059 else
13060 gotoff = bed->got_header_size;
13061
13062 /* Do the local .got entries first. */
13063 for (i = info->input_bfds; i; i = i->link.next)
13064 {
13065 bfd_signed_vma *local_got;
13066 bfd_size_type j, locsymcount;
13067 Elf_Internal_Shdr *symtab_hdr;
13068
13069 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13070 continue;
13071
13072 local_got = elf_local_got_refcounts (i);
13073 if (!local_got)
13074 continue;
13075
13076 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13077 if (elf_bad_symtab (i))
13078 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13079 else
13080 locsymcount = symtab_hdr->sh_info;
13081
13082 for (j = 0; j < locsymcount; ++j)
13083 {
13084 if (local_got[j] > 0)
13085 {
13086 local_got[j] = gotoff;
13087 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13088 }
13089 else
13090 local_got[j] = (bfd_vma) -1;
13091 }
13092 }
13093
13094 /* Then the global .got entries. .plt refcounts are handled by
13095 adjust_dynamic_symbol */
13096 gofarg.gotoff = gotoff;
13097 gofarg.info = info;
13098 elf_link_hash_traverse (elf_hash_table (info),
13099 elf_gc_allocate_got_offsets,
13100 &gofarg);
13101 return TRUE;
13102 }
13103
13104 /* Many folk need no more in the way of final link than this, once
13105 got entry reference counting is enabled. */
13106
13107 bfd_boolean
13108 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13109 {
13110 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13111 return FALSE;
13112
13113 /* Invoke the regular ELF backend linker to do all the work. */
13114 return bfd_elf_final_link (abfd, info);
13115 }
13116
13117 bfd_boolean
13118 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13119 {
13120 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13121
13122 if (rcookie->bad_symtab)
13123 rcookie->rel = rcookie->rels;
13124
13125 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13126 {
13127 unsigned long r_symndx;
13128
13129 if (! rcookie->bad_symtab)
13130 if (rcookie->rel->r_offset > offset)
13131 return FALSE;
13132 if (rcookie->rel->r_offset != offset)
13133 continue;
13134
13135 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
13136 if (r_symndx == STN_UNDEF)
13137 return TRUE;
13138
13139 if (r_symndx >= rcookie->locsymcount
13140 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13141 {
13142 struct elf_link_hash_entry *h;
13143
13144 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13145
13146 while (h->root.type == bfd_link_hash_indirect
13147 || h->root.type == bfd_link_hash_warning)
13148 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13149
13150 if ((h->root.type == bfd_link_hash_defined
13151 || h->root.type == bfd_link_hash_defweak)
13152 && (h->root.u.def.section->owner != rcookie->abfd
13153 || h->root.u.def.section->kept_section != NULL
13154 || discarded_section (h->root.u.def.section)))
13155 return TRUE;
13156 }
13157 else
13158 {
13159 /* It's not a relocation against a global symbol,
13160 but it could be a relocation against a local
13161 symbol for a discarded section. */
13162 asection *isec;
13163 Elf_Internal_Sym *isym;
13164
13165 /* Need to: get the symbol; get the section. */
13166 isym = &rcookie->locsyms[r_symndx];
13167 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
13168 if (isec != NULL
13169 && (isec->kept_section != NULL
13170 || discarded_section (isec)))
13171 return TRUE;
13172 }
13173 return FALSE;
13174 }
13175 return FALSE;
13176 }
13177
13178 /* Discard unneeded references to discarded sections.
13179 Returns -1 on error, 1 if any section's size was changed, 0 if
13180 nothing changed. This function assumes that the relocations are in
13181 sorted order, which is true for all known assemblers. */
13182
13183 int
13184 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13185 {
13186 struct elf_reloc_cookie cookie;
13187 asection *o;
13188 bfd *abfd;
13189 int changed = 0;
13190
13191 if (info->traditional_format
13192 || !is_elf_hash_table (info->hash))
13193 return 0;
13194
13195 o = bfd_get_section_by_name (output_bfd, ".stab");
13196 if (o != NULL)
13197 {
13198 asection *i;
13199
13200 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13201 {
13202 if (i->size == 0
13203 || i->reloc_count == 0
13204 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13205 continue;
13206
13207 abfd = i->owner;
13208 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13209 continue;
13210
13211 if (!init_reloc_cookie_for_section (&cookie, info, i))
13212 return -1;
13213
13214 if (_bfd_discard_section_stabs (abfd, i,
13215 elf_section_data (i)->sec_info,
13216 bfd_elf_reloc_symbol_deleted_p,
13217 &cookie))
13218 changed = 1;
13219
13220 fini_reloc_cookie_for_section (&cookie, i);
13221 }
13222 }
13223
13224 o = NULL;
13225 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13226 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
13227 if (o != NULL)
13228 {
13229 asection *i;
13230
13231 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13232 {
13233 if (i->size == 0)
13234 continue;
13235
13236 abfd = i->owner;
13237 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13238 continue;
13239
13240 if (!init_reloc_cookie_for_section (&cookie, info, i))
13241 return -1;
13242
13243 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13244 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
13245 bfd_elf_reloc_symbol_deleted_p,
13246 &cookie))
13247 changed = 1;
13248
13249 fini_reloc_cookie_for_section (&cookie, i);
13250 }
13251 }
13252
13253 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13254 {
13255 const struct elf_backend_data *bed;
13256
13257 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13258 continue;
13259
13260 bed = get_elf_backend_data (abfd);
13261
13262 if (bed->elf_backend_discard_info != NULL)
13263 {
13264 if (!init_reloc_cookie (&cookie, info, abfd))
13265 return -1;
13266
13267 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
13268 changed = 1;
13269
13270 fini_reloc_cookie (&cookie, abfd);
13271 }
13272 }
13273
13274 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13275 _bfd_elf_end_eh_frame_parsing (info);
13276
13277 if (info->eh_frame_hdr_type
13278 && !bfd_link_relocatable (info)
13279 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
13280 changed = 1;
13281
13282 return changed;
13283 }
13284
13285 bfd_boolean
13286 _bfd_elf_section_already_linked (bfd *abfd,
13287 asection *sec,
13288 struct bfd_link_info *info)
13289 {
13290 flagword flags;
13291 const char *name, *key;
13292 struct bfd_section_already_linked *l;
13293 struct bfd_section_already_linked_hash_entry *already_linked_list;
13294
13295 if (sec->output_section == bfd_abs_section_ptr)
13296 return FALSE;
13297
13298 flags = sec->flags;
13299
13300 /* Return if it isn't a linkonce section. A comdat group section
13301 also has SEC_LINK_ONCE set. */
13302 if ((flags & SEC_LINK_ONCE) == 0)
13303 return FALSE;
13304
13305 /* Don't put group member sections on our list of already linked
13306 sections. They are handled as a group via their group section. */
13307 if (elf_sec_group (sec) != NULL)
13308 return FALSE;
13309
13310 /* For a SHT_GROUP section, use the group signature as the key. */
13311 name = sec->name;
13312 if ((flags & SEC_GROUP) != 0
13313 && elf_next_in_group (sec) != NULL
13314 && elf_group_name (elf_next_in_group (sec)) != NULL)
13315 key = elf_group_name (elf_next_in_group (sec));
13316 else
13317 {
13318 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
13319 if (CONST_STRNEQ (name, ".gnu.linkonce.")
13320 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13321 key++;
13322 else
13323 /* Must be a user linkonce section that doesn't follow gcc's
13324 naming convention. In this case we won't be matching
13325 single member groups. */
13326 key = name;
13327 }
13328
13329 already_linked_list = bfd_section_already_linked_table_lookup (key);
13330
13331 for (l = already_linked_list->entry; l != NULL; l = l->next)
13332 {
13333 /* We may have 2 different types of sections on the list: group
13334 sections with a signature of <key> (<key> is some string),
13335 and linkonce sections named .gnu.linkonce.<type>.<key>.
13336 Match like sections. LTO plugin sections are an exception.
13337 They are always named .gnu.linkonce.t.<key> and match either
13338 type of section. */
13339 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13340 && ((flags & SEC_GROUP) != 0
13341 || strcmp (name, l->sec->name) == 0))
13342 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
13343 {
13344 /* The section has already been linked. See if we should
13345 issue a warning. */
13346 if (!_bfd_handle_already_linked (sec, l, info))
13347 return FALSE;
13348
13349 if (flags & SEC_GROUP)
13350 {
13351 asection *first = elf_next_in_group (sec);
13352 asection *s = first;
13353
13354 while (s != NULL)
13355 {
13356 s->output_section = bfd_abs_section_ptr;
13357 /* Record which group discards it. */
13358 s->kept_section = l->sec;
13359 s = elf_next_in_group (s);
13360 /* These lists are circular. */
13361 if (s == first)
13362 break;
13363 }
13364 }
13365
13366 return TRUE;
13367 }
13368 }
13369
13370 /* A single member comdat group section may be discarded by a
13371 linkonce section and vice versa. */
13372 if ((flags & SEC_GROUP) != 0)
13373 {
13374 asection *first = elf_next_in_group (sec);
13375
13376 if (first != NULL && elf_next_in_group (first) == first)
13377 /* Check this single member group against linkonce sections. */
13378 for (l = already_linked_list->entry; l != NULL; l = l->next)
13379 if ((l->sec->flags & SEC_GROUP) == 0
13380 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13381 {
13382 first->output_section = bfd_abs_section_ptr;
13383 first->kept_section = l->sec;
13384 sec->output_section = bfd_abs_section_ptr;
13385 break;
13386 }
13387 }
13388 else
13389 /* Check this linkonce section against single member groups. */
13390 for (l = already_linked_list->entry; l != NULL; l = l->next)
13391 if (l->sec->flags & SEC_GROUP)
13392 {
13393 asection *first = elf_next_in_group (l->sec);
13394
13395 if (first != NULL
13396 && elf_next_in_group (first) == first
13397 && bfd_elf_match_symbols_in_sections (first, sec, info))
13398 {
13399 sec->output_section = bfd_abs_section_ptr;
13400 sec->kept_section = first;
13401 break;
13402 }
13403 }
13404
13405 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13406 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13407 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13408 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13409 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13410 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13411 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13412 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13413 The reverse order cannot happen as there is never a bfd with only the
13414 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13415 matter as here were are looking only for cross-bfd sections. */
13416
13417 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13418 for (l = already_linked_list->entry; l != NULL; l = l->next)
13419 if ((l->sec->flags & SEC_GROUP) == 0
13420 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13421 {
13422 if (abfd != l->sec->owner)
13423 sec->output_section = bfd_abs_section_ptr;
13424 break;
13425 }
13426
13427 /* This is the first section with this name. Record it. */
13428 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
13429 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
13430 return sec->output_section == bfd_abs_section_ptr;
13431 }
13432
13433 bfd_boolean
13434 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
13435 {
13436 return sym->st_shndx == SHN_COMMON;
13437 }
13438
13439 unsigned int
13440 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13441 {
13442 return SHN_COMMON;
13443 }
13444
13445 asection *
13446 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13447 {
13448 return bfd_com_section_ptr;
13449 }
13450
13451 bfd_vma
13452 _bfd_elf_default_got_elt_size (bfd *abfd,
13453 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13454 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13455 bfd *ibfd ATTRIBUTE_UNUSED,
13456 unsigned long symndx ATTRIBUTE_UNUSED)
13457 {
13458 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13459 return bed->s->arch_size / 8;
13460 }
13461
13462 /* Routines to support the creation of dynamic relocs. */
13463
13464 /* Returns the name of the dynamic reloc section associated with SEC. */
13465
13466 static const char *
13467 get_dynamic_reloc_section_name (bfd * abfd,
13468 asection * sec,
13469 bfd_boolean is_rela)
13470 {
13471 char *name;
13472 const char *old_name = bfd_get_section_name (NULL, sec);
13473 const char *prefix = is_rela ? ".rela" : ".rel";
13474
13475 if (old_name == NULL)
13476 return NULL;
13477
13478 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
13479 sprintf (name, "%s%s", prefix, old_name);
13480
13481 return name;
13482 }
13483
13484 /* Returns the dynamic reloc section associated with SEC.
13485 If necessary compute the name of the dynamic reloc section based
13486 on SEC's name (looked up in ABFD's string table) and the setting
13487 of IS_RELA. */
13488
13489 asection *
13490 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
13491 asection * sec,
13492 bfd_boolean is_rela)
13493 {
13494 asection * reloc_sec = elf_section_data (sec)->sreloc;
13495
13496 if (reloc_sec == NULL)
13497 {
13498 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13499
13500 if (name != NULL)
13501 {
13502 reloc_sec = bfd_get_linker_section (abfd, name);
13503
13504 if (reloc_sec != NULL)
13505 elf_section_data (sec)->sreloc = reloc_sec;
13506 }
13507 }
13508
13509 return reloc_sec;
13510 }
13511
13512 /* Returns the dynamic reloc section associated with SEC. If the
13513 section does not exist it is created and attached to the DYNOBJ
13514 bfd and stored in the SRELOC field of SEC's elf_section_data
13515 structure.
13516
13517 ALIGNMENT is the alignment for the newly created section and
13518 IS_RELA defines whether the name should be .rela.<SEC's name>
13519 or .rel.<SEC's name>. The section name is looked up in the
13520 string table associated with ABFD. */
13521
13522 asection *
13523 _bfd_elf_make_dynamic_reloc_section (asection *sec,
13524 bfd *dynobj,
13525 unsigned int alignment,
13526 bfd *abfd,
13527 bfd_boolean is_rela)
13528 {
13529 asection * reloc_sec = elf_section_data (sec)->sreloc;
13530
13531 if (reloc_sec == NULL)
13532 {
13533 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13534
13535 if (name == NULL)
13536 return NULL;
13537
13538 reloc_sec = bfd_get_linker_section (dynobj, name);
13539
13540 if (reloc_sec == NULL)
13541 {
13542 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
13543 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
13544 if ((sec->flags & SEC_ALLOC) != 0)
13545 flags |= SEC_ALLOC | SEC_LOAD;
13546
13547 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
13548 if (reloc_sec != NULL)
13549 {
13550 /* _bfd_elf_get_sec_type_attr chooses a section type by
13551 name. Override as it may be wrong, eg. for a user
13552 section named "auto" we'll get ".relauto" which is
13553 seen to be a .rela section. */
13554 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
13555 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
13556 reloc_sec = NULL;
13557 }
13558 }
13559
13560 elf_section_data (sec)->sreloc = reloc_sec;
13561 }
13562
13563 return reloc_sec;
13564 }
13565
13566 /* Copy the ELF symbol type and other attributes for a linker script
13567 assignment from HSRC to HDEST. Generally this should be treated as
13568 if we found a strong non-dynamic definition for HDEST (except that
13569 ld ignores multiple definition errors). */
13570 void
13571 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
13572 struct bfd_link_hash_entry *hdest,
13573 struct bfd_link_hash_entry *hsrc)
13574 {
13575 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
13576 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
13577 Elf_Internal_Sym isym;
13578
13579 ehdest->type = ehsrc->type;
13580 ehdest->target_internal = ehsrc->target_internal;
13581
13582 isym.st_other = ehsrc->other;
13583 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
13584 }
13585
13586 /* Append a RELA relocation REL to section S in BFD. */
13587
13588 void
13589 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13590 {
13591 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13592 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
13593 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
13594 bed->s->swap_reloca_out (abfd, rel, loc);
13595 }
13596
13597 /* Append a REL relocation REL to section S in BFD. */
13598
13599 void
13600 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13601 {
13602 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13603 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
13604 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
13605 bed->s->swap_reloc_out (abfd, rel, loc);
13606 }
This page took 0.360982 seconds and 4 git commands to generate.