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