ad8493b3a90317b5f3c7b54a6a2778d257da607a
[binutils-gdb.git] / bfd / elflink.c
1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2016 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
20
21 #include "sysdep.h"
22 #include "bfd.h"
23 #include "bfd_stdint.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26 #define ARCH_SIZE 0
27 #include "elf-bfd.h"
28 #include "safe-ctype.h"
29 #include "libiberty.h"
30 #include "objalloc.h"
31 #if BFD_SUPPORTS_PLUGINS
32 #include "plugin.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 \f
202 /* Create a strtab to hold the dynamic symbol names. */
203 static bfd_boolean
204 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
205 {
206 struct elf_link_hash_table *hash_table;
207
208 hash_table = elf_hash_table (info);
209 if (hash_table->dynobj == NULL)
210 {
211 /* We may not set dynobj, an input file holding linker created
212 dynamic sections to abfd, which may be a dynamic object with
213 its own dynamic sections. We need to find a normal input file
214 to hold linker created sections if possible. */
215 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
216 {
217 bfd *ibfd;
218 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
219 if ((ibfd->flags
220 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0)
221 {
222 abfd = ibfd;
223 break;
224 }
225 }
226 hash_table->dynobj = abfd;
227 }
228
229 if (hash_table->dynstr == NULL)
230 {
231 hash_table->dynstr = _bfd_elf_strtab_init ();
232 if (hash_table->dynstr == NULL)
233 return FALSE;
234 }
235 return TRUE;
236 }
237
238 /* Create some sections which will be filled in with dynamic linking
239 information. ABFD is an input file which requires dynamic sections
240 to be created. The dynamic sections take up virtual memory space
241 when the final executable is run, so we need to create them before
242 addresses are assigned to the output sections. We work out the
243 actual contents and size of these sections later. */
244
245 bfd_boolean
246 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
247 {
248 flagword flags;
249 asection *s;
250 const struct elf_backend_data *bed;
251 struct elf_link_hash_entry *h;
252
253 if (! is_elf_hash_table (info->hash))
254 return FALSE;
255
256 if (elf_hash_table (info)->dynamic_sections_created)
257 return TRUE;
258
259 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
260 return FALSE;
261
262 abfd = elf_hash_table (info)->dynobj;
263 bed = get_elf_backend_data (abfd);
264
265 flags = bed->dynamic_sec_flags;
266
267 /* A dynamically linked executable has a .interp section, but a
268 shared library does not. */
269 if (bfd_link_executable (info) && !info->nointerp)
270 {
271 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
272 flags | SEC_READONLY);
273 if (s == NULL)
274 return FALSE;
275 }
276
277 /* Create sections to hold version informations. These are removed
278 if they are not needed. */
279 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
280 flags | SEC_READONLY);
281 if (s == NULL
282 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
283 return FALSE;
284
285 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
286 flags | SEC_READONLY);
287 if (s == NULL
288 || ! bfd_set_section_alignment (abfd, s, 1))
289 return FALSE;
290
291 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
292 flags | SEC_READONLY);
293 if (s == NULL
294 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
295 return FALSE;
296
297 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
298 flags | SEC_READONLY);
299 if (s == NULL
300 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
301 return FALSE;
302 elf_hash_table (info)->dynsym = s;
303
304 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
305 flags | SEC_READONLY);
306 if (s == NULL)
307 return FALSE;
308
309 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
310 if (s == NULL
311 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
312 return FALSE;
313
314 /* The special symbol _DYNAMIC is always set to the start of the
315 .dynamic section. We could set _DYNAMIC in a linker script, but we
316 only want to define it if we are, in fact, creating a .dynamic
317 section. We don't want to define it if there is no .dynamic
318 section, since on some ELF platforms the start up code examines it
319 to decide how to initialize the process. */
320 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
321 elf_hash_table (info)->hdynamic = h;
322 if (h == NULL)
323 return FALSE;
324
325 if (info->emit_hash)
326 {
327 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
328 flags | SEC_READONLY);
329 if (s == NULL
330 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
331 return FALSE;
332 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
333 }
334
335 if (info->emit_gnu_hash)
336 {
337 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
338 flags | SEC_READONLY);
339 if (s == NULL
340 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
341 return FALSE;
342 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
343 4 32-bit words followed by variable count of 64-bit words, then
344 variable count of 32-bit words. */
345 if (bed->s->arch_size == 64)
346 elf_section_data (s)->this_hdr.sh_entsize = 0;
347 else
348 elf_section_data (s)->this_hdr.sh_entsize = 4;
349 }
350
351 /* Let the backend create the rest of the sections. This lets the
352 backend set the right flags. The backend will normally create
353 the .got and .plt sections. */
354 if (bed->elf_backend_create_dynamic_sections == NULL
355 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
356 return FALSE;
357
358 elf_hash_table (info)->dynamic_sections_created = TRUE;
359
360 return TRUE;
361 }
362
363 /* Create dynamic sections when linking against a dynamic object. */
364
365 bfd_boolean
366 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
367 {
368 flagword flags, pltflags;
369 struct elf_link_hash_entry *h;
370 asection *s;
371 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
372 struct elf_link_hash_table *htab = elf_hash_table (info);
373
374 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
375 .rel[a].bss sections. */
376 flags = bed->dynamic_sec_flags;
377
378 pltflags = flags;
379 if (bed->plt_not_loaded)
380 /* We do not clear SEC_ALLOC here because we still want the OS to
381 allocate space for the section; it's just that there's nothing
382 to read in from the object file. */
383 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
384 else
385 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
386 if (bed->plt_readonly)
387 pltflags |= SEC_READONLY;
388
389 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
390 if (s == NULL
391 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
392 return FALSE;
393 htab->splt = s;
394
395 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
396 .plt section. */
397 if (bed->want_plt_sym)
398 {
399 h = _bfd_elf_define_linkage_sym (abfd, info, s,
400 "_PROCEDURE_LINKAGE_TABLE_");
401 elf_hash_table (info)->hplt = h;
402 if (h == NULL)
403 return FALSE;
404 }
405
406 s = bfd_make_section_anyway_with_flags (abfd,
407 (bed->rela_plts_and_copies_p
408 ? ".rela.plt" : ".rel.plt"),
409 flags | SEC_READONLY);
410 if (s == NULL
411 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
412 return FALSE;
413 htab->srelplt = s;
414
415 if (! _bfd_elf_create_got_section (abfd, info))
416 return FALSE;
417
418 if (bed->want_dynbss)
419 {
420 /* The .dynbss section is a place to put symbols which are defined
421 by dynamic objects, are referenced by regular objects, and are
422 not functions. We must allocate space for them in the process
423 image and use a R_*_COPY reloc to tell the dynamic linker to
424 initialize them at run time. The linker script puts the .dynbss
425 section into the .bss section of the final image. */
426 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
427 (SEC_ALLOC | SEC_LINKER_CREATED));
428 if (s == NULL)
429 return FALSE;
430
431 /* The .rel[a].bss section holds copy relocs. This section is not
432 normally needed. We need to create it here, though, so that the
433 linker will map it to an output section. We can't just create it
434 only if we need it, because we will not know whether we need it
435 until we have seen all the input files, and the first time the
436 main linker code calls BFD after examining all the input files
437 (size_dynamic_sections) the input sections have already been
438 mapped to the output sections. If the section turns out not to
439 be needed, we can discard it later. We will never need this
440 section when generating a shared object, since they do not use
441 copy relocs. */
442 if (! bfd_link_pic (info))
443 {
444 s = bfd_make_section_anyway_with_flags (abfd,
445 (bed->rela_plts_and_copies_p
446 ? ".rela.bss" : ".rel.bss"),
447 flags | SEC_READONLY);
448 if (s == NULL
449 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
450 return FALSE;
451 }
452 }
453
454 return TRUE;
455 }
456 \f
457 /* Record a new dynamic symbol. We record the dynamic symbols as we
458 read the input files, since we need to have a list of all of them
459 before we can determine the final sizes of the output sections.
460 Note that we may actually call this function even though we are not
461 going to output any dynamic symbols; in some cases we know that a
462 symbol should be in the dynamic symbol table, but only if there is
463 one. */
464
465 bfd_boolean
466 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
467 struct elf_link_hash_entry *h)
468 {
469 if (h->dynindx == -1)
470 {
471 struct elf_strtab_hash *dynstr;
472 char *p;
473 const char *name;
474 size_t indx;
475
476 /* XXX: The ABI draft says the linker must turn hidden and
477 internal symbols into STB_LOCAL symbols when producing the
478 DSO. However, if ld.so honors st_other in the dynamic table,
479 this would not be necessary. */
480 switch (ELF_ST_VISIBILITY (h->other))
481 {
482 case STV_INTERNAL:
483 case STV_HIDDEN:
484 if (h->root.type != bfd_link_hash_undefined
485 && h->root.type != bfd_link_hash_undefweak)
486 {
487 h->forced_local = 1;
488 if (!elf_hash_table (info)->is_relocatable_executable)
489 return TRUE;
490 }
491
492 default:
493 break;
494 }
495
496 h->dynindx = elf_hash_table (info)->dynsymcount;
497 ++elf_hash_table (info)->dynsymcount;
498
499 dynstr = elf_hash_table (info)->dynstr;
500 if (dynstr == NULL)
501 {
502 /* Create a strtab to hold the dynamic symbol names. */
503 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
504 if (dynstr == NULL)
505 return FALSE;
506 }
507
508 /* We don't put any version information in the dynamic string
509 table. */
510 name = h->root.root.string;
511 p = strchr (name, ELF_VER_CHR);
512 if (p != NULL)
513 /* We know that the p points into writable memory. In fact,
514 there are only a few symbols that have read-only names, being
515 those like _GLOBAL_OFFSET_TABLE_ that are created specially
516 by the backends. Most symbols will have names pointing into
517 an ELF string table read from a file, or to objalloc memory. */
518 *p = 0;
519
520 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
521
522 if (p != NULL)
523 *p = ELF_VER_CHR;
524
525 if (indx == (size_t) -1)
526 return FALSE;
527 h->dynstr_index = indx;
528 }
529
530 return TRUE;
531 }
532 \f
533 /* Mark a symbol dynamic. */
534
535 static void
536 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
537 struct elf_link_hash_entry *h,
538 Elf_Internal_Sym *sym)
539 {
540 struct bfd_elf_dynamic_list *d = info->dynamic_list;
541
542 /* It may be called more than once on the same H. */
543 if(h->dynamic || bfd_link_relocatable (info))
544 return;
545
546 if ((info->dynamic_data
547 && (h->type == STT_OBJECT
548 || h->type == STT_COMMON
549 || (sym != NULL
550 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
551 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
552 || (d != NULL
553 && h->root.type == bfd_link_hash_new
554 && (*d->match) (&d->head, NULL, h->root.root.string)))
555 h->dynamic = 1;
556 }
557
558 /* Record an assignment to a symbol made by a linker script. We need
559 this in case some dynamic object refers to this symbol. */
560
561 bfd_boolean
562 bfd_elf_record_link_assignment (bfd *output_bfd,
563 struct bfd_link_info *info,
564 const char *name,
565 bfd_boolean provide,
566 bfd_boolean hidden)
567 {
568 struct elf_link_hash_entry *h, *hv;
569 struct elf_link_hash_table *htab;
570 const struct elf_backend_data *bed;
571
572 if (!is_elf_hash_table (info->hash))
573 return TRUE;
574
575 htab = elf_hash_table (info);
576 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
577 if (h == NULL)
578 return provide;
579
580 if (h->versioned == unknown)
581 {
582 /* Set versioned if symbol version is unknown. */
583 char *version = strrchr (name, ELF_VER_CHR);
584 if (version)
585 {
586 if (version > name && version[-1] != ELF_VER_CHR)
587 h->versioned = versioned_hidden;
588 else
589 h->versioned = versioned;
590 }
591 }
592
593 switch (h->root.type)
594 {
595 case bfd_link_hash_defined:
596 case bfd_link_hash_defweak:
597 case bfd_link_hash_common:
598 break;
599 case bfd_link_hash_undefweak:
600 case bfd_link_hash_undefined:
601 /* Since we're defining the symbol, don't let it seem to have not
602 been defined. record_dynamic_symbol and size_dynamic_sections
603 may depend on this. */
604 h->root.type = bfd_link_hash_new;
605 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
606 bfd_link_repair_undef_list (&htab->root);
607 break;
608 case bfd_link_hash_new:
609 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
610 h->non_elf = 0;
611 break;
612 case bfd_link_hash_indirect:
613 /* We had a versioned symbol in a dynamic library. We make the
614 the versioned symbol point to this one. */
615 bed = get_elf_backend_data (output_bfd);
616 hv = h;
617 while (hv->root.type == bfd_link_hash_indirect
618 || hv->root.type == bfd_link_hash_warning)
619 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
620 /* We don't need to update h->root.u since linker will set them
621 later. */
622 h->root.type = bfd_link_hash_undefined;
623 hv->root.type = bfd_link_hash_indirect;
624 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
625 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
626 break;
627 case bfd_link_hash_warning:
628 abort ();
629 break;
630 }
631
632 /* If this symbol is being provided by the linker script, and it is
633 currently defined by a dynamic object, but not by a regular
634 object, then mark it as undefined so that the generic linker will
635 force the correct value. */
636 if (provide
637 && h->def_dynamic
638 && !h->def_regular)
639 h->root.type = bfd_link_hash_undefined;
640
641 /* If this symbol is not being provided by the linker script, and it is
642 currently defined by a dynamic object, but not by a regular object,
643 then clear out any version information because the symbol will not be
644 associated with the dynamic object any more. */
645 if (!provide
646 && h->def_dynamic
647 && !h->def_regular)
648 h->verinfo.verdef = NULL;
649
650 h->def_regular = 1;
651
652 if (hidden)
653 {
654 bed = get_elf_backend_data (output_bfd);
655 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
656 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
657 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
658 }
659
660 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
661 and executables. */
662 if (!bfd_link_relocatable (info)
663 && h->dynindx != -1
664 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
665 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
666 h->forced_local = 1;
667
668 if ((h->def_dynamic
669 || h->ref_dynamic
670 || bfd_link_dll (info)
671 || elf_hash_table (info)->is_relocatable_executable)
672 && h->dynindx == -1)
673 {
674 if (! bfd_elf_link_record_dynamic_symbol (info, h))
675 return FALSE;
676
677 /* If this is a weak defined symbol, and we know a corresponding
678 real symbol from the same dynamic object, make sure the real
679 symbol is also made into a dynamic symbol. */
680 if (h->u.weakdef != NULL
681 && h->u.weakdef->dynindx == -1)
682 {
683 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
684 return FALSE;
685 }
686 }
687
688 return TRUE;
689 }
690
691 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
692 success, and 2 on a failure caused by attempting to record a symbol
693 in a discarded section, eg. a discarded link-once section symbol. */
694
695 int
696 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
697 bfd *input_bfd,
698 long input_indx)
699 {
700 bfd_size_type amt;
701 struct elf_link_local_dynamic_entry *entry;
702 struct elf_link_hash_table *eht;
703 struct elf_strtab_hash *dynstr;
704 size_t dynstr_index;
705 char *name;
706 Elf_External_Sym_Shndx eshndx;
707 char esym[sizeof (Elf64_External_Sym)];
708
709 if (! is_elf_hash_table (info->hash))
710 return 0;
711
712 /* See if the entry exists already. */
713 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
714 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
715 return 1;
716
717 amt = sizeof (*entry);
718 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
719 if (entry == NULL)
720 return 0;
721
722 /* Go find the symbol, so that we can find it's name. */
723 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
724 1, input_indx, &entry->isym, esym, &eshndx))
725 {
726 bfd_release (input_bfd, entry);
727 return 0;
728 }
729
730 if (entry->isym.st_shndx != SHN_UNDEF
731 && entry->isym.st_shndx < SHN_LORESERVE)
732 {
733 asection *s;
734
735 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
736 if (s == NULL || bfd_is_abs_section (s->output_section))
737 {
738 /* We can still bfd_release here as nothing has done another
739 bfd_alloc. We can't do this later in this function. */
740 bfd_release (input_bfd, entry);
741 return 2;
742 }
743 }
744
745 name = (bfd_elf_string_from_elf_section
746 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
747 entry->isym.st_name));
748
749 dynstr = elf_hash_table (info)->dynstr;
750 if (dynstr == NULL)
751 {
752 /* Create a strtab to hold the dynamic symbol names. */
753 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
754 if (dynstr == NULL)
755 return 0;
756 }
757
758 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
759 if (dynstr_index == (size_t) -1)
760 return 0;
761 entry->isym.st_name = dynstr_index;
762
763 eht = elf_hash_table (info);
764
765 entry->next = eht->dynlocal;
766 eht->dynlocal = entry;
767 entry->input_bfd = input_bfd;
768 entry->input_indx = input_indx;
769 eht->dynsymcount++;
770
771 /* Whatever binding the symbol had before, it's now local. */
772 entry->isym.st_info
773 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
774
775 /* The dynindx will be set at the end of size_dynamic_sections. */
776
777 return 1;
778 }
779
780 /* Return the dynindex of a local dynamic symbol. */
781
782 long
783 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
784 bfd *input_bfd,
785 long input_indx)
786 {
787 struct elf_link_local_dynamic_entry *e;
788
789 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
790 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
791 return e->dynindx;
792 return -1;
793 }
794
795 /* This function is used to renumber the dynamic symbols, if some of
796 them are removed because they are marked as local. This is called
797 via elf_link_hash_traverse. */
798
799 static bfd_boolean
800 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
801 void *data)
802 {
803 size_t *count = (size_t *) data;
804
805 if (h->forced_local)
806 return TRUE;
807
808 if (h->dynindx != -1)
809 h->dynindx = ++(*count);
810
811 return TRUE;
812 }
813
814
815 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
816 STB_LOCAL binding. */
817
818 static bfd_boolean
819 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
820 void *data)
821 {
822 size_t *count = (size_t *) data;
823
824 if (!h->forced_local)
825 return TRUE;
826
827 if (h->dynindx != -1)
828 h->dynindx = ++(*count);
829
830 return TRUE;
831 }
832
833 /* Return true if the dynamic symbol for a given section should be
834 omitted when creating a shared library. */
835 bfd_boolean
836 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
837 struct bfd_link_info *info,
838 asection *p)
839 {
840 struct elf_link_hash_table *htab;
841 asection *ip;
842
843 switch (elf_section_data (p)->this_hdr.sh_type)
844 {
845 case SHT_PROGBITS:
846 case SHT_NOBITS:
847 /* If sh_type is yet undecided, assume it could be
848 SHT_PROGBITS/SHT_NOBITS. */
849 case SHT_NULL:
850 htab = elf_hash_table (info);
851 if (p == htab->tls_sec)
852 return FALSE;
853
854 if (htab->text_index_section != NULL)
855 return p != htab->text_index_section && p != htab->data_index_section;
856
857 return (htab->dynobj != NULL
858 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
859 && ip->output_section == p);
860
861 /* There shouldn't be section relative relocations
862 against any other section. */
863 default:
864 return TRUE;
865 }
866 }
867
868 /* Assign dynsym indices. In a shared library we generate a section
869 symbol for each output section, which come first. Next come symbols
870 which have been forced to local binding. Then all of the back-end
871 allocated local dynamic syms, followed by the rest of the global
872 symbols. */
873
874 static unsigned long
875 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
876 struct bfd_link_info *info,
877 unsigned long *section_sym_count)
878 {
879 unsigned long dynsymcount = 0;
880
881 if (bfd_link_pic (info)
882 || elf_hash_table (info)->is_relocatable_executable)
883 {
884 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
885 asection *p;
886 for (p = output_bfd->sections; p ; p = p->next)
887 if ((p->flags & SEC_EXCLUDE) == 0
888 && (p->flags & SEC_ALLOC) != 0
889 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
890 elf_section_data (p)->dynindx = ++dynsymcount;
891 else
892 elf_section_data (p)->dynindx = 0;
893 }
894 *section_sym_count = dynsymcount;
895
896 elf_link_hash_traverse (elf_hash_table (info),
897 elf_link_renumber_local_hash_table_dynsyms,
898 &dynsymcount);
899
900 if (elf_hash_table (info)->dynlocal)
901 {
902 struct elf_link_local_dynamic_entry *p;
903 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
904 p->dynindx = ++dynsymcount;
905 }
906
907 elf_link_hash_traverse (elf_hash_table (info),
908 elf_link_renumber_hash_table_dynsyms,
909 &dynsymcount);
910
911 /* There is an unused NULL entry at the head of the table which we
912 must account for in our count even if the table is empty since it
913 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
914 .dynamic section. */
915 dynsymcount++;
916
917 elf_hash_table (info)->dynsymcount = dynsymcount;
918 return dynsymcount;
919 }
920
921 /* Merge st_other field. */
922
923 static void
924 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
925 const Elf_Internal_Sym *isym, asection *sec,
926 bfd_boolean definition, bfd_boolean dynamic)
927 {
928 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
929
930 /* If st_other has a processor-specific meaning, specific
931 code might be needed here. */
932 if (bed->elf_backend_merge_symbol_attribute)
933 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
934 dynamic);
935
936 if (!dynamic)
937 {
938 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
939 unsigned hvis = ELF_ST_VISIBILITY (h->other);
940
941 /* Keep the most constraining visibility. Leave the remainder
942 of the st_other field to elf_backend_merge_symbol_attribute. */
943 if (symvis - 1 < hvis - 1)
944 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
945 }
946 else if (definition
947 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
948 && (sec->flags & SEC_READONLY) == 0)
949 h->protected_def = 1;
950 }
951
952 /* This function is called when we want to merge a new symbol with an
953 existing symbol. It handles the various cases which arise when we
954 find a definition in a dynamic object, or when there is already a
955 definition in a dynamic object. The new symbol is described by
956 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
957 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
958 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
959 of an old common symbol. We set OVERRIDE if the old symbol is
960 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
961 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
962 to change. By OK to change, we mean that we shouldn't warn if the
963 type or size does change. */
964
965 static bfd_boolean
966 _bfd_elf_merge_symbol (bfd *abfd,
967 struct bfd_link_info *info,
968 const char *name,
969 Elf_Internal_Sym *sym,
970 asection **psec,
971 bfd_vma *pvalue,
972 struct elf_link_hash_entry **sym_hash,
973 bfd **poldbfd,
974 bfd_boolean *pold_weak,
975 unsigned int *pold_alignment,
976 bfd_boolean *skip,
977 bfd_boolean *override,
978 bfd_boolean *type_change_ok,
979 bfd_boolean *size_change_ok,
980 bfd_boolean *matched)
981 {
982 asection *sec, *oldsec;
983 struct elf_link_hash_entry *h;
984 struct elf_link_hash_entry *hi;
985 struct elf_link_hash_entry *flip;
986 int bind;
987 bfd *oldbfd;
988 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
989 bfd_boolean newweak, oldweak, newfunc, oldfunc;
990 const struct elf_backend_data *bed;
991 char *new_version;
992
993 *skip = FALSE;
994 *override = FALSE;
995
996 sec = *psec;
997 bind = ELF_ST_BIND (sym->st_info);
998
999 if (! bfd_is_und_section (sec))
1000 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1001 else
1002 h = ((struct elf_link_hash_entry *)
1003 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1004 if (h == NULL)
1005 return FALSE;
1006 *sym_hash = h;
1007
1008 bed = get_elf_backend_data (abfd);
1009
1010 /* NEW_VERSION is the symbol version of the new symbol. */
1011 if (h->versioned != unversioned)
1012 {
1013 /* Symbol version is unknown or versioned. */
1014 new_version = strrchr (name, ELF_VER_CHR);
1015 if (new_version)
1016 {
1017 if (h->versioned == unknown)
1018 {
1019 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1020 h->versioned = versioned_hidden;
1021 else
1022 h->versioned = versioned;
1023 }
1024 new_version += 1;
1025 if (new_version[0] == '\0')
1026 new_version = NULL;
1027 }
1028 else
1029 h->versioned = unversioned;
1030 }
1031 else
1032 new_version = NULL;
1033
1034 /* For merging, we only care about real symbols. But we need to make
1035 sure that indirect symbol dynamic flags are updated. */
1036 hi = h;
1037 while (h->root.type == bfd_link_hash_indirect
1038 || h->root.type == bfd_link_hash_warning)
1039 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1040
1041 if (!*matched)
1042 {
1043 if (hi == h || h->root.type == bfd_link_hash_new)
1044 *matched = TRUE;
1045 else
1046 {
1047 /* OLD_HIDDEN is true if the existing symbol is only visible
1048 to the symbol with the same symbol version. NEW_HIDDEN is
1049 true if the new symbol is only visible to the symbol with
1050 the same symbol version. */
1051 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1052 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1053 if (!old_hidden && !new_hidden)
1054 /* The new symbol matches the existing symbol if both
1055 aren't hidden. */
1056 *matched = TRUE;
1057 else
1058 {
1059 /* OLD_VERSION is the symbol version of the existing
1060 symbol. */
1061 char *old_version;
1062
1063 if (h->versioned >= versioned)
1064 old_version = strrchr (h->root.root.string,
1065 ELF_VER_CHR) + 1;
1066 else
1067 old_version = NULL;
1068
1069 /* The new symbol matches the existing symbol if they
1070 have the same symbol version. */
1071 *matched = (old_version == new_version
1072 || (old_version != NULL
1073 && new_version != NULL
1074 && strcmp (old_version, new_version) == 0));
1075 }
1076 }
1077 }
1078
1079 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1080 existing symbol. */
1081
1082 oldbfd = NULL;
1083 oldsec = NULL;
1084 switch (h->root.type)
1085 {
1086 default:
1087 break;
1088
1089 case bfd_link_hash_undefined:
1090 case bfd_link_hash_undefweak:
1091 oldbfd = h->root.u.undef.abfd;
1092 break;
1093
1094 case bfd_link_hash_defined:
1095 case bfd_link_hash_defweak:
1096 oldbfd = h->root.u.def.section->owner;
1097 oldsec = h->root.u.def.section;
1098 break;
1099
1100 case bfd_link_hash_common:
1101 oldbfd = h->root.u.c.p->section->owner;
1102 oldsec = h->root.u.c.p->section;
1103 if (pold_alignment)
1104 *pold_alignment = h->root.u.c.p->alignment_power;
1105 break;
1106 }
1107 if (poldbfd && *poldbfd == NULL)
1108 *poldbfd = oldbfd;
1109
1110 /* Differentiate strong and weak symbols. */
1111 newweak = bind == STB_WEAK;
1112 oldweak = (h->root.type == bfd_link_hash_defweak
1113 || h->root.type == bfd_link_hash_undefweak);
1114 if (pold_weak)
1115 *pold_weak = oldweak;
1116
1117 /* This code is for coping with dynamic objects, and is only useful
1118 if we are doing an ELF link. */
1119 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1120 return TRUE;
1121
1122 /* We have to check it for every instance since the first few may be
1123 references and not all compilers emit symbol type for undefined
1124 symbols. */
1125 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1126
1127 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1128 respectively, is from a dynamic object. */
1129
1130 newdyn = (abfd->flags & DYNAMIC) != 0;
1131
1132 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1133 syms and defined syms in dynamic libraries respectively.
1134 ref_dynamic on the other hand can be set for a symbol defined in
1135 a dynamic library, and def_dynamic may not be set; When the
1136 definition in a dynamic lib is overridden by a definition in the
1137 executable use of the symbol in the dynamic lib becomes a
1138 reference to the executable symbol. */
1139 if (newdyn)
1140 {
1141 if (bfd_is_und_section (sec))
1142 {
1143 if (bind != STB_WEAK)
1144 {
1145 h->ref_dynamic_nonweak = 1;
1146 hi->ref_dynamic_nonweak = 1;
1147 }
1148 }
1149 else
1150 {
1151 /* Update the existing symbol only if they match. */
1152 if (*matched)
1153 h->dynamic_def = 1;
1154 hi->dynamic_def = 1;
1155 }
1156 }
1157
1158 /* If we just created the symbol, mark it as being an ELF symbol.
1159 Other than that, there is nothing to do--there is no merge issue
1160 with a newly defined symbol--so we just return. */
1161
1162 if (h->root.type == bfd_link_hash_new)
1163 {
1164 h->non_elf = 0;
1165 return TRUE;
1166 }
1167
1168 /* In cases involving weak versioned symbols, we may wind up trying
1169 to merge a symbol with itself. Catch that here, to avoid the
1170 confusion that results if we try to override a symbol with
1171 itself. The additional tests catch cases like
1172 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1173 dynamic object, which we do want to handle here. */
1174 if (abfd == oldbfd
1175 && (newweak || oldweak)
1176 && ((abfd->flags & DYNAMIC) == 0
1177 || !h->def_regular))
1178 return TRUE;
1179
1180 olddyn = FALSE;
1181 if (oldbfd != NULL)
1182 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1183 else if (oldsec != NULL)
1184 {
1185 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1186 indices used by MIPS ELF. */
1187 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1188 }
1189
1190 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1191 respectively, appear to be a definition rather than reference. */
1192
1193 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1194
1195 olddef = (h->root.type != bfd_link_hash_undefined
1196 && h->root.type != bfd_link_hash_undefweak
1197 && h->root.type != bfd_link_hash_common);
1198
1199 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1200 respectively, appear to be a function. */
1201
1202 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1203 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1204
1205 oldfunc = (h->type != STT_NOTYPE
1206 && bed->is_function_type (h->type));
1207
1208 /* If creating a default indirect symbol ("foo" or "foo@") from a
1209 dynamic versioned definition ("foo@@") skip doing so if there is
1210 an existing regular definition with a different type. We don't
1211 want, for example, a "time" variable in the executable overriding
1212 a "time" function in a shared library. */
1213 if (pold_alignment == NULL
1214 && newdyn
1215 && newdef
1216 && !olddyn
1217 && (olddef || h->root.type == bfd_link_hash_common)
1218 && ELF_ST_TYPE (sym->st_info) != h->type
1219 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1220 && h->type != STT_NOTYPE
1221 && !(newfunc && oldfunc))
1222 {
1223 *skip = TRUE;
1224 return TRUE;
1225 }
1226
1227 /* Check TLS symbols. We don't check undefined symbols introduced
1228 by "ld -u" which have no type (and oldbfd NULL), and we don't
1229 check symbols from plugins because they also have no type. */
1230 if (oldbfd != NULL
1231 && (oldbfd->flags & BFD_PLUGIN) == 0
1232 && (abfd->flags & BFD_PLUGIN) == 0
1233 && ELF_ST_TYPE (sym->st_info) != h->type
1234 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1235 {
1236 bfd *ntbfd, *tbfd;
1237 bfd_boolean ntdef, tdef;
1238 asection *ntsec, *tsec;
1239
1240 if (h->type == STT_TLS)
1241 {
1242 ntbfd = abfd;
1243 ntsec = sec;
1244 ntdef = newdef;
1245 tbfd = oldbfd;
1246 tsec = oldsec;
1247 tdef = olddef;
1248 }
1249 else
1250 {
1251 ntbfd = oldbfd;
1252 ntsec = oldsec;
1253 ntdef = olddef;
1254 tbfd = abfd;
1255 tsec = sec;
1256 tdef = newdef;
1257 }
1258
1259 if (tdef && ntdef)
1260 (*_bfd_error_handler)
1261 (_("%s: TLS definition in %B section %A "
1262 "mismatches non-TLS definition in %B section %A"),
1263 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1264 else if (!tdef && !ntdef)
1265 (*_bfd_error_handler)
1266 (_("%s: TLS reference in %B "
1267 "mismatches non-TLS reference in %B"),
1268 tbfd, ntbfd, h->root.root.string);
1269 else if (tdef)
1270 (*_bfd_error_handler)
1271 (_("%s: TLS definition in %B section %A "
1272 "mismatches non-TLS reference in %B"),
1273 tbfd, tsec, ntbfd, h->root.root.string);
1274 else
1275 (*_bfd_error_handler)
1276 (_("%s: TLS reference in %B "
1277 "mismatches non-TLS definition in %B section %A"),
1278 tbfd, ntbfd, ntsec, h->root.root.string);
1279
1280 bfd_set_error (bfd_error_bad_value);
1281 return FALSE;
1282 }
1283
1284 /* If the old symbol has non-default visibility, we ignore the new
1285 definition from a dynamic object. */
1286 if (newdyn
1287 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1288 && !bfd_is_und_section (sec))
1289 {
1290 *skip = TRUE;
1291 /* Make sure this symbol is dynamic. */
1292 h->ref_dynamic = 1;
1293 hi->ref_dynamic = 1;
1294 /* A protected symbol has external availability. Make sure it is
1295 recorded as dynamic.
1296
1297 FIXME: Should we check type and size for protected symbol? */
1298 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1299 return bfd_elf_link_record_dynamic_symbol (info, h);
1300 else
1301 return TRUE;
1302 }
1303 else if (!newdyn
1304 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1305 && h->def_dynamic)
1306 {
1307 /* If the new symbol with non-default visibility comes from a
1308 relocatable file and the old definition comes from a dynamic
1309 object, we remove the old definition. */
1310 if (hi->root.type == bfd_link_hash_indirect)
1311 {
1312 /* Handle the case where the old dynamic definition is
1313 default versioned. We need to copy the symbol info from
1314 the symbol with default version to the normal one if it
1315 was referenced before. */
1316 if (h->ref_regular)
1317 {
1318 hi->root.type = h->root.type;
1319 h->root.type = bfd_link_hash_indirect;
1320 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1321
1322 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1323 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1324 {
1325 /* If the new symbol is hidden or internal, completely undo
1326 any dynamic link state. */
1327 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1328 h->forced_local = 0;
1329 h->ref_dynamic = 0;
1330 }
1331 else
1332 h->ref_dynamic = 1;
1333
1334 h->def_dynamic = 0;
1335 /* FIXME: Should we check type and size for protected symbol? */
1336 h->size = 0;
1337 h->type = 0;
1338
1339 h = hi;
1340 }
1341 else
1342 h = hi;
1343 }
1344
1345 /* If the old symbol was undefined before, then it will still be
1346 on the undefs list. If the new symbol is undefined or
1347 common, we can't make it bfd_link_hash_new here, because new
1348 undefined or common symbols will be added to the undefs list
1349 by _bfd_generic_link_add_one_symbol. Symbols may not be
1350 added twice to the undefs list. Also, if the new symbol is
1351 undefweak then we don't want to lose the strong undef. */
1352 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1353 {
1354 h->root.type = bfd_link_hash_undefined;
1355 h->root.u.undef.abfd = abfd;
1356 }
1357 else
1358 {
1359 h->root.type = bfd_link_hash_new;
1360 h->root.u.undef.abfd = NULL;
1361 }
1362
1363 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1364 {
1365 /* If the new symbol is hidden or internal, completely undo
1366 any dynamic link state. */
1367 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1368 h->forced_local = 0;
1369 h->ref_dynamic = 0;
1370 }
1371 else
1372 h->ref_dynamic = 1;
1373 h->def_dynamic = 0;
1374 /* FIXME: Should we check type and size for protected symbol? */
1375 h->size = 0;
1376 h->type = 0;
1377 return TRUE;
1378 }
1379
1380 /* If a new weak symbol definition comes from a regular file and the
1381 old symbol comes from a dynamic library, we treat the new one as
1382 strong. Similarly, an old weak symbol definition from a regular
1383 file is treated as strong when the new symbol comes from a dynamic
1384 library. Further, an old weak symbol from a dynamic library is
1385 treated as strong if the new symbol is from a dynamic library.
1386 This reflects the way glibc's ld.so works.
1387
1388 Do this before setting *type_change_ok or *size_change_ok so that
1389 we warn properly when dynamic library symbols are overridden. */
1390
1391 if (newdef && !newdyn && olddyn)
1392 newweak = FALSE;
1393 if (olddef && newdyn)
1394 oldweak = FALSE;
1395
1396 /* Allow changes between different types of function symbol. */
1397 if (newfunc && oldfunc)
1398 *type_change_ok = TRUE;
1399
1400 /* It's OK to change the type if either the existing symbol or the
1401 new symbol is weak. A type change is also OK if the old symbol
1402 is undefined and the new symbol is defined. */
1403
1404 if (oldweak
1405 || newweak
1406 || (newdef
1407 && h->root.type == bfd_link_hash_undefined))
1408 *type_change_ok = TRUE;
1409
1410 /* It's OK to change the size if either the existing symbol or the
1411 new symbol is weak, or if the old symbol is undefined. */
1412
1413 if (*type_change_ok
1414 || h->root.type == bfd_link_hash_undefined)
1415 *size_change_ok = TRUE;
1416
1417 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1418 symbol, respectively, appears to be a common symbol in a dynamic
1419 object. If a symbol appears in an uninitialized section, and is
1420 not weak, and is not a function, then it may be a common symbol
1421 which was resolved when the dynamic object was created. We want
1422 to treat such symbols specially, because they raise special
1423 considerations when setting the symbol size: if the symbol
1424 appears as a common symbol in a regular object, and the size in
1425 the regular object is larger, we must make sure that we use the
1426 larger size. This problematic case can always be avoided in C,
1427 but it must be handled correctly when using Fortran shared
1428 libraries.
1429
1430 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1431 likewise for OLDDYNCOMMON and OLDDEF.
1432
1433 Note that this test is just a heuristic, and that it is quite
1434 possible to have an uninitialized symbol in a shared object which
1435 is really a definition, rather than a common symbol. This could
1436 lead to some minor confusion when the symbol really is a common
1437 symbol in some regular object. However, I think it will be
1438 harmless. */
1439
1440 if (newdyn
1441 && newdef
1442 && !newweak
1443 && (sec->flags & SEC_ALLOC) != 0
1444 && (sec->flags & SEC_LOAD) == 0
1445 && sym->st_size > 0
1446 && !newfunc)
1447 newdyncommon = TRUE;
1448 else
1449 newdyncommon = FALSE;
1450
1451 if (olddyn
1452 && olddef
1453 && h->root.type == bfd_link_hash_defined
1454 && h->def_dynamic
1455 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1456 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1457 && h->size > 0
1458 && !oldfunc)
1459 olddyncommon = TRUE;
1460 else
1461 olddyncommon = FALSE;
1462
1463 /* We now know everything about the old and new symbols. We ask the
1464 backend to check if we can merge them. */
1465 if (bed->merge_symbol != NULL)
1466 {
1467 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1468 return FALSE;
1469 sec = *psec;
1470 }
1471
1472 /* If both the old and the new symbols look like common symbols in a
1473 dynamic object, set the size of the symbol to the larger of the
1474 two. */
1475
1476 if (olddyncommon
1477 && newdyncommon
1478 && sym->st_size != h->size)
1479 {
1480 /* Since we think we have two common symbols, issue a multiple
1481 common warning if desired. Note that we only warn if the
1482 size is different. If the size is the same, we simply let
1483 the old symbol override the new one as normally happens with
1484 symbols defined in dynamic objects. */
1485
1486 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1487 bfd_link_hash_common, sym->st_size);
1488 if (sym->st_size > h->size)
1489 h->size = sym->st_size;
1490
1491 *size_change_ok = TRUE;
1492 }
1493
1494 /* If we are looking at a dynamic object, and we have found a
1495 definition, we need to see if the symbol was already defined by
1496 some other object. If so, we want to use the existing
1497 definition, and we do not want to report a multiple symbol
1498 definition error; we do this by clobbering *PSEC to be
1499 bfd_und_section_ptr.
1500
1501 We treat a common symbol as a definition if the symbol in the
1502 shared library is a function, since common symbols always
1503 represent variables; this can cause confusion in principle, but
1504 any such confusion would seem to indicate an erroneous program or
1505 shared library. We also permit a common symbol in a regular
1506 object to override a weak symbol in a shared object. A common
1507 symbol in executable also overrides a symbol in a shared object. */
1508
1509 if (newdyn
1510 && newdef
1511 && (olddef
1512 || (h->root.type == bfd_link_hash_common
1513 && (newweak
1514 || newfunc
1515 || (!olddyn && bfd_link_executable (info))))))
1516 {
1517 *override = TRUE;
1518 newdef = FALSE;
1519 newdyncommon = FALSE;
1520
1521 *psec = sec = bfd_und_section_ptr;
1522 *size_change_ok = TRUE;
1523
1524 /* If we get here when the old symbol is a common symbol, then
1525 we are explicitly letting it override a weak symbol or
1526 function in a dynamic object, and we don't want to warn about
1527 a type change. If the old symbol is a defined symbol, a type
1528 change warning may still be appropriate. */
1529
1530 if (h->root.type == bfd_link_hash_common)
1531 *type_change_ok = TRUE;
1532 }
1533
1534 /* Handle the special case of an old common symbol merging with a
1535 new symbol which looks like a common symbol in a shared object.
1536 We change *PSEC and *PVALUE to make the new symbol look like a
1537 common symbol, and let _bfd_generic_link_add_one_symbol do the
1538 right thing. */
1539
1540 if (newdyncommon
1541 && h->root.type == bfd_link_hash_common)
1542 {
1543 *override = TRUE;
1544 newdef = FALSE;
1545 newdyncommon = FALSE;
1546 *pvalue = sym->st_size;
1547 *psec = sec = bed->common_section (oldsec);
1548 *size_change_ok = TRUE;
1549 }
1550
1551 /* Skip weak definitions of symbols that are already defined. */
1552 if (newdef && olddef && newweak)
1553 {
1554 /* Don't skip new non-IR weak syms. */
1555 if (!(oldbfd != NULL
1556 && (oldbfd->flags & BFD_PLUGIN) != 0
1557 && (abfd->flags & BFD_PLUGIN) == 0))
1558 {
1559 newdef = FALSE;
1560 *skip = TRUE;
1561 }
1562
1563 /* Merge st_other. If the symbol already has a dynamic index,
1564 but visibility says it should not be visible, turn it into a
1565 local symbol. */
1566 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1567 if (h->dynindx != -1)
1568 switch (ELF_ST_VISIBILITY (h->other))
1569 {
1570 case STV_INTERNAL:
1571 case STV_HIDDEN:
1572 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1573 break;
1574 }
1575 }
1576
1577 /* If the old symbol is from a dynamic object, and the new symbol is
1578 a definition which is not from a dynamic object, then the new
1579 symbol overrides the old symbol. Symbols from regular files
1580 always take precedence over symbols from dynamic objects, even if
1581 they are defined after the dynamic object in the link.
1582
1583 As above, we again permit a common symbol in a regular object to
1584 override a definition in a shared object if the shared object
1585 symbol is a function or is weak. */
1586
1587 flip = NULL;
1588 if (!newdyn
1589 && (newdef
1590 || (bfd_is_com_section (sec)
1591 && (oldweak || oldfunc)))
1592 && olddyn
1593 && olddef
1594 && h->def_dynamic)
1595 {
1596 /* Change the hash table entry to undefined, and let
1597 _bfd_generic_link_add_one_symbol do the right thing with the
1598 new definition. */
1599
1600 h->root.type = bfd_link_hash_undefined;
1601 h->root.u.undef.abfd = h->root.u.def.section->owner;
1602 *size_change_ok = TRUE;
1603
1604 olddef = FALSE;
1605 olddyncommon = FALSE;
1606
1607 /* We again permit a type change when a common symbol may be
1608 overriding a function. */
1609
1610 if (bfd_is_com_section (sec))
1611 {
1612 if (oldfunc)
1613 {
1614 /* If a common symbol overrides a function, make sure
1615 that it isn't defined dynamically nor has type
1616 function. */
1617 h->def_dynamic = 0;
1618 h->type = STT_NOTYPE;
1619 }
1620 *type_change_ok = TRUE;
1621 }
1622
1623 if (hi->root.type == bfd_link_hash_indirect)
1624 flip = hi;
1625 else
1626 /* This union may have been set to be non-NULL when this symbol
1627 was seen in a dynamic object. We must force the union to be
1628 NULL, so that it is correct for a regular symbol. */
1629 h->verinfo.vertree = NULL;
1630 }
1631
1632 /* Handle the special case of a new common symbol merging with an
1633 old symbol that looks like it might be a common symbol defined in
1634 a shared object. Note that we have already handled the case in
1635 which a new common symbol should simply override the definition
1636 in the shared library. */
1637
1638 if (! newdyn
1639 && bfd_is_com_section (sec)
1640 && olddyncommon)
1641 {
1642 /* It would be best if we could set the hash table entry to a
1643 common symbol, but we don't know what to use for the section
1644 or the alignment. */
1645 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1646 bfd_link_hash_common, sym->st_size);
1647
1648 /* If the presumed common symbol in the dynamic object is
1649 larger, pretend that the new symbol has its size. */
1650
1651 if (h->size > *pvalue)
1652 *pvalue = h->size;
1653
1654 /* We need to remember the alignment required by the symbol
1655 in the dynamic object. */
1656 BFD_ASSERT (pold_alignment);
1657 *pold_alignment = h->root.u.def.section->alignment_power;
1658
1659 olddef = FALSE;
1660 olddyncommon = FALSE;
1661
1662 h->root.type = bfd_link_hash_undefined;
1663 h->root.u.undef.abfd = h->root.u.def.section->owner;
1664
1665 *size_change_ok = TRUE;
1666 *type_change_ok = TRUE;
1667
1668 if (hi->root.type == bfd_link_hash_indirect)
1669 flip = hi;
1670 else
1671 h->verinfo.vertree = NULL;
1672 }
1673
1674 if (flip != NULL)
1675 {
1676 /* Handle the case where we had a versioned symbol in a dynamic
1677 library and now find a definition in a normal object. In this
1678 case, we make the versioned symbol point to the normal one. */
1679 flip->root.type = h->root.type;
1680 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1681 h->root.type = bfd_link_hash_indirect;
1682 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1683 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1684 if (h->def_dynamic)
1685 {
1686 h->def_dynamic = 0;
1687 flip->ref_dynamic = 1;
1688 }
1689 }
1690
1691 return TRUE;
1692 }
1693
1694 /* This function is called to create an indirect symbol from the
1695 default for the symbol with the default version if needed. The
1696 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1697 set DYNSYM if the new indirect symbol is dynamic. */
1698
1699 static bfd_boolean
1700 _bfd_elf_add_default_symbol (bfd *abfd,
1701 struct bfd_link_info *info,
1702 struct elf_link_hash_entry *h,
1703 const char *name,
1704 Elf_Internal_Sym *sym,
1705 asection *sec,
1706 bfd_vma value,
1707 bfd **poldbfd,
1708 bfd_boolean *dynsym)
1709 {
1710 bfd_boolean type_change_ok;
1711 bfd_boolean size_change_ok;
1712 bfd_boolean skip;
1713 char *shortname;
1714 struct elf_link_hash_entry *hi;
1715 struct bfd_link_hash_entry *bh;
1716 const struct elf_backend_data *bed;
1717 bfd_boolean collect;
1718 bfd_boolean dynamic;
1719 bfd_boolean override;
1720 char *p;
1721 size_t len, shortlen;
1722 asection *tmp_sec;
1723 bfd_boolean matched;
1724
1725 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1726 return TRUE;
1727
1728 /* If this symbol has a version, and it is the default version, we
1729 create an indirect symbol from the default name to the fully
1730 decorated name. This will cause external references which do not
1731 specify a version to be bound to this version of the symbol. */
1732 p = strchr (name, ELF_VER_CHR);
1733 if (h->versioned == unknown)
1734 {
1735 if (p == NULL)
1736 {
1737 h->versioned = unversioned;
1738 return TRUE;
1739 }
1740 else
1741 {
1742 if (p[1] != ELF_VER_CHR)
1743 {
1744 h->versioned = versioned_hidden;
1745 return TRUE;
1746 }
1747 else
1748 h->versioned = versioned;
1749 }
1750 }
1751 else
1752 {
1753 /* PR ld/19073: We may see an unversioned definition after the
1754 default version. */
1755 if (p == NULL)
1756 return TRUE;
1757 }
1758
1759 bed = get_elf_backend_data (abfd);
1760 collect = bed->collect;
1761 dynamic = (abfd->flags & DYNAMIC) != 0;
1762
1763 shortlen = p - name;
1764 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1765 if (shortname == NULL)
1766 return FALSE;
1767 memcpy (shortname, name, shortlen);
1768 shortname[shortlen] = '\0';
1769
1770 /* We are going to create a new symbol. Merge it with any existing
1771 symbol with this name. For the purposes of the merge, act as
1772 though we were defining the symbol we just defined, although we
1773 actually going to define an indirect symbol. */
1774 type_change_ok = FALSE;
1775 size_change_ok = FALSE;
1776 matched = TRUE;
1777 tmp_sec = sec;
1778 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1779 &hi, poldbfd, NULL, NULL, &skip, &override,
1780 &type_change_ok, &size_change_ok, &matched))
1781 return FALSE;
1782
1783 if (skip)
1784 goto nondefault;
1785
1786 if (hi->def_regular)
1787 {
1788 /* If the undecorated symbol will have a version added by a
1789 script different to H, then don't indirect to/from the
1790 undecorated symbol. This isn't ideal because we may not yet
1791 have seen symbol versions, if given by a script on the
1792 command line rather than via --version-script. */
1793 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1794 {
1795 bfd_boolean hide;
1796
1797 hi->verinfo.vertree
1798 = bfd_find_version_for_sym (info->version_info,
1799 hi->root.root.string, &hide);
1800 if (hi->verinfo.vertree != NULL && hide)
1801 {
1802 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1803 goto nondefault;
1804 }
1805 }
1806 if (hi->verinfo.vertree != NULL
1807 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1808 goto nondefault;
1809 }
1810
1811 if (! override)
1812 {
1813 /* Add the default symbol if not performing a relocatable link. */
1814 if (! bfd_link_relocatable (info))
1815 {
1816 bh = &hi->root;
1817 if (! (_bfd_generic_link_add_one_symbol
1818 (info, abfd, shortname, BSF_INDIRECT,
1819 bfd_ind_section_ptr,
1820 0, name, FALSE, collect, &bh)))
1821 return FALSE;
1822 hi = (struct elf_link_hash_entry *) bh;
1823 }
1824 }
1825 else
1826 {
1827 /* In this case the symbol named SHORTNAME is overriding the
1828 indirect symbol we want to add. We were planning on making
1829 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1830 is the name without a version. NAME is the fully versioned
1831 name, and it is the default version.
1832
1833 Overriding means that we already saw a definition for the
1834 symbol SHORTNAME in a regular object, and it is overriding
1835 the symbol defined in the dynamic object.
1836
1837 When this happens, we actually want to change NAME, the
1838 symbol we just added, to refer to SHORTNAME. This will cause
1839 references to NAME in the shared object to become references
1840 to SHORTNAME in the regular object. This is what we expect
1841 when we override a function in a shared object: that the
1842 references in the shared object will be mapped to the
1843 definition in the regular object. */
1844
1845 while (hi->root.type == bfd_link_hash_indirect
1846 || hi->root.type == bfd_link_hash_warning)
1847 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1848
1849 h->root.type = bfd_link_hash_indirect;
1850 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1851 if (h->def_dynamic)
1852 {
1853 h->def_dynamic = 0;
1854 hi->ref_dynamic = 1;
1855 if (hi->ref_regular
1856 || hi->def_regular)
1857 {
1858 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1859 return FALSE;
1860 }
1861 }
1862
1863 /* Now set HI to H, so that the following code will set the
1864 other fields correctly. */
1865 hi = h;
1866 }
1867
1868 /* Check if HI is a warning symbol. */
1869 if (hi->root.type == bfd_link_hash_warning)
1870 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1871
1872 /* If there is a duplicate definition somewhere, then HI may not
1873 point to an indirect symbol. We will have reported an error to
1874 the user in that case. */
1875
1876 if (hi->root.type == bfd_link_hash_indirect)
1877 {
1878 struct elf_link_hash_entry *ht;
1879
1880 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1881 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1882
1883 /* A reference to the SHORTNAME symbol from a dynamic library
1884 will be satisfied by the versioned symbol at runtime. In
1885 effect, we have a reference to the versioned symbol. */
1886 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1887 hi->dynamic_def |= ht->dynamic_def;
1888
1889 /* See if the new flags lead us to realize that the symbol must
1890 be dynamic. */
1891 if (! *dynsym)
1892 {
1893 if (! dynamic)
1894 {
1895 if (! bfd_link_executable (info)
1896 || hi->def_dynamic
1897 || hi->ref_dynamic)
1898 *dynsym = TRUE;
1899 }
1900 else
1901 {
1902 if (hi->ref_regular)
1903 *dynsym = TRUE;
1904 }
1905 }
1906 }
1907
1908 /* We also need to define an indirection from the nondefault version
1909 of the symbol. */
1910
1911 nondefault:
1912 len = strlen (name);
1913 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
1914 if (shortname == NULL)
1915 return FALSE;
1916 memcpy (shortname, name, shortlen);
1917 memcpy (shortname + shortlen, p + 1, len - shortlen);
1918
1919 /* Once again, merge with any existing symbol. */
1920 type_change_ok = FALSE;
1921 size_change_ok = FALSE;
1922 tmp_sec = sec;
1923 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1924 &hi, poldbfd, NULL, NULL, &skip, &override,
1925 &type_change_ok, &size_change_ok, &matched))
1926 return FALSE;
1927
1928 if (skip)
1929 return TRUE;
1930
1931 if (override)
1932 {
1933 /* Here SHORTNAME is a versioned name, so we don't expect to see
1934 the type of override we do in the case above unless it is
1935 overridden by a versioned definition. */
1936 if (hi->root.type != bfd_link_hash_defined
1937 && hi->root.type != bfd_link_hash_defweak)
1938 (*_bfd_error_handler)
1939 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1940 abfd, shortname);
1941 }
1942 else
1943 {
1944 bh = &hi->root;
1945 if (! (_bfd_generic_link_add_one_symbol
1946 (info, abfd, shortname, BSF_INDIRECT,
1947 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1948 return FALSE;
1949 hi = (struct elf_link_hash_entry *) bh;
1950
1951 /* If there is a duplicate definition somewhere, then HI may not
1952 point to an indirect symbol. We will have reported an error
1953 to the user in that case. */
1954
1955 if (hi->root.type == bfd_link_hash_indirect)
1956 {
1957 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1958 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1959 hi->dynamic_def |= h->dynamic_def;
1960
1961 /* See if the new flags lead us to realize that the symbol
1962 must be dynamic. */
1963 if (! *dynsym)
1964 {
1965 if (! dynamic)
1966 {
1967 if (! bfd_link_executable (info)
1968 || hi->ref_dynamic)
1969 *dynsym = TRUE;
1970 }
1971 else
1972 {
1973 if (hi->ref_regular)
1974 *dynsym = TRUE;
1975 }
1976 }
1977 }
1978 }
1979
1980 return TRUE;
1981 }
1982 \f
1983 /* This routine is used to export all defined symbols into the dynamic
1984 symbol table. It is called via elf_link_hash_traverse. */
1985
1986 static bfd_boolean
1987 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1988 {
1989 struct elf_info_failed *eif = (struct elf_info_failed *) data;
1990
1991 /* Ignore indirect symbols. These are added by the versioning code. */
1992 if (h->root.type == bfd_link_hash_indirect)
1993 return TRUE;
1994
1995 /* Ignore this if we won't export it. */
1996 if (!eif->info->export_dynamic && !h->dynamic)
1997 return TRUE;
1998
1999 if (h->dynindx == -1
2000 && (h->def_regular || h->ref_regular)
2001 && ! bfd_hide_sym_by_version (eif->info->version_info,
2002 h->root.root.string))
2003 {
2004 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2005 {
2006 eif->failed = TRUE;
2007 return FALSE;
2008 }
2009 }
2010
2011 return TRUE;
2012 }
2013 \f
2014 /* Look through the symbols which are defined in other shared
2015 libraries and referenced here. Update the list of version
2016 dependencies. This will be put into the .gnu.version_r section.
2017 This function is called via elf_link_hash_traverse. */
2018
2019 static bfd_boolean
2020 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2021 void *data)
2022 {
2023 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2024 Elf_Internal_Verneed *t;
2025 Elf_Internal_Vernaux *a;
2026 bfd_size_type amt;
2027
2028 /* We only care about symbols defined in shared objects with version
2029 information. */
2030 if (!h->def_dynamic
2031 || h->def_regular
2032 || h->dynindx == -1
2033 || h->verinfo.verdef == NULL
2034 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2035 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2036 return TRUE;
2037
2038 /* See if we already know about this version. */
2039 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2040 t != NULL;
2041 t = t->vn_nextref)
2042 {
2043 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2044 continue;
2045
2046 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2047 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2048 return TRUE;
2049
2050 break;
2051 }
2052
2053 /* This is a new version. Add it to tree we are building. */
2054
2055 if (t == NULL)
2056 {
2057 amt = sizeof *t;
2058 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2059 if (t == NULL)
2060 {
2061 rinfo->failed = TRUE;
2062 return FALSE;
2063 }
2064
2065 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2066 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2067 elf_tdata (rinfo->info->output_bfd)->verref = t;
2068 }
2069
2070 amt = sizeof *a;
2071 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2072 if (a == NULL)
2073 {
2074 rinfo->failed = TRUE;
2075 return FALSE;
2076 }
2077
2078 /* Note that we are copying a string pointer here, and testing it
2079 above. If bfd_elf_string_from_elf_section is ever changed to
2080 discard the string data when low in memory, this will have to be
2081 fixed. */
2082 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2083
2084 a->vna_flags = h->verinfo.verdef->vd_flags;
2085 a->vna_nextptr = t->vn_auxptr;
2086
2087 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2088 ++rinfo->vers;
2089
2090 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2091
2092 t->vn_auxptr = a;
2093
2094 return TRUE;
2095 }
2096
2097 /* Figure out appropriate versions for all the symbols. We may not
2098 have the version number script until we have read all of the input
2099 files, so until that point we don't know which symbols should be
2100 local. This function is called via elf_link_hash_traverse. */
2101
2102 static bfd_boolean
2103 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2104 {
2105 struct elf_info_failed *sinfo;
2106 struct bfd_link_info *info;
2107 const struct elf_backend_data *bed;
2108 struct elf_info_failed eif;
2109 char *p;
2110
2111 sinfo = (struct elf_info_failed *) data;
2112 info = sinfo->info;
2113
2114 /* Fix the symbol flags. */
2115 eif.failed = FALSE;
2116 eif.info = info;
2117 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2118 {
2119 if (eif.failed)
2120 sinfo->failed = TRUE;
2121 return FALSE;
2122 }
2123
2124 /* We only need version numbers for symbols defined in regular
2125 objects. */
2126 if (!h->def_regular)
2127 return TRUE;
2128
2129 bed = get_elf_backend_data (info->output_bfd);
2130 p = strchr (h->root.root.string, ELF_VER_CHR);
2131 if (p != NULL && h->verinfo.vertree == NULL)
2132 {
2133 struct bfd_elf_version_tree *t;
2134
2135 ++p;
2136 if (*p == ELF_VER_CHR)
2137 ++p;
2138
2139 /* If there is no version string, we can just return out. */
2140 if (*p == '\0')
2141 return TRUE;
2142
2143 /* Look for the version. If we find it, it is no longer weak. */
2144 for (t = sinfo->info->version_info; t != NULL; t = t->next)
2145 {
2146 if (strcmp (t->name, p) == 0)
2147 {
2148 size_t len;
2149 char *alc;
2150 struct bfd_elf_version_expr *d;
2151
2152 len = p - h->root.root.string;
2153 alc = (char *) bfd_malloc (len);
2154 if (alc == NULL)
2155 {
2156 sinfo->failed = TRUE;
2157 return FALSE;
2158 }
2159 memcpy (alc, h->root.root.string, len - 1);
2160 alc[len - 1] = '\0';
2161 if (alc[len - 2] == ELF_VER_CHR)
2162 alc[len - 2] = '\0';
2163
2164 h->verinfo.vertree = t;
2165 t->used = TRUE;
2166 d = NULL;
2167
2168 if (t->globals.list != NULL)
2169 d = (*t->match) (&t->globals, NULL, alc);
2170
2171 /* See if there is anything to force this symbol to
2172 local scope. */
2173 if (d == NULL && t->locals.list != NULL)
2174 {
2175 d = (*t->match) (&t->locals, NULL, alc);
2176 if (d != NULL
2177 && h->dynindx != -1
2178 && ! info->export_dynamic)
2179 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2180 }
2181
2182 free (alc);
2183 break;
2184 }
2185 }
2186
2187 /* If we are building an application, we need to create a
2188 version node for this version. */
2189 if (t == NULL && bfd_link_executable (info))
2190 {
2191 struct bfd_elf_version_tree **pp;
2192 int version_index;
2193
2194 /* If we aren't going to export this symbol, we don't need
2195 to worry about it. */
2196 if (h->dynindx == -1)
2197 return TRUE;
2198
2199 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2200 sizeof *t);
2201 if (t == NULL)
2202 {
2203 sinfo->failed = TRUE;
2204 return FALSE;
2205 }
2206
2207 t->name = p;
2208 t->name_indx = (unsigned int) -1;
2209 t->used = TRUE;
2210
2211 version_index = 1;
2212 /* Don't count anonymous version tag. */
2213 if (sinfo->info->version_info != NULL
2214 && sinfo->info->version_info->vernum == 0)
2215 version_index = 0;
2216 for (pp = &sinfo->info->version_info;
2217 *pp != NULL;
2218 pp = &(*pp)->next)
2219 ++version_index;
2220 t->vernum = version_index;
2221
2222 *pp = t;
2223
2224 h->verinfo.vertree = t;
2225 }
2226 else if (t == NULL)
2227 {
2228 /* We could not find the version for a symbol when
2229 generating a shared archive. Return an error. */
2230 (*_bfd_error_handler)
2231 (_("%B: version node not found for symbol %s"),
2232 info->output_bfd, h->root.root.string);
2233 bfd_set_error (bfd_error_bad_value);
2234 sinfo->failed = TRUE;
2235 return FALSE;
2236 }
2237 }
2238
2239 /* If we don't have a version for this symbol, see if we can find
2240 something. */
2241 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
2242 {
2243 bfd_boolean hide;
2244
2245 h->verinfo.vertree
2246 = bfd_find_version_for_sym (sinfo->info->version_info,
2247 h->root.root.string, &hide);
2248 if (h->verinfo.vertree != NULL && hide)
2249 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2250 }
2251
2252 return TRUE;
2253 }
2254 \f
2255 /* Read and swap the relocs from the section indicated by SHDR. This
2256 may be either a REL or a RELA section. The relocations are
2257 translated into RELA relocations and stored in INTERNAL_RELOCS,
2258 which should have already been allocated to contain enough space.
2259 The EXTERNAL_RELOCS are a buffer where the external form of the
2260 relocations should be stored.
2261
2262 Returns FALSE if something goes wrong. */
2263
2264 static bfd_boolean
2265 elf_link_read_relocs_from_section (bfd *abfd,
2266 asection *sec,
2267 Elf_Internal_Shdr *shdr,
2268 void *external_relocs,
2269 Elf_Internal_Rela *internal_relocs)
2270 {
2271 const struct elf_backend_data *bed;
2272 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2273 const bfd_byte *erela;
2274 const bfd_byte *erelaend;
2275 Elf_Internal_Rela *irela;
2276 Elf_Internal_Shdr *symtab_hdr;
2277 size_t nsyms;
2278
2279 /* Position ourselves at the start of the section. */
2280 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2281 return FALSE;
2282
2283 /* Read the relocations. */
2284 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2285 return FALSE;
2286
2287 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2288 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2289
2290 bed = get_elf_backend_data (abfd);
2291
2292 /* Convert the external relocations to the internal format. */
2293 if (shdr->sh_entsize == bed->s->sizeof_rel)
2294 swap_in = bed->s->swap_reloc_in;
2295 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2296 swap_in = bed->s->swap_reloca_in;
2297 else
2298 {
2299 bfd_set_error (bfd_error_wrong_format);
2300 return FALSE;
2301 }
2302
2303 erela = (const bfd_byte *) external_relocs;
2304 erelaend = erela + shdr->sh_size;
2305 irela = internal_relocs;
2306 while (erela < erelaend)
2307 {
2308 bfd_vma r_symndx;
2309
2310 (*swap_in) (abfd, erela, irela);
2311 r_symndx = ELF32_R_SYM (irela->r_info);
2312 if (bed->s->arch_size == 64)
2313 r_symndx >>= 24;
2314 if (nsyms > 0)
2315 {
2316 if ((size_t) r_symndx >= nsyms)
2317 {
2318 (*_bfd_error_handler)
2319 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2320 " for offset 0x%lx in section `%A'"),
2321 abfd, sec,
2322 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2323 bfd_set_error (bfd_error_bad_value);
2324 return FALSE;
2325 }
2326 }
2327 else if (r_symndx != STN_UNDEF)
2328 {
2329 (*_bfd_error_handler)
2330 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2331 " when the object file has no symbol table"),
2332 abfd, sec,
2333 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2334 bfd_set_error (bfd_error_bad_value);
2335 return FALSE;
2336 }
2337 irela += bed->s->int_rels_per_ext_rel;
2338 erela += shdr->sh_entsize;
2339 }
2340
2341 return TRUE;
2342 }
2343
2344 /* Read and swap the relocs for a section O. They may have been
2345 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2346 not NULL, they are used as buffers to read into. They are known to
2347 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2348 the return value is allocated using either malloc or bfd_alloc,
2349 according to the KEEP_MEMORY argument. If O has two relocation
2350 sections (both REL and RELA relocations), then the REL_HDR
2351 relocations will appear first in INTERNAL_RELOCS, followed by the
2352 RELA_HDR relocations. */
2353
2354 Elf_Internal_Rela *
2355 _bfd_elf_link_read_relocs (bfd *abfd,
2356 asection *o,
2357 void *external_relocs,
2358 Elf_Internal_Rela *internal_relocs,
2359 bfd_boolean keep_memory)
2360 {
2361 void *alloc1 = NULL;
2362 Elf_Internal_Rela *alloc2 = NULL;
2363 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2364 struct bfd_elf_section_data *esdo = elf_section_data (o);
2365 Elf_Internal_Rela *internal_rela_relocs;
2366
2367 if (esdo->relocs != NULL)
2368 return esdo->relocs;
2369
2370 if (o->reloc_count == 0)
2371 return NULL;
2372
2373 if (internal_relocs == NULL)
2374 {
2375 bfd_size_type size;
2376
2377 size = o->reloc_count;
2378 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2379 if (keep_memory)
2380 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2381 else
2382 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2383 if (internal_relocs == NULL)
2384 goto error_return;
2385 }
2386
2387 if (external_relocs == NULL)
2388 {
2389 bfd_size_type size = 0;
2390
2391 if (esdo->rel.hdr)
2392 size += esdo->rel.hdr->sh_size;
2393 if (esdo->rela.hdr)
2394 size += esdo->rela.hdr->sh_size;
2395
2396 alloc1 = bfd_malloc (size);
2397 if (alloc1 == NULL)
2398 goto error_return;
2399 external_relocs = alloc1;
2400 }
2401
2402 internal_rela_relocs = internal_relocs;
2403 if (esdo->rel.hdr)
2404 {
2405 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2406 external_relocs,
2407 internal_relocs))
2408 goto error_return;
2409 external_relocs = (((bfd_byte *) external_relocs)
2410 + esdo->rel.hdr->sh_size);
2411 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2412 * bed->s->int_rels_per_ext_rel);
2413 }
2414
2415 if (esdo->rela.hdr
2416 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2417 external_relocs,
2418 internal_rela_relocs)))
2419 goto error_return;
2420
2421 /* Cache the results for next time, if we can. */
2422 if (keep_memory)
2423 esdo->relocs = internal_relocs;
2424
2425 if (alloc1 != NULL)
2426 free (alloc1);
2427
2428 /* Don't free alloc2, since if it was allocated we are passing it
2429 back (under the name of internal_relocs). */
2430
2431 return internal_relocs;
2432
2433 error_return:
2434 if (alloc1 != NULL)
2435 free (alloc1);
2436 if (alloc2 != NULL)
2437 {
2438 if (keep_memory)
2439 bfd_release (abfd, alloc2);
2440 else
2441 free (alloc2);
2442 }
2443 return NULL;
2444 }
2445
2446 /* Compute the size of, and allocate space for, REL_HDR which is the
2447 section header for a section containing relocations for O. */
2448
2449 static bfd_boolean
2450 _bfd_elf_link_size_reloc_section (bfd *abfd,
2451 struct bfd_elf_section_reloc_data *reldata)
2452 {
2453 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2454
2455 /* That allows us to calculate the size of the section. */
2456 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2457
2458 /* The contents field must last into write_object_contents, so we
2459 allocate it with bfd_alloc rather than malloc. Also since we
2460 cannot be sure that the contents will actually be filled in,
2461 we zero the allocated space. */
2462 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2463 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2464 return FALSE;
2465
2466 if (reldata->hashes == NULL && reldata->count)
2467 {
2468 struct elf_link_hash_entry **p;
2469
2470 p = ((struct elf_link_hash_entry **)
2471 bfd_zmalloc (reldata->count * sizeof (*p)));
2472 if (p == NULL)
2473 return FALSE;
2474
2475 reldata->hashes = p;
2476 }
2477
2478 return TRUE;
2479 }
2480
2481 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2482 originated from the section given by INPUT_REL_HDR) to the
2483 OUTPUT_BFD. */
2484
2485 bfd_boolean
2486 _bfd_elf_link_output_relocs (bfd *output_bfd,
2487 asection *input_section,
2488 Elf_Internal_Shdr *input_rel_hdr,
2489 Elf_Internal_Rela *internal_relocs,
2490 struct elf_link_hash_entry **rel_hash
2491 ATTRIBUTE_UNUSED)
2492 {
2493 Elf_Internal_Rela *irela;
2494 Elf_Internal_Rela *irelaend;
2495 bfd_byte *erel;
2496 struct bfd_elf_section_reloc_data *output_reldata;
2497 asection *output_section;
2498 const struct elf_backend_data *bed;
2499 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2500 struct bfd_elf_section_data *esdo;
2501
2502 output_section = input_section->output_section;
2503
2504 bed = get_elf_backend_data (output_bfd);
2505 esdo = elf_section_data (output_section);
2506 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2507 {
2508 output_reldata = &esdo->rel;
2509 swap_out = bed->s->swap_reloc_out;
2510 }
2511 else if (esdo->rela.hdr
2512 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2513 {
2514 output_reldata = &esdo->rela;
2515 swap_out = bed->s->swap_reloca_out;
2516 }
2517 else
2518 {
2519 (*_bfd_error_handler)
2520 (_("%B: relocation size mismatch in %B section %A"),
2521 output_bfd, input_section->owner, input_section);
2522 bfd_set_error (bfd_error_wrong_format);
2523 return FALSE;
2524 }
2525
2526 erel = output_reldata->hdr->contents;
2527 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2528 irela = internal_relocs;
2529 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2530 * bed->s->int_rels_per_ext_rel);
2531 while (irela < irelaend)
2532 {
2533 (*swap_out) (output_bfd, irela, erel);
2534 irela += bed->s->int_rels_per_ext_rel;
2535 erel += input_rel_hdr->sh_entsize;
2536 }
2537
2538 /* Bump the counter, so that we know where to add the next set of
2539 relocations. */
2540 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2541
2542 return TRUE;
2543 }
2544 \f
2545 /* Make weak undefined symbols in PIE dynamic. */
2546
2547 bfd_boolean
2548 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2549 struct elf_link_hash_entry *h)
2550 {
2551 if (bfd_link_pie (info)
2552 && h->dynindx == -1
2553 && h->root.type == bfd_link_hash_undefweak)
2554 return bfd_elf_link_record_dynamic_symbol (info, h);
2555
2556 return TRUE;
2557 }
2558
2559 /* Fix up the flags for a symbol. This handles various cases which
2560 can only be fixed after all the input files are seen. This is
2561 currently called by both adjust_dynamic_symbol and
2562 assign_sym_version, which is unnecessary but perhaps more robust in
2563 the face of future changes. */
2564
2565 static bfd_boolean
2566 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2567 struct elf_info_failed *eif)
2568 {
2569 const struct elf_backend_data *bed;
2570
2571 /* If this symbol was mentioned in a non-ELF file, try to set
2572 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2573 permit a non-ELF file to correctly refer to a symbol defined in
2574 an ELF dynamic object. */
2575 if (h->non_elf)
2576 {
2577 while (h->root.type == bfd_link_hash_indirect)
2578 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2579
2580 if (h->root.type != bfd_link_hash_defined
2581 && h->root.type != bfd_link_hash_defweak)
2582 {
2583 h->ref_regular = 1;
2584 h->ref_regular_nonweak = 1;
2585 }
2586 else
2587 {
2588 if (h->root.u.def.section->owner != NULL
2589 && (bfd_get_flavour (h->root.u.def.section->owner)
2590 == bfd_target_elf_flavour))
2591 {
2592 h->ref_regular = 1;
2593 h->ref_regular_nonweak = 1;
2594 }
2595 else
2596 h->def_regular = 1;
2597 }
2598
2599 if (h->dynindx == -1
2600 && (h->def_dynamic
2601 || h->ref_dynamic))
2602 {
2603 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2604 {
2605 eif->failed = TRUE;
2606 return FALSE;
2607 }
2608 }
2609 }
2610 else
2611 {
2612 /* Unfortunately, NON_ELF is only correct if the symbol
2613 was first seen in a non-ELF file. Fortunately, if the symbol
2614 was first seen in an ELF file, we're probably OK unless the
2615 symbol was defined in a non-ELF file. Catch that case here.
2616 FIXME: We're still in trouble if the symbol was first seen in
2617 a dynamic object, and then later in a non-ELF regular object. */
2618 if ((h->root.type == bfd_link_hash_defined
2619 || h->root.type == bfd_link_hash_defweak)
2620 && !h->def_regular
2621 && (h->root.u.def.section->owner != NULL
2622 ? (bfd_get_flavour (h->root.u.def.section->owner)
2623 != bfd_target_elf_flavour)
2624 : (bfd_is_abs_section (h->root.u.def.section)
2625 && !h->def_dynamic)))
2626 h->def_regular = 1;
2627 }
2628
2629 /* Backend specific symbol fixup. */
2630 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2631 if (bed->elf_backend_fixup_symbol
2632 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2633 return FALSE;
2634
2635 /* If this is a final link, and the symbol was defined as a common
2636 symbol in a regular object file, and there was no definition in
2637 any dynamic object, then the linker will have allocated space for
2638 the symbol in a common section but the DEF_REGULAR
2639 flag will not have been set. */
2640 if (h->root.type == bfd_link_hash_defined
2641 && !h->def_regular
2642 && h->ref_regular
2643 && !h->def_dynamic
2644 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2645 h->def_regular = 1;
2646
2647 /* If -Bsymbolic was used (which means to bind references to global
2648 symbols to the definition within the shared object), and this
2649 symbol was defined in a regular object, then it actually doesn't
2650 need a PLT entry. Likewise, if the symbol has non-default
2651 visibility. If the symbol has hidden or internal visibility, we
2652 will force it local. */
2653 if (h->needs_plt
2654 && bfd_link_pic (eif->info)
2655 && is_elf_hash_table (eif->info->hash)
2656 && (SYMBOLIC_BIND (eif->info, h)
2657 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2658 && h->def_regular)
2659 {
2660 bfd_boolean force_local;
2661
2662 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2663 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2664 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2665 }
2666
2667 /* If a weak undefined symbol has non-default visibility, we also
2668 hide it from the dynamic linker. */
2669 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2670 && h->root.type == bfd_link_hash_undefweak)
2671 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2672
2673 /* If this is a weak defined symbol in a dynamic object, and we know
2674 the real definition in the dynamic object, copy interesting flags
2675 over to the real definition. */
2676 if (h->u.weakdef != NULL)
2677 {
2678 /* If the real definition is defined by a regular object file,
2679 don't do anything special. See the longer description in
2680 _bfd_elf_adjust_dynamic_symbol, below. */
2681 if (h->u.weakdef->def_regular)
2682 h->u.weakdef = NULL;
2683 else
2684 {
2685 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2686
2687 while (h->root.type == bfd_link_hash_indirect)
2688 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2689
2690 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2691 || h->root.type == bfd_link_hash_defweak);
2692 BFD_ASSERT (weakdef->def_dynamic);
2693 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2694 || weakdef->root.type == bfd_link_hash_defweak);
2695 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2696 }
2697 }
2698
2699 return TRUE;
2700 }
2701
2702 /* Make the backend pick a good value for a dynamic symbol. This is
2703 called via elf_link_hash_traverse, and also calls itself
2704 recursively. */
2705
2706 static bfd_boolean
2707 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2708 {
2709 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2710 bfd *dynobj;
2711 const struct elf_backend_data *bed;
2712
2713 if (! is_elf_hash_table (eif->info->hash))
2714 return FALSE;
2715
2716 /* Ignore indirect symbols. These are added by the versioning code. */
2717 if (h->root.type == bfd_link_hash_indirect)
2718 return TRUE;
2719
2720 /* Fix the symbol flags. */
2721 if (! _bfd_elf_fix_symbol_flags (h, eif))
2722 return FALSE;
2723
2724 /* If this symbol does not require a PLT entry, and it is not
2725 defined by a dynamic object, or is not referenced by a regular
2726 object, ignore it. We do have to handle a weak defined symbol,
2727 even if no regular object refers to it, if we decided to add it
2728 to the dynamic symbol table. FIXME: Do we normally need to worry
2729 about symbols which are defined by one dynamic object and
2730 referenced by another one? */
2731 if (!h->needs_plt
2732 && h->type != STT_GNU_IFUNC
2733 && (h->def_regular
2734 || !h->def_dynamic
2735 || (!h->ref_regular
2736 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2737 {
2738 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2739 return TRUE;
2740 }
2741
2742 /* If we've already adjusted this symbol, don't do it again. This
2743 can happen via a recursive call. */
2744 if (h->dynamic_adjusted)
2745 return TRUE;
2746
2747 /* Don't look at this symbol again. Note that we must set this
2748 after checking the above conditions, because we may look at a
2749 symbol once, decide not to do anything, and then get called
2750 recursively later after REF_REGULAR is set below. */
2751 h->dynamic_adjusted = 1;
2752
2753 /* If this is a weak definition, and we know a real definition, and
2754 the real symbol is not itself defined by a regular object file,
2755 then get a good value for the real definition. We handle the
2756 real symbol first, for the convenience of the backend routine.
2757
2758 Note that there is a confusing case here. If the real definition
2759 is defined by a regular object file, we don't get the real symbol
2760 from the dynamic object, but we do get the weak symbol. If the
2761 processor backend uses a COPY reloc, then if some routine in the
2762 dynamic object changes the real symbol, we will not see that
2763 change in the corresponding weak symbol. This is the way other
2764 ELF linkers work as well, and seems to be a result of the shared
2765 library model.
2766
2767 I will clarify this issue. Most SVR4 shared libraries define the
2768 variable _timezone and define timezone as a weak synonym. The
2769 tzset call changes _timezone. If you write
2770 extern int timezone;
2771 int _timezone = 5;
2772 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2773 you might expect that, since timezone is a synonym for _timezone,
2774 the same number will print both times. However, if the processor
2775 backend uses a COPY reloc, then actually timezone will be copied
2776 into your process image, and, since you define _timezone
2777 yourself, _timezone will not. Thus timezone and _timezone will
2778 wind up at different memory locations. The tzset call will set
2779 _timezone, leaving timezone unchanged. */
2780
2781 if (h->u.weakdef != NULL)
2782 {
2783 /* If we get to this point, there is an implicit reference to
2784 H->U.WEAKDEF by a regular object file via the weak symbol H. */
2785 h->u.weakdef->ref_regular = 1;
2786
2787 /* Ensure that the backend adjust_dynamic_symbol function sees
2788 H->U.WEAKDEF before H by recursively calling ourselves. */
2789 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2790 return FALSE;
2791 }
2792
2793 /* If a symbol has no type and no size and does not require a PLT
2794 entry, then we are probably about to do the wrong thing here: we
2795 are probably going to create a COPY reloc for an empty object.
2796 This case can arise when a shared object is built with assembly
2797 code, and the assembly code fails to set the symbol type. */
2798 if (h->size == 0
2799 && h->type == STT_NOTYPE
2800 && !h->needs_plt)
2801 (*_bfd_error_handler)
2802 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2803 h->root.root.string);
2804
2805 dynobj = elf_hash_table (eif->info)->dynobj;
2806 bed = get_elf_backend_data (dynobj);
2807
2808 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2809 {
2810 eif->failed = TRUE;
2811 return FALSE;
2812 }
2813
2814 return TRUE;
2815 }
2816
2817 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2818 DYNBSS. */
2819
2820 bfd_boolean
2821 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2822 struct elf_link_hash_entry *h,
2823 asection *dynbss)
2824 {
2825 unsigned int power_of_two;
2826 bfd_vma mask;
2827 asection *sec = h->root.u.def.section;
2828
2829 /* The section aligment of definition is the maximum alignment
2830 requirement of symbols defined in the section. Since we don't
2831 know the symbol alignment requirement, we start with the
2832 maximum alignment and check low bits of the symbol address
2833 for the minimum alignment. */
2834 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2835 mask = ((bfd_vma) 1 << power_of_two) - 1;
2836 while ((h->root.u.def.value & mask) != 0)
2837 {
2838 mask >>= 1;
2839 --power_of_two;
2840 }
2841
2842 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2843 dynbss))
2844 {
2845 /* Adjust the section alignment if needed. */
2846 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2847 power_of_two))
2848 return FALSE;
2849 }
2850
2851 /* We make sure that the symbol will be aligned properly. */
2852 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2853
2854 /* Define the symbol as being at this point in DYNBSS. */
2855 h->root.u.def.section = dynbss;
2856 h->root.u.def.value = dynbss->size;
2857
2858 /* Increment the size of DYNBSS to make room for the symbol. */
2859 dynbss->size += h->size;
2860
2861 /* No error if extern_protected_data is true. */
2862 if (h->protected_def
2863 && (!info->extern_protected_data
2864 || (info->extern_protected_data < 0
2865 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
2866 info->callbacks->einfo
2867 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2868 h->root.root.string);
2869
2870 return TRUE;
2871 }
2872
2873 /* Adjust all external symbols pointing into SEC_MERGE sections
2874 to reflect the object merging within the sections. */
2875
2876 static bfd_boolean
2877 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2878 {
2879 asection *sec;
2880
2881 if ((h->root.type == bfd_link_hash_defined
2882 || h->root.type == bfd_link_hash_defweak)
2883 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2884 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
2885 {
2886 bfd *output_bfd = (bfd *) data;
2887
2888 h->root.u.def.value =
2889 _bfd_merged_section_offset (output_bfd,
2890 &h->root.u.def.section,
2891 elf_section_data (sec)->sec_info,
2892 h->root.u.def.value);
2893 }
2894
2895 return TRUE;
2896 }
2897
2898 /* Returns false if the symbol referred to by H should be considered
2899 to resolve local to the current module, and true if it should be
2900 considered to bind dynamically. */
2901
2902 bfd_boolean
2903 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2904 struct bfd_link_info *info,
2905 bfd_boolean not_local_protected)
2906 {
2907 bfd_boolean binding_stays_local_p;
2908 const struct elf_backend_data *bed;
2909 struct elf_link_hash_table *hash_table;
2910
2911 if (h == NULL)
2912 return FALSE;
2913
2914 while (h->root.type == bfd_link_hash_indirect
2915 || h->root.type == bfd_link_hash_warning)
2916 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2917
2918 /* If it was forced local, then clearly it's not dynamic. */
2919 if (h->dynindx == -1)
2920 return FALSE;
2921 if (h->forced_local)
2922 return FALSE;
2923
2924 /* Identify the cases where name binding rules say that a
2925 visible symbol resolves locally. */
2926 binding_stays_local_p = (bfd_link_executable (info)
2927 || SYMBOLIC_BIND (info, h));
2928
2929 switch (ELF_ST_VISIBILITY (h->other))
2930 {
2931 case STV_INTERNAL:
2932 case STV_HIDDEN:
2933 return FALSE;
2934
2935 case STV_PROTECTED:
2936 hash_table = elf_hash_table (info);
2937 if (!is_elf_hash_table (hash_table))
2938 return FALSE;
2939
2940 bed = get_elf_backend_data (hash_table->dynobj);
2941
2942 /* Proper resolution for function pointer equality may require
2943 that these symbols perhaps be resolved dynamically, even though
2944 we should be resolving them to the current module. */
2945 if (!not_local_protected || !bed->is_function_type (h->type))
2946 binding_stays_local_p = TRUE;
2947 break;
2948
2949 default:
2950 break;
2951 }
2952
2953 /* If it isn't defined locally, then clearly it's dynamic. */
2954 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2955 return TRUE;
2956
2957 /* Otherwise, the symbol is dynamic if binding rules don't tell
2958 us that it remains local. */
2959 return !binding_stays_local_p;
2960 }
2961
2962 /* Return true if the symbol referred to by H should be considered
2963 to resolve local to the current module, and false otherwise. Differs
2964 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2965 undefined symbols. The two functions are virtually identical except
2966 for the place where forced_local and dynindx == -1 are tested. If
2967 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2968 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2969 the symbol is local only for defined symbols.
2970 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2971 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2972 treatment of undefined weak symbols. For those that do not make
2973 undefined weak symbols dynamic, both functions may return false. */
2974
2975 bfd_boolean
2976 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2977 struct bfd_link_info *info,
2978 bfd_boolean local_protected)
2979 {
2980 const struct elf_backend_data *bed;
2981 struct elf_link_hash_table *hash_table;
2982
2983 /* If it's a local sym, of course we resolve locally. */
2984 if (h == NULL)
2985 return TRUE;
2986
2987 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2988 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2989 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2990 return TRUE;
2991
2992 /* Common symbols that become definitions don't get the DEF_REGULAR
2993 flag set, so test it first, and don't bail out. */
2994 if (ELF_COMMON_DEF_P (h))
2995 /* Do nothing. */;
2996 /* If we don't have a definition in a regular file, then we can't
2997 resolve locally. The sym is either undefined or dynamic. */
2998 else if (!h->def_regular)
2999 return FALSE;
3000
3001 /* Forced local symbols resolve locally. */
3002 if (h->forced_local)
3003 return TRUE;
3004
3005 /* As do non-dynamic symbols. */
3006 if (h->dynindx == -1)
3007 return TRUE;
3008
3009 /* At this point, we know the symbol is defined and dynamic. In an
3010 executable it must resolve locally, likewise when building symbolic
3011 shared libraries. */
3012 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3013 return TRUE;
3014
3015 /* Now deal with defined dynamic symbols in shared libraries. Ones
3016 with default visibility might not resolve locally. */
3017 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3018 return FALSE;
3019
3020 hash_table = elf_hash_table (info);
3021 if (!is_elf_hash_table (hash_table))
3022 return TRUE;
3023
3024 bed = get_elf_backend_data (hash_table->dynobj);
3025
3026 /* If extern_protected_data is false, STV_PROTECTED non-function
3027 symbols are local. */
3028 if ((!info->extern_protected_data
3029 || (info->extern_protected_data < 0
3030 && !bed->extern_protected_data))
3031 && !bed->is_function_type (h->type))
3032 return TRUE;
3033
3034 /* Function pointer equality tests may require that STV_PROTECTED
3035 symbols be treated as dynamic symbols. If the address of a
3036 function not defined in an executable is set to that function's
3037 plt entry in the executable, then the address of the function in
3038 a shared library must also be the plt entry in the executable. */
3039 return local_protected;
3040 }
3041
3042 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3043 aligned. Returns the first TLS output section. */
3044
3045 struct bfd_section *
3046 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3047 {
3048 struct bfd_section *sec, *tls;
3049 unsigned int align = 0;
3050
3051 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3052 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3053 break;
3054 tls = sec;
3055
3056 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3057 if (sec->alignment_power > align)
3058 align = sec->alignment_power;
3059
3060 elf_hash_table (info)->tls_sec = tls;
3061
3062 /* Ensure the alignment of the first section is the largest alignment,
3063 so that the tls segment starts aligned. */
3064 if (tls != NULL)
3065 tls->alignment_power = align;
3066
3067 return tls;
3068 }
3069
3070 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3071 static bfd_boolean
3072 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3073 Elf_Internal_Sym *sym)
3074 {
3075 const struct elf_backend_data *bed;
3076
3077 /* Local symbols do not count, but target specific ones might. */
3078 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3079 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3080 return FALSE;
3081
3082 bed = get_elf_backend_data (abfd);
3083 /* Function symbols do not count. */
3084 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3085 return FALSE;
3086
3087 /* If the section is undefined, then so is the symbol. */
3088 if (sym->st_shndx == SHN_UNDEF)
3089 return FALSE;
3090
3091 /* If the symbol is defined in the common section, then
3092 it is a common definition and so does not count. */
3093 if (bed->common_definition (sym))
3094 return FALSE;
3095
3096 /* If the symbol is in a target specific section then we
3097 must rely upon the backend to tell us what it is. */
3098 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3099 /* FIXME - this function is not coded yet:
3100
3101 return _bfd_is_global_symbol_definition (abfd, sym);
3102
3103 Instead for now assume that the definition is not global,
3104 Even if this is wrong, at least the linker will behave
3105 in the same way that it used to do. */
3106 return FALSE;
3107
3108 return TRUE;
3109 }
3110
3111 /* Search the symbol table of the archive element of the archive ABFD
3112 whose archive map contains a mention of SYMDEF, and determine if
3113 the symbol is defined in this element. */
3114 static bfd_boolean
3115 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3116 {
3117 Elf_Internal_Shdr * hdr;
3118 size_t symcount;
3119 size_t extsymcount;
3120 size_t extsymoff;
3121 Elf_Internal_Sym *isymbuf;
3122 Elf_Internal_Sym *isym;
3123 Elf_Internal_Sym *isymend;
3124 bfd_boolean result;
3125
3126 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3127 if (abfd == NULL)
3128 return FALSE;
3129
3130 if (! bfd_check_format (abfd, bfd_object))
3131 return FALSE;
3132
3133 /* Select the appropriate symbol table. If we don't know if the
3134 object file is an IR object, give linker LTO plugin a chance to
3135 get the correct symbol table. */
3136 if (abfd->plugin_format == bfd_plugin_yes
3137 #if BFD_SUPPORTS_PLUGINS
3138 || (abfd->plugin_format == bfd_plugin_unknown
3139 && bfd_link_plugin_object_p (abfd))
3140 #endif
3141 )
3142 {
3143 /* Use the IR symbol table if the object has been claimed by
3144 plugin. */
3145 abfd = abfd->plugin_dummy_bfd;
3146 hdr = &elf_tdata (abfd)->symtab_hdr;
3147 }
3148 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3149 hdr = &elf_tdata (abfd)->symtab_hdr;
3150 else
3151 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3152
3153 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3154
3155 /* The sh_info field of the symtab header tells us where the
3156 external symbols start. We don't care about the local symbols. */
3157 if (elf_bad_symtab (abfd))
3158 {
3159 extsymcount = symcount;
3160 extsymoff = 0;
3161 }
3162 else
3163 {
3164 extsymcount = symcount - hdr->sh_info;
3165 extsymoff = hdr->sh_info;
3166 }
3167
3168 if (extsymcount == 0)
3169 return FALSE;
3170
3171 /* Read in the symbol table. */
3172 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3173 NULL, NULL, NULL);
3174 if (isymbuf == NULL)
3175 return FALSE;
3176
3177 /* Scan the symbol table looking for SYMDEF. */
3178 result = FALSE;
3179 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3180 {
3181 const char *name;
3182
3183 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3184 isym->st_name);
3185 if (name == NULL)
3186 break;
3187
3188 if (strcmp (name, symdef->name) == 0)
3189 {
3190 result = is_global_data_symbol_definition (abfd, isym);
3191 break;
3192 }
3193 }
3194
3195 free (isymbuf);
3196
3197 return result;
3198 }
3199 \f
3200 /* Add an entry to the .dynamic table. */
3201
3202 bfd_boolean
3203 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3204 bfd_vma tag,
3205 bfd_vma val)
3206 {
3207 struct elf_link_hash_table *hash_table;
3208 const struct elf_backend_data *bed;
3209 asection *s;
3210 bfd_size_type newsize;
3211 bfd_byte *newcontents;
3212 Elf_Internal_Dyn dyn;
3213
3214 hash_table = elf_hash_table (info);
3215 if (! is_elf_hash_table (hash_table))
3216 return FALSE;
3217
3218 bed = get_elf_backend_data (hash_table->dynobj);
3219 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3220 BFD_ASSERT (s != NULL);
3221
3222 newsize = s->size + bed->s->sizeof_dyn;
3223 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3224 if (newcontents == NULL)
3225 return FALSE;
3226
3227 dyn.d_tag = tag;
3228 dyn.d_un.d_val = val;
3229 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3230
3231 s->size = newsize;
3232 s->contents = newcontents;
3233
3234 return TRUE;
3235 }
3236
3237 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3238 otherwise just check whether one already exists. Returns -1 on error,
3239 1 if a DT_NEEDED tag already exists, and 0 on success. */
3240
3241 static int
3242 elf_add_dt_needed_tag (bfd *abfd,
3243 struct bfd_link_info *info,
3244 const char *soname,
3245 bfd_boolean do_it)
3246 {
3247 struct elf_link_hash_table *hash_table;
3248 size_t strindex;
3249
3250 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3251 return -1;
3252
3253 hash_table = elf_hash_table (info);
3254 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3255 if (strindex == (size_t) -1)
3256 return -1;
3257
3258 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3259 {
3260 asection *sdyn;
3261 const struct elf_backend_data *bed;
3262 bfd_byte *extdyn;
3263
3264 bed = get_elf_backend_data (hash_table->dynobj);
3265 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3266 if (sdyn != NULL)
3267 for (extdyn = sdyn->contents;
3268 extdyn < sdyn->contents + sdyn->size;
3269 extdyn += bed->s->sizeof_dyn)
3270 {
3271 Elf_Internal_Dyn dyn;
3272
3273 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3274 if (dyn.d_tag == DT_NEEDED
3275 && dyn.d_un.d_val == strindex)
3276 {
3277 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3278 return 1;
3279 }
3280 }
3281 }
3282
3283 if (do_it)
3284 {
3285 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3286 return -1;
3287
3288 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3289 return -1;
3290 }
3291 else
3292 /* We were just checking for existence of the tag. */
3293 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3294
3295 return 0;
3296 }
3297
3298 /* Return true if SONAME is on the needed list between NEEDED and STOP
3299 (or the end of list if STOP is NULL), and needed by a library that
3300 will be loaded. */
3301
3302 static bfd_boolean
3303 on_needed_list (const char *soname,
3304 struct bfd_link_needed_list *needed,
3305 struct bfd_link_needed_list *stop)
3306 {
3307 struct bfd_link_needed_list *look;
3308 for (look = needed; look != stop; look = look->next)
3309 if (strcmp (soname, look->name) == 0
3310 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3311 /* If needed by a library that itself is not directly
3312 needed, recursively check whether that library is
3313 indirectly needed. Since we add DT_NEEDED entries to
3314 the end of the list, library dependencies appear after
3315 the library. Therefore search prior to the current
3316 LOOK, preventing possible infinite recursion. */
3317 || on_needed_list (elf_dt_name (look->by), needed, look)))
3318 return TRUE;
3319
3320 return FALSE;
3321 }
3322
3323 /* Sort symbol by value, section, and size. */
3324 static int
3325 elf_sort_symbol (const void *arg1, const void *arg2)
3326 {
3327 const struct elf_link_hash_entry *h1;
3328 const struct elf_link_hash_entry *h2;
3329 bfd_signed_vma vdiff;
3330
3331 h1 = *(const struct elf_link_hash_entry **) arg1;
3332 h2 = *(const struct elf_link_hash_entry **) arg2;
3333 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3334 if (vdiff != 0)
3335 return vdiff > 0 ? 1 : -1;
3336 else
3337 {
3338 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3339 if (sdiff != 0)
3340 return sdiff > 0 ? 1 : -1;
3341 }
3342 vdiff = h1->size - h2->size;
3343 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3344 }
3345
3346 /* This function is used to adjust offsets into .dynstr for
3347 dynamic symbols. This is called via elf_link_hash_traverse. */
3348
3349 static bfd_boolean
3350 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3351 {
3352 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3353
3354 if (h->dynindx != -1)
3355 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3356 return TRUE;
3357 }
3358
3359 /* Assign string offsets in .dynstr, update all structures referencing
3360 them. */
3361
3362 static bfd_boolean
3363 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3364 {
3365 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3366 struct elf_link_local_dynamic_entry *entry;
3367 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3368 bfd *dynobj = hash_table->dynobj;
3369 asection *sdyn;
3370 bfd_size_type size;
3371 const struct elf_backend_data *bed;
3372 bfd_byte *extdyn;
3373
3374 _bfd_elf_strtab_finalize (dynstr);
3375 size = _bfd_elf_strtab_size (dynstr);
3376
3377 bed = get_elf_backend_data (dynobj);
3378 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3379 BFD_ASSERT (sdyn != NULL);
3380
3381 /* Update all .dynamic entries referencing .dynstr strings. */
3382 for (extdyn = sdyn->contents;
3383 extdyn < sdyn->contents + sdyn->size;
3384 extdyn += bed->s->sizeof_dyn)
3385 {
3386 Elf_Internal_Dyn dyn;
3387
3388 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3389 switch (dyn.d_tag)
3390 {
3391 case DT_STRSZ:
3392 dyn.d_un.d_val = size;
3393 break;
3394 case DT_NEEDED:
3395 case DT_SONAME:
3396 case DT_RPATH:
3397 case DT_RUNPATH:
3398 case DT_FILTER:
3399 case DT_AUXILIARY:
3400 case DT_AUDIT:
3401 case DT_DEPAUDIT:
3402 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3403 break;
3404 default:
3405 continue;
3406 }
3407 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3408 }
3409
3410 /* Now update local dynamic symbols. */
3411 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3412 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3413 entry->isym.st_name);
3414
3415 /* And the rest of dynamic symbols. */
3416 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3417
3418 /* Adjust version definitions. */
3419 if (elf_tdata (output_bfd)->cverdefs)
3420 {
3421 asection *s;
3422 bfd_byte *p;
3423 size_t i;
3424 Elf_Internal_Verdef def;
3425 Elf_Internal_Verdaux defaux;
3426
3427 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3428 p = s->contents;
3429 do
3430 {
3431 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3432 &def);
3433 p += sizeof (Elf_External_Verdef);
3434 if (def.vd_aux != sizeof (Elf_External_Verdef))
3435 continue;
3436 for (i = 0; i < def.vd_cnt; ++i)
3437 {
3438 _bfd_elf_swap_verdaux_in (output_bfd,
3439 (Elf_External_Verdaux *) p, &defaux);
3440 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3441 defaux.vda_name);
3442 _bfd_elf_swap_verdaux_out (output_bfd,
3443 &defaux, (Elf_External_Verdaux *) p);
3444 p += sizeof (Elf_External_Verdaux);
3445 }
3446 }
3447 while (def.vd_next);
3448 }
3449
3450 /* Adjust version references. */
3451 if (elf_tdata (output_bfd)->verref)
3452 {
3453 asection *s;
3454 bfd_byte *p;
3455 size_t i;
3456 Elf_Internal_Verneed need;
3457 Elf_Internal_Vernaux needaux;
3458
3459 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3460 p = s->contents;
3461 do
3462 {
3463 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3464 &need);
3465 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3466 _bfd_elf_swap_verneed_out (output_bfd, &need,
3467 (Elf_External_Verneed *) p);
3468 p += sizeof (Elf_External_Verneed);
3469 for (i = 0; i < need.vn_cnt; ++i)
3470 {
3471 _bfd_elf_swap_vernaux_in (output_bfd,
3472 (Elf_External_Vernaux *) p, &needaux);
3473 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3474 needaux.vna_name);
3475 _bfd_elf_swap_vernaux_out (output_bfd,
3476 &needaux,
3477 (Elf_External_Vernaux *) p);
3478 p += sizeof (Elf_External_Vernaux);
3479 }
3480 }
3481 while (need.vn_next);
3482 }
3483
3484 return TRUE;
3485 }
3486 \f
3487 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3488 The default is to only match when the INPUT and OUTPUT are exactly
3489 the same target. */
3490
3491 bfd_boolean
3492 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3493 const bfd_target *output)
3494 {
3495 return input == output;
3496 }
3497
3498 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3499 This version is used when different targets for the same architecture
3500 are virtually identical. */
3501
3502 bfd_boolean
3503 _bfd_elf_relocs_compatible (const bfd_target *input,
3504 const bfd_target *output)
3505 {
3506 const struct elf_backend_data *obed, *ibed;
3507
3508 if (input == output)
3509 return TRUE;
3510
3511 ibed = xvec_get_elf_backend_data (input);
3512 obed = xvec_get_elf_backend_data (output);
3513
3514 if (ibed->arch != obed->arch)
3515 return FALSE;
3516
3517 /* If both backends are using this function, deem them compatible. */
3518 return ibed->relocs_compatible == obed->relocs_compatible;
3519 }
3520
3521 /* Make a special call to the linker "notice" function to tell it that
3522 we are about to handle an as-needed lib, or have finished
3523 processing the lib. */
3524
3525 bfd_boolean
3526 _bfd_elf_notice_as_needed (bfd *ibfd,
3527 struct bfd_link_info *info,
3528 enum notice_asneeded_action act)
3529 {
3530 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3531 }
3532
3533 /* Check relocations an ELF object file. */
3534
3535 bfd_boolean
3536 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3537 {
3538 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3539 struct elf_link_hash_table *htab = elf_hash_table (info);
3540
3541 /* If this object is the same format as the output object, and it is
3542 not a shared library, then let the backend look through the
3543 relocs.
3544
3545 This is required to build global offset table entries and to
3546 arrange for dynamic relocs. It is not required for the
3547 particular common case of linking non PIC code, even when linking
3548 against shared libraries, but unfortunately there is no way of
3549 knowing whether an object file has been compiled PIC or not.
3550 Looking through the relocs is not particularly time consuming.
3551 The problem is that we must either (1) keep the relocs in memory,
3552 which causes the linker to require additional runtime memory or
3553 (2) read the relocs twice from the input file, which wastes time.
3554 This would be a good case for using mmap.
3555
3556 I have no idea how to handle linking PIC code into a file of a
3557 different format. It probably can't be done. */
3558 if ((abfd->flags & DYNAMIC) == 0
3559 && is_elf_hash_table (htab)
3560 && bed->check_relocs != NULL
3561 && elf_object_id (abfd) == elf_hash_table_id (htab)
3562 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3563 {
3564 asection *o;
3565
3566 for (o = abfd->sections; o != NULL; o = o->next)
3567 {
3568 Elf_Internal_Rela *internal_relocs;
3569 bfd_boolean ok;
3570
3571 /* Don't check relocations in excluded sections. */
3572 if ((o->flags & SEC_RELOC) == 0
3573 || (o->flags & SEC_EXCLUDE) != 0
3574 || o->reloc_count == 0
3575 || ((info->strip == strip_all || info->strip == strip_debugger)
3576 && (o->flags & SEC_DEBUGGING) != 0)
3577 || bfd_is_abs_section (o->output_section))
3578 continue;
3579
3580 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3581 info->keep_memory);
3582 if (internal_relocs == NULL)
3583 return FALSE;
3584
3585 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3586
3587 if (elf_section_data (o)->relocs != internal_relocs)
3588 free (internal_relocs);
3589
3590 if (! ok)
3591 return FALSE;
3592 }
3593 }
3594
3595 return TRUE;
3596 }
3597
3598 /* Add symbols from an ELF object file to the linker hash table. */
3599
3600 static bfd_boolean
3601 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3602 {
3603 Elf_Internal_Ehdr *ehdr;
3604 Elf_Internal_Shdr *hdr;
3605 size_t symcount;
3606 size_t extsymcount;
3607 size_t extsymoff;
3608 struct elf_link_hash_entry **sym_hash;
3609 bfd_boolean dynamic;
3610 Elf_External_Versym *extversym = NULL;
3611 Elf_External_Versym *ever;
3612 struct elf_link_hash_entry *weaks;
3613 struct elf_link_hash_entry **nondeflt_vers = NULL;
3614 size_t nondeflt_vers_cnt = 0;
3615 Elf_Internal_Sym *isymbuf = NULL;
3616 Elf_Internal_Sym *isym;
3617 Elf_Internal_Sym *isymend;
3618 const struct elf_backend_data *bed;
3619 bfd_boolean add_needed;
3620 struct elf_link_hash_table *htab;
3621 bfd_size_type amt;
3622 void *alloc_mark = NULL;
3623 struct bfd_hash_entry **old_table = NULL;
3624 unsigned int old_size = 0;
3625 unsigned int old_count = 0;
3626 void *old_tab = NULL;
3627 void *old_ent;
3628 struct bfd_link_hash_entry *old_undefs = NULL;
3629 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3630 void *old_strtab = NULL;
3631 size_t tabsize = 0;
3632 asection *s;
3633 bfd_boolean just_syms;
3634
3635 htab = elf_hash_table (info);
3636 bed = get_elf_backend_data (abfd);
3637
3638 if ((abfd->flags & DYNAMIC) == 0)
3639 dynamic = FALSE;
3640 else
3641 {
3642 dynamic = TRUE;
3643
3644 /* You can't use -r against a dynamic object. Also, there's no
3645 hope of using a dynamic object which does not exactly match
3646 the format of the output file. */
3647 if (bfd_link_relocatable (info)
3648 || !is_elf_hash_table (htab)
3649 || info->output_bfd->xvec != abfd->xvec)
3650 {
3651 if (bfd_link_relocatable (info))
3652 bfd_set_error (bfd_error_invalid_operation);
3653 else
3654 bfd_set_error (bfd_error_wrong_format);
3655 goto error_return;
3656 }
3657 }
3658
3659 ehdr = elf_elfheader (abfd);
3660 if (info->warn_alternate_em
3661 && bed->elf_machine_code != ehdr->e_machine
3662 && ((bed->elf_machine_alt1 != 0
3663 && ehdr->e_machine == bed->elf_machine_alt1)
3664 || (bed->elf_machine_alt2 != 0
3665 && ehdr->e_machine == bed->elf_machine_alt2)))
3666 info->callbacks->einfo
3667 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3668 ehdr->e_machine, abfd, bed->elf_machine_code);
3669
3670 /* As a GNU extension, any input sections which are named
3671 .gnu.warning.SYMBOL are treated as warning symbols for the given
3672 symbol. This differs from .gnu.warning sections, which generate
3673 warnings when they are included in an output file. */
3674 /* PR 12761: Also generate this warning when building shared libraries. */
3675 for (s = abfd->sections; s != NULL; s = s->next)
3676 {
3677 const char *name;
3678
3679 name = bfd_get_section_name (abfd, s);
3680 if (CONST_STRNEQ (name, ".gnu.warning."))
3681 {
3682 char *msg;
3683 bfd_size_type sz;
3684
3685 name += sizeof ".gnu.warning." - 1;
3686
3687 /* If this is a shared object, then look up the symbol
3688 in the hash table. If it is there, and it is already
3689 been defined, then we will not be using the entry
3690 from this shared object, so we don't need to warn.
3691 FIXME: If we see the definition in a regular object
3692 later on, we will warn, but we shouldn't. The only
3693 fix is to keep track of what warnings we are supposed
3694 to emit, and then handle them all at the end of the
3695 link. */
3696 if (dynamic)
3697 {
3698 struct elf_link_hash_entry *h;
3699
3700 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3701
3702 /* FIXME: What about bfd_link_hash_common? */
3703 if (h != NULL
3704 && (h->root.type == bfd_link_hash_defined
3705 || h->root.type == bfd_link_hash_defweak))
3706 continue;
3707 }
3708
3709 sz = s->size;
3710 msg = (char *) bfd_alloc (abfd, sz + 1);
3711 if (msg == NULL)
3712 goto error_return;
3713
3714 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3715 goto error_return;
3716
3717 msg[sz] = '\0';
3718
3719 if (! (_bfd_generic_link_add_one_symbol
3720 (info, abfd, name, BSF_WARNING, s, 0, msg,
3721 FALSE, bed->collect, NULL)))
3722 goto error_return;
3723
3724 if (bfd_link_executable (info))
3725 {
3726 /* Clobber the section size so that the warning does
3727 not get copied into the output file. */
3728 s->size = 0;
3729
3730 /* Also set SEC_EXCLUDE, so that symbols defined in
3731 the warning section don't get copied to the output. */
3732 s->flags |= SEC_EXCLUDE;
3733 }
3734 }
3735 }
3736
3737 just_syms = ((s = abfd->sections) != NULL
3738 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3739
3740 add_needed = TRUE;
3741 if (! dynamic)
3742 {
3743 /* If we are creating a shared library, create all the dynamic
3744 sections immediately. We need to attach them to something,
3745 so we attach them to this BFD, provided it is the right
3746 format and is not from ld --just-symbols. Always create the
3747 dynamic sections for -E/--dynamic-list. FIXME: If there
3748 are no input BFD's of the same format as the output, we can't
3749 make a shared library. */
3750 if (!just_syms
3751 && (bfd_link_pic (info)
3752 || (!bfd_link_relocatable (info)
3753 && (info->export_dynamic || info->dynamic)))
3754 && is_elf_hash_table (htab)
3755 && info->output_bfd->xvec == abfd->xvec
3756 && !htab->dynamic_sections_created)
3757 {
3758 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3759 goto error_return;
3760 }
3761 }
3762 else if (!is_elf_hash_table (htab))
3763 goto error_return;
3764 else
3765 {
3766 const char *soname = NULL;
3767 char *audit = NULL;
3768 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3769 int ret;
3770
3771 /* ld --just-symbols and dynamic objects don't mix very well.
3772 ld shouldn't allow it. */
3773 if (just_syms)
3774 abort ();
3775
3776 /* If this dynamic lib was specified on the command line with
3777 --as-needed in effect, then we don't want to add a DT_NEEDED
3778 tag unless the lib is actually used. Similary for libs brought
3779 in by another lib's DT_NEEDED. When --no-add-needed is used
3780 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3781 any dynamic library in DT_NEEDED tags in the dynamic lib at
3782 all. */
3783 add_needed = (elf_dyn_lib_class (abfd)
3784 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3785 | DYN_NO_NEEDED)) == 0;
3786
3787 s = bfd_get_section_by_name (abfd, ".dynamic");
3788 if (s != NULL)
3789 {
3790 bfd_byte *dynbuf;
3791 bfd_byte *extdyn;
3792 unsigned int elfsec;
3793 unsigned long shlink;
3794
3795 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3796 {
3797 error_free_dyn:
3798 free (dynbuf);
3799 goto error_return;
3800 }
3801
3802 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3803 if (elfsec == SHN_BAD)
3804 goto error_free_dyn;
3805 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3806
3807 for (extdyn = dynbuf;
3808 extdyn < dynbuf + s->size;
3809 extdyn += bed->s->sizeof_dyn)
3810 {
3811 Elf_Internal_Dyn dyn;
3812
3813 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3814 if (dyn.d_tag == DT_SONAME)
3815 {
3816 unsigned int tagv = dyn.d_un.d_val;
3817 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3818 if (soname == NULL)
3819 goto error_free_dyn;
3820 }
3821 if (dyn.d_tag == DT_NEEDED)
3822 {
3823 struct bfd_link_needed_list *n, **pn;
3824 char *fnm, *anm;
3825 unsigned int tagv = dyn.d_un.d_val;
3826
3827 amt = sizeof (struct bfd_link_needed_list);
3828 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3829 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3830 if (n == NULL || fnm == NULL)
3831 goto error_free_dyn;
3832 amt = strlen (fnm) + 1;
3833 anm = (char *) bfd_alloc (abfd, amt);
3834 if (anm == NULL)
3835 goto error_free_dyn;
3836 memcpy (anm, fnm, amt);
3837 n->name = anm;
3838 n->by = abfd;
3839 n->next = NULL;
3840 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3841 ;
3842 *pn = n;
3843 }
3844 if (dyn.d_tag == DT_RUNPATH)
3845 {
3846 struct bfd_link_needed_list *n, **pn;
3847 char *fnm, *anm;
3848 unsigned int tagv = dyn.d_un.d_val;
3849
3850 amt = sizeof (struct bfd_link_needed_list);
3851 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3852 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3853 if (n == NULL || fnm == NULL)
3854 goto error_free_dyn;
3855 amt = strlen (fnm) + 1;
3856 anm = (char *) bfd_alloc (abfd, amt);
3857 if (anm == NULL)
3858 goto error_free_dyn;
3859 memcpy (anm, fnm, amt);
3860 n->name = anm;
3861 n->by = abfd;
3862 n->next = NULL;
3863 for (pn = & runpath;
3864 *pn != NULL;
3865 pn = &(*pn)->next)
3866 ;
3867 *pn = n;
3868 }
3869 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3870 if (!runpath && dyn.d_tag == DT_RPATH)
3871 {
3872 struct bfd_link_needed_list *n, **pn;
3873 char *fnm, *anm;
3874 unsigned int tagv = dyn.d_un.d_val;
3875
3876 amt = sizeof (struct bfd_link_needed_list);
3877 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3878 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3879 if (n == NULL || fnm == NULL)
3880 goto error_free_dyn;
3881 amt = strlen (fnm) + 1;
3882 anm = (char *) bfd_alloc (abfd, amt);
3883 if (anm == NULL)
3884 goto error_free_dyn;
3885 memcpy (anm, fnm, amt);
3886 n->name = anm;
3887 n->by = abfd;
3888 n->next = NULL;
3889 for (pn = & rpath;
3890 *pn != NULL;
3891 pn = &(*pn)->next)
3892 ;
3893 *pn = n;
3894 }
3895 if (dyn.d_tag == DT_AUDIT)
3896 {
3897 unsigned int tagv = dyn.d_un.d_val;
3898 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3899 }
3900 }
3901
3902 free (dynbuf);
3903 }
3904
3905 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3906 frees all more recently bfd_alloc'd blocks as well. */
3907 if (runpath)
3908 rpath = runpath;
3909
3910 if (rpath)
3911 {
3912 struct bfd_link_needed_list **pn;
3913 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3914 ;
3915 *pn = rpath;
3916 }
3917
3918 /* We do not want to include any of the sections in a dynamic
3919 object in the output file. We hack by simply clobbering the
3920 list of sections in the BFD. This could be handled more
3921 cleanly by, say, a new section flag; the existing
3922 SEC_NEVER_LOAD flag is not the one we want, because that one
3923 still implies that the section takes up space in the output
3924 file. */
3925 bfd_section_list_clear (abfd);
3926
3927 /* Find the name to use in a DT_NEEDED entry that refers to this
3928 object. If the object has a DT_SONAME entry, we use it.
3929 Otherwise, if the generic linker stuck something in
3930 elf_dt_name, we use that. Otherwise, we just use the file
3931 name. */
3932 if (soname == NULL || *soname == '\0')
3933 {
3934 soname = elf_dt_name (abfd);
3935 if (soname == NULL || *soname == '\0')
3936 soname = bfd_get_filename (abfd);
3937 }
3938
3939 /* Save the SONAME because sometimes the linker emulation code
3940 will need to know it. */
3941 elf_dt_name (abfd) = soname;
3942
3943 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3944 if (ret < 0)
3945 goto error_return;
3946
3947 /* If we have already included this dynamic object in the
3948 link, just ignore it. There is no reason to include a
3949 particular dynamic object more than once. */
3950 if (ret > 0)
3951 return TRUE;
3952
3953 /* Save the DT_AUDIT entry for the linker emulation code. */
3954 elf_dt_audit (abfd) = audit;
3955 }
3956
3957 /* If this is a dynamic object, we always link against the .dynsym
3958 symbol table, not the .symtab symbol table. The dynamic linker
3959 will only see the .dynsym symbol table, so there is no reason to
3960 look at .symtab for a dynamic object. */
3961
3962 if (! dynamic || elf_dynsymtab (abfd) == 0)
3963 hdr = &elf_tdata (abfd)->symtab_hdr;
3964 else
3965 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3966
3967 symcount = hdr->sh_size / bed->s->sizeof_sym;
3968
3969 /* The sh_info field of the symtab header tells us where the
3970 external symbols start. We don't care about the local symbols at
3971 this point. */
3972 if (elf_bad_symtab (abfd))
3973 {
3974 extsymcount = symcount;
3975 extsymoff = 0;
3976 }
3977 else
3978 {
3979 extsymcount = symcount - hdr->sh_info;
3980 extsymoff = hdr->sh_info;
3981 }
3982
3983 sym_hash = elf_sym_hashes (abfd);
3984 if (extsymcount != 0)
3985 {
3986 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3987 NULL, NULL, NULL);
3988 if (isymbuf == NULL)
3989 goto error_return;
3990
3991 if (sym_hash == NULL)
3992 {
3993 /* We store a pointer to the hash table entry for each
3994 external symbol. */
3995 amt = extsymcount;
3996 amt *= sizeof (struct elf_link_hash_entry *);
3997 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
3998 if (sym_hash == NULL)
3999 goto error_free_sym;
4000 elf_sym_hashes (abfd) = sym_hash;
4001 }
4002 }
4003
4004 if (dynamic)
4005 {
4006 /* Read in any version definitions. */
4007 if (!_bfd_elf_slurp_version_tables (abfd,
4008 info->default_imported_symver))
4009 goto error_free_sym;
4010
4011 /* Read in the symbol versions, but don't bother to convert them
4012 to internal format. */
4013 if (elf_dynversym (abfd) != 0)
4014 {
4015 Elf_Internal_Shdr *versymhdr;
4016
4017 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4018 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4019 if (extversym == NULL)
4020 goto error_free_sym;
4021 amt = versymhdr->sh_size;
4022 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4023 || bfd_bread (extversym, amt, abfd) != amt)
4024 goto error_free_vers;
4025 }
4026 }
4027
4028 /* If we are loading an as-needed shared lib, save the symbol table
4029 state before we start adding symbols. If the lib turns out
4030 to be unneeded, restore the state. */
4031 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4032 {
4033 unsigned int i;
4034 size_t entsize;
4035
4036 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4037 {
4038 struct bfd_hash_entry *p;
4039 struct elf_link_hash_entry *h;
4040
4041 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4042 {
4043 h = (struct elf_link_hash_entry *) p;
4044 entsize += htab->root.table.entsize;
4045 if (h->root.type == bfd_link_hash_warning)
4046 entsize += htab->root.table.entsize;
4047 }
4048 }
4049
4050 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4051 old_tab = bfd_malloc (tabsize + entsize);
4052 if (old_tab == NULL)
4053 goto error_free_vers;
4054
4055 /* Remember the current objalloc pointer, so that all mem for
4056 symbols added can later be reclaimed. */
4057 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4058 if (alloc_mark == NULL)
4059 goto error_free_vers;
4060
4061 /* Make a special call to the linker "notice" function to
4062 tell it that we are about to handle an as-needed lib. */
4063 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4064 goto error_free_vers;
4065
4066 /* Clone the symbol table. Remember some pointers into the
4067 symbol table, and dynamic symbol count. */
4068 old_ent = (char *) old_tab + tabsize;
4069 memcpy (old_tab, htab->root.table.table, tabsize);
4070 old_undefs = htab->root.undefs;
4071 old_undefs_tail = htab->root.undefs_tail;
4072 old_table = htab->root.table.table;
4073 old_size = htab->root.table.size;
4074 old_count = htab->root.table.count;
4075 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4076 if (old_strtab == NULL)
4077 goto error_free_vers;
4078
4079 for (i = 0; i < htab->root.table.size; i++)
4080 {
4081 struct bfd_hash_entry *p;
4082 struct elf_link_hash_entry *h;
4083
4084 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4085 {
4086 memcpy (old_ent, p, htab->root.table.entsize);
4087 old_ent = (char *) old_ent + htab->root.table.entsize;
4088 h = (struct elf_link_hash_entry *) p;
4089 if (h->root.type == bfd_link_hash_warning)
4090 {
4091 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4092 old_ent = (char *) old_ent + htab->root.table.entsize;
4093 }
4094 }
4095 }
4096 }
4097
4098 weaks = NULL;
4099 ever = extversym != NULL ? extversym + extsymoff : NULL;
4100 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4101 isym < isymend;
4102 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4103 {
4104 int bind;
4105 bfd_vma value;
4106 asection *sec, *new_sec;
4107 flagword flags;
4108 const char *name;
4109 struct elf_link_hash_entry *h;
4110 struct elf_link_hash_entry *hi;
4111 bfd_boolean definition;
4112 bfd_boolean size_change_ok;
4113 bfd_boolean type_change_ok;
4114 bfd_boolean new_weakdef;
4115 bfd_boolean new_weak;
4116 bfd_boolean old_weak;
4117 bfd_boolean override;
4118 bfd_boolean common;
4119 bfd_boolean discarded;
4120 unsigned int old_alignment;
4121 bfd *old_bfd;
4122 bfd_boolean matched;
4123
4124 override = FALSE;
4125
4126 flags = BSF_NO_FLAGS;
4127 sec = NULL;
4128 value = isym->st_value;
4129 common = bed->common_definition (isym);
4130 discarded = FALSE;
4131
4132 bind = ELF_ST_BIND (isym->st_info);
4133 switch (bind)
4134 {
4135 case STB_LOCAL:
4136 /* This should be impossible, since ELF requires that all
4137 global symbols follow all local symbols, and that sh_info
4138 point to the first global symbol. Unfortunately, Irix 5
4139 screws this up. */
4140 continue;
4141
4142 case STB_GLOBAL:
4143 if (isym->st_shndx != SHN_UNDEF && !common)
4144 flags = BSF_GLOBAL;
4145 break;
4146
4147 case STB_WEAK:
4148 flags = BSF_WEAK;
4149 break;
4150
4151 case STB_GNU_UNIQUE:
4152 flags = BSF_GNU_UNIQUE;
4153 break;
4154
4155 default:
4156 /* Leave it up to the processor backend. */
4157 break;
4158 }
4159
4160 if (isym->st_shndx == SHN_UNDEF)
4161 sec = bfd_und_section_ptr;
4162 else if (isym->st_shndx == SHN_ABS)
4163 sec = bfd_abs_section_ptr;
4164 else if (isym->st_shndx == SHN_COMMON)
4165 {
4166 sec = bfd_com_section_ptr;
4167 /* What ELF calls the size we call the value. What ELF
4168 calls the value we call the alignment. */
4169 value = isym->st_size;
4170 }
4171 else
4172 {
4173 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4174 if (sec == NULL)
4175 sec = bfd_abs_section_ptr;
4176 else if (discarded_section (sec))
4177 {
4178 /* Symbols from discarded section are undefined. We keep
4179 its visibility. */
4180 sec = bfd_und_section_ptr;
4181 discarded = TRUE;
4182 isym->st_shndx = SHN_UNDEF;
4183 }
4184 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4185 value -= sec->vma;
4186 }
4187
4188 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4189 isym->st_name);
4190 if (name == NULL)
4191 goto error_free_vers;
4192
4193 if (isym->st_shndx == SHN_COMMON
4194 && (abfd->flags & BFD_PLUGIN) != 0)
4195 {
4196 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4197
4198 if (xc == NULL)
4199 {
4200 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4201 | SEC_EXCLUDE);
4202 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4203 if (xc == NULL)
4204 goto error_free_vers;
4205 }
4206 sec = xc;
4207 }
4208 else if (isym->st_shndx == SHN_COMMON
4209 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4210 && !bfd_link_relocatable (info))
4211 {
4212 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4213
4214 if (tcomm == NULL)
4215 {
4216 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4217 | SEC_LINKER_CREATED);
4218 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4219 if (tcomm == NULL)
4220 goto error_free_vers;
4221 }
4222 sec = tcomm;
4223 }
4224 else if (bed->elf_add_symbol_hook)
4225 {
4226 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4227 &sec, &value))
4228 goto error_free_vers;
4229
4230 /* The hook function sets the name to NULL if this symbol
4231 should be skipped for some reason. */
4232 if (name == NULL)
4233 continue;
4234 }
4235
4236 /* Sanity check that all possibilities were handled. */
4237 if (sec == NULL)
4238 {
4239 bfd_set_error (bfd_error_bad_value);
4240 goto error_free_vers;
4241 }
4242
4243 /* Silently discard TLS symbols from --just-syms. There's
4244 no way to combine a static TLS block with a new TLS block
4245 for this executable. */
4246 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4247 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4248 continue;
4249
4250 if (bfd_is_und_section (sec)
4251 || bfd_is_com_section (sec))
4252 definition = FALSE;
4253 else
4254 definition = TRUE;
4255
4256 size_change_ok = FALSE;
4257 type_change_ok = bed->type_change_ok;
4258 old_weak = FALSE;
4259 matched = FALSE;
4260 old_alignment = 0;
4261 old_bfd = NULL;
4262 new_sec = sec;
4263
4264 if (is_elf_hash_table (htab))
4265 {
4266 Elf_Internal_Versym iver;
4267 unsigned int vernum = 0;
4268 bfd_boolean skip;
4269
4270 if (ever == NULL)
4271 {
4272 if (info->default_imported_symver)
4273 /* Use the default symbol version created earlier. */
4274 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4275 else
4276 iver.vs_vers = 0;
4277 }
4278 else
4279 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4280
4281 vernum = iver.vs_vers & VERSYM_VERSION;
4282
4283 /* If this is a hidden symbol, or if it is not version
4284 1, we append the version name to the symbol name.
4285 However, we do not modify a non-hidden absolute symbol
4286 if it is not a function, because it might be the version
4287 symbol itself. FIXME: What if it isn't? */
4288 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4289 || (vernum > 1
4290 && (!bfd_is_abs_section (sec)
4291 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4292 {
4293 const char *verstr;
4294 size_t namelen, verlen, newlen;
4295 char *newname, *p;
4296
4297 if (isym->st_shndx != SHN_UNDEF)
4298 {
4299 if (vernum > elf_tdata (abfd)->cverdefs)
4300 verstr = NULL;
4301 else if (vernum > 1)
4302 verstr =
4303 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4304 else
4305 verstr = "";
4306
4307 if (verstr == NULL)
4308 {
4309 (*_bfd_error_handler)
4310 (_("%B: %s: invalid version %u (max %d)"),
4311 abfd, name, vernum,
4312 elf_tdata (abfd)->cverdefs);
4313 bfd_set_error (bfd_error_bad_value);
4314 goto error_free_vers;
4315 }
4316 }
4317 else
4318 {
4319 /* We cannot simply test for the number of
4320 entries in the VERNEED section since the
4321 numbers for the needed versions do not start
4322 at 0. */
4323 Elf_Internal_Verneed *t;
4324
4325 verstr = NULL;
4326 for (t = elf_tdata (abfd)->verref;
4327 t != NULL;
4328 t = t->vn_nextref)
4329 {
4330 Elf_Internal_Vernaux *a;
4331
4332 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4333 {
4334 if (a->vna_other == vernum)
4335 {
4336 verstr = a->vna_nodename;
4337 break;
4338 }
4339 }
4340 if (a != NULL)
4341 break;
4342 }
4343 if (verstr == NULL)
4344 {
4345 (*_bfd_error_handler)
4346 (_("%B: %s: invalid needed version %d"),
4347 abfd, name, vernum);
4348 bfd_set_error (bfd_error_bad_value);
4349 goto error_free_vers;
4350 }
4351 }
4352
4353 namelen = strlen (name);
4354 verlen = strlen (verstr);
4355 newlen = namelen + verlen + 2;
4356 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4357 && isym->st_shndx != SHN_UNDEF)
4358 ++newlen;
4359
4360 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4361 if (newname == NULL)
4362 goto error_free_vers;
4363 memcpy (newname, name, namelen);
4364 p = newname + namelen;
4365 *p++ = ELF_VER_CHR;
4366 /* If this is a defined non-hidden version symbol,
4367 we add another @ to the name. This indicates the
4368 default version of the symbol. */
4369 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4370 && isym->st_shndx != SHN_UNDEF)
4371 *p++ = ELF_VER_CHR;
4372 memcpy (p, verstr, verlen + 1);
4373
4374 name = newname;
4375 }
4376
4377 /* If this symbol has default visibility and the user has
4378 requested we not re-export it, then mark it as hidden. */
4379 if (!bfd_is_und_section (sec)
4380 && !dynamic
4381 && abfd->no_export
4382 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4383 isym->st_other = (STV_HIDDEN
4384 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4385
4386 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4387 sym_hash, &old_bfd, &old_weak,
4388 &old_alignment, &skip, &override,
4389 &type_change_ok, &size_change_ok,
4390 &matched))
4391 goto error_free_vers;
4392
4393 if (skip)
4394 continue;
4395
4396 /* Override a definition only if the new symbol matches the
4397 existing one. */
4398 if (override && matched)
4399 definition = FALSE;
4400
4401 h = *sym_hash;
4402 while (h->root.type == bfd_link_hash_indirect
4403 || h->root.type == bfd_link_hash_warning)
4404 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4405
4406 if (elf_tdata (abfd)->verdef != NULL
4407 && vernum > 1
4408 && definition)
4409 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4410 }
4411
4412 if (! (_bfd_generic_link_add_one_symbol
4413 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4414 (struct bfd_link_hash_entry **) sym_hash)))
4415 goto error_free_vers;
4416
4417 if ((flags & BSF_GNU_UNIQUE)
4418 && (abfd->flags & DYNAMIC) == 0
4419 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4420 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
4421
4422 h = *sym_hash;
4423 /* We need to make sure that indirect symbol dynamic flags are
4424 updated. */
4425 hi = h;
4426 while (h->root.type == bfd_link_hash_indirect
4427 || h->root.type == bfd_link_hash_warning)
4428 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4429
4430 /* Setting the index to -3 tells elf_link_output_extsym that
4431 this symbol is defined in a discarded section. */
4432 if (discarded)
4433 h->indx = -3;
4434
4435 *sym_hash = h;
4436
4437 new_weak = (flags & BSF_WEAK) != 0;
4438 new_weakdef = FALSE;
4439 if (dynamic
4440 && definition
4441 && new_weak
4442 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4443 && is_elf_hash_table (htab)
4444 && h->u.weakdef == NULL)
4445 {
4446 /* Keep a list of all weak defined non function symbols from
4447 a dynamic object, using the weakdef field. Later in this
4448 function we will set the weakdef field to the correct
4449 value. We only put non-function symbols from dynamic
4450 objects on this list, because that happens to be the only
4451 time we need to know the normal symbol corresponding to a
4452 weak symbol, and the information is time consuming to
4453 figure out. If the weakdef field is not already NULL,
4454 then this symbol was already defined by some previous
4455 dynamic object, and we will be using that previous
4456 definition anyhow. */
4457
4458 h->u.weakdef = weaks;
4459 weaks = h;
4460 new_weakdef = TRUE;
4461 }
4462
4463 /* Set the alignment of a common symbol. */
4464 if ((common || bfd_is_com_section (sec))
4465 && h->root.type == bfd_link_hash_common)
4466 {
4467 unsigned int align;
4468
4469 if (common)
4470 align = bfd_log2 (isym->st_value);
4471 else
4472 {
4473 /* The new symbol is a common symbol in a shared object.
4474 We need to get the alignment from the section. */
4475 align = new_sec->alignment_power;
4476 }
4477 if (align > old_alignment)
4478 h->root.u.c.p->alignment_power = align;
4479 else
4480 h->root.u.c.p->alignment_power = old_alignment;
4481 }
4482
4483 if (is_elf_hash_table (htab))
4484 {
4485 /* Set a flag in the hash table entry indicating the type of
4486 reference or definition we just found. A dynamic symbol
4487 is one which is referenced or defined by both a regular
4488 object and a shared object. */
4489 bfd_boolean dynsym = FALSE;
4490
4491 /* Plugin symbols aren't normal. Don't set def_regular or
4492 ref_regular for them, or make them dynamic. */
4493 if ((abfd->flags & BFD_PLUGIN) != 0)
4494 ;
4495 else if (! dynamic)
4496 {
4497 if (! definition)
4498 {
4499 h->ref_regular = 1;
4500 if (bind != STB_WEAK)
4501 h->ref_regular_nonweak = 1;
4502 }
4503 else
4504 {
4505 h->def_regular = 1;
4506 if (h->def_dynamic)
4507 {
4508 h->def_dynamic = 0;
4509 h->ref_dynamic = 1;
4510 }
4511 }
4512
4513 /* If the indirect symbol has been forced local, don't
4514 make the real symbol dynamic. */
4515 if ((h == hi || !hi->forced_local)
4516 && (bfd_link_dll (info)
4517 || h->def_dynamic
4518 || h->ref_dynamic))
4519 dynsym = TRUE;
4520 }
4521 else
4522 {
4523 if (! definition)
4524 {
4525 h->ref_dynamic = 1;
4526 hi->ref_dynamic = 1;
4527 }
4528 else
4529 {
4530 h->def_dynamic = 1;
4531 hi->def_dynamic = 1;
4532 }
4533
4534 /* If the indirect symbol has been forced local, don't
4535 make the real symbol dynamic. */
4536 if ((h == hi || !hi->forced_local)
4537 && (h->def_regular
4538 || h->ref_regular
4539 || (h->u.weakdef != NULL
4540 && ! new_weakdef
4541 && h->u.weakdef->dynindx != -1)))
4542 dynsym = TRUE;
4543 }
4544
4545 /* Check to see if we need to add an indirect symbol for
4546 the default name. */
4547 if (definition
4548 || (!override && h->root.type == bfd_link_hash_common))
4549 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4550 sec, value, &old_bfd, &dynsym))
4551 goto error_free_vers;
4552
4553 /* Check the alignment when a common symbol is involved. This
4554 can change when a common symbol is overridden by a normal
4555 definition or a common symbol is ignored due to the old
4556 normal definition. We need to make sure the maximum
4557 alignment is maintained. */
4558 if ((old_alignment || common)
4559 && h->root.type != bfd_link_hash_common)
4560 {
4561 unsigned int common_align;
4562 unsigned int normal_align;
4563 unsigned int symbol_align;
4564 bfd *normal_bfd;
4565 bfd *common_bfd;
4566
4567 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4568 || h->root.type == bfd_link_hash_defweak);
4569
4570 symbol_align = ffs (h->root.u.def.value) - 1;
4571 if (h->root.u.def.section->owner != NULL
4572 && (h->root.u.def.section->owner->flags
4573 & (DYNAMIC | BFD_PLUGIN)) == 0)
4574 {
4575 normal_align = h->root.u.def.section->alignment_power;
4576 if (normal_align > symbol_align)
4577 normal_align = symbol_align;
4578 }
4579 else
4580 normal_align = symbol_align;
4581
4582 if (old_alignment)
4583 {
4584 common_align = old_alignment;
4585 common_bfd = old_bfd;
4586 normal_bfd = abfd;
4587 }
4588 else
4589 {
4590 common_align = bfd_log2 (isym->st_value);
4591 common_bfd = abfd;
4592 normal_bfd = old_bfd;
4593 }
4594
4595 if (normal_align < common_align)
4596 {
4597 /* PR binutils/2735 */
4598 if (normal_bfd == NULL)
4599 (*_bfd_error_handler)
4600 (_("Warning: alignment %u of common symbol `%s' in %B is"
4601 " greater than the alignment (%u) of its section %A"),
4602 common_bfd, h->root.u.def.section,
4603 1 << common_align, name, 1 << normal_align);
4604 else
4605 (*_bfd_error_handler)
4606 (_("Warning: alignment %u of symbol `%s' in %B"
4607 " is smaller than %u in %B"),
4608 normal_bfd, common_bfd,
4609 1 << normal_align, name, 1 << common_align);
4610 }
4611 }
4612
4613 /* Remember the symbol size if it isn't undefined. */
4614 if (isym->st_size != 0
4615 && isym->st_shndx != SHN_UNDEF
4616 && (definition || h->size == 0))
4617 {
4618 if (h->size != 0
4619 && h->size != isym->st_size
4620 && ! size_change_ok)
4621 (*_bfd_error_handler)
4622 (_("Warning: size of symbol `%s' changed"
4623 " from %lu in %B to %lu in %B"),
4624 old_bfd, abfd,
4625 name, (unsigned long) h->size,
4626 (unsigned long) isym->st_size);
4627
4628 h->size = isym->st_size;
4629 }
4630
4631 /* If this is a common symbol, then we always want H->SIZE
4632 to be the size of the common symbol. The code just above
4633 won't fix the size if a common symbol becomes larger. We
4634 don't warn about a size change here, because that is
4635 covered by --warn-common. Allow changes between different
4636 function types. */
4637 if (h->root.type == bfd_link_hash_common)
4638 h->size = h->root.u.c.size;
4639
4640 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4641 && ((definition && !new_weak)
4642 || (old_weak && h->root.type == bfd_link_hash_common)
4643 || h->type == STT_NOTYPE))
4644 {
4645 unsigned int type = ELF_ST_TYPE (isym->st_info);
4646
4647 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4648 symbol. */
4649 if (type == STT_GNU_IFUNC
4650 && (abfd->flags & DYNAMIC) != 0)
4651 type = STT_FUNC;
4652
4653 if (h->type != type)
4654 {
4655 if (h->type != STT_NOTYPE && ! type_change_ok)
4656 (*_bfd_error_handler)
4657 (_("Warning: type of symbol `%s' changed"
4658 " from %d to %d in %B"),
4659 abfd, name, h->type, type);
4660
4661 h->type = type;
4662 }
4663 }
4664
4665 /* Merge st_other field. */
4666 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4667
4668 /* We don't want to make debug symbol dynamic. */
4669 if (definition
4670 && (sec->flags & SEC_DEBUGGING)
4671 && !bfd_link_relocatable (info))
4672 dynsym = FALSE;
4673
4674 /* Nor should we make plugin symbols dynamic. */
4675 if ((abfd->flags & BFD_PLUGIN) != 0)
4676 dynsym = FALSE;
4677
4678 if (definition)
4679 {
4680 h->target_internal = isym->st_target_internal;
4681 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4682 }
4683
4684 if (definition && !dynamic)
4685 {
4686 char *p = strchr (name, ELF_VER_CHR);
4687 if (p != NULL && p[1] != ELF_VER_CHR)
4688 {
4689 /* Queue non-default versions so that .symver x, x@FOO
4690 aliases can be checked. */
4691 if (!nondeflt_vers)
4692 {
4693 amt = ((isymend - isym + 1)
4694 * sizeof (struct elf_link_hash_entry *));
4695 nondeflt_vers
4696 = (struct elf_link_hash_entry **) bfd_malloc (amt);
4697 if (!nondeflt_vers)
4698 goto error_free_vers;
4699 }
4700 nondeflt_vers[nondeflt_vers_cnt++] = h;
4701 }
4702 }
4703
4704 if (dynsym && h->dynindx == -1)
4705 {
4706 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4707 goto error_free_vers;
4708 if (h->u.weakdef != NULL
4709 && ! new_weakdef
4710 && h->u.weakdef->dynindx == -1)
4711 {
4712 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4713 goto error_free_vers;
4714 }
4715 }
4716 else if (h->dynindx != -1)
4717 /* If the symbol already has a dynamic index, but
4718 visibility says it should not be visible, turn it into
4719 a local symbol. */
4720 switch (ELF_ST_VISIBILITY (h->other))
4721 {
4722 case STV_INTERNAL:
4723 case STV_HIDDEN:
4724 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4725 dynsym = FALSE;
4726 break;
4727 }
4728
4729 /* Don't add DT_NEEDED for references from the dummy bfd nor
4730 for unmatched symbol. */
4731 if (!add_needed
4732 && matched
4733 && definition
4734 && ((dynsym
4735 && h->ref_regular_nonweak
4736 && (old_bfd == NULL
4737 || (old_bfd->flags & BFD_PLUGIN) == 0))
4738 || (h->ref_dynamic_nonweak
4739 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4740 && !on_needed_list (elf_dt_name (abfd),
4741 htab->needed, NULL))))
4742 {
4743 int ret;
4744 const char *soname = elf_dt_name (abfd);
4745
4746 info->callbacks->minfo ("%!", soname, old_bfd,
4747 h->root.root.string);
4748
4749 /* A symbol from a library loaded via DT_NEEDED of some
4750 other library is referenced by a regular object.
4751 Add a DT_NEEDED entry for it. Issue an error if
4752 --no-add-needed is used and the reference was not
4753 a weak one. */
4754 if (old_bfd != NULL
4755 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4756 {
4757 (*_bfd_error_handler)
4758 (_("%B: undefined reference to symbol '%s'"),
4759 old_bfd, name);
4760 bfd_set_error (bfd_error_missing_dso);
4761 goto error_free_vers;
4762 }
4763
4764 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4765 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
4766
4767 add_needed = TRUE;
4768 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4769 if (ret < 0)
4770 goto error_free_vers;
4771
4772 BFD_ASSERT (ret == 0);
4773 }
4774 }
4775 }
4776
4777 if (extversym != NULL)
4778 {
4779 free (extversym);
4780 extversym = NULL;
4781 }
4782
4783 if (isymbuf != NULL)
4784 {
4785 free (isymbuf);
4786 isymbuf = NULL;
4787 }
4788
4789 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4790 {
4791 unsigned int i;
4792
4793 /* Restore the symbol table. */
4794 old_ent = (char *) old_tab + tabsize;
4795 memset (elf_sym_hashes (abfd), 0,
4796 extsymcount * sizeof (struct elf_link_hash_entry *));
4797 htab->root.table.table = old_table;
4798 htab->root.table.size = old_size;
4799 htab->root.table.count = old_count;
4800 memcpy (htab->root.table.table, old_tab, tabsize);
4801 htab->root.undefs = old_undefs;
4802 htab->root.undefs_tail = old_undefs_tail;
4803 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
4804 free (old_strtab);
4805 old_strtab = NULL;
4806 for (i = 0; i < htab->root.table.size; i++)
4807 {
4808 struct bfd_hash_entry *p;
4809 struct elf_link_hash_entry *h;
4810 bfd_size_type size;
4811 unsigned int alignment_power;
4812
4813 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4814 {
4815 h = (struct elf_link_hash_entry *) p;
4816 if (h->root.type == bfd_link_hash_warning)
4817 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4818
4819 /* Preserve the maximum alignment and size for common
4820 symbols even if this dynamic lib isn't on DT_NEEDED
4821 since it can still be loaded at run time by another
4822 dynamic lib. */
4823 if (h->root.type == bfd_link_hash_common)
4824 {
4825 size = h->root.u.c.size;
4826 alignment_power = h->root.u.c.p->alignment_power;
4827 }
4828 else
4829 {
4830 size = 0;
4831 alignment_power = 0;
4832 }
4833 memcpy (p, old_ent, htab->root.table.entsize);
4834 old_ent = (char *) old_ent + htab->root.table.entsize;
4835 h = (struct elf_link_hash_entry *) p;
4836 if (h->root.type == bfd_link_hash_warning)
4837 {
4838 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4839 old_ent = (char *) old_ent + htab->root.table.entsize;
4840 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4841 }
4842 if (h->root.type == bfd_link_hash_common)
4843 {
4844 if (size > h->root.u.c.size)
4845 h->root.u.c.size = size;
4846 if (alignment_power > h->root.u.c.p->alignment_power)
4847 h->root.u.c.p->alignment_power = alignment_power;
4848 }
4849 }
4850 }
4851
4852 /* Make a special call to the linker "notice" function to
4853 tell it that symbols added for crefs may need to be removed. */
4854 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
4855 goto error_free_vers;
4856
4857 free (old_tab);
4858 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4859 alloc_mark);
4860 if (nondeflt_vers != NULL)
4861 free (nondeflt_vers);
4862 return TRUE;
4863 }
4864
4865 if (old_tab != NULL)
4866 {
4867 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
4868 goto error_free_vers;
4869 free (old_tab);
4870 old_tab = NULL;
4871 }
4872
4873 /* Now that all the symbols from this input file are created, if
4874 not performing a relocatable link, handle .symver foo, foo@BAR
4875 such that any relocs against foo become foo@BAR. */
4876 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4877 {
4878 size_t cnt, symidx;
4879
4880 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4881 {
4882 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4883 char *shortname, *p;
4884
4885 p = strchr (h->root.root.string, ELF_VER_CHR);
4886 if (p == NULL
4887 || (h->root.type != bfd_link_hash_defined
4888 && h->root.type != bfd_link_hash_defweak))
4889 continue;
4890
4891 amt = p - h->root.root.string;
4892 shortname = (char *) bfd_malloc (amt + 1);
4893 if (!shortname)
4894 goto error_free_vers;
4895 memcpy (shortname, h->root.root.string, amt);
4896 shortname[amt] = '\0';
4897
4898 hi = (struct elf_link_hash_entry *)
4899 bfd_link_hash_lookup (&htab->root, shortname,
4900 FALSE, FALSE, FALSE);
4901 if (hi != NULL
4902 && hi->root.type == h->root.type
4903 && hi->root.u.def.value == h->root.u.def.value
4904 && hi->root.u.def.section == h->root.u.def.section)
4905 {
4906 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4907 hi->root.type = bfd_link_hash_indirect;
4908 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4909 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4910 sym_hash = elf_sym_hashes (abfd);
4911 if (sym_hash)
4912 for (symidx = 0; symidx < extsymcount; ++symidx)
4913 if (sym_hash[symidx] == hi)
4914 {
4915 sym_hash[symidx] = h;
4916 break;
4917 }
4918 }
4919 free (shortname);
4920 }
4921 free (nondeflt_vers);
4922 nondeflt_vers = NULL;
4923 }
4924
4925 /* Now set the weakdefs field correctly for all the weak defined
4926 symbols we found. The only way to do this is to search all the
4927 symbols. Since we only need the information for non functions in
4928 dynamic objects, that's the only time we actually put anything on
4929 the list WEAKS. We need this information so that if a regular
4930 object refers to a symbol defined weakly in a dynamic object, the
4931 real symbol in the dynamic object is also put in the dynamic
4932 symbols; we also must arrange for both symbols to point to the
4933 same memory location. We could handle the general case of symbol
4934 aliasing, but a general symbol alias can only be generated in
4935 assembler code, handling it correctly would be very time
4936 consuming, and other ELF linkers don't handle general aliasing
4937 either. */
4938 if (weaks != NULL)
4939 {
4940 struct elf_link_hash_entry **hpp;
4941 struct elf_link_hash_entry **hppend;
4942 struct elf_link_hash_entry **sorted_sym_hash;
4943 struct elf_link_hash_entry *h;
4944 size_t sym_count;
4945
4946 /* Since we have to search the whole symbol list for each weak
4947 defined symbol, search time for N weak defined symbols will be
4948 O(N^2). Binary search will cut it down to O(NlogN). */
4949 amt = extsymcount;
4950 amt *= sizeof (struct elf_link_hash_entry *);
4951 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4952 if (sorted_sym_hash == NULL)
4953 goto error_return;
4954 sym_hash = sorted_sym_hash;
4955 hpp = elf_sym_hashes (abfd);
4956 hppend = hpp + extsymcount;
4957 sym_count = 0;
4958 for (; hpp < hppend; hpp++)
4959 {
4960 h = *hpp;
4961 if (h != NULL
4962 && h->root.type == bfd_link_hash_defined
4963 && !bed->is_function_type (h->type))
4964 {
4965 *sym_hash = h;
4966 sym_hash++;
4967 sym_count++;
4968 }
4969 }
4970
4971 qsort (sorted_sym_hash, sym_count,
4972 sizeof (struct elf_link_hash_entry *),
4973 elf_sort_symbol);
4974
4975 while (weaks != NULL)
4976 {
4977 struct elf_link_hash_entry *hlook;
4978 asection *slook;
4979 bfd_vma vlook;
4980 size_t i, j, idx = 0;
4981
4982 hlook = weaks;
4983 weaks = hlook->u.weakdef;
4984 hlook->u.weakdef = NULL;
4985
4986 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4987 || hlook->root.type == bfd_link_hash_defweak
4988 || hlook->root.type == bfd_link_hash_common
4989 || hlook->root.type == bfd_link_hash_indirect);
4990 slook = hlook->root.u.def.section;
4991 vlook = hlook->root.u.def.value;
4992
4993 i = 0;
4994 j = sym_count;
4995 while (i != j)
4996 {
4997 bfd_signed_vma vdiff;
4998 idx = (i + j) / 2;
4999 h = sorted_sym_hash[idx];
5000 vdiff = vlook - h->root.u.def.value;
5001 if (vdiff < 0)
5002 j = idx;
5003 else if (vdiff > 0)
5004 i = idx + 1;
5005 else
5006 {
5007 int sdiff = slook->id - h->root.u.def.section->id;
5008 if (sdiff < 0)
5009 j = idx;
5010 else if (sdiff > 0)
5011 i = idx + 1;
5012 else
5013 break;
5014 }
5015 }
5016
5017 /* We didn't find a value/section match. */
5018 if (i == j)
5019 continue;
5020
5021 /* With multiple aliases, or when the weak symbol is already
5022 strongly defined, we have multiple matching symbols and
5023 the binary search above may land on any of them. Step
5024 one past the matching symbol(s). */
5025 while (++idx != j)
5026 {
5027 h = sorted_sym_hash[idx];
5028 if (h->root.u.def.section != slook
5029 || h->root.u.def.value != vlook)
5030 break;
5031 }
5032
5033 /* Now look back over the aliases. Since we sorted by size
5034 as well as value and section, we'll choose the one with
5035 the largest size. */
5036 while (idx-- != i)
5037 {
5038 h = sorted_sym_hash[idx];
5039
5040 /* Stop if value or section doesn't match. */
5041 if (h->root.u.def.section != slook
5042 || h->root.u.def.value != vlook)
5043 break;
5044 else if (h != hlook)
5045 {
5046 hlook->u.weakdef = h;
5047
5048 /* If the weak definition is in the list of dynamic
5049 symbols, make sure the real definition is put
5050 there as well. */
5051 if (hlook->dynindx != -1 && h->dynindx == -1)
5052 {
5053 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5054 {
5055 err_free_sym_hash:
5056 free (sorted_sym_hash);
5057 goto error_return;
5058 }
5059 }
5060
5061 /* If the real definition is in the list of dynamic
5062 symbols, make sure the weak definition is put
5063 there as well. If we don't do this, then the
5064 dynamic loader might not merge the entries for the
5065 real definition and the weak definition. */
5066 if (h->dynindx != -1 && hlook->dynindx == -1)
5067 {
5068 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5069 goto err_free_sym_hash;
5070 }
5071 break;
5072 }
5073 }
5074 }
5075
5076 free (sorted_sym_hash);
5077 }
5078
5079 if (bed->check_directives
5080 && !(*bed->check_directives) (abfd, info))
5081 return FALSE;
5082
5083 if (!info->check_relocs_after_open_input
5084 && !_bfd_elf_link_check_relocs (abfd, info))
5085 return FALSE;
5086
5087 /* If this is a non-traditional link, try to optimize the handling
5088 of the .stab/.stabstr sections. */
5089 if (! dynamic
5090 && ! info->traditional_format
5091 && is_elf_hash_table (htab)
5092 && (info->strip != strip_all && info->strip != strip_debugger))
5093 {
5094 asection *stabstr;
5095
5096 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5097 if (stabstr != NULL)
5098 {
5099 bfd_size_type string_offset = 0;
5100 asection *stab;
5101
5102 for (stab = abfd->sections; stab; stab = stab->next)
5103 if (CONST_STRNEQ (stab->name, ".stab")
5104 && (!stab->name[5] ||
5105 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5106 && (stab->flags & SEC_MERGE) == 0
5107 && !bfd_is_abs_section (stab->output_section))
5108 {
5109 struct bfd_elf_section_data *secdata;
5110
5111 secdata = elf_section_data (stab);
5112 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5113 stabstr, &secdata->sec_info,
5114 &string_offset))
5115 goto error_return;
5116 if (secdata->sec_info)
5117 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5118 }
5119 }
5120 }
5121
5122 if (is_elf_hash_table (htab) && add_needed)
5123 {
5124 /* Add this bfd to the loaded list. */
5125 struct elf_link_loaded_list *n;
5126
5127 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5128 if (n == NULL)
5129 goto error_return;
5130 n->abfd = abfd;
5131 n->next = htab->loaded;
5132 htab->loaded = n;
5133 }
5134
5135 return TRUE;
5136
5137 error_free_vers:
5138 if (old_tab != NULL)
5139 free (old_tab);
5140 if (old_strtab != NULL)
5141 free (old_strtab);
5142 if (nondeflt_vers != NULL)
5143 free (nondeflt_vers);
5144 if (extversym != NULL)
5145 free (extversym);
5146 error_free_sym:
5147 if (isymbuf != NULL)
5148 free (isymbuf);
5149 error_return:
5150 return FALSE;
5151 }
5152
5153 /* Return the linker hash table entry of a symbol that might be
5154 satisfied by an archive symbol. Return -1 on error. */
5155
5156 struct elf_link_hash_entry *
5157 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5158 struct bfd_link_info *info,
5159 const char *name)
5160 {
5161 struct elf_link_hash_entry *h;
5162 char *p, *copy;
5163 size_t len, first;
5164
5165 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5166 if (h != NULL)
5167 return h;
5168
5169 /* If this is a default version (the name contains @@), look up the
5170 symbol again with only one `@' as well as without the version.
5171 The effect is that references to the symbol with and without the
5172 version will be matched by the default symbol in the archive. */
5173
5174 p = strchr (name, ELF_VER_CHR);
5175 if (p == NULL || p[1] != ELF_VER_CHR)
5176 return h;
5177
5178 /* First check with only one `@'. */
5179 len = strlen (name);
5180 copy = (char *) bfd_alloc (abfd, len);
5181 if (copy == NULL)
5182 return (struct elf_link_hash_entry *) 0 - 1;
5183
5184 first = p - name + 1;
5185 memcpy (copy, name, first);
5186 memcpy (copy + first, name + first + 1, len - first);
5187
5188 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5189 if (h == NULL)
5190 {
5191 /* We also need to check references to the symbol without the
5192 version. */
5193 copy[first - 1] = '\0';
5194 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5195 FALSE, FALSE, TRUE);
5196 }
5197
5198 bfd_release (abfd, copy);
5199 return h;
5200 }
5201
5202 /* Add symbols from an ELF archive file to the linker hash table. We
5203 don't use _bfd_generic_link_add_archive_symbols because we need to
5204 handle versioned symbols.
5205
5206 Fortunately, ELF archive handling is simpler than that done by
5207 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5208 oddities. In ELF, if we find a symbol in the archive map, and the
5209 symbol is currently undefined, we know that we must pull in that
5210 object file.
5211
5212 Unfortunately, we do have to make multiple passes over the symbol
5213 table until nothing further is resolved. */
5214
5215 static bfd_boolean
5216 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5217 {
5218 symindex c;
5219 unsigned char *included = NULL;
5220 carsym *symdefs;
5221 bfd_boolean loop;
5222 bfd_size_type amt;
5223 const struct elf_backend_data *bed;
5224 struct elf_link_hash_entry * (*archive_symbol_lookup)
5225 (bfd *, struct bfd_link_info *, const char *);
5226
5227 if (! bfd_has_map (abfd))
5228 {
5229 /* An empty archive is a special case. */
5230 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5231 return TRUE;
5232 bfd_set_error (bfd_error_no_armap);
5233 return FALSE;
5234 }
5235
5236 /* Keep track of all symbols we know to be already defined, and all
5237 files we know to be already included. This is to speed up the
5238 second and subsequent passes. */
5239 c = bfd_ardata (abfd)->symdef_count;
5240 if (c == 0)
5241 return TRUE;
5242 amt = c;
5243 amt *= sizeof (*included);
5244 included = (unsigned char *) bfd_zmalloc (amt);
5245 if (included == NULL)
5246 return FALSE;
5247
5248 symdefs = bfd_ardata (abfd)->symdefs;
5249 bed = get_elf_backend_data (abfd);
5250 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5251
5252 do
5253 {
5254 file_ptr last;
5255 symindex i;
5256 carsym *symdef;
5257 carsym *symdefend;
5258
5259 loop = FALSE;
5260 last = -1;
5261
5262 symdef = symdefs;
5263 symdefend = symdef + c;
5264 for (i = 0; symdef < symdefend; symdef++, i++)
5265 {
5266 struct elf_link_hash_entry *h;
5267 bfd *element;
5268 struct bfd_link_hash_entry *undefs_tail;
5269 symindex mark;
5270
5271 if (included[i])
5272 continue;
5273 if (symdef->file_offset == last)
5274 {
5275 included[i] = TRUE;
5276 continue;
5277 }
5278
5279 h = archive_symbol_lookup (abfd, info, symdef->name);
5280 if (h == (struct elf_link_hash_entry *) 0 - 1)
5281 goto error_return;
5282
5283 if (h == NULL)
5284 continue;
5285
5286 if (h->root.type == bfd_link_hash_common)
5287 {
5288 /* We currently have a common symbol. The archive map contains
5289 a reference to this symbol, so we may want to include it. We
5290 only want to include it however, if this archive element
5291 contains a definition of the symbol, not just another common
5292 declaration of it.
5293
5294 Unfortunately some archivers (including GNU ar) will put
5295 declarations of common symbols into their archive maps, as
5296 well as real definitions, so we cannot just go by the archive
5297 map alone. Instead we must read in the element's symbol
5298 table and check that to see what kind of symbol definition
5299 this is. */
5300 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5301 continue;
5302 }
5303 else if (h->root.type != bfd_link_hash_undefined)
5304 {
5305 if (h->root.type != bfd_link_hash_undefweak)
5306 /* Symbol must be defined. Don't check it again. */
5307 included[i] = TRUE;
5308 continue;
5309 }
5310
5311 /* We need to include this archive member. */
5312 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5313 if (element == NULL)
5314 goto error_return;
5315
5316 if (! bfd_check_format (element, bfd_object))
5317 goto error_return;
5318
5319 undefs_tail = info->hash->undefs_tail;
5320
5321 if (!(*info->callbacks
5322 ->add_archive_element) (info, element, symdef->name, &element))
5323 continue;
5324 if (!bfd_link_add_symbols (element, info))
5325 goto error_return;
5326
5327 /* If there are any new undefined symbols, we need to make
5328 another pass through the archive in order to see whether
5329 they can be defined. FIXME: This isn't perfect, because
5330 common symbols wind up on undefs_tail and because an
5331 undefined symbol which is defined later on in this pass
5332 does not require another pass. This isn't a bug, but it
5333 does make the code less efficient than it could be. */
5334 if (undefs_tail != info->hash->undefs_tail)
5335 loop = TRUE;
5336
5337 /* Look backward to mark all symbols from this object file
5338 which we have already seen in this pass. */
5339 mark = i;
5340 do
5341 {
5342 included[mark] = TRUE;
5343 if (mark == 0)
5344 break;
5345 --mark;
5346 }
5347 while (symdefs[mark].file_offset == symdef->file_offset);
5348
5349 /* We mark subsequent symbols from this object file as we go
5350 on through the loop. */
5351 last = symdef->file_offset;
5352 }
5353 }
5354 while (loop);
5355
5356 free (included);
5357
5358 return TRUE;
5359
5360 error_return:
5361 if (included != NULL)
5362 free (included);
5363 return FALSE;
5364 }
5365
5366 /* Given an ELF BFD, add symbols to the global hash table as
5367 appropriate. */
5368
5369 bfd_boolean
5370 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5371 {
5372 switch (bfd_get_format (abfd))
5373 {
5374 case bfd_object:
5375 return elf_link_add_object_symbols (abfd, info);
5376 case bfd_archive:
5377 return elf_link_add_archive_symbols (abfd, info);
5378 default:
5379 bfd_set_error (bfd_error_wrong_format);
5380 return FALSE;
5381 }
5382 }
5383 \f
5384 struct hash_codes_info
5385 {
5386 unsigned long *hashcodes;
5387 bfd_boolean error;
5388 };
5389
5390 /* This function will be called though elf_link_hash_traverse to store
5391 all hash value of the exported symbols in an array. */
5392
5393 static bfd_boolean
5394 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5395 {
5396 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5397 const char *name;
5398 unsigned long ha;
5399 char *alc = NULL;
5400
5401 /* Ignore indirect symbols. These are added by the versioning code. */
5402 if (h->dynindx == -1)
5403 return TRUE;
5404
5405 name = h->root.root.string;
5406 if (h->versioned >= versioned)
5407 {
5408 char *p = strchr (name, ELF_VER_CHR);
5409 if (p != NULL)
5410 {
5411 alc = (char *) bfd_malloc (p - name + 1);
5412 if (alc == NULL)
5413 {
5414 inf->error = TRUE;
5415 return FALSE;
5416 }
5417 memcpy (alc, name, p - name);
5418 alc[p - name] = '\0';
5419 name = alc;
5420 }
5421 }
5422
5423 /* Compute the hash value. */
5424 ha = bfd_elf_hash (name);
5425
5426 /* Store the found hash value in the array given as the argument. */
5427 *(inf->hashcodes)++ = ha;
5428
5429 /* And store it in the struct so that we can put it in the hash table
5430 later. */
5431 h->u.elf_hash_value = ha;
5432
5433 if (alc != NULL)
5434 free (alc);
5435
5436 return TRUE;
5437 }
5438
5439 struct collect_gnu_hash_codes
5440 {
5441 bfd *output_bfd;
5442 const struct elf_backend_data *bed;
5443 unsigned long int nsyms;
5444 unsigned long int maskbits;
5445 unsigned long int *hashcodes;
5446 unsigned long int *hashval;
5447 unsigned long int *indx;
5448 unsigned long int *counts;
5449 bfd_vma *bitmask;
5450 bfd_byte *contents;
5451 long int min_dynindx;
5452 unsigned long int bucketcount;
5453 unsigned long int symindx;
5454 long int local_indx;
5455 long int shift1, shift2;
5456 unsigned long int mask;
5457 bfd_boolean error;
5458 };
5459
5460 /* This function will be called though elf_link_hash_traverse to store
5461 all hash value of the exported symbols in an array. */
5462
5463 static bfd_boolean
5464 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5465 {
5466 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5467 const char *name;
5468 unsigned long ha;
5469 char *alc = NULL;
5470
5471 /* Ignore indirect symbols. These are added by the versioning code. */
5472 if (h->dynindx == -1)
5473 return TRUE;
5474
5475 /* Ignore also local symbols and undefined symbols. */
5476 if (! (*s->bed->elf_hash_symbol) (h))
5477 return TRUE;
5478
5479 name = h->root.root.string;
5480 if (h->versioned >= versioned)
5481 {
5482 char *p = strchr (name, ELF_VER_CHR);
5483 if (p != NULL)
5484 {
5485 alc = (char *) bfd_malloc (p - name + 1);
5486 if (alc == NULL)
5487 {
5488 s->error = TRUE;
5489 return FALSE;
5490 }
5491 memcpy (alc, name, p - name);
5492 alc[p - name] = '\0';
5493 name = alc;
5494 }
5495 }
5496
5497 /* Compute the hash value. */
5498 ha = bfd_elf_gnu_hash (name);
5499
5500 /* Store the found hash value in the array for compute_bucket_count,
5501 and also for .dynsym reordering purposes. */
5502 s->hashcodes[s->nsyms] = ha;
5503 s->hashval[h->dynindx] = ha;
5504 ++s->nsyms;
5505 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5506 s->min_dynindx = h->dynindx;
5507
5508 if (alc != NULL)
5509 free (alc);
5510
5511 return TRUE;
5512 }
5513
5514 /* This function will be called though elf_link_hash_traverse to do
5515 final dynaminc symbol renumbering. */
5516
5517 static bfd_boolean
5518 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5519 {
5520 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5521 unsigned long int bucket;
5522 unsigned long int val;
5523
5524 /* Ignore indirect symbols. */
5525 if (h->dynindx == -1)
5526 return TRUE;
5527
5528 /* Ignore also local symbols and undefined symbols. */
5529 if (! (*s->bed->elf_hash_symbol) (h))
5530 {
5531 if (h->dynindx >= s->min_dynindx)
5532 h->dynindx = s->local_indx++;
5533 return TRUE;
5534 }
5535
5536 bucket = s->hashval[h->dynindx] % s->bucketcount;
5537 val = (s->hashval[h->dynindx] >> s->shift1)
5538 & ((s->maskbits >> s->shift1) - 1);
5539 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5540 s->bitmask[val]
5541 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5542 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5543 if (s->counts[bucket] == 1)
5544 /* Last element terminates the chain. */
5545 val |= 1;
5546 bfd_put_32 (s->output_bfd, val,
5547 s->contents + (s->indx[bucket] - s->symindx) * 4);
5548 --s->counts[bucket];
5549 h->dynindx = s->indx[bucket]++;
5550 return TRUE;
5551 }
5552
5553 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5554
5555 bfd_boolean
5556 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5557 {
5558 return !(h->forced_local
5559 || h->root.type == bfd_link_hash_undefined
5560 || h->root.type == bfd_link_hash_undefweak
5561 || ((h->root.type == bfd_link_hash_defined
5562 || h->root.type == bfd_link_hash_defweak)
5563 && h->root.u.def.section->output_section == NULL));
5564 }
5565
5566 /* Array used to determine the number of hash table buckets to use
5567 based on the number of symbols there are. If there are fewer than
5568 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5569 fewer than 37 we use 17 buckets, and so forth. We never use more
5570 than 32771 buckets. */
5571
5572 static const size_t elf_buckets[] =
5573 {
5574 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5575 16411, 32771, 0
5576 };
5577
5578 /* Compute bucket count for hashing table. We do not use a static set
5579 of possible tables sizes anymore. Instead we determine for all
5580 possible reasonable sizes of the table the outcome (i.e., the
5581 number of collisions etc) and choose the best solution. The
5582 weighting functions are not too simple to allow the table to grow
5583 without bounds. Instead one of the weighting factors is the size.
5584 Therefore the result is always a good payoff between few collisions
5585 (= short chain lengths) and table size. */
5586 static size_t
5587 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5588 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5589 unsigned long int nsyms,
5590 int gnu_hash)
5591 {
5592 size_t best_size = 0;
5593 unsigned long int i;
5594
5595 /* We have a problem here. The following code to optimize the table
5596 size requires an integer type with more the 32 bits. If
5597 BFD_HOST_U_64_BIT is set we know about such a type. */
5598 #ifdef BFD_HOST_U_64_BIT
5599 if (info->optimize)
5600 {
5601 size_t minsize;
5602 size_t maxsize;
5603 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5604 bfd *dynobj = elf_hash_table (info)->dynobj;
5605 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5606 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5607 unsigned long int *counts;
5608 bfd_size_type amt;
5609 unsigned int no_improvement_count = 0;
5610
5611 /* Possible optimization parameters: if we have NSYMS symbols we say
5612 that the hashing table must at least have NSYMS/4 and at most
5613 2*NSYMS buckets. */
5614 minsize = nsyms / 4;
5615 if (minsize == 0)
5616 minsize = 1;
5617 best_size = maxsize = nsyms * 2;
5618 if (gnu_hash)
5619 {
5620 if (minsize < 2)
5621 minsize = 2;
5622 if ((best_size & 31) == 0)
5623 ++best_size;
5624 }
5625
5626 /* Create array where we count the collisions in. We must use bfd_malloc
5627 since the size could be large. */
5628 amt = maxsize;
5629 amt *= sizeof (unsigned long int);
5630 counts = (unsigned long int *) bfd_malloc (amt);
5631 if (counts == NULL)
5632 return 0;
5633
5634 /* Compute the "optimal" size for the hash table. The criteria is a
5635 minimal chain length. The minor criteria is (of course) the size
5636 of the table. */
5637 for (i = minsize; i < maxsize; ++i)
5638 {
5639 /* Walk through the array of hashcodes and count the collisions. */
5640 BFD_HOST_U_64_BIT max;
5641 unsigned long int j;
5642 unsigned long int fact;
5643
5644 if (gnu_hash && (i & 31) == 0)
5645 continue;
5646
5647 memset (counts, '\0', i * sizeof (unsigned long int));
5648
5649 /* Determine how often each hash bucket is used. */
5650 for (j = 0; j < nsyms; ++j)
5651 ++counts[hashcodes[j] % i];
5652
5653 /* For the weight function we need some information about the
5654 pagesize on the target. This is information need not be 100%
5655 accurate. Since this information is not available (so far) we
5656 define it here to a reasonable default value. If it is crucial
5657 to have a better value some day simply define this value. */
5658 # ifndef BFD_TARGET_PAGESIZE
5659 # define BFD_TARGET_PAGESIZE (4096)
5660 # endif
5661
5662 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5663 and the chains. */
5664 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5665
5666 # if 1
5667 /* Variant 1: optimize for short chains. We add the squares
5668 of all the chain lengths (which favors many small chain
5669 over a few long chains). */
5670 for (j = 0; j < i; ++j)
5671 max += counts[j] * counts[j];
5672
5673 /* This adds penalties for the overall size of the table. */
5674 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5675 max *= fact * fact;
5676 # else
5677 /* Variant 2: Optimize a lot more for small table. Here we
5678 also add squares of the size but we also add penalties for
5679 empty slots (the +1 term). */
5680 for (j = 0; j < i; ++j)
5681 max += (1 + counts[j]) * (1 + counts[j]);
5682
5683 /* The overall size of the table is considered, but not as
5684 strong as in variant 1, where it is squared. */
5685 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5686 max *= fact;
5687 # endif
5688
5689 /* Compare with current best results. */
5690 if (max < best_chlen)
5691 {
5692 best_chlen = max;
5693 best_size = i;
5694 no_improvement_count = 0;
5695 }
5696 /* PR 11843: Avoid futile long searches for the best bucket size
5697 when there are a large number of symbols. */
5698 else if (++no_improvement_count == 100)
5699 break;
5700 }
5701
5702 free (counts);
5703 }
5704 else
5705 #endif /* defined (BFD_HOST_U_64_BIT) */
5706 {
5707 /* This is the fallback solution if no 64bit type is available or if we
5708 are not supposed to spend much time on optimizations. We select the
5709 bucket count using a fixed set of numbers. */
5710 for (i = 0; elf_buckets[i] != 0; i++)
5711 {
5712 best_size = elf_buckets[i];
5713 if (nsyms < elf_buckets[i + 1])
5714 break;
5715 }
5716 if (gnu_hash && best_size < 2)
5717 best_size = 2;
5718 }
5719
5720 return best_size;
5721 }
5722
5723 /* Size any SHT_GROUP section for ld -r. */
5724
5725 bfd_boolean
5726 _bfd_elf_size_group_sections (struct bfd_link_info *info)
5727 {
5728 bfd *ibfd;
5729
5730 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5731 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5732 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5733 return FALSE;
5734 return TRUE;
5735 }
5736
5737 /* Set a default stack segment size. The value in INFO wins. If it
5738 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5739 undefined it is initialized. */
5740
5741 bfd_boolean
5742 bfd_elf_stack_segment_size (bfd *output_bfd,
5743 struct bfd_link_info *info,
5744 const char *legacy_symbol,
5745 bfd_vma default_size)
5746 {
5747 struct elf_link_hash_entry *h = NULL;
5748
5749 /* Look for legacy symbol. */
5750 if (legacy_symbol)
5751 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5752 FALSE, FALSE, FALSE);
5753 if (h && (h->root.type == bfd_link_hash_defined
5754 || h->root.type == bfd_link_hash_defweak)
5755 && h->def_regular
5756 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5757 {
5758 /* The symbol has no type if specified on the command line. */
5759 h->type = STT_OBJECT;
5760 if (info->stacksize)
5761 (*_bfd_error_handler) (_("%B: stack size specified and %s set"),
5762 output_bfd, legacy_symbol);
5763 else if (h->root.u.def.section != bfd_abs_section_ptr)
5764 (*_bfd_error_handler) (_("%B: %s not absolute"),
5765 output_bfd, legacy_symbol);
5766 else
5767 info->stacksize = h->root.u.def.value;
5768 }
5769
5770 if (!info->stacksize)
5771 /* If the user didn't set a size, or explicitly inhibit the
5772 size, set it now. */
5773 info->stacksize = default_size;
5774
5775 /* Provide the legacy symbol, if it is referenced. */
5776 if (h && (h->root.type == bfd_link_hash_undefined
5777 || h->root.type == bfd_link_hash_undefweak))
5778 {
5779 struct bfd_link_hash_entry *bh = NULL;
5780
5781 if (!(_bfd_generic_link_add_one_symbol
5782 (info, output_bfd, legacy_symbol,
5783 BSF_GLOBAL, bfd_abs_section_ptr,
5784 info->stacksize >= 0 ? info->stacksize : 0,
5785 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5786 return FALSE;
5787
5788 h = (struct elf_link_hash_entry *) bh;
5789 h->def_regular = 1;
5790 h->type = STT_OBJECT;
5791 }
5792
5793 return TRUE;
5794 }
5795
5796 /* Set up the sizes and contents of the ELF dynamic sections. This is
5797 called by the ELF linker emulation before_allocation routine. We
5798 must set the sizes of the sections before the linker sets the
5799 addresses of the various sections. */
5800
5801 bfd_boolean
5802 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5803 const char *soname,
5804 const char *rpath,
5805 const char *filter_shlib,
5806 const char *audit,
5807 const char *depaudit,
5808 const char * const *auxiliary_filters,
5809 struct bfd_link_info *info,
5810 asection **sinterpptr)
5811 {
5812 size_t soname_indx;
5813 bfd *dynobj;
5814 const struct elf_backend_data *bed;
5815 struct elf_info_failed asvinfo;
5816
5817 *sinterpptr = NULL;
5818
5819 soname_indx = (size_t) -1;
5820
5821 if (!is_elf_hash_table (info->hash))
5822 return TRUE;
5823
5824 bed = get_elf_backend_data (output_bfd);
5825
5826 /* Any syms created from now on start with -1 in
5827 got.refcount/offset and plt.refcount/offset. */
5828 elf_hash_table (info)->init_got_refcount
5829 = elf_hash_table (info)->init_got_offset;
5830 elf_hash_table (info)->init_plt_refcount
5831 = elf_hash_table (info)->init_plt_offset;
5832
5833 if (bfd_link_relocatable (info)
5834 && !_bfd_elf_size_group_sections (info))
5835 return FALSE;
5836
5837 /* The backend may have to create some sections regardless of whether
5838 we're dynamic or not. */
5839 if (bed->elf_backend_always_size_sections
5840 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5841 return FALSE;
5842
5843 /* Determine any GNU_STACK segment requirements, after the backend
5844 has had a chance to set a default segment size. */
5845 if (info->execstack)
5846 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
5847 else if (info->noexecstack)
5848 elf_stack_flags (output_bfd) = PF_R | PF_W;
5849 else
5850 {
5851 bfd *inputobj;
5852 asection *notesec = NULL;
5853 int exec = 0;
5854
5855 for (inputobj = info->input_bfds;
5856 inputobj;
5857 inputobj = inputobj->link.next)
5858 {
5859 asection *s;
5860
5861 if (inputobj->flags
5862 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5863 continue;
5864 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5865 if (s)
5866 {
5867 if (s->flags & SEC_CODE)
5868 exec = PF_X;
5869 notesec = s;
5870 }
5871 else if (bed->default_execstack)
5872 exec = PF_X;
5873 }
5874 if (notesec || info->stacksize > 0)
5875 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
5876 if (notesec && exec && bfd_link_relocatable (info)
5877 && notesec->output_section != bfd_abs_section_ptr)
5878 notesec->output_section->flags |= SEC_CODE;
5879 }
5880
5881 dynobj = elf_hash_table (info)->dynobj;
5882
5883 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5884 {
5885 struct elf_info_failed eif;
5886 struct elf_link_hash_entry *h;
5887 asection *dynstr;
5888 struct bfd_elf_version_tree *t;
5889 struct bfd_elf_version_expr *d;
5890 asection *s;
5891 bfd_boolean all_defined;
5892
5893 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
5894 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
5895
5896 if (soname != NULL)
5897 {
5898 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5899 soname, TRUE);
5900 if (soname_indx == (size_t) -1
5901 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5902 return FALSE;
5903 }
5904
5905 if (info->symbolic)
5906 {
5907 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5908 return FALSE;
5909 info->flags |= DF_SYMBOLIC;
5910 }
5911
5912 if (rpath != NULL)
5913 {
5914 size_t indx;
5915 bfd_vma tag;
5916
5917 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5918 TRUE);
5919 if (indx == (size_t) -1)
5920 return FALSE;
5921
5922 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
5923 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
5924 return FALSE;
5925 }
5926
5927 if (filter_shlib != NULL)
5928 {
5929 size_t indx;
5930
5931 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5932 filter_shlib, TRUE);
5933 if (indx == (size_t) -1
5934 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5935 return FALSE;
5936 }
5937
5938 if (auxiliary_filters != NULL)
5939 {
5940 const char * const *p;
5941
5942 for (p = auxiliary_filters; *p != NULL; p++)
5943 {
5944 size_t indx;
5945
5946 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5947 *p, TRUE);
5948 if (indx == (size_t) -1
5949 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5950 return FALSE;
5951 }
5952 }
5953
5954 if (audit != NULL)
5955 {
5956 size_t indx;
5957
5958 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5959 TRUE);
5960 if (indx == (size_t) -1
5961 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5962 return FALSE;
5963 }
5964
5965 if (depaudit != NULL)
5966 {
5967 size_t indx;
5968
5969 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
5970 TRUE);
5971 if (indx == (size_t) -1
5972 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
5973 return FALSE;
5974 }
5975
5976 eif.info = info;
5977 eif.failed = FALSE;
5978
5979 /* If we are supposed to export all symbols into the dynamic symbol
5980 table (this is not the normal case), then do so. */
5981 if (info->export_dynamic
5982 || (bfd_link_executable (info) && info->dynamic))
5983 {
5984 elf_link_hash_traverse (elf_hash_table (info),
5985 _bfd_elf_export_symbol,
5986 &eif);
5987 if (eif.failed)
5988 return FALSE;
5989 }
5990
5991 /* Make all global versions with definition. */
5992 for (t = info->version_info; t != NULL; t = t->next)
5993 for (d = t->globals.list; d != NULL; d = d->next)
5994 if (!d->symver && d->literal)
5995 {
5996 const char *verstr, *name;
5997 size_t namelen, verlen, newlen;
5998 char *newname, *p, leading_char;
5999 struct elf_link_hash_entry *newh;
6000
6001 leading_char = bfd_get_symbol_leading_char (output_bfd);
6002 name = d->pattern;
6003 namelen = strlen (name) + (leading_char != '\0');
6004 verstr = t->name;
6005 verlen = strlen (verstr);
6006 newlen = namelen + verlen + 3;
6007
6008 newname = (char *) bfd_malloc (newlen);
6009 if (newname == NULL)
6010 return FALSE;
6011 newname[0] = leading_char;
6012 memcpy (newname + (leading_char != '\0'), name, namelen);
6013
6014 /* Check the hidden versioned definition. */
6015 p = newname + namelen;
6016 *p++ = ELF_VER_CHR;
6017 memcpy (p, verstr, verlen + 1);
6018 newh = elf_link_hash_lookup (elf_hash_table (info),
6019 newname, FALSE, FALSE,
6020 FALSE);
6021 if (newh == NULL
6022 || (newh->root.type != bfd_link_hash_defined
6023 && newh->root.type != bfd_link_hash_defweak))
6024 {
6025 /* Check the default versioned definition. */
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 }
6032 free (newname);
6033
6034 /* Mark this version if there is a definition and it is
6035 not defined in a shared object. */
6036 if (newh != NULL
6037 && !newh->def_dynamic
6038 && (newh->root.type == bfd_link_hash_defined
6039 || newh->root.type == bfd_link_hash_defweak))
6040 d->symver = 1;
6041 }
6042
6043 /* Attach all the symbols to their version information. */
6044 asvinfo.info = info;
6045 asvinfo.failed = FALSE;
6046
6047 elf_link_hash_traverse (elf_hash_table (info),
6048 _bfd_elf_link_assign_sym_version,
6049 &asvinfo);
6050 if (asvinfo.failed)
6051 return FALSE;
6052
6053 if (!info->allow_undefined_version)
6054 {
6055 /* Check if all global versions have a definition. */
6056 all_defined = TRUE;
6057 for (t = info->version_info; t != NULL; t = t->next)
6058 for (d = t->globals.list; d != NULL; d = d->next)
6059 if (d->literal && !d->symver && !d->script)
6060 {
6061 (*_bfd_error_handler)
6062 (_("%s: undefined version: %s"),
6063 d->pattern, t->name);
6064 all_defined = FALSE;
6065 }
6066
6067 if (!all_defined)
6068 {
6069 bfd_set_error (bfd_error_bad_value);
6070 return FALSE;
6071 }
6072 }
6073
6074 /* Find all symbols which were defined in a dynamic object and make
6075 the backend pick a reasonable value for them. */
6076 elf_link_hash_traverse (elf_hash_table (info),
6077 _bfd_elf_adjust_dynamic_symbol,
6078 &eif);
6079 if (eif.failed)
6080 return FALSE;
6081
6082 /* Add some entries to the .dynamic section. We fill in some of the
6083 values later, in bfd_elf_final_link, but we must add the entries
6084 now so that we know the final size of the .dynamic section. */
6085
6086 /* If there are initialization and/or finalization functions to
6087 call then add the corresponding DT_INIT/DT_FINI entries. */
6088 h = (info->init_function
6089 ? elf_link_hash_lookup (elf_hash_table (info),
6090 info->init_function, FALSE,
6091 FALSE, FALSE)
6092 : NULL);
6093 if (h != NULL
6094 && (h->ref_regular
6095 || h->def_regular))
6096 {
6097 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6098 return FALSE;
6099 }
6100 h = (info->fini_function
6101 ? elf_link_hash_lookup (elf_hash_table (info),
6102 info->fini_function, FALSE,
6103 FALSE, FALSE)
6104 : NULL);
6105 if (h != NULL
6106 && (h->ref_regular
6107 || h->def_regular))
6108 {
6109 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6110 return FALSE;
6111 }
6112
6113 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6114 if (s != NULL && s->linker_has_input)
6115 {
6116 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6117 if (! bfd_link_executable (info))
6118 {
6119 bfd *sub;
6120 asection *o;
6121
6122 for (sub = info->input_bfds; sub != NULL;
6123 sub = sub->link.next)
6124 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
6125 for (o = sub->sections; o != NULL; o = o->next)
6126 if (elf_section_data (o)->this_hdr.sh_type
6127 == SHT_PREINIT_ARRAY)
6128 {
6129 (*_bfd_error_handler)
6130 (_("%B: .preinit_array section is not allowed in DSO"),
6131 sub);
6132 break;
6133 }
6134
6135 bfd_set_error (bfd_error_nonrepresentable_section);
6136 return FALSE;
6137 }
6138
6139 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6140 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6141 return FALSE;
6142 }
6143 s = bfd_get_section_by_name (output_bfd, ".init_array");
6144 if (s != NULL && s->linker_has_input)
6145 {
6146 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6147 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6148 return FALSE;
6149 }
6150 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6151 if (s != NULL && s->linker_has_input)
6152 {
6153 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6154 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6155 return FALSE;
6156 }
6157
6158 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6159 /* If .dynstr is excluded from the link, we don't want any of
6160 these tags. Strictly, we should be checking each section
6161 individually; This quick check covers for the case where
6162 someone does a /DISCARD/ : { *(*) }. */
6163 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6164 {
6165 bfd_size_type strsize;
6166
6167 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6168 if ((info->emit_hash
6169 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6170 || (info->emit_gnu_hash
6171 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6172 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6173 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6174 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6175 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6176 bed->s->sizeof_sym))
6177 return FALSE;
6178 }
6179 }
6180
6181 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6182 return FALSE;
6183
6184 /* The backend must work out the sizes of all the other dynamic
6185 sections. */
6186 if (dynobj != NULL
6187 && bed->elf_backend_size_dynamic_sections != NULL
6188 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6189 return FALSE;
6190
6191 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6192 {
6193 unsigned long section_sym_count;
6194 struct bfd_elf_version_tree *verdefs;
6195 asection *s;
6196
6197 /* Set up the version definition section. */
6198 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6199 BFD_ASSERT (s != NULL);
6200
6201 /* We may have created additional version definitions if we are
6202 just linking a regular application. */
6203 verdefs = info->version_info;
6204
6205 /* Skip anonymous version tag. */
6206 if (verdefs != NULL && verdefs->vernum == 0)
6207 verdefs = verdefs->next;
6208
6209 if (verdefs == NULL && !info->create_default_symver)
6210 s->flags |= SEC_EXCLUDE;
6211 else
6212 {
6213 unsigned int cdefs;
6214 bfd_size_type size;
6215 struct bfd_elf_version_tree *t;
6216 bfd_byte *p;
6217 Elf_Internal_Verdef def;
6218 Elf_Internal_Verdaux defaux;
6219 struct bfd_link_hash_entry *bh;
6220 struct elf_link_hash_entry *h;
6221 const char *name;
6222
6223 cdefs = 0;
6224 size = 0;
6225
6226 /* Make space for the base version. */
6227 size += sizeof (Elf_External_Verdef);
6228 size += sizeof (Elf_External_Verdaux);
6229 ++cdefs;
6230
6231 /* Make space for the default version. */
6232 if (info->create_default_symver)
6233 {
6234 size += sizeof (Elf_External_Verdef);
6235 ++cdefs;
6236 }
6237
6238 for (t = verdefs; t != NULL; t = t->next)
6239 {
6240 struct bfd_elf_version_deps *n;
6241
6242 /* Don't emit base version twice. */
6243 if (t->vernum == 0)
6244 continue;
6245
6246 size += sizeof (Elf_External_Verdef);
6247 size += sizeof (Elf_External_Verdaux);
6248 ++cdefs;
6249
6250 for (n = t->deps; n != NULL; n = n->next)
6251 size += sizeof (Elf_External_Verdaux);
6252 }
6253
6254 s->size = size;
6255 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6256 if (s->contents == NULL && s->size != 0)
6257 return FALSE;
6258
6259 /* Fill in the version definition section. */
6260
6261 p = s->contents;
6262
6263 def.vd_version = VER_DEF_CURRENT;
6264 def.vd_flags = VER_FLG_BASE;
6265 def.vd_ndx = 1;
6266 def.vd_cnt = 1;
6267 if (info->create_default_symver)
6268 {
6269 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6270 def.vd_next = sizeof (Elf_External_Verdef);
6271 }
6272 else
6273 {
6274 def.vd_aux = sizeof (Elf_External_Verdef);
6275 def.vd_next = (sizeof (Elf_External_Verdef)
6276 + sizeof (Elf_External_Verdaux));
6277 }
6278
6279 if (soname_indx != (size_t) -1)
6280 {
6281 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6282 soname_indx);
6283 def.vd_hash = bfd_elf_hash (soname);
6284 defaux.vda_name = soname_indx;
6285 name = soname;
6286 }
6287 else
6288 {
6289 size_t indx;
6290
6291 name = lbasename (output_bfd->filename);
6292 def.vd_hash = bfd_elf_hash (name);
6293 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6294 name, FALSE);
6295 if (indx == (size_t) -1)
6296 return FALSE;
6297 defaux.vda_name = indx;
6298 }
6299 defaux.vda_next = 0;
6300
6301 _bfd_elf_swap_verdef_out (output_bfd, &def,
6302 (Elf_External_Verdef *) p);
6303 p += sizeof (Elf_External_Verdef);
6304 if (info->create_default_symver)
6305 {
6306 /* Add a symbol representing this version. */
6307 bh = NULL;
6308 if (! (_bfd_generic_link_add_one_symbol
6309 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6310 0, NULL, FALSE,
6311 get_elf_backend_data (dynobj)->collect, &bh)))
6312 return FALSE;
6313 h = (struct elf_link_hash_entry *) bh;
6314 h->non_elf = 0;
6315 h->def_regular = 1;
6316 h->type = STT_OBJECT;
6317 h->verinfo.vertree = NULL;
6318
6319 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6320 return FALSE;
6321
6322 /* Create a duplicate of the base version with the same
6323 aux block, but different flags. */
6324 def.vd_flags = 0;
6325 def.vd_ndx = 2;
6326 def.vd_aux = sizeof (Elf_External_Verdef);
6327 if (verdefs)
6328 def.vd_next = (sizeof (Elf_External_Verdef)
6329 + sizeof (Elf_External_Verdaux));
6330 else
6331 def.vd_next = 0;
6332 _bfd_elf_swap_verdef_out (output_bfd, &def,
6333 (Elf_External_Verdef *) p);
6334 p += sizeof (Elf_External_Verdef);
6335 }
6336 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6337 (Elf_External_Verdaux *) p);
6338 p += sizeof (Elf_External_Verdaux);
6339
6340 for (t = verdefs; t != NULL; t = t->next)
6341 {
6342 unsigned int cdeps;
6343 struct bfd_elf_version_deps *n;
6344
6345 /* Don't emit the base version twice. */
6346 if (t->vernum == 0)
6347 continue;
6348
6349 cdeps = 0;
6350 for (n = t->deps; n != NULL; n = n->next)
6351 ++cdeps;
6352
6353 /* Add a symbol representing this version. */
6354 bh = NULL;
6355 if (! (_bfd_generic_link_add_one_symbol
6356 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6357 0, NULL, FALSE,
6358 get_elf_backend_data (dynobj)->collect, &bh)))
6359 return FALSE;
6360 h = (struct elf_link_hash_entry *) bh;
6361 h->non_elf = 0;
6362 h->def_regular = 1;
6363 h->type = STT_OBJECT;
6364 h->verinfo.vertree = t;
6365
6366 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6367 return FALSE;
6368
6369 def.vd_version = VER_DEF_CURRENT;
6370 def.vd_flags = 0;
6371 if (t->globals.list == NULL
6372 && t->locals.list == NULL
6373 && ! t->used)
6374 def.vd_flags |= VER_FLG_WEAK;
6375 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6376 def.vd_cnt = cdeps + 1;
6377 def.vd_hash = bfd_elf_hash (t->name);
6378 def.vd_aux = sizeof (Elf_External_Verdef);
6379 def.vd_next = 0;
6380
6381 /* If a basever node is next, it *must* be the last node in
6382 the chain, otherwise Verdef construction breaks. */
6383 if (t->next != NULL && t->next->vernum == 0)
6384 BFD_ASSERT (t->next->next == NULL);
6385
6386 if (t->next != NULL && t->next->vernum != 0)
6387 def.vd_next = (sizeof (Elf_External_Verdef)
6388 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6389
6390 _bfd_elf_swap_verdef_out (output_bfd, &def,
6391 (Elf_External_Verdef *) p);
6392 p += sizeof (Elf_External_Verdef);
6393
6394 defaux.vda_name = h->dynstr_index;
6395 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6396 h->dynstr_index);
6397 defaux.vda_next = 0;
6398 if (t->deps != NULL)
6399 defaux.vda_next = sizeof (Elf_External_Verdaux);
6400 t->name_indx = defaux.vda_name;
6401
6402 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6403 (Elf_External_Verdaux *) p);
6404 p += sizeof (Elf_External_Verdaux);
6405
6406 for (n = t->deps; n != NULL; n = n->next)
6407 {
6408 if (n->version_needed == NULL)
6409 {
6410 /* This can happen if there was an error in the
6411 version script. */
6412 defaux.vda_name = 0;
6413 }
6414 else
6415 {
6416 defaux.vda_name = n->version_needed->name_indx;
6417 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6418 defaux.vda_name);
6419 }
6420 if (n->next == NULL)
6421 defaux.vda_next = 0;
6422 else
6423 defaux.vda_next = sizeof (Elf_External_Verdaux);
6424
6425 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6426 (Elf_External_Verdaux *) p);
6427 p += sizeof (Elf_External_Verdaux);
6428 }
6429 }
6430
6431 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6432 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6433 return FALSE;
6434
6435 elf_tdata (output_bfd)->cverdefs = cdefs;
6436 }
6437
6438 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6439 {
6440 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6441 return FALSE;
6442 }
6443 else if (info->flags & DF_BIND_NOW)
6444 {
6445 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6446 return FALSE;
6447 }
6448
6449 if (info->flags_1)
6450 {
6451 if (bfd_link_executable (info))
6452 info->flags_1 &= ~ (DF_1_INITFIRST
6453 | DF_1_NODELETE
6454 | DF_1_NOOPEN);
6455 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6456 return FALSE;
6457 }
6458
6459 /* Work out the size of the version reference section. */
6460
6461 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6462 BFD_ASSERT (s != NULL);
6463 {
6464 struct elf_find_verdep_info sinfo;
6465
6466 sinfo.info = info;
6467 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6468 if (sinfo.vers == 0)
6469 sinfo.vers = 1;
6470 sinfo.failed = FALSE;
6471
6472 elf_link_hash_traverse (elf_hash_table (info),
6473 _bfd_elf_link_find_version_dependencies,
6474 &sinfo);
6475 if (sinfo.failed)
6476 return FALSE;
6477
6478 if (elf_tdata (output_bfd)->verref == NULL)
6479 s->flags |= SEC_EXCLUDE;
6480 else
6481 {
6482 Elf_Internal_Verneed *t;
6483 unsigned int size;
6484 unsigned int crefs;
6485 bfd_byte *p;
6486
6487 /* Build the version dependency section. */
6488 size = 0;
6489 crefs = 0;
6490 for (t = elf_tdata (output_bfd)->verref;
6491 t != NULL;
6492 t = t->vn_nextref)
6493 {
6494 Elf_Internal_Vernaux *a;
6495
6496 size += sizeof (Elf_External_Verneed);
6497 ++crefs;
6498 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6499 size += sizeof (Elf_External_Vernaux);
6500 }
6501
6502 s->size = size;
6503 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6504 if (s->contents == NULL)
6505 return FALSE;
6506
6507 p = s->contents;
6508 for (t = elf_tdata (output_bfd)->verref;
6509 t != NULL;
6510 t = t->vn_nextref)
6511 {
6512 unsigned int caux;
6513 Elf_Internal_Vernaux *a;
6514 size_t indx;
6515
6516 caux = 0;
6517 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6518 ++caux;
6519
6520 t->vn_version = VER_NEED_CURRENT;
6521 t->vn_cnt = caux;
6522 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6523 elf_dt_name (t->vn_bfd) != NULL
6524 ? elf_dt_name (t->vn_bfd)
6525 : lbasename (t->vn_bfd->filename),
6526 FALSE);
6527 if (indx == (size_t) -1)
6528 return FALSE;
6529 t->vn_file = indx;
6530 t->vn_aux = sizeof (Elf_External_Verneed);
6531 if (t->vn_nextref == NULL)
6532 t->vn_next = 0;
6533 else
6534 t->vn_next = (sizeof (Elf_External_Verneed)
6535 + caux * sizeof (Elf_External_Vernaux));
6536
6537 _bfd_elf_swap_verneed_out (output_bfd, t,
6538 (Elf_External_Verneed *) p);
6539 p += sizeof (Elf_External_Verneed);
6540
6541 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6542 {
6543 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6544 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6545 a->vna_nodename, FALSE);
6546 if (indx == (size_t) -1)
6547 return FALSE;
6548 a->vna_name = indx;
6549 if (a->vna_nextptr == NULL)
6550 a->vna_next = 0;
6551 else
6552 a->vna_next = sizeof (Elf_External_Vernaux);
6553
6554 _bfd_elf_swap_vernaux_out (output_bfd, a,
6555 (Elf_External_Vernaux *) p);
6556 p += sizeof (Elf_External_Vernaux);
6557 }
6558 }
6559
6560 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6561 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6562 return FALSE;
6563
6564 elf_tdata (output_bfd)->cverrefs = crefs;
6565 }
6566 }
6567
6568 if ((elf_tdata (output_bfd)->cverrefs == 0
6569 && elf_tdata (output_bfd)->cverdefs == 0)
6570 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6571 &section_sym_count) == 0)
6572 {
6573 s = bfd_get_linker_section (dynobj, ".gnu.version");
6574 s->flags |= SEC_EXCLUDE;
6575 }
6576 }
6577 return TRUE;
6578 }
6579
6580 /* Find the first non-excluded output section. We'll use its
6581 section symbol for some emitted relocs. */
6582 void
6583 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6584 {
6585 asection *s;
6586
6587 for (s = output_bfd->sections; s != NULL; s = s->next)
6588 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6589 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6590 {
6591 elf_hash_table (info)->text_index_section = s;
6592 break;
6593 }
6594 }
6595
6596 /* Find two non-excluded output sections, one for code, one for data.
6597 We'll use their section symbols for some emitted relocs. */
6598 void
6599 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6600 {
6601 asection *s;
6602
6603 /* Data first, since setting text_index_section changes
6604 _bfd_elf_link_omit_section_dynsym. */
6605 for (s = output_bfd->sections; s != NULL; s = s->next)
6606 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6607 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6608 {
6609 elf_hash_table (info)->data_index_section = s;
6610 break;
6611 }
6612
6613 for (s = output_bfd->sections; s != NULL; s = s->next)
6614 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6615 == (SEC_ALLOC | SEC_READONLY))
6616 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6617 {
6618 elf_hash_table (info)->text_index_section = s;
6619 break;
6620 }
6621
6622 if (elf_hash_table (info)->text_index_section == NULL)
6623 elf_hash_table (info)->text_index_section
6624 = elf_hash_table (info)->data_index_section;
6625 }
6626
6627 bfd_boolean
6628 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6629 {
6630 const struct elf_backend_data *bed;
6631
6632 if (!is_elf_hash_table (info->hash))
6633 return TRUE;
6634
6635 bed = get_elf_backend_data (output_bfd);
6636 (*bed->elf_backend_init_index_section) (output_bfd, info);
6637
6638 if (elf_hash_table (info)->dynamic_sections_created)
6639 {
6640 bfd *dynobj;
6641 asection *s;
6642 bfd_size_type dynsymcount;
6643 unsigned long section_sym_count;
6644 unsigned int dtagcount;
6645
6646 dynobj = elf_hash_table (info)->dynobj;
6647
6648 /* Assign dynsym indicies. In a shared library we generate a
6649 section symbol for each output section, which come first.
6650 Next come all of the back-end allocated local dynamic syms,
6651 followed by the rest of the global symbols. */
6652
6653 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6654 &section_sym_count);
6655
6656 /* Work out the size of the symbol version section. */
6657 s = bfd_get_linker_section (dynobj, ".gnu.version");
6658 BFD_ASSERT (s != NULL);
6659 if ((s->flags & SEC_EXCLUDE) == 0)
6660 {
6661 s->size = dynsymcount * sizeof (Elf_External_Versym);
6662 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6663 if (s->contents == NULL)
6664 return FALSE;
6665
6666 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6667 return FALSE;
6668 }
6669
6670 /* Set the size of the .dynsym and .hash sections. We counted
6671 the number of dynamic symbols in elf_link_add_object_symbols.
6672 We will build the contents of .dynsym and .hash when we build
6673 the final symbol table, because until then we do not know the
6674 correct value to give the symbols. We built the .dynstr
6675 section as we went along in elf_link_add_object_symbols. */
6676 s = elf_hash_table (info)->dynsym;
6677 BFD_ASSERT (s != NULL);
6678 s->size = dynsymcount * bed->s->sizeof_sym;
6679
6680 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6681 if (s->contents == NULL)
6682 return FALSE;
6683
6684 /* The first entry in .dynsym is a dummy symbol. Clear all the
6685 section syms, in case we don't output them all. */
6686 ++section_sym_count;
6687 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6688
6689 elf_hash_table (info)->bucketcount = 0;
6690
6691 /* Compute the size of the hashing table. As a side effect this
6692 computes the hash values for all the names we export. */
6693 if (info->emit_hash)
6694 {
6695 unsigned long int *hashcodes;
6696 struct hash_codes_info hashinf;
6697 bfd_size_type amt;
6698 unsigned long int nsyms;
6699 size_t bucketcount;
6700 size_t hash_entry_size;
6701
6702 /* Compute the hash values for all exported symbols. At the same
6703 time store the values in an array so that we could use them for
6704 optimizations. */
6705 amt = dynsymcount * sizeof (unsigned long int);
6706 hashcodes = (unsigned long int *) bfd_malloc (amt);
6707 if (hashcodes == NULL)
6708 return FALSE;
6709 hashinf.hashcodes = hashcodes;
6710 hashinf.error = FALSE;
6711
6712 /* Put all hash values in HASHCODES. */
6713 elf_link_hash_traverse (elf_hash_table (info),
6714 elf_collect_hash_codes, &hashinf);
6715 if (hashinf.error)
6716 {
6717 free (hashcodes);
6718 return FALSE;
6719 }
6720
6721 nsyms = hashinf.hashcodes - hashcodes;
6722 bucketcount
6723 = compute_bucket_count (info, hashcodes, nsyms, 0);
6724 free (hashcodes);
6725
6726 if (bucketcount == 0)
6727 return FALSE;
6728
6729 elf_hash_table (info)->bucketcount = bucketcount;
6730
6731 s = bfd_get_linker_section (dynobj, ".hash");
6732 BFD_ASSERT (s != NULL);
6733 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6734 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6735 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6736 if (s->contents == NULL)
6737 return FALSE;
6738
6739 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6740 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6741 s->contents + hash_entry_size);
6742 }
6743
6744 if (info->emit_gnu_hash)
6745 {
6746 size_t i, cnt;
6747 unsigned char *contents;
6748 struct collect_gnu_hash_codes cinfo;
6749 bfd_size_type amt;
6750 size_t bucketcount;
6751
6752 memset (&cinfo, 0, sizeof (cinfo));
6753
6754 /* Compute the hash values for all exported symbols. At the same
6755 time store the values in an array so that we could use them for
6756 optimizations. */
6757 amt = dynsymcount * 2 * sizeof (unsigned long int);
6758 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
6759 if (cinfo.hashcodes == NULL)
6760 return FALSE;
6761
6762 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6763 cinfo.min_dynindx = -1;
6764 cinfo.output_bfd = output_bfd;
6765 cinfo.bed = bed;
6766
6767 /* Put all hash values in HASHCODES. */
6768 elf_link_hash_traverse (elf_hash_table (info),
6769 elf_collect_gnu_hash_codes, &cinfo);
6770 if (cinfo.error)
6771 {
6772 free (cinfo.hashcodes);
6773 return FALSE;
6774 }
6775
6776 bucketcount
6777 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6778
6779 if (bucketcount == 0)
6780 {
6781 free (cinfo.hashcodes);
6782 return FALSE;
6783 }
6784
6785 s = bfd_get_linker_section (dynobj, ".gnu.hash");
6786 BFD_ASSERT (s != NULL);
6787
6788 if (cinfo.nsyms == 0)
6789 {
6790 /* Empty .gnu.hash section is special. */
6791 BFD_ASSERT (cinfo.min_dynindx == -1);
6792 free (cinfo.hashcodes);
6793 s->size = 5 * 4 + bed->s->arch_size / 8;
6794 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6795 if (contents == NULL)
6796 return FALSE;
6797 s->contents = contents;
6798 /* 1 empty bucket. */
6799 bfd_put_32 (output_bfd, 1, contents);
6800 /* SYMIDX above the special symbol 0. */
6801 bfd_put_32 (output_bfd, 1, contents + 4);
6802 /* Just one word for bitmask. */
6803 bfd_put_32 (output_bfd, 1, contents + 8);
6804 /* Only hash fn bloom filter. */
6805 bfd_put_32 (output_bfd, 0, contents + 12);
6806 /* No hashes are valid - empty bitmask. */
6807 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6808 /* No hashes in the only bucket. */
6809 bfd_put_32 (output_bfd, 0,
6810 contents + 16 + bed->s->arch_size / 8);
6811 }
6812 else
6813 {
6814 unsigned long int maskwords, maskbitslog2, x;
6815 BFD_ASSERT (cinfo.min_dynindx != -1);
6816
6817 x = cinfo.nsyms;
6818 maskbitslog2 = 1;
6819 while ((x >>= 1) != 0)
6820 ++maskbitslog2;
6821 if (maskbitslog2 < 3)
6822 maskbitslog2 = 5;
6823 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6824 maskbitslog2 = maskbitslog2 + 3;
6825 else
6826 maskbitslog2 = maskbitslog2 + 2;
6827 if (bed->s->arch_size == 64)
6828 {
6829 if (maskbitslog2 == 5)
6830 maskbitslog2 = 6;
6831 cinfo.shift1 = 6;
6832 }
6833 else
6834 cinfo.shift1 = 5;
6835 cinfo.mask = (1 << cinfo.shift1) - 1;
6836 cinfo.shift2 = maskbitslog2;
6837 cinfo.maskbits = 1 << maskbitslog2;
6838 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6839 amt = bucketcount * sizeof (unsigned long int) * 2;
6840 amt += maskwords * sizeof (bfd_vma);
6841 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
6842 if (cinfo.bitmask == NULL)
6843 {
6844 free (cinfo.hashcodes);
6845 return FALSE;
6846 }
6847
6848 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
6849 cinfo.indx = cinfo.counts + bucketcount;
6850 cinfo.symindx = dynsymcount - cinfo.nsyms;
6851 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6852
6853 /* Determine how often each hash bucket is used. */
6854 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6855 for (i = 0; i < cinfo.nsyms; ++i)
6856 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6857
6858 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6859 if (cinfo.counts[i] != 0)
6860 {
6861 cinfo.indx[i] = cnt;
6862 cnt += cinfo.counts[i];
6863 }
6864 BFD_ASSERT (cnt == dynsymcount);
6865 cinfo.bucketcount = bucketcount;
6866 cinfo.local_indx = cinfo.min_dynindx;
6867
6868 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6869 s->size += cinfo.maskbits / 8;
6870 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6871 if (contents == NULL)
6872 {
6873 free (cinfo.bitmask);
6874 free (cinfo.hashcodes);
6875 return FALSE;
6876 }
6877
6878 s->contents = contents;
6879 bfd_put_32 (output_bfd, bucketcount, contents);
6880 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6881 bfd_put_32 (output_bfd, maskwords, contents + 8);
6882 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6883 contents += 16 + cinfo.maskbits / 8;
6884
6885 for (i = 0; i < bucketcount; ++i)
6886 {
6887 if (cinfo.counts[i] == 0)
6888 bfd_put_32 (output_bfd, 0, contents);
6889 else
6890 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6891 contents += 4;
6892 }
6893
6894 cinfo.contents = contents;
6895
6896 /* Renumber dynamic symbols, populate .gnu.hash section. */
6897 elf_link_hash_traverse (elf_hash_table (info),
6898 elf_renumber_gnu_hash_syms, &cinfo);
6899
6900 contents = s->contents + 16;
6901 for (i = 0; i < maskwords; ++i)
6902 {
6903 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6904 contents);
6905 contents += bed->s->arch_size / 8;
6906 }
6907
6908 free (cinfo.bitmask);
6909 free (cinfo.hashcodes);
6910 }
6911 }
6912
6913 s = bfd_get_linker_section (dynobj, ".dynstr");
6914 BFD_ASSERT (s != NULL);
6915
6916 elf_finalize_dynstr (output_bfd, info);
6917
6918 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6919
6920 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6921 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6922 return FALSE;
6923 }
6924
6925 return TRUE;
6926 }
6927 \f
6928 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
6929
6930 static void
6931 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6932 asection *sec)
6933 {
6934 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
6935 sec->sec_info_type = SEC_INFO_TYPE_NONE;
6936 }
6937
6938 /* Finish SHF_MERGE section merging. */
6939
6940 bfd_boolean
6941 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
6942 {
6943 bfd *ibfd;
6944 asection *sec;
6945
6946 if (!is_elf_hash_table (info->hash))
6947 return FALSE;
6948
6949 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6950 if ((ibfd->flags & DYNAMIC) == 0
6951 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6952 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
6953 == get_elf_backend_data (obfd)->s->elfclass))
6954 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6955 if ((sec->flags & SEC_MERGE) != 0
6956 && !bfd_is_abs_section (sec->output_section))
6957 {
6958 struct bfd_elf_section_data *secdata;
6959
6960 secdata = elf_section_data (sec);
6961 if (! _bfd_add_merge_section (obfd,
6962 &elf_hash_table (info)->merge_info,
6963 sec, &secdata->sec_info))
6964 return FALSE;
6965 else if (secdata->sec_info)
6966 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
6967 }
6968
6969 if (elf_hash_table (info)->merge_info != NULL)
6970 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
6971 merge_sections_remove_hook);
6972 return TRUE;
6973 }
6974
6975 /* Create an entry in an ELF linker hash table. */
6976
6977 struct bfd_hash_entry *
6978 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6979 struct bfd_hash_table *table,
6980 const char *string)
6981 {
6982 /* Allocate the structure if it has not already been allocated by a
6983 subclass. */
6984 if (entry == NULL)
6985 {
6986 entry = (struct bfd_hash_entry *)
6987 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
6988 if (entry == NULL)
6989 return entry;
6990 }
6991
6992 /* Call the allocation method of the superclass. */
6993 entry = _bfd_link_hash_newfunc (entry, table, string);
6994 if (entry != NULL)
6995 {
6996 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6997 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6998
6999 /* Set local fields. */
7000 ret->indx = -1;
7001 ret->dynindx = -1;
7002 ret->got = htab->init_got_refcount;
7003 ret->plt = htab->init_plt_refcount;
7004 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7005 - offsetof (struct elf_link_hash_entry, size)));
7006 /* Assume that we have been called by a non-ELF symbol reader.
7007 This flag is then reset by the code which reads an ELF input
7008 file. This ensures that a symbol created by a non-ELF symbol
7009 reader will have the flag set correctly. */
7010 ret->non_elf = 1;
7011 }
7012
7013 return entry;
7014 }
7015
7016 /* Copy data from an indirect symbol to its direct symbol, hiding the
7017 old indirect symbol. Also used for copying flags to a weakdef. */
7018
7019 void
7020 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7021 struct elf_link_hash_entry *dir,
7022 struct elf_link_hash_entry *ind)
7023 {
7024 struct elf_link_hash_table *htab;
7025
7026 /* Copy down any references that we may have already seen to the
7027 symbol which just became indirect if DIR isn't a hidden versioned
7028 symbol. */
7029
7030 if (dir->versioned != versioned_hidden)
7031 {
7032 dir->ref_dynamic |= ind->ref_dynamic;
7033 dir->ref_regular |= ind->ref_regular;
7034 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7035 dir->non_got_ref |= ind->non_got_ref;
7036 dir->needs_plt |= ind->needs_plt;
7037 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7038 }
7039
7040 if (ind->root.type != bfd_link_hash_indirect)
7041 return;
7042
7043 /* Copy over the global and procedure linkage table refcount entries.
7044 These may have been already set up by a check_relocs routine. */
7045 htab = elf_hash_table (info);
7046 if (ind->got.refcount > htab->init_got_refcount.refcount)
7047 {
7048 if (dir->got.refcount < 0)
7049 dir->got.refcount = 0;
7050 dir->got.refcount += ind->got.refcount;
7051 ind->got.refcount = htab->init_got_refcount.refcount;
7052 }
7053
7054 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7055 {
7056 if (dir->plt.refcount < 0)
7057 dir->plt.refcount = 0;
7058 dir->plt.refcount += ind->plt.refcount;
7059 ind->plt.refcount = htab->init_plt_refcount.refcount;
7060 }
7061
7062 if (ind->dynindx != -1)
7063 {
7064 if (dir->dynindx != -1)
7065 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7066 dir->dynindx = ind->dynindx;
7067 dir->dynstr_index = ind->dynstr_index;
7068 ind->dynindx = -1;
7069 ind->dynstr_index = 0;
7070 }
7071 }
7072
7073 void
7074 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7075 struct elf_link_hash_entry *h,
7076 bfd_boolean force_local)
7077 {
7078 /* STT_GNU_IFUNC symbol must go through PLT. */
7079 if (h->type != STT_GNU_IFUNC)
7080 {
7081 h->plt = elf_hash_table (info)->init_plt_offset;
7082 h->needs_plt = 0;
7083 }
7084 if (force_local)
7085 {
7086 h->forced_local = 1;
7087 if (h->dynindx != -1)
7088 {
7089 h->dynindx = -1;
7090 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7091 h->dynstr_index);
7092 }
7093 }
7094 }
7095
7096 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7097 caller. */
7098
7099 bfd_boolean
7100 _bfd_elf_link_hash_table_init
7101 (struct elf_link_hash_table *table,
7102 bfd *abfd,
7103 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7104 struct bfd_hash_table *,
7105 const char *),
7106 unsigned int entsize,
7107 enum elf_target_id target_id)
7108 {
7109 bfd_boolean ret;
7110 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7111
7112 table->init_got_refcount.refcount = can_refcount - 1;
7113 table->init_plt_refcount.refcount = can_refcount - 1;
7114 table->init_got_offset.offset = -(bfd_vma) 1;
7115 table->init_plt_offset.offset = -(bfd_vma) 1;
7116 /* The first dynamic symbol is a dummy. */
7117 table->dynsymcount = 1;
7118
7119 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7120
7121 table->root.type = bfd_link_elf_hash_table;
7122 table->hash_table_id = target_id;
7123
7124 return ret;
7125 }
7126
7127 /* Create an ELF linker hash table. */
7128
7129 struct bfd_link_hash_table *
7130 _bfd_elf_link_hash_table_create (bfd *abfd)
7131 {
7132 struct elf_link_hash_table *ret;
7133 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7134
7135 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7136 if (ret == NULL)
7137 return NULL;
7138
7139 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7140 sizeof (struct elf_link_hash_entry),
7141 GENERIC_ELF_DATA))
7142 {
7143 free (ret);
7144 return NULL;
7145 }
7146 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7147
7148 return &ret->root;
7149 }
7150
7151 /* Destroy an ELF linker hash table. */
7152
7153 void
7154 _bfd_elf_link_hash_table_free (bfd *obfd)
7155 {
7156 struct elf_link_hash_table *htab;
7157
7158 htab = (struct elf_link_hash_table *) obfd->link.hash;
7159 if (htab->dynstr != NULL)
7160 _bfd_elf_strtab_free (htab->dynstr);
7161 _bfd_merge_sections_free (htab->merge_info);
7162 _bfd_generic_link_hash_table_free (obfd);
7163 }
7164
7165 /* This is a hook for the ELF emulation code in the generic linker to
7166 tell the backend linker what file name to use for the DT_NEEDED
7167 entry for a dynamic object. */
7168
7169 void
7170 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7171 {
7172 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7173 && bfd_get_format (abfd) == bfd_object)
7174 elf_dt_name (abfd) = name;
7175 }
7176
7177 int
7178 bfd_elf_get_dyn_lib_class (bfd *abfd)
7179 {
7180 int lib_class;
7181 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7182 && bfd_get_format (abfd) == bfd_object)
7183 lib_class = elf_dyn_lib_class (abfd);
7184 else
7185 lib_class = 0;
7186 return lib_class;
7187 }
7188
7189 void
7190 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7191 {
7192 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7193 && bfd_get_format (abfd) == bfd_object)
7194 elf_dyn_lib_class (abfd) = lib_class;
7195 }
7196
7197 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7198 the linker ELF emulation code. */
7199
7200 struct bfd_link_needed_list *
7201 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7202 struct bfd_link_info *info)
7203 {
7204 if (! is_elf_hash_table (info->hash))
7205 return NULL;
7206 return elf_hash_table (info)->needed;
7207 }
7208
7209 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7210 hook for the linker ELF emulation code. */
7211
7212 struct bfd_link_needed_list *
7213 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7214 struct bfd_link_info *info)
7215 {
7216 if (! is_elf_hash_table (info->hash))
7217 return NULL;
7218 return elf_hash_table (info)->runpath;
7219 }
7220
7221 /* Get the name actually used for a dynamic object for a link. This
7222 is the SONAME entry if there is one. Otherwise, it is the string
7223 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7224
7225 const char *
7226 bfd_elf_get_dt_soname (bfd *abfd)
7227 {
7228 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7229 && bfd_get_format (abfd) == bfd_object)
7230 return elf_dt_name (abfd);
7231 return NULL;
7232 }
7233
7234 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7235 the ELF linker emulation code. */
7236
7237 bfd_boolean
7238 bfd_elf_get_bfd_needed_list (bfd *abfd,
7239 struct bfd_link_needed_list **pneeded)
7240 {
7241 asection *s;
7242 bfd_byte *dynbuf = NULL;
7243 unsigned int elfsec;
7244 unsigned long shlink;
7245 bfd_byte *extdyn, *extdynend;
7246 size_t extdynsize;
7247 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7248
7249 *pneeded = NULL;
7250
7251 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7252 || bfd_get_format (abfd) != bfd_object)
7253 return TRUE;
7254
7255 s = bfd_get_section_by_name (abfd, ".dynamic");
7256 if (s == NULL || s->size == 0)
7257 return TRUE;
7258
7259 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7260 goto error_return;
7261
7262 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7263 if (elfsec == SHN_BAD)
7264 goto error_return;
7265
7266 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7267
7268 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7269 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7270
7271 extdyn = dynbuf;
7272 extdynend = extdyn + s->size;
7273 for (; extdyn < extdynend; extdyn += extdynsize)
7274 {
7275 Elf_Internal_Dyn dyn;
7276
7277 (*swap_dyn_in) (abfd, extdyn, &dyn);
7278
7279 if (dyn.d_tag == DT_NULL)
7280 break;
7281
7282 if (dyn.d_tag == DT_NEEDED)
7283 {
7284 const char *string;
7285 struct bfd_link_needed_list *l;
7286 unsigned int tagv = dyn.d_un.d_val;
7287 bfd_size_type amt;
7288
7289 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7290 if (string == NULL)
7291 goto error_return;
7292
7293 amt = sizeof *l;
7294 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7295 if (l == NULL)
7296 goto error_return;
7297
7298 l->by = abfd;
7299 l->name = string;
7300 l->next = *pneeded;
7301 *pneeded = l;
7302 }
7303 }
7304
7305 free (dynbuf);
7306
7307 return TRUE;
7308
7309 error_return:
7310 if (dynbuf != NULL)
7311 free (dynbuf);
7312 return FALSE;
7313 }
7314
7315 struct elf_symbuf_symbol
7316 {
7317 unsigned long st_name; /* Symbol name, index in string tbl */
7318 unsigned char st_info; /* Type and binding attributes */
7319 unsigned char st_other; /* Visibilty, and target specific */
7320 };
7321
7322 struct elf_symbuf_head
7323 {
7324 struct elf_symbuf_symbol *ssym;
7325 size_t count;
7326 unsigned int st_shndx;
7327 };
7328
7329 struct elf_symbol
7330 {
7331 union
7332 {
7333 Elf_Internal_Sym *isym;
7334 struct elf_symbuf_symbol *ssym;
7335 } u;
7336 const char *name;
7337 };
7338
7339 /* Sort references to symbols by ascending section number. */
7340
7341 static int
7342 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7343 {
7344 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7345 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7346
7347 return s1->st_shndx - s2->st_shndx;
7348 }
7349
7350 static int
7351 elf_sym_name_compare (const void *arg1, const void *arg2)
7352 {
7353 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7354 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7355 return strcmp (s1->name, s2->name);
7356 }
7357
7358 static struct elf_symbuf_head *
7359 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
7360 {
7361 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7362 struct elf_symbuf_symbol *ssym;
7363 struct elf_symbuf_head *ssymbuf, *ssymhead;
7364 size_t i, shndx_count, total_size;
7365
7366 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7367 if (indbuf == NULL)
7368 return NULL;
7369
7370 for (ind = indbuf, i = 0; i < symcount; i++)
7371 if (isymbuf[i].st_shndx != SHN_UNDEF)
7372 *ind++ = &isymbuf[i];
7373 indbufend = ind;
7374
7375 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7376 elf_sort_elf_symbol);
7377
7378 shndx_count = 0;
7379 if (indbufend > indbuf)
7380 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7381 if (ind[0]->st_shndx != ind[1]->st_shndx)
7382 shndx_count++;
7383
7384 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7385 + (indbufend - indbuf) * sizeof (*ssym));
7386 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7387 if (ssymbuf == NULL)
7388 {
7389 free (indbuf);
7390 return NULL;
7391 }
7392
7393 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7394 ssymbuf->ssym = NULL;
7395 ssymbuf->count = shndx_count;
7396 ssymbuf->st_shndx = 0;
7397 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7398 {
7399 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7400 {
7401 ssymhead++;
7402 ssymhead->ssym = ssym;
7403 ssymhead->count = 0;
7404 ssymhead->st_shndx = (*ind)->st_shndx;
7405 }
7406 ssym->st_name = (*ind)->st_name;
7407 ssym->st_info = (*ind)->st_info;
7408 ssym->st_other = (*ind)->st_other;
7409 ssymhead->count++;
7410 }
7411 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
7412 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7413 == total_size));
7414
7415 free (indbuf);
7416 return ssymbuf;
7417 }
7418
7419 /* Check if 2 sections define the same set of local and global
7420 symbols. */
7421
7422 static bfd_boolean
7423 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7424 struct bfd_link_info *info)
7425 {
7426 bfd *bfd1, *bfd2;
7427 const struct elf_backend_data *bed1, *bed2;
7428 Elf_Internal_Shdr *hdr1, *hdr2;
7429 size_t symcount1, symcount2;
7430 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7431 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7432 Elf_Internal_Sym *isym, *isymend;
7433 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7434 size_t count1, count2, i;
7435 unsigned int shndx1, shndx2;
7436 bfd_boolean result;
7437
7438 bfd1 = sec1->owner;
7439 bfd2 = sec2->owner;
7440
7441 /* Both sections have to be in ELF. */
7442 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7443 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7444 return FALSE;
7445
7446 if (elf_section_type (sec1) != elf_section_type (sec2))
7447 return FALSE;
7448
7449 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7450 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7451 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7452 return FALSE;
7453
7454 bed1 = get_elf_backend_data (bfd1);
7455 bed2 = get_elf_backend_data (bfd2);
7456 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7457 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7458 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7459 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7460
7461 if (symcount1 == 0 || symcount2 == 0)
7462 return FALSE;
7463
7464 result = FALSE;
7465 isymbuf1 = NULL;
7466 isymbuf2 = NULL;
7467 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7468 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7469
7470 if (ssymbuf1 == NULL)
7471 {
7472 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7473 NULL, NULL, NULL);
7474 if (isymbuf1 == NULL)
7475 goto done;
7476
7477 if (!info->reduce_memory_overheads)
7478 elf_tdata (bfd1)->symbuf = ssymbuf1
7479 = elf_create_symbuf (symcount1, isymbuf1);
7480 }
7481
7482 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7483 {
7484 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7485 NULL, NULL, NULL);
7486 if (isymbuf2 == NULL)
7487 goto done;
7488
7489 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7490 elf_tdata (bfd2)->symbuf = ssymbuf2
7491 = elf_create_symbuf (symcount2, isymbuf2);
7492 }
7493
7494 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7495 {
7496 /* Optimized faster version. */
7497 size_t lo, hi, mid;
7498 struct elf_symbol *symp;
7499 struct elf_symbuf_symbol *ssym, *ssymend;
7500
7501 lo = 0;
7502 hi = ssymbuf1->count;
7503 ssymbuf1++;
7504 count1 = 0;
7505 while (lo < hi)
7506 {
7507 mid = (lo + hi) / 2;
7508 if (shndx1 < ssymbuf1[mid].st_shndx)
7509 hi = mid;
7510 else if (shndx1 > ssymbuf1[mid].st_shndx)
7511 lo = mid + 1;
7512 else
7513 {
7514 count1 = ssymbuf1[mid].count;
7515 ssymbuf1 += mid;
7516 break;
7517 }
7518 }
7519
7520 lo = 0;
7521 hi = ssymbuf2->count;
7522 ssymbuf2++;
7523 count2 = 0;
7524 while (lo < hi)
7525 {
7526 mid = (lo + hi) / 2;
7527 if (shndx2 < ssymbuf2[mid].st_shndx)
7528 hi = mid;
7529 else if (shndx2 > ssymbuf2[mid].st_shndx)
7530 lo = mid + 1;
7531 else
7532 {
7533 count2 = ssymbuf2[mid].count;
7534 ssymbuf2 += mid;
7535 break;
7536 }
7537 }
7538
7539 if (count1 == 0 || count2 == 0 || count1 != count2)
7540 goto done;
7541
7542 symtable1
7543 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7544 symtable2
7545 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
7546 if (symtable1 == NULL || symtable2 == NULL)
7547 goto done;
7548
7549 symp = symtable1;
7550 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7551 ssym < ssymend; ssym++, symp++)
7552 {
7553 symp->u.ssym = ssym;
7554 symp->name = bfd_elf_string_from_elf_section (bfd1,
7555 hdr1->sh_link,
7556 ssym->st_name);
7557 }
7558
7559 symp = symtable2;
7560 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7561 ssym < ssymend; ssym++, symp++)
7562 {
7563 symp->u.ssym = ssym;
7564 symp->name = bfd_elf_string_from_elf_section (bfd2,
7565 hdr2->sh_link,
7566 ssym->st_name);
7567 }
7568
7569 /* Sort symbol by name. */
7570 qsort (symtable1, count1, sizeof (struct elf_symbol),
7571 elf_sym_name_compare);
7572 qsort (symtable2, count1, sizeof (struct elf_symbol),
7573 elf_sym_name_compare);
7574
7575 for (i = 0; i < count1; i++)
7576 /* Two symbols must have the same binding, type and name. */
7577 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7578 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7579 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7580 goto done;
7581
7582 result = TRUE;
7583 goto done;
7584 }
7585
7586 symtable1 = (struct elf_symbol *)
7587 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7588 symtable2 = (struct elf_symbol *)
7589 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7590 if (symtable1 == NULL || symtable2 == NULL)
7591 goto done;
7592
7593 /* Count definitions in the section. */
7594 count1 = 0;
7595 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7596 if (isym->st_shndx == shndx1)
7597 symtable1[count1++].u.isym = isym;
7598
7599 count2 = 0;
7600 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7601 if (isym->st_shndx == shndx2)
7602 symtable2[count2++].u.isym = isym;
7603
7604 if (count1 == 0 || count2 == 0 || count1 != count2)
7605 goto done;
7606
7607 for (i = 0; i < count1; i++)
7608 symtable1[i].name
7609 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7610 symtable1[i].u.isym->st_name);
7611
7612 for (i = 0; i < count2; i++)
7613 symtable2[i].name
7614 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7615 symtable2[i].u.isym->st_name);
7616
7617 /* Sort symbol by name. */
7618 qsort (symtable1, count1, sizeof (struct elf_symbol),
7619 elf_sym_name_compare);
7620 qsort (symtable2, count1, sizeof (struct elf_symbol),
7621 elf_sym_name_compare);
7622
7623 for (i = 0; i < count1; i++)
7624 /* Two symbols must have the same binding, type and name. */
7625 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7626 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7627 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7628 goto done;
7629
7630 result = TRUE;
7631
7632 done:
7633 if (symtable1)
7634 free (symtable1);
7635 if (symtable2)
7636 free (symtable2);
7637 if (isymbuf1)
7638 free (isymbuf1);
7639 if (isymbuf2)
7640 free (isymbuf2);
7641
7642 return result;
7643 }
7644
7645 /* Return TRUE if 2 section types are compatible. */
7646
7647 bfd_boolean
7648 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7649 bfd *bbfd, const asection *bsec)
7650 {
7651 if (asec == NULL
7652 || bsec == NULL
7653 || abfd->xvec->flavour != bfd_target_elf_flavour
7654 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7655 return TRUE;
7656
7657 return elf_section_type (asec) == elf_section_type (bsec);
7658 }
7659 \f
7660 /* Final phase of ELF linker. */
7661
7662 /* A structure we use to avoid passing large numbers of arguments. */
7663
7664 struct elf_final_link_info
7665 {
7666 /* General link information. */
7667 struct bfd_link_info *info;
7668 /* Output BFD. */
7669 bfd *output_bfd;
7670 /* Symbol string table. */
7671 struct elf_strtab_hash *symstrtab;
7672 /* .hash section. */
7673 asection *hash_sec;
7674 /* symbol version section (.gnu.version). */
7675 asection *symver_sec;
7676 /* Buffer large enough to hold contents of any section. */
7677 bfd_byte *contents;
7678 /* Buffer large enough to hold external relocs of any section. */
7679 void *external_relocs;
7680 /* Buffer large enough to hold internal relocs of any section. */
7681 Elf_Internal_Rela *internal_relocs;
7682 /* Buffer large enough to hold external local symbols of any input
7683 BFD. */
7684 bfd_byte *external_syms;
7685 /* And a buffer for symbol section indices. */
7686 Elf_External_Sym_Shndx *locsym_shndx;
7687 /* Buffer large enough to hold internal local symbols of any input
7688 BFD. */
7689 Elf_Internal_Sym *internal_syms;
7690 /* Array large enough to hold a symbol index for each local symbol
7691 of any input BFD. */
7692 long *indices;
7693 /* Array large enough to hold a section pointer for each local
7694 symbol of any input BFD. */
7695 asection **sections;
7696 /* Buffer for SHT_SYMTAB_SHNDX section. */
7697 Elf_External_Sym_Shndx *symshndxbuf;
7698 /* Number of STT_FILE syms seen. */
7699 size_t filesym_count;
7700 };
7701
7702 /* This struct is used to pass information to elf_link_output_extsym. */
7703
7704 struct elf_outext_info
7705 {
7706 bfd_boolean failed;
7707 bfd_boolean localsyms;
7708 bfd_boolean file_sym_done;
7709 struct elf_final_link_info *flinfo;
7710 };
7711
7712
7713 /* Support for evaluating a complex relocation.
7714
7715 Complex relocations are generalized, self-describing relocations. The
7716 implementation of them consists of two parts: complex symbols, and the
7717 relocations themselves.
7718
7719 The relocations are use a reserved elf-wide relocation type code (R_RELC
7720 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7721 information (start bit, end bit, word width, etc) into the addend. This
7722 information is extracted from CGEN-generated operand tables within gas.
7723
7724 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7725 internal) representing prefix-notation expressions, including but not
7726 limited to those sorts of expressions normally encoded as addends in the
7727 addend field. The symbol mangling format is:
7728
7729 <node> := <literal>
7730 | <unary-operator> ':' <node>
7731 | <binary-operator> ':' <node> ':' <node>
7732 ;
7733
7734 <literal> := 's' <digits=N> ':' <N character symbol name>
7735 | 'S' <digits=N> ':' <N character section name>
7736 | '#' <hexdigits>
7737 ;
7738
7739 <binary-operator> := as in C
7740 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7741
7742 static void
7743 set_symbol_value (bfd *bfd_with_globals,
7744 Elf_Internal_Sym *isymbuf,
7745 size_t locsymcount,
7746 size_t symidx,
7747 bfd_vma val)
7748 {
7749 struct elf_link_hash_entry **sym_hashes;
7750 struct elf_link_hash_entry *h;
7751 size_t extsymoff = locsymcount;
7752
7753 if (symidx < locsymcount)
7754 {
7755 Elf_Internal_Sym *sym;
7756
7757 sym = isymbuf + symidx;
7758 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7759 {
7760 /* It is a local symbol: move it to the
7761 "absolute" section and give it a value. */
7762 sym->st_shndx = SHN_ABS;
7763 sym->st_value = val;
7764 return;
7765 }
7766 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7767 extsymoff = 0;
7768 }
7769
7770 /* It is a global symbol: set its link type
7771 to "defined" and give it a value. */
7772
7773 sym_hashes = elf_sym_hashes (bfd_with_globals);
7774 h = sym_hashes [symidx - extsymoff];
7775 while (h->root.type == bfd_link_hash_indirect
7776 || h->root.type == bfd_link_hash_warning)
7777 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7778 h->root.type = bfd_link_hash_defined;
7779 h->root.u.def.value = val;
7780 h->root.u.def.section = bfd_abs_section_ptr;
7781 }
7782
7783 static bfd_boolean
7784 resolve_symbol (const char *name,
7785 bfd *input_bfd,
7786 struct elf_final_link_info *flinfo,
7787 bfd_vma *result,
7788 Elf_Internal_Sym *isymbuf,
7789 size_t locsymcount)
7790 {
7791 Elf_Internal_Sym *sym;
7792 struct bfd_link_hash_entry *global_entry;
7793 const char *candidate = NULL;
7794 Elf_Internal_Shdr *symtab_hdr;
7795 size_t i;
7796
7797 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7798
7799 for (i = 0; i < locsymcount; ++ i)
7800 {
7801 sym = isymbuf + i;
7802
7803 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7804 continue;
7805
7806 candidate = bfd_elf_string_from_elf_section (input_bfd,
7807 symtab_hdr->sh_link,
7808 sym->st_name);
7809 #ifdef DEBUG
7810 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7811 name, candidate, (unsigned long) sym->st_value);
7812 #endif
7813 if (candidate && strcmp (candidate, name) == 0)
7814 {
7815 asection *sec = flinfo->sections [i];
7816
7817 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7818 *result += sec->output_offset + sec->output_section->vma;
7819 #ifdef DEBUG
7820 printf ("Found symbol with value %8.8lx\n",
7821 (unsigned long) *result);
7822 #endif
7823 return TRUE;
7824 }
7825 }
7826
7827 /* Hmm, haven't found it yet. perhaps it is a global. */
7828 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
7829 FALSE, FALSE, TRUE);
7830 if (!global_entry)
7831 return FALSE;
7832
7833 if (global_entry->type == bfd_link_hash_defined
7834 || global_entry->type == bfd_link_hash_defweak)
7835 {
7836 *result = (global_entry->u.def.value
7837 + global_entry->u.def.section->output_section->vma
7838 + global_entry->u.def.section->output_offset);
7839 #ifdef DEBUG
7840 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7841 global_entry->root.string, (unsigned long) *result);
7842 #endif
7843 return TRUE;
7844 }
7845
7846 return FALSE;
7847 }
7848
7849 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
7850 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
7851 names like "foo.end" which is the end address of section "foo". */
7852
7853 static bfd_boolean
7854 resolve_section (const char *name,
7855 asection *sections,
7856 bfd_vma *result,
7857 bfd * abfd)
7858 {
7859 asection *curr;
7860 unsigned int len;
7861
7862 for (curr = sections; curr; curr = curr->next)
7863 if (strcmp (curr->name, name) == 0)
7864 {
7865 *result = curr->vma;
7866 return TRUE;
7867 }
7868
7869 /* Hmm. still haven't found it. try pseudo-section names. */
7870 /* FIXME: This could be coded more efficiently... */
7871 for (curr = sections; curr; curr = curr->next)
7872 {
7873 len = strlen (curr->name);
7874 if (len > strlen (name))
7875 continue;
7876
7877 if (strncmp (curr->name, name, len) == 0)
7878 {
7879 if (strncmp (".end", name + len, 4) == 0)
7880 {
7881 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
7882 return TRUE;
7883 }
7884
7885 /* Insert more pseudo-section names here, if you like. */
7886 }
7887 }
7888
7889 return FALSE;
7890 }
7891
7892 static void
7893 undefined_reference (const char *reftype, const char *name)
7894 {
7895 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7896 reftype, name);
7897 }
7898
7899 static bfd_boolean
7900 eval_symbol (bfd_vma *result,
7901 const char **symp,
7902 bfd *input_bfd,
7903 struct elf_final_link_info *flinfo,
7904 bfd_vma dot,
7905 Elf_Internal_Sym *isymbuf,
7906 size_t locsymcount,
7907 int signed_p)
7908 {
7909 size_t len;
7910 size_t symlen;
7911 bfd_vma a;
7912 bfd_vma b;
7913 char symbuf[4096];
7914 const char *sym = *symp;
7915 const char *symend;
7916 bfd_boolean symbol_is_section = FALSE;
7917
7918 len = strlen (sym);
7919 symend = sym + len;
7920
7921 if (len < 1 || len > sizeof (symbuf))
7922 {
7923 bfd_set_error (bfd_error_invalid_operation);
7924 return FALSE;
7925 }
7926
7927 switch (* sym)
7928 {
7929 case '.':
7930 *result = dot;
7931 *symp = sym + 1;
7932 return TRUE;
7933
7934 case '#':
7935 ++sym;
7936 *result = strtoul (sym, (char **) symp, 16);
7937 return TRUE;
7938
7939 case 'S':
7940 symbol_is_section = TRUE;
7941 case 's':
7942 ++sym;
7943 symlen = strtol (sym, (char **) symp, 10);
7944 sym = *symp + 1; /* Skip the trailing ':'. */
7945
7946 if (symend < sym || symlen + 1 > sizeof (symbuf))
7947 {
7948 bfd_set_error (bfd_error_invalid_operation);
7949 return FALSE;
7950 }
7951
7952 memcpy (symbuf, sym, symlen);
7953 symbuf[symlen] = '\0';
7954 *symp = sym + symlen;
7955
7956 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7957 the symbol as a section, or vice-versa. so we're pretty liberal in our
7958 interpretation here; section means "try section first", not "must be a
7959 section", and likewise with symbol. */
7960
7961 if (symbol_is_section)
7962 {
7963 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
7964 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
7965 isymbuf, locsymcount))
7966 {
7967 undefined_reference ("section", symbuf);
7968 return FALSE;
7969 }
7970 }
7971 else
7972 {
7973 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
7974 isymbuf, locsymcount)
7975 && !resolve_section (symbuf, flinfo->output_bfd->sections,
7976 result, input_bfd))
7977 {
7978 undefined_reference ("symbol", symbuf);
7979 return FALSE;
7980 }
7981 }
7982
7983 return TRUE;
7984
7985 /* All that remains are operators. */
7986
7987 #define UNARY_OP(op) \
7988 if (strncmp (sym, #op, strlen (#op)) == 0) \
7989 { \
7990 sym += strlen (#op); \
7991 if (*sym == ':') \
7992 ++sym; \
7993 *symp = sym; \
7994 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
7995 isymbuf, locsymcount, signed_p)) \
7996 return FALSE; \
7997 if (signed_p) \
7998 *result = op ((bfd_signed_vma) a); \
7999 else \
8000 *result = op a; \
8001 return TRUE; \
8002 }
8003
8004 #define BINARY_OP(op) \
8005 if (strncmp (sym, #op, strlen (#op)) == 0) \
8006 { \
8007 sym += strlen (#op); \
8008 if (*sym == ':') \
8009 ++sym; \
8010 *symp = sym; \
8011 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8012 isymbuf, locsymcount, signed_p)) \
8013 return FALSE; \
8014 ++*symp; \
8015 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8016 isymbuf, locsymcount, signed_p)) \
8017 return FALSE; \
8018 if (signed_p) \
8019 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8020 else \
8021 *result = a op b; \
8022 return TRUE; \
8023 }
8024
8025 default:
8026 UNARY_OP (0-);
8027 BINARY_OP (<<);
8028 BINARY_OP (>>);
8029 BINARY_OP (==);
8030 BINARY_OP (!=);
8031 BINARY_OP (<=);
8032 BINARY_OP (>=);
8033 BINARY_OP (&&);
8034 BINARY_OP (||);
8035 UNARY_OP (~);
8036 UNARY_OP (!);
8037 BINARY_OP (*);
8038 BINARY_OP (/);
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 #undef UNARY_OP
8048 #undef BINARY_OP
8049 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8050 bfd_set_error (bfd_error_invalid_operation);
8051 return FALSE;
8052 }
8053 }
8054
8055 static void
8056 put_value (bfd_vma size,
8057 unsigned long chunksz,
8058 bfd *input_bfd,
8059 bfd_vma x,
8060 bfd_byte *location)
8061 {
8062 location += (size - chunksz);
8063
8064 for (; size; size -= chunksz, location -= chunksz)
8065 {
8066 switch (chunksz)
8067 {
8068 case 1:
8069 bfd_put_8 (input_bfd, x, location);
8070 x >>= 8;
8071 break;
8072 case 2:
8073 bfd_put_16 (input_bfd, x, location);
8074 x >>= 16;
8075 break;
8076 case 4:
8077 bfd_put_32 (input_bfd, x, location);
8078 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8079 x >>= 16;
8080 x >>= 16;
8081 break;
8082 #ifdef BFD64
8083 case 8:
8084 bfd_put_64 (input_bfd, x, location);
8085 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8086 x >>= 32;
8087 x >>= 32;
8088 break;
8089 #endif
8090 default:
8091 abort ();
8092 break;
8093 }
8094 }
8095 }
8096
8097 static bfd_vma
8098 get_value (bfd_vma size,
8099 unsigned long chunksz,
8100 bfd *input_bfd,
8101 bfd_byte *location)
8102 {
8103 int shift;
8104 bfd_vma x = 0;
8105
8106 /* Sanity checks. */
8107 BFD_ASSERT (chunksz <= sizeof (x)
8108 && size >= chunksz
8109 && chunksz != 0
8110 && (size % chunksz) == 0
8111 && input_bfd != NULL
8112 && location != NULL);
8113
8114 if (chunksz == sizeof (x))
8115 {
8116 BFD_ASSERT (size == chunksz);
8117
8118 /* Make sure that we do not perform an undefined shift operation.
8119 We know that size == chunksz so there will only be one iteration
8120 of the loop below. */
8121 shift = 0;
8122 }
8123 else
8124 shift = 8 * chunksz;
8125
8126 for (; size; size -= chunksz, location += chunksz)
8127 {
8128 switch (chunksz)
8129 {
8130 case 1:
8131 x = (x << shift) | bfd_get_8 (input_bfd, location);
8132 break;
8133 case 2:
8134 x = (x << shift) | bfd_get_16 (input_bfd, location);
8135 break;
8136 case 4:
8137 x = (x << shift) | bfd_get_32 (input_bfd, location);
8138 break;
8139 #ifdef BFD64
8140 case 8:
8141 x = (x << shift) | bfd_get_64 (input_bfd, location);
8142 break;
8143 #endif
8144 default:
8145 abort ();
8146 }
8147 }
8148 return x;
8149 }
8150
8151 static void
8152 decode_complex_addend (unsigned long *start, /* in bits */
8153 unsigned long *oplen, /* in bits */
8154 unsigned long *len, /* in bits */
8155 unsigned long *wordsz, /* in bytes */
8156 unsigned long *chunksz, /* in bytes */
8157 unsigned long *lsb0_p,
8158 unsigned long *signed_p,
8159 unsigned long *trunc_p,
8160 unsigned long encoded)
8161 {
8162 * start = encoded & 0x3F;
8163 * len = (encoded >> 6) & 0x3F;
8164 * oplen = (encoded >> 12) & 0x3F;
8165 * wordsz = (encoded >> 18) & 0xF;
8166 * chunksz = (encoded >> 22) & 0xF;
8167 * lsb0_p = (encoded >> 27) & 1;
8168 * signed_p = (encoded >> 28) & 1;
8169 * trunc_p = (encoded >> 29) & 1;
8170 }
8171
8172 bfd_reloc_status_type
8173 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8174 asection *input_section ATTRIBUTE_UNUSED,
8175 bfd_byte *contents,
8176 Elf_Internal_Rela *rel,
8177 bfd_vma relocation)
8178 {
8179 bfd_vma shift, x, mask;
8180 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8181 bfd_reloc_status_type r;
8182
8183 /* Perform this reloc, since it is complex.
8184 (this is not to say that it necessarily refers to a complex
8185 symbol; merely that it is a self-describing CGEN based reloc.
8186 i.e. the addend has the complete reloc information (bit start, end,
8187 word size, etc) encoded within it.). */
8188
8189 decode_complex_addend (&start, &oplen, &len, &wordsz,
8190 &chunksz, &lsb0_p, &signed_p,
8191 &trunc_p, rel->r_addend);
8192
8193 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8194
8195 if (lsb0_p)
8196 shift = (start + 1) - len;
8197 else
8198 shift = (8 * wordsz) - (start + len);
8199
8200 x = get_value (wordsz, chunksz, input_bfd,
8201 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8202
8203 #ifdef DEBUG
8204 printf ("Doing complex reloc: "
8205 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8206 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8207 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8208 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8209 oplen, (unsigned long) x, (unsigned long) mask,
8210 (unsigned long) relocation);
8211 #endif
8212
8213 r = bfd_reloc_ok;
8214 if (! trunc_p)
8215 /* Now do an overflow check. */
8216 r = bfd_check_overflow ((signed_p
8217 ? complain_overflow_signed
8218 : complain_overflow_unsigned),
8219 len, 0, (8 * wordsz),
8220 relocation);
8221
8222 /* Do the deed. */
8223 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8224
8225 #ifdef DEBUG
8226 printf (" relocation: %8.8lx\n"
8227 " shifted mask: %8.8lx\n"
8228 " shifted/masked reloc: %8.8lx\n"
8229 " result: %8.8lx\n",
8230 (unsigned long) relocation, (unsigned long) (mask << shift),
8231 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8232 #endif
8233 put_value (wordsz, chunksz, input_bfd, x,
8234 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8235 return r;
8236 }
8237
8238 /* Functions to read r_offset from external (target order) reloc
8239 entry. Faster than bfd_getl32 et al, because we let the compiler
8240 know the value is aligned. */
8241
8242 static bfd_vma
8243 ext32l_r_offset (const void *p)
8244 {
8245 union aligned32
8246 {
8247 uint32_t v;
8248 unsigned char c[4];
8249 };
8250 const union aligned32 *a
8251 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8252
8253 uint32_t aval = ( (uint32_t) a->c[0]
8254 | (uint32_t) a->c[1] << 8
8255 | (uint32_t) a->c[2] << 16
8256 | (uint32_t) a->c[3] << 24);
8257 return aval;
8258 }
8259
8260 static bfd_vma
8261 ext32b_r_offset (const void *p)
8262 {
8263 union aligned32
8264 {
8265 uint32_t v;
8266 unsigned char c[4];
8267 };
8268 const union aligned32 *a
8269 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8270
8271 uint32_t aval = ( (uint32_t) a->c[0] << 24
8272 | (uint32_t) a->c[1] << 16
8273 | (uint32_t) a->c[2] << 8
8274 | (uint32_t) a->c[3]);
8275 return aval;
8276 }
8277
8278 #ifdef BFD_HOST_64_BIT
8279 static bfd_vma
8280 ext64l_r_offset (const void *p)
8281 {
8282 union aligned64
8283 {
8284 uint64_t v;
8285 unsigned char c[8];
8286 };
8287 const union aligned64 *a
8288 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8289
8290 uint64_t aval = ( (uint64_t) a->c[0]
8291 | (uint64_t) a->c[1] << 8
8292 | (uint64_t) a->c[2] << 16
8293 | (uint64_t) a->c[3] << 24
8294 | (uint64_t) a->c[4] << 32
8295 | (uint64_t) a->c[5] << 40
8296 | (uint64_t) a->c[6] << 48
8297 | (uint64_t) a->c[7] << 56);
8298 return aval;
8299 }
8300
8301 static bfd_vma
8302 ext64b_r_offset (const void *p)
8303 {
8304 union aligned64
8305 {
8306 uint64_t v;
8307 unsigned char c[8];
8308 };
8309 const union aligned64 *a
8310 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8311
8312 uint64_t aval = ( (uint64_t) a->c[0] << 56
8313 | (uint64_t) a->c[1] << 48
8314 | (uint64_t) a->c[2] << 40
8315 | (uint64_t) a->c[3] << 32
8316 | (uint64_t) a->c[4] << 24
8317 | (uint64_t) a->c[5] << 16
8318 | (uint64_t) a->c[6] << 8
8319 | (uint64_t) a->c[7]);
8320 return aval;
8321 }
8322 #endif
8323
8324 /* When performing a relocatable link, the input relocations are
8325 preserved. But, if they reference global symbols, the indices
8326 referenced must be updated. Update all the relocations found in
8327 RELDATA. */
8328
8329 static bfd_boolean
8330 elf_link_adjust_relocs (bfd *abfd,
8331 struct bfd_elf_section_reloc_data *reldata,
8332 bfd_boolean sort)
8333 {
8334 unsigned int i;
8335 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8336 bfd_byte *erela;
8337 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8338 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8339 bfd_vma r_type_mask;
8340 int r_sym_shift;
8341 unsigned int count = reldata->count;
8342 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8343
8344 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8345 {
8346 swap_in = bed->s->swap_reloc_in;
8347 swap_out = bed->s->swap_reloc_out;
8348 }
8349 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8350 {
8351 swap_in = bed->s->swap_reloca_in;
8352 swap_out = bed->s->swap_reloca_out;
8353 }
8354 else
8355 abort ();
8356
8357 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8358 abort ();
8359
8360 if (bed->s->arch_size == 32)
8361 {
8362 r_type_mask = 0xff;
8363 r_sym_shift = 8;
8364 }
8365 else
8366 {
8367 r_type_mask = 0xffffffff;
8368 r_sym_shift = 32;
8369 }
8370
8371 erela = reldata->hdr->contents;
8372 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8373 {
8374 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8375 unsigned int j;
8376
8377 if (*rel_hash == NULL)
8378 continue;
8379
8380 BFD_ASSERT ((*rel_hash)->indx >= 0);
8381
8382 (*swap_in) (abfd, erela, irela);
8383 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8384 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8385 | (irela[j].r_info & r_type_mask));
8386 (*swap_out) (abfd, irela, erela);
8387 }
8388
8389 if (sort && count != 0)
8390 {
8391 bfd_vma (*ext_r_off) (const void *);
8392 bfd_vma r_off;
8393 size_t elt_size;
8394 bfd_byte *base, *end, *p, *loc;
8395 bfd_byte *buf = NULL;
8396
8397 if (bed->s->arch_size == 32)
8398 {
8399 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8400 ext_r_off = ext32l_r_offset;
8401 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8402 ext_r_off = ext32b_r_offset;
8403 else
8404 abort ();
8405 }
8406 else
8407 {
8408 #ifdef BFD_HOST_64_BIT
8409 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8410 ext_r_off = ext64l_r_offset;
8411 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8412 ext_r_off = ext64b_r_offset;
8413 else
8414 #endif
8415 abort ();
8416 }
8417
8418 /* Must use a stable sort here. A modified insertion sort,
8419 since the relocs are mostly sorted already. */
8420 elt_size = reldata->hdr->sh_entsize;
8421 base = reldata->hdr->contents;
8422 end = base + count * elt_size;
8423 if (elt_size > sizeof (Elf64_External_Rela))
8424 abort ();
8425
8426 /* Ensure the first element is lowest. This acts as a sentinel,
8427 speeding the main loop below. */
8428 r_off = (*ext_r_off) (base);
8429 for (p = loc = base; (p += elt_size) < end; )
8430 {
8431 bfd_vma r_off2 = (*ext_r_off) (p);
8432 if (r_off > r_off2)
8433 {
8434 r_off = r_off2;
8435 loc = p;
8436 }
8437 }
8438 if (loc != base)
8439 {
8440 /* Don't just swap *base and *loc as that changes the order
8441 of the original base[0] and base[1] if they happen to
8442 have the same r_offset. */
8443 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8444 memcpy (onebuf, loc, elt_size);
8445 memmove (base + elt_size, base, loc - base);
8446 memcpy (base, onebuf, elt_size);
8447 }
8448
8449 for (p = base + elt_size; (p += elt_size) < end; )
8450 {
8451 /* base to p is sorted, *p is next to insert. */
8452 r_off = (*ext_r_off) (p);
8453 /* Search the sorted region for location to insert. */
8454 loc = p - elt_size;
8455 while (r_off < (*ext_r_off) (loc))
8456 loc -= elt_size;
8457 loc += elt_size;
8458 if (loc != p)
8459 {
8460 /* Chances are there is a run of relocs to insert here,
8461 from one of more input files. Files are not always
8462 linked in order due to the way elf_link_input_bfd is
8463 called. See pr17666. */
8464 size_t sortlen = p - loc;
8465 bfd_vma r_off2 = (*ext_r_off) (loc);
8466 size_t runlen = elt_size;
8467 size_t buf_size = 96 * 1024;
8468 while (p + runlen < end
8469 && (sortlen <= buf_size
8470 || runlen + elt_size <= buf_size)
8471 && r_off2 > (*ext_r_off) (p + runlen))
8472 runlen += elt_size;
8473 if (buf == NULL)
8474 {
8475 buf = bfd_malloc (buf_size);
8476 if (buf == NULL)
8477 return FALSE;
8478 }
8479 if (runlen < sortlen)
8480 {
8481 memcpy (buf, p, runlen);
8482 memmove (loc + runlen, loc, sortlen);
8483 memcpy (loc, buf, runlen);
8484 }
8485 else
8486 {
8487 memcpy (buf, loc, sortlen);
8488 memmove (loc, p, runlen);
8489 memcpy (loc + runlen, buf, sortlen);
8490 }
8491 p += runlen - elt_size;
8492 }
8493 }
8494 /* Hashes are no longer valid. */
8495 free (reldata->hashes);
8496 reldata->hashes = NULL;
8497 free (buf);
8498 }
8499 return TRUE;
8500 }
8501
8502 struct elf_link_sort_rela
8503 {
8504 union {
8505 bfd_vma offset;
8506 bfd_vma sym_mask;
8507 } u;
8508 enum elf_reloc_type_class type;
8509 /* We use this as an array of size int_rels_per_ext_rel. */
8510 Elf_Internal_Rela rela[1];
8511 };
8512
8513 static int
8514 elf_link_sort_cmp1 (const void *A, const void *B)
8515 {
8516 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8517 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8518 int relativea, relativeb;
8519
8520 relativea = a->type == reloc_class_relative;
8521 relativeb = b->type == reloc_class_relative;
8522
8523 if (relativea < relativeb)
8524 return 1;
8525 if (relativea > relativeb)
8526 return -1;
8527 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8528 return -1;
8529 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8530 return 1;
8531 if (a->rela->r_offset < b->rela->r_offset)
8532 return -1;
8533 if (a->rela->r_offset > b->rela->r_offset)
8534 return 1;
8535 return 0;
8536 }
8537
8538 static int
8539 elf_link_sort_cmp2 (const void *A, const void *B)
8540 {
8541 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8542 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8543
8544 if (a->type < b->type)
8545 return -1;
8546 if (a->type > b->type)
8547 return 1;
8548 if (a->u.offset < b->u.offset)
8549 return -1;
8550 if (a->u.offset > b->u.offset)
8551 return 1;
8552 if (a->rela->r_offset < b->rela->r_offset)
8553 return -1;
8554 if (a->rela->r_offset > b->rela->r_offset)
8555 return 1;
8556 return 0;
8557 }
8558
8559 static size_t
8560 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8561 {
8562 asection *dynamic_relocs;
8563 asection *rela_dyn;
8564 asection *rel_dyn;
8565 bfd_size_type count, size;
8566 size_t i, ret, sort_elt, ext_size;
8567 bfd_byte *sort, *s_non_relative, *p;
8568 struct elf_link_sort_rela *sq;
8569 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8570 int i2e = bed->s->int_rels_per_ext_rel;
8571 unsigned int opb = bfd_octets_per_byte (abfd);
8572 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8573 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8574 struct bfd_link_order *lo;
8575 bfd_vma r_sym_mask;
8576 bfd_boolean use_rela;
8577
8578 /* Find a dynamic reloc section. */
8579 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8580 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8581 if (rela_dyn != NULL && rela_dyn->size > 0
8582 && rel_dyn != NULL && rel_dyn->size > 0)
8583 {
8584 bfd_boolean use_rela_initialised = FALSE;
8585
8586 /* This is just here to stop gcc from complaining.
8587 Its initialization checking code is not perfect. */
8588 use_rela = TRUE;
8589
8590 /* Both sections are present. Examine the sizes
8591 of the indirect sections to help us choose. */
8592 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8593 if (lo->type == bfd_indirect_link_order)
8594 {
8595 asection *o = lo->u.indirect.section;
8596
8597 if ((o->size % bed->s->sizeof_rela) == 0)
8598 {
8599 if ((o->size % bed->s->sizeof_rel) == 0)
8600 /* Section size is divisible by both rel and rela sizes.
8601 It is of no help to us. */
8602 ;
8603 else
8604 {
8605 /* Section size is only divisible by rela. */
8606 if (use_rela_initialised && (use_rela == FALSE))
8607 {
8608 _bfd_error_handler (_("%B: Unable to sort relocs - "
8609 "they are in more than one size"),
8610 abfd);
8611 bfd_set_error (bfd_error_invalid_operation);
8612 return 0;
8613 }
8614 else
8615 {
8616 use_rela = TRUE;
8617 use_rela_initialised = TRUE;
8618 }
8619 }
8620 }
8621 else if ((o->size % bed->s->sizeof_rel) == 0)
8622 {
8623 /* Section size is only divisible by rel. */
8624 if (use_rela_initialised && (use_rela == TRUE))
8625 {
8626 _bfd_error_handler (_("%B: Unable to sort relocs - "
8627 "they are in more than one size"),
8628 abfd);
8629 bfd_set_error (bfd_error_invalid_operation);
8630 return 0;
8631 }
8632 else
8633 {
8634 use_rela = FALSE;
8635 use_rela_initialised = TRUE;
8636 }
8637 }
8638 else
8639 {
8640 /* The section size is not divisible by either -
8641 something is wrong. */
8642 _bfd_error_handler (_("%B: Unable to sort relocs - "
8643 "they are of an unknown size"), abfd);
8644 bfd_set_error (bfd_error_invalid_operation);
8645 return 0;
8646 }
8647 }
8648
8649 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8650 if (lo->type == bfd_indirect_link_order)
8651 {
8652 asection *o = lo->u.indirect.section;
8653
8654 if ((o->size % bed->s->sizeof_rela) == 0)
8655 {
8656 if ((o->size % bed->s->sizeof_rel) == 0)
8657 /* Section size is divisible by both rel and rela sizes.
8658 It is of no help to us. */
8659 ;
8660 else
8661 {
8662 /* Section size is only divisible by rela. */
8663 if (use_rela_initialised && (use_rela == FALSE))
8664 {
8665 _bfd_error_handler (_("%B: Unable to sort relocs - "
8666 "they are in more than one size"),
8667 abfd);
8668 bfd_set_error (bfd_error_invalid_operation);
8669 return 0;
8670 }
8671 else
8672 {
8673 use_rela = TRUE;
8674 use_rela_initialised = TRUE;
8675 }
8676 }
8677 }
8678 else if ((o->size % bed->s->sizeof_rel) == 0)
8679 {
8680 /* Section size is only divisible by rel. */
8681 if (use_rela_initialised && (use_rela == TRUE))
8682 {
8683 _bfd_error_handler (_("%B: Unable to sort relocs - "
8684 "they are in more than one size"),
8685 abfd);
8686 bfd_set_error (bfd_error_invalid_operation);
8687 return 0;
8688 }
8689 else
8690 {
8691 use_rela = FALSE;
8692 use_rela_initialised = TRUE;
8693 }
8694 }
8695 else
8696 {
8697 /* The section size is not divisible by either -
8698 something is wrong. */
8699 _bfd_error_handler (_("%B: Unable to sort relocs - "
8700 "they are of an unknown size"), abfd);
8701 bfd_set_error (bfd_error_invalid_operation);
8702 return 0;
8703 }
8704 }
8705
8706 if (! use_rela_initialised)
8707 /* Make a guess. */
8708 use_rela = TRUE;
8709 }
8710 else if (rela_dyn != NULL && rela_dyn->size > 0)
8711 use_rela = TRUE;
8712 else if (rel_dyn != NULL && rel_dyn->size > 0)
8713 use_rela = FALSE;
8714 else
8715 return 0;
8716
8717 if (use_rela)
8718 {
8719 dynamic_relocs = rela_dyn;
8720 ext_size = bed->s->sizeof_rela;
8721 swap_in = bed->s->swap_reloca_in;
8722 swap_out = bed->s->swap_reloca_out;
8723 }
8724 else
8725 {
8726 dynamic_relocs = rel_dyn;
8727 ext_size = bed->s->sizeof_rel;
8728 swap_in = bed->s->swap_reloc_in;
8729 swap_out = bed->s->swap_reloc_out;
8730 }
8731
8732 size = 0;
8733 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8734 if (lo->type == bfd_indirect_link_order)
8735 size += lo->u.indirect.section->size;
8736
8737 if (size != dynamic_relocs->size)
8738 return 0;
8739
8740 sort_elt = (sizeof (struct elf_link_sort_rela)
8741 + (i2e - 1) * sizeof (Elf_Internal_Rela));
8742
8743 count = dynamic_relocs->size / ext_size;
8744 if (count == 0)
8745 return 0;
8746 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
8747
8748 if (sort == NULL)
8749 {
8750 (*info->callbacks->warning)
8751 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8752 return 0;
8753 }
8754
8755 if (bed->s->arch_size == 32)
8756 r_sym_mask = ~(bfd_vma) 0xff;
8757 else
8758 r_sym_mask = ~(bfd_vma) 0xffffffff;
8759
8760 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8761 if (lo->type == bfd_indirect_link_order)
8762 {
8763 bfd_byte *erel, *erelend;
8764 asection *o = lo->u.indirect.section;
8765
8766 if (o->contents == NULL && o->size != 0)
8767 {
8768 /* This is a reloc section that is being handled as a normal
8769 section. See bfd_section_from_shdr. We can't combine
8770 relocs in this case. */
8771 free (sort);
8772 return 0;
8773 }
8774 erel = o->contents;
8775 erelend = o->contents + o->size;
8776 p = sort + o->output_offset * opb / ext_size * sort_elt;
8777
8778 while (erel < erelend)
8779 {
8780 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8781
8782 (*swap_in) (abfd, erel, s->rela);
8783 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
8784 s->u.sym_mask = r_sym_mask;
8785 p += sort_elt;
8786 erel += ext_size;
8787 }
8788 }
8789
8790 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8791
8792 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8793 {
8794 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8795 if (s->type != reloc_class_relative)
8796 break;
8797 }
8798 ret = i;
8799 s_non_relative = p;
8800
8801 sq = (struct elf_link_sort_rela *) s_non_relative;
8802 for (; i < count; i++, p += sort_elt)
8803 {
8804 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8805 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8806 sq = sp;
8807 sp->u.offset = sq->rela->r_offset;
8808 }
8809
8810 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8811
8812 struct elf_link_hash_table *htab = elf_hash_table (info);
8813 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
8814 {
8815 /* We have plt relocs in .rela.dyn. */
8816 sq = (struct elf_link_sort_rela *) sort;
8817 for (i = 0; i < count; i++)
8818 if (sq[count - i - 1].type != reloc_class_plt)
8819 break;
8820 if (i != 0 && htab->srelplt->size == i * ext_size)
8821 {
8822 struct bfd_link_order **plo;
8823 /* Put srelplt link_order last. This is so the output_offset
8824 set in the next loop is correct for DT_JMPREL. */
8825 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
8826 if ((*plo)->type == bfd_indirect_link_order
8827 && (*plo)->u.indirect.section == htab->srelplt)
8828 {
8829 lo = *plo;
8830 *plo = lo->next;
8831 }
8832 else
8833 plo = &(*plo)->next;
8834 *plo = lo;
8835 lo->next = NULL;
8836 dynamic_relocs->map_tail.link_order = lo;
8837 }
8838 }
8839
8840 p = sort;
8841 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8842 if (lo->type == bfd_indirect_link_order)
8843 {
8844 bfd_byte *erel, *erelend;
8845 asection *o = lo->u.indirect.section;
8846
8847 erel = o->contents;
8848 erelend = o->contents + o->size;
8849 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
8850 while (erel < erelend)
8851 {
8852 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8853 (*swap_out) (abfd, s->rela, erel);
8854 p += sort_elt;
8855 erel += ext_size;
8856 }
8857 }
8858
8859 free (sort);
8860 *psec = dynamic_relocs;
8861 return ret;
8862 }
8863
8864 /* Add a symbol to the output symbol string table. */
8865
8866 static int
8867 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
8868 const char *name,
8869 Elf_Internal_Sym *elfsym,
8870 asection *input_sec,
8871 struct elf_link_hash_entry *h)
8872 {
8873 int (*output_symbol_hook)
8874 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8875 struct elf_link_hash_entry *);
8876 struct elf_link_hash_table *hash_table;
8877 const struct elf_backend_data *bed;
8878 bfd_size_type strtabsize;
8879
8880 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8881
8882 bed = get_elf_backend_data (flinfo->output_bfd);
8883 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8884 if (output_symbol_hook != NULL)
8885 {
8886 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
8887 if (ret != 1)
8888 return ret;
8889 }
8890
8891 if (name == NULL
8892 || *name == '\0'
8893 || (input_sec->flags & SEC_EXCLUDE))
8894 elfsym->st_name = (unsigned long) -1;
8895 else
8896 {
8897 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8898 to get the final offset for st_name. */
8899 elfsym->st_name
8900 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
8901 name, FALSE);
8902 if (elfsym->st_name == (unsigned long) -1)
8903 return 0;
8904 }
8905
8906 hash_table = elf_hash_table (flinfo->info);
8907 strtabsize = hash_table->strtabsize;
8908 if (strtabsize <= hash_table->strtabcount)
8909 {
8910 strtabsize += strtabsize;
8911 hash_table->strtabsize = strtabsize;
8912 strtabsize *= sizeof (*hash_table->strtab);
8913 hash_table->strtab
8914 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
8915 strtabsize);
8916 if (hash_table->strtab == NULL)
8917 return 0;
8918 }
8919 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
8920 hash_table->strtab[hash_table->strtabcount].dest_index
8921 = hash_table->strtabcount;
8922 hash_table->strtab[hash_table->strtabcount].destshndx_index
8923 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
8924
8925 bfd_get_symcount (flinfo->output_bfd) += 1;
8926 hash_table->strtabcount += 1;
8927
8928 return 1;
8929 }
8930
8931 /* Swap symbols out to the symbol table and flush the output symbols to
8932 the file. */
8933
8934 static bfd_boolean
8935 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
8936 {
8937 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
8938 bfd_size_type amt;
8939 size_t i;
8940 const struct elf_backend_data *bed;
8941 bfd_byte *symbuf;
8942 Elf_Internal_Shdr *hdr;
8943 file_ptr pos;
8944 bfd_boolean ret;
8945
8946 if (!hash_table->strtabcount)
8947 return TRUE;
8948
8949 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8950
8951 bed = get_elf_backend_data (flinfo->output_bfd);
8952
8953 amt = bed->s->sizeof_sym * hash_table->strtabcount;
8954 symbuf = (bfd_byte *) bfd_malloc (amt);
8955 if (symbuf == NULL)
8956 return FALSE;
8957
8958 if (flinfo->symshndxbuf)
8959 {
8960 amt = sizeof (Elf_External_Sym_Shndx);
8961 amt *= bfd_get_symcount (flinfo->output_bfd);
8962 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
8963 if (flinfo->symshndxbuf == NULL)
8964 {
8965 free (symbuf);
8966 return FALSE;
8967 }
8968 }
8969
8970 for (i = 0; i < hash_table->strtabcount; i++)
8971 {
8972 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
8973 if (elfsym->sym.st_name == (unsigned long) -1)
8974 elfsym->sym.st_name = 0;
8975 else
8976 elfsym->sym.st_name
8977 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
8978 elfsym->sym.st_name);
8979 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
8980 ((bfd_byte *) symbuf
8981 + (elfsym->dest_index
8982 * bed->s->sizeof_sym)),
8983 (flinfo->symshndxbuf
8984 + elfsym->destshndx_index));
8985 }
8986
8987 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
8988 pos = hdr->sh_offset + hdr->sh_size;
8989 amt = hash_table->strtabcount * bed->s->sizeof_sym;
8990 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
8991 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
8992 {
8993 hdr->sh_size += amt;
8994 ret = TRUE;
8995 }
8996 else
8997 ret = FALSE;
8998
8999 free (symbuf);
9000
9001 free (hash_table->strtab);
9002 hash_table->strtab = NULL;
9003
9004 return ret;
9005 }
9006
9007 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9008
9009 static bfd_boolean
9010 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9011 {
9012 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9013 && sym->st_shndx < SHN_LORESERVE)
9014 {
9015 /* The gABI doesn't support dynamic symbols in output sections
9016 beyond 64k. */
9017 (*_bfd_error_handler)
9018 (_("%B: Too many sections: %d (>= %d)"),
9019 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9020 bfd_set_error (bfd_error_nonrepresentable_section);
9021 return FALSE;
9022 }
9023 return TRUE;
9024 }
9025
9026 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9027 allowing an unsatisfied unversioned symbol in the DSO to match a
9028 versioned symbol that would normally require an explicit version.
9029 We also handle the case that a DSO references a hidden symbol
9030 which may be satisfied by a versioned symbol in another DSO. */
9031
9032 static bfd_boolean
9033 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9034 const struct elf_backend_data *bed,
9035 struct elf_link_hash_entry *h)
9036 {
9037 bfd *abfd;
9038 struct elf_link_loaded_list *loaded;
9039
9040 if (!is_elf_hash_table (info->hash))
9041 return FALSE;
9042
9043 /* Check indirect symbol. */
9044 while (h->root.type == bfd_link_hash_indirect)
9045 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9046
9047 switch (h->root.type)
9048 {
9049 default:
9050 abfd = NULL;
9051 break;
9052
9053 case bfd_link_hash_undefined:
9054 case bfd_link_hash_undefweak:
9055 abfd = h->root.u.undef.abfd;
9056 if ((abfd->flags & DYNAMIC) == 0
9057 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9058 return FALSE;
9059 break;
9060
9061 case bfd_link_hash_defined:
9062 case bfd_link_hash_defweak:
9063 abfd = h->root.u.def.section->owner;
9064 break;
9065
9066 case bfd_link_hash_common:
9067 abfd = h->root.u.c.p->section->owner;
9068 break;
9069 }
9070 BFD_ASSERT (abfd != NULL);
9071
9072 for (loaded = elf_hash_table (info)->loaded;
9073 loaded != NULL;
9074 loaded = loaded->next)
9075 {
9076 bfd *input;
9077 Elf_Internal_Shdr *hdr;
9078 size_t symcount;
9079 size_t extsymcount;
9080 size_t extsymoff;
9081 Elf_Internal_Shdr *versymhdr;
9082 Elf_Internal_Sym *isym;
9083 Elf_Internal_Sym *isymend;
9084 Elf_Internal_Sym *isymbuf;
9085 Elf_External_Versym *ever;
9086 Elf_External_Versym *extversym;
9087
9088 input = loaded->abfd;
9089
9090 /* We check each DSO for a possible hidden versioned definition. */
9091 if (input == abfd
9092 || (input->flags & DYNAMIC) == 0
9093 || elf_dynversym (input) == 0)
9094 continue;
9095
9096 hdr = &elf_tdata (input)->dynsymtab_hdr;
9097
9098 symcount = hdr->sh_size / bed->s->sizeof_sym;
9099 if (elf_bad_symtab (input))
9100 {
9101 extsymcount = symcount;
9102 extsymoff = 0;
9103 }
9104 else
9105 {
9106 extsymcount = symcount - hdr->sh_info;
9107 extsymoff = hdr->sh_info;
9108 }
9109
9110 if (extsymcount == 0)
9111 continue;
9112
9113 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9114 NULL, NULL, NULL);
9115 if (isymbuf == NULL)
9116 return FALSE;
9117
9118 /* Read in any version definitions. */
9119 versymhdr = &elf_tdata (input)->dynversym_hdr;
9120 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
9121 if (extversym == NULL)
9122 goto error_ret;
9123
9124 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9125 || (bfd_bread (extversym, versymhdr->sh_size, input)
9126 != versymhdr->sh_size))
9127 {
9128 free (extversym);
9129 error_ret:
9130 free (isymbuf);
9131 return FALSE;
9132 }
9133
9134 ever = extversym + extsymoff;
9135 isymend = isymbuf + extsymcount;
9136 for (isym = isymbuf; isym < isymend; isym++, ever++)
9137 {
9138 const char *name;
9139 Elf_Internal_Versym iver;
9140 unsigned short version_index;
9141
9142 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9143 || isym->st_shndx == SHN_UNDEF)
9144 continue;
9145
9146 name = bfd_elf_string_from_elf_section (input,
9147 hdr->sh_link,
9148 isym->st_name);
9149 if (strcmp (name, h->root.root.string) != 0)
9150 continue;
9151
9152 _bfd_elf_swap_versym_in (input, ever, &iver);
9153
9154 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9155 && !(h->def_regular
9156 && h->forced_local))
9157 {
9158 /* If we have a non-hidden versioned sym, then it should
9159 have provided a definition for the undefined sym unless
9160 it is defined in a non-shared object and forced local.
9161 */
9162 abort ();
9163 }
9164
9165 version_index = iver.vs_vers & VERSYM_VERSION;
9166 if (version_index == 1 || version_index == 2)
9167 {
9168 /* This is the base or first version. We can use it. */
9169 free (extversym);
9170 free (isymbuf);
9171 return TRUE;
9172 }
9173 }
9174
9175 free (extversym);
9176 free (isymbuf);
9177 }
9178
9179 return FALSE;
9180 }
9181
9182 /* Convert ELF common symbol TYPE. */
9183
9184 static int
9185 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9186 {
9187 /* Commom symbol can only appear in relocatable link. */
9188 if (!bfd_link_relocatable (info))
9189 abort ();
9190 switch (info->elf_stt_common)
9191 {
9192 case unchanged:
9193 break;
9194 case elf_stt_common:
9195 type = STT_COMMON;
9196 break;
9197 case no_elf_stt_common:
9198 type = STT_OBJECT;
9199 break;
9200 }
9201 return type;
9202 }
9203
9204 /* Add an external symbol to the symbol table. This is called from
9205 the hash table traversal routine. When generating a shared object,
9206 we go through the symbol table twice. The first time we output
9207 anything that might have been forced to local scope in a version
9208 script. The second time we output the symbols that are still
9209 global symbols. */
9210
9211 static bfd_boolean
9212 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9213 {
9214 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9215 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9216 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9217 bfd_boolean strip;
9218 Elf_Internal_Sym sym;
9219 asection *input_sec;
9220 const struct elf_backend_data *bed;
9221 long indx;
9222 int ret;
9223 unsigned int type;
9224 /* A symbol is bound locally if it is forced local or it is locally
9225 defined, hidden versioned, not referenced by shared library and
9226 not exported when linking executable. */
9227 bfd_boolean local_bind = (h->forced_local
9228 || (bfd_link_executable (flinfo->info)
9229 && !flinfo->info->export_dynamic
9230 && !h->dynamic
9231 && !h->ref_dynamic
9232 && h->def_regular
9233 && h->versioned == versioned_hidden));
9234
9235 if (h->root.type == bfd_link_hash_warning)
9236 {
9237 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9238 if (h->root.type == bfd_link_hash_new)
9239 return TRUE;
9240 }
9241
9242 /* Decide whether to output this symbol in this pass. */
9243 if (eoinfo->localsyms)
9244 {
9245 if (!local_bind)
9246 return TRUE;
9247 }
9248 else
9249 {
9250 if (local_bind)
9251 return TRUE;
9252 }
9253
9254 bed = get_elf_backend_data (flinfo->output_bfd);
9255
9256 if (h->root.type == bfd_link_hash_undefined)
9257 {
9258 /* If we have an undefined symbol reference here then it must have
9259 come from a shared library that is being linked in. (Undefined
9260 references in regular files have already been handled unless
9261 they are in unreferenced sections which are removed by garbage
9262 collection). */
9263 bfd_boolean ignore_undef = FALSE;
9264
9265 /* Some symbols may be special in that the fact that they're
9266 undefined can be safely ignored - let backend determine that. */
9267 if (bed->elf_backend_ignore_undef_symbol)
9268 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9269
9270 /* If we are reporting errors for this situation then do so now. */
9271 if (!ignore_undef
9272 && h->ref_dynamic
9273 && (!h->ref_regular || flinfo->info->gc_sections)
9274 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9275 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9276 (*flinfo->info->callbacks->undefined_symbol)
9277 (flinfo->info, h->root.root.string,
9278 h->ref_regular ? NULL : h->root.u.undef.abfd,
9279 NULL, 0,
9280 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
9281
9282 /* Strip a global symbol defined in a discarded section. */
9283 if (h->indx == -3)
9284 return TRUE;
9285 }
9286
9287 /* We should also warn if a forced local symbol is referenced from
9288 shared libraries. */
9289 if (bfd_link_executable (flinfo->info)
9290 && h->forced_local
9291 && h->ref_dynamic
9292 && h->def_regular
9293 && !h->dynamic_def
9294 && h->ref_dynamic_nonweak
9295 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9296 {
9297 bfd *def_bfd;
9298 const char *msg;
9299 struct elf_link_hash_entry *hi = h;
9300
9301 /* Check indirect symbol. */
9302 while (hi->root.type == bfd_link_hash_indirect)
9303 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9304
9305 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9306 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9307 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9308 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9309 else
9310 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
9311 def_bfd = flinfo->output_bfd;
9312 if (hi->root.u.def.section != bfd_abs_section_ptr)
9313 def_bfd = hi->root.u.def.section->owner;
9314 (*_bfd_error_handler) (msg, flinfo->output_bfd, def_bfd,
9315 h->root.root.string);
9316 bfd_set_error (bfd_error_bad_value);
9317 eoinfo->failed = TRUE;
9318 return FALSE;
9319 }
9320
9321 /* We don't want to output symbols that have never been mentioned by
9322 a regular file, or that we have been told to strip. However, if
9323 h->indx is set to -2, the symbol is used by a reloc and we must
9324 output it. */
9325 strip = FALSE;
9326 if (h->indx == -2)
9327 ;
9328 else if ((h->def_dynamic
9329 || h->ref_dynamic
9330 || h->root.type == bfd_link_hash_new)
9331 && !h->def_regular
9332 && !h->ref_regular)
9333 strip = TRUE;
9334 else if (flinfo->info->strip == strip_all)
9335 strip = TRUE;
9336 else if (flinfo->info->strip == strip_some
9337 && bfd_hash_lookup (flinfo->info->keep_hash,
9338 h->root.root.string, FALSE, FALSE) == NULL)
9339 strip = TRUE;
9340 else if ((h->root.type == bfd_link_hash_defined
9341 || h->root.type == bfd_link_hash_defweak)
9342 && ((flinfo->info->strip_discarded
9343 && discarded_section (h->root.u.def.section))
9344 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9345 && h->root.u.def.section->owner != NULL
9346 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9347 strip = TRUE;
9348 else if ((h->root.type == bfd_link_hash_undefined
9349 || h->root.type == bfd_link_hash_undefweak)
9350 && h->root.u.undef.abfd != NULL
9351 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9352 strip = TRUE;
9353
9354 type = h->type;
9355
9356 /* If we're stripping it, and it's not a dynamic symbol, there's
9357 nothing else to do. However, if it is a forced local symbol or
9358 an ifunc symbol we need to give the backend finish_dynamic_symbol
9359 function a chance to make it dynamic. */
9360 if (strip
9361 && h->dynindx == -1
9362 && type != STT_GNU_IFUNC
9363 && !h->forced_local)
9364 return TRUE;
9365
9366 sym.st_value = 0;
9367 sym.st_size = h->size;
9368 sym.st_other = h->other;
9369 switch (h->root.type)
9370 {
9371 default:
9372 case bfd_link_hash_new:
9373 case bfd_link_hash_warning:
9374 abort ();
9375 return FALSE;
9376
9377 case bfd_link_hash_undefined:
9378 case bfd_link_hash_undefweak:
9379 input_sec = bfd_und_section_ptr;
9380 sym.st_shndx = SHN_UNDEF;
9381 break;
9382
9383 case bfd_link_hash_defined:
9384 case bfd_link_hash_defweak:
9385 {
9386 input_sec = h->root.u.def.section;
9387 if (input_sec->output_section != NULL)
9388 {
9389 sym.st_shndx =
9390 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9391 input_sec->output_section);
9392 if (sym.st_shndx == SHN_BAD)
9393 {
9394 (*_bfd_error_handler)
9395 (_("%B: could not find output section %A for input section %A"),
9396 flinfo->output_bfd, input_sec->output_section, input_sec);
9397 bfd_set_error (bfd_error_nonrepresentable_section);
9398 eoinfo->failed = TRUE;
9399 return FALSE;
9400 }
9401
9402 /* ELF symbols in relocatable files are section relative,
9403 but in nonrelocatable files they are virtual
9404 addresses. */
9405 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9406 if (!bfd_link_relocatable (flinfo->info))
9407 {
9408 sym.st_value += input_sec->output_section->vma;
9409 if (h->type == STT_TLS)
9410 {
9411 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9412 if (tls_sec != NULL)
9413 sym.st_value -= tls_sec->vma;
9414 }
9415 }
9416 }
9417 else
9418 {
9419 BFD_ASSERT (input_sec->owner == NULL
9420 || (input_sec->owner->flags & DYNAMIC) != 0);
9421 sym.st_shndx = SHN_UNDEF;
9422 input_sec = bfd_und_section_ptr;
9423 }
9424 }
9425 break;
9426
9427 case bfd_link_hash_common:
9428 input_sec = h->root.u.c.p->section;
9429 sym.st_shndx = bed->common_section_index (input_sec);
9430 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9431 break;
9432
9433 case bfd_link_hash_indirect:
9434 /* These symbols are created by symbol versioning. They point
9435 to the decorated version of the name. For example, if the
9436 symbol foo@@GNU_1.2 is the default, which should be used when
9437 foo is used with no version, then we add an indirect symbol
9438 foo which points to foo@@GNU_1.2. We ignore these symbols,
9439 since the indirected symbol is already in the hash table. */
9440 return TRUE;
9441 }
9442
9443 if (type == STT_COMMON || type == STT_OBJECT)
9444 switch (h->root.type)
9445 {
9446 case bfd_link_hash_common:
9447 type = elf_link_convert_common_type (flinfo->info, type);
9448 break;
9449 case bfd_link_hash_defined:
9450 case bfd_link_hash_defweak:
9451 if (bed->common_definition (&sym))
9452 type = elf_link_convert_common_type (flinfo->info, type);
9453 else
9454 type = STT_OBJECT;
9455 break;
9456 case bfd_link_hash_undefined:
9457 case bfd_link_hash_undefweak:
9458 break;
9459 default:
9460 abort ();
9461 }
9462
9463 if (local_bind)
9464 {
9465 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9466 /* Turn off visibility on local symbol. */
9467 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9468 }
9469 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9470 else if (h->unique_global && h->def_regular)
9471 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9472 else if (h->root.type == bfd_link_hash_undefweak
9473 || h->root.type == bfd_link_hash_defweak)
9474 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9475 else
9476 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9477 sym.st_target_internal = h->target_internal;
9478
9479 /* Give the processor backend a chance to tweak the symbol value,
9480 and also to finish up anything that needs to be done for this
9481 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9482 forced local syms when non-shared is due to a historical quirk.
9483 STT_GNU_IFUNC symbol must go through PLT. */
9484 if ((h->type == STT_GNU_IFUNC
9485 && h->def_regular
9486 && !bfd_link_relocatable (flinfo->info))
9487 || ((h->dynindx != -1
9488 || h->forced_local)
9489 && ((bfd_link_pic (flinfo->info)
9490 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9491 || h->root.type != bfd_link_hash_undefweak))
9492 || !h->forced_local)
9493 && elf_hash_table (flinfo->info)->dynamic_sections_created))
9494 {
9495 if (! ((*bed->elf_backend_finish_dynamic_symbol)
9496 (flinfo->output_bfd, flinfo->info, h, &sym)))
9497 {
9498 eoinfo->failed = TRUE;
9499 return FALSE;
9500 }
9501 }
9502
9503 /* If we are marking the symbol as undefined, and there are no
9504 non-weak references to this symbol from a regular object, then
9505 mark the symbol as weak undefined; if there are non-weak
9506 references, mark the symbol as strong. We can't do this earlier,
9507 because it might not be marked as undefined until the
9508 finish_dynamic_symbol routine gets through with it. */
9509 if (sym.st_shndx == SHN_UNDEF
9510 && h->ref_regular
9511 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9512 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9513 {
9514 int bindtype;
9515 type = ELF_ST_TYPE (sym.st_info);
9516
9517 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9518 if (type == STT_GNU_IFUNC)
9519 type = STT_FUNC;
9520
9521 if (h->ref_regular_nonweak)
9522 bindtype = STB_GLOBAL;
9523 else
9524 bindtype = STB_WEAK;
9525 sym.st_info = ELF_ST_INFO (bindtype, type);
9526 }
9527
9528 /* If this is a symbol defined in a dynamic library, don't use the
9529 symbol size from the dynamic library. Relinking an executable
9530 against a new library may introduce gratuitous changes in the
9531 executable's symbols if we keep the size. */
9532 if (sym.st_shndx == SHN_UNDEF
9533 && !h->def_regular
9534 && h->def_dynamic)
9535 sym.st_size = 0;
9536
9537 /* If a non-weak symbol with non-default visibility is not defined
9538 locally, it is a fatal error. */
9539 if (!bfd_link_relocatable (flinfo->info)
9540 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9541 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9542 && h->root.type == bfd_link_hash_undefined
9543 && !h->def_regular)
9544 {
9545 const char *msg;
9546
9547 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9548 msg = _("%B: protected symbol `%s' isn't defined");
9549 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9550 msg = _("%B: internal symbol `%s' isn't defined");
9551 else
9552 msg = _("%B: hidden symbol `%s' isn't defined");
9553 (*_bfd_error_handler) (msg, flinfo->output_bfd, h->root.root.string);
9554 bfd_set_error (bfd_error_bad_value);
9555 eoinfo->failed = TRUE;
9556 return FALSE;
9557 }
9558
9559 /* If this symbol should be put in the .dynsym section, then put it
9560 there now. We already know the symbol index. We also fill in
9561 the entry in the .hash section. */
9562 if (elf_hash_table (flinfo->info)->dynsym != NULL
9563 && h->dynindx != -1
9564 && elf_hash_table (flinfo->info)->dynamic_sections_created)
9565 {
9566 bfd_byte *esym;
9567
9568 /* Since there is no version information in the dynamic string,
9569 if there is no version info in symbol version section, we will
9570 have a run-time problem if not linking executable, referenced
9571 by shared library, not locally defined, or not bound locally.
9572 */
9573 if (h->verinfo.verdef == NULL
9574 && !local_bind
9575 && (!bfd_link_executable (flinfo->info)
9576 || h->ref_dynamic
9577 || !h->def_regular))
9578 {
9579 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9580
9581 if (p && p [1] != '\0')
9582 {
9583 (*_bfd_error_handler)
9584 (_("%B: No symbol version section for versioned symbol `%s'"),
9585 flinfo->output_bfd, h->root.root.string);
9586 eoinfo->failed = TRUE;
9587 return FALSE;
9588 }
9589 }
9590
9591 sym.st_name = h->dynstr_index;
9592 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9593 + h->dynindx * bed->s->sizeof_sym);
9594 if (!check_dynsym (flinfo->output_bfd, &sym))
9595 {
9596 eoinfo->failed = TRUE;
9597 return FALSE;
9598 }
9599 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
9600
9601 if (flinfo->hash_sec != NULL)
9602 {
9603 size_t hash_entry_size;
9604 bfd_byte *bucketpos;
9605 bfd_vma chain;
9606 size_t bucketcount;
9607 size_t bucket;
9608
9609 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
9610 bucket = h->u.elf_hash_value % bucketcount;
9611
9612 hash_entry_size
9613 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9614 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
9615 + (bucket + 2) * hash_entry_size);
9616 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9617 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9618 bucketpos);
9619 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9620 ((bfd_byte *) flinfo->hash_sec->contents
9621 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9622 }
9623
9624 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
9625 {
9626 Elf_Internal_Versym iversym;
9627 Elf_External_Versym *eversym;
9628
9629 if (!h->def_regular)
9630 {
9631 if (h->verinfo.verdef == NULL
9632 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9633 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
9634 iversym.vs_vers = 0;
9635 else
9636 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9637 }
9638 else
9639 {
9640 if (h->verinfo.vertree == NULL)
9641 iversym.vs_vers = 1;
9642 else
9643 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
9644 if (flinfo->info->create_default_symver)
9645 iversym.vs_vers++;
9646 }
9647
9648 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9649 defined locally. */
9650 if (h->versioned == versioned_hidden && h->def_regular)
9651 iversym.vs_vers |= VERSYM_HIDDEN;
9652
9653 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
9654 eversym += h->dynindx;
9655 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
9656 }
9657 }
9658
9659 /* If the symbol is undefined, and we didn't output it to .dynsym,
9660 strip it from .symtab too. Obviously we can't do this for
9661 relocatable output or when needed for --emit-relocs. */
9662 else if (input_sec == bfd_und_section_ptr
9663 && h->indx != -2
9664 && !bfd_link_relocatable (flinfo->info))
9665 return TRUE;
9666 /* Also strip others that we couldn't earlier due to dynamic symbol
9667 processing. */
9668 if (strip)
9669 return TRUE;
9670 if ((input_sec->flags & SEC_EXCLUDE) != 0)
9671 return TRUE;
9672
9673 /* Output a FILE symbol so that following locals are not associated
9674 with the wrong input file. We need one for forced local symbols
9675 if we've seen more than one FILE symbol or when we have exactly
9676 one FILE symbol but global symbols are present in a file other
9677 than the one with the FILE symbol. We also need one if linker
9678 defined symbols are present. In practice these conditions are
9679 always met, so just emit the FILE symbol unconditionally. */
9680 if (eoinfo->localsyms
9681 && !eoinfo->file_sym_done
9682 && eoinfo->flinfo->filesym_count != 0)
9683 {
9684 Elf_Internal_Sym fsym;
9685
9686 memset (&fsym, 0, sizeof (fsym));
9687 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9688 fsym.st_shndx = SHN_ABS;
9689 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9690 bfd_und_section_ptr, NULL))
9691 return FALSE;
9692
9693 eoinfo->file_sym_done = TRUE;
9694 }
9695
9696 indx = bfd_get_symcount (flinfo->output_bfd);
9697 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9698 input_sec, h);
9699 if (ret == 0)
9700 {
9701 eoinfo->failed = TRUE;
9702 return FALSE;
9703 }
9704 else if (ret == 1)
9705 h->indx = indx;
9706 else if (h->indx == -2)
9707 abort();
9708
9709 return TRUE;
9710 }
9711
9712 /* Return TRUE if special handling is done for relocs in SEC against
9713 symbols defined in discarded sections. */
9714
9715 static bfd_boolean
9716 elf_section_ignore_discarded_relocs (asection *sec)
9717 {
9718 const struct elf_backend_data *bed;
9719
9720 switch (sec->sec_info_type)
9721 {
9722 case SEC_INFO_TYPE_STABS:
9723 case SEC_INFO_TYPE_EH_FRAME:
9724 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
9725 return TRUE;
9726 default:
9727 break;
9728 }
9729
9730 bed = get_elf_backend_data (sec->owner);
9731 if (bed->elf_backend_ignore_discarded_relocs != NULL
9732 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9733 return TRUE;
9734
9735 return FALSE;
9736 }
9737
9738 /* Return a mask saying how ld should treat relocations in SEC against
9739 symbols defined in discarded sections. If this function returns
9740 COMPLAIN set, ld will issue a warning message. If this function
9741 returns PRETEND set, and the discarded section was link-once and the
9742 same size as the kept link-once section, ld will pretend that the
9743 symbol was actually defined in the kept section. Otherwise ld will
9744 zero the reloc (at least that is the intent, but some cooperation by
9745 the target dependent code is needed, particularly for REL targets). */
9746
9747 unsigned int
9748 _bfd_elf_default_action_discarded (asection *sec)
9749 {
9750 if (sec->flags & SEC_DEBUGGING)
9751 return PRETEND;
9752
9753 if (strcmp (".eh_frame", sec->name) == 0)
9754 return 0;
9755
9756 if (strcmp (".gcc_except_table", sec->name) == 0)
9757 return 0;
9758
9759 return COMPLAIN | PRETEND;
9760 }
9761
9762 /* Find a match between a section and a member of a section group. */
9763
9764 static asection *
9765 match_group_member (asection *sec, asection *group,
9766 struct bfd_link_info *info)
9767 {
9768 asection *first = elf_next_in_group (group);
9769 asection *s = first;
9770
9771 while (s != NULL)
9772 {
9773 if (bfd_elf_match_symbols_in_sections (s, sec, info))
9774 return s;
9775
9776 s = elf_next_in_group (s);
9777 if (s == first)
9778 break;
9779 }
9780
9781 return NULL;
9782 }
9783
9784 /* Check if the kept section of a discarded section SEC can be used
9785 to replace it. Return the replacement if it is OK. Otherwise return
9786 NULL. */
9787
9788 asection *
9789 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
9790 {
9791 asection *kept;
9792
9793 kept = sec->kept_section;
9794 if (kept != NULL)
9795 {
9796 if ((kept->flags & SEC_GROUP) != 0)
9797 kept = match_group_member (sec, kept, info);
9798 if (kept != NULL
9799 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9800 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
9801 kept = NULL;
9802 sec->kept_section = kept;
9803 }
9804 return kept;
9805 }
9806
9807 /* Link an input file into the linker output file. This function
9808 handles all the sections and relocations of the input file at once.
9809 This is so that we only have to read the local symbols once, and
9810 don't have to keep them in memory. */
9811
9812 static bfd_boolean
9813 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
9814 {
9815 int (*relocate_section)
9816 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9817 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9818 bfd *output_bfd;
9819 Elf_Internal_Shdr *symtab_hdr;
9820 size_t locsymcount;
9821 size_t extsymoff;
9822 Elf_Internal_Sym *isymbuf;
9823 Elf_Internal_Sym *isym;
9824 Elf_Internal_Sym *isymend;
9825 long *pindex;
9826 asection **ppsection;
9827 asection *o;
9828 const struct elf_backend_data *bed;
9829 struct elf_link_hash_entry **sym_hashes;
9830 bfd_size_type address_size;
9831 bfd_vma r_type_mask;
9832 int r_sym_shift;
9833 bfd_boolean have_file_sym = FALSE;
9834
9835 output_bfd = flinfo->output_bfd;
9836 bed = get_elf_backend_data (output_bfd);
9837 relocate_section = bed->elf_backend_relocate_section;
9838
9839 /* If this is a dynamic object, we don't want to do anything here:
9840 we don't want the local symbols, and we don't want the section
9841 contents. */
9842 if ((input_bfd->flags & DYNAMIC) != 0)
9843 return TRUE;
9844
9845 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9846 if (elf_bad_symtab (input_bfd))
9847 {
9848 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9849 extsymoff = 0;
9850 }
9851 else
9852 {
9853 locsymcount = symtab_hdr->sh_info;
9854 extsymoff = symtab_hdr->sh_info;
9855 }
9856
9857 /* Read the local symbols. */
9858 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9859 if (isymbuf == NULL && locsymcount != 0)
9860 {
9861 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
9862 flinfo->internal_syms,
9863 flinfo->external_syms,
9864 flinfo->locsym_shndx);
9865 if (isymbuf == NULL)
9866 return FALSE;
9867 }
9868
9869 /* Find local symbol sections and adjust values of symbols in
9870 SEC_MERGE sections. Write out those local symbols we know are
9871 going into the output file. */
9872 isymend = isymbuf + locsymcount;
9873 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
9874 isym < isymend;
9875 isym++, pindex++, ppsection++)
9876 {
9877 asection *isec;
9878 const char *name;
9879 Elf_Internal_Sym osym;
9880 long indx;
9881 int ret;
9882
9883 *pindex = -1;
9884
9885 if (elf_bad_symtab (input_bfd))
9886 {
9887 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9888 {
9889 *ppsection = NULL;
9890 continue;
9891 }
9892 }
9893
9894 if (isym->st_shndx == SHN_UNDEF)
9895 isec = bfd_und_section_ptr;
9896 else if (isym->st_shndx == SHN_ABS)
9897 isec = bfd_abs_section_ptr;
9898 else if (isym->st_shndx == SHN_COMMON)
9899 isec = bfd_com_section_ptr;
9900 else
9901 {
9902 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9903 if (isec == NULL)
9904 {
9905 /* Don't attempt to output symbols with st_shnx in the
9906 reserved range other than SHN_ABS and SHN_COMMON. */
9907 *ppsection = NULL;
9908 continue;
9909 }
9910 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
9911 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9912 isym->st_value =
9913 _bfd_merged_section_offset (output_bfd, &isec,
9914 elf_section_data (isec)->sec_info,
9915 isym->st_value);
9916 }
9917
9918 *ppsection = isec;
9919
9920 /* Don't output the first, undefined, symbol. In fact, don't
9921 output any undefined local symbol. */
9922 if (isec == bfd_und_section_ptr)
9923 continue;
9924
9925 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9926 {
9927 /* We never output section symbols. Instead, we use the
9928 section symbol of the corresponding section in the output
9929 file. */
9930 continue;
9931 }
9932
9933 /* If we are stripping all symbols, we don't want to output this
9934 one. */
9935 if (flinfo->info->strip == strip_all)
9936 continue;
9937
9938 /* If we are discarding all local symbols, we don't want to
9939 output this one. If we are generating a relocatable output
9940 file, then some of the local symbols may be required by
9941 relocs; we output them below as we discover that they are
9942 needed. */
9943 if (flinfo->info->discard == discard_all)
9944 continue;
9945
9946 /* If this symbol is defined in a section which we are
9947 discarding, we don't need to keep it. */
9948 if (isym->st_shndx != SHN_UNDEF
9949 && isym->st_shndx < SHN_LORESERVE
9950 && bfd_section_removed_from_list (output_bfd,
9951 isec->output_section))
9952 continue;
9953
9954 /* Get the name of the symbol. */
9955 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9956 isym->st_name);
9957 if (name == NULL)
9958 return FALSE;
9959
9960 /* See if we are discarding symbols with this name. */
9961 if ((flinfo->info->strip == strip_some
9962 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
9963 == NULL))
9964 || (((flinfo->info->discard == discard_sec_merge
9965 && (isec->flags & SEC_MERGE)
9966 && !bfd_link_relocatable (flinfo->info))
9967 || flinfo->info->discard == discard_l)
9968 && bfd_is_local_label_name (input_bfd, name)))
9969 continue;
9970
9971 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
9972 {
9973 if (input_bfd->lto_output)
9974 /* -flto puts a temp file name here. This means builds
9975 are not reproducible. Discard the symbol. */
9976 continue;
9977 have_file_sym = TRUE;
9978 flinfo->filesym_count += 1;
9979 }
9980 if (!have_file_sym)
9981 {
9982 /* In the absence of debug info, bfd_find_nearest_line uses
9983 FILE symbols to determine the source file for local
9984 function symbols. Provide a FILE symbol here if input
9985 files lack such, so that their symbols won't be
9986 associated with a previous input file. It's not the
9987 source file, but the best we can do. */
9988 have_file_sym = TRUE;
9989 flinfo->filesym_count += 1;
9990 memset (&osym, 0, sizeof (osym));
9991 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9992 osym.st_shndx = SHN_ABS;
9993 if (!elf_link_output_symstrtab (flinfo,
9994 (input_bfd->lto_output ? NULL
9995 : input_bfd->filename),
9996 &osym, bfd_abs_section_ptr,
9997 NULL))
9998 return FALSE;
9999 }
10000
10001 osym = *isym;
10002
10003 /* Adjust the section index for the output file. */
10004 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10005 isec->output_section);
10006 if (osym.st_shndx == SHN_BAD)
10007 return FALSE;
10008
10009 /* ELF symbols in relocatable files are section relative, but
10010 in executable files they are virtual addresses. Note that
10011 this code assumes that all ELF sections have an associated
10012 BFD section with a reasonable value for output_offset; below
10013 we assume that they also have a reasonable value for
10014 output_section. Any special sections must be set up to meet
10015 these requirements. */
10016 osym.st_value += isec->output_offset;
10017 if (!bfd_link_relocatable (flinfo->info))
10018 {
10019 osym.st_value += isec->output_section->vma;
10020 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10021 {
10022 /* STT_TLS symbols are relative to PT_TLS segment base. */
10023 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10024 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10025 }
10026 }
10027
10028 indx = bfd_get_symcount (output_bfd);
10029 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10030 if (ret == 0)
10031 return FALSE;
10032 else if (ret == 1)
10033 *pindex = indx;
10034 }
10035
10036 if (bed->s->arch_size == 32)
10037 {
10038 r_type_mask = 0xff;
10039 r_sym_shift = 8;
10040 address_size = 4;
10041 }
10042 else
10043 {
10044 r_type_mask = 0xffffffff;
10045 r_sym_shift = 32;
10046 address_size = 8;
10047 }
10048
10049 /* Relocate the contents of each section. */
10050 sym_hashes = elf_sym_hashes (input_bfd);
10051 for (o = input_bfd->sections; o != NULL; o = o->next)
10052 {
10053 bfd_byte *contents;
10054
10055 if (! o->linker_mark)
10056 {
10057 /* This section was omitted from the link. */
10058 continue;
10059 }
10060
10061 if (bfd_link_relocatable (flinfo->info)
10062 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10063 {
10064 /* Deal with the group signature symbol. */
10065 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10066 unsigned long symndx = sec_data->this_hdr.sh_info;
10067 asection *osec = o->output_section;
10068
10069 if (symndx >= locsymcount
10070 || (elf_bad_symtab (input_bfd)
10071 && flinfo->sections[symndx] == NULL))
10072 {
10073 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10074 while (h->root.type == bfd_link_hash_indirect
10075 || h->root.type == bfd_link_hash_warning)
10076 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10077 /* Arrange for symbol to be output. */
10078 h->indx = -2;
10079 elf_section_data (osec)->this_hdr.sh_info = -2;
10080 }
10081 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10082 {
10083 /* We'll use the output section target_index. */
10084 asection *sec = flinfo->sections[symndx]->output_section;
10085 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10086 }
10087 else
10088 {
10089 if (flinfo->indices[symndx] == -1)
10090 {
10091 /* Otherwise output the local symbol now. */
10092 Elf_Internal_Sym sym = isymbuf[symndx];
10093 asection *sec = flinfo->sections[symndx]->output_section;
10094 const char *name;
10095 long indx;
10096 int ret;
10097
10098 name = bfd_elf_string_from_elf_section (input_bfd,
10099 symtab_hdr->sh_link,
10100 sym.st_name);
10101 if (name == NULL)
10102 return FALSE;
10103
10104 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10105 sec);
10106 if (sym.st_shndx == SHN_BAD)
10107 return FALSE;
10108
10109 sym.st_value += o->output_offset;
10110
10111 indx = bfd_get_symcount (output_bfd);
10112 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10113 NULL);
10114 if (ret == 0)
10115 return FALSE;
10116 else if (ret == 1)
10117 flinfo->indices[symndx] = indx;
10118 else
10119 abort ();
10120 }
10121 elf_section_data (osec)->this_hdr.sh_info
10122 = flinfo->indices[symndx];
10123 }
10124 }
10125
10126 if ((o->flags & SEC_HAS_CONTENTS) == 0
10127 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10128 continue;
10129
10130 if ((o->flags & SEC_LINKER_CREATED) != 0)
10131 {
10132 /* Section was created by _bfd_elf_link_create_dynamic_sections
10133 or somesuch. */
10134 continue;
10135 }
10136
10137 /* Get the contents of the section. They have been cached by a
10138 relaxation routine. Note that o is a section in an input
10139 file, so the contents field will not have been set by any of
10140 the routines which work on output files. */
10141 if (elf_section_data (o)->this_hdr.contents != NULL)
10142 {
10143 contents = elf_section_data (o)->this_hdr.contents;
10144 if (bed->caches_rawsize
10145 && o->rawsize != 0
10146 && o->rawsize < o->size)
10147 {
10148 memcpy (flinfo->contents, contents, o->rawsize);
10149 contents = flinfo->contents;
10150 }
10151 }
10152 else
10153 {
10154 contents = flinfo->contents;
10155 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10156 return FALSE;
10157 }
10158
10159 if ((o->flags & SEC_RELOC) != 0)
10160 {
10161 Elf_Internal_Rela *internal_relocs;
10162 Elf_Internal_Rela *rel, *relend;
10163 int action_discarded;
10164 int ret;
10165
10166 /* Get the swapped relocs. */
10167 internal_relocs
10168 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10169 flinfo->internal_relocs, FALSE);
10170 if (internal_relocs == NULL
10171 && o->reloc_count > 0)
10172 return FALSE;
10173
10174 /* We need to reverse-copy input .ctors/.dtors sections if
10175 they are placed in .init_array/.finit_array for output. */
10176 if (o->size > address_size
10177 && ((strncmp (o->name, ".ctors", 6) == 0
10178 && strcmp (o->output_section->name,
10179 ".init_array") == 0)
10180 || (strncmp (o->name, ".dtors", 6) == 0
10181 && strcmp (o->output_section->name,
10182 ".fini_array") == 0))
10183 && (o->name[6] == 0 || o->name[6] == '.'))
10184 {
10185 if (o->size != o->reloc_count * address_size)
10186 {
10187 (*_bfd_error_handler)
10188 (_("error: %B: size of section %A is not "
10189 "multiple of address size"),
10190 input_bfd, o);
10191 bfd_set_error (bfd_error_on_input);
10192 return FALSE;
10193 }
10194 o->flags |= SEC_ELF_REVERSE_COPY;
10195 }
10196
10197 action_discarded = -1;
10198 if (!elf_section_ignore_discarded_relocs (o))
10199 action_discarded = (*bed->action_discarded) (o);
10200
10201 /* Run through the relocs evaluating complex reloc symbols and
10202 looking for relocs against symbols from discarded sections
10203 or section symbols from removed link-once sections.
10204 Complain about relocs against discarded sections. Zero
10205 relocs against removed link-once sections. */
10206
10207 rel = internal_relocs;
10208 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
10209 for ( ; rel < relend; rel++)
10210 {
10211 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10212 unsigned int s_type;
10213 asection **ps, *sec;
10214 struct elf_link_hash_entry *h = NULL;
10215 const char *sym_name;
10216
10217 if (r_symndx == STN_UNDEF)
10218 continue;
10219
10220 if (r_symndx >= locsymcount
10221 || (elf_bad_symtab (input_bfd)
10222 && flinfo->sections[r_symndx] == NULL))
10223 {
10224 h = sym_hashes[r_symndx - extsymoff];
10225
10226 /* Badly formatted input files can contain relocs that
10227 reference non-existant symbols. Check here so that
10228 we do not seg fault. */
10229 if (h == NULL)
10230 {
10231 char buffer [32];
10232
10233 sprintf_vma (buffer, rel->r_info);
10234 (*_bfd_error_handler)
10235 (_("error: %B contains a reloc (0x%s) for section %A "
10236 "that references a non-existent global symbol"),
10237 input_bfd, o, buffer);
10238 bfd_set_error (bfd_error_bad_value);
10239 return FALSE;
10240 }
10241
10242 while (h->root.type == bfd_link_hash_indirect
10243 || h->root.type == bfd_link_hash_warning)
10244 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10245
10246 s_type = h->type;
10247
10248 /* If a plugin symbol is referenced from a non-IR file,
10249 mark the symbol as undefined. Note that the
10250 linker may attach linker created dynamic sections
10251 to the plugin bfd. Symbols defined in linker
10252 created sections are not plugin symbols. */
10253 if (h->root.non_ir_ref
10254 && (h->root.type == bfd_link_hash_defined
10255 || h->root.type == bfd_link_hash_defweak)
10256 && (h->root.u.def.section->flags
10257 & SEC_LINKER_CREATED) == 0
10258 && h->root.u.def.section->owner != NULL
10259 && (h->root.u.def.section->owner->flags
10260 & BFD_PLUGIN) != 0)
10261 {
10262 h->root.type = bfd_link_hash_undefined;
10263 h->root.u.undef.abfd = h->root.u.def.section->owner;
10264 }
10265
10266 ps = NULL;
10267 if (h->root.type == bfd_link_hash_defined
10268 || h->root.type == bfd_link_hash_defweak)
10269 ps = &h->root.u.def.section;
10270
10271 sym_name = h->root.root.string;
10272 }
10273 else
10274 {
10275 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10276
10277 s_type = ELF_ST_TYPE (sym->st_info);
10278 ps = &flinfo->sections[r_symndx];
10279 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10280 sym, *ps);
10281 }
10282
10283 if ((s_type == STT_RELC || s_type == STT_SRELC)
10284 && !bfd_link_relocatable (flinfo->info))
10285 {
10286 bfd_vma val;
10287 bfd_vma dot = (rel->r_offset
10288 + o->output_offset + o->output_section->vma);
10289 #ifdef DEBUG
10290 printf ("Encountered a complex symbol!");
10291 printf (" (input_bfd %s, section %s, reloc %ld\n",
10292 input_bfd->filename, o->name,
10293 (long) (rel - internal_relocs));
10294 printf (" symbol: idx %8.8lx, name %s\n",
10295 r_symndx, sym_name);
10296 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10297 (unsigned long) rel->r_info,
10298 (unsigned long) rel->r_offset);
10299 #endif
10300 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10301 isymbuf, locsymcount, s_type == STT_SRELC))
10302 return FALSE;
10303
10304 /* Symbol evaluated OK. Update to absolute value. */
10305 set_symbol_value (input_bfd, isymbuf, locsymcount,
10306 r_symndx, val);
10307 continue;
10308 }
10309
10310 if (action_discarded != -1 && ps != NULL)
10311 {
10312 /* Complain if the definition comes from a
10313 discarded section. */
10314 if ((sec = *ps) != NULL && discarded_section (sec))
10315 {
10316 BFD_ASSERT (r_symndx != STN_UNDEF);
10317 if (action_discarded & COMPLAIN)
10318 (*flinfo->info->callbacks->einfo)
10319 (_("%X`%s' referenced in section `%A' of %B: "
10320 "defined in discarded section `%A' of %B\n"),
10321 sym_name, o, input_bfd, sec, sec->owner);
10322
10323 /* Try to do the best we can to support buggy old
10324 versions of gcc. Pretend that the symbol is
10325 really defined in the kept linkonce section.
10326 FIXME: This is quite broken. Modifying the
10327 symbol here means we will be changing all later
10328 uses of the symbol, not just in this section. */
10329 if (action_discarded & PRETEND)
10330 {
10331 asection *kept;
10332
10333 kept = _bfd_elf_check_kept_section (sec,
10334 flinfo->info);
10335 if (kept != NULL)
10336 {
10337 *ps = kept;
10338 continue;
10339 }
10340 }
10341 }
10342 }
10343 }
10344
10345 /* Relocate the section by invoking a back end routine.
10346
10347 The back end routine is responsible for adjusting the
10348 section contents as necessary, and (if using Rela relocs
10349 and generating a relocatable output file) adjusting the
10350 reloc addend as necessary.
10351
10352 The back end routine does not have to worry about setting
10353 the reloc address or the reloc symbol index.
10354
10355 The back end routine is given a pointer to the swapped in
10356 internal symbols, and can access the hash table entries
10357 for the external symbols via elf_sym_hashes (input_bfd).
10358
10359 When generating relocatable output, the back end routine
10360 must handle STB_LOCAL/STT_SECTION symbols specially. The
10361 output symbol is going to be a section symbol
10362 corresponding to the output section, which will require
10363 the addend to be adjusted. */
10364
10365 ret = (*relocate_section) (output_bfd, flinfo->info,
10366 input_bfd, o, contents,
10367 internal_relocs,
10368 isymbuf,
10369 flinfo->sections);
10370 if (!ret)
10371 return FALSE;
10372
10373 if (ret == 2
10374 || bfd_link_relocatable (flinfo->info)
10375 || flinfo->info->emitrelocations)
10376 {
10377 Elf_Internal_Rela *irela;
10378 Elf_Internal_Rela *irelaend, *irelamid;
10379 bfd_vma last_offset;
10380 struct elf_link_hash_entry **rel_hash;
10381 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10382 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10383 unsigned int next_erel;
10384 bfd_boolean rela_normal;
10385 struct bfd_elf_section_data *esdi, *esdo;
10386
10387 esdi = elf_section_data (o);
10388 esdo = elf_section_data (o->output_section);
10389 rela_normal = FALSE;
10390
10391 /* Adjust the reloc addresses and symbol indices. */
10392
10393 irela = internal_relocs;
10394 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
10395 rel_hash = esdo->rel.hashes + esdo->rel.count;
10396 /* We start processing the REL relocs, if any. When we reach
10397 IRELAMID in the loop, we switch to the RELA relocs. */
10398 irelamid = irela;
10399 if (esdi->rel.hdr != NULL)
10400 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10401 * bed->s->int_rels_per_ext_rel);
10402 rel_hash_list = rel_hash;
10403 rela_hash_list = NULL;
10404 last_offset = o->output_offset;
10405 if (!bfd_link_relocatable (flinfo->info))
10406 last_offset += o->output_section->vma;
10407 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10408 {
10409 unsigned long r_symndx;
10410 asection *sec;
10411 Elf_Internal_Sym sym;
10412
10413 if (next_erel == bed->s->int_rels_per_ext_rel)
10414 {
10415 rel_hash++;
10416 next_erel = 0;
10417 }
10418
10419 if (irela == irelamid)
10420 {
10421 rel_hash = esdo->rela.hashes + esdo->rela.count;
10422 rela_hash_list = rel_hash;
10423 rela_normal = bed->rela_normal;
10424 }
10425
10426 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10427 flinfo->info, o,
10428 irela->r_offset);
10429 if (irela->r_offset >= (bfd_vma) -2)
10430 {
10431 /* This is a reloc for a deleted entry or somesuch.
10432 Turn it into an R_*_NONE reloc, at the same
10433 offset as the last reloc. elf_eh_frame.c and
10434 bfd_elf_discard_info rely on reloc offsets
10435 being ordered. */
10436 irela->r_offset = last_offset;
10437 irela->r_info = 0;
10438 irela->r_addend = 0;
10439 continue;
10440 }
10441
10442 irela->r_offset += o->output_offset;
10443
10444 /* Relocs in an executable have to be virtual addresses. */
10445 if (!bfd_link_relocatable (flinfo->info))
10446 irela->r_offset += o->output_section->vma;
10447
10448 last_offset = irela->r_offset;
10449
10450 r_symndx = irela->r_info >> r_sym_shift;
10451 if (r_symndx == STN_UNDEF)
10452 continue;
10453
10454 if (r_symndx >= locsymcount
10455 || (elf_bad_symtab (input_bfd)
10456 && flinfo->sections[r_symndx] == NULL))
10457 {
10458 struct elf_link_hash_entry *rh;
10459 unsigned long indx;
10460
10461 /* This is a reloc against a global symbol. We
10462 have not yet output all the local symbols, so
10463 we do not know the symbol index of any global
10464 symbol. We set the rel_hash entry for this
10465 reloc to point to the global hash table entry
10466 for this symbol. The symbol index is then
10467 set at the end of bfd_elf_final_link. */
10468 indx = r_symndx - extsymoff;
10469 rh = elf_sym_hashes (input_bfd)[indx];
10470 while (rh->root.type == bfd_link_hash_indirect
10471 || rh->root.type == bfd_link_hash_warning)
10472 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10473
10474 /* Setting the index to -2 tells
10475 elf_link_output_extsym that this symbol is
10476 used by a reloc. */
10477 BFD_ASSERT (rh->indx < 0);
10478 rh->indx = -2;
10479
10480 *rel_hash = rh;
10481
10482 continue;
10483 }
10484
10485 /* This is a reloc against a local symbol. */
10486
10487 *rel_hash = NULL;
10488 sym = isymbuf[r_symndx];
10489 sec = flinfo->sections[r_symndx];
10490 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10491 {
10492 /* I suppose the backend ought to fill in the
10493 section of any STT_SECTION symbol against a
10494 processor specific section. */
10495 r_symndx = STN_UNDEF;
10496 if (bfd_is_abs_section (sec))
10497 ;
10498 else if (sec == NULL || sec->owner == NULL)
10499 {
10500 bfd_set_error (bfd_error_bad_value);
10501 return FALSE;
10502 }
10503 else
10504 {
10505 asection *osec = sec->output_section;
10506
10507 /* If we have discarded a section, the output
10508 section will be the absolute section. In
10509 case of discarded SEC_MERGE sections, use
10510 the kept section. relocate_section should
10511 have already handled discarded linkonce
10512 sections. */
10513 if (bfd_is_abs_section (osec)
10514 && sec->kept_section != NULL
10515 && sec->kept_section->output_section != NULL)
10516 {
10517 osec = sec->kept_section->output_section;
10518 irela->r_addend -= osec->vma;
10519 }
10520
10521 if (!bfd_is_abs_section (osec))
10522 {
10523 r_symndx = osec->target_index;
10524 if (r_symndx == STN_UNDEF)
10525 {
10526 irela->r_addend += osec->vma;
10527 osec = _bfd_nearby_section (output_bfd, osec,
10528 osec->vma);
10529 irela->r_addend -= osec->vma;
10530 r_symndx = osec->target_index;
10531 }
10532 }
10533 }
10534
10535 /* Adjust the addend according to where the
10536 section winds up in the output section. */
10537 if (rela_normal)
10538 irela->r_addend += sec->output_offset;
10539 }
10540 else
10541 {
10542 if (flinfo->indices[r_symndx] == -1)
10543 {
10544 unsigned long shlink;
10545 const char *name;
10546 asection *osec;
10547 long indx;
10548
10549 if (flinfo->info->strip == strip_all)
10550 {
10551 /* You can't do ld -r -s. */
10552 bfd_set_error (bfd_error_invalid_operation);
10553 return FALSE;
10554 }
10555
10556 /* This symbol was skipped earlier, but
10557 since it is needed by a reloc, we
10558 must output it now. */
10559 shlink = symtab_hdr->sh_link;
10560 name = (bfd_elf_string_from_elf_section
10561 (input_bfd, shlink, sym.st_name));
10562 if (name == NULL)
10563 return FALSE;
10564
10565 osec = sec->output_section;
10566 sym.st_shndx =
10567 _bfd_elf_section_from_bfd_section (output_bfd,
10568 osec);
10569 if (sym.st_shndx == SHN_BAD)
10570 return FALSE;
10571
10572 sym.st_value += sec->output_offset;
10573 if (!bfd_link_relocatable (flinfo->info))
10574 {
10575 sym.st_value += osec->vma;
10576 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10577 {
10578 /* STT_TLS symbols are relative to PT_TLS
10579 segment base. */
10580 BFD_ASSERT (elf_hash_table (flinfo->info)
10581 ->tls_sec != NULL);
10582 sym.st_value -= (elf_hash_table (flinfo->info)
10583 ->tls_sec->vma);
10584 }
10585 }
10586
10587 indx = bfd_get_symcount (output_bfd);
10588 ret = elf_link_output_symstrtab (flinfo, name,
10589 &sym, sec,
10590 NULL);
10591 if (ret == 0)
10592 return FALSE;
10593 else if (ret == 1)
10594 flinfo->indices[r_symndx] = indx;
10595 else
10596 abort ();
10597 }
10598
10599 r_symndx = flinfo->indices[r_symndx];
10600 }
10601
10602 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10603 | (irela->r_info & r_type_mask));
10604 }
10605
10606 /* Swap out the relocs. */
10607 input_rel_hdr = esdi->rel.hdr;
10608 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
10609 {
10610 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10611 input_rel_hdr,
10612 internal_relocs,
10613 rel_hash_list))
10614 return FALSE;
10615 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10616 * bed->s->int_rels_per_ext_rel);
10617 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
10618 }
10619
10620 input_rela_hdr = esdi->rela.hdr;
10621 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10622 {
10623 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10624 input_rela_hdr,
10625 internal_relocs,
10626 rela_hash_list))
10627 return FALSE;
10628 }
10629 }
10630 }
10631
10632 /* Write out the modified section contents. */
10633 if (bed->elf_backend_write_section
10634 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
10635 contents))
10636 {
10637 /* Section written out. */
10638 }
10639 else switch (o->sec_info_type)
10640 {
10641 case SEC_INFO_TYPE_STABS:
10642 if (! (_bfd_write_section_stabs
10643 (output_bfd,
10644 &elf_hash_table (flinfo->info)->stab_info,
10645 o, &elf_section_data (o)->sec_info, contents)))
10646 return FALSE;
10647 break;
10648 case SEC_INFO_TYPE_MERGE:
10649 if (! _bfd_write_merged_section (output_bfd, o,
10650 elf_section_data (o)->sec_info))
10651 return FALSE;
10652 break;
10653 case SEC_INFO_TYPE_EH_FRAME:
10654 {
10655 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
10656 o, contents))
10657 return FALSE;
10658 }
10659 break;
10660 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10661 {
10662 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10663 flinfo->info,
10664 o, contents))
10665 return FALSE;
10666 }
10667 break;
10668 default:
10669 {
10670 if (! (o->flags & SEC_EXCLUDE))
10671 {
10672 file_ptr offset = (file_ptr) o->output_offset;
10673 bfd_size_type todo = o->size;
10674
10675 offset *= bfd_octets_per_byte (output_bfd);
10676
10677 if ((o->flags & SEC_ELF_REVERSE_COPY))
10678 {
10679 /* Reverse-copy input section to output. */
10680 do
10681 {
10682 todo -= address_size;
10683 if (! bfd_set_section_contents (output_bfd,
10684 o->output_section,
10685 contents + todo,
10686 offset,
10687 address_size))
10688 return FALSE;
10689 if (todo == 0)
10690 break;
10691 offset += address_size;
10692 }
10693 while (1);
10694 }
10695 else if (! bfd_set_section_contents (output_bfd,
10696 o->output_section,
10697 contents,
10698 offset, todo))
10699 return FALSE;
10700 }
10701 }
10702 break;
10703 }
10704 }
10705
10706 return TRUE;
10707 }
10708
10709 /* Generate a reloc when linking an ELF file. This is a reloc
10710 requested by the linker, and does not come from any input file. This
10711 is used to build constructor and destructor tables when linking
10712 with -Ur. */
10713
10714 static bfd_boolean
10715 elf_reloc_link_order (bfd *output_bfd,
10716 struct bfd_link_info *info,
10717 asection *output_section,
10718 struct bfd_link_order *link_order)
10719 {
10720 reloc_howto_type *howto;
10721 long indx;
10722 bfd_vma offset;
10723 bfd_vma addend;
10724 struct bfd_elf_section_reloc_data *reldata;
10725 struct elf_link_hash_entry **rel_hash_ptr;
10726 Elf_Internal_Shdr *rel_hdr;
10727 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10728 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10729 bfd_byte *erel;
10730 unsigned int i;
10731 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
10732
10733 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10734 if (howto == NULL)
10735 {
10736 bfd_set_error (bfd_error_bad_value);
10737 return FALSE;
10738 }
10739
10740 addend = link_order->u.reloc.p->addend;
10741
10742 if (esdo->rel.hdr)
10743 reldata = &esdo->rel;
10744 else if (esdo->rela.hdr)
10745 reldata = &esdo->rela;
10746 else
10747 {
10748 reldata = NULL;
10749 BFD_ASSERT (0);
10750 }
10751
10752 /* Figure out the symbol index. */
10753 rel_hash_ptr = reldata->hashes + reldata->count;
10754 if (link_order->type == bfd_section_reloc_link_order)
10755 {
10756 indx = link_order->u.reloc.p->u.section->target_index;
10757 BFD_ASSERT (indx != 0);
10758 *rel_hash_ptr = NULL;
10759 }
10760 else
10761 {
10762 struct elf_link_hash_entry *h;
10763
10764 /* Treat a reloc against a defined symbol as though it were
10765 actually against the section. */
10766 h = ((struct elf_link_hash_entry *)
10767 bfd_wrapped_link_hash_lookup (output_bfd, info,
10768 link_order->u.reloc.p->u.name,
10769 FALSE, FALSE, TRUE));
10770 if (h != NULL
10771 && (h->root.type == bfd_link_hash_defined
10772 || h->root.type == bfd_link_hash_defweak))
10773 {
10774 asection *section;
10775
10776 section = h->root.u.def.section;
10777 indx = section->output_section->target_index;
10778 *rel_hash_ptr = NULL;
10779 /* It seems that we ought to add the symbol value to the
10780 addend here, but in practice it has already been added
10781 because it was passed to constructor_callback. */
10782 addend += section->output_section->vma + section->output_offset;
10783 }
10784 else if (h != NULL)
10785 {
10786 /* Setting the index to -2 tells elf_link_output_extsym that
10787 this symbol is used by a reloc. */
10788 h->indx = -2;
10789 *rel_hash_ptr = h;
10790 indx = 0;
10791 }
10792 else
10793 {
10794 (*info->callbacks->unattached_reloc)
10795 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
10796 indx = 0;
10797 }
10798 }
10799
10800 /* If this is an inplace reloc, we must write the addend into the
10801 object file. */
10802 if (howto->partial_inplace && addend != 0)
10803 {
10804 bfd_size_type size;
10805 bfd_reloc_status_type rstat;
10806 bfd_byte *buf;
10807 bfd_boolean ok;
10808 const char *sym_name;
10809
10810 size = (bfd_size_type) bfd_get_reloc_size (howto);
10811 buf = (bfd_byte *) bfd_zmalloc (size);
10812 if (buf == NULL && size != 0)
10813 return FALSE;
10814 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10815 switch (rstat)
10816 {
10817 case bfd_reloc_ok:
10818 break;
10819
10820 default:
10821 case bfd_reloc_outofrange:
10822 abort ();
10823
10824 case bfd_reloc_overflow:
10825 if (link_order->type == bfd_section_reloc_link_order)
10826 sym_name = bfd_section_name (output_bfd,
10827 link_order->u.reloc.p->u.section);
10828 else
10829 sym_name = link_order->u.reloc.p->u.name;
10830 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
10831 howto->name, addend, NULL, NULL,
10832 (bfd_vma) 0);
10833 break;
10834 }
10835
10836 ok = bfd_set_section_contents (output_bfd, output_section, buf,
10837 link_order->offset
10838 * bfd_octets_per_byte (output_bfd),
10839 size);
10840 free (buf);
10841 if (! ok)
10842 return FALSE;
10843 }
10844
10845 /* The address of a reloc is relative to the section in a
10846 relocatable file, and is a virtual address in an executable
10847 file. */
10848 offset = link_order->offset;
10849 if (! bfd_link_relocatable (info))
10850 offset += output_section->vma;
10851
10852 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10853 {
10854 irel[i].r_offset = offset;
10855 irel[i].r_info = 0;
10856 irel[i].r_addend = 0;
10857 }
10858 if (bed->s->arch_size == 32)
10859 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10860 else
10861 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10862
10863 rel_hdr = reldata->hdr;
10864 erel = rel_hdr->contents;
10865 if (rel_hdr->sh_type == SHT_REL)
10866 {
10867 erel += reldata->count * bed->s->sizeof_rel;
10868 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10869 }
10870 else
10871 {
10872 irel[0].r_addend = addend;
10873 erel += reldata->count * bed->s->sizeof_rela;
10874 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10875 }
10876
10877 ++reldata->count;
10878
10879 return TRUE;
10880 }
10881
10882
10883 /* Get the output vma of the section pointed to by the sh_link field. */
10884
10885 static bfd_vma
10886 elf_get_linked_section_vma (struct bfd_link_order *p)
10887 {
10888 Elf_Internal_Shdr **elf_shdrp;
10889 asection *s;
10890 int elfsec;
10891
10892 s = p->u.indirect.section;
10893 elf_shdrp = elf_elfsections (s->owner);
10894 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10895 elfsec = elf_shdrp[elfsec]->sh_link;
10896 /* PR 290:
10897 The Intel C compiler generates SHT_IA_64_UNWIND with
10898 SHF_LINK_ORDER. But it doesn't set the sh_link or
10899 sh_info fields. Hence we could get the situation
10900 where elfsec is 0. */
10901 if (elfsec == 0)
10902 {
10903 const struct elf_backend_data *bed
10904 = get_elf_backend_data (s->owner);
10905 if (bed->link_order_error_handler)
10906 bed->link_order_error_handler
10907 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
10908 return 0;
10909 }
10910 else
10911 {
10912 s = elf_shdrp[elfsec]->bfd_section;
10913 return s->output_section->vma + s->output_offset;
10914 }
10915 }
10916
10917
10918 /* Compare two sections based on the locations of the sections they are
10919 linked to. Used by elf_fixup_link_order. */
10920
10921 static int
10922 compare_link_order (const void * a, const void * b)
10923 {
10924 bfd_vma apos;
10925 bfd_vma bpos;
10926
10927 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10928 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10929 if (apos < bpos)
10930 return -1;
10931 return apos > bpos;
10932 }
10933
10934
10935 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10936 order as their linked sections. Returns false if this could not be done
10937 because an output section includes both ordered and unordered
10938 sections. Ideally we'd do this in the linker proper. */
10939
10940 static bfd_boolean
10941 elf_fixup_link_order (bfd *abfd, asection *o)
10942 {
10943 int seen_linkorder;
10944 int seen_other;
10945 int n;
10946 struct bfd_link_order *p;
10947 bfd *sub;
10948 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10949 unsigned elfsec;
10950 struct bfd_link_order **sections;
10951 asection *s, *other_sec, *linkorder_sec;
10952 bfd_vma offset;
10953
10954 other_sec = NULL;
10955 linkorder_sec = NULL;
10956 seen_other = 0;
10957 seen_linkorder = 0;
10958 for (p = o->map_head.link_order; p != NULL; p = p->next)
10959 {
10960 if (p->type == bfd_indirect_link_order)
10961 {
10962 s = p->u.indirect.section;
10963 sub = s->owner;
10964 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10965 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
10966 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
10967 && elfsec < elf_numsections (sub)
10968 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
10969 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
10970 {
10971 seen_linkorder++;
10972 linkorder_sec = s;
10973 }
10974 else
10975 {
10976 seen_other++;
10977 other_sec = s;
10978 }
10979 }
10980 else
10981 seen_other++;
10982
10983 if (seen_other && seen_linkorder)
10984 {
10985 if (other_sec && linkorder_sec)
10986 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10987 o, linkorder_sec,
10988 linkorder_sec->owner, other_sec,
10989 other_sec->owner);
10990 else
10991 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
10992 o);
10993 bfd_set_error (bfd_error_bad_value);
10994 return FALSE;
10995 }
10996 }
10997
10998 if (!seen_linkorder)
10999 return TRUE;
11000
11001 sections = (struct bfd_link_order **)
11002 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11003 if (sections == NULL)
11004 return FALSE;
11005 seen_linkorder = 0;
11006
11007 for (p = o->map_head.link_order; p != NULL; p = p->next)
11008 {
11009 sections[seen_linkorder++] = p;
11010 }
11011 /* Sort the input sections in the order of their linked section. */
11012 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11013 compare_link_order);
11014
11015 /* Change the offsets of the sections. */
11016 offset = 0;
11017 for (n = 0; n < seen_linkorder; n++)
11018 {
11019 s = sections[n]->u.indirect.section;
11020 offset &= ~(bfd_vma) 0 << s->alignment_power;
11021 s->output_offset = offset / bfd_octets_per_byte (abfd);
11022 sections[n]->offset = offset;
11023 offset += sections[n]->size;
11024 }
11025
11026 free (sections);
11027 return TRUE;
11028 }
11029
11030 static void
11031 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11032 {
11033 asection *o;
11034
11035 if (flinfo->symstrtab != NULL)
11036 _bfd_elf_strtab_free (flinfo->symstrtab);
11037 if (flinfo->contents != NULL)
11038 free (flinfo->contents);
11039 if (flinfo->external_relocs != NULL)
11040 free (flinfo->external_relocs);
11041 if (flinfo->internal_relocs != NULL)
11042 free (flinfo->internal_relocs);
11043 if (flinfo->external_syms != NULL)
11044 free (flinfo->external_syms);
11045 if (flinfo->locsym_shndx != NULL)
11046 free (flinfo->locsym_shndx);
11047 if (flinfo->internal_syms != NULL)
11048 free (flinfo->internal_syms);
11049 if (flinfo->indices != NULL)
11050 free (flinfo->indices);
11051 if (flinfo->sections != NULL)
11052 free (flinfo->sections);
11053 if (flinfo->symshndxbuf != NULL)
11054 free (flinfo->symshndxbuf);
11055 for (o = obfd->sections; o != NULL; o = o->next)
11056 {
11057 struct bfd_elf_section_data *esdo = elf_section_data (o);
11058 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11059 free (esdo->rel.hashes);
11060 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11061 free (esdo->rela.hashes);
11062 }
11063 }
11064
11065 /* Do the final step of an ELF link. */
11066
11067 bfd_boolean
11068 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11069 {
11070 bfd_boolean dynamic;
11071 bfd_boolean emit_relocs;
11072 bfd *dynobj;
11073 struct elf_final_link_info flinfo;
11074 asection *o;
11075 struct bfd_link_order *p;
11076 bfd *sub;
11077 bfd_size_type max_contents_size;
11078 bfd_size_type max_external_reloc_size;
11079 bfd_size_type max_internal_reloc_count;
11080 bfd_size_type max_sym_count;
11081 bfd_size_type max_sym_shndx_count;
11082 Elf_Internal_Sym elfsym;
11083 unsigned int i;
11084 Elf_Internal_Shdr *symtab_hdr;
11085 Elf_Internal_Shdr *symtab_shndx_hdr;
11086 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11087 struct elf_outext_info eoinfo;
11088 bfd_boolean merged;
11089 size_t relativecount = 0;
11090 asection *reldyn = 0;
11091 bfd_size_type amt;
11092 asection *attr_section = NULL;
11093 bfd_vma attr_size = 0;
11094 const char *std_attrs_section;
11095
11096 if (! is_elf_hash_table (info->hash))
11097 return FALSE;
11098
11099 if (bfd_link_pic (info))
11100 abfd->flags |= DYNAMIC;
11101
11102 dynamic = elf_hash_table (info)->dynamic_sections_created;
11103 dynobj = elf_hash_table (info)->dynobj;
11104
11105 emit_relocs = (bfd_link_relocatable (info)
11106 || info->emitrelocations);
11107
11108 flinfo.info = info;
11109 flinfo.output_bfd = abfd;
11110 flinfo.symstrtab = _bfd_elf_strtab_init ();
11111 if (flinfo.symstrtab == NULL)
11112 return FALSE;
11113
11114 if (! dynamic)
11115 {
11116 flinfo.hash_sec = NULL;
11117 flinfo.symver_sec = NULL;
11118 }
11119 else
11120 {
11121 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11122 /* Note that dynsym_sec can be NULL (on VMS). */
11123 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11124 /* Note that it is OK if symver_sec is NULL. */
11125 }
11126
11127 flinfo.contents = NULL;
11128 flinfo.external_relocs = NULL;
11129 flinfo.internal_relocs = NULL;
11130 flinfo.external_syms = NULL;
11131 flinfo.locsym_shndx = NULL;
11132 flinfo.internal_syms = NULL;
11133 flinfo.indices = NULL;
11134 flinfo.sections = NULL;
11135 flinfo.symshndxbuf = NULL;
11136 flinfo.filesym_count = 0;
11137
11138 /* The object attributes have been merged. Remove the input
11139 sections from the link, and set the contents of the output
11140 secton. */
11141 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11142 for (o = abfd->sections; o != NULL; o = o->next)
11143 {
11144 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11145 || strcmp (o->name, ".gnu.attributes") == 0)
11146 {
11147 for (p = o->map_head.link_order; p != NULL; p = p->next)
11148 {
11149 asection *input_section;
11150
11151 if (p->type != bfd_indirect_link_order)
11152 continue;
11153 input_section = p->u.indirect.section;
11154 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11155 elf_link_input_bfd ignores this section. */
11156 input_section->flags &= ~SEC_HAS_CONTENTS;
11157 }
11158
11159 attr_size = bfd_elf_obj_attr_size (abfd);
11160 if (attr_size)
11161 {
11162 bfd_set_section_size (abfd, o, attr_size);
11163 attr_section = o;
11164 /* Skip this section later on. */
11165 o->map_head.link_order = NULL;
11166 }
11167 else
11168 o->flags |= SEC_EXCLUDE;
11169 }
11170 }
11171
11172 /* Count up the number of relocations we will output for each output
11173 section, so that we know the sizes of the reloc sections. We
11174 also figure out some maximum sizes. */
11175 max_contents_size = 0;
11176 max_external_reloc_size = 0;
11177 max_internal_reloc_count = 0;
11178 max_sym_count = 0;
11179 max_sym_shndx_count = 0;
11180 merged = FALSE;
11181 for (o = abfd->sections; o != NULL; o = o->next)
11182 {
11183 struct bfd_elf_section_data *esdo = elf_section_data (o);
11184 o->reloc_count = 0;
11185
11186 for (p = o->map_head.link_order; p != NULL; p = p->next)
11187 {
11188 unsigned int reloc_count = 0;
11189 unsigned int additional_reloc_count = 0;
11190 struct bfd_elf_section_data *esdi = NULL;
11191
11192 if (p->type == bfd_section_reloc_link_order
11193 || p->type == bfd_symbol_reloc_link_order)
11194 reloc_count = 1;
11195 else if (p->type == bfd_indirect_link_order)
11196 {
11197 asection *sec;
11198
11199 sec = p->u.indirect.section;
11200 esdi = elf_section_data (sec);
11201
11202 /* Mark all sections which are to be included in the
11203 link. This will normally be every section. We need
11204 to do this so that we can identify any sections which
11205 the linker has decided to not include. */
11206 sec->linker_mark = TRUE;
11207
11208 if (sec->flags & SEC_MERGE)
11209 merged = TRUE;
11210
11211 if (esdo->this_hdr.sh_type == SHT_REL
11212 || esdo->this_hdr.sh_type == SHT_RELA)
11213 /* Some backends use reloc_count in relocation sections
11214 to count particular types of relocs. Of course,
11215 reloc sections themselves can't have relocations. */
11216 reloc_count = 0;
11217 else if (emit_relocs)
11218 {
11219 reloc_count = sec->reloc_count;
11220 if (bed->elf_backend_count_additional_relocs)
11221 {
11222 int c;
11223 c = (*bed->elf_backend_count_additional_relocs) (sec);
11224 additional_reloc_count += c;
11225 }
11226 }
11227 else if (bed->elf_backend_count_relocs)
11228 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11229
11230 if (sec->rawsize > max_contents_size)
11231 max_contents_size = sec->rawsize;
11232 if (sec->size > max_contents_size)
11233 max_contents_size = sec->size;
11234
11235 /* We are interested in just local symbols, not all
11236 symbols. */
11237 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11238 && (sec->owner->flags & DYNAMIC) == 0)
11239 {
11240 size_t sym_count;
11241
11242 if (elf_bad_symtab (sec->owner))
11243 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11244 / bed->s->sizeof_sym);
11245 else
11246 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11247
11248 if (sym_count > max_sym_count)
11249 max_sym_count = sym_count;
11250
11251 if (sym_count > max_sym_shndx_count
11252 && elf_symtab_shndx_list (sec->owner) != NULL)
11253 max_sym_shndx_count = sym_count;
11254
11255 if ((sec->flags & SEC_RELOC) != 0)
11256 {
11257 size_t ext_size = 0;
11258
11259 if (esdi->rel.hdr != NULL)
11260 ext_size = esdi->rel.hdr->sh_size;
11261 if (esdi->rela.hdr != NULL)
11262 ext_size += esdi->rela.hdr->sh_size;
11263
11264 if (ext_size > max_external_reloc_size)
11265 max_external_reloc_size = ext_size;
11266 if (sec->reloc_count > max_internal_reloc_count)
11267 max_internal_reloc_count = sec->reloc_count;
11268 }
11269 }
11270 }
11271
11272 if (reloc_count == 0)
11273 continue;
11274
11275 reloc_count += additional_reloc_count;
11276 o->reloc_count += reloc_count;
11277
11278 if (p->type == bfd_indirect_link_order && emit_relocs)
11279 {
11280 if (esdi->rel.hdr)
11281 {
11282 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11283 esdo->rel.count += additional_reloc_count;
11284 }
11285 if (esdi->rela.hdr)
11286 {
11287 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11288 esdo->rela.count += additional_reloc_count;
11289 }
11290 }
11291 else
11292 {
11293 if (o->use_rela_p)
11294 esdo->rela.count += reloc_count;
11295 else
11296 esdo->rel.count += reloc_count;
11297 }
11298 }
11299
11300 if (o->reloc_count > 0)
11301 o->flags |= SEC_RELOC;
11302 else
11303 {
11304 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11305 set it (this is probably a bug) and if it is set
11306 assign_section_numbers will create a reloc section. */
11307 o->flags &=~ SEC_RELOC;
11308 }
11309
11310 /* If the SEC_ALLOC flag is not set, force the section VMA to
11311 zero. This is done in elf_fake_sections as well, but forcing
11312 the VMA to 0 here will ensure that relocs against these
11313 sections are handled correctly. */
11314 if ((o->flags & SEC_ALLOC) == 0
11315 && ! o->user_set_vma)
11316 o->vma = 0;
11317 }
11318
11319 if (! bfd_link_relocatable (info) && merged)
11320 elf_link_hash_traverse (elf_hash_table (info),
11321 _bfd_elf_link_sec_merge_syms, abfd);
11322
11323 /* Figure out the file positions for everything but the symbol table
11324 and the relocs. We set symcount to force assign_section_numbers
11325 to create a symbol table. */
11326 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
11327 BFD_ASSERT (! abfd->output_has_begun);
11328 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11329 goto error_return;
11330
11331 /* Set sizes, and assign file positions for reloc sections. */
11332 for (o = abfd->sections; o != NULL; o = o->next)
11333 {
11334 struct bfd_elf_section_data *esdo = elf_section_data (o);
11335 if ((o->flags & SEC_RELOC) != 0)
11336 {
11337 if (esdo->rel.hdr
11338 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
11339 goto error_return;
11340
11341 if (esdo->rela.hdr
11342 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
11343 goto error_return;
11344 }
11345
11346 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11347 to count upwards while actually outputting the relocations. */
11348 esdo->rel.count = 0;
11349 esdo->rela.count = 0;
11350
11351 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11352 {
11353 /* Cache the section contents so that they can be compressed
11354 later. Use bfd_malloc since it will be freed by
11355 bfd_compress_section_contents. */
11356 unsigned char *contents = esdo->this_hdr.contents;
11357 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11358 abort ();
11359 contents
11360 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11361 if (contents == NULL)
11362 goto error_return;
11363 esdo->this_hdr.contents = contents;
11364 }
11365 }
11366
11367 /* We have now assigned file positions for all the sections except
11368 .symtab, .strtab, and non-loaded reloc sections. We start the
11369 .symtab section at the current file position, and write directly
11370 to it. We build the .strtab section in memory. */
11371 bfd_get_symcount (abfd) = 0;
11372 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11373 /* sh_name is set in prep_headers. */
11374 symtab_hdr->sh_type = SHT_SYMTAB;
11375 /* sh_flags, sh_addr and sh_size all start off zero. */
11376 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11377 /* sh_link is set in assign_section_numbers. */
11378 /* sh_info is set below. */
11379 /* sh_offset is set just below. */
11380 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11381
11382 if (max_sym_count < 20)
11383 max_sym_count = 20;
11384 elf_hash_table (info)->strtabsize = max_sym_count;
11385 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11386 elf_hash_table (info)->strtab
11387 = (struct elf_sym_strtab *) bfd_malloc (amt);
11388 if (elf_hash_table (info)->strtab == NULL)
11389 goto error_return;
11390 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11391 flinfo.symshndxbuf
11392 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11393 ? (Elf_External_Sym_Shndx *) -1 : NULL);
11394
11395 if (info->strip != strip_all || emit_relocs)
11396 {
11397 file_ptr off = elf_next_file_pos (abfd);
11398
11399 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11400
11401 /* Note that at this point elf_next_file_pos (abfd) is
11402 incorrect. We do not yet know the size of the .symtab section.
11403 We correct next_file_pos below, after we do know the size. */
11404
11405 /* Start writing out the symbol table. The first symbol is always a
11406 dummy symbol. */
11407 elfsym.st_value = 0;
11408 elfsym.st_size = 0;
11409 elfsym.st_info = 0;
11410 elfsym.st_other = 0;
11411 elfsym.st_shndx = SHN_UNDEF;
11412 elfsym.st_target_internal = 0;
11413 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11414 bfd_und_section_ptr, NULL) != 1)
11415 goto error_return;
11416
11417 /* Output a symbol for each section. We output these even if we are
11418 discarding local symbols, since they are used for relocs. These
11419 symbols have no names. We store the index of each one in the
11420 index field of the section, so that we can find it again when
11421 outputting relocs. */
11422
11423 elfsym.st_size = 0;
11424 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11425 elfsym.st_other = 0;
11426 elfsym.st_value = 0;
11427 elfsym.st_target_internal = 0;
11428 for (i = 1; i < elf_numsections (abfd); i++)
11429 {
11430 o = bfd_section_from_elf_index (abfd, i);
11431 if (o != NULL)
11432 {
11433 o->target_index = bfd_get_symcount (abfd);
11434 elfsym.st_shndx = i;
11435 if (!bfd_link_relocatable (info))
11436 elfsym.st_value = o->vma;
11437 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11438 NULL) != 1)
11439 goto error_return;
11440 }
11441 }
11442 }
11443
11444 /* Allocate some memory to hold information read in from the input
11445 files. */
11446 if (max_contents_size != 0)
11447 {
11448 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11449 if (flinfo.contents == NULL)
11450 goto error_return;
11451 }
11452
11453 if (max_external_reloc_size != 0)
11454 {
11455 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11456 if (flinfo.external_relocs == NULL)
11457 goto error_return;
11458 }
11459
11460 if (max_internal_reloc_count != 0)
11461 {
11462 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11463 amt *= sizeof (Elf_Internal_Rela);
11464 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11465 if (flinfo.internal_relocs == NULL)
11466 goto error_return;
11467 }
11468
11469 if (max_sym_count != 0)
11470 {
11471 amt = max_sym_count * bed->s->sizeof_sym;
11472 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11473 if (flinfo.external_syms == NULL)
11474 goto error_return;
11475
11476 amt = max_sym_count * sizeof (Elf_Internal_Sym);
11477 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11478 if (flinfo.internal_syms == NULL)
11479 goto error_return;
11480
11481 amt = max_sym_count * sizeof (long);
11482 flinfo.indices = (long int *) bfd_malloc (amt);
11483 if (flinfo.indices == NULL)
11484 goto error_return;
11485
11486 amt = max_sym_count * sizeof (asection *);
11487 flinfo.sections = (asection **) bfd_malloc (amt);
11488 if (flinfo.sections == NULL)
11489 goto error_return;
11490 }
11491
11492 if (max_sym_shndx_count != 0)
11493 {
11494 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
11495 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11496 if (flinfo.locsym_shndx == NULL)
11497 goto error_return;
11498 }
11499
11500 if (elf_hash_table (info)->tls_sec)
11501 {
11502 bfd_vma base, end = 0;
11503 asection *sec;
11504
11505 for (sec = elf_hash_table (info)->tls_sec;
11506 sec && (sec->flags & SEC_THREAD_LOCAL);
11507 sec = sec->next)
11508 {
11509 bfd_size_type size = sec->size;
11510
11511 if (size == 0
11512 && (sec->flags & SEC_HAS_CONTENTS) == 0)
11513 {
11514 struct bfd_link_order *ord = sec->map_tail.link_order;
11515
11516 if (ord != NULL)
11517 size = ord->offset + ord->size;
11518 }
11519 end = sec->vma + size;
11520 }
11521 base = elf_hash_table (info)->tls_sec->vma;
11522 /* Only align end of TLS section if static TLS doesn't have special
11523 alignment requirements. */
11524 if (bed->static_tls_alignment == 1)
11525 end = align_power (end,
11526 elf_hash_table (info)->tls_sec->alignment_power);
11527 elf_hash_table (info)->tls_size = end - base;
11528 }
11529
11530 /* Reorder SHF_LINK_ORDER sections. */
11531 for (o = abfd->sections; o != NULL; o = o->next)
11532 {
11533 if (!elf_fixup_link_order (abfd, o))
11534 return FALSE;
11535 }
11536
11537 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11538 return FALSE;
11539
11540 /* Since ELF permits relocations to be against local symbols, we
11541 must have the local symbols available when we do the relocations.
11542 Since we would rather only read the local symbols once, and we
11543 would rather not keep them in memory, we handle all the
11544 relocations for a single input file at the same time.
11545
11546 Unfortunately, there is no way to know the total number of local
11547 symbols until we have seen all of them, and the local symbol
11548 indices precede the global symbol indices. This means that when
11549 we are generating relocatable output, and we see a reloc against
11550 a global symbol, we can not know the symbol index until we have
11551 finished examining all the local symbols to see which ones we are
11552 going to output. To deal with this, we keep the relocations in
11553 memory, and don't output them until the end of the link. This is
11554 an unfortunate waste of memory, but I don't see a good way around
11555 it. Fortunately, it only happens when performing a relocatable
11556 link, which is not the common case. FIXME: If keep_memory is set
11557 we could write the relocs out and then read them again; I don't
11558 know how bad the memory loss will be. */
11559
11560 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11561 sub->output_has_begun = FALSE;
11562 for (o = abfd->sections; o != NULL; o = o->next)
11563 {
11564 for (p = o->map_head.link_order; p != NULL; p = p->next)
11565 {
11566 if (p->type == bfd_indirect_link_order
11567 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11568 == bfd_target_elf_flavour)
11569 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11570 {
11571 if (! sub->output_has_begun)
11572 {
11573 if (! elf_link_input_bfd (&flinfo, sub))
11574 goto error_return;
11575 sub->output_has_begun = TRUE;
11576 }
11577 }
11578 else if (p->type == bfd_section_reloc_link_order
11579 || p->type == bfd_symbol_reloc_link_order)
11580 {
11581 if (! elf_reloc_link_order (abfd, info, o, p))
11582 goto error_return;
11583 }
11584 else
11585 {
11586 if (! _bfd_default_link_order (abfd, info, o, p))
11587 {
11588 if (p->type == bfd_indirect_link_order
11589 && (bfd_get_flavour (sub)
11590 == bfd_target_elf_flavour)
11591 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11592 != bed->s->elfclass))
11593 {
11594 const char *iclass, *oclass;
11595
11596 switch (bed->s->elfclass)
11597 {
11598 case ELFCLASS64: oclass = "ELFCLASS64"; break;
11599 case ELFCLASS32: oclass = "ELFCLASS32"; break;
11600 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11601 default: abort ();
11602 }
11603
11604 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
11605 {
11606 case ELFCLASS64: iclass = "ELFCLASS64"; break;
11607 case ELFCLASS32: iclass = "ELFCLASS32"; break;
11608 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11609 default: abort ();
11610 }
11611
11612 bfd_set_error (bfd_error_wrong_format);
11613 (*_bfd_error_handler)
11614 (_("%B: file class %s incompatible with %s"),
11615 sub, iclass, oclass);
11616 }
11617
11618 goto error_return;
11619 }
11620 }
11621 }
11622 }
11623
11624 /* Free symbol buffer if needed. */
11625 if (!info->reduce_memory_overheads)
11626 {
11627 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11628 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11629 && elf_tdata (sub)->symbuf)
11630 {
11631 free (elf_tdata (sub)->symbuf);
11632 elf_tdata (sub)->symbuf = NULL;
11633 }
11634 }
11635
11636 /* Output any global symbols that got converted to local in a
11637 version script or due to symbol visibility. We do this in a
11638 separate step since ELF requires all local symbols to appear
11639 prior to any global symbols. FIXME: We should only do this if
11640 some global symbols were, in fact, converted to become local.
11641 FIXME: Will this work correctly with the Irix 5 linker? */
11642 eoinfo.failed = FALSE;
11643 eoinfo.flinfo = &flinfo;
11644 eoinfo.localsyms = TRUE;
11645 eoinfo.file_sym_done = FALSE;
11646 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11647 if (eoinfo.failed)
11648 return FALSE;
11649
11650 /* If backend needs to output some local symbols not present in the hash
11651 table, do it now. */
11652 if (bed->elf_backend_output_arch_local_syms
11653 && (info->strip != strip_all || emit_relocs))
11654 {
11655 typedef int (*out_sym_func)
11656 (void *, const char *, Elf_Internal_Sym *, asection *,
11657 struct elf_link_hash_entry *);
11658
11659 if (! ((*bed->elf_backend_output_arch_local_syms)
11660 (abfd, info, &flinfo,
11661 (out_sym_func) elf_link_output_symstrtab)))
11662 return FALSE;
11663 }
11664
11665 /* That wrote out all the local symbols. Finish up the symbol table
11666 with the global symbols. Even if we want to strip everything we
11667 can, we still need to deal with those global symbols that got
11668 converted to local in a version script. */
11669
11670 /* The sh_info field records the index of the first non local symbol. */
11671 symtab_hdr->sh_info = bfd_get_symcount (abfd);
11672
11673 if (dynamic
11674 && elf_hash_table (info)->dynsym != NULL
11675 && (elf_hash_table (info)->dynsym->output_section
11676 != bfd_abs_section_ptr))
11677 {
11678 Elf_Internal_Sym sym;
11679 bfd_byte *dynsym = elf_hash_table (info)->dynsym->contents;
11680 long last_local = 0;
11681
11682 /* Write out the section symbols for the output sections. */
11683 if (bfd_link_pic (info)
11684 || elf_hash_table (info)->is_relocatable_executable)
11685 {
11686 asection *s;
11687
11688 sym.st_size = 0;
11689 sym.st_name = 0;
11690 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11691 sym.st_other = 0;
11692 sym.st_target_internal = 0;
11693
11694 for (s = abfd->sections; s != NULL; s = s->next)
11695 {
11696 int indx;
11697 bfd_byte *dest;
11698 long dynindx;
11699
11700 dynindx = elf_section_data (s)->dynindx;
11701 if (dynindx <= 0)
11702 continue;
11703 indx = elf_section_data (s)->this_idx;
11704 BFD_ASSERT (indx > 0);
11705 sym.st_shndx = indx;
11706 if (! check_dynsym (abfd, &sym))
11707 return FALSE;
11708 sym.st_value = s->vma;
11709 dest = dynsym + dynindx * bed->s->sizeof_sym;
11710 if (last_local < dynindx)
11711 last_local = dynindx;
11712 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11713 }
11714 }
11715
11716 /* Write out the local dynsyms. */
11717 if (elf_hash_table (info)->dynlocal)
11718 {
11719 struct elf_link_local_dynamic_entry *e;
11720 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
11721 {
11722 asection *s;
11723 bfd_byte *dest;
11724
11725 /* Copy the internal symbol and turn off visibility.
11726 Note that we saved a word of storage and overwrote
11727 the original st_name with the dynstr_index. */
11728 sym = e->isym;
11729 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
11730
11731 s = bfd_section_from_elf_index (e->input_bfd,
11732 e->isym.st_shndx);
11733 if (s != NULL)
11734 {
11735 sym.st_shndx =
11736 elf_section_data (s->output_section)->this_idx;
11737 if (! check_dynsym (abfd, &sym))
11738 return FALSE;
11739 sym.st_value = (s->output_section->vma
11740 + s->output_offset
11741 + e->isym.st_value);
11742 }
11743
11744 if (last_local < e->dynindx)
11745 last_local = e->dynindx;
11746
11747 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
11748 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11749 }
11750 }
11751
11752 elf_section_data (elf_hash_table (info)->dynsym->output_section)->this_hdr.sh_info =
11753 last_local + 1;
11754 }
11755
11756 /* We get the global symbols from the hash table. */
11757 eoinfo.failed = FALSE;
11758 eoinfo.localsyms = FALSE;
11759 eoinfo.flinfo = &flinfo;
11760 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11761 if (eoinfo.failed)
11762 return FALSE;
11763
11764 /* If backend needs to output some symbols not present in the hash
11765 table, do it now. */
11766 if (bed->elf_backend_output_arch_syms
11767 && (info->strip != strip_all || emit_relocs))
11768 {
11769 typedef int (*out_sym_func)
11770 (void *, const char *, Elf_Internal_Sym *, asection *,
11771 struct elf_link_hash_entry *);
11772
11773 if (! ((*bed->elf_backend_output_arch_syms)
11774 (abfd, info, &flinfo,
11775 (out_sym_func) elf_link_output_symstrtab)))
11776 return FALSE;
11777 }
11778
11779 /* Finalize the .strtab section. */
11780 _bfd_elf_strtab_finalize (flinfo.symstrtab);
11781
11782 /* Swap out the .strtab section. */
11783 if (!elf_link_swap_symbols_out (&flinfo))
11784 return FALSE;
11785
11786 /* Now we know the size of the symtab section. */
11787 if (bfd_get_symcount (abfd) > 0)
11788 {
11789 /* Finish up and write out the symbol string table (.strtab)
11790 section. */
11791 Elf_Internal_Shdr *symstrtab_hdr;
11792 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
11793
11794 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
11795 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
11796 {
11797 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
11798 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
11799 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
11800 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
11801 symtab_shndx_hdr->sh_size = amt;
11802
11803 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
11804 off, TRUE);
11805
11806 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
11807 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
11808 return FALSE;
11809 }
11810
11811 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
11812 /* sh_name was set in prep_headers. */
11813 symstrtab_hdr->sh_type = SHT_STRTAB;
11814 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
11815 symstrtab_hdr->sh_addr = 0;
11816 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
11817 symstrtab_hdr->sh_entsize = 0;
11818 symstrtab_hdr->sh_link = 0;
11819 symstrtab_hdr->sh_info = 0;
11820 /* sh_offset is set just below. */
11821 symstrtab_hdr->sh_addralign = 1;
11822
11823 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
11824 off, TRUE);
11825 elf_next_file_pos (abfd) = off;
11826
11827 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
11828 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
11829 return FALSE;
11830 }
11831
11832 /* Adjust the relocs to have the correct symbol indices. */
11833 for (o = abfd->sections; o != NULL; o = o->next)
11834 {
11835 struct bfd_elf_section_data *esdo = elf_section_data (o);
11836 bfd_boolean sort;
11837 if ((o->flags & SEC_RELOC) == 0)
11838 continue;
11839
11840 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
11841 if (esdo->rel.hdr != NULL
11842 && !elf_link_adjust_relocs (abfd, &esdo->rel, sort))
11843 return FALSE;
11844 if (esdo->rela.hdr != NULL
11845 && !elf_link_adjust_relocs (abfd, &esdo->rela, sort))
11846 return FALSE;
11847
11848 /* Set the reloc_count field to 0 to prevent write_relocs from
11849 trying to swap the relocs out itself. */
11850 o->reloc_count = 0;
11851 }
11852
11853 if (dynamic && info->combreloc && dynobj != NULL)
11854 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
11855
11856 /* If we are linking against a dynamic object, or generating a
11857 shared library, finish up the dynamic linking information. */
11858 if (dynamic)
11859 {
11860 bfd_byte *dyncon, *dynconend;
11861
11862 /* Fix up .dynamic entries. */
11863 o = bfd_get_linker_section (dynobj, ".dynamic");
11864 BFD_ASSERT (o != NULL);
11865
11866 dyncon = o->contents;
11867 dynconend = o->contents + o->size;
11868 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11869 {
11870 Elf_Internal_Dyn dyn;
11871 const char *name;
11872 unsigned int type;
11873
11874 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11875
11876 switch (dyn.d_tag)
11877 {
11878 default:
11879 continue;
11880 case DT_NULL:
11881 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
11882 {
11883 switch (elf_section_data (reldyn)->this_hdr.sh_type)
11884 {
11885 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
11886 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
11887 default: continue;
11888 }
11889 dyn.d_un.d_val = relativecount;
11890 relativecount = 0;
11891 break;
11892 }
11893 continue;
11894
11895 case DT_INIT:
11896 name = info->init_function;
11897 goto get_sym;
11898 case DT_FINI:
11899 name = info->fini_function;
11900 get_sym:
11901 {
11902 struct elf_link_hash_entry *h;
11903
11904 h = elf_link_hash_lookup (elf_hash_table (info), name,
11905 FALSE, FALSE, TRUE);
11906 if (h != NULL
11907 && (h->root.type == bfd_link_hash_defined
11908 || h->root.type == bfd_link_hash_defweak))
11909 {
11910 dyn.d_un.d_ptr = h->root.u.def.value;
11911 o = h->root.u.def.section;
11912 if (o->output_section != NULL)
11913 dyn.d_un.d_ptr += (o->output_section->vma
11914 + o->output_offset);
11915 else
11916 {
11917 /* The symbol is imported from another shared
11918 library and does not apply to this one. */
11919 dyn.d_un.d_ptr = 0;
11920 }
11921 break;
11922 }
11923 }
11924 continue;
11925
11926 case DT_PREINIT_ARRAYSZ:
11927 name = ".preinit_array";
11928 goto get_out_size;
11929 case DT_INIT_ARRAYSZ:
11930 name = ".init_array";
11931 goto get_out_size;
11932 case DT_FINI_ARRAYSZ:
11933 name = ".fini_array";
11934 get_out_size:
11935 o = bfd_get_section_by_name (abfd, name);
11936 if (o == NULL)
11937 {
11938 (*_bfd_error_handler)
11939 (_("could not find section %s"), name);
11940 goto error_return;
11941 }
11942 if (o->size == 0)
11943 (*_bfd_error_handler)
11944 (_("warning: %s section has zero size"), name);
11945 dyn.d_un.d_val = o->size;
11946 break;
11947
11948 case DT_PREINIT_ARRAY:
11949 name = ".preinit_array";
11950 goto get_out_vma;
11951 case DT_INIT_ARRAY:
11952 name = ".init_array";
11953 goto get_out_vma;
11954 case DT_FINI_ARRAY:
11955 name = ".fini_array";
11956 get_out_vma:
11957 o = bfd_get_section_by_name (abfd, name);
11958 goto do_vma;
11959
11960 case DT_HASH:
11961 name = ".hash";
11962 goto get_vma;
11963 case DT_GNU_HASH:
11964 name = ".gnu.hash";
11965 goto get_vma;
11966 case DT_STRTAB:
11967 name = ".dynstr";
11968 goto get_vma;
11969 case DT_SYMTAB:
11970 name = ".dynsym";
11971 goto get_vma;
11972 case DT_VERDEF:
11973 name = ".gnu.version_d";
11974 goto get_vma;
11975 case DT_VERNEED:
11976 name = ".gnu.version_r";
11977 goto get_vma;
11978 case DT_VERSYM:
11979 name = ".gnu.version";
11980 get_vma:
11981 o = bfd_get_linker_section (dynobj, name);
11982 do_vma:
11983 if (o == NULL)
11984 {
11985 (*_bfd_error_handler)
11986 (_("could not find section %s"), name);
11987 goto error_return;
11988 }
11989 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
11990 {
11991 (*_bfd_error_handler)
11992 (_("warning: section '%s' is being made into a note"), name);
11993 bfd_set_error (bfd_error_nonrepresentable_section);
11994 goto error_return;
11995 }
11996 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
11997 break;
11998
11999 case DT_REL:
12000 case DT_RELA:
12001 case DT_RELSZ:
12002 case DT_RELASZ:
12003 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12004 type = SHT_REL;
12005 else
12006 type = SHT_RELA;
12007 dyn.d_un.d_val = 0;
12008 dyn.d_un.d_ptr = 0;
12009 for (i = 1; i < elf_numsections (abfd); i++)
12010 {
12011 Elf_Internal_Shdr *hdr;
12012
12013 hdr = elf_elfsections (abfd)[i];
12014 if (hdr->sh_type == type
12015 && (hdr->sh_flags & SHF_ALLOC) != 0)
12016 {
12017 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12018 dyn.d_un.d_val += hdr->sh_size;
12019 else
12020 {
12021 if (dyn.d_un.d_ptr == 0
12022 || hdr->sh_addr < dyn.d_un.d_ptr)
12023 dyn.d_un.d_ptr = hdr->sh_addr;
12024 }
12025 }
12026 }
12027 break;
12028 }
12029 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12030 }
12031 }
12032
12033 /* If we have created any dynamic sections, then output them. */
12034 if (dynobj != NULL)
12035 {
12036 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12037 goto error_return;
12038
12039 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12040 if (((info->warn_shared_textrel && bfd_link_pic (info))
12041 || info->error_textrel)
12042 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
12043 {
12044 bfd_byte *dyncon, *dynconend;
12045
12046 dyncon = o->contents;
12047 dynconend = o->contents + o->size;
12048 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12049 {
12050 Elf_Internal_Dyn dyn;
12051
12052 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12053
12054 if (dyn.d_tag == DT_TEXTREL)
12055 {
12056 if (info->error_textrel)
12057 info->callbacks->einfo
12058 (_("%P%X: read-only segment has dynamic relocations.\n"));
12059 else
12060 info->callbacks->einfo
12061 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
12062 break;
12063 }
12064 }
12065 }
12066
12067 for (o = dynobj->sections; o != NULL; o = o->next)
12068 {
12069 if ((o->flags & SEC_HAS_CONTENTS) == 0
12070 || o->size == 0
12071 || o->output_section == bfd_abs_section_ptr)
12072 continue;
12073 if ((o->flags & SEC_LINKER_CREATED) == 0)
12074 {
12075 /* At this point, we are only interested in sections
12076 created by _bfd_elf_link_create_dynamic_sections. */
12077 continue;
12078 }
12079 if (elf_hash_table (info)->stab_info.stabstr == o)
12080 continue;
12081 if (elf_hash_table (info)->eh_info.hdr_sec == o)
12082 continue;
12083 if (strcmp (o->name, ".dynstr") != 0)
12084 {
12085 if (! bfd_set_section_contents (abfd, o->output_section,
12086 o->contents,
12087 (file_ptr) o->output_offset
12088 * bfd_octets_per_byte (abfd),
12089 o->size))
12090 goto error_return;
12091 }
12092 else
12093 {
12094 /* The contents of the .dynstr section are actually in a
12095 stringtab. */
12096 file_ptr off;
12097
12098 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12099 if (bfd_seek (abfd, off, SEEK_SET) != 0
12100 || ! _bfd_elf_strtab_emit (abfd,
12101 elf_hash_table (info)->dynstr))
12102 goto error_return;
12103 }
12104 }
12105 }
12106
12107 if (bfd_link_relocatable (info))
12108 {
12109 bfd_boolean failed = FALSE;
12110
12111 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12112 if (failed)
12113 goto error_return;
12114 }
12115
12116 /* If we have optimized stabs strings, output them. */
12117 if (elf_hash_table (info)->stab_info.stabstr != NULL)
12118 {
12119 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
12120 goto error_return;
12121 }
12122
12123 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12124 goto error_return;
12125
12126 elf_final_link_free (abfd, &flinfo);
12127
12128 elf_linker (abfd) = TRUE;
12129
12130 if (attr_section)
12131 {
12132 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
12133 if (contents == NULL)
12134 return FALSE; /* Bail out and fail. */
12135 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12136 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12137 free (contents);
12138 }
12139
12140 return TRUE;
12141
12142 error_return:
12143 elf_final_link_free (abfd, &flinfo);
12144 return FALSE;
12145 }
12146 \f
12147 /* Initialize COOKIE for input bfd ABFD. */
12148
12149 static bfd_boolean
12150 init_reloc_cookie (struct elf_reloc_cookie *cookie,
12151 struct bfd_link_info *info, bfd *abfd)
12152 {
12153 Elf_Internal_Shdr *symtab_hdr;
12154 const struct elf_backend_data *bed;
12155
12156 bed = get_elf_backend_data (abfd);
12157 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12158
12159 cookie->abfd = abfd;
12160 cookie->sym_hashes = elf_sym_hashes (abfd);
12161 cookie->bad_symtab = elf_bad_symtab (abfd);
12162 if (cookie->bad_symtab)
12163 {
12164 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12165 cookie->extsymoff = 0;
12166 }
12167 else
12168 {
12169 cookie->locsymcount = symtab_hdr->sh_info;
12170 cookie->extsymoff = symtab_hdr->sh_info;
12171 }
12172
12173 if (bed->s->arch_size == 32)
12174 cookie->r_sym_shift = 8;
12175 else
12176 cookie->r_sym_shift = 32;
12177
12178 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12179 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12180 {
12181 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12182 cookie->locsymcount, 0,
12183 NULL, NULL, NULL);
12184 if (cookie->locsyms == NULL)
12185 {
12186 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12187 return FALSE;
12188 }
12189 if (info->keep_memory)
12190 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12191 }
12192 return TRUE;
12193 }
12194
12195 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12196
12197 static void
12198 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12199 {
12200 Elf_Internal_Shdr *symtab_hdr;
12201
12202 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12203 if (cookie->locsyms != NULL
12204 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12205 free (cookie->locsyms);
12206 }
12207
12208 /* Initialize the relocation information in COOKIE for input section SEC
12209 of input bfd ABFD. */
12210
12211 static bfd_boolean
12212 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12213 struct bfd_link_info *info, bfd *abfd,
12214 asection *sec)
12215 {
12216 const struct elf_backend_data *bed;
12217
12218 if (sec->reloc_count == 0)
12219 {
12220 cookie->rels = NULL;
12221 cookie->relend = NULL;
12222 }
12223 else
12224 {
12225 bed = get_elf_backend_data (abfd);
12226
12227 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12228 info->keep_memory);
12229 if (cookie->rels == NULL)
12230 return FALSE;
12231 cookie->rel = cookie->rels;
12232 cookie->relend = (cookie->rels
12233 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
12234 }
12235 cookie->rel = cookie->rels;
12236 return TRUE;
12237 }
12238
12239 /* Free the memory allocated by init_reloc_cookie_rels,
12240 if appropriate. */
12241
12242 static void
12243 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12244 asection *sec)
12245 {
12246 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12247 free (cookie->rels);
12248 }
12249
12250 /* Initialize the whole of COOKIE for input section SEC. */
12251
12252 static bfd_boolean
12253 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12254 struct bfd_link_info *info,
12255 asection *sec)
12256 {
12257 if (!init_reloc_cookie (cookie, info, sec->owner))
12258 goto error1;
12259 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12260 goto error2;
12261 return TRUE;
12262
12263 error2:
12264 fini_reloc_cookie (cookie, sec->owner);
12265 error1:
12266 return FALSE;
12267 }
12268
12269 /* Free the memory allocated by init_reloc_cookie_for_section,
12270 if appropriate. */
12271
12272 static void
12273 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12274 asection *sec)
12275 {
12276 fini_reloc_cookie_rels (cookie, sec);
12277 fini_reloc_cookie (cookie, sec->owner);
12278 }
12279 \f
12280 /* Garbage collect unused sections. */
12281
12282 /* Default gc_mark_hook. */
12283
12284 asection *
12285 _bfd_elf_gc_mark_hook (asection *sec,
12286 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12287 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12288 struct elf_link_hash_entry *h,
12289 Elf_Internal_Sym *sym)
12290 {
12291 if (h != NULL)
12292 {
12293 switch (h->root.type)
12294 {
12295 case bfd_link_hash_defined:
12296 case bfd_link_hash_defweak:
12297 return h->root.u.def.section;
12298
12299 case bfd_link_hash_common:
12300 return h->root.u.c.p->section;
12301
12302 default:
12303 break;
12304 }
12305 }
12306 else
12307 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12308
12309 return NULL;
12310 }
12311
12312 /* For undefined __start_<name> and __stop_<name> symbols, return the
12313 first input section matching <name>. Return NULL otherwise. */
12314
12315 asection *
12316 _bfd_elf_is_start_stop (const struct bfd_link_info *info,
12317 struct elf_link_hash_entry *h)
12318 {
12319 asection *s;
12320 const char *sec_name;
12321
12322 if (h->root.type != bfd_link_hash_undefined
12323 && h->root.type != bfd_link_hash_undefweak)
12324 return NULL;
12325
12326 s = h->root.u.undef.section;
12327 if (s != NULL)
12328 {
12329 if (s == (asection *) 0 - 1)
12330 return NULL;
12331 return s;
12332 }
12333
12334 sec_name = NULL;
12335 if (strncmp (h->root.root.string, "__start_", 8) == 0)
12336 sec_name = h->root.root.string + 8;
12337 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
12338 sec_name = h->root.root.string + 7;
12339
12340 if (sec_name != NULL && *sec_name != '\0')
12341 {
12342 bfd *i;
12343
12344 for (i = info->input_bfds; i != NULL; i = i->link.next)
12345 {
12346 s = bfd_get_section_by_name (i, sec_name);
12347 if (s != NULL)
12348 {
12349 h->root.u.undef.section = s;
12350 break;
12351 }
12352 }
12353 }
12354
12355 if (s == NULL)
12356 h->root.u.undef.section = (asection *) 0 - 1;
12357
12358 return s;
12359 }
12360
12361 /* COOKIE->rel describes a relocation against section SEC, which is
12362 a section we've decided to keep. Return the section that contains
12363 the relocation symbol, or NULL if no section contains it. */
12364
12365 asection *
12366 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12367 elf_gc_mark_hook_fn gc_mark_hook,
12368 struct elf_reloc_cookie *cookie,
12369 bfd_boolean *start_stop)
12370 {
12371 unsigned long r_symndx;
12372 struct elf_link_hash_entry *h;
12373
12374 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
12375 if (r_symndx == STN_UNDEF)
12376 return NULL;
12377
12378 if (r_symndx >= cookie->locsymcount
12379 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12380 {
12381 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
12382 if (h == NULL)
12383 {
12384 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12385 sec->owner);
12386 return NULL;
12387 }
12388 while (h->root.type == bfd_link_hash_indirect
12389 || h->root.type == bfd_link_hash_warning)
12390 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12391 h->mark = 1;
12392 /* If this symbol is weak and there is a non-weak definition, we
12393 keep the non-weak definition because many backends put
12394 dynamic reloc info on the non-weak definition for code
12395 handling copy relocs. */
12396 if (h->u.weakdef != NULL)
12397 h->u.weakdef->mark = 1;
12398
12399 if (start_stop != NULL)
12400 {
12401 /* To work around a glibc bug, mark all XXX input sections
12402 when there is an as yet undefined reference to __start_XXX
12403 or __stop_XXX symbols. The linker will later define such
12404 symbols for orphan input sections that have a name
12405 representable as a C identifier. */
12406 asection *s = _bfd_elf_is_start_stop (info, h);
12407
12408 if (s != NULL)
12409 {
12410 *start_stop = !s->gc_mark;
12411 return s;
12412 }
12413 }
12414
12415 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12416 }
12417
12418 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12419 &cookie->locsyms[r_symndx]);
12420 }
12421
12422 /* COOKIE->rel describes a relocation against section SEC, which is
12423 a section we've decided to keep. Mark the section that contains
12424 the relocation symbol. */
12425
12426 bfd_boolean
12427 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12428 asection *sec,
12429 elf_gc_mark_hook_fn gc_mark_hook,
12430 struct elf_reloc_cookie *cookie)
12431 {
12432 asection *rsec;
12433 bfd_boolean start_stop = FALSE;
12434
12435 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12436 while (rsec != NULL)
12437 {
12438 if (!rsec->gc_mark)
12439 {
12440 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12441 || (rsec->owner->flags & DYNAMIC) != 0)
12442 rsec->gc_mark = 1;
12443 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12444 return FALSE;
12445 }
12446 if (!start_stop)
12447 break;
12448 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
12449 }
12450 return TRUE;
12451 }
12452
12453 /* The mark phase of garbage collection. For a given section, mark
12454 it and any sections in this section's group, and all the sections
12455 which define symbols to which it refers. */
12456
12457 bfd_boolean
12458 _bfd_elf_gc_mark (struct bfd_link_info *info,
12459 asection *sec,
12460 elf_gc_mark_hook_fn gc_mark_hook)
12461 {
12462 bfd_boolean ret;
12463 asection *group_sec, *eh_frame;
12464
12465 sec->gc_mark = 1;
12466
12467 /* Mark all the sections in the group. */
12468 group_sec = elf_section_data (sec)->next_in_group;
12469 if (group_sec && !group_sec->gc_mark)
12470 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
12471 return FALSE;
12472
12473 /* Look through the section relocs. */
12474 ret = TRUE;
12475 eh_frame = elf_eh_frame_section (sec->owner);
12476 if ((sec->flags & SEC_RELOC) != 0
12477 && sec->reloc_count > 0
12478 && sec != eh_frame)
12479 {
12480 struct elf_reloc_cookie cookie;
12481
12482 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12483 ret = FALSE;
12484 else
12485 {
12486 for (; cookie.rel < cookie.relend; cookie.rel++)
12487 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
12488 {
12489 ret = FALSE;
12490 break;
12491 }
12492 fini_reloc_cookie_for_section (&cookie, sec);
12493 }
12494 }
12495
12496 if (ret && eh_frame && elf_fde_list (sec))
12497 {
12498 struct elf_reloc_cookie cookie;
12499
12500 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12501 ret = FALSE;
12502 else
12503 {
12504 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12505 gc_mark_hook, &cookie))
12506 ret = FALSE;
12507 fini_reloc_cookie_for_section (&cookie, eh_frame);
12508 }
12509 }
12510
12511 eh_frame = elf_section_eh_frame_entry (sec);
12512 if (ret && eh_frame && !eh_frame->gc_mark)
12513 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12514 ret = FALSE;
12515
12516 return ret;
12517 }
12518
12519 /* Scan and mark sections in a special or debug section group. */
12520
12521 static void
12522 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12523 {
12524 /* Point to first section of section group. */
12525 asection *ssec;
12526 /* Used to iterate the section group. */
12527 asection *msec;
12528
12529 bfd_boolean is_special_grp = TRUE;
12530 bfd_boolean is_debug_grp = TRUE;
12531
12532 /* First scan to see if group contains any section other than debug
12533 and special section. */
12534 ssec = msec = elf_next_in_group (grp);
12535 do
12536 {
12537 if ((msec->flags & SEC_DEBUGGING) == 0)
12538 is_debug_grp = FALSE;
12539
12540 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12541 is_special_grp = FALSE;
12542
12543 msec = elf_next_in_group (msec);
12544 }
12545 while (msec != ssec);
12546
12547 /* If this is a pure debug section group or pure special section group,
12548 keep all sections in this group. */
12549 if (is_debug_grp || is_special_grp)
12550 {
12551 do
12552 {
12553 msec->gc_mark = 1;
12554 msec = elf_next_in_group (msec);
12555 }
12556 while (msec != ssec);
12557 }
12558 }
12559
12560 /* Keep debug and special sections. */
12561
12562 bfd_boolean
12563 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12564 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12565 {
12566 bfd *ibfd;
12567
12568 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12569 {
12570 asection *isec;
12571 bfd_boolean some_kept;
12572 bfd_boolean debug_frag_seen;
12573
12574 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12575 continue;
12576
12577 /* Ensure all linker created sections are kept,
12578 see if any other section is already marked,
12579 and note if we have any fragmented debug sections. */
12580 debug_frag_seen = some_kept = FALSE;
12581 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12582 {
12583 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12584 isec->gc_mark = 1;
12585 else if (isec->gc_mark)
12586 some_kept = TRUE;
12587
12588 if (debug_frag_seen == FALSE
12589 && (isec->flags & SEC_DEBUGGING)
12590 && CONST_STRNEQ (isec->name, ".debug_line."))
12591 debug_frag_seen = TRUE;
12592 }
12593
12594 /* If no section in this file will be kept, then we can
12595 toss out the debug and special sections. */
12596 if (!some_kept)
12597 continue;
12598
12599 /* Keep debug and special sections like .comment when they are
12600 not part of a group. Also keep section groups that contain
12601 just debug sections or special sections. */
12602 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12603 {
12604 if ((isec->flags & SEC_GROUP) != 0)
12605 _bfd_elf_gc_mark_debug_special_section_group (isec);
12606 else if (((isec->flags & SEC_DEBUGGING) != 0
12607 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12608 && elf_next_in_group (isec) == NULL)
12609 isec->gc_mark = 1;
12610 }
12611
12612 if (! debug_frag_seen)
12613 continue;
12614
12615 /* Look for CODE sections which are going to be discarded,
12616 and find and discard any fragmented debug sections which
12617 are associated with that code section. */
12618 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12619 if ((isec->flags & SEC_CODE) != 0
12620 && isec->gc_mark == 0)
12621 {
12622 unsigned int ilen;
12623 asection *dsec;
12624
12625 ilen = strlen (isec->name);
12626
12627 /* Association is determined by the name of the debug section
12628 containing the name of the code section as a suffix. For
12629 example .debug_line.text.foo is a debug section associated
12630 with .text.foo. */
12631 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12632 {
12633 unsigned int dlen;
12634
12635 if (dsec->gc_mark == 0
12636 || (dsec->flags & SEC_DEBUGGING) == 0)
12637 continue;
12638
12639 dlen = strlen (dsec->name);
12640
12641 if (dlen > ilen
12642 && strncmp (dsec->name + (dlen - ilen),
12643 isec->name, ilen) == 0)
12644 {
12645 dsec->gc_mark = 0;
12646 }
12647 }
12648 }
12649 }
12650 return TRUE;
12651 }
12652
12653 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12654
12655 struct elf_gc_sweep_symbol_info
12656 {
12657 struct bfd_link_info *info;
12658 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
12659 bfd_boolean);
12660 };
12661
12662 static bfd_boolean
12663 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
12664 {
12665 if (!h->mark
12666 && (((h->root.type == bfd_link_hash_defined
12667 || h->root.type == bfd_link_hash_defweak)
12668 && !((h->def_regular || ELF_COMMON_DEF_P (h))
12669 && h->root.u.def.section->gc_mark))
12670 || h->root.type == bfd_link_hash_undefined
12671 || h->root.type == bfd_link_hash_undefweak))
12672 {
12673 struct elf_gc_sweep_symbol_info *inf;
12674
12675 inf = (struct elf_gc_sweep_symbol_info *) data;
12676 (*inf->hide_symbol) (inf->info, h, TRUE);
12677 h->def_regular = 0;
12678 h->ref_regular = 0;
12679 h->ref_regular_nonweak = 0;
12680 }
12681
12682 return TRUE;
12683 }
12684
12685 /* The sweep phase of garbage collection. Remove all garbage sections. */
12686
12687 typedef bfd_boolean (*gc_sweep_hook_fn)
12688 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
12689
12690 static bfd_boolean
12691 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
12692 {
12693 bfd *sub;
12694 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12695 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
12696 unsigned long section_sym_count;
12697 struct elf_gc_sweep_symbol_info sweep_info;
12698
12699 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12700 {
12701 asection *o;
12702
12703 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12704 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
12705 continue;
12706
12707 for (o = sub->sections; o != NULL; o = o->next)
12708 {
12709 /* When any section in a section group is kept, we keep all
12710 sections in the section group. If the first member of
12711 the section group is excluded, we will also exclude the
12712 group section. */
12713 if (o->flags & SEC_GROUP)
12714 {
12715 asection *first = elf_next_in_group (o);
12716 o->gc_mark = first->gc_mark;
12717 }
12718
12719 if (o->gc_mark)
12720 continue;
12721
12722 /* Skip sweeping sections already excluded. */
12723 if (o->flags & SEC_EXCLUDE)
12724 continue;
12725
12726 /* Since this is early in the link process, it is simple
12727 to remove a section from the output. */
12728 o->flags |= SEC_EXCLUDE;
12729
12730 if (info->print_gc_sections && o->size != 0)
12731 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
12732
12733 /* But we also have to update some of the relocation
12734 info we collected before. */
12735 if (gc_sweep_hook
12736 && (o->flags & SEC_RELOC) != 0
12737 && o->reloc_count != 0
12738 && !((info->strip == strip_all || info->strip == strip_debugger)
12739 && (o->flags & SEC_DEBUGGING) != 0)
12740 && !bfd_is_abs_section (o->output_section))
12741 {
12742 Elf_Internal_Rela *internal_relocs;
12743 bfd_boolean r;
12744
12745 internal_relocs
12746 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
12747 info->keep_memory);
12748 if (internal_relocs == NULL)
12749 return FALSE;
12750
12751 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
12752
12753 if (elf_section_data (o)->relocs != internal_relocs)
12754 free (internal_relocs);
12755
12756 if (!r)
12757 return FALSE;
12758 }
12759 }
12760 }
12761
12762 /* Remove the symbols that were in the swept sections from the dynamic
12763 symbol table. GCFIXME: Anyone know how to get them out of the
12764 static symbol table as well? */
12765 sweep_info.info = info;
12766 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
12767 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
12768 &sweep_info);
12769
12770 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
12771 return TRUE;
12772 }
12773
12774 /* Propagate collected vtable information. This is called through
12775 elf_link_hash_traverse. */
12776
12777 static bfd_boolean
12778 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
12779 {
12780 /* Those that are not vtables. */
12781 if (h->vtable == NULL || h->vtable->parent == NULL)
12782 return TRUE;
12783
12784 /* Those vtables that do not have parents, we cannot merge. */
12785 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
12786 return TRUE;
12787
12788 /* If we've already been done, exit. */
12789 if (h->vtable->used && h->vtable->used[-1])
12790 return TRUE;
12791
12792 /* Make sure the parent's table is up to date. */
12793 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
12794
12795 if (h->vtable->used == NULL)
12796 {
12797 /* None of this table's entries were referenced. Re-use the
12798 parent's table. */
12799 h->vtable->used = h->vtable->parent->vtable->used;
12800 h->vtable->size = h->vtable->parent->vtable->size;
12801 }
12802 else
12803 {
12804 size_t n;
12805 bfd_boolean *cu, *pu;
12806
12807 /* Or the parent's entries into ours. */
12808 cu = h->vtable->used;
12809 cu[-1] = TRUE;
12810 pu = h->vtable->parent->vtable->used;
12811 if (pu != NULL)
12812 {
12813 const struct elf_backend_data *bed;
12814 unsigned int log_file_align;
12815
12816 bed = get_elf_backend_data (h->root.u.def.section->owner);
12817 log_file_align = bed->s->log_file_align;
12818 n = h->vtable->parent->vtable->size >> log_file_align;
12819 while (n--)
12820 {
12821 if (*pu)
12822 *cu = TRUE;
12823 pu++;
12824 cu++;
12825 }
12826 }
12827 }
12828
12829 return TRUE;
12830 }
12831
12832 static bfd_boolean
12833 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
12834 {
12835 asection *sec;
12836 bfd_vma hstart, hend;
12837 Elf_Internal_Rela *relstart, *relend, *rel;
12838 const struct elf_backend_data *bed;
12839 unsigned int log_file_align;
12840
12841 /* Take care of both those symbols that do not describe vtables as
12842 well as those that are not loaded. */
12843 if (h->vtable == NULL || h->vtable->parent == NULL)
12844 return TRUE;
12845
12846 BFD_ASSERT (h->root.type == bfd_link_hash_defined
12847 || h->root.type == bfd_link_hash_defweak);
12848
12849 sec = h->root.u.def.section;
12850 hstart = h->root.u.def.value;
12851 hend = hstart + h->size;
12852
12853 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
12854 if (!relstart)
12855 return *(bfd_boolean *) okp = FALSE;
12856 bed = get_elf_backend_data (sec->owner);
12857 log_file_align = bed->s->log_file_align;
12858
12859 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
12860
12861 for (rel = relstart; rel < relend; ++rel)
12862 if (rel->r_offset >= hstart && rel->r_offset < hend)
12863 {
12864 /* If the entry is in use, do nothing. */
12865 if (h->vtable->used
12866 && (rel->r_offset - hstart) < h->vtable->size)
12867 {
12868 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
12869 if (h->vtable->used[entry])
12870 continue;
12871 }
12872 /* Otherwise, kill it. */
12873 rel->r_offset = rel->r_info = rel->r_addend = 0;
12874 }
12875
12876 return TRUE;
12877 }
12878
12879 /* Mark sections containing dynamically referenced symbols. When
12880 building shared libraries, we must assume that any visible symbol is
12881 referenced. */
12882
12883 bfd_boolean
12884 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
12885 {
12886 struct bfd_link_info *info = (struct bfd_link_info *) inf;
12887 struct bfd_elf_dynamic_list *d = info->dynamic_list;
12888
12889 if ((h->root.type == bfd_link_hash_defined
12890 || h->root.type == bfd_link_hash_defweak)
12891 && (h->ref_dynamic
12892 || ((h->def_regular || ELF_COMMON_DEF_P (h))
12893 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
12894 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
12895 && (!bfd_link_executable (info)
12896 || info->export_dynamic
12897 || (h->dynamic
12898 && d != NULL
12899 && (*d->match) (&d->head, NULL, h->root.root.string)))
12900 && (h->versioned >= versioned
12901 || !bfd_hide_sym_by_version (info->version_info,
12902 h->root.root.string)))))
12903 h->root.u.def.section->flags |= SEC_KEEP;
12904
12905 return TRUE;
12906 }
12907
12908 /* Keep all sections containing symbols undefined on the command-line,
12909 and the section containing the entry symbol. */
12910
12911 void
12912 _bfd_elf_gc_keep (struct bfd_link_info *info)
12913 {
12914 struct bfd_sym_chain *sym;
12915
12916 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
12917 {
12918 struct elf_link_hash_entry *h;
12919
12920 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
12921 FALSE, FALSE, FALSE);
12922
12923 if (h != NULL
12924 && (h->root.type == bfd_link_hash_defined
12925 || h->root.type == bfd_link_hash_defweak)
12926 && !bfd_is_abs_section (h->root.u.def.section))
12927 h->root.u.def.section->flags |= SEC_KEEP;
12928 }
12929 }
12930
12931 bfd_boolean
12932 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
12933 struct bfd_link_info *info)
12934 {
12935 bfd *ibfd = info->input_bfds;
12936
12937 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12938 {
12939 asection *sec;
12940 struct elf_reloc_cookie cookie;
12941
12942 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12943 continue;
12944
12945 if (!init_reloc_cookie (&cookie, info, ibfd))
12946 return FALSE;
12947
12948 for (sec = ibfd->sections; sec; sec = sec->next)
12949 {
12950 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
12951 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
12952 {
12953 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
12954 fini_reloc_cookie_rels (&cookie, sec);
12955 }
12956 }
12957 }
12958 return TRUE;
12959 }
12960
12961 /* Do mark and sweep of unused sections. */
12962
12963 bfd_boolean
12964 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
12965 {
12966 bfd_boolean ok = TRUE;
12967 bfd *sub;
12968 elf_gc_mark_hook_fn gc_mark_hook;
12969 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12970 struct elf_link_hash_table *htab;
12971
12972 if (!bed->can_gc_sections
12973 || !is_elf_hash_table (info->hash))
12974 {
12975 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
12976 return TRUE;
12977 }
12978
12979 bed->gc_keep (info);
12980 htab = elf_hash_table (info);
12981
12982 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
12983 at the .eh_frame section if we can mark the FDEs individually. */
12984 for (sub = info->input_bfds;
12985 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
12986 sub = sub->link.next)
12987 {
12988 asection *sec;
12989 struct elf_reloc_cookie cookie;
12990
12991 sec = bfd_get_section_by_name (sub, ".eh_frame");
12992 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
12993 {
12994 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
12995 if (elf_section_data (sec)->sec_info
12996 && (sec->flags & SEC_LINKER_CREATED) == 0)
12997 elf_eh_frame_section (sub) = sec;
12998 fini_reloc_cookie_for_section (&cookie, sec);
12999 sec = bfd_get_next_section_by_name (NULL, sec);
13000 }
13001 }
13002
13003 /* Apply transitive closure to the vtable entry usage info. */
13004 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13005 if (!ok)
13006 return FALSE;
13007
13008 /* Kill the vtable relocations that were not used. */
13009 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13010 if (!ok)
13011 return FALSE;
13012
13013 /* Mark dynamically referenced symbols. */
13014 if (htab->dynamic_sections_created)
13015 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13016
13017 /* Grovel through relocs to find out who stays ... */
13018 gc_mark_hook = bed->gc_mark_hook;
13019 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13020 {
13021 asection *o;
13022
13023 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13024 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13025 continue;
13026
13027 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13028 Also treat note sections as a root, if the section is not part
13029 of a group. */
13030 for (o = sub->sections; o != NULL; o = o->next)
13031 if (!o->gc_mark
13032 && (o->flags & SEC_EXCLUDE) == 0
13033 && ((o->flags & SEC_KEEP) != 0
13034 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13035 && elf_next_in_group (o) == NULL )))
13036 {
13037 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13038 return FALSE;
13039 }
13040 }
13041
13042 /* Allow the backend to mark additional target specific sections. */
13043 bed->gc_mark_extra_sections (info, gc_mark_hook);
13044
13045 /* ... and mark SEC_EXCLUDE for those that go. */
13046 return elf_gc_sweep (abfd, info);
13047 }
13048 \f
13049 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13050
13051 bfd_boolean
13052 bfd_elf_gc_record_vtinherit (bfd *abfd,
13053 asection *sec,
13054 struct elf_link_hash_entry *h,
13055 bfd_vma offset)
13056 {
13057 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13058 struct elf_link_hash_entry **search, *child;
13059 size_t extsymcount;
13060 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13061
13062 /* The sh_info field of the symtab header tells us where the
13063 external symbols start. We don't care about the local symbols at
13064 this point. */
13065 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13066 if (!elf_bad_symtab (abfd))
13067 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13068
13069 sym_hashes = elf_sym_hashes (abfd);
13070 sym_hashes_end = sym_hashes + extsymcount;
13071
13072 /* Hunt down the child symbol, which is in this section at the same
13073 offset as the relocation. */
13074 for (search = sym_hashes; search != sym_hashes_end; ++search)
13075 {
13076 if ((child = *search) != NULL
13077 && (child->root.type == bfd_link_hash_defined
13078 || child->root.type == bfd_link_hash_defweak)
13079 && child->root.u.def.section == sec
13080 && child->root.u.def.value == offset)
13081 goto win;
13082 }
13083
13084 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
13085 abfd, sec, (unsigned long) offset);
13086 bfd_set_error (bfd_error_invalid_operation);
13087 return FALSE;
13088
13089 win:
13090 if (!child->vtable)
13091 {
13092 child->vtable = ((struct elf_link_virtual_table_entry *)
13093 bfd_zalloc (abfd, sizeof (*child->vtable)));
13094 if (!child->vtable)
13095 return FALSE;
13096 }
13097 if (!h)
13098 {
13099 /* This *should* only be the absolute section. It could potentially
13100 be that someone has defined a non-global vtable though, which
13101 would be bad. It isn't worth paging in the local symbols to be
13102 sure though; that case should simply be handled by the assembler. */
13103
13104 child->vtable->parent = (struct elf_link_hash_entry *) -1;
13105 }
13106 else
13107 child->vtable->parent = h;
13108
13109 return TRUE;
13110 }
13111
13112 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13113
13114 bfd_boolean
13115 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13116 asection *sec ATTRIBUTE_UNUSED,
13117 struct elf_link_hash_entry *h,
13118 bfd_vma addend)
13119 {
13120 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13121 unsigned int log_file_align = bed->s->log_file_align;
13122
13123 if (!h->vtable)
13124 {
13125 h->vtable = ((struct elf_link_virtual_table_entry *)
13126 bfd_zalloc (abfd, sizeof (*h->vtable)));
13127 if (!h->vtable)
13128 return FALSE;
13129 }
13130
13131 if (addend >= h->vtable->size)
13132 {
13133 size_t size, bytes, file_align;
13134 bfd_boolean *ptr = h->vtable->used;
13135
13136 /* While the symbol is undefined, we have to be prepared to handle
13137 a zero size. */
13138 file_align = 1 << log_file_align;
13139 if (h->root.type == bfd_link_hash_undefined)
13140 size = addend + file_align;
13141 else
13142 {
13143 size = h->size;
13144 if (addend >= size)
13145 {
13146 /* Oops! We've got a reference past the defined end of
13147 the table. This is probably a bug -- shall we warn? */
13148 size = addend + file_align;
13149 }
13150 }
13151 size = (size + file_align - 1) & -file_align;
13152
13153 /* Allocate one extra entry for use as a "done" flag for the
13154 consolidation pass. */
13155 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13156
13157 if (ptr)
13158 {
13159 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
13160
13161 if (ptr != NULL)
13162 {
13163 size_t oldbytes;
13164
13165 oldbytes = (((h->vtable->size >> log_file_align) + 1)
13166 * sizeof (bfd_boolean));
13167 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13168 }
13169 }
13170 else
13171 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13172
13173 if (ptr == NULL)
13174 return FALSE;
13175
13176 /* And arrange for that done flag to be at index -1. */
13177 h->vtable->used = ptr + 1;
13178 h->vtable->size = size;
13179 }
13180
13181 h->vtable->used[addend >> log_file_align] = TRUE;
13182
13183 return TRUE;
13184 }
13185
13186 /* Map an ELF section header flag to its corresponding string. */
13187 typedef struct
13188 {
13189 char *flag_name;
13190 flagword flag_value;
13191 } elf_flags_to_name_table;
13192
13193 static elf_flags_to_name_table elf_flags_to_names [] =
13194 {
13195 { "SHF_WRITE", SHF_WRITE },
13196 { "SHF_ALLOC", SHF_ALLOC },
13197 { "SHF_EXECINSTR", SHF_EXECINSTR },
13198 { "SHF_MERGE", SHF_MERGE },
13199 { "SHF_STRINGS", SHF_STRINGS },
13200 { "SHF_INFO_LINK", SHF_INFO_LINK},
13201 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13202 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13203 { "SHF_GROUP", SHF_GROUP },
13204 { "SHF_TLS", SHF_TLS },
13205 { "SHF_MASKOS", SHF_MASKOS },
13206 { "SHF_EXCLUDE", SHF_EXCLUDE },
13207 };
13208
13209 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13210 bfd_boolean
13211 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13212 struct flag_info *flaginfo,
13213 asection *section)
13214 {
13215 const bfd_vma sh_flags = elf_section_flags (section);
13216
13217 if (!flaginfo->flags_initialized)
13218 {
13219 bfd *obfd = info->output_bfd;
13220 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13221 struct flag_info_list *tf = flaginfo->flag_list;
13222 int with_hex = 0;
13223 int without_hex = 0;
13224
13225 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13226 {
13227 unsigned i;
13228 flagword (*lookup) (char *);
13229
13230 lookup = bed->elf_backend_lookup_section_flags_hook;
13231 if (lookup != NULL)
13232 {
13233 flagword hexval = (*lookup) ((char *) tf->name);
13234
13235 if (hexval != 0)
13236 {
13237 if (tf->with == with_flags)
13238 with_hex |= hexval;
13239 else if (tf->with == without_flags)
13240 without_hex |= hexval;
13241 tf->valid = TRUE;
13242 continue;
13243 }
13244 }
13245 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13246 {
13247 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13248 {
13249 if (tf->with == with_flags)
13250 with_hex |= elf_flags_to_names[i].flag_value;
13251 else if (tf->with == without_flags)
13252 without_hex |= elf_flags_to_names[i].flag_value;
13253 tf->valid = TRUE;
13254 break;
13255 }
13256 }
13257 if (!tf->valid)
13258 {
13259 info->callbacks->einfo
13260 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13261 return FALSE;
13262 }
13263 }
13264 flaginfo->flags_initialized = TRUE;
13265 flaginfo->only_with_flags |= with_hex;
13266 flaginfo->not_with_flags |= without_hex;
13267 }
13268
13269 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13270 return FALSE;
13271
13272 if ((flaginfo->not_with_flags & sh_flags) != 0)
13273 return FALSE;
13274
13275 return TRUE;
13276 }
13277
13278 struct alloc_got_off_arg {
13279 bfd_vma gotoff;
13280 struct bfd_link_info *info;
13281 };
13282
13283 /* We need a special top-level link routine to convert got reference counts
13284 to real got offsets. */
13285
13286 static bfd_boolean
13287 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13288 {
13289 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13290 bfd *obfd = gofarg->info->output_bfd;
13291 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13292
13293 if (h->got.refcount > 0)
13294 {
13295 h->got.offset = gofarg->gotoff;
13296 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13297 }
13298 else
13299 h->got.offset = (bfd_vma) -1;
13300
13301 return TRUE;
13302 }
13303
13304 /* And an accompanying bit to work out final got entry offsets once
13305 we're done. Should be called from final_link. */
13306
13307 bfd_boolean
13308 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13309 struct bfd_link_info *info)
13310 {
13311 bfd *i;
13312 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13313 bfd_vma gotoff;
13314 struct alloc_got_off_arg gofarg;
13315
13316 BFD_ASSERT (abfd == info->output_bfd);
13317
13318 if (! is_elf_hash_table (info->hash))
13319 return FALSE;
13320
13321 /* The GOT offset is relative to the .got section, but the GOT header is
13322 put into the .got.plt section, if the backend uses it. */
13323 if (bed->want_got_plt)
13324 gotoff = 0;
13325 else
13326 gotoff = bed->got_header_size;
13327
13328 /* Do the local .got entries first. */
13329 for (i = info->input_bfds; i; i = i->link.next)
13330 {
13331 bfd_signed_vma *local_got;
13332 size_t j, locsymcount;
13333 Elf_Internal_Shdr *symtab_hdr;
13334
13335 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13336 continue;
13337
13338 local_got = elf_local_got_refcounts (i);
13339 if (!local_got)
13340 continue;
13341
13342 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13343 if (elf_bad_symtab (i))
13344 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13345 else
13346 locsymcount = symtab_hdr->sh_info;
13347
13348 for (j = 0; j < locsymcount; ++j)
13349 {
13350 if (local_got[j] > 0)
13351 {
13352 local_got[j] = gotoff;
13353 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13354 }
13355 else
13356 local_got[j] = (bfd_vma) -1;
13357 }
13358 }
13359
13360 /* Then the global .got entries. .plt refcounts are handled by
13361 adjust_dynamic_symbol */
13362 gofarg.gotoff = gotoff;
13363 gofarg.info = info;
13364 elf_link_hash_traverse (elf_hash_table (info),
13365 elf_gc_allocate_got_offsets,
13366 &gofarg);
13367 return TRUE;
13368 }
13369
13370 /* Many folk need no more in the way of final link than this, once
13371 got entry reference counting is enabled. */
13372
13373 bfd_boolean
13374 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13375 {
13376 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13377 return FALSE;
13378
13379 /* Invoke the regular ELF backend linker to do all the work. */
13380 return bfd_elf_final_link (abfd, info);
13381 }
13382
13383 bfd_boolean
13384 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13385 {
13386 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13387
13388 if (rcookie->bad_symtab)
13389 rcookie->rel = rcookie->rels;
13390
13391 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13392 {
13393 unsigned long r_symndx;
13394
13395 if (! rcookie->bad_symtab)
13396 if (rcookie->rel->r_offset > offset)
13397 return FALSE;
13398 if (rcookie->rel->r_offset != offset)
13399 continue;
13400
13401 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
13402 if (r_symndx == STN_UNDEF)
13403 return TRUE;
13404
13405 if (r_symndx >= rcookie->locsymcount
13406 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13407 {
13408 struct elf_link_hash_entry *h;
13409
13410 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13411
13412 while (h->root.type == bfd_link_hash_indirect
13413 || h->root.type == bfd_link_hash_warning)
13414 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13415
13416 if ((h->root.type == bfd_link_hash_defined
13417 || h->root.type == bfd_link_hash_defweak)
13418 && (h->root.u.def.section->owner != rcookie->abfd
13419 || h->root.u.def.section->kept_section != NULL
13420 || discarded_section (h->root.u.def.section)))
13421 return TRUE;
13422 }
13423 else
13424 {
13425 /* It's not a relocation against a global symbol,
13426 but it could be a relocation against a local
13427 symbol for a discarded section. */
13428 asection *isec;
13429 Elf_Internal_Sym *isym;
13430
13431 /* Need to: get the symbol; get the section. */
13432 isym = &rcookie->locsyms[r_symndx];
13433 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
13434 if (isec != NULL
13435 && (isec->kept_section != NULL
13436 || discarded_section (isec)))
13437 return TRUE;
13438 }
13439 return FALSE;
13440 }
13441 return FALSE;
13442 }
13443
13444 /* Discard unneeded references to discarded sections.
13445 Returns -1 on error, 1 if any section's size was changed, 0 if
13446 nothing changed. This function assumes that the relocations are in
13447 sorted order, which is true for all known assemblers. */
13448
13449 int
13450 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13451 {
13452 struct elf_reloc_cookie cookie;
13453 asection *o;
13454 bfd *abfd;
13455 int changed = 0;
13456
13457 if (info->traditional_format
13458 || !is_elf_hash_table (info->hash))
13459 return 0;
13460
13461 o = bfd_get_section_by_name (output_bfd, ".stab");
13462 if (o != NULL)
13463 {
13464 asection *i;
13465
13466 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13467 {
13468 if (i->size == 0
13469 || i->reloc_count == 0
13470 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13471 continue;
13472
13473 abfd = i->owner;
13474 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13475 continue;
13476
13477 if (!init_reloc_cookie_for_section (&cookie, info, i))
13478 return -1;
13479
13480 if (_bfd_discard_section_stabs (abfd, i,
13481 elf_section_data (i)->sec_info,
13482 bfd_elf_reloc_symbol_deleted_p,
13483 &cookie))
13484 changed = 1;
13485
13486 fini_reloc_cookie_for_section (&cookie, i);
13487 }
13488 }
13489
13490 o = NULL;
13491 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13492 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
13493 if (o != NULL)
13494 {
13495 asection *i;
13496
13497 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13498 {
13499 if (i->size == 0)
13500 continue;
13501
13502 abfd = i->owner;
13503 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13504 continue;
13505
13506 if (!init_reloc_cookie_for_section (&cookie, info, i))
13507 return -1;
13508
13509 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13510 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
13511 bfd_elf_reloc_symbol_deleted_p,
13512 &cookie))
13513 changed = 1;
13514
13515 fini_reloc_cookie_for_section (&cookie, i);
13516 }
13517 }
13518
13519 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13520 {
13521 const struct elf_backend_data *bed;
13522
13523 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13524 continue;
13525
13526 bed = get_elf_backend_data (abfd);
13527
13528 if (bed->elf_backend_discard_info != NULL)
13529 {
13530 if (!init_reloc_cookie (&cookie, info, abfd))
13531 return -1;
13532
13533 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
13534 changed = 1;
13535
13536 fini_reloc_cookie (&cookie, abfd);
13537 }
13538 }
13539
13540 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13541 _bfd_elf_end_eh_frame_parsing (info);
13542
13543 if (info->eh_frame_hdr_type
13544 && !bfd_link_relocatable (info)
13545 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
13546 changed = 1;
13547
13548 return changed;
13549 }
13550
13551 bfd_boolean
13552 _bfd_elf_section_already_linked (bfd *abfd,
13553 asection *sec,
13554 struct bfd_link_info *info)
13555 {
13556 flagword flags;
13557 const char *name, *key;
13558 struct bfd_section_already_linked *l;
13559 struct bfd_section_already_linked_hash_entry *already_linked_list;
13560
13561 if (sec->output_section == bfd_abs_section_ptr)
13562 return FALSE;
13563
13564 flags = sec->flags;
13565
13566 /* Return if it isn't a linkonce section. A comdat group section
13567 also has SEC_LINK_ONCE set. */
13568 if ((flags & SEC_LINK_ONCE) == 0)
13569 return FALSE;
13570
13571 /* Don't put group member sections on our list of already linked
13572 sections. They are handled as a group via their group section. */
13573 if (elf_sec_group (sec) != NULL)
13574 return FALSE;
13575
13576 /* For a SHT_GROUP section, use the group signature as the key. */
13577 name = sec->name;
13578 if ((flags & SEC_GROUP) != 0
13579 && elf_next_in_group (sec) != NULL
13580 && elf_group_name (elf_next_in_group (sec)) != NULL)
13581 key = elf_group_name (elf_next_in_group (sec));
13582 else
13583 {
13584 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
13585 if (CONST_STRNEQ (name, ".gnu.linkonce.")
13586 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13587 key++;
13588 else
13589 /* Must be a user linkonce section that doesn't follow gcc's
13590 naming convention. In this case we won't be matching
13591 single member groups. */
13592 key = name;
13593 }
13594
13595 already_linked_list = bfd_section_already_linked_table_lookup (key);
13596
13597 for (l = already_linked_list->entry; l != NULL; l = l->next)
13598 {
13599 /* We may have 2 different types of sections on the list: group
13600 sections with a signature of <key> (<key> is some string),
13601 and linkonce sections named .gnu.linkonce.<type>.<key>.
13602 Match like sections. LTO plugin sections are an exception.
13603 They are always named .gnu.linkonce.t.<key> and match either
13604 type of section. */
13605 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13606 && ((flags & SEC_GROUP) != 0
13607 || strcmp (name, l->sec->name) == 0))
13608 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
13609 {
13610 /* The section has already been linked. See if we should
13611 issue a warning. */
13612 if (!_bfd_handle_already_linked (sec, l, info))
13613 return FALSE;
13614
13615 if (flags & SEC_GROUP)
13616 {
13617 asection *first = elf_next_in_group (sec);
13618 asection *s = first;
13619
13620 while (s != NULL)
13621 {
13622 s->output_section = bfd_abs_section_ptr;
13623 /* Record which group discards it. */
13624 s->kept_section = l->sec;
13625 s = elf_next_in_group (s);
13626 /* These lists are circular. */
13627 if (s == first)
13628 break;
13629 }
13630 }
13631
13632 return TRUE;
13633 }
13634 }
13635
13636 /* A single member comdat group section may be discarded by a
13637 linkonce section and vice versa. */
13638 if ((flags & SEC_GROUP) != 0)
13639 {
13640 asection *first = elf_next_in_group (sec);
13641
13642 if (first != NULL && elf_next_in_group (first) == first)
13643 /* Check this single member group against linkonce sections. */
13644 for (l = already_linked_list->entry; l != NULL; l = l->next)
13645 if ((l->sec->flags & SEC_GROUP) == 0
13646 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13647 {
13648 first->output_section = bfd_abs_section_ptr;
13649 first->kept_section = l->sec;
13650 sec->output_section = bfd_abs_section_ptr;
13651 break;
13652 }
13653 }
13654 else
13655 /* Check this linkonce section against single member groups. */
13656 for (l = already_linked_list->entry; l != NULL; l = l->next)
13657 if (l->sec->flags & SEC_GROUP)
13658 {
13659 asection *first = elf_next_in_group (l->sec);
13660
13661 if (first != NULL
13662 && elf_next_in_group (first) == first
13663 && bfd_elf_match_symbols_in_sections (first, sec, info))
13664 {
13665 sec->output_section = bfd_abs_section_ptr;
13666 sec->kept_section = first;
13667 break;
13668 }
13669 }
13670
13671 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13672 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13673 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13674 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13675 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13676 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13677 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13678 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13679 The reverse order cannot happen as there is never a bfd with only the
13680 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13681 matter as here were are looking only for cross-bfd sections. */
13682
13683 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13684 for (l = already_linked_list->entry; l != NULL; l = l->next)
13685 if ((l->sec->flags & SEC_GROUP) == 0
13686 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13687 {
13688 if (abfd != l->sec->owner)
13689 sec->output_section = bfd_abs_section_ptr;
13690 break;
13691 }
13692
13693 /* This is the first section with this name. Record it. */
13694 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
13695 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
13696 return sec->output_section == bfd_abs_section_ptr;
13697 }
13698
13699 bfd_boolean
13700 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
13701 {
13702 return sym->st_shndx == SHN_COMMON;
13703 }
13704
13705 unsigned int
13706 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13707 {
13708 return SHN_COMMON;
13709 }
13710
13711 asection *
13712 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13713 {
13714 return bfd_com_section_ptr;
13715 }
13716
13717 bfd_vma
13718 _bfd_elf_default_got_elt_size (bfd *abfd,
13719 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13720 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13721 bfd *ibfd ATTRIBUTE_UNUSED,
13722 unsigned long symndx ATTRIBUTE_UNUSED)
13723 {
13724 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13725 return bed->s->arch_size / 8;
13726 }
13727
13728 /* Routines to support the creation of dynamic relocs. */
13729
13730 /* Returns the name of the dynamic reloc section associated with SEC. */
13731
13732 static const char *
13733 get_dynamic_reloc_section_name (bfd * abfd,
13734 asection * sec,
13735 bfd_boolean is_rela)
13736 {
13737 char *name;
13738 const char *old_name = bfd_get_section_name (NULL, sec);
13739 const char *prefix = is_rela ? ".rela" : ".rel";
13740
13741 if (old_name == NULL)
13742 return NULL;
13743
13744 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
13745 sprintf (name, "%s%s", prefix, old_name);
13746
13747 return name;
13748 }
13749
13750 /* Returns the dynamic reloc section associated with SEC.
13751 If necessary compute the name of the dynamic reloc section based
13752 on SEC's name (looked up in ABFD's string table) and the setting
13753 of IS_RELA. */
13754
13755 asection *
13756 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
13757 asection * sec,
13758 bfd_boolean is_rela)
13759 {
13760 asection * reloc_sec = elf_section_data (sec)->sreloc;
13761
13762 if (reloc_sec == NULL)
13763 {
13764 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13765
13766 if (name != NULL)
13767 {
13768 reloc_sec = bfd_get_linker_section (abfd, name);
13769
13770 if (reloc_sec != NULL)
13771 elf_section_data (sec)->sreloc = reloc_sec;
13772 }
13773 }
13774
13775 return reloc_sec;
13776 }
13777
13778 /* Returns the dynamic reloc section associated with SEC. If the
13779 section does not exist it is created and attached to the DYNOBJ
13780 bfd and stored in the SRELOC field of SEC's elf_section_data
13781 structure.
13782
13783 ALIGNMENT is the alignment for the newly created section and
13784 IS_RELA defines whether the name should be .rela.<SEC's name>
13785 or .rel.<SEC's name>. The section name is looked up in the
13786 string table associated with ABFD. */
13787
13788 asection *
13789 _bfd_elf_make_dynamic_reloc_section (asection *sec,
13790 bfd *dynobj,
13791 unsigned int alignment,
13792 bfd *abfd,
13793 bfd_boolean is_rela)
13794 {
13795 asection * reloc_sec = elf_section_data (sec)->sreloc;
13796
13797 if (reloc_sec == NULL)
13798 {
13799 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13800
13801 if (name == NULL)
13802 return NULL;
13803
13804 reloc_sec = bfd_get_linker_section (dynobj, name);
13805
13806 if (reloc_sec == NULL)
13807 {
13808 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
13809 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
13810 if ((sec->flags & SEC_ALLOC) != 0)
13811 flags |= SEC_ALLOC | SEC_LOAD;
13812
13813 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
13814 if (reloc_sec != NULL)
13815 {
13816 /* _bfd_elf_get_sec_type_attr chooses a section type by
13817 name. Override as it may be wrong, eg. for a user
13818 section named "auto" we'll get ".relauto" which is
13819 seen to be a .rela section. */
13820 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
13821 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
13822 reloc_sec = NULL;
13823 }
13824 }
13825
13826 elf_section_data (sec)->sreloc = reloc_sec;
13827 }
13828
13829 return reloc_sec;
13830 }
13831
13832 /* Copy the ELF symbol type and other attributes for a linker script
13833 assignment from HSRC to HDEST. Generally this should be treated as
13834 if we found a strong non-dynamic definition for HDEST (except that
13835 ld ignores multiple definition errors). */
13836 void
13837 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
13838 struct bfd_link_hash_entry *hdest,
13839 struct bfd_link_hash_entry *hsrc)
13840 {
13841 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
13842 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
13843 Elf_Internal_Sym isym;
13844
13845 ehdest->type = ehsrc->type;
13846 ehdest->target_internal = ehsrc->target_internal;
13847
13848 isym.st_other = ehsrc->other;
13849 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
13850 }
13851
13852 /* Append a RELA relocation REL to section S in BFD. */
13853
13854 void
13855 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13856 {
13857 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13858 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
13859 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
13860 bed->s->swap_reloca_out (abfd, rel, loc);
13861 }
13862
13863 /* Append a REL relocation REL to section S in BFD. */
13864
13865 void
13866 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13867 {
13868 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13869 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
13870 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
13871 bed->s->swap_reloc_out (abfd, rel, loc);
13872 }