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