2003-09-27 Andrew Cagney <cagney@redhat.com>
[binutils-gdb.git] / bfd / elflink.c
1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
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
28 bfd_boolean
29 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
30 {
31 flagword flags;
32 asection *s;
33 struct elf_link_hash_entry *h;
34 struct bfd_link_hash_entry *bh;
35 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
36 int ptralign;
37
38 /* This function may be called more than once. */
39 s = bfd_get_section_by_name (abfd, ".got");
40 if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
41 return TRUE;
42
43 switch (bed->s->arch_size)
44 {
45 case 32:
46 ptralign = 2;
47 break;
48
49 case 64:
50 ptralign = 3;
51 break;
52
53 default:
54 bfd_set_error (bfd_error_bad_value);
55 return FALSE;
56 }
57
58 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
59 | SEC_LINKER_CREATED);
60
61 s = bfd_make_section (abfd, ".got");
62 if (s == NULL
63 || !bfd_set_section_flags (abfd, s, flags)
64 || !bfd_set_section_alignment (abfd, s, ptralign))
65 return FALSE;
66
67 if (bed->want_got_plt)
68 {
69 s = bfd_make_section (abfd, ".got.plt");
70 if (s == NULL
71 || !bfd_set_section_flags (abfd, s, flags)
72 || !bfd_set_section_alignment (abfd, s, ptralign))
73 return FALSE;
74 }
75
76 if (bed->want_got_sym)
77 {
78 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
79 (or .got.plt) section. We don't do this in the linker script
80 because we don't want to define the symbol if we are not creating
81 a global offset table. */
82 bh = NULL;
83 if (!(_bfd_generic_link_add_one_symbol
84 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
85 bed->got_symbol_offset, NULL, FALSE, bed->collect, &bh)))
86 return FALSE;
87 h = (struct elf_link_hash_entry *) bh;
88 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
89 h->type = STT_OBJECT;
90
91 if (! info->executable
92 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
93 return FALSE;
94
95 elf_hash_table (info)->hgot = h;
96 }
97
98 /* The first bit of the global offset table is the header. */
99 s->_raw_size += bed->got_header_size + bed->got_symbol_offset;
100
101 return TRUE;
102 }
103 \f
104 /* Create some sections which will be filled in with dynamic linking
105 information. ABFD is an input file which requires dynamic sections
106 to be created. The dynamic sections take up virtual memory space
107 when the final executable is run, so we need to create them before
108 addresses are assigned to the output sections. We work out the
109 actual contents and size of these sections later. */
110
111 bfd_boolean
112 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
113 {
114 flagword flags;
115 register asection *s;
116 struct elf_link_hash_entry *h;
117 struct bfd_link_hash_entry *bh;
118 const struct elf_backend_data *bed;
119
120 if (! is_elf_hash_table (info))
121 return FALSE;
122
123 if (elf_hash_table (info)->dynamic_sections_created)
124 return TRUE;
125
126 /* Make sure that all dynamic sections use the same input BFD. */
127 if (elf_hash_table (info)->dynobj == NULL)
128 elf_hash_table (info)->dynobj = abfd;
129 else
130 abfd = elf_hash_table (info)->dynobj;
131
132 /* Note that we set the SEC_IN_MEMORY flag for all of these
133 sections. */
134 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
135 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
136
137 /* A dynamically linked executable has a .interp section, but a
138 shared library does not. */
139 if (info->executable)
140 {
141 s = bfd_make_section (abfd, ".interp");
142 if (s == NULL
143 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
144 return FALSE;
145 }
146
147 if (! info->traditional_format
148 && info->hash->creator->flavour == bfd_target_elf_flavour)
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, *alc;
357 const char *name;
358 bfd_boolean copy;
359 bfd_size_type indx;
360
361 /* XXX: The ABI draft says the linker must turn hidden and
362 internal symbols into STB_LOCAL symbols when producing the
363 DSO. However, if ld.so honors st_other in the dynamic table,
364 this would not be necessary. */
365 switch (ELF_ST_VISIBILITY (h->other))
366 {
367 case STV_INTERNAL:
368 case STV_HIDDEN:
369 if (h->root.type != bfd_link_hash_undefined
370 && h->root.type != bfd_link_hash_undefweak)
371 {
372 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
373 return TRUE;
374 }
375
376 default:
377 break;
378 }
379
380 h->dynindx = elf_hash_table (info)->dynsymcount;
381 ++elf_hash_table (info)->dynsymcount;
382
383 dynstr = elf_hash_table (info)->dynstr;
384 if (dynstr == NULL)
385 {
386 /* Create a strtab to hold the dynamic symbol names. */
387 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
388 if (dynstr == NULL)
389 return FALSE;
390 }
391
392 /* We don't put any version information in the dynamic string
393 table. */
394 name = h->root.root.string;
395 p = strchr (name, ELF_VER_CHR);
396 if (p == NULL)
397 {
398 alc = NULL;
399 copy = FALSE;
400 }
401 else
402 {
403 size_t len = p - name + 1;
404
405 alc = bfd_malloc (len);
406 if (alc == NULL)
407 return FALSE;
408 memcpy (alc, name, len - 1);
409 alc[len - 1] = '\0';
410 name = alc;
411 copy = TRUE;
412 }
413
414 indx = _bfd_elf_strtab_add (dynstr, name, copy);
415
416 if (alc != NULL)
417 free (alc);
418
419 if (indx == (bfd_size_type) -1)
420 return FALSE;
421 h->dynstr_index = indx;
422 }
423
424 return TRUE;
425 }
426 \f
427 /* Record an assignment to a symbol made by a linker script. We need
428 this in case some dynamic object refers to this symbol. */
429
430 bfd_boolean
431 bfd_elf_record_link_assignment (bfd *output_bfd ATTRIBUTE_UNUSED,
432 struct bfd_link_info *info,
433 const char *name,
434 bfd_boolean provide)
435 {
436 struct elf_link_hash_entry *h;
437
438 if (info->hash->creator->flavour != bfd_target_elf_flavour)
439 return TRUE;
440
441 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, TRUE, FALSE);
442 if (h == NULL)
443 return FALSE;
444
445 if (h->root.type == bfd_link_hash_new)
446 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
447
448 /* If this symbol is being provided by the linker script, and it is
449 currently defined by a dynamic object, but not by a regular
450 object, then mark it as undefined so that the generic linker will
451 force the correct value. */
452 if (provide
453 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
454 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
455 h->root.type = bfd_link_hash_undefined;
456
457 /* If this symbol is not being provided by the linker script, and it is
458 currently defined by a dynamic object, but not by a regular object,
459 then clear out any version information because the symbol will not be
460 associated with the dynamic object any more. */
461 if (!provide
462 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
463 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
464 h->verinfo.verdef = NULL;
465
466 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
467
468 if (((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC
469 | ELF_LINK_HASH_REF_DYNAMIC)) != 0
470 || info->shared)
471 && h->dynindx == -1)
472 {
473 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
474 return FALSE;
475
476 /* If this is a weak defined symbol, and we know a corresponding
477 real symbol from the same dynamic object, make sure the real
478 symbol is also made into a dynamic symbol. */
479 if (h->weakdef != NULL
480 && h->weakdef->dynindx == -1)
481 {
482 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
483 return FALSE;
484 }
485 }
486
487 return TRUE;
488 }
489
490 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
491 success, and 2 on a failure caused by attempting to record a symbol
492 in a discarded section, eg. a discarded link-once section symbol. */
493
494 int
495 elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
496 bfd *input_bfd,
497 long input_indx)
498 {
499 bfd_size_type amt;
500 struct elf_link_local_dynamic_entry *entry;
501 struct elf_link_hash_table *eht;
502 struct elf_strtab_hash *dynstr;
503 unsigned long dynstr_index;
504 char *name;
505 Elf_External_Sym_Shndx eshndx;
506 char esym[sizeof (Elf64_External_Sym)];
507
508 if (! is_elf_hash_table (info))
509 return 0;
510
511 /* See if the entry exists already. */
512 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
513 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
514 return 1;
515
516 amt = sizeof (*entry);
517 entry = bfd_alloc (input_bfd, amt);
518 if (entry == NULL)
519 return 0;
520
521 /* Go find the symbol, so that we can find it's name. */
522 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
523 1, input_indx, &entry->isym, esym, &eshndx))
524 {
525 bfd_release (input_bfd, entry);
526 return 0;
527 }
528
529 if (entry->isym.st_shndx != SHN_UNDEF
530 && (entry->isym.st_shndx < SHN_LORESERVE
531 || entry->isym.st_shndx > SHN_HIRESERVE))
532 {
533 asection *s;
534
535 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
536 if (s == NULL || bfd_is_abs_section (s->output_section))
537 {
538 /* We can still bfd_release here as nothing has done another
539 bfd_alloc. We can't do this later in this function. */
540 bfd_release (input_bfd, entry);
541 return 2;
542 }
543 }
544
545 name = (bfd_elf_string_from_elf_section
546 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
547 entry->isym.st_name));
548
549 dynstr = elf_hash_table (info)->dynstr;
550 if (dynstr == NULL)
551 {
552 /* Create a strtab to hold the dynamic symbol names. */
553 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
554 if (dynstr == NULL)
555 return 0;
556 }
557
558 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
559 if (dynstr_index == (unsigned long) -1)
560 return 0;
561 entry->isym.st_name = dynstr_index;
562
563 eht = elf_hash_table (info);
564
565 entry->next = eht->dynlocal;
566 eht->dynlocal = entry;
567 entry->input_bfd = input_bfd;
568 entry->input_indx = input_indx;
569 eht->dynsymcount++;
570
571 /* Whatever binding the symbol had before, it's now local. */
572 entry->isym.st_info
573 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
574
575 /* The dynindx will be set at the end of size_dynamic_sections. */
576
577 return 1;
578 }
579
580 /* Return the dynindex of a local dynamic symbol. */
581
582 long
583 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
584 bfd *input_bfd,
585 long input_indx)
586 {
587 struct elf_link_local_dynamic_entry *e;
588
589 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
590 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
591 return e->dynindx;
592 return -1;
593 }
594
595 /* This function is used to renumber the dynamic symbols, if some of
596 them are removed because they are marked as local. This is called
597 via elf_link_hash_traverse. */
598
599 static bfd_boolean
600 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
601 void *data)
602 {
603 size_t *count = data;
604
605 if (h->root.type == bfd_link_hash_warning)
606 h = (struct elf_link_hash_entry *) h->root.u.i.link;
607
608 if (h->dynindx != -1)
609 h->dynindx = ++(*count);
610
611 return TRUE;
612 }
613
614 /* Assign dynsym indices. In a shared library we generate a section
615 symbol for each output section, which come first. Next come all of
616 the back-end allocated local dynamic syms, followed by the rest of
617 the global symbols. */
618
619 unsigned long
620 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
621 {
622 unsigned long dynsymcount = 0;
623
624 if (info->shared)
625 {
626 asection *p;
627 for (p = output_bfd->sections; p ; p = p->next)
628 if ((p->flags & SEC_EXCLUDE) == 0)
629 elf_section_data (p)->dynindx = ++dynsymcount;
630 }
631
632 if (elf_hash_table (info)->dynlocal)
633 {
634 struct elf_link_local_dynamic_entry *p;
635 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
636 p->dynindx = ++dynsymcount;
637 }
638
639 elf_link_hash_traverse (elf_hash_table (info),
640 elf_link_renumber_hash_table_dynsyms,
641 &dynsymcount);
642
643 /* There is an unused NULL entry at the head of the table which
644 we must account for in our count. Unless there weren't any
645 symbols, which means we'll have no table at all. */
646 if (dynsymcount != 0)
647 ++dynsymcount;
648
649 return elf_hash_table (info)->dynsymcount = dynsymcount;
650 }
651
652 /* This function is called when we want to define a new symbol. It
653 handles the various cases which arise when we find a definition in
654 a dynamic object, or when there is already a definition in a
655 dynamic object. The new symbol is described by NAME, SYM, PSEC,
656 and PVALUE. We set SYM_HASH to the hash table entry. We set
657 OVERRIDE if the old symbol is overriding a new definition. We set
658 TYPE_CHANGE_OK if it is OK for the type to change. We set
659 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
660 change, we mean that we shouldn't warn if the type or size does
661 change. DT_NEEDED indicates if it comes from a DT_NEEDED entry of
662 a shared object. */
663
664 bfd_boolean
665 _bfd_elf_merge_symbol (bfd *abfd,
666 struct bfd_link_info *info,
667 const char *name,
668 Elf_Internal_Sym *sym,
669 asection **psec,
670 bfd_vma *pvalue,
671 struct elf_link_hash_entry **sym_hash,
672 bfd_boolean *skip,
673 bfd_boolean *override,
674 bfd_boolean *type_change_ok,
675 bfd_boolean *size_change_ok,
676 bfd_boolean dt_needed)
677 {
678 asection *sec;
679 struct elf_link_hash_entry *h;
680 struct elf_link_hash_entry *flip;
681 int bind;
682 bfd *oldbfd;
683 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
684 bfd_boolean newweakdef, oldweakdef, newweakundef, oldweakundef;
685
686 *skip = FALSE;
687 *override = FALSE;
688
689 sec = *psec;
690 bind = ELF_ST_BIND (sym->st_info);
691
692 if (! bfd_is_und_section (sec))
693 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
694 else
695 h = ((struct elf_link_hash_entry *)
696 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
697 if (h == NULL)
698 return FALSE;
699 *sym_hash = h;
700
701 /* This code is for coping with dynamic objects, and is only useful
702 if we are doing an ELF link. */
703 if (info->hash->creator != abfd->xvec)
704 return TRUE;
705
706 /* For merging, we only care about real symbols. */
707
708 while (h->root.type == bfd_link_hash_indirect
709 || h->root.type == bfd_link_hash_warning)
710 h = (struct elf_link_hash_entry *) h->root.u.i.link;
711
712 /* If we just created the symbol, mark it as being an ELF symbol.
713 Other than that, there is nothing to do--there is no merge issue
714 with a newly defined symbol--so we just return. */
715
716 if (h->root.type == bfd_link_hash_new)
717 {
718 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
719 return TRUE;
720 }
721
722 /* OLDBFD is a BFD associated with the existing symbol. */
723
724 switch (h->root.type)
725 {
726 default:
727 oldbfd = NULL;
728 break;
729
730 case bfd_link_hash_undefined:
731 case bfd_link_hash_undefweak:
732 oldbfd = h->root.u.undef.abfd;
733 break;
734
735 case bfd_link_hash_defined:
736 case bfd_link_hash_defweak:
737 oldbfd = h->root.u.def.section->owner;
738 break;
739
740 case bfd_link_hash_common:
741 oldbfd = h->root.u.c.p->section->owner;
742 break;
743 }
744
745 /* In cases involving weak versioned symbols, we may wind up trying
746 to merge a symbol with itself. Catch that here, to avoid the
747 confusion that results if we try to override a symbol with
748 itself. The additional tests catch cases like
749 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
750 dynamic object, which we do want to handle here. */
751 if (abfd == oldbfd
752 && ((abfd->flags & DYNAMIC) == 0
753 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0))
754 return TRUE;
755
756 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
757 respectively, is from a dynamic object. */
758
759 if ((abfd->flags & DYNAMIC) != 0)
760 newdyn = TRUE;
761 else
762 newdyn = FALSE;
763
764 if (oldbfd != NULL)
765 olddyn = (oldbfd->flags & DYNAMIC) != 0;
766 else
767 {
768 asection *hsec;
769
770 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
771 indices used by MIPS ELF. */
772 switch (h->root.type)
773 {
774 default:
775 hsec = NULL;
776 break;
777
778 case bfd_link_hash_defined:
779 case bfd_link_hash_defweak:
780 hsec = h->root.u.def.section;
781 break;
782
783 case bfd_link_hash_common:
784 hsec = h->root.u.c.p->section;
785 break;
786 }
787
788 if (hsec == NULL)
789 olddyn = FALSE;
790 else
791 olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0;
792 }
793
794 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
795 respectively, appear to be a definition rather than reference. */
796
797 if (bfd_is_und_section (sec) || bfd_is_com_section (sec))
798 newdef = FALSE;
799 else
800 newdef = TRUE;
801
802 if (h->root.type == bfd_link_hash_undefined
803 || h->root.type == bfd_link_hash_undefweak
804 || h->root.type == bfd_link_hash_common)
805 olddef = FALSE;
806 else
807 olddef = TRUE;
808
809 /* We need to rememeber if a symbol has a definition in a dynamic
810 object or is weak in all dynamic objects. Internal and hidden
811 visibility will make it unavailable to dynamic objects. */
812 if (newdyn && (h->elf_link_hash_flags & ELF_LINK_DYNAMIC_DEF) == 0)
813 {
814 if (!bfd_is_und_section (sec))
815 h->elf_link_hash_flags |= ELF_LINK_DYNAMIC_DEF;
816 else
817 {
818 /* Check if this symbol is weak in all dynamic objects. If it
819 is the first time we see it in a dynamic object, we mark
820 if it is weak. Otherwise, we clear it. */
821 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0)
822 {
823 if (bind == STB_WEAK)
824 h->elf_link_hash_flags |= ELF_LINK_DYNAMIC_WEAK;
825 }
826 else if (bind != STB_WEAK)
827 h->elf_link_hash_flags &= ~ELF_LINK_DYNAMIC_WEAK;
828 }
829 }
830
831 /* If the old symbol has non-default visibility, we ignore the new
832 definition from a dynamic object. */
833 if (newdyn
834 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
835 && !bfd_is_und_section (sec))
836 {
837 *skip = TRUE;
838 /* Make sure this symbol is dynamic. */
839 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
840 /* A protected symbol has external availability. Make sure it is
841 recorded as dynamic.
842
843 FIXME: Should we check type and size for protected symbol? */
844 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
845 return _bfd_elf_link_record_dynamic_symbol (info, h);
846 else
847 return TRUE;
848 }
849 else if (!newdyn
850 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
851 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
852 {
853 /* If the new symbol with non-default visibility comes from a
854 relocatable file and the old definition comes from a dynamic
855 object, we remove the old definition. */
856 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
857 h = *sym_hash;
858 h->root.type = bfd_link_hash_new;
859 h->root.u.undef.abfd = NULL;
860 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
861 {
862 h->elf_link_hash_flags &= ~ELF_LINK_HASH_DEF_DYNAMIC;
863 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_DYNAMIC
864 | ELF_LINK_DYNAMIC_DEF);
865 }
866 /* FIXME: Should we check type and size for protected symbol? */
867 h->size = 0;
868 h->type = 0;
869 return TRUE;
870 }
871
872 /* We need to treat weak definiton right, depending on if there is a
873 definition from a dynamic object. */
874 if (bind == STB_WEAK)
875 {
876 if (olddef)
877 {
878 newweakdef = TRUE;
879 newweakundef = FALSE;
880 }
881 else
882 {
883 newweakdef = FALSE;
884 newweakundef = TRUE;
885 }
886 }
887 else
888 newweakdef = newweakundef = FALSE;
889
890 /* If the new weak definition comes from a relocatable file and the
891 old symbol comes from a dynamic object, we treat the new one as
892 strong. */
893 if (newweakdef && !newdyn && olddyn)
894 newweakdef = FALSE;
895
896 if (h->root.type == bfd_link_hash_defweak)
897 {
898 oldweakdef = TRUE;
899 oldweakundef = FALSE;
900 }
901 else if (h->root.type == bfd_link_hash_undefweak)
902 {
903 oldweakdef = FALSE;
904 oldweakundef = TRUE;
905 }
906 else
907 oldweakdef = oldweakundef = FALSE;
908
909 /* If the old weak definition comes from a relocatable file and the
910 new symbol comes from a dynamic object, we treat the old one as
911 strong. */
912 if (oldweakdef && !olddyn && newdyn)
913 oldweakdef = FALSE;
914
915 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
916 symbol, respectively, appears to be a common symbol in a dynamic
917 object. If a symbol appears in an uninitialized section, and is
918 not weak, and is not a function, then it may be a common symbol
919 which was resolved when the dynamic object was created. We want
920 to treat such symbols specially, because they raise special
921 considerations when setting the symbol size: if the symbol
922 appears as a common symbol in a regular object, and the size in
923 the regular object is larger, we must make sure that we use the
924 larger size. This problematic case can always be avoided in C,
925 but it must be handled correctly when using Fortran shared
926 libraries.
927
928 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
929 likewise for OLDDYNCOMMON and OLDDEF.
930
931 Note that this test is just a heuristic, and that it is quite
932 possible to have an uninitialized symbol in a shared object which
933 is really a definition, rather than a common symbol. This could
934 lead to some minor confusion when the symbol really is a common
935 symbol in some regular object. However, I think it will be
936 harmless. */
937
938 if (newdyn
939 && newdef
940 && (sec->flags & SEC_ALLOC) != 0
941 && (sec->flags & SEC_LOAD) == 0
942 && sym->st_size > 0
943 && !newweakdef
944 && !newweakundef
945 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
946 newdyncommon = TRUE;
947 else
948 newdyncommon = FALSE;
949
950 if (olddyn
951 && olddef
952 && h->root.type == bfd_link_hash_defined
953 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
954 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
955 && (h->root.u.def.section->flags & SEC_LOAD) == 0
956 && h->size > 0
957 && h->type != STT_FUNC)
958 olddyncommon = TRUE;
959 else
960 olddyncommon = FALSE;
961
962 /* It's OK to change the type if either the existing symbol or the
963 new symbol is weak unless it comes from a DT_NEEDED entry of
964 a shared object, in which case, the DT_NEEDED entry may not be
965 required at the run time. The type change is also OK if the
966 old symbol is undefined and the new symbol is defined. */
967
968 if ((! dt_needed && oldweakdef)
969 || oldweakundef
970 || newweakdef
971 || newweakundef
972 || (newdef
973 && (h->root.type == bfd_link_hash_undefined
974 || h->root.type == bfd_link_hash_undefweak)))
975 *type_change_ok = TRUE;
976
977 /* It's OK to change the size if either the existing symbol or the
978 new symbol is weak, or if the old symbol is undefined. */
979
980 if (*type_change_ok
981 || h->root.type == bfd_link_hash_undefined)
982 *size_change_ok = TRUE;
983
984 /* If both the old and the new symbols look like common symbols in a
985 dynamic object, set the size of the symbol to the larger of the
986 two. */
987
988 if (olddyncommon
989 && newdyncommon
990 && sym->st_size != h->size)
991 {
992 /* Since we think we have two common symbols, issue a multiple
993 common warning if desired. Note that we only warn if the
994 size is different. If the size is the same, we simply let
995 the old symbol override the new one as normally happens with
996 symbols defined in dynamic objects. */
997
998 if (! ((*info->callbacks->multiple_common)
999 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1000 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1001 return FALSE;
1002
1003 if (sym->st_size > h->size)
1004 h->size = sym->st_size;
1005
1006 *size_change_ok = TRUE;
1007 }
1008
1009 /* If we are looking at a dynamic object, and we have found a
1010 definition, we need to see if the symbol was already defined by
1011 some other object. If so, we want to use the existing
1012 definition, and we do not want to report a multiple symbol
1013 definition error; we do this by clobbering *PSEC to be
1014 bfd_und_section_ptr.
1015
1016 We treat a common symbol as a definition if the symbol in the
1017 shared library is a function, since common symbols always
1018 represent variables; this can cause confusion in principle, but
1019 any such confusion would seem to indicate an erroneous program or
1020 shared library. We also permit a common symbol in a regular
1021 object to override a weak symbol in a shared object.
1022
1023 We prefer a non-weak definition in a shared library to a weak
1024 definition in the executable unless it comes from a DT_NEEDED
1025 entry of a shared object, in which case, the DT_NEEDED entry
1026 may not be required at the run time. */
1027
1028 if (newdyn
1029 && newdef
1030 && (olddef
1031 || (h->root.type == bfd_link_hash_common
1032 && (newweakdef
1033 || newweakundef
1034 || ELF_ST_TYPE (sym->st_info) == STT_FUNC)))
1035 && (!oldweakdef
1036 || dt_needed
1037 || newweakdef
1038 || newweakundef))
1039 {
1040 *override = TRUE;
1041 newdef = FALSE;
1042 newdyncommon = FALSE;
1043
1044 *psec = sec = bfd_und_section_ptr;
1045 *size_change_ok = TRUE;
1046
1047 /* If we get here when the old symbol is a common symbol, then
1048 we are explicitly letting it override a weak symbol or
1049 function in a dynamic object, and we don't want to warn about
1050 a type change. If the old symbol is a defined symbol, a type
1051 change warning may still be appropriate. */
1052
1053 if (h->root.type == bfd_link_hash_common)
1054 *type_change_ok = TRUE;
1055 }
1056
1057 /* Handle the special case of an old common symbol merging with a
1058 new symbol which looks like a common symbol in a shared object.
1059 We change *PSEC and *PVALUE to make the new symbol look like a
1060 common symbol, and let _bfd_generic_link_add_one_symbol will do
1061 the right thing. */
1062
1063 if (newdyncommon
1064 && h->root.type == bfd_link_hash_common)
1065 {
1066 *override = TRUE;
1067 newdef = FALSE;
1068 newdyncommon = FALSE;
1069 *pvalue = sym->st_size;
1070 *psec = sec = bfd_com_section_ptr;
1071 *size_change_ok = TRUE;
1072 }
1073
1074 /* If the old symbol is from a dynamic object, and the new symbol is
1075 a definition which is not from a dynamic object, then the new
1076 symbol overrides the old symbol. Symbols from regular files
1077 always take precedence over symbols from dynamic objects, even if
1078 they are defined after the dynamic object in the link.
1079
1080 As above, we again permit a common symbol in a regular object to
1081 override a definition in a shared object if the shared object
1082 symbol is a function or is weak.
1083
1084 As above, we permit a non-weak definition in a shared object to
1085 override a weak definition in a regular object. */
1086
1087 flip = NULL;
1088 if (! newdyn
1089 && (newdef
1090 || (bfd_is_com_section (sec)
1091 && (oldweakdef || h->type == STT_FUNC)))
1092 && olddyn
1093 && olddef
1094 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1095 && ((!newweakdef && !newweakundef) || oldweakdef))
1096 {
1097 /* Change the hash table entry to undefined, and let
1098 _bfd_generic_link_add_one_symbol do the right thing with the
1099 new definition. */
1100
1101 h->root.type = bfd_link_hash_undefined;
1102 h->root.u.undef.abfd = h->root.u.def.section->owner;
1103 *size_change_ok = TRUE;
1104
1105 olddef = FALSE;
1106 olddyncommon = FALSE;
1107
1108 /* We again permit a type change when a common symbol may be
1109 overriding a function. */
1110
1111 if (bfd_is_com_section (sec))
1112 *type_change_ok = TRUE;
1113
1114 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1115 flip = *sym_hash;
1116 else
1117 /* This union may have been set to be non-NULL when this symbol
1118 was seen in a dynamic object. We must force the union to be
1119 NULL, so that it is correct for a regular symbol. */
1120 h->verinfo.vertree = NULL;
1121 }
1122
1123 /* Handle the special case of a new common symbol merging with an
1124 old symbol that looks like it might be a common symbol defined in
1125 a shared object. Note that we have already handled the case in
1126 which a new common symbol should simply override the definition
1127 in the shared library. */
1128
1129 if (! newdyn
1130 && bfd_is_com_section (sec)
1131 && olddyncommon)
1132 {
1133 /* It would be best if we could set the hash table entry to a
1134 common symbol, but we don't know what to use for the section
1135 or the alignment. */
1136 if (! ((*info->callbacks->multiple_common)
1137 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1138 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1139 return FALSE;
1140
1141 /* If the predumed common symbol in the dynamic object is
1142 larger, pretend that the new symbol has its size. */
1143
1144 if (h->size > *pvalue)
1145 *pvalue = h->size;
1146
1147 /* FIXME: We no longer know the alignment required by the symbol
1148 in the dynamic object, so we just wind up using the one from
1149 the regular object. */
1150
1151 olddef = FALSE;
1152 olddyncommon = FALSE;
1153
1154 h->root.type = bfd_link_hash_undefined;
1155 h->root.u.undef.abfd = h->root.u.def.section->owner;
1156
1157 *size_change_ok = TRUE;
1158 *type_change_ok = TRUE;
1159
1160 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1161 flip = *sym_hash;
1162 else
1163 h->verinfo.vertree = NULL;
1164 }
1165
1166 if (flip != NULL)
1167 {
1168 /* Handle the case where we had a versioned symbol in a dynamic
1169 library and now find a definition in a normal object. In this
1170 case, we make the versioned symbol point to the normal one. */
1171 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1172 flip->root.type = h->root.type;
1173 h->root.type = bfd_link_hash_indirect;
1174 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1175 (*bed->elf_backend_copy_indirect_symbol) (bed, flip, h);
1176 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1177 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
1178 {
1179 h->elf_link_hash_flags &= ~ELF_LINK_HASH_DEF_DYNAMIC;
1180 flip->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1181 }
1182 }
1183
1184 /* Handle the special case of a weak definition in a regular object
1185 followed by a non-weak definition in a shared object. In this
1186 case, we prefer the definition in the shared object unless it
1187 comes from a DT_NEEDED entry of a shared object, in which case,
1188 the DT_NEEDED entry may not be required at the run time. */
1189 if (olddef
1190 && ! dt_needed
1191 && oldweakdef
1192 && newdef
1193 && newdyn
1194 && !newweakdef
1195 && !newweakundef)
1196 {
1197 /* To make this work we have to frob the flags so that the rest
1198 of the code does not think we are using the regular
1199 definition. */
1200 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
1201 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
1202 else if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
1203 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1204 h->elf_link_hash_flags &= ~ (ELF_LINK_HASH_DEF_REGULAR
1205 | ELF_LINK_HASH_DEF_DYNAMIC);
1206
1207 /* If H is the target of an indirection, we want the caller to
1208 use H rather than the indirect symbol. Otherwise if we are
1209 defining a new indirect symbol we will wind up attaching it
1210 to the entry we are overriding. */
1211 *sym_hash = h;
1212 }
1213
1214 /* Handle the special case of a non-weak definition in a shared
1215 object followed by a weak definition in a regular object. In
1216 this case we prefer the definition in the shared object. To make
1217 this work we have to tell the caller to not treat the new symbol
1218 as a definition. */
1219 if (olddef
1220 && olddyn
1221 && !oldweakdef
1222 && newdef
1223 && ! newdyn
1224 && (newweakdef || newweakundef))
1225 *override = TRUE;
1226
1227 return TRUE;
1228 }
1229
1230 /* This function is called to create an indirect symbol from the
1231 default for the symbol with the default version if needed. The
1232 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1233 set DYNSYM if the new indirect symbol is dynamic. DT_NEEDED
1234 indicates if it comes from a DT_NEEDED entry of a shared object. */
1235
1236 bfd_boolean
1237 _bfd_elf_add_default_symbol (bfd *abfd,
1238 struct bfd_link_info *info,
1239 struct elf_link_hash_entry *h,
1240 const char *name,
1241 Elf_Internal_Sym *sym,
1242 asection **psec,
1243 bfd_vma *value,
1244 bfd_boolean *dynsym,
1245 bfd_boolean override,
1246 bfd_boolean dt_needed)
1247 {
1248 bfd_boolean type_change_ok;
1249 bfd_boolean size_change_ok;
1250 bfd_boolean skip;
1251 char *shortname;
1252 struct elf_link_hash_entry *hi;
1253 struct bfd_link_hash_entry *bh;
1254 const struct elf_backend_data *bed;
1255 bfd_boolean collect;
1256 bfd_boolean dynamic;
1257 char *p;
1258 size_t len, shortlen;
1259 asection *sec;
1260
1261 /* If this symbol has a version, and it is the default version, we
1262 create an indirect symbol from the default name to the fully
1263 decorated name. This will cause external references which do not
1264 specify a version to be bound to this version of the symbol. */
1265 p = strchr (name, ELF_VER_CHR);
1266 if (p == NULL || p[1] != ELF_VER_CHR)
1267 return TRUE;
1268
1269 if (override)
1270 {
1271 /* We are overridden by an old defition. We need to check if we
1272 need to create the indirect symbol from the default name. */
1273 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1274 FALSE, FALSE);
1275 BFD_ASSERT (hi != NULL);
1276 if (hi == h)
1277 return TRUE;
1278 while (hi->root.type == bfd_link_hash_indirect
1279 || hi->root.type == bfd_link_hash_warning)
1280 {
1281 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1282 if (hi == h)
1283 return TRUE;
1284 }
1285 }
1286
1287 bed = get_elf_backend_data (abfd);
1288 collect = bed->collect;
1289 dynamic = (abfd->flags & DYNAMIC) != 0;
1290
1291 shortlen = p - name;
1292 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1293 if (shortname == NULL)
1294 return FALSE;
1295 memcpy (shortname, name, shortlen);
1296 shortname[shortlen] = '\0';
1297
1298 /* We are going to create a new symbol. Merge it with any existing
1299 symbol with this name. For the purposes of the merge, act as
1300 though we were defining the symbol we just defined, although we
1301 actually going to define an indirect symbol. */
1302 type_change_ok = FALSE;
1303 size_change_ok = FALSE;
1304 sec = *psec;
1305 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1306 &hi, &skip, &override, &type_change_ok,
1307 &size_change_ok, dt_needed))
1308 return FALSE;
1309
1310 if (skip)
1311 goto nondefault;
1312
1313 if (! override)
1314 {
1315 bh = &hi->root;
1316 if (! (_bfd_generic_link_add_one_symbol
1317 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1318 0, name, FALSE, collect, &bh)))
1319 return FALSE;
1320 hi = (struct elf_link_hash_entry *) bh;
1321 }
1322 else
1323 {
1324 /* In this case the symbol named SHORTNAME is overriding the
1325 indirect symbol we want to add. We were planning on making
1326 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1327 is the name without a version. NAME is the fully versioned
1328 name, and it is the default version.
1329
1330 Overriding means that we already saw a definition for the
1331 symbol SHORTNAME in a regular object, and it is overriding
1332 the symbol defined in the dynamic object.
1333
1334 When this happens, we actually want to change NAME, the
1335 symbol we just added, to refer to SHORTNAME. This will cause
1336 references to NAME in the shared object to become references
1337 to SHORTNAME in the regular object. This is what we expect
1338 when we override a function in a shared object: that the
1339 references in the shared object will be mapped to the
1340 definition in the regular object. */
1341
1342 while (hi->root.type == bfd_link_hash_indirect
1343 || hi->root.type == bfd_link_hash_warning)
1344 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1345
1346 h->root.type = bfd_link_hash_indirect;
1347 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1348 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
1349 {
1350 h->elf_link_hash_flags &=~ ELF_LINK_HASH_DEF_DYNAMIC;
1351 hi->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1352 if (hi->elf_link_hash_flags
1353 & (ELF_LINK_HASH_REF_REGULAR
1354 | ELF_LINK_HASH_DEF_REGULAR))
1355 {
1356 if (! _bfd_elf_link_record_dynamic_symbol (info, hi))
1357 return FALSE;
1358 }
1359 }
1360
1361 /* Now set HI to H, so that the following code will set the
1362 other fields correctly. */
1363 hi = h;
1364 }
1365
1366 /* If there is a duplicate definition somewhere, then HI may not
1367 point to an indirect symbol. We will have reported an error to
1368 the user in that case. */
1369
1370 if (hi->root.type == bfd_link_hash_indirect)
1371 {
1372 struct elf_link_hash_entry *ht;
1373
1374 /* If the symbol became indirect, then we assume that we have
1375 not seen a definition before. */
1376 BFD_ASSERT ((hi->elf_link_hash_flags
1377 & (ELF_LINK_HASH_DEF_DYNAMIC
1378 | ELF_LINK_HASH_DEF_REGULAR)) == 0);
1379
1380 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1381 (*bed->elf_backend_copy_indirect_symbol) (bed, ht, hi);
1382
1383 /* See if the new flags lead us to realize that the symbol must
1384 be dynamic. */
1385 if (! *dynsym)
1386 {
1387 if (! dynamic)
1388 {
1389 if (info->shared
1390 || ((hi->elf_link_hash_flags
1391 & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1392 *dynsym = TRUE;
1393 }
1394 else
1395 {
1396 if ((hi->elf_link_hash_flags
1397 & ELF_LINK_HASH_REF_REGULAR) != 0)
1398 *dynsym = TRUE;
1399 }
1400 }
1401 }
1402
1403 /* We also need to define an indirection from the nondefault version
1404 of the symbol. */
1405
1406 nondefault:
1407 len = strlen (name);
1408 shortname = bfd_hash_allocate (&info->hash->table, len);
1409 if (shortname == NULL)
1410 return FALSE;
1411 memcpy (shortname, name, shortlen);
1412 memcpy (shortname + shortlen, p + 1, len - shortlen);
1413
1414 /* Once again, merge with any existing symbol. */
1415 type_change_ok = FALSE;
1416 size_change_ok = FALSE;
1417 sec = *psec;
1418 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1419 &hi, &skip, &override, &type_change_ok,
1420 &size_change_ok, dt_needed))
1421 return FALSE;
1422
1423 if (skip)
1424 return TRUE;
1425
1426 if (override)
1427 {
1428 /* Here SHORTNAME is a versioned name, so we don't expect to see
1429 the type of override we do in the case above unless it is
1430 overridden by a versioned definiton. */
1431 if (hi->root.type != bfd_link_hash_defined
1432 && hi->root.type != bfd_link_hash_defweak)
1433 (*_bfd_error_handler)
1434 (_("%s: warning: unexpected redefinition of indirect versioned symbol `%s'"),
1435 bfd_archive_filename (abfd), shortname);
1436 }
1437 else
1438 {
1439 bh = &hi->root;
1440 if (! (_bfd_generic_link_add_one_symbol
1441 (info, abfd, shortname, BSF_INDIRECT,
1442 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1443 return FALSE;
1444 hi = (struct elf_link_hash_entry *) bh;
1445
1446 /* If there is a duplicate definition somewhere, then HI may not
1447 point to an indirect symbol. We will have reported an error
1448 to the user in that case. */
1449
1450 if (hi->root.type == bfd_link_hash_indirect)
1451 {
1452 /* If the symbol became indirect, then we assume that we have
1453 not seen a definition before. */
1454 BFD_ASSERT ((hi->elf_link_hash_flags
1455 & (ELF_LINK_HASH_DEF_DYNAMIC
1456 | ELF_LINK_HASH_DEF_REGULAR)) == 0);
1457
1458 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
1459
1460 /* See if the new flags lead us to realize that the symbol
1461 must be dynamic. */
1462 if (! *dynsym)
1463 {
1464 if (! dynamic)
1465 {
1466 if (info->shared
1467 || ((hi->elf_link_hash_flags
1468 & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1469 *dynsym = TRUE;
1470 }
1471 else
1472 {
1473 if ((hi->elf_link_hash_flags
1474 & ELF_LINK_HASH_REF_REGULAR) != 0)
1475 *dynsym = TRUE;
1476 }
1477 }
1478 }
1479 }
1480
1481 return TRUE;
1482 }
1483 \f
1484 /* This routine is used to export all defined symbols into the dynamic
1485 symbol table. It is called via elf_link_hash_traverse. */
1486
1487 bfd_boolean
1488 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1489 {
1490 struct elf_info_failed *eif = data;
1491
1492 /* Ignore indirect symbols. These are added by the versioning code. */
1493 if (h->root.type == bfd_link_hash_indirect)
1494 return TRUE;
1495
1496 if (h->root.type == bfd_link_hash_warning)
1497 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1498
1499 if (h->dynindx == -1
1500 && (h->elf_link_hash_flags
1501 & (ELF_LINK_HASH_DEF_REGULAR | ELF_LINK_HASH_REF_REGULAR)) != 0)
1502 {
1503 struct bfd_elf_version_tree *t;
1504 struct bfd_elf_version_expr *d;
1505
1506 for (t = eif->verdefs; t != NULL; t = t->next)
1507 {
1508 if (t->globals != NULL)
1509 {
1510 for (d = t->globals; d != NULL; d = d->next)
1511 {
1512 if ((*d->match) (d, h->root.root.string))
1513 goto doit;
1514 }
1515 }
1516
1517 if (t->locals != NULL)
1518 {
1519 for (d = t->locals ; d != NULL; d = d->next)
1520 {
1521 if ((*d->match) (d, h->root.root.string))
1522 return TRUE;
1523 }
1524 }
1525 }
1526
1527 if (!eif->verdefs)
1528 {
1529 doit:
1530 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
1531 {
1532 eif->failed = TRUE;
1533 return FALSE;
1534 }
1535 }
1536 }
1537
1538 return TRUE;
1539 }
1540 \f
1541 /* Look through the symbols which are defined in other shared
1542 libraries and referenced here. Update the list of version
1543 dependencies. This will be put into the .gnu.version_r section.
1544 This function is called via elf_link_hash_traverse. */
1545
1546 bfd_boolean
1547 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1548 void *data)
1549 {
1550 struct elf_find_verdep_info *rinfo = data;
1551 Elf_Internal_Verneed *t;
1552 Elf_Internal_Vernaux *a;
1553 bfd_size_type amt;
1554
1555 if (h->root.type == bfd_link_hash_warning)
1556 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1557
1558 /* We only care about symbols defined in shared objects with version
1559 information. */
1560 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
1561 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1562 || h->dynindx == -1
1563 || h->verinfo.verdef == NULL)
1564 return TRUE;
1565
1566 /* See if we already know about this version. */
1567 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1568 {
1569 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1570 continue;
1571
1572 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1573 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1574 return TRUE;
1575
1576 break;
1577 }
1578
1579 /* This is a new version. Add it to tree we are building. */
1580
1581 if (t == NULL)
1582 {
1583 amt = sizeof *t;
1584 t = bfd_zalloc (rinfo->output_bfd, amt);
1585 if (t == NULL)
1586 {
1587 rinfo->failed = TRUE;
1588 return FALSE;
1589 }
1590
1591 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1592 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1593 elf_tdata (rinfo->output_bfd)->verref = t;
1594 }
1595
1596 amt = sizeof *a;
1597 a = bfd_zalloc (rinfo->output_bfd, amt);
1598
1599 /* Note that we are copying a string pointer here, and testing it
1600 above. If bfd_elf_string_from_elf_section is ever changed to
1601 discard the string data when low in memory, this will have to be
1602 fixed. */
1603 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1604
1605 a->vna_flags = h->verinfo.verdef->vd_flags;
1606 a->vna_nextptr = t->vn_auxptr;
1607
1608 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1609 ++rinfo->vers;
1610
1611 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1612
1613 t->vn_auxptr = a;
1614
1615 return TRUE;
1616 }
1617
1618 /* Figure out appropriate versions for all the symbols. We may not
1619 have the version number script until we have read all of the input
1620 files, so until that point we don't know which symbols should be
1621 local. This function is called via elf_link_hash_traverse. */
1622
1623 bfd_boolean
1624 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1625 {
1626 struct elf_assign_sym_version_info *sinfo;
1627 struct bfd_link_info *info;
1628 const struct elf_backend_data *bed;
1629 struct elf_info_failed eif;
1630 char *p;
1631 bfd_size_type amt;
1632
1633 sinfo = data;
1634 info = sinfo->info;
1635
1636 if (h->root.type == bfd_link_hash_warning)
1637 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1638
1639 /* Fix the symbol flags. */
1640 eif.failed = FALSE;
1641 eif.info = info;
1642 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1643 {
1644 if (eif.failed)
1645 sinfo->failed = TRUE;
1646 return FALSE;
1647 }
1648
1649 /* We only need version numbers for symbols defined in regular
1650 objects. */
1651 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1652 return TRUE;
1653
1654 bed = get_elf_backend_data (sinfo->output_bfd);
1655 p = strchr (h->root.root.string, ELF_VER_CHR);
1656 if (p != NULL && h->verinfo.vertree == NULL)
1657 {
1658 struct bfd_elf_version_tree *t;
1659 bfd_boolean hidden;
1660
1661 hidden = TRUE;
1662
1663 /* There are two consecutive ELF_VER_CHR characters if this is
1664 not a hidden symbol. */
1665 ++p;
1666 if (*p == ELF_VER_CHR)
1667 {
1668 hidden = FALSE;
1669 ++p;
1670 }
1671
1672 /* If there is no version string, we can just return out. */
1673 if (*p == '\0')
1674 {
1675 if (hidden)
1676 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
1677 return TRUE;
1678 }
1679
1680 /* Look for the version. If we find it, it is no longer weak. */
1681 for (t = sinfo->verdefs; t != NULL; t = t->next)
1682 {
1683 if (strcmp (t->name, p) == 0)
1684 {
1685 size_t len;
1686 char *alc;
1687 struct bfd_elf_version_expr *d;
1688
1689 len = p - h->root.root.string;
1690 alc = bfd_malloc (len);
1691 if (alc == NULL)
1692 return FALSE;
1693 memcpy (alc, h->root.root.string, len - 1);
1694 alc[len - 1] = '\0';
1695 if (alc[len - 2] == ELF_VER_CHR)
1696 alc[len - 2] = '\0';
1697
1698 h->verinfo.vertree = t;
1699 t->used = TRUE;
1700 d = NULL;
1701
1702 if (t->globals != NULL)
1703 {
1704 for (d = t->globals; d != NULL; d = d->next)
1705 if ((*d->match) (d, alc))
1706 break;
1707 }
1708
1709 /* See if there is anything to force this symbol to
1710 local scope. */
1711 if (d == NULL && t->locals != NULL)
1712 {
1713 for (d = t->locals; d != NULL; d = d->next)
1714 {
1715 if ((*d->match) (d, alc))
1716 {
1717 if (h->dynindx != -1
1718 && info->shared
1719 && ! info->export_dynamic)
1720 {
1721 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1722 }
1723
1724 break;
1725 }
1726 }
1727 }
1728
1729 free (alc);
1730 break;
1731 }
1732 }
1733
1734 /* If we are building an application, we need to create a
1735 version node for this version. */
1736 if (t == NULL && info->executable)
1737 {
1738 struct bfd_elf_version_tree **pp;
1739 int version_index;
1740
1741 /* If we aren't going to export this symbol, we don't need
1742 to worry about it. */
1743 if (h->dynindx == -1)
1744 return TRUE;
1745
1746 amt = sizeof *t;
1747 t = bfd_alloc (sinfo->output_bfd, amt);
1748 if (t == NULL)
1749 {
1750 sinfo->failed = TRUE;
1751 return FALSE;
1752 }
1753
1754 t->next = NULL;
1755 t->name = p;
1756 t->globals = NULL;
1757 t->locals = NULL;
1758 t->deps = NULL;
1759 t->name_indx = (unsigned int) -1;
1760 t->used = TRUE;
1761
1762 version_index = 1;
1763 /* Don't count anonymous version tag. */
1764 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1765 version_index = 0;
1766 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1767 ++version_index;
1768 t->vernum = version_index;
1769
1770 *pp = t;
1771
1772 h->verinfo.vertree = t;
1773 }
1774 else if (t == NULL)
1775 {
1776 /* We could not find the version for a symbol when
1777 generating a shared archive. Return an error. */
1778 (*_bfd_error_handler)
1779 (_("%s: undefined versioned symbol name %s"),
1780 bfd_get_filename (sinfo->output_bfd), h->root.root.string);
1781 bfd_set_error (bfd_error_bad_value);
1782 sinfo->failed = TRUE;
1783 return FALSE;
1784 }
1785
1786 if (hidden)
1787 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
1788 }
1789
1790 /* If we don't have a version for this symbol, see if we can find
1791 something. */
1792 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1793 {
1794 struct bfd_elf_version_tree *t;
1795 struct bfd_elf_version_tree *local_ver;
1796 struct bfd_elf_version_expr *d;
1797
1798 /* See if can find what version this symbol is in. If the
1799 symbol is supposed to be local, then don't actually register
1800 it. */
1801 local_ver = NULL;
1802 for (t = sinfo->verdefs; t != NULL; t = t->next)
1803 {
1804 if (t->globals != NULL)
1805 {
1806 bfd_boolean matched;
1807
1808 matched = FALSE;
1809 for (d = t->globals; d != NULL; d = d->next)
1810 {
1811 if ((*d->match) (d, h->root.root.string))
1812 {
1813 if (d->symver)
1814 matched = TRUE;
1815 else
1816 {
1817 /* There is a version without definition. Make
1818 the symbol the default definition for this
1819 version. */
1820 h->verinfo.vertree = t;
1821 local_ver = NULL;
1822 d->script = 1;
1823 break;
1824 }
1825 }
1826 }
1827
1828 if (d != NULL)
1829 break;
1830 else if (matched)
1831 /* There is no undefined version for this symbol. Hide the
1832 default one. */
1833 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1834 }
1835
1836 if (t->locals != NULL)
1837 {
1838 for (d = t->locals; d != NULL; d = d->next)
1839 {
1840 /* If the match is "*", keep looking for a more
1841 explicit, perhaps even global, match. */
1842 if (d->pattern[0] == '*' && d->pattern[1] == '\0')
1843 local_ver = t;
1844 else if ((*d->match) (d, h->root.root.string))
1845 {
1846 local_ver = t;
1847 break;
1848 }
1849 }
1850
1851 if (d != NULL)
1852 break;
1853 }
1854 }
1855
1856 if (local_ver != NULL)
1857 {
1858 h->verinfo.vertree = local_ver;
1859 if (h->dynindx != -1
1860 && info->shared
1861 && ! info->export_dynamic)
1862 {
1863 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1864 }
1865 }
1866 }
1867
1868 return TRUE;
1869 }
1870 \f
1871 /* Read and swap the relocs from the section indicated by SHDR. This
1872 may be either a REL or a RELA section. The relocations are
1873 translated into RELA relocations and stored in INTERNAL_RELOCS,
1874 which should have already been allocated to contain enough space.
1875 The EXTERNAL_RELOCS are a buffer where the external form of the
1876 relocations should be stored.
1877
1878 Returns FALSE if something goes wrong. */
1879
1880 static bfd_boolean
1881 elf_link_read_relocs_from_section (bfd *abfd,
1882 asection *sec,
1883 Elf_Internal_Shdr *shdr,
1884 void *external_relocs,
1885 Elf_Internal_Rela *internal_relocs)
1886 {
1887 const struct elf_backend_data *bed;
1888 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
1889 const bfd_byte *erela;
1890 const bfd_byte *erelaend;
1891 Elf_Internal_Rela *irela;
1892 Elf_Internal_Shdr *symtab_hdr;
1893 size_t nsyms;
1894
1895 /* If there aren't any relocations, that's OK. */
1896 if (!shdr)
1897 return TRUE;
1898
1899 /* Position ourselves at the start of the section. */
1900 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
1901 return FALSE;
1902
1903 /* Read the relocations. */
1904 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
1905 return FALSE;
1906
1907 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1908 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
1909
1910 bed = get_elf_backend_data (abfd);
1911
1912 /* Convert the external relocations to the internal format. */
1913 if (shdr->sh_entsize == bed->s->sizeof_rel)
1914 swap_in = bed->s->swap_reloc_in;
1915 else if (shdr->sh_entsize == bed->s->sizeof_rela)
1916 swap_in = bed->s->swap_reloca_in;
1917 else
1918 {
1919 bfd_set_error (bfd_error_wrong_format);
1920 return FALSE;
1921 }
1922
1923 erela = external_relocs;
1924 erelaend = erela + NUM_SHDR_ENTRIES (shdr) * shdr->sh_entsize;
1925 irela = internal_relocs;
1926 while (erela < erelaend)
1927 {
1928 bfd_vma r_symndx;
1929
1930 (*swap_in) (abfd, erela, irela);
1931 r_symndx = ELF32_R_SYM (irela->r_info);
1932 if (bed->s->arch_size == 64)
1933 r_symndx >>= 24;
1934 if ((size_t) r_symndx >= nsyms)
1935 {
1936 (*_bfd_error_handler)
1937 (_("%s: bad reloc symbol index (0x%lx >= 0x%lx) for offset 0x%lx in section `%s'"),
1938 bfd_archive_filename (abfd), (unsigned long) r_symndx,
1939 (unsigned long) nsyms, irela->r_offset, sec->name);
1940 bfd_set_error (bfd_error_bad_value);
1941 return FALSE;
1942 }
1943 irela += bed->s->int_rels_per_ext_rel;
1944 erela += shdr->sh_entsize;
1945 }
1946
1947 return TRUE;
1948 }
1949
1950 /* Read and swap the relocs for a section O. They may have been
1951 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1952 not NULL, they are used as buffers to read into. They are known to
1953 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1954 the return value is allocated using either malloc or bfd_alloc,
1955 according to the KEEP_MEMORY argument. If O has two relocation
1956 sections (both REL and RELA relocations), then the REL_HDR
1957 relocations will appear first in INTERNAL_RELOCS, followed by the
1958 REL_HDR2 relocations. */
1959
1960 Elf_Internal_Rela *
1961 _bfd_elf_link_read_relocs (bfd *abfd,
1962 asection *o,
1963 void *external_relocs,
1964 Elf_Internal_Rela *internal_relocs,
1965 bfd_boolean keep_memory)
1966 {
1967 Elf_Internal_Shdr *rel_hdr;
1968 void *alloc1 = NULL;
1969 Elf_Internal_Rela *alloc2 = NULL;
1970 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1971
1972 if (elf_section_data (o)->relocs != NULL)
1973 return elf_section_data (o)->relocs;
1974
1975 if (o->reloc_count == 0)
1976 return NULL;
1977
1978 rel_hdr = &elf_section_data (o)->rel_hdr;
1979
1980 if (internal_relocs == NULL)
1981 {
1982 bfd_size_type size;
1983
1984 size = o->reloc_count;
1985 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
1986 if (keep_memory)
1987 internal_relocs = bfd_alloc (abfd, size);
1988 else
1989 internal_relocs = alloc2 = bfd_malloc (size);
1990 if (internal_relocs == NULL)
1991 goto error_return;
1992 }
1993
1994 if (external_relocs == NULL)
1995 {
1996 bfd_size_type size = rel_hdr->sh_size;
1997
1998 if (elf_section_data (o)->rel_hdr2)
1999 size += elf_section_data (o)->rel_hdr2->sh_size;
2000 alloc1 = bfd_malloc (size);
2001 if (alloc1 == NULL)
2002 goto error_return;
2003 external_relocs = alloc1;
2004 }
2005
2006 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
2007 external_relocs,
2008 internal_relocs))
2009 goto error_return;
2010 if (!elf_link_read_relocs_from_section
2011 (abfd, o,
2012 elf_section_data (o)->rel_hdr2,
2013 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2014 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2015 * bed->s->int_rels_per_ext_rel)))
2016 goto error_return;
2017
2018 /* Cache the results for next time, if we can. */
2019 if (keep_memory)
2020 elf_section_data (o)->relocs = internal_relocs;
2021
2022 if (alloc1 != NULL)
2023 free (alloc1);
2024
2025 /* Don't free alloc2, since if it was allocated we are passing it
2026 back (under the name of internal_relocs). */
2027
2028 return internal_relocs;
2029
2030 error_return:
2031 if (alloc1 != NULL)
2032 free (alloc1);
2033 if (alloc2 != NULL)
2034 free (alloc2);
2035 return NULL;
2036 }
2037
2038 /* Compute the size of, and allocate space for, REL_HDR which is the
2039 section header for a section containing relocations for O. */
2040
2041 bfd_boolean
2042 _bfd_elf_link_size_reloc_section (bfd *abfd,
2043 Elf_Internal_Shdr *rel_hdr,
2044 asection *o)
2045 {
2046 bfd_size_type reloc_count;
2047 bfd_size_type num_rel_hashes;
2048
2049 /* Figure out how many relocations there will be. */
2050 if (rel_hdr == &elf_section_data (o)->rel_hdr)
2051 reloc_count = elf_section_data (o)->rel_count;
2052 else
2053 reloc_count = elf_section_data (o)->rel_count2;
2054
2055 num_rel_hashes = o->reloc_count;
2056 if (num_rel_hashes < reloc_count)
2057 num_rel_hashes = reloc_count;
2058
2059 /* That allows us to calculate the size of the section. */
2060 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
2061
2062 /* The contents field must last into write_object_contents, so we
2063 allocate it with bfd_alloc rather than malloc. Also since we
2064 cannot be sure that the contents will actually be filled in,
2065 we zero the allocated space. */
2066 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
2067 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2068 return FALSE;
2069
2070 /* We only allocate one set of hash entries, so we only do it the
2071 first time we are called. */
2072 if (elf_section_data (o)->rel_hashes == NULL
2073 && num_rel_hashes)
2074 {
2075 struct elf_link_hash_entry **p;
2076
2077 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
2078 if (p == NULL)
2079 return FALSE;
2080
2081 elf_section_data (o)->rel_hashes = p;
2082 }
2083
2084 return TRUE;
2085 }
2086
2087 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2088 originated from the section given by INPUT_REL_HDR) to the
2089 OUTPUT_BFD. */
2090
2091 bfd_boolean
2092 _bfd_elf_link_output_relocs (bfd *output_bfd,
2093 asection *input_section,
2094 Elf_Internal_Shdr *input_rel_hdr,
2095 Elf_Internal_Rela *internal_relocs)
2096 {
2097 Elf_Internal_Rela *irela;
2098 Elf_Internal_Rela *irelaend;
2099 bfd_byte *erel;
2100 Elf_Internal_Shdr *output_rel_hdr;
2101 asection *output_section;
2102 unsigned int *rel_countp = NULL;
2103 const struct elf_backend_data *bed;
2104 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2105
2106 output_section = input_section->output_section;
2107 output_rel_hdr = NULL;
2108
2109 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2110 == input_rel_hdr->sh_entsize)
2111 {
2112 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2113 rel_countp = &elf_section_data (output_section)->rel_count;
2114 }
2115 else if (elf_section_data (output_section)->rel_hdr2
2116 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2117 == input_rel_hdr->sh_entsize))
2118 {
2119 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2120 rel_countp = &elf_section_data (output_section)->rel_count2;
2121 }
2122 else
2123 {
2124 (*_bfd_error_handler)
2125 (_("%s: relocation size mismatch in %s section %s"),
2126 bfd_get_filename (output_bfd),
2127 bfd_archive_filename (input_section->owner),
2128 input_section->name);
2129 bfd_set_error (bfd_error_wrong_object_format);
2130 return FALSE;
2131 }
2132
2133 bed = get_elf_backend_data (output_bfd);
2134 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2135 swap_out = bed->s->swap_reloc_out;
2136 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2137 swap_out = bed->s->swap_reloca_out;
2138 else
2139 abort ();
2140
2141 erel = output_rel_hdr->contents;
2142 erel += *rel_countp * input_rel_hdr->sh_entsize;
2143 irela = internal_relocs;
2144 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2145 * bed->s->int_rels_per_ext_rel);
2146 while (irela < irelaend)
2147 {
2148 (*swap_out) (output_bfd, irela, erel);
2149 irela += bed->s->int_rels_per_ext_rel;
2150 erel += input_rel_hdr->sh_entsize;
2151 }
2152
2153 /* Bump the counter, so that we know where to add the next set of
2154 relocations. */
2155 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2156
2157 return TRUE;
2158 }
2159 \f
2160 /* Fix up the flags for a symbol. This handles various cases which
2161 can only be fixed after all the input files are seen. This is
2162 currently called by both adjust_dynamic_symbol and
2163 assign_sym_version, which is unnecessary but perhaps more robust in
2164 the face of future changes. */
2165
2166 bfd_boolean
2167 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2168 struct elf_info_failed *eif)
2169 {
2170 /* If this symbol was mentioned in a non-ELF file, try to set
2171 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2172 permit a non-ELF file to correctly refer to a symbol defined in
2173 an ELF dynamic object. */
2174 if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) != 0)
2175 {
2176 while (h->root.type == bfd_link_hash_indirect)
2177 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2178
2179 if (h->root.type != bfd_link_hash_defined
2180 && h->root.type != bfd_link_hash_defweak)
2181 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
2182 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
2183 else
2184 {
2185 if (h->root.u.def.section->owner != NULL
2186 && (bfd_get_flavour (h->root.u.def.section->owner)
2187 == bfd_target_elf_flavour))
2188 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
2189 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
2190 else
2191 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2192 }
2193
2194 if (h->dynindx == -1
2195 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2196 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0))
2197 {
2198 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
2199 {
2200 eif->failed = TRUE;
2201 return FALSE;
2202 }
2203 }
2204 }
2205 else
2206 {
2207 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
2208 was first seen in a non-ELF file. Fortunately, if the symbol
2209 was first seen in an ELF file, we're probably OK unless the
2210 symbol was defined in a non-ELF file. Catch that case here.
2211 FIXME: We're still in trouble if the symbol was first seen in
2212 a dynamic object, and then later in a non-ELF regular object. */
2213 if ((h->root.type == bfd_link_hash_defined
2214 || h->root.type == bfd_link_hash_defweak)
2215 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
2216 && (h->root.u.def.section->owner != NULL
2217 ? (bfd_get_flavour (h->root.u.def.section->owner)
2218 != bfd_target_elf_flavour)
2219 : (bfd_is_abs_section (h->root.u.def.section)
2220 && (h->elf_link_hash_flags
2221 & ELF_LINK_HASH_DEF_DYNAMIC) == 0)))
2222 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2223 }
2224
2225 /* If this is a final link, and the symbol was defined as a common
2226 symbol in a regular object file, and there was no definition in
2227 any dynamic object, then the linker will have allocated space for
2228 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
2229 flag will not have been set. */
2230 if (h->root.type == bfd_link_hash_defined
2231 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
2232 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
2233 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
2234 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2235 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2236
2237 /* If -Bsymbolic was used (which means to bind references to global
2238 symbols to the definition within the shared object), and this
2239 symbol was defined in a regular object, then it actually doesn't
2240 need a PLT entry. Likewise, if the symbol has non-default
2241 visibility. If the symbol has hidden or internal visibility, we
2242 will force it local. */
2243 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
2244 && eif->info->shared
2245 && is_elf_hash_table (eif->info)
2246 && (eif->info->symbolic
2247 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2248 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
2249 {
2250 const struct elf_backend_data *bed;
2251 bfd_boolean force_local;
2252
2253 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2254
2255 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2256 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2257 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2258 }
2259
2260 /* If a weak undefined symbol has non-default visibility, we also
2261 hide it from the dynamic linker. */
2262 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2263 && h->root.type == bfd_link_hash_undefweak)
2264 {
2265 const struct elf_backend_data *bed;
2266 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2267 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2268 }
2269
2270 /* If this is a weak defined symbol in a dynamic object, and we know
2271 the real definition in the dynamic object, copy interesting flags
2272 over to the real definition. */
2273 if (h->weakdef != NULL)
2274 {
2275 struct elf_link_hash_entry *weakdef;
2276
2277 weakdef = h->weakdef;
2278 if (h->root.type == bfd_link_hash_indirect)
2279 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2280
2281 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2282 || h->root.type == bfd_link_hash_defweak);
2283 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2284 || weakdef->root.type == bfd_link_hash_defweak);
2285 BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC);
2286
2287 /* If the real definition is defined by a regular object file,
2288 don't do anything special. See the longer description in
2289 _bfd_elf_adjust_dynamic_symbol, below. */
2290 if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
2291 h->weakdef = NULL;
2292 else
2293 {
2294 const struct elf_backend_data *bed;
2295
2296 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2297 (*bed->elf_backend_copy_indirect_symbol) (bed, weakdef, h);
2298 }
2299 }
2300
2301 return TRUE;
2302 }
2303
2304 /* Make the backend pick a good value for a dynamic symbol. This is
2305 called via elf_link_hash_traverse, and also calls itself
2306 recursively. */
2307
2308 bfd_boolean
2309 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2310 {
2311 struct elf_info_failed *eif = data;
2312 bfd *dynobj;
2313 const struct elf_backend_data *bed;
2314
2315 if (! is_elf_hash_table (eif->info))
2316 return FALSE;
2317
2318 if (h->root.type == bfd_link_hash_warning)
2319 {
2320 h->plt = elf_hash_table (eif->info)->init_offset;
2321 h->got = elf_hash_table (eif->info)->init_offset;
2322
2323 /* When warning symbols are created, they **replace** the "real"
2324 entry in the hash table, thus we never get to see the real
2325 symbol in a hash traversal. So look at it now. */
2326 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2327 }
2328
2329 /* Ignore indirect symbols. These are added by the versioning code. */
2330 if (h->root.type == bfd_link_hash_indirect)
2331 return TRUE;
2332
2333 /* Fix the symbol flags. */
2334 if (! _bfd_elf_fix_symbol_flags (h, eif))
2335 return FALSE;
2336
2337 /* If this symbol does not require a PLT entry, and it is not
2338 defined by a dynamic object, or is not referenced by a regular
2339 object, ignore it. We do have to handle a weak defined symbol,
2340 even if no regular object refers to it, if we decided to add it
2341 to the dynamic symbol table. FIXME: Do we normally need to worry
2342 about symbols which are defined by one dynamic object and
2343 referenced by another one? */
2344 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0
2345 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
2346 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
2347 || ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
2348 && (h->weakdef == NULL || h->weakdef->dynindx == -1))))
2349 {
2350 h->plt = elf_hash_table (eif->info)->init_offset;
2351 return TRUE;
2352 }
2353
2354 /* If we've already adjusted this symbol, don't do it again. This
2355 can happen via a recursive call. */
2356 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
2357 return TRUE;
2358
2359 /* Don't look at this symbol again. Note that we must set this
2360 after checking the above conditions, because we may look at a
2361 symbol once, decide not to do anything, and then get called
2362 recursively later after REF_REGULAR is set below. */
2363 h->elf_link_hash_flags |= ELF_LINK_HASH_DYNAMIC_ADJUSTED;
2364
2365 /* If this is a weak definition, and we know a real definition, and
2366 the real symbol is not itself defined by a regular object file,
2367 then get a good value for the real definition. We handle the
2368 real symbol first, for the convenience of the backend routine.
2369
2370 Note that there is a confusing case here. If the real definition
2371 is defined by a regular object file, we don't get the real symbol
2372 from the dynamic object, but we do get the weak symbol. If the
2373 processor backend uses a COPY reloc, then if some routine in the
2374 dynamic object changes the real symbol, we will not see that
2375 change in the corresponding weak symbol. This is the way other
2376 ELF linkers work as well, and seems to be a result of the shared
2377 library model.
2378
2379 I will clarify this issue. Most SVR4 shared libraries define the
2380 variable _timezone and define timezone as a weak synonym. The
2381 tzset call changes _timezone. If you write
2382 extern int timezone;
2383 int _timezone = 5;
2384 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2385 you might expect that, since timezone is a synonym for _timezone,
2386 the same number will print both times. However, if the processor
2387 backend uses a COPY reloc, then actually timezone will be copied
2388 into your process image, and, since you define _timezone
2389 yourself, _timezone will not. Thus timezone and _timezone will
2390 wind up at different memory locations. The tzset call will set
2391 _timezone, leaving timezone unchanged. */
2392
2393 if (h->weakdef != NULL)
2394 {
2395 /* If we get to this point, we know there is an implicit
2396 reference by a regular object file via the weak symbol H.
2397 FIXME: Is this really true? What if the traversal finds
2398 H->WEAKDEF before it finds H? */
2399 h->weakdef->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
2400
2401 if (! _bfd_elf_adjust_dynamic_symbol (h->weakdef, eif))
2402 return FALSE;
2403 }
2404
2405 /* If a symbol has no type and no size and does not require a PLT
2406 entry, then we are probably about to do the wrong thing here: we
2407 are probably going to create a COPY reloc for an empty object.
2408 This case can arise when a shared object is built with assembly
2409 code, and the assembly code fails to set the symbol type. */
2410 if (h->size == 0
2411 && h->type == STT_NOTYPE
2412 && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0)
2413 (*_bfd_error_handler)
2414 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2415 h->root.root.string);
2416
2417 dynobj = elf_hash_table (eif->info)->dynobj;
2418 bed = get_elf_backend_data (dynobj);
2419 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2420 {
2421 eif->failed = TRUE;
2422 return FALSE;
2423 }
2424
2425 return TRUE;
2426 }
2427
2428 /* Adjust all external symbols pointing into SEC_MERGE sections
2429 to reflect the object merging within the sections. */
2430
2431 bfd_boolean
2432 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2433 {
2434 asection *sec;
2435
2436 if (h->root.type == bfd_link_hash_warning)
2437 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2438
2439 if ((h->root.type == bfd_link_hash_defined
2440 || h->root.type == bfd_link_hash_defweak)
2441 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2442 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2443 {
2444 bfd *output_bfd = data;
2445
2446 h->root.u.def.value =
2447 _bfd_merged_section_offset (output_bfd,
2448 &h->root.u.def.section,
2449 elf_section_data (sec)->sec_info,
2450 h->root.u.def.value, 0);
2451 }
2452
2453 return TRUE;
2454 }
2455
2456 /* Returns false if the symbol referred to by H should be considered
2457 to resolve local to the current module, and true if it should be
2458 considered to bind dynamically. */
2459
2460 bfd_boolean
2461 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2462 struct bfd_link_info *info,
2463 bfd_boolean ignore_protected)
2464 {
2465 bfd_boolean binding_stays_local_p;
2466
2467 if (h == NULL)
2468 return FALSE;
2469
2470 while (h->root.type == bfd_link_hash_indirect
2471 || h->root.type == bfd_link_hash_warning)
2472 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2473
2474 /* If it was forced local, then clearly it's not dynamic. */
2475 if (h->dynindx == -1)
2476 return FALSE;
2477 if (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
2478 return FALSE;
2479
2480 /* Identify the cases where name binding rules say that a
2481 visible symbol resolves locally. */
2482 binding_stays_local_p = info->executable || info->symbolic;
2483
2484 switch (ELF_ST_VISIBILITY (h->other))
2485 {
2486 case STV_INTERNAL:
2487 case STV_HIDDEN:
2488 return FALSE;
2489
2490 case STV_PROTECTED:
2491 /* Proper resolution for function pointer equality may require
2492 that these symbols perhaps be resolved dynamically, even though
2493 we should be resolving them to the current module. */
2494 if (!ignore_protected)
2495 binding_stays_local_p = TRUE;
2496 break;
2497
2498 default:
2499 break;
2500 }
2501
2502 /* If it isn't defined locally, then clearly it's dynamic. */
2503 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2504 return TRUE;
2505
2506 /* Otherwise, the symbol is dynamic if binding rules don't tell
2507 us that it remains local. */
2508 return !binding_stays_local_p;
2509 }
2510
2511 /* Return true if the symbol referred to by H should be considered
2512 to resolve local to the current module, and false otherwise. Differs
2513 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2514 undefined symbols and weak symbols. */
2515
2516 bfd_boolean
2517 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2518 struct bfd_link_info *info,
2519 bfd_boolean local_protected)
2520 {
2521 /* If it's a local sym, of course we resolve locally. */
2522 if (h == NULL)
2523 return TRUE;
2524
2525 /* If we don't have a definition in a regular file, then we can't
2526 resolve locally. The sym is either undefined or dynamic. */
2527 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2528 return FALSE;
2529
2530 /* Forced local symbols resolve locally. */
2531 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
2532 return TRUE;
2533
2534 /* As do non-dynamic symbols. */
2535 if (h->dynindx == -1)
2536 return TRUE;
2537
2538 /* At this point, we know the symbol is defined and dynamic. In an
2539 executable it must resolve locally, likewise when building symbolic
2540 shared libraries. */
2541 if (info->executable || info->symbolic)
2542 return TRUE;
2543
2544 /* Now deal with defined dynamic symbols in shared libraries. Ones
2545 with default visibility might not resolve locally. */
2546 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2547 return FALSE;
2548
2549 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2550 if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED)
2551 return TRUE;
2552
2553 /* Function pointer equality tests may require that STV_PROTECTED
2554 symbols be treated as dynamic symbols, even when we know that the
2555 dynamic linker will resolve them locally. */
2556 return local_protected;
2557 }