2004-12-01 Paul Brook <paul@codesourcery.com>
[deliverable/binutils-gdb.git] / bfd / elflink.c
1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #define ARCH_SIZE 0
26 #include "elf-bfd.h"
27 #include "safe-ctype.h"
28 #include "libiberty.h"
29
30 bfd_boolean
31 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
32 {
33 flagword flags;
34 asection *s;
35 struct elf_link_hash_entry *h;
36 struct bfd_link_hash_entry *bh;
37 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
38 int ptralign;
39
40 /* This function may be called more than once. */
41 s = bfd_get_section_by_name (abfd, ".got");
42 if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
43 return TRUE;
44
45 switch (bed->s->arch_size)
46 {
47 case 32:
48 ptralign = 2;
49 break;
50
51 case 64:
52 ptralign = 3;
53 break;
54
55 default:
56 bfd_set_error (bfd_error_bad_value);
57 return FALSE;
58 }
59
60 flags = bed->dynamic_sec_flags;
61
62 s = bfd_make_section (abfd, ".got");
63 if (s == NULL
64 || !bfd_set_section_flags (abfd, s, flags)
65 || !bfd_set_section_alignment (abfd, s, ptralign))
66 return FALSE;
67
68 if (bed->want_got_plt)
69 {
70 s = bfd_make_section (abfd, ".got.plt");
71 if (s == NULL
72 || !bfd_set_section_flags (abfd, s, flags)
73 || !bfd_set_section_alignment (abfd, s, ptralign))
74 return FALSE;
75 }
76
77 if (bed->want_got_sym)
78 {
79 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
80 (or .got.plt) section. We don't do this in the linker script
81 because we don't want to define the symbol if we are not creating
82 a global offset table. */
83 bh = NULL;
84 if (!(_bfd_generic_link_add_one_symbol
85 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
86 bed->got_symbol_offset, NULL, FALSE, bed->collect, &bh)))
87 return FALSE;
88 h = (struct elf_link_hash_entry *) bh;
89 h->def_regular = 1;
90 h->type = STT_OBJECT;
91 h->other = STV_HIDDEN;
92
93 if (! info->executable
94 && ! bfd_elf_link_record_dynamic_symbol (info, h))
95 return FALSE;
96
97 elf_hash_table (info)->hgot = h;
98 }
99
100 /* The first bit of the global offset table is the header. */
101 s->size += bed->got_header_size + bed->got_symbol_offset;
102
103 return TRUE;
104 }
105 \f
106 /* Create some sections which will be filled in with dynamic linking
107 information. ABFD is an input file which requires dynamic sections
108 to be created. The dynamic sections take up virtual memory space
109 when the final executable is run, so we need to create them before
110 addresses are assigned to the output sections. We work out the
111 actual contents and size of these sections later. */
112
113 bfd_boolean
114 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
115 {
116 flagword flags;
117 register asection *s;
118 struct elf_link_hash_entry *h;
119 struct bfd_link_hash_entry *bh;
120 const struct elf_backend_data *bed;
121
122 if (! is_elf_hash_table (info->hash))
123 return FALSE;
124
125 if (elf_hash_table (info)->dynamic_sections_created)
126 return TRUE;
127
128 /* Make sure that all dynamic sections use the same input BFD. */
129 if (elf_hash_table (info)->dynobj == NULL)
130 elf_hash_table (info)->dynobj = abfd;
131 else
132 abfd = elf_hash_table (info)->dynobj;
133
134 bed = get_elf_backend_data (abfd);
135
136 flags = bed->dynamic_sec_flags;
137
138 /* A dynamically linked executable has a .interp section, but a
139 shared library does not. */
140 if (info->executable)
141 {
142 s = bfd_make_section (abfd, ".interp");
143 if (s == NULL
144 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
145 return FALSE;
146 }
147
148 if (! info->traditional_format)
149 {
150 s = bfd_make_section (abfd, ".eh_frame_hdr");
151 if (s == NULL
152 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
153 || ! bfd_set_section_alignment (abfd, s, 2))
154 return FALSE;
155 elf_hash_table (info)->eh_info.hdr_sec = s;
156 }
157
158 /* Create sections to hold version informations. These are removed
159 if they are not needed. */
160 s = bfd_make_section (abfd, ".gnu.version_d");
161 if (s == NULL
162 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
163 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
164 return FALSE;
165
166 s = bfd_make_section (abfd, ".gnu.version");
167 if (s == NULL
168 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
169 || ! bfd_set_section_alignment (abfd, s, 1))
170 return FALSE;
171
172 s = bfd_make_section (abfd, ".gnu.version_r");
173 if (s == NULL
174 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
175 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
176 return FALSE;
177
178 s = bfd_make_section (abfd, ".dynsym");
179 if (s == NULL
180 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
181 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
182 return FALSE;
183
184 s = bfd_make_section (abfd, ".dynstr");
185 if (s == NULL
186 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
187 return FALSE;
188
189 /* Create a strtab to hold the dynamic symbol names. */
190 if (elf_hash_table (info)->dynstr == NULL)
191 {
192 elf_hash_table (info)->dynstr = _bfd_elf_strtab_init ();
193 if (elf_hash_table (info)->dynstr == NULL)
194 return FALSE;
195 }
196
197 s = bfd_make_section (abfd, ".dynamic");
198 if (s == NULL
199 || ! bfd_set_section_flags (abfd, s, flags)
200 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
201 return FALSE;
202
203 /* The special symbol _DYNAMIC is always set to the start of the
204 .dynamic section. This call occurs before we have processed the
205 symbols for any dynamic object, so we don't have to worry about
206 overriding a dynamic definition. We could set _DYNAMIC in a
207 linker script, but we only want to define it if we are, in fact,
208 creating a .dynamic section. We don't want to define it if there
209 is no .dynamic section, since on some ELF platforms the start up
210 code examines it to decide how to initialize the process. */
211 bh = NULL;
212 if (! (_bfd_generic_link_add_one_symbol
213 (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, 0, NULL, FALSE,
214 get_elf_backend_data (abfd)->collect, &bh)))
215 return FALSE;
216 h = (struct elf_link_hash_entry *) bh;
217 h->def_regular = 1;
218 h->type = STT_OBJECT;
219
220 if (! info->executable
221 && ! bfd_elf_link_record_dynamic_symbol (info, h))
222 return FALSE;
223
224 s = bfd_make_section (abfd, ".hash");
225 if (s == NULL
226 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
227 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
228 return FALSE;
229 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
230
231 /* Let the backend create the rest of the sections. This lets the
232 backend set the right flags. The backend will normally create
233 the .got and .plt sections. */
234 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
235 return FALSE;
236
237 elf_hash_table (info)->dynamic_sections_created = TRUE;
238
239 return TRUE;
240 }
241
242 /* Create dynamic sections when linking against a dynamic object. */
243
244 bfd_boolean
245 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
246 {
247 flagword flags, pltflags;
248 asection *s;
249 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
250
251 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
252 .rel[a].bss sections. */
253 flags = bed->dynamic_sec_flags;
254
255 pltflags = flags;
256 if (bed->plt_not_loaded)
257 /* We do not clear SEC_ALLOC here because we still want the OS to
258 allocate space for the section; it's just that there's nothing
259 to read in from the object file. */
260 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
261 else
262 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
263 if (bed->plt_readonly)
264 pltflags |= SEC_READONLY;
265
266 s = bfd_make_section (abfd, ".plt");
267 if (s == NULL
268 || ! bfd_set_section_flags (abfd, s, pltflags)
269 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
270 return FALSE;
271
272 if (bed->want_plt_sym)
273 {
274 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
275 .plt section. */
276 struct elf_link_hash_entry *h;
277 struct bfd_link_hash_entry *bh = NULL;
278
279 if (! (_bfd_generic_link_add_one_symbol
280 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s, 0, NULL,
281 FALSE, get_elf_backend_data (abfd)->collect, &bh)))
282 return FALSE;
283 h = (struct elf_link_hash_entry *) bh;
284 h->def_regular = 1;
285 h->type = STT_OBJECT;
286
287 if (! info->executable
288 && ! bfd_elf_link_record_dynamic_symbol (info, h))
289 return FALSE;
290 }
291
292 s = bfd_make_section (abfd,
293 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
294 if (s == NULL
295 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
296 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
297 return FALSE;
298
299 if (! _bfd_elf_create_got_section (abfd, info))
300 return FALSE;
301
302 if (bed->want_dynbss)
303 {
304 /* The .dynbss section is a place to put symbols which are defined
305 by dynamic objects, are referenced by regular objects, and are
306 not functions. We must allocate space for them in the process
307 image and use a R_*_COPY reloc to tell the dynamic linker to
308 initialize them at run time. The linker script puts the .dynbss
309 section into the .bss section of the final image. */
310 s = bfd_make_section (abfd, ".dynbss");
311 if (s == NULL
312 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC | SEC_LINKER_CREATED))
313 return FALSE;
314
315 /* The .rel[a].bss section holds copy relocs. This section is not
316 normally needed. We need to create it here, though, so that the
317 linker will map it to an output section. We can't just create it
318 only if we need it, because we will not know whether we need it
319 until we have seen all the input files, and the first time the
320 main linker code calls BFD after examining all the input files
321 (size_dynamic_sections) the input sections have already been
322 mapped to the output sections. If the section turns out not to
323 be needed, we can discard it later. We will never need this
324 section when generating a shared object, since they do not use
325 copy relocs. */
326 if (! info->shared)
327 {
328 s = bfd_make_section (abfd,
329 (bed->default_use_rela_p
330 ? ".rela.bss" : ".rel.bss"));
331 if (s == NULL
332 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
333 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
334 return FALSE;
335 }
336 }
337
338 return TRUE;
339 }
340 \f
341 /* Record a new dynamic symbol. We record the dynamic symbols as we
342 read the input files, since we need to have a list of all of them
343 before we can determine the final sizes of the output sections.
344 Note that we may actually call this function even though we are not
345 going to output any dynamic symbols; in some cases we know that a
346 symbol should be in the dynamic symbol table, but only if there is
347 one. */
348
349 bfd_boolean
350 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
351 struct elf_link_hash_entry *h)
352 {
353 if (h->dynindx == -1)
354 {
355 struct elf_strtab_hash *dynstr;
356 char *p;
357 const char *name;
358 bfd_size_type indx;
359
360 /* XXX: The ABI draft says the linker must turn hidden and
361 internal symbols into STB_LOCAL symbols when producing the
362 DSO. However, if ld.so honors st_other in the dynamic table,
363 this would not be necessary. */
364 switch (ELF_ST_VISIBILITY (h->other))
365 {
366 case STV_INTERNAL:
367 case STV_HIDDEN:
368 if (h->root.type != bfd_link_hash_undefined
369 && h->root.type != bfd_link_hash_undefweak)
370 {
371 h->forced_local = 1;
372 return TRUE;
373 }
374
375 default:
376 break;
377 }
378
379 h->dynindx = elf_hash_table (info)->dynsymcount;
380 ++elf_hash_table (info)->dynsymcount;
381
382 dynstr = elf_hash_table (info)->dynstr;
383 if (dynstr == NULL)
384 {
385 /* Create a strtab to hold the dynamic symbol names. */
386 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
387 if (dynstr == NULL)
388 return FALSE;
389 }
390
391 /* We don't put any version information in the dynamic string
392 table. */
393 name = h->root.root.string;
394 p = strchr (name, ELF_VER_CHR);
395 if (p != NULL)
396 /* We know that the p points into writable memory. In fact,
397 there are only a few symbols that have read-only names, being
398 those like _GLOBAL_OFFSET_TABLE_ that are created specially
399 by the backends. Most symbols will have names pointing into
400 an ELF string table read from a file, or to objalloc memory. */
401 *p = 0;
402
403 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
404
405 if (p != NULL)
406 *p = ELF_VER_CHR;
407
408 if (indx == (bfd_size_type) -1)
409 return FALSE;
410 h->dynstr_index = indx;
411 }
412
413 return TRUE;
414 }
415 \f
416 /* Record an assignment to a symbol made by a linker script. We need
417 this in case some dynamic object refers to this symbol. */
418
419 bfd_boolean
420 bfd_elf_record_link_assignment (bfd *output_bfd ATTRIBUTE_UNUSED,
421 struct bfd_link_info *info,
422 const char *name,
423 bfd_boolean provide)
424 {
425 struct elf_link_hash_entry *h;
426
427 if (!is_elf_hash_table (info->hash))
428 return TRUE;
429
430 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, TRUE, FALSE);
431 if (h == NULL)
432 return FALSE;
433
434 /* Since we're defining the symbol, don't let it seem to have not
435 been defined. record_dynamic_symbol and size_dynamic_sections
436 may depend on this.
437 ??? Changing bfd_link_hash_undefined to bfd_link_hash_new (or
438 to bfd_link_hash_undefweak, see linker.c:link_action) runs the risk
439 of some later symbol manipulation setting the symbol back to
440 bfd_link_hash_undefined, and the linker trying to add the symbol to
441 the undefs list twice. */
442 if (h->root.type == bfd_link_hash_undefweak
443 || h->root.type == bfd_link_hash_undefined)
444 h->root.type = bfd_link_hash_new;
445
446 if (h->root.type == bfd_link_hash_new)
447 h->non_elf = 0;
448
449 /* If this symbol is being provided by the linker script, and it is
450 currently defined by a dynamic object, but not by a regular
451 object, then mark it as undefined so that the generic linker will
452 force the correct value. */
453 if (provide
454 && h->def_dynamic
455 && !h->def_regular)
456 h->root.type = bfd_link_hash_undefined;
457
458 /* If this symbol is not being provided by the linker script, and it is
459 currently defined by a dynamic object, but not by a regular object,
460 then clear out any version information because the symbol will not be
461 associated with the dynamic object any more. */
462 if (!provide
463 && h->def_dynamic
464 && !h->def_regular)
465 h->verinfo.verdef = NULL;
466
467 h->def_regular = 1;
468
469 if ((h->def_dynamic
470 || h->ref_dynamic
471 || info->shared)
472 && h->dynindx == -1)
473 {
474 if (! bfd_elf_link_record_dynamic_symbol (info, h))
475 return FALSE;
476
477 /* If this is a weak defined symbol, and we know a corresponding
478 real symbol from the same dynamic object, make sure the real
479 symbol is also made into a dynamic symbol. */
480 if (h->u.weakdef != NULL
481 && h->u.weakdef->dynindx == -1)
482 {
483 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
484 return FALSE;
485 }
486 }
487
488 return TRUE;
489 }
490
491 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
492 success, and 2 on a failure caused by attempting to record a symbol
493 in a discarded section, eg. a discarded link-once section symbol. */
494
495 int
496 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
497 bfd *input_bfd,
498 long input_indx)
499 {
500 bfd_size_type amt;
501 struct elf_link_local_dynamic_entry *entry;
502 struct elf_link_hash_table *eht;
503 struct elf_strtab_hash *dynstr;
504 unsigned long dynstr_index;
505 char *name;
506 Elf_External_Sym_Shndx eshndx;
507 char esym[sizeof (Elf64_External_Sym)];
508
509 if (! is_elf_hash_table (info->hash))
510 return 0;
511
512 /* See if the entry exists already. */
513 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
514 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
515 return 1;
516
517 amt = sizeof (*entry);
518 entry = bfd_alloc (input_bfd, amt);
519 if (entry == NULL)
520 return 0;
521
522 /* Go find the symbol, so that we can find it's name. */
523 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
524 1, input_indx, &entry->isym, esym, &eshndx))
525 {
526 bfd_release (input_bfd, entry);
527 return 0;
528 }
529
530 if (entry->isym.st_shndx != SHN_UNDEF
531 && (entry->isym.st_shndx < SHN_LORESERVE
532 || entry->isym.st_shndx > SHN_HIRESERVE))
533 {
534 asection *s;
535
536 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
537 if (s == NULL || bfd_is_abs_section (s->output_section))
538 {
539 /* We can still bfd_release here as nothing has done another
540 bfd_alloc. We can't do this later in this function. */
541 bfd_release (input_bfd, entry);
542 return 2;
543 }
544 }
545
546 name = (bfd_elf_string_from_elf_section
547 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
548 entry->isym.st_name));
549
550 dynstr = elf_hash_table (info)->dynstr;
551 if (dynstr == NULL)
552 {
553 /* Create a strtab to hold the dynamic symbol names. */
554 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
555 if (dynstr == NULL)
556 return 0;
557 }
558
559 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
560 if (dynstr_index == (unsigned long) -1)
561 return 0;
562 entry->isym.st_name = dynstr_index;
563
564 eht = elf_hash_table (info);
565
566 entry->next = eht->dynlocal;
567 eht->dynlocal = entry;
568 entry->input_bfd = input_bfd;
569 entry->input_indx = input_indx;
570 eht->dynsymcount++;
571
572 /* Whatever binding the symbol had before, it's now local. */
573 entry->isym.st_info
574 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
575
576 /* The dynindx will be set at the end of size_dynamic_sections. */
577
578 return 1;
579 }
580
581 /* Return the dynindex of a local dynamic symbol. */
582
583 long
584 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
585 bfd *input_bfd,
586 long input_indx)
587 {
588 struct elf_link_local_dynamic_entry *e;
589
590 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
591 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
592 return e->dynindx;
593 return -1;
594 }
595
596 /* This function is used to renumber the dynamic symbols, if some of
597 them are removed because they are marked as local. This is called
598 via elf_link_hash_traverse. */
599
600 static bfd_boolean
601 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
602 void *data)
603 {
604 size_t *count = data;
605
606 if (h->root.type == bfd_link_hash_warning)
607 h = (struct elf_link_hash_entry *) h->root.u.i.link;
608
609 if (h->dynindx != -1)
610 h->dynindx = ++(*count);
611
612 return TRUE;
613 }
614
615 /* Return true if the dynamic symbol for a given section should be
616 omitted when creating a shared library. */
617 bfd_boolean
618 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
619 struct bfd_link_info *info,
620 asection *p)
621 {
622 switch (elf_section_data (p)->this_hdr.sh_type)
623 {
624 case SHT_PROGBITS:
625 case SHT_NOBITS:
626 /* If sh_type is yet undecided, assume it could be
627 SHT_PROGBITS/SHT_NOBITS. */
628 case SHT_NULL:
629 if (strcmp (p->name, ".got") == 0
630 || strcmp (p->name, ".got.plt") == 0
631 || strcmp (p->name, ".plt") == 0)
632 {
633 asection *ip;
634 bfd *dynobj = elf_hash_table (info)->dynobj;
635
636 if (dynobj != NULL
637 && (ip = bfd_get_section_by_name (dynobj, p->name)) != NULL
638 && (ip->flags & SEC_LINKER_CREATED)
639 && ip->output_section == p)
640 return TRUE;
641 }
642 return FALSE;
643
644 /* There shouldn't be section relative relocations
645 against any other section. */
646 default:
647 return TRUE;
648 }
649 }
650
651 /* Assign dynsym indices. In a shared library we generate a section
652 symbol for each output section, which come first. Next come all of
653 the back-end allocated local dynamic syms, followed by the rest of
654 the global symbols. */
655
656 unsigned long
657 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
658 {
659 unsigned long dynsymcount = 0;
660
661 if (info->shared)
662 {
663 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
664 asection *p;
665 for (p = output_bfd->sections; p ; p = p->next)
666 if ((p->flags & SEC_EXCLUDE) == 0
667 && (p->flags & SEC_ALLOC) != 0
668 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
669 elf_section_data (p)->dynindx = ++dynsymcount;
670 }
671
672 if (elf_hash_table (info)->dynlocal)
673 {
674 struct elf_link_local_dynamic_entry *p;
675 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
676 p->dynindx = ++dynsymcount;
677 }
678
679 elf_link_hash_traverse (elf_hash_table (info),
680 elf_link_renumber_hash_table_dynsyms,
681 &dynsymcount);
682
683 /* There is an unused NULL entry at the head of the table which
684 we must account for in our count. Unless there weren't any
685 symbols, which means we'll have no table at all. */
686 if (dynsymcount != 0)
687 ++dynsymcount;
688
689 return elf_hash_table (info)->dynsymcount = dynsymcount;
690 }
691
692 /* This function is called when we want to define a new symbol. It
693 handles the various cases which arise when we find a definition in
694 a dynamic object, or when there is already a definition in a
695 dynamic object. The new symbol is described by NAME, SYM, PSEC,
696 and PVALUE. We set SYM_HASH to the hash table entry. We set
697 OVERRIDE if the old symbol is overriding a new definition. We set
698 TYPE_CHANGE_OK if it is OK for the type to change. We set
699 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
700 change, we mean that we shouldn't warn if the type or size does
701 change. */
702
703 bfd_boolean
704 _bfd_elf_merge_symbol (bfd *abfd,
705 struct bfd_link_info *info,
706 const char *name,
707 Elf_Internal_Sym *sym,
708 asection **psec,
709 bfd_vma *pvalue,
710 struct elf_link_hash_entry **sym_hash,
711 bfd_boolean *skip,
712 bfd_boolean *override,
713 bfd_boolean *type_change_ok,
714 bfd_boolean *size_change_ok)
715 {
716 asection *sec, *oldsec;
717 struct elf_link_hash_entry *h;
718 struct elf_link_hash_entry *flip;
719 int bind;
720 bfd *oldbfd;
721 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
722 bfd_boolean newweak, oldweak;
723
724 *skip = FALSE;
725 *override = FALSE;
726
727 sec = *psec;
728 bind = ELF_ST_BIND (sym->st_info);
729
730 if (! bfd_is_und_section (sec))
731 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
732 else
733 h = ((struct elf_link_hash_entry *)
734 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
735 if (h == NULL)
736 return FALSE;
737 *sym_hash = h;
738
739 /* This code is for coping with dynamic objects, and is only useful
740 if we are doing an ELF link. */
741 if (info->hash->creator != abfd->xvec)
742 return TRUE;
743
744 /* For merging, we only care about real symbols. */
745
746 while (h->root.type == bfd_link_hash_indirect
747 || h->root.type == bfd_link_hash_warning)
748 h = (struct elf_link_hash_entry *) h->root.u.i.link;
749
750 /* If we just created the symbol, mark it as being an ELF symbol.
751 Other than that, there is nothing to do--there is no merge issue
752 with a newly defined symbol--so we just return. */
753
754 if (h->root.type == bfd_link_hash_new)
755 {
756 h->non_elf = 0;
757 return TRUE;
758 }
759
760 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
761 existing symbol. */
762
763 switch (h->root.type)
764 {
765 default:
766 oldbfd = NULL;
767 oldsec = NULL;
768 break;
769
770 case bfd_link_hash_undefined:
771 case bfd_link_hash_undefweak:
772 oldbfd = h->root.u.undef.abfd;
773 oldsec = NULL;
774 break;
775
776 case bfd_link_hash_defined:
777 case bfd_link_hash_defweak:
778 oldbfd = h->root.u.def.section->owner;
779 oldsec = h->root.u.def.section;
780 break;
781
782 case bfd_link_hash_common:
783 oldbfd = h->root.u.c.p->section->owner;
784 oldsec = h->root.u.c.p->section;
785 break;
786 }
787
788 /* In cases involving weak versioned symbols, we may wind up trying
789 to merge a symbol with itself. Catch that here, to avoid the
790 confusion that results if we try to override a symbol with
791 itself. The additional tests catch cases like
792 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
793 dynamic object, which we do want to handle here. */
794 if (abfd == oldbfd
795 && ((abfd->flags & DYNAMIC) == 0
796 || !h->def_regular))
797 return TRUE;
798
799 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
800 respectively, is from a dynamic object. */
801
802 if ((abfd->flags & DYNAMIC) != 0)
803 newdyn = TRUE;
804 else
805 newdyn = FALSE;
806
807 if (oldbfd != NULL)
808 olddyn = (oldbfd->flags & DYNAMIC) != 0;
809 else
810 {
811 asection *hsec;
812
813 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
814 indices used by MIPS ELF. */
815 switch (h->root.type)
816 {
817 default:
818 hsec = NULL;
819 break;
820
821 case bfd_link_hash_defined:
822 case bfd_link_hash_defweak:
823 hsec = h->root.u.def.section;
824 break;
825
826 case bfd_link_hash_common:
827 hsec = h->root.u.c.p->section;
828 break;
829 }
830
831 if (hsec == NULL)
832 olddyn = FALSE;
833 else
834 olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0;
835 }
836
837 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
838 respectively, appear to be a definition rather than reference. */
839
840 if (bfd_is_und_section (sec) || bfd_is_com_section (sec))
841 newdef = FALSE;
842 else
843 newdef = TRUE;
844
845 if (h->root.type == bfd_link_hash_undefined
846 || h->root.type == bfd_link_hash_undefweak
847 || h->root.type == bfd_link_hash_common)
848 olddef = FALSE;
849 else
850 olddef = TRUE;
851
852 /* Check TLS symbol. */
853 if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)
854 && ELF_ST_TYPE (sym->st_info) != h->type)
855 {
856 bfd *ntbfd, *tbfd;
857 bfd_boolean ntdef, tdef;
858 asection *ntsec, *tsec;
859
860 if (h->type == STT_TLS)
861 {
862 ntbfd = abfd;
863 ntsec = sec;
864 ntdef = newdef;
865 tbfd = oldbfd;
866 tsec = oldsec;
867 tdef = olddef;
868 }
869 else
870 {
871 ntbfd = oldbfd;
872 ntsec = oldsec;
873 ntdef = olddef;
874 tbfd = abfd;
875 tsec = sec;
876 tdef = newdef;
877 }
878
879 if (tdef && ntdef)
880 (*_bfd_error_handler)
881 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
882 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
883 else if (!tdef && !ntdef)
884 (*_bfd_error_handler)
885 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
886 tbfd, ntbfd, h->root.root.string);
887 else if (tdef)
888 (*_bfd_error_handler)
889 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
890 tbfd, tsec, ntbfd, h->root.root.string);
891 else
892 (*_bfd_error_handler)
893 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
894 tbfd, ntbfd, ntsec, h->root.root.string);
895
896 bfd_set_error (bfd_error_bad_value);
897 return FALSE;
898 }
899
900 /* We need to remember if a symbol has a definition in a dynamic
901 object or is weak in all dynamic objects. Internal and hidden
902 visibility will make it unavailable to dynamic objects. */
903 if (newdyn && !h->dynamic_def)
904 {
905 if (!bfd_is_und_section (sec))
906 h->dynamic_def = 1;
907 else
908 {
909 /* Check if this symbol is weak in all dynamic objects. If it
910 is the first time we see it in a dynamic object, we mark
911 if it is weak. Otherwise, we clear it. */
912 if (!h->ref_dynamic)
913 {
914 if (bind == STB_WEAK)
915 h->dynamic_weak = 1;
916 }
917 else if (bind != STB_WEAK)
918 h->dynamic_weak = 0;
919 }
920 }
921
922 /* If the old symbol has non-default visibility, we ignore the new
923 definition from a dynamic object. */
924 if (newdyn
925 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
926 && !bfd_is_und_section (sec))
927 {
928 *skip = TRUE;
929 /* Make sure this symbol is dynamic. */
930 h->ref_dynamic = 1;
931 /* A protected symbol has external availability. Make sure it is
932 recorded as dynamic.
933
934 FIXME: Should we check type and size for protected symbol? */
935 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
936 return bfd_elf_link_record_dynamic_symbol (info, h);
937 else
938 return TRUE;
939 }
940 else if (!newdyn
941 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
942 && h->def_dynamic)
943 {
944 /* If the new symbol with non-default visibility comes from a
945 relocatable file and the old definition comes from a dynamic
946 object, we remove the old definition. */
947 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
948 h = *sym_hash;
949
950 if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root)
951 && bfd_is_und_section (sec))
952 {
953 /* If the new symbol is undefined and the old symbol was
954 also undefined before, we need to make sure
955 _bfd_generic_link_add_one_symbol doesn't mess
956 up the linker hash table undefs list. Since the old
957 definition came from a dynamic object, it is still on the
958 undefs list. */
959 h->root.type = bfd_link_hash_undefined;
960 h->root.u.undef.abfd = abfd;
961 }
962 else
963 {
964 h->root.type = bfd_link_hash_new;
965 h->root.u.undef.abfd = NULL;
966 }
967
968 if (h->def_dynamic)
969 {
970 h->def_dynamic = 0;
971 h->ref_dynamic = 1;
972 h->dynamic_def = 1;
973 }
974 /* FIXME: Should we check type and size for protected symbol? */
975 h->size = 0;
976 h->type = 0;
977 return TRUE;
978 }
979
980 /* Differentiate strong and weak symbols. */
981 newweak = bind == STB_WEAK;
982 oldweak = (h->root.type == bfd_link_hash_defweak
983 || h->root.type == bfd_link_hash_undefweak);
984
985 /* If a new weak symbol definition comes from a regular file and the
986 old symbol comes from a dynamic library, we treat the new one as
987 strong. Similarly, an old weak symbol definition from a regular
988 file is treated as strong when the new symbol comes from a dynamic
989 library. Further, an old weak symbol from a dynamic library is
990 treated as strong if the new symbol is from a dynamic library.
991 This reflects the way glibc's ld.so works.
992
993 Do this before setting *type_change_ok or *size_change_ok so that
994 we warn properly when dynamic library symbols are overridden. */
995
996 if (newdef && !newdyn && olddyn)
997 newweak = FALSE;
998 if (olddef && newdyn)
999 oldweak = FALSE;
1000
1001 /* It's OK to change the type if either the existing symbol or the
1002 new symbol is weak. A type change is also OK if the old symbol
1003 is undefined and the new symbol is defined. */
1004
1005 if (oldweak
1006 || newweak
1007 || (newdef
1008 && h->root.type == bfd_link_hash_undefined))
1009 *type_change_ok = TRUE;
1010
1011 /* It's OK to change the size if either the existing symbol or the
1012 new symbol is weak, or if the old symbol is undefined. */
1013
1014 if (*type_change_ok
1015 || h->root.type == bfd_link_hash_undefined)
1016 *size_change_ok = TRUE;
1017
1018 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1019 symbol, respectively, appears to be a common symbol in a dynamic
1020 object. If a symbol appears in an uninitialized section, and is
1021 not weak, and is not a function, then it may be a common symbol
1022 which was resolved when the dynamic object was created. We want
1023 to treat such symbols specially, because they raise special
1024 considerations when setting the symbol size: if the symbol
1025 appears as a common symbol in a regular object, and the size in
1026 the regular object is larger, we must make sure that we use the
1027 larger size. This problematic case can always be avoided in C,
1028 but it must be handled correctly when using Fortran shared
1029 libraries.
1030
1031 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1032 likewise for OLDDYNCOMMON and OLDDEF.
1033
1034 Note that this test is just a heuristic, and that it is quite
1035 possible to have an uninitialized symbol in a shared object which
1036 is really a definition, rather than a common symbol. This could
1037 lead to some minor confusion when the symbol really is a common
1038 symbol in some regular object. However, I think it will be
1039 harmless. */
1040
1041 if (newdyn
1042 && newdef
1043 && !newweak
1044 && (sec->flags & SEC_ALLOC) != 0
1045 && (sec->flags & SEC_LOAD) == 0
1046 && sym->st_size > 0
1047 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
1048 newdyncommon = TRUE;
1049 else
1050 newdyncommon = FALSE;
1051
1052 if (olddyn
1053 && olddef
1054 && h->root.type == bfd_link_hash_defined
1055 && h->def_dynamic
1056 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1057 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1058 && h->size > 0
1059 && h->type != STT_FUNC)
1060 olddyncommon = TRUE;
1061 else
1062 olddyncommon = FALSE;
1063
1064 /* If both the old and the new symbols look like common symbols in a
1065 dynamic object, set the size of the symbol to the larger of the
1066 two. */
1067
1068 if (olddyncommon
1069 && newdyncommon
1070 && sym->st_size != h->size)
1071 {
1072 /* Since we think we have two common symbols, issue a multiple
1073 common warning if desired. Note that we only warn if the
1074 size is different. If the size is the same, we simply let
1075 the old symbol override the new one as normally happens with
1076 symbols defined in dynamic objects. */
1077
1078 if (! ((*info->callbacks->multiple_common)
1079 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1080 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1081 return FALSE;
1082
1083 if (sym->st_size > h->size)
1084 h->size = sym->st_size;
1085
1086 *size_change_ok = TRUE;
1087 }
1088
1089 /* If we are looking at a dynamic object, and we have found a
1090 definition, we need to see if the symbol was already defined by
1091 some other object. If so, we want to use the existing
1092 definition, and we do not want to report a multiple symbol
1093 definition error; we do this by clobbering *PSEC to be
1094 bfd_und_section_ptr.
1095
1096 We treat a common symbol as a definition if the symbol in the
1097 shared library is a function, since common symbols always
1098 represent variables; this can cause confusion in principle, but
1099 any such confusion would seem to indicate an erroneous program or
1100 shared library. We also permit a common symbol in a regular
1101 object to override a weak symbol in a shared object. */
1102
1103 if (newdyn
1104 && newdef
1105 && (olddef
1106 || (h->root.type == bfd_link_hash_common
1107 && (newweak
1108 || ELF_ST_TYPE (sym->st_info) == STT_FUNC))))
1109 {
1110 *override = TRUE;
1111 newdef = FALSE;
1112 newdyncommon = FALSE;
1113
1114 *psec = sec = bfd_und_section_ptr;
1115 *size_change_ok = TRUE;
1116
1117 /* If we get here when the old symbol is a common symbol, then
1118 we are explicitly letting it override a weak symbol or
1119 function in a dynamic object, and we don't want to warn about
1120 a type change. If the old symbol is a defined symbol, a type
1121 change warning may still be appropriate. */
1122
1123 if (h->root.type == bfd_link_hash_common)
1124 *type_change_ok = TRUE;
1125 }
1126
1127 /* Handle the special case of an old common symbol merging with a
1128 new symbol which looks like a common symbol in a shared object.
1129 We change *PSEC and *PVALUE to make the new symbol look like a
1130 common symbol, and let _bfd_generic_link_add_one_symbol will do
1131 the right thing. */
1132
1133 if (newdyncommon
1134 && h->root.type == bfd_link_hash_common)
1135 {
1136 *override = TRUE;
1137 newdef = FALSE;
1138 newdyncommon = FALSE;
1139 *pvalue = sym->st_size;
1140 *psec = sec = bfd_com_section_ptr;
1141 *size_change_ok = TRUE;
1142 }
1143
1144 /* If the old symbol is from a dynamic object, and the new symbol is
1145 a definition which is not from a dynamic object, then the new
1146 symbol overrides the old symbol. Symbols from regular files
1147 always take precedence over symbols from dynamic objects, even if
1148 they are defined after the dynamic object in the link.
1149
1150 As above, we again permit a common symbol in a regular object to
1151 override a definition in a shared object if the shared object
1152 symbol is a function or is weak. */
1153
1154 flip = NULL;
1155 if (! newdyn
1156 && (newdef
1157 || (bfd_is_com_section (sec)
1158 && (oldweak
1159 || h->type == STT_FUNC)))
1160 && olddyn
1161 && olddef
1162 && h->def_dynamic)
1163 {
1164 /* Change the hash table entry to undefined, and let
1165 _bfd_generic_link_add_one_symbol do the right thing with the
1166 new definition. */
1167
1168 h->root.type = bfd_link_hash_undefined;
1169 h->root.u.undef.abfd = h->root.u.def.section->owner;
1170 *size_change_ok = TRUE;
1171
1172 olddef = FALSE;
1173 olddyncommon = FALSE;
1174
1175 /* We again permit a type change when a common symbol may be
1176 overriding a function. */
1177
1178 if (bfd_is_com_section (sec))
1179 *type_change_ok = TRUE;
1180
1181 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1182 flip = *sym_hash;
1183 else
1184 /* This union may have been set to be non-NULL when this symbol
1185 was seen in a dynamic object. We must force the union to be
1186 NULL, so that it is correct for a regular symbol. */
1187 h->verinfo.vertree = NULL;
1188 }
1189
1190 /* Handle the special case of a new common symbol merging with an
1191 old symbol that looks like it might be a common symbol defined in
1192 a shared object. Note that we have already handled the case in
1193 which a new common symbol should simply override the definition
1194 in the shared library. */
1195
1196 if (! newdyn
1197 && bfd_is_com_section (sec)
1198 && olddyncommon)
1199 {
1200 /* It would be best if we could set the hash table entry to a
1201 common symbol, but we don't know what to use for the section
1202 or the alignment. */
1203 if (! ((*info->callbacks->multiple_common)
1204 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1205 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1206 return FALSE;
1207
1208 /* If the presumed common symbol in the dynamic object is
1209 larger, pretend that the new symbol has its size. */
1210
1211 if (h->size > *pvalue)
1212 *pvalue = h->size;
1213
1214 /* FIXME: We no longer know the alignment required by the symbol
1215 in the dynamic object, so we just wind up using the one from
1216 the regular object. */
1217
1218 olddef = FALSE;
1219 olddyncommon = FALSE;
1220
1221 h->root.type = bfd_link_hash_undefined;
1222 h->root.u.undef.abfd = h->root.u.def.section->owner;
1223
1224 *size_change_ok = TRUE;
1225 *type_change_ok = TRUE;
1226
1227 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1228 flip = *sym_hash;
1229 else
1230 h->verinfo.vertree = NULL;
1231 }
1232
1233 if (flip != NULL)
1234 {
1235 /* Handle the case where we had a versioned symbol in a dynamic
1236 library and now find a definition in a normal object. In this
1237 case, we make the versioned symbol point to the normal one. */
1238 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1239 flip->root.type = h->root.type;
1240 h->root.type = bfd_link_hash_indirect;
1241 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1242 (*bed->elf_backend_copy_indirect_symbol) (bed, flip, h);
1243 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1244 if (h->def_dynamic)
1245 {
1246 h->def_dynamic = 0;
1247 flip->ref_dynamic = 1;
1248 }
1249 }
1250
1251 return TRUE;
1252 }
1253
1254 /* This function is called to create an indirect symbol from the
1255 default for the symbol with the default version if needed. The
1256 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1257 set DYNSYM if the new indirect symbol is dynamic. */
1258
1259 bfd_boolean
1260 _bfd_elf_add_default_symbol (bfd *abfd,
1261 struct bfd_link_info *info,
1262 struct elf_link_hash_entry *h,
1263 const char *name,
1264 Elf_Internal_Sym *sym,
1265 asection **psec,
1266 bfd_vma *value,
1267 bfd_boolean *dynsym,
1268 bfd_boolean override)
1269 {
1270 bfd_boolean type_change_ok;
1271 bfd_boolean size_change_ok;
1272 bfd_boolean skip;
1273 char *shortname;
1274 struct elf_link_hash_entry *hi;
1275 struct bfd_link_hash_entry *bh;
1276 const struct elf_backend_data *bed;
1277 bfd_boolean collect;
1278 bfd_boolean dynamic;
1279 char *p;
1280 size_t len, shortlen;
1281 asection *sec;
1282
1283 /* If this symbol has a version, and it is the default version, we
1284 create an indirect symbol from the default name to the fully
1285 decorated name. This will cause external references which do not
1286 specify a version to be bound to this version of the symbol. */
1287 p = strchr (name, ELF_VER_CHR);
1288 if (p == NULL || p[1] != ELF_VER_CHR)
1289 return TRUE;
1290
1291 if (override)
1292 {
1293 /* We are overridden by an old definition. We need to check if we
1294 need to create the indirect symbol from the default name. */
1295 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1296 FALSE, FALSE);
1297 BFD_ASSERT (hi != NULL);
1298 if (hi == h)
1299 return TRUE;
1300 while (hi->root.type == bfd_link_hash_indirect
1301 || hi->root.type == bfd_link_hash_warning)
1302 {
1303 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1304 if (hi == h)
1305 return TRUE;
1306 }
1307 }
1308
1309 bed = get_elf_backend_data (abfd);
1310 collect = bed->collect;
1311 dynamic = (abfd->flags & DYNAMIC) != 0;
1312
1313 shortlen = p - name;
1314 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1315 if (shortname == NULL)
1316 return FALSE;
1317 memcpy (shortname, name, shortlen);
1318 shortname[shortlen] = '\0';
1319
1320 /* We are going to create a new symbol. Merge it with any existing
1321 symbol with this name. For the purposes of the merge, act as
1322 though we were defining the symbol we just defined, although we
1323 actually going to define an indirect symbol. */
1324 type_change_ok = FALSE;
1325 size_change_ok = FALSE;
1326 sec = *psec;
1327 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1328 &hi, &skip, &override, &type_change_ok,
1329 &size_change_ok))
1330 return FALSE;
1331
1332 if (skip)
1333 goto nondefault;
1334
1335 if (! override)
1336 {
1337 bh = &hi->root;
1338 if (! (_bfd_generic_link_add_one_symbol
1339 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1340 0, name, FALSE, collect, &bh)))
1341 return FALSE;
1342 hi = (struct elf_link_hash_entry *) bh;
1343 }
1344 else
1345 {
1346 /* In this case the symbol named SHORTNAME is overriding the
1347 indirect symbol we want to add. We were planning on making
1348 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1349 is the name without a version. NAME is the fully versioned
1350 name, and it is the default version.
1351
1352 Overriding means that we already saw a definition for the
1353 symbol SHORTNAME in a regular object, and it is overriding
1354 the symbol defined in the dynamic object.
1355
1356 When this happens, we actually want to change NAME, the
1357 symbol we just added, to refer to SHORTNAME. This will cause
1358 references to NAME in the shared object to become references
1359 to SHORTNAME in the regular object. This is what we expect
1360 when we override a function in a shared object: that the
1361 references in the shared object will be mapped to the
1362 definition in the regular object. */
1363
1364 while (hi->root.type == bfd_link_hash_indirect
1365 || hi->root.type == bfd_link_hash_warning)
1366 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1367
1368 h->root.type = bfd_link_hash_indirect;
1369 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1370 if (h->def_dynamic)
1371 {
1372 h->def_dynamic = 0;
1373 hi->ref_dynamic = 1;
1374 if (hi->ref_regular
1375 || hi->def_regular)
1376 {
1377 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1378 return FALSE;
1379 }
1380 }
1381
1382 /* Now set HI to H, so that the following code will set the
1383 other fields correctly. */
1384 hi = h;
1385 }
1386
1387 /* If there is a duplicate definition somewhere, then HI may not
1388 point to an indirect symbol. We will have reported an error to
1389 the user in that case. */
1390
1391 if (hi->root.type == bfd_link_hash_indirect)
1392 {
1393 struct elf_link_hash_entry *ht;
1394
1395 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1396 (*bed->elf_backend_copy_indirect_symbol) (bed, ht, hi);
1397
1398 /* See if the new flags lead us to realize that the symbol must
1399 be dynamic. */
1400 if (! *dynsym)
1401 {
1402 if (! dynamic)
1403 {
1404 if (info->shared
1405 || hi->ref_dynamic)
1406 *dynsym = TRUE;
1407 }
1408 else
1409 {
1410 if (hi->ref_regular)
1411 *dynsym = TRUE;
1412 }
1413 }
1414 }
1415
1416 /* We also need to define an indirection from the nondefault version
1417 of the symbol. */
1418
1419 nondefault:
1420 len = strlen (name);
1421 shortname = bfd_hash_allocate (&info->hash->table, len);
1422 if (shortname == NULL)
1423 return FALSE;
1424 memcpy (shortname, name, shortlen);
1425 memcpy (shortname + shortlen, p + 1, len - shortlen);
1426
1427 /* Once again, merge with any existing symbol. */
1428 type_change_ok = FALSE;
1429 size_change_ok = FALSE;
1430 sec = *psec;
1431 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1432 &hi, &skip, &override, &type_change_ok,
1433 &size_change_ok))
1434 return FALSE;
1435
1436 if (skip)
1437 return TRUE;
1438
1439 if (override)
1440 {
1441 /* Here SHORTNAME is a versioned name, so we don't expect to see
1442 the type of override we do in the case above unless it is
1443 overridden by a versioned definition. */
1444 if (hi->root.type != bfd_link_hash_defined
1445 && hi->root.type != bfd_link_hash_defweak)
1446 (*_bfd_error_handler)
1447 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1448 abfd, shortname);
1449 }
1450 else
1451 {
1452 bh = &hi->root;
1453 if (! (_bfd_generic_link_add_one_symbol
1454 (info, abfd, shortname, BSF_INDIRECT,
1455 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1456 return FALSE;
1457 hi = (struct elf_link_hash_entry *) bh;
1458
1459 /* If there is a duplicate definition somewhere, then HI may not
1460 point to an indirect symbol. We will have reported an error
1461 to the user in that case. */
1462
1463 if (hi->root.type == bfd_link_hash_indirect)
1464 {
1465 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
1466
1467 /* See if the new flags lead us to realize that the symbol
1468 must be dynamic. */
1469 if (! *dynsym)
1470 {
1471 if (! dynamic)
1472 {
1473 if (info->shared
1474 || hi->ref_dynamic)
1475 *dynsym = TRUE;
1476 }
1477 else
1478 {
1479 if (hi->ref_regular)
1480 *dynsym = TRUE;
1481 }
1482 }
1483 }
1484 }
1485
1486 return TRUE;
1487 }
1488 \f
1489 /* This routine is used to export all defined symbols into the dynamic
1490 symbol table. It is called via elf_link_hash_traverse. */
1491
1492 bfd_boolean
1493 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1494 {
1495 struct elf_info_failed *eif = data;
1496
1497 /* Ignore indirect symbols. These are added by the versioning code. */
1498 if (h->root.type == bfd_link_hash_indirect)
1499 return TRUE;
1500
1501 if (h->root.type == bfd_link_hash_warning)
1502 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1503
1504 if (h->dynindx == -1
1505 && (h->def_regular
1506 || h->ref_regular))
1507 {
1508 struct bfd_elf_version_tree *t;
1509 struct bfd_elf_version_expr *d;
1510
1511 for (t = eif->verdefs; t != NULL; t = t->next)
1512 {
1513 if (t->globals.list != NULL)
1514 {
1515 d = (*t->match) (&t->globals, NULL, h->root.root.string);
1516 if (d != NULL)
1517 goto doit;
1518 }
1519
1520 if (t->locals.list != NULL)
1521 {
1522 d = (*t->match) (&t->locals, NULL, h->root.root.string);
1523 if (d != NULL)
1524 return TRUE;
1525 }
1526 }
1527
1528 if (!eif->verdefs)
1529 {
1530 doit:
1531 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1532 {
1533 eif->failed = TRUE;
1534 return FALSE;
1535 }
1536 }
1537 }
1538
1539 return TRUE;
1540 }
1541 \f
1542 /* Look through the symbols which are defined in other shared
1543 libraries and referenced here. Update the list of version
1544 dependencies. This will be put into the .gnu.version_r section.
1545 This function is called via elf_link_hash_traverse. */
1546
1547 bfd_boolean
1548 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1549 void *data)
1550 {
1551 struct elf_find_verdep_info *rinfo = data;
1552 Elf_Internal_Verneed *t;
1553 Elf_Internal_Vernaux *a;
1554 bfd_size_type amt;
1555
1556 if (h->root.type == bfd_link_hash_warning)
1557 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1558
1559 /* We only care about symbols defined in shared objects with version
1560 information. */
1561 if (!h->def_dynamic
1562 || h->def_regular
1563 || h->dynindx == -1
1564 || h->verinfo.verdef == NULL)
1565 return TRUE;
1566
1567 /* See if we already know about this version. */
1568 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1569 {
1570 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1571 continue;
1572
1573 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1574 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1575 return TRUE;
1576
1577 break;
1578 }
1579
1580 /* This is a new version. Add it to tree we are building. */
1581
1582 if (t == NULL)
1583 {
1584 amt = sizeof *t;
1585 t = bfd_zalloc (rinfo->output_bfd, amt);
1586 if (t == NULL)
1587 {
1588 rinfo->failed = TRUE;
1589 return FALSE;
1590 }
1591
1592 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1593 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1594 elf_tdata (rinfo->output_bfd)->verref = t;
1595 }
1596
1597 amt = sizeof *a;
1598 a = bfd_zalloc (rinfo->output_bfd, amt);
1599
1600 /* Note that we are copying a string pointer here, and testing it
1601 above. If bfd_elf_string_from_elf_section is ever changed to
1602 discard the string data when low in memory, this will have to be
1603 fixed. */
1604 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1605
1606 a->vna_flags = h->verinfo.verdef->vd_flags;
1607 a->vna_nextptr = t->vn_auxptr;
1608
1609 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1610 ++rinfo->vers;
1611
1612 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1613
1614 t->vn_auxptr = a;
1615
1616 return TRUE;
1617 }
1618
1619 /* Figure out appropriate versions for all the symbols. We may not
1620 have the version number script until we have read all of the input
1621 files, so until that point we don't know which symbols should be
1622 local. This function is called via elf_link_hash_traverse. */
1623
1624 bfd_boolean
1625 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1626 {
1627 struct elf_assign_sym_version_info *sinfo;
1628 struct bfd_link_info *info;
1629 const struct elf_backend_data *bed;
1630 struct elf_info_failed eif;
1631 char *p;
1632 bfd_size_type amt;
1633
1634 sinfo = data;
1635 info = sinfo->info;
1636
1637 if (h->root.type == bfd_link_hash_warning)
1638 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1639
1640 /* Fix the symbol flags. */
1641 eif.failed = FALSE;
1642 eif.info = info;
1643 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1644 {
1645 if (eif.failed)
1646 sinfo->failed = TRUE;
1647 return FALSE;
1648 }
1649
1650 /* We only need version numbers for symbols defined in regular
1651 objects. */
1652 if (!h->def_regular)
1653 return TRUE;
1654
1655 bed = get_elf_backend_data (sinfo->output_bfd);
1656 p = strchr (h->root.root.string, ELF_VER_CHR);
1657 if (p != NULL && h->verinfo.vertree == NULL)
1658 {
1659 struct bfd_elf_version_tree *t;
1660 bfd_boolean hidden;
1661
1662 hidden = TRUE;
1663
1664 /* There are two consecutive ELF_VER_CHR characters if this is
1665 not a hidden symbol. */
1666 ++p;
1667 if (*p == ELF_VER_CHR)
1668 {
1669 hidden = FALSE;
1670 ++p;
1671 }
1672
1673 /* If there is no version string, we can just return out. */
1674 if (*p == '\0')
1675 {
1676 if (hidden)
1677 h->hidden = 1;
1678 return TRUE;
1679 }
1680
1681 /* Look for the version. If we find it, it is no longer weak. */
1682 for (t = sinfo->verdefs; t != NULL; t = t->next)
1683 {
1684 if (strcmp (t->name, p) == 0)
1685 {
1686 size_t len;
1687 char *alc;
1688 struct bfd_elf_version_expr *d;
1689
1690 len = p - h->root.root.string;
1691 alc = bfd_malloc (len);
1692 if (alc == NULL)
1693 return FALSE;
1694 memcpy (alc, h->root.root.string, len - 1);
1695 alc[len - 1] = '\0';
1696 if (alc[len - 2] == ELF_VER_CHR)
1697 alc[len - 2] = '\0';
1698
1699 h->verinfo.vertree = t;
1700 t->used = TRUE;
1701 d = NULL;
1702
1703 if (t->globals.list != NULL)
1704 d = (*t->match) (&t->globals, NULL, alc);
1705
1706 /* See if there is anything to force this symbol to
1707 local scope. */
1708 if (d == NULL && t->locals.list != NULL)
1709 {
1710 d = (*t->match) (&t->locals, NULL, alc);
1711 if (d != NULL
1712 && h->dynindx != -1
1713 && info->shared
1714 && ! info->export_dynamic)
1715 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1716 }
1717
1718 free (alc);
1719 break;
1720 }
1721 }
1722
1723 /* If we are building an application, we need to create a
1724 version node for this version. */
1725 if (t == NULL && info->executable)
1726 {
1727 struct bfd_elf_version_tree **pp;
1728 int version_index;
1729
1730 /* If we aren't going to export this symbol, we don't need
1731 to worry about it. */
1732 if (h->dynindx == -1)
1733 return TRUE;
1734
1735 amt = sizeof *t;
1736 t = bfd_zalloc (sinfo->output_bfd, amt);
1737 if (t == NULL)
1738 {
1739 sinfo->failed = TRUE;
1740 return FALSE;
1741 }
1742
1743 t->name = p;
1744 t->name_indx = (unsigned int) -1;
1745 t->used = TRUE;
1746
1747 version_index = 1;
1748 /* Don't count anonymous version tag. */
1749 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1750 version_index = 0;
1751 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1752 ++version_index;
1753 t->vernum = version_index;
1754
1755 *pp = t;
1756
1757 h->verinfo.vertree = t;
1758 }
1759 else if (t == NULL)
1760 {
1761 /* We could not find the version for a symbol when
1762 generating a shared archive. Return an error. */
1763 (*_bfd_error_handler)
1764 (_("%B: undefined versioned symbol name %s"),
1765 sinfo->output_bfd, h->root.root.string);
1766 bfd_set_error (bfd_error_bad_value);
1767 sinfo->failed = TRUE;
1768 return FALSE;
1769 }
1770
1771 if (hidden)
1772 h->hidden = 1;
1773 }
1774
1775 /* If we don't have a version for this symbol, see if we can find
1776 something. */
1777 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1778 {
1779 struct bfd_elf_version_tree *t;
1780 struct bfd_elf_version_tree *local_ver;
1781 struct bfd_elf_version_expr *d;
1782
1783 /* See if can find what version this symbol is in. If the
1784 symbol is supposed to be local, then don't actually register
1785 it. */
1786 local_ver = NULL;
1787 for (t = sinfo->verdefs; t != NULL; t = t->next)
1788 {
1789 if (t->globals.list != NULL)
1790 {
1791 bfd_boolean matched;
1792
1793 matched = FALSE;
1794 d = NULL;
1795 while ((d = (*t->match) (&t->globals, d,
1796 h->root.root.string)) != NULL)
1797 if (d->symver)
1798 matched = TRUE;
1799 else
1800 {
1801 /* There is a version without definition. Make
1802 the symbol the default definition for this
1803 version. */
1804 h->verinfo.vertree = t;
1805 local_ver = NULL;
1806 d->script = 1;
1807 break;
1808 }
1809 if (d != NULL)
1810 break;
1811 else if (matched)
1812 /* There is no undefined version for this symbol. Hide the
1813 default one. */
1814 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1815 }
1816
1817 if (t->locals.list != NULL)
1818 {
1819 d = NULL;
1820 while ((d = (*t->match) (&t->locals, d,
1821 h->root.root.string)) != NULL)
1822 {
1823 local_ver = t;
1824 /* If the match is "*", keep looking for a more
1825 explicit, perhaps even global, match.
1826 XXX: Shouldn't this be !d->wildcard instead? */
1827 if (d->pattern[0] != '*' || d->pattern[1] != '\0')
1828 break;
1829 }
1830
1831 if (d != NULL)
1832 break;
1833 }
1834 }
1835
1836 if (local_ver != NULL)
1837 {
1838 h->verinfo.vertree = local_ver;
1839 if (h->dynindx != -1
1840 && info->shared
1841 && ! info->export_dynamic)
1842 {
1843 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1844 }
1845 }
1846 }
1847
1848 return TRUE;
1849 }
1850 \f
1851 /* Read and swap the relocs from the section indicated by SHDR. This
1852 may be either a REL or a RELA section. The relocations are
1853 translated into RELA relocations and stored in INTERNAL_RELOCS,
1854 which should have already been allocated to contain enough space.
1855 The EXTERNAL_RELOCS are a buffer where the external form of the
1856 relocations should be stored.
1857
1858 Returns FALSE if something goes wrong. */
1859
1860 static bfd_boolean
1861 elf_link_read_relocs_from_section (bfd *abfd,
1862 asection *sec,
1863 Elf_Internal_Shdr *shdr,
1864 void *external_relocs,
1865 Elf_Internal_Rela *internal_relocs)
1866 {
1867 const struct elf_backend_data *bed;
1868 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
1869 const bfd_byte *erela;
1870 const bfd_byte *erelaend;
1871 Elf_Internal_Rela *irela;
1872 Elf_Internal_Shdr *symtab_hdr;
1873 size_t nsyms;
1874
1875 /* Position ourselves at the start of the section. */
1876 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
1877 return FALSE;
1878
1879 /* Read the relocations. */
1880 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
1881 return FALSE;
1882
1883 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1884 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
1885
1886 bed = get_elf_backend_data (abfd);
1887
1888 /* Convert the external relocations to the internal format. */
1889 if (shdr->sh_entsize == bed->s->sizeof_rel)
1890 swap_in = bed->s->swap_reloc_in;
1891 else if (shdr->sh_entsize == bed->s->sizeof_rela)
1892 swap_in = bed->s->swap_reloca_in;
1893 else
1894 {
1895 bfd_set_error (bfd_error_wrong_format);
1896 return FALSE;
1897 }
1898
1899 erela = external_relocs;
1900 erelaend = erela + shdr->sh_size;
1901 irela = internal_relocs;
1902 while (erela < erelaend)
1903 {
1904 bfd_vma r_symndx;
1905
1906 (*swap_in) (abfd, erela, irela);
1907 r_symndx = ELF32_R_SYM (irela->r_info);
1908 if (bed->s->arch_size == 64)
1909 r_symndx >>= 24;
1910 if ((size_t) r_symndx >= nsyms)
1911 {
1912 (*_bfd_error_handler)
1913 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
1914 " for offset 0x%lx in section `%A'"),
1915 abfd, sec,
1916 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
1917 bfd_set_error (bfd_error_bad_value);
1918 return FALSE;
1919 }
1920 irela += bed->s->int_rels_per_ext_rel;
1921 erela += shdr->sh_entsize;
1922 }
1923
1924 return TRUE;
1925 }
1926
1927 /* Read and swap the relocs for a section O. They may have been
1928 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1929 not NULL, they are used as buffers to read into. They are known to
1930 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1931 the return value is allocated using either malloc or bfd_alloc,
1932 according to the KEEP_MEMORY argument. If O has two relocation
1933 sections (both REL and RELA relocations), then the REL_HDR
1934 relocations will appear first in INTERNAL_RELOCS, followed by the
1935 REL_HDR2 relocations. */
1936
1937 Elf_Internal_Rela *
1938 _bfd_elf_link_read_relocs (bfd *abfd,
1939 asection *o,
1940 void *external_relocs,
1941 Elf_Internal_Rela *internal_relocs,
1942 bfd_boolean keep_memory)
1943 {
1944 Elf_Internal_Shdr *rel_hdr;
1945 void *alloc1 = NULL;
1946 Elf_Internal_Rela *alloc2 = NULL;
1947 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1948
1949 if (elf_section_data (o)->relocs != NULL)
1950 return elf_section_data (o)->relocs;
1951
1952 if (o->reloc_count == 0)
1953 return NULL;
1954
1955 rel_hdr = &elf_section_data (o)->rel_hdr;
1956
1957 if (internal_relocs == NULL)
1958 {
1959 bfd_size_type size;
1960
1961 size = o->reloc_count;
1962 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
1963 if (keep_memory)
1964 internal_relocs = bfd_alloc (abfd, size);
1965 else
1966 internal_relocs = alloc2 = bfd_malloc (size);
1967 if (internal_relocs == NULL)
1968 goto error_return;
1969 }
1970
1971 if (external_relocs == NULL)
1972 {
1973 bfd_size_type size = rel_hdr->sh_size;
1974
1975 if (elf_section_data (o)->rel_hdr2)
1976 size += elf_section_data (o)->rel_hdr2->sh_size;
1977 alloc1 = bfd_malloc (size);
1978 if (alloc1 == NULL)
1979 goto error_return;
1980 external_relocs = alloc1;
1981 }
1982
1983 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
1984 external_relocs,
1985 internal_relocs))
1986 goto error_return;
1987 if (elf_section_data (o)->rel_hdr2
1988 && (!elf_link_read_relocs_from_section
1989 (abfd, o,
1990 elf_section_data (o)->rel_hdr2,
1991 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
1992 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
1993 * bed->s->int_rels_per_ext_rel))))
1994 goto error_return;
1995
1996 /* Cache the results for next time, if we can. */
1997 if (keep_memory)
1998 elf_section_data (o)->relocs = internal_relocs;
1999
2000 if (alloc1 != NULL)
2001 free (alloc1);
2002
2003 /* Don't free alloc2, since if it was allocated we are passing it
2004 back (under the name of internal_relocs). */
2005
2006 return internal_relocs;
2007
2008 error_return:
2009 if (alloc1 != NULL)
2010 free (alloc1);
2011 if (alloc2 != NULL)
2012 free (alloc2);
2013 return NULL;
2014 }
2015
2016 /* Compute the size of, and allocate space for, REL_HDR which is the
2017 section header for a section containing relocations for O. */
2018
2019 bfd_boolean
2020 _bfd_elf_link_size_reloc_section (bfd *abfd,
2021 Elf_Internal_Shdr *rel_hdr,
2022 asection *o)
2023 {
2024 bfd_size_type reloc_count;
2025 bfd_size_type num_rel_hashes;
2026
2027 /* Figure out how many relocations there will be. */
2028 if (rel_hdr == &elf_section_data (o)->rel_hdr)
2029 reloc_count = elf_section_data (o)->rel_count;
2030 else
2031 reloc_count = elf_section_data (o)->rel_count2;
2032
2033 num_rel_hashes = o->reloc_count;
2034 if (num_rel_hashes < reloc_count)
2035 num_rel_hashes = reloc_count;
2036
2037 /* That allows us to calculate the size of the section. */
2038 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
2039
2040 /* The contents field must last into write_object_contents, so we
2041 allocate it with bfd_alloc rather than malloc. Also since we
2042 cannot be sure that the contents will actually be filled in,
2043 we zero the allocated space. */
2044 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
2045 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2046 return FALSE;
2047
2048 /* We only allocate one set of hash entries, so we only do it the
2049 first time we are called. */
2050 if (elf_section_data (o)->rel_hashes == NULL
2051 && num_rel_hashes)
2052 {
2053 struct elf_link_hash_entry **p;
2054
2055 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
2056 if (p == NULL)
2057 return FALSE;
2058
2059 elf_section_data (o)->rel_hashes = p;
2060 }
2061
2062 return TRUE;
2063 }
2064
2065 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2066 originated from the section given by INPUT_REL_HDR) to the
2067 OUTPUT_BFD. */
2068
2069 bfd_boolean
2070 _bfd_elf_link_output_relocs (bfd *output_bfd,
2071 asection *input_section,
2072 Elf_Internal_Shdr *input_rel_hdr,
2073 Elf_Internal_Rela *internal_relocs)
2074 {
2075 Elf_Internal_Rela *irela;
2076 Elf_Internal_Rela *irelaend;
2077 bfd_byte *erel;
2078 Elf_Internal_Shdr *output_rel_hdr;
2079 asection *output_section;
2080 unsigned int *rel_countp = NULL;
2081 const struct elf_backend_data *bed;
2082 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2083
2084 output_section = input_section->output_section;
2085 output_rel_hdr = NULL;
2086
2087 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2088 == input_rel_hdr->sh_entsize)
2089 {
2090 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2091 rel_countp = &elf_section_data (output_section)->rel_count;
2092 }
2093 else if (elf_section_data (output_section)->rel_hdr2
2094 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2095 == input_rel_hdr->sh_entsize))
2096 {
2097 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2098 rel_countp = &elf_section_data (output_section)->rel_count2;
2099 }
2100 else
2101 {
2102 (*_bfd_error_handler)
2103 (_("%B: relocation size mismatch in %B section %A"),
2104 output_bfd, input_section->owner, input_section);
2105 bfd_set_error (bfd_error_wrong_object_format);
2106 return FALSE;
2107 }
2108
2109 bed = get_elf_backend_data (output_bfd);
2110 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2111 swap_out = bed->s->swap_reloc_out;
2112 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2113 swap_out = bed->s->swap_reloca_out;
2114 else
2115 abort ();
2116
2117 erel = output_rel_hdr->contents;
2118 erel += *rel_countp * input_rel_hdr->sh_entsize;
2119 irela = internal_relocs;
2120 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2121 * bed->s->int_rels_per_ext_rel);
2122 while (irela < irelaend)
2123 {
2124 (*swap_out) (output_bfd, irela, erel);
2125 irela += bed->s->int_rels_per_ext_rel;
2126 erel += input_rel_hdr->sh_entsize;
2127 }
2128
2129 /* Bump the counter, so that we know where to add the next set of
2130 relocations. */
2131 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2132
2133 return TRUE;
2134 }
2135 \f
2136 /* Fix up the flags for a symbol. This handles various cases which
2137 can only be fixed after all the input files are seen. This is
2138 currently called by both adjust_dynamic_symbol and
2139 assign_sym_version, which is unnecessary but perhaps more robust in
2140 the face of future changes. */
2141
2142 bfd_boolean
2143 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2144 struct elf_info_failed *eif)
2145 {
2146 /* If this symbol was mentioned in a non-ELF file, try to set
2147 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2148 permit a non-ELF file to correctly refer to a symbol defined in
2149 an ELF dynamic object. */
2150 if (h->non_elf)
2151 {
2152 while (h->root.type == bfd_link_hash_indirect)
2153 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2154
2155 if (h->root.type != bfd_link_hash_defined
2156 && h->root.type != bfd_link_hash_defweak)
2157 {
2158 h->ref_regular = 1;
2159 h->ref_regular_nonweak = 1;
2160 }
2161 else
2162 {
2163 if (h->root.u.def.section->owner != NULL
2164 && (bfd_get_flavour (h->root.u.def.section->owner)
2165 == bfd_target_elf_flavour))
2166 {
2167 h->ref_regular = 1;
2168 h->ref_regular_nonweak = 1;
2169 }
2170 else
2171 h->def_regular = 1;
2172 }
2173
2174 if (h->dynindx == -1
2175 && (h->def_dynamic
2176 || h->ref_dynamic))
2177 {
2178 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2179 {
2180 eif->failed = TRUE;
2181 return FALSE;
2182 }
2183 }
2184 }
2185 else
2186 {
2187 /* Unfortunately, NON_ELF is only correct if the symbol
2188 was first seen in a non-ELF file. Fortunately, if the symbol
2189 was first seen in an ELF file, we're probably OK unless the
2190 symbol was defined in a non-ELF file. Catch that case here.
2191 FIXME: We're still in trouble if the symbol was first seen in
2192 a dynamic object, and then later in a non-ELF regular object. */
2193 if ((h->root.type == bfd_link_hash_defined
2194 || h->root.type == bfd_link_hash_defweak)
2195 && !h->def_regular
2196 && (h->root.u.def.section->owner != NULL
2197 ? (bfd_get_flavour (h->root.u.def.section->owner)
2198 != bfd_target_elf_flavour)
2199 : (bfd_is_abs_section (h->root.u.def.section)
2200 && !h->def_dynamic)))
2201 h->def_regular = 1;
2202 }
2203
2204 /* If this is a final link, and the symbol was defined as a common
2205 symbol in a regular object file, and there was no definition in
2206 any dynamic object, then the linker will have allocated space for
2207 the symbol in a common section but the DEF_REGULAR
2208 flag will not have been set. */
2209 if (h->root.type == bfd_link_hash_defined
2210 && !h->def_regular
2211 && h->ref_regular
2212 && !h->def_dynamic
2213 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2214 h->def_regular = 1;
2215
2216 /* If -Bsymbolic was used (which means to bind references to global
2217 symbols to the definition within the shared object), and this
2218 symbol was defined in a regular object, then it actually doesn't
2219 need a PLT entry. Likewise, if the symbol has non-default
2220 visibility. If the symbol has hidden or internal visibility, we
2221 will force it local. */
2222 if (h->needs_plt
2223 && eif->info->shared
2224 && is_elf_hash_table (eif->info->hash)
2225 && (eif->info->symbolic
2226 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2227 && h->def_regular)
2228 {
2229 const struct elf_backend_data *bed;
2230 bfd_boolean force_local;
2231
2232 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2233
2234 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2235 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2236 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2237 }
2238
2239 /* If a weak undefined symbol has non-default visibility, we also
2240 hide it from the dynamic linker. */
2241 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2242 && h->root.type == bfd_link_hash_undefweak)
2243 {
2244 const struct elf_backend_data *bed;
2245 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2246 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2247 }
2248
2249 /* If this is a weak defined symbol in a dynamic object, and we know
2250 the real definition in the dynamic object, copy interesting flags
2251 over to the real definition. */
2252 if (h->u.weakdef != NULL)
2253 {
2254 struct elf_link_hash_entry *weakdef;
2255
2256 weakdef = h->u.weakdef;
2257 if (h->root.type == bfd_link_hash_indirect)
2258 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2259
2260 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2261 || h->root.type == bfd_link_hash_defweak);
2262 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2263 || weakdef->root.type == bfd_link_hash_defweak);
2264 BFD_ASSERT (weakdef->def_dynamic);
2265
2266 /* If the real definition is defined by a regular object file,
2267 don't do anything special. See the longer description in
2268 _bfd_elf_adjust_dynamic_symbol, below. */
2269 if (weakdef->def_regular)
2270 h->u.weakdef = NULL;
2271 else
2272 {
2273 const struct elf_backend_data *bed;
2274
2275 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2276 (*bed->elf_backend_copy_indirect_symbol) (bed, weakdef, h);
2277 }
2278 }
2279
2280 return TRUE;
2281 }
2282
2283 /* Make the backend pick a good value for a dynamic symbol. This is
2284 called via elf_link_hash_traverse, and also calls itself
2285 recursively. */
2286
2287 bfd_boolean
2288 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2289 {
2290 struct elf_info_failed *eif = data;
2291 bfd *dynobj;
2292 const struct elf_backend_data *bed;
2293
2294 if (! is_elf_hash_table (eif->info->hash))
2295 return FALSE;
2296
2297 if (h->root.type == bfd_link_hash_warning)
2298 {
2299 h->plt = elf_hash_table (eif->info)->init_offset;
2300 h->got = elf_hash_table (eif->info)->init_offset;
2301
2302 /* When warning symbols are created, they **replace** the "real"
2303 entry in the hash table, thus we never get to see the real
2304 symbol in a hash traversal. So look at it now. */
2305 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2306 }
2307
2308 /* Ignore indirect symbols. These are added by the versioning code. */
2309 if (h->root.type == bfd_link_hash_indirect)
2310 return TRUE;
2311
2312 /* Fix the symbol flags. */
2313 if (! _bfd_elf_fix_symbol_flags (h, eif))
2314 return FALSE;
2315
2316 /* If this symbol does not require a PLT entry, and it is not
2317 defined by a dynamic object, or is not referenced by a regular
2318 object, ignore it. We do have to handle a weak defined symbol,
2319 even if no regular object refers to it, if we decided to add it
2320 to the dynamic symbol table. FIXME: Do we normally need to worry
2321 about symbols which are defined by one dynamic object and
2322 referenced by another one? */
2323 if (!h->needs_plt
2324 && (h->def_regular
2325 || !h->def_dynamic
2326 || (!h->ref_regular
2327 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2328 {
2329 h->plt = elf_hash_table (eif->info)->init_offset;
2330 return TRUE;
2331 }
2332
2333 /* If we've already adjusted this symbol, don't do it again. This
2334 can happen via a recursive call. */
2335 if (h->dynamic_adjusted)
2336 return TRUE;
2337
2338 /* Don't look at this symbol again. Note that we must set this
2339 after checking the above conditions, because we may look at a
2340 symbol once, decide not to do anything, and then get called
2341 recursively later after REF_REGULAR is set below. */
2342 h->dynamic_adjusted = 1;
2343
2344 /* If this is a weak definition, and we know a real definition, and
2345 the real symbol is not itself defined by a regular object file,
2346 then get a good value for the real definition. We handle the
2347 real symbol first, for the convenience of the backend routine.
2348
2349 Note that there is a confusing case here. If the real definition
2350 is defined by a regular object file, we don't get the real symbol
2351 from the dynamic object, but we do get the weak symbol. If the
2352 processor backend uses a COPY reloc, then if some routine in the
2353 dynamic object changes the real symbol, we will not see that
2354 change in the corresponding weak symbol. This is the way other
2355 ELF linkers work as well, and seems to be a result of the shared
2356 library model.
2357
2358 I will clarify this issue. Most SVR4 shared libraries define the
2359 variable _timezone and define timezone as a weak synonym. The
2360 tzset call changes _timezone. If you write
2361 extern int timezone;
2362 int _timezone = 5;
2363 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2364 you might expect that, since timezone is a synonym for _timezone,
2365 the same number will print both times. However, if the processor
2366 backend uses a COPY reloc, then actually timezone will be copied
2367 into your process image, and, since you define _timezone
2368 yourself, _timezone will not. Thus timezone and _timezone will
2369 wind up at different memory locations. The tzset call will set
2370 _timezone, leaving timezone unchanged. */
2371
2372 if (h->u.weakdef != NULL)
2373 {
2374 /* If we get to this point, we know there is an implicit
2375 reference by a regular object file via the weak symbol H.
2376 FIXME: Is this really true? What if the traversal finds
2377 H->U.WEAKDEF before it finds H? */
2378 h->u.weakdef->ref_regular = 1;
2379
2380 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2381 return FALSE;
2382 }
2383
2384 /* If a symbol has no type and no size and does not require a PLT
2385 entry, then we are probably about to do the wrong thing here: we
2386 are probably going to create a COPY reloc for an empty object.
2387 This case can arise when a shared object is built with assembly
2388 code, and the assembly code fails to set the symbol type. */
2389 if (h->size == 0
2390 && h->type == STT_NOTYPE
2391 && !h->needs_plt)
2392 (*_bfd_error_handler)
2393 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2394 h->root.root.string);
2395
2396 dynobj = elf_hash_table (eif->info)->dynobj;
2397 bed = get_elf_backend_data (dynobj);
2398 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2399 {
2400 eif->failed = TRUE;
2401 return FALSE;
2402 }
2403
2404 return TRUE;
2405 }
2406
2407 /* Adjust all external symbols pointing into SEC_MERGE sections
2408 to reflect the object merging within the sections. */
2409
2410 bfd_boolean
2411 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2412 {
2413 asection *sec;
2414
2415 if (h->root.type == bfd_link_hash_warning)
2416 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2417
2418 if ((h->root.type == bfd_link_hash_defined
2419 || h->root.type == bfd_link_hash_defweak)
2420 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2421 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2422 {
2423 bfd *output_bfd = data;
2424
2425 h->root.u.def.value =
2426 _bfd_merged_section_offset (output_bfd,
2427 &h->root.u.def.section,
2428 elf_section_data (sec)->sec_info,
2429 h->root.u.def.value);
2430 }
2431
2432 return TRUE;
2433 }
2434
2435 /* Returns false if the symbol referred to by H should be considered
2436 to resolve local to the current module, and true if it should be
2437 considered to bind dynamically. */
2438
2439 bfd_boolean
2440 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2441 struct bfd_link_info *info,
2442 bfd_boolean ignore_protected)
2443 {
2444 bfd_boolean binding_stays_local_p;
2445
2446 if (h == NULL)
2447 return FALSE;
2448
2449 while (h->root.type == bfd_link_hash_indirect
2450 || h->root.type == bfd_link_hash_warning)
2451 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2452
2453 /* If it was forced local, then clearly it's not dynamic. */
2454 if (h->dynindx == -1)
2455 return FALSE;
2456 if (h->forced_local)
2457 return FALSE;
2458
2459 /* Identify the cases where name binding rules say that a
2460 visible symbol resolves locally. */
2461 binding_stays_local_p = info->executable || info->symbolic;
2462
2463 switch (ELF_ST_VISIBILITY (h->other))
2464 {
2465 case STV_INTERNAL:
2466 case STV_HIDDEN:
2467 return FALSE;
2468
2469 case STV_PROTECTED:
2470 /* Proper resolution for function pointer equality may require
2471 that these symbols perhaps be resolved dynamically, even though
2472 we should be resolving them to the current module. */
2473 if (!ignore_protected)
2474 binding_stays_local_p = TRUE;
2475 break;
2476
2477 default:
2478 break;
2479 }
2480
2481 /* If it isn't defined locally, then clearly it's dynamic. */
2482 if (!h->def_regular)
2483 return TRUE;
2484
2485 /* Otherwise, the symbol is dynamic if binding rules don't tell
2486 us that it remains local. */
2487 return !binding_stays_local_p;
2488 }
2489
2490 /* Return true if the symbol referred to by H should be considered
2491 to resolve local to the current module, and false otherwise. Differs
2492 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2493 undefined symbols and weak symbols. */
2494
2495 bfd_boolean
2496 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2497 struct bfd_link_info *info,
2498 bfd_boolean local_protected)
2499 {
2500 /* If it's a local sym, of course we resolve locally. */
2501 if (h == NULL)
2502 return TRUE;
2503
2504 /* Common symbols that become definitions don't get the DEF_REGULAR
2505 flag set, so test it first, and don't bail out. */
2506 if (ELF_COMMON_DEF_P (h))
2507 /* Do nothing. */;
2508 /* If we don't have a definition in a regular file, then we can't
2509 resolve locally. The sym is either undefined or dynamic. */
2510 else if (!h->def_regular)
2511 return FALSE;
2512
2513 /* Forced local symbols resolve locally. */
2514 if (h->forced_local)
2515 return TRUE;
2516
2517 /* As do non-dynamic symbols. */
2518 if (h->dynindx == -1)
2519 return TRUE;
2520
2521 /* At this point, we know the symbol is defined and dynamic. In an
2522 executable it must resolve locally, likewise when building symbolic
2523 shared libraries. */
2524 if (info->executable || info->symbolic)
2525 return TRUE;
2526
2527 /* Now deal with defined dynamic symbols in shared libraries. Ones
2528 with default visibility might not resolve locally. */
2529 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2530 return FALSE;
2531
2532 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2533 if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED)
2534 return TRUE;
2535
2536 /* Function pointer equality tests may require that STV_PROTECTED
2537 symbols be treated as dynamic symbols, even when we know that the
2538 dynamic linker will resolve them locally. */
2539 return local_protected;
2540 }
2541
2542 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2543 aligned. Returns the first TLS output section. */
2544
2545 struct bfd_section *
2546 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2547 {
2548 struct bfd_section *sec, *tls;
2549 unsigned int align = 0;
2550
2551 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2552 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2553 break;
2554 tls = sec;
2555
2556 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2557 if (sec->alignment_power > align)
2558 align = sec->alignment_power;
2559
2560 elf_hash_table (info)->tls_sec = tls;
2561
2562 /* Ensure the alignment of the first section is the largest alignment,
2563 so that the tls segment starts aligned. */
2564 if (tls != NULL)
2565 tls->alignment_power = align;
2566
2567 return tls;
2568 }
2569
2570 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2571 static bfd_boolean
2572 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2573 Elf_Internal_Sym *sym)
2574 {
2575 /* Local symbols do not count, but target specific ones might. */
2576 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2577 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2578 return FALSE;
2579
2580 /* Function symbols do not count. */
2581 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
2582 return FALSE;
2583
2584 /* If the section is undefined, then so is the symbol. */
2585 if (sym->st_shndx == SHN_UNDEF)
2586 return FALSE;
2587
2588 /* If the symbol is defined in the common section, then
2589 it is a common definition and so does not count. */
2590 if (sym->st_shndx == SHN_COMMON)
2591 return FALSE;
2592
2593 /* If the symbol is in a target specific section then we
2594 must rely upon the backend to tell us what it is. */
2595 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2596 /* FIXME - this function is not coded yet:
2597
2598 return _bfd_is_global_symbol_definition (abfd, sym);
2599
2600 Instead for now assume that the definition is not global,
2601 Even if this is wrong, at least the linker will behave
2602 in the same way that it used to do. */
2603 return FALSE;
2604
2605 return TRUE;
2606 }
2607
2608 /* Search the symbol table of the archive element of the archive ABFD
2609 whose archive map contains a mention of SYMDEF, and determine if
2610 the symbol is defined in this element. */
2611 static bfd_boolean
2612 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2613 {
2614 Elf_Internal_Shdr * hdr;
2615 bfd_size_type symcount;
2616 bfd_size_type extsymcount;
2617 bfd_size_type extsymoff;
2618 Elf_Internal_Sym *isymbuf;
2619 Elf_Internal_Sym *isym;
2620 Elf_Internal_Sym *isymend;
2621 bfd_boolean result;
2622
2623 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2624 if (abfd == NULL)
2625 return FALSE;
2626
2627 if (! bfd_check_format (abfd, bfd_object))
2628 return FALSE;
2629
2630 /* If we have already included the element containing this symbol in the
2631 link then we do not need to include it again. Just claim that any symbol
2632 it contains is not a definition, so that our caller will not decide to
2633 (re)include this element. */
2634 if (abfd->archive_pass)
2635 return FALSE;
2636
2637 /* Select the appropriate symbol table. */
2638 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2639 hdr = &elf_tdata (abfd)->symtab_hdr;
2640 else
2641 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2642
2643 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2644
2645 /* The sh_info field of the symtab header tells us where the
2646 external symbols start. We don't care about the local symbols. */
2647 if (elf_bad_symtab (abfd))
2648 {
2649 extsymcount = symcount;
2650 extsymoff = 0;
2651 }
2652 else
2653 {
2654 extsymcount = symcount - hdr->sh_info;
2655 extsymoff = hdr->sh_info;
2656 }
2657
2658 if (extsymcount == 0)
2659 return FALSE;
2660
2661 /* Read in the symbol table. */
2662 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2663 NULL, NULL, NULL);
2664 if (isymbuf == NULL)
2665 return FALSE;
2666
2667 /* Scan the symbol table looking for SYMDEF. */
2668 result = FALSE;
2669 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2670 {
2671 const char *name;
2672
2673 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
2674 isym->st_name);
2675 if (name == NULL)
2676 break;
2677
2678 if (strcmp (name, symdef->name) == 0)
2679 {
2680 result = is_global_data_symbol_definition (abfd, isym);
2681 break;
2682 }
2683 }
2684
2685 free (isymbuf);
2686
2687 return result;
2688 }
2689 \f
2690 /* Add an entry to the .dynamic table. */
2691
2692 bfd_boolean
2693 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
2694 bfd_vma tag,
2695 bfd_vma val)
2696 {
2697 struct elf_link_hash_table *hash_table;
2698 const struct elf_backend_data *bed;
2699 asection *s;
2700 bfd_size_type newsize;
2701 bfd_byte *newcontents;
2702 Elf_Internal_Dyn dyn;
2703
2704 hash_table = elf_hash_table (info);
2705 if (! is_elf_hash_table (hash_table))
2706 return FALSE;
2707
2708 if (info->warn_shared_textrel && info->shared && tag == DT_TEXTREL)
2709 _bfd_error_handler
2710 (_("warning: creating a DT_TEXTREL in a shared object."));
2711
2712 bed = get_elf_backend_data (hash_table->dynobj);
2713 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2714 BFD_ASSERT (s != NULL);
2715
2716 newsize = s->size + bed->s->sizeof_dyn;
2717 newcontents = bfd_realloc (s->contents, newsize);
2718 if (newcontents == NULL)
2719 return FALSE;
2720
2721 dyn.d_tag = tag;
2722 dyn.d_un.d_val = val;
2723 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
2724
2725 s->size = newsize;
2726 s->contents = newcontents;
2727
2728 return TRUE;
2729 }
2730
2731 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2732 otherwise just check whether one already exists. Returns -1 on error,
2733 1 if a DT_NEEDED tag already exists, and 0 on success. */
2734
2735 static int
2736 elf_add_dt_needed_tag (struct bfd_link_info *info,
2737 const char *soname,
2738 bfd_boolean do_it)
2739 {
2740 struct elf_link_hash_table *hash_table;
2741 bfd_size_type oldsize;
2742 bfd_size_type strindex;
2743
2744 hash_table = elf_hash_table (info);
2745 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2746 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
2747 if (strindex == (bfd_size_type) -1)
2748 return -1;
2749
2750 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
2751 {
2752 asection *sdyn;
2753 const struct elf_backend_data *bed;
2754 bfd_byte *extdyn;
2755
2756 bed = get_elf_backend_data (hash_table->dynobj);
2757 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2758 BFD_ASSERT (sdyn != NULL);
2759
2760 for (extdyn = sdyn->contents;
2761 extdyn < sdyn->contents + sdyn->size;
2762 extdyn += bed->s->sizeof_dyn)
2763 {
2764 Elf_Internal_Dyn dyn;
2765
2766 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
2767 if (dyn.d_tag == DT_NEEDED
2768 && dyn.d_un.d_val == strindex)
2769 {
2770 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2771 return 1;
2772 }
2773 }
2774 }
2775
2776 if (do_it)
2777 {
2778 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
2779 return -1;
2780 }
2781 else
2782 /* We were just checking for existence of the tag. */
2783 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2784
2785 return 0;
2786 }
2787
2788 /* Sort symbol by value and section. */
2789 static int
2790 elf_sort_symbol (const void *arg1, const void *arg2)
2791 {
2792 const struct elf_link_hash_entry *h1;
2793 const struct elf_link_hash_entry *h2;
2794 bfd_signed_vma vdiff;
2795
2796 h1 = *(const struct elf_link_hash_entry **) arg1;
2797 h2 = *(const struct elf_link_hash_entry **) arg2;
2798 vdiff = h1->root.u.def.value - h2->root.u.def.value;
2799 if (vdiff != 0)
2800 return vdiff > 0 ? 1 : -1;
2801 else
2802 {
2803 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
2804 if (sdiff != 0)
2805 return sdiff > 0 ? 1 : -1;
2806 }
2807 return 0;
2808 }
2809
2810 /* This function is used to adjust offsets into .dynstr for
2811 dynamic symbols. This is called via elf_link_hash_traverse. */
2812
2813 static bfd_boolean
2814 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
2815 {
2816 struct elf_strtab_hash *dynstr = data;
2817
2818 if (h->root.type == bfd_link_hash_warning)
2819 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2820
2821 if (h->dynindx != -1)
2822 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
2823 return TRUE;
2824 }
2825
2826 /* Assign string offsets in .dynstr, update all structures referencing
2827 them. */
2828
2829 static bfd_boolean
2830 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
2831 {
2832 struct elf_link_hash_table *hash_table = elf_hash_table (info);
2833 struct elf_link_local_dynamic_entry *entry;
2834 struct elf_strtab_hash *dynstr = hash_table->dynstr;
2835 bfd *dynobj = hash_table->dynobj;
2836 asection *sdyn;
2837 bfd_size_type size;
2838 const struct elf_backend_data *bed;
2839 bfd_byte *extdyn;
2840
2841 _bfd_elf_strtab_finalize (dynstr);
2842 size = _bfd_elf_strtab_size (dynstr);
2843
2844 bed = get_elf_backend_data (dynobj);
2845 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2846 BFD_ASSERT (sdyn != NULL);
2847
2848 /* Update all .dynamic entries referencing .dynstr strings. */
2849 for (extdyn = sdyn->contents;
2850 extdyn < sdyn->contents + sdyn->size;
2851 extdyn += bed->s->sizeof_dyn)
2852 {
2853 Elf_Internal_Dyn dyn;
2854
2855 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
2856 switch (dyn.d_tag)
2857 {
2858 case DT_STRSZ:
2859 dyn.d_un.d_val = size;
2860 break;
2861 case DT_NEEDED:
2862 case DT_SONAME:
2863 case DT_RPATH:
2864 case DT_RUNPATH:
2865 case DT_FILTER:
2866 case DT_AUXILIARY:
2867 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
2868 break;
2869 default:
2870 continue;
2871 }
2872 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
2873 }
2874
2875 /* Now update local dynamic symbols. */
2876 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
2877 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
2878 entry->isym.st_name);
2879
2880 /* And the rest of dynamic symbols. */
2881 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
2882
2883 /* Adjust version definitions. */
2884 if (elf_tdata (output_bfd)->cverdefs)
2885 {
2886 asection *s;
2887 bfd_byte *p;
2888 bfd_size_type i;
2889 Elf_Internal_Verdef def;
2890 Elf_Internal_Verdaux defaux;
2891
2892 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
2893 p = s->contents;
2894 do
2895 {
2896 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
2897 &def);
2898 p += sizeof (Elf_External_Verdef);
2899 if (def.vd_aux != sizeof (Elf_External_Verdef))
2900 continue;
2901 for (i = 0; i < def.vd_cnt; ++i)
2902 {
2903 _bfd_elf_swap_verdaux_in (output_bfd,
2904 (Elf_External_Verdaux *) p, &defaux);
2905 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
2906 defaux.vda_name);
2907 _bfd_elf_swap_verdaux_out (output_bfd,
2908 &defaux, (Elf_External_Verdaux *) p);
2909 p += sizeof (Elf_External_Verdaux);
2910 }
2911 }
2912 while (def.vd_next);
2913 }
2914
2915 /* Adjust version references. */
2916 if (elf_tdata (output_bfd)->verref)
2917 {
2918 asection *s;
2919 bfd_byte *p;
2920 bfd_size_type i;
2921 Elf_Internal_Verneed need;
2922 Elf_Internal_Vernaux needaux;
2923
2924 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
2925 p = s->contents;
2926 do
2927 {
2928 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
2929 &need);
2930 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
2931 _bfd_elf_swap_verneed_out (output_bfd, &need,
2932 (Elf_External_Verneed *) p);
2933 p += sizeof (Elf_External_Verneed);
2934 for (i = 0; i < need.vn_cnt; ++i)
2935 {
2936 _bfd_elf_swap_vernaux_in (output_bfd,
2937 (Elf_External_Vernaux *) p, &needaux);
2938 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
2939 needaux.vna_name);
2940 _bfd_elf_swap_vernaux_out (output_bfd,
2941 &needaux,
2942 (Elf_External_Vernaux *) p);
2943 p += sizeof (Elf_External_Vernaux);
2944 }
2945 }
2946 while (need.vn_next);
2947 }
2948
2949 return TRUE;
2950 }
2951 \f
2952 /* Add symbols from an ELF object file to the linker hash table. */
2953
2954 static bfd_boolean
2955 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
2956 {
2957 bfd_boolean (*add_symbol_hook)
2958 (bfd *, struct bfd_link_info *, Elf_Internal_Sym *,
2959 const char **, flagword *, asection **, bfd_vma *);
2960 bfd_boolean (*check_relocs)
2961 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
2962 bfd_boolean (*check_directives)
2963 (bfd *, struct bfd_link_info *);
2964 bfd_boolean collect;
2965 Elf_Internal_Shdr *hdr;
2966 bfd_size_type symcount;
2967 bfd_size_type extsymcount;
2968 bfd_size_type extsymoff;
2969 struct elf_link_hash_entry **sym_hash;
2970 bfd_boolean dynamic;
2971 Elf_External_Versym *extversym = NULL;
2972 Elf_External_Versym *ever;
2973 struct elf_link_hash_entry *weaks;
2974 struct elf_link_hash_entry **nondeflt_vers = NULL;
2975 bfd_size_type nondeflt_vers_cnt = 0;
2976 Elf_Internal_Sym *isymbuf = NULL;
2977 Elf_Internal_Sym *isym;
2978 Elf_Internal_Sym *isymend;
2979 const struct elf_backend_data *bed;
2980 bfd_boolean add_needed;
2981 struct elf_link_hash_table * hash_table;
2982 bfd_size_type amt;
2983
2984 hash_table = elf_hash_table (info);
2985
2986 bed = get_elf_backend_data (abfd);
2987 add_symbol_hook = bed->elf_add_symbol_hook;
2988 collect = bed->collect;
2989
2990 if ((abfd->flags & DYNAMIC) == 0)
2991 dynamic = FALSE;
2992 else
2993 {
2994 dynamic = TRUE;
2995
2996 /* You can't use -r against a dynamic object. Also, there's no
2997 hope of using a dynamic object which does not exactly match
2998 the format of the output file. */
2999 if (info->relocatable
3000 || !is_elf_hash_table (hash_table)
3001 || hash_table->root.creator != abfd->xvec)
3002 {
3003 if (info->relocatable)
3004 bfd_set_error (bfd_error_invalid_operation);
3005 else
3006 bfd_set_error (bfd_error_wrong_format);
3007 goto error_return;
3008 }
3009 }
3010
3011 /* As a GNU extension, any input sections which are named
3012 .gnu.warning.SYMBOL are treated as warning symbols for the given
3013 symbol. This differs from .gnu.warning sections, which generate
3014 warnings when they are included in an output file. */
3015 if (info->executable)
3016 {
3017 asection *s;
3018
3019 for (s = abfd->sections; s != NULL; s = s->next)
3020 {
3021 const char *name;
3022
3023 name = bfd_get_section_name (abfd, s);
3024 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
3025 {
3026 char *msg;
3027 bfd_size_type sz;
3028 bfd_size_type prefix_len;
3029 const char * gnu_warning_prefix = _("warning: ");
3030
3031 name += sizeof ".gnu.warning." - 1;
3032
3033 /* If this is a shared object, then look up the symbol
3034 in the hash table. If it is there, and it is already
3035 been defined, then we will not be using the entry
3036 from this shared object, so we don't need to warn.
3037 FIXME: If we see the definition in a regular object
3038 later on, we will warn, but we shouldn't. The only
3039 fix is to keep track of what warnings we are supposed
3040 to emit, and then handle them all at the end of the
3041 link. */
3042 if (dynamic)
3043 {
3044 struct elf_link_hash_entry *h;
3045
3046 h = elf_link_hash_lookup (hash_table, name,
3047 FALSE, FALSE, TRUE);
3048
3049 /* FIXME: What about bfd_link_hash_common? */
3050 if (h != NULL
3051 && (h->root.type == bfd_link_hash_defined
3052 || h->root.type == bfd_link_hash_defweak))
3053 {
3054 /* We don't want to issue this warning. Clobber
3055 the section size so that the warning does not
3056 get copied into the output file. */
3057 s->size = 0;
3058 continue;
3059 }
3060 }
3061
3062 sz = s->size;
3063 prefix_len = strlen (gnu_warning_prefix);
3064 msg = bfd_alloc (abfd, prefix_len + sz + 1);
3065 if (msg == NULL)
3066 goto error_return;
3067
3068 strcpy (msg, gnu_warning_prefix);
3069 if (! bfd_get_section_contents (abfd, s, msg + prefix_len, 0, sz))
3070 goto error_return;
3071
3072 msg[prefix_len + sz] = '\0';
3073
3074 if (! (_bfd_generic_link_add_one_symbol
3075 (info, abfd, name, BSF_WARNING, s, 0, msg,
3076 FALSE, collect, NULL)))
3077 goto error_return;
3078
3079 if (! info->relocatable)
3080 {
3081 /* Clobber the section size so that the warning does
3082 not get copied into the output file. */
3083 s->size = 0;
3084 }
3085 }
3086 }
3087 }
3088
3089 add_needed = TRUE;
3090 if (! dynamic)
3091 {
3092 /* If we are creating a shared library, create all the dynamic
3093 sections immediately. We need to attach them to something,
3094 so we attach them to this BFD, provided it is the right
3095 format. FIXME: If there are no input BFD's of the same
3096 format as the output, we can't make a shared library. */
3097 if (info->shared
3098 && is_elf_hash_table (hash_table)
3099 && hash_table->root.creator == abfd->xvec
3100 && ! hash_table->dynamic_sections_created)
3101 {
3102 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3103 goto error_return;
3104 }
3105 }
3106 else if (!is_elf_hash_table (hash_table))
3107 goto error_return;
3108 else
3109 {
3110 asection *s;
3111 const char *soname = NULL;
3112 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3113 int ret;
3114
3115 /* ld --just-symbols and dynamic objects don't mix very well.
3116 Test for --just-symbols by looking at info set up by
3117 _bfd_elf_link_just_syms. */
3118 if ((s = abfd->sections) != NULL
3119 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3120 goto error_return;
3121
3122 /* If this dynamic lib was specified on the command line with
3123 --as-needed in effect, then we don't want to add a DT_NEEDED
3124 tag unless the lib is actually used. Similary for libs brought
3125 in by another lib's DT_NEEDED. When --no-add-needed is used
3126 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3127 any dynamic library in DT_NEEDED tags in the dynamic lib at
3128 all. */
3129 add_needed = (elf_dyn_lib_class (abfd)
3130 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3131 | DYN_NO_NEEDED)) == 0;
3132
3133 s = bfd_get_section_by_name (abfd, ".dynamic");
3134 if (s != NULL)
3135 {
3136 bfd_byte *dynbuf;
3137 bfd_byte *extdyn;
3138 int elfsec;
3139 unsigned long shlink;
3140
3141 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3142 goto error_free_dyn;
3143
3144 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3145 if (elfsec == -1)
3146 goto error_free_dyn;
3147 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3148
3149 for (extdyn = dynbuf;
3150 extdyn < dynbuf + s->size;
3151 extdyn += bed->s->sizeof_dyn)
3152 {
3153 Elf_Internal_Dyn dyn;
3154
3155 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3156 if (dyn.d_tag == DT_SONAME)
3157 {
3158 unsigned int tagv = dyn.d_un.d_val;
3159 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3160 if (soname == NULL)
3161 goto error_free_dyn;
3162 }
3163 if (dyn.d_tag == DT_NEEDED)
3164 {
3165 struct bfd_link_needed_list *n, **pn;
3166 char *fnm, *anm;
3167 unsigned int tagv = dyn.d_un.d_val;
3168
3169 amt = sizeof (struct bfd_link_needed_list);
3170 n = bfd_alloc (abfd, amt);
3171 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3172 if (n == NULL || fnm == NULL)
3173 goto error_free_dyn;
3174 amt = strlen (fnm) + 1;
3175 anm = bfd_alloc (abfd, amt);
3176 if (anm == NULL)
3177 goto error_free_dyn;
3178 memcpy (anm, fnm, amt);
3179 n->name = anm;
3180 n->by = abfd;
3181 n->next = NULL;
3182 for (pn = & hash_table->needed;
3183 *pn != NULL;
3184 pn = &(*pn)->next)
3185 ;
3186 *pn = n;
3187 }
3188 if (dyn.d_tag == DT_RUNPATH)
3189 {
3190 struct bfd_link_needed_list *n, **pn;
3191 char *fnm, *anm;
3192 unsigned int tagv = dyn.d_un.d_val;
3193
3194 amt = sizeof (struct bfd_link_needed_list);
3195 n = bfd_alloc (abfd, amt);
3196 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3197 if (n == NULL || fnm == NULL)
3198 goto error_free_dyn;
3199 amt = strlen (fnm) + 1;
3200 anm = bfd_alloc (abfd, amt);
3201 if (anm == NULL)
3202 goto error_free_dyn;
3203 memcpy (anm, fnm, amt);
3204 n->name = anm;
3205 n->by = abfd;
3206 n->next = NULL;
3207 for (pn = & runpath;
3208 *pn != NULL;
3209 pn = &(*pn)->next)
3210 ;
3211 *pn = n;
3212 }
3213 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3214 if (!runpath && dyn.d_tag == DT_RPATH)
3215 {
3216 struct bfd_link_needed_list *n, **pn;
3217 char *fnm, *anm;
3218 unsigned int tagv = dyn.d_un.d_val;
3219
3220 amt = sizeof (struct bfd_link_needed_list);
3221 n = bfd_alloc (abfd, amt);
3222 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3223 if (n == NULL || fnm == NULL)
3224 goto error_free_dyn;
3225 amt = strlen (fnm) + 1;
3226 anm = bfd_alloc (abfd, amt);
3227 if (anm == NULL)
3228 {
3229 error_free_dyn:
3230 free (dynbuf);
3231 goto error_return;
3232 }
3233 memcpy (anm, fnm, amt);
3234 n->name = anm;
3235 n->by = abfd;
3236 n->next = NULL;
3237 for (pn = & rpath;
3238 *pn != NULL;
3239 pn = &(*pn)->next)
3240 ;
3241 *pn = n;
3242 }
3243 }
3244
3245 free (dynbuf);
3246 }
3247
3248 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3249 frees all more recently bfd_alloc'd blocks as well. */
3250 if (runpath)
3251 rpath = runpath;
3252
3253 if (rpath)
3254 {
3255 struct bfd_link_needed_list **pn;
3256 for (pn = & hash_table->runpath;
3257 *pn != NULL;
3258 pn = &(*pn)->next)
3259 ;
3260 *pn = rpath;
3261 }
3262
3263 /* We do not want to include any of the sections in a dynamic
3264 object in the output file. We hack by simply clobbering the
3265 list of sections in the BFD. This could be handled more
3266 cleanly by, say, a new section flag; the existing
3267 SEC_NEVER_LOAD flag is not the one we want, because that one
3268 still implies that the section takes up space in the output
3269 file. */
3270 bfd_section_list_clear (abfd);
3271
3272 /* If this is the first dynamic object found in the link, create
3273 the special sections required for dynamic linking. */
3274 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3275 goto error_return;
3276
3277 /* Find the name to use in a DT_NEEDED entry that refers to this
3278 object. If the object has a DT_SONAME entry, we use it.
3279 Otherwise, if the generic linker stuck something in
3280 elf_dt_name, we use that. Otherwise, we just use the file
3281 name. */
3282 if (soname == NULL || *soname == '\0')
3283 {
3284 soname = elf_dt_name (abfd);
3285 if (soname == NULL || *soname == '\0')
3286 soname = bfd_get_filename (abfd);
3287 }
3288
3289 /* Save the SONAME because sometimes the linker emulation code
3290 will need to know it. */
3291 elf_dt_name (abfd) = soname;
3292
3293 ret = elf_add_dt_needed_tag (info, soname, add_needed);
3294 if (ret < 0)
3295 goto error_return;
3296
3297 /* If we have already included this dynamic object in the
3298 link, just ignore it. There is no reason to include a
3299 particular dynamic object more than once. */
3300 if (ret > 0)
3301 return TRUE;
3302 }
3303
3304 /* If this is a dynamic object, we always link against the .dynsym
3305 symbol table, not the .symtab symbol table. The dynamic linker
3306 will only see the .dynsym symbol table, so there is no reason to
3307 look at .symtab for a dynamic object. */
3308
3309 if (! dynamic || elf_dynsymtab (abfd) == 0)
3310 hdr = &elf_tdata (abfd)->symtab_hdr;
3311 else
3312 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3313
3314 symcount = hdr->sh_size / bed->s->sizeof_sym;
3315
3316 /* The sh_info field of the symtab header tells us where the
3317 external symbols start. We don't care about the local symbols at
3318 this point. */
3319 if (elf_bad_symtab (abfd))
3320 {
3321 extsymcount = symcount;
3322 extsymoff = 0;
3323 }
3324 else
3325 {
3326 extsymcount = symcount - hdr->sh_info;
3327 extsymoff = hdr->sh_info;
3328 }
3329
3330 sym_hash = NULL;
3331 if (extsymcount != 0)
3332 {
3333 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3334 NULL, NULL, NULL);
3335 if (isymbuf == NULL)
3336 goto error_return;
3337
3338 /* We store a pointer to the hash table entry for each external
3339 symbol. */
3340 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3341 sym_hash = bfd_alloc (abfd, amt);
3342 if (sym_hash == NULL)
3343 goto error_free_sym;
3344 elf_sym_hashes (abfd) = sym_hash;
3345 }
3346
3347 if (dynamic)
3348 {
3349 /* Read in any version definitions. */
3350 if (!_bfd_elf_slurp_version_tables (abfd,
3351 info->default_imported_symver))
3352 goto error_free_sym;
3353
3354 /* Read in the symbol versions, but don't bother to convert them
3355 to internal format. */
3356 if (elf_dynversym (abfd) != 0)
3357 {
3358 Elf_Internal_Shdr *versymhdr;
3359
3360 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3361 extversym = bfd_malloc (versymhdr->sh_size);
3362 if (extversym == NULL)
3363 goto error_free_sym;
3364 amt = versymhdr->sh_size;
3365 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3366 || bfd_bread (extversym, amt, abfd) != amt)
3367 goto error_free_vers;
3368 }
3369 }
3370
3371 weaks = NULL;
3372
3373 ever = extversym != NULL ? extversym + extsymoff : NULL;
3374 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3375 isym < isymend;
3376 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3377 {
3378 int bind;
3379 bfd_vma value;
3380 asection *sec;
3381 flagword flags;
3382 const char *name;
3383 struct elf_link_hash_entry *h;
3384 bfd_boolean definition;
3385 bfd_boolean size_change_ok;
3386 bfd_boolean type_change_ok;
3387 bfd_boolean new_weakdef;
3388 bfd_boolean override;
3389 unsigned int old_alignment;
3390 bfd *old_bfd;
3391
3392 override = FALSE;
3393
3394 flags = BSF_NO_FLAGS;
3395 sec = NULL;
3396 value = isym->st_value;
3397 *sym_hash = NULL;
3398
3399 bind = ELF_ST_BIND (isym->st_info);
3400 if (bind == STB_LOCAL)
3401 {
3402 /* This should be impossible, since ELF requires that all
3403 global symbols follow all local symbols, and that sh_info
3404 point to the first global symbol. Unfortunately, Irix 5
3405 screws this up. */
3406 continue;
3407 }
3408 else if (bind == STB_GLOBAL)
3409 {
3410 if (isym->st_shndx != SHN_UNDEF
3411 && isym->st_shndx != SHN_COMMON)
3412 flags = BSF_GLOBAL;
3413 }
3414 else if (bind == STB_WEAK)
3415 flags = BSF_WEAK;
3416 else
3417 {
3418 /* Leave it up to the processor backend. */
3419 }
3420
3421 if (isym->st_shndx == SHN_UNDEF)
3422 sec = bfd_und_section_ptr;
3423 else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
3424 {
3425 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3426 if (sec == NULL)
3427 sec = bfd_abs_section_ptr;
3428 else if (sec->kept_section)
3429 {
3430 /* Symbols from discarded section are undefined. */
3431 sec = bfd_und_section_ptr;
3432 isym->st_shndx = SHN_UNDEF;
3433 }
3434 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3435 value -= sec->vma;
3436 }
3437 else if (isym->st_shndx == SHN_ABS)
3438 sec = bfd_abs_section_ptr;
3439 else if (isym->st_shndx == SHN_COMMON)
3440 {
3441 sec = bfd_com_section_ptr;
3442 /* What ELF calls the size we call the value. What ELF
3443 calls the value we call the alignment. */
3444 value = isym->st_size;
3445 }
3446 else
3447 {
3448 /* Leave it up to the processor backend. */
3449 }
3450
3451 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3452 isym->st_name);
3453 if (name == NULL)
3454 goto error_free_vers;
3455
3456 if (isym->st_shndx == SHN_COMMON
3457 && ELF_ST_TYPE (isym->st_info) == STT_TLS)
3458 {
3459 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3460
3461 if (tcomm == NULL)
3462 {
3463 tcomm = bfd_make_section (abfd, ".tcommon");
3464 if (tcomm == NULL
3465 || !bfd_set_section_flags (abfd, tcomm, (SEC_ALLOC
3466 | SEC_IS_COMMON
3467 | SEC_LINKER_CREATED
3468 | SEC_THREAD_LOCAL)))
3469 goto error_free_vers;
3470 }
3471 sec = tcomm;
3472 }
3473 else if (add_symbol_hook)
3474 {
3475 if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec,
3476 &value))
3477 goto error_free_vers;
3478
3479 /* The hook function sets the name to NULL if this symbol
3480 should be skipped for some reason. */
3481 if (name == NULL)
3482 continue;
3483 }
3484
3485 /* Sanity check that all possibilities were handled. */
3486 if (sec == NULL)
3487 {
3488 bfd_set_error (bfd_error_bad_value);
3489 goto error_free_vers;
3490 }
3491
3492 if (bfd_is_und_section (sec)
3493 || bfd_is_com_section (sec))
3494 definition = FALSE;
3495 else
3496 definition = TRUE;
3497
3498 size_change_ok = FALSE;
3499 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
3500 old_alignment = 0;
3501 old_bfd = NULL;
3502
3503 if (is_elf_hash_table (hash_table))
3504 {
3505 Elf_Internal_Versym iver;
3506 unsigned int vernum = 0;
3507 bfd_boolean skip;
3508
3509 if (ever == NULL)
3510 {
3511 if (info->default_imported_symver)
3512 /* Use the default symbol version created earlier. */
3513 iver.vs_vers = elf_tdata (abfd)->cverdefs;
3514 else
3515 iver.vs_vers = 0;
3516 }
3517 else
3518 _bfd_elf_swap_versym_in (abfd, ever, &iver);
3519
3520 vernum = iver.vs_vers & VERSYM_VERSION;
3521
3522 /* If this is a hidden symbol, or if it is not version
3523 1, we append the version name to the symbol name.
3524 However, we do not modify a non-hidden absolute
3525 symbol, because it might be the version symbol
3526 itself. FIXME: What if it isn't? */
3527 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
3528 || (vernum > 1 && ! bfd_is_abs_section (sec)))
3529 {
3530 const char *verstr;
3531 size_t namelen, verlen, newlen;
3532 char *newname, *p;
3533
3534 if (isym->st_shndx != SHN_UNDEF)
3535 {
3536 if (vernum > elf_tdata (abfd)->cverdefs)
3537 verstr = NULL;
3538 else if (vernum > 1)
3539 verstr =
3540 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
3541 else
3542 verstr = "";
3543
3544 if (verstr == NULL)
3545 {
3546 (*_bfd_error_handler)
3547 (_("%B: %s: invalid version %u (max %d)"),
3548 abfd, name, vernum,
3549 elf_tdata (abfd)->cverdefs);
3550 bfd_set_error (bfd_error_bad_value);
3551 goto error_free_vers;
3552 }
3553 }
3554 else
3555 {
3556 /* We cannot simply test for the number of
3557 entries in the VERNEED section since the
3558 numbers for the needed versions do not start
3559 at 0. */
3560 Elf_Internal_Verneed *t;
3561
3562 verstr = NULL;
3563 for (t = elf_tdata (abfd)->verref;
3564 t != NULL;
3565 t = t->vn_nextref)
3566 {
3567 Elf_Internal_Vernaux *a;
3568
3569 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3570 {
3571 if (a->vna_other == vernum)
3572 {
3573 verstr = a->vna_nodename;
3574 break;
3575 }
3576 }
3577 if (a != NULL)
3578 break;
3579 }
3580 if (verstr == NULL)
3581 {
3582 (*_bfd_error_handler)
3583 (_("%B: %s: invalid needed version %d"),
3584 abfd, name, vernum);
3585 bfd_set_error (bfd_error_bad_value);
3586 goto error_free_vers;
3587 }
3588 }
3589
3590 namelen = strlen (name);
3591 verlen = strlen (verstr);
3592 newlen = namelen + verlen + 2;
3593 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3594 && isym->st_shndx != SHN_UNDEF)
3595 ++newlen;
3596
3597 newname = bfd_alloc (abfd, newlen);
3598 if (newname == NULL)
3599 goto error_free_vers;
3600 memcpy (newname, name, namelen);
3601 p = newname + namelen;
3602 *p++ = ELF_VER_CHR;
3603 /* If this is a defined non-hidden version symbol,
3604 we add another @ to the name. This indicates the
3605 default version of the symbol. */
3606 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3607 && isym->st_shndx != SHN_UNDEF)
3608 *p++ = ELF_VER_CHR;
3609 memcpy (p, verstr, verlen + 1);
3610
3611 name = newname;
3612 }
3613
3614 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
3615 sym_hash, &skip, &override,
3616 &type_change_ok, &size_change_ok))
3617 goto error_free_vers;
3618
3619 if (skip)
3620 continue;
3621
3622 if (override)
3623 definition = FALSE;
3624
3625 h = *sym_hash;
3626 while (h->root.type == bfd_link_hash_indirect
3627 || h->root.type == bfd_link_hash_warning)
3628 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3629
3630 /* Remember the old alignment if this is a common symbol, so
3631 that we don't reduce the alignment later on. We can't
3632 check later, because _bfd_generic_link_add_one_symbol
3633 will set a default for the alignment which we want to
3634 override. We also remember the old bfd where the existing
3635 definition comes from. */
3636 switch (h->root.type)
3637 {
3638 default:
3639 break;
3640
3641 case bfd_link_hash_defined:
3642 case bfd_link_hash_defweak:
3643 old_bfd = h->root.u.def.section->owner;
3644 break;
3645
3646 case bfd_link_hash_common:
3647 old_bfd = h->root.u.c.p->section->owner;
3648 old_alignment = h->root.u.c.p->alignment_power;
3649 break;
3650 }
3651
3652 if (elf_tdata (abfd)->verdef != NULL
3653 && ! override
3654 && vernum > 1
3655 && definition)
3656 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
3657 }
3658
3659 if (! (_bfd_generic_link_add_one_symbol
3660 (info, abfd, name, flags, sec, value, NULL, FALSE, collect,
3661 (struct bfd_link_hash_entry **) sym_hash)))
3662 goto error_free_vers;
3663
3664 h = *sym_hash;
3665 while (h->root.type == bfd_link_hash_indirect
3666 || h->root.type == bfd_link_hash_warning)
3667 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3668 *sym_hash = h;
3669
3670 new_weakdef = FALSE;
3671 if (dynamic
3672 && definition
3673 && (flags & BSF_WEAK) != 0
3674 && ELF_ST_TYPE (isym->st_info) != STT_FUNC
3675 && is_elf_hash_table (hash_table)
3676 && h->u.weakdef == NULL)
3677 {
3678 /* Keep a list of all weak defined non function symbols from
3679 a dynamic object, using the weakdef field. Later in this
3680 function we will set the weakdef field to the correct
3681 value. We only put non-function symbols from dynamic
3682 objects on this list, because that happens to be the only
3683 time we need to know the normal symbol corresponding to a
3684 weak symbol, and the information is time consuming to
3685 figure out. If the weakdef field is not already NULL,
3686 then this symbol was already defined by some previous
3687 dynamic object, and we will be using that previous
3688 definition anyhow. */
3689
3690 h->u.weakdef = weaks;
3691 weaks = h;
3692 new_weakdef = TRUE;
3693 }
3694
3695 /* Set the alignment of a common symbol. */
3696 if (isym->st_shndx == SHN_COMMON
3697 && h->root.type == bfd_link_hash_common)
3698 {
3699 unsigned int align;
3700
3701 align = bfd_log2 (isym->st_value);
3702 if (align > old_alignment
3703 /* Permit an alignment power of zero if an alignment of one
3704 is specified and no other alignments have been specified. */
3705 || (isym->st_value == 1 && old_alignment == 0))
3706 h->root.u.c.p->alignment_power = align;
3707 else
3708 h->root.u.c.p->alignment_power = old_alignment;
3709 }
3710
3711 if (is_elf_hash_table (hash_table))
3712 {
3713 bfd_boolean dynsym;
3714
3715 /* Check the alignment when a common symbol is involved. This
3716 can change when a common symbol is overridden by a normal
3717 definition or a common symbol is ignored due to the old
3718 normal definition. We need to make sure the maximum
3719 alignment is maintained. */
3720 if ((old_alignment || isym->st_shndx == SHN_COMMON)
3721 && h->root.type != bfd_link_hash_common)
3722 {
3723 unsigned int common_align;
3724 unsigned int normal_align;
3725 unsigned int symbol_align;
3726 bfd *normal_bfd;
3727 bfd *common_bfd;
3728
3729 symbol_align = ffs (h->root.u.def.value) - 1;
3730 if (h->root.u.def.section->owner != NULL
3731 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3732 {
3733 normal_align = h->root.u.def.section->alignment_power;
3734 if (normal_align > symbol_align)
3735 normal_align = symbol_align;
3736 }
3737 else
3738 normal_align = symbol_align;
3739
3740 if (old_alignment)
3741 {
3742 common_align = old_alignment;
3743 common_bfd = old_bfd;
3744 normal_bfd = abfd;
3745 }
3746 else
3747 {
3748 common_align = bfd_log2 (isym->st_value);
3749 common_bfd = abfd;
3750 normal_bfd = old_bfd;
3751 }
3752
3753 if (normal_align < common_align)
3754 (*_bfd_error_handler)
3755 (_("Warning: alignment %u of symbol `%s' in %B"
3756 " is smaller than %u in %B"),
3757 normal_bfd, common_bfd,
3758 1 << normal_align, name, 1 << common_align);
3759 }
3760
3761 /* Remember the symbol size and type. */
3762 if (isym->st_size != 0
3763 && (definition || h->size == 0))
3764 {
3765 if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
3766 (*_bfd_error_handler)
3767 (_("Warning: size of symbol `%s' changed"
3768 " from %lu in %B to %lu in %B"),
3769 old_bfd, abfd,
3770 name, (unsigned long) h->size,
3771 (unsigned long) isym->st_size);
3772
3773 h->size = isym->st_size;
3774 }
3775
3776 /* If this is a common symbol, then we always want H->SIZE
3777 to be the size of the common symbol. The code just above
3778 won't fix the size if a common symbol becomes larger. We
3779 don't warn about a size change here, because that is
3780 covered by --warn-common. */
3781 if (h->root.type == bfd_link_hash_common)
3782 h->size = h->root.u.c.size;
3783
3784 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
3785 && (definition || h->type == STT_NOTYPE))
3786 {
3787 if (h->type != STT_NOTYPE
3788 && h->type != ELF_ST_TYPE (isym->st_info)
3789 && ! type_change_ok)
3790 (*_bfd_error_handler)
3791 (_("Warning: type of symbol `%s' changed"
3792 " from %d to %d in %B"),
3793 abfd, name, h->type, ELF_ST_TYPE (isym->st_info));
3794
3795 h->type = ELF_ST_TYPE (isym->st_info);
3796 }
3797
3798 /* If st_other has a processor-specific meaning, specific
3799 code might be needed here. We never merge the visibility
3800 attribute with the one from a dynamic object. */
3801 if (bed->elf_backend_merge_symbol_attribute)
3802 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
3803 dynamic);
3804
3805 /* If this symbol has default visibility and the user has requested
3806 we not re-export it, then mark it as hidden. */
3807 if (definition && !dynamic
3808 && (abfd->no_export
3809 || (abfd->my_archive && abfd->my_archive->no_export))
3810 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
3811 isym->st_other = STV_HIDDEN | (isym->st_other & ~ ELF_ST_VISIBILITY (-1));
3812
3813 if (isym->st_other != 0 && !dynamic)
3814 {
3815 unsigned char hvis, symvis, other, nvis;
3816
3817 /* Take the balance of OTHER from the definition. */
3818 other = (definition ? isym->st_other : h->other);
3819 other &= ~ ELF_ST_VISIBILITY (-1);
3820
3821 /* Combine visibilities, using the most constraining one. */
3822 hvis = ELF_ST_VISIBILITY (h->other);
3823 symvis = ELF_ST_VISIBILITY (isym->st_other);
3824 if (! hvis)
3825 nvis = symvis;
3826 else if (! symvis)
3827 nvis = hvis;
3828 else
3829 nvis = hvis < symvis ? hvis : symvis;
3830
3831 h->other = other | nvis;
3832 }
3833
3834 /* Set a flag in the hash table entry indicating the type of
3835 reference or definition we just found. Keep a count of
3836 the number of dynamic symbols we find. A dynamic symbol
3837 is one which is referenced or defined by both a regular
3838 object and a shared object. */
3839 dynsym = FALSE;
3840 if (! dynamic)
3841 {
3842 if (! definition)
3843 {
3844 h->ref_regular = 1;
3845 if (bind != STB_WEAK)
3846 h->ref_regular_nonweak = 1;
3847 }
3848 else
3849 h->def_regular = 1;
3850 if (! info->executable
3851 || h->def_dynamic
3852 || h->ref_dynamic)
3853 dynsym = TRUE;
3854 }
3855 else
3856 {
3857 if (! definition)
3858 h->ref_dynamic = 1;
3859 else
3860 h->def_dynamic = 1;
3861 if (h->def_regular
3862 || h->ref_regular
3863 || (h->u.weakdef != NULL
3864 && ! new_weakdef
3865 && h->u.weakdef->dynindx != -1))
3866 dynsym = TRUE;
3867 }
3868
3869 /* Check to see if we need to add an indirect symbol for
3870 the default name. */
3871 if (definition || h->root.type == bfd_link_hash_common)
3872 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
3873 &sec, &value, &dynsym,
3874 override))
3875 goto error_free_vers;
3876
3877 if (definition && !dynamic)
3878 {
3879 char *p = strchr (name, ELF_VER_CHR);
3880 if (p != NULL && p[1] != ELF_VER_CHR)
3881 {
3882 /* Queue non-default versions so that .symver x, x@FOO
3883 aliases can be checked. */
3884 if (! nondeflt_vers)
3885 {
3886 amt = (isymend - isym + 1)
3887 * sizeof (struct elf_link_hash_entry *);
3888 nondeflt_vers = bfd_malloc (amt);
3889 }
3890 nondeflt_vers [nondeflt_vers_cnt++] = h;
3891 }
3892 }
3893
3894 if (dynsym && h->dynindx == -1)
3895 {
3896 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3897 goto error_free_vers;
3898 if (h->u.weakdef != NULL
3899 && ! new_weakdef
3900 && h->u.weakdef->dynindx == -1)
3901 {
3902 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
3903 goto error_free_vers;
3904 }
3905 }
3906 else if (dynsym && h->dynindx != -1)
3907 /* If the symbol already has a dynamic index, but
3908 visibility says it should not be visible, turn it into
3909 a local symbol. */
3910 switch (ELF_ST_VISIBILITY (h->other))
3911 {
3912 case STV_INTERNAL:
3913 case STV_HIDDEN:
3914 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
3915 dynsym = FALSE;
3916 break;
3917 }
3918
3919 if (!add_needed
3920 && definition
3921 && dynsym
3922 && h->ref_regular)
3923 {
3924 int ret;
3925 const char *soname = elf_dt_name (abfd);
3926
3927 /* A symbol from a library loaded via DT_NEEDED of some
3928 other library is referenced by a regular object.
3929 Add a DT_NEEDED entry for it. Issue an error if
3930 --no-add-needed is used. */
3931 if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
3932 {
3933 (*_bfd_error_handler)
3934 (_("%s: invalid DSO for symbol `%s' definition"),
3935 abfd, name);
3936 bfd_set_error (bfd_error_bad_value);
3937 goto error_free_vers;
3938 }
3939
3940 add_needed = TRUE;
3941 ret = elf_add_dt_needed_tag (info, soname, add_needed);
3942 if (ret < 0)
3943 goto error_free_vers;
3944
3945 BFD_ASSERT (ret == 0);
3946 }
3947 }
3948 }
3949
3950 /* Now that all the symbols from this input file are created, handle
3951 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
3952 if (nondeflt_vers != NULL)
3953 {
3954 bfd_size_type cnt, symidx;
3955
3956 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
3957 {
3958 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
3959 char *shortname, *p;
3960
3961 p = strchr (h->root.root.string, ELF_VER_CHR);
3962 if (p == NULL
3963 || (h->root.type != bfd_link_hash_defined
3964 && h->root.type != bfd_link_hash_defweak))
3965 continue;
3966
3967 amt = p - h->root.root.string;
3968 shortname = bfd_malloc (amt + 1);
3969 memcpy (shortname, h->root.root.string, amt);
3970 shortname[amt] = '\0';
3971
3972 hi = (struct elf_link_hash_entry *)
3973 bfd_link_hash_lookup (&hash_table->root, shortname,
3974 FALSE, FALSE, FALSE);
3975 if (hi != NULL
3976 && hi->root.type == h->root.type
3977 && hi->root.u.def.value == h->root.u.def.value
3978 && hi->root.u.def.section == h->root.u.def.section)
3979 {
3980 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
3981 hi->root.type = bfd_link_hash_indirect;
3982 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
3983 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
3984 sym_hash = elf_sym_hashes (abfd);
3985 if (sym_hash)
3986 for (symidx = 0; symidx < extsymcount; ++symidx)
3987 if (sym_hash[symidx] == hi)
3988 {
3989 sym_hash[symidx] = h;
3990 break;
3991 }
3992 }
3993 free (shortname);
3994 }
3995 free (nondeflt_vers);
3996 nondeflt_vers = NULL;
3997 }
3998
3999 if (extversym != NULL)
4000 {
4001 free (extversym);
4002 extversym = NULL;
4003 }
4004
4005 if (isymbuf != NULL)
4006 free (isymbuf);
4007 isymbuf = NULL;
4008
4009 /* Now set the weakdefs field correctly for all the weak defined
4010 symbols we found. The only way to do this is to search all the
4011 symbols. Since we only need the information for non functions in
4012 dynamic objects, that's the only time we actually put anything on
4013 the list WEAKS. We need this information so that if a regular
4014 object refers to a symbol defined weakly in a dynamic object, the
4015 real symbol in the dynamic object is also put in the dynamic
4016 symbols; we also must arrange for both symbols to point to the
4017 same memory location. We could handle the general case of symbol
4018 aliasing, but a general symbol alias can only be generated in
4019 assembler code, handling it correctly would be very time
4020 consuming, and other ELF linkers don't handle general aliasing
4021 either. */
4022 if (weaks != NULL)
4023 {
4024 struct elf_link_hash_entry **hpp;
4025 struct elf_link_hash_entry **hppend;
4026 struct elf_link_hash_entry **sorted_sym_hash;
4027 struct elf_link_hash_entry *h;
4028 size_t sym_count;
4029
4030 /* Since we have to search the whole symbol list for each weak
4031 defined symbol, search time for N weak defined symbols will be
4032 O(N^2). Binary search will cut it down to O(NlogN). */
4033 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4034 sorted_sym_hash = bfd_malloc (amt);
4035 if (sorted_sym_hash == NULL)
4036 goto error_return;
4037 sym_hash = sorted_sym_hash;
4038 hpp = elf_sym_hashes (abfd);
4039 hppend = hpp + extsymcount;
4040 sym_count = 0;
4041 for (; hpp < hppend; hpp++)
4042 {
4043 h = *hpp;
4044 if (h != NULL
4045 && h->root.type == bfd_link_hash_defined
4046 && h->type != STT_FUNC)
4047 {
4048 *sym_hash = h;
4049 sym_hash++;
4050 sym_count++;
4051 }
4052 }
4053
4054 qsort (sorted_sym_hash, sym_count,
4055 sizeof (struct elf_link_hash_entry *),
4056 elf_sort_symbol);
4057
4058 while (weaks != NULL)
4059 {
4060 struct elf_link_hash_entry *hlook;
4061 asection *slook;
4062 bfd_vma vlook;
4063 long ilook;
4064 size_t i, j, idx;
4065
4066 hlook = weaks;
4067 weaks = hlook->u.weakdef;
4068 hlook->u.weakdef = NULL;
4069
4070 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4071 || hlook->root.type == bfd_link_hash_defweak
4072 || hlook->root.type == bfd_link_hash_common
4073 || hlook->root.type == bfd_link_hash_indirect);
4074 slook = hlook->root.u.def.section;
4075 vlook = hlook->root.u.def.value;
4076
4077 ilook = -1;
4078 i = 0;
4079 j = sym_count;
4080 while (i < j)
4081 {
4082 bfd_signed_vma vdiff;
4083 idx = (i + j) / 2;
4084 h = sorted_sym_hash [idx];
4085 vdiff = vlook - h->root.u.def.value;
4086 if (vdiff < 0)
4087 j = idx;
4088 else if (vdiff > 0)
4089 i = idx + 1;
4090 else
4091 {
4092 long sdiff = slook->id - h->root.u.def.section->id;
4093 if (sdiff < 0)
4094 j = idx;
4095 else if (sdiff > 0)
4096 i = idx + 1;
4097 else
4098 {
4099 ilook = idx;
4100 break;
4101 }
4102 }
4103 }
4104
4105 /* We didn't find a value/section match. */
4106 if (ilook == -1)
4107 continue;
4108
4109 for (i = ilook; i < sym_count; i++)
4110 {
4111 h = sorted_sym_hash [i];
4112
4113 /* Stop if value or section doesn't match. */
4114 if (h->root.u.def.value != vlook
4115 || h->root.u.def.section != slook)
4116 break;
4117 else if (h != hlook)
4118 {
4119 hlook->u.weakdef = h;
4120
4121 /* If the weak definition is in the list of dynamic
4122 symbols, make sure the real definition is put
4123 there as well. */
4124 if (hlook->dynindx != -1 && h->dynindx == -1)
4125 {
4126 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4127 goto error_return;
4128 }
4129
4130 /* If the real definition is in the list of dynamic
4131 symbols, make sure the weak definition is put
4132 there as well. If we don't do this, then the
4133 dynamic loader might not merge the entries for the
4134 real definition and the weak definition. */
4135 if (h->dynindx != -1 && hlook->dynindx == -1)
4136 {
4137 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4138 goto error_return;
4139 }
4140 break;
4141 }
4142 }
4143 }
4144
4145 free (sorted_sym_hash);
4146 }
4147
4148 check_directives = get_elf_backend_data (abfd)->check_directives;
4149 if (check_directives)
4150 check_directives (abfd, info);
4151
4152 /* If this object is the same format as the output object, and it is
4153 not a shared library, then let the backend look through the
4154 relocs.
4155
4156 This is required to build global offset table entries and to
4157 arrange for dynamic relocs. It is not required for the
4158 particular common case of linking non PIC code, even when linking
4159 against shared libraries, but unfortunately there is no way of
4160 knowing whether an object file has been compiled PIC or not.
4161 Looking through the relocs is not particularly time consuming.
4162 The problem is that we must either (1) keep the relocs in memory,
4163 which causes the linker to require additional runtime memory or
4164 (2) read the relocs twice from the input file, which wastes time.
4165 This would be a good case for using mmap.
4166
4167 I have no idea how to handle linking PIC code into a file of a
4168 different format. It probably can't be done. */
4169 check_relocs = get_elf_backend_data (abfd)->check_relocs;
4170 if (! dynamic
4171 && is_elf_hash_table (hash_table)
4172 && hash_table->root.creator == abfd->xvec
4173 && check_relocs != NULL)
4174 {
4175 asection *o;
4176
4177 for (o = abfd->sections; o != NULL; o = o->next)
4178 {
4179 Elf_Internal_Rela *internal_relocs;
4180 bfd_boolean ok;
4181
4182 if ((o->flags & SEC_RELOC) == 0
4183 || o->reloc_count == 0
4184 || ((info->strip == strip_all || info->strip == strip_debugger)
4185 && (o->flags & SEC_DEBUGGING) != 0)
4186 || bfd_is_abs_section (o->output_section))
4187 continue;
4188
4189 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4190 info->keep_memory);
4191 if (internal_relocs == NULL)
4192 goto error_return;
4193
4194 ok = (*check_relocs) (abfd, info, o, internal_relocs);
4195
4196 if (elf_section_data (o)->relocs != internal_relocs)
4197 free (internal_relocs);
4198
4199 if (! ok)
4200 goto error_return;
4201 }
4202 }
4203
4204 /* If this is a non-traditional link, try to optimize the handling
4205 of the .stab/.stabstr sections. */
4206 if (! dynamic
4207 && ! info->traditional_format
4208 && is_elf_hash_table (hash_table)
4209 && (info->strip != strip_all && info->strip != strip_debugger))
4210 {
4211 asection *stabstr;
4212
4213 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4214 if (stabstr != NULL)
4215 {
4216 bfd_size_type string_offset = 0;
4217 asection *stab;
4218
4219 for (stab = abfd->sections; stab; stab = stab->next)
4220 if (strncmp (".stab", stab->name, 5) == 0
4221 && (!stab->name[5] ||
4222 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4223 && (stab->flags & SEC_MERGE) == 0
4224 && !bfd_is_abs_section (stab->output_section))
4225 {
4226 struct bfd_elf_section_data *secdata;
4227
4228 secdata = elf_section_data (stab);
4229 if (! _bfd_link_section_stabs (abfd,
4230 &hash_table->stab_info,
4231 stab, stabstr,
4232 &secdata->sec_info,
4233 &string_offset))
4234 goto error_return;
4235 if (secdata->sec_info)
4236 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4237 }
4238 }
4239 }
4240
4241 if (is_elf_hash_table (hash_table))
4242 {
4243 /* Add this bfd to the loaded list. */
4244 struct elf_link_loaded_list *n;
4245
4246 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4247 if (n == NULL)
4248 goto error_return;
4249 n->abfd = abfd;
4250 n->next = hash_table->loaded;
4251 hash_table->loaded = n;
4252 }
4253
4254 return TRUE;
4255
4256 error_free_vers:
4257 if (nondeflt_vers != NULL)
4258 free (nondeflt_vers);
4259 if (extversym != NULL)
4260 free (extversym);
4261 error_free_sym:
4262 if (isymbuf != NULL)
4263 free (isymbuf);
4264 error_return:
4265 return FALSE;
4266 }
4267
4268 /* Return the linker hash table entry of a symbol that might be
4269 satisfied by an archive symbol. Return -1 on error. */
4270
4271 struct elf_link_hash_entry *
4272 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4273 struct bfd_link_info *info,
4274 const char *name)
4275 {
4276 struct elf_link_hash_entry *h;
4277 char *p, *copy;
4278 size_t len, first;
4279
4280 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4281 if (h != NULL)
4282 return h;
4283
4284 /* If this is a default version (the name contains @@), look up the
4285 symbol again with only one `@' as well as without the version.
4286 The effect is that references to the symbol with and without the
4287 version will be matched by the default symbol in the archive. */
4288
4289 p = strchr (name, ELF_VER_CHR);
4290 if (p == NULL || p[1] != ELF_VER_CHR)
4291 return h;
4292
4293 /* First check with only one `@'. */
4294 len = strlen (name);
4295 copy = bfd_alloc (abfd, len);
4296 if (copy == NULL)
4297 return (struct elf_link_hash_entry *) 0 - 1;
4298
4299 first = p - name + 1;
4300 memcpy (copy, name, first);
4301 memcpy (copy + first, name + first + 1, len - first);
4302
4303 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4304 if (h == NULL)
4305 {
4306 /* We also need to check references to the symbol without the
4307 version. */
4308 copy[first - 1] = '\0';
4309 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4310 FALSE, FALSE, FALSE);
4311 }
4312
4313 bfd_release (abfd, copy);
4314 return h;
4315 }
4316
4317 /* Add symbols from an ELF archive file to the linker hash table. We
4318 don't use _bfd_generic_link_add_archive_symbols because of a
4319 problem which arises on UnixWare. The UnixWare libc.so is an
4320 archive which includes an entry libc.so.1 which defines a bunch of
4321 symbols. The libc.so archive also includes a number of other
4322 object files, which also define symbols, some of which are the same
4323 as those defined in libc.so.1. Correct linking requires that we
4324 consider each object file in turn, and include it if it defines any
4325 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4326 this; it looks through the list of undefined symbols, and includes
4327 any object file which defines them. When this algorithm is used on
4328 UnixWare, it winds up pulling in libc.so.1 early and defining a
4329 bunch of symbols. This means that some of the other objects in the
4330 archive are not included in the link, which is incorrect since they
4331 precede libc.so.1 in the archive.
4332
4333 Fortunately, ELF archive handling is simpler than that done by
4334 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4335 oddities. In ELF, if we find a symbol in the archive map, and the
4336 symbol is currently undefined, we know that we must pull in that
4337 object file.
4338
4339 Unfortunately, we do have to make multiple passes over the symbol
4340 table until nothing further is resolved. */
4341
4342 static bfd_boolean
4343 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4344 {
4345 symindex c;
4346 bfd_boolean *defined = NULL;
4347 bfd_boolean *included = NULL;
4348 carsym *symdefs;
4349 bfd_boolean loop;
4350 bfd_size_type amt;
4351 const struct elf_backend_data *bed;
4352 struct elf_link_hash_entry * (*archive_symbol_lookup)
4353 (bfd *, struct bfd_link_info *, const char *);
4354
4355 if (! bfd_has_map (abfd))
4356 {
4357 /* An empty archive is a special case. */
4358 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4359 return TRUE;
4360 bfd_set_error (bfd_error_no_armap);
4361 return FALSE;
4362 }
4363
4364 /* Keep track of all symbols we know to be already defined, and all
4365 files we know to be already included. This is to speed up the
4366 second and subsequent passes. */
4367 c = bfd_ardata (abfd)->symdef_count;
4368 if (c == 0)
4369 return TRUE;
4370 amt = c;
4371 amt *= sizeof (bfd_boolean);
4372 defined = bfd_zmalloc (amt);
4373 included = bfd_zmalloc (amt);
4374 if (defined == NULL || included == NULL)
4375 goto error_return;
4376
4377 symdefs = bfd_ardata (abfd)->symdefs;
4378 bed = get_elf_backend_data (abfd);
4379 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
4380
4381 do
4382 {
4383 file_ptr last;
4384 symindex i;
4385 carsym *symdef;
4386 carsym *symdefend;
4387
4388 loop = FALSE;
4389 last = -1;
4390
4391 symdef = symdefs;
4392 symdefend = symdef + c;
4393 for (i = 0; symdef < symdefend; symdef++, i++)
4394 {
4395 struct elf_link_hash_entry *h;
4396 bfd *element;
4397 struct bfd_link_hash_entry *undefs_tail;
4398 symindex mark;
4399
4400 if (defined[i] || included[i])
4401 continue;
4402 if (symdef->file_offset == last)
4403 {
4404 included[i] = TRUE;
4405 continue;
4406 }
4407
4408 h = archive_symbol_lookup (abfd, info, symdef->name);
4409 if (h == (struct elf_link_hash_entry *) 0 - 1)
4410 goto error_return;
4411
4412 if (h == NULL)
4413 continue;
4414
4415 if (h->root.type == bfd_link_hash_common)
4416 {
4417 /* We currently have a common symbol. The archive map contains
4418 a reference to this symbol, so we may want to include it. We
4419 only want to include it however, if this archive element
4420 contains a definition of the symbol, not just another common
4421 declaration of it.
4422
4423 Unfortunately some archivers (including GNU ar) will put
4424 declarations of common symbols into their archive maps, as
4425 well as real definitions, so we cannot just go by the archive
4426 map alone. Instead we must read in the element's symbol
4427 table and check that to see what kind of symbol definition
4428 this is. */
4429 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4430 continue;
4431 }
4432 else if (h->root.type != bfd_link_hash_undefined)
4433 {
4434 if (h->root.type != bfd_link_hash_undefweak)
4435 defined[i] = TRUE;
4436 continue;
4437 }
4438
4439 /* We need to include this archive member. */
4440 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4441 if (element == NULL)
4442 goto error_return;
4443
4444 if (! bfd_check_format (element, bfd_object))
4445 goto error_return;
4446
4447 /* Doublecheck that we have not included this object
4448 already--it should be impossible, but there may be
4449 something wrong with the archive. */
4450 if (element->archive_pass != 0)
4451 {
4452 bfd_set_error (bfd_error_bad_value);
4453 goto error_return;
4454 }
4455 element->archive_pass = 1;
4456
4457 undefs_tail = info->hash->undefs_tail;
4458
4459 if (! (*info->callbacks->add_archive_element) (info, element,
4460 symdef->name))
4461 goto error_return;
4462 if (! bfd_link_add_symbols (element, info))
4463 goto error_return;
4464
4465 /* If there are any new undefined symbols, we need to make
4466 another pass through the archive in order to see whether
4467 they can be defined. FIXME: This isn't perfect, because
4468 common symbols wind up on undefs_tail and because an
4469 undefined symbol which is defined later on in this pass
4470 does not require another pass. This isn't a bug, but it
4471 does make the code less efficient than it could be. */
4472 if (undefs_tail != info->hash->undefs_tail)
4473 loop = TRUE;
4474
4475 /* Look backward to mark all symbols from this object file
4476 which we have already seen in this pass. */
4477 mark = i;
4478 do
4479 {
4480 included[mark] = TRUE;
4481 if (mark == 0)
4482 break;
4483 --mark;
4484 }
4485 while (symdefs[mark].file_offset == symdef->file_offset);
4486
4487 /* We mark subsequent symbols from this object file as we go
4488 on through the loop. */
4489 last = symdef->file_offset;
4490 }
4491 }
4492 while (loop);
4493
4494 free (defined);
4495 free (included);
4496
4497 return TRUE;
4498
4499 error_return:
4500 if (defined != NULL)
4501 free (defined);
4502 if (included != NULL)
4503 free (included);
4504 return FALSE;
4505 }
4506
4507 /* Given an ELF BFD, add symbols to the global hash table as
4508 appropriate. */
4509
4510 bfd_boolean
4511 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
4512 {
4513 switch (bfd_get_format (abfd))
4514 {
4515 case bfd_object:
4516 return elf_link_add_object_symbols (abfd, info);
4517 case bfd_archive:
4518 return elf_link_add_archive_symbols (abfd, info);
4519 default:
4520 bfd_set_error (bfd_error_wrong_format);
4521 return FALSE;
4522 }
4523 }
4524 \f
4525 /* This function will be called though elf_link_hash_traverse to store
4526 all hash value of the exported symbols in an array. */
4527
4528 static bfd_boolean
4529 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
4530 {
4531 unsigned long **valuep = data;
4532 const char *name;
4533 char *p;
4534 unsigned long ha;
4535 char *alc = NULL;
4536
4537 if (h->root.type == bfd_link_hash_warning)
4538 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4539
4540 /* Ignore indirect symbols. These are added by the versioning code. */
4541 if (h->dynindx == -1)
4542 return TRUE;
4543
4544 name = h->root.root.string;
4545 p = strchr (name, ELF_VER_CHR);
4546 if (p != NULL)
4547 {
4548 alc = bfd_malloc (p - name + 1);
4549 memcpy (alc, name, p - name);
4550 alc[p - name] = '\0';
4551 name = alc;
4552 }
4553
4554 /* Compute the hash value. */
4555 ha = bfd_elf_hash (name);
4556
4557 /* Store the found hash value in the array given as the argument. */
4558 *(*valuep)++ = ha;
4559
4560 /* And store it in the struct so that we can put it in the hash table
4561 later. */
4562 h->u.elf_hash_value = ha;
4563
4564 if (alc != NULL)
4565 free (alc);
4566
4567 return TRUE;
4568 }
4569
4570 /* Array used to determine the number of hash table buckets to use
4571 based on the number of symbols there are. If there are fewer than
4572 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4573 fewer than 37 we use 17 buckets, and so forth. We never use more
4574 than 32771 buckets. */
4575
4576 static const size_t elf_buckets[] =
4577 {
4578 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4579 16411, 32771, 0
4580 };
4581
4582 /* Compute bucket count for hashing table. We do not use a static set
4583 of possible tables sizes anymore. Instead we determine for all
4584 possible reasonable sizes of the table the outcome (i.e., the
4585 number of collisions etc) and choose the best solution. The
4586 weighting functions are not too simple to allow the table to grow
4587 without bounds. Instead one of the weighting factors is the size.
4588 Therefore the result is always a good payoff between few collisions
4589 (= short chain lengths) and table size. */
4590 static size_t
4591 compute_bucket_count (struct bfd_link_info *info)
4592 {
4593 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
4594 size_t best_size = 0;
4595 unsigned long int *hashcodes;
4596 unsigned long int *hashcodesp;
4597 unsigned long int i;
4598 bfd_size_type amt;
4599
4600 /* Compute the hash values for all exported symbols. At the same
4601 time store the values in an array so that we could use them for
4602 optimizations. */
4603 amt = dynsymcount;
4604 amt *= sizeof (unsigned long int);
4605 hashcodes = bfd_malloc (amt);
4606 if (hashcodes == NULL)
4607 return 0;
4608 hashcodesp = hashcodes;
4609
4610 /* Put all hash values in HASHCODES. */
4611 elf_link_hash_traverse (elf_hash_table (info),
4612 elf_collect_hash_codes, &hashcodesp);
4613
4614 /* We have a problem here. The following code to optimize the table
4615 size requires an integer type with more the 32 bits. If
4616 BFD_HOST_U_64_BIT is set we know about such a type. */
4617 #ifdef BFD_HOST_U_64_BIT
4618 if (info->optimize)
4619 {
4620 unsigned long int nsyms = hashcodesp - hashcodes;
4621 size_t minsize;
4622 size_t maxsize;
4623 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
4624 unsigned long int *counts ;
4625 bfd *dynobj = elf_hash_table (info)->dynobj;
4626 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
4627
4628 /* Possible optimization parameters: if we have NSYMS symbols we say
4629 that the hashing table must at least have NSYMS/4 and at most
4630 2*NSYMS buckets. */
4631 minsize = nsyms / 4;
4632 if (minsize == 0)
4633 minsize = 1;
4634 best_size = maxsize = nsyms * 2;
4635
4636 /* Create array where we count the collisions in. We must use bfd_malloc
4637 since the size could be large. */
4638 amt = maxsize;
4639 amt *= sizeof (unsigned long int);
4640 counts = bfd_malloc (amt);
4641 if (counts == NULL)
4642 {
4643 free (hashcodes);
4644 return 0;
4645 }
4646
4647 /* Compute the "optimal" size for the hash table. The criteria is a
4648 minimal chain length. The minor criteria is (of course) the size
4649 of the table. */
4650 for (i = minsize; i < maxsize; ++i)
4651 {
4652 /* Walk through the array of hashcodes and count the collisions. */
4653 BFD_HOST_U_64_BIT max;
4654 unsigned long int j;
4655 unsigned long int fact;
4656
4657 memset (counts, '\0', i * sizeof (unsigned long int));
4658
4659 /* Determine how often each hash bucket is used. */
4660 for (j = 0; j < nsyms; ++j)
4661 ++counts[hashcodes[j] % i];
4662
4663 /* For the weight function we need some information about the
4664 pagesize on the target. This is information need not be 100%
4665 accurate. Since this information is not available (so far) we
4666 define it here to a reasonable default value. If it is crucial
4667 to have a better value some day simply define this value. */
4668 # ifndef BFD_TARGET_PAGESIZE
4669 # define BFD_TARGET_PAGESIZE (4096)
4670 # endif
4671
4672 /* We in any case need 2 + NSYMS entries for the size values and
4673 the chains. */
4674 max = (2 + nsyms) * (bed->s->arch_size / 8);
4675
4676 # if 1
4677 /* Variant 1: optimize for short chains. We add the squares
4678 of all the chain lengths (which favors many small chain
4679 over a few long chains). */
4680 for (j = 0; j < i; ++j)
4681 max += counts[j] * counts[j];
4682
4683 /* This adds penalties for the overall size of the table. */
4684 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4685 max *= fact * fact;
4686 # else
4687 /* Variant 2: Optimize a lot more for small table. Here we
4688 also add squares of the size but we also add penalties for
4689 empty slots (the +1 term). */
4690 for (j = 0; j < i; ++j)
4691 max += (1 + counts[j]) * (1 + counts[j]);
4692
4693 /* The overall size of the table is considered, but not as
4694 strong as in variant 1, where it is squared. */
4695 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4696 max *= fact;
4697 # endif
4698
4699 /* Compare with current best results. */
4700 if (max < best_chlen)
4701 {
4702 best_chlen = max;
4703 best_size = i;
4704 }
4705 }
4706
4707 free (counts);
4708 }
4709 else
4710 #endif /* defined (BFD_HOST_U_64_BIT) */
4711 {
4712 /* This is the fallback solution if no 64bit type is available or if we
4713 are not supposed to spend much time on optimizations. We select the
4714 bucket count using a fixed set of numbers. */
4715 for (i = 0; elf_buckets[i] != 0; i++)
4716 {
4717 best_size = elf_buckets[i];
4718 if (dynsymcount < elf_buckets[i + 1])
4719 break;
4720 }
4721 }
4722
4723 /* Free the arrays we needed. */
4724 free (hashcodes);
4725
4726 return best_size;
4727 }
4728
4729 /* Set up the sizes and contents of the ELF dynamic sections. This is
4730 called by the ELF linker emulation before_allocation routine. We
4731 must set the sizes of the sections before the linker sets the
4732 addresses of the various sections. */
4733
4734 bfd_boolean
4735 bfd_elf_size_dynamic_sections (bfd *output_bfd,
4736 const char *soname,
4737 const char *rpath,
4738 const char *filter_shlib,
4739 const char * const *auxiliary_filters,
4740 struct bfd_link_info *info,
4741 asection **sinterpptr,
4742 struct bfd_elf_version_tree *verdefs)
4743 {
4744 bfd_size_type soname_indx;
4745 bfd *dynobj;
4746 const struct elf_backend_data *bed;
4747 struct elf_assign_sym_version_info asvinfo;
4748
4749 *sinterpptr = NULL;
4750
4751 soname_indx = (bfd_size_type) -1;
4752
4753 if (!is_elf_hash_table (info->hash))
4754 return TRUE;
4755
4756 elf_tdata (output_bfd)->relro = info->relro;
4757 if (info->execstack)
4758 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
4759 else if (info->noexecstack)
4760 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
4761 else
4762 {
4763 bfd *inputobj;
4764 asection *notesec = NULL;
4765 int exec = 0;
4766
4767 for (inputobj = info->input_bfds;
4768 inputobj;
4769 inputobj = inputobj->link_next)
4770 {
4771 asection *s;
4772
4773 if (inputobj->flags & DYNAMIC)
4774 continue;
4775 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
4776 if (s)
4777 {
4778 if (s->flags & SEC_CODE)
4779 exec = PF_X;
4780 notesec = s;
4781 }
4782 else
4783 exec = PF_X;
4784 }
4785 if (notesec)
4786 {
4787 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
4788 if (exec && info->relocatable
4789 && notesec->output_section != bfd_abs_section_ptr)
4790 notesec->output_section->flags |= SEC_CODE;
4791 }
4792 }
4793
4794 /* Any syms created from now on start with -1 in
4795 got.refcount/offset and plt.refcount/offset. */
4796 elf_hash_table (info)->init_refcount = elf_hash_table (info)->init_offset;
4797
4798 /* The backend may have to create some sections regardless of whether
4799 we're dynamic or not. */
4800 bed = get_elf_backend_data (output_bfd);
4801 if (bed->elf_backend_always_size_sections
4802 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
4803 return FALSE;
4804
4805 dynobj = elf_hash_table (info)->dynobj;
4806
4807 /* If there were no dynamic objects in the link, there is nothing to
4808 do here. */
4809 if (dynobj == NULL)
4810 return TRUE;
4811
4812 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
4813 return FALSE;
4814
4815 if (elf_hash_table (info)->dynamic_sections_created)
4816 {
4817 struct elf_info_failed eif;
4818 struct elf_link_hash_entry *h;
4819 asection *dynstr;
4820 struct bfd_elf_version_tree *t;
4821 struct bfd_elf_version_expr *d;
4822 bfd_boolean all_defined;
4823
4824 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
4825 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
4826
4827 if (soname != NULL)
4828 {
4829 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4830 soname, TRUE);
4831 if (soname_indx == (bfd_size_type) -1
4832 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
4833 return FALSE;
4834 }
4835
4836 if (info->symbolic)
4837 {
4838 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
4839 return FALSE;
4840 info->flags |= DF_SYMBOLIC;
4841 }
4842
4843 if (rpath != NULL)
4844 {
4845 bfd_size_type indx;
4846
4847 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
4848 TRUE);
4849 if (indx == (bfd_size_type) -1
4850 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
4851 return FALSE;
4852
4853 if (info->new_dtags)
4854 {
4855 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
4856 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
4857 return FALSE;
4858 }
4859 }
4860
4861 if (filter_shlib != NULL)
4862 {
4863 bfd_size_type indx;
4864
4865 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4866 filter_shlib, TRUE);
4867 if (indx == (bfd_size_type) -1
4868 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
4869 return FALSE;
4870 }
4871
4872 if (auxiliary_filters != NULL)
4873 {
4874 const char * const *p;
4875
4876 for (p = auxiliary_filters; *p != NULL; p++)
4877 {
4878 bfd_size_type indx;
4879
4880 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4881 *p, TRUE);
4882 if (indx == (bfd_size_type) -1
4883 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
4884 return FALSE;
4885 }
4886 }
4887
4888 eif.info = info;
4889 eif.verdefs = verdefs;
4890 eif.failed = FALSE;
4891
4892 /* If we are supposed to export all symbols into the dynamic symbol
4893 table (this is not the normal case), then do so. */
4894 if (info->export_dynamic)
4895 {
4896 elf_link_hash_traverse (elf_hash_table (info),
4897 _bfd_elf_export_symbol,
4898 &eif);
4899 if (eif.failed)
4900 return FALSE;
4901 }
4902
4903 /* Make all global versions with definition. */
4904 for (t = verdefs; t != NULL; t = t->next)
4905 for (d = t->globals.list; d != NULL; d = d->next)
4906 if (!d->symver && d->symbol)
4907 {
4908 const char *verstr, *name;
4909 size_t namelen, verlen, newlen;
4910 char *newname, *p;
4911 struct elf_link_hash_entry *newh;
4912
4913 name = d->symbol;
4914 namelen = strlen (name);
4915 verstr = t->name;
4916 verlen = strlen (verstr);
4917 newlen = namelen + verlen + 3;
4918
4919 newname = bfd_malloc (newlen);
4920 if (newname == NULL)
4921 return FALSE;
4922 memcpy (newname, name, namelen);
4923
4924 /* Check the hidden versioned definition. */
4925 p = newname + namelen;
4926 *p++ = ELF_VER_CHR;
4927 memcpy (p, verstr, verlen + 1);
4928 newh = elf_link_hash_lookup (elf_hash_table (info),
4929 newname, FALSE, FALSE,
4930 FALSE);
4931 if (newh == NULL
4932 || (newh->root.type != bfd_link_hash_defined
4933 && newh->root.type != bfd_link_hash_defweak))
4934 {
4935 /* Check the default versioned definition. */
4936 *p++ = ELF_VER_CHR;
4937 memcpy (p, verstr, verlen + 1);
4938 newh = elf_link_hash_lookup (elf_hash_table (info),
4939 newname, FALSE, FALSE,
4940 FALSE);
4941 }
4942 free (newname);
4943
4944 /* Mark this version if there is a definition and it is
4945 not defined in a shared object. */
4946 if (newh != NULL
4947 && !newh->def_dynamic
4948 && (newh->root.type == bfd_link_hash_defined
4949 || newh->root.type == bfd_link_hash_defweak))
4950 d->symver = 1;
4951 }
4952
4953 /* Attach all the symbols to their version information. */
4954 asvinfo.output_bfd = output_bfd;
4955 asvinfo.info = info;
4956 asvinfo.verdefs = verdefs;
4957 asvinfo.failed = FALSE;
4958
4959 elf_link_hash_traverse (elf_hash_table (info),
4960 _bfd_elf_link_assign_sym_version,
4961 &asvinfo);
4962 if (asvinfo.failed)
4963 return FALSE;
4964
4965 if (!info->allow_undefined_version)
4966 {
4967 /* Check if all global versions have a definition. */
4968 all_defined = TRUE;
4969 for (t = verdefs; t != NULL; t = t->next)
4970 for (d = t->globals.list; d != NULL; d = d->next)
4971 if (!d->symver && !d->script)
4972 {
4973 (*_bfd_error_handler)
4974 (_("%s: undefined version: %s"),
4975 d->pattern, t->name);
4976 all_defined = FALSE;
4977 }
4978
4979 if (!all_defined)
4980 {
4981 bfd_set_error (bfd_error_bad_value);
4982 return FALSE;
4983 }
4984 }
4985
4986 /* Find all symbols which were defined in a dynamic object and make
4987 the backend pick a reasonable value for them. */
4988 elf_link_hash_traverse (elf_hash_table (info),
4989 _bfd_elf_adjust_dynamic_symbol,
4990 &eif);
4991 if (eif.failed)
4992 return FALSE;
4993
4994 /* Add some entries to the .dynamic section. We fill in some of the
4995 values later, in bfd_elf_final_link, but we must add the entries
4996 now so that we know the final size of the .dynamic section. */
4997
4998 /* If there are initialization and/or finalization functions to
4999 call then add the corresponding DT_INIT/DT_FINI entries. */
5000 h = (info->init_function
5001 ? elf_link_hash_lookup (elf_hash_table (info),
5002 info->init_function, FALSE,
5003 FALSE, FALSE)
5004 : NULL);
5005 if (h != NULL
5006 && (h->ref_regular
5007 || h->def_regular))
5008 {
5009 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5010 return FALSE;
5011 }
5012 h = (info->fini_function
5013 ? elf_link_hash_lookup (elf_hash_table (info),
5014 info->fini_function, FALSE,
5015 FALSE, FALSE)
5016 : NULL);
5017 if (h != NULL
5018 && (h->ref_regular
5019 || h->def_regular))
5020 {
5021 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5022 return FALSE;
5023 }
5024
5025 if (bfd_get_section_by_name (output_bfd, ".preinit_array") != NULL)
5026 {
5027 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5028 if (! info->executable)
5029 {
5030 bfd *sub;
5031 asection *o;
5032
5033 for (sub = info->input_bfds; sub != NULL;
5034 sub = sub->link_next)
5035 for (o = sub->sections; o != NULL; o = o->next)
5036 if (elf_section_data (o)->this_hdr.sh_type
5037 == SHT_PREINIT_ARRAY)
5038 {
5039 (*_bfd_error_handler)
5040 (_("%B: .preinit_array section is not allowed in DSO"),
5041 sub);
5042 break;
5043 }
5044
5045 bfd_set_error (bfd_error_nonrepresentable_section);
5046 return FALSE;
5047 }
5048
5049 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5050 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5051 return FALSE;
5052 }
5053 if (bfd_get_section_by_name (output_bfd, ".init_array") != NULL)
5054 {
5055 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5056 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5057 return FALSE;
5058 }
5059 if (bfd_get_section_by_name (output_bfd, ".fini_array") != NULL)
5060 {
5061 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5062 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5063 return FALSE;
5064 }
5065
5066 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5067 /* If .dynstr is excluded from the link, we don't want any of
5068 these tags. Strictly, we should be checking each section
5069 individually; This quick check covers for the case where
5070 someone does a /DISCARD/ : { *(*) }. */
5071 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5072 {
5073 bfd_size_type strsize;
5074
5075 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5076 if (!_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)
5077 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5078 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5079 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5080 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5081 bed->s->sizeof_sym))
5082 return FALSE;
5083 }
5084 }
5085
5086 /* The backend must work out the sizes of all the other dynamic
5087 sections. */
5088 if (bed->elf_backend_size_dynamic_sections
5089 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5090 return FALSE;
5091
5092 if (elf_hash_table (info)->dynamic_sections_created)
5093 {
5094 bfd_size_type dynsymcount;
5095 asection *s;
5096 size_t bucketcount = 0;
5097 size_t hash_entry_size;
5098 unsigned int dtagcount;
5099
5100 /* Set up the version definition section. */
5101 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5102 BFD_ASSERT (s != NULL);
5103
5104 /* We may have created additional version definitions if we are
5105 just linking a regular application. */
5106 verdefs = asvinfo.verdefs;
5107
5108 /* Skip anonymous version tag. */
5109 if (verdefs != NULL && verdefs->vernum == 0)
5110 verdefs = verdefs->next;
5111
5112 if (verdefs == NULL && !info->create_default_symver)
5113 _bfd_strip_section_from_output (info, s);
5114 else
5115 {
5116 unsigned int cdefs;
5117 bfd_size_type size;
5118 struct bfd_elf_version_tree *t;
5119 bfd_byte *p;
5120 Elf_Internal_Verdef def;
5121 Elf_Internal_Verdaux defaux;
5122 struct bfd_link_hash_entry *bh;
5123 struct elf_link_hash_entry *h;
5124 const char *name;
5125
5126 cdefs = 0;
5127 size = 0;
5128
5129 /* Make space for the base version. */
5130 size += sizeof (Elf_External_Verdef);
5131 size += sizeof (Elf_External_Verdaux);
5132 ++cdefs;
5133
5134 /* Make space for the default version. */
5135 if (info->create_default_symver)
5136 {
5137 size += sizeof (Elf_External_Verdef);
5138 ++cdefs;
5139 }
5140
5141 for (t = verdefs; t != NULL; t = t->next)
5142 {
5143 struct bfd_elf_version_deps *n;
5144
5145 size += sizeof (Elf_External_Verdef);
5146 size += sizeof (Elf_External_Verdaux);
5147 ++cdefs;
5148
5149 for (n = t->deps; n != NULL; n = n->next)
5150 size += sizeof (Elf_External_Verdaux);
5151 }
5152
5153 s->size = size;
5154 s->contents = bfd_alloc (output_bfd, s->size);
5155 if (s->contents == NULL && s->size != 0)
5156 return FALSE;
5157
5158 /* Fill in the version definition section. */
5159
5160 p = s->contents;
5161
5162 def.vd_version = VER_DEF_CURRENT;
5163 def.vd_flags = VER_FLG_BASE;
5164 def.vd_ndx = 1;
5165 def.vd_cnt = 1;
5166 if (info->create_default_symver)
5167 {
5168 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5169 def.vd_next = sizeof (Elf_External_Verdef);
5170 }
5171 else
5172 {
5173 def.vd_aux = sizeof (Elf_External_Verdef);
5174 def.vd_next = (sizeof (Elf_External_Verdef)
5175 + sizeof (Elf_External_Verdaux));
5176 }
5177
5178 if (soname_indx != (bfd_size_type) -1)
5179 {
5180 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5181 soname_indx);
5182 def.vd_hash = bfd_elf_hash (soname);
5183 defaux.vda_name = soname_indx;
5184 name = soname;
5185 }
5186 else
5187 {
5188 bfd_size_type indx;
5189
5190 name = basename (output_bfd->filename);
5191 def.vd_hash = bfd_elf_hash (name);
5192 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5193 name, FALSE);
5194 if (indx == (bfd_size_type) -1)
5195 return FALSE;
5196 defaux.vda_name = indx;
5197 }
5198 defaux.vda_next = 0;
5199
5200 _bfd_elf_swap_verdef_out (output_bfd, &def,
5201 (Elf_External_Verdef *) p);
5202 p += sizeof (Elf_External_Verdef);
5203 if (info->create_default_symver)
5204 {
5205 /* Add a symbol representing this version. */
5206 bh = NULL;
5207 if (! (_bfd_generic_link_add_one_symbol
5208 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
5209 0, NULL, FALSE,
5210 get_elf_backend_data (dynobj)->collect, &bh)))
5211 return FALSE;
5212 h = (struct elf_link_hash_entry *) bh;
5213 h->non_elf = 0;
5214 h->def_regular = 1;
5215 h->type = STT_OBJECT;
5216 h->verinfo.vertree = NULL;
5217
5218 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5219 return FALSE;
5220
5221 /* Create a duplicate of the base version with the same
5222 aux block, but different flags. */
5223 def.vd_flags = 0;
5224 def.vd_ndx = 2;
5225 def.vd_aux = sizeof (Elf_External_Verdef);
5226 if (verdefs)
5227 def.vd_next = (sizeof (Elf_External_Verdef)
5228 + sizeof (Elf_External_Verdaux));
5229 else
5230 def.vd_next = 0;
5231 _bfd_elf_swap_verdef_out (output_bfd, &def,
5232 (Elf_External_Verdef *) p);
5233 p += sizeof (Elf_External_Verdef);
5234 }
5235 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5236 (Elf_External_Verdaux *) p);
5237 p += sizeof (Elf_External_Verdaux);
5238
5239 for (t = verdefs; t != NULL; t = t->next)
5240 {
5241 unsigned int cdeps;
5242 struct bfd_elf_version_deps *n;
5243
5244 cdeps = 0;
5245 for (n = t->deps; n != NULL; n = n->next)
5246 ++cdeps;
5247
5248 /* Add a symbol representing this version. */
5249 bh = NULL;
5250 if (! (_bfd_generic_link_add_one_symbol
5251 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5252 0, NULL, FALSE,
5253 get_elf_backend_data (dynobj)->collect, &bh)))
5254 return FALSE;
5255 h = (struct elf_link_hash_entry *) bh;
5256 h->non_elf = 0;
5257 h->def_regular = 1;
5258 h->type = STT_OBJECT;
5259 h->verinfo.vertree = t;
5260
5261 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5262 return FALSE;
5263
5264 def.vd_version = VER_DEF_CURRENT;
5265 def.vd_flags = 0;
5266 if (t->globals.list == NULL
5267 && t->locals.list == NULL
5268 && ! t->used)
5269 def.vd_flags |= VER_FLG_WEAK;
5270 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5271 def.vd_cnt = cdeps + 1;
5272 def.vd_hash = bfd_elf_hash (t->name);
5273 def.vd_aux = sizeof (Elf_External_Verdef);
5274 def.vd_next = 0;
5275 if (t->next != NULL)
5276 def.vd_next = (sizeof (Elf_External_Verdef)
5277 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5278
5279 _bfd_elf_swap_verdef_out (output_bfd, &def,
5280 (Elf_External_Verdef *) p);
5281 p += sizeof (Elf_External_Verdef);
5282
5283 defaux.vda_name = h->dynstr_index;
5284 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5285 h->dynstr_index);
5286 defaux.vda_next = 0;
5287 if (t->deps != NULL)
5288 defaux.vda_next = sizeof (Elf_External_Verdaux);
5289 t->name_indx = defaux.vda_name;
5290
5291 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5292 (Elf_External_Verdaux *) p);
5293 p += sizeof (Elf_External_Verdaux);
5294
5295 for (n = t->deps; n != NULL; n = n->next)
5296 {
5297 if (n->version_needed == NULL)
5298 {
5299 /* This can happen if there was an error in the
5300 version script. */
5301 defaux.vda_name = 0;
5302 }
5303 else
5304 {
5305 defaux.vda_name = n->version_needed->name_indx;
5306 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5307 defaux.vda_name);
5308 }
5309 if (n->next == NULL)
5310 defaux.vda_next = 0;
5311 else
5312 defaux.vda_next = sizeof (Elf_External_Verdaux);
5313
5314 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5315 (Elf_External_Verdaux *) p);
5316 p += sizeof (Elf_External_Verdaux);
5317 }
5318 }
5319
5320 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
5321 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
5322 return FALSE;
5323
5324 elf_tdata (output_bfd)->cverdefs = cdefs;
5325 }
5326
5327 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
5328 {
5329 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
5330 return FALSE;
5331 }
5332 else if (info->flags & DF_BIND_NOW)
5333 {
5334 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
5335 return FALSE;
5336 }
5337
5338 if (info->flags_1)
5339 {
5340 if (info->executable)
5341 info->flags_1 &= ~ (DF_1_INITFIRST
5342 | DF_1_NODELETE
5343 | DF_1_NOOPEN);
5344 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
5345 return FALSE;
5346 }
5347
5348 /* Work out the size of the version reference section. */
5349
5350 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
5351 BFD_ASSERT (s != NULL);
5352 {
5353 struct elf_find_verdep_info sinfo;
5354
5355 sinfo.output_bfd = output_bfd;
5356 sinfo.info = info;
5357 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
5358 if (sinfo.vers == 0)
5359 sinfo.vers = 1;
5360 sinfo.failed = FALSE;
5361
5362 elf_link_hash_traverse (elf_hash_table (info),
5363 _bfd_elf_link_find_version_dependencies,
5364 &sinfo);
5365
5366 if (elf_tdata (output_bfd)->verref == NULL)
5367 _bfd_strip_section_from_output (info, s);
5368 else
5369 {
5370 Elf_Internal_Verneed *t;
5371 unsigned int size;
5372 unsigned int crefs;
5373 bfd_byte *p;
5374
5375 /* Build the version definition section. */
5376 size = 0;
5377 crefs = 0;
5378 for (t = elf_tdata (output_bfd)->verref;
5379 t != NULL;
5380 t = t->vn_nextref)
5381 {
5382 Elf_Internal_Vernaux *a;
5383
5384 size += sizeof (Elf_External_Verneed);
5385 ++crefs;
5386 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5387 size += sizeof (Elf_External_Vernaux);
5388 }
5389
5390 s->size = size;
5391 s->contents = bfd_alloc (output_bfd, s->size);
5392 if (s->contents == NULL)
5393 return FALSE;
5394
5395 p = s->contents;
5396 for (t = elf_tdata (output_bfd)->verref;
5397 t != NULL;
5398 t = t->vn_nextref)
5399 {
5400 unsigned int caux;
5401 Elf_Internal_Vernaux *a;
5402 bfd_size_type indx;
5403
5404 caux = 0;
5405 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5406 ++caux;
5407
5408 t->vn_version = VER_NEED_CURRENT;
5409 t->vn_cnt = caux;
5410 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5411 elf_dt_name (t->vn_bfd) != NULL
5412 ? elf_dt_name (t->vn_bfd)
5413 : basename (t->vn_bfd->filename),
5414 FALSE);
5415 if (indx == (bfd_size_type) -1)
5416 return FALSE;
5417 t->vn_file = indx;
5418 t->vn_aux = sizeof (Elf_External_Verneed);
5419 if (t->vn_nextref == NULL)
5420 t->vn_next = 0;
5421 else
5422 t->vn_next = (sizeof (Elf_External_Verneed)
5423 + caux * sizeof (Elf_External_Vernaux));
5424
5425 _bfd_elf_swap_verneed_out (output_bfd, t,
5426 (Elf_External_Verneed *) p);
5427 p += sizeof (Elf_External_Verneed);
5428
5429 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5430 {
5431 a->vna_hash = bfd_elf_hash (a->vna_nodename);
5432 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5433 a->vna_nodename, FALSE);
5434 if (indx == (bfd_size_type) -1)
5435 return FALSE;
5436 a->vna_name = indx;
5437 if (a->vna_nextptr == NULL)
5438 a->vna_next = 0;
5439 else
5440 a->vna_next = sizeof (Elf_External_Vernaux);
5441
5442 _bfd_elf_swap_vernaux_out (output_bfd, a,
5443 (Elf_External_Vernaux *) p);
5444 p += sizeof (Elf_External_Vernaux);
5445 }
5446 }
5447
5448 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
5449 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
5450 return FALSE;
5451
5452 elf_tdata (output_bfd)->cverrefs = crefs;
5453 }
5454 }
5455
5456 /* Assign dynsym indicies. In a shared library we generate a
5457 section symbol for each output section, which come first.
5458 Next come all of the back-end allocated local dynamic syms,
5459 followed by the rest of the global symbols. */
5460
5461 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
5462
5463 /* Work out the size of the symbol version section. */
5464 s = bfd_get_section_by_name (dynobj, ".gnu.version");
5465 BFD_ASSERT (s != NULL);
5466 if (dynsymcount == 0
5467 || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL
5468 && !info->create_default_symver))
5469 {
5470 _bfd_strip_section_from_output (info, s);
5471 /* The DYNSYMCOUNT might have changed if we were going to
5472 output a dynamic symbol table entry for S. */
5473 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
5474 }
5475 else
5476 {
5477 s->size = dynsymcount * sizeof (Elf_External_Versym);
5478 s->contents = bfd_zalloc (output_bfd, s->size);
5479 if (s->contents == NULL)
5480 return FALSE;
5481
5482 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
5483 return FALSE;
5484 }
5485
5486 /* Set the size of the .dynsym and .hash sections. We counted
5487 the number of dynamic symbols in elf_link_add_object_symbols.
5488 We will build the contents of .dynsym and .hash when we build
5489 the final symbol table, because until then we do not know the
5490 correct value to give the symbols. We built the .dynstr
5491 section as we went along in elf_link_add_object_symbols. */
5492 s = bfd_get_section_by_name (dynobj, ".dynsym");
5493 BFD_ASSERT (s != NULL);
5494 s->size = dynsymcount * bed->s->sizeof_sym;
5495 s->contents = bfd_alloc (output_bfd, s->size);
5496 if (s->contents == NULL && s->size != 0)
5497 return FALSE;
5498
5499 if (dynsymcount != 0)
5500 {
5501 Elf_Internal_Sym isym;
5502
5503 /* The first entry in .dynsym is a dummy symbol. */
5504 isym.st_value = 0;
5505 isym.st_size = 0;
5506 isym.st_name = 0;
5507 isym.st_info = 0;
5508 isym.st_other = 0;
5509 isym.st_shndx = 0;
5510 bed->s->swap_symbol_out (output_bfd, &isym, s->contents, 0);
5511 }
5512
5513 /* Compute the size of the hashing table. As a side effect this
5514 computes the hash values for all the names we export. */
5515 bucketcount = compute_bucket_count (info);
5516
5517 s = bfd_get_section_by_name (dynobj, ".hash");
5518 BFD_ASSERT (s != NULL);
5519 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
5520 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
5521 s->contents = bfd_zalloc (output_bfd, s->size);
5522 if (s->contents == NULL)
5523 return FALSE;
5524
5525 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
5526 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
5527 s->contents + hash_entry_size);
5528
5529 elf_hash_table (info)->bucketcount = bucketcount;
5530
5531 s = bfd_get_section_by_name (dynobj, ".dynstr");
5532 BFD_ASSERT (s != NULL);
5533
5534 elf_finalize_dynstr (output_bfd, info);
5535
5536 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5537
5538 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
5539 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
5540 return FALSE;
5541 }
5542
5543 return TRUE;
5544 }
5545
5546 /* Final phase of ELF linker. */
5547
5548 /* A structure we use to avoid passing large numbers of arguments. */
5549
5550 struct elf_final_link_info
5551 {
5552 /* General link information. */
5553 struct bfd_link_info *info;
5554 /* Output BFD. */
5555 bfd *output_bfd;
5556 /* Symbol string table. */
5557 struct bfd_strtab_hash *symstrtab;
5558 /* .dynsym section. */
5559 asection *dynsym_sec;
5560 /* .hash section. */
5561 asection *hash_sec;
5562 /* symbol version section (.gnu.version). */
5563 asection *symver_sec;
5564 /* Buffer large enough to hold contents of any section. */
5565 bfd_byte *contents;
5566 /* Buffer large enough to hold external relocs of any section. */
5567 void *external_relocs;
5568 /* Buffer large enough to hold internal relocs of any section. */
5569 Elf_Internal_Rela *internal_relocs;
5570 /* Buffer large enough to hold external local symbols of any input
5571 BFD. */
5572 bfd_byte *external_syms;
5573 /* And a buffer for symbol section indices. */
5574 Elf_External_Sym_Shndx *locsym_shndx;
5575 /* Buffer large enough to hold internal local symbols of any input
5576 BFD. */
5577 Elf_Internal_Sym *internal_syms;
5578 /* Array large enough to hold a symbol index for each local symbol
5579 of any input BFD. */
5580 long *indices;
5581 /* Array large enough to hold a section pointer for each local
5582 symbol of any input BFD. */
5583 asection **sections;
5584 /* Buffer to hold swapped out symbols. */
5585 bfd_byte *symbuf;
5586 /* And one for symbol section indices. */
5587 Elf_External_Sym_Shndx *symshndxbuf;
5588 /* Number of swapped out symbols in buffer. */
5589 size_t symbuf_count;
5590 /* Number of symbols which fit in symbuf. */
5591 size_t symbuf_size;
5592 /* And same for symshndxbuf. */
5593 size_t shndxbuf_size;
5594 };
5595
5596 /* This struct is used to pass information to elf_link_output_extsym. */
5597
5598 struct elf_outext_info
5599 {
5600 bfd_boolean failed;
5601 bfd_boolean localsyms;
5602 struct elf_final_link_info *finfo;
5603 };
5604
5605 /* When performing a relocatable link, the input relocations are
5606 preserved. But, if they reference global symbols, the indices
5607 referenced must be updated. Update all the relocations in
5608 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5609
5610 static void
5611 elf_link_adjust_relocs (bfd *abfd,
5612 Elf_Internal_Shdr *rel_hdr,
5613 unsigned int count,
5614 struct elf_link_hash_entry **rel_hash)
5615 {
5616 unsigned int i;
5617 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5618 bfd_byte *erela;
5619 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5620 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5621 bfd_vma r_type_mask;
5622 int r_sym_shift;
5623
5624 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
5625 {
5626 swap_in = bed->s->swap_reloc_in;
5627 swap_out = bed->s->swap_reloc_out;
5628 }
5629 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
5630 {
5631 swap_in = bed->s->swap_reloca_in;
5632 swap_out = bed->s->swap_reloca_out;
5633 }
5634 else
5635 abort ();
5636
5637 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
5638 abort ();
5639
5640 if (bed->s->arch_size == 32)
5641 {
5642 r_type_mask = 0xff;
5643 r_sym_shift = 8;
5644 }
5645 else
5646 {
5647 r_type_mask = 0xffffffff;
5648 r_sym_shift = 32;
5649 }
5650
5651 erela = rel_hdr->contents;
5652 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
5653 {
5654 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
5655 unsigned int j;
5656
5657 if (*rel_hash == NULL)
5658 continue;
5659
5660 BFD_ASSERT ((*rel_hash)->indx >= 0);
5661
5662 (*swap_in) (abfd, erela, irela);
5663 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
5664 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
5665 | (irela[j].r_info & r_type_mask));
5666 (*swap_out) (abfd, irela, erela);
5667 }
5668 }
5669
5670 struct elf_link_sort_rela
5671 {
5672 union {
5673 bfd_vma offset;
5674 bfd_vma sym_mask;
5675 } u;
5676 enum elf_reloc_type_class type;
5677 /* We use this as an array of size int_rels_per_ext_rel. */
5678 Elf_Internal_Rela rela[1];
5679 };
5680
5681 static int
5682 elf_link_sort_cmp1 (const void *A, const void *B)
5683 {
5684 const struct elf_link_sort_rela *a = A;
5685 const struct elf_link_sort_rela *b = B;
5686 int relativea, relativeb;
5687
5688 relativea = a->type == reloc_class_relative;
5689 relativeb = b->type == reloc_class_relative;
5690
5691 if (relativea < relativeb)
5692 return 1;
5693 if (relativea > relativeb)
5694 return -1;
5695 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
5696 return -1;
5697 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
5698 return 1;
5699 if (a->rela->r_offset < b->rela->r_offset)
5700 return -1;
5701 if (a->rela->r_offset > b->rela->r_offset)
5702 return 1;
5703 return 0;
5704 }
5705
5706 static int
5707 elf_link_sort_cmp2 (const void *A, const void *B)
5708 {
5709 const struct elf_link_sort_rela *a = A;
5710 const struct elf_link_sort_rela *b = B;
5711 int copya, copyb;
5712
5713 if (a->u.offset < b->u.offset)
5714 return -1;
5715 if (a->u.offset > b->u.offset)
5716 return 1;
5717 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
5718 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
5719 if (copya < copyb)
5720 return -1;
5721 if (copya > copyb)
5722 return 1;
5723 if (a->rela->r_offset < b->rela->r_offset)
5724 return -1;
5725 if (a->rela->r_offset > b->rela->r_offset)
5726 return 1;
5727 return 0;
5728 }
5729
5730 static size_t
5731 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
5732 {
5733 asection *reldyn;
5734 bfd_size_type count, size;
5735 size_t i, ret, sort_elt, ext_size;
5736 bfd_byte *sort, *s_non_relative, *p;
5737 struct elf_link_sort_rela *sq;
5738 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5739 int i2e = bed->s->int_rels_per_ext_rel;
5740 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5741 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5742 struct bfd_link_order *lo;
5743 bfd_vma r_sym_mask;
5744
5745 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
5746 if (reldyn == NULL || reldyn->size == 0)
5747 {
5748 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
5749 if (reldyn == NULL || reldyn->size == 0)
5750 return 0;
5751 ext_size = bed->s->sizeof_rel;
5752 swap_in = bed->s->swap_reloc_in;
5753 swap_out = bed->s->swap_reloc_out;
5754 }
5755 else
5756 {
5757 ext_size = bed->s->sizeof_rela;
5758 swap_in = bed->s->swap_reloca_in;
5759 swap_out = bed->s->swap_reloca_out;
5760 }
5761 count = reldyn->size / ext_size;
5762
5763 size = 0;
5764 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5765 if (lo->type == bfd_indirect_link_order)
5766 {
5767 asection *o = lo->u.indirect.section;
5768 size += o->size;
5769 }
5770
5771 if (size != reldyn->size)
5772 return 0;
5773
5774 sort_elt = (sizeof (struct elf_link_sort_rela)
5775 + (i2e - 1) * sizeof (Elf_Internal_Rela));
5776 sort = bfd_zmalloc (sort_elt * count);
5777 if (sort == NULL)
5778 {
5779 (*info->callbacks->warning)
5780 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
5781 return 0;
5782 }
5783
5784 if (bed->s->arch_size == 32)
5785 r_sym_mask = ~(bfd_vma) 0xff;
5786 else
5787 r_sym_mask = ~(bfd_vma) 0xffffffff;
5788
5789 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5790 if (lo->type == bfd_indirect_link_order)
5791 {
5792 bfd_byte *erel, *erelend;
5793 asection *o = lo->u.indirect.section;
5794
5795 if (o->contents == NULL && o->size != 0)
5796 {
5797 /* This is a reloc section that is being handled as a normal
5798 section. See bfd_section_from_shdr. We can't combine
5799 relocs in this case. */
5800 free (sort);
5801 return 0;
5802 }
5803 erel = o->contents;
5804 erelend = o->contents + o->size;
5805 p = sort + o->output_offset / ext_size * sort_elt;
5806 while (erel < erelend)
5807 {
5808 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5809 (*swap_in) (abfd, erel, s->rela);
5810 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
5811 s->u.sym_mask = r_sym_mask;
5812 p += sort_elt;
5813 erel += ext_size;
5814 }
5815 }
5816
5817 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
5818
5819 for (i = 0, p = sort; i < count; i++, p += sort_elt)
5820 {
5821 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5822 if (s->type != reloc_class_relative)
5823 break;
5824 }
5825 ret = i;
5826 s_non_relative = p;
5827
5828 sq = (struct elf_link_sort_rela *) s_non_relative;
5829 for (; i < count; i++, p += sort_elt)
5830 {
5831 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
5832 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
5833 sq = sp;
5834 sp->u.offset = sq->rela->r_offset;
5835 }
5836
5837 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
5838
5839 for (lo = reldyn->link_order_head; lo != NULL; lo = lo->next)
5840 if (lo->type == bfd_indirect_link_order)
5841 {
5842 bfd_byte *erel, *erelend;
5843 asection *o = lo->u.indirect.section;
5844
5845 erel = o->contents;
5846 erelend = o->contents + o->size;
5847 p = sort + o->output_offset / ext_size * sort_elt;
5848 while (erel < erelend)
5849 {
5850 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
5851 (*swap_out) (abfd, s->rela, erel);
5852 p += sort_elt;
5853 erel += ext_size;
5854 }
5855 }
5856
5857 free (sort);
5858 *psec = reldyn;
5859 return ret;
5860 }
5861
5862 /* Flush the output symbols to the file. */
5863
5864 static bfd_boolean
5865 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
5866 const struct elf_backend_data *bed)
5867 {
5868 if (finfo->symbuf_count > 0)
5869 {
5870 Elf_Internal_Shdr *hdr;
5871 file_ptr pos;
5872 bfd_size_type amt;
5873
5874 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
5875 pos = hdr->sh_offset + hdr->sh_size;
5876 amt = finfo->symbuf_count * bed->s->sizeof_sym;
5877 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
5878 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
5879 return FALSE;
5880
5881 hdr->sh_size += amt;
5882 finfo->symbuf_count = 0;
5883 }
5884
5885 return TRUE;
5886 }
5887
5888 /* Add a symbol to the output symbol table. */
5889
5890 static bfd_boolean
5891 elf_link_output_sym (struct elf_final_link_info *finfo,
5892 const char *name,
5893 Elf_Internal_Sym *elfsym,
5894 asection *input_sec,
5895 struct elf_link_hash_entry *h)
5896 {
5897 bfd_byte *dest;
5898 Elf_External_Sym_Shndx *destshndx;
5899 bfd_boolean (*output_symbol_hook)
5900 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
5901 struct elf_link_hash_entry *);
5902 const struct elf_backend_data *bed;
5903
5904 bed = get_elf_backend_data (finfo->output_bfd);
5905 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
5906 if (output_symbol_hook != NULL)
5907 {
5908 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
5909 return FALSE;
5910 }
5911
5912 if (name == NULL || *name == '\0')
5913 elfsym->st_name = 0;
5914 else if (input_sec->flags & SEC_EXCLUDE)
5915 elfsym->st_name = 0;
5916 else
5917 {
5918 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
5919 name, TRUE, FALSE);
5920 if (elfsym->st_name == (unsigned long) -1)
5921 return FALSE;
5922 }
5923
5924 if (finfo->symbuf_count >= finfo->symbuf_size)
5925 {
5926 if (! elf_link_flush_output_syms (finfo, bed))
5927 return FALSE;
5928 }
5929
5930 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
5931 destshndx = finfo->symshndxbuf;
5932 if (destshndx != NULL)
5933 {
5934 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
5935 {
5936 bfd_size_type amt;
5937
5938 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
5939 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
5940 if (destshndx == NULL)
5941 return FALSE;
5942 memset ((char *) destshndx + amt, 0, amt);
5943 finfo->shndxbuf_size *= 2;
5944 }
5945 destshndx += bfd_get_symcount (finfo->output_bfd);
5946 }
5947
5948 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
5949 finfo->symbuf_count += 1;
5950 bfd_get_symcount (finfo->output_bfd) += 1;
5951
5952 return TRUE;
5953 }
5954
5955 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
5956 allowing an unsatisfied unversioned symbol in the DSO to match a
5957 versioned symbol that would normally require an explicit version.
5958 We also handle the case that a DSO references a hidden symbol
5959 which may be satisfied by a versioned symbol in another DSO. */
5960
5961 static bfd_boolean
5962 elf_link_check_versioned_symbol (struct bfd_link_info *info,
5963 const struct elf_backend_data *bed,
5964 struct elf_link_hash_entry *h)
5965 {
5966 bfd *abfd;
5967 struct elf_link_loaded_list *loaded;
5968
5969 if (!is_elf_hash_table (info->hash))
5970 return FALSE;
5971
5972 switch (h->root.type)
5973 {
5974 default:
5975 abfd = NULL;
5976 break;
5977
5978 case bfd_link_hash_undefined:
5979 case bfd_link_hash_undefweak:
5980 abfd = h->root.u.undef.abfd;
5981 if ((abfd->flags & DYNAMIC) == 0
5982 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
5983 return FALSE;
5984 break;
5985
5986 case bfd_link_hash_defined:
5987 case bfd_link_hash_defweak:
5988 abfd = h->root.u.def.section->owner;
5989 break;
5990
5991 case bfd_link_hash_common:
5992 abfd = h->root.u.c.p->section->owner;
5993 break;
5994 }
5995 BFD_ASSERT (abfd != NULL);
5996
5997 for (loaded = elf_hash_table (info)->loaded;
5998 loaded != NULL;
5999 loaded = loaded->next)
6000 {
6001 bfd *input;
6002 Elf_Internal_Shdr *hdr;
6003 bfd_size_type symcount;
6004 bfd_size_type extsymcount;
6005 bfd_size_type extsymoff;
6006 Elf_Internal_Shdr *versymhdr;
6007 Elf_Internal_Sym *isym;
6008 Elf_Internal_Sym *isymend;
6009 Elf_Internal_Sym *isymbuf;
6010 Elf_External_Versym *ever;
6011 Elf_External_Versym *extversym;
6012
6013 input = loaded->abfd;
6014
6015 /* We check each DSO for a possible hidden versioned definition. */
6016 if (input == abfd
6017 || (input->flags & DYNAMIC) == 0
6018 || elf_dynversym (input) == 0)
6019 continue;
6020
6021 hdr = &elf_tdata (input)->dynsymtab_hdr;
6022
6023 symcount = hdr->sh_size / bed->s->sizeof_sym;
6024 if (elf_bad_symtab (input))
6025 {
6026 extsymcount = symcount;
6027 extsymoff = 0;
6028 }
6029 else
6030 {
6031 extsymcount = symcount - hdr->sh_info;
6032 extsymoff = hdr->sh_info;
6033 }
6034
6035 if (extsymcount == 0)
6036 continue;
6037
6038 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
6039 NULL, NULL, NULL);
6040 if (isymbuf == NULL)
6041 return FALSE;
6042
6043 /* Read in any version definitions. */
6044 versymhdr = &elf_tdata (input)->dynversym_hdr;
6045 extversym = bfd_malloc (versymhdr->sh_size);
6046 if (extversym == NULL)
6047 goto error_ret;
6048
6049 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
6050 || (bfd_bread (extversym, versymhdr->sh_size, input)
6051 != versymhdr->sh_size))
6052 {
6053 free (extversym);
6054 error_ret:
6055 free (isymbuf);
6056 return FALSE;
6057 }
6058
6059 ever = extversym + extsymoff;
6060 isymend = isymbuf + extsymcount;
6061 for (isym = isymbuf; isym < isymend; isym++, ever++)
6062 {
6063 const char *name;
6064 Elf_Internal_Versym iver;
6065 unsigned short version_index;
6066
6067 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
6068 || isym->st_shndx == SHN_UNDEF)
6069 continue;
6070
6071 name = bfd_elf_string_from_elf_section (input,
6072 hdr->sh_link,
6073 isym->st_name);
6074 if (strcmp (name, h->root.root.string) != 0)
6075 continue;
6076
6077 _bfd_elf_swap_versym_in (input, ever, &iver);
6078
6079 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
6080 {
6081 /* If we have a non-hidden versioned sym, then it should
6082 have provided a definition for the undefined sym. */
6083 abort ();
6084 }
6085
6086 version_index = iver.vs_vers & VERSYM_VERSION;
6087 if (version_index == 1 || version_index == 2)
6088 {
6089 /* This is the base or first version. We can use it. */
6090 free (extversym);
6091 free (isymbuf);
6092 return TRUE;
6093 }
6094 }
6095
6096 free (extversym);
6097 free (isymbuf);
6098 }
6099
6100 return FALSE;
6101 }
6102
6103 /* Add an external symbol to the symbol table. This is called from
6104 the hash table traversal routine. When generating a shared object,
6105 we go through the symbol table twice. The first time we output
6106 anything that might have been forced to local scope in a version
6107 script. The second time we output the symbols that are still
6108 global symbols. */
6109
6110 static bfd_boolean
6111 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
6112 {
6113 struct elf_outext_info *eoinfo = data;
6114 struct elf_final_link_info *finfo = eoinfo->finfo;
6115 bfd_boolean strip;
6116 Elf_Internal_Sym sym;
6117 asection *input_sec;
6118 const struct elf_backend_data *bed;
6119
6120 if (h->root.type == bfd_link_hash_warning)
6121 {
6122 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6123 if (h->root.type == bfd_link_hash_new)
6124 return TRUE;
6125 }
6126
6127 /* Decide whether to output this symbol in this pass. */
6128 if (eoinfo->localsyms)
6129 {
6130 if (!h->forced_local)
6131 return TRUE;
6132 }
6133 else
6134 {
6135 if (h->forced_local)
6136 return TRUE;
6137 }
6138
6139 bed = get_elf_backend_data (finfo->output_bfd);
6140
6141 /* If we have an undefined symbol reference here then it must have
6142 come from a shared library that is being linked in. (Undefined
6143 references in regular files have already been handled). If we
6144 are reporting errors for this situation then do so now. */
6145 if (h->root.type == bfd_link_hash_undefined
6146 && h->ref_dynamic
6147 && !h->ref_regular
6148 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
6149 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
6150 {
6151 if (! ((*finfo->info->callbacks->undefined_symbol)
6152 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
6153 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
6154 {
6155 eoinfo->failed = TRUE;
6156 return FALSE;
6157 }
6158 }
6159
6160 /* We should also warn if a forced local symbol is referenced from
6161 shared libraries. */
6162 if (! finfo->info->relocatable
6163 && (! finfo->info->shared)
6164 && h->forced_local
6165 && h->ref_dynamic
6166 && !h->dynamic_def
6167 && !h->dynamic_weak
6168 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
6169 {
6170 (*_bfd_error_handler)
6171 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6172 finfo->output_bfd, h->root.u.def.section->owner,
6173 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
6174 ? "internal"
6175 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
6176 ? "hidden" : "local",
6177 h->root.root.string);
6178 eoinfo->failed = TRUE;
6179 return FALSE;
6180 }
6181
6182 /* We don't want to output symbols that have never been mentioned by
6183 a regular file, or that we have been told to strip. However, if
6184 h->indx is set to -2, the symbol is used by a reloc and we must
6185 output it. */
6186 if (h->indx == -2)
6187 strip = FALSE;
6188 else if ((h->def_dynamic
6189 || h->ref_dynamic)
6190 && !h->def_regular
6191 && !h->ref_regular)
6192 strip = TRUE;
6193 else if (finfo->info->strip == strip_all)
6194 strip = TRUE;
6195 else if (finfo->info->strip == strip_some
6196 && bfd_hash_lookup (finfo->info->keep_hash,
6197 h->root.root.string, FALSE, FALSE) == NULL)
6198 strip = TRUE;
6199 else if (finfo->info->strip_discarded
6200 && (h->root.type == bfd_link_hash_defined
6201 || h->root.type == bfd_link_hash_defweak)
6202 && elf_discarded_section (h->root.u.def.section))
6203 strip = TRUE;
6204 else
6205 strip = FALSE;
6206
6207 /* If we're stripping it, and it's not a dynamic symbol, there's
6208 nothing else to do unless it is a forced local symbol. */
6209 if (strip
6210 && h->dynindx == -1
6211 && !h->forced_local)
6212 return TRUE;
6213
6214 sym.st_value = 0;
6215 sym.st_size = h->size;
6216 sym.st_other = h->other;
6217 if (h->forced_local)
6218 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
6219 else if (h->root.type == bfd_link_hash_undefweak
6220 || h->root.type == bfd_link_hash_defweak)
6221 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
6222 else
6223 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
6224
6225 switch (h->root.type)
6226 {
6227 default:
6228 case bfd_link_hash_new:
6229 case bfd_link_hash_warning:
6230 abort ();
6231 return FALSE;
6232
6233 case bfd_link_hash_undefined:
6234 case bfd_link_hash_undefweak:
6235 input_sec = bfd_und_section_ptr;
6236 sym.st_shndx = SHN_UNDEF;
6237 break;
6238
6239 case bfd_link_hash_defined:
6240 case bfd_link_hash_defweak:
6241 {
6242 input_sec = h->root.u.def.section;
6243 if (input_sec->output_section != NULL)
6244 {
6245 sym.st_shndx =
6246 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
6247 input_sec->output_section);
6248 if (sym.st_shndx == SHN_BAD)
6249 {
6250 (*_bfd_error_handler)
6251 (_("%B: could not find output section %A for input section %A"),
6252 finfo->output_bfd, input_sec->output_section, input_sec);
6253 eoinfo->failed = TRUE;
6254 return FALSE;
6255 }
6256
6257 /* ELF symbols in relocatable files are section relative,
6258 but in nonrelocatable files they are virtual
6259 addresses. */
6260 sym.st_value = h->root.u.def.value + input_sec->output_offset;
6261 if (! finfo->info->relocatable)
6262 {
6263 sym.st_value += input_sec->output_section->vma;
6264 if (h->type == STT_TLS)
6265 {
6266 /* STT_TLS symbols are relative to PT_TLS segment
6267 base. */
6268 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6269 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6270 }
6271 }
6272 }
6273 else
6274 {
6275 BFD_ASSERT (input_sec->owner == NULL
6276 || (input_sec->owner->flags & DYNAMIC) != 0);
6277 sym.st_shndx = SHN_UNDEF;
6278 input_sec = bfd_und_section_ptr;
6279 }
6280 }
6281 break;
6282
6283 case bfd_link_hash_common:
6284 input_sec = h->root.u.c.p->section;
6285 sym.st_shndx = SHN_COMMON;
6286 sym.st_value = 1 << h->root.u.c.p->alignment_power;
6287 break;
6288
6289 case bfd_link_hash_indirect:
6290 /* These symbols are created by symbol versioning. They point
6291 to the decorated version of the name. For example, if the
6292 symbol foo@@GNU_1.2 is the default, which should be used when
6293 foo is used with no version, then we add an indirect symbol
6294 foo which points to foo@@GNU_1.2. We ignore these symbols,
6295 since the indirected symbol is already in the hash table. */
6296 return TRUE;
6297 }
6298
6299 /* Give the processor backend a chance to tweak the symbol value,
6300 and also to finish up anything that needs to be done for this
6301 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6302 forced local syms when non-shared is due to a historical quirk. */
6303 if ((h->dynindx != -1
6304 || h->forced_local)
6305 && ((finfo->info->shared
6306 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6307 || h->root.type != bfd_link_hash_undefweak))
6308 || !h->forced_local)
6309 && elf_hash_table (finfo->info)->dynamic_sections_created)
6310 {
6311 if (! ((*bed->elf_backend_finish_dynamic_symbol)
6312 (finfo->output_bfd, finfo->info, h, &sym)))
6313 {
6314 eoinfo->failed = TRUE;
6315 return FALSE;
6316 }
6317 }
6318
6319 /* If we are marking the symbol as undefined, and there are no
6320 non-weak references to this symbol from a regular object, then
6321 mark the symbol as weak undefined; if there are non-weak
6322 references, mark the symbol as strong. We can't do this earlier,
6323 because it might not be marked as undefined until the
6324 finish_dynamic_symbol routine gets through with it. */
6325 if (sym.st_shndx == SHN_UNDEF
6326 && h->ref_regular
6327 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
6328 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
6329 {
6330 int bindtype;
6331
6332 if (h->ref_regular_nonweak)
6333 bindtype = STB_GLOBAL;
6334 else
6335 bindtype = STB_WEAK;
6336 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
6337 }
6338
6339 /* If a non-weak symbol with non-default visibility is not defined
6340 locally, it is a fatal error. */
6341 if (! finfo->info->relocatable
6342 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
6343 && ELF_ST_BIND (sym.st_info) != STB_WEAK
6344 && h->root.type == bfd_link_hash_undefined
6345 && !h->def_regular)
6346 {
6347 (*_bfd_error_handler)
6348 (_("%B: %s symbol `%s' isn't defined"),
6349 finfo->output_bfd,
6350 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
6351 ? "protected"
6352 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
6353 ? "internal" : "hidden",
6354 h->root.root.string);
6355 eoinfo->failed = TRUE;
6356 return FALSE;
6357 }
6358
6359 /* If this symbol should be put in the .dynsym section, then put it
6360 there now. We already know the symbol index. We also fill in
6361 the entry in the .hash section. */
6362 if (h->dynindx != -1
6363 && elf_hash_table (finfo->info)->dynamic_sections_created)
6364 {
6365 size_t bucketcount;
6366 size_t bucket;
6367 size_t hash_entry_size;
6368 bfd_byte *bucketpos;
6369 bfd_vma chain;
6370 bfd_byte *esym;
6371
6372 sym.st_name = h->dynstr_index;
6373 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
6374 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
6375
6376 bucketcount = elf_hash_table (finfo->info)->bucketcount;
6377 bucket = h->u.elf_hash_value % bucketcount;
6378 hash_entry_size
6379 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
6380 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
6381 + (bucket + 2) * hash_entry_size);
6382 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
6383 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
6384 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
6385 ((bfd_byte *) finfo->hash_sec->contents
6386 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
6387
6388 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
6389 {
6390 Elf_Internal_Versym iversym;
6391 Elf_External_Versym *eversym;
6392
6393 if (!h->def_regular)
6394 {
6395 if (h->verinfo.verdef == NULL)
6396 iversym.vs_vers = 0;
6397 else
6398 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
6399 }
6400 else
6401 {
6402 if (h->verinfo.vertree == NULL)
6403 iversym.vs_vers = 1;
6404 else
6405 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
6406 if (finfo->info->create_default_symver)
6407 iversym.vs_vers++;
6408 }
6409
6410 if (h->hidden)
6411 iversym.vs_vers |= VERSYM_HIDDEN;
6412
6413 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
6414 eversym += h->dynindx;
6415 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
6416 }
6417 }
6418
6419 /* If we're stripping it, then it was just a dynamic symbol, and
6420 there's nothing else to do. */
6421 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
6422 return TRUE;
6423
6424 h->indx = bfd_get_symcount (finfo->output_bfd);
6425
6426 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
6427 {
6428 eoinfo->failed = TRUE;
6429 return FALSE;
6430 }
6431
6432 return TRUE;
6433 }
6434
6435 /* Return TRUE if special handling is done for relocs in SEC against
6436 symbols defined in discarded sections. */
6437
6438 static bfd_boolean
6439 elf_section_ignore_discarded_relocs (asection *sec)
6440 {
6441 const struct elf_backend_data *bed;
6442
6443 switch (sec->sec_info_type)
6444 {
6445 case ELF_INFO_TYPE_STABS:
6446 case ELF_INFO_TYPE_EH_FRAME:
6447 return TRUE;
6448 default:
6449 break;
6450 }
6451
6452 bed = get_elf_backend_data (sec->owner);
6453 if (bed->elf_backend_ignore_discarded_relocs != NULL
6454 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
6455 return TRUE;
6456
6457 return FALSE;
6458 }
6459
6460 enum action_discarded
6461 {
6462 COMPLAIN = 1,
6463 PRETEND = 2
6464 };
6465
6466 /* Return a mask saying how ld should treat relocations in SEC against
6467 symbols defined in discarded sections. If this function returns
6468 COMPLAIN set, ld will issue a warning message. If this function
6469 returns PRETEND set, and the discarded section was link-once and the
6470 same size as the kept link-once section, ld will pretend that the
6471 symbol was actually defined in the kept section. Otherwise ld will
6472 zero the reloc (at least that is the intent, but some cooperation by
6473 the target dependent code is needed, particularly for REL targets). */
6474
6475 static unsigned int
6476 elf_action_discarded (asection *sec)
6477 {
6478 if (sec->flags & SEC_DEBUGGING)
6479 return PRETEND;
6480
6481 if (strcmp (".eh_frame", sec->name) == 0)
6482 return 0;
6483
6484 if (strcmp (".gcc_except_table", sec->name) == 0)
6485 return 0;
6486
6487 if (strcmp (".PARISC.unwind", sec->name) == 0)
6488 return 0;
6489
6490 if (strcmp (".fixup", sec->name) == 0)
6491 return 0;
6492
6493 return COMPLAIN | PRETEND;
6494 }
6495
6496 /* Find a match between a section and a member of a section group. */
6497
6498 static asection *
6499 match_group_member (asection *sec, asection *group)
6500 {
6501 asection *first = elf_next_in_group (group);
6502 asection *s = first;
6503
6504 while (s != NULL)
6505 {
6506 if (bfd_elf_match_symbols_in_sections (s, sec))
6507 return s;
6508
6509 if (s == first)
6510 break;
6511 }
6512
6513 return NULL;
6514 }
6515
6516 /* Link an input file into the linker output file. This function
6517 handles all the sections and relocations of the input file at once.
6518 This is so that we only have to read the local symbols once, and
6519 don't have to keep them in memory. */
6520
6521 static bfd_boolean
6522 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
6523 {
6524 bfd_boolean (*relocate_section)
6525 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
6526 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
6527 bfd *output_bfd;
6528 Elf_Internal_Shdr *symtab_hdr;
6529 size_t locsymcount;
6530 size_t extsymoff;
6531 Elf_Internal_Sym *isymbuf;
6532 Elf_Internal_Sym *isym;
6533 Elf_Internal_Sym *isymend;
6534 long *pindex;
6535 asection **ppsection;
6536 asection *o;
6537 const struct elf_backend_data *bed;
6538 bfd_boolean emit_relocs;
6539 struct elf_link_hash_entry **sym_hashes;
6540
6541 output_bfd = finfo->output_bfd;
6542 bed = get_elf_backend_data (output_bfd);
6543 relocate_section = bed->elf_backend_relocate_section;
6544
6545 /* If this is a dynamic object, we don't want to do anything here:
6546 we don't want the local symbols, and we don't want the section
6547 contents. */
6548 if ((input_bfd->flags & DYNAMIC) != 0)
6549 return TRUE;
6550
6551 emit_relocs = (finfo->info->relocatable
6552 || finfo->info->emitrelocations
6553 || bed->elf_backend_emit_relocs);
6554
6555 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6556 if (elf_bad_symtab (input_bfd))
6557 {
6558 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
6559 extsymoff = 0;
6560 }
6561 else
6562 {
6563 locsymcount = symtab_hdr->sh_info;
6564 extsymoff = symtab_hdr->sh_info;
6565 }
6566
6567 /* Read the local symbols. */
6568 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6569 if (isymbuf == NULL && locsymcount != 0)
6570 {
6571 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
6572 finfo->internal_syms,
6573 finfo->external_syms,
6574 finfo->locsym_shndx);
6575 if (isymbuf == NULL)
6576 return FALSE;
6577 }
6578
6579 /* Find local symbol sections and adjust values of symbols in
6580 SEC_MERGE sections. Write out those local symbols we know are
6581 going into the output file. */
6582 isymend = isymbuf + locsymcount;
6583 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
6584 isym < isymend;
6585 isym++, pindex++, ppsection++)
6586 {
6587 asection *isec;
6588 const char *name;
6589 Elf_Internal_Sym osym;
6590
6591 *pindex = -1;
6592
6593 if (elf_bad_symtab (input_bfd))
6594 {
6595 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
6596 {
6597 *ppsection = NULL;
6598 continue;
6599 }
6600 }
6601
6602 if (isym->st_shndx == SHN_UNDEF)
6603 isec = bfd_und_section_ptr;
6604 else if (isym->st_shndx < SHN_LORESERVE
6605 || isym->st_shndx > SHN_HIRESERVE)
6606 {
6607 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
6608 if (isec
6609 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
6610 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
6611 isym->st_value =
6612 _bfd_merged_section_offset (output_bfd, &isec,
6613 elf_section_data (isec)->sec_info,
6614 isym->st_value);
6615 }
6616 else if (isym->st_shndx == SHN_ABS)
6617 isec = bfd_abs_section_ptr;
6618 else if (isym->st_shndx == SHN_COMMON)
6619 isec = bfd_com_section_ptr;
6620 else
6621 {
6622 /* Who knows? */
6623 isec = NULL;
6624 }
6625
6626 *ppsection = isec;
6627
6628 /* Don't output the first, undefined, symbol. */
6629 if (ppsection == finfo->sections)
6630 continue;
6631
6632 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6633 {
6634 /* We never output section symbols. Instead, we use the
6635 section symbol of the corresponding section in the output
6636 file. */
6637 continue;
6638 }
6639
6640 /* If we are stripping all symbols, we don't want to output this
6641 one. */
6642 if (finfo->info->strip == strip_all)
6643 continue;
6644
6645 /* If we are discarding all local symbols, we don't want to
6646 output this one. If we are generating a relocatable output
6647 file, then some of the local symbols may be required by
6648 relocs; we output them below as we discover that they are
6649 needed. */
6650 if (finfo->info->discard == discard_all)
6651 continue;
6652
6653 /* If this symbol is defined in a section which we are
6654 discarding, we don't need to keep it, but note that
6655 linker_mark is only reliable for sections that have contents.
6656 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6657 as well as linker_mark. */
6658 if ((isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
6659 && isec != NULL
6660 && ((! isec->linker_mark && (isec->flags & SEC_HAS_CONTENTS) != 0)
6661 || (! finfo->info->relocatable
6662 && (isec->flags & SEC_EXCLUDE) != 0)))
6663 continue;
6664
6665 /* Get the name of the symbol. */
6666 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
6667 isym->st_name);
6668 if (name == NULL)
6669 return FALSE;
6670
6671 /* See if we are discarding symbols with this name. */
6672 if ((finfo->info->strip == strip_some
6673 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
6674 == NULL))
6675 || (((finfo->info->discard == discard_sec_merge
6676 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
6677 || finfo->info->discard == discard_l)
6678 && bfd_is_local_label_name (input_bfd, name)))
6679 continue;
6680
6681 /* If we get here, we are going to output this symbol. */
6682
6683 osym = *isym;
6684
6685 /* Adjust the section index for the output file. */
6686 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
6687 isec->output_section);
6688 if (osym.st_shndx == SHN_BAD)
6689 return FALSE;
6690
6691 *pindex = bfd_get_symcount (output_bfd);
6692
6693 /* ELF symbols in relocatable files are section relative, but
6694 in executable files they are virtual addresses. Note that
6695 this code assumes that all ELF sections have an associated
6696 BFD section with a reasonable value for output_offset; below
6697 we assume that they also have a reasonable value for
6698 output_section. Any special sections must be set up to meet
6699 these requirements. */
6700 osym.st_value += isec->output_offset;
6701 if (! finfo->info->relocatable)
6702 {
6703 osym.st_value += isec->output_section->vma;
6704 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
6705 {
6706 /* STT_TLS symbols are relative to PT_TLS segment base. */
6707 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6708 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6709 }
6710 }
6711
6712 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
6713 return FALSE;
6714 }
6715
6716 /* Relocate the contents of each section. */
6717 sym_hashes = elf_sym_hashes (input_bfd);
6718 for (o = input_bfd->sections; o != NULL; o = o->next)
6719 {
6720 bfd_byte *contents;
6721
6722 if (! o->linker_mark)
6723 {
6724 /* This section was omitted from the link. */
6725 continue;
6726 }
6727
6728 if ((o->flags & SEC_HAS_CONTENTS) == 0
6729 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
6730 continue;
6731
6732 if ((o->flags & SEC_LINKER_CREATED) != 0)
6733 {
6734 /* Section was created by _bfd_elf_link_create_dynamic_sections
6735 or somesuch. */
6736 continue;
6737 }
6738
6739 /* Get the contents of the section. They have been cached by a
6740 relaxation routine. Note that o is a section in an input
6741 file, so the contents field will not have been set by any of
6742 the routines which work on output files. */
6743 if (elf_section_data (o)->this_hdr.contents != NULL)
6744 contents = elf_section_data (o)->this_hdr.contents;
6745 else
6746 {
6747 bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
6748
6749 contents = finfo->contents;
6750 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
6751 return FALSE;
6752 }
6753
6754 if ((o->flags & SEC_RELOC) != 0)
6755 {
6756 Elf_Internal_Rela *internal_relocs;
6757 bfd_vma r_type_mask;
6758 int r_sym_shift;
6759
6760 /* Get the swapped relocs. */
6761 internal_relocs
6762 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
6763 finfo->internal_relocs, FALSE);
6764 if (internal_relocs == NULL
6765 && o->reloc_count > 0)
6766 return FALSE;
6767
6768 if (bed->s->arch_size == 32)
6769 {
6770 r_type_mask = 0xff;
6771 r_sym_shift = 8;
6772 }
6773 else
6774 {
6775 r_type_mask = 0xffffffff;
6776 r_sym_shift = 32;
6777 }
6778
6779 /* Run through the relocs looking for any against symbols
6780 from discarded sections and section symbols from
6781 removed link-once sections. Complain about relocs
6782 against discarded sections. Zero relocs against removed
6783 link-once sections. Preserve debug information as much
6784 as we can. */
6785 if (!elf_section_ignore_discarded_relocs (o))
6786 {
6787 Elf_Internal_Rela *rel, *relend;
6788 unsigned int action = elf_action_discarded (o);
6789
6790 rel = internal_relocs;
6791 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
6792 for ( ; rel < relend; rel++)
6793 {
6794 unsigned long r_symndx = rel->r_info >> r_sym_shift;
6795 asection **ps, *sec;
6796 struct elf_link_hash_entry *h = NULL;
6797 const char *sym_name;
6798
6799 if (r_symndx == STN_UNDEF)
6800 continue;
6801
6802 if (r_symndx >= locsymcount
6803 || (elf_bad_symtab (input_bfd)
6804 && finfo->sections[r_symndx] == NULL))
6805 {
6806 h = sym_hashes[r_symndx - extsymoff];
6807 while (h->root.type == bfd_link_hash_indirect
6808 || h->root.type == bfd_link_hash_warning)
6809 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6810
6811 if (h->root.type != bfd_link_hash_defined
6812 && h->root.type != bfd_link_hash_defweak)
6813 continue;
6814
6815 ps = &h->root.u.def.section;
6816 sym_name = h->root.root.string;
6817 }
6818 else
6819 {
6820 Elf_Internal_Sym *sym = isymbuf + r_symndx;
6821 ps = &finfo->sections[r_symndx];
6822 sym_name = bfd_elf_local_sym_name (input_bfd, sym);
6823 }
6824
6825 /* Complain if the definition comes from a
6826 discarded section. */
6827 if ((sec = *ps) != NULL && elf_discarded_section (sec))
6828 {
6829 asection *kept;
6830
6831 BFD_ASSERT (r_symndx != 0);
6832 if (action & COMPLAIN)
6833 {
6834 (*_bfd_error_handler)
6835 (_("`%s' referenced in section `%A' of %B: "
6836 "defined in discarded section `%A' of %B\n"),
6837 o, input_bfd, sec, sec->owner, sym_name);
6838 }
6839
6840 /* Try to do the best we can to support buggy old
6841 versions of gcc. If we've warned, or this is
6842 debugging info, pretend that the symbol is
6843 really defined in the kept linkonce section.
6844 FIXME: This is quite broken. Modifying the
6845 symbol here means we will be changing all later
6846 uses of the symbol, not just in this section.
6847 The only thing that makes this half reasonable
6848 is that we warn in non-debug sections, and
6849 debug sections tend to come after other
6850 sections. */
6851 kept = sec->kept_section;
6852 if (kept != NULL && (action & PRETEND))
6853 {
6854 if (elf_sec_group (sec) != NULL)
6855 kept = match_group_member (sec, kept);
6856 if (kept != NULL
6857 && sec->size == kept->size)
6858 {
6859 *ps = kept;
6860 continue;
6861 }
6862 }
6863
6864 /* Remove the symbol reference from the reloc, but
6865 don't kill the reloc completely. This is so that
6866 a zero value will be written into the section,
6867 which may have non-zero contents put there by the
6868 assembler. Zero in things like an eh_frame fde
6869 pc_begin allows stack unwinders to recognize the
6870 fde as bogus. */
6871 rel->r_info &= r_type_mask;
6872 rel->r_addend = 0;
6873 }
6874 }
6875 }
6876
6877 /* Relocate the section by invoking a back end routine.
6878
6879 The back end routine is responsible for adjusting the
6880 section contents as necessary, and (if using Rela relocs
6881 and generating a relocatable output file) adjusting the
6882 reloc addend as necessary.
6883
6884 The back end routine does not have to worry about setting
6885 the reloc address or the reloc symbol index.
6886
6887 The back end routine is given a pointer to the swapped in
6888 internal symbols, and can access the hash table entries
6889 for the external symbols via elf_sym_hashes (input_bfd).
6890
6891 When generating relocatable output, the back end routine
6892 must handle STB_LOCAL/STT_SECTION symbols specially. The
6893 output symbol is going to be a section symbol
6894 corresponding to the output section, which will require
6895 the addend to be adjusted. */
6896
6897 if (! (*relocate_section) (output_bfd, finfo->info,
6898 input_bfd, o, contents,
6899 internal_relocs,
6900 isymbuf,
6901 finfo->sections))
6902 return FALSE;
6903
6904 if (emit_relocs)
6905 {
6906 Elf_Internal_Rela *irela;
6907 Elf_Internal_Rela *irelaend;
6908 bfd_vma last_offset;
6909 struct elf_link_hash_entry **rel_hash;
6910 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
6911 unsigned int next_erel;
6912 bfd_boolean (*reloc_emitter)
6913 (bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *);
6914 bfd_boolean rela_normal;
6915
6916 input_rel_hdr = &elf_section_data (o)->rel_hdr;
6917 rela_normal = (bed->rela_normal
6918 && (input_rel_hdr->sh_entsize
6919 == bed->s->sizeof_rela));
6920
6921 /* Adjust the reloc addresses and symbol indices. */
6922
6923 irela = internal_relocs;
6924 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
6925 rel_hash = (elf_section_data (o->output_section)->rel_hashes
6926 + elf_section_data (o->output_section)->rel_count
6927 + elf_section_data (o->output_section)->rel_count2);
6928 last_offset = o->output_offset;
6929 if (!finfo->info->relocatable)
6930 last_offset += o->output_section->vma;
6931 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
6932 {
6933 unsigned long r_symndx;
6934 asection *sec;
6935 Elf_Internal_Sym sym;
6936
6937 if (next_erel == bed->s->int_rels_per_ext_rel)
6938 {
6939 rel_hash++;
6940 next_erel = 0;
6941 }
6942
6943 irela->r_offset = _bfd_elf_section_offset (output_bfd,
6944 finfo->info, o,
6945 irela->r_offset);
6946 if (irela->r_offset >= (bfd_vma) -2)
6947 {
6948 /* This is a reloc for a deleted entry or somesuch.
6949 Turn it into an R_*_NONE reloc, at the same
6950 offset as the last reloc. elf_eh_frame.c and
6951 elf_bfd_discard_info rely on reloc offsets
6952 being ordered. */
6953 irela->r_offset = last_offset;
6954 irela->r_info = 0;
6955 irela->r_addend = 0;
6956 continue;
6957 }
6958
6959 irela->r_offset += o->output_offset;
6960
6961 /* Relocs in an executable have to be virtual addresses. */
6962 if (!finfo->info->relocatable)
6963 irela->r_offset += o->output_section->vma;
6964
6965 last_offset = irela->r_offset;
6966
6967 r_symndx = irela->r_info >> r_sym_shift;
6968 if (r_symndx == STN_UNDEF)
6969 continue;
6970
6971 if (r_symndx >= locsymcount
6972 || (elf_bad_symtab (input_bfd)
6973 && finfo->sections[r_symndx] == NULL))
6974 {
6975 struct elf_link_hash_entry *rh;
6976 unsigned long indx;
6977
6978 /* This is a reloc against a global symbol. We
6979 have not yet output all the local symbols, so
6980 we do not know the symbol index of any global
6981 symbol. We set the rel_hash entry for this
6982 reloc to point to the global hash table entry
6983 for this symbol. The symbol index is then
6984 set at the end of bfd_elf_final_link. */
6985 indx = r_symndx - extsymoff;
6986 rh = elf_sym_hashes (input_bfd)[indx];
6987 while (rh->root.type == bfd_link_hash_indirect
6988 || rh->root.type == bfd_link_hash_warning)
6989 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
6990
6991 /* Setting the index to -2 tells
6992 elf_link_output_extsym that this symbol is
6993 used by a reloc. */
6994 BFD_ASSERT (rh->indx < 0);
6995 rh->indx = -2;
6996
6997 *rel_hash = rh;
6998
6999 continue;
7000 }
7001
7002 /* This is a reloc against a local symbol. */
7003
7004 *rel_hash = NULL;
7005 sym = isymbuf[r_symndx];
7006 sec = finfo->sections[r_symndx];
7007 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
7008 {
7009 /* I suppose the backend ought to fill in the
7010 section of any STT_SECTION symbol against a
7011 processor specific section. */
7012 r_symndx = 0;
7013 if (bfd_is_abs_section (sec))
7014 ;
7015 else if (sec == NULL || sec->owner == NULL)
7016 {
7017 bfd_set_error (bfd_error_bad_value);
7018 return FALSE;
7019 }
7020 else
7021 {
7022 asection *osec = sec->output_section;
7023
7024 /* If we have discarded a section, the output
7025 section will be the absolute section. In
7026 case of discarded link-once and discarded
7027 SEC_MERGE sections, use the kept section. */
7028 if (bfd_is_abs_section (osec)
7029 && sec->kept_section != NULL
7030 && sec->kept_section->output_section != NULL)
7031 {
7032 osec = sec->kept_section->output_section;
7033 irela->r_addend -= osec->vma;
7034 }
7035
7036 if (!bfd_is_abs_section (osec))
7037 {
7038 r_symndx = osec->target_index;
7039 BFD_ASSERT (r_symndx != 0);
7040 }
7041 }
7042
7043 /* Adjust the addend according to where the
7044 section winds up in the output section. */
7045 if (rela_normal)
7046 irela->r_addend += sec->output_offset;
7047 }
7048 else
7049 {
7050 if (finfo->indices[r_symndx] == -1)
7051 {
7052 unsigned long shlink;
7053 const char *name;
7054 asection *osec;
7055
7056 if (finfo->info->strip == strip_all)
7057 {
7058 /* You can't do ld -r -s. */
7059 bfd_set_error (bfd_error_invalid_operation);
7060 return FALSE;
7061 }
7062
7063 /* This symbol was skipped earlier, but
7064 since it is needed by a reloc, we
7065 must output it now. */
7066 shlink = symtab_hdr->sh_link;
7067 name = (bfd_elf_string_from_elf_section
7068 (input_bfd, shlink, sym.st_name));
7069 if (name == NULL)
7070 return FALSE;
7071
7072 osec = sec->output_section;
7073 sym.st_shndx =
7074 _bfd_elf_section_from_bfd_section (output_bfd,
7075 osec);
7076 if (sym.st_shndx == SHN_BAD)
7077 return FALSE;
7078
7079 sym.st_value += sec->output_offset;
7080 if (! finfo->info->relocatable)
7081 {
7082 sym.st_value += osec->vma;
7083 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
7084 {
7085 /* STT_TLS symbols are relative to PT_TLS
7086 segment base. */
7087 BFD_ASSERT (elf_hash_table (finfo->info)
7088 ->tls_sec != NULL);
7089 sym.st_value -= (elf_hash_table (finfo->info)
7090 ->tls_sec->vma);
7091 }
7092 }
7093
7094 finfo->indices[r_symndx]
7095 = bfd_get_symcount (output_bfd);
7096
7097 if (! elf_link_output_sym (finfo, name, &sym, sec,
7098 NULL))
7099 return FALSE;
7100 }
7101
7102 r_symndx = finfo->indices[r_symndx];
7103 }
7104
7105 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
7106 | (irela->r_info & r_type_mask));
7107 }
7108
7109 /* Swap out the relocs. */
7110 if (bed->elf_backend_emit_relocs
7111 && !(finfo->info->relocatable
7112 || finfo->info->emitrelocations))
7113 reloc_emitter = bed->elf_backend_emit_relocs;
7114 else
7115 reloc_emitter = _bfd_elf_link_output_relocs;
7116
7117 if (input_rel_hdr->sh_size != 0
7118 && ! (*reloc_emitter) (output_bfd, o, input_rel_hdr,
7119 internal_relocs))
7120 return FALSE;
7121
7122 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
7123 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
7124 {
7125 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
7126 * bed->s->int_rels_per_ext_rel);
7127 if (! (*reloc_emitter) (output_bfd, o, input_rel_hdr2,
7128 internal_relocs))
7129 return FALSE;
7130 }
7131 }
7132 }
7133
7134 /* Write out the modified section contents. */
7135 if (bed->elf_backend_write_section
7136 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
7137 {
7138 /* Section written out. */
7139 }
7140 else switch (o->sec_info_type)
7141 {
7142 case ELF_INFO_TYPE_STABS:
7143 if (! (_bfd_write_section_stabs
7144 (output_bfd,
7145 &elf_hash_table (finfo->info)->stab_info,
7146 o, &elf_section_data (o)->sec_info, contents)))
7147 return FALSE;
7148 break;
7149 case ELF_INFO_TYPE_MERGE:
7150 if (! _bfd_write_merged_section (output_bfd, o,
7151 elf_section_data (o)->sec_info))
7152 return FALSE;
7153 break;
7154 case ELF_INFO_TYPE_EH_FRAME:
7155 {
7156 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
7157 o, contents))
7158 return FALSE;
7159 }
7160 break;
7161 default:
7162 {
7163 if (! (o->flags & SEC_EXCLUDE)
7164 && ! bfd_set_section_contents (output_bfd, o->output_section,
7165 contents,
7166 (file_ptr) o->output_offset,
7167 o->size))
7168 return FALSE;
7169 }
7170 break;
7171 }
7172 }
7173
7174 return TRUE;
7175 }
7176
7177 /* Generate a reloc when linking an ELF file. This is a reloc
7178 requested by the linker, and does come from any input file. This
7179 is used to build constructor and destructor tables when linking
7180 with -Ur. */
7181
7182 static bfd_boolean
7183 elf_reloc_link_order (bfd *output_bfd,
7184 struct bfd_link_info *info,
7185 asection *output_section,
7186 struct bfd_link_order *link_order)
7187 {
7188 reloc_howto_type *howto;
7189 long indx;
7190 bfd_vma offset;
7191 bfd_vma addend;
7192 struct elf_link_hash_entry **rel_hash_ptr;
7193 Elf_Internal_Shdr *rel_hdr;
7194 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7195 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
7196 bfd_byte *erel;
7197 unsigned int i;
7198
7199 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
7200 if (howto == NULL)
7201 {
7202 bfd_set_error (bfd_error_bad_value);
7203 return FALSE;
7204 }
7205
7206 addend = link_order->u.reloc.p->addend;
7207
7208 /* Figure out the symbol index. */
7209 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
7210 + elf_section_data (output_section)->rel_count
7211 + elf_section_data (output_section)->rel_count2);
7212 if (link_order->type == bfd_section_reloc_link_order)
7213 {
7214 indx = link_order->u.reloc.p->u.section->target_index;
7215 BFD_ASSERT (indx != 0);
7216 *rel_hash_ptr = NULL;
7217 }
7218 else
7219 {
7220 struct elf_link_hash_entry *h;
7221
7222 /* Treat a reloc against a defined symbol as though it were
7223 actually against the section. */
7224 h = ((struct elf_link_hash_entry *)
7225 bfd_wrapped_link_hash_lookup (output_bfd, info,
7226 link_order->u.reloc.p->u.name,
7227 FALSE, FALSE, TRUE));
7228 if (h != NULL
7229 && (h->root.type == bfd_link_hash_defined
7230 || h->root.type == bfd_link_hash_defweak))
7231 {
7232 asection *section;
7233
7234 section = h->root.u.def.section;
7235 indx = section->output_section->target_index;
7236 *rel_hash_ptr = NULL;
7237 /* It seems that we ought to add the symbol value to the
7238 addend here, but in practice it has already been added
7239 because it was passed to constructor_callback. */
7240 addend += section->output_section->vma + section->output_offset;
7241 }
7242 else if (h != NULL)
7243 {
7244 /* Setting the index to -2 tells elf_link_output_extsym that
7245 this symbol is used by a reloc. */
7246 h->indx = -2;
7247 *rel_hash_ptr = h;
7248 indx = 0;
7249 }
7250 else
7251 {
7252 if (! ((*info->callbacks->unattached_reloc)
7253 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
7254 return FALSE;
7255 indx = 0;
7256 }
7257 }
7258
7259 /* If this is an inplace reloc, we must write the addend into the
7260 object file. */
7261 if (howto->partial_inplace && addend != 0)
7262 {
7263 bfd_size_type size;
7264 bfd_reloc_status_type rstat;
7265 bfd_byte *buf;
7266 bfd_boolean ok;
7267 const char *sym_name;
7268
7269 size = bfd_get_reloc_size (howto);
7270 buf = bfd_zmalloc (size);
7271 if (buf == NULL)
7272 return FALSE;
7273 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
7274 switch (rstat)
7275 {
7276 case bfd_reloc_ok:
7277 break;
7278
7279 default:
7280 case bfd_reloc_outofrange:
7281 abort ();
7282
7283 case bfd_reloc_overflow:
7284 if (link_order->type == bfd_section_reloc_link_order)
7285 sym_name = bfd_section_name (output_bfd,
7286 link_order->u.reloc.p->u.section);
7287 else
7288 sym_name = link_order->u.reloc.p->u.name;
7289 if (! ((*info->callbacks->reloc_overflow)
7290 (info, NULL, sym_name, howto->name, addend, NULL,
7291 NULL, (bfd_vma) 0)))
7292 {
7293 free (buf);
7294 return FALSE;
7295 }
7296 break;
7297 }
7298 ok = bfd_set_section_contents (output_bfd, output_section, buf,
7299 link_order->offset, size);
7300 free (buf);
7301 if (! ok)
7302 return FALSE;
7303 }
7304
7305 /* The address of a reloc is relative to the section in a
7306 relocatable file, and is a virtual address in an executable
7307 file. */
7308 offset = link_order->offset;
7309 if (! info->relocatable)
7310 offset += output_section->vma;
7311
7312 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7313 {
7314 irel[i].r_offset = offset;
7315 irel[i].r_info = 0;
7316 irel[i].r_addend = 0;
7317 }
7318 if (bed->s->arch_size == 32)
7319 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
7320 else
7321 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
7322
7323 rel_hdr = &elf_section_data (output_section)->rel_hdr;
7324 erel = rel_hdr->contents;
7325 if (rel_hdr->sh_type == SHT_REL)
7326 {
7327 erel += (elf_section_data (output_section)->rel_count
7328 * bed->s->sizeof_rel);
7329 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
7330 }
7331 else
7332 {
7333 irel[0].r_addend = addend;
7334 erel += (elf_section_data (output_section)->rel_count
7335 * bed->s->sizeof_rela);
7336 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
7337 }
7338
7339 ++elf_section_data (output_section)->rel_count;
7340
7341 return TRUE;
7342 }
7343
7344
7345 /* Get the output vma of the section pointed to by the sh_link field. */
7346
7347 static bfd_vma
7348 elf_get_linked_section_vma (struct bfd_link_order *p)
7349 {
7350 Elf_Internal_Shdr **elf_shdrp;
7351 asection *s;
7352 int elfsec;
7353
7354 s = p->u.indirect.section;
7355 elf_shdrp = elf_elfsections (s->owner);
7356 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
7357 elfsec = elf_shdrp[elfsec]->sh_link;
7358 /* PR 290:
7359 The Intel C compiler generates SHT_IA_64_UNWIND with
7360 SHF_LINK_ORDER. But it doesn't set theh sh_link or
7361 sh_info fields. Hence we could get the situation
7362 where elfsec is 0. */
7363 if (elfsec == 0)
7364 {
7365 const struct elf_backend_data *bed
7366 = get_elf_backend_data (s->owner);
7367 if (bed->link_order_error_handler)
7368 bed->link_order_error_handler
7369 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
7370 return 0;
7371 }
7372 else
7373 {
7374 s = elf_shdrp[elfsec]->bfd_section;
7375 return s->output_section->vma + s->output_offset;
7376 }
7377 }
7378
7379
7380 /* Compare two sections based on the locations of the sections they are
7381 linked to. Used by elf_fixup_link_order. */
7382
7383 static int
7384 compare_link_order (const void * a, const void * b)
7385 {
7386 bfd_vma apos;
7387 bfd_vma bpos;
7388
7389 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
7390 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
7391 if (apos < bpos)
7392 return -1;
7393 return apos > bpos;
7394 }
7395
7396
7397 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7398 order as their linked sections. Returns false if this could not be done
7399 because an output section includes both ordered and unordered
7400 sections. Ideally we'd do this in the linker proper. */
7401
7402 static bfd_boolean
7403 elf_fixup_link_order (bfd *abfd, asection *o)
7404 {
7405 int seen_linkorder;
7406 int seen_other;
7407 int n;
7408 struct bfd_link_order *p;
7409 bfd *sub;
7410 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7411 int elfsec;
7412 struct bfd_link_order **sections;
7413 asection *s;
7414 bfd_vma offset;
7415
7416 seen_other = 0;
7417 seen_linkorder = 0;
7418 for (p = o->link_order_head; p != NULL; p = p->next)
7419 {
7420 if (p->type == bfd_indirect_link_order
7421 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
7422 == bfd_target_elf_flavour)
7423 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
7424 {
7425 s = p->u.indirect.section;
7426 elfsec = _bfd_elf_section_from_bfd_section (sub, s);
7427 if (elfsec != -1
7428 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER)
7429 seen_linkorder++;
7430 else
7431 seen_other++;
7432 }
7433 else
7434 seen_other++;
7435 }
7436
7437 if (!seen_linkorder)
7438 return TRUE;
7439
7440 if (seen_other && seen_linkorder)
7441 {
7442 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
7443 o);
7444 bfd_set_error (bfd_error_bad_value);
7445 return FALSE;
7446 }
7447
7448 sections = (struct bfd_link_order **)
7449 xmalloc (seen_linkorder * sizeof (struct bfd_link_order *));
7450 seen_linkorder = 0;
7451
7452 for (p = o->link_order_head; p != NULL; p = p->next)
7453 {
7454 sections[seen_linkorder++] = p;
7455 }
7456 /* Sort the input sections in the order of their linked section. */
7457 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
7458 compare_link_order);
7459
7460 /* Change the offsets of the sections. */
7461 offset = 0;
7462 for (n = 0; n < seen_linkorder; n++)
7463 {
7464 s = sections[n]->u.indirect.section;
7465 offset &= ~(bfd_vma)((1 << s->alignment_power) - 1);
7466 s->output_offset = offset;
7467 sections[n]->offset = offset;
7468 offset += sections[n]->size;
7469 }
7470
7471 return TRUE;
7472 }
7473
7474
7475 /* Do the final step of an ELF link. */
7476
7477 bfd_boolean
7478 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
7479 {
7480 bfd_boolean dynamic;
7481 bfd_boolean emit_relocs;
7482 bfd *dynobj;
7483 struct elf_final_link_info finfo;
7484 register asection *o;
7485 register struct bfd_link_order *p;
7486 register bfd *sub;
7487 bfd_size_type max_contents_size;
7488 bfd_size_type max_external_reloc_size;
7489 bfd_size_type max_internal_reloc_count;
7490 bfd_size_type max_sym_count;
7491 bfd_size_type max_sym_shndx_count;
7492 file_ptr off;
7493 Elf_Internal_Sym elfsym;
7494 unsigned int i;
7495 Elf_Internal_Shdr *symtab_hdr;
7496 Elf_Internal_Shdr *symtab_shndx_hdr;
7497 Elf_Internal_Shdr *symstrtab_hdr;
7498 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7499 struct elf_outext_info eoinfo;
7500 bfd_boolean merged;
7501 size_t relativecount = 0;
7502 asection *reldyn = 0;
7503 bfd_size_type amt;
7504
7505 if (! is_elf_hash_table (info->hash))
7506 return FALSE;
7507
7508 if (info->shared)
7509 abfd->flags |= DYNAMIC;
7510
7511 dynamic = elf_hash_table (info)->dynamic_sections_created;
7512 dynobj = elf_hash_table (info)->dynobj;
7513
7514 emit_relocs = (info->relocatable
7515 || info->emitrelocations
7516 || bed->elf_backend_emit_relocs);
7517
7518 finfo.info = info;
7519 finfo.output_bfd = abfd;
7520 finfo.symstrtab = _bfd_elf_stringtab_init ();
7521 if (finfo.symstrtab == NULL)
7522 return FALSE;
7523
7524 if (! dynamic)
7525 {
7526 finfo.dynsym_sec = NULL;
7527 finfo.hash_sec = NULL;
7528 finfo.symver_sec = NULL;
7529 }
7530 else
7531 {
7532 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
7533 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
7534 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
7535 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
7536 /* Note that it is OK if symver_sec is NULL. */
7537 }
7538
7539 finfo.contents = NULL;
7540 finfo.external_relocs = NULL;
7541 finfo.internal_relocs = NULL;
7542 finfo.external_syms = NULL;
7543 finfo.locsym_shndx = NULL;
7544 finfo.internal_syms = NULL;
7545 finfo.indices = NULL;
7546 finfo.sections = NULL;
7547 finfo.symbuf = NULL;
7548 finfo.symshndxbuf = NULL;
7549 finfo.symbuf_count = 0;
7550 finfo.shndxbuf_size = 0;
7551
7552 /* Count up the number of relocations we will output for each output
7553 section, so that we know the sizes of the reloc sections. We
7554 also figure out some maximum sizes. */
7555 max_contents_size = 0;
7556 max_external_reloc_size = 0;
7557 max_internal_reloc_count = 0;
7558 max_sym_count = 0;
7559 max_sym_shndx_count = 0;
7560 merged = FALSE;
7561 for (o = abfd->sections; o != NULL; o = o->next)
7562 {
7563 struct bfd_elf_section_data *esdo = elf_section_data (o);
7564 o->reloc_count = 0;
7565
7566 for (p = o->link_order_head; p != NULL; p = p->next)
7567 {
7568 unsigned int reloc_count = 0;
7569 struct bfd_elf_section_data *esdi = NULL;
7570 unsigned int *rel_count1;
7571
7572 if (p->type == bfd_section_reloc_link_order
7573 || p->type == bfd_symbol_reloc_link_order)
7574 reloc_count = 1;
7575 else if (p->type == bfd_indirect_link_order)
7576 {
7577 asection *sec;
7578
7579 sec = p->u.indirect.section;
7580 esdi = elf_section_data (sec);
7581
7582 /* Mark all sections which are to be included in the
7583 link. This will normally be every section. We need
7584 to do this so that we can identify any sections which
7585 the linker has decided to not include. */
7586 sec->linker_mark = TRUE;
7587
7588 if (sec->flags & SEC_MERGE)
7589 merged = TRUE;
7590
7591 if (info->relocatable || info->emitrelocations)
7592 reloc_count = sec->reloc_count;
7593 else if (bed->elf_backend_count_relocs)
7594 {
7595 Elf_Internal_Rela * relocs;
7596
7597 relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
7598 info->keep_memory);
7599
7600 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
7601
7602 if (elf_section_data (o)->relocs != relocs)
7603 free (relocs);
7604 }
7605
7606 if (sec->rawsize > max_contents_size)
7607 max_contents_size = sec->rawsize;
7608 if (sec->size > max_contents_size)
7609 max_contents_size = sec->size;
7610
7611 /* We are interested in just local symbols, not all
7612 symbols. */
7613 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
7614 && (sec->owner->flags & DYNAMIC) == 0)
7615 {
7616 size_t sym_count;
7617
7618 if (elf_bad_symtab (sec->owner))
7619 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
7620 / bed->s->sizeof_sym);
7621 else
7622 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
7623
7624 if (sym_count > max_sym_count)
7625 max_sym_count = sym_count;
7626
7627 if (sym_count > max_sym_shndx_count
7628 && elf_symtab_shndx (sec->owner) != 0)
7629 max_sym_shndx_count = sym_count;
7630
7631 if ((sec->flags & SEC_RELOC) != 0)
7632 {
7633 size_t ext_size;
7634
7635 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
7636 if (ext_size > max_external_reloc_size)
7637 max_external_reloc_size = ext_size;
7638 if (sec->reloc_count > max_internal_reloc_count)
7639 max_internal_reloc_count = sec->reloc_count;
7640 }
7641 }
7642 }
7643
7644 if (reloc_count == 0)
7645 continue;
7646
7647 o->reloc_count += reloc_count;
7648
7649 /* MIPS may have a mix of REL and RELA relocs on sections.
7650 To support this curious ABI we keep reloc counts in
7651 elf_section_data too. We must be careful to add the
7652 relocations from the input section to the right output
7653 count. FIXME: Get rid of one count. We have
7654 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7655 rel_count1 = &esdo->rel_count;
7656 if (esdi != NULL)
7657 {
7658 bfd_boolean same_size;
7659 bfd_size_type entsize1;
7660
7661 entsize1 = esdi->rel_hdr.sh_entsize;
7662 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
7663 || entsize1 == bed->s->sizeof_rela);
7664 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
7665
7666 if (!same_size)
7667 rel_count1 = &esdo->rel_count2;
7668
7669 if (esdi->rel_hdr2 != NULL)
7670 {
7671 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
7672 unsigned int alt_count;
7673 unsigned int *rel_count2;
7674
7675 BFD_ASSERT (entsize2 != entsize1
7676 && (entsize2 == bed->s->sizeof_rel
7677 || entsize2 == bed->s->sizeof_rela));
7678
7679 rel_count2 = &esdo->rel_count2;
7680 if (!same_size)
7681 rel_count2 = &esdo->rel_count;
7682
7683 /* The following is probably too simplistic if the
7684 backend counts output relocs unusually. */
7685 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
7686 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
7687 *rel_count2 += alt_count;
7688 reloc_count -= alt_count;
7689 }
7690 }
7691 *rel_count1 += reloc_count;
7692 }
7693
7694 if (o->reloc_count > 0)
7695 o->flags |= SEC_RELOC;
7696 else
7697 {
7698 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7699 set it (this is probably a bug) and if it is set
7700 assign_section_numbers will create a reloc section. */
7701 o->flags &=~ SEC_RELOC;
7702 }
7703
7704 /* If the SEC_ALLOC flag is not set, force the section VMA to
7705 zero. This is done in elf_fake_sections as well, but forcing
7706 the VMA to 0 here will ensure that relocs against these
7707 sections are handled correctly. */
7708 if ((o->flags & SEC_ALLOC) == 0
7709 && ! o->user_set_vma)
7710 o->vma = 0;
7711 }
7712
7713 if (! info->relocatable && merged)
7714 elf_link_hash_traverse (elf_hash_table (info),
7715 _bfd_elf_link_sec_merge_syms, abfd);
7716
7717 /* Figure out the file positions for everything but the symbol table
7718 and the relocs. We set symcount to force assign_section_numbers
7719 to create a symbol table. */
7720 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
7721 BFD_ASSERT (! abfd->output_has_begun);
7722 if (! _bfd_elf_compute_section_file_positions (abfd, info))
7723 goto error_return;
7724
7725 /* Set sizes, and assign file positions for reloc sections. */
7726 for (o = abfd->sections; o != NULL; o = o->next)
7727 {
7728 if ((o->flags & SEC_RELOC) != 0)
7729 {
7730 if (!(_bfd_elf_link_size_reloc_section
7731 (abfd, &elf_section_data (o)->rel_hdr, o)))
7732 goto error_return;
7733
7734 if (elf_section_data (o)->rel_hdr2
7735 && !(_bfd_elf_link_size_reloc_section
7736 (abfd, elf_section_data (o)->rel_hdr2, o)))
7737 goto error_return;
7738 }
7739
7740 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7741 to count upwards while actually outputting the relocations. */
7742 elf_section_data (o)->rel_count = 0;
7743 elf_section_data (o)->rel_count2 = 0;
7744 }
7745
7746 _bfd_elf_assign_file_positions_for_relocs (abfd);
7747
7748 /* We have now assigned file positions for all the sections except
7749 .symtab and .strtab. We start the .symtab section at the current
7750 file position, and write directly to it. We build the .strtab
7751 section in memory. */
7752 bfd_get_symcount (abfd) = 0;
7753 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7754 /* sh_name is set in prep_headers. */
7755 symtab_hdr->sh_type = SHT_SYMTAB;
7756 /* sh_flags, sh_addr and sh_size all start off zero. */
7757 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
7758 /* sh_link is set in assign_section_numbers. */
7759 /* sh_info is set below. */
7760 /* sh_offset is set just below. */
7761 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
7762
7763 off = elf_tdata (abfd)->next_file_pos;
7764 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
7765
7766 /* Note that at this point elf_tdata (abfd)->next_file_pos is
7767 incorrect. We do not yet know the size of the .symtab section.
7768 We correct next_file_pos below, after we do know the size. */
7769
7770 /* Allocate a buffer to hold swapped out symbols. This is to avoid
7771 continuously seeking to the right position in the file. */
7772 if (! info->keep_memory || max_sym_count < 20)
7773 finfo.symbuf_size = 20;
7774 else
7775 finfo.symbuf_size = max_sym_count;
7776 amt = finfo.symbuf_size;
7777 amt *= bed->s->sizeof_sym;
7778 finfo.symbuf = bfd_malloc (amt);
7779 if (finfo.symbuf == NULL)
7780 goto error_return;
7781 if (elf_numsections (abfd) > SHN_LORESERVE)
7782 {
7783 /* Wild guess at number of output symbols. realloc'd as needed. */
7784 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
7785 finfo.shndxbuf_size = amt;
7786 amt *= sizeof (Elf_External_Sym_Shndx);
7787 finfo.symshndxbuf = bfd_zmalloc (amt);
7788 if (finfo.symshndxbuf == NULL)
7789 goto error_return;
7790 }
7791
7792 /* Start writing out the symbol table. The first symbol is always a
7793 dummy symbol. */
7794 if (info->strip != strip_all
7795 || emit_relocs)
7796 {
7797 elfsym.st_value = 0;
7798 elfsym.st_size = 0;
7799 elfsym.st_info = 0;
7800 elfsym.st_other = 0;
7801 elfsym.st_shndx = SHN_UNDEF;
7802 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
7803 NULL))
7804 goto error_return;
7805 }
7806
7807 #if 0
7808 /* Some standard ELF linkers do this, but we don't because it causes
7809 bootstrap comparison failures. */
7810 /* Output a file symbol for the output file as the second symbol.
7811 We output this even if we are discarding local symbols, although
7812 I'm not sure if this is correct. */
7813 elfsym.st_value = 0;
7814 elfsym.st_size = 0;
7815 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
7816 elfsym.st_other = 0;
7817 elfsym.st_shndx = SHN_ABS;
7818 if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd),
7819 &elfsym, bfd_abs_section_ptr, NULL))
7820 goto error_return;
7821 #endif
7822
7823 /* Output a symbol for each section. We output these even if we are
7824 discarding local symbols, since they are used for relocs. These
7825 symbols have no names. We store the index of each one in the
7826 index field of the section, so that we can find it again when
7827 outputting relocs. */
7828 if (info->strip != strip_all
7829 || emit_relocs)
7830 {
7831 elfsym.st_size = 0;
7832 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
7833 elfsym.st_other = 0;
7834 for (i = 1; i < elf_numsections (abfd); i++)
7835 {
7836 o = bfd_section_from_elf_index (abfd, i);
7837 if (o != NULL)
7838 o->target_index = bfd_get_symcount (abfd);
7839 elfsym.st_shndx = i;
7840 if (info->relocatable || o == NULL)
7841 elfsym.st_value = 0;
7842 else
7843 elfsym.st_value = o->vma;
7844 if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
7845 goto error_return;
7846 if (i == SHN_LORESERVE - 1)
7847 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
7848 }
7849 }
7850
7851 /* Allocate some memory to hold information read in from the input
7852 files. */
7853 if (max_contents_size != 0)
7854 {
7855 finfo.contents = bfd_malloc (max_contents_size);
7856 if (finfo.contents == NULL)
7857 goto error_return;
7858 }
7859
7860 if (max_external_reloc_size != 0)
7861 {
7862 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
7863 if (finfo.external_relocs == NULL)
7864 goto error_return;
7865 }
7866
7867 if (max_internal_reloc_count != 0)
7868 {
7869 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
7870 amt *= sizeof (Elf_Internal_Rela);
7871 finfo.internal_relocs = bfd_malloc (amt);
7872 if (finfo.internal_relocs == NULL)
7873 goto error_return;
7874 }
7875
7876 if (max_sym_count != 0)
7877 {
7878 amt = max_sym_count * bed->s->sizeof_sym;
7879 finfo.external_syms = bfd_malloc (amt);
7880 if (finfo.external_syms == NULL)
7881 goto error_return;
7882
7883 amt = max_sym_count * sizeof (Elf_Internal_Sym);
7884 finfo.internal_syms = bfd_malloc (amt);
7885 if (finfo.internal_syms == NULL)
7886 goto error_return;
7887
7888 amt = max_sym_count * sizeof (long);
7889 finfo.indices = bfd_malloc (amt);
7890 if (finfo.indices == NULL)
7891 goto error_return;
7892
7893 amt = max_sym_count * sizeof (asection *);
7894 finfo.sections = bfd_malloc (amt);
7895 if (finfo.sections == NULL)
7896 goto error_return;
7897 }
7898
7899 if (max_sym_shndx_count != 0)
7900 {
7901 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
7902 finfo.locsym_shndx = bfd_malloc (amt);
7903 if (finfo.locsym_shndx == NULL)
7904 goto error_return;
7905 }
7906
7907 if (elf_hash_table (info)->tls_sec)
7908 {
7909 bfd_vma base, end = 0;
7910 asection *sec;
7911
7912 for (sec = elf_hash_table (info)->tls_sec;
7913 sec && (sec->flags & SEC_THREAD_LOCAL);
7914 sec = sec->next)
7915 {
7916 bfd_vma size = sec->size;
7917
7918 if (size == 0 && (sec->flags & SEC_HAS_CONTENTS) == 0)
7919 {
7920 struct bfd_link_order *o;
7921
7922 for (o = sec->link_order_head; o != NULL; o = o->next)
7923 if (size < o->offset + o->size)
7924 size = o->offset + o->size;
7925 }
7926 end = sec->vma + size;
7927 }
7928 base = elf_hash_table (info)->tls_sec->vma;
7929 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
7930 elf_hash_table (info)->tls_size = end - base;
7931 }
7932
7933 /* Reorder SHF_LINK_ORDER sections. */
7934 for (o = abfd->sections; o != NULL; o = o->next)
7935 {
7936 if (!elf_fixup_link_order (abfd, o))
7937 return FALSE;
7938 }
7939
7940 /* Since ELF permits relocations to be against local symbols, we
7941 must have the local symbols available when we do the relocations.
7942 Since we would rather only read the local symbols once, and we
7943 would rather not keep them in memory, we handle all the
7944 relocations for a single input file at the same time.
7945
7946 Unfortunately, there is no way to know the total number of local
7947 symbols until we have seen all of them, and the local symbol
7948 indices precede the global symbol indices. This means that when
7949 we are generating relocatable output, and we see a reloc against
7950 a global symbol, we can not know the symbol index until we have
7951 finished examining all the local symbols to see which ones we are
7952 going to output. To deal with this, we keep the relocations in
7953 memory, and don't output them until the end of the link. This is
7954 an unfortunate waste of memory, but I don't see a good way around
7955 it. Fortunately, it only happens when performing a relocatable
7956 link, which is not the common case. FIXME: If keep_memory is set
7957 we could write the relocs out and then read them again; I don't
7958 know how bad the memory loss will be. */
7959
7960 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
7961 sub->output_has_begun = FALSE;
7962 for (o = abfd->sections; o != NULL; o = o->next)
7963 {
7964 for (p = o->link_order_head; p != NULL; p = p->next)
7965 {
7966 if (p->type == bfd_indirect_link_order
7967 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
7968 == bfd_target_elf_flavour)
7969 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
7970 {
7971 if (! sub->output_has_begun)
7972 {
7973 if (! elf_link_input_bfd (&finfo, sub))
7974 goto error_return;
7975 sub->output_has_begun = TRUE;
7976 }
7977 }
7978 else if (p->type == bfd_section_reloc_link_order
7979 || p->type == bfd_symbol_reloc_link_order)
7980 {
7981 if (! elf_reloc_link_order (abfd, info, o, p))
7982 goto error_return;
7983 }
7984 else
7985 {
7986 if (! _bfd_default_link_order (abfd, info, o, p))
7987 goto error_return;
7988 }
7989 }
7990 }
7991
7992 /* Output any global symbols that got converted to local in a
7993 version script or due to symbol visibility. We do this in a
7994 separate step since ELF requires all local symbols to appear
7995 prior to any global symbols. FIXME: We should only do this if
7996 some global symbols were, in fact, converted to become local.
7997 FIXME: Will this work correctly with the Irix 5 linker? */
7998 eoinfo.failed = FALSE;
7999 eoinfo.finfo = &finfo;
8000 eoinfo.localsyms = TRUE;
8001 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8002 &eoinfo);
8003 if (eoinfo.failed)
8004 return FALSE;
8005
8006 /* That wrote out all the local symbols. Finish up the symbol table
8007 with the global symbols. Even if we want to strip everything we
8008 can, we still need to deal with those global symbols that got
8009 converted to local in a version script. */
8010
8011 /* The sh_info field records the index of the first non local symbol. */
8012 symtab_hdr->sh_info = bfd_get_symcount (abfd);
8013
8014 if (dynamic
8015 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
8016 {
8017 Elf_Internal_Sym sym;
8018 bfd_byte *dynsym = finfo.dynsym_sec->contents;
8019 long last_local = 0;
8020
8021 /* Write out the section symbols for the output sections. */
8022 if (info->shared)
8023 {
8024 asection *s;
8025
8026 sym.st_size = 0;
8027 sym.st_name = 0;
8028 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8029 sym.st_other = 0;
8030
8031 for (s = abfd->sections; s != NULL; s = s->next)
8032 {
8033 int indx;
8034 bfd_byte *dest;
8035 long dynindx;
8036
8037 dynindx = elf_section_data (s)->dynindx;
8038 if (dynindx <= 0)
8039 continue;
8040 indx = elf_section_data (s)->this_idx;
8041 BFD_ASSERT (indx > 0);
8042 sym.st_shndx = indx;
8043 sym.st_value = s->vma;
8044 dest = dynsym + dynindx * bed->s->sizeof_sym;
8045 if (last_local < dynindx)
8046 last_local = dynindx;
8047 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
8048 }
8049 }
8050
8051 /* Write out the local dynsyms. */
8052 if (elf_hash_table (info)->dynlocal)
8053 {
8054 struct elf_link_local_dynamic_entry *e;
8055 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
8056 {
8057 asection *s;
8058 bfd_byte *dest;
8059
8060 sym.st_size = e->isym.st_size;
8061 sym.st_other = e->isym.st_other;
8062
8063 /* Copy the internal symbol as is.
8064 Note that we saved a word of storage and overwrote
8065 the original st_name with the dynstr_index. */
8066 sym = e->isym;
8067
8068 if (e->isym.st_shndx != SHN_UNDEF
8069 && (e->isym.st_shndx < SHN_LORESERVE
8070 || e->isym.st_shndx > SHN_HIRESERVE))
8071 {
8072 s = bfd_section_from_elf_index (e->input_bfd,
8073 e->isym.st_shndx);
8074
8075 sym.st_shndx =
8076 elf_section_data (s->output_section)->this_idx;
8077 sym.st_value = (s->output_section->vma
8078 + s->output_offset
8079 + e->isym.st_value);
8080 }
8081
8082 if (last_local < e->dynindx)
8083 last_local = e->dynindx;
8084
8085 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
8086 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
8087 }
8088 }
8089
8090 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
8091 last_local + 1;
8092 }
8093
8094 /* We get the global symbols from the hash table. */
8095 eoinfo.failed = FALSE;
8096 eoinfo.localsyms = FALSE;
8097 eoinfo.finfo = &finfo;
8098 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8099 &eoinfo);
8100 if (eoinfo.failed)
8101 return FALSE;
8102
8103 /* If backend needs to output some symbols not present in the hash
8104 table, do it now. */
8105 if (bed->elf_backend_output_arch_syms)
8106 {
8107 typedef bfd_boolean (*out_sym_func)
8108 (void *, const char *, Elf_Internal_Sym *, asection *,
8109 struct elf_link_hash_entry *);
8110
8111 if (! ((*bed->elf_backend_output_arch_syms)
8112 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
8113 return FALSE;
8114 }
8115
8116 /* Flush all symbols to the file. */
8117 if (! elf_link_flush_output_syms (&finfo, bed))
8118 return FALSE;
8119
8120 /* Now we know the size of the symtab section. */
8121 off += symtab_hdr->sh_size;
8122
8123 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
8124 if (symtab_shndx_hdr->sh_name != 0)
8125 {
8126 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8127 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8128 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8129 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
8130 symtab_shndx_hdr->sh_size = amt;
8131
8132 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
8133 off, TRUE);
8134
8135 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
8136 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
8137 return FALSE;
8138 }
8139
8140
8141 /* Finish up and write out the symbol string table (.strtab)
8142 section. */
8143 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8144 /* sh_name was set in prep_headers. */
8145 symstrtab_hdr->sh_type = SHT_STRTAB;
8146 symstrtab_hdr->sh_flags = 0;
8147 symstrtab_hdr->sh_addr = 0;
8148 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
8149 symstrtab_hdr->sh_entsize = 0;
8150 symstrtab_hdr->sh_link = 0;
8151 symstrtab_hdr->sh_info = 0;
8152 /* sh_offset is set just below. */
8153 symstrtab_hdr->sh_addralign = 1;
8154
8155 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
8156 elf_tdata (abfd)->next_file_pos = off;
8157
8158 if (bfd_get_symcount (abfd) > 0)
8159 {
8160 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
8161 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
8162 return FALSE;
8163 }
8164
8165 /* Adjust the relocs to have the correct symbol indices. */
8166 for (o = abfd->sections; o != NULL; o = o->next)
8167 {
8168 if ((o->flags & SEC_RELOC) == 0)
8169 continue;
8170
8171 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
8172 elf_section_data (o)->rel_count,
8173 elf_section_data (o)->rel_hashes);
8174 if (elf_section_data (o)->rel_hdr2 != NULL)
8175 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
8176 elf_section_data (o)->rel_count2,
8177 (elf_section_data (o)->rel_hashes
8178 + elf_section_data (o)->rel_count));
8179
8180 /* Set the reloc_count field to 0 to prevent write_relocs from
8181 trying to swap the relocs out itself. */
8182 o->reloc_count = 0;
8183 }
8184
8185 if (dynamic && info->combreloc && dynobj != NULL)
8186 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
8187
8188 /* If we are linking against a dynamic object, or generating a
8189 shared library, finish up the dynamic linking information. */
8190 if (dynamic)
8191 {
8192 bfd_byte *dyncon, *dynconend;
8193
8194 /* Fix up .dynamic entries. */
8195 o = bfd_get_section_by_name (dynobj, ".dynamic");
8196 BFD_ASSERT (o != NULL);
8197
8198 dyncon = o->contents;
8199 dynconend = o->contents + o->size;
8200 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
8201 {
8202 Elf_Internal_Dyn dyn;
8203 const char *name;
8204 unsigned int type;
8205
8206 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
8207
8208 switch (dyn.d_tag)
8209 {
8210 default:
8211 continue;
8212 case DT_NULL:
8213 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
8214 {
8215 switch (elf_section_data (reldyn)->this_hdr.sh_type)
8216 {
8217 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
8218 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
8219 default: continue;
8220 }
8221 dyn.d_un.d_val = relativecount;
8222 relativecount = 0;
8223 break;
8224 }
8225 continue;
8226
8227 case DT_INIT:
8228 name = info->init_function;
8229 goto get_sym;
8230 case DT_FINI:
8231 name = info->fini_function;
8232 get_sym:
8233 {
8234 struct elf_link_hash_entry *h;
8235
8236 h = elf_link_hash_lookup (elf_hash_table (info), name,
8237 FALSE, FALSE, TRUE);
8238 if (h != NULL
8239 && (h->root.type == bfd_link_hash_defined
8240 || h->root.type == bfd_link_hash_defweak))
8241 {
8242 dyn.d_un.d_val = h->root.u.def.value;
8243 o = h->root.u.def.section;
8244 if (o->output_section != NULL)
8245 dyn.d_un.d_val += (o->output_section->vma
8246 + o->output_offset);
8247 else
8248 {
8249 /* The symbol is imported from another shared
8250 library and does not apply to this one. */
8251 dyn.d_un.d_val = 0;
8252 }
8253 break;
8254 }
8255 }
8256 continue;
8257
8258 case DT_PREINIT_ARRAYSZ:
8259 name = ".preinit_array";
8260 goto get_size;
8261 case DT_INIT_ARRAYSZ:
8262 name = ".init_array";
8263 goto get_size;
8264 case DT_FINI_ARRAYSZ:
8265 name = ".fini_array";
8266 get_size:
8267 o = bfd_get_section_by_name (abfd, name);
8268 if (o == NULL)
8269 {
8270 (*_bfd_error_handler)
8271 (_("%B: could not find output section %s"), abfd, name);
8272 goto error_return;
8273 }
8274 if (o->size == 0)
8275 (*_bfd_error_handler)
8276 (_("warning: %s section has zero size"), name);
8277 dyn.d_un.d_val = o->size;
8278 break;
8279
8280 case DT_PREINIT_ARRAY:
8281 name = ".preinit_array";
8282 goto get_vma;
8283 case DT_INIT_ARRAY:
8284 name = ".init_array";
8285 goto get_vma;
8286 case DT_FINI_ARRAY:
8287 name = ".fini_array";
8288 goto get_vma;
8289
8290 case DT_HASH:
8291 name = ".hash";
8292 goto get_vma;
8293 case DT_STRTAB:
8294 name = ".dynstr";
8295 goto get_vma;
8296 case DT_SYMTAB:
8297 name = ".dynsym";
8298 goto get_vma;
8299 case DT_VERDEF:
8300 name = ".gnu.version_d";
8301 goto get_vma;
8302 case DT_VERNEED:
8303 name = ".gnu.version_r";
8304 goto get_vma;
8305 case DT_VERSYM:
8306 name = ".gnu.version";
8307 get_vma:
8308 o = bfd_get_section_by_name (abfd, name);
8309 if (o == NULL)
8310 {
8311 (*_bfd_error_handler)
8312 (_("%B: could not find output section %s"), abfd, name);
8313 goto error_return;
8314 }
8315 dyn.d_un.d_ptr = o->vma;
8316 break;
8317
8318 case DT_REL:
8319 case DT_RELA:
8320 case DT_RELSZ:
8321 case DT_RELASZ:
8322 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
8323 type = SHT_REL;
8324 else
8325 type = SHT_RELA;
8326 dyn.d_un.d_val = 0;
8327 for (i = 1; i < elf_numsections (abfd); i++)
8328 {
8329 Elf_Internal_Shdr *hdr;
8330
8331 hdr = elf_elfsections (abfd)[i];
8332 if (hdr->sh_type == type
8333 && (hdr->sh_flags & SHF_ALLOC) != 0)
8334 {
8335 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
8336 dyn.d_un.d_val += hdr->sh_size;
8337 else
8338 {
8339 if (dyn.d_un.d_val == 0
8340 || hdr->sh_addr < dyn.d_un.d_val)
8341 dyn.d_un.d_val = hdr->sh_addr;
8342 }
8343 }
8344 }
8345 break;
8346 }
8347 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
8348 }
8349 }
8350
8351 /* If we have created any dynamic sections, then output them. */
8352 if (dynobj != NULL)
8353 {
8354 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
8355 goto error_return;
8356
8357 for (o = dynobj->sections; o != NULL; o = o->next)
8358 {
8359 if ((o->flags & SEC_HAS_CONTENTS) == 0
8360 || o->size == 0
8361 || o->output_section == bfd_abs_section_ptr)
8362 continue;
8363 if ((o->flags & SEC_LINKER_CREATED) == 0)
8364 {
8365 /* At this point, we are only interested in sections
8366 created by _bfd_elf_link_create_dynamic_sections. */
8367 continue;
8368 }
8369 if (elf_hash_table (info)->stab_info.stabstr == o)
8370 continue;
8371 if (elf_hash_table (info)->eh_info.hdr_sec == o)
8372 continue;
8373 if ((elf_section_data (o->output_section)->this_hdr.sh_type
8374 != SHT_STRTAB)
8375 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
8376 {
8377 if (! bfd_set_section_contents (abfd, o->output_section,
8378 o->contents,
8379 (file_ptr) o->output_offset,
8380 o->size))
8381 goto error_return;
8382 }
8383 else
8384 {
8385 /* The contents of the .dynstr section are actually in a
8386 stringtab. */
8387 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
8388 if (bfd_seek (abfd, off, SEEK_SET) != 0
8389 || ! _bfd_elf_strtab_emit (abfd,
8390 elf_hash_table (info)->dynstr))
8391 goto error_return;
8392 }
8393 }
8394 }
8395
8396 if (info->relocatable)
8397 {
8398 bfd_boolean failed = FALSE;
8399
8400 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
8401 if (failed)
8402 goto error_return;
8403 }
8404
8405 /* If we have optimized stabs strings, output them. */
8406 if (elf_hash_table (info)->stab_info.stabstr != NULL)
8407 {
8408 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
8409 goto error_return;
8410 }
8411
8412 if (info->eh_frame_hdr)
8413 {
8414 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
8415 goto error_return;
8416 }
8417
8418 if (finfo.symstrtab != NULL)
8419 _bfd_stringtab_free (finfo.symstrtab);
8420 if (finfo.contents != NULL)
8421 free (finfo.contents);
8422 if (finfo.external_relocs != NULL)
8423 free (finfo.external_relocs);
8424 if (finfo.internal_relocs != NULL)
8425 free (finfo.internal_relocs);
8426 if (finfo.external_syms != NULL)
8427 free (finfo.external_syms);
8428 if (finfo.locsym_shndx != NULL)
8429 free (finfo.locsym_shndx);
8430 if (finfo.internal_syms != NULL)
8431 free (finfo.internal_syms);
8432 if (finfo.indices != NULL)
8433 free (finfo.indices);
8434 if (finfo.sections != NULL)
8435 free (finfo.sections);
8436 if (finfo.symbuf != NULL)
8437 free (finfo.symbuf);
8438 if (finfo.symshndxbuf != NULL)
8439 free (finfo.symshndxbuf);
8440 for (o = abfd->sections; o != NULL; o = o->next)
8441 {
8442 if ((o->flags & SEC_RELOC) != 0
8443 && elf_section_data (o)->rel_hashes != NULL)
8444 free (elf_section_data (o)->rel_hashes);
8445 }
8446
8447 elf_tdata (abfd)->linker = TRUE;
8448
8449 return TRUE;
8450
8451 error_return:
8452 if (finfo.symstrtab != NULL)
8453 _bfd_stringtab_free (finfo.symstrtab);
8454 if (finfo.contents != NULL)
8455 free (finfo.contents);
8456 if (finfo.external_relocs != NULL)
8457 free (finfo.external_relocs);
8458 if (finfo.internal_relocs != NULL)
8459 free (finfo.internal_relocs);
8460 if (finfo.external_syms != NULL)
8461 free (finfo.external_syms);
8462 if (finfo.locsym_shndx != NULL)
8463 free (finfo.locsym_shndx);
8464 if (finfo.internal_syms != NULL)
8465 free (finfo.internal_syms);
8466 if (finfo.indices != NULL)
8467 free (finfo.indices);
8468 if (finfo.sections != NULL)
8469 free (finfo.sections);
8470 if (finfo.symbuf != NULL)
8471 free (finfo.symbuf);
8472 if (finfo.symshndxbuf != NULL)
8473 free (finfo.symshndxbuf);
8474 for (o = abfd->sections; o != NULL; o = o->next)
8475 {
8476 if ((o->flags & SEC_RELOC) != 0
8477 && elf_section_data (o)->rel_hashes != NULL)
8478 free (elf_section_data (o)->rel_hashes);
8479 }
8480
8481 return FALSE;
8482 }
8483 \f
8484 /* Garbage collect unused sections. */
8485
8486 /* The mark phase of garbage collection. For a given section, mark
8487 it and any sections in this section's group, and all the sections
8488 which define symbols to which it refers. */
8489
8490 typedef asection * (*gc_mark_hook_fn)
8491 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
8492 struct elf_link_hash_entry *, Elf_Internal_Sym *);
8493
8494 bfd_boolean
8495 _bfd_elf_gc_mark (struct bfd_link_info *info,
8496 asection *sec,
8497 gc_mark_hook_fn gc_mark_hook)
8498 {
8499 bfd_boolean ret;
8500 asection *group_sec;
8501
8502 sec->gc_mark = 1;
8503
8504 /* Mark all the sections in the group. */
8505 group_sec = elf_section_data (sec)->next_in_group;
8506 if (group_sec && !group_sec->gc_mark)
8507 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
8508 return FALSE;
8509
8510 /* Look through the section relocs. */
8511 ret = TRUE;
8512 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
8513 {
8514 Elf_Internal_Rela *relstart, *rel, *relend;
8515 Elf_Internal_Shdr *symtab_hdr;
8516 struct elf_link_hash_entry **sym_hashes;
8517 size_t nlocsyms;
8518 size_t extsymoff;
8519 bfd *input_bfd = sec->owner;
8520 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
8521 Elf_Internal_Sym *isym = NULL;
8522 int r_sym_shift;
8523
8524 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8525 sym_hashes = elf_sym_hashes (input_bfd);
8526
8527 /* Read the local symbols. */
8528 if (elf_bad_symtab (input_bfd))
8529 {
8530 nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym;
8531 extsymoff = 0;
8532 }
8533 else
8534 extsymoff = nlocsyms = symtab_hdr->sh_info;
8535
8536 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
8537 if (isym == NULL && nlocsyms != 0)
8538 {
8539 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
8540 NULL, NULL, NULL);
8541 if (isym == NULL)
8542 return FALSE;
8543 }
8544
8545 /* Read the relocations. */
8546 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
8547 info->keep_memory);
8548 if (relstart == NULL)
8549 {
8550 ret = FALSE;
8551 goto out1;
8552 }
8553 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
8554
8555 if (bed->s->arch_size == 32)
8556 r_sym_shift = 8;
8557 else
8558 r_sym_shift = 32;
8559
8560 for (rel = relstart; rel < relend; rel++)
8561 {
8562 unsigned long r_symndx;
8563 asection *rsec;
8564 struct elf_link_hash_entry *h;
8565
8566 r_symndx = rel->r_info >> r_sym_shift;
8567 if (r_symndx == 0)
8568 continue;
8569
8570 if (r_symndx >= nlocsyms
8571 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
8572 {
8573 h = sym_hashes[r_symndx - extsymoff];
8574 while (h->root.type == bfd_link_hash_indirect
8575 || h->root.type == bfd_link_hash_warning)
8576 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8577 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
8578 }
8579 else
8580 {
8581 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
8582 }
8583
8584 if (rsec && !rsec->gc_mark)
8585 {
8586 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
8587 rsec->gc_mark = 1;
8588 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
8589 {
8590 ret = FALSE;
8591 goto out2;
8592 }
8593 }
8594 }
8595
8596 out2:
8597 if (elf_section_data (sec)->relocs != relstart)
8598 free (relstart);
8599 out1:
8600 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
8601 {
8602 if (! info->keep_memory)
8603 free (isym);
8604 else
8605 symtab_hdr->contents = (unsigned char *) isym;
8606 }
8607 }
8608
8609 return ret;
8610 }
8611
8612 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8613
8614 static bfd_boolean
8615 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *idxptr)
8616 {
8617 int *idx = idxptr;
8618
8619 if (h->root.type == bfd_link_hash_warning)
8620 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8621
8622 if (h->dynindx != -1
8623 && ((h->root.type != bfd_link_hash_defined
8624 && h->root.type != bfd_link_hash_defweak)
8625 || h->root.u.def.section->gc_mark))
8626 h->dynindx = (*idx)++;
8627
8628 return TRUE;
8629 }
8630
8631 /* The sweep phase of garbage collection. Remove all garbage sections. */
8632
8633 typedef bfd_boolean (*gc_sweep_hook_fn)
8634 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
8635
8636 static bfd_boolean
8637 elf_gc_sweep (struct bfd_link_info *info, gc_sweep_hook_fn gc_sweep_hook)
8638 {
8639 bfd *sub;
8640
8641 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8642 {
8643 asection *o;
8644
8645 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8646 continue;
8647
8648 for (o = sub->sections; o != NULL; o = o->next)
8649 {
8650 /* Keep debug and special sections. */
8651 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
8652 || (o->flags & (SEC_ALLOC | SEC_LOAD)) == 0)
8653 o->gc_mark = 1;
8654
8655 if (o->gc_mark)
8656 continue;
8657
8658 /* Skip sweeping sections already excluded. */
8659 if (o->flags & SEC_EXCLUDE)
8660 continue;
8661
8662 /* Since this is early in the link process, it is simple
8663 to remove a section from the output. */
8664 o->flags |= SEC_EXCLUDE;
8665
8666 /* But we also have to update some of the relocation
8667 info we collected before. */
8668 if (gc_sweep_hook
8669 && (o->flags & SEC_RELOC) && o->reloc_count > 0)
8670 {
8671 Elf_Internal_Rela *internal_relocs;
8672 bfd_boolean r;
8673
8674 internal_relocs
8675 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
8676 info->keep_memory);
8677 if (internal_relocs == NULL)
8678 return FALSE;
8679
8680 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
8681
8682 if (elf_section_data (o)->relocs != internal_relocs)
8683 free (internal_relocs);
8684
8685 if (!r)
8686 return FALSE;
8687 }
8688 }
8689 }
8690
8691 /* Remove the symbols that were in the swept sections from the dynamic
8692 symbol table. GCFIXME: Anyone know how to get them out of the
8693 static symbol table as well? */
8694 {
8695 int i = 0;
8696
8697 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, &i);
8698
8699 elf_hash_table (info)->dynsymcount = i;
8700 }
8701
8702 return TRUE;
8703 }
8704
8705 /* Propagate collected vtable information. This is called through
8706 elf_link_hash_traverse. */
8707
8708 static bfd_boolean
8709 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
8710 {
8711 if (h->root.type == bfd_link_hash_warning)
8712 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8713
8714 /* Those that are not vtables. */
8715 if (h->vtable == NULL || h->vtable->parent == NULL)
8716 return TRUE;
8717
8718 /* Those vtables that do not have parents, we cannot merge. */
8719 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
8720 return TRUE;
8721
8722 /* If we've already been done, exit. */
8723 if (h->vtable->used && h->vtable->used[-1])
8724 return TRUE;
8725
8726 /* Make sure the parent's table is up to date. */
8727 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
8728
8729 if (h->vtable->used == NULL)
8730 {
8731 /* None of this table's entries were referenced. Re-use the
8732 parent's table. */
8733 h->vtable->used = h->vtable->parent->vtable->used;
8734 h->vtable->size = h->vtable->parent->vtable->size;
8735 }
8736 else
8737 {
8738 size_t n;
8739 bfd_boolean *cu, *pu;
8740
8741 /* Or the parent's entries into ours. */
8742 cu = h->vtable->used;
8743 cu[-1] = TRUE;
8744 pu = h->vtable->parent->vtable->used;
8745 if (pu != NULL)
8746 {
8747 const struct elf_backend_data *bed;
8748 unsigned int log_file_align;
8749
8750 bed = get_elf_backend_data (h->root.u.def.section->owner);
8751 log_file_align = bed->s->log_file_align;
8752 n = h->vtable->parent->vtable->size >> log_file_align;
8753 while (n--)
8754 {
8755 if (*pu)
8756 *cu = TRUE;
8757 pu++;
8758 cu++;
8759 }
8760 }
8761 }
8762
8763 return TRUE;
8764 }
8765
8766 static bfd_boolean
8767 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
8768 {
8769 asection *sec;
8770 bfd_vma hstart, hend;
8771 Elf_Internal_Rela *relstart, *relend, *rel;
8772 const struct elf_backend_data *bed;
8773 unsigned int log_file_align;
8774
8775 if (h->root.type == bfd_link_hash_warning)
8776 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8777
8778 /* Take care of both those symbols that do not describe vtables as
8779 well as those that are not loaded. */
8780 if (h->vtable == NULL || h->vtable->parent == NULL)
8781 return TRUE;
8782
8783 BFD_ASSERT (h->root.type == bfd_link_hash_defined
8784 || h->root.type == bfd_link_hash_defweak);
8785
8786 sec = h->root.u.def.section;
8787 hstart = h->root.u.def.value;
8788 hend = hstart + h->size;
8789
8790 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
8791 if (!relstart)
8792 return *(bfd_boolean *) okp = FALSE;
8793 bed = get_elf_backend_data (sec->owner);
8794 log_file_align = bed->s->log_file_align;
8795
8796 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
8797
8798 for (rel = relstart; rel < relend; ++rel)
8799 if (rel->r_offset >= hstart && rel->r_offset < hend)
8800 {
8801 /* If the entry is in use, do nothing. */
8802 if (h->vtable->used
8803 && (rel->r_offset - hstart) < h->vtable->size)
8804 {
8805 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
8806 if (h->vtable->used[entry])
8807 continue;
8808 }
8809 /* Otherwise, kill it. */
8810 rel->r_offset = rel->r_info = rel->r_addend = 0;
8811 }
8812
8813 return TRUE;
8814 }
8815
8816 /* Mark sections containing dynamically referenced symbols. This is called
8817 through elf_link_hash_traverse. */
8818
8819 static bfd_boolean
8820 elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h,
8821 void *okp ATTRIBUTE_UNUSED)
8822 {
8823 if (h->root.type == bfd_link_hash_warning)
8824 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8825
8826 if ((h->root.type == bfd_link_hash_defined
8827 || h->root.type == bfd_link_hash_defweak)
8828 && h->ref_dynamic)
8829 h->root.u.def.section->flags |= SEC_KEEP;
8830
8831 return TRUE;
8832 }
8833
8834 /* Do mark and sweep of unused sections. */
8835
8836 bfd_boolean
8837 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
8838 {
8839 bfd_boolean ok = TRUE;
8840 bfd *sub;
8841 asection * (*gc_mark_hook)
8842 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
8843 struct elf_link_hash_entry *h, Elf_Internal_Sym *);
8844
8845 if (!get_elf_backend_data (abfd)->can_gc_sections
8846 || info->relocatable
8847 || info->emitrelocations
8848 || info->shared
8849 || !is_elf_hash_table (info->hash))
8850 {
8851 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
8852 return TRUE;
8853 }
8854
8855 /* Apply transitive closure to the vtable entry usage info. */
8856 elf_link_hash_traverse (elf_hash_table (info),
8857 elf_gc_propagate_vtable_entries_used,
8858 &ok);
8859 if (!ok)
8860 return FALSE;
8861
8862 /* Kill the vtable relocations that were not used. */
8863 elf_link_hash_traverse (elf_hash_table (info),
8864 elf_gc_smash_unused_vtentry_relocs,
8865 &ok);
8866 if (!ok)
8867 return FALSE;
8868
8869 /* Mark dynamically referenced symbols. */
8870 if (elf_hash_table (info)->dynamic_sections_created)
8871 elf_link_hash_traverse (elf_hash_table (info),
8872 elf_gc_mark_dynamic_ref_symbol,
8873 &ok);
8874 if (!ok)
8875 return FALSE;
8876
8877 /* Grovel through relocs to find out who stays ... */
8878 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
8879 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8880 {
8881 asection *o;
8882
8883 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8884 continue;
8885
8886 for (o = sub->sections; o != NULL; o = o->next)
8887 {
8888 if (o->flags & SEC_KEEP)
8889 {
8890 /* _bfd_elf_discard_section_eh_frame knows how to discard
8891 orphaned FDEs so don't mark sections referenced by the
8892 EH frame section. */
8893 if (strcmp (o->name, ".eh_frame") == 0)
8894 o->gc_mark = 1;
8895 else if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
8896 return FALSE;
8897 }
8898 }
8899 }
8900
8901 /* ... and mark SEC_EXCLUDE for those that go. */
8902 if (!elf_gc_sweep (info, get_elf_backend_data (abfd)->gc_sweep_hook))
8903 return FALSE;
8904
8905 return TRUE;
8906 }
8907 \f
8908 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
8909
8910 bfd_boolean
8911 bfd_elf_gc_record_vtinherit (bfd *abfd,
8912 asection *sec,
8913 struct elf_link_hash_entry *h,
8914 bfd_vma offset)
8915 {
8916 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
8917 struct elf_link_hash_entry **search, *child;
8918 bfd_size_type extsymcount;
8919 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8920
8921 /* The sh_info field of the symtab header tells us where the
8922 external symbols start. We don't care about the local symbols at
8923 this point. */
8924 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
8925 if (!elf_bad_symtab (abfd))
8926 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
8927
8928 sym_hashes = elf_sym_hashes (abfd);
8929 sym_hashes_end = sym_hashes + extsymcount;
8930
8931 /* Hunt down the child symbol, which is in this section at the same
8932 offset as the relocation. */
8933 for (search = sym_hashes; search != sym_hashes_end; ++search)
8934 {
8935 if ((child = *search) != NULL
8936 && (child->root.type == bfd_link_hash_defined
8937 || child->root.type == bfd_link_hash_defweak)
8938 && child->root.u.def.section == sec
8939 && child->root.u.def.value == offset)
8940 goto win;
8941 }
8942
8943 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
8944 abfd, sec, (unsigned long) offset);
8945 bfd_set_error (bfd_error_invalid_operation);
8946 return FALSE;
8947
8948 win:
8949 if (!child->vtable)
8950 {
8951 child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable));
8952 if (!child->vtable)
8953 return FALSE;
8954 }
8955 if (!h)
8956 {
8957 /* This *should* only be the absolute section. It could potentially
8958 be that someone has defined a non-global vtable though, which
8959 would be bad. It isn't worth paging in the local symbols to be
8960 sure though; that case should simply be handled by the assembler. */
8961
8962 child->vtable->parent = (struct elf_link_hash_entry *) -1;
8963 }
8964 else
8965 child->vtable->parent = h;
8966
8967 return TRUE;
8968 }
8969
8970 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
8971
8972 bfd_boolean
8973 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
8974 asection *sec ATTRIBUTE_UNUSED,
8975 struct elf_link_hash_entry *h,
8976 bfd_vma addend)
8977 {
8978 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8979 unsigned int log_file_align = bed->s->log_file_align;
8980
8981 if (!h->vtable)
8982 {
8983 h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable));
8984 if (!h->vtable)
8985 return FALSE;
8986 }
8987
8988 if (addend >= h->vtable->size)
8989 {
8990 size_t size, bytes, file_align;
8991 bfd_boolean *ptr = h->vtable->used;
8992
8993 /* While the symbol is undefined, we have to be prepared to handle
8994 a zero size. */
8995 file_align = 1 << log_file_align;
8996 if (h->root.type == bfd_link_hash_undefined)
8997 size = addend + file_align;
8998 else
8999 {
9000 size = h->size;
9001 if (addend >= size)
9002 {
9003 /* Oops! We've got a reference past the defined end of
9004 the table. This is probably a bug -- shall we warn? */
9005 size = addend + file_align;
9006 }
9007 }
9008 size = (size + file_align - 1) & -file_align;
9009
9010 /* Allocate one extra entry for use as a "done" flag for the
9011 consolidation pass. */
9012 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
9013
9014 if (ptr)
9015 {
9016 ptr = bfd_realloc (ptr - 1, bytes);
9017
9018 if (ptr != NULL)
9019 {
9020 size_t oldbytes;
9021
9022 oldbytes = (((h->vtable->size >> log_file_align) + 1)
9023 * sizeof (bfd_boolean));
9024 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
9025 }
9026 }
9027 else
9028 ptr = bfd_zmalloc (bytes);
9029
9030 if (ptr == NULL)
9031 return FALSE;
9032
9033 /* And arrange for that done flag to be at index -1. */
9034 h->vtable->used = ptr + 1;
9035 h->vtable->size = size;
9036 }
9037
9038 h->vtable->used[addend >> log_file_align] = TRUE;
9039
9040 return TRUE;
9041 }
9042
9043 struct alloc_got_off_arg {
9044 bfd_vma gotoff;
9045 unsigned int got_elt_size;
9046 };
9047
9048 /* We need a special top-level link routine to convert got reference counts
9049 to real got offsets. */
9050
9051 static bfd_boolean
9052 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
9053 {
9054 struct alloc_got_off_arg *gofarg = arg;
9055
9056 if (h->root.type == bfd_link_hash_warning)
9057 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9058
9059 if (h->got.refcount > 0)
9060 {
9061 h->got.offset = gofarg->gotoff;
9062 gofarg->gotoff += gofarg->got_elt_size;
9063 }
9064 else
9065 h->got.offset = (bfd_vma) -1;
9066
9067 return TRUE;
9068 }
9069
9070 /* And an accompanying bit to work out final got entry offsets once
9071 we're done. Should be called from final_link. */
9072
9073 bfd_boolean
9074 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
9075 struct bfd_link_info *info)
9076 {
9077 bfd *i;
9078 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9079 bfd_vma gotoff;
9080 unsigned int got_elt_size = bed->s->arch_size / 8;
9081 struct alloc_got_off_arg gofarg;
9082
9083 if (! is_elf_hash_table (info->hash))
9084 return FALSE;
9085
9086 /* The GOT offset is relative to the .got section, but the GOT header is
9087 put into the .got.plt section, if the backend uses it. */
9088 if (bed->want_got_plt)
9089 gotoff = 0;
9090 else
9091 gotoff = bed->got_header_size;
9092
9093 /* Do the local .got entries first. */
9094 for (i = info->input_bfds; i; i = i->link_next)
9095 {
9096 bfd_signed_vma *local_got;
9097 bfd_size_type j, locsymcount;
9098 Elf_Internal_Shdr *symtab_hdr;
9099
9100 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
9101 continue;
9102
9103 local_got = elf_local_got_refcounts (i);
9104 if (!local_got)
9105 continue;
9106
9107 symtab_hdr = &elf_tdata (i)->symtab_hdr;
9108 if (elf_bad_symtab (i))
9109 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9110 else
9111 locsymcount = symtab_hdr->sh_info;
9112
9113 for (j = 0; j < locsymcount; ++j)
9114 {
9115 if (local_got[j] > 0)
9116 {
9117 local_got[j] = gotoff;
9118 gotoff += got_elt_size;
9119 }
9120 else
9121 local_got[j] = (bfd_vma) -1;
9122 }
9123 }
9124
9125 /* Then the global .got entries. .plt refcounts are handled by
9126 adjust_dynamic_symbol */
9127 gofarg.gotoff = gotoff;
9128 gofarg.got_elt_size = got_elt_size;
9129 elf_link_hash_traverse (elf_hash_table (info),
9130 elf_gc_allocate_got_offsets,
9131 &gofarg);
9132 return TRUE;
9133 }
9134
9135 /* Many folk need no more in the way of final link than this, once
9136 got entry reference counting is enabled. */
9137
9138 bfd_boolean
9139 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
9140 {
9141 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
9142 return FALSE;
9143
9144 /* Invoke the regular ELF backend linker to do all the work. */
9145 return bfd_elf_final_link (abfd, info);
9146 }
9147
9148 bfd_boolean
9149 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
9150 {
9151 struct elf_reloc_cookie *rcookie = cookie;
9152
9153 if (rcookie->bad_symtab)
9154 rcookie->rel = rcookie->rels;
9155
9156 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
9157 {
9158 unsigned long r_symndx;
9159
9160 if (! rcookie->bad_symtab)
9161 if (rcookie->rel->r_offset > offset)
9162 return FALSE;
9163 if (rcookie->rel->r_offset != offset)
9164 continue;
9165
9166 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
9167 if (r_symndx == SHN_UNDEF)
9168 return TRUE;
9169
9170 if (r_symndx >= rcookie->locsymcount
9171 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
9172 {
9173 struct elf_link_hash_entry *h;
9174
9175 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
9176
9177 while (h->root.type == bfd_link_hash_indirect
9178 || h->root.type == bfd_link_hash_warning)
9179 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9180
9181 if ((h->root.type == bfd_link_hash_defined
9182 || h->root.type == bfd_link_hash_defweak)
9183 && elf_discarded_section (h->root.u.def.section))
9184 return TRUE;
9185 else
9186 return FALSE;
9187 }
9188 else
9189 {
9190 /* It's not a relocation against a global symbol,
9191 but it could be a relocation against a local
9192 symbol for a discarded section. */
9193 asection *isec;
9194 Elf_Internal_Sym *isym;
9195
9196 /* Need to: get the symbol; get the section. */
9197 isym = &rcookie->locsyms[r_symndx];
9198 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
9199 {
9200 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
9201 if (isec != NULL && elf_discarded_section (isec))
9202 return TRUE;
9203 }
9204 }
9205 return FALSE;
9206 }
9207 return FALSE;
9208 }
9209
9210 /* Discard unneeded references to discarded sections.
9211 Returns TRUE if any section's size was changed. */
9212 /* This function assumes that the relocations are in sorted order,
9213 which is true for all known assemblers. */
9214
9215 bfd_boolean
9216 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
9217 {
9218 struct elf_reloc_cookie cookie;
9219 asection *stab, *eh;
9220 Elf_Internal_Shdr *symtab_hdr;
9221 const struct elf_backend_data *bed;
9222 bfd *abfd;
9223 unsigned int count;
9224 bfd_boolean ret = FALSE;
9225
9226 if (info->traditional_format
9227 || !is_elf_hash_table (info->hash))
9228 return FALSE;
9229
9230 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
9231 {
9232 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
9233 continue;
9234
9235 bed = get_elf_backend_data (abfd);
9236
9237 if ((abfd->flags & DYNAMIC) != 0)
9238 continue;
9239
9240 eh = bfd_get_section_by_name (abfd, ".eh_frame");
9241 if (info->relocatable
9242 || (eh != NULL
9243 && (eh->size == 0
9244 || bfd_is_abs_section (eh->output_section))))
9245 eh = NULL;
9246
9247 stab = bfd_get_section_by_name (abfd, ".stab");
9248 if (stab != NULL
9249 && (stab->size == 0
9250 || bfd_is_abs_section (stab->output_section)
9251 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
9252 stab = NULL;
9253
9254 if (stab == NULL
9255 && eh == NULL
9256 && bed->elf_backend_discard_info == NULL)
9257 continue;
9258
9259 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9260 cookie.abfd = abfd;
9261 cookie.sym_hashes = elf_sym_hashes (abfd);
9262 cookie.bad_symtab = elf_bad_symtab (abfd);
9263 if (cookie.bad_symtab)
9264 {
9265 cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9266 cookie.extsymoff = 0;
9267 }
9268 else
9269 {
9270 cookie.locsymcount = symtab_hdr->sh_info;
9271 cookie.extsymoff = symtab_hdr->sh_info;
9272 }
9273
9274 if (bed->s->arch_size == 32)
9275 cookie.r_sym_shift = 8;
9276 else
9277 cookie.r_sym_shift = 32;
9278
9279 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
9280 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
9281 {
9282 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9283 cookie.locsymcount, 0,
9284 NULL, NULL, NULL);
9285 if (cookie.locsyms == NULL)
9286 return FALSE;
9287 }
9288
9289 if (stab != NULL)
9290 {
9291 cookie.rels = NULL;
9292 count = stab->reloc_count;
9293 if (count != 0)
9294 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
9295 info->keep_memory);
9296 if (cookie.rels != NULL)
9297 {
9298 cookie.rel = cookie.rels;
9299 cookie.relend = cookie.rels;
9300 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9301 if (_bfd_discard_section_stabs (abfd, stab,
9302 elf_section_data (stab)->sec_info,
9303 bfd_elf_reloc_symbol_deleted_p,
9304 &cookie))
9305 ret = TRUE;
9306 if (elf_section_data (stab)->relocs != cookie.rels)
9307 free (cookie.rels);
9308 }
9309 }
9310
9311 if (eh != NULL)
9312 {
9313 cookie.rels = NULL;
9314 count = eh->reloc_count;
9315 if (count != 0)
9316 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
9317 info->keep_memory);
9318 cookie.rel = cookie.rels;
9319 cookie.relend = cookie.rels;
9320 if (cookie.rels != NULL)
9321 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9322
9323 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
9324 bfd_elf_reloc_symbol_deleted_p,
9325 &cookie))
9326 ret = TRUE;
9327
9328 if (cookie.rels != NULL
9329 && elf_section_data (eh)->relocs != cookie.rels)
9330 free (cookie.rels);
9331 }
9332
9333 if (bed->elf_backend_discard_info != NULL
9334 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
9335 ret = TRUE;
9336
9337 if (cookie.locsyms != NULL
9338 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
9339 {
9340 if (! info->keep_memory)
9341 free (cookie.locsyms);
9342 else
9343 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
9344 }
9345 }
9346
9347 if (info->eh_frame_hdr
9348 && !info->relocatable
9349 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
9350 ret = TRUE;
9351
9352 return ret;
9353 }
9354
9355 void
9356 _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section * sec)
9357 {
9358 flagword flags;
9359 const char *name, *p;
9360 struct bfd_section_already_linked *l;
9361 struct bfd_section_already_linked_hash_entry *already_linked_list;
9362 asection *group;
9363
9364 /* A single member comdat group section may be discarded by a
9365 linkonce section. See below. */
9366 if (sec->output_section == bfd_abs_section_ptr)
9367 return;
9368
9369 flags = sec->flags;
9370
9371 /* Check if it belongs to a section group. */
9372 group = elf_sec_group (sec);
9373
9374 /* Return if it isn't a linkonce section nor a member of a group. A
9375 comdat group section also has SEC_LINK_ONCE set. */
9376 if ((flags & SEC_LINK_ONCE) == 0 && group == NULL)
9377 return;
9378
9379 if (group)
9380 {
9381 /* If this is the member of a single member comdat group, check if
9382 the group should be discarded. */
9383 if (elf_next_in_group (sec) == sec
9384 && (group->flags & SEC_LINK_ONCE) != 0)
9385 sec = group;
9386 else
9387 return;
9388 }
9389
9390 /* FIXME: When doing a relocatable link, we may have trouble
9391 copying relocations in other sections that refer to local symbols
9392 in the section being discarded. Those relocations will have to
9393 be converted somehow; as of this writing I'm not sure that any of
9394 the backends handle that correctly.
9395
9396 It is tempting to instead not discard link once sections when
9397 doing a relocatable link (technically, they should be discarded
9398 whenever we are building constructors). However, that fails,
9399 because the linker winds up combining all the link once sections
9400 into a single large link once section, which defeats the purpose
9401 of having link once sections in the first place.
9402
9403 Also, not merging link once sections in a relocatable link
9404 causes trouble for MIPS ELF, which relies on link once semantics
9405 to handle the .reginfo section correctly. */
9406
9407 name = bfd_get_section_name (abfd, sec);
9408
9409 if (strncmp (name, ".gnu.linkonce.", sizeof (".gnu.linkonce.") - 1) == 0
9410 && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
9411 p++;
9412 else
9413 p = name;
9414
9415 already_linked_list = bfd_section_already_linked_table_lookup (p);
9416
9417 for (l = already_linked_list->entry; l != NULL; l = l->next)
9418 {
9419 /* We may have 3 different sections on the list: group section,
9420 comdat section and linkonce section. SEC may be a linkonce or
9421 group section. We match a group section with a group section,
9422 a linkonce section with a linkonce section, and ignore comdat
9423 section. */
9424 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
9425 && strcmp (name, l->sec->name) == 0
9426 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
9427 {
9428 /* The section has already been linked. See if we should
9429 issue a warning. */
9430 switch (flags & SEC_LINK_DUPLICATES)
9431 {
9432 default:
9433 abort ();
9434
9435 case SEC_LINK_DUPLICATES_DISCARD:
9436 break;
9437
9438 case SEC_LINK_DUPLICATES_ONE_ONLY:
9439 (*_bfd_error_handler)
9440 (_("%B: ignoring duplicate section `%A'\n"),
9441 abfd, sec);
9442 break;
9443
9444 case SEC_LINK_DUPLICATES_SAME_SIZE:
9445 if (sec->size != l->sec->size)
9446 (*_bfd_error_handler)
9447 (_("%B: duplicate section `%A' has different size\n"),
9448 abfd, sec);
9449 break;
9450
9451 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
9452 if (sec->size != l->sec->size)
9453 (*_bfd_error_handler)
9454 (_("%B: duplicate section `%A' has different size\n"),
9455 abfd, sec);
9456 else if (sec->size != 0)
9457 {
9458 bfd_byte *sec_contents, *l_sec_contents;
9459
9460 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
9461 (*_bfd_error_handler)
9462 (_("%B: warning: could not read contents of section `%A'\n"),
9463 abfd, sec);
9464 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
9465 &l_sec_contents))
9466 (*_bfd_error_handler)
9467 (_("%B: warning: could not read contents of section `%A'\n"),
9468 l->sec->owner, l->sec);
9469 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
9470 (*_bfd_error_handler)
9471 (_("%B: warning: duplicate section `%A' has different contents\n"),
9472 abfd, sec);
9473
9474 if (sec_contents)
9475 free (sec_contents);
9476 if (l_sec_contents)
9477 free (l_sec_contents);
9478 }
9479 break;
9480 }
9481
9482 /* Set the output_section field so that lang_add_section
9483 does not create a lang_input_section structure for this
9484 section. Since there might be a symbol in the section
9485 being discarded, we must retain a pointer to the section
9486 which we are really going to use. */
9487 sec->output_section = bfd_abs_section_ptr;
9488 sec->kept_section = l->sec;
9489
9490 if (flags & SEC_GROUP)
9491 {
9492 asection *first = elf_next_in_group (sec);
9493 asection *s = first;
9494
9495 while (s != NULL)
9496 {
9497 s->output_section = bfd_abs_section_ptr;
9498 /* Record which group discards it. */
9499 s->kept_section = l->sec;
9500 s = elf_next_in_group (s);
9501 /* These lists are circular. */
9502 if (s == first)
9503 break;
9504 }
9505 }
9506
9507 return;
9508 }
9509 }
9510
9511 if (group)
9512 {
9513 /* If this is the member of a single member comdat group and the
9514 group hasn't be discarded, we check if it matches a linkonce
9515 section. We only record the discarded comdat group. Otherwise
9516 the undiscarded group will be discarded incorrectly later since
9517 itself has been recorded. */
9518 for (l = already_linked_list->entry; l != NULL; l = l->next)
9519 if ((l->sec->flags & SEC_GROUP) == 0
9520 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
9521 && bfd_elf_match_symbols_in_sections (l->sec,
9522 elf_next_in_group (sec)))
9523 {
9524 elf_next_in_group (sec)->output_section = bfd_abs_section_ptr;
9525 elf_next_in_group (sec)->kept_section = l->sec;
9526 group->output_section = bfd_abs_section_ptr;
9527 break;
9528 }
9529 if (l == NULL)
9530 return;
9531 }
9532 else
9533 /* There is no direct match. But for linkonce section, we should
9534 check if there is a match with comdat group member. We always
9535 record the linkonce section, discarded or not. */
9536 for (l = already_linked_list->entry; l != NULL; l = l->next)
9537 if (l->sec->flags & SEC_GROUP)
9538 {
9539 asection *first = elf_next_in_group (l->sec);
9540
9541 if (first != NULL
9542 && elf_next_in_group (first) == first
9543 && bfd_elf_match_symbols_in_sections (first, sec))
9544 {
9545 sec->output_section = bfd_abs_section_ptr;
9546 sec->kept_section = l->sec;
9547 break;
9548 }
9549 }
9550
9551 /* This is the first section with this name. Record it. */
9552 bfd_section_already_linked_table_insert (already_linked_list, sec);
9553 }
This page took 0.460981 seconds and 4 git commands to generate.