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