]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elflink.c
2003-05-16 Andrew Cagney <cagney@redhat.com>
[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
858 && ELF_ST_VISIBILITY (h->other)
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
874 && ELF_ST_VISIBILITY (sym->st_other)
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
AM
886 h->elf_link_hash_flags &= ~ELF_LINK_HASH_DEF_DYNAMIC;
887 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
888 }
889 /* FIXME: Should we check type and size for protected symbol? */
890 h->size = 0;
891 h->type = 0;
892 return TRUE;
893 }
14a793b2 894
45d6a902
AM
895 /* We need to treat weak definiton right, depending on if there is a
896 definition from a dynamic object. */
897 if (bind == STB_WEAK)
898 {
899 if (olddef)
900 {
901 newweakdef = TRUE;
902 newweakundef = FALSE;
903 }
904 else
905 {
906 newweakdef = FALSE;
907 newweakundef = TRUE;
908 }
909 }
910 else
911 newweakdef = newweakundef = FALSE;
14a793b2 912
45d6a902
AM
913 /* If the new weak definition comes from a relocatable file and the
914 old symbol comes from a dynamic object, we treat the new one as
915 strong. */
916 if (newweakdef && !newdyn && olddyn)
917 newweakdef = FALSE;
252b5132 918
45d6a902
AM
919 if (h->root.type == bfd_link_hash_defweak)
920 {
921 oldweakdef = TRUE;
922 oldweakundef = FALSE;
923 }
924 else if (h->root.type == bfd_link_hash_undefweak)
925 {
926 oldweakdef = FALSE;
927 oldweakundef = TRUE;
928 }
929 else
930 oldweakdef = oldweakundef = FALSE;
931
932 /* If the old weak definition comes from a relocatable file and the
933 new symbol comes from a dynamic object, we treat the old one as
934 strong. */
935 if (oldweakdef && !olddyn && newdyn)
936 oldweakdef = FALSE;
937
938 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
939 symbol, respectively, appears to be a common symbol in a dynamic
940 object. If a symbol appears in an uninitialized section, and is
941 not weak, and is not a function, then it may be a common symbol
942 which was resolved when the dynamic object was created. We want
943 to treat such symbols specially, because they raise special
944 considerations when setting the symbol size: if the symbol
945 appears as a common symbol in a regular object, and the size in
946 the regular object is larger, we must make sure that we use the
947 larger size. This problematic case can always be avoided in C,
948 but it must be handled correctly when using Fortran shared
949 libraries.
950
951 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
952 likewise for OLDDYNCOMMON and OLDDEF.
953
954 Note that this test is just a heuristic, and that it is quite
955 possible to have an uninitialized symbol in a shared object which
956 is really a definition, rather than a common symbol. This could
957 lead to some minor confusion when the symbol really is a common
958 symbol in some regular object. However, I think it will be
959 harmless. */
960
961 if (newdyn
962 && newdef
963 && (sec->flags & SEC_ALLOC) != 0
964 && (sec->flags & SEC_LOAD) == 0
965 && sym->st_size > 0
966 && !newweakdef
967 && !newweakundef
968 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
969 newdyncommon = TRUE;
970 else
971 newdyncommon = FALSE;
972
973 if (olddyn
974 && olddef
975 && h->root.type == bfd_link_hash_defined
976 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
977 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
978 && (h->root.u.def.section->flags & SEC_LOAD) == 0
979 && h->size > 0
980 && h->type != STT_FUNC)
981 olddyncommon = TRUE;
982 else
983 olddyncommon = FALSE;
984
985 /* It's OK to change the type if either the existing symbol or the
986 new symbol is weak unless it comes from a DT_NEEDED entry of
987 a shared object, in which case, the DT_NEEDED entry may not be
988 required at the run time. */
989
990 if ((! dt_needed && oldweakdef)
991 || oldweakundef
992 || newweakdef
993 || newweakundef)
994 *type_change_ok = TRUE;
995
996 /* It's OK to change the size if either the existing symbol or the
997 new symbol is weak, or if the old symbol is undefined. */
998
999 if (*type_change_ok
1000 || h->root.type == bfd_link_hash_undefined)
1001 *size_change_ok = TRUE;
1002
1003 /* If both the old and the new symbols look like common symbols in a
1004 dynamic object, set the size of the symbol to the larger of the
1005 two. */
1006
1007 if (olddyncommon
1008 && newdyncommon
1009 && sym->st_size != h->size)
1010 {
1011 /* Since we think we have two common symbols, issue a multiple
1012 common warning if desired. Note that we only warn if the
1013 size is different. If the size is the same, we simply let
1014 the old symbol override the new one as normally happens with
1015 symbols defined in dynamic objects. */
1016
1017 if (! ((*info->callbacks->multiple_common)
1018 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1019 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1020 return FALSE;
252b5132 1021
45d6a902
AM
1022 if (sym->st_size > h->size)
1023 h->size = sym->st_size;
252b5132 1024
45d6a902 1025 *size_change_ok = TRUE;
252b5132
RH
1026 }
1027
45d6a902
AM
1028 /* If we are looking at a dynamic object, and we have found a
1029 definition, we need to see if the symbol was already defined by
1030 some other object. If so, we want to use the existing
1031 definition, and we do not want to report a multiple symbol
1032 definition error; we do this by clobbering *PSEC to be
1033 bfd_und_section_ptr.
1034
1035 We treat a common symbol as a definition if the symbol in the
1036 shared library is a function, since common symbols always
1037 represent variables; this can cause confusion in principle, but
1038 any such confusion would seem to indicate an erroneous program or
1039 shared library. We also permit a common symbol in a regular
1040 object to override a weak symbol in a shared object.
1041
1042 We prefer a non-weak definition in a shared library to a weak
1043 definition in the executable unless it comes from a DT_NEEDED
1044 entry of a shared object, in which case, the DT_NEEDED entry
1045 may not be required at the run time. */
1046
1047 if (newdyn
1048 && newdef
1049 && (olddef
1050 || (h->root.type == bfd_link_hash_common
1051 && (newweakdef
1052 || newweakundef
1053 || ELF_ST_TYPE (sym->st_info) == STT_FUNC)))
1054 && (!oldweakdef
1055 || dt_needed
1056 || newweakdef
1057 || newweakundef))
1058 {
1059 *override = TRUE;
1060 newdef = FALSE;
1061 newdyncommon = FALSE;
252b5132 1062
45d6a902
AM
1063 *psec = sec = bfd_und_section_ptr;
1064 *size_change_ok = TRUE;
252b5132 1065
45d6a902
AM
1066 /* If we get here when the old symbol is a common symbol, then
1067 we are explicitly letting it override a weak symbol or
1068 function in a dynamic object, and we don't want to warn about
1069 a type change. If the old symbol is a defined symbol, a type
1070 change warning may still be appropriate. */
252b5132 1071
45d6a902
AM
1072 if (h->root.type == bfd_link_hash_common)
1073 *type_change_ok = TRUE;
1074 }
1075
1076 /* Handle the special case of an old common symbol merging with a
1077 new symbol which looks like a common symbol in a shared object.
1078 We change *PSEC and *PVALUE to make the new symbol look like a
1079 common symbol, and let _bfd_generic_link_add_one_symbol will do
1080 the right thing. */
1081
1082 if (newdyncommon
1083 && h->root.type == bfd_link_hash_common)
1084 {
1085 *override = TRUE;
1086 newdef = FALSE;
1087 newdyncommon = FALSE;
1088 *pvalue = sym->st_size;
1089 *psec = sec = bfd_com_section_ptr;
1090 *size_change_ok = TRUE;
1091 }
1092
1093 /* If the old symbol is from a dynamic object, and the new symbol is
1094 a definition which is not from a dynamic object, then the new
1095 symbol overrides the old symbol. Symbols from regular files
1096 always take precedence over symbols from dynamic objects, even if
1097 they are defined after the dynamic object in the link.
1098
1099 As above, we again permit a common symbol in a regular object to
1100 override a definition in a shared object if the shared object
1101 symbol is a function or is weak.
1102
1103 As above, we permit a non-weak definition in a shared object to
1104 override a weak definition in a regular object. */
1105
1106 flip = NULL;
1107 if (! newdyn
1108 && (newdef
1109 || (bfd_is_com_section (sec)
1110 && (oldweakdef || h->type == STT_FUNC)))
1111 && olddyn
1112 && olddef
1113 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1114 && ((!newweakdef && !newweakundef) || oldweakdef))
1115 {
1116 /* Change the hash table entry to undefined, and let
1117 _bfd_generic_link_add_one_symbol do the right thing with the
1118 new definition. */
1119
1120 h->root.type = bfd_link_hash_undefined;
1121 h->root.u.undef.abfd = h->root.u.def.section->owner;
1122 *size_change_ok = TRUE;
1123
1124 olddef = FALSE;
1125 olddyncommon = FALSE;
1126
1127 /* We again permit a type change when a common symbol may be
1128 overriding a function. */
1129
1130 if (bfd_is_com_section (sec))
1131 *type_change_ok = TRUE;
1132
1133 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1134 flip = *sym_hash;
1135 else
1136 /* This union may have been set to be non-NULL when this symbol
1137 was seen in a dynamic object. We must force the union to be
1138 NULL, so that it is correct for a regular symbol. */
1139 h->verinfo.vertree = NULL;
1140 }
1141
1142 /* Handle the special case of a new common symbol merging with an
1143 old symbol that looks like it might be a common symbol defined in
1144 a shared object. Note that we have already handled the case in
1145 which a new common symbol should simply override the definition
1146 in the shared library. */
1147
1148 if (! newdyn
1149 && bfd_is_com_section (sec)
1150 && olddyncommon)
1151 {
1152 /* It would be best if we could set the hash table entry to a
1153 common symbol, but we don't know what to use for the section
1154 or the alignment. */
1155 if (! ((*info->callbacks->multiple_common)
1156 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1157 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1158 return FALSE;
1159
1160 /* If the predumed common symbol in the dynamic object is
1161 larger, pretend that the new symbol has its size. */
1162
1163 if (h->size > *pvalue)
1164 *pvalue = h->size;
1165
1166 /* FIXME: We no longer know the alignment required by the symbol
1167 in the dynamic object, so we just wind up using the one from
1168 the regular object. */
1169
1170 olddef = FALSE;
1171 olddyncommon = FALSE;
1172
1173 h->root.type = bfd_link_hash_undefined;
1174 h->root.u.undef.abfd = h->root.u.def.section->owner;
1175
1176 *size_change_ok = TRUE;
1177 *type_change_ok = TRUE;
1178
1179 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1180 flip = *sym_hash;
1181 else
1182 h->verinfo.vertree = NULL;
1183 }
1184
1185 if (flip != NULL)
1186 {
1187 /* Handle the case where we had a versioned symbol in a dynamic
1188 library and now find a definition in a normal object. In this
1189 case, we make the versioned symbol point to the normal one. */
1190 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1191 flip->root.type = h->root.type;
1192 h->root.type = bfd_link_hash_indirect;
1193 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1194 (*bed->elf_backend_copy_indirect_symbol) (bed, flip, h);
1195 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1196 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
1197 {
1198 h->elf_link_hash_flags &= ~ELF_LINK_HASH_DEF_DYNAMIC;
1199 flip->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1200 }
1201 }
1202
1203 /* Handle the special case of a weak definition in a regular object
1204 followed by a non-weak definition in a shared object. In this
1205 case, we prefer the definition in the shared object unless it
1206 comes from a DT_NEEDED entry of a shared object, in which case,
1207 the DT_NEEDED entry may not be required at the run time. */
1208 if (olddef
1209 && ! dt_needed
1210 && oldweakdef
1211 && newdef
1212 && newdyn
1213 && !newweakdef
1214 && !newweakundef)
1215 {
1216 /* To make this work we have to frob the flags so that the rest
1217 of the code does not think we are using the regular
1218 definition. */
1219 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
1220 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
1221 else if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
1222 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1223 h->elf_link_hash_flags &= ~ (ELF_LINK_HASH_DEF_REGULAR
1224 | ELF_LINK_HASH_DEF_DYNAMIC);
1225
1226 /* If H is the target of an indirection, we want the caller to
1227 use H rather than the indirect symbol. Otherwise if we are
1228 defining a new indirect symbol we will wind up attaching it
1229 to the entry we are overriding. */
1230 *sym_hash = h;
1231 }
1232
1233 /* Handle the special case of a non-weak definition in a shared
1234 object followed by a weak definition in a regular object. In
1235 this case we prefer the definition in the shared object. To make
1236 this work we have to tell the caller to not treat the new symbol
1237 as a definition. */
1238 if (olddef
1239 && olddyn
1240 && !oldweakdef
1241 && newdef
1242 && ! newdyn
1243 && (newweakdef || newweakundef))
1244 *override = TRUE;
1245
1246 return TRUE;
1247}
1248
1249/* This function is called to create an indirect symbol from the
1250 default for the symbol with the default version if needed. The
1251 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1252 set DYNSYM if the new indirect symbol is dynamic. DT_NEEDED
1253 indicates if it comes from a DT_NEEDED entry of a shared object. */
1254
1255bfd_boolean
1256_bfd_elf_add_default_symbol (abfd, info, h, name, sym, psec, value,
1257 dynsym, override, dt_needed)
1258 bfd *abfd;
1259 struct bfd_link_info *info;
1260 struct elf_link_hash_entry *h;
1261 const char *name;
1262 Elf_Internal_Sym *sym;
1263 asection **psec;
1264 bfd_vma *value;
1265 bfd_boolean *dynsym;
1266 bfd_boolean override;
1267 bfd_boolean dt_needed;
1268{
1269 bfd_boolean type_change_ok;
1270 bfd_boolean size_change_ok;
1271 bfd_boolean skip;
1272 char *shortname;
1273 struct elf_link_hash_entry *hi;
1274 struct bfd_link_hash_entry *bh;
1275 struct elf_backend_data *bed;
1276 bfd_boolean collect;
1277 bfd_boolean dynamic;
1278 char *p;
1279 size_t len, shortlen;
1280 asection *sec;
1281
1282 /* If this symbol has a version, and it is the default version, we
1283 create an indirect symbol from the default name to the fully
1284 decorated name. This will cause external references which do not
1285 specify a version to be bound to this version of the symbol. */
1286 p = strchr (name, ELF_VER_CHR);
1287 if (p == NULL || p[1] != ELF_VER_CHR)
1288 return TRUE;
1289
1290 if (override)
1291 {
1292 /* We are overridden by an old defition. We need to check if we
1293 need to create the indirect symbol from the default name. */
1294 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1295 FALSE, FALSE);
1296 BFD_ASSERT (hi != NULL);
1297 if (hi == h)
1298 return TRUE;
1299 while (hi->root.type == bfd_link_hash_indirect
1300 || hi->root.type == bfd_link_hash_warning)
1301 {
1302 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1303 if (hi == h)
1304 return TRUE;
1305 }
1306 }
1307
1308 bed = get_elf_backend_data (abfd);
1309 collect = bed->collect;
1310 dynamic = (abfd->flags & DYNAMIC) != 0;
1311
1312 shortlen = p - name;
1313 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1314 if (shortname == NULL)
1315 return FALSE;
1316 memcpy (shortname, name, shortlen);
1317 shortname[shortlen] = '\0';
1318
1319 /* We are going to create a new symbol. Merge it with any existing
1320 symbol with this name. For the purposes of the merge, act as
1321 though we were defining the symbol we just defined, although we
1322 actually going to define an indirect symbol. */
1323 type_change_ok = FALSE;
1324 size_change_ok = FALSE;
1325 sec = *psec;
1326 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1327 &hi, &skip, &override, &type_change_ok,
1328 &size_change_ok, dt_needed))
1329 return FALSE;
1330
1331 if (skip)
1332 goto nondefault;
1333
1334 if (! override)
1335 {
1336 bh = &hi->root;
1337 if (! (_bfd_generic_link_add_one_symbol
1338 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1339 (bfd_vma) 0, name, FALSE, collect, &bh)))
1340 return FALSE;
1341 hi = (struct elf_link_hash_entry *) bh;
1342 }
1343 else
1344 {
1345 /* In this case the symbol named SHORTNAME is overriding the
1346 indirect symbol we want to add. We were planning on making
1347 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1348 is the name without a version. NAME is the fully versioned
1349 name, and it is the default version.
1350
1351 Overriding means that we already saw a definition for the
1352 symbol SHORTNAME in a regular object, and it is overriding
1353 the symbol defined in the dynamic object.
1354
1355 When this happens, we actually want to change NAME, the
1356 symbol we just added, to refer to SHORTNAME. This will cause
1357 references to NAME in the shared object to become references
1358 to SHORTNAME in the regular object. This is what we expect
1359 when we override a function in a shared object: that the
1360 references in the shared object will be mapped to the
1361 definition in the regular object. */
1362
1363 while (hi->root.type == bfd_link_hash_indirect
1364 || hi->root.type == bfd_link_hash_warning)
1365 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1366
1367 h->root.type = bfd_link_hash_indirect;
1368 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1369 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
1370 {
1371 h->elf_link_hash_flags &=~ ELF_LINK_HASH_DEF_DYNAMIC;
1372 hi->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1373 if (hi->elf_link_hash_flags
1374 & (ELF_LINK_HASH_REF_REGULAR
1375 | ELF_LINK_HASH_DEF_REGULAR))
1376 {
1377 if (! _bfd_elf_link_record_dynamic_symbol (info, hi))
1378 return FALSE;
1379 }
1380 }
1381
1382 /* Now set HI to H, so that the following code will set the
1383 other fields correctly. */
1384 hi = h;
1385 }
1386
1387 /* If there is a duplicate definition somewhere, then HI may not
1388 point to an indirect symbol. We will have reported an error to
1389 the user in that case. */
1390
1391 if (hi->root.type == bfd_link_hash_indirect)
1392 {
1393 struct elf_link_hash_entry *ht;
1394
1395 /* If the symbol became indirect, then we assume that we have
1396 not seen a definition before. */
1397 BFD_ASSERT ((hi->elf_link_hash_flags
1398 & (ELF_LINK_HASH_DEF_DYNAMIC
1399 | ELF_LINK_HASH_DEF_REGULAR)) == 0);
1400
1401 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1402 (*bed->elf_backend_copy_indirect_symbol) (bed, ht, hi);
1403
1404 /* See if the new flags lead us to realize that the symbol must
1405 be dynamic. */
1406 if (! *dynsym)
1407 {
1408 if (! dynamic)
1409 {
1410 if (info->shared
1411 || ((hi->elf_link_hash_flags
1412 & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1413 *dynsym = TRUE;
1414 }
1415 else
1416 {
1417 if ((hi->elf_link_hash_flags
1418 & ELF_LINK_HASH_REF_REGULAR) != 0)
1419 *dynsym = TRUE;
1420 }
1421 }
1422 }
1423
1424 /* We also need to define an indirection from the nondefault version
1425 of the symbol. */
1426
1427nondefault:
1428 len = strlen (name);
1429 shortname = bfd_hash_allocate (&info->hash->table, len);
1430 if (shortname == NULL)
1431 return FALSE;
1432 memcpy (shortname, name, shortlen);
1433 memcpy (shortname + shortlen, p + 1, len - shortlen);
1434
1435 /* Once again, merge with any existing symbol. */
1436 type_change_ok = FALSE;
1437 size_change_ok = FALSE;
1438 sec = *psec;
1439 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1440 &hi, &skip, &override, &type_change_ok,
1441 &size_change_ok, dt_needed))
1442 return FALSE;
1443
1444 if (skip)
1445 return TRUE;
1446
1447 if (override)
1448 {
1449 /* Here SHORTNAME is a versioned name, so we don't expect to see
1450 the type of override we do in the case above unless it is
1451 overridden by a versioned definiton. */
1452 if (hi->root.type != bfd_link_hash_defined
1453 && hi->root.type != bfd_link_hash_defweak)
1454 (*_bfd_error_handler)
1455 (_("%s: warning: unexpected redefinition of indirect versioned symbol `%s'"),
1456 bfd_archive_filename (abfd), shortname);
1457 }
1458 else
1459 {
1460 bh = &hi->root;
1461 if (! (_bfd_generic_link_add_one_symbol
1462 (info, abfd, shortname, BSF_INDIRECT,
1463 bfd_ind_section_ptr, (bfd_vma) 0, name, FALSE, collect, &bh)))
1464 return FALSE;
1465 hi = (struct elf_link_hash_entry *) bh;
1466
1467 /* If there is a duplicate definition somewhere, then HI may not
1468 point to an indirect symbol. We will have reported an error
1469 to the user in that case. */
1470
1471 if (hi->root.type == bfd_link_hash_indirect)
1472 {
1473 /* If the symbol became indirect, then we assume that we have
1474 not seen a definition before. */
1475 BFD_ASSERT ((hi->elf_link_hash_flags
1476 & (ELF_LINK_HASH_DEF_DYNAMIC
1477 | ELF_LINK_HASH_DEF_REGULAR)) == 0);
1478
1479 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
1480
1481 /* See if the new flags lead us to realize that the symbol
1482 must be dynamic. */
1483 if (! *dynsym)
1484 {
1485 if (! dynamic)
1486 {
1487 if (info->shared
1488 || ((hi->elf_link_hash_flags
1489 & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1490 *dynsym = TRUE;
1491 }
1492 else
1493 {
1494 if ((hi->elf_link_hash_flags
1495 & ELF_LINK_HASH_REF_REGULAR) != 0)
1496 *dynsym = TRUE;
1497 }
1498 }
1499 }
1500 }
1501
1502 return TRUE;
1503}
1504\f
1505/* This routine is used to export all defined symbols into the dynamic
1506 symbol table. It is called via elf_link_hash_traverse. */
1507
1508bfd_boolean
1509_bfd_elf_export_symbol (h, data)
1510 struct elf_link_hash_entry *h;
1511 PTR data;
1512{
1513 struct elf_info_failed *eif = (struct elf_info_failed *) data;
1514
1515 /* Ignore indirect symbols. These are added by the versioning code. */
1516 if (h->root.type == bfd_link_hash_indirect)
1517 return TRUE;
1518
1519 if (h->root.type == bfd_link_hash_warning)
1520 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1521
1522 if (h->dynindx == -1
1523 && (h->elf_link_hash_flags
1524 & (ELF_LINK_HASH_DEF_REGULAR | ELF_LINK_HASH_REF_REGULAR)) != 0)
1525 {
1526 struct bfd_elf_version_tree *t;
1527 struct bfd_elf_version_expr *d;
1528
1529 for (t = eif->verdefs; t != NULL; t = t->next)
1530 {
1531 if (t->globals != NULL)
1532 {
1533 for (d = t->globals; d != NULL; d = d->next)
1534 {
1535 if ((*d->match) (d, h->root.root.string))
1536 goto doit;
1537 }
1538 }
1539
1540 if (t->locals != NULL)
1541 {
1542 for (d = t->locals ; d != NULL; d = d->next)
1543 {
1544 if ((*d->match) (d, h->root.root.string))
1545 return TRUE;
1546 }
1547 }
1548 }
1549
1550 if (!eif->verdefs)
1551 {
1552 doit:
1553 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
1554 {
1555 eif->failed = TRUE;
1556 return FALSE;
1557 }
1558 }
1559 }
1560
1561 return TRUE;
1562}
1563\f
1564/* Look through the symbols which are defined in other shared
1565 libraries and referenced here. Update the list of version
1566 dependencies. This will be put into the .gnu.version_r section.
1567 This function is called via elf_link_hash_traverse. */
1568
1569bfd_boolean
1570_bfd_elf_link_find_version_dependencies (h, data)
1571 struct elf_link_hash_entry *h;
1572 PTR data;
1573{
1574 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
1575 Elf_Internal_Verneed *t;
1576 Elf_Internal_Vernaux *a;
1577 bfd_size_type amt;
1578
1579 if (h->root.type == bfd_link_hash_warning)
1580 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1581
1582 /* We only care about symbols defined in shared objects with version
1583 information. */
1584 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
1585 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1586 || h->dynindx == -1
1587 || h->verinfo.verdef == NULL)
1588 return TRUE;
1589
1590 /* See if we already know about this version. */
1591 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1592 {
1593 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1594 continue;
1595
1596 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1597 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1598 return TRUE;
1599
1600 break;
1601 }
1602
1603 /* This is a new version. Add it to tree we are building. */
1604
1605 if (t == NULL)
1606 {
1607 amt = sizeof *t;
1608 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->output_bfd, amt);
1609 if (t == NULL)
1610 {
1611 rinfo->failed = TRUE;
1612 return FALSE;
1613 }
1614
1615 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1616 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1617 elf_tdata (rinfo->output_bfd)->verref = t;
1618 }
1619
1620 amt = sizeof *a;
1621 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->output_bfd, amt);
1622
1623 /* Note that we are copying a string pointer here, and testing it
1624 above. If bfd_elf_string_from_elf_section is ever changed to
1625 discard the string data when low in memory, this will have to be
1626 fixed. */
1627 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1628
1629 a->vna_flags = h->verinfo.verdef->vd_flags;
1630 a->vna_nextptr = t->vn_auxptr;
1631
1632 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1633 ++rinfo->vers;
1634
1635 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1636
1637 t->vn_auxptr = a;
1638
1639 return TRUE;
1640}
1641
1642/* Figure out appropriate versions for all the symbols. We may not
1643 have the version number script until we have read all of the input
1644 files, so until that point we don't know which symbols should be
1645 local. This function is called via elf_link_hash_traverse. */
1646
1647bfd_boolean
1648_bfd_elf_link_assign_sym_version (h, data)
1649 struct elf_link_hash_entry *h;
1650 PTR data;
1651{
1652 struct elf_assign_sym_version_info *sinfo;
1653 struct bfd_link_info *info;
1654 struct elf_backend_data *bed;
1655 struct elf_info_failed eif;
1656 char *p;
1657 bfd_size_type amt;
1658
1659 sinfo = (struct elf_assign_sym_version_info *) data;
1660 info = sinfo->info;
1661
1662 if (h->root.type == bfd_link_hash_warning)
1663 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1664
1665 /* Fix the symbol flags. */
1666 eif.failed = FALSE;
1667 eif.info = info;
1668 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1669 {
1670 if (eif.failed)
1671 sinfo->failed = TRUE;
1672 return FALSE;
1673 }
1674
1675 /* We only need version numbers for symbols defined in regular
1676 objects. */
1677 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1678 return TRUE;
1679
1680 bed = get_elf_backend_data (sinfo->output_bfd);
1681 p = strchr (h->root.root.string, ELF_VER_CHR);
1682 if (p != NULL && h->verinfo.vertree == NULL)
1683 {
1684 struct bfd_elf_version_tree *t;
1685 bfd_boolean hidden;
1686
1687 hidden = TRUE;
1688
1689 /* There are two consecutive ELF_VER_CHR characters if this is
1690 not a hidden symbol. */
1691 ++p;
1692 if (*p == ELF_VER_CHR)
1693 {
1694 hidden = FALSE;
1695 ++p;
1696 }
1697
1698 /* If there is no version string, we can just return out. */
1699 if (*p == '\0')
1700 {
1701 if (hidden)
1702 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
1703 return TRUE;
1704 }
1705
1706 /* Look for the version. If we find it, it is no longer weak. */
1707 for (t = sinfo->verdefs; t != NULL; t = t->next)
1708 {
1709 if (strcmp (t->name, p) == 0)
1710 {
1711 size_t len;
1712 char *alc;
1713 struct bfd_elf_version_expr *d;
1714
1715 len = p - h->root.root.string;
1716 alc = bfd_malloc ((bfd_size_type) len);
1717 if (alc == NULL)
1718 return FALSE;
1719 memcpy (alc, h->root.root.string, len - 1);
1720 alc[len - 1] = '\0';
1721 if (alc[len - 2] == ELF_VER_CHR)
1722 alc[len - 2] = '\0';
1723
1724 h->verinfo.vertree = t;
1725 t->used = TRUE;
1726 d = NULL;
1727
1728 if (t->globals != NULL)
1729 {
1730 for (d = t->globals; d != NULL; d = d->next)
1731 if ((*d->match) (d, alc))
1732 break;
1733 }
1734
1735 /* See if there is anything to force this symbol to
1736 local scope. */
1737 if (d == NULL && t->locals != NULL)
1738 {
1739 for (d = t->locals; d != NULL; d = d->next)
1740 {
1741 if ((*d->match) (d, alc))
1742 {
1743 if (h->dynindx != -1
1744 && info->shared
1745 && ! info->export_dynamic)
1746 {
1747 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1748 }
1749
1750 break;
1751 }
1752 }
1753 }
1754
1755 free (alc);
1756 break;
1757 }
1758 }
1759
1760 /* If we are building an application, we need to create a
1761 version node for this version. */
1762 if (t == NULL && ! info->shared)
1763 {
1764 struct bfd_elf_version_tree **pp;
1765 int version_index;
1766
1767 /* If we aren't going to export this symbol, we don't need
1768 to worry about it. */
1769 if (h->dynindx == -1)
1770 return TRUE;
1771
1772 amt = sizeof *t;
1773 t = ((struct bfd_elf_version_tree *)
1774 bfd_alloc (sinfo->output_bfd, amt));
1775 if (t == NULL)
1776 {
1777 sinfo->failed = TRUE;
1778 return FALSE;
1779 }
1780
1781 t->next = NULL;
1782 t->name = p;
1783 t->globals = NULL;
1784 t->locals = NULL;
1785 t->deps = NULL;
1786 t->name_indx = (unsigned int) -1;
1787 t->used = TRUE;
1788
1789 version_index = 1;
1790 /* Don't count anonymous version tag. */
1791 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1792 version_index = 0;
1793 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1794 ++version_index;
1795 t->vernum = version_index;
1796
1797 *pp = t;
1798
1799 h->verinfo.vertree = t;
1800 }
1801 else if (t == NULL)
1802 {
1803 /* We could not find the version for a symbol when
1804 generating a shared archive. Return an error. */
1805 (*_bfd_error_handler)
1806 (_("%s: undefined versioned symbol name %s"),
1807 bfd_get_filename (sinfo->output_bfd), h->root.root.string);
1808 bfd_set_error (bfd_error_bad_value);
1809 sinfo->failed = TRUE;
1810 return FALSE;
1811 }
1812
1813 if (hidden)
1814 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
1815 }
1816
1817 /* If we don't have a version for this symbol, see if we can find
1818 something. */
1819 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1820 {
1821 struct bfd_elf_version_tree *t;
1822 struct bfd_elf_version_tree *local_ver;
1823 struct bfd_elf_version_expr *d;
1824
1825 /* See if can find what version this symbol is in. If the
1826 symbol is supposed to be local, then don't actually register
1827 it. */
1828 local_ver = NULL;
1829 for (t = sinfo->verdefs; t != NULL; t = t->next)
1830 {
1831 if (t->globals != NULL)
1832 {
1833 bfd_boolean matched;
1834
1835 matched = FALSE;
1836 for (d = t->globals; d != NULL; d = d->next)
1837 {
1838 if ((*d->match) (d, h->root.root.string))
1839 {
1840 if (d->symver)
1841 matched = TRUE;
1842 else
1843 {
1844 /* There is a version without definition. Make
1845 the symbol the default definition for this
1846 version. */
1847 h->verinfo.vertree = t;
1848 local_ver = NULL;
1849 d->script = 1;
1850 break;
1851 }
1852 }
1853 }
1854
1855 if (d != NULL)
1856 break;
1857 else if (matched)
1858 /* There is no undefined version for this symbol. Hide the
1859 default one. */
1860 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1861 }
1862
1863 if (t->locals != NULL)
1864 {
1865 for (d = t->locals; d != NULL; d = d->next)
1866 {
1867 /* If the match is "*", keep looking for a more
1868 explicit, perhaps even global, match. */
1869 if (d->pattern[0] == '*' && d->pattern[1] == '\0')
1870 local_ver = t;
1871 else if ((*d->match) (d, h->root.root.string))
1872 {
1873 local_ver = t;
1874 break;
1875 }
1876 }
1877
1878 if (d != NULL)
1879 break;
1880 }
1881 }
1882
1883 if (local_ver != NULL)
1884 {
1885 h->verinfo.vertree = local_ver;
1886 if (h->dynindx != -1
1887 && info->shared
1888 && ! info->export_dynamic)
1889 {
1890 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1891 }
1892 }
1893 }
1894
1895 return TRUE;
1896}
1897\f
1898/* Create a special linker section, or return a pointer to a linker
1899 section already created */
1900
1901elf_linker_section_t *
1902_bfd_elf_create_linker_section (abfd, info, which, defaults)
1903 bfd *abfd;
1904 struct bfd_link_info *info;
1905 enum elf_linker_section_enum which;
1906 elf_linker_section_t *defaults;
1907{
1908 bfd *dynobj = elf_hash_table (info)->dynobj;
1909 elf_linker_section_t *lsect;
1910
1911 /* Record the first bfd section that needs the special section */
1912 if (!dynobj)
1913 dynobj = elf_hash_table (info)->dynobj = abfd;
1914
1915 /* If this is the first time, create the section */
1916 lsect = elf_linker_section (dynobj, which);
1917 if (!lsect)
1918 {
1919 asection *s;
1920 bfd_size_type amt = sizeof (elf_linker_section_t);
1921
1922 lsect = (elf_linker_section_t *) bfd_alloc (dynobj, amt);
1923
1924 *lsect = *defaults;
1925 elf_linker_section (dynobj, which) = lsect;
1926 lsect->which = which;
1927 lsect->hole_written_p = FALSE;
1928
1929 /* See if the sections already exist */
1930 lsect->section = s = bfd_get_section_by_name (dynobj, lsect->name);
1931 if (!s || (s->flags & defaults->flags) != defaults->flags)
1932 {
1933 lsect->section = s = bfd_make_section_anyway (dynobj, lsect->name);
1934
1935 if (s == NULL)
1936 return (elf_linker_section_t *)0;
1937
1938 bfd_set_section_flags (dynobj, s, defaults->flags);
1939 bfd_set_section_alignment (dynobj, s, lsect->alignment);
1940 }
1941 else if (bfd_get_section_alignment (dynobj, s) < lsect->alignment)
1942 bfd_set_section_alignment (dynobj, s, lsect->alignment);
1943
1944 s->_raw_size = align_power (s->_raw_size, lsect->alignment);
1945
1946 /* Is there a hole we have to provide? If so check whether the
1947 segment is too big already */
1948 if (lsect->hole_size)
1949 {
1950 lsect->hole_offset = s->_raw_size;
1951 s->_raw_size += lsect->hole_size;
1952 if (lsect->hole_offset > lsect->max_hole_offset)
1953 {
1954 (*_bfd_error_handler)
1955 (_("%s: Section %s is too large to add hole of %ld bytes"),
1956 bfd_get_filename (abfd),
1957 lsect->name,
1958 (long) lsect->hole_size);
1959
1960 bfd_set_error (bfd_error_bad_value);
1961 return (elf_linker_section_t *)0;
1962 }
1963 }
1964
1965#ifdef DEBUG
1966 fprintf (stderr, "Creating section %s, current size = %ld\n",
1967 lsect->name, (long)s->_raw_size);
1968#endif
1969
1970 if (lsect->sym_name)
1971 {
1972 struct elf_link_hash_entry *h;
1973 struct bfd_link_hash_entry *bh;
1974
1975#ifdef DEBUG
1976 fprintf (stderr, "Adding %s to section %s\n",
1977 lsect->sym_name,
1978 lsect->name);
1979#endif
1980 bh = bfd_link_hash_lookup (info->hash, lsect->sym_name,
1981 FALSE, FALSE, FALSE);
1982
1983 if ((bh == NULL || bh->type == bfd_link_hash_undefined)
1984 && !(_bfd_generic_link_add_one_symbol
1985 (info, abfd, lsect->sym_name, BSF_GLOBAL, s,
1986 (lsect->hole_size
1987 ? s->_raw_size - lsect->hole_size + lsect->sym_offset
1988 : lsect->sym_offset),
1989 (const char *) NULL, FALSE,
1990 get_elf_backend_data (abfd)->collect, &bh)))
1991 return (elf_linker_section_t *) 0;
1992 h = (struct elf_link_hash_entry *) bh;
1993
1994 if ((defaults->which != LINKER_SECTION_SDATA)
1995 && (defaults->which != LINKER_SECTION_SDATA2))
1996 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_DYNAMIC;
1997
1998 h->type = STT_OBJECT;
1999 lsect->sym_hash = h;
2000
2001 if (info->shared
2002 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
2003 return (elf_linker_section_t *) 0;
2004 }
2005 }
2006
2007#if 0
2008 /* This does not make sense. The sections which may exist in the
2009 object file have nothing to do with the sections we want to
2010 create. */
2011
2012 /* Find the related sections if they have been created */
2013 if (lsect->bss_name && !lsect->bss_section)
2014 lsect->bss_section = bfd_get_section_by_name (dynobj, lsect->bss_name);
2015
2016 if (lsect->rel_name && !lsect->rel_section)
2017 lsect->rel_section = bfd_get_section_by_name (dynobj, lsect->rel_name);
2018#endif
2019
2020 return lsect;
2021}
2022\f
2023/* Find a linker generated pointer with a given addend and type. */
252b5132
RH
2024
2025elf_linker_section_pointers_t *
2026_bfd_elf_find_pointer_linker_section (linker_pointers, addend, which)
2027 elf_linker_section_pointers_t *linker_pointers;
dc810e39 2028 bfd_vma addend;
252b5132
RH
2029 elf_linker_section_enum_t which;
2030{
2031 for ( ; linker_pointers != NULL; linker_pointers = linker_pointers->next)
2032 {
2033 if (which == linker_pointers->which && addend == linker_pointers->addend)
2034 return linker_pointers;
2035 }
2036
2037 return (elf_linker_section_pointers_t *)0;
2038}
252b5132
RH
2039\f
2040/* Make the .rela section corresponding to the generated linker section. */
2041
b34976b6 2042bfd_boolean
252b5132
RH
2043_bfd_elf_make_linker_section_rela (dynobj, lsect, alignment)
2044 bfd *dynobj;
2045 elf_linker_section_t *lsect;
2046 int alignment;
2047{
2048 if (lsect->rel_section)
b34976b6 2049 return TRUE;
252b5132
RH
2050
2051 lsect->rel_section = bfd_get_section_by_name (dynobj, lsect->rel_name);
2052 if (lsect->rel_section == NULL)
2053 {
2054 lsect->rel_section = bfd_make_section (dynobj, lsect->rel_name);
2055 if (lsect->rel_section == NULL
2056 || ! bfd_set_section_flags (dynobj,
2057 lsect->rel_section,
2058 (SEC_ALLOC
2059 | SEC_LOAD
2060 | SEC_HAS_CONTENTS
2061 | SEC_IN_MEMORY
2062 | SEC_LINKER_CREATED
2063 | SEC_READONLY))
2064 || ! bfd_set_section_alignment (dynobj, lsect->rel_section, alignment))
b34976b6 2065 return FALSE;
252b5132
RH
2066 }
2067
b34976b6 2068 return TRUE;
252b5132 2069}
45d6a902
AM
2070\f
2071/* Read and swap the relocs from the section indicated by SHDR. This
2072 may be either a REL or a RELA section. The relocations are
2073 translated into RELA relocations and stored in INTERNAL_RELOCS,
2074 which should have already been allocated to contain enough space.
2075 The EXTERNAL_RELOCS are a buffer where the external form of the
2076 relocations should be stored.
2077
2078 Returns FALSE if something goes wrong. */
2079
2080static bfd_boolean
2081elf_link_read_relocs_from_section (abfd, shdr, external_relocs,
2082 internal_relocs)
2083 bfd *abfd;
2084 Elf_Internal_Shdr *shdr;
2085 PTR external_relocs;
2086 Elf_Internal_Rela *internal_relocs;
2087{
2088 struct elf_backend_data *bed;
2089 void (*swap_in) PARAMS ((bfd *, const bfd_byte *, Elf_Internal_Rela *));
2090 const bfd_byte *erela;
2091 const bfd_byte *erelaend;
2092 Elf_Internal_Rela *irela;
2093
2094 /* If there aren't any relocations, that's OK. */
2095 if (!shdr)
2096 return TRUE;
2097
2098 /* Position ourselves at the start of the section. */
2099 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2100 return FALSE;
2101
2102 /* Read the relocations. */
2103 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2104 return FALSE;
2105
2106 bed = get_elf_backend_data (abfd);
2107
2108 /* Convert the external relocations to the internal format. */
2109 if (shdr->sh_entsize == bed->s->sizeof_rel)
2110 swap_in = bed->s->swap_reloc_in;
2111 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2112 swap_in = bed->s->swap_reloca_in;
2113 else
2114 {
2115 bfd_set_error (bfd_error_wrong_format);
2116 return FALSE;
2117 }
2118
2119 erela = external_relocs;
2120 erelaend = erela + NUM_SHDR_ENTRIES (shdr) * shdr->sh_entsize;
2121 irela = internal_relocs;
2122 while (erela < erelaend)
2123 {
2124 (*swap_in) (abfd, erela, irela);
2125 irela += bed->s->int_rels_per_ext_rel;
2126 erela += shdr->sh_entsize;
2127 }
2128
2129 return TRUE;
2130}
2131
2132/* Read and swap the relocs for a section O. They may have been
2133 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2134 not NULL, they are used as buffers to read into. They are known to
2135 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2136 the return value is allocated using either malloc or bfd_alloc,
2137 according to the KEEP_MEMORY argument. If O has two relocation
2138 sections (both REL and RELA relocations), then the REL_HDR
2139 relocations will appear first in INTERNAL_RELOCS, followed by the
2140 REL_HDR2 relocations. */
2141
2142Elf_Internal_Rela *
2143_bfd_elf_link_read_relocs (abfd, o, external_relocs, internal_relocs,
2144 keep_memory)
2145 bfd *abfd;
2146 asection *o;
2147 PTR external_relocs;
2148 Elf_Internal_Rela *internal_relocs;
2149 bfd_boolean keep_memory;
2150{
2151 Elf_Internal_Shdr *rel_hdr;
2152 PTR alloc1 = NULL;
2153 Elf_Internal_Rela *alloc2 = NULL;
2154 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2155
2156 if (elf_section_data (o)->relocs != NULL)
2157 return elf_section_data (o)->relocs;
2158
2159 if (o->reloc_count == 0)
2160 return NULL;
2161
2162 rel_hdr = &elf_section_data (o)->rel_hdr;
2163
2164 if (internal_relocs == NULL)
2165 {
2166 bfd_size_type size;
2167
2168 size = o->reloc_count;
2169 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2170 if (keep_memory)
2171 internal_relocs = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2172 else
2173 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2174 if (internal_relocs == NULL)
2175 goto error_return;
2176 }
2177
2178 if (external_relocs == NULL)
2179 {
2180 bfd_size_type size = rel_hdr->sh_size;
2181
2182 if (elf_section_data (o)->rel_hdr2)
2183 size += elf_section_data (o)->rel_hdr2->sh_size;
2184 alloc1 = (PTR) bfd_malloc (size);
2185 if (alloc1 == NULL)
2186 goto error_return;
2187 external_relocs = alloc1;
2188 }
2189
2190 if (!elf_link_read_relocs_from_section (abfd, rel_hdr,
2191 external_relocs,
2192 internal_relocs))
2193 goto error_return;
2194 if (!elf_link_read_relocs_from_section
2195 (abfd,
2196 elf_section_data (o)->rel_hdr2,
2197 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2198 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2199 * bed->s->int_rels_per_ext_rel)))
2200 goto error_return;
2201
2202 /* Cache the results for next time, if we can. */
2203 if (keep_memory)
2204 elf_section_data (o)->relocs = internal_relocs;
2205
2206 if (alloc1 != NULL)
2207 free (alloc1);
2208
2209 /* Don't free alloc2, since if it was allocated we are passing it
2210 back (under the name of internal_relocs). */
2211
2212 return internal_relocs;
2213
2214 error_return:
2215 if (alloc1 != NULL)
2216 free (alloc1);
2217 if (alloc2 != NULL)
2218 free (alloc2);
2219 return NULL;
2220}
2221
2222/* Compute the size of, and allocate space for, REL_HDR which is the
2223 section header for a section containing relocations for O. */
2224
2225bfd_boolean
2226_bfd_elf_link_size_reloc_section (abfd, rel_hdr, o)
2227 bfd *abfd;
2228 Elf_Internal_Shdr *rel_hdr;
2229 asection *o;
2230{
2231 bfd_size_type reloc_count;
2232 bfd_size_type num_rel_hashes;
2233
2234 /* Figure out how many relocations there will be. */
2235 if (rel_hdr == &elf_section_data (o)->rel_hdr)
2236 reloc_count = elf_section_data (o)->rel_count;
2237 else
2238 reloc_count = elf_section_data (o)->rel_count2;
2239
2240 num_rel_hashes = o->reloc_count;
2241 if (num_rel_hashes < reloc_count)
2242 num_rel_hashes = reloc_count;
2243
2244 /* That allows us to calculate the size of the section. */
2245 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
2246
2247 /* The contents field must last into write_object_contents, so we
2248 allocate it with bfd_alloc rather than malloc. Also since we
2249 cannot be sure that the contents will actually be filled in,
2250 we zero the allocated space. */
2251 rel_hdr->contents = (PTR) bfd_zalloc (abfd, rel_hdr->sh_size);
2252 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2253 return FALSE;
2254
2255 /* We only allocate one set of hash entries, so we only do it the
2256 first time we are called. */
2257 if (elf_section_data (o)->rel_hashes == NULL
2258 && num_rel_hashes)
2259 {
2260 struct elf_link_hash_entry **p;
2261
2262 p = ((struct elf_link_hash_entry **)
2263 bfd_zmalloc (num_rel_hashes
2264 * sizeof (struct elf_link_hash_entry *)));
2265 if (p == NULL)
2266 return FALSE;
2267
2268 elf_section_data (o)->rel_hashes = p;
2269 }
2270
2271 return TRUE;
2272}
2273
2274/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2275 originated from the section given by INPUT_REL_HDR) to the
2276 OUTPUT_BFD. */
2277
2278bfd_boolean
2279_bfd_elf_link_output_relocs (output_bfd, input_section, input_rel_hdr,
2280 internal_relocs)
2281 bfd *output_bfd;
2282 asection *input_section;
2283 Elf_Internal_Shdr *input_rel_hdr;
2284 Elf_Internal_Rela *internal_relocs;
2285{
2286 Elf_Internal_Rela *irela;
2287 Elf_Internal_Rela *irelaend;
2288 bfd_byte *erel;
2289 Elf_Internal_Shdr *output_rel_hdr;
2290 asection *output_section;
2291 unsigned int *rel_countp = NULL;
2292 struct elf_backend_data *bed;
2293 void (*swap_out) PARAMS ((bfd *, const Elf_Internal_Rela *, bfd_byte *));
2294
2295 output_section = input_section->output_section;
2296 output_rel_hdr = NULL;
2297
2298 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2299 == input_rel_hdr->sh_entsize)
2300 {
2301 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2302 rel_countp = &elf_section_data (output_section)->rel_count;
2303 }
2304 else if (elf_section_data (output_section)->rel_hdr2
2305 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2306 == input_rel_hdr->sh_entsize))
2307 {
2308 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2309 rel_countp = &elf_section_data (output_section)->rel_count2;
2310 }
2311 else
2312 {
2313 (*_bfd_error_handler)
2314 (_("%s: relocation size mismatch in %s section %s"),
2315 bfd_get_filename (output_bfd),
2316 bfd_archive_filename (input_section->owner),
2317 input_section->name);
2318 bfd_set_error (bfd_error_wrong_object_format);
2319 return FALSE;
2320 }
2321
2322 bed = get_elf_backend_data (output_bfd);
2323 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2324 swap_out = bed->s->swap_reloc_out;
2325 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2326 swap_out = bed->s->swap_reloca_out;
2327 else
2328 abort ();
2329
2330 erel = output_rel_hdr->contents;
2331 erel += *rel_countp * input_rel_hdr->sh_entsize;
2332 irela = internal_relocs;
2333 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2334 * bed->s->int_rels_per_ext_rel);
2335 while (irela < irelaend)
2336 {
2337 (*swap_out) (output_bfd, irela, erel);
2338 irela += bed->s->int_rels_per_ext_rel;
2339 erel += input_rel_hdr->sh_entsize;
2340 }
2341
2342 /* Bump the counter, so that we know where to add the next set of
2343 relocations. */
2344 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2345
2346 return TRUE;
2347}
2348\f
2349/* Fix up the flags for a symbol. This handles various cases which
2350 can only be fixed after all the input files are seen. This is
2351 currently called by both adjust_dynamic_symbol and
2352 assign_sym_version, which is unnecessary but perhaps more robust in
2353 the face of future changes. */
2354
2355bfd_boolean
2356_bfd_elf_fix_symbol_flags (h, eif)
2357 struct elf_link_hash_entry *h;
2358 struct elf_info_failed *eif;
2359{
2360 /* If this symbol was mentioned in a non-ELF file, try to set
2361 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2362 permit a non-ELF file to correctly refer to a symbol defined in
2363 an ELF dynamic object. */
2364 if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) != 0)
2365 {
2366 while (h->root.type == bfd_link_hash_indirect)
2367 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2368
2369 if (h->root.type != bfd_link_hash_defined
2370 && h->root.type != bfd_link_hash_defweak)
2371 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
2372 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
2373 else
2374 {
2375 if (h->root.u.def.section->owner != NULL
2376 && (bfd_get_flavour (h->root.u.def.section->owner)
2377 == bfd_target_elf_flavour))
2378 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
2379 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
2380 else
2381 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2382 }
2383
2384 if (h->dynindx == -1
2385 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2386 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0))
2387 {
2388 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
2389 {
2390 eif->failed = TRUE;
2391 return FALSE;
2392 }
2393 }
2394 }
2395 else
2396 {
2397 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
2398 was first seen in a non-ELF file. Fortunately, if the symbol
2399 was first seen in an ELF file, we're probably OK unless the
2400 symbol was defined in a non-ELF file. Catch that case here.
2401 FIXME: We're still in trouble if the symbol was first seen in
2402 a dynamic object, and then later in a non-ELF regular object. */
2403 if ((h->root.type == bfd_link_hash_defined
2404 || h->root.type == bfd_link_hash_defweak)
2405 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
2406 && (h->root.u.def.section->owner != NULL
2407 ? (bfd_get_flavour (h->root.u.def.section->owner)
2408 != bfd_target_elf_flavour)
2409 : (bfd_is_abs_section (h->root.u.def.section)
2410 && (h->elf_link_hash_flags
2411 & ELF_LINK_HASH_DEF_DYNAMIC) == 0)))
2412 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2413 }
2414
2415 /* If this is a final link, and the symbol was defined as a common
2416 symbol in a regular object file, and there was no definition in
2417 any dynamic object, then the linker will have allocated space for
2418 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
2419 flag will not have been set. */
2420 if (h->root.type == bfd_link_hash_defined
2421 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
2422 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
2423 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
2424 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2425 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2426
2427 /* If -Bsymbolic was used (which means to bind references to global
2428 symbols to the definition within the shared object), and this
2429 symbol was defined in a regular object, then it actually doesn't
c1be741f
L
2430 need a PLT entry. Likewise, if the symbol has non-default
2431 visibility. If the symbol has hidden or internal visibility, we
2432 will force it local. */
45d6a902
AM
2433 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
2434 && eif->info->shared
2435 && is_elf_hash_table (eif->info)
2436 && (eif->info->symbolic
c1be741f 2437 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
45d6a902
AM
2438 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
2439 {
2440 struct elf_backend_data *bed;
2441 bfd_boolean force_local;
2442
2443 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2444
2445 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2446 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2447 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2448 }
2449
2450 /* If a weak undefined symbol has non-default visibility, we also
2451 hide it from the dynamic linker. */
2452 if (ELF_ST_VISIBILITY (h->other)
2453 && h->root.type == bfd_link_hash_undefweak)
2454 {
2455 struct elf_backend_data *bed;
2456 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2457 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2458 }
2459
2460 /* If this is a weak defined symbol in a dynamic object, and we know
2461 the real definition in the dynamic object, copy interesting flags
2462 over to the real definition. */
2463 if (h->weakdef != NULL)
2464 {
2465 struct elf_link_hash_entry *weakdef;
2466
2467 weakdef = h->weakdef;
2468 if (h->root.type == bfd_link_hash_indirect)
2469 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2470
2471 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2472 || h->root.type == bfd_link_hash_defweak);
2473 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2474 || weakdef->root.type == bfd_link_hash_defweak);
2475 BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC);
2476
2477 /* If the real definition is defined by a regular object file,
2478 don't do anything special. See the longer description in
2479 _bfd_elf_adjust_dynamic_symbol, below. */
2480 if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
2481 h->weakdef = NULL;
2482 else
2483 {
2484 struct elf_backend_data *bed;
2485
2486 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2487 (*bed->elf_backend_copy_indirect_symbol) (bed, weakdef, h);
2488 }
2489 }
2490
2491 return TRUE;
2492}
2493
2494/* Make the backend pick a good value for a dynamic symbol. This is
2495 called via elf_link_hash_traverse, and also calls itself
2496 recursively. */
2497
2498bfd_boolean
2499_bfd_elf_adjust_dynamic_symbol (h, data)
2500 struct elf_link_hash_entry *h;
2501 PTR data;
2502{
2503 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2504 bfd *dynobj;
2505 struct elf_backend_data *bed;
2506
2507 if (! is_elf_hash_table (eif->info))
2508 return FALSE;
2509
2510 if (h->root.type == bfd_link_hash_warning)
2511 {
2512 h->plt = elf_hash_table (eif->info)->init_offset;
2513 h->got = elf_hash_table (eif->info)->init_offset;
2514
2515 /* When warning symbols are created, they **replace** the "real"
2516 entry in the hash table, thus we never get to see the real
2517 symbol in a hash traversal. So look at it now. */
2518 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2519 }
2520
2521 /* Ignore indirect symbols. These are added by the versioning code. */
2522 if (h->root.type == bfd_link_hash_indirect)
2523 return TRUE;
2524
2525 /* Fix the symbol flags. */
2526 if (! _bfd_elf_fix_symbol_flags (h, eif))
2527 return FALSE;
2528
2529 /* If this symbol does not require a PLT entry, and it is not
2530 defined by a dynamic object, or is not referenced by a regular
2531 object, ignore it. We do have to handle a weak defined symbol,
2532 even if no regular object refers to it, if we decided to add it
2533 to the dynamic symbol table. FIXME: Do we normally need to worry
2534 about symbols which are defined by one dynamic object and
2535 referenced by another one? */
2536 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0
2537 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
2538 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
2539 || ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
2540 && (h->weakdef == NULL || h->weakdef->dynindx == -1))))
2541 {
2542 h->plt = elf_hash_table (eif->info)->init_offset;
2543 return TRUE;
2544 }
2545
2546 /* If we've already adjusted this symbol, don't do it again. This
2547 can happen via a recursive call. */
2548 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
2549 return TRUE;
2550
2551 /* Don't look at this symbol again. Note that we must set this
2552 after checking the above conditions, because we may look at a
2553 symbol once, decide not to do anything, and then get called
2554 recursively later after REF_REGULAR is set below. */
2555 h->elf_link_hash_flags |= ELF_LINK_HASH_DYNAMIC_ADJUSTED;
2556
2557 /* If this is a weak definition, and we know a real definition, and
2558 the real symbol is not itself defined by a regular object file,
2559 then get a good value for the real definition. We handle the
2560 real symbol first, for the convenience of the backend routine.
2561
2562 Note that there is a confusing case here. If the real definition
2563 is defined by a regular object file, we don't get the real symbol
2564 from the dynamic object, but we do get the weak symbol. If the
2565 processor backend uses a COPY reloc, then if some routine in the
2566 dynamic object changes the real symbol, we will not see that
2567 change in the corresponding weak symbol. This is the way other
2568 ELF linkers work as well, and seems to be a result of the shared
2569 library model.
2570
2571 I will clarify this issue. Most SVR4 shared libraries define the
2572 variable _timezone and define timezone as a weak synonym. The
2573 tzset call changes _timezone. If you write
2574 extern int timezone;
2575 int _timezone = 5;
2576 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2577 you might expect that, since timezone is a synonym for _timezone,
2578 the same number will print both times. However, if the processor
2579 backend uses a COPY reloc, then actually timezone will be copied
2580 into your process image, and, since you define _timezone
2581 yourself, _timezone will not. Thus timezone and _timezone will
2582 wind up at different memory locations. The tzset call will set
2583 _timezone, leaving timezone unchanged. */
2584
2585 if (h->weakdef != NULL)
2586 {
2587 /* If we get to this point, we know there is an implicit
2588 reference by a regular object file via the weak symbol H.
2589 FIXME: Is this really true? What if the traversal finds
2590 H->WEAKDEF before it finds H? */
2591 h->weakdef->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
2592
2593 if (! _bfd_elf_adjust_dynamic_symbol (h->weakdef, (PTR) eif))
2594 return FALSE;
2595 }
2596
2597 /* If a symbol has no type and no size and does not require a PLT
2598 entry, then we are probably about to do the wrong thing here: we
2599 are probably going to create a COPY reloc for an empty object.
2600 This case can arise when a shared object is built with assembly
2601 code, and the assembly code fails to set the symbol type. */
2602 if (h->size == 0
2603 && h->type == STT_NOTYPE
2604 && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0)
2605 (*_bfd_error_handler)
2606 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2607 h->root.root.string);
2608
2609 dynobj = elf_hash_table (eif->info)->dynobj;
2610 bed = get_elf_backend_data (dynobj);
2611 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2612 {
2613 eif->failed = TRUE;
2614 return FALSE;
2615 }
2616
2617 return TRUE;
2618}
2619
2620/* Adjust all external symbols pointing into SEC_MERGE sections
2621 to reflect the object merging within the sections. */
2622
2623bfd_boolean
2624_bfd_elf_link_sec_merge_syms (h, data)
2625 struct elf_link_hash_entry *h;
2626 PTR data;
2627{
2628 asection *sec;
2629
2630 if (h->root.type == bfd_link_hash_warning)
2631 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2632
2633 if ((h->root.type == bfd_link_hash_defined
2634 || h->root.type == bfd_link_hash_defweak)
2635 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2636 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2637 {
2638 bfd *output_bfd = (bfd *) data;
2639
2640 h->root.u.def.value =
2641 _bfd_merged_section_offset (output_bfd,
2642 &h->root.u.def.section,
2643 elf_section_data (sec)->sec_info,
2644 h->root.u.def.value, (bfd_vma) 0);
2645 }
2646
2647 return TRUE;
2648}