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