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