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