* Makefile.tpl (top level bootstrap support): Remove now-unneeded
[deliverable/binutils-gdb.git] / bfd / elflink.c
CommitLineData
252b5132 1/* ELF linking support for BFD.
051d5130 2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
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
02bb6eae
AO
433 /* Since we're defining the symbol, don't let it seem to have not
434 been defined. record_dynamic_symbol and size_dynamic_sections
435 may depend on this. */
436 if (h->root.type == bfd_link_hash_undefweak
437 || h->root.type == bfd_link_hash_undefined)
438 h->root.type = bfd_link_hash_new;
439
45d6a902
AM
440 if (h->root.type == bfd_link_hash_new)
441 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
442
443 /* If this symbol is being provided by the linker script, and it is
444 currently defined by a dynamic object, but not by a regular
445 object, then mark it as undefined so that the generic linker will
446 force the correct value. */
447 if (provide
448 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
449 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
450 h->root.type = bfd_link_hash_undefined;
451
452 /* If this symbol is not being provided by the linker script, and it is
453 currently defined by a dynamic object, but not by a regular object,
454 then clear out any version information because the symbol will not be
455 associated with the dynamic object any more. */
456 if (!provide
457 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
458 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
459 h->verinfo.verdef = NULL;
460
461 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
462
463 if (((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC
464 | ELF_LINK_HASH_REF_DYNAMIC)) != 0
465 || info->shared)
466 && h->dynindx == -1)
467 {
468 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
469 return FALSE;
470
471 /* If this is a weak defined symbol, and we know a corresponding
472 real symbol from the same dynamic object, make sure the real
473 symbol is also made into a dynamic symbol. */
474 if (h->weakdef != NULL
475 && h->weakdef->dynindx == -1)
476 {
477 if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
478 return FALSE;
479 }
480 }
481
482 return TRUE;
483}
42751cf3 484
8c58d23b
AM
485/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
486 success, and 2 on a failure caused by attempting to record a symbol
487 in a discarded section, eg. a discarded link-once section symbol. */
488
489int
268b6b39
AM
490elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
491 bfd *input_bfd,
492 long input_indx)
8c58d23b
AM
493{
494 bfd_size_type amt;
495 struct elf_link_local_dynamic_entry *entry;
496 struct elf_link_hash_table *eht;
497 struct elf_strtab_hash *dynstr;
498 unsigned long dynstr_index;
499 char *name;
500 Elf_External_Sym_Shndx eshndx;
501 char esym[sizeof (Elf64_External_Sym)];
502
0eddce27 503 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
504 return 0;
505
506 /* See if the entry exists already. */
507 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
508 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
509 return 1;
510
511 amt = sizeof (*entry);
268b6b39 512 entry = bfd_alloc (input_bfd, amt);
8c58d23b
AM
513 if (entry == NULL)
514 return 0;
515
516 /* Go find the symbol, so that we can find it's name. */
517 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 518 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
519 {
520 bfd_release (input_bfd, entry);
521 return 0;
522 }
523
524 if (entry->isym.st_shndx != SHN_UNDEF
525 && (entry->isym.st_shndx < SHN_LORESERVE
526 || entry->isym.st_shndx > SHN_HIRESERVE))
527 {
528 asection *s;
529
530 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
531 if (s == NULL || bfd_is_abs_section (s->output_section))
532 {
533 /* We can still bfd_release here as nothing has done another
534 bfd_alloc. We can't do this later in this function. */
535 bfd_release (input_bfd, entry);
536 return 2;
537 }
538 }
539
540 name = (bfd_elf_string_from_elf_section
541 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
542 entry->isym.st_name));
543
544 dynstr = elf_hash_table (info)->dynstr;
545 if (dynstr == NULL)
546 {
547 /* Create a strtab to hold the dynamic symbol names. */
548 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
549 if (dynstr == NULL)
550 return 0;
551 }
552
b34976b6 553 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
8c58d23b
AM
554 if (dynstr_index == (unsigned long) -1)
555 return 0;
556 entry->isym.st_name = dynstr_index;
557
558 eht = elf_hash_table (info);
559
560 entry->next = eht->dynlocal;
561 eht->dynlocal = entry;
562 entry->input_bfd = input_bfd;
563 entry->input_indx = input_indx;
564 eht->dynsymcount++;
565
566 /* Whatever binding the symbol had before, it's now local. */
567 entry->isym.st_info
568 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
569
570 /* The dynindx will be set at the end of size_dynamic_sections. */
571
572 return 1;
573}
574
30b30c21 575/* Return the dynindex of a local dynamic symbol. */
42751cf3 576
30b30c21 577long
268b6b39
AM
578_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
579 bfd *input_bfd,
580 long input_indx)
30b30c21
RH
581{
582 struct elf_link_local_dynamic_entry *e;
583
584 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
585 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
586 return e->dynindx;
587 return -1;
588}
589
590/* This function is used to renumber the dynamic symbols, if some of
591 them are removed because they are marked as local. This is called
592 via elf_link_hash_traverse. */
593
b34976b6 594static bfd_boolean
268b6b39
AM
595elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
596 void *data)
42751cf3 597{
268b6b39 598 size_t *count = data;
30b30c21 599
e92d460e
AM
600 if (h->root.type == bfd_link_hash_warning)
601 h = (struct elf_link_hash_entry *) h->root.u.i.link;
602
42751cf3 603 if (h->dynindx != -1)
30b30c21
RH
604 h->dynindx = ++(*count);
605
b34976b6 606 return TRUE;
42751cf3 607}
30b30c21 608
062e2358 609/* Assign dynsym indices. In a shared library we generate a section
30b30c21
RH
610 symbol for each output section, which come first. Next come all of
611 the back-end allocated local dynamic syms, followed by the rest of
612 the global symbols. */
613
614unsigned long
268b6b39 615_bfd_elf_link_renumber_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
30b30c21
RH
616{
617 unsigned long dynsymcount = 0;
618
619 if (info->shared)
620 {
621 asection *p;
622 for (p = output_bfd->sections; p ; p = p->next)
bc0ba537
AM
623 if ((p->flags & SEC_EXCLUDE) == 0)
624 elf_section_data (p)->dynindx = ++dynsymcount;
30b30c21
RH
625 }
626
627 if (elf_hash_table (info)->dynlocal)
628 {
629 struct elf_link_local_dynamic_entry *p;
630 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
631 p->dynindx = ++dynsymcount;
632 }
633
634 elf_link_hash_traverse (elf_hash_table (info),
635 elf_link_renumber_hash_table_dynsyms,
636 &dynsymcount);
637
638 /* There is an unused NULL entry at the head of the table which
639 we must account for in our count. Unless there weren't any
640 symbols, which means we'll have no table at all. */
641 if (dynsymcount != 0)
642 ++dynsymcount;
643
644 return elf_hash_table (info)->dynsymcount = dynsymcount;
645}
252b5132 646
45d6a902
AM
647/* This function is called when we want to define a new symbol. It
648 handles the various cases which arise when we find a definition in
649 a dynamic object, or when there is already a definition in a
650 dynamic object. The new symbol is described by NAME, SYM, PSEC,
651 and PVALUE. We set SYM_HASH to the hash table entry. We set
652 OVERRIDE if the old symbol is overriding a new definition. We set
653 TYPE_CHANGE_OK if it is OK for the type to change. We set
654 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
655 change, we mean that we shouldn't warn if the type or size does
0f8a2703 656 change. */
45d6a902
AM
657
658bfd_boolean
268b6b39
AM
659_bfd_elf_merge_symbol (bfd *abfd,
660 struct bfd_link_info *info,
661 const char *name,
662 Elf_Internal_Sym *sym,
663 asection **psec,
664 bfd_vma *pvalue,
665 struct elf_link_hash_entry **sym_hash,
666 bfd_boolean *skip,
667 bfd_boolean *override,
668 bfd_boolean *type_change_ok,
0f8a2703 669 bfd_boolean *size_change_ok)
252b5132 670{
45d6a902
AM
671 asection *sec;
672 struct elf_link_hash_entry *h;
673 struct elf_link_hash_entry *flip;
674 int bind;
675 bfd *oldbfd;
676 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
79349b09 677 bfd_boolean newweak, oldweak;
45d6a902
AM
678
679 *skip = FALSE;
680 *override = FALSE;
681
682 sec = *psec;
683 bind = ELF_ST_BIND (sym->st_info);
684
685 if (! bfd_is_und_section (sec))
686 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
687 else
688 h = ((struct elf_link_hash_entry *)
689 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
690 if (h == NULL)
691 return FALSE;
692 *sym_hash = h;
252b5132 693
45d6a902
AM
694 /* This code is for coping with dynamic objects, and is only useful
695 if we are doing an ELF link. */
696 if (info->hash->creator != abfd->xvec)
697 return TRUE;
252b5132 698
45d6a902
AM
699 /* For merging, we only care about real symbols. */
700
701 while (h->root.type == bfd_link_hash_indirect
702 || h->root.type == bfd_link_hash_warning)
703 h = (struct elf_link_hash_entry *) h->root.u.i.link;
704
705 /* If we just created the symbol, mark it as being an ELF symbol.
706 Other than that, there is nothing to do--there is no merge issue
707 with a newly defined symbol--so we just return. */
708
709 if (h->root.type == bfd_link_hash_new)
252b5132 710 {
45d6a902
AM
711 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
712 return TRUE;
713 }
252b5132 714
45d6a902 715 /* OLDBFD is a BFD associated with the existing symbol. */
252b5132 716
45d6a902
AM
717 switch (h->root.type)
718 {
719 default:
720 oldbfd = NULL;
721 break;
252b5132 722
45d6a902
AM
723 case bfd_link_hash_undefined:
724 case bfd_link_hash_undefweak:
725 oldbfd = h->root.u.undef.abfd;
726 break;
727
728 case bfd_link_hash_defined:
729 case bfd_link_hash_defweak:
730 oldbfd = h->root.u.def.section->owner;
731 break;
732
733 case bfd_link_hash_common:
734 oldbfd = h->root.u.c.p->section->owner;
735 break;
736 }
737
738 /* In cases involving weak versioned symbols, we may wind up trying
739 to merge a symbol with itself. Catch that here, to avoid the
740 confusion that results if we try to override a symbol with
741 itself. The additional tests catch cases like
742 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
743 dynamic object, which we do want to handle here. */
744 if (abfd == oldbfd
745 && ((abfd->flags & DYNAMIC) == 0
746 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0))
747 return TRUE;
748
749 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
750 respectively, is from a dynamic object. */
751
752 if ((abfd->flags & DYNAMIC) != 0)
753 newdyn = TRUE;
754 else
755 newdyn = FALSE;
756
757 if (oldbfd != NULL)
758 olddyn = (oldbfd->flags & DYNAMIC) != 0;
759 else
760 {
761 asection *hsec;
762
763 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
764 indices used by MIPS ELF. */
765 switch (h->root.type)
252b5132 766 {
45d6a902
AM
767 default:
768 hsec = NULL;
769 break;
252b5132 770
45d6a902
AM
771 case bfd_link_hash_defined:
772 case bfd_link_hash_defweak:
773 hsec = h->root.u.def.section;
774 break;
252b5132 775
45d6a902
AM
776 case bfd_link_hash_common:
777 hsec = h->root.u.c.p->section;
778 break;
252b5132 779 }
252b5132 780
45d6a902
AM
781 if (hsec == NULL)
782 olddyn = FALSE;
783 else
784 olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0;
785 }
252b5132 786
45d6a902
AM
787 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
788 respectively, appear to be a definition rather than reference. */
789
790 if (bfd_is_und_section (sec) || bfd_is_com_section (sec))
791 newdef = FALSE;
792 else
793 newdef = TRUE;
794
795 if (h->root.type == bfd_link_hash_undefined
796 || h->root.type == bfd_link_hash_undefweak
797 || h->root.type == bfd_link_hash_common)
798 olddef = FALSE;
799 else
800 olddef = TRUE;
801
4cc11e76 802 /* We need to remember if a symbol has a definition in a dynamic
45d6a902
AM
803 object or is weak in all dynamic objects. Internal and hidden
804 visibility will make it unavailable to dynamic objects. */
805 if (newdyn && (h->elf_link_hash_flags & ELF_LINK_DYNAMIC_DEF) == 0)
806 {
807 if (!bfd_is_und_section (sec))
808 h->elf_link_hash_flags |= ELF_LINK_DYNAMIC_DEF;
809 else
252b5132 810 {
45d6a902
AM
811 /* Check if this symbol is weak in all dynamic objects. If it
812 is the first time we see it in a dynamic object, we mark
813 if it is weak. Otherwise, we clear it. */
814 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0)
79349b09 815 {
45d6a902
AM
816 if (bind == STB_WEAK)
817 h->elf_link_hash_flags |= ELF_LINK_DYNAMIC_WEAK;
252b5132 818 }
45d6a902
AM
819 else if (bind != STB_WEAK)
820 h->elf_link_hash_flags &= ~ELF_LINK_DYNAMIC_WEAK;
252b5132 821 }
45d6a902 822 }
252b5132 823
45d6a902
AM
824 /* If the old symbol has non-default visibility, we ignore the new
825 definition from a dynamic object. */
826 if (newdyn
9c7a29a3 827 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
828 && !bfd_is_und_section (sec))
829 {
830 *skip = TRUE;
831 /* Make sure this symbol is dynamic. */
832 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
833 /* A protected symbol has external availability. Make sure it is
834 recorded as dynamic.
835
836 FIXME: Should we check type and size for protected symbol? */
837 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
838 return _bfd_elf_link_record_dynamic_symbol (info, h);
839 else
840 return TRUE;
841 }
842 else if (!newdyn
9c7a29a3 843 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
45d6a902
AM
844 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
845 {
846 /* If the new symbol with non-default visibility comes from a
847 relocatable file and the old definition comes from a dynamic
848 object, we remove the old definition. */
849 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
850 h = *sym_hash;
1de1a317
L
851
852 if ((h->root.und_next || info->hash->undefs_tail == &h->root)
853 && bfd_is_und_section (sec))
854 {
855 /* If the new symbol is undefined and the old symbol was
856 also undefined before, we need to make sure
857 _bfd_generic_link_add_one_symbol doesn't mess
858 up the linker hash table undefs list. Since the old
859 definition came from a dynamic object, it is still on the
860 undefs list. */
861 h->root.type = bfd_link_hash_undefined;
862 /* FIXME: What if the new symbol is weak undefined? */
863 h->root.u.undef.abfd = abfd;
864 }
865 else
866 {
867 h->root.type = bfd_link_hash_new;
868 h->root.u.undef.abfd = NULL;
869 }
870
45d6a902 871 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
252b5132 872 {
45d6a902 873 h->elf_link_hash_flags &= ~ELF_LINK_HASH_DEF_DYNAMIC;
22d5e339
L
874 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_DYNAMIC
875 | ELF_LINK_DYNAMIC_DEF);
45d6a902
AM
876 }
877 /* FIXME: Should we check type and size for protected symbol? */
878 h->size = 0;
879 h->type = 0;
880 return TRUE;
881 }
14a793b2 882
79349b09
AM
883 /* Differentiate strong and weak symbols. */
884 newweak = bind == STB_WEAK;
885 oldweak = (h->root.type == bfd_link_hash_defweak
886 || h->root.type == bfd_link_hash_undefweak);
14a793b2 887
0f8a2703
AM
888 /* If a new weak symbol comes from a regular file and the old symbol
889 comes from a dynamic library, we treat the new one as strong.
890 Similarly, an old weak symbol from a regular file is treated as
891 strong when the new symbol comes from a dynamic library. Further,
892 an old weak symbol from a dynamic library is treated as strong if
893 the new symbol is from a dynamic library. This reflects the way
894 glibc's ld.so works. */
895 if (!newdyn && olddyn)
896 newweak = FALSE;
897 if (newdyn)
898 oldweak = FALSE;
899
79349b09
AM
900 /* It's OK to change the type if either the existing symbol or the
901 new symbol is weak. A type change is also OK if the old symbol
902 is undefined and the new symbol is defined. */
252b5132 903
79349b09
AM
904 if (oldweak
905 || newweak
906 || (newdef
907 && h->root.type == bfd_link_hash_undefined))
908 *type_change_ok = TRUE;
909
910 /* It's OK to change the size if either the existing symbol or the
911 new symbol is weak, or if the old symbol is undefined. */
912
913 if (*type_change_ok
914 || h->root.type == bfd_link_hash_undefined)
915 *size_change_ok = TRUE;
45d6a902 916
45d6a902
AM
917 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
918 symbol, respectively, appears to be a common symbol in a dynamic
919 object. If a symbol appears in an uninitialized section, and is
920 not weak, and is not a function, then it may be a common symbol
921 which was resolved when the dynamic object was created. We want
922 to treat such symbols specially, because they raise special
923 considerations when setting the symbol size: if the symbol
924 appears as a common symbol in a regular object, and the size in
925 the regular object is larger, we must make sure that we use the
926 larger size. This problematic case can always be avoided in C,
927 but it must be handled correctly when using Fortran shared
928 libraries.
929
930 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
931 likewise for OLDDYNCOMMON and OLDDEF.
932
933 Note that this test is just a heuristic, and that it is quite
934 possible to have an uninitialized symbol in a shared object which
935 is really a definition, rather than a common symbol. This could
936 lead to some minor confusion when the symbol really is a common
937 symbol in some regular object. However, I think it will be
938 harmless. */
939
940 if (newdyn
941 && newdef
79349b09 942 && !newweak
45d6a902
AM
943 && (sec->flags & SEC_ALLOC) != 0
944 && (sec->flags & SEC_LOAD) == 0
945 && sym->st_size > 0
45d6a902
AM
946 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
947 newdyncommon = TRUE;
948 else
949 newdyncommon = FALSE;
950
951 if (olddyn
952 && olddef
953 && h->root.type == bfd_link_hash_defined
954 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
955 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
956 && (h->root.u.def.section->flags & SEC_LOAD) == 0
957 && h->size > 0
958 && h->type != STT_FUNC)
959 olddyncommon = TRUE;
960 else
961 olddyncommon = FALSE;
962
45d6a902
AM
963 /* If both the old and the new symbols look like common symbols in a
964 dynamic object, set the size of the symbol to the larger of the
965 two. */
966
967 if (olddyncommon
968 && newdyncommon
969 && sym->st_size != h->size)
970 {
971 /* Since we think we have two common symbols, issue a multiple
972 common warning if desired. Note that we only warn if the
973 size is different. If the size is the same, we simply let
974 the old symbol override the new one as normally happens with
975 symbols defined in dynamic objects. */
976
977 if (! ((*info->callbacks->multiple_common)
978 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
979 h->size, abfd, bfd_link_hash_common, sym->st_size)))
980 return FALSE;
252b5132 981
45d6a902
AM
982 if (sym->st_size > h->size)
983 h->size = sym->st_size;
252b5132 984
45d6a902 985 *size_change_ok = TRUE;
252b5132
RH
986 }
987
45d6a902
AM
988 /* If we are looking at a dynamic object, and we have found a
989 definition, we need to see if the symbol was already defined by
990 some other object. If so, we want to use the existing
991 definition, and we do not want to report a multiple symbol
992 definition error; we do this by clobbering *PSEC to be
993 bfd_und_section_ptr.
994
995 We treat a common symbol as a definition if the symbol in the
996 shared library is a function, since common symbols always
997 represent variables; this can cause confusion in principle, but
998 any such confusion would seem to indicate an erroneous program or
999 shared library. We also permit a common symbol in a regular
79349b09 1000 object to override a weak symbol in a shared object. */
45d6a902
AM
1001
1002 if (newdyn
1003 && newdef
1004 && (olddef
1005 || (h->root.type == bfd_link_hash_common
79349b09 1006 && (newweak
0f8a2703 1007 || ELF_ST_TYPE (sym->st_info) == STT_FUNC))))
45d6a902
AM
1008 {
1009 *override = TRUE;
1010 newdef = FALSE;
1011 newdyncommon = FALSE;
252b5132 1012
45d6a902
AM
1013 *psec = sec = bfd_und_section_ptr;
1014 *size_change_ok = TRUE;
252b5132 1015
45d6a902
AM
1016 /* If we get here when the old symbol is a common symbol, then
1017 we are explicitly letting it override a weak symbol or
1018 function in a dynamic object, and we don't want to warn about
1019 a type change. If the old symbol is a defined symbol, a type
1020 change warning may still be appropriate. */
252b5132 1021
45d6a902
AM
1022 if (h->root.type == bfd_link_hash_common)
1023 *type_change_ok = TRUE;
1024 }
1025
1026 /* Handle the special case of an old common symbol merging with a
1027 new symbol which looks like a common symbol in a shared object.
1028 We change *PSEC and *PVALUE to make the new symbol look like a
1029 common symbol, and let _bfd_generic_link_add_one_symbol will do
1030 the right thing. */
1031
1032 if (newdyncommon
1033 && h->root.type == bfd_link_hash_common)
1034 {
1035 *override = TRUE;
1036 newdef = FALSE;
1037 newdyncommon = FALSE;
1038 *pvalue = sym->st_size;
1039 *psec = sec = bfd_com_section_ptr;
1040 *size_change_ok = TRUE;
1041 }
1042
1043 /* If the old symbol is from a dynamic object, and the new symbol is
1044 a definition which is not from a dynamic object, then the new
1045 symbol overrides the old symbol. Symbols from regular files
1046 always take precedence over symbols from dynamic objects, even if
1047 they are defined after the dynamic object in the link.
1048
1049 As above, we again permit a common symbol in a regular object to
1050 override a definition in a shared object if the shared object
0f8a2703 1051 symbol is a function or is weak. */
45d6a902
AM
1052
1053 flip = NULL;
1054 if (! newdyn
1055 && (newdef
1056 || (bfd_is_com_section (sec)
79349b09
AM
1057 && (oldweak
1058 || h->type == STT_FUNC)))
45d6a902
AM
1059 && olddyn
1060 && olddef
0f8a2703 1061 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
45d6a902
AM
1062 {
1063 /* Change the hash table entry to undefined, and let
1064 _bfd_generic_link_add_one_symbol do the right thing with the
1065 new definition. */
1066
1067 h->root.type = bfd_link_hash_undefined;
1068 h->root.u.undef.abfd = h->root.u.def.section->owner;
1069 *size_change_ok = TRUE;
1070
1071 olddef = FALSE;
1072 olddyncommon = FALSE;
1073
1074 /* We again permit a type change when a common symbol may be
1075 overriding a function. */
1076
1077 if (bfd_is_com_section (sec))
1078 *type_change_ok = TRUE;
1079
1080 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1081 flip = *sym_hash;
1082 else
1083 /* This union may have been set to be non-NULL when this symbol
1084 was seen in a dynamic object. We must force the union to be
1085 NULL, so that it is correct for a regular symbol. */
1086 h->verinfo.vertree = NULL;
1087 }
1088
1089 /* Handle the special case of a new common symbol merging with an
1090 old symbol that looks like it might be a common symbol defined in
1091 a shared object. Note that we have already handled the case in
1092 which a new common symbol should simply override the definition
1093 in the shared library. */
1094
1095 if (! newdyn
1096 && bfd_is_com_section (sec)
1097 && olddyncommon)
1098 {
1099 /* It would be best if we could set the hash table entry to a
1100 common symbol, but we don't know what to use for the section
1101 or the alignment. */
1102 if (! ((*info->callbacks->multiple_common)
1103 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1104 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1105 return FALSE;
1106
4cc11e76 1107 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1108 larger, pretend that the new symbol has its size. */
1109
1110 if (h->size > *pvalue)
1111 *pvalue = h->size;
1112
1113 /* FIXME: We no longer know the alignment required by the symbol
1114 in the dynamic object, so we just wind up using the one from
1115 the regular object. */
1116
1117 olddef = FALSE;
1118 olddyncommon = FALSE;
1119
1120 h->root.type = bfd_link_hash_undefined;
1121 h->root.u.undef.abfd = h->root.u.def.section->owner;
1122
1123 *size_change_ok = TRUE;
1124 *type_change_ok = TRUE;
1125
1126 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1127 flip = *sym_hash;
1128 else
1129 h->verinfo.vertree = NULL;
1130 }
1131
1132 if (flip != NULL)
1133 {
1134 /* Handle the case where we had a versioned symbol in a dynamic
1135 library and now find a definition in a normal object. In this
1136 case, we make the versioned symbol point to the normal one. */
9c5bfbb7 1137 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
45d6a902
AM
1138 flip->root.type = h->root.type;
1139 h->root.type = bfd_link_hash_indirect;
1140 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1141 (*bed->elf_backend_copy_indirect_symbol) (bed, flip, h);
1142 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1143 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
1144 {
1145 h->elf_link_hash_flags &= ~ELF_LINK_HASH_DEF_DYNAMIC;
1146 flip->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1147 }
1148 }
1149
45d6a902
AM
1150 return TRUE;
1151}
1152
1153/* This function is called to create an indirect symbol from the
1154 default for the symbol with the default version if needed. The
1155 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
0f8a2703 1156 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902
AM
1157
1158bfd_boolean
268b6b39
AM
1159_bfd_elf_add_default_symbol (bfd *abfd,
1160 struct bfd_link_info *info,
1161 struct elf_link_hash_entry *h,
1162 const char *name,
1163 Elf_Internal_Sym *sym,
1164 asection **psec,
1165 bfd_vma *value,
1166 bfd_boolean *dynsym,
0f8a2703 1167 bfd_boolean override)
45d6a902
AM
1168{
1169 bfd_boolean type_change_ok;
1170 bfd_boolean size_change_ok;
1171 bfd_boolean skip;
1172 char *shortname;
1173 struct elf_link_hash_entry *hi;
1174 struct bfd_link_hash_entry *bh;
9c5bfbb7 1175 const struct elf_backend_data *bed;
45d6a902
AM
1176 bfd_boolean collect;
1177 bfd_boolean dynamic;
1178 char *p;
1179 size_t len, shortlen;
1180 asection *sec;
1181
1182 /* If this symbol has a version, and it is the default version, we
1183 create an indirect symbol from the default name to the fully
1184 decorated name. This will cause external references which do not
1185 specify a version to be bound to this version of the symbol. */
1186 p = strchr (name, ELF_VER_CHR);
1187 if (p == NULL || p[1] != ELF_VER_CHR)
1188 return TRUE;
1189
1190 if (override)
1191 {
4cc11e76 1192 /* We are overridden by an old definition. We need to check if we
45d6a902
AM
1193 need to create the indirect symbol from the default name. */
1194 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1195 FALSE, FALSE);
1196 BFD_ASSERT (hi != NULL);
1197 if (hi == h)
1198 return TRUE;
1199 while (hi->root.type == bfd_link_hash_indirect
1200 || hi->root.type == bfd_link_hash_warning)
1201 {
1202 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1203 if (hi == h)
1204 return TRUE;
1205 }
1206 }
1207
1208 bed = get_elf_backend_data (abfd);
1209 collect = bed->collect;
1210 dynamic = (abfd->flags & DYNAMIC) != 0;
1211
1212 shortlen = p - name;
1213 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1214 if (shortname == NULL)
1215 return FALSE;
1216 memcpy (shortname, name, shortlen);
1217 shortname[shortlen] = '\0';
1218
1219 /* We are going to create a new symbol. Merge it with any existing
1220 symbol with this name. For the purposes of the merge, act as
1221 though we were defining the symbol we just defined, although we
1222 actually going to define an indirect symbol. */
1223 type_change_ok = FALSE;
1224 size_change_ok = FALSE;
1225 sec = *psec;
1226 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1227 &hi, &skip, &override, &type_change_ok,
0f8a2703 1228 &size_change_ok))
45d6a902
AM
1229 return FALSE;
1230
1231 if (skip)
1232 goto nondefault;
1233
1234 if (! override)
1235 {
1236 bh = &hi->root;
1237 if (! (_bfd_generic_link_add_one_symbol
1238 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
268b6b39 1239 0, name, FALSE, collect, &bh)))
45d6a902
AM
1240 return FALSE;
1241 hi = (struct elf_link_hash_entry *) bh;
1242 }
1243 else
1244 {
1245 /* In this case the symbol named SHORTNAME is overriding the
1246 indirect symbol we want to add. We were planning on making
1247 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1248 is the name without a version. NAME is the fully versioned
1249 name, and it is the default version.
1250
1251 Overriding means that we already saw a definition for the
1252 symbol SHORTNAME in a regular object, and it is overriding
1253 the symbol defined in the dynamic object.
1254
1255 When this happens, we actually want to change NAME, the
1256 symbol we just added, to refer to SHORTNAME. This will cause
1257 references to NAME in the shared object to become references
1258 to SHORTNAME in the regular object. This is what we expect
1259 when we override a function in a shared object: that the
1260 references in the shared object will be mapped to the
1261 definition in the regular object. */
1262
1263 while (hi->root.type == bfd_link_hash_indirect
1264 || hi->root.type == bfd_link_hash_warning)
1265 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1266
1267 h->root.type = bfd_link_hash_indirect;
1268 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1269 if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
1270 {
1271 h->elf_link_hash_flags &=~ ELF_LINK_HASH_DEF_DYNAMIC;
1272 hi->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
1273 if (hi->elf_link_hash_flags
1274 & (ELF_LINK_HASH_REF_REGULAR
1275 | ELF_LINK_HASH_DEF_REGULAR))
1276 {
1277 if (! _bfd_elf_link_record_dynamic_symbol (info, hi))
1278 return FALSE;
1279 }
1280 }
1281
1282 /* Now set HI to H, so that the following code will set the
1283 other fields correctly. */
1284 hi = h;
1285 }
1286
1287 /* If there is a duplicate definition somewhere, then HI may not
1288 point to an indirect symbol. We will have reported an error to
1289 the user in that case. */
1290
1291 if (hi->root.type == bfd_link_hash_indirect)
1292 {
1293 struct elf_link_hash_entry *ht;
1294
45d6a902
AM
1295 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1296 (*bed->elf_backend_copy_indirect_symbol) (bed, ht, hi);
1297
1298 /* See if the new flags lead us to realize that the symbol must
1299 be dynamic. */
1300 if (! *dynsym)
1301 {
1302 if (! dynamic)
1303 {
1304 if (info->shared
1305 || ((hi->elf_link_hash_flags
1306 & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1307 *dynsym = TRUE;
1308 }
1309 else
1310 {
1311 if ((hi->elf_link_hash_flags
1312 & ELF_LINK_HASH_REF_REGULAR) != 0)
1313 *dynsym = TRUE;
1314 }
1315 }
1316 }
1317
1318 /* We also need to define an indirection from the nondefault version
1319 of the symbol. */
1320
1321nondefault:
1322 len = strlen (name);
1323 shortname = bfd_hash_allocate (&info->hash->table, len);
1324 if (shortname == NULL)
1325 return FALSE;
1326 memcpy (shortname, name, shortlen);
1327 memcpy (shortname + shortlen, p + 1, len - shortlen);
1328
1329 /* Once again, merge with any existing symbol. */
1330 type_change_ok = FALSE;
1331 size_change_ok = FALSE;
1332 sec = *psec;
1333 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1334 &hi, &skip, &override, &type_change_ok,
0f8a2703 1335 &size_change_ok))
45d6a902
AM
1336 return FALSE;
1337
1338 if (skip)
1339 return TRUE;
1340
1341 if (override)
1342 {
1343 /* Here SHORTNAME is a versioned name, so we don't expect to see
1344 the type of override we do in the case above unless it is
4cc11e76 1345 overridden by a versioned definition. */
45d6a902
AM
1346 if (hi->root.type != bfd_link_hash_defined
1347 && hi->root.type != bfd_link_hash_defweak)
1348 (*_bfd_error_handler)
1349 (_("%s: warning: unexpected redefinition of indirect versioned symbol `%s'"),
1350 bfd_archive_filename (abfd), shortname);
1351 }
1352 else
1353 {
1354 bh = &hi->root;
1355 if (! (_bfd_generic_link_add_one_symbol
1356 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 1357 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
1358 return FALSE;
1359 hi = (struct elf_link_hash_entry *) bh;
1360
1361 /* If there is a duplicate definition somewhere, then HI may not
1362 point to an indirect symbol. We will have reported an error
1363 to the user in that case. */
1364
1365 if (hi->root.type == bfd_link_hash_indirect)
1366 {
45d6a902
AM
1367 (*bed->elf_backend_copy_indirect_symbol) (bed, h, hi);
1368
1369 /* See if the new flags lead us to realize that the symbol
1370 must be dynamic. */
1371 if (! *dynsym)
1372 {
1373 if (! dynamic)
1374 {
1375 if (info->shared
1376 || ((hi->elf_link_hash_flags
1377 & ELF_LINK_HASH_REF_DYNAMIC) != 0))
1378 *dynsym = TRUE;
1379 }
1380 else
1381 {
1382 if ((hi->elf_link_hash_flags
1383 & ELF_LINK_HASH_REF_REGULAR) != 0)
1384 *dynsym = TRUE;
1385 }
1386 }
1387 }
1388 }
1389
1390 return TRUE;
1391}
1392\f
1393/* This routine is used to export all defined symbols into the dynamic
1394 symbol table. It is called via elf_link_hash_traverse. */
1395
1396bfd_boolean
268b6b39 1397_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 1398{
268b6b39 1399 struct elf_info_failed *eif = data;
45d6a902
AM
1400
1401 /* Ignore indirect symbols. These are added by the versioning code. */
1402 if (h->root.type == bfd_link_hash_indirect)
1403 return TRUE;
1404
1405 if (h->root.type == bfd_link_hash_warning)
1406 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1407
1408 if (h->dynindx == -1
1409 && (h->elf_link_hash_flags
1410 & (ELF_LINK_HASH_DEF_REGULAR | ELF_LINK_HASH_REF_REGULAR)) != 0)
1411 {
1412 struct bfd_elf_version_tree *t;
1413 struct bfd_elf_version_expr *d;
1414
1415 for (t = eif->verdefs; t != NULL; t = t->next)
1416 {
108ba305 1417 if (t->globals.list != NULL)
45d6a902 1418 {
108ba305
JJ
1419 d = (*t->match) (&t->globals, NULL, h->root.root.string);
1420 if (d != NULL)
1421 goto doit;
45d6a902
AM
1422 }
1423
108ba305 1424 if (t->locals.list != NULL)
45d6a902 1425 {
108ba305
JJ
1426 d = (*t->match) (&t->locals, NULL, h->root.root.string);
1427 if (d != NULL)
1428 return TRUE;
45d6a902
AM
1429 }
1430 }
1431
1432 if (!eif->verdefs)
1433 {
1434 doit:
1435 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
1436 {
1437 eif->failed = TRUE;
1438 return FALSE;
1439 }
1440 }
1441 }
1442
1443 return TRUE;
1444}
1445\f
1446/* Look through the symbols which are defined in other shared
1447 libraries and referenced here. Update the list of version
1448 dependencies. This will be put into the .gnu.version_r section.
1449 This function is called via elf_link_hash_traverse. */
1450
1451bfd_boolean
268b6b39
AM
1452_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1453 void *data)
45d6a902 1454{
268b6b39 1455 struct elf_find_verdep_info *rinfo = data;
45d6a902
AM
1456 Elf_Internal_Verneed *t;
1457 Elf_Internal_Vernaux *a;
1458 bfd_size_type amt;
1459
1460 if (h->root.type == bfd_link_hash_warning)
1461 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1462
1463 /* We only care about symbols defined in shared objects with version
1464 information. */
1465 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
1466 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1467 || h->dynindx == -1
1468 || h->verinfo.verdef == NULL)
1469 return TRUE;
1470
1471 /* See if we already know about this version. */
1472 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1473 {
1474 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1475 continue;
1476
1477 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1478 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1479 return TRUE;
1480
1481 break;
1482 }
1483
1484 /* This is a new version. Add it to tree we are building. */
1485
1486 if (t == NULL)
1487 {
1488 amt = sizeof *t;
268b6b39 1489 t = bfd_zalloc (rinfo->output_bfd, amt);
45d6a902
AM
1490 if (t == NULL)
1491 {
1492 rinfo->failed = TRUE;
1493 return FALSE;
1494 }
1495
1496 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1497 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1498 elf_tdata (rinfo->output_bfd)->verref = t;
1499 }
1500
1501 amt = sizeof *a;
268b6b39 1502 a = bfd_zalloc (rinfo->output_bfd, amt);
45d6a902
AM
1503
1504 /* Note that we are copying a string pointer here, and testing it
1505 above. If bfd_elf_string_from_elf_section is ever changed to
1506 discard the string data when low in memory, this will have to be
1507 fixed. */
1508 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1509
1510 a->vna_flags = h->verinfo.verdef->vd_flags;
1511 a->vna_nextptr = t->vn_auxptr;
1512
1513 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1514 ++rinfo->vers;
1515
1516 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1517
1518 t->vn_auxptr = a;
1519
1520 return TRUE;
1521}
1522
1523/* Figure out appropriate versions for all the symbols. We may not
1524 have the version number script until we have read all of the input
1525 files, so until that point we don't know which symbols should be
1526 local. This function is called via elf_link_hash_traverse. */
1527
1528bfd_boolean
268b6b39 1529_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
1530{
1531 struct elf_assign_sym_version_info *sinfo;
1532 struct bfd_link_info *info;
9c5bfbb7 1533 const struct elf_backend_data *bed;
45d6a902
AM
1534 struct elf_info_failed eif;
1535 char *p;
1536 bfd_size_type amt;
1537
268b6b39 1538 sinfo = data;
45d6a902
AM
1539 info = sinfo->info;
1540
1541 if (h->root.type == bfd_link_hash_warning)
1542 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1543
1544 /* Fix the symbol flags. */
1545 eif.failed = FALSE;
1546 eif.info = info;
1547 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1548 {
1549 if (eif.failed)
1550 sinfo->failed = TRUE;
1551 return FALSE;
1552 }
1553
1554 /* We only need version numbers for symbols defined in regular
1555 objects. */
1556 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1557 return TRUE;
1558
1559 bed = get_elf_backend_data (sinfo->output_bfd);
1560 p = strchr (h->root.root.string, ELF_VER_CHR);
1561 if (p != NULL && h->verinfo.vertree == NULL)
1562 {
1563 struct bfd_elf_version_tree *t;
1564 bfd_boolean hidden;
1565
1566 hidden = TRUE;
1567
1568 /* There are two consecutive ELF_VER_CHR characters if this is
1569 not a hidden symbol. */
1570 ++p;
1571 if (*p == ELF_VER_CHR)
1572 {
1573 hidden = FALSE;
1574 ++p;
1575 }
1576
1577 /* If there is no version string, we can just return out. */
1578 if (*p == '\0')
1579 {
1580 if (hidden)
1581 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
1582 return TRUE;
1583 }
1584
1585 /* Look for the version. If we find it, it is no longer weak. */
1586 for (t = sinfo->verdefs; t != NULL; t = t->next)
1587 {
1588 if (strcmp (t->name, p) == 0)
1589 {
1590 size_t len;
1591 char *alc;
1592 struct bfd_elf_version_expr *d;
1593
1594 len = p - h->root.root.string;
268b6b39 1595 alc = bfd_malloc (len);
45d6a902
AM
1596 if (alc == NULL)
1597 return FALSE;
1598 memcpy (alc, h->root.root.string, len - 1);
1599 alc[len - 1] = '\0';
1600 if (alc[len - 2] == ELF_VER_CHR)
1601 alc[len - 2] = '\0';
1602
1603 h->verinfo.vertree = t;
1604 t->used = TRUE;
1605 d = NULL;
1606
108ba305
JJ
1607 if (t->globals.list != NULL)
1608 d = (*t->match) (&t->globals, NULL, alc);
45d6a902
AM
1609
1610 /* See if there is anything to force this symbol to
1611 local scope. */
108ba305 1612 if (d == NULL && t->locals.list != NULL)
45d6a902 1613 {
108ba305
JJ
1614 d = (*t->match) (&t->locals, NULL, alc);
1615 if (d != NULL
1616 && h->dynindx != -1
1617 && info->shared
1618 && ! info->export_dynamic)
1619 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
1620 }
1621
1622 free (alc);
1623 break;
1624 }
1625 }
1626
1627 /* If we are building an application, we need to create a
1628 version node for this version. */
36af4a4e 1629 if (t == NULL && info->executable)
45d6a902
AM
1630 {
1631 struct bfd_elf_version_tree **pp;
1632 int version_index;
1633
1634 /* If we aren't going to export this symbol, we don't need
1635 to worry about it. */
1636 if (h->dynindx == -1)
1637 return TRUE;
1638
1639 amt = sizeof *t;
108ba305 1640 t = bfd_zalloc (sinfo->output_bfd, amt);
45d6a902
AM
1641 if (t == NULL)
1642 {
1643 sinfo->failed = TRUE;
1644 return FALSE;
1645 }
1646
45d6a902 1647 t->name = p;
45d6a902
AM
1648 t->name_indx = (unsigned int) -1;
1649 t->used = TRUE;
1650
1651 version_index = 1;
1652 /* Don't count anonymous version tag. */
1653 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1654 version_index = 0;
1655 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1656 ++version_index;
1657 t->vernum = version_index;
1658
1659 *pp = t;
1660
1661 h->verinfo.vertree = t;
1662 }
1663 else if (t == NULL)
1664 {
1665 /* We could not find the version for a symbol when
1666 generating a shared archive. Return an error. */
1667 (*_bfd_error_handler)
1668 (_("%s: undefined versioned symbol name %s"),
1669 bfd_get_filename (sinfo->output_bfd), h->root.root.string);
1670 bfd_set_error (bfd_error_bad_value);
1671 sinfo->failed = TRUE;
1672 return FALSE;
1673 }
1674
1675 if (hidden)
1676 h->elf_link_hash_flags |= ELF_LINK_HIDDEN;
1677 }
1678
1679 /* If we don't have a version for this symbol, see if we can find
1680 something. */
1681 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1682 {
1683 struct bfd_elf_version_tree *t;
1684 struct bfd_elf_version_tree *local_ver;
1685 struct bfd_elf_version_expr *d;
1686
1687 /* See if can find what version this symbol is in. If the
1688 symbol is supposed to be local, then don't actually register
1689 it. */
1690 local_ver = NULL;
1691 for (t = sinfo->verdefs; t != NULL; t = t->next)
1692 {
108ba305 1693 if (t->globals.list != NULL)
45d6a902
AM
1694 {
1695 bfd_boolean matched;
1696
1697 matched = FALSE;
108ba305
JJ
1698 d = NULL;
1699 while ((d = (*t->match) (&t->globals, d,
1700 h->root.root.string)) != NULL)
1701 if (d->symver)
1702 matched = TRUE;
1703 else
1704 {
1705 /* There is a version without definition. Make
1706 the symbol the default definition for this
1707 version. */
1708 h->verinfo.vertree = t;
1709 local_ver = NULL;
1710 d->script = 1;
1711 break;
1712 }
45d6a902
AM
1713 if (d != NULL)
1714 break;
1715 else if (matched)
1716 /* There is no undefined version for this symbol. Hide the
1717 default one. */
1718 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1719 }
1720
108ba305 1721 if (t->locals.list != NULL)
45d6a902 1722 {
108ba305
JJ
1723 d = NULL;
1724 while ((d = (*t->match) (&t->locals, d,
1725 h->root.root.string)) != NULL)
45d6a902 1726 {
108ba305 1727 local_ver = t;
45d6a902 1728 /* If the match is "*", keep looking for a more
108ba305
JJ
1729 explicit, perhaps even global, match.
1730 XXX: Shouldn't this be !d->wildcard instead? */
1731 if (d->pattern[0] != '*' || d->pattern[1] != '\0')
1732 break;
45d6a902
AM
1733 }
1734
1735 if (d != NULL)
1736 break;
1737 }
1738 }
1739
1740 if (local_ver != NULL)
1741 {
1742 h->verinfo.vertree = local_ver;
1743 if (h->dynindx != -1
1744 && info->shared
1745 && ! info->export_dynamic)
1746 {
1747 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1748 }
1749 }
1750 }
1751
1752 return TRUE;
1753}
1754\f
45d6a902
AM
1755/* Read and swap the relocs from the section indicated by SHDR. This
1756 may be either a REL or a RELA section. The relocations are
1757 translated into RELA relocations and stored in INTERNAL_RELOCS,
1758 which should have already been allocated to contain enough space.
1759 The EXTERNAL_RELOCS are a buffer where the external form of the
1760 relocations should be stored.
1761
1762 Returns FALSE if something goes wrong. */
1763
1764static bfd_boolean
268b6b39 1765elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 1766 asection *sec,
268b6b39
AM
1767 Elf_Internal_Shdr *shdr,
1768 void *external_relocs,
1769 Elf_Internal_Rela *internal_relocs)
45d6a902 1770{
9c5bfbb7 1771 const struct elf_backend_data *bed;
268b6b39 1772 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
1773 const bfd_byte *erela;
1774 const bfd_byte *erelaend;
1775 Elf_Internal_Rela *irela;
243ef1e0
L
1776 Elf_Internal_Shdr *symtab_hdr;
1777 size_t nsyms;
45d6a902 1778
45d6a902
AM
1779 /* Position ourselves at the start of the section. */
1780 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
1781 return FALSE;
1782
1783 /* Read the relocations. */
1784 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
1785 return FALSE;
1786
243ef1e0
L
1787 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1788 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
1789
45d6a902
AM
1790 bed = get_elf_backend_data (abfd);
1791
1792 /* Convert the external relocations to the internal format. */
1793 if (shdr->sh_entsize == bed->s->sizeof_rel)
1794 swap_in = bed->s->swap_reloc_in;
1795 else if (shdr->sh_entsize == bed->s->sizeof_rela)
1796 swap_in = bed->s->swap_reloca_in;
1797 else
1798 {
1799 bfd_set_error (bfd_error_wrong_format);
1800 return FALSE;
1801 }
1802
1803 erela = external_relocs;
51992aec 1804 erelaend = erela + shdr->sh_size;
45d6a902
AM
1805 irela = internal_relocs;
1806 while (erela < erelaend)
1807 {
243ef1e0
L
1808 bfd_vma r_symndx;
1809
45d6a902 1810 (*swap_in) (abfd, erela, irela);
243ef1e0
L
1811 r_symndx = ELF32_R_SYM (irela->r_info);
1812 if (bed->s->arch_size == 64)
1813 r_symndx >>= 24;
1814 if ((size_t) r_symndx >= nsyms)
1815 {
1816 (*_bfd_error_handler)
1817 (_("%s: bad reloc symbol index (0x%lx >= 0x%lx) for offset 0x%lx in section `%s'"),
1818 bfd_archive_filename (abfd), (unsigned long) r_symndx,
1819 (unsigned long) nsyms, irela->r_offset, sec->name);
1820 bfd_set_error (bfd_error_bad_value);
1821 return FALSE;
1822 }
45d6a902
AM
1823 irela += bed->s->int_rels_per_ext_rel;
1824 erela += shdr->sh_entsize;
1825 }
1826
1827 return TRUE;
1828}
1829
1830/* Read and swap the relocs for a section O. They may have been
1831 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1832 not NULL, they are used as buffers to read into. They are known to
1833 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1834 the return value is allocated using either malloc or bfd_alloc,
1835 according to the KEEP_MEMORY argument. If O has two relocation
1836 sections (both REL and RELA relocations), then the REL_HDR
1837 relocations will appear first in INTERNAL_RELOCS, followed by the
1838 REL_HDR2 relocations. */
1839
1840Elf_Internal_Rela *
268b6b39
AM
1841_bfd_elf_link_read_relocs (bfd *abfd,
1842 asection *o,
1843 void *external_relocs,
1844 Elf_Internal_Rela *internal_relocs,
1845 bfd_boolean keep_memory)
45d6a902
AM
1846{
1847 Elf_Internal_Shdr *rel_hdr;
268b6b39 1848 void *alloc1 = NULL;
45d6a902 1849 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 1850 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
45d6a902
AM
1851
1852 if (elf_section_data (o)->relocs != NULL)
1853 return elf_section_data (o)->relocs;
1854
1855 if (o->reloc_count == 0)
1856 return NULL;
1857
1858 rel_hdr = &elf_section_data (o)->rel_hdr;
1859
1860 if (internal_relocs == NULL)
1861 {
1862 bfd_size_type size;
1863
1864 size = o->reloc_count;
1865 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
1866 if (keep_memory)
268b6b39 1867 internal_relocs = bfd_alloc (abfd, size);
45d6a902 1868 else
268b6b39 1869 internal_relocs = alloc2 = bfd_malloc (size);
45d6a902
AM
1870 if (internal_relocs == NULL)
1871 goto error_return;
1872 }
1873
1874 if (external_relocs == NULL)
1875 {
1876 bfd_size_type size = rel_hdr->sh_size;
1877
1878 if (elf_section_data (o)->rel_hdr2)
1879 size += elf_section_data (o)->rel_hdr2->sh_size;
268b6b39 1880 alloc1 = bfd_malloc (size);
45d6a902
AM
1881 if (alloc1 == NULL)
1882 goto error_return;
1883 external_relocs = alloc1;
1884 }
1885
243ef1e0 1886 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
45d6a902
AM
1887 external_relocs,
1888 internal_relocs))
1889 goto error_return;
51992aec
AM
1890 if (elf_section_data (o)->rel_hdr2
1891 && (!elf_link_read_relocs_from_section
1892 (abfd, o,
1893 elf_section_data (o)->rel_hdr2,
1894 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
1895 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
1896 * bed->s->int_rels_per_ext_rel))))
45d6a902
AM
1897 goto error_return;
1898
1899 /* Cache the results for next time, if we can. */
1900 if (keep_memory)
1901 elf_section_data (o)->relocs = internal_relocs;
1902
1903 if (alloc1 != NULL)
1904 free (alloc1);
1905
1906 /* Don't free alloc2, since if it was allocated we are passing it
1907 back (under the name of internal_relocs). */
1908
1909 return internal_relocs;
1910
1911 error_return:
1912 if (alloc1 != NULL)
1913 free (alloc1);
1914 if (alloc2 != NULL)
1915 free (alloc2);
1916 return NULL;
1917}
1918
1919/* Compute the size of, and allocate space for, REL_HDR which is the
1920 section header for a section containing relocations for O. */
1921
1922bfd_boolean
268b6b39
AM
1923_bfd_elf_link_size_reloc_section (bfd *abfd,
1924 Elf_Internal_Shdr *rel_hdr,
1925 asection *o)
45d6a902
AM
1926{
1927 bfd_size_type reloc_count;
1928 bfd_size_type num_rel_hashes;
1929
1930 /* Figure out how many relocations there will be. */
1931 if (rel_hdr == &elf_section_data (o)->rel_hdr)
1932 reloc_count = elf_section_data (o)->rel_count;
1933 else
1934 reloc_count = elf_section_data (o)->rel_count2;
1935
1936 num_rel_hashes = o->reloc_count;
1937 if (num_rel_hashes < reloc_count)
1938 num_rel_hashes = reloc_count;
1939
1940 /* That allows us to calculate the size of the section. */
1941 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
1942
1943 /* The contents field must last into write_object_contents, so we
1944 allocate it with bfd_alloc rather than malloc. Also since we
1945 cannot be sure that the contents will actually be filled in,
1946 we zero the allocated space. */
268b6b39 1947 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
1948 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
1949 return FALSE;
1950
1951 /* We only allocate one set of hash entries, so we only do it the
1952 first time we are called. */
1953 if (elf_section_data (o)->rel_hashes == NULL
1954 && num_rel_hashes)
1955 {
1956 struct elf_link_hash_entry **p;
1957
268b6b39 1958 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
45d6a902
AM
1959 if (p == NULL)
1960 return FALSE;
1961
1962 elf_section_data (o)->rel_hashes = p;
1963 }
1964
1965 return TRUE;
1966}
1967
1968/* Copy the relocations indicated by the INTERNAL_RELOCS (which
1969 originated from the section given by INPUT_REL_HDR) to the
1970 OUTPUT_BFD. */
1971
1972bfd_boolean
268b6b39
AM
1973_bfd_elf_link_output_relocs (bfd *output_bfd,
1974 asection *input_section,
1975 Elf_Internal_Shdr *input_rel_hdr,
1976 Elf_Internal_Rela *internal_relocs)
45d6a902
AM
1977{
1978 Elf_Internal_Rela *irela;
1979 Elf_Internal_Rela *irelaend;
1980 bfd_byte *erel;
1981 Elf_Internal_Shdr *output_rel_hdr;
1982 asection *output_section;
1983 unsigned int *rel_countp = NULL;
9c5bfbb7 1984 const struct elf_backend_data *bed;
268b6b39 1985 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
45d6a902
AM
1986
1987 output_section = input_section->output_section;
1988 output_rel_hdr = NULL;
1989
1990 if (elf_section_data (output_section)->rel_hdr.sh_entsize
1991 == input_rel_hdr->sh_entsize)
1992 {
1993 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
1994 rel_countp = &elf_section_data (output_section)->rel_count;
1995 }
1996 else if (elf_section_data (output_section)->rel_hdr2
1997 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
1998 == input_rel_hdr->sh_entsize))
1999 {
2000 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2001 rel_countp = &elf_section_data (output_section)->rel_count2;
2002 }
2003 else
2004 {
2005 (*_bfd_error_handler)
2006 (_("%s: relocation size mismatch in %s section %s"),
2007 bfd_get_filename (output_bfd),
2008 bfd_archive_filename (input_section->owner),
2009 input_section->name);
2010 bfd_set_error (bfd_error_wrong_object_format);
2011 return FALSE;
2012 }
2013
2014 bed = get_elf_backend_data (output_bfd);
2015 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2016 swap_out = bed->s->swap_reloc_out;
2017 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2018 swap_out = bed->s->swap_reloca_out;
2019 else
2020 abort ();
2021
2022 erel = output_rel_hdr->contents;
2023 erel += *rel_countp * input_rel_hdr->sh_entsize;
2024 irela = internal_relocs;
2025 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2026 * bed->s->int_rels_per_ext_rel);
2027 while (irela < irelaend)
2028 {
2029 (*swap_out) (output_bfd, irela, erel);
2030 irela += bed->s->int_rels_per_ext_rel;
2031 erel += input_rel_hdr->sh_entsize;
2032 }
2033
2034 /* Bump the counter, so that we know where to add the next set of
2035 relocations. */
2036 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2037
2038 return TRUE;
2039}
2040\f
2041/* Fix up the flags for a symbol. This handles various cases which
2042 can only be fixed after all the input files are seen. This is
2043 currently called by both adjust_dynamic_symbol and
2044 assign_sym_version, which is unnecessary but perhaps more robust in
2045 the face of future changes. */
2046
2047bfd_boolean
268b6b39
AM
2048_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2049 struct elf_info_failed *eif)
45d6a902
AM
2050{
2051 /* If this symbol was mentioned in a non-ELF file, try to set
2052 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2053 permit a non-ELF file to correctly refer to a symbol defined in
2054 an ELF dynamic object. */
2055 if ((h->elf_link_hash_flags & ELF_LINK_NON_ELF) != 0)
2056 {
2057 while (h->root.type == bfd_link_hash_indirect)
2058 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2059
2060 if (h->root.type != bfd_link_hash_defined
2061 && h->root.type != bfd_link_hash_defweak)
2062 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
2063 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
2064 else
2065 {
2066 if (h->root.u.def.section->owner != NULL
2067 && (bfd_get_flavour (h->root.u.def.section->owner)
2068 == bfd_target_elf_flavour))
2069 h->elf_link_hash_flags |= (ELF_LINK_HASH_REF_REGULAR
2070 | ELF_LINK_HASH_REF_REGULAR_NONWEAK);
2071 else
2072 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2073 }
2074
2075 if (h->dynindx == -1
2076 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2077 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0))
2078 {
2079 if (! _bfd_elf_link_record_dynamic_symbol (eif->info, h))
2080 {
2081 eif->failed = TRUE;
2082 return FALSE;
2083 }
2084 }
2085 }
2086 else
2087 {
2088 /* Unfortunately, ELF_LINK_NON_ELF is only correct if the symbol
2089 was first seen in a non-ELF file. Fortunately, if the symbol
2090 was first seen in an ELF file, we're probably OK unless the
2091 symbol was defined in a non-ELF file. Catch that case here.
2092 FIXME: We're still in trouble if the symbol was first seen in
2093 a dynamic object, and then later in a non-ELF regular object. */
2094 if ((h->root.type == bfd_link_hash_defined
2095 || h->root.type == bfd_link_hash_defweak)
2096 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
2097 && (h->root.u.def.section->owner != NULL
2098 ? (bfd_get_flavour (h->root.u.def.section->owner)
2099 != bfd_target_elf_flavour)
2100 : (bfd_is_abs_section (h->root.u.def.section)
2101 && (h->elf_link_hash_flags
2102 & ELF_LINK_HASH_DEF_DYNAMIC) == 0)))
2103 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2104 }
2105
2106 /* If this is a final link, and the symbol was defined as a common
2107 symbol in a regular object file, and there was no definition in
2108 any dynamic object, then the linker will have allocated space for
2109 the symbol in a common section but the ELF_LINK_HASH_DEF_REGULAR
2110 flag will not have been set. */
2111 if (h->root.type == bfd_link_hash_defined
2112 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
2113 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
2114 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
2115 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2116 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
2117
2118 /* If -Bsymbolic was used (which means to bind references to global
2119 symbols to the definition within the shared object), and this
2120 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2121 need a PLT entry. Likewise, if the symbol has non-default
2122 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2123 will force it local. */
45d6a902
AM
2124 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0
2125 && eif->info->shared
0eddce27 2126 && is_elf_hash_table (eif->info->hash)
45d6a902 2127 && (eif->info->symbolic
c1be741f 2128 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
45d6a902
AM
2129 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
2130 {
9c5bfbb7 2131 const struct elf_backend_data *bed;
45d6a902
AM
2132 bfd_boolean force_local;
2133
2134 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2135
2136 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2137 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2138 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2139 }
2140
2141 /* If a weak undefined symbol has non-default visibility, we also
2142 hide it from the dynamic linker. */
9c7a29a3 2143 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
2144 && h->root.type == bfd_link_hash_undefweak)
2145 {
9c5bfbb7 2146 const struct elf_backend_data *bed;
45d6a902
AM
2147 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2148 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2149 }
2150
2151 /* If this is a weak defined symbol in a dynamic object, and we know
2152 the real definition in the dynamic object, copy interesting flags
2153 over to the real definition. */
2154 if (h->weakdef != NULL)
2155 {
2156 struct elf_link_hash_entry *weakdef;
2157
2158 weakdef = h->weakdef;
2159 if (h->root.type == bfd_link_hash_indirect)
2160 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2161
2162 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2163 || h->root.type == bfd_link_hash_defweak);
2164 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2165 || weakdef->root.type == bfd_link_hash_defweak);
2166 BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC);
2167
2168 /* If the real definition is defined by a regular object file,
2169 don't do anything special. See the longer description in
2170 _bfd_elf_adjust_dynamic_symbol, below. */
2171 if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
2172 h->weakdef = NULL;
2173 else
2174 {
9c5bfbb7 2175 const struct elf_backend_data *bed;
45d6a902
AM
2176
2177 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2178 (*bed->elf_backend_copy_indirect_symbol) (bed, weakdef, h);
2179 }
2180 }
2181
2182 return TRUE;
2183}
2184
2185/* Make the backend pick a good value for a dynamic symbol. This is
2186 called via elf_link_hash_traverse, and also calls itself
2187 recursively. */
2188
2189bfd_boolean
268b6b39 2190_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2191{
268b6b39 2192 struct elf_info_failed *eif = data;
45d6a902 2193 bfd *dynobj;
9c5bfbb7 2194 const struct elf_backend_data *bed;
45d6a902 2195
0eddce27 2196 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2197 return FALSE;
2198
2199 if (h->root.type == bfd_link_hash_warning)
2200 {
2201 h->plt = elf_hash_table (eif->info)->init_offset;
2202 h->got = elf_hash_table (eif->info)->init_offset;
2203
2204 /* When warning symbols are created, they **replace** the "real"
2205 entry in the hash table, thus we never get to see the real
2206 symbol in a hash traversal. So look at it now. */
2207 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2208 }
2209
2210 /* Ignore indirect symbols. These are added by the versioning code. */
2211 if (h->root.type == bfd_link_hash_indirect)
2212 return TRUE;
2213
2214 /* Fix the symbol flags. */
2215 if (! _bfd_elf_fix_symbol_flags (h, eif))
2216 return FALSE;
2217
2218 /* If this symbol does not require a PLT entry, and it is not
2219 defined by a dynamic object, or is not referenced by a regular
2220 object, ignore it. We do have to handle a weak defined symbol,
2221 even if no regular object refers to it, if we decided to add it
2222 to the dynamic symbol table. FIXME: Do we normally need to worry
2223 about symbols which are defined by one dynamic object and
2224 referenced by another one? */
2225 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0
2226 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
2227 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
2228 || ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0
2229 && (h->weakdef == NULL || h->weakdef->dynindx == -1))))
2230 {
2231 h->plt = elf_hash_table (eif->info)->init_offset;
2232 return TRUE;
2233 }
2234
2235 /* If we've already adjusted this symbol, don't do it again. This
2236 can happen via a recursive call. */
2237 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
2238 return TRUE;
2239
2240 /* Don't look at this symbol again. Note that we must set this
2241 after checking the above conditions, because we may look at a
2242 symbol once, decide not to do anything, and then get called
2243 recursively later after REF_REGULAR is set below. */
2244 h->elf_link_hash_flags |= ELF_LINK_HASH_DYNAMIC_ADJUSTED;
2245
2246 /* If this is a weak definition, and we know a real definition, and
2247 the real symbol is not itself defined by a regular object file,
2248 then get a good value for the real definition. We handle the
2249 real symbol first, for the convenience of the backend routine.
2250
2251 Note that there is a confusing case here. If the real definition
2252 is defined by a regular object file, we don't get the real symbol
2253 from the dynamic object, but we do get the weak symbol. If the
2254 processor backend uses a COPY reloc, then if some routine in the
2255 dynamic object changes the real symbol, we will not see that
2256 change in the corresponding weak symbol. This is the way other
2257 ELF linkers work as well, and seems to be a result of the shared
2258 library model.
2259
2260 I will clarify this issue. Most SVR4 shared libraries define the
2261 variable _timezone and define timezone as a weak synonym. The
2262 tzset call changes _timezone. If you write
2263 extern int timezone;
2264 int _timezone = 5;
2265 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2266 you might expect that, since timezone is a synonym for _timezone,
2267 the same number will print both times. However, if the processor
2268 backend uses a COPY reloc, then actually timezone will be copied
2269 into your process image, and, since you define _timezone
2270 yourself, _timezone will not. Thus timezone and _timezone will
2271 wind up at different memory locations. The tzset call will set
2272 _timezone, leaving timezone unchanged. */
2273
2274 if (h->weakdef != NULL)
2275 {
2276 /* If we get to this point, we know there is an implicit
2277 reference by a regular object file via the weak symbol H.
2278 FIXME: Is this really true? What if the traversal finds
2279 H->WEAKDEF before it finds H? */
2280 h->weakdef->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
2281
268b6b39 2282 if (! _bfd_elf_adjust_dynamic_symbol (h->weakdef, eif))
45d6a902
AM
2283 return FALSE;
2284 }
2285
2286 /* If a symbol has no type and no size and does not require a PLT
2287 entry, then we are probably about to do the wrong thing here: we
2288 are probably going to create a COPY reloc for an empty object.
2289 This case can arise when a shared object is built with assembly
2290 code, and the assembly code fails to set the symbol type. */
2291 if (h->size == 0
2292 && h->type == STT_NOTYPE
2293 && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0)
2294 (*_bfd_error_handler)
2295 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2296 h->root.root.string);
2297
2298 dynobj = elf_hash_table (eif->info)->dynobj;
2299 bed = get_elf_backend_data (dynobj);
2300 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2301 {
2302 eif->failed = TRUE;
2303 return FALSE;
2304 }
2305
2306 return TRUE;
2307}
2308
2309/* Adjust all external symbols pointing into SEC_MERGE sections
2310 to reflect the object merging within the sections. */
2311
2312bfd_boolean
268b6b39 2313_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
2314{
2315 asection *sec;
2316
2317 if (h->root.type == bfd_link_hash_warning)
2318 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2319
2320 if ((h->root.type == bfd_link_hash_defined
2321 || h->root.type == bfd_link_hash_defweak)
2322 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2323 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2324 {
268b6b39 2325 bfd *output_bfd = data;
45d6a902
AM
2326
2327 h->root.u.def.value =
2328 _bfd_merged_section_offset (output_bfd,
2329 &h->root.u.def.section,
2330 elf_section_data (sec)->sec_info,
268b6b39 2331 h->root.u.def.value, 0);
45d6a902
AM
2332 }
2333
2334 return TRUE;
2335}
986a241f
RH
2336
2337/* Returns false if the symbol referred to by H should be considered
2338 to resolve local to the current module, and true if it should be
2339 considered to bind dynamically. */
2340
2341bfd_boolean
268b6b39
AM
2342_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2343 struct bfd_link_info *info,
2344 bfd_boolean ignore_protected)
986a241f
RH
2345{
2346 bfd_boolean binding_stays_local_p;
2347
2348 if (h == NULL)
2349 return FALSE;
2350
2351 while (h->root.type == bfd_link_hash_indirect
2352 || h->root.type == bfd_link_hash_warning)
2353 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2354
2355 /* If it was forced local, then clearly it's not dynamic. */
2356 if (h->dynindx == -1)
2357 return FALSE;
2358 if (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
2359 return FALSE;
2360
2361 /* Identify the cases where name binding rules say that a
2362 visible symbol resolves locally. */
2363 binding_stays_local_p = info->executable || info->symbolic;
2364
2365 switch (ELF_ST_VISIBILITY (h->other))
2366 {
2367 case STV_INTERNAL:
2368 case STV_HIDDEN:
2369 return FALSE;
2370
2371 case STV_PROTECTED:
2372 /* Proper resolution for function pointer equality may require
2373 that these symbols perhaps be resolved dynamically, even though
2374 we should be resolving them to the current module. */
2375 if (!ignore_protected)
2376 binding_stays_local_p = TRUE;
2377 break;
2378
2379 default:
986a241f
RH
2380 break;
2381 }
2382
aa37626c
L
2383 /* If it isn't defined locally, then clearly it's dynamic. */
2384 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2385 return TRUE;
2386
986a241f
RH
2387 /* Otherwise, the symbol is dynamic if binding rules don't tell
2388 us that it remains local. */
2389 return !binding_stays_local_p;
2390}
f6c52c13
AM
2391
2392/* Return true if the symbol referred to by H should be considered
2393 to resolve local to the current module, and false otherwise. Differs
2394 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2395 undefined symbols and weak symbols. */
2396
2397bfd_boolean
268b6b39
AM
2398_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2399 struct bfd_link_info *info,
2400 bfd_boolean local_protected)
f6c52c13
AM
2401{
2402 /* If it's a local sym, of course we resolve locally. */
2403 if (h == NULL)
2404 return TRUE;
2405
2406 /* If we don't have a definition in a regular file, then we can't
2407 resolve locally. The sym is either undefined or dynamic. */
2408 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2409 return FALSE;
2410
2411 /* Forced local symbols resolve locally. */
2412 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
2413 return TRUE;
2414
2415 /* As do non-dynamic symbols. */
2416 if (h->dynindx == -1)
2417 return TRUE;
2418
2419 /* At this point, we know the symbol is defined and dynamic. In an
2420 executable it must resolve locally, likewise when building symbolic
2421 shared libraries. */
2422 if (info->executable || info->symbolic)
2423 return TRUE;
2424
2425 /* Now deal with defined dynamic symbols in shared libraries. Ones
2426 with default visibility might not resolve locally. */
2427 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2428 return FALSE;
2429
2430 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2431 if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED)
2432 return TRUE;
2433
2434 /* Function pointer equality tests may require that STV_PROTECTED
2435 symbols be treated as dynamic symbols, even when we know that the
2436 dynamic linker will resolve them locally. */
2437 return local_protected;
2438}
e1918d23
AM
2439
2440/* Caches some TLS segment info, and ensures that the TLS segment vma is
2441 aligned. Returns the first TLS output section. */
2442
2443struct bfd_section *
2444_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2445{
2446 struct bfd_section *sec, *tls;
2447 unsigned int align = 0;
2448
2449 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2450 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2451 break;
2452 tls = sec;
2453
2454 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2455 if (sec->alignment_power > align)
2456 align = sec->alignment_power;
2457
2458 elf_hash_table (info)->tls_sec = tls;
2459
2460 /* Ensure the alignment of the first section is the largest alignment,
2461 so that the tls segment starts aligned. */
2462 if (tls != NULL)
2463 tls->alignment_power = align;
2464
2465 return tls;
2466}
0ad989f9
L
2467
2468/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2469static bfd_boolean
2470is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2471 Elf_Internal_Sym *sym)
2472{
2473 /* Local symbols do not count, but target specific ones might. */
2474 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2475 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2476 return FALSE;
2477
2478 /* Function symbols do not count. */
2479 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
2480 return FALSE;
2481
2482 /* If the section is undefined, then so is the symbol. */
2483 if (sym->st_shndx == SHN_UNDEF)
2484 return FALSE;
2485
2486 /* If the symbol is defined in the common section, then
2487 it is a common definition and so does not count. */
2488 if (sym->st_shndx == SHN_COMMON)
2489 return FALSE;
2490
2491 /* If the symbol is in a target specific section then we
2492 must rely upon the backend to tell us what it is. */
2493 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2494 /* FIXME - this function is not coded yet:
2495
2496 return _bfd_is_global_symbol_definition (abfd, sym);
2497
2498 Instead for now assume that the definition is not global,
2499 Even if this is wrong, at least the linker will behave
2500 in the same way that it used to do. */
2501 return FALSE;
2502
2503 return TRUE;
2504}
2505
2506/* Search the symbol table of the archive element of the archive ABFD
2507 whose archive map contains a mention of SYMDEF, and determine if
2508 the symbol is defined in this element. */
2509static bfd_boolean
2510elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2511{
2512 Elf_Internal_Shdr * hdr;
2513 bfd_size_type symcount;
2514 bfd_size_type extsymcount;
2515 bfd_size_type extsymoff;
2516 Elf_Internal_Sym *isymbuf;
2517 Elf_Internal_Sym *isym;
2518 Elf_Internal_Sym *isymend;
2519 bfd_boolean result;
2520
2521 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2522 if (abfd == NULL)
2523 return FALSE;
2524
2525 if (! bfd_check_format (abfd, bfd_object))
2526 return FALSE;
2527
2528 /* If we have already included the element containing this symbol in the
2529 link then we do not need to include it again. Just claim that any symbol
2530 it contains is not a definition, so that our caller will not decide to
2531 (re)include this element. */
2532 if (abfd->archive_pass)
2533 return FALSE;
2534
2535 /* Select the appropriate symbol table. */
2536 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2537 hdr = &elf_tdata (abfd)->symtab_hdr;
2538 else
2539 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2540
2541 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2542
2543 /* The sh_info field of the symtab header tells us where the
2544 external symbols start. We don't care about the local symbols. */
2545 if (elf_bad_symtab (abfd))
2546 {
2547 extsymcount = symcount;
2548 extsymoff = 0;
2549 }
2550 else
2551 {
2552 extsymcount = symcount - hdr->sh_info;
2553 extsymoff = hdr->sh_info;
2554 }
2555
2556 if (extsymcount == 0)
2557 return FALSE;
2558
2559 /* Read in the symbol table. */
2560 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2561 NULL, NULL, NULL);
2562 if (isymbuf == NULL)
2563 return FALSE;
2564
2565 /* Scan the symbol table looking for SYMDEF. */
2566 result = FALSE;
2567 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2568 {
2569 const char *name;
2570
2571 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
2572 isym->st_name);
2573 if (name == NULL)
2574 break;
2575
2576 if (strcmp (name, symdef->name) == 0)
2577 {
2578 result = is_global_data_symbol_definition (abfd, isym);
2579 break;
2580 }
2581 }
2582
2583 free (isymbuf);
2584
2585 return result;
2586}
2587\f
2588/* Add symbols from an ELF archive file to the linker hash table. We
2589 don't use _bfd_generic_link_add_archive_symbols because of a
2590 problem which arises on UnixWare. The UnixWare libc.so is an
2591 archive which includes an entry libc.so.1 which defines a bunch of
2592 symbols. The libc.so archive also includes a number of other
2593 object files, which also define symbols, some of which are the same
2594 as those defined in libc.so.1. Correct linking requires that we
2595 consider each object file in turn, and include it if it defines any
2596 symbols we need. _bfd_generic_link_add_archive_symbols does not do
2597 this; it looks through the list of undefined symbols, and includes
2598 any object file which defines them. When this algorithm is used on
2599 UnixWare, it winds up pulling in libc.so.1 early and defining a
2600 bunch of symbols. This means that some of the other objects in the
2601 archive are not included in the link, which is incorrect since they
2602 precede libc.so.1 in the archive.
2603
2604 Fortunately, ELF archive handling is simpler than that done by
2605 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
2606 oddities. In ELF, if we find a symbol in the archive map, and the
2607 symbol is currently undefined, we know that we must pull in that
2608 object file.
2609
2610 Unfortunately, we do have to make multiple passes over the symbol
2611 table until nothing further is resolved. */
2612
2613bfd_boolean
2614_bfd_elf_link_add_archive_symbols (bfd *abfd,
2615 struct bfd_link_info *info)
2616{
2617 symindex c;
2618 bfd_boolean *defined = NULL;
2619 bfd_boolean *included = NULL;
2620 carsym *symdefs;
2621 bfd_boolean loop;
2622 bfd_size_type amt;
2623
2624 if (! bfd_has_map (abfd))
2625 {
2626 /* An empty archive is a special case. */
2627 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
2628 return TRUE;
2629 bfd_set_error (bfd_error_no_armap);
2630 return FALSE;
2631 }
2632
2633 /* Keep track of all symbols we know to be already defined, and all
2634 files we know to be already included. This is to speed up the
2635 second and subsequent passes. */
2636 c = bfd_ardata (abfd)->symdef_count;
2637 if (c == 0)
2638 return TRUE;
2639 amt = c;
2640 amt *= sizeof (bfd_boolean);
2641 defined = bfd_zmalloc (amt);
2642 included = bfd_zmalloc (amt);
2643 if (defined == NULL || included == NULL)
2644 goto error_return;
2645
2646 symdefs = bfd_ardata (abfd)->symdefs;
2647
2648 do
2649 {
2650 file_ptr last;
2651 symindex i;
2652 carsym *symdef;
2653 carsym *symdefend;
2654
2655 loop = FALSE;
2656 last = -1;
2657
2658 symdef = symdefs;
2659 symdefend = symdef + c;
2660 for (i = 0; symdef < symdefend; symdef++, i++)
2661 {
2662 struct elf_link_hash_entry *h;
2663 bfd *element;
2664 struct bfd_link_hash_entry *undefs_tail;
2665 symindex mark;
2666
2667 if (defined[i] || included[i])
2668 continue;
2669 if (symdef->file_offset == last)
2670 {
2671 included[i] = TRUE;
2672 continue;
2673 }
2674
2675 h = elf_link_hash_lookup (elf_hash_table (info), symdef->name,
2676 FALSE, FALSE, FALSE);
2677
2678 if (h == NULL)
2679 {
2680 char *p, *copy;
2681 size_t len, first;
2682
2683 /* If this is a default version (the name contains @@),
2684 look up the symbol again with only one `@' as well
2685 as without the version. The effect is that references
2686 to the symbol with and without the version will be
2687 matched by the default symbol in the archive. */
2688
2689 p = strchr (symdef->name, ELF_VER_CHR);
2690 if (p == NULL || p[1] != ELF_VER_CHR)
2691 continue;
2692
2693 /* First check with only one `@'. */
2694 len = strlen (symdef->name);
2695 copy = bfd_alloc (abfd, len);
2696 if (copy == NULL)
2697 goto error_return;
2698 first = p - symdef->name + 1;
2699 memcpy (copy, symdef->name, first);
2700 memcpy (copy + first, symdef->name + first + 1, len - first);
2701
2702 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2703 FALSE, FALSE, FALSE);
2704
2705 if (h == NULL)
2706 {
2707 /* We also need to check references to the symbol
2708 without the version. */
2709
2710 copy[first - 1] = '\0';
2711 h = elf_link_hash_lookup (elf_hash_table (info),
2712 copy, FALSE, FALSE, FALSE);
2713 }
2714
2715 bfd_release (abfd, copy);
2716 }
2717
2718 if (h == NULL)
2719 continue;
2720
2721 if (h->root.type == bfd_link_hash_common)
2722 {
2723 /* We currently have a common symbol. The archive map contains
2724 a reference to this symbol, so we may want to include it. We
2725 only want to include it however, if this archive element
2726 contains a definition of the symbol, not just another common
2727 declaration of it.
2728
2729 Unfortunately some archivers (including GNU ar) will put
2730 declarations of common symbols into their archive maps, as
2731 well as real definitions, so we cannot just go by the archive
2732 map alone. Instead we must read in the element's symbol
2733 table and check that to see what kind of symbol definition
2734 this is. */
2735 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
2736 continue;
2737 }
2738 else if (h->root.type != bfd_link_hash_undefined)
2739 {
2740 if (h->root.type != bfd_link_hash_undefweak)
2741 defined[i] = TRUE;
2742 continue;
2743 }
2744
2745 /* We need to include this archive member. */
2746 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2747 if (element == NULL)
2748 goto error_return;
2749
2750 if (! bfd_check_format (element, bfd_object))
2751 goto error_return;
2752
2753 /* Doublecheck that we have not included this object
2754 already--it should be impossible, but there may be
2755 something wrong with the archive. */
2756 if (element->archive_pass != 0)
2757 {
2758 bfd_set_error (bfd_error_bad_value);
2759 goto error_return;
2760 }
2761 element->archive_pass = 1;
2762
2763 undefs_tail = info->hash->undefs_tail;
2764
2765 if (! (*info->callbacks->add_archive_element) (info, element,
2766 symdef->name))
2767 goto error_return;
2768 if (! bfd_link_add_symbols (element, info))
2769 goto error_return;
2770
2771 /* If there are any new undefined symbols, we need to make
2772 another pass through the archive in order to see whether
2773 they can be defined. FIXME: This isn't perfect, because
2774 common symbols wind up on undefs_tail and because an
2775 undefined symbol which is defined later on in this pass
2776 does not require another pass. This isn't a bug, but it
2777 does make the code less efficient than it could be. */
2778 if (undefs_tail != info->hash->undefs_tail)
2779 loop = TRUE;
2780
2781 /* Look backward to mark all symbols from this object file
2782 which we have already seen in this pass. */
2783 mark = i;
2784 do
2785 {
2786 included[mark] = TRUE;
2787 if (mark == 0)
2788 break;
2789 --mark;
2790 }
2791 while (symdefs[mark].file_offset == symdef->file_offset);
2792
2793 /* We mark subsequent symbols from this object file as we go
2794 on through the loop. */
2795 last = symdef->file_offset;
2796 }
2797 }
2798 while (loop);
2799
2800 free (defined);
2801 free (included);
2802
2803 return TRUE;
2804
2805 error_return:
2806 if (defined != NULL)
2807 free (defined);
2808 if (included != NULL)
2809 free (included);
2810 return FALSE;
2811}
This page took 0.356842 seconds and 4 git commands to generate.