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