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