Properly handle indirect symbols
[deliverable/binutils-gdb.git] / bfd / elflink.c
1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 Free Software Foundation, Inc.
5
6 This file is part of BFD, the Binary File Descriptor library.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
22
23 #include "sysdep.h"
24 #include "bfd.h"
25 #include "bfdlink.h"
26 #include "libbfd.h"
27 #define ARCH_SIZE 0
28 #include "elf-bfd.h"
29 #include "safe-ctype.h"
30 #include "libiberty.h"
31 #include "objalloc.h"
32
33 /* This struct is used to pass information to routines called via
34 elf_link_hash_traverse which must return failure. */
35
36 struct elf_info_failed
37 {
38 struct bfd_link_info *info;
39 bfd_boolean failed;
40 };
41
42 /* This structure is used to pass information to
43 _bfd_elf_link_find_version_dependencies. */
44
45 struct elf_find_verdep_info
46 {
47 /* General link information. */
48 struct bfd_link_info *info;
49 /* The number of dependencies. */
50 unsigned int vers;
51 /* Whether we had a failure. */
52 bfd_boolean failed;
53 };
54
55 static bfd_boolean _bfd_elf_fix_symbol_flags
56 (struct elf_link_hash_entry *, struct elf_info_failed *);
57
58 /* Define a symbol in a dynamic linkage section. */
59
60 struct elf_link_hash_entry *
61 _bfd_elf_define_linkage_sym (bfd *abfd,
62 struct bfd_link_info *info,
63 asection *sec,
64 const char *name)
65 {
66 struct elf_link_hash_entry *h;
67 struct bfd_link_hash_entry *bh;
68 const struct elf_backend_data *bed;
69
70 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
71 if (h != NULL)
72 {
73 /* Zap symbol defined in an as-needed lib that wasn't linked.
74 This is a symptom of a larger problem: Absolute symbols
75 defined in shared libraries can't be overridden, because we
76 lose the link to the bfd which is via the symbol section. */
77 h->root.type = bfd_link_hash_new;
78 }
79
80 bh = &h->root;
81 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
82 sec, 0, NULL, FALSE,
83 get_elf_backend_data (abfd)->collect,
84 &bh))
85 return NULL;
86 h = (struct elf_link_hash_entry *) bh;
87 h->def_regular = 1;
88 h->non_elf = 0;
89 h->type = STT_OBJECT;
90 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
91
92 bed = get_elf_backend_data (abfd);
93 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
94 return h;
95 }
96
97 bfd_boolean
98 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
99 {
100 flagword flags;
101 asection *s;
102 struct elf_link_hash_entry *h;
103 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
104 struct elf_link_hash_table *htab = elf_hash_table (info);
105
106 /* This function may be called more than once. */
107 s = bfd_get_linker_section (abfd, ".got");
108 if (s != NULL)
109 return TRUE;
110
111 flags = bed->dynamic_sec_flags;
112
113 s = bfd_make_section_anyway_with_flags (abfd,
114 (bed->rela_plts_and_copies_p
115 ? ".rela.got" : ".rel.got"),
116 (bed->dynamic_sec_flags
117 | SEC_READONLY));
118 if (s == NULL
119 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
120 return FALSE;
121 htab->srelgot = s;
122
123 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
124 if (s == NULL
125 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
126 return FALSE;
127 htab->sgot = s;
128
129 if (bed->want_got_plt)
130 {
131 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
132 if (s == NULL
133 || !bfd_set_section_alignment (abfd, s,
134 bed->s->log_file_align))
135 return FALSE;
136 htab->sgotplt = s;
137 }
138
139 /* The first bit of the global offset table is the header. */
140 s->size += bed->got_header_size;
141
142 if (bed->want_got_sym)
143 {
144 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
145 (or .got.plt) section. We don't do this in the linker script
146 because we don't want to define the symbol if we are not creating
147 a global offset table. */
148 h = _bfd_elf_define_linkage_sym (abfd, info, s,
149 "_GLOBAL_OFFSET_TABLE_");
150 elf_hash_table (info)->hgot = h;
151 if (h == NULL)
152 return FALSE;
153 }
154
155 return TRUE;
156 }
157 \f
158 /* Create a strtab to hold the dynamic symbol names. */
159 static bfd_boolean
160 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
161 {
162 struct elf_link_hash_table *hash_table;
163
164 hash_table = elf_hash_table (info);
165 if (hash_table->dynobj == NULL)
166 hash_table->dynobj = abfd;
167
168 if (hash_table->dynstr == NULL)
169 {
170 hash_table->dynstr = _bfd_elf_strtab_init ();
171 if (hash_table->dynstr == NULL)
172 return FALSE;
173 }
174 return TRUE;
175 }
176
177 /* Create some sections which will be filled in with dynamic linking
178 information. ABFD is an input file which requires dynamic sections
179 to be created. The dynamic sections take up virtual memory space
180 when the final executable is run, so we need to create them before
181 addresses are assigned to the output sections. We work out the
182 actual contents and size of these sections later. */
183
184 bfd_boolean
185 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
186 {
187 flagword flags;
188 asection *s;
189 const struct elf_backend_data *bed;
190
191 if (! is_elf_hash_table (info->hash))
192 return FALSE;
193
194 if (elf_hash_table (info)->dynamic_sections_created)
195 return TRUE;
196
197 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
198 return FALSE;
199
200 abfd = elf_hash_table (info)->dynobj;
201 bed = get_elf_backend_data (abfd);
202
203 flags = bed->dynamic_sec_flags;
204
205 /* A dynamically linked executable has a .interp section, but a
206 shared library does not. */
207 if (info->executable)
208 {
209 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
210 flags | SEC_READONLY);
211 if (s == NULL)
212 return FALSE;
213 }
214
215 /* Create sections to hold version informations. These are removed
216 if they are not needed. */
217 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
218 flags | SEC_READONLY);
219 if (s == NULL
220 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
221 return FALSE;
222
223 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
224 flags | SEC_READONLY);
225 if (s == NULL
226 || ! bfd_set_section_alignment (abfd, s, 1))
227 return FALSE;
228
229 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
230 flags | SEC_READONLY);
231 if (s == NULL
232 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
233 return FALSE;
234
235 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
236 flags | SEC_READONLY);
237 if (s == NULL
238 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
239 return FALSE;
240
241 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
242 flags | SEC_READONLY);
243 if (s == NULL)
244 return FALSE;
245
246 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
247 if (s == NULL
248 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
249 return FALSE;
250
251 /* The special symbol _DYNAMIC is always set to the start of the
252 .dynamic section. We could set _DYNAMIC in a linker script, but we
253 only want to define it if we are, in fact, creating a .dynamic
254 section. We don't want to define it if there is no .dynamic
255 section, since on some ELF platforms the start up code examines it
256 to decide how to initialize the process. */
257 if (!_bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC"))
258 return FALSE;
259
260 if (info->emit_hash)
261 {
262 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
263 flags | SEC_READONLY);
264 if (s == NULL
265 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
266 return FALSE;
267 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
268 }
269
270 if (info->emit_gnu_hash)
271 {
272 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
273 flags | SEC_READONLY);
274 if (s == NULL
275 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
276 return FALSE;
277 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
278 4 32-bit words followed by variable count of 64-bit words, then
279 variable count of 32-bit words. */
280 if (bed->s->arch_size == 64)
281 elf_section_data (s)->this_hdr.sh_entsize = 0;
282 else
283 elf_section_data (s)->this_hdr.sh_entsize = 4;
284 }
285
286 /* Let the backend create the rest of the sections. This lets the
287 backend set the right flags. The backend will normally create
288 the .got and .plt sections. */
289 if (bed->elf_backend_create_dynamic_sections == NULL
290 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
291 return FALSE;
292
293 elf_hash_table (info)->dynamic_sections_created = TRUE;
294
295 return TRUE;
296 }
297
298 /* Create dynamic sections when linking against a dynamic object. */
299
300 bfd_boolean
301 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
302 {
303 flagword flags, pltflags;
304 struct elf_link_hash_entry *h;
305 asection *s;
306 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
307 struct elf_link_hash_table *htab = elf_hash_table (info);
308
309 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
310 .rel[a].bss sections. */
311 flags = bed->dynamic_sec_flags;
312
313 pltflags = flags;
314 if (bed->plt_not_loaded)
315 /* We do not clear SEC_ALLOC here because we still want the OS to
316 allocate space for the section; it's just that there's nothing
317 to read in from the object file. */
318 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
319 else
320 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
321 if (bed->plt_readonly)
322 pltflags |= SEC_READONLY;
323
324 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
325 if (s == NULL
326 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
327 return FALSE;
328 htab->splt = s;
329
330 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
331 .plt section. */
332 if (bed->want_plt_sym)
333 {
334 h = _bfd_elf_define_linkage_sym (abfd, info, s,
335 "_PROCEDURE_LINKAGE_TABLE_");
336 elf_hash_table (info)->hplt = h;
337 if (h == NULL)
338 return FALSE;
339 }
340
341 s = bfd_make_section_anyway_with_flags (abfd,
342 (bed->rela_plts_and_copies_p
343 ? ".rela.plt" : ".rel.plt"),
344 flags | SEC_READONLY);
345 if (s == NULL
346 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
347 return FALSE;
348 htab->srelplt = s;
349
350 if (! _bfd_elf_create_got_section (abfd, info))
351 return FALSE;
352
353 if (bed->want_dynbss)
354 {
355 /* The .dynbss section is a place to put symbols which are defined
356 by dynamic objects, are referenced by regular objects, and are
357 not functions. We must allocate space for them in the process
358 image and use a R_*_COPY reloc to tell the dynamic linker to
359 initialize them at run time. The linker script puts the .dynbss
360 section into the .bss section of the final image. */
361 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
362 (SEC_ALLOC | SEC_LINKER_CREATED));
363 if (s == NULL)
364 return FALSE;
365
366 /* The .rel[a].bss section holds copy relocs. This section is not
367 normally needed. We need to create it here, though, so that the
368 linker will map it to an output section. We can't just create it
369 only if we need it, because we will not know whether we need it
370 until we have seen all the input files, and the first time the
371 main linker code calls BFD after examining all the input files
372 (size_dynamic_sections) the input sections have already been
373 mapped to the output sections. If the section turns out not to
374 be needed, we can discard it later. We will never need this
375 section when generating a shared object, since they do not use
376 copy relocs. */
377 if (! info->shared)
378 {
379 s = bfd_make_section_anyway_with_flags (abfd,
380 (bed->rela_plts_and_copies_p
381 ? ".rela.bss" : ".rel.bss"),
382 flags | SEC_READONLY);
383 if (s == NULL
384 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
385 return FALSE;
386 }
387 }
388
389 return TRUE;
390 }
391 \f
392 /* Record a new dynamic symbol. We record the dynamic symbols as we
393 read the input files, since we need to have a list of all of them
394 before we can determine the final sizes of the output sections.
395 Note that we may actually call this function even though we are not
396 going to output any dynamic symbols; in some cases we know that a
397 symbol should be in the dynamic symbol table, but only if there is
398 one. */
399
400 bfd_boolean
401 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
402 struct elf_link_hash_entry *h)
403 {
404 if (h->dynindx == -1)
405 {
406 struct elf_strtab_hash *dynstr;
407 char *p;
408 const char *name;
409 bfd_size_type indx;
410
411 /* XXX: The ABI draft says the linker must turn hidden and
412 internal symbols into STB_LOCAL symbols when producing the
413 DSO. However, if ld.so honors st_other in the dynamic table,
414 this would not be necessary. */
415 switch (ELF_ST_VISIBILITY (h->other))
416 {
417 case STV_INTERNAL:
418 case STV_HIDDEN:
419 if (h->root.type != bfd_link_hash_undefined
420 && h->root.type != bfd_link_hash_undefweak)
421 {
422 h->forced_local = 1;
423 if (!elf_hash_table (info)->is_relocatable_executable)
424 return TRUE;
425 }
426
427 default:
428 break;
429 }
430
431 h->dynindx = elf_hash_table (info)->dynsymcount;
432 ++elf_hash_table (info)->dynsymcount;
433
434 dynstr = elf_hash_table (info)->dynstr;
435 if (dynstr == NULL)
436 {
437 /* Create a strtab to hold the dynamic symbol names. */
438 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
439 if (dynstr == NULL)
440 return FALSE;
441 }
442
443 /* We don't put any version information in the dynamic string
444 table. */
445 name = h->root.root.string;
446 p = strchr (name, ELF_VER_CHR);
447 if (p != NULL)
448 /* We know that the p points into writable memory. In fact,
449 there are only a few symbols that have read-only names, being
450 those like _GLOBAL_OFFSET_TABLE_ that are created specially
451 by the backends. Most symbols will have names pointing into
452 an ELF string table read from a file, or to objalloc memory. */
453 *p = 0;
454
455 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
456
457 if (p != NULL)
458 *p = ELF_VER_CHR;
459
460 if (indx == (bfd_size_type) -1)
461 return FALSE;
462 h->dynstr_index = indx;
463 }
464
465 return TRUE;
466 }
467 \f
468 /* Mark a symbol dynamic. */
469
470 static void
471 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
472 struct elf_link_hash_entry *h,
473 Elf_Internal_Sym *sym)
474 {
475 struct bfd_elf_dynamic_list *d = info->dynamic_list;
476
477 /* It may be called more than once on the same H. */
478 if(h->dynamic || info->relocatable)
479 return;
480
481 if ((info->dynamic_data
482 && (h->type == STT_OBJECT
483 || (sym != NULL
484 && ELF_ST_TYPE (sym->st_info) == STT_OBJECT)))
485 || (d != NULL
486 && h->root.type == bfd_link_hash_new
487 && (*d->match) (&d->head, NULL, h->root.root.string)))
488 h->dynamic = 1;
489 }
490
491 /* Record an assignment to a symbol made by a linker script. We need
492 this in case some dynamic object refers to this symbol. */
493
494 bfd_boolean
495 bfd_elf_record_link_assignment (bfd *output_bfd,
496 struct bfd_link_info *info,
497 const char *name,
498 bfd_boolean provide,
499 bfd_boolean hidden)
500 {
501 struct elf_link_hash_entry *h, *hv;
502 struct elf_link_hash_table *htab;
503 const struct elf_backend_data *bed;
504
505 if (!is_elf_hash_table (info->hash))
506 return TRUE;
507
508 htab = elf_hash_table (info);
509 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
510 if (h == NULL)
511 return provide;
512
513 switch (h->root.type)
514 {
515 case bfd_link_hash_defined:
516 case bfd_link_hash_defweak:
517 case bfd_link_hash_common:
518 break;
519 case bfd_link_hash_undefweak:
520 case bfd_link_hash_undefined:
521 /* Since we're defining the symbol, don't let it seem to have not
522 been defined. record_dynamic_symbol and size_dynamic_sections
523 may depend on this. */
524 h->root.type = bfd_link_hash_new;
525 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
526 bfd_link_repair_undef_list (&htab->root);
527 break;
528 case bfd_link_hash_new:
529 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
530 h->non_elf = 0;
531 break;
532 case bfd_link_hash_indirect:
533 /* We had a versioned symbol in a dynamic library. We make the
534 the versioned symbol point to this one. */
535 bed = get_elf_backend_data (output_bfd);
536 hv = h;
537 while (hv->root.type == bfd_link_hash_indirect
538 || hv->root.type == bfd_link_hash_warning)
539 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
540 /* We don't need to update h->root.u since linker will set them
541 later. */
542 h->root.type = bfd_link_hash_undefined;
543 hv->root.type = bfd_link_hash_indirect;
544 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
545 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
546 break;
547 case bfd_link_hash_warning:
548 abort ();
549 break;
550 }
551
552 /* If this symbol is being provided by the linker script, and it is
553 currently defined by a dynamic object, but not by a regular
554 object, then mark it as undefined so that the generic linker will
555 force the correct value. */
556 if (provide
557 && h->def_dynamic
558 && !h->def_regular)
559 h->root.type = bfd_link_hash_undefined;
560
561 /* If this symbol is not being provided by the linker script, and it is
562 currently defined by a dynamic object, but not by a regular object,
563 then clear out any version information because the symbol will not be
564 associated with the dynamic object any more. */
565 if (!provide
566 && h->def_dynamic
567 && !h->def_regular)
568 h->verinfo.verdef = NULL;
569
570 h->def_regular = 1;
571
572 if (provide && hidden)
573 {
574 bed = get_elf_backend_data (output_bfd);
575 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
576 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
577 }
578
579 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
580 and executables. */
581 if (!info->relocatable
582 && h->dynindx != -1
583 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
584 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
585 h->forced_local = 1;
586
587 if ((h->def_dynamic
588 || h->ref_dynamic
589 || info->shared
590 || (info->executable && elf_hash_table (info)->is_relocatable_executable))
591 && h->dynindx == -1)
592 {
593 if (! bfd_elf_link_record_dynamic_symbol (info, h))
594 return FALSE;
595
596 /* If this is a weak defined symbol, and we know a corresponding
597 real symbol from the same dynamic object, make sure the real
598 symbol is also made into a dynamic symbol. */
599 if (h->u.weakdef != NULL
600 && h->u.weakdef->dynindx == -1)
601 {
602 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
603 return FALSE;
604 }
605 }
606
607 return TRUE;
608 }
609
610 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
611 success, and 2 on a failure caused by attempting to record a symbol
612 in a discarded section, eg. a discarded link-once section symbol. */
613
614 int
615 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
616 bfd *input_bfd,
617 long input_indx)
618 {
619 bfd_size_type amt;
620 struct elf_link_local_dynamic_entry *entry;
621 struct elf_link_hash_table *eht;
622 struct elf_strtab_hash *dynstr;
623 unsigned long dynstr_index;
624 char *name;
625 Elf_External_Sym_Shndx eshndx;
626 char esym[sizeof (Elf64_External_Sym)];
627
628 if (! is_elf_hash_table (info->hash))
629 return 0;
630
631 /* See if the entry exists already. */
632 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
633 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
634 return 1;
635
636 amt = sizeof (*entry);
637 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
638 if (entry == NULL)
639 return 0;
640
641 /* Go find the symbol, so that we can find it's name. */
642 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
643 1, input_indx, &entry->isym, esym, &eshndx))
644 {
645 bfd_release (input_bfd, entry);
646 return 0;
647 }
648
649 if (entry->isym.st_shndx != SHN_UNDEF
650 && entry->isym.st_shndx < SHN_LORESERVE)
651 {
652 asection *s;
653
654 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
655 if (s == NULL || bfd_is_abs_section (s->output_section))
656 {
657 /* We can still bfd_release here as nothing has done another
658 bfd_alloc. We can't do this later in this function. */
659 bfd_release (input_bfd, entry);
660 return 2;
661 }
662 }
663
664 name = (bfd_elf_string_from_elf_section
665 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
666 entry->isym.st_name));
667
668 dynstr = elf_hash_table (info)->dynstr;
669 if (dynstr == NULL)
670 {
671 /* Create a strtab to hold the dynamic symbol names. */
672 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
673 if (dynstr == NULL)
674 return 0;
675 }
676
677 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
678 if (dynstr_index == (unsigned long) -1)
679 return 0;
680 entry->isym.st_name = dynstr_index;
681
682 eht = elf_hash_table (info);
683
684 entry->next = eht->dynlocal;
685 eht->dynlocal = entry;
686 entry->input_bfd = input_bfd;
687 entry->input_indx = input_indx;
688 eht->dynsymcount++;
689
690 /* Whatever binding the symbol had before, it's now local. */
691 entry->isym.st_info
692 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
693
694 /* The dynindx will be set at the end of size_dynamic_sections. */
695
696 return 1;
697 }
698
699 /* Return the dynindex of a local dynamic symbol. */
700
701 long
702 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
703 bfd *input_bfd,
704 long input_indx)
705 {
706 struct elf_link_local_dynamic_entry *e;
707
708 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
709 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
710 return e->dynindx;
711 return -1;
712 }
713
714 /* This function is used to renumber the dynamic symbols, if some of
715 them are removed because they are marked as local. This is called
716 via elf_link_hash_traverse. */
717
718 static bfd_boolean
719 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
720 void *data)
721 {
722 size_t *count = (size_t *) data;
723
724 if (h->forced_local)
725 return TRUE;
726
727 if (h->dynindx != -1)
728 h->dynindx = ++(*count);
729
730 return TRUE;
731 }
732
733
734 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
735 STB_LOCAL binding. */
736
737 static bfd_boolean
738 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
739 void *data)
740 {
741 size_t *count = (size_t *) data;
742
743 if (!h->forced_local)
744 return TRUE;
745
746 if (h->dynindx != -1)
747 h->dynindx = ++(*count);
748
749 return TRUE;
750 }
751
752 /* Return true if the dynamic symbol for a given section should be
753 omitted when creating a shared library. */
754 bfd_boolean
755 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
756 struct bfd_link_info *info,
757 asection *p)
758 {
759 struct elf_link_hash_table *htab;
760
761 switch (elf_section_data (p)->this_hdr.sh_type)
762 {
763 case SHT_PROGBITS:
764 case SHT_NOBITS:
765 /* If sh_type is yet undecided, assume it could be
766 SHT_PROGBITS/SHT_NOBITS. */
767 case SHT_NULL:
768 htab = elf_hash_table (info);
769 if (p == htab->tls_sec)
770 return FALSE;
771
772 if (htab->text_index_section != NULL)
773 return p != htab->text_index_section && p != htab->data_index_section;
774
775 if (strcmp (p->name, ".got") == 0
776 || strcmp (p->name, ".got.plt") == 0
777 || strcmp (p->name, ".plt") == 0)
778 {
779 asection *ip;
780
781 if (htab->dynobj != NULL
782 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
783 && ip->output_section == p)
784 return TRUE;
785 }
786 return FALSE;
787
788 /* There shouldn't be section relative relocations
789 against any other section. */
790 default:
791 return TRUE;
792 }
793 }
794
795 /* Assign dynsym indices. In a shared library we generate a section
796 symbol for each output section, which come first. Next come symbols
797 which have been forced to local binding. Then all of the back-end
798 allocated local dynamic syms, followed by the rest of the global
799 symbols. */
800
801 static unsigned long
802 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
803 struct bfd_link_info *info,
804 unsigned long *section_sym_count)
805 {
806 unsigned long dynsymcount = 0;
807
808 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
809 {
810 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
811 asection *p;
812 for (p = output_bfd->sections; p ; p = p->next)
813 if ((p->flags & SEC_EXCLUDE) == 0
814 && (p->flags & SEC_ALLOC) != 0
815 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
816 elf_section_data (p)->dynindx = ++dynsymcount;
817 else
818 elf_section_data (p)->dynindx = 0;
819 }
820 *section_sym_count = dynsymcount;
821
822 elf_link_hash_traverse (elf_hash_table (info),
823 elf_link_renumber_local_hash_table_dynsyms,
824 &dynsymcount);
825
826 if (elf_hash_table (info)->dynlocal)
827 {
828 struct elf_link_local_dynamic_entry *p;
829 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
830 p->dynindx = ++dynsymcount;
831 }
832
833 elf_link_hash_traverse (elf_hash_table (info),
834 elf_link_renumber_hash_table_dynsyms,
835 &dynsymcount);
836
837 /* There is an unused NULL entry at the head of the table which
838 we must account for in our count. Unless there weren't any
839 symbols, which means we'll have no table at all. */
840 if (dynsymcount != 0)
841 ++dynsymcount;
842
843 elf_hash_table (info)->dynsymcount = dynsymcount;
844 return dynsymcount;
845 }
846
847 /* Merge st_other field. */
848
849 static void
850 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
851 Elf_Internal_Sym *isym, bfd_boolean definition,
852 bfd_boolean dynamic)
853 {
854 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
855
856 /* If st_other has a processor-specific meaning, specific
857 code might be needed here. We never merge the visibility
858 attribute with the one from a dynamic object. */
859 if (bed->elf_backend_merge_symbol_attribute)
860 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
861 dynamic);
862
863 /* If this symbol has default visibility and the user has requested
864 we not re-export it, then mark it as hidden. */
865 if (definition
866 && !dynamic
867 && (abfd->no_export
868 || (abfd->my_archive && abfd->my_archive->no_export))
869 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
870 isym->st_other = (STV_HIDDEN
871 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
872
873 if (!dynamic && ELF_ST_VISIBILITY (isym->st_other) != 0)
874 {
875 unsigned char hvis, symvis, other, nvis;
876
877 /* Only merge the visibility. Leave the remainder of the
878 st_other field to elf_backend_merge_symbol_attribute. */
879 other = h->other & ~ELF_ST_VISIBILITY (-1);
880
881 /* Combine visibilities, using the most constraining one. */
882 hvis = ELF_ST_VISIBILITY (h->other);
883 symvis = ELF_ST_VISIBILITY (isym->st_other);
884 if (! hvis)
885 nvis = symvis;
886 else if (! symvis)
887 nvis = hvis;
888 else
889 nvis = hvis < symvis ? hvis : symvis;
890
891 h->other = other | nvis;
892 }
893 }
894
895 /* Mark if a symbol has a definition in a dynamic object or is
896 weak in all dynamic objects. */
897
898 static void
899 _bfd_elf_mark_dynamic_def_weak (struct elf_link_hash_entry *h,
900 asection *sec, int bind)
901 {
902 if (!h->dynamic_def)
903 {
904 if (!bfd_is_und_section (sec))
905 h->dynamic_def = 1;
906 else
907 {
908 /* Check if this symbol is weak in all dynamic objects. If it
909 is the first time we see it in a dynamic object, we mark
910 if it is weak. Otherwise, we clear it. */
911 if (!h->ref_dynamic)
912 {
913 if (bind == STB_WEAK)
914 h->dynamic_weak = 1;
915 }
916 else if (bind != STB_WEAK)
917 h->dynamic_weak = 0;
918 }
919 }
920 }
921
922 /* This function is called when we want to define a new symbol. It
923 handles the various cases which arise when we find a definition in
924 a dynamic object, or when there is already a definition in a
925 dynamic object. The new symbol is described by NAME, SYM, PSEC,
926 and PVALUE. We set SYM_HASH to the hash table entry. We set
927 OVERRIDE if the old symbol is overriding a new definition. We set
928 TYPE_CHANGE_OK if it is OK for the type to change. We set
929 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
930 change, we mean that we shouldn't warn if the type or size does
931 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic
932 object is overridden by a regular object. */
933
934 bfd_boolean
935 _bfd_elf_merge_symbol (bfd *abfd,
936 struct bfd_link_info *info,
937 const char *name,
938 Elf_Internal_Sym *sym,
939 asection **psec,
940 bfd_vma *pvalue,
941 unsigned int *pold_alignment,
942 struct elf_link_hash_entry **sym_hash,
943 bfd_boolean *skip,
944 bfd_boolean *override,
945 bfd_boolean *type_change_ok,
946 bfd_boolean *size_change_ok)
947 {
948 asection *sec, *oldsec;
949 struct elf_link_hash_entry *h;
950 struct elf_link_hash_entry *hi;
951 struct elf_link_hash_entry *flip;
952 int bind;
953 bfd *oldbfd;
954 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
955 bfd_boolean newweak, oldweak, newfunc, oldfunc;
956 const struct elf_backend_data *bed;
957
958 *skip = FALSE;
959 *override = FALSE;
960
961 sec = *psec;
962 bind = ELF_ST_BIND (sym->st_info);
963
964 /* Silently discard TLS symbols from --just-syms. There's no way to
965 combine a static TLS block with a new TLS block for this executable. */
966 if (ELF_ST_TYPE (sym->st_info) == STT_TLS
967 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
968 {
969 *skip = TRUE;
970 return TRUE;
971 }
972
973 if (! bfd_is_und_section (sec))
974 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
975 else
976 h = ((struct elf_link_hash_entry *)
977 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
978 if (h == NULL)
979 return FALSE;
980 *sym_hash = h;
981
982 bed = get_elf_backend_data (abfd);
983
984 /* This code is for coping with dynamic objects, and is only useful
985 if we are doing an ELF link. */
986 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
987 return TRUE;
988
989 /* For merging, we only care about real symbols. But we need to make
990 sure that indirect symbol dynamic flags are updated. */
991 hi = h;
992 while (h->root.type == bfd_link_hash_indirect
993 || h->root.type == bfd_link_hash_warning)
994 h = (struct elf_link_hash_entry *) h->root.u.i.link;
995
996 /* We have to check it for every instance since the first few may be
997 refereences and not all compilers emit symbol type for undefined
998 symbols. */
999 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1000
1001 /* If we just created the symbol, mark it as being an ELF symbol.
1002 Other than that, there is nothing to do--there is no merge issue
1003 with a newly defined symbol--so we just return. */
1004
1005 if (h->root.type == bfd_link_hash_new)
1006 {
1007 h->non_elf = 0;
1008 return TRUE;
1009 }
1010
1011 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1012 existing symbol. */
1013
1014 switch (h->root.type)
1015 {
1016 default:
1017 oldbfd = NULL;
1018 oldsec = NULL;
1019 break;
1020
1021 case bfd_link_hash_undefined:
1022 case bfd_link_hash_undefweak:
1023 oldbfd = h->root.u.undef.abfd;
1024 oldsec = NULL;
1025 break;
1026
1027 case bfd_link_hash_defined:
1028 case bfd_link_hash_defweak:
1029 oldbfd = h->root.u.def.section->owner;
1030 oldsec = h->root.u.def.section;
1031 break;
1032
1033 case bfd_link_hash_common:
1034 oldbfd = h->root.u.c.p->section->owner;
1035 oldsec = h->root.u.c.p->section;
1036 break;
1037 }
1038
1039 /* Differentiate strong and weak symbols. */
1040 newweak = bind == STB_WEAK;
1041 oldweak = (h->root.type == bfd_link_hash_defweak
1042 || h->root.type == bfd_link_hash_undefweak);
1043
1044 /* In cases involving weak versioned symbols, we may wind up trying
1045 to merge a symbol with itself. Catch that here, to avoid the
1046 confusion that results if we try to override a symbol with
1047 itself. The additional tests catch cases like
1048 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1049 dynamic object, which we do want to handle here. */
1050 if (abfd == oldbfd
1051 && (newweak || oldweak)
1052 && ((abfd->flags & DYNAMIC) == 0
1053 || !h->def_regular))
1054 return TRUE;
1055
1056 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1057 respectively, is from a dynamic object. */
1058
1059 newdyn = (abfd->flags & DYNAMIC) != 0;
1060
1061 olddyn = FALSE;
1062 if (oldbfd != NULL)
1063 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1064 else if (oldsec != NULL)
1065 {
1066 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1067 indices used by MIPS ELF. */
1068 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1069 }
1070
1071 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1072 respectively, appear to be a definition rather than reference. */
1073
1074 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1075
1076 olddef = (h->root.type != bfd_link_hash_undefined
1077 && h->root.type != bfd_link_hash_undefweak
1078 && h->root.type != bfd_link_hash_common);
1079
1080 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1081 respectively, appear to be a function. */
1082
1083 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1084 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1085
1086 oldfunc = (h->type != STT_NOTYPE
1087 && bed->is_function_type (h->type));
1088
1089 /* When we try to create a default indirect symbol from the dynamic
1090 definition with the default version, we skip it if its type and
1091 the type of existing regular definition mismatch. We only do it
1092 if the existing regular definition won't be dynamic. */
1093 if (pold_alignment == NULL
1094 && !info->shared
1095 && !info->export_dynamic
1096 && !h->ref_dynamic
1097 && newdyn
1098 && newdef
1099 && !olddyn
1100 && (olddef || h->root.type == bfd_link_hash_common)
1101 && ELF_ST_TYPE (sym->st_info) != h->type
1102 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1103 && h->type != STT_NOTYPE
1104 && !(newfunc && oldfunc))
1105 {
1106 *skip = TRUE;
1107 return TRUE;
1108 }
1109
1110 /* Plugin symbol type isn't currently set. Stop bogus errors. */
1111 if (oldbfd != NULL && (oldbfd->flags & BFD_PLUGIN) != 0)
1112 *type_change_ok = TRUE;
1113
1114 /* Check TLS symbol. We don't check undefined symbol introduced by
1115 "ld -u". */
1116 else if (oldbfd != NULL
1117 && ELF_ST_TYPE (sym->st_info) != h->type
1118 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1119 {
1120 bfd *ntbfd, *tbfd;
1121 bfd_boolean ntdef, tdef;
1122 asection *ntsec, *tsec;
1123
1124 if (h->type == STT_TLS)
1125 {
1126 ntbfd = abfd;
1127 ntsec = sec;
1128 ntdef = newdef;
1129 tbfd = oldbfd;
1130 tsec = oldsec;
1131 tdef = olddef;
1132 }
1133 else
1134 {
1135 ntbfd = oldbfd;
1136 ntsec = oldsec;
1137 ntdef = olddef;
1138 tbfd = abfd;
1139 tsec = sec;
1140 tdef = newdef;
1141 }
1142
1143 if (tdef && ntdef)
1144 (*_bfd_error_handler)
1145 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
1146 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1147 else if (!tdef && !ntdef)
1148 (*_bfd_error_handler)
1149 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
1150 tbfd, ntbfd, h->root.root.string);
1151 else if (tdef)
1152 (*_bfd_error_handler)
1153 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
1154 tbfd, tsec, ntbfd, h->root.root.string);
1155 else
1156 (*_bfd_error_handler)
1157 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
1158 tbfd, ntbfd, ntsec, h->root.root.string);
1159
1160 bfd_set_error (bfd_error_bad_value);
1161 return FALSE;
1162 }
1163
1164 /* We need to remember if a symbol has a definition in a dynamic
1165 object or is weak in all dynamic objects. Internal and hidden
1166 visibility will make it unavailable to dynamic objects. */
1167 if (newdyn)
1168 {
1169 _bfd_elf_mark_dynamic_def_weak (h, sec, bind);
1170 if (h != hi)
1171 _bfd_elf_mark_dynamic_def_weak (hi, sec, bind);
1172 }
1173
1174 /* If the old symbol has non-default visibility, we ignore the new
1175 definition from a dynamic object. */
1176 if (newdyn
1177 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1178 && !bfd_is_und_section (sec))
1179 {
1180 *skip = TRUE;
1181 /* Make sure this symbol is dynamic. */
1182 h->ref_dynamic = 1;
1183 hi->ref_dynamic = 1;
1184 /* A protected symbol has external availability. Make sure it is
1185 recorded as dynamic.
1186
1187 FIXME: Should we check type and size for protected symbol? */
1188 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1189 return bfd_elf_link_record_dynamic_symbol (info, h);
1190 else
1191 return TRUE;
1192 }
1193 else if (!newdyn
1194 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1195 && h->def_dynamic)
1196 {
1197 /* If the new symbol with non-default visibility comes from a
1198 relocatable file and the old definition comes from a dynamic
1199 object, we remove the old definition. */
1200 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1201 {
1202 /* Handle the case where the old dynamic definition is
1203 default versioned. We need to copy the symbol info from
1204 the symbol with default version to the normal one if it
1205 was referenced before. */
1206 if (h->ref_regular)
1207 {
1208 struct elf_link_hash_entry *vh = *sym_hash;
1209
1210 vh->root.type = h->root.type;
1211 h->root.type = bfd_link_hash_indirect;
1212 (*bed->elf_backend_copy_indirect_symbol) (info, vh, h);
1213 /* Protected symbols will override the dynamic definition
1214 with default version. */
1215 if (ELF_ST_VISIBILITY (sym->st_other) == STV_PROTECTED)
1216 {
1217 h->root.u.i.link = (struct bfd_link_hash_entry *) vh;
1218 vh->dynamic_def = 1;
1219 vh->ref_dynamic = 1;
1220 }
1221 else
1222 {
1223 h->root.type = vh->root.type;
1224 vh->ref_dynamic = 0;
1225 /* We have to hide it here since it was made dynamic
1226 global with extra bits when the symbol info was
1227 copied from the old dynamic definition. */
1228 (*bed->elf_backend_hide_symbol) (info, vh, TRUE);
1229 }
1230 h = vh;
1231 }
1232 else
1233 h = *sym_hash;
1234 }
1235
1236 /* If the old symbol was undefined before, then it will still be
1237 on the undefs list. If the new symbol is undefined or
1238 common, we can't make it bfd_link_hash_new here, because new
1239 undefined or common symbols will be added to the undefs list
1240 by _bfd_generic_link_add_one_symbol. Symbols may not be
1241 added twice to the undefs list. Also, if the new symbol is
1242 undefweak then we don't want to lose the strong undef. */
1243 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1244 {
1245 h->root.type = bfd_link_hash_undefined;
1246 h->root.u.undef.abfd = abfd;
1247 }
1248 else
1249 {
1250 h->root.type = bfd_link_hash_new;
1251 h->root.u.undef.abfd = NULL;
1252 }
1253
1254 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1255 {
1256 /* If the new symbol is hidden or internal, completely undo
1257 any dynamic link state. */
1258 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1259 h->forced_local = 0;
1260 h->ref_dynamic = 0;
1261 }
1262 else
1263 h->ref_dynamic = 1;
1264 h->def_dynamic = 0;
1265 h->dynamic_def = 0;
1266 /* FIXME: Should we check type and size for protected symbol? */
1267 h->size = 0;
1268 h->type = 0;
1269 return TRUE;
1270 }
1271
1272 if (bind == STB_GNU_UNIQUE)
1273 h->unique_global = 1;
1274
1275 /* If a new weak symbol definition comes from a regular file and the
1276 old symbol comes from a dynamic library, we treat the new one as
1277 strong. Similarly, an old weak symbol definition from a regular
1278 file is treated as strong when the new symbol comes from a dynamic
1279 library. Further, an old weak symbol from a dynamic library is
1280 treated as strong if the new symbol is from a dynamic library.
1281 This reflects the way glibc's ld.so works.
1282
1283 Do this before setting *type_change_ok or *size_change_ok so that
1284 we warn properly when dynamic library symbols are overridden. */
1285
1286 if (newdef && !newdyn && olddyn)
1287 newweak = FALSE;
1288 if (olddef && newdyn)
1289 oldweak = FALSE;
1290
1291 /* Allow changes between different types of function symbol. */
1292 if (newfunc && oldfunc)
1293 *type_change_ok = TRUE;
1294
1295 /* It's OK to change the type if either the existing symbol or the
1296 new symbol is weak. A type change is also OK if the old symbol
1297 is undefined and the new symbol is defined. */
1298
1299 if (oldweak
1300 || newweak
1301 || (newdef
1302 && h->root.type == bfd_link_hash_undefined))
1303 *type_change_ok = TRUE;
1304
1305 /* It's OK to change the size if either the existing symbol or the
1306 new symbol is weak, or if the old symbol is undefined. */
1307
1308 if (*type_change_ok
1309 || h->root.type == bfd_link_hash_undefined)
1310 *size_change_ok = TRUE;
1311
1312 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1313 symbol, respectively, appears to be a common symbol in a dynamic
1314 object. If a symbol appears in an uninitialized section, and is
1315 not weak, and is not a function, then it may be a common symbol
1316 which was resolved when the dynamic object was created. We want
1317 to treat such symbols specially, because they raise special
1318 considerations when setting the symbol size: if the symbol
1319 appears as a common symbol in a regular object, and the size in
1320 the regular object is larger, we must make sure that we use the
1321 larger size. This problematic case can always be avoided in C,
1322 but it must be handled correctly when using Fortran shared
1323 libraries.
1324
1325 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1326 likewise for OLDDYNCOMMON and OLDDEF.
1327
1328 Note that this test is just a heuristic, and that it is quite
1329 possible to have an uninitialized symbol in a shared object which
1330 is really a definition, rather than a common symbol. This could
1331 lead to some minor confusion when the symbol really is a common
1332 symbol in some regular object. However, I think it will be
1333 harmless. */
1334
1335 if (newdyn
1336 && newdef
1337 && !newweak
1338 && (sec->flags & SEC_ALLOC) != 0
1339 && (sec->flags & SEC_LOAD) == 0
1340 && sym->st_size > 0
1341 && !newfunc)
1342 newdyncommon = TRUE;
1343 else
1344 newdyncommon = FALSE;
1345
1346 if (olddyn
1347 && olddef
1348 && h->root.type == bfd_link_hash_defined
1349 && h->def_dynamic
1350 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1351 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1352 && h->size > 0
1353 && !oldfunc)
1354 olddyncommon = TRUE;
1355 else
1356 olddyncommon = FALSE;
1357
1358 /* We now know everything about the old and new symbols. We ask the
1359 backend to check if we can merge them. */
1360 if (bed->merge_symbol
1361 && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue,
1362 pold_alignment, skip, override,
1363 type_change_ok, size_change_ok,
1364 &newdyn, &newdef, &newdyncommon, &newweak,
1365 abfd, &sec,
1366 &olddyn, &olddef, &olddyncommon, &oldweak,
1367 oldbfd, &oldsec))
1368 return FALSE;
1369
1370 /* If both the old and the new symbols look like common symbols in a
1371 dynamic object, set the size of the symbol to the larger of the
1372 two. */
1373
1374 if (olddyncommon
1375 && newdyncommon
1376 && sym->st_size != h->size)
1377 {
1378 /* Since we think we have two common symbols, issue a multiple
1379 common warning if desired. Note that we only warn if the
1380 size is different. If the size is the same, we simply let
1381 the old symbol override the new one as normally happens with
1382 symbols defined in dynamic objects. */
1383
1384 if (! ((*info->callbacks->multiple_common)
1385 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
1386 return FALSE;
1387
1388 if (sym->st_size > h->size)
1389 h->size = sym->st_size;
1390
1391 *size_change_ok = TRUE;
1392 }
1393
1394 /* If we are looking at a dynamic object, and we have found a
1395 definition, we need to see if the symbol was already defined by
1396 some other object. If so, we want to use the existing
1397 definition, and we do not want to report a multiple symbol
1398 definition error; we do this by clobbering *PSEC to be
1399 bfd_und_section_ptr.
1400
1401 We treat a common symbol as a definition if the symbol in the
1402 shared library is a function, since common symbols always
1403 represent variables; this can cause confusion in principle, but
1404 any such confusion would seem to indicate an erroneous program or
1405 shared library. We also permit a common symbol in a regular
1406 object to override a weak symbol in a shared object. */
1407
1408 if (newdyn
1409 && newdef
1410 && (olddef
1411 || (h->root.type == bfd_link_hash_common
1412 && (newweak || newfunc))))
1413 {
1414 *override = TRUE;
1415 newdef = FALSE;
1416 newdyncommon = FALSE;
1417
1418 *psec = sec = bfd_und_section_ptr;
1419 *size_change_ok = TRUE;
1420
1421 /* If we get here when the old symbol is a common symbol, then
1422 we are explicitly letting it override a weak symbol or
1423 function in a dynamic object, and we don't want to warn about
1424 a type change. If the old symbol is a defined symbol, a type
1425 change warning may still be appropriate. */
1426
1427 if (h->root.type == bfd_link_hash_common)
1428 *type_change_ok = TRUE;
1429 }
1430
1431 /* Handle the special case of an old common symbol merging with a
1432 new symbol which looks like a common symbol in a shared object.
1433 We change *PSEC and *PVALUE to make the new symbol look like a
1434 common symbol, and let _bfd_generic_link_add_one_symbol do the
1435 right thing. */
1436
1437 if (newdyncommon
1438 && h->root.type == bfd_link_hash_common)
1439 {
1440 *override = TRUE;
1441 newdef = FALSE;
1442 newdyncommon = FALSE;
1443 *pvalue = sym->st_size;
1444 *psec = sec = bed->common_section (oldsec);
1445 *size_change_ok = TRUE;
1446 }
1447
1448 /* Skip weak definitions of symbols that are already defined. */
1449 if (newdef && olddef && newweak)
1450 {
1451 /* Don't skip new non-IR weak syms. */
1452 if (!(oldbfd != NULL
1453 && (oldbfd->flags & BFD_PLUGIN) != 0
1454 && (abfd->flags & BFD_PLUGIN) == 0))
1455 *skip = TRUE;
1456
1457 /* Merge st_other. If the symbol already has a dynamic index,
1458 but visibility says it should not be visible, turn it into a
1459 local symbol. */
1460 elf_merge_st_other (abfd, h, sym, newdef, newdyn);
1461 if (h->dynindx != -1)
1462 switch (ELF_ST_VISIBILITY (h->other))
1463 {
1464 case STV_INTERNAL:
1465 case STV_HIDDEN:
1466 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1467 break;
1468 }
1469 }
1470
1471 /* If the old symbol is from a dynamic object, and the new symbol is
1472 a definition which is not from a dynamic object, then the new
1473 symbol overrides the old symbol. Symbols from regular files
1474 always take precedence over symbols from dynamic objects, even if
1475 they are defined after the dynamic object in the link.
1476
1477 As above, we again permit a common symbol in a regular object to
1478 override a definition in a shared object if the shared object
1479 symbol is a function or is weak. */
1480
1481 flip = NULL;
1482 if (!newdyn
1483 && (newdef
1484 || (bfd_is_com_section (sec)
1485 && (oldweak || oldfunc)))
1486 && olddyn
1487 && olddef
1488 && h->def_dynamic)
1489 {
1490 /* Change the hash table entry to undefined, and let
1491 _bfd_generic_link_add_one_symbol do the right thing with the
1492 new definition. */
1493
1494 h->root.type = bfd_link_hash_undefined;
1495 h->root.u.undef.abfd = h->root.u.def.section->owner;
1496 *size_change_ok = TRUE;
1497
1498 olddef = FALSE;
1499 olddyncommon = FALSE;
1500
1501 /* We again permit a type change when a common symbol may be
1502 overriding a function. */
1503
1504 if (bfd_is_com_section (sec))
1505 {
1506 if (oldfunc)
1507 {
1508 /* If a common symbol overrides a function, make sure
1509 that it isn't defined dynamically nor has type
1510 function. */
1511 h->def_dynamic = 0;
1512 h->type = STT_NOTYPE;
1513 }
1514 *type_change_ok = TRUE;
1515 }
1516
1517 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1518 flip = *sym_hash;
1519 else
1520 /* This union may have been set to be non-NULL when this symbol
1521 was seen in a dynamic object. We must force the union to be
1522 NULL, so that it is correct for a regular symbol. */
1523 h->verinfo.vertree = NULL;
1524 }
1525
1526 /* Handle the special case of a new common symbol merging with an
1527 old symbol that looks like it might be a common symbol defined in
1528 a shared object. Note that we have already handled the case in
1529 which a new common symbol should simply override the definition
1530 in the shared library. */
1531
1532 if (! newdyn
1533 && bfd_is_com_section (sec)
1534 && olddyncommon)
1535 {
1536 /* It would be best if we could set the hash table entry to a
1537 common symbol, but we don't know what to use for the section
1538 or the alignment. */
1539 if (! ((*info->callbacks->multiple_common)
1540 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
1541 return FALSE;
1542
1543 /* If the presumed common symbol in the dynamic object is
1544 larger, pretend that the new symbol has its size. */
1545
1546 if (h->size > *pvalue)
1547 *pvalue = h->size;
1548
1549 /* We need to remember the alignment required by the symbol
1550 in the dynamic object. */
1551 BFD_ASSERT (pold_alignment);
1552 *pold_alignment = h->root.u.def.section->alignment_power;
1553
1554 olddef = FALSE;
1555 olddyncommon = FALSE;
1556
1557 h->root.type = bfd_link_hash_undefined;
1558 h->root.u.undef.abfd = h->root.u.def.section->owner;
1559
1560 *size_change_ok = TRUE;
1561 *type_change_ok = TRUE;
1562
1563 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1564 flip = *sym_hash;
1565 else
1566 h->verinfo.vertree = NULL;
1567 }
1568
1569 if (flip != NULL)
1570 {
1571 /* Handle the case where we had a versioned symbol in a dynamic
1572 library and now find a definition in a normal object. In this
1573 case, we make the versioned symbol point to the normal one. */
1574 flip->root.type = h->root.type;
1575 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1576 h->root.type = bfd_link_hash_indirect;
1577 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1578 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1579 if (h->def_dynamic)
1580 {
1581 h->def_dynamic = 0;
1582 flip->ref_dynamic = 1;
1583 }
1584 }
1585
1586 return TRUE;
1587 }
1588
1589 /* This function is called to create an indirect symbol from the
1590 default for the symbol with the default version if needed. The
1591 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1592 set DYNSYM if the new indirect symbol is dynamic. */
1593
1594 static bfd_boolean
1595 _bfd_elf_add_default_symbol (bfd *abfd,
1596 struct bfd_link_info *info,
1597 struct elf_link_hash_entry *h,
1598 const char *name,
1599 Elf_Internal_Sym *sym,
1600 asection **psec,
1601 bfd_vma *value,
1602 bfd_boolean *dynsym,
1603 bfd_boolean override)
1604 {
1605 bfd_boolean type_change_ok;
1606 bfd_boolean size_change_ok;
1607 bfd_boolean skip;
1608 char *shortname;
1609 struct elf_link_hash_entry *hi;
1610 struct bfd_link_hash_entry *bh;
1611 const struct elf_backend_data *bed;
1612 bfd_boolean collect;
1613 bfd_boolean dynamic;
1614 char *p;
1615 size_t len, shortlen;
1616 asection *sec;
1617
1618 /* If this symbol has a version, and it is the default version, we
1619 create an indirect symbol from the default name to the fully
1620 decorated name. This will cause external references which do not
1621 specify a version to be bound to this version of the symbol. */
1622 p = strchr (name, ELF_VER_CHR);
1623 if (p == NULL || p[1] != ELF_VER_CHR)
1624 return TRUE;
1625
1626 if (override)
1627 {
1628 /* We are overridden by an old definition. We need to check if we
1629 need to create the indirect symbol from the default name. */
1630 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1631 FALSE, FALSE);
1632 BFD_ASSERT (hi != NULL);
1633 if (hi == h)
1634 return TRUE;
1635 while (hi->root.type == bfd_link_hash_indirect
1636 || hi->root.type == bfd_link_hash_warning)
1637 {
1638 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1639 if (hi == h)
1640 return TRUE;
1641 }
1642 }
1643
1644 bed = get_elf_backend_data (abfd);
1645 collect = bed->collect;
1646 dynamic = (abfd->flags & DYNAMIC) != 0;
1647
1648 shortlen = p - name;
1649 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1650 if (shortname == NULL)
1651 return FALSE;
1652 memcpy (shortname, name, shortlen);
1653 shortname[shortlen] = '\0';
1654
1655 /* We are going to create a new symbol. Merge it with any existing
1656 symbol with this name. For the purposes of the merge, act as
1657 though we were defining the symbol we just defined, although we
1658 actually going to define an indirect symbol. */
1659 type_change_ok = FALSE;
1660 size_change_ok = FALSE;
1661 sec = *psec;
1662 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1663 NULL, &hi, &skip, &override,
1664 &type_change_ok, &size_change_ok))
1665 return FALSE;
1666
1667 if (skip)
1668 goto nondefault;
1669
1670 if (! override)
1671 {
1672 bh = &hi->root;
1673 if (! (_bfd_generic_link_add_one_symbol
1674 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1675 0, name, FALSE, collect, &bh)))
1676 return FALSE;
1677 hi = (struct elf_link_hash_entry *) bh;
1678 }
1679 else
1680 {
1681 /* In this case the symbol named SHORTNAME is overriding the
1682 indirect symbol we want to add. We were planning on making
1683 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1684 is the name without a version. NAME is the fully versioned
1685 name, and it is the default version.
1686
1687 Overriding means that we already saw a definition for the
1688 symbol SHORTNAME in a regular object, and it is overriding
1689 the symbol defined in the dynamic object.
1690
1691 When this happens, we actually want to change NAME, the
1692 symbol we just added, to refer to SHORTNAME. This will cause
1693 references to NAME in the shared object to become references
1694 to SHORTNAME in the regular object. This is what we expect
1695 when we override a function in a shared object: that the
1696 references in the shared object will be mapped to the
1697 definition in the regular object. */
1698
1699 while (hi->root.type == bfd_link_hash_indirect
1700 || hi->root.type == bfd_link_hash_warning)
1701 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1702
1703 h->root.type = bfd_link_hash_indirect;
1704 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1705 if (h->def_dynamic)
1706 {
1707 h->def_dynamic = 0;
1708 hi->ref_dynamic = 1;
1709 if (hi->ref_regular
1710 || hi->def_regular)
1711 {
1712 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1713 return FALSE;
1714 }
1715 }
1716
1717 /* Now set HI to H, so that the following code will set the
1718 other fields correctly. */
1719 hi = h;
1720 }
1721
1722 /* Check if HI is a warning symbol. */
1723 if (hi->root.type == bfd_link_hash_warning)
1724 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1725
1726 /* If there is a duplicate definition somewhere, then HI may not
1727 point to an indirect symbol. We will have reported an error to
1728 the user in that case. */
1729
1730 if (hi->root.type == bfd_link_hash_indirect)
1731 {
1732 struct elf_link_hash_entry *ht;
1733
1734 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1735 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1736
1737 /* See if the new flags lead us to realize that the symbol must
1738 be dynamic. */
1739 if (! *dynsym)
1740 {
1741 if (! dynamic)
1742 {
1743 if (! info->executable
1744 || hi->def_dynamic
1745 || hi->ref_dynamic)
1746 *dynsym = TRUE;
1747 }
1748 else
1749 {
1750 if (hi->ref_regular)
1751 *dynsym = TRUE;
1752 }
1753 }
1754 }
1755
1756 /* We also need to define an indirection from the nondefault version
1757 of the symbol. */
1758
1759 nondefault:
1760 len = strlen (name);
1761 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
1762 if (shortname == NULL)
1763 return FALSE;
1764 memcpy (shortname, name, shortlen);
1765 memcpy (shortname + shortlen, p + 1, len - shortlen);
1766
1767 /* Once again, merge with any existing symbol. */
1768 type_change_ok = FALSE;
1769 size_change_ok = FALSE;
1770 sec = *psec;
1771 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1772 NULL, &hi, &skip, &override,
1773 &type_change_ok, &size_change_ok))
1774 return FALSE;
1775
1776 if (skip)
1777 return TRUE;
1778
1779 if (override)
1780 {
1781 /* Here SHORTNAME is a versioned name, so we don't expect to see
1782 the type of override we do in the case above unless it is
1783 overridden by a versioned definition. */
1784 if (hi->root.type != bfd_link_hash_defined
1785 && hi->root.type != bfd_link_hash_defweak)
1786 (*_bfd_error_handler)
1787 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1788 abfd, shortname);
1789 }
1790 else
1791 {
1792 bh = &hi->root;
1793 if (! (_bfd_generic_link_add_one_symbol
1794 (info, abfd, shortname, BSF_INDIRECT,
1795 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1796 return FALSE;
1797 hi = (struct elf_link_hash_entry *) bh;
1798
1799 /* If there is a duplicate definition somewhere, then HI may not
1800 point to an indirect symbol. We will have reported an error
1801 to the user in that case. */
1802
1803 if (hi->root.type == bfd_link_hash_indirect)
1804 {
1805 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1806
1807 /* See if the new flags lead us to realize that the symbol
1808 must be dynamic. */
1809 if (! *dynsym)
1810 {
1811 if (! dynamic)
1812 {
1813 if (! info->executable
1814 || hi->ref_dynamic)
1815 *dynsym = TRUE;
1816 }
1817 else
1818 {
1819 if (hi->ref_regular)
1820 *dynsym = TRUE;
1821 }
1822 }
1823 }
1824 }
1825
1826 return TRUE;
1827 }
1828 \f
1829 /* This routine is used to export all defined symbols into the dynamic
1830 symbol table. It is called via elf_link_hash_traverse. */
1831
1832 static bfd_boolean
1833 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1834 {
1835 struct elf_info_failed *eif = (struct elf_info_failed *) data;
1836
1837 /* Ignore indirect symbols. These are added by the versioning code. */
1838 if (h->root.type == bfd_link_hash_indirect)
1839 return TRUE;
1840
1841 /* Ignore this if we won't export it. */
1842 if (!eif->info->export_dynamic && !h->dynamic)
1843 return TRUE;
1844
1845 if (h->dynindx == -1
1846 && (h->def_regular || h->ref_regular)
1847 && ! bfd_hide_sym_by_version (eif->info->version_info,
1848 h->root.root.string))
1849 {
1850 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1851 {
1852 eif->failed = TRUE;
1853 return FALSE;
1854 }
1855 }
1856
1857 return TRUE;
1858 }
1859 \f
1860 /* Look through the symbols which are defined in other shared
1861 libraries and referenced here. Update the list of version
1862 dependencies. This will be put into the .gnu.version_r section.
1863 This function is called via elf_link_hash_traverse. */
1864
1865 static bfd_boolean
1866 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1867 void *data)
1868 {
1869 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
1870 Elf_Internal_Verneed *t;
1871 Elf_Internal_Vernaux *a;
1872 bfd_size_type amt;
1873
1874 /* We only care about symbols defined in shared objects with version
1875 information. */
1876 if (!h->def_dynamic
1877 || h->def_regular
1878 || h->dynindx == -1
1879 || h->verinfo.verdef == NULL)
1880 return TRUE;
1881
1882 /* See if we already know about this version. */
1883 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
1884 t != NULL;
1885 t = t->vn_nextref)
1886 {
1887 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1888 continue;
1889
1890 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1891 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1892 return TRUE;
1893
1894 break;
1895 }
1896
1897 /* This is a new version. Add it to tree we are building. */
1898
1899 if (t == NULL)
1900 {
1901 amt = sizeof *t;
1902 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
1903 if (t == NULL)
1904 {
1905 rinfo->failed = TRUE;
1906 return FALSE;
1907 }
1908
1909 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1910 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
1911 elf_tdata (rinfo->info->output_bfd)->verref = t;
1912 }
1913
1914 amt = sizeof *a;
1915 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
1916 if (a == NULL)
1917 {
1918 rinfo->failed = TRUE;
1919 return FALSE;
1920 }
1921
1922 /* Note that we are copying a string pointer here, and testing it
1923 above. If bfd_elf_string_from_elf_section is ever changed to
1924 discard the string data when low in memory, this will have to be
1925 fixed. */
1926 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1927
1928 a->vna_flags = h->verinfo.verdef->vd_flags;
1929 a->vna_nextptr = t->vn_auxptr;
1930
1931 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1932 ++rinfo->vers;
1933
1934 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1935
1936 t->vn_auxptr = a;
1937
1938 return TRUE;
1939 }
1940
1941 /* Figure out appropriate versions for all the symbols. We may not
1942 have the version number script until we have read all of the input
1943 files, so until that point we don't know which symbols should be
1944 local. This function is called via elf_link_hash_traverse. */
1945
1946 static bfd_boolean
1947 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1948 {
1949 struct elf_info_failed *sinfo;
1950 struct bfd_link_info *info;
1951 const struct elf_backend_data *bed;
1952 struct elf_info_failed eif;
1953 char *p;
1954 bfd_size_type amt;
1955
1956 sinfo = (struct elf_info_failed *) data;
1957 info = sinfo->info;
1958
1959 /* Fix the symbol flags. */
1960 eif.failed = FALSE;
1961 eif.info = info;
1962 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1963 {
1964 if (eif.failed)
1965 sinfo->failed = TRUE;
1966 return FALSE;
1967 }
1968
1969 /* We only need version numbers for symbols defined in regular
1970 objects. */
1971 if (!h->def_regular)
1972 return TRUE;
1973
1974 bed = get_elf_backend_data (info->output_bfd);
1975 p = strchr (h->root.root.string, ELF_VER_CHR);
1976 if (p != NULL && h->verinfo.vertree == NULL)
1977 {
1978 struct bfd_elf_version_tree *t;
1979 bfd_boolean hidden;
1980
1981 hidden = TRUE;
1982
1983 /* There are two consecutive ELF_VER_CHR characters if this is
1984 not a hidden symbol. */
1985 ++p;
1986 if (*p == ELF_VER_CHR)
1987 {
1988 hidden = FALSE;
1989 ++p;
1990 }
1991
1992 /* If there is no version string, we can just return out. */
1993 if (*p == '\0')
1994 {
1995 if (hidden)
1996 h->hidden = 1;
1997 return TRUE;
1998 }
1999
2000 /* Look for the version. If we find it, it is no longer weak. */
2001 for (t = sinfo->info->version_info; t != NULL; t = t->next)
2002 {
2003 if (strcmp (t->name, p) == 0)
2004 {
2005 size_t len;
2006 char *alc;
2007 struct bfd_elf_version_expr *d;
2008
2009 len = p - h->root.root.string;
2010 alc = (char *) bfd_malloc (len);
2011 if (alc == NULL)
2012 {
2013 sinfo->failed = TRUE;
2014 return FALSE;
2015 }
2016 memcpy (alc, h->root.root.string, len - 1);
2017 alc[len - 1] = '\0';
2018 if (alc[len - 2] == ELF_VER_CHR)
2019 alc[len - 2] = '\0';
2020
2021 h->verinfo.vertree = t;
2022 t->used = TRUE;
2023 d = NULL;
2024
2025 if (t->globals.list != NULL)
2026 d = (*t->match) (&t->globals, NULL, alc);
2027
2028 /* See if there is anything to force this symbol to
2029 local scope. */
2030 if (d == NULL && t->locals.list != NULL)
2031 {
2032 d = (*t->match) (&t->locals, NULL, alc);
2033 if (d != NULL
2034 && h->dynindx != -1
2035 && ! info->export_dynamic)
2036 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2037 }
2038
2039 free (alc);
2040 break;
2041 }
2042 }
2043
2044 /* If we are building an application, we need to create a
2045 version node for this version. */
2046 if (t == NULL && info->executable)
2047 {
2048 struct bfd_elf_version_tree **pp;
2049 int version_index;
2050
2051 /* If we aren't going to export this symbol, we don't need
2052 to worry about it. */
2053 if (h->dynindx == -1)
2054 return TRUE;
2055
2056 amt = sizeof *t;
2057 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, amt);
2058 if (t == NULL)
2059 {
2060 sinfo->failed = TRUE;
2061 return FALSE;
2062 }
2063
2064 t->name = p;
2065 t->name_indx = (unsigned int) -1;
2066 t->used = TRUE;
2067
2068 version_index = 1;
2069 /* Don't count anonymous version tag. */
2070 if (sinfo->info->version_info != NULL
2071 && sinfo->info->version_info->vernum == 0)
2072 version_index = 0;
2073 for (pp = &sinfo->info->version_info;
2074 *pp != NULL;
2075 pp = &(*pp)->next)
2076 ++version_index;
2077 t->vernum = version_index;
2078
2079 *pp = t;
2080
2081 h->verinfo.vertree = t;
2082 }
2083 else if (t == NULL)
2084 {
2085 /* We could not find the version for a symbol when
2086 generating a shared archive. Return an error. */
2087 (*_bfd_error_handler)
2088 (_("%B: version node not found for symbol %s"),
2089 info->output_bfd, h->root.root.string);
2090 bfd_set_error (bfd_error_bad_value);
2091 sinfo->failed = TRUE;
2092 return FALSE;
2093 }
2094
2095 if (hidden)
2096 h->hidden = 1;
2097 }
2098
2099 /* If we don't have a version for this symbol, see if we can find
2100 something. */
2101 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
2102 {
2103 bfd_boolean hide;
2104
2105 h->verinfo.vertree
2106 = bfd_find_version_for_sym (sinfo->info->version_info,
2107 h->root.root.string, &hide);
2108 if (h->verinfo.vertree != NULL && hide)
2109 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2110 }
2111
2112 return TRUE;
2113 }
2114 \f
2115 /* Read and swap the relocs from the section indicated by SHDR. This
2116 may be either a REL or a RELA section. The relocations are
2117 translated into RELA relocations and stored in INTERNAL_RELOCS,
2118 which should have already been allocated to contain enough space.
2119 The EXTERNAL_RELOCS are a buffer where the external form of the
2120 relocations should be stored.
2121
2122 Returns FALSE if something goes wrong. */
2123
2124 static bfd_boolean
2125 elf_link_read_relocs_from_section (bfd *abfd,
2126 asection *sec,
2127 Elf_Internal_Shdr *shdr,
2128 void *external_relocs,
2129 Elf_Internal_Rela *internal_relocs)
2130 {
2131 const struct elf_backend_data *bed;
2132 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2133 const bfd_byte *erela;
2134 const bfd_byte *erelaend;
2135 Elf_Internal_Rela *irela;
2136 Elf_Internal_Shdr *symtab_hdr;
2137 size_t nsyms;
2138
2139 /* Position ourselves at the start of the section. */
2140 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2141 return FALSE;
2142
2143 /* Read the relocations. */
2144 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2145 return FALSE;
2146
2147 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2148 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2149
2150 bed = get_elf_backend_data (abfd);
2151
2152 /* Convert the external relocations to the internal format. */
2153 if (shdr->sh_entsize == bed->s->sizeof_rel)
2154 swap_in = bed->s->swap_reloc_in;
2155 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2156 swap_in = bed->s->swap_reloca_in;
2157 else
2158 {
2159 bfd_set_error (bfd_error_wrong_format);
2160 return FALSE;
2161 }
2162
2163 erela = (const bfd_byte *) external_relocs;
2164 erelaend = erela + shdr->sh_size;
2165 irela = internal_relocs;
2166 while (erela < erelaend)
2167 {
2168 bfd_vma r_symndx;
2169
2170 (*swap_in) (abfd, erela, irela);
2171 r_symndx = ELF32_R_SYM (irela->r_info);
2172 if (bed->s->arch_size == 64)
2173 r_symndx >>= 24;
2174 if (nsyms > 0)
2175 {
2176 if ((size_t) r_symndx >= nsyms)
2177 {
2178 (*_bfd_error_handler)
2179 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2180 " for offset 0x%lx in section `%A'"),
2181 abfd, sec,
2182 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2183 bfd_set_error (bfd_error_bad_value);
2184 return FALSE;
2185 }
2186 }
2187 else if (r_symndx != STN_UNDEF)
2188 {
2189 (*_bfd_error_handler)
2190 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2191 " when the object file has no symbol table"),
2192 abfd, sec,
2193 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2194 bfd_set_error (bfd_error_bad_value);
2195 return FALSE;
2196 }
2197 irela += bed->s->int_rels_per_ext_rel;
2198 erela += shdr->sh_entsize;
2199 }
2200
2201 return TRUE;
2202 }
2203
2204 /* Read and swap the relocs for a section O. They may have been
2205 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2206 not NULL, they are used as buffers to read into. They are known to
2207 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2208 the return value is allocated using either malloc or bfd_alloc,
2209 according to the KEEP_MEMORY argument. If O has two relocation
2210 sections (both REL and RELA relocations), then the REL_HDR
2211 relocations will appear first in INTERNAL_RELOCS, followed by the
2212 RELA_HDR relocations. */
2213
2214 Elf_Internal_Rela *
2215 _bfd_elf_link_read_relocs (bfd *abfd,
2216 asection *o,
2217 void *external_relocs,
2218 Elf_Internal_Rela *internal_relocs,
2219 bfd_boolean keep_memory)
2220 {
2221 void *alloc1 = NULL;
2222 Elf_Internal_Rela *alloc2 = NULL;
2223 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2224 struct bfd_elf_section_data *esdo = elf_section_data (o);
2225 Elf_Internal_Rela *internal_rela_relocs;
2226
2227 if (esdo->relocs != NULL)
2228 return esdo->relocs;
2229
2230 if (o->reloc_count == 0)
2231 return NULL;
2232
2233 if (internal_relocs == NULL)
2234 {
2235 bfd_size_type size;
2236
2237 size = o->reloc_count;
2238 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2239 if (keep_memory)
2240 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2241 else
2242 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2243 if (internal_relocs == NULL)
2244 goto error_return;
2245 }
2246
2247 if (external_relocs == NULL)
2248 {
2249 bfd_size_type size = 0;
2250
2251 if (esdo->rel.hdr)
2252 size += esdo->rel.hdr->sh_size;
2253 if (esdo->rela.hdr)
2254 size += esdo->rela.hdr->sh_size;
2255
2256 alloc1 = bfd_malloc (size);
2257 if (alloc1 == NULL)
2258 goto error_return;
2259 external_relocs = alloc1;
2260 }
2261
2262 internal_rela_relocs = internal_relocs;
2263 if (esdo->rel.hdr)
2264 {
2265 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2266 external_relocs,
2267 internal_relocs))
2268 goto error_return;
2269 external_relocs = (((bfd_byte *) external_relocs)
2270 + esdo->rel.hdr->sh_size);
2271 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2272 * bed->s->int_rels_per_ext_rel);
2273 }
2274
2275 if (esdo->rela.hdr
2276 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2277 external_relocs,
2278 internal_rela_relocs)))
2279 goto error_return;
2280
2281 /* Cache the results for next time, if we can. */
2282 if (keep_memory)
2283 esdo->relocs = internal_relocs;
2284
2285 if (alloc1 != NULL)
2286 free (alloc1);
2287
2288 /* Don't free alloc2, since if it was allocated we are passing it
2289 back (under the name of internal_relocs). */
2290
2291 return internal_relocs;
2292
2293 error_return:
2294 if (alloc1 != NULL)
2295 free (alloc1);
2296 if (alloc2 != NULL)
2297 {
2298 if (keep_memory)
2299 bfd_release (abfd, alloc2);
2300 else
2301 free (alloc2);
2302 }
2303 return NULL;
2304 }
2305
2306 /* Compute the size of, and allocate space for, REL_HDR which is the
2307 section header for a section containing relocations for O. */
2308
2309 static bfd_boolean
2310 _bfd_elf_link_size_reloc_section (bfd *abfd,
2311 struct bfd_elf_section_reloc_data *reldata)
2312 {
2313 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2314
2315 /* That allows us to calculate the size of the section. */
2316 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2317
2318 /* The contents field must last into write_object_contents, so we
2319 allocate it with bfd_alloc rather than malloc. Also since we
2320 cannot be sure that the contents will actually be filled in,
2321 we zero the allocated space. */
2322 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2323 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2324 return FALSE;
2325
2326 if (reldata->hashes == NULL && reldata->count)
2327 {
2328 struct elf_link_hash_entry **p;
2329
2330 p = (struct elf_link_hash_entry **)
2331 bfd_zmalloc (reldata->count * sizeof (struct elf_link_hash_entry *));
2332 if (p == NULL)
2333 return FALSE;
2334
2335 reldata->hashes = p;
2336 }
2337
2338 return TRUE;
2339 }
2340
2341 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2342 originated from the section given by INPUT_REL_HDR) to the
2343 OUTPUT_BFD. */
2344
2345 bfd_boolean
2346 _bfd_elf_link_output_relocs (bfd *output_bfd,
2347 asection *input_section,
2348 Elf_Internal_Shdr *input_rel_hdr,
2349 Elf_Internal_Rela *internal_relocs,
2350 struct elf_link_hash_entry **rel_hash
2351 ATTRIBUTE_UNUSED)
2352 {
2353 Elf_Internal_Rela *irela;
2354 Elf_Internal_Rela *irelaend;
2355 bfd_byte *erel;
2356 struct bfd_elf_section_reloc_data *output_reldata;
2357 asection *output_section;
2358 const struct elf_backend_data *bed;
2359 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2360 struct bfd_elf_section_data *esdo;
2361
2362 output_section = input_section->output_section;
2363
2364 bed = get_elf_backend_data (output_bfd);
2365 esdo = elf_section_data (output_section);
2366 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2367 {
2368 output_reldata = &esdo->rel;
2369 swap_out = bed->s->swap_reloc_out;
2370 }
2371 else if (esdo->rela.hdr
2372 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2373 {
2374 output_reldata = &esdo->rela;
2375 swap_out = bed->s->swap_reloca_out;
2376 }
2377 else
2378 {
2379 (*_bfd_error_handler)
2380 (_("%B: relocation size mismatch in %B section %A"),
2381 output_bfd, input_section->owner, input_section);
2382 bfd_set_error (bfd_error_wrong_format);
2383 return FALSE;
2384 }
2385
2386 erel = output_reldata->hdr->contents;
2387 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2388 irela = internal_relocs;
2389 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2390 * bed->s->int_rels_per_ext_rel);
2391 while (irela < irelaend)
2392 {
2393 (*swap_out) (output_bfd, irela, erel);
2394 irela += bed->s->int_rels_per_ext_rel;
2395 erel += input_rel_hdr->sh_entsize;
2396 }
2397
2398 /* Bump the counter, so that we know where to add the next set of
2399 relocations. */
2400 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2401
2402 return TRUE;
2403 }
2404 \f
2405 /* Make weak undefined symbols in PIE dynamic. */
2406
2407 bfd_boolean
2408 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2409 struct elf_link_hash_entry *h)
2410 {
2411 if (info->pie
2412 && h->dynindx == -1
2413 && h->root.type == bfd_link_hash_undefweak)
2414 return bfd_elf_link_record_dynamic_symbol (info, h);
2415
2416 return TRUE;
2417 }
2418
2419 /* Fix up the flags for a symbol. This handles various cases which
2420 can only be fixed after all the input files are seen. This is
2421 currently called by both adjust_dynamic_symbol and
2422 assign_sym_version, which is unnecessary but perhaps more robust in
2423 the face of future changes. */
2424
2425 static bfd_boolean
2426 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2427 struct elf_info_failed *eif)
2428 {
2429 const struct elf_backend_data *bed;
2430
2431 /* If this symbol was mentioned in a non-ELF file, try to set
2432 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2433 permit a non-ELF file to correctly refer to a symbol defined in
2434 an ELF dynamic object. */
2435 if (h->non_elf)
2436 {
2437 while (h->root.type == bfd_link_hash_indirect)
2438 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2439
2440 if (h->root.type != bfd_link_hash_defined
2441 && h->root.type != bfd_link_hash_defweak)
2442 {
2443 h->ref_regular = 1;
2444 h->ref_regular_nonweak = 1;
2445 }
2446 else
2447 {
2448 if (h->root.u.def.section->owner != NULL
2449 && (bfd_get_flavour (h->root.u.def.section->owner)
2450 == bfd_target_elf_flavour))
2451 {
2452 h->ref_regular = 1;
2453 h->ref_regular_nonweak = 1;
2454 }
2455 else
2456 h->def_regular = 1;
2457 }
2458
2459 if (h->dynindx == -1
2460 && (h->def_dynamic
2461 || h->ref_dynamic))
2462 {
2463 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2464 {
2465 eif->failed = TRUE;
2466 return FALSE;
2467 }
2468 }
2469 }
2470 else
2471 {
2472 /* Unfortunately, NON_ELF is only correct if the symbol
2473 was first seen in a non-ELF file. Fortunately, if the symbol
2474 was first seen in an ELF file, we're probably OK unless the
2475 symbol was defined in a non-ELF file. Catch that case here.
2476 FIXME: We're still in trouble if the symbol was first seen in
2477 a dynamic object, and then later in a non-ELF regular object. */
2478 if ((h->root.type == bfd_link_hash_defined
2479 || h->root.type == bfd_link_hash_defweak)
2480 && !h->def_regular
2481 && (h->root.u.def.section->owner != NULL
2482 ? (bfd_get_flavour (h->root.u.def.section->owner)
2483 != bfd_target_elf_flavour)
2484 : (bfd_is_abs_section (h->root.u.def.section)
2485 && !h->def_dynamic)))
2486 h->def_regular = 1;
2487 }
2488
2489 /* Backend specific symbol fixup. */
2490 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2491 if (bed->elf_backend_fixup_symbol
2492 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2493 return FALSE;
2494
2495 /* If this is a final link, and the symbol was defined as a common
2496 symbol in a regular object file, and there was no definition in
2497 any dynamic object, then the linker will have allocated space for
2498 the symbol in a common section but the DEF_REGULAR
2499 flag will not have been set. */
2500 if (h->root.type == bfd_link_hash_defined
2501 && !h->def_regular
2502 && h->ref_regular
2503 && !h->def_dynamic
2504 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2505 h->def_regular = 1;
2506
2507 /* If -Bsymbolic was used (which means to bind references to global
2508 symbols to the definition within the shared object), and this
2509 symbol was defined in a regular object, then it actually doesn't
2510 need a PLT entry. Likewise, if the symbol has non-default
2511 visibility. If the symbol has hidden or internal visibility, we
2512 will force it local. */
2513 if (h->needs_plt
2514 && eif->info->shared
2515 && is_elf_hash_table (eif->info->hash)
2516 && (SYMBOLIC_BIND (eif->info, h)
2517 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2518 && h->def_regular)
2519 {
2520 bfd_boolean force_local;
2521
2522 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2523 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2524 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2525 }
2526
2527 /* If a weak undefined symbol has non-default visibility, we also
2528 hide it from the dynamic linker. */
2529 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2530 && h->root.type == bfd_link_hash_undefweak)
2531 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2532
2533 /* If this is a weak defined symbol in a dynamic object, and we know
2534 the real definition in the dynamic object, copy interesting flags
2535 over to the real definition. */
2536 if (h->u.weakdef != NULL)
2537 {
2538 /* If the real definition is defined by a regular object file,
2539 don't do anything special. See the longer description in
2540 _bfd_elf_adjust_dynamic_symbol, below. */
2541 if (h->u.weakdef->def_regular)
2542 h->u.weakdef = NULL;
2543 else
2544 {
2545 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2546
2547 while (h->root.type == bfd_link_hash_indirect)
2548 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2549
2550 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2551 || h->root.type == bfd_link_hash_defweak);
2552 BFD_ASSERT (weakdef->def_dynamic);
2553 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2554 || weakdef->root.type == bfd_link_hash_defweak);
2555 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2556 }
2557 }
2558
2559 return TRUE;
2560 }
2561
2562 /* Make the backend pick a good value for a dynamic symbol. This is
2563 called via elf_link_hash_traverse, and also calls itself
2564 recursively. */
2565
2566 static bfd_boolean
2567 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2568 {
2569 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2570 bfd *dynobj;
2571 const struct elf_backend_data *bed;
2572
2573 if (! is_elf_hash_table (eif->info->hash))
2574 return FALSE;
2575
2576 /* Ignore indirect symbols. These are added by the versioning code. */
2577 if (h->root.type == bfd_link_hash_indirect)
2578 return TRUE;
2579
2580 /* Fix the symbol flags. */
2581 if (! _bfd_elf_fix_symbol_flags (h, eif))
2582 return FALSE;
2583
2584 /* If this symbol does not require a PLT entry, and it is not
2585 defined by a dynamic object, or is not referenced by a regular
2586 object, ignore it. We do have to handle a weak defined symbol,
2587 even if no regular object refers to it, if we decided to add it
2588 to the dynamic symbol table. FIXME: Do we normally need to worry
2589 about symbols which are defined by one dynamic object and
2590 referenced by another one? */
2591 if (!h->needs_plt
2592 && h->type != STT_GNU_IFUNC
2593 && (h->def_regular
2594 || !h->def_dynamic
2595 || (!h->ref_regular
2596 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2597 {
2598 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2599 return TRUE;
2600 }
2601
2602 /* If we've already adjusted this symbol, don't do it again. This
2603 can happen via a recursive call. */
2604 if (h->dynamic_adjusted)
2605 return TRUE;
2606
2607 /* Don't look at this symbol again. Note that we must set this
2608 after checking the above conditions, because we may look at a
2609 symbol once, decide not to do anything, and then get called
2610 recursively later after REF_REGULAR is set below. */
2611 h->dynamic_adjusted = 1;
2612
2613 /* If this is a weak definition, and we know a real definition, and
2614 the real symbol is not itself defined by a regular object file,
2615 then get a good value for the real definition. We handle the
2616 real symbol first, for the convenience of the backend routine.
2617
2618 Note that there is a confusing case here. If the real definition
2619 is defined by a regular object file, we don't get the real symbol
2620 from the dynamic object, but we do get the weak symbol. If the
2621 processor backend uses a COPY reloc, then if some routine in the
2622 dynamic object changes the real symbol, we will not see that
2623 change in the corresponding weak symbol. This is the way other
2624 ELF linkers work as well, and seems to be a result of the shared
2625 library model.
2626
2627 I will clarify this issue. Most SVR4 shared libraries define the
2628 variable _timezone and define timezone as a weak synonym. The
2629 tzset call changes _timezone. If you write
2630 extern int timezone;
2631 int _timezone = 5;
2632 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2633 you might expect that, since timezone is a synonym for _timezone,
2634 the same number will print both times. However, if the processor
2635 backend uses a COPY reloc, then actually timezone will be copied
2636 into your process image, and, since you define _timezone
2637 yourself, _timezone will not. Thus timezone and _timezone will
2638 wind up at different memory locations. The tzset call will set
2639 _timezone, leaving timezone unchanged. */
2640
2641 if (h->u.weakdef != NULL)
2642 {
2643 /* If we get to this point, there is an implicit reference to
2644 H->U.WEAKDEF by a regular object file via the weak symbol H. */
2645 h->u.weakdef->ref_regular = 1;
2646
2647 /* Ensure that the backend adjust_dynamic_symbol function sees
2648 H->U.WEAKDEF before H by recursively calling ourselves. */
2649 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2650 return FALSE;
2651 }
2652
2653 /* If a symbol has no type and no size and does not require a PLT
2654 entry, then we are probably about to do the wrong thing here: we
2655 are probably going to create a COPY reloc for an empty object.
2656 This case can arise when a shared object is built with assembly
2657 code, and the assembly code fails to set the symbol type. */
2658 if (h->size == 0
2659 && h->type == STT_NOTYPE
2660 && !h->needs_plt)
2661 (*_bfd_error_handler)
2662 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2663 h->root.root.string);
2664
2665 dynobj = elf_hash_table (eif->info)->dynobj;
2666 bed = get_elf_backend_data (dynobj);
2667
2668 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2669 {
2670 eif->failed = TRUE;
2671 return FALSE;
2672 }
2673
2674 return TRUE;
2675 }
2676
2677 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2678 DYNBSS. */
2679
2680 bfd_boolean
2681 _bfd_elf_adjust_dynamic_copy (struct elf_link_hash_entry *h,
2682 asection *dynbss)
2683 {
2684 unsigned int power_of_two;
2685 bfd_vma mask;
2686 asection *sec = h->root.u.def.section;
2687
2688 /* The section aligment of definition is the maximum alignment
2689 requirement of symbols defined in the section. Since we don't
2690 know the symbol alignment requirement, we start with the
2691 maximum alignment and check low bits of the symbol address
2692 for the minimum alignment. */
2693 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2694 mask = ((bfd_vma) 1 << power_of_two) - 1;
2695 while ((h->root.u.def.value & mask) != 0)
2696 {
2697 mask >>= 1;
2698 --power_of_two;
2699 }
2700
2701 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2702 dynbss))
2703 {
2704 /* Adjust the section alignment if needed. */
2705 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2706 power_of_two))
2707 return FALSE;
2708 }
2709
2710 /* We make sure that the symbol will be aligned properly. */
2711 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2712
2713 /* Define the symbol as being at this point in DYNBSS. */
2714 h->root.u.def.section = dynbss;
2715 h->root.u.def.value = dynbss->size;
2716
2717 /* Increment the size of DYNBSS to make room for the symbol. */
2718 dynbss->size += h->size;
2719
2720 return TRUE;
2721 }
2722
2723 /* Adjust all external symbols pointing into SEC_MERGE sections
2724 to reflect the object merging within the sections. */
2725
2726 static bfd_boolean
2727 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2728 {
2729 asection *sec;
2730
2731 if ((h->root.type == bfd_link_hash_defined
2732 || h->root.type == bfd_link_hash_defweak)
2733 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2734 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
2735 {
2736 bfd *output_bfd = (bfd *) data;
2737
2738 h->root.u.def.value =
2739 _bfd_merged_section_offset (output_bfd,
2740 &h->root.u.def.section,
2741 elf_section_data (sec)->sec_info,
2742 h->root.u.def.value);
2743 }
2744
2745 return TRUE;
2746 }
2747
2748 /* Returns false if the symbol referred to by H should be considered
2749 to resolve local to the current module, and true if it should be
2750 considered to bind dynamically. */
2751
2752 bfd_boolean
2753 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2754 struct bfd_link_info *info,
2755 bfd_boolean not_local_protected)
2756 {
2757 bfd_boolean binding_stays_local_p;
2758 const struct elf_backend_data *bed;
2759 struct elf_link_hash_table *hash_table;
2760
2761 if (h == NULL)
2762 return FALSE;
2763
2764 while (h->root.type == bfd_link_hash_indirect
2765 || h->root.type == bfd_link_hash_warning)
2766 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2767
2768 /* If it was forced local, then clearly it's not dynamic. */
2769 if (h->dynindx == -1)
2770 return FALSE;
2771 if (h->forced_local)
2772 return FALSE;
2773
2774 /* Identify the cases where name binding rules say that a
2775 visible symbol resolves locally. */
2776 binding_stays_local_p = info->executable || SYMBOLIC_BIND (info, h);
2777
2778 switch (ELF_ST_VISIBILITY (h->other))
2779 {
2780 case STV_INTERNAL:
2781 case STV_HIDDEN:
2782 return FALSE;
2783
2784 case STV_PROTECTED:
2785 hash_table = elf_hash_table (info);
2786 if (!is_elf_hash_table (hash_table))
2787 return FALSE;
2788
2789 bed = get_elf_backend_data (hash_table->dynobj);
2790
2791 /* Proper resolution for function pointer equality may require
2792 that these symbols perhaps be resolved dynamically, even though
2793 we should be resolving them to the current module. */
2794 if (!not_local_protected || !bed->is_function_type (h->type))
2795 binding_stays_local_p = TRUE;
2796 break;
2797
2798 default:
2799 break;
2800 }
2801
2802 /* If it isn't defined locally, then clearly it's dynamic. */
2803 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2804 return TRUE;
2805
2806 /* Otherwise, the symbol is dynamic if binding rules don't tell
2807 us that it remains local. */
2808 return !binding_stays_local_p;
2809 }
2810
2811 /* Return true if the symbol referred to by H should be considered
2812 to resolve local to the current module, and false otherwise. Differs
2813 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2814 undefined symbols. The two functions are virtually identical except
2815 for the place where forced_local and dynindx == -1 are tested. If
2816 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2817 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2818 the symbol is local only for defined symbols.
2819 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2820 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2821 treatment of undefined weak symbols. For those that do not make
2822 undefined weak symbols dynamic, both functions may return false. */
2823
2824 bfd_boolean
2825 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2826 struct bfd_link_info *info,
2827 bfd_boolean local_protected)
2828 {
2829 const struct elf_backend_data *bed;
2830 struct elf_link_hash_table *hash_table;
2831
2832 /* If it's a local sym, of course we resolve locally. */
2833 if (h == NULL)
2834 return TRUE;
2835
2836 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2837 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2838 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2839 return TRUE;
2840
2841 /* Common symbols that become definitions don't get the DEF_REGULAR
2842 flag set, so test it first, and don't bail out. */
2843 if (ELF_COMMON_DEF_P (h))
2844 /* Do nothing. */;
2845 /* If we don't have a definition in a regular file, then we can't
2846 resolve locally. The sym is either undefined or dynamic. */
2847 else if (!h->def_regular)
2848 return FALSE;
2849
2850 /* Forced local symbols resolve locally. */
2851 if (h->forced_local)
2852 return TRUE;
2853
2854 /* As do non-dynamic symbols. */
2855 if (h->dynindx == -1)
2856 return TRUE;
2857
2858 /* At this point, we know the symbol is defined and dynamic. In an
2859 executable it must resolve locally, likewise when building symbolic
2860 shared libraries. */
2861 if (info->executable || SYMBOLIC_BIND (info, h))
2862 return TRUE;
2863
2864 /* Now deal with defined dynamic symbols in shared libraries. Ones
2865 with default visibility might not resolve locally. */
2866 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2867 return FALSE;
2868
2869 hash_table = elf_hash_table (info);
2870 if (!is_elf_hash_table (hash_table))
2871 return TRUE;
2872
2873 bed = get_elf_backend_data (hash_table->dynobj);
2874
2875 /* STV_PROTECTED non-function symbols are local. */
2876 if (!bed->is_function_type (h->type))
2877 return TRUE;
2878
2879 /* Function pointer equality tests may require that STV_PROTECTED
2880 symbols be treated as dynamic symbols. If the address of a
2881 function not defined in an executable is set to that function's
2882 plt entry in the executable, then the address of the function in
2883 a shared library must also be the plt entry in the executable. */
2884 return local_protected;
2885 }
2886
2887 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2888 aligned. Returns the first TLS output section. */
2889
2890 struct bfd_section *
2891 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2892 {
2893 struct bfd_section *sec, *tls;
2894 unsigned int align = 0;
2895
2896 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2897 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2898 break;
2899 tls = sec;
2900
2901 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2902 if (sec->alignment_power > align)
2903 align = sec->alignment_power;
2904
2905 elf_hash_table (info)->tls_sec = tls;
2906
2907 /* Ensure the alignment of the first section is the largest alignment,
2908 so that the tls segment starts aligned. */
2909 if (tls != NULL)
2910 tls->alignment_power = align;
2911
2912 return tls;
2913 }
2914
2915 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2916 static bfd_boolean
2917 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2918 Elf_Internal_Sym *sym)
2919 {
2920 const struct elf_backend_data *bed;
2921
2922 /* Local symbols do not count, but target specific ones might. */
2923 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2924 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2925 return FALSE;
2926
2927 bed = get_elf_backend_data (abfd);
2928 /* Function symbols do not count. */
2929 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
2930 return FALSE;
2931
2932 /* If the section is undefined, then so is the symbol. */
2933 if (sym->st_shndx == SHN_UNDEF)
2934 return FALSE;
2935
2936 /* If the symbol is defined in the common section, then
2937 it is a common definition and so does not count. */
2938 if (bed->common_definition (sym))
2939 return FALSE;
2940
2941 /* If the symbol is in a target specific section then we
2942 must rely upon the backend to tell us what it is. */
2943 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2944 /* FIXME - this function is not coded yet:
2945
2946 return _bfd_is_global_symbol_definition (abfd, sym);
2947
2948 Instead for now assume that the definition is not global,
2949 Even if this is wrong, at least the linker will behave
2950 in the same way that it used to do. */
2951 return FALSE;
2952
2953 return TRUE;
2954 }
2955
2956 /* Search the symbol table of the archive element of the archive ABFD
2957 whose archive map contains a mention of SYMDEF, and determine if
2958 the symbol is defined in this element. */
2959 static bfd_boolean
2960 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2961 {
2962 Elf_Internal_Shdr * hdr;
2963 bfd_size_type symcount;
2964 bfd_size_type extsymcount;
2965 bfd_size_type extsymoff;
2966 Elf_Internal_Sym *isymbuf;
2967 Elf_Internal_Sym *isym;
2968 Elf_Internal_Sym *isymend;
2969 bfd_boolean result;
2970
2971 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2972 if (abfd == NULL)
2973 return FALSE;
2974
2975 if (! bfd_check_format (abfd, bfd_object))
2976 return FALSE;
2977
2978 /* If we have already included the element containing this symbol in the
2979 link then we do not need to include it again. Just claim that any symbol
2980 it contains is not a definition, so that our caller will not decide to
2981 (re)include this element. */
2982 if (abfd->archive_pass)
2983 return FALSE;
2984
2985 /* Select the appropriate symbol table. */
2986 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2987 hdr = &elf_tdata (abfd)->symtab_hdr;
2988 else
2989 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2990
2991 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2992
2993 /* The sh_info field of the symtab header tells us where the
2994 external symbols start. We don't care about the local symbols. */
2995 if (elf_bad_symtab (abfd))
2996 {
2997 extsymcount = symcount;
2998 extsymoff = 0;
2999 }
3000 else
3001 {
3002 extsymcount = symcount - hdr->sh_info;
3003 extsymoff = hdr->sh_info;
3004 }
3005
3006 if (extsymcount == 0)
3007 return FALSE;
3008
3009 /* Read in the symbol table. */
3010 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3011 NULL, NULL, NULL);
3012 if (isymbuf == NULL)
3013 return FALSE;
3014
3015 /* Scan the symbol table looking for SYMDEF. */
3016 result = FALSE;
3017 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3018 {
3019 const char *name;
3020
3021 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3022 isym->st_name);
3023 if (name == NULL)
3024 break;
3025
3026 if (strcmp (name, symdef->name) == 0)
3027 {
3028 result = is_global_data_symbol_definition (abfd, isym);
3029 break;
3030 }
3031 }
3032
3033 free (isymbuf);
3034
3035 return result;
3036 }
3037 \f
3038 /* Add an entry to the .dynamic table. */
3039
3040 bfd_boolean
3041 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3042 bfd_vma tag,
3043 bfd_vma val)
3044 {
3045 struct elf_link_hash_table *hash_table;
3046 const struct elf_backend_data *bed;
3047 asection *s;
3048 bfd_size_type newsize;
3049 bfd_byte *newcontents;
3050 Elf_Internal_Dyn dyn;
3051
3052 hash_table = elf_hash_table (info);
3053 if (! is_elf_hash_table (hash_table))
3054 return FALSE;
3055
3056 bed = get_elf_backend_data (hash_table->dynobj);
3057 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3058 BFD_ASSERT (s != NULL);
3059
3060 newsize = s->size + bed->s->sizeof_dyn;
3061 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3062 if (newcontents == NULL)
3063 return FALSE;
3064
3065 dyn.d_tag = tag;
3066 dyn.d_un.d_val = val;
3067 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3068
3069 s->size = newsize;
3070 s->contents = newcontents;
3071
3072 return TRUE;
3073 }
3074
3075 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3076 otherwise just check whether one already exists. Returns -1 on error,
3077 1 if a DT_NEEDED tag already exists, and 0 on success. */
3078
3079 static int
3080 elf_add_dt_needed_tag (bfd *abfd,
3081 struct bfd_link_info *info,
3082 const char *soname,
3083 bfd_boolean do_it)
3084 {
3085 struct elf_link_hash_table *hash_table;
3086 bfd_size_type oldsize;
3087 bfd_size_type strindex;
3088
3089 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3090 return -1;
3091
3092 hash_table = elf_hash_table (info);
3093 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
3094 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3095 if (strindex == (bfd_size_type) -1)
3096 return -1;
3097
3098 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
3099 {
3100 asection *sdyn;
3101 const struct elf_backend_data *bed;
3102 bfd_byte *extdyn;
3103
3104 bed = get_elf_backend_data (hash_table->dynobj);
3105 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3106 if (sdyn != NULL)
3107 for (extdyn = sdyn->contents;
3108 extdyn < sdyn->contents + sdyn->size;
3109 extdyn += bed->s->sizeof_dyn)
3110 {
3111 Elf_Internal_Dyn dyn;
3112
3113 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3114 if (dyn.d_tag == DT_NEEDED
3115 && dyn.d_un.d_val == strindex)
3116 {
3117 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3118 return 1;
3119 }
3120 }
3121 }
3122
3123 if (do_it)
3124 {
3125 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3126 return -1;
3127
3128 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3129 return -1;
3130 }
3131 else
3132 /* We were just checking for existence of the tag. */
3133 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3134
3135 return 0;
3136 }
3137
3138 static bfd_boolean
3139 on_needed_list (const char *soname, struct bfd_link_needed_list *needed)
3140 {
3141 for (; needed != NULL; needed = needed->next)
3142 if (strcmp (soname, needed->name) == 0)
3143 return TRUE;
3144
3145 return FALSE;
3146 }
3147
3148 /* Sort symbol by value and section. */
3149 static int
3150 elf_sort_symbol (const void *arg1, const void *arg2)
3151 {
3152 const struct elf_link_hash_entry *h1;
3153 const struct elf_link_hash_entry *h2;
3154 bfd_signed_vma vdiff;
3155
3156 h1 = *(const struct elf_link_hash_entry **) arg1;
3157 h2 = *(const struct elf_link_hash_entry **) arg2;
3158 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3159 if (vdiff != 0)
3160 return vdiff > 0 ? 1 : -1;
3161 else
3162 {
3163 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3164 if (sdiff != 0)
3165 return sdiff > 0 ? 1 : -1;
3166 }
3167 return 0;
3168 }
3169
3170 /* This function is used to adjust offsets into .dynstr for
3171 dynamic symbols. This is called via elf_link_hash_traverse. */
3172
3173 static bfd_boolean
3174 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3175 {
3176 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3177
3178 if (h->dynindx != -1)
3179 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3180 return TRUE;
3181 }
3182
3183 /* Assign string offsets in .dynstr, update all structures referencing
3184 them. */
3185
3186 static bfd_boolean
3187 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3188 {
3189 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3190 struct elf_link_local_dynamic_entry *entry;
3191 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3192 bfd *dynobj = hash_table->dynobj;
3193 asection *sdyn;
3194 bfd_size_type size;
3195 const struct elf_backend_data *bed;
3196 bfd_byte *extdyn;
3197
3198 _bfd_elf_strtab_finalize (dynstr);
3199 size = _bfd_elf_strtab_size (dynstr);
3200
3201 bed = get_elf_backend_data (dynobj);
3202 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3203 BFD_ASSERT (sdyn != NULL);
3204
3205 /* Update all .dynamic entries referencing .dynstr strings. */
3206 for (extdyn = sdyn->contents;
3207 extdyn < sdyn->contents + sdyn->size;
3208 extdyn += bed->s->sizeof_dyn)
3209 {
3210 Elf_Internal_Dyn dyn;
3211
3212 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3213 switch (dyn.d_tag)
3214 {
3215 case DT_STRSZ:
3216 dyn.d_un.d_val = size;
3217 break;
3218 case DT_NEEDED:
3219 case DT_SONAME:
3220 case DT_RPATH:
3221 case DT_RUNPATH:
3222 case DT_FILTER:
3223 case DT_AUXILIARY:
3224 case DT_AUDIT:
3225 case DT_DEPAUDIT:
3226 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3227 break;
3228 default:
3229 continue;
3230 }
3231 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3232 }
3233
3234 /* Now update local dynamic symbols. */
3235 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3236 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3237 entry->isym.st_name);
3238
3239 /* And the rest of dynamic symbols. */
3240 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3241
3242 /* Adjust version definitions. */
3243 if (elf_tdata (output_bfd)->cverdefs)
3244 {
3245 asection *s;
3246 bfd_byte *p;
3247 bfd_size_type i;
3248 Elf_Internal_Verdef def;
3249 Elf_Internal_Verdaux defaux;
3250
3251 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3252 p = s->contents;
3253 do
3254 {
3255 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3256 &def);
3257 p += sizeof (Elf_External_Verdef);
3258 if (def.vd_aux != sizeof (Elf_External_Verdef))
3259 continue;
3260 for (i = 0; i < def.vd_cnt; ++i)
3261 {
3262 _bfd_elf_swap_verdaux_in (output_bfd,
3263 (Elf_External_Verdaux *) p, &defaux);
3264 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3265 defaux.vda_name);
3266 _bfd_elf_swap_verdaux_out (output_bfd,
3267 &defaux, (Elf_External_Verdaux *) p);
3268 p += sizeof (Elf_External_Verdaux);
3269 }
3270 }
3271 while (def.vd_next);
3272 }
3273
3274 /* Adjust version references. */
3275 if (elf_tdata (output_bfd)->verref)
3276 {
3277 asection *s;
3278 bfd_byte *p;
3279 bfd_size_type i;
3280 Elf_Internal_Verneed need;
3281 Elf_Internal_Vernaux needaux;
3282
3283 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3284 p = s->contents;
3285 do
3286 {
3287 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3288 &need);
3289 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3290 _bfd_elf_swap_verneed_out (output_bfd, &need,
3291 (Elf_External_Verneed *) p);
3292 p += sizeof (Elf_External_Verneed);
3293 for (i = 0; i < need.vn_cnt; ++i)
3294 {
3295 _bfd_elf_swap_vernaux_in (output_bfd,
3296 (Elf_External_Vernaux *) p, &needaux);
3297 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3298 needaux.vna_name);
3299 _bfd_elf_swap_vernaux_out (output_bfd,
3300 &needaux,
3301 (Elf_External_Vernaux *) p);
3302 p += sizeof (Elf_External_Vernaux);
3303 }
3304 }
3305 while (need.vn_next);
3306 }
3307
3308 return TRUE;
3309 }
3310 \f
3311 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3312 The default is to only match when the INPUT and OUTPUT are exactly
3313 the same target. */
3314
3315 bfd_boolean
3316 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3317 const bfd_target *output)
3318 {
3319 return input == output;
3320 }
3321
3322 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3323 This version is used when different targets for the same architecture
3324 are virtually identical. */
3325
3326 bfd_boolean
3327 _bfd_elf_relocs_compatible (const bfd_target *input,
3328 const bfd_target *output)
3329 {
3330 const struct elf_backend_data *obed, *ibed;
3331
3332 if (input == output)
3333 return TRUE;
3334
3335 ibed = xvec_get_elf_backend_data (input);
3336 obed = xvec_get_elf_backend_data (output);
3337
3338 if (ibed->arch != obed->arch)
3339 return FALSE;
3340
3341 /* If both backends are using this function, deem them compatible. */
3342 return ibed->relocs_compatible == obed->relocs_compatible;
3343 }
3344
3345 /* Add symbols from an ELF object file to the linker hash table. */
3346
3347 static bfd_boolean
3348 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3349 {
3350 Elf_Internal_Ehdr *ehdr;
3351 Elf_Internal_Shdr *hdr;
3352 bfd_size_type symcount;
3353 bfd_size_type extsymcount;
3354 bfd_size_type extsymoff;
3355 struct elf_link_hash_entry **sym_hash;
3356 bfd_boolean dynamic;
3357 Elf_External_Versym *extversym = NULL;
3358 Elf_External_Versym *ever;
3359 struct elf_link_hash_entry *weaks;
3360 struct elf_link_hash_entry **nondeflt_vers = NULL;
3361 bfd_size_type nondeflt_vers_cnt = 0;
3362 Elf_Internal_Sym *isymbuf = NULL;
3363 Elf_Internal_Sym *isym;
3364 Elf_Internal_Sym *isymend;
3365 const struct elf_backend_data *bed;
3366 bfd_boolean add_needed;
3367 struct elf_link_hash_table *htab;
3368 bfd_size_type amt;
3369 void *alloc_mark = NULL;
3370 struct bfd_hash_entry **old_table = NULL;
3371 unsigned int old_size = 0;
3372 unsigned int old_count = 0;
3373 void *old_tab = NULL;
3374 void *old_hash;
3375 void *old_ent;
3376 struct bfd_link_hash_entry *old_undefs = NULL;
3377 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3378 long old_dynsymcount = 0;
3379 size_t tabsize = 0;
3380 size_t hashsize = 0;
3381
3382 htab = elf_hash_table (info);
3383 bed = get_elf_backend_data (abfd);
3384
3385 if ((abfd->flags & DYNAMIC) == 0)
3386 dynamic = FALSE;
3387 else
3388 {
3389 dynamic = TRUE;
3390
3391 /* You can't use -r against a dynamic object. Also, there's no
3392 hope of using a dynamic object which does not exactly match
3393 the format of the output file. */
3394 if (info->relocatable
3395 || !is_elf_hash_table (htab)
3396 || info->output_bfd->xvec != abfd->xvec)
3397 {
3398 if (info->relocatable)
3399 bfd_set_error (bfd_error_invalid_operation);
3400 else
3401 bfd_set_error (bfd_error_wrong_format);
3402 goto error_return;
3403 }
3404 }
3405
3406 ehdr = elf_elfheader (abfd);
3407 if (info->warn_alternate_em
3408 && bed->elf_machine_code != ehdr->e_machine
3409 && ((bed->elf_machine_alt1 != 0
3410 && ehdr->e_machine == bed->elf_machine_alt1)
3411 || (bed->elf_machine_alt2 != 0
3412 && ehdr->e_machine == bed->elf_machine_alt2)))
3413 info->callbacks->einfo
3414 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3415 ehdr->e_machine, abfd, bed->elf_machine_code);
3416
3417 /* As a GNU extension, any input sections which are named
3418 .gnu.warning.SYMBOL are treated as warning symbols for the given
3419 symbol. This differs from .gnu.warning sections, which generate
3420 warnings when they are included in an output file. */
3421 /* PR 12761: Also generate this warning when building shared libraries. */
3422 if (info->executable || info->shared)
3423 {
3424 asection *s;
3425
3426 for (s = abfd->sections; s != NULL; s = s->next)
3427 {
3428 const char *name;
3429
3430 name = bfd_get_section_name (abfd, s);
3431 if (CONST_STRNEQ (name, ".gnu.warning."))
3432 {
3433 char *msg;
3434 bfd_size_type sz;
3435
3436 name += sizeof ".gnu.warning." - 1;
3437
3438 /* If this is a shared object, then look up the symbol
3439 in the hash table. If it is there, and it is already
3440 been defined, then we will not be using the entry
3441 from this shared object, so we don't need to warn.
3442 FIXME: If we see the definition in a regular object
3443 later on, we will warn, but we shouldn't. The only
3444 fix is to keep track of what warnings we are supposed
3445 to emit, and then handle them all at the end of the
3446 link. */
3447 if (dynamic)
3448 {
3449 struct elf_link_hash_entry *h;
3450
3451 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3452
3453 /* FIXME: What about bfd_link_hash_common? */
3454 if (h != NULL
3455 && (h->root.type == bfd_link_hash_defined
3456 || h->root.type == bfd_link_hash_defweak))
3457 {
3458 /* We don't want to issue this warning. Clobber
3459 the section size so that the warning does not
3460 get copied into the output file. */
3461 s->size = 0;
3462 continue;
3463 }
3464 }
3465
3466 sz = s->size;
3467 msg = (char *) bfd_alloc (abfd, sz + 1);
3468 if (msg == NULL)
3469 goto error_return;
3470
3471 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3472 goto error_return;
3473
3474 msg[sz] = '\0';
3475
3476 if (! (_bfd_generic_link_add_one_symbol
3477 (info, abfd, name, BSF_WARNING, s, 0, msg,
3478 FALSE, bed->collect, NULL)))
3479 goto error_return;
3480
3481 if (! info->relocatable)
3482 {
3483 /* Clobber the section size so that the warning does
3484 not get copied into the output file. */
3485 s->size = 0;
3486
3487 /* Also set SEC_EXCLUDE, so that symbols defined in
3488 the warning section don't get copied to the output. */
3489 s->flags |= SEC_EXCLUDE;
3490 }
3491 }
3492 }
3493 }
3494
3495 add_needed = TRUE;
3496 if (! dynamic)
3497 {
3498 /* If we are creating a shared library, create all the dynamic
3499 sections immediately. We need to attach them to something,
3500 so we attach them to this BFD, provided it is the right
3501 format. FIXME: If there are no input BFD's of the same
3502 format as the output, we can't make a shared library. */
3503 if (info->shared
3504 && is_elf_hash_table (htab)
3505 && info->output_bfd->xvec == abfd->xvec
3506 && !htab->dynamic_sections_created)
3507 {
3508 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3509 goto error_return;
3510 }
3511 }
3512 else if (!is_elf_hash_table (htab))
3513 goto error_return;
3514 else
3515 {
3516 asection *s;
3517 const char *soname = NULL;
3518 char *audit = NULL;
3519 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3520 int ret;
3521
3522 /* ld --just-symbols and dynamic objects don't mix very well.
3523 ld shouldn't allow it. */
3524 if ((s = abfd->sections) != NULL
3525 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
3526 abort ();
3527
3528 /* If this dynamic lib was specified on the command line with
3529 --as-needed in effect, then we don't want to add a DT_NEEDED
3530 tag unless the lib is actually used. Similary for libs brought
3531 in by another lib's DT_NEEDED. When --no-add-needed is used
3532 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3533 any dynamic library in DT_NEEDED tags in the dynamic lib at
3534 all. */
3535 add_needed = (elf_dyn_lib_class (abfd)
3536 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3537 | DYN_NO_NEEDED)) == 0;
3538
3539 s = bfd_get_section_by_name (abfd, ".dynamic");
3540 if (s != NULL)
3541 {
3542 bfd_byte *dynbuf;
3543 bfd_byte *extdyn;
3544 unsigned int elfsec;
3545 unsigned long shlink;
3546
3547 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3548 {
3549 error_free_dyn:
3550 free (dynbuf);
3551 goto error_return;
3552 }
3553
3554 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3555 if (elfsec == SHN_BAD)
3556 goto error_free_dyn;
3557 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3558
3559 for (extdyn = dynbuf;
3560 extdyn < dynbuf + s->size;
3561 extdyn += bed->s->sizeof_dyn)
3562 {
3563 Elf_Internal_Dyn dyn;
3564
3565 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3566 if (dyn.d_tag == DT_SONAME)
3567 {
3568 unsigned int tagv = dyn.d_un.d_val;
3569 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3570 if (soname == NULL)
3571 goto error_free_dyn;
3572 }
3573 if (dyn.d_tag == DT_NEEDED)
3574 {
3575 struct bfd_link_needed_list *n, **pn;
3576 char *fnm, *anm;
3577 unsigned int tagv = dyn.d_un.d_val;
3578
3579 amt = sizeof (struct bfd_link_needed_list);
3580 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3581 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3582 if (n == NULL || fnm == NULL)
3583 goto error_free_dyn;
3584 amt = strlen (fnm) + 1;
3585 anm = (char *) bfd_alloc (abfd, amt);
3586 if (anm == NULL)
3587 goto error_free_dyn;
3588 memcpy (anm, fnm, amt);
3589 n->name = anm;
3590 n->by = abfd;
3591 n->next = NULL;
3592 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3593 ;
3594 *pn = n;
3595 }
3596 if (dyn.d_tag == DT_RUNPATH)
3597 {
3598 struct bfd_link_needed_list *n, **pn;
3599 char *fnm, *anm;
3600 unsigned int tagv = dyn.d_un.d_val;
3601
3602 amt = sizeof (struct bfd_link_needed_list);
3603 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3604 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3605 if (n == NULL || fnm == NULL)
3606 goto error_free_dyn;
3607 amt = strlen (fnm) + 1;
3608 anm = (char *) bfd_alloc (abfd, amt);
3609 if (anm == NULL)
3610 goto error_free_dyn;
3611 memcpy (anm, fnm, amt);
3612 n->name = anm;
3613 n->by = abfd;
3614 n->next = NULL;
3615 for (pn = & runpath;
3616 *pn != NULL;
3617 pn = &(*pn)->next)
3618 ;
3619 *pn = n;
3620 }
3621 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3622 if (!runpath && dyn.d_tag == DT_RPATH)
3623 {
3624 struct bfd_link_needed_list *n, **pn;
3625 char *fnm, *anm;
3626 unsigned int tagv = dyn.d_un.d_val;
3627
3628 amt = sizeof (struct bfd_link_needed_list);
3629 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3630 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3631 if (n == NULL || fnm == NULL)
3632 goto error_free_dyn;
3633 amt = strlen (fnm) + 1;
3634 anm = (char *) bfd_alloc (abfd, amt);
3635 if (anm == NULL)
3636 goto error_free_dyn;
3637 memcpy (anm, fnm, amt);
3638 n->name = anm;
3639 n->by = abfd;
3640 n->next = NULL;
3641 for (pn = & rpath;
3642 *pn != NULL;
3643 pn = &(*pn)->next)
3644 ;
3645 *pn = n;
3646 }
3647 if (dyn.d_tag == DT_AUDIT)
3648 {
3649 unsigned int tagv = dyn.d_un.d_val;
3650 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3651 }
3652 }
3653
3654 free (dynbuf);
3655 }
3656
3657 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3658 frees all more recently bfd_alloc'd blocks as well. */
3659 if (runpath)
3660 rpath = runpath;
3661
3662 if (rpath)
3663 {
3664 struct bfd_link_needed_list **pn;
3665 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3666 ;
3667 *pn = rpath;
3668 }
3669
3670 /* We do not want to include any of the sections in a dynamic
3671 object in the output file. We hack by simply clobbering the
3672 list of sections in the BFD. This could be handled more
3673 cleanly by, say, a new section flag; the existing
3674 SEC_NEVER_LOAD flag is not the one we want, because that one
3675 still implies that the section takes up space in the output
3676 file. */
3677 bfd_section_list_clear (abfd);
3678
3679 /* Find the name to use in a DT_NEEDED entry that refers to this
3680 object. If the object has a DT_SONAME entry, we use it.
3681 Otherwise, if the generic linker stuck something in
3682 elf_dt_name, we use that. Otherwise, we just use the file
3683 name. */
3684 if (soname == NULL || *soname == '\0')
3685 {
3686 soname = elf_dt_name (abfd);
3687 if (soname == NULL || *soname == '\0')
3688 soname = bfd_get_filename (abfd);
3689 }
3690
3691 /* Save the SONAME because sometimes the linker emulation code
3692 will need to know it. */
3693 elf_dt_name (abfd) = soname;
3694
3695 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3696 if (ret < 0)
3697 goto error_return;
3698
3699 /* If we have already included this dynamic object in the
3700 link, just ignore it. There is no reason to include a
3701 particular dynamic object more than once. */
3702 if (ret > 0)
3703 return TRUE;
3704
3705 /* Save the DT_AUDIT entry for the linker emulation code. */
3706 elf_dt_audit (abfd) = audit;
3707 }
3708
3709 /* If this is a dynamic object, we always link against the .dynsym
3710 symbol table, not the .symtab symbol table. The dynamic linker
3711 will only see the .dynsym symbol table, so there is no reason to
3712 look at .symtab for a dynamic object. */
3713
3714 if (! dynamic || elf_dynsymtab (abfd) == 0)
3715 hdr = &elf_tdata (abfd)->symtab_hdr;
3716 else
3717 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3718
3719 symcount = hdr->sh_size / bed->s->sizeof_sym;
3720
3721 /* The sh_info field of the symtab header tells us where the
3722 external symbols start. We don't care about the local symbols at
3723 this point. */
3724 if (elf_bad_symtab (abfd))
3725 {
3726 extsymcount = symcount;
3727 extsymoff = 0;
3728 }
3729 else
3730 {
3731 extsymcount = symcount - hdr->sh_info;
3732 extsymoff = hdr->sh_info;
3733 }
3734
3735 sym_hash = NULL;
3736 if (extsymcount != 0)
3737 {
3738 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3739 NULL, NULL, NULL);
3740 if (isymbuf == NULL)
3741 goto error_return;
3742
3743 /* We store a pointer to the hash table entry for each external
3744 symbol. */
3745 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3746 sym_hash = (struct elf_link_hash_entry **) bfd_alloc (abfd, amt);
3747 if (sym_hash == NULL)
3748 goto error_free_sym;
3749 elf_sym_hashes (abfd) = sym_hash;
3750 }
3751
3752 if (dynamic)
3753 {
3754 /* Read in any version definitions. */
3755 if (!_bfd_elf_slurp_version_tables (abfd,
3756 info->default_imported_symver))
3757 goto error_free_sym;
3758
3759 /* Read in the symbol versions, but don't bother to convert them
3760 to internal format. */
3761 if (elf_dynversym (abfd) != 0)
3762 {
3763 Elf_Internal_Shdr *versymhdr;
3764
3765 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3766 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
3767 if (extversym == NULL)
3768 goto error_free_sym;
3769 amt = versymhdr->sh_size;
3770 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3771 || bfd_bread (extversym, amt, abfd) != amt)
3772 goto error_free_vers;
3773 }
3774 }
3775
3776 /* If we are loading an as-needed shared lib, save the symbol table
3777 state before we start adding symbols. If the lib turns out
3778 to be unneeded, restore the state. */
3779 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3780 {
3781 unsigned int i;
3782 size_t entsize;
3783
3784 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3785 {
3786 struct bfd_hash_entry *p;
3787 struct elf_link_hash_entry *h;
3788
3789 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3790 {
3791 h = (struct elf_link_hash_entry *) p;
3792 entsize += htab->root.table.entsize;
3793 if (h->root.type == bfd_link_hash_warning)
3794 entsize += htab->root.table.entsize;
3795 }
3796 }
3797
3798 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
3799 hashsize = extsymcount * sizeof (struct elf_link_hash_entry *);
3800 old_tab = bfd_malloc (tabsize + entsize + hashsize);
3801 if (old_tab == NULL)
3802 goto error_free_vers;
3803
3804 /* Remember the current objalloc pointer, so that all mem for
3805 symbols added can later be reclaimed. */
3806 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3807 if (alloc_mark == NULL)
3808 goto error_free_vers;
3809
3810 /* Make a special call to the linker "notice" function to
3811 tell it that we are about to handle an as-needed lib. */
3812 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
3813 notice_as_needed, 0, NULL))
3814 goto error_free_vers;
3815
3816 /* Clone the symbol table and sym hashes. Remember some
3817 pointers into the symbol table, and dynamic symbol count. */
3818 old_hash = (char *) old_tab + tabsize;
3819 old_ent = (char *) old_hash + hashsize;
3820 memcpy (old_tab, htab->root.table.table, tabsize);
3821 memcpy (old_hash, sym_hash, hashsize);
3822 old_undefs = htab->root.undefs;
3823 old_undefs_tail = htab->root.undefs_tail;
3824 old_table = htab->root.table.table;
3825 old_size = htab->root.table.size;
3826 old_count = htab->root.table.count;
3827 old_dynsymcount = htab->dynsymcount;
3828
3829 for (i = 0; i < htab->root.table.size; i++)
3830 {
3831 struct bfd_hash_entry *p;
3832 struct elf_link_hash_entry *h;
3833
3834 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3835 {
3836 memcpy (old_ent, p, htab->root.table.entsize);
3837 old_ent = (char *) old_ent + htab->root.table.entsize;
3838 h = (struct elf_link_hash_entry *) p;
3839 if (h->root.type == bfd_link_hash_warning)
3840 {
3841 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3842 old_ent = (char *) old_ent + htab->root.table.entsize;
3843 }
3844 }
3845 }
3846 }
3847
3848 weaks = NULL;
3849 ever = extversym != NULL ? extversym + extsymoff : NULL;
3850 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3851 isym < isymend;
3852 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3853 {
3854 int bind;
3855 bfd_vma value;
3856 asection *sec, *new_sec;
3857 flagword flags;
3858 const char *name;
3859 struct elf_link_hash_entry *h;
3860 struct elf_link_hash_entry *hi;
3861 bfd_boolean definition;
3862 bfd_boolean size_change_ok;
3863 bfd_boolean type_change_ok;
3864 bfd_boolean new_weakdef;
3865 bfd_boolean override;
3866 bfd_boolean common;
3867 unsigned int old_alignment;
3868 bfd *old_bfd;
3869 bfd * undef_bfd = NULL;
3870
3871 override = FALSE;
3872
3873 flags = BSF_NO_FLAGS;
3874 sec = NULL;
3875 value = isym->st_value;
3876 *sym_hash = NULL;
3877 common = bed->common_definition (isym);
3878
3879 bind = ELF_ST_BIND (isym->st_info);
3880 switch (bind)
3881 {
3882 case STB_LOCAL:
3883 /* This should be impossible, since ELF requires that all
3884 global symbols follow all local symbols, and that sh_info
3885 point to the first global symbol. Unfortunately, Irix 5
3886 screws this up. */
3887 continue;
3888
3889 case STB_GLOBAL:
3890 if (isym->st_shndx != SHN_UNDEF && !common)
3891 flags = BSF_GLOBAL;
3892 break;
3893
3894 case STB_WEAK:
3895 flags = BSF_WEAK;
3896 break;
3897
3898 case STB_GNU_UNIQUE:
3899 flags = BSF_GNU_UNIQUE;
3900 break;
3901
3902 default:
3903 /* Leave it up to the processor backend. */
3904 break;
3905 }
3906
3907 if (isym->st_shndx == SHN_UNDEF)
3908 sec = bfd_und_section_ptr;
3909 else if (isym->st_shndx == SHN_ABS)
3910 sec = bfd_abs_section_ptr;
3911 else if (isym->st_shndx == SHN_COMMON)
3912 {
3913 sec = bfd_com_section_ptr;
3914 /* What ELF calls the size we call the value. What ELF
3915 calls the value we call the alignment. */
3916 value = isym->st_size;
3917 }
3918 else
3919 {
3920 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3921 if (sec == NULL)
3922 sec = bfd_abs_section_ptr;
3923 else if (discarded_section (sec))
3924 {
3925 /* Symbols from discarded section are undefined. We keep
3926 its visibility. */
3927 sec = bfd_und_section_ptr;
3928 isym->st_shndx = SHN_UNDEF;
3929 }
3930 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3931 value -= sec->vma;
3932 }
3933
3934 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3935 isym->st_name);
3936 if (name == NULL)
3937 goto error_free_vers;
3938
3939 if (isym->st_shndx == SHN_COMMON
3940 && (abfd->flags & BFD_PLUGIN) != 0)
3941 {
3942 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
3943
3944 if (xc == NULL)
3945 {
3946 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
3947 | SEC_EXCLUDE);
3948 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
3949 if (xc == NULL)
3950 goto error_free_vers;
3951 }
3952 sec = xc;
3953 }
3954 else if (isym->st_shndx == SHN_COMMON
3955 && ELF_ST_TYPE (isym->st_info) == STT_TLS
3956 && !info->relocatable)
3957 {
3958 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3959
3960 if (tcomm == NULL)
3961 {
3962 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
3963 | SEC_LINKER_CREATED);
3964 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3965 if (tcomm == NULL)
3966 goto error_free_vers;
3967 }
3968 sec = tcomm;
3969 }
3970 else if (bed->elf_add_symbol_hook)
3971 {
3972 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
3973 &sec, &value))
3974 goto error_free_vers;
3975
3976 /* The hook function sets the name to NULL if this symbol
3977 should be skipped for some reason. */
3978 if (name == NULL)
3979 continue;
3980 }
3981
3982 /* Sanity check that all possibilities were handled. */
3983 if (sec == NULL)
3984 {
3985 bfd_set_error (bfd_error_bad_value);
3986 goto error_free_vers;
3987 }
3988
3989 if (bfd_is_und_section (sec)
3990 || bfd_is_com_section (sec))
3991 definition = FALSE;
3992 else
3993 definition = TRUE;
3994
3995 size_change_ok = FALSE;
3996 type_change_ok = bed->type_change_ok;
3997 old_alignment = 0;
3998 old_bfd = NULL;
3999 new_sec = sec;
4000
4001 if (is_elf_hash_table (htab))
4002 {
4003 Elf_Internal_Versym iver;
4004 unsigned int vernum = 0;
4005 bfd_boolean skip;
4006
4007 /* If this is a definition of a symbol which was previously
4008 referenced in a non-weak manner then make a note of the bfd
4009 that contained the reference. This is used if we need to
4010 refer to the source of the reference later on. */
4011 if (! bfd_is_und_section (sec))
4012 {
4013 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4014
4015 if (h != NULL
4016 && h->root.type == bfd_link_hash_undefined
4017 && h->root.u.undef.abfd)
4018 undef_bfd = h->root.u.undef.abfd;
4019 }
4020
4021 if (ever == NULL)
4022 {
4023 if (info->default_imported_symver)
4024 /* Use the default symbol version created earlier. */
4025 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4026 else
4027 iver.vs_vers = 0;
4028 }
4029 else
4030 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4031
4032 vernum = iver.vs_vers & VERSYM_VERSION;
4033
4034 /* If this is a hidden symbol, or if it is not version
4035 1, we append the version name to the symbol name.
4036 However, we do not modify a non-hidden absolute symbol
4037 if it is not a function, because it might be the version
4038 symbol itself. FIXME: What if it isn't? */
4039 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4040 || (vernum > 1
4041 && (!bfd_is_abs_section (sec)
4042 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4043 {
4044 const char *verstr;
4045 size_t namelen, verlen, newlen;
4046 char *newname, *p;
4047
4048 if (isym->st_shndx != SHN_UNDEF)
4049 {
4050 if (vernum > elf_tdata (abfd)->cverdefs)
4051 verstr = NULL;
4052 else if (vernum > 1)
4053 verstr =
4054 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4055 else
4056 verstr = "";
4057
4058 if (verstr == NULL)
4059 {
4060 (*_bfd_error_handler)
4061 (_("%B: %s: invalid version %u (max %d)"),
4062 abfd, name, vernum,
4063 elf_tdata (abfd)->cverdefs);
4064 bfd_set_error (bfd_error_bad_value);
4065 goto error_free_vers;
4066 }
4067 }
4068 else
4069 {
4070 /* We cannot simply test for the number of
4071 entries in the VERNEED section since the
4072 numbers for the needed versions do not start
4073 at 0. */
4074 Elf_Internal_Verneed *t;
4075
4076 verstr = NULL;
4077 for (t = elf_tdata (abfd)->verref;
4078 t != NULL;
4079 t = t->vn_nextref)
4080 {
4081 Elf_Internal_Vernaux *a;
4082
4083 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4084 {
4085 if (a->vna_other == vernum)
4086 {
4087 verstr = a->vna_nodename;
4088 break;
4089 }
4090 }
4091 if (a != NULL)
4092 break;
4093 }
4094 if (verstr == NULL)
4095 {
4096 (*_bfd_error_handler)
4097 (_("%B: %s: invalid needed version %d"),
4098 abfd, name, vernum);
4099 bfd_set_error (bfd_error_bad_value);
4100 goto error_free_vers;
4101 }
4102 }
4103
4104 namelen = strlen (name);
4105 verlen = strlen (verstr);
4106 newlen = namelen + verlen + 2;
4107 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4108 && isym->st_shndx != SHN_UNDEF)
4109 ++newlen;
4110
4111 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4112 if (newname == NULL)
4113 goto error_free_vers;
4114 memcpy (newname, name, namelen);
4115 p = newname + namelen;
4116 *p++ = ELF_VER_CHR;
4117 /* If this is a defined non-hidden version symbol,
4118 we add another @ to the name. This indicates the
4119 default version of the symbol. */
4120 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4121 && isym->st_shndx != SHN_UNDEF)
4122 *p++ = ELF_VER_CHR;
4123 memcpy (p, verstr, verlen + 1);
4124
4125 name = newname;
4126 }
4127
4128 /* If necessary, make a second attempt to locate the bfd
4129 containing an unresolved, non-weak reference to the
4130 current symbol. */
4131 if (! bfd_is_und_section (sec) && undef_bfd == NULL)
4132 {
4133 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4134
4135 if (h != NULL
4136 && h->root.type == bfd_link_hash_undefined
4137 && h->root.u.undef.abfd)
4138 undef_bfd = h->root.u.undef.abfd;
4139 }
4140
4141 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec,
4142 &value, &old_alignment,
4143 sym_hash, &skip, &override,
4144 &type_change_ok, &size_change_ok))
4145 goto error_free_vers;
4146
4147 if (skip)
4148 continue;
4149
4150 if (override)
4151 definition = FALSE;
4152
4153 h = *sym_hash;
4154 while (h->root.type == bfd_link_hash_indirect
4155 || h->root.type == bfd_link_hash_warning)
4156 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4157
4158 /* Remember the old alignment if this is a common symbol, so
4159 that we don't reduce the alignment later on. We can't
4160 check later, because _bfd_generic_link_add_one_symbol
4161 will set a default for the alignment which we want to
4162 override. We also remember the old bfd where the existing
4163 definition comes from. */
4164 switch (h->root.type)
4165 {
4166 default:
4167 break;
4168
4169 case bfd_link_hash_defined:
4170 case bfd_link_hash_defweak:
4171 old_bfd = h->root.u.def.section->owner;
4172 break;
4173
4174 case bfd_link_hash_common:
4175 old_bfd = h->root.u.c.p->section->owner;
4176 old_alignment = h->root.u.c.p->alignment_power;
4177 break;
4178 }
4179
4180 if (elf_tdata (abfd)->verdef != NULL
4181 && ! override
4182 && vernum > 1
4183 && definition)
4184 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4185 }
4186
4187 if (! (_bfd_generic_link_add_one_symbol
4188 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4189 (struct bfd_link_hash_entry **) sym_hash)))
4190 goto error_free_vers;
4191
4192 h = *sym_hash;
4193 /* We need to make sure that indirect symbol dynamic flags are
4194 updated. */
4195 hi = h;
4196 while (h->root.type == bfd_link_hash_indirect
4197 || h->root.type == bfd_link_hash_warning)
4198 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4199
4200 *sym_hash = h;
4201 if (is_elf_hash_table (htab))
4202 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4203
4204 new_weakdef = FALSE;
4205 if (dynamic
4206 && definition
4207 && (flags & BSF_WEAK) != 0
4208 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4209 && is_elf_hash_table (htab)
4210 && h->u.weakdef == NULL)
4211 {
4212 /* Keep a list of all weak defined non function symbols from
4213 a dynamic object, using the weakdef field. Later in this
4214 function we will set the weakdef field to the correct
4215 value. We only put non-function symbols from dynamic
4216 objects on this list, because that happens to be the only
4217 time we need to know the normal symbol corresponding to a
4218 weak symbol, and the information is time consuming to
4219 figure out. If the weakdef field is not already NULL,
4220 then this symbol was already defined by some previous
4221 dynamic object, and we will be using that previous
4222 definition anyhow. */
4223
4224 h->u.weakdef = weaks;
4225 weaks = h;
4226 new_weakdef = TRUE;
4227 }
4228
4229 /* Set the alignment of a common symbol. */
4230 if ((common || bfd_is_com_section (sec))
4231 && h->root.type == bfd_link_hash_common)
4232 {
4233 unsigned int align;
4234
4235 if (common)
4236 align = bfd_log2 (isym->st_value);
4237 else
4238 {
4239 /* The new symbol is a common symbol in a shared object.
4240 We need to get the alignment from the section. */
4241 align = new_sec->alignment_power;
4242 }
4243 if (align > old_alignment)
4244 h->root.u.c.p->alignment_power = align;
4245 else
4246 h->root.u.c.p->alignment_power = old_alignment;
4247 }
4248
4249 if (is_elf_hash_table (htab))
4250 {
4251 bfd_boolean dynsym;
4252
4253 /* Check the alignment when a common symbol is involved. This
4254 can change when a common symbol is overridden by a normal
4255 definition or a common symbol is ignored due to the old
4256 normal definition. We need to make sure the maximum
4257 alignment is maintained. */
4258 if ((old_alignment || common)
4259 && h->root.type != bfd_link_hash_common)
4260 {
4261 unsigned int common_align;
4262 unsigned int normal_align;
4263 unsigned int symbol_align;
4264 bfd *normal_bfd;
4265 bfd *common_bfd;
4266
4267 symbol_align = ffs (h->root.u.def.value) - 1;
4268 if (h->root.u.def.section->owner != NULL
4269 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4270 {
4271 normal_align = h->root.u.def.section->alignment_power;
4272 if (normal_align > symbol_align)
4273 normal_align = symbol_align;
4274 }
4275 else
4276 normal_align = symbol_align;
4277
4278 if (old_alignment)
4279 {
4280 common_align = old_alignment;
4281 common_bfd = old_bfd;
4282 normal_bfd = abfd;
4283 }
4284 else
4285 {
4286 common_align = bfd_log2 (isym->st_value);
4287 common_bfd = abfd;
4288 normal_bfd = old_bfd;
4289 }
4290
4291 if (normal_align < common_align)
4292 {
4293 /* PR binutils/2735 */
4294 if (normal_bfd == NULL)
4295 (*_bfd_error_handler)
4296 (_("Warning: alignment %u of common symbol `%s' in %B"
4297 " is greater than the alignment (%u) of its section %A"),
4298 common_bfd, h->root.u.def.section,
4299 1 << common_align, name, 1 << normal_align);
4300 else
4301 (*_bfd_error_handler)
4302 (_("Warning: alignment %u of symbol `%s' in %B"
4303 " is smaller than %u in %B"),
4304 normal_bfd, common_bfd,
4305 1 << normal_align, name, 1 << common_align);
4306 }
4307 }
4308
4309 /* Remember the symbol size if it isn't undefined. */
4310 if ((isym->st_size != 0 && isym->st_shndx != SHN_UNDEF)
4311 && (definition || h->size == 0))
4312 {
4313 if (h->size != 0
4314 && h->size != isym->st_size
4315 && ! size_change_ok)
4316 (*_bfd_error_handler)
4317 (_("Warning: size of symbol `%s' changed"
4318 " from %lu in %B to %lu in %B"),
4319 old_bfd, abfd,
4320 name, (unsigned long) h->size,
4321 (unsigned long) isym->st_size);
4322
4323 h->size = isym->st_size;
4324 }
4325
4326 /* If this is a common symbol, then we always want H->SIZE
4327 to be the size of the common symbol. The code just above
4328 won't fix the size if a common symbol becomes larger. We
4329 don't warn about a size change here, because that is
4330 covered by --warn-common. Allow changed between different
4331 function types. */
4332 if (h->root.type == bfd_link_hash_common)
4333 h->size = h->root.u.c.size;
4334
4335 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4336 && (definition || h->type == STT_NOTYPE))
4337 {
4338 unsigned int type = ELF_ST_TYPE (isym->st_info);
4339
4340 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4341 symbol. */
4342 if (type == STT_GNU_IFUNC
4343 && (abfd->flags & DYNAMIC) != 0)
4344 type = STT_FUNC;
4345
4346 if (h->type != type)
4347 {
4348 if (h->type != STT_NOTYPE && ! type_change_ok)
4349 (*_bfd_error_handler)
4350 (_("Warning: type of symbol `%s' changed"
4351 " from %d to %d in %B"),
4352 abfd, name, h->type, type);
4353
4354 h->type = type;
4355 }
4356 }
4357
4358 /* Merge st_other field. */
4359 elf_merge_st_other (abfd, h, isym, definition, dynamic);
4360
4361 /* Set a flag in the hash table entry indicating the type of
4362 reference or definition we just found. Keep a count of
4363 the number of dynamic symbols we find. A dynamic symbol
4364 is one which is referenced or defined by both a regular
4365 object and a shared object. */
4366 dynsym = FALSE;
4367 if (! dynamic)
4368 {
4369 if (! definition)
4370 {
4371 h->ref_regular = 1;
4372 if (bind != STB_WEAK)
4373 h->ref_regular_nonweak = 1;
4374 }
4375 else
4376 {
4377 h->def_regular = 1;
4378 if (h->def_dynamic)
4379 {
4380 h->def_dynamic = 0;
4381 h->ref_dynamic = 1;
4382 }
4383 }
4384
4385 /* If the indirect symbol has been forced local, don't
4386 make the real symbol dynamic. */
4387 if ((h == hi || !hi->forced_local)
4388 && (! info->executable
4389 || h->def_dynamic
4390 || h->ref_dynamic))
4391 dynsym = TRUE;
4392 }
4393 else
4394 {
4395 if (! definition)
4396 {
4397 h->ref_dynamic = 1;
4398 hi->ref_dynamic = 1;
4399 }
4400 else
4401 {
4402 h->def_dynamic = 1;
4403 h->dynamic_def = 1;
4404 hi->def_dynamic = 1;
4405 hi->dynamic_def = 1;
4406 }
4407
4408 /* If the indirect symbol has been forced local, don't
4409 make the real symbol dynamic. */
4410 if ((h == hi || !hi->forced_local)
4411 && (h->def_regular
4412 || h->ref_regular
4413 || (h->u.weakdef != NULL
4414 && ! new_weakdef
4415 && h->u.weakdef->dynindx != -1)))
4416 dynsym = TRUE;
4417 }
4418
4419 /* We don't want to make debug symbol dynamic. */
4420 if (definition && (sec->flags & SEC_DEBUGGING) && !info->relocatable)
4421 dynsym = FALSE;
4422
4423 /* Nor should we make plugin symbols dynamic. */
4424 if ((abfd->flags & BFD_PLUGIN) != 0)
4425 dynsym = FALSE;
4426
4427 if (definition)
4428 h->target_internal = isym->st_target_internal;
4429
4430 /* Check to see if we need to add an indirect symbol for
4431 the default name. */
4432 if (definition || h->root.type == bfd_link_hash_common)
4433 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4434 &sec, &value, &dynsym,
4435 override))
4436 goto error_free_vers;
4437
4438 if (definition && !dynamic)
4439 {
4440 char *p = strchr (name, ELF_VER_CHR);
4441 if (p != NULL && p[1] != ELF_VER_CHR)
4442 {
4443 /* Queue non-default versions so that .symver x, x@FOO
4444 aliases can be checked. */
4445 if (!nondeflt_vers)
4446 {
4447 amt = ((isymend - isym + 1)
4448 * sizeof (struct elf_link_hash_entry *));
4449 nondeflt_vers =
4450 (struct elf_link_hash_entry **) bfd_malloc (amt);
4451 if (!nondeflt_vers)
4452 goto error_free_vers;
4453 }
4454 nondeflt_vers[nondeflt_vers_cnt++] = h;
4455 }
4456 }
4457
4458 if (dynsym && h->dynindx == -1)
4459 {
4460 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4461 goto error_free_vers;
4462 if (h->u.weakdef != NULL
4463 && ! new_weakdef
4464 && h->u.weakdef->dynindx == -1)
4465 {
4466 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4467 goto error_free_vers;
4468 }
4469 }
4470 else if (dynsym && h->dynindx != -1)
4471 /* If the symbol already has a dynamic index, but
4472 visibility says it should not be visible, turn it into
4473 a local symbol. */
4474 switch (ELF_ST_VISIBILITY (h->other))
4475 {
4476 case STV_INTERNAL:
4477 case STV_HIDDEN:
4478 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4479 dynsym = FALSE;
4480 break;
4481 }
4482
4483 if (!add_needed
4484 && definition
4485 && ((dynsym
4486 && h->ref_regular)
4487 || (h->ref_dynamic
4488 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4489 && !on_needed_list (elf_dt_name (abfd), htab->needed))))
4490 {
4491 int ret;
4492 const char *soname = elf_dt_name (abfd);
4493
4494 /* A symbol from a library loaded via DT_NEEDED of some
4495 other library is referenced by a regular object.
4496 Add a DT_NEEDED entry for it. Issue an error if
4497 --no-add-needed is used and the reference was not
4498 a weak one. */
4499 if (undef_bfd != NULL
4500 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4501 {
4502 (*_bfd_error_handler)
4503 (_("%B: undefined reference to symbol '%s'"),
4504 undef_bfd, name);
4505 (*_bfd_error_handler)
4506 (_("note: '%s' is defined in DSO %B so try adding it to the linker command line"),
4507 abfd, name);
4508 bfd_set_error (bfd_error_invalid_operation);
4509 goto error_free_vers;
4510 }
4511
4512 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4513 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
4514
4515 add_needed = TRUE;
4516 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4517 if (ret < 0)
4518 goto error_free_vers;
4519
4520 BFD_ASSERT (ret == 0);
4521 }
4522 }
4523 }
4524
4525 if (extversym != NULL)
4526 {
4527 free (extversym);
4528 extversym = NULL;
4529 }
4530
4531 if (isymbuf != NULL)
4532 {
4533 free (isymbuf);
4534 isymbuf = NULL;
4535 }
4536
4537 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4538 {
4539 unsigned int i;
4540
4541 /* Restore the symbol table. */
4542 if (bed->as_needed_cleanup)
4543 (*bed->as_needed_cleanup) (abfd, info);
4544 old_hash = (char *) old_tab + tabsize;
4545 old_ent = (char *) old_hash + hashsize;
4546 sym_hash = elf_sym_hashes (abfd);
4547 htab->root.table.table = old_table;
4548 htab->root.table.size = old_size;
4549 htab->root.table.count = old_count;
4550 memcpy (htab->root.table.table, old_tab, tabsize);
4551 memcpy (sym_hash, old_hash, hashsize);
4552 htab->root.undefs = old_undefs;
4553 htab->root.undefs_tail = old_undefs_tail;
4554 for (i = 0; i < htab->root.table.size; i++)
4555 {
4556 struct bfd_hash_entry *p;
4557 struct elf_link_hash_entry *h;
4558 bfd_size_type size;
4559 unsigned int alignment_power;
4560
4561 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4562 {
4563 h = (struct elf_link_hash_entry *) p;
4564 if (h->root.type == bfd_link_hash_warning)
4565 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4566 if (h->dynindx >= old_dynsymcount)
4567 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
4568
4569 /* Preserve the maximum alignment and size for common
4570 symbols even if this dynamic lib isn't on DT_NEEDED
4571 since it can still be loaded at the run-time by another
4572 dynamic lib. */
4573 if (h->root.type == bfd_link_hash_common)
4574 {
4575 size = h->root.u.c.size;
4576 alignment_power = h->root.u.c.p->alignment_power;
4577 }
4578 else
4579 {
4580 size = 0;
4581 alignment_power = 0;
4582 }
4583 memcpy (p, old_ent, htab->root.table.entsize);
4584 old_ent = (char *) old_ent + htab->root.table.entsize;
4585 h = (struct elf_link_hash_entry *) p;
4586 if (h->root.type == bfd_link_hash_warning)
4587 {
4588 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4589 old_ent = (char *) old_ent + htab->root.table.entsize;
4590 }
4591 else if (h->root.type == bfd_link_hash_common)
4592 {
4593 if (size > h->root.u.c.size)
4594 h->root.u.c.size = size;
4595 if (alignment_power > h->root.u.c.p->alignment_power)
4596 h->root.u.c.p->alignment_power = alignment_power;
4597 }
4598 }
4599 }
4600
4601 /* Make a special call to the linker "notice" function to
4602 tell it that symbols added for crefs may need to be removed. */
4603 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
4604 notice_not_needed, 0, NULL))
4605 goto error_free_vers;
4606
4607 free (old_tab);
4608 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4609 alloc_mark);
4610 if (nondeflt_vers != NULL)
4611 free (nondeflt_vers);
4612 return TRUE;
4613 }
4614
4615 if (old_tab != NULL)
4616 {
4617 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
4618 notice_needed, 0, NULL))
4619 goto error_free_vers;
4620 free (old_tab);
4621 old_tab = NULL;
4622 }
4623
4624 /* Now that all the symbols from this input file are created, handle
4625 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4626 if (nondeflt_vers != NULL)
4627 {
4628 bfd_size_type cnt, symidx;
4629
4630 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4631 {
4632 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4633 char *shortname, *p;
4634
4635 p = strchr (h->root.root.string, ELF_VER_CHR);
4636 if (p == NULL
4637 || (h->root.type != bfd_link_hash_defined
4638 && h->root.type != bfd_link_hash_defweak))
4639 continue;
4640
4641 amt = p - h->root.root.string;
4642 shortname = (char *) bfd_malloc (amt + 1);
4643 if (!shortname)
4644 goto error_free_vers;
4645 memcpy (shortname, h->root.root.string, amt);
4646 shortname[amt] = '\0';
4647
4648 hi = (struct elf_link_hash_entry *)
4649 bfd_link_hash_lookup (&htab->root, shortname,
4650 FALSE, FALSE, FALSE);
4651 if (hi != NULL
4652 && hi->root.type == h->root.type
4653 && hi->root.u.def.value == h->root.u.def.value
4654 && hi->root.u.def.section == h->root.u.def.section)
4655 {
4656 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4657 hi->root.type = bfd_link_hash_indirect;
4658 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4659 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4660 sym_hash = elf_sym_hashes (abfd);
4661 if (sym_hash)
4662 for (symidx = 0; symidx < extsymcount; ++symidx)
4663 if (sym_hash[symidx] == hi)
4664 {
4665 sym_hash[symidx] = h;
4666 break;
4667 }
4668 }
4669 free (shortname);
4670 }
4671 free (nondeflt_vers);
4672 nondeflt_vers = NULL;
4673 }
4674
4675 /* Now set the weakdefs field correctly for all the weak defined
4676 symbols we found. The only way to do this is to search all the
4677 symbols. Since we only need the information for non functions in
4678 dynamic objects, that's the only time we actually put anything on
4679 the list WEAKS. We need this information so that if a regular
4680 object refers to a symbol defined weakly in a dynamic object, the
4681 real symbol in the dynamic object is also put in the dynamic
4682 symbols; we also must arrange for both symbols to point to the
4683 same memory location. We could handle the general case of symbol
4684 aliasing, but a general symbol alias can only be generated in
4685 assembler code, handling it correctly would be very time
4686 consuming, and other ELF linkers don't handle general aliasing
4687 either. */
4688 if (weaks != NULL)
4689 {
4690 struct elf_link_hash_entry **hpp;
4691 struct elf_link_hash_entry **hppend;
4692 struct elf_link_hash_entry **sorted_sym_hash;
4693 struct elf_link_hash_entry *h;
4694 size_t sym_count;
4695
4696 /* Since we have to search the whole symbol list for each weak
4697 defined symbol, search time for N weak defined symbols will be
4698 O(N^2). Binary search will cut it down to O(NlogN). */
4699 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4700 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4701 if (sorted_sym_hash == NULL)
4702 goto error_return;
4703 sym_hash = sorted_sym_hash;
4704 hpp = elf_sym_hashes (abfd);
4705 hppend = hpp + extsymcount;
4706 sym_count = 0;
4707 for (; hpp < hppend; hpp++)
4708 {
4709 h = *hpp;
4710 if (h != NULL
4711 && h->root.type == bfd_link_hash_defined
4712 && !bed->is_function_type (h->type))
4713 {
4714 *sym_hash = h;
4715 sym_hash++;
4716 sym_count++;
4717 }
4718 }
4719
4720 qsort (sorted_sym_hash, sym_count,
4721 sizeof (struct elf_link_hash_entry *),
4722 elf_sort_symbol);
4723
4724 while (weaks != NULL)
4725 {
4726 struct elf_link_hash_entry *hlook;
4727 asection *slook;
4728 bfd_vma vlook;
4729 long ilook;
4730 size_t i, j, idx;
4731
4732 hlook = weaks;
4733 weaks = hlook->u.weakdef;
4734 hlook->u.weakdef = NULL;
4735
4736 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4737 || hlook->root.type == bfd_link_hash_defweak
4738 || hlook->root.type == bfd_link_hash_common
4739 || hlook->root.type == bfd_link_hash_indirect);
4740 slook = hlook->root.u.def.section;
4741 vlook = hlook->root.u.def.value;
4742
4743 ilook = -1;
4744 i = 0;
4745 j = sym_count;
4746 while (i < j)
4747 {
4748 bfd_signed_vma vdiff;
4749 idx = (i + j) / 2;
4750 h = sorted_sym_hash [idx];
4751 vdiff = vlook - h->root.u.def.value;
4752 if (vdiff < 0)
4753 j = idx;
4754 else if (vdiff > 0)
4755 i = idx + 1;
4756 else
4757 {
4758 long sdiff = slook->id - h->root.u.def.section->id;
4759 if (sdiff < 0)
4760 j = idx;
4761 else if (sdiff > 0)
4762 i = idx + 1;
4763 else
4764 {
4765 ilook = idx;
4766 break;
4767 }
4768 }
4769 }
4770
4771 /* We didn't find a value/section match. */
4772 if (ilook == -1)
4773 continue;
4774
4775 for (i = ilook; i < sym_count; i++)
4776 {
4777 h = sorted_sym_hash [i];
4778
4779 /* Stop if value or section doesn't match. */
4780 if (h->root.u.def.value != vlook
4781 || h->root.u.def.section != slook)
4782 break;
4783 else if (h != hlook)
4784 {
4785 hlook->u.weakdef = h;
4786
4787 /* If the weak definition is in the list of dynamic
4788 symbols, make sure the real definition is put
4789 there as well. */
4790 if (hlook->dynindx != -1 && h->dynindx == -1)
4791 {
4792 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4793 {
4794 err_free_sym_hash:
4795 free (sorted_sym_hash);
4796 goto error_return;
4797 }
4798 }
4799
4800 /* If the real definition is in the list of dynamic
4801 symbols, make sure the weak definition is put
4802 there as well. If we don't do this, then the
4803 dynamic loader might not merge the entries for the
4804 real definition and the weak definition. */
4805 if (h->dynindx != -1 && hlook->dynindx == -1)
4806 {
4807 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4808 goto err_free_sym_hash;
4809 }
4810 break;
4811 }
4812 }
4813 }
4814
4815 free (sorted_sym_hash);
4816 }
4817
4818 if (bed->check_directives
4819 && !(*bed->check_directives) (abfd, info))
4820 return FALSE;
4821
4822 /* If this object is the same format as the output object, and it is
4823 not a shared library, then let the backend look through the
4824 relocs.
4825
4826 This is required to build global offset table entries and to
4827 arrange for dynamic relocs. It is not required for the
4828 particular common case of linking non PIC code, even when linking
4829 against shared libraries, but unfortunately there is no way of
4830 knowing whether an object file has been compiled PIC or not.
4831 Looking through the relocs is not particularly time consuming.
4832 The problem is that we must either (1) keep the relocs in memory,
4833 which causes the linker to require additional runtime memory or
4834 (2) read the relocs twice from the input file, which wastes time.
4835 This would be a good case for using mmap.
4836
4837 I have no idea how to handle linking PIC code into a file of a
4838 different format. It probably can't be done. */
4839 if (! dynamic
4840 && is_elf_hash_table (htab)
4841 && bed->check_relocs != NULL
4842 && elf_object_id (abfd) == elf_hash_table_id (htab)
4843 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4844 {
4845 asection *o;
4846
4847 for (o = abfd->sections; o != NULL; o = o->next)
4848 {
4849 Elf_Internal_Rela *internal_relocs;
4850 bfd_boolean ok;
4851
4852 if ((o->flags & SEC_RELOC) == 0
4853 || o->reloc_count == 0
4854 || ((info->strip == strip_all || info->strip == strip_debugger)
4855 && (o->flags & SEC_DEBUGGING) != 0)
4856 || bfd_is_abs_section (o->output_section))
4857 continue;
4858
4859 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4860 info->keep_memory);
4861 if (internal_relocs == NULL)
4862 goto error_return;
4863
4864 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4865
4866 if (elf_section_data (o)->relocs != internal_relocs)
4867 free (internal_relocs);
4868
4869 if (! ok)
4870 goto error_return;
4871 }
4872 }
4873
4874 /* If this is a non-traditional link, try to optimize the handling
4875 of the .stab/.stabstr sections. */
4876 if (! dynamic
4877 && ! info->traditional_format
4878 && is_elf_hash_table (htab)
4879 && (info->strip != strip_all && info->strip != strip_debugger))
4880 {
4881 asection *stabstr;
4882
4883 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4884 if (stabstr != NULL)
4885 {
4886 bfd_size_type string_offset = 0;
4887 asection *stab;
4888
4889 for (stab = abfd->sections; stab; stab = stab->next)
4890 if (CONST_STRNEQ (stab->name, ".stab")
4891 && (!stab->name[5] ||
4892 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4893 && (stab->flags & SEC_MERGE) == 0
4894 && !bfd_is_abs_section (stab->output_section))
4895 {
4896 struct bfd_elf_section_data *secdata;
4897
4898 secdata = elf_section_data (stab);
4899 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
4900 stabstr, &secdata->sec_info,
4901 &string_offset))
4902 goto error_return;
4903 if (secdata->sec_info)
4904 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4905 }
4906 }
4907 }
4908
4909 if (is_elf_hash_table (htab) && add_needed)
4910 {
4911 /* Add this bfd to the loaded list. */
4912 struct elf_link_loaded_list *n;
4913
4914 n = (struct elf_link_loaded_list *)
4915 bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4916 if (n == NULL)
4917 goto error_return;
4918 n->abfd = abfd;
4919 n->next = htab->loaded;
4920 htab->loaded = n;
4921 }
4922
4923 return TRUE;
4924
4925 error_free_vers:
4926 if (old_tab != NULL)
4927 free (old_tab);
4928 if (nondeflt_vers != NULL)
4929 free (nondeflt_vers);
4930 if (extversym != NULL)
4931 free (extversym);
4932 error_free_sym:
4933 if (isymbuf != NULL)
4934 free (isymbuf);
4935 error_return:
4936 return FALSE;
4937 }
4938
4939 /* Return the linker hash table entry of a symbol that might be
4940 satisfied by an archive symbol. Return -1 on error. */
4941
4942 struct elf_link_hash_entry *
4943 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4944 struct bfd_link_info *info,
4945 const char *name)
4946 {
4947 struct elf_link_hash_entry *h;
4948 char *p, *copy;
4949 size_t len, first;
4950
4951 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
4952 if (h != NULL)
4953 return h;
4954
4955 /* If this is a default version (the name contains @@), look up the
4956 symbol again with only one `@' as well as without the version.
4957 The effect is that references to the symbol with and without the
4958 version will be matched by the default symbol in the archive. */
4959
4960 p = strchr (name, ELF_VER_CHR);
4961 if (p == NULL || p[1] != ELF_VER_CHR)
4962 return h;
4963
4964 /* First check with only one `@'. */
4965 len = strlen (name);
4966 copy = (char *) bfd_alloc (abfd, len);
4967 if (copy == NULL)
4968 return (struct elf_link_hash_entry *) 0 - 1;
4969
4970 first = p - name + 1;
4971 memcpy (copy, name, first);
4972 memcpy (copy + first, name + first + 1, len - first);
4973
4974 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
4975 if (h == NULL)
4976 {
4977 /* We also need to check references to the symbol without the
4978 version. */
4979 copy[first - 1] = '\0';
4980 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4981 FALSE, FALSE, TRUE);
4982 }
4983
4984 bfd_release (abfd, copy);
4985 return h;
4986 }
4987
4988 /* Add symbols from an ELF archive file to the linker hash table. We
4989 don't use _bfd_generic_link_add_archive_symbols because of a
4990 problem which arises on UnixWare. The UnixWare libc.so is an
4991 archive which includes an entry libc.so.1 which defines a bunch of
4992 symbols. The libc.so archive also includes a number of other
4993 object files, which also define symbols, some of which are the same
4994 as those defined in libc.so.1. Correct linking requires that we
4995 consider each object file in turn, and include it if it defines any
4996 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4997 this; it looks through the list of undefined symbols, and includes
4998 any object file which defines them. When this algorithm is used on
4999 UnixWare, it winds up pulling in libc.so.1 early and defining a
5000 bunch of symbols. This means that some of the other objects in the
5001 archive are not included in the link, which is incorrect since they
5002 precede libc.so.1 in the archive.
5003
5004 Fortunately, ELF archive handling is simpler than that done by
5005 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5006 oddities. In ELF, if we find a symbol in the archive map, and the
5007 symbol is currently undefined, we know that we must pull in that
5008 object file.
5009
5010 Unfortunately, we do have to make multiple passes over the symbol
5011 table until nothing further is resolved. */
5012
5013 static bfd_boolean
5014 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5015 {
5016 symindex c;
5017 bfd_boolean *defined = NULL;
5018 bfd_boolean *included = NULL;
5019 carsym *symdefs;
5020 bfd_boolean loop;
5021 bfd_size_type amt;
5022 const struct elf_backend_data *bed;
5023 struct elf_link_hash_entry * (*archive_symbol_lookup)
5024 (bfd *, struct bfd_link_info *, const char *);
5025
5026 if (! bfd_has_map (abfd))
5027 {
5028 /* An empty archive is a special case. */
5029 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5030 return TRUE;
5031 bfd_set_error (bfd_error_no_armap);
5032 return FALSE;
5033 }
5034
5035 /* Keep track of all symbols we know to be already defined, and all
5036 files we know to be already included. This is to speed up the
5037 second and subsequent passes. */
5038 c = bfd_ardata (abfd)->symdef_count;
5039 if (c == 0)
5040 return TRUE;
5041 amt = c;
5042 amt *= sizeof (bfd_boolean);
5043 defined = (bfd_boolean *) bfd_zmalloc (amt);
5044 included = (bfd_boolean *) bfd_zmalloc (amt);
5045 if (defined == NULL || included == NULL)
5046 goto error_return;
5047
5048 symdefs = bfd_ardata (abfd)->symdefs;
5049 bed = get_elf_backend_data (abfd);
5050 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5051
5052 do
5053 {
5054 file_ptr last;
5055 symindex i;
5056 carsym *symdef;
5057 carsym *symdefend;
5058
5059 loop = FALSE;
5060 last = -1;
5061
5062 symdef = symdefs;
5063 symdefend = symdef + c;
5064 for (i = 0; symdef < symdefend; symdef++, i++)
5065 {
5066 struct elf_link_hash_entry *h;
5067 bfd *element;
5068 struct bfd_link_hash_entry *undefs_tail;
5069 symindex mark;
5070
5071 if (defined[i] || included[i])
5072 continue;
5073 if (symdef->file_offset == last)
5074 {
5075 included[i] = TRUE;
5076 continue;
5077 }
5078
5079 h = archive_symbol_lookup (abfd, info, symdef->name);
5080 if (h == (struct elf_link_hash_entry *) 0 - 1)
5081 goto error_return;
5082
5083 if (h == NULL)
5084 continue;
5085
5086 if (h->root.type == bfd_link_hash_common)
5087 {
5088 /* We currently have a common symbol. The archive map contains
5089 a reference to this symbol, so we may want to include it. We
5090 only want to include it however, if this archive element
5091 contains a definition of the symbol, not just another common
5092 declaration of it.
5093
5094 Unfortunately some archivers (including GNU ar) will put
5095 declarations of common symbols into their archive maps, as
5096 well as real definitions, so we cannot just go by the archive
5097 map alone. Instead we must read in the element's symbol
5098 table and check that to see what kind of symbol definition
5099 this is. */
5100 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5101 continue;
5102 }
5103 else if (h->root.type != bfd_link_hash_undefined)
5104 {
5105 if (h->root.type != bfd_link_hash_undefweak)
5106 defined[i] = TRUE;
5107 continue;
5108 }
5109
5110 /* We need to include this archive member. */
5111 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5112 if (element == NULL)
5113 goto error_return;
5114
5115 if (! bfd_check_format (element, bfd_object))
5116 goto error_return;
5117
5118 /* Doublecheck that we have not included this object
5119 already--it should be impossible, but there may be
5120 something wrong with the archive. */
5121 if (element->archive_pass != 0)
5122 {
5123 bfd_set_error (bfd_error_bad_value);
5124 goto error_return;
5125 }
5126 element->archive_pass = 1;
5127
5128 undefs_tail = info->hash->undefs_tail;
5129
5130 if (!(*info->callbacks
5131 ->add_archive_element) (info, element, symdef->name, &element))
5132 goto error_return;
5133 if (!bfd_link_add_symbols (element, info))
5134 goto error_return;
5135
5136 /* If there are any new undefined symbols, we need to make
5137 another pass through the archive in order to see whether
5138 they can be defined. FIXME: This isn't perfect, because
5139 common symbols wind up on undefs_tail and because an
5140 undefined symbol which is defined later on in this pass
5141 does not require another pass. This isn't a bug, but it
5142 does make the code less efficient than it could be. */
5143 if (undefs_tail != info->hash->undefs_tail)
5144 loop = TRUE;
5145
5146 /* Look backward to mark all symbols from this object file
5147 which we have already seen in this pass. */
5148 mark = i;
5149 do
5150 {
5151 included[mark] = TRUE;
5152 if (mark == 0)
5153 break;
5154 --mark;
5155 }
5156 while (symdefs[mark].file_offset == symdef->file_offset);
5157
5158 /* We mark subsequent symbols from this object file as we go
5159 on through the loop. */
5160 last = symdef->file_offset;
5161 }
5162 }
5163 while (loop);
5164
5165 free (defined);
5166 free (included);
5167
5168 return TRUE;
5169
5170 error_return:
5171 if (defined != NULL)
5172 free (defined);
5173 if (included != NULL)
5174 free (included);
5175 return FALSE;
5176 }
5177
5178 /* Given an ELF BFD, add symbols to the global hash table as
5179 appropriate. */
5180
5181 bfd_boolean
5182 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5183 {
5184 switch (bfd_get_format (abfd))
5185 {
5186 case bfd_object:
5187 return elf_link_add_object_symbols (abfd, info);
5188 case bfd_archive:
5189 return elf_link_add_archive_symbols (abfd, info);
5190 default:
5191 bfd_set_error (bfd_error_wrong_format);
5192 return FALSE;
5193 }
5194 }
5195 \f
5196 struct hash_codes_info
5197 {
5198 unsigned long *hashcodes;
5199 bfd_boolean error;
5200 };
5201
5202 /* This function will be called though elf_link_hash_traverse to store
5203 all hash value of the exported symbols in an array. */
5204
5205 static bfd_boolean
5206 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5207 {
5208 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5209 const char *name;
5210 char *p;
5211 unsigned long ha;
5212 char *alc = NULL;
5213
5214 /* Ignore indirect symbols. These are added by the versioning code. */
5215 if (h->dynindx == -1)
5216 return TRUE;
5217
5218 name = h->root.root.string;
5219 p = strchr (name, ELF_VER_CHR);
5220 if (p != NULL)
5221 {
5222 alc = (char *) bfd_malloc (p - name + 1);
5223 if (alc == NULL)
5224 {
5225 inf->error = TRUE;
5226 return FALSE;
5227 }
5228 memcpy (alc, name, p - name);
5229 alc[p - name] = '\0';
5230 name = alc;
5231 }
5232
5233 /* Compute the hash value. */
5234 ha = bfd_elf_hash (name);
5235
5236 /* Store the found hash value in the array given as the argument. */
5237 *(inf->hashcodes)++ = ha;
5238
5239 /* And store it in the struct so that we can put it in the hash table
5240 later. */
5241 h->u.elf_hash_value = ha;
5242
5243 if (alc != NULL)
5244 free (alc);
5245
5246 return TRUE;
5247 }
5248
5249 struct collect_gnu_hash_codes
5250 {
5251 bfd *output_bfd;
5252 const struct elf_backend_data *bed;
5253 unsigned long int nsyms;
5254 unsigned long int maskbits;
5255 unsigned long int *hashcodes;
5256 unsigned long int *hashval;
5257 unsigned long int *indx;
5258 unsigned long int *counts;
5259 bfd_vma *bitmask;
5260 bfd_byte *contents;
5261 long int min_dynindx;
5262 unsigned long int bucketcount;
5263 unsigned long int symindx;
5264 long int local_indx;
5265 long int shift1, shift2;
5266 unsigned long int mask;
5267 bfd_boolean error;
5268 };
5269
5270 /* This function will be called though elf_link_hash_traverse to store
5271 all hash value of the exported symbols in an array. */
5272
5273 static bfd_boolean
5274 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5275 {
5276 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5277 const char *name;
5278 char *p;
5279 unsigned long ha;
5280 char *alc = NULL;
5281
5282 /* Ignore indirect symbols. These are added by the versioning code. */
5283 if (h->dynindx == -1)
5284 return TRUE;
5285
5286 /* Ignore also local symbols and undefined symbols. */
5287 if (! (*s->bed->elf_hash_symbol) (h))
5288 return TRUE;
5289
5290 name = h->root.root.string;
5291 p = strchr (name, ELF_VER_CHR);
5292 if (p != NULL)
5293 {
5294 alc = (char *) bfd_malloc (p - name + 1);
5295 if (alc == NULL)
5296 {
5297 s->error = TRUE;
5298 return FALSE;
5299 }
5300 memcpy (alc, name, p - name);
5301 alc[p - name] = '\0';
5302 name = alc;
5303 }
5304
5305 /* Compute the hash value. */
5306 ha = bfd_elf_gnu_hash (name);
5307
5308 /* Store the found hash value in the array for compute_bucket_count,
5309 and also for .dynsym reordering purposes. */
5310 s->hashcodes[s->nsyms] = ha;
5311 s->hashval[h->dynindx] = ha;
5312 ++s->nsyms;
5313 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5314 s->min_dynindx = h->dynindx;
5315
5316 if (alc != NULL)
5317 free (alc);
5318
5319 return TRUE;
5320 }
5321
5322 /* This function will be called though elf_link_hash_traverse to do
5323 final dynaminc symbol renumbering. */
5324
5325 static bfd_boolean
5326 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5327 {
5328 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5329 unsigned long int bucket;
5330 unsigned long int val;
5331
5332 /* Ignore indirect symbols. */
5333 if (h->dynindx == -1)
5334 return TRUE;
5335
5336 /* Ignore also local symbols and undefined symbols. */
5337 if (! (*s->bed->elf_hash_symbol) (h))
5338 {
5339 if (h->dynindx >= s->min_dynindx)
5340 h->dynindx = s->local_indx++;
5341 return TRUE;
5342 }
5343
5344 bucket = s->hashval[h->dynindx] % s->bucketcount;
5345 val = (s->hashval[h->dynindx] >> s->shift1)
5346 & ((s->maskbits >> s->shift1) - 1);
5347 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5348 s->bitmask[val]
5349 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5350 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5351 if (s->counts[bucket] == 1)
5352 /* Last element terminates the chain. */
5353 val |= 1;
5354 bfd_put_32 (s->output_bfd, val,
5355 s->contents + (s->indx[bucket] - s->symindx) * 4);
5356 --s->counts[bucket];
5357 h->dynindx = s->indx[bucket]++;
5358 return TRUE;
5359 }
5360
5361 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5362
5363 bfd_boolean
5364 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5365 {
5366 return !(h->forced_local
5367 || h->root.type == bfd_link_hash_undefined
5368 || h->root.type == bfd_link_hash_undefweak
5369 || ((h->root.type == bfd_link_hash_defined
5370 || h->root.type == bfd_link_hash_defweak)
5371 && h->root.u.def.section->output_section == NULL));
5372 }
5373
5374 /* Array used to determine the number of hash table buckets to use
5375 based on the number of symbols there are. If there are fewer than
5376 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5377 fewer than 37 we use 17 buckets, and so forth. We never use more
5378 than 32771 buckets. */
5379
5380 static const size_t elf_buckets[] =
5381 {
5382 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5383 16411, 32771, 0
5384 };
5385
5386 /* Compute bucket count for hashing table. We do not use a static set
5387 of possible tables sizes anymore. Instead we determine for all
5388 possible reasonable sizes of the table the outcome (i.e., the
5389 number of collisions etc) and choose the best solution. The
5390 weighting functions are not too simple to allow the table to grow
5391 without bounds. Instead one of the weighting factors is the size.
5392 Therefore the result is always a good payoff between few collisions
5393 (= short chain lengths) and table size. */
5394 static size_t
5395 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5396 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5397 unsigned long int nsyms,
5398 int gnu_hash)
5399 {
5400 size_t best_size = 0;
5401 unsigned long int i;
5402
5403 /* We have a problem here. The following code to optimize the table
5404 size requires an integer type with more the 32 bits. If
5405 BFD_HOST_U_64_BIT is set we know about such a type. */
5406 #ifdef BFD_HOST_U_64_BIT
5407 if (info->optimize)
5408 {
5409 size_t minsize;
5410 size_t maxsize;
5411 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5412 bfd *dynobj = elf_hash_table (info)->dynobj;
5413 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5414 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5415 unsigned long int *counts;
5416 bfd_size_type amt;
5417 unsigned int no_improvement_count = 0;
5418
5419 /* Possible optimization parameters: if we have NSYMS symbols we say
5420 that the hashing table must at least have NSYMS/4 and at most
5421 2*NSYMS buckets. */
5422 minsize = nsyms / 4;
5423 if (minsize == 0)
5424 minsize = 1;
5425 best_size = maxsize = nsyms * 2;
5426 if (gnu_hash)
5427 {
5428 if (minsize < 2)
5429 minsize = 2;
5430 if ((best_size & 31) == 0)
5431 ++best_size;
5432 }
5433
5434 /* Create array where we count the collisions in. We must use bfd_malloc
5435 since the size could be large. */
5436 amt = maxsize;
5437 amt *= sizeof (unsigned long int);
5438 counts = (unsigned long int *) bfd_malloc (amt);
5439 if (counts == NULL)
5440 return 0;
5441
5442 /* Compute the "optimal" size for the hash table. The criteria is a
5443 minimal chain length. The minor criteria is (of course) the size
5444 of the table. */
5445 for (i = minsize; i < maxsize; ++i)
5446 {
5447 /* Walk through the array of hashcodes and count the collisions. */
5448 BFD_HOST_U_64_BIT max;
5449 unsigned long int j;
5450 unsigned long int fact;
5451
5452 if (gnu_hash && (i & 31) == 0)
5453 continue;
5454
5455 memset (counts, '\0', i * sizeof (unsigned long int));
5456
5457 /* Determine how often each hash bucket is used. */
5458 for (j = 0; j < nsyms; ++j)
5459 ++counts[hashcodes[j] % i];
5460
5461 /* For the weight function we need some information about the
5462 pagesize on the target. This is information need not be 100%
5463 accurate. Since this information is not available (so far) we
5464 define it here to a reasonable default value. If it is crucial
5465 to have a better value some day simply define this value. */
5466 # ifndef BFD_TARGET_PAGESIZE
5467 # define BFD_TARGET_PAGESIZE (4096)
5468 # endif
5469
5470 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5471 and the chains. */
5472 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5473
5474 # if 1
5475 /* Variant 1: optimize for short chains. We add the squares
5476 of all the chain lengths (which favors many small chain
5477 over a few long chains). */
5478 for (j = 0; j < i; ++j)
5479 max += counts[j] * counts[j];
5480
5481 /* This adds penalties for the overall size of the table. */
5482 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5483 max *= fact * fact;
5484 # else
5485 /* Variant 2: Optimize a lot more for small table. Here we
5486 also add squares of the size but we also add penalties for
5487 empty slots (the +1 term). */
5488 for (j = 0; j < i; ++j)
5489 max += (1 + counts[j]) * (1 + counts[j]);
5490
5491 /* The overall size of the table is considered, but not as
5492 strong as in variant 1, where it is squared. */
5493 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5494 max *= fact;
5495 # endif
5496
5497 /* Compare with current best results. */
5498 if (max < best_chlen)
5499 {
5500 best_chlen = max;
5501 best_size = i;
5502 no_improvement_count = 0;
5503 }
5504 /* PR 11843: Avoid futile long searches for the best bucket size
5505 when there are a large number of symbols. */
5506 else if (++no_improvement_count == 100)
5507 break;
5508 }
5509
5510 free (counts);
5511 }
5512 else
5513 #endif /* defined (BFD_HOST_U_64_BIT) */
5514 {
5515 /* This is the fallback solution if no 64bit type is available or if we
5516 are not supposed to spend much time on optimizations. We select the
5517 bucket count using a fixed set of numbers. */
5518 for (i = 0; elf_buckets[i] != 0; i++)
5519 {
5520 best_size = elf_buckets[i];
5521 if (nsyms < elf_buckets[i + 1])
5522 break;
5523 }
5524 if (gnu_hash && best_size < 2)
5525 best_size = 2;
5526 }
5527
5528 return best_size;
5529 }
5530
5531 /* Size any SHT_GROUP section for ld -r. */
5532
5533 bfd_boolean
5534 _bfd_elf_size_group_sections (struct bfd_link_info *info)
5535 {
5536 bfd *ibfd;
5537
5538 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
5539 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5540 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5541 return FALSE;
5542 return TRUE;
5543 }
5544
5545 /* Set up the sizes and contents of the ELF dynamic sections. This is
5546 called by the ELF linker emulation before_allocation routine. We
5547 must set the sizes of the sections before the linker sets the
5548 addresses of the various sections. */
5549
5550 bfd_boolean
5551 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5552 const char *soname,
5553 const char *rpath,
5554 const char *filter_shlib,
5555 const char *audit,
5556 const char *depaudit,
5557 const char * const *auxiliary_filters,
5558 struct bfd_link_info *info,
5559 asection **sinterpptr)
5560 {
5561 bfd_size_type soname_indx;
5562 bfd *dynobj;
5563 const struct elf_backend_data *bed;
5564 struct elf_info_failed asvinfo;
5565
5566 *sinterpptr = NULL;
5567
5568 soname_indx = (bfd_size_type) -1;
5569
5570 if (!is_elf_hash_table (info->hash))
5571 return TRUE;
5572
5573 bed = get_elf_backend_data (output_bfd);
5574 if (info->execstack)
5575 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
5576 else if (info->noexecstack)
5577 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
5578 else
5579 {
5580 bfd *inputobj;
5581 asection *notesec = NULL;
5582 int exec = 0;
5583
5584 for (inputobj = info->input_bfds;
5585 inputobj;
5586 inputobj = inputobj->link_next)
5587 {
5588 asection *s;
5589
5590 if (inputobj->flags
5591 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5592 continue;
5593 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5594 if (s)
5595 {
5596 if (s->flags & SEC_CODE)
5597 exec = PF_X;
5598 notesec = s;
5599 }
5600 else if (bed->default_execstack)
5601 exec = PF_X;
5602 }
5603 if (notesec)
5604 {
5605 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
5606 if (exec && info->relocatable
5607 && notesec->output_section != bfd_abs_section_ptr)
5608 notesec->output_section->flags |= SEC_CODE;
5609 }
5610 }
5611
5612 /* Any syms created from now on start with -1 in
5613 got.refcount/offset and plt.refcount/offset. */
5614 elf_hash_table (info)->init_got_refcount
5615 = elf_hash_table (info)->init_got_offset;
5616 elf_hash_table (info)->init_plt_refcount
5617 = elf_hash_table (info)->init_plt_offset;
5618
5619 if (info->relocatable
5620 && !_bfd_elf_size_group_sections (info))
5621 return FALSE;
5622
5623 /* The backend may have to create some sections regardless of whether
5624 we're dynamic or not. */
5625 if (bed->elf_backend_always_size_sections
5626 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5627 return FALSE;
5628
5629 dynobj = elf_hash_table (info)->dynobj;
5630
5631 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5632 {
5633 struct elf_info_failed eif;
5634 struct elf_link_hash_entry *h;
5635 asection *dynstr;
5636 struct bfd_elf_version_tree *t;
5637 struct bfd_elf_version_expr *d;
5638 asection *s;
5639 bfd_boolean all_defined;
5640
5641 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
5642 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
5643
5644 if (soname != NULL)
5645 {
5646 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5647 soname, TRUE);
5648 if (soname_indx == (bfd_size_type) -1
5649 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5650 return FALSE;
5651 }
5652
5653 if (info->symbolic)
5654 {
5655 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5656 return FALSE;
5657 info->flags |= DF_SYMBOLIC;
5658 }
5659
5660 if (rpath != NULL)
5661 {
5662 bfd_size_type indx;
5663
5664 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5665 TRUE);
5666 if (indx == (bfd_size_type) -1
5667 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
5668 return FALSE;
5669
5670 if (info->new_dtags)
5671 {
5672 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
5673 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
5674 return FALSE;
5675 }
5676 }
5677
5678 if (filter_shlib != NULL)
5679 {
5680 bfd_size_type indx;
5681
5682 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5683 filter_shlib, TRUE);
5684 if (indx == (bfd_size_type) -1
5685 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5686 return FALSE;
5687 }
5688
5689 if (auxiliary_filters != NULL)
5690 {
5691 const char * const *p;
5692
5693 for (p = auxiliary_filters; *p != NULL; p++)
5694 {
5695 bfd_size_type indx;
5696
5697 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5698 *p, TRUE);
5699 if (indx == (bfd_size_type) -1
5700 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5701 return FALSE;
5702 }
5703 }
5704
5705 if (audit != NULL)
5706 {
5707 bfd_size_type indx;
5708
5709 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5710 TRUE);
5711 if (indx == (bfd_size_type) -1
5712 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5713 return FALSE;
5714 }
5715
5716 if (depaudit != NULL)
5717 {
5718 bfd_size_type indx;
5719
5720 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
5721 TRUE);
5722 if (indx == (bfd_size_type) -1
5723 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
5724 return FALSE;
5725 }
5726
5727 eif.info = info;
5728 eif.failed = FALSE;
5729
5730 /* If we are supposed to export all symbols into the dynamic symbol
5731 table (this is not the normal case), then do so. */
5732 if (info->export_dynamic
5733 || (info->executable && info->dynamic))
5734 {
5735 elf_link_hash_traverse (elf_hash_table (info),
5736 _bfd_elf_export_symbol,
5737 &eif);
5738 if (eif.failed)
5739 return FALSE;
5740 }
5741
5742 /* Make all global versions with definition. */
5743 for (t = info->version_info; t != NULL; t = t->next)
5744 for (d = t->globals.list; d != NULL; d = d->next)
5745 if (!d->symver && d->literal)
5746 {
5747 const char *verstr, *name;
5748 size_t namelen, verlen, newlen;
5749 char *newname, *p, leading_char;
5750 struct elf_link_hash_entry *newh;
5751
5752 leading_char = bfd_get_symbol_leading_char (output_bfd);
5753 name = d->pattern;
5754 namelen = strlen (name) + (leading_char != '\0');
5755 verstr = t->name;
5756 verlen = strlen (verstr);
5757 newlen = namelen + verlen + 3;
5758
5759 newname = (char *) bfd_malloc (newlen);
5760 if (newname == NULL)
5761 return FALSE;
5762 newname[0] = leading_char;
5763 memcpy (newname + (leading_char != '\0'), name, namelen);
5764
5765 /* Check the hidden versioned definition. */
5766 p = newname + namelen;
5767 *p++ = ELF_VER_CHR;
5768 memcpy (p, verstr, verlen + 1);
5769 newh = elf_link_hash_lookup (elf_hash_table (info),
5770 newname, FALSE, FALSE,
5771 FALSE);
5772 if (newh == NULL
5773 || (newh->root.type != bfd_link_hash_defined
5774 && newh->root.type != bfd_link_hash_defweak))
5775 {
5776 /* Check the default versioned definition. */
5777 *p++ = ELF_VER_CHR;
5778 memcpy (p, verstr, verlen + 1);
5779 newh = elf_link_hash_lookup (elf_hash_table (info),
5780 newname, FALSE, FALSE,
5781 FALSE);
5782 }
5783 free (newname);
5784
5785 /* Mark this version if there is a definition and it is
5786 not defined in a shared object. */
5787 if (newh != NULL
5788 && !newh->def_dynamic
5789 && (newh->root.type == bfd_link_hash_defined
5790 || newh->root.type == bfd_link_hash_defweak))
5791 d->symver = 1;
5792 }
5793
5794 /* Attach all the symbols to their version information. */
5795 asvinfo.info = info;
5796 asvinfo.failed = FALSE;
5797
5798 elf_link_hash_traverse (elf_hash_table (info),
5799 _bfd_elf_link_assign_sym_version,
5800 &asvinfo);
5801 if (asvinfo.failed)
5802 return FALSE;
5803
5804 if (!info->allow_undefined_version)
5805 {
5806 /* Check if all global versions have a definition. */
5807 all_defined = TRUE;
5808 for (t = info->version_info; t != NULL; t = t->next)
5809 for (d = t->globals.list; d != NULL; d = d->next)
5810 if (d->literal && !d->symver && !d->script)
5811 {
5812 (*_bfd_error_handler)
5813 (_("%s: undefined version: %s"),
5814 d->pattern, t->name);
5815 all_defined = FALSE;
5816 }
5817
5818 if (!all_defined)
5819 {
5820 bfd_set_error (bfd_error_bad_value);
5821 return FALSE;
5822 }
5823 }
5824
5825 /* Find all symbols which were defined in a dynamic object and make
5826 the backend pick a reasonable value for them. */
5827 elf_link_hash_traverse (elf_hash_table (info),
5828 _bfd_elf_adjust_dynamic_symbol,
5829 &eif);
5830 if (eif.failed)
5831 return FALSE;
5832
5833 /* Add some entries to the .dynamic section. We fill in some of the
5834 values later, in bfd_elf_final_link, but we must add the entries
5835 now so that we know the final size of the .dynamic section. */
5836
5837 /* If there are initialization and/or finalization functions to
5838 call then add the corresponding DT_INIT/DT_FINI entries. */
5839 h = (info->init_function
5840 ? elf_link_hash_lookup (elf_hash_table (info),
5841 info->init_function, FALSE,
5842 FALSE, FALSE)
5843 : NULL);
5844 if (h != NULL
5845 && (h->ref_regular
5846 || h->def_regular))
5847 {
5848 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5849 return FALSE;
5850 }
5851 h = (info->fini_function
5852 ? elf_link_hash_lookup (elf_hash_table (info),
5853 info->fini_function, FALSE,
5854 FALSE, FALSE)
5855 : NULL);
5856 if (h != NULL
5857 && (h->ref_regular
5858 || h->def_regular))
5859 {
5860 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5861 return FALSE;
5862 }
5863
5864 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5865 if (s != NULL && s->linker_has_input)
5866 {
5867 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5868 if (! info->executable)
5869 {
5870 bfd *sub;
5871 asection *o;
5872
5873 for (sub = info->input_bfds; sub != NULL;
5874 sub = sub->link_next)
5875 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
5876 for (o = sub->sections; o != NULL; o = o->next)
5877 if (elf_section_data (o)->this_hdr.sh_type
5878 == SHT_PREINIT_ARRAY)
5879 {
5880 (*_bfd_error_handler)
5881 (_("%B: .preinit_array section is not allowed in DSO"),
5882 sub);
5883 break;
5884 }
5885
5886 bfd_set_error (bfd_error_nonrepresentable_section);
5887 return FALSE;
5888 }
5889
5890 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5891 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5892 return FALSE;
5893 }
5894 s = bfd_get_section_by_name (output_bfd, ".init_array");
5895 if (s != NULL && s->linker_has_input)
5896 {
5897 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5898 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5899 return FALSE;
5900 }
5901 s = bfd_get_section_by_name (output_bfd, ".fini_array");
5902 if (s != NULL && s->linker_has_input)
5903 {
5904 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5905 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5906 return FALSE;
5907 }
5908
5909 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
5910 /* If .dynstr is excluded from the link, we don't want any of
5911 these tags. Strictly, we should be checking each section
5912 individually; This quick check covers for the case where
5913 someone does a /DISCARD/ : { *(*) }. */
5914 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5915 {
5916 bfd_size_type strsize;
5917
5918 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5919 if ((info->emit_hash
5920 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
5921 || (info->emit_gnu_hash
5922 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5923 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5924 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5925 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5926 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5927 bed->s->sizeof_sym))
5928 return FALSE;
5929 }
5930 }
5931
5932 /* The backend must work out the sizes of all the other dynamic
5933 sections. */
5934 if (dynobj != NULL
5935 && bed->elf_backend_size_dynamic_sections != NULL
5936 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5937 return FALSE;
5938
5939 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
5940 return FALSE;
5941
5942 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5943 {
5944 unsigned long section_sym_count;
5945 struct bfd_elf_version_tree *verdefs;
5946 asection *s;
5947
5948 /* Set up the version definition section. */
5949 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5950 BFD_ASSERT (s != NULL);
5951
5952 /* We may have created additional version definitions if we are
5953 just linking a regular application. */
5954 verdefs = info->version_info;
5955
5956 /* Skip anonymous version tag. */
5957 if (verdefs != NULL && verdefs->vernum == 0)
5958 verdefs = verdefs->next;
5959
5960 if (verdefs == NULL && !info->create_default_symver)
5961 s->flags |= SEC_EXCLUDE;
5962 else
5963 {
5964 unsigned int cdefs;
5965 bfd_size_type size;
5966 struct bfd_elf_version_tree *t;
5967 bfd_byte *p;
5968 Elf_Internal_Verdef def;
5969 Elf_Internal_Verdaux defaux;
5970 struct bfd_link_hash_entry *bh;
5971 struct elf_link_hash_entry *h;
5972 const char *name;
5973
5974 cdefs = 0;
5975 size = 0;
5976
5977 /* Make space for the base version. */
5978 size += sizeof (Elf_External_Verdef);
5979 size += sizeof (Elf_External_Verdaux);
5980 ++cdefs;
5981
5982 /* Make space for the default version. */
5983 if (info->create_default_symver)
5984 {
5985 size += sizeof (Elf_External_Verdef);
5986 ++cdefs;
5987 }
5988
5989 for (t = verdefs; t != NULL; t = t->next)
5990 {
5991 struct bfd_elf_version_deps *n;
5992
5993 /* Don't emit base version twice. */
5994 if (t->vernum == 0)
5995 continue;
5996
5997 size += sizeof (Elf_External_Verdef);
5998 size += sizeof (Elf_External_Verdaux);
5999 ++cdefs;
6000
6001 for (n = t->deps; n != NULL; n = n->next)
6002 size += sizeof (Elf_External_Verdaux);
6003 }
6004
6005 s->size = size;
6006 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6007 if (s->contents == NULL && s->size != 0)
6008 return FALSE;
6009
6010 /* Fill in the version definition section. */
6011
6012 p = s->contents;
6013
6014 def.vd_version = VER_DEF_CURRENT;
6015 def.vd_flags = VER_FLG_BASE;
6016 def.vd_ndx = 1;
6017 def.vd_cnt = 1;
6018 if (info->create_default_symver)
6019 {
6020 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6021 def.vd_next = sizeof (Elf_External_Verdef);
6022 }
6023 else
6024 {
6025 def.vd_aux = sizeof (Elf_External_Verdef);
6026 def.vd_next = (sizeof (Elf_External_Verdef)
6027 + sizeof (Elf_External_Verdaux));
6028 }
6029
6030 if (soname_indx != (bfd_size_type) -1)
6031 {
6032 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6033 soname_indx);
6034 def.vd_hash = bfd_elf_hash (soname);
6035 defaux.vda_name = soname_indx;
6036 name = soname;
6037 }
6038 else
6039 {
6040 bfd_size_type indx;
6041
6042 name = lbasename (output_bfd->filename);
6043 def.vd_hash = bfd_elf_hash (name);
6044 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6045 name, FALSE);
6046 if (indx == (bfd_size_type) -1)
6047 return FALSE;
6048 defaux.vda_name = indx;
6049 }
6050 defaux.vda_next = 0;
6051
6052 _bfd_elf_swap_verdef_out (output_bfd, &def,
6053 (Elf_External_Verdef *) p);
6054 p += sizeof (Elf_External_Verdef);
6055 if (info->create_default_symver)
6056 {
6057 /* Add a symbol representing this version. */
6058 bh = NULL;
6059 if (! (_bfd_generic_link_add_one_symbol
6060 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6061 0, NULL, FALSE,
6062 get_elf_backend_data (dynobj)->collect, &bh)))
6063 return FALSE;
6064 h = (struct elf_link_hash_entry *) bh;
6065 h->non_elf = 0;
6066 h->def_regular = 1;
6067 h->type = STT_OBJECT;
6068 h->verinfo.vertree = NULL;
6069
6070 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6071 return FALSE;
6072
6073 /* Create a duplicate of the base version with the same
6074 aux block, but different flags. */
6075 def.vd_flags = 0;
6076 def.vd_ndx = 2;
6077 def.vd_aux = sizeof (Elf_External_Verdef);
6078 if (verdefs)
6079 def.vd_next = (sizeof (Elf_External_Verdef)
6080 + sizeof (Elf_External_Verdaux));
6081 else
6082 def.vd_next = 0;
6083 _bfd_elf_swap_verdef_out (output_bfd, &def,
6084 (Elf_External_Verdef *) p);
6085 p += sizeof (Elf_External_Verdef);
6086 }
6087 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6088 (Elf_External_Verdaux *) p);
6089 p += sizeof (Elf_External_Verdaux);
6090
6091 for (t = verdefs; t != NULL; t = t->next)
6092 {
6093 unsigned int cdeps;
6094 struct bfd_elf_version_deps *n;
6095
6096 /* Don't emit the base version twice. */
6097 if (t->vernum == 0)
6098 continue;
6099
6100 cdeps = 0;
6101 for (n = t->deps; n != NULL; n = n->next)
6102 ++cdeps;
6103
6104 /* Add a symbol representing this version. */
6105 bh = NULL;
6106 if (! (_bfd_generic_link_add_one_symbol
6107 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6108 0, NULL, FALSE,
6109 get_elf_backend_data (dynobj)->collect, &bh)))
6110 return FALSE;
6111 h = (struct elf_link_hash_entry *) bh;
6112 h->non_elf = 0;
6113 h->def_regular = 1;
6114 h->type = STT_OBJECT;
6115 h->verinfo.vertree = t;
6116
6117 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6118 return FALSE;
6119
6120 def.vd_version = VER_DEF_CURRENT;
6121 def.vd_flags = 0;
6122 if (t->globals.list == NULL
6123 && t->locals.list == NULL
6124 && ! t->used)
6125 def.vd_flags |= VER_FLG_WEAK;
6126 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6127 def.vd_cnt = cdeps + 1;
6128 def.vd_hash = bfd_elf_hash (t->name);
6129 def.vd_aux = sizeof (Elf_External_Verdef);
6130 def.vd_next = 0;
6131
6132 /* If a basever node is next, it *must* be the last node in
6133 the chain, otherwise Verdef construction breaks. */
6134 if (t->next != NULL && t->next->vernum == 0)
6135 BFD_ASSERT (t->next->next == NULL);
6136
6137 if (t->next != NULL && t->next->vernum != 0)
6138 def.vd_next = (sizeof (Elf_External_Verdef)
6139 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6140
6141 _bfd_elf_swap_verdef_out (output_bfd, &def,
6142 (Elf_External_Verdef *) p);
6143 p += sizeof (Elf_External_Verdef);
6144
6145 defaux.vda_name = h->dynstr_index;
6146 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6147 h->dynstr_index);
6148 defaux.vda_next = 0;
6149 if (t->deps != NULL)
6150 defaux.vda_next = sizeof (Elf_External_Verdaux);
6151 t->name_indx = defaux.vda_name;
6152
6153 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6154 (Elf_External_Verdaux *) p);
6155 p += sizeof (Elf_External_Verdaux);
6156
6157 for (n = t->deps; n != NULL; n = n->next)
6158 {
6159 if (n->version_needed == NULL)
6160 {
6161 /* This can happen if there was an error in the
6162 version script. */
6163 defaux.vda_name = 0;
6164 }
6165 else
6166 {
6167 defaux.vda_name = n->version_needed->name_indx;
6168 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6169 defaux.vda_name);
6170 }
6171 if (n->next == NULL)
6172 defaux.vda_next = 0;
6173 else
6174 defaux.vda_next = sizeof (Elf_External_Verdaux);
6175
6176 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6177 (Elf_External_Verdaux *) p);
6178 p += sizeof (Elf_External_Verdaux);
6179 }
6180 }
6181
6182 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6183 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6184 return FALSE;
6185
6186 elf_tdata (output_bfd)->cverdefs = cdefs;
6187 }
6188
6189 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6190 {
6191 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6192 return FALSE;
6193 }
6194 else if (info->flags & DF_BIND_NOW)
6195 {
6196 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6197 return FALSE;
6198 }
6199
6200 if (info->flags_1)
6201 {
6202 if (info->executable)
6203 info->flags_1 &= ~ (DF_1_INITFIRST
6204 | DF_1_NODELETE
6205 | DF_1_NOOPEN);
6206 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6207 return FALSE;
6208 }
6209
6210 /* Work out the size of the version reference section. */
6211
6212 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6213 BFD_ASSERT (s != NULL);
6214 {
6215 struct elf_find_verdep_info sinfo;
6216
6217 sinfo.info = info;
6218 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6219 if (sinfo.vers == 0)
6220 sinfo.vers = 1;
6221 sinfo.failed = FALSE;
6222
6223 elf_link_hash_traverse (elf_hash_table (info),
6224 _bfd_elf_link_find_version_dependencies,
6225 &sinfo);
6226 if (sinfo.failed)
6227 return FALSE;
6228
6229 if (elf_tdata (output_bfd)->verref == NULL)
6230 s->flags |= SEC_EXCLUDE;
6231 else
6232 {
6233 Elf_Internal_Verneed *t;
6234 unsigned int size;
6235 unsigned int crefs;
6236 bfd_byte *p;
6237
6238 /* Build the version dependency section. */
6239 size = 0;
6240 crefs = 0;
6241 for (t = elf_tdata (output_bfd)->verref;
6242 t != NULL;
6243 t = t->vn_nextref)
6244 {
6245 Elf_Internal_Vernaux *a;
6246
6247 size += sizeof (Elf_External_Verneed);
6248 ++crefs;
6249 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6250 size += sizeof (Elf_External_Vernaux);
6251 }
6252
6253 s->size = size;
6254 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6255 if (s->contents == NULL)
6256 return FALSE;
6257
6258 p = s->contents;
6259 for (t = elf_tdata (output_bfd)->verref;
6260 t != NULL;
6261 t = t->vn_nextref)
6262 {
6263 unsigned int caux;
6264 Elf_Internal_Vernaux *a;
6265 bfd_size_type indx;
6266
6267 caux = 0;
6268 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6269 ++caux;
6270
6271 t->vn_version = VER_NEED_CURRENT;
6272 t->vn_cnt = caux;
6273 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6274 elf_dt_name (t->vn_bfd) != NULL
6275 ? elf_dt_name (t->vn_bfd)
6276 : lbasename (t->vn_bfd->filename),
6277 FALSE);
6278 if (indx == (bfd_size_type) -1)
6279 return FALSE;
6280 t->vn_file = indx;
6281 t->vn_aux = sizeof (Elf_External_Verneed);
6282 if (t->vn_nextref == NULL)
6283 t->vn_next = 0;
6284 else
6285 t->vn_next = (sizeof (Elf_External_Verneed)
6286 + caux * sizeof (Elf_External_Vernaux));
6287
6288 _bfd_elf_swap_verneed_out (output_bfd, t,
6289 (Elf_External_Verneed *) p);
6290 p += sizeof (Elf_External_Verneed);
6291
6292 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6293 {
6294 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6295 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6296 a->vna_nodename, FALSE);
6297 if (indx == (bfd_size_type) -1)
6298 return FALSE;
6299 a->vna_name = indx;
6300 if (a->vna_nextptr == NULL)
6301 a->vna_next = 0;
6302 else
6303 a->vna_next = sizeof (Elf_External_Vernaux);
6304
6305 _bfd_elf_swap_vernaux_out (output_bfd, a,
6306 (Elf_External_Vernaux *) p);
6307 p += sizeof (Elf_External_Vernaux);
6308 }
6309 }
6310
6311 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6312 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6313 return FALSE;
6314
6315 elf_tdata (output_bfd)->cverrefs = crefs;
6316 }
6317 }
6318
6319 if ((elf_tdata (output_bfd)->cverrefs == 0
6320 && elf_tdata (output_bfd)->cverdefs == 0)
6321 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6322 &section_sym_count) == 0)
6323 {
6324 s = bfd_get_linker_section (dynobj, ".gnu.version");
6325 s->flags |= SEC_EXCLUDE;
6326 }
6327 }
6328 return TRUE;
6329 }
6330
6331 /* Find the first non-excluded output section. We'll use its
6332 section symbol for some emitted relocs. */
6333 void
6334 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6335 {
6336 asection *s;
6337
6338 for (s = output_bfd->sections; s != NULL; s = s->next)
6339 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6340 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6341 {
6342 elf_hash_table (info)->text_index_section = s;
6343 break;
6344 }
6345 }
6346
6347 /* Find two non-excluded output sections, one for code, one for data.
6348 We'll use their section symbols for some emitted relocs. */
6349 void
6350 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6351 {
6352 asection *s;
6353
6354 /* Data first, since setting text_index_section changes
6355 _bfd_elf_link_omit_section_dynsym. */
6356 for (s = output_bfd->sections; s != NULL; s = s->next)
6357 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6358 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6359 {
6360 elf_hash_table (info)->data_index_section = s;
6361 break;
6362 }
6363
6364 for (s = output_bfd->sections; s != NULL; s = s->next)
6365 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6366 == (SEC_ALLOC | SEC_READONLY))
6367 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6368 {
6369 elf_hash_table (info)->text_index_section = s;
6370 break;
6371 }
6372
6373 if (elf_hash_table (info)->text_index_section == NULL)
6374 elf_hash_table (info)->text_index_section
6375 = elf_hash_table (info)->data_index_section;
6376 }
6377
6378 bfd_boolean
6379 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6380 {
6381 const struct elf_backend_data *bed;
6382
6383 if (!is_elf_hash_table (info->hash))
6384 return TRUE;
6385
6386 bed = get_elf_backend_data (output_bfd);
6387 (*bed->elf_backend_init_index_section) (output_bfd, info);
6388
6389 if (elf_hash_table (info)->dynamic_sections_created)
6390 {
6391 bfd *dynobj;
6392 asection *s;
6393 bfd_size_type dynsymcount;
6394 unsigned long section_sym_count;
6395 unsigned int dtagcount;
6396
6397 dynobj = elf_hash_table (info)->dynobj;
6398
6399 /* Assign dynsym indicies. In a shared library we generate a
6400 section symbol for each output section, which come first.
6401 Next come all of the back-end allocated local dynamic syms,
6402 followed by the rest of the global symbols. */
6403
6404 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6405 &section_sym_count);
6406
6407 /* Work out the size of the symbol version section. */
6408 s = bfd_get_linker_section (dynobj, ".gnu.version");
6409 BFD_ASSERT (s != NULL);
6410 if (dynsymcount != 0
6411 && (s->flags & SEC_EXCLUDE) == 0)
6412 {
6413 s->size = dynsymcount * sizeof (Elf_External_Versym);
6414 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6415 if (s->contents == NULL)
6416 return FALSE;
6417
6418 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6419 return FALSE;
6420 }
6421
6422 /* Set the size of the .dynsym and .hash sections. We counted
6423 the number of dynamic symbols in elf_link_add_object_symbols.
6424 We will build the contents of .dynsym and .hash when we build
6425 the final symbol table, because until then we do not know the
6426 correct value to give the symbols. We built the .dynstr
6427 section as we went along in elf_link_add_object_symbols. */
6428 s = bfd_get_linker_section (dynobj, ".dynsym");
6429 BFD_ASSERT (s != NULL);
6430 s->size = dynsymcount * bed->s->sizeof_sym;
6431
6432 if (dynsymcount != 0)
6433 {
6434 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6435 if (s->contents == NULL)
6436 return FALSE;
6437
6438 /* The first entry in .dynsym is a dummy symbol.
6439 Clear all the section syms, in case we don't output them all. */
6440 ++section_sym_count;
6441 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6442 }
6443
6444 elf_hash_table (info)->bucketcount = 0;
6445
6446 /* Compute the size of the hashing table. As a side effect this
6447 computes the hash values for all the names we export. */
6448 if (info->emit_hash)
6449 {
6450 unsigned long int *hashcodes;
6451 struct hash_codes_info hashinf;
6452 bfd_size_type amt;
6453 unsigned long int nsyms;
6454 size_t bucketcount;
6455 size_t hash_entry_size;
6456
6457 /* Compute the hash values for all exported symbols. At the same
6458 time store the values in an array so that we could use them for
6459 optimizations. */
6460 amt = dynsymcount * sizeof (unsigned long int);
6461 hashcodes = (unsigned long int *) bfd_malloc (amt);
6462 if (hashcodes == NULL)
6463 return FALSE;
6464 hashinf.hashcodes = hashcodes;
6465 hashinf.error = FALSE;
6466
6467 /* Put all hash values in HASHCODES. */
6468 elf_link_hash_traverse (elf_hash_table (info),
6469 elf_collect_hash_codes, &hashinf);
6470 if (hashinf.error)
6471 {
6472 free (hashcodes);
6473 return FALSE;
6474 }
6475
6476 nsyms = hashinf.hashcodes - hashcodes;
6477 bucketcount
6478 = compute_bucket_count (info, hashcodes, nsyms, 0);
6479 free (hashcodes);
6480
6481 if (bucketcount == 0)
6482 return FALSE;
6483
6484 elf_hash_table (info)->bucketcount = bucketcount;
6485
6486 s = bfd_get_linker_section (dynobj, ".hash");
6487 BFD_ASSERT (s != NULL);
6488 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6489 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6490 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6491 if (s->contents == NULL)
6492 return FALSE;
6493
6494 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6495 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6496 s->contents + hash_entry_size);
6497 }
6498
6499 if (info->emit_gnu_hash)
6500 {
6501 size_t i, cnt;
6502 unsigned char *contents;
6503 struct collect_gnu_hash_codes cinfo;
6504 bfd_size_type amt;
6505 size_t bucketcount;
6506
6507 memset (&cinfo, 0, sizeof (cinfo));
6508
6509 /* Compute the hash values for all exported symbols. At the same
6510 time store the values in an array so that we could use them for
6511 optimizations. */
6512 amt = dynsymcount * 2 * sizeof (unsigned long int);
6513 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
6514 if (cinfo.hashcodes == NULL)
6515 return FALSE;
6516
6517 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6518 cinfo.min_dynindx = -1;
6519 cinfo.output_bfd = output_bfd;
6520 cinfo.bed = bed;
6521
6522 /* Put all hash values in HASHCODES. */
6523 elf_link_hash_traverse (elf_hash_table (info),
6524 elf_collect_gnu_hash_codes, &cinfo);
6525 if (cinfo.error)
6526 {
6527 free (cinfo.hashcodes);
6528 return FALSE;
6529 }
6530
6531 bucketcount
6532 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6533
6534 if (bucketcount == 0)
6535 {
6536 free (cinfo.hashcodes);
6537 return FALSE;
6538 }
6539
6540 s = bfd_get_linker_section (dynobj, ".gnu.hash");
6541 BFD_ASSERT (s != NULL);
6542
6543 if (cinfo.nsyms == 0)
6544 {
6545 /* Empty .gnu.hash section is special. */
6546 BFD_ASSERT (cinfo.min_dynindx == -1);
6547 free (cinfo.hashcodes);
6548 s->size = 5 * 4 + bed->s->arch_size / 8;
6549 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6550 if (contents == NULL)
6551 return FALSE;
6552 s->contents = contents;
6553 /* 1 empty bucket. */
6554 bfd_put_32 (output_bfd, 1, contents);
6555 /* SYMIDX above the special symbol 0. */
6556 bfd_put_32 (output_bfd, 1, contents + 4);
6557 /* Just one word for bitmask. */
6558 bfd_put_32 (output_bfd, 1, contents + 8);
6559 /* Only hash fn bloom filter. */
6560 bfd_put_32 (output_bfd, 0, contents + 12);
6561 /* No hashes are valid - empty bitmask. */
6562 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6563 /* No hashes in the only bucket. */
6564 bfd_put_32 (output_bfd, 0,
6565 contents + 16 + bed->s->arch_size / 8);
6566 }
6567 else
6568 {
6569 unsigned long int maskwords, maskbitslog2, x;
6570 BFD_ASSERT (cinfo.min_dynindx != -1);
6571
6572 x = cinfo.nsyms;
6573 maskbitslog2 = 1;
6574 while ((x >>= 1) != 0)
6575 ++maskbitslog2;
6576 if (maskbitslog2 < 3)
6577 maskbitslog2 = 5;
6578 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6579 maskbitslog2 = maskbitslog2 + 3;
6580 else
6581 maskbitslog2 = maskbitslog2 + 2;
6582 if (bed->s->arch_size == 64)
6583 {
6584 if (maskbitslog2 == 5)
6585 maskbitslog2 = 6;
6586 cinfo.shift1 = 6;
6587 }
6588 else
6589 cinfo.shift1 = 5;
6590 cinfo.mask = (1 << cinfo.shift1) - 1;
6591 cinfo.shift2 = maskbitslog2;
6592 cinfo.maskbits = 1 << maskbitslog2;
6593 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6594 amt = bucketcount * sizeof (unsigned long int) * 2;
6595 amt += maskwords * sizeof (bfd_vma);
6596 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
6597 if (cinfo.bitmask == NULL)
6598 {
6599 free (cinfo.hashcodes);
6600 return FALSE;
6601 }
6602
6603 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
6604 cinfo.indx = cinfo.counts + bucketcount;
6605 cinfo.symindx = dynsymcount - cinfo.nsyms;
6606 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6607
6608 /* Determine how often each hash bucket is used. */
6609 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6610 for (i = 0; i < cinfo.nsyms; ++i)
6611 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6612
6613 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6614 if (cinfo.counts[i] != 0)
6615 {
6616 cinfo.indx[i] = cnt;
6617 cnt += cinfo.counts[i];
6618 }
6619 BFD_ASSERT (cnt == dynsymcount);
6620 cinfo.bucketcount = bucketcount;
6621 cinfo.local_indx = cinfo.min_dynindx;
6622
6623 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6624 s->size += cinfo.maskbits / 8;
6625 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6626 if (contents == NULL)
6627 {
6628 free (cinfo.bitmask);
6629 free (cinfo.hashcodes);
6630 return FALSE;
6631 }
6632
6633 s->contents = contents;
6634 bfd_put_32 (output_bfd, bucketcount, contents);
6635 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6636 bfd_put_32 (output_bfd, maskwords, contents + 8);
6637 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6638 contents += 16 + cinfo.maskbits / 8;
6639
6640 for (i = 0; i < bucketcount; ++i)
6641 {
6642 if (cinfo.counts[i] == 0)
6643 bfd_put_32 (output_bfd, 0, contents);
6644 else
6645 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6646 contents += 4;
6647 }
6648
6649 cinfo.contents = contents;
6650
6651 /* Renumber dynamic symbols, populate .gnu.hash section. */
6652 elf_link_hash_traverse (elf_hash_table (info),
6653 elf_renumber_gnu_hash_syms, &cinfo);
6654
6655 contents = s->contents + 16;
6656 for (i = 0; i < maskwords; ++i)
6657 {
6658 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6659 contents);
6660 contents += bed->s->arch_size / 8;
6661 }
6662
6663 free (cinfo.bitmask);
6664 free (cinfo.hashcodes);
6665 }
6666 }
6667
6668 s = bfd_get_linker_section (dynobj, ".dynstr");
6669 BFD_ASSERT (s != NULL);
6670
6671 elf_finalize_dynstr (output_bfd, info);
6672
6673 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6674
6675 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6676 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6677 return FALSE;
6678 }
6679
6680 return TRUE;
6681 }
6682 \f
6683 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
6684
6685 static void
6686 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6687 asection *sec)
6688 {
6689 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
6690 sec->sec_info_type = SEC_INFO_TYPE_NONE;
6691 }
6692
6693 /* Finish SHF_MERGE section merging. */
6694
6695 bfd_boolean
6696 _bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info)
6697 {
6698 bfd *ibfd;
6699 asection *sec;
6700
6701 if (!is_elf_hash_table (info->hash))
6702 return FALSE;
6703
6704 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6705 if ((ibfd->flags & DYNAMIC) == 0)
6706 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6707 if ((sec->flags & SEC_MERGE) != 0
6708 && !bfd_is_abs_section (sec->output_section))
6709 {
6710 struct bfd_elf_section_data *secdata;
6711
6712 secdata = elf_section_data (sec);
6713 if (! _bfd_add_merge_section (abfd,
6714 &elf_hash_table (info)->merge_info,
6715 sec, &secdata->sec_info))
6716 return FALSE;
6717 else if (secdata->sec_info)
6718 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
6719 }
6720
6721 if (elf_hash_table (info)->merge_info != NULL)
6722 _bfd_merge_sections (abfd, info, elf_hash_table (info)->merge_info,
6723 merge_sections_remove_hook);
6724 return TRUE;
6725 }
6726
6727 /* Create an entry in an ELF linker hash table. */
6728
6729 struct bfd_hash_entry *
6730 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6731 struct bfd_hash_table *table,
6732 const char *string)
6733 {
6734 /* Allocate the structure if it has not already been allocated by a
6735 subclass. */
6736 if (entry == NULL)
6737 {
6738 entry = (struct bfd_hash_entry *)
6739 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
6740 if (entry == NULL)
6741 return entry;
6742 }
6743
6744 /* Call the allocation method of the superclass. */
6745 entry = _bfd_link_hash_newfunc (entry, table, string);
6746 if (entry != NULL)
6747 {
6748 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6749 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6750
6751 /* Set local fields. */
6752 ret->indx = -1;
6753 ret->dynindx = -1;
6754 ret->got = htab->init_got_refcount;
6755 ret->plt = htab->init_plt_refcount;
6756 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6757 - offsetof (struct elf_link_hash_entry, size)));
6758 /* Assume that we have been called by a non-ELF symbol reader.
6759 This flag is then reset by the code which reads an ELF input
6760 file. This ensures that a symbol created by a non-ELF symbol
6761 reader will have the flag set correctly. */
6762 ret->non_elf = 1;
6763 }
6764
6765 return entry;
6766 }
6767
6768 /* Copy data from an indirect symbol to its direct symbol, hiding the
6769 old indirect symbol. Also used for copying flags to a weakdef. */
6770
6771 void
6772 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
6773 struct elf_link_hash_entry *dir,
6774 struct elf_link_hash_entry *ind)
6775 {
6776 struct elf_link_hash_table *htab;
6777
6778 /* Copy down any references that we may have already seen to the
6779 symbol which just became indirect. */
6780
6781 dir->ref_dynamic |= ind->ref_dynamic;
6782 dir->ref_regular |= ind->ref_regular;
6783 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
6784 dir->non_got_ref |= ind->non_got_ref;
6785 dir->needs_plt |= ind->needs_plt;
6786 dir->pointer_equality_needed |= ind->pointer_equality_needed;
6787
6788 if (ind->root.type != bfd_link_hash_indirect)
6789 return;
6790
6791 /* Copy over the global and procedure linkage table refcount entries.
6792 These may have been already set up by a check_relocs routine. */
6793 htab = elf_hash_table (info);
6794 if (ind->got.refcount > htab->init_got_refcount.refcount)
6795 {
6796 if (dir->got.refcount < 0)
6797 dir->got.refcount = 0;
6798 dir->got.refcount += ind->got.refcount;
6799 ind->got.refcount = htab->init_got_refcount.refcount;
6800 }
6801
6802 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
6803 {
6804 if (dir->plt.refcount < 0)
6805 dir->plt.refcount = 0;
6806 dir->plt.refcount += ind->plt.refcount;
6807 ind->plt.refcount = htab->init_plt_refcount.refcount;
6808 }
6809
6810 if (ind->dynindx != -1)
6811 {
6812 if (dir->dynindx != -1)
6813 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
6814 dir->dynindx = ind->dynindx;
6815 dir->dynstr_index = ind->dynstr_index;
6816 ind->dynindx = -1;
6817 ind->dynstr_index = 0;
6818 }
6819 }
6820
6821 void
6822 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
6823 struct elf_link_hash_entry *h,
6824 bfd_boolean force_local)
6825 {
6826 /* STT_GNU_IFUNC symbol must go through PLT. */
6827 if (h->type != STT_GNU_IFUNC)
6828 {
6829 h->plt = elf_hash_table (info)->init_plt_offset;
6830 h->needs_plt = 0;
6831 }
6832 if (force_local)
6833 {
6834 h->forced_local = 1;
6835 if (h->dynindx != -1)
6836 {
6837 h->dynindx = -1;
6838 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
6839 h->dynstr_index);
6840 }
6841 }
6842 }
6843
6844 /* Initialize an ELF linker hash table. */
6845
6846 bfd_boolean
6847 _bfd_elf_link_hash_table_init
6848 (struct elf_link_hash_table *table,
6849 bfd *abfd,
6850 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
6851 struct bfd_hash_table *,
6852 const char *),
6853 unsigned int entsize,
6854 enum elf_target_id target_id)
6855 {
6856 bfd_boolean ret;
6857 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
6858
6859 memset (table, 0, sizeof * table);
6860 table->init_got_refcount.refcount = can_refcount - 1;
6861 table->init_plt_refcount.refcount = can_refcount - 1;
6862 table->init_got_offset.offset = -(bfd_vma) 1;
6863 table->init_plt_offset.offset = -(bfd_vma) 1;
6864 /* The first dynamic symbol is a dummy. */
6865 table->dynsymcount = 1;
6866
6867 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
6868
6869 table->root.type = bfd_link_elf_hash_table;
6870 table->hash_table_id = target_id;
6871
6872 return ret;
6873 }
6874
6875 /* Create an ELF linker hash table. */
6876
6877 struct bfd_link_hash_table *
6878 _bfd_elf_link_hash_table_create (bfd *abfd)
6879 {
6880 struct elf_link_hash_table *ret;
6881 bfd_size_type amt = sizeof (struct elf_link_hash_table);
6882
6883 ret = (struct elf_link_hash_table *) bfd_malloc (amt);
6884 if (ret == NULL)
6885 return NULL;
6886
6887 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
6888 sizeof (struct elf_link_hash_entry),
6889 GENERIC_ELF_DATA))
6890 {
6891 free (ret);
6892 return NULL;
6893 }
6894
6895 return &ret->root;
6896 }
6897
6898 /* This is a hook for the ELF emulation code in the generic linker to
6899 tell the backend linker what file name to use for the DT_NEEDED
6900 entry for a dynamic object. */
6901
6902 void
6903 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
6904 {
6905 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6906 && bfd_get_format (abfd) == bfd_object)
6907 elf_dt_name (abfd) = name;
6908 }
6909
6910 int
6911 bfd_elf_get_dyn_lib_class (bfd *abfd)
6912 {
6913 int lib_class;
6914 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6915 && bfd_get_format (abfd) == bfd_object)
6916 lib_class = elf_dyn_lib_class (abfd);
6917 else
6918 lib_class = 0;
6919 return lib_class;
6920 }
6921
6922 void
6923 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
6924 {
6925 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6926 && bfd_get_format (abfd) == bfd_object)
6927 elf_dyn_lib_class (abfd) = lib_class;
6928 }
6929
6930 /* Get the list of DT_NEEDED entries for a link. This is a hook for
6931 the linker ELF emulation code. */
6932
6933 struct bfd_link_needed_list *
6934 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
6935 struct bfd_link_info *info)
6936 {
6937 if (! is_elf_hash_table (info->hash))
6938 return NULL;
6939 return elf_hash_table (info)->needed;
6940 }
6941
6942 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
6943 hook for the linker ELF emulation code. */
6944
6945 struct bfd_link_needed_list *
6946 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
6947 struct bfd_link_info *info)
6948 {
6949 if (! is_elf_hash_table (info->hash))
6950 return NULL;
6951 return elf_hash_table (info)->runpath;
6952 }
6953
6954 /* Get the name actually used for a dynamic object for a link. This
6955 is the SONAME entry if there is one. Otherwise, it is the string
6956 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
6957
6958 const char *
6959 bfd_elf_get_dt_soname (bfd *abfd)
6960 {
6961 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6962 && bfd_get_format (abfd) == bfd_object)
6963 return elf_dt_name (abfd);
6964 return NULL;
6965 }
6966
6967 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
6968 the ELF linker emulation code. */
6969
6970 bfd_boolean
6971 bfd_elf_get_bfd_needed_list (bfd *abfd,
6972 struct bfd_link_needed_list **pneeded)
6973 {
6974 asection *s;
6975 bfd_byte *dynbuf = NULL;
6976 unsigned int elfsec;
6977 unsigned long shlink;
6978 bfd_byte *extdyn, *extdynend;
6979 size_t extdynsize;
6980 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
6981
6982 *pneeded = NULL;
6983
6984 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
6985 || bfd_get_format (abfd) != bfd_object)
6986 return TRUE;
6987
6988 s = bfd_get_section_by_name (abfd, ".dynamic");
6989 if (s == NULL || s->size == 0)
6990 return TRUE;
6991
6992 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
6993 goto error_return;
6994
6995 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
6996 if (elfsec == SHN_BAD)
6997 goto error_return;
6998
6999 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7000
7001 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7002 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7003
7004 extdyn = dynbuf;
7005 extdynend = extdyn + s->size;
7006 for (; extdyn < extdynend; extdyn += extdynsize)
7007 {
7008 Elf_Internal_Dyn dyn;
7009
7010 (*swap_dyn_in) (abfd, extdyn, &dyn);
7011
7012 if (dyn.d_tag == DT_NULL)
7013 break;
7014
7015 if (dyn.d_tag == DT_NEEDED)
7016 {
7017 const char *string;
7018 struct bfd_link_needed_list *l;
7019 unsigned int tagv = dyn.d_un.d_val;
7020 bfd_size_type amt;
7021
7022 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7023 if (string == NULL)
7024 goto error_return;
7025
7026 amt = sizeof *l;
7027 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7028 if (l == NULL)
7029 goto error_return;
7030
7031 l->by = abfd;
7032 l->name = string;
7033 l->next = *pneeded;
7034 *pneeded = l;
7035 }
7036 }
7037
7038 free (dynbuf);
7039
7040 return TRUE;
7041
7042 error_return:
7043 if (dynbuf != NULL)
7044 free (dynbuf);
7045 return FALSE;
7046 }
7047
7048 struct elf_symbuf_symbol
7049 {
7050 unsigned long st_name; /* Symbol name, index in string tbl */
7051 unsigned char st_info; /* Type and binding attributes */
7052 unsigned char st_other; /* Visibilty, and target specific */
7053 };
7054
7055 struct elf_symbuf_head
7056 {
7057 struct elf_symbuf_symbol *ssym;
7058 bfd_size_type count;
7059 unsigned int st_shndx;
7060 };
7061
7062 struct elf_symbol
7063 {
7064 union
7065 {
7066 Elf_Internal_Sym *isym;
7067 struct elf_symbuf_symbol *ssym;
7068 } u;
7069 const char *name;
7070 };
7071
7072 /* Sort references to symbols by ascending section number. */
7073
7074 static int
7075 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7076 {
7077 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7078 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7079
7080 return s1->st_shndx - s2->st_shndx;
7081 }
7082
7083 static int
7084 elf_sym_name_compare (const void *arg1, const void *arg2)
7085 {
7086 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7087 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7088 return strcmp (s1->name, s2->name);
7089 }
7090
7091 static struct elf_symbuf_head *
7092 elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
7093 {
7094 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7095 struct elf_symbuf_symbol *ssym;
7096 struct elf_symbuf_head *ssymbuf, *ssymhead;
7097 bfd_size_type i, shndx_count, total_size;
7098
7099 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7100 if (indbuf == NULL)
7101 return NULL;
7102
7103 for (ind = indbuf, i = 0; i < symcount; i++)
7104 if (isymbuf[i].st_shndx != SHN_UNDEF)
7105 *ind++ = &isymbuf[i];
7106 indbufend = ind;
7107
7108 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7109 elf_sort_elf_symbol);
7110
7111 shndx_count = 0;
7112 if (indbufend > indbuf)
7113 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7114 if (ind[0]->st_shndx != ind[1]->st_shndx)
7115 shndx_count++;
7116
7117 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7118 + (indbufend - indbuf) * sizeof (*ssym));
7119 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7120 if (ssymbuf == NULL)
7121 {
7122 free (indbuf);
7123 return NULL;
7124 }
7125
7126 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7127 ssymbuf->ssym = NULL;
7128 ssymbuf->count = shndx_count;
7129 ssymbuf->st_shndx = 0;
7130 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7131 {
7132 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7133 {
7134 ssymhead++;
7135 ssymhead->ssym = ssym;
7136 ssymhead->count = 0;
7137 ssymhead->st_shndx = (*ind)->st_shndx;
7138 }
7139 ssym->st_name = (*ind)->st_name;
7140 ssym->st_info = (*ind)->st_info;
7141 ssym->st_other = (*ind)->st_other;
7142 ssymhead->count++;
7143 }
7144 BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
7145 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7146 == total_size));
7147
7148 free (indbuf);
7149 return ssymbuf;
7150 }
7151
7152 /* Check if 2 sections define the same set of local and global
7153 symbols. */
7154
7155 static bfd_boolean
7156 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7157 struct bfd_link_info *info)
7158 {
7159 bfd *bfd1, *bfd2;
7160 const struct elf_backend_data *bed1, *bed2;
7161 Elf_Internal_Shdr *hdr1, *hdr2;
7162 bfd_size_type symcount1, symcount2;
7163 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7164 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7165 Elf_Internal_Sym *isym, *isymend;
7166 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7167 bfd_size_type count1, count2, i;
7168 unsigned int shndx1, shndx2;
7169 bfd_boolean result;
7170
7171 bfd1 = sec1->owner;
7172 bfd2 = sec2->owner;
7173
7174 /* Both sections have to be in ELF. */
7175 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7176 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7177 return FALSE;
7178
7179 if (elf_section_type (sec1) != elf_section_type (sec2))
7180 return FALSE;
7181
7182 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7183 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7184 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7185 return FALSE;
7186
7187 bed1 = get_elf_backend_data (bfd1);
7188 bed2 = get_elf_backend_data (bfd2);
7189 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7190 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7191 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7192 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7193
7194 if (symcount1 == 0 || symcount2 == 0)
7195 return FALSE;
7196
7197 result = FALSE;
7198 isymbuf1 = NULL;
7199 isymbuf2 = NULL;
7200 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7201 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7202
7203 if (ssymbuf1 == NULL)
7204 {
7205 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7206 NULL, NULL, NULL);
7207 if (isymbuf1 == NULL)
7208 goto done;
7209
7210 if (!info->reduce_memory_overheads)
7211 elf_tdata (bfd1)->symbuf = ssymbuf1
7212 = elf_create_symbuf (symcount1, isymbuf1);
7213 }
7214
7215 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7216 {
7217 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7218 NULL, NULL, NULL);
7219 if (isymbuf2 == NULL)
7220 goto done;
7221
7222 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7223 elf_tdata (bfd2)->symbuf = ssymbuf2
7224 = elf_create_symbuf (symcount2, isymbuf2);
7225 }
7226
7227 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7228 {
7229 /* Optimized faster version. */
7230 bfd_size_type lo, hi, mid;
7231 struct elf_symbol *symp;
7232 struct elf_symbuf_symbol *ssym, *ssymend;
7233
7234 lo = 0;
7235 hi = ssymbuf1->count;
7236 ssymbuf1++;
7237 count1 = 0;
7238 while (lo < hi)
7239 {
7240 mid = (lo + hi) / 2;
7241 if (shndx1 < ssymbuf1[mid].st_shndx)
7242 hi = mid;
7243 else if (shndx1 > ssymbuf1[mid].st_shndx)
7244 lo = mid + 1;
7245 else
7246 {
7247 count1 = ssymbuf1[mid].count;
7248 ssymbuf1 += mid;
7249 break;
7250 }
7251 }
7252
7253 lo = 0;
7254 hi = ssymbuf2->count;
7255 ssymbuf2++;
7256 count2 = 0;
7257 while (lo < hi)
7258 {
7259 mid = (lo + hi) / 2;
7260 if (shndx2 < ssymbuf2[mid].st_shndx)
7261 hi = mid;
7262 else if (shndx2 > ssymbuf2[mid].st_shndx)
7263 lo = mid + 1;
7264 else
7265 {
7266 count2 = ssymbuf2[mid].count;
7267 ssymbuf2 += mid;
7268 break;
7269 }
7270 }
7271
7272 if (count1 == 0 || count2 == 0 || count1 != count2)
7273 goto done;
7274
7275 symtable1 = (struct elf_symbol *)
7276 bfd_malloc (count1 * sizeof (struct elf_symbol));
7277 symtable2 = (struct elf_symbol *)
7278 bfd_malloc (count2 * sizeof (struct elf_symbol));
7279 if (symtable1 == NULL || symtable2 == NULL)
7280 goto done;
7281
7282 symp = symtable1;
7283 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7284 ssym < ssymend; ssym++, symp++)
7285 {
7286 symp->u.ssym = ssym;
7287 symp->name = bfd_elf_string_from_elf_section (bfd1,
7288 hdr1->sh_link,
7289 ssym->st_name);
7290 }
7291
7292 symp = symtable2;
7293 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7294 ssym < ssymend; ssym++, symp++)
7295 {
7296 symp->u.ssym = ssym;
7297 symp->name = bfd_elf_string_from_elf_section (bfd2,
7298 hdr2->sh_link,
7299 ssym->st_name);
7300 }
7301
7302 /* Sort symbol by name. */
7303 qsort (symtable1, count1, sizeof (struct elf_symbol),
7304 elf_sym_name_compare);
7305 qsort (symtable2, count1, sizeof (struct elf_symbol),
7306 elf_sym_name_compare);
7307
7308 for (i = 0; i < count1; i++)
7309 /* Two symbols must have the same binding, type and name. */
7310 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7311 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7312 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7313 goto done;
7314
7315 result = TRUE;
7316 goto done;
7317 }
7318
7319 symtable1 = (struct elf_symbol *)
7320 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7321 symtable2 = (struct elf_symbol *)
7322 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7323 if (symtable1 == NULL || symtable2 == NULL)
7324 goto done;
7325
7326 /* Count definitions in the section. */
7327 count1 = 0;
7328 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7329 if (isym->st_shndx == shndx1)
7330 symtable1[count1++].u.isym = isym;
7331
7332 count2 = 0;
7333 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7334 if (isym->st_shndx == shndx2)
7335 symtable2[count2++].u.isym = isym;
7336
7337 if (count1 == 0 || count2 == 0 || count1 != count2)
7338 goto done;
7339
7340 for (i = 0; i < count1; i++)
7341 symtable1[i].name
7342 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7343 symtable1[i].u.isym->st_name);
7344
7345 for (i = 0; i < count2; i++)
7346 symtable2[i].name
7347 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7348 symtable2[i].u.isym->st_name);
7349
7350 /* Sort symbol by name. */
7351 qsort (symtable1, count1, sizeof (struct elf_symbol),
7352 elf_sym_name_compare);
7353 qsort (symtable2, count1, sizeof (struct elf_symbol),
7354 elf_sym_name_compare);
7355
7356 for (i = 0; i < count1; i++)
7357 /* Two symbols must have the same binding, type and name. */
7358 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7359 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7360 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7361 goto done;
7362
7363 result = TRUE;
7364
7365 done:
7366 if (symtable1)
7367 free (symtable1);
7368 if (symtable2)
7369 free (symtable2);
7370 if (isymbuf1)
7371 free (isymbuf1);
7372 if (isymbuf2)
7373 free (isymbuf2);
7374
7375 return result;
7376 }
7377
7378 /* Return TRUE if 2 section types are compatible. */
7379
7380 bfd_boolean
7381 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7382 bfd *bbfd, const asection *bsec)
7383 {
7384 if (asec == NULL
7385 || bsec == NULL
7386 || abfd->xvec->flavour != bfd_target_elf_flavour
7387 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7388 return TRUE;
7389
7390 return elf_section_type (asec) == elf_section_type (bsec);
7391 }
7392 \f
7393 /* Final phase of ELF linker. */
7394
7395 /* A structure we use to avoid passing large numbers of arguments. */
7396
7397 struct elf_final_link_info
7398 {
7399 /* General link information. */
7400 struct bfd_link_info *info;
7401 /* Output BFD. */
7402 bfd *output_bfd;
7403 /* Symbol string table. */
7404 struct bfd_strtab_hash *symstrtab;
7405 /* .dynsym section. */
7406 asection *dynsym_sec;
7407 /* .hash section. */
7408 asection *hash_sec;
7409 /* symbol version section (.gnu.version). */
7410 asection *symver_sec;
7411 /* Buffer large enough to hold contents of any section. */
7412 bfd_byte *contents;
7413 /* Buffer large enough to hold external relocs of any section. */
7414 void *external_relocs;
7415 /* Buffer large enough to hold internal relocs of any section. */
7416 Elf_Internal_Rela *internal_relocs;
7417 /* Buffer large enough to hold external local symbols of any input
7418 BFD. */
7419 bfd_byte *external_syms;
7420 /* And a buffer for symbol section indices. */
7421 Elf_External_Sym_Shndx *locsym_shndx;
7422 /* Buffer large enough to hold internal local symbols of any input
7423 BFD. */
7424 Elf_Internal_Sym *internal_syms;
7425 /* Array large enough to hold a symbol index for each local symbol
7426 of any input BFD. */
7427 long *indices;
7428 /* Array large enough to hold a section pointer for each local
7429 symbol of any input BFD. */
7430 asection **sections;
7431 /* Buffer to hold swapped out symbols. */
7432 bfd_byte *symbuf;
7433 /* And one for symbol section indices. */
7434 Elf_External_Sym_Shndx *symshndxbuf;
7435 /* Number of swapped out symbols in buffer. */
7436 size_t symbuf_count;
7437 /* Number of symbols which fit in symbuf. */
7438 size_t symbuf_size;
7439 /* And same for symshndxbuf. */
7440 size_t shndxbuf_size;
7441 /* Number of STT_FILE syms seen. */
7442 size_t filesym_count;
7443 };
7444
7445 /* This struct is used to pass information to elf_link_output_extsym. */
7446
7447 struct elf_outext_info
7448 {
7449 bfd_boolean failed;
7450 bfd_boolean localsyms;
7451 bfd_boolean need_second_pass;
7452 bfd_boolean second_pass;
7453 struct elf_final_link_info *flinfo;
7454 };
7455
7456
7457 /* Support for evaluating a complex relocation.
7458
7459 Complex relocations are generalized, self-describing relocations. The
7460 implementation of them consists of two parts: complex symbols, and the
7461 relocations themselves.
7462
7463 The relocations are use a reserved elf-wide relocation type code (R_RELC
7464 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7465 information (start bit, end bit, word width, etc) into the addend. This
7466 information is extracted from CGEN-generated operand tables within gas.
7467
7468 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7469 internal) representing prefix-notation expressions, including but not
7470 limited to those sorts of expressions normally encoded as addends in the
7471 addend field. The symbol mangling format is:
7472
7473 <node> := <literal>
7474 | <unary-operator> ':' <node>
7475 | <binary-operator> ':' <node> ':' <node>
7476 ;
7477
7478 <literal> := 's' <digits=N> ':' <N character symbol name>
7479 | 'S' <digits=N> ':' <N character section name>
7480 | '#' <hexdigits>
7481 ;
7482
7483 <binary-operator> := as in C
7484 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7485
7486 static void
7487 set_symbol_value (bfd *bfd_with_globals,
7488 Elf_Internal_Sym *isymbuf,
7489 size_t locsymcount,
7490 size_t symidx,
7491 bfd_vma val)
7492 {
7493 struct elf_link_hash_entry **sym_hashes;
7494 struct elf_link_hash_entry *h;
7495 size_t extsymoff = locsymcount;
7496
7497 if (symidx < locsymcount)
7498 {
7499 Elf_Internal_Sym *sym;
7500
7501 sym = isymbuf + symidx;
7502 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7503 {
7504 /* It is a local symbol: move it to the
7505 "absolute" section and give it a value. */
7506 sym->st_shndx = SHN_ABS;
7507 sym->st_value = val;
7508 return;
7509 }
7510 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7511 extsymoff = 0;
7512 }
7513
7514 /* It is a global symbol: set its link type
7515 to "defined" and give it a value. */
7516
7517 sym_hashes = elf_sym_hashes (bfd_with_globals);
7518 h = sym_hashes [symidx - extsymoff];
7519 while (h->root.type == bfd_link_hash_indirect
7520 || h->root.type == bfd_link_hash_warning)
7521 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7522 h->root.type = bfd_link_hash_defined;
7523 h->root.u.def.value = val;
7524 h->root.u.def.section = bfd_abs_section_ptr;
7525 }
7526
7527 static bfd_boolean
7528 resolve_symbol (const char *name,
7529 bfd *input_bfd,
7530 struct elf_final_link_info *flinfo,
7531 bfd_vma *result,
7532 Elf_Internal_Sym *isymbuf,
7533 size_t locsymcount)
7534 {
7535 Elf_Internal_Sym *sym;
7536 struct bfd_link_hash_entry *global_entry;
7537 const char *candidate = NULL;
7538 Elf_Internal_Shdr *symtab_hdr;
7539 size_t i;
7540
7541 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7542
7543 for (i = 0; i < locsymcount; ++ i)
7544 {
7545 sym = isymbuf + i;
7546
7547 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7548 continue;
7549
7550 candidate = bfd_elf_string_from_elf_section (input_bfd,
7551 symtab_hdr->sh_link,
7552 sym->st_name);
7553 #ifdef DEBUG
7554 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7555 name, candidate, (unsigned long) sym->st_value);
7556 #endif
7557 if (candidate && strcmp (candidate, name) == 0)
7558 {
7559 asection *sec = flinfo->sections [i];
7560
7561 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7562 *result += sec->output_offset + sec->output_section->vma;
7563 #ifdef DEBUG
7564 printf ("Found symbol with value %8.8lx\n",
7565 (unsigned long) *result);
7566 #endif
7567 return TRUE;
7568 }
7569 }
7570
7571 /* Hmm, haven't found it yet. perhaps it is a global. */
7572 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
7573 FALSE, FALSE, TRUE);
7574 if (!global_entry)
7575 return FALSE;
7576
7577 if (global_entry->type == bfd_link_hash_defined
7578 || global_entry->type == bfd_link_hash_defweak)
7579 {
7580 *result = (global_entry->u.def.value
7581 + global_entry->u.def.section->output_section->vma
7582 + global_entry->u.def.section->output_offset);
7583 #ifdef DEBUG
7584 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7585 global_entry->root.string, (unsigned long) *result);
7586 #endif
7587 return TRUE;
7588 }
7589
7590 return FALSE;
7591 }
7592
7593 static bfd_boolean
7594 resolve_section (const char *name,
7595 asection *sections,
7596 bfd_vma *result)
7597 {
7598 asection *curr;
7599 unsigned int len;
7600
7601 for (curr = sections; curr; curr = curr->next)
7602 if (strcmp (curr->name, name) == 0)
7603 {
7604 *result = curr->vma;
7605 return TRUE;
7606 }
7607
7608 /* Hmm. still haven't found it. try pseudo-section names. */
7609 for (curr = sections; curr; curr = curr->next)
7610 {
7611 len = strlen (curr->name);
7612 if (len > strlen (name))
7613 continue;
7614
7615 if (strncmp (curr->name, name, len) == 0)
7616 {
7617 if (strncmp (".end", name + len, 4) == 0)
7618 {
7619 *result = curr->vma + curr->size;
7620 return TRUE;
7621 }
7622
7623 /* Insert more pseudo-section names here, if you like. */
7624 }
7625 }
7626
7627 return FALSE;
7628 }
7629
7630 static void
7631 undefined_reference (const char *reftype, const char *name)
7632 {
7633 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7634 reftype, name);
7635 }
7636
7637 static bfd_boolean
7638 eval_symbol (bfd_vma *result,
7639 const char **symp,
7640 bfd *input_bfd,
7641 struct elf_final_link_info *flinfo,
7642 bfd_vma dot,
7643 Elf_Internal_Sym *isymbuf,
7644 size_t locsymcount,
7645 int signed_p)
7646 {
7647 size_t len;
7648 size_t symlen;
7649 bfd_vma a;
7650 bfd_vma b;
7651 char symbuf[4096];
7652 const char *sym = *symp;
7653 const char *symend;
7654 bfd_boolean symbol_is_section = FALSE;
7655
7656 len = strlen (sym);
7657 symend = sym + len;
7658
7659 if (len < 1 || len > sizeof (symbuf))
7660 {
7661 bfd_set_error (bfd_error_invalid_operation);
7662 return FALSE;
7663 }
7664
7665 switch (* sym)
7666 {
7667 case '.':
7668 *result = dot;
7669 *symp = sym + 1;
7670 return TRUE;
7671
7672 case '#':
7673 ++sym;
7674 *result = strtoul (sym, (char **) symp, 16);
7675 return TRUE;
7676
7677 case 'S':
7678 symbol_is_section = TRUE;
7679 case 's':
7680 ++sym;
7681 symlen = strtol (sym, (char **) symp, 10);
7682 sym = *symp + 1; /* Skip the trailing ':'. */
7683
7684 if (symend < sym || symlen + 1 > sizeof (symbuf))
7685 {
7686 bfd_set_error (bfd_error_invalid_operation);
7687 return FALSE;
7688 }
7689
7690 memcpy (symbuf, sym, symlen);
7691 symbuf[symlen] = '\0';
7692 *symp = sym + symlen;
7693
7694 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7695 the symbol as a section, or vice-versa. so we're pretty liberal in our
7696 interpretation here; section means "try section first", not "must be a
7697 section", and likewise with symbol. */
7698
7699 if (symbol_is_section)
7700 {
7701 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result)
7702 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
7703 isymbuf, locsymcount))
7704 {
7705 undefined_reference ("section", symbuf);
7706 return FALSE;
7707 }
7708 }
7709 else
7710 {
7711 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
7712 isymbuf, locsymcount)
7713 && !resolve_section (symbuf, flinfo->output_bfd->sections,
7714 result))
7715 {
7716 undefined_reference ("symbol", symbuf);
7717 return FALSE;
7718 }
7719 }
7720
7721 return TRUE;
7722
7723 /* All that remains are operators. */
7724
7725 #define UNARY_OP(op) \
7726 if (strncmp (sym, #op, strlen (#op)) == 0) \
7727 { \
7728 sym += strlen (#op); \
7729 if (*sym == ':') \
7730 ++sym; \
7731 *symp = sym; \
7732 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
7733 isymbuf, locsymcount, signed_p)) \
7734 return FALSE; \
7735 if (signed_p) \
7736 *result = op ((bfd_signed_vma) a); \
7737 else \
7738 *result = op a; \
7739 return TRUE; \
7740 }
7741
7742 #define BINARY_OP(op) \
7743 if (strncmp (sym, #op, strlen (#op)) == 0) \
7744 { \
7745 sym += strlen (#op); \
7746 if (*sym == ':') \
7747 ++sym; \
7748 *symp = sym; \
7749 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
7750 isymbuf, locsymcount, signed_p)) \
7751 return FALSE; \
7752 ++*symp; \
7753 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
7754 isymbuf, locsymcount, signed_p)) \
7755 return FALSE; \
7756 if (signed_p) \
7757 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
7758 else \
7759 *result = a op b; \
7760 return TRUE; \
7761 }
7762
7763 default:
7764 UNARY_OP (0-);
7765 BINARY_OP (<<);
7766 BINARY_OP (>>);
7767 BINARY_OP (==);
7768 BINARY_OP (!=);
7769 BINARY_OP (<=);
7770 BINARY_OP (>=);
7771 BINARY_OP (&&);
7772 BINARY_OP (||);
7773 UNARY_OP (~);
7774 UNARY_OP (!);
7775 BINARY_OP (*);
7776 BINARY_OP (/);
7777 BINARY_OP (%);
7778 BINARY_OP (^);
7779 BINARY_OP (|);
7780 BINARY_OP (&);
7781 BINARY_OP (+);
7782 BINARY_OP (-);
7783 BINARY_OP (<);
7784 BINARY_OP (>);
7785 #undef UNARY_OP
7786 #undef BINARY_OP
7787 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
7788 bfd_set_error (bfd_error_invalid_operation);
7789 return FALSE;
7790 }
7791 }
7792
7793 static void
7794 put_value (bfd_vma size,
7795 unsigned long chunksz,
7796 bfd *input_bfd,
7797 bfd_vma x,
7798 bfd_byte *location)
7799 {
7800 location += (size - chunksz);
7801
7802 for (; size; size -= chunksz, location -= chunksz, x >>= (chunksz * 8))
7803 {
7804 switch (chunksz)
7805 {
7806 default:
7807 case 0:
7808 abort ();
7809 case 1:
7810 bfd_put_8 (input_bfd, x, location);
7811 break;
7812 case 2:
7813 bfd_put_16 (input_bfd, x, location);
7814 break;
7815 case 4:
7816 bfd_put_32 (input_bfd, x, location);
7817 break;
7818 case 8:
7819 #ifdef BFD64
7820 bfd_put_64 (input_bfd, x, location);
7821 #else
7822 abort ();
7823 #endif
7824 break;
7825 }
7826 }
7827 }
7828
7829 static bfd_vma
7830 get_value (bfd_vma size,
7831 unsigned long chunksz,
7832 bfd *input_bfd,
7833 bfd_byte *location)
7834 {
7835 bfd_vma x = 0;
7836
7837 for (; size; size -= chunksz, location += chunksz)
7838 {
7839 switch (chunksz)
7840 {
7841 default:
7842 case 0:
7843 abort ();
7844 case 1:
7845 x = (x << (8 * chunksz)) | bfd_get_8 (input_bfd, location);
7846 break;
7847 case 2:
7848 x = (x << (8 * chunksz)) | bfd_get_16 (input_bfd, location);
7849 break;
7850 case 4:
7851 x = (x << (8 * chunksz)) | bfd_get_32 (input_bfd, location);
7852 break;
7853 case 8:
7854 #ifdef BFD64
7855 x = (x << (8 * chunksz)) | bfd_get_64 (input_bfd, location);
7856 #else
7857 abort ();
7858 #endif
7859 break;
7860 }
7861 }
7862 return x;
7863 }
7864
7865 static void
7866 decode_complex_addend (unsigned long *start, /* in bits */
7867 unsigned long *oplen, /* in bits */
7868 unsigned long *len, /* in bits */
7869 unsigned long *wordsz, /* in bytes */
7870 unsigned long *chunksz, /* in bytes */
7871 unsigned long *lsb0_p,
7872 unsigned long *signed_p,
7873 unsigned long *trunc_p,
7874 unsigned long encoded)
7875 {
7876 * start = encoded & 0x3F;
7877 * len = (encoded >> 6) & 0x3F;
7878 * oplen = (encoded >> 12) & 0x3F;
7879 * wordsz = (encoded >> 18) & 0xF;
7880 * chunksz = (encoded >> 22) & 0xF;
7881 * lsb0_p = (encoded >> 27) & 1;
7882 * signed_p = (encoded >> 28) & 1;
7883 * trunc_p = (encoded >> 29) & 1;
7884 }
7885
7886 bfd_reloc_status_type
7887 bfd_elf_perform_complex_relocation (bfd *input_bfd,
7888 asection *input_section ATTRIBUTE_UNUSED,
7889 bfd_byte *contents,
7890 Elf_Internal_Rela *rel,
7891 bfd_vma relocation)
7892 {
7893 bfd_vma shift, x, mask;
7894 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
7895 bfd_reloc_status_type r;
7896
7897 /* Perform this reloc, since it is complex.
7898 (this is not to say that it necessarily refers to a complex
7899 symbol; merely that it is a self-describing CGEN based reloc.
7900 i.e. the addend has the complete reloc information (bit start, end,
7901 word size, etc) encoded within it.). */
7902
7903 decode_complex_addend (&start, &oplen, &len, &wordsz,
7904 &chunksz, &lsb0_p, &signed_p,
7905 &trunc_p, rel->r_addend);
7906
7907 mask = (((1L << (len - 1)) - 1) << 1) | 1;
7908
7909 if (lsb0_p)
7910 shift = (start + 1) - len;
7911 else
7912 shift = (8 * wordsz) - (start + len);
7913
7914 /* FIXME: octets_per_byte. */
7915 x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
7916
7917 #ifdef DEBUG
7918 printf ("Doing complex reloc: "
7919 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
7920 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
7921 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
7922 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
7923 oplen, (unsigned long) x, (unsigned long) mask,
7924 (unsigned long) relocation);
7925 #endif
7926
7927 r = bfd_reloc_ok;
7928 if (! trunc_p)
7929 /* Now do an overflow check. */
7930 r = bfd_check_overflow ((signed_p
7931 ? complain_overflow_signed
7932 : complain_overflow_unsigned),
7933 len, 0, (8 * wordsz),
7934 relocation);
7935
7936 /* Do the deed. */
7937 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
7938
7939 #ifdef DEBUG
7940 printf (" relocation: %8.8lx\n"
7941 " shifted mask: %8.8lx\n"
7942 " shifted/masked reloc: %8.8lx\n"
7943 " result: %8.8lx\n",
7944 (unsigned long) relocation, (unsigned long) (mask << shift),
7945 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
7946 #endif
7947 /* FIXME: octets_per_byte. */
7948 put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
7949 return r;
7950 }
7951
7952 /* When performing a relocatable link, the input relocations are
7953 preserved. But, if they reference global symbols, the indices
7954 referenced must be updated. Update all the relocations found in
7955 RELDATA. */
7956
7957 static void
7958 elf_link_adjust_relocs (bfd *abfd,
7959 struct bfd_elf_section_reloc_data *reldata)
7960 {
7961 unsigned int i;
7962 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7963 bfd_byte *erela;
7964 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
7965 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
7966 bfd_vma r_type_mask;
7967 int r_sym_shift;
7968 unsigned int count = reldata->count;
7969 struct elf_link_hash_entry **rel_hash = reldata->hashes;
7970
7971 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
7972 {
7973 swap_in = bed->s->swap_reloc_in;
7974 swap_out = bed->s->swap_reloc_out;
7975 }
7976 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
7977 {
7978 swap_in = bed->s->swap_reloca_in;
7979 swap_out = bed->s->swap_reloca_out;
7980 }
7981 else
7982 abort ();
7983
7984 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
7985 abort ();
7986
7987 if (bed->s->arch_size == 32)
7988 {
7989 r_type_mask = 0xff;
7990 r_sym_shift = 8;
7991 }
7992 else
7993 {
7994 r_type_mask = 0xffffffff;
7995 r_sym_shift = 32;
7996 }
7997
7998 erela = reldata->hdr->contents;
7999 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8000 {
8001 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8002 unsigned int j;
8003
8004 if (*rel_hash == NULL)
8005 continue;
8006
8007 BFD_ASSERT ((*rel_hash)->indx >= 0);
8008
8009 (*swap_in) (abfd, erela, irela);
8010 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8011 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8012 | (irela[j].r_info & r_type_mask));
8013 (*swap_out) (abfd, irela, erela);
8014 }
8015 }
8016
8017 struct elf_link_sort_rela
8018 {
8019 union {
8020 bfd_vma offset;
8021 bfd_vma sym_mask;
8022 } u;
8023 enum elf_reloc_type_class type;
8024 /* We use this as an array of size int_rels_per_ext_rel. */
8025 Elf_Internal_Rela rela[1];
8026 };
8027
8028 static int
8029 elf_link_sort_cmp1 (const void *A, const void *B)
8030 {
8031 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8032 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8033 int relativea, relativeb;
8034
8035 relativea = a->type == reloc_class_relative;
8036 relativeb = b->type == reloc_class_relative;
8037
8038 if (relativea < relativeb)
8039 return 1;
8040 if (relativea > relativeb)
8041 return -1;
8042 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8043 return -1;
8044 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8045 return 1;
8046 if (a->rela->r_offset < b->rela->r_offset)
8047 return -1;
8048 if (a->rela->r_offset > b->rela->r_offset)
8049 return 1;
8050 return 0;
8051 }
8052
8053 static int
8054 elf_link_sort_cmp2 (const void *A, const void *B)
8055 {
8056 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8057 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8058 int copya, copyb;
8059
8060 if (a->u.offset < b->u.offset)
8061 return -1;
8062 if (a->u.offset > b->u.offset)
8063 return 1;
8064 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
8065 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
8066 if (copya < copyb)
8067 return -1;
8068 if (copya > copyb)
8069 return 1;
8070 if (a->rela->r_offset < b->rela->r_offset)
8071 return -1;
8072 if (a->rela->r_offset > b->rela->r_offset)
8073 return 1;
8074 return 0;
8075 }
8076
8077 static size_t
8078 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8079 {
8080 asection *dynamic_relocs;
8081 asection *rela_dyn;
8082 asection *rel_dyn;
8083 bfd_size_type count, size;
8084 size_t i, ret, sort_elt, ext_size;
8085 bfd_byte *sort, *s_non_relative, *p;
8086 struct elf_link_sort_rela *sq;
8087 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8088 int i2e = bed->s->int_rels_per_ext_rel;
8089 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8090 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8091 struct bfd_link_order *lo;
8092 bfd_vma r_sym_mask;
8093 bfd_boolean use_rela;
8094
8095 /* Find a dynamic reloc section. */
8096 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8097 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8098 if (rela_dyn != NULL && rela_dyn->size > 0
8099 && rel_dyn != NULL && rel_dyn->size > 0)
8100 {
8101 bfd_boolean use_rela_initialised = FALSE;
8102
8103 /* This is just here to stop gcc from complaining.
8104 It's initialization checking code is not perfect. */
8105 use_rela = TRUE;
8106
8107 /* Both sections are present. Examine the sizes
8108 of the indirect sections to help us choose. */
8109 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8110 if (lo->type == bfd_indirect_link_order)
8111 {
8112 asection *o = lo->u.indirect.section;
8113
8114 if ((o->size % bed->s->sizeof_rela) == 0)
8115 {
8116 if ((o->size % bed->s->sizeof_rel) == 0)
8117 /* Section size is divisible by both rel and rela sizes.
8118 It is of no help to us. */
8119 ;
8120 else
8121 {
8122 /* Section size is only divisible by rela. */
8123 if (use_rela_initialised && (use_rela == FALSE))
8124 {
8125 _bfd_error_handler
8126 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8127 bfd_set_error (bfd_error_invalid_operation);
8128 return 0;
8129 }
8130 else
8131 {
8132 use_rela = TRUE;
8133 use_rela_initialised = TRUE;
8134 }
8135 }
8136 }
8137 else if ((o->size % bed->s->sizeof_rel) == 0)
8138 {
8139 /* Section size is only divisible by rel. */
8140 if (use_rela_initialised && (use_rela == TRUE))
8141 {
8142 _bfd_error_handler
8143 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8144 bfd_set_error (bfd_error_invalid_operation);
8145 return 0;
8146 }
8147 else
8148 {
8149 use_rela = FALSE;
8150 use_rela_initialised = TRUE;
8151 }
8152 }
8153 else
8154 {
8155 /* The section size is not divisible by either - something is wrong. */
8156 _bfd_error_handler
8157 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8158 bfd_set_error (bfd_error_invalid_operation);
8159 return 0;
8160 }
8161 }
8162
8163 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8164 if (lo->type == bfd_indirect_link_order)
8165 {
8166 asection *o = lo->u.indirect.section;
8167
8168 if ((o->size % bed->s->sizeof_rela) == 0)
8169 {
8170 if ((o->size % bed->s->sizeof_rel) == 0)
8171 /* Section size is divisible by both rel and rela sizes.
8172 It is of no help to us. */
8173 ;
8174 else
8175 {
8176 /* Section size is only divisible by rela. */
8177 if (use_rela_initialised && (use_rela == FALSE))
8178 {
8179 _bfd_error_handler
8180 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8181 bfd_set_error (bfd_error_invalid_operation);
8182 return 0;
8183 }
8184 else
8185 {
8186 use_rela = TRUE;
8187 use_rela_initialised = TRUE;
8188 }
8189 }
8190 }
8191 else if ((o->size % bed->s->sizeof_rel) == 0)
8192 {
8193 /* Section size is only divisible by rel. */
8194 if (use_rela_initialised && (use_rela == TRUE))
8195 {
8196 _bfd_error_handler
8197 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8198 bfd_set_error (bfd_error_invalid_operation);
8199 return 0;
8200 }
8201 else
8202 {
8203 use_rela = FALSE;
8204 use_rela_initialised = TRUE;
8205 }
8206 }
8207 else
8208 {
8209 /* The section size is not divisible by either - something is wrong. */
8210 _bfd_error_handler
8211 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8212 bfd_set_error (bfd_error_invalid_operation);
8213 return 0;
8214 }
8215 }
8216
8217 if (! use_rela_initialised)
8218 /* Make a guess. */
8219 use_rela = TRUE;
8220 }
8221 else if (rela_dyn != NULL && rela_dyn->size > 0)
8222 use_rela = TRUE;
8223 else if (rel_dyn != NULL && rel_dyn->size > 0)
8224 use_rela = FALSE;
8225 else
8226 return 0;
8227
8228 if (use_rela)
8229 {
8230 dynamic_relocs = rela_dyn;
8231 ext_size = bed->s->sizeof_rela;
8232 swap_in = bed->s->swap_reloca_in;
8233 swap_out = bed->s->swap_reloca_out;
8234 }
8235 else
8236 {
8237 dynamic_relocs = rel_dyn;
8238 ext_size = bed->s->sizeof_rel;
8239 swap_in = bed->s->swap_reloc_in;
8240 swap_out = bed->s->swap_reloc_out;
8241 }
8242
8243 size = 0;
8244 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8245 if (lo->type == bfd_indirect_link_order)
8246 size += lo->u.indirect.section->size;
8247
8248 if (size != dynamic_relocs->size)
8249 return 0;
8250
8251 sort_elt = (sizeof (struct elf_link_sort_rela)
8252 + (i2e - 1) * sizeof (Elf_Internal_Rela));
8253
8254 count = dynamic_relocs->size / ext_size;
8255 if (count == 0)
8256 return 0;
8257 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
8258
8259 if (sort == NULL)
8260 {
8261 (*info->callbacks->warning)
8262 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8263 return 0;
8264 }
8265
8266 if (bed->s->arch_size == 32)
8267 r_sym_mask = ~(bfd_vma) 0xff;
8268 else
8269 r_sym_mask = ~(bfd_vma) 0xffffffff;
8270
8271 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8272 if (lo->type == bfd_indirect_link_order)
8273 {
8274 bfd_byte *erel, *erelend;
8275 asection *o = lo->u.indirect.section;
8276
8277 if (o->contents == NULL && o->size != 0)
8278 {
8279 /* This is a reloc section that is being handled as a normal
8280 section. See bfd_section_from_shdr. We can't combine
8281 relocs in this case. */
8282 free (sort);
8283 return 0;
8284 }
8285 erel = o->contents;
8286 erelend = o->contents + o->size;
8287 /* FIXME: octets_per_byte. */
8288 p = sort + o->output_offset / ext_size * sort_elt;
8289
8290 while (erel < erelend)
8291 {
8292 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8293
8294 (*swap_in) (abfd, erel, s->rela);
8295 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
8296 s->u.sym_mask = r_sym_mask;
8297 p += sort_elt;
8298 erel += ext_size;
8299 }
8300 }
8301
8302 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8303
8304 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8305 {
8306 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8307 if (s->type != reloc_class_relative)
8308 break;
8309 }
8310 ret = i;
8311 s_non_relative = p;
8312
8313 sq = (struct elf_link_sort_rela *) s_non_relative;
8314 for (; i < count; i++, p += sort_elt)
8315 {
8316 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8317 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8318 sq = sp;
8319 sp->u.offset = sq->rela->r_offset;
8320 }
8321
8322 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8323
8324 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8325 if (lo->type == bfd_indirect_link_order)
8326 {
8327 bfd_byte *erel, *erelend;
8328 asection *o = lo->u.indirect.section;
8329
8330 erel = o->contents;
8331 erelend = o->contents + o->size;
8332 /* FIXME: octets_per_byte. */
8333 p = sort + o->output_offset / ext_size * sort_elt;
8334 while (erel < erelend)
8335 {
8336 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8337 (*swap_out) (abfd, s->rela, erel);
8338 p += sort_elt;
8339 erel += ext_size;
8340 }
8341 }
8342
8343 free (sort);
8344 *psec = dynamic_relocs;
8345 return ret;
8346 }
8347
8348 /* Flush the output symbols to the file. */
8349
8350 static bfd_boolean
8351 elf_link_flush_output_syms (struct elf_final_link_info *flinfo,
8352 const struct elf_backend_data *bed)
8353 {
8354 if (flinfo->symbuf_count > 0)
8355 {
8356 Elf_Internal_Shdr *hdr;
8357 file_ptr pos;
8358 bfd_size_type amt;
8359
8360 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
8361 pos = hdr->sh_offset + hdr->sh_size;
8362 amt = flinfo->symbuf_count * bed->s->sizeof_sym;
8363 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) != 0
8364 || bfd_bwrite (flinfo->symbuf, amt, flinfo->output_bfd) != amt)
8365 return FALSE;
8366
8367 hdr->sh_size += amt;
8368 flinfo->symbuf_count = 0;
8369 }
8370
8371 return TRUE;
8372 }
8373
8374 /* Add a symbol to the output symbol table. */
8375
8376 static int
8377 elf_link_output_sym (struct elf_final_link_info *flinfo,
8378 const char *name,
8379 Elf_Internal_Sym *elfsym,
8380 asection *input_sec,
8381 struct elf_link_hash_entry *h)
8382 {
8383 bfd_byte *dest;
8384 Elf_External_Sym_Shndx *destshndx;
8385 int (*output_symbol_hook)
8386 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8387 struct elf_link_hash_entry *);
8388 const struct elf_backend_data *bed;
8389
8390 bed = get_elf_backend_data (flinfo->output_bfd);
8391 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8392 if (output_symbol_hook != NULL)
8393 {
8394 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
8395 if (ret != 1)
8396 return ret;
8397 }
8398
8399 if (name == NULL || *name == '\0')
8400 elfsym->st_name = 0;
8401 else if (input_sec->flags & SEC_EXCLUDE)
8402 elfsym->st_name = 0;
8403 else
8404 {
8405 elfsym->st_name = (unsigned long) _bfd_stringtab_add (flinfo->symstrtab,
8406 name, TRUE, FALSE);
8407 if (elfsym->st_name == (unsigned long) -1)
8408 return 0;
8409 }
8410
8411 if (flinfo->symbuf_count >= flinfo->symbuf_size)
8412 {
8413 if (! elf_link_flush_output_syms (flinfo, bed))
8414 return 0;
8415 }
8416
8417 dest = flinfo->symbuf + flinfo->symbuf_count * bed->s->sizeof_sym;
8418 destshndx = flinfo->symshndxbuf;
8419 if (destshndx != NULL)
8420 {
8421 if (bfd_get_symcount (flinfo->output_bfd) >= flinfo->shndxbuf_size)
8422 {
8423 bfd_size_type amt;
8424
8425 amt = flinfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
8426 destshndx = (Elf_External_Sym_Shndx *) bfd_realloc (destshndx,
8427 amt * 2);
8428 if (destshndx == NULL)
8429 return 0;
8430 flinfo->symshndxbuf = destshndx;
8431 memset ((char *) destshndx + amt, 0, amt);
8432 flinfo->shndxbuf_size *= 2;
8433 }
8434 destshndx += bfd_get_symcount (flinfo->output_bfd);
8435 }
8436
8437 bed->s->swap_symbol_out (flinfo->output_bfd, elfsym, dest, destshndx);
8438 flinfo->symbuf_count += 1;
8439 bfd_get_symcount (flinfo->output_bfd) += 1;
8440
8441 return 1;
8442 }
8443
8444 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8445
8446 static bfd_boolean
8447 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8448 {
8449 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
8450 && sym->st_shndx < SHN_LORESERVE)
8451 {
8452 /* The gABI doesn't support dynamic symbols in output sections
8453 beyond 64k. */
8454 (*_bfd_error_handler)
8455 (_("%B: Too many sections: %d (>= %d)"),
8456 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
8457 bfd_set_error (bfd_error_nonrepresentable_section);
8458 return FALSE;
8459 }
8460 return TRUE;
8461 }
8462
8463 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8464 allowing an unsatisfied unversioned symbol in the DSO to match a
8465 versioned symbol that would normally require an explicit version.
8466 We also handle the case that a DSO references a hidden symbol
8467 which may be satisfied by a versioned symbol in another DSO. */
8468
8469 static bfd_boolean
8470 elf_link_check_versioned_symbol (struct bfd_link_info *info,
8471 const struct elf_backend_data *bed,
8472 struct elf_link_hash_entry *h)
8473 {
8474 bfd *abfd;
8475 struct elf_link_loaded_list *loaded;
8476
8477 if (!is_elf_hash_table (info->hash))
8478 return FALSE;
8479
8480 /* Check indirect symbol. */
8481 while (h->root.type == bfd_link_hash_indirect)
8482 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8483
8484 switch (h->root.type)
8485 {
8486 default:
8487 abfd = NULL;
8488 break;
8489
8490 case bfd_link_hash_undefined:
8491 case bfd_link_hash_undefweak:
8492 abfd = h->root.u.undef.abfd;
8493 if ((abfd->flags & DYNAMIC) == 0
8494 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
8495 return FALSE;
8496 break;
8497
8498 case bfd_link_hash_defined:
8499 case bfd_link_hash_defweak:
8500 abfd = h->root.u.def.section->owner;
8501 break;
8502
8503 case bfd_link_hash_common:
8504 abfd = h->root.u.c.p->section->owner;
8505 break;
8506 }
8507 BFD_ASSERT (abfd != NULL);
8508
8509 for (loaded = elf_hash_table (info)->loaded;
8510 loaded != NULL;
8511 loaded = loaded->next)
8512 {
8513 bfd *input;
8514 Elf_Internal_Shdr *hdr;
8515 bfd_size_type symcount;
8516 bfd_size_type extsymcount;
8517 bfd_size_type extsymoff;
8518 Elf_Internal_Shdr *versymhdr;
8519 Elf_Internal_Sym *isym;
8520 Elf_Internal_Sym *isymend;
8521 Elf_Internal_Sym *isymbuf;
8522 Elf_External_Versym *ever;
8523 Elf_External_Versym *extversym;
8524
8525 input = loaded->abfd;
8526
8527 /* We check each DSO for a possible hidden versioned definition. */
8528 if (input == abfd
8529 || (input->flags & DYNAMIC) == 0
8530 || elf_dynversym (input) == 0)
8531 continue;
8532
8533 hdr = &elf_tdata (input)->dynsymtab_hdr;
8534
8535 symcount = hdr->sh_size / bed->s->sizeof_sym;
8536 if (elf_bad_symtab (input))
8537 {
8538 extsymcount = symcount;
8539 extsymoff = 0;
8540 }
8541 else
8542 {
8543 extsymcount = symcount - hdr->sh_info;
8544 extsymoff = hdr->sh_info;
8545 }
8546
8547 if (extsymcount == 0)
8548 continue;
8549
8550 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
8551 NULL, NULL, NULL);
8552 if (isymbuf == NULL)
8553 return FALSE;
8554
8555 /* Read in any version definitions. */
8556 versymhdr = &elf_tdata (input)->dynversym_hdr;
8557 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
8558 if (extversym == NULL)
8559 goto error_ret;
8560
8561 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
8562 || (bfd_bread (extversym, versymhdr->sh_size, input)
8563 != versymhdr->sh_size))
8564 {
8565 free (extversym);
8566 error_ret:
8567 free (isymbuf);
8568 return FALSE;
8569 }
8570
8571 ever = extversym + extsymoff;
8572 isymend = isymbuf + extsymcount;
8573 for (isym = isymbuf; isym < isymend; isym++, ever++)
8574 {
8575 const char *name;
8576 Elf_Internal_Versym iver;
8577 unsigned short version_index;
8578
8579 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
8580 || isym->st_shndx == SHN_UNDEF)
8581 continue;
8582
8583 name = bfd_elf_string_from_elf_section (input,
8584 hdr->sh_link,
8585 isym->st_name);
8586 if (strcmp (name, h->root.root.string) != 0)
8587 continue;
8588
8589 _bfd_elf_swap_versym_in (input, ever, &iver);
8590
8591 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
8592 && !(h->def_regular
8593 && h->forced_local))
8594 {
8595 /* If we have a non-hidden versioned sym, then it should
8596 have provided a definition for the undefined sym unless
8597 it is defined in a non-shared object and forced local.
8598 */
8599 abort ();
8600 }
8601
8602 version_index = iver.vs_vers & VERSYM_VERSION;
8603 if (version_index == 1 || version_index == 2)
8604 {
8605 /* This is the base or first version. We can use it. */
8606 free (extversym);
8607 free (isymbuf);
8608 return TRUE;
8609 }
8610 }
8611
8612 free (extversym);
8613 free (isymbuf);
8614 }
8615
8616 return FALSE;
8617 }
8618
8619 /* Add an external symbol to the symbol table. This is called from
8620 the hash table traversal routine. When generating a shared object,
8621 we go through the symbol table twice. The first time we output
8622 anything that might have been forced to local scope in a version
8623 script. The second time we output the symbols that are still
8624 global symbols. */
8625
8626 static bfd_boolean
8627 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
8628 {
8629 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
8630 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8631 struct elf_final_link_info *flinfo = eoinfo->flinfo;
8632 bfd_boolean strip;
8633 Elf_Internal_Sym sym;
8634 asection *input_sec;
8635 const struct elf_backend_data *bed;
8636 long indx;
8637 int ret;
8638
8639 if (h->root.type == bfd_link_hash_warning)
8640 {
8641 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8642 if (h->root.type == bfd_link_hash_new)
8643 return TRUE;
8644 }
8645
8646 /* Decide whether to output this symbol in this pass. */
8647 if (eoinfo->localsyms)
8648 {
8649 if (!h->forced_local)
8650 return TRUE;
8651 if (eoinfo->second_pass
8652 && !((h->root.type == bfd_link_hash_defined
8653 || h->root.type == bfd_link_hash_defweak)
8654 && h->root.u.def.section->output_section != NULL))
8655 return TRUE;
8656 }
8657 else
8658 {
8659 if (h->forced_local)
8660 return TRUE;
8661 }
8662
8663 bed = get_elf_backend_data (flinfo->output_bfd);
8664
8665 if (h->root.type == bfd_link_hash_undefined)
8666 {
8667 /* If we have an undefined symbol reference here then it must have
8668 come from a shared library that is being linked in. (Undefined
8669 references in regular files have already been handled unless
8670 they are in unreferenced sections which are removed by garbage
8671 collection). */
8672 bfd_boolean ignore_undef = FALSE;
8673
8674 /* Some symbols may be special in that the fact that they're
8675 undefined can be safely ignored - let backend determine that. */
8676 if (bed->elf_backend_ignore_undef_symbol)
8677 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
8678
8679 /* If we are reporting errors for this situation then do so now. */
8680 if (!ignore_undef
8681 && h->ref_dynamic
8682 && (!h->ref_regular || flinfo->info->gc_sections)
8683 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
8684 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
8685 {
8686 if (!(flinfo->info->callbacks->undefined_symbol
8687 (flinfo->info, h->root.root.string,
8688 h->ref_regular ? NULL : h->root.u.undef.abfd,
8689 NULL, 0,
8690 (flinfo->info->unresolved_syms_in_shared_libs
8691 == RM_GENERATE_ERROR))))
8692 {
8693 bfd_set_error (bfd_error_bad_value);
8694 eoinfo->failed = TRUE;
8695 return FALSE;
8696 }
8697 }
8698 }
8699
8700 /* We should also warn if a forced local symbol is referenced from
8701 shared libraries. */
8702 if (!flinfo->info->relocatable
8703 && flinfo->info->executable
8704 && h->forced_local
8705 && h->ref_dynamic
8706 && h->def_regular
8707 && !h->dynamic_def
8708 && !h->dynamic_weak
8709 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
8710 {
8711 bfd *def_bfd;
8712 const char *msg;
8713 struct elf_link_hash_entry *hi = h;
8714
8715 /* Check indirect symbol. */
8716 while (hi->root.type == bfd_link_hash_indirect)
8717 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
8718
8719 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
8720 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
8721 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
8722 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
8723 else
8724 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
8725 def_bfd = flinfo->output_bfd;
8726 if (hi->root.u.def.section != bfd_abs_section_ptr)
8727 def_bfd = hi->root.u.def.section->owner;
8728 (*_bfd_error_handler) (msg, flinfo->output_bfd, def_bfd,
8729 h->root.root.string);
8730 bfd_set_error (bfd_error_bad_value);
8731 eoinfo->failed = TRUE;
8732 return FALSE;
8733 }
8734
8735 /* We don't want to output symbols that have never been mentioned by
8736 a regular file, or that we have been told to strip. However, if
8737 h->indx is set to -2, the symbol is used by a reloc and we must
8738 output it. */
8739 if (h->indx == -2)
8740 strip = FALSE;
8741 else if ((h->def_dynamic
8742 || h->ref_dynamic
8743 || h->root.type == bfd_link_hash_new)
8744 && !h->def_regular
8745 && !h->ref_regular)
8746 strip = TRUE;
8747 else if (flinfo->info->strip == strip_all)
8748 strip = TRUE;
8749 else if (flinfo->info->strip == strip_some
8750 && bfd_hash_lookup (flinfo->info->keep_hash,
8751 h->root.root.string, FALSE, FALSE) == NULL)
8752 strip = TRUE;
8753 else if ((h->root.type == bfd_link_hash_defined
8754 || h->root.type == bfd_link_hash_defweak)
8755 && ((flinfo->info->strip_discarded
8756 && discarded_section (h->root.u.def.section))
8757 || (h->root.u.def.section->owner != NULL
8758 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
8759 strip = TRUE;
8760 else if ((h->root.type == bfd_link_hash_undefined
8761 || h->root.type == bfd_link_hash_undefweak)
8762 && h->root.u.undef.abfd != NULL
8763 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
8764 strip = TRUE;
8765 else
8766 strip = FALSE;
8767
8768 /* If we're stripping it, and it's not a dynamic symbol, there's
8769 nothing else to do unless it is a forced local symbol or a
8770 STT_GNU_IFUNC symbol. */
8771 if (strip
8772 && h->dynindx == -1
8773 && h->type != STT_GNU_IFUNC
8774 && !h->forced_local)
8775 return TRUE;
8776
8777 sym.st_value = 0;
8778 sym.st_size = h->size;
8779 sym.st_other = h->other;
8780 if (h->forced_local)
8781 {
8782 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
8783 /* Turn off visibility on local symbol. */
8784 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
8785 }
8786 else if (h->unique_global)
8787 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, h->type);
8788 else if (h->root.type == bfd_link_hash_undefweak
8789 || h->root.type == bfd_link_hash_defweak)
8790 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
8791 else
8792 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
8793 sym.st_target_internal = h->target_internal;
8794
8795 switch (h->root.type)
8796 {
8797 default:
8798 case bfd_link_hash_new:
8799 case bfd_link_hash_warning:
8800 abort ();
8801 return FALSE;
8802
8803 case bfd_link_hash_undefined:
8804 case bfd_link_hash_undefweak:
8805 input_sec = bfd_und_section_ptr;
8806 sym.st_shndx = SHN_UNDEF;
8807 break;
8808
8809 case bfd_link_hash_defined:
8810 case bfd_link_hash_defweak:
8811 {
8812 input_sec = h->root.u.def.section;
8813 if (input_sec->output_section != NULL)
8814 {
8815 if (eoinfo->localsyms && flinfo->filesym_count == 1)
8816 {
8817 bfd_boolean second_pass_sym
8818 = (input_sec->owner == flinfo->output_bfd
8819 || input_sec->owner == NULL
8820 || (input_sec->flags & SEC_LINKER_CREATED) != 0
8821 || (input_sec->owner->flags & BFD_LINKER_CREATED) != 0);
8822
8823 eoinfo->need_second_pass |= second_pass_sym;
8824 if (eoinfo->second_pass != second_pass_sym)
8825 return TRUE;
8826 }
8827
8828 sym.st_shndx =
8829 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
8830 input_sec->output_section);
8831 if (sym.st_shndx == SHN_BAD)
8832 {
8833 (*_bfd_error_handler)
8834 (_("%B: could not find output section %A for input section %A"),
8835 flinfo->output_bfd, input_sec->output_section, input_sec);
8836 bfd_set_error (bfd_error_nonrepresentable_section);
8837 eoinfo->failed = TRUE;
8838 return FALSE;
8839 }
8840
8841 /* ELF symbols in relocatable files are section relative,
8842 but in nonrelocatable files they are virtual
8843 addresses. */
8844 sym.st_value = h->root.u.def.value + input_sec->output_offset;
8845 if (!flinfo->info->relocatable)
8846 {
8847 sym.st_value += input_sec->output_section->vma;
8848 if (h->type == STT_TLS)
8849 {
8850 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
8851 if (tls_sec != NULL)
8852 sym.st_value -= tls_sec->vma;
8853 else
8854 {
8855 /* The TLS section may have been garbage collected. */
8856 BFD_ASSERT (flinfo->info->gc_sections
8857 && !input_sec->gc_mark);
8858 }
8859 }
8860 }
8861 }
8862 else
8863 {
8864 BFD_ASSERT (input_sec->owner == NULL
8865 || (input_sec->owner->flags & DYNAMIC) != 0);
8866 sym.st_shndx = SHN_UNDEF;
8867 input_sec = bfd_und_section_ptr;
8868 }
8869 }
8870 break;
8871
8872 case bfd_link_hash_common:
8873 input_sec = h->root.u.c.p->section;
8874 sym.st_shndx = bed->common_section_index (input_sec);
8875 sym.st_value = 1 << h->root.u.c.p->alignment_power;
8876 break;
8877
8878 case bfd_link_hash_indirect:
8879 /* These symbols are created by symbol versioning. They point
8880 to the decorated version of the name. For example, if the
8881 symbol foo@@GNU_1.2 is the default, which should be used when
8882 foo is used with no version, then we add an indirect symbol
8883 foo which points to foo@@GNU_1.2. We ignore these symbols,
8884 since the indirected symbol is already in the hash table. */
8885 return TRUE;
8886 }
8887
8888 /* Give the processor backend a chance to tweak the symbol value,
8889 and also to finish up anything that needs to be done for this
8890 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
8891 forced local syms when non-shared is due to a historical quirk.
8892 STT_GNU_IFUNC symbol must go through PLT. */
8893 if ((h->type == STT_GNU_IFUNC
8894 && h->def_regular
8895 && !flinfo->info->relocatable)
8896 || ((h->dynindx != -1
8897 || h->forced_local)
8898 && ((flinfo->info->shared
8899 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8900 || h->root.type != bfd_link_hash_undefweak))
8901 || !h->forced_local)
8902 && elf_hash_table (flinfo->info)->dynamic_sections_created))
8903 {
8904 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8905 (flinfo->output_bfd, flinfo->info, h, &sym)))
8906 {
8907 eoinfo->failed = TRUE;
8908 return FALSE;
8909 }
8910 }
8911
8912 /* If we are marking the symbol as undefined, and there are no
8913 non-weak references to this symbol from a regular object, then
8914 mark the symbol as weak undefined; if there are non-weak
8915 references, mark the symbol as strong. We can't do this earlier,
8916 because it might not be marked as undefined until the
8917 finish_dynamic_symbol routine gets through with it. */
8918 if (sym.st_shndx == SHN_UNDEF
8919 && h->ref_regular
8920 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
8921 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
8922 {
8923 int bindtype;
8924 unsigned int type = ELF_ST_TYPE (sym.st_info);
8925
8926 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
8927 if (type == STT_GNU_IFUNC)
8928 type = STT_FUNC;
8929
8930 if (h->ref_regular_nonweak)
8931 bindtype = STB_GLOBAL;
8932 else
8933 bindtype = STB_WEAK;
8934 sym.st_info = ELF_ST_INFO (bindtype, type);
8935 }
8936
8937 /* If this is a symbol defined in a dynamic library, don't use the
8938 symbol size from the dynamic library. Relinking an executable
8939 against a new library may introduce gratuitous changes in the
8940 executable's symbols if we keep the size. */
8941 if (sym.st_shndx == SHN_UNDEF
8942 && !h->def_regular
8943 && h->def_dynamic)
8944 sym.st_size = 0;
8945
8946 /* If a non-weak symbol with non-default visibility is not defined
8947 locally, it is a fatal error. */
8948 if (!flinfo->info->relocatable
8949 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
8950 && ELF_ST_BIND (sym.st_info) != STB_WEAK
8951 && h->root.type == bfd_link_hash_undefined
8952 && !h->def_regular)
8953 {
8954 const char *msg;
8955
8956 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
8957 msg = _("%B: protected symbol `%s' isn't defined");
8958 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
8959 msg = _("%B: internal symbol `%s' isn't defined");
8960 else
8961 msg = _("%B: hidden symbol `%s' isn't defined");
8962 (*_bfd_error_handler) (msg, flinfo->output_bfd, h->root.root.string);
8963 bfd_set_error (bfd_error_bad_value);
8964 eoinfo->failed = TRUE;
8965 return FALSE;
8966 }
8967
8968 /* If this symbol should be put in the .dynsym section, then put it
8969 there now. We already know the symbol index. We also fill in
8970 the entry in the .hash section. */
8971 if (flinfo->dynsym_sec != NULL
8972 && h->dynindx != -1
8973 && elf_hash_table (flinfo->info)->dynamic_sections_created)
8974 {
8975 bfd_byte *esym;
8976
8977 /* Since there is no version information in the dynamic string,
8978 if there is no version info in symbol version section, we will
8979 have a run-time problem. */
8980 if (h->verinfo.verdef == NULL)
8981 {
8982 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
8983
8984 if (p && p [1] != '\0')
8985 {
8986 (*_bfd_error_handler)
8987 (_("%B: No symbol version section for versioned symbol `%s'"),
8988 flinfo->output_bfd, h->root.root.string);
8989 eoinfo->failed = TRUE;
8990 return FALSE;
8991 }
8992 }
8993
8994 sym.st_name = h->dynstr_index;
8995 esym = flinfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
8996 if (!check_dynsym (flinfo->output_bfd, &sym))
8997 {
8998 eoinfo->failed = TRUE;
8999 return FALSE;
9000 }
9001 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
9002
9003 if (flinfo->hash_sec != NULL)
9004 {
9005 size_t hash_entry_size;
9006 bfd_byte *bucketpos;
9007 bfd_vma chain;
9008 size_t bucketcount;
9009 size_t bucket;
9010
9011 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
9012 bucket = h->u.elf_hash_value % bucketcount;
9013
9014 hash_entry_size
9015 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9016 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
9017 + (bucket + 2) * hash_entry_size);
9018 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9019 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9020 bucketpos);
9021 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9022 ((bfd_byte *) flinfo->hash_sec->contents
9023 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9024 }
9025
9026 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
9027 {
9028 Elf_Internal_Versym iversym;
9029 Elf_External_Versym *eversym;
9030
9031 if (!h->def_regular)
9032 {
9033 if (h->verinfo.verdef == NULL)
9034 iversym.vs_vers = 0;
9035 else
9036 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9037 }
9038 else
9039 {
9040 if (h->verinfo.vertree == NULL)
9041 iversym.vs_vers = 1;
9042 else
9043 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
9044 if (flinfo->info->create_default_symver)
9045 iversym.vs_vers++;
9046 }
9047
9048 if (h->hidden)
9049 iversym.vs_vers |= VERSYM_HIDDEN;
9050
9051 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
9052 eversym += h->dynindx;
9053 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
9054 }
9055 }
9056
9057 /* If we're stripping it, then it was just a dynamic symbol, and
9058 there's nothing else to do. */
9059 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
9060 return TRUE;
9061
9062 indx = bfd_get_symcount (flinfo->output_bfd);
9063 ret = elf_link_output_sym (flinfo, h->root.root.string, &sym, input_sec, h);
9064 if (ret == 0)
9065 {
9066 eoinfo->failed = TRUE;
9067 return FALSE;
9068 }
9069 else if (ret == 1)
9070 h->indx = indx;
9071 else if (h->indx == -2)
9072 abort();
9073
9074 return TRUE;
9075 }
9076
9077 /* Return TRUE if special handling is done for relocs in SEC against
9078 symbols defined in discarded sections. */
9079
9080 static bfd_boolean
9081 elf_section_ignore_discarded_relocs (asection *sec)
9082 {
9083 const struct elf_backend_data *bed;
9084
9085 switch (sec->sec_info_type)
9086 {
9087 case SEC_INFO_TYPE_STABS:
9088 case SEC_INFO_TYPE_EH_FRAME:
9089 return TRUE;
9090 default:
9091 break;
9092 }
9093
9094 bed = get_elf_backend_data (sec->owner);
9095 if (bed->elf_backend_ignore_discarded_relocs != NULL
9096 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9097 return TRUE;
9098
9099 return FALSE;
9100 }
9101
9102 /* Return a mask saying how ld should treat relocations in SEC against
9103 symbols defined in discarded sections. If this function returns
9104 COMPLAIN set, ld will issue a warning message. If this function
9105 returns PRETEND set, and the discarded section was link-once and the
9106 same size as the kept link-once section, ld will pretend that the
9107 symbol was actually defined in the kept section. Otherwise ld will
9108 zero the reloc (at least that is the intent, but some cooperation by
9109 the target dependent code is needed, particularly for REL targets). */
9110
9111 unsigned int
9112 _bfd_elf_default_action_discarded (asection *sec)
9113 {
9114 if (sec->flags & SEC_DEBUGGING)
9115 return PRETEND;
9116
9117 if (strcmp (".eh_frame", sec->name) == 0)
9118 return 0;
9119
9120 if (strcmp (".gcc_except_table", sec->name) == 0)
9121 return 0;
9122
9123 return COMPLAIN | PRETEND;
9124 }
9125
9126 /* Find a match between a section and a member of a section group. */
9127
9128 static asection *
9129 match_group_member (asection *sec, asection *group,
9130 struct bfd_link_info *info)
9131 {
9132 asection *first = elf_next_in_group (group);
9133 asection *s = first;
9134
9135 while (s != NULL)
9136 {
9137 if (bfd_elf_match_symbols_in_sections (s, sec, info))
9138 return s;
9139
9140 s = elf_next_in_group (s);
9141 if (s == first)
9142 break;
9143 }
9144
9145 return NULL;
9146 }
9147
9148 /* Check if the kept section of a discarded section SEC can be used
9149 to replace it. Return the replacement if it is OK. Otherwise return
9150 NULL. */
9151
9152 asection *
9153 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
9154 {
9155 asection *kept;
9156
9157 kept = sec->kept_section;
9158 if (kept != NULL)
9159 {
9160 if ((kept->flags & SEC_GROUP) != 0)
9161 kept = match_group_member (sec, kept, info);
9162 if (kept != NULL
9163 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9164 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
9165 kept = NULL;
9166 sec->kept_section = kept;
9167 }
9168 return kept;
9169 }
9170
9171 /* Link an input file into the linker output file. This function
9172 handles all the sections and relocations of the input file at once.
9173 This is so that we only have to read the local symbols once, and
9174 don't have to keep them in memory. */
9175
9176 static bfd_boolean
9177 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
9178 {
9179 int (*relocate_section)
9180 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9181 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9182 bfd *output_bfd;
9183 Elf_Internal_Shdr *symtab_hdr;
9184 size_t locsymcount;
9185 size_t extsymoff;
9186 Elf_Internal_Sym *isymbuf;
9187 Elf_Internal_Sym *isym;
9188 Elf_Internal_Sym *isymend;
9189 long *pindex;
9190 asection **ppsection;
9191 asection *o;
9192 const struct elf_backend_data *bed;
9193 struct elf_link_hash_entry **sym_hashes;
9194 bfd_size_type address_size;
9195 bfd_vma r_type_mask;
9196 int r_sym_shift;
9197 bfd_boolean have_file_sym = FALSE;
9198
9199 output_bfd = flinfo->output_bfd;
9200 bed = get_elf_backend_data (output_bfd);
9201 relocate_section = bed->elf_backend_relocate_section;
9202
9203 /* If this is a dynamic object, we don't want to do anything here:
9204 we don't want the local symbols, and we don't want the section
9205 contents. */
9206 if ((input_bfd->flags & DYNAMIC) != 0)
9207 return TRUE;
9208
9209 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9210 if (elf_bad_symtab (input_bfd))
9211 {
9212 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9213 extsymoff = 0;
9214 }
9215 else
9216 {
9217 locsymcount = symtab_hdr->sh_info;
9218 extsymoff = symtab_hdr->sh_info;
9219 }
9220
9221 /* Read the local symbols. */
9222 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9223 if (isymbuf == NULL && locsymcount != 0)
9224 {
9225 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
9226 flinfo->internal_syms,
9227 flinfo->external_syms,
9228 flinfo->locsym_shndx);
9229 if (isymbuf == NULL)
9230 return FALSE;
9231 }
9232
9233 /* Find local symbol sections and adjust values of symbols in
9234 SEC_MERGE sections. Write out those local symbols we know are
9235 going into the output file. */
9236 isymend = isymbuf + locsymcount;
9237 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
9238 isym < isymend;
9239 isym++, pindex++, ppsection++)
9240 {
9241 asection *isec;
9242 const char *name;
9243 Elf_Internal_Sym osym;
9244 long indx;
9245 int ret;
9246
9247 *pindex = -1;
9248
9249 if (elf_bad_symtab (input_bfd))
9250 {
9251 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9252 {
9253 *ppsection = NULL;
9254 continue;
9255 }
9256 }
9257
9258 if (isym->st_shndx == SHN_UNDEF)
9259 isec = bfd_und_section_ptr;
9260 else if (isym->st_shndx == SHN_ABS)
9261 isec = bfd_abs_section_ptr;
9262 else if (isym->st_shndx == SHN_COMMON)
9263 isec = bfd_com_section_ptr;
9264 else
9265 {
9266 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9267 if (isec == NULL)
9268 {
9269 /* Don't attempt to output symbols with st_shnx in the
9270 reserved range other than SHN_ABS and SHN_COMMON. */
9271 *ppsection = NULL;
9272 continue;
9273 }
9274 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
9275 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9276 isym->st_value =
9277 _bfd_merged_section_offset (output_bfd, &isec,
9278 elf_section_data (isec)->sec_info,
9279 isym->st_value);
9280 }
9281
9282 *ppsection = isec;
9283
9284 /* Don't output the first, undefined, symbol. */
9285 if (ppsection == flinfo->sections)
9286 continue;
9287
9288 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9289 {
9290 /* We never output section symbols. Instead, we use the
9291 section symbol of the corresponding section in the output
9292 file. */
9293 continue;
9294 }
9295
9296 /* If we are stripping all symbols, we don't want to output this
9297 one. */
9298 if (flinfo->info->strip == strip_all)
9299 continue;
9300
9301 /* If we are discarding all local symbols, we don't want to
9302 output this one. If we are generating a relocatable output
9303 file, then some of the local symbols may be required by
9304 relocs; we output them below as we discover that they are
9305 needed. */
9306 if (flinfo->info->discard == discard_all)
9307 continue;
9308
9309 /* If this symbol is defined in a section which we are
9310 discarding, we don't need to keep it. */
9311 if (isym->st_shndx != SHN_UNDEF
9312 && isym->st_shndx < SHN_LORESERVE
9313 && bfd_section_removed_from_list (output_bfd,
9314 isec->output_section))
9315 continue;
9316
9317 /* Get the name of the symbol. */
9318 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9319 isym->st_name);
9320 if (name == NULL)
9321 return FALSE;
9322
9323 /* See if we are discarding symbols with this name. */
9324 if ((flinfo->info->strip == strip_some
9325 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
9326 == NULL))
9327 || (((flinfo->info->discard == discard_sec_merge
9328 && (isec->flags & SEC_MERGE) && !flinfo->info->relocatable)
9329 || flinfo->info->discard == discard_l)
9330 && bfd_is_local_label_name (input_bfd, name)))
9331 continue;
9332
9333 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
9334 {
9335 have_file_sym = TRUE;
9336 flinfo->filesym_count += 1;
9337 }
9338 if (!have_file_sym)
9339 {
9340 /* In the absence of debug info, bfd_find_nearest_line uses
9341 FILE symbols to determine the source file for local
9342 function symbols. Provide a FILE symbol here if input
9343 files lack such, so that their symbols won't be
9344 associated with a previous input file. It's not the
9345 source file, but the best we can do. */
9346 have_file_sym = TRUE;
9347 flinfo->filesym_count += 1;
9348 memset (&osym, 0, sizeof (osym));
9349 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9350 osym.st_shndx = SHN_ABS;
9351 if (!elf_link_output_sym (flinfo, input_bfd->filename, &osym,
9352 bfd_abs_section_ptr, NULL))
9353 return FALSE;
9354 }
9355
9356 osym = *isym;
9357
9358 /* Adjust the section index for the output file. */
9359 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9360 isec->output_section);
9361 if (osym.st_shndx == SHN_BAD)
9362 return FALSE;
9363
9364 /* ELF symbols in relocatable files are section relative, but
9365 in executable files they are virtual addresses. Note that
9366 this code assumes that all ELF sections have an associated
9367 BFD section with a reasonable value for output_offset; below
9368 we assume that they also have a reasonable value for
9369 output_section. Any special sections must be set up to meet
9370 these requirements. */
9371 osym.st_value += isec->output_offset;
9372 if (!flinfo->info->relocatable)
9373 {
9374 osym.st_value += isec->output_section->vma;
9375 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
9376 {
9377 /* STT_TLS symbols are relative to PT_TLS segment base. */
9378 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
9379 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
9380 }
9381 }
9382
9383 indx = bfd_get_symcount (output_bfd);
9384 ret = elf_link_output_sym (flinfo, name, &osym, isec, NULL);
9385 if (ret == 0)
9386 return FALSE;
9387 else if (ret == 1)
9388 *pindex = indx;
9389 }
9390
9391 if (bed->s->arch_size == 32)
9392 {
9393 r_type_mask = 0xff;
9394 r_sym_shift = 8;
9395 address_size = 4;
9396 }
9397 else
9398 {
9399 r_type_mask = 0xffffffff;
9400 r_sym_shift = 32;
9401 address_size = 8;
9402 }
9403
9404 /* Relocate the contents of each section. */
9405 sym_hashes = elf_sym_hashes (input_bfd);
9406 for (o = input_bfd->sections; o != NULL; o = o->next)
9407 {
9408 bfd_byte *contents;
9409
9410 if (! o->linker_mark)
9411 {
9412 /* This section was omitted from the link. */
9413 continue;
9414 }
9415
9416 if (flinfo->info->relocatable
9417 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
9418 {
9419 /* Deal with the group signature symbol. */
9420 struct bfd_elf_section_data *sec_data = elf_section_data (o);
9421 unsigned long symndx = sec_data->this_hdr.sh_info;
9422 asection *osec = o->output_section;
9423
9424 if (symndx >= locsymcount
9425 || (elf_bad_symtab (input_bfd)
9426 && flinfo->sections[symndx] == NULL))
9427 {
9428 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
9429 while (h->root.type == bfd_link_hash_indirect
9430 || h->root.type == bfd_link_hash_warning)
9431 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9432 /* Arrange for symbol to be output. */
9433 h->indx = -2;
9434 elf_section_data (osec)->this_hdr.sh_info = -2;
9435 }
9436 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
9437 {
9438 /* We'll use the output section target_index. */
9439 asection *sec = flinfo->sections[symndx]->output_section;
9440 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
9441 }
9442 else
9443 {
9444 if (flinfo->indices[symndx] == -1)
9445 {
9446 /* Otherwise output the local symbol now. */
9447 Elf_Internal_Sym sym = isymbuf[symndx];
9448 asection *sec = flinfo->sections[symndx]->output_section;
9449 const char *name;
9450 long indx;
9451 int ret;
9452
9453 name = bfd_elf_string_from_elf_section (input_bfd,
9454 symtab_hdr->sh_link,
9455 sym.st_name);
9456 if (name == NULL)
9457 return FALSE;
9458
9459 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9460 sec);
9461 if (sym.st_shndx == SHN_BAD)
9462 return FALSE;
9463
9464 sym.st_value += o->output_offset;
9465
9466 indx = bfd_get_symcount (output_bfd);
9467 ret = elf_link_output_sym (flinfo, name, &sym, o, NULL);
9468 if (ret == 0)
9469 return FALSE;
9470 else if (ret == 1)
9471 flinfo->indices[symndx] = indx;
9472 else
9473 abort ();
9474 }
9475 elf_section_data (osec)->this_hdr.sh_info
9476 = flinfo->indices[symndx];
9477 }
9478 }
9479
9480 if ((o->flags & SEC_HAS_CONTENTS) == 0
9481 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
9482 continue;
9483
9484 if ((o->flags & SEC_LINKER_CREATED) != 0)
9485 {
9486 /* Section was created by _bfd_elf_link_create_dynamic_sections
9487 or somesuch. */
9488 continue;
9489 }
9490
9491 /* Get the contents of the section. They have been cached by a
9492 relaxation routine. Note that o is a section in an input
9493 file, so the contents field will not have been set by any of
9494 the routines which work on output files. */
9495 if (elf_section_data (o)->this_hdr.contents != NULL)
9496 contents = elf_section_data (o)->this_hdr.contents;
9497 else
9498 {
9499 contents = flinfo->contents;
9500 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
9501 return FALSE;
9502 }
9503
9504 if ((o->flags & SEC_RELOC) != 0)
9505 {
9506 Elf_Internal_Rela *internal_relocs;
9507 Elf_Internal_Rela *rel, *relend;
9508 int action_discarded;
9509 int ret;
9510
9511 /* Get the swapped relocs. */
9512 internal_relocs
9513 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
9514 flinfo->internal_relocs, FALSE);
9515 if (internal_relocs == NULL
9516 && o->reloc_count > 0)
9517 return FALSE;
9518
9519 /* We need to reverse-copy input .ctors/.dtors sections if
9520 they are placed in .init_array/.finit_array for output. */
9521 if (o->size > address_size
9522 && ((strncmp (o->name, ".ctors", 6) == 0
9523 && strcmp (o->output_section->name,
9524 ".init_array") == 0)
9525 || (strncmp (o->name, ".dtors", 6) == 0
9526 && strcmp (o->output_section->name,
9527 ".fini_array") == 0))
9528 && (o->name[6] == 0 || o->name[6] == '.'))
9529 {
9530 if (o->size != o->reloc_count * address_size)
9531 {
9532 (*_bfd_error_handler)
9533 (_("error: %B: size of section %A is not "
9534 "multiple of address size"),
9535 input_bfd, o);
9536 bfd_set_error (bfd_error_on_input);
9537 return FALSE;
9538 }
9539 o->flags |= SEC_ELF_REVERSE_COPY;
9540 }
9541
9542 action_discarded = -1;
9543 if (!elf_section_ignore_discarded_relocs (o))
9544 action_discarded = (*bed->action_discarded) (o);
9545
9546 /* Run through the relocs evaluating complex reloc symbols and
9547 looking for relocs against symbols from discarded sections
9548 or section symbols from removed link-once sections.
9549 Complain about relocs against discarded sections. Zero
9550 relocs against removed link-once sections. */
9551
9552 rel = internal_relocs;
9553 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
9554 for ( ; rel < relend; rel++)
9555 {
9556 unsigned long r_symndx = rel->r_info >> r_sym_shift;
9557 unsigned int s_type;
9558 asection **ps, *sec;
9559 struct elf_link_hash_entry *h = NULL;
9560 const char *sym_name;
9561
9562 if (r_symndx == STN_UNDEF)
9563 continue;
9564
9565 if (r_symndx >= locsymcount
9566 || (elf_bad_symtab (input_bfd)
9567 && flinfo->sections[r_symndx] == NULL))
9568 {
9569 h = sym_hashes[r_symndx - extsymoff];
9570
9571 /* Badly formatted input files can contain relocs that
9572 reference non-existant symbols. Check here so that
9573 we do not seg fault. */
9574 if (h == NULL)
9575 {
9576 char buffer [32];
9577
9578 sprintf_vma (buffer, rel->r_info);
9579 (*_bfd_error_handler)
9580 (_("error: %B contains a reloc (0x%s) for section %A "
9581 "that references a non-existent global symbol"),
9582 input_bfd, o, buffer);
9583 bfd_set_error (bfd_error_bad_value);
9584 return FALSE;
9585 }
9586
9587 while (h->root.type == bfd_link_hash_indirect
9588 || h->root.type == bfd_link_hash_warning)
9589 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9590
9591 s_type = h->type;
9592
9593 ps = NULL;
9594 if (h->root.type == bfd_link_hash_defined
9595 || h->root.type == bfd_link_hash_defweak)
9596 ps = &h->root.u.def.section;
9597
9598 sym_name = h->root.root.string;
9599 }
9600 else
9601 {
9602 Elf_Internal_Sym *sym = isymbuf + r_symndx;
9603
9604 s_type = ELF_ST_TYPE (sym->st_info);
9605 ps = &flinfo->sections[r_symndx];
9606 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
9607 sym, *ps);
9608 }
9609
9610 if ((s_type == STT_RELC || s_type == STT_SRELC)
9611 && !flinfo->info->relocatable)
9612 {
9613 bfd_vma val;
9614 bfd_vma dot = (rel->r_offset
9615 + o->output_offset + o->output_section->vma);
9616 #ifdef DEBUG
9617 printf ("Encountered a complex symbol!");
9618 printf (" (input_bfd %s, section %s, reloc %ld\n",
9619 input_bfd->filename, o->name,
9620 (long) (rel - internal_relocs));
9621 printf (" symbol: idx %8.8lx, name %s\n",
9622 r_symndx, sym_name);
9623 printf (" reloc : info %8.8lx, addr %8.8lx\n",
9624 (unsigned long) rel->r_info,
9625 (unsigned long) rel->r_offset);
9626 #endif
9627 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
9628 isymbuf, locsymcount, s_type == STT_SRELC))
9629 return FALSE;
9630
9631 /* Symbol evaluated OK. Update to absolute value. */
9632 set_symbol_value (input_bfd, isymbuf, locsymcount,
9633 r_symndx, val);
9634 continue;
9635 }
9636
9637 if (action_discarded != -1 && ps != NULL)
9638 {
9639 /* Complain if the definition comes from a
9640 discarded section. */
9641 if ((sec = *ps) != NULL && discarded_section (sec))
9642 {
9643 BFD_ASSERT (r_symndx != STN_UNDEF);
9644 if (action_discarded & COMPLAIN)
9645 (*flinfo->info->callbacks->einfo)
9646 (_("%X`%s' referenced in section `%A' of %B: "
9647 "defined in discarded section `%A' of %B\n"),
9648 sym_name, o, input_bfd, sec, sec->owner);
9649
9650 /* Try to do the best we can to support buggy old
9651 versions of gcc. Pretend that the symbol is
9652 really defined in the kept linkonce section.
9653 FIXME: This is quite broken. Modifying the
9654 symbol here means we will be changing all later
9655 uses of the symbol, not just in this section. */
9656 if (action_discarded & PRETEND)
9657 {
9658 asection *kept;
9659
9660 kept = _bfd_elf_check_kept_section (sec,
9661 flinfo->info);
9662 if (kept != NULL)
9663 {
9664 *ps = kept;
9665 continue;
9666 }
9667 }
9668 }
9669 }
9670 }
9671
9672 /* Relocate the section by invoking a back end routine.
9673
9674 The back end routine is responsible for adjusting the
9675 section contents as necessary, and (if using Rela relocs
9676 and generating a relocatable output file) adjusting the
9677 reloc addend as necessary.
9678
9679 The back end routine does not have to worry about setting
9680 the reloc address or the reloc symbol index.
9681
9682 The back end routine is given a pointer to the swapped in
9683 internal symbols, and can access the hash table entries
9684 for the external symbols via elf_sym_hashes (input_bfd).
9685
9686 When generating relocatable output, the back end routine
9687 must handle STB_LOCAL/STT_SECTION symbols specially. The
9688 output symbol is going to be a section symbol
9689 corresponding to the output section, which will require
9690 the addend to be adjusted. */
9691
9692 ret = (*relocate_section) (output_bfd, flinfo->info,
9693 input_bfd, o, contents,
9694 internal_relocs,
9695 isymbuf,
9696 flinfo->sections);
9697 if (!ret)
9698 return FALSE;
9699
9700 if (ret == 2
9701 || flinfo->info->relocatable
9702 || flinfo->info->emitrelocations)
9703 {
9704 Elf_Internal_Rela *irela;
9705 Elf_Internal_Rela *irelaend, *irelamid;
9706 bfd_vma last_offset;
9707 struct elf_link_hash_entry **rel_hash;
9708 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
9709 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
9710 unsigned int next_erel;
9711 bfd_boolean rela_normal;
9712 struct bfd_elf_section_data *esdi, *esdo;
9713
9714 esdi = elf_section_data (o);
9715 esdo = elf_section_data (o->output_section);
9716 rela_normal = FALSE;
9717
9718 /* Adjust the reloc addresses and symbol indices. */
9719
9720 irela = internal_relocs;
9721 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
9722 rel_hash = esdo->rel.hashes + esdo->rel.count;
9723 /* We start processing the REL relocs, if any. When we reach
9724 IRELAMID in the loop, we switch to the RELA relocs. */
9725 irelamid = irela;
9726 if (esdi->rel.hdr != NULL)
9727 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
9728 * bed->s->int_rels_per_ext_rel);
9729 rel_hash_list = rel_hash;
9730 rela_hash_list = NULL;
9731 last_offset = o->output_offset;
9732 if (!flinfo->info->relocatable)
9733 last_offset += o->output_section->vma;
9734 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
9735 {
9736 unsigned long r_symndx;
9737 asection *sec;
9738 Elf_Internal_Sym sym;
9739
9740 if (next_erel == bed->s->int_rels_per_ext_rel)
9741 {
9742 rel_hash++;
9743 next_erel = 0;
9744 }
9745
9746 if (irela == irelamid)
9747 {
9748 rel_hash = esdo->rela.hashes + esdo->rela.count;
9749 rela_hash_list = rel_hash;
9750 rela_normal = bed->rela_normal;
9751 }
9752
9753 irela->r_offset = _bfd_elf_section_offset (output_bfd,
9754 flinfo->info, o,
9755 irela->r_offset);
9756 if (irela->r_offset >= (bfd_vma) -2)
9757 {
9758 /* This is a reloc for a deleted entry or somesuch.
9759 Turn it into an R_*_NONE reloc, at the same
9760 offset as the last reloc. elf_eh_frame.c and
9761 bfd_elf_discard_info rely on reloc offsets
9762 being ordered. */
9763 irela->r_offset = last_offset;
9764 irela->r_info = 0;
9765 irela->r_addend = 0;
9766 continue;
9767 }
9768
9769 irela->r_offset += o->output_offset;
9770
9771 /* Relocs in an executable have to be virtual addresses. */
9772 if (!flinfo->info->relocatable)
9773 irela->r_offset += o->output_section->vma;
9774
9775 last_offset = irela->r_offset;
9776
9777 r_symndx = irela->r_info >> r_sym_shift;
9778 if (r_symndx == STN_UNDEF)
9779 continue;
9780
9781 if (r_symndx >= locsymcount
9782 || (elf_bad_symtab (input_bfd)
9783 && flinfo->sections[r_symndx] == NULL))
9784 {
9785 struct elf_link_hash_entry *rh;
9786 unsigned long indx;
9787
9788 /* This is a reloc against a global symbol. We
9789 have not yet output all the local symbols, so
9790 we do not know the symbol index of any global
9791 symbol. We set the rel_hash entry for this
9792 reloc to point to the global hash table entry
9793 for this symbol. The symbol index is then
9794 set at the end of bfd_elf_final_link. */
9795 indx = r_symndx - extsymoff;
9796 rh = elf_sym_hashes (input_bfd)[indx];
9797 while (rh->root.type == bfd_link_hash_indirect
9798 || rh->root.type == bfd_link_hash_warning)
9799 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
9800
9801 /* Setting the index to -2 tells
9802 elf_link_output_extsym that this symbol is
9803 used by a reloc. */
9804 BFD_ASSERT (rh->indx < 0);
9805 rh->indx = -2;
9806
9807 *rel_hash = rh;
9808
9809 continue;
9810 }
9811
9812 /* This is a reloc against a local symbol. */
9813
9814 *rel_hash = NULL;
9815 sym = isymbuf[r_symndx];
9816 sec = flinfo->sections[r_symndx];
9817 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
9818 {
9819 /* I suppose the backend ought to fill in the
9820 section of any STT_SECTION symbol against a
9821 processor specific section. */
9822 r_symndx = STN_UNDEF;
9823 if (bfd_is_abs_section (sec))
9824 ;
9825 else if (sec == NULL || sec->owner == NULL)
9826 {
9827 bfd_set_error (bfd_error_bad_value);
9828 return FALSE;
9829 }
9830 else
9831 {
9832 asection *osec = sec->output_section;
9833
9834 /* If we have discarded a section, the output
9835 section will be the absolute section. In
9836 case of discarded SEC_MERGE sections, use
9837 the kept section. relocate_section should
9838 have already handled discarded linkonce
9839 sections. */
9840 if (bfd_is_abs_section (osec)
9841 && sec->kept_section != NULL
9842 && sec->kept_section->output_section != NULL)
9843 {
9844 osec = sec->kept_section->output_section;
9845 irela->r_addend -= osec->vma;
9846 }
9847
9848 if (!bfd_is_abs_section (osec))
9849 {
9850 r_symndx = osec->target_index;
9851 if (r_symndx == STN_UNDEF)
9852 {
9853 irela->r_addend += osec->vma;
9854 osec = _bfd_nearby_section (output_bfd, osec,
9855 osec->vma);
9856 irela->r_addend -= osec->vma;
9857 r_symndx = osec->target_index;
9858 }
9859 }
9860 }
9861
9862 /* Adjust the addend according to where the
9863 section winds up in the output section. */
9864 if (rela_normal)
9865 irela->r_addend += sec->output_offset;
9866 }
9867 else
9868 {
9869 if (flinfo->indices[r_symndx] == -1)
9870 {
9871 unsigned long shlink;
9872 const char *name;
9873 asection *osec;
9874 long indx;
9875
9876 if (flinfo->info->strip == strip_all)
9877 {
9878 /* You can't do ld -r -s. */
9879 bfd_set_error (bfd_error_invalid_operation);
9880 return FALSE;
9881 }
9882
9883 /* This symbol was skipped earlier, but
9884 since it is needed by a reloc, we
9885 must output it now. */
9886 shlink = symtab_hdr->sh_link;
9887 name = (bfd_elf_string_from_elf_section
9888 (input_bfd, shlink, sym.st_name));
9889 if (name == NULL)
9890 return FALSE;
9891
9892 osec = sec->output_section;
9893 sym.st_shndx =
9894 _bfd_elf_section_from_bfd_section (output_bfd,
9895 osec);
9896 if (sym.st_shndx == SHN_BAD)
9897 return FALSE;
9898
9899 sym.st_value += sec->output_offset;
9900 if (!flinfo->info->relocatable)
9901 {
9902 sym.st_value += osec->vma;
9903 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
9904 {
9905 /* STT_TLS symbols are relative to PT_TLS
9906 segment base. */
9907 BFD_ASSERT (elf_hash_table (flinfo->info)
9908 ->tls_sec != NULL);
9909 sym.st_value -= (elf_hash_table (flinfo->info)
9910 ->tls_sec->vma);
9911 }
9912 }
9913
9914 indx = bfd_get_symcount (output_bfd);
9915 ret = elf_link_output_sym (flinfo, name, &sym, sec,
9916 NULL);
9917 if (ret == 0)
9918 return FALSE;
9919 else if (ret == 1)
9920 flinfo->indices[r_symndx] = indx;
9921 else
9922 abort ();
9923 }
9924
9925 r_symndx = flinfo->indices[r_symndx];
9926 }
9927
9928 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
9929 | (irela->r_info & r_type_mask));
9930 }
9931
9932 /* Swap out the relocs. */
9933 input_rel_hdr = esdi->rel.hdr;
9934 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
9935 {
9936 if (!bed->elf_backend_emit_relocs (output_bfd, o,
9937 input_rel_hdr,
9938 internal_relocs,
9939 rel_hash_list))
9940 return FALSE;
9941 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
9942 * bed->s->int_rels_per_ext_rel);
9943 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
9944 }
9945
9946 input_rela_hdr = esdi->rela.hdr;
9947 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
9948 {
9949 if (!bed->elf_backend_emit_relocs (output_bfd, o,
9950 input_rela_hdr,
9951 internal_relocs,
9952 rela_hash_list))
9953 return FALSE;
9954 }
9955 }
9956 }
9957
9958 /* Write out the modified section contents. */
9959 if (bed->elf_backend_write_section
9960 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
9961 contents))
9962 {
9963 /* Section written out. */
9964 }
9965 else switch (o->sec_info_type)
9966 {
9967 case SEC_INFO_TYPE_STABS:
9968 if (! (_bfd_write_section_stabs
9969 (output_bfd,
9970 &elf_hash_table (flinfo->info)->stab_info,
9971 o, &elf_section_data (o)->sec_info, contents)))
9972 return FALSE;
9973 break;
9974 case SEC_INFO_TYPE_MERGE:
9975 if (! _bfd_write_merged_section (output_bfd, o,
9976 elf_section_data (o)->sec_info))
9977 return FALSE;
9978 break;
9979 case SEC_INFO_TYPE_EH_FRAME:
9980 {
9981 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
9982 o, contents))
9983 return FALSE;
9984 }
9985 break;
9986 default:
9987 {
9988 /* FIXME: octets_per_byte. */
9989 if (! (o->flags & SEC_EXCLUDE))
9990 {
9991 file_ptr offset = (file_ptr) o->output_offset;
9992 bfd_size_type todo = o->size;
9993 if ((o->flags & SEC_ELF_REVERSE_COPY))
9994 {
9995 /* Reverse-copy input section to output. */
9996 do
9997 {
9998 todo -= address_size;
9999 if (! bfd_set_section_contents (output_bfd,
10000 o->output_section,
10001 contents + todo,
10002 offset,
10003 address_size))
10004 return FALSE;
10005 if (todo == 0)
10006 break;
10007 offset += address_size;
10008 }
10009 while (1);
10010 }
10011 else if (! bfd_set_section_contents (output_bfd,
10012 o->output_section,
10013 contents,
10014 offset, todo))
10015 return FALSE;
10016 }
10017 }
10018 break;
10019 }
10020 }
10021
10022 return TRUE;
10023 }
10024
10025 /* Generate a reloc when linking an ELF file. This is a reloc
10026 requested by the linker, and does not come from any input file. This
10027 is used to build constructor and destructor tables when linking
10028 with -Ur. */
10029
10030 static bfd_boolean
10031 elf_reloc_link_order (bfd *output_bfd,
10032 struct bfd_link_info *info,
10033 asection *output_section,
10034 struct bfd_link_order *link_order)
10035 {
10036 reloc_howto_type *howto;
10037 long indx;
10038 bfd_vma offset;
10039 bfd_vma addend;
10040 struct bfd_elf_section_reloc_data *reldata;
10041 struct elf_link_hash_entry **rel_hash_ptr;
10042 Elf_Internal_Shdr *rel_hdr;
10043 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10044 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10045 bfd_byte *erel;
10046 unsigned int i;
10047 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
10048
10049 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10050 if (howto == NULL)
10051 {
10052 bfd_set_error (bfd_error_bad_value);
10053 return FALSE;
10054 }
10055
10056 addend = link_order->u.reloc.p->addend;
10057
10058 if (esdo->rel.hdr)
10059 reldata = &esdo->rel;
10060 else if (esdo->rela.hdr)
10061 reldata = &esdo->rela;
10062 else
10063 {
10064 reldata = NULL;
10065 BFD_ASSERT (0);
10066 }
10067
10068 /* Figure out the symbol index. */
10069 rel_hash_ptr = reldata->hashes + reldata->count;
10070 if (link_order->type == bfd_section_reloc_link_order)
10071 {
10072 indx = link_order->u.reloc.p->u.section->target_index;
10073 BFD_ASSERT (indx != 0);
10074 *rel_hash_ptr = NULL;
10075 }
10076 else
10077 {
10078 struct elf_link_hash_entry *h;
10079
10080 /* Treat a reloc against a defined symbol as though it were
10081 actually against the section. */
10082 h = ((struct elf_link_hash_entry *)
10083 bfd_wrapped_link_hash_lookup (output_bfd, info,
10084 link_order->u.reloc.p->u.name,
10085 FALSE, FALSE, TRUE));
10086 if (h != NULL
10087 && (h->root.type == bfd_link_hash_defined
10088 || h->root.type == bfd_link_hash_defweak))
10089 {
10090 asection *section;
10091
10092 section = h->root.u.def.section;
10093 indx = section->output_section->target_index;
10094 *rel_hash_ptr = NULL;
10095 /* It seems that we ought to add the symbol value to the
10096 addend here, but in practice it has already been added
10097 because it was passed to constructor_callback. */
10098 addend += section->output_section->vma + section->output_offset;
10099 }
10100 else if (h != NULL)
10101 {
10102 /* Setting the index to -2 tells elf_link_output_extsym that
10103 this symbol is used by a reloc. */
10104 h->indx = -2;
10105 *rel_hash_ptr = h;
10106 indx = 0;
10107 }
10108 else
10109 {
10110 if (! ((*info->callbacks->unattached_reloc)
10111 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
10112 return FALSE;
10113 indx = 0;
10114 }
10115 }
10116
10117 /* If this is an inplace reloc, we must write the addend into the
10118 object file. */
10119 if (howto->partial_inplace && addend != 0)
10120 {
10121 bfd_size_type size;
10122 bfd_reloc_status_type rstat;
10123 bfd_byte *buf;
10124 bfd_boolean ok;
10125 const char *sym_name;
10126
10127 size = (bfd_size_type) bfd_get_reloc_size (howto);
10128 buf = (bfd_byte *) bfd_zmalloc (size);
10129 if (buf == NULL)
10130 return FALSE;
10131 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10132 switch (rstat)
10133 {
10134 case bfd_reloc_ok:
10135 break;
10136
10137 default:
10138 case bfd_reloc_outofrange:
10139 abort ();
10140
10141 case bfd_reloc_overflow:
10142 if (link_order->type == bfd_section_reloc_link_order)
10143 sym_name = bfd_section_name (output_bfd,
10144 link_order->u.reloc.p->u.section);
10145 else
10146 sym_name = link_order->u.reloc.p->u.name;
10147 if (! ((*info->callbacks->reloc_overflow)
10148 (info, NULL, sym_name, howto->name, addend, NULL,
10149 NULL, (bfd_vma) 0)))
10150 {
10151 free (buf);
10152 return FALSE;
10153 }
10154 break;
10155 }
10156 ok = bfd_set_section_contents (output_bfd, output_section, buf,
10157 link_order->offset, size);
10158 free (buf);
10159 if (! ok)
10160 return FALSE;
10161 }
10162
10163 /* The address of a reloc is relative to the section in a
10164 relocatable file, and is a virtual address in an executable
10165 file. */
10166 offset = link_order->offset;
10167 if (! info->relocatable)
10168 offset += output_section->vma;
10169
10170 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10171 {
10172 irel[i].r_offset = offset;
10173 irel[i].r_info = 0;
10174 irel[i].r_addend = 0;
10175 }
10176 if (bed->s->arch_size == 32)
10177 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10178 else
10179 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10180
10181 rel_hdr = reldata->hdr;
10182 erel = rel_hdr->contents;
10183 if (rel_hdr->sh_type == SHT_REL)
10184 {
10185 erel += reldata->count * bed->s->sizeof_rel;
10186 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10187 }
10188 else
10189 {
10190 irel[0].r_addend = addend;
10191 erel += reldata->count * bed->s->sizeof_rela;
10192 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10193 }
10194
10195 ++reldata->count;
10196
10197 return TRUE;
10198 }
10199
10200
10201 /* Get the output vma of the section pointed to by the sh_link field. */
10202
10203 static bfd_vma
10204 elf_get_linked_section_vma (struct bfd_link_order *p)
10205 {
10206 Elf_Internal_Shdr **elf_shdrp;
10207 asection *s;
10208 int elfsec;
10209
10210 s = p->u.indirect.section;
10211 elf_shdrp = elf_elfsections (s->owner);
10212 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10213 elfsec = elf_shdrp[elfsec]->sh_link;
10214 /* PR 290:
10215 The Intel C compiler generates SHT_IA_64_UNWIND with
10216 SHF_LINK_ORDER. But it doesn't set the sh_link or
10217 sh_info fields. Hence we could get the situation
10218 where elfsec is 0. */
10219 if (elfsec == 0)
10220 {
10221 const struct elf_backend_data *bed
10222 = get_elf_backend_data (s->owner);
10223 if (bed->link_order_error_handler)
10224 bed->link_order_error_handler
10225 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
10226 return 0;
10227 }
10228 else
10229 {
10230 s = elf_shdrp[elfsec]->bfd_section;
10231 return s->output_section->vma + s->output_offset;
10232 }
10233 }
10234
10235
10236 /* Compare two sections based on the locations of the sections they are
10237 linked to. Used by elf_fixup_link_order. */
10238
10239 static int
10240 compare_link_order (const void * a, const void * b)
10241 {
10242 bfd_vma apos;
10243 bfd_vma bpos;
10244
10245 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10246 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10247 if (apos < bpos)
10248 return -1;
10249 return apos > bpos;
10250 }
10251
10252
10253 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10254 order as their linked sections. Returns false if this could not be done
10255 because an output section includes both ordered and unordered
10256 sections. Ideally we'd do this in the linker proper. */
10257
10258 static bfd_boolean
10259 elf_fixup_link_order (bfd *abfd, asection *o)
10260 {
10261 int seen_linkorder;
10262 int seen_other;
10263 int n;
10264 struct bfd_link_order *p;
10265 bfd *sub;
10266 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10267 unsigned elfsec;
10268 struct bfd_link_order **sections;
10269 asection *s, *other_sec, *linkorder_sec;
10270 bfd_vma offset;
10271
10272 other_sec = NULL;
10273 linkorder_sec = NULL;
10274 seen_other = 0;
10275 seen_linkorder = 0;
10276 for (p = o->map_head.link_order; p != NULL; p = p->next)
10277 {
10278 if (p->type == bfd_indirect_link_order)
10279 {
10280 s = p->u.indirect.section;
10281 sub = s->owner;
10282 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10283 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
10284 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
10285 && elfsec < elf_numsections (sub)
10286 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
10287 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
10288 {
10289 seen_linkorder++;
10290 linkorder_sec = s;
10291 }
10292 else
10293 {
10294 seen_other++;
10295 other_sec = s;
10296 }
10297 }
10298 else
10299 seen_other++;
10300
10301 if (seen_other && seen_linkorder)
10302 {
10303 if (other_sec && linkorder_sec)
10304 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10305 o, linkorder_sec,
10306 linkorder_sec->owner, other_sec,
10307 other_sec->owner);
10308 else
10309 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
10310 o);
10311 bfd_set_error (bfd_error_bad_value);
10312 return FALSE;
10313 }
10314 }
10315
10316 if (!seen_linkorder)
10317 return TRUE;
10318
10319 sections = (struct bfd_link_order **)
10320 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
10321 if (sections == NULL)
10322 return FALSE;
10323 seen_linkorder = 0;
10324
10325 for (p = o->map_head.link_order; p != NULL; p = p->next)
10326 {
10327 sections[seen_linkorder++] = p;
10328 }
10329 /* Sort the input sections in the order of their linked section. */
10330 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
10331 compare_link_order);
10332
10333 /* Change the offsets of the sections. */
10334 offset = 0;
10335 for (n = 0; n < seen_linkorder; n++)
10336 {
10337 s = sections[n]->u.indirect.section;
10338 offset &= ~(bfd_vma) 0 << s->alignment_power;
10339 s->output_offset = offset;
10340 sections[n]->offset = offset;
10341 /* FIXME: octets_per_byte. */
10342 offset += sections[n]->size;
10343 }
10344
10345 free (sections);
10346 return TRUE;
10347 }
10348
10349
10350 /* Do the final step of an ELF link. */
10351
10352 bfd_boolean
10353 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
10354 {
10355 bfd_boolean dynamic;
10356 bfd_boolean emit_relocs;
10357 bfd *dynobj;
10358 struct elf_final_link_info flinfo;
10359 asection *o;
10360 struct bfd_link_order *p;
10361 bfd *sub;
10362 bfd_size_type max_contents_size;
10363 bfd_size_type max_external_reloc_size;
10364 bfd_size_type max_internal_reloc_count;
10365 bfd_size_type max_sym_count;
10366 bfd_size_type max_sym_shndx_count;
10367 file_ptr off;
10368 Elf_Internal_Sym elfsym;
10369 unsigned int i;
10370 Elf_Internal_Shdr *symtab_hdr;
10371 Elf_Internal_Shdr *symtab_shndx_hdr;
10372 Elf_Internal_Shdr *symstrtab_hdr;
10373 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10374 struct elf_outext_info eoinfo;
10375 bfd_boolean merged;
10376 size_t relativecount = 0;
10377 asection *reldyn = 0;
10378 bfd_size_type amt;
10379 asection *attr_section = NULL;
10380 bfd_vma attr_size = 0;
10381 const char *std_attrs_section;
10382
10383 if (! is_elf_hash_table (info->hash))
10384 return FALSE;
10385
10386 if (info->shared)
10387 abfd->flags |= DYNAMIC;
10388
10389 dynamic = elf_hash_table (info)->dynamic_sections_created;
10390 dynobj = elf_hash_table (info)->dynobj;
10391
10392 emit_relocs = (info->relocatable
10393 || info->emitrelocations);
10394
10395 flinfo.info = info;
10396 flinfo.output_bfd = abfd;
10397 flinfo.symstrtab = _bfd_elf_stringtab_init ();
10398 if (flinfo.symstrtab == NULL)
10399 return FALSE;
10400
10401 if (! dynamic)
10402 {
10403 flinfo.dynsym_sec = NULL;
10404 flinfo.hash_sec = NULL;
10405 flinfo.symver_sec = NULL;
10406 }
10407 else
10408 {
10409 flinfo.dynsym_sec = bfd_get_linker_section (dynobj, ".dynsym");
10410 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
10411 /* Note that dynsym_sec can be NULL (on VMS). */
10412 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
10413 /* Note that it is OK if symver_sec is NULL. */
10414 }
10415
10416 flinfo.contents = NULL;
10417 flinfo.external_relocs = NULL;
10418 flinfo.internal_relocs = NULL;
10419 flinfo.external_syms = NULL;
10420 flinfo.locsym_shndx = NULL;
10421 flinfo.internal_syms = NULL;
10422 flinfo.indices = NULL;
10423 flinfo.sections = NULL;
10424 flinfo.symbuf = NULL;
10425 flinfo.symshndxbuf = NULL;
10426 flinfo.symbuf_count = 0;
10427 flinfo.shndxbuf_size = 0;
10428 flinfo.filesym_count = 0;
10429
10430 /* The object attributes have been merged. Remove the input
10431 sections from the link, and set the contents of the output
10432 secton. */
10433 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
10434 for (o = abfd->sections; o != NULL; o = o->next)
10435 {
10436 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
10437 || strcmp (o->name, ".gnu.attributes") == 0)
10438 {
10439 for (p = o->map_head.link_order; p != NULL; p = p->next)
10440 {
10441 asection *input_section;
10442
10443 if (p->type != bfd_indirect_link_order)
10444 continue;
10445 input_section = p->u.indirect.section;
10446 /* Hack: reset the SEC_HAS_CONTENTS flag so that
10447 elf_link_input_bfd ignores this section. */
10448 input_section->flags &= ~SEC_HAS_CONTENTS;
10449 }
10450
10451 attr_size = bfd_elf_obj_attr_size (abfd);
10452 if (attr_size)
10453 {
10454 bfd_set_section_size (abfd, o, attr_size);
10455 attr_section = o;
10456 /* Skip this section later on. */
10457 o->map_head.link_order = NULL;
10458 }
10459 else
10460 o->flags |= SEC_EXCLUDE;
10461 }
10462 }
10463
10464 /* Count up the number of relocations we will output for each output
10465 section, so that we know the sizes of the reloc sections. We
10466 also figure out some maximum sizes. */
10467 max_contents_size = 0;
10468 max_external_reloc_size = 0;
10469 max_internal_reloc_count = 0;
10470 max_sym_count = 0;
10471 max_sym_shndx_count = 0;
10472 merged = FALSE;
10473 for (o = abfd->sections; o != NULL; o = o->next)
10474 {
10475 struct bfd_elf_section_data *esdo = elf_section_data (o);
10476 o->reloc_count = 0;
10477
10478 for (p = o->map_head.link_order; p != NULL; p = p->next)
10479 {
10480 unsigned int reloc_count = 0;
10481 struct bfd_elf_section_data *esdi = NULL;
10482
10483 if (p->type == bfd_section_reloc_link_order
10484 || p->type == bfd_symbol_reloc_link_order)
10485 reloc_count = 1;
10486 else if (p->type == bfd_indirect_link_order)
10487 {
10488 asection *sec;
10489
10490 sec = p->u.indirect.section;
10491 esdi = elf_section_data (sec);
10492
10493 /* Mark all sections which are to be included in the
10494 link. This will normally be every section. We need
10495 to do this so that we can identify any sections which
10496 the linker has decided to not include. */
10497 sec->linker_mark = TRUE;
10498
10499 if (sec->flags & SEC_MERGE)
10500 merged = TRUE;
10501
10502 if (esdo->this_hdr.sh_type == SHT_REL
10503 || esdo->this_hdr.sh_type == SHT_RELA)
10504 /* Some backends use reloc_count in relocation sections
10505 to count particular types of relocs. Of course,
10506 reloc sections themselves can't have relocations. */
10507 reloc_count = 0;
10508 else if (info->relocatable || info->emitrelocations)
10509 reloc_count = sec->reloc_count;
10510 else if (bed->elf_backend_count_relocs)
10511 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
10512
10513 if (sec->rawsize > max_contents_size)
10514 max_contents_size = sec->rawsize;
10515 if (sec->size > max_contents_size)
10516 max_contents_size = sec->size;
10517
10518 /* We are interested in just local symbols, not all
10519 symbols. */
10520 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
10521 && (sec->owner->flags & DYNAMIC) == 0)
10522 {
10523 size_t sym_count;
10524
10525 if (elf_bad_symtab (sec->owner))
10526 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
10527 / bed->s->sizeof_sym);
10528 else
10529 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
10530
10531 if (sym_count > max_sym_count)
10532 max_sym_count = sym_count;
10533
10534 if (sym_count > max_sym_shndx_count
10535 && elf_symtab_shndx (sec->owner) != 0)
10536 max_sym_shndx_count = sym_count;
10537
10538 if ((sec->flags & SEC_RELOC) != 0)
10539 {
10540 size_t ext_size = 0;
10541
10542 if (esdi->rel.hdr != NULL)
10543 ext_size = esdi->rel.hdr->sh_size;
10544 if (esdi->rela.hdr != NULL)
10545 ext_size += esdi->rela.hdr->sh_size;
10546
10547 if (ext_size > max_external_reloc_size)
10548 max_external_reloc_size = ext_size;
10549 if (sec->reloc_count > max_internal_reloc_count)
10550 max_internal_reloc_count = sec->reloc_count;
10551 }
10552 }
10553 }
10554
10555 if (reloc_count == 0)
10556 continue;
10557
10558 o->reloc_count += reloc_count;
10559
10560 if (p->type == bfd_indirect_link_order
10561 && (info->relocatable || info->emitrelocations))
10562 {
10563 if (esdi->rel.hdr)
10564 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
10565 if (esdi->rela.hdr)
10566 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
10567 }
10568 else
10569 {
10570 if (o->use_rela_p)
10571 esdo->rela.count += reloc_count;
10572 else
10573 esdo->rel.count += reloc_count;
10574 }
10575 }
10576
10577 if (o->reloc_count > 0)
10578 o->flags |= SEC_RELOC;
10579 else
10580 {
10581 /* Explicitly clear the SEC_RELOC flag. The linker tends to
10582 set it (this is probably a bug) and if it is set
10583 assign_section_numbers will create a reloc section. */
10584 o->flags &=~ SEC_RELOC;
10585 }
10586
10587 /* If the SEC_ALLOC flag is not set, force the section VMA to
10588 zero. This is done in elf_fake_sections as well, but forcing
10589 the VMA to 0 here will ensure that relocs against these
10590 sections are handled correctly. */
10591 if ((o->flags & SEC_ALLOC) == 0
10592 && ! o->user_set_vma)
10593 o->vma = 0;
10594 }
10595
10596 if (! info->relocatable && merged)
10597 elf_link_hash_traverse (elf_hash_table (info),
10598 _bfd_elf_link_sec_merge_syms, abfd);
10599
10600 /* Figure out the file positions for everything but the symbol table
10601 and the relocs. We set symcount to force assign_section_numbers
10602 to create a symbol table. */
10603 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
10604 BFD_ASSERT (! abfd->output_has_begun);
10605 if (! _bfd_elf_compute_section_file_positions (abfd, info))
10606 goto error_return;
10607
10608 /* Set sizes, and assign file positions for reloc sections. */
10609 for (o = abfd->sections; o != NULL; o = o->next)
10610 {
10611 struct bfd_elf_section_data *esdo = elf_section_data (o);
10612 if ((o->flags & SEC_RELOC) != 0)
10613 {
10614 if (esdo->rel.hdr
10615 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
10616 goto error_return;
10617
10618 if (esdo->rela.hdr
10619 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
10620 goto error_return;
10621 }
10622
10623 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
10624 to count upwards while actually outputting the relocations. */
10625 esdo->rel.count = 0;
10626 esdo->rela.count = 0;
10627 }
10628
10629 _bfd_elf_assign_file_positions_for_relocs (abfd);
10630
10631 /* We have now assigned file positions for all the sections except
10632 .symtab and .strtab. We start the .symtab section at the current
10633 file position, and write directly to it. We build the .strtab
10634 section in memory. */
10635 bfd_get_symcount (abfd) = 0;
10636 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10637 /* sh_name is set in prep_headers. */
10638 symtab_hdr->sh_type = SHT_SYMTAB;
10639 /* sh_flags, sh_addr and sh_size all start off zero. */
10640 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
10641 /* sh_link is set in assign_section_numbers. */
10642 /* sh_info is set below. */
10643 /* sh_offset is set just below. */
10644 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
10645
10646 off = elf_tdata (abfd)->next_file_pos;
10647 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
10648
10649 /* Note that at this point elf_tdata (abfd)->next_file_pos is
10650 incorrect. We do not yet know the size of the .symtab section.
10651 We correct next_file_pos below, after we do know the size. */
10652
10653 /* Allocate a buffer to hold swapped out symbols. This is to avoid
10654 continuously seeking to the right position in the file. */
10655 if (! info->keep_memory || max_sym_count < 20)
10656 flinfo.symbuf_size = 20;
10657 else
10658 flinfo.symbuf_size = max_sym_count;
10659 amt = flinfo.symbuf_size;
10660 amt *= bed->s->sizeof_sym;
10661 flinfo.symbuf = (bfd_byte *) bfd_malloc (amt);
10662 if (flinfo.symbuf == NULL)
10663 goto error_return;
10664 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
10665 {
10666 /* Wild guess at number of output symbols. realloc'd as needed. */
10667 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
10668 flinfo.shndxbuf_size = amt;
10669 amt *= sizeof (Elf_External_Sym_Shndx);
10670 flinfo.symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
10671 if (flinfo.symshndxbuf == NULL)
10672 goto error_return;
10673 }
10674
10675 /* Start writing out the symbol table. The first symbol is always a
10676 dummy symbol. */
10677 if (info->strip != strip_all
10678 || emit_relocs)
10679 {
10680 elfsym.st_value = 0;
10681 elfsym.st_size = 0;
10682 elfsym.st_info = 0;
10683 elfsym.st_other = 0;
10684 elfsym.st_shndx = SHN_UNDEF;
10685 elfsym.st_target_internal = 0;
10686 if (elf_link_output_sym (&flinfo, NULL, &elfsym, bfd_und_section_ptr,
10687 NULL) != 1)
10688 goto error_return;
10689 }
10690
10691 /* Output a symbol for each section. We output these even if we are
10692 discarding local symbols, since they are used for relocs. These
10693 symbols have no names. We store the index of each one in the
10694 index field of the section, so that we can find it again when
10695 outputting relocs. */
10696 if (info->strip != strip_all
10697 || emit_relocs)
10698 {
10699 elfsym.st_size = 0;
10700 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
10701 elfsym.st_other = 0;
10702 elfsym.st_value = 0;
10703 elfsym.st_target_internal = 0;
10704 for (i = 1; i < elf_numsections (abfd); i++)
10705 {
10706 o = bfd_section_from_elf_index (abfd, i);
10707 if (o != NULL)
10708 {
10709 o->target_index = bfd_get_symcount (abfd);
10710 elfsym.st_shndx = i;
10711 if (!info->relocatable)
10712 elfsym.st_value = o->vma;
10713 if (elf_link_output_sym (&flinfo, NULL, &elfsym, o, NULL) != 1)
10714 goto error_return;
10715 }
10716 }
10717 }
10718
10719 /* Allocate some memory to hold information read in from the input
10720 files. */
10721 if (max_contents_size != 0)
10722 {
10723 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
10724 if (flinfo.contents == NULL)
10725 goto error_return;
10726 }
10727
10728 if (max_external_reloc_size != 0)
10729 {
10730 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
10731 if (flinfo.external_relocs == NULL)
10732 goto error_return;
10733 }
10734
10735 if (max_internal_reloc_count != 0)
10736 {
10737 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
10738 amt *= sizeof (Elf_Internal_Rela);
10739 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
10740 if (flinfo.internal_relocs == NULL)
10741 goto error_return;
10742 }
10743
10744 if (max_sym_count != 0)
10745 {
10746 amt = max_sym_count * bed->s->sizeof_sym;
10747 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
10748 if (flinfo.external_syms == NULL)
10749 goto error_return;
10750
10751 amt = max_sym_count * sizeof (Elf_Internal_Sym);
10752 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
10753 if (flinfo.internal_syms == NULL)
10754 goto error_return;
10755
10756 amt = max_sym_count * sizeof (long);
10757 flinfo.indices = (long int *) bfd_malloc (amt);
10758 if (flinfo.indices == NULL)
10759 goto error_return;
10760
10761 amt = max_sym_count * sizeof (asection *);
10762 flinfo.sections = (asection **) bfd_malloc (amt);
10763 if (flinfo.sections == NULL)
10764 goto error_return;
10765 }
10766
10767 if (max_sym_shndx_count != 0)
10768 {
10769 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
10770 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
10771 if (flinfo.locsym_shndx == NULL)
10772 goto error_return;
10773 }
10774
10775 if (elf_hash_table (info)->tls_sec)
10776 {
10777 bfd_vma base, end = 0;
10778 asection *sec;
10779
10780 for (sec = elf_hash_table (info)->tls_sec;
10781 sec && (sec->flags & SEC_THREAD_LOCAL);
10782 sec = sec->next)
10783 {
10784 bfd_size_type size = sec->size;
10785
10786 if (size == 0
10787 && (sec->flags & SEC_HAS_CONTENTS) == 0)
10788 {
10789 struct bfd_link_order *ord = sec->map_tail.link_order;
10790
10791 if (ord != NULL)
10792 size = ord->offset + ord->size;
10793 }
10794 end = sec->vma + size;
10795 }
10796 base = elf_hash_table (info)->tls_sec->vma;
10797 /* Only align end of TLS section if static TLS doesn't have special
10798 alignment requirements. */
10799 if (bed->static_tls_alignment == 1)
10800 end = align_power (end,
10801 elf_hash_table (info)->tls_sec->alignment_power);
10802 elf_hash_table (info)->tls_size = end - base;
10803 }
10804
10805 /* Reorder SHF_LINK_ORDER sections. */
10806 for (o = abfd->sections; o != NULL; o = o->next)
10807 {
10808 if (!elf_fixup_link_order (abfd, o))
10809 return FALSE;
10810 }
10811
10812 /* Since ELF permits relocations to be against local symbols, we
10813 must have the local symbols available when we do the relocations.
10814 Since we would rather only read the local symbols once, and we
10815 would rather not keep them in memory, we handle all the
10816 relocations for a single input file at the same time.
10817
10818 Unfortunately, there is no way to know the total number of local
10819 symbols until we have seen all of them, and the local symbol
10820 indices precede the global symbol indices. This means that when
10821 we are generating relocatable output, and we see a reloc against
10822 a global symbol, we can not know the symbol index until we have
10823 finished examining all the local symbols to see which ones we are
10824 going to output. To deal with this, we keep the relocations in
10825 memory, and don't output them until the end of the link. This is
10826 an unfortunate waste of memory, but I don't see a good way around
10827 it. Fortunately, it only happens when performing a relocatable
10828 link, which is not the common case. FIXME: If keep_memory is set
10829 we could write the relocs out and then read them again; I don't
10830 know how bad the memory loss will be. */
10831
10832 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10833 sub->output_has_begun = FALSE;
10834 for (o = abfd->sections; o != NULL; o = o->next)
10835 {
10836 for (p = o->map_head.link_order; p != NULL; p = p->next)
10837 {
10838 if (p->type == bfd_indirect_link_order
10839 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
10840 == bfd_target_elf_flavour)
10841 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
10842 {
10843 if (! sub->output_has_begun)
10844 {
10845 if (! elf_link_input_bfd (&flinfo, sub))
10846 goto error_return;
10847 sub->output_has_begun = TRUE;
10848 }
10849 }
10850 else if (p->type == bfd_section_reloc_link_order
10851 || p->type == bfd_symbol_reloc_link_order)
10852 {
10853 if (! elf_reloc_link_order (abfd, info, o, p))
10854 goto error_return;
10855 }
10856 else
10857 {
10858 if (! _bfd_default_link_order (abfd, info, o, p))
10859 {
10860 if (p->type == bfd_indirect_link_order
10861 && (bfd_get_flavour (sub)
10862 == bfd_target_elf_flavour)
10863 && (elf_elfheader (sub)->e_ident[EI_CLASS]
10864 != bed->s->elfclass))
10865 {
10866 const char *iclass, *oclass;
10867
10868 if (bed->s->elfclass == ELFCLASS64)
10869 {
10870 iclass = "ELFCLASS32";
10871 oclass = "ELFCLASS64";
10872 }
10873 else
10874 {
10875 iclass = "ELFCLASS64";
10876 oclass = "ELFCLASS32";
10877 }
10878
10879 bfd_set_error (bfd_error_wrong_format);
10880 (*_bfd_error_handler)
10881 (_("%B: file class %s incompatible with %s"),
10882 sub, iclass, oclass);
10883 }
10884
10885 goto error_return;
10886 }
10887 }
10888 }
10889 }
10890
10891 /* Free symbol buffer if needed. */
10892 if (!info->reduce_memory_overheads)
10893 {
10894 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10895 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10896 && elf_tdata (sub)->symbuf)
10897 {
10898 free (elf_tdata (sub)->symbuf);
10899 elf_tdata (sub)->symbuf = NULL;
10900 }
10901 }
10902
10903 /* Output a FILE symbol so that following locals are not associated
10904 with the wrong input file. */
10905 memset (&elfsym, 0, sizeof (elfsym));
10906 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10907 elfsym.st_shndx = SHN_ABS;
10908
10909 if (flinfo.filesym_count > 1
10910 && !elf_link_output_sym (&flinfo, NULL, &elfsym,
10911 bfd_und_section_ptr, NULL))
10912 return FALSE;
10913
10914 /* Output any global symbols that got converted to local in a
10915 version script or due to symbol visibility. We do this in a
10916 separate step since ELF requires all local symbols to appear
10917 prior to any global symbols. FIXME: We should only do this if
10918 some global symbols were, in fact, converted to become local.
10919 FIXME: Will this work correctly with the Irix 5 linker? */
10920 eoinfo.failed = FALSE;
10921 eoinfo.flinfo = &flinfo;
10922 eoinfo.localsyms = TRUE;
10923 eoinfo.need_second_pass = FALSE;
10924 eoinfo.second_pass = FALSE;
10925 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
10926 if (eoinfo.failed)
10927 return FALSE;
10928
10929 if (flinfo.filesym_count == 1
10930 && !elf_link_output_sym (&flinfo, NULL, &elfsym,
10931 bfd_und_section_ptr, NULL))
10932 return FALSE;
10933
10934 if (eoinfo.need_second_pass)
10935 {
10936 eoinfo.second_pass = TRUE;
10937 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
10938 if (eoinfo.failed)
10939 return FALSE;
10940 }
10941
10942 /* If backend needs to output some local symbols not present in the hash
10943 table, do it now. */
10944 if (bed->elf_backend_output_arch_local_syms)
10945 {
10946 typedef int (*out_sym_func)
10947 (void *, const char *, Elf_Internal_Sym *, asection *,
10948 struct elf_link_hash_entry *);
10949
10950 if (! ((*bed->elf_backend_output_arch_local_syms)
10951 (abfd, info, &flinfo, (out_sym_func) elf_link_output_sym)))
10952 return FALSE;
10953 }
10954
10955 /* That wrote out all the local symbols. Finish up the symbol table
10956 with the global symbols. Even if we want to strip everything we
10957 can, we still need to deal with those global symbols that got
10958 converted to local in a version script. */
10959
10960 /* The sh_info field records the index of the first non local symbol. */
10961 symtab_hdr->sh_info = bfd_get_symcount (abfd);
10962
10963 if (dynamic
10964 && flinfo.dynsym_sec != NULL
10965 && flinfo.dynsym_sec->output_section != bfd_abs_section_ptr)
10966 {
10967 Elf_Internal_Sym sym;
10968 bfd_byte *dynsym = flinfo.dynsym_sec->contents;
10969 long last_local = 0;
10970
10971 /* Write out the section symbols for the output sections. */
10972 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
10973 {
10974 asection *s;
10975
10976 sym.st_size = 0;
10977 sym.st_name = 0;
10978 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
10979 sym.st_other = 0;
10980 sym.st_target_internal = 0;
10981
10982 for (s = abfd->sections; s != NULL; s = s->next)
10983 {
10984 int indx;
10985 bfd_byte *dest;
10986 long dynindx;
10987
10988 dynindx = elf_section_data (s)->dynindx;
10989 if (dynindx <= 0)
10990 continue;
10991 indx = elf_section_data (s)->this_idx;
10992 BFD_ASSERT (indx > 0);
10993 sym.st_shndx = indx;
10994 if (! check_dynsym (abfd, &sym))
10995 return FALSE;
10996 sym.st_value = s->vma;
10997 dest = dynsym + dynindx * bed->s->sizeof_sym;
10998 if (last_local < dynindx)
10999 last_local = dynindx;
11000 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11001 }
11002 }
11003
11004 /* Write out the local dynsyms. */
11005 if (elf_hash_table (info)->dynlocal)
11006 {
11007 struct elf_link_local_dynamic_entry *e;
11008 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
11009 {
11010 asection *s;
11011 bfd_byte *dest;
11012
11013 /* Copy the internal symbol and turn off visibility.
11014 Note that we saved a word of storage and overwrote
11015 the original st_name with the dynstr_index. */
11016 sym = e->isym;
11017 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
11018
11019 s = bfd_section_from_elf_index (e->input_bfd,
11020 e->isym.st_shndx);
11021 if (s != NULL)
11022 {
11023 sym.st_shndx =
11024 elf_section_data (s->output_section)->this_idx;
11025 if (! check_dynsym (abfd, &sym))
11026 return FALSE;
11027 sym.st_value = (s->output_section->vma
11028 + s->output_offset
11029 + e->isym.st_value);
11030 }
11031
11032 if (last_local < e->dynindx)
11033 last_local = e->dynindx;
11034
11035 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
11036 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11037 }
11038 }
11039
11040 elf_section_data (flinfo.dynsym_sec->output_section)->this_hdr.sh_info =
11041 last_local + 1;
11042 }
11043
11044 /* We get the global symbols from the hash table. */
11045 eoinfo.failed = FALSE;
11046 eoinfo.localsyms = FALSE;
11047 eoinfo.flinfo = &flinfo;
11048 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11049 if (eoinfo.failed)
11050 return FALSE;
11051
11052 /* If backend needs to output some symbols not present in the hash
11053 table, do it now. */
11054 if (bed->elf_backend_output_arch_syms)
11055 {
11056 typedef int (*out_sym_func)
11057 (void *, const char *, Elf_Internal_Sym *, asection *,
11058 struct elf_link_hash_entry *);
11059
11060 if (! ((*bed->elf_backend_output_arch_syms)
11061 (abfd, info, &flinfo, (out_sym_func) elf_link_output_sym)))
11062 return FALSE;
11063 }
11064
11065 /* Flush all symbols to the file. */
11066 if (! elf_link_flush_output_syms (&flinfo, bed))
11067 return FALSE;
11068
11069 /* Now we know the size of the symtab section. */
11070 off += symtab_hdr->sh_size;
11071
11072 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
11073 if (symtab_shndx_hdr->sh_name != 0)
11074 {
11075 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
11076 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
11077 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
11078 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
11079 symtab_shndx_hdr->sh_size = amt;
11080
11081 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
11082 off, TRUE);
11083
11084 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
11085 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
11086 return FALSE;
11087 }
11088
11089
11090 /* Finish up and write out the symbol string table (.strtab)
11091 section. */
11092 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
11093 /* sh_name was set in prep_headers. */
11094 symstrtab_hdr->sh_type = SHT_STRTAB;
11095 symstrtab_hdr->sh_flags = 0;
11096 symstrtab_hdr->sh_addr = 0;
11097 symstrtab_hdr->sh_size = _bfd_stringtab_size (flinfo.symstrtab);
11098 symstrtab_hdr->sh_entsize = 0;
11099 symstrtab_hdr->sh_link = 0;
11100 symstrtab_hdr->sh_info = 0;
11101 /* sh_offset is set just below. */
11102 symstrtab_hdr->sh_addralign = 1;
11103
11104 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
11105 elf_tdata (abfd)->next_file_pos = off;
11106
11107 if (bfd_get_symcount (abfd) > 0)
11108 {
11109 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
11110 || ! _bfd_stringtab_emit (abfd, flinfo.symstrtab))
11111 return FALSE;
11112 }
11113
11114 /* Adjust the relocs to have the correct symbol indices. */
11115 for (o = abfd->sections; o != NULL; o = o->next)
11116 {
11117 struct bfd_elf_section_data *esdo = elf_section_data (o);
11118 if ((o->flags & SEC_RELOC) == 0)
11119 continue;
11120
11121 if (esdo->rel.hdr != NULL)
11122 elf_link_adjust_relocs (abfd, &esdo->rel);
11123 if (esdo->rela.hdr != NULL)
11124 elf_link_adjust_relocs (abfd, &esdo->rela);
11125
11126 /* Set the reloc_count field to 0 to prevent write_relocs from
11127 trying to swap the relocs out itself. */
11128 o->reloc_count = 0;
11129 }
11130
11131 if (dynamic && info->combreloc && dynobj != NULL)
11132 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
11133
11134 /* If we are linking against a dynamic object, or generating a
11135 shared library, finish up the dynamic linking information. */
11136 if (dynamic)
11137 {
11138 bfd_byte *dyncon, *dynconend;
11139
11140 /* Fix up .dynamic entries. */
11141 o = bfd_get_linker_section (dynobj, ".dynamic");
11142 BFD_ASSERT (o != NULL);
11143
11144 dyncon = o->contents;
11145 dynconend = o->contents + o->size;
11146 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11147 {
11148 Elf_Internal_Dyn dyn;
11149 const char *name;
11150 unsigned int type;
11151
11152 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11153
11154 switch (dyn.d_tag)
11155 {
11156 default:
11157 continue;
11158 case DT_NULL:
11159 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
11160 {
11161 switch (elf_section_data (reldyn)->this_hdr.sh_type)
11162 {
11163 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
11164 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
11165 default: continue;
11166 }
11167 dyn.d_un.d_val = relativecount;
11168 relativecount = 0;
11169 break;
11170 }
11171 continue;
11172
11173 case DT_INIT:
11174 name = info->init_function;
11175 goto get_sym;
11176 case DT_FINI:
11177 name = info->fini_function;
11178 get_sym:
11179 {
11180 struct elf_link_hash_entry *h;
11181
11182 h = elf_link_hash_lookup (elf_hash_table (info), name,
11183 FALSE, FALSE, TRUE);
11184 if (h != NULL
11185 && (h->root.type == bfd_link_hash_defined
11186 || h->root.type == bfd_link_hash_defweak))
11187 {
11188 dyn.d_un.d_ptr = h->root.u.def.value;
11189 o = h->root.u.def.section;
11190 if (o->output_section != NULL)
11191 dyn.d_un.d_ptr += (o->output_section->vma
11192 + o->output_offset);
11193 else
11194 {
11195 /* The symbol is imported from another shared
11196 library and does not apply to this one. */
11197 dyn.d_un.d_ptr = 0;
11198 }
11199 break;
11200 }
11201 }
11202 continue;
11203
11204 case DT_PREINIT_ARRAYSZ:
11205 name = ".preinit_array";
11206 goto get_size;
11207 case DT_INIT_ARRAYSZ:
11208 name = ".init_array";
11209 goto get_size;
11210 case DT_FINI_ARRAYSZ:
11211 name = ".fini_array";
11212 get_size:
11213 o = bfd_get_section_by_name (abfd, name);
11214 if (o == NULL)
11215 {
11216 (*_bfd_error_handler)
11217 (_("%B: could not find output section %s"), abfd, name);
11218 goto error_return;
11219 }
11220 if (o->size == 0)
11221 (*_bfd_error_handler)
11222 (_("warning: %s section has zero size"), name);
11223 dyn.d_un.d_val = o->size;
11224 break;
11225
11226 case DT_PREINIT_ARRAY:
11227 name = ".preinit_array";
11228 goto get_vma;
11229 case DT_INIT_ARRAY:
11230 name = ".init_array";
11231 goto get_vma;
11232 case DT_FINI_ARRAY:
11233 name = ".fini_array";
11234 goto get_vma;
11235
11236 case DT_HASH:
11237 name = ".hash";
11238 goto get_vma;
11239 case DT_GNU_HASH:
11240 name = ".gnu.hash";
11241 goto get_vma;
11242 case DT_STRTAB:
11243 name = ".dynstr";
11244 goto get_vma;
11245 case DT_SYMTAB:
11246 name = ".dynsym";
11247 goto get_vma;
11248 case DT_VERDEF:
11249 name = ".gnu.version_d";
11250 goto get_vma;
11251 case DT_VERNEED:
11252 name = ".gnu.version_r";
11253 goto get_vma;
11254 case DT_VERSYM:
11255 name = ".gnu.version";
11256 get_vma:
11257 o = bfd_get_section_by_name (abfd, name);
11258 if (o == NULL)
11259 {
11260 (*_bfd_error_handler)
11261 (_("%B: could not find output section %s"), abfd, name);
11262 goto error_return;
11263 }
11264 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
11265 {
11266 (*_bfd_error_handler)
11267 (_("warning: section '%s' is being made into a note"), name);
11268 bfd_set_error (bfd_error_nonrepresentable_section);
11269 goto error_return;
11270 }
11271 dyn.d_un.d_ptr = o->vma;
11272 break;
11273
11274 case DT_REL:
11275 case DT_RELA:
11276 case DT_RELSZ:
11277 case DT_RELASZ:
11278 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
11279 type = SHT_REL;
11280 else
11281 type = SHT_RELA;
11282 dyn.d_un.d_val = 0;
11283 dyn.d_un.d_ptr = 0;
11284 for (i = 1; i < elf_numsections (abfd); i++)
11285 {
11286 Elf_Internal_Shdr *hdr;
11287
11288 hdr = elf_elfsections (abfd)[i];
11289 if (hdr->sh_type == type
11290 && (hdr->sh_flags & SHF_ALLOC) != 0)
11291 {
11292 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
11293 dyn.d_un.d_val += hdr->sh_size;
11294 else
11295 {
11296 if (dyn.d_un.d_ptr == 0
11297 || hdr->sh_addr < dyn.d_un.d_ptr)
11298 dyn.d_un.d_ptr = hdr->sh_addr;
11299 }
11300 }
11301 }
11302 break;
11303 }
11304 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
11305 }
11306 }
11307
11308 /* If we have created any dynamic sections, then output them. */
11309 if (dynobj != NULL)
11310 {
11311 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
11312 goto error_return;
11313
11314 /* Check for DT_TEXTREL (late, in case the backend removes it). */
11315 if (((info->warn_shared_textrel && info->shared)
11316 || info->error_textrel)
11317 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
11318 {
11319 bfd_byte *dyncon, *dynconend;
11320
11321 dyncon = o->contents;
11322 dynconend = o->contents + o->size;
11323 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11324 {
11325 Elf_Internal_Dyn dyn;
11326
11327 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11328
11329 if (dyn.d_tag == DT_TEXTREL)
11330 {
11331 if (info->error_textrel)
11332 info->callbacks->einfo
11333 (_("%P%X: read-only segment has dynamic relocations.\n"));
11334 else
11335 info->callbacks->einfo
11336 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
11337 break;
11338 }
11339 }
11340 }
11341
11342 for (o = dynobj->sections; o != NULL; o = o->next)
11343 {
11344 if ((o->flags & SEC_HAS_CONTENTS) == 0
11345 || o->size == 0
11346 || o->output_section == bfd_abs_section_ptr)
11347 continue;
11348 if ((o->flags & SEC_LINKER_CREATED) == 0)
11349 {
11350 /* At this point, we are only interested in sections
11351 created by _bfd_elf_link_create_dynamic_sections. */
11352 continue;
11353 }
11354 if (elf_hash_table (info)->stab_info.stabstr == o)
11355 continue;
11356 if (elf_hash_table (info)->eh_info.hdr_sec == o)
11357 continue;
11358 if (strcmp (o->name, ".dynstr") != 0)
11359 {
11360 /* FIXME: octets_per_byte. */
11361 if (! bfd_set_section_contents (abfd, o->output_section,
11362 o->contents,
11363 (file_ptr) o->output_offset,
11364 o->size))
11365 goto error_return;
11366 }
11367 else
11368 {
11369 /* The contents of the .dynstr section are actually in a
11370 stringtab. */
11371 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
11372 if (bfd_seek (abfd, off, SEEK_SET) != 0
11373 || ! _bfd_elf_strtab_emit (abfd,
11374 elf_hash_table (info)->dynstr))
11375 goto error_return;
11376 }
11377 }
11378 }
11379
11380 if (info->relocatable)
11381 {
11382 bfd_boolean failed = FALSE;
11383
11384 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
11385 if (failed)
11386 goto error_return;
11387 }
11388
11389 /* If we have optimized stabs strings, output them. */
11390 if (elf_hash_table (info)->stab_info.stabstr != NULL)
11391 {
11392 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
11393 goto error_return;
11394 }
11395
11396 if (info->eh_frame_hdr)
11397 {
11398 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
11399 goto error_return;
11400 }
11401
11402 if (flinfo.symstrtab != NULL)
11403 _bfd_stringtab_free (flinfo.symstrtab);
11404 if (flinfo.contents != NULL)
11405 free (flinfo.contents);
11406 if (flinfo.external_relocs != NULL)
11407 free (flinfo.external_relocs);
11408 if (flinfo.internal_relocs != NULL)
11409 free (flinfo.internal_relocs);
11410 if (flinfo.external_syms != NULL)
11411 free (flinfo.external_syms);
11412 if (flinfo.locsym_shndx != NULL)
11413 free (flinfo.locsym_shndx);
11414 if (flinfo.internal_syms != NULL)
11415 free (flinfo.internal_syms);
11416 if (flinfo.indices != NULL)
11417 free (flinfo.indices);
11418 if (flinfo.sections != NULL)
11419 free (flinfo.sections);
11420 if (flinfo.symbuf != NULL)
11421 free (flinfo.symbuf);
11422 if (flinfo.symshndxbuf != NULL)
11423 free (flinfo.symshndxbuf);
11424 for (o = abfd->sections; o != NULL; o = o->next)
11425 {
11426 struct bfd_elf_section_data *esdo = elf_section_data (o);
11427 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11428 free (esdo->rel.hashes);
11429 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11430 free (esdo->rela.hashes);
11431 }
11432
11433 elf_tdata (abfd)->linker = TRUE;
11434
11435 if (attr_section)
11436 {
11437 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
11438 if (contents == NULL)
11439 return FALSE; /* Bail out and fail. */
11440 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
11441 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
11442 free (contents);
11443 }
11444
11445 return TRUE;
11446
11447 error_return:
11448 if (flinfo.symstrtab != NULL)
11449 _bfd_stringtab_free (flinfo.symstrtab);
11450 if (flinfo.contents != NULL)
11451 free (flinfo.contents);
11452 if (flinfo.external_relocs != NULL)
11453 free (flinfo.external_relocs);
11454 if (flinfo.internal_relocs != NULL)
11455 free (flinfo.internal_relocs);
11456 if (flinfo.external_syms != NULL)
11457 free (flinfo.external_syms);
11458 if (flinfo.locsym_shndx != NULL)
11459 free (flinfo.locsym_shndx);
11460 if (flinfo.internal_syms != NULL)
11461 free (flinfo.internal_syms);
11462 if (flinfo.indices != NULL)
11463 free (flinfo.indices);
11464 if (flinfo.sections != NULL)
11465 free (flinfo.sections);
11466 if (flinfo.symbuf != NULL)
11467 free (flinfo.symbuf);
11468 if (flinfo.symshndxbuf != NULL)
11469 free (flinfo.symshndxbuf);
11470 for (o = abfd->sections; o != NULL; o = o->next)
11471 {
11472 struct bfd_elf_section_data *esdo = elf_section_data (o);
11473 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11474 free (esdo->rel.hashes);
11475 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11476 free (esdo->rela.hashes);
11477 }
11478
11479 return FALSE;
11480 }
11481 \f
11482 /* Initialize COOKIE for input bfd ABFD. */
11483
11484 static bfd_boolean
11485 init_reloc_cookie (struct elf_reloc_cookie *cookie,
11486 struct bfd_link_info *info, bfd *abfd)
11487 {
11488 Elf_Internal_Shdr *symtab_hdr;
11489 const struct elf_backend_data *bed;
11490
11491 bed = get_elf_backend_data (abfd);
11492 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11493
11494 cookie->abfd = abfd;
11495 cookie->sym_hashes = elf_sym_hashes (abfd);
11496 cookie->bad_symtab = elf_bad_symtab (abfd);
11497 if (cookie->bad_symtab)
11498 {
11499 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11500 cookie->extsymoff = 0;
11501 }
11502 else
11503 {
11504 cookie->locsymcount = symtab_hdr->sh_info;
11505 cookie->extsymoff = symtab_hdr->sh_info;
11506 }
11507
11508 if (bed->s->arch_size == 32)
11509 cookie->r_sym_shift = 8;
11510 else
11511 cookie->r_sym_shift = 32;
11512
11513 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
11514 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
11515 {
11516 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
11517 cookie->locsymcount, 0,
11518 NULL, NULL, NULL);
11519 if (cookie->locsyms == NULL)
11520 {
11521 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
11522 return FALSE;
11523 }
11524 if (info->keep_memory)
11525 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
11526 }
11527 return TRUE;
11528 }
11529
11530 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
11531
11532 static void
11533 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
11534 {
11535 Elf_Internal_Shdr *symtab_hdr;
11536
11537 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11538 if (cookie->locsyms != NULL
11539 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
11540 free (cookie->locsyms);
11541 }
11542
11543 /* Initialize the relocation information in COOKIE for input section SEC
11544 of input bfd ABFD. */
11545
11546 static bfd_boolean
11547 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11548 struct bfd_link_info *info, bfd *abfd,
11549 asection *sec)
11550 {
11551 const struct elf_backend_data *bed;
11552
11553 if (sec->reloc_count == 0)
11554 {
11555 cookie->rels = NULL;
11556 cookie->relend = NULL;
11557 }
11558 else
11559 {
11560 bed = get_elf_backend_data (abfd);
11561
11562 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
11563 info->keep_memory);
11564 if (cookie->rels == NULL)
11565 return FALSE;
11566 cookie->rel = cookie->rels;
11567 cookie->relend = (cookie->rels
11568 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
11569 }
11570 cookie->rel = cookie->rels;
11571 return TRUE;
11572 }
11573
11574 /* Free the memory allocated by init_reloc_cookie_rels,
11575 if appropriate. */
11576
11577 static void
11578 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11579 asection *sec)
11580 {
11581 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
11582 free (cookie->rels);
11583 }
11584
11585 /* Initialize the whole of COOKIE for input section SEC. */
11586
11587 static bfd_boolean
11588 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11589 struct bfd_link_info *info,
11590 asection *sec)
11591 {
11592 if (!init_reloc_cookie (cookie, info, sec->owner))
11593 goto error1;
11594 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
11595 goto error2;
11596 return TRUE;
11597
11598 error2:
11599 fini_reloc_cookie (cookie, sec->owner);
11600 error1:
11601 return FALSE;
11602 }
11603
11604 /* Free the memory allocated by init_reloc_cookie_for_section,
11605 if appropriate. */
11606
11607 static void
11608 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11609 asection *sec)
11610 {
11611 fini_reloc_cookie_rels (cookie, sec);
11612 fini_reloc_cookie (cookie, sec->owner);
11613 }
11614 \f
11615 /* Garbage collect unused sections. */
11616
11617 /* Default gc_mark_hook. */
11618
11619 asection *
11620 _bfd_elf_gc_mark_hook (asection *sec,
11621 struct bfd_link_info *info ATTRIBUTE_UNUSED,
11622 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
11623 struct elf_link_hash_entry *h,
11624 Elf_Internal_Sym *sym)
11625 {
11626 const char *sec_name;
11627
11628 if (h != NULL)
11629 {
11630 switch (h->root.type)
11631 {
11632 case bfd_link_hash_defined:
11633 case bfd_link_hash_defweak:
11634 return h->root.u.def.section;
11635
11636 case bfd_link_hash_common:
11637 return h->root.u.c.p->section;
11638
11639 case bfd_link_hash_undefined:
11640 case bfd_link_hash_undefweak:
11641 /* To work around a glibc bug, keep all XXX input sections
11642 when there is an as yet undefined reference to __start_XXX
11643 or __stop_XXX symbols. The linker will later define such
11644 symbols for orphan input sections that have a name
11645 representable as a C identifier. */
11646 if (strncmp (h->root.root.string, "__start_", 8) == 0)
11647 sec_name = h->root.root.string + 8;
11648 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
11649 sec_name = h->root.root.string + 7;
11650 else
11651 sec_name = NULL;
11652
11653 if (sec_name && *sec_name != '\0')
11654 {
11655 bfd *i;
11656
11657 for (i = info->input_bfds; i; i = i->link_next)
11658 {
11659 sec = bfd_get_section_by_name (i, sec_name);
11660 if (sec)
11661 sec->flags |= SEC_KEEP;
11662 }
11663 }
11664 break;
11665
11666 default:
11667 break;
11668 }
11669 }
11670 else
11671 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
11672
11673 return NULL;
11674 }
11675
11676 /* COOKIE->rel describes a relocation against section SEC, which is
11677 a section we've decided to keep. Return the section that contains
11678 the relocation symbol, or NULL if no section contains it. */
11679
11680 asection *
11681 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
11682 elf_gc_mark_hook_fn gc_mark_hook,
11683 struct elf_reloc_cookie *cookie)
11684 {
11685 unsigned long r_symndx;
11686 struct elf_link_hash_entry *h;
11687
11688 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
11689 if (r_symndx == STN_UNDEF)
11690 return NULL;
11691
11692 if (r_symndx >= cookie->locsymcount
11693 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
11694 {
11695 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
11696 while (h->root.type == bfd_link_hash_indirect
11697 || h->root.type == bfd_link_hash_warning)
11698 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11699 h->mark = 1;
11700 /* If this symbol is weak and there is a non-weak definition, we
11701 keep the non-weak definition because many backends put
11702 dynamic reloc info on the non-weak definition for code
11703 handling copy relocs. */
11704 if (h->u.weakdef != NULL)
11705 h->u.weakdef->mark = 1;
11706 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
11707 }
11708
11709 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
11710 &cookie->locsyms[r_symndx]);
11711 }
11712
11713 /* COOKIE->rel describes a relocation against section SEC, which is
11714 a section we've decided to keep. Mark the section that contains
11715 the relocation symbol. */
11716
11717 bfd_boolean
11718 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
11719 asection *sec,
11720 elf_gc_mark_hook_fn gc_mark_hook,
11721 struct elf_reloc_cookie *cookie)
11722 {
11723 asection *rsec;
11724
11725 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie);
11726 if (rsec && !rsec->gc_mark)
11727 {
11728 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
11729 || (rsec->owner->flags & DYNAMIC) != 0)
11730 rsec->gc_mark = 1;
11731 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
11732 return FALSE;
11733 }
11734 return TRUE;
11735 }
11736
11737 /* The mark phase of garbage collection. For a given section, mark
11738 it and any sections in this section's group, and all the sections
11739 which define symbols to which it refers. */
11740
11741 bfd_boolean
11742 _bfd_elf_gc_mark (struct bfd_link_info *info,
11743 asection *sec,
11744 elf_gc_mark_hook_fn gc_mark_hook)
11745 {
11746 bfd_boolean ret;
11747 asection *group_sec, *eh_frame;
11748
11749 sec->gc_mark = 1;
11750
11751 /* Mark all the sections in the group. */
11752 group_sec = elf_section_data (sec)->next_in_group;
11753 if (group_sec && !group_sec->gc_mark)
11754 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
11755 return FALSE;
11756
11757 /* Look through the section relocs. */
11758 ret = TRUE;
11759 eh_frame = elf_eh_frame_section (sec->owner);
11760 if ((sec->flags & SEC_RELOC) != 0
11761 && sec->reloc_count > 0
11762 && sec != eh_frame)
11763 {
11764 struct elf_reloc_cookie cookie;
11765
11766 if (!init_reloc_cookie_for_section (&cookie, info, sec))
11767 ret = FALSE;
11768 else
11769 {
11770 for (; cookie.rel < cookie.relend; cookie.rel++)
11771 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
11772 {
11773 ret = FALSE;
11774 break;
11775 }
11776 fini_reloc_cookie_for_section (&cookie, sec);
11777 }
11778 }
11779
11780 if (ret && eh_frame && elf_fde_list (sec))
11781 {
11782 struct elf_reloc_cookie cookie;
11783
11784 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
11785 ret = FALSE;
11786 else
11787 {
11788 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
11789 gc_mark_hook, &cookie))
11790 ret = FALSE;
11791 fini_reloc_cookie_for_section (&cookie, eh_frame);
11792 }
11793 }
11794
11795 return ret;
11796 }
11797
11798 /* Keep debug and special sections. */
11799
11800 bfd_boolean
11801 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
11802 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
11803 {
11804 bfd *ibfd;
11805
11806 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11807 {
11808 asection *isec;
11809 bfd_boolean some_kept;
11810
11811 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
11812 continue;
11813
11814 /* Ensure all linker created sections are kept, and see whether
11815 any other section is already marked. */
11816 some_kept = FALSE;
11817 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
11818 {
11819 if ((isec->flags & SEC_LINKER_CREATED) != 0)
11820 isec->gc_mark = 1;
11821 else if (isec->gc_mark)
11822 some_kept = TRUE;
11823 }
11824
11825 /* If no section in this file will be kept, then we can
11826 toss out debug sections. */
11827 if (!some_kept)
11828 continue;
11829
11830 /* Keep debug and special sections like .comment when they are
11831 not part of a group, or when we have single-member groups. */
11832 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
11833 if ((elf_next_in_group (isec) == NULL
11834 || elf_next_in_group (isec) == isec)
11835 && ((isec->flags & SEC_DEBUGGING) != 0
11836 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0))
11837 isec->gc_mark = 1;
11838 }
11839 return TRUE;
11840 }
11841
11842 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
11843
11844 struct elf_gc_sweep_symbol_info
11845 {
11846 struct bfd_link_info *info;
11847 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
11848 bfd_boolean);
11849 };
11850
11851 static bfd_boolean
11852 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
11853 {
11854 if (!h->mark
11855 && (((h->root.type == bfd_link_hash_defined
11856 || h->root.type == bfd_link_hash_defweak)
11857 && !(h->def_regular
11858 && h->root.u.def.section->gc_mark))
11859 || h->root.type == bfd_link_hash_undefined
11860 || h->root.type == bfd_link_hash_undefweak))
11861 {
11862 struct elf_gc_sweep_symbol_info *inf;
11863
11864 inf = (struct elf_gc_sweep_symbol_info *) data;
11865 (*inf->hide_symbol) (inf->info, h, TRUE);
11866 h->def_regular = 0;
11867 h->ref_regular = 0;
11868 h->ref_regular_nonweak = 0;
11869 }
11870
11871 return TRUE;
11872 }
11873
11874 /* The sweep phase of garbage collection. Remove all garbage sections. */
11875
11876 typedef bfd_boolean (*gc_sweep_hook_fn)
11877 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
11878
11879 static bfd_boolean
11880 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
11881 {
11882 bfd *sub;
11883 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11884 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
11885 unsigned long section_sym_count;
11886 struct elf_gc_sweep_symbol_info sweep_info;
11887
11888 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11889 {
11890 asection *o;
11891
11892 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
11893 continue;
11894
11895 for (o = sub->sections; o != NULL; o = o->next)
11896 {
11897 /* When any section in a section group is kept, we keep all
11898 sections in the section group. If the first member of
11899 the section group is excluded, we will also exclude the
11900 group section. */
11901 if (o->flags & SEC_GROUP)
11902 {
11903 asection *first = elf_next_in_group (o);
11904 o->gc_mark = first->gc_mark;
11905 }
11906
11907 if (o->gc_mark)
11908 continue;
11909
11910 /* Skip sweeping sections already excluded. */
11911 if (o->flags & SEC_EXCLUDE)
11912 continue;
11913
11914 /* Since this is early in the link process, it is simple
11915 to remove a section from the output. */
11916 o->flags |= SEC_EXCLUDE;
11917
11918 if (info->print_gc_sections && o->size != 0)
11919 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
11920
11921 /* But we also have to update some of the relocation
11922 info we collected before. */
11923 if (gc_sweep_hook
11924 && (o->flags & SEC_RELOC) != 0
11925 && o->reloc_count > 0
11926 && !bfd_is_abs_section (o->output_section))
11927 {
11928 Elf_Internal_Rela *internal_relocs;
11929 bfd_boolean r;
11930
11931 internal_relocs
11932 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
11933 info->keep_memory);
11934 if (internal_relocs == NULL)
11935 return FALSE;
11936
11937 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
11938
11939 if (elf_section_data (o)->relocs != internal_relocs)
11940 free (internal_relocs);
11941
11942 if (!r)
11943 return FALSE;
11944 }
11945 }
11946 }
11947
11948 /* Remove the symbols that were in the swept sections from the dynamic
11949 symbol table. GCFIXME: Anyone know how to get them out of the
11950 static symbol table as well? */
11951 sweep_info.info = info;
11952 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
11953 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
11954 &sweep_info);
11955
11956 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
11957 return TRUE;
11958 }
11959
11960 /* Propagate collected vtable information. This is called through
11961 elf_link_hash_traverse. */
11962
11963 static bfd_boolean
11964 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
11965 {
11966 /* Those that are not vtables. */
11967 if (h->vtable == NULL || h->vtable->parent == NULL)
11968 return TRUE;
11969
11970 /* Those vtables that do not have parents, we cannot merge. */
11971 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
11972 return TRUE;
11973
11974 /* If we've already been done, exit. */
11975 if (h->vtable->used && h->vtable->used[-1])
11976 return TRUE;
11977
11978 /* Make sure the parent's table is up to date. */
11979 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
11980
11981 if (h->vtable->used == NULL)
11982 {
11983 /* None of this table's entries were referenced. Re-use the
11984 parent's table. */
11985 h->vtable->used = h->vtable->parent->vtable->used;
11986 h->vtable->size = h->vtable->parent->vtable->size;
11987 }
11988 else
11989 {
11990 size_t n;
11991 bfd_boolean *cu, *pu;
11992
11993 /* Or the parent's entries into ours. */
11994 cu = h->vtable->used;
11995 cu[-1] = TRUE;
11996 pu = h->vtable->parent->vtable->used;
11997 if (pu != NULL)
11998 {
11999 const struct elf_backend_data *bed;
12000 unsigned int log_file_align;
12001
12002 bed = get_elf_backend_data (h->root.u.def.section->owner);
12003 log_file_align = bed->s->log_file_align;
12004 n = h->vtable->parent->vtable->size >> log_file_align;
12005 while (n--)
12006 {
12007 if (*pu)
12008 *cu = TRUE;
12009 pu++;
12010 cu++;
12011 }
12012 }
12013 }
12014
12015 return TRUE;
12016 }
12017
12018 static bfd_boolean
12019 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
12020 {
12021 asection *sec;
12022 bfd_vma hstart, hend;
12023 Elf_Internal_Rela *relstart, *relend, *rel;
12024 const struct elf_backend_data *bed;
12025 unsigned int log_file_align;
12026
12027 /* Take care of both those symbols that do not describe vtables as
12028 well as those that are not loaded. */
12029 if (h->vtable == NULL || h->vtable->parent == NULL)
12030 return TRUE;
12031
12032 BFD_ASSERT (h->root.type == bfd_link_hash_defined
12033 || h->root.type == bfd_link_hash_defweak);
12034
12035 sec = h->root.u.def.section;
12036 hstart = h->root.u.def.value;
12037 hend = hstart + h->size;
12038
12039 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
12040 if (!relstart)
12041 return *(bfd_boolean *) okp = FALSE;
12042 bed = get_elf_backend_data (sec->owner);
12043 log_file_align = bed->s->log_file_align;
12044
12045 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
12046
12047 for (rel = relstart; rel < relend; ++rel)
12048 if (rel->r_offset >= hstart && rel->r_offset < hend)
12049 {
12050 /* If the entry is in use, do nothing. */
12051 if (h->vtable->used
12052 && (rel->r_offset - hstart) < h->vtable->size)
12053 {
12054 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
12055 if (h->vtable->used[entry])
12056 continue;
12057 }
12058 /* Otherwise, kill it. */
12059 rel->r_offset = rel->r_info = rel->r_addend = 0;
12060 }
12061
12062 return TRUE;
12063 }
12064
12065 /* Mark sections containing dynamically referenced symbols. When
12066 building shared libraries, we must assume that any visible symbol is
12067 referenced. */
12068
12069 bfd_boolean
12070 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
12071 {
12072 struct bfd_link_info *info = (struct bfd_link_info *) inf;
12073
12074 if ((h->root.type == bfd_link_hash_defined
12075 || h->root.type == bfd_link_hash_defweak)
12076 && (h->ref_dynamic
12077 || ((!info->executable || info->export_dynamic)
12078 && h->def_regular
12079 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
12080 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
12081 && (strchr (h->root.root.string, ELF_VER_CHR) != NULL
12082 || !bfd_hide_sym_by_version (info->version_info,
12083 h->root.root.string)))))
12084 h->root.u.def.section->flags |= SEC_KEEP;
12085
12086 return TRUE;
12087 }
12088
12089 /* Keep all sections containing symbols undefined on the command-line,
12090 and the section containing the entry symbol. */
12091
12092 void
12093 _bfd_elf_gc_keep (struct bfd_link_info *info)
12094 {
12095 struct bfd_sym_chain *sym;
12096
12097 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
12098 {
12099 struct elf_link_hash_entry *h;
12100
12101 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
12102 FALSE, FALSE, FALSE);
12103
12104 if (h != NULL
12105 && (h->root.type == bfd_link_hash_defined
12106 || h->root.type == bfd_link_hash_defweak)
12107 && !bfd_is_abs_section (h->root.u.def.section))
12108 h->root.u.def.section->flags |= SEC_KEEP;
12109 }
12110 }
12111
12112 /* Do mark and sweep of unused sections. */
12113
12114 bfd_boolean
12115 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
12116 {
12117 bfd_boolean ok = TRUE;
12118 bfd *sub;
12119 elf_gc_mark_hook_fn gc_mark_hook;
12120 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12121
12122 if (!bed->can_gc_sections
12123 || !is_elf_hash_table (info->hash))
12124 {
12125 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
12126 return TRUE;
12127 }
12128
12129 bed->gc_keep (info);
12130
12131 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
12132 at the .eh_frame section if we can mark the FDEs individually. */
12133 _bfd_elf_begin_eh_frame_parsing (info);
12134 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
12135 {
12136 asection *sec;
12137 struct elf_reloc_cookie cookie;
12138
12139 sec = bfd_get_section_by_name (sub, ".eh_frame");
12140 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
12141 {
12142 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
12143 if (elf_section_data (sec)->sec_info
12144 && (sec->flags & SEC_LINKER_CREATED) == 0)
12145 elf_eh_frame_section (sub) = sec;
12146 fini_reloc_cookie_for_section (&cookie, sec);
12147 sec = bfd_get_next_section_by_name (sec);
12148 }
12149 }
12150 _bfd_elf_end_eh_frame_parsing (info);
12151
12152 /* Apply transitive closure to the vtable entry usage info. */
12153 elf_link_hash_traverse (elf_hash_table (info),
12154 elf_gc_propagate_vtable_entries_used,
12155 &ok);
12156 if (!ok)
12157 return FALSE;
12158
12159 /* Kill the vtable relocations that were not used. */
12160 elf_link_hash_traverse (elf_hash_table (info),
12161 elf_gc_smash_unused_vtentry_relocs,
12162 &ok);
12163 if (!ok)
12164 return FALSE;
12165
12166 /* Mark dynamically referenced symbols. */
12167 if (elf_hash_table (info)->dynamic_sections_created)
12168 elf_link_hash_traverse (elf_hash_table (info),
12169 bed->gc_mark_dynamic_ref,
12170 info);
12171
12172 /* Grovel through relocs to find out who stays ... */
12173 gc_mark_hook = bed->gc_mark_hook;
12174 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
12175 {
12176 asection *o;
12177
12178 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
12179 continue;
12180
12181 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
12182 Also treat note sections as a root, if the section is not part
12183 of a group. */
12184 for (o = sub->sections; o != NULL; o = o->next)
12185 if (!o->gc_mark
12186 && (o->flags & SEC_EXCLUDE) == 0
12187 && ((o->flags & SEC_KEEP) != 0
12188 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
12189 && elf_next_in_group (o) == NULL )))
12190 {
12191 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12192 return FALSE;
12193 }
12194 }
12195
12196 /* Allow the backend to mark additional target specific sections. */
12197 bed->gc_mark_extra_sections (info, gc_mark_hook);
12198
12199 /* ... and mark SEC_EXCLUDE for those that go. */
12200 return elf_gc_sweep (abfd, info);
12201 }
12202 \f
12203 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
12204
12205 bfd_boolean
12206 bfd_elf_gc_record_vtinherit (bfd *abfd,
12207 asection *sec,
12208 struct elf_link_hash_entry *h,
12209 bfd_vma offset)
12210 {
12211 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
12212 struct elf_link_hash_entry **search, *child;
12213 bfd_size_type extsymcount;
12214 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12215
12216 /* The sh_info field of the symtab header tells us where the
12217 external symbols start. We don't care about the local symbols at
12218 this point. */
12219 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
12220 if (!elf_bad_symtab (abfd))
12221 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
12222
12223 sym_hashes = elf_sym_hashes (abfd);
12224 sym_hashes_end = sym_hashes + extsymcount;
12225
12226 /* Hunt down the child symbol, which is in this section at the same
12227 offset as the relocation. */
12228 for (search = sym_hashes; search != sym_hashes_end; ++search)
12229 {
12230 if ((child = *search) != NULL
12231 && (child->root.type == bfd_link_hash_defined
12232 || child->root.type == bfd_link_hash_defweak)
12233 && child->root.u.def.section == sec
12234 && child->root.u.def.value == offset)
12235 goto win;
12236 }
12237
12238 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
12239 abfd, sec, (unsigned long) offset);
12240 bfd_set_error (bfd_error_invalid_operation);
12241 return FALSE;
12242
12243 win:
12244 if (!child->vtable)
12245 {
12246 child->vtable = (struct elf_link_virtual_table_entry *)
12247 bfd_zalloc (abfd, sizeof (*child->vtable));
12248 if (!child->vtable)
12249 return FALSE;
12250 }
12251 if (!h)
12252 {
12253 /* This *should* only be the absolute section. It could potentially
12254 be that someone has defined a non-global vtable though, which
12255 would be bad. It isn't worth paging in the local symbols to be
12256 sure though; that case should simply be handled by the assembler. */
12257
12258 child->vtable->parent = (struct elf_link_hash_entry *) -1;
12259 }
12260 else
12261 child->vtable->parent = h;
12262
12263 return TRUE;
12264 }
12265
12266 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
12267
12268 bfd_boolean
12269 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
12270 asection *sec ATTRIBUTE_UNUSED,
12271 struct elf_link_hash_entry *h,
12272 bfd_vma addend)
12273 {
12274 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12275 unsigned int log_file_align = bed->s->log_file_align;
12276
12277 if (!h->vtable)
12278 {
12279 h->vtable = (struct elf_link_virtual_table_entry *)
12280 bfd_zalloc (abfd, sizeof (*h->vtable));
12281 if (!h->vtable)
12282 return FALSE;
12283 }
12284
12285 if (addend >= h->vtable->size)
12286 {
12287 size_t size, bytes, file_align;
12288 bfd_boolean *ptr = h->vtable->used;
12289
12290 /* While the symbol is undefined, we have to be prepared to handle
12291 a zero size. */
12292 file_align = 1 << log_file_align;
12293 if (h->root.type == bfd_link_hash_undefined)
12294 size = addend + file_align;
12295 else
12296 {
12297 size = h->size;
12298 if (addend >= size)
12299 {
12300 /* Oops! We've got a reference past the defined end of
12301 the table. This is probably a bug -- shall we warn? */
12302 size = addend + file_align;
12303 }
12304 }
12305 size = (size + file_align - 1) & -file_align;
12306
12307 /* Allocate one extra entry for use as a "done" flag for the
12308 consolidation pass. */
12309 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
12310
12311 if (ptr)
12312 {
12313 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
12314
12315 if (ptr != NULL)
12316 {
12317 size_t oldbytes;
12318
12319 oldbytes = (((h->vtable->size >> log_file_align) + 1)
12320 * sizeof (bfd_boolean));
12321 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
12322 }
12323 }
12324 else
12325 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
12326
12327 if (ptr == NULL)
12328 return FALSE;
12329
12330 /* And arrange for that done flag to be at index -1. */
12331 h->vtable->used = ptr + 1;
12332 h->vtable->size = size;
12333 }
12334
12335 h->vtable->used[addend >> log_file_align] = TRUE;
12336
12337 return TRUE;
12338 }
12339
12340 /* Map an ELF section header flag to its corresponding string. */
12341 typedef struct
12342 {
12343 char *flag_name;
12344 flagword flag_value;
12345 } elf_flags_to_name_table;
12346
12347 static elf_flags_to_name_table elf_flags_to_names [] =
12348 {
12349 { "SHF_WRITE", SHF_WRITE },
12350 { "SHF_ALLOC", SHF_ALLOC },
12351 { "SHF_EXECINSTR", SHF_EXECINSTR },
12352 { "SHF_MERGE", SHF_MERGE },
12353 { "SHF_STRINGS", SHF_STRINGS },
12354 { "SHF_INFO_LINK", SHF_INFO_LINK},
12355 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
12356 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
12357 { "SHF_GROUP", SHF_GROUP },
12358 { "SHF_TLS", SHF_TLS },
12359 { "SHF_MASKOS", SHF_MASKOS },
12360 { "SHF_EXCLUDE", SHF_EXCLUDE },
12361 };
12362
12363 /* Returns TRUE if the section is to be included, otherwise FALSE. */
12364 bfd_boolean
12365 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
12366 struct flag_info *flaginfo,
12367 asection *section)
12368 {
12369 const bfd_vma sh_flags = elf_section_flags (section);
12370
12371 if (!flaginfo->flags_initialized)
12372 {
12373 bfd *obfd = info->output_bfd;
12374 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
12375 struct flag_info_list *tf = flaginfo->flag_list;
12376 int with_hex = 0;
12377 int without_hex = 0;
12378
12379 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
12380 {
12381 unsigned i;
12382 flagword (*lookup) (char *);
12383
12384 lookup = bed->elf_backend_lookup_section_flags_hook;
12385 if (lookup != NULL)
12386 {
12387 flagword hexval = (*lookup) ((char *) tf->name);
12388
12389 if (hexval != 0)
12390 {
12391 if (tf->with == with_flags)
12392 with_hex |= hexval;
12393 else if (tf->with == without_flags)
12394 without_hex |= hexval;
12395 tf->valid = TRUE;
12396 continue;
12397 }
12398 }
12399 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
12400 {
12401 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
12402 {
12403 if (tf->with == with_flags)
12404 with_hex |= elf_flags_to_names[i].flag_value;
12405 else if (tf->with == without_flags)
12406 without_hex |= elf_flags_to_names[i].flag_value;
12407 tf->valid = TRUE;
12408 break;
12409 }
12410 }
12411 if (!tf->valid)
12412 {
12413 info->callbacks->einfo
12414 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
12415 return FALSE;
12416 }
12417 }
12418 flaginfo->flags_initialized = TRUE;
12419 flaginfo->only_with_flags |= with_hex;
12420 flaginfo->not_with_flags |= without_hex;
12421 }
12422
12423 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
12424 return FALSE;
12425
12426 if ((flaginfo->not_with_flags & sh_flags) != 0)
12427 return FALSE;
12428
12429 return TRUE;
12430 }
12431
12432 struct alloc_got_off_arg {
12433 bfd_vma gotoff;
12434 struct bfd_link_info *info;
12435 };
12436
12437 /* We need a special top-level link routine to convert got reference counts
12438 to real got offsets. */
12439
12440 static bfd_boolean
12441 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
12442 {
12443 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
12444 bfd *obfd = gofarg->info->output_bfd;
12445 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
12446
12447 if (h->got.refcount > 0)
12448 {
12449 h->got.offset = gofarg->gotoff;
12450 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
12451 }
12452 else
12453 h->got.offset = (bfd_vma) -1;
12454
12455 return TRUE;
12456 }
12457
12458 /* And an accompanying bit to work out final got entry offsets once
12459 we're done. Should be called from final_link. */
12460
12461 bfd_boolean
12462 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
12463 struct bfd_link_info *info)
12464 {
12465 bfd *i;
12466 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12467 bfd_vma gotoff;
12468 struct alloc_got_off_arg gofarg;
12469
12470 BFD_ASSERT (abfd == info->output_bfd);
12471
12472 if (! is_elf_hash_table (info->hash))
12473 return FALSE;
12474
12475 /* The GOT offset is relative to the .got section, but the GOT header is
12476 put into the .got.plt section, if the backend uses it. */
12477 if (bed->want_got_plt)
12478 gotoff = 0;
12479 else
12480 gotoff = bed->got_header_size;
12481
12482 /* Do the local .got entries first. */
12483 for (i = info->input_bfds; i; i = i->link_next)
12484 {
12485 bfd_signed_vma *local_got;
12486 bfd_size_type j, locsymcount;
12487 Elf_Internal_Shdr *symtab_hdr;
12488
12489 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
12490 continue;
12491
12492 local_got = elf_local_got_refcounts (i);
12493 if (!local_got)
12494 continue;
12495
12496 symtab_hdr = &elf_tdata (i)->symtab_hdr;
12497 if (elf_bad_symtab (i))
12498 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12499 else
12500 locsymcount = symtab_hdr->sh_info;
12501
12502 for (j = 0; j < locsymcount; ++j)
12503 {
12504 if (local_got[j] > 0)
12505 {
12506 local_got[j] = gotoff;
12507 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
12508 }
12509 else
12510 local_got[j] = (bfd_vma) -1;
12511 }
12512 }
12513
12514 /* Then the global .got entries. .plt refcounts are handled by
12515 adjust_dynamic_symbol */
12516 gofarg.gotoff = gotoff;
12517 gofarg.info = info;
12518 elf_link_hash_traverse (elf_hash_table (info),
12519 elf_gc_allocate_got_offsets,
12520 &gofarg);
12521 return TRUE;
12522 }
12523
12524 /* Many folk need no more in the way of final link than this, once
12525 got entry reference counting is enabled. */
12526
12527 bfd_boolean
12528 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
12529 {
12530 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
12531 return FALSE;
12532
12533 /* Invoke the regular ELF backend linker to do all the work. */
12534 return bfd_elf_final_link (abfd, info);
12535 }
12536
12537 bfd_boolean
12538 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
12539 {
12540 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
12541
12542 if (rcookie->bad_symtab)
12543 rcookie->rel = rcookie->rels;
12544
12545 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
12546 {
12547 unsigned long r_symndx;
12548
12549 if (! rcookie->bad_symtab)
12550 if (rcookie->rel->r_offset > offset)
12551 return FALSE;
12552 if (rcookie->rel->r_offset != offset)
12553 continue;
12554
12555 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
12556 if (r_symndx == STN_UNDEF)
12557 return TRUE;
12558
12559 if (r_symndx >= rcookie->locsymcount
12560 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12561 {
12562 struct elf_link_hash_entry *h;
12563
12564 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
12565
12566 while (h->root.type == bfd_link_hash_indirect
12567 || h->root.type == bfd_link_hash_warning)
12568 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12569
12570 if ((h->root.type == bfd_link_hash_defined
12571 || h->root.type == bfd_link_hash_defweak)
12572 && discarded_section (h->root.u.def.section))
12573 return TRUE;
12574 else
12575 return FALSE;
12576 }
12577 else
12578 {
12579 /* It's not a relocation against a global symbol,
12580 but it could be a relocation against a local
12581 symbol for a discarded section. */
12582 asection *isec;
12583 Elf_Internal_Sym *isym;
12584
12585 /* Need to: get the symbol; get the section. */
12586 isym = &rcookie->locsyms[r_symndx];
12587 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
12588 if (isec != NULL && discarded_section (isec))
12589 return TRUE;
12590 }
12591 return FALSE;
12592 }
12593 return FALSE;
12594 }
12595
12596 /* Discard unneeded references to discarded sections.
12597 Returns TRUE if any section's size was changed. */
12598 /* This function assumes that the relocations are in sorted order,
12599 which is true for all known assemblers. */
12600
12601 bfd_boolean
12602 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
12603 {
12604 struct elf_reloc_cookie cookie;
12605 asection *stab, *eh;
12606 const struct elf_backend_data *bed;
12607 bfd *abfd;
12608 bfd_boolean ret = FALSE;
12609
12610 if (info->traditional_format
12611 || !is_elf_hash_table (info->hash))
12612 return FALSE;
12613
12614 _bfd_elf_begin_eh_frame_parsing (info);
12615 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
12616 {
12617 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
12618 continue;
12619
12620 bed = get_elf_backend_data (abfd);
12621
12622 eh = NULL;
12623 if (!info->relocatable)
12624 {
12625 eh = bfd_get_section_by_name (abfd, ".eh_frame");
12626 while (eh != NULL
12627 && (eh->size == 0
12628 || bfd_is_abs_section (eh->output_section)))
12629 eh = bfd_get_next_section_by_name (eh);
12630 }
12631
12632 stab = bfd_get_section_by_name (abfd, ".stab");
12633 if (stab != NULL
12634 && (stab->size == 0
12635 || bfd_is_abs_section (stab->output_section)
12636 || stab->sec_info_type != SEC_INFO_TYPE_STABS))
12637 stab = NULL;
12638
12639 if (stab == NULL
12640 && eh == NULL
12641 && bed->elf_backend_discard_info == NULL)
12642 continue;
12643
12644 if (!init_reloc_cookie (&cookie, info, abfd))
12645 return FALSE;
12646
12647 if (stab != NULL
12648 && stab->reloc_count > 0
12649 && init_reloc_cookie_rels (&cookie, info, abfd, stab))
12650 {
12651 if (_bfd_discard_section_stabs (abfd, stab,
12652 elf_section_data (stab)->sec_info,
12653 bfd_elf_reloc_symbol_deleted_p,
12654 &cookie))
12655 ret = TRUE;
12656 fini_reloc_cookie_rels (&cookie, stab);
12657 }
12658
12659 while (eh != NULL
12660 && init_reloc_cookie_rels (&cookie, info, abfd, eh))
12661 {
12662 _bfd_elf_parse_eh_frame (abfd, info, eh, &cookie);
12663 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
12664 bfd_elf_reloc_symbol_deleted_p,
12665 &cookie))
12666 ret = TRUE;
12667 fini_reloc_cookie_rels (&cookie, eh);
12668 eh = bfd_get_next_section_by_name (eh);
12669 }
12670
12671 if (bed->elf_backend_discard_info != NULL
12672 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
12673 ret = TRUE;
12674
12675 fini_reloc_cookie (&cookie, abfd);
12676 }
12677 _bfd_elf_end_eh_frame_parsing (info);
12678
12679 if (info->eh_frame_hdr
12680 && !info->relocatable
12681 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
12682 ret = TRUE;
12683
12684 return ret;
12685 }
12686
12687 bfd_boolean
12688 _bfd_elf_section_already_linked (bfd *abfd,
12689 asection *sec,
12690 struct bfd_link_info *info)
12691 {
12692 flagword flags;
12693 const char *name, *key;
12694 struct bfd_section_already_linked *l;
12695 struct bfd_section_already_linked_hash_entry *already_linked_list;
12696
12697 if (sec->output_section == bfd_abs_section_ptr)
12698 return FALSE;
12699
12700 flags = sec->flags;
12701
12702 /* Return if it isn't a linkonce section. A comdat group section
12703 also has SEC_LINK_ONCE set. */
12704 if ((flags & SEC_LINK_ONCE) == 0)
12705 return FALSE;
12706
12707 /* Don't put group member sections on our list of already linked
12708 sections. They are handled as a group via their group section. */
12709 if (elf_sec_group (sec) != NULL)
12710 return FALSE;
12711
12712 /* For a SHT_GROUP section, use the group signature as the key. */
12713 name = sec->name;
12714 if ((flags & SEC_GROUP) != 0
12715 && elf_next_in_group (sec) != NULL
12716 && elf_group_name (elf_next_in_group (sec)) != NULL)
12717 key = elf_group_name (elf_next_in_group (sec));
12718 else
12719 {
12720 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
12721 if (CONST_STRNEQ (name, ".gnu.linkonce.")
12722 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
12723 key++;
12724 else
12725 /* Must be a user linkonce section that doesn't follow gcc's
12726 naming convention. In this case we won't be matching
12727 single member groups. */
12728 key = name;
12729 }
12730
12731 already_linked_list = bfd_section_already_linked_table_lookup (key);
12732
12733 for (l = already_linked_list->entry; l != NULL; l = l->next)
12734 {
12735 /* We may have 2 different types of sections on the list: group
12736 sections with a signature of <key> (<key> is some string),
12737 and linkonce sections named .gnu.linkonce.<type>.<key>.
12738 Match like sections. LTO plugin sections are an exception.
12739 They are always named .gnu.linkonce.t.<key> and match either
12740 type of section. */
12741 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
12742 && ((flags & SEC_GROUP) != 0
12743 || strcmp (name, l->sec->name) == 0))
12744 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
12745 {
12746 /* The section has already been linked. See if we should
12747 issue a warning. */
12748 if (!_bfd_handle_already_linked (sec, l, info))
12749 return FALSE;
12750
12751 if (flags & SEC_GROUP)
12752 {
12753 asection *first = elf_next_in_group (sec);
12754 asection *s = first;
12755
12756 while (s != NULL)
12757 {
12758 s->output_section = bfd_abs_section_ptr;
12759 /* Record which group discards it. */
12760 s->kept_section = l->sec;
12761 s = elf_next_in_group (s);
12762 /* These lists are circular. */
12763 if (s == first)
12764 break;
12765 }
12766 }
12767
12768 return TRUE;
12769 }
12770 }
12771
12772 /* A single member comdat group section may be discarded by a
12773 linkonce section and vice versa. */
12774 if ((flags & SEC_GROUP) != 0)
12775 {
12776 asection *first = elf_next_in_group (sec);
12777
12778 if (first != NULL && elf_next_in_group (first) == first)
12779 /* Check this single member group against linkonce sections. */
12780 for (l = already_linked_list->entry; l != NULL; l = l->next)
12781 if ((l->sec->flags & SEC_GROUP) == 0
12782 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
12783 {
12784 first->output_section = bfd_abs_section_ptr;
12785 first->kept_section = l->sec;
12786 sec->output_section = bfd_abs_section_ptr;
12787 break;
12788 }
12789 }
12790 else
12791 /* Check this linkonce section against single member groups. */
12792 for (l = already_linked_list->entry; l != NULL; l = l->next)
12793 if (l->sec->flags & SEC_GROUP)
12794 {
12795 asection *first = elf_next_in_group (l->sec);
12796
12797 if (first != NULL
12798 && elf_next_in_group (first) == first
12799 && bfd_elf_match_symbols_in_sections (first, sec, info))
12800 {
12801 sec->output_section = bfd_abs_section_ptr;
12802 sec->kept_section = first;
12803 break;
12804 }
12805 }
12806
12807 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
12808 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
12809 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
12810 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
12811 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
12812 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
12813 `.gnu.linkonce.t.F' section from a different bfd not requiring any
12814 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
12815 The reverse order cannot happen as there is never a bfd with only the
12816 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
12817 matter as here were are looking only for cross-bfd sections. */
12818
12819 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
12820 for (l = already_linked_list->entry; l != NULL; l = l->next)
12821 if ((l->sec->flags & SEC_GROUP) == 0
12822 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
12823 {
12824 if (abfd != l->sec->owner)
12825 sec->output_section = bfd_abs_section_ptr;
12826 break;
12827 }
12828
12829 /* This is the first section with this name. Record it. */
12830 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
12831 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
12832 return sec->output_section == bfd_abs_section_ptr;
12833 }
12834
12835 bfd_boolean
12836 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
12837 {
12838 return sym->st_shndx == SHN_COMMON;
12839 }
12840
12841 unsigned int
12842 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
12843 {
12844 return SHN_COMMON;
12845 }
12846
12847 asection *
12848 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
12849 {
12850 return bfd_com_section_ptr;
12851 }
12852
12853 bfd_vma
12854 _bfd_elf_default_got_elt_size (bfd *abfd,
12855 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12856 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
12857 bfd *ibfd ATTRIBUTE_UNUSED,
12858 unsigned long symndx ATTRIBUTE_UNUSED)
12859 {
12860 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12861 return bed->s->arch_size / 8;
12862 }
12863
12864 /* Routines to support the creation of dynamic relocs. */
12865
12866 /* Returns the name of the dynamic reloc section associated with SEC. */
12867
12868 static const char *
12869 get_dynamic_reloc_section_name (bfd * abfd,
12870 asection * sec,
12871 bfd_boolean is_rela)
12872 {
12873 char *name;
12874 const char *old_name = bfd_get_section_name (NULL, sec);
12875 const char *prefix = is_rela ? ".rela" : ".rel";
12876
12877 if (old_name == NULL)
12878 return NULL;
12879
12880 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
12881 sprintf (name, "%s%s", prefix, old_name);
12882
12883 return name;
12884 }
12885
12886 /* Returns the dynamic reloc section associated with SEC.
12887 If necessary compute the name of the dynamic reloc section based
12888 on SEC's name (looked up in ABFD's string table) and the setting
12889 of IS_RELA. */
12890
12891 asection *
12892 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
12893 asection * sec,
12894 bfd_boolean is_rela)
12895 {
12896 asection * reloc_sec = elf_section_data (sec)->sreloc;
12897
12898 if (reloc_sec == NULL)
12899 {
12900 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
12901
12902 if (name != NULL)
12903 {
12904 reloc_sec = bfd_get_linker_section (abfd, name);
12905
12906 if (reloc_sec != NULL)
12907 elf_section_data (sec)->sreloc = reloc_sec;
12908 }
12909 }
12910
12911 return reloc_sec;
12912 }
12913
12914 /* Returns the dynamic reloc section associated with SEC. If the
12915 section does not exist it is created and attached to the DYNOBJ
12916 bfd and stored in the SRELOC field of SEC's elf_section_data
12917 structure.
12918
12919 ALIGNMENT is the alignment for the newly created section and
12920 IS_RELA defines whether the name should be .rela.<SEC's name>
12921 or .rel.<SEC's name>. The section name is looked up in the
12922 string table associated with ABFD. */
12923
12924 asection *
12925 _bfd_elf_make_dynamic_reloc_section (asection * sec,
12926 bfd * dynobj,
12927 unsigned int alignment,
12928 bfd * abfd,
12929 bfd_boolean is_rela)
12930 {
12931 asection * reloc_sec = elf_section_data (sec)->sreloc;
12932
12933 if (reloc_sec == NULL)
12934 {
12935 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
12936
12937 if (name == NULL)
12938 return NULL;
12939
12940 reloc_sec = bfd_get_linker_section (dynobj, name);
12941
12942 if (reloc_sec == NULL)
12943 {
12944 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
12945 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
12946 if ((sec->flags & SEC_ALLOC) != 0)
12947 flags |= SEC_ALLOC | SEC_LOAD;
12948
12949 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
12950 if (reloc_sec != NULL)
12951 {
12952 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
12953 reloc_sec = NULL;
12954 }
12955 }
12956
12957 elf_section_data (sec)->sreloc = reloc_sec;
12958 }
12959
12960 return reloc_sec;
12961 }
12962
12963 /* Copy the ELF symbol type associated with a linker hash entry. */
12964 void
12965 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd ATTRIBUTE_UNUSED,
12966 struct bfd_link_hash_entry * hdest,
12967 struct bfd_link_hash_entry * hsrc)
12968 {
12969 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *)hdest;
12970 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *)hsrc;
12971
12972 ehdest->type = ehsrc->type;
12973 ehdest->target_internal = ehsrc->target_internal;
12974 }
12975
12976 /* Append a RELA relocation REL to section S in BFD. */
12977
12978 void
12979 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
12980 {
12981 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12982 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
12983 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
12984 bed->s->swap_reloca_out (abfd, rel, loc);
12985 }
12986
12987 /* Append a REL relocation REL to section S in BFD. */
12988
12989 void
12990 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
12991 {
12992 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12993 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
12994 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
12995 bed->s->swap_reloca_out (abfd, rel, loc);
12996 }
This page took 0.306061 seconds and 4 git commands to generate.