* elfxx-target.h (bfd_elfNN_bfd_link_add_symbols): Define.
[binutils-gdb.git] / bfd / elflink.c
1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #define ARCH_SIZE 0
26 #include "elf-bfd.h"
27 #include "safe-ctype.h"
28
29 bfd_boolean
30 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
31 {
32 flagword flags;
33 asection *s;
34 struct elf_link_hash_entry *h;
35 struct bfd_link_hash_entry *bh;
36 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
37 int ptralign;
38
39 /* This function may be called more than once. */
40 s = bfd_get_section_by_name (abfd, ".got");
41 if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
42 return TRUE;
43
44 switch (bed->s->arch_size)
45 {
46 case 32:
47 ptralign = 2;
48 break;
49
50 case 64:
51 ptralign = 3;
52 break;
53
54 default:
55 bfd_set_error (bfd_error_bad_value);
56 return FALSE;
57 }
58
59 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
60 | SEC_LINKER_CREATED);
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->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
90 h->type = STT_OBJECT;
91
92 if (! info->executable
93 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
94 return FALSE;
95
96 elf_hash_table (info)->hgot = h;
97 }
98
99 /* The first bit of the global offset table is the header. */
100 s->_raw_size += bed->got_header_size + bed->got_symbol_offset;
101
102 return TRUE;
103 }
104 \f
105 /* Create some sections which will be filled in with dynamic linking
106 information. ABFD is an input file which requires dynamic sections
107 to be created. The dynamic sections take up virtual memory space
108 when the final executable is run, so we need to create them before
109 addresses are assigned to the output sections. We work out the
110 actual contents and size of these sections later. */
111
112 bfd_boolean
113 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
114 {
115 flagword flags;
116 register asection *s;
117 struct elf_link_hash_entry *h;
118 struct bfd_link_hash_entry *bh;
119 const struct elf_backend_data *bed;
120
121 if (! is_elf_hash_table (info->hash))
122 return FALSE;
123
124 if (elf_hash_table (info)->dynamic_sections_created)
125 return TRUE;
126
127 /* Make sure that all dynamic sections use the same input BFD. */
128 if (elf_hash_table (info)->dynobj == NULL)
129 elf_hash_table (info)->dynobj = abfd;
130 else
131 abfd = elf_hash_table (info)->dynobj;
132
133 /* Note that we set the SEC_IN_MEMORY flag for all of these
134 sections. */
135 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
136 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
137
138 /* A dynamically linked executable has a .interp section, but a
139 shared library does not. */
140 if (info->executable)
141 {
142 s = bfd_make_section (abfd, ".interp");
143 if (s == NULL
144 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
145 return FALSE;
146 }
147
148 if (! info->traditional_format)
149 {
150 s = bfd_make_section (abfd, ".eh_frame_hdr");
151 if (s == NULL
152 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
153 || ! bfd_set_section_alignment (abfd, s, 2))
154 return FALSE;
155 elf_hash_table (info)->eh_info.hdr_sec = s;
156 }
157
158 bed = get_elf_backend_data (abfd);
159
160 /* Create sections to hold version informations. These are removed
161 if they are not needed. */
162 s = bfd_make_section (abfd, ".gnu.version_d");
163 if (s == NULL
164 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
165 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
166 return FALSE;
167
168 s = bfd_make_section (abfd, ".gnu.version");
169 if (s == NULL
170 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
171 || ! bfd_set_section_alignment (abfd, s, 1))
172 return FALSE;
173
174 s = bfd_make_section (abfd, ".gnu.version_r");
175 if (s == NULL
176 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
177 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
178 return FALSE;
179
180 s = bfd_make_section (abfd, ".dynsym");
181 if (s == NULL
182 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
183 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
184 return FALSE;
185
186 s = bfd_make_section (abfd, ".dynstr");
187 if (s == NULL
188 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
189 return FALSE;
190
191 /* Create a strtab to hold the dynamic symbol names. */
192 if (elf_hash_table (info)->dynstr == NULL)
193 {
194 elf_hash_table (info)->dynstr = _bfd_elf_strtab_init ();
195 if (elf_hash_table (info)->dynstr == NULL)
196 return FALSE;
197 }
198
199 s = bfd_make_section (abfd, ".dynamic");
200 if (s == NULL
201 || ! bfd_set_section_flags (abfd, s, flags)
202 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
203 return FALSE;
204
205 /* The special symbol _DYNAMIC is always set to the start of the
206 .dynamic section. This call occurs before we have processed the
207 symbols for any dynamic object, so we don't have to worry about
208 overriding a dynamic definition. We could set _DYNAMIC in a
209 linker script, but we only want to define it if we are, in fact,
210 creating a .dynamic section. We don't want to define it if there
211 is no .dynamic section, since on some ELF platforms the start up
212 code examines it to decide how to initialize the process. */
213 bh = NULL;
214 if (! (_bfd_generic_link_add_one_symbol
215 (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, 0, NULL, FALSE,
216 get_elf_backend_data (abfd)->collect, &bh)))
217 return FALSE;
218 h = (struct elf_link_hash_entry *) bh;
219 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
220 h->type = STT_OBJECT;
221
222 if (! info->executable
223 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
224 return FALSE;
225
226 s = bfd_make_section (abfd, ".hash");
227 if (s == NULL
228 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
229 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
230 return FALSE;
231 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
232
233 /* Let the backend create the rest of the sections. This lets the
234 backend set the right flags. The backend will normally create
235 the .got and .plt sections. */
236 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
237 return FALSE;
238
239 elf_hash_table (info)->dynamic_sections_created = TRUE;
240
241 return TRUE;
242 }
243
244 /* Create dynamic sections when linking against a dynamic object. */
245
246 bfd_boolean
247 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
248 {
249 flagword flags, pltflags;
250 asection *s;
251 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252
253 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
254 .rel[a].bss sections. */
255
256 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
257 | SEC_LINKER_CREATED);
258
259 pltflags = flags;
260 pltflags |= SEC_CODE;
261 if (bed->plt_not_loaded)
262 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
263 if (bed->plt_readonly)
264 pltflags |= SEC_READONLY;
265
266 s = bfd_make_section (abfd, ".plt");
267 if (s == NULL
268 || ! bfd_set_section_flags (abfd, s, pltflags)
269 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
270 return FALSE;
271
272 if (bed->want_plt_sym)
273 {
274 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
275 .plt section. */
276 struct elf_link_hash_entry *h;
277 struct bfd_link_hash_entry *bh = NULL;
278
279 if (! (_bfd_generic_link_add_one_symbol
280 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s, 0, NULL,
281 FALSE, get_elf_backend_data (abfd)->collect, &bh)))
282 return FALSE;
283 h = (struct elf_link_hash_entry *) bh;
284 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
285 h->type = STT_OBJECT;
286
287 if (! info->executable
288 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
289 return FALSE;
290 }
291
292 s = bfd_make_section (abfd,
293 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
294 if (s == NULL
295 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
296 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
297 return FALSE;
298
299 if (! _bfd_elf_create_got_section (abfd, info))
300 return FALSE;
301
302 if (bed->want_dynbss)
303 {
304 /* The .dynbss section is a place to put symbols which are defined
305 by dynamic objects, are referenced by regular objects, and are
306 not functions. We must allocate space for them in the process
307 image and use a R_*_COPY reloc to tell the dynamic linker to
308 initialize them at run time. The linker script puts the .dynbss
309 section into the .bss section of the final image. */
310 s = bfd_make_section (abfd, ".dynbss");
311 if (s == NULL
312 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC | SEC_LINKER_CREATED))
313 return FALSE;
314
315 /* The .rel[a].bss section holds copy relocs. This section is not
316 normally needed. We need to create it here, though, so that the
317 linker will map it to an output section. We can't just create it
318 only if we need it, because we will not know whether we need it
319 until we have seen all the input files, and the first time the
320 main linker code calls BFD after examining all the input files
321 (size_dynamic_sections) the input sections have already been
322 mapped to the output sections. If the section turns out not to
323 be needed, we can discard it later. We will never need this
324 section when generating a shared object, since they do not use
325 copy relocs. */
326 if (! info->shared)
327 {
328 s = bfd_make_section (abfd,
329 (bed->default_use_rela_p
330 ? ".rela.bss" : ".rel.bss"));
331 if (s == NULL
332 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
333 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
334 return FALSE;
335 }
336 }
337
338 return TRUE;
339 }
340 \f
341 /* Record a new dynamic symbol. We record the dynamic symbols as we
342 read the input files, since we need to have a list of all of them
343 before we can determine the final sizes of the output sections.
344 Note that we may actually call this function even though we are not
345 going to output any dynamic symbols; in some cases we know that a
346 symbol should be in the dynamic symbol table, but only if there is
347 one. */
348
349 bfd_boolean
350 _bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
351 struct elf_link_hash_entry *h)
352 {
353 if (h->dynindx == -1)
354 {
355 struct elf_strtab_hash *dynstr;
356 char *p;
357 const char *name;
358 bfd_size_type indx;
359
360 /* XXX: The ABI draft says the linker must turn hidden and
361 internal symbols into STB_LOCAL symbols when producing the
362 DSO. However, if ld.so honors st_other in the dynamic table,
363 this would not be necessary. */
364 switch (ELF_ST_VISIBILITY (h->other))
365 {
366 case STV_INTERNAL:
367 case STV_HIDDEN:
368 if (h->root.type != bfd_link_hash_undefined
369 && h->root.type != bfd_link_hash_undefweak)
370 {
371 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
372 return TRUE;
373 }
374
375 default:
376 break;
377 }
378
379 h->dynindx = elf_hash_table (info)->dynsymcount;
380 ++elf_hash_table (info)->dynsymcount;
381
382 dynstr = elf_hash_table (info)->dynstr;
383 if (dynstr == NULL)
384 {
385 /* Create a strtab to hold the dynamic symbol names. */
386 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
387 if (dynstr == NULL)
388 return FALSE;
389 }
390
391 /* We don't put any version information in the dynamic string
392 table. */
393 name = h->root.root.string;
394 p = strchr (name, ELF_VER_CHR);
395 if (p != NULL)
396 /* We know that the p points into writable memory. In fact,
397 there are only a few symbols that have read-only names, being
398 those like _GLOBAL_OFFSET_TABLE_ that are created specially
399 by the backends. Most symbols will have names pointing into
400 an ELF string table read from a file, or to objalloc memory. */
401 *p = 0;
402
403 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
404
405 if (p != NULL)
406 *p = ELF_VER_CHR;
407
408 if (indx == (bfd_size_type) -1)
409 return FALSE;
410 h->dynstr_index = indx;
411 }
412
413 return TRUE;
414 }
415 \f
416 /* Record an assignment to a symbol made by a linker script. We need
417 this in case some dynamic object refers to this symbol. */
418
419 bfd_boolean
420 bfd_elf_record_link_assignment (bfd *output_bfd ATTRIBUTE_UNUSED,
421 struct bfd_link_info *info,
422 const char *name,
423 bfd_boolean provide)
424 {
425 struct elf_link_hash_entry *h;
426
427 if (!is_elf_hash_table (info->hash))
428 return TRUE;
429
430 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, TRUE, FALSE);
431 if (h == NULL)
432 return FALSE;
433
434 /* Since we're defining the symbol, don't let it seem to have not
435 been defined. record_dynamic_symbol and size_dynamic_sections
436 may depend on this. */
437 if (h->root.type == bfd_link_hash_undefweak
438 || h->root.type == bfd_link_hash_undefined)
439 h->root.type = bfd_link_hash_new;
440
441 if (h->root.type == bfd_link_hash_new)
442 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
443
444 /* If this symbol is being provided by the linker script, and it is
445 currently defined by a dynamic object, but not by a regular
446 object, then mark it as undefined so that the generic linker will
447 force the correct value. */
448 if (provide
449 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
450 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
451 h->root.type = bfd_link_hash_undefined;
452
453 /* If this symbol is not being provided by the linker script, and it is
454 currently defined by a dynamic object, but not by a regular object,
455 then clear out any version information because the symbol will not be
456 associated with the dynamic object any more. */
457 if (!provide
458 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
459 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
460 h->verinfo.verdef = NULL;
461
462 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
463
464 if (((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC
465 | ELF_LINK_HASH_REF_DYNAMIC)) != 0
466 || info->shared)
467 && h->dynindx == -1)
468 {
469 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
470 return FALSE;
471
472 /* If this is a weak defined symbol, and we know a corresponding
473 real symbol from the same dynamic object, make sure the real
474 symbol is also made into a dynamic symbol. */
475 if (h->weakdef != NULL
476 && h->weakdef->dynindx == -1)
477 {
478 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
479 return FALSE;
480 }
481 }
482
483 return TRUE;
484 }
485
486 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
487 success, and 2 on a failure caused by attempting to record a symbol
488 in a discarded section, eg. a discarded link-once section symbol. */
489
490 int
491 elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
492 bfd *input_bfd,
493 long input_indx)
494 {
495 bfd_size_type amt;
496 struct elf_link_local_dynamic_entry *entry;
497 struct elf_link_hash_table *eht;
498 struct elf_strtab_hash *dynstr;
499 unsigned long dynstr_index;
500 char *name;
501 Elf_External_Sym_Shndx eshndx;
502 char esym[sizeof (Elf64_External_Sym)];
503
504 if (! is_elf_hash_table (info->hash))
505 return 0;
506
507 /* See if the entry exists already. */
508 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
509 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
510 return 1;
511
512 amt = sizeof (*entry);
513 entry = bfd_alloc (input_bfd, amt);
514 if (entry == NULL)
515 return 0;
516
517 /* Go find the symbol, so that we can find it's name. */
518 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
519 1, input_indx, &entry->isym, esym, &eshndx))
520 {
521 bfd_release (input_bfd, entry);
522 return 0;
523 }
524
525 if (entry->isym.st_shndx != SHN_UNDEF
526 && (entry->isym.st_shndx < SHN_LORESERVE
527 || entry->isym.st_shndx > SHN_HIRESERVE))
528 {
529 asection *s;
530
531 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
532 if (s == NULL || bfd_is_abs_section (s->output_section))
533 {
534 /* We can still bfd_release here as nothing has done another
535 bfd_alloc. We can't do this later in this function. */
536 bfd_release (input_bfd, entry);
537 return 2;
538 }
539 }
540
541 name = (bfd_elf_string_from_elf_section
542 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
543 entry->isym.st_name));
544
545 dynstr = elf_hash_table (info)->dynstr;
546 if (dynstr == NULL)
547 {
548 /* Create a strtab to hold the dynamic symbol names. */
549 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
550 if (dynstr == NULL)
551 return 0;
552 }
553
554 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
555 if (dynstr_index == (unsigned long) -1)
556 return 0;
557 entry->isym.st_name = dynstr_index;
558
559 eht = elf_hash_table (info);
560
561 entry->next = eht->dynlocal;
562 eht->dynlocal = entry;
563 entry->input_bfd = input_bfd;
564 entry->input_indx = input_indx;
565 eht->dynsymcount++;
566
567 /* Whatever binding the symbol had before, it's now local. */
568 entry->isym.st_info
569 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
570
571 /* The dynindx will be set at the end of size_dynamic_sections. */
572
573 return 1;
574 }
575
576 /* Return the dynindex of a local dynamic symbol. */
577
578 long
579 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
580 bfd *input_bfd,
581 long input_indx)
582 {
583 struct elf_link_local_dynamic_entry *e;
584
585 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
586 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
587 return e->dynindx;
588 return -1;
589 }
590
591 /* This function is used to renumber the dynamic symbols, if some of
592 them are removed because they are marked as local. This is called
593 via elf_link_hash_traverse. */
594
595 static bfd_boolean
596 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
597 void *data)
598 {
599 size_t *count = data;
600
601 if (h->root.type == bfd_link_hash_warning)
602 h = (struct elf_link_hash_entry *) h->root.u.i.link;
603
604 if (h->dynindx != -1)
605 h->dynindx = ++(*count);
606
607 return TRUE;
608 }
609
610 /* Assign dynsym indices. In a shared library we generate a section
611 symbol for each output section, which come first. Next come all of
612 the back-end allocated local dynamic syms, followed by the rest of
613 the global symbols. */
614
615 unsigned long
616 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
617 {
618 unsigned long dynsymcount = 0;
619
620 if (info->shared)
621 {
622 asection *p;
623 for (p = output_bfd->sections; p ; p = p->next)
624 if ((p->flags & SEC_EXCLUDE) == 0)
625 elf_section_data (p)->dynindx = ++dynsymcount;
626 }
627
628 if (elf_hash_table (info)->dynlocal)
629 {
630 struct elf_link_local_dynamic_entry *p;
631 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
632 p->dynindx = ++dynsymcount;
633 }
634
635 elf_link_hash_traverse (elf_hash_table (info),
636 elf_link_renumber_hash_table_dynsyms,
637 &dynsymcount);
638
639 /* There is an unused NULL entry at the head of the table which
640 we must account for in our count. Unless there weren't any
641 symbols, which means we'll have no table at all. */
642 if (dynsymcount != 0)
643 ++dynsymcount;
644
645 return elf_hash_table (info)->dynsymcount = dynsymcount;
646 }
647
648 /* This function is called when we want to define a new symbol. It
649 handles the various cases which arise when we find a definition in
650 a dynamic object, or when there is already a definition in a
651 dynamic object. The new symbol is described by NAME, SYM, PSEC,
652 and PVALUE. We set SYM_HASH to the hash table entry. We set
653 OVERRIDE if the old symbol is overriding a new definition. We set
654 TYPE_CHANGE_OK if it is OK for the type to change. We set
655 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
656 change, we mean that we shouldn't warn if the type or size does
657 change. */
658
659 bfd_boolean
660 _bfd_elf_merge_symbol (bfd *abfd,
661 struct bfd_link_info *info,
662 const char *name,
663 Elf_Internal_Sym *sym,
664 asection **psec,
665 bfd_vma *pvalue,
666 struct elf_link_hash_entry **sym_hash,
667 bfd_boolean *skip,
668 bfd_boolean *override,
669 bfd_boolean *type_change_ok,
670 bfd_boolean *size_change_ok)
671 {
672 asection *sec;
673 struct elf_link_hash_entry *h;
674 struct elf_link_hash_entry *flip;
675 int bind;
676 bfd *oldbfd;
677 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
678 bfd_boolean newweak, oldweak;
679
680 *skip = FALSE;
681 *override = FALSE;
682
683 sec = *psec;
684 bind = ELF_ST_BIND (sym->st_info);
685
686 if (! bfd_is_und_section (sec))
687 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
688 else
689 h = ((struct elf_link_hash_entry *)
690 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
691 if (h == NULL)
692 return FALSE;
693 *sym_hash = h;
694
695 /* This code is for coping with dynamic objects, and is only useful
696 if we are doing an ELF link. */
697 if (info->hash->creator != abfd->xvec)
698 return TRUE;
699
700 /* For merging, we only care about real symbols. */
701
702 while (h->root.type == bfd_link_hash_indirect
703 || h->root.type == bfd_link_hash_warning)
704 h = (struct elf_link_hash_entry *) h->root.u.i.link;
705
706 /* If we just created the symbol, mark it as being an ELF symbol.
707 Other than that, there is nothing to do--there is no merge issue
708 with a newly defined symbol--so we just return. */
709
710 if (h->root.type == bfd_link_hash_new)
711 {
712 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
713 return TRUE;
714 }
715
716 /* OLDBFD is a BFD associated with the existing symbol. */
717
718 switch (h->root.type)
719 {
720 default:
721 oldbfd = NULL;
722 break;
723
724 case bfd_link_hash_undefined:
725 case bfd_link_hash_undefweak:
726 oldbfd = h->root.u.undef.abfd;
727 break;
728
729 case bfd_link_hash_defined:
730 case bfd_link_hash_defweak:
731 oldbfd = h->root.u.def.section->owner;
732 break;
733
734 case bfd_link_hash_common:
735 oldbfd = h->root.u.c.p->section->owner;
736 break;
737 }
738
739 /* In cases involving weak versioned symbols, we may wind up trying
740 to merge a symbol with itself. Catch that here, to avoid the
741 confusion that results if we try to override a symbol with
742 itself. The additional tests catch cases like
743 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
744 dynamic object, which we do want to handle here. */
745 if (abfd == oldbfd
746 && ((abfd->flags & DYNAMIC) == 0
747 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0))
748 return TRUE;
749
750 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
751 respectively, is from a dynamic object. */
752
753 if ((abfd->flags & DYNAMIC) != 0)
754 newdyn = TRUE;
755 else
756 newdyn = FALSE;
757
758 if (oldbfd != NULL)
759 olddyn = (oldbfd->flags & DYNAMIC) != 0;
760 else
761 {
762 asection *hsec;
763
764 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
765 indices used by MIPS ELF. */
766 switch (h->root.type)
767 {
768 default:
769 hsec = NULL;
770 break;
771
772 case bfd_link_hash_defined:
773 case bfd_link_hash_defweak:
774 hsec = h->root.u.def.section;
775 break;
776
777 case bfd_link_hash_common:
778 hsec = h->root.u.c.p->section;
779 break;
780 }
781
782 if (hsec == NULL)
783 olddyn = FALSE;
784 else
785 olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0;
786 }
787
788 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
789 respectively, appear to be a definition rather than reference. */
790
791 if (bfd_is_und_section (sec) || bfd_is_com_section (sec))
792 newdef = FALSE;
793 else
794 newdef = TRUE;
795
796 if (h->root.type == bfd_link_hash_undefined
797 || h->root.type == bfd_link_hash_undefweak
798 || h->root.type == bfd_link_hash_common)
799 olddef = FALSE;
800 else
801 olddef = TRUE;
802
803 /* We need to remember if a symbol has a definition in a dynamic
804 object or is weak in all dynamic objects. Internal and hidden
805 visibility will make it unavailable to dynamic objects. */
806 if (newdyn && (h->elf_link_hash_flags & ELF_LINK_DYNAMIC_DEF) == 0)
807 {
808 if (!bfd_is_und_section (sec))
809 h->elf_link_hash_flags |= ELF_LINK_DYNAMIC_DEF;
810 else
811 {
812 /* Check if this symbol is weak in all dynamic objects. If it
813 is the first time we see it in a dynamic object, we mark
814 if it is weak. Otherwise, we clear it. */
815 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0)
816 {
817 if (bind == STB_WEAK)
818 h->elf_link_hash_flags |= ELF_LINK_DYNAMIC_WEAK;
819 }
820 else if (bind != STB_WEAK)
821 h->elf_link_hash_flags &= ~ELF_LINK_DYNAMIC_WEAK;
822 }
823 }
824
825 /* If the old symbol has non-default visibility, we ignore the new
826 definition from a dynamic object. */
827 if (newdyn
828 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
829 && !bfd_is_und_section (sec))
830 {
831 *skip = TRUE;
832 /* Make sure this symbol is dynamic. */
833 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
834 /* A protected symbol has external availability. Make sure it is
835 recorded as dynamic.
836
837 FIXME: Should we check type and size for protected symbol? */
838 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
839 return _bfd_elf_link_record_dynamic_symbol (info, h);
840 else
841 return TRUE;
842 }
843 else if (!newdyn
844 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
845 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
846 {
847 /* If the new symbol with non-default visibility comes from a
848 relocatable file and the old definition comes from a dynamic
849 object, we remove the old definition. */
850 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
851 h = *sym_hash;
852
853 if ((h->root.und_next || info->hash->undefs_tail == &h->root)
854 && bfd_is_und_section (sec))
855 {
856 /* If the new symbol is undefined and the old symbol was
857 also undefined before, we need to make sure
858 _bfd_generic_link_add_one_symbol doesn't mess
859 up the linker hash table undefs list. Since the old
860 definition came from a dynamic object, it is still on the
861 undefs list. */
862 h->root.type = bfd_link_hash_undefined;
863 /* FIXME: What if the new symbol is weak undefined? */
864 h->root.u.undef.abfd = abfd;
865 }
866 else
867 {
868 h->root.type = bfd_link_hash_new;
869 h->root.u.undef.abfd = NULL;
870 }
871
872 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
873 {
874 h->elf_link_hash_flags &= ~ELF_LINK_HASH_DEF_DYNAMIC;
875 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_DYNAMIC
876 | ELF_LINK_DYNAMIC_DEF);
877 }
878 /* FIXME: Should we check type and size for protected symbol? */
879 h->size = 0;
880 h->type = 0;
881 return TRUE;
882 }
883
884 /* Differentiate strong and weak symbols. */
885 newweak = bind == STB_WEAK;
886 oldweak = (h->root.type == bfd_link_hash_defweak
887 || h->root.type == bfd_link_hash_undefweak);
888
889 /* If a new weak symbol comes from a regular file and the old symbol
890 comes from a dynamic library, we treat the new one as strong.
891 Similarly, an old weak symbol from a regular file is treated as
892 strong when the new symbol comes from a dynamic library. Further,
893 an old weak symbol from a dynamic library is treated as strong if
894 the new symbol is from a dynamic library. This reflects the way
895 glibc's ld.so works. */
896 if (!newdyn && olddyn)
897 newweak = FALSE;
898 if (newdyn)
899 oldweak = FALSE;
900
901 /* It's OK to change the type if either the existing symbol or the
902 new symbol is weak. A type change is also OK if the old symbol
903 is undefined and the new symbol is defined. */
904
905 if (oldweak
906 || newweak
907 || (newdef
908 && h->root.type == bfd_link_hash_undefined))
909 *type_change_ok = TRUE;
910
911 /* It's OK to change the size if either the existing symbol or the
912 new symbol is weak, or if the old symbol is undefined. */
913
914 if (*type_change_ok
915 || h->root.type == bfd_link_hash_undefined)
916 *size_change_ok = TRUE;
917
918 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
919 symbol, respectively, appears to be a common symbol in a dynamic
920 object. If a symbol appears in an uninitialized section, and is
921 not weak, and is not a function, then it may be a common symbol
922 which was resolved when the dynamic object was created. We want
923 to treat such symbols specially, because they raise special
924 considerations when setting the symbol size: if the symbol
925 appears as a common symbol in a regular object, and the size in
926 the regular object is larger, we must make sure that we use the
927 larger size. This problematic case can always be avoided in C,
928 but it must be handled correctly when using Fortran shared
929 libraries.
930
931 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
932 likewise for OLDDYNCOMMON and OLDDEF.
933
934 Note that this test is just a heuristic, and that it is quite
935 possible to have an uninitialized symbol in a shared object which
936 is really a definition, rather than a common symbol. This could
937 lead to some minor confusion when the symbol really is a common
938 symbol in some regular object. However, I think it will be
939 harmless. */
940
941 if (newdyn
942 && newdef
943 && !newweak
944 && (sec->flags & SEC_ALLOC) != 0
945 && (sec->flags & SEC_LOAD) == 0
946 && sym->st_size > 0
947 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
948 newdyncommon = TRUE;
949 else
950 newdyncommon = FALSE;
951
952 if (olddyn
953 && olddef
954 && h->root.type == bfd_link_hash_defined
955 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
956 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
957 && (h->root.u.def.section->flags & SEC_LOAD) == 0
958 && h->size > 0
959 && h->type != STT_FUNC)
960 olddyncommon = TRUE;
961 else
962 olddyncommon = FALSE;
963
964 /* If both the old and the new symbols look like common symbols in a
965 dynamic object, set the size of the symbol to the larger of the
966 two. */
967
968 if (olddyncommon
969 && newdyncommon
970 && sym->st_size != h->size)
971 {
972 /* Since we think we have two common symbols, issue a multiple
973 common warning if desired. Note that we only warn if the
974 size is different. If the size is the same, we simply let
975 the old symbol override the new one as normally happens with
976 symbols defined in dynamic objects. */
977
978 if (! ((*info->callbacks->multiple_common)
979 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
980 h->size, abfd, bfd_link_hash_common, sym->st_size)))
981 return FALSE;
982
983 if (sym->st_size > h->size)
984 h->size = sym->st_size;
985
986 *size_change_ok = TRUE;
987 }
988
989 /* If we are looking at a dynamic object, and we have found a
990 definition, we need to see if the symbol was already defined by
991 some other object. If so, we want to use the existing
992 definition, and we do not want to report a multiple symbol
993 definition error; we do this by clobbering *PSEC to be
994 bfd_und_section_ptr.
995
996 We treat a common symbol as a definition if the symbol in the
997 shared library is a function, since common symbols always
998 represent variables; this can cause confusion in principle, but
999 any such confusion would seem to indicate an erroneous program or
1000 shared library. We also permit a common symbol in a regular
1001 object to override a weak symbol in a shared object. */
1002
1003 if (newdyn
1004 && newdef
1005 && (olddef
1006 || (h->root.type == bfd_link_hash_common
1007 && (newweak
1008 || ELF_ST_TYPE (sym->st_info) == STT_FUNC))))
1009 {
1010 *override = TRUE;
1011 newdef = FALSE;
1012 newdyncommon = FALSE;
1013
1014 *psec = sec = bfd_und_section_ptr;
1015 *size_change_ok = TRUE;
1016
1017 /* If we get here when the old symbol is a common symbol, then
1018 we are explicitly letting it override a weak symbol or
1019 function in a dynamic object, and we don't want to warn about
1020 a type change. If the old symbol is a defined symbol, a type
1021 change warning may still be appropriate. */
1022
1023 if (h->root.type == bfd_link_hash_common)
1024 *type_change_ok = TRUE;
1025 }
1026
1027 /* Handle the special case of an old common symbol merging with a
1028 new symbol which looks like a common symbol in a shared object.
1029 We change *PSEC and *PVALUE to make the new symbol look like a
1030 common symbol, and let _bfd_generic_link_add_one_symbol will do
1031 the right thing. */
1032
1033 if (newdyncommon
1034 && h->root.type == bfd_link_hash_common)
1035 {
1036 *override = TRUE;
1037 newdef = FALSE;
1038 newdyncommon = FALSE;
1039 *pvalue = sym->st_size;
1040 *psec = sec = bfd_com_section_ptr;
1041 *size_change_ok = TRUE;
1042 }
1043
1044 /* If the old symbol is from a dynamic object, and the new symbol is
1045 a definition which is not from a dynamic object, then the new
1046 symbol overrides the old symbol. Symbols from regular files
1047 always take precedence over symbols from dynamic objects, even if
1048 they are defined after the dynamic object in the link.
1049
1050 As above, we again permit a common symbol in a regular object to
1051 override a definition in a shared object if the shared object
1052 symbol is a function or is weak. */
1053
1054 flip = NULL;
1055 if (! newdyn
1056 && (newdef
1057 || (bfd_is_com_section (sec)
1058 && (oldweak
1059 || h->type == STT_FUNC)))
1060 && olddyn
1061 && olddef
1062 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
1063 {
1064 /* Change the hash table entry to undefined, and let
1065 _bfd_generic_link_add_one_symbol do the right thing with the
1066 new definition. */
1067
1068 h->root.type = bfd_link_hash_undefined;
1069 h->root.u.undef.abfd = h->root.u.def.section->owner;
1070 *size_change_ok = TRUE;
1071
1072 olddef = FALSE;
1073 olddyncommon = FALSE;
1074
1075 /* We again permit a type change when a common symbol may be
1076 overriding a function. */
1077
1078 if (bfd_is_com_section (sec))
1079 *type_change_ok = TRUE;
1080
1081 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1082 flip = *sym_hash;
1083 else
1084 /* This union may have been set to be non-NULL when this symbol
1085 was seen in a dynamic object. We must force the union to be
1086 NULL, so that it is correct for a regular symbol. */
1087 h->verinfo.vertree = NULL;
1088 }
1089
1090 /* Handle the special case of a new common symbol merging with an
1091 old symbol that looks like it might be a common symbol defined in
1092 a shared object. Note that we have already handled the case in
1093 which a new common symbol should simply override the definition
1094 in the shared library. */
1095
1096 if (! newdyn
1097 && bfd_is_com_section (sec)
1098 && olddyncommon)
1099 {
1100 /* It would be best if we could set the hash table entry to a
1101 common symbol, but we don't know what to use for the section
1102 or the alignment. */
1103 if (! ((*info->callbacks->multiple_common)
1104 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1105 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1106 return FALSE;
1107
1108 /* If the presumed common symbol in the dynamic object is
1109 larger, pretend that the new symbol has its size. */
1110
1111 if (h->size > *pvalue)
1112 *pvalue = h->size;
1113
1114 /* FIXME: We no longer know the alignment required by the symbol
1115 in the dynamic object, so we just wind up using the one from
1116 the regular object. */
1117
1118 olddef = FALSE;
1119 olddyncommon = FALSE;
1120
1121 h->root.type = bfd_link_hash_undefined;
1122 h->root.u.undef.abfd = h->root.u.def.section->owner;
1123
1124 *size_change_ok = TRUE;
1125 *type_change_ok = TRUE;
1126
1127 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1128 flip = *sym_hash;
1129 else
1130 h->verinfo.vertree = NULL;
1131 }
1132
1133 if (flip != NULL)
1134 {
1135 /* Handle the case where we had a versioned symbol in a dynamic
1136 library and now find a definition in a normal object. In this
1137 case, we make the versioned symbol point to the normal one. */
1138 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1139 flip->root.type = h->root.type;
1140 h->root.type = bfd_link_hash_indirect;
1141 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1142 (*bed->elf_backend_copy_indirect_symbol) (bed, flip, h);
1143 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1144 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
1145 {
1146 h->elf_link_hash_flags &= ~ELF_LINK_HASH_DEF_DYNAMIC;
1147 flip->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1148 }
1149 }
1150
1151 return TRUE;
1152 }
1153
1154 /* This function is called to create an indirect symbol from the
1155 default for the symbol with the default version if needed. The
1156 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1157 set DYNSYM if the new indirect symbol is dynamic. */
1158
1159 bfd_boolean
1160 _bfd_elf_add_default_symbol (bfd *abfd,
1161 struct bfd_link_info *info,
1162 struct elf_link_hash_entry *h,
1163 const char *name,
1164 Elf_Internal_Sym *sym,
1165 asection **psec,
1166 bfd_vma *value,
1167 bfd_boolean *dynsym,
1168 bfd_boolean override)
1169 {
1170 bfd_boolean type_change_ok;
1171 bfd_boolean size_change_ok;
1172 bfd_boolean skip;
1173 char *shortname;
1174 struct elf_link_hash_entry *hi;
1175 struct bfd_link_hash_entry *bh;
1176 const struct elf_backend_data *bed;
1177 bfd_boolean collect;
1178 bfd_boolean dynamic;
1179 char *p;
1180 size_t len, shortlen;
1181 asection *sec;
1182
1183 /* If this symbol has a version, and it is the default version, we
1184 create an indirect symbol from the default name to the fully
1185 decorated name. This will cause external references which do not
1186 specify a version to be bound to this version of the symbol. */
1187 p = strchr (name, ELF_VER_CHR);
1188 if (p == NULL || p[1] != ELF_VER_CHR)
1189 return TRUE;
1190
1191 if (override)
1192 {
1193 /* We are overridden by an old definition. We need to check if we
1194 need to create the indirect symbol from the default name. */
1195 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1196 FALSE, FALSE);
1197 BFD_ASSERT (hi != NULL);
1198 if (hi == h)
1199 return TRUE;
1200 while (hi->root.type == bfd_link_hash_indirect
1201 || hi->root.type == bfd_link_hash_warning)
1202 {
1203 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1204 if (hi == h)
1205 return TRUE;
1206 }
1207 }
1208
1209 bed = get_elf_backend_data (abfd);
1210 collect = bed->collect;
1211 dynamic = (abfd->flags & DYNAMIC) != 0;
1212
1213 shortlen = p - name;
1214 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1215 if (shortname == NULL)
1216 return FALSE;
1217 memcpy (shortname, name, shortlen);
1218 shortname[shortlen] = '\0';
1219
1220 /* We are going to create a new symbol. Merge it with any existing
1221 symbol with this name. For the purposes of the merge, act as
1222 though we were defining the symbol we just defined, although we
1223 actually going to define an indirect symbol. */
1224 type_change_ok = FALSE;
1225 size_change_ok = FALSE;
1226 sec = *psec;
1227 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1228 &hi, &skip, &override, &type_change_ok,
1229 &size_change_ok))
1230 return FALSE;
1231
1232 if (skip)
1233 goto nondefault;
1234
1235 if (! override)
1236 {
1237 bh = &hi->root;
1238 if (! (_bfd_generic_link_add_one_symbol
1239 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1240 0, name, FALSE, collect, &bh)))
1241 return FALSE;
1242 hi = (struct elf_link_hash_entry *) bh;
1243 }
1244 else
1245 {
1246 /* In this case the symbol named SHORTNAME is overriding the
1247 indirect symbol we want to add. We were planning on making
1248 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1249 is the name without a version. NAME is the fully versioned
1250 name, and it is the default version.
1251
1252 Overriding means that we already saw a definition for the
1253 symbol SHORTNAME in a regular object, and it is overriding
1254 the symbol defined in the dynamic object.
1255
1256 When this happens, we actually want to change NAME, the
1257 symbol we just added, to refer to SHORTNAME. This will cause
1258 references to NAME in the shared object to become references
1259 to SHORTNAME in the regular object. This is what we expect
1260 when we override a function in a shared object: that the
1261 references in the shared object will be mapped to the
1262 definition in the regular object. */
1263
1264 while (hi->root.type == bfd_link_hash_indirect
1265 || hi->root.type == bfd_link_hash_warning)
1266 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1267
1268 h->root.type = bfd_link_hash_indirect;
1269 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1270 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
1271 {
1272 h->elf_link_hash_flags &=~ ELF_LINK_HASH_DEF_DYNAMIC;
1273 hi->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1274 if (hi->elf_link_hash_flags
1275 & (ELF_LINK_HASH_REF_REGULAR
1276 | ELF_LINK_HASH_DEF_REGULAR))
1277 {
1278 if (! _bfd_elf_link_record_dynamic_symbol (info, hi))
1279 return FALSE;
1280 }
1281 }
1282
1283 /* Now set HI to H, so that the following code will set the
1284 other fields correctly. */
1285 hi = h;
1286 }
1287
1288 /* If there is a duplicate definition somewhere, then HI may not
1289 point to an indirect symbol. We will have reported an error to
1290 the user in that case. */
1291
1292 if (hi->root.type == bfd_link_hash_indirect)
1293 {
1294 struct elf_link_hash_entry *ht;
1295
1296 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1297 (*bed->elf_backend_copy_indirect_symbol) (bed, ht, hi);
1298
1299 /* See if the new flags lead us to realize that the symbol must
1300 be dynamic. */
1301 if (! *dynsym)
1302 {
1303 if (! dynamic)
1304 {
1305 if (info->shared
1306 || ((hi->elf_link_hash_flags
1307 & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1308 *dynsym = TRUE;
1309 }
1310 else
1311 {
1312 if ((hi->elf_link_hash_flags
1313 & ELF_LINK_HASH_REF_REGULAR) != 0)
1314 *dynsym = TRUE;
1315 }
1316 }
1317 }
1318
1319 /* We also need to define an indirection from the nondefault version
1320 of the symbol. */
1321
1322 nondefault:
1323 len = strlen (name);
1324 shortname = bfd_hash_allocate (&info->hash->table, len);
1325 if (shortname == NULL)
1326 return FALSE;
1327 memcpy (shortname, name, shortlen);
1328 memcpy (shortname + shortlen, p + 1, len - shortlen);
1329
1330 /* Once again, merge with any existing symbol. */
1331 type_change_ok = FALSE;
1332 size_change_ok = FALSE;
1333 sec = *psec;
1334 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1335 &hi, &skip, &override, &type_change_ok,
1336 &size_change_ok))
1337 return FALSE;
1338
1339 if (skip)
1340 return TRUE;
1341
1342 if (override)
1343 {
1344 /* Here SHORTNAME is a versioned name, so we don't expect to see
1345 the type of override we do in the case above unless it is
1346 overridden by a versioned definition. */
1347 if (hi->root.type != bfd_link_hash_defined
1348 && hi->root.type != bfd_link_hash_defweak)
1349 (*_bfd_error_handler)
1350 (_("%s: warning: unexpected redefinition of indirect versioned symbol `%s'"),
1351 bfd_archive_filename (abfd), shortname);
1352 }
1353 else
1354 {
1355 bh = &hi->root;
1356 if (! (_bfd_generic_link_add_one_symbol
1357 (info, abfd, shortname, BSF_INDIRECT,
1358 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1359 return FALSE;
1360 hi = (struct elf_link_hash_entry *) bh;
1361
1362 /* If there is a duplicate definition somewhere, then HI may not
1363 point to an indirect symbol. We will have reported an error
1364 to the user in that case. */
1365
1366 if (hi->root.type == bfd_link_hash_indirect)
1367 {
1368 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
1369
1370 /* See if the new flags lead us to realize that the symbol
1371 must be dynamic. */
1372 if (! *dynsym)
1373 {
1374 if (! dynamic)
1375 {
1376 if (info->shared
1377 || ((hi->elf_link_hash_flags
1378 & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1379 *dynsym = TRUE;
1380 }
1381 else
1382 {
1383 if ((hi->elf_link_hash_flags
1384 & ELF_LINK_HASH_REF_REGULAR) != 0)
1385 *dynsym = TRUE;
1386 }
1387 }
1388 }
1389 }
1390
1391 return TRUE;
1392 }
1393 \f
1394 /* This routine is used to export all defined symbols into the dynamic
1395 symbol table. It is called via elf_link_hash_traverse. */
1396
1397 bfd_boolean
1398 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1399 {
1400 struct elf_info_failed *eif = data;
1401
1402 /* Ignore indirect symbols. These are added by the versioning code. */
1403 if (h->root.type == bfd_link_hash_indirect)
1404 return TRUE;
1405
1406 if (h->root.type == bfd_link_hash_warning)
1407 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1408
1409 if (h->dynindx == -1
1410 && (h->elf_link_hash_flags
1411 & (ELF_LINK_HASH_DEF_REGULAR | ELF_LINK_HASH_REF_REGULAR)) != 0)
1412 {
1413 struct bfd_elf_version_tree *t;
1414 struct bfd_elf_version_expr *d;
1415
1416 for (t = eif->verdefs; t != NULL; t = t->next)
1417 {
1418 if (t->globals.list != NULL)
1419 {
1420 d = (*t->match) (&t->globals, NULL, h->root.root.string);
1421 if (d != NULL)
1422 goto doit;
1423 }
1424
1425 if (t->locals.list != NULL)
1426 {
1427 d = (*t->match) (&t->locals, NULL, h->root.root.string);
1428 if (d != NULL)
1429 return TRUE;
1430 }
1431 }
1432
1433 if (!eif->verdefs)
1434 {
1435 doit:
1436 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
1437 {
1438 eif->failed = TRUE;
1439 return FALSE;
1440 }
1441 }
1442 }
1443
1444 return TRUE;
1445 }
1446 \f
1447 /* Look through the symbols which are defined in other shared
1448 libraries and referenced here. Update the list of version
1449 dependencies. This will be put into the .gnu.version_r section.
1450 This function is called via elf_link_hash_traverse. */
1451
1452 bfd_boolean
1453 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1454 void *data)
1455 {
1456 struct elf_find_verdep_info *rinfo = data;
1457 Elf_Internal_Verneed *t;
1458 Elf_Internal_Vernaux *a;
1459 bfd_size_type amt;
1460
1461 if (h->root.type == bfd_link_hash_warning)
1462 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1463
1464 /* We only care about symbols defined in shared objects with version
1465 information. */
1466 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
1467 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1468 || h->dynindx == -1
1469 || h->verinfo.verdef == NULL)
1470 return TRUE;
1471
1472 /* See if we already know about this version. */
1473 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1474 {
1475 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1476 continue;
1477
1478 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1479 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1480 return TRUE;
1481
1482 break;
1483 }
1484
1485 /* This is a new version. Add it to tree we are building. */
1486
1487 if (t == NULL)
1488 {
1489 amt = sizeof *t;
1490 t = bfd_zalloc (rinfo->output_bfd, amt);
1491 if (t == NULL)
1492 {
1493 rinfo->failed = TRUE;
1494 return FALSE;
1495 }
1496
1497 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1498 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1499 elf_tdata (rinfo->output_bfd)->verref = t;
1500 }
1501
1502 amt = sizeof *a;
1503 a = bfd_zalloc (rinfo->output_bfd, amt);
1504
1505 /* Note that we are copying a string pointer here, and testing it
1506 above. If bfd_elf_string_from_elf_section is ever changed to
1507 discard the string data when low in memory, this will have to be
1508 fixed. */
1509 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1510
1511 a->vna_flags = h->verinfo.verdef->vd_flags;
1512 a->vna_nextptr = t->vn_auxptr;
1513
1514 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1515 ++rinfo->vers;
1516
1517 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1518
1519 t->vn_auxptr = a;
1520
1521 return TRUE;
1522 }
1523
1524 /* Figure out appropriate versions for all the symbols. We may not
1525 have the version number script until we have read all of the input
1526 files, so until that point we don't know which symbols should be
1527 local. This function is called via elf_link_hash_traverse. */
1528
1529 bfd_boolean
1530 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1531 {
1532 struct elf_assign_sym_version_info *sinfo;
1533 struct bfd_link_info *info;
1534 const struct elf_backend_data *bed;
1535 struct elf_info_failed eif;
1536 char *p;
1537 bfd_size_type amt;
1538
1539 sinfo = data;
1540 info = sinfo->info;
1541
1542 if (h->root.type == bfd_link_hash_warning)
1543 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1544
1545 /* Fix the symbol flags. */
1546 eif.failed = FALSE;
1547 eif.info = info;
1548 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1549 {
1550 if (eif.failed)
1551 sinfo->failed = TRUE;
1552 return FALSE;
1553 }
1554
1555 /* We only need version numbers for symbols defined in regular
1556 objects. */
1557 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1558 return TRUE;
1559
1560 bed = get_elf_backend_data (sinfo->output_bfd);
1561 p = strchr (h->root.root.string, ELF_VER_CHR);
1562 if (p != NULL && h->verinfo.vertree == NULL)
1563 {
1564 struct bfd_elf_version_tree *t;
1565 bfd_boolean hidden;
1566
1567 hidden = TRUE;
1568
1569 /* There are two consecutive ELF_VER_CHR characters if this is
1570 not a hidden symbol. */
1571 ++p;
1572 if (*p == ELF_VER_CHR)
1573 {
1574 hidden = FALSE;
1575 ++p;
1576 }
1577
1578 /* If there is no version string, we can just return out. */
1579 if (*p == '\0')
1580 {
1581 if (hidden)
1582 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
1583 return TRUE;
1584 }
1585
1586 /* Look for the version. If we find it, it is no longer weak. */
1587 for (t = sinfo->verdefs; t != NULL; t = t->next)
1588 {
1589 if (strcmp (t->name, p) == 0)
1590 {
1591 size_t len;
1592 char *alc;
1593 struct bfd_elf_version_expr *d;
1594
1595 len = p - h->root.root.string;
1596 alc = bfd_malloc (len);
1597 if (alc == NULL)
1598 return FALSE;
1599 memcpy (alc, h->root.root.string, len - 1);
1600 alc[len - 1] = '\0';
1601 if (alc[len - 2] == ELF_VER_CHR)
1602 alc[len - 2] = '\0';
1603
1604 h->verinfo.vertree = t;
1605 t->used = TRUE;
1606 d = NULL;
1607
1608 if (t->globals.list != NULL)
1609 d = (*t->match) (&t->globals, NULL, alc);
1610
1611 /* See if there is anything to force this symbol to
1612 local scope. */
1613 if (d == NULL && t->locals.list != NULL)
1614 {
1615 d = (*t->match) (&t->locals, NULL, alc);
1616 if (d != NULL
1617 && h->dynindx != -1
1618 && info->shared
1619 && ! info->export_dynamic)
1620 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1621 }
1622
1623 free (alc);
1624 break;
1625 }
1626 }
1627
1628 /* If we are building an application, we need to create a
1629 version node for this version. */
1630 if (t == NULL && info->executable)
1631 {
1632 struct bfd_elf_version_tree **pp;
1633 int version_index;
1634
1635 /* If we aren't going to export this symbol, we don't need
1636 to worry about it. */
1637 if (h->dynindx == -1)
1638 return TRUE;
1639
1640 amt = sizeof *t;
1641 t = bfd_zalloc (sinfo->output_bfd, amt);
1642 if (t == NULL)
1643 {
1644 sinfo->failed = TRUE;
1645 return FALSE;
1646 }
1647
1648 t->name = p;
1649 t->name_indx = (unsigned int) -1;
1650 t->used = TRUE;
1651
1652 version_index = 1;
1653 /* Don't count anonymous version tag. */
1654 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1655 version_index = 0;
1656 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1657 ++version_index;
1658 t->vernum = version_index;
1659
1660 *pp = t;
1661
1662 h->verinfo.vertree = t;
1663 }
1664 else if (t == NULL)
1665 {
1666 /* We could not find the version for a symbol when
1667 generating a shared archive. Return an error. */
1668 (*_bfd_error_handler)
1669 (_("%s: undefined versioned symbol name %s"),
1670 bfd_get_filename (sinfo->output_bfd), h->root.root.string);
1671 bfd_set_error (bfd_error_bad_value);
1672 sinfo->failed = TRUE;
1673 return FALSE;
1674 }
1675
1676 if (hidden)
1677 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
1678 }
1679
1680 /* If we don't have a version for this symbol, see if we can find
1681 something. */
1682 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1683 {
1684 struct bfd_elf_version_tree *t;
1685 struct bfd_elf_version_tree *local_ver;
1686 struct bfd_elf_version_expr *d;
1687
1688 /* See if can find what version this symbol is in. If the
1689 symbol is supposed to be local, then don't actually register
1690 it. */
1691 local_ver = NULL;
1692 for (t = sinfo->verdefs; t != NULL; t = t->next)
1693 {
1694 if (t->globals.list != NULL)
1695 {
1696 bfd_boolean matched;
1697
1698 matched = FALSE;
1699 d = NULL;
1700 while ((d = (*t->match) (&t->globals, d,
1701 h->root.root.string)) != NULL)
1702 if (d->symver)
1703 matched = TRUE;
1704 else
1705 {
1706 /* There is a version without definition. Make
1707 the symbol the default definition for this
1708 version. */
1709 h->verinfo.vertree = t;
1710 local_ver = NULL;
1711 d->script = 1;
1712 break;
1713 }
1714 if (d != NULL)
1715 break;
1716 else if (matched)
1717 /* There is no undefined version for this symbol. Hide the
1718 default one. */
1719 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1720 }
1721
1722 if (t->locals.list != NULL)
1723 {
1724 d = NULL;
1725 while ((d = (*t->match) (&t->locals, d,
1726 h->root.root.string)) != NULL)
1727 {
1728 local_ver = t;
1729 /* If the match is "*", keep looking for a more
1730 explicit, perhaps even global, match.
1731 XXX: Shouldn't this be !d->wildcard instead? */
1732 if (d->pattern[0] != '*' || d->pattern[1] != '\0')
1733 break;
1734 }
1735
1736 if (d != NULL)
1737 break;
1738 }
1739 }
1740
1741 if (local_ver != NULL)
1742 {
1743 h->verinfo.vertree = local_ver;
1744 if (h->dynindx != -1
1745 && info->shared
1746 && ! info->export_dynamic)
1747 {
1748 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1749 }
1750 }
1751 }
1752
1753 return TRUE;
1754 }
1755 \f
1756 /* Read and swap the relocs from the section indicated by SHDR. This
1757 may be either a REL or a RELA section. The relocations are
1758 translated into RELA relocations and stored in INTERNAL_RELOCS,
1759 which should have already been allocated to contain enough space.
1760 The EXTERNAL_RELOCS are a buffer where the external form of the
1761 relocations should be stored.
1762
1763 Returns FALSE if something goes wrong. */
1764
1765 static bfd_boolean
1766 elf_link_read_relocs_from_section (bfd *abfd,
1767 asection *sec,
1768 Elf_Internal_Shdr *shdr,
1769 void *external_relocs,
1770 Elf_Internal_Rela *internal_relocs)
1771 {
1772 const struct elf_backend_data *bed;
1773 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
1774 const bfd_byte *erela;
1775 const bfd_byte *erelaend;
1776 Elf_Internal_Rela *irela;
1777 Elf_Internal_Shdr *symtab_hdr;
1778 size_t nsyms;
1779
1780 /* Position ourselves at the start of the section. */
1781 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
1782 return FALSE;
1783
1784 /* Read the relocations. */
1785 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
1786 return FALSE;
1787
1788 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1789 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
1790
1791 bed = get_elf_backend_data (abfd);
1792
1793 /* Convert the external relocations to the internal format. */
1794 if (shdr->sh_entsize == bed->s->sizeof_rel)
1795 swap_in = bed->s->swap_reloc_in;
1796 else if (shdr->sh_entsize == bed->s->sizeof_rela)
1797 swap_in = bed->s->swap_reloca_in;
1798 else
1799 {
1800 bfd_set_error (bfd_error_wrong_format);
1801 return FALSE;
1802 }
1803
1804 erela = external_relocs;
1805 erelaend = erela + shdr->sh_size;
1806 irela = internal_relocs;
1807 while (erela < erelaend)
1808 {
1809 bfd_vma r_symndx;
1810
1811 (*swap_in) (abfd, erela, irela);
1812 r_symndx = ELF32_R_SYM (irela->r_info);
1813 if (bed->s->arch_size == 64)
1814 r_symndx >>= 24;
1815 if ((size_t) r_symndx >= nsyms)
1816 {
1817 (*_bfd_error_handler)
1818 (_("%s: bad reloc symbol index (0x%lx >= 0x%lx) for offset 0x%lx in section `%s'"),
1819 bfd_archive_filename (abfd), (unsigned long) r_symndx,
1820 (unsigned long) nsyms, irela->r_offset, sec->name);
1821 bfd_set_error (bfd_error_bad_value);
1822 return FALSE;
1823 }
1824 irela += bed->s->int_rels_per_ext_rel;
1825 erela += shdr->sh_entsize;
1826 }
1827
1828 return TRUE;
1829 }
1830
1831 /* Read and swap the relocs for a section O. They may have been
1832 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1833 not NULL, they are used as buffers to read into. They are known to
1834 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1835 the return value is allocated using either malloc or bfd_alloc,
1836 according to the KEEP_MEMORY argument. If O has two relocation
1837 sections (both REL and RELA relocations), then the REL_HDR
1838 relocations will appear first in INTERNAL_RELOCS, followed by the
1839 REL_HDR2 relocations. */
1840
1841 Elf_Internal_Rela *
1842 _bfd_elf_link_read_relocs (bfd *abfd,
1843 asection *o,
1844 void *external_relocs,
1845 Elf_Internal_Rela *internal_relocs,
1846 bfd_boolean keep_memory)
1847 {
1848 Elf_Internal_Shdr *rel_hdr;
1849 void *alloc1 = NULL;
1850 Elf_Internal_Rela *alloc2 = NULL;
1851 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1852
1853 if (elf_section_data (o)->relocs != NULL)
1854 return elf_section_data (o)->relocs;
1855
1856 if (o->reloc_count == 0)
1857 return NULL;
1858
1859 rel_hdr = &elf_section_data (o)->rel_hdr;
1860
1861 if (internal_relocs == NULL)
1862 {
1863 bfd_size_type size;
1864
1865 size = o->reloc_count;
1866 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
1867 if (keep_memory)
1868 internal_relocs = bfd_alloc (abfd, size);
1869 else
1870 internal_relocs = alloc2 = bfd_malloc (size);
1871 if (internal_relocs == NULL)
1872 goto error_return;
1873 }
1874
1875 if (external_relocs == NULL)
1876 {
1877 bfd_size_type size = rel_hdr->sh_size;
1878
1879 if (elf_section_data (o)->rel_hdr2)
1880 size += elf_section_data (o)->rel_hdr2->sh_size;
1881 alloc1 = bfd_malloc (size);
1882 if (alloc1 == NULL)
1883 goto error_return;
1884 external_relocs = alloc1;
1885 }
1886
1887 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
1888 external_relocs,
1889 internal_relocs))
1890 goto error_return;
1891 if (elf_section_data (o)->rel_hdr2
1892 && (!elf_link_read_relocs_from_section
1893 (abfd, o,
1894 elf_section_data (o)->rel_hdr2,
1895 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
1896 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
1897 * bed->s->int_rels_per_ext_rel))))
1898 goto error_return;
1899
1900 /* Cache the results for next time, if we can. */
1901 if (keep_memory)
1902 elf_section_data (o)->relocs = internal_relocs;
1903
1904 if (alloc1 != NULL)
1905 free (alloc1);
1906
1907 /* Don't free alloc2, since if it was allocated we are passing it
1908 back (under the name of internal_relocs). */
1909
1910 return internal_relocs;
1911
1912 error_return:
1913 if (alloc1 != NULL)
1914 free (alloc1);
1915 if (alloc2 != NULL)
1916 free (alloc2);
1917 return NULL;
1918 }
1919
1920 /* Compute the size of, and allocate space for, REL_HDR which is the
1921 section header for a section containing relocations for O. */
1922
1923 bfd_boolean
1924 _bfd_elf_link_size_reloc_section (bfd *abfd,
1925 Elf_Internal_Shdr *rel_hdr,
1926 asection *o)
1927 {
1928 bfd_size_type reloc_count;
1929 bfd_size_type num_rel_hashes;
1930
1931 /* Figure out how many relocations there will be. */
1932 if (rel_hdr == &elf_section_data (o)->rel_hdr)
1933 reloc_count = elf_section_data (o)->rel_count;
1934 else
1935 reloc_count = elf_section_data (o)->rel_count2;
1936
1937 num_rel_hashes = o->reloc_count;
1938 if (num_rel_hashes < reloc_count)
1939 num_rel_hashes = reloc_count;
1940
1941 /* That allows us to calculate the size of the section. */
1942 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
1943
1944 /* The contents field must last into write_object_contents, so we
1945 allocate it with bfd_alloc rather than malloc. Also since we
1946 cannot be sure that the contents will actually be filled in,
1947 we zero the allocated space. */
1948 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
1949 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
1950 return FALSE;
1951
1952 /* We only allocate one set of hash entries, so we only do it the
1953 first time we are called. */
1954 if (elf_section_data (o)->rel_hashes == NULL
1955 && num_rel_hashes)
1956 {
1957 struct elf_link_hash_entry **p;
1958
1959 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
1960 if (p == NULL)
1961 return FALSE;
1962
1963 elf_section_data (o)->rel_hashes = p;
1964 }
1965
1966 return TRUE;
1967 }
1968
1969 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
1970 originated from the section given by INPUT_REL_HDR) to the
1971 OUTPUT_BFD. */
1972
1973 bfd_boolean
1974 _bfd_elf_link_output_relocs (bfd *output_bfd,
1975 asection *input_section,
1976 Elf_Internal_Shdr *input_rel_hdr,
1977 Elf_Internal_Rela *internal_relocs)
1978 {
1979 Elf_Internal_Rela *irela;
1980 Elf_Internal_Rela *irelaend;
1981 bfd_byte *erel;
1982 Elf_Internal_Shdr *output_rel_hdr;
1983 asection *output_section;
1984 unsigned int *rel_countp = NULL;
1985 const struct elf_backend_data *bed;
1986 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
1987
1988 output_section = input_section->output_section;
1989 output_rel_hdr = NULL;
1990
1991 if (elf_section_data (output_section)->rel_hdr.sh_entsize
1992 == input_rel_hdr->sh_entsize)
1993 {
1994 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
1995 rel_countp = &elf_section_data (output_section)->rel_count;
1996 }
1997 else if (elf_section_data (output_section)->rel_hdr2
1998 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
1999 == input_rel_hdr->sh_entsize))
2000 {
2001 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2002 rel_countp = &elf_section_data (output_section)->rel_count2;
2003 }
2004 else
2005 {
2006 (*_bfd_error_handler)
2007 (_("%s: relocation size mismatch in %s section %s"),
2008 bfd_get_filename (output_bfd),
2009 bfd_archive_filename (input_section->owner),
2010 input_section->name);
2011 bfd_set_error (bfd_error_wrong_object_format);
2012 return FALSE;
2013 }
2014
2015 bed = get_elf_backend_data (output_bfd);
2016 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2017 swap_out = bed->s->swap_reloc_out;
2018 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2019 swap_out = bed->s->swap_reloca_out;
2020 else
2021 abort ();
2022
2023 erel = output_rel_hdr->contents;
2024 erel += *rel_countp * input_rel_hdr->sh_entsize;
2025 irela = internal_relocs;
2026 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2027 * bed->s->int_rels_per_ext_rel);
2028 while (irela < irelaend)
2029 {
2030 (*swap_out) (output_bfd, irela, erel);
2031 irela += bed->s->int_rels_per_ext_rel;
2032 erel += input_rel_hdr->sh_entsize;
2033 }
2034
2035 /* Bump the counter, so that we know where to add the next set of
2036 relocations. */
2037 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2038
2039 return TRUE;
2040 }
2041 \f
2042 /* Fix up the flags for a symbol. This handles various cases which
2043 can only be fixed after all the input files are seen. This is
2044 currently called by both adjust_dynamic_symbol and
2045 assign_sym_version, which is unnecessary but perhaps more robust in
2046 the face of future changes. */
2047
2048 bfd_boolean
2049 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2050 struct elf_info_failed *eif)
2051 {
2052 /* If this symbol was mentioned in a non-ELF file, try to set
2053 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2054 permit a non-ELF file to correctly refer to a symbol defined in
2055 an ELF dynamic object. */
2056 if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) != 0)
2057 {
2058 while (h->root.type == bfd_link_hash_indirect)
2059 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2060
2061 if (h->root.type != bfd_link_hash_defined
2062 && h->root.type != bfd_link_hash_defweak)
2063 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
2064 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
2065 else
2066 {
2067 if (h->root.u.def.section->owner != NULL
2068 && (bfd_get_flavour (h->root.u.def.section->owner)
2069 == bfd_target_elf_flavour))
2070 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
2071 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
2072 else
2073 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2074 }
2075
2076 if (h->dynindx == -1
2077 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2078 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0))
2079 {
2080 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
2081 {
2082 eif->failed = TRUE;
2083 return FALSE;
2084 }
2085 }
2086 }
2087 else
2088 {
2089 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
2090 was first seen in a non-ELF file. Fortunately, if the symbol
2091 was first seen in an ELF file, we're probably OK unless the
2092 symbol was defined in a non-ELF file. Catch that case here.
2093 FIXME: We're still in trouble if the symbol was first seen in
2094 a dynamic object, and then later in a non-ELF regular object. */
2095 if ((h->root.type == bfd_link_hash_defined
2096 || h->root.type == bfd_link_hash_defweak)
2097 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
2098 && (h->root.u.def.section->owner != NULL
2099 ? (bfd_get_flavour (h->root.u.def.section->owner)
2100 != bfd_target_elf_flavour)
2101 : (bfd_is_abs_section (h->root.u.def.section)
2102 && (h->elf_link_hash_flags
2103 & ELF_LINK_HASH_DEF_DYNAMIC) == 0)))
2104 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2105 }
2106
2107 /* If this is a final link, and the symbol was defined as a common
2108 symbol in a regular object file, and there was no definition in
2109 any dynamic object, then the linker will have allocated space for
2110 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
2111 flag will not have been set. */
2112 if (h->root.type == bfd_link_hash_defined
2113 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
2114 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
2115 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
2116 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2117 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2118
2119 /* If -Bsymbolic was used (which means to bind references to global
2120 symbols to the definition within the shared object), and this
2121 symbol was defined in a regular object, then it actually doesn't
2122 need a PLT entry. Likewise, if the symbol has non-default
2123 visibility. If the symbol has hidden or internal visibility, we
2124 will force it local. */
2125 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
2126 && eif->info->shared
2127 && is_elf_hash_table (eif->info->hash)
2128 && (eif->info->symbolic
2129 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2130 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
2131 {
2132 const struct elf_backend_data *bed;
2133 bfd_boolean force_local;
2134
2135 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2136
2137 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2138 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2139 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2140 }
2141
2142 /* If a weak undefined symbol has non-default visibility, we also
2143 hide it from the dynamic linker. */
2144 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2145 && h->root.type == bfd_link_hash_undefweak)
2146 {
2147 const struct elf_backend_data *bed;
2148 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2149 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2150 }
2151
2152 /* If this is a weak defined symbol in a dynamic object, and we know
2153 the real definition in the dynamic object, copy interesting flags
2154 over to the real definition. */
2155 if (h->weakdef != NULL)
2156 {
2157 struct elf_link_hash_entry *weakdef;
2158
2159 weakdef = h->weakdef;
2160 if (h->root.type == bfd_link_hash_indirect)
2161 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2162
2163 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2164 || h->root.type == bfd_link_hash_defweak);
2165 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2166 || weakdef->root.type == bfd_link_hash_defweak);
2167 BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC);
2168
2169 /* If the real definition is defined by a regular object file,
2170 don't do anything special. See the longer description in
2171 _bfd_elf_adjust_dynamic_symbol, below. */
2172 if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
2173 h->weakdef = NULL;
2174 else
2175 {
2176 const struct elf_backend_data *bed;
2177
2178 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2179 (*bed->elf_backend_copy_indirect_symbol) (bed, weakdef, h);
2180 }
2181 }
2182
2183 return TRUE;
2184 }
2185
2186 /* Make the backend pick a good value for a dynamic symbol. This is
2187 called via elf_link_hash_traverse, and also calls itself
2188 recursively. */
2189
2190 bfd_boolean
2191 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2192 {
2193 struct elf_info_failed *eif = data;
2194 bfd *dynobj;
2195 const struct elf_backend_data *bed;
2196
2197 if (! is_elf_hash_table (eif->info->hash))
2198 return FALSE;
2199
2200 if (h->root.type == bfd_link_hash_warning)
2201 {
2202 h->plt = elf_hash_table (eif->info)->init_offset;
2203 h->got = elf_hash_table (eif->info)->init_offset;
2204
2205 /* When warning symbols are created, they **replace** the "real"
2206 entry in the hash table, thus we never get to see the real
2207 symbol in a hash traversal. So look at it now. */
2208 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2209 }
2210
2211 /* Ignore indirect symbols. These are added by the versioning code. */
2212 if (h->root.type == bfd_link_hash_indirect)
2213 return TRUE;
2214
2215 /* Fix the symbol flags. */
2216 if (! _bfd_elf_fix_symbol_flags (h, eif))
2217 return FALSE;
2218
2219 /* If this symbol does not require a PLT entry, and it is not
2220 defined by a dynamic object, or is not referenced by a regular
2221 object, ignore it. We do have to handle a weak defined symbol,
2222 even if no regular object refers to it, if we decided to add it
2223 to the dynamic symbol table. FIXME: Do we normally need to worry
2224 about symbols which are defined by one dynamic object and
2225 referenced by another one? */
2226 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0
2227 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
2228 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
2229 || ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
2230 && (h->weakdef == NULL || h->weakdef->dynindx == -1))))
2231 {
2232 h->plt = elf_hash_table (eif->info)->init_offset;
2233 return TRUE;
2234 }
2235
2236 /* If we've already adjusted this symbol, don't do it again. This
2237 can happen via a recursive call. */
2238 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
2239 return TRUE;
2240
2241 /* Don't look at this symbol again. Note that we must set this
2242 after checking the above conditions, because we may look at a
2243 symbol once, decide not to do anything, and then get called
2244 recursively later after REF_REGULAR is set below. */
2245 h->elf_link_hash_flags |= ELF_LINK_HASH_DYNAMIC_ADJUSTED;
2246
2247 /* If this is a weak definition, and we know a real definition, and
2248 the real symbol is not itself defined by a regular object file,
2249 then get a good value for the real definition. We handle the
2250 real symbol first, for the convenience of the backend routine.
2251
2252 Note that there is a confusing case here. If the real definition
2253 is defined by a regular object file, we don't get the real symbol
2254 from the dynamic object, but we do get the weak symbol. If the
2255 processor backend uses a COPY reloc, then if some routine in the
2256 dynamic object changes the real symbol, we will not see that
2257 change in the corresponding weak symbol. This is the way other
2258 ELF linkers work as well, and seems to be a result of the shared
2259 library model.
2260
2261 I will clarify this issue. Most SVR4 shared libraries define the
2262 variable _timezone and define timezone as a weak synonym. The
2263 tzset call changes _timezone. If you write
2264 extern int timezone;
2265 int _timezone = 5;
2266 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2267 you might expect that, since timezone is a synonym for _timezone,
2268 the same number will print both times. However, if the processor
2269 backend uses a COPY reloc, then actually timezone will be copied
2270 into your process image, and, since you define _timezone
2271 yourself, _timezone will not. Thus timezone and _timezone will
2272 wind up at different memory locations. The tzset call will set
2273 _timezone, leaving timezone unchanged. */
2274
2275 if (h->weakdef != NULL)
2276 {
2277 /* If we get to this point, we know there is an implicit
2278 reference by a regular object file via the weak symbol H.
2279 FIXME: Is this really true? What if the traversal finds
2280 H->WEAKDEF before it finds H? */
2281 h->weakdef->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
2282
2283 if (! _bfd_elf_adjust_dynamic_symbol (h->weakdef, eif))
2284 return FALSE;
2285 }
2286
2287 /* If a symbol has no type and no size and does not require a PLT
2288 entry, then we are probably about to do the wrong thing here: we
2289 are probably going to create a COPY reloc for an empty object.
2290 This case can arise when a shared object is built with assembly
2291 code, and the assembly code fails to set the symbol type. */
2292 if (h->size == 0
2293 && h->type == STT_NOTYPE
2294 && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0)
2295 (*_bfd_error_handler)
2296 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2297 h->root.root.string);
2298
2299 dynobj = elf_hash_table (eif->info)->dynobj;
2300 bed = get_elf_backend_data (dynobj);
2301 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2302 {
2303 eif->failed = TRUE;
2304 return FALSE;
2305 }
2306
2307 return TRUE;
2308 }
2309
2310 /* Adjust all external symbols pointing into SEC_MERGE sections
2311 to reflect the object merging within the sections. */
2312
2313 bfd_boolean
2314 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2315 {
2316 asection *sec;
2317
2318 if (h->root.type == bfd_link_hash_warning)
2319 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2320
2321 if ((h->root.type == bfd_link_hash_defined
2322 || h->root.type == bfd_link_hash_defweak)
2323 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2324 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2325 {
2326 bfd *output_bfd = data;
2327
2328 h->root.u.def.value =
2329 _bfd_merged_section_offset (output_bfd,
2330 &h->root.u.def.section,
2331 elf_section_data (sec)->sec_info,
2332 h->root.u.def.value, 0);
2333 }
2334
2335 return TRUE;
2336 }
2337
2338 /* Returns false if the symbol referred to by H should be considered
2339 to resolve local to the current module, and true if it should be
2340 considered to bind dynamically. */
2341
2342 bfd_boolean
2343 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2344 struct bfd_link_info *info,
2345 bfd_boolean ignore_protected)
2346 {
2347 bfd_boolean binding_stays_local_p;
2348
2349 if (h == NULL)
2350 return FALSE;
2351
2352 while (h->root.type == bfd_link_hash_indirect
2353 || h->root.type == bfd_link_hash_warning)
2354 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2355
2356 /* If it was forced local, then clearly it's not dynamic. */
2357 if (h->dynindx == -1)
2358 return FALSE;
2359 if (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
2360 return FALSE;
2361
2362 /* Identify the cases where name binding rules say that a
2363 visible symbol resolves locally. */
2364 binding_stays_local_p = info->executable || info->symbolic;
2365
2366 switch (ELF_ST_VISIBILITY (h->other))
2367 {
2368 case STV_INTERNAL:
2369 case STV_HIDDEN:
2370 return FALSE;
2371
2372 case STV_PROTECTED:
2373 /* Proper resolution for function pointer equality may require
2374 that these symbols perhaps be resolved dynamically, even though
2375 we should be resolving them to the current module. */
2376 if (!ignore_protected)
2377 binding_stays_local_p = TRUE;
2378 break;
2379
2380 default:
2381 break;
2382 }
2383
2384 /* If it isn't defined locally, then clearly it's dynamic. */
2385 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2386 return TRUE;
2387
2388 /* Otherwise, the symbol is dynamic if binding rules don't tell
2389 us that it remains local. */
2390 return !binding_stays_local_p;
2391 }
2392
2393 /* Return true if the symbol referred to by H should be considered
2394 to resolve local to the current module, and false otherwise. Differs
2395 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2396 undefined symbols and weak symbols. */
2397
2398 bfd_boolean
2399 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2400 struct bfd_link_info *info,
2401 bfd_boolean local_protected)
2402 {
2403 /* If it's a local sym, of course we resolve locally. */
2404 if (h == NULL)
2405 return TRUE;
2406
2407 /* If we don't have a definition in a regular file, then we can't
2408 resolve locally. The sym is either undefined or dynamic. */
2409 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2410 return FALSE;
2411
2412 /* Forced local symbols resolve locally. */
2413 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
2414 return TRUE;
2415
2416 /* As do non-dynamic symbols. */
2417 if (h->dynindx == -1)
2418 return TRUE;
2419
2420 /* At this point, we know the symbol is defined and dynamic. In an
2421 executable it must resolve locally, likewise when building symbolic
2422 shared libraries. */
2423 if (info->executable || info->symbolic)
2424 return TRUE;
2425
2426 /* Now deal with defined dynamic symbols in shared libraries. Ones
2427 with default visibility might not resolve locally. */
2428 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2429 return FALSE;
2430
2431 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2432 if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED)
2433 return TRUE;
2434
2435 /* Function pointer equality tests may require that STV_PROTECTED
2436 symbols be treated as dynamic symbols, even when we know that the
2437 dynamic linker will resolve them locally. */
2438 return local_protected;
2439 }
2440
2441 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2442 aligned. Returns the first TLS output section. */
2443
2444 struct bfd_section *
2445 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2446 {
2447 struct bfd_section *sec, *tls;
2448 unsigned int align = 0;
2449
2450 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2451 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2452 break;
2453 tls = sec;
2454
2455 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2456 if (sec->alignment_power > align)
2457 align = sec->alignment_power;
2458
2459 elf_hash_table (info)->tls_sec = tls;
2460
2461 /* Ensure the alignment of the first section is the largest alignment,
2462 so that the tls segment starts aligned. */
2463 if (tls != NULL)
2464 tls->alignment_power = align;
2465
2466 return tls;
2467 }
2468
2469 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2470 static bfd_boolean
2471 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2472 Elf_Internal_Sym *sym)
2473 {
2474 /* Local symbols do not count, but target specific ones might. */
2475 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2476 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2477 return FALSE;
2478
2479 /* Function symbols do not count. */
2480 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
2481 return FALSE;
2482
2483 /* If the section is undefined, then so is the symbol. */
2484 if (sym->st_shndx == SHN_UNDEF)
2485 return FALSE;
2486
2487 /* If the symbol is defined in the common section, then
2488 it is a common definition and so does not count. */
2489 if (sym->st_shndx == SHN_COMMON)
2490 return FALSE;
2491
2492 /* If the symbol is in a target specific section then we
2493 must rely upon the backend to tell us what it is. */
2494 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2495 /* FIXME - this function is not coded yet:
2496
2497 return _bfd_is_global_symbol_definition (abfd, sym);
2498
2499 Instead for now assume that the definition is not global,
2500 Even if this is wrong, at least the linker will behave
2501 in the same way that it used to do. */
2502 return FALSE;
2503
2504 return TRUE;
2505 }
2506
2507 /* Search the symbol table of the archive element of the archive ABFD
2508 whose archive map contains a mention of SYMDEF, and determine if
2509 the symbol is defined in this element. */
2510 static bfd_boolean
2511 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2512 {
2513 Elf_Internal_Shdr * hdr;
2514 bfd_size_type symcount;
2515 bfd_size_type extsymcount;
2516 bfd_size_type extsymoff;
2517 Elf_Internal_Sym *isymbuf;
2518 Elf_Internal_Sym *isym;
2519 Elf_Internal_Sym *isymend;
2520 bfd_boolean result;
2521
2522 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2523 if (abfd == NULL)
2524 return FALSE;
2525
2526 if (! bfd_check_format (abfd, bfd_object))
2527 return FALSE;
2528
2529 /* If we have already included the element containing this symbol in the
2530 link then we do not need to include it again. Just claim that any symbol
2531 it contains is not a definition, so that our caller will not decide to
2532 (re)include this element. */
2533 if (abfd->archive_pass)
2534 return FALSE;
2535
2536 /* Select the appropriate symbol table. */
2537 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2538 hdr = &elf_tdata (abfd)->symtab_hdr;
2539 else
2540 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2541
2542 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2543
2544 /* The sh_info field of the symtab header tells us where the
2545 external symbols start. We don't care about the local symbols. */
2546 if (elf_bad_symtab (abfd))
2547 {
2548 extsymcount = symcount;
2549 extsymoff = 0;
2550 }
2551 else
2552 {
2553 extsymcount = symcount - hdr->sh_info;
2554 extsymoff = hdr->sh_info;
2555 }
2556
2557 if (extsymcount == 0)
2558 return FALSE;
2559
2560 /* Read in the symbol table. */
2561 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2562 NULL, NULL, NULL);
2563 if (isymbuf == NULL)
2564 return FALSE;
2565
2566 /* Scan the symbol table looking for SYMDEF. */
2567 result = FALSE;
2568 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2569 {
2570 const char *name;
2571
2572 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
2573 isym->st_name);
2574 if (name == NULL)
2575 break;
2576
2577 if (strcmp (name, symdef->name) == 0)
2578 {
2579 result = is_global_data_symbol_definition (abfd, isym);
2580 break;
2581 }
2582 }
2583
2584 free (isymbuf);
2585
2586 return result;
2587 }
2588 \f
2589 /* Add an entry to the .dynamic table. */
2590
2591 bfd_boolean
2592 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
2593 bfd_vma tag,
2594 bfd_vma val)
2595 {
2596 struct elf_link_hash_table *hash_table;
2597 const struct elf_backend_data *bed;
2598 asection *s;
2599 bfd_size_type newsize;
2600 bfd_byte *newcontents;
2601 Elf_Internal_Dyn dyn;
2602
2603 hash_table = elf_hash_table (info);
2604 if (! is_elf_hash_table (hash_table))
2605 return FALSE;
2606
2607 bed = get_elf_backend_data (hash_table->dynobj);
2608 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2609 BFD_ASSERT (s != NULL);
2610
2611 newsize = s->_raw_size + bed->s->sizeof_dyn;
2612 newcontents = bfd_realloc (s->contents, newsize);
2613 if (newcontents == NULL)
2614 return FALSE;
2615
2616 dyn.d_tag = tag;
2617 dyn.d_un.d_val = val;
2618 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->_raw_size);
2619
2620 s->_raw_size = newsize;
2621 s->contents = newcontents;
2622
2623 return TRUE;
2624 }
2625
2626 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2627 otherwise just check whether one already exists. Returns -1 on error,
2628 1 if a DT_NEEDED tag already exists, and 0 on success. */
2629
2630 static int
2631 elf_add_dt_needed_tag (struct bfd_link_info *info,
2632 const char *soname,
2633 bfd_boolean do_it)
2634 {
2635 struct elf_link_hash_table *hash_table;
2636 bfd_size_type oldsize;
2637 bfd_size_type strindex;
2638
2639 hash_table = elf_hash_table (info);
2640 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2641 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
2642 if (strindex == (bfd_size_type) -1)
2643 return -1;
2644
2645 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
2646 {
2647 asection *sdyn;
2648 const struct elf_backend_data *bed;
2649 bfd_byte *extdyn;
2650
2651 bed = get_elf_backend_data (hash_table->dynobj);
2652 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2653 BFD_ASSERT (sdyn != NULL);
2654
2655 for (extdyn = sdyn->contents;
2656 extdyn < sdyn->contents + sdyn->_raw_size;
2657 extdyn += bed->s->sizeof_dyn)
2658 {
2659 Elf_Internal_Dyn dyn;
2660
2661 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
2662 if (dyn.d_tag == DT_NEEDED
2663 && dyn.d_un.d_val == strindex)
2664 {
2665 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2666 return 1;
2667 }
2668 }
2669 }
2670
2671 if (do_it)
2672 {
2673 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
2674 return -1;
2675 }
2676 else
2677 /* We were just checking for existence of the tag. */
2678 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2679
2680 return 0;
2681 }
2682
2683 /* Sort symbol by value and section. */
2684 static int
2685 elf_sort_symbol (const void *arg1, const void *arg2)
2686 {
2687 const struct elf_link_hash_entry *h1;
2688 const struct elf_link_hash_entry *h2;
2689 bfd_signed_vma vdiff;
2690
2691 h1 = *(const struct elf_link_hash_entry **) arg1;
2692 h2 = *(const struct elf_link_hash_entry **) arg2;
2693 vdiff = h1->root.u.def.value - h2->root.u.def.value;
2694 if (vdiff != 0)
2695 return vdiff > 0 ? 1 : -1;
2696 else
2697 {
2698 long sdiff = h1->root.u.def.section - h2->root.u.def.section;
2699 if (sdiff != 0)
2700 return sdiff > 0 ? 1 : -1;
2701 }
2702 return 0;
2703 }
2704
2705 /* This function is used to adjust offsets into .dynstr for
2706 dynamic symbols. This is called via elf_link_hash_traverse. */
2707
2708 static bfd_boolean
2709 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
2710 {
2711 struct elf_strtab_hash *dynstr = data;
2712
2713 if (h->root.type == bfd_link_hash_warning)
2714 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2715
2716 if (h->dynindx != -1)
2717 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
2718 return TRUE;
2719 }
2720
2721 /* Assign string offsets in .dynstr, update all structures referencing
2722 them. */
2723
2724 static bfd_boolean
2725 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
2726 {
2727 struct elf_link_hash_table *hash_table = elf_hash_table (info);
2728 struct elf_link_local_dynamic_entry *entry;
2729 struct elf_strtab_hash *dynstr = hash_table->dynstr;
2730 bfd *dynobj = hash_table->dynobj;
2731 asection *sdyn;
2732 bfd_size_type size;
2733 const struct elf_backend_data *bed;
2734 bfd_byte *extdyn;
2735
2736 _bfd_elf_strtab_finalize (dynstr);
2737 size = _bfd_elf_strtab_size (dynstr);
2738
2739 bed = get_elf_backend_data (dynobj);
2740 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2741 BFD_ASSERT (sdyn != NULL);
2742
2743 /* Update all .dynamic entries referencing .dynstr strings. */
2744 for (extdyn = sdyn->contents;
2745 extdyn < sdyn->contents + sdyn->_raw_size;
2746 extdyn += bed->s->sizeof_dyn)
2747 {
2748 Elf_Internal_Dyn dyn;
2749
2750 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
2751 switch (dyn.d_tag)
2752 {
2753 case DT_STRSZ:
2754 dyn.d_un.d_val = size;
2755 break;
2756 case DT_NEEDED:
2757 case DT_SONAME:
2758 case DT_RPATH:
2759 case DT_RUNPATH:
2760 case DT_FILTER:
2761 case DT_AUXILIARY:
2762 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
2763 break;
2764 default:
2765 continue;
2766 }
2767 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
2768 }
2769
2770 /* Now update local dynamic symbols. */
2771 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
2772 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
2773 entry->isym.st_name);
2774
2775 /* And the rest of dynamic symbols. */
2776 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
2777
2778 /* Adjust version definitions. */
2779 if (elf_tdata (output_bfd)->cverdefs)
2780 {
2781 asection *s;
2782 bfd_byte *p;
2783 bfd_size_type i;
2784 Elf_Internal_Verdef def;
2785 Elf_Internal_Verdaux defaux;
2786
2787 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
2788 p = s->contents;
2789 do
2790 {
2791 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
2792 &def);
2793 p += sizeof (Elf_External_Verdef);
2794 for (i = 0; i < def.vd_cnt; ++i)
2795 {
2796 _bfd_elf_swap_verdaux_in (output_bfd,
2797 (Elf_External_Verdaux *) p, &defaux);
2798 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
2799 defaux.vda_name);
2800 _bfd_elf_swap_verdaux_out (output_bfd,
2801 &defaux, (Elf_External_Verdaux *) p);
2802 p += sizeof (Elf_External_Verdaux);
2803 }
2804 }
2805 while (def.vd_next);
2806 }
2807
2808 /* Adjust version references. */
2809 if (elf_tdata (output_bfd)->verref)
2810 {
2811 asection *s;
2812 bfd_byte *p;
2813 bfd_size_type i;
2814 Elf_Internal_Verneed need;
2815 Elf_Internal_Vernaux needaux;
2816
2817 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
2818 p = s->contents;
2819 do
2820 {
2821 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
2822 &need);
2823 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
2824 _bfd_elf_swap_verneed_out (output_bfd, &need,
2825 (Elf_External_Verneed *) p);
2826 p += sizeof (Elf_External_Verneed);
2827 for (i = 0; i < need.vn_cnt; ++i)
2828 {
2829 _bfd_elf_swap_vernaux_in (output_bfd,
2830 (Elf_External_Vernaux *) p, &needaux);
2831 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
2832 needaux.vna_name);
2833 _bfd_elf_swap_vernaux_out (output_bfd,
2834 &needaux,
2835 (Elf_External_Vernaux *) p);
2836 p += sizeof (Elf_External_Vernaux);
2837 }
2838 }
2839 while (need.vn_next);
2840 }
2841
2842 return TRUE;
2843 }
2844 \f
2845 /* Add symbols from an ELF object file to the linker hash table. */
2846
2847 static bfd_boolean
2848 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
2849 {
2850 bfd_boolean (*add_symbol_hook)
2851 (bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
2852 const char **, flagword *, asection **, bfd_vma *);
2853 bfd_boolean (*check_relocs)
2854 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
2855 bfd_boolean collect;
2856 Elf_Internal_Shdr *hdr;
2857 bfd_size_type symcount;
2858 bfd_size_type extsymcount;
2859 bfd_size_type extsymoff;
2860 struct elf_link_hash_entry **sym_hash;
2861 bfd_boolean dynamic;
2862 Elf_External_Versym *extversym = NULL;
2863 Elf_External_Versym *ever;
2864 struct elf_link_hash_entry *weaks;
2865 struct elf_link_hash_entry **nondeflt_vers = NULL;
2866 bfd_size_type nondeflt_vers_cnt = 0;
2867 Elf_Internal_Sym *isymbuf = NULL;
2868 Elf_Internal_Sym *isym;
2869 Elf_Internal_Sym *isymend;
2870 const struct elf_backend_data *bed;
2871 bfd_boolean add_needed;
2872 struct elf_link_hash_table * hash_table;
2873 bfd_size_type amt;
2874
2875 hash_table = elf_hash_table (info);
2876
2877 bed = get_elf_backend_data (abfd);
2878 add_symbol_hook = bed->elf_add_symbol_hook;
2879 collect = bed->collect;
2880
2881 if ((abfd->flags & DYNAMIC) == 0)
2882 dynamic = FALSE;
2883 else
2884 {
2885 dynamic = TRUE;
2886
2887 /* You can't use -r against a dynamic object. Also, there's no
2888 hope of using a dynamic object which does not exactly match
2889 the format of the output file. */
2890 if (info->relocatable
2891 || !is_elf_hash_table (hash_table)
2892 || hash_table->root.creator != abfd->xvec)
2893 {
2894 bfd_set_error (bfd_error_invalid_operation);
2895 goto error_return;
2896 }
2897 }
2898
2899 /* As a GNU extension, any input sections which are named
2900 .gnu.warning.SYMBOL are treated as warning symbols for the given
2901 symbol. This differs from .gnu.warning sections, which generate
2902 warnings when they are included in an output file. */
2903 if (info->executable)
2904 {
2905 asection *s;
2906
2907 for (s = abfd->sections; s != NULL; s = s->next)
2908 {
2909 const char *name;
2910
2911 name = bfd_get_section_name (abfd, s);
2912 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
2913 {
2914 char *msg;
2915 bfd_size_type sz;
2916 bfd_size_type prefix_len;
2917 const char * gnu_warning_prefix = _("warning: ");
2918
2919 name += sizeof ".gnu.warning." - 1;
2920
2921 /* If this is a shared object, then look up the symbol
2922 in the hash table. If it is there, and it is already
2923 been defined, then we will not be using the entry
2924 from this shared object, so we don't need to warn.
2925 FIXME: If we see the definition in a regular object
2926 later on, we will warn, but we shouldn't. The only
2927 fix is to keep track of what warnings we are supposed
2928 to emit, and then handle them all at the end of the
2929 link. */
2930 if (dynamic)
2931 {
2932 struct elf_link_hash_entry *h;
2933
2934 h = elf_link_hash_lookup (hash_table, name,
2935 FALSE, FALSE, TRUE);
2936
2937 /* FIXME: What about bfd_link_hash_common? */
2938 if (h != NULL
2939 && (h->root.type == bfd_link_hash_defined
2940 || h->root.type == bfd_link_hash_defweak))
2941 {
2942 /* We don't want to issue this warning. Clobber
2943 the section size so that the warning does not
2944 get copied into the output file. */
2945 s->_raw_size = 0;
2946 continue;
2947 }
2948 }
2949
2950 sz = bfd_section_size (abfd, s);
2951 prefix_len = strlen (gnu_warning_prefix);
2952 msg = bfd_alloc (abfd, prefix_len + sz + 1);
2953 if (msg == NULL)
2954 goto error_return;
2955
2956 strcpy (msg, gnu_warning_prefix);
2957 if (! bfd_get_section_contents (abfd, s, msg + prefix_len, 0, sz))
2958 goto error_return;
2959
2960 msg[prefix_len + sz] = '\0';
2961
2962 if (! (_bfd_generic_link_add_one_symbol
2963 (info, abfd, name, BSF_WARNING, s, 0, msg,
2964 FALSE, collect, NULL)))
2965 goto error_return;
2966
2967 if (! info->relocatable)
2968 {
2969 /* Clobber the section size so that the warning does
2970 not get copied into the output file. */
2971 s->_raw_size = 0;
2972 }
2973 }
2974 }
2975 }
2976
2977 add_needed = TRUE;
2978 if (! dynamic)
2979 {
2980 /* If we are creating a shared library, create all the dynamic
2981 sections immediately. We need to attach them to something,
2982 so we attach them to this BFD, provided it is the right
2983 format. FIXME: If there are no input BFD's of the same
2984 format as the output, we can't make a shared library. */
2985 if (info->shared
2986 && is_elf_hash_table (hash_table)
2987 && hash_table->root.creator == abfd->xvec
2988 && ! hash_table->dynamic_sections_created)
2989 {
2990 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
2991 goto error_return;
2992 }
2993 }
2994 else if (!is_elf_hash_table (hash_table))
2995 goto error_return;
2996 else
2997 {
2998 asection *s;
2999 const char *soname = NULL;
3000 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3001 int ret;
3002
3003 /* ld --just-symbols and dynamic objects don't mix very well.
3004 Test for --just-symbols by looking at info set up by
3005 _bfd_elf_link_just_syms. */
3006 if ((s = abfd->sections) != NULL
3007 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3008 goto error_return;
3009
3010 /* If this dynamic lib was specified on the command line with
3011 --as-needed in effect, then we don't want to add a DT_NEEDED
3012 tag unless the lib is actually used. Similary for libs brought
3013 in by another lib's DT_NEEDED. */
3014 add_needed = elf_dyn_lib_class (abfd) == DYN_NORMAL;
3015
3016 s = bfd_get_section_by_name (abfd, ".dynamic");
3017 if (s != NULL)
3018 {
3019 bfd_byte *dynbuf;
3020 bfd_byte *extdyn;
3021 int elfsec;
3022 unsigned long shlink;
3023
3024 dynbuf = bfd_malloc (s->_raw_size);
3025 if (dynbuf == NULL)
3026 goto error_return;
3027
3028 if (! bfd_get_section_contents (abfd, s, dynbuf, 0, s->_raw_size))
3029 goto error_free_dyn;
3030
3031 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3032 if (elfsec == -1)
3033 goto error_free_dyn;
3034 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3035
3036 for (extdyn = dynbuf;
3037 extdyn < dynbuf + s->_raw_size;
3038 extdyn += bed->s->sizeof_dyn)
3039 {
3040 Elf_Internal_Dyn dyn;
3041
3042 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3043 if (dyn.d_tag == DT_SONAME)
3044 {
3045 unsigned int tagv = dyn.d_un.d_val;
3046 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3047 if (soname == NULL)
3048 goto error_free_dyn;
3049 }
3050 if (dyn.d_tag == DT_NEEDED)
3051 {
3052 struct bfd_link_needed_list *n, **pn;
3053 char *fnm, *anm;
3054 unsigned int tagv = dyn.d_un.d_val;
3055
3056 amt = sizeof (struct bfd_link_needed_list);
3057 n = bfd_alloc (abfd, amt);
3058 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3059 if (n == NULL || fnm == NULL)
3060 goto error_free_dyn;
3061 amt = strlen (fnm) + 1;
3062 anm = bfd_alloc (abfd, amt);
3063 if (anm == NULL)
3064 goto error_free_dyn;
3065 memcpy (anm, fnm, amt);
3066 n->name = anm;
3067 n->by = abfd;
3068 n->next = NULL;
3069 for (pn = & hash_table->needed;
3070 *pn != NULL;
3071 pn = &(*pn)->next)
3072 ;
3073 *pn = n;
3074 }
3075 if (dyn.d_tag == DT_RUNPATH)
3076 {
3077 struct bfd_link_needed_list *n, **pn;
3078 char *fnm, *anm;
3079 unsigned int tagv = dyn.d_un.d_val;
3080
3081 amt = sizeof (struct bfd_link_needed_list);
3082 n = bfd_alloc (abfd, amt);
3083 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3084 if (n == NULL || fnm == NULL)
3085 goto error_free_dyn;
3086 amt = strlen (fnm) + 1;
3087 anm = bfd_alloc (abfd, amt);
3088 if (anm == NULL)
3089 goto error_free_dyn;
3090 memcpy (anm, fnm, amt);
3091 n->name = anm;
3092 n->by = abfd;
3093 n->next = NULL;
3094 for (pn = & runpath;
3095 *pn != NULL;
3096 pn = &(*pn)->next)
3097 ;
3098 *pn = n;
3099 }
3100 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3101 if (!runpath && dyn.d_tag == DT_RPATH)
3102 {
3103 struct bfd_link_needed_list *n, **pn;
3104 char *fnm, *anm;
3105 unsigned int tagv = dyn.d_un.d_val;
3106
3107 amt = sizeof (struct bfd_link_needed_list);
3108 n = bfd_alloc (abfd, amt);
3109 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3110 if (n == NULL || fnm == NULL)
3111 goto error_free_dyn;
3112 amt = strlen (fnm) + 1;
3113 anm = bfd_alloc (abfd, amt);
3114 if (anm == NULL)
3115 {
3116 error_free_dyn:
3117 free (dynbuf);
3118 goto error_return;
3119 }
3120 memcpy (anm, fnm, amt);
3121 n->name = anm;
3122 n->by = abfd;
3123 n->next = NULL;
3124 for (pn = & rpath;
3125 *pn != NULL;
3126 pn = &(*pn)->next)
3127 ;
3128 *pn = n;
3129 }
3130 }
3131
3132 free (dynbuf);
3133 }
3134
3135 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3136 frees all more recently bfd_alloc'd blocks as well. */
3137 if (runpath)
3138 rpath = runpath;
3139
3140 if (rpath)
3141 {
3142 struct bfd_link_needed_list **pn;
3143 for (pn = & hash_table->runpath;
3144 *pn != NULL;
3145 pn = &(*pn)->next)
3146 ;
3147 *pn = rpath;
3148 }
3149
3150 /* We do not want to include any of the sections in a dynamic
3151 object in the output file. We hack by simply clobbering the
3152 list of sections in the BFD. This could be handled more
3153 cleanly by, say, a new section flag; the existing
3154 SEC_NEVER_LOAD flag is not the one we want, because that one
3155 still implies that the section takes up space in the output
3156 file. */
3157 bfd_section_list_clear (abfd);
3158
3159 /* If this is the first dynamic object found in the link, create
3160 the special sections required for dynamic linking. */
3161 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3162 goto error_return;
3163
3164 /* Find the name to use in a DT_NEEDED entry that refers to this
3165 object. If the object has a DT_SONAME entry, we use it.
3166 Otherwise, if the generic linker stuck something in
3167 elf_dt_name, we use that. Otherwise, we just use the file
3168 name. */
3169 if (soname == NULL || *soname == '\0')
3170 {
3171 soname = elf_dt_name (abfd);
3172 if (soname == NULL || *soname == '\0')
3173 soname = bfd_get_filename (abfd);
3174 }
3175
3176 /* Save the SONAME because sometimes the linker emulation code
3177 will need to know it. */
3178 elf_dt_name (abfd) = soname;
3179
3180 ret = elf_add_dt_needed_tag (info, soname, add_needed);
3181 if (ret < 0)
3182 goto error_return;
3183
3184 /* If we have already included this dynamic object in the
3185 link, just ignore it. There is no reason to include a
3186 particular dynamic object more than once. */
3187 if (ret > 0)
3188 return TRUE;
3189 }
3190
3191 /* If this is a dynamic object, we always link against the .dynsym
3192 symbol table, not the .symtab symbol table. The dynamic linker
3193 will only see the .dynsym symbol table, so there is no reason to
3194 look at .symtab for a dynamic object. */
3195
3196 if (! dynamic || elf_dynsymtab (abfd) == 0)
3197 hdr = &elf_tdata (abfd)->symtab_hdr;
3198 else
3199 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3200
3201 symcount = hdr->sh_size / bed->s->sizeof_sym;
3202
3203 /* The sh_info field of the symtab header tells us where the
3204 external symbols start. We don't care about the local symbols at
3205 this point. */
3206 if (elf_bad_symtab (abfd))
3207 {
3208 extsymcount = symcount;
3209 extsymoff = 0;
3210 }
3211 else
3212 {
3213 extsymcount = symcount - hdr->sh_info;
3214 extsymoff = hdr->sh_info;
3215 }
3216
3217 sym_hash = NULL;
3218 if (extsymcount != 0)
3219 {
3220 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3221 NULL, NULL, NULL);
3222 if (isymbuf == NULL)
3223 goto error_return;
3224
3225 /* We store a pointer to the hash table entry for each external
3226 symbol. */
3227 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3228 sym_hash = bfd_alloc (abfd, amt);
3229 if (sym_hash == NULL)
3230 goto error_free_sym;
3231 elf_sym_hashes (abfd) = sym_hash;
3232 }
3233
3234 if (dynamic)
3235 {
3236 /* Read in any version definitions. */
3237 if (! _bfd_elf_slurp_version_tables (abfd))
3238 goto error_free_sym;
3239
3240 /* Read in the symbol versions, but don't bother to convert them
3241 to internal format. */
3242 if (elf_dynversym (abfd) != 0)
3243 {
3244 Elf_Internal_Shdr *versymhdr;
3245
3246 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3247 extversym = bfd_malloc (versymhdr->sh_size);
3248 if (extversym == NULL)
3249 goto error_free_sym;
3250 amt = versymhdr->sh_size;
3251 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3252 || bfd_bread (extversym, amt, abfd) != amt)
3253 goto error_free_vers;
3254 }
3255 }
3256
3257 weaks = NULL;
3258
3259 ever = extversym != NULL ? extversym + extsymoff : NULL;
3260 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3261 isym < isymend;
3262 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3263 {
3264 int bind;
3265 bfd_vma value;
3266 asection *sec;
3267 flagword flags;
3268 const char *name;
3269 struct elf_link_hash_entry *h;
3270 bfd_boolean definition;
3271 bfd_boolean size_change_ok;
3272 bfd_boolean type_change_ok;
3273 bfd_boolean new_weakdef;
3274 bfd_boolean override;
3275 unsigned int old_alignment;
3276 bfd *old_bfd;
3277
3278 override = FALSE;
3279
3280 flags = BSF_NO_FLAGS;
3281 sec = NULL;
3282 value = isym->st_value;
3283 *sym_hash = NULL;
3284
3285 bind = ELF_ST_BIND (isym->st_info);
3286 if (bind == STB_LOCAL)
3287 {
3288 /* This should be impossible, since ELF requires that all
3289 global symbols follow all local symbols, and that sh_info
3290 point to the first global symbol. Unfortunately, Irix 5
3291 screws this up. */
3292 continue;
3293 }
3294 else if (bind == STB_GLOBAL)
3295 {
3296 if (isym->st_shndx != SHN_UNDEF
3297 && isym->st_shndx != SHN_COMMON)
3298 flags = BSF_GLOBAL;
3299 }
3300 else if (bind == STB_WEAK)
3301 flags = BSF_WEAK;
3302 else
3303 {
3304 /* Leave it up to the processor backend. */
3305 }
3306
3307 if (isym->st_shndx == SHN_UNDEF)
3308 sec = bfd_und_section_ptr;
3309 else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
3310 {
3311 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3312 if (sec == NULL)
3313 sec = bfd_abs_section_ptr;
3314 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3315 value -= sec->vma;
3316 }
3317 else if (isym->st_shndx == SHN_ABS)
3318 sec = bfd_abs_section_ptr;
3319 else if (isym->st_shndx == SHN_COMMON)
3320 {
3321 sec = bfd_com_section_ptr;
3322 /* What ELF calls the size we call the value. What ELF
3323 calls the value we call the alignment. */
3324 value = isym->st_size;
3325 }
3326 else
3327 {
3328 /* Leave it up to the processor backend. */
3329 }
3330
3331 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3332 isym->st_name);
3333 if (name == NULL)
3334 goto error_free_vers;
3335
3336 if (isym->st_shndx == SHN_COMMON
3337 && ELF_ST_TYPE (isym->st_info) == STT_TLS)
3338 {
3339 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3340
3341 if (tcomm == NULL)
3342 {
3343 tcomm = bfd_make_section (abfd, ".tcommon");
3344 if (tcomm == NULL
3345 || !bfd_set_section_flags (abfd, tcomm, (SEC_ALLOC
3346 | SEC_IS_COMMON
3347 | SEC_LINKER_CREATED
3348 | SEC_THREAD_LOCAL)))
3349 goto error_free_vers;
3350 }
3351 sec = tcomm;
3352 }
3353 else if (add_symbol_hook)
3354 {
3355 if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec,
3356 &value))
3357 goto error_free_vers;
3358
3359 /* The hook function sets the name to NULL if this symbol
3360 should be skipped for some reason. */
3361 if (name == NULL)
3362 continue;
3363 }
3364
3365 /* Sanity check that all possibilities were handled. */
3366 if (sec == NULL)
3367 {
3368 bfd_set_error (bfd_error_bad_value);
3369 goto error_free_vers;
3370 }
3371
3372 if (bfd_is_und_section (sec)
3373 || bfd_is_com_section (sec))
3374 definition = FALSE;
3375 else
3376 definition = TRUE;
3377
3378 size_change_ok = FALSE;
3379 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
3380 old_alignment = 0;
3381 old_bfd = NULL;
3382
3383 if (is_elf_hash_table (hash_table))
3384 {
3385 Elf_Internal_Versym iver;
3386 unsigned int vernum = 0;
3387 bfd_boolean skip;
3388
3389 if (ever != NULL)
3390 {
3391 _bfd_elf_swap_versym_in (abfd, ever, &iver);
3392 vernum = iver.vs_vers & VERSYM_VERSION;
3393
3394 /* If this is a hidden symbol, or if it is not version
3395 1, we append the version name to the symbol name.
3396 However, we do not modify a non-hidden absolute
3397 symbol, because it might be the version symbol
3398 itself. FIXME: What if it isn't? */
3399 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
3400 || (vernum > 1 && ! bfd_is_abs_section (sec)))
3401 {
3402 const char *verstr;
3403 size_t namelen, verlen, newlen;
3404 char *newname, *p;
3405
3406 if (isym->st_shndx != SHN_UNDEF)
3407 {
3408 if (vernum > elf_tdata (abfd)->dynverdef_hdr.sh_info)
3409 {
3410 (*_bfd_error_handler)
3411 (_("%s: %s: invalid version %u (max %d)"),
3412 bfd_archive_filename (abfd), name, vernum,
3413 elf_tdata (abfd)->dynverdef_hdr.sh_info);
3414 bfd_set_error (bfd_error_bad_value);
3415 goto error_free_vers;
3416 }
3417 else if (vernum > 1)
3418 verstr =
3419 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
3420 else
3421 verstr = "";
3422 }
3423 else
3424 {
3425 /* We cannot simply test for the number of
3426 entries in the VERNEED section since the
3427 numbers for the needed versions do not start
3428 at 0. */
3429 Elf_Internal_Verneed *t;
3430
3431 verstr = NULL;
3432 for (t = elf_tdata (abfd)->verref;
3433 t != NULL;
3434 t = t->vn_nextref)
3435 {
3436 Elf_Internal_Vernaux *a;
3437
3438 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3439 {
3440 if (a->vna_other == vernum)
3441 {
3442 verstr = a->vna_nodename;
3443 break;
3444 }
3445 }
3446 if (a != NULL)
3447 break;
3448 }
3449 if (verstr == NULL)
3450 {
3451 (*_bfd_error_handler)
3452 (_("%s: %s: invalid needed version %d"),
3453 bfd_archive_filename (abfd), name, vernum);
3454 bfd_set_error (bfd_error_bad_value);
3455 goto error_free_vers;
3456 }
3457 }
3458
3459 namelen = strlen (name);
3460 verlen = strlen (verstr);
3461 newlen = namelen + verlen + 2;
3462 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3463 && isym->st_shndx != SHN_UNDEF)
3464 ++newlen;
3465
3466 newname = bfd_alloc (abfd, newlen);
3467 if (newname == NULL)
3468 goto error_free_vers;
3469 memcpy (newname, name, namelen);
3470 p = newname + namelen;
3471 *p++ = ELF_VER_CHR;
3472 /* If this is a defined non-hidden version symbol,
3473 we add another @ to the name. This indicates the
3474 default version of the symbol. */
3475 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3476 && isym->st_shndx != SHN_UNDEF)
3477 *p++ = ELF_VER_CHR;
3478 memcpy (p, verstr, verlen + 1);
3479
3480 name = newname;
3481 }
3482 }
3483
3484 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
3485 sym_hash, &skip, &override,
3486 &type_change_ok, &size_change_ok))
3487 goto error_free_vers;
3488
3489 if (skip)
3490 continue;
3491
3492 if (override)
3493 definition = FALSE;
3494
3495 h = *sym_hash;
3496 while (h->root.type == bfd_link_hash_indirect
3497 || h->root.type == bfd_link_hash_warning)
3498 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3499
3500 /* Remember the old alignment if this is a common symbol, so
3501 that we don't reduce the alignment later on. We can't
3502 check later, because _bfd_generic_link_add_one_symbol
3503 will set a default for the alignment which we want to
3504 override. We also remember the old bfd where the existing
3505 definition comes from. */
3506 switch (h->root.type)
3507 {
3508 default:
3509 break;
3510
3511 case bfd_link_hash_defined:
3512 case bfd_link_hash_defweak:
3513 old_bfd = h->root.u.def.section->owner;
3514 break;
3515
3516 case bfd_link_hash_common:
3517 old_bfd = h->root.u.c.p->section->owner;
3518 old_alignment = h->root.u.c.p->alignment_power;
3519 break;
3520 }
3521
3522 if (elf_tdata (abfd)->verdef != NULL
3523 && ! override
3524 && vernum > 1
3525 && definition)
3526 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
3527 }
3528
3529 if (! (_bfd_generic_link_add_one_symbol
3530 (info, abfd, name, flags, sec, value, NULL, FALSE, collect,
3531 (struct bfd_link_hash_entry **) sym_hash)))
3532 goto error_free_vers;
3533
3534 h = *sym_hash;
3535 while (h->root.type == bfd_link_hash_indirect
3536 || h->root.type == bfd_link_hash_warning)
3537 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3538 *sym_hash = h;
3539
3540 new_weakdef = FALSE;
3541 if (dynamic
3542 && definition
3543 && (flags & BSF_WEAK) != 0
3544 && ELF_ST_TYPE (isym->st_info) != STT_FUNC
3545 && is_elf_hash_table (hash_table)
3546 && h->weakdef == NULL)
3547 {
3548 /* Keep a list of all weak defined non function symbols from
3549 a dynamic object, using the weakdef field. Later in this
3550 function we will set the weakdef field to the correct
3551 value. We only put non-function symbols from dynamic
3552 objects on this list, because that happens to be the only
3553 time we need to know the normal symbol corresponding to a
3554 weak symbol, and the information is time consuming to
3555 figure out. If the weakdef field is not already NULL,
3556 then this symbol was already defined by some previous
3557 dynamic object, and we will be using that previous
3558 definition anyhow. */
3559
3560 h->weakdef = weaks;
3561 weaks = h;
3562 new_weakdef = TRUE;
3563 }
3564
3565 /* Set the alignment of a common symbol. */
3566 if (isym->st_shndx == SHN_COMMON
3567 && h->root.type == bfd_link_hash_common)
3568 {
3569 unsigned int align;
3570
3571 align = bfd_log2 (isym->st_value);
3572 if (align > old_alignment
3573 /* Permit an alignment power of zero if an alignment of one
3574 is specified and no other alignments have been specified. */
3575 || (isym->st_value == 1 && old_alignment == 0))
3576 h->root.u.c.p->alignment_power = align;
3577 else
3578 h->root.u.c.p->alignment_power = old_alignment;
3579 }
3580
3581 if (is_elf_hash_table (hash_table))
3582 {
3583 int old_flags;
3584 bfd_boolean dynsym;
3585 int new_flag;
3586
3587 /* Check the alignment when a common symbol is involved. This
3588 can change when a common symbol is overridden by a normal
3589 definition or a common symbol is ignored due to the old
3590 normal definition. We need to make sure the maximum
3591 alignment is maintained. */
3592 if ((old_alignment || isym->st_shndx == SHN_COMMON)
3593 && h->root.type != bfd_link_hash_common)
3594 {
3595 unsigned int common_align;
3596 unsigned int normal_align;
3597 unsigned int symbol_align;
3598 bfd *normal_bfd;
3599 bfd *common_bfd;
3600
3601 symbol_align = ffs (h->root.u.def.value) - 1;
3602 if (h->root.u.def.section->owner != NULL
3603 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3604 {
3605 normal_align = h->root.u.def.section->alignment_power;
3606 if (normal_align > symbol_align)
3607 normal_align = symbol_align;
3608 }
3609 else
3610 normal_align = symbol_align;
3611
3612 if (old_alignment)
3613 {
3614 common_align = old_alignment;
3615 common_bfd = old_bfd;
3616 normal_bfd = abfd;
3617 }
3618 else
3619 {
3620 common_align = bfd_log2 (isym->st_value);
3621 common_bfd = abfd;
3622 normal_bfd = old_bfd;
3623 }
3624
3625 if (normal_align < common_align)
3626 (*_bfd_error_handler)
3627 (_("Warning: alignment %u of symbol `%s' in %s is smaller than %u in %s"),
3628 1 << normal_align,
3629 name,
3630 bfd_archive_filename (normal_bfd),
3631 1 << common_align,
3632 bfd_archive_filename (common_bfd));
3633 }
3634
3635 /* Remember the symbol size and type. */
3636 if (isym->st_size != 0
3637 && (definition || h->size == 0))
3638 {
3639 if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
3640 (*_bfd_error_handler)
3641 (_("Warning: size of symbol `%s' changed from %lu in %s to %lu in %s"),
3642 name, (unsigned long) h->size,
3643 bfd_archive_filename (old_bfd),
3644 (unsigned long) isym->st_size,
3645 bfd_archive_filename (abfd));
3646
3647 h->size = isym->st_size;
3648 }
3649
3650 /* If this is a common symbol, then we always want H->SIZE
3651 to be the size of the common symbol. The code just above
3652 won't fix the size if a common symbol becomes larger. We
3653 don't warn about a size change here, because that is
3654 covered by --warn-common. */
3655 if (h->root.type == bfd_link_hash_common)
3656 h->size = h->root.u.c.size;
3657
3658 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
3659 && (definition || h->type == STT_NOTYPE))
3660 {
3661 if (h->type != STT_NOTYPE
3662 && h->type != ELF_ST_TYPE (isym->st_info)
3663 && ! type_change_ok)
3664 (*_bfd_error_handler)
3665 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
3666 name, h->type, ELF_ST_TYPE (isym->st_info),
3667 bfd_archive_filename (abfd));
3668
3669 h->type = ELF_ST_TYPE (isym->st_info);
3670 }
3671
3672 /* If st_other has a processor-specific meaning, specific
3673 code might be needed here. We never merge the visibility
3674 attribute with the one from a dynamic object. */
3675 if (bed->elf_backend_merge_symbol_attribute)
3676 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
3677 dynamic);
3678
3679 if (isym->st_other != 0 && !dynamic)
3680 {
3681 unsigned char hvis, symvis, other, nvis;
3682
3683 /* Take the balance of OTHER from the definition. */
3684 other = (definition ? isym->st_other : h->other);
3685 other &= ~ ELF_ST_VISIBILITY (-1);
3686
3687 /* Combine visibilities, using the most constraining one. */
3688 hvis = ELF_ST_VISIBILITY (h->other);
3689 symvis = ELF_ST_VISIBILITY (isym->st_other);
3690 if (! hvis)
3691 nvis = symvis;
3692 else if (! symvis)
3693 nvis = hvis;
3694 else
3695 nvis = hvis < symvis ? hvis : symvis;
3696
3697 h->other = other | nvis;
3698 }
3699
3700 /* Set a flag in the hash table entry indicating the type of
3701 reference or definition we just found. Keep a count of
3702 the number of dynamic symbols we find. A dynamic symbol
3703 is one which is referenced or defined by both a regular
3704 object and a shared object. */
3705 old_flags = h->elf_link_hash_flags;
3706 dynsym = FALSE;
3707 if (! dynamic)
3708 {
3709 if (! definition)
3710 {
3711 new_flag = ELF_LINK_HASH_REF_REGULAR;
3712 if (bind != STB_WEAK)
3713 new_flag |= ELF_LINK_HASH_REF_REGULAR_NONWEAK;
3714 }
3715 else
3716 new_flag = ELF_LINK_HASH_DEF_REGULAR;
3717 if (! info->executable
3718 || (old_flags & (ELF_LINK_HASH_DEF_DYNAMIC
3719 | ELF_LINK_HASH_REF_DYNAMIC)) != 0)
3720 dynsym = TRUE;
3721 }
3722 else
3723 {
3724 if (! definition)
3725 new_flag = ELF_LINK_HASH_REF_DYNAMIC;
3726 else
3727 new_flag = ELF_LINK_HASH_DEF_DYNAMIC;
3728 if ((old_flags & (ELF_LINK_HASH_DEF_REGULAR
3729 | ELF_LINK_HASH_REF_REGULAR)) != 0
3730 || (h->weakdef != NULL
3731 && ! new_weakdef
3732 && h->weakdef->dynindx != -1))
3733 dynsym = TRUE;
3734 }
3735
3736 h->elf_link_hash_flags |= new_flag;
3737
3738 /* Check to see if we need to add an indirect symbol for
3739 the default name. */
3740 if (definition || h->root.type == bfd_link_hash_common)
3741 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
3742 &sec, &value, &dynsym,
3743 override))
3744 goto error_free_vers;
3745
3746 if (definition && !dynamic)
3747 {
3748 char *p = strchr (name, ELF_VER_CHR);
3749 if (p != NULL && p[1] != ELF_VER_CHR)
3750 {
3751 /* Queue non-default versions so that .symver x, x@FOO
3752 aliases can be checked. */
3753 if (! nondeflt_vers)
3754 {
3755 amt = (isymend - isym + 1)
3756 * sizeof (struct elf_link_hash_entry *);
3757 nondeflt_vers = bfd_malloc (amt);
3758 }
3759 nondeflt_vers [nondeflt_vers_cnt++] = h;
3760 }
3761 }
3762
3763 if (dynsym && h->dynindx == -1)
3764 {
3765 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
3766 goto error_free_vers;
3767 if (h->weakdef != NULL
3768 && ! new_weakdef
3769 && h->weakdef->dynindx == -1)
3770 {
3771 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
3772 goto error_free_vers;
3773 }
3774 }
3775 else if (dynsym && h->dynindx != -1)
3776 /* If the symbol already has a dynamic index, but
3777 visibility says it should not be visible, turn it into
3778 a local symbol. */
3779 switch (ELF_ST_VISIBILITY (h->other))
3780 {
3781 case STV_INTERNAL:
3782 case STV_HIDDEN:
3783 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
3784 dynsym = FALSE;
3785 break;
3786 }
3787
3788 if (!add_needed
3789 && definition
3790 && dynsym
3791 && (h->elf_link_hash_flags
3792 & ELF_LINK_HASH_REF_REGULAR) != 0)
3793 {
3794 int ret;
3795 const char *soname = elf_dt_name (abfd);
3796
3797 /* A symbol from a library loaded via DT_NEEDED of some
3798 other library is referenced by a regular object.
3799 Add a DT_NEEDED entry for it. */
3800 add_needed = TRUE;
3801 ret = elf_add_dt_needed_tag (info, soname, add_needed);
3802 if (ret < 0)
3803 goto error_free_vers;
3804
3805 BFD_ASSERT (ret == 0);
3806 }
3807 }
3808 }
3809
3810 /* Now that all the symbols from this input file are created, handle
3811 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
3812 if (nondeflt_vers != NULL)
3813 {
3814 bfd_size_type cnt, symidx;
3815
3816 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
3817 {
3818 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
3819 char *shortname, *p;
3820
3821 p = strchr (h->root.root.string, ELF_VER_CHR);
3822 if (p == NULL
3823 || (h->root.type != bfd_link_hash_defined
3824 && h->root.type != bfd_link_hash_defweak))
3825 continue;
3826
3827 amt = p - h->root.root.string;
3828 shortname = bfd_malloc (amt + 1);
3829 memcpy (shortname, h->root.root.string, amt);
3830 shortname[amt] = '\0';
3831
3832 hi = (struct elf_link_hash_entry *)
3833 bfd_link_hash_lookup (&hash_table->root, shortname,
3834 FALSE, FALSE, FALSE);
3835 if (hi != NULL
3836 && hi->root.type == h->root.type
3837 && hi->root.u.def.value == h->root.u.def.value
3838 && hi->root.u.def.section == h->root.u.def.section)
3839 {
3840 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
3841 hi->root.type = bfd_link_hash_indirect;
3842 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
3843 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
3844 sym_hash = elf_sym_hashes (abfd);
3845 if (sym_hash)
3846 for (symidx = 0; symidx < extsymcount; ++symidx)
3847 if (sym_hash[symidx] == hi)
3848 {
3849 sym_hash[symidx] = h;
3850 break;
3851 }
3852 }
3853 free (shortname);
3854 }
3855 free (nondeflt_vers);
3856 nondeflt_vers = NULL;
3857 }
3858
3859 if (extversym != NULL)
3860 {
3861 free (extversym);
3862 extversym = NULL;
3863 }
3864
3865 if (isymbuf != NULL)
3866 free (isymbuf);
3867 isymbuf = NULL;
3868
3869 /* Now set the weakdefs field correctly for all the weak defined
3870 symbols we found. The only way to do this is to search all the
3871 symbols. Since we only need the information for non functions in
3872 dynamic objects, that's the only time we actually put anything on
3873 the list WEAKS. We need this information so that if a regular
3874 object refers to a symbol defined weakly in a dynamic object, the
3875 real symbol in the dynamic object is also put in the dynamic
3876 symbols; we also must arrange for both symbols to point to the
3877 same memory location. We could handle the general case of symbol
3878 aliasing, but a general symbol alias can only be generated in
3879 assembler code, handling it correctly would be very time
3880 consuming, and other ELF linkers don't handle general aliasing
3881 either. */
3882 if (weaks != NULL)
3883 {
3884 struct elf_link_hash_entry **hpp;
3885 struct elf_link_hash_entry **hppend;
3886 struct elf_link_hash_entry **sorted_sym_hash;
3887 struct elf_link_hash_entry *h;
3888 size_t sym_count;
3889
3890 /* Since we have to search the whole symbol list for each weak
3891 defined symbol, search time for N weak defined symbols will be
3892 O(N^2). Binary search will cut it down to O(NlogN). */
3893 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3894 sorted_sym_hash = bfd_malloc (amt);
3895 if (sorted_sym_hash == NULL)
3896 goto error_return;
3897 sym_hash = sorted_sym_hash;
3898 hpp = elf_sym_hashes (abfd);
3899 hppend = hpp + extsymcount;
3900 sym_count = 0;
3901 for (; hpp < hppend; hpp++)
3902 {
3903 h = *hpp;
3904 if (h != NULL
3905 && h->root.type == bfd_link_hash_defined
3906 && h->type != STT_FUNC)
3907 {
3908 *sym_hash = h;
3909 sym_hash++;
3910 sym_count++;
3911 }
3912 }
3913
3914 qsort (sorted_sym_hash, sym_count,
3915 sizeof (struct elf_link_hash_entry *),
3916 elf_sort_symbol);
3917
3918 while (weaks != NULL)
3919 {
3920 struct elf_link_hash_entry *hlook;
3921 asection *slook;
3922 bfd_vma vlook;
3923 long ilook;
3924 size_t i, j, idx;
3925
3926 hlook = weaks;
3927 weaks = hlook->weakdef;
3928 hlook->weakdef = NULL;
3929
3930 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
3931 || hlook->root.type == bfd_link_hash_defweak
3932 || hlook->root.type == bfd_link_hash_common
3933 || hlook->root.type == bfd_link_hash_indirect);
3934 slook = hlook->root.u.def.section;
3935 vlook = hlook->root.u.def.value;
3936
3937 ilook = -1;
3938 i = 0;
3939 j = sym_count;
3940 while (i < j)
3941 {
3942 bfd_signed_vma vdiff;
3943 idx = (i + j) / 2;
3944 h = sorted_sym_hash [idx];
3945 vdiff = vlook - h->root.u.def.value;
3946 if (vdiff < 0)
3947 j = idx;
3948 else if (vdiff > 0)
3949 i = idx + 1;
3950 else
3951 {
3952 long sdiff = slook - h->root.u.def.section;
3953 if (sdiff < 0)
3954 j = idx;
3955 else if (sdiff > 0)
3956 i = idx + 1;
3957 else
3958 {
3959 ilook = idx;
3960 break;
3961 }
3962 }
3963 }
3964
3965 /* We didn't find a value/section match. */
3966 if (ilook == -1)
3967 continue;
3968
3969 for (i = ilook; i < sym_count; i++)
3970 {
3971 h = sorted_sym_hash [i];
3972
3973 /* Stop if value or section doesn't match. */
3974 if (h->root.u.def.value != vlook
3975 || h->root.u.def.section != slook)
3976 break;
3977 else if (h != hlook)
3978 {
3979 hlook->weakdef = h;
3980
3981 /* If the weak definition is in the list of dynamic
3982 symbols, make sure the real definition is put
3983 there as well. */
3984 if (hlook->dynindx != -1 && h->dynindx == -1)
3985 {
3986 if (! _bfd_elf_link_record_dynamic_symbol (info,
3987 h))
3988 goto error_return;
3989 }
3990
3991 /* If the real definition is in the list of dynamic
3992 symbols, make sure the weak definition is put
3993 there as well. If we don't do this, then the
3994 dynamic loader might not merge the entries for the
3995 real definition and the weak definition. */
3996 if (h->dynindx != -1 && hlook->dynindx == -1)
3997 {
3998 if (! _bfd_elf_link_record_dynamic_symbol (info,
3999 hlook))
4000 goto error_return;
4001 }
4002 break;
4003 }
4004 }
4005 }
4006
4007 free (sorted_sym_hash);
4008 }
4009
4010 /* If this object is the same format as the output object, and it is
4011 not a shared library, then let the backend look through the
4012 relocs.
4013
4014 This is required to build global offset table entries and to
4015 arrange for dynamic relocs. It is not required for the
4016 particular common case of linking non PIC code, even when linking
4017 against shared libraries, but unfortunately there is no way of
4018 knowing whether an object file has been compiled PIC or not.
4019 Looking through the relocs is not particularly time consuming.
4020 The problem is that we must either (1) keep the relocs in memory,
4021 which causes the linker to require additional runtime memory or
4022 (2) read the relocs twice from the input file, which wastes time.
4023 This would be a good case for using mmap.
4024
4025 I have no idea how to handle linking PIC code into a file of a
4026 different format. It probably can't be done. */
4027 check_relocs = get_elf_backend_data (abfd)->check_relocs;
4028 if (! dynamic
4029 && is_elf_hash_table (hash_table)
4030 && hash_table->root.creator == abfd->xvec
4031 && check_relocs != NULL)
4032 {
4033 asection *o;
4034
4035 for (o = abfd->sections; o != NULL; o = o->next)
4036 {
4037 Elf_Internal_Rela *internal_relocs;
4038 bfd_boolean ok;
4039
4040 if ((o->flags & SEC_RELOC) == 0
4041 || o->reloc_count == 0
4042 || ((info->strip == strip_all || info->strip == strip_debugger)
4043 && (o->flags & SEC_DEBUGGING) != 0)
4044 || bfd_is_abs_section (o->output_section))
4045 continue;
4046
4047 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4048 info->keep_memory);
4049 if (internal_relocs == NULL)
4050 goto error_return;
4051
4052 ok = (*check_relocs) (abfd, info, o, internal_relocs);
4053
4054 if (elf_section_data (o)->relocs != internal_relocs)
4055 free (internal_relocs);
4056
4057 if (! ok)
4058 goto error_return;
4059 }
4060 }
4061
4062 /* If this is a non-traditional link, try to optimize the handling
4063 of the .stab/.stabstr sections. */
4064 if (! dynamic
4065 && ! info->traditional_format
4066 && is_elf_hash_table (hash_table)
4067 && (info->strip != strip_all && info->strip != strip_debugger))
4068 {
4069 asection *stabstr;
4070
4071 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4072 if (stabstr != NULL)
4073 {
4074 bfd_size_type string_offset = 0;
4075 asection *stab;
4076
4077 for (stab = abfd->sections; stab; stab = stab->next)
4078 if (strncmp (".stab", stab->name, 5) == 0
4079 && (!stab->name[5] ||
4080 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4081 && (stab->flags & SEC_MERGE) == 0
4082 && !bfd_is_abs_section (stab->output_section))
4083 {
4084 struct bfd_elf_section_data *secdata;
4085
4086 secdata = elf_section_data (stab);
4087 if (! _bfd_link_section_stabs (abfd,
4088 & hash_table->stab_info,
4089 stab, stabstr,
4090 &secdata->sec_info,
4091 &string_offset))
4092 goto error_return;
4093 if (secdata->sec_info)
4094 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4095 }
4096 }
4097 }
4098
4099 if (! info->relocatable
4100 && ! dynamic
4101 && is_elf_hash_table (hash_table))
4102 {
4103 asection *s;
4104
4105 for (s = abfd->sections; s != NULL; s = s->next)
4106 if ((s->flags & SEC_MERGE) != 0
4107 && !bfd_is_abs_section (s->output_section))
4108 {
4109 struct bfd_elf_section_data *secdata;
4110
4111 secdata = elf_section_data (s);
4112 if (! _bfd_merge_section (abfd,
4113 & hash_table->merge_info,
4114 s, &secdata->sec_info))
4115 goto error_return;
4116 else if (secdata->sec_info)
4117 s->sec_info_type = ELF_INFO_TYPE_MERGE;
4118 }
4119 }
4120
4121 if (is_elf_hash_table (hash_table))
4122 {
4123 /* Add this bfd to the loaded list. */
4124 struct elf_link_loaded_list *n;
4125
4126 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4127 if (n == NULL)
4128 goto error_return;
4129 n->abfd = abfd;
4130 n->next = hash_table->loaded;
4131 hash_table->loaded = n;
4132 }
4133
4134 return TRUE;
4135
4136 error_free_vers:
4137 if (nondeflt_vers != NULL)
4138 free (nondeflt_vers);
4139 if (extversym != NULL)
4140 free (extversym);
4141 error_free_sym:
4142 if (isymbuf != NULL)
4143 free (isymbuf);
4144 error_return:
4145 return FALSE;
4146 }
4147
4148 /* Add symbols from an ELF archive file to the linker hash table. We
4149 don't use _bfd_generic_link_add_archive_symbols because of a
4150 problem which arises on UnixWare. The UnixWare libc.so is an
4151 archive which includes an entry libc.so.1 which defines a bunch of
4152 symbols. The libc.so archive also includes a number of other
4153 object files, which also define symbols, some of which are the same
4154 as those defined in libc.so.1. Correct linking requires that we
4155 consider each object file in turn, and include it if it defines any
4156 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4157 this; it looks through the list of undefined symbols, and includes
4158 any object file which defines them. When this algorithm is used on
4159 UnixWare, it winds up pulling in libc.so.1 early and defining a
4160 bunch of symbols. This means that some of the other objects in the
4161 archive are not included in the link, which is incorrect since they
4162 precede libc.so.1 in the archive.
4163
4164 Fortunately, ELF archive handling is simpler than that done by
4165 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4166 oddities. In ELF, if we find a symbol in the archive map, and the
4167 symbol is currently undefined, we know that we must pull in that
4168 object file.
4169
4170 Unfortunately, we do have to make multiple passes over the symbol
4171 table until nothing further is resolved. */
4172
4173 static bfd_boolean
4174 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4175 {
4176 symindex c;
4177 bfd_boolean *defined = NULL;
4178 bfd_boolean *included = NULL;
4179 carsym *symdefs;
4180 bfd_boolean loop;
4181 bfd_size_type amt;
4182
4183 if (! bfd_has_map (abfd))
4184 {
4185 /* An empty archive is a special case. */
4186 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4187 return TRUE;
4188 bfd_set_error (bfd_error_no_armap);
4189 return FALSE;
4190 }
4191
4192 /* Keep track of all symbols we know to be already defined, and all
4193 files we know to be already included. This is to speed up the
4194 second and subsequent passes. */
4195 c = bfd_ardata (abfd)->symdef_count;
4196 if (c == 0)
4197 return TRUE;
4198 amt = c;
4199 amt *= sizeof (bfd_boolean);
4200 defined = bfd_zmalloc (amt);
4201 included = bfd_zmalloc (amt);
4202 if (defined == NULL || included == NULL)
4203 goto error_return;
4204
4205 symdefs = bfd_ardata (abfd)->symdefs;
4206
4207 do
4208 {
4209 file_ptr last;
4210 symindex i;
4211 carsym *symdef;
4212 carsym *symdefend;
4213
4214 loop = FALSE;
4215 last = -1;
4216
4217 symdef = symdefs;
4218 symdefend = symdef + c;
4219 for (i = 0; symdef < symdefend; symdef++, i++)
4220 {
4221 struct elf_link_hash_entry *h;
4222 bfd *element;
4223 struct bfd_link_hash_entry *undefs_tail;
4224 symindex mark;
4225
4226 if (defined[i] || included[i])
4227 continue;
4228 if (symdef->file_offset == last)
4229 {
4230 included[i] = TRUE;
4231 continue;
4232 }
4233
4234 h = elf_link_hash_lookup (elf_hash_table (info), symdef->name,
4235 FALSE, FALSE, FALSE);
4236
4237 if (h == NULL)
4238 {
4239 char *p, *copy;
4240 size_t len, first;
4241
4242 /* If this is a default version (the name contains @@),
4243 look up the symbol again with only one `@' as well
4244 as without the version. The effect is that references
4245 to the symbol with and without the version will be
4246 matched by the default symbol in the archive. */
4247
4248 p = strchr (symdef->name, ELF_VER_CHR);
4249 if (p == NULL || p[1] != ELF_VER_CHR)
4250 continue;
4251
4252 /* First check with only one `@'. */
4253 len = strlen (symdef->name);
4254 copy = bfd_alloc (abfd, len);
4255 if (copy == NULL)
4256 goto error_return;
4257 first = p - symdef->name + 1;
4258 memcpy (copy, symdef->name, first);
4259 memcpy (copy + first, symdef->name + first + 1, len - first);
4260
4261 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4262 FALSE, FALSE, FALSE);
4263
4264 if (h == NULL)
4265 {
4266 /* We also need to check references to the symbol
4267 without the version. */
4268
4269 copy[first - 1] = '\0';
4270 h = elf_link_hash_lookup (elf_hash_table (info),
4271 copy, FALSE, FALSE, FALSE);
4272 }
4273
4274 bfd_release (abfd, copy);
4275 }
4276
4277 if (h == NULL)
4278 continue;
4279
4280 if (h->root.type == bfd_link_hash_common)
4281 {
4282 /* We currently have a common symbol. The archive map contains
4283 a reference to this symbol, so we may want to include it. We
4284 only want to include it however, if this archive element
4285 contains a definition of the symbol, not just another common
4286 declaration of it.
4287
4288 Unfortunately some archivers (including GNU ar) will put
4289 declarations of common symbols into their archive maps, as
4290 well as real definitions, so we cannot just go by the archive
4291 map alone. Instead we must read in the element's symbol
4292 table and check that to see what kind of symbol definition
4293 this is. */
4294 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4295 continue;
4296 }
4297 else if (h->root.type != bfd_link_hash_undefined)
4298 {
4299 if (h->root.type != bfd_link_hash_undefweak)
4300 defined[i] = TRUE;
4301 continue;
4302 }
4303
4304 /* We need to include this archive member. */
4305 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4306 if (element == NULL)
4307 goto error_return;
4308
4309 if (! bfd_check_format (element, bfd_object))
4310 goto error_return;
4311
4312 /* Doublecheck that we have not included this object
4313 already--it should be impossible, but there may be
4314 something wrong with the archive. */
4315 if (element->archive_pass != 0)
4316 {
4317 bfd_set_error (bfd_error_bad_value);
4318 goto error_return;
4319 }
4320 element->archive_pass = 1;
4321
4322 undefs_tail = info->hash->undefs_tail;
4323
4324 if (! (*info->callbacks->add_archive_element) (info, element,
4325 symdef->name))
4326 goto error_return;
4327 if (! bfd_link_add_symbols (element, info))
4328 goto error_return;
4329
4330 /* If there are any new undefined symbols, we need to make
4331 another pass through the archive in order to see whether
4332 they can be defined. FIXME: This isn't perfect, because
4333 common symbols wind up on undefs_tail and because an
4334 undefined symbol which is defined later on in this pass
4335 does not require another pass. This isn't a bug, but it
4336 does make the code less efficient than it could be. */
4337 if (undefs_tail != info->hash->undefs_tail)
4338 loop = TRUE;
4339
4340 /* Look backward to mark all symbols from this object file
4341 which we have already seen in this pass. */
4342 mark = i;
4343 do
4344 {
4345 included[mark] = TRUE;
4346 if (mark == 0)
4347 break;
4348 --mark;
4349 }
4350 while (symdefs[mark].file_offset == symdef->file_offset);
4351
4352 /* We mark subsequent symbols from this object file as we go
4353 on through the loop. */
4354 last = symdef->file_offset;
4355 }
4356 }
4357 while (loop);
4358
4359 free (defined);
4360 free (included);
4361
4362 return TRUE;
4363
4364 error_return:
4365 if (defined != NULL)
4366 free (defined);
4367 if (included != NULL)
4368 free (included);
4369 return FALSE;
4370 }
4371
4372 /* Given an ELF BFD, add symbols to the global hash table as
4373 appropriate. */
4374
4375 bfd_boolean
4376 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
4377 {
4378 switch (bfd_get_format (abfd))
4379 {
4380 case bfd_object:
4381 return elf_link_add_object_symbols (abfd, info);
4382 case bfd_archive:
4383 return elf_link_add_archive_symbols (abfd, info);
4384 default:
4385 bfd_set_error (bfd_error_wrong_format);
4386 return FALSE;
4387 }
4388 }
4389 \f
4390 /* This function will be called though elf_link_hash_traverse to store
4391 all hash value of the exported symbols in an array. */
4392
4393 static bfd_boolean
4394 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
4395 {
4396 unsigned long **valuep = data;
4397 const char *name;
4398 char *p;
4399 unsigned long ha;
4400 char *alc = NULL;
4401
4402 if (h->root.type == bfd_link_hash_warning)
4403 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4404
4405 /* Ignore indirect symbols. These are added by the versioning code. */
4406 if (h->dynindx == -1)
4407 return TRUE;
4408
4409 name = h->root.root.string;
4410 p = strchr (name, ELF_VER_CHR);
4411 if (p != NULL)
4412 {
4413 alc = bfd_malloc (p - name + 1);
4414 memcpy (alc, name, p - name);
4415 alc[p - name] = '\0';
4416 name = alc;
4417 }
4418
4419 /* Compute the hash value. */
4420 ha = bfd_elf_hash (name);
4421
4422 /* Store the found hash value in the array given as the argument. */
4423 *(*valuep)++ = ha;
4424
4425 /* And store it in the struct so that we can put it in the hash table
4426 later. */
4427 h->elf_hash_value = ha;
4428
4429 if (alc != NULL)
4430 free (alc);
4431
4432 return TRUE;
4433 }
4434
4435 /* Array used to determine the number of hash table buckets to use
4436 based on the number of symbols there are. If there are fewer than
4437 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4438 fewer than 37 we use 17 buckets, and so forth. We never use more
4439 than 32771 buckets. */
4440
4441 static const size_t elf_buckets[] =
4442 {
4443 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4444 16411, 32771, 0
4445 };
4446
4447 /* Compute bucket count for hashing table. We do not use a static set
4448 of possible tables sizes anymore. Instead we determine for all
4449 possible reasonable sizes of the table the outcome (i.e., the
4450 number of collisions etc) and choose the best solution. The
4451 weighting functions are not too simple to allow the table to grow
4452 without bounds. Instead one of the weighting factors is the size.
4453 Therefore the result is always a good payoff between few collisions
4454 (= short chain lengths) and table size. */
4455 static size_t
4456 compute_bucket_count (struct bfd_link_info *info)
4457 {
4458 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
4459 size_t best_size = 0;
4460 unsigned long int *hashcodes;
4461 unsigned long int *hashcodesp;
4462 unsigned long int i;
4463 bfd_size_type amt;
4464
4465 /* Compute the hash values for all exported symbols. At the same
4466 time store the values in an array so that we could use them for
4467 optimizations. */
4468 amt = dynsymcount;
4469 amt *= sizeof (unsigned long int);
4470 hashcodes = bfd_malloc (amt);
4471 if (hashcodes == NULL)
4472 return 0;
4473 hashcodesp = hashcodes;
4474
4475 /* Put all hash values in HASHCODES. */
4476 elf_link_hash_traverse (elf_hash_table (info),
4477 elf_collect_hash_codes, &hashcodesp);
4478
4479 /* We have a problem here. The following code to optimize the table
4480 size requires an integer type with more the 32 bits. If
4481 BFD_HOST_U_64_BIT is set we know about such a type. */
4482 #ifdef BFD_HOST_U_64_BIT
4483 if (info->optimize)
4484 {
4485 unsigned long int nsyms = hashcodesp - hashcodes;
4486 size_t minsize;
4487 size_t maxsize;
4488 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
4489 unsigned long int *counts ;
4490 bfd *dynobj = elf_hash_table (info)->dynobj;
4491 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
4492
4493 /* Possible optimization parameters: if we have NSYMS symbols we say
4494 that the hashing table must at least have NSYMS/4 and at most
4495 2*NSYMS buckets. */
4496 minsize = nsyms / 4;
4497 if (minsize == 0)
4498 minsize = 1;
4499 best_size = maxsize = nsyms * 2;
4500
4501 /* Create array where we count the collisions in. We must use bfd_malloc
4502 since the size could be large. */
4503 amt = maxsize;
4504 amt *= sizeof (unsigned long int);
4505 counts = bfd_malloc (amt);
4506 if (counts == NULL)
4507 {
4508 free (hashcodes);
4509 return 0;
4510 }
4511
4512 /* Compute the "optimal" size for the hash table. The criteria is a
4513 minimal chain length. The minor criteria is (of course) the size
4514 of the table. */
4515 for (i = minsize; i < maxsize; ++i)
4516 {
4517 /* Walk through the array of hashcodes and count the collisions. */
4518 BFD_HOST_U_64_BIT max;
4519 unsigned long int j;
4520 unsigned long int fact;
4521
4522 memset (counts, '\0', i * sizeof (unsigned long int));
4523
4524 /* Determine how often each hash bucket is used. */
4525 for (j = 0; j < nsyms; ++j)
4526 ++counts[hashcodes[j] % i];
4527
4528 /* For the weight function we need some information about the
4529 pagesize on the target. This is information need not be 100%
4530 accurate. Since this information is not available (so far) we
4531 define it here to a reasonable default value. If it is crucial
4532 to have a better value some day simply define this value. */
4533 # ifndef BFD_TARGET_PAGESIZE
4534 # define BFD_TARGET_PAGESIZE (4096)
4535 # endif
4536
4537 /* We in any case need 2 + NSYMS entries for the size values and
4538 the chains. */
4539 max = (2 + nsyms) * (bed->s->arch_size / 8);
4540
4541 # if 1
4542 /* Variant 1: optimize for short chains. We add the squares
4543 of all the chain lengths (which favors many small chain
4544 over a few long chains). */
4545 for (j = 0; j < i; ++j)
4546 max += counts[j] * counts[j];
4547
4548 /* This adds penalties for the overall size of the table. */
4549 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4550 max *= fact * fact;
4551 # else
4552 /* Variant 2: Optimize a lot more for small table. Here we
4553 also add squares of the size but we also add penalties for
4554 empty slots (the +1 term). */
4555 for (j = 0; j < i; ++j)
4556 max += (1 + counts[j]) * (1 + counts[j]);
4557
4558 /* The overall size of the table is considered, but not as
4559 strong as in variant 1, where it is squared. */
4560 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4561 max *= fact;
4562 # endif
4563
4564 /* Compare with current best results. */
4565 if (max < best_chlen)
4566 {
4567 best_chlen = max;
4568 best_size = i;
4569 }
4570 }
4571
4572 free (counts);
4573 }
4574 else
4575 #endif /* defined (BFD_HOST_U_64_BIT) */
4576 {
4577 /* This is the fallback solution if no 64bit type is available or if we
4578 are not supposed to spend much time on optimizations. We select the
4579 bucket count using a fixed set of numbers. */
4580 for (i = 0; elf_buckets[i] != 0; i++)
4581 {
4582 best_size = elf_buckets[i];
4583 if (dynsymcount < elf_buckets[i + 1])
4584 break;
4585 }
4586 }
4587
4588 /* Free the arrays we needed. */
4589 free (hashcodes);
4590
4591 return best_size;
4592 }
4593
4594 /* Set up the sizes and contents of the ELF dynamic sections. This is
4595 called by the ELF linker emulation before_allocation routine. We
4596 must set the sizes of the sections before the linker sets the
4597 addresses of the various sections. */
4598
4599 bfd_boolean
4600 bfd_elf_size_dynamic_sections (bfd *output_bfd,
4601 const char *soname,
4602 const char *rpath,
4603 const char *filter_shlib,
4604 const char * const *auxiliary_filters,
4605 struct bfd_link_info *info,
4606 asection **sinterpptr,
4607 struct bfd_elf_version_tree *verdefs)
4608 {
4609 bfd_size_type soname_indx;
4610 bfd *dynobj;
4611 const struct elf_backend_data *bed;
4612 struct elf_assign_sym_version_info asvinfo;
4613
4614 *sinterpptr = NULL;
4615
4616 soname_indx = (bfd_size_type) -1;
4617
4618 if (!is_elf_hash_table (info->hash))
4619 return TRUE;
4620
4621 if (info->execstack)
4622 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
4623 else if (info->noexecstack)
4624 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
4625 else
4626 {
4627 bfd *inputobj;
4628 asection *notesec = NULL;
4629 int exec = 0;
4630
4631 for (inputobj = info->input_bfds;
4632 inputobj;
4633 inputobj = inputobj->link_next)
4634 {
4635 asection *s;
4636
4637 if (inputobj->flags & DYNAMIC)
4638 continue;
4639 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
4640 if (s)
4641 {
4642 if (s->flags & SEC_CODE)
4643 exec = PF_X;
4644 notesec = s;
4645 }
4646 else
4647 exec = PF_X;
4648 }
4649 if (notesec)
4650 {
4651 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
4652 if (exec && info->relocatable
4653 && notesec->output_section != bfd_abs_section_ptr)
4654 notesec->output_section->flags |= SEC_CODE;
4655 }
4656 }
4657
4658 /* Any syms created from now on start with -1 in
4659 got.refcount/offset and plt.refcount/offset. */
4660 elf_hash_table (info)->init_refcount = elf_hash_table (info)->init_offset;
4661
4662 /* The backend may have to create some sections regardless of whether
4663 we're dynamic or not. */
4664 bed = get_elf_backend_data (output_bfd);
4665 if (bed->elf_backend_always_size_sections
4666 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
4667 return FALSE;
4668
4669 dynobj = elf_hash_table (info)->dynobj;
4670
4671 /* If there were no dynamic objects in the link, there is nothing to
4672 do here. */
4673 if (dynobj == NULL)
4674 return TRUE;
4675
4676 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
4677 return FALSE;
4678
4679 if (elf_hash_table (info)->dynamic_sections_created)
4680 {
4681 struct elf_info_failed eif;
4682 struct elf_link_hash_entry *h;
4683 asection *dynstr;
4684 struct bfd_elf_version_tree *t;
4685 struct bfd_elf_version_expr *d;
4686 bfd_boolean all_defined;
4687
4688 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
4689 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
4690
4691 if (soname != NULL)
4692 {
4693 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4694 soname, TRUE);
4695 if (soname_indx == (bfd_size_type) -1
4696 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
4697 return FALSE;
4698 }
4699
4700 if (info->symbolic)
4701 {
4702 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
4703 return FALSE;
4704 info->flags |= DF_SYMBOLIC;
4705 }
4706
4707 if (rpath != NULL)
4708 {
4709 bfd_size_type indx;
4710
4711 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
4712 TRUE);
4713 if (indx == (bfd_size_type) -1
4714 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
4715 return FALSE;
4716
4717 if (info->new_dtags)
4718 {
4719 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
4720 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
4721 return FALSE;
4722 }
4723 }
4724
4725 if (filter_shlib != NULL)
4726 {
4727 bfd_size_type indx;
4728
4729 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4730 filter_shlib, TRUE);
4731 if (indx == (bfd_size_type) -1
4732 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
4733 return FALSE;
4734 }
4735
4736 if (auxiliary_filters != NULL)
4737 {
4738 const char * const *p;
4739
4740 for (p = auxiliary_filters; *p != NULL; p++)
4741 {
4742 bfd_size_type indx;
4743
4744 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
4745 *p, TRUE);
4746 if (indx == (bfd_size_type) -1
4747 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
4748 return FALSE;
4749 }
4750 }
4751
4752 eif.info = info;
4753 eif.verdefs = verdefs;
4754 eif.failed = FALSE;
4755
4756 /* If we are supposed to export all symbols into the dynamic symbol
4757 table (this is not the normal case), then do so. */
4758 if (info->export_dynamic)
4759 {
4760 elf_link_hash_traverse (elf_hash_table (info),
4761 _bfd_elf_export_symbol,
4762 &eif);
4763 if (eif.failed)
4764 return FALSE;
4765 }
4766
4767 /* Make all global versions with definition. */
4768 for (t = verdefs; t != NULL; t = t->next)
4769 for (d = t->globals.list; d != NULL; d = d->next)
4770 if (!d->symver && d->symbol)
4771 {
4772 const char *verstr, *name;
4773 size_t namelen, verlen, newlen;
4774 char *newname, *p;
4775 struct elf_link_hash_entry *newh;
4776
4777 name = d->symbol;
4778 namelen = strlen (name);
4779 verstr = t->name;
4780 verlen = strlen (verstr);
4781 newlen = namelen + verlen + 3;
4782
4783 newname = bfd_malloc (newlen);
4784 if (newname == NULL)
4785 return FALSE;
4786 memcpy (newname, name, namelen);
4787
4788 /* Check the hidden versioned definition. */
4789 p = newname + namelen;
4790 *p++ = ELF_VER_CHR;
4791 memcpy (p, verstr, verlen + 1);
4792 newh = elf_link_hash_lookup (elf_hash_table (info),
4793 newname, FALSE, FALSE,
4794 FALSE);
4795 if (newh == NULL
4796 || (newh->root.type != bfd_link_hash_defined
4797 && newh->root.type != bfd_link_hash_defweak))
4798 {
4799 /* Check the default versioned definition. */
4800 *p++ = ELF_VER_CHR;
4801 memcpy (p, verstr, verlen + 1);
4802 newh = elf_link_hash_lookup (elf_hash_table (info),
4803 newname, FALSE, FALSE,
4804 FALSE);
4805 }
4806 free (newname);
4807
4808 /* Mark this version if there is a definition and it is
4809 not defined in a shared object. */
4810 if (newh != NULL
4811 && ((newh->elf_link_hash_flags
4812 & ELF_LINK_HASH_DEF_DYNAMIC) == 0)
4813 && (newh->root.type == bfd_link_hash_defined
4814 || newh->root.type == bfd_link_hash_defweak))
4815 d->symver = 1;
4816 }
4817
4818 /* Attach all the symbols to their version information. */
4819 asvinfo.output_bfd = output_bfd;
4820 asvinfo.info = info;
4821 asvinfo.verdefs = verdefs;
4822 asvinfo.failed = FALSE;
4823
4824 elf_link_hash_traverse (elf_hash_table (info),
4825 _bfd_elf_link_assign_sym_version,
4826 &asvinfo);
4827 if (asvinfo.failed)
4828 return FALSE;
4829
4830 if (!info->allow_undefined_version)
4831 {
4832 /* Check if all global versions have a definition. */
4833 all_defined = TRUE;
4834 for (t = verdefs; t != NULL; t = t->next)
4835 for (d = t->globals.list; d != NULL; d = d->next)
4836 if (!d->symver && !d->script)
4837 {
4838 (*_bfd_error_handler)
4839 (_("%s: undefined version: %s"),
4840 d->pattern, t->name);
4841 all_defined = FALSE;
4842 }
4843
4844 if (!all_defined)
4845 {
4846 bfd_set_error (bfd_error_bad_value);
4847 return FALSE;
4848 }
4849 }
4850
4851 /* Find all symbols which were defined in a dynamic object and make
4852 the backend pick a reasonable value for them. */
4853 elf_link_hash_traverse (elf_hash_table (info),
4854 _bfd_elf_adjust_dynamic_symbol,
4855 &eif);
4856 if (eif.failed)
4857 return FALSE;
4858
4859 /* Add some entries to the .dynamic section. We fill in some of the
4860 values later, in elf_bfd_final_link, but we must add the entries
4861 now so that we know the final size of the .dynamic section. */
4862
4863 /* If there are initialization and/or finalization functions to
4864 call then add the corresponding DT_INIT/DT_FINI entries. */
4865 h = (info->init_function
4866 ? elf_link_hash_lookup (elf_hash_table (info),
4867 info->init_function, FALSE,
4868 FALSE, FALSE)
4869 : NULL);
4870 if (h != NULL
4871 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
4872 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
4873 {
4874 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
4875 return FALSE;
4876 }
4877 h = (info->fini_function
4878 ? elf_link_hash_lookup (elf_hash_table (info),
4879 info->fini_function, FALSE,
4880 FALSE, FALSE)
4881 : NULL);
4882 if (h != NULL
4883 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
4884 | ELF_LINK_HASH_DEF_REGULAR)) != 0)
4885 {
4886 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
4887 return FALSE;
4888 }
4889
4890 if (bfd_get_section_by_name (output_bfd, ".preinit_array") != NULL)
4891 {
4892 /* DT_PREINIT_ARRAY is not allowed in shared library. */
4893 if (! info->executable)
4894 {
4895 bfd *sub;
4896 asection *o;
4897
4898 for (sub = info->input_bfds; sub != NULL;
4899 sub = sub->link_next)
4900 for (o = sub->sections; o != NULL; o = o->next)
4901 if (elf_section_data (o)->this_hdr.sh_type
4902 == SHT_PREINIT_ARRAY)
4903 {
4904 (*_bfd_error_handler)
4905 (_("%s: .preinit_array section is not allowed in DSO"),
4906 bfd_archive_filename (sub));
4907 break;
4908 }
4909
4910 bfd_set_error (bfd_error_nonrepresentable_section);
4911 return FALSE;
4912 }
4913
4914 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
4915 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
4916 return FALSE;
4917 }
4918 if (bfd_get_section_by_name (output_bfd, ".init_array") != NULL)
4919 {
4920 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
4921 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
4922 return FALSE;
4923 }
4924 if (bfd_get_section_by_name (output_bfd, ".fini_array") != NULL)
4925 {
4926 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
4927 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
4928 return FALSE;
4929 }
4930
4931 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
4932 /* If .dynstr is excluded from the link, we don't want any of
4933 these tags. Strictly, we should be checking each section
4934 individually; This quick check covers for the case where
4935 someone does a /DISCARD/ : { *(*) }. */
4936 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
4937 {
4938 bfd_size_type strsize;
4939
4940 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
4941 if (!_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)
4942 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
4943 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
4944 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
4945 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
4946 bed->s->sizeof_sym))
4947 return FALSE;
4948 }
4949 }
4950
4951 /* The backend must work out the sizes of all the other dynamic
4952 sections. */
4953 if (bed->elf_backend_size_dynamic_sections
4954 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
4955 return FALSE;
4956
4957 if (elf_hash_table (info)->dynamic_sections_created)
4958 {
4959 bfd_size_type dynsymcount;
4960 asection *s;
4961 size_t bucketcount = 0;
4962 size_t hash_entry_size;
4963 unsigned int dtagcount;
4964
4965 /* Set up the version definition section. */
4966 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
4967 BFD_ASSERT (s != NULL);
4968
4969 /* We may have created additional version definitions if we are
4970 just linking a regular application. */
4971 verdefs = asvinfo.verdefs;
4972
4973 /* Skip anonymous version tag. */
4974 if (verdefs != NULL && verdefs->vernum == 0)
4975 verdefs = verdefs->next;
4976
4977 if (verdefs == NULL)
4978 _bfd_strip_section_from_output (info, s);
4979 else
4980 {
4981 unsigned int cdefs;
4982 bfd_size_type size;
4983 struct bfd_elf_version_tree *t;
4984 bfd_byte *p;
4985 Elf_Internal_Verdef def;
4986 Elf_Internal_Verdaux defaux;
4987
4988 cdefs = 0;
4989 size = 0;
4990
4991 /* Make space for the base version. */
4992 size += sizeof (Elf_External_Verdef);
4993 size += sizeof (Elf_External_Verdaux);
4994 ++cdefs;
4995
4996 for (t = verdefs; t != NULL; t = t->next)
4997 {
4998 struct bfd_elf_version_deps *n;
4999
5000 size += sizeof (Elf_External_Verdef);
5001 size += sizeof (Elf_External_Verdaux);
5002 ++cdefs;
5003
5004 for (n = t->deps; n != NULL; n = n->next)
5005 size += sizeof (Elf_External_Verdaux);
5006 }
5007
5008 s->_raw_size = size;
5009 s->contents = bfd_alloc (output_bfd, s->_raw_size);
5010 if (s->contents == NULL && s->_raw_size != 0)
5011 return FALSE;
5012
5013 /* Fill in the version definition section. */
5014
5015 p = s->contents;
5016
5017 def.vd_version = VER_DEF_CURRENT;
5018 def.vd_flags = VER_FLG_BASE;
5019 def.vd_ndx = 1;
5020 def.vd_cnt = 1;
5021 def.vd_aux = sizeof (Elf_External_Verdef);
5022 def.vd_next = (sizeof (Elf_External_Verdef)
5023 + sizeof (Elf_External_Verdaux));
5024
5025 if (soname_indx != (bfd_size_type) -1)
5026 {
5027 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5028 soname_indx);
5029 def.vd_hash = bfd_elf_hash (soname);
5030 defaux.vda_name = soname_indx;
5031 }
5032 else
5033 {
5034 const char *name;
5035 bfd_size_type indx;
5036
5037 name = basename (output_bfd->filename);
5038 def.vd_hash = bfd_elf_hash (name);
5039 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5040 name, FALSE);
5041 if (indx == (bfd_size_type) -1)
5042 return FALSE;
5043 defaux.vda_name = indx;
5044 }
5045 defaux.vda_next = 0;
5046
5047 _bfd_elf_swap_verdef_out (output_bfd, &def,
5048 (Elf_External_Verdef *) p);
5049 p += sizeof (Elf_External_Verdef);
5050 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5051 (Elf_External_Verdaux *) p);
5052 p += sizeof (Elf_External_Verdaux);
5053
5054 for (t = verdefs; t != NULL; t = t->next)
5055 {
5056 unsigned int cdeps;
5057 struct bfd_elf_version_deps *n;
5058 struct elf_link_hash_entry *h;
5059 struct bfd_link_hash_entry *bh;
5060
5061 cdeps = 0;
5062 for (n = t->deps; n != NULL; n = n->next)
5063 ++cdeps;
5064
5065 /* Add a symbol representing this version. */
5066 bh = NULL;
5067 if (! (_bfd_generic_link_add_one_symbol
5068 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5069 0, NULL, FALSE,
5070 get_elf_backend_data (dynobj)->collect, &bh)))
5071 return FALSE;
5072 h = (struct elf_link_hash_entry *) bh;
5073 h->elf_link_hash_flags &= ~ ELF_LINK_NON_ELF;
5074 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5075 h->type = STT_OBJECT;
5076 h->verinfo.vertree = t;
5077
5078 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
5079 return FALSE;
5080
5081 def.vd_version = VER_DEF_CURRENT;
5082 def.vd_flags = 0;
5083 if (t->globals.list == NULL
5084 && t->locals.list == NULL
5085 && ! t->used)
5086 def.vd_flags |= VER_FLG_WEAK;
5087 def.vd_ndx = t->vernum + 1;
5088 def.vd_cnt = cdeps + 1;
5089 def.vd_hash = bfd_elf_hash (t->name);
5090 def.vd_aux = sizeof (Elf_External_Verdef);
5091 def.vd_next = 0;
5092 if (t->next != NULL)
5093 def.vd_next = (sizeof (Elf_External_Verdef)
5094 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5095
5096 _bfd_elf_swap_verdef_out (output_bfd, &def,
5097 (Elf_External_Verdef *) p);
5098 p += sizeof (Elf_External_Verdef);
5099
5100 defaux.vda_name = h->dynstr_index;
5101 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5102 h->dynstr_index);
5103 defaux.vda_next = 0;
5104 if (t->deps != NULL)
5105 defaux.vda_next = sizeof (Elf_External_Verdaux);
5106 t->name_indx = defaux.vda_name;
5107
5108 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5109 (Elf_External_Verdaux *) p);
5110 p += sizeof (Elf_External_Verdaux);
5111
5112 for (n = t->deps; n != NULL; n = n->next)
5113 {
5114 if (n->version_needed == NULL)
5115 {
5116 /* This can happen if there was an error in the
5117 version script. */
5118 defaux.vda_name = 0;
5119 }
5120 else
5121 {
5122 defaux.vda_name = n->version_needed->name_indx;
5123 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5124 defaux.vda_name);
5125 }
5126 if (n->next == NULL)
5127 defaux.vda_next = 0;
5128 else
5129 defaux.vda_next = sizeof (Elf_External_Verdaux);
5130
5131 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5132 (Elf_External_Verdaux *) p);
5133 p += sizeof (Elf_External_Verdaux);
5134 }
5135 }
5136
5137 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
5138 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
5139 return FALSE;
5140
5141 elf_tdata (output_bfd)->cverdefs = cdefs;
5142 }
5143
5144 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
5145 {
5146 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
5147 return FALSE;
5148 }
5149 else if (info->flags & DF_BIND_NOW)
5150 {
5151 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
5152 return FALSE;
5153 }
5154
5155 if (info->flags_1)
5156 {
5157 if (info->executable)
5158 info->flags_1 &= ~ (DF_1_INITFIRST
5159 | DF_1_NODELETE
5160 | DF_1_NOOPEN);
5161 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
5162 return FALSE;
5163 }
5164
5165 /* Work out the size of the version reference section. */
5166
5167 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
5168 BFD_ASSERT (s != NULL);
5169 {
5170 struct elf_find_verdep_info sinfo;
5171
5172 sinfo.output_bfd = output_bfd;
5173 sinfo.info = info;
5174 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
5175 if (sinfo.vers == 0)
5176 sinfo.vers = 1;
5177 sinfo.failed = FALSE;
5178
5179 elf_link_hash_traverse (elf_hash_table (info),
5180 _bfd_elf_link_find_version_dependencies,
5181 &sinfo);
5182
5183 if (elf_tdata (output_bfd)->verref == NULL)
5184 _bfd_strip_section_from_output (info, s);
5185 else
5186 {
5187 Elf_Internal_Verneed *t;
5188 unsigned int size;
5189 unsigned int crefs;
5190 bfd_byte *p;
5191
5192 /* Build the version definition section. */
5193 size = 0;
5194 crefs = 0;
5195 for (t = elf_tdata (output_bfd)->verref;
5196 t != NULL;
5197 t = t->vn_nextref)
5198 {
5199 Elf_Internal_Vernaux *a;
5200
5201 size += sizeof (Elf_External_Verneed);
5202 ++crefs;
5203 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5204 size += sizeof (Elf_External_Vernaux);
5205 }
5206
5207 s->_raw_size = size;
5208 s->contents = bfd_alloc (output_bfd, s->_raw_size);
5209 if (s->contents == NULL)
5210 return FALSE;
5211
5212 p = s->contents;
5213 for (t = elf_tdata (output_bfd)->verref;
5214 t != NULL;
5215 t = t->vn_nextref)
5216 {
5217 unsigned int caux;
5218 Elf_Internal_Vernaux *a;
5219 bfd_size_type indx;
5220
5221 caux = 0;
5222 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5223 ++caux;
5224
5225 t->vn_version = VER_NEED_CURRENT;
5226 t->vn_cnt = caux;
5227 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5228 elf_dt_name (t->vn_bfd) != NULL
5229 ? elf_dt_name (t->vn_bfd)
5230 : basename (t->vn_bfd->filename),
5231 FALSE);
5232 if (indx == (bfd_size_type) -1)
5233 return FALSE;
5234 t->vn_file = indx;
5235 t->vn_aux = sizeof (Elf_External_Verneed);
5236 if (t->vn_nextref == NULL)
5237 t->vn_next = 0;
5238 else
5239 t->vn_next = (sizeof (Elf_External_Verneed)
5240 + caux * sizeof (Elf_External_Vernaux));
5241
5242 _bfd_elf_swap_verneed_out (output_bfd, t,
5243 (Elf_External_Verneed *) p);
5244 p += sizeof (Elf_External_Verneed);
5245
5246 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5247 {
5248 a->vna_hash = bfd_elf_hash (a->vna_nodename);
5249 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5250 a->vna_nodename, FALSE);
5251 if (indx == (bfd_size_type) -1)
5252 return FALSE;
5253 a->vna_name = indx;
5254 if (a->vna_nextptr == NULL)
5255 a->vna_next = 0;
5256 else
5257 a->vna_next = sizeof (Elf_External_Vernaux);
5258
5259 _bfd_elf_swap_vernaux_out (output_bfd, a,
5260 (Elf_External_Vernaux *) p);
5261 p += sizeof (Elf_External_Vernaux);
5262 }
5263 }
5264
5265 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
5266 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
5267 return FALSE;
5268
5269 elf_tdata (output_bfd)->cverrefs = crefs;
5270 }
5271 }
5272
5273 /* Assign dynsym indicies. In a shared library we generate a
5274 section symbol for each output section, which come first.
5275 Next come all of the back-end allocated local dynamic syms,
5276 followed by the rest of the global symbols. */
5277
5278 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
5279
5280 /* Work out the size of the symbol version section. */
5281 s = bfd_get_section_by_name (dynobj, ".gnu.version");
5282 BFD_ASSERT (s != NULL);
5283 if (dynsymcount == 0
5284 || (verdefs == NULL && elf_tdata (output_bfd)->verref == NULL))
5285 {
5286 _bfd_strip_section_from_output (info, s);
5287 /* The DYNSYMCOUNT might have changed if we were going to
5288 output a dynamic symbol table entry for S. */
5289 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info);
5290 }
5291 else
5292 {
5293 s->_raw_size = dynsymcount * sizeof (Elf_External_Versym);
5294 s->contents = bfd_zalloc (output_bfd, s->_raw_size);
5295 if (s->contents == NULL)
5296 return FALSE;
5297
5298 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
5299 return FALSE;
5300 }
5301
5302 /* Set the size of the .dynsym and .hash sections. We counted
5303 the number of dynamic symbols in elf_link_add_object_symbols.
5304 We will build the contents of .dynsym and .hash when we build
5305 the final symbol table, because until then we do not know the
5306 correct value to give the symbols. We built the .dynstr
5307 section as we went along in elf_link_add_object_symbols. */
5308 s = bfd_get_section_by_name (dynobj, ".dynsym");
5309 BFD_ASSERT (s != NULL);
5310 s->_raw_size = dynsymcount * bed->s->sizeof_sym;
5311 s->contents = bfd_alloc (output_bfd, s->_raw_size);
5312 if (s->contents == NULL && s->_raw_size != 0)
5313 return FALSE;
5314
5315 if (dynsymcount != 0)
5316 {
5317 Elf_Internal_Sym isym;
5318
5319 /* The first entry in .dynsym is a dummy symbol. */
5320 isym.st_value = 0;
5321 isym.st_size = 0;
5322 isym.st_name = 0;
5323 isym.st_info = 0;
5324 isym.st_other = 0;
5325 isym.st_shndx = 0;
5326 bed->s->swap_symbol_out (output_bfd, &isym, s->contents, 0);
5327 }
5328
5329 /* Compute the size of the hashing table. As a side effect this
5330 computes the hash values for all the names we export. */
5331 bucketcount = compute_bucket_count (info);
5332
5333 s = bfd_get_section_by_name (dynobj, ".hash");
5334 BFD_ASSERT (s != NULL);
5335 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
5336 s->_raw_size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
5337 s->contents = bfd_zalloc (output_bfd, s->_raw_size);
5338 if (s->contents == NULL)
5339 return FALSE;
5340
5341 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
5342 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
5343 s->contents + hash_entry_size);
5344
5345 elf_hash_table (info)->bucketcount = bucketcount;
5346
5347 s = bfd_get_section_by_name (dynobj, ".dynstr");
5348 BFD_ASSERT (s != NULL);
5349
5350 elf_finalize_dynstr (output_bfd, info);
5351
5352 s->_raw_size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5353
5354 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
5355 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
5356 return FALSE;
5357 }
5358
5359 return TRUE;
5360 }