elflink.c revision 1.11.2.2 1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2016 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
20
21 #include "sysdep.h"
22 #include "bfd.h"
23 #include "bfd_stdint.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26 #define ARCH_SIZE 0
27 #include "elf-bfd.h"
28 #include "safe-ctype.h"
29 #include "libiberty.h"
30 #include "objalloc.h"
31 #if BFD_SUPPORTS_PLUGINS
32 #include "plugin.h"
33 #endif
34
35 /* This struct is used to pass information to routines called via
36 elf_link_hash_traverse which must return failure. */
37
38 struct elf_info_failed
39 {
40 struct bfd_link_info *info;
41 bfd_boolean failed;
42 };
43
44 /* This structure is used to pass information to
45 _bfd_elf_link_find_version_dependencies. */
46
47 struct elf_find_verdep_info
48 {
49 /* General link information. */
50 struct bfd_link_info *info;
51 /* The number of dependencies. */
52 unsigned int vers;
53 /* Whether we had a failure. */
54 bfd_boolean failed;
55 };
56
57 static bfd_boolean _bfd_elf_fix_symbol_flags
58 (struct elf_link_hash_entry *, struct elf_info_failed *);
59
60 asection *
61 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
62 unsigned long r_symndx,
63 bfd_boolean discard)
64 {
65 if (r_symndx >= cookie->locsymcount
66 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
67 {
68 struct elf_link_hash_entry *h;
69
70 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
71
72 while (h->root.type == bfd_link_hash_indirect
73 || h->root.type == bfd_link_hash_warning)
74 h = (struct elf_link_hash_entry *) h->root.u.i.link;
75
76 if ((h->root.type == bfd_link_hash_defined
77 || h->root.type == bfd_link_hash_defweak)
78 && discarded_section (h->root.u.def.section))
79 return h->root.u.def.section;
80 else
81 return NULL;
82 }
83 else
84 {
85 /* It's not a relocation against a global symbol,
86 but it could be a relocation against a local
87 symbol for a discarded section. */
88 asection *isec;
89 Elf_Internal_Sym *isym;
90
91 /* Need to: get the symbol; get the section. */
92 isym = &cookie->locsyms[r_symndx];
93 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
94 if (isec != NULL
95 && discard ? discarded_section (isec) : 1)
96 return isec;
97 }
98 return NULL;
99 }
100
101 /* Define a symbol in a dynamic linkage section. */
102
103 struct elf_link_hash_entry *
104 _bfd_elf_define_linkage_sym (bfd *abfd,
105 struct bfd_link_info *info,
106 asection *sec,
107 const char *name)
108 {
109 struct elf_link_hash_entry *h;
110 struct bfd_link_hash_entry *bh;
111 const struct elf_backend_data *bed;
112
113 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
114 if (h != NULL)
115 {
116 /* Zap symbol defined in an as-needed lib that wasn't linked.
117 This is a symptom of a larger problem: Absolute symbols
118 defined in shared libraries can't be overridden, because we
119 lose the link to the bfd which is via the symbol section. */
120 h->root.type = bfd_link_hash_new;
121 }
122
123 bh = &h->root;
124 bed = get_elf_backend_data (abfd);
125 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
126 sec, 0, NULL, FALSE, bed->collect,
127 &bh))
128 return NULL;
129 h = (struct elf_link_hash_entry *) bh;
130 h->def_regular = 1;
131 h->non_elf = 0;
132 h->root.linker_def = 1;
133 h->type = STT_OBJECT;
134 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
135 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
136
137 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
138 return h;
139 }
140
141 bfd_boolean
142 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
143 {
144 flagword flags;
145 asection *s;
146 struct elf_link_hash_entry *h;
147 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
148 struct elf_link_hash_table *htab = elf_hash_table (info);
149
150 /* This function may be called more than once. */
151 s = bfd_get_linker_section (abfd, ".got");
152 if (s != NULL)
153 return TRUE;
154
155 flags = bed->dynamic_sec_flags;
156
157 s = bfd_make_section_anyway_with_flags (abfd,
158 (bed->rela_plts_and_copies_p
159 ? ".rela.got" : ".rel.got"),
160 (bed->dynamic_sec_flags
161 | SEC_READONLY));
162 if (s == NULL
163 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
164 return FALSE;
165 htab->srelgot = s;
166
167 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
168 if (s == NULL
169 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
170 return FALSE;
171 htab->sgot = s;
172
173 if (bed->want_got_plt)
174 {
175 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
176 if (s == NULL
177 || !bfd_set_section_alignment (abfd, s,
178 bed->s->log_file_align))
179 return FALSE;
180 htab->sgotplt = s;
181 }
182
183 /* The first bit of the global offset table is the header. */
184 s->size += bed->got_header_size;
185
186 if (bed->want_got_sym)
187 {
188 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
189 (or .got.plt) section. We don't do this in the linker script
190 because we don't want to define the symbol if we are not creating
191 a global offset table. */
192 h = _bfd_elf_define_linkage_sym (abfd, info, s,
193 "_GLOBAL_OFFSET_TABLE_");
194 elf_hash_table (info)->hgot = h;
195 if (h == NULL)
196 return FALSE;
197 }
198
199 return TRUE;
200 }
201
202 /* Create a strtab to hold the dynamic symbol names. */
204 static bfd_boolean
205 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
206 {
207 struct elf_link_hash_table *hash_table;
208
209 hash_table = elf_hash_table (info);
210 if (hash_table->dynobj == NULL)
211 {
212 /* We may not set dynobj, an input file holding linker created
213 dynamic sections to abfd, which may be a dynamic object with
214 its own dynamic sections. We need to find a normal input file
215 to hold linker created sections if possible. */
216 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
217 {
218 bfd *ibfd;
219 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
220 if ((ibfd->flags
221 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0)
222 {
223 abfd = ibfd;
224 break;
225 }
226 }
227 hash_table->dynobj = abfd;
228 }
229
230 if (hash_table->dynstr == NULL)
231 {
232 hash_table->dynstr = _bfd_elf_strtab_init ();
233 if (hash_table->dynstr == NULL)
234 return FALSE;
235 }
236 return TRUE;
237 }
238
239 /* Create some sections which will be filled in with dynamic linking
240 information. ABFD is an input file which requires dynamic sections
241 to be created. The dynamic sections take up virtual memory space
242 when the final executable is run, so we need to create them before
243 addresses are assigned to the output sections. We work out the
244 actual contents and size of these sections later. */
245
246 bfd_boolean
247 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
248 {
249 flagword flags;
250 asection *s;
251 const struct elf_backend_data *bed;
252 struct elf_link_hash_entry *h;
253
254 if (! is_elf_hash_table (info->hash))
255 return FALSE;
256
257 if (elf_hash_table (info)->dynamic_sections_created)
258 return TRUE;
259
260 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
261 return FALSE;
262
263 abfd = elf_hash_table (info)->dynobj;
264 bed = get_elf_backend_data (abfd);
265
266 flags = bed->dynamic_sec_flags;
267
268 /* A dynamically linked executable has a .interp section, but a
269 shared library does not. */
270 if (bfd_link_executable (info) && !info->nointerp)
271 {
272 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
273 flags | SEC_READONLY);
274 if (s == NULL)
275 return FALSE;
276 }
277
278 /* Create sections to hold version informations. These are removed
279 if they are not needed. */
280 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
281 flags | SEC_READONLY);
282 if (s == NULL
283 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
284 return FALSE;
285
286 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
287 flags | SEC_READONLY);
288 if (s == NULL
289 || ! bfd_set_section_alignment (abfd, s, 1))
290 return FALSE;
291
292 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
293 flags | SEC_READONLY);
294 if (s == NULL
295 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
296 return FALSE;
297
298 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
299 flags | SEC_READONLY);
300 if (s == NULL
301 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
302 return FALSE;
303 elf_hash_table (info)->dynsym = s;
304
305 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
306 flags | SEC_READONLY);
307 if (s == NULL)
308 return FALSE;
309
310 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
311 if (s == NULL
312 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
313 return FALSE;
314
315 /* The special symbol _DYNAMIC is always set to the start of the
316 .dynamic section. We could set _DYNAMIC in a linker script, but we
317 only want to define it if we are, in fact, creating a .dynamic
318 section. We don't want to define it if there is no .dynamic
319 section, since on some ELF platforms the start up code examines it
320 to decide how to initialize the process. */
321 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
322 elf_hash_table (info)->hdynamic = h;
323 if (h == NULL)
324 return FALSE;
325
326 if (info->emit_hash)
327 {
328 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
329 flags | SEC_READONLY);
330 if (s == NULL
331 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
332 return FALSE;
333 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
334 }
335
336 if (info->emit_gnu_hash)
337 {
338 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
339 flags | SEC_READONLY);
340 if (s == NULL
341 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
342 return FALSE;
343 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
344 4 32-bit words followed by variable count of 64-bit words, then
345 variable count of 32-bit words. */
346 if (bed->s->arch_size == 64)
347 elf_section_data (s)->this_hdr.sh_entsize = 0;
348 else
349 elf_section_data (s)->this_hdr.sh_entsize = 4;
350 }
351
352 /* Let the backend create the rest of the sections. This lets the
353 backend set the right flags. The backend will normally create
354 the .got and .plt sections. */
355 if (bed->elf_backend_create_dynamic_sections == NULL
356 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
357 return FALSE;
358
359 elf_hash_table (info)->dynamic_sections_created = TRUE;
360
361 return TRUE;
362 }
363
364 /* Create dynamic sections when linking against a dynamic object. */
365
366 bfd_boolean
367 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
368 {
369 flagword flags, pltflags;
370 struct elf_link_hash_entry *h;
371 asection *s;
372 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
373 struct elf_link_hash_table *htab = elf_hash_table (info);
374
375 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
376 .rel[a].bss sections. */
377 flags = bed->dynamic_sec_flags;
378
379 pltflags = flags;
380 if (bed->plt_not_loaded)
381 /* We do not clear SEC_ALLOC here because we still want the OS to
382 allocate space for the section; it's just that there's nothing
383 to read in from the object file. */
384 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
385 else
386 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
387 if (bed->plt_readonly)
388 pltflags |= SEC_READONLY;
389
390 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
391 if (s == NULL
392 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
393 return FALSE;
394 htab->splt = s;
395
396 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
397 .plt section. */
398 if (bed->want_plt_sym)
399 {
400 h = _bfd_elf_define_linkage_sym (abfd, info, s,
401 "_PROCEDURE_LINKAGE_TABLE_");
402 elf_hash_table (info)->hplt = h;
403 if (h == NULL)
404 return FALSE;
405 }
406
407 s = bfd_make_section_anyway_with_flags (abfd,
408 (bed->rela_plts_and_copies_p
409 ? ".rela.plt" : ".rel.plt"),
410 flags | SEC_READONLY);
411 if (s == NULL
412 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
413 return FALSE;
414 htab->srelplt = s;
415
416 if (! _bfd_elf_create_got_section (abfd, info))
417 return FALSE;
418
419 if (bed->want_dynbss)
420 {
421 /* The .dynbss section is a place to put symbols which are defined
422 by dynamic objects, are referenced by regular objects, and are
423 not functions. We must allocate space for them in the process
424 image and use a R_*_COPY reloc to tell the dynamic linker to
425 initialize them at run time. The linker script puts the .dynbss
426 section into the .bss section of the final image. */
427 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
428 (SEC_ALLOC | SEC_LINKER_CREATED));
429 if (s == NULL)
430 return FALSE;
431
432 /* The .rel[a].bss section holds copy relocs. This section is not
433 normally needed. We need to create it here, though, so that the
434 linker will map it to an output section. We can't just create it
435 only if we need it, because we will not know whether we need it
436 until we have seen all the input files, and the first time the
437 main linker code calls BFD after examining all the input files
438 (size_dynamic_sections) the input sections have already been
439 mapped to the output sections. If the section turns out not to
440 be needed, we can discard it later. We will never need this
441 section when generating a shared object, since they do not use
442 copy relocs. */
443 if (! bfd_link_pic (info))
444 {
445 s = bfd_make_section_anyway_with_flags (abfd,
446 (bed->rela_plts_and_copies_p
447 ? ".rela.bss" : ".rel.bss"),
448 flags | SEC_READONLY);
449 if (s == NULL
450 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
451 return FALSE;
452 }
453 }
454
455 return TRUE;
456 }
457
458 /* Record a new dynamic symbol. We record the dynamic symbols as we
460 read the input files, since we need to have a list of all of them
461 before we can determine the final sizes of the output sections.
462 Note that we may actually call this function even though we are not
463 going to output any dynamic symbols; in some cases we know that a
464 symbol should be in the dynamic symbol table, but only if there is
465 one. */
466
467 bfd_boolean
468 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
469 struct elf_link_hash_entry *h)
470 {
471 if (h->dynindx == -1)
472 {
473 struct elf_strtab_hash *dynstr;
474 char *p;
475 const char *name;
476 size_t indx;
477
478 /* XXX: The ABI draft says the linker must turn hidden and
479 internal symbols into STB_LOCAL symbols when producing the
480 DSO. However, if ld.so honors st_other in the dynamic table,
481 this would not be necessary. */
482 switch (ELF_ST_VISIBILITY (h->other))
483 {
484 case STV_INTERNAL:
485 case STV_HIDDEN:
486 if (h->root.type != bfd_link_hash_undefined
487 && h->root.type != bfd_link_hash_undefweak)
488 {
489 h->forced_local = 1;
490 if (!elf_hash_table (info)->is_relocatable_executable)
491 return TRUE;
492 }
493
494 default:
495 break;
496 }
497
498 h->dynindx = elf_hash_table (info)->dynsymcount;
499 ++elf_hash_table (info)->dynsymcount;
500
501 dynstr = elf_hash_table (info)->dynstr;
502 if (dynstr == NULL)
503 {
504 /* Create a strtab to hold the dynamic symbol names. */
505 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
506 if (dynstr == NULL)
507 return FALSE;
508 }
509
510 /* We don't put any version information in the dynamic string
511 table. */
512 name = h->root.root.string;
513 p = strchr (name, ELF_VER_CHR);
514 if (p != NULL)
515 /* We know that the p points into writable memory. In fact,
516 there are only a few symbols that have read-only names, being
517 those like _GLOBAL_OFFSET_TABLE_ that are created specially
518 by the backends. Most symbols will have names pointing into
519 an ELF string table read from a file, or to objalloc memory. */
520 *p = 0;
521
522 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
523
524 if (p != NULL)
525 *p = ELF_VER_CHR;
526
527 if (indx == (size_t) -1)
528 return FALSE;
529 h->dynstr_index = indx;
530 }
531
532 return TRUE;
533 }
534
535 /* Mark a symbol dynamic. */
537
538 static void
539 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
540 struct elf_link_hash_entry *h,
541 Elf_Internal_Sym *sym)
542 {
543 struct bfd_elf_dynamic_list *d = info->dynamic_list;
544
545 /* It may be called more than once on the same H. */
546 if(h->dynamic || bfd_link_relocatable (info))
547 return;
548
549 if ((info->dynamic_data
550 && (h->type == STT_OBJECT
551 || h->type == STT_COMMON
552 || (sym != NULL
553 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
554 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
555 || (d != NULL
556 && h->root.type == bfd_link_hash_new
557 && (*d->match) (&d->head, NULL, h->root.root.string)))
558 h->dynamic = 1;
559 }
560
561 /* Record an assignment to a symbol made by a linker script. We need
562 this in case some dynamic object refers to this symbol. */
563
564 bfd_boolean
565 bfd_elf_record_link_assignment (bfd *output_bfd,
566 struct bfd_link_info *info,
567 const char *name,
568 bfd_boolean provide,
569 bfd_boolean hidden)
570 {
571 struct elf_link_hash_entry *h, *hv;
572 struct elf_link_hash_table *htab;
573 const struct elf_backend_data *bed;
574
575 if (!is_elf_hash_table (info->hash))
576 return TRUE;
577
578 htab = elf_hash_table (info);
579 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
580 if (h == NULL)
581 return provide;
582
583 if (h->versioned == unknown)
584 {
585 /* Set versioned if symbol version is unknown. */
586 char *version = strrchr (name, ELF_VER_CHR);
587 if (version)
588 {
589 if (version > name && version[-1] != ELF_VER_CHR)
590 h->versioned = versioned_hidden;
591 else
592 h->versioned = versioned;
593 }
594 }
595
596 switch (h->root.type)
597 {
598 case bfd_link_hash_defined:
599 case bfd_link_hash_defweak:
600 case bfd_link_hash_common:
601 break;
602 case bfd_link_hash_undefweak:
603 case bfd_link_hash_undefined:
604 /* Since we're defining the symbol, don't let it seem to have not
605 been defined. record_dynamic_symbol and size_dynamic_sections
606 may depend on this. */
607 h->root.type = bfd_link_hash_new;
608 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
609 bfd_link_repair_undef_list (&htab->root);
610 break;
611 case bfd_link_hash_new:
612 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
613 h->non_elf = 0;
614 break;
615 case bfd_link_hash_indirect:
616 /* We had a versioned symbol in a dynamic library. We make the
617 the versioned symbol point to this one. */
618 bed = get_elf_backend_data (output_bfd);
619 hv = h;
620 while (hv->root.type == bfd_link_hash_indirect
621 || hv->root.type == bfd_link_hash_warning)
622 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
623 /* We don't need to update h->root.u since linker will set them
624 later. */
625 h->root.type = bfd_link_hash_undefined;
626 hv->root.type = bfd_link_hash_indirect;
627 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
628 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
629 break;
630 case bfd_link_hash_warning:
631 abort ();
632 break;
633 }
634
635 /* If this symbol is being provided by the linker script, and it is
636 currently defined by a dynamic object, but not by a regular
637 object, then mark it as undefined so that the generic linker will
638 force the correct value. */
639 if (provide
640 && h->def_dynamic
641 && !h->def_regular)
642 h->root.type = bfd_link_hash_undefined;
643
644 /* If this symbol is not being provided by the linker script, and it is
645 currently defined by a dynamic object, but not by a regular object,
646 then clear out any version information because the symbol will not be
647 associated with the dynamic object any more. */
648 if (!provide
649 && h->def_dynamic
650 && !h->def_regular)
651 h->verinfo.verdef = NULL;
652
653 h->def_regular = 1;
654
655 if (hidden)
656 {
657 bed = get_elf_backend_data (output_bfd);
658 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
659 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
660 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
661 }
662
663 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
664 and executables. */
665 if (!bfd_link_relocatable (info)
666 && h->dynindx != -1
667 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
668 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
669 h->forced_local = 1;
670
671 if ((h->def_dynamic
672 || h->ref_dynamic
673 || bfd_link_dll (info)
674 || elf_hash_table (info)->is_relocatable_executable)
675 && h->dynindx == -1)
676 {
677 if (! bfd_elf_link_record_dynamic_symbol (info, h))
678 return FALSE;
679
680 /* If this is a weak defined symbol, and we know a corresponding
681 real symbol from the same dynamic object, make sure the real
682 symbol is also made into a dynamic symbol. */
683 if (h->u.weakdef != NULL
684 && h->u.weakdef->dynindx == -1)
685 {
686 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
687 return FALSE;
688 }
689 }
690
691 return TRUE;
692 }
693
694 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
695 success, and 2 on a failure caused by attempting to record a symbol
696 in a discarded section, eg. a discarded link-once section symbol. */
697
698 int
699 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
700 bfd *input_bfd,
701 long input_indx)
702 {
703 bfd_size_type amt;
704 struct elf_link_local_dynamic_entry *entry;
705 struct elf_link_hash_table *eht;
706 struct elf_strtab_hash *dynstr;
707 size_t dynstr_index;
708 char *name;
709 Elf_External_Sym_Shndx eshndx;
710 char esym[sizeof (Elf64_External_Sym)];
711
712 if (! is_elf_hash_table (info->hash))
713 return 0;
714
715 /* See if the entry exists already. */
716 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
717 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
718 return 1;
719
720 amt = sizeof (*entry);
721 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
722 if (entry == NULL)
723 return 0;
724
725 /* Go find the symbol, so that we can find it's name. */
726 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
727 1, input_indx, &entry->isym, esym, &eshndx))
728 {
729 bfd_release (input_bfd, entry);
730 return 0;
731 }
732
733 if (entry->isym.st_shndx != SHN_UNDEF
734 && entry->isym.st_shndx < SHN_LORESERVE)
735 {
736 asection *s;
737
738 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
739 if (s == NULL || bfd_is_abs_section (s->output_section))
740 {
741 /* We can still bfd_release here as nothing has done another
742 bfd_alloc. We can't do this later in this function. */
743 bfd_release (input_bfd, entry);
744 return 2;
745 }
746 }
747
748 name = (bfd_elf_string_from_elf_section
749 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
750 entry->isym.st_name));
751
752 dynstr = elf_hash_table (info)->dynstr;
753 if (dynstr == NULL)
754 {
755 /* Create a strtab to hold the dynamic symbol names. */
756 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
757 if (dynstr == NULL)
758 return 0;
759 }
760
761 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
762 if (dynstr_index == (size_t) -1)
763 return 0;
764 entry->isym.st_name = dynstr_index;
765
766 eht = elf_hash_table (info);
767
768 entry->next = eht->dynlocal;
769 eht->dynlocal = entry;
770 entry->input_bfd = input_bfd;
771 entry->input_indx = input_indx;
772 eht->dynsymcount++;
773
774 /* Whatever binding the symbol had before, it's now local. */
775 entry->isym.st_info
776 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
777
778 /* The dynindx will be set at the end of size_dynamic_sections. */
779
780 return 1;
781 }
782
783 /* Return the dynindex of a local dynamic symbol. */
784
785 long
786 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
787 bfd *input_bfd,
788 long input_indx)
789 {
790 struct elf_link_local_dynamic_entry *e;
791
792 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
793 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
794 return e->dynindx;
795 return -1;
796 }
797
798 /* This function is used to renumber the dynamic symbols, if some of
799 them are removed because they are marked as local. This is called
800 via elf_link_hash_traverse. */
801
802 static bfd_boolean
803 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
804 void *data)
805 {
806 size_t *count = (size_t *) data;
807
808 if (h->forced_local)
809 return TRUE;
810
811 if (h->dynindx != -1)
812 h->dynindx = ++(*count);
813
814 return TRUE;
815 }
816
817
818 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
819 STB_LOCAL binding. */
820
821 static bfd_boolean
822 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
823 void *data)
824 {
825 size_t *count = (size_t *) data;
826
827 if (!h->forced_local)
828 return TRUE;
829
830 if (h->dynindx != -1)
831 h->dynindx = ++(*count);
832
833 return TRUE;
834 }
835
836 /* Return true if the dynamic symbol for a given section should be
837 omitted when creating a shared library. */
838 bfd_boolean
839 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
840 struct bfd_link_info *info,
841 asection *p)
842 {
843 struct elf_link_hash_table *htab;
844 asection *ip;
845
846 switch (elf_section_data (p)->this_hdr.sh_type)
847 {
848 case SHT_PROGBITS:
849 case SHT_NOBITS:
850 /* If sh_type is yet undecided, assume it could be
851 SHT_PROGBITS/SHT_NOBITS. */
852 case SHT_NULL:
853 htab = elf_hash_table (info);
854 if (p == htab->tls_sec)
855 return FALSE;
856
857 if (htab->text_index_section != NULL)
858 return p != htab->text_index_section && p != htab->data_index_section;
859
860 return (htab->dynobj != NULL
861 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
862 && ip->output_section == p);
863
864 /* There shouldn't be section relative relocations
865 against any other section. */
866 default:
867 return TRUE;
868 }
869 }
870
871 /* Assign dynsym indices. In a shared library we generate a section
872 symbol for each output section, which come first. Next come symbols
873 which have been forced to local binding. Then all of the back-end
874 allocated local dynamic syms, followed by the rest of the global
875 symbols. */
876
877 static unsigned long
878 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
879 struct bfd_link_info *info,
880 unsigned long *section_sym_count)
881 {
882 unsigned long dynsymcount = 0;
883
884 if (bfd_link_pic (info)
885 || elf_hash_table (info)->is_relocatable_executable)
886 {
887 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
888 asection *p;
889 for (p = output_bfd->sections; p ; p = p->next)
890 if ((p->flags & SEC_EXCLUDE) == 0
891 && (p->flags & SEC_ALLOC) != 0
892 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
893 elf_section_data (p)->dynindx = ++dynsymcount;
894 else
895 elf_section_data (p)->dynindx = 0;
896 }
897 *section_sym_count = dynsymcount;
898
899 elf_link_hash_traverse (elf_hash_table (info),
900 elf_link_renumber_local_hash_table_dynsyms,
901 &dynsymcount);
902
903 if (elf_hash_table (info)->dynlocal)
904 {
905 struct elf_link_local_dynamic_entry *p;
906 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
907 p->dynindx = ++dynsymcount;
908 }
909
910 elf_link_hash_traverse (elf_hash_table (info),
911 elf_link_renumber_hash_table_dynsyms,
912 &dynsymcount);
913
914 /* There is an unused NULL entry at the head of the table which we
915 must account for in our count even if the table is empty since it
916 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
917 .dynamic section. */
918 dynsymcount++;
919
920 elf_hash_table (info)->dynsymcount = dynsymcount;
921 return dynsymcount;
922 }
923
924 /* Merge st_other field. */
925
926 static void
927 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
928 const Elf_Internal_Sym *isym, asection *sec,
929 bfd_boolean definition, bfd_boolean dynamic)
930 {
931 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
932
933 /* If st_other has a processor-specific meaning, specific
934 code might be needed here. */
935 if (bed->elf_backend_merge_symbol_attribute)
936 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
937 dynamic);
938
939 if (!dynamic)
940 {
941 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
942 unsigned hvis = ELF_ST_VISIBILITY (h->other);
943
944 /* Keep the most constraining visibility. Leave the remainder
945 of the st_other field to elf_backend_merge_symbol_attribute. */
946 if (symvis - 1 < hvis - 1)
947 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
948 }
949 else if (definition
950 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
951 && (sec->flags & SEC_READONLY) == 0)
952 h->protected_def = 1;
953 }
954
955 /* This function is called when we want to merge a new symbol with an
956 existing symbol. It handles the various cases which arise when we
957 find a definition in a dynamic object, or when there is already a
958 definition in a dynamic object. The new symbol is described by
959 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
960 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
961 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
962 of an old common symbol. We set OVERRIDE if the old symbol is
963 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
964 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
965 to change. By OK to change, we mean that we shouldn't warn if the
966 type or size does change. */
967
968 static bfd_boolean
969 _bfd_elf_merge_symbol (bfd *abfd,
970 struct bfd_link_info *info,
971 const char *name,
972 Elf_Internal_Sym *sym,
973 asection **psec,
974 bfd_vma *pvalue,
975 struct elf_link_hash_entry **sym_hash,
976 bfd **poldbfd,
977 bfd_boolean *pold_weak,
978 unsigned int *pold_alignment,
979 bfd_boolean *skip,
980 bfd_boolean *override,
981 bfd_boolean *type_change_ok,
982 bfd_boolean *size_change_ok,
983 bfd_boolean *matched)
984 {
985 asection *sec, *oldsec;
986 struct elf_link_hash_entry *h;
987 struct elf_link_hash_entry *hi;
988 struct elf_link_hash_entry *flip;
989 int bind;
990 bfd *oldbfd;
991 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
992 bfd_boolean newweak, oldweak, newfunc, oldfunc;
993 const struct elf_backend_data *bed;
994 char *new_version;
995
996 *skip = FALSE;
997 *override = FALSE;
998
999 sec = *psec;
1000 bind = ELF_ST_BIND (sym->st_info);
1001
1002 if (! bfd_is_und_section (sec))
1003 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1004 else
1005 h = ((struct elf_link_hash_entry *)
1006 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1007 if (h == NULL)
1008 return FALSE;
1009 *sym_hash = h;
1010
1011 bed = get_elf_backend_data (abfd);
1012
1013 /* NEW_VERSION is the symbol version of the new symbol. */
1014 if (h->versioned != unversioned)
1015 {
1016 /* Symbol version is unknown or versioned. */
1017 new_version = strrchr (name, ELF_VER_CHR);
1018 if (new_version)
1019 {
1020 if (h->versioned == unknown)
1021 {
1022 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1023 h->versioned = versioned_hidden;
1024 else
1025 h->versioned = versioned;
1026 }
1027 new_version += 1;
1028 if (new_version[0] == '\0')
1029 new_version = NULL;
1030 }
1031 else
1032 h->versioned = unversioned;
1033 }
1034 else
1035 new_version = NULL;
1036
1037 /* For merging, we only care about real symbols. But we need to make
1038 sure that indirect symbol dynamic flags are updated. */
1039 hi = h;
1040 while (h->root.type == bfd_link_hash_indirect
1041 || h->root.type == bfd_link_hash_warning)
1042 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1043
1044 if (!*matched)
1045 {
1046 if (hi == h || h->root.type == bfd_link_hash_new)
1047 *matched = TRUE;
1048 else
1049 {
1050 /* OLD_HIDDEN is true if the existing symbol is only visible
1051 to the symbol with the same symbol version. NEW_HIDDEN is
1052 true if the new symbol is only visible to the symbol with
1053 the same symbol version. */
1054 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1055 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1056 if (!old_hidden && !new_hidden)
1057 /* The new symbol matches the existing symbol if both
1058 aren't hidden. */
1059 *matched = TRUE;
1060 else
1061 {
1062 /* OLD_VERSION is the symbol version of the existing
1063 symbol. */
1064 char *old_version;
1065
1066 if (h->versioned >= versioned)
1067 old_version = strrchr (h->root.root.string,
1068 ELF_VER_CHR) + 1;
1069 else
1070 old_version = NULL;
1071
1072 /* The new symbol matches the existing symbol if they
1073 have the same symbol version. */
1074 *matched = (old_version == new_version
1075 || (old_version != NULL
1076 && new_version != NULL
1077 && strcmp (old_version, new_version) == 0));
1078 }
1079 }
1080 }
1081
1082 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1083 existing symbol. */
1084
1085 oldbfd = NULL;
1086 oldsec = NULL;
1087 switch (h->root.type)
1088 {
1089 default:
1090 break;
1091
1092 case bfd_link_hash_undefined:
1093 case bfd_link_hash_undefweak:
1094 oldbfd = h->root.u.undef.abfd;
1095 break;
1096
1097 case bfd_link_hash_defined:
1098 case bfd_link_hash_defweak:
1099 oldbfd = h->root.u.def.section->owner;
1100 oldsec = h->root.u.def.section;
1101 break;
1102
1103 case bfd_link_hash_common:
1104 oldbfd = h->root.u.c.p->section->owner;
1105 oldsec = h->root.u.c.p->section;
1106 if (pold_alignment)
1107 *pold_alignment = h->root.u.c.p->alignment_power;
1108 break;
1109 }
1110 if (poldbfd && *poldbfd == NULL)
1111 *poldbfd = oldbfd;
1112
1113 /* Differentiate strong and weak symbols. */
1114 newweak = bind == STB_WEAK;
1115 oldweak = (h->root.type == bfd_link_hash_defweak
1116 || h->root.type == bfd_link_hash_undefweak);
1117 if (pold_weak)
1118 *pold_weak = oldweak;
1119
1120 /* This code is for coping with dynamic objects, and is only useful
1121 if we are doing an ELF link. */
1122 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1123 return TRUE;
1124
1125 /* We have to check it for every instance since the first few may be
1126 references and not all compilers emit symbol type for undefined
1127 symbols. */
1128 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1129
1130 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1131 respectively, is from a dynamic object. */
1132
1133 newdyn = (abfd->flags & DYNAMIC) != 0;
1134
1135 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1136 syms and defined syms in dynamic libraries respectively.
1137 ref_dynamic on the other hand can be set for a symbol defined in
1138 a dynamic library, and def_dynamic may not be set; When the
1139 definition in a dynamic lib is overridden by a definition in the
1140 executable use of the symbol in the dynamic lib becomes a
1141 reference to the executable symbol. */
1142 if (newdyn)
1143 {
1144 if (bfd_is_und_section (sec))
1145 {
1146 if (bind != STB_WEAK)
1147 {
1148 h->ref_dynamic_nonweak = 1;
1149 hi->ref_dynamic_nonweak = 1;
1150 }
1151 }
1152 else
1153 {
1154 /* Update the existing symbol only if they match. */
1155 if (*matched)
1156 h->dynamic_def = 1;
1157 hi->dynamic_def = 1;
1158 }
1159 }
1160
1161 /* If we just created the symbol, mark it as being an ELF symbol.
1162 Other than that, there is nothing to do--there is no merge issue
1163 with a newly defined symbol--so we just return. */
1164
1165 if (h->root.type == bfd_link_hash_new)
1166 {
1167 h->non_elf = 0;
1168 return TRUE;
1169 }
1170
1171 /* In cases involving weak versioned symbols, we may wind up trying
1172 to merge a symbol with itself. Catch that here, to avoid the
1173 confusion that results if we try to override a symbol with
1174 itself. The additional tests catch cases like
1175 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1176 dynamic object, which we do want to handle here. */
1177 if (abfd == oldbfd
1178 && (newweak || oldweak)
1179 && ((abfd->flags & DYNAMIC) == 0
1180 || !h->def_regular))
1181 return TRUE;
1182
1183 olddyn = FALSE;
1184 if (oldbfd != NULL)
1185 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1186 else if (oldsec != NULL)
1187 {
1188 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1189 indices used by MIPS ELF. */
1190 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1191 }
1192
1193 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1194 respectively, appear to be a definition rather than reference. */
1195
1196 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1197
1198 olddef = (h->root.type != bfd_link_hash_undefined
1199 && h->root.type != bfd_link_hash_undefweak
1200 && h->root.type != bfd_link_hash_common);
1201
1202 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1203 respectively, appear to be a function. */
1204
1205 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1206 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1207
1208 oldfunc = (h->type != STT_NOTYPE
1209 && bed->is_function_type (h->type));
1210
1211 /* If creating a default indirect symbol ("foo" or "foo@") from a
1212 dynamic versioned definition ("foo@@") skip doing so if there is
1213 an existing regular definition with a different type. We don't
1214 want, for example, a "time" variable in the executable overriding
1215 a "time" function in a shared library. */
1216 if (pold_alignment == NULL
1217 && newdyn
1218 && newdef
1219 && !olddyn
1220 && (olddef || h->root.type == bfd_link_hash_common)
1221 && ELF_ST_TYPE (sym->st_info) != h->type
1222 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1223 && h->type != STT_NOTYPE
1224 && !(newfunc && oldfunc))
1225 {
1226 *skip = TRUE;
1227 return TRUE;
1228 }
1229
1230 /* Check TLS symbols. We don't check undefined symbols introduced
1231 by "ld -u" which have no type (and oldbfd NULL), and we don't
1232 check symbols from plugins because they also have no type. */
1233 if (oldbfd != NULL
1234 && (oldbfd->flags & BFD_PLUGIN) == 0
1235 && (abfd->flags & BFD_PLUGIN) == 0
1236 && ELF_ST_TYPE (sym->st_info) != h->type
1237 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1238 {
1239 bfd *ntbfd, *tbfd;
1240 bfd_boolean ntdef, tdef;
1241 asection *ntsec, *tsec;
1242
1243 if (h->type == STT_TLS)
1244 {
1245 ntbfd = abfd;
1246 ntsec = sec;
1247 ntdef = newdef;
1248 tbfd = oldbfd;
1249 tsec = oldsec;
1250 tdef = olddef;
1251 }
1252 else
1253 {
1254 ntbfd = oldbfd;
1255 ntsec = oldsec;
1256 ntdef = olddef;
1257 tbfd = abfd;
1258 tsec = sec;
1259 tdef = newdef;
1260 }
1261
1262 if (tdef && ntdef)
1263 (*_bfd_error_handler)
1264 (_("%s: TLS definition in %B section %A "
1265 "mismatches non-TLS definition in %B section %A"),
1266 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1267 else if (!tdef && !ntdef)
1268 (*_bfd_error_handler)
1269 (_("%s: TLS reference in %B "
1270 "mismatches non-TLS reference in %B"),
1271 tbfd, ntbfd, h->root.root.string);
1272 else if (tdef)
1273 (*_bfd_error_handler)
1274 (_("%s: TLS definition in %B section %A "
1275 "mismatches non-TLS reference in %B"),
1276 tbfd, tsec, ntbfd, h->root.root.string);
1277 else
1278 (*_bfd_error_handler)
1279 (_("%s: TLS reference in %B "
1280 "mismatches non-TLS definition in %B section %A"),
1281 tbfd, ntbfd, ntsec, h->root.root.string);
1282
1283 bfd_set_error (bfd_error_bad_value);
1284 return FALSE;
1285 }
1286
1287 /* If the old symbol has non-default visibility, we ignore the new
1288 definition from a dynamic object. */
1289 if (newdyn
1290 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1291 && !bfd_is_und_section (sec))
1292 {
1293 *skip = TRUE;
1294 /* Make sure this symbol is dynamic. */
1295 h->ref_dynamic = 1;
1296 hi->ref_dynamic = 1;
1297 /* A protected symbol has external availability. Make sure it is
1298 recorded as dynamic.
1299
1300 FIXME: Should we check type and size for protected symbol? */
1301 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1302 return bfd_elf_link_record_dynamic_symbol (info, h);
1303 else
1304 return TRUE;
1305 }
1306 else if (!newdyn
1307 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1308 && h->def_dynamic)
1309 {
1310 /* If the new symbol with non-default visibility comes from a
1311 relocatable file and the old definition comes from a dynamic
1312 object, we remove the old definition. */
1313 if (hi->root.type == bfd_link_hash_indirect)
1314 {
1315 /* Handle the case where the old dynamic definition is
1316 default versioned. We need to copy the symbol info from
1317 the symbol with default version to the normal one if it
1318 was referenced before. */
1319 if (h->ref_regular)
1320 {
1321 hi->root.type = h->root.type;
1322 h->root.type = bfd_link_hash_indirect;
1323 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1324
1325 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1326 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1327 {
1328 /* If the new symbol is hidden or internal, completely undo
1329 any dynamic link state. */
1330 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1331 h->forced_local = 0;
1332 h->ref_dynamic = 0;
1333 }
1334 else
1335 h->ref_dynamic = 1;
1336
1337 h->def_dynamic = 0;
1338 /* FIXME: Should we check type and size for protected symbol? */
1339 h->size = 0;
1340 h->type = 0;
1341
1342 h = hi;
1343 }
1344 else
1345 h = hi;
1346 }
1347
1348 /* If the old symbol was undefined before, then it will still be
1349 on the undefs list. If the new symbol is undefined or
1350 common, we can't make it bfd_link_hash_new here, because new
1351 undefined or common symbols will be added to the undefs list
1352 by _bfd_generic_link_add_one_symbol. Symbols may not be
1353 added twice to the undefs list. Also, if the new symbol is
1354 undefweak then we don't want to lose the strong undef. */
1355 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1356 {
1357 h->root.type = bfd_link_hash_undefined;
1358 h->root.u.undef.abfd = abfd;
1359 }
1360 else
1361 {
1362 h->root.type = bfd_link_hash_new;
1363 h->root.u.undef.abfd = NULL;
1364 }
1365
1366 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1367 {
1368 /* If the new symbol is hidden or internal, completely undo
1369 any dynamic link state. */
1370 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1371 h->forced_local = 0;
1372 h->ref_dynamic = 0;
1373 }
1374 else
1375 h->ref_dynamic = 1;
1376 h->def_dynamic = 0;
1377 /* FIXME: Should we check type and size for protected symbol? */
1378 h->size = 0;
1379 h->type = 0;
1380 return TRUE;
1381 }
1382
1383 /* If a new weak symbol definition comes from a regular file and the
1384 old symbol comes from a dynamic library, we treat the new one as
1385 strong. Similarly, an old weak symbol definition from a regular
1386 file is treated as strong when the new symbol comes from a dynamic
1387 library. Further, an old weak symbol from a dynamic library is
1388 treated as strong if the new symbol is from a dynamic library.
1389 This reflects the way glibc's ld.so works.
1390
1391 Do this before setting *type_change_ok or *size_change_ok so that
1392 we warn properly when dynamic library symbols are overridden. */
1393
1394 if (newdef && !newdyn && olddyn)
1395 newweak = FALSE;
1396 if (olddef && newdyn)
1397 oldweak = FALSE;
1398
1399 /* Allow changes between different types of function symbol. */
1400 if (newfunc && oldfunc)
1401 *type_change_ok = TRUE;
1402
1403 /* It's OK to change the type if either the existing symbol or the
1404 new symbol is weak. A type change is also OK if the old symbol
1405 is undefined and the new symbol is defined. */
1406
1407 if (oldweak
1408 || newweak
1409 || (newdef
1410 && h->root.type == bfd_link_hash_undefined))
1411 *type_change_ok = TRUE;
1412
1413 /* It's OK to change the size if either the existing symbol or the
1414 new symbol is weak, or if the old symbol is undefined. */
1415
1416 if (*type_change_ok
1417 || h->root.type == bfd_link_hash_undefined)
1418 *size_change_ok = TRUE;
1419
1420 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1421 symbol, respectively, appears to be a common symbol in a dynamic
1422 object. If a symbol appears in an uninitialized section, and is
1423 not weak, and is not a function, then it may be a common symbol
1424 which was resolved when the dynamic object was created. We want
1425 to treat such symbols specially, because they raise special
1426 considerations when setting the symbol size: if the symbol
1427 appears as a common symbol in a regular object, and the size in
1428 the regular object is larger, we must make sure that we use the
1429 larger size. This problematic case can always be avoided in C,
1430 but it must be handled correctly when using Fortran shared
1431 libraries.
1432
1433 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1434 likewise for OLDDYNCOMMON and OLDDEF.
1435
1436 Note that this test is just a heuristic, and that it is quite
1437 possible to have an uninitialized symbol in a shared object which
1438 is really a definition, rather than a common symbol. This could
1439 lead to some minor confusion when the symbol really is a common
1440 symbol in some regular object. However, I think it will be
1441 harmless. */
1442
1443 if (newdyn
1444 && newdef
1445 && !newweak
1446 && (sec->flags & SEC_ALLOC) != 0
1447 && (sec->flags & SEC_LOAD) == 0
1448 && sym->st_size > 0
1449 && !newfunc)
1450 newdyncommon = TRUE;
1451 else
1452 newdyncommon = FALSE;
1453
1454 if (olddyn
1455 && olddef
1456 && h->root.type == bfd_link_hash_defined
1457 && h->def_dynamic
1458 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1459 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1460 && h->size > 0
1461 && !oldfunc)
1462 olddyncommon = TRUE;
1463 else
1464 olddyncommon = FALSE;
1465
1466 /* We now know everything about the old and new symbols. We ask the
1467 backend to check if we can merge them. */
1468 if (bed->merge_symbol != NULL)
1469 {
1470 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1471 return FALSE;
1472 sec = *psec;
1473 }
1474
1475 /* If both the old and the new symbols look like common symbols in a
1476 dynamic object, set the size of the symbol to the larger of the
1477 two. */
1478
1479 if (olddyncommon
1480 && newdyncommon
1481 && sym->st_size != h->size)
1482 {
1483 /* Since we think we have two common symbols, issue a multiple
1484 common warning if desired. Note that we only warn if the
1485 size is different. If the size is the same, we simply let
1486 the old symbol override the new one as normally happens with
1487 symbols defined in dynamic objects. */
1488
1489 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1490 bfd_link_hash_common, sym->st_size);
1491 if (sym->st_size > h->size)
1492 h->size = sym->st_size;
1493
1494 *size_change_ok = TRUE;
1495 }
1496
1497 /* If we are looking at a dynamic object, and we have found a
1498 definition, we need to see if the symbol was already defined by
1499 some other object. If so, we want to use the existing
1500 definition, and we do not want to report a multiple symbol
1501 definition error; we do this by clobbering *PSEC to be
1502 bfd_und_section_ptr.
1503
1504 We treat a common symbol as a definition if the symbol in the
1505 shared library is a function, since common symbols always
1506 represent variables; this can cause confusion in principle, but
1507 any such confusion would seem to indicate an erroneous program or
1508 shared library. We also permit a common symbol in a regular
1509 object to override a weak symbol in a shared object. A common
1510 symbol in executable also overrides a symbol in a shared object. */
1511
1512 if (newdyn
1513 && newdef
1514 && (olddef
1515 || (h->root.type == bfd_link_hash_common
1516 && (newweak
1517 || newfunc
1518 || (!olddyn && bfd_link_executable (info))))))
1519 {
1520 *override = TRUE;
1521 newdef = FALSE;
1522 newdyncommon = FALSE;
1523
1524 *psec = sec = bfd_und_section_ptr;
1525 *size_change_ok = TRUE;
1526
1527 /* If we get here when the old symbol is a common symbol, then
1528 we are explicitly letting it override a weak symbol or
1529 function in a dynamic object, and we don't want to warn about
1530 a type change. If the old symbol is a defined symbol, a type
1531 change warning may still be appropriate. */
1532
1533 if (h->root.type == bfd_link_hash_common)
1534 *type_change_ok = TRUE;
1535 }
1536
1537 /* Handle the special case of an old common symbol merging with a
1538 new symbol which looks like a common symbol in a shared object.
1539 We change *PSEC and *PVALUE to make the new symbol look like a
1540 common symbol, and let _bfd_generic_link_add_one_symbol do the
1541 right thing. */
1542
1543 if (newdyncommon
1544 && h->root.type == bfd_link_hash_common)
1545 {
1546 *override = TRUE;
1547 newdef = FALSE;
1548 newdyncommon = FALSE;
1549 *pvalue = sym->st_size;
1550 *psec = sec = bed->common_section (oldsec);
1551 *size_change_ok = TRUE;
1552 }
1553
1554 /* Skip weak definitions of symbols that are already defined. */
1555 if (newdef && olddef && newweak)
1556 {
1557 /* Don't skip new non-IR weak syms. */
1558 if (!(oldbfd != NULL
1559 && (oldbfd->flags & BFD_PLUGIN) != 0
1560 && (abfd->flags & BFD_PLUGIN) == 0))
1561 {
1562 newdef = FALSE;
1563 *skip = TRUE;
1564 }
1565
1566 /* Merge st_other. If the symbol already has a dynamic index,
1567 but visibility says it should not be visible, turn it into a
1568 local symbol. */
1569 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1570 if (h->dynindx != -1)
1571 switch (ELF_ST_VISIBILITY (h->other))
1572 {
1573 case STV_INTERNAL:
1574 case STV_HIDDEN:
1575 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1576 break;
1577 }
1578 }
1579
1580 /* If the old symbol is from a dynamic object, and the new symbol is
1581 a definition which is not from a dynamic object, then the new
1582 symbol overrides the old symbol. Symbols from regular files
1583 always take precedence over symbols from dynamic objects, even if
1584 they are defined after the dynamic object in the link.
1585
1586 As above, we again permit a common symbol in a regular object to
1587 override a definition in a shared object if the shared object
1588 symbol is a function or is weak. */
1589
1590 flip = NULL;
1591 if (!newdyn
1592 && (newdef
1593 || (bfd_is_com_section (sec)
1594 && (oldweak || oldfunc)))
1595 && olddyn
1596 && olddef
1597 && h->def_dynamic)
1598 {
1599 /* Change the hash table entry to undefined, and let
1600 _bfd_generic_link_add_one_symbol do the right thing with the
1601 new definition. */
1602
1603 h->root.type = bfd_link_hash_undefined;
1604 h->root.u.undef.abfd = h->root.u.def.section->owner;
1605 *size_change_ok = TRUE;
1606
1607 olddef = FALSE;
1608 olddyncommon = FALSE;
1609
1610 /* We again permit a type change when a common symbol may be
1611 overriding a function. */
1612
1613 if (bfd_is_com_section (sec))
1614 {
1615 if (oldfunc)
1616 {
1617 /* If a common symbol overrides a function, make sure
1618 that it isn't defined dynamically nor has type
1619 function. */
1620 h->def_dynamic = 0;
1621 h->type = STT_NOTYPE;
1622 }
1623 *type_change_ok = TRUE;
1624 }
1625
1626 if (hi->root.type == bfd_link_hash_indirect)
1627 flip = hi;
1628 else
1629 /* This union may have been set to be non-NULL when this symbol
1630 was seen in a dynamic object. We must force the union to be
1631 NULL, so that it is correct for a regular symbol. */
1632 h->verinfo.vertree = NULL;
1633 }
1634
1635 /* Handle the special case of a new common symbol merging with an
1636 old symbol that looks like it might be a common symbol defined in
1637 a shared object. Note that we have already handled the case in
1638 which a new common symbol should simply override the definition
1639 in the shared library. */
1640
1641 if (! newdyn
1642 && bfd_is_com_section (sec)
1643 && olddyncommon)
1644 {
1645 /* It would be best if we could set the hash table entry to a
1646 common symbol, but we don't know what to use for the section
1647 or the alignment. */
1648 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1649 bfd_link_hash_common, sym->st_size);
1650
1651 /* If the presumed common symbol in the dynamic object is
1652 larger, pretend that the new symbol has its size. */
1653
1654 if (h->size > *pvalue)
1655 *pvalue = h->size;
1656
1657 /* We need to remember the alignment required by the symbol
1658 in the dynamic object. */
1659 BFD_ASSERT (pold_alignment);
1660 *pold_alignment = h->root.u.def.section->alignment_power;
1661
1662 olddef = FALSE;
1663 olddyncommon = FALSE;
1664
1665 h->root.type = bfd_link_hash_undefined;
1666 h->root.u.undef.abfd = h->root.u.def.section->owner;
1667
1668 *size_change_ok = TRUE;
1669 *type_change_ok = TRUE;
1670
1671 if (hi->root.type == bfd_link_hash_indirect)
1672 flip = hi;
1673 else
1674 h->verinfo.vertree = NULL;
1675 }
1676
1677 if (flip != NULL)
1678 {
1679 /* Handle the case where we had a versioned symbol in a dynamic
1680 library and now find a definition in a normal object. In this
1681 case, we make the versioned symbol point to the normal one. */
1682 flip->root.type = h->root.type;
1683 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1684 h->root.type = bfd_link_hash_indirect;
1685 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1686 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1687 if (h->def_dynamic)
1688 {
1689 h->def_dynamic = 0;
1690 flip->ref_dynamic = 1;
1691 }
1692 }
1693
1694 return TRUE;
1695 }
1696
1697 /* This function is called to create an indirect symbol from the
1698 default for the symbol with the default version if needed. The
1699 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1700 set DYNSYM if the new indirect symbol is dynamic. */
1701
1702 static bfd_boolean
1703 _bfd_elf_add_default_symbol (bfd *abfd,
1704 struct bfd_link_info *info,
1705 struct elf_link_hash_entry *h,
1706 const char *name,
1707 Elf_Internal_Sym *sym,
1708 asection *sec,
1709 bfd_vma value,
1710 bfd **poldbfd,
1711 bfd_boolean *dynsym)
1712 {
1713 bfd_boolean type_change_ok;
1714 bfd_boolean size_change_ok;
1715 bfd_boolean skip;
1716 char *shortname;
1717 struct elf_link_hash_entry *hi;
1718 struct bfd_link_hash_entry *bh;
1719 const struct elf_backend_data *bed;
1720 bfd_boolean collect;
1721 bfd_boolean dynamic;
1722 bfd_boolean override;
1723 char *p;
1724 size_t len, shortlen;
1725 asection *tmp_sec;
1726 bfd_boolean matched;
1727
1728 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1729 return TRUE;
1730
1731 /* If this symbol has a version, and it is the default version, we
1732 create an indirect symbol from the default name to the fully
1733 decorated name. This will cause external references which do not
1734 specify a version to be bound to this version of the symbol. */
1735 p = strchr (name, ELF_VER_CHR);
1736 if (h->versioned == unknown)
1737 {
1738 if (p == NULL)
1739 {
1740 h->versioned = unversioned;
1741 return TRUE;
1742 }
1743 else
1744 {
1745 if (p[1] != ELF_VER_CHR)
1746 {
1747 h->versioned = versioned_hidden;
1748 return TRUE;
1749 }
1750 else
1751 h->versioned = versioned;
1752 }
1753 }
1754 else
1755 {
1756 /* PR ld/19073: We may see an unversioned definition after the
1757 default version. */
1758 if (p == NULL)
1759 return TRUE;
1760 }
1761
1762 bed = get_elf_backend_data (abfd);
1763 collect = bed->collect;
1764 dynamic = (abfd->flags & DYNAMIC) != 0;
1765
1766 shortlen = p - name;
1767 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1768 if (shortname == NULL)
1769 return FALSE;
1770 memcpy (shortname, name, shortlen);
1771 shortname[shortlen] = '\0';
1772
1773 /* We are going to create a new symbol. Merge it with any existing
1774 symbol with this name. For the purposes of the merge, act as
1775 though we were defining the symbol we just defined, although we
1776 actually going to define an indirect symbol. */
1777 type_change_ok = FALSE;
1778 size_change_ok = FALSE;
1779 matched = TRUE;
1780 tmp_sec = sec;
1781 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1782 &hi, poldbfd, NULL, NULL, &skip, &override,
1783 &type_change_ok, &size_change_ok, &matched))
1784 return FALSE;
1785
1786 if (skip)
1787 goto nondefault;
1788
1789 if (hi->def_regular)
1790 {
1791 /* If the undecorated symbol will have a version added by a
1792 script different to H, then don't indirect to/from the
1793 undecorated symbol. This isn't ideal because we may not yet
1794 have seen symbol versions, if given by a script on the
1795 command line rather than via --version-script. */
1796 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1797 {
1798 bfd_boolean hide;
1799
1800 hi->verinfo.vertree
1801 = bfd_find_version_for_sym (info->version_info,
1802 hi->root.root.string, &hide);
1803 if (hi->verinfo.vertree != NULL && hide)
1804 {
1805 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1806 goto nondefault;
1807 }
1808 }
1809 if (hi->verinfo.vertree != NULL
1810 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1811 goto nondefault;
1812 }
1813
1814 if (! override)
1815 {
1816 /* Add the default symbol if not performing a relocatable link. */
1817 if (! bfd_link_relocatable (info))
1818 {
1819 bh = &hi->root;
1820 if (! (_bfd_generic_link_add_one_symbol
1821 (info, abfd, shortname, BSF_INDIRECT,
1822 bfd_ind_section_ptr,
1823 0, name, FALSE, collect, &bh)))
1824 return FALSE;
1825 hi = (struct elf_link_hash_entry *) bh;
1826 }
1827 }
1828 else
1829 {
1830 /* In this case the symbol named SHORTNAME is overriding the
1831 indirect symbol we want to add. We were planning on making
1832 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1833 is the name without a version. NAME is the fully versioned
1834 name, and it is the default version.
1835
1836 Overriding means that we already saw a definition for the
1837 symbol SHORTNAME in a regular object, and it is overriding
1838 the symbol defined in the dynamic object.
1839
1840 When this happens, we actually want to change NAME, the
1841 symbol we just added, to refer to SHORTNAME. This will cause
1842 references to NAME in the shared object to become references
1843 to SHORTNAME in the regular object. This is what we expect
1844 when we override a function in a shared object: that the
1845 references in the shared object will be mapped to the
1846 definition in the regular object. */
1847
1848 while (hi->root.type == bfd_link_hash_indirect
1849 || hi->root.type == bfd_link_hash_warning)
1850 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1851
1852 h->root.type = bfd_link_hash_indirect;
1853 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1854 if (h->def_dynamic)
1855 {
1856 h->def_dynamic = 0;
1857 hi->ref_dynamic = 1;
1858 if (hi->ref_regular
1859 || hi->def_regular)
1860 {
1861 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1862 return FALSE;
1863 }
1864 }
1865
1866 /* Now set HI to H, so that the following code will set the
1867 other fields correctly. */
1868 hi = h;
1869 }
1870
1871 /* Check if HI is a warning symbol. */
1872 if (hi->root.type == bfd_link_hash_warning)
1873 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1874
1875 /* If there is a duplicate definition somewhere, then HI may not
1876 point to an indirect symbol. We will have reported an error to
1877 the user in that case. */
1878
1879 if (hi->root.type == bfd_link_hash_indirect)
1880 {
1881 struct elf_link_hash_entry *ht;
1882
1883 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1884 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1885
1886 /* A reference to the SHORTNAME symbol from a dynamic library
1887 will be satisfied by the versioned symbol at runtime. In
1888 effect, we have a reference to the versioned symbol. */
1889 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1890 hi->dynamic_def |= ht->dynamic_def;
1891
1892 /* See if the new flags lead us to realize that the symbol must
1893 be dynamic. */
1894 if (! *dynsym)
1895 {
1896 if (! dynamic)
1897 {
1898 if (! bfd_link_executable (info)
1899 || hi->def_dynamic
1900 || hi->ref_dynamic)
1901 *dynsym = TRUE;
1902 }
1903 else
1904 {
1905 if (hi->ref_regular)
1906 *dynsym = TRUE;
1907 }
1908 }
1909 }
1910
1911 /* We also need to define an indirection from the nondefault version
1912 of the symbol. */
1913
1914 nondefault:
1915 len = strlen (name);
1916 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
1917 if (shortname == NULL)
1918 return FALSE;
1919 memcpy (shortname, name, shortlen);
1920 memcpy (shortname + shortlen, p + 1, len - shortlen);
1921
1922 /* Once again, merge with any existing symbol. */
1923 type_change_ok = FALSE;
1924 size_change_ok = FALSE;
1925 tmp_sec = sec;
1926 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1927 &hi, poldbfd, NULL, NULL, &skip, &override,
1928 &type_change_ok, &size_change_ok, &matched))
1929 return FALSE;
1930
1931 if (skip)
1932 return TRUE;
1933
1934 if (override)
1935 {
1936 /* Here SHORTNAME is a versioned name, so we don't expect to see
1937 the type of override we do in the case above unless it is
1938 overridden by a versioned definition. */
1939 if (hi->root.type != bfd_link_hash_defined
1940 && hi->root.type != bfd_link_hash_defweak)
1941 (*_bfd_error_handler)
1942 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1943 abfd, shortname);
1944 }
1945 else
1946 {
1947 bh = &hi->root;
1948 if (! (_bfd_generic_link_add_one_symbol
1949 (info, abfd, shortname, BSF_INDIRECT,
1950 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1951 return FALSE;
1952 hi = (struct elf_link_hash_entry *) bh;
1953
1954 /* If there is a duplicate definition somewhere, then HI may not
1955 point to an indirect symbol. We will have reported an error
1956 to the user in that case. */
1957
1958 if (hi->root.type == bfd_link_hash_indirect)
1959 {
1960 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1961 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1962 hi->dynamic_def |= h->dynamic_def;
1963
1964 /* See if the new flags lead us to realize that the symbol
1965 must be dynamic. */
1966 if (! *dynsym)
1967 {
1968 if (! dynamic)
1969 {
1970 if (! bfd_link_executable (info)
1971 || hi->ref_dynamic)
1972 *dynsym = TRUE;
1973 }
1974 else
1975 {
1976 if (hi->ref_regular)
1977 *dynsym = TRUE;
1978 }
1979 }
1980 }
1981 }
1982
1983 return TRUE;
1984 }
1985
1986 /* This routine is used to export all defined symbols into the dynamic
1988 symbol table. It is called via elf_link_hash_traverse. */
1989
1990 static bfd_boolean
1991 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1992 {
1993 struct elf_info_failed *eif = (struct elf_info_failed *) data;
1994
1995 /* Ignore indirect symbols. These are added by the versioning code. */
1996 if (h->root.type == bfd_link_hash_indirect)
1997 return TRUE;
1998
1999 /* Ignore this if we won't export it. */
2000 if (!eif->info->export_dynamic && !h->dynamic)
2001 return TRUE;
2002
2003 if (h->dynindx == -1
2004 && (h->def_regular || h->ref_regular)
2005 && ! bfd_hide_sym_by_version (eif->info->version_info,
2006 h->root.root.string))
2007 {
2008 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2009 {
2010 eif->failed = TRUE;
2011 return FALSE;
2012 }
2013 }
2014
2015 return TRUE;
2016 }
2017
2018 /* Look through the symbols which are defined in other shared
2020 libraries and referenced here. Update the list of version
2021 dependencies. This will be put into the .gnu.version_r section.
2022 This function is called via elf_link_hash_traverse. */
2023
2024 static bfd_boolean
2025 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2026 void *data)
2027 {
2028 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2029 Elf_Internal_Verneed *t;
2030 Elf_Internal_Vernaux *a;
2031 bfd_size_type amt;
2032
2033 /* We only care about symbols defined in shared objects with version
2034 information. */
2035 if (!h->def_dynamic
2036 || h->def_regular
2037 || h->dynindx == -1
2038 || h->verinfo.verdef == NULL
2039 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2040 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2041 return TRUE;
2042
2043 /* See if we already know about this version. */
2044 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2045 t != NULL;
2046 t = t->vn_nextref)
2047 {
2048 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2049 continue;
2050
2051 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2052 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2053 return TRUE;
2054
2055 break;
2056 }
2057
2058 /* This is a new version. Add it to tree we are building. */
2059
2060 if (t == NULL)
2061 {
2062 amt = sizeof *t;
2063 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2064 if (t == NULL)
2065 {
2066 rinfo->failed = TRUE;
2067 return FALSE;
2068 }
2069
2070 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2071 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2072 elf_tdata (rinfo->info->output_bfd)->verref = t;
2073 }
2074
2075 amt = sizeof *a;
2076 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2077 if (a == NULL)
2078 {
2079 rinfo->failed = TRUE;
2080 return FALSE;
2081 }
2082
2083 /* Note that we are copying a string pointer here, and testing it
2084 above. If bfd_elf_string_from_elf_section is ever changed to
2085 discard the string data when low in memory, this will have to be
2086 fixed. */
2087 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2088
2089 a->vna_flags = h->verinfo.verdef->vd_flags;
2090 a->vna_nextptr = t->vn_auxptr;
2091
2092 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2093 ++rinfo->vers;
2094
2095 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2096
2097 t->vn_auxptr = a;
2098
2099 return TRUE;
2100 }
2101
2102 /* Figure out appropriate versions for all the symbols. We may not
2103 have the version number script until we have read all of the input
2104 files, so until that point we don't know which symbols should be
2105 local. This function is called via elf_link_hash_traverse. */
2106
2107 static bfd_boolean
2108 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2109 {
2110 struct elf_info_failed *sinfo;
2111 struct bfd_link_info *info;
2112 const struct elf_backend_data *bed;
2113 struct elf_info_failed eif;
2114 char *p;
2115
2116 sinfo = (struct elf_info_failed *) data;
2117 info = sinfo->info;
2118
2119 /* Fix the symbol flags. */
2120 eif.failed = FALSE;
2121 eif.info = info;
2122 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2123 {
2124 if (eif.failed)
2125 sinfo->failed = TRUE;
2126 return FALSE;
2127 }
2128
2129 /* We only need version numbers for symbols defined in regular
2130 objects. */
2131 if (!h->def_regular)
2132 return TRUE;
2133
2134 bed = get_elf_backend_data (info->output_bfd);
2135 p = strchr (h->root.root.string, ELF_VER_CHR);
2136 if (p != NULL && h->verinfo.vertree == NULL)
2137 {
2138 struct bfd_elf_version_tree *t;
2139
2140 ++p;
2141 if (*p == ELF_VER_CHR)
2142 ++p;
2143
2144 /* If there is no version string, we can just return out. */
2145 if (*p == '\0')
2146 return TRUE;
2147
2148 /* Look for the version. If we find it, it is no longer weak. */
2149 for (t = sinfo->info->version_info; t != NULL; t = t->next)
2150 {
2151 if (strcmp (t->name, p) == 0)
2152 {
2153 size_t len;
2154 char *alc;
2155 struct bfd_elf_version_expr *d;
2156
2157 len = p - h->root.root.string;
2158 alc = (char *) bfd_malloc (len);
2159 if (alc == NULL)
2160 {
2161 sinfo->failed = TRUE;
2162 return FALSE;
2163 }
2164 memcpy (alc, h->root.root.string, len - 1);
2165 alc[len - 1] = '\0';
2166 if (alc[len - 2] == ELF_VER_CHR)
2167 alc[len - 2] = '\0';
2168
2169 h->verinfo.vertree = t;
2170 t->used = TRUE;
2171 d = NULL;
2172
2173 if (t->globals.list != NULL)
2174 d = (*t->match) (&t->globals, NULL, alc);
2175
2176 /* See if there is anything to force this symbol to
2177 local scope. */
2178 if (d == NULL && t->locals.list != NULL)
2179 {
2180 d = (*t->match) (&t->locals, NULL, alc);
2181 if (d != NULL
2182 && h->dynindx != -1
2183 && ! info->export_dynamic)
2184 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2185 }
2186
2187 free (alc);
2188 break;
2189 }
2190 }
2191
2192 /* If we are building an application, we need to create a
2193 version node for this version. */
2194 if (t == NULL && bfd_link_executable (info))
2195 {
2196 struct bfd_elf_version_tree **pp;
2197 int version_index;
2198
2199 /* If we aren't going to export this symbol, we don't need
2200 to worry about it. */
2201 if (h->dynindx == -1)
2202 return TRUE;
2203
2204 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2205 sizeof *t);
2206 if (t == NULL)
2207 {
2208 sinfo->failed = TRUE;
2209 return FALSE;
2210 }
2211
2212 t->name = p;
2213 t->name_indx = (unsigned int) -1;
2214 t->used = TRUE;
2215
2216 version_index = 1;
2217 /* Don't count anonymous version tag. */
2218 if (sinfo->info->version_info != NULL
2219 && sinfo->info->version_info->vernum == 0)
2220 version_index = 0;
2221 for (pp = &sinfo->info->version_info;
2222 *pp != NULL;
2223 pp = &(*pp)->next)
2224 ++version_index;
2225 t->vernum = version_index;
2226
2227 *pp = t;
2228
2229 h->verinfo.vertree = t;
2230 }
2231 else if (t == NULL)
2232 {
2233 /* We could not find the version for a symbol when
2234 generating a shared archive. Return an error. */
2235 (*_bfd_error_handler)
2236 (_("%B: version node not found for symbol %s"),
2237 info->output_bfd, h->root.root.string);
2238 bfd_set_error (bfd_error_bad_value);
2239 sinfo->failed = TRUE;
2240 return FALSE;
2241 }
2242 }
2243
2244 /* If we don't have a version for this symbol, see if we can find
2245 something. */
2246 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
2247 {
2248 bfd_boolean hide;
2249
2250 h->verinfo.vertree
2251 = bfd_find_version_for_sym (sinfo->info->version_info,
2252 h->root.root.string, &hide);
2253 if (h->verinfo.vertree != NULL && hide)
2254 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2255 }
2256
2257 return TRUE;
2258 }
2259
2260 /* Read and swap the relocs from the section indicated by SHDR. This
2262 may be either a REL or a RELA section. The relocations are
2263 translated into RELA relocations and stored in INTERNAL_RELOCS,
2264 which should have already been allocated to contain enough space.
2265 The EXTERNAL_RELOCS are a buffer where the external form of the
2266 relocations should be stored.
2267
2268 Returns FALSE if something goes wrong. */
2269
2270 static bfd_boolean
2271 elf_link_read_relocs_from_section (bfd *abfd,
2272 asection *sec,
2273 Elf_Internal_Shdr *shdr,
2274 void *external_relocs,
2275 Elf_Internal_Rela *internal_relocs)
2276 {
2277 const struct elf_backend_data *bed;
2278 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2279 const bfd_byte *erela;
2280 const bfd_byte *erelaend;
2281 Elf_Internal_Rela *irela;
2282 Elf_Internal_Shdr *symtab_hdr;
2283 size_t nsyms;
2284
2285 /* Position ourselves at the start of the section. */
2286 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2287 return FALSE;
2288
2289 /* Read the relocations. */
2290 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2291 return FALSE;
2292
2293 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2294 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2295
2296 bed = get_elf_backend_data (abfd);
2297
2298 /* Convert the external relocations to the internal format. */
2299 if (shdr->sh_entsize == bed->s->sizeof_rel)
2300 swap_in = bed->s->swap_reloc_in;
2301 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2302 swap_in = bed->s->swap_reloca_in;
2303 else
2304 {
2305 bfd_set_error (bfd_error_wrong_format);
2306 return FALSE;
2307 }
2308
2309 erela = (const bfd_byte *) external_relocs;
2310 erelaend = erela + shdr->sh_size;
2311 irela = internal_relocs;
2312 while (erela < erelaend)
2313 {
2314 bfd_vma r_symndx;
2315
2316 (*swap_in) (abfd, erela, irela);
2317 r_symndx = ELF32_R_SYM (irela->r_info);
2318 if (bed->s->arch_size == 64)
2319 r_symndx >>= 24;
2320 if (nsyms > 0)
2321 {
2322 if ((size_t) r_symndx >= nsyms)
2323 {
2324 (*_bfd_error_handler)
2325 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2326 " for offset 0x%lx in section `%A'"),
2327 abfd, sec,
2328 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2329 bfd_set_error (bfd_error_bad_value);
2330 return FALSE;
2331 }
2332 }
2333 else if (r_symndx != STN_UNDEF)
2334 {
2335 (*_bfd_error_handler)
2336 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2337 " when the object file has no symbol table"),
2338 abfd, sec,
2339 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2340 bfd_set_error (bfd_error_bad_value);
2341 return FALSE;
2342 }
2343 irela += bed->s->int_rels_per_ext_rel;
2344 erela += shdr->sh_entsize;
2345 }
2346
2347 return TRUE;
2348 }
2349
2350 /* Read and swap the relocs for a section O. They may have been
2351 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2352 not NULL, they are used as buffers to read into. They are known to
2353 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2354 the return value is allocated using either malloc or bfd_alloc,
2355 according to the KEEP_MEMORY argument. If O has two relocation
2356 sections (both REL and RELA relocations), then the REL_HDR
2357 relocations will appear first in INTERNAL_RELOCS, followed by the
2358 RELA_HDR relocations. */
2359
2360 Elf_Internal_Rela *
2361 _bfd_elf_link_read_relocs (bfd *abfd,
2362 asection *o,
2363 void *external_relocs,
2364 Elf_Internal_Rela *internal_relocs,
2365 bfd_boolean keep_memory)
2366 {
2367 void *alloc1 = NULL;
2368 Elf_Internal_Rela *alloc2 = NULL;
2369 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2370 struct bfd_elf_section_data *esdo = elf_section_data (o);
2371 Elf_Internal_Rela *internal_rela_relocs;
2372
2373 if (esdo->relocs != NULL)
2374 return esdo->relocs;
2375
2376 if (o->reloc_count == 0)
2377 return NULL;
2378
2379 if (internal_relocs == NULL)
2380 {
2381 bfd_size_type size;
2382
2383 size = o->reloc_count;
2384 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2385 if (keep_memory)
2386 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2387 else
2388 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2389 if (internal_relocs == NULL)
2390 goto error_return;
2391 }
2392
2393 if (external_relocs == NULL)
2394 {
2395 bfd_size_type size = 0;
2396
2397 if (esdo->rel.hdr)
2398 size += esdo->rel.hdr->sh_size;
2399 if (esdo->rela.hdr)
2400 size += esdo->rela.hdr->sh_size;
2401
2402 alloc1 = bfd_malloc (size);
2403 if (alloc1 == NULL)
2404 goto error_return;
2405 external_relocs = alloc1;
2406 }
2407
2408 internal_rela_relocs = internal_relocs;
2409 if (esdo->rel.hdr)
2410 {
2411 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2412 external_relocs,
2413 internal_relocs))
2414 goto error_return;
2415 external_relocs = (((bfd_byte *) external_relocs)
2416 + esdo->rel.hdr->sh_size);
2417 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2418 * bed->s->int_rels_per_ext_rel);
2419 }
2420
2421 if (esdo->rela.hdr
2422 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2423 external_relocs,
2424 internal_rela_relocs)))
2425 goto error_return;
2426
2427 /* Cache the results for next time, if we can. */
2428 if (keep_memory)
2429 esdo->relocs = internal_relocs;
2430
2431 if (alloc1 != NULL)
2432 free (alloc1);
2433
2434 /* Don't free alloc2, since if it was allocated we are passing it
2435 back (under the name of internal_relocs). */
2436
2437 return internal_relocs;
2438
2439 error_return:
2440 if (alloc1 != NULL)
2441 free (alloc1);
2442 if (alloc2 != NULL)
2443 {
2444 if (keep_memory)
2445 bfd_release (abfd, alloc2);
2446 else
2447 free (alloc2);
2448 }
2449 return NULL;
2450 }
2451
2452 /* Compute the size of, and allocate space for, REL_HDR which is the
2453 section header for a section containing relocations for O. */
2454
2455 static bfd_boolean
2456 _bfd_elf_link_size_reloc_section (bfd *abfd,
2457 struct bfd_elf_section_reloc_data *reldata)
2458 {
2459 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2460
2461 /* That allows us to calculate the size of the section. */
2462 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2463
2464 /* The contents field must last into write_object_contents, so we
2465 allocate it with bfd_alloc rather than malloc. Also since we
2466 cannot be sure that the contents will actually be filled in,
2467 we zero the allocated space. */
2468 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2469 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2470 return FALSE;
2471
2472 if (reldata->hashes == NULL && reldata->count)
2473 {
2474 struct elf_link_hash_entry **p;
2475
2476 p = ((struct elf_link_hash_entry **)
2477 bfd_zmalloc (reldata->count * sizeof (*p)));
2478 if (p == NULL)
2479 return FALSE;
2480
2481 reldata->hashes = p;
2482 }
2483
2484 return TRUE;
2485 }
2486
2487 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2488 originated from the section given by INPUT_REL_HDR) to the
2489 OUTPUT_BFD. */
2490
2491 bfd_boolean
2492 _bfd_elf_link_output_relocs (bfd *output_bfd,
2493 asection *input_section,
2494 Elf_Internal_Shdr *input_rel_hdr,
2495 Elf_Internal_Rela *internal_relocs,
2496 struct elf_link_hash_entry **rel_hash
2497 ATTRIBUTE_UNUSED)
2498 {
2499 Elf_Internal_Rela *irela;
2500 Elf_Internal_Rela *irelaend;
2501 bfd_byte *erel;
2502 struct bfd_elf_section_reloc_data *output_reldata;
2503 asection *output_section;
2504 const struct elf_backend_data *bed;
2505 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2506 struct bfd_elf_section_data *esdo;
2507
2508 output_section = input_section->output_section;
2509
2510 bed = get_elf_backend_data (output_bfd);
2511 esdo = elf_section_data (output_section);
2512 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2513 {
2514 output_reldata = &esdo->rel;
2515 swap_out = bed->s->swap_reloc_out;
2516 }
2517 else if (esdo->rela.hdr
2518 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2519 {
2520 output_reldata = &esdo->rela;
2521 swap_out = bed->s->swap_reloca_out;
2522 }
2523 else
2524 {
2525 (*_bfd_error_handler)
2526 (_("%B: relocation size mismatch in %B section %A"),
2527 output_bfd, input_section->owner, input_section);
2528 bfd_set_error (bfd_error_wrong_format);
2529 return FALSE;
2530 }
2531
2532 erel = output_reldata->hdr->contents;
2533 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2534 irela = internal_relocs;
2535 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2536 * bed->s->int_rels_per_ext_rel);
2537 while (irela < irelaend)
2538 {
2539 (*swap_out) (output_bfd, irela, erel);
2540 irela += bed->s->int_rels_per_ext_rel;
2541 erel += input_rel_hdr->sh_entsize;
2542 }
2543
2544 /* Bump the counter, so that we know where to add the next set of
2545 relocations. */
2546 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2547
2548 return TRUE;
2549 }
2550
2551 /* Make weak undefined symbols in PIE dynamic. */
2553
2554 bfd_boolean
2555 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2556 struct elf_link_hash_entry *h)
2557 {
2558 if (bfd_link_pie (info)
2559 && h->dynindx == -1
2560 && h->root.type == bfd_link_hash_undefweak)
2561 return bfd_elf_link_record_dynamic_symbol (info, h);
2562
2563 return TRUE;
2564 }
2565
2566 /* Fix up the flags for a symbol. This handles various cases which
2567 can only be fixed after all the input files are seen. This is
2568 currently called by both adjust_dynamic_symbol and
2569 assign_sym_version, which is unnecessary but perhaps more robust in
2570 the face of future changes. */
2571
2572 static bfd_boolean
2573 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2574 struct elf_info_failed *eif)
2575 {
2576 const struct elf_backend_data *bed;
2577
2578 /* If this symbol was mentioned in a non-ELF file, try to set
2579 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2580 permit a non-ELF file to correctly refer to a symbol defined in
2581 an ELF dynamic object. */
2582 if (h->non_elf)
2583 {
2584 while (h->root.type == bfd_link_hash_indirect)
2585 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2586
2587 if (h->root.type != bfd_link_hash_defined
2588 && h->root.type != bfd_link_hash_defweak)
2589 {
2590 h->ref_regular = 1;
2591 h->ref_regular_nonweak = 1;
2592 }
2593 else
2594 {
2595 if (h->root.u.def.section->owner != NULL
2596 && (bfd_get_flavour (h->root.u.def.section->owner)
2597 == bfd_target_elf_flavour))
2598 {
2599 h->ref_regular = 1;
2600 h->ref_regular_nonweak = 1;
2601 }
2602 else
2603 h->def_regular = 1;
2604 }
2605
2606 if (h->dynindx == -1
2607 && (h->def_dynamic
2608 || h->ref_dynamic))
2609 {
2610 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2611 {
2612 eif->failed = TRUE;
2613 return FALSE;
2614 }
2615 }
2616 }
2617 else
2618 {
2619 /* Unfortunately, NON_ELF is only correct if the symbol
2620 was first seen in a non-ELF file. Fortunately, if the symbol
2621 was first seen in an ELF file, we're probably OK unless the
2622 symbol was defined in a non-ELF file. Catch that case here.
2623 FIXME: We're still in trouble if the symbol was first seen in
2624 a dynamic object, and then later in a non-ELF regular object. */
2625 if ((h->root.type == bfd_link_hash_defined
2626 || h->root.type == bfd_link_hash_defweak)
2627 && !h->def_regular
2628 && (h->root.u.def.section->owner != NULL
2629 ? (bfd_get_flavour (h->root.u.def.section->owner)
2630 != bfd_target_elf_flavour)
2631 : (bfd_is_abs_section (h->root.u.def.section)
2632 && !h->def_dynamic)))
2633 h->def_regular = 1;
2634 }
2635
2636 /* Backend specific symbol fixup. */
2637 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2638 if (bed->elf_backend_fixup_symbol
2639 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2640 return FALSE;
2641
2642 /* If this is a final link, and the symbol was defined as a common
2643 symbol in a regular object file, and there was no definition in
2644 any dynamic object, then the linker will have allocated space for
2645 the symbol in a common section but the DEF_REGULAR
2646 flag will not have been set. */
2647 if (h->root.type == bfd_link_hash_defined
2648 && !h->def_regular
2649 && h->ref_regular
2650 && !h->def_dynamic
2651 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2652 h->def_regular = 1;
2653
2654 /* If -Bsymbolic was used (which means to bind references to global
2655 symbols to the definition within the shared object), and this
2656 symbol was defined in a regular object, then it actually doesn't
2657 need a PLT entry. Likewise, if the symbol has non-default
2658 visibility. If the symbol has hidden or internal visibility, we
2659 will force it local. */
2660 if (h->needs_plt
2661 && bfd_link_pic (eif->info)
2662 && is_elf_hash_table (eif->info->hash)
2663 && (SYMBOLIC_BIND (eif->info, h)
2664 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2665 && h->def_regular)
2666 {
2667 bfd_boolean force_local;
2668
2669 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2670 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2671 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2672 }
2673
2674 /* If a weak undefined symbol has non-default visibility, we also
2675 hide it from the dynamic linker. */
2676 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2677 && h->root.type == bfd_link_hash_undefweak)
2678 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2679
2680 /* If this is a weak defined symbol in a dynamic object, and we know
2681 the real definition in the dynamic object, copy interesting flags
2682 over to the real definition. */
2683 if (h->u.weakdef != NULL)
2684 {
2685 /* If the real definition is defined by a regular object file,
2686 don't do anything special. See the longer description in
2687 _bfd_elf_adjust_dynamic_symbol, below. */
2688 if (h->u.weakdef->def_regular)
2689 h->u.weakdef = NULL;
2690 else
2691 {
2692 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2693
2694 while (h->root.type == bfd_link_hash_indirect)
2695 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2696
2697 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2698 || h->root.type == bfd_link_hash_defweak);
2699 BFD_ASSERT (weakdef->def_dynamic);
2700 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2701 || weakdef->root.type == bfd_link_hash_defweak);
2702 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2703 }
2704 }
2705
2706 return TRUE;
2707 }
2708
2709 /* Make the backend pick a good value for a dynamic symbol. This is
2710 called via elf_link_hash_traverse, and also calls itself
2711 recursively. */
2712
2713 static bfd_boolean
2714 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2715 {
2716 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2717 bfd *dynobj;
2718 const struct elf_backend_data *bed;
2719
2720 if (! is_elf_hash_table (eif->info->hash))
2721 return FALSE;
2722
2723 /* Ignore indirect symbols. These are added by the versioning code. */
2724 if (h->root.type == bfd_link_hash_indirect)
2725 return TRUE;
2726
2727 /* Fix the symbol flags. */
2728 if (! _bfd_elf_fix_symbol_flags (h, eif))
2729 return FALSE;
2730
2731 /* If this symbol does not require a PLT entry, and it is not
2732 defined by a dynamic object, or is not referenced by a regular
2733 object, ignore it. We do have to handle a weak defined symbol,
2734 even if no regular object refers to it, if we decided to add it
2735 to the dynamic symbol table. FIXME: Do we normally need to worry
2736 about symbols which are defined by one dynamic object and
2737 referenced by another one? */
2738 if (!h->needs_plt
2739 && h->type != STT_GNU_IFUNC
2740 && (h->def_regular
2741 || !h->def_dynamic
2742 || (!h->ref_regular
2743 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2744 {
2745 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2746 return TRUE;
2747 }
2748
2749 /* If we've already adjusted this symbol, don't do it again. This
2750 can happen via a recursive call. */
2751 if (h->dynamic_adjusted)
2752 return TRUE;
2753
2754 /* Don't look at this symbol again. Note that we must set this
2755 after checking the above conditions, because we may look at a
2756 symbol once, decide not to do anything, and then get called
2757 recursively later after REF_REGULAR is set below. */
2758 h->dynamic_adjusted = 1;
2759
2760 /* If this is a weak definition, and we know a real definition, and
2761 the real symbol is not itself defined by a regular object file,
2762 then get a good value for the real definition. We handle the
2763 real symbol first, for the convenience of the backend routine.
2764
2765 Note that there is a confusing case here. If the real definition
2766 is defined by a regular object file, we don't get the real symbol
2767 from the dynamic object, but we do get the weak symbol. If the
2768 processor backend uses a COPY reloc, then if some routine in the
2769 dynamic object changes the real symbol, we will not see that
2770 change in the corresponding weak symbol. This is the way other
2771 ELF linkers work as well, and seems to be a result of the shared
2772 library model.
2773
2774 I will clarify this issue. Most SVR4 shared libraries define the
2775 variable _timezone and define timezone as a weak synonym. The
2776 tzset call changes _timezone. If you write
2777 extern int timezone;
2778 int _timezone = 5;
2779 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2780 you might expect that, since timezone is a synonym for _timezone,
2781 the same number will print both times. However, if the processor
2782 backend uses a COPY reloc, then actually timezone will be copied
2783 into your process image, and, since you define _timezone
2784 yourself, _timezone will not. Thus timezone and _timezone will
2785 wind up at different memory locations. The tzset call will set
2786 _timezone, leaving timezone unchanged. */
2787
2788 if (h->u.weakdef != NULL)
2789 {
2790 /* If we get to this point, there is an implicit reference to
2791 H->U.WEAKDEF by a regular object file via the weak symbol H. */
2792 h->u.weakdef->ref_regular = 1;
2793
2794 /* Ensure that the backend adjust_dynamic_symbol function sees
2795 H->U.WEAKDEF before H by recursively calling ourselves. */
2796 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2797 return FALSE;
2798 }
2799
2800 /* If a symbol has no type and no size and does not require a PLT
2801 entry, then we are probably about to do the wrong thing here: we
2802 are probably going to create a COPY reloc for an empty object.
2803 This case can arise when a shared object is built with assembly
2804 code, and the assembly code fails to set the symbol type. */
2805 if (h->size == 0
2806 && h->type == STT_NOTYPE
2807 && !h->needs_plt)
2808 (*_bfd_error_handler)
2809 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2810 h->root.root.string);
2811
2812 dynobj = elf_hash_table (eif->info)->dynobj;
2813 bed = get_elf_backend_data (dynobj);
2814
2815 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2816 {
2817 eif->failed = TRUE;
2818 return FALSE;
2819 }
2820
2821 return TRUE;
2822 }
2823
2824 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2825 DYNBSS. */
2826
2827 bfd_boolean
2828 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2829 struct elf_link_hash_entry *h,
2830 asection *dynbss)
2831 {
2832 unsigned int power_of_two;
2833 bfd_vma mask;
2834 asection *sec = h->root.u.def.section;
2835
2836 /* The section aligment of definition is the maximum alignment
2837 requirement of symbols defined in the section. Since we don't
2838 know the symbol alignment requirement, we start with the
2839 maximum alignment and check low bits of the symbol address
2840 for the minimum alignment. */
2841 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2842 mask = ((bfd_vma) 1 << power_of_two) - 1;
2843 while ((h->root.u.def.value & mask) != 0)
2844 {
2845 mask >>= 1;
2846 --power_of_two;
2847 }
2848
2849 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2850 dynbss))
2851 {
2852 /* Adjust the section alignment if needed. */
2853 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2854 power_of_two))
2855 return FALSE;
2856 }
2857
2858 /* We make sure that the symbol will be aligned properly. */
2859 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2860
2861 /* Define the symbol as being at this point in DYNBSS. */
2862 h->root.u.def.section = dynbss;
2863 h->root.u.def.value = dynbss->size;
2864
2865 /* Increment the size of DYNBSS to make room for the symbol. */
2866 dynbss->size += h->size;
2867
2868 /* No error if extern_protected_data is true. */
2869 if (h->protected_def
2870 && (!info->extern_protected_data
2871 || (info->extern_protected_data < 0
2872 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
2873 info->callbacks->einfo
2874 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2875 h->root.root.string);
2876
2877 return TRUE;
2878 }
2879
2880 /* Adjust all external symbols pointing into SEC_MERGE sections
2881 to reflect the object merging within the sections. */
2882
2883 static bfd_boolean
2884 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2885 {
2886 asection *sec;
2887
2888 if ((h->root.type == bfd_link_hash_defined
2889 || h->root.type == bfd_link_hash_defweak)
2890 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2891 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
2892 {
2893 bfd *output_bfd = (bfd *) data;
2894
2895 h->root.u.def.value =
2896 _bfd_merged_section_offset (output_bfd,
2897 &h->root.u.def.section,
2898 elf_section_data (sec)->sec_info,
2899 h->root.u.def.value);
2900 }
2901
2902 return TRUE;
2903 }
2904
2905 /* Returns false if the symbol referred to by H should be considered
2906 to resolve local to the current module, and true if it should be
2907 considered to bind dynamically. */
2908
2909 bfd_boolean
2910 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2911 struct bfd_link_info *info,
2912 bfd_boolean not_local_protected)
2913 {
2914 bfd_boolean binding_stays_local_p;
2915 const struct elf_backend_data *bed;
2916 struct elf_link_hash_table *hash_table;
2917
2918 if (h == NULL)
2919 return FALSE;
2920
2921 while (h->root.type == bfd_link_hash_indirect
2922 || h->root.type == bfd_link_hash_warning)
2923 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2924
2925 /* If it was forced local, then clearly it's not dynamic. */
2926 if (h->dynindx == -1)
2927 return FALSE;
2928 if (h->forced_local)
2929 return FALSE;
2930
2931 /* Identify the cases where name binding rules say that a
2932 visible symbol resolves locally. */
2933 binding_stays_local_p = (bfd_link_executable (info)
2934 || SYMBOLIC_BIND (info, h));
2935
2936 switch (ELF_ST_VISIBILITY (h->other))
2937 {
2938 case STV_INTERNAL:
2939 case STV_HIDDEN:
2940 return FALSE;
2941
2942 case STV_PROTECTED:
2943 hash_table = elf_hash_table (info);
2944 if (!is_elf_hash_table (hash_table))
2945 return FALSE;
2946
2947 bed = get_elf_backend_data (hash_table->dynobj);
2948
2949 /* Proper resolution for function pointer equality may require
2950 that these symbols perhaps be resolved dynamically, even though
2951 we should be resolving them to the current module. */
2952 if (!not_local_protected || !bed->is_function_type (h->type))
2953 binding_stays_local_p = TRUE;
2954 break;
2955
2956 default:
2957 break;
2958 }
2959
2960 /* If it isn't defined locally, then clearly it's dynamic. */
2961 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2962 return TRUE;
2963
2964 /* Otherwise, the symbol is dynamic if binding rules don't tell
2965 us that it remains local. */
2966 return !binding_stays_local_p;
2967 }
2968
2969 /* Return true if the symbol referred to by H should be considered
2970 to resolve local to the current module, and false otherwise. Differs
2971 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2972 undefined symbols. The two functions are virtually identical except
2973 for the place where forced_local and dynindx == -1 are tested. If
2974 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2975 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2976 the symbol is local only for defined symbols.
2977 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2978 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2979 treatment of undefined weak symbols. For those that do not make
2980 undefined weak symbols dynamic, both functions may return false. */
2981
2982 bfd_boolean
2983 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2984 struct bfd_link_info *info,
2985 bfd_boolean local_protected)
2986 {
2987 const struct elf_backend_data *bed;
2988 struct elf_link_hash_table *hash_table;
2989
2990 /* If it's a local sym, of course we resolve locally. */
2991 if (h == NULL)
2992 return TRUE;
2993
2994 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2995 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2996 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2997 return TRUE;
2998
2999 /* Common symbols that become definitions don't get the DEF_REGULAR
3000 flag set, so test it first, and don't bail out. */
3001 if (ELF_COMMON_DEF_P (h))
3002 /* Do nothing. */;
3003 /* If we don't have a definition in a regular file, then we can't
3004 resolve locally. The sym is either undefined or dynamic. */
3005 else if (!h->def_regular)
3006 return FALSE;
3007
3008 /* Forced local symbols resolve locally. */
3009 if (h->forced_local)
3010 return TRUE;
3011
3012 /* As do non-dynamic symbols. */
3013 if (h->dynindx == -1)
3014 return TRUE;
3015
3016 /* At this point, we know the symbol is defined and dynamic. In an
3017 executable it must resolve locally, likewise when building symbolic
3018 shared libraries. */
3019 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3020 return TRUE;
3021
3022 /* Now deal with defined dynamic symbols in shared libraries. Ones
3023 with default visibility might not resolve locally. */
3024 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3025 return FALSE;
3026
3027 hash_table = elf_hash_table (info);
3028 if (!is_elf_hash_table (hash_table))
3029 return TRUE;
3030
3031 bed = get_elf_backend_data (hash_table->dynobj);
3032
3033 /* If extern_protected_data is false, STV_PROTECTED non-function
3034 symbols are local. */
3035 if ((!info->extern_protected_data
3036 || (info->extern_protected_data < 0
3037 && !bed->extern_protected_data))
3038 && !bed->is_function_type (h->type))
3039 return TRUE;
3040
3041 /* Function pointer equality tests may require that STV_PROTECTED
3042 symbols be treated as dynamic symbols. If the address of a
3043 function not defined in an executable is set to that function's
3044 plt entry in the executable, then the address of the function in
3045 a shared library must also be the plt entry in the executable. */
3046 return local_protected;
3047 }
3048
3049 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3050 aligned. Returns the first TLS output section. */
3051
3052 struct bfd_section *
3053 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3054 {
3055 struct bfd_section *sec, *tls;
3056 unsigned int align = 0;
3057
3058 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3059 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3060 break;
3061 tls = sec;
3062
3063 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3064 if (sec->alignment_power > align)
3065 align = sec->alignment_power;
3066
3067 elf_hash_table (info)->tls_sec = tls;
3068
3069 /* Ensure the alignment of the first section is the largest alignment,
3070 so that the tls segment starts aligned. */
3071 if (tls != NULL)
3072 tls->alignment_power = align;
3073
3074 return tls;
3075 }
3076
3077 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3078 static bfd_boolean
3079 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3080 Elf_Internal_Sym *sym)
3081 {
3082 const struct elf_backend_data *bed;
3083
3084 /* Local symbols do not count, but target specific ones might. */
3085 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3086 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3087 return FALSE;
3088
3089 bed = get_elf_backend_data (abfd);
3090 /* Function symbols do not count. */
3091 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3092 return FALSE;
3093
3094 /* If the section is undefined, then so is the symbol. */
3095 if (sym->st_shndx == SHN_UNDEF)
3096 return FALSE;
3097
3098 /* If the symbol is defined in the common section, then
3099 it is a common definition and so does not count. */
3100 if (bed->common_definition (sym))
3101 return FALSE;
3102
3103 /* If the symbol is in a target specific section then we
3104 must rely upon the backend to tell us what it is. */
3105 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3106 /* FIXME - this function is not coded yet:
3107
3108 return _bfd_is_global_symbol_definition (abfd, sym);
3109
3110 Instead for now assume that the definition is not global,
3111 Even if this is wrong, at least the linker will behave
3112 in the same way that it used to do. */
3113 return FALSE;
3114
3115 return TRUE;
3116 }
3117
3118 /* Search the symbol table of the archive element of the archive ABFD
3119 whose archive map contains a mention of SYMDEF, and determine if
3120 the symbol is defined in this element. */
3121 static bfd_boolean
3122 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3123 {
3124 Elf_Internal_Shdr * hdr;
3125 size_t symcount;
3126 size_t extsymcount;
3127 size_t extsymoff;
3128 Elf_Internal_Sym *isymbuf;
3129 Elf_Internal_Sym *isym;
3130 Elf_Internal_Sym *isymend;
3131 bfd_boolean result;
3132
3133 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3134 if (abfd == NULL)
3135 return FALSE;
3136
3137 if (! bfd_check_format (abfd, bfd_object))
3138 return FALSE;
3139
3140 /* Select the appropriate symbol table. If we don't know if the
3141 object file is an IR object, give linker LTO plugin a chance to
3142 get the correct symbol table. */
3143 if (abfd->plugin_format == bfd_plugin_yes
3144 #if BFD_SUPPORTS_PLUGINS
3145 || (abfd->plugin_format == bfd_plugin_unknown
3146 && bfd_link_plugin_object_p (abfd))
3147 #endif
3148 )
3149 {
3150 /* Use the IR symbol table if the object has been claimed by
3151 plugin. */
3152 abfd = abfd->plugin_dummy_bfd;
3153 hdr = &elf_tdata (abfd)->symtab_hdr;
3154 }
3155 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3156 hdr = &elf_tdata (abfd)->symtab_hdr;
3157 else
3158 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3159
3160 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3161
3162 /* The sh_info field of the symtab header tells us where the
3163 external symbols start. We don't care about the local symbols. */
3164 if (elf_bad_symtab (abfd))
3165 {
3166 extsymcount = symcount;
3167 extsymoff = 0;
3168 }
3169 else
3170 {
3171 extsymcount = symcount - hdr->sh_info;
3172 extsymoff = hdr->sh_info;
3173 }
3174
3175 if (extsymcount == 0)
3176 return FALSE;
3177
3178 /* Read in the symbol table. */
3179 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3180 NULL, NULL, NULL);
3181 if (isymbuf == NULL)
3182 return FALSE;
3183
3184 /* Scan the symbol table looking for SYMDEF. */
3185 result = FALSE;
3186 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3187 {
3188 const char *name;
3189
3190 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3191 isym->st_name);
3192 if (name == NULL)
3193 break;
3194
3195 if (strcmp (name, symdef->name) == 0)
3196 {
3197 result = is_global_data_symbol_definition (abfd, isym);
3198 break;
3199 }
3200 }
3201
3202 free (isymbuf);
3203
3204 return result;
3205 }
3206
3207 /* Add an entry to the .dynamic table. */
3209
3210 bfd_boolean
3211 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3212 bfd_vma tag,
3213 bfd_vma val)
3214 {
3215 struct elf_link_hash_table *hash_table;
3216 const struct elf_backend_data *bed;
3217 asection *s;
3218 bfd_size_type newsize;
3219 bfd_byte *newcontents;
3220 Elf_Internal_Dyn dyn;
3221
3222 hash_table = elf_hash_table (info);
3223 if (! is_elf_hash_table (hash_table))
3224 return FALSE;
3225
3226 bed = get_elf_backend_data (hash_table->dynobj);
3227 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3228 BFD_ASSERT (s != NULL);
3229
3230 newsize = s->size + bed->s->sizeof_dyn;
3231 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3232 if (newcontents == NULL)
3233 return FALSE;
3234
3235 dyn.d_tag = tag;
3236 dyn.d_un.d_val = val;
3237 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3238
3239 s->size = newsize;
3240 s->contents = newcontents;
3241
3242 return TRUE;
3243 }
3244
3245 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3246 otherwise just check whether one already exists. Returns -1 on error,
3247 1 if a DT_NEEDED tag already exists, and 0 on success. */
3248
3249 static int
3250 elf_add_dt_needed_tag (bfd *abfd,
3251 struct bfd_link_info *info,
3252 const char *soname,
3253 bfd_boolean do_it)
3254 {
3255 struct elf_link_hash_table *hash_table;
3256 size_t strindex;
3257
3258 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3259 return -1;
3260
3261 hash_table = elf_hash_table (info);
3262 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3263 if (strindex == (size_t) -1)
3264 return -1;
3265
3266 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3267 {
3268 asection *sdyn;
3269 const struct elf_backend_data *bed;
3270 bfd_byte *extdyn;
3271
3272 bed = get_elf_backend_data (hash_table->dynobj);
3273 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3274 if (sdyn != NULL)
3275 for (extdyn = sdyn->contents;
3276 extdyn < sdyn->contents + sdyn->size;
3277 extdyn += bed->s->sizeof_dyn)
3278 {
3279 Elf_Internal_Dyn dyn;
3280
3281 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3282 if (dyn.d_tag == DT_NEEDED
3283 && dyn.d_un.d_val == strindex)
3284 {
3285 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3286 return 1;
3287 }
3288 }
3289 }
3290
3291 if (do_it)
3292 {
3293 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3294 return -1;
3295
3296 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3297 return -1;
3298 }
3299 else
3300 /* We were just checking for existence of the tag. */
3301 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3302
3303 return 0;
3304 }
3305
3306 /* Return true if SONAME is on the needed list between NEEDED and STOP
3307 (or the end of list if STOP is NULL), and needed by a library that
3308 will be loaded. */
3309
3310 static bfd_boolean
3311 on_needed_list (const char *soname,
3312 struct bfd_link_needed_list *needed,
3313 struct bfd_link_needed_list *stop)
3314 {
3315 struct bfd_link_needed_list *look;
3316 for (look = needed; look != stop; look = look->next)
3317 if (strcmp (soname, look->name) == 0
3318 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3319 /* If needed by a library that itself is not directly
3320 needed, recursively check whether that library is
3321 indirectly needed. Since we add DT_NEEDED entries to
3322 the end of the list, library dependencies appear after
3323 the library. Therefore search prior to the current
3324 LOOK, preventing possible infinite recursion. */
3325 || on_needed_list (elf_dt_name (look->by), needed, look)))
3326 return TRUE;
3327
3328 return FALSE;
3329 }
3330
3331 /* Sort symbol by value, section, and size. */
3332 static int
3333 elf_sort_symbol (const void *arg1, const void *arg2)
3334 {
3335 const struct elf_link_hash_entry *h1;
3336 const struct elf_link_hash_entry *h2;
3337 bfd_signed_vma vdiff;
3338
3339 h1 = *(const struct elf_link_hash_entry **) arg1;
3340 h2 = *(const struct elf_link_hash_entry **) arg2;
3341 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3342 if (vdiff != 0)
3343 return vdiff > 0 ? 1 : -1;
3344 else
3345 {
3346 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3347 if (sdiff != 0)
3348 return sdiff > 0 ? 1 : -1;
3349 }
3350 vdiff = h1->size - h2->size;
3351 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3352 }
3353
3354 /* This function is used to adjust offsets into .dynstr for
3355 dynamic symbols. This is called via elf_link_hash_traverse. */
3356
3357 static bfd_boolean
3358 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3359 {
3360 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3361
3362 if (h->dynindx != -1)
3363 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3364 return TRUE;
3365 }
3366
3367 /* Assign string offsets in .dynstr, update all structures referencing
3368 them. */
3369
3370 static bfd_boolean
3371 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3372 {
3373 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3374 struct elf_link_local_dynamic_entry *entry;
3375 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3376 bfd *dynobj = hash_table->dynobj;
3377 asection *sdyn;
3378 bfd_size_type size;
3379 const struct elf_backend_data *bed;
3380 bfd_byte *extdyn;
3381
3382 _bfd_elf_strtab_finalize (dynstr);
3383 size = _bfd_elf_strtab_size (dynstr);
3384
3385 bed = get_elf_backend_data (dynobj);
3386 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3387 BFD_ASSERT (sdyn != NULL);
3388
3389 /* Update all .dynamic entries referencing .dynstr strings. */
3390 for (extdyn = sdyn->contents;
3391 extdyn < sdyn->contents + sdyn->size;
3392 extdyn += bed->s->sizeof_dyn)
3393 {
3394 Elf_Internal_Dyn dyn;
3395
3396 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3397 switch (dyn.d_tag)
3398 {
3399 case DT_STRSZ:
3400 dyn.d_un.d_val = size;
3401 break;
3402 case DT_NEEDED:
3403 case DT_SONAME:
3404 case DT_RPATH:
3405 case DT_RUNPATH:
3406 case DT_FILTER:
3407 case DT_AUXILIARY:
3408 case DT_AUDIT:
3409 case DT_DEPAUDIT:
3410 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3411 break;
3412 default:
3413 continue;
3414 }
3415 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3416 }
3417
3418 /* Now update local dynamic symbols. */
3419 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3420 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3421 entry->isym.st_name);
3422
3423 /* And the rest of dynamic symbols. */
3424 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3425
3426 /* Adjust version definitions. */
3427 if (elf_tdata (output_bfd)->cverdefs)
3428 {
3429 asection *s;
3430 bfd_byte *p;
3431 size_t i;
3432 Elf_Internal_Verdef def;
3433 Elf_Internal_Verdaux defaux;
3434
3435 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3436 p = s->contents;
3437 do
3438 {
3439 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3440 &def);
3441 p += sizeof (Elf_External_Verdef);
3442 if (def.vd_aux != sizeof (Elf_External_Verdef))
3443 continue;
3444 for (i = 0; i < def.vd_cnt; ++i)
3445 {
3446 _bfd_elf_swap_verdaux_in (output_bfd,
3447 (Elf_External_Verdaux *) p, &defaux);
3448 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3449 defaux.vda_name);
3450 _bfd_elf_swap_verdaux_out (output_bfd,
3451 &defaux, (Elf_External_Verdaux *) p);
3452 p += sizeof (Elf_External_Verdaux);
3453 }
3454 }
3455 while (def.vd_next);
3456 }
3457
3458 /* Adjust version references. */
3459 if (elf_tdata (output_bfd)->verref)
3460 {
3461 asection *s;
3462 bfd_byte *p;
3463 size_t i;
3464 Elf_Internal_Verneed need;
3465 Elf_Internal_Vernaux needaux;
3466
3467 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3468 p = s->contents;
3469 do
3470 {
3471 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3472 &need);
3473 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3474 _bfd_elf_swap_verneed_out (output_bfd, &need,
3475 (Elf_External_Verneed *) p);
3476 p += sizeof (Elf_External_Verneed);
3477 for (i = 0; i < need.vn_cnt; ++i)
3478 {
3479 _bfd_elf_swap_vernaux_in (output_bfd,
3480 (Elf_External_Vernaux *) p, &needaux);
3481 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3482 needaux.vna_name);
3483 _bfd_elf_swap_vernaux_out (output_bfd,
3484 &needaux,
3485 (Elf_External_Vernaux *) p);
3486 p += sizeof (Elf_External_Vernaux);
3487 }
3488 }
3489 while (need.vn_next);
3490 }
3491
3492 return TRUE;
3493 }
3494
3495 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3497 The default is to only match when the INPUT and OUTPUT are exactly
3498 the same target. */
3499
3500 bfd_boolean
3501 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3502 const bfd_target *output)
3503 {
3504 return input == output;
3505 }
3506
3507 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3508 This version is used when different targets for the same architecture
3509 are virtually identical. */
3510
3511 bfd_boolean
3512 _bfd_elf_relocs_compatible (const bfd_target *input,
3513 const bfd_target *output)
3514 {
3515 const struct elf_backend_data *obed, *ibed;
3516
3517 if (input == output)
3518 return TRUE;
3519
3520 ibed = xvec_get_elf_backend_data (input);
3521 obed = xvec_get_elf_backend_data (output);
3522
3523 if (ibed->arch != obed->arch)
3524 return FALSE;
3525
3526 /* If both backends are using this function, deem them compatible. */
3527 return ibed->relocs_compatible == obed->relocs_compatible;
3528 }
3529
3530 /* Make a special call to the linker "notice" function to tell it that
3531 we are about to handle an as-needed lib, or have finished
3532 processing the lib. */
3533
3534 bfd_boolean
3535 _bfd_elf_notice_as_needed (bfd *ibfd,
3536 struct bfd_link_info *info,
3537 enum notice_asneeded_action act)
3538 {
3539 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3540 }
3541
3542 /* Check relocations an ELF object file. */
3543
3544 bfd_boolean
3545 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3546 {
3547 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3548 struct elf_link_hash_table *htab = elf_hash_table (info);
3549
3550 /* If this object is the same format as the output object, and it is
3551 not a shared library, then let the backend look through the
3552 relocs.
3553
3554 This is required to build global offset table entries and to
3555 arrange for dynamic relocs. It is not required for the
3556 particular common case of linking non PIC code, even when linking
3557 against shared libraries, but unfortunately there is no way of
3558 knowing whether an object file has been compiled PIC or not.
3559 Looking through the relocs is not particularly time consuming.
3560 The problem is that we must either (1) keep the relocs in memory,
3561 which causes the linker to require additional runtime memory or
3562 (2) read the relocs twice from the input file, which wastes time.
3563 This would be a good case for using mmap.
3564
3565 I have no idea how to handle linking PIC code into a file of a
3566 different format. It probably can't be done. */
3567 if ((abfd->flags & DYNAMIC) == 0
3568 && is_elf_hash_table (htab)
3569 && bed->check_relocs != NULL
3570 && elf_object_id (abfd) == elf_hash_table_id (htab)
3571 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3572 {
3573 asection *o;
3574
3575 for (o = abfd->sections; o != NULL; o = o->next)
3576 {
3577 Elf_Internal_Rela *internal_relocs;
3578 bfd_boolean ok;
3579
3580 /* Don't check relocations in excluded sections. */
3581 if ((o->flags & SEC_RELOC) == 0
3582 || (o->flags & SEC_EXCLUDE) != 0
3583 || o->reloc_count == 0
3584 || ((info->strip == strip_all || info->strip == strip_debugger)
3585 && (o->flags & SEC_DEBUGGING) != 0)
3586 || bfd_is_abs_section (o->output_section))
3587 continue;
3588
3589 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3590 info->keep_memory);
3591 if (internal_relocs == NULL)
3592 return FALSE;
3593
3594 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3595
3596 if (elf_section_data (o)->relocs != internal_relocs)
3597 free (internal_relocs);
3598
3599 if (! ok)
3600 return FALSE;
3601 }
3602 }
3603
3604 return TRUE;
3605 }
3606
3607 /* Add symbols from an ELF object file to the linker hash table. */
3608
3609 static bfd_boolean
3610 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3611 {
3612 Elf_Internal_Ehdr *ehdr;
3613 Elf_Internal_Shdr *hdr;
3614 size_t symcount;
3615 size_t extsymcount;
3616 size_t extsymoff;
3617 struct elf_link_hash_entry **sym_hash;
3618 bfd_boolean dynamic;
3619 Elf_External_Versym *extversym = NULL;
3620 Elf_External_Versym *ever;
3621 struct elf_link_hash_entry *weaks;
3622 struct elf_link_hash_entry **nondeflt_vers = NULL;
3623 size_t nondeflt_vers_cnt = 0;
3624 Elf_Internal_Sym *isymbuf = NULL;
3625 Elf_Internal_Sym *isym;
3626 Elf_Internal_Sym *isymend;
3627 const struct elf_backend_data *bed;
3628 bfd_boolean add_needed;
3629 struct elf_link_hash_table *htab;
3630 bfd_size_type amt;
3631 void *alloc_mark = NULL;
3632 struct bfd_hash_entry **old_table = NULL;
3633 unsigned int old_size = 0;
3634 unsigned int old_count = 0;
3635 void *old_tab = NULL;
3636 void *old_ent;
3637 struct bfd_link_hash_entry *old_undefs = NULL;
3638 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3639 void *old_strtab = NULL;
3640 size_t tabsize = 0;
3641 asection *s;
3642 bfd_boolean just_syms;
3643
3644 htab = elf_hash_table (info);
3645 bed = get_elf_backend_data (abfd);
3646
3647 if ((abfd->flags & DYNAMIC) == 0)
3648 dynamic = FALSE;
3649 else
3650 {
3651 dynamic = TRUE;
3652
3653 /* You can't use -r against a dynamic object. Also, there's no
3654 hope of using a dynamic object which does not exactly match
3655 the format of the output file. */
3656 if (bfd_link_relocatable (info)
3657 || !is_elf_hash_table (htab)
3658 || info->output_bfd->xvec != abfd->xvec)
3659 {
3660 if (bfd_link_relocatable (info))
3661 bfd_set_error (bfd_error_invalid_operation);
3662 else
3663 bfd_set_error (bfd_error_wrong_format);
3664 goto error_return;
3665 }
3666 }
3667
3668 ehdr = elf_elfheader (abfd);
3669 if (info->warn_alternate_em
3670 && bed->elf_machine_code != ehdr->e_machine
3671 && ((bed->elf_machine_alt1 != 0
3672 && ehdr->e_machine == bed->elf_machine_alt1)
3673 || (bed->elf_machine_alt2 != 0
3674 && ehdr->e_machine == bed->elf_machine_alt2)))
3675 info->callbacks->einfo
3676 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3677 ehdr->e_machine, abfd, bed->elf_machine_code);
3678
3679 /* As a GNU extension, any input sections which are named
3680 .gnu.warning.SYMBOL are treated as warning symbols for the given
3681 symbol. This differs from .gnu.warning sections, which generate
3682 warnings when they are included in an output file. */
3683 /* PR 12761: Also generate this warning when building shared libraries. */
3684 for (s = abfd->sections; s != NULL; s = s->next)
3685 {
3686 const char *name;
3687
3688 name = bfd_get_section_name (abfd, s);
3689 if (CONST_STRNEQ (name, ".gnu.warning."))
3690 {
3691 char *msg;
3692 bfd_size_type sz;
3693
3694 name += sizeof ".gnu.warning." - 1;
3695
3696 /* If this is a shared object, then look up the symbol
3697 in the hash table. If it is there, and it is already
3698 been defined, then we will not be using the entry
3699 from this shared object, so we don't need to warn.
3700 FIXME: If we see the definition in a regular object
3701 later on, we will warn, but we shouldn't. The only
3702 fix is to keep track of what warnings we are supposed
3703 to emit, and then handle them all at the end of the
3704 link. */
3705 if (dynamic)
3706 {
3707 struct elf_link_hash_entry *h;
3708
3709 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3710
3711 /* FIXME: What about bfd_link_hash_common? */
3712 if (h != NULL
3713 && (h->root.type == bfd_link_hash_defined
3714 || h->root.type == bfd_link_hash_defweak))
3715 continue;
3716 }
3717
3718 sz = s->size;
3719 msg = (char *) bfd_alloc (abfd, sz + 1);
3720 if (msg == NULL)
3721 goto error_return;
3722
3723 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3724 goto error_return;
3725
3726 msg[sz] = '\0';
3727
3728 if (! (_bfd_generic_link_add_one_symbol
3729 (info, abfd, name, BSF_WARNING, s, 0, msg,
3730 FALSE, bed->collect, NULL)))
3731 goto error_return;
3732
3733 if (bfd_link_executable (info))
3734 {
3735 /* Clobber the section size so that the warning does
3736 not get copied into the output file. */
3737 s->size = 0;
3738
3739 /* Also set SEC_EXCLUDE, so that symbols defined in
3740 the warning section don't get copied to the output. */
3741 s->flags |= SEC_EXCLUDE;
3742 }
3743 }
3744 }
3745
3746 just_syms = ((s = abfd->sections) != NULL
3747 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3748
3749 add_needed = TRUE;
3750 if (! dynamic)
3751 {
3752 /* If we are creating a shared library, create all the dynamic
3753 sections immediately. We need to attach them to something,
3754 so we attach them to this BFD, provided it is the right
3755 format and is not from ld --just-symbols. Always create the
3756 dynamic sections for -E/--dynamic-list. FIXME: If there
3757 are no input BFD's of the same format as the output, we can't
3758 make a shared library. */
3759 if (!just_syms
3760 && (bfd_link_pic (info)
3761 || (!bfd_link_relocatable (info)
3762 && (info->export_dynamic || info->dynamic)))
3763 && is_elf_hash_table (htab)
3764 && info->output_bfd->xvec == abfd->xvec
3765 && !htab->dynamic_sections_created)
3766 {
3767 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3768 goto error_return;
3769 }
3770 }
3771 else if (!is_elf_hash_table (htab))
3772 goto error_return;
3773 else
3774 {
3775 const char *soname = NULL;
3776 char *audit = NULL;
3777 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3778 int ret;
3779
3780 /* ld --just-symbols and dynamic objects don't mix very well.
3781 ld shouldn't allow it. */
3782 if (just_syms)
3783 abort ();
3784
3785 /* If this dynamic lib was specified on the command line with
3786 --as-needed in effect, then we don't want to add a DT_NEEDED
3787 tag unless the lib is actually used. Similary for libs brought
3788 in by another lib's DT_NEEDED. When --no-add-needed is used
3789 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3790 any dynamic library in DT_NEEDED tags in the dynamic lib at
3791 all. */
3792 add_needed = (elf_dyn_lib_class (abfd)
3793 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3794 | DYN_NO_NEEDED)) == 0;
3795
3796 s = bfd_get_section_by_name (abfd, ".dynamic");
3797 if (s != NULL)
3798 {
3799 bfd_byte *dynbuf;
3800 bfd_byte *extdyn;
3801 unsigned int elfsec;
3802 unsigned long shlink;
3803
3804 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3805 {
3806 error_free_dyn:
3807 free (dynbuf);
3808 goto error_return;
3809 }
3810
3811 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3812 if (elfsec == SHN_BAD)
3813 goto error_free_dyn;
3814 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3815
3816 for (extdyn = dynbuf;
3817 extdyn < dynbuf + s->size;
3818 extdyn += bed->s->sizeof_dyn)
3819 {
3820 Elf_Internal_Dyn dyn;
3821
3822 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3823 if (dyn.d_tag == DT_SONAME)
3824 {
3825 unsigned int tagv = dyn.d_un.d_val;
3826 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3827 if (soname == NULL)
3828 goto error_free_dyn;
3829 }
3830 if (dyn.d_tag == DT_NEEDED)
3831 {
3832 struct bfd_link_needed_list *n, **pn;
3833 char *fnm, *anm;
3834 unsigned int tagv = dyn.d_un.d_val;
3835
3836 amt = sizeof (struct bfd_link_needed_list);
3837 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3838 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3839 if (n == NULL || fnm == NULL)
3840 goto error_free_dyn;
3841 amt = strlen (fnm) + 1;
3842 anm = (char *) bfd_alloc (abfd, amt);
3843 if (anm == NULL)
3844 goto error_free_dyn;
3845 memcpy (anm, fnm, amt);
3846 n->name = anm;
3847 n->by = abfd;
3848 n->next = NULL;
3849 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3850 ;
3851 *pn = n;
3852 }
3853 if (dyn.d_tag == DT_RUNPATH)
3854 {
3855 struct bfd_link_needed_list *n, **pn;
3856 char *fnm, *anm;
3857 unsigned int tagv = dyn.d_un.d_val;
3858
3859 amt = sizeof (struct bfd_link_needed_list);
3860 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3861 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3862 if (n == NULL || fnm == NULL)
3863 goto error_free_dyn;
3864 amt = strlen (fnm) + 1;
3865 anm = (char *) bfd_alloc (abfd, amt);
3866 if (anm == NULL)
3867 goto error_free_dyn;
3868 memcpy (anm, fnm, amt);
3869 n->name = anm;
3870 n->by = abfd;
3871 n->next = NULL;
3872 for (pn = & runpath;
3873 *pn != NULL;
3874 pn = &(*pn)->next)
3875 ;
3876 *pn = n;
3877 }
3878 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3879 if (!runpath && dyn.d_tag == DT_RPATH)
3880 {
3881 struct bfd_link_needed_list *n, **pn;
3882 char *fnm, *anm;
3883 unsigned int tagv = dyn.d_un.d_val;
3884
3885 amt = sizeof (struct bfd_link_needed_list);
3886 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3887 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3888 if (n == NULL || fnm == NULL)
3889 goto error_free_dyn;
3890 amt = strlen (fnm) + 1;
3891 anm = (char *) bfd_alloc (abfd, amt);
3892 if (anm == NULL)
3893 goto error_free_dyn;
3894 memcpy (anm, fnm, amt);
3895 n->name = anm;
3896 n->by = abfd;
3897 n->next = NULL;
3898 for (pn = & rpath;
3899 *pn != NULL;
3900 pn = &(*pn)->next)
3901 ;
3902 *pn = n;
3903 }
3904 if (dyn.d_tag == DT_AUDIT)
3905 {
3906 unsigned int tagv = dyn.d_un.d_val;
3907 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3908 }
3909 }
3910
3911 free (dynbuf);
3912 }
3913
3914 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3915 frees all more recently bfd_alloc'd blocks as well. */
3916 if (runpath)
3917 rpath = runpath;
3918
3919 if (rpath)
3920 {
3921 struct bfd_link_needed_list **pn;
3922 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3923 ;
3924 *pn = rpath;
3925 }
3926
3927 /* We do not want to include any of the sections in a dynamic
3928 object in the output file. We hack by simply clobbering the
3929 list of sections in the BFD. This could be handled more
3930 cleanly by, say, a new section flag; the existing
3931 SEC_NEVER_LOAD flag is not the one we want, because that one
3932 still implies that the section takes up space in the output
3933 file. */
3934 bfd_section_list_clear (abfd);
3935
3936 /* Find the name to use in a DT_NEEDED entry that refers to this
3937 object. If the object has a DT_SONAME entry, we use it.
3938 Otherwise, if the generic linker stuck something in
3939 elf_dt_name, we use that. Otherwise, we just use the file
3940 name. */
3941 if (soname == NULL || *soname == '\0')
3942 {
3943 soname = elf_dt_name (abfd);
3944 if (soname == NULL || *soname == '\0')
3945 soname = bfd_get_filename (abfd);
3946 }
3947
3948 /* Save the SONAME because sometimes the linker emulation code
3949 will need to know it. */
3950 elf_dt_name (abfd) = soname;
3951
3952 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3953 if (ret < 0)
3954 goto error_return;
3955
3956 /* If we have already included this dynamic object in the
3957 link, just ignore it. There is no reason to include a
3958 particular dynamic object more than once. */
3959 if (ret > 0)
3960 return TRUE;
3961
3962 /* Save the DT_AUDIT entry for the linker emulation code. */
3963 elf_dt_audit (abfd) = audit;
3964 }
3965
3966 /* If this is a dynamic object, we always link against the .dynsym
3967 symbol table, not the .symtab symbol table. The dynamic linker
3968 will only see the .dynsym symbol table, so there is no reason to
3969 look at .symtab for a dynamic object. */
3970
3971 if (! dynamic || elf_dynsymtab (abfd) == 0)
3972 hdr = &elf_tdata (abfd)->symtab_hdr;
3973 else
3974 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3975
3976 symcount = hdr->sh_size / bed->s->sizeof_sym;
3977
3978 /* The sh_info field of the symtab header tells us where the
3979 external symbols start. We don't care about the local symbols at
3980 this point. */
3981 if (elf_bad_symtab (abfd))
3982 {
3983 extsymcount = symcount;
3984 extsymoff = 0;
3985 }
3986 else
3987 {
3988 extsymcount = symcount - hdr->sh_info;
3989 extsymoff = hdr->sh_info;
3990 }
3991
3992 sym_hash = elf_sym_hashes (abfd);
3993 if (extsymcount != 0)
3994 {
3995 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3996 NULL, NULL, NULL);
3997 if (isymbuf == NULL)
3998 goto error_return;
3999
4000 if (sym_hash == NULL)
4001 {
4002 /* We store a pointer to the hash table entry for each
4003 external symbol. */
4004 amt = extsymcount;
4005 amt *= sizeof (struct elf_link_hash_entry *);
4006 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4007 if (sym_hash == NULL)
4008 goto error_free_sym;
4009 elf_sym_hashes (abfd) = sym_hash;
4010 }
4011 }
4012
4013 if (dynamic)
4014 {
4015 /* Read in any version definitions. */
4016 if (!_bfd_elf_slurp_version_tables (abfd,
4017 info->default_imported_symver))
4018 goto error_free_sym;
4019
4020 /* Read in the symbol versions, but don't bother to convert them
4021 to internal format. */
4022 if (elf_dynversym (abfd) != 0)
4023 {
4024 Elf_Internal_Shdr *versymhdr;
4025
4026 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4027 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4028 if (extversym == NULL)
4029 goto error_free_sym;
4030 amt = versymhdr->sh_size;
4031 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4032 || bfd_bread (extversym, amt, abfd) != amt)
4033 goto error_free_vers;
4034 }
4035 }
4036
4037 /* If we are loading an as-needed shared lib, save the symbol table
4038 state before we start adding symbols. If the lib turns out
4039 to be unneeded, restore the state. */
4040 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4041 {
4042 unsigned int i;
4043 size_t entsize;
4044
4045 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4046 {
4047 struct bfd_hash_entry *p;
4048 struct elf_link_hash_entry *h;
4049
4050 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4051 {
4052 h = (struct elf_link_hash_entry *) p;
4053 entsize += htab->root.table.entsize;
4054 if (h->root.type == bfd_link_hash_warning)
4055 entsize += htab->root.table.entsize;
4056 }
4057 }
4058
4059 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4060 old_tab = bfd_malloc (tabsize + entsize);
4061 if (old_tab == NULL)
4062 goto error_free_vers;
4063
4064 /* Remember the current objalloc pointer, so that all mem for
4065 symbols added can later be reclaimed. */
4066 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4067 if (alloc_mark == NULL)
4068 goto error_free_vers;
4069
4070 /* Make a special call to the linker "notice" function to
4071 tell it that we are about to handle an as-needed lib. */
4072 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4073 goto error_free_vers;
4074
4075 /* Clone the symbol table. Remember some pointers into the
4076 symbol table, and dynamic symbol count. */
4077 old_ent = (char *) old_tab + tabsize;
4078 memcpy (old_tab, htab->root.table.table, tabsize);
4079 old_undefs = htab->root.undefs;
4080 old_undefs_tail = htab->root.undefs_tail;
4081 old_table = htab->root.table.table;
4082 old_size = htab->root.table.size;
4083 old_count = htab->root.table.count;
4084 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4085 if (old_strtab == NULL)
4086 goto error_free_vers;
4087
4088 for (i = 0; i < htab->root.table.size; i++)
4089 {
4090 struct bfd_hash_entry *p;
4091 struct elf_link_hash_entry *h;
4092
4093 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4094 {
4095 memcpy (old_ent, p, htab->root.table.entsize);
4096 old_ent = (char *) old_ent + htab->root.table.entsize;
4097 h = (struct elf_link_hash_entry *) p;
4098 if (h->root.type == bfd_link_hash_warning)
4099 {
4100 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4101 old_ent = (char *) old_ent + htab->root.table.entsize;
4102 }
4103 }
4104 }
4105 }
4106
4107 weaks = NULL;
4108 ever = extversym != NULL ? extversym + extsymoff : NULL;
4109 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4110 isym < isymend;
4111 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4112 {
4113 int bind;
4114 bfd_vma value;
4115 asection *sec, *new_sec;
4116 flagword flags;
4117 const char *name;
4118 struct elf_link_hash_entry *h;
4119 struct elf_link_hash_entry *hi;
4120 bfd_boolean definition;
4121 bfd_boolean size_change_ok;
4122 bfd_boolean type_change_ok;
4123 bfd_boolean new_weakdef;
4124 bfd_boolean new_weak;
4125 bfd_boolean old_weak;
4126 bfd_boolean override;
4127 bfd_boolean common;
4128 bfd_boolean discarded;
4129 unsigned int old_alignment;
4130 bfd *old_bfd;
4131 bfd_boolean matched;
4132
4133 override = FALSE;
4134
4135 flags = BSF_NO_FLAGS;
4136 sec = NULL;
4137 value = isym->st_value;
4138 common = bed->common_definition (isym);
4139 discarded = FALSE;
4140
4141 bind = ELF_ST_BIND (isym->st_info);
4142 switch (bind)
4143 {
4144 case STB_LOCAL:
4145 /* This should be impossible, since ELF requires that all
4146 global symbols follow all local symbols, and that sh_info
4147 point to the first global symbol. Unfortunately, Irix 5
4148 screws this up. */
4149 continue;
4150
4151 case STB_GLOBAL:
4152 if (isym->st_shndx != SHN_UNDEF && !common)
4153 flags = BSF_GLOBAL;
4154 break;
4155
4156 case STB_WEAK:
4157 flags = BSF_WEAK;
4158 break;
4159
4160 case STB_GNU_UNIQUE:
4161 flags = BSF_GNU_UNIQUE;
4162 break;
4163
4164 default:
4165 /* Leave it up to the processor backend. */
4166 break;
4167 }
4168
4169 if (isym->st_shndx == SHN_UNDEF)
4170 sec = bfd_und_section_ptr;
4171 else if (isym->st_shndx == SHN_ABS)
4172 sec = bfd_abs_section_ptr;
4173 else if (isym->st_shndx == SHN_COMMON)
4174 {
4175 sec = bfd_com_section_ptr;
4176 /* What ELF calls the size we call the value. What ELF
4177 calls the value we call the alignment. */
4178 value = isym->st_size;
4179 }
4180 else
4181 {
4182 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4183 if (sec == NULL)
4184 sec = bfd_abs_section_ptr;
4185 else if (discarded_section (sec))
4186 {
4187 /* Symbols from discarded section are undefined. We keep
4188 its visibility. */
4189 sec = bfd_und_section_ptr;
4190 discarded = TRUE;
4191 isym->st_shndx = SHN_UNDEF;
4192 }
4193 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4194 value -= sec->vma;
4195 }
4196
4197 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4198 isym->st_name);
4199 if (name == NULL)
4200 goto error_free_vers;
4201
4202 if (isym->st_shndx == SHN_COMMON
4203 && (abfd->flags & BFD_PLUGIN) != 0)
4204 {
4205 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4206
4207 if (xc == NULL)
4208 {
4209 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4210 | SEC_EXCLUDE);
4211 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4212 if (xc == NULL)
4213 goto error_free_vers;
4214 }
4215 sec = xc;
4216 }
4217 else if (isym->st_shndx == SHN_COMMON
4218 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4219 && !bfd_link_relocatable (info))
4220 {
4221 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4222
4223 if (tcomm == NULL)
4224 {
4225 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4226 | SEC_LINKER_CREATED);
4227 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4228 if (tcomm == NULL)
4229 goto error_free_vers;
4230 }
4231 sec = tcomm;
4232 }
4233 else if (bed->elf_add_symbol_hook)
4234 {
4235 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4236 &sec, &value))
4237 goto error_free_vers;
4238
4239 /* The hook function sets the name to NULL if this symbol
4240 should be skipped for some reason. */
4241 if (name == NULL)
4242 continue;
4243 }
4244
4245 /* Sanity check that all possibilities were handled. */
4246 if (sec == NULL)
4247 {
4248 bfd_set_error (bfd_error_bad_value);
4249 goto error_free_vers;
4250 }
4251
4252 /* Silently discard TLS symbols from --just-syms. There's
4253 no way to combine a static TLS block with a new TLS block
4254 for this executable. */
4255 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4256 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4257 continue;
4258
4259 if (bfd_is_und_section (sec)
4260 || bfd_is_com_section (sec))
4261 definition = FALSE;
4262 else
4263 definition = TRUE;
4264
4265 size_change_ok = FALSE;
4266 type_change_ok = bed->type_change_ok;
4267 old_weak = FALSE;
4268 matched = FALSE;
4269 old_alignment = 0;
4270 old_bfd = NULL;
4271 new_sec = sec;
4272
4273 if (is_elf_hash_table (htab))
4274 {
4275 Elf_Internal_Versym iver;
4276 unsigned int vernum = 0;
4277 bfd_boolean skip;
4278
4279 if (ever == NULL)
4280 {
4281 if (info->default_imported_symver)
4282 /* Use the default symbol version created earlier. */
4283 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4284 else
4285 iver.vs_vers = 0;
4286 }
4287 else
4288 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4289
4290 vernum = iver.vs_vers & VERSYM_VERSION;
4291
4292 /* If this is a hidden symbol, or if it is not version
4293 1, we append the version name to the symbol name.
4294 However, we do not modify a non-hidden absolute symbol
4295 if it is not a function, because it might be the version
4296 symbol itself. FIXME: What if it isn't? */
4297 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4298 || (vernum > 1
4299 && (!bfd_is_abs_section (sec)
4300 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4301 {
4302 const char *verstr;
4303 size_t namelen, verlen, newlen;
4304 char *newname, *p;
4305
4306 if (isym->st_shndx != SHN_UNDEF)
4307 {
4308 if (vernum > elf_tdata (abfd)->cverdefs)
4309 verstr = NULL;
4310 else if (vernum > 1)
4311 verstr =
4312 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4313 else
4314 verstr = "";
4315
4316 if (verstr == NULL)
4317 {
4318 (*_bfd_error_handler)
4319 (_("%B: %s: invalid version %u (max %d)"),
4320 abfd, name, vernum,
4321 elf_tdata (abfd)->cverdefs);
4322 bfd_set_error (bfd_error_bad_value);
4323 goto error_free_vers;
4324 }
4325 }
4326 else
4327 {
4328 /* We cannot simply test for the number of
4329 entries in the VERNEED section since the
4330 numbers for the needed versions do not start
4331 at 0. */
4332 Elf_Internal_Verneed *t;
4333
4334 verstr = NULL;
4335 for (t = elf_tdata (abfd)->verref;
4336 t != NULL;
4337 t = t->vn_nextref)
4338 {
4339 Elf_Internal_Vernaux *a;
4340
4341 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4342 {
4343 if (a->vna_other == vernum)
4344 {
4345 verstr = a->vna_nodename;
4346 break;
4347 }
4348 }
4349 if (a != NULL)
4350 break;
4351 }
4352 if (verstr == NULL)
4353 {
4354 (*_bfd_error_handler)
4355 (_("%B: %s: invalid needed version %d"),
4356 abfd, name, vernum);
4357 bfd_set_error (bfd_error_bad_value);
4358 goto error_free_vers;
4359 }
4360 }
4361
4362 namelen = strlen (name);
4363 verlen = strlen (verstr);
4364 newlen = namelen + verlen + 2;
4365 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4366 && isym->st_shndx != SHN_UNDEF)
4367 ++newlen;
4368
4369 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4370 if (newname == NULL)
4371 goto error_free_vers;
4372 memcpy (newname, name, namelen);
4373 p = newname + namelen;
4374 *p++ = ELF_VER_CHR;
4375 /* If this is a defined non-hidden version symbol,
4376 we add another @ to the name. This indicates the
4377 default version of the symbol. */
4378 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4379 && isym->st_shndx != SHN_UNDEF)
4380 *p++ = ELF_VER_CHR;
4381 memcpy (p, verstr, verlen + 1);
4382
4383 name = newname;
4384 }
4385
4386 /* If this symbol has default visibility and the user has
4387 requested we not re-export it, then mark it as hidden. */
4388 if (!bfd_is_und_section (sec)
4389 && !dynamic
4390 && abfd->no_export
4391 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4392 isym->st_other = (STV_HIDDEN
4393 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4394
4395 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4396 sym_hash, &old_bfd, &old_weak,
4397 &old_alignment, &skip, &override,
4398 &type_change_ok, &size_change_ok,
4399 &matched))
4400 goto error_free_vers;
4401
4402 if (skip)
4403 continue;
4404
4405 /* Override a definition only if the new symbol matches the
4406 existing one. */
4407 if (override && matched)
4408 definition = FALSE;
4409
4410 h = *sym_hash;
4411 while (h->root.type == bfd_link_hash_indirect
4412 || h->root.type == bfd_link_hash_warning)
4413 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4414
4415 if (elf_tdata (abfd)->verdef != NULL
4416 && vernum > 1
4417 && definition)
4418 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4419 }
4420
4421 if (! (_bfd_generic_link_add_one_symbol
4422 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4423 (struct bfd_link_hash_entry **) sym_hash)))
4424 goto error_free_vers;
4425
4426 if ((flags & BSF_GNU_UNIQUE)
4427 && (abfd->flags & DYNAMIC) == 0
4428 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4429 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
4430
4431 h = *sym_hash;
4432 /* We need to make sure that indirect symbol dynamic flags are
4433 updated. */
4434 hi = h;
4435 while (h->root.type == bfd_link_hash_indirect
4436 || h->root.type == bfd_link_hash_warning)
4437 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4438
4439 /* Setting the index to -3 tells elf_link_output_extsym that
4440 this symbol is defined in a discarded section. */
4441 if (discarded)
4442 h->indx = -3;
4443
4444 *sym_hash = h;
4445
4446 new_weak = (flags & BSF_WEAK) != 0;
4447 new_weakdef = FALSE;
4448 if (dynamic
4449 && definition
4450 && new_weak
4451 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4452 && is_elf_hash_table (htab)
4453 && h->u.weakdef == NULL)
4454 {
4455 /* Keep a list of all weak defined non function symbols from
4456 a dynamic object, using the weakdef field. Later in this
4457 function we will set the weakdef field to the correct
4458 value. We only put non-function symbols from dynamic
4459 objects on this list, because that happens to be the only
4460 time we need to know the normal symbol corresponding to a
4461 weak symbol, and the information is time consuming to
4462 figure out. If the weakdef field is not already NULL,
4463 then this symbol was already defined by some previous
4464 dynamic object, and we will be using that previous
4465 definition anyhow. */
4466
4467 h->u.weakdef = weaks;
4468 weaks = h;
4469 new_weakdef = TRUE;
4470 }
4471
4472 /* Set the alignment of a common symbol. */
4473 if ((common || bfd_is_com_section (sec))
4474 && h->root.type == bfd_link_hash_common)
4475 {
4476 unsigned int align;
4477
4478 if (common)
4479 align = bfd_log2 (isym->st_value);
4480 else
4481 {
4482 /* The new symbol is a common symbol in a shared object.
4483 We need to get the alignment from the section. */
4484 align = new_sec->alignment_power;
4485 }
4486 if (align > old_alignment)
4487 h->root.u.c.p->alignment_power = align;
4488 else
4489 h->root.u.c.p->alignment_power = old_alignment;
4490 }
4491
4492 if (is_elf_hash_table (htab))
4493 {
4494 /* Set a flag in the hash table entry indicating the type of
4495 reference or definition we just found. A dynamic symbol
4496 is one which is referenced or defined by both a regular
4497 object and a shared object. */
4498 bfd_boolean dynsym = FALSE;
4499
4500 /* Plugin symbols aren't normal. Don't set def_regular or
4501 ref_regular for them, or make them dynamic. */
4502 if ((abfd->flags & BFD_PLUGIN) != 0)
4503 ;
4504 else if (! dynamic)
4505 {
4506 if (! definition)
4507 {
4508 h->ref_regular = 1;
4509 if (bind != STB_WEAK)
4510 h->ref_regular_nonweak = 1;
4511 }
4512 else
4513 {
4514 h->def_regular = 1;
4515 if (h->def_dynamic)
4516 {
4517 h->def_dynamic = 0;
4518 h->ref_dynamic = 1;
4519 }
4520 }
4521
4522 /* If the indirect symbol has been forced local, don't
4523 make the real symbol dynamic. */
4524 if ((h == hi || !hi->forced_local)
4525 && (bfd_link_dll (info)
4526 || h->def_dynamic
4527 || h->ref_dynamic))
4528 dynsym = TRUE;
4529 }
4530 else
4531 {
4532 if (! definition)
4533 {
4534 h->ref_dynamic = 1;
4535 hi->ref_dynamic = 1;
4536 }
4537 else
4538 {
4539 h->def_dynamic = 1;
4540 hi->def_dynamic = 1;
4541 }
4542
4543 /* If the indirect symbol has been forced local, don't
4544 make the real symbol dynamic. */
4545 if ((h == hi || !hi->forced_local)
4546 && (h->def_regular
4547 || h->ref_regular
4548 || (h->u.weakdef != NULL
4549 && ! new_weakdef
4550 && h->u.weakdef->dynindx != -1)))
4551 dynsym = TRUE;
4552 }
4553
4554 /* Check to see if we need to add an indirect symbol for
4555 the default name. */
4556 if (definition
4557 || (!override && h->root.type == bfd_link_hash_common))
4558 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4559 sec, value, &old_bfd, &dynsym))
4560 goto error_free_vers;
4561
4562 /* Check the alignment when a common symbol is involved. This
4563 can change when a common symbol is overridden by a normal
4564 definition or a common symbol is ignored due to the old
4565 normal definition. We need to make sure the maximum
4566 alignment is maintained. */
4567 if ((old_alignment || common)
4568 && h->root.type != bfd_link_hash_common)
4569 {
4570 unsigned int common_align;
4571 unsigned int normal_align;
4572 unsigned int symbol_align;
4573 bfd *normal_bfd;
4574 bfd *common_bfd;
4575
4576 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4577 || h->root.type == bfd_link_hash_defweak);
4578
4579 symbol_align = ffs (h->root.u.def.value) - 1;
4580 if (h->root.u.def.section->owner != NULL
4581 && (h->root.u.def.section->owner->flags
4582 & (DYNAMIC | BFD_PLUGIN)) == 0)
4583 {
4584 normal_align = h->root.u.def.section->alignment_power;
4585 if (normal_align > symbol_align)
4586 normal_align = symbol_align;
4587 }
4588 else
4589 normal_align = symbol_align;
4590
4591 if (old_alignment)
4592 {
4593 common_align = old_alignment;
4594 common_bfd = old_bfd;
4595 normal_bfd = abfd;
4596 }
4597 else
4598 {
4599 common_align = bfd_log2 (isym->st_value);
4600 common_bfd = abfd;
4601 normal_bfd = old_bfd;
4602 }
4603
4604 if (normal_align < common_align)
4605 {
4606 /* PR binutils/2735 */
4607 if (normal_bfd == NULL)
4608 (*_bfd_error_handler)
4609 (_("Warning: alignment %u of common symbol `%s' in %B is"
4610 " greater than the alignment (%u) of its section %A"),
4611 common_bfd, h->root.u.def.section,
4612 1 << common_align, name, 1 << normal_align);
4613 else
4614 (*_bfd_error_handler)
4615 (_("Warning: alignment %u of symbol `%s' in %B"
4616 " is smaller than %u in %B"),
4617 normal_bfd, common_bfd,
4618 1 << normal_align, name, 1 << common_align);
4619 }
4620 }
4621
4622 /* Remember the symbol size if it isn't undefined. */
4623 if (isym->st_size != 0
4624 && isym->st_shndx != SHN_UNDEF
4625 && (definition || h->size == 0))
4626 {
4627 if (h->size != 0
4628 && h->size != isym->st_size
4629 && ! size_change_ok)
4630 (*_bfd_error_handler)
4631 (_("Warning: size of symbol `%s' changed"
4632 " from %lu in %B to %lu in %B"),
4633 old_bfd, abfd,
4634 name, (unsigned long) h->size,
4635 (unsigned long) isym->st_size);
4636
4637 h->size = isym->st_size;
4638 }
4639
4640 /* If this is a common symbol, then we always want H->SIZE
4641 to be the size of the common symbol. The code just above
4642 won't fix the size if a common symbol becomes larger. We
4643 don't warn about a size change here, because that is
4644 covered by --warn-common. Allow changes between different
4645 function types. */
4646 if (h->root.type == bfd_link_hash_common)
4647 h->size = h->root.u.c.size;
4648
4649 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4650 && ((definition && !new_weak)
4651 || (old_weak && h->root.type == bfd_link_hash_common)
4652 || h->type == STT_NOTYPE))
4653 {
4654 unsigned int type = ELF_ST_TYPE (isym->st_info);
4655
4656 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4657 symbol. */
4658 if (type == STT_GNU_IFUNC
4659 && (abfd->flags & DYNAMIC) != 0)
4660 type = STT_FUNC;
4661
4662 if (h->type != type)
4663 {
4664 if (h->type != STT_NOTYPE && ! type_change_ok)
4665 (*_bfd_error_handler)
4666 (_("Warning: type of symbol `%s' changed"
4667 " from %d to %d in %B"),
4668 abfd, name, h->type, type);
4669
4670 h->type = type;
4671 }
4672 }
4673
4674 /* Merge st_other field. */
4675 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4676
4677 /* We don't want to make debug symbol dynamic. */
4678 if (definition
4679 && (sec->flags & SEC_DEBUGGING)
4680 && !bfd_link_relocatable (info))
4681 dynsym = FALSE;
4682
4683 /* Nor should we make plugin symbols dynamic. */
4684 if ((abfd->flags & BFD_PLUGIN) != 0)
4685 dynsym = FALSE;
4686
4687 if (definition)
4688 {
4689 h->target_internal = isym->st_target_internal;
4690 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4691 }
4692
4693 if (definition && !dynamic)
4694 {
4695 char *p = strchr (name, ELF_VER_CHR);
4696 if (p != NULL && p[1] != ELF_VER_CHR)
4697 {
4698 /* Queue non-default versions so that .symver x, x@FOO
4699 aliases can be checked. */
4700 if (!nondeflt_vers)
4701 {
4702 amt = ((isymend - isym + 1)
4703 * sizeof (struct elf_link_hash_entry *));
4704 nondeflt_vers
4705 = (struct elf_link_hash_entry **) bfd_malloc (amt);
4706 if (!nondeflt_vers)
4707 goto error_free_vers;
4708 }
4709 nondeflt_vers[nondeflt_vers_cnt++] = h;
4710 }
4711 }
4712
4713 if (dynsym && h->dynindx == -1)
4714 {
4715 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4716 goto error_free_vers;
4717 if (h->u.weakdef != NULL
4718 && ! new_weakdef
4719 && h->u.weakdef->dynindx == -1)
4720 {
4721 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4722 goto error_free_vers;
4723 }
4724 }
4725 else if (h->dynindx != -1)
4726 /* If the symbol already has a dynamic index, but
4727 visibility says it should not be visible, turn it into
4728 a local symbol. */
4729 switch (ELF_ST_VISIBILITY (h->other))
4730 {
4731 case STV_INTERNAL:
4732 case STV_HIDDEN:
4733 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4734 dynsym = FALSE;
4735 break;
4736 }
4737
4738 /* Don't add DT_NEEDED for references from the dummy bfd nor
4739 for unmatched symbol. */
4740 if (!add_needed
4741 && matched
4742 && definition
4743 && ((dynsym
4744 && h->ref_regular_nonweak
4745 && (old_bfd == NULL
4746 || (old_bfd->flags & BFD_PLUGIN) == 0))
4747 || (h->ref_dynamic_nonweak
4748 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4749 && !on_needed_list (elf_dt_name (abfd),
4750 htab->needed, NULL))))
4751 {
4752 int ret;
4753 const char *soname = elf_dt_name (abfd);
4754
4755 info->callbacks->minfo ("%!", soname, old_bfd,
4756 h->root.root.string);
4757
4758 /* A symbol from a library loaded via DT_NEEDED of some
4759 other library is referenced by a regular object.
4760 Add a DT_NEEDED entry for it. Issue an error if
4761 --no-add-needed is used and the reference was not
4762 a weak one. */
4763 if (old_bfd != NULL
4764 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4765 {
4766 (*_bfd_error_handler)
4767 (_("%B: undefined reference to symbol '%s'"),
4768 old_bfd, name);
4769 bfd_set_error (bfd_error_missing_dso);
4770 goto error_free_vers;
4771 }
4772
4773 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4774 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
4775
4776 add_needed = TRUE;
4777 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4778 if (ret < 0)
4779 goto error_free_vers;
4780
4781 BFD_ASSERT (ret == 0);
4782 }
4783 }
4784 }
4785
4786 if (extversym != NULL)
4787 {
4788 free (extversym);
4789 extversym = NULL;
4790 }
4791
4792 if (isymbuf != NULL)
4793 {
4794 free (isymbuf);
4795 isymbuf = NULL;
4796 }
4797
4798 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4799 {
4800 unsigned int i;
4801
4802 /* Restore the symbol table. */
4803 old_ent = (char *) old_tab + tabsize;
4804 memset (elf_sym_hashes (abfd), 0,
4805 extsymcount * sizeof (struct elf_link_hash_entry *));
4806 htab->root.table.table = old_table;
4807 htab->root.table.size = old_size;
4808 htab->root.table.count = old_count;
4809 memcpy (htab->root.table.table, old_tab, tabsize);
4810 htab->root.undefs = old_undefs;
4811 htab->root.undefs_tail = old_undefs_tail;
4812 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
4813 free (old_strtab);
4814 old_strtab = NULL;
4815 for (i = 0; i < htab->root.table.size; i++)
4816 {
4817 struct bfd_hash_entry *p;
4818 struct elf_link_hash_entry *h;
4819 bfd_size_type size;
4820 unsigned int alignment_power;
4821
4822 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4823 {
4824 h = (struct elf_link_hash_entry *) p;
4825 if (h->root.type == bfd_link_hash_warning)
4826 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4827
4828 /* Preserve the maximum alignment and size for common
4829 symbols even if this dynamic lib isn't on DT_NEEDED
4830 since it can still be loaded at run time by another
4831 dynamic lib. */
4832 if (h->root.type == bfd_link_hash_common)
4833 {
4834 size = h->root.u.c.size;
4835 alignment_power = h->root.u.c.p->alignment_power;
4836 }
4837 else
4838 {
4839 size = 0;
4840 alignment_power = 0;
4841 }
4842 memcpy (p, old_ent, htab->root.table.entsize);
4843 old_ent = (char *) old_ent + htab->root.table.entsize;
4844 h = (struct elf_link_hash_entry *) p;
4845 if (h->root.type == bfd_link_hash_warning)
4846 {
4847 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4848 old_ent = (char *) old_ent + htab->root.table.entsize;
4849 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4850 }
4851 if (h->root.type == bfd_link_hash_common)
4852 {
4853 if (size > h->root.u.c.size)
4854 h->root.u.c.size = size;
4855 if (alignment_power > h->root.u.c.p->alignment_power)
4856 h->root.u.c.p->alignment_power = alignment_power;
4857 }
4858 }
4859 }
4860
4861 /* Make a special call to the linker "notice" function to
4862 tell it that symbols added for crefs may need to be removed. */
4863 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
4864 goto error_free_vers;
4865
4866 free (old_tab);
4867 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4868 alloc_mark);
4869 if (nondeflt_vers != NULL)
4870 free (nondeflt_vers);
4871 return TRUE;
4872 }
4873
4874 if (old_tab != NULL)
4875 {
4876 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
4877 goto error_free_vers;
4878 free (old_tab);
4879 old_tab = NULL;
4880 }
4881
4882 /* Now that all the symbols from this input file are created, if
4883 not performing a relocatable link, handle .symver foo, foo@BAR
4884 such that any relocs against foo become foo@BAR. */
4885 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4886 {
4887 size_t cnt, symidx;
4888
4889 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4890 {
4891 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4892 char *shortname, *p;
4893
4894 p = strchr (h->root.root.string, ELF_VER_CHR);
4895 if (p == NULL
4896 || (h->root.type != bfd_link_hash_defined
4897 && h->root.type != bfd_link_hash_defweak))
4898 continue;
4899
4900 amt = p - h->root.root.string;
4901 shortname = (char *) bfd_malloc (amt + 1);
4902 if (!shortname)
4903 goto error_free_vers;
4904 memcpy (shortname, h->root.root.string, amt);
4905 shortname[amt] = '\0';
4906
4907 hi = (struct elf_link_hash_entry *)
4908 bfd_link_hash_lookup (&htab->root, shortname,
4909 FALSE, FALSE, FALSE);
4910 if (hi != NULL
4911 && hi->root.type == h->root.type
4912 && hi->root.u.def.value == h->root.u.def.value
4913 && hi->root.u.def.section == h->root.u.def.section)
4914 {
4915 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4916 hi->root.type = bfd_link_hash_indirect;
4917 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4918 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4919 sym_hash = elf_sym_hashes (abfd);
4920 if (sym_hash)
4921 for (symidx = 0; symidx < extsymcount; ++symidx)
4922 if (sym_hash[symidx] == hi)
4923 {
4924 sym_hash[symidx] = h;
4925 break;
4926 }
4927 }
4928 free (shortname);
4929 }
4930 free (nondeflt_vers);
4931 nondeflt_vers = NULL;
4932 }
4933
4934 /* Now set the weakdefs field correctly for all the weak defined
4935 symbols we found. The only way to do this is to search all the
4936 symbols. Since we only need the information for non functions in
4937 dynamic objects, that's the only time we actually put anything on
4938 the list WEAKS. We need this information so that if a regular
4939 object refers to a symbol defined weakly in a dynamic object, the
4940 real symbol in the dynamic object is also put in the dynamic
4941 symbols; we also must arrange for both symbols to point to the
4942 same memory location. We could handle the general case of symbol
4943 aliasing, but a general symbol alias can only be generated in
4944 assembler code, handling it correctly would be very time
4945 consuming, and other ELF linkers don't handle general aliasing
4946 either. */
4947 if (weaks != NULL)
4948 {
4949 struct elf_link_hash_entry **hpp;
4950 struct elf_link_hash_entry **hppend;
4951 struct elf_link_hash_entry **sorted_sym_hash;
4952 struct elf_link_hash_entry *h;
4953 size_t sym_count;
4954
4955 /* Since we have to search the whole symbol list for each weak
4956 defined symbol, search time for N weak defined symbols will be
4957 O(N^2). Binary search will cut it down to O(NlogN). */
4958 amt = extsymcount;
4959 amt *= sizeof (struct elf_link_hash_entry *);
4960 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4961 if (sorted_sym_hash == NULL)
4962 goto error_return;
4963 sym_hash = sorted_sym_hash;
4964 hpp = elf_sym_hashes (abfd);
4965 hppend = hpp + extsymcount;
4966 sym_count = 0;
4967 for (; hpp < hppend; hpp++)
4968 {
4969 h = *hpp;
4970 if (h != NULL
4971 && h->root.type == bfd_link_hash_defined
4972 && !bed->is_function_type (h->type))
4973 {
4974 *sym_hash = h;
4975 sym_hash++;
4976 sym_count++;
4977 }
4978 }
4979
4980 qsort (sorted_sym_hash, sym_count,
4981 sizeof (struct elf_link_hash_entry *),
4982 elf_sort_symbol);
4983
4984 while (weaks != NULL)
4985 {
4986 struct elf_link_hash_entry *hlook;
4987 asection *slook;
4988 bfd_vma vlook;
4989 size_t i, j, idx = 0;
4990
4991 hlook = weaks;
4992 weaks = hlook->u.weakdef;
4993 hlook->u.weakdef = NULL;
4994
4995 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4996 || hlook->root.type == bfd_link_hash_defweak
4997 || hlook->root.type == bfd_link_hash_common
4998 || hlook->root.type == bfd_link_hash_indirect);
4999 slook = hlook->root.u.def.section;
5000 vlook = hlook->root.u.def.value;
5001
5002 i = 0;
5003 j = sym_count;
5004 while (i != j)
5005 {
5006 bfd_signed_vma vdiff;
5007 idx = (i + j) / 2;
5008 h = sorted_sym_hash[idx];
5009 vdiff = vlook - h->root.u.def.value;
5010 if (vdiff < 0)
5011 j = idx;
5012 else if (vdiff > 0)
5013 i = idx + 1;
5014 else
5015 {
5016 int sdiff = slook->id - h->root.u.def.section->id;
5017 if (sdiff < 0)
5018 j = idx;
5019 else if (sdiff > 0)
5020 i = idx + 1;
5021 else
5022 break;
5023 }
5024 }
5025
5026 /* We didn't find a value/section match. */
5027 if (i == j)
5028 continue;
5029
5030 /* With multiple aliases, or when the weak symbol is already
5031 strongly defined, we have multiple matching symbols and
5032 the binary search above may land on any of them. Step
5033 one past the matching symbol(s). */
5034 while (++idx != j)
5035 {
5036 h = sorted_sym_hash[idx];
5037 if (h->root.u.def.section != slook
5038 || h->root.u.def.value != vlook)
5039 break;
5040 }
5041
5042 /* Now look back over the aliases. Since we sorted by size
5043 as well as value and section, we'll choose the one with
5044 the largest size. */
5045 while (idx-- != i)
5046 {
5047 h = sorted_sym_hash[idx];
5048
5049 /* Stop if value or section doesn't match. */
5050 if (h->root.u.def.section != slook
5051 || h->root.u.def.value != vlook)
5052 break;
5053 else if (h != hlook)
5054 {
5055 hlook->u.weakdef = h;
5056
5057 /* If the weak definition is in the list of dynamic
5058 symbols, make sure the real definition is put
5059 there as well. */
5060 if (hlook->dynindx != -1 && h->dynindx == -1)
5061 {
5062 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5063 {
5064 err_free_sym_hash:
5065 free (sorted_sym_hash);
5066 goto error_return;
5067 }
5068 }
5069
5070 /* If the real definition is in the list of dynamic
5071 symbols, make sure the weak definition is put
5072 there as well. If we don't do this, then the
5073 dynamic loader might not merge the entries for the
5074 real definition and the weak definition. */
5075 if (h->dynindx != -1 && hlook->dynindx == -1)
5076 {
5077 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5078 goto err_free_sym_hash;
5079 }
5080 break;
5081 }
5082 }
5083 }
5084
5085 free (sorted_sym_hash);
5086 }
5087
5088 if (bed->check_directives
5089 && !(*bed->check_directives) (abfd, info))
5090 return FALSE;
5091
5092 if (!info->check_relocs_after_open_input
5093 && !_bfd_elf_link_check_relocs (abfd, info))
5094 return FALSE;
5095
5096 /* If this is a non-traditional link, try to optimize the handling
5097 of the .stab/.stabstr sections. */
5098 if (! dynamic
5099 && ! info->traditional_format
5100 && is_elf_hash_table (htab)
5101 && (info->strip != strip_all && info->strip != strip_debugger))
5102 {
5103 asection *stabstr;
5104
5105 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5106 if (stabstr != NULL)
5107 {
5108 bfd_size_type string_offset = 0;
5109 asection *stab;
5110
5111 for (stab = abfd->sections; stab; stab = stab->next)
5112 if (CONST_STRNEQ (stab->name, ".stab")
5113 && (!stab->name[5] ||
5114 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5115 && (stab->flags & SEC_MERGE) == 0
5116 && !bfd_is_abs_section (stab->output_section))
5117 {
5118 struct bfd_elf_section_data *secdata;
5119
5120 secdata = elf_section_data (stab);
5121 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5122 stabstr, &secdata->sec_info,
5123 &string_offset))
5124 goto error_return;
5125 if (secdata->sec_info)
5126 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5127 }
5128 }
5129 }
5130
5131 if (is_elf_hash_table (htab) && add_needed)
5132 {
5133 /* Add this bfd to the loaded list. */
5134 struct elf_link_loaded_list *n;
5135
5136 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5137 if (n == NULL)
5138 goto error_return;
5139 n->abfd = abfd;
5140 n->next = htab->loaded;
5141 htab->loaded = n;
5142 }
5143
5144 return TRUE;
5145
5146 error_free_vers:
5147 if (old_tab != NULL)
5148 free (old_tab);
5149 if (old_strtab != NULL)
5150 free (old_strtab);
5151 if (nondeflt_vers != NULL)
5152 free (nondeflt_vers);
5153 if (extversym != NULL)
5154 free (extversym);
5155 error_free_sym:
5156 if (isymbuf != NULL)
5157 free (isymbuf);
5158 error_return:
5159 return FALSE;
5160 }
5161
5162 /* Return the linker hash table entry of a symbol that might be
5163 satisfied by an archive symbol. Return -1 on error. */
5164
5165 struct elf_link_hash_entry *
5166 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5167 struct bfd_link_info *info,
5168 const char *name)
5169 {
5170 struct elf_link_hash_entry *h;
5171 char *p, *copy;
5172 size_t len, first;
5173
5174 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5175 if (h != NULL)
5176 return h;
5177
5178 /* If this is a default version (the name contains @@), look up the
5179 symbol again with only one `@' as well as without the version.
5180 The effect is that references to the symbol with and without the
5181 version will be matched by the default symbol in the archive. */
5182
5183 p = strchr (name, ELF_VER_CHR);
5184 if (p == NULL || p[1] != ELF_VER_CHR)
5185 return h;
5186
5187 /* First check with only one `@'. */
5188 len = strlen (name);
5189 copy = (char *) bfd_alloc (abfd, len);
5190 if (copy == NULL)
5191 return (struct elf_link_hash_entry *) 0 - 1;
5192
5193 first = p - name + 1;
5194 memcpy (copy, name, first);
5195 memcpy (copy + first, name + first + 1, len - first);
5196
5197 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5198 if (h == NULL)
5199 {
5200 /* We also need to check references to the symbol without the
5201 version. */
5202 copy[first - 1] = '\0';
5203 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5204 FALSE, FALSE, TRUE);
5205 }
5206
5207 bfd_release (abfd, copy);
5208 return h;
5209 }
5210
5211 /* Add symbols from an ELF archive file to the linker hash table. We
5212 don't use _bfd_generic_link_add_archive_symbols because we need to
5213 handle versioned symbols.
5214
5215 Fortunately, ELF archive handling is simpler than that done by
5216 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5217 oddities. In ELF, if we find a symbol in the archive map, and the
5218 symbol is currently undefined, we know that we must pull in that
5219 object file.
5220
5221 Unfortunately, we do have to make multiple passes over the symbol
5222 table until nothing further is resolved. */
5223
5224 static bfd_boolean
5225 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5226 {
5227 symindex c;
5228 unsigned char *included = NULL;
5229 carsym *symdefs;
5230 bfd_boolean loop;
5231 bfd_size_type amt;
5232 const struct elf_backend_data *bed;
5233 struct elf_link_hash_entry * (*archive_symbol_lookup)
5234 (bfd *, struct bfd_link_info *, const char *);
5235
5236 if (! bfd_has_map (abfd))
5237 {
5238 /* An empty archive is a special case. */
5239 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5240 return TRUE;
5241 bfd_set_error (bfd_error_no_armap);
5242 return FALSE;
5243 }
5244
5245 /* Keep track of all symbols we know to be already defined, and all
5246 files we know to be already included. This is to speed up the
5247 second and subsequent passes. */
5248 c = bfd_ardata (abfd)->symdef_count;
5249 if (c == 0)
5250 return TRUE;
5251 amt = c;
5252 amt *= sizeof (*included);
5253 included = (unsigned char *) bfd_zmalloc (amt);
5254 if (included == NULL)
5255 return FALSE;
5256
5257 symdefs = bfd_ardata (abfd)->symdefs;
5258 bed = get_elf_backend_data (abfd);
5259 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5260
5261 do
5262 {
5263 file_ptr last;
5264 symindex i;
5265 carsym *symdef;
5266 carsym *symdefend;
5267
5268 loop = FALSE;
5269 last = -1;
5270
5271 symdef = symdefs;
5272 symdefend = symdef + c;
5273 for (i = 0; symdef < symdefend; symdef++, i++)
5274 {
5275 struct elf_link_hash_entry *h;
5276 bfd *element;
5277 struct bfd_link_hash_entry *undefs_tail;
5278 symindex mark;
5279
5280 if (included[i])
5281 continue;
5282 if (symdef->file_offset == last)
5283 {
5284 included[i] = TRUE;
5285 continue;
5286 }
5287
5288 h = archive_symbol_lookup (abfd, info, symdef->name);
5289 if (h == (struct elf_link_hash_entry *) 0 - 1)
5290 goto error_return;
5291
5292 if (h == NULL)
5293 continue;
5294
5295 if (h->root.type == bfd_link_hash_common)
5296 {
5297 /* We currently have a common symbol. The archive map contains
5298 a reference to this symbol, so we may want to include it. We
5299 only want to include it however, if this archive element
5300 contains a definition of the symbol, not just another common
5301 declaration of it.
5302
5303 Unfortunately some archivers (including GNU ar) will put
5304 declarations of common symbols into their archive maps, as
5305 well as real definitions, so we cannot just go by the archive
5306 map alone. Instead we must read in the element's symbol
5307 table and check that to see what kind of symbol definition
5308 this is. */
5309 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5310 continue;
5311 }
5312 else if (h->root.type != bfd_link_hash_undefined)
5313 {
5314 if (h->root.type != bfd_link_hash_undefweak)
5315 /* Symbol must be defined. Don't check it again. */
5316 included[i] = TRUE;
5317 continue;
5318 }
5319
5320 /* We need to include this archive member. */
5321 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5322 if (element == NULL)
5323 goto error_return;
5324
5325 if (! bfd_check_format (element, bfd_object))
5326 goto error_return;
5327
5328 undefs_tail = info->hash->undefs_tail;
5329
5330 if (!(*info->callbacks
5331 ->add_archive_element) (info, element, symdef->name, &element))
5332 continue;
5333 if (!bfd_link_add_symbols (element, info))
5334 goto error_return;
5335
5336 /* If there are any new undefined symbols, we need to make
5337 another pass through the archive in order to see whether
5338 they can be defined. FIXME: This isn't perfect, because
5339 common symbols wind up on undefs_tail and because an
5340 undefined symbol which is defined later on in this pass
5341 does not require another pass. This isn't a bug, but it
5342 does make the code less efficient than it could be. */
5343 if (undefs_tail != info->hash->undefs_tail)
5344 loop = TRUE;
5345
5346 /* Look backward to mark all symbols from this object file
5347 which we have already seen in this pass. */
5348 mark = i;
5349 do
5350 {
5351 included[mark] = TRUE;
5352 if (mark == 0)
5353 break;
5354 --mark;
5355 }
5356 while (symdefs[mark].file_offset == symdef->file_offset);
5357
5358 /* We mark subsequent symbols from this object file as we go
5359 on through the loop. */
5360 last = symdef->file_offset;
5361 }
5362 }
5363 while (loop);
5364
5365 free (included);
5366
5367 return TRUE;
5368
5369 error_return:
5370 if (included != NULL)
5371 free (included);
5372 return FALSE;
5373 }
5374
5375 /* Given an ELF BFD, add symbols to the global hash table as
5376 appropriate. */
5377
5378 bfd_boolean
5379 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5380 {
5381 switch (bfd_get_format (abfd))
5382 {
5383 case bfd_object:
5384 return elf_link_add_object_symbols (abfd, info);
5385 case bfd_archive:
5386 return elf_link_add_archive_symbols (abfd, info);
5387 default:
5388 bfd_set_error (bfd_error_wrong_format);
5389 return FALSE;
5390 }
5391 }
5392
5393 struct hash_codes_info
5395 {
5396 unsigned long *hashcodes;
5397 bfd_boolean error;
5398 };
5399
5400 /* This function will be called though elf_link_hash_traverse to store
5401 all hash value of the exported symbols in an array. */
5402
5403 static bfd_boolean
5404 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5405 {
5406 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5407 const char *name;
5408 unsigned long ha;
5409 char *alc = NULL;
5410
5411 /* Ignore indirect symbols. These are added by the versioning code. */
5412 if (h->dynindx == -1)
5413 return TRUE;
5414
5415 name = h->root.root.string;
5416 if (h->versioned >= versioned)
5417 {
5418 char *p = strchr (name, ELF_VER_CHR);
5419 if (p != NULL)
5420 {
5421 alc = (char *) bfd_malloc (p - name + 1);
5422 if (alc == NULL)
5423 {
5424 inf->error = TRUE;
5425 return FALSE;
5426 }
5427 memcpy (alc, name, p - name);
5428 alc[p - name] = '\0';
5429 name = alc;
5430 }
5431 }
5432
5433 /* Compute the hash value. */
5434 ha = bfd_elf_hash (name);
5435
5436 /* Store the found hash value in the array given as the argument. */
5437 *(inf->hashcodes)++ = ha;
5438
5439 /* And store it in the struct so that we can put it in the hash table
5440 later. */
5441 h->u.elf_hash_value = ha;
5442
5443 if (alc != NULL)
5444 free (alc);
5445
5446 return TRUE;
5447 }
5448
5449 struct collect_gnu_hash_codes
5450 {
5451 bfd *output_bfd;
5452 const struct elf_backend_data *bed;
5453 unsigned long int nsyms;
5454 unsigned long int maskbits;
5455 unsigned long int *hashcodes;
5456 unsigned long int *hashval;
5457 unsigned long int *indx;
5458 unsigned long int *counts;
5459 bfd_vma *bitmask;
5460 bfd_byte *contents;
5461 long int min_dynindx;
5462 unsigned long int bucketcount;
5463 unsigned long int symindx;
5464 long int local_indx;
5465 long int shift1, shift2;
5466 unsigned long int mask;
5467 bfd_boolean error;
5468 };
5469
5470 /* This function will be called though elf_link_hash_traverse to store
5471 all hash value of the exported symbols in an array. */
5472
5473 static bfd_boolean
5474 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5475 {
5476 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5477 const char *name;
5478 unsigned long ha;
5479 char *alc = NULL;
5480
5481 /* Ignore indirect symbols. These are added by the versioning code. */
5482 if (h->dynindx == -1)
5483 return TRUE;
5484
5485 /* Ignore also local symbols and undefined symbols. */
5486 if (! (*s->bed->elf_hash_symbol) (h))
5487 return TRUE;
5488
5489 name = h->root.root.string;
5490 if (h->versioned >= versioned)
5491 {
5492 char *p = strchr (name, ELF_VER_CHR);
5493 if (p != NULL)
5494 {
5495 alc = (char *) bfd_malloc (p - name + 1);
5496 if (alc == NULL)
5497 {
5498 s->error = TRUE;
5499 return FALSE;
5500 }
5501 memcpy (alc, name, p - name);
5502 alc[p - name] = '\0';
5503 name = alc;
5504 }
5505 }
5506
5507 /* Compute the hash value. */
5508 ha = bfd_elf_gnu_hash (name);
5509
5510 /* Store the found hash value in the array for compute_bucket_count,
5511 and also for .dynsym reordering purposes. */
5512 s->hashcodes[s->nsyms] = ha;
5513 s->hashval[h->dynindx] = ha;
5514 ++s->nsyms;
5515 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5516 s->min_dynindx = h->dynindx;
5517
5518 if (alc != NULL)
5519 free (alc);
5520
5521 return TRUE;
5522 }
5523
5524 /* This function will be called though elf_link_hash_traverse to do
5525 final dynaminc symbol renumbering. */
5526
5527 static bfd_boolean
5528 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5529 {
5530 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5531 unsigned long int bucket;
5532 unsigned long int val;
5533
5534 /* Ignore indirect symbols. */
5535 if (h->dynindx == -1)
5536 return TRUE;
5537
5538 /* Ignore also local symbols and undefined symbols. */
5539 if (! (*s->bed->elf_hash_symbol) (h))
5540 {
5541 if (h->dynindx >= s->min_dynindx)
5542 h->dynindx = s->local_indx++;
5543 return TRUE;
5544 }
5545
5546 bucket = s->hashval[h->dynindx] % s->bucketcount;
5547 val = (s->hashval[h->dynindx] >> s->shift1)
5548 & ((s->maskbits >> s->shift1) - 1);
5549 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5550 s->bitmask[val]
5551 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5552 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5553 if (s->counts[bucket] == 1)
5554 /* Last element terminates the chain. */
5555 val |= 1;
5556 bfd_put_32 (s->output_bfd, val,
5557 s->contents + (s->indx[bucket] - s->symindx) * 4);
5558 --s->counts[bucket];
5559 h->dynindx = s->indx[bucket]++;
5560 return TRUE;
5561 }
5562
5563 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5564
5565 bfd_boolean
5566 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5567 {
5568 return !(h->forced_local
5569 || h->root.type == bfd_link_hash_undefined
5570 || h->root.type == bfd_link_hash_undefweak
5571 || ((h->root.type == bfd_link_hash_defined
5572 || h->root.type == bfd_link_hash_defweak)
5573 && h->root.u.def.section->output_section == NULL));
5574 }
5575
5576 /* Array used to determine the number of hash table buckets to use
5577 based on the number of symbols there are. If there are fewer than
5578 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5579 fewer than 37 we use 17 buckets, and so forth. We never use more
5580 than 32771 buckets. */
5581
5582 static const size_t elf_buckets[] =
5583 {
5584 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5585 16411, 32771, 0
5586 };
5587
5588 /* Compute bucket count for hashing table. We do not use a static set
5589 of possible tables sizes anymore. Instead we determine for all
5590 possible reasonable sizes of the table the outcome (i.e., the
5591 number of collisions etc) and choose the best solution. The
5592 weighting functions are not too simple to allow the table to grow
5593 without bounds. Instead one of the weighting factors is the size.
5594 Therefore the result is always a good payoff between few collisions
5595 (= short chain lengths) and table size. */
5596 static size_t
5597 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5598 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5599 unsigned long int nsyms,
5600 int gnu_hash)
5601 {
5602 size_t best_size = 0;
5603 unsigned long int i;
5604
5605 /* We have a problem here. The following code to optimize the table
5606 size requires an integer type with more the 32 bits. If
5607 BFD_HOST_U_64_BIT is set we know about such a type. */
5608 #ifdef BFD_HOST_U_64_BIT
5609 if (info->optimize)
5610 {
5611 size_t minsize;
5612 size_t maxsize;
5613 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5614 bfd *dynobj = elf_hash_table (info)->dynobj;
5615 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5616 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5617 unsigned long int *counts;
5618 bfd_size_type amt;
5619 unsigned int no_improvement_count = 0;
5620
5621 /* Possible optimization parameters: if we have NSYMS symbols we say
5622 that the hashing table must at least have NSYMS/4 and at most
5623 2*NSYMS buckets. */
5624 minsize = nsyms / 4;
5625 if (minsize == 0)
5626 minsize = 1;
5627 best_size = maxsize = nsyms * 2;
5628 if (gnu_hash)
5629 {
5630 if (minsize < 2)
5631 minsize = 2;
5632 if ((best_size & 31) == 0)
5633 ++best_size;
5634 }
5635
5636 /* Create array where we count the collisions in. We must use bfd_malloc
5637 since the size could be large. */
5638 amt = maxsize;
5639 amt *= sizeof (unsigned long int);
5640 counts = (unsigned long int *) bfd_malloc (amt);
5641 if (counts == NULL)
5642 return 0;
5643
5644 /* Compute the "optimal" size for the hash table. The criteria is a
5645 minimal chain length. The minor criteria is (of course) the size
5646 of the table. */
5647 for (i = minsize; i < maxsize; ++i)
5648 {
5649 /* Walk through the array of hashcodes and count the collisions. */
5650 BFD_HOST_U_64_BIT max;
5651 unsigned long int j;
5652 unsigned long int fact;
5653
5654 if (gnu_hash && (i & 31) == 0)
5655 continue;
5656
5657 memset (counts, '\0', i * sizeof (unsigned long int));
5658
5659 /* Determine how often each hash bucket is used. */
5660 for (j = 0; j < nsyms; ++j)
5661 ++counts[hashcodes[j] % i];
5662
5663 /* For the weight function we need some information about the
5664 pagesize on the target. This is information need not be 100%
5665 accurate. Since this information is not available (so far) we
5666 define it here to a reasonable default value. If it is crucial
5667 to have a better value some day simply define this value. */
5668 # ifndef BFD_TARGET_PAGESIZE
5669 # define BFD_TARGET_PAGESIZE (4096)
5670 # endif
5671
5672 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5673 and the chains. */
5674 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5675
5676 # if 1
5677 /* Variant 1: optimize for short chains. We add the squares
5678 of all the chain lengths (which favors many small chain
5679 over a few long chains). */
5680 for (j = 0; j < i; ++j)
5681 max += counts[j] * counts[j];
5682
5683 /* This adds penalties for the overall size of the table. */
5684 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5685 max *= fact * fact;
5686 # else
5687 /* Variant 2: Optimize a lot more for small table. Here we
5688 also add squares of the size but we also add penalties for
5689 empty slots (the +1 term). */
5690 for (j = 0; j < i; ++j)
5691 max += (1 + counts[j]) * (1 + counts[j]);
5692
5693 /* The overall size of the table is considered, but not as
5694 strong as in variant 1, where it is squared. */
5695 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5696 max *= fact;
5697 # endif
5698
5699 /* Compare with current best results. */
5700 if (max < best_chlen)
5701 {
5702 best_chlen = max;
5703 best_size = i;
5704 no_improvement_count = 0;
5705 }
5706 /* PR 11843: Avoid futile long searches for the best bucket size
5707 when there are a large number of symbols. */
5708 else if (++no_improvement_count == 100)
5709 break;
5710 }
5711
5712 free (counts);
5713 }
5714 else
5715 #endif /* defined (BFD_HOST_U_64_BIT) */
5716 {
5717 /* This is the fallback solution if no 64bit type is available or if we
5718 are not supposed to spend much time on optimizations. We select the
5719 bucket count using a fixed set of numbers. */
5720 for (i = 0; elf_buckets[i] != 0; i++)
5721 {
5722 best_size = elf_buckets[i];
5723 if (nsyms < elf_buckets[i + 1])
5724 break;
5725 }
5726 if (gnu_hash && best_size < 2)
5727 best_size = 2;
5728 }
5729
5730 return best_size;
5731 }
5732
5733 /* Size any SHT_GROUP section for ld -r. */
5734
5735 bfd_boolean
5736 _bfd_elf_size_group_sections (struct bfd_link_info *info)
5737 {
5738 bfd *ibfd;
5739
5740 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5741 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5742 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5743 return FALSE;
5744 return TRUE;
5745 }
5746
5747 /* Set a default stack segment size. The value in INFO wins. If it
5748 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5749 undefined it is initialized. */
5750
5751 bfd_boolean
5752 bfd_elf_stack_segment_size (bfd *output_bfd,
5753 struct bfd_link_info *info,
5754 const char *legacy_symbol,
5755 bfd_vma default_size)
5756 {
5757 struct elf_link_hash_entry *h = NULL;
5758
5759 /* Look for legacy symbol. */
5760 if (legacy_symbol)
5761 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5762 FALSE, FALSE, FALSE);
5763 if (h && (h->root.type == bfd_link_hash_defined
5764 || h->root.type == bfd_link_hash_defweak)
5765 && h->def_regular
5766 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5767 {
5768 /* The symbol has no type if specified on the command line. */
5769 h->type = STT_OBJECT;
5770 if (info->stacksize)
5771 (*_bfd_error_handler) (_("%B: stack size specified and %s set"),
5772 output_bfd, legacy_symbol);
5773 else if (h->root.u.def.section != bfd_abs_section_ptr)
5774 (*_bfd_error_handler) (_("%B: %s not absolute"),
5775 output_bfd, legacy_symbol);
5776 else
5777 info->stacksize = h->root.u.def.value;
5778 }
5779
5780 if (!info->stacksize)
5781 /* If the user didn't set a size, or explicitly inhibit the
5782 size, set it now. */
5783 info->stacksize = default_size;
5784
5785 /* Provide the legacy symbol, if it is referenced. */
5786 if (h && (h->root.type == bfd_link_hash_undefined
5787 || h->root.type == bfd_link_hash_undefweak))
5788 {
5789 struct bfd_link_hash_entry *bh = NULL;
5790
5791 if (!(_bfd_generic_link_add_one_symbol
5792 (info, output_bfd, legacy_symbol,
5793 BSF_GLOBAL, bfd_abs_section_ptr,
5794 info->stacksize >= 0 ? info->stacksize : 0,
5795 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5796 return FALSE;
5797
5798 h = (struct elf_link_hash_entry *) bh;
5799 h->def_regular = 1;
5800 h->type = STT_OBJECT;
5801 }
5802
5803 return TRUE;
5804 }
5805
5806 /* Set up the sizes and contents of the ELF dynamic sections. This is
5807 called by the ELF linker emulation before_allocation routine. We
5808 must set the sizes of the sections before the linker sets the
5809 addresses of the various sections. */
5810
5811 bfd_boolean
5812 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5813 const char *soname,
5814 const char *rpath,
5815 const char *filter_shlib,
5816 const char *audit,
5817 const char *depaudit,
5818 const char * const *auxiliary_filters,
5819 struct bfd_link_info *info,
5820 asection **sinterpptr)
5821 {
5822 size_t soname_indx;
5823 bfd *dynobj;
5824 const struct elf_backend_data *bed;
5825 struct elf_info_failed asvinfo;
5826
5827 *sinterpptr = NULL;
5828
5829 soname_indx = (size_t) -1;
5830
5831 if (!is_elf_hash_table (info->hash))
5832 return TRUE;
5833
5834 bed = get_elf_backend_data (output_bfd);
5835
5836 /* Any syms created from now on start with -1 in
5837 got.refcount/offset and plt.refcount/offset. */
5838 elf_hash_table (info)->init_got_refcount
5839 = elf_hash_table (info)->init_got_offset;
5840 elf_hash_table (info)->init_plt_refcount
5841 = elf_hash_table (info)->init_plt_offset;
5842
5843 if (bfd_link_relocatable (info)
5844 && !_bfd_elf_size_group_sections (info))
5845 return FALSE;
5846
5847 /* The backend may have to create some sections regardless of whether
5848 we're dynamic or not. */
5849 if (bed->elf_backend_always_size_sections
5850 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5851 return FALSE;
5852
5853 /* Determine any GNU_STACK segment requirements, after the backend
5854 has had a chance to set a default segment size. */
5855 if (info->execstack)
5856 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
5857 else if (info->noexecstack)
5858 elf_stack_flags (output_bfd) = PF_R | PF_W;
5859 else
5860 {
5861 bfd *inputobj;
5862 asection *notesec = NULL;
5863 int exec = 0;
5864
5865 for (inputobj = info->input_bfds;
5866 inputobj;
5867 inputobj = inputobj->link.next)
5868 {
5869 asection *s;
5870
5871 if (inputobj->flags
5872 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5873 continue;
5874 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5875 if (s)
5876 {
5877 if (s->flags & SEC_CODE)
5878 exec = PF_X;
5879 notesec = s;
5880 }
5881 else if (bed->default_execstack)
5882 exec = PF_X;
5883 }
5884 if (notesec || info->stacksize > 0)
5885 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
5886 if (notesec && exec && bfd_link_relocatable (info)
5887 && notesec->output_section != bfd_abs_section_ptr)
5888 notesec->output_section->flags |= SEC_CODE;
5889 }
5890
5891 dynobj = elf_hash_table (info)->dynobj;
5892
5893 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5894 {
5895 struct elf_info_failed eif;
5896 struct elf_link_hash_entry *h;
5897 asection *dynstr;
5898 struct bfd_elf_version_tree *t;
5899 struct bfd_elf_version_expr *d;
5900 asection *s;
5901 bfd_boolean all_defined;
5902
5903 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
5904 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
5905
5906 if (soname != NULL)
5907 {
5908 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5909 soname, TRUE);
5910 if (soname_indx == (size_t) -1
5911 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5912 return FALSE;
5913 }
5914
5915 if (info->symbolic)
5916 {
5917 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5918 return FALSE;
5919 info->flags |= DF_SYMBOLIC;
5920 }
5921
5922 if (rpath != NULL)
5923 {
5924 size_t indx;
5925 bfd_vma tag;
5926
5927 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5928 TRUE);
5929 if (indx == (size_t) -1)
5930 return FALSE;
5931
5932 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
5933 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
5934 return FALSE;
5935 }
5936
5937 if (filter_shlib != NULL)
5938 {
5939 size_t indx;
5940
5941 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5942 filter_shlib, TRUE);
5943 if (indx == (size_t) -1
5944 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5945 return FALSE;
5946 }
5947
5948 if (auxiliary_filters != NULL)
5949 {
5950 const char * const *p;
5951
5952 for (p = auxiliary_filters; *p != NULL; p++)
5953 {
5954 size_t indx;
5955
5956 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5957 *p, TRUE);
5958 if (indx == (size_t) -1
5959 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5960 return FALSE;
5961 }
5962 }
5963
5964 if (audit != NULL)
5965 {
5966 size_t indx;
5967
5968 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5969 TRUE);
5970 if (indx == (size_t) -1
5971 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5972 return FALSE;
5973 }
5974
5975 if (depaudit != NULL)
5976 {
5977 size_t indx;
5978
5979 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
5980 TRUE);
5981 if (indx == (size_t) -1
5982 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
5983 return FALSE;
5984 }
5985
5986 eif.info = info;
5987 eif.failed = FALSE;
5988
5989 /* If we are supposed to export all symbols into the dynamic symbol
5990 table (this is not the normal case), then do so. */
5991 if (info->export_dynamic
5992 || (bfd_link_executable (info) && info->dynamic))
5993 {
5994 elf_link_hash_traverse (elf_hash_table (info),
5995 _bfd_elf_export_symbol,
5996 &eif);
5997 if (eif.failed)
5998 return FALSE;
5999 }
6000
6001 /* Make all global versions with definition. */
6002 for (t = info->version_info; t != NULL; t = t->next)
6003 for (d = t->globals.list; d != NULL; d = d->next)
6004 if (!d->symver && d->literal)
6005 {
6006 const char *verstr, *name;
6007 size_t namelen, verlen, newlen;
6008 char *newname, *p, leading_char;
6009 struct elf_link_hash_entry *newh;
6010
6011 leading_char = bfd_get_symbol_leading_char (output_bfd);
6012 name = d->pattern;
6013 namelen = strlen (name) + (leading_char != '\0');
6014 verstr = t->name;
6015 verlen = strlen (verstr);
6016 newlen = namelen + verlen + 3;
6017
6018 newname = (char *) bfd_malloc (newlen);
6019 if (newname == NULL)
6020 return FALSE;
6021 newname[0] = leading_char;
6022 memcpy (newname + (leading_char != '\0'), name, namelen);
6023
6024 /* Check the hidden versioned definition. */
6025 p = newname + namelen;
6026 *p++ = ELF_VER_CHR;
6027 memcpy (p, verstr, verlen + 1);
6028 newh = elf_link_hash_lookup (elf_hash_table (info),
6029 newname, FALSE, FALSE,
6030 FALSE);
6031 if (newh == NULL
6032 || (newh->root.type != bfd_link_hash_defined
6033 && newh->root.type != bfd_link_hash_defweak))
6034 {
6035 /* Check the default versioned definition. */
6036 *p++ = ELF_VER_CHR;
6037 memcpy (p, verstr, verlen + 1);
6038 newh = elf_link_hash_lookup (elf_hash_table (info),
6039 newname, FALSE, FALSE,
6040 FALSE);
6041 }
6042 free (newname);
6043
6044 /* Mark this version if there is a definition and it is
6045 not defined in a shared object. */
6046 if (newh != NULL
6047 && !newh->def_dynamic
6048 && (newh->root.type == bfd_link_hash_defined
6049 || newh->root.type == bfd_link_hash_defweak))
6050 d->symver = 1;
6051 }
6052
6053 /* Attach all the symbols to their version information. */
6054 asvinfo.info = info;
6055 asvinfo.failed = FALSE;
6056
6057 elf_link_hash_traverse (elf_hash_table (info),
6058 _bfd_elf_link_assign_sym_version,
6059 &asvinfo);
6060 if (asvinfo.failed)
6061 return FALSE;
6062
6063 if (!info->allow_undefined_version)
6064 {
6065 /* Check if all global versions have a definition. */
6066 all_defined = TRUE;
6067 for (t = info->version_info; t != NULL; t = t->next)
6068 for (d = t->globals.list; d != NULL; d = d->next)
6069 if (d->literal && !d->symver && !d->script)
6070 {
6071 (*_bfd_error_handler)
6072 (_("%s: undefined version: %s"),
6073 d->pattern, t->name);
6074 all_defined = FALSE;
6075 }
6076
6077 if (!all_defined)
6078 {
6079 bfd_set_error (bfd_error_bad_value);
6080 return FALSE;
6081 }
6082 }
6083
6084 /* Find all symbols which were defined in a dynamic object and make
6085 the backend pick a reasonable value for them. */
6086 elf_link_hash_traverse (elf_hash_table (info),
6087 _bfd_elf_adjust_dynamic_symbol,
6088 &eif);
6089 if (eif.failed)
6090 return FALSE;
6091
6092 /* Add some entries to the .dynamic section. We fill in some of the
6093 values later, in bfd_elf_final_link, but we must add the entries
6094 now so that we know the final size of the .dynamic section. */
6095
6096 /* If there are initialization and/or finalization functions to
6097 call then add the corresponding DT_INIT/DT_FINI entries. */
6098 h = (info->init_function
6099 ? elf_link_hash_lookup (elf_hash_table (info),
6100 info->init_function, FALSE,
6101 FALSE, FALSE)
6102 : NULL);
6103 if (h != NULL
6104 && (h->ref_regular
6105 || h->def_regular))
6106 {
6107 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6108 return FALSE;
6109 }
6110 h = (info->fini_function
6111 ? elf_link_hash_lookup (elf_hash_table (info),
6112 info->fini_function, FALSE,
6113 FALSE, FALSE)
6114 : NULL);
6115 if (h != NULL
6116 && (h->ref_regular
6117 || h->def_regular))
6118 {
6119 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6120 return FALSE;
6121 }
6122
6123 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6124 if (s != NULL && s->linker_has_input)
6125 {
6126 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6127 if (! bfd_link_executable (info))
6128 {
6129 bfd *sub;
6130 asection *o;
6131
6132 for (sub = info->input_bfds; sub != NULL;
6133 sub = sub->link.next)
6134 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
6135 for (o = sub->sections; o != NULL; o = o->next)
6136 if (elf_section_data (o)->this_hdr.sh_type
6137 == SHT_PREINIT_ARRAY)
6138 {
6139 (*_bfd_error_handler)
6140 (_("%B: .preinit_array section is not allowed in DSO"),
6141 sub);
6142 break;
6143 }
6144
6145 bfd_set_error (bfd_error_nonrepresentable_section);
6146 return FALSE;
6147 }
6148
6149 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6150 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6151 return FALSE;
6152 }
6153 s = bfd_get_section_by_name (output_bfd, ".init_array");
6154 if (s != NULL && s->linker_has_input)
6155 {
6156 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6157 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6158 return FALSE;
6159 }
6160 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6161 if (s != NULL && s->linker_has_input)
6162 {
6163 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6164 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6165 return FALSE;
6166 }
6167
6168 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6169 /* If .dynstr is excluded from the link, we don't want any of
6170 these tags. Strictly, we should be checking each section
6171 individually; This quick check covers for the case where
6172 someone does a /DISCARD/ : { *(*) }. */
6173 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6174 {
6175 bfd_size_type strsize;
6176
6177 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6178 if ((info->emit_hash
6179 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6180 || (info->emit_gnu_hash
6181 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6182 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6183 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6184 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6185 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6186 bed->s->sizeof_sym))
6187 return FALSE;
6188 }
6189 }
6190
6191 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6192 return FALSE;
6193
6194 /* The backend must work out the sizes of all the other dynamic
6195 sections. */
6196 if (dynobj != NULL
6197 && bed->elf_backend_size_dynamic_sections != NULL
6198 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6199 return FALSE;
6200
6201 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6202 {
6203 unsigned long section_sym_count;
6204 struct bfd_elf_version_tree *verdefs;
6205 asection *s;
6206
6207 /* Set up the version definition section. */
6208 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6209 BFD_ASSERT (s != NULL);
6210
6211 /* We may have created additional version definitions if we are
6212 just linking a regular application. */
6213 verdefs = info->version_info;
6214
6215 /* Skip anonymous version tag. */
6216 if (verdefs != NULL && verdefs->vernum == 0)
6217 verdefs = verdefs->next;
6218
6219 if (verdefs == NULL && !info->create_default_symver)
6220 s->flags |= SEC_EXCLUDE;
6221 else
6222 {
6223 unsigned int cdefs;
6224 bfd_size_type size;
6225 struct bfd_elf_version_tree *t;
6226 bfd_byte *p;
6227 Elf_Internal_Verdef def;
6228 Elf_Internal_Verdaux defaux;
6229 struct bfd_link_hash_entry *bh;
6230 struct elf_link_hash_entry *h;
6231 const char *name;
6232
6233 cdefs = 0;
6234 size = 0;
6235
6236 /* Make space for the base version. */
6237 size += sizeof (Elf_External_Verdef);
6238 size += sizeof (Elf_External_Verdaux);
6239 ++cdefs;
6240
6241 /* Make space for the default version. */
6242 if (info->create_default_symver)
6243 {
6244 size += sizeof (Elf_External_Verdef);
6245 ++cdefs;
6246 }
6247
6248 for (t = verdefs; t != NULL; t = t->next)
6249 {
6250 struct bfd_elf_version_deps *n;
6251
6252 /* Don't emit base version twice. */
6253 if (t->vernum == 0)
6254 continue;
6255
6256 size += sizeof (Elf_External_Verdef);
6257 size += sizeof (Elf_External_Verdaux);
6258 ++cdefs;
6259
6260 for (n = t->deps; n != NULL; n = n->next)
6261 size += sizeof (Elf_External_Verdaux);
6262 }
6263
6264 s->size = size;
6265 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6266 if (s->contents == NULL && s->size != 0)
6267 return FALSE;
6268
6269 /* Fill in the version definition section. */
6270
6271 p = s->contents;
6272
6273 def.vd_version = VER_DEF_CURRENT;
6274 def.vd_flags = VER_FLG_BASE;
6275 def.vd_ndx = 1;
6276 def.vd_cnt = 1;
6277 if (info->create_default_symver)
6278 {
6279 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6280 def.vd_next = sizeof (Elf_External_Verdef);
6281 }
6282 else
6283 {
6284 def.vd_aux = sizeof (Elf_External_Verdef);
6285 def.vd_next = (sizeof (Elf_External_Verdef)
6286 + sizeof (Elf_External_Verdaux));
6287 }
6288
6289 if (soname_indx != (size_t) -1)
6290 {
6291 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6292 soname_indx);
6293 def.vd_hash = bfd_elf_hash (soname);
6294 defaux.vda_name = soname_indx;
6295 name = soname;
6296 }
6297 else
6298 {
6299 size_t indx;
6300
6301 name = lbasename (output_bfd->filename);
6302 def.vd_hash = bfd_elf_hash (name);
6303 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6304 name, FALSE);
6305 if (indx == (size_t) -1)
6306 return FALSE;
6307 defaux.vda_name = indx;
6308 }
6309 defaux.vda_next = 0;
6310
6311 _bfd_elf_swap_verdef_out (output_bfd, &def,
6312 (Elf_External_Verdef *) p);
6313 p += sizeof (Elf_External_Verdef);
6314 if (info->create_default_symver)
6315 {
6316 /* Add a symbol representing this version. */
6317 bh = NULL;
6318 if (! (_bfd_generic_link_add_one_symbol
6319 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6320 0, NULL, FALSE,
6321 get_elf_backend_data (dynobj)->collect, &bh)))
6322 return FALSE;
6323 h = (struct elf_link_hash_entry *) bh;
6324 h->non_elf = 0;
6325 h->def_regular = 1;
6326 h->type = STT_OBJECT;
6327 h->verinfo.vertree = NULL;
6328
6329 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6330 return FALSE;
6331
6332 /* Create a duplicate of the base version with the same
6333 aux block, but different flags. */
6334 def.vd_flags = 0;
6335 def.vd_ndx = 2;
6336 def.vd_aux = sizeof (Elf_External_Verdef);
6337 if (verdefs)
6338 def.vd_next = (sizeof (Elf_External_Verdef)
6339 + sizeof (Elf_External_Verdaux));
6340 else
6341 def.vd_next = 0;
6342 _bfd_elf_swap_verdef_out (output_bfd, &def,
6343 (Elf_External_Verdef *) p);
6344 p += sizeof (Elf_External_Verdef);
6345 }
6346 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6347 (Elf_External_Verdaux *) p);
6348 p += sizeof (Elf_External_Verdaux);
6349
6350 for (t = verdefs; t != NULL; t = t->next)
6351 {
6352 unsigned int cdeps;
6353 struct bfd_elf_version_deps *n;
6354
6355 /* Don't emit the base version twice. */
6356 if (t->vernum == 0)
6357 continue;
6358
6359 cdeps = 0;
6360 for (n = t->deps; n != NULL; n = n->next)
6361 ++cdeps;
6362
6363 /* Add a symbol representing this version. */
6364 bh = NULL;
6365 if (! (_bfd_generic_link_add_one_symbol
6366 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6367 0, NULL, FALSE,
6368 get_elf_backend_data (dynobj)->collect, &bh)))
6369 return FALSE;
6370 h = (struct elf_link_hash_entry *) bh;
6371 h->non_elf = 0;
6372 h->def_regular = 1;
6373 h->type = STT_OBJECT;
6374 h->verinfo.vertree = t;
6375
6376 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6377 return FALSE;
6378
6379 def.vd_version = VER_DEF_CURRENT;
6380 def.vd_flags = 0;
6381 if (t->globals.list == NULL
6382 && t->locals.list == NULL
6383 && ! t->used)
6384 def.vd_flags |= VER_FLG_WEAK;
6385 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6386 def.vd_cnt = cdeps + 1;
6387 def.vd_hash = bfd_elf_hash (t->name);
6388 def.vd_aux = sizeof (Elf_External_Verdef);
6389 def.vd_next = 0;
6390
6391 /* If a basever node is next, it *must* be the last node in
6392 the chain, otherwise Verdef construction breaks. */
6393 if (t->next != NULL && t->next->vernum == 0)
6394 BFD_ASSERT (t->next->next == NULL);
6395
6396 if (t->next != NULL && t->next->vernum != 0)
6397 def.vd_next = (sizeof (Elf_External_Verdef)
6398 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6399
6400 _bfd_elf_swap_verdef_out (output_bfd, &def,
6401 (Elf_External_Verdef *) p);
6402 p += sizeof (Elf_External_Verdef);
6403
6404 defaux.vda_name = h->dynstr_index;
6405 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6406 h->dynstr_index);
6407 defaux.vda_next = 0;
6408 if (t->deps != NULL)
6409 defaux.vda_next = sizeof (Elf_External_Verdaux);
6410 t->name_indx = defaux.vda_name;
6411
6412 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6413 (Elf_External_Verdaux *) p);
6414 p += sizeof (Elf_External_Verdaux);
6415
6416 for (n = t->deps; n != NULL; n = n->next)
6417 {
6418 if (n->version_needed == NULL)
6419 {
6420 /* This can happen if there was an error in the
6421 version script. */
6422 defaux.vda_name = 0;
6423 }
6424 else
6425 {
6426 defaux.vda_name = n->version_needed->name_indx;
6427 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6428 defaux.vda_name);
6429 }
6430 if (n->next == NULL)
6431 defaux.vda_next = 0;
6432 else
6433 defaux.vda_next = sizeof (Elf_External_Verdaux);
6434
6435 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6436 (Elf_External_Verdaux *) p);
6437 p += sizeof (Elf_External_Verdaux);
6438 }
6439 }
6440
6441 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6442 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6443 return FALSE;
6444
6445 elf_tdata (output_bfd)->cverdefs = cdefs;
6446 }
6447
6448 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6449 {
6450 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6451 return FALSE;
6452 }
6453 else if (info->flags & DF_BIND_NOW)
6454 {
6455 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6456 return FALSE;
6457 }
6458
6459 if (info->flags_1)
6460 {
6461 if (bfd_link_executable (info))
6462 info->flags_1 &= ~ (DF_1_INITFIRST
6463 | DF_1_NODELETE
6464 | DF_1_NOOPEN);
6465 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6466 return FALSE;
6467 }
6468
6469 /* Work out the size of the version reference section. */
6470
6471 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6472 BFD_ASSERT (s != NULL);
6473 {
6474 struct elf_find_verdep_info sinfo;
6475
6476 sinfo.info = info;
6477 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6478 if (sinfo.vers == 0)
6479 sinfo.vers = 1;
6480 sinfo.failed = FALSE;
6481
6482 elf_link_hash_traverse (elf_hash_table (info),
6483 _bfd_elf_link_find_version_dependencies,
6484 &sinfo);
6485 if (sinfo.failed)
6486 return FALSE;
6487
6488 if (elf_tdata (output_bfd)->verref == NULL)
6489 s->flags |= SEC_EXCLUDE;
6490 else
6491 {
6492 Elf_Internal_Verneed *t;
6493 unsigned int size;
6494 unsigned int crefs;
6495 bfd_byte *p;
6496
6497 /* Build the version dependency section. */
6498 size = 0;
6499 crefs = 0;
6500 for (t = elf_tdata (output_bfd)->verref;
6501 t != NULL;
6502 t = t->vn_nextref)
6503 {
6504 Elf_Internal_Vernaux *a;
6505
6506 size += sizeof (Elf_External_Verneed);
6507 ++crefs;
6508 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6509 size += sizeof (Elf_External_Vernaux);
6510 }
6511
6512 s->size = size;
6513 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6514 if (s->contents == NULL)
6515 return FALSE;
6516
6517 p = s->contents;
6518 for (t = elf_tdata (output_bfd)->verref;
6519 t != NULL;
6520 t = t->vn_nextref)
6521 {
6522 unsigned int caux;
6523 Elf_Internal_Vernaux *a;
6524 size_t indx;
6525
6526 caux = 0;
6527 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6528 ++caux;
6529
6530 t->vn_version = VER_NEED_CURRENT;
6531 t->vn_cnt = caux;
6532 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6533 elf_dt_name (t->vn_bfd) != NULL
6534 ? elf_dt_name (t->vn_bfd)
6535 : lbasename (t->vn_bfd->filename),
6536 FALSE);
6537 if (indx == (size_t) -1)
6538 return FALSE;
6539 t->vn_file = indx;
6540 t->vn_aux = sizeof (Elf_External_Verneed);
6541 if (t->vn_nextref == NULL)
6542 t->vn_next = 0;
6543 else
6544 t->vn_next = (sizeof (Elf_External_Verneed)
6545 + caux * sizeof (Elf_External_Vernaux));
6546
6547 _bfd_elf_swap_verneed_out (output_bfd, t,
6548 (Elf_External_Verneed *) p);
6549 p += sizeof (Elf_External_Verneed);
6550
6551 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6552 {
6553 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6554 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6555 a->vna_nodename, FALSE);
6556 if (indx == (size_t) -1)
6557 return FALSE;
6558 a->vna_name = indx;
6559 if (a->vna_nextptr == NULL)
6560 a->vna_next = 0;
6561 else
6562 a->vna_next = sizeof (Elf_External_Vernaux);
6563
6564 _bfd_elf_swap_vernaux_out (output_bfd, a,
6565 (Elf_External_Vernaux *) p);
6566 p += sizeof (Elf_External_Vernaux);
6567 }
6568 }
6569
6570 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6571 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6572 return FALSE;
6573
6574 elf_tdata (output_bfd)->cverrefs = crefs;
6575 }
6576 }
6577
6578 if ((elf_tdata (output_bfd)->cverrefs == 0
6579 && elf_tdata (output_bfd)->cverdefs == 0)
6580 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6581 §ion_sym_count) == 0)
6582 {
6583 s = bfd_get_linker_section (dynobj, ".gnu.version");
6584 s->flags |= SEC_EXCLUDE;
6585 }
6586 }
6587 return TRUE;
6588 }
6589
6590 /* Find the first non-excluded output section. We'll use its
6591 section symbol for some emitted relocs. */
6592 void
6593 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6594 {
6595 asection *s;
6596
6597 for (s = output_bfd->sections; s != NULL; s = s->next)
6598 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6599 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6600 {
6601 elf_hash_table (info)->text_index_section = s;
6602 break;
6603 }
6604 }
6605
6606 /* Find two non-excluded output sections, one for code, one for data.
6607 We'll use their section symbols for some emitted relocs. */
6608 void
6609 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6610 {
6611 asection *s;
6612
6613 /* Data first, since setting text_index_section changes
6614 _bfd_elf_link_omit_section_dynsym. */
6615 for (s = output_bfd->sections; s != NULL; s = s->next)
6616 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6617 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6618 {
6619 elf_hash_table (info)->data_index_section = s;
6620 break;
6621 }
6622
6623 for (s = output_bfd->sections; s != NULL; s = s->next)
6624 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6625 == (SEC_ALLOC | SEC_READONLY))
6626 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6627 {
6628 elf_hash_table (info)->text_index_section = s;
6629 break;
6630 }
6631
6632 if (elf_hash_table (info)->text_index_section == NULL)
6633 elf_hash_table (info)->text_index_section
6634 = elf_hash_table (info)->data_index_section;
6635 }
6636
6637 bfd_boolean
6638 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6639 {
6640 const struct elf_backend_data *bed;
6641
6642 if (!is_elf_hash_table (info->hash))
6643 return TRUE;
6644
6645 bed = get_elf_backend_data (output_bfd);
6646 (*bed->elf_backend_init_index_section) (output_bfd, info);
6647
6648 if (elf_hash_table (info)->dynamic_sections_created)
6649 {
6650 bfd *dynobj;
6651 asection *s;
6652 bfd_size_type dynsymcount;
6653 unsigned long section_sym_count;
6654 unsigned int dtagcount;
6655
6656 dynobj = elf_hash_table (info)->dynobj;
6657
6658 /* Assign dynsym indicies. In a shared library we generate a
6659 section symbol for each output section, which come first.
6660 Next come all of the back-end allocated local dynamic syms,
6661 followed by the rest of the global symbols. */
6662
6663 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6664 §ion_sym_count);
6665
6666 /* Work out the size of the symbol version section. */
6667 s = bfd_get_linker_section (dynobj, ".gnu.version");
6668 BFD_ASSERT (s != NULL);
6669 if ((s->flags & SEC_EXCLUDE) == 0)
6670 {
6671 s->size = dynsymcount * sizeof (Elf_External_Versym);
6672 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6673 if (s->contents == NULL)
6674 return FALSE;
6675
6676 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6677 return FALSE;
6678 }
6679
6680 /* Set the size of the .dynsym and .hash sections. We counted
6681 the number of dynamic symbols in elf_link_add_object_symbols.
6682 We will build the contents of .dynsym and .hash when we build
6683 the final symbol table, because until then we do not know the
6684 correct value to give the symbols. We built the .dynstr
6685 section as we went along in elf_link_add_object_symbols. */
6686 s = elf_hash_table (info)->dynsym;
6687 BFD_ASSERT (s != NULL);
6688 s->size = dynsymcount * bed->s->sizeof_sym;
6689
6690 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6691 if (s->contents == NULL)
6692 return FALSE;
6693
6694 /* The first entry in .dynsym is a dummy symbol. Clear all the
6695 section syms, in case we don't output them all. */
6696 ++section_sym_count;
6697 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6698
6699 elf_hash_table (info)->bucketcount = 0;
6700
6701 /* Compute the size of the hashing table. As a side effect this
6702 computes the hash values for all the names we export. */
6703 if (info->emit_hash)
6704 {
6705 unsigned long int *hashcodes;
6706 struct hash_codes_info hashinf;
6707 bfd_size_type amt;
6708 unsigned long int nsyms;
6709 size_t bucketcount;
6710 size_t hash_entry_size;
6711
6712 /* Compute the hash values for all exported symbols. At the same
6713 time store the values in an array so that we could use them for
6714 optimizations. */
6715 amt = dynsymcount * sizeof (unsigned long int);
6716 hashcodes = (unsigned long int *) bfd_malloc (amt);
6717 if (hashcodes == NULL)
6718 return FALSE;
6719 hashinf.hashcodes = hashcodes;
6720 hashinf.error = FALSE;
6721
6722 /* Put all hash values in HASHCODES. */
6723 elf_link_hash_traverse (elf_hash_table (info),
6724 elf_collect_hash_codes, &hashinf);
6725 if (hashinf.error)
6726 {
6727 free (hashcodes);
6728 return FALSE;
6729 }
6730
6731 nsyms = hashinf.hashcodes - hashcodes;
6732 bucketcount
6733 = compute_bucket_count (info, hashcodes, nsyms, 0);
6734 free (hashcodes);
6735
6736 if (bucketcount == 0)
6737 return FALSE;
6738
6739 elf_hash_table (info)->bucketcount = bucketcount;
6740
6741 s = bfd_get_linker_section (dynobj, ".hash");
6742 BFD_ASSERT (s != NULL);
6743 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6744 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6745 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6746 if (s->contents == NULL)
6747 return FALSE;
6748
6749 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6750 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6751 s->contents + hash_entry_size);
6752 }
6753
6754 if (info->emit_gnu_hash)
6755 {
6756 size_t i, cnt;
6757 unsigned char *contents;
6758 struct collect_gnu_hash_codes cinfo;
6759 bfd_size_type amt;
6760 size_t bucketcount;
6761
6762 memset (&cinfo, 0, sizeof (cinfo));
6763
6764 /* Compute the hash values for all exported symbols. At the same
6765 time store the values in an array so that we could use them for
6766 optimizations. */
6767 amt = dynsymcount * 2 * sizeof (unsigned long int);
6768 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
6769 if (cinfo.hashcodes == NULL)
6770 return FALSE;
6771
6772 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6773 cinfo.min_dynindx = -1;
6774 cinfo.output_bfd = output_bfd;
6775 cinfo.bed = bed;
6776
6777 /* Put all hash values in HASHCODES. */
6778 elf_link_hash_traverse (elf_hash_table (info),
6779 elf_collect_gnu_hash_codes, &cinfo);
6780 if (cinfo.error)
6781 {
6782 free (cinfo.hashcodes);
6783 return FALSE;
6784 }
6785
6786 bucketcount
6787 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6788
6789 if (bucketcount == 0)
6790 {
6791 free (cinfo.hashcodes);
6792 return FALSE;
6793 }
6794
6795 s = bfd_get_linker_section (dynobj, ".gnu.hash");
6796 BFD_ASSERT (s != NULL);
6797
6798 if (cinfo.nsyms == 0)
6799 {
6800 /* Empty .gnu.hash section is special. */
6801 BFD_ASSERT (cinfo.min_dynindx == -1);
6802 free (cinfo.hashcodes);
6803 s->size = 5 * 4 + bed->s->arch_size / 8;
6804 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6805 if (contents == NULL)
6806 return FALSE;
6807 s->contents = contents;
6808 /* 1 empty bucket. */
6809 bfd_put_32 (output_bfd, 1, contents);
6810 /* SYMIDX above the special symbol 0. */
6811 bfd_put_32 (output_bfd, 1, contents + 4);
6812 /* Just one word for bitmask. */
6813 bfd_put_32 (output_bfd, 1, contents + 8);
6814 /* Only hash fn bloom filter. */
6815 bfd_put_32 (output_bfd, 0, contents + 12);
6816 /* No hashes are valid - empty bitmask. */
6817 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6818 /* No hashes in the only bucket. */
6819 bfd_put_32 (output_bfd, 0,
6820 contents + 16 + bed->s->arch_size / 8);
6821 }
6822 else
6823 {
6824 unsigned long int maskwords, maskbitslog2, x;
6825 BFD_ASSERT (cinfo.min_dynindx != -1);
6826
6827 x = cinfo.nsyms;
6828 maskbitslog2 = 1;
6829 while ((x >>= 1) != 0)
6830 ++maskbitslog2;
6831 if (maskbitslog2 < 3)
6832 maskbitslog2 = 5;
6833 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6834 maskbitslog2 = maskbitslog2 + 3;
6835 else
6836 maskbitslog2 = maskbitslog2 + 2;
6837 if (bed->s->arch_size == 64)
6838 {
6839 if (maskbitslog2 == 5)
6840 maskbitslog2 = 6;
6841 cinfo.shift1 = 6;
6842 }
6843 else
6844 cinfo.shift1 = 5;
6845 cinfo.mask = (1 << cinfo.shift1) - 1;
6846 cinfo.shift2 = maskbitslog2;
6847 cinfo.maskbits = 1 << maskbitslog2;
6848 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6849 amt = bucketcount * sizeof (unsigned long int) * 2;
6850 amt += maskwords * sizeof (bfd_vma);
6851 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
6852 if (cinfo.bitmask == NULL)
6853 {
6854 free (cinfo.hashcodes);
6855 return FALSE;
6856 }
6857
6858 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
6859 cinfo.indx = cinfo.counts + bucketcount;
6860 cinfo.symindx = dynsymcount - cinfo.nsyms;
6861 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6862
6863 /* Determine how often each hash bucket is used. */
6864 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6865 for (i = 0; i < cinfo.nsyms; ++i)
6866 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6867
6868 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6869 if (cinfo.counts[i] != 0)
6870 {
6871 cinfo.indx[i] = cnt;
6872 cnt += cinfo.counts[i];
6873 }
6874 BFD_ASSERT (cnt == dynsymcount);
6875 cinfo.bucketcount = bucketcount;
6876 cinfo.local_indx = cinfo.min_dynindx;
6877
6878 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6879 s->size += cinfo.maskbits / 8;
6880 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6881 if (contents == NULL)
6882 {
6883 free (cinfo.bitmask);
6884 free (cinfo.hashcodes);
6885 return FALSE;
6886 }
6887
6888 s->contents = contents;
6889 bfd_put_32 (output_bfd, bucketcount, contents);
6890 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6891 bfd_put_32 (output_bfd, maskwords, contents + 8);
6892 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6893 contents += 16 + cinfo.maskbits / 8;
6894
6895 for (i = 0; i < bucketcount; ++i)
6896 {
6897 if (cinfo.counts[i] == 0)
6898 bfd_put_32 (output_bfd, 0, contents);
6899 else
6900 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6901 contents += 4;
6902 }
6903
6904 cinfo.contents = contents;
6905
6906 /* Renumber dynamic symbols, populate .gnu.hash section. */
6907 elf_link_hash_traverse (elf_hash_table (info),
6908 elf_renumber_gnu_hash_syms, &cinfo);
6909
6910 contents = s->contents + 16;
6911 for (i = 0; i < maskwords; ++i)
6912 {
6913 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6914 contents);
6915 contents += bed->s->arch_size / 8;
6916 }
6917
6918 free (cinfo.bitmask);
6919 free (cinfo.hashcodes);
6920 }
6921 }
6922
6923 s = bfd_get_linker_section (dynobj, ".dynstr");
6924 BFD_ASSERT (s != NULL);
6925
6926 elf_finalize_dynstr (output_bfd, info);
6927
6928 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6929
6930 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6931 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6932 return FALSE;
6933 }
6934
6935 return TRUE;
6936 }
6937
6938 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
6940
6941 static void
6942 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6943 asection *sec)
6944 {
6945 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
6946 sec->sec_info_type = SEC_INFO_TYPE_NONE;
6947 }
6948
6949 /* Finish SHF_MERGE section merging. */
6950
6951 bfd_boolean
6952 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
6953 {
6954 bfd *ibfd;
6955 asection *sec;
6956
6957 if (!is_elf_hash_table (info->hash))
6958 return FALSE;
6959
6960 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6961 if ((ibfd->flags & DYNAMIC) == 0
6962 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6963 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
6964 == get_elf_backend_data (obfd)->s->elfclass))
6965 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6966 if ((sec->flags & SEC_MERGE) != 0
6967 && !bfd_is_abs_section (sec->output_section))
6968 {
6969 struct bfd_elf_section_data *secdata;
6970
6971 secdata = elf_section_data (sec);
6972 if (! _bfd_add_merge_section (obfd,
6973 &elf_hash_table (info)->merge_info,
6974 sec, &secdata->sec_info))
6975 return FALSE;
6976 else if (secdata->sec_info)
6977 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
6978 }
6979
6980 if (elf_hash_table (info)->merge_info != NULL)
6981 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
6982 merge_sections_remove_hook);
6983 return TRUE;
6984 }
6985
6986 /* Create an entry in an ELF linker hash table. */
6987
6988 struct bfd_hash_entry *
6989 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6990 struct bfd_hash_table *table,
6991 const char *string)
6992 {
6993 /* Allocate the structure if it has not already been allocated by a
6994 subclass. */
6995 if (entry == NULL)
6996 {
6997 entry = (struct bfd_hash_entry *)
6998 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
6999 if (entry == NULL)
7000 return entry;
7001 }
7002
7003 /* Call the allocation method of the superclass. */
7004 entry = _bfd_link_hash_newfunc (entry, table, string);
7005 if (entry != NULL)
7006 {
7007 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7008 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7009
7010 /* Set local fields. */
7011 ret->indx = -1;
7012 ret->dynindx = -1;
7013 ret->got = htab->init_got_refcount;
7014 ret->plt = htab->init_plt_refcount;
7015 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7016 - offsetof (struct elf_link_hash_entry, size)));
7017 /* Assume that we have been called by a non-ELF symbol reader.
7018 This flag is then reset by the code which reads an ELF input
7019 file. This ensures that a symbol created by a non-ELF symbol
7020 reader will have the flag set correctly. */
7021 ret->non_elf = 1;
7022 }
7023
7024 return entry;
7025 }
7026
7027 /* Copy data from an indirect symbol to its direct symbol, hiding the
7028 old indirect symbol. Also used for copying flags to a weakdef. */
7029
7030 void
7031 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7032 struct elf_link_hash_entry *dir,
7033 struct elf_link_hash_entry *ind)
7034 {
7035 struct elf_link_hash_table *htab;
7036
7037 /* Copy down any references that we may have already seen to the
7038 symbol which just became indirect if DIR isn't a hidden versioned
7039 symbol. */
7040
7041 if (dir->versioned != versioned_hidden)
7042 {
7043 dir->ref_dynamic |= ind->ref_dynamic;
7044 dir->ref_regular |= ind->ref_regular;
7045 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7046 dir->non_got_ref |= ind->non_got_ref;
7047 dir->needs_plt |= ind->needs_plt;
7048 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7049 }
7050
7051 if (ind->root.type != bfd_link_hash_indirect)
7052 return;
7053
7054 /* Copy over the global and procedure linkage table refcount entries.
7055 These may have been already set up by a check_relocs routine. */
7056 htab = elf_hash_table (info);
7057 if (ind->got.refcount > htab->init_got_refcount.refcount)
7058 {
7059 if (dir->got.refcount < 0)
7060 dir->got.refcount = 0;
7061 dir->got.refcount += ind->got.refcount;
7062 ind->got.refcount = htab->init_got_refcount.refcount;
7063 }
7064
7065 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7066 {
7067 if (dir->plt.refcount < 0)
7068 dir->plt.refcount = 0;
7069 dir->plt.refcount += ind->plt.refcount;
7070 ind->plt.refcount = htab->init_plt_refcount.refcount;
7071 }
7072
7073 if (ind->dynindx != -1)
7074 {
7075 if (dir->dynindx != -1)
7076 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7077 dir->dynindx = ind->dynindx;
7078 dir->dynstr_index = ind->dynstr_index;
7079 ind->dynindx = -1;
7080 ind->dynstr_index = 0;
7081 }
7082 }
7083
7084 void
7085 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7086 struct elf_link_hash_entry *h,
7087 bfd_boolean force_local)
7088 {
7089 /* STT_GNU_IFUNC symbol must go through PLT. */
7090 if (h->type != STT_GNU_IFUNC)
7091 {
7092 h->plt = elf_hash_table (info)->init_plt_offset;
7093 h->needs_plt = 0;
7094 }
7095 if (force_local)
7096 {
7097 h->forced_local = 1;
7098 if (h->dynindx != -1)
7099 {
7100 h->dynindx = -1;
7101 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7102 h->dynstr_index);
7103 }
7104 }
7105 }
7106
7107 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7108 caller. */
7109
7110 bfd_boolean
7111 _bfd_elf_link_hash_table_init
7112 (struct elf_link_hash_table *table,
7113 bfd *abfd,
7114 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7115 struct bfd_hash_table *,
7116 const char *),
7117 unsigned int entsize,
7118 enum elf_target_id target_id)
7119 {
7120 bfd_boolean ret;
7121 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7122
7123 table->init_got_refcount.refcount = can_refcount - 1;
7124 table->init_plt_refcount.refcount = can_refcount - 1;
7125 table->init_got_offset.offset = -(bfd_vma) 1;
7126 table->init_plt_offset.offset = -(bfd_vma) 1;
7127 /* The first dynamic symbol is a dummy. */
7128 table->dynsymcount = 1;
7129
7130 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7131
7132 table->root.type = bfd_link_elf_hash_table;
7133 table->hash_table_id = target_id;
7134
7135 return ret;
7136 }
7137
7138 /* Create an ELF linker hash table. */
7139
7140 struct bfd_link_hash_table *
7141 _bfd_elf_link_hash_table_create (bfd *abfd)
7142 {
7143 struct elf_link_hash_table *ret;
7144 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7145
7146 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7147 if (ret == NULL)
7148 return NULL;
7149
7150 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7151 sizeof (struct elf_link_hash_entry),
7152 GENERIC_ELF_DATA))
7153 {
7154 free (ret);
7155 return NULL;
7156 }
7157 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7158
7159 return &ret->root;
7160 }
7161
7162 /* Destroy an ELF linker hash table. */
7163
7164 void
7165 _bfd_elf_link_hash_table_free (bfd *obfd)
7166 {
7167 struct elf_link_hash_table *htab;
7168
7169 htab = (struct elf_link_hash_table *) obfd->link.hash;
7170 if (htab->dynstr != NULL)
7171 _bfd_elf_strtab_free (htab->dynstr);
7172 _bfd_merge_sections_free (htab->merge_info);
7173 _bfd_generic_link_hash_table_free (obfd);
7174 }
7175
7176 /* This is a hook for the ELF emulation code in the generic linker to
7177 tell the backend linker what file name to use for the DT_NEEDED
7178 entry for a dynamic object. */
7179
7180 void
7181 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7182 {
7183 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7184 && bfd_get_format (abfd) == bfd_object)
7185 elf_dt_name (abfd) = name;
7186 }
7187
7188 int
7189 bfd_elf_get_dyn_lib_class (bfd *abfd)
7190 {
7191 int lib_class;
7192 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7193 && bfd_get_format (abfd) == bfd_object)
7194 lib_class = elf_dyn_lib_class (abfd);
7195 else
7196 lib_class = 0;
7197 return lib_class;
7198 }
7199
7200 void
7201 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7202 {
7203 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7204 && bfd_get_format (abfd) == bfd_object)
7205 elf_dyn_lib_class (abfd) = lib_class;
7206 }
7207
7208 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7209 the linker ELF emulation code. */
7210
7211 struct bfd_link_needed_list *
7212 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7213 struct bfd_link_info *info)
7214 {
7215 if (! is_elf_hash_table (info->hash))
7216 return NULL;
7217 return elf_hash_table (info)->needed;
7218 }
7219
7220 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7221 hook for the linker ELF emulation code. */
7222
7223 struct bfd_link_needed_list *
7224 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7225 struct bfd_link_info *info)
7226 {
7227 if (! is_elf_hash_table (info->hash))
7228 return NULL;
7229 return elf_hash_table (info)->runpath;
7230 }
7231
7232 /* Get the name actually used for a dynamic object for a link. This
7233 is the SONAME entry if there is one. Otherwise, it is the string
7234 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7235
7236 const char *
7237 bfd_elf_get_dt_soname (bfd *abfd)
7238 {
7239 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7240 && bfd_get_format (abfd) == bfd_object)
7241 return elf_dt_name (abfd);
7242 return NULL;
7243 }
7244
7245 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7246 the ELF linker emulation code. */
7247
7248 bfd_boolean
7249 bfd_elf_get_bfd_needed_list (bfd *abfd,
7250 struct bfd_link_needed_list **pneeded)
7251 {
7252 asection *s;
7253 bfd_byte *dynbuf = NULL;
7254 unsigned int elfsec;
7255 unsigned long shlink;
7256 bfd_byte *extdyn, *extdynend;
7257 size_t extdynsize;
7258 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7259
7260 *pneeded = NULL;
7261
7262 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7263 || bfd_get_format (abfd) != bfd_object)
7264 return TRUE;
7265
7266 s = bfd_get_section_by_name (abfd, ".dynamic");
7267 if (s == NULL || s->size == 0)
7268 return TRUE;
7269
7270 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7271 goto error_return;
7272
7273 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7274 if (elfsec == SHN_BAD)
7275 goto error_return;
7276
7277 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7278
7279 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7280 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7281
7282 extdyn = dynbuf;
7283 extdynend = extdyn + s->size;
7284 for (; extdyn < extdynend; extdyn += extdynsize)
7285 {
7286 Elf_Internal_Dyn dyn;
7287
7288 (*swap_dyn_in) (abfd, extdyn, &dyn);
7289
7290 if (dyn.d_tag == DT_NULL)
7291 break;
7292
7293 if (dyn.d_tag == DT_NEEDED)
7294 {
7295 const char *string;
7296 struct bfd_link_needed_list *l;
7297 unsigned int tagv = dyn.d_un.d_val;
7298 bfd_size_type amt;
7299
7300 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7301 if (string == NULL)
7302 goto error_return;
7303
7304 amt = sizeof *l;
7305 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7306 if (l == NULL)
7307 goto error_return;
7308
7309 l->by = abfd;
7310 l->name = string;
7311 l->next = *pneeded;
7312 *pneeded = l;
7313 }
7314 }
7315
7316 free (dynbuf);
7317
7318 return TRUE;
7319
7320 error_return:
7321 if (dynbuf != NULL)
7322 free (dynbuf);
7323 return FALSE;
7324 }
7325
7326 struct elf_symbuf_symbol
7327 {
7328 unsigned long st_name; /* Symbol name, index in string tbl */
7329 unsigned char st_info; /* Type and binding attributes */
7330 unsigned char st_other; /* Visibilty, and target specific */
7331 };
7332
7333 struct elf_symbuf_head
7334 {
7335 struct elf_symbuf_symbol *ssym;
7336 size_t count;
7337 unsigned int st_shndx;
7338 };
7339
7340 struct elf_symbol
7341 {
7342 union
7343 {
7344 Elf_Internal_Sym *isym;
7345 struct elf_symbuf_symbol *ssym;
7346 } u;
7347 const char *name;
7348 };
7349
7350 /* Sort references to symbols by ascending section number. */
7351
7352 static int
7353 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7354 {
7355 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7356 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7357
7358 return s1->st_shndx - s2->st_shndx;
7359 }
7360
7361 static int
7362 elf_sym_name_compare (const void *arg1, const void *arg2)
7363 {
7364 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7365 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7366 return strcmp (s1->name, s2->name);
7367 }
7368
7369 static struct elf_symbuf_head *
7370 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
7371 {
7372 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7373 struct elf_symbuf_symbol *ssym;
7374 struct elf_symbuf_head *ssymbuf, *ssymhead;
7375 size_t i, shndx_count, total_size;
7376
7377 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7378 if (indbuf == NULL)
7379 return NULL;
7380
7381 for (ind = indbuf, i = 0; i < symcount; i++)
7382 if (isymbuf[i].st_shndx != SHN_UNDEF)
7383 *ind++ = &isymbuf[i];
7384 indbufend = ind;
7385
7386 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7387 elf_sort_elf_symbol);
7388
7389 shndx_count = 0;
7390 if (indbufend > indbuf)
7391 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7392 if (ind[0]->st_shndx != ind[1]->st_shndx)
7393 shndx_count++;
7394
7395 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7396 + (indbufend - indbuf) * sizeof (*ssym));
7397 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7398 if (ssymbuf == NULL)
7399 {
7400 free (indbuf);
7401 return NULL;
7402 }
7403
7404 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7405 ssymbuf->ssym = NULL;
7406 ssymbuf->count = shndx_count;
7407 ssymbuf->st_shndx = 0;
7408 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7409 {
7410 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7411 {
7412 ssymhead++;
7413 ssymhead->ssym = ssym;
7414 ssymhead->count = 0;
7415 ssymhead->st_shndx = (*ind)->st_shndx;
7416 }
7417 ssym->st_name = (*ind)->st_name;
7418 ssym->st_info = (*ind)->st_info;
7419 ssym->st_other = (*ind)->st_other;
7420 ssymhead->count++;
7421 }
7422 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
7423 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7424 == total_size));
7425
7426 free (indbuf);
7427 return ssymbuf;
7428 }
7429
7430 /* Check if 2 sections define the same set of local and global
7431 symbols. */
7432
7433 static bfd_boolean
7434 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7435 struct bfd_link_info *info)
7436 {
7437 bfd *bfd1, *bfd2;
7438 const struct elf_backend_data *bed1, *bed2;
7439 Elf_Internal_Shdr *hdr1, *hdr2;
7440 size_t symcount1, symcount2;
7441 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7442 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7443 Elf_Internal_Sym *isym, *isymend;
7444 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7445 size_t count1, count2, i;
7446 unsigned int shndx1, shndx2;
7447 bfd_boolean result;
7448
7449 bfd1 = sec1->owner;
7450 bfd2 = sec2->owner;
7451
7452 /* Both sections have to be in ELF. */
7453 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7454 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7455 return FALSE;
7456
7457 if (elf_section_type (sec1) != elf_section_type (sec2))
7458 return FALSE;
7459
7460 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7461 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7462 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7463 return FALSE;
7464
7465 bed1 = get_elf_backend_data (bfd1);
7466 bed2 = get_elf_backend_data (bfd2);
7467 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7468 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7469 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7470 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7471
7472 if (symcount1 == 0 || symcount2 == 0)
7473 return FALSE;
7474
7475 result = FALSE;
7476 isymbuf1 = NULL;
7477 isymbuf2 = NULL;
7478 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7479 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7480
7481 if (ssymbuf1 == NULL)
7482 {
7483 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7484 NULL, NULL, NULL);
7485 if (isymbuf1 == NULL)
7486 goto done;
7487
7488 if (!info->reduce_memory_overheads)
7489 elf_tdata (bfd1)->symbuf = ssymbuf1
7490 = elf_create_symbuf (symcount1, isymbuf1);
7491 }
7492
7493 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7494 {
7495 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7496 NULL, NULL, NULL);
7497 if (isymbuf2 == NULL)
7498 goto done;
7499
7500 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7501 elf_tdata (bfd2)->symbuf = ssymbuf2
7502 = elf_create_symbuf (symcount2, isymbuf2);
7503 }
7504
7505 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7506 {
7507 /* Optimized faster version. */
7508 size_t lo, hi, mid;
7509 struct elf_symbol *symp;
7510 struct elf_symbuf_symbol *ssym, *ssymend;
7511
7512 lo = 0;
7513 hi = ssymbuf1->count;
7514 ssymbuf1++;
7515 count1 = 0;
7516 while (lo < hi)
7517 {
7518 mid = (lo + hi) / 2;
7519 if (shndx1 < ssymbuf1[mid].st_shndx)
7520 hi = mid;
7521 else if (shndx1 > ssymbuf1[mid].st_shndx)
7522 lo = mid + 1;
7523 else
7524 {
7525 count1 = ssymbuf1[mid].count;
7526 ssymbuf1 += mid;
7527 break;
7528 }
7529 }
7530
7531 lo = 0;
7532 hi = ssymbuf2->count;
7533 ssymbuf2++;
7534 count2 = 0;
7535 while (lo < hi)
7536 {
7537 mid = (lo + hi) / 2;
7538 if (shndx2 < ssymbuf2[mid].st_shndx)
7539 hi = mid;
7540 else if (shndx2 > ssymbuf2[mid].st_shndx)
7541 lo = mid + 1;
7542 else
7543 {
7544 count2 = ssymbuf2[mid].count;
7545 ssymbuf2 += mid;
7546 break;
7547 }
7548 }
7549
7550 if (count1 == 0 || count2 == 0 || count1 != count2)
7551 goto done;
7552
7553 symtable1
7554 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7555 symtable2
7556 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
7557 if (symtable1 == NULL || symtable2 == NULL)
7558 goto done;
7559
7560 symp = symtable1;
7561 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7562 ssym < ssymend; ssym++, symp++)
7563 {
7564 symp->u.ssym = ssym;
7565 symp->name = bfd_elf_string_from_elf_section (bfd1,
7566 hdr1->sh_link,
7567 ssym->st_name);
7568 }
7569
7570 symp = symtable2;
7571 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7572 ssym < ssymend; ssym++, symp++)
7573 {
7574 symp->u.ssym = ssym;
7575 symp->name = bfd_elf_string_from_elf_section (bfd2,
7576 hdr2->sh_link,
7577 ssym->st_name);
7578 }
7579
7580 /* Sort symbol by name. */
7581 qsort (symtable1, count1, sizeof (struct elf_symbol),
7582 elf_sym_name_compare);
7583 qsort (symtable2, count1, sizeof (struct elf_symbol),
7584 elf_sym_name_compare);
7585
7586 for (i = 0; i < count1; i++)
7587 /* Two symbols must have the same binding, type and name. */
7588 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7589 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7590 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7591 goto done;
7592
7593 result = TRUE;
7594 goto done;
7595 }
7596
7597 symtable1 = (struct elf_symbol *)
7598 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7599 symtable2 = (struct elf_symbol *)
7600 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7601 if (symtable1 == NULL || symtable2 == NULL)
7602 goto done;
7603
7604 /* Count definitions in the section. */
7605 count1 = 0;
7606 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7607 if (isym->st_shndx == shndx1)
7608 symtable1[count1++].u.isym = isym;
7609
7610 count2 = 0;
7611 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7612 if (isym->st_shndx == shndx2)
7613 symtable2[count2++].u.isym = isym;
7614
7615 if (count1 == 0 || count2 == 0 || count1 != count2)
7616 goto done;
7617
7618 for (i = 0; i < count1; i++)
7619 symtable1[i].name
7620 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7621 symtable1[i].u.isym->st_name);
7622
7623 for (i = 0; i < count2; i++)
7624 symtable2[i].name
7625 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7626 symtable2[i].u.isym->st_name);
7627
7628 /* Sort symbol by name. */
7629 qsort (symtable1, count1, sizeof (struct elf_symbol),
7630 elf_sym_name_compare);
7631 qsort (symtable2, count1, sizeof (struct elf_symbol),
7632 elf_sym_name_compare);
7633
7634 for (i = 0; i < count1; i++)
7635 /* Two symbols must have the same binding, type and name. */
7636 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7637 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7638 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7639 goto done;
7640
7641 result = TRUE;
7642
7643 done:
7644 if (symtable1)
7645 free (symtable1);
7646 if (symtable2)
7647 free (symtable2);
7648 if (isymbuf1)
7649 free (isymbuf1);
7650 if (isymbuf2)
7651 free (isymbuf2);
7652
7653 return result;
7654 }
7655
7656 /* Return TRUE if 2 section types are compatible. */
7657
7658 bfd_boolean
7659 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7660 bfd *bbfd, const asection *bsec)
7661 {
7662 if (asec == NULL
7663 || bsec == NULL
7664 || abfd->xvec->flavour != bfd_target_elf_flavour
7665 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7666 return TRUE;
7667
7668 return elf_section_type (asec) == elf_section_type (bsec);
7669 }
7670
7671 /* Final phase of ELF linker. */
7673
7674 /* A structure we use to avoid passing large numbers of arguments. */
7675
7676 struct elf_final_link_info
7677 {
7678 /* General link information. */
7679 struct bfd_link_info *info;
7680 /* Output BFD. */
7681 bfd *output_bfd;
7682 /* Symbol string table. */
7683 struct elf_strtab_hash *symstrtab;
7684 /* .hash section. */
7685 asection *hash_sec;
7686 /* symbol version section (.gnu.version). */
7687 asection *symver_sec;
7688 /* Buffer large enough to hold contents of any section. */
7689 bfd_byte *contents;
7690 /* Buffer large enough to hold external relocs of any section. */
7691 void *external_relocs;
7692 /* Buffer large enough to hold internal relocs of any section. */
7693 Elf_Internal_Rela *internal_relocs;
7694 /* Buffer large enough to hold external local symbols of any input
7695 BFD. */
7696 bfd_byte *external_syms;
7697 /* And a buffer for symbol section indices. */
7698 Elf_External_Sym_Shndx *locsym_shndx;
7699 /* Buffer large enough to hold internal local symbols of any input
7700 BFD. */
7701 Elf_Internal_Sym *internal_syms;
7702 /* Array large enough to hold a symbol index for each local symbol
7703 of any input BFD. */
7704 long *indices;
7705 /* Array large enough to hold a section pointer for each local
7706 symbol of any input BFD. */
7707 asection **sections;
7708 /* Buffer for SHT_SYMTAB_SHNDX section. */
7709 Elf_External_Sym_Shndx *symshndxbuf;
7710 /* Number of STT_FILE syms seen. */
7711 size_t filesym_count;
7712 };
7713
7714 /* This struct is used to pass information to elf_link_output_extsym. */
7715
7716 struct elf_outext_info
7717 {
7718 bfd_boolean failed;
7719 bfd_boolean localsyms;
7720 bfd_boolean file_sym_done;
7721 struct elf_final_link_info *flinfo;
7722 };
7723
7724
7725 /* Support for evaluating a complex relocation.
7726
7727 Complex relocations are generalized, self-describing relocations. The
7728 implementation of them consists of two parts: complex symbols, and the
7729 relocations themselves.
7730
7731 The relocations are use a reserved elf-wide relocation type code (R_RELC
7732 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7733 information (start bit, end bit, word width, etc) into the addend. This
7734 information is extracted from CGEN-generated operand tables within gas.
7735
7736 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7737 internal) representing prefix-notation expressions, including but not
7738 limited to those sorts of expressions normally encoded as addends in the
7739 addend field. The symbol mangling format is:
7740
7741 <node> := <literal>
7742 | <unary-operator> ':' <node>
7743 | <binary-operator> ':' <node> ':' <node>
7744 ;
7745
7746 <literal> := 's' <digits=N> ':' <N character symbol name>
7747 | 'S' <digits=N> ':' <N character section name>
7748 | '#' <hexdigits>
7749 ;
7750
7751 <binary-operator> := as in C
7752 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7753
7754 static void
7755 set_symbol_value (bfd *bfd_with_globals,
7756 Elf_Internal_Sym *isymbuf,
7757 size_t locsymcount,
7758 size_t symidx,
7759 bfd_vma val)
7760 {
7761 struct elf_link_hash_entry **sym_hashes;
7762 struct elf_link_hash_entry *h;
7763 size_t extsymoff = locsymcount;
7764
7765 if (symidx < locsymcount)
7766 {
7767 Elf_Internal_Sym *sym;
7768
7769 sym = isymbuf + symidx;
7770 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7771 {
7772 /* It is a local symbol: move it to the
7773 "absolute" section and give it a value. */
7774 sym->st_shndx = SHN_ABS;
7775 sym->st_value = val;
7776 return;
7777 }
7778 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7779 extsymoff = 0;
7780 }
7781
7782 /* It is a global symbol: set its link type
7783 to "defined" and give it a value. */
7784
7785 sym_hashes = elf_sym_hashes (bfd_with_globals);
7786 h = sym_hashes [symidx - extsymoff];
7787 while (h->root.type == bfd_link_hash_indirect
7788 || h->root.type == bfd_link_hash_warning)
7789 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7790 h->root.type = bfd_link_hash_defined;
7791 h->root.u.def.value = val;
7792 h->root.u.def.section = bfd_abs_section_ptr;
7793 }
7794
7795 static bfd_boolean
7796 resolve_symbol (const char *name,
7797 bfd *input_bfd,
7798 struct elf_final_link_info *flinfo,
7799 bfd_vma *result,
7800 Elf_Internal_Sym *isymbuf,
7801 size_t locsymcount)
7802 {
7803 Elf_Internal_Sym *sym;
7804 struct bfd_link_hash_entry *global_entry;
7805 const char *candidate = NULL;
7806 Elf_Internal_Shdr *symtab_hdr;
7807 size_t i;
7808
7809 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7810
7811 for (i = 0; i < locsymcount; ++ i)
7812 {
7813 sym = isymbuf + i;
7814
7815 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7816 continue;
7817
7818 candidate = bfd_elf_string_from_elf_section (input_bfd,
7819 symtab_hdr->sh_link,
7820 sym->st_name);
7821 #ifdef DEBUG
7822 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7823 name, candidate, (unsigned long) sym->st_value);
7824 #endif
7825 if (candidate && strcmp (candidate, name) == 0)
7826 {
7827 asection *sec = flinfo->sections [i];
7828
7829 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7830 *result += sec->output_offset + sec->output_section->vma;
7831 #ifdef DEBUG
7832 printf ("Found symbol with value %8.8lx\n",
7833 (unsigned long) *result);
7834 #endif
7835 return TRUE;
7836 }
7837 }
7838
7839 /* Hmm, haven't found it yet. perhaps it is a global. */
7840 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
7841 FALSE, FALSE, TRUE);
7842 if (!global_entry)
7843 return FALSE;
7844
7845 if (global_entry->type == bfd_link_hash_defined
7846 || global_entry->type == bfd_link_hash_defweak)
7847 {
7848 *result = (global_entry->u.def.value
7849 + global_entry->u.def.section->output_section->vma
7850 + global_entry->u.def.section->output_offset);
7851 #ifdef DEBUG
7852 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7853 global_entry->root.string, (unsigned long) *result);
7854 #endif
7855 return TRUE;
7856 }
7857
7858 return FALSE;
7859 }
7860
7861 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
7862 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
7863 names like "foo.end" which is the end address of section "foo". */
7864
7865 static bfd_boolean
7866 resolve_section (const char *name,
7867 asection *sections,
7868 bfd_vma *result,
7869 bfd * abfd)
7870 {
7871 asection *curr;
7872 unsigned int len;
7873
7874 for (curr = sections; curr; curr = curr->next)
7875 if (strcmp (curr->name, name) == 0)
7876 {
7877 *result = curr->vma;
7878 return TRUE;
7879 }
7880
7881 /* Hmm. still haven't found it. try pseudo-section names. */
7882 /* FIXME: This could be coded more efficiently... */
7883 for (curr = sections; curr; curr = curr->next)
7884 {
7885 len = strlen (curr->name);
7886 if (len > strlen (name))
7887 continue;
7888
7889 if (strncmp (curr->name, name, len) == 0)
7890 {
7891 if (strncmp (".end", name + len, 4) == 0)
7892 {
7893 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
7894 return TRUE;
7895 }
7896
7897 /* Insert more pseudo-section names here, if you like. */
7898 }
7899 }
7900
7901 return FALSE;
7902 }
7903
7904 static void
7905 undefined_reference (const char *reftype, const char *name)
7906 {
7907 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7908 reftype, name);
7909 }
7910
7911 static bfd_boolean
7912 eval_symbol (bfd_vma *result,
7913 const char **symp,
7914 bfd *input_bfd,
7915 struct elf_final_link_info *flinfo,
7916 bfd_vma dot,
7917 Elf_Internal_Sym *isymbuf,
7918 size_t locsymcount,
7919 int signed_p)
7920 {
7921 size_t len;
7922 size_t symlen;
7923 bfd_vma a;
7924 bfd_vma b;
7925 char symbuf[4096];
7926 const char *sym = *symp;
7927 const char *symend;
7928 bfd_boolean symbol_is_section = FALSE;
7929
7930 len = strlen (sym);
7931 symend = sym + len;
7932
7933 if (len < 1 || len > sizeof (symbuf))
7934 {
7935 bfd_set_error (bfd_error_invalid_operation);
7936 return FALSE;
7937 }
7938
7939 switch (* sym)
7940 {
7941 case '.':
7942 *result = dot;
7943 *symp = sym + 1;
7944 return TRUE;
7945
7946 case '#':
7947 ++sym;
7948 *result = strtoul (sym, (char **) symp, 16);
7949 return TRUE;
7950
7951 case 'S':
7952 symbol_is_section = TRUE;
7953 case 's':
7954 ++sym;
7955 symlen = strtol (sym, (char **) symp, 10);
7956 sym = *symp + 1; /* Skip the trailing ':'. */
7957
7958 if (symend < sym || symlen + 1 > sizeof (symbuf))
7959 {
7960 bfd_set_error (bfd_error_invalid_operation);
7961 return FALSE;
7962 }
7963
7964 memcpy (symbuf, sym, symlen);
7965 symbuf[symlen] = '\0';
7966 *symp = sym + symlen;
7967
7968 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7969 the symbol as a section, or vice-versa. so we're pretty liberal in our
7970 interpretation here; section means "try section first", not "must be a
7971 section", and likewise with symbol. */
7972
7973 if (symbol_is_section)
7974 {
7975 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
7976 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
7977 isymbuf, locsymcount))
7978 {
7979 undefined_reference ("section", symbuf);
7980 return FALSE;
7981 }
7982 }
7983 else
7984 {
7985 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
7986 isymbuf, locsymcount)
7987 && !resolve_section (symbuf, flinfo->output_bfd->sections,
7988 result, input_bfd))
7989 {
7990 undefined_reference ("symbol", symbuf);
7991 return FALSE;
7992 }
7993 }
7994
7995 return TRUE;
7996
7997 /* All that remains are operators. */
7998
7999 #define UNARY_OP(op) \
8000 if (strncmp (sym, #op, strlen (#op)) == 0) \
8001 { \
8002 sym += strlen (#op); \
8003 if (*sym == ':') \
8004 ++sym; \
8005 *symp = sym; \
8006 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8007 isymbuf, locsymcount, signed_p)) \
8008 return FALSE; \
8009 if (signed_p) \
8010 *result = op ((bfd_signed_vma) a); \
8011 else \
8012 *result = op a; \
8013 return TRUE; \
8014 }
8015
8016 #define BINARY_OP(op) \
8017 if (strncmp (sym, #op, strlen (#op)) == 0) \
8018 { \
8019 sym += strlen (#op); \
8020 if (*sym == ':') \
8021 ++sym; \
8022 *symp = sym; \
8023 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8024 isymbuf, locsymcount, signed_p)) \
8025 return FALSE; \
8026 ++*symp; \
8027 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8028 isymbuf, locsymcount, signed_p)) \
8029 return FALSE; \
8030 if (signed_p) \
8031 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8032 else \
8033 *result = a op b; \
8034 return TRUE; \
8035 }
8036
8037 default:
8038 UNARY_OP (0-);
8039 BINARY_OP (<<);
8040 BINARY_OP (>>);
8041 BINARY_OP (==);
8042 BINARY_OP (!=);
8043 BINARY_OP (<=);
8044 BINARY_OP (>=);
8045 BINARY_OP (&&);
8046 BINARY_OP (||);
8047 UNARY_OP (~);
8048 UNARY_OP (!);
8049 BINARY_OP (*);
8050 BINARY_OP (/);
8051 BINARY_OP (%);
8052 BINARY_OP (^);
8053 BINARY_OP (|);
8054 BINARY_OP (&);
8055 BINARY_OP (+);
8056 BINARY_OP (-);
8057 BINARY_OP (<);
8058 BINARY_OP (>);
8059 #undef UNARY_OP
8060 #undef BINARY_OP
8061 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8062 bfd_set_error (bfd_error_invalid_operation);
8063 return FALSE;
8064 }
8065 }
8066
8067 static void
8068 put_value (bfd_vma size,
8069 unsigned long chunksz,
8070 bfd *input_bfd,
8071 bfd_vma x,
8072 bfd_byte *location)
8073 {
8074 location += (size - chunksz);
8075
8076 for (; size; size -= chunksz, location -= chunksz)
8077 {
8078 switch (chunksz)
8079 {
8080 case 1:
8081 bfd_put_8 (input_bfd, x, location);
8082 x >>= 8;
8083 break;
8084 case 2:
8085 bfd_put_16 (input_bfd, x, location);
8086 x >>= 16;
8087 break;
8088 case 4:
8089 bfd_put_32 (input_bfd, x, location);
8090 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8091 x >>= 16;
8092 x >>= 16;
8093 break;
8094 #ifdef BFD64
8095 case 8:
8096 bfd_put_64 (input_bfd, x, location);
8097 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8098 x >>= 32;
8099 x >>= 32;
8100 break;
8101 #endif
8102 default:
8103 abort ();
8104 break;
8105 }
8106 }
8107 }
8108
8109 static bfd_vma
8110 get_value (bfd_vma size,
8111 unsigned long chunksz,
8112 bfd *input_bfd,
8113 bfd_byte *location)
8114 {
8115 int shift;
8116 bfd_vma x = 0;
8117
8118 /* Sanity checks. */
8119 BFD_ASSERT (chunksz <= sizeof (x)
8120 && size >= chunksz
8121 && chunksz != 0
8122 && (size % chunksz) == 0
8123 && input_bfd != NULL
8124 && location != NULL);
8125
8126 if (chunksz == sizeof (x))
8127 {
8128 BFD_ASSERT (size == chunksz);
8129
8130 /* Make sure that we do not perform an undefined shift operation.
8131 We know that size == chunksz so there will only be one iteration
8132 of the loop below. */
8133 shift = 0;
8134 }
8135 else
8136 shift = 8 * chunksz;
8137
8138 for (; size; size -= chunksz, location += chunksz)
8139 {
8140 switch (chunksz)
8141 {
8142 case 1:
8143 x = (x << shift) | bfd_get_8 (input_bfd, location);
8144 break;
8145 case 2:
8146 x = (x << shift) | bfd_get_16 (input_bfd, location);
8147 break;
8148 case 4:
8149 x = (x << shift) | bfd_get_32 (input_bfd, location);
8150 break;
8151 #ifdef BFD64
8152 case 8:
8153 x = (x << shift) | bfd_get_64 (input_bfd, location);
8154 break;
8155 #endif
8156 default:
8157 abort ();
8158 }
8159 }
8160 return x;
8161 }
8162
8163 static void
8164 decode_complex_addend (unsigned long *start, /* in bits */
8165 unsigned long *oplen, /* in bits */
8166 unsigned long *len, /* in bits */
8167 unsigned long *wordsz, /* in bytes */
8168 unsigned long *chunksz, /* in bytes */
8169 unsigned long *lsb0_p,
8170 unsigned long *signed_p,
8171 unsigned long *trunc_p,
8172 unsigned long encoded)
8173 {
8174 * start = encoded & 0x3F;
8175 * len = (encoded >> 6) & 0x3F;
8176 * oplen = (encoded >> 12) & 0x3F;
8177 * wordsz = (encoded >> 18) & 0xF;
8178 * chunksz = (encoded >> 22) & 0xF;
8179 * lsb0_p = (encoded >> 27) & 1;
8180 * signed_p = (encoded >> 28) & 1;
8181 * trunc_p = (encoded >> 29) & 1;
8182 }
8183
8184 bfd_reloc_status_type
8185 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8186 asection *input_section ATTRIBUTE_UNUSED,
8187 bfd_byte *contents,
8188 Elf_Internal_Rela *rel,
8189 bfd_vma relocation)
8190 {
8191 bfd_vma shift, x, mask;
8192 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8193 bfd_reloc_status_type r;
8194
8195 /* Perform this reloc, since it is complex.
8196 (this is not to say that it necessarily refers to a complex
8197 symbol; merely that it is a self-describing CGEN based reloc.
8198 i.e. the addend has the complete reloc information (bit start, end,
8199 word size, etc) encoded within it.). */
8200
8201 decode_complex_addend (&start, &oplen, &len, &wordsz,
8202 &chunksz, &lsb0_p, &signed_p,
8203 &trunc_p, rel->r_addend);
8204
8205 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8206
8207 if (lsb0_p)
8208 shift = (start + 1) - len;
8209 else
8210 shift = (8 * wordsz) - (start + len);
8211
8212 x = get_value (wordsz, chunksz, input_bfd,
8213 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8214
8215 #ifdef DEBUG
8216 printf ("Doing complex reloc: "
8217 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8218 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8219 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8220 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8221 oplen, (unsigned long) x, (unsigned long) mask,
8222 (unsigned long) relocation);
8223 #endif
8224
8225 r = bfd_reloc_ok;
8226 if (! trunc_p)
8227 /* Now do an overflow check. */
8228 r = bfd_check_overflow ((signed_p
8229 ? complain_overflow_signed
8230 : complain_overflow_unsigned),
8231 len, 0, (8 * wordsz),
8232 relocation);
8233
8234 /* Do the deed. */
8235 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8236
8237 #ifdef DEBUG
8238 printf (" relocation: %8.8lx\n"
8239 " shifted mask: %8.8lx\n"
8240 " shifted/masked reloc: %8.8lx\n"
8241 " result: %8.8lx\n",
8242 (unsigned long) relocation, (unsigned long) (mask << shift),
8243 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8244 #endif
8245 put_value (wordsz, chunksz, input_bfd, x,
8246 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8247 return r;
8248 }
8249
8250 /* Functions to read r_offset from external (target order) reloc
8251 entry. Faster than bfd_getl32 et al, because we let the compiler
8252 know the value is aligned. */
8253
8254 static bfd_vma
8255 ext32l_r_offset (const void *p)
8256 {
8257 union aligned32
8258 {
8259 uint32_t v;
8260 unsigned char c[4];
8261 };
8262 const union aligned32 *a
8263 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8264
8265 uint32_t aval = ( (uint32_t) a->c[0]
8266 | (uint32_t) a->c[1] << 8
8267 | (uint32_t) a->c[2] << 16
8268 | (uint32_t) a->c[3] << 24);
8269 return aval;
8270 }
8271
8272 static bfd_vma
8273 ext32b_r_offset (const void *p)
8274 {
8275 union aligned32
8276 {
8277 uint32_t v;
8278 unsigned char c[4];
8279 };
8280 const union aligned32 *a
8281 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8282
8283 uint32_t aval = ( (uint32_t) a->c[0] << 24
8284 | (uint32_t) a->c[1] << 16
8285 | (uint32_t) a->c[2] << 8
8286 | (uint32_t) a->c[3]);
8287 return aval;
8288 }
8289
8290 #ifdef BFD_HOST_64_BIT
8291 static bfd_vma
8292 ext64l_r_offset (const void *p)
8293 {
8294 union aligned64
8295 {
8296 uint64_t v;
8297 unsigned char c[8];
8298 };
8299 const union aligned64 *a
8300 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8301
8302 uint64_t aval = ( (uint64_t) a->c[0]
8303 | (uint64_t) a->c[1] << 8
8304 | (uint64_t) a->c[2] << 16
8305 | (uint64_t) a->c[3] << 24
8306 | (uint64_t) a->c[4] << 32
8307 | (uint64_t) a->c[5] << 40
8308 | (uint64_t) a->c[6] << 48
8309 | (uint64_t) a->c[7] << 56);
8310 return aval;
8311 }
8312
8313 static bfd_vma
8314 ext64b_r_offset (const void *p)
8315 {
8316 union aligned64
8317 {
8318 uint64_t v;
8319 unsigned char c[8];
8320 };
8321 const union aligned64 *a
8322 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8323
8324 uint64_t aval = ( (uint64_t) a->c[0] << 56
8325 | (uint64_t) a->c[1] << 48
8326 | (uint64_t) a->c[2] << 40
8327 | (uint64_t) a->c[3] << 32
8328 | (uint64_t) a->c[4] << 24
8329 | (uint64_t) a->c[5] << 16
8330 | (uint64_t) a->c[6] << 8
8331 | (uint64_t) a->c[7]);
8332 return aval;
8333 }
8334 #endif
8335
8336 /* When performing a relocatable link, the input relocations are
8337 preserved. But, if they reference global symbols, the indices
8338 referenced must be updated. Update all the relocations found in
8339 RELDATA. */
8340
8341 static bfd_boolean
8342 elf_link_adjust_relocs (bfd *abfd,
8343 struct bfd_elf_section_reloc_data *reldata,
8344 bfd_boolean sort)
8345 {
8346 unsigned int i;
8347 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8348 bfd_byte *erela;
8349 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8350 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8351 bfd_vma r_type_mask;
8352 int r_sym_shift;
8353 unsigned int count = reldata->count;
8354 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8355
8356 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8357 {
8358 swap_in = bed->s->swap_reloc_in;
8359 swap_out = bed->s->swap_reloc_out;
8360 }
8361 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8362 {
8363 swap_in = bed->s->swap_reloca_in;
8364 swap_out = bed->s->swap_reloca_out;
8365 }
8366 else
8367 abort ();
8368
8369 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8370 abort ();
8371
8372 if (bed->s->arch_size == 32)
8373 {
8374 r_type_mask = 0xff;
8375 r_sym_shift = 8;
8376 }
8377 else
8378 {
8379 r_type_mask = 0xffffffff;
8380 r_sym_shift = 32;
8381 }
8382
8383 erela = reldata->hdr->contents;
8384 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8385 {
8386 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8387 unsigned int j;
8388
8389 if (*rel_hash == NULL)
8390 continue;
8391
8392 BFD_ASSERT ((*rel_hash)->indx >= 0);
8393
8394 (*swap_in) (abfd, erela, irela);
8395 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8396 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8397 | (irela[j].r_info & r_type_mask));
8398 (*swap_out) (abfd, irela, erela);
8399 }
8400
8401 if (sort && count != 0)
8402 {
8403 bfd_vma (*ext_r_off) (const void *);
8404 bfd_vma r_off;
8405 size_t elt_size;
8406 bfd_byte *base, *end, *p, *loc;
8407 bfd_byte *buf = NULL;
8408
8409 if (bed->s->arch_size == 32)
8410 {
8411 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8412 ext_r_off = ext32l_r_offset;
8413 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8414 ext_r_off = ext32b_r_offset;
8415 else
8416 abort ();
8417 }
8418 else
8419 {
8420 #ifdef BFD_HOST_64_BIT
8421 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8422 ext_r_off = ext64l_r_offset;
8423 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8424 ext_r_off = ext64b_r_offset;
8425 else
8426 #endif
8427 abort ();
8428 }
8429
8430 /* Must use a stable sort here. A modified insertion sort,
8431 since the relocs are mostly sorted already. */
8432 elt_size = reldata->hdr->sh_entsize;
8433 base = reldata->hdr->contents;
8434 end = base + count * elt_size;
8435 if (elt_size > sizeof (Elf64_External_Rela))
8436 abort ();
8437
8438 /* Ensure the first element is lowest. This acts as a sentinel,
8439 speeding the main loop below. */
8440 r_off = (*ext_r_off) (base);
8441 for (p = loc = base; (p += elt_size) < end; )
8442 {
8443 bfd_vma r_off2 = (*ext_r_off) (p);
8444 if (r_off > r_off2)
8445 {
8446 r_off = r_off2;
8447 loc = p;
8448 }
8449 }
8450 if (loc != base)
8451 {
8452 /* Don't just swap *base and *loc as that changes the order
8453 of the original base[0] and base[1] if they happen to
8454 have the same r_offset. */
8455 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8456 memcpy (onebuf, loc, elt_size);
8457 memmove (base + elt_size, base, loc - base);
8458 memcpy (base, onebuf, elt_size);
8459 }
8460
8461 for (p = base + elt_size; (p += elt_size) < end; )
8462 {
8463 /* base to p is sorted, *p is next to insert. */
8464 r_off = (*ext_r_off) (p);
8465 /* Search the sorted region for location to insert. */
8466 loc = p - elt_size;
8467 while (r_off < (*ext_r_off) (loc))
8468 loc -= elt_size;
8469 loc += elt_size;
8470 if (loc != p)
8471 {
8472 /* Chances are there is a run of relocs to insert here,
8473 from one of more input files. Files are not always
8474 linked in order due to the way elf_link_input_bfd is
8475 called. See pr17666. */
8476 size_t sortlen = p - loc;
8477 bfd_vma r_off2 = (*ext_r_off) (loc);
8478 size_t runlen = elt_size;
8479 size_t buf_size = 96 * 1024;
8480 while (p + runlen < end
8481 && (sortlen <= buf_size
8482 || runlen + elt_size <= buf_size)
8483 && r_off2 > (*ext_r_off) (p + runlen))
8484 runlen += elt_size;
8485 if (buf == NULL)
8486 {
8487 buf = bfd_malloc (buf_size);
8488 if (buf == NULL)
8489 return FALSE;
8490 }
8491 if (runlen < sortlen)
8492 {
8493 memcpy (buf, p, runlen);
8494 memmove (loc + runlen, loc, sortlen);
8495 memcpy (loc, buf, runlen);
8496 }
8497 else
8498 {
8499 memcpy (buf, loc, sortlen);
8500 memmove (loc, p, runlen);
8501 memcpy (loc + runlen, buf, sortlen);
8502 }
8503 p += runlen - elt_size;
8504 }
8505 }
8506 /* Hashes are no longer valid. */
8507 free (reldata->hashes);
8508 reldata->hashes = NULL;
8509 free (buf);
8510 }
8511 return TRUE;
8512 }
8513
8514 struct elf_link_sort_rela
8515 {
8516 union {
8517 bfd_vma offset;
8518 bfd_vma sym_mask;
8519 } u;
8520 enum elf_reloc_type_class type;
8521 /* We use this as an array of size int_rels_per_ext_rel. */
8522 Elf_Internal_Rela rela[1];
8523 };
8524
8525 static int
8526 elf_link_sort_cmp1 (const void *A, const void *B)
8527 {
8528 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8529 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8530 int relativea, relativeb;
8531
8532 relativea = a->type == reloc_class_relative;
8533 relativeb = b->type == reloc_class_relative;
8534
8535 if (relativea < relativeb)
8536 return 1;
8537 if (relativea > relativeb)
8538 return -1;
8539 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8540 return -1;
8541 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8542 return 1;
8543 if (a->rela->r_offset < b->rela->r_offset)
8544 return -1;
8545 if (a->rela->r_offset > b->rela->r_offset)
8546 return 1;
8547 return 0;
8548 }
8549
8550 static int
8551 elf_link_sort_cmp2 (const void *A, const void *B)
8552 {
8553 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8554 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8555
8556 if (a->type < b->type)
8557 return -1;
8558 if (a->type > b->type)
8559 return 1;
8560 if (a->u.offset < b->u.offset)
8561 return -1;
8562 if (a->u.offset > b->u.offset)
8563 return 1;
8564 if (a->rela->r_offset < b->rela->r_offset)
8565 return -1;
8566 if (a->rela->r_offset > b->rela->r_offset)
8567 return 1;
8568 return 0;
8569 }
8570
8571 static size_t
8572 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8573 {
8574 asection *dynamic_relocs;
8575 asection *rela_dyn;
8576 asection *rel_dyn;
8577 bfd_size_type count, size;
8578 size_t i, ret, sort_elt, ext_size;
8579 bfd_byte *sort, *s_non_relative, *p;
8580 struct elf_link_sort_rela *sq;
8581 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8582 int i2e = bed->s->int_rels_per_ext_rel;
8583 unsigned int opb = bfd_octets_per_byte (abfd);
8584 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8585 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8586 struct bfd_link_order *lo;
8587 bfd_vma r_sym_mask;
8588 bfd_boolean use_rela;
8589
8590 /* Find a dynamic reloc section. */
8591 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8592 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8593 if (rela_dyn != NULL && rela_dyn->size > 0
8594 && rel_dyn != NULL && rel_dyn->size > 0)
8595 {
8596 bfd_boolean use_rela_initialised = FALSE;
8597
8598 /* This is just here to stop gcc from complaining.
8599 Its initialization checking code is not perfect. */
8600 use_rela = TRUE;
8601
8602 /* Both sections are present. Examine the sizes
8603 of the indirect sections to help us choose. */
8604 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8605 if (lo->type == bfd_indirect_link_order)
8606 {
8607 asection *o = lo->u.indirect.section;
8608
8609 if ((o->size % bed->s->sizeof_rela) == 0)
8610 {
8611 if ((o->size % bed->s->sizeof_rel) == 0)
8612 /* Section size is divisible by both rel and rela sizes.
8613 It is of no help to us. */
8614 ;
8615 else
8616 {
8617 /* Section size is only divisible by rela. */
8618 if (use_rela_initialised && (use_rela == FALSE))
8619 {
8620 _bfd_error_handler (_("%B: Unable to sort relocs - "
8621 "they are in more than one size"),
8622 abfd);
8623 bfd_set_error (bfd_error_invalid_operation);
8624 return 0;
8625 }
8626 else
8627 {
8628 use_rela = TRUE;
8629 use_rela_initialised = TRUE;
8630 }
8631 }
8632 }
8633 else if ((o->size % bed->s->sizeof_rel) == 0)
8634 {
8635 /* Section size is only divisible by rel. */
8636 if (use_rela_initialised && (use_rela == TRUE))
8637 {
8638 _bfd_error_handler (_("%B: Unable to sort relocs - "
8639 "they are in more than one size"),
8640 abfd);
8641 bfd_set_error (bfd_error_invalid_operation);
8642 return 0;
8643 }
8644 else
8645 {
8646 use_rela = FALSE;
8647 use_rela_initialised = TRUE;
8648 }
8649 }
8650 else
8651 {
8652 /* The section size is not divisible by either -
8653 something is wrong. */
8654 _bfd_error_handler (_("%B: Unable to sort relocs - "
8655 "they are of an unknown size"), abfd);
8656 bfd_set_error (bfd_error_invalid_operation);
8657 return 0;
8658 }
8659 }
8660
8661 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8662 if (lo->type == bfd_indirect_link_order)
8663 {
8664 asection *o = lo->u.indirect.section;
8665
8666 if ((o->size % bed->s->sizeof_rela) == 0)
8667 {
8668 if ((o->size % bed->s->sizeof_rel) == 0)
8669 /* Section size is divisible by both rel and rela sizes.
8670 It is of no help to us. */
8671 ;
8672 else
8673 {
8674 /* Section size is only divisible by rela. */
8675 if (use_rela_initialised && (use_rela == FALSE))
8676 {
8677 _bfd_error_handler (_("%B: Unable to sort relocs - "
8678 "they are in more than one size"),
8679 abfd);
8680 bfd_set_error (bfd_error_invalid_operation);
8681 return 0;
8682 }
8683 else
8684 {
8685 use_rela = TRUE;
8686 use_rela_initialised = TRUE;
8687 }
8688 }
8689 }
8690 else if ((o->size % bed->s->sizeof_rel) == 0)
8691 {
8692 /* Section size is only divisible by rel. */
8693 if (use_rela_initialised && (use_rela == TRUE))
8694 {
8695 _bfd_error_handler (_("%B: Unable to sort relocs - "
8696 "they are in more than one size"),
8697 abfd);
8698 bfd_set_error (bfd_error_invalid_operation);
8699 return 0;
8700 }
8701 else
8702 {
8703 use_rela = FALSE;
8704 use_rela_initialised = TRUE;
8705 }
8706 }
8707 else
8708 {
8709 /* The section size is not divisible by either -
8710 something is wrong. */
8711 _bfd_error_handler (_("%B: Unable to sort relocs - "
8712 "they are of an unknown size"), abfd);
8713 bfd_set_error (bfd_error_invalid_operation);
8714 return 0;
8715 }
8716 }
8717
8718 if (! use_rela_initialised)
8719 /* Make a guess. */
8720 use_rela = TRUE;
8721 }
8722 else if (rela_dyn != NULL && rela_dyn->size > 0)
8723 use_rela = TRUE;
8724 else if (rel_dyn != NULL && rel_dyn->size > 0)
8725 use_rela = FALSE;
8726 else
8727 return 0;
8728
8729 if (use_rela)
8730 {
8731 dynamic_relocs = rela_dyn;
8732 ext_size = bed->s->sizeof_rela;
8733 swap_in = bed->s->swap_reloca_in;
8734 swap_out = bed->s->swap_reloca_out;
8735 }
8736 else
8737 {
8738 dynamic_relocs = rel_dyn;
8739 ext_size = bed->s->sizeof_rel;
8740 swap_in = bed->s->swap_reloc_in;
8741 swap_out = bed->s->swap_reloc_out;
8742 }
8743
8744 size = 0;
8745 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8746 if (lo->type == bfd_indirect_link_order)
8747 size += lo->u.indirect.section->size;
8748
8749 if (size != dynamic_relocs->size)
8750 return 0;
8751
8752 sort_elt = (sizeof (struct elf_link_sort_rela)
8753 + (i2e - 1) * sizeof (Elf_Internal_Rela));
8754
8755 count = dynamic_relocs->size / ext_size;
8756 if (count == 0)
8757 return 0;
8758 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
8759
8760 if (sort == NULL)
8761 {
8762 (*info->callbacks->warning)
8763 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8764 return 0;
8765 }
8766
8767 if (bed->s->arch_size == 32)
8768 r_sym_mask = ~(bfd_vma) 0xff;
8769 else
8770 r_sym_mask = ~(bfd_vma) 0xffffffff;
8771
8772 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8773 if (lo->type == bfd_indirect_link_order)
8774 {
8775 bfd_byte *erel, *erelend;
8776 asection *o = lo->u.indirect.section;
8777
8778 if (o->contents == NULL && o->size != 0)
8779 {
8780 /* This is a reloc section that is being handled as a normal
8781 section. See bfd_section_from_shdr. We can't combine
8782 relocs in this case. */
8783 free (sort);
8784 return 0;
8785 }
8786 erel = o->contents;
8787 erelend = o->contents + o->size;
8788 p = sort + o->output_offset * opb / ext_size * sort_elt;
8789
8790 while (erel < erelend)
8791 {
8792 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8793
8794 (*swap_in) (abfd, erel, s->rela);
8795 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
8796 s->u.sym_mask = r_sym_mask;
8797 p += sort_elt;
8798 erel += ext_size;
8799 }
8800 }
8801
8802 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8803
8804 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8805 {
8806 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8807 if (s->type != reloc_class_relative)
8808 break;
8809 }
8810 ret = i;
8811 s_non_relative = p;
8812
8813 sq = (struct elf_link_sort_rela *) s_non_relative;
8814 for (; i < count; i++, p += sort_elt)
8815 {
8816 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8817 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8818 sq = sp;
8819 sp->u.offset = sq->rela->r_offset;
8820 }
8821
8822 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8823
8824 struct elf_link_hash_table *htab = elf_hash_table (info);
8825 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
8826 {
8827 /* We have plt relocs in .rela.dyn. */
8828 sq = (struct elf_link_sort_rela *) sort;
8829 for (i = 0; i < count; i++)
8830 if (sq[count - i - 1].type != reloc_class_plt)
8831 break;
8832 if (i != 0 && htab->srelplt->size == i * ext_size)
8833 {
8834 struct bfd_link_order **plo;
8835 /* Put srelplt link_order last. This is so the output_offset
8836 set in the next loop is correct for DT_JMPREL. */
8837 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
8838 if ((*plo)->type == bfd_indirect_link_order
8839 && (*plo)->u.indirect.section == htab->srelplt)
8840 {
8841 lo = *plo;
8842 *plo = lo->next;
8843 }
8844 else
8845 plo = &(*plo)->next;
8846 *plo = lo;
8847 lo->next = NULL;
8848 dynamic_relocs->map_tail.link_order = lo;
8849 }
8850 }
8851
8852 p = sort;
8853 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8854 if (lo->type == bfd_indirect_link_order)
8855 {
8856 bfd_byte *erel, *erelend;
8857 asection *o = lo->u.indirect.section;
8858
8859 erel = o->contents;
8860 erelend = o->contents + o->size;
8861 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
8862 while (erel < erelend)
8863 {
8864 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8865 (*swap_out) (abfd, s->rela, erel);
8866 p += sort_elt;
8867 erel += ext_size;
8868 }
8869 }
8870
8871 free (sort);
8872 *psec = dynamic_relocs;
8873 return ret;
8874 }
8875
8876 /* Add a symbol to the output symbol string table. */
8877
8878 static int
8879 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
8880 const char *name,
8881 Elf_Internal_Sym *elfsym,
8882 asection *input_sec,
8883 struct elf_link_hash_entry *h)
8884 {
8885 int (*output_symbol_hook)
8886 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8887 struct elf_link_hash_entry *);
8888 struct elf_link_hash_table *hash_table;
8889 const struct elf_backend_data *bed;
8890 bfd_size_type strtabsize;
8891
8892 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8893
8894 bed = get_elf_backend_data (flinfo->output_bfd);
8895 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8896 if (output_symbol_hook != NULL)
8897 {
8898 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
8899 if (ret != 1)
8900 return ret;
8901 }
8902
8903 if (name == NULL
8904 || *name == '\0'
8905 || (input_sec->flags & SEC_EXCLUDE))
8906 elfsym->st_name = (unsigned long) -1;
8907 else
8908 {
8909 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8910 to get the final offset for st_name. */
8911 elfsym->st_name
8912 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
8913 name, FALSE);
8914 if (elfsym->st_name == (unsigned long) -1)
8915 return 0;
8916 }
8917
8918 hash_table = elf_hash_table (flinfo->info);
8919 strtabsize = hash_table->strtabsize;
8920 if (strtabsize <= hash_table->strtabcount)
8921 {
8922 strtabsize += strtabsize;
8923 hash_table->strtabsize = strtabsize;
8924 strtabsize *= sizeof (*hash_table->strtab);
8925 hash_table->strtab
8926 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
8927 strtabsize);
8928 if (hash_table->strtab == NULL)
8929 return 0;
8930 }
8931 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
8932 hash_table->strtab[hash_table->strtabcount].dest_index
8933 = hash_table->strtabcount;
8934 hash_table->strtab[hash_table->strtabcount].destshndx_index
8935 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
8936
8937 bfd_get_symcount (flinfo->output_bfd) += 1;
8938 hash_table->strtabcount += 1;
8939
8940 return 1;
8941 }
8942
8943 /* Swap symbols out to the symbol table and flush the output symbols to
8944 the file. */
8945
8946 static bfd_boolean
8947 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
8948 {
8949 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
8950 bfd_size_type amt;
8951 size_t i;
8952 const struct elf_backend_data *bed;
8953 bfd_byte *symbuf;
8954 Elf_Internal_Shdr *hdr;
8955 file_ptr pos;
8956 bfd_boolean ret;
8957
8958 if (!hash_table->strtabcount)
8959 return TRUE;
8960
8961 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8962
8963 bed = get_elf_backend_data (flinfo->output_bfd);
8964
8965 amt = bed->s->sizeof_sym * hash_table->strtabcount;
8966 symbuf = (bfd_byte *) bfd_malloc (amt);
8967 if (symbuf == NULL)
8968 return FALSE;
8969
8970 if (flinfo->symshndxbuf)
8971 {
8972 amt = sizeof (Elf_External_Sym_Shndx);
8973 amt *= bfd_get_symcount (flinfo->output_bfd);
8974 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
8975 if (flinfo->symshndxbuf == NULL)
8976 {
8977 free (symbuf);
8978 return FALSE;
8979 }
8980 }
8981
8982 for (i = 0; i < hash_table->strtabcount; i++)
8983 {
8984 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
8985 if (elfsym->sym.st_name == (unsigned long) -1)
8986 elfsym->sym.st_name = 0;
8987 else
8988 elfsym->sym.st_name
8989 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
8990 elfsym->sym.st_name);
8991 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
8992 ((bfd_byte *) symbuf
8993 + (elfsym->dest_index
8994 * bed->s->sizeof_sym)),
8995 (flinfo->symshndxbuf
8996 + elfsym->destshndx_index));
8997 }
8998
8999 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9000 pos = hdr->sh_offset + hdr->sh_size;
9001 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9002 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9003 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9004 {
9005 hdr->sh_size += amt;
9006 ret = TRUE;
9007 }
9008 else
9009 ret = FALSE;
9010
9011 free (symbuf);
9012
9013 free (hash_table->strtab);
9014 hash_table->strtab = NULL;
9015
9016 return ret;
9017 }
9018
9019 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9020
9021 static bfd_boolean
9022 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9023 {
9024 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9025 && sym->st_shndx < SHN_LORESERVE)
9026 {
9027 /* The gABI doesn't support dynamic symbols in output sections
9028 beyond 64k. */
9029 (*_bfd_error_handler)
9030 (_("%B: Too many sections: %d (>= %d)"),
9031 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9032 bfd_set_error (bfd_error_nonrepresentable_section);
9033 return FALSE;
9034 }
9035 return TRUE;
9036 }
9037
9038 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9039 allowing an unsatisfied unversioned symbol in the DSO to match a
9040 versioned symbol that would normally require an explicit version.
9041 We also handle the case that a DSO references a hidden symbol
9042 which may be satisfied by a versioned symbol in another DSO. */
9043
9044 static bfd_boolean
9045 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9046 const struct elf_backend_data *bed,
9047 struct elf_link_hash_entry *h)
9048 {
9049 bfd *abfd;
9050 struct elf_link_loaded_list *loaded;
9051
9052 if (!is_elf_hash_table (info->hash))
9053 return FALSE;
9054
9055 /* Check indirect symbol. */
9056 while (h->root.type == bfd_link_hash_indirect)
9057 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9058
9059 switch (h->root.type)
9060 {
9061 default:
9062 abfd = NULL;
9063 break;
9064
9065 case bfd_link_hash_undefined:
9066 case bfd_link_hash_undefweak:
9067 abfd = h->root.u.undef.abfd;
9068 if (abfd == NULL
9069 || (abfd->flags & DYNAMIC) == 0
9070 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9071 return FALSE;
9072 break;
9073
9074 case bfd_link_hash_defined:
9075 case bfd_link_hash_defweak:
9076 abfd = h->root.u.def.section->owner;
9077 break;
9078
9079 case bfd_link_hash_common:
9080 abfd = h->root.u.c.p->section->owner;
9081 break;
9082 }
9083 BFD_ASSERT (abfd != NULL);
9084
9085 for (loaded = elf_hash_table (info)->loaded;
9086 loaded != NULL;
9087 loaded = loaded->next)
9088 {
9089 bfd *input;
9090 Elf_Internal_Shdr *hdr;
9091 size_t symcount;
9092 size_t extsymcount;
9093 size_t extsymoff;
9094 Elf_Internal_Shdr *versymhdr;
9095 Elf_Internal_Sym *isym;
9096 Elf_Internal_Sym *isymend;
9097 Elf_Internal_Sym *isymbuf;
9098 Elf_External_Versym *ever;
9099 Elf_External_Versym *extversym;
9100
9101 input = loaded->abfd;
9102
9103 /* We check each DSO for a possible hidden versioned definition. */
9104 if (input == abfd
9105 || (input->flags & DYNAMIC) == 0
9106 || elf_dynversym (input) == 0)
9107 continue;
9108
9109 hdr = &elf_tdata (input)->dynsymtab_hdr;
9110
9111 symcount = hdr->sh_size / bed->s->sizeof_sym;
9112 if (elf_bad_symtab (input))
9113 {
9114 extsymcount = symcount;
9115 extsymoff = 0;
9116 }
9117 else
9118 {
9119 extsymcount = symcount - hdr->sh_info;
9120 extsymoff = hdr->sh_info;
9121 }
9122
9123 if (extsymcount == 0)
9124 continue;
9125
9126 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9127 NULL, NULL, NULL);
9128 if (isymbuf == NULL)
9129 return FALSE;
9130
9131 /* Read in any version definitions. */
9132 versymhdr = &elf_tdata (input)->dynversym_hdr;
9133 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
9134 if (extversym == NULL)
9135 goto error_ret;
9136
9137 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9138 || (bfd_bread (extversym, versymhdr->sh_size, input)
9139 != versymhdr->sh_size))
9140 {
9141 free (extversym);
9142 error_ret:
9143 free (isymbuf);
9144 return FALSE;
9145 }
9146
9147 ever = extversym + extsymoff;
9148 isymend = isymbuf + extsymcount;
9149 for (isym = isymbuf; isym < isymend; isym++, ever++)
9150 {
9151 const char *name;
9152 Elf_Internal_Versym iver;
9153 unsigned short version_index;
9154
9155 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9156 || isym->st_shndx == SHN_UNDEF)
9157 continue;
9158
9159 name = bfd_elf_string_from_elf_section (input,
9160 hdr->sh_link,
9161 isym->st_name);
9162 if (strcmp (name, h->root.root.string) != 0)
9163 continue;
9164
9165 _bfd_elf_swap_versym_in (input, ever, &iver);
9166
9167 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9168 && !(h->def_regular
9169 && h->forced_local))
9170 {
9171 /* If we have a non-hidden versioned sym, then it should
9172 have provided a definition for the undefined sym unless
9173 it is defined in a non-shared object and forced local.
9174 */
9175 abort ();
9176 }
9177
9178 version_index = iver.vs_vers & VERSYM_VERSION;
9179 if (version_index == 1 || version_index == 2)
9180 {
9181 /* This is the base or first version. We can use it. */
9182 free (extversym);
9183 free (isymbuf);
9184 return TRUE;
9185 }
9186 }
9187
9188 free (extversym);
9189 free (isymbuf);
9190 }
9191
9192 return FALSE;
9193 }
9194
9195 /* Convert ELF common symbol TYPE. */
9196
9197 static int
9198 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9199 {
9200 /* Commom symbol can only appear in relocatable link. */
9201 if (!bfd_link_relocatable (info))
9202 abort ();
9203 switch (info->elf_stt_common)
9204 {
9205 case unchanged:
9206 break;
9207 case elf_stt_common:
9208 type = STT_COMMON;
9209 break;
9210 case no_elf_stt_common:
9211 type = STT_OBJECT;
9212 break;
9213 }
9214 return type;
9215 }
9216
9217 /* Add an external symbol to the symbol table. This is called from
9218 the hash table traversal routine. When generating a shared object,
9219 we go through the symbol table twice. The first time we output
9220 anything that might have been forced to local scope in a version
9221 script. The second time we output the symbols that are still
9222 global symbols. */
9223
9224 static bfd_boolean
9225 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9226 {
9227 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9228 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9229 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9230 bfd_boolean strip;
9231 Elf_Internal_Sym sym;
9232 asection *input_sec;
9233 const struct elf_backend_data *bed;
9234 long indx;
9235 int ret;
9236 unsigned int type;
9237 /* A symbol is bound locally if it is forced local or it is locally
9238 defined, hidden versioned, not referenced by shared library and
9239 not exported when linking executable. */
9240 bfd_boolean local_bind = (h->forced_local
9241 || (bfd_link_executable (flinfo->info)
9242 && !flinfo->info->export_dynamic
9243 && !h->dynamic
9244 && !h->ref_dynamic
9245 && h->def_regular
9246 && h->versioned == versioned_hidden));
9247
9248 if (h->root.type == bfd_link_hash_warning)
9249 {
9250 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9251 if (h->root.type == bfd_link_hash_new)
9252 return TRUE;
9253 }
9254
9255 /* Decide whether to output this symbol in this pass. */
9256 if (eoinfo->localsyms)
9257 {
9258 if (!local_bind)
9259 return TRUE;
9260 }
9261 else
9262 {
9263 if (local_bind)
9264 return TRUE;
9265 }
9266
9267 bed = get_elf_backend_data (flinfo->output_bfd);
9268
9269 if (h->root.type == bfd_link_hash_undefined)
9270 {
9271 /* If we have an undefined symbol reference here then it must have
9272 come from a shared library that is being linked in. (Undefined
9273 references in regular files have already been handled unless
9274 they are in unreferenced sections which are removed by garbage
9275 collection). */
9276 bfd_boolean ignore_undef = FALSE;
9277
9278 /* Some symbols may be special in that the fact that they're
9279 undefined can be safely ignored - let backend determine that. */
9280 if (bed->elf_backend_ignore_undef_symbol)
9281 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9282
9283 /* If we are reporting errors for this situation then do so now. */
9284 if (!ignore_undef
9285 && h->ref_dynamic
9286 && (!h->ref_regular || flinfo->info->gc_sections)
9287 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9288 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9289 (*flinfo->info->callbacks->undefined_symbol)
9290 (flinfo->info, h->root.root.string,
9291 h->ref_regular ? NULL : h->root.u.undef.abfd,
9292 NULL, 0,
9293 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
9294
9295 /* Strip a global symbol defined in a discarded section. */
9296 if (h->indx == -3)
9297 return TRUE;
9298 }
9299
9300 /* We should also warn if a forced local symbol is referenced from
9301 shared libraries. */
9302 if (bfd_link_executable (flinfo->info)
9303 && h->forced_local
9304 && h->ref_dynamic
9305 && h->def_regular
9306 && !h->dynamic_def
9307 && h->ref_dynamic_nonweak
9308 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9309 {
9310 bfd *def_bfd;
9311 const char *msg;
9312 struct elf_link_hash_entry *hi = h;
9313
9314 /* Check indirect symbol. */
9315 while (hi->root.type == bfd_link_hash_indirect)
9316 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9317
9318 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9319 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9320 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9321 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9322 else
9323 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
9324 def_bfd = flinfo->output_bfd;
9325 if (hi->root.u.def.section != bfd_abs_section_ptr)
9326 def_bfd = hi->root.u.def.section->owner;
9327 (*_bfd_error_handler) (msg, flinfo->output_bfd, def_bfd,
9328 h->root.root.string);
9329 bfd_set_error (bfd_error_bad_value);
9330 eoinfo->failed = TRUE;
9331 return FALSE;
9332 }
9333
9334 /* We don't want to output symbols that have never been mentioned by
9335 a regular file, or that we have been told to strip. However, if
9336 h->indx is set to -2, the symbol is used by a reloc and we must
9337 output it. */
9338 strip = FALSE;
9339 if (h->indx == -2)
9340 ;
9341 else if ((h->def_dynamic
9342 || h->ref_dynamic
9343 || h->root.type == bfd_link_hash_new)
9344 && !h->def_regular
9345 && !h->ref_regular)
9346 strip = TRUE;
9347 else if (flinfo->info->strip == strip_all)
9348 strip = TRUE;
9349 else if (flinfo->info->strip == strip_some
9350 && bfd_hash_lookup (flinfo->info->keep_hash,
9351 h->root.root.string, FALSE, FALSE) == NULL)
9352 strip = TRUE;
9353 else if ((h->root.type == bfd_link_hash_defined
9354 || h->root.type == bfd_link_hash_defweak)
9355 && ((flinfo->info->strip_discarded
9356 && discarded_section (h->root.u.def.section))
9357 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9358 && h->root.u.def.section->owner != NULL
9359 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9360 strip = TRUE;
9361 else if ((h->root.type == bfd_link_hash_undefined
9362 || h->root.type == bfd_link_hash_undefweak)
9363 && h->root.u.undef.abfd != NULL
9364 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9365 strip = TRUE;
9366
9367 type = h->type;
9368
9369 /* If we're stripping it, and it's not a dynamic symbol, there's
9370 nothing else to do. However, if it is a forced local symbol or
9371 an ifunc symbol we need to give the backend finish_dynamic_symbol
9372 function a chance to make it dynamic. */
9373 if (strip
9374 && h->dynindx == -1
9375 && type != STT_GNU_IFUNC
9376 && !h->forced_local)
9377 return TRUE;
9378
9379 sym.st_value = 0;
9380 sym.st_size = h->size;
9381 sym.st_other = h->other;
9382 switch (h->root.type)
9383 {
9384 default:
9385 case bfd_link_hash_new:
9386 case bfd_link_hash_warning:
9387 abort ();
9388 return FALSE;
9389
9390 case bfd_link_hash_undefined:
9391 case bfd_link_hash_undefweak:
9392 input_sec = bfd_und_section_ptr;
9393 sym.st_shndx = SHN_UNDEF;
9394 break;
9395
9396 case bfd_link_hash_defined:
9397 case bfd_link_hash_defweak:
9398 {
9399 input_sec = h->root.u.def.section;
9400 if (input_sec->output_section != NULL)
9401 {
9402 sym.st_shndx =
9403 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9404 input_sec->output_section);
9405 if (sym.st_shndx == SHN_BAD)
9406 {
9407 (*_bfd_error_handler)
9408 (_("%B: could not find output section %A for input section %A"),
9409 flinfo->output_bfd, input_sec->output_section, input_sec);
9410 bfd_set_error (bfd_error_nonrepresentable_section);
9411 eoinfo->failed = TRUE;
9412 return FALSE;
9413 }
9414
9415 /* ELF symbols in relocatable files are section relative,
9416 but in nonrelocatable files they are virtual
9417 addresses. */
9418 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9419 if (!bfd_link_relocatable (flinfo->info))
9420 {
9421 sym.st_value += input_sec->output_section->vma;
9422 if (h->type == STT_TLS)
9423 {
9424 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9425 if (tls_sec != NULL)
9426 sym.st_value -= tls_sec->vma;
9427 }
9428 }
9429 }
9430 else
9431 {
9432 BFD_ASSERT (input_sec->owner == NULL
9433 || (input_sec->owner->flags & DYNAMIC) != 0);
9434 sym.st_shndx = SHN_UNDEF;
9435 input_sec = bfd_und_section_ptr;
9436 }
9437 }
9438 break;
9439
9440 case bfd_link_hash_common:
9441 input_sec = h->root.u.c.p->section;
9442 sym.st_shndx = bed->common_section_index (input_sec);
9443 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9444 break;
9445
9446 case bfd_link_hash_indirect:
9447 /* These symbols are created by symbol versioning. They point
9448 to the decorated version of the name. For example, if the
9449 symbol foo@@GNU_1.2 is the default, which should be used when
9450 foo is used with no version, then we add an indirect symbol
9451 foo which points to foo@@GNU_1.2. We ignore these symbols,
9452 since the indirected symbol is already in the hash table. */
9453 return TRUE;
9454 }
9455
9456 if (type == STT_COMMON || type == STT_OBJECT)
9457 switch (h->root.type)
9458 {
9459 case bfd_link_hash_common:
9460 type = elf_link_convert_common_type (flinfo->info, type);
9461 break;
9462 case bfd_link_hash_defined:
9463 case bfd_link_hash_defweak:
9464 if (bed->common_definition (&sym))
9465 type = elf_link_convert_common_type (flinfo->info, type);
9466 else
9467 type = STT_OBJECT;
9468 break;
9469 case bfd_link_hash_undefined:
9470 case bfd_link_hash_undefweak:
9471 break;
9472 default:
9473 abort ();
9474 }
9475
9476 if (local_bind)
9477 {
9478 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9479 /* Turn off visibility on local symbol. */
9480 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9481 }
9482 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9483 else if (h->unique_global && h->def_regular)
9484 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9485 else if (h->root.type == bfd_link_hash_undefweak
9486 || h->root.type == bfd_link_hash_defweak)
9487 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9488 else
9489 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9490 sym.st_target_internal = h->target_internal;
9491
9492 /* Give the processor backend a chance to tweak the symbol value,
9493 and also to finish up anything that needs to be done for this
9494 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9495 forced local syms when non-shared is due to a historical quirk.
9496 STT_GNU_IFUNC symbol must go through PLT. */
9497 if ((h->type == STT_GNU_IFUNC
9498 && h->def_regular
9499 && !bfd_link_relocatable (flinfo->info))
9500 || ((h->dynindx != -1
9501 || h->forced_local)
9502 && ((bfd_link_pic (flinfo->info)
9503 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9504 || h->root.type != bfd_link_hash_undefweak))
9505 || !h->forced_local)
9506 && elf_hash_table (flinfo->info)->dynamic_sections_created))
9507 {
9508 if (! ((*bed->elf_backend_finish_dynamic_symbol)
9509 (flinfo->output_bfd, flinfo->info, h, &sym)))
9510 {
9511 eoinfo->failed = TRUE;
9512 return FALSE;
9513 }
9514 }
9515
9516 /* If we are marking the symbol as undefined, and there are no
9517 non-weak references to this symbol from a regular object, then
9518 mark the symbol as weak undefined; if there are non-weak
9519 references, mark the symbol as strong. We can't do this earlier,
9520 because it might not be marked as undefined until the
9521 finish_dynamic_symbol routine gets through with it. */
9522 if (sym.st_shndx == SHN_UNDEF
9523 && h->ref_regular
9524 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9525 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9526 {
9527 int bindtype;
9528 type = ELF_ST_TYPE (sym.st_info);
9529
9530 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9531 if (type == STT_GNU_IFUNC)
9532 type = STT_FUNC;
9533
9534 if (h->ref_regular_nonweak)
9535 bindtype = STB_GLOBAL;
9536 else
9537 bindtype = STB_WEAK;
9538 sym.st_info = ELF_ST_INFO (bindtype, type);
9539 }
9540
9541 /* If this is a symbol defined in a dynamic library, don't use the
9542 symbol size from the dynamic library. Relinking an executable
9543 against a new library may introduce gratuitous changes in the
9544 executable's symbols if we keep the size. */
9545 if (sym.st_shndx == SHN_UNDEF
9546 && !h->def_regular
9547 && h->def_dynamic)
9548 sym.st_size = 0;
9549
9550 /* If a non-weak symbol with non-default visibility is not defined
9551 locally, it is a fatal error. */
9552 if (!bfd_link_relocatable (flinfo->info)
9553 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9554 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9555 && h->root.type == bfd_link_hash_undefined
9556 && !h->def_regular)
9557 {
9558 const char *msg;
9559
9560 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9561 msg = _("%B: protected symbol `%s' isn't defined");
9562 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9563 msg = _("%B: internal symbol `%s' isn't defined");
9564 else
9565 msg = _("%B: hidden symbol `%s' isn't defined");
9566 (*_bfd_error_handler) (msg, flinfo->output_bfd, h->root.root.string);
9567 bfd_set_error (bfd_error_bad_value);
9568 eoinfo->failed = TRUE;
9569 return FALSE;
9570 }
9571
9572 /* If this symbol should be put in the .dynsym section, then put it
9573 there now. We already know the symbol index. We also fill in
9574 the entry in the .hash section. */
9575 if (elf_hash_table (flinfo->info)->dynsym != NULL
9576 && h->dynindx != -1
9577 && elf_hash_table (flinfo->info)->dynamic_sections_created)
9578 {
9579 bfd_byte *esym;
9580
9581 /* Since there is no version information in the dynamic string,
9582 if there is no version info in symbol version section, we will
9583 have a run-time problem if not linking executable, referenced
9584 by shared library, not locally defined, or not bound locally.
9585 */
9586 if (h->verinfo.verdef == NULL
9587 && !local_bind
9588 && (!bfd_link_executable (flinfo->info)
9589 || h->ref_dynamic
9590 || !h->def_regular))
9591 {
9592 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9593
9594 if (p && p [1] != '\0')
9595 {
9596 (*_bfd_error_handler)
9597 (_("%B: No symbol version section for versioned symbol `%s'"),
9598 flinfo->output_bfd, h->root.root.string);
9599 eoinfo->failed = TRUE;
9600 return FALSE;
9601 }
9602 }
9603
9604 sym.st_name = h->dynstr_index;
9605 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9606 + h->dynindx * bed->s->sizeof_sym);
9607 if (!check_dynsym (flinfo->output_bfd, &sym))
9608 {
9609 eoinfo->failed = TRUE;
9610 return FALSE;
9611 }
9612 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
9613
9614 if (flinfo->hash_sec != NULL)
9615 {
9616 size_t hash_entry_size;
9617 bfd_byte *bucketpos;
9618 bfd_vma chain;
9619 size_t bucketcount;
9620 size_t bucket;
9621
9622 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
9623 bucket = h->u.elf_hash_value % bucketcount;
9624
9625 hash_entry_size
9626 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9627 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
9628 + (bucket + 2) * hash_entry_size);
9629 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9630 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9631 bucketpos);
9632 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9633 ((bfd_byte *) flinfo->hash_sec->contents
9634 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9635 }
9636
9637 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
9638 {
9639 Elf_Internal_Versym iversym;
9640 Elf_External_Versym *eversym;
9641
9642 if (!h->def_regular)
9643 {
9644 if (h->verinfo.verdef == NULL
9645 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9646 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
9647 iversym.vs_vers = 0;
9648 else
9649 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9650 }
9651 else
9652 {
9653 if (h->verinfo.vertree == NULL)
9654 iversym.vs_vers = 1;
9655 else
9656 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
9657 if (flinfo->info->create_default_symver)
9658 iversym.vs_vers++;
9659 }
9660
9661 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9662 defined locally. */
9663 if (h->versioned == versioned_hidden && h->def_regular)
9664 iversym.vs_vers |= VERSYM_HIDDEN;
9665
9666 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
9667 eversym += h->dynindx;
9668 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
9669 }
9670 }
9671
9672 /* If the symbol is undefined, and we didn't output it to .dynsym,
9673 strip it from .symtab too. Obviously we can't do this for
9674 relocatable output or when needed for --emit-relocs. */
9675 else if (input_sec == bfd_und_section_ptr
9676 && h->indx != -2
9677 && !bfd_link_relocatable (flinfo->info))
9678 return TRUE;
9679 /* Also strip others that we couldn't earlier due to dynamic symbol
9680 processing. */
9681 if (strip)
9682 return TRUE;
9683 if ((input_sec->flags & SEC_EXCLUDE) != 0)
9684 return TRUE;
9685
9686 /* Output a FILE symbol so that following locals are not associated
9687 with the wrong input file. We need one for forced local symbols
9688 if we've seen more than one FILE symbol or when we have exactly
9689 one FILE symbol but global symbols are present in a file other
9690 than the one with the FILE symbol. We also need one if linker
9691 defined symbols are present. In practice these conditions are
9692 always met, so just emit the FILE symbol unconditionally. */
9693 if (eoinfo->localsyms
9694 && !eoinfo->file_sym_done
9695 && eoinfo->flinfo->filesym_count != 0)
9696 {
9697 Elf_Internal_Sym fsym;
9698
9699 memset (&fsym, 0, sizeof (fsym));
9700 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9701 fsym.st_shndx = SHN_ABS;
9702 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9703 bfd_und_section_ptr, NULL))
9704 return FALSE;
9705
9706 eoinfo->file_sym_done = TRUE;
9707 }
9708
9709 indx = bfd_get_symcount (flinfo->output_bfd);
9710 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9711 input_sec, h);
9712 if (ret == 0)
9713 {
9714 eoinfo->failed = TRUE;
9715 return FALSE;
9716 }
9717 else if (ret == 1)
9718 h->indx = indx;
9719 else if (h->indx == -2)
9720 abort();
9721
9722 return TRUE;
9723 }
9724
9725 /* Return TRUE if special handling is done for relocs in SEC against
9726 symbols defined in discarded sections. */
9727
9728 static bfd_boolean
9729 elf_section_ignore_discarded_relocs (asection *sec)
9730 {
9731 const struct elf_backend_data *bed;
9732
9733 switch (sec->sec_info_type)
9734 {
9735 case SEC_INFO_TYPE_STABS:
9736 case SEC_INFO_TYPE_EH_FRAME:
9737 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
9738 return TRUE;
9739 default:
9740 break;
9741 }
9742
9743 bed = get_elf_backend_data (sec->owner);
9744 if (bed->elf_backend_ignore_discarded_relocs != NULL
9745 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9746 return TRUE;
9747
9748 return FALSE;
9749 }
9750
9751 /* Return a mask saying how ld should treat relocations in SEC against
9752 symbols defined in discarded sections. If this function returns
9753 COMPLAIN set, ld will issue a warning message. If this function
9754 returns PRETEND set, and the discarded section was link-once and the
9755 same size as the kept link-once section, ld will pretend that the
9756 symbol was actually defined in the kept section. Otherwise ld will
9757 zero the reloc (at least that is the intent, but some cooperation by
9758 the target dependent code is needed, particularly for REL targets). */
9759
9760 unsigned int
9761 _bfd_elf_default_action_discarded (asection *sec)
9762 {
9763 if (sec->flags & SEC_DEBUGGING)
9764 return PRETEND;
9765
9766 if (strcmp (".eh_frame", sec->name) == 0)
9767 return 0;
9768
9769 if (strcmp (".gcc_except_table", sec->name) == 0)
9770 return 0;
9771
9772 return COMPLAIN | PRETEND;
9773 }
9774
9775 /* Find a match between a section and a member of a section group. */
9776
9777 static asection *
9778 match_group_member (asection *sec, asection *group,
9779 struct bfd_link_info *info)
9780 {
9781 asection *first = elf_next_in_group (group);
9782 asection *s = first;
9783
9784 while (s != NULL)
9785 {
9786 if (bfd_elf_match_symbols_in_sections (s, sec, info))
9787 return s;
9788
9789 s = elf_next_in_group (s);
9790 if (s == first)
9791 break;
9792 }
9793
9794 return NULL;
9795 }
9796
9797 /* Check if the kept section of a discarded section SEC can be used
9798 to replace it. Return the replacement if it is OK. Otherwise return
9799 NULL. */
9800
9801 asection *
9802 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
9803 {
9804 asection *kept;
9805
9806 kept = sec->kept_section;
9807 if (kept != NULL)
9808 {
9809 if ((kept->flags & SEC_GROUP) != 0)
9810 kept = match_group_member (sec, kept, info);
9811 if (kept != NULL
9812 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9813 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
9814 kept = NULL;
9815 sec->kept_section = kept;
9816 }
9817 return kept;
9818 }
9819
9820 /* Link an input file into the linker output file. This function
9821 handles all the sections and relocations of the input file at once.
9822 This is so that we only have to read the local symbols once, and
9823 don't have to keep them in memory. */
9824
9825 static bfd_boolean
9826 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
9827 {
9828 int (*relocate_section)
9829 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9830 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9831 bfd *output_bfd;
9832 Elf_Internal_Shdr *symtab_hdr;
9833 size_t locsymcount;
9834 size_t extsymoff;
9835 Elf_Internal_Sym *isymbuf;
9836 Elf_Internal_Sym *isym;
9837 Elf_Internal_Sym *isymend;
9838 long *pindex;
9839 asection **ppsection;
9840 asection *o;
9841 const struct elf_backend_data *bed;
9842 struct elf_link_hash_entry **sym_hashes;
9843 bfd_size_type address_size;
9844 bfd_vma r_type_mask;
9845 int r_sym_shift;
9846 bfd_boolean have_file_sym = FALSE;
9847
9848 output_bfd = flinfo->output_bfd;
9849 bed = get_elf_backend_data (output_bfd);
9850 relocate_section = bed->elf_backend_relocate_section;
9851
9852 /* If this is a dynamic object, we don't want to do anything here:
9853 we don't want the local symbols, and we don't want the section
9854 contents. */
9855 if ((input_bfd->flags & DYNAMIC) != 0)
9856 return TRUE;
9857
9858 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9859 if (elf_bad_symtab (input_bfd))
9860 {
9861 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9862 extsymoff = 0;
9863 }
9864 else
9865 {
9866 locsymcount = symtab_hdr->sh_info;
9867 extsymoff = symtab_hdr->sh_info;
9868 }
9869
9870 /* Read the local symbols. */
9871 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9872 if (isymbuf == NULL && locsymcount != 0)
9873 {
9874 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
9875 flinfo->internal_syms,
9876 flinfo->external_syms,
9877 flinfo->locsym_shndx);
9878 if (isymbuf == NULL)
9879 return FALSE;
9880 }
9881
9882 /* Find local symbol sections and adjust values of symbols in
9883 SEC_MERGE sections. Write out those local symbols we know are
9884 going into the output file. */
9885 isymend = isymbuf + locsymcount;
9886 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
9887 isym < isymend;
9888 isym++, pindex++, ppsection++)
9889 {
9890 asection *isec;
9891 const char *name;
9892 Elf_Internal_Sym osym;
9893 long indx;
9894 int ret;
9895
9896 *pindex = -1;
9897
9898 if (elf_bad_symtab (input_bfd))
9899 {
9900 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9901 {
9902 *ppsection = NULL;
9903 continue;
9904 }
9905 }
9906
9907 if (isym->st_shndx == SHN_UNDEF)
9908 isec = bfd_und_section_ptr;
9909 else if (isym->st_shndx == SHN_ABS)
9910 isec = bfd_abs_section_ptr;
9911 else if (isym->st_shndx == SHN_COMMON)
9912 isec = bfd_com_section_ptr;
9913 else
9914 {
9915 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9916 if (isec == NULL)
9917 {
9918 /* Don't attempt to output symbols with st_shnx in the
9919 reserved range other than SHN_ABS and SHN_COMMON. */
9920 *ppsection = NULL;
9921 continue;
9922 }
9923 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
9924 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9925 isym->st_value =
9926 _bfd_merged_section_offset (output_bfd, &isec,
9927 elf_section_data (isec)->sec_info,
9928 isym->st_value);
9929 }
9930
9931 *ppsection = isec;
9932
9933 /* Don't output the first, undefined, symbol. In fact, don't
9934 output any undefined local symbol. */
9935 if (isec == bfd_und_section_ptr)
9936 continue;
9937
9938 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9939 {
9940 /* We never output section symbols. Instead, we use the
9941 section symbol of the corresponding section in the output
9942 file. */
9943 continue;
9944 }
9945
9946 /* If we are stripping all symbols, we don't want to output this
9947 one. */
9948 if (flinfo->info->strip == strip_all)
9949 continue;
9950
9951 /* If we are discarding all local symbols, we don't want to
9952 output this one. If we are generating a relocatable output
9953 file, then some of the local symbols may be required by
9954 relocs; we output them below as we discover that they are
9955 needed. */
9956 if (flinfo->info->discard == discard_all)
9957 continue;
9958
9959 /* If this symbol is defined in a section which we are
9960 discarding, we don't need to keep it. */
9961 if (isym->st_shndx != SHN_UNDEF
9962 && isym->st_shndx < SHN_LORESERVE
9963 && bfd_section_removed_from_list (output_bfd,
9964 isec->output_section))
9965 continue;
9966
9967 /* Get the name of the symbol. */
9968 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9969 isym->st_name);
9970 if (name == NULL)
9971 return FALSE;
9972
9973 /* See if we are discarding symbols with this name. */
9974 if ((flinfo->info->strip == strip_some
9975 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
9976 == NULL))
9977 || (((flinfo->info->discard == discard_sec_merge
9978 && (isec->flags & SEC_MERGE)
9979 && !bfd_link_relocatable (flinfo->info))
9980 || flinfo->info->discard == discard_l)
9981 && bfd_is_local_label_name (input_bfd, name)))
9982 continue;
9983
9984 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
9985 {
9986 if (input_bfd->lto_output)
9987 /* -flto puts a temp file name here. This means builds
9988 are not reproducible. Discard the symbol. */
9989 continue;
9990 have_file_sym = TRUE;
9991 flinfo->filesym_count += 1;
9992 }
9993 if (!have_file_sym)
9994 {
9995 /* In the absence of debug info, bfd_find_nearest_line uses
9996 FILE symbols to determine the source file for local
9997 function symbols. Provide a FILE symbol here if input
9998 files lack such, so that their symbols won't be
9999 associated with a previous input file. It's not the
10000 source file, but the best we can do. */
10001 have_file_sym = TRUE;
10002 flinfo->filesym_count += 1;
10003 memset (&osym, 0, sizeof (osym));
10004 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10005 osym.st_shndx = SHN_ABS;
10006 if (!elf_link_output_symstrtab (flinfo,
10007 (input_bfd->lto_output ? NULL
10008 : input_bfd->filename),
10009 &osym, bfd_abs_section_ptr,
10010 NULL))
10011 return FALSE;
10012 }
10013
10014 osym = *isym;
10015
10016 /* Adjust the section index for the output file. */
10017 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10018 isec->output_section);
10019 if (osym.st_shndx == SHN_BAD)
10020 return FALSE;
10021
10022 /* ELF symbols in relocatable files are section relative, but
10023 in executable files they are virtual addresses. Note that
10024 this code assumes that all ELF sections have an associated
10025 BFD section with a reasonable value for output_offset; below
10026 we assume that they also have a reasonable value for
10027 output_section. Any special sections must be set up to meet
10028 these requirements. */
10029 osym.st_value += isec->output_offset;
10030 if (!bfd_link_relocatable (flinfo->info))
10031 {
10032 osym.st_value += isec->output_section->vma;
10033 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10034 {
10035 /* STT_TLS symbols are relative to PT_TLS segment base. */
10036 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10037 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10038 }
10039 }
10040
10041 indx = bfd_get_symcount (output_bfd);
10042 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10043 if (ret == 0)
10044 return FALSE;
10045 else if (ret == 1)
10046 *pindex = indx;
10047 }
10048
10049 if (bed->s->arch_size == 32)
10050 {
10051 r_type_mask = 0xff;
10052 r_sym_shift = 8;
10053 address_size = 4;
10054 }
10055 else
10056 {
10057 r_type_mask = 0xffffffff;
10058 r_sym_shift = 32;
10059 address_size = 8;
10060 }
10061
10062 /* Relocate the contents of each section. */
10063 sym_hashes = elf_sym_hashes (input_bfd);
10064 for (o = input_bfd->sections; o != NULL; o = o->next)
10065 {
10066 bfd_byte *contents;
10067
10068 if (! o->linker_mark)
10069 {
10070 /* This section was omitted from the link. */
10071 continue;
10072 }
10073
10074 if (bfd_link_relocatable (flinfo->info)
10075 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10076 {
10077 /* Deal with the group signature symbol. */
10078 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10079 unsigned long symndx = sec_data->this_hdr.sh_info;
10080 asection *osec = o->output_section;
10081
10082 if (symndx >= locsymcount
10083 || (elf_bad_symtab (input_bfd)
10084 && flinfo->sections[symndx] == NULL))
10085 {
10086 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10087 while (h->root.type == bfd_link_hash_indirect
10088 || h->root.type == bfd_link_hash_warning)
10089 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10090 /* Arrange for symbol to be output. */
10091 h->indx = -2;
10092 elf_section_data (osec)->this_hdr.sh_info = -2;
10093 }
10094 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10095 {
10096 /* We'll use the output section target_index. */
10097 asection *sec = flinfo->sections[symndx]->output_section;
10098 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10099 }
10100 else
10101 {
10102 if (flinfo->indices[symndx] == -1)
10103 {
10104 /* Otherwise output the local symbol now. */
10105 Elf_Internal_Sym sym = isymbuf[symndx];
10106 asection *sec = flinfo->sections[symndx]->output_section;
10107 const char *name;
10108 long indx;
10109 int ret;
10110
10111 name = bfd_elf_string_from_elf_section (input_bfd,
10112 symtab_hdr->sh_link,
10113 sym.st_name);
10114 if (name == NULL)
10115 return FALSE;
10116
10117 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10118 sec);
10119 if (sym.st_shndx == SHN_BAD)
10120 return FALSE;
10121
10122 sym.st_value += o->output_offset;
10123
10124 indx = bfd_get_symcount (output_bfd);
10125 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10126 NULL);
10127 if (ret == 0)
10128 return FALSE;
10129 else if (ret == 1)
10130 flinfo->indices[symndx] = indx;
10131 else
10132 abort ();
10133 }
10134 elf_section_data (osec)->this_hdr.sh_info
10135 = flinfo->indices[symndx];
10136 }
10137 }
10138
10139 if ((o->flags & SEC_HAS_CONTENTS) == 0
10140 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10141 continue;
10142
10143 if ((o->flags & SEC_LINKER_CREATED) != 0)
10144 {
10145 /* Section was created by _bfd_elf_link_create_dynamic_sections
10146 or somesuch. */
10147 continue;
10148 }
10149
10150 /* Get the contents of the section. They have been cached by a
10151 relaxation routine. Note that o is a section in an input
10152 file, so the contents field will not have been set by any of
10153 the routines which work on output files. */
10154 if (elf_section_data (o)->this_hdr.contents != NULL)
10155 {
10156 contents = elf_section_data (o)->this_hdr.contents;
10157 if (bed->caches_rawsize
10158 && o->rawsize != 0
10159 && o->rawsize < o->size)
10160 {
10161 memcpy (flinfo->contents, contents, o->rawsize);
10162 contents = flinfo->contents;
10163 }
10164 }
10165 else
10166 {
10167 contents = flinfo->contents;
10168 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10169 return FALSE;
10170 }
10171
10172 if ((o->flags & SEC_RELOC) != 0)
10173 {
10174 Elf_Internal_Rela *internal_relocs;
10175 Elf_Internal_Rela *rel, *relend;
10176 int action_discarded;
10177 int ret;
10178
10179 /* Get the swapped relocs. */
10180 internal_relocs
10181 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10182 flinfo->internal_relocs, FALSE);
10183 if (internal_relocs == NULL
10184 && o->reloc_count > 0)
10185 return FALSE;
10186
10187 /* We need to reverse-copy input .ctors/.dtors sections if
10188 they are placed in .init_array/.finit_array for output. */
10189 if (o->size > address_size
10190 && ((strncmp (o->name, ".ctors", 6) == 0
10191 && strcmp (o->output_section->name,
10192 ".init_array") == 0)
10193 || (strncmp (o->name, ".dtors", 6) == 0
10194 && strcmp (o->output_section->name,
10195 ".fini_array") == 0))
10196 && (o->name[6] == 0 || o->name[6] == '.'))
10197 {
10198 if (o->size != o->reloc_count * address_size)
10199 {
10200 (*_bfd_error_handler)
10201 (_("error: %B: size of section %A is not "
10202 "multiple of address size"),
10203 input_bfd, o);
10204 bfd_set_error (bfd_error_on_input);
10205 return FALSE;
10206 }
10207 o->flags |= SEC_ELF_REVERSE_COPY;
10208 }
10209
10210 action_discarded = -1;
10211 if (!elf_section_ignore_discarded_relocs (o))
10212 action_discarded = (*bed->action_discarded) (o);
10213
10214 /* Run through the relocs evaluating complex reloc symbols and
10215 looking for relocs against symbols from discarded sections
10216 or section symbols from removed link-once sections.
10217 Complain about relocs against discarded sections. Zero
10218 relocs against removed link-once sections. */
10219
10220 rel = internal_relocs;
10221 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
10222 for ( ; rel < relend; rel++)
10223 {
10224 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10225 unsigned int s_type;
10226 asection **ps, *sec;
10227 struct elf_link_hash_entry *h = NULL;
10228 const char *sym_name;
10229
10230 if (r_symndx == STN_UNDEF)
10231 continue;
10232
10233 if (r_symndx >= locsymcount
10234 || (elf_bad_symtab (input_bfd)
10235 && flinfo->sections[r_symndx] == NULL))
10236 {
10237 h = sym_hashes[r_symndx - extsymoff];
10238
10239 /* Badly formatted input files can contain relocs that
10240 reference non-existant symbols. Check here so that
10241 we do not seg fault. */
10242 if (h == NULL)
10243 {
10244 char buffer [32];
10245
10246 sprintf_vma (buffer, rel->r_info);
10247 (*_bfd_error_handler)
10248 (_("error: %B contains a reloc (0x%s) for section %A "
10249 "that references a non-existent global symbol"),
10250 input_bfd, o, buffer);
10251 bfd_set_error (bfd_error_bad_value);
10252 return FALSE;
10253 }
10254
10255 while (h->root.type == bfd_link_hash_indirect
10256 || h->root.type == bfd_link_hash_warning)
10257 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10258
10259 s_type = h->type;
10260
10261 /* If a plugin symbol is referenced from a non-IR file,
10262 mark the symbol as undefined. Note that the
10263 linker may attach linker created dynamic sections
10264 to the plugin bfd. Symbols defined in linker
10265 created sections are not plugin symbols. */
10266 if (h->root.non_ir_ref
10267 && (h->root.type == bfd_link_hash_defined
10268 || h->root.type == bfd_link_hash_defweak)
10269 && (h->root.u.def.section->flags
10270 & SEC_LINKER_CREATED) == 0
10271 && h->root.u.def.section->owner != NULL
10272 && (h->root.u.def.section->owner->flags
10273 & BFD_PLUGIN) != 0)
10274 {
10275 h->root.type = bfd_link_hash_undefined;
10276 h->root.u.undef.abfd = h->root.u.def.section->owner;
10277 }
10278
10279 ps = NULL;
10280 if (h->root.type == bfd_link_hash_defined
10281 || h->root.type == bfd_link_hash_defweak)
10282 ps = &h->root.u.def.section;
10283
10284 sym_name = h->root.root.string;
10285 }
10286 else
10287 {
10288 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10289
10290 s_type = ELF_ST_TYPE (sym->st_info);
10291 ps = &flinfo->sections[r_symndx];
10292 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10293 sym, *ps);
10294 }
10295
10296 if ((s_type == STT_RELC || s_type == STT_SRELC)
10297 && !bfd_link_relocatable (flinfo->info))
10298 {
10299 bfd_vma val;
10300 bfd_vma dot = (rel->r_offset
10301 + o->output_offset + o->output_section->vma);
10302 #ifdef DEBUG
10303 printf ("Encountered a complex symbol!");
10304 printf (" (input_bfd %s, section %s, reloc %ld\n",
10305 input_bfd->filename, o->name,
10306 (long) (rel - internal_relocs));
10307 printf (" symbol: idx %8.8lx, name %s\n",
10308 r_symndx, sym_name);
10309 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10310 (unsigned long) rel->r_info,
10311 (unsigned long) rel->r_offset);
10312 #endif
10313 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10314 isymbuf, locsymcount, s_type == STT_SRELC))
10315 return FALSE;
10316
10317 /* Symbol evaluated OK. Update to absolute value. */
10318 set_symbol_value (input_bfd, isymbuf, locsymcount,
10319 r_symndx, val);
10320 continue;
10321 }
10322
10323 if (action_discarded != -1 && ps != NULL)
10324 {
10325 /* Complain if the definition comes from a
10326 discarded section. */
10327 if ((sec = *ps) != NULL && discarded_section (sec))
10328 {
10329 BFD_ASSERT (r_symndx != STN_UNDEF);
10330 if (action_discarded & COMPLAIN)
10331 (*flinfo->info->callbacks->einfo)
10332 (_("%X`%s' referenced in section `%A' of %B: "
10333 "defined in discarded section `%A' of %B\n"),
10334 sym_name, o, input_bfd, sec, sec->owner);
10335
10336 /* Try to do the best we can to support buggy old
10337 versions of gcc. Pretend that the symbol is
10338 really defined in the kept linkonce section.
10339 FIXME: This is quite broken. Modifying the
10340 symbol here means we will be changing all later
10341 uses of the symbol, not just in this section. */
10342 if (action_discarded & PRETEND)
10343 {
10344 asection *kept;
10345
10346 kept = _bfd_elf_check_kept_section (sec,
10347 flinfo->info);
10348 if (kept != NULL)
10349 {
10350 *ps = kept;
10351 continue;
10352 }
10353 }
10354 }
10355 }
10356 }
10357
10358 /* Relocate the section by invoking a back end routine.
10359
10360 The back end routine is responsible for adjusting the
10361 section contents as necessary, and (if using Rela relocs
10362 and generating a relocatable output file) adjusting the
10363 reloc addend as necessary.
10364
10365 The back end routine does not have to worry about setting
10366 the reloc address or the reloc symbol index.
10367
10368 The back end routine is given a pointer to the swapped in
10369 internal symbols, and can access the hash table entries
10370 for the external symbols via elf_sym_hashes (input_bfd).
10371
10372 When generating relocatable output, the back end routine
10373 must handle STB_LOCAL/STT_SECTION symbols specially. The
10374 output symbol is going to be a section symbol
10375 corresponding to the output section, which will require
10376 the addend to be adjusted. */
10377
10378 ret = (*relocate_section) (output_bfd, flinfo->info,
10379 input_bfd, o, contents,
10380 internal_relocs,
10381 isymbuf,
10382 flinfo->sections);
10383 if (!ret)
10384 return FALSE;
10385
10386 if (ret == 2
10387 || bfd_link_relocatable (flinfo->info)
10388 || flinfo->info->emitrelocations)
10389 {
10390 Elf_Internal_Rela *irela;
10391 Elf_Internal_Rela *irelaend, *irelamid;
10392 bfd_vma last_offset;
10393 struct elf_link_hash_entry **rel_hash;
10394 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10395 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10396 unsigned int next_erel;
10397 bfd_boolean rela_normal;
10398 struct bfd_elf_section_data *esdi, *esdo;
10399
10400 esdi = elf_section_data (o);
10401 esdo = elf_section_data (o->output_section);
10402 rela_normal = FALSE;
10403
10404 /* Adjust the reloc addresses and symbol indices. */
10405
10406 irela = internal_relocs;
10407 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
10408 rel_hash = esdo->rel.hashes + esdo->rel.count;
10409 /* We start processing the REL relocs, if any. When we reach
10410 IRELAMID in the loop, we switch to the RELA relocs. */
10411 irelamid = irela;
10412 if (esdi->rel.hdr != NULL)
10413 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10414 * bed->s->int_rels_per_ext_rel);
10415 rel_hash_list = rel_hash;
10416 rela_hash_list = NULL;
10417 last_offset = o->output_offset;
10418 if (!bfd_link_relocatable (flinfo->info))
10419 last_offset += o->output_section->vma;
10420 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10421 {
10422 unsigned long r_symndx;
10423 asection *sec;
10424 Elf_Internal_Sym sym;
10425
10426 if (next_erel == bed->s->int_rels_per_ext_rel)
10427 {
10428 rel_hash++;
10429 next_erel = 0;
10430 }
10431
10432 if (irela == irelamid)
10433 {
10434 rel_hash = esdo->rela.hashes + esdo->rela.count;
10435 rela_hash_list = rel_hash;
10436 rela_normal = bed->rela_normal;
10437 }
10438
10439 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10440 flinfo->info, o,
10441 irela->r_offset);
10442 if (irela->r_offset >= (bfd_vma) -2)
10443 {
10444 /* This is a reloc for a deleted entry or somesuch.
10445 Turn it into an R_*_NONE reloc, at the same
10446 offset as the last reloc. elf_eh_frame.c and
10447 bfd_elf_discard_info rely on reloc offsets
10448 being ordered. */
10449 irela->r_offset = last_offset;
10450 irela->r_info = 0;
10451 irela->r_addend = 0;
10452 continue;
10453 }
10454
10455 irela->r_offset += o->output_offset;
10456
10457 /* Relocs in an executable have to be virtual addresses. */
10458 if (!bfd_link_relocatable (flinfo->info))
10459 irela->r_offset += o->output_section->vma;
10460
10461 last_offset = irela->r_offset;
10462
10463 r_symndx = irela->r_info >> r_sym_shift;
10464 if (r_symndx == STN_UNDEF)
10465 continue;
10466
10467 if (r_symndx >= locsymcount
10468 || (elf_bad_symtab (input_bfd)
10469 && flinfo->sections[r_symndx] == NULL))
10470 {
10471 struct elf_link_hash_entry *rh;
10472 unsigned long indx;
10473
10474 /* This is a reloc against a global symbol. We
10475 have not yet output all the local symbols, so
10476 we do not know the symbol index of any global
10477 symbol. We set the rel_hash entry for this
10478 reloc to point to the global hash table entry
10479 for this symbol. The symbol index is then
10480 set at the end of bfd_elf_final_link. */
10481 indx = r_symndx - extsymoff;
10482 rh = elf_sym_hashes (input_bfd)[indx];
10483 while (rh->root.type == bfd_link_hash_indirect
10484 || rh->root.type == bfd_link_hash_warning)
10485 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10486
10487 /* Setting the index to -2 tells
10488 elf_link_output_extsym that this symbol is
10489 used by a reloc. */
10490 BFD_ASSERT (rh->indx < 0);
10491 rh->indx = -2;
10492
10493 *rel_hash = rh;
10494
10495 continue;
10496 }
10497
10498 /* This is a reloc against a local symbol. */
10499
10500 *rel_hash = NULL;
10501 sym = isymbuf[r_symndx];
10502 sec = flinfo->sections[r_symndx];
10503 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10504 {
10505 /* I suppose the backend ought to fill in the
10506 section of any STT_SECTION symbol against a
10507 processor specific section. */
10508 r_symndx = STN_UNDEF;
10509 if (bfd_is_abs_section (sec))
10510 ;
10511 else if (sec == NULL || sec->owner == NULL)
10512 {
10513 bfd_set_error (bfd_error_bad_value);
10514 return FALSE;
10515 }
10516 else
10517 {
10518 asection *osec = sec->output_section;
10519
10520 /* If we have discarded a section, the output
10521 section will be the absolute section. In
10522 case of discarded SEC_MERGE sections, use
10523 the kept section. relocate_section should
10524 have already handled discarded linkonce
10525 sections. */
10526 if (bfd_is_abs_section (osec)
10527 && sec->kept_section != NULL
10528 && sec->kept_section->output_section != NULL)
10529 {
10530 osec = sec->kept_section->output_section;
10531 irela->r_addend -= osec->vma;
10532 }
10533
10534 if (!bfd_is_abs_section (osec))
10535 {
10536 r_symndx = osec->target_index;
10537 if (r_symndx == STN_UNDEF)
10538 {
10539 irela->r_addend += osec->vma;
10540 osec = _bfd_nearby_section (output_bfd, osec,
10541 osec->vma);
10542 irela->r_addend -= osec->vma;
10543 r_symndx = osec->target_index;
10544 }
10545 }
10546 }
10547
10548 /* Adjust the addend according to where the
10549 section winds up in the output section. */
10550 if (rela_normal)
10551 irela->r_addend += sec->output_offset;
10552 }
10553 else
10554 {
10555 if (flinfo->indices[r_symndx] == -1)
10556 {
10557 unsigned long shlink;
10558 const char *name;
10559 asection *osec;
10560 long indx;
10561
10562 if (flinfo->info->strip == strip_all)
10563 {
10564 /* You can't do ld -r -s. */
10565 bfd_set_error (bfd_error_invalid_operation);
10566 return FALSE;
10567 }
10568
10569 /* This symbol was skipped earlier, but
10570 since it is needed by a reloc, we
10571 must output it now. */
10572 shlink = symtab_hdr->sh_link;
10573 name = (bfd_elf_string_from_elf_section
10574 (input_bfd, shlink, sym.st_name));
10575 if (name == NULL)
10576 return FALSE;
10577
10578 osec = sec->output_section;
10579 sym.st_shndx =
10580 _bfd_elf_section_from_bfd_section (output_bfd,
10581 osec);
10582 if (sym.st_shndx == SHN_BAD)
10583 return FALSE;
10584
10585 sym.st_value += sec->output_offset;
10586 if (!bfd_link_relocatable (flinfo->info))
10587 {
10588 sym.st_value += osec->vma;
10589 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10590 {
10591 /* STT_TLS symbols are relative to PT_TLS
10592 segment base. */
10593 BFD_ASSERT (elf_hash_table (flinfo->info)
10594 ->tls_sec != NULL);
10595 sym.st_value -= (elf_hash_table (flinfo->info)
10596 ->tls_sec->vma);
10597 }
10598 }
10599
10600 indx = bfd_get_symcount (output_bfd);
10601 ret = elf_link_output_symstrtab (flinfo, name,
10602 &sym, sec,
10603 NULL);
10604 if (ret == 0)
10605 return FALSE;
10606 else if (ret == 1)
10607 flinfo->indices[r_symndx] = indx;
10608 else
10609 abort ();
10610 }
10611
10612 r_symndx = flinfo->indices[r_symndx];
10613 }
10614
10615 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10616 | (irela->r_info & r_type_mask));
10617 }
10618
10619 /* Swap out the relocs. */
10620 input_rel_hdr = esdi->rel.hdr;
10621 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
10622 {
10623 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10624 input_rel_hdr,
10625 internal_relocs,
10626 rel_hash_list))
10627 return FALSE;
10628 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10629 * bed->s->int_rels_per_ext_rel);
10630 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
10631 }
10632
10633 input_rela_hdr = esdi->rela.hdr;
10634 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10635 {
10636 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10637 input_rela_hdr,
10638 internal_relocs,
10639 rela_hash_list))
10640 return FALSE;
10641 }
10642 }
10643 }
10644
10645 /* Write out the modified section contents. */
10646 if (bed->elf_backend_write_section
10647 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
10648 contents))
10649 {
10650 /* Section written out. */
10651 }
10652 else switch (o->sec_info_type)
10653 {
10654 case SEC_INFO_TYPE_STABS:
10655 if (! (_bfd_write_section_stabs
10656 (output_bfd,
10657 &elf_hash_table (flinfo->info)->stab_info,
10658 o, &elf_section_data (o)->sec_info, contents)))
10659 return FALSE;
10660 break;
10661 case SEC_INFO_TYPE_MERGE:
10662 if (! _bfd_write_merged_section (output_bfd, o,
10663 elf_section_data (o)->sec_info))
10664 return FALSE;
10665 break;
10666 case SEC_INFO_TYPE_EH_FRAME:
10667 {
10668 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
10669 o, contents))
10670 return FALSE;
10671 }
10672 break;
10673 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10674 {
10675 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10676 flinfo->info,
10677 o, contents))
10678 return FALSE;
10679 }
10680 break;
10681 default:
10682 {
10683 if (! (o->flags & SEC_EXCLUDE))
10684 {
10685 file_ptr offset = (file_ptr) o->output_offset;
10686 bfd_size_type todo = o->size;
10687
10688 offset *= bfd_octets_per_byte (output_bfd);
10689
10690 if ((o->flags & SEC_ELF_REVERSE_COPY))
10691 {
10692 /* Reverse-copy input section to output. */
10693 do
10694 {
10695 todo -= address_size;
10696 if (! bfd_set_section_contents (output_bfd,
10697 o->output_section,
10698 contents + todo,
10699 offset,
10700 address_size))
10701 return FALSE;
10702 if (todo == 0)
10703 break;
10704 offset += address_size;
10705 }
10706 while (1);
10707 }
10708 else if (! bfd_set_section_contents (output_bfd,
10709 o->output_section,
10710 contents,
10711 offset, todo))
10712 return FALSE;
10713 }
10714 }
10715 break;
10716 }
10717 }
10718
10719 return TRUE;
10720 }
10721
10722 /* Generate a reloc when linking an ELF file. This is a reloc
10723 requested by the linker, and does not come from any input file. This
10724 is used to build constructor and destructor tables when linking
10725 with -Ur. */
10726
10727 static bfd_boolean
10728 elf_reloc_link_order (bfd *output_bfd,
10729 struct bfd_link_info *info,
10730 asection *output_section,
10731 struct bfd_link_order *link_order)
10732 {
10733 reloc_howto_type *howto;
10734 long indx;
10735 bfd_vma offset;
10736 bfd_vma addend;
10737 struct bfd_elf_section_reloc_data *reldata;
10738 struct elf_link_hash_entry **rel_hash_ptr;
10739 Elf_Internal_Shdr *rel_hdr;
10740 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10741 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10742 bfd_byte *erel;
10743 unsigned int i;
10744 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
10745
10746 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10747 if (howto == NULL)
10748 {
10749 bfd_set_error (bfd_error_bad_value);
10750 return FALSE;
10751 }
10752
10753 addend = link_order->u.reloc.p->addend;
10754
10755 if (esdo->rel.hdr)
10756 reldata = &esdo->rel;
10757 else if (esdo->rela.hdr)
10758 reldata = &esdo->rela;
10759 else
10760 {
10761 reldata = NULL;
10762 BFD_ASSERT (0);
10763 }
10764
10765 /* Figure out the symbol index. */
10766 rel_hash_ptr = reldata->hashes + reldata->count;
10767 if (link_order->type == bfd_section_reloc_link_order)
10768 {
10769 indx = link_order->u.reloc.p->u.section->target_index;
10770 BFD_ASSERT (indx != 0);
10771 *rel_hash_ptr = NULL;
10772 }
10773 else
10774 {
10775 struct elf_link_hash_entry *h;
10776
10777 /* Treat a reloc against a defined symbol as though it were
10778 actually against the section. */
10779 h = ((struct elf_link_hash_entry *)
10780 bfd_wrapped_link_hash_lookup (output_bfd, info,
10781 link_order->u.reloc.p->u.name,
10782 FALSE, FALSE, TRUE));
10783 if (h != NULL
10784 && (h->root.type == bfd_link_hash_defined
10785 || h->root.type == bfd_link_hash_defweak))
10786 {
10787 asection *section;
10788
10789 section = h->root.u.def.section;
10790 indx = section->output_section->target_index;
10791 *rel_hash_ptr = NULL;
10792 /* It seems that we ought to add the symbol value to the
10793 addend here, but in practice it has already been added
10794 because it was passed to constructor_callback. */
10795 addend += section->output_section->vma + section->output_offset;
10796 }
10797 else if (h != NULL)
10798 {
10799 /* Setting the index to -2 tells elf_link_output_extsym that
10800 this symbol is used by a reloc. */
10801 h->indx = -2;
10802 *rel_hash_ptr = h;
10803 indx = 0;
10804 }
10805 else
10806 {
10807 (*info->callbacks->unattached_reloc)
10808 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
10809 indx = 0;
10810 }
10811 }
10812
10813 /* If this is an inplace reloc, we must write the addend into the
10814 object file. */
10815 if (howto->partial_inplace && addend != 0)
10816 {
10817 bfd_size_type size;
10818 bfd_reloc_status_type rstat;
10819 bfd_byte *buf;
10820 bfd_boolean ok;
10821 const char *sym_name;
10822
10823 size = (bfd_size_type) bfd_get_reloc_size (howto);
10824 buf = (bfd_byte *) bfd_zmalloc (size);
10825 if (buf == NULL && size != 0)
10826 return FALSE;
10827 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10828 switch (rstat)
10829 {
10830 case bfd_reloc_ok:
10831 break;
10832
10833 default:
10834 case bfd_reloc_outofrange:
10835 abort ();
10836
10837 case bfd_reloc_overflow:
10838 if (link_order->type == bfd_section_reloc_link_order)
10839 sym_name = bfd_section_name (output_bfd,
10840 link_order->u.reloc.p->u.section);
10841 else
10842 sym_name = link_order->u.reloc.p->u.name;
10843 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
10844 howto->name, addend, NULL, NULL,
10845 (bfd_vma) 0);
10846 break;
10847 }
10848
10849 ok = bfd_set_section_contents (output_bfd, output_section, buf,
10850 link_order->offset
10851 * bfd_octets_per_byte (output_bfd),
10852 size);
10853 free (buf);
10854 if (! ok)
10855 return FALSE;
10856 }
10857
10858 /* The address of a reloc is relative to the section in a
10859 relocatable file, and is a virtual address in an executable
10860 file. */
10861 offset = link_order->offset;
10862 if (! bfd_link_relocatable (info))
10863 offset += output_section->vma;
10864
10865 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10866 {
10867 irel[i].r_offset = offset;
10868 irel[i].r_info = 0;
10869 irel[i].r_addend = 0;
10870 }
10871 if (bed->s->arch_size == 32)
10872 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10873 else
10874 #ifdef BFD64
10875 {
10876 bfd_uint64_t indx64 = indx;
10877 irel[0].r_info = ELF64_R_INFO (indx64, howto->type);
10878 }
10879 #else
10880 BFD_FAIL();
10881 #endif
10882
10883 rel_hdr = reldata->hdr;
10884 erel = rel_hdr->contents;
10885 if (rel_hdr->sh_type == SHT_REL)
10886 {
10887 erel += reldata->count * bed->s->sizeof_rel;
10888 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10889 }
10890 else
10891 {
10892 irel[0].r_addend = addend;
10893 erel += reldata->count * bed->s->sizeof_rela;
10894 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10895 }
10896
10897 ++reldata->count;
10898
10899 return TRUE;
10900 }
10901
10902
10903 /* Get the output vma of the section pointed to by the sh_link field. */
10904
10905 static bfd_vma
10906 elf_get_linked_section_vma (struct bfd_link_order *p)
10907 {
10908 Elf_Internal_Shdr **elf_shdrp;
10909 asection *s;
10910 int elfsec;
10911
10912 s = p->u.indirect.section;
10913 elf_shdrp = elf_elfsections (s->owner);
10914 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10915 elfsec = elf_shdrp[elfsec]->sh_link;
10916 /* PR 290:
10917 The Intel C compiler generates SHT_IA_64_UNWIND with
10918 SHF_LINK_ORDER. But it doesn't set the sh_link or
10919 sh_info fields. Hence we could get the situation
10920 where elfsec is 0. */
10921 if (elfsec == 0)
10922 {
10923 const struct elf_backend_data *bed
10924 = get_elf_backend_data (s->owner);
10925 if (bed->link_order_error_handler)
10926 bed->link_order_error_handler
10927 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
10928 return 0;
10929 }
10930 else
10931 {
10932 s = elf_shdrp[elfsec]->bfd_section;
10933 return s->output_section->vma + s->output_offset;
10934 }
10935 }
10936
10937
10938 /* Compare two sections based on the locations of the sections they are
10939 linked to. Used by elf_fixup_link_order. */
10940
10941 static int
10942 compare_link_order (const void * a, const void * b)
10943 {
10944 bfd_vma apos;
10945 bfd_vma bpos;
10946
10947 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10948 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10949 if (apos < bpos)
10950 return -1;
10951 return apos > bpos;
10952 }
10953
10954
10955 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10956 order as their linked sections. Returns false if this could not be done
10957 because an output section includes both ordered and unordered
10958 sections. Ideally we'd do this in the linker proper. */
10959
10960 static bfd_boolean
10961 elf_fixup_link_order (bfd *abfd, asection *o)
10962 {
10963 int seen_linkorder;
10964 int seen_other;
10965 int n;
10966 struct bfd_link_order *p;
10967 bfd *sub;
10968 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10969 unsigned elfsec;
10970 struct bfd_link_order **sections;
10971 asection *s, *other_sec, *linkorder_sec;
10972 bfd_vma offset;
10973
10974 other_sec = NULL;
10975 linkorder_sec = NULL;
10976 seen_other = 0;
10977 seen_linkorder = 0;
10978 for (p = o->map_head.link_order; p != NULL; p = p->next)
10979 {
10980 if (p->type == bfd_indirect_link_order)
10981 {
10982 s = p->u.indirect.section;
10983 sub = s->owner;
10984 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10985 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
10986 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
10987 && elfsec < elf_numsections (sub)
10988 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
10989 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
10990 {
10991 seen_linkorder++;
10992 linkorder_sec = s;
10993 }
10994 else
10995 {
10996 seen_other++;
10997 other_sec = s;
10998 }
10999 }
11000 else
11001 seen_other++;
11002
11003 if (seen_other && seen_linkorder)
11004 {
11005 if (other_sec && linkorder_sec)
11006 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
11007 o, linkorder_sec,
11008 linkorder_sec->owner, other_sec,
11009 other_sec->owner);
11010 else
11011 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
11012 o);
11013 bfd_set_error (bfd_error_bad_value);
11014 return FALSE;
11015 }
11016 }
11017
11018 if (!seen_linkorder)
11019 return TRUE;
11020
11021 sections = (struct bfd_link_order **)
11022 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11023 if (sections == NULL)
11024 return FALSE;
11025 seen_linkorder = 0;
11026
11027 for (p = o->map_head.link_order; p != NULL; p = p->next)
11028 {
11029 sections[seen_linkorder++] = p;
11030 }
11031 /* Sort the input sections in the order of their linked section. */
11032 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11033 compare_link_order);
11034
11035 /* Change the offsets of the sections. */
11036 offset = 0;
11037 for (n = 0; n < seen_linkorder; n++)
11038 {
11039 s = sections[n]->u.indirect.section;
11040 offset &= ~(bfd_vma) 0 << s->alignment_power;
11041 s->output_offset = offset / bfd_octets_per_byte (abfd);
11042 sections[n]->offset = offset;
11043 offset += sections[n]->size;
11044 }
11045
11046 free (sections);
11047 return TRUE;
11048 }
11049
11050 static void
11051 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11052 {
11053 asection *o;
11054
11055 if (flinfo->symstrtab != NULL)
11056 _bfd_elf_strtab_free (flinfo->symstrtab);
11057 if (flinfo->contents != NULL)
11058 free (flinfo->contents);
11059 if (flinfo->external_relocs != NULL)
11060 free (flinfo->external_relocs);
11061 if (flinfo->internal_relocs != NULL)
11062 free (flinfo->internal_relocs);
11063 if (flinfo->external_syms != NULL)
11064 free (flinfo->external_syms);
11065 if (flinfo->locsym_shndx != NULL)
11066 free (flinfo->locsym_shndx);
11067 if (flinfo->internal_syms != NULL)
11068 free (flinfo->internal_syms);
11069 if (flinfo->indices != NULL)
11070 free (flinfo->indices);
11071 if (flinfo->sections != NULL)
11072 free (flinfo->sections);
11073 if (flinfo->symshndxbuf != NULL)
11074 free (flinfo->symshndxbuf);
11075 for (o = obfd->sections; o != NULL; o = o->next)
11076 {
11077 struct bfd_elf_section_data *esdo = elf_section_data (o);
11078 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11079 free (esdo->rel.hashes);
11080 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11081 free (esdo->rela.hashes);
11082 }
11083 }
11084
11085 /* Do the final step of an ELF link. */
11086
11087 bfd_boolean
11088 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11089 {
11090 bfd_boolean dynamic;
11091 bfd_boolean emit_relocs;
11092 bfd *dynobj;
11093 struct elf_final_link_info flinfo;
11094 asection *o;
11095 struct bfd_link_order *p;
11096 bfd *sub;
11097 bfd_size_type max_contents_size;
11098 bfd_size_type max_external_reloc_size;
11099 bfd_size_type max_internal_reloc_count;
11100 bfd_size_type max_sym_count;
11101 bfd_size_type max_sym_shndx_count;
11102 Elf_Internal_Sym elfsym;
11103 unsigned int i;
11104 Elf_Internal_Shdr *symtab_hdr;
11105 Elf_Internal_Shdr *symtab_shndx_hdr;
11106 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11107 struct elf_outext_info eoinfo;
11108 bfd_boolean merged;
11109 size_t relativecount = 0;
11110 asection *reldyn = 0;
11111 bfd_size_type amt;
11112 asection *attr_section = NULL;
11113 bfd_vma attr_size = 0;
11114 const char *std_attrs_section;
11115
11116 if (! is_elf_hash_table (info->hash))
11117 return FALSE;
11118
11119 if (bfd_link_pic (info))
11120 abfd->flags |= DYNAMIC;
11121
11122 dynamic = elf_hash_table (info)->dynamic_sections_created;
11123 dynobj = elf_hash_table (info)->dynobj;
11124
11125 emit_relocs = (bfd_link_relocatable (info)
11126 || info->emitrelocations);
11127
11128 flinfo.info = info;
11129 flinfo.output_bfd = abfd;
11130 flinfo.symstrtab = _bfd_elf_strtab_init ();
11131 if (flinfo.symstrtab == NULL)
11132 return FALSE;
11133
11134 if (! dynamic)
11135 {
11136 flinfo.hash_sec = NULL;
11137 flinfo.symver_sec = NULL;
11138 }
11139 else
11140 {
11141 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11142 /* Note that dynsym_sec can be NULL (on VMS). */
11143 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11144 /* Note that it is OK if symver_sec is NULL. */
11145 }
11146
11147 flinfo.contents = NULL;
11148 flinfo.external_relocs = NULL;
11149 flinfo.internal_relocs = NULL;
11150 flinfo.external_syms = NULL;
11151 flinfo.locsym_shndx = NULL;
11152 flinfo.internal_syms = NULL;
11153 flinfo.indices = NULL;
11154 flinfo.sections = NULL;
11155 flinfo.symshndxbuf = NULL;
11156 flinfo.filesym_count = 0;
11157
11158 /* The object attributes have been merged. Remove the input
11159 sections from the link, and set the contents of the output
11160 secton. */
11161 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11162 for (o = abfd->sections; o != NULL; o = o->next)
11163 {
11164 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11165 || strcmp (o->name, ".gnu.attributes") == 0)
11166 {
11167 for (p = o->map_head.link_order; p != NULL; p = p->next)
11168 {
11169 asection *input_section;
11170
11171 if (p->type != bfd_indirect_link_order)
11172 continue;
11173 input_section = p->u.indirect.section;
11174 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11175 elf_link_input_bfd ignores this section. */
11176 input_section->flags &= ~SEC_HAS_CONTENTS;
11177 }
11178
11179 attr_size = bfd_elf_obj_attr_size (abfd);
11180 if (attr_size)
11181 {
11182 bfd_set_section_size (abfd, o, attr_size);
11183 attr_section = o;
11184 /* Skip this section later on. */
11185 o->map_head.link_order = NULL;
11186 }
11187 else
11188 o->flags |= SEC_EXCLUDE;
11189 }
11190 }
11191
11192 /* Count up the number of relocations we will output for each output
11193 section, so that we know the sizes of the reloc sections. We
11194 also figure out some maximum sizes. */
11195 max_contents_size = 0;
11196 max_external_reloc_size = 0;
11197 max_internal_reloc_count = 0;
11198 max_sym_count = 0;
11199 max_sym_shndx_count = 0;
11200 merged = FALSE;
11201 for (o = abfd->sections; o != NULL; o = o->next)
11202 {
11203 struct bfd_elf_section_data *esdo = elf_section_data (o);
11204 o->reloc_count = 0;
11205
11206 for (p = o->map_head.link_order; p != NULL; p = p->next)
11207 {
11208 unsigned int reloc_count = 0;
11209 unsigned int additional_reloc_count = 0;
11210 struct bfd_elf_section_data *esdi = NULL;
11211
11212 if (p->type == bfd_section_reloc_link_order
11213 || p->type == bfd_symbol_reloc_link_order)
11214 reloc_count = 1;
11215 else if (p->type == bfd_indirect_link_order)
11216 {
11217 asection *sec;
11218
11219 sec = p->u.indirect.section;
11220 esdi = elf_section_data (sec);
11221
11222 /* Mark all sections which are to be included in the
11223 link. This will normally be every section. We need
11224 to do this so that we can identify any sections which
11225 the linker has decided to not include. */
11226 sec->linker_mark = TRUE;
11227
11228 if (sec->flags & SEC_MERGE)
11229 merged = TRUE;
11230
11231 if (esdo->this_hdr.sh_type == SHT_REL
11232 || esdo->this_hdr.sh_type == SHT_RELA)
11233 /* Some backends use reloc_count in relocation sections
11234 to count particular types of relocs. Of course,
11235 reloc sections themselves can't have relocations. */
11236 reloc_count = 0;
11237 else if (emit_relocs)
11238 {
11239 reloc_count = sec->reloc_count;
11240 if (bed->elf_backend_count_additional_relocs)
11241 {
11242 int c;
11243 c = (*bed->elf_backend_count_additional_relocs) (sec);
11244 additional_reloc_count += c;
11245 }
11246 }
11247 else if (bed->elf_backend_count_relocs)
11248 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11249
11250 if (sec->rawsize > max_contents_size)
11251 max_contents_size = sec->rawsize;
11252 if (sec->size > max_contents_size)
11253 max_contents_size = sec->size;
11254
11255 /* We are interested in just local symbols, not all
11256 symbols. */
11257 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11258 && (sec->owner->flags & DYNAMIC) == 0)
11259 {
11260 size_t sym_count;
11261
11262 if (elf_bad_symtab (sec->owner))
11263 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11264 / bed->s->sizeof_sym);
11265 else
11266 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11267
11268 if (sym_count > max_sym_count)
11269 max_sym_count = sym_count;
11270
11271 if (sym_count > max_sym_shndx_count
11272 && elf_symtab_shndx_list (sec->owner) != NULL)
11273 max_sym_shndx_count = sym_count;
11274
11275 if ((sec->flags & SEC_RELOC) != 0)
11276 {
11277 size_t ext_size = 0;
11278
11279 if (esdi->rel.hdr != NULL)
11280 ext_size = esdi->rel.hdr->sh_size;
11281 if (esdi->rela.hdr != NULL)
11282 ext_size += esdi->rela.hdr->sh_size;
11283
11284 if (ext_size > max_external_reloc_size)
11285 max_external_reloc_size = ext_size;
11286 if (sec->reloc_count > max_internal_reloc_count)
11287 max_internal_reloc_count = sec->reloc_count;
11288 }
11289 }
11290 }
11291
11292 if (reloc_count == 0)
11293 continue;
11294
11295 reloc_count += additional_reloc_count;
11296 o->reloc_count += reloc_count;
11297
11298 if (p->type == bfd_indirect_link_order && emit_relocs)
11299 {
11300 if (esdi->rel.hdr)
11301 {
11302 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11303 esdo->rel.count += additional_reloc_count;
11304 }
11305 if (esdi->rela.hdr)
11306 {
11307 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11308 esdo->rela.count += additional_reloc_count;
11309 }
11310 }
11311 else
11312 {
11313 if (o->use_rela_p)
11314 esdo->rela.count += reloc_count;
11315 else
11316 esdo->rel.count += reloc_count;
11317 }
11318 }
11319
11320 if (o->reloc_count > 0)
11321 o->flags |= SEC_RELOC;
11322 else
11323 {
11324 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11325 set it (this is probably a bug) and if it is set
11326 assign_section_numbers will create a reloc section. */
11327 o->flags &=~ SEC_RELOC;
11328 }
11329
11330 /* If the SEC_ALLOC flag is not set, force the section VMA to
11331 zero. This is done in elf_fake_sections as well, but forcing
11332 the VMA to 0 here will ensure that relocs against these
11333 sections are handled correctly. */
11334 if ((o->flags & SEC_ALLOC) == 0
11335 && ! o->user_set_vma)
11336 o->vma = 0;
11337 }
11338
11339 if (! bfd_link_relocatable (info) && merged)
11340 elf_link_hash_traverse (elf_hash_table (info),
11341 _bfd_elf_link_sec_merge_syms, abfd);
11342
11343 /* Figure out the file positions for everything but the symbol table
11344 and the relocs. We set symcount to force assign_section_numbers
11345 to create a symbol table. */
11346 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
11347 BFD_ASSERT (! abfd->output_has_begun);
11348 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11349 goto error_return;
11350
11351 /* Set sizes, and assign file positions for reloc sections. */
11352 for (o = abfd->sections; o != NULL; o = o->next)
11353 {
11354 struct bfd_elf_section_data *esdo = elf_section_data (o);
11355 if ((o->flags & SEC_RELOC) != 0)
11356 {
11357 if (esdo->rel.hdr
11358 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
11359 goto error_return;
11360
11361 if (esdo->rela.hdr
11362 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
11363 goto error_return;
11364 }
11365
11366 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11367 to count upwards while actually outputting the relocations. */
11368 esdo->rel.count = 0;
11369 esdo->rela.count = 0;
11370
11371 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11372 {
11373 /* Cache the section contents so that they can be compressed
11374 later. Use bfd_malloc since it will be freed by
11375 bfd_compress_section_contents. */
11376 unsigned char *contents = esdo->this_hdr.contents;
11377 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11378 abort ();
11379 contents
11380 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11381 if (contents == NULL)
11382 goto error_return;
11383 esdo->this_hdr.contents = contents;
11384 }
11385 }
11386
11387 /* We have now assigned file positions for all the sections except
11388 .symtab, .strtab, and non-loaded reloc sections. We start the
11389 .symtab section at the current file position, and write directly
11390 to it. We build the .strtab section in memory. */
11391 bfd_get_symcount (abfd) = 0;
11392 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11393 /* sh_name is set in prep_headers. */
11394 symtab_hdr->sh_type = SHT_SYMTAB;
11395 /* sh_flags, sh_addr and sh_size all start off zero. */
11396 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11397 /* sh_link is set in assign_section_numbers. */
11398 /* sh_info is set below. */
11399 /* sh_offset is set just below. */
11400 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11401
11402 if (max_sym_count < 20)
11403 max_sym_count = 20;
11404 elf_hash_table (info)->strtabsize = max_sym_count;
11405 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11406 elf_hash_table (info)->strtab
11407 = (struct elf_sym_strtab *) bfd_malloc (amt);
11408 if (elf_hash_table (info)->strtab == NULL)
11409 goto error_return;
11410 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11411 flinfo.symshndxbuf
11412 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11413 ? (Elf_External_Sym_Shndx *) -1 : NULL);
11414
11415 if (info->strip != strip_all || emit_relocs)
11416 {
11417 file_ptr off = elf_next_file_pos (abfd);
11418
11419 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11420
11421 /* Note that at this point elf_next_file_pos (abfd) is
11422 incorrect. We do not yet know the size of the .symtab section.
11423 We correct next_file_pos below, after we do know the size. */
11424
11425 /* Start writing out the symbol table. The first symbol is always a
11426 dummy symbol. */
11427 elfsym.st_value = 0;
11428 elfsym.st_size = 0;
11429 elfsym.st_info = 0;
11430 elfsym.st_other = 0;
11431 elfsym.st_shndx = SHN_UNDEF;
11432 elfsym.st_target_internal = 0;
11433 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11434 bfd_und_section_ptr, NULL) != 1)
11435 goto error_return;
11436
11437 /* Output a symbol for each section. We output these even if we are
11438 discarding local symbols, since they are used for relocs. These
11439 symbols have no names. We store the index of each one in the
11440 index field of the section, so that we can find it again when
11441 outputting relocs. */
11442
11443 elfsym.st_size = 0;
11444 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11445 elfsym.st_other = 0;
11446 elfsym.st_value = 0;
11447 elfsym.st_target_internal = 0;
11448 for (i = 1; i < elf_numsections (abfd); i++)
11449 {
11450 o = bfd_section_from_elf_index (abfd, i);
11451 if (o != NULL)
11452 {
11453 o->target_index = bfd_get_symcount (abfd);
11454 elfsym.st_shndx = i;
11455 if (!bfd_link_relocatable (info))
11456 elfsym.st_value = o->vma;
11457 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11458 NULL) != 1)
11459 goto error_return;
11460 }
11461 }
11462 }
11463
11464 /* Allocate some memory to hold information read in from the input
11465 files. */
11466 if (max_contents_size != 0)
11467 {
11468 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11469 if (flinfo.contents == NULL)
11470 goto error_return;
11471 }
11472
11473 if (max_external_reloc_size != 0)
11474 {
11475 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11476 if (flinfo.external_relocs == NULL)
11477 goto error_return;
11478 }
11479
11480 if (max_internal_reloc_count != 0)
11481 {
11482 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11483 amt *= sizeof (Elf_Internal_Rela);
11484 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11485 if (flinfo.internal_relocs == NULL)
11486 goto error_return;
11487 }
11488
11489 if (max_sym_count != 0)
11490 {
11491 amt = max_sym_count * bed->s->sizeof_sym;
11492 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11493 if (flinfo.external_syms == NULL)
11494 goto error_return;
11495
11496 amt = max_sym_count * sizeof (Elf_Internal_Sym);
11497 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11498 if (flinfo.internal_syms == NULL)
11499 goto error_return;
11500
11501 amt = max_sym_count * sizeof (long);
11502 flinfo.indices = (long int *) bfd_malloc (amt);
11503 if (flinfo.indices == NULL)
11504 goto error_return;
11505
11506 amt = max_sym_count * sizeof (asection *);
11507 flinfo.sections = (asection **) bfd_malloc (amt);
11508 if (flinfo.sections == NULL)
11509 goto error_return;
11510 }
11511
11512 if (max_sym_shndx_count != 0)
11513 {
11514 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
11515 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11516 if (flinfo.locsym_shndx == NULL)
11517 goto error_return;
11518 }
11519
11520 if (elf_hash_table (info)->tls_sec)
11521 {
11522 bfd_vma base, end = 0;
11523 asection *sec;
11524
11525 for (sec = elf_hash_table (info)->tls_sec;
11526 sec && (sec->flags & SEC_THREAD_LOCAL);
11527 sec = sec->next)
11528 {
11529 bfd_size_type size = sec->size;
11530
11531 if (size == 0
11532 && (sec->flags & SEC_HAS_CONTENTS) == 0)
11533 {
11534 struct bfd_link_order *ord = sec->map_tail.link_order;
11535
11536 if (ord != NULL)
11537 size = ord->offset + ord->size;
11538 }
11539 end = sec->vma + size;
11540 }
11541 base = elf_hash_table (info)->tls_sec->vma;
11542 /* Only align end of TLS section if static TLS doesn't have special
11543 alignment requirements. */
11544 if (bed->static_tls_alignment == 1)
11545 end = align_power (end,
11546 elf_hash_table (info)->tls_sec->alignment_power);
11547 elf_hash_table (info)->tls_size = end - base;
11548 }
11549
11550 /* Reorder SHF_LINK_ORDER sections. */
11551 for (o = abfd->sections; o != NULL; o = o->next)
11552 {
11553 if (!elf_fixup_link_order (abfd, o))
11554 return FALSE;
11555 }
11556
11557 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11558 return FALSE;
11559
11560 /* Since ELF permits relocations to be against local symbols, we
11561 must have the local symbols available when we do the relocations.
11562 Since we would rather only read the local symbols once, and we
11563 would rather not keep them in memory, we handle all the
11564 relocations for a single input file at the same time.
11565
11566 Unfortunately, there is no way to know the total number of local
11567 symbols until we have seen all of them, and the local symbol
11568 indices precede the global symbol indices. This means that when
11569 we are generating relocatable output, and we see a reloc against
11570 a global symbol, we can not know the symbol index until we have
11571 finished examining all the local symbols to see which ones we are
11572 going to output. To deal with this, we keep the relocations in
11573 memory, and don't output them until the end of the link. This is
11574 an unfortunate waste of memory, but I don't see a good way around
11575 it. Fortunately, it only happens when performing a relocatable
11576 link, which is not the common case. FIXME: If keep_memory is set
11577 we could write the relocs out and then read them again; I don't
11578 know how bad the memory loss will be. */
11579
11580 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11581 sub->output_has_begun = FALSE;
11582 for (o = abfd->sections; o != NULL; o = o->next)
11583 {
11584 for (p = o->map_head.link_order; p != NULL; p = p->next)
11585 {
11586 if (p->type == bfd_indirect_link_order
11587 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11588 == bfd_target_elf_flavour)
11589 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11590 {
11591 if (! sub->output_has_begun)
11592 {
11593 if (! elf_link_input_bfd (&flinfo, sub))
11594 goto error_return;
11595 sub->output_has_begun = TRUE;
11596 }
11597 }
11598 else if (p->type == bfd_section_reloc_link_order
11599 || p->type == bfd_symbol_reloc_link_order)
11600 {
11601 if (! elf_reloc_link_order (abfd, info, o, p))
11602 goto error_return;
11603 }
11604 else
11605 {
11606 if (! _bfd_default_link_order (abfd, info, o, p))
11607 {
11608 if (p->type == bfd_indirect_link_order
11609 && (bfd_get_flavour (sub)
11610 == bfd_target_elf_flavour)
11611 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11612 != bed->s->elfclass))
11613 {
11614 const char *iclass, *oclass;
11615
11616 switch (bed->s->elfclass)
11617 {
11618 case ELFCLASS64: oclass = "ELFCLASS64"; break;
11619 case ELFCLASS32: oclass = "ELFCLASS32"; break;
11620 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11621 default: abort ();
11622 }
11623
11624 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
11625 {
11626 case ELFCLASS64: iclass = "ELFCLASS64"; break;
11627 case ELFCLASS32: iclass = "ELFCLASS32"; break;
11628 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11629 default: abort ();
11630 }
11631
11632 bfd_set_error (bfd_error_wrong_format);
11633 (*_bfd_error_handler)
11634 (_("%B: file class %s incompatible with %s"),
11635 sub, iclass, oclass);
11636 }
11637
11638 goto error_return;
11639 }
11640 }
11641 }
11642 }
11643
11644 /* Free symbol buffer if needed. */
11645 if (!info->reduce_memory_overheads)
11646 {
11647 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11648 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11649 && elf_tdata (sub)->symbuf)
11650 {
11651 free (elf_tdata (sub)->symbuf);
11652 elf_tdata (sub)->symbuf = NULL;
11653 }
11654 }
11655
11656 /* Output any global symbols that got converted to local in a
11657 version script or due to symbol visibility. We do this in a
11658 separate step since ELF requires all local symbols to appear
11659 prior to any global symbols. FIXME: We should only do this if
11660 some global symbols were, in fact, converted to become local.
11661 FIXME: Will this work correctly with the Irix 5 linker? */
11662 eoinfo.failed = FALSE;
11663 eoinfo.flinfo = &flinfo;
11664 eoinfo.localsyms = TRUE;
11665 eoinfo.file_sym_done = FALSE;
11666 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11667 if (eoinfo.failed)
11668 return FALSE;
11669
11670 /* If backend needs to output some local symbols not present in the hash
11671 table, do it now. */
11672 if (bed->elf_backend_output_arch_local_syms
11673 && (info->strip != strip_all || emit_relocs))
11674 {
11675 typedef int (*out_sym_func)
11676 (void *, const char *, Elf_Internal_Sym *, asection *,
11677 struct elf_link_hash_entry *);
11678
11679 if (! ((*bed->elf_backend_output_arch_local_syms)
11680 (abfd, info, &flinfo,
11681 (out_sym_func) elf_link_output_symstrtab)))
11682 return FALSE;
11683 }
11684
11685 /* That wrote out all the local symbols. Finish up the symbol table
11686 with the global symbols. Even if we want to strip everything we
11687 can, we still need to deal with those global symbols that got
11688 converted to local in a version script. */
11689
11690 /* The sh_info field records the index of the first non local symbol. */
11691 symtab_hdr->sh_info = bfd_get_symcount (abfd);
11692
11693 if (dynamic
11694 && elf_hash_table (info)->dynsym != NULL
11695 && (elf_hash_table (info)->dynsym->output_section
11696 != bfd_abs_section_ptr))
11697 {
11698 Elf_Internal_Sym sym;
11699 bfd_byte *dynsym = elf_hash_table (info)->dynsym->contents;
11700 long last_local = 0;
11701
11702 /* Write out the section symbols for the output sections. */
11703 if (bfd_link_pic (info)
11704 || elf_hash_table (info)->is_relocatable_executable)
11705 {
11706 asection *s;
11707
11708 sym.st_size = 0;
11709 sym.st_name = 0;
11710 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11711 sym.st_other = 0;
11712 sym.st_target_internal = 0;
11713
11714 for (s = abfd->sections; s != NULL; s = s->next)
11715 {
11716 int indx;
11717 bfd_byte *dest;
11718 long dynindx;
11719
11720 dynindx = elf_section_data (s)->dynindx;
11721 if (dynindx <= 0)
11722 continue;
11723 indx = elf_section_data (s)->this_idx;
11724 BFD_ASSERT (indx > 0);
11725 sym.st_shndx = indx;
11726 if (! check_dynsym (abfd, &sym))
11727 return FALSE;
11728 sym.st_value = s->vma;
11729 dest = dynsym + dynindx * bed->s->sizeof_sym;
11730 if (last_local < dynindx)
11731 last_local = dynindx;
11732 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11733 }
11734 }
11735
11736 /* Write out the local dynsyms. */
11737 if (elf_hash_table (info)->dynlocal)
11738 {
11739 struct elf_link_local_dynamic_entry *e;
11740 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
11741 {
11742 asection *s;
11743 bfd_byte *dest;
11744
11745 /* Copy the internal symbol and turn off visibility.
11746 Note that we saved a word of storage and overwrote
11747 the original st_name with the dynstr_index. */
11748 sym = e->isym;
11749 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
11750
11751 s = bfd_section_from_elf_index (e->input_bfd,
11752 e->isym.st_shndx);
11753 if (s != NULL)
11754 {
11755 sym.st_shndx =
11756 elf_section_data (s->output_section)->this_idx;
11757 if (! check_dynsym (abfd, &sym))
11758 return FALSE;
11759 sym.st_value = (s->output_section->vma
11760 + s->output_offset
11761 + e->isym.st_value);
11762 }
11763
11764 if (last_local < e->dynindx)
11765 last_local = e->dynindx;
11766
11767 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
11768 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11769 }
11770 }
11771
11772 elf_section_data (elf_hash_table (info)->dynsym->output_section)->this_hdr.sh_info =
11773 last_local + 1;
11774 }
11775
11776 /* We get the global symbols from the hash table. */
11777 eoinfo.failed = FALSE;
11778 eoinfo.localsyms = FALSE;
11779 eoinfo.flinfo = &flinfo;
11780 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11781 if (eoinfo.failed)
11782 return FALSE;
11783
11784 /* If backend needs to output some symbols not present in the hash
11785 table, do it now. */
11786 if (bed->elf_backend_output_arch_syms
11787 && (info->strip != strip_all || emit_relocs))
11788 {
11789 typedef int (*out_sym_func)
11790 (void *, const char *, Elf_Internal_Sym *, asection *,
11791 struct elf_link_hash_entry *);
11792
11793 if (! ((*bed->elf_backend_output_arch_syms)
11794 (abfd, info, &flinfo,
11795 (out_sym_func) elf_link_output_symstrtab)))
11796 return FALSE;
11797 }
11798
11799 /* Finalize the .strtab section. */
11800 _bfd_elf_strtab_finalize (flinfo.symstrtab);
11801
11802 /* Swap out the .strtab section. */
11803 if (!elf_link_swap_symbols_out (&flinfo))
11804 return FALSE;
11805
11806 /* Now we know the size of the symtab section. */
11807 if (bfd_get_symcount (abfd) > 0)
11808 {
11809 /* Finish up and write out the symbol string table (.strtab)
11810 section. */
11811 Elf_Internal_Shdr *symstrtab_hdr;
11812 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
11813
11814 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
11815 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
11816 {
11817 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
11818 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
11819 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
11820 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
11821 symtab_shndx_hdr->sh_size = amt;
11822
11823 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
11824 off, TRUE);
11825
11826 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
11827 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
11828 return FALSE;
11829 }
11830
11831 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
11832 /* sh_name was set in prep_headers. */
11833 symstrtab_hdr->sh_type = SHT_STRTAB;
11834 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
11835 symstrtab_hdr->sh_addr = 0;
11836 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
11837 symstrtab_hdr->sh_entsize = 0;
11838 symstrtab_hdr->sh_link = 0;
11839 symstrtab_hdr->sh_info = 0;
11840 /* sh_offset is set just below. */
11841 symstrtab_hdr->sh_addralign = 1;
11842
11843 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
11844 off, TRUE);
11845 elf_next_file_pos (abfd) = off;
11846
11847 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
11848 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
11849 return FALSE;
11850 }
11851
11852 /* Adjust the relocs to have the correct symbol indices. */
11853 for (o = abfd->sections; o != NULL; o = o->next)
11854 {
11855 struct bfd_elf_section_data *esdo = elf_section_data (o);
11856 bfd_boolean sort;
11857 if ((o->flags & SEC_RELOC) == 0)
11858 continue;
11859
11860 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
11861 if (esdo->rel.hdr != NULL
11862 && !elf_link_adjust_relocs (abfd, &esdo->rel, sort))
11863 return FALSE;
11864 if (esdo->rela.hdr != NULL
11865 && !elf_link_adjust_relocs (abfd, &esdo->rela, sort))
11866 return FALSE;
11867
11868 /* Set the reloc_count field to 0 to prevent write_relocs from
11869 trying to swap the relocs out itself. */
11870 o->reloc_count = 0;
11871 }
11872
11873 if (dynamic && info->combreloc && dynobj != NULL)
11874 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
11875
11876 /* If we are linking against a dynamic object, or generating a
11877 shared library, finish up the dynamic linking information. */
11878 if (dynamic)
11879 {
11880 bfd_byte *dyncon, *dynconend;
11881
11882 /* Fix up .dynamic entries. */
11883 o = bfd_get_linker_section (dynobj, ".dynamic");
11884 BFD_ASSERT (o != NULL);
11885
11886 dyncon = o->contents;
11887 dynconend = o->contents + o->size;
11888 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11889 {
11890 Elf_Internal_Dyn dyn;
11891 const char *name;
11892 unsigned int type;
11893
11894 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11895
11896 switch (dyn.d_tag)
11897 {
11898 default:
11899 continue;
11900 case DT_NULL:
11901 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
11902 {
11903 switch (elf_section_data (reldyn)->this_hdr.sh_type)
11904 {
11905 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
11906 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
11907 default: continue;
11908 }
11909 dyn.d_un.d_val = relativecount;
11910 relativecount = 0;
11911 break;
11912 }
11913 continue;
11914
11915 case DT_INIT:
11916 name = info->init_function;
11917 goto get_sym;
11918 case DT_FINI:
11919 name = info->fini_function;
11920 get_sym:
11921 {
11922 struct elf_link_hash_entry *h;
11923
11924 h = elf_link_hash_lookup (elf_hash_table (info), name,
11925 FALSE, FALSE, TRUE);
11926 if (h != NULL
11927 && (h->root.type == bfd_link_hash_defined
11928 || h->root.type == bfd_link_hash_defweak))
11929 {
11930 dyn.d_un.d_ptr = h->root.u.def.value;
11931 o = h->root.u.def.section;
11932 if (o->output_section != NULL)
11933 dyn.d_un.d_ptr += (o->output_section->vma
11934 + o->output_offset);
11935 else
11936 {
11937 /* The symbol is imported from another shared
11938 library and does not apply to this one. */
11939 dyn.d_un.d_ptr = 0;
11940 }
11941 break;
11942 }
11943 }
11944 continue;
11945
11946 case DT_PREINIT_ARRAYSZ:
11947 name = ".preinit_array";
11948 goto get_out_size;
11949 case DT_INIT_ARRAYSZ:
11950 name = ".init_array";
11951 goto get_out_size;
11952 case DT_FINI_ARRAYSZ:
11953 name = ".fini_array";
11954 get_out_size:
11955 o = bfd_get_section_by_name (abfd, name);
11956 if (o == NULL)
11957 {
11958 (*_bfd_error_handler)
11959 (_("could not find section %s"), name);
11960 goto error_return;
11961 }
11962 if (o->size == 0)
11963 (*_bfd_error_handler)
11964 (_("warning: %s section has zero size"), name);
11965 dyn.d_un.d_val = o->size;
11966 break;
11967
11968 case DT_PREINIT_ARRAY:
11969 name = ".preinit_array";
11970 goto get_out_vma;
11971 case DT_INIT_ARRAY:
11972 name = ".init_array";
11973 goto get_out_vma;
11974 case DT_FINI_ARRAY:
11975 name = ".fini_array";
11976 get_out_vma:
11977 o = bfd_get_section_by_name (abfd, name);
11978 goto do_vma;
11979
11980 case DT_HASH:
11981 name = ".hash";
11982 goto get_vma;
11983 case DT_GNU_HASH:
11984 name = ".gnu.hash";
11985 goto get_vma;
11986 case DT_STRTAB:
11987 name = ".dynstr";
11988 goto get_vma;
11989 case DT_SYMTAB:
11990 name = ".dynsym";
11991 goto get_vma;
11992 case DT_VERDEF:
11993 name = ".gnu.version_d";
11994 goto get_vma;
11995 case DT_VERNEED:
11996 name = ".gnu.version_r";
11997 goto get_vma;
11998 case DT_VERSYM:
11999 name = ".gnu.version";
12000 get_vma:
12001 o = bfd_get_linker_section (dynobj, name);
12002 do_vma:
12003 if (o == NULL)
12004 {
12005 (*_bfd_error_handler)
12006 (_("could not find section %s"), name);
12007 goto error_return;
12008 }
12009 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12010 {
12011 (*_bfd_error_handler)
12012 (_("warning: section '%s' is being made into a note"), name);
12013 bfd_set_error (bfd_error_nonrepresentable_section);
12014 goto error_return;
12015 }
12016 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
12017 break;
12018
12019 case DT_REL:
12020 case DT_RELA:
12021 case DT_RELSZ:
12022 case DT_RELASZ:
12023 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12024 type = SHT_REL;
12025 else
12026 type = SHT_RELA;
12027 dyn.d_un.d_val = 0;
12028 dyn.d_un.d_ptr = 0;
12029 for (i = 1; i < elf_numsections (abfd); i++)
12030 {
12031 Elf_Internal_Shdr *hdr;
12032
12033 hdr = elf_elfsections (abfd)[i];
12034 if (hdr->sh_type == type
12035 && (hdr->sh_flags & SHF_ALLOC) != 0)
12036 {
12037 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12038 dyn.d_un.d_val += hdr->sh_size;
12039 else
12040 {
12041 if (dyn.d_un.d_ptr == 0
12042 || hdr->sh_addr < dyn.d_un.d_ptr)
12043 dyn.d_un.d_ptr = hdr->sh_addr;
12044 }
12045 }
12046 }
12047 break;
12048 }
12049 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12050 }
12051 }
12052
12053 /* If we have created any dynamic sections, then output them. */
12054 if (dynobj != NULL)
12055 {
12056 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12057 goto error_return;
12058
12059 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12060 if (((info->warn_shared_textrel && bfd_link_pic (info))
12061 || info->error_textrel)
12062 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
12063 {
12064 bfd_byte *dyncon, *dynconend;
12065
12066 dyncon = o->contents;
12067 dynconend = o->contents + o->size;
12068 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12069 {
12070 Elf_Internal_Dyn dyn;
12071
12072 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12073
12074 if (dyn.d_tag == DT_TEXTREL)
12075 {
12076 if (info->error_textrel)
12077 info->callbacks->einfo
12078 (_("%P%X: read-only segment has dynamic relocations.\n"));
12079 else
12080 info->callbacks->einfo
12081 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
12082 break;
12083 }
12084 }
12085 }
12086
12087 for (o = dynobj->sections; o != NULL; o = o->next)
12088 {
12089 if ((o->flags & SEC_HAS_CONTENTS) == 0
12090 || o->size == 0
12091 || o->output_section == bfd_abs_section_ptr)
12092 continue;
12093 if ((o->flags & SEC_LINKER_CREATED) == 0)
12094 {
12095 /* At this point, we are only interested in sections
12096 created by _bfd_elf_link_create_dynamic_sections. */
12097 continue;
12098 }
12099 if (elf_hash_table (info)->stab_info.stabstr == o)
12100 continue;
12101 if (elf_hash_table (info)->eh_info.hdr_sec == o)
12102 continue;
12103 if (strcmp (o->name, ".dynstr") != 0)
12104 {
12105 if (! bfd_set_section_contents (abfd, o->output_section,
12106 o->contents,
12107 (file_ptr) o->output_offset
12108 * bfd_octets_per_byte (abfd),
12109 o->size))
12110 goto error_return;
12111 }
12112 else
12113 {
12114 /* The contents of the .dynstr section are actually in a
12115 stringtab. */
12116 file_ptr off;
12117
12118 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12119 if (bfd_seek (abfd, off, SEEK_SET) != 0
12120 || ! _bfd_elf_strtab_emit (abfd,
12121 elf_hash_table (info)->dynstr))
12122 goto error_return;
12123 }
12124 }
12125 }
12126
12127 if (bfd_link_relocatable (info))
12128 {
12129 bfd_boolean failed = FALSE;
12130
12131 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12132 if (failed)
12133 goto error_return;
12134 }
12135
12136 /* If we have optimized stabs strings, output them. */
12137 if (elf_hash_table (info)->stab_info.stabstr != NULL)
12138 {
12139 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
12140 goto error_return;
12141 }
12142
12143 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12144 goto error_return;
12145
12146 elf_final_link_free (abfd, &flinfo);
12147
12148 elf_linker (abfd) = TRUE;
12149
12150 if (attr_section)
12151 {
12152 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
12153 if (contents == NULL)
12154 return FALSE; /* Bail out and fail. */
12155 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12156 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12157 free (contents);
12158 }
12159
12160 return TRUE;
12161
12162 error_return:
12163 elf_final_link_free (abfd, &flinfo);
12164 return FALSE;
12165 }
12166
12167 /* Initialize COOKIE for input bfd ABFD. */
12169
12170 static bfd_boolean
12171 init_reloc_cookie (struct elf_reloc_cookie *cookie,
12172 struct bfd_link_info *info, bfd *abfd)
12173 {
12174 Elf_Internal_Shdr *symtab_hdr;
12175 const struct elf_backend_data *bed;
12176
12177 bed = get_elf_backend_data (abfd);
12178 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12179
12180 cookie->abfd = abfd;
12181 cookie->sym_hashes = elf_sym_hashes (abfd);
12182 cookie->bad_symtab = elf_bad_symtab (abfd);
12183 if (cookie->bad_symtab)
12184 {
12185 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12186 cookie->extsymoff = 0;
12187 }
12188 else
12189 {
12190 cookie->locsymcount = symtab_hdr->sh_info;
12191 cookie->extsymoff = symtab_hdr->sh_info;
12192 }
12193
12194 if (bed->s->arch_size == 32)
12195 cookie->r_sym_shift = 8;
12196 else
12197 cookie->r_sym_shift = 32;
12198
12199 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12200 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12201 {
12202 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12203 cookie->locsymcount, 0,
12204 NULL, NULL, NULL);
12205 if (cookie->locsyms == NULL)
12206 {
12207 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12208 return FALSE;
12209 }
12210 if (info->keep_memory)
12211 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12212 }
12213 return TRUE;
12214 }
12215
12216 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12217
12218 static void
12219 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12220 {
12221 Elf_Internal_Shdr *symtab_hdr;
12222
12223 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12224 if (cookie->locsyms != NULL
12225 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12226 free (cookie->locsyms);
12227 }
12228
12229 /* Initialize the relocation information in COOKIE for input section SEC
12230 of input bfd ABFD. */
12231
12232 static bfd_boolean
12233 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12234 struct bfd_link_info *info, bfd *abfd,
12235 asection *sec)
12236 {
12237 const struct elf_backend_data *bed;
12238
12239 if (sec->reloc_count == 0)
12240 {
12241 cookie->rels = NULL;
12242 cookie->relend = NULL;
12243 }
12244 else
12245 {
12246 bed = get_elf_backend_data (abfd);
12247
12248 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12249 info->keep_memory);
12250 if (cookie->rels == NULL)
12251 return FALSE;
12252 cookie->rel = cookie->rels;
12253 cookie->relend = (cookie->rels
12254 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
12255 }
12256 cookie->rel = cookie->rels;
12257 return TRUE;
12258 }
12259
12260 /* Free the memory allocated by init_reloc_cookie_rels,
12261 if appropriate. */
12262
12263 static void
12264 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12265 asection *sec)
12266 {
12267 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12268 free (cookie->rels);
12269 }
12270
12271 /* Initialize the whole of COOKIE for input section SEC. */
12272
12273 static bfd_boolean
12274 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12275 struct bfd_link_info *info,
12276 asection *sec)
12277 {
12278 if (!init_reloc_cookie (cookie, info, sec->owner))
12279 goto error1;
12280 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12281 goto error2;
12282 return TRUE;
12283
12284 error2:
12285 fini_reloc_cookie (cookie, sec->owner);
12286 error1:
12287 return FALSE;
12288 }
12289
12290 /* Free the memory allocated by init_reloc_cookie_for_section,
12291 if appropriate. */
12292
12293 static void
12294 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12295 asection *sec)
12296 {
12297 fini_reloc_cookie_rels (cookie, sec);
12298 fini_reloc_cookie (cookie, sec->owner);
12299 }
12300
12301 /* Garbage collect unused sections. */
12303
12304 /* Default gc_mark_hook. */
12305
12306 asection *
12307 _bfd_elf_gc_mark_hook (asection *sec,
12308 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12309 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12310 struct elf_link_hash_entry *h,
12311 Elf_Internal_Sym *sym)
12312 {
12313 if (h != NULL)
12314 {
12315 switch (h->root.type)
12316 {
12317 case bfd_link_hash_defined:
12318 case bfd_link_hash_defweak:
12319 return h->root.u.def.section;
12320
12321 case bfd_link_hash_common:
12322 return h->root.u.c.p->section;
12323
12324 default:
12325 break;
12326 }
12327 }
12328 else
12329 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12330
12331 return NULL;
12332 }
12333
12334 /* For undefined __start_<name> and __stop_<name> symbols, return the
12335 first input section matching <name>. Return NULL otherwise. */
12336
12337 asection *
12338 _bfd_elf_is_start_stop (const struct bfd_link_info *info,
12339 struct elf_link_hash_entry *h)
12340 {
12341 asection *s;
12342 const char *sec_name;
12343
12344 if (h->root.type != bfd_link_hash_undefined
12345 && h->root.type != bfd_link_hash_undefweak)
12346 return NULL;
12347
12348 s = h->root.u.undef.section;
12349 if (s != NULL)
12350 {
12351 if (s == (asection *) 0 - 1)
12352 return NULL;
12353 return s;
12354 }
12355
12356 sec_name = NULL;
12357 if (strncmp (h->root.root.string, "__start_", 8) == 0)
12358 sec_name = h->root.root.string + 8;
12359 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
12360 sec_name = h->root.root.string + 7;
12361
12362 if (sec_name != NULL && *sec_name != '\0')
12363 {
12364 bfd *i;
12365
12366 for (i = info->input_bfds; i != NULL; i = i->link.next)
12367 {
12368 s = bfd_get_section_by_name (i, sec_name);
12369 if (s != NULL)
12370 {
12371 h->root.u.undef.section = s;
12372 break;
12373 }
12374 }
12375 }
12376
12377 if (s == NULL)
12378 h->root.u.undef.section = (asection *) 0 - 1;
12379
12380 return s;
12381 }
12382
12383 /* COOKIE->rel describes a relocation against section SEC, which is
12384 a section we've decided to keep. Return the section that contains
12385 the relocation symbol, or NULL if no section contains it. */
12386
12387 asection *
12388 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12389 elf_gc_mark_hook_fn gc_mark_hook,
12390 struct elf_reloc_cookie *cookie,
12391 bfd_boolean *start_stop)
12392 {
12393 unsigned long r_symndx;
12394 struct elf_link_hash_entry *h;
12395
12396 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
12397 if (r_symndx == STN_UNDEF)
12398 return NULL;
12399
12400 if (r_symndx >= cookie->locsymcount
12401 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12402 {
12403 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
12404 if (h == NULL)
12405 {
12406 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12407 sec->owner);
12408 return NULL;
12409 }
12410 while (h->root.type == bfd_link_hash_indirect
12411 || h->root.type == bfd_link_hash_warning)
12412 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12413 h->mark = 1;
12414 /* If this symbol is weak and there is a non-weak definition, we
12415 keep the non-weak definition because many backends put
12416 dynamic reloc info on the non-weak definition for code
12417 handling copy relocs. */
12418 if (h->u.weakdef != NULL)
12419 h->u.weakdef->mark = 1;
12420
12421 if (start_stop != NULL)
12422 {
12423 /* To work around a glibc bug, mark all XXX input sections
12424 when there is an as yet undefined reference to __start_XXX
12425 or __stop_XXX symbols. The linker will later define such
12426 symbols for orphan input sections that have a name
12427 representable as a C identifier. */
12428 asection *s = _bfd_elf_is_start_stop (info, h);
12429
12430 if (s != NULL)
12431 {
12432 *start_stop = !s->gc_mark;
12433 return s;
12434 }
12435 }
12436
12437 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12438 }
12439
12440 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12441 &cookie->locsyms[r_symndx]);
12442 }
12443
12444 /* COOKIE->rel describes a relocation against section SEC, which is
12445 a section we've decided to keep. Mark the section that contains
12446 the relocation symbol. */
12447
12448 bfd_boolean
12449 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12450 asection *sec,
12451 elf_gc_mark_hook_fn gc_mark_hook,
12452 struct elf_reloc_cookie *cookie)
12453 {
12454 asection *rsec;
12455 bfd_boolean start_stop = FALSE;
12456
12457 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12458 while (rsec != NULL)
12459 {
12460 if (!rsec->gc_mark)
12461 {
12462 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12463 || (rsec->owner->flags & DYNAMIC) != 0)
12464 rsec->gc_mark = 1;
12465 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12466 return FALSE;
12467 }
12468 if (!start_stop)
12469 break;
12470 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
12471 }
12472 return TRUE;
12473 }
12474
12475 /* The mark phase of garbage collection. For a given section, mark
12476 it and any sections in this section's group, and all the sections
12477 which define symbols to which it refers. */
12478
12479 bfd_boolean
12480 _bfd_elf_gc_mark (struct bfd_link_info *info,
12481 asection *sec,
12482 elf_gc_mark_hook_fn gc_mark_hook)
12483 {
12484 bfd_boolean ret;
12485 asection *group_sec, *eh_frame;
12486
12487 sec->gc_mark = 1;
12488
12489 /* Mark all the sections in the group. */
12490 group_sec = elf_section_data (sec)->next_in_group;
12491 if (group_sec && !group_sec->gc_mark)
12492 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
12493 return FALSE;
12494
12495 /* Look through the section relocs. */
12496 ret = TRUE;
12497 eh_frame = elf_eh_frame_section (sec->owner);
12498 if ((sec->flags & SEC_RELOC) != 0
12499 && sec->reloc_count > 0
12500 && sec != eh_frame)
12501 {
12502 struct elf_reloc_cookie cookie;
12503
12504 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12505 ret = FALSE;
12506 else
12507 {
12508 for (; cookie.rel < cookie.relend; cookie.rel++)
12509 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
12510 {
12511 ret = FALSE;
12512 break;
12513 }
12514 fini_reloc_cookie_for_section (&cookie, sec);
12515 }
12516 }
12517
12518 if (ret && eh_frame && elf_fde_list (sec))
12519 {
12520 struct elf_reloc_cookie cookie;
12521
12522 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12523 ret = FALSE;
12524 else
12525 {
12526 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12527 gc_mark_hook, &cookie))
12528 ret = FALSE;
12529 fini_reloc_cookie_for_section (&cookie, eh_frame);
12530 }
12531 }
12532
12533 eh_frame = elf_section_eh_frame_entry (sec);
12534 if (ret && eh_frame && !eh_frame->gc_mark)
12535 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12536 ret = FALSE;
12537
12538 return ret;
12539 }
12540
12541 /* Scan and mark sections in a special or debug section group. */
12542
12543 static void
12544 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12545 {
12546 /* Point to first section of section group. */
12547 asection *ssec;
12548 /* Used to iterate the section group. */
12549 asection *msec;
12550
12551 bfd_boolean is_special_grp = TRUE;
12552 bfd_boolean is_debug_grp = TRUE;
12553
12554 /* First scan to see if group contains any section other than debug
12555 and special section. */
12556 ssec = msec = elf_next_in_group (grp);
12557 do
12558 {
12559 if ((msec->flags & SEC_DEBUGGING) == 0)
12560 is_debug_grp = FALSE;
12561
12562 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12563 is_special_grp = FALSE;
12564
12565 msec = elf_next_in_group (msec);
12566 }
12567 while (msec != ssec);
12568
12569 /* If this is a pure debug section group or pure special section group,
12570 keep all sections in this group. */
12571 if (is_debug_grp || is_special_grp)
12572 {
12573 do
12574 {
12575 msec->gc_mark = 1;
12576 msec = elf_next_in_group (msec);
12577 }
12578 while (msec != ssec);
12579 }
12580 }
12581
12582 /* Keep debug and special sections. */
12583
12584 bfd_boolean
12585 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12586 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12587 {
12588 bfd *ibfd;
12589
12590 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12591 {
12592 asection *isec;
12593 bfd_boolean some_kept;
12594 bfd_boolean debug_frag_seen;
12595
12596 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12597 continue;
12598
12599 /* Ensure all linker created sections are kept,
12600 see if any other section is already marked,
12601 and note if we have any fragmented debug sections. */
12602 debug_frag_seen = some_kept = FALSE;
12603 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12604 {
12605 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12606 isec->gc_mark = 1;
12607 else if (isec->gc_mark)
12608 some_kept = TRUE;
12609
12610 if (debug_frag_seen == FALSE
12611 && (isec->flags & SEC_DEBUGGING)
12612 && CONST_STRNEQ (isec->name, ".debug_line."))
12613 debug_frag_seen = TRUE;
12614 }
12615
12616 /* If no section in this file will be kept, then we can
12617 toss out the debug and special sections. */
12618 if (!some_kept)
12619 continue;
12620
12621 /* Keep debug and special sections like .comment when they are
12622 not part of a group. Also keep section groups that contain
12623 just debug sections or special sections. */
12624 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12625 {
12626 if ((isec->flags & SEC_GROUP) != 0)
12627 _bfd_elf_gc_mark_debug_special_section_group (isec);
12628 else if (((isec->flags & SEC_DEBUGGING) != 0
12629 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12630 && elf_next_in_group (isec) == NULL)
12631 isec->gc_mark = 1;
12632 }
12633
12634 if (! debug_frag_seen)
12635 continue;
12636
12637 /* Look for CODE sections which are going to be discarded,
12638 and find and discard any fragmented debug sections which
12639 are associated with that code section. */
12640 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12641 if ((isec->flags & SEC_CODE) != 0
12642 && isec->gc_mark == 0)
12643 {
12644 unsigned int ilen;
12645 asection *dsec;
12646
12647 ilen = strlen (isec->name);
12648
12649 /* Association is determined by the name of the debug section
12650 containing the name of the code section as a suffix. For
12651 example .debug_line.text.foo is a debug section associated
12652 with .text.foo. */
12653 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12654 {
12655 unsigned int dlen;
12656
12657 if (dsec->gc_mark == 0
12658 || (dsec->flags & SEC_DEBUGGING) == 0)
12659 continue;
12660
12661 dlen = strlen (dsec->name);
12662
12663 if (dlen > ilen
12664 && strncmp (dsec->name + (dlen - ilen),
12665 isec->name, ilen) == 0)
12666 {
12667 dsec->gc_mark = 0;
12668 }
12669 }
12670 }
12671 }
12672 return TRUE;
12673 }
12674
12675 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12676
12677 struct elf_gc_sweep_symbol_info
12678 {
12679 struct bfd_link_info *info;
12680 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
12681 bfd_boolean);
12682 };
12683
12684 static bfd_boolean
12685 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
12686 {
12687 if (!h->mark
12688 && (((h->root.type == bfd_link_hash_defined
12689 || h->root.type == bfd_link_hash_defweak)
12690 && !((h->def_regular || ELF_COMMON_DEF_P (h))
12691 && h->root.u.def.section->gc_mark))
12692 || h->root.type == bfd_link_hash_undefined
12693 || h->root.type == bfd_link_hash_undefweak))
12694 {
12695 struct elf_gc_sweep_symbol_info *inf;
12696
12697 inf = (struct elf_gc_sweep_symbol_info *) data;
12698 (*inf->hide_symbol) (inf->info, h, TRUE);
12699 h->def_regular = 0;
12700 h->ref_regular = 0;
12701 h->ref_regular_nonweak = 0;
12702 }
12703
12704 return TRUE;
12705 }
12706
12707 /* The sweep phase of garbage collection. Remove all garbage sections. */
12708
12709 typedef bfd_boolean (*gc_sweep_hook_fn)
12710 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
12711
12712 static bfd_boolean
12713 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
12714 {
12715 bfd *sub;
12716 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12717 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
12718 unsigned long section_sym_count;
12719 struct elf_gc_sweep_symbol_info sweep_info;
12720
12721 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12722 {
12723 asection *o;
12724
12725 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12726 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
12727 continue;
12728
12729 for (o = sub->sections; o != NULL; o = o->next)
12730 {
12731 /* When any section in a section group is kept, we keep all
12732 sections in the section group. If the first member of
12733 the section group is excluded, we will also exclude the
12734 group section. */
12735 if (o->flags & SEC_GROUP)
12736 {
12737 asection *first = elf_next_in_group (o);
12738 o->gc_mark = first->gc_mark;
12739 }
12740
12741 if (o->gc_mark)
12742 continue;
12743
12744 /* Skip sweeping sections already excluded. */
12745 if (o->flags & SEC_EXCLUDE)
12746 continue;
12747
12748 /* Since this is early in the link process, it is simple
12749 to remove a section from the output. */
12750 o->flags |= SEC_EXCLUDE;
12751
12752 if (info->print_gc_sections && o->size != 0)
12753 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
12754
12755 /* But we also have to update some of the relocation
12756 info we collected before. */
12757 if (gc_sweep_hook
12758 && (o->flags & SEC_RELOC) != 0
12759 && o->reloc_count != 0
12760 && !((info->strip == strip_all || info->strip == strip_debugger)
12761 && (o->flags & SEC_DEBUGGING) != 0)
12762 && !bfd_is_abs_section (o->output_section))
12763 {
12764 Elf_Internal_Rela *internal_relocs;
12765 bfd_boolean r;
12766
12767 internal_relocs
12768 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
12769 info->keep_memory);
12770 if (internal_relocs == NULL)
12771 return FALSE;
12772
12773 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
12774
12775 if (elf_section_data (o)->relocs != internal_relocs)
12776 free (internal_relocs);
12777
12778 if (!r)
12779 return FALSE;
12780 }
12781 }
12782 }
12783
12784 /* Remove the symbols that were in the swept sections from the dynamic
12785 symbol table. GCFIXME: Anyone know how to get them out of the
12786 static symbol table as well? */
12787 sweep_info.info = info;
12788 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
12789 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
12790 &sweep_info);
12791
12792 _bfd_elf_link_renumber_dynsyms (abfd, info, §ion_sym_count);
12793 return TRUE;
12794 }
12795
12796 /* Propagate collected vtable information. This is called through
12797 elf_link_hash_traverse. */
12798
12799 static bfd_boolean
12800 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
12801 {
12802 /* Those that are not vtables. */
12803 if (h->vtable == NULL || h->vtable->parent == NULL)
12804 return TRUE;
12805
12806 /* Those vtables that do not have parents, we cannot merge. */
12807 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
12808 return TRUE;
12809
12810 /* If we've already been done, exit. */
12811 if (h->vtable->used && h->vtable->used[-1])
12812 return TRUE;
12813
12814 /* Make sure the parent's table is up to date. */
12815 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
12816
12817 if (h->vtable->used == NULL)
12818 {
12819 /* None of this table's entries were referenced. Re-use the
12820 parent's table. */
12821 h->vtable->used = h->vtable->parent->vtable->used;
12822 h->vtable->size = h->vtable->parent->vtable->size;
12823 }
12824 else
12825 {
12826 size_t n;
12827 bfd_boolean *cu, *pu;
12828
12829 /* Or the parent's entries into ours. */
12830 cu = h->vtable->used;
12831 cu[-1] = TRUE;
12832 pu = h->vtable->parent->vtable->used;
12833 if (pu != NULL)
12834 {
12835 const struct elf_backend_data *bed;
12836 unsigned int log_file_align;
12837
12838 bed = get_elf_backend_data (h->root.u.def.section->owner);
12839 log_file_align = bed->s->log_file_align;
12840 n = h->vtable->parent->vtable->size >> log_file_align;
12841 while (n--)
12842 {
12843 if (*pu)
12844 *cu = TRUE;
12845 pu++;
12846 cu++;
12847 }
12848 }
12849 }
12850
12851 return TRUE;
12852 }
12853
12854 static bfd_boolean
12855 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
12856 {
12857 asection *sec;
12858 bfd_vma hstart, hend;
12859 Elf_Internal_Rela *relstart, *relend, *rel;
12860 const struct elf_backend_data *bed;
12861 unsigned int log_file_align;
12862
12863 /* Take care of both those symbols that do not describe vtables as
12864 well as those that are not loaded. */
12865 if (h->vtable == NULL || h->vtable->parent == NULL)
12866 return TRUE;
12867
12868 BFD_ASSERT (h->root.type == bfd_link_hash_defined
12869 || h->root.type == bfd_link_hash_defweak);
12870
12871 sec = h->root.u.def.section;
12872 hstart = h->root.u.def.value;
12873 hend = hstart + h->size;
12874
12875 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
12876 if (!relstart)
12877 return *(bfd_boolean *) okp = FALSE;
12878 bed = get_elf_backend_data (sec->owner);
12879 log_file_align = bed->s->log_file_align;
12880
12881 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
12882
12883 for (rel = relstart; rel < relend; ++rel)
12884 if (rel->r_offset >= hstart && rel->r_offset < hend)
12885 {
12886 /* If the entry is in use, do nothing. */
12887 if (h->vtable->used
12888 && (rel->r_offset - hstart) < h->vtable->size)
12889 {
12890 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
12891 if (h->vtable->used[entry])
12892 continue;
12893 }
12894 /* Otherwise, kill it. */
12895 rel->r_offset = rel->r_info = rel->r_addend = 0;
12896 }
12897
12898 return TRUE;
12899 }
12900
12901 /* Mark sections containing dynamically referenced symbols. When
12902 building shared libraries, we must assume that any visible symbol is
12903 referenced. */
12904
12905 bfd_boolean
12906 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
12907 {
12908 struct bfd_link_info *info = (struct bfd_link_info *) inf;
12909 struct bfd_elf_dynamic_list *d = info->dynamic_list;
12910
12911 if ((h->root.type == bfd_link_hash_defined
12912 || h->root.type == bfd_link_hash_defweak)
12913 && (h->ref_dynamic
12914 || ((h->def_regular || ELF_COMMON_DEF_P (h))
12915 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
12916 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
12917 && (!bfd_link_executable (info)
12918 || info->export_dynamic
12919 || (h->dynamic
12920 && d != NULL
12921 && (*d->match) (&d->head, NULL, h->root.root.string)))
12922 && (h->versioned >= versioned
12923 || !bfd_hide_sym_by_version (info->version_info,
12924 h->root.root.string)))))
12925 h->root.u.def.section->flags |= SEC_KEEP;
12926
12927 return TRUE;
12928 }
12929
12930 /* Keep all sections containing symbols undefined on the command-line,
12931 and the section containing the entry symbol. */
12932
12933 void
12934 _bfd_elf_gc_keep (struct bfd_link_info *info)
12935 {
12936 struct bfd_sym_chain *sym;
12937
12938 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
12939 {
12940 struct elf_link_hash_entry *h;
12941
12942 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
12943 FALSE, FALSE, FALSE);
12944
12945 if (h != NULL
12946 && (h->root.type == bfd_link_hash_defined
12947 || h->root.type == bfd_link_hash_defweak)
12948 && !bfd_is_abs_section (h->root.u.def.section))
12949 h->root.u.def.section->flags |= SEC_KEEP;
12950 }
12951 }
12952
12953 bfd_boolean
12954 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
12955 struct bfd_link_info *info)
12956 {
12957 bfd *ibfd = info->input_bfds;
12958
12959 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12960 {
12961 asection *sec;
12962 struct elf_reloc_cookie cookie;
12963
12964 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12965 continue;
12966
12967 if (!init_reloc_cookie (&cookie, info, ibfd))
12968 return FALSE;
12969
12970 for (sec = ibfd->sections; sec; sec = sec->next)
12971 {
12972 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
12973 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
12974 {
12975 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
12976 fini_reloc_cookie_rels (&cookie, sec);
12977 }
12978 }
12979 }
12980 return TRUE;
12981 }
12982
12983 /* Do mark and sweep of unused sections. */
12984
12985 bfd_boolean
12986 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
12987 {
12988 bfd_boolean ok = TRUE;
12989 bfd *sub;
12990 elf_gc_mark_hook_fn gc_mark_hook;
12991 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12992 struct elf_link_hash_table *htab;
12993
12994 if (!bed->can_gc_sections
12995 || !is_elf_hash_table (info->hash))
12996 {
12997 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
12998 return TRUE;
12999 }
13000
13001 bed->gc_keep (info);
13002 htab = elf_hash_table (info);
13003
13004 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13005 at the .eh_frame section if we can mark the FDEs individually. */
13006 for (sub = info->input_bfds;
13007 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13008 sub = sub->link.next)
13009 {
13010 asection *sec;
13011 struct elf_reloc_cookie cookie;
13012
13013 sec = bfd_get_section_by_name (sub, ".eh_frame");
13014 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
13015 {
13016 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
13017 if (elf_section_data (sec)->sec_info
13018 && (sec->flags & SEC_LINKER_CREATED) == 0)
13019 elf_eh_frame_section (sub) = sec;
13020 fini_reloc_cookie_for_section (&cookie, sec);
13021 sec = bfd_get_next_section_by_name (NULL, sec);
13022 }
13023 }
13024
13025 /* Apply transitive closure to the vtable entry usage info. */
13026 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13027 if (!ok)
13028 return FALSE;
13029
13030 /* Kill the vtable relocations that were not used. */
13031 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13032 if (!ok)
13033 return FALSE;
13034
13035 /* Mark dynamically referenced symbols. */
13036 if (htab->dynamic_sections_created)
13037 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13038
13039 /* Grovel through relocs to find out who stays ... */
13040 gc_mark_hook = bed->gc_mark_hook;
13041 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13042 {
13043 asection *o;
13044
13045 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13046 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13047 continue;
13048
13049 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13050 Also treat note sections as a root, if the section is not part
13051 of a group. */
13052 for (o = sub->sections; o != NULL; o = o->next)
13053 if (!o->gc_mark
13054 && (o->flags & SEC_EXCLUDE) == 0
13055 && ((o->flags & SEC_KEEP) != 0
13056 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13057 && elf_next_in_group (o) == NULL )))
13058 {
13059 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13060 return FALSE;
13061 }
13062 }
13063
13064 /* Allow the backend to mark additional target specific sections. */
13065 bed->gc_mark_extra_sections (info, gc_mark_hook);
13066
13067 /* ... and mark SEC_EXCLUDE for those that go. */
13068 return elf_gc_sweep (abfd, info);
13069 }
13070
13071 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13073
13074 bfd_boolean
13075 bfd_elf_gc_record_vtinherit (bfd *abfd,
13076 asection *sec,
13077 struct elf_link_hash_entry *h,
13078 bfd_vma offset)
13079 {
13080 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13081 struct elf_link_hash_entry **search, *child;
13082 size_t extsymcount;
13083 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13084
13085 /* The sh_info field of the symtab header tells us where the
13086 external symbols start. We don't care about the local symbols at
13087 this point. */
13088 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13089 if (!elf_bad_symtab (abfd))
13090 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13091
13092 sym_hashes = elf_sym_hashes (abfd);
13093 sym_hashes_end = sym_hashes + extsymcount;
13094
13095 /* Hunt down the child symbol, which is in this section at the same
13096 offset as the relocation. */
13097 for (search = sym_hashes; search != sym_hashes_end; ++search)
13098 {
13099 if ((child = *search) != NULL
13100 && (child->root.type == bfd_link_hash_defined
13101 || child->root.type == bfd_link_hash_defweak)
13102 && child->root.u.def.section == sec
13103 && child->root.u.def.value == offset)
13104 goto win;
13105 }
13106
13107 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
13108 abfd, sec, (unsigned long) offset);
13109 bfd_set_error (bfd_error_invalid_operation);
13110 return FALSE;
13111
13112 win:
13113 if (!child->vtable)
13114 {
13115 child->vtable = ((struct elf_link_virtual_table_entry *)
13116 bfd_zalloc (abfd, sizeof (*child->vtable)));
13117 if (!child->vtable)
13118 return FALSE;
13119 }
13120 if (!h)
13121 {
13122 /* This *should* only be the absolute section. It could potentially
13123 be that someone has defined a non-global vtable though, which
13124 would be bad. It isn't worth paging in the local symbols to be
13125 sure though; that case should simply be handled by the assembler. */
13126
13127 child->vtable->parent = (struct elf_link_hash_entry *) -1;
13128 }
13129 else
13130 child->vtable->parent = h;
13131
13132 return TRUE;
13133 }
13134
13135 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13136
13137 bfd_boolean
13138 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13139 asection *sec ATTRIBUTE_UNUSED,
13140 struct elf_link_hash_entry *h,
13141 bfd_vma addend)
13142 {
13143 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13144 unsigned int log_file_align = bed->s->log_file_align;
13145
13146 if (!h->vtable)
13147 {
13148 h->vtable = ((struct elf_link_virtual_table_entry *)
13149 bfd_zalloc (abfd, sizeof (*h->vtable)));
13150 if (!h->vtable)
13151 return FALSE;
13152 }
13153
13154 if (addend >= h->vtable->size)
13155 {
13156 size_t size, bytes, file_align;
13157 bfd_boolean *ptr = h->vtable->used;
13158
13159 /* While the symbol is undefined, we have to be prepared to handle
13160 a zero size. */
13161 file_align = 1 << log_file_align;
13162 if (h->root.type == bfd_link_hash_undefined)
13163 size = addend + file_align;
13164 else
13165 {
13166 size = h->size;
13167 if (addend >= size)
13168 {
13169 /* Oops! We've got a reference past the defined end of
13170 the table. This is probably a bug -- shall we warn? */
13171 size = addend + file_align;
13172 }
13173 }
13174 size = (size + file_align - 1) & -file_align;
13175
13176 /* Allocate one extra entry for use as a "done" flag for the
13177 consolidation pass. */
13178 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13179
13180 if (ptr)
13181 {
13182 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
13183
13184 if (ptr != NULL)
13185 {
13186 size_t oldbytes;
13187
13188 oldbytes = (((h->vtable->size >> log_file_align) + 1)
13189 * sizeof (bfd_boolean));
13190 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13191 }
13192 }
13193 else
13194 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13195
13196 if (ptr == NULL)
13197 return FALSE;
13198
13199 /* And arrange for that done flag to be at index -1. */
13200 h->vtable->used = ptr + 1;
13201 h->vtable->size = size;
13202 }
13203
13204 h->vtable->used[addend >> log_file_align] = TRUE;
13205
13206 return TRUE;
13207 }
13208
13209 /* Map an ELF section header flag to its corresponding string. */
13210 typedef struct
13211 {
13212 char *flag_name;
13213 flagword flag_value;
13214 } elf_flags_to_name_table;
13215
13216 static elf_flags_to_name_table elf_flags_to_names [] =
13217 {
13218 { "SHF_WRITE", SHF_WRITE },
13219 { "SHF_ALLOC", SHF_ALLOC },
13220 { "SHF_EXECINSTR", SHF_EXECINSTR },
13221 { "SHF_MERGE", SHF_MERGE },
13222 { "SHF_STRINGS", SHF_STRINGS },
13223 { "SHF_INFO_LINK", SHF_INFO_LINK},
13224 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13225 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13226 { "SHF_GROUP", SHF_GROUP },
13227 { "SHF_TLS", SHF_TLS },
13228 { "SHF_MASKOS", SHF_MASKOS },
13229 { "SHF_EXCLUDE", SHF_EXCLUDE },
13230 };
13231
13232 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13233 bfd_boolean
13234 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13235 struct flag_info *flaginfo,
13236 asection *section)
13237 {
13238 const bfd_vma sh_flags = elf_section_flags (section);
13239
13240 if (!flaginfo->flags_initialized)
13241 {
13242 bfd *obfd = info->output_bfd;
13243 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13244 struct flag_info_list *tf = flaginfo->flag_list;
13245 int with_hex = 0;
13246 int without_hex = 0;
13247
13248 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13249 {
13250 unsigned i;
13251 flagword (*lookup) (char *);
13252
13253 lookup = bed->elf_backend_lookup_section_flags_hook;
13254 if (lookup != NULL)
13255 {
13256 flagword hexval = (*lookup) ((char *) tf->name);
13257
13258 if (hexval != 0)
13259 {
13260 if (tf->with == with_flags)
13261 with_hex |= hexval;
13262 else if (tf->with == without_flags)
13263 without_hex |= hexval;
13264 tf->valid = TRUE;
13265 continue;
13266 }
13267 }
13268 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13269 {
13270 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13271 {
13272 if (tf->with == with_flags)
13273 with_hex |= elf_flags_to_names[i].flag_value;
13274 else if (tf->with == without_flags)
13275 without_hex |= elf_flags_to_names[i].flag_value;
13276 tf->valid = TRUE;
13277 break;
13278 }
13279 }
13280 if (!tf->valid)
13281 {
13282 info->callbacks->einfo
13283 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13284 return FALSE;
13285 }
13286 }
13287 flaginfo->flags_initialized = TRUE;
13288 flaginfo->only_with_flags |= with_hex;
13289 flaginfo->not_with_flags |= without_hex;
13290 }
13291
13292 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13293 return FALSE;
13294
13295 if ((flaginfo->not_with_flags & sh_flags) != 0)
13296 return FALSE;
13297
13298 return TRUE;
13299 }
13300
13301 struct alloc_got_off_arg {
13302 bfd_vma gotoff;
13303 struct bfd_link_info *info;
13304 };
13305
13306 /* We need a special top-level link routine to convert got reference counts
13307 to real got offsets. */
13308
13309 static bfd_boolean
13310 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13311 {
13312 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13313 bfd *obfd = gofarg->info->output_bfd;
13314 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13315
13316 if (h->got.refcount > 0)
13317 {
13318 h->got.offset = gofarg->gotoff;
13319 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13320 }
13321 else
13322 h->got.offset = (bfd_vma) -1;
13323
13324 return TRUE;
13325 }
13326
13327 /* And an accompanying bit to work out final got entry offsets once
13328 we're done. Should be called from final_link. */
13329
13330 bfd_boolean
13331 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13332 struct bfd_link_info *info)
13333 {
13334 bfd *i;
13335 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13336 bfd_vma gotoff;
13337 struct alloc_got_off_arg gofarg;
13338
13339 BFD_ASSERT (abfd == info->output_bfd);
13340
13341 if (! is_elf_hash_table (info->hash))
13342 return FALSE;
13343
13344 /* The GOT offset is relative to the .got section, but the GOT header is
13345 put into the .got.plt section, if the backend uses it. */
13346 if (bed->want_got_plt)
13347 gotoff = 0;
13348 else
13349 gotoff = bed->got_header_size;
13350
13351 /* Do the local .got entries first. */
13352 for (i = info->input_bfds; i; i = i->link.next)
13353 {
13354 bfd_signed_vma *local_got;
13355 size_t j, locsymcount;
13356 Elf_Internal_Shdr *symtab_hdr;
13357
13358 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13359 continue;
13360
13361 local_got = elf_local_got_refcounts (i);
13362 if (!local_got)
13363 continue;
13364
13365 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13366 if (elf_bad_symtab (i))
13367 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13368 else
13369 locsymcount = symtab_hdr->sh_info;
13370
13371 for (j = 0; j < locsymcount; ++j)
13372 {
13373 if (local_got[j] > 0)
13374 {
13375 local_got[j] = gotoff;
13376 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13377 }
13378 else
13379 local_got[j] = (bfd_vma) -1;
13380 }
13381 }
13382
13383 /* Then the global .got entries. .plt refcounts are handled by
13384 adjust_dynamic_symbol */
13385 gofarg.gotoff = gotoff;
13386 gofarg.info = info;
13387 elf_link_hash_traverse (elf_hash_table (info),
13388 elf_gc_allocate_got_offsets,
13389 &gofarg);
13390 return TRUE;
13391 }
13392
13393 /* Many folk need no more in the way of final link than this, once
13394 got entry reference counting is enabled. */
13395
13396 bfd_boolean
13397 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13398 {
13399 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13400 return FALSE;
13401
13402 /* Invoke the regular ELF backend linker to do all the work. */
13403 return bfd_elf_final_link (abfd, info);
13404 }
13405
13406 bfd_boolean
13407 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13408 {
13409 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13410
13411 if (rcookie->bad_symtab)
13412 rcookie->rel = rcookie->rels;
13413
13414 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13415 {
13416 unsigned long r_symndx;
13417
13418 if (! rcookie->bad_symtab)
13419 if (rcookie->rel->r_offset > offset)
13420 return FALSE;
13421 if (rcookie->rel->r_offset != offset)
13422 continue;
13423
13424 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
13425 if (r_symndx == STN_UNDEF)
13426 return TRUE;
13427
13428 if (r_symndx >= rcookie->locsymcount
13429 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13430 {
13431 struct elf_link_hash_entry *h;
13432
13433 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13434
13435 while (h->root.type == bfd_link_hash_indirect
13436 || h->root.type == bfd_link_hash_warning)
13437 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13438
13439 if ((h->root.type == bfd_link_hash_defined
13440 || h->root.type == bfd_link_hash_defweak)
13441 && (h->root.u.def.section->owner != rcookie->abfd
13442 || h->root.u.def.section->kept_section != NULL
13443 || discarded_section (h->root.u.def.section)))
13444 return TRUE;
13445 }
13446 else
13447 {
13448 /* It's not a relocation against a global symbol,
13449 but it could be a relocation against a local
13450 symbol for a discarded section. */
13451 asection *isec;
13452 Elf_Internal_Sym *isym;
13453
13454 /* Need to: get the symbol; get the section. */
13455 isym = &rcookie->locsyms[r_symndx];
13456 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
13457 if (isec != NULL
13458 && (isec->kept_section != NULL
13459 || discarded_section (isec)))
13460 return TRUE;
13461 }
13462 return FALSE;
13463 }
13464 return FALSE;
13465 }
13466
13467 /* Discard unneeded references to discarded sections.
13468 Returns -1 on error, 1 if any section's size was changed, 0 if
13469 nothing changed. This function assumes that the relocations are in
13470 sorted order, which is true for all known assemblers. */
13471
13472 int
13473 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13474 {
13475 struct elf_reloc_cookie cookie;
13476 asection *o;
13477 bfd *abfd;
13478 int changed = 0;
13479
13480 if (info->traditional_format
13481 || !is_elf_hash_table (info->hash))
13482 return 0;
13483
13484 o = bfd_get_section_by_name (output_bfd, ".stab");
13485 if (o != NULL)
13486 {
13487 asection *i;
13488
13489 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13490 {
13491 if (i->size == 0
13492 || i->reloc_count == 0
13493 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13494 continue;
13495
13496 abfd = i->owner;
13497 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13498 continue;
13499
13500 if (!init_reloc_cookie_for_section (&cookie, info, i))
13501 return -1;
13502
13503 if (_bfd_discard_section_stabs (abfd, i,
13504 elf_section_data (i)->sec_info,
13505 bfd_elf_reloc_symbol_deleted_p,
13506 &cookie))
13507 changed = 1;
13508
13509 fini_reloc_cookie_for_section (&cookie, i);
13510 }
13511 }
13512
13513 o = NULL;
13514 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13515 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
13516 if (o != NULL)
13517 {
13518 asection *i;
13519
13520 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13521 {
13522 if (i->size == 0)
13523 continue;
13524
13525 abfd = i->owner;
13526 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13527 continue;
13528
13529 if (!init_reloc_cookie_for_section (&cookie, info, i))
13530 return -1;
13531
13532 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13533 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
13534 bfd_elf_reloc_symbol_deleted_p,
13535 &cookie))
13536 changed = 1;
13537
13538 fini_reloc_cookie_for_section (&cookie, i);
13539 }
13540 }
13541
13542 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13543 {
13544 const struct elf_backend_data *bed;
13545
13546 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13547 continue;
13548
13549 bed = get_elf_backend_data (abfd);
13550
13551 if (bed->elf_backend_discard_info != NULL)
13552 {
13553 if (!init_reloc_cookie (&cookie, info, abfd))
13554 return -1;
13555
13556 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
13557 changed = 1;
13558
13559 fini_reloc_cookie (&cookie, abfd);
13560 }
13561 }
13562
13563 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13564 _bfd_elf_end_eh_frame_parsing (info);
13565
13566 if (info->eh_frame_hdr_type
13567 && !bfd_link_relocatable (info)
13568 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
13569 changed = 1;
13570
13571 return changed;
13572 }
13573
13574 bfd_boolean
13575 _bfd_elf_section_already_linked (bfd *abfd,
13576 asection *sec,
13577 struct bfd_link_info *info)
13578 {
13579 flagword flags;
13580 const char *name, *key;
13581 struct bfd_section_already_linked *l;
13582 struct bfd_section_already_linked_hash_entry *already_linked_list;
13583
13584 if (sec->output_section == bfd_abs_section_ptr)
13585 return FALSE;
13586
13587 flags = sec->flags;
13588
13589 /* Return if it isn't a linkonce section. A comdat group section
13590 also has SEC_LINK_ONCE set. */
13591 if ((flags & SEC_LINK_ONCE) == 0)
13592 return FALSE;
13593
13594 /* Don't put group member sections on our list of already linked
13595 sections. They are handled as a group via their group section. */
13596 if (elf_sec_group (sec) != NULL)
13597 return FALSE;
13598
13599 /* For a SHT_GROUP section, use the group signature as the key. */
13600 name = sec->name;
13601 if ((flags & SEC_GROUP) != 0
13602 && elf_next_in_group (sec) != NULL
13603 && elf_group_name (elf_next_in_group (sec)) != NULL)
13604 key = elf_group_name (elf_next_in_group (sec));
13605 else
13606 {
13607 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
13608 if (CONST_STRNEQ (name, ".gnu.linkonce.")
13609 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13610 key++;
13611 else
13612 /* Must be a user linkonce section that doesn't follow gcc's
13613 naming convention. In this case we won't be matching
13614 single member groups. */
13615 key = name;
13616 }
13617
13618 already_linked_list = bfd_section_already_linked_table_lookup (key);
13619
13620 for (l = already_linked_list->entry; l != NULL; l = l->next)
13621 {
13622 /* We may have 2 different types of sections on the list: group
13623 sections with a signature of <key> (<key> is some string),
13624 and linkonce sections named .gnu.linkonce.<type>.<key>.
13625 Match like sections. LTO plugin sections are an exception.
13626 They are always named .gnu.linkonce.t.<key> and match either
13627 type of section. */
13628 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13629 && ((flags & SEC_GROUP) != 0
13630 || strcmp (name, l->sec->name) == 0))
13631 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
13632 {
13633 /* The section has already been linked. See if we should
13634 issue a warning. */
13635 if (!_bfd_handle_already_linked (sec, l, info))
13636 return FALSE;
13637
13638 if (flags & SEC_GROUP)
13639 {
13640 asection *first = elf_next_in_group (sec);
13641 asection *s = first;
13642
13643 while (s != NULL)
13644 {
13645 s->output_section = bfd_abs_section_ptr;
13646 /* Record which group discards it. */
13647 s->kept_section = l->sec;
13648 s = elf_next_in_group (s);
13649 /* These lists are circular. */
13650 if (s == first)
13651 break;
13652 }
13653 }
13654
13655 return TRUE;
13656 }
13657 }
13658
13659 /* A single member comdat group section may be discarded by a
13660 linkonce section and vice versa. */
13661 if ((flags & SEC_GROUP) != 0)
13662 {
13663 asection *first = elf_next_in_group (sec);
13664
13665 if (first != NULL && elf_next_in_group (first) == first)
13666 /* Check this single member group against linkonce sections. */
13667 for (l = already_linked_list->entry; l != NULL; l = l->next)
13668 if ((l->sec->flags & SEC_GROUP) == 0
13669 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13670 {
13671 first->output_section = bfd_abs_section_ptr;
13672 first->kept_section = l->sec;
13673 sec->output_section = bfd_abs_section_ptr;
13674 break;
13675 }
13676 }
13677 else
13678 /* Check this linkonce section against single member groups. */
13679 for (l = already_linked_list->entry; l != NULL; l = l->next)
13680 if (l->sec->flags & SEC_GROUP)
13681 {
13682 asection *first = elf_next_in_group (l->sec);
13683
13684 if (first != NULL
13685 && elf_next_in_group (first) == first
13686 && bfd_elf_match_symbols_in_sections (first, sec, info))
13687 {
13688 sec->output_section = bfd_abs_section_ptr;
13689 sec->kept_section = first;
13690 break;
13691 }
13692 }
13693
13694 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13695 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13696 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13697 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13698 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13699 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13700 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13701 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13702 The reverse order cannot happen as there is never a bfd with only the
13703 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13704 matter as here were are looking only for cross-bfd sections. */
13705
13706 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13707 for (l = already_linked_list->entry; l != NULL; l = l->next)
13708 if ((l->sec->flags & SEC_GROUP) == 0
13709 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13710 {
13711 if (abfd != l->sec->owner)
13712 sec->output_section = bfd_abs_section_ptr;
13713 break;
13714 }
13715
13716 /* This is the first section with this name. Record it. */
13717 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
13718 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
13719 return sec->output_section == bfd_abs_section_ptr;
13720 }
13721
13722 bfd_boolean
13723 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
13724 {
13725 return sym->st_shndx == SHN_COMMON;
13726 }
13727
13728 unsigned int
13729 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13730 {
13731 return SHN_COMMON;
13732 }
13733
13734 asection *
13735 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13736 {
13737 return bfd_com_section_ptr;
13738 }
13739
13740 bfd_vma
13741 _bfd_elf_default_got_elt_size (bfd *abfd,
13742 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13743 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13744 bfd *ibfd ATTRIBUTE_UNUSED,
13745 unsigned long symndx ATTRIBUTE_UNUSED)
13746 {
13747 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13748 return bed->s->arch_size / 8;
13749 }
13750
13751 /* Routines to support the creation of dynamic relocs. */
13752
13753 /* Returns the name of the dynamic reloc section associated with SEC. */
13754
13755 static const char *
13756 get_dynamic_reloc_section_name (bfd * abfd,
13757 asection * sec,
13758 bfd_boolean is_rela)
13759 {
13760 char *name;
13761 const char *old_name = bfd_get_section_name (NULL, sec);
13762 const char *prefix = is_rela ? ".rela" : ".rel";
13763
13764 if (old_name == NULL)
13765 return NULL;
13766
13767 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
13768 sprintf (name, "%s%s", prefix, old_name);
13769
13770 return name;
13771 }
13772
13773 /* Returns the dynamic reloc section associated with SEC.
13774 If necessary compute the name of the dynamic reloc section based
13775 on SEC's name (looked up in ABFD's string table) and the setting
13776 of IS_RELA. */
13777
13778 asection *
13779 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
13780 asection * sec,
13781 bfd_boolean is_rela)
13782 {
13783 asection * reloc_sec = elf_section_data (sec)->sreloc;
13784
13785 if (reloc_sec == NULL)
13786 {
13787 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13788
13789 if (name != NULL)
13790 {
13791 reloc_sec = bfd_get_linker_section (abfd, name);
13792
13793 if (reloc_sec != NULL)
13794 elf_section_data (sec)->sreloc = reloc_sec;
13795 }
13796 }
13797
13798 return reloc_sec;
13799 }
13800
13801 /* Returns the dynamic reloc section associated with SEC. If the
13802 section does not exist it is created and attached to the DYNOBJ
13803 bfd and stored in the SRELOC field of SEC's elf_section_data
13804 structure.
13805
13806 ALIGNMENT is the alignment for the newly created section and
13807 IS_RELA defines whether the name should be .rela.<SEC's name>
13808 or .rel.<SEC's name>. The section name is looked up in the
13809 string table associated with ABFD. */
13810
13811 asection *
13812 _bfd_elf_make_dynamic_reloc_section (asection *sec,
13813 bfd *dynobj,
13814 unsigned int alignment,
13815 bfd *abfd,
13816 bfd_boolean is_rela)
13817 {
13818 asection * reloc_sec = elf_section_data (sec)->sreloc;
13819
13820 if (reloc_sec == NULL)
13821 {
13822 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13823
13824 if (name == NULL)
13825 return NULL;
13826
13827 reloc_sec = bfd_get_linker_section (dynobj, name);
13828
13829 if (reloc_sec == NULL)
13830 {
13831 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
13832 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
13833 if ((sec->flags & SEC_ALLOC) != 0)
13834 flags |= SEC_ALLOC | SEC_LOAD;
13835
13836 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
13837 if (reloc_sec != NULL)
13838 {
13839 /* _bfd_elf_get_sec_type_attr chooses a section type by
13840 name. Override as it may be wrong, eg. for a user
13841 section named "auto" we'll get ".relauto" which is
13842 seen to be a .rela section. */
13843 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
13844 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
13845 reloc_sec = NULL;
13846 }
13847 }
13848
13849 elf_section_data (sec)->sreloc = reloc_sec;
13850 }
13851
13852 return reloc_sec;
13853 }
13854
13855 /* Copy the ELF symbol type and other attributes for a linker script
13856 assignment from HSRC to HDEST. Generally this should be treated as
13857 if we found a strong non-dynamic definition for HDEST (except that
13858 ld ignores multiple definition errors). */
13859 void
13860 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
13861 struct bfd_link_hash_entry *hdest,
13862 struct bfd_link_hash_entry *hsrc)
13863 {
13864 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
13865 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
13866 Elf_Internal_Sym isym;
13867
13868 ehdest->type = ehsrc->type;
13869 ehdest->target_internal = ehsrc->target_internal;
13870
13871 isym.st_other = ehsrc->other;
13872 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
13873 }
13874
13875 /* Append a RELA relocation REL to section S in BFD. */
13876
13877 void
13878 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13879 {
13880 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13881 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
13882 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
13883 bed->s->swap_reloca_out (abfd, rel, loc);
13884 }
13885
13886 /* Append a REL relocation REL to section S in BFD. */
13887
13888 void
13889 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13890 {
13891 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13892 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
13893 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
13894 bed->s->swap_reloc_out (abfd, rel, loc);
13895 }
13896