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