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