2007-09-26 Pierre Muller <muller@ics.u-strasbg.fr>
[deliverable/binutils-gdb.git] / bfd / elflink.c
CommitLineData
252b5132 1/* ELF linking support for BFD.
64d03ab5 2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
8da3dbc5 3 2005, 2006, 2007 Free Software Foundation, Inc.
252b5132 4
8fdd7217 5 This file is part of BFD, the Binary File Descriptor library.
252b5132 6
8fdd7217
NC
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
cd123cb7 9 the Free Software Foundation; either version 3 of the License, or
8fdd7217 10 (at your option) any later version.
252b5132 11
8fdd7217
NC
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
252b5132 16
8fdd7217
NC
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
cd123cb7
NC
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
252b5132 21
252b5132 22#include "sysdep.h"
3db64b00 23#include "bfd.h"
252b5132
RH
24#include "bfdlink.h"
25#include "libbfd.h"
26#define ARCH_SIZE 0
27#include "elf-bfd.h"
4ad4eba5 28#include "safe-ctype.h"
ccf2f652 29#include "libiberty.h"
66eb6687 30#include "objalloc.h"
252b5132 31
d98685ac
AM
32/* Define a symbol in a dynamic linkage section. */
33
34struct elf_link_hash_entry *
35_bfd_elf_define_linkage_sym (bfd *abfd,
36 struct bfd_link_info *info,
37 asection *sec,
38 const char *name)
39{
40 struct elf_link_hash_entry *h;
41 struct bfd_link_hash_entry *bh;
ccabcbe5 42 const struct elf_backend_data *bed;
d98685ac
AM
43
44 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
45 if (h != NULL)
46 {
47 /* Zap symbol defined in an as-needed lib that wasn't linked.
48 This is a symptom of a larger problem: Absolute symbols
49 defined in shared libraries can't be overridden, because we
50 lose the link to the bfd which is via the symbol section. */
51 h->root.type = bfd_link_hash_new;
52 }
53
54 bh = &h->root;
55 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
56 sec, 0, NULL, FALSE,
57 get_elf_backend_data (abfd)->collect,
58 &bh))
59 return NULL;
60 h = (struct elf_link_hash_entry *) bh;
61 h->def_regular = 1;
62 h->type = STT_OBJECT;
63 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
64
ccabcbe5
AM
65 bed = get_elf_backend_data (abfd);
66 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
d98685ac
AM
67 return h;
68}
69
b34976b6 70bfd_boolean
268b6b39 71_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
72{
73 flagword flags;
aad5d350 74 asection *s;
252b5132 75 struct elf_link_hash_entry *h;
9c5bfbb7 76 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132
RH
77 int ptralign;
78
79 /* This function may be called more than once. */
aad5d350
AM
80 s = bfd_get_section_by_name (abfd, ".got");
81 if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
b34976b6 82 return TRUE;
252b5132
RH
83
84 switch (bed->s->arch_size)
85 {
bb0deeff
AO
86 case 32:
87 ptralign = 2;
88 break;
89
90 case 64:
91 ptralign = 3;
92 break;
93
94 default:
95 bfd_set_error (bfd_error_bad_value);
b34976b6 96 return FALSE;
252b5132
RH
97 }
98
e5a52504 99 flags = bed->dynamic_sec_flags;
252b5132 100
3496cb2a 101 s = bfd_make_section_with_flags (abfd, ".got", flags);
252b5132 102 if (s == NULL
252b5132 103 || !bfd_set_section_alignment (abfd, s, ptralign))
b34976b6 104 return FALSE;
252b5132
RH
105
106 if (bed->want_got_plt)
107 {
3496cb2a 108 s = bfd_make_section_with_flags (abfd, ".got.plt", flags);
252b5132 109 if (s == NULL
252b5132 110 || !bfd_set_section_alignment (abfd, s, ptralign))
b34976b6 111 return FALSE;
252b5132
RH
112 }
113
2517a57f
AM
114 if (bed->want_got_sym)
115 {
116 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
117 (or .got.plt) section. We don't do this in the linker script
118 because we don't want to define the symbol if we are not creating
119 a global offset table. */
d98685ac 120 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_");
2517a57f 121 elf_hash_table (info)->hgot = h;
d98685ac
AM
122 if (h == NULL)
123 return FALSE;
2517a57f 124 }
252b5132
RH
125
126 /* The first bit of the global offset table is the header. */
3b36f7e6 127 s->size += bed->got_header_size;
252b5132 128
b34976b6 129 return TRUE;
252b5132
RH
130}
131\f
7e9f0867
AM
132/* Create a strtab to hold the dynamic symbol names. */
133static bfd_boolean
134_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
135{
136 struct elf_link_hash_table *hash_table;
137
138 hash_table = elf_hash_table (info);
139 if (hash_table->dynobj == NULL)
140 hash_table->dynobj = abfd;
141
142 if (hash_table->dynstr == NULL)
143 {
144 hash_table->dynstr = _bfd_elf_strtab_init ();
145 if (hash_table->dynstr == NULL)
146 return FALSE;
147 }
148 return TRUE;
149}
150
45d6a902
AM
151/* Create some sections which will be filled in with dynamic linking
152 information. ABFD is an input file which requires dynamic sections
153 to be created. The dynamic sections take up virtual memory space
154 when the final executable is run, so we need to create them before
155 addresses are assigned to the output sections. We work out the
156 actual contents and size of these sections later. */
252b5132 157
b34976b6 158bfd_boolean
268b6b39 159_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
252b5132 160{
45d6a902
AM
161 flagword flags;
162 register asection *s;
9c5bfbb7 163 const struct elf_backend_data *bed;
252b5132 164
0eddce27 165 if (! is_elf_hash_table (info->hash))
45d6a902
AM
166 return FALSE;
167
168 if (elf_hash_table (info)->dynamic_sections_created)
169 return TRUE;
170
7e9f0867
AM
171 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
172 return FALSE;
45d6a902 173
7e9f0867 174 abfd = elf_hash_table (info)->dynobj;
e5a52504
MM
175 bed = get_elf_backend_data (abfd);
176
177 flags = bed->dynamic_sec_flags;
45d6a902
AM
178
179 /* A dynamically linked executable has a .interp section, but a
180 shared library does not. */
36af4a4e 181 if (info->executable)
252b5132 182 {
3496cb2a
L
183 s = bfd_make_section_with_flags (abfd, ".interp",
184 flags | SEC_READONLY);
185 if (s == NULL)
45d6a902
AM
186 return FALSE;
187 }
bb0deeff 188
45d6a902
AM
189 /* Create sections to hold version informations. These are removed
190 if they are not needed. */
3496cb2a
L
191 s = bfd_make_section_with_flags (abfd, ".gnu.version_d",
192 flags | SEC_READONLY);
45d6a902 193 if (s == NULL
45d6a902
AM
194 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
195 return FALSE;
196
3496cb2a
L
197 s = bfd_make_section_with_flags (abfd, ".gnu.version",
198 flags | SEC_READONLY);
45d6a902 199 if (s == NULL
45d6a902
AM
200 || ! bfd_set_section_alignment (abfd, s, 1))
201 return FALSE;
202
3496cb2a
L
203 s = bfd_make_section_with_flags (abfd, ".gnu.version_r",
204 flags | SEC_READONLY);
45d6a902 205 if (s == NULL
45d6a902
AM
206 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
207 return FALSE;
208
3496cb2a
L
209 s = bfd_make_section_with_flags (abfd, ".dynsym",
210 flags | SEC_READONLY);
45d6a902 211 if (s == NULL
45d6a902
AM
212 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
213 return FALSE;
214
3496cb2a
L
215 s = bfd_make_section_with_flags (abfd, ".dynstr",
216 flags | SEC_READONLY);
217 if (s == NULL)
45d6a902
AM
218 return FALSE;
219
3496cb2a 220 s = bfd_make_section_with_flags (abfd, ".dynamic", flags);
45d6a902 221 if (s == NULL
45d6a902
AM
222 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
223 return FALSE;
224
225 /* The special symbol _DYNAMIC is always set to the start of the
77cfaee6
AM
226 .dynamic section. We could set _DYNAMIC in a linker script, but we
227 only want to define it if we are, in fact, creating a .dynamic
228 section. We don't want to define it if there is no .dynamic
229 section, since on some ELF platforms the start up code examines it
230 to decide how to initialize the process. */
d98685ac 231 if (!_bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC"))
45d6a902
AM
232 return FALSE;
233
fdc90cb4
JJ
234 if (info->emit_hash)
235 {
236 s = bfd_make_section_with_flags (abfd, ".hash", flags | SEC_READONLY);
237 if (s == NULL
238 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
239 return FALSE;
240 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
241 }
242
243 if (info->emit_gnu_hash)
244 {
245 s = bfd_make_section_with_flags (abfd, ".gnu.hash",
246 flags | SEC_READONLY);
247 if (s == NULL
248 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
249 return FALSE;
250 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
251 4 32-bit words followed by variable count of 64-bit words, then
252 variable count of 32-bit words. */
253 if (bed->s->arch_size == 64)
254 elf_section_data (s)->this_hdr.sh_entsize = 0;
255 else
256 elf_section_data (s)->this_hdr.sh_entsize = 4;
257 }
45d6a902
AM
258
259 /* Let the backend create the rest of the sections. This lets the
260 backend set the right flags. The backend will normally create
261 the .got and .plt sections. */
262 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
263 return FALSE;
264
265 elf_hash_table (info)->dynamic_sections_created = TRUE;
266
267 return TRUE;
268}
269
270/* Create dynamic sections when linking against a dynamic object. */
271
272bfd_boolean
268b6b39 273_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
45d6a902
AM
274{
275 flagword flags, pltflags;
7325306f 276 struct elf_link_hash_entry *h;
45d6a902 277 asection *s;
9c5bfbb7 278 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
45d6a902 279
252b5132
RH
280 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
281 .rel[a].bss sections. */
e5a52504 282 flags = bed->dynamic_sec_flags;
252b5132
RH
283
284 pltflags = flags;
252b5132 285 if (bed->plt_not_loaded)
6df4d94c
MM
286 /* We do not clear SEC_ALLOC here because we still want the OS to
287 allocate space for the section; it's just that there's nothing
288 to read in from the object file. */
5d1634d7 289 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
6df4d94c
MM
290 else
291 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
252b5132
RH
292 if (bed->plt_readonly)
293 pltflags |= SEC_READONLY;
294
3496cb2a 295 s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
252b5132 296 if (s == NULL
252b5132 297 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
b34976b6 298 return FALSE;
252b5132 299
d98685ac
AM
300 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
301 .plt section. */
7325306f
RS
302 if (bed->want_plt_sym)
303 {
304 h = _bfd_elf_define_linkage_sym (abfd, info, s,
305 "_PROCEDURE_LINKAGE_TABLE_");
306 elf_hash_table (info)->hplt = h;
307 if (h == NULL)
308 return FALSE;
309 }
252b5132 310
3496cb2a
L
311 s = bfd_make_section_with_flags (abfd,
312 (bed->default_use_rela_p
313 ? ".rela.plt" : ".rel.plt"),
314 flags | SEC_READONLY);
252b5132 315 if (s == NULL
45d6a902 316 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 317 return FALSE;
252b5132
RH
318
319 if (! _bfd_elf_create_got_section (abfd, info))
b34976b6 320 return FALSE;
252b5132 321
3018b441
RH
322 if (bed->want_dynbss)
323 {
324 /* The .dynbss section is a place to put symbols which are defined
325 by dynamic objects, are referenced by regular objects, and are
326 not functions. We must allocate space for them in the process
327 image and use a R_*_COPY reloc to tell the dynamic linker to
328 initialize them at run time. The linker script puts the .dynbss
329 section into the .bss section of the final image. */
3496cb2a
L
330 s = bfd_make_section_with_flags (abfd, ".dynbss",
331 (SEC_ALLOC
332 | SEC_LINKER_CREATED));
333 if (s == NULL)
b34976b6 334 return FALSE;
252b5132 335
3018b441 336 /* The .rel[a].bss section holds copy relocs. This section is not
77cfaee6
AM
337 normally needed. We need to create it here, though, so that the
338 linker will map it to an output section. We can't just create it
339 only if we need it, because we will not know whether we need it
340 until we have seen all the input files, and the first time the
341 main linker code calls BFD after examining all the input files
342 (size_dynamic_sections) the input sections have already been
343 mapped to the output sections. If the section turns out not to
344 be needed, we can discard it later. We will never need this
345 section when generating a shared object, since they do not use
346 copy relocs. */
3018b441
RH
347 if (! info->shared)
348 {
3496cb2a
L
349 s = bfd_make_section_with_flags (abfd,
350 (bed->default_use_rela_p
351 ? ".rela.bss" : ".rel.bss"),
352 flags | SEC_READONLY);
3018b441 353 if (s == NULL
45d6a902 354 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
b34976b6 355 return FALSE;
3018b441 356 }
252b5132
RH
357 }
358
b34976b6 359 return TRUE;
252b5132
RH
360}
361\f
252b5132
RH
362/* Record a new dynamic symbol. We record the dynamic symbols as we
363 read the input files, since we need to have a list of all of them
364 before we can determine the final sizes of the output sections.
365 Note that we may actually call this function even though we are not
366 going to output any dynamic symbols; in some cases we know that a
367 symbol should be in the dynamic symbol table, but only if there is
368 one. */
369
b34976b6 370bfd_boolean
c152c796
AM
371bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
372 struct elf_link_hash_entry *h)
252b5132
RH
373{
374 if (h->dynindx == -1)
375 {
2b0f7ef9 376 struct elf_strtab_hash *dynstr;
68b6ddd0 377 char *p;
252b5132 378 const char *name;
252b5132
RH
379 bfd_size_type indx;
380
7a13edea
NC
381 /* XXX: The ABI draft says the linker must turn hidden and
382 internal symbols into STB_LOCAL symbols when producing the
383 DSO. However, if ld.so honors st_other in the dynamic table,
384 this would not be necessary. */
385 switch (ELF_ST_VISIBILITY (h->other))
386 {
387 case STV_INTERNAL:
388 case STV_HIDDEN:
9d6eee78
L
389 if (h->root.type != bfd_link_hash_undefined
390 && h->root.type != bfd_link_hash_undefweak)
38048eb9 391 {
f5385ebf 392 h->forced_local = 1;
67687978
PB
393 if (!elf_hash_table (info)->is_relocatable_executable)
394 return TRUE;
7a13edea 395 }
0444bdd4 396
7a13edea
NC
397 default:
398 break;
399 }
400
252b5132
RH
401 h->dynindx = elf_hash_table (info)->dynsymcount;
402 ++elf_hash_table (info)->dynsymcount;
403
404 dynstr = elf_hash_table (info)->dynstr;
405 if (dynstr == NULL)
406 {
407 /* Create a strtab to hold the dynamic symbol names. */
2b0f7ef9 408 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
252b5132 409 if (dynstr == NULL)
b34976b6 410 return FALSE;
252b5132
RH
411 }
412
413 /* We don't put any version information in the dynamic string
aad5d350 414 table. */
252b5132
RH
415 name = h->root.root.string;
416 p = strchr (name, ELF_VER_CHR);
68b6ddd0
AM
417 if (p != NULL)
418 /* We know that the p points into writable memory. In fact,
419 there are only a few symbols that have read-only names, being
420 those like _GLOBAL_OFFSET_TABLE_ that are created specially
421 by the backends. Most symbols will have names pointing into
422 an ELF string table read from a file, or to objalloc memory. */
423 *p = 0;
424
425 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
426
427 if (p != NULL)
428 *p = ELF_VER_CHR;
252b5132
RH
429
430 if (indx == (bfd_size_type) -1)
b34976b6 431 return FALSE;
252b5132
RH
432 h->dynstr_index = indx;
433 }
434
b34976b6 435 return TRUE;
252b5132 436}
45d6a902 437\f
55255dae
L
438/* Mark a symbol dynamic. */
439
440void
441bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
40b36307
L
442 struct elf_link_hash_entry *h,
443 Elf_Internal_Sym *sym)
55255dae 444{
40b36307 445 struct bfd_elf_dynamic_list *d = info->dynamic_list;
55255dae 446
40b36307
L
447 /* It may be called more than once on the same H. */
448 if(h->dynamic || info->relocatable)
55255dae
L
449 return;
450
40b36307
L
451 if ((info->dynamic_data
452 && (h->type == STT_OBJECT
453 || (sym != NULL
454 && ELF_ST_TYPE (sym->st_info) == STT_OBJECT)))
455 || (d != NULL
456 && h->root.type == bfd_link_hash_new
457 && (*d->match) (&d->head, NULL, h->root.root.string)))
55255dae
L
458 h->dynamic = 1;
459}
460
45d6a902
AM
461/* Record an assignment to a symbol made by a linker script. We need
462 this in case some dynamic object refers to this symbol. */
463
464bfd_boolean
fe21a8fc
L
465bfd_elf_record_link_assignment (bfd *output_bfd,
466 struct bfd_link_info *info,
268b6b39 467 const char *name,
fe21a8fc
L
468 bfd_boolean provide,
469 bfd_boolean hidden)
45d6a902 470{
00cbee0a 471 struct elf_link_hash_entry *h, *hv;
4ea42fb7 472 struct elf_link_hash_table *htab;
00cbee0a 473 const struct elf_backend_data *bed;
45d6a902 474
0eddce27 475 if (!is_elf_hash_table (info->hash))
45d6a902
AM
476 return TRUE;
477
4ea42fb7
AM
478 htab = elf_hash_table (info);
479 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
45d6a902 480 if (h == NULL)
4ea42fb7 481 return provide;
45d6a902 482
00cbee0a 483 switch (h->root.type)
77cfaee6 484 {
00cbee0a
L
485 case bfd_link_hash_defined:
486 case bfd_link_hash_defweak:
487 case bfd_link_hash_common:
488 break;
489 case bfd_link_hash_undefweak:
490 case bfd_link_hash_undefined:
491 /* Since we're defining the symbol, don't let it seem to have not
492 been defined. record_dynamic_symbol and size_dynamic_sections
493 may depend on this. */
4ea42fb7 494 h->root.type = bfd_link_hash_new;
77cfaee6
AM
495 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
496 bfd_link_repair_undef_list (&htab->root);
00cbee0a
L
497 break;
498 case bfd_link_hash_new:
40b36307 499 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
55255dae 500 h->non_elf = 0;
00cbee0a
L
501 break;
502 case bfd_link_hash_indirect:
503 /* We had a versioned symbol in a dynamic library. We make the
504 the versioned symbol point to this one. */
505 bed = get_elf_backend_data (output_bfd);
506 hv = h;
507 while (hv->root.type == bfd_link_hash_indirect
508 || hv->root.type == bfd_link_hash_warning)
509 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
510 /* We don't need to update h->root.u since linker will set them
511 later. */
512 h->root.type = bfd_link_hash_undefined;
513 hv->root.type = bfd_link_hash_indirect;
514 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
515 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
516 break;
517 case bfd_link_hash_warning:
518 abort ();
519 break;
55255dae 520 }
45d6a902
AM
521
522 /* If this symbol is being provided by the linker script, and it is
523 currently defined by a dynamic object, but not by a regular
524 object, then mark it as undefined so that the generic linker will
525 force the correct value. */
526 if (provide
f5385ebf
AM
527 && h->def_dynamic
528 && !h->def_regular)
45d6a902
AM
529 h->root.type = bfd_link_hash_undefined;
530
531 /* If this symbol is not being provided by the linker script, and it is
532 currently defined by a dynamic object, but not by a regular object,
533 then clear out any version information because the symbol will not be
534 associated with the dynamic object any more. */
535 if (!provide
f5385ebf
AM
536 && h->def_dynamic
537 && !h->def_regular)
45d6a902
AM
538 h->verinfo.verdef = NULL;
539
f5385ebf 540 h->def_regular = 1;
45d6a902 541
fe21a8fc
L
542 if (provide && hidden)
543 {
544 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
545
546 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
547 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
548 }
549
6fa3860b
PB
550 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
551 and executables. */
552 if (!info->relocatable
553 && h->dynindx != -1
554 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
555 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
556 h->forced_local = 1;
557
f5385ebf
AM
558 if ((h->def_dynamic
559 || h->ref_dynamic
67687978
PB
560 || info->shared
561 || (info->executable && elf_hash_table (info)->is_relocatable_executable))
45d6a902
AM
562 && h->dynindx == -1)
563 {
c152c796 564 if (! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
565 return FALSE;
566
567 /* If this is a weak defined symbol, and we know a corresponding
568 real symbol from the same dynamic object, make sure the real
569 symbol is also made into a dynamic symbol. */
f6e332e6
AM
570 if (h->u.weakdef != NULL
571 && h->u.weakdef->dynindx == -1)
45d6a902 572 {
f6e332e6 573 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
45d6a902
AM
574 return FALSE;
575 }
576 }
577
578 return TRUE;
579}
42751cf3 580
8c58d23b
AM
581/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
582 success, and 2 on a failure caused by attempting to record a symbol
583 in a discarded section, eg. a discarded link-once section symbol. */
584
585int
c152c796
AM
586bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
587 bfd *input_bfd,
588 long input_indx)
8c58d23b
AM
589{
590 bfd_size_type amt;
591 struct elf_link_local_dynamic_entry *entry;
592 struct elf_link_hash_table *eht;
593 struct elf_strtab_hash *dynstr;
594 unsigned long dynstr_index;
595 char *name;
596 Elf_External_Sym_Shndx eshndx;
597 char esym[sizeof (Elf64_External_Sym)];
598
0eddce27 599 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
600 return 0;
601
602 /* See if the entry exists already. */
603 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
604 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
605 return 1;
606
607 amt = sizeof (*entry);
268b6b39 608 entry = bfd_alloc (input_bfd, amt);
8c58d23b
AM
609 if (entry == NULL)
610 return 0;
611
612 /* Go find the symbol, so that we can find it's name. */
613 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 614 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
615 {
616 bfd_release (input_bfd, entry);
617 return 0;
618 }
619
620 if (entry->isym.st_shndx != SHN_UNDEF
621 && (entry->isym.st_shndx < SHN_LORESERVE
622 || entry->isym.st_shndx > SHN_HIRESERVE))
623 {
624 asection *s;
625
626 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
627 if (s == NULL || bfd_is_abs_section (s->output_section))
628 {
629 /* We can still bfd_release here as nothing has done another
630 bfd_alloc. We can't do this later in this function. */
631 bfd_release (input_bfd, entry);
632 return 2;
633 }
634 }
635
636 name = (bfd_elf_string_from_elf_section
637 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
638 entry->isym.st_name));
639
640 dynstr = elf_hash_table (info)->dynstr;
641 if (dynstr == NULL)
642 {
643 /* Create a strtab to hold the dynamic symbol names. */
644 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
645 if (dynstr == NULL)
646 return 0;
647 }
648
b34976b6 649 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
8c58d23b
AM
650 if (dynstr_index == (unsigned long) -1)
651 return 0;
652 entry->isym.st_name = dynstr_index;
653
654 eht = elf_hash_table (info);
655
656 entry->next = eht->dynlocal;
657 eht->dynlocal = entry;
658 entry->input_bfd = input_bfd;
659 entry->input_indx = input_indx;
660 eht->dynsymcount++;
661
662 /* Whatever binding the symbol had before, it's now local. */
663 entry->isym.st_info
664 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
665
666 /* The dynindx will be set at the end of size_dynamic_sections. */
667
668 return 1;
669}
670
30b30c21 671/* Return the dynindex of a local dynamic symbol. */
42751cf3 672
30b30c21 673long
268b6b39
AM
674_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
675 bfd *input_bfd,
676 long input_indx)
30b30c21
RH
677{
678 struct elf_link_local_dynamic_entry *e;
679
680 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
681 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
682 return e->dynindx;
683 return -1;
684}
685
686/* This function is used to renumber the dynamic symbols, if some of
687 them are removed because they are marked as local. This is called
688 via elf_link_hash_traverse. */
689
b34976b6 690static bfd_boolean
268b6b39
AM
691elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
692 void *data)
42751cf3 693{
268b6b39 694 size_t *count = data;
30b30c21 695
e92d460e
AM
696 if (h->root.type == bfd_link_hash_warning)
697 h = (struct elf_link_hash_entry *) h->root.u.i.link;
698
6fa3860b
PB
699 if (h->forced_local)
700 return TRUE;
701
702 if (h->dynindx != -1)
703 h->dynindx = ++(*count);
704
705 return TRUE;
706}
707
708
709/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
710 STB_LOCAL binding. */
711
712static bfd_boolean
713elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
714 void *data)
715{
716 size_t *count = data;
717
718 if (h->root.type == bfd_link_hash_warning)
719 h = (struct elf_link_hash_entry *) h->root.u.i.link;
720
721 if (!h->forced_local)
722 return TRUE;
723
42751cf3 724 if (h->dynindx != -1)
30b30c21
RH
725 h->dynindx = ++(*count);
726
b34976b6 727 return TRUE;
42751cf3 728}
30b30c21 729
aee6f5b4
AO
730/* Return true if the dynamic symbol for a given section should be
731 omitted when creating a shared library. */
732bfd_boolean
733_bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
734 struct bfd_link_info *info,
735 asection *p)
736{
74541ad4
AM
737 struct elf_link_hash_table *htab;
738
aee6f5b4
AO
739 switch (elf_section_data (p)->this_hdr.sh_type)
740 {
741 case SHT_PROGBITS:
742 case SHT_NOBITS:
743 /* If sh_type is yet undecided, assume it could be
744 SHT_PROGBITS/SHT_NOBITS. */
745 case SHT_NULL:
74541ad4
AM
746 htab = elf_hash_table (info);
747 if (p == htab->tls_sec)
748 return FALSE;
749
750 if (htab->text_index_section != NULL)
751 return p != htab->text_index_section && p != htab->data_index_section;
752
aee6f5b4
AO
753 if (strcmp (p->name, ".got") == 0
754 || strcmp (p->name, ".got.plt") == 0
755 || strcmp (p->name, ".plt") == 0)
756 {
757 asection *ip;
aee6f5b4 758
74541ad4
AM
759 if (htab->dynobj != NULL
760 && (ip = bfd_get_section_by_name (htab->dynobj, p->name)) != NULL
aee6f5b4
AO
761 && (ip->flags & SEC_LINKER_CREATED)
762 && ip->output_section == p)
763 return TRUE;
764 }
765 return FALSE;
766
767 /* There shouldn't be section relative relocations
768 against any other section. */
769 default:
770 return TRUE;
771 }
772}
773
062e2358 774/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
775 symbol for each output section, which come first. Next come symbols
776 which have been forced to local binding. Then all of the back-end
777 allocated local dynamic syms, followed by the rest of the global
778 symbols. */
30b30c21 779
554220db
AM
780static unsigned long
781_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
782 struct bfd_link_info *info,
783 unsigned long *section_sym_count)
30b30c21
RH
784{
785 unsigned long dynsymcount = 0;
786
67687978 787 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
30b30c21 788 {
aee6f5b4 789 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
790 asection *p;
791 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 792 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4
AO
793 && (p->flags & SEC_ALLOC) != 0
794 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
795 elf_section_data (p)->dynindx = ++dynsymcount;
74541ad4
AM
796 else
797 elf_section_data (p)->dynindx = 0;
30b30c21 798 }
554220db 799 *section_sym_count = dynsymcount;
30b30c21 800
6fa3860b
PB
801 elf_link_hash_traverse (elf_hash_table (info),
802 elf_link_renumber_local_hash_table_dynsyms,
803 &dynsymcount);
804
30b30c21
RH
805 if (elf_hash_table (info)->dynlocal)
806 {
807 struct elf_link_local_dynamic_entry *p;
808 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
809 p->dynindx = ++dynsymcount;
810 }
811
812 elf_link_hash_traverse (elf_hash_table (info),
813 elf_link_renumber_hash_table_dynsyms,
814 &dynsymcount);
815
816 /* There is an unused NULL entry at the head of the table which
817 we must account for in our count. Unless there weren't any
818 symbols, which means we'll have no table at all. */
819 if (dynsymcount != 0)
820 ++dynsymcount;
821
ccabcbe5
AM
822 elf_hash_table (info)->dynsymcount = dynsymcount;
823 return dynsymcount;
30b30c21 824}
252b5132 825
45d6a902
AM
826/* This function is called when we want to define a new symbol. It
827 handles the various cases which arise when we find a definition in
828 a dynamic object, or when there is already a definition in a
829 dynamic object. The new symbol is described by NAME, SYM, PSEC,
830 and PVALUE. We set SYM_HASH to the hash table entry. We set
831 OVERRIDE if the old symbol is overriding a new definition. We set
832 TYPE_CHANGE_OK if it is OK for the type to change. We set
833 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
834 change, we mean that we shouldn't warn if the type or size does
af44c138
L
835 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic
836 object is overridden by a regular object. */
45d6a902
AM
837
838bfd_boolean
268b6b39
AM
839_bfd_elf_merge_symbol (bfd *abfd,
840 struct bfd_link_info *info,
841 const char *name,
842 Elf_Internal_Sym *sym,
843 asection **psec,
844 bfd_vma *pvalue,
af44c138 845 unsigned int *pold_alignment,
268b6b39
AM
846 struct elf_link_hash_entry **sym_hash,
847 bfd_boolean *skip,
848 bfd_boolean *override,
849 bfd_boolean *type_change_ok,
0f8a2703 850 bfd_boolean *size_change_ok)
252b5132 851{
7479dfd4 852 asection *sec, *oldsec;
45d6a902
AM
853 struct elf_link_hash_entry *h;
854 struct elf_link_hash_entry *flip;
855 int bind;
856 bfd *oldbfd;
857 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
77cfaee6 858 bfd_boolean newweak, oldweak;
a4d8e49b 859 const struct elf_backend_data *bed;
45d6a902
AM
860
861 *skip = FALSE;
862 *override = FALSE;
863
864 sec = *psec;
865 bind = ELF_ST_BIND (sym->st_info);
866
cd7be95b
KH
867 /* Silently discard TLS symbols from --just-syms. There's no way to
868 combine a static TLS block with a new TLS block for this executable. */
869 if (ELF_ST_TYPE (sym->st_info) == STT_TLS
870 && sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
871 {
872 *skip = TRUE;
873 return TRUE;
874 }
875
45d6a902
AM
876 if (! bfd_is_und_section (sec))
877 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
878 else
879 h = ((struct elf_link_hash_entry *)
880 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
881 if (h == NULL)
882 return FALSE;
883 *sym_hash = h;
252b5132 884
45d6a902
AM
885 /* This code is for coping with dynamic objects, and is only useful
886 if we are doing an ELF link. */
887 if (info->hash->creator != abfd->xvec)
888 return TRUE;
252b5132 889
45d6a902
AM
890 /* For merging, we only care about real symbols. */
891
892 while (h->root.type == bfd_link_hash_indirect
893 || h->root.type == bfd_link_hash_warning)
894 h = (struct elf_link_hash_entry *) h->root.u.i.link;
895
40b36307
L
896 /* We have to check it for every instance since the first few may be
897 refereences and not all compilers emit symbol type for undefined
898 symbols. */
899 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
900
45d6a902
AM
901 /* If we just created the symbol, mark it as being an ELF symbol.
902 Other than that, there is nothing to do--there is no merge issue
903 with a newly defined symbol--so we just return. */
904
905 if (h->root.type == bfd_link_hash_new)
252b5132 906 {
f5385ebf 907 h->non_elf = 0;
45d6a902
AM
908 return TRUE;
909 }
252b5132 910
7479dfd4
L
911 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
912 existing symbol. */
252b5132 913
45d6a902
AM
914 switch (h->root.type)
915 {
916 default:
917 oldbfd = NULL;
7479dfd4 918 oldsec = NULL;
45d6a902 919 break;
252b5132 920
45d6a902
AM
921 case bfd_link_hash_undefined:
922 case bfd_link_hash_undefweak:
923 oldbfd = h->root.u.undef.abfd;
7479dfd4 924 oldsec = NULL;
45d6a902
AM
925 break;
926
927 case bfd_link_hash_defined:
928 case bfd_link_hash_defweak:
929 oldbfd = h->root.u.def.section->owner;
7479dfd4 930 oldsec = h->root.u.def.section;
45d6a902
AM
931 break;
932
933 case bfd_link_hash_common:
934 oldbfd = h->root.u.c.p->section->owner;
7479dfd4 935 oldsec = h->root.u.c.p->section;
45d6a902
AM
936 break;
937 }
938
939 /* In cases involving weak versioned symbols, we may wind up trying
940 to merge a symbol with itself. Catch that here, to avoid the
941 confusion that results if we try to override a symbol with
942 itself. The additional tests catch cases like
943 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
944 dynamic object, which we do want to handle here. */
945 if (abfd == oldbfd
946 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 947 || !h->def_regular))
45d6a902
AM
948 return TRUE;
949
950 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
951 respectively, is from a dynamic object. */
952
707bba77 953 newdyn = (abfd->flags & DYNAMIC) != 0;
45d6a902 954
707bba77 955 olddyn = FALSE;
45d6a902
AM
956 if (oldbfd != NULL)
957 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 958 else if (oldsec != NULL)
45d6a902 959 {
707bba77 960 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 961 indices used by MIPS ELF. */
707bba77 962 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 963 }
252b5132 964
45d6a902
AM
965 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
966 respectively, appear to be a definition rather than reference. */
967
707bba77 968 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 969
707bba77
AM
970 olddef = (h->root.type != bfd_link_hash_undefined
971 && h->root.type != bfd_link_hash_undefweak
972 && h->root.type != bfd_link_hash_common);
45d6a902 973
fcb93ecf 974 bed = get_elf_backend_data (abfd);
580a2b6e
L
975 /* When we try to create a default indirect symbol from the dynamic
976 definition with the default version, we skip it if its type and
977 the type of existing regular definition mismatch. We only do it
978 if the existing regular definition won't be dynamic. */
979 if (pold_alignment == NULL
980 && !info->shared
981 && !info->export_dynamic
982 && !h->ref_dynamic
983 && newdyn
984 && newdef
985 && !olddyn
986 && (olddef || h->root.type == bfd_link_hash_common)
987 && ELF_ST_TYPE (sym->st_info) != h->type
988 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
fcb93ecf
PB
989 && h->type != STT_NOTYPE
990 && !(bed->is_function_type (ELF_ST_TYPE (sym->st_info))
991 && bed->is_function_type (h->type)))
580a2b6e
L
992 {
993 *skip = TRUE;
994 return TRUE;
995 }
996
68f49ba3
L
997 /* Check TLS symbol. We don't check undefined symbol introduced by
998 "ld -u". */
7479dfd4 999 if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)
68f49ba3
L
1000 && ELF_ST_TYPE (sym->st_info) != h->type
1001 && oldbfd != NULL)
7479dfd4
L
1002 {
1003 bfd *ntbfd, *tbfd;
1004 bfd_boolean ntdef, tdef;
1005 asection *ntsec, *tsec;
1006
1007 if (h->type == STT_TLS)
1008 {
3b36f7e6 1009 ntbfd = abfd;
7479dfd4
L
1010 ntsec = sec;
1011 ntdef = newdef;
1012 tbfd = oldbfd;
1013 tsec = oldsec;
1014 tdef = olddef;
1015 }
1016 else
1017 {
1018 ntbfd = oldbfd;
1019 ntsec = oldsec;
1020 ntdef = olddef;
1021 tbfd = abfd;
1022 tsec = sec;
1023 tdef = newdef;
1024 }
1025
1026 if (tdef && ntdef)
1027 (*_bfd_error_handler)
1028 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
1029 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1030 else if (!tdef && !ntdef)
1031 (*_bfd_error_handler)
1032 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
1033 tbfd, ntbfd, h->root.root.string);
1034 else if (tdef)
1035 (*_bfd_error_handler)
1036 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
1037 tbfd, tsec, ntbfd, h->root.root.string);
1038 else
1039 (*_bfd_error_handler)
1040 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
1041 tbfd, ntbfd, ntsec, h->root.root.string);
1042
1043 bfd_set_error (bfd_error_bad_value);
1044 return FALSE;
1045 }
1046
4cc11e76 1047 /* We need to remember if a symbol has a definition in a dynamic
45d6a902
AM
1048 object or is weak in all dynamic objects. Internal and hidden
1049 visibility will make it unavailable to dynamic objects. */
f5385ebf 1050 if (newdyn && !h->dynamic_def)
45d6a902
AM
1051 {
1052 if (!bfd_is_und_section (sec))
f5385ebf 1053 h->dynamic_def = 1;
45d6a902 1054 else
252b5132 1055 {
45d6a902
AM
1056 /* Check if this symbol is weak in all dynamic objects. If it
1057 is the first time we see it in a dynamic object, we mark
1058 if it is weak. Otherwise, we clear it. */
f5385ebf 1059 if (!h->ref_dynamic)
79349b09 1060 {
45d6a902 1061 if (bind == STB_WEAK)
f5385ebf 1062 h->dynamic_weak = 1;
252b5132 1063 }
45d6a902 1064 else if (bind != STB_WEAK)
f5385ebf 1065 h->dynamic_weak = 0;
252b5132 1066 }
45d6a902 1067 }
252b5132 1068
45d6a902
AM
1069 /* If the old symbol has non-default visibility, we ignore the new
1070 definition from a dynamic object. */
1071 if (newdyn
9c7a29a3 1072 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1073 && !bfd_is_und_section (sec))
1074 {
1075 *skip = TRUE;
1076 /* Make sure this symbol is dynamic. */
f5385ebf 1077 h->ref_dynamic = 1;
45d6a902
AM
1078 /* A protected symbol has external availability. Make sure it is
1079 recorded as dynamic.
1080
1081 FIXME: Should we check type and size for protected symbol? */
1082 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1083 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1084 else
1085 return TRUE;
1086 }
1087 else if (!newdyn
9c7a29a3 1088 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1089 && h->def_dynamic)
45d6a902
AM
1090 {
1091 /* If the new symbol with non-default visibility comes from a
1092 relocatable file and the old definition comes from a dynamic
1093 object, we remove the old definition. */
1094 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1095 {
1096 /* Handle the case where the old dynamic definition is
1097 default versioned. We need to copy the symbol info from
1098 the symbol with default version to the normal one if it
1099 was referenced before. */
1100 if (h->ref_regular)
1101 {
1102 const struct elf_backend_data *bed
1103 = get_elf_backend_data (abfd);
1104 struct elf_link_hash_entry *vh = *sym_hash;
1105 vh->root.type = h->root.type;
1106 h->root.type = bfd_link_hash_indirect;
1107 (*bed->elf_backend_copy_indirect_symbol) (info, vh, h);
1108 /* Protected symbols will override the dynamic definition
1109 with default version. */
1110 if (ELF_ST_VISIBILITY (sym->st_other) == STV_PROTECTED)
1111 {
1112 h->root.u.i.link = (struct bfd_link_hash_entry *) vh;
1113 vh->dynamic_def = 1;
1114 vh->ref_dynamic = 1;
1115 }
1116 else
1117 {
1118 h->root.type = vh->root.type;
1119 vh->ref_dynamic = 0;
1120 /* We have to hide it here since it was made dynamic
1121 global with extra bits when the symbol info was
1122 copied from the old dynamic definition. */
1123 (*bed->elf_backend_hide_symbol) (info, vh, TRUE);
1124 }
1125 h = vh;
1126 }
1127 else
1128 h = *sym_hash;
1129 }
1de1a317 1130
f6e332e6 1131 if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317
L
1132 && bfd_is_und_section (sec))
1133 {
1134 /* If the new symbol is undefined and the old symbol was
1135 also undefined before, we need to make sure
1136 _bfd_generic_link_add_one_symbol doesn't mess
f6e332e6 1137 up the linker hash table undefs list. Since the old
1de1a317
L
1138 definition came from a dynamic object, it is still on the
1139 undefs list. */
1140 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1141 h->root.u.undef.abfd = abfd;
1142 }
1143 else
1144 {
1145 h->root.type = bfd_link_hash_new;
1146 h->root.u.undef.abfd = NULL;
1147 }
1148
f5385ebf 1149 if (h->def_dynamic)
252b5132 1150 {
f5385ebf
AM
1151 h->def_dynamic = 0;
1152 h->ref_dynamic = 1;
1153 h->dynamic_def = 1;
45d6a902
AM
1154 }
1155 /* FIXME: Should we check type and size for protected symbol? */
1156 h->size = 0;
1157 h->type = 0;
1158 return TRUE;
1159 }
14a793b2 1160
79349b09
AM
1161 /* Differentiate strong and weak symbols. */
1162 newweak = bind == STB_WEAK;
1163 oldweak = (h->root.type == bfd_link_hash_defweak
1164 || h->root.type == bfd_link_hash_undefweak);
14a793b2 1165
15b43f48
AM
1166 /* If a new weak symbol definition comes from a regular file and the
1167 old symbol comes from a dynamic library, we treat the new one as
1168 strong. Similarly, an old weak symbol definition from a regular
1169 file is treated as strong when the new symbol comes from a dynamic
1170 library. Further, an old weak symbol from a dynamic library is
1171 treated as strong if the new symbol is from a dynamic library.
1172 This reflects the way glibc's ld.so works.
1173
1174 Do this before setting *type_change_ok or *size_change_ok so that
1175 we warn properly when dynamic library symbols are overridden. */
1176
1177 if (newdef && !newdyn && olddyn)
0f8a2703 1178 newweak = FALSE;
15b43f48 1179 if (olddef && newdyn)
0f8a2703
AM
1180 oldweak = FALSE;
1181
fcb93ecf
PB
1182 /* Allow changes between different types of funciton symbol. */
1183 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info))
1184 && bed->is_function_type (h->type))
1185 *type_change_ok = TRUE;
1186
79349b09
AM
1187 /* It's OK to change the type if either the existing symbol or the
1188 new symbol is weak. A type change is also OK if the old symbol
1189 is undefined and the new symbol is defined. */
252b5132 1190
79349b09
AM
1191 if (oldweak
1192 || newweak
1193 || (newdef
1194 && h->root.type == bfd_link_hash_undefined))
1195 *type_change_ok = TRUE;
1196
1197 /* It's OK to change the size if either the existing symbol or the
1198 new symbol is weak, or if the old symbol is undefined. */
1199
1200 if (*type_change_ok
1201 || h->root.type == bfd_link_hash_undefined)
1202 *size_change_ok = TRUE;
45d6a902 1203
45d6a902
AM
1204 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1205 symbol, respectively, appears to be a common symbol in a dynamic
1206 object. If a symbol appears in an uninitialized section, and is
1207 not weak, and is not a function, then it may be a common symbol
1208 which was resolved when the dynamic object was created. We want
1209 to treat such symbols specially, because they raise special
1210 considerations when setting the symbol size: if the symbol
1211 appears as a common symbol in a regular object, and the size in
1212 the regular object is larger, we must make sure that we use the
1213 larger size. This problematic case can always be avoided in C,
1214 but it must be handled correctly when using Fortran shared
1215 libraries.
1216
1217 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1218 likewise for OLDDYNCOMMON and OLDDEF.
1219
1220 Note that this test is just a heuristic, and that it is quite
1221 possible to have an uninitialized symbol in a shared object which
1222 is really a definition, rather than a common symbol. This could
1223 lead to some minor confusion when the symbol really is a common
1224 symbol in some regular object. However, I think it will be
1225 harmless. */
1226
1227 if (newdyn
1228 && newdef
79349b09 1229 && !newweak
45d6a902
AM
1230 && (sec->flags & SEC_ALLOC) != 0
1231 && (sec->flags & SEC_LOAD) == 0
1232 && sym->st_size > 0
fcb93ecf 1233 && !bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
45d6a902
AM
1234 newdyncommon = TRUE;
1235 else
1236 newdyncommon = FALSE;
1237
1238 if (olddyn
1239 && olddef
1240 && h->root.type == bfd_link_hash_defined
f5385ebf 1241 && h->def_dynamic
45d6a902
AM
1242 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1243 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1244 && h->size > 0
fcb93ecf 1245 && !bed->is_function_type (h->type))
45d6a902
AM
1246 olddyncommon = TRUE;
1247 else
1248 olddyncommon = FALSE;
1249
a4d8e49b
L
1250 /* We now know everything about the old and new symbols. We ask the
1251 backend to check if we can merge them. */
a4d8e49b
L
1252 if (bed->merge_symbol
1253 && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue,
1254 pold_alignment, skip, override,
1255 type_change_ok, size_change_ok,
1256 &newdyn, &newdef, &newdyncommon, &newweak,
1257 abfd, &sec,
1258 &olddyn, &olddef, &olddyncommon, &oldweak,
1259 oldbfd, &oldsec))
1260 return FALSE;
1261
45d6a902
AM
1262 /* If both the old and the new symbols look like common symbols in a
1263 dynamic object, set the size of the symbol to the larger of the
1264 two. */
1265
1266 if (olddyncommon
1267 && newdyncommon
1268 && sym->st_size != h->size)
1269 {
1270 /* Since we think we have two common symbols, issue a multiple
1271 common warning if desired. Note that we only warn if the
1272 size is different. If the size is the same, we simply let
1273 the old symbol override the new one as normally happens with
1274 symbols defined in dynamic objects. */
1275
1276 if (! ((*info->callbacks->multiple_common)
1277 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1278 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1279 return FALSE;
252b5132 1280
45d6a902
AM
1281 if (sym->st_size > h->size)
1282 h->size = sym->st_size;
252b5132 1283
45d6a902 1284 *size_change_ok = TRUE;
252b5132
RH
1285 }
1286
45d6a902
AM
1287 /* If we are looking at a dynamic object, and we have found a
1288 definition, we need to see if the symbol was already defined by
1289 some other object. If so, we want to use the existing
1290 definition, and we do not want to report a multiple symbol
1291 definition error; we do this by clobbering *PSEC to be
1292 bfd_und_section_ptr.
1293
1294 We treat a common symbol as a definition if the symbol in the
1295 shared library is a function, since common symbols always
1296 represent variables; this can cause confusion in principle, but
1297 any such confusion would seem to indicate an erroneous program or
1298 shared library. We also permit a common symbol in a regular
79349b09 1299 object to override a weak symbol in a shared object. */
45d6a902
AM
1300
1301 if (newdyn
1302 && newdef
77cfaee6 1303 && (olddef
45d6a902 1304 || (h->root.type == bfd_link_hash_common
79349b09 1305 && (newweak
fcb93ecf 1306 || bed->is_function_type (ELF_ST_TYPE (sym->st_info))))))
45d6a902
AM
1307 {
1308 *override = TRUE;
1309 newdef = FALSE;
1310 newdyncommon = FALSE;
252b5132 1311
45d6a902
AM
1312 *psec = sec = bfd_und_section_ptr;
1313 *size_change_ok = TRUE;
252b5132 1314
45d6a902
AM
1315 /* If we get here when the old symbol is a common symbol, then
1316 we are explicitly letting it override a weak symbol or
1317 function in a dynamic object, and we don't want to warn about
1318 a type change. If the old symbol is a defined symbol, a type
1319 change warning may still be appropriate. */
252b5132 1320
45d6a902
AM
1321 if (h->root.type == bfd_link_hash_common)
1322 *type_change_ok = TRUE;
1323 }
1324
1325 /* Handle the special case of an old common symbol merging with a
1326 new symbol which looks like a common symbol in a shared object.
1327 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1328 common symbol, and let _bfd_generic_link_add_one_symbol do the
1329 right thing. */
45d6a902
AM
1330
1331 if (newdyncommon
1332 && h->root.type == bfd_link_hash_common)
1333 {
1334 *override = TRUE;
1335 newdef = FALSE;
1336 newdyncommon = FALSE;
1337 *pvalue = sym->st_size;
a4d8e49b 1338 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1339 *size_change_ok = TRUE;
1340 }
1341
c5e2cead 1342 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1343 if (newdef && olddef && newweak)
c5e2cead
L
1344 *skip = TRUE;
1345
45d6a902
AM
1346 /* If the old symbol is from a dynamic object, and the new symbol is
1347 a definition which is not from a dynamic object, then the new
1348 symbol overrides the old symbol. Symbols from regular files
1349 always take precedence over symbols from dynamic objects, even if
1350 they are defined after the dynamic object in the link.
1351
1352 As above, we again permit a common symbol in a regular object to
1353 override a definition in a shared object if the shared object
0f8a2703 1354 symbol is a function or is weak. */
45d6a902
AM
1355
1356 flip = NULL;
77cfaee6 1357 if (!newdyn
45d6a902
AM
1358 && (newdef
1359 || (bfd_is_com_section (sec)
79349b09 1360 && (oldweak
fcb93ecf 1361 || bed->is_function_type (h->type))))
45d6a902
AM
1362 && olddyn
1363 && olddef
f5385ebf 1364 && h->def_dynamic)
45d6a902
AM
1365 {
1366 /* Change the hash table entry to undefined, and let
1367 _bfd_generic_link_add_one_symbol do the right thing with the
1368 new definition. */
1369
1370 h->root.type = bfd_link_hash_undefined;
1371 h->root.u.undef.abfd = h->root.u.def.section->owner;
1372 *size_change_ok = TRUE;
1373
1374 olddef = FALSE;
1375 olddyncommon = FALSE;
1376
1377 /* We again permit a type change when a common symbol may be
1378 overriding a function. */
1379
1380 if (bfd_is_com_section (sec))
1381 *type_change_ok = TRUE;
1382
1383 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1384 flip = *sym_hash;
1385 else
1386 /* This union may have been set to be non-NULL when this symbol
1387 was seen in a dynamic object. We must force the union to be
1388 NULL, so that it is correct for a regular symbol. */
1389 h->verinfo.vertree = NULL;
1390 }
1391
1392 /* Handle the special case of a new common symbol merging with an
1393 old symbol that looks like it might be a common symbol defined in
1394 a shared object. Note that we have already handled the case in
1395 which a new common symbol should simply override the definition
1396 in the shared library. */
1397
1398 if (! newdyn
1399 && bfd_is_com_section (sec)
1400 && olddyncommon)
1401 {
1402 /* It would be best if we could set the hash table entry to a
1403 common symbol, but we don't know what to use for the section
1404 or the alignment. */
1405 if (! ((*info->callbacks->multiple_common)
1406 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1407 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1408 return FALSE;
1409
4cc11e76 1410 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1411 larger, pretend that the new symbol has its size. */
1412
1413 if (h->size > *pvalue)
1414 *pvalue = h->size;
1415
af44c138
L
1416 /* We need to remember the alignment required by the symbol
1417 in the dynamic object. */
1418 BFD_ASSERT (pold_alignment);
1419 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1420
1421 olddef = FALSE;
1422 olddyncommon = FALSE;
1423
1424 h->root.type = bfd_link_hash_undefined;
1425 h->root.u.undef.abfd = h->root.u.def.section->owner;
1426
1427 *size_change_ok = TRUE;
1428 *type_change_ok = TRUE;
1429
1430 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1431 flip = *sym_hash;
1432 else
1433 h->verinfo.vertree = NULL;
1434 }
1435
1436 if (flip != NULL)
1437 {
1438 /* Handle the case where we had a versioned symbol in a dynamic
1439 library and now find a definition in a normal object. In this
1440 case, we make the versioned symbol point to the normal one. */
9c5bfbb7 1441 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
45d6a902 1442 flip->root.type = h->root.type;
00cbee0a 1443 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1444 h->root.type = bfd_link_hash_indirect;
1445 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1446 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1447 if (h->def_dynamic)
45d6a902 1448 {
f5385ebf
AM
1449 h->def_dynamic = 0;
1450 flip->ref_dynamic = 1;
45d6a902
AM
1451 }
1452 }
1453
45d6a902
AM
1454 return TRUE;
1455}
1456
1457/* This function is called to create an indirect symbol from the
1458 default for the symbol with the default version if needed. The
1459 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
0f8a2703 1460 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902
AM
1461
1462bfd_boolean
268b6b39
AM
1463_bfd_elf_add_default_symbol (bfd *abfd,
1464 struct bfd_link_info *info,
1465 struct elf_link_hash_entry *h,
1466 const char *name,
1467 Elf_Internal_Sym *sym,
1468 asection **psec,
1469 bfd_vma *value,
1470 bfd_boolean *dynsym,
0f8a2703 1471 bfd_boolean override)
45d6a902
AM
1472{
1473 bfd_boolean type_change_ok;
1474 bfd_boolean size_change_ok;
1475 bfd_boolean skip;
1476 char *shortname;
1477 struct elf_link_hash_entry *hi;
1478 struct bfd_link_hash_entry *bh;
9c5bfbb7 1479 const struct elf_backend_data *bed;
45d6a902
AM
1480 bfd_boolean collect;
1481 bfd_boolean dynamic;
1482 char *p;
1483 size_t len, shortlen;
1484 asection *sec;
1485
1486 /* If this symbol has a version, and it is the default version, we
1487 create an indirect symbol from the default name to the fully
1488 decorated name. This will cause external references which do not
1489 specify a version to be bound to this version of the symbol. */
1490 p = strchr (name, ELF_VER_CHR);
1491 if (p == NULL || p[1] != ELF_VER_CHR)
1492 return TRUE;
1493
1494 if (override)
1495 {
4cc11e76 1496 /* We are overridden by an old definition. We need to check if we
45d6a902
AM
1497 need to create the indirect symbol from the default name. */
1498 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1499 FALSE, FALSE);
1500 BFD_ASSERT (hi != NULL);
1501 if (hi == h)
1502 return TRUE;
1503 while (hi->root.type == bfd_link_hash_indirect
1504 || hi->root.type == bfd_link_hash_warning)
1505 {
1506 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1507 if (hi == h)
1508 return TRUE;
1509 }
1510 }
1511
1512 bed = get_elf_backend_data (abfd);
1513 collect = bed->collect;
1514 dynamic = (abfd->flags & DYNAMIC) != 0;
1515
1516 shortlen = p - name;
1517 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1518 if (shortname == NULL)
1519 return FALSE;
1520 memcpy (shortname, name, shortlen);
1521 shortname[shortlen] = '\0';
1522
1523 /* We are going to create a new symbol. Merge it with any existing
1524 symbol with this name. For the purposes of the merge, act as
1525 though we were defining the symbol we just defined, although we
1526 actually going to define an indirect symbol. */
1527 type_change_ok = FALSE;
1528 size_change_ok = FALSE;
1529 sec = *psec;
1530 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
af44c138
L
1531 NULL, &hi, &skip, &override,
1532 &type_change_ok, &size_change_ok))
45d6a902
AM
1533 return FALSE;
1534
1535 if (skip)
1536 goto nondefault;
1537
1538 if (! override)
1539 {
1540 bh = &hi->root;
1541 if (! (_bfd_generic_link_add_one_symbol
1542 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
268b6b39 1543 0, name, FALSE, collect, &bh)))
45d6a902
AM
1544 return FALSE;
1545 hi = (struct elf_link_hash_entry *) bh;
1546 }
1547 else
1548 {
1549 /* In this case the symbol named SHORTNAME is overriding the
1550 indirect symbol we want to add. We were planning on making
1551 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1552 is the name without a version. NAME is the fully versioned
1553 name, and it is the default version.
1554
1555 Overriding means that we already saw a definition for the
1556 symbol SHORTNAME in a regular object, and it is overriding
1557 the symbol defined in the dynamic object.
1558
1559 When this happens, we actually want to change NAME, the
1560 symbol we just added, to refer to SHORTNAME. This will cause
1561 references to NAME in the shared object to become references
1562 to SHORTNAME in the regular object. This is what we expect
1563 when we override a function in a shared object: that the
1564 references in the shared object will be mapped to the
1565 definition in the regular object. */
1566
1567 while (hi->root.type == bfd_link_hash_indirect
1568 || hi->root.type == bfd_link_hash_warning)
1569 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1570
1571 h->root.type = bfd_link_hash_indirect;
1572 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 1573 if (h->def_dynamic)
45d6a902 1574 {
f5385ebf
AM
1575 h->def_dynamic = 0;
1576 hi->ref_dynamic = 1;
1577 if (hi->ref_regular
1578 || hi->def_regular)
45d6a902 1579 {
c152c796 1580 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
1581 return FALSE;
1582 }
1583 }
1584
1585 /* Now set HI to H, so that the following code will set the
1586 other fields correctly. */
1587 hi = h;
1588 }
1589
fab4a87f
L
1590 /* Check if HI is a warning symbol. */
1591 if (hi->root.type == bfd_link_hash_warning)
1592 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1593
45d6a902
AM
1594 /* If there is a duplicate definition somewhere, then HI may not
1595 point to an indirect symbol. We will have reported an error to
1596 the user in that case. */
1597
1598 if (hi->root.type == bfd_link_hash_indirect)
1599 {
1600 struct elf_link_hash_entry *ht;
1601
45d6a902 1602 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 1603 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902
AM
1604
1605 /* See if the new flags lead us to realize that the symbol must
1606 be dynamic. */
1607 if (! *dynsym)
1608 {
1609 if (! dynamic)
1610 {
1611 if (info->shared
f5385ebf 1612 || hi->ref_dynamic)
45d6a902
AM
1613 *dynsym = TRUE;
1614 }
1615 else
1616 {
f5385ebf 1617 if (hi->ref_regular)
45d6a902
AM
1618 *dynsym = TRUE;
1619 }
1620 }
1621 }
1622
1623 /* We also need to define an indirection from the nondefault version
1624 of the symbol. */
1625
1626nondefault:
1627 len = strlen (name);
1628 shortname = bfd_hash_allocate (&info->hash->table, len);
1629 if (shortname == NULL)
1630 return FALSE;
1631 memcpy (shortname, name, shortlen);
1632 memcpy (shortname + shortlen, p + 1, len - shortlen);
1633
1634 /* Once again, merge with any existing symbol. */
1635 type_change_ok = FALSE;
1636 size_change_ok = FALSE;
1637 sec = *psec;
1638 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
af44c138
L
1639 NULL, &hi, &skip, &override,
1640 &type_change_ok, &size_change_ok))
45d6a902
AM
1641 return FALSE;
1642
1643 if (skip)
1644 return TRUE;
1645
1646 if (override)
1647 {
1648 /* Here SHORTNAME is a versioned name, so we don't expect to see
1649 the type of override we do in the case above unless it is
4cc11e76 1650 overridden by a versioned definition. */
45d6a902
AM
1651 if (hi->root.type != bfd_link_hash_defined
1652 && hi->root.type != bfd_link_hash_defweak)
1653 (*_bfd_error_handler)
d003868e
AM
1654 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1655 abfd, shortname);
45d6a902
AM
1656 }
1657 else
1658 {
1659 bh = &hi->root;
1660 if (! (_bfd_generic_link_add_one_symbol
1661 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 1662 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
1663 return FALSE;
1664 hi = (struct elf_link_hash_entry *) bh;
1665
1666 /* If there is a duplicate definition somewhere, then HI may not
1667 point to an indirect symbol. We will have reported an error
1668 to the user in that case. */
1669
1670 if (hi->root.type == bfd_link_hash_indirect)
1671 {
fcfa13d2 1672 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
45d6a902
AM
1673
1674 /* See if the new flags lead us to realize that the symbol
1675 must be dynamic. */
1676 if (! *dynsym)
1677 {
1678 if (! dynamic)
1679 {
1680 if (info->shared
f5385ebf 1681 || hi->ref_dynamic)
45d6a902
AM
1682 *dynsym = TRUE;
1683 }
1684 else
1685 {
f5385ebf 1686 if (hi->ref_regular)
45d6a902
AM
1687 *dynsym = TRUE;
1688 }
1689 }
1690 }
1691 }
1692
1693 return TRUE;
1694}
1695\f
1696/* This routine is used to export all defined symbols into the dynamic
1697 symbol table. It is called via elf_link_hash_traverse. */
1698
1699bfd_boolean
268b6b39 1700_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 1701{
268b6b39 1702 struct elf_info_failed *eif = data;
45d6a902 1703
55255dae
L
1704 /* Ignore this if we won't export it. */
1705 if (!eif->info->export_dynamic && !h->dynamic)
1706 return TRUE;
1707
45d6a902
AM
1708 /* Ignore indirect symbols. These are added by the versioning code. */
1709 if (h->root.type == bfd_link_hash_indirect)
1710 return TRUE;
1711
1712 if (h->root.type == bfd_link_hash_warning)
1713 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1714
1715 if (h->dynindx == -1
f5385ebf
AM
1716 && (h->def_regular
1717 || h->ref_regular))
45d6a902
AM
1718 {
1719 struct bfd_elf_version_tree *t;
1720 struct bfd_elf_version_expr *d;
1721
1722 for (t = eif->verdefs; t != NULL; t = t->next)
1723 {
108ba305 1724 if (t->globals.list != NULL)
45d6a902 1725 {
108ba305
JJ
1726 d = (*t->match) (&t->globals, NULL, h->root.root.string);
1727 if (d != NULL)
1728 goto doit;
45d6a902
AM
1729 }
1730
108ba305 1731 if (t->locals.list != NULL)
45d6a902 1732 {
108ba305
JJ
1733 d = (*t->match) (&t->locals, NULL, h->root.root.string);
1734 if (d != NULL)
1735 return TRUE;
45d6a902
AM
1736 }
1737 }
1738
1739 if (!eif->verdefs)
1740 {
1741 doit:
c152c796 1742 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
1743 {
1744 eif->failed = TRUE;
1745 return FALSE;
1746 }
1747 }
1748 }
1749
1750 return TRUE;
1751}
1752\f
1753/* Look through the symbols which are defined in other shared
1754 libraries and referenced here. Update the list of version
1755 dependencies. This will be put into the .gnu.version_r section.
1756 This function is called via elf_link_hash_traverse. */
1757
1758bfd_boolean
268b6b39
AM
1759_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1760 void *data)
45d6a902 1761{
268b6b39 1762 struct elf_find_verdep_info *rinfo = data;
45d6a902
AM
1763 Elf_Internal_Verneed *t;
1764 Elf_Internal_Vernaux *a;
1765 bfd_size_type amt;
1766
1767 if (h->root.type == bfd_link_hash_warning)
1768 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1769
1770 /* We only care about symbols defined in shared objects with version
1771 information. */
f5385ebf
AM
1772 if (!h->def_dynamic
1773 || h->def_regular
45d6a902
AM
1774 || h->dynindx == -1
1775 || h->verinfo.verdef == NULL)
1776 return TRUE;
1777
1778 /* See if we already know about this version. */
1779 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1780 {
1781 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1782 continue;
1783
1784 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1785 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1786 return TRUE;
1787
1788 break;
1789 }
1790
1791 /* This is a new version. Add it to tree we are building. */
1792
1793 if (t == NULL)
1794 {
1795 amt = sizeof *t;
268b6b39 1796 t = bfd_zalloc (rinfo->output_bfd, amt);
45d6a902
AM
1797 if (t == NULL)
1798 {
1799 rinfo->failed = TRUE;
1800 return FALSE;
1801 }
1802
1803 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1804 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1805 elf_tdata (rinfo->output_bfd)->verref = t;
1806 }
1807
1808 amt = sizeof *a;
268b6b39 1809 a = bfd_zalloc (rinfo->output_bfd, amt);
14b1c01e
AM
1810 if (a == NULL)
1811 {
1812 rinfo->failed = TRUE;
1813 return FALSE;
1814 }
45d6a902
AM
1815
1816 /* Note that we are copying a string pointer here, and testing it
1817 above. If bfd_elf_string_from_elf_section is ever changed to
1818 discard the string data when low in memory, this will have to be
1819 fixed. */
1820 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1821
1822 a->vna_flags = h->verinfo.verdef->vd_flags;
1823 a->vna_nextptr = t->vn_auxptr;
1824
1825 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1826 ++rinfo->vers;
1827
1828 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1829
1830 t->vn_auxptr = a;
1831
1832 return TRUE;
1833}
1834
1835/* Figure out appropriate versions for all the symbols. We may not
1836 have the version number script until we have read all of the input
1837 files, so until that point we don't know which symbols should be
1838 local. This function is called via elf_link_hash_traverse. */
1839
1840bfd_boolean
268b6b39 1841_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
1842{
1843 struct elf_assign_sym_version_info *sinfo;
1844 struct bfd_link_info *info;
9c5bfbb7 1845 const struct elf_backend_data *bed;
45d6a902
AM
1846 struct elf_info_failed eif;
1847 char *p;
1848 bfd_size_type amt;
1849
268b6b39 1850 sinfo = data;
45d6a902
AM
1851 info = sinfo->info;
1852
1853 if (h->root.type == bfd_link_hash_warning)
1854 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1855
1856 /* Fix the symbol flags. */
1857 eif.failed = FALSE;
1858 eif.info = info;
1859 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1860 {
1861 if (eif.failed)
1862 sinfo->failed = TRUE;
1863 return FALSE;
1864 }
1865
1866 /* We only need version numbers for symbols defined in regular
1867 objects. */
f5385ebf 1868 if (!h->def_regular)
45d6a902
AM
1869 return TRUE;
1870
1871 bed = get_elf_backend_data (sinfo->output_bfd);
1872 p = strchr (h->root.root.string, ELF_VER_CHR);
1873 if (p != NULL && h->verinfo.vertree == NULL)
1874 {
1875 struct bfd_elf_version_tree *t;
1876 bfd_boolean hidden;
1877
1878 hidden = TRUE;
1879
1880 /* There are two consecutive ELF_VER_CHR characters if this is
1881 not a hidden symbol. */
1882 ++p;
1883 if (*p == ELF_VER_CHR)
1884 {
1885 hidden = FALSE;
1886 ++p;
1887 }
1888
1889 /* If there is no version string, we can just return out. */
1890 if (*p == '\0')
1891 {
1892 if (hidden)
f5385ebf 1893 h->hidden = 1;
45d6a902
AM
1894 return TRUE;
1895 }
1896
1897 /* Look for the version. If we find it, it is no longer weak. */
1898 for (t = sinfo->verdefs; t != NULL; t = t->next)
1899 {
1900 if (strcmp (t->name, p) == 0)
1901 {
1902 size_t len;
1903 char *alc;
1904 struct bfd_elf_version_expr *d;
1905
1906 len = p - h->root.root.string;
268b6b39 1907 alc = bfd_malloc (len);
45d6a902 1908 if (alc == NULL)
14b1c01e
AM
1909 {
1910 sinfo->failed = TRUE;
1911 return FALSE;
1912 }
45d6a902
AM
1913 memcpy (alc, h->root.root.string, len - 1);
1914 alc[len - 1] = '\0';
1915 if (alc[len - 2] == ELF_VER_CHR)
1916 alc[len - 2] = '\0';
1917
1918 h->verinfo.vertree = t;
1919 t->used = TRUE;
1920 d = NULL;
1921
108ba305
JJ
1922 if (t->globals.list != NULL)
1923 d = (*t->match) (&t->globals, NULL, alc);
45d6a902
AM
1924
1925 /* See if there is anything to force this symbol to
1926 local scope. */
108ba305 1927 if (d == NULL && t->locals.list != NULL)
45d6a902 1928 {
108ba305
JJ
1929 d = (*t->match) (&t->locals, NULL, alc);
1930 if (d != NULL
1931 && h->dynindx != -1
108ba305
JJ
1932 && ! info->export_dynamic)
1933 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
1934 }
1935
1936 free (alc);
1937 break;
1938 }
1939 }
1940
1941 /* If we are building an application, we need to create a
1942 version node for this version. */
36af4a4e 1943 if (t == NULL && info->executable)
45d6a902
AM
1944 {
1945 struct bfd_elf_version_tree **pp;
1946 int version_index;
1947
1948 /* If we aren't going to export this symbol, we don't need
1949 to worry about it. */
1950 if (h->dynindx == -1)
1951 return TRUE;
1952
1953 amt = sizeof *t;
108ba305 1954 t = bfd_zalloc (sinfo->output_bfd, amt);
45d6a902
AM
1955 if (t == NULL)
1956 {
1957 sinfo->failed = TRUE;
1958 return FALSE;
1959 }
1960
45d6a902 1961 t->name = p;
45d6a902
AM
1962 t->name_indx = (unsigned int) -1;
1963 t->used = TRUE;
1964
1965 version_index = 1;
1966 /* Don't count anonymous version tag. */
1967 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1968 version_index = 0;
1969 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1970 ++version_index;
1971 t->vernum = version_index;
1972
1973 *pp = t;
1974
1975 h->verinfo.vertree = t;
1976 }
1977 else if (t == NULL)
1978 {
1979 /* We could not find the version for a symbol when
1980 generating a shared archive. Return an error. */
1981 (*_bfd_error_handler)
c55fe096 1982 (_("%B: version node not found for symbol %s"),
d003868e 1983 sinfo->output_bfd, h->root.root.string);
45d6a902
AM
1984 bfd_set_error (bfd_error_bad_value);
1985 sinfo->failed = TRUE;
1986 return FALSE;
1987 }
1988
1989 if (hidden)
f5385ebf 1990 h->hidden = 1;
45d6a902
AM
1991 }
1992
1993 /* If we don't have a version for this symbol, see if we can find
1994 something. */
1995 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1996 {
1997 struct bfd_elf_version_tree *t;
1998 struct bfd_elf_version_tree *local_ver;
1999 struct bfd_elf_version_expr *d;
2000
2001 /* See if can find what version this symbol is in. If the
2002 symbol is supposed to be local, then don't actually register
2003 it. */
2004 local_ver = NULL;
2005 for (t = sinfo->verdefs; t != NULL; t = t->next)
2006 {
108ba305 2007 if (t->globals.list != NULL)
45d6a902
AM
2008 {
2009 bfd_boolean matched;
2010
2011 matched = FALSE;
108ba305
JJ
2012 d = NULL;
2013 while ((d = (*t->match) (&t->globals, d,
2014 h->root.root.string)) != NULL)
2015 if (d->symver)
2016 matched = TRUE;
2017 else
2018 {
2019 /* There is a version without definition. Make
2020 the symbol the default definition for this
2021 version. */
2022 h->verinfo.vertree = t;
2023 local_ver = NULL;
2024 d->script = 1;
2025 break;
2026 }
45d6a902
AM
2027 if (d != NULL)
2028 break;
2029 else if (matched)
2030 /* There is no undefined version for this symbol. Hide the
2031 default one. */
2032 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2033 }
2034
108ba305 2035 if (t->locals.list != NULL)
45d6a902 2036 {
108ba305
JJ
2037 d = NULL;
2038 while ((d = (*t->match) (&t->locals, d,
2039 h->root.root.string)) != NULL)
45d6a902 2040 {
108ba305 2041 local_ver = t;
45d6a902 2042 /* If the match is "*", keep looking for a more
108ba305
JJ
2043 explicit, perhaps even global, match.
2044 XXX: Shouldn't this be !d->wildcard instead? */
2045 if (d->pattern[0] != '*' || d->pattern[1] != '\0')
2046 break;
45d6a902
AM
2047 }
2048
2049 if (d != NULL)
2050 break;
2051 }
2052 }
2053
2054 if (local_ver != NULL)
2055 {
2056 h->verinfo.vertree = local_ver;
2057 if (h->dynindx != -1
45d6a902
AM
2058 && ! info->export_dynamic)
2059 {
2060 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2061 }
2062 }
2063 }
2064
2065 return TRUE;
2066}
2067\f
45d6a902
AM
2068/* Read and swap the relocs from the section indicated by SHDR. This
2069 may be either a REL or a RELA section. The relocations are
2070 translated into RELA relocations and stored in INTERNAL_RELOCS,
2071 which should have already been allocated to contain enough space.
2072 The EXTERNAL_RELOCS are a buffer where the external form of the
2073 relocations should be stored.
2074
2075 Returns FALSE if something goes wrong. */
2076
2077static bfd_boolean
268b6b39 2078elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2079 asection *sec,
268b6b39
AM
2080 Elf_Internal_Shdr *shdr,
2081 void *external_relocs,
2082 Elf_Internal_Rela *internal_relocs)
45d6a902 2083{
9c5bfbb7 2084 const struct elf_backend_data *bed;
268b6b39 2085 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2086 const bfd_byte *erela;
2087 const bfd_byte *erelaend;
2088 Elf_Internal_Rela *irela;
243ef1e0
L
2089 Elf_Internal_Shdr *symtab_hdr;
2090 size_t nsyms;
45d6a902 2091
45d6a902
AM
2092 /* Position ourselves at the start of the section. */
2093 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2094 return FALSE;
2095
2096 /* Read the relocations. */
2097 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2098 return FALSE;
2099
243ef1e0
L
2100 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2101 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
2102
45d6a902
AM
2103 bed = get_elf_backend_data (abfd);
2104
2105 /* Convert the external relocations to the internal format. */
2106 if (shdr->sh_entsize == bed->s->sizeof_rel)
2107 swap_in = bed->s->swap_reloc_in;
2108 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2109 swap_in = bed->s->swap_reloca_in;
2110 else
2111 {
2112 bfd_set_error (bfd_error_wrong_format);
2113 return FALSE;
2114 }
2115
2116 erela = external_relocs;
51992aec 2117 erelaend = erela + shdr->sh_size;
45d6a902
AM
2118 irela = internal_relocs;
2119 while (erela < erelaend)
2120 {
243ef1e0
L
2121 bfd_vma r_symndx;
2122
45d6a902 2123 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2124 r_symndx = ELF32_R_SYM (irela->r_info);
2125 if (bed->s->arch_size == 64)
2126 r_symndx >>= 24;
2127 if ((size_t) r_symndx >= nsyms)
2128 {
2129 (*_bfd_error_handler)
d003868e
AM
2130 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2131 " for offset 0x%lx in section `%A'"),
2132 abfd, sec,
2133 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
243ef1e0
L
2134 bfd_set_error (bfd_error_bad_value);
2135 return FALSE;
2136 }
45d6a902
AM
2137 irela += bed->s->int_rels_per_ext_rel;
2138 erela += shdr->sh_entsize;
2139 }
2140
2141 return TRUE;
2142}
2143
2144/* Read and swap the relocs for a section O. They may have been
2145 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2146 not NULL, they are used as buffers to read into. They are known to
2147 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2148 the return value is allocated using either malloc or bfd_alloc,
2149 according to the KEEP_MEMORY argument. If O has two relocation
2150 sections (both REL and RELA relocations), then the REL_HDR
2151 relocations will appear first in INTERNAL_RELOCS, followed by the
2152 REL_HDR2 relocations. */
2153
2154Elf_Internal_Rela *
268b6b39
AM
2155_bfd_elf_link_read_relocs (bfd *abfd,
2156 asection *o,
2157 void *external_relocs,
2158 Elf_Internal_Rela *internal_relocs,
2159 bfd_boolean keep_memory)
45d6a902
AM
2160{
2161 Elf_Internal_Shdr *rel_hdr;
268b6b39 2162 void *alloc1 = NULL;
45d6a902 2163 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2164 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
45d6a902
AM
2165
2166 if (elf_section_data (o)->relocs != NULL)
2167 return elf_section_data (o)->relocs;
2168
2169 if (o->reloc_count == 0)
2170 return NULL;
2171
2172 rel_hdr = &elf_section_data (o)->rel_hdr;
2173
2174 if (internal_relocs == NULL)
2175 {
2176 bfd_size_type size;
2177
2178 size = o->reloc_count;
2179 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2180 if (keep_memory)
268b6b39 2181 internal_relocs = bfd_alloc (abfd, size);
45d6a902 2182 else
268b6b39 2183 internal_relocs = alloc2 = bfd_malloc (size);
45d6a902
AM
2184 if (internal_relocs == NULL)
2185 goto error_return;
2186 }
2187
2188 if (external_relocs == NULL)
2189 {
2190 bfd_size_type size = rel_hdr->sh_size;
2191
2192 if (elf_section_data (o)->rel_hdr2)
2193 size += elf_section_data (o)->rel_hdr2->sh_size;
268b6b39 2194 alloc1 = bfd_malloc (size);
45d6a902
AM
2195 if (alloc1 == NULL)
2196 goto error_return;
2197 external_relocs = alloc1;
2198 }
2199
243ef1e0 2200 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
45d6a902
AM
2201 external_relocs,
2202 internal_relocs))
2203 goto error_return;
51992aec
AM
2204 if (elf_section_data (o)->rel_hdr2
2205 && (!elf_link_read_relocs_from_section
2206 (abfd, o,
2207 elf_section_data (o)->rel_hdr2,
2208 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2209 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2210 * bed->s->int_rels_per_ext_rel))))
45d6a902
AM
2211 goto error_return;
2212
2213 /* Cache the results for next time, if we can. */
2214 if (keep_memory)
2215 elf_section_data (o)->relocs = internal_relocs;
2216
2217 if (alloc1 != NULL)
2218 free (alloc1);
2219
2220 /* Don't free alloc2, since if it was allocated we are passing it
2221 back (under the name of internal_relocs). */
2222
2223 return internal_relocs;
2224
2225 error_return:
2226 if (alloc1 != NULL)
2227 free (alloc1);
2228 if (alloc2 != NULL)
2229 free (alloc2);
2230 return NULL;
2231}
2232
2233/* Compute the size of, and allocate space for, REL_HDR which is the
2234 section header for a section containing relocations for O. */
2235
2236bfd_boolean
268b6b39
AM
2237_bfd_elf_link_size_reloc_section (bfd *abfd,
2238 Elf_Internal_Shdr *rel_hdr,
2239 asection *o)
45d6a902
AM
2240{
2241 bfd_size_type reloc_count;
2242 bfd_size_type num_rel_hashes;
2243
2244 /* Figure out how many relocations there will be. */
2245 if (rel_hdr == &elf_section_data (o)->rel_hdr)
2246 reloc_count = elf_section_data (o)->rel_count;
2247 else
2248 reloc_count = elf_section_data (o)->rel_count2;
2249
2250 num_rel_hashes = o->reloc_count;
2251 if (num_rel_hashes < reloc_count)
2252 num_rel_hashes = reloc_count;
2253
2254 /* That allows us to calculate the size of the section. */
2255 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
2256
2257 /* The contents field must last into write_object_contents, so we
2258 allocate it with bfd_alloc rather than malloc. Also since we
2259 cannot be sure that the contents will actually be filled in,
2260 we zero the allocated space. */
268b6b39 2261 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2262 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2263 return FALSE;
2264
2265 /* We only allocate one set of hash entries, so we only do it the
2266 first time we are called. */
2267 if (elf_section_data (o)->rel_hashes == NULL
2268 && num_rel_hashes)
2269 {
2270 struct elf_link_hash_entry **p;
2271
268b6b39 2272 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
45d6a902
AM
2273 if (p == NULL)
2274 return FALSE;
2275
2276 elf_section_data (o)->rel_hashes = p;
2277 }
2278
2279 return TRUE;
2280}
2281
2282/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2283 originated from the section given by INPUT_REL_HDR) to the
2284 OUTPUT_BFD. */
2285
2286bfd_boolean
268b6b39
AM
2287_bfd_elf_link_output_relocs (bfd *output_bfd,
2288 asection *input_section,
2289 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2290 Elf_Internal_Rela *internal_relocs,
2291 struct elf_link_hash_entry **rel_hash
2292 ATTRIBUTE_UNUSED)
45d6a902
AM
2293{
2294 Elf_Internal_Rela *irela;
2295 Elf_Internal_Rela *irelaend;
2296 bfd_byte *erel;
2297 Elf_Internal_Shdr *output_rel_hdr;
2298 asection *output_section;
2299 unsigned int *rel_countp = NULL;
9c5bfbb7 2300 const struct elf_backend_data *bed;
268b6b39 2301 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
45d6a902
AM
2302
2303 output_section = input_section->output_section;
2304 output_rel_hdr = NULL;
2305
2306 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2307 == input_rel_hdr->sh_entsize)
2308 {
2309 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2310 rel_countp = &elf_section_data (output_section)->rel_count;
2311 }
2312 else if (elf_section_data (output_section)->rel_hdr2
2313 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2314 == input_rel_hdr->sh_entsize))
2315 {
2316 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2317 rel_countp = &elf_section_data (output_section)->rel_count2;
2318 }
2319 else
2320 {
2321 (*_bfd_error_handler)
d003868e
AM
2322 (_("%B: relocation size mismatch in %B section %A"),
2323 output_bfd, input_section->owner, input_section);
45d6a902
AM
2324 bfd_set_error (bfd_error_wrong_object_format);
2325 return FALSE;
2326 }
2327
2328 bed = get_elf_backend_data (output_bfd);
2329 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2330 swap_out = bed->s->swap_reloc_out;
2331 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2332 swap_out = bed->s->swap_reloca_out;
2333 else
2334 abort ();
2335
2336 erel = output_rel_hdr->contents;
2337 erel += *rel_countp * input_rel_hdr->sh_entsize;
2338 irela = internal_relocs;
2339 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2340 * bed->s->int_rels_per_ext_rel);
2341 while (irela < irelaend)
2342 {
2343 (*swap_out) (output_bfd, irela, erel);
2344 irela += bed->s->int_rels_per_ext_rel;
2345 erel += input_rel_hdr->sh_entsize;
2346 }
2347
2348 /* Bump the counter, so that we know where to add the next set of
2349 relocations. */
2350 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2351
2352 return TRUE;
2353}
2354\f
508c3946
L
2355/* Make weak undefined symbols in PIE dynamic. */
2356
2357bfd_boolean
2358_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2359 struct elf_link_hash_entry *h)
2360{
2361 if (info->pie
2362 && h->dynindx == -1
2363 && h->root.type == bfd_link_hash_undefweak)
2364 return bfd_elf_link_record_dynamic_symbol (info, h);
2365
2366 return TRUE;
2367}
2368
45d6a902
AM
2369/* Fix up the flags for a symbol. This handles various cases which
2370 can only be fixed after all the input files are seen. This is
2371 currently called by both adjust_dynamic_symbol and
2372 assign_sym_version, which is unnecessary but perhaps more robust in
2373 the face of future changes. */
2374
2375bfd_boolean
268b6b39
AM
2376_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2377 struct elf_info_failed *eif)
45d6a902 2378{
33774f08 2379 const struct elf_backend_data *bed;
508c3946 2380
45d6a902
AM
2381 /* If this symbol was mentioned in a non-ELF file, try to set
2382 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2383 permit a non-ELF file to correctly refer to a symbol defined in
2384 an ELF dynamic object. */
f5385ebf 2385 if (h->non_elf)
45d6a902
AM
2386 {
2387 while (h->root.type == bfd_link_hash_indirect)
2388 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2389
2390 if (h->root.type != bfd_link_hash_defined
2391 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2392 {
2393 h->ref_regular = 1;
2394 h->ref_regular_nonweak = 1;
2395 }
45d6a902
AM
2396 else
2397 {
2398 if (h->root.u.def.section->owner != NULL
2399 && (bfd_get_flavour (h->root.u.def.section->owner)
2400 == bfd_target_elf_flavour))
f5385ebf
AM
2401 {
2402 h->ref_regular = 1;
2403 h->ref_regular_nonweak = 1;
2404 }
45d6a902 2405 else
f5385ebf 2406 h->def_regular = 1;
45d6a902
AM
2407 }
2408
2409 if (h->dynindx == -1
f5385ebf
AM
2410 && (h->def_dynamic
2411 || h->ref_dynamic))
45d6a902 2412 {
c152c796 2413 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2414 {
2415 eif->failed = TRUE;
2416 return FALSE;
2417 }
2418 }
2419 }
2420 else
2421 {
f5385ebf 2422 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2423 was first seen in a non-ELF file. Fortunately, if the symbol
2424 was first seen in an ELF file, we're probably OK unless the
2425 symbol was defined in a non-ELF file. Catch that case here.
2426 FIXME: We're still in trouble if the symbol was first seen in
2427 a dynamic object, and then later in a non-ELF regular object. */
2428 if ((h->root.type == bfd_link_hash_defined
2429 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2430 && !h->def_regular
45d6a902
AM
2431 && (h->root.u.def.section->owner != NULL
2432 ? (bfd_get_flavour (h->root.u.def.section->owner)
2433 != bfd_target_elf_flavour)
2434 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2435 && !h->def_dynamic)))
2436 h->def_regular = 1;
45d6a902
AM
2437 }
2438
508c3946 2439 /* Backend specific symbol fixup. */
33774f08
AM
2440 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2441 if (bed->elf_backend_fixup_symbol
2442 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2443 return FALSE;
508c3946 2444
45d6a902
AM
2445 /* If this is a final link, and the symbol was defined as a common
2446 symbol in a regular object file, and there was no definition in
2447 any dynamic object, then the linker will have allocated space for
f5385ebf 2448 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2449 flag will not have been set. */
2450 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2451 && !h->def_regular
2452 && h->ref_regular
2453 && !h->def_dynamic
45d6a902 2454 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
f5385ebf 2455 h->def_regular = 1;
45d6a902
AM
2456
2457 /* If -Bsymbolic was used (which means to bind references to global
2458 symbols to the definition within the shared object), and this
2459 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2460 need a PLT entry. Likewise, if the symbol has non-default
2461 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2462 will force it local. */
f5385ebf 2463 if (h->needs_plt
45d6a902 2464 && eif->info->shared
0eddce27 2465 && is_elf_hash_table (eif->info->hash)
55255dae 2466 && (SYMBOLIC_BIND (eif->info, h)
c1be741f 2467 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
f5385ebf 2468 && h->def_regular)
45d6a902 2469 {
45d6a902
AM
2470 bfd_boolean force_local;
2471
45d6a902
AM
2472 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2473 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2474 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2475 }
2476
2477 /* If a weak undefined symbol has non-default visibility, we also
2478 hide it from the dynamic linker. */
9c7a29a3 2479 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902 2480 && h->root.type == bfd_link_hash_undefweak)
33774f08 2481 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
45d6a902
AM
2482
2483 /* If this is a weak defined symbol in a dynamic object, and we know
2484 the real definition in the dynamic object, copy interesting flags
2485 over to the real definition. */
f6e332e6 2486 if (h->u.weakdef != NULL)
45d6a902
AM
2487 {
2488 struct elf_link_hash_entry *weakdef;
2489
f6e332e6 2490 weakdef = h->u.weakdef;
45d6a902
AM
2491 if (h->root.type == bfd_link_hash_indirect)
2492 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2493
2494 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2495 || h->root.type == bfd_link_hash_defweak);
f5385ebf 2496 BFD_ASSERT (weakdef->def_dynamic);
45d6a902
AM
2497
2498 /* If the real definition is defined by a regular object file,
2499 don't do anything special. See the longer description in
2500 _bfd_elf_adjust_dynamic_symbol, below. */
f5385ebf 2501 if (weakdef->def_regular)
f6e332e6 2502 h->u.weakdef = NULL;
45d6a902 2503 else
a26587ba
RS
2504 {
2505 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2506 || weakdef->root.type == bfd_link_hash_defweak);
2507 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2508 }
45d6a902
AM
2509 }
2510
2511 return TRUE;
2512}
2513
2514/* Make the backend pick a good value for a dynamic symbol. This is
2515 called via elf_link_hash_traverse, and also calls itself
2516 recursively. */
2517
2518bfd_boolean
268b6b39 2519_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2520{
268b6b39 2521 struct elf_info_failed *eif = data;
45d6a902 2522 bfd *dynobj;
9c5bfbb7 2523 const struct elf_backend_data *bed;
45d6a902 2524
0eddce27 2525 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2526 return FALSE;
2527
2528 if (h->root.type == bfd_link_hash_warning)
2529 {
a6aa5195
AM
2530 h->got = elf_hash_table (eif->info)->init_got_offset;
2531 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
2532
2533 /* When warning symbols are created, they **replace** the "real"
2534 entry in the hash table, thus we never get to see the real
2535 symbol in a hash traversal. So look at it now. */
2536 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2537 }
2538
2539 /* Ignore indirect symbols. These are added by the versioning code. */
2540 if (h->root.type == bfd_link_hash_indirect)
2541 return TRUE;
2542
2543 /* Fix the symbol flags. */
2544 if (! _bfd_elf_fix_symbol_flags (h, eif))
2545 return FALSE;
2546
2547 /* If this symbol does not require a PLT entry, and it is not
2548 defined by a dynamic object, or is not referenced by a regular
2549 object, ignore it. We do have to handle a weak defined symbol,
2550 even if no regular object refers to it, if we decided to add it
2551 to the dynamic symbol table. FIXME: Do we normally need to worry
2552 about symbols which are defined by one dynamic object and
2553 referenced by another one? */
f5385ebf
AM
2554 if (!h->needs_plt
2555 && (h->def_regular
2556 || !h->def_dynamic
2557 || (!h->ref_regular
f6e332e6 2558 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
45d6a902 2559 {
a6aa5195 2560 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
2561 return TRUE;
2562 }
2563
2564 /* If we've already adjusted this symbol, don't do it again. This
2565 can happen via a recursive call. */
f5385ebf 2566 if (h->dynamic_adjusted)
45d6a902
AM
2567 return TRUE;
2568
2569 /* Don't look at this symbol again. Note that we must set this
2570 after checking the above conditions, because we may look at a
2571 symbol once, decide not to do anything, and then get called
2572 recursively later after REF_REGULAR is set below. */
f5385ebf 2573 h->dynamic_adjusted = 1;
45d6a902
AM
2574
2575 /* If this is a weak definition, and we know a real definition, and
2576 the real symbol is not itself defined by a regular object file,
2577 then get a good value for the real definition. We handle the
2578 real symbol first, for the convenience of the backend routine.
2579
2580 Note that there is a confusing case here. If the real definition
2581 is defined by a regular object file, we don't get the real symbol
2582 from the dynamic object, but we do get the weak symbol. If the
2583 processor backend uses a COPY reloc, then if some routine in the
2584 dynamic object changes the real symbol, we will not see that
2585 change in the corresponding weak symbol. This is the way other
2586 ELF linkers work as well, and seems to be a result of the shared
2587 library model.
2588
2589 I will clarify this issue. Most SVR4 shared libraries define the
2590 variable _timezone and define timezone as a weak synonym. The
2591 tzset call changes _timezone. If you write
2592 extern int timezone;
2593 int _timezone = 5;
2594 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2595 you might expect that, since timezone is a synonym for _timezone,
2596 the same number will print both times. However, if the processor
2597 backend uses a COPY reloc, then actually timezone will be copied
2598 into your process image, and, since you define _timezone
2599 yourself, _timezone will not. Thus timezone and _timezone will
2600 wind up at different memory locations. The tzset call will set
2601 _timezone, leaving timezone unchanged. */
2602
f6e332e6 2603 if (h->u.weakdef != NULL)
45d6a902
AM
2604 {
2605 /* If we get to this point, we know there is an implicit
2606 reference by a regular object file via the weak symbol H.
2607 FIXME: Is this really true? What if the traversal finds
f6e332e6
AM
2608 H->U.WEAKDEF before it finds H? */
2609 h->u.weakdef->ref_regular = 1;
45d6a902 2610
f6e332e6 2611 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
45d6a902
AM
2612 return FALSE;
2613 }
2614
2615 /* If a symbol has no type and no size and does not require a PLT
2616 entry, then we are probably about to do the wrong thing here: we
2617 are probably going to create a COPY reloc for an empty object.
2618 This case can arise when a shared object is built with assembly
2619 code, and the assembly code fails to set the symbol type. */
2620 if (h->size == 0
2621 && h->type == STT_NOTYPE
f5385ebf 2622 && !h->needs_plt)
45d6a902
AM
2623 (*_bfd_error_handler)
2624 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2625 h->root.root.string);
2626
2627 dynobj = elf_hash_table (eif->info)->dynobj;
2628 bed = get_elf_backend_data (dynobj);
2629 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2630 {
2631 eif->failed = TRUE;
2632 return FALSE;
2633 }
2634
2635 return TRUE;
2636}
2637
027297b7
L
2638/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2639 DYNBSS. */
2640
2641bfd_boolean
2642_bfd_elf_adjust_dynamic_copy (struct elf_link_hash_entry *h,
2643 asection *dynbss)
2644{
91ac5911 2645 unsigned int power_of_two;
027297b7
L
2646 bfd_vma mask;
2647 asection *sec = h->root.u.def.section;
2648
2649 /* The section aligment of definition is the maximum alignment
91ac5911
L
2650 requirement of symbols defined in the section. Since we don't
2651 know the symbol alignment requirement, we start with the
2652 maximum alignment and check low bits of the symbol address
2653 for the minimum alignment. */
2654 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2655 mask = ((bfd_vma) 1 << power_of_two) - 1;
2656 while ((h->root.u.def.value & mask) != 0)
2657 {
2658 mask >>= 1;
2659 --power_of_two;
2660 }
027297b7 2661
91ac5911
L
2662 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2663 dynbss))
027297b7
L
2664 {
2665 /* Adjust the section alignment if needed. */
2666 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
91ac5911 2667 power_of_two))
027297b7
L
2668 return FALSE;
2669 }
2670
91ac5911 2671 /* We make sure that the symbol will be aligned properly. */
027297b7
L
2672 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2673
2674 /* Define the symbol as being at this point in DYNBSS. */
2675 h->root.u.def.section = dynbss;
2676 h->root.u.def.value = dynbss->size;
2677
2678 /* Increment the size of DYNBSS to make room for the symbol. */
2679 dynbss->size += h->size;
2680
2681 return TRUE;
2682}
2683
45d6a902
AM
2684/* Adjust all external symbols pointing into SEC_MERGE sections
2685 to reflect the object merging within the sections. */
2686
2687bfd_boolean
268b6b39 2688_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
2689{
2690 asection *sec;
2691
2692 if (h->root.type == bfd_link_hash_warning)
2693 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2694
2695 if ((h->root.type == bfd_link_hash_defined
2696 || h->root.type == bfd_link_hash_defweak)
2697 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2698 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2699 {
268b6b39 2700 bfd *output_bfd = data;
45d6a902
AM
2701
2702 h->root.u.def.value =
2703 _bfd_merged_section_offset (output_bfd,
2704 &h->root.u.def.section,
2705 elf_section_data (sec)->sec_info,
753731ee 2706 h->root.u.def.value);
45d6a902
AM
2707 }
2708
2709 return TRUE;
2710}
986a241f
RH
2711
2712/* Returns false if the symbol referred to by H should be considered
2713 to resolve local to the current module, and true if it should be
2714 considered to bind dynamically. */
2715
2716bfd_boolean
268b6b39
AM
2717_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2718 struct bfd_link_info *info,
2719 bfd_boolean ignore_protected)
986a241f
RH
2720{
2721 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
2722 const struct elf_backend_data *bed;
2723 struct elf_link_hash_table *hash_table;
986a241f
RH
2724
2725 if (h == NULL)
2726 return FALSE;
2727
2728 while (h->root.type == bfd_link_hash_indirect
2729 || h->root.type == bfd_link_hash_warning)
2730 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2731
2732 /* If it was forced local, then clearly it's not dynamic. */
2733 if (h->dynindx == -1)
2734 return FALSE;
f5385ebf 2735 if (h->forced_local)
986a241f
RH
2736 return FALSE;
2737
2738 /* Identify the cases where name binding rules say that a
2739 visible symbol resolves locally. */
55255dae 2740 binding_stays_local_p = info->executable || SYMBOLIC_BIND (info, h);
986a241f
RH
2741
2742 switch (ELF_ST_VISIBILITY (h->other))
2743 {
2744 case STV_INTERNAL:
2745 case STV_HIDDEN:
2746 return FALSE;
2747
2748 case STV_PROTECTED:
fcb93ecf
PB
2749 hash_table = elf_hash_table (info);
2750 if (!is_elf_hash_table (hash_table))
2751 return FALSE;
2752
2753 bed = get_elf_backend_data (hash_table->dynobj);
2754
986a241f
RH
2755 /* Proper resolution for function pointer equality may require
2756 that these symbols perhaps be resolved dynamically, even though
2757 we should be resolving them to the current module. */
fcb93ecf 2758 if (!ignore_protected || !bed->is_function_type (h->type))
986a241f
RH
2759 binding_stays_local_p = TRUE;
2760 break;
2761
2762 default:
986a241f
RH
2763 break;
2764 }
2765
aa37626c 2766 /* If it isn't defined locally, then clearly it's dynamic. */
f5385ebf 2767 if (!h->def_regular)
aa37626c
L
2768 return TRUE;
2769
986a241f
RH
2770 /* Otherwise, the symbol is dynamic if binding rules don't tell
2771 us that it remains local. */
2772 return !binding_stays_local_p;
2773}
f6c52c13
AM
2774
2775/* Return true if the symbol referred to by H should be considered
2776 to resolve local to the current module, and false otherwise. Differs
2777 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2778 undefined symbols and weak symbols. */
2779
2780bfd_boolean
268b6b39
AM
2781_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2782 struct bfd_link_info *info,
2783 bfd_boolean local_protected)
f6c52c13 2784{
fcb93ecf
PB
2785 const struct elf_backend_data *bed;
2786 struct elf_link_hash_table *hash_table;
2787
f6c52c13
AM
2788 /* If it's a local sym, of course we resolve locally. */
2789 if (h == NULL)
2790 return TRUE;
2791
7e2294f9
AO
2792 /* Common symbols that become definitions don't get the DEF_REGULAR
2793 flag set, so test it first, and don't bail out. */
2794 if (ELF_COMMON_DEF_P (h))
2795 /* Do nothing. */;
f6c52c13 2796 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
2797 resolve locally. The sym is either undefined or dynamic. */
2798 else if (!h->def_regular)
f6c52c13
AM
2799 return FALSE;
2800
2801 /* Forced local symbols resolve locally. */
f5385ebf 2802 if (h->forced_local)
f6c52c13
AM
2803 return TRUE;
2804
2805 /* As do non-dynamic symbols. */
2806 if (h->dynindx == -1)
2807 return TRUE;
2808
2809 /* At this point, we know the symbol is defined and dynamic. In an
2810 executable it must resolve locally, likewise when building symbolic
2811 shared libraries. */
55255dae 2812 if (info->executable || SYMBOLIC_BIND (info, h))
f6c52c13
AM
2813 return TRUE;
2814
2815 /* Now deal with defined dynamic symbols in shared libraries. Ones
2816 with default visibility might not resolve locally. */
2817 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2818 return FALSE;
2819
2820 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2821 if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED)
2822 return TRUE;
2823
fcb93ecf
PB
2824 hash_table = elf_hash_table (info);
2825 if (!is_elf_hash_table (hash_table))
2826 return TRUE;
2827
2828 bed = get_elf_backend_data (hash_table->dynobj);
2829
1c16dfa5 2830 /* STV_PROTECTED non-function symbols are local. */
fcb93ecf 2831 if (!bed->is_function_type (h->type))
1c16dfa5
L
2832 return TRUE;
2833
f6c52c13
AM
2834 /* Function pointer equality tests may require that STV_PROTECTED
2835 symbols be treated as dynamic symbols, even when we know that the
2836 dynamic linker will resolve them locally. */
2837 return local_protected;
2838}
e1918d23
AM
2839
2840/* Caches some TLS segment info, and ensures that the TLS segment vma is
2841 aligned. Returns the first TLS output section. */
2842
2843struct bfd_section *
2844_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2845{
2846 struct bfd_section *sec, *tls;
2847 unsigned int align = 0;
2848
2849 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2850 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2851 break;
2852 tls = sec;
2853
2854 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2855 if (sec->alignment_power > align)
2856 align = sec->alignment_power;
2857
2858 elf_hash_table (info)->tls_sec = tls;
2859
2860 /* Ensure the alignment of the first section is the largest alignment,
2861 so that the tls segment starts aligned. */
2862 if (tls != NULL)
2863 tls->alignment_power = align;
2864
2865 return tls;
2866}
0ad989f9
L
2867
2868/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2869static bfd_boolean
2870is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2871 Elf_Internal_Sym *sym)
2872{
a4d8e49b
L
2873 const struct elf_backend_data *bed;
2874
0ad989f9
L
2875 /* Local symbols do not count, but target specific ones might. */
2876 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2877 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2878 return FALSE;
2879
fcb93ecf 2880 bed = get_elf_backend_data (abfd);
0ad989f9 2881 /* Function symbols do not count. */
fcb93ecf 2882 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
2883 return FALSE;
2884
2885 /* If the section is undefined, then so is the symbol. */
2886 if (sym->st_shndx == SHN_UNDEF)
2887 return FALSE;
2888
2889 /* If the symbol is defined in the common section, then
2890 it is a common definition and so does not count. */
a4d8e49b 2891 if (bed->common_definition (sym))
0ad989f9
L
2892 return FALSE;
2893
2894 /* If the symbol is in a target specific section then we
2895 must rely upon the backend to tell us what it is. */
2896 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2897 /* FIXME - this function is not coded yet:
2898
2899 return _bfd_is_global_symbol_definition (abfd, sym);
2900
2901 Instead for now assume that the definition is not global,
2902 Even if this is wrong, at least the linker will behave
2903 in the same way that it used to do. */
2904 return FALSE;
2905
2906 return TRUE;
2907}
2908
2909/* Search the symbol table of the archive element of the archive ABFD
2910 whose archive map contains a mention of SYMDEF, and determine if
2911 the symbol is defined in this element. */
2912static bfd_boolean
2913elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2914{
2915 Elf_Internal_Shdr * hdr;
2916 bfd_size_type symcount;
2917 bfd_size_type extsymcount;
2918 bfd_size_type extsymoff;
2919 Elf_Internal_Sym *isymbuf;
2920 Elf_Internal_Sym *isym;
2921 Elf_Internal_Sym *isymend;
2922 bfd_boolean result;
2923
2924 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2925 if (abfd == NULL)
2926 return FALSE;
2927
2928 if (! bfd_check_format (abfd, bfd_object))
2929 return FALSE;
2930
2931 /* If we have already included the element containing this symbol in the
2932 link then we do not need to include it again. Just claim that any symbol
2933 it contains is not a definition, so that our caller will not decide to
2934 (re)include this element. */
2935 if (abfd->archive_pass)
2936 return FALSE;
2937
2938 /* Select the appropriate symbol table. */
2939 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2940 hdr = &elf_tdata (abfd)->symtab_hdr;
2941 else
2942 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2943
2944 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2945
2946 /* The sh_info field of the symtab header tells us where the
2947 external symbols start. We don't care about the local symbols. */
2948 if (elf_bad_symtab (abfd))
2949 {
2950 extsymcount = symcount;
2951 extsymoff = 0;
2952 }
2953 else
2954 {
2955 extsymcount = symcount - hdr->sh_info;
2956 extsymoff = hdr->sh_info;
2957 }
2958
2959 if (extsymcount == 0)
2960 return FALSE;
2961
2962 /* Read in the symbol table. */
2963 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2964 NULL, NULL, NULL);
2965 if (isymbuf == NULL)
2966 return FALSE;
2967
2968 /* Scan the symbol table looking for SYMDEF. */
2969 result = FALSE;
2970 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2971 {
2972 const char *name;
2973
2974 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
2975 isym->st_name);
2976 if (name == NULL)
2977 break;
2978
2979 if (strcmp (name, symdef->name) == 0)
2980 {
2981 result = is_global_data_symbol_definition (abfd, isym);
2982 break;
2983 }
2984 }
2985
2986 free (isymbuf);
2987
2988 return result;
2989}
2990\f
5a580b3a
AM
2991/* Add an entry to the .dynamic table. */
2992
2993bfd_boolean
2994_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
2995 bfd_vma tag,
2996 bfd_vma val)
2997{
2998 struct elf_link_hash_table *hash_table;
2999 const struct elf_backend_data *bed;
3000 asection *s;
3001 bfd_size_type newsize;
3002 bfd_byte *newcontents;
3003 Elf_Internal_Dyn dyn;
3004
3005 hash_table = elf_hash_table (info);
3006 if (! is_elf_hash_table (hash_table))
3007 return FALSE;
3008
3009 bed = get_elf_backend_data (hash_table->dynobj);
3010 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
3011 BFD_ASSERT (s != NULL);
3012
eea6121a 3013 newsize = s->size + bed->s->sizeof_dyn;
5a580b3a
AM
3014 newcontents = bfd_realloc (s->contents, newsize);
3015 if (newcontents == NULL)
3016 return FALSE;
3017
3018 dyn.d_tag = tag;
3019 dyn.d_un.d_val = val;
eea6121a 3020 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3021
eea6121a 3022 s->size = newsize;
5a580b3a
AM
3023 s->contents = newcontents;
3024
3025 return TRUE;
3026}
3027
3028/* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3029 otherwise just check whether one already exists. Returns -1 on error,
3030 1 if a DT_NEEDED tag already exists, and 0 on success. */
3031
4ad4eba5 3032static int
7e9f0867
AM
3033elf_add_dt_needed_tag (bfd *abfd,
3034 struct bfd_link_info *info,
4ad4eba5
AM
3035 const char *soname,
3036 bfd_boolean do_it)
5a580b3a
AM
3037{
3038 struct elf_link_hash_table *hash_table;
3039 bfd_size_type oldsize;
3040 bfd_size_type strindex;
3041
7e9f0867
AM
3042 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3043 return -1;
3044
5a580b3a
AM
3045 hash_table = elf_hash_table (info);
3046 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
3047 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3048 if (strindex == (bfd_size_type) -1)
3049 return -1;
3050
3051 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
3052 {
3053 asection *sdyn;
3054 const struct elf_backend_data *bed;
3055 bfd_byte *extdyn;
3056
3057 bed = get_elf_backend_data (hash_table->dynobj);
3058 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3059 if (sdyn != NULL)
3060 for (extdyn = sdyn->contents;
3061 extdyn < sdyn->contents + sdyn->size;
3062 extdyn += bed->s->sizeof_dyn)
3063 {
3064 Elf_Internal_Dyn dyn;
5a580b3a 3065
7e9f0867
AM
3066 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3067 if (dyn.d_tag == DT_NEEDED
3068 && dyn.d_un.d_val == strindex)
3069 {
3070 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3071 return 1;
3072 }
3073 }
5a580b3a
AM
3074 }
3075
3076 if (do_it)
3077 {
7e9f0867
AM
3078 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3079 return -1;
3080
5a580b3a
AM
3081 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3082 return -1;
3083 }
3084 else
3085 /* We were just checking for existence of the tag. */
3086 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3087
3088 return 0;
3089}
3090
3091/* Sort symbol by value and section. */
4ad4eba5
AM
3092static int
3093elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3094{
3095 const struct elf_link_hash_entry *h1;
3096 const struct elf_link_hash_entry *h2;
10b7e05b 3097 bfd_signed_vma vdiff;
5a580b3a
AM
3098
3099 h1 = *(const struct elf_link_hash_entry **) arg1;
3100 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3101 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3102 if (vdiff != 0)
3103 return vdiff > 0 ? 1 : -1;
3104 else
3105 {
3106 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3107 if (sdiff != 0)
3108 return sdiff > 0 ? 1 : -1;
3109 }
5a580b3a
AM
3110 return 0;
3111}
4ad4eba5 3112
5a580b3a
AM
3113/* This function is used to adjust offsets into .dynstr for
3114 dynamic symbols. This is called via elf_link_hash_traverse. */
3115
3116static bfd_boolean
3117elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3118{
3119 struct elf_strtab_hash *dynstr = data;
3120
3121 if (h->root.type == bfd_link_hash_warning)
3122 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3123
3124 if (h->dynindx != -1)
3125 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3126 return TRUE;
3127}
3128
3129/* Assign string offsets in .dynstr, update all structures referencing
3130 them. */
3131
4ad4eba5
AM
3132static bfd_boolean
3133elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3134{
3135 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3136 struct elf_link_local_dynamic_entry *entry;
3137 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3138 bfd *dynobj = hash_table->dynobj;
3139 asection *sdyn;
3140 bfd_size_type size;
3141 const struct elf_backend_data *bed;
3142 bfd_byte *extdyn;
3143
3144 _bfd_elf_strtab_finalize (dynstr);
3145 size = _bfd_elf_strtab_size (dynstr);
3146
3147 bed = get_elf_backend_data (dynobj);
3148 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3149 BFD_ASSERT (sdyn != NULL);
3150
3151 /* Update all .dynamic entries referencing .dynstr strings. */
3152 for (extdyn = sdyn->contents;
eea6121a 3153 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3154 extdyn += bed->s->sizeof_dyn)
3155 {
3156 Elf_Internal_Dyn dyn;
3157
3158 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3159 switch (dyn.d_tag)
3160 {
3161 case DT_STRSZ:
3162 dyn.d_un.d_val = size;
3163 break;
3164 case DT_NEEDED:
3165 case DT_SONAME:
3166 case DT_RPATH:
3167 case DT_RUNPATH:
3168 case DT_FILTER:
3169 case DT_AUXILIARY:
3170 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3171 break;
3172 default:
3173 continue;
3174 }
3175 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3176 }
3177
3178 /* Now update local dynamic symbols. */
3179 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3180 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3181 entry->isym.st_name);
3182
3183 /* And the rest of dynamic symbols. */
3184 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3185
3186 /* Adjust version definitions. */
3187 if (elf_tdata (output_bfd)->cverdefs)
3188 {
3189 asection *s;
3190 bfd_byte *p;
3191 bfd_size_type i;
3192 Elf_Internal_Verdef def;
3193 Elf_Internal_Verdaux defaux;
3194
3195 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3196 p = s->contents;
3197 do
3198 {
3199 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3200 &def);
3201 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3202 if (def.vd_aux != sizeof (Elf_External_Verdef))
3203 continue;
5a580b3a
AM
3204 for (i = 0; i < def.vd_cnt; ++i)
3205 {
3206 _bfd_elf_swap_verdaux_in (output_bfd,
3207 (Elf_External_Verdaux *) p, &defaux);
3208 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3209 defaux.vda_name);
3210 _bfd_elf_swap_verdaux_out (output_bfd,
3211 &defaux, (Elf_External_Verdaux *) p);
3212 p += sizeof (Elf_External_Verdaux);
3213 }
3214 }
3215 while (def.vd_next);
3216 }
3217
3218 /* Adjust version references. */
3219 if (elf_tdata (output_bfd)->verref)
3220 {
3221 asection *s;
3222 bfd_byte *p;
3223 bfd_size_type i;
3224 Elf_Internal_Verneed need;
3225 Elf_Internal_Vernaux needaux;
3226
3227 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3228 p = s->contents;
3229 do
3230 {
3231 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3232 &need);
3233 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3234 _bfd_elf_swap_verneed_out (output_bfd, &need,
3235 (Elf_External_Verneed *) p);
3236 p += sizeof (Elf_External_Verneed);
3237 for (i = 0; i < need.vn_cnt; ++i)
3238 {
3239 _bfd_elf_swap_vernaux_in (output_bfd,
3240 (Elf_External_Vernaux *) p, &needaux);
3241 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3242 needaux.vna_name);
3243 _bfd_elf_swap_vernaux_out (output_bfd,
3244 &needaux,
3245 (Elf_External_Vernaux *) p);
3246 p += sizeof (Elf_External_Vernaux);
3247 }
3248 }
3249 while (need.vn_next);
3250 }
3251
3252 return TRUE;
3253}
3254\f
4ad4eba5
AM
3255/* Add symbols from an ELF object file to the linker hash table. */
3256
3257static bfd_boolean
3258elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3259{
4ad4eba5
AM
3260 Elf_Internal_Shdr *hdr;
3261 bfd_size_type symcount;
3262 bfd_size_type extsymcount;
3263 bfd_size_type extsymoff;
3264 struct elf_link_hash_entry **sym_hash;
3265 bfd_boolean dynamic;
3266 Elf_External_Versym *extversym = NULL;
3267 Elf_External_Versym *ever;
3268 struct elf_link_hash_entry *weaks;
3269 struct elf_link_hash_entry **nondeflt_vers = NULL;
3270 bfd_size_type nondeflt_vers_cnt = 0;
3271 Elf_Internal_Sym *isymbuf = NULL;
3272 Elf_Internal_Sym *isym;
3273 Elf_Internal_Sym *isymend;
3274 const struct elf_backend_data *bed;
3275 bfd_boolean add_needed;
66eb6687 3276 struct elf_link_hash_table *htab;
4ad4eba5 3277 bfd_size_type amt;
66eb6687 3278 void *alloc_mark = NULL;
4f87808c
AM
3279 struct bfd_hash_entry **old_table = NULL;
3280 unsigned int old_size = 0;
3281 unsigned int old_count = 0;
66eb6687
AM
3282 void *old_tab = NULL;
3283 void *old_hash;
3284 void *old_ent;
3285 struct bfd_link_hash_entry *old_undefs = NULL;
3286 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3287 long old_dynsymcount = 0;
3288 size_t tabsize = 0;
3289 size_t hashsize = 0;
4ad4eba5 3290
66eb6687 3291 htab = elf_hash_table (info);
4ad4eba5 3292 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
3293
3294 if ((abfd->flags & DYNAMIC) == 0)
3295 dynamic = FALSE;
3296 else
3297 {
3298 dynamic = TRUE;
3299
3300 /* You can't use -r against a dynamic object. Also, there's no
3301 hope of using a dynamic object which does not exactly match
3302 the format of the output file. */
3303 if (info->relocatable
66eb6687
AM
3304 || !is_elf_hash_table (htab)
3305 || htab->root.creator != abfd->xvec)
4ad4eba5 3306 {
9a0789ec
NC
3307 if (info->relocatable)
3308 bfd_set_error (bfd_error_invalid_operation);
3309 else
3310 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
3311 goto error_return;
3312 }
3313 }
3314
3315 /* As a GNU extension, any input sections which are named
3316 .gnu.warning.SYMBOL are treated as warning symbols for the given
3317 symbol. This differs from .gnu.warning sections, which generate
3318 warnings when they are included in an output file. */
3319 if (info->executable)
3320 {
3321 asection *s;
3322
3323 for (s = abfd->sections; s != NULL; s = s->next)
3324 {
3325 const char *name;
3326
3327 name = bfd_get_section_name (abfd, s);
0112cd26 3328 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5
AM
3329 {
3330 char *msg;
3331 bfd_size_type sz;
4ad4eba5
AM
3332
3333 name += sizeof ".gnu.warning." - 1;
3334
3335 /* If this is a shared object, then look up the symbol
3336 in the hash table. If it is there, and it is already
3337 been defined, then we will not be using the entry
3338 from this shared object, so we don't need to warn.
3339 FIXME: If we see the definition in a regular object
3340 later on, we will warn, but we shouldn't. The only
3341 fix is to keep track of what warnings we are supposed
3342 to emit, and then handle them all at the end of the
3343 link. */
3344 if (dynamic)
3345 {
3346 struct elf_link_hash_entry *h;
3347
66eb6687 3348 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
4ad4eba5
AM
3349
3350 /* FIXME: What about bfd_link_hash_common? */
3351 if (h != NULL
3352 && (h->root.type == bfd_link_hash_defined
3353 || h->root.type == bfd_link_hash_defweak))
3354 {
3355 /* We don't want to issue this warning. Clobber
3356 the section size so that the warning does not
3357 get copied into the output file. */
eea6121a 3358 s->size = 0;
4ad4eba5
AM
3359 continue;
3360 }
3361 }
3362
eea6121a 3363 sz = s->size;
370a0e1b 3364 msg = bfd_alloc (abfd, sz + 1);
4ad4eba5
AM
3365 if (msg == NULL)
3366 goto error_return;
3367
370a0e1b 3368 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4ad4eba5
AM
3369 goto error_return;
3370
370a0e1b 3371 msg[sz] = '\0';
4ad4eba5
AM
3372
3373 if (! (_bfd_generic_link_add_one_symbol
3374 (info, abfd, name, BSF_WARNING, s, 0, msg,
66eb6687 3375 FALSE, bed->collect, NULL)))
4ad4eba5
AM
3376 goto error_return;
3377
3378 if (! info->relocatable)
3379 {
3380 /* Clobber the section size so that the warning does
3381 not get copied into the output file. */
eea6121a 3382 s->size = 0;
11d2f718
AM
3383
3384 /* Also set SEC_EXCLUDE, so that symbols defined in
3385 the warning section don't get copied to the output. */
3386 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
3387 }
3388 }
3389 }
3390 }
3391
3392 add_needed = TRUE;
3393 if (! dynamic)
3394 {
3395 /* If we are creating a shared library, create all the dynamic
3396 sections immediately. We need to attach them to something,
3397 so we attach them to this BFD, provided it is the right
3398 format. FIXME: If there are no input BFD's of the same
3399 format as the output, we can't make a shared library. */
3400 if (info->shared
66eb6687
AM
3401 && is_elf_hash_table (htab)
3402 && htab->root.creator == abfd->xvec
3403 && !htab->dynamic_sections_created)
4ad4eba5
AM
3404 {
3405 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3406 goto error_return;
3407 }
3408 }
66eb6687 3409 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
3410 goto error_return;
3411 else
3412 {
3413 asection *s;
3414 const char *soname = NULL;
3415 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3416 int ret;
3417
3418 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 3419 ld shouldn't allow it. */
4ad4eba5
AM
3420 if ((s = abfd->sections) != NULL
3421 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
92fd189d 3422 abort ();
4ad4eba5
AM
3423
3424 /* If this dynamic lib was specified on the command line with
3425 --as-needed in effect, then we don't want to add a DT_NEEDED
3426 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
3427 in by another lib's DT_NEEDED. When --no-add-needed is used
3428 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3429 any dynamic library in DT_NEEDED tags in the dynamic lib at
3430 all. */
3431 add_needed = (elf_dyn_lib_class (abfd)
3432 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3433 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
3434
3435 s = bfd_get_section_by_name (abfd, ".dynamic");
3436 if (s != NULL)
3437 {
3438 bfd_byte *dynbuf;
3439 bfd_byte *extdyn;
3440 int elfsec;
3441 unsigned long shlink;
3442
eea6121a 3443 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4ad4eba5
AM
3444 goto error_free_dyn;
3445
3446 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3447 if (elfsec == -1)
3448 goto error_free_dyn;
3449 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3450
3451 for (extdyn = dynbuf;
eea6121a 3452 extdyn < dynbuf + s->size;
4ad4eba5
AM
3453 extdyn += bed->s->sizeof_dyn)
3454 {
3455 Elf_Internal_Dyn dyn;
3456
3457 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3458 if (dyn.d_tag == DT_SONAME)
3459 {
3460 unsigned int tagv = dyn.d_un.d_val;
3461 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3462 if (soname == NULL)
3463 goto error_free_dyn;
3464 }
3465 if (dyn.d_tag == DT_NEEDED)
3466 {
3467 struct bfd_link_needed_list *n, **pn;
3468 char *fnm, *anm;
3469 unsigned int tagv = dyn.d_un.d_val;
3470
3471 amt = sizeof (struct bfd_link_needed_list);
3472 n = bfd_alloc (abfd, amt);
3473 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3474 if (n == NULL || fnm == NULL)
3475 goto error_free_dyn;
3476 amt = strlen (fnm) + 1;
3477 anm = bfd_alloc (abfd, amt);
3478 if (anm == NULL)
3479 goto error_free_dyn;
3480 memcpy (anm, fnm, amt);
3481 n->name = anm;
3482 n->by = abfd;
3483 n->next = NULL;
66eb6687 3484 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3485 ;
3486 *pn = n;
3487 }
3488 if (dyn.d_tag == DT_RUNPATH)
3489 {
3490 struct bfd_link_needed_list *n, **pn;
3491 char *fnm, *anm;
3492 unsigned int tagv = dyn.d_un.d_val;
3493
3494 amt = sizeof (struct bfd_link_needed_list);
3495 n = bfd_alloc (abfd, amt);
3496 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3497 if (n == NULL || fnm == NULL)
3498 goto error_free_dyn;
3499 amt = strlen (fnm) + 1;
3500 anm = bfd_alloc (abfd, amt);
3501 if (anm == NULL)
3502 goto error_free_dyn;
3503 memcpy (anm, fnm, amt);
3504 n->name = anm;
3505 n->by = abfd;
3506 n->next = NULL;
3507 for (pn = & runpath;
3508 *pn != NULL;
3509 pn = &(*pn)->next)
3510 ;
3511 *pn = n;
3512 }
3513 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3514 if (!runpath && dyn.d_tag == DT_RPATH)
3515 {
3516 struct bfd_link_needed_list *n, **pn;
3517 char *fnm, *anm;
3518 unsigned int tagv = dyn.d_un.d_val;
3519
3520 amt = sizeof (struct bfd_link_needed_list);
3521 n = bfd_alloc (abfd, amt);
3522 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3523 if (n == NULL || fnm == NULL)
3524 goto error_free_dyn;
3525 amt = strlen (fnm) + 1;
3526 anm = bfd_alloc (abfd, amt);
3527 if (anm == NULL)
3528 {
3529 error_free_dyn:
3530 free (dynbuf);
3531 goto error_return;
3532 }
3533 memcpy (anm, fnm, amt);
3534 n->name = anm;
3535 n->by = abfd;
3536 n->next = NULL;
3537 for (pn = & rpath;
3538 *pn != NULL;
3539 pn = &(*pn)->next)
3540 ;
3541 *pn = n;
3542 }
3543 }
3544
3545 free (dynbuf);
3546 }
3547
3548 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3549 frees all more recently bfd_alloc'd blocks as well. */
3550 if (runpath)
3551 rpath = runpath;
3552
3553 if (rpath)
3554 {
3555 struct bfd_link_needed_list **pn;
66eb6687 3556 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
3557 ;
3558 *pn = rpath;
3559 }
3560
3561 /* We do not want to include any of the sections in a dynamic
3562 object in the output file. We hack by simply clobbering the
3563 list of sections in the BFD. This could be handled more
3564 cleanly by, say, a new section flag; the existing
3565 SEC_NEVER_LOAD flag is not the one we want, because that one
3566 still implies that the section takes up space in the output
3567 file. */
3568 bfd_section_list_clear (abfd);
3569
4ad4eba5
AM
3570 /* Find the name to use in a DT_NEEDED entry that refers to this
3571 object. If the object has a DT_SONAME entry, we use it.
3572 Otherwise, if the generic linker stuck something in
3573 elf_dt_name, we use that. Otherwise, we just use the file
3574 name. */
3575 if (soname == NULL || *soname == '\0')
3576 {
3577 soname = elf_dt_name (abfd);
3578 if (soname == NULL || *soname == '\0')
3579 soname = bfd_get_filename (abfd);
3580 }
3581
3582 /* Save the SONAME because sometimes the linker emulation code
3583 will need to know it. */
3584 elf_dt_name (abfd) = soname;
3585
7e9f0867 3586 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
3587 if (ret < 0)
3588 goto error_return;
3589
3590 /* If we have already included this dynamic object in the
3591 link, just ignore it. There is no reason to include a
3592 particular dynamic object more than once. */
3593 if (ret > 0)
3594 return TRUE;
3595 }
3596
3597 /* If this is a dynamic object, we always link against the .dynsym
3598 symbol table, not the .symtab symbol table. The dynamic linker
3599 will only see the .dynsym symbol table, so there is no reason to
3600 look at .symtab for a dynamic object. */
3601
3602 if (! dynamic || elf_dynsymtab (abfd) == 0)
3603 hdr = &elf_tdata (abfd)->symtab_hdr;
3604 else
3605 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3606
3607 symcount = hdr->sh_size / bed->s->sizeof_sym;
3608
3609 /* The sh_info field of the symtab header tells us where the
3610 external symbols start. We don't care about the local symbols at
3611 this point. */
3612 if (elf_bad_symtab (abfd))
3613 {
3614 extsymcount = symcount;
3615 extsymoff = 0;
3616 }
3617 else
3618 {
3619 extsymcount = symcount - hdr->sh_info;
3620 extsymoff = hdr->sh_info;
3621 }
3622
3623 sym_hash = NULL;
3624 if (extsymcount != 0)
3625 {
3626 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3627 NULL, NULL, NULL);
3628 if (isymbuf == NULL)
3629 goto error_return;
3630
3631 /* We store a pointer to the hash table entry for each external
3632 symbol. */
3633 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3634 sym_hash = bfd_alloc (abfd, amt);
3635 if (sym_hash == NULL)
3636 goto error_free_sym;
3637 elf_sym_hashes (abfd) = sym_hash;
3638 }
3639
3640 if (dynamic)
3641 {
3642 /* Read in any version definitions. */
fc0e6df6
PB
3643 if (!_bfd_elf_slurp_version_tables (abfd,
3644 info->default_imported_symver))
4ad4eba5
AM
3645 goto error_free_sym;
3646
3647 /* Read in the symbol versions, but don't bother to convert them
3648 to internal format. */
3649 if (elf_dynversym (abfd) != 0)
3650 {
3651 Elf_Internal_Shdr *versymhdr;
3652
3653 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3654 extversym = bfd_malloc (versymhdr->sh_size);
3655 if (extversym == NULL)
3656 goto error_free_sym;
3657 amt = versymhdr->sh_size;
3658 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3659 || bfd_bread (extversym, amt, abfd) != amt)
3660 goto error_free_vers;
3661 }
3662 }
3663
66eb6687
AM
3664 /* If we are loading an as-needed shared lib, save the symbol table
3665 state before we start adding symbols. If the lib turns out
3666 to be unneeded, restore the state. */
3667 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3668 {
3669 unsigned int i;
3670 size_t entsize;
3671
3672 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3673 {
3674 struct bfd_hash_entry *p;
2de92251 3675 struct elf_link_hash_entry *h;
66eb6687
AM
3676
3677 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
3678 {
3679 h = (struct elf_link_hash_entry *) p;
3680 entsize += htab->root.table.entsize;
3681 if (h->root.type == bfd_link_hash_warning)
3682 entsize += htab->root.table.entsize;
3683 }
66eb6687
AM
3684 }
3685
3686 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
3687 hashsize = extsymcount * sizeof (struct elf_link_hash_entry *);
3688 old_tab = bfd_malloc (tabsize + entsize + hashsize);
3689 if (old_tab == NULL)
3690 goto error_free_vers;
3691
3692 /* Remember the current objalloc pointer, so that all mem for
3693 symbols added can later be reclaimed. */
3694 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3695 if (alloc_mark == NULL)
3696 goto error_free_vers;
3697
5061a885
AM
3698 /* Make a special call to the linker "notice" function to
3699 tell it that we are about to handle an as-needed lib. */
3700 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
3701 notice_as_needed))
9af2a943 3702 goto error_free_vers;
5061a885 3703
66eb6687
AM
3704 /* Clone the symbol table and sym hashes. Remember some
3705 pointers into the symbol table, and dynamic symbol count. */
3706 old_hash = (char *) old_tab + tabsize;
3707 old_ent = (char *) old_hash + hashsize;
3708 memcpy (old_tab, htab->root.table.table, tabsize);
3709 memcpy (old_hash, sym_hash, hashsize);
3710 old_undefs = htab->root.undefs;
3711 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
3712 old_table = htab->root.table.table;
3713 old_size = htab->root.table.size;
3714 old_count = htab->root.table.count;
66eb6687
AM
3715 old_dynsymcount = htab->dynsymcount;
3716
3717 for (i = 0; i < htab->root.table.size; i++)
3718 {
3719 struct bfd_hash_entry *p;
2de92251 3720 struct elf_link_hash_entry *h;
66eb6687
AM
3721
3722 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3723 {
3724 memcpy (old_ent, p, htab->root.table.entsize);
3725 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
3726 h = (struct elf_link_hash_entry *) p;
3727 if (h->root.type == bfd_link_hash_warning)
3728 {
3729 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3730 old_ent = (char *) old_ent + htab->root.table.entsize;
3731 }
66eb6687
AM
3732 }
3733 }
3734 }
4ad4eba5 3735
66eb6687 3736 weaks = NULL;
4ad4eba5
AM
3737 ever = extversym != NULL ? extversym + extsymoff : NULL;
3738 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3739 isym < isymend;
3740 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3741 {
3742 int bind;
3743 bfd_vma value;
af44c138 3744 asection *sec, *new_sec;
4ad4eba5
AM
3745 flagword flags;
3746 const char *name;
3747 struct elf_link_hash_entry *h;
3748 bfd_boolean definition;
3749 bfd_boolean size_change_ok;
3750 bfd_boolean type_change_ok;
3751 bfd_boolean new_weakdef;
3752 bfd_boolean override;
a4d8e49b 3753 bfd_boolean common;
4ad4eba5
AM
3754 unsigned int old_alignment;
3755 bfd *old_bfd;
3756
3757 override = FALSE;
3758
3759 flags = BSF_NO_FLAGS;
3760 sec = NULL;
3761 value = isym->st_value;
3762 *sym_hash = NULL;
a4d8e49b 3763 common = bed->common_definition (isym);
4ad4eba5
AM
3764
3765 bind = ELF_ST_BIND (isym->st_info);
3766 if (bind == STB_LOCAL)
3767 {
3768 /* This should be impossible, since ELF requires that all
3769 global symbols follow all local symbols, and that sh_info
3770 point to the first global symbol. Unfortunately, Irix 5
3771 screws this up. */
3772 continue;
3773 }
3774 else if (bind == STB_GLOBAL)
3775 {
a4d8e49b 3776 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5
AM
3777 flags = BSF_GLOBAL;
3778 }
3779 else if (bind == STB_WEAK)
3780 flags = BSF_WEAK;
3781 else
3782 {
3783 /* Leave it up to the processor backend. */
3784 }
3785
3786 if (isym->st_shndx == SHN_UNDEF)
3787 sec = bfd_und_section_ptr;
66eb6687
AM
3788 else if (isym->st_shndx < SHN_LORESERVE
3789 || isym->st_shndx > SHN_HIRESERVE)
4ad4eba5
AM
3790 {
3791 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3792 if (sec == NULL)
3793 sec = bfd_abs_section_ptr;
529fcb95
PB
3794 else if (sec->kept_section)
3795 {
e5d08002
L
3796 /* Symbols from discarded section are undefined. We keep
3797 its visibility. */
529fcb95
PB
3798 sec = bfd_und_section_ptr;
3799 isym->st_shndx = SHN_UNDEF;
3800 }
4ad4eba5
AM
3801 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3802 value -= sec->vma;
3803 }
3804 else if (isym->st_shndx == SHN_ABS)
3805 sec = bfd_abs_section_ptr;
3806 else if (isym->st_shndx == SHN_COMMON)
3807 {
3808 sec = bfd_com_section_ptr;
3809 /* What ELF calls the size we call the value. What ELF
3810 calls the value we call the alignment. */
3811 value = isym->st_size;
3812 }
3813 else
3814 {
3815 /* Leave it up to the processor backend. */
3816 }
3817
3818 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3819 isym->st_name);
3820 if (name == NULL)
3821 goto error_free_vers;
3822
3823 if (isym->st_shndx == SHN_COMMON
6a4a0940
JJ
3824 && ELF_ST_TYPE (isym->st_info) == STT_TLS
3825 && !info->relocatable)
4ad4eba5
AM
3826 {
3827 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3828
3829 if (tcomm == NULL)
3830 {
3496cb2a
L
3831 tcomm = bfd_make_section_with_flags (abfd, ".tcommon",
3832 (SEC_ALLOC
3833 | SEC_IS_COMMON
3834 | SEC_LINKER_CREATED
3835 | SEC_THREAD_LOCAL));
3836 if (tcomm == NULL)
4ad4eba5
AM
3837 goto error_free_vers;
3838 }
3839 sec = tcomm;
3840 }
66eb6687 3841 else if (bed->elf_add_symbol_hook)
4ad4eba5 3842 {
66eb6687
AM
3843 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
3844 &sec, &value))
4ad4eba5
AM
3845 goto error_free_vers;
3846
3847 /* The hook function sets the name to NULL if this symbol
3848 should be skipped for some reason. */
3849 if (name == NULL)
3850 continue;
3851 }
3852
3853 /* Sanity check that all possibilities were handled. */
3854 if (sec == NULL)
3855 {
3856 bfd_set_error (bfd_error_bad_value);
3857 goto error_free_vers;
3858 }
3859
3860 if (bfd_is_und_section (sec)
3861 || bfd_is_com_section (sec))
3862 definition = FALSE;
3863 else
3864 definition = TRUE;
3865
3866 size_change_ok = FALSE;
66eb6687 3867 type_change_ok = bed->type_change_ok;
4ad4eba5
AM
3868 old_alignment = 0;
3869 old_bfd = NULL;
af44c138 3870 new_sec = sec;
4ad4eba5 3871
66eb6687 3872 if (is_elf_hash_table (htab))
4ad4eba5
AM
3873 {
3874 Elf_Internal_Versym iver;
3875 unsigned int vernum = 0;
3876 bfd_boolean skip;
3877
fc0e6df6 3878 if (ever == NULL)
4ad4eba5 3879 {
fc0e6df6
PB
3880 if (info->default_imported_symver)
3881 /* Use the default symbol version created earlier. */
3882 iver.vs_vers = elf_tdata (abfd)->cverdefs;
3883 else
3884 iver.vs_vers = 0;
3885 }
3886 else
3887 _bfd_elf_swap_versym_in (abfd, ever, &iver);
3888
3889 vernum = iver.vs_vers & VERSYM_VERSION;
3890
3891 /* If this is a hidden symbol, or if it is not version
3892 1, we append the version name to the symbol name.
cc86ff91
EB
3893 However, we do not modify a non-hidden absolute symbol
3894 if it is not a function, because it might be the version
3895 symbol itself. FIXME: What if it isn't? */
fc0e6df6 3896 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
3897 || (vernum > 1
3898 && (!bfd_is_abs_section (sec)
3899 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
3900 {
3901 const char *verstr;
3902 size_t namelen, verlen, newlen;
3903 char *newname, *p;
3904
3905 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 3906 {
fc0e6df6
PB
3907 if (vernum > elf_tdata (abfd)->cverdefs)
3908 verstr = NULL;
3909 else if (vernum > 1)
3910 verstr =
3911 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
3912 else
3913 verstr = "";
4ad4eba5 3914
fc0e6df6 3915 if (verstr == NULL)
4ad4eba5 3916 {
fc0e6df6
PB
3917 (*_bfd_error_handler)
3918 (_("%B: %s: invalid version %u (max %d)"),
3919 abfd, name, vernum,
3920 elf_tdata (abfd)->cverdefs);
3921 bfd_set_error (bfd_error_bad_value);
3922 goto error_free_vers;
4ad4eba5 3923 }
fc0e6df6
PB
3924 }
3925 else
3926 {
3927 /* We cannot simply test for the number of
3928 entries in the VERNEED section since the
3929 numbers for the needed versions do not start
3930 at 0. */
3931 Elf_Internal_Verneed *t;
3932
3933 verstr = NULL;
3934 for (t = elf_tdata (abfd)->verref;
3935 t != NULL;
3936 t = t->vn_nextref)
4ad4eba5 3937 {
fc0e6df6 3938 Elf_Internal_Vernaux *a;
4ad4eba5 3939
fc0e6df6
PB
3940 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3941 {
3942 if (a->vna_other == vernum)
4ad4eba5 3943 {
fc0e6df6
PB
3944 verstr = a->vna_nodename;
3945 break;
4ad4eba5 3946 }
4ad4eba5 3947 }
fc0e6df6
PB
3948 if (a != NULL)
3949 break;
3950 }
3951 if (verstr == NULL)
3952 {
3953 (*_bfd_error_handler)
3954 (_("%B: %s: invalid needed version %d"),
3955 abfd, name, vernum);
3956 bfd_set_error (bfd_error_bad_value);
3957 goto error_free_vers;
4ad4eba5 3958 }
4ad4eba5 3959 }
fc0e6df6
PB
3960
3961 namelen = strlen (name);
3962 verlen = strlen (verstr);
3963 newlen = namelen + verlen + 2;
3964 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3965 && isym->st_shndx != SHN_UNDEF)
3966 ++newlen;
3967
66eb6687 3968 newname = bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
3969 if (newname == NULL)
3970 goto error_free_vers;
3971 memcpy (newname, name, namelen);
3972 p = newname + namelen;
3973 *p++ = ELF_VER_CHR;
3974 /* If this is a defined non-hidden version symbol,
3975 we add another @ to the name. This indicates the
3976 default version of the symbol. */
3977 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3978 && isym->st_shndx != SHN_UNDEF)
3979 *p++ = ELF_VER_CHR;
3980 memcpy (p, verstr, verlen + 1);
3981
3982 name = newname;
4ad4eba5
AM
3983 }
3984
af44c138
L
3985 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec,
3986 &value, &old_alignment,
4ad4eba5
AM
3987 sym_hash, &skip, &override,
3988 &type_change_ok, &size_change_ok))
3989 goto error_free_vers;
3990
3991 if (skip)
3992 continue;
3993
3994 if (override)
3995 definition = FALSE;
3996
3997 h = *sym_hash;
3998 while (h->root.type == bfd_link_hash_indirect
3999 || h->root.type == bfd_link_hash_warning)
4000 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4001
4002 /* Remember the old alignment if this is a common symbol, so
4003 that we don't reduce the alignment later on. We can't
4004 check later, because _bfd_generic_link_add_one_symbol
4005 will set a default for the alignment which we want to
4006 override. We also remember the old bfd where the existing
4007 definition comes from. */
4008 switch (h->root.type)
4009 {
4010 default:
4011 break;
4012
4013 case bfd_link_hash_defined:
4014 case bfd_link_hash_defweak:
4015 old_bfd = h->root.u.def.section->owner;
4016 break;
4017
4018 case bfd_link_hash_common:
4019 old_bfd = h->root.u.c.p->section->owner;
4020 old_alignment = h->root.u.c.p->alignment_power;
4021 break;
4022 }
4023
4024 if (elf_tdata (abfd)->verdef != NULL
4025 && ! override
4026 && vernum > 1
4027 && definition)
4028 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4029 }
4030
4031 if (! (_bfd_generic_link_add_one_symbol
66eb6687 4032 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4ad4eba5
AM
4033 (struct bfd_link_hash_entry **) sym_hash)))
4034 goto error_free_vers;
4035
4036 h = *sym_hash;
4037 while (h->root.type == bfd_link_hash_indirect
4038 || h->root.type == bfd_link_hash_warning)
4039 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4040 *sym_hash = h;
4041
4042 new_weakdef = FALSE;
4043 if (dynamic
4044 && definition
4045 && (flags & BSF_WEAK) != 0
fcb93ecf 4046 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4047 && is_elf_hash_table (htab)
f6e332e6 4048 && h->u.weakdef == NULL)
4ad4eba5
AM
4049 {
4050 /* Keep a list of all weak defined non function symbols from
4051 a dynamic object, using the weakdef field. Later in this
4052 function we will set the weakdef field to the correct
4053 value. We only put non-function symbols from dynamic
4054 objects on this list, because that happens to be the only
4055 time we need to know the normal symbol corresponding to a
4056 weak symbol, and the information is time consuming to
4057 figure out. If the weakdef field is not already NULL,
4058 then this symbol was already defined by some previous
4059 dynamic object, and we will be using that previous
4060 definition anyhow. */
4061
f6e332e6 4062 h->u.weakdef = weaks;
4ad4eba5
AM
4063 weaks = h;
4064 new_weakdef = TRUE;
4065 }
4066
4067 /* Set the alignment of a common symbol. */
a4d8e49b 4068 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4069 && h->root.type == bfd_link_hash_common)
4070 {
4071 unsigned int align;
4072
a4d8e49b 4073 if (common)
af44c138
L
4074 align = bfd_log2 (isym->st_value);
4075 else
4076 {
4077 /* The new symbol is a common symbol in a shared object.
4078 We need to get the alignment from the section. */
4079 align = new_sec->alignment_power;
4080 }
4ad4eba5
AM
4081 if (align > old_alignment
4082 /* Permit an alignment power of zero if an alignment of one
4083 is specified and no other alignments have been specified. */
4084 || (isym->st_value == 1 && old_alignment == 0))
4085 h->root.u.c.p->alignment_power = align;
4086 else
4087 h->root.u.c.p->alignment_power = old_alignment;
4088 }
4089
66eb6687 4090 if (is_elf_hash_table (htab))
4ad4eba5 4091 {
4ad4eba5 4092 bfd_boolean dynsym;
4ad4eba5
AM
4093
4094 /* Check the alignment when a common symbol is involved. This
4095 can change when a common symbol is overridden by a normal
4096 definition or a common symbol is ignored due to the old
4097 normal definition. We need to make sure the maximum
4098 alignment is maintained. */
a4d8e49b 4099 if ((old_alignment || common)
4ad4eba5
AM
4100 && h->root.type != bfd_link_hash_common)
4101 {
4102 unsigned int common_align;
4103 unsigned int normal_align;
4104 unsigned int symbol_align;
4105 bfd *normal_bfd;
4106 bfd *common_bfd;
4107
4108 symbol_align = ffs (h->root.u.def.value) - 1;
4109 if (h->root.u.def.section->owner != NULL
4110 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4111 {
4112 normal_align = h->root.u.def.section->alignment_power;
4113 if (normal_align > symbol_align)
4114 normal_align = symbol_align;
4115 }
4116 else
4117 normal_align = symbol_align;
4118
4119 if (old_alignment)
4120 {
4121 common_align = old_alignment;
4122 common_bfd = old_bfd;
4123 normal_bfd = abfd;
4124 }
4125 else
4126 {
4127 common_align = bfd_log2 (isym->st_value);
4128 common_bfd = abfd;
4129 normal_bfd = old_bfd;
4130 }
4131
4132 if (normal_align < common_align)
d07676f8
NC
4133 {
4134 /* PR binutils/2735 */
4135 if (normal_bfd == NULL)
4136 (*_bfd_error_handler)
4137 (_("Warning: alignment %u of common symbol `%s' in %B"
4138 " is greater than the alignment (%u) of its section %A"),
4139 common_bfd, h->root.u.def.section,
4140 1 << common_align, name, 1 << normal_align);
4141 else
4142 (*_bfd_error_handler)
4143 (_("Warning: alignment %u of symbol `%s' in %B"
4144 " is smaller than %u in %B"),
4145 normal_bfd, common_bfd,
4146 1 << normal_align, name, 1 << common_align);
4147 }
4ad4eba5
AM
4148 }
4149
83ad0046
L
4150 /* Remember the symbol size if it isn't undefined. */
4151 if ((isym->st_size != 0 && isym->st_shndx != SHN_UNDEF)
4ad4eba5
AM
4152 && (definition || h->size == 0))
4153 {
83ad0046
L
4154 if (h->size != 0
4155 && h->size != isym->st_size
4156 && ! size_change_ok)
4ad4eba5 4157 (*_bfd_error_handler)
d003868e
AM
4158 (_("Warning: size of symbol `%s' changed"
4159 " from %lu in %B to %lu in %B"),
4160 old_bfd, abfd,
4ad4eba5 4161 name, (unsigned long) h->size,
d003868e 4162 (unsigned long) isym->st_size);
4ad4eba5
AM
4163
4164 h->size = isym->st_size;
4165 }
4166
4167 /* If this is a common symbol, then we always want H->SIZE
4168 to be the size of the common symbol. The code just above
4169 won't fix the size if a common symbol becomes larger. We
4170 don't warn about a size change here, because that is
fcb93ecf
PB
4171 covered by --warn-common. Allow changed between different
4172 function types. */
4ad4eba5
AM
4173 if (h->root.type == bfd_link_hash_common)
4174 h->size = h->root.u.c.size;
4175
4176 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4177 && (definition || h->type == STT_NOTYPE))
4178 {
4179 if (h->type != STT_NOTYPE
4180 && h->type != ELF_ST_TYPE (isym->st_info)
4181 && ! type_change_ok)
4182 (*_bfd_error_handler)
d003868e
AM
4183 (_("Warning: type of symbol `%s' changed"
4184 " from %d to %d in %B"),
4185 abfd, name, h->type, ELF_ST_TYPE (isym->st_info));
4ad4eba5
AM
4186
4187 h->type = ELF_ST_TYPE (isym->st_info);
4188 }
4189
4190 /* If st_other has a processor-specific meaning, specific
4191 code might be needed here. We never merge the visibility
4192 attribute with the one from a dynamic object. */
4193 if (bed->elf_backend_merge_symbol_attribute)
4194 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
4195 dynamic);
4196
b58f81ae
DJ
4197 /* If this symbol has default visibility and the user has requested
4198 we not re-export it, then mark it as hidden. */
4199 if (definition && !dynamic
4200 && (abfd->no_export
4201 || (abfd->my_archive && abfd->my_archive->no_export))
4202 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
66eb6687
AM
4203 isym->st_other = (STV_HIDDEN
4204 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
b58f81ae 4205
8992f0d7 4206 if (ELF_ST_VISIBILITY (isym->st_other) != 0 && !dynamic)
4ad4eba5
AM
4207 {
4208 unsigned char hvis, symvis, other, nvis;
4209
8992f0d7
TS
4210 /* Only merge the visibility. Leave the remainder of the
4211 st_other field to elf_backend_merge_symbol_attribute. */
4212 other = h->other & ~ELF_ST_VISIBILITY (-1);
4ad4eba5
AM
4213
4214 /* Combine visibilities, using the most constraining one. */
4215 hvis = ELF_ST_VISIBILITY (h->other);
4216 symvis = ELF_ST_VISIBILITY (isym->st_other);
4217 if (! hvis)
4218 nvis = symvis;
4219 else if (! symvis)
4220 nvis = hvis;
4221 else
4222 nvis = hvis < symvis ? hvis : symvis;
4223
4224 h->other = other | nvis;
4225 }
4226
4227 /* Set a flag in the hash table entry indicating the type of
4228 reference or definition we just found. Keep a count of
4229 the number of dynamic symbols we find. A dynamic symbol
4230 is one which is referenced or defined by both a regular
4231 object and a shared object. */
4ad4eba5
AM
4232 dynsym = FALSE;
4233 if (! dynamic)
4234 {
4235 if (! definition)
4236 {
f5385ebf 4237 h->ref_regular = 1;
4ad4eba5 4238 if (bind != STB_WEAK)
f5385ebf 4239 h->ref_regular_nonweak = 1;
4ad4eba5
AM
4240 }
4241 else
f5385ebf 4242 h->def_regular = 1;
4ad4eba5 4243 if (! info->executable
f5385ebf
AM
4244 || h->def_dynamic
4245 || h->ref_dynamic)
4ad4eba5
AM
4246 dynsym = TRUE;
4247 }
4248 else
4249 {
4250 if (! definition)
f5385ebf 4251 h->ref_dynamic = 1;
4ad4eba5 4252 else
f5385ebf
AM
4253 h->def_dynamic = 1;
4254 if (h->def_regular
4255 || h->ref_regular
f6e332e6 4256 || (h->u.weakdef != NULL
4ad4eba5 4257 && ! new_weakdef
f6e332e6 4258 && h->u.weakdef->dynindx != -1))
4ad4eba5
AM
4259 dynsym = TRUE;
4260 }
4261
92b7c7b6
L
4262 if (definition && (sec->flags & SEC_DEBUGGING))
4263 {
4264 /* We don't want to make debug symbol dynamic. */
4265 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4266 dynsym = FALSE;
4267 }
4268
4ad4eba5
AM
4269 /* Check to see if we need to add an indirect symbol for
4270 the default name. */
4271 if (definition || h->root.type == bfd_link_hash_common)
4272 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4273 &sec, &value, &dynsym,
4274 override))
4275 goto error_free_vers;
4276
4277 if (definition && !dynamic)
4278 {
4279 char *p = strchr (name, ELF_VER_CHR);
4280 if (p != NULL && p[1] != ELF_VER_CHR)
4281 {
4282 /* Queue non-default versions so that .symver x, x@FOO
4283 aliases can be checked. */
66eb6687 4284 if (!nondeflt_vers)
4ad4eba5 4285 {
66eb6687
AM
4286 amt = ((isymend - isym + 1)
4287 * sizeof (struct elf_link_hash_entry *));
4ad4eba5 4288 nondeflt_vers = bfd_malloc (amt);
14b1c01e
AM
4289 if (!nondeflt_vers)
4290 goto error_free_vers;
4ad4eba5 4291 }
66eb6687 4292 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
4293 }
4294 }
4295
4296 if (dynsym && h->dynindx == -1)
4297 {
c152c796 4298 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 4299 goto error_free_vers;
f6e332e6 4300 if (h->u.weakdef != NULL
4ad4eba5 4301 && ! new_weakdef
f6e332e6 4302 && h->u.weakdef->dynindx == -1)
4ad4eba5 4303 {
66eb6687 4304 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4ad4eba5
AM
4305 goto error_free_vers;
4306 }
4307 }
4308 else if (dynsym && h->dynindx != -1)
4309 /* If the symbol already has a dynamic index, but
4310 visibility says it should not be visible, turn it into
4311 a local symbol. */
4312 switch (ELF_ST_VISIBILITY (h->other))
4313 {
4314 case STV_INTERNAL:
4315 case STV_HIDDEN:
4316 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4317 dynsym = FALSE;
4318 break;
4319 }
4320
4321 if (!add_needed
4322 && definition
4323 && dynsym
f5385ebf 4324 && h->ref_regular)
4ad4eba5
AM
4325 {
4326 int ret;
4327 const char *soname = elf_dt_name (abfd);
4328
4329 /* A symbol from a library loaded via DT_NEEDED of some
4330 other library is referenced by a regular object.
e56f61be
L
4331 Add a DT_NEEDED entry for it. Issue an error if
4332 --no-add-needed is used. */
4333 if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4334 {
4335 (*_bfd_error_handler)
4336 (_("%s: invalid DSO for symbol `%s' definition"),
d003868e 4337 abfd, name);
e56f61be
L
4338 bfd_set_error (bfd_error_bad_value);
4339 goto error_free_vers;
4340 }
4341
a5db907e
AM
4342 elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED;
4343
4ad4eba5 4344 add_needed = TRUE;
7e9f0867 4345 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4ad4eba5
AM
4346 if (ret < 0)
4347 goto error_free_vers;
4348
4349 BFD_ASSERT (ret == 0);
4350 }
4351 }
4352 }
4353
66eb6687
AM
4354 if (extversym != NULL)
4355 {
4356 free (extversym);
4357 extversym = NULL;
4358 }
4359
4360 if (isymbuf != NULL)
4361 {
4362 free (isymbuf);
4363 isymbuf = NULL;
4364 }
4365
4366 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4367 {
4368 unsigned int i;
4369
4370 /* Restore the symbol table. */
97fed1c9
JJ
4371 if (bed->as_needed_cleanup)
4372 (*bed->as_needed_cleanup) (abfd, info);
66eb6687
AM
4373 old_hash = (char *) old_tab + tabsize;
4374 old_ent = (char *) old_hash + hashsize;
4375 sym_hash = elf_sym_hashes (abfd);
4f87808c
AM
4376 htab->root.table.table = old_table;
4377 htab->root.table.size = old_size;
4378 htab->root.table.count = old_count;
66eb6687
AM
4379 memcpy (htab->root.table.table, old_tab, tabsize);
4380 memcpy (sym_hash, old_hash, hashsize);
4381 htab->root.undefs = old_undefs;
4382 htab->root.undefs_tail = old_undefs_tail;
4383 for (i = 0; i < htab->root.table.size; i++)
4384 {
4385 struct bfd_hash_entry *p;
4386 struct elf_link_hash_entry *h;
4387
4388 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4389 {
4390 h = (struct elf_link_hash_entry *) p;
2de92251
AM
4391 if (h->root.type == bfd_link_hash_warning)
4392 h = (struct elf_link_hash_entry *) h->root.u.i.link;
66eb6687
AM
4393 if (h->dynindx >= old_dynsymcount)
4394 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
2de92251 4395
66eb6687
AM
4396 memcpy (p, old_ent, htab->root.table.entsize);
4397 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4398 h = (struct elf_link_hash_entry *) p;
4399 if (h->root.type == bfd_link_hash_warning)
4400 {
4401 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4402 old_ent = (char *) old_ent + htab->root.table.entsize;
4403 }
66eb6687
AM
4404 }
4405 }
4406
5061a885
AM
4407 /* Make a special call to the linker "notice" function to
4408 tell it that symbols added for crefs may need to be removed. */
4409 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
4410 notice_not_needed))
9af2a943 4411 goto error_free_vers;
5061a885 4412
66eb6687
AM
4413 free (old_tab);
4414 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4415 alloc_mark);
4416 if (nondeflt_vers != NULL)
4417 free (nondeflt_vers);
4418 return TRUE;
4419 }
2de92251 4420
66eb6687
AM
4421 if (old_tab != NULL)
4422 {
5061a885
AM
4423 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
4424 notice_needed))
9af2a943 4425 goto error_free_vers;
66eb6687
AM
4426 free (old_tab);
4427 old_tab = NULL;
4428 }
4429
4ad4eba5
AM
4430 /* Now that all the symbols from this input file are created, handle
4431 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4432 if (nondeflt_vers != NULL)
4433 {
4434 bfd_size_type cnt, symidx;
4435
4436 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4437 {
4438 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4439 char *shortname, *p;
4440
4441 p = strchr (h->root.root.string, ELF_VER_CHR);
4442 if (p == NULL
4443 || (h->root.type != bfd_link_hash_defined
4444 && h->root.type != bfd_link_hash_defweak))
4445 continue;
4446
4447 amt = p - h->root.root.string;
4448 shortname = bfd_malloc (amt + 1);
14b1c01e
AM
4449 if (!shortname)
4450 goto error_free_vers;
4ad4eba5
AM
4451 memcpy (shortname, h->root.root.string, amt);
4452 shortname[amt] = '\0';
4453
4454 hi = (struct elf_link_hash_entry *)
66eb6687 4455 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
4456 FALSE, FALSE, FALSE);
4457 if (hi != NULL
4458 && hi->root.type == h->root.type
4459 && hi->root.u.def.value == h->root.u.def.value
4460 && hi->root.u.def.section == h->root.u.def.section)
4461 {
4462 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4463 hi->root.type = bfd_link_hash_indirect;
4464 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 4465 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
4466 sym_hash = elf_sym_hashes (abfd);
4467 if (sym_hash)
4468 for (symidx = 0; symidx < extsymcount; ++symidx)
4469 if (sym_hash[symidx] == hi)
4470 {
4471 sym_hash[symidx] = h;
4472 break;
4473 }
4474 }
4475 free (shortname);
4476 }
4477 free (nondeflt_vers);
4478 nondeflt_vers = NULL;
4479 }
4480
4ad4eba5
AM
4481 /* Now set the weakdefs field correctly for all the weak defined
4482 symbols we found. The only way to do this is to search all the
4483 symbols. Since we only need the information for non functions in
4484 dynamic objects, that's the only time we actually put anything on
4485 the list WEAKS. We need this information so that if a regular
4486 object refers to a symbol defined weakly in a dynamic object, the
4487 real symbol in the dynamic object is also put in the dynamic
4488 symbols; we also must arrange for both symbols to point to the
4489 same memory location. We could handle the general case of symbol
4490 aliasing, but a general symbol alias can only be generated in
4491 assembler code, handling it correctly would be very time
4492 consuming, and other ELF linkers don't handle general aliasing
4493 either. */
4494 if (weaks != NULL)
4495 {
4496 struct elf_link_hash_entry **hpp;
4497 struct elf_link_hash_entry **hppend;
4498 struct elf_link_hash_entry **sorted_sym_hash;
4499 struct elf_link_hash_entry *h;
4500 size_t sym_count;
4501
4502 /* Since we have to search the whole symbol list for each weak
4503 defined symbol, search time for N weak defined symbols will be
4504 O(N^2). Binary search will cut it down to O(NlogN). */
4505 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4506 sorted_sym_hash = bfd_malloc (amt);
4507 if (sorted_sym_hash == NULL)
4508 goto error_return;
4509 sym_hash = sorted_sym_hash;
4510 hpp = elf_sym_hashes (abfd);
4511 hppend = hpp + extsymcount;
4512 sym_count = 0;
4513 for (; hpp < hppend; hpp++)
4514 {
4515 h = *hpp;
4516 if (h != NULL
4517 && h->root.type == bfd_link_hash_defined
fcb93ecf 4518 && !bed->is_function_type (h->type))
4ad4eba5
AM
4519 {
4520 *sym_hash = h;
4521 sym_hash++;
4522 sym_count++;
4523 }
4524 }
4525
4526 qsort (sorted_sym_hash, sym_count,
4527 sizeof (struct elf_link_hash_entry *),
4528 elf_sort_symbol);
4529
4530 while (weaks != NULL)
4531 {
4532 struct elf_link_hash_entry *hlook;
4533 asection *slook;
4534 bfd_vma vlook;
4535 long ilook;
4536 size_t i, j, idx;
4537
4538 hlook = weaks;
f6e332e6
AM
4539 weaks = hlook->u.weakdef;
4540 hlook->u.weakdef = NULL;
4ad4eba5
AM
4541
4542 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4543 || hlook->root.type == bfd_link_hash_defweak
4544 || hlook->root.type == bfd_link_hash_common
4545 || hlook->root.type == bfd_link_hash_indirect);
4546 slook = hlook->root.u.def.section;
4547 vlook = hlook->root.u.def.value;
4548
4549 ilook = -1;
4550 i = 0;
4551 j = sym_count;
4552 while (i < j)
4553 {
4554 bfd_signed_vma vdiff;
4555 idx = (i + j) / 2;
4556 h = sorted_sym_hash [idx];
4557 vdiff = vlook - h->root.u.def.value;
4558 if (vdiff < 0)
4559 j = idx;
4560 else if (vdiff > 0)
4561 i = idx + 1;
4562 else
4563 {
a9b881be 4564 long sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
4565 if (sdiff < 0)
4566 j = idx;
4567 else if (sdiff > 0)
4568 i = idx + 1;
4569 else
4570 {
4571 ilook = idx;
4572 break;
4573 }
4574 }
4575 }
4576
4577 /* We didn't find a value/section match. */
4578 if (ilook == -1)
4579 continue;
4580
4581 for (i = ilook; i < sym_count; i++)
4582 {
4583 h = sorted_sym_hash [i];
4584
4585 /* Stop if value or section doesn't match. */
4586 if (h->root.u.def.value != vlook
4587 || h->root.u.def.section != slook)
4588 break;
4589 else if (h != hlook)
4590 {
f6e332e6 4591 hlook->u.weakdef = h;
4ad4eba5
AM
4592
4593 /* If the weak definition is in the list of dynamic
4594 symbols, make sure the real definition is put
4595 there as well. */
4596 if (hlook->dynindx != -1 && h->dynindx == -1)
4597 {
c152c796 4598 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5
AM
4599 goto error_return;
4600 }
4601
4602 /* If the real definition is in the list of dynamic
4603 symbols, make sure the weak definition is put
4604 there as well. If we don't do this, then the
4605 dynamic loader might not merge the entries for the
4606 real definition and the weak definition. */
4607 if (h->dynindx != -1 && hlook->dynindx == -1)
4608 {
c152c796 4609 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4ad4eba5
AM
4610 goto error_return;
4611 }
4612 break;
4613 }
4614 }
4615 }
4616
4617 free (sorted_sym_hash);
4618 }
4619
66eb6687
AM
4620 if (bed->check_directives)
4621 (*bed->check_directives) (abfd, info);
85fbca6a 4622
4ad4eba5
AM
4623 /* If this object is the same format as the output object, and it is
4624 not a shared library, then let the backend look through the
4625 relocs.
4626
4627 This is required to build global offset table entries and to
4628 arrange for dynamic relocs. It is not required for the
4629 particular common case of linking non PIC code, even when linking
4630 against shared libraries, but unfortunately there is no way of
4631 knowing whether an object file has been compiled PIC or not.
4632 Looking through the relocs is not particularly time consuming.
4633 The problem is that we must either (1) keep the relocs in memory,
4634 which causes the linker to require additional runtime memory or
4635 (2) read the relocs twice from the input file, which wastes time.
4636 This would be a good case for using mmap.
4637
4638 I have no idea how to handle linking PIC code into a file of a
4639 different format. It probably can't be done. */
4ad4eba5 4640 if (! dynamic
66eb6687
AM
4641 && is_elf_hash_table (htab)
4642 && htab->root.creator == abfd->xvec
4643 && bed->check_relocs != NULL)
4ad4eba5
AM
4644 {
4645 asection *o;
4646
4647 for (o = abfd->sections; o != NULL; o = o->next)
4648 {
4649 Elf_Internal_Rela *internal_relocs;
4650 bfd_boolean ok;
4651
4652 if ((o->flags & SEC_RELOC) == 0
4653 || o->reloc_count == 0
4654 || ((info->strip == strip_all || info->strip == strip_debugger)
4655 && (o->flags & SEC_DEBUGGING) != 0)
4656 || bfd_is_abs_section (o->output_section))
4657 continue;
4658
4659 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4660 info->keep_memory);
4661 if (internal_relocs == NULL)
4662 goto error_return;
4663
66eb6687 4664 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4ad4eba5
AM
4665
4666 if (elf_section_data (o)->relocs != internal_relocs)
4667 free (internal_relocs);
4668
4669 if (! ok)
4670 goto error_return;
4671 }
4672 }
4673
4674 /* If this is a non-traditional link, try to optimize the handling
4675 of the .stab/.stabstr sections. */
4676 if (! dynamic
4677 && ! info->traditional_format
66eb6687 4678 && is_elf_hash_table (htab)
4ad4eba5
AM
4679 && (info->strip != strip_all && info->strip != strip_debugger))
4680 {
4681 asection *stabstr;
4682
4683 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4684 if (stabstr != NULL)
4685 {
4686 bfd_size_type string_offset = 0;
4687 asection *stab;
4688
4689 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 4690 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
4691 && (!stab->name[5] ||
4692 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4693 && (stab->flags & SEC_MERGE) == 0
4694 && !bfd_is_abs_section (stab->output_section))
4695 {
4696 struct bfd_elf_section_data *secdata;
4697
4698 secdata = elf_section_data (stab);
66eb6687
AM
4699 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
4700 stabstr, &secdata->sec_info,
4ad4eba5
AM
4701 &string_offset))
4702 goto error_return;
4703 if (secdata->sec_info)
4704 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4705 }
4706 }
4707 }
4708
66eb6687 4709 if (is_elf_hash_table (htab) && add_needed)
4ad4eba5
AM
4710 {
4711 /* Add this bfd to the loaded list. */
4712 struct elf_link_loaded_list *n;
4713
4714 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4715 if (n == NULL)
4716 goto error_return;
4717 n->abfd = abfd;
66eb6687
AM
4718 n->next = htab->loaded;
4719 htab->loaded = n;
4ad4eba5
AM
4720 }
4721
4722 return TRUE;
4723
4724 error_free_vers:
66eb6687
AM
4725 if (old_tab != NULL)
4726 free (old_tab);
4ad4eba5
AM
4727 if (nondeflt_vers != NULL)
4728 free (nondeflt_vers);
4729 if (extversym != NULL)
4730 free (extversym);
4731 error_free_sym:
4732 if (isymbuf != NULL)
4733 free (isymbuf);
4734 error_return:
4735 return FALSE;
4736}
4737
8387904d
AM
4738/* Return the linker hash table entry of a symbol that might be
4739 satisfied by an archive symbol. Return -1 on error. */
4740
4741struct elf_link_hash_entry *
4742_bfd_elf_archive_symbol_lookup (bfd *abfd,
4743 struct bfd_link_info *info,
4744 const char *name)
4745{
4746 struct elf_link_hash_entry *h;
4747 char *p, *copy;
4748 size_t len, first;
4749
4750 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4751 if (h != NULL)
4752 return h;
4753
4754 /* If this is a default version (the name contains @@), look up the
4755 symbol again with only one `@' as well as without the version.
4756 The effect is that references to the symbol with and without the
4757 version will be matched by the default symbol in the archive. */
4758
4759 p = strchr (name, ELF_VER_CHR);
4760 if (p == NULL || p[1] != ELF_VER_CHR)
4761 return h;
4762
4763 /* First check with only one `@'. */
4764 len = strlen (name);
4765 copy = bfd_alloc (abfd, len);
4766 if (copy == NULL)
4767 return (struct elf_link_hash_entry *) 0 - 1;
4768
4769 first = p - name + 1;
4770 memcpy (copy, name, first);
4771 memcpy (copy + first, name + first + 1, len - first);
4772
4773 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4774 if (h == NULL)
4775 {
4776 /* We also need to check references to the symbol without the
4777 version. */
4778 copy[first - 1] = '\0';
4779 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4780 FALSE, FALSE, FALSE);
4781 }
4782
4783 bfd_release (abfd, copy);
4784 return h;
4785}
4786
0ad989f9
L
4787/* Add symbols from an ELF archive file to the linker hash table. We
4788 don't use _bfd_generic_link_add_archive_symbols because of a
4789 problem which arises on UnixWare. The UnixWare libc.so is an
4790 archive which includes an entry libc.so.1 which defines a bunch of
4791 symbols. The libc.so archive also includes a number of other
4792 object files, which also define symbols, some of which are the same
4793 as those defined in libc.so.1. Correct linking requires that we
4794 consider each object file in turn, and include it if it defines any
4795 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4796 this; it looks through the list of undefined symbols, and includes
4797 any object file which defines them. When this algorithm is used on
4798 UnixWare, it winds up pulling in libc.so.1 early and defining a
4799 bunch of symbols. This means that some of the other objects in the
4800 archive are not included in the link, which is incorrect since they
4801 precede libc.so.1 in the archive.
4802
4803 Fortunately, ELF archive handling is simpler than that done by
4804 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4805 oddities. In ELF, if we find a symbol in the archive map, and the
4806 symbol is currently undefined, we know that we must pull in that
4807 object file.
4808
4809 Unfortunately, we do have to make multiple passes over the symbol
4810 table until nothing further is resolved. */
4811
4ad4eba5
AM
4812static bfd_boolean
4813elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
4814{
4815 symindex c;
4816 bfd_boolean *defined = NULL;
4817 bfd_boolean *included = NULL;
4818 carsym *symdefs;
4819 bfd_boolean loop;
4820 bfd_size_type amt;
8387904d
AM
4821 const struct elf_backend_data *bed;
4822 struct elf_link_hash_entry * (*archive_symbol_lookup)
4823 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
4824
4825 if (! bfd_has_map (abfd))
4826 {
4827 /* An empty archive is a special case. */
4828 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4829 return TRUE;
4830 bfd_set_error (bfd_error_no_armap);
4831 return FALSE;
4832 }
4833
4834 /* Keep track of all symbols we know to be already defined, and all
4835 files we know to be already included. This is to speed up the
4836 second and subsequent passes. */
4837 c = bfd_ardata (abfd)->symdef_count;
4838 if (c == 0)
4839 return TRUE;
4840 amt = c;
4841 amt *= sizeof (bfd_boolean);
4842 defined = bfd_zmalloc (amt);
4843 included = bfd_zmalloc (amt);
4844 if (defined == NULL || included == NULL)
4845 goto error_return;
4846
4847 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
4848 bed = get_elf_backend_data (abfd);
4849 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
4850
4851 do
4852 {
4853 file_ptr last;
4854 symindex i;
4855 carsym *symdef;
4856 carsym *symdefend;
4857
4858 loop = FALSE;
4859 last = -1;
4860
4861 symdef = symdefs;
4862 symdefend = symdef + c;
4863 for (i = 0; symdef < symdefend; symdef++, i++)
4864 {
4865 struct elf_link_hash_entry *h;
4866 bfd *element;
4867 struct bfd_link_hash_entry *undefs_tail;
4868 symindex mark;
4869
4870 if (defined[i] || included[i])
4871 continue;
4872 if (symdef->file_offset == last)
4873 {
4874 included[i] = TRUE;
4875 continue;
4876 }
4877
8387904d
AM
4878 h = archive_symbol_lookup (abfd, info, symdef->name);
4879 if (h == (struct elf_link_hash_entry *) 0 - 1)
4880 goto error_return;
0ad989f9
L
4881
4882 if (h == NULL)
4883 continue;
4884
4885 if (h->root.type == bfd_link_hash_common)
4886 {
4887 /* We currently have a common symbol. The archive map contains
4888 a reference to this symbol, so we may want to include it. We
4889 only want to include it however, if this archive element
4890 contains a definition of the symbol, not just another common
4891 declaration of it.
4892
4893 Unfortunately some archivers (including GNU ar) will put
4894 declarations of common symbols into their archive maps, as
4895 well as real definitions, so we cannot just go by the archive
4896 map alone. Instead we must read in the element's symbol
4897 table and check that to see what kind of symbol definition
4898 this is. */
4899 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4900 continue;
4901 }
4902 else if (h->root.type != bfd_link_hash_undefined)
4903 {
4904 if (h->root.type != bfd_link_hash_undefweak)
4905 defined[i] = TRUE;
4906 continue;
4907 }
4908
4909 /* We need to include this archive member. */
4910 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4911 if (element == NULL)
4912 goto error_return;
4913
4914 if (! bfd_check_format (element, bfd_object))
4915 goto error_return;
4916
4917 /* Doublecheck that we have not included this object
4918 already--it should be impossible, but there may be
4919 something wrong with the archive. */
4920 if (element->archive_pass != 0)
4921 {
4922 bfd_set_error (bfd_error_bad_value);
4923 goto error_return;
4924 }
4925 element->archive_pass = 1;
4926
4927 undefs_tail = info->hash->undefs_tail;
4928
4929 if (! (*info->callbacks->add_archive_element) (info, element,
4930 symdef->name))
4931 goto error_return;
4932 if (! bfd_link_add_symbols (element, info))
4933 goto error_return;
4934
4935 /* If there are any new undefined symbols, we need to make
4936 another pass through the archive in order to see whether
4937 they can be defined. FIXME: This isn't perfect, because
4938 common symbols wind up on undefs_tail and because an
4939 undefined symbol which is defined later on in this pass
4940 does not require another pass. This isn't a bug, but it
4941 does make the code less efficient than it could be. */
4942 if (undefs_tail != info->hash->undefs_tail)
4943 loop = TRUE;
4944
4945 /* Look backward to mark all symbols from this object file
4946 which we have already seen in this pass. */
4947 mark = i;
4948 do
4949 {
4950 included[mark] = TRUE;
4951 if (mark == 0)
4952 break;
4953 --mark;
4954 }
4955 while (symdefs[mark].file_offset == symdef->file_offset);
4956
4957 /* We mark subsequent symbols from this object file as we go
4958 on through the loop. */
4959 last = symdef->file_offset;
4960 }
4961 }
4962 while (loop);
4963
4964 free (defined);
4965 free (included);
4966
4967 return TRUE;
4968
4969 error_return:
4970 if (defined != NULL)
4971 free (defined);
4972 if (included != NULL)
4973 free (included);
4974 return FALSE;
4975}
4ad4eba5
AM
4976
4977/* Given an ELF BFD, add symbols to the global hash table as
4978 appropriate. */
4979
4980bfd_boolean
4981bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
4982{
4983 switch (bfd_get_format (abfd))
4984 {
4985 case bfd_object:
4986 return elf_link_add_object_symbols (abfd, info);
4987 case bfd_archive:
4988 return elf_link_add_archive_symbols (abfd, info);
4989 default:
4990 bfd_set_error (bfd_error_wrong_format);
4991 return FALSE;
4992 }
4993}
5a580b3a 4994\f
14b1c01e
AM
4995struct hash_codes_info
4996{
4997 unsigned long *hashcodes;
4998 bfd_boolean error;
4999};
5000
5a580b3a
AM
5001/* This function will be called though elf_link_hash_traverse to store
5002 all hash value of the exported symbols in an array. */
5003
5004static bfd_boolean
5005elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5006{
14b1c01e 5007 struct hash_codes_info *inf = data;
5a580b3a
AM
5008 const char *name;
5009 char *p;
5010 unsigned long ha;
5011 char *alc = NULL;
5012
5013 if (h->root.type == bfd_link_hash_warning)
5014 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5015
5016 /* Ignore indirect symbols. These are added by the versioning code. */
5017 if (h->dynindx == -1)
5018 return TRUE;
5019
5020 name = h->root.root.string;
5021 p = strchr (name, ELF_VER_CHR);
5022 if (p != NULL)
5023 {
5024 alc = bfd_malloc (p - name + 1);
14b1c01e
AM
5025 if (alc == NULL)
5026 {
5027 inf->error = TRUE;
5028 return FALSE;
5029 }
5a580b3a
AM
5030 memcpy (alc, name, p - name);
5031 alc[p - name] = '\0';
5032 name = alc;
5033 }
5034
5035 /* Compute the hash value. */
5036 ha = bfd_elf_hash (name);
5037
5038 /* Store the found hash value in the array given as the argument. */
14b1c01e 5039 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5040
5041 /* And store it in the struct so that we can put it in the hash table
5042 later. */
f6e332e6 5043 h->u.elf_hash_value = ha;
5a580b3a
AM
5044
5045 if (alc != NULL)
5046 free (alc);
5047
5048 return TRUE;
5049}
5050
fdc90cb4
JJ
5051struct collect_gnu_hash_codes
5052{
5053 bfd *output_bfd;
5054 const struct elf_backend_data *bed;
5055 unsigned long int nsyms;
5056 unsigned long int maskbits;
5057 unsigned long int *hashcodes;
5058 unsigned long int *hashval;
5059 unsigned long int *indx;
5060 unsigned long int *counts;
5061 bfd_vma *bitmask;
5062 bfd_byte *contents;
5063 long int min_dynindx;
5064 unsigned long int bucketcount;
5065 unsigned long int symindx;
5066 long int local_indx;
5067 long int shift1, shift2;
5068 unsigned long int mask;
14b1c01e 5069 bfd_boolean error;
fdc90cb4
JJ
5070};
5071
5072/* This function will be called though elf_link_hash_traverse to store
5073 all hash value of the exported symbols in an array. */
5074
5075static bfd_boolean
5076elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5077{
5078 struct collect_gnu_hash_codes *s = data;
5079 const char *name;
5080 char *p;
5081 unsigned long ha;
5082 char *alc = NULL;
5083
5084 if (h->root.type == bfd_link_hash_warning)
5085 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5086
5087 /* Ignore indirect symbols. These are added by the versioning code. */
5088 if (h->dynindx == -1)
5089 return TRUE;
5090
5091 /* Ignore also local symbols and undefined symbols. */
5092 if (! (*s->bed->elf_hash_symbol) (h))
5093 return TRUE;
5094
5095 name = h->root.root.string;
5096 p = strchr (name, ELF_VER_CHR);
5097 if (p != NULL)
5098 {
5099 alc = bfd_malloc (p - name + 1);
14b1c01e
AM
5100 if (alc == NULL)
5101 {
5102 s->error = TRUE;
5103 return FALSE;
5104 }
fdc90cb4
JJ
5105 memcpy (alc, name, p - name);
5106 alc[p - name] = '\0';
5107 name = alc;
5108 }
5109
5110 /* Compute the hash value. */
5111 ha = bfd_elf_gnu_hash (name);
5112
5113 /* Store the found hash value in the array for compute_bucket_count,
5114 and also for .dynsym reordering purposes. */
5115 s->hashcodes[s->nsyms] = ha;
5116 s->hashval[h->dynindx] = ha;
5117 ++s->nsyms;
5118 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5119 s->min_dynindx = h->dynindx;
5120
5121 if (alc != NULL)
5122 free (alc);
5123
5124 return TRUE;
5125}
5126
5127/* This function will be called though elf_link_hash_traverse to do
5128 final dynaminc symbol renumbering. */
5129
5130static bfd_boolean
5131elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5132{
5133 struct collect_gnu_hash_codes *s = data;
5134 unsigned long int bucket;
5135 unsigned long int val;
5136
5137 if (h->root.type == bfd_link_hash_warning)
5138 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5139
5140 /* Ignore indirect symbols. */
5141 if (h->dynindx == -1)
5142 return TRUE;
5143
5144 /* Ignore also local symbols and undefined symbols. */
5145 if (! (*s->bed->elf_hash_symbol) (h))
5146 {
5147 if (h->dynindx >= s->min_dynindx)
5148 h->dynindx = s->local_indx++;
5149 return TRUE;
5150 }
5151
5152 bucket = s->hashval[h->dynindx] % s->bucketcount;
5153 val = (s->hashval[h->dynindx] >> s->shift1)
5154 & ((s->maskbits >> s->shift1) - 1);
5155 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5156 s->bitmask[val]
5157 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5158 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5159 if (s->counts[bucket] == 1)
5160 /* Last element terminates the chain. */
5161 val |= 1;
5162 bfd_put_32 (s->output_bfd, val,
5163 s->contents + (s->indx[bucket] - s->symindx) * 4);
5164 --s->counts[bucket];
5165 h->dynindx = s->indx[bucket]++;
5166 return TRUE;
5167}
5168
5169/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5170
5171bfd_boolean
5172_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5173{
5174 return !(h->forced_local
5175 || h->root.type == bfd_link_hash_undefined
5176 || h->root.type == bfd_link_hash_undefweak
5177 || ((h->root.type == bfd_link_hash_defined
5178 || h->root.type == bfd_link_hash_defweak)
5179 && h->root.u.def.section->output_section == NULL));
5180}
5181
5a580b3a
AM
5182/* Array used to determine the number of hash table buckets to use
5183 based on the number of symbols there are. If there are fewer than
5184 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5185 fewer than 37 we use 17 buckets, and so forth. We never use more
5186 than 32771 buckets. */
5187
5188static const size_t elf_buckets[] =
5189{
5190 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5191 16411, 32771, 0
5192};
5193
5194/* Compute bucket count for hashing table. We do not use a static set
5195 of possible tables sizes anymore. Instead we determine for all
5196 possible reasonable sizes of the table the outcome (i.e., the
5197 number of collisions etc) and choose the best solution. The
5198 weighting functions are not too simple to allow the table to grow
5199 without bounds. Instead one of the weighting factors is the size.
5200 Therefore the result is always a good payoff between few collisions
5201 (= short chain lengths) and table size. */
5202static size_t
fdc90cb4
JJ
5203compute_bucket_count (struct bfd_link_info *info, unsigned long int *hashcodes,
5204 unsigned long int nsyms, int gnu_hash)
5a580b3a
AM
5205{
5206 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5207 size_t best_size = 0;
5a580b3a
AM
5208 unsigned long int i;
5209 bfd_size_type amt;
5210
5a580b3a
AM
5211 /* We have a problem here. The following code to optimize the table
5212 size requires an integer type with more the 32 bits. If
5213 BFD_HOST_U_64_BIT is set we know about such a type. */
5214#ifdef BFD_HOST_U_64_BIT
5215 if (info->optimize)
5216 {
5a580b3a
AM
5217 size_t minsize;
5218 size_t maxsize;
5219 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a
AM
5220 bfd *dynobj = elf_hash_table (info)->dynobj;
5221 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 5222 unsigned long int *counts;
5a580b3a
AM
5223
5224 /* Possible optimization parameters: if we have NSYMS symbols we say
5225 that the hashing table must at least have NSYMS/4 and at most
5226 2*NSYMS buckets. */
5227 minsize = nsyms / 4;
5228 if (minsize == 0)
5229 minsize = 1;
5230 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
5231 if (gnu_hash)
5232 {
5233 if (minsize < 2)
5234 minsize = 2;
5235 if ((best_size & 31) == 0)
5236 ++best_size;
5237 }
5a580b3a
AM
5238
5239 /* Create array where we count the collisions in. We must use bfd_malloc
5240 since the size could be large. */
5241 amt = maxsize;
5242 amt *= sizeof (unsigned long int);
5243 counts = bfd_malloc (amt);
5244 if (counts == NULL)
fdc90cb4 5245 return 0;
5a580b3a
AM
5246
5247 /* Compute the "optimal" size for the hash table. The criteria is a
5248 minimal chain length. The minor criteria is (of course) the size
5249 of the table. */
5250 for (i = minsize; i < maxsize; ++i)
5251 {
5252 /* Walk through the array of hashcodes and count the collisions. */
5253 BFD_HOST_U_64_BIT max;
5254 unsigned long int j;
5255 unsigned long int fact;
5256
fdc90cb4
JJ
5257 if (gnu_hash && (i & 31) == 0)
5258 continue;
5259
5a580b3a
AM
5260 memset (counts, '\0', i * sizeof (unsigned long int));
5261
5262 /* Determine how often each hash bucket is used. */
5263 for (j = 0; j < nsyms; ++j)
5264 ++counts[hashcodes[j] % i];
5265
5266 /* For the weight function we need some information about the
5267 pagesize on the target. This is information need not be 100%
5268 accurate. Since this information is not available (so far) we
5269 define it here to a reasonable default value. If it is crucial
5270 to have a better value some day simply define this value. */
5271# ifndef BFD_TARGET_PAGESIZE
5272# define BFD_TARGET_PAGESIZE (4096)
5273# endif
5274
fdc90cb4
JJ
5275 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5276 and the chains. */
5277 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
5278
5279# if 1
5280 /* Variant 1: optimize for short chains. We add the squares
5281 of all the chain lengths (which favors many small chain
5282 over a few long chains). */
5283 for (j = 0; j < i; ++j)
5284 max += counts[j] * counts[j];
5285
5286 /* This adds penalties for the overall size of the table. */
fdc90cb4 5287 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5288 max *= fact * fact;
5289# else
5290 /* Variant 2: Optimize a lot more for small table. Here we
5291 also add squares of the size but we also add penalties for
5292 empty slots (the +1 term). */
5293 for (j = 0; j < i; ++j)
5294 max += (1 + counts[j]) * (1 + counts[j]);
5295
5296 /* The overall size of the table is considered, but not as
5297 strong as in variant 1, where it is squared. */
fdc90cb4 5298 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
5299 max *= fact;
5300# endif
5301
5302 /* Compare with current best results. */
5303 if (max < best_chlen)
5304 {
5305 best_chlen = max;
5306 best_size = i;
5307 }
5308 }
5309
5310 free (counts);
5311 }
5312 else
5313#endif /* defined (BFD_HOST_U_64_BIT) */
5314 {
5315 /* This is the fallback solution if no 64bit type is available or if we
5316 are not supposed to spend much time on optimizations. We select the
5317 bucket count using a fixed set of numbers. */
5318 for (i = 0; elf_buckets[i] != 0; i++)
5319 {
5320 best_size = elf_buckets[i];
fdc90cb4 5321 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
5322 break;
5323 }
fdc90cb4
JJ
5324 if (gnu_hash && best_size < 2)
5325 best_size = 2;
5a580b3a
AM
5326 }
5327
5a580b3a
AM
5328 return best_size;
5329}
5330
5331/* Set up the sizes and contents of the ELF dynamic sections. This is
5332 called by the ELF linker emulation before_allocation routine. We
5333 must set the sizes of the sections before the linker sets the
5334 addresses of the various sections. */
5335
5336bfd_boolean
5337bfd_elf_size_dynamic_sections (bfd *output_bfd,
5338 const char *soname,
5339 const char *rpath,
5340 const char *filter_shlib,
5341 const char * const *auxiliary_filters,
5342 struct bfd_link_info *info,
5343 asection **sinterpptr,
5344 struct bfd_elf_version_tree *verdefs)
5345{
5346 bfd_size_type soname_indx;
5347 bfd *dynobj;
5348 const struct elf_backend_data *bed;
5349 struct elf_assign_sym_version_info asvinfo;
5350
5351 *sinterpptr = NULL;
5352
5353 soname_indx = (bfd_size_type) -1;
5354
5355 if (!is_elf_hash_table (info->hash))
5356 return TRUE;
5357
6bfdb61b 5358 bed = get_elf_backend_data (output_bfd);
5a580b3a
AM
5359 if (info->execstack)
5360 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
5361 else if (info->noexecstack)
5362 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
5363 else
5364 {
5365 bfd *inputobj;
5366 asection *notesec = NULL;
5367 int exec = 0;
5368
5369 for (inputobj = info->input_bfds;
5370 inputobj;
5371 inputobj = inputobj->link_next)
5372 {
5373 asection *s;
5374
d457dcf6 5375 if (inputobj->flags & (DYNAMIC | BFD_LINKER_CREATED))
5a580b3a
AM
5376 continue;
5377 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5378 if (s)
5379 {
5380 if (s->flags & SEC_CODE)
5381 exec = PF_X;
5382 notesec = s;
5383 }
6bfdb61b 5384 else if (bed->default_execstack)
5a580b3a
AM
5385 exec = PF_X;
5386 }
5387 if (notesec)
5388 {
5389 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
5390 if (exec && info->relocatable
5391 && notesec->output_section != bfd_abs_section_ptr)
5392 notesec->output_section->flags |= SEC_CODE;
5393 }
5394 }
5395
5396 /* Any syms created from now on start with -1 in
5397 got.refcount/offset and plt.refcount/offset. */
a6aa5195
AM
5398 elf_hash_table (info)->init_got_refcount
5399 = elf_hash_table (info)->init_got_offset;
5400 elf_hash_table (info)->init_plt_refcount
5401 = elf_hash_table (info)->init_plt_offset;
5a580b3a
AM
5402
5403 /* The backend may have to create some sections regardless of whether
5404 we're dynamic or not. */
5a580b3a
AM
5405 if (bed->elf_backend_always_size_sections
5406 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5407 return FALSE;
5408
eb3d5f3b
JB
5409 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
5410 return FALSE;
5411
5a580b3a
AM
5412 dynobj = elf_hash_table (info)->dynobj;
5413
5414 /* If there were no dynamic objects in the link, there is nothing to
5415 do here. */
5416 if (dynobj == NULL)
5417 return TRUE;
5418
5a580b3a
AM
5419 if (elf_hash_table (info)->dynamic_sections_created)
5420 {
5421 struct elf_info_failed eif;
5422 struct elf_link_hash_entry *h;
5423 asection *dynstr;
5424 struct bfd_elf_version_tree *t;
5425 struct bfd_elf_version_expr *d;
046183de 5426 asection *s;
5a580b3a
AM
5427 bfd_boolean all_defined;
5428
5429 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
5430 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
5431
5432 if (soname != NULL)
5433 {
5434 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5435 soname, TRUE);
5436 if (soname_indx == (bfd_size_type) -1
5437 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5438 return FALSE;
5439 }
5440
5441 if (info->symbolic)
5442 {
5443 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5444 return FALSE;
5445 info->flags |= DF_SYMBOLIC;
5446 }
5447
5448 if (rpath != NULL)
5449 {
5450 bfd_size_type indx;
5451
5452 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5453 TRUE);
5454 if (indx == (bfd_size_type) -1
5455 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
5456 return FALSE;
5457
5458 if (info->new_dtags)
5459 {
5460 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
5461 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
5462 return FALSE;
5463 }
5464 }
5465
5466 if (filter_shlib != NULL)
5467 {
5468 bfd_size_type indx;
5469
5470 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5471 filter_shlib, TRUE);
5472 if (indx == (bfd_size_type) -1
5473 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5474 return FALSE;
5475 }
5476
5477 if (auxiliary_filters != NULL)
5478 {
5479 const char * const *p;
5480
5481 for (p = auxiliary_filters; *p != NULL; p++)
5482 {
5483 bfd_size_type indx;
5484
5485 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5486 *p, TRUE);
5487 if (indx == (bfd_size_type) -1
5488 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5489 return FALSE;
5490 }
5491 }
5492
5493 eif.info = info;
5494 eif.verdefs = verdefs;
5495 eif.failed = FALSE;
5496
5497 /* If we are supposed to export all symbols into the dynamic symbol
5498 table (this is not the normal case), then do so. */
55255dae
L
5499 if (info->export_dynamic
5500 || (info->executable && info->dynamic))
5a580b3a
AM
5501 {
5502 elf_link_hash_traverse (elf_hash_table (info),
5503 _bfd_elf_export_symbol,
5504 &eif);
5505 if (eif.failed)
5506 return FALSE;
5507 }
5508
5509 /* Make all global versions with definition. */
5510 for (t = verdefs; t != NULL; t = t->next)
5511 for (d = t->globals.list; d != NULL; d = d->next)
5512 if (!d->symver && d->symbol)
5513 {
5514 const char *verstr, *name;
5515 size_t namelen, verlen, newlen;
5516 char *newname, *p;
5517 struct elf_link_hash_entry *newh;
5518
5519 name = d->symbol;
5520 namelen = strlen (name);
5521 verstr = t->name;
5522 verlen = strlen (verstr);
5523 newlen = namelen + verlen + 3;
5524
5525 newname = bfd_malloc (newlen);
5526 if (newname == NULL)
5527 return FALSE;
5528 memcpy (newname, name, namelen);
5529
5530 /* Check the hidden versioned definition. */
5531 p = newname + namelen;
5532 *p++ = ELF_VER_CHR;
5533 memcpy (p, verstr, verlen + 1);
5534 newh = elf_link_hash_lookup (elf_hash_table (info),
5535 newname, FALSE, FALSE,
5536 FALSE);
5537 if (newh == NULL
5538 || (newh->root.type != bfd_link_hash_defined
5539 && newh->root.type != bfd_link_hash_defweak))
5540 {
5541 /* Check the default versioned definition. */
5542 *p++ = ELF_VER_CHR;
5543 memcpy (p, verstr, verlen + 1);
5544 newh = elf_link_hash_lookup (elf_hash_table (info),
5545 newname, FALSE, FALSE,
5546 FALSE);
5547 }
5548 free (newname);
5549
5550 /* Mark this version if there is a definition and it is
5551 not defined in a shared object. */
5552 if (newh != NULL
f5385ebf 5553 && !newh->def_dynamic
5a580b3a
AM
5554 && (newh->root.type == bfd_link_hash_defined
5555 || newh->root.type == bfd_link_hash_defweak))
5556 d->symver = 1;
5557 }
5558
5559 /* Attach all the symbols to their version information. */
5560 asvinfo.output_bfd = output_bfd;
5561 asvinfo.info = info;
5562 asvinfo.verdefs = verdefs;
5563 asvinfo.failed = FALSE;
5564
5565 elf_link_hash_traverse (elf_hash_table (info),
5566 _bfd_elf_link_assign_sym_version,
5567 &asvinfo);
5568 if (asvinfo.failed)
5569 return FALSE;
5570
5571 if (!info->allow_undefined_version)
5572 {
5573 /* Check if all global versions have a definition. */
5574 all_defined = TRUE;
5575 for (t = verdefs; t != NULL; t = t->next)
5576 for (d = t->globals.list; d != NULL; d = d->next)
5577 if (!d->symver && !d->script)
5578 {
5579 (*_bfd_error_handler)
5580 (_("%s: undefined version: %s"),
5581 d->pattern, t->name);
5582 all_defined = FALSE;
5583 }
5584
5585 if (!all_defined)
5586 {
5587 bfd_set_error (bfd_error_bad_value);
5588 return FALSE;
5589 }
5590 }
5591
5592 /* Find all symbols which were defined in a dynamic object and make
5593 the backend pick a reasonable value for them. */
5594 elf_link_hash_traverse (elf_hash_table (info),
5595 _bfd_elf_adjust_dynamic_symbol,
5596 &eif);
5597 if (eif.failed)
5598 return FALSE;
5599
5600 /* Add some entries to the .dynamic section. We fill in some of the
ee75fd95 5601 values later, in bfd_elf_final_link, but we must add the entries
5a580b3a
AM
5602 now so that we know the final size of the .dynamic section. */
5603
5604 /* If there are initialization and/or finalization functions to
5605 call then add the corresponding DT_INIT/DT_FINI entries. */
5606 h = (info->init_function
5607 ? elf_link_hash_lookup (elf_hash_table (info),
5608 info->init_function, FALSE,
5609 FALSE, FALSE)
5610 : NULL);
5611 if (h != NULL
f5385ebf
AM
5612 && (h->ref_regular
5613 || h->def_regular))
5a580b3a
AM
5614 {
5615 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5616 return FALSE;
5617 }
5618 h = (info->fini_function
5619 ? elf_link_hash_lookup (elf_hash_table (info),
5620 info->fini_function, FALSE,
5621 FALSE, FALSE)
5622 : NULL);
5623 if (h != NULL
f5385ebf
AM
5624 && (h->ref_regular
5625 || h->def_regular))
5a580b3a
AM
5626 {
5627 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5628 return FALSE;
5629 }
5630
046183de
AM
5631 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5632 if (s != NULL && s->linker_has_input)
5a580b3a
AM
5633 {
5634 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5635 if (! info->executable)
5636 {
5637 bfd *sub;
5638 asection *o;
5639
5640 for (sub = info->input_bfds; sub != NULL;
5641 sub = sub->link_next)
3fcd97f1
JJ
5642 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
5643 for (o = sub->sections; o != NULL; o = o->next)
5644 if (elf_section_data (o)->this_hdr.sh_type
5645 == SHT_PREINIT_ARRAY)
5646 {
5647 (*_bfd_error_handler)
5648 (_("%B: .preinit_array section is not allowed in DSO"),
5649 sub);
5650 break;
5651 }
5a580b3a
AM
5652
5653 bfd_set_error (bfd_error_nonrepresentable_section);
5654 return FALSE;
5655 }
5656
5657 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5658 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5659 return FALSE;
5660 }
046183de
AM
5661 s = bfd_get_section_by_name (output_bfd, ".init_array");
5662 if (s != NULL && s->linker_has_input)
5a580b3a
AM
5663 {
5664 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5665 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5666 return FALSE;
5667 }
046183de
AM
5668 s = bfd_get_section_by_name (output_bfd, ".fini_array");
5669 if (s != NULL && s->linker_has_input)
5a580b3a
AM
5670 {
5671 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5672 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5673 return FALSE;
5674 }
5675
5676 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5677 /* If .dynstr is excluded from the link, we don't want any of
5678 these tags. Strictly, we should be checking each section
5679 individually; This quick check covers for the case where
5680 someone does a /DISCARD/ : { *(*) }. */
5681 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5682 {
5683 bfd_size_type strsize;
5684
5685 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
fdc90cb4
JJ
5686 if ((info->emit_hash
5687 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
5688 || (info->emit_gnu_hash
5689 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5a580b3a
AM
5690 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5691 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5692 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5693 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5694 bed->s->sizeof_sym))
5695 return FALSE;
5696 }
5697 }
5698
5699 /* The backend must work out the sizes of all the other dynamic
5700 sections. */
5701 if (bed->elf_backend_size_dynamic_sections
5702 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5703 return FALSE;
5704
5705 if (elf_hash_table (info)->dynamic_sections_created)
5706 {
554220db 5707 unsigned long section_sym_count;
5a580b3a 5708 asection *s;
5a580b3a
AM
5709
5710 /* Set up the version definition section. */
5711 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5712 BFD_ASSERT (s != NULL);
5713
5714 /* We may have created additional version definitions if we are
5715 just linking a regular application. */
5716 verdefs = asvinfo.verdefs;
5717
5718 /* Skip anonymous version tag. */
5719 if (verdefs != NULL && verdefs->vernum == 0)
5720 verdefs = verdefs->next;
5721
3e3b46e5 5722 if (verdefs == NULL && !info->create_default_symver)
8423293d 5723 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
5724 else
5725 {
5726 unsigned int cdefs;
5727 bfd_size_type size;
5728 struct bfd_elf_version_tree *t;
5729 bfd_byte *p;
5730 Elf_Internal_Verdef def;
5731 Elf_Internal_Verdaux defaux;
3e3b46e5
PB
5732 struct bfd_link_hash_entry *bh;
5733 struct elf_link_hash_entry *h;
5734 const char *name;
5a580b3a
AM
5735
5736 cdefs = 0;
5737 size = 0;
5738
5739 /* Make space for the base version. */
5740 size += sizeof (Elf_External_Verdef);
5741 size += sizeof (Elf_External_Verdaux);
5742 ++cdefs;
5743
3e3b46e5
PB
5744 /* Make space for the default version. */
5745 if (info->create_default_symver)
5746 {
5747 size += sizeof (Elf_External_Verdef);
5748 ++cdefs;
5749 }
5750
5a580b3a
AM
5751 for (t = verdefs; t != NULL; t = t->next)
5752 {
5753 struct bfd_elf_version_deps *n;
5754
5755 size += sizeof (Elf_External_Verdef);
5756 size += sizeof (Elf_External_Verdaux);
5757 ++cdefs;
5758
5759 for (n = t->deps; n != NULL; n = n->next)
5760 size += sizeof (Elf_External_Verdaux);
5761 }
5762
eea6121a
AM
5763 s->size = size;
5764 s->contents = bfd_alloc (output_bfd, s->size);
5765 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
5766 return FALSE;
5767
5768 /* Fill in the version definition section. */
5769
5770 p = s->contents;
5771
5772 def.vd_version = VER_DEF_CURRENT;
5773 def.vd_flags = VER_FLG_BASE;
5774 def.vd_ndx = 1;
5775 def.vd_cnt = 1;
3e3b46e5
PB
5776 if (info->create_default_symver)
5777 {
5778 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5779 def.vd_next = sizeof (Elf_External_Verdef);
5780 }
5781 else
5782 {
5783 def.vd_aux = sizeof (Elf_External_Verdef);
5784 def.vd_next = (sizeof (Elf_External_Verdef)
5785 + sizeof (Elf_External_Verdaux));
5786 }
5a580b3a
AM
5787
5788 if (soname_indx != (bfd_size_type) -1)
5789 {
5790 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5791 soname_indx);
5792 def.vd_hash = bfd_elf_hash (soname);
5793 defaux.vda_name = soname_indx;
3e3b46e5 5794 name = soname;
5a580b3a
AM
5795 }
5796 else
5797 {
5a580b3a
AM
5798 bfd_size_type indx;
5799
06084812 5800 name = lbasename (output_bfd->filename);
5a580b3a
AM
5801 def.vd_hash = bfd_elf_hash (name);
5802 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5803 name, FALSE);
5804 if (indx == (bfd_size_type) -1)
5805 return FALSE;
5806 defaux.vda_name = indx;
5807 }
5808 defaux.vda_next = 0;
5809
5810 _bfd_elf_swap_verdef_out (output_bfd, &def,
5811 (Elf_External_Verdef *) p);
5812 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
5813 if (info->create_default_symver)
5814 {
5815 /* Add a symbol representing this version. */
5816 bh = NULL;
5817 if (! (_bfd_generic_link_add_one_symbol
5818 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
5819 0, NULL, FALSE,
5820 get_elf_backend_data (dynobj)->collect, &bh)))
5821 return FALSE;
5822 h = (struct elf_link_hash_entry *) bh;
5823 h->non_elf = 0;
5824 h->def_regular = 1;
5825 h->type = STT_OBJECT;
5826 h->verinfo.vertree = NULL;
5827
5828 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5829 return FALSE;
5830
5831 /* Create a duplicate of the base version with the same
5832 aux block, but different flags. */
5833 def.vd_flags = 0;
5834 def.vd_ndx = 2;
5835 def.vd_aux = sizeof (Elf_External_Verdef);
5836 if (verdefs)
5837 def.vd_next = (sizeof (Elf_External_Verdef)
5838 + sizeof (Elf_External_Verdaux));
5839 else
5840 def.vd_next = 0;
5841 _bfd_elf_swap_verdef_out (output_bfd, &def,
5842 (Elf_External_Verdef *) p);
5843 p += sizeof (Elf_External_Verdef);
5844 }
5a580b3a
AM
5845 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5846 (Elf_External_Verdaux *) p);
5847 p += sizeof (Elf_External_Verdaux);
5848
5849 for (t = verdefs; t != NULL; t = t->next)
5850 {
5851 unsigned int cdeps;
5852 struct bfd_elf_version_deps *n;
5a580b3a
AM
5853
5854 cdeps = 0;
5855 for (n = t->deps; n != NULL; n = n->next)
5856 ++cdeps;
5857
5858 /* Add a symbol representing this version. */
5859 bh = NULL;
5860 if (! (_bfd_generic_link_add_one_symbol
5861 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5862 0, NULL, FALSE,
5863 get_elf_backend_data (dynobj)->collect, &bh)))
5864 return FALSE;
5865 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
5866 h->non_elf = 0;
5867 h->def_regular = 1;
5a580b3a
AM
5868 h->type = STT_OBJECT;
5869 h->verinfo.vertree = t;
5870
c152c796 5871 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
5872 return FALSE;
5873
5874 def.vd_version = VER_DEF_CURRENT;
5875 def.vd_flags = 0;
5876 if (t->globals.list == NULL
5877 && t->locals.list == NULL
5878 && ! t->used)
5879 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 5880 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
5881 def.vd_cnt = cdeps + 1;
5882 def.vd_hash = bfd_elf_hash (t->name);
5883 def.vd_aux = sizeof (Elf_External_Verdef);
5884 def.vd_next = 0;
5885 if (t->next != NULL)
5886 def.vd_next = (sizeof (Elf_External_Verdef)
5887 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5888
5889 _bfd_elf_swap_verdef_out (output_bfd, &def,
5890 (Elf_External_Verdef *) p);
5891 p += sizeof (Elf_External_Verdef);
5892
5893 defaux.vda_name = h->dynstr_index;
5894 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5895 h->dynstr_index);
5896 defaux.vda_next = 0;
5897 if (t->deps != NULL)
5898 defaux.vda_next = sizeof (Elf_External_Verdaux);
5899 t->name_indx = defaux.vda_name;
5900
5901 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5902 (Elf_External_Verdaux *) p);
5903 p += sizeof (Elf_External_Verdaux);
5904
5905 for (n = t->deps; n != NULL; n = n->next)
5906 {
5907 if (n->version_needed == NULL)
5908 {
5909 /* This can happen if there was an error in the
5910 version script. */
5911 defaux.vda_name = 0;
5912 }
5913 else
5914 {
5915 defaux.vda_name = n->version_needed->name_indx;
5916 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5917 defaux.vda_name);
5918 }
5919 if (n->next == NULL)
5920 defaux.vda_next = 0;
5921 else
5922 defaux.vda_next = sizeof (Elf_External_Verdaux);
5923
5924 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5925 (Elf_External_Verdaux *) p);
5926 p += sizeof (Elf_External_Verdaux);
5927 }
5928 }
5929
5930 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
5931 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
5932 return FALSE;
5933
5934 elf_tdata (output_bfd)->cverdefs = cdefs;
5935 }
5936
5937 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
5938 {
5939 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
5940 return FALSE;
5941 }
5942 else if (info->flags & DF_BIND_NOW)
5943 {
5944 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
5945 return FALSE;
5946 }
5947
5948 if (info->flags_1)
5949 {
5950 if (info->executable)
5951 info->flags_1 &= ~ (DF_1_INITFIRST
5952 | DF_1_NODELETE
5953 | DF_1_NOOPEN);
5954 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
5955 return FALSE;
5956 }
5957
5958 /* Work out the size of the version reference section. */
5959
5960 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
5961 BFD_ASSERT (s != NULL);
5962 {
5963 struct elf_find_verdep_info sinfo;
5964
5965 sinfo.output_bfd = output_bfd;
5966 sinfo.info = info;
5967 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
5968 if (sinfo.vers == 0)
5969 sinfo.vers = 1;
5970 sinfo.failed = FALSE;
5971
5972 elf_link_hash_traverse (elf_hash_table (info),
5973 _bfd_elf_link_find_version_dependencies,
5974 &sinfo);
14b1c01e
AM
5975 if (sinfo.failed)
5976 return FALSE;
5a580b3a
AM
5977
5978 if (elf_tdata (output_bfd)->verref == NULL)
8423293d 5979 s->flags |= SEC_EXCLUDE;
5a580b3a
AM
5980 else
5981 {
5982 Elf_Internal_Verneed *t;
5983 unsigned int size;
5984 unsigned int crefs;
5985 bfd_byte *p;
5986
5987 /* Build the version definition section. */
5988 size = 0;
5989 crefs = 0;
5990 for (t = elf_tdata (output_bfd)->verref;
5991 t != NULL;
5992 t = t->vn_nextref)
5993 {
5994 Elf_Internal_Vernaux *a;
5995
5996 size += sizeof (Elf_External_Verneed);
5997 ++crefs;
5998 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5999 size += sizeof (Elf_External_Vernaux);
6000 }
6001
eea6121a
AM
6002 s->size = size;
6003 s->contents = bfd_alloc (output_bfd, s->size);
5a580b3a
AM
6004 if (s->contents == NULL)
6005 return FALSE;
6006
6007 p = s->contents;
6008 for (t = elf_tdata (output_bfd)->verref;
6009 t != NULL;
6010 t = t->vn_nextref)
6011 {
6012 unsigned int caux;
6013 Elf_Internal_Vernaux *a;
6014 bfd_size_type indx;
6015
6016 caux = 0;
6017 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6018 ++caux;
6019
6020 t->vn_version = VER_NEED_CURRENT;
6021 t->vn_cnt = caux;
6022 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6023 elf_dt_name (t->vn_bfd) != NULL
6024 ? elf_dt_name (t->vn_bfd)
06084812 6025 : lbasename (t->vn_bfd->filename),
5a580b3a
AM
6026 FALSE);
6027 if (indx == (bfd_size_type) -1)
6028 return FALSE;
6029 t->vn_file = indx;
6030 t->vn_aux = sizeof (Elf_External_Verneed);
6031 if (t->vn_nextref == NULL)
6032 t->vn_next = 0;
6033 else
6034 t->vn_next = (sizeof (Elf_External_Verneed)
6035 + caux * sizeof (Elf_External_Vernaux));
6036
6037 _bfd_elf_swap_verneed_out (output_bfd, t,
6038 (Elf_External_Verneed *) p);
6039 p += sizeof (Elf_External_Verneed);
6040
6041 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6042 {
6043 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6044 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6045 a->vna_nodename, FALSE);
6046 if (indx == (bfd_size_type) -1)
6047 return FALSE;
6048 a->vna_name = indx;
6049 if (a->vna_nextptr == NULL)
6050 a->vna_next = 0;
6051 else
6052 a->vna_next = sizeof (Elf_External_Vernaux);
6053
6054 _bfd_elf_swap_vernaux_out (output_bfd, a,
6055 (Elf_External_Vernaux *) p);
6056 p += sizeof (Elf_External_Vernaux);
6057 }
6058 }
6059
6060 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6061 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6062 return FALSE;
6063
6064 elf_tdata (output_bfd)->cverrefs = crefs;
6065 }
6066 }
6067
8423293d
AM
6068 if ((elf_tdata (output_bfd)->cverrefs == 0
6069 && elf_tdata (output_bfd)->cverdefs == 0)
6070 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6071 &section_sym_count) == 0)
6072 {
6073 s = bfd_get_section_by_name (dynobj, ".gnu.version");
6074 s->flags |= SEC_EXCLUDE;
6075 }
6076 }
6077 return TRUE;
6078}
6079
74541ad4
AM
6080/* Find the first non-excluded output section. We'll use its
6081 section symbol for some emitted relocs. */
6082void
6083_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6084{
6085 asection *s;
6086
6087 for (s = output_bfd->sections; s != NULL; s = s->next)
6088 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6089 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6090 {
6091 elf_hash_table (info)->text_index_section = s;
6092 break;
6093 }
6094}
6095
6096/* Find two non-excluded output sections, one for code, one for data.
6097 We'll use their section symbols for some emitted relocs. */
6098void
6099_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6100{
6101 asection *s;
6102
6103 for (s = output_bfd->sections; s != NULL; s = s->next)
6104 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6105 == (SEC_ALLOC | SEC_READONLY))
6106 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6107 {
6108 elf_hash_table (info)->text_index_section = s;
6109 break;
6110 }
6111
6112 for (s = output_bfd->sections; s != NULL; s = s->next)
6113 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6114 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6115 {
6116 elf_hash_table (info)->data_index_section = s;
6117 break;
6118 }
6119
6120 if (elf_hash_table (info)->text_index_section == NULL)
6121 elf_hash_table (info)->text_index_section
6122 = elf_hash_table (info)->data_index_section;
6123}
6124
8423293d
AM
6125bfd_boolean
6126bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6127{
74541ad4
AM
6128 const struct elf_backend_data *bed;
6129
8423293d
AM
6130 if (!is_elf_hash_table (info->hash))
6131 return TRUE;
6132
74541ad4
AM
6133 bed = get_elf_backend_data (output_bfd);
6134 (*bed->elf_backend_init_index_section) (output_bfd, info);
6135
8423293d
AM
6136 if (elf_hash_table (info)->dynamic_sections_created)
6137 {
6138 bfd *dynobj;
8423293d
AM
6139 asection *s;
6140 bfd_size_type dynsymcount;
6141 unsigned long section_sym_count;
8423293d
AM
6142 unsigned int dtagcount;
6143
6144 dynobj = elf_hash_table (info)->dynobj;
6145
5a580b3a
AM
6146 /* Assign dynsym indicies. In a shared library we generate a
6147 section symbol for each output section, which come first.
6148 Next come all of the back-end allocated local dynamic syms,
6149 followed by the rest of the global symbols. */
6150
554220db
AM
6151 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6152 &section_sym_count);
5a580b3a
AM
6153
6154 /* Work out the size of the symbol version section. */
6155 s = bfd_get_section_by_name (dynobj, ".gnu.version");
6156 BFD_ASSERT (s != NULL);
8423293d
AM
6157 if (dynsymcount != 0
6158 && (s->flags & SEC_EXCLUDE) == 0)
5a580b3a 6159 {
eea6121a
AM
6160 s->size = dynsymcount * sizeof (Elf_External_Versym);
6161 s->contents = bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
6162 if (s->contents == NULL)
6163 return FALSE;
6164
6165 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6166 return FALSE;
6167 }
6168
6169 /* Set the size of the .dynsym and .hash sections. We counted
6170 the number of dynamic symbols in elf_link_add_object_symbols.
6171 We will build the contents of .dynsym and .hash when we build
6172 the final symbol table, because until then we do not know the
6173 correct value to give the symbols. We built the .dynstr
6174 section as we went along in elf_link_add_object_symbols. */
6175 s = bfd_get_section_by_name (dynobj, ".dynsym");
6176 BFD_ASSERT (s != NULL);
eea6121a 6177 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a
AM
6178
6179 if (dynsymcount != 0)
6180 {
554220db
AM
6181 s->contents = bfd_alloc (output_bfd, s->size);
6182 if (s->contents == NULL)
6183 return FALSE;
5a580b3a 6184
554220db
AM
6185 /* The first entry in .dynsym is a dummy symbol.
6186 Clear all the section syms, in case we don't output them all. */
6187 ++section_sym_count;
6188 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a
AM
6189 }
6190
fdc90cb4
JJ
6191 elf_hash_table (info)->bucketcount = 0;
6192
5a580b3a
AM
6193 /* Compute the size of the hashing table. As a side effect this
6194 computes the hash values for all the names we export. */
fdc90cb4
JJ
6195 if (info->emit_hash)
6196 {
6197 unsigned long int *hashcodes;
14b1c01e 6198 struct hash_codes_info hashinf;
fdc90cb4
JJ
6199 bfd_size_type amt;
6200 unsigned long int nsyms;
6201 size_t bucketcount;
6202 size_t hash_entry_size;
6203
6204 /* Compute the hash values for all exported symbols. At the same
6205 time store the values in an array so that we could use them for
6206 optimizations. */
6207 amt = dynsymcount * sizeof (unsigned long int);
6208 hashcodes = bfd_malloc (amt);
6209 if (hashcodes == NULL)
6210 return FALSE;
14b1c01e
AM
6211 hashinf.hashcodes = hashcodes;
6212 hashinf.error = FALSE;
5a580b3a 6213
fdc90cb4
JJ
6214 /* Put all hash values in HASHCODES. */
6215 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
6216 elf_collect_hash_codes, &hashinf);
6217 if (hashinf.error)
6218 return FALSE;
5a580b3a 6219
14b1c01e 6220 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
6221 bucketcount
6222 = compute_bucket_count (info, hashcodes, nsyms, 0);
6223 free (hashcodes);
6224
6225 if (bucketcount == 0)
6226 return FALSE;
5a580b3a 6227
fdc90cb4
JJ
6228 elf_hash_table (info)->bucketcount = bucketcount;
6229
6230 s = bfd_get_section_by_name (dynobj, ".hash");
6231 BFD_ASSERT (s != NULL);
6232 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6233 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6234 s->contents = bfd_zalloc (output_bfd, s->size);
6235 if (s->contents == NULL)
6236 return FALSE;
6237
6238 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6239 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6240 s->contents + hash_entry_size);
6241 }
6242
6243 if (info->emit_gnu_hash)
6244 {
6245 size_t i, cnt;
6246 unsigned char *contents;
6247 struct collect_gnu_hash_codes cinfo;
6248 bfd_size_type amt;
6249 size_t bucketcount;
6250
6251 memset (&cinfo, 0, sizeof (cinfo));
6252
6253 /* Compute the hash values for all exported symbols. At the same
6254 time store the values in an array so that we could use them for
6255 optimizations. */
6256 amt = dynsymcount * 2 * sizeof (unsigned long int);
6257 cinfo.hashcodes = bfd_malloc (amt);
6258 if (cinfo.hashcodes == NULL)
6259 return FALSE;
6260
6261 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6262 cinfo.min_dynindx = -1;
6263 cinfo.output_bfd = output_bfd;
6264 cinfo.bed = bed;
6265
6266 /* Put all hash values in HASHCODES. */
6267 elf_link_hash_traverse (elf_hash_table (info),
6268 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e
AM
6269 if (cinfo.error)
6270 return FALSE;
fdc90cb4
JJ
6271
6272 bucketcount
6273 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6274
6275 if (bucketcount == 0)
6276 {
6277 free (cinfo.hashcodes);
6278 return FALSE;
6279 }
6280
6281 s = bfd_get_section_by_name (dynobj, ".gnu.hash");
6282 BFD_ASSERT (s != NULL);
6283
6284 if (cinfo.nsyms == 0)
6285 {
6286 /* Empty .gnu.hash section is special. */
6287 BFD_ASSERT (cinfo.min_dynindx == -1);
6288 free (cinfo.hashcodes);
6289 s->size = 5 * 4 + bed->s->arch_size / 8;
6290 contents = bfd_zalloc (output_bfd, s->size);
6291 if (contents == NULL)
6292 return FALSE;
6293 s->contents = contents;
6294 /* 1 empty bucket. */
6295 bfd_put_32 (output_bfd, 1, contents);
6296 /* SYMIDX above the special symbol 0. */
6297 bfd_put_32 (output_bfd, 1, contents + 4);
6298 /* Just one word for bitmask. */
6299 bfd_put_32 (output_bfd, 1, contents + 8);
6300 /* Only hash fn bloom filter. */
6301 bfd_put_32 (output_bfd, 0, contents + 12);
6302 /* No hashes are valid - empty bitmask. */
6303 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6304 /* No hashes in the only bucket. */
6305 bfd_put_32 (output_bfd, 0,
6306 contents + 16 + bed->s->arch_size / 8);
6307 }
6308 else
6309 {
fdc90cb4 6310 unsigned long int maskwords, maskbitslog2;
0b33793d 6311 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4
JJ
6312
6313 maskbitslog2 = bfd_log2 (cinfo.nsyms) + 1;
6314 if (maskbitslog2 < 3)
6315 maskbitslog2 = 5;
6316 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6317 maskbitslog2 = maskbitslog2 + 3;
6318 else
6319 maskbitslog2 = maskbitslog2 + 2;
6320 if (bed->s->arch_size == 64)
6321 {
6322 if (maskbitslog2 == 5)
6323 maskbitslog2 = 6;
6324 cinfo.shift1 = 6;
6325 }
6326 else
6327 cinfo.shift1 = 5;
6328 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 6329 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
6330 cinfo.maskbits = 1 << maskbitslog2;
6331 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6332 amt = bucketcount * sizeof (unsigned long int) * 2;
6333 amt += maskwords * sizeof (bfd_vma);
6334 cinfo.bitmask = bfd_malloc (amt);
6335 if (cinfo.bitmask == NULL)
6336 {
6337 free (cinfo.hashcodes);
6338 return FALSE;
6339 }
6340
6341 cinfo.counts = (void *) (cinfo.bitmask + maskwords);
6342 cinfo.indx = cinfo.counts + bucketcount;
6343 cinfo.symindx = dynsymcount - cinfo.nsyms;
6344 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6345
6346 /* Determine how often each hash bucket is used. */
6347 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6348 for (i = 0; i < cinfo.nsyms; ++i)
6349 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6350
6351 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6352 if (cinfo.counts[i] != 0)
6353 {
6354 cinfo.indx[i] = cnt;
6355 cnt += cinfo.counts[i];
6356 }
6357 BFD_ASSERT (cnt == dynsymcount);
6358 cinfo.bucketcount = bucketcount;
6359 cinfo.local_indx = cinfo.min_dynindx;
6360
6361 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6362 s->size += cinfo.maskbits / 8;
6363 contents = bfd_zalloc (output_bfd, s->size);
6364 if (contents == NULL)
6365 {
6366 free (cinfo.bitmask);
6367 free (cinfo.hashcodes);
6368 return FALSE;
6369 }
6370
6371 s->contents = contents;
6372 bfd_put_32 (output_bfd, bucketcount, contents);
6373 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6374 bfd_put_32 (output_bfd, maskwords, contents + 8);
6375 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6376 contents += 16 + cinfo.maskbits / 8;
6377
6378 for (i = 0; i < bucketcount; ++i)
6379 {
6380 if (cinfo.counts[i] == 0)
6381 bfd_put_32 (output_bfd, 0, contents);
6382 else
6383 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6384 contents += 4;
6385 }
6386
6387 cinfo.contents = contents;
6388
6389 /* Renumber dynamic symbols, populate .gnu.hash section. */
6390 elf_link_hash_traverse (elf_hash_table (info),
6391 elf_renumber_gnu_hash_syms, &cinfo);
6392
6393 contents = s->contents + 16;
6394 for (i = 0; i < maskwords; ++i)
6395 {
6396 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6397 contents);
6398 contents += bed->s->arch_size / 8;
6399 }
6400
6401 free (cinfo.bitmask);
6402 free (cinfo.hashcodes);
6403 }
6404 }
5a580b3a
AM
6405
6406 s = bfd_get_section_by_name (dynobj, ".dynstr");
6407 BFD_ASSERT (s != NULL);
6408
4ad4eba5 6409 elf_finalize_dynstr (output_bfd, info);
5a580b3a 6410
eea6121a 6411 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
6412
6413 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6414 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6415 return FALSE;
6416 }
6417
6418 return TRUE;
6419}
4d269e42
AM
6420\f
6421/* Indicate that we are only retrieving symbol values from this
6422 section. */
6423
6424void
6425_bfd_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
6426{
6427 if (is_elf_hash_table (info->hash))
6428 sec->sec_info_type = ELF_INFO_TYPE_JUST_SYMS;
6429 _bfd_generic_link_just_syms (sec, info);
6430}
6431
6432/* Make sure sec_info_type is cleared if sec_info is cleared too. */
6433
6434static void
6435merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6436 asection *sec)
6437{
6438 BFD_ASSERT (sec->sec_info_type == ELF_INFO_TYPE_MERGE);
6439 sec->sec_info_type = ELF_INFO_TYPE_NONE;
6440}
6441
6442/* Finish SHF_MERGE section merging. */
6443
6444bfd_boolean
6445_bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info)
6446{
6447 bfd *ibfd;
6448 asection *sec;
6449
6450 if (!is_elf_hash_table (info->hash))
6451 return FALSE;
6452
6453 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6454 if ((ibfd->flags & DYNAMIC) == 0)
6455 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6456 if ((sec->flags & SEC_MERGE) != 0
6457 && !bfd_is_abs_section (sec->output_section))
6458 {
6459 struct bfd_elf_section_data *secdata;
6460
6461 secdata = elf_section_data (sec);
6462 if (! _bfd_add_merge_section (abfd,
6463 &elf_hash_table (info)->merge_info,
6464 sec, &secdata->sec_info))
6465 return FALSE;
6466 else if (secdata->sec_info)
6467 sec->sec_info_type = ELF_INFO_TYPE_MERGE;
6468 }
6469
6470 if (elf_hash_table (info)->merge_info != NULL)
6471 _bfd_merge_sections (abfd, info, elf_hash_table (info)->merge_info,
6472 merge_sections_remove_hook);
6473 return TRUE;
6474}
6475
6476/* Create an entry in an ELF linker hash table. */
6477
6478struct bfd_hash_entry *
6479_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6480 struct bfd_hash_table *table,
6481 const char *string)
6482{
6483 /* Allocate the structure if it has not already been allocated by a
6484 subclass. */
6485 if (entry == NULL)
6486 {
6487 entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
6488 if (entry == NULL)
6489 return entry;
6490 }
6491
6492 /* Call the allocation method of the superclass. */
6493 entry = _bfd_link_hash_newfunc (entry, table, string);
6494 if (entry != NULL)
6495 {
6496 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6497 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6498
6499 /* Set local fields. */
6500 ret->indx = -1;
6501 ret->dynindx = -1;
6502 ret->got = htab->init_got_refcount;
6503 ret->plt = htab->init_plt_refcount;
6504 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6505 - offsetof (struct elf_link_hash_entry, size)));
6506 /* Assume that we have been called by a non-ELF symbol reader.
6507 This flag is then reset by the code which reads an ELF input
6508 file. This ensures that a symbol created by a non-ELF symbol
6509 reader will have the flag set correctly. */
6510 ret->non_elf = 1;
6511 }
6512
6513 return entry;
6514}
6515
6516/* Copy data from an indirect symbol to its direct symbol, hiding the
6517 old indirect symbol. Also used for copying flags to a weakdef. */
6518
6519void
6520_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
6521 struct elf_link_hash_entry *dir,
6522 struct elf_link_hash_entry *ind)
6523{
6524 struct elf_link_hash_table *htab;
6525
6526 /* Copy down any references that we may have already seen to the
6527 symbol which just became indirect. */
6528
6529 dir->ref_dynamic |= ind->ref_dynamic;
6530 dir->ref_regular |= ind->ref_regular;
6531 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
6532 dir->non_got_ref |= ind->non_got_ref;
6533 dir->needs_plt |= ind->needs_plt;
6534 dir->pointer_equality_needed |= ind->pointer_equality_needed;
6535
6536 if (ind->root.type != bfd_link_hash_indirect)
6537 return;
6538
6539 /* Copy over the global and procedure linkage table refcount entries.
6540 These may have been already set up by a check_relocs routine. */
6541 htab = elf_hash_table (info);
6542 if (ind->got.refcount > htab->init_got_refcount.refcount)
6543 {
6544 if (dir->got.refcount < 0)
6545 dir->got.refcount = 0;
6546 dir->got.refcount += ind->got.refcount;
6547 ind->got.refcount = htab->init_got_refcount.refcount;
6548 }
6549
6550 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
6551 {
6552 if (dir->plt.refcount < 0)
6553 dir->plt.refcount = 0;
6554 dir->plt.refcount += ind->plt.refcount;
6555 ind->plt.refcount = htab->init_plt_refcount.refcount;
6556 }
6557
6558 if (ind->dynindx != -1)
6559 {
6560 if (dir->dynindx != -1)
6561 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
6562 dir->dynindx = ind->dynindx;
6563 dir->dynstr_index = ind->dynstr_index;
6564 ind->dynindx = -1;
6565 ind->dynstr_index = 0;
6566 }
6567}
6568
6569void
6570_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
6571 struct elf_link_hash_entry *h,
6572 bfd_boolean force_local)
6573{
6574 h->plt = elf_hash_table (info)->init_plt_offset;
6575 h->needs_plt = 0;
6576 if (force_local)
6577 {
6578 h->forced_local = 1;
6579 if (h->dynindx != -1)
6580 {
6581 h->dynindx = -1;
6582 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
6583 h->dynstr_index);
6584 }
6585 }
6586}
6587
6588/* Initialize an ELF linker hash table. */
6589
6590bfd_boolean
6591_bfd_elf_link_hash_table_init
6592 (struct elf_link_hash_table *table,
6593 bfd *abfd,
6594 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
6595 struct bfd_hash_table *,
6596 const char *),
6597 unsigned int entsize)
6598{
6599 bfd_boolean ret;
6600 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
6601
6602 memset (table, 0, sizeof * table);
6603 table->init_got_refcount.refcount = can_refcount - 1;
6604 table->init_plt_refcount.refcount = can_refcount - 1;
6605 table->init_got_offset.offset = -(bfd_vma) 1;
6606 table->init_plt_offset.offset = -(bfd_vma) 1;
6607 /* The first dynamic symbol is a dummy. */
6608 table->dynsymcount = 1;
6609
6610 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
6611 table->root.type = bfd_link_elf_hash_table;
6612
6613 return ret;
6614}
6615
6616/* Create an ELF linker hash table. */
6617
6618struct bfd_link_hash_table *
6619_bfd_elf_link_hash_table_create (bfd *abfd)
6620{
6621 struct elf_link_hash_table *ret;
6622 bfd_size_type amt = sizeof (struct elf_link_hash_table);
6623
6624 ret = bfd_malloc (amt);
6625 if (ret == NULL)
6626 return NULL;
6627
6628 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
6629 sizeof (struct elf_link_hash_entry)))
6630 {
6631 free (ret);
6632 return NULL;
6633 }
6634
6635 return &ret->root;
6636}
6637
6638/* This is a hook for the ELF emulation code in the generic linker to
6639 tell the backend linker what file name to use for the DT_NEEDED
6640 entry for a dynamic object. */
6641
6642void
6643bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
6644{
6645 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6646 && bfd_get_format (abfd) == bfd_object)
6647 elf_dt_name (abfd) = name;
6648}
6649
6650int
6651bfd_elf_get_dyn_lib_class (bfd *abfd)
6652{
6653 int lib_class;
6654 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6655 && bfd_get_format (abfd) == bfd_object)
6656 lib_class = elf_dyn_lib_class (abfd);
6657 else
6658 lib_class = 0;
6659 return lib_class;
6660}
6661
6662void
6663bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
6664{
6665 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6666 && bfd_get_format (abfd) == bfd_object)
6667 elf_dyn_lib_class (abfd) = lib_class;
6668}
6669
6670/* Get the list of DT_NEEDED entries for a link. This is a hook for
6671 the linker ELF emulation code. */
6672
6673struct bfd_link_needed_list *
6674bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
6675 struct bfd_link_info *info)
6676{
6677 if (! is_elf_hash_table (info->hash))
6678 return NULL;
6679 return elf_hash_table (info)->needed;
6680}
6681
6682/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
6683 hook for the linker ELF emulation code. */
6684
6685struct bfd_link_needed_list *
6686bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
6687 struct bfd_link_info *info)
6688{
6689 if (! is_elf_hash_table (info->hash))
6690 return NULL;
6691 return elf_hash_table (info)->runpath;
6692}
6693
6694/* Get the name actually used for a dynamic object for a link. This
6695 is the SONAME entry if there is one. Otherwise, it is the string
6696 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
6697
6698const char *
6699bfd_elf_get_dt_soname (bfd *abfd)
6700{
6701 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6702 && bfd_get_format (abfd) == bfd_object)
6703 return elf_dt_name (abfd);
6704 return NULL;
6705}
6706
6707/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
6708 the ELF linker emulation code. */
6709
6710bfd_boolean
6711bfd_elf_get_bfd_needed_list (bfd *abfd,
6712 struct bfd_link_needed_list **pneeded)
6713{
6714 asection *s;
6715 bfd_byte *dynbuf = NULL;
6716 int elfsec;
6717 unsigned long shlink;
6718 bfd_byte *extdyn, *extdynend;
6719 size_t extdynsize;
6720 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
6721
6722 *pneeded = NULL;
6723
6724 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
6725 || bfd_get_format (abfd) != bfd_object)
6726 return TRUE;
6727
6728 s = bfd_get_section_by_name (abfd, ".dynamic");
6729 if (s == NULL || s->size == 0)
6730 return TRUE;
6731
6732 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
6733 goto error_return;
6734
6735 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
6736 if (elfsec == -1)
6737 goto error_return;
6738
6739 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 6740
4d269e42
AM
6741 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
6742 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
6743
6744 extdyn = dynbuf;
6745 extdynend = extdyn + s->size;
6746 for (; extdyn < extdynend; extdyn += extdynsize)
6747 {
6748 Elf_Internal_Dyn dyn;
6749
6750 (*swap_dyn_in) (abfd, extdyn, &dyn);
6751
6752 if (dyn.d_tag == DT_NULL)
6753 break;
6754
6755 if (dyn.d_tag == DT_NEEDED)
6756 {
6757 const char *string;
6758 struct bfd_link_needed_list *l;
6759 unsigned int tagv = dyn.d_un.d_val;
6760 bfd_size_type amt;
6761
6762 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
6763 if (string == NULL)
6764 goto error_return;
6765
6766 amt = sizeof *l;
6767 l = bfd_alloc (abfd, amt);
6768 if (l == NULL)
6769 goto error_return;
6770
6771 l->by = abfd;
6772 l->name = string;
6773 l->next = *pneeded;
6774 *pneeded = l;
6775 }
6776 }
6777
6778 free (dynbuf);
6779
6780 return TRUE;
6781
6782 error_return:
6783 if (dynbuf != NULL)
6784 free (dynbuf);
6785 return FALSE;
6786}
6787
6788struct elf_symbuf_symbol
6789{
6790 unsigned long st_name; /* Symbol name, index in string tbl */
6791 unsigned char st_info; /* Type and binding attributes */
6792 unsigned char st_other; /* Visibilty, and target specific */
6793};
6794
6795struct elf_symbuf_head
6796{
6797 struct elf_symbuf_symbol *ssym;
6798 bfd_size_type count;
6799 unsigned int st_shndx;
6800};
6801
6802struct elf_symbol
6803{
6804 union
6805 {
6806 Elf_Internal_Sym *isym;
6807 struct elf_symbuf_symbol *ssym;
6808 } u;
6809 const char *name;
6810};
6811
6812/* Sort references to symbols by ascending section number. */
6813
6814static int
6815elf_sort_elf_symbol (const void *arg1, const void *arg2)
6816{
6817 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
6818 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
6819
6820 return s1->st_shndx - s2->st_shndx;
6821}
6822
6823static int
6824elf_sym_name_compare (const void *arg1, const void *arg2)
6825{
6826 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
6827 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
6828 return strcmp (s1->name, s2->name);
6829}
6830
6831static struct elf_symbuf_head *
6832elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
6833{
14b1c01e 6834 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
6835 struct elf_symbuf_symbol *ssym;
6836 struct elf_symbuf_head *ssymbuf, *ssymhead;
6837 bfd_size_type i, shndx_count;
6838
14b1c01e 6839 indbuf = bfd_malloc2 (symcount, sizeof (*indbuf));
4d269e42
AM
6840 if (indbuf == NULL)
6841 return NULL;
6842
6843 for (ind = indbuf, i = 0; i < symcount; i++)
6844 if (isymbuf[i].st_shndx != SHN_UNDEF)
6845 *ind++ = &isymbuf[i];
6846 indbufend = ind;
6847
6848 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
6849 elf_sort_elf_symbol);
6850
6851 shndx_count = 0;
6852 if (indbufend > indbuf)
6853 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
6854 if (ind[0]->st_shndx != ind[1]->st_shndx)
6855 shndx_count++;
6856
6857 ssymbuf = bfd_malloc ((shndx_count + 1) * sizeof (*ssymbuf)
6858 + (indbufend - indbuf) * sizeof (*ssymbuf));
6859 if (ssymbuf == NULL)
6860 {
6861 free (indbuf);
6862 return NULL;
6863 }
6864
6865 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count);
6866 ssymbuf->ssym = NULL;
6867 ssymbuf->count = shndx_count;
6868 ssymbuf->st_shndx = 0;
6869 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
6870 {
6871 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
6872 {
6873 ssymhead++;
6874 ssymhead->ssym = ssym;
6875 ssymhead->count = 0;
6876 ssymhead->st_shndx = (*ind)->st_shndx;
6877 }
6878 ssym->st_name = (*ind)->st_name;
6879 ssym->st_info = (*ind)->st_info;
6880 ssym->st_other = (*ind)->st_other;
6881 ssymhead->count++;
6882 }
6883 BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count);
6884
6885 free (indbuf);
6886 return ssymbuf;
6887}
6888
6889/* Check if 2 sections define the same set of local and global
6890 symbols. */
6891
6892bfd_boolean
6893bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
6894 struct bfd_link_info *info)
6895{
6896 bfd *bfd1, *bfd2;
6897 const struct elf_backend_data *bed1, *bed2;
6898 Elf_Internal_Shdr *hdr1, *hdr2;
6899 bfd_size_type symcount1, symcount2;
6900 Elf_Internal_Sym *isymbuf1, *isymbuf2;
6901 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
6902 Elf_Internal_Sym *isym, *isymend;
6903 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
6904 bfd_size_type count1, count2, i;
6905 int shndx1, shndx2;
6906 bfd_boolean result;
6907
6908 bfd1 = sec1->owner;
6909 bfd2 = sec2->owner;
6910
6911 /* If both are .gnu.linkonce sections, they have to have the same
6912 section name. */
6913 if (CONST_STRNEQ (sec1->name, ".gnu.linkonce")
6914 && CONST_STRNEQ (sec2->name, ".gnu.linkonce"))
6915 return strcmp (sec1->name + sizeof ".gnu.linkonce",
6916 sec2->name + sizeof ".gnu.linkonce") == 0;
6917
6918 /* Both sections have to be in ELF. */
6919 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
6920 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
6921 return FALSE;
6922
6923 if (elf_section_type (sec1) != elf_section_type (sec2))
6924 return FALSE;
6925
6926 if ((elf_section_flags (sec1) & SHF_GROUP) != 0
6927 && (elf_section_flags (sec2) & SHF_GROUP) != 0)
6928 {
6929 /* If both are members of section groups, they have to have the
6930 same group name. */
6931 if (strcmp (elf_group_name (sec1), elf_group_name (sec2)) != 0)
6932 return FALSE;
6933 }
6934
6935 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
6936 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
6937 if (shndx1 == -1 || shndx2 == -1)
6938 return FALSE;
6939
6940 bed1 = get_elf_backend_data (bfd1);
6941 bed2 = get_elf_backend_data (bfd2);
6942 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
6943 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
6944 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
6945 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
6946
6947 if (symcount1 == 0 || symcount2 == 0)
6948 return FALSE;
6949
6950 result = FALSE;
6951 isymbuf1 = NULL;
6952 isymbuf2 = NULL;
6953 ssymbuf1 = elf_tdata (bfd1)->symbuf;
6954 ssymbuf2 = elf_tdata (bfd2)->symbuf;
6955
6956 if (ssymbuf1 == NULL)
6957 {
6958 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
6959 NULL, NULL, NULL);
6960 if (isymbuf1 == NULL)
6961 goto done;
6962
6963 if (!info->reduce_memory_overheads)
6964 elf_tdata (bfd1)->symbuf = ssymbuf1
6965 = elf_create_symbuf (symcount1, isymbuf1);
6966 }
6967
6968 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
6969 {
6970 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
6971 NULL, NULL, NULL);
6972 if (isymbuf2 == NULL)
6973 goto done;
6974
6975 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
6976 elf_tdata (bfd2)->symbuf = ssymbuf2
6977 = elf_create_symbuf (symcount2, isymbuf2);
6978 }
6979
6980 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
6981 {
6982 /* Optimized faster version. */
6983 bfd_size_type lo, hi, mid;
6984 struct elf_symbol *symp;
6985 struct elf_symbuf_symbol *ssym, *ssymend;
6986
6987 lo = 0;
6988 hi = ssymbuf1->count;
6989 ssymbuf1++;
6990 count1 = 0;
6991 while (lo < hi)
6992 {
6993 mid = (lo + hi) / 2;
6994 if ((unsigned int) shndx1 < ssymbuf1[mid].st_shndx)
6995 hi = mid;
6996 else if ((unsigned int) shndx1 > ssymbuf1[mid].st_shndx)
6997 lo = mid + 1;
6998 else
6999 {
7000 count1 = ssymbuf1[mid].count;
7001 ssymbuf1 += mid;
7002 break;
7003 }
7004 }
7005
7006 lo = 0;
7007 hi = ssymbuf2->count;
7008 ssymbuf2++;
7009 count2 = 0;
7010 while (lo < hi)
7011 {
7012 mid = (lo + hi) / 2;
7013 if ((unsigned int) shndx2 < ssymbuf2[mid].st_shndx)
7014 hi = mid;
7015 else if ((unsigned int) shndx2 > ssymbuf2[mid].st_shndx)
7016 lo = mid + 1;
7017 else
7018 {
7019 count2 = ssymbuf2[mid].count;
7020 ssymbuf2 += mid;
7021 break;
7022 }
7023 }
7024
7025 if (count1 == 0 || count2 == 0 || count1 != count2)
7026 goto done;
7027
7028 symtable1 = bfd_malloc (count1 * sizeof (struct elf_symbol));
7029 symtable2 = bfd_malloc (count2 * sizeof (struct elf_symbol));
7030 if (symtable1 == NULL || symtable2 == NULL)
7031 goto done;
7032
7033 symp = symtable1;
7034 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7035 ssym < ssymend; ssym++, symp++)
7036 {
7037 symp->u.ssym = ssym;
7038 symp->name = bfd_elf_string_from_elf_section (bfd1,
7039 hdr1->sh_link,
7040 ssym->st_name);
7041 }
7042
7043 symp = symtable2;
7044 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7045 ssym < ssymend; ssym++, symp++)
7046 {
7047 symp->u.ssym = ssym;
7048 symp->name = bfd_elf_string_from_elf_section (bfd2,
7049 hdr2->sh_link,
7050 ssym->st_name);
7051 }
7052
7053 /* Sort symbol by name. */
7054 qsort (symtable1, count1, sizeof (struct elf_symbol),
7055 elf_sym_name_compare);
7056 qsort (symtable2, count1, sizeof (struct elf_symbol),
7057 elf_sym_name_compare);
7058
7059 for (i = 0; i < count1; i++)
7060 /* Two symbols must have the same binding, type and name. */
7061 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7062 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7063 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7064 goto done;
7065
7066 result = TRUE;
7067 goto done;
7068 }
7069
7070 symtable1 = bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7071 symtable2 = bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7072 if (symtable1 == NULL || symtable2 == NULL)
7073 goto done;
7074
7075 /* Count definitions in the section. */
7076 count1 = 0;
7077 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7078 if (isym->st_shndx == (unsigned int) shndx1)
7079 symtable1[count1++].u.isym = isym;
7080
7081 count2 = 0;
7082 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7083 if (isym->st_shndx == (unsigned int) shndx2)
7084 symtable2[count2++].u.isym = isym;
7085
7086 if (count1 == 0 || count2 == 0 || count1 != count2)
7087 goto done;
7088
7089 for (i = 0; i < count1; i++)
7090 symtable1[i].name
7091 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7092 symtable1[i].u.isym->st_name);
7093
7094 for (i = 0; i < count2; i++)
7095 symtable2[i].name
7096 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7097 symtable2[i].u.isym->st_name);
7098
7099 /* Sort symbol by name. */
7100 qsort (symtable1, count1, sizeof (struct elf_symbol),
7101 elf_sym_name_compare);
7102 qsort (symtable2, count1, sizeof (struct elf_symbol),
7103 elf_sym_name_compare);
7104
7105 for (i = 0; i < count1; i++)
7106 /* Two symbols must have the same binding, type and name. */
7107 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7108 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7109 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7110 goto done;
7111
7112 result = TRUE;
7113
7114done:
7115 if (symtable1)
7116 free (symtable1);
7117 if (symtable2)
7118 free (symtable2);
7119 if (isymbuf1)
7120 free (isymbuf1);
7121 if (isymbuf2)
7122 free (isymbuf2);
7123
7124 return result;
7125}
7126
7127/* Return TRUE if 2 section types are compatible. */
7128
7129bfd_boolean
7130_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7131 bfd *bbfd, const asection *bsec)
7132{
7133 if (asec == NULL
7134 || bsec == NULL
7135 || abfd->xvec->flavour != bfd_target_elf_flavour
7136 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7137 return TRUE;
7138
7139 return elf_section_type (asec) == elf_section_type (bsec);
7140}
7141\f
c152c796
AM
7142/* Final phase of ELF linker. */
7143
7144/* A structure we use to avoid passing large numbers of arguments. */
7145
7146struct elf_final_link_info
7147{
7148 /* General link information. */
7149 struct bfd_link_info *info;
7150 /* Output BFD. */
7151 bfd *output_bfd;
7152 /* Symbol string table. */
7153 struct bfd_strtab_hash *symstrtab;
7154 /* .dynsym section. */
7155 asection *dynsym_sec;
7156 /* .hash section. */
7157 asection *hash_sec;
7158 /* symbol version section (.gnu.version). */
7159 asection *symver_sec;
7160 /* Buffer large enough to hold contents of any section. */
7161 bfd_byte *contents;
7162 /* Buffer large enough to hold external relocs of any section. */
7163 void *external_relocs;
7164 /* Buffer large enough to hold internal relocs of any section. */
7165 Elf_Internal_Rela *internal_relocs;
7166 /* Buffer large enough to hold external local symbols of any input
7167 BFD. */
7168 bfd_byte *external_syms;
7169 /* And a buffer for symbol section indices. */
7170 Elf_External_Sym_Shndx *locsym_shndx;
7171 /* Buffer large enough to hold internal local symbols of any input
7172 BFD. */
7173 Elf_Internal_Sym *internal_syms;
7174 /* Array large enough to hold a symbol index for each local symbol
7175 of any input BFD. */
7176 long *indices;
7177 /* Array large enough to hold a section pointer for each local
7178 symbol of any input BFD. */
7179 asection **sections;
7180 /* Buffer to hold swapped out symbols. */
7181 bfd_byte *symbuf;
7182 /* And one for symbol section indices. */
7183 Elf_External_Sym_Shndx *symshndxbuf;
7184 /* Number of swapped out symbols in buffer. */
7185 size_t symbuf_count;
7186 /* Number of symbols which fit in symbuf. */
7187 size_t symbuf_size;
7188 /* And same for symshndxbuf. */
7189 size_t shndxbuf_size;
7190};
7191
7192/* This struct is used to pass information to elf_link_output_extsym. */
7193
7194struct elf_outext_info
7195{
7196 bfd_boolean failed;
7197 bfd_boolean localsyms;
7198 struct elf_final_link_info *finfo;
7199};
7200
d9352518
DB
7201
7202/* Support for evaluating a complex relocation.
7203
7204 Complex relocations are generalized, self-describing relocations. The
7205 implementation of them consists of two parts: complex symbols, and the
7206 relocations themselves.
7207
7208 The relocations are use a reserved elf-wide relocation type code (R_RELC
7209 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7210 information (start bit, end bit, word width, etc) into the addend. This
7211 information is extracted from CGEN-generated operand tables within gas.
7212
7213 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7214 internal) representing prefix-notation expressions, including but not
7215 limited to those sorts of expressions normally encoded as addends in the
7216 addend field. The symbol mangling format is:
7217
7218 <node> := <literal>
7219 | <unary-operator> ':' <node>
7220 | <binary-operator> ':' <node> ':' <node>
7221 ;
7222
7223 <literal> := 's' <digits=N> ':' <N character symbol name>
7224 | 'S' <digits=N> ':' <N character section name>
7225 | '#' <hexdigits>
7226 ;
7227
7228 <binary-operator> := as in C
7229 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7230
7231static void
7232set_symbol_value (bfd * bfd_with_globals,
7233 struct elf_final_link_info * finfo,
7234 int symidx,
7235 bfd_vma val)
7236{
7237 bfd_boolean is_local;
7238 Elf_Internal_Sym * sym;
7239 struct elf_link_hash_entry ** sym_hashes;
7240 struct elf_link_hash_entry * h;
7241
7242 sym_hashes = elf_sym_hashes (bfd_with_globals);
7243 sym = finfo->internal_syms + symidx;
7244 is_local = ELF_ST_BIND(sym->st_info) == STB_LOCAL;
7245
7246 if (is_local)
7247 {
7248 /* It is a local symbol: move it to the
7249 "absolute" section and give it a value. */
7250 sym->st_shndx = SHN_ABS;
7251 sym->st_value = val;
7252 }
7253 else
7254 {
7255 /* It is a global symbol: set its link type
7256 to "defined" and give it a value. */
7257 h = sym_hashes [symidx];
7258 while (h->root.type == bfd_link_hash_indirect
7259 || h->root.type == bfd_link_hash_warning)
7260 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7261 h->root.type = bfd_link_hash_defined;
7262 h->root.u.def.value = val;
7263 h->root.u.def.section = bfd_abs_section_ptr;
7264 }
7265}
7266
7267static bfd_boolean
7268resolve_symbol (const char * name,
7269 bfd * input_bfd,
7270 struct elf_final_link_info * finfo,
7271 bfd_vma * result,
7272 size_t locsymcount)
7273{
7274 Elf_Internal_Sym * sym;
7275 struct bfd_link_hash_entry * global_entry;
7276 const char * candidate = NULL;
7277 Elf_Internal_Shdr * symtab_hdr;
7278 asection * sec = NULL;
7279 size_t i;
7280
7281 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7282
7283 for (i = 0; i < locsymcount; ++ i)
7284 {
7285 sym = finfo->internal_syms + i;
7286 sec = finfo->sections [i];
7287
7288 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7289 continue;
7290
7291 candidate = bfd_elf_string_from_elf_section (input_bfd,
7292 symtab_hdr->sh_link,
7293 sym->st_name);
7294#ifdef DEBUG
7295 printf ("Comparing string: '%s' vs. '%s' = 0x%x\n",
7296 name, candidate, (unsigned int)sym->st_value);
7297#endif
7298 if (candidate && strcmp (candidate, name) == 0)
7299 {
7300 * result = sym->st_value;
7301
7302 if (sym->st_shndx > SHN_UNDEF &&
7303 sym->st_shndx < SHN_LORESERVE)
7304 {
7305#ifdef DEBUG
7306 printf ("adjusting for sec '%s' @ 0x%x + 0x%x\n",
7307 sec->output_section->name,
7308 (unsigned int)sec->output_section->vma,
7309 (unsigned int)sec->output_offset);
7310#endif
7311 * result += sec->output_offset + sec->output_section->vma;
7312 }
7313#ifdef DEBUG
7314 printf ("Found symbol with effective value %8.8x\n", (unsigned int)* result);
7315#endif
7316 return TRUE;
7317 }
7318 }
7319
7320 /* Hmm, haven't found it yet. perhaps it is a global. */
7321 global_entry = bfd_link_hash_lookup (finfo->info->hash, name, FALSE, FALSE, TRUE);
7322 if (!global_entry)
7323 return FALSE;
7324
7325 if (global_entry->type == bfd_link_hash_defined
7326 || global_entry->type == bfd_link_hash_defweak)
7327 {
7328 * result = global_entry->u.def.value
7329 + global_entry->u.def.section->output_section->vma
7330 + global_entry->u.def.section->output_offset;
7331#ifdef DEBUG
7332 printf ("Found GLOBAL symbol '%s' with value %8.8x\n",
7333 global_entry->root.string, (unsigned int)*result);
7334#endif
7335 return TRUE;
7336 }
7337
7338 if (global_entry->type == bfd_link_hash_common)
7339 {
7340 *result = global_entry->u.def.value +
7341 bfd_com_section_ptr->output_section->vma +
7342 bfd_com_section_ptr->output_offset;
7343#ifdef DEBUG
7344 printf ("Found COMMON symbol '%s' with value %8.8x\n",
7345 global_entry->root.string, (unsigned int)*result);
7346#endif
7347 return TRUE;
7348 }
7349
7350 return FALSE;
7351}
7352
7353static bfd_boolean
7354resolve_section (const char * name,
7355 asection * sections,
7356 bfd_vma * result)
7357{
7358 asection * curr;
7359 unsigned int len;
7360
7361 for (curr = sections; curr; curr = curr->next)
7362 if (strcmp (curr->name, name) == 0)
7363 {
7364 *result = curr->vma;
7365 return TRUE;
7366 }
7367
7368 /* Hmm. still haven't found it. try pseudo-section names. */
7369 for (curr = sections; curr; curr = curr->next)
7370 {
7371 len = strlen (curr->name);
7372 if (len > strlen (name))
7373 continue;
7374
7375 if (strncmp (curr->name, name, len) == 0)
7376 {
7377 if (strncmp (".end", name + len, 4) == 0)
7378 {
7379 *result = curr->vma + curr->size;
7380 return TRUE;
7381 }
7382
7383 /* Insert more pseudo-section names here, if you like. */
7384 }
7385 }
7386
7387 return FALSE;
7388}
7389
7390static void
7391undefined_reference (const char * reftype,
7392 const char * name)
7393{
7394 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"), reftype, name);
7395}
7396
7397static bfd_boolean
7398eval_symbol (bfd_vma * result,
7399 char * sym,
7400 char ** advanced,
7401 bfd * input_bfd,
7402 struct elf_final_link_info * finfo,
7403 bfd_vma addr,
7404 bfd_vma section_offset,
7405 size_t locsymcount,
7406 int signed_p)
7407{
7408 int len;
7409 int symlen;
7410 bfd_vma a;
7411 bfd_vma b;
7412 const int bufsz = 4096;
7413 char symbuf [bufsz];
7414 const char * symend;
7415 bfd_boolean symbol_is_section = FALSE;
7416
7417 len = strlen (sym);
7418 symend = sym + len;
7419
7420 if (len < 1 || len > bufsz)
7421 {
7422 bfd_set_error (bfd_error_invalid_operation);
7423 return FALSE;
7424 }
7425
7426 switch (* sym)
7427 {
7428 case '.':
7429 * result = addr + section_offset;
7430 * advanced = sym + 1;
7431 return TRUE;
7432
7433 case '#':
7434 ++ sym;
7435 * result = strtoul (sym, advanced, 16);
7436 return TRUE;
7437
7438 case 'S':
7439 symbol_is_section = TRUE;
7440 case 's':
7441 ++ sym;
7442 symlen = strtol (sym, &sym, 10);
7443 ++ sym; /* Skip the trailing ':'. */
7444
7445 if ((symend < sym) || ((symlen + 1) > bufsz))
7446 {
7447 bfd_set_error (bfd_error_invalid_operation);
7448 return FALSE;
7449 }
7450
7451 memcpy (symbuf, sym, symlen);
7452 symbuf [symlen] = '\0';
7453 * advanced = sym + symlen;
7454
7455 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7456 the symbol as a section, or vice-versa. so we're pretty liberal in our
7457 interpretation here; section means "try section first", not "must be a
7458 section", and likewise with symbol. */
7459
7460 if (symbol_is_section)
7461 {
7462 if ((resolve_section (symbuf, finfo->output_bfd->sections, result) != TRUE)
7463 && (resolve_symbol (symbuf, input_bfd, finfo, result, locsymcount) != TRUE))
7464 {
7465 undefined_reference ("section", symbuf);
7466 return FALSE;
7467 }
7468 }
7469 else
7470 {
7471 if ((resolve_symbol (symbuf, input_bfd, finfo, result, locsymcount) != TRUE)
7472 && (resolve_section (symbuf, finfo->output_bfd->sections,
7473 result) != TRUE))
7474 {
7475 undefined_reference ("symbol", symbuf);
7476 return FALSE;
7477 }
7478 }
7479
7480 return TRUE;
7481
7482 /* All that remains are operators. */
7483
7484#define UNARY_OP(op) \
7485 if (strncmp (sym, #op, strlen (#op)) == 0) \
7486 { \
7487 sym += strlen (#op); \
7488 if (* sym == ':') \
7489 ++ sym; \
7490 if (eval_symbol (& a, sym, & sym, input_bfd, finfo, addr, \
7491 section_offset, locsymcount, signed_p) \
7492 != TRUE) \
7493 return FALSE; \
7494 if (signed_p) \
7495 * result = op ((signed)a); \
7496 else \
7497 * result = op a; \
7498 * advanced = sym; \
7499 return TRUE; \
7500 }
7501
7502#define BINARY_OP(op) \
7503 if (strncmp (sym, #op, strlen (#op)) == 0) \
7504 { \
7505 sym += strlen (#op); \
7506 if (* sym == ':') \
7507 ++ sym; \
7508 if (eval_symbol (& a, sym, & sym, input_bfd, finfo, addr, \
7509 section_offset, locsymcount, signed_p) \
7510 != TRUE) \
7511 return FALSE; \
7512 ++ sym; \
7513 if (eval_symbol (& b, sym, & sym, input_bfd, finfo, addr, \
7514 section_offset, locsymcount, signed_p) \
7515 != TRUE) \
7516 return FALSE; \
7517 if (signed_p) \
7518 * result = ((signed) a) op ((signed) b); \
7519 else \
7520 * result = a op b; \
7521 * advanced = sym; \
7522 return TRUE; \
7523 }
7524
7525 default:
7526 UNARY_OP (0-);
7527 BINARY_OP (<<);
7528 BINARY_OP (>>);
7529 BINARY_OP (==);
7530 BINARY_OP (!=);
7531 BINARY_OP (<=);
7532 BINARY_OP (>=);
7533 BINARY_OP (&&);
7534 BINARY_OP (||);
7535 UNARY_OP (~);
7536 UNARY_OP (!);
7537 BINARY_OP (*);
7538 BINARY_OP (/);
7539 BINARY_OP (%);
7540 BINARY_OP (^);
7541 BINARY_OP (|);
7542 BINARY_OP (&);
7543 BINARY_OP (+);
7544 BINARY_OP (-);
7545 BINARY_OP (<);
7546 BINARY_OP (>);
7547#undef UNARY_OP
7548#undef BINARY_OP
7549 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
7550 bfd_set_error (bfd_error_invalid_operation);
7551 return FALSE;
7552 }
7553}
7554
7555/* Entry point to evaluator, called from elf_link_input_bfd. */
7556
7557static bfd_boolean
7558evaluate_complex_relocation_symbols (bfd * input_bfd,
7559 struct elf_final_link_info * finfo,
7560 size_t locsymcount)
7561{
7562 const struct elf_backend_data * bed;
7563 Elf_Internal_Shdr * symtab_hdr;
7564 struct elf_link_hash_entry ** sym_hashes;
7565 asection * reloc_sec;
7566 bfd_boolean result = TRUE;
7567
7568 /* For each section, we're going to check and see if it has any
7569 complex relocations, and we're going to evaluate any of them
7570 we can. */
7571
7572 if (finfo->info->relocatable)
7573 return TRUE;
7574
7575 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7576 sym_hashes = elf_sym_hashes (input_bfd);
7577 bed = get_elf_backend_data (input_bfd);
7578
7579 for (reloc_sec = input_bfd->sections; reloc_sec; reloc_sec = reloc_sec->next)
7580 {
7581 Elf_Internal_Rela * internal_relocs;
7582 unsigned long i;
7583
7584 /* This section was omitted from the link. */
7585 if (! reloc_sec->linker_mark)
7586 continue;
7587
7588 /* Only process sections containing relocs. */
7589 if ((reloc_sec->flags & SEC_RELOC) == 0)
7590 continue;
7591
7592 if (reloc_sec->reloc_count == 0)
7593 continue;
7594
7595 /* Read in the relocs for this section. */
7596 internal_relocs
7597 = _bfd_elf_link_read_relocs (input_bfd, reloc_sec, NULL,
7598 (Elf_Internal_Rela *) NULL,
7599 FALSE);
7600 if (internal_relocs == NULL)
7601 continue;
7602
7603 for (i = reloc_sec->reloc_count; i--;)
7604 {
7605 Elf_Internal_Rela * rel;
7606 char * sym_name;
947844a3 7607 bfd_vma index;
d9352518
DB
7608 Elf_Internal_Sym * sym;
7609 bfd_vma result;
7610 bfd_vma section_offset;
7611 bfd_vma addr;
7612 int signed_p = 0;
7613
7614 rel = internal_relocs + i;
7615 section_offset = reloc_sec->output_section->vma
7616 + reloc_sec->output_offset;
7617 addr = rel->r_offset;
7618
7619 index = ELF32_R_SYM (rel->r_info);
7620 if (bed->s->arch_size == 64)
7621 index >>= 24;
4aac632e
AM
7622
7623 if (index == STN_UNDEF)
7624 continue;
7625
d9352518
DB
7626 if (index < locsymcount)
7627 {
7628 /* The symbol is local. */
7629 sym = finfo->internal_syms + index;
7630
7631 /* We're only processing STT_RELC or STT_SRELC type symbols. */
7632 if ((ELF_ST_TYPE (sym->st_info) != STT_RELC) &&
7633 (ELF_ST_TYPE (sym->st_info) != STT_SRELC))
7634 continue;
7635
7636 sym_name = bfd_elf_string_from_elf_section
7637 (input_bfd, symtab_hdr->sh_link, sym->st_name);
7638
7639 signed_p = (ELF_ST_TYPE (sym->st_info) == STT_SRELC);
7640 }
7641 else
7642 {
7643 /* The symbol is global. */
7644 struct elf_link_hash_entry * h;
7645
7646 if (elf_bad_symtab (input_bfd))
7647 continue;
7648
7649 h = sym_hashes [index - locsymcount];
7650 while ( h->root.type == bfd_link_hash_indirect
7651 || h->root.type == bfd_link_hash_warning)
7652 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7653
7654 if (h->type != STT_RELC && h->type != STT_SRELC)
7655 continue;
7656
7657 signed_p = (h->type == STT_SRELC);
7658 sym_name = (char *) h->root.root.string;
7659 }
7660#ifdef DEBUG
7661 printf ("Encountered a complex symbol!");
7662 printf (" (input_bfd %s, section %s, reloc %ld\n",
7663 input_bfd->filename, reloc_sec->name, i);
7664 printf (" symbol: idx %8.8lx, name %s\n",
7665 index, sym_name);
7666 printf (" reloc : info %8.8lx, addr %8.8lx\n",
7667 rel->r_info, addr);
7668 printf (" Evaluating '%s' ...\n ", sym_name);
7669#endif
7670 if (eval_symbol (& result, sym_name, & sym_name, input_bfd,
7671 finfo, addr, section_offset, locsymcount,
7672 signed_p))
7673 /* Symbol evaluated OK. Update to absolute value. */
7674 set_symbol_value (input_bfd, finfo, index, result);
7675
7676 else
7677 result = FALSE;
7678 }
7679
7680 if (internal_relocs != elf_section_data (reloc_sec)->relocs)
7681 free (internal_relocs);
7682 }
7683
7684 /* If nothing went wrong, then we adjusted
7685 everything we wanted to adjust. */
7686 return result;
7687}
7688
7689static void
7690put_value (bfd_vma size,
7691 unsigned long chunksz,
7692 bfd * input_bfd,
7693 bfd_vma x,
7694 bfd_byte * location)
7695{
7696 location += (size - chunksz);
7697
7698 for (; size; size -= chunksz, location -= chunksz, x >>= (chunksz * 8))
7699 {
7700 switch (chunksz)
7701 {
7702 default:
7703 case 0:
7704 abort ();
7705 case 1:
7706 bfd_put_8 (input_bfd, x, location);
7707 break;
7708 case 2:
7709 bfd_put_16 (input_bfd, x, location);
7710 break;
7711 case 4:
7712 bfd_put_32 (input_bfd, x, location);
7713 break;
7714 case 8:
7715#ifdef BFD64
7716 bfd_put_64 (input_bfd, x, location);
7717#else
7718 abort ();
7719#endif
7720 break;
7721 }
7722 }
7723}
7724
7725static bfd_vma
7726get_value (bfd_vma size,
7727 unsigned long chunksz,
7728 bfd * input_bfd,
7729 bfd_byte * location)
7730{
7731 bfd_vma x = 0;
7732
7733 for (; size; size -= chunksz, location += chunksz)
7734 {
7735 switch (chunksz)
7736 {
7737 default:
7738 case 0:
7739 abort ();
7740 case 1:
7741 x = (x << (8 * chunksz)) | bfd_get_8 (input_bfd, location);
7742 break;
7743 case 2:
7744 x = (x << (8 * chunksz)) | bfd_get_16 (input_bfd, location);
7745 break;
7746 case 4:
7747 x = (x << (8 * chunksz)) | bfd_get_32 (input_bfd, location);
7748 break;
7749 case 8:
7750#ifdef BFD64
7751 x = (x << (8 * chunksz)) | bfd_get_64 (input_bfd, location);
7752#else
7753 abort ();
7754#endif
7755 break;
7756 }
7757 }
7758 return x;
7759}
7760
7761static void
7762decode_complex_addend
7763 (unsigned long * start, /* in bits */
7764 unsigned long * oplen, /* in bits */
7765 unsigned long * len, /* in bits */
7766 unsigned long * wordsz, /* in bytes */
7767 unsigned long * chunksz, /* in bytes */
7768 unsigned long * lsb0_p,
7769 unsigned long * signed_p,
7770 unsigned long * trunc_p,
7771 unsigned long encoded)
7772{
7773 * start = encoded & 0x3F;
7774 * len = (encoded >> 6) & 0x3F;
7775 * oplen = (encoded >> 12) & 0x3F;
7776 * wordsz = (encoded >> 18) & 0xF;
7777 * chunksz = (encoded >> 22) & 0xF;
7778 * lsb0_p = (encoded >> 27) & 1;
7779 * signed_p = (encoded >> 28) & 1;
7780 * trunc_p = (encoded >> 29) & 1;
7781}
7782
7783void
7784bfd_elf_perform_complex_relocation
7785 (bfd * output_bfd ATTRIBUTE_UNUSED,
7786 struct bfd_link_info * info,
7787 bfd * input_bfd,
7788 asection * input_section,
7789 bfd_byte * contents,
7790 Elf_Internal_Rela * rel,
7791 Elf_Internal_Sym * local_syms,
7792 asection ** local_sections)
7793{
7794 const struct elf_backend_data * bed;
7795 Elf_Internal_Shdr * symtab_hdr;
7796 asection * sec;
7797 bfd_vma relocation = 0, shift, x;
947844a3 7798 bfd_vma r_symndx;
d9352518
DB
7799 bfd_vma mask;
7800 unsigned long start, oplen, len, wordsz,
7801 chunksz, lsb0_p, signed_p, trunc_p;
7802
7803 /* Perform this reloc, since it is complex.
7804 (this is not to say that it necessarily refers to a complex
7805 symbol; merely that it is a self-describing CGEN based reloc.
7806 i.e. the addend has the complete reloc information (bit start, end,
7807 word size, etc) encoded within it.). */
7808 r_symndx = ELF32_R_SYM (rel->r_info);
7809 bed = get_elf_backend_data (input_bfd);
7810 if (bed->s->arch_size == 64)
7811 r_symndx >>= 24;
7812
7813#ifdef DEBUG
7814 printf ("Performing complex relocation %ld...\n", r_symndx);
7815#endif
7816
7817 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7818 if (r_symndx < symtab_hdr->sh_info)
7819 {
7820 /* The symbol is local. */
7821 Elf_Internal_Sym * sym;
7822
7823 sym = local_syms + r_symndx;
7824 sec = local_sections [r_symndx];
7825 relocation = sym->st_value;
7826 if (sym->st_shndx > SHN_UNDEF &&
7827 sym->st_shndx < SHN_LORESERVE)
7828 relocation += (sec->output_offset +
7829 sec->output_section->vma);
7830 }
7831 else
7832 {
7833 /* The symbol is global. */
7834 struct elf_link_hash_entry **sym_hashes;
7835 struct elf_link_hash_entry * h;
7836
7837 sym_hashes = elf_sym_hashes (input_bfd);
7838 h = sym_hashes [r_symndx];
7839
7840 while (h->root.type == bfd_link_hash_indirect
7841 || h->root.type == bfd_link_hash_warning)
7842 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7843
7844 if (h->root.type == bfd_link_hash_defined
7845 || h->root.type == bfd_link_hash_defweak)
7846 {
7847 sec = h->root.u.def.section;
7848 relocation = h->root.u.def.value;
7849
7850 if (! bfd_is_abs_section (sec))
7851 relocation += (sec->output_section->vma
7852 + sec->output_offset);
7853 }
7854 if (h->root.type == bfd_link_hash_undefined
7855 && !((*info->callbacks->undefined_symbol)
7856 (info, h->root.root.string, input_bfd,
7857 input_section, rel->r_offset,
7858 info->unresolved_syms_in_objects == RM_GENERATE_ERROR
7859 || ELF_ST_VISIBILITY (h->other))))
7860 return;
7861 }
7862
7863 decode_complex_addend (& start, & oplen, & len, & wordsz,
7864 & chunksz, & lsb0_p, & signed_p,
7865 & trunc_p, rel->r_addend);
7866
7867 mask = (((1L << (len - 1)) - 1) << 1) | 1;
7868
7869 if (lsb0_p)
7870 shift = (start + 1) - len;
7871 else
7872 shift = (8 * wordsz) - (start + len);
7873
7874 x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
7875
7876#ifdef DEBUG
7877 printf ("Doing complex reloc: "
7878 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
7879 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
7880 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
7881 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
7882 oplen, x, mask, relocation);
7883#endif
7884
7885 if (! trunc_p)
7886 {
7887 /* Now do an overflow check. */
7888 if (bfd_check_overflow ((signed_p ?
7889 complain_overflow_signed :
7890 complain_overflow_unsigned),
7891 len, 0, (8 * wordsz),
7892 relocation) == bfd_reloc_overflow)
7893 (*_bfd_error_handler)
7894 ("%s (%s + 0x%lx): relocation overflow: 0x%lx %sdoes not fit "
7895 "within 0x%lx",
7896 input_bfd->filename, input_section->name, rel->r_offset,
7897 relocation, (signed_p ? "(signed) " : ""), mask);
7898 }
7899
7900 /* Do the deed. */
7901 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
7902
7903#ifdef DEBUG
7904 printf (" relocation: %8.8lx\n"
7905 " shifted mask: %8.8lx\n"
7906 " shifted/masked reloc: %8.8lx\n"
7907 " result: %8.8lx\n",
7908 relocation, (mask << shift),
7909 ((relocation & mask) << shift), x);
7910#endif
7911 put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
7912}
7913
c152c796
AM
7914/* When performing a relocatable link, the input relocations are
7915 preserved. But, if they reference global symbols, the indices
7916 referenced must be updated. Update all the relocations in
7917 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
7918
7919static void
7920elf_link_adjust_relocs (bfd *abfd,
7921 Elf_Internal_Shdr *rel_hdr,
7922 unsigned int count,
7923 struct elf_link_hash_entry **rel_hash)
7924{
7925 unsigned int i;
7926 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7927 bfd_byte *erela;
7928 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
7929 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
7930 bfd_vma r_type_mask;
7931 int r_sym_shift;
7932
7933 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
7934 {
7935 swap_in = bed->s->swap_reloc_in;
7936 swap_out = bed->s->swap_reloc_out;
7937 }
7938 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
7939 {
7940 swap_in = bed->s->swap_reloca_in;
7941 swap_out = bed->s->swap_reloca_out;
7942 }
7943 else
7944 abort ();
7945
7946 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
7947 abort ();
7948
7949 if (bed->s->arch_size == 32)
7950 {
7951 r_type_mask = 0xff;
7952 r_sym_shift = 8;
7953 }
7954 else
7955 {
7956 r_type_mask = 0xffffffff;
7957 r_sym_shift = 32;
7958 }
7959
7960 erela = rel_hdr->contents;
7961 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
7962 {
7963 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
7964 unsigned int j;
7965
7966 if (*rel_hash == NULL)
7967 continue;
7968
7969 BFD_ASSERT ((*rel_hash)->indx >= 0);
7970
7971 (*swap_in) (abfd, erela, irela);
7972 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
7973 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
7974 | (irela[j].r_info & r_type_mask));
7975 (*swap_out) (abfd, irela, erela);
7976 }
7977}
7978
7979struct elf_link_sort_rela
7980{
7981 union {
7982 bfd_vma offset;
7983 bfd_vma sym_mask;
7984 } u;
7985 enum elf_reloc_type_class type;
7986 /* We use this as an array of size int_rels_per_ext_rel. */
7987 Elf_Internal_Rela rela[1];
7988};
7989
7990static int
7991elf_link_sort_cmp1 (const void *A, const void *B)
7992{
7993 const struct elf_link_sort_rela *a = A;
7994 const struct elf_link_sort_rela *b = B;
7995 int relativea, relativeb;
7996
7997 relativea = a->type == reloc_class_relative;
7998 relativeb = b->type == reloc_class_relative;
7999
8000 if (relativea < relativeb)
8001 return 1;
8002 if (relativea > relativeb)
8003 return -1;
8004 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8005 return -1;
8006 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8007 return 1;
8008 if (a->rela->r_offset < b->rela->r_offset)
8009 return -1;
8010 if (a->rela->r_offset > b->rela->r_offset)
8011 return 1;
8012 return 0;
8013}
8014
8015static int
8016elf_link_sort_cmp2 (const void *A, const void *B)
8017{
8018 const struct elf_link_sort_rela *a = A;
8019 const struct elf_link_sort_rela *b = B;
8020 int copya, copyb;
8021
8022 if (a->u.offset < b->u.offset)
8023 return -1;
8024 if (a->u.offset > b->u.offset)
8025 return 1;
8026 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
8027 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
8028 if (copya < copyb)
8029 return -1;
8030 if (copya > copyb)
8031 return 1;
8032 if (a->rela->r_offset < b->rela->r_offset)
8033 return -1;
8034 if (a->rela->r_offset > b->rela->r_offset)
8035 return 1;
8036 return 0;
8037}
8038
8039static size_t
8040elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8041{
3410fea8 8042 asection *dynamic_relocs;
fc66a176
L
8043 asection *rela_dyn;
8044 asection *rel_dyn;
c152c796
AM
8045 bfd_size_type count, size;
8046 size_t i, ret, sort_elt, ext_size;
8047 bfd_byte *sort, *s_non_relative, *p;
8048 struct elf_link_sort_rela *sq;
8049 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8050 int i2e = bed->s->int_rels_per_ext_rel;
8051 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8052 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8053 struct bfd_link_order *lo;
8054 bfd_vma r_sym_mask;
3410fea8 8055 bfd_boolean use_rela;
c152c796 8056
3410fea8
NC
8057 /* Find a dynamic reloc section. */
8058 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8059 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8060 if (rela_dyn != NULL && rela_dyn->size > 0
8061 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 8062 {
3410fea8
NC
8063 bfd_boolean use_rela_initialised = FALSE;
8064
8065 /* This is just here to stop gcc from complaining.
8066 It's initialization checking code is not perfect. */
8067 use_rela = TRUE;
8068
8069 /* Both sections are present. Examine the sizes
8070 of the indirect sections to help us choose. */
8071 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8072 if (lo->type == bfd_indirect_link_order)
8073 {
8074 asection *o = lo->u.indirect.section;
8075
8076 if ((o->size % bed->s->sizeof_rela) == 0)
8077 {
8078 if ((o->size % bed->s->sizeof_rel) == 0)
8079 /* Section size is divisible by both rel and rela sizes.
8080 It is of no help to us. */
8081 ;
8082 else
8083 {
8084 /* Section size is only divisible by rela. */
8085 if (use_rela_initialised && (use_rela == FALSE))
8086 {
8087 _bfd_error_handler
8088 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8089 bfd_set_error (bfd_error_invalid_operation);
8090 return 0;
8091 }
8092 else
8093 {
8094 use_rela = TRUE;
8095 use_rela_initialised = TRUE;
8096 }
8097 }
8098 }
8099 else if ((o->size % bed->s->sizeof_rel) == 0)
8100 {
8101 /* Section size is only divisible by rel. */
8102 if (use_rela_initialised && (use_rela == TRUE))
8103 {
8104 _bfd_error_handler
8105 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8106 bfd_set_error (bfd_error_invalid_operation);
8107 return 0;
8108 }
8109 else
8110 {
8111 use_rela = FALSE;
8112 use_rela_initialised = TRUE;
8113 }
8114 }
8115 else
8116 {
8117 /* The section size is not divisible by either - something is wrong. */
8118 _bfd_error_handler
8119 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8120 bfd_set_error (bfd_error_invalid_operation);
8121 return 0;
8122 }
8123 }
8124
8125 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8126 if (lo->type == bfd_indirect_link_order)
8127 {
8128 asection *o = lo->u.indirect.section;
8129
8130 if ((o->size % bed->s->sizeof_rela) == 0)
8131 {
8132 if ((o->size % bed->s->sizeof_rel) == 0)
8133 /* Section size is divisible by both rel and rela sizes.
8134 It is of no help to us. */
8135 ;
8136 else
8137 {
8138 /* Section size is only divisible by rela. */
8139 if (use_rela_initialised && (use_rela == FALSE))
8140 {
8141 _bfd_error_handler
8142 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8143 bfd_set_error (bfd_error_invalid_operation);
8144 return 0;
8145 }
8146 else
8147 {
8148 use_rela = TRUE;
8149 use_rela_initialised = TRUE;
8150 }
8151 }
8152 }
8153 else if ((o->size % bed->s->sizeof_rel) == 0)
8154 {
8155 /* Section size is only divisible by rel. */
8156 if (use_rela_initialised && (use_rela == TRUE))
8157 {
8158 _bfd_error_handler
8159 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8160 bfd_set_error (bfd_error_invalid_operation);
8161 return 0;
8162 }
8163 else
8164 {
8165 use_rela = FALSE;
8166 use_rela_initialised = TRUE;
8167 }
8168 }
8169 else
8170 {
8171 /* The section size is not divisible by either - something is wrong. */
8172 _bfd_error_handler
8173 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8174 bfd_set_error (bfd_error_invalid_operation);
8175 return 0;
8176 }
8177 }
8178
8179 if (! use_rela_initialised)
8180 /* Make a guess. */
8181 use_rela = TRUE;
c152c796 8182 }
fc66a176
L
8183 else if (rela_dyn != NULL && rela_dyn->size > 0)
8184 use_rela = TRUE;
8185 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 8186 use_rela = FALSE;
c152c796 8187 else
fc66a176 8188 return 0;
3410fea8
NC
8189
8190 if (use_rela)
c152c796 8191 {
3410fea8 8192 dynamic_relocs = rela_dyn;
c152c796
AM
8193 ext_size = bed->s->sizeof_rela;
8194 swap_in = bed->s->swap_reloca_in;
8195 swap_out = bed->s->swap_reloca_out;
8196 }
3410fea8
NC
8197 else
8198 {
8199 dynamic_relocs = rel_dyn;
8200 ext_size = bed->s->sizeof_rel;
8201 swap_in = bed->s->swap_reloc_in;
8202 swap_out = bed->s->swap_reloc_out;
8203 }
c152c796
AM
8204
8205 size = 0;
3410fea8 8206 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 8207 if (lo->type == bfd_indirect_link_order)
3410fea8 8208 size += lo->u.indirect.section->size;
c152c796 8209
3410fea8 8210 if (size != dynamic_relocs->size)
c152c796
AM
8211 return 0;
8212
8213 sort_elt = (sizeof (struct elf_link_sort_rela)
8214 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
8215
8216 count = dynamic_relocs->size / ext_size;
c152c796 8217 sort = bfd_zmalloc (sort_elt * count);
3410fea8 8218
c152c796
AM
8219 if (sort == NULL)
8220 {
8221 (*info->callbacks->warning)
8222 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8223 return 0;
8224 }
8225
8226 if (bed->s->arch_size == 32)
8227 r_sym_mask = ~(bfd_vma) 0xff;
8228 else
8229 r_sym_mask = ~(bfd_vma) 0xffffffff;
8230
3410fea8 8231 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8232 if (lo->type == bfd_indirect_link_order)
8233 {
8234 bfd_byte *erel, *erelend;
8235 asection *o = lo->u.indirect.section;
8236
1da212d6
AM
8237 if (o->contents == NULL && o->size != 0)
8238 {
8239 /* This is a reloc section that is being handled as a normal
8240 section. See bfd_section_from_shdr. We can't combine
8241 relocs in this case. */
8242 free (sort);
8243 return 0;
8244 }
c152c796 8245 erel = o->contents;
eea6121a 8246 erelend = o->contents + o->size;
c152c796 8247 p = sort + o->output_offset / ext_size * sort_elt;
3410fea8 8248
c152c796
AM
8249 while (erel < erelend)
8250 {
8251 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 8252
c152c796
AM
8253 (*swap_in) (abfd, erel, s->rela);
8254 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
8255 s->u.sym_mask = r_sym_mask;
8256 p += sort_elt;
8257 erel += ext_size;
8258 }
8259 }
8260
8261 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8262
8263 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8264 {
8265 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8266 if (s->type != reloc_class_relative)
8267 break;
8268 }
8269 ret = i;
8270 s_non_relative = p;
8271
8272 sq = (struct elf_link_sort_rela *) s_non_relative;
8273 for (; i < count; i++, p += sort_elt)
8274 {
8275 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8276 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8277 sq = sp;
8278 sp->u.offset = sq->rela->r_offset;
8279 }
8280
8281 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8282
3410fea8 8283 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
8284 if (lo->type == bfd_indirect_link_order)
8285 {
8286 bfd_byte *erel, *erelend;
8287 asection *o = lo->u.indirect.section;
8288
8289 erel = o->contents;
eea6121a 8290 erelend = o->contents + o->size;
c152c796
AM
8291 p = sort + o->output_offset / ext_size * sort_elt;
8292 while (erel < erelend)
8293 {
8294 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8295 (*swap_out) (abfd, s->rela, erel);
8296 p += sort_elt;
8297 erel += ext_size;
8298 }
8299 }
8300
8301 free (sort);
3410fea8 8302 *psec = dynamic_relocs;
c152c796
AM
8303 return ret;
8304}
8305
8306/* Flush the output symbols to the file. */
8307
8308static bfd_boolean
8309elf_link_flush_output_syms (struct elf_final_link_info *finfo,
8310 const struct elf_backend_data *bed)
8311{
8312 if (finfo->symbuf_count > 0)
8313 {
8314 Elf_Internal_Shdr *hdr;
8315 file_ptr pos;
8316 bfd_size_type amt;
8317
8318 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
8319 pos = hdr->sh_offset + hdr->sh_size;
8320 amt = finfo->symbuf_count * bed->s->sizeof_sym;
8321 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
8322 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
8323 return FALSE;
8324
8325 hdr->sh_size += amt;
8326 finfo->symbuf_count = 0;
8327 }
8328
8329 return TRUE;
8330}
8331
8332/* Add a symbol to the output symbol table. */
8333
8334static bfd_boolean
8335elf_link_output_sym (struct elf_final_link_info *finfo,
8336 const char *name,
8337 Elf_Internal_Sym *elfsym,
8338 asection *input_sec,
8339 struct elf_link_hash_entry *h)
8340{
8341 bfd_byte *dest;
8342 Elf_External_Sym_Shndx *destshndx;
8343 bfd_boolean (*output_symbol_hook)
8344 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8345 struct elf_link_hash_entry *);
8346 const struct elf_backend_data *bed;
8347
8348 bed = get_elf_backend_data (finfo->output_bfd);
8349 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8350 if (output_symbol_hook != NULL)
8351 {
8352 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
8353 return FALSE;
8354 }
8355
8356 if (name == NULL || *name == '\0')
8357 elfsym->st_name = 0;
8358 else if (input_sec->flags & SEC_EXCLUDE)
8359 elfsym->st_name = 0;
8360 else
8361 {
8362 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
8363 name, TRUE, FALSE);
8364 if (elfsym->st_name == (unsigned long) -1)
8365 return FALSE;
8366 }
8367
8368 if (finfo->symbuf_count >= finfo->symbuf_size)
8369 {
8370 if (! elf_link_flush_output_syms (finfo, bed))
8371 return FALSE;
8372 }
8373
8374 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
8375 destshndx = finfo->symshndxbuf;
8376 if (destshndx != NULL)
8377 {
8378 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
8379 {
8380 bfd_size_type amt;
8381
8382 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
8383 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
8384 if (destshndx == NULL)
8385 return FALSE;
8386 memset ((char *) destshndx + amt, 0, amt);
8387 finfo->shndxbuf_size *= 2;
8388 }
8389 destshndx += bfd_get_symcount (finfo->output_bfd);
8390 }
8391
8392 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
8393 finfo->symbuf_count += 1;
8394 bfd_get_symcount (finfo->output_bfd) += 1;
8395
8396 return TRUE;
8397}
8398
c0d5a53d
L
8399/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8400
8401static bfd_boolean
8402check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8403{
8404 if (sym->st_shndx > SHN_HIRESERVE)
8405 {
8406 /* The gABI doesn't support dynamic symbols in output sections
8407 beyond 64k. */
8408 (*_bfd_error_handler)
8409 (_("%B: Too many sections: %d (>= %d)"),
8410 abfd, bfd_count_sections (abfd), SHN_LORESERVE);
8411 bfd_set_error (bfd_error_nonrepresentable_section);
8412 return FALSE;
8413 }
8414 return TRUE;
8415}
8416
c152c796
AM
8417/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8418 allowing an unsatisfied unversioned symbol in the DSO to match a
8419 versioned symbol that would normally require an explicit version.
8420 We also handle the case that a DSO references a hidden symbol
8421 which may be satisfied by a versioned symbol in another DSO. */
8422
8423static bfd_boolean
8424elf_link_check_versioned_symbol (struct bfd_link_info *info,
8425 const struct elf_backend_data *bed,
8426 struct elf_link_hash_entry *h)
8427{
8428 bfd *abfd;
8429 struct elf_link_loaded_list *loaded;
8430
8431 if (!is_elf_hash_table (info->hash))
8432 return FALSE;
8433
8434 switch (h->root.type)
8435 {
8436 default:
8437 abfd = NULL;
8438 break;
8439
8440 case bfd_link_hash_undefined:
8441 case bfd_link_hash_undefweak:
8442 abfd = h->root.u.undef.abfd;
8443 if ((abfd->flags & DYNAMIC) == 0
e56f61be 8444 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
8445 return FALSE;
8446 break;
8447
8448 case bfd_link_hash_defined:
8449 case bfd_link_hash_defweak:
8450 abfd = h->root.u.def.section->owner;
8451 break;
8452
8453 case bfd_link_hash_common:
8454 abfd = h->root.u.c.p->section->owner;
8455 break;
8456 }
8457 BFD_ASSERT (abfd != NULL);
8458
8459 for (loaded = elf_hash_table (info)->loaded;
8460 loaded != NULL;
8461 loaded = loaded->next)
8462 {
8463 bfd *input;
8464 Elf_Internal_Shdr *hdr;
8465 bfd_size_type symcount;
8466 bfd_size_type extsymcount;
8467 bfd_size_type extsymoff;
8468 Elf_Internal_Shdr *versymhdr;
8469 Elf_Internal_Sym *isym;
8470 Elf_Internal_Sym *isymend;
8471 Elf_Internal_Sym *isymbuf;
8472 Elf_External_Versym *ever;
8473 Elf_External_Versym *extversym;
8474
8475 input = loaded->abfd;
8476
8477 /* We check each DSO for a possible hidden versioned definition. */
8478 if (input == abfd
8479 || (input->flags & DYNAMIC) == 0
8480 || elf_dynversym (input) == 0)
8481 continue;
8482
8483 hdr = &elf_tdata (input)->dynsymtab_hdr;
8484
8485 symcount = hdr->sh_size / bed->s->sizeof_sym;
8486 if (elf_bad_symtab (input))
8487 {
8488 extsymcount = symcount;
8489 extsymoff = 0;
8490 }
8491 else
8492 {
8493 extsymcount = symcount - hdr->sh_info;
8494 extsymoff = hdr->sh_info;
8495 }
8496
8497 if (extsymcount == 0)
8498 continue;
8499
8500 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
8501 NULL, NULL, NULL);
8502 if (isymbuf == NULL)
8503 return FALSE;
8504
8505 /* Read in any version definitions. */
8506 versymhdr = &elf_tdata (input)->dynversym_hdr;
8507 extversym = bfd_malloc (versymhdr->sh_size);
8508 if (extversym == NULL)
8509 goto error_ret;
8510
8511 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
8512 || (bfd_bread (extversym, versymhdr->sh_size, input)
8513 != versymhdr->sh_size))
8514 {
8515 free (extversym);
8516 error_ret:
8517 free (isymbuf);
8518 return FALSE;
8519 }
8520
8521 ever = extversym + extsymoff;
8522 isymend = isymbuf + extsymcount;
8523 for (isym = isymbuf; isym < isymend; isym++, ever++)
8524 {
8525 const char *name;
8526 Elf_Internal_Versym iver;
8527 unsigned short version_index;
8528
8529 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
8530 || isym->st_shndx == SHN_UNDEF)
8531 continue;
8532
8533 name = bfd_elf_string_from_elf_section (input,
8534 hdr->sh_link,
8535 isym->st_name);
8536 if (strcmp (name, h->root.root.string) != 0)
8537 continue;
8538
8539 _bfd_elf_swap_versym_in (input, ever, &iver);
8540
8541 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
8542 {
8543 /* If we have a non-hidden versioned sym, then it should
8544 have provided a definition for the undefined sym. */
8545 abort ();
8546 }
8547
8548 version_index = iver.vs_vers & VERSYM_VERSION;
8549 if (version_index == 1 || version_index == 2)
8550 {
8551 /* This is the base or first version. We can use it. */
8552 free (extversym);
8553 free (isymbuf);
8554 return TRUE;
8555 }
8556 }
8557
8558 free (extversym);
8559 free (isymbuf);
8560 }
8561
8562 return FALSE;
8563}
8564
8565/* Add an external symbol to the symbol table. This is called from
8566 the hash table traversal routine. When generating a shared object,
8567 we go through the symbol table twice. The first time we output
8568 anything that might have been forced to local scope in a version
8569 script. The second time we output the symbols that are still
8570 global symbols. */
8571
8572static bfd_boolean
8573elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
8574{
8575 struct elf_outext_info *eoinfo = data;
8576 struct elf_final_link_info *finfo = eoinfo->finfo;
8577 bfd_boolean strip;
8578 Elf_Internal_Sym sym;
8579 asection *input_sec;
8580 const struct elf_backend_data *bed;
8581
8582 if (h->root.type == bfd_link_hash_warning)
8583 {
8584 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8585 if (h->root.type == bfd_link_hash_new)
8586 return TRUE;
8587 }
8588
8589 /* Decide whether to output this symbol in this pass. */
8590 if (eoinfo->localsyms)
8591 {
f5385ebf 8592 if (!h->forced_local)
c152c796
AM
8593 return TRUE;
8594 }
8595 else
8596 {
f5385ebf 8597 if (h->forced_local)
c152c796
AM
8598 return TRUE;
8599 }
8600
8601 bed = get_elf_backend_data (finfo->output_bfd);
8602
12ac1cf5 8603 if (h->root.type == bfd_link_hash_undefined)
c152c796 8604 {
12ac1cf5
NC
8605 /* If we have an undefined symbol reference here then it must have
8606 come from a shared library that is being linked in. (Undefined
8607 references in regular files have already been handled). */
8608 bfd_boolean ignore_undef = FALSE;
8609
8610 /* Some symbols may be special in that the fact that they're
8611 undefined can be safely ignored - let backend determine that. */
8612 if (bed->elf_backend_ignore_undef_symbol)
8613 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
8614
8615 /* If we are reporting errors for this situation then do so now. */
8616 if (ignore_undef == FALSE
8617 && h->ref_dynamic
8618 && ! h->ref_regular
8619 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
8620 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
c152c796 8621 {
12ac1cf5
NC
8622 if (! (finfo->info->callbacks->undefined_symbol
8623 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
8624 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
8625 {
8626 eoinfo->failed = TRUE;
8627 return FALSE;
8628 }
c152c796
AM
8629 }
8630 }
8631
8632 /* We should also warn if a forced local symbol is referenced from
8633 shared libraries. */
8634 if (! finfo->info->relocatable
8635 && (! finfo->info->shared)
f5385ebf
AM
8636 && h->forced_local
8637 && h->ref_dynamic
8638 && !h->dynamic_def
8639 && !h->dynamic_weak
c152c796
AM
8640 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
8641 {
8642 (*_bfd_error_handler)
d003868e 8643 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
cfca085c
L
8644 finfo->output_bfd,
8645 h->root.u.def.section == bfd_abs_section_ptr
8646 ? finfo->output_bfd : h->root.u.def.section->owner,
c152c796
AM
8647 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
8648 ? "internal"
8649 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
d003868e
AM
8650 ? "hidden" : "local",
8651 h->root.root.string);
c152c796
AM
8652 eoinfo->failed = TRUE;
8653 return FALSE;
8654 }
8655
8656 /* We don't want to output symbols that have never been mentioned by
8657 a regular file, or that we have been told to strip. However, if
8658 h->indx is set to -2, the symbol is used by a reloc and we must
8659 output it. */
8660 if (h->indx == -2)
8661 strip = FALSE;
f5385ebf 8662 else if ((h->def_dynamic
77cfaee6
AM
8663 || h->ref_dynamic
8664 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
8665 && !h->def_regular
8666 && !h->ref_regular)
c152c796
AM
8667 strip = TRUE;
8668 else if (finfo->info->strip == strip_all)
8669 strip = TRUE;
8670 else if (finfo->info->strip == strip_some
8671 && bfd_hash_lookup (finfo->info->keep_hash,
8672 h->root.root.string, FALSE, FALSE) == NULL)
8673 strip = TRUE;
8674 else if (finfo->info->strip_discarded
8675 && (h->root.type == bfd_link_hash_defined
8676 || h->root.type == bfd_link_hash_defweak)
8677 && elf_discarded_section (h->root.u.def.section))
8678 strip = TRUE;
8679 else
8680 strip = FALSE;
8681
8682 /* If we're stripping it, and it's not a dynamic symbol, there's
8683 nothing else to do unless it is a forced local symbol. */
8684 if (strip
8685 && h->dynindx == -1
f5385ebf 8686 && !h->forced_local)
c152c796
AM
8687 return TRUE;
8688
8689 sym.st_value = 0;
8690 sym.st_size = h->size;
8691 sym.st_other = h->other;
f5385ebf 8692 if (h->forced_local)
c152c796
AM
8693 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
8694 else if (h->root.type == bfd_link_hash_undefweak
8695 || h->root.type == bfd_link_hash_defweak)
8696 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
8697 else
8698 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
8699
8700 switch (h->root.type)
8701 {
8702 default:
8703 case bfd_link_hash_new:
8704 case bfd_link_hash_warning:
8705 abort ();
8706 return FALSE;
8707
8708 case bfd_link_hash_undefined:
8709 case bfd_link_hash_undefweak:
8710 input_sec = bfd_und_section_ptr;
8711 sym.st_shndx = SHN_UNDEF;
8712 break;
8713
8714 case bfd_link_hash_defined:
8715 case bfd_link_hash_defweak:
8716 {
8717 input_sec = h->root.u.def.section;
8718 if (input_sec->output_section != NULL)
8719 {
8720 sym.st_shndx =
8721 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
8722 input_sec->output_section);
8723 if (sym.st_shndx == SHN_BAD)
8724 {
8725 (*_bfd_error_handler)
d003868e
AM
8726 (_("%B: could not find output section %A for input section %A"),
8727 finfo->output_bfd, input_sec->output_section, input_sec);
c152c796
AM
8728 eoinfo->failed = TRUE;
8729 return FALSE;
8730 }
8731
8732 /* ELF symbols in relocatable files are section relative,
8733 but in nonrelocatable files they are virtual
8734 addresses. */
8735 sym.st_value = h->root.u.def.value + input_sec->output_offset;
8736 if (! finfo->info->relocatable)
8737 {
8738 sym.st_value += input_sec->output_section->vma;
8739 if (h->type == STT_TLS)
8740 {
8741 /* STT_TLS symbols are relative to PT_TLS segment
8742 base. */
8743 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
8744 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
8745 }
8746 }
8747 }
8748 else
8749 {
8750 BFD_ASSERT (input_sec->owner == NULL
8751 || (input_sec->owner->flags & DYNAMIC) != 0);
8752 sym.st_shndx = SHN_UNDEF;
8753 input_sec = bfd_und_section_ptr;
8754 }
8755 }
8756 break;
8757
8758 case bfd_link_hash_common:
8759 input_sec = h->root.u.c.p->section;
a4d8e49b 8760 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
8761 sym.st_value = 1 << h->root.u.c.p->alignment_power;
8762 break;
8763
8764 case bfd_link_hash_indirect:
8765 /* These symbols are created by symbol versioning. They point
8766 to the decorated version of the name. For example, if the
8767 symbol foo@@GNU_1.2 is the default, which should be used when
8768 foo is used with no version, then we add an indirect symbol
8769 foo which points to foo@@GNU_1.2. We ignore these symbols,
8770 since the indirected symbol is already in the hash table. */
8771 return TRUE;
8772 }
8773
8774 /* Give the processor backend a chance to tweak the symbol value,
8775 and also to finish up anything that needs to be done for this
8776 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
8777 forced local syms when non-shared is due to a historical quirk. */
8778 if ((h->dynindx != -1
f5385ebf 8779 || h->forced_local)
c152c796
AM
8780 && ((finfo->info->shared
8781 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8782 || h->root.type != bfd_link_hash_undefweak))
f5385ebf 8783 || !h->forced_local)
c152c796
AM
8784 && elf_hash_table (finfo->info)->dynamic_sections_created)
8785 {
8786 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8787 (finfo->output_bfd, finfo->info, h, &sym)))
8788 {
8789 eoinfo->failed = TRUE;
8790 return FALSE;
8791 }
8792 }
8793
8794 /* If we are marking the symbol as undefined, and there are no
8795 non-weak references to this symbol from a regular object, then
8796 mark the symbol as weak undefined; if there are non-weak
8797 references, mark the symbol as strong. We can't do this earlier,
8798 because it might not be marked as undefined until the
8799 finish_dynamic_symbol routine gets through with it. */
8800 if (sym.st_shndx == SHN_UNDEF
f5385ebf 8801 && h->ref_regular
c152c796
AM
8802 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
8803 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
8804 {
8805 int bindtype;
8806
f5385ebf 8807 if (h->ref_regular_nonweak)
c152c796
AM
8808 bindtype = STB_GLOBAL;
8809 else
8810 bindtype = STB_WEAK;
8811 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
8812 }
8813
8814 /* If a non-weak symbol with non-default visibility is not defined
8815 locally, it is a fatal error. */
8816 if (! finfo->info->relocatable
8817 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
8818 && ELF_ST_BIND (sym.st_info) != STB_WEAK
8819 && h->root.type == bfd_link_hash_undefined
f5385ebf 8820 && !h->def_regular)
c152c796
AM
8821 {
8822 (*_bfd_error_handler)
d003868e
AM
8823 (_("%B: %s symbol `%s' isn't defined"),
8824 finfo->output_bfd,
8825 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
8826 ? "protected"
8827 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
8828 ? "internal" : "hidden",
8829 h->root.root.string);
c152c796
AM
8830 eoinfo->failed = TRUE;
8831 return FALSE;
8832 }
8833
8834 /* If this symbol should be put in the .dynsym section, then put it
8835 there now. We already know the symbol index. We also fill in
8836 the entry in the .hash section. */
8837 if (h->dynindx != -1
8838 && elf_hash_table (finfo->info)->dynamic_sections_created)
8839 {
c152c796
AM
8840 bfd_byte *esym;
8841
8842 sym.st_name = h->dynstr_index;
8843 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
c0d5a53d
L
8844 if (! check_dynsym (finfo->output_bfd, &sym))
8845 {
8846 eoinfo->failed = TRUE;
8847 return FALSE;
8848 }
c152c796
AM
8849 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
8850
fdc90cb4
JJ
8851 if (finfo->hash_sec != NULL)
8852 {
8853 size_t hash_entry_size;
8854 bfd_byte *bucketpos;
8855 bfd_vma chain;
41198d0c
L
8856 size_t bucketcount;
8857 size_t bucket;
8858
8859 bucketcount = elf_hash_table (finfo->info)->bucketcount;
8860 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
8861
8862 hash_entry_size
8863 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
8864 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
8865 + (bucket + 2) * hash_entry_size);
8866 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
8867 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
8868 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
8869 ((bfd_byte *) finfo->hash_sec->contents
8870 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
8871 }
c152c796
AM
8872
8873 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
8874 {
8875 Elf_Internal_Versym iversym;
8876 Elf_External_Versym *eversym;
8877
f5385ebf 8878 if (!h->def_regular)
c152c796
AM
8879 {
8880 if (h->verinfo.verdef == NULL)
8881 iversym.vs_vers = 0;
8882 else
8883 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
8884 }
8885 else
8886 {
8887 if (h->verinfo.vertree == NULL)
8888 iversym.vs_vers = 1;
8889 else
8890 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
3e3b46e5
PB
8891 if (finfo->info->create_default_symver)
8892 iversym.vs_vers++;
c152c796
AM
8893 }
8894
f5385ebf 8895 if (h->hidden)
c152c796
AM
8896 iversym.vs_vers |= VERSYM_HIDDEN;
8897
8898 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
8899 eversym += h->dynindx;
8900 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
8901 }
8902 }
8903
8904 /* If we're stripping it, then it was just a dynamic symbol, and
8905 there's nothing else to do. */
8906 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
8907 return TRUE;
8908
8909 h->indx = bfd_get_symcount (finfo->output_bfd);
8910
8911 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
8912 {
8913 eoinfo->failed = TRUE;
8914 return FALSE;
8915 }
8916
8917 return TRUE;
8918}
8919
cdd3575c
AM
8920/* Return TRUE if special handling is done for relocs in SEC against
8921 symbols defined in discarded sections. */
8922
c152c796
AM
8923static bfd_boolean
8924elf_section_ignore_discarded_relocs (asection *sec)
8925{
8926 const struct elf_backend_data *bed;
8927
cdd3575c
AM
8928 switch (sec->sec_info_type)
8929 {
8930 case ELF_INFO_TYPE_STABS:
8931 case ELF_INFO_TYPE_EH_FRAME:
8932 return TRUE;
8933 default:
8934 break;
8935 }
c152c796
AM
8936
8937 bed = get_elf_backend_data (sec->owner);
8938 if (bed->elf_backend_ignore_discarded_relocs != NULL
8939 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
8940 return TRUE;
8941
8942 return FALSE;
8943}
8944
9e66c942
AM
8945/* Return a mask saying how ld should treat relocations in SEC against
8946 symbols defined in discarded sections. If this function returns
8947 COMPLAIN set, ld will issue a warning message. If this function
8948 returns PRETEND set, and the discarded section was link-once and the
8949 same size as the kept link-once section, ld will pretend that the
8950 symbol was actually defined in the kept section. Otherwise ld will
8951 zero the reloc (at least that is the intent, but some cooperation by
8952 the target dependent code is needed, particularly for REL targets). */
8953
8a696751
AM
8954unsigned int
8955_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 8956{
9e66c942 8957 if (sec->flags & SEC_DEBUGGING)
69d54b1b 8958 return PRETEND;
cdd3575c
AM
8959
8960 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 8961 return 0;
cdd3575c
AM
8962
8963 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 8964 return 0;
cdd3575c 8965
9e66c942 8966 return COMPLAIN | PRETEND;
cdd3575c
AM
8967}
8968
3d7f7666
L
8969/* Find a match between a section and a member of a section group. */
8970
8971static asection *
c0f00686
L
8972match_group_member (asection *sec, asection *group,
8973 struct bfd_link_info *info)
3d7f7666
L
8974{
8975 asection *first = elf_next_in_group (group);
8976 asection *s = first;
8977
8978 while (s != NULL)
8979 {
c0f00686 8980 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
8981 return s;
8982
83180ade 8983 s = elf_next_in_group (s);
3d7f7666
L
8984 if (s == first)
8985 break;
8986 }
8987
8988 return NULL;
8989}
8990
01b3c8ab 8991/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
8992 to replace it. Return the replacement if it is OK. Otherwise return
8993 NULL. */
01b3c8ab
L
8994
8995asection *
c0f00686 8996_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
8997{
8998 asection *kept;
8999
9000 kept = sec->kept_section;
9001 if (kept != NULL)
9002 {
c2370991 9003 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 9004 kept = match_group_member (sec, kept, info);
01b3c8ab
L
9005 if (kept != NULL && sec->size != kept->size)
9006 kept = NULL;
c2370991 9007 sec->kept_section = kept;
01b3c8ab
L
9008 }
9009 return kept;
9010}
9011
c152c796
AM
9012/* Link an input file into the linker output file. This function
9013 handles all the sections and relocations of the input file at once.
9014 This is so that we only have to read the local symbols once, and
9015 don't have to keep them in memory. */
9016
9017static bfd_boolean
9018elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
9019{
ece5ef60 9020 int (*relocate_section)
c152c796
AM
9021 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9022 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9023 bfd *output_bfd;
9024 Elf_Internal_Shdr *symtab_hdr;
9025 size_t locsymcount;
9026 size_t extsymoff;
9027 Elf_Internal_Sym *isymbuf;
9028 Elf_Internal_Sym *isym;
9029 Elf_Internal_Sym *isymend;
9030 long *pindex;
9031 asection **ppsection;
9032 asection *o;
9033 const struct elf_backend_data *bed;
c152c796
AM
9034 struct elf_link_hash_entry **sym_hashes;
9035
9036 output_bfd = finfo->output_bfd;
9037 bed = get_elf_backend_data (output_bfd);
9038 relocate_section = bed->elf_backend_relocate_section;
9039
9040 /* If this is a dynamic object, we don't want to do anything here:
9041 we don't want the local symbols, and we don't want the section
9042 contents. */
9043 if ((input_bfd->flags & DYNAMIC) != 0)
9044 return TRUE;
9045
c152c796
AM
9046 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9047 if (elf_bad_symtab (input_bfd))
9048 {
9049 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9050 extsymoff = 0;
9051 }
9052 else
9053 {
9054 locsymcount = symtab_hdr->sh_info;
9055 extsymoff = symtab_hdr->sh_info;
9056 }
9057
9058 /* Read the local symbols. */
9059 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9060 if (isymbuf == NULL && locsymcount != 0)
9061 {
9062 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
9063 finfo->internal_syms,
9064 finfo->external_syms,
9065 finfo->locsym_shndx);
9066 if (isymbuf == NULL)
9067 return FALSE;
9068 }
b31847af
DB
9069 /* evaluate_complex_relocation_symbols looks for symbols in
9070 finfo->internal_syms. */
9071 else if (isymbuf != NULL && locsymcount != 0)
9072 {
9073 bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
9074 finfo->internal_syms,
9075 finfo->external_syms,
9076 finfo->locsym_shndx);
9077 }
c152c796
AM
9078
9079 /* Find local symbol sections and adjust values of symbols in
9080 SEC_MERGE sections. Write out those local symbols we know are
9081 going into the output file. */
9082 isymend = isymbuf + locsymcount;
9083 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
9084 isym < isymend;
9085 isym++, pindex++, ppsection++)
9086 {
9087 asection *isec;
9088 const char *name;
9089 Elf_Internal_Sym osym;
9090
9091 *pindex = -1;
9092
9093 if (elf_bad_symtab (input_bfd))
9094 {
9095 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9096 {
9097 *ppsection = NULL;
9098 continue;
9099 }
9100 }
9101
9102 if (isym->st_shndx == SHN_UNDEF)
9103 isec = bfd_und_section_ptr;
9104 else if (isym->st_shndx < SHN_LORESERVE
9105 || isym->st_shndx > SHN_HIRESERVE)
9106 {
9107 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9108 if (isec
9109 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
9110 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9111 isym->st_value =
9112 _bfd_merged_section_offset (output_bfd, &isec,
9113 elf_section_data (isec)->sec_info,
753731ee 9114 isym->st_value);
c152c796
AM
9115 }
9116 else if (isym->st_shndx == SHN_ABS)
9117 isec = bfd_abs_section_ptr;
9118 else if (isym->st_shndx == SHN_COMMON)
9119 isec = bfd_com_section_ptr;
9120 else
9121 {
f02571c5
AM
9122 /* Don't attempt to output symbols with st_shnx in the
9123 reserved range other than SHN_ABS and SHN_COMMON. */
9124 *ppsection = NULL;
9125 continue;
c152c796
AM
9126 }
9127
9128 *ppsection = isec;
9129
9130 /* Don't output the first, undefined, symbol. */
9131 if (ppsection == finfo->sections)
9132 continue;
9133
9134 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9135 {
9136 /* We never output section symbols. Instead, we use the
9137 section symbol of the corresponding section in the output
9138 file. */
9139 continue;
9140 }
9141
9142 /* If we are stripping all symbols, we don't want to output this
9143 one. */
9144 if (finfo->info->strip == strip_all)
9145 continue;
9146
9147 /* If we are discarding all local symbols, we don't want to
9148 output this one. If we are generating a relocatable output
9149 file, then some of the local symbols may be required by
9150 relocs; we output them below as we discover that they are
9151 needed. */
9152 if (finfo->info->discard == discard_all)
9153 continue;
9154
9155 /* If this symbol is defined in a section which we are
f02571c5
AM
9156 discarding, we don't need to keep it. */
9157 if (isym->st_shndx != SHN_UNDEF
9158 && (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
ccf5f610 9159 && (isec == NULL
f02571c5
AM
9160 || bfd_section_removed_from_list (output_bfd,
9161 isec->output_section)))
e75a280b
L
9162 continue;
9163
c152c796
AM
9164 /* Get the name of the symbol. */
9165 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9166 isym->st_name);
9167 if (name == NULL)
9168 return FALSE;
9169
9170 /* See if we are discarding symbols with this name. */
9171 if ((finfo->info->strip == strip_some
9172 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
9173 == NULL))
9174 || (((finfo->info->discard == discard_sec_merge
9175 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
9176 || finfo->info->discard == discard_l)
9177 && bfd_is_local_label_name (input_bfd, name)))
9178 continue;
9179
9180 /* If we get here, we are going to output this symbol. */
9181
9182 osym = *isym;
9183
9184 /* Adjust the section index for the output file. */
9185 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9186 isec->output_section);
9187 if (osym.st_shndx == SHN_BAD)
9188 return FALSE;
9189
9190 *pindex = bfd_get_symcount (output_bfd);
9191
9192 /* ELF symbols in relocatable files are section relative, but
9193 in executable files they are virtual addresses. Note that
9194 this code assumes that all ELF sections have an associated
9195 BFD section with a reasonable value for output_offset; below
9196 we assume that they also have a reasonable value for
9197 output_section. Any special sections must be set up to meet
9198 these requirements. */
9199 osym.st_value += isec->output_offset;
9200 if (! finfo->info->relocatable)
9201 {
9202 osym.st_value += isec->output_section->vma;
9203 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
9204 {
9205 /* STT_TLS symbols are relative to PT_TLS segment base. */
9206 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
9207 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
9208 }
9209 }
9210
9211 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
9212 return FALSE;
9213 }
9214
d9352518
DB
9215 if (! evaluate_complex_relocation_symbols (input_bfd, finfo, locsymcount))
9216 return FALSE;
9217
c152c796
AM
9218 /* Relocate the contents of each section. */
9219 sym_hashes = elf_sym_hashes (input_bfd);
9220 for (o = input_bfd->sections; o != NULL; o = o->next)
9221 {
9222 bfd_byte *contents;
9223
9224 if (! o->linker_mark)
9225 {
9226 /* This section was omitted from the link. */
9227 continue;
9228 }
9229
9230 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 9231 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
9232 continue;
9233
9234 if ((o->flags & SEC_LINKER_CREATED) != 0)
9235 {
9236 /* Section was created by _bfd_elf_link_create_dynamic_sections
9237 or somesuch. */
9238 continue;
9239 }
9240
9241 /* Get the contents of the section. They have been cached by a
9242 relaxation routine. Note that o is a section in an input
9243 file, so the contents field will not have been set by any of
9244 the routines which work on output files. */
9245 if (elf_section_data (o)->this_hdr.contents != NULL)
9246 contents = elf_section_data (o)->this_hdr.contents;
9247 else
9248 {
eea6121a
AM
9249 bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
9250
c152c796 9251 contents = finfo->contents;
eea6121a 9252 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
c152c796
AM
9253 return FALSE;
9254 }
9255
9256 if ((o->flags & SEC_RELOC) != 0)
9257 {
9258 Elf_Internal_Rela *internal_relocs;
9259 bfd_vma r_type_mask;
9260 int r_sym_shift;
ece5ef60 9261 int ret;
c152c796
AM
9262
9263 /* Get the swapped relocs. */
9264 internal_relocs
9265 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
9266 finfo->internal_relocs, FALSE);
9267 if (internal_relocs == NULL
9268 && o->reloc_count > 0)
9269 return FALSE;
9270
9271 if (bed->s->arch_size == 32)
9272 {
9273 r_type_mask = 0xff;
9274 r_sym_shift = 8;
9275 }
9276 else
9277 {
9278 r_type_mask = 0xffffffff;
9279 r_sym_shift = 32;
9280 }
9281
9282 /* Run through the relocs looking for any against symbols
9283 from discarded sections and section symbols from
9284 removed link-once sections. Complain about relocs
9285 against discarded sections. Zero relocs against removed
e0ae6d6f 9286 link-once sections. */
c152c796
AM
9287 if (!elf_section_ignore_discarded_relocs (o))
9288 {
9289 Elf_Internal_Rela *rel, *relend;
8a696751 9290 unsigned int action = (*bed->action_discarded) (o);
c152c796
AM
9291
9292 rel = internal_relocs;
9293 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
9294 for ( ; rel < relend; rel++)
9295 {
9296 unsigned long r_symndx = rel->r_info >> r_sym_shift;
cdd3575c
AM
9297 asection **ps, *sec;
9298 struct elf_link_hash_entry *h = NULL;
9299 const char *sym_name;
c152c796 9300
ee75fd95
AM
9301 if (r_symndx == STN_UNDEF)
9302 continue;
9303
c152c796
AM
9304 if (r_symndx >= locsymcount
9305 || (elf_bad_symtab (input_bfd)
9306 && finfo->sections[r_symndx] == NULL))
9307 {
c152c796 9308 h = sym_hashes[r_symndx - extsymoff];
dce669a1 9309
8c19749a
NC
9310 /* Badly formatted input files can contain relocs that
9311 reference non-existant symbols. Check here so that
9312 we do not seg fault. */
9313 if (h == NULL)
9314 {
9315 char buffer [32];
9316
9317 sprintf_vma (buffer, rel->r_info);
9318 (*_bfd_error_handler)
9319 (_("error: %B contains a reloc (0x%s) for section %A "
9320 "that references a non-existent global symbol"),
9321 input_bfd, o, buffer);
9322 bfd_set_error (bfd_error_bad_value);
9323 return FALSE;
9324 }
3b36f7e6 9325
c152c796
AM
9326 while (h->root.type == bfd_link_hash_indirect
9327 || h->root.type == bfd_link_hash_warning)
9328 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9329
cdd3575c
AM
9330 if (h->root.type != bfd_link_hash_defined
9331 && h->root.type != bfd_link_hash_defweak)
9332 continue;
9333
9334 ps = &h->root.u.def.section;
9335 sym_name = h->root.root.string;
c152c796
AM
9336 }
9337 else
9338 {
cdd3575c
AM
9339 Elf_Internal_Sym *sym = isymbuf + r_symndx;
9340 ps = &finfo->sections[r_symndx];
26c61ae5
L
9341 sym_name = bfd_elf_sym_name (input_bfd,
9342 symtab_hdr,
9343 sym, *ps);
cdd3575c 9344 }
c152c796 9345
cdd3575c
AM
9346 /* Complain if the definition comes from a
9347 discarded section. */
9348 if ((sec = *ps) != NULL && elf_discarded_section (sec))
9349 {
87e5235d 9350 BFD_ASSERT (r_symndx != 0);
9e66c942 9351 if (action & COMPLAIN)
e1fffbe6
AM
9352 (*finfo->info->callbacks->einfo)
9353 (_("%X`%s' referenced in section `%A' of %B: "
58ac56d0 9354 "defined in discarded section `%A' of %B\n"),
e1fffbe6 9355 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 9356
87e5235d 9357 /* Try to do the best we can to support buggy old
e0ae6d6f 9358 versions of gcc. Pretend that the symbol is
87e5235d
AM
9359 really defined in the kept linkonce section.
9360 FIXME: This is quite broken. Modifying the
9361 symbol here means we will be changing all later
e0ae6d6f 9362 uses of the symbol, not just in this section. */
01b3c8ab 9363 if (action & PRETEND)
87e5235d 9364 {
01b3c8ab
L
9365 asection *kept;
9366
c0f00686
L
9367 kept = _bfd_elf_check_kept_section (sec,
9368 finfo->info);
01b3c8ab 9369 if (kept != NULL)
87e5235d
AM
9370 {
9371 *ps = kept;
9372 continue;
9373 }
9374 }
c152c796
AM
9375 }
9376 }
9377 }
9378
9379 /* Relocate the section by invoking a back end routine.
9380
9381 The back end routine is responsible for adjusting the
9382 section contents as necessary, and (if using Rela relocs
9383 and generating a relocatable output file) adjusting the
9384 reloc addend as necessary.
9385
9386 The back end routine does not have to worry about setting
9387 the reloc address or the reloc symbol index.
9388
9389 The back end routine is given a pointer to the swapped in
9390 internal symbols, and can access the hash table entries
9391 for the external symbols via elf_sym_hashes (input_bfd).
9392
9393 When generating relocatable output, the back end routine
9394 must handle STB_LOCAL/STT_SECTION symbols specially. The
9395 output symbol is going to be a section symbol
9396 corresponding to the output section, which will require
9397 the addend to be adjusted. */
9398
ece5ef60 9399 ret = (*relocate_section) (output_bfd, finfo->info,
c152c796
AM
9400 input_bfd, o, contents,
9401 internal_relocs,
9402 isymbuf,
ece5ef60
AM
9403 finfo->sections);
9404 if (!ret)
c152c796
AM
9405 return FALSE;
9406
ece5ef60
AM
9407 if (ret == 2
9408 || finfo->info->relocatable
9409 || finfo->info->emitrelocations)
c152c796
AM
9410 {
9411 Elf_Internal_Rela *irela;
9412 Elf_Internal_Rela *irelaend;
9413 bfd_vma last_offset;
9414 struct elf_link_hash_entry **rel_hash;
eac338cf 9415 struct elf_link_hash_entry **rel_hash_list;
c152c796
AM
9416 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
9417 unsigned int next_erel;
c152c796
AM
9418 bfd_boolean rela_normal;
9419
9420 input_rel_hdr = &elf_section_data (o)->rel_hdr;
9421 rela_normal = (bed->rela_normal
9422 && (input_rel_hdr->sh_entsize
9423 == bed->s->sizeof_rela));
9424
9425 /* Adjust the reloc addresses and symbol indices. */
9426
9427 irela = internal_relocs;
9428 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
9429 rel_hash = (elf_section_data (o->output_section)->rel_hashes
9430 + elf_section_data (o->output_section)->rel_count
9431 + elf_section_data (o->output_section)->rel_count2);
eac338cf 9432 rel_hash_list = rel_hash;
c152c796
AM
9433 last_offset = o->output_offset;
9434 if (!finfo->info->relocatable)
9435 last_offset += o->output_section->vma;
9436 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
9437 {
9438 unsigned long r_symndx;
9439 asection *sec;
9440 Elf_Internal_Sym sym;
9441
9442 if (next_erel == bed->s->int_rels_per_ext_rel)
9443 {
9444 rel_hash++;
9445 next_erel = 0;
9446 }
9447
9448 irela->r_offset = _bfd_elf_section_offset (output_bfd,
9449 finfo->info, o,
9450 irela->r_offset);
9451 if (irela->r_offset >= (bfd_vma) -2)
9452 {
9453 /* This is a reloc for a deleted entry or somesuch.
9454 Turn it into an R_*_NONE reloc, at the same
9455 offset as the last reloc. elf_eh_frame.c and
e460dd0d 9456 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
9457 being ordered. */
9458 irela->r_offset = last_offset;
9459 irela->r_info = 0;
9460 irela->r_addend = 0;
9461 continue;
9462 }
9463
9464 irela->r_offset += o->output_offset;
9465
9466 /* Relocs in an executable have to be virtual addresses. */
9467 if (!finfo->info->relocatable)
9468 irela->r_offset += o->output_section->vma;
9469
9470 last_offset = irela->r_offset;
9471
9472 r_symndx = irela->r_info >> r_sym_shift;
9473 if (r_symndx == STN_UNDEF)
9474 continue;
9475
9476 if (r_symndx >= locsymcount
9477 || (elf_bad_symtab (input_bfd)
9478 && finfo->sections[r_symndx] == NULL))
9479 {
9480 struct elf_link_hash_entry *rh;
9481 unsigned long indx;
9482
9483 /* This is a reloc against a global symbol. We
9484 have not yet output all the local symbols, so
9485 we do not know the symbol index of any global
9486 symbol. We set the rel_hash entry for this
9487 reloc to point to the global hash table entry
9488 for this symbol. The symbol index is then
ee75fd95 9489 set at the end of bfd_elf_final_link. */
c152c796
AM
9490 indx = r_symndx - extsymoff;
9491 rh = elf_sym_hashes (input_bfd)[indx];
9492 while (rh->root.type == bfd_link_hash_indirect
9493 || rh->root.type == bfd_link_hash_warning)
9494 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
9495
9496 /* Setting the index to -2 tells
9497 elf_link_output_extsym that this symbol is
9498 used by a reloc. */
9499 BFD_ASSERT (rh->indx < 0);
9500 rh->indx = -2;
9501
9502 *rel_hash = rh;
9503
9504 continue;
9505 }
9506
9507 /* This is a reloc against a local symbol. */
9508
9509 *rel_hash = NULL;
9510 sym = isymbuf[r_symndx];
9511 sec = finfo->sections[r_symndx];
9512 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
9513 {
9514 /* I suppose the backend ought to fill in the
9515 section of any STT_SECTION symbol against a
6a8d1586
AM
9516 processor specific section. */
9517 r_symndx = 0;
9518 if (bfd_is_abs_section (sec))
9519 ;
c152c796
AM
9520 else if (sec == NULL || sec->owner == NULL)
9521 {
9522 bfd_set_error (bfd_error_bad_value);
9523 return FALSE;
9524 }
9525 else
9526 {
6a8d1586
AM
9527 asection *osec = sec->output_section;
9528
9529 /* If we have discarded a section, the output
9530 section will be the absolute section. In
ab96bf03
AM
9531 case of discarded SEC_MERGE sections, use
9532 the kept section. relocate_section should
9533 have already handled discarded linkonce
9534 sections. */
6a8d1586
AM
9535 if (bfd_is_abs_section (osec)
9536 && sec->kept_section != NULL
9537 && sec->kept_section->output_section != NULL)
9538 {
9539 osec = sec->kept_section->output_section;
9540 irela->r_addend -= osec->vma;
9541 }
9542
9543 if (!bfd_is_abs_section (osec))
9544 {
9545 r_symndx = osec->target_index;
74541ad4
AM
9546 if (r_symndx == 0)
9547 {
9548 struct elf_link_hash_table *htab;
9549 asection *oi;
9550
9551 htab = elf_hash_table (finfo->info);
9552 oi = htab->text_index_section;
9553 if ((osec->flags & SEC_READONLY) == 0
9554 && htab->data_index_section != NULL)
9555 oi = htab->data_index_section;
9556
9557 if (oi != NULL)
9558 {
9559 irela->r_addend += osec->vma - oi->vma;
9560 r_symndx = oi->target_index;
9561 }
9562 }
9563
6a8d1586
AM
9564 BFD_ASSERT (r_symndx != 0);
9565 }
c152c796
AM
9566 }
9567
9568 /* Adjust the addend according to where the
9569 section winds up in the output section. */
9570 if (rela_normal)
9571 irela->r_addend += sec->output_offset;
9572 }
9573 else
9574 {
9575 if (finfo->indices[r_symndx] == -1)
9576 {
9577 unsigned long shlink;
9578 const char *name;
9579 asection *osec;
9580
9581 if (finfo->info->strip == strip_all)
9582 {
9583 /* You can't do ld -r -s. */
9584 bfd_set_error (bfd_error_invalid_operation);
9585 return FALSE;
9586 }
9587
9588 /* This symbol was skipped earlier, but
9589 since it is needed by a reloc, we
9590 must output it now. */
9591 shlink = symtab_hdr->sh_link;
9592 name = (bfd_elf_string_from_elf_section
9593 (input_bfd, shlink, sym.st_name));
9594 if (name == NULL)
9595 return FALSE;
9596
9597 osec = sec->output_section;
9598 sym.st_shndx =
9599 _bfd_elf_section_from_bfd_section (output_bfd,
9600 osec);
9601 if (sym.st_shndx == SHN_BAD)
9602 return FALSE;
9603
9604 sym.st_value += sec->output_offset;
9605 if (! finfo->info->relocatable)
9606 {
9607 sym.st_value += osec->vma;
9608 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
9609 {
9610 /* STT_TLS symbols are relative to PT_TLS
9611 segment base. */
9612 BFD_ASSERT (elf_hash_table (finfo->info)
9613 ->tls_sec != NULL);
9614 sym.st_value -= (elf_hash_table (finfo->info)
9615 ->tls_sec->vma);
9616 }
9617 }
9618
9619 finfo->indices[r_symndx]
9620 = bfd_get_symcount (output_bfd);
9621
9622 if (! elf_link_output_sym (finfo, name, &sym, sec,
9623 NULL))
9624 return FALSE;
9625 }
9626
9627 r_symndx = finfo->indices[r_symndx];
9628 }
9629
9630 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
9631 | (irela->r_info & r_type_mask));
9632 }
9633
9634 /* Swap out the relocs. */
c152c796 9635 if (input_rel_hdr->sh_size != 0
eac338cf
PB
9636 && !bed->elf_backend_emit_relocs (output_bfd, o,
9637 input_rel_hdr,
9638 internal_relocs,
9639 rel_hash_list))
c152c796
AM
9640 return FALSE;
9641
9642 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
9643 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
9644 {
9645 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
9646 * bed->s->int_rels_per_ext_rel);
eac338cf
PB
9647 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
9648 if (!bed->elf_backend_emit_relocs (output_bfd, o,
9649 input_rel_hdr2,
9650 internal_relocs,
9651 rel_hash_list))
c152c796
AM
9652 return FALSE;
9653 }
9654 }
9655 }
9656
9657 /* Write out the modified section contents. */
9658 if (bed->elf_backend_write_section
c7b8f16e
JB
9659 && (*bed->elf_backend_write_section) (output_bfd, finfo->info, o,
9660 contents))
c152c796
AM
9661 {
9662 /* Section written out. */
9663 }
9664 else switch (o->sec_info_type)
9665 {
9666 case ELF_INFO_TYPE_STABS:
9667 if (! (_bfd_write_section_stabs
9668 (output_bfd,
9669 &elf_hash_table (finfo->info)->stab_info,
9670 o, &elf_section_data (o)->sec_info, contents)))
9671 return FALSE;
9672 break;
9673 case ELF_INFO_TYPE_MERGE:
9674 if (! _bfd_write_merged_section (output_bfd, o,
9675 elf_section_data (o)->sec_info))
9676 return FALSE;
9677 break;
9678 case ELF_INFO_TYPE_EH_FRAME:
9679 {
9680 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
9681 o, contents))
9682 return FALSE;
9683 }
9684 break;
9685 default:
9686 {
c152c796
AM
9687 if (! (o->flags & SEC_EXCLUDE)
9688 && ! bfd_set_section_contents (output_bfd, o->output_section,
9689 contents,
9690 (file_ptr) o->output_offset,
eea6121a 9691 o->size))
c152c796
AM
9692 return FALSE;
9693 }
9694 break;
9695 }
9696 }
9697
9698 return TRUE;
9699}
9700
9701/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 9702 requested by the linker, and does not come from any input file. This
c152c796
AM
9703 is used to build constructor and destructor tables when linking
9704 with -Ur. */
9705
9706static bfd_boolean
9707elf_reloc_link_order (bfd *output_bfd,
9708 struct bfd_link_info *info,
9709 asection *output_section,
9710 struct bfd_link_order *link_order)
9711{
9712 reloc_howto_type *howto;
9713 long indx;
9714 bfd_vma offset;
9715 bfd_vma addend;
9716 struct elf_link_hash_entry **rel_hash_ptr;
9717 Elf_Internal_Shdr *rel_hdr;
9718 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
9719 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
9720 bfd_byte *erel;
9721 unsigned int i;
9722
9723 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
9724 if (howto == NULL)
9725 {
9726 bfd_set_error (bfd_error_bad_value);
9727 return FALSE;
9728 }
9729
9730 addend = link_order->u.reloc.p->addend;
9731
9732 /* Figure out the symbol index. */
9733 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
9734 + elf_section_data (output_section)->rel_count
9735 + elf_section_data (output_section)->rel_count2);
9736 if (link_order->type == bfd_section_reloc_link_order)
9737 {
9738 indx = link_order->u.reloc.p->u.section->target_index;
9739 BFD_ASSERT (indx != 0);
9740 *rel_hash_ptr = NULL;
9741 }
9742 else
9743 {
9744 struct elf_link_hash_entry *h;
9745
9746 /* Treat a reloc against a defined symbol as though it were
9747 actually against the section. */
9748 h = ((struct elf_link_hash_entry *)
9749 bfd_wrapped_link_hash_lookup (output_bfd, info,
9750 link_order->u.reloc.p->u.name,
9751 FALSE, FALSE, TRUE));
9752 if (h != NULL
9753 && (h->root.type == bfd_link_hash_defined
9754 || h->root.type == bfd_link_hash_defweak))
9755 {
9756 asection *section;
9757
9758 section = h->root.u.def.section;
9759 indx = section->output_section->target_index;
9760 *rel_hash_ptr = NULL;
9761 /* It seems that we ought to add the symbol value to the
9762 addend here, but in practice it has already been added
9763 because it was passed to constructor_callback. */
9764 addend += section->output_section->vma + section->output_offset;
9765 }
9766 else if (h != NULL)
9767 {
9768 /* Setting the index to -2 tells elf_link_output_extsym that
9769 this symbol is used by a reloc. */
9770 h->indx = -2;
9771 *rel_hash_ptr = h;
9772 indx = 0;
9773 }
9774 else
9775 {
9776 if (! ((*info->callbacks->unattached_reloc)
9777 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
9778 return FALSE;
9779 indx = 0;
9780 }
9781 }
9782
9783 /* If this is an inplace reloc, we must write the addend into the
9784 object file. */
9785 if (howto->partial_inplace && addend != 0)
9786 {
9787 bfd_size_type size;
9788 bfd_reloc_status_type rstat;
9789 bfd_byte *buf;
9790 bfd_boolean ok;
9791 const char *sym_name;
9792
9793 size = bfd_get_reloc_size (howto);
9794 buf = bfd_zmalloc (size);
9795 if (buf == NULL)
9796 return FALSE;
9797 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
9798 switch (rstat)
9799 {
9800 case bfd_reloc_ok:
9801 break;
9802
9803 default:
9804 case bfd_reloc_outofrange:
9805 abort ();
9806
9807 case bfd_reloc_overflow:
9808 if (link_order->type == bfd_section_reloc_link_order)
9809 sym_name = bfd_section_name (output_bfd,
9810 link_order->u.reloc.p->u.section);
9811 else
9812 sym_name = link_order->u.reloc.p->u.name;
9813 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f
L
9814 (info, NULL, sym_name, howto->name, addend, NULL,
9815 NULL, (bfd_vma) 0)))
c152c796
AM
9816 {
9817 free (buf);
9818 return FALSE;
9819 }
9820 break;
9821 }
9822 ok = bfd_set_section_contents (output_bfd, output_section, buf,
9823 link_order->offset, size);
9824 free (buf);
9825 if (! ok)
9826 return FALSE;
9827 }
9828
9829 /* The address of a reloc is relative to the section in a
9830 relocatable file, and is a virtual address in an executable
9831 file. */
9832 offset = link_order->offset;
9833 if (! info->relocatable)
9834 offset += output_section->vma;
9835
9836 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
9837 {
9838 irel[i].r_offset = offset;
9839 irel[i].r_info = 0;
9840 irel[i].r_addend = 0;
9841 }
9842 if (bed->s->arch_size == 32)
9843 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
9844 else
9845 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
9846
9847 rel_hdr = &elf_section_data (output_section)->rel_hdr;
9848 erel = rel_hdr->contents;
9849 if (rel_hdr->sh_type == SHT_REL)
9850 {
9851 erel += (elf_section_data (output_section)->rel_count
9852 * bed->s->sizeof_rel);
9853 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
9854 }
9855 else
9856 {
9857 irel[0].r_addend = addend;
9858 erel += (elf_section_data (output_section)->rel_count
9859 * bed->s->sizeof_rela);
9860 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
9861 }
9862
9863 ++elf_section_data (output_section)->rel_count;
9864
9865 return TRUE;
9866}
9867
0b52efa6
PB
9868
9869/* Get the output vma of the section pointed to by the sh_link field. */
9870
9871static bfd_vma
9872elf_get_linked_section_vma (struct bfd_link_order *p)
9873{
9874 Elf_Internal_Shdr **elf_shdrp;
9875 asection *s;
9876 int elfsec;
9877
9878 s = p->u.indirect.section;
9879 elf_shdrp = elf_elfsections (s->owner);
9880 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
9881 elfsec = elf_shdrp[elfsec]->sh_link;
185d09ad
L
9882 /* PR 290:
9883 The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 9884 SHF_LINK_ORDER. But it doesn't set the sh_link or
185d09ad
L
9885 sh_info fields. Hence we could get the situation
9886 where elfsec is 0. */
9887 if (elfsec == 0)
9888 {
9889 const struct elf_backend_data *bed
9890 = get_elf_backend_data (s->owner);
9891 if (bed->link_order_error_handler)
d003868e
AM
9892 bed->link_order_error_handler
9893 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
185d09ad
L
9894 return 0;
9895 }
9896 else
9897 {
9898 s = elf_shdrp[elfsec]->bfd_section;
9899 return s->output_section->vma + s->output_offset;
9900 }
0b52efa6
PB
9901}
9902
9903
9904/* Compare two sections based on the locations of the sections they are
9905 linked to. Used by elf_fixup_link_order. */
9906
9907static int
9908compare_link_order (const void * a, const void * b)
9909{
9910 bfd_vma apos;
9911 bfd_vma bpos;
9912
9913 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
9914 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
9915 if (apos < bpos)
9916 return -1;
9917 return apos > bpos;
9918}
9919
9920
9921/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
9922 order as their linked sections. Returns false if this could not be done
9923 because an output section includes both ordered and unordered
9924 sections. Ideally we'd do this in the linker proper. */
9925
9926static bfd_boolean
9927elf_fixup_link_order (bfd *abfd, asection *o)
9928{
9929 int seen_linkorder;
9930 int seen_other;
9931 int n;
9932 struct bfd_link_order *p;
9933 bfd *sub;
9934 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b761a207 9935 unsigned elfsec;
0b52efa6 9936 struct bfd_link_order **sections;
d33cdfe3 9937 asection *s, *other_sec, *linkorder_sec;
0b52efa6 9938 bfd_vma offset;
3b36f7e6 9939
d33cdfe3
L
9940 other_sec = NULL;
9941 linkorder_sec = NULL;
0b52efa6
PB
9942 seen_other = 0;
9943 seen_linkorder = 0;
8423293d 9944 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 9945 {
d33cdfe3 9946 if (p->type == bfd_indirect_link_order)
0b52efa6
PB
9947 {
9948 s = p->u.indirect.section;
d33cdfe3
L
9949 sub = s->owner;
9950 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
9951 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
b761a207
BE
9952 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
9953 && elfsec < elf_numsections (sub)
0b52efa6 9954 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER)
d33cdfe3
L
9955 {
9956 seen_linkorder++;
9957 linkorder_sec = s;
9958 }
0b52efa6 9959 else
d33cdfe3
L
9960 {
9961 seen_other++;
9962 other_sec = s;
9963 }
0b52efa6
PB
9964 }
9965 else
9966 seen_other++;
d33cdfe3
L
9967
9968 if (seen_other && seen_linkorder)
9969 {
9970 if (other_sec && linkorder_sec)
9971 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
9972 o, linkorder_sec,
9973 linkorder_sec->owner, other_sec,
9974 other_sec->owner);
9975 else
9976 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
9977 o);
9978 bfd_set_error (bfd_error_bad_value);
9979 return FALSE;
9980 }
0b52efa6
PB
9981 }
9982
9983 if (!seen_linkorder)
9984 return TRUE;
9985
0b52efa6 9986 sections = (struct bfd_link_order **)
14b1c01e
AM
9987 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
9988 if (sections == NULL)
9989 return FALSE;
0b52efa6 9990 seen_linkorder = 0;
3b36f7e6 9991
8423293d 9992 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6
PB
9993 {
9994 sections[seen_linkorder++] = p;
9995 }
9996 /* Sort the input sections in the order of their linked section. */
9997 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
9998 compare_link_order);
9999
10000 /* Change the offsets of the sections. */
10001 offset = 0;
10002 for (n = 0; n < seen_linkorder; n++)
10003 {
10004 s = sections[n]->u.indirect.section;
461686a3 10005 offset &= ~(bfd_vma) 0 << s->alignment_power;
0b52efa6
PB
10006 s->output_offset = offset;
10007 sections[n]->offset = offset;
10008 offset += sections[n]->size;
10009 }
10010
10011 return TRUE;
10012}
10013
10014
c152c796
AM
10015/* Do the final step of an ELF link. */
10016
10017bfd_boolean
10018bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
10019{
10020 bfd_boolean dynamic;
10021 bfd_boolean emit_relocs;
10022 bfd *dynobj;
10023 struct elf_final_link_info finfo;
10024 register asection *o;
10025 register struct bfd_link_order *p;
10026 register bfd *sub;
10027 bfd_size_type max_contents_size;
10028 bfd_size_type max_external_reloc_size;
10029 bfd_size_type max_internal_reloc_count;
10030 bfd_size_type max_sym_count;
10031 bfd_size_type max_sym_shndx_count;
10032 file_ptr off;
10033 Elf_Internal_Sym elfsym;
10034 unsigned int i;
10035 Elf_Internal_Shdr *symtab_hdr;
10036 Elf_Internal_Shdr *symtab_shndx_hdr;
10037 Elf_Internal_Shdr *symstrtab_hdr;
10038 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10039 struct elf_outext_info eoinfo;
10040 bfd_boolean merged;
10041 size_t relativecount = 0;
10042 asection *reldyn = 0;
10043 bfd_size_type amt;
104d59d1
JM
10044 asection *attr_section = NULL;
10045 bfd_vma attr_size = 0;
10046 const char *std_attrs_section;
c152c796
AM
10047
10048 if (! is_elf_hash_table (info->hash))
10049 return FALSE;
10050
10051 if (info->shared)
10052 abfd->flags |= DYNAMIC;
10053
10054 dynamic = elf_hash_table (info)->dynamic_sections_created;
10055 dynobj = elf_hash_table (info)->dynobj;
10056
10057 emit_relocs = (info->relocatable
a4676736 10058 || info->emitrelocations);
c152c796
AM
10059
10060 finfo.info = info;
10061 finfo.output_bfd = abfd;
10062 finfo.symstrtab = _bfd_elf_stringtab_init ();
10063 if (finfo.symstrtab == NULL)
10064 return FALSE;
10065
10066 if (! dynamic)
10067 {
10068 finfo.dynsym_sec = NULL;
10069 finfo.hash_sec = NULL;
10070 finfo.symver_sec = NULL;
10071 }
10072 else
10073 {
10074 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
10075 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
fdc90cb4 10076 BFD_ASSERT (finfo.dynsym_sec != NULL);
c152c796
AM
10077 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
10078 /* Note that it is OK if symver_sec is NULL. */
10079 }
10080
10081 finfo.contents = NULL;
10082 finfo.external_relocs = NULL;
10083 finfo.internal_relocs = NULL;
10084 finfo.external_syms = NULL;
10085 finfo.locsym_shndx = NULL;
10086 finfo.internal_syms = NULL;
10087 finfo.indices = NULL;
10088 finfo.sections = NULL;
10089 finfo.symbuf = NULL;
10090 finfo.symshndxbuf = NULL;
10091 finfo.symbuf_count = 0;
10092 finfo.shndxbuf_size = 0;
10093
104d59d1
JM
10094 /* The object attributes have been merged. Remove the input
10095 sections from the link, and set the contents of the output
10096 secton. */
10097 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
10098 for (o = abfd->sections; o != NULL; o = o->next)
10099 {
10100 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
10101 || strcmp (o->name, ".gnu.attributes") == 0)
10102 {
10103 for (p = o->map_head.link_order; p != NULL; p = p->next)
10104 {
10105 asection *input_section;
10106
10107 if (p->type != bfd_indirect_link_order)
10108 continue;
10109 input_section = p->u.indirect.section;
10110 /* Hack: reset the SEC_HAS_CONTENTS flag so that
10111 elf_link_input_bfd ignores this section. */
10112 input_section->flags &= ~SEC_HAS_CONTENTS;
10113 }
10114
10115 attr_size = bfd_elf_obj_attr_size (abfd);
10116 if (attr_size)
10117 {
10118 bfd_set_section_size (abfd, o, attr_size);
10119 attr_section = o;
10120 /* Skip this section later on. */
10121 o->map_head.link_order = NULL;
10122 }
10123 else
10124 o->flags |= SEC_EXCLUDE;
10125 }
10126 }
10127
c152c796
AM
10128 /* Count up the number of relocations we will output for each output
10129 section, so that we know the sizes of the reloc sections. We
10130 also figure out some maximum sizes. */
10131 max_contents_size = 0;
10132 max_external_reloc_size = 0;
10133 max_internal_reloc_count = 0;
10134 max_sym_count = 0;
10135 max_sym_shndx_count = 0;
10136 merged = FALSE;
10137 for (o = abfd->sections; o != NULL; o = o->next)
10138 {
10139 struct bfd_elf_section_data *esdo = elf_section_data (o);
10140 o->reloc_count = 0;
10141
8423293d 10142 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
10143 {
10144 unsigned int reloc_count = 0;
10145 struct bfd_elf_section_data *esdi = NULL;
10146 unsigned int *rel_count1;
10147
10148 if (p->type == bfd_section_reloc_link_order
10149 || p->type == bfd_symbol_reloc_link_order)
10150 reloc_count = 1;
10151 else if (p->type == bfd_indirect_link_order)
10152 {
10153 asection *sec;
10154
10155 sec = p->u.indirect.section;
10156 esdi = elf_section_data (sec);
10157
10158 /* Mark all sections which are to be included in the
10159 link. This will normally be every section. We need
10160 to do this so that we can identify any sections which
10161 the linker has decided to not include. */
10162 sec->linker_mark = TRUE;
10163
10164 if (sec->flags & SEC_MERGE)
10165 merged = TRUE;
10166
10167 if (info->relocatable || info->emitrelocations)
10168 reloc_count = sec->reloc_count;
10169 else if (bed->elf_backend_count_relocs)
10170 {
10171 Elf_Internal_Rela * relocs;
10172
1182cb93
AM
10173 relocs = _bfd_elf_link_read_relocs (sec->owner, sec,
10174 NULL, NULL,
c152c796
AM
10175 info->keep_memory);
10176
1182cb93
AM
10177 if (relocs != NULL)
10178 {
10179 reloc_count
10180 = (*bed->elf_backend_count_relocs) (sec, relocs);
c152c796 10181
1182cb93
AM
10182 if (elf_section_data (sec)->relocs != relocs)
10183 free (relocs);
10184 }
c152c796
AM
10185 }
10186
eea6121a
AM
10187 if (sec->rawsize > max_contents_size)
10188 max_contents_size = sec->rawsize;
10189 if (sec->size > max_contents_size)
10190 max_contents_size = sec->size;
c152c796
AM
10191
10192 /* We are interested in just local symbols, not all
10193 symbols. */
10194 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
10195 && (sec->owner->flags & DYNAMIC) == 0)
10196 {
10197 size_t sym_count;
10198
10199 if (elf_bad_symtab (sec->owner))
10200 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
10201 / bed->s->sizeof_sym);
10202 else
10203 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
10204
10205 if (sym_count > max_sym_count)
10206 max_sym_count = sym_count;
10207
10208 if (sym_count > max_sym_shndx_count
10209 && elf_symtab_shndx (sec->owner) != 0)
10210 max_sym_shndx_count = sym_count;
10211
10212 if ((sec->flags & SEC_RELOC) != 0)
10213 {
10214 size_t ext_size;
10215
10216 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
10217 if (ext_size > max_external_reloc_size)
10218 max_external_reloc_size = ext_size;
10219 if (sec->reloc_count > max_internal_reloc_count)
10220 max_internal_reloc_count = sec->reloc_count;
10221 }
10222 }
10223 }
10224
10225 if (reloc_count == 0)
10226 continue;
10227
10228 o->reloc_count += reloc_count;
10229
10230 /* MIPS may have a mix of REL and RELA relocs on sections.
10231 To support this curious ABI we keep reloc counts in
10232 elf_section_data too. We must be careful to add the
10233 relocations from the input section to the right output
10234 count. FIXME: Get rid of one count. We have
10235 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
10236 rel_count1 = &esdo->rel_count;
10237 if (esdi != NULL)
10238 {
10239 bfd_boolean same_size;
10240 bfd_size_type entsize1;
10241
10242 entsize1 = esdi->rel_hdr.sh_entsize;
10243 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
10244 || entsize1 == bed->s->sizeof_rela);
10245 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
10246
10247 if (!same_size)
10248 rel_count1 = &esdo->rel_count2;
10249
10250 if (esdi->rel_hdr2 != NULL)
10251 {
10252 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
10253 unsigned int alt_count;
10254 unsigned int *rel_count2;
10255
10256 BFD_ASSERT (entsize2 != entsize1
10257 && (entsize2 == bed->s->sizeof_rel
10258 || entsize2 == bed->s->sizeof_rela));
10259
10260 rel_count2 = &esdo->rel_count2;
10261 if (!same_size)
10262 rel_count2 = &esdo->rel_count;
10263
10264 /* The following is probably too simplistic if the
10265 backend counts output relocs unusually. */
10266 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
10267 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
10268 *rel_count2 += alt_count;
10269 reloc_count -= alt_count;
10270 }
10271 }
10272 *rel_count1 += reloc_count;
10273 }
10274
10275 if (o->reloc_count > 0)
10276 o->flags |= SEC_RELOC;
10277 else
10278 {
10279 /* Explicitly clear the SEC_RELOC flag. The linker tends to
10280 set it (this is probably a bug) and if it is set
10281 assign_section_numbers will create a reloc section. */
10282 o->flags &=~ SEC_RELOC;
10283 }
10284
10285 /* If the SEC_ALLOC flag is not set, force the section VMA to
10286 zero. This is done in elf_fake_sections as well, but forcing
10287 the VMA to 0 here will ensure that relocs against these
10288 sections are handled correctly. */
10289 if ((o->flags & SEC_ALLOC) == 0
10290 && ! o->user_set_vma)
10291 o->vma = 0;
10292 }
10293
10294 if (! info->relocatable && merged)
10295 elf_link_hash_traverse (elf_hash_table (info),
10296 _bfd_elf_link_sec_merge_syms, abfd);
10297
10298 /* Figure out the file positions for everything but the symbol table
10299 and the relocs. We set symcount to force assign_section_numbers
10300 to create a symbol table. */
10301 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
10302 BFD_ASSERT (! abfd->output_has_begun);
10303 if (! _bfd_elf_compute_section_file_positions (abfd, info))
10304 goto error_return;
10305
ee75fd95 10306 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
10307 for (o = abfd->sections; o != NULL; o = o->next)
10308 {
10309 if ((o->flags & SEC_RELOC) != 0)
10310 {
10311 if (!(_bfd_elf_link_size_reloc_section
10312 (abfd, &elf_section_data (o)->rel_hdr, o)))
10313 goto error_return;
10314
10315 if (elf_section_data (o)->rel_hdr2
10316 && !(_bfd_elf_link_size_reloc_section
10317 (abfd, elf_section_data (o)->rel_hdr2, o)))
10318 goto error_return;
10319 }
10320
10321 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
10322 to count upwards while actually outputting the relocations. */
10323 elf_section_data (o)->rel_count = 0;
10324 elf_section_data (o)->rel_count2 = 0;
10325 }
10326
10327 _bfd_elf_assign_file_positions_for_relocs (abfd);
10328
10329 /* We have now assigned file positions for all the sections except
10330 .symtab and .strtab. We start the .symtab section at the current
10331 file position, and write directly to it. We build the .strtab
10332 section in memory. */
10333 bfd_get_symcount (abfd) = 0;
10334 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10335 /* sh_name is set in prep_headers. */
10336 symtab_hdr->sh_type = SHT_SYMTAB;
10337 /* sh_flags, sh_addr and sh_size all start off zero. */
10338 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
10339 /* sh_link is set in assign_section_numbers. */
10340 /* sh_info is set below. */
10341 /* sh_offset is set just below. */
10342 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
10343
10344 off = elf_tdata (abfd)->next_file_pos;
10345 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
10346
10347 /* Note that at this point elf_tdata (abfd)->next_file_pos is
10348 incorrect. We do not yet know the size of the .symtab section.
10349 We correct next_file_pos below, after we do know the size. */
10350
10351 /* Allocate a buffer to hold swapped out symbols. This is to avoid
10352 continuously seeking to the right position in the file. */
10353 if (! info->keep_memory || max_sym_count < 20)
10354 finfo.symbuf_size = 20;
10355 else
10356 finfo.symbuf_size = max_sym_count;
10357 amt = finfo.symbuf_size;
10358 amt *= bed->s->sizeof_sym;
10359 finfo.symbuf = bfd_malloc (amt);
10360 if (finfo.symbuf == NULL)
10361 goto error_return;
10362 if (elf_numsections (abfd) > SHN_LORESERVE)
10363 {
10364 /* Wild guess at number of output symbols. realloc'd as needed. */
10365 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
10366 finfo.shndxbuf_size = amt;
10367 amt *= sizeof (Elf_External_Sym_Shndx);
10368 finfo.symshndxbuf = bfd_zmalloc (amt);
10369 if (finfo.symshndxbuf == NULL)
10370 goto error_return;
10371 }
10372
10373 /* Start writing out the symbol table. The first symbol is always a
10374 dummy symbol. */
10375 if (info->strip != strip_all
10376 || emit_relocs)
10377 {
10378 elfsym.st_value = 0;
10379 elfsym.st_size = 0;
10380 elfsym.st_info = 0;
10381 elfsym.st_other = 0;
10382 elfsym.st_shndx = SHN_UNDEF;
10383 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
10384 NULL))
10385 goto error_return;
10386 }
10387
c152c796
AM
10388 /* Output a symbol for each section. We output these even if we are
10389 discarding local symbols, since they are used for relocs. These
10390 symbols have no names. We store the index of each one in the
10391 index field of the section, so that we can find it again when
10392 outputting relocs. */
10393 if (info->strip != strip_all
10394 || emit_relocs)
10395 {
10396 elfsym.st_size = 0;
10397 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
10398 elfsym.st_other = 0;
f0b5bb34 10399 elfsym.st_value = 0;
c152c796
AM
10400 for (i = 1; i < elf_numsections (abfd); i++)
10401 {
10402 o = bfd_section_from_elf_index (abfd, i);
10403 if (o != NULL)
f0b5bb34
AM
10404 {
10405 o->target_index = bfd_get_symcount (abfd);
10406 elfsym.st_shndx = i;
10407 if (!info->relocatable)
10408 elfsym.st_value = o->vma;
10409 if (!elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
10410 goto error_return;
10411 }
c152c796
AM
10412 if (i == SHN_LORESERVE - 1)
10413 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
10414 }
10415 }
10416
10417 /* Allocate some memory to hold information read in from the input
10418 files. */
10419 if (max_contents_size != 0)
10420 {
10421 finfo.contents = bfd_malloc (max_contents_size);
10422 if (finfo.contents == NULL)
10423 goto error_return;
10424 }
10425
10426 if (max_external_reloc_size != 0)
10427 {
10428 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
10429 if (finfo.external_relocs == NULL)
10430 goto error_return;
10431 }
10432
10433 if (max_internal_reloc_count != 0)
10434 {
10435 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
10436 amt *= sizeof (Elf_Internal_Rela);
10437 finfo.internal_relocs = bfd_malloc (amt);
10438 if (finfo.internal_relocs == NULL)
10439 goto error_return;
10440 }
10441
10442 if (max_sym_count != 0)
10443 {
10444 amt = max_sym_count * bed->s->sizeof_sym;
10445 finfo.external_syms = bfd_malloc (amt);
10446 if (finfo.external_syms == NULL)
10447 goto error_return;
10448
10449 amt = max_sym_count * sizeof (Elf_Internal_Sym);
10450 finfo.internal_syms = bfd_malloc (amt);
10451 if (finfo.internal_syms == NULL)
10452 goto error_return;
10453
10454 amt = max_sym_count * sizeof (long);
10455 finfo.indices = bfd_malloc (amt);
10456 if (finfo.indices == NULL)
10457 goto error_return;
10458
10459 amt = max_sym_count * sizeof (asection *);
10460 finfo.sections = bfd_malloc (amt);
10461 if (finfo.sections == NULL)
10462 goto error_return;
10463 }
10464
10465 if (max_sym_shndx_count != 0)
10466 {
10467 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
10468 finfo.locsym_shndx = bfd_malloc (amt);
10469 if (finfo.locsym_shndx == NULL)
10470 goto error_return;
10471 }
10472
10473 if (elf_hash_table (info)->tls_sec)
10474 {
10475 bfd_vma base, end = 0;
10476 asection *sec;
10477
10478 for (sec = elf_hash_table (info)->tls_sec;
10479 sec && (sec->flags & SEC_THREAD_LOCAL);
10480 sec = sec->next)
10481 {
3a800eb9 10482 bfd_size_type size = sec->size;
c152c796 10483
3a800eb9
AM
10484 if (size == 0
10485 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 10486 {
3a800eb9
AM
10487 struct bfd_link_order *o = sec->map_tail.link_order;
10488 if (o != NULL)
10489 size = o->offset + o->size;
c152c796
AM
10490 }
10491 end = sec->vma + size;
10492 }
10493 base = elf_hash_table (info)->tls_sec->vma;
10494 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
10495 elf_hash_table (info)->tls_size = end - base;
10496 }
10497
0b52efa6
PB
10498 /* Reorder SHF_LINK_ORDER sections. */
10499 for (o = abfd->sections; o != NULL; o = o->next)
10500 {
10501 if (!elf_fixup_link_order (abfd, o))
10502 return FALSE;
10503 }
10504
c152c796
AM
10505 /* Since ELF permits relocations to be against local symbols, we
10506 must have the local symbols available when we do the relocations.
10507 Since we would rather only read the local symbols once, and we
10508 would rather not keep them in memory, we handle all the
10509 relocations for a single input file at the same time.
10510
10511 Unfortunately, there is no way to know the total number of local
10512 symbols until we have seen all of them, and the local symbol
10513 indices precede the global symbol indices. This means that when
10514 we are generating relocatable output, and we see a reloc against
10515 a global symbol, we can not know the symbol index until we have
10516 finished examining all the local symbols to see which ones we are
10517 going to output. To deal with this, we keep the relocations in
10518 memory, and don't output them until the end of the link. This is
10519 an unfortunate waste of memory, but I don't see a good way around
10520 it. Fortunately, it only happens when performing a relocatable
10521 link, which is not the common case. FIXME: If keep_memory is set
10522 we could write the relocs out and then read them again; I don't
10523 know how bad the memory loss will be. */
10524
10525 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10526 sub->output_has_begun = FALSE;
10527 for (o = abfd->sections; o != NULL; o = o->next)
10528 {
8423293d 10529 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
10530 {
10531 if (p->type == bfd_indirect_link_order
10532 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
10533 == bfd_target_elf_flavour)
10534 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
10535 {
10536 if (! sub->output_has_begun)
10537 {
10538 if (! elf_link_input_bfd (&finfo, sub))
10539 goto error_return;
10540 sub->output_has_begun = TRUE;
10541 }
10542 }
10543 else if (p->type == bfd_section_reloc_link_order
10544 || p->type == bfd_symbol_reloc_link_order)
10545 {
10546 if (! elf_reloc_link_order (abfd, info, o, p))
10547 goto error_return;
10548 }
10549 else
10550 {
10551 if (! _bfd_default_link_order (abfd, info, o, p))
10552 goto error_return;
10553 }
10554 }
10555 }
10556
c0f00686
L
10557 /* Free symbol buffer if needed. */
10558 if (!info->reduce_memory_overheads)
10559 {
10560 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3fcd97f1
JJ
10561 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10562 && elf_tdata (sub)->symbuf)
c0f00686
L
10563 {
10564 free (elf_tdata (sub)->symbuf);
10565 elf_tdata (sub)->symbuf = NULL;
10566 }
10567 }
10568
c152c796
AM
10569 /* Output any global symbols that got converted to local in a
10570 version script or due to symbol visibility. We do this in a
10571 separate step since ELF requires all local symbols to appear
10572 prior to any global symbols. FIXME: We should only do this if
10573 some global symbols were, in fact, converted to become local.
10574 FIXME: Will this work correctly with the Irix 5 linker? */
10575 eoinfo.failed = FALSE;
10576 eoinfo.finfo = &finfo;
10577 eoinfo.localsyms = TRUE;
10578 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
10579 &eoinfo);
10580 if (eoinfo.failed)
10581 return FALSE;
10582
4e617b1e
PB
10583 /* If backend needs to output some local symbols not present in the hash
10584 table, do it now. */
10585 if (bed->elf_backend_output_arch_local_syms)
10586 {
10587 typedef bfd_boolean (*out_sym_func)
10588 (void *, const char *, Elf_Internal_Sym *, asection *,
10589 struct elf_link_hash_entry *);
10590
10591 if (! ((*bed->elf_backend_output_arch_local_syms)
10592 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
10593 return FALSE;
10594 }
10595
c152c796
AM
10596 /* That wrote out all the local symbols. Finish up the symbol table
10597 with the global symbols. Even if we want to strip everything we
10598 can, we still need to deal with those global symbols that got
10599 converted to local in a version script. */
10600
10601 /* The sh_info field records the index of the first non local symbol. */
10602 symtab_hdr->sh_info = bfd_get_symcount (abfd);
10603
10604 if (dynamic
10605 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
10606 {
10607 Elf_Internal_Sym sym;
10608 bfd_byte *dynsym = finfo.dynsym_sec->contents;
10609 long last_local = 0;
10610
10611 /* Write out the section symbols for the output sections. */
67687978 10612 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
c152c796
AM
10613 {
10614 asection *s;
10615
10616 sym.st_size = 0;
10617 sym.st_name = 0;
10618 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
10619 sym.st_other = 0;
10620
10621 for (s = abfd->sections; s != NULL; s = s->next)
10622 {
10623 int indx;
10624 bfd_byte *dest;
10625 long dynindx;
10626
c152c796 10627 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
10628 if (dynindx <= 0)
10629 continue;
10630 indx = elf_section_data (s)->this_idx;
c152c796
AM
10631 BFD_ASSERT (indx > 0);
10632 sym.st_shndx = indx;
c0d5a53d
L
10633 if (! check_dynsym (abfd, &sym))
10634 return FALSE;
c152c796
AM
10635 sym.st_value = s->vma;
10636 dest = dynsym + dynindx * bed->s->sizeof_sym;
8c37241b
JJ
10637 if (last_local < dynindx)
10638 last_local = dynindx;
c152c796
AM
10639 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
10640 }
c152c796
AM
10641 }
10642
10643 /* Write out the local dynsyms. */
10644 if (elf_hash_table (info)->dynlocal)
10645 {
10646 struct elf_link_local_dynamic_entry *e;
10647 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
10648 {
10649 asection *s;
10650 bfd_byte *dest;
10651
10652 sym.st_size = e->isym.st_size;
10653 sym.st_other = e->isym.st_other;
10654
10655 /* Copy the internal symbol as is.
10656 Note that we saved a word of storage and overwrote
10657 the original st_name with the dynstr_index. */
10658 sym = e->isym;
10659
10660 if (e->isym.st_shndx != SHN_UNDEF
10661 && (e->isym.st_shndx < SHN_LORESERVE
10662 || e->isym.st_shndx > SHN_HIRESERVE))
10663 {
10664 s = bfd_section_from_elf_index (e->input_bfd,
10665 e->isym.st_shndx);
10666
10667 sym.st_shndx =
10668 elf_section_data (s->output_section)->this_idx;
c0d5a53d
L
10669 if (! check_dynsym (abfd, &sym))
10670 return FALSE;
c152c796
AM
10671 sym.st_value = (s->output_section->vma
10672 + s->output_offset
10673 + e->isym.st_value);
10674 }
10675
10676 if (last_local < e->dynindx)
10677 last_local = e->dynindx;
10678
10679 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
10680 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
10681 }
10682 }
10683
10684 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
10685 last_local + 1;
10686 }
10687
10688 /* We get the global symbols from the hash table. */
10689 eoinfo.failed = FALSE;
10690 eoinfo.localsyms = FALSE;
10691 eoinfo.finfo = &finfo;
10692 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
10693 &eoinfo);
10694 if (eoinfo.failed)
10695 return FALSE;
10696
10697 /* If backend needs to output some symbols not present in the hash
10698 table, do it now. */
10699 if (bed->elf_backend_output_arch_syms)
10700 {
10701 typedef bfd_boolean (*out_sym_func)
10702 (void *, const char *, Elf_Internal_Sym *, asection *,
10703 struct elf_link_hash_entry *);
10704
10705 if (! ((*bed->elf_backend_output_arch_syms)
10706 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
10707 return FALSE;
10708 }
10709
10710 /* Flush all symbols to the file. */
10711 if (! elf_link_flush_output_syms (&finfo, bed))
10712 return FALSE;
10713
10714 /* Now we know the size of the symtab section. */
10715 off += symtab_hdr->sh_size;
10716
10717 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
10718 if (symtab_shndx_hdr->sh_name != 0)
10719 {
10720 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
10721 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
10722 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
10723 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
10724 symtab_shndx_hdr->sh_size = amt;
10725
10726 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
10727 off, TRUE);
10728
10729 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
10730 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
10731 return FALSE;
10732 }
10733
10734
10735 /* Finish up and write out the symbol string table (.strtab)
10736 section. */
10737 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
10738 /* sh_name was set in prep_headers. */
10739 symstrtab_hdr->sh_type = SHT_STRTAB;
10740 symstrtab_hdr->sh_flags = 0;
10741 symstrtab_hdr->sh_addr = 0;
10742 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
10743 symstrtab_hdr->sh_entsize = 0;
10744 symstrtab_hdr->sh_link = 0;
10745 symstrtab_hdr->sh_info = 0;
10746 /* sh_offset is set just below. */
10747 symstrtab_hdr->sh_addralign = 1;
10748
10749 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
10750 elf_tdata (abfd)->next_file_pos = off;
10751
10752 if (bfd_get_symcount (abfd) > 0)
10753 {
10754 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
10755 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
10756 return FALSE;
10757 }
10758
10759 /* Adjust the relocs to have the correct symbol indices. */
10760 for (o = abfd->sections; o != NULL; o = o->next)
10761 {
10762 if ((o->flags & SEC_RELOC) == 0)
10763 continue;
10764
10765 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
10766 elf_section_data (o)->rel_count,
10767 elf_section_data (o)->rel_hashes);
10768 if (elf_section_data (o)->rel_hdr2 != NULL)
10769 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
10770 elf_section_data (o)->rel_count2,
10771 (elf_section_data (o)->rel_hashes
10772 + elf_section_data (o)->rel_count));
10773
10774 /* Set the reloc_count field to 0 to prevent write_relocs from
10775 trying to swap the relocs out itself. */
10776 o->reloc_count = 0;
10777 }
10778
10779 if (dynamic && info->combreloc && dynobj != NULL)
10780 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
10781
10782 /* If we are linking against a dynamic object, or generating a
10783 shared library, finish up the dynamic linking information. */
10784 if (dynamic)
10785 {
10786 bfd_byte *dyncon, *dynconend;
10787
10788 /* Fix up .dynamic entries. */
10789 o = bfd_get_section_by_name (dynobj, ".dynamic");
10790 BFD_ASSERT (o != NULL);
10791
10792 dyncon = o->contents;
eea6121a 10793 dynconend = o->contents + o->size;
c152c796
AM
10794 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
10795 {
10796 Elf_Internal_Dyn dyn;
10797 const char *name;
10798 unsigned int type;
10799
10800 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
10801
10802 switch (dyn.d_tag)
10803 {
10804 default:
10805 continue;
10806 case DT_NULL:
10807 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
10808 {
10809 switch (elf_section_data (reldyn)->this_hdr.sh_type)
10810 {
10811 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
10812 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
10813 default: continue;
10814 }
10815 dyn.d_un.d_val = relativecount;
10816 relativecount = 0;
10817 break;
10818 }
10819 continue;
10820
10821 case DT_INIT:
10822 name = info->init_function;
10823 goto get_sym;
10824 case DT_FINI:
10825 name = info->fini_function;
10826 get_sym:
10827 {
10828 struct elf_link_hash_entry *h;
10829
10830 h = elf_link_hash_lookup (elf_hash_table (info), name,
10831 FALSE, FALSE, TRUE);
10832 if (h != NULL
10833 && (h->root.type == bfd_link_hash_defined
10834 || h->root.type == bfd_link_hash_defweak))
10835 {
10836 dyn.d_un.d_val = h->root.u.def.value;
10837 o = h->root.u.def.section;
10838 if (o->output_section != NULL)
10839 dyn.d_un.d_val += (o->output_section->vma
10840 + o->output_offset);
10841 else
10842 {
10843 /* The symbol is imported from another shared
10844 library and does not apply to this one. */
10845 dyn.d_un.d_val = 0;
10846 }
10847 break;
10848 }
10849 }
10850 continue;
10851
10852 case DT_PREINIT_ARRAYSZ:
10853 name = ".preinit_array";
10854 goto get_size;
10855 case DT_INIT_ARRAYSZ:
10856 name = ".init_array";
10857 goto get_size;
10858 case DT_FINI_ARRAYSZ:
10859 name = ".fini_array";
10860 get_size:
10861 o = bfd_get_section_by_name (abfd, name);
10862 if (o == NULL)
10863 {
10864 (*_bfd_error_handler)
d003868e 10865 (_("%B: could not find output section %s"), abfd, name);
c152c796
AM
10866 goto error_return;
10867 }
eea6121a 10868 if (o->size == 0)
c152c796
AM
10869 (*_bfd_error_handler)
10870 (_("warning: %s section has zero size"), name);
eea6121a 10871 dyn.d_un.d_val = o->size;
c152c796
AM
10872 break;
10873
10874 case DT_PREINIT_ARRAY:
10875 name = ".preinit_array";
10876 goto get_vma;
10877 case DT_INIT_ARRAY:
10878 name = ".init_array";
10879 goto get_vma;
10880 case DT_FINI_ARRAY:
10881 name = ".fini_array";
10882 goto get_vma;
10883
10884 case DT_HASH:
10885 name = ".hash";
10886 goto get_vma;
fdc90cb4
JJ
10887 case DT_GNU_HASH:
10888 name = ".gnu.hash";
10889 goto get_vma;
c152c796
AM
10890 case DT_STRTAB:
10891 name = ".dynstr";
10892 goto get_vma;
10893 case DT_SYMTAB:
10894 name = ".dynsym";
10895 goto get_vma;
10896 case DT_VERDEF:
10897 name = ".gnu.version_d";
10898 goto get_vma;
10899 case DT_VERNEED:
10900 name = ".gnu.version_r";
10901 goto get_vma;
10902 case DT_VERSYM:
10903 name = ".gnu.version";
10904 get_vma:
10905 o = bfd_get_section_by_name (abfd, name);
10906 if (o == NULL)
10907 {
10908 (*_bfd_error_handler)
d003868e 10909 (_("%B: could not find output section %s"), abfd, name);
c152c796
AM
10910 goto error_return;
10911 }
10912 dyn.d_un.d_ptr = o->vma;
10913 break;
10914
10915 case DT_REL:
10916 case DT_RELA:
10917 case DT_RELSZ:
10918 case DT_RELASZ:
10919 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
10920 type = SHT_REL;
10921 else
10922 type = SHT_RELA;
10923 dyn.d_un.d_val = 0;
10924 for (i = 1; i < elf_numsections (abfd); i++)
10925 {
10926 Elf_Internal_Shdr *hdr;
10927
10928 hdr = elf_elfsections (abfd)[i];
10929 if (hdr->sh_type == type
10930 && (hdr->sh_flags & SHF_ALLOC) != 0)
10931 {
10932 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
10933 dyn.d_un.d_val += hdr->sh_size;
10934 else
10935 {
10936 if (dyn.d_un.d_val == 0
10937 || hdr->sh_addr < dyn.d_un.d_val)
10938 dyn.d_un.d_val = hdr->sh_addr;
10939 }
10940 }
10941 }
10942 break;
10943 }
10944 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
10945 }
10946 }
10947
10948 /* If we have created any dynamic sections, then output them. */
10949 if (dynobj != NULL)
10950 {
10951 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
10952 goto error_return;
10953
943284cc
DJ
10954 /* Check for DT_TEXTREL (late, in case the backend removes it). */
10955 if (info->warn_shared_textrel && info->shared)
10956 {
10957 bfd_byte *dyncon, *dynconend;
10958
10959 /* Fix up .dynamic entries. */
10960 o = bfd_get_section_by_name (dynobj, ".dynamic");
10961 BFD_ASSERT (o != NULL);
10962
10963 dyncon = o->contents;
10964 dynconend = o->contents + o->size;
10965 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
10966 {
10967 Elf_Internal_Dyn dyn;
10968
10969 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
10970
10971 if (dyn.d_tag == DT_TEXTREL)
10972 {
9267588c
L
10973 info->callbacks->einfo
10974 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
943284cc
DJ
10975 break;
10976 }
10977 }
10978 }
10979
c152c796
AM
10980 for (o = dynobj->sections; o != NULL; o = o->next)
10981 {
10982 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 10983 || o->size == 0
c152c796
AM
10984 || o->output_section == bfd_abs_section_ptr)
10985 continue;
10986 if ((o->flags & SEC_LINKER_CREATED) == 0)
10987 {
10988 /* At this point, we are only interested in sections
10989 created by _bfd_elf_link_create_dynamic_sections. */
10990 continue;
10991 }
3722b82f
AM
10992 if (elf_hash_table (info)->stab_info.stabstr == o)
10993 continue;
eea6121a
AM
10994 if (elf_hash_table (info)->eh_info.hdr_sec == o)
10995 continue;
c152c796
AM
10996 if ((elf_section_data (o->output_section)->this_hdr.sh_type
10997 != SHT_STRTAB)
10998 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
10999 {
11000 if (! bfd_set_section_contents (abfd, o->output_section,
11001 o->contents,
11002 (file_ptr) o->output_offset,
eea6121a 11003 o->size))
c152c796
AM
11004 goto error_return;
11005 }
11006 else
11007 {
11008 /* The contents of the .dynstr section are actually in a
11009 stringtab. */
11010 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
11011 if (bfd_seek (abfd, off, SEEK_SET) != 0
11012 || ! _bfd_elf_strtab_emit (abfd,
11013 elf_hash_table (info)->dynstr))
11014 goto error_return;
11015 }
11016 }
11017 }
11018
11019 if (info->relocatable)
11020 {
11021 bfd_boolean failed = FALSE;
11022
11023 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
11024 if (failed)
11025 goto error_return;
11026 }
11027
11028 /* If we have optimized stabs strings, output them. */
3722b82f 11029 if (elf_hash_table (info)->stab_info.stabstr != NULL)
c152c796
AM
11030 {
11031 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
11032 goto error_return;
11033 }
11034
11035 if (info->eh_frame_hdr)
11036 {
11037 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
11038 goto error_return;
11039 }
11040
11041 if (finfo.symstrtab != NULL)
11042 _bfd_stringtab_free (finfo.symstrtab);
11043 if (finfo.contents != NULL)
11044 free (finfo.contents);
11045 if (finfo.external_relocs != NULL)
11046 free (finfo.external_relocs);
11047 if (finfo.internal_relocs != NULL)
11048 free (finfo.internal_relocs);
11049 if (finfo.external_syms != NULL)
11050 free (finfo.external_syms);
11051 if (finfo.locsym_shndx != NULL)
11052 free (finfo.locsym_shndx);
11053 if (finfo.internal_syms != NULL)
11054 free (finfo.internal_syms);
11055 if (finfo.indices != NULL)
11056 free (finfo.indices);
11057 if (finfo.sections != NULL)
11058 free (finfo.sections);
11059 if (finfo.symbuf != NULL)
11060 free (finfo.symbuf);
11061 if (finfo.symshndxbuf != NULL)
11062 free (finfo.symshndxbuf);
11063 for (o = abfd->sections; o != NULL; o = o->next)
11064 {
11065 if ((o->flags & SEC_RELOC) != 0
11066 && elf_section_data (o)->rel_hashes != NULL)
11067 free (elf_section_data (o)->rel_hashes);
11068 }
11069
11070 elf_tdata (abfd)->linker = TRUE;
11071
104d59d1
JM
11072 if (attr_section)
11073 {
11074 bfd_byte *contents = bfd_malloc (attr_size);
11075 if (contents == NULL)
d0f16d5e 11076 return FALSE; /* Bail out and fail. */
104d59d1
JM
11077 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
11078 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
11079 free (contents);
11080 }
11081
c152c796
AM
11082 return TRUE;
11083
11084 error_return:
11085 if (finfo.symstrtab != NULL)
11086 _bfd_stringtab_free (finfo.symstrtab);
11087 if (finfo.contents != NULL)
11088 free (finfo.contents);
11089 if (finfo.external_relocs != NULL)
11090 free (finfo.external_relocs);
11091 if (finfo.internal_relocs != NULL)
11092 free (finfo.internal_relocs);
11093 if (finfo.external_syms != NULL)
11094 free (finfo.external_syms);
11095 if (finfo.locsym_shndx != NULL)
11096 free (finfo.locsym_shndx);
11097 if (finfo.internal_syms != NULL)
11098 free (finfo.internal_syms);
11099 if (finfo.indices != NULL)
11100 free (finfo.indices);
11101 if (finfo.sections != NULL)
11102 free (finfo.sections);
11103 if (finfo.symbuf != NULL)
11104 free (finfo.symbuf);
11105 if (finfo.symshndxbuf != NULL)
11106 free (finfo.symshndxbuf);
11107 for (o = abfd->sections; o != NULL; o = o->next)
11108 {
11109 if ((o->flags & SEC_RELOC) != 0
11110 && elf_section_data (o)->rel_hashes != NULL)
11111 free (elf_section_data (o)->rel_hashes);
11112 }
11113
11114 return FALSE;
11115}
11116\f
11117/* Garbage collect unused sections. */
11118
07adf181
AM
11119/* Default gc_mark_hook. */
11120
11121asection *
11122_bfd_elf_gc_mark_hook (asection *sec,
11123 struct bfd_link_info *info ATTRIBUTE_UNUSED,
11124 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
11125 struct elf_link_hash_entry *h,
11126 Elf_Internal_Sym *sym)
11127{
11128 if (h != NULL)
11129 {
11130 switch (h->root.type)
11131 {
11132 case bfd_link_hash_defined:
11133 case bfd_link_hash_defweak:
11134 return h->root.u.def.section;
11135
11136 case bfd_link_hash_common:
11137 return h->root.u.c.p->section;
11138
11139 default:
11140 break;
11141 }
11142 }
11143 else
11144 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
11145
11146 return NULL;
11147}
11148
11149/* The mark phase of garbage collection. For a given section, mark
11150 it and any sections in this section's group, and all the sections
11151 which define symbols to which it refers. */
11152
ccfa59ea
AM
11153bfd_boolean
11154_bfd_elf_gc_mark (struct bfd_link_info *info,
11155 asection *sec,
6a5bb875 11156 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
11157{
11158 bfd_boolean ret;
39c2f51b 11159 bfd_boolean is_eh;
c152c796
AM
11160 asection *group_sec;
11161
11162 sec->gc_mark = 1;
11163
11164 /* Mark all the sections in the group. */
11165 group_sec = elf_section_data (sec)->next_in_group;
11166 if (group_sec && !group_sec->gc_mark)
ccfa59ea 11167 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
11168 return FALSE;
11169
11170 /* Look through the section relocs. */
11171 ret = TRUE;
39c2f51b 11172 is_eh = strcmp (sec->name, ".eh_frame") == 0;
c152c796
AM
11173 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
11174 {
11175 Elf_Internal_Rela *relstart, *rel, *relend;
11176 Elf_Internal_Shdr *symtab_hdr;
11177 struct elf_link_hash_entry **sym_hashes;
11178 size_t nlocsyms;
11179 size_t extsymoff;
11180 bfd *input_bfd = sec->owner;
11181 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
11182 Elf_Internal_Sym *isym = NULL;
11183 int r_sym_shift;
11184
11185 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
11186 sym_hashes = elf_sym_hashes (input_bfd);
11187
11188 /* Read the local symbols. */
11189 if (elf_bad_symtab (input_bfd))
11190 {
11191 nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym;
11192 extsymoff = 0;
11193 }
11194 else
11195 extsymoff = nlocsyms = symtab_hdr->sh_info;
11196
11197 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
11198 if (isym == NULL && nlocsyms != 0)
11199 {
11200 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
11201 NULL, NULL, NULL);
11202 if (isym == NULL)
11203 return FALSE;
11204 }
11205
11206 /* Read the relocations. */
11207 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
11208 info->keep_memory);
11209 if (relstart == NULL)
11210 {
11211 ret = FALSE;
11212 goto out1;
11213 }
11214 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
11215
11216 if (bed->s->arch_size == 32)
11217 r_sym_shift = 8;
11218 else
11219 r_sym_shift = 32;
11220
11221 for (rel = relstart; rel < relend; rel++)
11222 {
11223 unsigned long r_symndx;
11224 asection *rsec;
11225 struct elf_link_hash_entry *h;
11226
11227 r_symndx = rel->r_info >> r_sym_shift;
11228 if (r_symndx == 0)
11229 continue;
11230
11231 if (r_symndx >= nlocsyms
11232 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
11233 {
11234 h = sym_hashes[r_symndx - extsymoff];
20f0a1ad
AM
11235 while (h->root.type == bfd_link_hash_indirect
11236 || h->root.type == bfd_link_hash_warning)
11237 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796
AM
11238 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
11239 }
11240 else
11241 {
11242 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
11243 }
11244
11245 if (rsec && !rsec->gc_mark)
11246 {
11247 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
11248 rsec->gc_mark = 1;
39c2f51b
AM
11249 else if (is_eh)
11250 rsec->gc_mark_from_eh = 1;
ccfa59ea 11251 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
c152c796
AM
11252 {
11253 ret = FALSE;
11254 goto out2;
11255 }
11256 }
11257 }
11258
11259 out2:
11260 if (elf_section_data (sec)->relocs != relstart)
11261 free (relstart);
11262 out1:
11263 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
11264 {
11265 if (! info->keep_memory)
11266 free (isym);
11267 else
11268 symtab_hdr->contents = (unsigned char *) isym;
11269 }
11270 }
11271
11272 return ret;
11273}
11274
11275/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
11276
c17d87de
NC
11277struct elf_gc_sweep_symbol_info
11278{
ccabcbe5
AM
11279 struct bfd_link_info *info;
11280 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
11281 bfd_boolean);
11282};
11283
c152c796 11284static bfd_boolean
ccabcbe5 11285elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
c152c796 11286{
c152c796
AM
11287 if (h->root.type == bfd_link_hash_warning)
11288 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11289
ccabcbe5
AM
11290 if ((h->root.type == bfd_link_hash_defined
11291 || h->root.type == bfd_link_hash_defweak)
11292 && !h->root.u.def.section->gc_mark
11293 && !(h->root.u.def.section->owner->flags & DYNAMIC))
11294 {
11295 struct elf_gc_sweep_symbol_info *inf = data;
11296 (*inf->hide_symbol) (inf->info, h, TRUE);
11297 }
c152c796
AM
11298
11299 return TRUE;
11300}
11301
11302/* The sweep phase of garbage collection. Remove all garbage sections. */
11303
11304typedef bfd_boolean (*gc_sweep_hook_fn)
11305 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
11306
11307static bfd_boolean
ccabcbe5 11308elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
11309{
11310 bfd *sub;
ccabcbe5
AM
11311 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11312 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
11313 unsigned long section_sym_count;
11314 struct elf_gc_sweep_symbol_info sweep_info;
c152c796
AM
11315
11316 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11317 {
11318 asection *o;
11319
11320 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
11321 continue;
11322
11323 for (o = sub->sections; o != NULL; o = o->next)
11324 {
7c2c8505
AM
11325 /* Keep debug and special sections. */
11326 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
dea5f36a 11327 || (o->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
c152c796
AM
11328 o->gc_mark = 1;
11329
11330 if (o->gc_mark)
11331 continue;
11332
11333 /* Skip sweeping sections already excluded. */
11334 if (o->flags & SEC_EXCLUDE)
11335 continue;
11336
11337 /* Since this is early in the link process, it is simple
11338 to remove a section from the output. */
11339 o->flags |= SEC_EXCLUDE;
11340
c55fe096 11341 if (info->print_gc_sections && o->size != 0)
c17d87de
NC
11342 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
11343
c152c796
AM
11344 /* But we also have to update some of the relocation
11345 info we collected before. */
11346 if (gc_sweep_hook
e8aaee2a
AM
11347 && (o->flags & SEC_RELOC) != 0
11348 && o->reloc_count > 0
11349 && !bfd_is_abs_section (o->output_section))
c152c796
AM
11350 {
11351 Elf_Internal_Rela *internal_relocs;
11352 bfd_boolean r;
11353
11354 internal_relocs
11355 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
11356 info->keep_memory);
11357 if (internal_relocs == NULL)
11358 return FALSE;
11359
11360 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
11361
11362 if (elf_section_data (o)->relocs != internal_relocs)
11363 free (internal_relocs);
11364
11365 if (!r)
11366 return FALSE;
11367 }
11368 }
11369 }
11370
11371 /* Remove the symbols that were in the swept sections from the dynamic
11372 symbol table. GCFIXME: Anyone know how to get them out of the
11373 static symbol table as well? */
ccabcbe5
AM
11374 sweep_info.info = info;
11375 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
11376 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
11377 &sweep_info);
c152c796 11378
ccabcbe5 11379 _bfd_elf_link_renumber_dynsyms (abfd, info, &section_sym_count);
c152c796
AM
11380 return TRUE;
11381}
11382
11383/* Propagate collected vtable information. This is called through
11384 elf_link_hash_traverse. */
11385
11386static bfd_boolean
11387elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
11388{
11389 if (h->root.type == bfd_link_hash_warning)
11390 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11391
11392 /* Those that are not vtables. */
f6e332e6 11393 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
11394 return TRUE;
11395
11396 /* Those vtables that do not have parents, we cannot merge. */
f6e332e6 11397 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
11398 return TRUE;
11399
11400 /* If we've already been done, exit. */
f6e332e6 11401 if (h->vtable->used && h->vtable->used[-1])
c152c796
AM
11402 return TRUE;
11403
11404 /* Make sure the parent's table is up to date. */
f6e332e6 11405 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
c152c796 11406
f6e332e6 11407 if (h->vtable->used == NULL)
c152c796
AM
11408 {
11409 /* None of this table's entries were referenced. Re-use the
11410 parent's table. */
f6e332e6
AM
11411 h->vtable->used = h->vtable->parent->vtable->used;
11412 h->vtable->size = h->vtable->parent->vtable->size;
c152c796
AM
11413 }
11414 else
11415 {
11416 size_t n;
11417 bfd_boolean *cu, *pu;
11418
11419 /* Or the parent's entries into ours. */
f6e332e6 11420 cu = h->vtable->used;
c152c796 11421 cu[-1] = TRUE;
f6e332e6 11422 pu = h->vtable->parent->vtable->used;
c152c796
AM
11423 if (pu != NULL)
11424 {
11425 const struct elf_backend_data *bed;
11426 unsigned int log_file_align;
11427
11428 bed = get_elf_backend_data (h->root.u.def.section->owner);
11429 log_file_align = bed->s->log_file_align;
f6e332e6 11430 n = h->vtable->parent->vtable->size >> log_file_align;
c152c796
AM
11431 while (n--)
11432 {
11433 if (*pu)
11434 *cu = TRUE;
11435 pu++;
11436 cu++;
11437 }
11438 }
11439 }
11440
11441 return TRUE;
11442}
11443
11444static bfd_boolean
11445elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
11446{
11447 asection *sec;
11448 bfd_vma hstart, hend;
11449 Elf_Internal_Rela *relstart, *relend, *rel;
11450 const struct elf_backend_data *bed;
11451 unsigned int log_file_align;
11452
11453 if (h->root.type == bfd_link_hash_warning)
11454 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11455
11456 /* Take care of both those symbols that do not describe vtables as
11457 well as those that are not loaded. */
f6e332e6 11458 if (h->vtable == NULL || h->vtable->parent == NULL)
c152c796
AM
11459 return TRUE;
11460
11461 BFD_ASSERT (h->root.type == bfd_link_hash_defined
11462 || h->root.type == bfd_link_hash_defweak);
11463
11464 sec = h->root.u.def.section;
11465 hstart = h->root.u.def.value;
11466 hend = hstart + h->size;
11467
11468 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
11469 if (!relstart)
11470 return *(bfd_boolean *) okp = FALSE;
11471 bed = get_elf_backend_data (sec->owner);
11472 log_file_align = bed->s->log_file_align;
11473
11474 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
11475
11476 for (rel = relstart; rel < relend; ++rel)
11477 if (rel->r_offset >= hstart && rel->r_offset < hend)
11478 {
11479 /* If the entry is in use, do nothing. */
f6e332e6
AM
11480 if (h->vtable->used
11481 && (rel->r_offset - hstart) < h->vtable->size)
c152c796
AM
11482 {
11483 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
f6e332e6 11484 if (h->vtable->used[entry])
c152c796
AM
11485 continue;
11486 }
11487 /* Otherwise, kill it. */
11488 rel->r_offset = rel->r_info = rel->r_addend = 0;
11489 }
11490
11491 return TRUE;
11492}
11493
87538722
AM
11494/* Mark sections containing dynamically referenced symbols. When
11495 building shared libraries, we must assume that any visible symbol is
11496 referenced. */
715df9b8 11497
64d03ab5
AM
11498bfd_boolean
11499bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 11500{
87538722
AM
11501 struct bfd_link_info *info = (struct bfd_link_info *) inf;
11502
715df9b8
EB
11503 if (h->root.type == bfd_link_hash_warning)
11504 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11505
11506 if ((h->root.type == bfd_link_hash_defined
11507 || h->root.type == bfd_link_hash_defweak)
87538722 11508 && (h->ref_dynamic
5adcfd8b 11509 || (!info->executable
87538722
AM
11510 && h->def_regular
11511 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
11512 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN)))
715df9b8
EB
11513 h->root.u.def.section->flags |= SEC_KEEP;
11514
11515 return TRUE;
11516}
3b36f7e6 11517
c152c796
AM
11518/* Do mark and sweep of unused sections. */
11519
11520bfd_boolean
11521bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
11522{
11523 bfd_boolean ok = TRUE;
11524 bfd *sub;
6a5bb875 11525 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 11526 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
c152c796 11527
64d03ab5 11528 if (!bed->can_gc_sections
c152c796
AM
11529 || info->relocatable
11530 || info->emitrelocations
715df9b8 11531 || !is_elf_hash_table (info->hash))
c152c796
AM
11532 {
11533 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
11534 return TRUE;
11535 }
11536
11537 /* Apply transitive closure to the vtable entry usage info. */
11538 elf_link_hash_traverse (elf_hash_table (info),
11539 elf_gc_propagate_vtable_entries_used,
11540 &ok);
11541 if (!ok)
11542 return FALSE;
11543
11544 /* Kill the vtable relocations that were not used. */
11545 elf_link_hash_traverse (elf_hash_table (info),
11546 elf_gc_smash_unused_vtentry_relocs,
11547 &ok);
11548 if (!ok)
11549 return FALSE;
11550
715df9b8
EB
11551 /* Mark dynamically referenced symbols. */
11552 if (elf_hash_table (info)->dynamic_sections_created)
11553 elf_link_hash_traverse (elf_hash_table (info),
64d03ab5 11554 bed->gc_mark_dynamic_ref,
87538722 11555 info);
c152c796 11556
715df9b8 11557 /* Grovel through relocs to find out who stays ... */
64d03ab5 11558 gc_mark_hook = bed->gc_mark_hook;
c152c796
AM
11559 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11560 {
11561 asection *o;
11562
11563 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
11564 continue;
11565
11566 for (o = sub->sections; o != NULL; o = o->next)
a14a5de3 11567 if ((o->flags & (SEC_EXCLUDE | SEC_KEEP)) == SEC_KEEP && !o->gc_mark)
39c2f51b
AM
11568 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
11569 return FALSE;
c152c796
AM
11570 }
11571
6a5bb875
PB
11572 /* Allow the backend to mark additional target specific sections. */
11573 if (bed->gc_mark_extra_sections)
11574 bed->gc_mark_extra_sections(info, gc_mark_hook);
11575
9e8cc8b4
AM
11576 /* ... again for sections marked from eh_frame. */
11577 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11578 {
11579 asection *o;
11580
11581 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
11582 continue;
11583
a48710b3
EB
11584 /* Keep .gcc_except_table.* if the associated .text.* (or the
11585 associated .gnu.linkonce.t.* if .text.* doesn't exist) is
9e8cc8b4 11586 marked. This isn't very nice, but the proper solution,
2de92251 11587 splitting .eh_frame up and using comdat doesn't pan out
9e8cc8b4
AM
11588 easily due to needing special relocs to handle the
11589 difference of two symbols in separate sections.
11590 Don't keep code sections referenced by .eh_frame. */
ea9986ff 11591#define TEXT_PREFIX ".text."
a48710b3 11592#define TEXT_PREFIX2 ".gnu.linkonce.t."
ea9986ff 11593#define GCC_EXCEPT_TABLE_PREFIX ".gcc_except_table."
9e8cc8b4
AM
11594 for (o = sub->sections; o != NULL; o = o->next)
11595 if (!o->gc_mark && o->gc_mark_from_eh && (o->flags & SEC_CODE) == 0)
11596 {
ea9986ff 11597 if (CONST_STRNEQ (o->name, GCC_EXCEPT_TABLE_PREFIX))
9e8cc8b4 11598 {
9e8cc8b4 11599 char *fn_name;
ea9986ff 11600 const char *sec_name;
9e8cc8b4 11601 asection *fn_text;
a48710b3 11602 unsigned o_name_prefix_len , fn_name_prefix_len, tmp;
9e8cc8b4 11603
a48710b3 11604 o_name_prefix_len = strlen (GCC_EXCEPT_TABLE_PREFIX);
ea9986ff 11605 sec_name = o->name + o_name_prefix_len;
a48710b3
EB
11606 fn_name_prefix_len = strlen (TEXT_PREFIX);
11607 tmp = strlen (TEXT_PREFIX2);
11608 if (tmp > fn_name_prefix_len)
11609 fn_name_prefix_len = tmp;
11610 fn_name
11611 = bfd_malloc (fn_name_prefix_len + strlen (sec_name) + 1);
9e8cc8b4
AM
11612 if (fn_name == NULL)
11613 return FALSE;
a48710b3
EB
11614
11615 /* Try the first prefix. */
ea9986ff 11616 sprintf (fn_name, "%s%s", TEXT_PREFIX, sec_name);
9e8cc8b4 11617 fn_text = bfd_get_section_by_name (sub, fn_name);
a48710b3
EB
11618
11619 /* Try the second prefix. */
11620 if (fn_text == NULL)
11621 {
11622 sprintf (fn_name, "%s%s", TEXT_PREFIX2, sec_name);
11623 fn_text = bfd_get_section_by_name (sub, fn_name);
11624 }
11625
9e8cc8b4
AM
11626 free (fn_name);
11627 if (fn_text == NULL || !fn_text->gc_mark)
11628 continue;
11629 }
11630
11631 /* If not using specially named exception table section,
11632 then keep whatever we are using. */
11633 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
11634 return FALSE;
11635 }
11636 }
11637
c152c796 11638 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 11639 return elf_gc_sweep (abfd, info);
c152c796
AM
11640}
11641\f
11642/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
11643
11644bfd_boolean
11645bfd_elf_gc_record_vtinherit (bfd *abfd,
11646 asection *sec,
11647 struct elf_link_hash_entry *h,
11648 bfd_vma offset)
11649{
11650 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
11651 struct elf_link_hash_entry **search, *child;
11652 bfd_size_type extsymcount;
11653 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11654
11655 /* The sh_info field of the symtab header tells us where the
11656 external symbols start. We don't care about the local symbols at
11657 this point. */
11658 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
11659 if (!elf_bad_symtab (abfd))
11660 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
11661
11662 sym_hashes = elf_sym_hashes (abfd);
11663 sym_hashes_end = sym_hashes + extsymcount;
11664
11665 /* Hunt down the child symbol, which is in this section at the same
11666 offset as the relocation. */
11667 for (search = sym_hashes; search != sym_hashes_end; ++search)
11668 {
11669 if ((child = *search) != NULL
11670 && (child->root.type == bfd_link_hash_defined
11671 || child->root.type == bfd_link_hash_defweak)
11672 && child->root.u.def.section == sec
11673 && child->root.u.def.value == offset)
11674 goto win;
11675 }
11676
d003868e
AM
11677 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
11678 abfd, sec, (unsigned long) offset);
c152c796
AM
11679 bfd_set_error (bfd_error_invalid_operation);
11680 return FALSE;
11681
11682 win:
f6e332e6
AM
11683 if (!child->vtable)
11684 {
11685 child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable));
11686 if (!child->vtable)
11687 return FALSE;
11688 }
c152c796
AM
11689 if (!h)
11690 {
11691 /* This *should* only be the absolute section. It could potentially
11692 be that someone has defined a non-global vtable though, which
11693 would be bad. It isn't worth paging in the local symbols to be
11694 sure though; that case should simply be handled by the assembler. */
11695
f6e332e6 11696 child->vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
11697 }
11698 else
f6e332e6 11699 child->vtable->parent = h;
c152c796
AM
11700
11701 return TRUE;
11702}
11703
11704/* Called from check_relocs to record the existence of a VTENTRY reloc. */
11705
11706bfd_boolean
11707bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
11708 asection *sec ATTRIBUTE_UNUSED,
11709 struct elf_link_hash_entry *h,
11710 bfd_vma addend)
11711{
11712 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11713 unsigned int log_file_align = bed->s->log_file_align;
11714
f6e332e6
AM
11715 if (!h->vtable)
11716 {
11717 h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable));
11718 if (!h->vtable)
11719 return FALSE;
11720 }
11721
11722 if (addend >= h->vtable->size)
c152c796
AM
11723 {
11724 size_t size, bytes, file_align;
f6e332e6 11725 bfd_boolean *ptr = h->vtable->used;
c152c796
AM
11726
11727 /* While the symbol is undefined, we have to be prepared to handle
11728 a zero size. */
11729 file_align = 1 << log_file_align;
11730 if (h->root.type == bfd_link_hash_undefined)
11731 size = addend + file_align;
11732 else
11733 {
11734 size = h->size;
11735 if (addend >= size)
11736 {
11737 /* Oops! We've got a reference past the defined end of
11738 the table. This is probably a bug -- shall we warn? */
11739 size = addend + file_align;
11740 }
11741 }
11742 size = (size + file_align - 1) & -file_align;
11743
11744 /* Allocate one extra entry for use as a "done" flag for the
11745 consolidation pass. */
11746 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
11747
11748 if (ptr)
11749 {
11750 ptr = bfd_realloc (ptr - 1, bytes);
11751
11752 if (ptr != NULL)
11753 {
11754 size_t oldbytes;
11755
f6e332e6 11756 oldbytes = (((h->vtable->size >> log_file_align) + 1)
c152c796
AM
11757 * sizeof (bfd_boolean));
11758 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
11759 }
11760 }
11761 else
11762 ptr = bfd_zmalloc (bytes);
11763
11764 if (ptr == NULL)
11765 return FALSE;
11766
11767 /* And arrange for that done flag to be at index -1. */
f6e332e6
AM
11768 h->vtable->used = ptr + 1;
11769 h->vtable->size = size;
c152c796
AM
11770 }
11771
f6e332e6 11772 h->vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
11773
11774 return TRUE;
11775}
11776
11777struct alloc_got_off_arg {
11778 bfd_vma gotoff;
11779 unsigned int got_elt_size;
11780};
11781
11782/* We need a special top-level link routine to convert got reference counts
11783 to real got offsets. */
11784
11785static bfd_boolean
11786elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
11787{
11788 struct alloc_got_off_arg *gofarg = arg;
11789
11790 if (h->root.type == bfd_link_hash_warning)
11791 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11792
11793 if (h->got.refcount > 0)
11794 {
11795 h->got.offset = gofarg->gotoff;
11796 gofarg->gotoff += gofarg->got_elt_size;
11797 }
11798 else
11799 h->got.offset = (bfd_vma) -1;
11800
11801 return TRUE;
11802}
11803
11804/* And an accompanying bit to work out final got entry offsets once
11805 we're done. Should be called from final_link. */
11806
11807bfd_boolean
11808bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
11809 struct bfd_link_info *info)
11810{
11811 bfd *i;
11812 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11813 bfd_vma gotoff;
11814 unsigned int got_elt_size = bed->s->arch_size / 8;
11815 struct alloc_got_off_arg gofarg;
11816
11817 if (! is_elf_hash_table (info->hash))
11818 return FALSE;
11819
11820 /* The GOT offset is relative to the .got section, but the GOT header is
11821 put into the .got.plt section, if the backend uses it. */
11822 if (bed->want_got_plt)
11823 gotoff = 0;
11824 else
11825 gotoff = bed->got_header_size;
11826
11827 /* Do the local .got entries first. */
11828 for (i = info->input_bfds; i; i = i->link_next)
11829 {
11830 bfd_signed_vma *local_got;
11831 bfd_size_type j, locsymcount;
11832 Elf_Internal_Shdr *symtab_hdr;
11833
11834 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
11835 continue;
11836
11837 local_got = elf_local_got_refcounts (i);
11838 if (!local_got)
11839 continue;
11840
11841 symtab_hdr = &elf_tdata (i)->symtab_hdr;
11842 if (elf_bad_symtab (i))
11843 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11844 else
11845 locsymcount = symtab_hdr->sh_info;
11846
11847 for (j = 0; j < locsymcount; ++j)
11848 {
11849 if (local_got[j] > 0)
11850 {
11851 local_got[j] = gotoff;
11852 gotoff += got_elt_size;
11853 }
11854 else
11855 local_got[j] = (bfd_vma) -1;
11856 }
11857 }
11858
11859 /* Then the global .got entries. .plt refcounts are handled by
11860 adjust_dynamic_symbol */
11861 gofarg.gotoff = gotoff;
11862 gofarg.got_elt_size = got_elt_size;
11863 elf_link_hash_traverse (elf_hash_table (info),
11864 elf_gc_allocate_got_offsets,
11865 &gofarg);
11866 return TRUE;
11867}
11868
11869/* Many folk need no more in the way of final link than this, once
11870 got entry reference counting is enabled. */
11871
11872bfd_boolean
11873bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
11874{
11875 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
11876 return FALSE;
11877
11878 /* Invoke the regular ELF backend linker to do all the work. */
11879 return bfd_elf_final_link (abfd, info);
11880}
11881
11882bfd_boolean
11883bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
11884{
11885 struct elf_reloc_cookie *rcookie = cookie;
11886
11887 if (rcookie->bad_symtab)
11888 rcookie->rel = rcookie->rels;
11889
11890 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
11891 {
11892 unsigned long r_symndx;
11893
11894 if (! rcookie->bad_symtab)
11895 if (rcookie->rel->r_offset > offset)
11896 return FALSE;
11897 if (rcookie->rel->r_offset != offset)
11898 continue;
11899
11900 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
11901 if (r_symndx == SHN_UNDEF)
11902 return TRUE;
11903
11904 if (r_symndx >= rcookie->locsymcount
11905 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
11906 {
11907 struct elf_link_hash_entry *h;
11908
11909 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
11910
11911 while (h->root.type == bfd_link_hash_indirect
11912 || h->root.type == bfd_link_hash_warning)
11913 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11914
11915 if ((h->root.type == bfd_link_hash_defined
11916 || h->root.type == bfd_link_hash_defweak)
11917 && elf_discarded_section (h->root.u.def.section))
11918 return TRUE;
11919 else
11920 return FALSE;
11921 }
11922 else
11923 {
11924 /* It's not a relocation against a global symbol,
11925 but it could be a relocation against a local
11926 symbol for a discarded section. */
11927 asection *isec;
11928 Elf_Internal_Sym *isym;
11929
11930 /* Need to: get the symbol; get the section. */
11931 isym = &rcookie->locsyms[r_symndx];
11932 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
11933 {
11934 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
11935 if (isec != NULL && elf_discarded_section (isec))
11936 return TRUE;
11937 }
11938 }
11939 return FALSE;
11940 }
11941 return FALSE;
11942}
11943
11944/* Discard unneeded references to discarded sections.
11945 Returns TRUE if any section's size was changed. */
11946/* This function assumes that the relocations are in sorted order,
11947 which is true for all known assemblers. */
11948
11949bfd_boolean
11950bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
11951{
11952 struct elf_reloc_cookie cookie;
11953 asection *stab, *eh;
11954 Elf_Internal_Shdr *symtab_hdr;
11955 const struct elf_backend_data *bed;
11956 bfd *abfd;
11957 unsigned int count;
11958 bfd_boolean ret = FALSE;
11959
11960 if (info->traditional_format
11961 || !is_elf_hash_table (info->hash))
11962 return FALSE;
11963
11964 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
11965 {
11966 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
11967 continue;
11968
11969 bed = get_elf_backend_data (abfd);
11970
11971 if ((abfd->flags & DYNAMIC) != 0)
11972 continue;
11973
8da3dbc5
AM
11974 eh = NULL;
11975 if (!info->relocatable)
11976 {
11977 eh = bfd_get_section_by_name (abfd, ".eh_frame");
11978 if (eh != NULL
eea6121a 11979 && (eh->size == 0
8da3dbc5
AM
11980 || bfd_is_abs_section (eh->output_section)))
11981 eh = NULL;
11982 }
c152c796
AM
11983
11984 stab = bfd_get_section_by_name (abfd, ".stab");
11985 if (stab != NULL
eea6121a 11986 && (stab->size == 0
c152c796
AM
11987 || bfd_is_abs_section (stab->output_section)
11988 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
11989 stab = NULL;
11990
11991 if (stab == NULL
11992 && eh == NULL
11993 && bed->elf_backend_discard_info == NULL)
11994 continue;
11995
11996 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11997 cookie.abfd = abfd;
11998 cookie.sym_hashes = elf_sym_hashes (abfd);
11999 cookie.bad_symtab = elf_bad_symtab (abfd);
12000 if (cookie.bad_symtab)
12001 {
12002 cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12003 cookie.extsymoff = 0;
12004 }
12005 else
12006 {
12007 cookie.locsymcount = symtab_hdr->sh_info;
12008 cookie.extsymoff = symtab_hdr->sh_info;
12009 }
12010
12011 if (bed->s->arch_size == 32)
12012 cookie.r_sym_shift = 8;
12013 else
12014 cookie.r_sym_shift = 32;
12015
12016 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12017 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
12018 {
12019 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12020 cookie.locsymcount, 0,
12021 NULL, NULL, NULL);
12022 if (cookie.locsyms == NULL)
1182cb93
AM
12023 {
12024 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12025 return FALSE;
12026 }
c152c796
AM
12027 }
12028
12029 if (stab != NULL)
12030 {
12031 cookie.rels = NULL;
12032 count = stab->reloc_count;
12033 if (count != 0)
12034 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
12035 info->keep_memory);
12036 if (cookie.rels != NULL)
12037 {
12038 cookie.rel = cookie.rels;
12039 cookie.relend = cookie.rels;
12040 cookie.relend += count * bed->s->int_rels_per_ext_rel;
12041 if (_bfd_discard_section_stabs (abfd, stab,
12042 elf_section_data (stab)->sec_info,
12043 bfd_elf_reloc_symbol_deleted_p,
12044 &cookie))
12045 ret = TRUE;
12046 if (elf_section_data (stab)->relocs != cookie.rels)
12047 free (cookie.rels);
12048 }
12049 }
12050
12051 if (eh != NULL)
12052 {
12053 cookie.rels = NULL;
12054 count = eh->reloc_count;
12055 if (count != 0)
12056 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
12057 info->keep_memory);
12058 cookie.rel = cookie.rels;
12059 cookie.relend = cookie.rels;
12060 if (cookie.rels != NULL)
12061 cookie.relend += count * bed->s->int_rels_per_ext_rel;
12062
12063 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
12064 bfd_elf_reloc_symbol_deleted_p,
12065 &cookie))
12066 ret = TRUE;
12067
12068 if (cookie.rels != NULL
12069 && elf_section_data (eh)->relocs != cookie.rels)
12070 free (cookie.rels);
12071 }
12072
12073 if (bed->elf_backend_discard_info != NULL
12074 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
12075 ret = TRUE;
12076
12077 if (cookie.locsyms != NULL
12078 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
12079 {
12080 if (! info->keep_memory)
12081 free (cookie.locsyms);
12082 else
12083 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
12084 }
12085 }
12086
12087 if (info->eh_frame_hdr
12088 && !info->relocatable
12089 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
12090 ret = TRUE;
12091
12092 return ret;
12093}
082b7297
L
12094
12095void
c0f00686
L
12096_bfd_elf_section_already_linked (bfd *abfd, struct bfd_section *sec,
12097 struct bfd_link_info *info)
082b7297
L
12098{
12099 flagword flags;
6d2cd210 12100 const char *name, *p;
082b7297
L
12101 struct bfd_section_already_linked *l;
12102 struct bfd_section_already_linked_hash_entry *already_linked_list;
3d7f7666 12103
3d7f7666
L
12104 if (sec->output_section == bfd_abs_section_ptr)
12105 return;
082b7297
L
12106
12107 flags = sec->flags;
3d7f7666 12108
c2370991
AM
12109 /* Return if it isn't a linkonce section. A comdat group section
12110 also has SEC_LINK_ONCE set. */
12111 if ((flags & SEC_LINK_ONCE) == 0)
082b7297
L
12112 return;
12113
c2370991
AM
12114 /* Don't put group member sections on our list of already linked
12115 sections. They are handled as a group via their group section. */
12116 if (elf_sec_group (sec) != NULL)
12117 return;
3d7f7666 12118
082b7297
L
12119 /* FIXME: When doing a relocatable link, we may have trouble
12120 copying relocations in other sections that refer to local symbols
12121 in the section being discarded. Those relocations will have to
12122 be converted somehow; as of this writing I'm not sure that any of
12123 the backends handle that correctly.
12124
12125 It is tempting to instead not discard link once sections when
12126 doing a relocatable link (technically, they should be discarded
12127 whenever we are building constructors). However, that fails,
12128 because the linker winds up combining all the link once sections
12129 into a single large link once section, which defeats the purpose
12130 of having link once sections in the first place.
12131
12132 Also, not merging link once sections in a relocatable link
12133 causes trouble for MIPS ELF, which relies on link once semantics
12134 to handle the .reginfo section correctly. */
12135
12136 name = bfd_get_section_name (abfd, sec);
12137
0112cd26 12138 if (CONST_STRNEQ (name, ".gnu.linkonce.")
6d2cd210
JJ
12139 && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
12140 p++;
12141 else
12142 p = name;
12143
12144 already_linked_list = bfd_section_already_linked_table_lookup (p);
082b7297
L
12145
12146 for (l = already_linked_list->entry; l != NULL; l = l->next)
12147 {
c2370991
AM
12148 /* We may have 2 different types of sections on the list: group
12149 sections and linkonce sections. Match like sections. */
3d7f7666 12150 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
6d2cd210 12151 && strcmp (name, l->sec->name) == 0
082b7297
L
12152 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
12153 {
12154 /* The section has already been linked. See if we should
6d2cd210 12155 issue a warning. */
082b7297
L
12156 switch (flags & SEC_LINK_DUPLICATES)
12157 {
12158 default:
12159 abort ();
12160
12161 case SEC_LINK_DUPLICATES_DISCARD:
12162 break;
12163
12164 case SEC_LINK_DUPLICATES_ONE_ONLY:
12165 (*_bfd_error_handler)
c93625e2 12166 (_("%B: ignoring duplicate section `%A'"),
d003868e 12167 abfd, sec);
082b7297
L
12168 break;
12169
12170 case SEC_LINK_DUPLICATES_SAME_SIZE:
12171 if (sec->size != l->sec->size)
12172 (*_bfd_error_handler)
c93625e2 12173 (_("%B: duplicate section `%A' has different size"),
d003868e 12174 abfd, sec);
082b7297 12175 break;
ea5158d8
DJ
12176
12177 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
12178 if (sec->size != l->sec->size)
12179 (*_bfd_error_handler)
c93625e2 12180 (_("%B: duplicate section `%A' has different size"),
ea5158d8
DJ
12181 abfd, sec);
12182 else if (sec->size != 0)
12183 {
12184 bfd_byte *sec_contents, *l_sec_contents;
12185
12186 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
12187 (*_bfd_error_handler)
c93625e2 12188 (_("%B: warning: could not read contents of section `%A'"),
ea5158d8
DJ
12189 abfd, sec);
12190 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
12191 &l_sec_contents))
12192 (*_bfd_error_handler)
c93625e2 12193 (_("%B: warning: could not read contents of section `%A'"),
ea5158d8
DJ
12194 l->sec->owner, l->sec);
12195 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
12196 (*_bfd_error_handler)
c93625e2 12197 (_("%B: warning: duplicate section `%A' has different contents"),
ea5158d8
DJ
12198 abfd, sec);
12199
12200 if (sec_contents)
12201 free (sec_contents);
12202 if (l_sec_contents)
12203 free (l_sec_contents);
12204 }
12205 break;
082b7297
L
12206 }
12207
12208 /* Set the output_section field so that lang_add_section
12209 does not create a lang_input_section structure for this
12210 section. Since there might be a symbol in the section
12211 being discarded, we must retain a pointer to the section
12212 which we are really going to use. */
12213 sec->output_section = bfd_abs_section_ptr;
12214 sec->kept_section = l->sec;
3b36f7e6 12215
082b7297 12216 if (flags & SEC_GROUP)
3d7f7666
L
12217 {
12218 asection *first = elf_next_in_group (sec);
12219 asection *s = first;
12220
12221 while (s != NULL)
12222 {
12223 s->output_section = bfd_abs_section_ptr;
12224 /* Record which group discards it. */
12225 s->kept_section = l->sec;
12226 s = elf_next_in_group (s);
12227 /* These lists are circular. */
12228 if (s == first)
12229 break;
12230 }
12231 }
082b7297
L
12232
12233 return;
12234 }
12235 }
12236
c2370991
AM
12237 /* A single member comdat group section may be discarded by a
12238 linkonce section and vice versa. */
12239
12240 if ((flags & SEC_GROUP) != 0)
3d7f7666 12241 {
c2370991
AM
12242 asection *first = elf_next_in_group (sec);
12243
12244 if (first != NULL && elf_next_in_group (first) == first)
12245 /* Check this single member group against linkonce sections. */
12246 for (l = already_linked_list->entry; l != NULL; l = l->next)
12247 if ((l->sec->flags & SEC_GROUP) == 0
12248 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
12249 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
12250 {
12251 first->output_section = bfd_abs_section_ptr;
12252 first->kept_section = l->sec;
12253 sec->output_section = bfd_abs_section_ptr;
12254 break;
12255 }
3d7f7666
L
12256 }
12257 else
c2370991 12258 /* Check this linkonce section against single member groups. */
6d2cd210
JJ
12259 for (l = already_linked_list->entry; l != NULL; l = l->next)
12260 if (l->sec->flags & SEC_GROUP)
12261 {
12262 asection *first = elf_next_in_group (l->sec);
12263
12264 if (first != NULL
12265 && elf_next_in_group (first) == first
c0f00686 12266 && bfd_elf_match_symbols_in_sections (first, sec, info))
6d2cd210
JJ
12267 {
12268 sec->output_section = bfd_abs_section_ptr;
c2370991 12269 sec->kept_section = first;
6d2cd210
JJ
12270 break;
12271 }
12272 }
12273
082b7297 12274 /* This is the first section with this name. Record it. */
a6626e8c
MS
12275 if (! bfd_section_already_linked_table_insert (already_linked_list, sec))
12276 info->callbacks->einfo (_("%F%P: already_linked_table: %E"));
082b7297 12277}
81e1b023 12278
a4d8e49b
L
12279bfd_boolean
12280_bfd_elf_common_definition (Elf_Internal_Sym *sym)
12281{
12282 return sym->st_shndx == SHN_COMMON;
12283}
12284
12285unsigned int
12286_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
12287{
12288 return SHN_COMMON;
12289}
12290
12291asection *
12292_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
12293{
12294 return bfd_com_section_ptr;
12295}
This page took 1.162368 seconds and 4 git commands to generate.