Don't change the default symbol for relocatable link
[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 (info->executable)
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
283 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
284 flags | SEC_READONLY);
285 if (s == NULL)
286 return FALSE;
287
288 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
289 if (s == NULL
290 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
291 return FALSE;
292
293 /* The special symbol _DYNAMIC is always set to the start of the
294 .dynamic section. We could set _DYNAMIC in a linker script, but we
295 only want to define it if we are, in fact, creating a .dynamic
296 section. We don't want to define it if there is no .dynamic
297 section, since on some ELF platforms the start up code examines it
298 to decide how to initialize the process. */
299 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
300 elf_hash_table (info)->hdynamic = h;
301 if (h == NULL)
302 return FALSE;
303
304 if (info->emit_hash)
305 {
306 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
307 flags | SEC_READONLY);
308 if (s == NULL
309 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
310 return FALSE;
311 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
312 }
313
314 if (info->emit_gnu_hash)
315 {
316 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
317 flags | SEC_READONLY);
318 if (s == NULL
319 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
320 return FALSE;
321 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
322 4 32-bit words followed by variable count of 64-bit words, then
323 variable count of 32-bit words. */
324 if (bed->s->arch_size == 64)
325 elf_section_data (s)->this_hdr.sh_entsize = 0;
326 else
327 elf_section_data (s)->this_hdr.sh_entsize = 4;
328 }
329
330 /* Let the backend create the rest of the sections. This lets the
331 backend set the right flags. The backend will normally create
332 the .got and .plt sections. */
333 if (bed->elf_backend_create_dynamic_sections == NULL
334 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
335 return FALSE;
336
337 elf_hash_table (info)->dynamic_sections_created = TRUE;
338
339 return TRUE;
340 }
341
342 /* Create dynamic sections when linking against a dynamic object. */
343
344 bfd_boolean
345 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
346 {
347 flagword flags, pltflags;
348 struct elf_link_hash_entry *h;
349 asection *s;
350 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
351 struct elf_link_hash_table *htab = elf_hash_table (info);
352
353 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
354 .rel[a].bss sections. */
355 flags = bed->dynamic_sec_flags;
356
357 pltflags = flags;
358 if (bed->plt_not_loaded)
359 /* We do not clear SEC_ALLOC here because we still want the OS to
360 allocate space for the section; it's just that there's nothing
361 to read in from the object file. */
362 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
363 else
364 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
365 if (bed->plt_readonly)
366 pltflags |= SEC_READONLY;
367
368 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
369 if (s == NULL
370 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
371 return FALSE;
372 htab->splt = s;
373
374 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
375 .plt section. */
376 if (bed->want_plt_sym)
377 {
378 h = _bfd_elf_define_linkage_sym (abfd, info, s,
379 "_PROCEDURE_LINKAGE_TABLE_");
380 elf_hash_table (info)->hplt = h;
381 if (h == NULL)
382 return FALSE;
383 }
384
385 s = bfd_make_section_anyway_with_flags (abfd,
386 (bed->rela_plts_and_copies_p
387 ? ".rela.plt" : ".rel.plt"),
388 flags | SEC_READONLY);
389 if (s == NULL
390 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
391 return FALSE;
392 htab->srelplt = s;
393
394 if (! _bfd_elf_create_got_section (abfd, info))
395 return FALSE;
396
397 if (bed->want_dynbss)
398 {
399 /* The .dynbss section is a place to put symbols which are defined
400 by dynamic objects, are referenced by regular objects, and are
401 not functions. We must allocate space for them in the process
402 image and use a R_*_COPY reloc to tell the dynamic linker to
403 initialize them at run time. The linker script puts the .dynbss
404 section into the .bss section of the final image. */
405 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
406 (SEC_ALLOC | SEC_LINKER_CREATED));
407 if (s == NULL)
408 return FALSE;
409
410 /* The .rel[a].bss section holds copy relocs. This section is not
411 normally needed. We need to create it here, though, so that the
412 linker will map it to an output section. We can't just create it
413 only if we need it, because we will not know whether we need it
414 until we have seen all the input files, and the first time the
415 main linker code calls BFD after examining all the input files
416 (size_dynamic_sections) the input sections have already been
417 mapped to the output sections. If the section turns out not to
418 be needed, we can discard it later. We will never need this
419 section when generating a shared object, since they do not use
420 copy relocs. */
421 if (! info->shared)
422 {
423 s = bfd_make_section_anyway_with_flags (abfd,
424 (bed->rela_plts_and_copies_p
425 ? ".rela.bss" : ".rel.bss"),
426 flags | SEC_READONLY);
427 if (s == NULL
428 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
429 return FALSE;
430 }
431 }
432
433 return TRUE;
434 }
435 \f
436 /* Record a new dynamic symbol. We record the dynamic symbols as we
437 read the input files, since we need to have a list of all of them
438 before we can determine the final sizes of the output sections.
439 Note that we may actually call this function even though we are not
440 going to output any dynamic symbols; in some cases we know that a
441 symbol should be in the dynamic symbol table, but only if there is
442 one. */
443
444 bfd_boolean
445 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
446 struct elf_link_hash_entry *h)
447 {
448 if (h->dynindx == -1)
449 {
450 struct elf_strtab_hash *dynstr;
451 char *p;
452 const char *name;
453 bfd_size_type indx;
454
455 /* XXX: The ABI draft says the linker must turn hidden and
456 internal symbols into STB_LOCAL symbols when producing the
457 DSO. However, if ld.so honors st_other in the dynamic table,
458 this would not be necessary. */
459 switch (ELF_ST_VISIBILITY (h->other))
460 {
461 case STV_INTERNAL:
462 case STV_HIDDEN:
463 if (h->root.type != bfd_link_hash_undefined
464 && h->root.type != bfd_link_hash_undefweak)
465 {
466 h->forced_local = 1;
467 if (!elf_hash_table (info)->is_relocatable_executable)
468 return TRUE;
469 }
470
471 default:
472 break;
473 }
474
475 h->dynindx = elf_hash_table (info)->dynsymcount;
476 ++elf_hash_table (info)->dynsymcount;
477
478 dynstr = elf_hash_table (info)->dynstr;
479 if (dynstr == NULL)
480 {
481 /* Create a strtab to hold the dynamic symbol names. */
482 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
483 if (dynstr == NULL)
484 return FALSE;
485 }
486
487 /* We don't put any version information in the dynamic string
488 table. */
489 name = h->root.root.string;
490 p = strchr (name, ELF_VER_CHR);
491 if (p != NULL)
492 /* We know that the p points into writable memory. In fact,
493 there are only a few symbols that have read-only names, being
494 those like _GLOBAL_OFFSET_TABLE_ that are created specially
495 by the backends. Most symbols will have names pointing into
496 an ELF string table read from a file, or to objalloc memory. */
497 *p = 0;
498
499 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
500
501 if (p != NULL)
502 *p = ELF_VER_CHR;
503
504 if (indx == (bfd_size_type) -1)
505 return FALSE;
506 h->dynstr_index = indx;
507 }
508
509 return TRUE;
510 }
511 \f
512 /* Mark a symbol dynamic. */
513
514 static void
515 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
516 struct elf_link_hash_entry *h,
517 Elf_Internal_Sym *sym)
518 {
519 struct bfd_elf_dynamic_list *d = info->dynamic_list;
520
521 /* It may be called more than once on the same H. */
522 if(h->dynamic || info->relocatable)
523 return;
524
525 if ((info->dynamic_data
526 && (h->type == STT_OBJECT
527 || (sym != NULL
528 && ELF_ST_TYPE (sym->st_info) == STT_OBJECT)))
529 || (d != NULL
530 && h->root.type == bfd_link_hash_new
531 && (*d->match) (&d->head, NULL, h->root.root.string)))
532 h->dynamic = 1;
533 }
534
535 /* Record an assignment to a symbol made by a linker script. We need
536 this in case some dynamic object refers to this symbol. */
537
538 bfd_boolean
539 bfd_elf_record_link_assignment (bfd *output_bfd,
540 struct bfd_link_info *info,
541 const char *name,
542 bfd_boolean provide,
543 bfd_boolean hidden)
544 {
545 struct elf_link_hash_entry *h, *hv;
546 struct elf_link_hash_table *htab;
547 const struct elf_backend_data *bed;
548
549 if (!is_elf_hash_table (info->hash))
550 return TRUE;
551
552 htab = elf_hash_table (info);
553 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
554 if (h == NULL)
555 return provide;
556
557 switch (h->root.type)
558 {
559 case bfd_link_hash_defined:
560 case bfd_link_hash_defweak:
561 case bfd_link_hash_common:
562 break;
563 case bfd_link_hash_undefweak:
564 case bfd_link_hash_undefined:
565 /* Since we're defining the symbol, don't let it seem to have not
566 been defined. record_dynamic_symbol and size_dynamic_sections
567 may depend on this. */
568 h->root.type = bfd_link_hash_new;
569 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
570 bfd_link_repair_undef_list (&htab->root);
571 break;
572 case bfd_link_hash_new:
573 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
574 h->non_elf = 0;
575 break;
576 case bfd_link_hash_indirect:
577 /* We had a versioned symbol in a dynamic library. We make the
578 the versioned symbol point to this one. */
579 bed = get_elf_backend_data (output_bfd);
580 hv = h;
581 while (hv->root.type == bfd_link_hash_indirect
582 || hv->root.type == bfd_link_hash_warning)
583 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
584 /* We don't need to update h->root.u since linker will set them
585 later. */
586 h->root.type = bfd_link_hash_undefined;
587 hv->root.type = bfd_link_hash_indirect;
588 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
589 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
590 break;
591 case bfd_link_hash_warning:
592 abort ();
593 break;
594 }
595
596 /* If this symbol is being provided by the linker script, and it is
597 currently defined by a dynamic object, but not by a regular
598 object, then mark it as undefined so that the generic linker will
599 force the correct value. */
600 if (provide
601 && h->def_dynamic
602 && !h->def_regular)
603 h->root.type = bfd_link_hash_undefined;
604
605 /* If this symbol is not being provided by the linker script, and it is
606 currently defined by a dynamic object, but not by a regular object,
607 then clear out any version information because the symbol will not be
608 associated with the dynamic object any more. */
609 if (!provide
610 && h->def_dynamic
611 && !h->def_regular)
612 h->verinfo.verdef = NULL;
613
614 h->def_regular = 1;
615
616 if (hidden)
617 {
618 bed = get_elf_backend_data (output_bfd);
619 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
620 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
621 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
622 }
623
624 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
625 and executables. */
626 if (!info->relocatable
627 && h->dynindx != -1
628 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
629 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
630 h->forced_local = 1;
631
632 if ((h->def_dynamic
633 || h->ref_dynamic
634 || info->shared
635 || (info->executable && elf_hash_table (info)->is_relocatable_executable))
636 && h->dynindx == -1)
637 {
638 if (! bfd_elf_link_record_dynamic_symbol (info, h))
639 return FALSE;
640
641 /* If this is a weak defined symbol, and we know a corresponding
642 real symbol from the same dynamic object, make sure the real
643 symbol is also made into a dynamic symbol. */
644 if (h->u.weakdef != NULL
645 && h->u.weakdef->dynindx == -1)
646 {
647 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
648 return FALSE;
649 }
650 }
651
652 return TRUE;
653 }
654
655 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
656 success, and 2 on a failure caused by attempting to record a symbol
657 in a discarded section, eg. a discarded link-once section symbol. */
658
659 int
660 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
661 bfd *input_bfd,
662 long input_indx)
663 {
664 bfd_size_type amt;
665 struct elf_link_local_dynamic_entry *entry;
666 struct elf_link_hash_table *eht;
667 struct elf_strtab_hash *dynstr;
668 unsigned long dynstr_index;
669 char *name;
670 Elf_External_Sym_Shndx eshndx;
671 char esym[sizeof (Elf64_External_Sym)];
672
673 if (! is_elf_hash_table (info->hash))
674 return 0;
675
676 /* See if the entry exists already. */
677 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
678 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
679 return 1;
680
681 amt = sizeof (*entry);
682 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
683 if (entry == NULL)
684 return 0;
685
686 /* Go find the symbol, so that we can find it's name. */
687 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
688 1, input_indx, &entry->isym, esym, &eshndx))
689 {
690 bfd_release (input_bfd, entry);
691 return 0;
692 }
693
694 if (entry->isym.st_shndx != SHN_UNDEF
695 && entry->isym.st_shndx < SHN_LORESERVE)
696 {
697 asection *s;
698
699 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
700 if (s == NULL || bfd_is_abs_section (s->output_section))
701 {
702 /* We can still bfd_release here as nothing has done another
703 bfd_alloc. We can't do this later in this function. */
704 bfd_release (input_bfd, entry);
705 return 2;
706 }
707 }
708
709 name = (bfd_elf_string_from_elf_section
710 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
711 entry->isym.st_name));
712
713 dynstr = elf_hash_table (info)->dynstr;
714 if (dynstr == NULL)
715 {
716 /* Create a strtab to hold the dynamic symbol names. */
717 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
718 if (dynstr == NULL)
719 return 0;
720 }
721
722 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
723 if (dynstr_index == (unsigned long) -1)
724 return 0;
725 entry->isym.st_name = dynstr_index;
726
727 eht = elf_hash_table (info);
728
729 entry->next = eht->dynlocal;
730 eht->dynlocal = entry;
731 entry->input_bfd = input_bfd;
732 entry->input_indx = input_indx;
733 eht->dynsymcount++;
734
735 /* Whatever binding the symbol had before, it's now local. */
736 entry->isym.st_info
737 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
738
739 /* The dynindx will be set at the end of size_dynamic_sections. */
740
741 return 1;
742 }
743
744 /* Return the dynindex of a local dynamic symbol. */
745
746 long
747 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
748 bfd *input_bfd,
749 long input_indx)
750 {
751 struct elf_link_local_dynamic_entry *e;
752
753 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
754 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
755 return e->dynindx;
756 return -1;
757 }
758
759 /* This function is used to renumber the dynamic symbols, if some of
760 them are removed because they are marked as local. This is called
761 via elf_link_hash_traverse. */
762
763 static bfd_boolean
764 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
765 void *data)
766 {
767 size_t *count = (size_t *) data;
768
769 if (h->forced_local)
770 return TRUE;
771
772 if (h->dynindx != -1)
773 h->dynindx = ++(*count);
774
775 return TRUE;
776 }
777
778
779 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
780 STB_LOCAL binding. */
781
782 static bfd_boolean
783 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
784 void *data)
785 {
786 size_t *count = (size_t *) data;
787
788 if (!h->forced_local)
789 return TRUE;
790
791 if (h->dynindx != -1)
792 h->dynindx = ++(*count);
793
794 return TRUE;
795 }
796
797 /* Return true if the dynamic symbol for a given section should be
798 omitted when creating a shared library. */
799 bfd_boolean
800 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
801 struct bfd_link_info *info,
802 asection *p)
803 {
804 struct elf_link_hash_table *htab;
805 asection *ip;
806
807 switch (elf_section_data (p)->this_hdr.sh_type)
808 {
809 case SHT_PROGBITS:
810 case SHT_NOBITS:
811 /* If sh_type is yet undecided, assume it could be
812 SHT_PROGBITS/SHT_NOBITS. */
813 case SHT_NULL:
814 htab = elf_hash_table (info);
815 if (p == htab->tls_sec)
816 return FALSE;
817
818 if (htab->text_index_section != NULL)
819 return p != htab->text_index_section && p != htab->data_index_section;
820
821 return (htab->dynobj != NULL
822 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
823 && ip->output_section == p);
824
825 /* There shouldn't be section relative relocations
826 against any other section. */
827 default:
828 return TRUE;
829 }
830 }
831
832 /* Assign dynsym indices. In a shared library we generate a section
833 symbol for each output section, which come first. Next come symbols
834 which have been forced to local binding. Then all of the back-end
835 allocated local dynamic syms, followed by the rest of the global
836 symbols. */
837
838 static unsigned long
839 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
840 struct bfd_link_info *info,
841 unsigned long *section_sym_count)
842 {
843 unsigned long dynsymcount = 0;
844
845 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
846 {
847 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
848 asection *p;
849 for (p = output_bfd->sections; p ; p = p->next)
850 if ((p->flags & SEC_EXCLUDE) == 0
851 && (p->flags & SEC_ALLOC) != 0
852 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
853 elf_section_data (p)->dynindx = ++dynsymcount;
854 else
855 elf_section_data (p)->dynindx = 0;
856 }
857 *section_sym_count = dynsymcount;
858
859 elf_link_hash_traverse (elf_hash_table (info),
860 elf_link_renumber_local_hash_table_dynsyms,
861 &dynsymcount);
862
863 if (elf_hash_table (info)->dynlocal)
864 {
865 struct elf_link_local_dynamic_entry *p;
866 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
867 p->dynindx = ++dynsymcount;
868 }
869
870 elf_link_hash_traverse (elf_hash_table (info),
871 elf_link_renumber_hash_table_dynsyms,
872 &dynsymcount);
873
874 /* There is an unused NULL entry at the head of the table which
875 we must account for in our count. Unless there weren't any
876 symbols, which means we'll have no table at all. */
877 if (dynsymcount != 0)
878 ++dynsymcount;
879
880 elf_hash_table (info)->dynsymcount = dynsymcount;
881 return dynsymcount;
882 }
883
884 /* Merge st_other field. */
885
886 static void
887 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
888 const Elf_Internal_Sym *isym, asection *sec,
889 bfd_boolean definition, bfd_boolean dynamic)
890 {
891 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
892
893 /* If st_other has a processor-specific meaning, specific
894 code might be needed here. */
895 if (bed->elf_backend_merge_symbol_attribute)
896 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
897 dynamic);
898
899 if (!dynamic)
900 {
901 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
902 unsigned hvis = ELF_ST_VISIBILITY (h->other);
903
904 /* Keep the most constraining visibility. Leave the remainder
905 of the st_other field to elf_backend_merge_symbol_attribute. */
906 if (symvis - 1 < hvis - 1)
907 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
908 }
909 else if (definition
910 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
911 && (sec->flags & SEC_READONLY) == 0)
912 h->protected_def = 1;
913 }
914
915 /* This function is called when we want to merge a new symbol with an
916 existing symbol. It handles the various cases which arise when we
917 find a definition in a dynamic object, or when there is already a
918 definition in a dynamic object. The new symbol is described by
919 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
920 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
921 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
922 of an old common symbol. We set OVERRIDE if the old symbol is
923 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
924 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
925 to change. By OK to change, we mean that we shouldn't warn if the
926 type or size does change. */
927
928 static bfd_boolean
929 _bfd_elf_merge_symbol (bfd *abfd,
930 struct bfd_link_info *info,
931 const char *name,
932 Elf_Internal_Sym *sym,
933 asection **psec,
934 bfd_vma *pvalue,
935 struct elf_link_hash_entry **sym_hash,
936 bfd **poldbfd,
937 bfd_boolean *pold_weak,
938 unsigned int *pold_alignment,
939 bfd_boolean *skip,
940 bfd_boolean *override,
941 bfd_boolean *type_change_ok,
942 bfd_boolean *size_change_ok)
943 {
944 asection *sec, *oldsec;
945 struct elf_link_hash_entry *h;
946 struct elf_link_hash_entry *hi;
947 struct elf_link_hash_entry *flip;
948 int bind;
949 bfd *oldbfd;
950 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
951 bfd_boolean newweak, oldweak, newfunc, oldfunc;
952 const struct elf_backend_data *bed;
953
954 *skip = FALSE;
955 *override = FALSE;
956
957 sec = *psec;
958 bind = ELF_ST_BIND (sym->st_info);
959
960 if (! bfd_is_und_section (sec))
961 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
962 else
963 h = ((struct elf_link_hash_entry *)
964 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
965 if (h == NULL)
966 return FALSE;
967 *sym_hash = h;
968
969 bed = get_elf_backend_data (abfd);
970
971 /* For merging, we only care about real symbols. But we need to make
972 sure that indirect symbol dynamic flags are updated. */
973 hi = h;
974 while (h->root.type == bfd_link_hash_indirect
975 || h->root.type == bfd_link_hash_warning)
976 h = (struct elf_link_hash_entry *) h->root.u.i.link;
977
978 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
979 existing symbol. */
980
981 oldbfd = NULL;
982 oldsec = NULL;
983 switch (h->root.type)
984 {
985 default:
986 break;
987
988 case bfd_link_hash_undefined:
989 case bfd_link_hash_undefweak:
990 oldbfd = h->root.u.undef.abfd;
991 break;
992
993 case bfd_link_hash_defined:
994 case bfd_link_hash_defweak:
995 oldbfd = h->root.u.def.section->owner;
996 oldsec = h->root.u.def.section;
997 break;
998
999 case bfd_link_hash_common:
1000 oldbfd = h->root.u.c.p->section->owner;
1001 oldsec = h->root.u.c.p->section;
1002 if (pold_alignment)
1003 *pold_alignment = h->root.u.c.p->alignment_power;
1004 break;
1005 }
1006 if (poldbfd && *poldbfd == NULL)
1007 *poldbfd = oldbfd;
1008
1009 /* Differentiate strong and weak symbols. */
1010 newweak = bind == STB_WEAK;
1011 oldweak = (h->root.type == bfd_link_hash_defweak
1012 || h->root.type == bfd_link_hash_undefweak);
1013 if (pold_weak)
1014 *pold_weak = oldweak;
1015
1016 /* This code is for coping with dynamic objects, and is only useful
1017 if we are doing an ELF link. */
1018 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1019 return TRUE;
1020
1021 /* We have to check it for every instance since the first few may be
1022 references and not all compilers emit symbol type for undefined
1023 symbols. */
1024 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1025
1026 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1027 respectively, is from a dynamic object. */
1028
1029 newdyn = (abfd->flags & DYNAMIC) != 0;
1030
1031 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1032 syms and defined syms in dynamic libraries respectively.
1033 ref_dynamic on the other hand can be set for a symbol defined in
1034 a dynamic library, and def_dynamic may not be set; When the
1035 definition in a dynamic lib is overridden by a definition in the
1036 executable use of the symbol in the dynamic lib becomes a
1037 reference to the executable symbol. */
1038 if (newdyn)
1039 {
1040 if (bfd_is_und_section (sec))
1041 {
1042 if (bind != STB_WEAK)
1043 {
1044 h->ref_dynamic_nonweak = 1;
1045 hi->ref_dynamic_nonweak = 1;
1046 }
1047 }
1048 else
1049 {
1050 h->dynamic_def = 1;
1051 hi->dynamic_def = 1;
1052 }
1053 }
1054
1055 /* If we just created the symbol, mark it as being an ELF symbol.
1056 Other than that, there is nothing to do--there is no merge issue
1057 with a newly defined symbol--so we just return. */
1058
1059 if (h->root.type == bfd_link_hash_new)
1060 {
1061 h->non_elf = 0;
1062 return TRUE;
1063 }
1064
1065 /* In cases involving weak versioned symbols, we may wind up trying
1066 to merge a symbol with itself. Catch that here, to avoid the
1067 confusion that results if we try to override a symbol with
1068 itself. The additional tests catch cases like
1069 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1070 dynamic object, which we do want to handle here. */
1071 if (abfd == oldbfd
1072 && (newweak || oldweak)
1073 && ((abfd->flags & DYNAMIC) == 0
1074 || !h->def_regular))
1075 return TRUE;
1076
1077 olddyn = FALSE;
1078 if (oldbfd != NULL)
1079 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1080 else if (oldsec != NULL)
1081 {
1082 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1083 indices used by MIPS ELF. */
1084 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1085 }
1086
1087 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1088 respectively, appear to be a definition rather than reference. */
1089
1090 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1091
1092 olddef = (h->root.type != bfd_link_hash_undefined
1093 && h->root.type != bfd_link_hash_undefweak
1094 && h->root.type != bfd_link_hash_common);
1095
1096 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1097 respectively, appear to be a function. */
1098
1099 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1100 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1101
1102 oldfunc = (h->type != STT_NOTYPE
1103 && bed->is_function_type (h->type));
1104
1105 /* When we try to create a default indirect symbol from the dynamic
1106 definition with the default version, we skip it if its type and
1107 the type of existing regular definition mismatch. */
1108 if (pold_alignment == NULL
1109 && newdyn
1110 && newdef
1111 && !olddyn
1112 && (((olddef || h->root.type == bfd_link_hash_common)
1113 && ELF_ST_TYPE (sym->st_info) != h->type
1114 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1115 && h->type != STT_NOTYPE
1116 && !(newfunc && oldfunc))
1117 || (olddef
1118 && ((h->type == STT_GNU_IFUNC)
1119 != (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))))
1120 {
1121 *skip = TRUE;
1122 return TRUE;
1123 }
1124
1125 /* Check TLS symbols. We don't check undefined symbols introduced
1126 by "ld -u" which have no type (and oldbfd NULL), and we don't
1127 check symbols from plugins because they also have no type. */
1128 if (oldbfd != NULL
1129 && (oldbfd->flags & BFD_PLUGIN) == 0
1130 && (abfd->flags & BFD_PLUGIN) == 0
1131 && ELF_ST_TYPE (sym->st_info) != h->type
1132 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1133 {
1134 bfd *ntbfd, *tbfd;
1135 bfd_boolean ntdef, tdef;
1136 asection *ntsec, *tsec;
1137
1138 if (h->type == STT_TLS)
1139 {
1140 ntbfd = abfd;
1141 ntsec = sec;
1142 ntdef = newdef;
1143 tbfd = oldbfd;
1144 tsec = oldsec;
1145 tdef = olddef;
1146 }
1147 else
1148 {
1149 ntbfd = oldbfd;
1150 ntsec = oldsec;
1151 ntdef = olddef;
1152 tbfd = abfd;
1153 tsec = sec;
1154 tdef = newdef;
1155 }
1156
1157 if (tdef && ntdef)
1158 (*_bfd_error_handler)
1159 (_("%s: TLS definition in %B section %A "
1160 "mismatches non-TLS definition in %B section %A"),
1161 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1162 else if (!tdef && !ntdef)
1163 (*_bfd_error_handler)
1164 (_("%s: TLS reference in %B "
1165 "mismatches non-TLS reference in %B"),
1166 tbfd, ntbfd, h->root.root.string);
1167 else if (tdef)
1168 (*_bfd_error_handler)
1169 (_("%s: TLS definition in %B section %A "
1170 "mismatches non-TLS reference in %B"),
1171 tbfd, tsec, ntbfd, h->root.root.string);
1172 else
1173 (*_bfd_error_handler)
1174 (_("%s: TLS reference in %B "
1175 "mismatches non-TLS definition in %B section %A"),
1176 tbfd, ntbfd, ntsec, h->root.root.string);
1177
1178 bfd_set_error (bfd_error_bad_value);
1179 return FALSE;
1180 }
1181
1182 /* If the old symbol has non-default visibility, we ignore the new
1183 definition from a dynamic object. */
1184 if (newdyn
1185 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1186 && !bfd_is_und_section (sec))
1187 {
1188 *skip = TRUE;
1189 /* Make sure this symbol is dynamic. */
1190 h->ref_dynamic = 1;
1191 hi->ref_dynamic = 1;
1192 /* A protected symbol has external availability. Make sure it is
1193 recorded as dynamic.
1194
1195 FIXME: Should we check type and size for protected symbol? */
1196 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1197 return bfd_elf_link_record_dynamic_symbol (info, h);
1198 else
1199 return TRUE;
1200 }
1201 else if (!newdyn
1202 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1203 && h->def_dynamic)
1204 {
1205 /* If the new symbol with non-default visibility comes from a
1206 relocatable file and the old definition comes from a dynamic
1207 object, we remove the old definition. */
1208 if (hi->root.type == bfd_link_hash_indirect)
1209 {
1210 /* Handle the case where the old dynamic definition is
1211 default versioned. We need to copy the symbol info from
1212 the symbol with default version to the normal one if it
1213 was referenced before. */
1214 if (h->ref_regular)
1215 {
1216 hi->root.type = h->root.type;
1217 h->root.type = bfd_link_hash_indirect;
1218 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1219
1220 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1221 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1222 {
1223 /* If the new symbol is hidden or internal, completely undo
1224 any dynamic link state. */
1225 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1226 h->forced_local = 0;
1227 h->ref_dynamic = 0;
1228 }
1229 else
1230 h->ref_dynamic = 1;
1231
1232 h->def_dynamic = 0;
1233 /* FIXME: Should we check type and size for protected symbol? */
1234 h->size = 0;
1235 h->type = 0;
1236
1237 h = hi;
1238 }
1239 else
1240 h = hi;
1241 }
1242
1243 /* If the old symbol was undefined before, then it will still be
1244 on the undefs list. If the new symbol is undefined or
1245 common, we can't make it bfd_link_hash_new here, because new
1246 undefined or common symbols will be added to the undefs list
1247 by _bfd_generic_link_add_one_symbol. Symbols may not be
1248 added twice to the undefs list. Also, if the new symbol is
1249 undefweak then we don't want to lose the strong undef. */
1250 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1251 {
1252 h->root.type = bfd_link_hash_undefined;
1253 h->root.u.undef.abfd = abfd;
1254 }
1255 else
1256 {
1257 h->root.type = bfd_link_hash_new;
1258 h->root.u.undef.abfd = NULL;
1259 }
1260
1261 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1262 {
1263 /* If the new symbol is hidden or internal, completely undo
1264 any dynamic link state. */
1265 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1266 h->forced_local = 0;
1267 h->ref_dynamic = 0;
1268 }
1269 else
1270 h->ref_dynamic = 1;
1271 h->def_dynamic = 0;
1272 /* FIXME: Should we check type and size for protected symbol? */
1273 h->size = 0;
1274 h->type = 0;
1275 return TRUE;
1276 }
1277
1278 /* If a new weak symbol definition comes from a regular file and the
1279 old symbol comes from a dynamic library, we treat the new one as
1280 strong. Similarly, an old weak symbol definition from a regular
1281 file is treated as strong when the new symbol comes from a dynamic
1282 library. Further, an old weak symbol from a dynamic library is
1283 treated as strong if the new symbol is from a dynamic library.
1284 This reflects the way glibc's ld.so works.
1285
1286 Do this before setting *type_change_ok or *size_change_ok so that
1287 we warn properly when dynamic library symbols are overridden. */
1288
1289 if (newdef && !newdyn && olddyn)
1290 newweak = FALSE;
1291 if (olddef && newdyn)
1292 oldweak = FALSE;
1293
1294 /* Allow changes between different types of function symbol. */
1295 if (newfunc && oldfunc)
1296 *type_change_ok = TRUE;
1297
1298 /* It's OK to change the type if either the existing symbol or the
1299 new symbol is weak. A type change is also OK if the old symbol
1300 is undefined and the new symbol is defined. */
1301
1302 if (oldweak
1303 || newweak
1304 || (newdef
1305 && h->root.type == bfd_link_hash_undefined))
1306 *type_change_ok = TRUE;
1307
1308 /* It's OK to change the size if either the existing symbol or the
1309 new symbol is weak, or if the old symbol is undefined. */
1310
1311 if (*type_change_ok
1312 || h->root.type == bfd_link_hash_undefined)
1313 *size_change_ok = TRUE;
1314
1315 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1316 symbol, respectively, appears to be a common symbol in a dynamic
1317 object. If a symbol appears in an uninitialized section, and is
1318 not weak, and is not a function, then it may be a common symbol
1319 which was resolved when the dynamic object was created. We want
1320 to treat such symbols specially, because they raise special
1321 considerations when setting the symbol size: if the symbol
1322 appears as a common symbol in a regular object, and the size in
1323 the regular object is larger, we must make sure that we use the
1324 larger size. This problematic case can always be avoided in C,
1325 but it must be handled correctly when using Fortran shared
1326 libraries.
1327
1328 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1329 likewise for OLDDYNCOMMON and OLDDEF.
1330
1331 Note that this test is just a heuristic, and that it is quite
1332 possible to have an uninitialized symbol in a shared object which
1333 is really a definition, rather than a common symbol. This could
1334 lead to some minor confusion when the symbol really is a common
1335 symbol in some regular object. However, I think it will be
1336 harmless. */
1337
1338 if (newdyn
1339 && newdef
1340 && !newweak
1341 && (sec->flags & SEC_ALLOC) != 0
1342 && (sec->flags & SEC_LOAD) == 0
1343 && sym->st_size > 0
1344 && !newfunc)
1345 newdyncommon = TRUE;
1346 else
1347 newdyncommon = FALSE;
1348
1349 if (olddyn
1350 && olddef
1351 && h->root.type == bfd_link_hash_defined
1352 && h->def_dynamic
1353 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1354 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1355 && h->size > 0
1356 && !oldfunc)
1357 olddyncommon = TRUE;
1358 else
1359 olddyncommon = FALSE;
1360
1361 /* We now know everything about the old and new symbols. We ask the
1362 backend to check if we can merge them. */
1363 if (bed->merge_symbol != NULL)
1364 {
1365 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1366 return FALSE;
1367 sec = *psec;
1368 }
1369
1370 /* If both the old and the new symbols look like common symbols in a
1371 dynamic object, set the size of the symbol to the larger of the
1372 two. */
1373
1374 if (olddyncommon
1375 && newdyncommon
1376 && sym->st_size != h->size)
1377 {
1378 /* Since we think we have two common symbols, issue a multiple
1379 common warning if desired. Note that we only warn if the
1380 size is different. If the size is the same, we simply let
1381 the old symbol override the new one as normally happens with
1382 symbols defined in dynamic objects. */
1383
1384 if (! ((*info->callbacks->multiple_common)
1385 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
1386 return FALSE;
1387
1388 if (sym->st_size > h->size)
1389 h->size = sym->st_size;
1390
1391 *size_change_ok = TRUE;
1392 }
1393
1394 /* If we are looking at a dynamic object, and we have found a
1395 definition, we need to see if the symbol was already defined by
1396 some other object. If so, we want to use the existing
1397 definition, and we do not want to report a multiple symbol
1398 definition error; we do this by clobbering *PSEC to be
1399 bfd_und_section_ptr.
1400
1401 We treat a common symbol as a definition if the symbol in the
1402 shared library is a function, since common symbols always
1403 represent variables; this can cause confusion in principle, but
1404 any such confusion would seem to indicate an erroneous program or
1405 shared library. We also permit a common symbol in a regular
1406 object to override a weak symbol in a shared object. */
1407
1408 if (newdyn
1409 && newdef
1410 && (olddef
1411 || (h->root.type == bfd_link_hash_common
1412 && (newweak || newfunc))))
1413 {
1414 *override = TRUE;
1415 newdef = FALSE;
1416 newdyncommon = FALSE;
1417
1418 *psec = sec = bfd_und_section_ptr;
1419 *size_change_ok = TRUE;
1420
1421 /* If we get here when the old symbol is a common symbol, then
1422 we are explicitly letting it override a weak symbol or
1423 function in a dynamic object, and we don't want to warn about
1424 a type change. If the old symbol is a defined symbol, a type
1425 change warning may still be appropriate. */
1426
1427 if (h->root.type == bfd_link_hash_common)
1428 *type_change_ok = TRUE;
1429 }
1430
1431 /* Handle the special case of an old common symbol merging with a
1432 new symbol which looks like a common symbol in a shared object.
1433 We change *PSEC and *PVALUE to make the new symbol look like a
1434 common symbol, and let _bfd_generic_link_add_one_symbol do the
1435 right thing. */
1436
1437 if (newdyncommon
1438 && h->root.type == bfd_link_hash_common)
1439 {
1440 *override = TRUE;
1441 newdef = FALSE;
1442 newdyncommon = FALSE;
1443 *pvalue = sym->st_size;
1444 *psec = sec = bed->common_section (oldsec);
1445 *size_change_ok = TRUE;
1446 }
1447
1448 /* Skip weak definitions of symbols that are already defined. */
1449 if (newdef && olddef && newweak)
1450 {
1451 /* Don't skip new non-IR weak syms. */
1452 if (!(oldbfd != NULL
1453 && (oldbfd->flags & BFD_PLUGIN) != 0
1454 && (abfd->flags & BFD_PLUGIN) == 0))
1455 {
1456 newdef = FALSE;
1457 *skip = TRUE;
1458 }
1459
1460 /* Merge st_other. If the symbol already has a dynamic index,
1461 but visibility says it should not be visible, turn it into a
1462 local symbol. */
1463 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1464 if (h->dynindx != -1)
1465 switch (ELF_ST_VISIBILITY (h->other))
1466 {
1467 case STV_INTERNAL:
1468 case STV_HIDDEN:
1469 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1470 break;
1471 }
1472 }
1473
1474 /* If the old symbol is from a dynamic object, and the new symbol is
1475 a definition which is not from a dynamic object, then the new
1476 symbol overrides the old symbol. Symbols from regular files
1477 always take precedence over symbols from dynamic objects, even if
1478 they are defined after the dynamic object in the link.
1479
1480 As above, we again permit a common symbol in a regular object to
1481 override a definition in a shared object if the shared object
1482 symbol is a function or is weak. */
1483
1484 flip = NULL;
1485 if (!newdyn
1486 && (newdef
1487 || (bfd_is_com_section (sec)
1488 && (oldweak || oldfunc)))
1489 && olddyn
1490 && olddef
1491 && h->def_dynamic)
1492 {
1493 /* Change the hash table entry to undefined, and let
1494 _bfd_generic_link_add_one_symbol do the right thing with the
1495 new definition. */
1496
1497 h->root.type = bfd_link_hash_undefined;
1498 h->root.u.undef.abfd = h->root.u.def.section->owner;
1499 *size_change_ok = TRUE;
1500
1501 olddef = FALSE;
1502 olddyncommon = FALSE;
1503
1504 /* We again permit a type change when a common symbol may be
1505 overriding a function. */
1506
1507 if (bfd_is_com_section (sec))
1508 {
1509 if (oldfunc)
1510 {
1511 /* If a common symbol overrides a function, make sure
1512 that it isn't defined dynamically nor has type
1513 function. */
1514 h->def_dynamic = 0;
1515 h->type = STT_NOTYPE;
1516 }
1517 *type_change_ok = TRUE;
1518 }
1519
1520 if (hi->root.type == bfd_link_hash_indirect)
1521 flip = hi;
1522 else
1523 /* This union may have been set to be non-NULL when this symbol
1524 was seen in a dynamic object. We must force the union to be
1525 NULL, so that it is correct for a regular symbol. */
1526 h->verinfo.vertree = NULL;
1527 }
1528
1529 /* Handle the special case of a new common symbol merging with an
1530 old symbol that looks like it might be a common symbol defined in
1531 a shared object. Note that we have already handled the case in
1532 which a new common symbol should simply override the definition
1533 in the shared library. */
1534
1535 if (! newdyn
1536 && bfd_is_com_section (sec)
1537 && olddyncommon)
1538 {
1539 /* It would be best if we could set the hash table entry to a
1540 common symbol, but we don't know what to use for the section
1541 or the alignment. */
1542 if (! ((*info->callbacks->multiple_common)
1543 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
1544 return FALSE;
1545
1546 /* If the presumed common symbol in the dynamic object is
1547 larger, pretend that the new symbol has its size. */
1548
1549 if (h->size > *pvalue)
1550 *pvalue = h->size;
1551
1552 /* We need to remember the alignment required by the symbol
1553 in the dynamic object. */
1554 BFD_ASSERT (pold_alignment);
1555 *pold_alignment = h->root.u.def.section->alignment_power;
1556
1557 olddef = FALSE;
1558 olddyncommon = FALSE;
1559
1560 h->root.type = bfd_link_hash_undefined;
1561 h->root.u.undef.abfd = h->root.u.def.section->owner;
1562
1563 *size_change_ok = TRUE;
1564 *type_change_ok = TRUE;
1565
1566 if (hi->root.type == bfd_link_hash_indirect)
1567 flip = hi;
1568 else
1569 h->verinfo.vertree = NULL;
1570 }
1571
1572 if (flip != NULL)
1573 {
1574 /* Handle the case where we had a versioned symbol in a dynamic
1575 library and now find a definition in a normal object. In this
1576 case, we make the versioned symbol point to the normal one. */
1577 flip->root.type = h->root.type;
1578 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1579 h->root.type = bfd_link_hash_indirect;
1580 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1581 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1582 if (h->def_dynamic)
1583 {
1584 h->def_dynamic = 0;
1585 flip->ref_dynamic = 1;
1586 }
1587 }
1588
1589 return TRUE;
1590 }
1591
1592 /* This function is called to create an indirect symbol from the
1593 default for the symbol with the default version if needed. The
1594 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1595 set DYNSYM if the new indirect symbol is dynamic. */
1596
1597 static bfd_boolean
1598 _bfd_elf_add_default_symbol (bfd *abfd,
1599 struct bfd_link_info *info,
1600 struct elf_link_hash_entry *h,
1601 const char *name,
1602 Elf_Internal_Sym *sym,
1603 asection *sec,
1604 bfd_vma value,
1605 bfd **poldbfd,
1606 bfd_boolean *dynsym)
1607 {
1608 bfd_boolean type_change_ok;
1609 bfd_boolean size_change_ok;
1610 bfd_boolean skip;
1611 char *shortname;
1612 struct elf_link_hash_entry *hi;
1613 struct bfd_link_hash_entry *bh;
1614 const struct elf_backend_data *bed;
1615 bfd_boolean collect;
1616 bfd_boolean dynamic;
1617 bfd_boolean override;
1618 char *p;
1619 size_t len, shortlen;
1620 asection *tmp_sec;
1621
1622 /* If this symbol has a version, and it is the default version, we
1623 create an indirect symbol from the default name to the fully
1624 decorated name. This will cause external references which do not
1625 specify a version to be bound to this version of the symbol. */
1626 p = strchr (name, ELF_VER_CHR);
1627 if (p == NULL || p[1] != ELF_VER_CHR)
1628 return TRUE;
1629
1630 bed = get_elf_backend_data (abfd);
1631 collect = bed->collect;
1632 dynamic = (abfd->flags & DYNAMIC) != 0;
1633
1634 shortlen = p - name;
1635 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1636 if (shortname == NULL)
1637 return FALSE;
1638 memcpy (shortname, name, shortlen);
1639 shortname[shortlen] = '\0';
1640
1641 /* We are going to create a new symbol. Merge it with any existing
1642 symbol with this name. For the purposes of the merge, act as
1643 though we were defining the symbol we just defined, although we
1644 actually going to define an indirect symbol. */
1645 type_change_ok = FALSE;
1646 size_change_ok = FALSE;
1647 tmp_sec = sec;
1648 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1649 &hi, poldbfd, NULL, NULL, &skip, &override,
1650 &type_change_ok, &size_change_ok))
1651 return FALSE;
1652
1653 if (skip)
1654 goto nondefault;
1655
1656 if (! override)
1657 {
1658 /* Add the default symbol if not performing a relocatable link. */
1659 if (! info->relocatable)
1660 {
1661 bh = &hi->root;
1662 if (! (_bfd_generic_link_add_one_symbol
1663 (info, abfd, shortname, BSF_INDIRECT,
1664 bfd_ind_section_ptr,
1665 0, name, FALSE, collect, &bh)))
1666 return FALSE;
1667 hi = (struct elf_link_hash_entry *) bh;
1668 }
1669 }
1670 else
1671 {
1672 /* In this case the symbol named SHORTNAME is overriding the
1673 indirect symbol we want to add. We were planning on making
1674 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1675 is the name without a version. NAME is the fully versioned
1676 name, and it is the default version.
1677
1678 Overriding means that we already saw a definition for the
1679 symbol SHORTNAME in a regular object, and it is overriding
1680 the symbol defined in the dynamic object.
1681
1682 When this happens, we actually want to change NAME, the
1683 symbol we just added, to refer to SHORTNAME. This will cause
1684 references to NAME in the shared object to become references
1685 to SHORTNAME in the regular object. This is what we expect
1686 when we override a function in a shared object: that the
1687 references in the shared object will be mapped to the
1688 definition in the regular object. */
1689
1690 while (hi->root.type == bfd_link_hash_indirect
1691 || hi->root.type == bfd_link_hash_warning)
1692 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1693
1694 h->root.type = bfd_link_hash_indirect;
1695 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1696 if (h->def_dynamic)
1697 {
1698 h->def_dynamic = 0;
1699 hi->ref_dynamic = 1;
1700 if (hi->ref_regular
1701 || hi->def_regular)
1702 {
1703 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1704 return FALSE;
1705 }
1706 }
1707
1708 /* Now set HI to H, so that the following code will set the
1709 other fields correctly. */
1710 hi = h;
1711 }
1712
1713 /* Check if HI is a warning symbol. */
1714 if (hi->root.type == bfd_link_hash_warning)
1715 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1716
1717 /* If there is a duplicate definition somewhere, then HI may not
1718 point to an indirect symbol. We will have reported an error to
1719 the user in that case. */
1720
1721 if (hi->root.type == bfd_link_hash_indirect)
1722 {
1723 struct elf_link_hash_entry *ht;
1724
1725 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1726 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1727
1728 /* A reference to the SHORTNAME symbol from a dynamic library
1729 will be satisfied by the versioned symbol at runtime. In
1730 effect, we have a reference to the versioned symbol. */
1731 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1732 hi->dynamic_def |= ht->dynamic_def;
1733
1734 /* See if the new flags lead us to realize that the symbol must
1735 be dynamic. */
1736 if (! *dynsym)
1737 {
1738 if (! dynamic)
1739 {
1740 if (! info->executable
1741 || hi->def_dynamic
1742 || hi->ref_dynamic)
1743 *dynsym = TRUE;
1744 }
1745 else
1746 {
1747 if (hi->ref_regular)
1748 *dynsym = TRUE;
1749 }
1750 }
1751 }
1752
1753 /* We also need to define an indirection from the nondefault version
1754 of the symbol. */
1755
1756 nondefault:
1757 len = strlen (name);
1758 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
1759 if (shortname == NULL)
1760 return FALSE;
1761 memcpy (shortname, name, shortlen);
1762 memcpy (shortname + shortlen, p + 1, len - shortlen);
1763
1764 /* Once again, merge with any existing symbol. */
1765 type_change_ok = FALSE;
1766 size_change_ok = FALSE;
1767 tmp_sec = sec;
1768 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1769 &hi, poldbfd, NULL, NULL, &skip, &override,
1770 &type_change_ok, &size_change_ok))
1771 return FALSE;
1772
1773 if (skip)
1774 return TRUE;
1775
1776 if (override)
1777 {
1778 /* Here SHORTNAME is a versioned name, so we don't expect to see
1779 the type of override we do in the case above unless it is
1780 overridden by a versioned definition. */
1781 if (hi->root.type != bfd_link_hash_defined
1782 && hi->root.type != bfd_link_hash_defweak)
1783 (*_bfd_error_handler)
1784 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1785 abfd, shortname);
1786 }
1787 else
1788 {
1789 bh = &hi->root;
1790 if (! (_bfd_generic_link_add_one_symbol
1791 (info, abfd, shortname, BSF_INDIRECT,
1792 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1793 return FALSE;
1794 hi = (struct elf_link_hash_entry *) bh;
1795
1796 /* If there is a duplicate definition somewhere, then HI may not
1797 point to an indirect symbol. We will have reported an error
1798 to the user in that case. */
1799
1800 if (hi->root.type == bfd_link_hash_indirect)
1801 {
1802 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1803 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1804 hi->dynamic_def |= h->dynamic_def;
1805
1806 /* See if the new flags lead us to realize that the symbol
1807 must be dynamic. */
1808 if (! *dynsym)
1809 {
1810 if (! dynamic)
1811 {
1812 if (! info->executable
1813 || hi->ref_dynamic)
1814 *dynsym = TRUE;
1815 }
1816 else
1817 {
1818 if (hi->ref_regular)
1819 *dynsym = TRUE;
1820 }
1821 }
1822 }
1823 }
1824
1825 return TRUE;
1826 }
1827 \f
1828 /* This routine is used to export all defined symbols into the dynamic
1829 symbol table. It is called via elf_link_hash_traverse. */
1830
1831 static bfd_boolean
1832 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1833 {
1834 struct elf_info_failed *eif = (struct elf_info_failed *) data;
1835
1836 /* Ignore indirect symbols. These are added by the versioning code. */
1837 if (h->root.type == bfd_link_hash_indirect)
1838 return TRUE;
1839
1840 /* Ignore this if we won't export it. */
1841 if (!eif->info->export_dynamic && !h->dynamic)
1842 return TRUE;
1843
1844 if (h->dynindx == -1
1845 && (h->def_regular || h->ref_regular)
1846 && ! bfd_hide_sym_by_version (eif->info->version_info,
1847 h->root.root.string))
1848 {
1849 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1850 {
1851 eif->failed = TRUE;
1852 return FALSE;
1853 }
1854 }
1855
1856 return TRUE;
1857 }
1858 \f
1859 /* Look through the symbols which are defined in other shared
1860 libraries and referenced here. Update the list of version
1861 dependencies. This will be put into the .gnu.version_r section.
1862 This function is called via elf_link_hash_traverse. */
1863
1864 static bfd_boolean
1865 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1866 void *data)
1867 {
1868 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
1869 Elf_Internal_Verneed *t;
1870 Elf_Internal_Vernaux *a;
1871 bfd_size_type amt;
1872
1873 /* We only care about symbols defined in shared objects with version
1874 information. */
1875 if (!h->def_dynamic
1876 || h->def_regular
1877 || h->dynindx == -1
1878 || h->verinfo.verdef == NULL
1879 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
1880 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
1881 return TRUE;
1882
1883 /* See if we already know about this version. */
1884 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
1885 t != NULL;
1886 t = t->vn_nextref)
1887 {
1888 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1889 continue;
1890
1891 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1892 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1893 return TRUE;
1894
1895 break;
1896 }
1897
1898 /* This is a new version. Add it to tree we are building. */
1899
1900 if (t == NULL)
1901 {
1902 amt = sizeof *t;
1903 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
1904 if (t == NULL)
1905 {
1906 rinfo->failed = TRUE;
1907 return FALSE;
1908 }
1909
1910 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1911 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
1912 elf_tdata (rinfo->info->output_bfd)->verref = t;
1913 }
1914
1915 amt = sizeof *a;
1916 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
1917 if (a == NULL)
1918 {
1919 rinfo->failed = TRUE;
1920 return FALSE;
1921 }
1922
1923 /* Note that we are copying a string pointer here, and testing it
1924 above. If bfd_elf_string_from_elf_section is ever changed to
1925 discard the string data when low in memory, this will have to be
1926 fixed. */
1927 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1928
1929 a->vna_flags = h->verinfo.verdef->vd_flags;
1930 a->vna_nextptr = t->vn_auxptr;
1931
1932 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1933 ++rinfo->vers;
1934
1935 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1936
1937 t->vn_auxptr = a;
1938
1939 return TRUE;
1940 }
1941
1942 /* Figure out appropriate versions for all the symbols. We may not
1943 have the version number script until we have read all of the input
1944 files, so until that point we don't know which symbols should be
1945 local. This function is called via elf_link_hash_traverse. */
1946
1947 static bfd_boolean
1948 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1949 {
1950 struct elf_info_failed *sinfo;
1951 struct bfd_link_info *info;
1952 const struct elf_backend_data *bed;
1953 struct elf_info_failed eif;
1954 char *p;
1955 bfd_size_type amt;
1956
1957 sinfo = (struct elf_info_failed *) data;
1958 info = sinfo->info;
1959
1960 /* Fix the symbol flags. */
1961 eif.failed = FALSE;
1962 eif.info = info;
1963 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1964 {
1965 if (eif.failed)
1966 sinfo->failed = TRUE;
1967 return FALSE;
1968 }
1969
1970 /* We only need version numbers for symbols defined in regular
1971 objects. */
1972 if (!h->def_regular)
1973 return TRUE;
1974
1975 bed = get_elf_backend_data (info->output_bfd);
1976 p = strchr (h->root.root.string, ELF_VER_CHR);
1977 if (p != NULL && h->verinfo.vertree == NULL)
1978 {
1979 struct bfd_elf_version_tree *t;
1980 bfd_boolean hidden;
1981
1982 hidden = TRUE;
1983
1984 /* There are two consecutive ELF_VER_CHR characters if this is
1985 not a hidden symbol. */
1986 ++p;
1987 if (*p == ELF_VER_CHR)
1988 {
1989 hidden = FALSE;
1990 ++p;
1991 }
1992
1993 /* If there is no version string, we can just return out. */
1994 if (*p == '\0')
1995 {
1996 if (hidden)
1997 h->hidden = 1;
1998 return TRUE;
1999 }
2000
2001 /* Look for the version. If we find it, it is no longer weak. */
2002 for (t = sinfo->info->version_info; t != NULL; t = t->next)
2003 {
2004 if (strcmp (t->name, p) == 0)
2005 {
2006 size_t len;
2007 char *alc;
2008 struct bfd_elf_version_expr *d;
2009
2010 len = p - h->root.root.string;
2011 alc = (char *) bfd_malloc (len);
2012 if (alc == NULL)
2013 {
2014 sinfo->failed = TRUE;
2015 return FALSE;
2016 }
2017 memcpy (alc, h->root.root.string, len - 1);
2018 alc[len - 1] = '\0';
2019 if (alc[len - 2] == ELF_VER_CHR)
2020 alc[len - 2] = '\0';
2021
2022 h->verinfo.vertree = t;
2023 t->used = TRUE;
2024 d = NULL;
2025
2026 if (t->globals.list != NULL)
2027 d = (*t->match) (&t->globals, NULL, alc);
2028
2029 /* See if there is anything to force this symbol to
2030 local scope. */
2031 if (d == NULL && t->locals.list != NULL)
2032 {
2033 d = (*t->match) (&t->locals, NULL, alc);
2034 if (d != NULL
2035 && h->dynindx != -1
2036 && ! info->export_dynamic)
2037 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2038 }
2039
2040 free (alc);
2041 break;
2042 }
2043 }
2044
2045 /* If we are building an application, we need to create a
2046 version node for this version. */
2047 if (t == NULL && info->executable)
2048 {
2049 struct bfd_elf_version_tree **pp;
2050 int version_index;
2051
2052 /* If we aren't going to export this symbol, we don't need
2053 to worry about it. */
2054 if (h->dynindx == -1)
2055 return TRUE;
2056
2057 amt = sizeof *t;
2058 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, amt);
2059 if (t == NULL)
2060 {
2061 sinfo->failed = TRUE;
2062 return FALSE;
2063 }
2064
2065 t->name = p;
2066 t->name_indx = (unsigned int) -1;
2067 t->used = TRUE;
2068
2069 version_index = 1;
2070 /* Don't count anonymous version tag. */
2071 if (sinfo->info->version_info != NULL
2072 && sinfo->info->version_info->vernum == 0)
2073 version_index = 0;
2074 for (pp = &sinfo->info->version_info;
2075 *pp != NULL;
2076 pp = &(*pp)->next)
2077 ++version_index;
2078 t->vernum = version_index;
2079
2080 *pp = t;
2081
2082 h->verinfo.vertree = t;
2083 }
2084 else if (t == NULL)
2085 {
2086 /* We could not find the version for a symbol when
2087 generating a shared archive. Return an error. */
2088 (*_bfd_error_handler)
2089 (_("%B: version node not found for symbol %s"),
2090 info->output_bfd, h->root.root.string);
2091 bfd_set_error (bfd_error_bad_value);
2092 sinfo->failed = TRUE;
2093 return FALSE;
2094 }
2095
2096 if (hidden)
2097 h->hidden = 1;
2098 }
2099
2100 /* If we don't have a version for this symbol, see if we can find
2101 something. */
2102 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
2103 {
2104 bfd_boolean hide;
2105
2106 h->verinfo.vertree
2107 = bfd_find_version_for_sym (sinfo->info->version_info,
2108 h->root.root.string, &hide);
2109 if (h->verinfo.vertree != NULL && hide)
2110 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2111 }
2112
2113 return TRUE;
2114 }
2115 \f
2116 /* Read and swap the relocs from the section indicated by SHDR. This
2117 may be either a REL or a RELA section. The relocations are
2118 translated into RELA relocations and stored in INTERNAL_RELOCS,
2119 which should have already been allocated to contain enough space.
2120 The EXTERNAL_RELOCS are a buffer where the external form of the
2121 relocations should be stored.
2122
2123 Returns FALSE if something goes wrong. */
2124
2125 static bfd_boolean
2126 elf_link_read_relocs_from_section (bfd *abfd,
2127 asection *sec,
2128 Elf_Internal_Shdr *shdr,
2129 void *external_relocs,
2130 Elf_Internal_Rela *internal_relocs)
2131 {
2132 const struct elf_backend_data *bed;
2133 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2134 const bfd_byte *erela;
2135 const bfd_byte *erelaend;
2136 Elf_Internal_Rela *irela;
2137 Elf_Internal_Shdr *symtab_hdr;
2138 size_t nsyms;
2139
2140 /* Position ourselves at the start of the section. */
2141 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2142 return FALSE;
2143
2144 /* Read the relocations. */
2145 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2146 return FALSE;
2147
2148 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2149 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2150
2151 bed = get_elf_backend_data (abfd);
2152
2153 /* Convert the external relocations to the internal format. */
2154 if (shdr->sh_entsize == bed->s->sizeof_rel)
2155 swap_in = bed->s->swap_reloc_in;
2156 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2157 swap_in = bed->s->swap_reloca_in;
2158 else
2159 {
2160 bfd_set_error (bfd_error_wrong_format);
2161 return FALSE;
2162 }
2163
2164 erela = (const bfd_byte *) external_relocs;
2165 erelaend = erela + shdr->sh_size;
2166 irela = internal_relocs;
2167 while (erela < erelaend)
2168 {
2169 bfd_vma r_symndx;
2170
2171 (*swap_in) (abfd, erela, irela);
2172 r_symndx = ELF32_R_SYM (irela->r_info);
2173 if (bed->s->arch_size == 64)
2174 r_symndx >>= 24;
2175 if (nsyms > 0)
2176 {
2177 if ((size_t) r_symndx >= nsyms)
2178 {
2179 (*_bfd_error_handler)
2180 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2181 " for offset 0x%lx in section `%A'"),
2182 abfd, sec,
2183 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2184 bfd_set_error (bfd_error_bad_value);
2185 return FALSE;
2186 }
2187 }
2188 else if (r_symndx != STN_UNDEF)
2189 {
2190 (*_bfd_error_handler)
2191 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2192 " when the object file has no symbol table"),
2193 abfd, sec,
2194 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2195 bfd_set_error (bfd_error_bad_value);
2196 return FALSE;
2197 }
2198 irela += bed->s->int_rels_per_ext_rel;
2199 erela += shdr->sh_entsize;
2200 }
2201
2202 return TRUE;
2203 }
2204
2205 /* Read and swap the relocs for a section O. They may have been
2206 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2207 not NULL, they are used as buffers to read into. They are known to
2208 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2209 the return value is allocated using either malloc or bfd_alloc,
2210 according to the KEEP_MEMORY argument. If O has two relocation
2211 sections (both REL and RELA relocations), then the REL_HDR
2212 relocations will appear first in INTERNAL_RELOCS, followed by the
2213 RELA_HDR relocations. */
2214
2215 Elf_Internal_Rela *
2216 _bfd_elf_link_read_relocs (bfd *abfd,
2217 asection *o,
2218 void *external_relocs,
2219 Elf_Internal_Rela *internal_relocs,
2220 bfd_boolean keep_memory)
2221 {
2222 void *alloc1 = NULL;
2223 Elf_Internal_Rela *alloc2 = NULL;
2224 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2225 struct bfd_elf_section_data *esdo = elf_section_data (o);
2226 Elf_Internal_Rela *internal_rela_relocs;
2227
2228 if (esdo->relocs != NULL)
2229 return esdo->relocs;
2230
2231 if (o->reloc_count == 0)
2232 return NULL;
2233
2234 if (internal_relocs == NULL)
2235 {
2236 bfd_size_type size;
2237
2238 size = o->reloc_count;
2239 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2240 if (keep_memory)
2241 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2242 else
2243 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2244 if (internal_relocs == NULL)
2245 goto error_return;
2246 }
2247
2248 if (external_relocs == NULL)
2249 {
2250 bfd_size_type size = 0;
2251
2252 if (esdo->rel.hdr)
2253 size += esdo->rel.hdr->sh_size;
2254 if (esdo->rela.hdr)
2255 size += esdo->rela.hdr->sh_size;
2256
2257 alloc1 = bfd_malloc (size);
2258 if (alloc1 == NULL)
2259 goto error_return;
2260 external_relocs = alloc1;
2261 }
2262
2263 internal_rela_relocs = internal_relocs;
2264 if (esdo->rel.hdr)
2265 {
2266 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2267 external_relocs,
2268 internal_relocs))
2269 goto error_return;
2270 external_relocs = (((bfd_byte *) external_relocs)
2271 + esdo->rel.hdr->sh_size);
2272 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2273 * bed->s->int_rels_per_ext_rel);
2274 }
2275
2276 if (esdo->rela.hdr
2277 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2278 external_relocs,
2279 internal_rela_relocs)))
2280 goto error_return;
2281
2282 /* Cache the results for next time, if we can. */
2283 if (keep_memory)
2284 esdo->relocs = internal_relocs;
2285
2286 if (alloc1 != NULL)
2287 free (alloc1);
2288
2289 /* Don't free alloc2, since if it was allocated we are passing it
2290 back (under the name of internal_relocs). */
2291
2292 return internal_relocs;
2293
2294 error_return:
2295 if (alloc1 != NULL)
2296 free (alloc1);
2297 if (alloc2 != NULL)
2298 {
2299 if (keep_memory)
2300 bfd_release (abfd, alloc2);
2301 else
2302 free (alloc2);
2303 }
2304 return NULL;
2305 }
2306
2307 /* Compute the size of, and allocate space for, REL_HDR which is the
2308 section header for a section containing relocations for O. */
2309
2310 static bfd_boolean
2311 _bfd_elf_link_size_reloc_section (bfd *abfd,
2312 struct bfd_elf_section_reloc_data *reldata)
2313 {
2314 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2315
2316 /* That allows us to calculate the size of the section. */
2317 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2318
2319 /* The contents field must last into write_object_contents, so we
2320 allocate it with bfd_alloc rather than malloc. Also since we
2321 cannot be sure that the contents will actually be filled in,
2322 we zero the allocated space. */
2323 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2324 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2325 return FALSE;
2326
2327 if (reldata->hashes == NULL && reldata->count)
2328 {
2329 struct elf_link_hash_entry **p;
2330
2331 p = ((struct elf_link_hash_entry **)
2332 bfd_zmalloc (reldata->count * sizeof (*p)));
2333 if (p == NULL)
2334 return FALSE;
2335
2336 reldata->hashes = p;
2337 }
2338
2339 return TRUE;
2340 }
2341
2342 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2343 originated from the section given by INPUT_REL_HDR) to the
2344 OUTPUT_BFD. */
2345
2346 bfd_boolean
2347 _bfd_elf_link_output_relocs (bfd *output_bfd,
2348 asection *input_section,
2349 Elf_Internal_Shdr *input_rel_hdr,
2350 Elf_Internal_Rela *internal_relocs,
2351 struct elf_link_hash_entry **rel_hash
2352 ATTRIBUTE_UNUSED)
2353 {
2354 Elf_Internal_Rela *irela;
2355 Elf_Internal_Rela *irelaend;
2356 bfd_byte *erel;
2357 struct bfd_elf_section_reloc_data *output_reldata;
2358 asection *output_section;
2359 const struct elf_backend_data *bed;
2360 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2361 struct bfd_elf_section_data *esdo;
2362
2363 output_section = input_section->output_section;
2364
2365 bed = get_elf_backend_data (output_bfd);
2366 esdo = elf_section_data (output_section);
2367 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2368 {
2369 output_reldata = &esdo->rel;
2370 swap_out = bed->s->swap_reloc_out;
2371 }
2372 else if (esdo->rela.hdr
2373 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2374 {
2375 output_reldata = &esdo->rela;
2376 swap_out = bed->s->swap_reloca_out;
2377 }
2378 else
2379 {
2380 (*_bfd_error_handler)
2381 (_("%B: relocation size mismatch in %B section %A"),
2382 output_bfd, input_section->owner, input_section);
2383 bfd_set_error (bfd_error_wrong_format);
2384 return FALSE;
2385 }
2386
2387 erel = output_reldata->hdr->contents;
2388 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2389 irela = internal_relocs;
2390 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2391 * bed->s->int_rels_per_ext_rel);
2392 while (irela < irelaend)
2393 {
2394 (*swap_out) (output_bfd, irela, erel);
2395 irela += bed->s->int_rels_per_ext_rel;
2396 erel += input_rel_hdr->sh_entsize;
2397 }
2398
2399 /* Bump the counter, so that we know where to add the next set of
2400 relocations. */
2401 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2402
2403 return TRUE;
2404 }
2405 \f
2406 /* Make weak undefined symbols in PIE dynamic. */
2407
2408 bfd_boolean
2409 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2410 struct elf_link_hash_entry *h)
2411 {
2412 if (info->pie
2413 && h->dynindx == -1
2414 && h->root.type == bfd_link_hash_undefweak)
2415 return bfd_elf_link_record_dynamic_symbol (info, h);
2416
2417 return TRUE;
2418 }
2419
2420 /* Fix up the flags for a symbol. This handles various cases which
2421 can only be fixed after all the input files are seen. This is
2422 currently called by both adjust_dynamic_symbol and
2423 assign_sym_version, which is unnecessary but perhaps more robust in
2424 the face of future changes. */
2425
2426 static bfd_boolean
2427 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2428 struct elf_info_failed *eif)
2429 {
2430 const struct elf_backend_data *bed;
2431
2432 /* If this symbol was mentioned in a non-ELF file, try to set
2433 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2434 permit a non-ELF file to correctly refer to a symbol defined in
2435 an ELF dynamic object. */
2436 if (h->non_elf)
2437 {
2438 while (h->root.type == bfd_link_hash_indirect)
2439 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2440
2441 if (h->root.type != bfd_link_hash_defined
2442 && h->root.type != bfd_link_hash_defweak)
2443 {
2444 h->ref_regular = 1;
2445 h->ref_regular_nonweak = 1;
2446 }
2447 else
2448 {
2449 if (h->root.u.def.section->owner != NULL
2450 && (bfd_get_flavour (h->root.u.def.section->owner)
2451 == bfd_target_elf_flavour))
2452 {
2453 h->ref_regular = 1;
2454 h->ref_regular_nonweak = 1;
2455 }
2456 else
2457 h->def_regular = 1;
2458 }
2459
2460 if (h->dynindx == -1
2461 && (h->def_dynamic
2462 || h->ref_dynamic))
2463 {
2464 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2465 {
2466 eif->failed = TRUE;
2467 return FALSE;
2468 }
2469 }
2470 }
2471 else
2472 {
2473 /* Unfortunately, NON_ELF is only correct if the symbol
2474 was first seen in a non-ELF file. Fortunately, if the symbol
2475 was first seen in an ELF file, we're probably OK unless the
2476 symbol was defined in a non-ELF file. Catch that case here.
2477 FIXME: We're still in trouble if the symbol was first seen in
2478 a dynamic object, and then later in a non-ELF regular object. */
2479 if ((h->root.type == bfd_link_hash_defined
2480 || h->root.type == bfd_link_hash_defweak)
2481 && !h->def_regular
2482 && (h->root.u.def.section->owner != NULL
2483 ? (bfd_get_flavour (h->root.u.def.section->owner)
2484 != bfd_target_elf_flavour)
2485 : (bfd_is_abs_section (h->root.u.def.section)
2486 && !h->def_dynamic)))
2487 h->def_regular = 1;
2488 }
2489
2490 /* Backend specific symbol fixup. */
2491 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2492 if (bed->elf_backend_fixup_symbol
2493 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2494 return FALSE;
2495
2496 /* If this is a final link, and the symbol was defined as a common
2497 symbol in a regular object file, and there was no definition in
2498 any dynamic object, then the linker will have allocated space for
2499 the symbol in a common section but the DEF_REGULAR
2500 flag will not have been set. */
2501 if (h->root.type == bfd_link_hash_defined
2502 && !h->def_regular
2503 && h->ref_regular
2504 && !h->def_dynamic
2505 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2506 h->def_regular = 1;
2507
2508 /* If -Bsymbolic was used (which means to bind references to global
2509 symbols to the definition within the shared object), and this
2510 symbol was defined in a regular object, then it actually doesn't
2511 need a PLT entry. Likewise, if the symbol has non-default
2512 visibility. If the symbol has hidden or internal visibility, we
2513 will force it local. */
2514 if (h->needs_plt
2515 && eif->info->shared
2516 && is_elf_hash_table (eif->info->hash)
2517 && (SYMBOLIC_BIND (eif->info, h)
2518 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2519 && h->def_regular)
2520 {
2521 bfd_boolean force_local;
2522
2523 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2524 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2525 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2526 }
2527
2528 /* If a weak undefined symbol has non-default visibility, we also
2529 hide it from the dynamic linker. */
2530 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2531 && h->root.type == bfd_link_hash_undefweak)
2532 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2533
2534 /* If this is a weak defined symbol in a dynamic object, and we know
2535 the real definition in the dynamic object, copy interesting flags
2536 over to the real definition. */
2537 if (h->u.weakdef != NULL)
2538 {
2539 /* If the real definition is defined by a regular object file,
2540 don't do anything special. See the longer description in
2541 _bfd_elf_adjust_dynamic_symbol, below. */
2542 if (h->u.weakdef->def_regular)
2543 h->u.weakdef = NULL;
2544 else
2545 {
2546 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2547
2548 while (h->root.type == bfd_link_hash_indirect)
2549 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2550
2551 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2552 || h->root.type == bfd_link_hash_defweak);
2553 BFD_ASSERT (weakdef->def_dynamic);
2554 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2555 || weakdef->root.type == bfd_link_hash_defweak);
2556 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2557 }
2558 }
2559
2560 return TRUE;
2561 }
2562
2563 /* Make the backend pick a good value for a dynamic symbol. This is
2564 called via elf_link_hash_traverse, and also calls itself
2565 recursively. */
2566
2567 static bfd_boolean
2568 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2569 {
2570 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2571 bfd *dynobj;
2572 const struct elf_backend_data *bed;
2573
2574 if (! is_elf_hash_table (eif->info->hash))
2575 return FALSE;
2576
2577 /* Ignore indirect symbols. These are added by the versioning code. */
2578 if (h->root.type == bfd_link_hash_indirect)
2579 return TRUE;
2580
2581 /* Fix the symbol flags. */
2582 if (! _bfd_elf_fix_symbol_flags (h, eif))
2583 return FALSE;
2584
2585 /* If this symbol does not require a PLT entry, and it is not
2586 defined by a dynamic object, or is not referenced by a regular
2587 object, ignore it. We do have to handle a weak defined symbol,
2588 even if no regular object refers to it, if we decided to add it
2589 to the dynamic symbol table. FIXME: Do we normally need to worry
2590 about symbols which are defined by one dynamic object and
2591 referenced by another one? */
2592 if (!h->needs_plt
2593 && h->type != STT_GNU_IFUNC
2594 && (h->def_regular
2595 || !h->def_dynamic
2596 || (!h->ref_regular
2597 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2598 {
2599 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2600 return TRUE;
2601 }
2602
2603 /* If we've already adjusted this symbol, don't do it again. This
2604 can happen via a recursive call. */
2605 if (h->dynamic_adjusted)
2606 return TRUE;
2607
2608 /* Don't look at this symbol again. Note that we must set this
2609 after checking the above conditions, because we may look at a
2610 symbol once, decide not to do anything, and then get called
2611 recursively later after REF_REGULAR is set below. */
2612 h->dynamic_adjusted = 1;
2613
2614 /* If this is a weak definition, and we know a real definition, and
2615 the real symbol is not itself defined by a regular object file,
2616 then get a good value for the real definition. We handle the
2617 real symbol first, for the convenience of the backend routine.
2618
2619 Note that there is a confusing case here. If the real definition
2620 is defined by a regular object file, we don't get the real symbol
2621 from the dynamic object, but we do get the weak symbol. If the
2622 processor backend uses a COPY reloc, then if some routine in the
2623 dynamic object changes the real symbol, we will not see that
2624 change in the corresponding weak symbol. This is the way other
2625 ELF linkers work as well, and seems to be a result of the shared
2626 library model.
2627
2628 I will clarify this issue. Most SVR4 shared libraries define the
2629 variable _timezone and define timezone as a weak synonym. The
2630 tzset call changes _timezone. If you write
2631 extern int timezone;
2632 int _timezone = 5;
2633 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2634 you might expect that, since timezone is a synonym for _timezone,
2635 the same number will print both times. However, if the processor
2636 backend uses a COPY reloc, then actually timezone will be copied
2637 into your process image, and, since you define _timezone
2638 yourself, _timezone will not. Thus timezone and _timezone will
2639 wind up at different memory locations. The tzset call will set
2640 _timezone, leaving timezone unchanged. */
2641
2642 if (h->u.weakdef != NULL)
2643 {
2644 /* If we get to this point, there is an implicit reference to
2645 H->U.WEAKDEF by a regular object file via the weak symbol H. */
2646 h->u.weakdef->ref_regular = 1;
2647
2648 /* Ensure that the backend adjust_dynamic_symbol function sees
2649 H->U.WEAKDEF before H by recursively calling ourselves. */
2650 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2651 return FALSE;
2652 }
2653
2654 /* If a symbol has no type and no size and does not require a PLT
2655 entry, then we are probably about to do the wrong thing here: we
2656 are probably going to create a COPY reloc for an empty object.
2657 This case can arise when a shared object is built with assembly
2658 code, and the assembly code fails to set the symbol type. */
2659 if (h->size == 0
2660 && h->type == STT_NOTYPE
2661 && !h->needs_plt)
2662 (*_bfd_error_handler)
2663 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2664 h->root.root.string);
2665
2666 dynobj = elf_hash_table (eif->info)->dynobj;
2667 bed = get_elf_backend_data (dynobj);
2668
2669 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2670 {
2671 eif->failed = TRUE;
2672 return FALSE;
2673 }
2674
2675 return TRUE;
2676 }
2677
2678 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2679 DYNBSS. */
2680
2681 bfd_boolean
2682 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2683 struct elf_link_hash_entry *h,
2684 asection *dynbss)
2685 {
2686 unsigned int power_of_two;
2687 bfd_vma mask;
2688 asection *sec = h->root.u.def.section;
2689
2690 /* The section aligment of definition is the maximum alignment
2691 requirement of symbols defined in the section. Since we don't
2692 know the symbol alignment requirement, we start with the
2693 maximum alignment and check low bits of the symbol address
2694 for the minimum alignment. */
2695 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2696 mask = ((bfd_vma) 1 << power_of_two) - 1;
2697 while ((h->root.u.def.value & mask) != 0)
2698 {
2699 mask >>= 1;
2700 --power_of_two;
2701 }
2702
2703 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2704 dynbss))
2705 {
2706 /* Adjust the section alignment if needed. */
2707 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2708 power_of_two))
2709 return FALSE;
2710 }
2711
2712 /* We make sure that the symbol will be aligned properly. */
2713 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2714
2715 /* Define the symbol as being at this point in DYNBSS. */
2716 h->root.u.def.section = dynbss;
2717 h->root.u.def.value = dynbss->size;
2718
2719 /* Increment the size of DYNBSS to make room for the symbol. */
2720 dynbss->size += h->size;
2721
2722 /* No error if extern_protected_data is true. */
2723 if (h->protected_def
2724 && (!info->extern_protected_data
2725 || (info->extern_protected_data < 0
2726 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
2727 info->callbacks->einfo
2728 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2729 h->root.root.string);
2730
2731 return TRUE;
2732 }
2733
2734 /* Adjust all external symbols pointing into SEC_MERGE sections
2735 to reflect the object merging within the sections. */
2736
2737 static bfd_boolean
2738 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2739 {
2740 asection *sec;
2741
2742 if ((h->root.type == bfd_link_hash_defined
2743 || h->root.type == bfd_link_hash_defweak)
2744 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2745 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
2746 {
2747 bfd *output_bfd = (bfd *) data;
2748
2749 h->root.u.def.value =
2750 _bfd_merged_section_offset (output_bfd,
2751 &h->root.u.def.section,
2752 elf_section_data (sec)->sec_info,
2753 h->root.u.def.value);
2754 }
2755
2756 return TRUE;
2757 }
2758
2759 /* Returns false if the symbol referred to by H should be considered
2760 to resolve local to the current module, and true if it should be
2761 considered to bind dynamically. */
2762
2763 bfd_boolean
2764 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2765 struct bfd_link_info *info,
2766 bfd_boolean not_local_protected)
2767 {
2768 bfd_boolean binding_stays_local_p;
2769 const struct elf_backend_data *bed;
2770 struct elf_link_hash_table *hash_table;
2771
2772 if (h == NULL)
2773 return FALSE;
2774
2775 while (h->root.type == bfd_link_hash_indirect
2776 || h->root.type == bfd_link_hash_warning)
2777 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2778
2779 /* If it was forced local, then clearly it's not dynamic. */
2780 if (h->dynindx == -1)
2781 return FALSE;
2782 if (h->forced_local)
2783 return FALSE;
2784
2785 /* Identify the cases where name binding rules say that a
2786 visible symbol resolves locally. */
2787 binding_stays_local_p = info->executable || SYMBOLIC_BIND (info, h);
2788
2789 switch (ELF_ST_VISIBILITY (h->other))
2790 {
2791 case STV_INTERNAL:
2792 case STV_HIDDEN:
2793 return FALSE;
2794
2795 case STV_PROTECTED:
2796 hash_table = elf_hash_table (info);
2797 if (!is_elf_hash_table (hash_table))
2798 return FALSE;
2799
2800 bed = get_elf_backend_data (hash_table->dynobj);
2801
2802 /* Proper resolution for function pointer equality may require
2803 that these symbols perhaps be resolved dynamically, even though
2804 we should be resolving them to the current module. */
2805 if (!not_local_protected || !bed->is_function_type (h->type))
2806 binding_stays_local_p = TRUE;
2807 break;
2808
2809 default:
2810 break;
2811 }
2812
2813 /* If it isn't defined locally, then clearly it's dynamic. */
2814 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2815 return TRUE;
2816
2817 /* Otherwise, the symbol is dynamic if binding rules don't tell
2818 us that it remains local. */
2819 return !binding_stays_local_p;
2820 }
2821
2822 /* Return true if the symbol referred to by H should be considered
2823 to resolve local to the current module, and false otherwise. Differs
2824 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2825 undefined symbols. The two functions are virtually identical except
2826 for the place where forced_local and dynindx == -1 are tested. If
2827 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2828 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2829 the symbol is local only for defined symbols.
2830 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2831 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2832 treatment of undefined weak symbols. For those that do not make
2833 undefined weak symbols dynamic, both functions may return false. */
2834
2835 bfd_boolean
2836 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2837 struct bfd_link_info *info,
2838 bfd_boolean local_protected)
2839 {
2840 const struct elf_backend_data *bed;
2841 struct elf_link_hash_table *hash_table;
2842
2843 /* If it's a local sym, of course we resolve locally. */
2844 if (h == NULL)
2845 return TRUE;
2846
2847 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2848 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2849 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2850 return TRUE;
2851
2852 /* Common symbols that become definitions don't get the DEF_REGULAR
2853 flag set, so test it first, and don't bail out. */
2854 if (ELF_COMMON_DEF_P (h))
2855 /* Do nothing. */;
2856 /* If we don't have a definition in a regular file, then we can't
2857 resolve locally. The sym is either undefined or dynamic. */
2858 else if (!h->def_regular)
2859 return FALSE;
2860
2861 /* Forced local symbols resolve locally. */
2862 if (h->forced_local)
2863 return TRUE;
2864
2865 /* As do non-dynamic symbols. */
2866 if (h->dynindx == -1)
2867 return TRUE;
2868
2869 /* At this point, we know the symbol is defined and dynamic. In an
2870 executable it must resolve locally, likewise when building symbolic
2871 shared libraries. */
2872 if (info->executable || SYMBOLIC_BIND (info, h))
2873 return TRUE;
2874
2875 /* Now deal with defined dynamic symbols in shared libraries. Ones
2876 with default visibility might not resolve locally. */
2877 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2878 return FALSE;
2879
2880 hash_table = elf_hash_table (info);
2881 if (!is_elf_hash_table (hash_table))
2882 return TRUE;
2883
2884 bed = get_elf_backend_data (hash_table->dynobj);
2885
2886 /* If extern_protected_data is false, STV_PROTECTED non-function
2887 symbols are local. */
2888 if ((!info->extern_protected_data
2889 || (info->extern_protected_data < 0
2890 && !bed->extern_protected_data))
2891 && !bed->is_function_type (h->type))
2892 return TRUE;
2893
2894 /* Function pointer equality tests may require that STV_PROTECTED
2895 symbols be treated as dynamic symbols. If the address of a
2896 function not defined in an executable is set to that function's
2897 plt entry in the executable, then the address of the function in
2898 a shared library must also be the plt entry in the executable. */
2899 return local_protected;
2900 }
2901
2902 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2903 aligned. Returns the first TLS output section. */
2904
2905 struct bfd_section *
2906 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2907 {
2908 struct bfd_section *sec, *tls;
2909 unsigned int align = 0;
2910
2911 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2912 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2913 break;
2914 tls = sec;
2915
2916 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2917 if (sec->alignment_power > align)
2918 align = sec->alignment_power;
2919
2920 elf_hash_table (info)->tls_sec = tls;
2921
2922 /* Ensure the alignment of the first section is the largest alignment,
2923 so that the tls segment starts aligned. */
2924 if (tls != NULL)
2925 tls->alignment_power = align;
2926
2927 return tls;
2928 }
2929
2930 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2931 static bfd_boolean
2932 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2933 Elf_Internal_Sym *sym)
2934 {
2935 const struct elf_backend_data *bed;
2936
2937 /* Local symbols do not count, but target specific ones might. */
2938 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2939 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2940 return FALSE;
2941
2942 bed = get_elf_backend_data (abfd);
2943 /* Function symbols do not count. */
2944 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
2945 return FALSE;
2946
2947 /* If the section is undefined, then so is the symbol. */
2948 if (sym->st_shndx == SHN_UNDEF)
2949 return FALSE;
2950
2951 /* If the symbol is defined in the common section, then
2952 it is a common definition and so does not count. */
2953 if (bed->common_definition (sym))
2954 return FALSE;
2955
2956 /* If the symbol is in a target specific section then we
2957 must rely upon the backend to tell us what it is. */
2958 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2959 /* FIXME - this function is not coded yet:
2960
2961 return _bfd_is_global_symbol_definition (abfd, sym);
2962
2963 Instead for now assume that the definition is not global,
2964 Even if this is wrong, at least the linker will behave
2965 in the same way that it used to do. */
2966 return FALSE;
2967
2968 return TRUE;
2969 }
2970
2971 /* Search the symbol table of the archive element of the archive ABFD
2972 whose archive map contains a mention of SYMDEF, and determine if
2973 the symbol is defined in this element. */
2974 static bfd_boolean
2975 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2976 {
2977 Elf_Internal_Shdr * hdr;
2978 bfd_size_type symcount;
2979 bfd_size_type extsymcount;
2980 bfd_size_type extsymoff;
2981 Elf_Internal_Sym *isymbuf;
2982 Elf_Internal_Sym *isym;
2983 Elf_Internal_Sym *isymend;
2984 bfd_boolean result;
2985
2986 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2987 if (abfd == NULL)
2988 return FALSE;
2989
2990 /* Return FALSE if the object has been claimed by plugin. */
2991 if (abfd->plugin_format == bfd_plugin_yes)
2992 return FALSE;
2993
2994 if (! bfd_check_format (abfd, bfd_object))
2995 return FALSE;
2996
2997 /* Select the appropriate symbol table. */
2998 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2999 hdr = &elf_tdata (abfd)->symtab_hdr;
3000 else
3001 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3002
3003 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3004
3005 /* The sh_info field of the symtab header tells us where the
3006 external symbols start. We don't care about the local symbols. */
3007 if (elf_bad_symtab (abfd))
3008 {
3009 extsymcount = symcount;
3010 extsymoff = 0;
3011 }
3012 else
3013 {
3014 extsymcount = symcount - hdr->sh_info;
3015 extsymoff = hdr->sh_info;
3016 }
3017
3018 if (extsymcount == 0)
3019 return FALSE;
3020
3021 /* Read in the symbol table. */
3022 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3023 NULL, NULL, NULL);
3024 if (isymbuf == NULL)
3025 return FALSE;
3026
3027 /* Scan the symbol table looking for SYMDEF. */
3028 result = FALSE;
3029 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3030 {
3031 const char *name;
3032
3033 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3034 isym->st_name);
3035 if (name == NULL)
3036 break;
3037
3038 if (strcmp (name, symdef->name) == 0)
3039 {
3040 result = is_global_data_symbol_definition (abfd, isym);
3041 break;
3042 }
3043 }
3044
3045 free (isymbuf);
3046
3047 return result;
3048 }
3049 \f
3050 /* Add an entry to the .dynamic table. */
3051
3052 bfd_boolean
3053 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3054 bfd_vma tag,
3055 bfd_vma val)
3056 {
3057 struct elf_link_hash_table *hash_table;
3058 const struct elf_backend_data *bed;
3059 asection *s;
3060 bfd_size_type newsize;
3061 bfd_byte *newcontents;
3062 Elf_Internal_Dyn dyn;
3063
3064 hash_table = elf_hash_table (info);
3065 if (! is_elf_hash_table (hash_table))
3066 return FALSE;
3067
3068 bed = get_elf_backend_data (hash_table->dynobj);
3069 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3070 BFD_ASSERT (s != NULL);
3071
3072 newsize = s->size + bed->s->sizeof_dyn;
3073 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3074 if (newcontents == NULL)
3075 return FALSE;
3076
3077 dyn.d_tag = tag;
3078 dyn.d_un.d_val = val;
3079 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3080
3081 s->size = newsize;
3082 s->contents = newcontents;
3083
3084 return TRUE;
3085 }
3086
3087 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3088 otherwise just check whether one already exists. Returns -1 on error,
3089 1 if a DT_NEEDED tag already exists, and 0 on success. */
3090
3091 static int
3092 elf_add_dt_needed_tag (bfd *abfd,
3093 struct bfd_link_info *info,
3094 const char *soname,
3095 bfd_boolean do_it)
3096 {
3097 struct elf_link_hash_table *hash_table;
3098 bfd_size_type strindex;
3099
3100 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3101 return -1;
3102
3103 hash_table = elf_hash_table (info);
3104 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3105 if (strindex == (bfd_size_type) -1)
3106 return -1;
3107
3108 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3109 {
3110 asection *sdyn;
3111 const struct elf_backend_data *bed;
3112 bfd_byte *extdyn;
3113
3114 bed = get_elf_backend_data (hash_table->dynobj);
3115 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3116 if (sdyn != NULL)
3117 for (extdyn = sdyn->contents;
3118 extdyn < sdyn->contents + sdyn->size;
3119 extdyn += bed->s->sizeof_dyn)
3120 {
3121 Elf_Internal_Dyn dyn;
3122
3123 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3124 if (dyn.d_tag == DT_NEEDED
3125 && dyn.d_un.d_val == strindex)
3126 {
3127 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3128 return 1;
3129 }
3130 }
3131 }
3132
3133 if (do_it)
3134 {
3135 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3136 return -1;
3137
3138 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3139 return -1;
3140 }
3141 else
3142 /* We were just checking for existence of the tag. */
3143 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3144
3145 return 0;
3146 }
3147
3148 static bfd_boolean
3149 on_needed_list (const char *soname, struct bfd_link_needed_list *needed)
3150 {
3151 for (; needed != NULL; needed = needed->next)
3152 if ((elf_dyn_lib_class (needed->by) & DYN_AS_NEEDED) == 0
3153 && strcmp (soname, needed->name) == 0)
3154 return TRUE;
3155
3156 return FALSE;
3157 }
3158
3159 /* Sort symbol by value, section, and size. */
3160 static int
3161 elf_sort_symbol (const void *arg1, const void *arg2)
3162 {
3163 const struct elf_link_hash_entry *h1;
3164 const struct elf_link_hash_entry *h2;
3165 bfd_signed_vma vdiff;
3166
3167 h1 = *(const struct elf_link_hash_entry **) arg1;
3168 h2 = *(const struct elf_link_hash_entry **) arg2;
3169 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3170 if (vdiff != 0)
3171 return vdiff > 0 ? 1 : -1;
3172 else
3173 {
3174 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3175 if (sdiff != 0)
3176 return sdiff > 0 ? 1 : -1;
3177 }
3178 vdiff = h1->size - h2->size;
3179 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3180 }
3181
3182 /* This function is used to adjust offsets into .dynstr for
3183 dynamic symbols. This is called via elf_link_hash_traverse. */
3184
3185 static bfd_boolean
3186 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3187 {
3188 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3189
3190 if (h->dynindx != -1)
3191 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3192 return TRUE;
3193 }
3194
3195 /* Assign string offsets in .dynstr, update all structures referencing
3196 them. */
3197
3198 static bfd_boolean
3199 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3200 {
3201 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3202 struct elf_link_local_dynamic_entry *entry;
3203 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3204 bfd *dynobj = hash_table->dynobj;
3205 asection *sdyn;
3206 bfd_size_type size;
3207 const struct elf_backend_data *bed;
3208 bfd_byte *extdyn;
3209
3210 _bfd_elf_strtab_finalize (dynstr);
3211 size = _bfd_elf_strtab_size (dynstr);
3212
3213 bed = get_elf_backend_data (dynobj);
3214 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3215 BFD_ASSERT (sdyn != NULL);
3216
3217 /* Update all .dynamic entries referencing .dynstr strings. */
3218 for (extdyn = sdyn->contents;
3219 extdyn < sdyn->contents + sdyn->size;
3220 extdyn += bed->s->sizeof_dyn)
3221 {
3222 Elf_Internal_Dyn dyn;
3223
3224 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3225 switch (dyn.d_tag)
3226 {
3227 case DT_STRSZ:
3228 dyn.d_un.d_val = size;
3229 break;
3230 case DT_NEEDED:
3231 case DT_SONAME:
3232 case DT_RPATH:
3233 case DT_RUNPATH:
3234 case DT_FILTER:
3235 case DT_AUXILIARY:
3236 case DT_AUDIT:
3237 case DT_DEPAUDIT:
3238 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3239 break;
3240 default:
3241 continue;
3242 }
3243 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3244 }
3245
3246 /* Now update local dynamic symbols. */
3247 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3248 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3249 entry->isym.st_name);
3250
3251 /* And the rest of dynamic symbols. */
3252 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3253
3254 /* Adjust version definitions. */
3255 if (elf_tdata (output_bfd)->cverdefs)
3256 {
3257 asection *s;
3258 bfd_byte *p;
3259 bfd_size_type i;
3260 Elf_Internal_Verdef def;
3261 Elf_Internal_Verdaux defaux;
3262
3263 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3264 p = s->contents;
3265 do
3266 {
3267 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3268 &def);
3269 p += sizeof (Elf_External_Verdef);
3270 if (def.vd_aux != sizeof (Elf_External_Verdef))
3271 continue;
3272 for (i = 0; i < def.vd_cnt; ++i)
3273 {
3274 _bfd_elf_swap_verdaux_in (output_bfd,
3275 (Elf_External_Verdaux *) p, &defaux);
3276 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3277 defaux.vda_name);
3278 _bfd_elf_swap_verdaux_out (output_bfd,
3279 &defaux, (Elf_External_Verdaux *) p);
3280 p += sizeof (Elf_External_Verdaux);
3281 }
3282 }
3283 while (def.vd_next);
3284 }
3285
3286 /* Adjust version references. */
3287 if (elf_tdata (output_bfd)->verref)
3288 {
3289 asection *s;
3290 bfd_byte *p;
3291 bfd_size_type i;
3292 Elf_Internal_Verneed need;
3293 Elf_Internal_Vernaux needaux;
3294
3295 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3296 p = s->contents;
3297 do
3298 {
3299 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3300 &need);
3301 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3302 _bfd_elf_swap_verneed_out (output_bfd, &need,
3303 (Elf_External_Verneed *) p);
3304 p += sizeof (Elf_External_Verneed);
3305 for (i = 0; i < need.vn_cnt; ++i)
3306 {
3307 _bfd_elf_swap_vernaux_in (output_bfd,
3308 (Elf_External_Vernaux *) p, &needaux);
3309 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3310 needaux.vna_name);
3311 _bfd_elf_swap_vernaux_out (output_bfd,
3312 &needaux,
3313 (Elf_External_Vernaux *) p);
3314 p += sizeof (Elf_External_Vernaux);
3315 }
3316 }
3317 while (need.vn_next);
3318 }
3319
3320 return TRUE;
3321 }
3322 \f
3323 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3324 The default is to only match when the INPUT and OUTPUT are exactly
3325 the same target. */
3326
3327 bfd_boolean
3328 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3329 const bfd_target *output)
3330 {
3331 return input == output;
3332 }
3333
3334 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3335 This version is used when different targets for the same architecture
3336 are virtually identical. */
3337
3338 bfd_boolean
3339 _bfd_elf_relocs_compatible (const bfd_target *input,
3340 const bfd_target *output)
3341 {
3342 const struct elf_backend_data *obed, *ibed;
3343
3344 if (input == output)
3345 return TRUE;
3346
3347 ibed = xvec_get_elf_backend_data (input);
3348 obed = xvec_get_elf_backend_data (output);
3349
3350 if (ibed->arch != obed->arch)
3351 return FALSE;
3352
3353 /* If both backends are using this function, deem them compatible. */
3354 return ibed->relocs_compatible == obed->relocs_compatible;
3355 }
3356
3357 /* Make a special call to the linker "notice" function to tell it that
3358 we are about to handle an as-needed lib, or have finished
3359 processing the lib. */
3360
3361 bfd_boolean
3362 _bfd_elf_notice_as_needed (bfd *ibfd,
3363 struct bfd_link_info *info,
3364 enum notice_asneeded_action act)
3365 {
3366 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3367 }
3368
3369 /* Add symbols from an ELF object file to the linker hash table. */
3370
3371 static bfd_boolean
3372 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3373 {
3374 Elf_Internal_Ehdr *ehdr;
3375 Elf_Internal_Shdr *hdr;
3376 bfd_size_type symcount;
3377 bfd_size_type extsymcount;
3378 bfd_size_type extsymoff;
3379 struct elf_link_hash_entry **sym_hash;
3380 bfd_boolean dynamic;
3381 Elf_External_Versym *extversym = NULL;
3382 Elf_External_Versym *ever;
3383 struct elf_link_hash_entry *weaks;
3384 struct elf_link_hash_entry **nondeflt_vers = NULL;
3385 bfd_size_type nondeflt_vers_cnt = 0;
3386 Elf_Internal_Sym *isymbuf = NULL;
3387 Elf_Internal_Sym *isym;
3388 Elf_Internal_Sym *isymend;
3389 const struct elf_backend_data *bed;
3390 bfd_boolean add_needed;
3391 struct elf_link_hash_table *htab;
3392 bfd_size_type amt;
3393 void *alloc_mark = NULL;
3394 struct bfd_hash_entry **old_table = NULL;
3395 unsigned int old_size = 0;
3396 unsigned int old_count = 0;
3397 void *old_tab = NULL;
3398 void *old_ent;
3399 struct bfd_link_hash_entry *old_undefs = NULL;
3400 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3401 long old_dynsymcount = 0;
3402 bfd_size_type old_dynstr_size = 0;
3403 size_t tabsize = 0;
3404 asection *s;
3405 bfd_boolean just_syms;
3406
3407 htab = elf_hash_table (info);
3408 bed = get_elf_backend_data (abfd);
3409
3410 if ((abfd->flags & DYNAMIC) == 0)
3411 dynamic = FALSE;
3412 else
3413 {
3414 dynamic = TRUE;
3415
3416 /* You can't use -r against a dynamic object. Also, there's no
3417 hope of using a dynamic object which does not exactly match
3418 the format of the output file. */
3419 if (info->relocatable
3420 || !is_elf_hash_table (htab)
3421 || info->output_bfd->xvec != abfd->xvec)
3422 {
3423 if (info->relocatable)
3424 bfd_set_error (bfd_error_invalid_operation);
3425 else
3426 bfd_set_error (bfd_error_wrong_format);
3427 goto error_return;
3428 }
3429 }
3430
3431 ehdr = elf_elfheader (abfd);
3432 if (info->warn_alternate_em
3433 && bed->elf_machine_code != ehdr->e_machine
3434 && ((bed->elf_machine_alt1 != 0
3435 && ehdr->e_machine == bed->elf_machine_alt1)
3436 || (bed->elf_machine_alt2 != 0
3437 && ehdr->e_machine == bed->elf_machine_alt2)))
3438 info->callbacks->einfo
3439 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3440 ehdr->e_machine, abfd, bed->elf_machine_code);
3441
3442 /* As a GNU extension, any input sections which are named
3443 .gnu.warning.SYMBOL are treated as warning symbols for the given
3444 symbol. This differs from .gnu.warning sections, which generate
3445 warnings when they are included in an output file. */
3446 /* PR 12761: Also generate this warning when building shared libraries. */
3447 for (s = abfd->sections; s != NULL; s = s->next)
3448 {
3449 const char *name;
3450
3451 name = bfd_get_section_name (abfd, s);
3452 if (CONST_STRNEQ (name, ".gnu.warning."))
3453 {
3454 char *msg;
3455 bfd_size_type sz;
3456
3457 name += sizeof ".gnu.warning." - 1;
3458
3459 /* If this is a shared object, then look up the symbol
3460 in the hash table. If it is there, and it is already
3461 been defined, then we will not be using the entry
3462 from this shared object, so we don't need to warn.
3463 FIXME: If we see the definition in a regular object
3464 later on, we will warn, but we shouldn't. The only
3465 fix is to keep track of what warnings we are supposed
3466 to emit, and then handle them all at the end of the
3467 link. */
3468 if (dynamic)
3469 {
3470 struct elf_link_hash_entry *h;
3471
3472 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3473
3474 /* FIXME: What about bfd_link_hash_common? */
3475 if (h != NULL
3476 && (h->root.type == bfd_link_hash_defined
3477 || h->root.type == bfd_link_hash_defweak))
3478 continue;
3479 }
3480
3481 sz = s->size;
3482 msg = (char *) bfd_alloc (abfd, sz + 1);
3483 if (msg == NULL)
3484 goto error_return;
3485
3486 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3487 goto error_return;
3488
3489 msg[sz] = '\0';
3490
3491 if (! (_bfd_generic_link_add_one_symbol
3492 (info, abfd, name, BSF_WARNING, s, 0, msg,
3493 FALSE, bed->collect, NULL)))
3494 goto error_return;
3495
3496 if (info->executable)
3497 {
3498 /* Clobber the section size so that the warning does
3499 not get copied into the output file. */
3500 s->size = 0;
3501
3502 /* Also set SEC_EXCLUDE, so that symbols defined in
3503 the warning section don't get copied to the output. */
3504 s->flags |= SEC_EXCLUDE;
3505 }
3506 }
3507 }
3508
3509 just_syms = ((s = abfd->sections) != NULL
3510 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3511
3512 add_needed = TRUE;
3513 if (! dynamic)
3514 {
3515 /* If we are creating a shared library, create all the dynamic
3516 sections immediately. We need to attach them to something,
3517 so we attach them to this BFD, provided it is the right
3518 format and is not from ld --just-symbols. FIXME: If there
3519 are no input BFD's of the same format as the output, we can't
3520 make a shared library. */
3521 if (!just_syms
3522 && info->shared
3523 && is_elf_hash_table (htab)
3524 && info->output_bfd->xvec == abfd->xvec
3525 && !htab->dynamic_sections_created)
3526 {
3527 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3528 goto error_return;
3529 }
3530 }
3531 else if (!is_elf_hash_table (htab))
3532 goto error_return;
3533 else
3534 {
3535 const char *soname = NULL;
3536 char *audit = NULL;
3537 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3538 int ret;
3539
3540 /* ld --just-symbols and dynamic objects don't mix very well.
3541 ld shouldn't allow it. */
3542 if (just_syms)
3543 abort ();
3544
3545 /* If this dynamic lib was specified on the command line with
3546 --as-needed in effect, then we don't want to add a DT_NEEDED
3547 tag unless the lib is actually used. Similary for libs brought
3548 in by another lib's DT_NEEDED. When --no-add-needed is used
3549 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3550 any dynamic library in DT_NEEDED tags in the dynamic lib at
3551 all. */
3552 add_needed = (elf_dyn_lib_class (abfd)
3553 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3554 | DYN_NO_NEEDED)) == 0;
3555
3556 s = bfd_get_section_by_name (abfd, ".dynamic");
3557 if (s != NULL)
3558 {
3559 bfd_byte *dynbuf;
3560 bfd_byte *extdyn;
3561 unsigned int elfsec;
3562 unsigned long shlink;
3563
3564 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3565 {
3566 error_free_dyn:
3567 free (dynbuf);
3568 goto error_return;
3569 }
3570
3571 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3572 if (elfsec == SHN_BAD)
3573 goto error_free_dyn;
3574 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3575
3576 for (extdyn = dynbuf;
3577 extdyn < dynbuf + s->size;
3578 extdyn += bed->s->sizeof_dyn)
3579 {
3580 Elf_Internal_Dyn dyn;
3581
3582 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3583 if (dyn.d_tag == DT_SONAME)
3584 {
3585 unsigned int tagv = dyn.d_un.d_val;
3586 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3587 if (soname == NULL)
3588 goto error_free_dyn;
3589 }
3590 if (dyn.d_tag == DT_NEEDED)
3591 {
3592 struct bfd_link_needed_list *n, **pn;
3593 char *fnm, *anm;
3594 unsigned int tagv = dyn.d_un.d_val;
3595
3596 amt = sizeof (struct bfd_link_needed_list);
3597 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3598 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3599 if (n == NULL || fnm == NULL)
3600 goto error_free_dyn;
3601 amt = strlen (fnm) + 1;
3602 anm = (char *) bfd_alloc (abfd, amt);
3603 if (anm == NULL)
3604 goto error_free_dyn;
3605 memcpy (anm, fnm, amt);
3606 n->name = anm;
3607 n->by = abfd;
3608 n->next = NULL;
3609 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3610 ;
3611 *pn = n;
3612 }
3613 if (dyn.d_tag == DT_RUNPATH)
3614 {
3615 struct bfd_link_needed_list *n, **pn;
3616 char *fnm, *anm;
3617 unsigned int tagv = dyn.d_un.d_val;
3618
3619 amt = sizeof (struct bfd_link_needed_list);
3620 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3621 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3622 if (n == NULL || fnm == NULL)
3623 goto error_free_dyn;
3624 amt = strlen (fnm) + 1;
3625 anm = (char *) bfd_alloc (abfd, amt);
3626 if (anm == NULL)
3627 goto error_free_dyn;
3628 memcpy (anm, fnm, amt);
3629 n->name = anm;
3630 n->by = abfd;
3631 n->next = NULL;
3632 for (pn = & runpath;
3633 *pn != NULL;
3634 pn = &(*pn)->next)
3635 ;
3636 *pn = n;
3637 }
3638 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3639 if (!runpath && dyn.d_tag == DT_RPATH)
3640 {
3641 struct bfd_link_needed_list *n, **pn;
3642 char *fnm, *anm;
3643 unsigned int tagv = dyn.d_un.d_val;
3644
3645 amt = sizeof (struct bfd_link_needed_list);
3646 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3647 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3648 if (n == NULL || fnm == NULL)
3649 goto error_free_dyn;
3650 amt = strlen (fnm) + 1;
3651 anm = (char *) bfd_alloc (abfd, amt);
3652 if (anm == NULL)
3653 goto error_free_dyn;
3654 memcpy (anm, fnm, amt);
3655 n->name = anm;
3656 n->by = abfd;
3657 n->next = NULL;
3658 for (pn = & rpath;
3659 *pn != NULL;
3660 pn = &(*pn)->next)
3661 ;
3662 *pn = n;
3663 }
3664 if (dyn.d_tag == DT_AUDIT)
3665 {
3666 unsigned int tagv = dyn.d_un.d_val;
3667 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3668 }
3669 }
3670
3671 free (dynbuf);
3672 }
3673
3674 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3675 frees all more recently bfd_alloc'd blocks as well. */
3676 if (runpath)
3677 rpath = runpath;
3678
3679 if (rpath)
3680 {
3681 struct bfd_link_needed_list **pn;
3682 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3683 ;
3684 *pn = rpath;
3685 }
3686
3687 /* We do not want to include any of the sections in a dynamic
3688 object in the output file. We hack by simply clobbering the
3689 list of sections in the BFD. This could be handled more
3690 cleanly by, say, a new section flag; the existing
3691 SEC_NEVER_LOAD flag is not the one we want, because that one
3692 still implies that the section takes up space in the output
3693 file. */
3694 bfd_section_list_clear (abfd);
3695
3696 /* Find the name to use in a DT_NEEDED entry that refers to this
3697 object. If the object has a DT_SONAME entry, we use it.
3698 Otherwise, if the generic linker stuck something in
3699 elf_dt_name, we use that. Otherwise, we just use the file
3700 name. */
3701 if (soname == NULL || *soname == '\0')
3702 {
3703 soname = elf_dt_name (abfd);
3704 if (soname == NULL || *soname == '\0')
3705 soname = bfd_get_filename (abfd);
3706 }
3707
3708 /* Save the SONAME because sometimes the linker emulation code
3709 will need to know it. */
3710 elf_dt_name (abfd) = soname;
3711
3712 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3713 if (ret < 0)
3714 goto error_return;
3715
3716 /* If we have already included this dynamic object in the
3717 link, just ignore it. There is no reason to include a
3718 particular dynamic object more than once. */
3719 if (ret > 0)
3720 return TRUE;
3721
3722 /* Save the DT_AUDIT entry for the linker emulation code. */
3723 elf_dt_audit (abfd) = audit;
3724 }
3725
3726 /* If this is a dynamic object, we always link against the .dynsym
3727 symbol table, not the .symtab symbol table. The dynamic linker
3728 will only see the .dynsym symbol table, so there is no reason to
3729 look at .symtab for a dynamic object. */
3730
3731 if (! dynamic || elf_dynsymtab (abfd) == 0)
3732 hdr = &elf_tdata (abfd)->symtab_hdr;
3733 else
3734 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3735
3736 symcount = hdr->sh_size / bed->s->sizeof_sym;
3737
3738 /* The sh_info field of the symtab header tells us where the
3739 external symbols start. We don't care about the local symbols at
3740 this point. */
3741 if (elf_bad_symtab (abfd))
3742 {
3743 extsymcount = symcount;
3744 extsymoff = 0;
3745 }
3746 else
3747 {
3748 extsymcount = symcount - hdr->sh_info;
3749 extsymoff = hdr->sh_info;
3750 }
3751
3752 sym_hash = elf_sym_hashes (abfd);
3753 if (extsymcount != 0)
3754 {
3755 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3756 NULL, NULL, NULL);
3757 if (isymbuf == NULL)
3758 goto error_return;
3759
3760 if (sym_hash == NULL)
3761 {
3762 /* We store a pointer to the hash table entry for each
3763 external symbol. */
3764 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3765 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
3766 if (sym_hash == NULL)
3767 goto error_free_sym;
3768 elf_sym_hashes (abfd) = sym_hash;
3769 }
3770 }
3771
3772 if (dynamic)
3773 {
3774 /* Read in any version definitions. */
3775 if (!_bfd_elf_slurp_version_tables (abfd,
3776 info->default_imported_symver))
3777 goto error_free_sym;
3778
3779 /* Read in the symbol versions, but don't bother to convert them
3780 to internal format. */
3781 if (elf_dynversym (abfd) != 0)
3782 {
3783 Elf_Internal_Shdr *versymhdr;
3784
3785 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3786 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
3787 if (extversym == NULL)
3788 goto error_free_sym;
3789 amt = versymhdr->sh_size;
3790 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3791 || bfd_bread (extversym, amt, abfd) != amt)
3792 goto error_free_vers;
3793 }
3794 }
3795
3796 /* If we are loading an as-needed shared lib, save the symbol table
3797 state before we start adding symbols. If the lib turns out
3798 to be unneeded, restore the state. */
3799 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3800 {
3801 unsigned int i;
3802 size_t entsize;
3803
3804 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3805 {
3806 struct bfd_hash_entry *p;
3807 struct elf_link_hash_entry *h;
3808
3809 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3810 {
3811 h = (struct elf_link_hash_entry *) p;
3812 entsize += htab->root.table.entsize;
3813 if (h->root.type == bfd_link_hash_warning)
3814 entsize += htab->root.table.entsize;
3815 }
3816 }
3817
3818 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
3819 old_tab = bfd_malloc (tabsize + entsize);
3820 if (old_tab == NULL)
3821 goto error_free_vers;
3822
3823 /* Remember the current objalloc pointer, so that all mem for
3824 symbols added can later be reclaimed. */
3825 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3826 if (alloc_mark == NULL)
3827 goto error_free_vers;
3828
3829 /* Make a special call to the linker "notice" function to
3830 tell it that we are about to handle an as-needed lib. */
3831 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
3832 goto error_free_vers;
3833
3834 /* Clone the symbol table. Remember some pointers into the
3835 symbol table, and dynamic symbol count. */
3836 old_ent = (char *) old_tab + tabsize;
3837 memcpy (old_tab, htab->root.table.table, tabsize);
3838 old_undefs = htab->root.undefs;
3839 old_undefs_tail = htab->root.undefs_tail;
3840 old_table = htab->root.table.table;
3841 old_size = htab->root.table.size;
3842 old_count = htab->root.table.count;
3843 old_dynsymcount = htab->dynsymcount;
3844 old_dynstr_size = _bfd_elf_strtab_size (htab->dynstr);
3845
3846 for (i = 0; i < htab->root.table.size; i++)
3847 {
3848 struct bfd_hash_entry *p;
3849 struct elf_link_hash_entry *h;
3850
3851 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3852 {
3853 memcpy (old_ent, p, htab->root.table.entsize);
3854 old_ent = (char *) old_ent + htab->root.table.entsize;
3855 h = (struct elf_link_hash_entry *) p;
3856 if (h->root.type == bfd_link_hash_warning)
3857 {
3858 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3859 old_ent = (char *) old_ent + htab->root.table.entsize;
3860 }
3861 }
3862 }
3863 }
3864
3865 weaks = NULL;
3866 ever = extversym != NULL ? extversym + extsymoff : NULL;
3867 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3868 isym < isymend;
3869 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3870 {
3871 int bind;
3872 bfd_vma value;
3873 asection *sec, *new_sec;
3874 flagword flags;
3875 const char *name;
3876 struct elf_link_hash_entry *h;
3877 struct elf_link_hash_entry *hi;
3878 bfd_boolean definition;
3879 bfd_boolean size_change_ok;
3880 bfd_boolean type_change_ok;
3881 bfd_boolean new_weakdef;
3882 bfd_boolean new_weak;
3883 bfd_boolean old_weak;
3884 bfd_boolean override;
3885 bfd_boolean common;
3886 unsigned int old_alignment;
3887 bfd *old_bfd;
3888
3889 override = FALSE;
3890
3891 flags = BSF_NO_FLAGS;
3892 sec = NULL;
3893 value = isym->st_value;
3894 common = bed->common_definition (isym);
3895
3896 bind = ELF_ST_BIND (isym->st_info);
3897 switch (bind)
3898 {
3899 case STB_LOCAL:
3900 /* This should be impossible, since ELF requires that all
3901 global symbols follow all local symbols, and that sh_info
3902 point to the first global symbol. Unfortunately, Irix 5
3903 screws this up. */
3904 continue;
3905
3906 case STB_GLOBAL:
3907 if (isym->st_shndx != SHN_UNDEF && !common)
3908 flags = BSF_GLOBAL;
3909 break;
3910
3911 case STB_WEAK:
3912 flags = BSF_WEAK;
3913 break;
3914
3915 case STB_GNU_UNIQUE:
3916 flags = BSF_GNU_UNIQUE;
3917 break;
3918
3919 default:
3920 /* Leave it up to the processor backend. */
3921 break;
3922 }
3923
3924 if (isym->st_shndx == SHN_UNDEF)
3925 sec = bfd_und_section_ptr;
3926 else if (isym->st_shndx == SHN_ABS)
3927 sec = bfd_abs_section_ptr;
3928 else if (isym->st_shndx == SHN_COMMON)
3929 {
3930 sec = bfd_com_section_ptr;
3931 /* What ELF calls the size we call the value. What ELF
3932 calls the value we call the alignment. */
3933 value = isym->st_size;
3934 }
3935 else
3936 {
3937 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3938 if (sec == NULL)
3939 sec = bfd_abs_section_ptr;
3940 else if (discarded_section (sec))
3941 {
3942 /* Symbols from discarded section are undefined. We keep
3943 its visibility. */
3944 sec = bfd_und_section_ptr;
3945 isym->st_shndx = SHN_UNDEF;
3946 }
3947 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3948 value -= sec->vma;
3949 }
3950
3951 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3952 isym->st_name);
3953 if (name == NULL)
3954 goto error_free_vers;
3955
3956 if (isym->st_shndx == SHN_COMMON
3957 && (abfd->flags & BFD_PLUGIN) != 0)
3958 {
3959 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
3960
3961 if (xc == NULL)
3962 {
3963 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
3964 | SEC_EXCLUDE);
3965 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
3966 if (xc == NULL)
3967 goto error_free_vers;
3968 }
3969 sec = xc;
3970 }
3971 else if (isym->st_shndx == SHN_COMMON
3972 && ELF_ST_TYPE (isym->st_info) == STT_TLS
3973 && !info->relocatable)
3974 {
3975 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3976
3977 if (tcomm == NULL)
3978 {
3979 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
3980 | SEC_LINKER_CREATED);
3981 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3982 if (tcomm == NULL)
3983 goto error_free_vers;
3984 }
3985 sec = tcomm;
3986 }
3987 else if (bed->elf_add_symbol_hook)
3988 {
3989 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
3990 &sec, &value))
3991 goto error_free_vers;
3992
3993 /* The hook function sets the name to NULL if this symbol
3994 should be skipped for some reason. */
3995 if (name == NULL)
3996 continue;
3997 }
3998
3999 /* Sanity check that all possibilities were handled. */
4000 if (sec == NULL)
4001 {
4002 bfd_set_error (bfd_error_bad_value);
4003 goto error_free_vers;
4004 }
4005
4006 /* Silently discard TLS symbols from --just-syms. There's
4007 no way to combine a static TLS block with a new TLS block
4008 for this executable. */
4009 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4010 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4011 continue;
4012
4013 if (bfd_is_und_section (sec)
4014 || bfd_is_com_section (sec))
4015 definition = FALSE;
4016 else
4017 definition = TRUE;
4018
4019 size_change_ok = FALSE;
4020 type_change_ok = bed->type_change_ok;
4021 old_weak = FALSE;
4022 old_alignment = 0;
4023 old_bfd = NULL;
4024 new_sec = sec;
4025
4026 if (is_elf_hash_table (htab))
4027 {
4028 Elf_Internal_Versym iver;
4029 unsigned int vernum = 0;
4030 bfd_boolean skip;
4031
4032 if (ever == NULL)
4033 {
4034 if (info->default_imported_symver)
4035 /* Use the default symbol version created earlier. */
4036 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4037 else
4038 iver.vs_vers = 0;
4039 }
4040 else
4041 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4042
4043 vernum = iver.vs_vers & VERSYM_VERSION;
4044
4045 /* If this is a hidden symbol, or if it is not version
4046 1, we append the version name to the symbol name.
4047 However, we do not modify a non-hidden absolute symbol
4048 if it is not a function, because it might be the version
4049 symbol itself. FIXME: What if it isn't? */
4050 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4051 || (vernum > 1
4052 && (!bfd_is_abs_section (sec)
4053 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4054 {
4055 const char *verstr;
4056 size_t namelen, verlen, newlen;
4057 char *newname, *p;
4058
4059 if (isym->st_shndx != SHN_UNDEF)
4060 {
4061 if (vernum > elf_tdata (abfd)->cverdefs)
4062 verstr = NULL;
4063 else if (vernum > 1)
4064 verstr =
4065 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4066 else
4067 verstr = "";
4068
4069 if (verstr == NULL)
4070 {
4071 (*_bfd_error_handler)
4072 (_("%B: %s: invalid version %u (max %d)"),
4073 abfd, name, vernum,
4074 elf_tdata (abfd)->cverdefs);
4075 bfd_set_error (bfd_error_bad_value);
4076 goto error_free_vers;
4077 }
4078 }
4079 else
4080 {
4081 /* We cannot simply test for the number of
4082 entries in the VERNEED section since the
4083 numbers for the needed versions do not start
4084 at 0. */
4085 Elf_Internal_Verneed *t;
4086
4087 verstr = NULL;
4088 for (t = elf_tdata (abfd)->verref;
4089 t != NULL;
4090 t = t->vn_nextref)
4091 {
4092 Elf_Internal_Vernaux *a;
4093
4094 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4095 {
4096 if (a->vna_other == vernum)
4097 {
4098 verstr = a->vna_nodename;
4099 break;
4100 }
4101 }
4102 if (a != NULL)
4103 break;
4104 }
4105 if (verstr == NULL)
4106 {
4107 (*_bfd_error_handler)
4108 (_("%B: %s: invalid needed version %d"),
4109 abfd, name, vernum);
4110 bfd_set_error (bfd_error_bad_value);
4111 goto error_free_vers;
4112 }
4113 }
4114
4115 namelen = strlen (name);
4116 verlen = strlen (verstr);
4117 newlen = namelen + verlen + 2;
4118 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4119 && isym->st_shndx != SHN_UNDEF)
4120 ++newlen;
4121
4122 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4123 if (newname == NULL)
4124 goto error_free_vers;
4125 memcpy (newname, name, namelen);
4126 p = newname + namelen;
4127 *p++ = ELF_VER_CHR;
4128 /* If this is a defined non-hidden version symbol,
4129 we add another @ to the name. This indicates the
4130 default version of the symbol. */
4131 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4132 && isym->st_shndx != SHN_UNDEF)
4133 *p++ = ELF_VER_CHR;
4134 memcpy (p, verstr, verlen + 1);
4135
4136 name = newname;
4137 }
4138
4139 /* If this symbol has default visibility and the user has
4140 requested we not re-export it, then mark it as hidden. */
4141 if (definition
4142 && !dynamic
4143 && abfd->no_export
4144 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4145 isym->st_other = (STV_HIDDEN
4146 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4147
4148 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4149 sym_hash, &old_bfd, &old_weak,
4150 &old_alignment, &skip, &override,
4151 &type_change_ok, &size_change_ok))
4152 goto error_free_vers;
4153
4154 if (skip)
4155 continue;
4156
4157 if (override)
4158 definition = FALSE;
4159
4160 h = *sym_hash;
4161 while (h->root.type == bfd_link_hash_indirect
4162 || h->root.type == bfd_link_hash_warning)
4163 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4164
4165 if (elf_tdata (abfd)->verdef != NULL
4166 && vernum > 1
4167 && definition)
4168 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4169 }
4170
4171 if (! (_bfd_generic_link_add_one_symbol
4172 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4173 (struct bfd_link_hash_entry **) sym_hash)))
4174 goto error_free_vers;
4175
4176 h = *sym_hash;
4177 /* We need to make sure that indirect symbol dynamic flags are
4178 updated. */
4179 hi = h;
4180 while (h->root.type == bfd_link_hash_indirect
4181 || h->root.type == bfd_link_hash_warning)
4182 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4183
4184 *sym_hash = h;
4185
4186 new_weak = (flags & BSF_WEAK) != 0;
4187 new_weakdef = FALSE;
4188 if (dynamic
4189 && definition
4190 && new_weak
4191 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4192 && is_elf_hash_table (htab)
4193 && h->u.weakdef == NULL)
4194 {
4195 /* Keep a list of all weak defined non function symbols from
4196 a dynamic object, using the weakdef field. Later in this
4197 function we will set the weakdef field to the correct
4198 value. We only put non-function symbols from dynamic
4199 objects on this list, because that happens to be the only
4200 time we need to know the normal symbol corresponding to a
4201 weak symbol, and the information is time consuming to
4202 figure out. If the weakdef field is not already NULL,
4203 then this symbol was already defined by some previous
4204 dynamic object, and we will be using that previous
4205 definition anyhow. */
4206
4207 h->u.weakdef = weaks;
4208 weaks = h;
4209 new_weakdef = TRUE;
4210 }
4211
4212 /* Set the alignment of a common symbol. */
4213 if ((common || bfd_is_com_section (sec))
4214 && h->root.type == bfd_link_hash_common)
4215 {
4216 unsigned int align;
4217
4218 if (common)
4219 align = bfd_log2 (isym->st_value);
4220 else
4221 {
4222 /* The new symbol is a common symbol in a shared object.
4223 We need to get the alignment from the section. */
4224 align = new_sec->alignment_power;
4225 }
4226 if (align > old_alignment)
4227 h->root.u.c.p->alignment_power = align;
4228 else
4229 h->root.u.c.p->alignment_power = old_alignment;
4230 }
4231
4232 if (is_elf_hash_table (htab))
4233 {
4234 /* Set a flag in the hash table entry indicating the type of
4235 reference or definition we just found. A dynamic symbol
4236 is one which is referenced or defined by both a regular
4237 object and a shared object. */
4238 bfd_boolean dynsym = FALSE;
4239
4240 /* Plugin symbols aren't normal. Don't set def_regular or
4241 ref_regular for them, or make them dynamic. */
4242 if ((abfd->flags & BFD_PLUGIN) != 0)
4243 ;
4244 else if (! dynamic)
4245 {
4246 if (! definition)
4247 {
4248 h->ref_regular = 1;
4249 if (bind != STB_WEAK)
4250 h->ref_regular_nonweak = 1;
4251 }
4252 else
4253 {
4254 h->def_regular = 1;
4255 if (h->def_dynamic)
4256 {
4257 h->def_dynamic = 0;
4258 h->ref_dynamic = 1;
4259 }
4260 }
4261
4262 /* If the indirect symbol has been forced local, don't
4263 make the real symbol dynamic. */
4264 if ((h == hi || !hi->forced_local)
4265 && ((! info->executable && ! info->relocatable)
4266 || h->def_dynamic
4267 || h->ref_dynamic))
4268 dynsym = TRUE;
4269 }
4270 else
4271 {
4272 if (! definition)
4273 {
4274 h->ref_dynamic = 1;
4275 hi->ref_dynamic = 1;
4276 }
4277 else
4278 {
4279 h->def_dynamic = 1;
4280 hi->def_dynamic = 1;
4281 }
4282
4283 /* If the indirect symbol has been forced local, don't
4284 make the real symbol dynamic. */
4285 if ((h == hi || !hi->forced_local)
4286 && (h->def_regular
4287 || h->ref_regular
4288 || (h->u.weakdef != NULL
4289 && ! new_weakdef
4290 && h->u.weakdef->dynindx != -1)))
4291 dynsym = TRUE;
4292 }
4293
4294 /* Check to see if we need to add an indirect symbol for
4295 the default name. */
4296 if (definition
4297 || (!override && h->root.type == bfd_link_hash_common))
4298 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4299 sec, value, &old_bfd, &dynsym))
4300 goto error_free_vers;
4301
4302 /* Check the alignment when a common symbol is involved. This
4303 can change when a common symbol is overridden by a normal
4304 definition or a common symbol is ignored due to the old
4305 normal definition. We need to make sure the maximum
4306 alignment is maintained. */
4307 if ((old_alignment || common)
4308 && h->root.type != bfd_link_hash_common)
4309 {
4310 unsigned int common_align;
4311 unsigned int normal_align;
4312 unsigned int symbol_align;
4313 bfd *normal_bfd;
4314 bfd *common_bfd;
4315
4316 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4317 || h->root.type == bfd_link_hash_defweak);
4318
4319 symbol_align = ffs (h->root.u.def.value) - 1;
4320 if (h->root.u.def.section->owner != NULL
4321 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4322 {
4323 normal_align = h->root.u.def.section->alignment_power;
4324 if (normal_align > symbol_align)
4325 normal_align = symbol_align;
4326 }
4327 else
4328 normal_align = symbol_align;
4329
4330 if (old_alignment)
4331 {
4332 common_align = old_alignment;
4333 common_bfd = old_bfd;
4334 normal_bfd = abfd;
4335 }
4336 else
4337 {
4338 common_align = bfd_log2 (isym->st_value);
4339 common_bfd = abfd;
4340 normal_bfd = old_bfd;
4341 }
4342
4343 if (normal_align < common_align)
4344 {
4345 /* PR binutils/2735 */
4346 if (normal_bfd == NULL)
4347 (*_bfd_error_handler)
4348 (_("Warning: alignment %u of common symbol `%s' in %B is"
4349 " greater than the alignment (%u) of its section %A"),
4350 common_bfd, h->root.u.def.section,
4351 1 << common_align, name, 1 << normal_align);
4352 else
4353 (*_bfd_error_handler)
4354 (_("Warning: alignment %u of symbol `%s' in %B"
4355 " is smaller than %u in %B"),
4356 normal_bfd, common_bfd,
4357 1 << normal_align, name, 1 << common_align);
4358 }
4359 }
4360
4361 /* Remember the symbol size if it isn't undefined. */
4362 if (isym->st_size != 0
4363 && isym->st_shndx != SHN_UNDEF
4364 && (definition || h->size == 0))
4365 {
4366 if (h->size != 0
4367 && h->size != isym->st_size
4368 && ! size_change_ok)
4369 (*_bfd_error_handler)
4370 (_("Warning: size of symbol `%s' changed"
4371 " from %lu in %B to %lu in %B"),
4372 old_bfd, abfd,
4373 name, (unsigned long) h->size,
4374 (unsigned long) isym->st_size);
4375
4376 h->size = isym->st_size;
4377 }
4378
4379 /* If this is a common symbol, then we always want H->SIZE
4380 to be the size of the common symbol. The code just above
4381 won't fix the size if a common symbol becomes larger. We
4382 don't warn about a size change here, because that is
4383 covered by --warn-common. Allow changes between different
4384 function types. */
4385 if (h->root.type == bfd_link_hash_common)
4386 h->size = h->root.u.c.size;
4387
4388 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4389 && ((definition && !new_weak)
4390 || (old_weak && h->root.type == bfd_link_hash_common)
4391 || h->type == STT_NOTYPE))
4392 {
4393 unsigned int type = ELF_ST_TYPE (isym->st_info);
4394
4395 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4396 symbol. */
4397 if (type == STT_GNU_IFUNC
4398 && (abfd->flags & DYNAMIC) != 0)
4399 type = STT_FUNC;
4400
4401 if (h->type != type)
4402 {
4403 if (h->type != STT_NOTYPE && ! type_change_ok)
4404 (*_bfd_error_handler)
4405 (_("Warning: type of symbol `%s' changed"
4406 " from %d to %d in %B"),
4407 abfd, name, h->type, type);
4408
4409 h->type = type;
4410 }
4411 }
4412
4413 /* Merge st_other field. */
4414 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4415
4416 /* We don't want to make debug symbol dynamic. */
4417 if (definition && (sec->flags & SEC_DEBUGGING) && !info->relocatable)
4418 dynsym = FALSE;
4419
4420 /* Nor should we make plugin symbols dynamic. */
4421 if ((abfd->flags & BFD_PLUGIN) != 0)
4422 dynsym = FALSE;
4423
4424 if (definition)
4425 {
4426 h->target_internal = isym->st_target_internal;
4427 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4428 }
4429
4430 if (definition && !dynamic)
4431 {
4432 char *p = strchr (name, ELF_VER_CHR);
4433 if (p != NULL && p[1] != ELF_VER_CHR)
4434 {
4435 /* Queue non-default versions so that .symver x, x@FOO
4436 aliases can be checked. */
4437 if (!nondeflt_vers)
4438 {
4439 amt = ((isymend - isym + 1)
4440 * sizeof (struct elf_link_hash_entry *));
4441 nondeflt_vers
4442 = (struct elf_link_hash_entry **) bfd_malloc (amt);
4443 if (!nondeflt_vers)
4444 goto error_free_vers;
4445 }
4446 nondeflt_vers[nondeflt_vers_cnt++] = h;
4447 }
4448 }
4449
4450 if (dynsym && h->dynindx == -1)
4451 {
4452 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4453 goto error_free_vers;
4454 if (h->u.weakdef != NULL
4455 && ! new_weakdef
4456 && h->u.weakdef->dynindx == -1)
4457 {
4458 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4459 goto error_free_vers;
4460 }
4461 }
4462 else if (dynsym && h->dynindx != -1)
4463 /* If the symbol already has a dynamic index, but
4464 visibility says it should not be visible, turn it into
4465 a local symbol. */
4466 switch (ELF_ST_VISIBILITY (h->other))
4467 {
4468 case STV_INTERNAL:
4469 case STV_HIDDEN:
4470 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4471 dynsym = FALSE;
4472 break;
4473 }
4474
4475 /* Don't add DT_NEEDED for references from the dummy bfd. */
4476 if (!add_needed
4477 && definition
4478 && ((dynsym
4479 && h->ref_regular_nonweak
4480 && (old_bfd == NULL
4481 || (old_bfd->flags & BFD_PLUGIN) == 0))
4482 || (h->ref_dynamic_nonweak
4483 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4484 && !on_needed_list (elf_dt_name (abfd), htab->needed))))
4485 {
4486 int ret;
4487 const char *soname = elf_dt_name (abfd);
4488
4489 info->callbacks->minfo ("%!", soname, old_bfd,
4490 h->root.root.string);
4491
4492 /* A symbol from a library loaded via DT_NEEDED of some
4493 other library is referenced by a regular object.
4494 Add a DT_NEEDED entry for it. Issue an error if
4495 --no-add-needed is used and the reference was not
4496 a weak one. */
4497 if (old_bfd != NULL
4498 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4499 {
4500 (*_bfd_error_handler)
4501 (_("%B: undefined reference to symbol '%s'"),
4502 old_bfd, name);
4503 bfd_set_error (bfd_error_missing_dso);
4504 goto error_free_vers;
4505 }
4506
4507 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4508 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
4509
4510 add_needed = TRUE;
4511 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4512 if (ret < 0)
4513 goto error_free_vers;
4514
4515 BFD_ASSERT (ret == 0);
4516 }
4517 }
4518 }
4519
4520 if (extversym != NULL)
4521 {
4522 free (extversym);
4523 extversym = NULL;
4524 }
4525
4526 if (isymbuf != NULL)
4527 {
4528 free (isymbuf);
4529 isymbuf = NULL;
4530 }
4531
4532 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4533 {
4534 unsigned int i;
4535
4536 /* Restore the symbol table. */
4537 old_ent = (char *) old_tab + tabsize;
4538 memset (elf_sym_hashes (abfd), 0,
4539 extsymcount * sizeof (struct elf_link_hash_entry *));
4540 htab->root.table.table = old_table;
4541 htab->root.table.size = old_size;
4542 htab->root.table.count = old_count;
4543 memcpy (htab->root.table.table, old_tab, tabsize);
4544 htab->root.undefs = old_undefs;
4545 htab->root.undefs_tail = old_undefs_tail;
4546 _bfd_elf_strtab_restore_size (htab->dynstr, old_dynstr_size);
4547 for (i = 0; i < htab->root.table.size; i++)
4548 {
4549 struct bfd_hash_entry *p;
4550 struct elf_link_hash_entry *h;
4551 bfd_size_type size;
4552 unsigned int alignment_power;
4553
4554 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4555 {
4556 h = (struct elf_link_hash_entry *) p;
4557 if (h->root.type == bfd_link_hash_warning)
4558 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4559 if (h->dynindx >= old_dynsymcount
4560 && h->dynstr_index < old_dynstr_size)
4561 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
4562
4563 /* Preserve the maximum alignment and size for common
4564 symbols even if this dynamic lib isn't on DT_NEEDED
4565 since it can still be loaded at run time by another
4566 dynamic lib. */
4567 if (h->root.type == bfd_link_hash_common)
4568 {
4569 size = h->root.u.c.size;
4570 alignment_power = h->root.u.c.p->alignment_power;
4571 }
4572 else
4573 {
4574 size = 0;
4575 alignment_power = 0;
4576 }
4577 memcpy (p, old_ent, htab->root.table.entsize);
4578 old_ent = (char *) old_ent + htab->root.table.entsize;
4579 h = (struct elf_link_hash_entry *) p;
4580 if (h->root.type == bfd_link_hash_warning)
4581 {
4582 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4583 old_ent = (char *) old_ent + htab->root.table.entsize;
4584 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4585 }
4586 if (h->root.type == bfd_link_hash_common)
4587 {
4588 if (size > h->root.u.c.size)
4589 h->root.u.c.size = size;
4590 if (alignment_power > h->root.u.c.p->alignment_power)
4591 h->root.u.c.p->alignment_power = alignment_power;
4592 }
4593 }
4594 }
4595
4596 /* Make a special call to the linker "notice" function to
4597 tell it that symbols added for crefs may need to be removed. */
4598 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
4599 goto error_free_vers;
4600
4601 free (old_tab);
4602 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4603 alloc_mark);
4604 if (nondeflt_vers != NULL)
4605 free (nondeflt_vers);
4606 return TRUE;
4607 }
4608
4609 if (old_tab != NULL)
4610 {
4611 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
4612 goto error_free_vers;
4613 free (old_tab);
4614 old_tab = NULL;
4615 }
4616
4617 /* Now that all the symbols from this input file are created, if
4618 not performing a relocatable link, handle .symver foo, foo@BAR
4619 such that any relocs against foo become foo@BAR. */
4620 if (!info->relocatable && nondeflt_vers != NULL)
4621 {
4622 bfd_size_type cnt, symidx;
4623
4624 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4625 {
4626 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4627 char *shortname, *p;
4628
4629 p = strchr (h->root.root.string, ELF_VER_CHR);
4630 if (p == NULL
4631 || (h->root.type != bfd_link_hash_defined
4632 && h->root.type != bfd_link_hash_defweak))
4633 continue;
4634
4635 amt = p - h->root.root.string;
4636 shortname = (char *) bfd_malloc (amt + 1);
4637 if (!shortname)
4638 goto error_free_vers;
4639 memcpy (shortname, h->root.root.string, amt);
4640 shortname[amt] = '\0';
4641
4642 hi = (struct elf_link_hash_entry *)
4643 bfd_link_hash_lookup (&htab->root, shortname,
4644 FALSE, FALSE, FALSE);
4645 if (hi != NULL
4646 && hi->root.type == h->root.type
4647 && hi->root.u.def.value == h->root.u.def.value
4648 && hi->root.u.def.section == h->root.u.def.section)
4649 {
4650 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4651 hi->root.type = bfd_link_hash_indirect;
4652 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4653 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4654 sym_hash = elf_sym_hashes (abfd);
4655 if (sym_hash)
4656 for (symidx = 0; symidx < extsymcount; ++symidx)
4657 if (sym_hash[symidx] == hi)
4658 {
4659 sym_hash[symidx] = h;
4660 break;
4661 }
4662 }
4663 free (shortname);
4664 }
4665 free (nondeflt_vers);
4666 nondeflt_vers = NULL;
4667 }
4668
4669 /* Now set the weakdefs field correctly for all the weak defined
4670 symbols we found. The only way to do this is to search all the
4671 symbols. Since we only need the information for non functions in
4672 dynamic objects, that's the only time we actually put anything on
4673 the list WEAKS. We need this information so that if a regular
4674 object refers to a symbol defined weakly in a dynamic object, the
4675 real symbol in the dynamic object is also put in the dynamic
4676 symbols; we also must arrange for both symbols to point to the
4677 same memory location. We could handle the general case of symbol
4678 aliasing, but a general symbol alias can only be generated in
4679 assembler code, handling it correctly would be very time
4680 consuming, and other ELF linkers don't handle general aliasing
4681 either. */
4682 if (weaks != NULL)
4683 {
4684 struct elf_link_hash_entry **hpp;
4685 struct elf_link_hash_entry **hppend;
4686 struct elf_link_hash_entry **sorted_sym_hash;
4687 struct elf_link_hash_entry *h;
4688 size_t sym_count;
4689
4690 /* Since we have to search the whole symbol list for each weak
4691 defined symbol, search time for N weak defined symbols will be
4692 O(N^2). Binary search will cut it down to O(NlogN). */
4693 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4694 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4695 if (sorted_sym_hash == NULL)
4696 goto error_return;
4697 sym_hash = sorted_sym_hash;
4698 hpp = elf_sym_hashes (abfd);
4699 hppend = hpp + extsymcount;
4700 sym_count = 0;
4701 for (; hpp < hppend; hpp++)
4702 {
4703 h = *hpp;
4704 if (h != NULL
4705 && h->root.type == bfd_link_hash_defined
4706 && !bed->is_function_type (h->type))
4707 {
4708 *sym_hash = h;
4709 sym_hash++;
4710 sym_count++;
4711 }
4712 }
4713
4714 qsort (sorted_sym_hash, sym_count,
4715 sizeof (struct elf_link_hash_entry *),
4716 elf_sort_symbol);
4717
4718 while (weaks != NULL)
4719 {
4720 struct elf_link_hash_entry *hlook;
4721 asection *slook;
4722 bfd_vma vlook;
4723 size_t i, j, idx = 0;
4724
4725 hlook = weaks;
4726 weaks = hlook->u.weakdef;
4727 hlook->u.weakdef = NULL;
4728
4729 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4730 || hlook->root.type == bfd_link_hash_defweak
4731 || hlook->root.type == bfd_link_hash_common
4732 || hlook->root.type == bfd_link_hash_indirect);
4733 slook = hlook->root.u.def.section;
4734 vlook = hlook->root.u.def.value;
4735
4736 i = 0;
4737 j = sym_count;
4738 while (i != j)
4739 {
4740 bfd_signed_vma vdiff;
4741 idx = (i + j) / 2;
4742 h = sorted_sym_hash[idx];
4743 vdiff = vlook - h->root.u.def.value;
4744 if (vdiff < 0)
4745 j = idx;
4746 else if (vdiff > 0)
4747 i = idx + 1;
4748 else
4749 {
4750 long sdiff = slook->id - h->root.u.def.section->id;
4751 if (sdiff < 0)
4752 j = idx;
4753 else if (sdiff > 0)
4754 i = idx + 1;
4755 else
4756 break;
4757 }
4758 }
4759
4760 /* We didn't find a value/section match. */
4761 if (i == j)
4762 continue;
4763
4764 /* With multiple aliases, or when the weak symbol is already
4765 strongly defined, we have multiple matching symbols and
4766 the binary search above may land on any of them. Step
4767 one past the matching symbol(s). */
4768 while (++idx != j)
4769 {
4770 h = sorted_sym_hash[idx];
4771 if (h->root.u.def.section != slook
4772 || h->root.u.def.value != vlook)
4773 break;
4774 }
4775
4776 /* Now look back over the aliases. Since we sorted by size
4777 as well as value and section, we'll choose the one with
4778 the largest size. */
4779 while (idx-- != i)
4780 {
4781 h = sorted_sym_hash[idx];
4782
4783 /* Stop if value or section doesn't match. */
4784 if (h->root.u.def.section != slook
4785 || h->root.u.def.value != vlook)
4786 break;
4787 else if (h != hlook)
4788 {
4789 hlook->u.weakdef = h;
4790
4791 /* If the weak definition is in the list of dynamic
4792 symbols, make sure the real definition is put
4793 there as well. */
4794 if (hlook->dynindx != -1 && h->dynindx == -1)
4795 {
4796 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4797 {
4798 err_free_sym_hash:
4799 free (sorted_sym_hash);
4800 goto error_return;
4801 }
4802 }
4803
4804 /* If the real definition is in the list of dynamic
4805 symbols, make sure the weak definition is put
4806 there as well. If we don't do this, then the
4807 dynamic loader might not merge the entries for the
4808 real definition and the weak definition. */
4809 if (h->dynindx != -1 && hlook->dynindx == -1)
4810 {
4811 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4812 goto err_free_sym_hash;
4813 }
4814 break;
4815 }
4816 }
4817 }
4818
4819 free (sorted_sym_hash);
4820 }
4821
4822 if (bed->check_directives
4823 && !(*bed->check_directives) (abfd, info))
4824 return FALSE;
4825
4826 /* If this object is the same format as the output object, and it is
4827 not a shared library, then let the backend look through the
4828 relocs.
4829
4830 This is required to build global offset table entries and to
4831 arrange for dynamic relocs. It is not required for the
4832 particular common case of linking non PIC code, even when linking
4833 against shared libraries, but unfortunately there is no way of
4834 knowing whether an object file has been compiled PIC or not.
4835 Looking through the relocs is not particularly time consuming.
4836 The problem is that we must either (1) keep the relocs in memory,
4837 which causes the linker to require additional runtime memory or
4838 (2) read the relocs twice from the input file, which wastes time.
4839 This would be a good case for using mmap.
4840
4841 I have no idea how to handle linking PIC code into a file of a
4842 different format. It probably can't be done. */
4843 if (! dynamic
4844 && is_elf_hash_table (htab)
4845 && bed->check_relocs != NULL
4846 && elf_object_id (abfd) == elf_hash_table_id (htab)
4847 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4848 {
4849 asection *o;
4850
4851 for (o = abfd->sections; o != NULL; o = o->next)
4852 {
4853 Elf_Internal_Rela *internal_relocs;
4854 bfd_boolean ok;
4855
4856 if ((o->flags & SEC_RELOC) == 0
4857 || o->reloc_count == 0
4858 || ((info->strip == strip_all || info->strip == strip_debugger)
4859 && (o->flags & SEC_DEBUGGING) != 0)
4860 || bfd_is_abs_section (o->output_section))
4861 continue;
4862
4863 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4864 info->keep_memory);
4865 if (internal_relocs == NULL)
4866 goto error_return;
4867
4868 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4869
4870 if (elf_section_data (o)->relocs != internal_relocs)
4871 free (internal_relocs);
4872
4873 if (! ok)
4874 goto error_return;
4875 }
4876 }
4877
4878 /* If this is a non-traditional link, try to optimize the handling
4879 of the .stab/.stabstr sections. */
4880 if (! dynamic
4881 && ! info->traditional_format
4882 && is_elf_hash_table (htab)
4883 && (info->strip != strip_all && info->strip != strip_debugger))
4884 {
4885 asection *stabstr;
4886
4887 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4888 if (stabstr != NULL)
4889 {
4890 bfd_size_type string_offset = 0;
4891 asection *stab;
4892
4893 for (stab = abfd->sections; stab; stab = stab->next)
4894 if (CONST_STRNEQ (stab->name, ".stab")
4895 && (!stab->name[5] ||
4896 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4897 && (stab->flags & SEC_MERGE) == 0
4898 && !bfd_is_abs_section (stab->output_section))
4899 {
4900 struct bfd_elf_section_data *secdata;
4901
4902 secdata = elf_section_data (stab);
4903 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
4904 stabstr, &secdata->sec_info,
4905 &string_offset))
4906 goto error_return;
4907 if (secdata->sec_info)
4908 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4909 }
4910 }
4911 }
4912
4913 if (is_elf_hash_table (htab) && add_needed)
4914 {
4915 /* Add this bfd to the loaded list. */
4916 struct elf_link_loaded_list *n;
4917
4918 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4919 if (n == NULL)
4920 goto error_return;
4921 n->abfd = abfd;
4922 n->next = htab->loaded;
4923 htab->loaded = n;
4924 }
4925
4926 return TRUE;
4927
4928 error_free_vers:
4929 if (old_tab != NULL)
4930 free (old_tab);
4931 if (nondeflt_vers != NULL)
4932 free (nondeflt_vers);
4933 if (extversym != NULL)
4934 free (extversym);
4935 error_free_sym:
4936 if (isymbuf != NULL)
4937 free (isymbuf);
4938 error_return:
4939 return FALSE;
4940 }
4941
4942 /* Return the linker hash table entry of a symbol that might be
4943 satisfied by an archive symbol. Return -1 on error. */
4944
4945 struct elf_link_hash_entry *
4946 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4947 struct bfd_link_info *info,
4948 const char *name)
4949 {
4950 struct elf_link_hash_entry *h;
4951 char *p, *copy;
4952 size_t len, first;
4953
4954 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
4955 if (h != NULL)
4956 return h;
4957
4958 /* If this is a default version (the name contains @@), look up the
4959 symbol again with only one `@' as well as without the version.
4960 The effect is that references to the symbol with and without the
4961 version will be matched by the default symbol in the archive. */
4962
4963 p = strchr (name, ELF_VER_CHR);
4964 if (p == NULL || p[1] != ELF_VER_CHR)
4965 return h;
4966
4967 /* First check with only one `@'. */
4968 len = strlen (name);
4969 copy = (char *) bfd_alloc (abfd, len);
4970 if (copy == NULL)
4971 return (struct elf_link_hash_entry *) 0 - 1;
4972
4973 first = p - name + 1;
4974 memcpy (copy, name, first);
4975 memcpy (copy + first, name + first + 1, len - first);
4976
4977 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
4978 if (h == NULL)
4979 {
4980 /* We also need to check references to the symbol without the
4981 version. */
4982 copy[first - 1] = '\0';
4983 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4984 FALSE, FALSE, TRUE);
4985 }
4986
4987 bfd_release (abfd, copy);
4988 return h;
4989 }
4990
4991 /* Add symbols from an ELF archive file to the linker hash table. We
4992 don't use _bfd_generic_link_add_archive_symbols because we need to
4993 handle versioned symbols.
4994
4995 Fortunately, ELF archive handling is simpler than that done by
4996 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4997 oddities. In ELF, if we find a symbol in the archive map, and the
4998 symbol is currently undefined, we know that we must pull in that
4999 object file.
5000
5001 Unfortunately, we do have to make multiple passes over the symbol
5002 table until nothing further is resolved. */
5003
5004 static bfd_boolean
5005 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5006 {
5007 symindex c;
5008 unsigned char *included = NULL;
5009 carsym *symdefs;
5010 bfd_boolean loop;
5011 bfd_size_type amt;
5012 const struct elf_backend_data *bed;
5013 struct elf_link_hash_entry * (*archive_symbol_lookup)
5014 (bfd *, struct bfd_link_info *, const char *);
5015
5016 if (! bfd_has_map (abfd))
5017 {
5018 /* An empty archive is a special case. */
5019 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5020 return TRUE;
5021 bfd_set_error (bfd_error_no_armap);
5022 return FALSE;
5023 }
5024
5025 /* Keep track of all symbols we know to be already defined, and all
5026 files we know to be already included. This is to speed up the
5027 second and subsequent passes. */
5028 c = bfd_ardata (abfd)->symdef_count;
5029 if (c == 0)
5030 return TRUE;
5031 amt = c;
5032 amt *= sizeof (*included);
5033 included = (unsigned char *) bfd_zmalloc (amt);
5034 if (included == NULL)
5035 return FALSE;
5036
5037 symdefs = bfd_ardata (abfd)->symdefs;
5038 bed = get_elf_backend_data (abfd);
5039 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5040
5041 do
5042 {
5043 file_ptr last;
5044 symindex i;
5045 carsym *symdef;
5046 carsym *symdefend;
5047
5048 loop = FALSE;
5049 last = -1;
5050
5051 symdef = symdefs;
5052 symdefend = symdef + c;
5053 for (i = 0; symdef < symdefend; symdef++, i++)
5054 {
5055 struct elf_link_hash_entry *h;
5056 bfd *element;
5057 struct bfd_link_hash_entry *undefs_tail;
5058 symindex mark;
5059
5060 if (included[i])
5061 continue;
5062 if (symdef->file_offset == last)
5063 {
5064 included[i] = TRUE;
5065 continue;
5066 }
5067
5068 h = archive_symbol_lookup (abfd, info, symdef->name);
5069 if (h == (struct elf_link_hash_entry *) 0 - 1)
5070 goto error_return;
5071
5072 if (h == NULL)
5073 continue;
5074
5075 if (h->root.type == bfd_link_hash_common)
5076 {
5077 /* We currently have a common symbol. The archive map contains
5078 a reference to this symbol, so we may want to include it. We
5079 only want to include it however, if this archive element
5080 contains a definition of the symbol, not just another common
5081 declaration of it.
5082
5083 Unfortunately some archivers (including GNU ar) will put
5084 declarations of common symbols into their archive maps, as
5085 well as real definitions, so we cannot just go by the archive
5086 map alone. Instead we must read in the element's symbol
5087 table and check that to see what kind of symbol definition
5088 this is. */
5089 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5090 continue;
5091 }
5092 else if (h->root.type != bfd_link_hash_undefined)
5093 {
5094 if (h->root.type != bfd_link_hash_undefweak)
5095 /* Symbol must be defined. Don't check it again. */
5096 included[i] = TRUE;
5097 continue;
5098 }
5099
5100 /* We need to include this archive member. */
5101 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5102 if (element == NULL)
5103 goto error_return;
5104
5105 if (! bfd_check_format (element, bfd_object))
5106 goto error_return;
5107
5108 undefs_tail = info->hash->undefs_tail;
5109
5110 if (!(*info->callbacks
5111 ->add_archive_element) (info, element, symdef->name, &element))
5112 goto error_return;
5113 if (!bfd_link_add_symbols (element, info))
5114 goto error_return;
5115
5116 /* If there are any new undefined symbols, we need to make
5117 another pass through the archive in order to see whether
5118 they can be defined. FIXME: This isn't perfect, because
5119 common symbols wind up on undefs_tail and because an
5120 undefined symbol which is defined later on in this pass
5121 does not require another pass. This isn't a bug, but it
5122 does make the code less efficient than it could be. */
5123 if (undefs_tail != info->hash->undefs_tail)
5124 loop = TRUE;
5125
5126 /* Look backward to mark all symbols from this object file
5127 which we have already seen in this pass. */
5128 mark = i;
5129 do
5130 {
5131 included[mark] = TRUE;
5132 if (mark == 0)
5133 break;
5134 --mark;
5135 }
5136 while (symdefs[mark].file_offset == symdef->file_offset);
5137
5138 /* We mark subsequent symbols from this object file as we go
5139 on through the loop. */
5140 last = symdef->file_offset;
5141 }
5142 }
5143 while (loop);
5144
5145 free (included);
5146
5147 return TRUE;
5148
5149 error_return:
5150 if (included != NULL)
5151 free (included);
5152 return FALSE;
5153 }
5154
5155 /* Given an ELF BFD, add symbols to the global hash table as
5156 appropriate. */
5157
5158 bfd_boolean
5159 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5160 {
5161 switch (bfd_get_format (abfd))
5162 {
5163 case bfd_object:
5164 return elf_link_add_object_symbols (abfd, info);
5165 case bfd_archive:
5166 return elf_link_add_archive_symbols (abfd, info);
5167 default:
5168 bfd_set_error (bfd_error_wrong_format);
5169 return FALSE;
5170 }
5171 }
5172 \f
5173 struct hash_codes_info
5174 {
5175 unsigned long *hashcodes;
5176 bfd_boolean error;
5177 };
5178
5179 /* This function will be called though elf_link_hash_traverse to store
5180 all hash value of the exported symbols in an array. */
5181
5182 static bfd_boolean
5183 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5184 {
5185 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5186 const char *name;
5187 char *p;
5188 unsigned long ha;
5189 char *alc = NULL;
5190
5191 /* Ignore indirect symbols. These are added by the versioning code. */
5192 if (h->dynindx == -1)
5193 return TRUE;
5194
5195 name = h->root.root.string;
5196 p = strchr (name, ELF_VER_CHR);
5197 if (p != NULL)
5198 {
5199 alc = (char *) bfd_malloc (p - name + 1);
5200 if (alc == NULL)
5201 {
5202 inf->error = TRUE;
5203 return FALSE;
5204 }
5205 memcpy (alc, name, p - name);
5206 alc[p - name] = '\0';
5207 name = alc;
5208 }
5209
5210 /* Compute the hash value. */
5211 ha = bfd_elf_hash (name);
5212
5213 /* Store the found hash value in the array given as the argument. */
5214 *(inf->hashcodes)++ = ha;
5215
5216 /* And store it in the struct so that we can put it in the hash table
5217 later. */
5218 h->u.elf_hash_value = ha;
5219
5220 if (alc != NULL)
5221 free (alc);
5222
5223 return TRUE;
5224 }
5225
5226 struct collect_gnu_hash_codes
5227 {
5228 bfd *output_bfd;
5229 const struct elf_backend_data *bed;
5230 unsigned long int nsyms;
5231 unsigned long int maskbits;
5232 unsigned long int *hashcodes;
5233 unsigned long int *hashval;
5234 unsigned long int *indx;
5235 unsigned long int *counts;
5236 bfd_vma *bitmask;
5237 bfd_byte *contents;
5238 long int min_dynindx;
5239 unsigned long int bucketcount;
5240 unsigned long int symindx;
5241 long int local_indx;
5242 long int shift1, shift2;
5243 unsigned long int mask;
5244 bfd_boolean error;
5245 };
5246
5247 /* This function will be called though elf_link_hash_traverse to store
5248 all hash value of the exported symbols in an array. */
5249
5250 static bfd_boolean
5251 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5252 {
5253 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5254 const char *name;
5255 char *p;
5256 unsigned long ha;
5257 char *alc = NULL;
5258
5259 /* Ignore indirect symbols. These are added by the versioning code. */
5260 if (h->dynindx == -1)
5261 return TRUE;
5262
5263 /* Ignore also local symbols and undefined symbols. */
5264 if (! (*s->bed->elf_hash_symbol) (h))
5265 return TRUE;
5266
5267 name = h->root.root.string;
5268 p = strchr (name, ELF_VER_CHR);
5269 if (p != NULL)
5270 {
5271 alc = (char *) bfd_malloc (p - name + 1);
5272 if (alc == NULL)
5273 {
5274 s->error = TRUE;
5275 return FALSE;
5276 }
5277 memcpy (alc, name, p - name);
5278 alc[p - name] = '\0';
5279 name = alc;
5280 }
5281
5282 /* Compute the hash value. */
5283 ha = bfd_elf_gnu_hash (name);
5284
5285 /* Store the found hash value in the array for compute_bucket_count,
5286 and also for .dynsym reordering purposes. */
5287 s->hashcodes[s->nsyms] = ha;
5288 s->hashval[h->dynindx] = ha;
5289 ++s->nsyms;
5290 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5291 s->min_dynindx = h->dynindx;
5292
5293 if (alc != NULL)
5294 free (alc);
5295
5296 return TRUE;
5297 }
5298
5299 /* This function will be called though elf_link_hash_traverse to do
5300 final dynaminc symbol renumbering. */
5301
5302 static bfd_boolean
5303 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5304 {
5305 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5306 unsigned long int bucket;
5307 unsigned long int val;
5308
5309 /* Ignore indirect symbols. */
5310 if (h->dynindx == -1)
5311 return TRUE;
5312
5313 /* Ignore also local symbols and undefined symbols. */
5314 if (! (*s->bed->elf_hash_symbol) (h))
5315 {
5316 if (h->dynindx >= s->min_dynindx)
5317 h->dynindx = s->local_indx++;
5318 return TRUE;
5319 }
5320
5321 bucket = s->hashval[h->dynindx] % s->bucketcount;
5322 val = (s->hashval[h->dynindx] >> s->shift1)
5323 & ((s->maskbits >> s->shift1) - 1);
5324 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5325 s->bitmask[val]
5326 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5327 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5328 if (s->counts[bucket] == 1)
5329 /* Last element terminates the chain. */
5330 val |= 1;
5331 bfd_put_32 (s->output_bfd, val,
5332 s->contents + (s->indx[bucket] - s->symindx) * 4);
5333 --s->counts[bucket];
5334 h->dynindx = s->indx[bucket]++;
5335 return TRUE;
5336 }
5337
5338 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5339
5340 bfd_boolean
5341 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5342 {
5343 return !(h->forced_local
5344 || h->root.type == bfd_link_hash_undefined
5345 || h->root.type == bfd_link_hash_undefweak
5346 || ((h->root.type == bfd_link_hash_defined
5347 || h->root.type == bfd_link_hash_defweak)
5348 && h->root.u.def.section->output_section == NULL));
5349 }
5350
5351 /* Array used to determine the number of hash table buckets to use
5352 based on the number of symbols there are. If there are fewer than
5353 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5354 fewer than 37 we use 17 buckets, and so forth. We never use more
5355 than 32771 buckets. */
5356
5357 static const size_t elf_buckets[] =
5358 {
5359 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5360 16411, 32771, 0
5361 };
5362
5363 /* Compute bucket count for hashing table. We do not use a static set
5364 of possible tables sizes anymore. Instead we determine for all
5365 possible reasonable sizes of the table the outcome (i.e., the
5366 number of collisions etc) and choose the best solution. The
5367 weighting functions are not too simple to allow the table to grow
5368 without bounds. Instead one of the weighting factors is the size.
5369 Therefore the result is always a good payoff between few collisions
5370 (= short chain lengths) and table size. */
5371 static size_t
5372 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5373 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5374 unsigned long int nsyms,
5375 int gnu_hash)
5376 {
5377 size_t best_size = 0;
5378 unsigned long int i;
5379
5380 /* We have a problem here. The following code to optimize the table
5381 size requires an integer type with more the 32 bits. If
5382 BFD_HOST_U_64_BIT is set we know about such a type. */
5383 #ifdef BFD_HOST_U_64_BIT
5384 if (info->optimize)
5385 {
5386 size_t minsize;
5387 size_t maxsize;
5388 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5389 bfd *dynobj = elf_hash_table (info)->dynobj;
5390 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5391 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5392 unsigned long int *counts;
5393 bfd_size_type amt;
5394 unsigned int no_improvement_count = 0;
5395
5396 /* Possible optimization parameters: if we have NSYMS symbols we say
5397 that the hashing table must at least have NSYMS/4 and at most
5398 2*NSYMS buckets. */
5399 minsize = nsyms / 4;
5400 if (minsize == 0)
5401 minsize = 1;
5402 best_size = maxsize = nsyms * 2;
5403 if (gnu_hash)
5404 {
5405 if (minsize < 2)
5406 minsize = 2;
5407 if ((best_size & 31) == 0)
5408 ++best_size;
5409 }
5410
5411 /* Create array where we count the collisions in. We must use bfd_malloc
5412 since the size could be large. */
5413 amt = maxsize;
5414 amt *= sizeof (unsigned long int);
5415 counts = (unsigned long int *) bfd_malloc (amt);
5416 if (counts == NULL)
5417 return 0;
5418
5419 /* Compute the "optimal" size for the hash table. The criteria is a
5420 minimal chain length. The minor criteria is (of course) the size
5421 of the table. */
5422 for (i = minsize; i < maxsize; ++i)
5423 {
5424 /* Walk through the array of hashcodes and count the collisions. */
5425 BFD_HOST_U_64_BIT max;
5426 unsigned long int j;
5427 unsigned long int fact;
5428
5429 if (gnu_hash && (i & 31) == 0)
5430 continue;
5431
5432 memset (counts, '\0', i * sizeof (unsigned long int));
5433
5434 /* Determine how often each hash bucket is used. */
5435 for (j = 0; j < nsyms; ++j)
5436 ++counts[hashcodes[j] % i];
5437
5438 /* For the weight function we need some information about the
5439 pagesize on the target. This is information need not be 100%
5440 accurate. Since this information is not available (so far) we
5441 define it here to a reasonable default value. If it is crucial
5442 to have a better value some day simply define this value. */
5443 # ifndef BFD_TARGET_PAGESIZE
5444 # define BFD_TARGET_PAGESIZE (4096)
5445 # endif
5446
5447 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5448 and the chains. */
5449 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5450
5451 # if 1
5452 /* Variant 1: optimize for short chains. We add the squares
5453 of all the chain lengths (which favors many small chain
5454 over a few long chains). */
5455 for (j = 0; j < i; ++j)
5456 max += counts[j] * counts[j];
5457
5458 /* This adds penalties for the overall size of the table. */
5459 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5460 max *= fact * fact;
5461 # else
5462 /* Variant 2: Optimize a lot more for small table. Here we
5463 also add squares of the size but we also add penalties for
5464 empty slots (the +1 term). */
5465 for (j = 0; j < i; ++j)
5466 max += (1 + counts[j]) * (1 + counts[j]);
5467
5468 /* The overall size of the table is considered, but not as
5469 strong as in variant 1, where it is squared. */
5470 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5471 max *= fact;
5472 # endif
5473
5474 /* Compare with current best results. */
5475 if (max < best_chlen)
5476 {
5477 best_chlen = max;
5478 best_size = i;
5479 no_improvement_count = 0;
5480 }
5481 /* PR 11843: Avoid futile long searches for the best bucket size
5482 when there are a large number of symbols. */
5483 else if (++no_improvement_count == 100)
5484 break;
5485 }
5486
5487 free (counts);
5488 }
5489 else
5490 #endif /* defined (BFD_HOST_U_64_BIT) */
5491 {
5492 /* This is the fallback solution if no 64bit type is available or if we
5493 are not supposed to spend much time on optimizations. We select the
5494 bucket count using a fixed set of numbers. */
5495 for (i = 0; elf_buckets[i] != 0; i++)
5496 {
5497 best_size = elf_buckets[i];
5498 if (nsyms < elf_buckets[i + 1])
5499 break;
5500 }
5501 if (gnu_hash && best_size < 2)
5502 best_size = 2;
5503 }
5504
5505 return best_size;
5506 }
5507
5508 /* Size any SHT_GROUP section for ld -r. */
5509
5510 bfd_boolean
5511 _bfd_elf_size_group_sections (struct bfd_link_info *info)
5512 {
5513 bfd *ibfd;
5514
5515 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5516 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5517 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5518 return FALSE;
5519 return TRUE;
5520 }
5521
5522 /* Set a default stack segment size. The value in INFO wins. If it
5523 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5524 undefined it is initialized. */
5525
5526 bfd_boolean
5527 bfd_elf_stack_segment_size (bfd *output_bfd,
5528 struct bfd_link_info *info,
5529 const char *legacy_symbol,
5530 bfd_vma default_size)
5531 {
5532 struct elf_link_hash_entry *h = NULL;
5533
5534 /* Look for legacy symbol. */
5535 if (legacy_symbol)
5536 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5537 FALSE, FALSE, FALSE);
5538 if (h && (h->root.type == bfd_link_hash_defined
5539 || h->root.type == bfd_link_hash_defweak)
5540 && h->def_regular
5541 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5542 {
5543 /* The symbol has no type if specified on the command line. */
5544 h->type = STT_OBJECT;
5545 if (info->stacksize)
5546 (*_bfd_error_handler) (_("%B: stack size specified and %s set"),
5547 output_bfd, legacy_symbol);
5548 else if (h->root.u.def.section != bfd_abs_section_ptr)
5549 (*_bfd_error_handler) (_("%B: %s not absolute"),
5550 output_bfd, legacy_symbol);
5551 else
5552 info->stacksize = h->root.u.def.value;
5553 }
5554
5555 if (!info->stacksize)
5556 /* If the user didn't set a size, or explicitly inhibit the
5557 size, set it now. */
5558 info->stacksize = default_size;
5559
5560 /* Provide the legacy symbol, if it is referenced. */
5561 if (h && (h->root.type == bfd_link_hash_undefined
5562 || h->root.type == bfd_link_hash_undefweak))
5563 {
5564 struct bfd_link_hash_entry *bh = NULL;
5565
5566 if (!(_bfd_generic_link_add_one_symbol
5567 (info, output_bfd, legacy_symbol,
5568 BSF_GLOBAL, bfd_abs_section_ptr,
5569 info->stacksize >= 0 ? info->stacksize : 0,
5570 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5571 return FALSE;
5572
5573 h = (struct elf_link_hash_entry *) bh;
5574 h->def_regular = 1;
5575 h->type = STT_OBJECT;
5576 }
5577
5578 return TRUE;
5579 }
5580
5581 /* Set up the sizes and contents of the ELF dynamic sections. This is
5582 called by the ELF linker emulation before_allocation routine. We
5583 must set the sizes of the sections before the linker sets the
5584 addresses of the various sections. */
5585
5586 bfd_boolean
5587 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5588 const char *soname,
5589 const char *rpath,
5590 const char *filter_shlib,
5591 const char *audit,
5592 const char *depaudit,
5593 const char * const *auxiliary_filters,
5594 struct bfd_link_info *info,
5595 asection **sinterpptr)
5596 {
5597 bfd_size_type soname_indx;
5598 bfd *dynobj;
5599 const struct elf_backend_data *bed;
5600 struct elf_info_failed asvinfo;
5601
5602 *sinterpptr = NULL;
5603
5604 soname_indx = (bfd_size_type) -1;
5605
5606 if (!is_elf_hash_table (info->hash))
5607 return TRUE;
5608
5609 bed = get_elf_backend_data (output_bfd);
5610
5611 /* Any syms created from now on start with -1 in
5612 got.refcount/offset and plt.refcount/offset. */
5613 elf_hash_table (info)->init_got_refcount
5614 = elf_hash_table (info)->init_got_offset;
5615 elf_hash_table (info)->init_plt_refcount
5616 = elf_hash_table (info)->init_plt_offset;
5617
5618 if (info->relocatable
5619 && !_bfd_elf_size_group_sections (info))
5620 return FALSE;
5621
5622 /* The backend may have to create some sections regardless of whether
5623 we're dynamic or not. */
5624 if (bed->elf_backend_always_size_sections
5625 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5626 return FALSE;
5627
5628 /* Determine any GNU_STACK segment requirements, after the backend
5629 has had a chance to set a default segment size. */
5630 if (info->execstack)
5631 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
5632 else if (info->noexecstack)
5633 elf_stack_flags (output_bfd) = PF_R | PF_W;
5634 else
5635 {
5636 bfd *inputobj;
5637 asection *notesec = NULL;
5638 int exec = 0;
5639
5640 for (inputobj = info->input_bfds;
5641 inputobj;
5642 inputobj = inputobj->link.next)
5643 {
5644 asection *s;
5645
5646 if (inputobj->flags
5647 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5648 continue;
5649 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5650 if (s)
5651 {
5652 if (s->flags & SEC_CODE)
5653 exec = PF_X;
5654 notesec = s;
5655 }
5656 else if (bed->default_execstack)
5657 exec = PF_X;
5658 }
5659 if (notesec || info->stacksize > 0)
5660 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
5661 if (notesec && exec && info->relocatable
5662 && notesec->output_section != bfd_abs_section_ptr)
5663 notesec->output_section->flags |= SEC_CODE;
5664 }
5665
5666 dynobj = elf_hash_table (info)->dynobj;
5667
5668 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5669 {
5670 struct elf_info_failed eif;
5671 struct elf_link_hash_entry *h;
5672 asection *dynstr;
5673 struct bfd_elf_version_tree *t;
5674 struct bfd_elf_version_expr *d;
5675 asection *s;
5676 bfd_boolean all_defined;
5677
5678 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
5679 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
5680
5681 if (soname != NULL)
5682 {
5683 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5684 soname, TRUE);
5685 if (soname_indx == (bfd_size_type) -1
5686 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5687 return FALSE;
5688 }
5689
5690 if (info->symbolic)
5691 {
5692 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5693 return FALSE;
5694 info->flags |= DF_SYMBOLIC;
5695 }
5696
5697 if (rpath != NULL)
5698 {
5699 bfd_size_type indx;
5700 bfd_vma tag;
5701
5702 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5703 TRUE);
5704 if (indx == (bfd_size_type) -1)
5705 return FALSE;
5706
5707 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
5708 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
5709 return FALSE;
5710 }
5711
5712 if (filter_shlib != NULL)
5713 {
5714 bfd_size_type indx;
5715
5716 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5717 filter_shlib, TRUE);
5718 if (indx == (bfd_size_type) -1
5719 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5720 return FALSE;
5721 }
5722
5723 if (auxiliary_filters != NULL)
5724 {
5725 const char * const *p;
5726
5727 for (p = auxiliary_filters; *p != NULL; p++)
5728 {
5729 bfd_size_type indx;
5730
5731 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5732 *p, TRUE);
5733 if (indx == (bfd_size_type) -1
5734 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5735 return FALSE;
5736 }
5737 }
5738
5739 if (audit != NULL)
5740 {
5741 bfd_size_type indx;
5742
5743 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5744 TRUE);
5745 if (indx == (bfd_size_type) -1
5746 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5747 return FALSE;
5748 }
5749
5750 if (depaudit != NULL)
5751 {
5752 bfd_size_type indx;
5753
5754 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
5755 TRUE);
5756 if (indx == (bfd_size_type) -1
5757 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
5758 return FALSE;
5759 }
5760
5761 eif.info = info;
5762 eif.failed = FALSE;
5763
5764 /* If we are supposed to export all symbols into the dynamic symbol
5765 table (this is not the normal case), then do so. */
5766 if (info->export_dynamic
5767 || (info->executable && info->dynamic))
5768 {
5769 elf_link_hash_traverse (elf_hash_table (info),
5770 _bfd_elf_export_symbol,
5771 &eif);
5772 if (eif.failed)
5773 return FALSE;
5774 }
5775
5776 /* Make all global versions with definition. */
5777 for (t = info->version_info; t != NULL; t = t->next)
5778 for (d = t->globals.list; d != NULL; d = d->next)
5779 if (!d->symver && d->literal)
5780 {
5781 const char *verstr, *name;
5782 size_t namelen, verlen, newlen;
5783 char *newname, *p, leading_char;
5784 struct elf_link_hash_entry *newh;
5785
5786 leading_char = bfd_get_symbol_leading_char (output_bfd);
5787 name = d->pattern;
5788 namelen = strlen (name) + (leading_char != '\0');
5789 verstr = t->name;
5790 verlen = strlen (verstr);
5791 newlen = namelen + verlen + 3;
5792
5793 newname = (char *) bfd_malloc (newlen);
5794 if (newname == NULL)
5795 return FALSE;
5796 newname[0] = leading_char;
5797 memcpy (newname + (leading_char != '\0'), name, namelen);
5798
5799 /* Check the hidden versioned definition. */
5800 p = newname + namelen;
5801 *p++ = ELF_VER_CHR;
5802 memcpy (p, verstr, verlen + 1);
5803 newh = elf_link_hash_lookup (elf_hash_table (info),
5804 newname, FALSE, FALSE,
5805 FALSE);
5806 if (newh == NULL
5807 || (newh->root.type != bfd_link_hash_defined
5808 && newh->root.type != bfd_link_hash_defweak))
5809 {
5810 /* Check the default versioned definition. */
5811 *p++ = ELF_VER_CHR;
5812 memcpy (p, verstr, verlen + 1);
5813 newh = elf_link_hash_lookup (elf_hash_table (info),
5814 newname, FALSE, FALSE,
5815 FALSE);
5816 }
5817 free (newname);
5818
5819 /* Mark this version if there is a definition and it is
5820 not defined in a shared object. */
5821 if (newh != NULL
5822 && !newh->def_dynamic
5823 && (newh->root.type == bfd_link_hash_defined
5824 || newh->root.type == bfd_link_hash_defweak))
5825 d->symver = 1;
5826 }
5827
5828 /* Attach all the symbols to their version information. */
5829 asvinfo.info = info;
5830 asvinfo.failed = FALSE;
5831
5832 elf_link_hash_traverse (elf_hash_table (info),
5833 _bfd_elf_link_assign_sym_version,
5834 &asvinfo);
5835 if (asvinfo.failed)
5836 return FALSE;
5837
5838 if (!info->allow_undefined_version)
5839 {
5840 /* Check if all global versions have a definition. */
5841 all_defined = TRUE;
5842 for (t = info->version_info; t != NULL; t = t->next)
5843 for (d = t->globals.list; d != NULL; d = d->next)
5844 if (d->literal && !d->symver && !d->script)
5845 {
5846 (*_bfd_error_handler)
5847 (_("%s: undefined version: %s"),
5848 d->pattern, t->name);
5849 all_defined = FALSE;
5850 }
5851
5852 if (!all_defined)
5853 {
5854 bfd_set_error (bfd_error_bad_value);
5855 return FALSE;
5856 }
5857 }
5858
5859 /* Find all symbols which were defined in a dynamic object and make
5860 the backend pick a reasonable value for them. */
5861 elf_link_hash_traverse (elf_hash_table (info),
5862 _bfd_elf_adjust_dynamic_symbol,
5863 &eif);
5864 if (eif.failed)
5865 return FALSE;
5866
5867 /* Add some entries to the .dynamic section. We fill in some of the
5868 values later, in bfd_elf_final_link, but we must add the entries
5869 now so that we know the final size of the .dynamic section. */
5870
5871 /* If there are initialization and/or finalization functions to
5872 call then add the corresponding DT_INIT/DT_FINI entries. */
5873 h = (info->init_function
5874 ? elf_link_hash_lookup (elf_hash_table (info),
5875 info->init_function, FALSE,
5876 FALSE, FALSE)
5877 : NULL);
5878 if (h != NULL
5879 && (h->ref_regular
5880 || h->def_regular))
5881 {
5882 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5883 return FALSE;
5884 }
5885 h = (info->fini_function
5886 ? elf_link_hash_lookup (elf_hash_table (info),
5887 info->fini_function, FALSE,
5888 FALSE, FALSE)
5889 : NULL);
5890 if (h != NULL
5891 && (h->ref_regular
5892 || h->def_regular))
5893 {
5894 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5895 return FALSE;
5896 }
5897
5898 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5899 if (s != NULL && s->linker_has_input)
5900 {
5901 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5902 if (! info->executable)
5903 {
5904 bfd *sub;
5905 asection *o;
5906
5907 for (sub = info->input_bfds; sub != NULL;
5908 sub = sub->link.next)
5909 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
5910 for (o = sub->sections; o != NULL; o = o->next)
5911 if (elf_section_data (o)->this_hdr.sh_type
5912 == SHT_PREINIT_ARRAY)
5913 {
5914 (*_bfd_error_handler)
5915 (_("%B: .preinit_array section is not allowed in DSO"),
5916 sub);
5917 break;
5918 }
5919
5920 bfd_set_error (bfd_error_nonrepresentable_section);
5921 return FALSE;
5922 }
5923
5924 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5925 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5926 return FALSE;
5927 }
5928 s = bfd_get_section_by_name (output_bfd, ".init_array");
5929 if (s != NULL && s->linker_has_input)
5930 {
5931 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5932 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5933 return FALSE;
5934 }
5935 s = bfd_get_section_by_name (output_bfd, ".fini_array");
5936 if (s != NULL && s->linker_has_input)
5937 {
5938 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5939 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5940 return FALSE;
5941 }
5942
5943 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
5944 /* If .dynstr is excluded from the link, we don't want any of
5945 these tags. Strictly, we should be checking each section
5946 individually; This quick check covers for the case where
5947 someone does a /DISCARD/ : { *(*) }. */
5948 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5949 {
5950 bfd_size_type strsize;
5951
5952 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5953 if ((info->emit_hash
5954 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
5955 || (info->emit_gnu_hash
5956 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5957 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5958 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5959 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5960 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5961 bed->s->sizeof_sym))
5962 return FALSE;
5963 }
5964 }
5965
5966 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
5967 return FALSE;
5968
5969 /* The backend must work out the sizes of all the other dynamic
5970 sections. */
5971 if (dynobj != NULL
5972 && bed->elf_backend_size_dynamic_sections != NULL
5973 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5974 return FALSE;
5975
5976 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5977 {
5978 unsigned long section_sym_count;
5979 struct bfd_elf_version_tree *verdefs;
5980 asection *s;
5981
5982 /* Set up the version definition section. */
5983 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5984 BFD_ASSERT (s != NULL);
5985
5986 /* We may have created additional version definitions if we are
5987 just linking a regular application. */
5988 verdefs = info->version_info;
5989
5990 /* Skip anonymous version tag. */
5991 if (verdefs != NULL && verdefs->vernum == 0)
5992 verdefs = verdefs->next;
5993
5994 if (verdefs == NULL && !info->create_default_symver)
5995 s->flags |= SEC_EXCLUDE;
5996 else
5997 {
5998 unsigned int cdefs;
5999 bfd_size_type size;
6000 struct bfd_elf_version_tree *t;
6001 bfd_byte *p;
6002 Elf_Internal_Verdef def;
6003 Elf_Internal_Verdaux defaux;
6004 struct bfd_link_hash_entry *bh;
6005 struct elf_link_hash_entry *h;
6006 const char *name;
6007
6008 cdefs = 0;
6009 size = 0;
6010
6011 /* Make space for the base version. */
6012 size += sizeof (Elf_External_Verdef);
6013 size += sizeof (Elf_External_Verdaux);
6014 ++cdefs;
6015
6016 /* Make space for the default version. */
6017 if (info->create_default_symver)
6018 {
6019 size += sizeof (Elf_External_Verdef);
6020 ++cdefs;
6021 }
6022
6023 for (t = verdefs; t != NULL; t = t->next)
6024 {
6025 struct bfd_elf_version_deps *n;
6026
6027 /* Don't emit base version twice. */
6028 if (t->vernum == 0)
6029 continue;
6030
6031 size += sizeof (Elf_External_Verdef);
6032 size += sizeof (Elf_External_Verdaux);
6033 ++cdefs;
6034
6035 for (n = t->deps; n != NULL; n = n->next)
6036 size += sizeof (Elf_External_Verdaux);
6037 }
6038
6039 s->size = size;
6040 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6041 if (s->contents == NULL && s->size != 0)
6042 return FALSE;
6043
6044 /* Fill in the version definition section. */
6045
6046 p = s->contents;
6047
6048 def.vd_version = VER_DEF_CURRENT;
6049 def.vd_flags = VER_FLG_BASE;
6050 def.vd_ndx = 1;
6051 def.vd_cnt = 1;
6052 if (info->create_default_symver)
6053 {
6054 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6055 def.vd_next = sizeof (Elf_External_Verdef);
6056 }
6057 else
6058 {
6059 def.vd_aux = sizeof (Elf_External_Verdef);
6060 def.vd_next = (sizeof (Elf_External_Verdef)
6061 + sizeof (Elf_External_Verdaux));
6062 }
6063
6064 if (soname_indx != (bfd_size_type) -1)
6065 {
6066 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6067 soname_indx);
6068 def.vd_hash = bfd_elf_hash (soname);
6069 defaux.vda_name = soname_indx;
6070 name = soname;
6071 }
6072 else
6073 {
6074 bfd_size_type indx;
6075
6076 name = lbasename (output_bfd->filename);
6077 def.vd_hash = bfd_elf_hash (name);
6078 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6079 name, FALSE);
6080 if (indx == (bfd_size_type) -1)
6081 return FALSE;
6082 defaux.vda_name = indx;
6083 }
6084 defaux.vda_next = 0;
6085
6086 _bfd_elf_swap_verdef_out (output_bfd, &def,
6087 (Elf_External_Verdef *) p);
6088 p += sizeof (Elf_External_Verdef);
6089 if (info->create_default_symver)
6090 {
6091 /* Add a symbol representing this version. */
6092 bh = NULL;
6093 if (! (_bfd_generic_link_add_one_symbol
6094 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6095 0, NULL, FALSE,
6096 get_elf_backend_data (dynobj)->collect, &bh)))
6097 return FALSE;
6098 h = (struct elf_link_hash_entry *) bh;
6099 h->non_elf = 0;
6100 h->def_regular = 1;
6101 h->type = STT_OBJECT;
6102 h->verinfo.vertree = NULL;
6103
6104 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6105 return FALSE;
6106
6107 /* Create a duplicate of the base version with the same
6108 aux block, but different flags. */
6109 def.vd_flags = 0;
6110 def.vd_ndx = 2;
6111 def.vd_aux = sizeof (Elf_External_Verdef);
6112 if (verdefs)
6113 def.vd_next = (sizeof (Elf_External_Verdef)
6114 + sizeof (Elf_External_Verdaux));
6115 else
6116 def.vd_next = 0;
6117 _bfd_elf_swap_verdef_out (output_bfd, &def,
6118 (Elf_External_Verdef *) p);
6119 p += sizeof (Elf_External_Verdef);
6120 }
6121 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6122 (Elf_External_Verdaux *) p);
6123 p += sizeof (Elf_External_Verdaux);
6124
6125 for (t = verdefs; t != NULL; t = t->next)
6126 {
6127 unsigned int cdeps;
6128 struct bfd_elf_version_deps *n;
6129
6130 /* Don't emit the base version twice. */
6131 if (t->vernum == 0)
6132 continue;
6133
6134 cdeps = 0;
6135 for (n = t->deps; n != NULL; n = n->next)
6136 ++cdeps;
6137
6138 /* Add a symbol representing this version. */
6139 bh = NULL;
6140 if (! (_bfd_generic_link_add_one_symbol
6141 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6142 0, NULL, FALSE,
6143 get_elf_backend_data (dynobj)->collect, &bh)))
6144 return FALSE;
6145 h = (struct elf_link_hash_entry *) bh;
6146 h->non_elf = 0;
6147 h->def_regular = 1;
6148 h->type = STT_OBJECT;
6149 h->verinfo.vertree = t;
6150
6151 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6152 return FALSE;
6153
6154 def.vd_version = VER_DEF_CURRENT;
6155 def.vd_flags = 0;
6156 if (t->globals.list == NULL
6157 && t->locals.list == NULL
6158 && ! t->used)
6159 def.vd_flags |= VER_FLG_WEAK;
6160 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6161 def.vd_cnt = cdeps + 1;
6162 def.vd_hash = bfd_elf_hash (t->name);
6163 def.vd_aux = sizeof (Elf_External_Verdef);
6164 def.vd_next = 0;
6165
6166 /* If a basever node is next, it *must* be the last node in
6167 the chain, otherwise Verdef construction breaks. */
6168 if (t->next != NULL && t->next->vernum == 0)
6169 BFD_ASSERT (t->next->next == NULL);
6170
6171 if (t->next != NULL && t->next->vernum != 0)
6172 def.vd_next = (sizeof (Elf_External_Verdef)
6173 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6174
6175 _bfd_elf_swap_verdef_out (output_bfd, &def,
6176 (Elf_External_Verdef *) p);
6177 p += sizeof (Elf_External_Verdef);
6178
6179 defaux.vda_name = h->dynstr_index;
6180 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6181 h->dynstr_index);
6182 defaux.vda_next = 0;
6183 if (t->deps != NULL)
6184 defaux.vda_next = sizeof (Elf_External_Verdaux);
6185 t->name_indx = defaux.vda_name;
6186
6187 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6188 (Elf_External_Verdaux *) p);
6189 p += sizeof (Elf_External_Verdaux);
6190
6191 for (n = t->deps; n != NULL; n = n->next)
6192 {
6193 if (n->version_needed == NULL)
6194 {
6195 /* This can happen if there was an error in the
6196 version script. */
6197 defaux.vda_name = 0;
6198 }
6199 else
6200 {
6201 defaux.vda_name = n->version_needed->name_indx;
6202 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6203 defaux.vda_name);
6204 }
6205 if (n->next == NULL)
6206 defaux.vda_next = 0;
6207 else
6208 defaux.vda_next = sizeof (Elf_External_Verdaux);
6209
6210 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6211 (Elf_External_Verdaux *) p);
6212 p += sizeof (Elf_External_Verdaux);
6213 }
6214 }
6215
6216 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6217 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6218 return FALSE;
6219
6220 elf_tdata (output_bfd)->cverdefs = cdefs;
6221 }
6222
6223 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6224 {
6225 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6226 return FALSE;
6227 }
6228 else if (info->flags & DF_BIND_NOW)
6229 {
6230 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6231 return FALSE;
6232 }
6233
6234 if (info->flags_1)
6235 {
6236 if (info->executable)
6237 info->flags_1 &= ~ (DF_1_INITFIRST
6238 | DF_1_NODELETE
6239 | DF_1_NOOPEN);
6240 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6241 return FALSE;
6242 }
6243
6244 /* Work out the size of the version reference section. */
6245
6246 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6247 BFD_ASSERT (s != NULL);
6248 {
6249 struct elf_find_verdep_info sinfo;
6250
6251 sinfo.info = info;
6252 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6253 if (sinfo.vers == 0)
6254 sinfo.vers = 1;
6255 sinfo.failed = FALSE;
6256
6257 elf_link_hash_traverse (elf_hash_table (info),
6258 _bfd_elf_link_find_version_dependencies,
6259 &sinfo);
6260 if (sinfo.failed)
6261 return FALSE;
6262
6263 if (elf_tdata (output_bfd)->verref == NULL)
6264 s->flags |= SEC_EXCLUDE;
6265 else
6266 {
6267 Elf_Internal_Verneed *t;
6268 unsigned int size;
6269 unsigned int crefs;
6270 bfd_byte *p;
6271
6272 /* Build the version dependency section. */
6273 size = 0;
6274 crefs = 0;
6275 for (t = elf_tdata (output_bfd)->verref;
6276 t != NULL;
6277 t = t->vn_nextref)
6278 {
6279 Elf_Internal_Vernaux *a;
6280
6281 size += sizeof (Elf_External_Verneed);
6282 ++crefs;
6283 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6284 size += sizeof (Elf_External_Vernaux);
6285 }
6286
6287 s->size = size;
6288 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6289 if (s->contents == NULL)
6290 return FALSE;
6291
6292 p = s->contents;
6293 for (t = elf_tdata (output_bfd)->verref;
6294 t != NULL;
6295 t = t->vn_nextref)
6296 {
6297 unsigned int caux;
6298 Elf_Internal_Vernaux *a;
6299 bfd_size_type indx;
6300
6301 caux = 0;
6302 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6303 ++caux;
6304
6305 t->vn_version = VER_NEED_CURRENT;
6306 t->vn_cnt = caux;
6307 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6308 elf_dt_name (t->vn_bfd) != NULL
6309 ? elf_dt_name (t->vn_bfd)
6310 : lbasename (t->vn_bfd->filename),
6311 FALSE);
6312 if (indx == (bfd_size_type) -1)
6313 return FALSE;
6314 t->vn_file = indx;
6315 t->vn_aux = sizeof (Elf_External_Verneed);
6316 if (t->vn_nextref == NULL)
6317 t->vn_next = 0;
6318 else
6319 t->vn_next = (sizeof (Elf_External_Verneed)
6320 + caux * sizeof (Elf_External_Vernaux));
6321
6322 _bfd_elf_swap_verneed_out (output_bfd, t,
6323 (Elf_External_Verneed *) p);
6324 p += sizeof (Elf_External_Verneed);
6325
6326 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6327 {
6328 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6329 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6330 a->vna_nodename, FALSE);
6331 if (indx == (bfd_size_type) -1)
6332 return FALSE;
6333 a->vna_name = indx;
6334 if (a->vna_nextptr == NULL)
6335 a->vna_next = 0;
6336 else
6337 a->vna_next = sizeof (Elf_External_Vernaux);
6338
6339 _bfd_elf_swap_vernaux_out (output_bfd, a,
6340 (Elf_External_Vernaux *) p);
6341 p += sizeof (Elf_External_Vernaux);
6342 }
6343 }
6344
6345 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6346 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6347 return FALSE;
6348
6349 elf_tdata (output_bfd)->cverrefs = crefs;
6350 }
6351 }
6352
6353 if ((elf_tdata (output_bfd)->cverrefs == 0
6354 && elf_tdata (output_bfd)->cverdefs == 0)
6355 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6356 &section_sym_count) == 0)
6357 {
6358 s = bfd_get_linker_section (dynobj, ".gnu.version");
6359 s->flags |= SEC_EXCLUDE;
6360 }
6361 }
6362 return TRUE;
6363 }
6364
6365 /* Find the first non-excluded output section. We'll use its
6366 section symbol for some emitted relocs. */
6367 void
6368 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6369 {
6370 asection *s;
6371
6372 for (s = output_bfd->sections; s != NULL; s = s->next)
6373 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6374 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6375 {
6376 elf_hash_table (info)->text_index_section = s;
6377 break;
6378 }
6379 }
6380
6381 /* Find two non-excluded output sections, one for code, one for data.
6382 We'll use their section symbols for some emitted relocs. */
6383 void
6384 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6385 {
6386 asection *s;
6387
6388 /* Data first, since setting text_index_section changes
6389 _bfd_elf_link_omit_section_dynsym. */
6390 for (s = output_bfd->sections; s != NULL; s = s->next)
6391 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6392 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6393 {
6394 elf_hash_table (info)->data_index_section = s;
6395 break;
6396 }
6397
6398 for (s = output_bfd->sections; s != NULL; s = s->next)
6399 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6400 == (SEC_ALLOC | SEC_READONLY))
6401 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6402 {
6403 elf_hash_table (info)->text_index_section = s;
6404 break;
6405 }
6406
6407 if (elf_hash_table (info)->text_index_section == NULL)
6408 elf_hash_table (info)->text_index_section
6409 = elf_hash_table (info)->data_index_section;
6410 }
6411
6412 bfd_boolean
6413 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6414 {
6415 const struct elf_backend_data *bed;
6416
6417 if (!is_elf_hash_table (info->hash))
6418 return TRUE;
6419
6420 bed = get_elf_backend_data (output_bfd);
6421 (*bed->elf_backend_init_index_section) (output_bfd, info);
6422
6423 if (elf_hash_table (info)->dynamic_sections_created)
6424 {
6425 bfd *dynobj;
6426 asection *s;
6427 bfd_size_type dynsymcount;
6428 unsigned long section_sym_count;
6429 unsigned int dtagcount;
6430
6431 dynobj = elf_hash_table (info)->dynobj;
6432
6433 /* Assign dynsym indicies. In a shared library we generate a
6434 section symbol for each output section, which come first.
6435 Next come all of the back-end allocated local dynamic syms,
6436 followed by the rest of the global symbols. */
6437
6438 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6439 &section_sym_count);
6440
6441 /* Work out the size of the symbol version section. */
6442 s = bfd_get_linker_section (dynobj, ".gnu.version");
6443 BFD_ASSERT (s != NULL);
6444 if (dynsymcount != 0
6445 && (s->flags & SEC_EXCLUDE) == 0)
6446 {
6447 s->size = dynsymcount * sizeof (Elf_External_Versym);
6448 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6449 if (s->contents == NULL)
6450 return FALSE;
6451
6452 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6453 return FALSE;
6454 }
6455
6456 /* Set the size of the .dynsym and .hash sections. We counted
6457 the number of dynamic symbols in elf_link_add_object_symbols.
6458 We will build the contents of .dynsym and .hash when we build
6459 the final symbol table, because until then we do not know the
6460 correct value to give the symbols. We built the .dynstr
6461 section as we went along in elf_link_add_object_symbols. */
6462 s = bfd_get_linker_section (dynobj, ".dynsym");
6463 BFD_ASSERT (s != NULL);
6464 s->size = dynsymcount * bed->s->sizeof_sym;
6465
6466 if (dynsymcount != 0)
6467 {
6468 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6469 if (s->contents == NULL)
6470 return FALSE;
6471
6472 /* The first entry in .dynsym is a dummy symbol.
6473 Clear all the section syms, in case we don't output them all. */
6474 ++section_sym_count;
6475 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6476 }
6477
6478 elf_hash_table (info)->bucketcount = 0;
6479
6480 /* Compute the size of the hashing table. As a side effect this
6481 computes the hash values for all the names we export. */
6482 if (info->emit_hash)
6483 {
6484 unsigned long int *hashcodes;
6485 struct hash_codes_info hashinf;
6486 bfd_size_type amt;
6487 unsigned long int nsyms;
6488 size_t bucketcount;
6489 size_t hash_entry_size;
6490
6491 /* Compute the hash values for all exported symbols. At the same
6492 time store the values in an array so that we could use them for
6493 optimizations. */
6494 amt = dynsymcount * sizeof (unsigned long int);
6495 hashcodes = (unsigned long int *) bfd_malloc (amt);
6496 if (hashcodes == NULL)
6497 return FALSE;
6498 hashinf.hashcodes = hashcodes;
6499 hashinf.error = FALSE;
6500
6501 /* Put all hash values in HASHCODES. */
6502 elf_link_hash_traverse (elf_hash_table (info),
6503 elf_collect_hash_codes, &hashinf);
6504 if (hashinf.error)
6505 {
6506 free (hashcodes);
6507 return FALSE;
6508 }
6509
6510 nsyms = hashinf.hashcodes - hashcodes;
6511 bucketcount
6512 = compute_bucket_count (info, hashcodes, nsyms, 0);
6513 free (hashcodes);
6514
6515 if (bucketcount == 0)
6516 return FALSE;
6517
6518 elf_hash_table (info)->bucketcount = bucketcount;
6519
6520 s = bfd_get_linker_section (dynobj, ".hash");
6521 BFD_ASSERT (s != NULL);
6522 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6523 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6524 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6525 if (s->contents == NULL)
6526 return FALSE;
6527
6528 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6529 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6530 s->contents + hash_entry_size);
6531 }
6532
6533 if (info->emit_gnu_hash)
6534 {
6535 size_t i, cnt;
6536 unsigned char *contents;
6537 struct collect_gnu_hash_codes cinfo;
6538 bfd_size_type amt;
6539 size_t bucketcount;
6540
6541 memset (&cinfo, 0, sizeof (cinfo));
6542
6543 /* Compute the hash values for all exported symbols. At the same
6544 time store the values in an array so that we could use them for
6545 optimizations. */
6546 amt = dynsymcount * 2 * sizeof (unsigned long int);
6547 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
6548 if (cinfo.hashcodes == NULL)
6549 return FALSE;
6550
6551 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6552 cinfo.min_dynindx = -1;
6553 cinfo.output_bfd = output_bfd;
6554 cinfo.bed = bed;
6555
6556 /* Put all hash values in HASHCODES. */
6557 elf_link_hash_traverse (elf_hash_table (info),
6558 elf_collect_gnu_hash_codes, &cinfo);
6559 if (cinfo.error)
6560 {
6561 free (cinfo.hashcodes);
6562 return FALSE;
6563 }
6564
6565 bucketcount
6566 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6567
6568 if (bucketcount == 0)
6569 {
6570 free (cinfo.hashcodes);
6571 return FALSE;
6572 }
6573
6574 s = bfd_get_linker_section (dynobj, ".gnu.hash");
6575 BFD_ASSERT (s != NULL);
6576
6577 if (cinfo.nsyms == 0)
6578 {
6579 /* Empty .gnu.hash section is special. */
6580 BFD_ASSERT (cinfo.min_dynindx == -1);
6581 free (cinfo.hashcodes);
6582 s->size = 5 * 4 + bed->s->arch_size / 8;
6583 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6584 if (contents == NULL)
6585 return FALSE;
6586 s->contents = contents;
6587 /* 1 empty bucket. */
6588 bfd_put_32 (output_bfd, 1, contents);
6589 /* SYMIDX above the special symbol 0. */
6590 bfd_put_32 (output_bfd, 1, contents + 4);
6591 /* Just one word for bitmask. */
6592 bfd_put_32 (output_bfd, 1, contents + 8);
6593 /* Only hash fn bloom filter. */
6594 bfd_put_32 (output_bfd, 0, contents + 12);
6595 /* No hashes are valid - empty bitmask. */
6596 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6597 /* No hashes in the only bucket. */
6598 bfd_put_32 (output_bfd, 0,
6599 contents + 16 + bed->s->arch_size / 8);
6600 }
6601 else
6602 {
6603 unsigned long int maskwords, maskbitslog2, x;
6604 BFD_ASSERT (cinfo.min_dynindx != -1);
6605
6606 x = cinfo.nsyms;
6607 maskbitslog2 = 1;
6608 while ((x >>= 1) != 0)
6609 ++maskbitslog2;
6610 if (maskbitslog2 < 3)
6611 maskbitslog2 = 5;
6612 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6613 maskbitslog2 = maskbitslog2 + 3;
6614 else
6615 maskbitslog2 = maskbitslog2 + 2;
6616 if (bed->s->arch_size == 64)
6617 {
6618 if (maskbitslog2 == 5)
6619 maskbitslog2 = 6;
6620 cinfo.shift1 = 6;
6621 }
6622 else
6623 cinfo.shift1 = 5;
6624 cinfo.mask = (1 << cinfo.shift1) - 1;
6625 cinfo.shift2 = maskbitslog2;
6626 cinfo.maskbits = 1 << maskbitslog2;
6627 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6628 amt = bucketcount * sizeof (unsigned long int) * 2;
6629 amt += maskwords * sizeof (bfd_vma);
6630 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
6631 if (cinfo.bitmask == NULL)
6632 {
6633 free (cinfo.hashcodes);
6634 return FALSE;
6635 }
6636
6637 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
6638 cinfo.indx = cinfo.counts + bucketcount;
6639 cinfo.symindx = dynsymcount - cinfo.nsyms;
6640 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6641
6642 /* Determine how often each hash bucket is used. */
6643 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6644 for (i = 0; i < cinfo.nsyms; ++i)
6645 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6646
6647 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6648 if (cinfo.counts[i] != 0)
6649 {
6650 cinfo.indx[i] = cnt;
6651 cnt += cinfo.counts[i];
6652 }
6653 BFD_ASSERT (cnt == dynsymcount);
6654 cinfo.bucketcount = bucketcount;
6655 cinfo.local_indx = cinfo.min_dynindx;
6656
6657 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6658 s->size += cinfo.maskbits / 8;
6659 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6660 if (contents == NULL)
6661 {
6662 free (cinfo.bitmask);
6663 free (cinfo.hashcodes);
6664 return FALSE;
6665 }
6666
6667 s->contents = contents;
6668 bfd_put_32 (output_bfd, bucketcount, contents);
6669 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6670 bfd_put_32 (output_bfd, maskwords, contents + 8);
6671 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6672 contents += 16 + cinfo.maskbits / 8;
6673
6674 for (i = 0; i < bucketcount; ++i)
6675 {
6676 if (cinfo.counts[i] == 0)
6677 bfd_put_32 (output_bfd, 0, contents);
6678 else
6679 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6680 contents += 4;
6681 }
6682
6683 cinfo.contents = contents;
6684
6685 /* Renumber dynamic symbols, populate .gnu.hash section. */
6686 elf_link_hash_traverse (elf_hash_table (info),
6687 elf_renumber_gnu_hash_syms, &cinfo);
6688
6689 contents = s->contents + 16;
6690 for (i = 0; i < maskwords; ++i)
6691 {
6692 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6693 contents);
6694 contents += bed->s->arch_size / 8;
6695 }
6696
6697 free (cinfo.bitmask);
6698 free (cinfo.hashcodes);
6699 }
6700 }
6701
6702 s = bfd_get_linker_section (dynobj, ".dynstr");
6703 BFD_ASSERT (s != NULL);
6704
6705 elf_finalize_dynstr (output_bfd, info);
6706
6707 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6708
6709 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6710 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6711 return FALSE;
6712 }
6713
6714 return TRUE;
6715 }
6716 \f
6717 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
6718
6719 static void
6720 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6721 asection *sec)
6722 {
6723 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
6724 sec->sec_info_type = SEC_INFO_TYPE_NONE;
6725 }
6726
6727 /* Finish SHF_MERGE section merging. */
6728
6729 bfd_boolean
6730 _bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info)
6731 {
6732 bfd *ibfd;
6733 asection *sec;
6734
6735 if (!is_elf_hash_table (info->hash))
6736 return FALSE;
6737
6738 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6739 if ((ibfd->flags & DYNAMIC) == 0)
6740 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6741 if ((sec->flags & SEC_MERGE) != 0
6742 && !bfd_is_abs_section (sec->output_section))
6743 {
6744 struct bfd_elf_section_data *secdata;
6745
6746 secdata = elf_section_data (sec);
6747 if (! _bfd_add_merge_section (abfd,
6748 &elf_hash_table (info)->merge_info,
6749 sec, &secdata->sec_info))
6750 return FALSE;
6751 else if (secdata->sec_info)
6752 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
6753 }
6754
6755 if (elf_hash_table (info)->merge_info != NULL)
6756 _bfd_merge_sections (abfd, info, elf_hash_table (info)->merge_info,
6757 merge_sections_remove_hook);
6758 return TRUE;
6759 }
6760
6761 /* Create an entry in an ELF linker hash table. */
6762
6763 struct bfd_hash_entry *
6764 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6765 struct bfd_hash_table *table,
6766 const char *string)
6767 {
6768 /* Allocate the structure if it has not already been allocated by a
6769 subclass. */
6770 if (entry == NULL)
6771 {
6772 entry = (struct bfd_hash_entry *)
6773 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
6774 if (entry == NULL)
6775 return entry;
6776 }
6777
6778 /* Call the allocation method of the superclass. */
6779 entry = _bfd_link_hash_newfunc (entry, table, string);
6780 if (entry != NULL)
6781 {
6782 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6783 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6784
6785 /* Set local fields. */
6786 ret->indx = -1;
6787 ret->dynindx = -1;
6788 ret->got = htab->init_got_refcount;
6789 ret->plt = htab->init_plt_refcount;
6790 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6791 - offsetof (struct elf_link_hash_entry, size)));
6792 /* Assume that we have been called by a non-ELF symbol reader.
6793 This flag is then reset by the code which reads an ELF input
6794 file. This ensures that a symbol created by a non-ELF symbol
6795 reader will have the flag set correctly. */
6796 ret->non_elf = 1;
6797 }
6798
6799 return entry;
6800 }
6801
6802 /* Copy data from an indirect symbol to its direct symbol, hiding the
6803 old indirect symbol. Also used for copying flags to a weakdef. */
6804
6805 void
6806 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
6807 struct elf_link_hash_entry *dir,
6808 struct elf_link_hash_entry *ind)
6809 {
6810 struct elf_link_hash_table *htab;
6811
6812 /* Copy down any references that we may have already seen to the
6813 symbol which just became indirect. */
6814
6815 dir->ref_dynamic |= ind->ref_dynamic;
6816 dir->ref_regular |= ind->ref_regular;
6817 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
6818 dir->non_got_ref |= ind->non_got_ref;
6819 dir->needs_plt |= ind->needs_plt;
6820 dir->pointer_equality_needed |= ind->pointer_equality_needed;
6821
6822 if (ind->root.type != bfd_link_hash_indirect)
6823 return;
6824
6825 /* Copy over the global and procedure linkage table refcount entries.
6826 These may have been already set up by a check_relocs routine. */
6827 htab = elf_hash_table (info);
6828 if (ind->got.refcount > htab->init_got_refcount.refcount)
6829 {
6830 if (dir->got.refcount < 0)
6831 dir->got.refcount = 0;
6832 dir->got.refcount += ind->got.refcount;
6833 ind->got.refcount = htab->init_got_refcount.refcount;
6834 }
6835
6836 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
6837 {
6838 if (dir->plt.refcount < 0)
6839 dir->plt.refcount = 0;
6840 dir->plt.refcount += ind->plt.refcount;
6841 ind->plt.refcount = htab->init_plt_refcount.refcount;
6842 }
6843
6844 if (ind->dynindx != -1)
6845 {
6846 if (dir->dynindx != -1)
6847 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
6848 dir->dynindx = ind->dynindx;
6849 dir->dynstr_index = ind->dynstr_index;
6850 ind->dynindx = -1;
6851 ind->dynstr_index = 0;
6852 }
6853 }
6854
6855 void
6856 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
6857 struct elf_link_hash_entry *h,
6858 bfd_boolean force_local)
6859 {
6860 /* STT_GNU_IFUNC symbol must go through PLT. */
6861 if (h->type != STT_GNU_IFUNC)
6862 {
6863 h->plt = elf_hash_table (info)->init_plt_offset;
6864 h->needs_plt = 0;
6865 }
6866 if (force_local)
6867 {
6868 h->forced_local = 1;
6869 if (h->dynindx != -1)
6870 {
6871 h->dynindx = -1;
6872 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
6873 h->dynstr_index);
6874 }
6875 }
6876 }
6877
6878 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
6879 caller. */
6880
6881 bfd_boolean
6882 _bfd_elf_link_hash_table_init
6883 (struct elf_link_hash_table *table,
6884 bfd *abfd,
6885 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
6886 struct bfd_hash_table *,
6887 const char *),
6888 unsigned int entsize,
6889 enum elf_target_id target_id)
6890 {
6891 bfd_boolean ret;
6892 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
6893
6894 table->init_got_refcount.refcount = can_refcount - 1;
6895 table->init_plt_refcount.refcount = can_refcount - 1;
6896 table->init_got_offset.offset = -(bfd_vma) 1;
6897 table->init_plt_offset.offset = -(bfd_vma) 1;
6898 /* The first dynamic symbol is a dummy. */
6899 table->dynsymcount = 1;
6900
6901 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
6902
6903 table->root.type = bfd_link_elf_hash_table;
6904 table->hash_table_id = target_id;
6905
6906 return ret;
6907 }
6908
6909 /* Create an ELF linker hash table. */
6910
6911 struct bfd_link_hash_table *
6912 _bfd_elf_link_hash_table_create (bfd *abfd)
6913 {
6914 struct elf_link_hash_table *ret;
6915 bfd_size_type amt = sizeof (struct elf_link_hash_table);
6916
6917 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
6918 if (ret == NULL)
6919 return NULL;
6920
6921 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
6922 sizeof (struct elf_link_hash_entry),
6923 GENERIC_ELF_DATA))
6924 {
6925 free (ret);
6926 return NULL;
6927 }
6928 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
6929
6930 return &ret->root;
6931 }
6932
6933 /* Destroy an ELF linker hash table. */
6934
6935 void
6936 _bfd_elf_link_hash_table_free (bfd *obfd)
6937 {
6938 struct elf_link_hash_table *htab;
6939
6940 htab = (struct elf_link_hash_table *) obfd->link.hash;
6941 if (htab->dynstr != NULL)
6942 _bfd_elf_strtab_free (htab->dynstr);
6943 _bfd_merge_sections_free (htab->merge_info);
6944 _bfd_generic_link_hash_table_free (obfd);
6945 }
6946
6947 /* This is a hook for the ELF emulation code in the generic linker to
6948 tell the backend linker what file name to use for the DT_NEEDED
6949 entry for a dynamic object. */
6950
6951 void
6952 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
6953 {
6954 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6955 && bfd_get_format (abfd) == bfd_object)
6956 elf_dt_name (abfd) = name;
6957 }
6958
6959 int
6960 bfd_elf_get_dyn_lib_class (bfd *abfd)
6961 {
6962 int lib_class;
6963 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6964 && bfd_get_format (abfd) == bfd_object)
6965 lib_class = elf_dyn_lib_class (abfd);
6966 else
6967 lib_class = 0;
6968 return lib_class;
6969 }
6970
6971 void
6972 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
6973 {
6974 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6975 && bfd_get_format (abfd) == bfd_object)
6976 elf_dyn_lib_class (abfd) = lib_class;
6977 }
6978
6979 /* Get the list of DT_NEEDED entries for a link. This is a hook for
6980 the linker ELF emulation code. */
6981
6982 struct bfd_link_needed_list *
6983 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
6984 struct bfd_link_info *info)
6985 {
6986 if (! is_elf_hash_table (info->hash))
6987 return NULL;
6988 return elf_hash_table (info)->needed;
6989 }
6990
6991 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
6992 hook for the linker ELF emulation code. */
6993
6994 struct bfd_link_needed_list *
6995 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
6996 struct bfd_link_info *info)
6997 {
6998 if (! is_elf_hash_table (info->hash))
6999 return NULL;
7000 return elf_hash_table (info)->runpath;
7001 }
7002
7003 /* Get the name actually used for a dynamic object for a link. This
7004 is the SONAME entry if there is one. Otherwise, it is the string
7005 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7006
7007 const char *
7008 bfd_elf_get_dt_soname (bfd *abfd)
7009 {
7010 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7011 && bfd_get_format (abfd) == bfd_object)
7012 return elf_dt_name (abfd);
7013 return NULL;
7014 }
7015
7016 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7017 the ELF linker emulation code. */
7018
7019 bfd_boolean
7020 bfd_elf_get_bfd_needed_list (bfd *abfd,
7021 struct bfd_link_needed_list **pneeded)
7022 {
7023 asection *s;
7024 bfd_byte *dynbuf = NULL;
7025 unsigned int elfsec;
7026 unsigned long shlink;
7027 bfd_byte *extdyn, *extdynend;
7028 size_t extdynsize;
7029 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7030
7031 *pneeded = NULL;
7032
7033 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7034 || bfd_get_format (abfd) != bfd_object)
7035 return TRUE;
7036
7037 s = bfd_get_section_by_name (abfd, ".dynamic");
7038 if (s == NULL || s->size == 0)
7039 return TRUE;
7040
7041 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7042 goto error_return;
7043
7044 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7045 if (elfsec == SHN_BAD)
7046 goto error_return;
7047
7048 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7049
7050 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7051 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7052
7053 extdyn = dynbuf;
7054 extdynend = extdyn + s->size;
7055 for (; extdyn < extdynend; extdyn += extdynsize)
7056 {
7057 Elf_Internal_Dyn dyn;
7058
7059 (*swap_dyn_in) (abfd, extdyn, &dyn);
7060
7061 if (dyn.d_tag == DT_NULL)
7062 break;
7063
7064 if (dyn.d_tag == DT_NEEDED)
7065 {
7066 const char *string;
7067 struct bfd_link_needed_list *l;
7068 unsigned int tagv = dyn.d_un.d_val;
7069 bfd_size_type amt;
7070
7071 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7072 if (string == NULL)
7073 goto error_return;
7074
7075 amt = sizeof *l;
7076 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7077 if (l == NULL)
7078 goto error_return;
7079
7080 l->by = abfd;
7081 l->name = string;
7082 l->next = *pneeded;
7083 *pneeded = l;
7084 }
7085 }
7086
7087 free (dynbuf);
7088
7089 return TRUE;
7090
7091 error_return:
7092 if (dynbuf != NULL)
7093 free (dynbuf);
7094 return FALSE;
7095 }
7096
7097 struct elf_symbuf_symbol
7098 {
7099 unsigned long st_name; /* Symbol name, index in string tbl */
7100 unsigned char st_info; /* Type and binding attributes */
7101 unsigned char st_other; /* Visibilty, and target specific */
7102 };
7103
7104 struct elf_symbuf_head
7105 {
7106 struct elf_symbuf_symbol *ssym;
7107 bfd_size_type count;
7108 unsigned int st_shndx;
7109 };
7110
7111 struct elf_symbol
7112 {
7113 union
7114 {
7115 Elf_Internal_Sym *isym;
7116 struct elf_symbuf_symbol *ssym;
7117 } u;
7118 const char *name;
7119 };
7120
7121 /* Sort references to symbols by ascending section number. */
7122
7123 static int
7124 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7125 {
7126 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7127 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7128
7129 return s1->st_shndx - s2->st_shndx;
7130 }
7131
7132 static int
7133 elf_sym_name_compare (const void *arg1, const void *arg2)
7134 {
7135 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7136 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7137 return strcmp (s1->name, s2->name);
7138 }
7139
7140 static struct elf_symbuf_head *
7141 elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
7142 {
7143 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7144 struct elf_symbuf_symbol *ssym;
7145 struct elf_symbuf_head *ssymbuf, *ssymhead;
7146 bfd_size_type i, shndx_count, total_size;
7147
7148 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7149 if (indbuf == NULL)
7150 return NULL;
7151
7152 for (ind = indbuf, i = 0; i < symcount; i++)
7153 if (isymbuf[i].st_shndx != SHN_UNDEF)
7154 *ind++ = &isymbuf[i];
7155 indbufend = ind;
7156
7157 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7158 elf_sort_elf_symbol);
7159
7160 shndx_count = 0;
7161 if (indbufend > indbuf)
7162 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7163 if (ind[0]->st_shndx != ind[1]->st_shndx)
7164 shndx_count++;
7165
7166 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7167 + (indbufend - indbuf) * sizeof (*ssym));
7168 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7169 if (ssymbuf == NULL)
7170 {
7171 free (indbuf);
7172 return NULL;
7173 }
7174
7175 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7176 ssymbuf->ssym = NULL;
7177 ssymbuf->count = shndx_count;
7178 ssymbuf->st_shndx = 0;
7179 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7180 {
7181 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7182 {
7183 ssymhead++;
7184 ssymhead->ssym = ssym;
7185 ssymhead->count = 0;
7186 ssymhead->st_shndx = (*ind)->st_shndx;
7187 }
7188 ssym->st_name = (*ind)->st_name;
7189 ssym->st_info = (*ind)->st_info;
7190 ssym->st_other = (*ind)->st_other;
7191 ssymhead->count++;
7192 }
7193 BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
7194 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7195 == total_size));
7196
7197 free (indbuf);
7198 return ssymbuf;
7199 }
7200
7201 /* Check if 2 sections define the same set of local and global
7202 symbols. */
7203
7204 static bfd_boolean
7205 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7206 struct bfd_link_info *info)
7207 {
7208 bfd *bfd1, *bfd2;
7209 const struct elf_backend_data *bed1, *bed2;
7210 Elf_Internal_Shdr *hdr1, *hdr2;
7211 bfd_size_type symcount1, symcount2;
7212 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7213 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7214 Elf_Internal_Sym *isym, *isymend;
7215 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7216 bfd_size_type count1, count2, i;
7217 unsigned int shndx1, shndx2;
7218 bfd_boolean result;
7219
7220 bfd1 = sec1->owner;
7221 bfd2 = sec2->owner;
7222
7223 /* Both sections have to be in ELF. */
7224 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7225 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7226 return FALSE;
7227
7228 if (elf_section_type (sec1) != elf_section_type (sec2))
7229 return FALSE;
7230
7231 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7232 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7233 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7234 return FALSE;
7235
7236 bed1 = get_elf_backend_data (bfd1);
7237 bed2 = get_elf_backend_data (bfd2);
7238 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7239 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7240 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7241 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7242
7243 if (symcount1 == 0 || symcount2 == 0)
7244 return FALSE;
7245
7246 result = FALSE;
7247 isymbuf1 = NULL;
7248 isymbuf2 = NULL;
7249 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7250 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7251
7252 if (ssymbuf1 == NULL)
7253 {
7254 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7255 NULL, NULL, NULL);
7256 if (isymbuf1 == NULL)
7257 goto done;
7258
7259 if (!info->reduce_memory_overheads)
7260 elf_tdata (bfd1)->symbuf = ssymbuf1
7261 = elf_create_symbuf (symcount1, isymbuf1);
7262 }
7263
7264 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7265 {
7266 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7267 NULL, NULL, NULL);
7268 if (isymbuf2 == NULL)
7269 goto done;
7270
7271 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7272 elf_tdata (bfd2)->symbuf = ssymbuf2
7273 = elf_create_symbuf (symcount2, isymbuf2);
7274 }
7275
7276 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7277 {
7278 /* Optimized faster version. */
7279 bfd_size_type lo, hi, mid;
7280 struct elf_symbol *symp;
7281 struct elf_symbuf_symbol *ssym, *ssymend;
7282
7283 lo = 0;
7284 hi = ssymbuf1->count;
7285 ssymbuf1++;
7286 count1 = 0;
7287 while (lo < hi)
7288 {
7289 mid = (lo + hi) / 2;
7290 if (shndx1 < ssymbuf1[mid].st_shndx)
7291 hi = mid;
7292 else if (shndx1 > ssymbuf1[mid].st_shndx)
7293 lo = mid + 1;
7294 else
7295 {
7296 count1 = ssymbuf1[mid].count;
7297 ssymbuf1 += mid;
7298 break;
7299 }
7300 }
7301
7302 lo = 0;
7303 hi = ssymbuf2->count;
7304 ssymbuf2++;
7305 count2 = 0;
7306 while (lo < hi)
7307 {
7308 mid = (lo + hi) / 2;
7309 if (shndx2 < ssymbuf2[mid].st_shndx)
7310 hi = mid;
7311 else if (shndx2 > ssymbuf2[mid].st_shndx)
7312 lo = mid + 1;
7313 else
7314 {
7315 count2 = ssymbuf2[mid].count;
7316 ssymbuf2 += mid;
7317 break;
7318 }
7319 }
7320
7321 if (count1 == 0 || count2 == 0 || count1 != count2)
7322 goto done;
7323
7324 symtable1
7325 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7326 symtable2
7327 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
7328 if (symtable1 == NULL || symtable2 == NULL)
7329 goto done;
7330
7331 symp = symtable1;
7332 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7333 ssym < ssymend; ssym++, symp++)
7334 {
7335 symp->u.ssym = ssym;
7336 symp->name = bfd_elf_string_from_elf_section (bfd1,
7337 hdr1->sh_link,
7338 ssym->st_name);
7339 }
7340
7341 symp = symtable2;
7342 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7343 ssym < ssymend; ssym++, symp++)
7344 {
7345 symp->u.ssym = ssym;
7346 symp->name = bfd_elf_string_from_elf_section (bfd2,
7347 hdr2->sh_link,
7348 ssym->st_name);
7349 }
7350
7351 /* Sort symbol by name. */
7352 qsort (symtable1, count1, sizeof (struct elf_symbol),
7353 elf_sym_name_compare);
7354 qsort (symtable2, count1, sizeof (struct elf_symbol),
7355 elf_sym_name_compare);
7356
7357 for (i = 0; i < count1; i++)
7358 /* Two symbols must have the same binding, type and name. */
7359 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7360 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7361 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7362 goto done;
7363
7364 result = TRUE;
7365 goto done;
7366 }
7367
7368 symtable1 = (struct elf_symbol *)
7369 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7370 symtable2 = (struct elf_symbol *)
7371 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7372 if (symtable1 == NULL || symtable2 == NULL)
7373 goto done;
7374
7375 /* Count definitions in the section. */
7376 count1 = 0;
7377 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7378 if (isym->st_shndx == shndx1)
7379 symtable1[count1++].u.isym = isym;
7380
7381 count2 = 0;
7382 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7383 if (isym->st_shndx == shndx2)
7384 symtable2[count2++].u.isym = isym;
7385
7386 if (count1 == 0 || count2 == 0 || count1 != count2)
7387 goto done;
7388
7389 for (i = 0; i < count1; i++)
7390 symtable1[i].name
7391 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7392 symtable1[i].u.isym->st_name);
7393
7394 for (i = 0; i < count2; i++)
7395 symtable2[i].name
7396 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7397 symtable2[i].u.isym->st_name);
7398
7399 /* Sort symbol by name. */
7400 qsort (symtable1, count1, sizeof (struct elf_symbol),
7401 elf_sym_name_compare);
7402 qsort (symtable2, count1, sizeof (struct elf_symbol),
7403 elf_sym_name_compare);
7404
7405 for (i = 0; i < count1; i++)
7406 /* Two symbols must have the same binding, type and name. */
7407 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7408 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7409 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7410 goto done;
7411
7412 result = TRUE;
7413
7414 done:
7415 if (symtable1)
7416 free (symtable1);
7417 if (symtable2)
7418 free (symtable2);
7419 if (isymbuf1)
7420 free (isymbuf1);
7421 if (isymbuf2)
7422 free (isymbuf2);
7423
7424 return result;
7425 }
7426
7427 /* Return TRUE if 2 section types are compatible. */
7428
7429 bfd_boolean
7430 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7431 bfd *bbfd, const asection *bsec)
7432 {
7433 if (asec == NULL
7434 || bsec == NULL
7435 || abfd->xvec->flavour != bfd_target_elf_flavour
7436 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7437 return TRUE;
7438
7439 return elf_section_type (asec) == elf_section_type (bsec);
7440 }
7441 \f
7442 /* Final phase of ELF linker. */
7443
7444 /* A structure we use to avoid passing large numbers of arguments. */
7445
7446 struct elf_final_link_info
7447 {
7448 /* General link information. */
7449 struct bfd_link_info *info;
7450 /* Output BFD. */
7451 bfd *output_bfd;
7452 /* Symbol string table. */
7453 struct elf_strtab_hash *symstrtab;
7454 /* .dynsym section. */
7455 asection *dynsym_sec;
7456 /* .hash section. */
7457 asection *hash_sec;
7458 /* symbol version section (.gnu.version). */
7459 asection *symver_sec;
7460 /* Buffer large enough to hold contents of any section. */
7461 bfd_byte *contents;
7462 /* Buffer large enough to hold external relocs of any section. */
7463 void *external_relocs;
7464 /* Buffer large enough to hold internal relocs of any section. */
7465 Elf_Internal_Rela *internal_relocs;
7466 /* Buffer large enough to hold external local symbols of any input
7467 BFD. */
7468 bfd_byte *external_syms;
7469 /* And a buffer for symbol section indices. */
7470 Elf_External_Sym_Shndx *locsym_shndx;
7471 /* Buffer large enough to hold internal local symbols of any input
7472 BFD. */
7473 Elf_Internal_Sym *internal_syms;
7474 /* Array large enough to hold a symbol index for each local symbol
7475 of any input BFD. */
7476 long *indices;
7477 /* Array large enough to hold a section pointer for each local
7478 symbol of any input BFD. */
7479 asection **sections;
7480 /* Buffer for SHT_SYMTAB_SHNDX section. */
7481 Elf_External_Sym_Shndx *symshndxbuf;
7482 /* Number of STT_FILE syms seen. */
7483 size_t filesym_count;
7484 };
7485
7486 /* This struct is used to pass information to elf_link_output_extsym. */
7487
7488 struct elf_outext_info
7489 {
7490 bfd_boolean failed;
7491 bfd_boolean localsyms;
7492 bfd_boolean file_sym_done;
7493 struct elf_final_link_info *flinfo;
7494 };
7495
7496
7497 /* Support for evaluating a complex relocation.
7498
7499 Complex relocations are generalized, self-describing relocations. The
7500 implementation of them consists of two parts: complex symbols, and the
7501 relocations themselves.
7502
7503 The relocations are use a reserved elf-wide relocation type code (R_RELC
7504 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7505 information (start bit, end bit, word width, etc) into the addend. This
7506 information is extracted from CGEN-generated operand tables within gas.
7507
7508 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7509 internal) representing prefix-notation expressions, including but not
7510 limited to those sorts of expressions normally encoded as addends in the
7511 addend field. The symbol mangling format is:
7512
7513 <node> := <literal>
7514 | <unary-operator> ':' <node>
7515 | <binary-operator> ':' <node> ':' <node>
7516 ;
7517
7518 <literal> := 's' <digits=N> ':' <N character symbol name>
7519 | 'S' <digits=N> ':' <N character section name>
7520 | '#' <hexdigits>
7521 ;
7522
7523 <binary-operator> := as in C
7524 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7525
7526 static void
7527 set_symbol_value (bfd *bfd_with_globals,
7528 Elf_Internal_Sym *isymbuf,
7529 size_t locsymcount,
7530 size_t symidx,
7531 bfd_vma val)
7532 {
7533 struct elf_link_hash_entry **sym_hashes;
7534 struct elf_link_hash_entry *h;
7535 size_t extsymoff = locsymcount;
7536
7537 if (symidx < locsymcount)
7538 {
7539 Elf_Internal_Sym *sym;
7540
7541 sym = isymbuf + symidx;
7542 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7543 {
7544 /* It is a local symbol: move it to the
7545 "absolute" section and give it a value. */
7546 sym->st_shndx = SHN_ABS;
7547 sym->st_value = val;
7548 return;
7549 }
7550 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7551 extsymoff = 0;
7552 }
7553
7554 /* It is a global symbol: set its link type
7555 to "defined" and give it a value. */
7556
7557 sym_hashes = elf_sym_hashes (bfd_with_globals);
7558 h = sym_hashes [symidx - extsymoff];
7559 while (h->root.type == bfd_link_hash_indirect
7560 || h->root.type == bfd_link_hash_warning)
7561 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7562 h->root.type = bfd_link_hash_defined;
7563 h->root.u.def.value = val;
7564 h->root.u.def.section = bfd_abs_section_ptr;
7565 }
7566
7567 static bfd_boolean
7568 resolve_symbol (const char *name,
7569 bfd *input_bfd,
7570 struct elf_final_link_info *flinfo,
7571 bfd_vma *result,
7572 Elf_Internal_Sym *isymbuf,
7573 size_t locsymcount)
7574 {
7575 Elf_Internal_Sym *sym;
7576 struct bfd_link_hash_entry *global_entry;
7577 const char *candidate = NULL;
7578 Elf_Internal_Shdr *symtab_hdr;
7579 size_t i;
7580
7581 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7582
7583 for (i = 0; i < locsymcount; ++ i)
7584 {
7585 sym = isymbuf + i;
7586
7587 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7588 continue;
7589
7590 candidate = bfd_elf_string_from_elf_section (input_bfd,
7591 symtab_hdr->sh_link,
7592 sym->st_name);
7593 #ifdef DEBUG
7594 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7595 name, candidate, (unsigned long) sym->st_value);
7596 #endif
7597 if (candidate && strcmp (candidate, name) == 0)
7598 {
7599 asection *sec = flinfo->sections [i];
7600
7601 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7602 *result += sec->output_offset + sec->output_section->vma;
7603 #ifdef DEBUG
7604 printf ("Found symbol with value %8.8lx\n",
7605 (unsigned long) *result);
7606 #endif
7607 return TRUE;
7608 }
7609 }
7610
7611 /* Hmm, haven't found it yet. perhaps it is a global. */
7612 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
7613 FALSE, FALSE, TRUE);
7614 if (!global_entry)
7615 return FALSE;
7616
7617 if (global_entry->type == bfd_link_hash_defined
7618 || global_entry->type == bfd_link_hash_defweak)
7619 {
7620 *result = (global_entry->u.def.value
7621 + global_entry->u.def.section->output_section->vma
7622 + global_entry->u.def.section->output_offset);
7623 #ifdef DEBUG
7624 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7625 global_entry->root.string, (unsigned long) *result);
7626 #endif
7627 return TRUE;
7628 }
7629
7630 return FALSE;
7631 }
7632
7633 static bfd_boolean
7634 resolve_section (const char *name,
7635 asection *sections,
7636 bfd_vma *result)
7637 {
7638 asection *curr;
7639 unsigned int len;
7640
7641 for (curr = sections; curr; curr = curr->next)
7642 if (strcmp (curr->name, name) == 0)
7643 {
7644 *result = curr->vma;
7645 return TRUE;
7646 }
7647
7648 /* Hmm. still haven't found it. try pseudo-section names. */
7649 for (curr = sections; curr; curr = curr->next)
7650 {
7651 len = strlen (curr->name);
7652 if (len > strlen (name))
7653 continue;
7654
7655 if (strncmp (curr->name, name, len) == 0)
7656 {
7657 if (strncmp (".end", name + len, 4) == 0)
7658 {
7659 *result = curr->vma + curr->size;
7660 return TRUE;
7661 }
7662
7663 /* Insert more pseudo-section names here, if you like. */
7664 }
7665 }
7666
7667 return FALSE;
7668 }
7669
7670 static void
7671 undefined_reference (const char *reftype, const char *name)
7672 {
7673 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7674 reftype, name);
7675 }
7676
7677 static bfd_boolean
7678 eval_symbol (bfd_vma *result,
7679 const char **symp,
7680 bfd *input_bfd,
7681 struct elf_final_link_info *flinfo,
7682 bfd_vma dot,
7683 Elf_Internal_Sym *isymbuf,
7684 size_t locsymcount,
7685 int signed_p)
7686 {
7687 size_t len;
7688 size_t symlen;
7689 bfd_vma a;
7690 bfd_vma b;
7691 char symbuf[4096];
7692 const char *sym = *symp;
7693 const char *symend;
7694 bfd_boolean symbol_is_section = FALSE;
7695
7696 len = strlen (sym);
7697 symend = sym + len;
7698
7699 if (len < 1 || len > sizeof (symbuf))
7700 {
7701 bfd_set_error (bfd_error_invalid_operation);
7702 return FALSE;
7703 }
7704
7705 switch (* sym)
7706 {
7707 case '.':
7708 *result = dot;
7709 *symp = sym + 1;
7710 return TRUE;
7711
7712 case '#':
7713 ++sym;
7714 *result = strtoul (sym, (char **) symp, 16);
7715 return TRUE;
7716
7717 case 'S':
7718 symbol_is_section = TRUE;
7719 case 's':
7720 ++sym;
7721 symlen = strtol (sym, (char **) symp, 10);
7722 sym = *symp + 1; /* Skip the trailing ':'. */
7723
7724 if (symend < sym || symlen + 1 > sizeof (symbuf))
7725 {
7726 bfd_set_error (bfd_error_invalid_operation);
7727 return FALSE;
7728 }
7729
7730 memcpy (symbuf, sym, symlen);
7731 symbuf[symlen] = '\0';
7732 *symp = sym + symlen;
7733
7734 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7735 the symbol as a section, or vice-versa. so we're pretty liberal in our
7736 interpretation here; section means "try section first", not "must be a
7737 section", and likewise with symbol. */
7738
7739 if (symbol_is_section)
7740 {
7741 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result)
7742 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
7743 isymbuf, locsymcount))
7744 {
7745 undefined_reference ("section", symbuf);
7746 return FALSE;
7747 }
7748 }
7749 else
7750 {
7751 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
7752 isymbuf, locsymcount)
7753 && !resolve_section (symbuf, flinfo->output_bfd->sections,
7754 result))
7755 {
7756 undefined_reference ("symbol", symbuf);
7757 return FALSE;
7758 }
7759 }
7760
7761 return TRUE;
7762
7763 /* All that remains are operators. */
7764
7765 #define UNARY_OP(op) \
7766 if (strncmp (sym, #op, strlen (#op)) == 0) \
7767 { \
7768 sym += strlen (#op); \
7769 if (*sym == ':') \
7770 ++sym; \
7771 *symp = sym; \
7772 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
7773 isymbuf, locsymcount, signed_p)) \
7774 return FALSE; \
7775 if (signed_p) \
7776 *result = op ((bfd_signed_vma) a); \
7777 else \
7778 *result = op a; \
7779 return TRUE; \
7780 }
7781
7782 #define BINARY_OP(op) \
7783 if (strncmp (sym, #op, strlen (#op)) == 0) \
7784 { \
7785 sym += strlen (#op); \
7786 if (*sym == ':') \
7787 ++sym; \
7788 *symp = sym; \
7789 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
7790 isymbuf, locsymcount, signed_p)) \
7791 return FALSE; \
7792 ++*symp; \
7793 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
7794 isymbuf, locsymcount, signed_p)) \
7795 return FALSE; \
7796 if (signed_p) \
7797 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
7798 else \
7799 *result = a op b; \
7800 return TRUE; \
7801 }
7802
7803 default:
7804 UNARY_OP (0-);
7805 BINARY_OP (<<);
7806 BINARY_OP (>>);
7807 BINARY_OP (==);
7808 BINARY_OP (!=);
7809 BINARY_OP (<=);
7810 BINARY_OP (>=);
7811 BINARY_OP (&&);
7812 BINARY_OP (||);
7813 UNARY_OP (~);
7814 UNARY_OP (!);
7815 BINARY_OP (*);
7816 BINARY_OP (/);
7817 BINARY_OP (%);
7818 BINARY_OP (^);
7819 BINARY_OP (|);
7820 BINARY_OP (&);
7821 BINARY_OP (+);
7822 BINARY_OP (-);
7823 BINARY_OP (<);
7824 BINARY_OP (>);
7825 #undef UNARY_OP
7826 #undef BINARY_OP
7827 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
7828 bfd_set_error (bfd_error_invalid_operation);
7829 return FALSE;
7830 }
7831 }
7832
7833 static void
7834 put_value (bfd_vma size,
7835 unsigned long chunksz,
7836 bfd *input_bfd,
7837 bfd_vma x,
7838 bfd_byte *location)
7839 {
7840 location += (size - chunksz);
7841
7842 for (; size; size -= chunksz, location -= chunksz)
7843 {
7844 switch (chunksz)
7845 {
7846 case 1:
7847 bfd_put_8 (input_bfd, x, location);
7848 x >>= 8;
7849 break;
7850 case 2:
7851 bfd_put_16 (input_bfd, x, location);
7852 x >>= 16;
7853 break;
7854 case 4:
7855 bfd_put_32 (input_bfd, x, location);
7856 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
7857 x >>= 16;
7858 x >>= 16;
7859 break;
7860 #ifdef BFD64
7861 case 8:
7862 bfd_put_64 (input_bfd, x, location);
7863 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
7864 x >>= 32;
7865 x >>= 32;
7866 break;
7867 #endif
7868 default:
7869 abort ();
7870 break;
7871 }
7872 }
7873 }
7874
7875 static bfd_vma
7876 get_value (bfd_vma size,
7877 unsigned long chunksz,
7878 bfd *input_bfd,
7879 bfd_byte *location)
7880 {
7881 int shift;
7882 bfd_vma x = 0;
7883
7884 /* Sanity checks. */
7885 BFD_ASSERT (chunksz <= sizeof (x)
7886 && size >= chunksz
7887 && chunksz != 0
7888 && (size % chunksz) == 0
7889 && input_bfd != NULL
7890 && location != NULL);
7891
7892 if (chunksz == sizeof (x))
7893 {
7894 BFD_ASSERT (size == chunksz);
7895
7896 /* Make sure that we do not perform an undefined shift operation.
7897 We know that size == chunksz so there will only be one iteration
7898 of the loop below. */
7899 shift = 0;
7900 }
7901 else
7902 shift = 8 * chunksz;
7903
7904 for (; size; size -= chunksz, location += chunksz)
7905 {
7906 switch (chunksz)
7907 {
7908 case 1:
7909 x = (x << shift) | bfd_get_8 (input_bfd, location);
7910 break;
7911 case 2:
7912 x = (x << shift) | bfd_get_16 (input_bfd, location);
7913 break;
7914 case 4:
7915 x = (x << shift) | bfd_get_32 (input_bfd, location);
7916 break;
7917 #ifdef BFD64
7918 case 8:
7919 x = (x << shift) | bfd_get_64 (input_bfd, location);
7920 break;
7921 #endif
7922 default:
7923 abort ();
7924 }
7925 }
7926 return x;
7927 }
7928
7929 static void
7930 decode_complex_addend (unsigned long *start, /* in bits */
7931 unsigned long *oplen, /* in bits */
7932 unsigned long *len, /* in bits */
7933 unsigned long *wordsz, /* in bytes */
7934 unsigned long *chunksz, /* in bytes */
7935 unsigned long *lsb0_p,
7936 unsigned long *signed_p,
7937 unsigned long *trunc_p,
7938 unsigned long encoded)
7939 {
7940 * start = encoded & 0x3F;
7941 * len = (encoded >> 6) & 0x3F;
7942 * oplen = (encoded >> 12) & 0x3F;
7943 * wordsz = (encoded >> 18) & 0xF;
7944 * chunksz = (encoded >> 22) & 0xF;
7945 * lsb0_p = (encoded >> 27) & 1;
7946 * signed_p = (encoded >> 28) & 1;
7947 * trunc_p = (encoded >> 29) & 1;
7948 }
7949
7950 bfd_reloc_status_type
7951 bfd_elf_perform_complex_relocation (bfd *input_bfd,
7952 asection *input_section ATTRIBUTE_UNUSED,
7953 bfd_byte *contents,
7954 Elf_Internal_Rela *rel,
7955 bfd_vma relocation)
7956 {
7957 bfd_vma shift, x, mask;
7958 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
7959 bfd_reloc_status_type r;
7960
7961 /* Perform this reloc, since it is complex.
7962 (this is not to say that it necessarily refers to a complex
7963 symbol; merely that it is a self-describing CGEN based reloc.
7964 i.e. the addend has the complete reloc information (bit start, end,
7965 word size, etc) encoded within it.). */
7966
7967 decode_complex_addend (&start, &oplen, &len, &wordsz,
7968 &chunksz, &lsb0_p, &signed_p,
7969 &trunc_p, rel->r_addend);
7970
7971 mask = (((1L << (len - 1)) - 1) << 1) | 1;
7972
7973 if (lsb0_p)
7974 shift = (start + 1) - len;
7975 else
7976 shift = (8 * wordsz) - (start + len);
7977
7978 /* FIXME: octets_per_byte. */
7979 x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
7980
7981 #ifdef DEBUG
7982 printf ("Doing complex reloc: "
7983 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
7984 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
7985 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
7986 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
7987 oplen, (unsigned long) x, (unsigned long) mask,
7988 (unsigned long) relocation);
7989 #endif
7990
7991 r = bfd_reloc_ok;
7992 if (! trunc_p)
7993 /* Now do an overflow check. */
7994 r = bfd_check_overflow ((signed_p
7995 ? complain_overflow_signed
7996 : complain_overflow_unsigned),
7997 len, 0, (8 * wordsz),
7998 relocation);
7999
8000 /* Do the deed. */
8001 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8002
8003 #ifdef DEBUG
8004 printf (" relocation: %8.8lx\n"
8005 " shifted mask: %8.8lx\n"
8006 " shifted/masked reloc: %8.8lx\n"
8007 " result: %8.8lx\n",
8008 (unsigned long) relocation, (unsigned long) (mask << shift),
8009 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8010 #endif
8011 /* FIXME: octets_per_byte. */
8012 put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
8013 return r;
8014 }
8015
8016 /* qsort comparison functions sorting external relocs by r_offset. */
8017
8018 static int
8019 cmp_ext32l_r_offset (const void *p, const void *q)
8020 {
8021 union aligned32
8022 {
8023 uint32_t v;
8024 unsigned char c[4];
8025 };
8026 const union aligned32 *a
8027 = (const union aligned32 *) ((const Elf32_External_Rel *) p)->r_offset;
8028 const union aligned32 *b
8029 = (const union aligned32 *) ((const Elf32_External_Rel *) q)->r_offset;
8030
8031 uint32_t aval = ( (uint32_t) a->c[0]
8032 | (uint32_t) a->c[1] << 8
8033 | (uint32_t) a->c[2] << 16
8034 | (uint32_t) a->c[3] << 24);
8035 uint32_t bval = ( (uint32_t) b->c[0]
8036 | (uint32_t) b->c[1] << 8
8037 | (uint32_t) b->c[2] << 16
8038 | (uint32_t) b->c[3] << 24);
8039 if (aval < bval)
8040 return -1;
8041 else if (aval > bval)
8042 return 1;
8043 return 0;
8044 }
8045
8046 static int
8047 cmp_ext32b_r_offset (const void *p, const void *q)
8048 {
8049 union aligned32
8050 {
8051 uint32_t v;
8052 unsigned char c[4];
8053 };
8054 const union aligned32 *a
8055 = (const union aligned32 *) ((const Elf32_External_Rel *) p)->r_offset;
8056 const union aligned32 *b
8057 = (const union aligned32 *) ((const Elf32_External_Rel *) q)->r_offset;
8058
8059 uint32_t aval = ( (uint32_t) a->c[0] << 24
8060 | (uint32_t) a->c[1] << 16
8061 | (uint32_t) a->c[2] << 8
8062 | (uint32_t) a->c[3]);
8063 uint32_t bval = ( (uint32_t) b->c[0] << 24
8064 | (uint32_t) b->c[1] << 16
8065 | (uint32_t) b->c[2] << 8
8066 | (uint32_t) b->c[3]);
8067 if (aval < bval)
8068 return -1;
8069 else if (aval > bval)
8070 return 1;
8071 return 0;
8072 }
8073
8074 #ifdef BFD_HOST_64_BIT
8075 static int
8076 cmp_ext64l_r_offset (const void *p, const void *q)
8077 {
8078 union aligned64
8079 {
8080 uint64_t v;
8081 unsigned char c[8];
8082 };
8083 const union aligned64 *a
8084 = (const union aligned64 *) ((const Elf64_External_Rel *) p)->r_offset;
8085 const union aligned64 *b
8086 = (const union aligned64 *) ((const Elf64_External_Rel *) q)->r_offset;
8087
8088 uint64_t aval = ( (uint64_t) a->c[0]
8089 | (uint64_t) a->c[1] << 8
8090 | (uint64_t) a->c[2] << 16
8091 | (uint64_t) a->c[3] << 24
8092 | (uint64_t) a->c[4] << 32
8093 | (uint64_t) a->c[5] << 40
8094 | (uint64_t) a->c[6] << 48
8095 | (uint64_t) a->c[7] << 56);
8096 uint64_t bval = ( (uint64_t) b->c[0]
8097 | (uint64_t) b->c[1] << 8
8098 | (uint64_t) b->c[2] << 16
8099 | (uint64_t) b->c[3] << 24
8100 | (uint64_t) b->c[4] << 32
8101 | (uint64_t) b->c[5] << 40
8102 | (uint64_t) b->c[6] << 48
8103 | (uint64_t) b->c[7] << 56);
8104 if (aval < bval)
8105 return -1;
8106 else if (aval > bval)
8107 return 1;
8108 return 0;
8109 }
8110
8111 static int
8112 cmp_ext64b_r_offset (const void *p, const void *q)
8113 {
8114 union aligned64
8115 {
8116 uint64_t v;
8117 unsigned char c[8];
8118 };
8119 const union aligned64 *a
8120 = (const union aligned64 *) ((const Elf64_External_Rel *) p)->r_offset;
8121 const union aligned64 *b
8122 = (const union aligned64 *) ((const Elf64_External_Rel *) q)->r_offset;
8123
8124 uint64_t aval = ( (uint64_t) a->c[0] << 56
8125 | (uint64_t) a->c[1] << 48
8126 | (uint64_t) a->c[2] << 40
8127 | (uint64_t) a->c[3] << 32
8128 | (uint64_t) a->c[4] << 24
8129 | (uint64_t) a->c[5] << 16
8130 | (uint64_t) a->c[6] << 8
8131 | (uint64_t) a->c[7]);
8132 uint64_t bval = ( (uint64_t) b->c[0] << 56
8133 | (uint64_t) b->c[1] << 48
8134 | (uint64_t) b->c[2] << 40
8135 | (uint64_t) b->c[3] << 32
8136 | (uint64_t) b->c[4] << 24
8137 | (uint64_t) b->c[5] << 16
8138 | (uint64_t) b->c[6] << 8
8139 | (uint64_t) b->c[7]);
8140 if (aval < bval)
8141 return -1;
8142 else if (aval > bval)
8143 return 1;
8144 return 0;
8145 }
8146 #endif
8147
8148 /* When performing a relocatable link, the input relocations are
8149 preserved. But, if they reference global symbols, the indices
8150 referenced must be updated. Update all the relocations found in
8151 RELDATA. */
8152
8153 static void
8154 elf_link_adjust_relocs (bfd *abfd,
8155 struct bfd_elf_section_reloc_data *reldata,
8156 bfd_boolean sort)
8157 {
8158 unsigned int i;
8159 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8160 bfd_byte *erela;
8161 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8162 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8163 bfd_vma r_type_mask;
8164 int r_sym_shift;
8165 unsigned int count = reldata->count;
8166 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8167
8168 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8169 {
8170 swap_in = bed->s->swap_reloc_in;
8171 swap_out = bed->s->swap_reloc_out;
8172 }
8173 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8174 {
8175 swap_in = bed->s->swap_reloca_in;
8176 swap_out = bed->s->swap_reloca_out;
8177 }
8178 else
8179 abort ();
8180
8181 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8182 abort ();
8183
8184 if (bed->s->arch_size == 32)
8185 {
8186 r_type_mask = 0xff;
8187 r_sym_shift = 8;
8188 }
8189 else
8190 {
8191 r_type_mask = 0xffffffff;
8192 r_sym_shift = 32;
8193 }
8194
8195 erela = reldata->hdr->contents;
8196 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8197 {
8198 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8199 unsigned int j;
8200
8201 if (*rel_hash == NULL)
8202 continue;
8203
8204 BFD_ASSERT ((*rel_hash)->indx >= 0);
8205
8206 (*swap_in) (abfd, erela, irela);
8207 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8208 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8209 | (irela[j].r_info & r_type_mask));
8210 (*swap_out) (abfd, irela, erela);
8211 }
8212
8213 if (sort)
8214 {
8215 int (*compare) (const void *, const void *);
8216
8217 if (bed->s->arch_size == 32)
8218 {
8219 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8220 compare = cmp_ext32l_r_offset;
8221 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8222 compare = cmp_ext32b_r_offset;
8223 else
8224 abort ();
8225 }
8226 else
8227 {
8228 #ifdef BFD_HOST_64_BIT
8229 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8230 compare = cmp_ext64l_r_offset;
8231 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8232 compare = cmp_ext64b_r_offset;
8233 else
8234 #endif
8235 abort ();
8236 }
8237 qsort (reldata->hdr->contents, count, reldata->hdr->sh_entsize, compare);
8238 free (reldata->hashes);
8239 reldata->hashes = NULL;
8240 }
8241 }
8242
8243 struct elf_link_sort_rela
8244 {
8245 union {
8246 bfd_vma offset;
8247 bfd_vma sym_mask;
8248 } u;
8249 enum elf_reloc_type_class type;
8250 /* We use this as an array of size int_rels_per_ext_rel. */
8251 Elf_Internal_Rela rela[1];
8252 };
8253
8254 static int
8255 elf_link_sort_cmp1 (const void *A, const void *B)
8256 {
8257 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8258 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8259 int relativea, relativeb;
8260
8261 relativea = a->type == reloc_class_relative;
8262 relativeb = b->type == reloc_class_relative;
8263
8264 if (relativea < relativeb)
8265 return 1;
8266 if (relativea > relativeb)
8267 return -1;
8268 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8269 return -1;
8270 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8271 return 1;
8272 if (a->rela->r_offset < b->rela->r_offset)
8273 return -1;
8274 if (a->rela->r_offset > b->rela->r_offset)
8275 return 1;
8276 return 0;
8277 }
8278
8279 static int
8280 elf_link_sort_cmp2 (const void *A, const void *B)
8281 {
8282 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8283 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8284
8285 if (a->type < b->type)
8286 return -1;
8287 if (a->type > b->type)
8288 return 1;
8289 if (a->u.offset < b->u.offset)
8290 return -1;
8291 if (a->u.offset > b->u.offset)
8292 return 1;
8293 if (a->rela->r_offset < b->rela->r_offset)
8294 return -1;
8295 if (a->rela->r_offset > b->rela->r_offset)
8296 return 1;
8297 return 0;
8298 }
8299
8300 static size_t
8301 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8302 {
8303 asection *dynamic_relocs;
8304 asection *rela_dyn;
8305 asection *rel_dyn;
8306 bfd_size_type count, size;
8307 size_t i, ret, sort_elt, ext_size;
8308 bfd_byte *sort, *s_non_relative, *p;
8309 struct elf_link_sort_rela *sq;
8310 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8311 int i2e = bed->s->int_rels_per_ext_rel;
8312 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8313 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8314 struct bfd_link_order *lo;
8315 bfd_vma r_sym_mask;
8316 bfd_boolean use_rela;
8317
8318 /* Find a dynamic reloc section. */
8319 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8320 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8321 if (rela_dyn != NULL && rela_dyn->size > 0
8322 && rel_dyn != NULL && rel_dyn->size > 0)
8323 {
8324 bfd_boolean use_rela_initialised = FALSE;
8325
8326 /* This is just here to stop gcc from complaining.
8327 It's initialization checking code is not perfect. */
8328 use_rela = TRUE;
8329
8330 /* Both sections are present. Examine the sizes
8331 of the indirect sections to help us choose. */
8332 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8333 if (lo->type == bfd_indirect_link_order)
8334 {
8335 asection *o = lo->u.indirect.section;
8336
8337 if ((o->size % bed->s->sizeof_rela) == 0)
8338 {
8339 if ((o->size % bed->s->sizeof_rel) == 0)
8340 /* Section size is divisible by both rel and rela sizes.
8341 It is of no help to us. */
8342 ;
8343 else
8344 {
8345 /* Section size is only divisible by rela. */
8346 if (use_rela_initialised && (use_rela == FALSE))
8347 {
8348 _bfd_error_handler
8349 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8350 bfd_set_error (bfd_error_invalid_operation);
8351 return 0;
8352 }
8353 else
8354 {
8355 use_rela = TRUE;
8356 use_rela_initialised = TRUE;
8357 }
8358 }
8359 }
8360 else if ((o->size % bed->s->sizeof_rel) == 0)
8361 {
8362 /* Section size is only divisible by rel. */
8363 if (use_rela_initialised && (use_rela == TRUE))
8364 {
8365 _bfd_error_handler
8366 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8367 bfd_set_error (bfd_error_invalid_operation);
8368 return 0;
8369 }
8370 else
8371 {
8372 use_rela = FALSE;
8373 use_rela_initialised = TRUE;
8374 }
8375 }
8376 else
8377 {
8378 /* The section size is not divisible by either - something is wrong. */
8379 _bfd_error_handler
8380 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8381 bfd_set_error (bfd_error_invalid_operation);
8382 return 0;
8383 }
8384 }
8385
8386 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8387 if (lo->type == bfd_indirect_link_order)
8388 {
8389 asection *o = lo->u.indirect.section;
8390
8391 if ((o->size % bed->s->sizeof_rela) == 0)
8392 {
8393 if ((o->size % bed->s->sizeof_rel) == 0)
8394 /* Section size is divisible by both rel and rela sizes.
8395 It is of no help to us. */
8396 ;
8397 else
8398 {
8399 /* Section size is only divisible by rela. */
8400 if (use_rela_initialised && (use_rela == FALSE))
8401 {
8402 _bfd_error_handler
8403 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8404 bfd_set_error (bfd_error_invalid_operation);
8405 return 0;
8406 }
8407 else
8408 {
8409 use_rela = TRUE;
8410 use_rela_initialised = TRUE;
8411 }
8412 }
8413 }
8414 else if ((o->size % bed->s->sizeof_rel) == 0)
8415 {
8416 /* Section size is only divisible by rel. */
8417 if (use_rela_initialised && (use_rela == TRUE))
8418 {
8419 _bfd_error_handler
8420 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8421 bfd_set_error (bfd_error_invalid_operation);
8422 return 0;
8423 }
8424 else
8425 {
8426 use_rela = FALSE;
8427 use_rela_initialised = TRUE;
8428 }
8429 }
8430 else
8431 {
8432 /* The section size is not divisible by either - something is wrong. */
8433 _bfd_error_handler
8434 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8435 bfd_set_error (bfd_error_invalid_operation);
8436 return 0;
8437 }
8438 }
8439
8440 if (! use_rela_initialised)
8441 /* Make a guess. */
8442 use_rela = TRUE;
8443 }
8444 else if (rela_dyn != NULL && rela_dyn->size > 0)
8445 use_rela = TRUE;
8446 else if (rel_dyn != NULL && rel_dyn->size > 0)
8447 use_rela = FALSE;
8448 else
8449 return 0;
8450
8451 if (use_rela)
8452 {
8453 dynamic_relocs = rela_dyn;
8454 ext_size = bed->s->sizeof_rela;
8455 swap_in = bed->s->swap_reloca_in;
8456 swap_out = bed->s->swap_reloca_out;
8457 }
8458 else
8459 {
8460 dynamic_relocs = rel_dyn;
8461 ext_size = bed->s->sizeof_rel;
8462 swap_in = bed->s->swap_reloc_in;
8463 swap_out = bed->s->swap_reloc_out;
8464 }
8465
8466 size = 0;
8467 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8468 if (lo->type == bfd_indirect_link_order)
8469 size += lo->u.indirect.section->size;
8470
8471 if (size != dynamic_relocs->size)
8472 return 0;
8473
8474 sort_elt = (sizeof (struct elf_link_sort_rela)
8475 + (i2e - 1) * sizeof (Elf_Internal_Rela));
8476
8477 count = dynamic_relocs->size / ext_size;
8478 if (count == 0)
8479 return 0;
8480 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
8481
8482 if (sort == NULL)
8483 {
8484 (*info->callbacks->warning)
8485 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8486 return 0;
8487 }
8488
8489 if (bed->s->arch_size == 32)
8490 r_sym_mask = ~(bfd_vma) 0xff;
8491 else
8492 r_sym_mask = ~(bfd_vma) 0xffffffff;
8493
8494 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8495 if (lo->type == bfd_indirect_link_order)
8496 {
8497 bfd_byte *erel, *erelend;
8498 asection *o = lo->u.indirect.section;
8499
8500 if (o->contents == NULL && o->size != 0)
8501 {
8502 /* This is a reloc section that is being handled as a normal
8503 section. See bfd_section_from_shdr. We can't combine
8504 relocs in this case. */
8505 free (sort);
8506 return 0;
8507 }
8508 erel = o->contents;
8509 erelend = o->contents + o->size;
8510 /* FIXME: octets_per_byte. */
8511 p = sort + o->output_offset / ext_size * sort_elt;
8512
8513 while (erel < erelend)
8514 {
8515 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8516
8517 (*swap_in) (abfd, erel, s->rela);
8518 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
8519 s->u.sym_mask = r_sym_mask;
8520 p += sort_elt;
8521 erel += ext_size;
8522 }
8523 }
8524
8525 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8526
8527 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8528 {
8529 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8530 if (s->type != reloc_class_relative)
8531 break;
8532 }
8533 ret = i;
8534 s_non_relative = p;
8535
8536 sq = (struct elf_link_sort_rela *) s_non_relative;
8537 for (; i < count; i++, p += sort_elt)
8538 {
8539 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8540 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8541 sq = sp;
8542 sp->u.offset = sq->rela->r_offset;
8543 }
8544
8545 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8546
8547 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8548 if (lo->type == bfd_indirect_link_order)
8549 {
8550 bfd_byte *erel, *erelend;
8551 asection *o = lo->u.indirect.section;
8552
8553 erel = o->contents;
8554 erelend = o->contents + o->size;
8555 /* FIXME: octets_per_byte. */
8556 p = sort + o->output_offset / ext_size * sort_elt;
8557 while (erel < erelend)
8558 {
8559 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8560 (*swap_out) (abfd, s->rela, erel);
8561 p += sort_elt;
8562 erel += ext_size;
8563 }
8564 }
8565
8566 free (sort);
8567 *psec = dynamic_relocs;
8568 return ret;
8569 }
8570
8571 /* Add a symbol to the output symbol string table. */
8572
8573 static int
8574 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
8575 const char *name,
8576 Elf_Internal_Sym *elfsym,
8577 asection *input_sec,
8578 struct elf_link_hash_entry *h)
8579 {
8580 int (*output_symbol_hook)
8581 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8582 struct elf_link_hash_entry *);
8583 struct elf_link_hash_table *hash_table;
8584 const struct elf_backend_data *bed;
8585 bfd_size_type strtabsize;
8586
8587 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8588
8589 bed = get_elf_backend_data (flinfo->output_bfd);
8590 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8591 if (output_symbol_hook != NULL)
8592 {
8593 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
8594 if (ret != 1)
8595 return ret;
8596 }
8597
8598 if (name == NULL
8599 || *name == '\0'
8600 || (input_sec->flags & SEC_EXCLUDE))
8601 elfsym->st_name = (unsigned long) -1;
8602 else
8603 {
8604 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8605 to get the final offset for st_name. */
8606 elfsym->st_name
8607 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
8608 name, FALSE);
8609 if (elfsym->st_name == (unsigned long) -1)
8610 return 0;
8611 }
8612
8613 hash_table = elf_hash_table (flinfo->info);
8614 strtabsize = hash_table->strtabsize;
8615 if (strtabsize <= hash_table->strtabcount)
8616 {
8617 strtabsize += strtabsize;
8618 hash_table->strtabsize = strtabsize;
8619 strtabsize *= sizeof (*hash_table->strtab);
8620 hash_table->strtab
8621 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
8622 strtabsize);
8623 if (hash_table->strtab == NULL)
8624 return 0;
8625 }
8626 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
8627 hash_table->strtab[hash_table->strtabcount].dest_index
8628 = hash_table->strtabcount;
8629 hash_table->strtab[hash_table->strtabcount].destshndx_index
8630 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
8631
8632 bfd_get_symcount (flinfo->output_bfd) += 1;
8633 hash_table->strtabcount += 1;
8634
8635 return 1;
8636 }
8637
8638 /* Swap symbols out to the symbol table and flush the output symbols to
8639 the file. */
8640
8641 static bfd_boolean
8642 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
8643 {
8644 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
8645 bfd_size_type amt, i;
8646 const struct elf_backend_data *bed;
8647 bfd_byte *symbuf;
8648 Elf_Internal_Shdr *hdr;
8649 file_ptr pos;
8650 bfd_boolean ret;
8651
8652 if (!hash_table->strtabcount)
8653 return TRUE;
8654
8655 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8656
8657 bed = get_elf_backend_data (flinfo->output_bfd);
8658
8659 amt = bed->s->sizeof_sym * hash_table->strtabcount;
8660 symbuf = (bfd_byte *) bfd_malloc (amt);
8661 if (symbuf == NULL)
8662 return FALSE;
8663
8664 if (flinfo->symshndxbuf)
8665 {
8666 amt = (sizeof (Elf_External_Sym_Shndx)
8667 * (bfd_get_symcount (flinfo->output_bfd)));
8668 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
8669 if (flinfo->symshndxbuf == NULL)
8670 {
8671 free (symbuf);
8672 return FALSE;
8673 }
8674 }
8675
8676 for (i = 0; i < hash_table->strtabcount; i++)
8677 {
8678 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
8679 if (elfsym->sym.st_name == (unsigned long) -1)
8680 elfsym->sym.st_name = 0;
8681 else
8682 elfsym->sym.st_name
8683 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
8684 elfsym->sym.st_name);
8685 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
8686 ((bfd_byte *) symbuf
8687 + (elfsym->dest_index
8688 * bed->s->sizeof_sym)),
8689 (flinfo->symshndxbuf
8690 + elfsym->destshndx_index));
8691 }
8692
8693 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
8694 pos = hdr->sh_offset + hdr->sh_size;
8695 amt = hash_table->strtabcount * bed->s->sizeof_sym;
8696 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
8697 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
8698 {
8699 hdr->sh_size += amt;
8700 ret = TRUE;
8701 }
8702 else
8703 ret = FALSE;
8704
8705 free (symbuf);
8706
8707 free (hash_table->strtab);
8708 hash_table->strtab = NULL;
8709
8710 return ret;
8711 }
8712
8713 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8714
8715 static bfd_boolean
8716 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8717 {
8718 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
8719 && sym->st_shndx < SHN_LORESERVE)
8720 {
8721 /* The gABI doesn't support dynamic symbols in output sections
8722 beyond 64k. */
8723 (*_bfd_error_handler)
8724 (_("%B: Too many sections: %d (>= %d)"),
8725 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
8726 bfd_set_error (bfd_error_nonrepresentable_section);
8727 return FALSE;
8728 }
8729 return TRUE;
8730 }
8731
8732 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8733 allowing an unsatisfied unversioned symbol in the DSO to match a
8734 versioned symbol that would normally require an explicit version.
8735 We also handle the case that a DSO references a hidden symbol
8736 which may be satisfied by a versioned symbol in another DSO. */
8737
8738 static bfd_boolean
8739 elf_link_check_versioned_symbol (struct bfd_link_info *info,
8740 const struct elf_backend_data *bed,
8741 struct elf_link_hash_entry *h)
8742 {
8743 bfd *abfd;
8744 struct elf_link_loaded_list *loaded;
8745
8746 if (!is_elf_hash_table (info->hash))
8747 return FALSE;
8748
8749 /* Check indirect symbol. */
8750 while (h->root.type == bfd_link_hash_indirect)
8751 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8752
8753 switch (h->root.type)
8754 {
8755 default:
8756 abfd = NULL;
8757 break;
8758
8759 case bfd_link_hash_undefined:
8760 case bfd_link_hash_undefweak:
8761 abfd = h->root.u.undef.abfd;
8762 if ((abfd->flags & DYNAMIC) == 0
8763 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
8764 return FALSE;
8765 break;
8766
8767 case bfd_link_hash_defined:
8768 case bfd_link_hash_defweak:
8769 abfd = h->root.u.def.section->owner;
8770 break;
8771
8772 case bfd_link_hash_common:
8773 abfd = h->root.u.c.p->section->owner;
8774 break;
8775 }
8776 BFD_ASSERT (abfd != NULL);
8777
8778 for (loaded = elf_hash_table (info)->loaded;
8779 loaded != NULL;
8780 loaded = loaded->next)
8781 {
8782 bfd *input;
8783 Elf_Internal_Shdr *hdr;
8784 bfd_size_type symcount;
8785 bfd_size_type extsymcount;
8786 bfd_size_type extsymoff;
8787 Elf_Internal_Shdr *versymhdr;
8788 Elf_Internal_Sym *isym;
8789 Elf_Internal_Sym *isymend;
8790 Elf_Internal_Sym *isymbuf;
8791 Elf_External_Versym *ever;
8792 Elf_External_Versym *extversym;
8793
8794 input = loaded->abfd;
8795
8796 /* We check each DSO for a possible hidden versioned definition. */
8797 if (input == abfd
8798 || (input->flags & DYNAMIC) == 0
8799 || elf_dynversym (input) == 0)
8800 continue;
8801
8802 hdr = &elf_tdata (input)->dynsymtab_hdr;
8803
8804 symcount = hdr->sh_size / bed->s->sizeof_sym;
8805 if (elf_bad_symtab (input))
8806 {
8807 extsymcount = symcount;
8808 extsymoff = 0;
8809 }
8810 else
8811 {
8812 extsymcount = symcount - hdr->sh_info;
8813 extsymoff = hdr->sh_info;
8814 }
8815
8816 if (extsymcount == 0)
8817 continue;
8818
8819 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
8820 NULL, NULL, NULL);
8821 if (isymbuf == NULL)
8822 return FALSE;
8823
8824 /* Read in any version definitions. */
8825 versymhdr = &elf_tdata (input)->dynversym_hdr;
8826 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
8827 if (extversym == NULL)
8828 goto error_ret;
8829
8830 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
8831 || (bfd_bread (extversym, versymhdr->sh_size, input)
8832 != versymhdr->sh_size))
8833 {
8834 free (extversym);
8835 error_ret:
8836 free (isymbuf);
8837 return FALSE;
8838 }
8839
8840 ever = extversym + extsymoff;
8841 isymend = isymbuf + extsymcount;
8842 for (isym = isymbuf; isym < isymend; isym++, ever++)
8843 {
8844 const char *name;
8845 Elf_Internal_Versym iver;
8846 unsigned short version_index;
8847
8848 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
8849 || isym->st_shndx == SHN_UNDEF)
8850 continue;
8851
8852 name = bfd_elf_string_from_elf_section (input,
8853 hdr->sh_link,
8854 isym->st_name);
8855 if (strcmp (name, h->root.root.string) != 0)
8856 continue;
8857
8858 _bfd_elf_swap_versym_in (input, ever, &iver);
8859
8860 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
8861 && !(h->def_regular
8862 && h->forced_local))
8863 {
8864 /* If we have a non-hidden versioned sym, then it should
8865 have provided a definition for the undefined sym unless
8866 it is defined in a non-shared object and forced local.
8867 */
8868 abort ();
8869 }
8870
8871 version_index = iver.vs_vers & VERSYM_VERSION;
8872 if (version_index == 1 || version_index == 2)
8873 {
8874 /* This is the base or first version. We can use it. */
8875 free (extversym);
8876 free (isymbuf);
8877 return TRUE;
8878 }
8879 }
8880
8881 free (extversym);
8882 free (isymbuf);
8883 }
8884
8885 return FALSE;
8886 }
8887
8888 /* Add an external symbol to the symbol table. This is called from
8889 the hash table traversal routine. When generating a shared object,
8890 we go through the symbol table twice. The first time we output
8891 anything that might have been forced to local scope in a version
8892 script. The second time we output the symbols that are still
8893 global symbols. */
8894
8895 static bfd_boolean
8896 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
8897 {
8898 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
8899 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8900 struct elf_final_link_info *flinfo = eoinfo->flinfo;
8901 bfd_boolean strip;
8902 Elf_Internal_Sym sym;
8903 asection *input_sec;
8904 const struct elf_backend_data *bed;
8905 long indx;
8906 int ret;
8907
8908 if (h->root.type == bfd_link_hash_warning)
8909 {
8910 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8911 if (h->root.type == bfd_link_hash_new)
8912 return TRUE;
8913 }
8914
8915 /* Decide whether to output this symbol in this pass. */
8916 if (eoinfo->localsyms)
8917 {
8918 if (!h->forced_local)
8919 return TRUE;
8920 }
8921 else
8922 {
8923 if (h->forced_local)
8924 return TRUE;
8925 }
8926
8927 bed = get_elf_backend_data (flinfo->output_bfd);
8928
8929 if (h->root.type == bfd_link_hash_undefined)
8930 {
8931 /* If we have an undefined symbol reference here then it must have
8932 come from a shared library that is being linked in. (Undefined
8933 references in regular files have already been handled unless
8934 they are in unreferenced sections which are removed by garbage
8935 collection). */
8936 bfd_boolean ignore_undef = FALSE;
8937
8938 /* Some symbols may be special in that the fact that they're
8939 undefined can be safely ignored - let backend determine that. */
8940 if (bed->elf_backend_ignore_undef_symbol)
8941 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
8942
8943 /* If we are reporting errors for this situation then do so now. */
8944 if (!ignore_undef
8945 && h->ref_dynamic
8946 && (!h->ref_regular || flinfo->info->gc_sections)
8947 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
8948 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
8949 {
8950 if (!(flinfo->info->callbacks->undefined_symbol
8951 (flinfo->info, h->root.root.string,
8952 h->ref_regular ? NULL : h->root.u.undef.abfd,
8953 NULL, 0,
8954 (flinfo->info->unresolved_syms_in_shared_libs
8955 == RM_GENERATE_ERROR))))
8956 {
8957 bfd_set_error (bfd_error_bad_value);
8958 eoinfo->failed = TRUE;
8959 return FALSE;
8960 }
8961 }
8962 }
8963
8964 /* We should also warn if a forced local symbol is referenced from
8965 shared libraries. */
8966 if (flinfo->info->executable
8967 && h->forced_local
8968 && h->ref_dynamic
8969 && h->def_regular
8970 && !h->dynamic_def
8971 && h->ref_dynamic_nonweak
8972 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
8973 {
8974 bfd *def_bfd;
8975 const char *msg;
8976 struct elf_link_hash_entry *hi = h;
8977
8978 /* Check indirect symbol. */
8979 while (hi->root.type == bfd_link_hash_indirect)
8980 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
8981
8982 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
8983 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
8984 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
8985 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
8986 else
8987 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
8988 def_bfd = flinfo->output_bfd;
8989 if (hi->root.u.def.section != bfd_abs_section_ptr)
8990 def_bfd = hi->root.u.def.section->owner;
8991 (*_bfd_error_handler) (msg, flinfo->output_bfd, def_bfd,
8992 h->root.root.string);
8993 bfd_set_error (bfd_error_bad_value);
8994 eoinfo->failed = TRUE;
8995 return FALSE;
8996 }
8997
8998 /* We don't want to output symbols that have never been mentioned by
8999 a regular file, or that we have been told to strip. However, if
9000 h->indx is set to -2, the symbol is used by a reloc and we must
9001 output it. */
9002 strip = FALSE;
9003 if (h->indx == -2)
9004 ;
9005 else if ((h->def_dynamic
9006 || h->ref_dynamic
9007 || h->root.type == bfd_link_hash_new)
9008 && !h->def_regular
9009 && !h->ref_regular)
9010 strip = TRUE;
9011 else if (flinfo->info->strip == strip_all)
9012 strip = TRUE;
9013 else if (flinfo->info->strip == strip_some
9014 && bfd_hash_lookup (flinfo->info->keep_hash,
9015 h->root.root.string, FALSE, FALSE) == NULL)
9016 strip = TRUE;
9017 else if ((h->root.type == bfd_link_hash_defined
9018 || h->root.type == bfd_link_hash_defweak)
9019 && ((flinfo->info->strip_discarded
9020 && discarded_section (h->root.u.def.section))
9021 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9022 && h->root.u.def.section->owner != NULL
9023 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9024 strip = TRUE;
9025 else if ((h->root.type == bfd_link_hash_undefined
9026 || h->root.type == bfd_link_hash_undefweak)
9027 && h->root.u.undef.abfd != NULL
9028 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9029 strip = TRUE;
9030
9031 /* If we're stripping it, and it's not a dynamic symbol, there's
9032 nothing else to do. However, if it is a forced local symbol or
9033 an ifunc symbol we need to give the backend finish_dynamic_symbol
9034 function a chance to make it dynamic. */
9035 if (strip
9036 && h->dynindx == -1
9037 && h->type != STT_GNU_IFUNC
9038 && !h->forced_local)
9039 return TRUE;
9040
9041 sym.st_value = 0;
9042 sym.st_size = h->size;
9043 sym.st_other = h->other;
9044 if (h->forced_local)
9045 {
9046 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
9047 /* Turn off visibility on local symbol. */
9048 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9049 }
9050 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9051 else if (h->unique_global && h->def_regular)
9052 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, h->type);
9053 else if (h->root.type == bfd_link_hash_undefweak
9054 || h->root.type == bfd_link_hash_defweak)
9055 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
9056 else
9057 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
9058 sym.st_target_internal = h->target_internal;
9059
9060 switch (h->root.type)
9061 {
9062 default:
9063 case bfd_link_hash_new:
9064 case bfd_link_hash_warning:
9065 abort ();
9066 return FALSE;
9067
9068 case bfd_link_hash_undefined:
9069 case bfd_link_hash_undefweak:
9070 input_sec = bfd_und_section_ptr;
9071 sym.st_shndx = SHN_UNDEF;
9072 break;
9073
9074 case bfd_link_hash_defined:
9075 case bfd_link_hash_defweak:
9076 {
9077 input_sec = h->root.u.def.section;
9078 if (input_sec->output_section != NULL)
9079 {
9080 sym.st_shndx =
9081 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9082 input_sec->output_section);
9083 if (sym.st_shndx == SHN_BAD)
9084 {
9085 (*_bfd_error_handler)
9086 (_("%B: could not find output section %A for input section %A"),
9087 flinfo->output_bfd, input_sec->output_section, input_sec);
9088 bfd_set_error (bfd_error_nonrepresentable_section);
9089 eoinfo->failed = TRUE;
9090 return FALSE;
9091 }
9092
9093 /* ELF symbols in relocatable files are section relative,
9094 but in nonrelocatable files they are virtual
9095 addresses. */
9096 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9097 if (!flinfo->info->relocatable)
9098 {
9099 sym.st_value += input_sec->output_section->vma;
9100 if (h->type == STT_TLS)
9101 {
9102 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9103 if (tls_sec != NULL)
9104 sym.st_value -= tls_sec->vma;
9105 }
9106 }
9107 }
9108 else
9109 {
9110 BFD_ASSERT (input_sec->owner == NULL
9111 || (input_sec->owner->flags & DYNAMIC) != 0);
9112 sym.st_shndx = SHN_UNDEF;
9113 input_sec = bfd_und_section_ptr;
9114 }
9115 }
9116 break;
9117
9118 case bfd_link_hash_common:
9119 input_sec = h->root.u.c.p->section;
9120 sym.st_shndx = bed->common_section_index (input_sec);
9121 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9122 break;
9123
9124 case bfd_link_hash_indirect:
9125 /* These symbols are created by symbol versioning. They point
9126 to the decorated version of the name. For example, if the
9127 symbol foo@@GNU_1.2 is the default, which should be used when
9128 foo is used with no version, then we add an indirect symbol
9129 foo which points to foo@@GNU_1.2. We ignore these symbols,
9130 since the indirected symbol is already in the hash table. */
9131 return TRUE;
9132 }
9133
9134 /* Give the processor backend a chance to tweak the symbol value,
9135 and also to finish up anything that needs to be done for this
9136 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9137 forced local syms when non-shared is due to a historical quirk.
9138 STT_GNU_IFUNC symbol must go through PLT. */
9139 if ((h->type == STT_GNU_IFUNC
9140 && h->def_regular
9141 && !flinfo->info->relocatable)
9142 || ((h->dynindx != -1
9143 || h->forced_local)
9144 && ((flinfo->info->shared
9145 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9146 || h->root.type != bfd_link_hash_undefweak))
9147 || !h->forced_local)
9148 && elf_hash_table (flinfo->info)->dynamic_sections_created))
9149 {
9150 if (! ((*bed->elf_backend_finish_dynamic_symbol)
9151 (flinfo->output_bfd, flinfo->info, h, &sym)))
9152 {
9153 eoinfo->failed = TRUE;
9154 return FALSE;
9155 }
9156 }
9157
9158 /* If we are marking the symbol as undefined, and there are no
9159 non-weak references to this symbol from a regular object, then
9160 mark the symbol as weak undefined; if there are non-weak
9161 references, mark the symbol as strong. We can't do this earlier,
9162 because it might not be marked as undefined until the
9163 finish_dynamic_symbol routine gets through with it. */
9164 if (sym.st_shndx == SHN_UNDEF
9165 && h->ref_regular
9166 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9167 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9168 {
9169 int bindtype;
9170 unsigned int type = ELF_ST_TYPE (sym.st_info);
9171
9172 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9173 if (type == STT_GNU_IFUNC)
9174 type = STT_FUNC;
9175
9176 if (h->ref_regular_nonweak)
9177 bindtype = STB_GLOBAL;
9178 else
9179 bindtype = STB_WEAK;
9180 sym.st_info = ELF_ST_INFO (bindtype, type);
9181 }
9182
9183 /* If this is a symbol defined in a dynamic library, don't use the
9184 symbol size from the dynamic library. Relinking an executable
9185 against a new library may introduce gratuitous changes in the
9186 executable's symbols if we keep the size. */
9187 if (sym.st_shndx == SHN_UNDEF
9188 && !h->def_regular
9189 && h->def_dynamic)
9190 sym.st_size = 0;
9191
9192 /* If a non-weak symbol with non-default visibility is not defined
9193 locally, it is a fatal error. */
9194 if (!flinfo->info->relocatable
9195 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9196 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9197 && h->root.type == bfd_link_hash_undefined
9198 && !h->def_regular)
9199 {
9200 const char *msg;
9201
9202 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9203 msg = _("%B: protected symbol `%s' isn't defined");
9204 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9205 msg = _("%B: internal symbol `%s' isn't defined");
9206 else
9207 msg = _("%B: hidden symbol `%s' isn't defined");
9208 (*_bfd_error_handler) (msg, flinfo->output_bfd, h->root.root.string);
9209 bfd_set_error (bfd_error_bad_value);
9210 eoinfo->failed = TRUE;
9211 return FALSE;
9212 }
9213
9214 /* If this symbol should be put in the .dynsym section, then put it
9215 there now. We already know the symbol index. We also fill in
9216 the entry in the .hash section. */
9217 if (flinfo->dynsym_sec != NULL
9218 && h->dynindx != -1
9219 && elf_hash_table (flinfo->info)->dynamic_sections_created)
9220 {
9221 bfd_byte *esym;
9222
9223 /* Since there is no version information in the dynamic string,
9224 if there is no version info in symbol version section, we will
9225 have a run-time problem if not linking executable, referenced
9226 by shared library, or not locally defined. */
9227 if (h->verinfo.verdef == NULL
9228 && (!flinfo->info->executable
9229 || h->ref_dynamic
9230 || !h->def_regular))
9231 {
9232 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9233
9234 if (p && p [1] != '\0')
9235 {
9236 (*_bfd_error_handler)
9237 (_("%B: No symbol version section for versioned symbol `%s'"),
9238 flinfo->output_bfd, h->root.root.string);
9239 eoinfo->failed = TRUE;
9240 return FALSE;
9241 }
9242 }
9243
9244 sym.st_name = h->dynstr_index;
9245 esym = flinfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
9246 if (!check_dynsym (flinfo->output_bfd, &sym))
9247 {
9248 eoinfo->failed = TRUE;
9249 return FALSE;
9250 }
9251 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
9252
9253 if (flinfo->hash_sec != NULL)
9254 {
9255 size_t hash_entry_size;
9256 bfd_byte *bucketpos;
9257 bfd_vma chain;
9258 size_t bucketcount;
9259 size_t bucket;
9260
9261 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
9262 bucket = h->u.elf_hash_value % bucketcount;
9263
9264 hash_entry_size
9265 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9266 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
9267 + (bucket + 2) * hash_entry_size);
9268 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9269 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9270 bucketpos);
9271 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9272 ((bfd_byte *) flinfo->hash_sec->contents
9273 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9274 }
9275
9276 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
9277 {
9278 Elf_Internal_Versym iversym;
9279 Elf_External_Versym *eversym;
9280
9281 if (!h->def_regular)
9282 {
9283 if (h->verinfo.verdef == NULL
9284 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9285 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
9286 iversym.vs_vers = 0;
9287 else
9288 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9289 }
9290 else
9291 {
9292 if (h->verinfo.vertree == NULL)
9293 iversym.vs_vers = 1;
9294 else
9295 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
9296 if (flinfo->info->create_default_symver)
9297 iversym.vs_vers++;
9298 }
9299
9300 if (h->hidden)
9301 iversym.vs_vers |= VERSYM_HIDDEN;
9302
9303 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
9304 eversym += h->dynindx;
9305 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
9306 }
9307 }
9308
9309 /* If the symbol is undefined, and we didn't output it to .dynsym,
9310 strip it from .symtab too. Obviously we can't do this for
9311 relocatable output or when needed for --emit-relocs. */
9312 else if (input_sec == bfd_und_section_ptr
9313 && h->indx != -2
9314 && !flinfo->info->relocatable)
9315 return TRUE;
9316 /* Also strip others that we couldn't earlier due to dynamic symbol
9317 processing. */
9318 if (strip)
9319 return TRUE;
9320 if ((input_sec->flags & SEC_EXCLUDE) != 0)
9321 return TRUE;
9322
9323 /* Output a FILE symbol so that following locals are not associated
9324 with the wrong input file. We need one for forced local symbols
9325 if we've seen more than one FILE symbol or when we have exactly
9326 one FILE symbol but global symbols are present in a file other
9327 than the one with the FILE symbol. We also need one if linker
9328 defined symbols are present. In practice these conditions are
9329 always met, so just emit the FILE symbol unconditionally. */
9330 if (eoinfo->localsyms
9331 && !eoinfo->file_sym_done
9332 && eoinfo->flinfo->filesym_count != 0)
9333 {
9334 Elf_Internal_Sym fsym;
9335
9336 memset (&fsym, 0, sizeof (fsym));
9337 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9338 fsym.st_shndx = SHN_ABS;
9339 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9340 bfd_und_section_ptr, NULL))
9341 return FALSE;
9342
9343 eoinfo->file_sym_done = TRUE;
9344 }
9345
9346 indx = bfd_get_symcount (flinfo->output_bfd);
9347 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9348 input_sec, h);
9349 if (ret == 0)
9350 {
9351 eoinfo->failed = TRUE;
9352 return FALSE;
9353 }
9354 else if (ret == 1)
9355 h->indx = indx;
9356 else if (h->indx == -2)
9357 abort();
9358
9359 return TRUE;
9360 }
9361
9362 /* Return TRUE if special handling is done for relocs in SEC against
9363 symbols defined in discarded sections. */
9364
9365 static bfd_boolean
9366 elf_section_ignore_discarded_relocs (asection *sec)
9367 {
9368 const struct elf_backend_data *bed;
9369
9370 switch (sec->sec_info_type)
9371 {
9372 case SEC_INFO_TYPE_STABS:
9373 case SEC_INFO_TYPE_EH_FRAME:
9374 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
9375 return TRUE;
9376 default:
9377 break;
9378 }
9379
9380 bed = get_elf_backend_data (sec->owner);
9381 if (bed->elf_backend_ignore_discarded_relocs != NULL
9382 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9383 return TRUE;
9384
9385 return FALSE;
9386 }
9387
9388 /* Return a mask saying how ld should treat relocations in SEC against
9389 symbols defined in discarded sections. If this function returns
9390 COMPLAIN set, ld will issue a warning message. If this function
9391 returns PRETEND set, and the discarded section was link-once and the
9392 same size as the kept link-once section, ld will pretend that the
9393 symbol was actually defined in the kept section. Otherwise ld will
9394 zero the reloc (at least that is the intent, but some cooperation by
9395 the target dependent code is needed, particularly for REL targets). */
9396
9397 unsigned int
9398 _bfd_elf_default_action_discarded (asection *sec)
9399 {
9400 if (sec->flags & SEC_DEBUGGING)
9401 return PRETEND;
9402
9403 if (strcmp (".eh_frame", sec->name) == 0)
9404 return 0;
9405
9406 if (strcmp (".gcc_except_table", sec->name) == 0)
9407 return 0;
9408
9409 return COMPLAIN | PRETEND;
9410 }
9411
9412 /* Find a match between a section and a member of a section group. */
9413
9414 static asection *
9415 match_group_member (asection *sec, asection *group,
9416 struct bfd_link_info *info)
9417 {
9418 asection *first = elf_next_in_group (group);
9419 asection *s = first;
9420
9421 while (s != NULL)
9422 {
9423 if (bfd_elf_match_symbols_in_sections (s, sec, info))
9424 return s;
9425
9426 s = elf_next_in_group (s);
9427 if (s == first)
9428 break;
9429 }
9430
9431 return NULL;
9432 }
9433
9434 /* Check if the kept section of a discarded section SEC can be used
9435 to replace it. Return the replacement if it is OK. Otherwise return
9436 NULL. */
9437
9438 asection *
9439 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
9440 {
9441 asection *kept;
9442
9443 kept = sec->kept_section;
9444 if (kept != NULL)
9445 {
9446 if ((kept->flags & SEC_GROUP) != 0)
9447 kept = match_group_member (sec, kept, info);
9448 if (kept != NULL
9449 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9450 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
9451 kept = NULL;
9452 sec->kept_section = kept;
9453 }
9454 return kept;
9455 }
9456
9457 /* Link an input file into the linker output file. This function
9458 handles all the sections and relocations of the input file at once.
9459 This is so that we only have to read the local symbols once, and
9460 don't have to keep them in memory. */
9461
9462 static bfd_boolean
9463 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
9464 {
9465 int (*relocate_section)
9466 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9467 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9468 bfd *output_bfd;
9469 Elf_Internal_Shdr *symtab_hdr;
9470 size_t locsymcount;
9471 size_t extsymoff;
9472 Elf_Internal_Sym *isymbuf;
9473 Elf_Internal_Sym *isym;
9474 Elf_Internal_Sym *isymend;
9475 long *pindex;
9476 asection **ppsection;
9477 asection *o;
9478 const struct elf_backend_data *bed;
9479 struct elf_link_hash_entry **sym_hashes;
9480 bfd_size_type address_size;
9481 bfd_vma r_type_mask;
9482 int r_sym_shift;
9483 bfd_boolean have_file_sym = FALSE;
9484
9485 output_bfd = flinfo->output_bfd;
9486 bed = get_elf_backend_data (output_bfd);
9487 relocate_section = bed->elf_backend_relocate_section;
9488
9489 /* If this is a dynamic object, we don't want to do anything here:
9490 we don't want the local symbols, and we don't want the section
9491 contents. */
9492 if ((input_bfd->flags & DYNAMIC) != 0)
9493 return TRUE;
9494
9495 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9496 if (elf_bad_symtab (input_bfd))
9497 {
9498 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9499 extsymoff = 0;
9500 }
9501 else
9502 {
9503 locsymcount = symtab_hdr->sh_info;
9504 extsymoff = symtab_hdr->sh_info;
9505 }
9506
9507 /* Read the local symbols. */
9508 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9509 if (isymbuf == NULL && locsymcount != 0)
9510 {
9511 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
9512 flinfo->internal_syms,
9513 flinfo->external_syms,
9514 flinfo->locsym_shndx);
9515 if (isymbuf == NULL)
9516 return FALSE;
9517 }
9518
9519 /* Find local symbol sections and adjust values of symbols in
9520 SEC_MERGE sections. Write out those local symbols we know are
9521 going into the output file. */
9522 isymend = isymbuf + locsymcount;
9523 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
9524 isym < isymend;
9525 isym++, pindex++, ppsection++)
9526 {
9527 asection *isec;
9528 const char *name;
9529 Elf_Internal_Sym osym;
9530 long indx;
9531 int ret;
9532
9533 *pindex = -1;
9534
9535 if (elf_bad_symtab (input_bfd))
9536 {
9537 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9538 {
9539 *ppsection = NULL;
9540 continue;
9541 }
9542 }
9543
9544 if (isym->st_shndx == SHN_UNDEF)
9545 isec = bfd_und_section_ptr;
9546 else if (isym->st_shndx == SHN_ABS)
9547 isec = bfd_abs_section_ptr;
9548 else if (isym->st_shndx == SHN_COMMON)
9549 isec = bfd_com_section_ptr;
9550 else
9551 {
9552 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9553 if (isec == NULL)
9554 {
9555 /* Don't attempt to output symbols with st_shnx in the
9556 reserved range other than SHN_ABS and SHN_COMMON. */
9557 *ppsection = NULL;
9558 continue;
9559 }
9560 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
9561 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9562 isym->st_value =
9563 _bfd_merged_section_offset (output_bfd, &isec,
9564 elf_section_data (isec)->sec_info,
9565 isym->st_value);
9566 }
9567
9568 *ppsection = isec;
9569
9570 /* Don't output the first, undefined, symbol. In fact, don't
9571 output any undefined local symbol. */
9572 if (isec == bfd_und_section_ptr)
9573 continue;
9574
9575 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9576 {
9577 /* We never output section symbols. Instead, we use the
9578 section symbol of the corresponding section in the output
9579 file. */
9580 continue;
9581 }
9582
9583 /* If we are stripping all symbols, we don't want to output this
9584 one. */
9585 if (flinfo->info->strip == strip_all)
9586 continue;
9587
9588 /* If we are discarding all local symbols, we don't want to
9589 output this one. If we are generating a relocatable output
9590 file, then some of the local symbols may be required by
9591 relocs; we output them below as we discover that they are
9592 needed. */
9593 if (flinfo->info->discard == discard_all)
9594 continue;
9595
9596 /* If this symbol is defined in a section which we are
9597 discarding, we don't need to keep it. */
9598 if (isym->st_shndx != SHN_UNDEF
9599 && isym->st_shndx < SHN_LORESERVE
9600 && bfd_section_removed_from_list (output_bfd,
9601 isec->output_section))
9602 continue;
9603
9604 /* Get the name of the symbol. */
9605 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9606 isym->st_name);
9607 if (name == NULL)
9608 return FALSE;
9609
9610 /* See if we are discarding symbols with this name. */
9611 if ((flinfo->info->strip == strip_some
9612 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
9613 == NULL))
9614 || (((flinfo->info->discard == discard_sec_merge
9615 && (isec->flags & SEC_MERGE) && !flinfo->info->relocatable)
9616 || flinfo->info->discard == discard_l)
9617 && bfd_is_local_label_name (input_bfd, name)))
9618 continue;
9619
9620 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
9621 {
9622 if (input_bfd->lto_output)
9623 /* -flto puts a temp file name here. This means builds
9624 are not reproducible. Discard the symbol. */
9625 continue;
9626 have_file_sym = TRUE;
9627 flinfo->filesym_count += 1;
9628 }
9629 if (!have_file_sym)
9630 {
9631 /* In the absence of debug info, bfd_find_nearest_line uses
9632 FILE symbols to determine the source file for local
9633 function symbols. Provide a FILE symbol here if input
9634 files lack such, so that their symbols won't be
9635 associated with a previous input file. It's not the
9636 source file, but the best we can do. */
9637 have_file_sym = TRUE;
9638 flinfo->filesym_count += 1;
9639 memset (&osym, 0, sizeof (osym));
9640 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9641 osym.st_shndx = SHN_ABS;
9642 if (!elf_link_output_symstrtab (flinfo,
9643 (input_bfd->lto_output ? NULL
9644 : input_bfd->filename),
9645 &osym, bfd_abs_section_ptr,
9646 NULL))
9647 return FALSE;
9648 }
9649
9650 osym = *isym;
9651
9652 /* Adjust the section index for the output file. */
9653 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9654 isec->output_section);
9655 if (osym.st_shndx == SHN_BAD)
9656 return FALSE;
9657
9658 /* ELF symbols in relocatable files are section relative, but
9659 in executable files they are virtual addresses. Note that
9660 this code assumes that all ELF sections have an associated
9661 BFD section with a reasonable value for output_offset; below
9662 we assume that they also have a reasonable value for
9663 output_section. Any special sections must be set up to meet
9664 these requirements. */
9665 osym.st_value += isec->output_offset;
9666 if (!flinfo->info->relocatable)
9667 {
9668 osym.st_value += isec->output_section->vma;
9669 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
9670 {
9671 /* STT_TLS symbols are relative to PT_TLS segment base. */
9672 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
9673 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
9674 }
9675 }
9676
9677 indx = bfd_get_symcount (output_bfd);
9678 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
9679 if (ret == 0)
9680 return FALSE;
9681 else if (ret == 1)
9682 *pindex = indx;
9683 }
9684
9685 if (bed->s->arch_size == 32)
9686 {
9687 r_type_mask = 0xff;
9688 r_sym_shift = 8;
9689 address_size = 4;
9690 }
9691 else
9692 {
9693 r_type_mask = 0xffffffff;
9694 r_sym_shift = 32;
9695 address_size = 8;
9696 }
9697
9698 /* Relocate the contents of each section. */
9699 sym_hashes = elf_sym_hashes (input_bfd);
9700 for (o = input_bfd->sections; o != NULL; o = o->next)
9701 {
9702 bfd_byte *contents;
9703
9704 if (! o->linker_mark)
9705 {
9706 /* This section was omitted from the link. */
9707 continue;
9708 }
9709
9710 if (flinfo->info->relocatable
9711 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
9712 {
9713 /* Deal with the group signature symbol. */
9714 struct bfd_elf_section_data *sec_data = elf_section_data (o);
9715 unsigned long symndx = sec_data->this_hdr.sh_info;
9716 asection *osec = o->output_section;
9717
9718 if (symndx >= locsymcount
9719 || (elf_bad_symtab (input_bfd)
9720 && flinfo->sections[symndx] == NULL))
9721 {
9722 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
9723 while (h->root.type == bfd_link_hash_indirect
9724 || h->root.type == bfd_link_hash_warning)
9725 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9726 /* Arrange for symbol to be output. */
9727 h->indx = -2;
9728 elf_section_data (osec)->this_hdr.sh_info = -2;
9729 }
9730 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
9731 {
9732 /* We'll use the output section target_index. */
9733 asection *sec = flinfo->sections[symndx]->output_section;
9734 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
9735 }
9736 else
9737 {
9738 if (flinfo->indices[symndx] == -1)
9739 {
9740 /* Otherwise output the local symbol now. */
9741 Elf_Internal_Sym sym = isymbuf[symndx];
9742 asection *sec = flinfo->sections[symndx]->output_section;
9743 const char *name;
9744 long indx;
9745 int ret;
9746
9747 name = bfd_elf_string_from_elf_section (input_bfd,
9748 symtab_hdr->sh_link,
9749 sym.st_name);
9750 if (name == NULL)
9751 return FALSE;
9752
9753 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9754 sec);
9755 if (sym.st_shndx == SHN_BAD)
9756 return FALSE;
9757
9758 sym.st_value += o->output_offset;
9759
9760 indx = bfd_get_symcount (output_bfd);
9761 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
9762 NULL);
9763 if (ret == 0)
9764 return FALSE;
9765 else if (ret == 1)
9766 flinfo->indices[symndx] = indx;
9767 else
9768 abort ();
9769 }
9770 elf_section_data (osec)->this_hdr.sh_info
9771 = flinfo->indices[symndx];
9772 }
9773 }
9774
9775 if ((o->flags & SEC_HAS_CONTENTS) == 0
9776 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
9777 continue;
9778
9779 if ((o->flags & SEC_LINKER_CREATED) != 0)
9780 {
9781 /* Section was created by _bfd_elf_link_create_dynamic_sections
9782 or somesuch. */
9783 continue;
9784 }
9785
9786 /* Get the contents of the section. They have been cached by a
9787 relaxation routine. Note that o is a section in an input
9788 file, so the contents field will not have been set by any of
9789 the routines which work on output files. */
9790 if (elf_section_data (o)->this_hdr.contents != NULL)
9791 {
9792 contents = elf_section_data (o)->this_hdr.contents;
9793 if (bed->caches_rawsize
9794 && o->rawsize != 0
9795 && o->rawsize < o->size)
9796 {
9797 memcpy (flinfo->contents, contents, o->rawsize);
9798 contents = flinfo->contents;
9799 }
9800 }
9801 else
9802 {
9803 contents = flinfo->contents;
9804 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
9805 return FALSE;
9806 }
9807
9808 if ((o->flags & SEC_RELOC) != 0)
9809 {
9810 Elf_Internal_Rela *internal_relocs;
9811 Elf_Internal_Rela *rel, *relend;
9812 int action_discarded;
9813 int ret;
9814
9815 /* Get the swapped relocs. */
9816 internal_relocs
9817 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
9818 flinfo->internal_relocs, FALSE);
9819 if (internal_relocs == NULL
9820 && o->reloc_count > 0)
9821 return FALSE;
9822
9823 /* We need to reverse-copy input .ctors/.dtors sections if
9824 they are placed in .init_array/.finit_array for output. */
9825 if (o->size > address_size
9826 && ((strncmp (o->name, ".ctors", 6) == 0
9827 && strcmp (o->output_section->name,
9828 ".init_array") == 0)
9829 || (strncmp (o->name, ".dtors", 6) == 0
9830 && strcmp (o->output_section->name,
9831 ".fini_array") == 0))
9832 && (o->name[6] == 0 || o->name[6] == '.'))
9833 {
9834 if (o->size != o->reloc_count * address_size)
9835 {
9836 (*_bfd_error_handler)
9837 (_("error: %B: size of section %A is not "
9838 "multiple of address size"),
9839 input_bfd, o);
9840 bfd_set_error (bfd_error_on_input);
9841 return FALSE;
9842 }
9843 o->flags |= SEC_ELF_REVERSE_COPY;
9844 }
9845
9846 action_discarded = -1;
9847 if (!elf_section_ignore_discarded_relocs (o))
9848 action_discarded = (*bed->action_discarded) (o);
9849
9850 /* Run through the relocs evaluating complex reloc symbols and
9851 looking for relocs against symbols from discarded sections
9852 or section symbols from removed link-once sections.
9853 Complain about relocs against discarded sections. Zero
9854 relocs against removed link-once sections. */
9855
9856 rel = internal_relocs;
9857 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
9858 for ( ; rel < relend; rel++)
9859 {
9860 unsigned long r_symndx = rel->r_info >> r_sym_shift;
9861 unsigned int s_type;
9862 asection **ps, *sec;
9863 struct elf_link_hash_entry *h = NULL;
9864 const char *sym_name;
9865
9866 if (r_symndx == STN_UNDEF)
9867 continue;
9868
9869 if (r_symndx >= locsymcount
9870 || (elf_bad_symtab (input_bfd)
9871 && flinfo->sections[r_symndx] == NULL))
9872 {
9873 h = sym_hashes[r_symndx - extsymoff];
9874
9875 /* Badly formatted input files can contain relocs that
9876 reference non-existant symbols. Check here so that
9877 we do not seg fault. */
9878 if (h == NULL)
9879 {
9880 char buffer [32];
9881
9882 sprintf_vma (buffer, rel->r_info);
9883 (*_bfd_error_handler)
9884 (_("error: %B contains a reloc (0x%s) for section %A "
9885 "that references a non-existent global symbol"),
9886 input_bfd, o, buffer);
9887 bfd_set_error (bfd_error_bad_value);
9888 return FALSE;
9889 }
9890
9891 while (h->root.type == bfd_link_hash_indirect
9892 || h->root.type == bfd_link_hash_warning)
9893 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9894
9895 s_type = h->type;
9896
9897 /* If a plugin symbol is referenced from a non-IR file,
9898 mark the symbol as undefined. Note that the
9899 linker may attach linker created dynamic sections
9900 to the plugin bfd. Symbols defined in linker
9901 created sections are not plugin symbols. */
9902 if (h->root.non_ir_ref
9903 && (h->root.type == bfd_link_hash_defined
9904 || h->root.type == bfd_link_hash_defweak)
9905 && (h->root.u.def.section->flags
9906 & SEC_LINKER_CREATED) == 0
9907 && h->root.u.def.section->owner != NULL
9908 && (h->root.u.def.section->owner->flags
9909 & BFD_PLUGIN) != 0)
9910 {
9911 h->root.type = bfd_link_hash_undefined;
9912 h->root.u.undef.abfd = h->root.u.def.section->owner;
9913 }
9914
9915 ps = NULL;
9916 if (h->root.type == bfd_link_hash_defined
9917 || h->root.type == bfd_link_hash_defweak)
9918 ps = &h->root.u.def.section;
9919
9920 sym_name = h->root.root.string;
9921 }
9922 else
9923 {
9924 Elf_Internal_Sym *sym = isymbuf + r_symndx;
9925
9926 s_type = ELF_ST_TYPE (sym->st_info);
9927 ps = &flinfo->sections[r_symndx];
9928 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
9929 sym, *ps);
9930 }
9931
9932 if ((s_type == STT_RELC || s_type == STT_SRELC)
9933 && !flinfo->info->relocatable)
9934 {
9935 bfd_vma val;
9936 bfd_vma dot = (rel->r_offset
9937 + o->output_offset + o->output_section->vma);
9938 #ifdef DEBUG
9939 printf ("Encountered a complex symbol!");
9940 printf (" (input_bfd %s, section %s, reloc %ld\n",
9941 input_bfd->filename, o->name,
9942 (long) (rel - internal_relocs));
9943 printf (" symbol: idx %8.8lx, name %s\n",
9944 r_symndx, sym_name);
9945 printf (" reloc : info %8.8lx, addr %8.8lx\n",
9946 (unsigned long) rel->r_info,
9947 (unsigned long) rel->r_offset);
9948 #endif
9949 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
9950 isymbuf, locsymcount, s_type == STT_SRELC))
9951 return FALSE;
9952
9953 /* Symbol evaluated OK. Update to absolute value. */
9954 set_symbol_value (input_bfd, isymbuf, locsymcount,
9955 r_symndx, val);
9956 continue;
9957 }
9958
9959 if (action_discarded != -1 && ps != NULL)
9960 {
9961 /* Complain if the definition comes from a
9962 discarded section. */
9963 if ((sec = *ps) != NULL && discarded_section (sec))
9964 {
9965 BFD_ASSERT (r_symndx != STN_UNDEF);
9966 if (action_discarded & COMPLAIN)
9967 (*flinfo->info->callbacks->einfo)
9968 (_("%X`%s' referenced in section `%A' of %B: "
9969 "defined in discarded section `%A' of %B\n"),
9970 sym_name, o, input_bfd, sec, sec->owner);
9971
9972 /* Try to do the best we can to support buggy old
9973 versions of gcc. Pretend that the symbol is
9974 really defined in the kept linkonce section.
9975 FIXME: This is quite broken. Modifying the
9976 symbol here means we will be changing all later
9977 uses of the symbol, not just in this section. */
9978 if (action_discarded & PRETEND)
9979 {
9980 asection *kept;
9981
9982 kept = _bfd_elf_check_kept_section (sec,
9983 flinfo->info);
9984 if (kept != NULL)
9985 {
9986 *ps = kept;
9987 continue;
9988 }
9989 }
9990 }
9991 }
9992 }
9993
9994 /* Relocate the section by invoking a back end routine.
9995
9996 The back end routine is responsible for adjusting the
9997 section contents as necessary, and (if using Rela relocs
9998 and generating a relocatable output file) adjusting the
9999 reloc addend as necessary.
10000
10001 The back end routine does not have to worry about setting
10002 the reloc address or the reloc symbol index.
10003
10004 The back end routine is given a pointer to the swapped in
10005 internal symbols, and can access the hash table entries
10006 for the external symbols via elf_sym_hashes (input_bfd).
10007
10008 When generating relocatable output, the back end routine
10009 must handle STB_LOCAL/STT_SECTION symbols specially. The
10010 output symbol is going to be a section symbol
10011 corresponding to the output section, which will require
10012 the addend to be adjusted. */
10013
10014 ret = (*relocate_section) (output_bfd, flinfo->info,
10015 input_bfd, o, contents,
10016 internal_relocs,
10017 isymbuf,
10018 flinfo->sections);
10019 if (!ret)
10020 return FALSE;
10021
10022 if (ret == 2
10023 || flinfo->info->relocatable
10024 || flinfo->info->emitrelocations)
10025 {
10026 Elf_Internal_Rela *irela;
10027 Elf_Internal_Rela *irelaend, *irelamid;
10028 bfd_vma last_offset;
10029 struct elf_link_hash_entry **rel_hash;
10030 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10031 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10032 unsigned int next_erel;
10033 bfd_boolean rela_normal;
10034 struct bfd_elf_section_data *esdi, *esdo;
10035
10036 esdi = elf_section_data (o);
10037 esdo = elf_section_data (o->output_section);
10038 rela_normal = FALSE;
10039
10040 /* Adjust the reloc addresses and symbol indices. */
10041
10042 irela = internal_relocs;
10043 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
10044 rel_hash = esdo->rel.hashes + esdo->rel.count;
10045 /* We start processing the REL relocs, if any. When we reach
10046 IRELAMID in the loop, we switch to the RELA relocs. */
10047 irelamid = irela;
10048 if (esdi->rel.hdr != NULL)
10049 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10050 * bed->s->int_rels_per_ext_rel);
10051 rel_hash_list = rel_hash;
10052 rela_hash_list = NULL;
10053 last_offset = o->output_offset;
10054 if (!flinfo->info->relocatable)
10055 last_offset += o->output_section->vma;
10056 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10057 {
10058 unsigned long r_symndx;
10059 asection *sec;
10060 Elf_Internal_Sym sym;
10061
10062 if (next_erel == bed->s->int_rels_per_ext_rel)
10063 {
10064 rel_hash++;
10065 next_erel = 0;
10066 }
10067
10068 if (irela == irelamid)
10069 {
10070 rel_hash = esdo->rela.hashes + esdo->rela.count;
10071 rela_hash_list = rel_hash;
10072 rela_normal = bed->rela_normal;
10073 }
10074
10075 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10076 flinfo->info, o,
10077 irela->r_offset);
10078 if (irela->r_offset >= (bfd_vma) -2)
10079 {
10080 /* This is a reloc for a deleted entry or somesuch.
10081 Turn it into an R_*_NONE reloc, at the same
10082 offset as the last reloc. elf_eh_frame.c and
10083 bfd_elf_discard_info rely on reloc offsets
10084 being ordered. */
10085 irela->r_offset = last_offset;
10086 irela->r_info = 0;
10087 irela->r_addend = 0;
10088 continue;
10089 }
10090
10091 irela->r_offset += o->output_offset;
10092
10093 /* Relocs in an executable have to be virtual addresses. */
10094 if (!flinfo->info->relocatable)
10095 irela->r_offset += o->output_section->vma;
10096
10097 last_offset = irela->r_offset;
10098
10099 r_symndx = irela->r_info >> r_sym_shift;
10100 if (r_symndx == STN_UNDEF)
10101 continue;
10102
10103 if (r_symndx >= locsymcount
10104 || (elf_bad_symtab (input_bfd)
10105 && flinfo->sections[r_symndx] == NULL))
10106 {
10107 struct elf_link_hash_entry *rh;
10108 unsigned long indx;
10109
10110 /* This is a reloc against a global symbol. We
10111 have not yet output all the local symbols, so
10112 we do not know the symbol index of any global
10113 symbol. We set the rel_hash entry for this
10114 reloc to point to the global hash table entry
10115 for this symbol. The symbol index is then
10116 set at the end of bfd_elf_final_link. */
10117 indx = r_symndx - extsymoff;
10118 rh = elf_sym_hashes (input_bfd)[indx];
10119 while (rh->root.type == bfd_link_hash_indirect
10120 || rh->root.type == bfd_link_hash_warning)
10121 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10122
10123 /* Setting the index to -2 tells
10124 elf_link_output_extsym that this symbol is
10125 used by a reloc. */
10126 BFD_ASSERT (rh->indx < 0);
10127 rh->indx = -2;
10128
10129 *rel_hash = rh;
10130
10131 continue;
10132 }
10133
10134 /* This is a reloc against a local symbol. */
10135
10136 *rel_hash = NULL;
10137 sym = isymbuf[r_symndx];
10138 sec = flinfo->sections[r_symndx];
10139 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10140 {
10141 /* I suppose the backend ought to fill in the
10142 section of any STT_SECTION symbol against a
10143 processor specific section. */
10144 r_symndx = STN_UNDEF;
10145 if (bfd_is_abs_section (sec))
10146 ;
10147 else if (sec == NULL || sec->owner == NULL)
10148 {
10149 bfd_set_error (bfd_error_bad_value);
10150 return FALSE;
10151 }
10152 else
10153 {
10154 asection *osec = sec->output_section;
10155
10156 /* If we have discarded a section, the output
10157 section will be the absolute section. In
10158 case of discarded SEC_MERGE sections, use
10159 the kept section. relocate_section should
10160 have already handled discarded linkonce
10161 sections. */
10162 if (bfd_is_abs_section (osec)
10163 && sec->kept_section != NULL
10164 && sec->kept_section->output_section != NULL)
10165 {
10166 osec = sec->kept_section->output_section;
10167 irela->r_addend -= osec->vma;
10168 }
10169
10170 if (!bfd_is_abs_section (osec))
10171 {
10172 r_symndx = osec->target_index;
10173 if (r_symndx == STN_UNDEF)
10174 {
10175 irela->r_addend += osec->vma;
10176 osec = _bfd_nearby_section (output_bfd, osec,
10177 osec->vma);
10178 irela->r_addend -= osec->vma;
10179 r_symndx = osec->target_index;
10180 }
10181 }
10182 }
10183
10184 /* Adjust the addend according to where the
10185 section winds up in the output section. */
10186 if (rela_normal)
10187 irela->r_addend += sec->output_offset;
10188 }
10189 else
10190 {
10191 if (flinfo->indices[r_symndx] == -1)
10192 {
10193 unsigned long shlink;
10194 const char *name;
10195 asection *osec;
10196 long indx;
10197
10198 if (flinfo->info->strip == strip_all)
10199 {
10200 /* You can't do ld -r -s. */
10201 bfd_set_error (bfd_error_invalid_operation);
10202 return FALSE;
10203 }
10204
10205 /* This symbol was skipped earlier, but
10206 since it is needed by a reloc, we
10207 must output it now. */
10208 shlink = symtab_hdr->sh_link;
10209 name = (bfd_elf_string_from_elf_section
10210 (input_bfd, shlink, sym.st_name));
10211 if (name == NULL)
10212 return FALSE;
10213
10214 osec = sec->output_section;
10215 sym.st_shndx =
10216 _bfd_elf_section_from_bfd_section (output_bfd,
10217 osec);
10218 if (sym.st_shndx == SHN_BAD)
10219 return FALSE;
10220
10221 sym.st_value += sec->output_offset;
10222 if (!flinfo->info->relocatable)
10223 {
10224 sym.st_value += osec->vma;
10225 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10226 {
10227 /* STT_TLS symbols are relative to PT_TLS
10228 segment base. */
10229 BFD_ASSERT (elf_hash_table (flinfo->info)
10230 ->tls_sec != NULL);
10231 sym.st_value -= (elf_hash_table (flinfo->info)
10232 ->tls_sec->vma);
10233 }
10234 }
10235
10236 indx = bfd_get_symcount (output_bfd);
10237 ret = elf_link_output_symstrtab (flinfo, name,
10238 &sym, sec,
10239 NULL);
10240 if (ret == 0)
10241 return FALSE;
10242 else if (ret == 1)
10243 flinfo->indices[r_symndx] = indx;
10244 else
10245 abort ();
10246 }
10247
10248 r_symndx = flinfo->indices[r_symndx];
10249 }
10250
10251 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10252 | (irela->r_info & r_type_mask));
10253 }
10254
10255 /* Swap out the relocs. */
10256 input_rel_hdr = esdi->rel.hdr;
10257 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
10258 {
10259 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10260 input_rel_hdr,
10261 internal_relocs,
10262 rel_hash_list))
10263 return FALSE;
10264 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10265 * bed->s->int_rels_per_ext_rel);
10266 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
10267 }
10268
10269 input_rela_hdr = esdi->rela.hdr;
10270 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10271 {
10272 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10273 input_rela_hdr,
10274 internal_relocs,
10275 rela_hash_list))
10276 return FALSE;
10277 }
10278 }
10279 }
10280
10281 /* Write out the modified section contents. */
10282 if (bed->elf_backend_write_section
10283 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
10284 contents))
10285 {
10286 /* Section written out. */
10287 }
10288 else switch (o->sec_info_type)
10289 {
10290 case SEC_INFO_TYPE_STABS:
10291 if (! (_bfd_write_section_stabs
10292 (output_bfd,
10293 &elf_hash_table (flinfo->info)->stab_info,
10294 o, &elf_section_data (o)->sec_info, contents)))
10295 return FALSE;
10296 break;
10297 case SEC_INFO_TYPE_MERGE:
10298 if (! _bfd_write_merged_section (output_bfd, o,
10299 elf_section_data (o)->sec_info))
10300 return FALSE;
10301 break;
10302 case SEC_INFO_TYPE_EH_FRAME:
10303 {
10304 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
10305 o, contents))
10306 return FALSE;
10307 }
10308 break;
10309 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10310 {
10311 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10312 flinfo->info,
10313 o, contents))
10314 return FALSE;
10315 }
10316 break;
10317 default:
10318 {
10319 /* FIXME: octets_per_byte. */
10320 if (! (o->flags & SEC_EXCLUDE))
10321 {
10322 file_ptr offset = (file_ptr) o->output_offset;
10323 bfd_size_type todo = o->size;
10324 if ((o->flags & SEC_ELF_REVERSE_COPY))
10325 {
10326 /* Reverse-copy input section to output. */
10327 do
10328 {
10329 todo -= address_size;
10330 if (! bfd_set_section_contents (output_bfd,
10331 o->output_section,
10332 contents + todo,
10333 offset,
10334 address_size))
10335 return FALSE;
10336 if (todo == 0)
10337 break;
10338 offset += address_size;
10339 }
10340 while (1);
10341 }
10342 else if (! bfd_set_section_contents (output_bfd,
10343 o->output_section,
10344 contents,
10345 offset, todo))
10346 return FALSE;
10347 }
10348 }
10349 break;
10350 }
10351 }
10352
10353 return TRUE;
10354 }
10355
10356 /* Generate a reloc when linking an ELF file. This is a reloc
10357 requested by the linker, and does not come from any input file. This
10358 is used to build constructor and destructor tables when linking
10359 with -Ur. */
10360
10361 static bfd_boolean
10362 elf_reloc_link_order (bfd *output_bfd,
10363 struct bfd_link_info *info,
10364 asection *output_section,
10365 struct bfd_link_order *link_order)
10366 {
10367 reloc_howto_type *howto;
10368 long indx;
10369 bfd_vma offset;
10370 bfd_vma addend;
10371 struct bfd_elf_section_reloc_data *reldata;
10372 struct elf_link_hash_entry **rel_hash_ptr;
10373 Elf_Internal_Shdr *rel_hdr;
10374 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10375 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10376 bfd_byte *erel;
10377 unsigned int i;
10378 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
10379
10380 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10381 if (howto == NULL)
10382 {
10383 bfd_set_error (bfd_error_bad_value);
10384 return FALSE;
10385 }
10386
10387 addend = link_order->u.reloc.p->addend;
10388
10389 if (esdo->rel.hdr)
10390 reldata = &esdo->rel;
10391 else if (esdo->rela.hdr)
10392 reldata = &esdo->rela;
10393 else
10394 {
10395 reldata = NULL;
10396 BFD_ASSERT (0);
10397 }
10398
10399 /* Figure out the symbol index. */
10400 rel_hash_ptr = reldata->hashes + reldata->count;
10401 if (link_order->type == bfd_section_reloc_link_order)
10402 {
10403 indx = link_order->u.reloc.p->u.section->target_index;
10404 BFD_ASSERT (indx != 0);
10405 *rel_hash_ptr = NULL;
10406 }
10407 else
10408 {
10409 struct elf_link_hash_entry *h;
10410
10411 /* Treat a reloc against a defined symbol as though it were
10412 actually against the section. */
10413 h = ((struct elf_link_hash_entry *)
10414 bfd_wrapped_link_hash_lookup (output_bfd, info,
10415 link_order->u.reloc.p->u.name,
10416 FALSE, FALSE, TRUE));
10417 if (h != NULL
10418 && (h->root.type == bfd_link_hash_defined
10419 || h->root.type == bfd_link_hash_defweak))
10420 {
10421 asection *section;
10422
10423 section = h->root.u.def.section;
10424 indx = section->output_section->target_index;
10425 *rel_hash_ptr = NULL;
10426 /* It seems that we ought to add the symbol value to the
10427 addend here, but in practice it has already been added
10428 because it was passed to constructor_callback. */
10429 addend += section->output_section->vma + section->output_offset;
10430 }
10431 else if (h != NULL)
10432 {
10433 /* Setting the index to -2 tells elf_link_output_extsym that
10434 this symbol is used by a reloc. */
10435 h->indx = -2;
10436 *rel_hash_ptr = h;
10437 indx = 0;
10438 }
10439 else
10440 {
10441 if (! ((*info->callbacks->unattached_reloc)
10442 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
10443 return FALSE;
10444 indx = 0;
10445 }
10446 }
10447
10448 /* If this is an inplace reloc, we must write the addend into the
10449 object file. */
10450 if (howto->partial_inplace && addend != 0)
10451 {
10452 bfd_size_type size;
10453 bfd_reloc_status_type rstat;
10454 bfd_byte *buf;
10455 bfd_boolean ok;
10456 const char *sym_name;
10457
10458 size = (bfd_size_type) bfd_get_reloc_size (howto);
10459 buf = (bfd_byte *) bfd_zmalloc (size);
10460 if (buf == NULL && size != 0)
10461 return FALSE;
10462 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10463 switch (rstat)
10464 {
10465 case bfd_reloc_ok:
10466 break;
10467
10468 default:
10469 case bfd_reloc_outofrange:
10470 abort ();
10471
10472 case bfd_reloc_overflow:
10473 if (link_order->type == bfd_section_reloc_link_order)
10474 sym_name = bfd_section_name (output_bfd,
10475 link_order->u.reloc.p->u.section);
10476 else
10477 sym_name = link_order->u.reloc.p->u.name;
10478 if (! ((*info->callbacks->reloc_overflow)
10479 (info, NULL, sym_name, howto->name, addend, NULL,
10480 NULL, (bfd_vma) 0)))
10481 {
10482 free (buf);
10483 return FALSE;
10484 }
10485 break;
10486 }
10487 ok = bfd_set_section_contents (output_bfd, output_section, buf,
10488 link_order->offset, size);
10489 free (buf);
10490 if (! ok)
10491 return FALSE;
10492 }
10493
10494 /* The address of a reloc is relative to the section in a
10495 relocatable file, and is a virtual address in an executable
10496 file. */
10497 offset = link_order->offset;
10498 if (! info->relocatable)
10499 offset += output_section->vma;
10500
10501 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10502 {
10503 irel[i].r_offset = offset;
10504 irel[i].r_info = 0;
10505 irel[i].r_addend = 0;
10506 }
10507 if (bed->s->arch_size == 32)
10508 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10509 else
10510 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10511
10512 rel_hdr = reldata->hdr;
10513 erel = rel_hdr->contents;
10514 if (rel_hdr->sh_type == SHT_REL)
10515 {
10516 erel += reldata->count * bed->s->sizeof_rel;
10517 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10518 }
10519 else
10520 {
10521 irel[0].r_addend = addend;
10522 erel += reldata->count * bed->s->sizeof_rela;
10523 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10524 }
10525
10526 ++reldata->count;
10527
10528 return TRUE;
10529 }
10530
10531
10532 /* Get the output vma of the section pointed to by the sh_link field. */
10533
10534 static bfd_vma
10535 elf_get_linked_section_vma (struct bfd_link_order *p)
10536 {
10537 Elf_Internal_Shdr **elf_shdrp;
10538 asection *s;
10539 int elfsec;
10540
10541 s = p->u.indirect.section;
10542 elf_shdrp = elf_elfsections (s->owner);
10543 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10544 elfsec = elf_shdrp[elfsec]->sh_link;
10545 /* PR 290:
10546 The Intel C compiler generates SHT_IA_64_UNWIND with
10547 SHF_LINK_ORDER. But it doesn't set the sh_link or
10548 sh_info fields. Hence we could get the situation
10549 where elfsec is 0. */
10550 if (elfsec == 0)
10551 {
10552 const struct elf_backend_data *bed
10553 = get_elf_backend_data (s->owner);
10554 if (bed->link_order_error_handler)
10555 bed->link_order_error_handler
10556 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
10557 return 0;
10558 }
10559 else
10560 {
10561 s = elf_shdrp[elfsec]->bfd_section;
10562 return s->output_section->vma + s->output_offset;
10563 }
10564 }
10565
10566
10567 /* Compare two sections based on the locations of the sections they are
10568 linked to. Used by elf_fixup_link_order. */
10569
10570 static int
10571 compare_link_order (const void * a, const void * b)
10572 {
10573 bfd_vma apos;
10574 bfd_vma bpos;
10575
10576 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10577 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10578 if (apos < bpos)
10579 return -1;
10580 return apos > bpos;
10581 }
10582
10583
10584 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10585 order as their linked sections. Returns false if this could not be done
10586 because an output section includes both ordered and unordered
10587 sections. Ideally we'd do this in the linker proper. */
10588
10589 static bfd_boolean
10590 elf_fixup_link_order (bfd *abfd, asection *o)
10591 {
10592 int seen_linkorder;
10593 int seen_other;
10594 int n;
10595 struct bfd_link_order *p;
10596 bfd *sub;
10597 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10598 unsigned elfsec;
10599 struct bfd_link_order **sections;
10600 asection *s, *other_sec, *linkorder_sec;
10601 bfd_vma offset;
10602
10603 other_sec = NULL;
10604 linkorder_sec = NULL;
10605 seen_other = 0;
10606 seen_linkorder = 0;
10607 for (p = o->map_head.link_order; p != NULL; p = p->next)
10608 {
10609 if (p->type == bfd_indirect_link_order)
10610 {
10611 s = p->u.indirect.section;
10612 sub = s->owner;
10613 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10614 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
10615 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
10616 && elfsec < elf_numsections (sub)
10617 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
10618 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
10619 {
10620 seen_linkorder++;
10621 linkorder_sec = s;
10622 }
10623 else
10624 {
10625 seen_other++;
10626 other_sec = s;
10627 }
10628 }
10629 else
10630 seen_other++;
10631
10632 if (seen_other && seen_linkorder)
10633 {
10634 if (other_sec && linkorder_sec)
10635 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10636 o, linkorder_sec,
10637 linkorder_sec->owner, other_sec,
10638 other_sec->owner);
10639 else
10640 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
10641 o);
10642 bfd_set_error (bfd_error_bad_value);
10643 return FALSE;
10644 }
10645 }
10646
10647 if (!seen_linkorder)
10648 return TRUE;
10649
10650 sections = (struct bfd_link_order **)
10651 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
10652 if (sections == NULL)
10653 return FALSE;
10654 seen_linkorder = 0;
10655
10656 for (p = o->map_head.link_order; p != NULL; p = p->next)
10657 {
10658 sections[seen_linkorder++] = p;
10659 }
10660 /* Sort the input sections in the order of their linked section. */
10661 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
10662 compare_link_order);
10663
10664 /* Change the offsets of the sections. */
10665 offset = 0;
10666 for (n = 0; n < seen_linkorder; n++)
10667 {
10668 s = sections[n]->u.indirect.section;
10669 offset &= ~(bfd_vma) 0 << s->alignment_power;
10670 s->output_offset = offset;
10671 sections[n]->offset = offset;
10672 /* FIXME: octets_per_byte. */
10673 offset += sections[n]->size;
10674 }
10675
10676 free (sections);
10677 return TRUE;
10678 }
10679
10680 static void
10681 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
10682 {
10683 asection *o;
10684
10685 if (flinfo->symstrtab != NULL)
10686 _bfd_elf_strtab_free (flinfo->symstrtab);
10687 if (flinfo->contents != NULL)
10688 free (flinfo->contents);
10689 if (flinfo->external_relocs != NULL)
10690 free (flinfo->external_relocs);
10691 if (flinfo->internal_relocs != NULL)
10692 free (flinfo->internal_relocs);
10693 if (flinfo->external_syms != NULL)
10694 free (flinfo->external_syms);
10695 if (flinfo->locsym_shndx != NULL)
10696 free (flinfo->locsym_shndx);
10697 if (flinfo->internal_syms != NULL)
10698 free (flinfo->internal_syms);
10699 if (flinfo->indices != NULL)
10700 free (flinfo->indices);
10701 if (flinfo->sections != NULL)
10702 free (flinfo->sections);
10703 if (flinfo->symshndxbuf != NULL)
10704 free (flinfo->symshndxbuf);
10705 for (o = obfd->sections; o != NULL; o = o->next)
10706 {
10707 struct bfd_elf_section_data *esdo = elf_section_data (o);
10708 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
10709 free (esdo->rel.hashes);
10710 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
10711 free (esdo->rela.hashes);
10712 }
10713 }
10714
10715 /* Do the final step of an ELF link. */
10716
10717 bfd_boolean
10718 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
10719 {
10720 bfd_boolean dynamic;
10721 bfd_boolean emit_relocs;
10722 bfd *dynobj;
10723 struct elf_final_link_info flinfo;
10724 asection *o;
10725 struct bfd_link_order *p;
10726 bfd *sub;
10727 bfd_size_type max_contents_size;
10728 bfd_size_type max_external_reloc_size;
10729 bfd_size_type max_internal_reloc_count;
10730 bfd_size_type max_sym_count;
10731 bfd_size_type max_sym_shndx_count;
10732 Elf_Internal_Sym elfsym;
10733 unsigned int i;
10734 Elf_Internal_Shdr *symtab_hdr;
10735 Elf_Internal_Shdr *symtab_shndx_hdr;
10736 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10737 struct elf_outext_info eoinfo;
10738 bfd_boolean merged;
10739 size_t relativecount = 0;
10740 asection *reldyn = 0;
10741 bfd_size_type amt;
10742 asection *attr_section = NULL;
10743 bfd_vma attr_size = 0;
10744 const char *std_attrs_section;
10745
10746 if (! is_elf_hash_table (info->hash))
10747 return FALSE;
10748
10749 if (info->shared)
10750 abfd->flags |= DYNAMIC;
10751
10752 dynamic = elf_hash_table (info)->dynamic_sections_created;
10753 dynobj = elf_hash_table (info)->dynobj;
10754
10755 emit_relocs = (info->relocatable
10756 || info->emitrelocations);
10757
10758 flinfo.info = info;
10759 flinfo.output_bfd = abfd;
10760 flinfo.symstrtab = _bfd_elf_strtab_init ();
10761 if (flinfo.symstrtab == NULL)
10762 return FALSE;
10763
10764 if (! dynamic)
10765 {
10766 flinfo.dynsym_sec = NULL;
10767 flinfo.hash_sec = NULL;
10768 flinfo.symver_sec = NULL;
10769 }
10770 else
10771 {
10772 flinfo.dynsym_sec = bfd_get_linker_section (dynobj, ".dynsym");
10773 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
10774 /* Note that dynsym_sec can be NULL (on VMS). */
10775 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
10776 /* Note that it is OK if symver_sec is NULL. */
10777 }
10778
10779 flinfo.contents = NULL;
10780 flinfo.external_relocs = NULL;
10781 flinfo.internal_relocs = NULL;
10782 flinfo.external_syms = NULL;
10783 flinfo.locsym_shndx = NULL;
10784 flinfo.internal_syms = NULL;
10785 flinfo.indices = NULL;
10786 flinfo.sections = NULL;
10787 flinfo.symshndxbuf = NULL;
10788 flinfo.filesym_count = 0;
10789
10790 /* The object attributes have been merged. Remove the input
10791 sections from the link, and set the contents of the output
10792 secton. */
10793 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
10794 for (o = abfd->sections; o != NULL; o = o->next)
10795 {
10796 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
10797 || strcmp (o->name, ".gnu.attributes") == 0)
10798 {
10799 for (p = o->map_head.link_order; p != NULL; p = p->next)
10800 {
10801 asection *input_section;
10802
10803 if (p->type != bfd_indirect_link_order)
10804 continue;
10805 input_section = p->u.indirect.section;
10806 /* Hack: reset the SEC_HAS_CONTENTS flag so that
10807 elf_link_input_bfd ignores this section. */
10808 input_section->flags &= ~SEC_HAS_CONTENTS;
10809 }
10810
10811 attr_size = bfd_elf_obj_attr_size (abfd);
10812 if (attr_size)
10813 {
10814 bfd_set_section_size (abfd, o, attr_size);
10815 attr_section = o;
10816 /* Skip this section later on. */
10817 o->map_head.link_order = NULL;
10818 }
10819 else
10820 o->flags |= SEC_EXCLUDE;
10821 }
10822 }
10823
10824 /* Count up the number of relocations we will output for each output
10825 section, so that we know the sizes of the reloc sections. We
10826 also figure out some maximum sizes. */
10827 max_contents_size = 0;
10828 max_external_reloc_size = 0;
10829 max_internal_reloc_count = 0;
10830 max_sym_count = 0;
10831 max_sym_shndx_count = 0;
10832 merged = FALSE;
10833 for (o = abfd->sections; o != NULL; o = o->next)
10834 {
10835 struct bfd_elf_section_data *esdo = elf_section_data (o);
10836 o->reloc_count = 0;
10837
10838 for (p = o->map_head.link_order; p != NULL; p = p->next)
10839 {
10840 unsigned int reloc_count = 0;
10841 struct bfd_elf_section_data *esdi = NULL;
10842
10843 if (p->type == bfd_section_reloc_link_order
10844 || p->type == bfd_symbol_reloc_link_order)
10845 reloc_count = 1;
10846 else if (p->type == bfd_indirect_link_order)
10847 {
10848 asection *sec;
10849
10850 sec = p->u.indirect.section;
10851 esdi = elf_section_data (sec);
10852
10853 /* Mark all sections which are to be included in the
10854 link. This will normally be every section. We need
10855 to do this so that we can identify any sections which
10856 the linker has decided to not include. */
10857 sec->linker_mark = TRUE;
10858
10859 if (sec->flags & SEC_MERGE)
10860 merged = TRUE;
10861
10862 if (esdo->this_hdr.sh_type == SHT_REL
10863 || esdo->this_hdr.sh_type == SHT_RELA)
10864 /* Some backends use reloc_count in relocation sections
10865 to count particular types of relocs. Of course,
10866 reloc sections themselves can't have relocations. */
10867 reloc_count = 0;
10868 else if (info->relocatable || info->emitrelocations)
10869 reloc_count = sec->reloc_count;
10870 else if (bed->elf_backend_count_relocs)
10871 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
10872
10873 if (sec->rawsize > max_contents_size)
10874 max_contents_size = sec->rawsize;
10875 if (sec->size > max_contents_size)
10876 max_contents_size = sec->size;
10877
10878 /* We are interested in just local symbols, not all
10879 symbols. */
10880 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
10881 && (sec->owner->flags & DYNAMIC) == 0)
10882 {
10883 size_t sym_count;
10884
10885 if (elf_bad_symtab (sec->owner))
10886 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
10887 / bed->s->sizeof_sym);
10888 else
10889 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
10890
10891 if (sym_count > max_sym_count)
10892 max_sym_count = sym_count;
10893
10894 if (sym_count > max_sym_shndx_count
10895 && elf_symtab_shndx (sec->owner) != 0)
10896 max_sym_shndx_count = sym_count;
10897
10898 if ((sec->flags & SEC_RELOC) != 0)
10899 {
10900 size_t ext_size = 0;
10901
10902 if (esdi->rel.hdr != NULL)
10903 ext_size = esdi->rel.hdr->sh_size;
10904 if (esdi->rela.hdr != NULL)
10905 ext_size += esdi->rela.hdr->sh_size;
10906
10907 if (ext_size > max_external_reloc_size)
10908 max_external_reloc_size = ext_size;
10909 if (sec->reloc_count > max_internal_reloc_count)
10910 max_internal_reloc_count = sec->reloc_count;
10911 }
10912 }
10913 }
10914
10915 if (reloc_count == 0)
10916 continue;
10917
10918 o->reloc_count += reloc_count;
10919
10920 if (p->type == bfd_indirect_link_order
10921 && (info->relocatable || info->emitrelocations))
10922 {
10923 if (esdi->rel.hdr)
10924 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
10925 if (esdi->rela.hdr)
10926 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
10927 }
10928 else
10929 {
10930 if (o->use_rela_p)
10931 esdo->rela.count += reloc_count;
10932 else
10933 esdo->rel.count += reloc_count;
10934 }
10935 }
10936
10937 if (o->reloc_count > 0)
10938 o->flags |= SEC_RELOC;
10939 else
10940 {
10941 /* Explicitly clear the SEC_RELOC flag. The linker tends to
10942 set it (this is probably a bug) and if it is set
10943 assign_section_numbers will create a reloc section. */
10944 o->flags &=~ SEC_RELOC;
10945 }
10946
10947 /* If the SEC_ALLOC flag is not set, force the section VMA to
10948 zero. This is done in elf_fake_sections as well, but forcing
10949 the VMA to 0 here will ensure that relocs against these
10950 sections are handled correctly. */
10951 if ((o->flags & SEC_ALLOC) == 0
10952 && ! o->user_set_vma)
10953 o->vma = 0;
10954 }
10955
10956 if (! info->relocatable && merged)
10957 elf_link_hash_traverse (elf_hash_table (info),
10958 _bfd_elf_link_sec_merge_syms, abfd);
10959
10960 /* Figure out the file positions for everything but the symbol table
10961 and the relocs. We set symcount to force assign_section_numbers
10962 to create a symbol table. */
10963 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
10964 BFD_ASSERT (! abfd->output_has_begun);
10965 if (! _bfd_elf_compute_section_file_positions (abfd, info))
10966 goto error_return;
10967
10968 /* Set sizes, and assign file positions for reloc sections. */
10969 for (o = abfd->sections; o != NULL; o = o->next)
10970 {
10971 struct bfd_elf_section_data *esdo = elf_section_data (o);
10972 if ((o->flags & SEC_RELOC) != 0)
10973 {
10974 if (esdo->rel.hdr
10975 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
10976 goto error_return;
10977
10978 if (esdo->rela.hdr
10979 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
10980 goto error_return;
10981 }
10982
10983 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
10984 to count upwards while actually outputting the relocations. */
10985 esdo->rel.count = 0;
10986 esdo->rela.count = 0;
10987
10988 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
10989 {
10990 /* Cache the section contents so that they can be compressed
10991 later. Use bfd_malloc since it will be freed by
10992 bfd_compress_section_contents. */
10993 unsigned char *contents = esdo->this_hdr.contents;
10994 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
10995 abort ();
10996 contents
10997 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
10998 if (contents == NULL)
10999 goto error_return;
11000 esdo->this_hdr.contents = contents;
11001 }
11002 }
11003
11004 /* We have now assigned file positions for all the sections except
11005 .symtab, .strtab, and non-loaded reloc sections. We start the
11006 .symtab section at the current file position, and write directly
11007 to it. We build the .strtab section in memory. */
11008 bfd_get_symcount (abfd) = 0;
11009 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11010 /* sh_name is set in prep_headers. */
11011 symtab_hdr->sh_type = SHT_SYMTAB;
11012 /* sh_flags, sh_addr and sh_size all start off zero. */
11013 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11014 /* sh_link is set in assign_section_numbers. */
11015 /* sh_info is set below. */
11016 /* sh_offset is set just below. */
11017 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11018
11019 if (max_sym_count < 20)
11020 max_sym_count = 20;
11021 elf_hash_table (info)->strtabsize = max_sym_count;
11022 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11023 elf_hash_table (info)->strtab
11024 = (struct elf_sym_strtab *) bfd_malloc (amt);
11025 if (elf_hash_table (info)->strtab == NULL)
11026 goto error_return;
11027 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11028 flinfo.symshndxbuf
11029 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11030 ? (Elf_External_Sym_Shndx *) -1 : NULL);
11031
11032 if (info->strip != strip_all || emit_relocs)
11033 {
11034 file_ptr off = elf_next_file_pos (abfd);
11035
11036 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11037
11038 /* Note that at this point elf_next_file_pos (abfd) is
11039 incorrect. We do not yet know the size of the .symtab section.
11040 We correct next_file_pos below, after we do know the size. */
11041
11042 /* Start writing out the symbol table. The first symbol is always a
11043 dummy symbol. */
11044 elfsym.st_value = 0;
11045 elfsym.st_size = 0;
11046 elfsym.st_info = 0;
11047 elfsym.st_other = 0;
11048 elfsym.st_shndx = SHN_UNDEF;
11049 elfsym.st_target_internal = 0;
11050 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11051 bfd_und_section_ptr, NULL) != 1)
11052 goto error_return;
11053
11054 /* Output a symbol for each section. We output these even if we are
11055 discarding local symbols, since they are used for relocs. These
11056 symbols have no names. We store the index of each one in the
11057 index field of the section, so that we can find it again when
11058 outputting relocs. */
11059
11060 elfsym.st_size = 0;
11061 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11062 elfsym.st_other = 0;
11063 elfsym.st_value = 0;
11064 elfsym.st_target_internal = 0;
11065 for (i = 1; i < elf_numsections (abfd); i++)
11066 {
11067 o = bfd_section_from_elf_index (abfd, i);
11068 if (o != NULL)
11069 {
11070 o->target_index = bfd_get_symcount (abfd);
11071 elfsym.st_shndx = i;
11072 if (!info->relocatable)
11073 elfsym.st_value = o->vma;
11074 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11075 NULL) != 1)
11076 goto error_return;
11077 }
11078 }
11079 }
11080
11081 /* Allocate some memory to hold information read in from the input
11082 files. */
11083 if (max_contents_size != 0)
11084 {
11085 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11086 if (flinfo.contents == NULL)
11087 goto error_return;
11088 }
11089
11090 if (max_external_reloc_size != 0)
11091 {
11092 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11093 if (flinfo.external_relocs == NULL)
11094 goto error_return;
11095 }
11096
11097 if (max_internal_reloc_count != 0)
11098 {
11099 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11100 amt *= sizeof (Elf_Internal_Rela);
11101 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11102 if (flinfo.internal_relocs == NULL)
11103 goto error_return;
11104 }
11105
11106 if (max_sym_count != 0)
11107 {
11108 amt = max_sym_count * bed->s->sizeof_sym;
11109 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11110 if (flinfo.external_syms == NULL)
11111 goto error_return;
11112
11113 amt = max_sym_count * sizeof (Elf_Internal_Sym);
11114 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11115 if (flinfo.internal_syms == NULL)
11116 goto error_return;
11117
11118 amt = max_sym_count * sizeof (long);
11119 flinfo.indices = (long int *) bfd_malloc (amt);
11120 if (flinfo.indices == NULL)
11121 goto error_return;
11122
11123 amt = max_sym_count * sizeof (asection *);
11124 flinfo.sections = (asection **) bfd_malloc (amt);
11125 if (flinfo.sections == NULL)
11126 goto error_return;
11127 }
11128
11129 if (max_sym_shndx_count != 0)
11130 {
11131 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
11132 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11133 if (flinfo.locsym_shndx == NULL)
11134 goto error_return;
11135 }
11136
11137 if (elf_hash_table (info)->tls_sec)
11138 {
11139 bfd_vma base, end = 0;
11140 asection *sec;
11141
11142 for (sec = elf_hash_table (info)->tls_sec;
11143 sec && (sec->flags & SEC_THREAD_LOCAL);
11144 sec = sec->next)
11145 {
11146 bfd_size_type size = sec->size;
11147
11148 if (size == 0
11149 && (sec->flags & SEC_HAS_CONTENTS) == 0)
11150 {
11151 struct bfd_link_order *ord = sec->map_tail.link_order;
11152
11153 if (ord != NULL)
11154 size = ord->offset + ord->size;
11155 }
11156 end = sec->vma + size;
11157 }
11158 base = elf_hash_table (info)->tls_sec->vma;
11159 /* Only align end of TLS section if static TLS doesn't have special
11160 alignment requirements. */
11161 if (bed->static_tls_alignment == 1)
11162 end = align_power (end,
11163 elf_hash_table (info)->tls_sec->alignment_power);
11164 elf_hash_table (info)->tls_size = end - base;
11165 }
11166
11167 /* Reorder SHF_LINK_ORDER sections. */
11168 for (o = abfd->sections; o != NULL; o = o->next)
11169 {
11170 if (!elf_fixup_link_order (abfd, o))
11171 return FALSE;
11172 }
11173
11174 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11175 return FALSE;
11176
11177 /* Since ELF permits relocations to be against local symbols, we
11178 must have the local symbols available when we do the relocations.
11179 Since we would rather only read the local symbols once, and we
11180 would rather not keep them in memory, we handle all the
11181 relocations for a single input file at the same time.
11182
11183 Unfortunately, there is no way to know the total number of local
11184 symbols until we have seen all of them, and the local symbol
11185 indices precede the global symbol indices. This means that when
11186 we are generating relocatable output, and we see a reloc against
11187 a global symbol, we can not know the symbol index until we have
11188 finished examining all the local symbols to see which ones we are
11189 going to output. To deal with this, we keep the relocations in
11190 memory, and don't output them until the end of the link. This is
11191 an unfortunate waste of memory, but I don't see a good way around
11192 it. Fortunately, it only happens when performing a relocatable
11193 link, which is not the common case. FIXME: If keep_memory is set
11194 we could write the relocs out and then read them again; I don't
11195 know how bad the memory loss will be. */
11196
11197 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11198 sub->output_has_begun = FALSE;
11199 for (o = abfd->sections; o != NULL; o = o->next)
11200 {
11201 for (p = o->map_head.link_order; p != NULL; p = p->next)
11202 {
11203 if (p->type == bfd_indirect_link_order
11204 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11205 == bfd_target_elf_flavour)
11206 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11207 {
11208 if (! sub->output_has_begun)
11209 {
11210 if (! elf_link_input_bfd (&flinfo, sub))
11211 goto error_return;
11212 sub->output_has_begun = TRUE;
11213 }
11214 }
11215 else if (p->type == bfd_section_reloc_link_order
11216 || p->type == bfd_symbol_reloc_link_order)
11217 {
11218 if (! elf_reloc_link_order (abfd, info, o, p))
11219 goto error_return;
11220 }
11221 else
11222 {
11223 if (! _bfd_default_link_order (abfd, info, o, p))
11224 {
11225 if (p->type == bfd_indirect_link_order
11226 && (bfd_get_flavour (sub)
11227 == bfd_target_elf_flavour)
11228 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11229 != bed->s->elfclass))
11230 {
11231 const char *iclass, *oclass;
11232
11233 if (bed->s->elfclass == ELFCLASS64)
11234 {
11235 iclass = "ELFCLASS32";
11236 oclass = "ELFCLASS64";
11237 }
11238 else
11239 {
11240 iclass = "ELFCLASS64";
11241 oclass = "ELFCLASS32";
11242 }
11243
11244 bfd_set_error (bfd_error_wrong_format);
11245 (*_bfd_error_handler)
11246 (_("%B: file class %s incompatible with %s"),
11247 sub, iclass, oclass);
11248 }
11249
11250 goto error_return;
11251 }
11252 }
11253 }
11254 }
11255
11256 /* Free symbol buffer if needed. */
11257 if (!info->reduce_memory_overheads)
11258 {
11259 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11260 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11261 && elf_tdata (sub)->symbuf)
11262 {
11263 free (elf_tdata (sub)->symbuf);
11264 elf_tdata (sub)->symbuf = NULL;
11265 }
11266 }
11267
11268 /* Output any global symbols that got converted to local in a
11269 version script or due to symbol visibility. We do this in a
11270 separate step since ELF requires all local symbols to appear
11271 prior to any global symbols. FIXME: We should only do this if
11272 some global symbols were, in fact, converted to become local.
11273 FIXME: Will this work correctly with the Irix 5 linker? */
11274 eoinfo.failed = FALSE;
11275 eoinfo.flinfo = &flinfo;
11276 eoinfo.localsyms = TRUE;
11277 eoinfo.file_sym_done = FALSE;
11278 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11279 if (eoinfo.failed)
11280 return FALSE;
11281
11282 /* If backend needs to output some local symbols not present in the hash
11283 table, do it now. */
11284 if (bed->elf_backend_output_arch_local_syms
11285 && (info->strip != strip_all || emit_relocs))
11286 {
11287 typedef int (*out_sym_func)
11288 (void *, const char *, Elf_Internal_Sym *, asection *,
11289 struct elf_link_hash_entry *);
11290
11291 if (! ((*bed->elf_backend_output_arch_local_syms)
11292 (abfd, info, &flinfo,
11293 (out_sym_func) elf_link_output_symstrtab)))
11294 return FALSE;
11295 }
11296
11297 /* That wrote out all the local symbols. Finish up the symbol table
11298 with the global symbols. Even if we want to strip everything we
11299 can, we still need to deal with those global symbols that got
11300 converted to local in a version script. */
11301
11302 /* The sh_info field records the index of the first non local symbol. */
11303 symtab_hdr->sh_info = bfd_get_symcount (abfd);
11304
11305 if (dynamic
11306 && flinfo.dynsym_sec != NULL
11307 && flinfo.dynsym_sec->output_section != bfd_abs_section_ptr)
11308 {
11309 Elf_Internal_Sym sym;
11310 bfd_byte *dynsym = flinfo.dynsym_sec->contents;
11311 long last_local = 0;
11312
11313 /* Write out the section symbols for the output sections. */
11314 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
11315 {
11316 asection *s;
11317
11318 sym.st_size = 0;
11319 sym.st_name = 0;
11320 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11321 sym.st_other = 0;
11322 sym.st_target_internal = 0;
11323
11324 for (s = abfd->sections; s != NULL; s = s->next)
11325 {
11326 int indx;
11327 bfd_byte *dest;
11328 long dynindx;
11329
11330 dynindx = elf_section_data (s)->dynindx;
11331 if (dynindx <= 0)
11332 continue;
11333 indx = elf_section_data (s)->this_idx;
11334 BFD_ASSERT (indx > 0);
11335 sym.st_shndx = indx;
11336 if (! check_dynsym (abfd, &sym))
11337 return FALSE;
11338 sym.st_value = s->vma;
11339 dest = dynsym + dynindx * bed->s->sizeof_sym;
11340 if (last_local < dynindx)
11341 last_local = dynindx;
11342 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11343 }
11344 }
11345
11346 /* Write out the local dynsyms. */
11347 if (elf_hash_table (info)->dynlocal)
11348 {
11349 struct elf_link_local_dynamic_entry *e;
11350 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
11351 {
11352 asection *s;
11353 bfd_byte *dest;
11354
11355 /* Copy the internal symbol and turn off visibility.
11356 Note that we saved a word of storage and overwrote
11357 the original st_name with the dynstr_index. */
11358 sym = e->isym;
11359 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
11360
11361 s = bfd_section_from_elf_index (e->input_bfd,
11362 e->isym.st_shndx);
11363 if (s != NULL)
11364 {
11365 sym.st_shndx =
11366 elf_section_data (s->output_section)->this_idx;
11367 if (! check_dynsym (abfd, &sym))
11368 return FALSE;
11369 sym.st_value = (s->output_section->vma
11370 + s->output_offset
11371 + e->isym.st_value);
11372 }
11373
11374 if (last_local < e->dynindx)
11375 last_local = e->dynindx;
11376
11377 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
11378 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11379 }
11380 }
11381
11382 elf_section_data (flinfo.dynsym_sec->output_section)->this_hdr.sh_info =
11383 last_local + 1;
11384 }
11385
11386 /* We get the global symbols from the hash table. */
11387 eoinfo.failed = FALSE;
11388 eoinfo.localsyms = FALSE;
11389 eoinfo.flinfo = &flinfo;
11390 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11391 if (eoinfo.failed)
11392 return FALSE;
11393
11394 /* If backend needs to output some symbols not present in the hash
11395 table, do it now. */
11396 if (bed->elf_backend_output_arch_syms
11397 && (info->strip != strip_all || emit_relocs))
11398 {
11399 typedef int (*out_sym_func)
11400 (void *, const char *, Elf_Internal_Sym *, asection *,
11401 struct elf_link_hash_entry *);
11402
11403 if (! ((*bed->elf_backend_output_arch_syms)
11404 (abfd, info, &flinfo,
11405 (out_sym_func) elf_link_output_symstrtab)))
11406 return FALSE;
11407 }
11408
11409 /* Finalize the .strtab section. */
11410 _bfd_elf_strtab_finalize (flinfo.symstrtab);
11411
11412 /* Swap out the .strtab section. */
11413 if (!elf_link_swap_symbols_out (&flinfo))
11414 return FALSE;
11415
11416 /* Now we know the size of the symtab section. */
11417 if (bfd_get_symcount (abfd) > 0)
11418 {
11419 /* Finish up and write out the symbol string table (.strtab)
11420 section. */
11421 Elf_Internal_Shdr *symstrtab_hdr;
11422 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
11423
11424 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
11425 if (symtab_shndx_hdr->sh_name != 0)
11426 {
11427 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
11428 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
11429 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
11430 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
11431 symtab_shndx_hdr->sh_size = amt;
11432
11433 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
11434 off, TRUE);
11435
11436 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
11437 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
11438 return FALSE;
11439 }
11440
11441 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
11442 /* sh_name was set in prep_headers. */
11443 symstrtab_hdr->sh_type = SHT_STRTAB;
11444 symstrtab_hdr->sh_flags = 0;
11445 symstrtab_hdr->sh_addr = 0;
11446 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
11447 symstrtab_hdr->sh_entsize = 0;
11448 symstrtab_hdr->sh_link = 0;
11449 symstrtab_hdr->sh_info = 0;
11450 /* sh_offset is set just below. */
11451 symstrtab_hdr->sh_addralign = 1;
11452
11453 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
11454 off, TRUE);
11455 elf_next_file_pos (abfd) = off;
11456
11457 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
11458 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
11459 return FALSE;
11460 }
11461
11462 /* Adjust the relocs to have the correct symbol indices. */
11463 for (o = abfd->sections; o != NULL; o = o->next)
11464 {
11465 struct bfd_elf_section_data *esdo = elf_section_data (o);
11466 bfd_boolean sort;
11467 if ((o->flags & SEC_RELOC) == 0)
11468 continue;
11469
11470 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
11471 if (esdo->rel.hdr != NULL)
11472 elf_link_adjust_relocs (abfd, &esdo->rel, sort);
11473 if (esdo->rela.hdr != NULL)
11474 elf_link_adjust_relocs (abfd, &esdo->rela, sort);
11475
11476 /* Set the reloc_count field to 0 to prevent write_relocs from
11477 trying to swap the relocs out itself. */
11478 o->reloc_count = 0;
11479 }
11480
11481 if (dynamic && info->combreloc && dynobj != NULL)
11482 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
11483
11484 /* If we are linking against a dynamic object, or generating a
11485 shared library, finish up the dynamic linking information. */
11486 if (dynamic)
11487 {
11488 bfd_byte *dyncon, *dynconend;
11489
11490 /* Fix up .dynamic entries. */
11491 o = bfd_get_linker_section (dynobj, ".dynamic");
11492 BFD_ASSERT (o != NULL);
11493
11494 dyncon = o->contents;
11495 dynconend = o->contents + o->size;
11496 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11497 {
11498 Elf_Internal_Dyn dyn;
11499 const char *name;
11500 unsigned int type;
11501
11502 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11503
11504 switch (dyn.d_tag)
11505 {
11506 default:
11507 continue;
11508 case DT_NULL:
11509 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
11510 {
11511 switch (elf_section_data (reldyn)->this_hdr.sh_type)
11512 {
11513 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
11514 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
11515 default: continue;
11516 }
11517 dyn.d_un.d_val = relativecount;
11518 relativecount = 0;
11519 break;
11520 }
11521 continue;
11522
11523 case DT_INIT:
11524 name = info->init_function;
11525 goto get_sym;
11526 case DT_FINI:
11527 name = info->fini_function;
11528 get_sym:
11529 {
11530 struct elf_link_hash_entry *h;
11531
11532 h = elf_link_hash_lookup (elf_hash_table (info), name,
11533 FALSE, FALSE, TRUE);
11534 if (h != NULL
11535 && (h->root.type == bfd_link_hash_defined
11536 || h->root.type == bfd_link_hash_defweak))
11537 {
11538 dyn.d_un.d_ptr = h->root.u.def.value;
11539 o = h->root.u.def.section;
11540 if (o->output_section != NULL)
11541 dyn.d_un.d_ptr += (o->output_section->vma
11542 + o->output_offset);
11543 else
11544 {
11545 /* The symbol is imported from another shared
11546 library and does not apply to this one. */
11547 dyn.d_un.d_ptr = 0;
11548 }
11549 break;
11550 }
11551 }
11552 continue;
11553
11554 case DT_PREINIT_ARRAYSZ:
11555 name = ".preinit_array";
11556 goto get_size;
11557 case DT_INIT_ARRAYSZ:
11558 name = ".init_array";
11559 goto get_size;
11560 case DT_FINI_ARRAYSZ:
11561 name = ".fini_array";
11562 get_size:
11563 o = bfd_get_section_by_name (abfd, name);
11564 if (o == NULL)
11565 {
11566 (*_bfd_error_handler)
11567 (_("%B: could not find output section %s"), abfd, name);
11568 goto error_return;
11569 }
11570 if (o->size == 0)
11571 (*_bfd_error_handler)
11572 (_("warning: %s section has zero size"), name);
11573 dyn.d_un.d_val = o->size;
11574 break;
11575
11576 case DT_PREINIT_ARRAY:
11577 name = ".preinit_array";
11578 goto get_vma;
11579 case DT_INIT_ARRAY:
11580 name = ".init_array";
11581 goto get_vma;
11582 case DT_FINI_ARRAY:
11583 name = ".fini_array";
11584 goto get_vma;
11585
11586 case DT_HASH:
11587 name = ".hash";
11588 goto get_vma;
11589 case DT_GNU_HASH:
11590 name = ".gnu.hash";
11591 goto get_vma;
11592 case DT_STRTAB:
11593 name = ".dynstr";
11594 goto get_vma;
11595 case DT_SYMTAB:
11596 name = ".dynsym";
11597 goto get_vma;
11598 case DT_VERDEF:
11599 name = ".gnu.version_d";
11600 goto get_vma;
11601 case DT_VERNEED:
11602 name = ".gnu.version_r";
11603 goto get_vma;
11604 case DT_VERSYM:
11605 name = ".gnu.version";
11606 get_vma:
11607 o = bfd_get_section_by_name (abfd, name);
11608 if (o == NULL)
11609 {
11610 (*_bfd_error_handler)
11611 (_("%B: could not find output section %s"), abfd, name);
11612 goto error_return;
11613 }
11614 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
11615 {
11616 (*_bfd_error_handler)
11617 (_("warning: section '%s' is being made into a note"), name);
11618 bfd_set_error (bfd_error_nonrepresentable_section);
11619 goto error_return;
11620 }
11621 dyn.d_un.d_ptr = o->vma;
11622 break;
11623
11624 case DT_REL:
11625 case DT_RELA:
11626 case DT_RELSZ:
11627 case DT_RELASZ:
11628 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
11629 type = SHT_REL;
11630 else
11631 type = SHT_RELA;
11632 dyn.d_un.d_val = 0;
11633 dyn.d_un.d_ptr = 0;
11634 for (i = 1; i < elf_numsections (abfd); i++)
11635 {
11636 Elf_Internal_Shdr *hdr;
11637
11638 hdr = elf_elfsections (abfd)[i];
11639 if (hdr->sh_type == type
11640 && (hdr->sh_flags & SHF_ALLOC) != 0)
11641 {
11642 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
11643 dyn.d_un.d_val += hdr->sh_size;
11644 else
11645 {
11646 if (dyn.d_un.d_ptr == 0
11647 || hdr->sh_addr < dyn.d_un.d_ptr)
11648 dyn.d_un.d_ptr = hdr->sh_addr;
11649 }
11650 }
11651 }
11652 break;
11653 }
11654 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
11655 }
11656 }
11657
11658 /* If we have created any dynamic sections, then output them. */
11659 if (dynobj != NULL)
11660 {
11661 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
11662 goto error_return;
11663
11664 /* Check for DT_TEXTREL (late, in case the backend removes it). */
11665 if (((info->warn_shared_textrel && info->shared)
11666 || info->error_textrel)
11667 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
11668 {
11669 bfd_byte *dyncon, *dynconend;
11670
11671 dyncon = o->contents;
11672 dynconend = o->contents + o->size;
11673 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11674 {
11675 Elf_Internal_Dyn dyn;
11676
11677 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11678
11679 if (dyn.d_tag == DT_TEXTREL)
11680 {
11681 if (info->error_textrel)
11682 info->callbacks->einfo
11683 (_("%P%X: read-only segment has dynamic relocations.\n"));
11684 else
11685 info->callbacks->einfo
11686 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
11687 break;
11688 }
11689 }
11690 }
11691
11692 for (o = dynobj->sections; o != NULL; o = o->next)
11693 {
11694 if ((o->flags & SEC_HAS_CONTENTS) == 0
11695 || o->size == 0
11696 || o->output_section == bfd_abs_section_ptr)
11697 continue;
11698 if ((o->flags & SEC_LINKER_CREATED) == 0)
11699 {
11700 /* At this point, we are only interested in sections
11701 created by _bfd_elf_link_create_dynamic_sections. */
11702 continue;
11703 }
11704 if (elf_hash_table (info)->stab_info.stabstr == o)
11705 continue;
11706 if (elf_hash_table (info)->eh_info.hdr_sec == o)
11707 continue;
11708 if (strcmp (o->name, ".dynstr") != 0)
11709 {
11710 /* FIXME: octets_per_byte. */
11711 if (! bfd_set_section_contents (abfd, o->output_section,
11712 o->contents,
11713 (file_ptr) o->output_offset,
11714 o->size))
11715 goto error_return;
11716 }
11717 else
11718 {
11719 /* The contents of the .dynstr section are actually in a
11720 stringtab. */
11721 file_ptr off;
11722
11723 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
11724 if (bfd_seek (abfd, off, SEEK_SET) != 0
11725 || ! _bfd_elf_strtab_emit (abfd,
11726 elf_hash_table (info)->dynstr))
11727 goto error_return;
11728 }
11729 }
11730 }
11731
11732 if (info->relocatable)
11733 {
11734 bfd_boolean failed = FALSE;
11735
11736 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
11737 if (failed)
11738 goto error_return;
11739 }
11740
11741 /* If we have optimized stabs strings, output them. */
11742 if (elf_hash_table (info)->stab_info.stabstr != NULL)
11743 {
11744 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
11745 goto error_return;
11746 }
11747
11748 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
11749 goto error_return;
11750
11751 elf_final_link_free (abfd, &flinfo);
11752
11753 elf_linker (abfd) = TRUE;
11754
11755 if (attr_section)
11756 {
11757 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
11758 if (contents == NULL)
11759 return FALSE; /* Bail out and fail. */
11760 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
11761 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
11762 free (contents);
11763 }
11764
11765 return TRUE;
11766
11767 error_return:
11768 elf_final_link_free (abfd, &flinfo);
11769 return FALSE;
11770 }
11771 \f
11772 /* Initialize COOKIE for input bfd ABFD. */
11773
11774 static bfd_boolean
11775 init_reloc_cookie (struct elf_reloc_cookie *cookie,
11776 struct bfd_link_info *info, bfd *abfd)
11777 {
11778 Elf_Internal_Shdr *symtab_hdr;
11779 const struct elf_backend_data *bed;
11780
11781 bed = get_elf_backend_data (abfd);
11782 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11783
11784 cookie->abfd = abfd;
11785 cookie->sym_hashes = elf_sym_hashes (abfd);
11786 cookie->bad_symtab = elf_bad_symtab (abfd);
11787 if (cookie->bad_symtab)
11788 {
11789 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11790 cookie->extsymoff = 0;
11791 }
11792 else
11793 {
11794 cookie->locsymcount = symtab_hdr->sh_info;
11795 cookie->extsymoff = symtab_hdr->sh_info;
11796 }
11797
11798 if (bed->s->arch_size == 32)
11799 cookie->r_sym_shift = 8;
11800 else
11801 cookie->r_sym_shift = 32;
11802
11803 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
11804 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
11805 {
11806 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
11807 cookie->locsymcount, 0,
11808 NULL, NULL, NULL);
11809 if (cookie->locsyms == NULL)
11810 {
11811 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
11812 return FALSE;
11813 }
11814 if (info->keep_memory)
11815 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
11816 }
11817 return TRUE;
11818 }
11819
11820 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
11821
11822 static void
11823 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
11824 {
11825 Elf_Internal_Shdr *symtab_hdr;
11826
11827 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11828 if (cookie->locsyms != NULL
11829 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
11830 free (cookie->locsyms);
11831 }
11832
11833 /* Initialize the relocation information in COOKIE for input section SEC
11834 of input bfd ABFD. */
11835
11836 static bfd_boolean
11837 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11838 struct bfd_link_info *info, bfd *abfd,
11839 asection *sec)
11840 {
11841 const struct elf_backend_data *bed;
11842
11843 if (sec->reloc_count == 0)
11844 {
11845 cookie->rels = NULL;
11846 cookie->relend = NULL;
11847 }
11848 else
11849 {
11850 bed = get_elf_backend_data (abfd);
11851
11852 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
11853 info->keep_memory);
11854 if (cookie->rels == NULL)
11855 return FALSE;
11856 cookie->rel = cookie->rels;
11857 cookie->relend = (cookie->rels
11858 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
11859 }
11860 cookie->rel = cookie->rels;
11861 return TRUE;
11862 }
11863
11864 /* Free the memory allocated by init_reloc_cookie_rels,
11865 if appropriate. */
11866
11867 static void
11868 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11869 asection *sec)
11870 {
11871 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
11872 free (cookie->rels);
11873 }
11874
11875 /* Initialize the whole of COOKIE for input section SEC. */
11876
11877 static bfd_boolean
11878 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11879 struct bfd_link_info *info,
11880 asection *sec)
11881 {
11882 if (!init_reloc_cookie (cookie, info, sec->owner))
11883 goto error1;
11884 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
11885 goto error2;
11886 return TRUE;
11887
11888 error2:
11889 fini_reloc_cookie (cookie, sec->owner);
11890 error1:
11891 return FALSE;
11892 }
11893
11894 /* Free the memory allocated by init_reloc_cookie_for_section,
11895 if appropriate. */
11896
11897 static void
11898 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11899 asection *sec)
11900 {
11901 fini_reloc_cookie_rels (cookie, sec);
11902 fini_reloc_cookie (cookie, sec->owner);
11903 }
11904 \f
11905 /* Garbage collect unused sections. */
11906
11907 /* Default gc_mark_hook. */
11908
11909 asection *
11910 _bfd_elf_gc_mark_hook (asection *sec,
11911 struct bfd_link_info *info ATTRIBUTE_UNUSED,
11912 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
11913 struct elf_link_hash_entry *h,
11914 Elf_Internal_Sym *sym)
11915 {
11916 const char *sec_name;
11917
11918 if (h != NULL)
11919 {
11920 switch (h->root.type)
11921 {
11922 case bfd_link_hash_defined:
11923 case bfd_link_hash_defweak:
11924 return h->root.u.def.section;
11925
11926 case bfd_link_hash_common:
11927 return h->root.u.c.p->section;
11928
11929 case bfd_link_hash_undefined:
11930 case bfd_link_hash_undefweak:
11931 /* To work around a glibc bug, keep all XXX input sections
11932 when there is an as yet undefined reference to __start_XXX
11933 or __stop_XXX symbols. The linker will later define such
11934 symbols for orphan input sections that have a name
11935 representable as a C identifier. */
11936 if (strncmp (h->root.root.string, "__start_", 8) == 0)
11937 sec_name = h->root.root.string + 8;
11938 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
11939 sec_name = h->root.root.string + 7;
11940 else
11941 sec_name = NULL;
11942
11943 if (sec_name && *sec_name != '\0')
11944 {
11945 bfd *i;
11946
11947 for (i = info->input_bfds; i; i = i->link.next)
11948 {
11949 sec = bfd_get_section_by_name (i, sec_name);
11950 if (sec)
11951 sec->flags |= SEC_KEEP;
11952 }
11953 }
11954 break;
11955
11956 default:
11957 break;
11958 }
11959 }
11960 else
11961 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
11962
11963 return NULL;
11964 }
11965
11966 /* COOKIE->rel describes a relocation against section SEC, which is
11967 a section we've decided to keep. Return the section that contains
11968 the relocation symbol, or NULL if no section contains it. */
11969
11970 asection *
11971 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
11972 elf_gc_mark_hook_fn gc_mark_hook,
11973 struct elf_reloc_cookie *cookie)
11974 {
11975 unsigned long r_symndx;
11976 struct elf_link_hash_entry *h;
11977
11978 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
11979 if (r_symndx == STN_UNDEF)
11980 return NULL;
11981
11982 if (r_symndx >= cookie->locsymcount
11983 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
11984 {
11985 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
11986 if (h == NULL)
11987 {
11988 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
11989 sec->owner);
11990 return NULL;
11991 }
11992 while (h->root.type == bfd_link_hash_indirect
11993 || h->root.type == bfd_link_hash_warning)
11994 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11995 h->mark = 1;
11996 /* If this symbol is weak and there is a non-weak definition, we
11997 keep the non-weak definition because many backends put
11998 dynamic reloc info on the non-weak definition for code
11999 handling copy relocs. */
12000 if (h->u.weakdef != NULL)
12001 h->u.weakdef->mark = 1;
12002 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12003 }
12004
12005 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12006 &cookie->locsyms[r_symndx]);
12007 }
12008
12009 /* COOKIE->rel describes a relocation against section SEC, which is
12010 a section we've decided to keep. Mark the section that contains
12011 the relocation symbol. */
12012
12013 bfd_boolean
12014 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12015 asection *sec,
12016 elf_gc_mark_hook_fn gc_mark_hook,
12017 struct elf_reloc_cookie *cookie)
12018 {
12019 asection *rsec;
12020
12021 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie);
12022 if (rsec && !rsec->gc_mark)
12023 {
12024 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12025 || (rsec->owner->flags & DYNAMIC) != 0)
12026 rsec->gc_mark = 1;
12027 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12028 return FALSE;
12029 }
12030 return TRUE;
12031 }
12032
12033 /* The mark phase of garbage collection. For a given section, mark
12034 it and any sections in this section's group, and all the sections
12035 which define symbols to which it refers. */
12036
12037 bfd_boolean
12038 _bfd_elf_gc_mark (struct bfd_link_info *info,
12039 asection *sec,
12040 elf_gc_mark_hook_fn gc_mark_hook)
12041 {
12042 bfd_boolean ret;
12043 asection *group_sec, *eh_frame;
12044
12045 sec->gc_mark = 1;
12046
12047 /* Mark all the sections in the group. */
12048 group_sec = elf_section_data (sec)->next_in_group;
12049 if (group_sec && !group_sec->gc_mark)
12050 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
12051 return FALSE;
12052
12053 /* Look through the section relocs. */
12054 ret = TRUE;
12055 eh_frame = elf_eh_frame_section (sec->owner);
12056 if ((sec->flags & SEC_RELOC) != 0
12057 && sec->reloc_count > 0
12058 && sec != eh_frame)
12059 {
12060 struct elf_reloc_cookie cookie;
12061
12062 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12063 ret = FALSE;
12064 else
12065 {
12066 for (; cookie.rel < cookie.relend; cookie.rel++)
12067 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
12068 {
12069 ret = FALSE;
12070 break;
12071 }
12072 fini_reloc_cookie_for_section (&cookie, sec);
12073 }
12074 }
12075
12076 if (ret && eh_frame && elf_fde_list (sec))
12077 {
12078 struct elf_reloc_cookie cookie;
12079
12080 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12081 ret = FALSE;
12082 else
12083 {
12084 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12085 gc_mark_hook, &cookie))
12086 ret = FALSE;
12087 fini_reloc_cookie_for_section (&cookie, eh_frame);
12088 }
12089 }
12090
12091 eh_frame = elf_section_eh_frame_entry (sec);
12092 if (ret && eh_frame && !eh_frame->gc_mark)
12093 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12094 ret = FALSE;
12095
12096 return ret;
12097 }
12098
12099 /* Scan and mark sections in a special or debug section group. */
12100
12101 static void
12102 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12103 {
12104 /* Point to first section of section group. */
12105 asection *ssec;
12106 /* Used to iterate the section group. */
12107 asection *msec;
12108
12109 bfd_boolean is_special_grp = TRUE;
12110 bfd_boolean is_debug_grp = TRUE;
12111
12112 /* First scan to see if group contains any section other than debug
12113 and special section. */
12114 ssec = msec = elf_next_in_group (grp);
12115 do
12116 {
12117 if ((msec->flags & SEC_DEBUGGING) == 0)
12118 is_debug_grp = FALSE;
12119
12120 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12121 is_special_grp = FALSE;
12122
12123 msec = elf_next_in_group (msec);
12124 }
12125 while (msec != ssec);
12126
12127 /* If this is a pure debug section group or pure special section group,
12128 keep all sections in this group. */
12129 if (is_debug_grp || is_special_grp)
12130 {
12131 do
12132 {
12133 msec->gc_mark = 1;
12134 msec = elf_next_in_group (msec);
12135 }
12136 while (msec != ssec);
12137 }
12138 }
12139
12140 /* Keep debug and special sections. */
12141
12142 bfd_boolean
12143 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12144 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12145 {
12146 bfd *ibfd;
12147
12148 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12149 {
12150 asection *isec;
12151 bfd_boolean some_kept;
12152 bfd_boolean debug_frag_seen;
12153
12154 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12155 continue;
12156
12157 /* Ensure all linker created sections are kept,
12158 see if any other section is already marked,
12159 and note if we have any fragmented debug sections. */
12160 debug_frag_seen = some_kept = FALSE;
12161 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12162 {
12163 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12164 isec->gc_mark = 1;
12165 else if (isec->gc_mark)
12166 some_kept = TRUE;
12167
12168 if (debug_frag_seen == FALSE
12169 && (isec->flags & SEC_DEBUGGING)
12170 && CONST_STRNEQ (isec->name, ".debug_line."))
12171 debug_frag_seen = TRUE;
12172 }
12173
12174 /* If no section in this file will be kept, then we can
12175 toss out the debug and special sections. */
12176 if (!some_kept)
12177 continue;
12178
12179 /* Keep debug and special sections like .comment when they are
12180 not part of a group. Also keep section groups that contain
12181 just debug sections or special sections. */
12182 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12183 {
12184 if ((isec->flags & SEC_GROUP) != 0)
12185 _bfd_elf_gc_mark_debug_special_section_group (isec);
12186 else if (((isec->flags & SEC_DEBUGGING) != 0
12187 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12188 && elf_next_in_group (isec) == NULL)
12189 isec->gc_mark = 1;
12190 }
12191
12192 if (! debug_frag_seen)
12193 continue;
12194
12195 /* Look for CODE sections which are going to be discarded,
12196 and find and discard any fragmented debug sections which
12197 are associated with that code section. */
12198 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12199 if ((isec->flags & SEC_CODE) != 0
12200 && isec->gc_mark == 0)
12201 {
12202 unsigned int ilen;
12203 asection *dsec;
12204
12205 ilen = strlen (isec->name);
12206
12207 /* Association is determined by the name of the debug section
12208 containing the name of the code section as a suffix. For
12209 example .debug_line.text.foo is a debug section associated
12210 with .text.foo. */
12211 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12212 {
12213 unsigned int dlen;
12214
12215 if (dsec->gc_mark == 0
12216 || (dsec->flags & SEC_DEBUGGING) == 0)
12217 continue;
12218
12219 dlen = strlen (dsec->name);
12220
12221 if (dlen > ilen
12222 && strncmp (dsec->name + (dlen - ilen),
12223 isec->name, ilen) == 0)
12224 {
12225 dsec->gc_mark = 0;
12226 }
12227 }
12228 }
12229 }
12230 return TRUE;
12231 }
12232
12233 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12234
12235 struct elf_gc_sweep_symbol_info
12236 {
12237 struct bfd_link_info *info;
12238 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
12239 bfd_boolean);
12240 };
12241
12242 static bfd_boolean
12243 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
12244 {
12245 if (!h->mark
12246 && (((h->root.type == bfd_link_hash_defined
12247 || h->root.type == bfd_link_hash_defweak)
12248 && !((h->def_regular || ELF_COMMON_DEF_P (h))
12249 && h->root.u.def.section->gc_mark))
12250 || h->root.type == bfd_link_hash_undefined
12251 || h->root.type == bfd_link_hash_undefweak))
12252 {
12253 struct elf_gc_sweep_symbol_info *inf;
12254
12255 inf = (struct elf_gc_sweep_symbol_info *) data;
12256 (*inf->hide_symbol) (inf->info, h, TRUE);
12257 h->def_regular = 0;
12258 h->ref_regular = 0;
12259 h->ref_regular_nonweak = 0;
12260 }
12261
12262 return TRUE;
12263 }
12264
12265 /* The sweep phase of garbage collection. Remove all garbage sections. */
12266
12267 typedef bfd_boolean (*gc_sweep_hook_fn)
12268 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
12269
12270 static bfd_boolean
12271 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
12272 {
12273 bfd *sub;
12274 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12275 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
12276 unsigned long section_sym_count;
12277 struct elf_gc_sweep_symbol_info sweep_info;
12278
12279 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12280 {
12281 asection *o;
12282
12283 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12284 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
12285 continue;
12286
12287 for (o = sub->sections; o != NULL; o = o->next)
12288 {
12289 /* When any section in a section group is kept, we keep all
12290 sections in the section group. If the first member of
12291 the section group is excluded, we will also exclude the
12292 group section. */
12293 if (o->flags & SEC_GROUP)
12294 {
12295 asection *first = elf_next_in_group (o);
12296 o->gc_mark = first->gc_mark;
12297 }
12298
12299 if (o->gc_mark)
12300 continue;
12301
12302 /* Skip sweeping sections already excluded. */
12303 if (o->flags & SEC_EXCLUDE)
12304 continue;
12305
12306 /* Since this is early in the link process, it is simple
12307 to remove a section from the output. */
12308 o->flags |= SEC_EXCLUDE;
12309
12310 if (info->print_gc_sections && o->size != 0)
12311 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
12312
12313 /* But we also have to update some of the relocation
12314 info we collected before. */
12315 if (gc_sweep_hook
12316 && (o->flags & SEC_RELOC) != 0
12317 && o->reloc_count != 0
12318 && !((info->strip == strip_all || info->strip == strip_debugger)
12319 && (o->flags & SEC_DEBUGGING) != 0)
12320 && !bfd_is_abs_section (o->output_section))
12321 {
12322 Elf_Internal_Rela *internal_relocs;
12323 bfd_boolean r;
12324
12325 internal_relocs
12326 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
12327 info->keep_memory);
12328 if (internal_relocs == NULL)
12329 return FALSE;
12330
12331 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
12332
12333 if (elf_section_data (o)->relocs != internal_relocs)
12334 free (internal_relocs);
12335
12336 if (!r)
12337 return FALSE;
12338 }
12339 }
12340 }
12341
12342 /* Remove the symbols that were in the swept sections from the dynamic
12343 symbol table. GCFIXME: Anyone know how to get them out of the
12344 static symbol table as well? */
12345 sweep_info.info = info;
12346 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
12347 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
12348 &sweep_info);
12349
12350 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
12351 return TRUE;
12352 }
12353
12354 /* Propagate collected vtable information. This is called through
12355 elf_link_hash_traverse. */
12356
12357 static bfd_boolean
12358 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
12359 {
12360 /* Those that are not vtables. */
12361 if (h->vtable == NULL || h->vtable->parent == NULL)
12362 return TRUE;
12363
12364 /* Those vtables that do not have parents, we cannot merge. */
12365 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
12366 return TRUE;
12367
12368 /* If we've already been done, exit. */
12369 if (h->vtable->used && h->vtable->used[-1])
12370 return TRUE;
12371
12372 /* Make sure the parent's table is up to date. */
12373 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
12374
12375 if (h->vtable->used == NULL)
12376 {
12377 /* None of this table's entries were referenced. Re-use the
12378 parent's table. */
12379 h->vtable->used = h->vtable->parent->vtable->used;
12380 h->vtable->size = h->vtable->parent->vtable->size;
12381 }
12382 else
12383 {
12384 size_t n;
12385 bfd_boolean *cu, *pu;
12386
12387 /* Or the parent's entries into ours. */
12388 cu = h->vtable->used;
12389 cu[-1] = TRUE;
12390 pu = h->vtable->parent->vtable->used;
12391 if (pu != NULL)
12392 {
12393 const struct elf_backend_data *bed;
12394 unsigned int log_file_align;
12395
12396 bed = get_elf_backend_data (h->root.u.def.section->owner);
12397 log_file_align = bed->s->log_file_align;
12398 n = h->vtable->parent->vtable->size >> log_file_align;
12399 while (n--)
12400 {
12401 if (*pu)
12402 *cu = TRUE;
12403 pu++;
12404 cu++;
12405 }
12406 }
12407 }
12408
12409 return TRUE;
12410 }
12411
12412 static bfd_boolean
12413 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
12414 {
12415 asection *sec;
12416 bfd_vma hstart, hend;
12417 Elf_Internal_Rela *relstart, *relend, *rel;
12418 const struct elf_backend_data *bed;
12419 unsigned int log_file_align;
12420
12421 /* Take care of both those symbols that do not describe vtables as
12422 well as those that are not loaded. */
12423 if (h->vtable == NULL || h->vtable->parent == NULL)
12424 return TRUE;
12425
12426 BFD_ASSERT (h->root.type == bfd_link_hash_defined
12427 || h->root.type == bfd_link_hash_defweak);
12428
12429 sec = h->root.u.def.section;
12430 hstart = h->root.u.def.value;
12431 hend = hstart + h->size;
12432
12433 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
12434 if (!relstart)
12435 return *(bfd_boolean *) okp = FALSE;
12436 bed = get_elf_backend_data (sec->owner);
12437 log_file_align = bed->s->log_file_align;
12438
12439 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
12440
12441 for (rel = relstart; rel < relend; ++rel)
12442 if (rel->r_offset >= hstart && rel->r_offset < hend)
12443 {
12444 /* If the entry is in use, do nothing. */
12445 if (h->vtable->used
12446 && (rel->r_offset - hstart) < h->vtable->size)
12447 {
12448 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
12449 if (h->vtable->used[entry])
12450 continue;
12451 }
12452 /* Otherwise, kill it. */
12453 rel->r_offset = rel->r_info = rel->r_addend = 0;
12454 }
12455
12456 return TRUE;
12457 }
12458
12459 /* Mark sections containing dynamically referenced symbols. When
12460 building shared libraries, we must assume that any visible symbol is
12461 referenced. */
12462
12463 bfd_boolean
12464 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
12465 {
12466 struct bfd_link_info *info = (struct bfd_link_info *) inf;
12467 struct bfd_elf_dynamic_list *d = info->dynamic_list;
12468
12469 if ((h->root.type == bfd_link_hash_defined
12470 || h->root.type == bfd_link_hash_defweak)
12471 && (h->ref_dynamic
12472 || ((h->def_regular || ELF_COMMON_DEF_P (h))
12473 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
12474 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
12475 && (!info->executable
12476 || info->export_dynamic
12477 || (h->dynamic
12478 && d != NULL
12479 && (*d->match) (&d->head, NULL, h->root.root.string)))
12480 && (strchr (h->root.root.string, ELF_VER_CHR) != NULL
12481 || !bfd_hide_sym_by_version (info->version_info,
12482 h->root.root.string)))))
12483 h->root.u.def.section->flags |= SEC_KEEP;
12484
12485 return TRUE;
12486 }
12487
12488 /* Keep all sections containing symbols undefined on the command-line,
12489 and the section containing the entry symbol. */
12490
12491 void
12492 _bfd_elf_gc_keep (struct bfd_link_info *info)
12493 {
12494 struct bfd_sym_chain *sym;
12495
12496 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
12497 {
12498 struct elf_link_hash_entry *h;
12499
12500 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
12501 FALSE, FALSE, FALSE);
12502
12503 if (h != NULL
12504 && (h->root.type == bfd_link_hash_defined
12505 || h->root.type == bfd_link_hash_defweak)
12506 && !bfd_is_abs_section (h->root.u.def.section))
12507 h->root.u.def.section->flags |= SEC_KEEP;
12508 }
12509 }
12510
12511 bfd_boolean
12512 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
12513 struct bfd_link_info *info)
12514 {
12515 bfd *ibfd = info->input_bfds;
12516
12517 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12518 {
12519 asection *sec;
12520 struct elf_reloc_cookie cookie;
12521
12522 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12523 continue;
12524
12525 if (!init_reloc_cookie (&cookie, info, ibfd))
12526 return FALSE;
12527
12528 for (sec = ibfd->sections; sec; sec = sec->next)
12529 {
12530 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
12531 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
12532 {
12533 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
12534 fini_reloc_cookie_rels (&cookie, sec);
12535 }
12536 }
12537 }
12538 return TRUE;
12539 }
12540
12541 /* Do mark and sweep of unused sections. */
12542
12543 bfd_boolean
12544 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
12545 {
12546 bfd_boolean ok = TRUE;
12547 bfd *sub;
12548 elf_gc_mark_hook_fn gc_mark_hook;
12549 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12550 struct elf_link_hash_table *htab;
12551
12552 if (!bed->can_gc_sections
12553 || !is_elf_hash_table (info->hash))
12554 {
12555 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
12556 return TRUE;
12557 }
12558
12559 bed->gc_keep (info);
12560 htab = elf_hash_table (info);
12561
12562 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
12563 at the .eh_frame section if we can mark the FDEs individually. */
12564 for (sub = info->input_bfds;
12565 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
12566 sub = sub->link.next)
12567 {
12568 asection *sec;
12569 struct elf_reloc_cookie cookie;
12570
12571 sec = bfd_get_section_by_name (sub, ".eh_frame");
12572 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
12573 {
12574 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
12575 if (elf_section_data (sec)->sec_info
12576 && (sec->flags & SEC_LINKER_CREATED) == 0)
12577 elf_eh_frame_section (sub) = sec;
12578 fini_reloc_cookie_for_section (&cookie, sec);
12579 sec = bfd_get_next_section_by_name (sec);
12580 }
12581 }
12582
12583 /* Apply transitive closure to the vtable entry usage info. */
12584 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
12585 if (!ok)
12586 return FALSE;
12587
12588 /* Kill the vtable relocations that were not used. */
12589 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
12590 if (!ok)
12591 return FALSE;
12592
12593 /* Mark dynamically referenced symbols. */
12594 if (htab->dynamic_sections_created)
12595 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
12596
12597 /* Grovel through relocs to find out who stays ... */
12598 gc_mark_hook = bed->gc_mark_hook;
12599 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12600 {
12601 asection *o;
12602
12603 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12604 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
12605 continue;
12606
12607 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
12608 Also treat note sections as a root, if the section is not part
12609 of a group. */
12610 for (o = sub->sections; o != NULL; o = o->next)
12611 if (!o->gc_mark
12612 && (o->flags & SEC_EXCLUDE) == 0
12613 && ((o->flags & SEC_KEEP) != 0
12614 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
12615 && elf_next_in_group (o) == NULL )))
12616 {
12617 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12618 return FALSE;
12619 }
12620 }
12621
12622 /* Allow the backend to mark additional target specific sections. */
12623 bed->gc_mark_extra_sections (info, gc_mark_hook);
12624
12625 /* ... and mark SEC_EXCLUDE for those that go. */
12626 return elf_gc_sweep (abfd, info);
12627 }
12628 \f
12629 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
12630
12631 bfd_boolean
12632 bfd_elf_gc_record_vtinherit (bfd *abfd,
12633 asection *sec,
12634 struct elf_link_hash_entry *h,
12635 bfd_vma offset)
12636 {
12637 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
12638 struct elf_link_hash_entry **search, *child;
12639 bfd_size_type extsymcount;
12640 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12641
12642 /* The sh_info field of the symtab header tells us where the
12643 external symbols start. We don't care about the local symbols at
12644 this point. */
12645 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
12646 if (!elf_bad_symtab (abfd))
12647 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
12648
12649 sym_hashes = elf_sym_hashes (abfd);
12650 sym_hashes_end = sym_hashes + extsymcount;
12651
12652 /* Hunt down the child symbol, which is in this section at the same
12653 offset as the relocation. */
12654 for (search = sym_hashes; search != sym_hashes_end; ++search)
12655 {
12656 if ((child = *search) != NULL
12657 && (child->root.type == bfd_link_hash_defined
12658 || child->root.type == bfd_link_hash_defweak)
12659 && child->root.u.def.section == sec
12660 && child->root.u.def.value == offset)
12661 goto win;
12662 }
12663
12664 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
12665 abfd, sec, (unsigned long) offset);
12666 bfd_set_error (bfd_error_invalid_operation);
12667 return FALSE;
12668
12669 win:
12670 if (!child->vtable)
12671 {
12672 child->vtable = ((struct elf_link_virtual_table_entry *)
12673 bfd_zalloc (abfd, sizeof (*child->vtable)));
12674 if (!child->vtable)
12675 return FALSE;
12676 }
12677 if (!h)
12678 {
12679 /* This *should* only be the absolute section. It could potentially
12680 be that someone has defined a non-global vtable though, which
12681 would be bad. It isn't worth paging in the local symbols to be
12682 sure though; that case should simply be handled by the assembler. */
12683
12684 child->vtable->parent = (struct elf_link_hash_entry *) -1;
12685 }
12686 else
12687 child->vtable->parent = h;
12688
12689 return TRUE;
12690 }
12691
12692 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
12693
12694 bfd_boolean
12695 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
12696 asection *sec ATTRIBUTE_UNUSED,
12697 struct elf_link_hash_entry *h,
12698 bfd_vma addend)
12699 {
12700 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12701 unsigned int log_file_align = bed->s->log_file_align;
12702
12703 if (!h->vtable)
12704 {
12705 h->vtable = ((struct elf_link_virtual_table_entry *)
12706 bfd_zalloc (abfd, sizeof (*h->vtable)));
12707 if (!h->vtable)
12708 return FALSE;
12709 }
12710
12711 if (addend >= h->vtable->size)
12712 {
12713 size_t size, bytes, file_align;
12714 bfd_boolean *ptr = h->vtable->used;
12715
12716 /* While the symbol is undefined, we have to be prepared to handle
12717 a zero size. */
12718 file_align = 1 << log_file_align;
12719 if (h->root.type == bfd_link_hash_undefined)
12720 size = addend + file_align;
12721 else
12722 {
12723 size = h->size;
12724 if (addend >= size)
12725 {
12726 /* Oops! We've got a reference past the defined end of
12727 the table. This is probably a bug -- shall we warn? */
12728 size = addend + file_align;
12729 }
12730 }
12731 size = (size + file_align - 1) & -file_align;
12732
12733 /* Allocate one extra entry for use as a "done" flag for the
12734 consolidation pass. */
12735 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
12736
12737 if (ptr)
12738 {
12739 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
12740
12741 if (ptr != NULL)
12742 {
12743 size_t oldbytes;
12744
12745 oldbytes = (((h->vtable->size >> log_file_align) + 1)
12746 * sizeof (bfd_boolean));
12747 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
12748 }
12749 }
12750 else
12751 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
12752
12753 if (ptr == NULL)
12754 return FALSE;
12755
12756 /* And arrange for that done flag to be at index -1. */
12757 h->vtable->used = ptr + 1;
12758 h->vtable->size = size;
12759 }
12760
12761 h->vtable->used[addend >> log_file_align] = TRUE;
12762
12763 return TRUE;
12764 }
12765
12766 /* Map an ELF section header flag to its corresponding string. */
12767 typedef struct
12768 {
12769 char *flag_name;
12770 flagword flag_value;
12771 } elf_flags_to_name_table;
12772
12773 static elf_flags_to_name_table elf_flags_to_names [] =
12774 {
12775 { "SHF_WRITE", SHF_WRITE },
12776 { "SHF_ALLOC", SHF_ALLOC },
12777 { "SHF_EXECINSTR", SHF_EXECINSTR },
12778 { "SHF_MERGE", SHF_MERGE },
12779 { "SHF_STRINGS", SHF_STRINGS },
12780 { "SHF_INFO_LINK", SHF_INFO_LINK},
12781 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
12782 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
12783 { "SHF_GROUP", SHF_GROUP },
12784 { "SHF_TLS", SHF_TLS },
12785 { "SHF_MASKOS", SHF_MASKOS },
12786 { "SHF_EXCLUDE", SHF_EXCLUDE },
12787 };
12788
12789 /* Returns TRUE if the section is to be included, otherwise FALSE. */
12790 bfd_boolean
12791 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
12792 struct flag_info *flaginfo,
12793 asection *section)
12794 {
12795 const bfd_vma sh_flags = elf_section_flags (section);
12796
12797 if (!flaginfo->flags_initialized)
12798 {
12799 bfd *obfd = info->output_bfd;
12800 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
12801 struct flag_info_list *tf = flaginfo->flag_list;
12802 int with_hex = 0;
12803 int without_hex = 0;
12804
12805 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
12806 {
12807 unsigned i;
12808 flagword (*lookup) (char *);
12809
12810 lookup = bed->elf_backend_lookup_section_flags_hook;
12811 if (lookup != NULL)
12812 {
12813 flagword hexval = (*lookup) ((char *) tf->name);
12814
12815 if (hexval != 0)
12816 {
12817 if (tf->with == with_flags)
12818 with_hex |= hexval;
12819 else if (tf->with == without_flags)
12820 without_hex |= hexval;
12821 tf->valid = TRUE;
12822 continue;
12823 }
12824 }
12825 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
12826 {
12827 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
12828 {
12829 if (tf->with == with_flags)
12830 with_hex |= elf_flags_to_names[i].flag_value;
12831 else if (tf->with == without_flags)
12832 without_hex |= elf_flags_to_names[i].flag_value;
12833 tf->valid = TRUE;
12834 break;
12835 }
12836 }
12837 if (!tf->valid)
12838 {
12839 info->callbacks->einfo
12840 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
12841 return FALSE;
12842 }
12843 }
12844 flaginfo->flags_initialized = TRUE;
12845 flaginfo->only_with_flags |= with_hex;
12846 flaginfo->not_with_flags |= without_hex;
12847 }
12848
12849 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
12850 return FALSE;
12851
12852 if ((flaginfo->not_with_flags & sh_flags) != 0)
12853 return FALSE;
12854
12855 return TRUE;
12856 }
12857
12858 struct alloc_got_off_arg {
12859 bfd_vma gotoff;
12860 struct bfd_link_info *info;
12861 };
12862
12863 /* We need a special top-level link routine to convert got reference counts
12864 to real got offsets. */
12865
12866 static bfd_boolean
12867 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
12868 {
12869 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
12870 bfd *obfd = gofarg->info->output_bfd;
12871 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
12872
12873 if (h->got.refcount > 0)
12874 {
12875 h->got.offset = gofarg->gotoff;
12876 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
12877 }
12878 else
12879 h->got.offset = (bfd_vma) -1;
12880
12881 return TRUE;
12882 }
12883
12884 /* And an accompanying bit to work out final got entry offsets once
12885 we're done. Should be called from final_link. */
12886
12887 bfd_boolean
12888 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
12889 struct bfd_link_info *info)
12890 {
12891 bfd *i;
12892 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12893 bfd_vma gotoff;
12894 struct alloc_got_off_arg gofarg;
12895
12896 BFD_ASSERT (abfd == info->output_bfd);
12897
12898 if (! is_elf_hash_table (info->hash))
12899 return FALSE;
12900
12901 /* The GOT offset is relative to the .got section, but the GOT header is
12902 put into the .got.plt section, if the backend uses it. */
12903 if (bed->want_got_plt)
12904 gotoff = 0;
12905 else
12906 gotoff = bed->got_header_size;
12907
12908 /* Do the local .got entries first. */
12909 for (i = info->input_bfds; i; i = i->link.next)
12910 {
12911 bfd_signed_vma *local_got;
12912 bfd_size_type j, locsymcount;
12913 Elf_Internal_Shdr *symtab_hdr;
12914
12915 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
12916 continue;
12917
12918 local_got = elf_local_got_refcounts (i);
12919 if (!local_got)
12920 continue;
12921
12922 symtab_hdr = &elf_tdata (i)->symtab_hdr;
12923 if (elf_bad_symtab (i))
12924 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12925 else
12926 locsymcount = symtab_hdr->sh_info;
12927
12928 for (j = 0; j < locsymcount; ++j)
12929 {
12930 if (local_got[j] > 0)
12931 {
12932 local_got[j] = gotoff;
12933 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
12934 }
12935 else
12936 local_got[j] = (bfd_vma) -1;
12937 }
12938 }
12939
12940 /* Then the global .got entries. .plt refcounts are handled by
12941 adjust_dynamic_symbol */
12942 gofarg.gotoff = gotoff;
12943 gofarg.info = info;
12944 elf_link_hash_traverse (elf_hash_table (info),
12945 elf_gc_allocate_got_offsets,
12946 &gofarg);
12947 return TRUE;
12948 }
12949
12950 /* Many folk need no more in the way of final link than this, once
12951 got entry reference counting is enabled. */
12952
12953 bfd_boolean
12954 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
12955 {
12956 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
12957 return FALSE;
12958
12959 /* Invoke the regular ELF backend linker to do all the work. */
12960 return bfd_elf_final_link (abfd, info);
12961 }
12962
12963 bfd_boolean
12964 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
12965 {
12966 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
12967
12968 if (rcookie->bad_symtab)
12969 rcookie->rel = rcookie->rels;
12970
12971 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
12972 {
12973 unsigned long r_symndx;
12974
12975 if (! rcookie->bad_symtab)
12976 if (rcookie->rel->r_offset > offset)
12977 return FALSE;
12978 if (rcookie->rel->r_offset != offset)
12979 continue;
12980
12981 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
12982 if (r_symndx == STN_UNDEF)
12983 return TRUE;
12984
12985 if (r_symndx >= rcookie->locsymcount
12986 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12987 {
12988 struct elf_link_hash_entry *h;
12989
12990 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
12991
12992 while (h->root.type == bfd_link_hash_indirect
12993 || h->root.type == bfd_link_hash_warning)
12994 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12995
12996 if ((h->root.type == bfd_link_hash_defined
12997 || h->root.type == bfd_link_hash_defweak)
12998 && (h->root.u.def.section->owner != rcookie->abfd
12999 || h->root.u.def.section->kept_section != NULL
13000 || discarded_section (h->root.u.def.section)))
13001 return TRUE;
13002 }
13003 else
13004 {
13005 /* It's not a relocation against a global symbol,
13006 but it could be a relocation against a local
13007 symbol for a discarded section. */
13008 asection *isec;
13009 Elf_Internal_Sym *isym;
13010
13011 /* Need to: get the symbol; get the section. */
13012 isym = &rcookie->locsyms[r_symndx];
13013 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
13014 if (isec != NULL
13015 && (isec->kept_section != NULL
13016 || discarded_section (isec)))
13017 return TRUE;
13018 }
13019 return FALSE;
13020 }
13021 return FALSE;
13022 }
13023
13024 /* Discard unneeded references to discarded sections.
13025 Returns -1 on error, 1 if any section's size was changed, 0 if
13026 nothing changed. This function assumes that the relocations are in
13027 sorted order, which is true for all known assemblers. */
13028
13029 int
13030 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13031 {
13032 struct elf_reloc_cookie cookie;
13033 asection *o;
13034 bfd *abfd;
13035 int changed = 0;
13036
13037 if (info->traditional_format
13038 || !is_elf_hash_table (info->hash))
13039 return 0;
13040
13041 o = bfd_get_section_by_name (output_bfd, ".stab");
13042 if (o != NULL)
13043 {
13044 asection *i;
13045
13046 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13047 {
13048 if (i->size == 0
13049 || i->reloc_count == 0
13050 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13051 continue;
13052
13053 abfd = i->owner;
13054 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13055 continue;
13056
13057 if (!init_reloc_cookie_for_section (&cookie, info, i))
13058 return -1;
13059
13060 if (_bfd_discard_section_stabs (abfd, i,
13061 elf_section_data (i)->sec_info,
13062 bfd_elf_reloc_symbol_deleted_p,
13063 &cookie))
13064 changed = 1;
13065
13066 fini_reloc_cookie_for_section (&cookie, i);
13067 }
13068 }
13069
13070 o = NULL;
13071 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13072 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
13073 if (o != NULL)
13074 {
13075 asection *i;
13076
13077 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13078 {
13079 if (i->size == 0)
13080 continue;
13081
13082 abfd = i->owner;
13083 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13084 continue;
13085
13086 if (!init_reloc_cookie_for_section (&cookie, info, i))
13087 return -1;
13088
13089 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13090 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
13091 bfd_elf_reloc_symbol_deleted_p,
13092 &cookie))
13093 changed = 1;
13094
13095 fini_reloc_cookie_for_section (&cookie, i);
13096 }
13097 }
13098
13099 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13100 {
13101 const struct elf_backend_data *bed;
13102
13103 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13104 continue;
13105
13106 bed = get_elf_backend_data (abfd);
13107
13108 if (bed->elf_backend_discard_info != NULL)
13109 {
13110 if (!init_reloc_cookie (&cookie, info, abfd))
13111 return -1;
13112
13113 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
13114 changed = 1;
13115
13116 fini_reloc_cookie (&cookie, abfd);
13117 }
13118 }
13119
13120 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13121 _bfd_elf_end_eh_frame_parsing (info);
13122
13123 if (info->eh_frame_hdr_type
13124 && !info->relocatable
13125 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
13126 changed = 1;
13127
13128 return changed;
13129 }
13130
13131 bfd_boolean
13132 _bfd_elf_section_already_linked (bfd *abfd,
13133 asection *sec,
13134 struct bfd_link_info *info)
13135 {
13136 flagword flags;
13137 const char *name, *key;
13138 struct bfd_section_already_linked *l;
13139 struct bfd_section_already_linked_hash_entry *already_linked_list;
13140
13141 if (sec->output_section == bfd_abs_section_ptr)
13142 return FALSE;
13143
13144 flags = sec->flags;
13145
13146 /* Return if it isn't a linkonce section. A comdat group section
13147 also has SEC_LINK_ONCE set. */
13148 if ((flags & SEC_LINK_ONCE) == 0)
13149 return FALSE;
13150
13151 /* Don't put group member sections on our list of already linked
13152 sections. They are handled as a group via their group section. */
13153 if (elf_sec_group (sec) != NULL)
13154 return FALSE;
13155
13156 /* For a SHT_GROUP section, use the group signature as the key. */
13157 name = sec->name;
13158 if ((flags & SEC_GROUP) != 0
13159 && elf_next_in_group (sec) != NULL
13160 && elf_group_name (elf_next_in_group (sec)) != NULL)
13161 key = elf_group_name (elf_next_in_group (sec));
13162 else
13163 {
13164 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
13165 if (CONST_STRNEQ (name, ".gnu.linkonce.")
13166 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13167 key++;
13168 else
13169 /* Must be a user linkonce section that doesn't follow gcc's
13170 naming convention. In this case we won't be matching
13171 single member groups. */
13172 key = name;
13173 }
13174
13175 already_linked_list = bfd_section_already_linked_table_lookup (key);
13176
13177 for (l = already_linked_list->entry; l != NULL; l = l->next)
13178 {
13179 /* We may have 2 different types of sections on the list: group
13180 sections with a signature of <key> (<key> is some string),
13181 and linkonce sections named .gnu.linkonce.<type>.<key>.
13182 Match like sections. LTO plugin sections are an exception.
13183 They are always named .gnu.linkonce.t.<key> and match either
13184 type of section. */
13185 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13186 && ((flags & SEC_GROUP) != 0
13187 || strcmp (name, l->sec->name) == 0))
13188 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
13189 {
13190 /* The section has already been linked. See if we should
13191 issue a warning. */
13192 if (!_bfd_handle_already_linked (sec, l, info))
13193 return FALSE;
13194
13195 if (flags & SEC_GROUP)
13196 {
13197 asection *first = elf_next_in_group (sec);
13198 asection *s = first;
13199
13200 while (s != NULL)
13201 {
13202 s->output_section = bfd_abs_section_ptr;
13203 /* Record which group discards it. */
13204 s->kept_section = l->sec;
13205 s = elf_next_in_group (s);
13206 /* These lists are circular. */
13207 if (s == first)
13208 break;
13209 }
13210 }
13211
13212 return TRUE;
13213 }
13214 }
13215
13216 /* A single member comdat group section may be discarded by a
13217 linkonce section and vice versa. */
13218 if ((flags & SEC_GROUP) != 0)
13219 {
13220 asection *first = elf_next_in_group (sec);
13221
13222 if (first != NULL && elf_next_in_group (first) == first)
13223 /* Check this single member group against linkonce sections. */
13224 for (l = already_linked_list->entry; l != NULL; l = l->next)
13225 if ((l->sec->flags & SEC_GROUP) == 0
13226 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13227 {
13228 first->output_section = bfd_abs_section_ptr;
13229 first->kept_section = l->sec;
13230 sec->output_section = bfd_abs_section_ptr;
13231 break;
13232 }
13233 }
13234 else
13235 /* Check this linkonce section against single member groups. */
13236 for (l = already_linked_list->entry; l != NULL; l = l->next)
13237 if (l->sec->flags & SEC_GROUP)
13238 {
13239 asection *first = elf_next_in_group (l->sec);
13240
13241 if (first != NULL
13242 && elf_next_in_group (first) == first
13243 && bfd_elf_match_symbols_in_sections (first, sec, info))
13244 {
13245 sec->output_section = bfd_abs_section_ptr;
13246 sec->kept_section = first;
13247 break;
13248 }
13249 }
13250
13251 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13252 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13253 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13254 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13255 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13256 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13257 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13258 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13259 The reverse order cannot happen as there is never a bfd with only the
13260 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13261 matter as here were are looking only for cross-bfd sections. */
13262
13263 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13264 for (l = already_linked_list->entry; l != NULL; l = l->next)
13265 if ((l->sec->flags & SEC_GROUP) == 0
13266 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13267 {
13268 if (abfd != l->sec->owner)
13269 sec->output_section = bfd_abs_section_ptr;
13270 break;
13271 }
13272
13273 /* This is the first section with this name. Record it. */
13274 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
13275 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
13276 return sec->output_section == bfd_abs_section_ptr;
13277 }
13278
13279 bfd_boolean
13280 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
13281 {
13282 return sym->st_shndx == SHN_COMMON;
13283 }
13284
13285 unsigned int
13286 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13287 {
13288 return SHN_COMMON;
13289 }
13290
13291 asection *
13292 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13293 {
13294 return bfd_com_section_ptr;
13295 }
13296
13297 bfd_vma
13298 _bfd_elf_default_got_elt_size (bfd *abfd,
13299 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13300 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13301 bfd *ibfd ATTRIBUTE_UNUSED,
13302 unsigned long symndx ATTRIBUTE_UNUSED)
13303 {
13304 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13305 return bed->s->arch_size / 8;
13306 }
13307
13308 /* Routines to support the creation of dynamic relocs. */
13309
13310 /* Returns the name of the dynamic reloc section associated with SEC. */
13311
13312 static const char *
13313 get_dynamic_reloc_section_name (bfd * abfd,
13314 asection * sec,
13315 bfd_boolean is_rela)
13316 {
13317 char *name;
13318 const char *old_name = bfd_get_section_name (NULL, sec);
13319 const char *prefix = is_rela ? ".rela" : ".rel";
13320
13321 if (old_name == NULL)
13322 return NULL;
13323
13324 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
13325 sprintf (name, "%s%s", prefix, old_name);
13326
13327 return name;
13328 }
13329
13330 /* Returns the dynamic reloc section associated with SEC.
13331 If necessary compute the name of the dynamic reloc section based
13332 on SEC's name (looked up in ABFD's string table) and the setting
13333 of IS_RELA. */
13334
13335 asection *
13336 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
13337 asection * sec,
13338 bfd_boolean is_rela)
13339 {
13340 asection * reloc_sec = elf_section_data (sec)->sreloc;
13341
13342 if (reloc_sec == NULL)
13343 {
13344 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13345
13346 if (name != NULL)
13347 {
13348 reloc_sec = bfd_get_linker_section (abfd, name);
13349
13350 if (reloc_sec != NULL)
13351 elf_section_data (sec)->sreloc = reloc_sec;
13352 }
13353 }
13354
13355 return reloc_sec;
13356 }
13357
13358 /* Returns the dynamic reloc section associated with SEC. If the
13359 section does not exist it is created and attached to the DYNOBJ
13360 bfd and stored in the SRELOC field of SEC's elf_section_data
13361 structure.
13362
13363 ALIGNMENT is the alignment for the newly created section and
13364 IS_RELA defines whether the name should be .rela.<SEC's name>
13365 or .rel.<SEC's name>. The section name is looked up in the
13366 string table associated with ABFD. */
13367
13368 asection *
13369 _bfd_elf_make_dynamic_reloc_section (asection *sec,
13370 bfd *dynobj,
13371 unsigned int alignment,
13372 bfd *abfd,
13373 bfd_boolean is_rela)
13374 {
13375 asection * reloc_sec = elf_section_data (sec)->sreloc;
13376
13377 if (reloc_sec == NULL)
13378 {
13379 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13380
13381 if (name == NULL)
13382 return NULL;
13383
13384 reloc_sec = bfd_get_linker_section (dynobj, name);
13385
13386 if (reloc_sec == NULL)
13387 {
13388 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
13389 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
13390 if ((sec->flags & SEC_ALLOC) != 0)
13391 flags |= SEC_ALLOC | SEC_LOAD;
13392
13393 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
13394 if (reloc_sec != NULL)
13395 {
13396 /* _bfd_elf_get_sec_type_attr chooses a section type by
13397 name. Override as it may be wrong, eg. for a user
13398 section named "auto" we'll get ".relauto" which is
13399 seen to be a .rela section. */
13400 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
13401 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
13402 reloc_sec = NULL;
13403 }
13404 }
13405
13406 elf_section_data (sec)->sreloc = reloc_sec;
13407 }
13408
13409 return reloc_sec;
13410 }
13411
13412 /* Copy the ELF symbol type and other attributes for a linker script
13413 assignment from HSRC to HDEST. Generally this should be treated as
13414 if we found a strong non-dynamic definition for HDEST (except that
13415 ld ignores multiple definition errors). */
13416 void
13417 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
13418 struct bfd_link_hash_entry *hdest,
13419 struct bfd_link_hash_entry *hsrc)
13420 {
13421 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
13422 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
13423 Elf_Internal_Sym isym;
13424
13425 ehdest->type = ehsrc->type;
13426 ehdest->target_internal = ehsrc->target_internal;
13427
13428 isym.st_other = ehsrc->other;
13429 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
13430 }
13431
13432 /* Append a RELA relocation REL to section S in BFD. */
13433
13434 void
13435 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13436 {
13437 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13438 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
13439 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
13440 bed->s->swap_reloca_out (abfd, rel, loc);
13441 }
13442
13443 /* Append a REL relocation REL to section S in BFD. */
13444
13445 void
13446 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13447 {
13448 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13449 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
13450 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
13451 bed->s->swap_reloc_out (abfd, rel, loc);
13452 }
This page took 0.540894 seconds and 4 git commands to generate.