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