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