]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elflink.c
PR27311 again, ld.bfd (symbol from plugin): undefined reference
[thirdparty/binutils-gdb.git] / bfd / elflink.c
CommitLineData
252b5132 1/* ELF linking support for BFD.
250d07de 2 Copyright (C) 1995-2021 Free Software Foundation, Inc.
252b5132 3
8fdd7217 4 This file is part of BFD, the Binary File Descriptor library.
252b5132 5
8fdd7217
NC
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
cd123cb7 8 the Free Software Foundation; either version 3 of the License, or
8fdd7217 9 (at your option) any later version.
252b5132 10
8fdd7217
NC
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
252b5132 15
8fdd7217
NC
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
cd123cb7
NC
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
252b5132 20
252b5132 21#include "sysdep.h"
3db64b00 22#include "bfd.h"
252b5132
RH
23#include "bfdlink.h"
24#include "libbfd.h"
25#define ARCH_SIZE 0
26#include "elf-bfd.h"
4ad4eba5 27#include "safe-ctype.h"
ccf2f652 28#include "libiberty.h"
66eb6687 29#include "objalloc.h"
08ce1d72 30#if BFD_SUPPORTS_PLUGINS
7d0b9ebc 31#include "plugin-api.h"
7dc3990e
L
32#include "plugin.h"
33#endif
252b5132 34
4b69ce9b
AM
35#ifdef HAVE_LIMITS_H
36#include <limits.h>
37#endif
38#ifndef CHAR_BIT
39#define CHAR_BIT 8
40#endif
41
28caa186
AM
42/* This struct is used to pass information to routines called via
43 elf_link_hash_traverse which must return failure. */
44
45struct elf_info_failed
46{
47 struct bfd_link_info *info;
28caa186
AM
48 bfd_boolean failed;
49};
50
51/* This structure is used to pass information to
52 _bfd_elf_link_find_version_dependencies. */
53
54struct elf_find_verdep_info
55{
56 /* General link information. */
57 struct bfd_link_info *info;
58 /* The number of dependencies. */
59 unsigned int vers;
60 /* Whether we had a failure. */
61 bfd_boolean failed;
62};
63
64static bfd_boolean _bfd_elf_fix_symbol_flags
65 (struct elf_link_hash_entry *, struct elf_info_failed *);
66
2f0c68f2
CM
67asection *
68_bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
69 unsigned long r_symndx,
70 bfd_boolean discard)
71{
72 if (r_symndx >= cookie->locsymcount
73 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
74 {
75 struct elf_link_hash_entry *h;
76
77 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
78
79 while (h->root.type == bfd_link_hash_indirect
80 || h->root.type == bfd_link_hash_warning)
81 h = (struct elf_link_hash_entry *) h->root.u.i.link;
82
83 if ((h->root.type == bfd_link_hash_defined
84 || h->root.type == bfd_link_hash_defweak)
85 && discarded_section (h->root.u.def.section))
07d6d2b8 86 return h->root.u.def.section;
2f0c68f2
CM
87 else
88 return NULL;
89 }
90 else
91 {
92 /* It's not a relocation against a global symbol,
93 but it could be a relocation against a local
94 symbol for a discarded section. */
95 asection *isec;
96 Elf_Internal_Sym *isym;
97
98 /* Need to: get the symbol; get the section. */
99 isym = &cookie->locsyms[r_symndx];
100 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
101 if (isec != NULL
102 && discard ? discarded_section (isec) : 1)
103 return isec;
104 }
105 return NULL;
106}
107
d98685ac
AM
108/* Define a symbol in a dynamic linkage section. */
109
110struct elf_link_hash_entry *
111_bfd_elf_define_linkage_sym (bfd *abfd,
112 struct bfd_link_info *info,
113 asection *sec,
114 const char *name)
115{
116 struct elf_link_hash_entry *h;
117 struct bfd_link_hash_entry *bh;
ccabcbe5 118 const struct elf_backend_data *bed;
d98685ac
AM
119
120 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
121 if (h != NULL)
122 {
123 /* Zap symbol defined in an as-needed lib that wasn't linked.
124 This is a symptom of a larger problem: Absolute symbols
125 defined in shared libraries can't be overridden, because we
126 lose the link to the bfd which is via the symbol section. */
127 h->root.type = bfd_link_hash_new;
ad32986f 128 bh = &h->root;
d98685ac 129 }
ad32986f
NC
130 else
131 bh = NULL;
d98685ac 132
cf18fda4 133 bed = get_elf_backend_data (abfd);
d98685ac 134 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
cf18fda4 135 sec, 0, NULL, FALSE, bed->collect,
d98685ac
AM
136 &bh))
137 return NULL;
138 h = (struct elf_link_hash_entry *) bh;
ad32986f 139 BFD_ASSERT (h != NULL);
d98685ac 140 h->def_regular = 1;
e28df02b 141 h->non_elf = 0;
12b2843a 142 h->root.linker_def = 1;
d98685ac 143 h->type = STT_OBJECT;
00b7642b
AM
144 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
145 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
d98685ac 146
ccabcbe5 147 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
d98685ac
AM
148 return h;
149}
150
b34976b6 151bfd_boolean
268b6b39 152_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
153{
154 flagword flags;
aad5d350 155 asection *s;
252b5132 156 struct elf_link_hash_entry *h;
9c5bfbb7 157 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 158 struct elf_link_hash_table *htab = elf_hash_table (info);
252b5132
RH
159
160 /* This function may be called more than once. */
ce558b89 161 if (htab->sgot != NULL)
b34976b6 162 return TRUE;
252b5132 163
e5a52504 164 flags = bed->dynamic_sec_flags;
252b5132 165
14b2f831
AM
166 s = bfd_make_section_anyway_with_flags (abfd,
167 (bed->rela_plts_and_copies_p
168 ? ".rela.got" : ".rel.got"),
169 (bed->dynamic_sec_flags
170 | SEC_READONLY));
6de2ae4a 171 if (s == NULL
fd361982 172 || !bfd_set_section_alignment (s, bed->s->log_file_align))
6de2ae4a
L
173 return FALSE;
174 htab->srelgot = s;
252b5132 175
14b2f831 176 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
64e77c6d 177 if (s == NULL
fd361982 178 || !bfd_set_section_alignment (s, bed->s->log_file_align))
64e77c6d
L
179 return FALSE;
180 htab->sgot = s;
181
252b5132
RH
182 if (bed->want_got_plt)
183 {
14b2f831 184 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
252b5132 185 if (s == NULL
fd361982 186 || !bfd_set_section_alignment (s, bed->s->log_file_align))
b34976b6 187 return FALSE;
6de2ae4a 188 htab->sgotplt = s;
252b5132
RH
189 }
190
64e77c6d
L
191 /* The first bit of the global offset table is the header. */
192 s->size += bed->got_header_size;
193
2517a57f
AM
194 if (bed->want_got_sym)
195 {
196 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
197 (or .got.plt) section. We don't do this in the linker script
198 because we don't want to define the symbol if we are not creating
199 a global offset table. */
6de2ae4a
L
200 h = _bfd_elf_define_linkage_sym (abfd, info, s,
201 "_GLOBAL_OFFSET_TABLE_");
2517a57f 202 elf_hash_table (info)->hgot = h;
d98685ac
AM
203 if (h == NULL)
204 return FALSE;
2517a57f 205 }
252b5132 206
b34976b6 207 return TRUE;
252b5132
RH
208}
209\f
7e9f0867
AM
210/* Create a strtab to hold the dynamic symbol names. */
211static bfd_boolean
212_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
213{
214 struct elf_link_hash_table *hash_table;
215
216 hash_table = elf_hash_table (info);
217 if (hash_table->dynobj == NULL)
6cd255ca
L
218 {
219 /* We may not set dynobj, an input file holding linker created
220 dynamic sections to abfd, which may be a dynamic object with
221 its own dynamic sections. We need to find a normal input file
222 to hold linker created sections if possible. */
223 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
224 {
225 bfd *ibfd;
57963c05 226 asection *s;
6cd255ca 227 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
6645479e 228 if ((ibfd->flags
57963c05
AM
229 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
230 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
4de5434b 231 && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
57963c05
AM
232 && !((s = ibfd->sections) != NULL
233 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
6cd255ca
L
234 {
235 abfd = ibfd;
236 break;
237 }
238 }
239 hash_table->dynobj = abfd;
240 }
7e9f0867
AM
241
242 if (hash_table->dynstr == NULL)
243 {
244 hash_table->dynstr = _bfd_elf_strtab_init ();
245 if (hash_table->dynstr == NULL)
246 return FALSE;
247 }
248 return TRUE;
249}
250
45d6a902
AM
251/* Create some sections which will be filled in with dynamic linking
252 information. ABFD is an input file which requires dynamic sections
253 to be created. The dynamic sections take up virtual memory space
254 when the final executable is run, so we need to create them before
255 addresses are assigned to the output sections. We work out the
256 actual contents and size of these sections later. */
252b5132 257
b34976b6 258bfd_boolean
268b6b39 259_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
252b5132 260{
45d6a902 261 flagword flags;
91d6fa6a 262 asection *s;
9c5bfbb7 263 const struct elf_backend_data *bed;
9637f6ef 264 struct elf_link_hash_entry *h;
252b5132 265
0eddce27 266 if (! is_elf_hash_table (info->hash))
45d6a902
AM
267 return FALSE;
268
269 if (elf_hash_table (info)->dynamic_sections_created)
270 return TRUE;
271
7e9f0867
AM
272 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
273 return FALSE;
45d6a902 274
7e9f0867 275 abfd = elf_hash_table (info)->dynobj;
e5a52504
MM
276 bed = get_elf_backend_data (abfd);
277
278 flags = bed->dynamic_sec_flags;
45d6a902
AM
279
280 /* A dynamically linked executable has a .interp section, but a
281 shared library does not. */
9b8b325a 282 if (bfd_link_executable (info) && !info->nointerp)
252b5132 283 {
14b2f831
AM
284 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
285 flags | SEC_READONLY);
3496cb2a 286 if (s == NULL)
45d6a902
AM
287 return FALSE;
288 }
bb0deeff 289
45d6a902
AM
290 /* Create sections to hold version informations. These are removed
291 if they are not needed. */
14b2f831
AM
292 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
293 flags | SEC_READONLY);
45d6a902 294 if (s == NULL
fd361982 295 || !bfd_set_section_alignment (s, bed->s->log_file_align))
45d6a902
AM
296 return FALSE;
297
14b2f831
AM
298 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
299 flags | SEC_READONLY);
45d6a902 300 if (s == NULL
fd361982 301 || !bfd_set_section_alignment (s, 1))
45d6a902
AM
302 return FALSE;
303
14b2f831
AM
304 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
305 flags | SEC_READONLY);
45d6a902 306 if (s == NULL
fd361982 307 || !bfd_set_section_alignment (s, bed->s->log_file_align))
45d6a902
AM
308 return FALSE;
309
14b2f831
AM
310 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
311 flags | SEC_READONLY);
45d6a902 312 if (s == NULL
fd361982 313 || !bfd_set_section_alignment (s, bed->s->log_file_align))
45d6a902 314 return FALSE;
cae1fbbb 315 elf_hash_table (info)->dynsym = s;
45d6a902 316
14b2f831
AM
317 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
318 flags | SEC_READONLY);
3496cb2a 319 if (s == NULL)
45d6a902
AM
320 return FALSE;
321
14b2f831 322 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
45d6a902 323 if (s == NULL
fd361982 324 || !bfd_set_section_alignment (s, bed->s->log_file_align))
45d6a902
AM
325 return FALSE;
326
327 /* The special symbol _DYNAMIC is always set to the start of the
77cfaee6
AM
328 .dynamic section. We could set _DYNAMIC in a linker script, but we
329 only want to define it if we are, in fact, creating a .dynamic
330 section. We don't want to define it if there is no .dynamic
331 section, since on some ELF platforms the start up code examines it
332 to decide how to initialize the process. */
9637f6ef
L
333 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
334 elf_hash_table (info)->hdynamic = h;
335 if (h == NULL)
45d6a902
AM
336 return FALSE;
337
fdc90cb4
JJ
338 if (info->emit_hash)
339 {
14b2f831
AM
340 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
341 flags | SEC_READONLY);
fdc90cb4 342 if (s == NULL
fd361982 343 || !bfd_set_section_alignment (s, bed->s->log_file_align))
fdc90cb4
JJ
344 return FALSE;
345 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
346 }
347
f16a9783 348 if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL)
fdc90cb4 349 {
14b2f831
AM
350 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
351 flags | SEC_READONLY);
fdc90cb4 352 if (s == NULL
fd361982 353 || !bfd_set_section_alignment (s, bed->s->log_file_align))
fdc90cb4
JJ
354 return FALSE;
355 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
356 4 32-bit words followed by variable count of 64-bit words, then
357 variable count of 32-bit words. */
358 if (bed->s->arch_size == 64)
359 elf_section_data (s)->this_hdr.sh_entsize = 0;
360 else
361 elf_section_data (s)->this_hdr.sh_entsize = 4;
362 }
45d6a902
AM
363
364 /* Let the backend create the rest of the sections. This lets the
365 backend set the right flags. The backend will normally create
366 the .got and .plt sections. */
894891db
NC
367 if (bed->elf_backend_create_dynamic_sections == NULL
368 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
45d6a902
AM
369 return FALSE;
370
371 elf_hash_table (info)->dynamic_sections_created = TRUE;
372
373 return TRUE;
374}
375
376/* Create dynamic sections when linking against a dynamic object. */
377
378bfd_boolean
268b6b39 379_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
45d6a902
AM
380{
381 flagword flags, pltflags;
7325306f 382 struct elf_link_hash_entry *h;
45d6a902 383 asection *s;
9c5bfbb7 384 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 385 struct elf_link_hash_table *htab = elf_hash_table (info);
45d6a902 386
252b5132
RH
387 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
388 .rel[a].bss sections. */
e5a52504 389 flags = bed->dynamic_sec_flags;
252b5132
RH
390
391 pltflags = flags;
252b5132 392 if (bed->plt_not_loaded)
6df4d94c
MM
393 /* We do not clear SEC_ALLOC here because we still want the OS to
394 allocate space for the section; it's just that there's nothing
395 to read in from the object file. */
5d1634d7 396 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
6df4d94c
MM
397 else
398 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
252b5132
RH
399 if (bed->plt_readonly)
400 pltflags |= SEC_READONLY;
401
14b2f831 402 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
252b5132 403 if (s == NULL
fd361982 404 || !bfd_set_section_alignment (s, bed->plt_alignment))
b34976b6 405 return FALSE;
6de2ae4a 406 htab->splt = s;
252b5132 407
d98685ac
AM
408 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
409 .plt section. */
7325306f
RS
410 if (bed->want_plt_sym)
411 {
412 h = _bfd_elf_define_linkage_sym (abfd, info, s,
413 "_PROCEDURE_LINKAGE_TABLE_");
414 elf_hash_table (info)->hplt = h;
415 if (h == NULL)
416 return FALSE;
417 }
252b5132 418
14b2f831
AM
419 s = bfd_make_section_anyway_with_flags (abfd,
420 (bed->rela_plts_and_copies_p
421 ? ".rela.plt" : ".rel.plt"),
422 flags | SEC_READONLY);
252b5132 423 if (s == NULL
fd361982 424 || !bfd_set_section_alignment (s, bed->s->log_file_align))
b34976b6 425 return FALSE;
6de2ae4a 426 htab->srelplt = s;
252b5132
RH
427
428 if (! _bfd_elf_create_got_section (abfd, info))
b34976b6 429 return FALSE;
252b5132 430
3018b441
RH
431 if (bed->want_dynbss)
432 {
433 /* The .dynbss section is a place to put symbols which are defined
434 by dynamic objects, are referenced by regular objects, and are
435 not functions. We must allocate space for them in the process
436 image and use a R_*_COPY reloc to tell the dynamic linker to
437 initialize them at run time. The linker script puts the .dynbss
438 section into the .bss section of the final image. */
14b2f831 439 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
afbf7e8e 440 SEC_ALLOC | SEC_LINKER_CREATED);
3496cb2a 441 if (s == NULL)
b34976b6 442 return FALSE;
9d19e4fd 443 htab->sdynbss = s;
252b5132 444
5474d94f
AM
445 if (bed->want_dynrelro)
446 {
447 /* Similarly, but for symbols that were originally in read-only
afbf7e8e
AM
448 sections. This section doesn't really need to have contents,
449 but make it like other .data.rel.ro sections. */
5474d94f 450 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
afbf7e8e 451 flags);
5474d94f
AM
452 if (s == NULL)
453 return FALSE;
454 htab->sdynrelro = s;
455 }
456
3018b441 457 /* The .rel[a].bss section holds copy relocs. This section is not
77cfaee6
AM
458 normally needed. We need to create it here, though, so that the
459 linker will map it to an output section. We can't just create it
460 only if we need it, because we will not know whether we need it
461 until we have seen all the input files, and the first time the
462 main linker code calls BFD after examining all the input files
463 (size_dynamic_sections) the input sections have already been
464 mapped to the output sections. If the section turns out not to
465 be needed, we can discard it later. We will never need this
466 section when generating a shared object, since they do not use
467 copy relocs. */
9d19e4fd 468 if (bfd_link_executable (info))
3018b441 469 {
14b2f831
AM
470 s = bfd_make_section_anyway_with_flags (abfd,
471 (bed->rela_plts_and_copies_p
472 ? ".rela.bss" : ".rel.bss"),
473 flags | SEC_READONLY);
3018b441 474 if (s == NULL
fd361982 475 || !bfd_set_section_alignment (s, bed->s->log_file_align))
b34976b6 476 return FALSE;
9d19e4fd 477 htab->srelbss = s;
5474d94f
AM
478
479 if (bed->want_dynrelro)
480 {
481 s = (bfd_make_section_anyway_with_flags
482 (abfd, (bed->rela_plts_and_copies_p
483 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
484 flags | SEC_READONLY));
485 if (s == NULL
fd361982 486 || !bfd_set_section_alignment (s, bed->s->log_file_align))
5474d94f
AM
487 return FALSE;
488 htab->sreldynrelro = s;
489 }
3018b441 490 }
252b5132
RH
491 }
492
b34976b6 493 return TRUE;
252b5132
RH
494}
495\f
252b5132
RH
496/* Record a new dynamic symbol. We record the dynamic symbols as we
497 read the input files, since we need to have a list of all of them
498 before we can determine the final sizes of the output sections.
499 Note that we may actually call this function even though we are not
500 going to output any dynamic symbols; in some cases we know that a
501 symbol should be in the dynamic symbol table, but only if there is
502 one. */
503
b34976b6 504bfd_boolean
c152c796
AM
505bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
506 struct elf_link_hash_entry *h)
252b5132
RH
507{
508 if (h->dynindx == -1)
509 {
2b0f7ef9 510 struct elf_strtab_hash *dynstr;
68b6ddd0 511 char *p;
252b5132 512 const char *name;
ef53be89 513 size_t indx;
252b5132 514
a896df97
AM
515 if (h->root.type == bfd_link_hash_defined
516 || h->root.type == bfd_link_hash_defweak)
517 {
518 /* An IR symbol should not be made dynamic. */
519 if (h->root.u.def.section != NULL
520 && h->root.u.def.section->owner != NULL
521 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)
522 return TRUE;
523 }
524
7a13edea
NC
525 /* XXX: The ABI draft says the linker must turn hidden and
526 internal symbols into STB_LOCAL symbols when producing the
527 DSO. However, if ld.so honors st_other in the dynamic table,
528 this would not be necessary. */
529 switch (ELF_ST_VISIBILITY (h->other))
530 {
531 case STV_INTERNAL:
532 case STV_HIDDEN:
9d6eee78
L
533 if (h->root.type != bfd_link_hash_undefined
534 && h->root.type != bfd_link_hash_undefweak)
38048eb9 535 {
f5385ebf 536 h->forced_local = 1;
c8bad65e
AM
537 if (!elf_hash_table (info)->is_relocatable_executable
538 || ((h->root.type == bfd_link_hash_defined
539 || h->root.type == bfd_link_hash_defweak)
c4566785 540 && h->root.u.def.section->owner != NULL
c8bad65e
AM
541 && h->root.u.def.section->owner->no_export)
542 || (h->root.type == bfd_link_hash_common
c4566785 543 && h->root.u.c.p->section->owner != NULL
c8bad65e 544 && h->root.u.c.p->section->owner->no_export))
67687978 545 return TRUE;
7a13edea 546 }
0444bdd4 547
7a13edea
NC
548 default:
549 break;
550 }
551
252b5132
RH
552 h->dynindx = elf_hash_table (info)->dynsymcount;
553 ++elf_hash_table (info)->dynsymcount;
554
555 dynstr = elf_hash_table (info)->dynstr;
556 if (dynstr == NULL)
557 {
558 /* Create a strtab to hold the dynamic symbol names. */
2b0f7ef9 559 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
252b5132 560 if (dynstr == NULL)
b34976b6 561 return FALSE;
252b5132
RH
562 }
563
564 /* We don't put any version information in the dynamic string
aad5d350 565 table. */
252b5132
RH
566 name = h->root.root.string;
567 p = strchr (name, ELF_VER_CHR);
68b6ddd0
AM
568 if (p != NULL)
569 /* We know that the p points into writable memory. In fact,
570 there are only a few symbols that have read-only names, being
571 those like _GLOBAL_OFFSET_TABLE_ that are created specially
572 by the backends. Most symbols will have names pointing into
573 an ELF string table read from a file, or to objalloc memory. */
574 *p = 0;
575
576 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
577
578 if (p != NULL)
579 *p = ELF_VER_CHR;
252b5132 580
ef53be89 581 if (indx == (size_t) -1)
b34976b6 582 return FALSE;
252b5132
RH
583 h->dynstr_index = indx;
584 }
585
b34976b6 586 return TRUE;
252b5132 587}
45d6a902 588\f
55255dae
L
589/* Mark a symbol dynamic. */
590
28caa186 591static void
55255dae 592bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
40b36307
L
593 struct elf_link_hash_entry *h,
594 Elf_Internal_Sym *sym)
55255dae 595{
40b36307 596 struct bfd_elf_dynamic_list *d = info->dynamic_list;
55255dae 597
40b36307 598 /* It may be called more than once on the same H. */
0e1862bb 599 if(h->dynamic || bfd_link_relocatable (info))
55255dae
L
600 return;
601
40b36307
L
602 if ((info->dynamic_data
603 && (h->type == STT_OBJECT
b8871f35 604 || h->type == STT_COMMON
40b36307 605 || (sym != NULL
b8871f35
L
606 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
607 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
a0c8462f 608 || (d != NULL
73ec947d 609 && h->non_elf
40b36307 610 && (*d->match) (&d->head, NULL, h->root.root.string)))
416c34d6
L
611 {
612 h->dynamic = 1;
613 /* NB: If a symbol is made dynamic by --dynamic-list, it has
614 non-IR reference. */
615 h->root.non_ir_ref_dynamic = 1;
616 }
55255dae
L
617}
618
45d6a902
AM
619/* Record an assignment to a symbol made by a linker script. We need
620 this in case some dynamic object refers to this symbol. */
621
622bfd_boolean
fe21a8fc
L
623bfd_elf_record_link_assignment (bfd *output_bfd,
624 struct bfd_link_info *info,
268b6b39 625 const char *name,
fe21a8fc
L
626 bfd_boolean provide,
627 bfd_boolean hidden)
45d6a902 628{
00cbee0a 629 struct elf_link_hash_entry *h, *hv;
4ea42fb7 630 struct elf_link_hash_table *htab;
00cbee0a 631 const struct elf_backend_data *bed;
45d6a902 632
0eddce27 633 if (!is_elf_hash_table (info->hash))
45d6a902
AM
634 return TRUE;
635
4ea42fb7
AM
636 htab = elf_hash_table (info);
637 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
45d6a902 638 if (h == NULL)
4ea42fb7 639 return provide;
45d6a902 640
8e2a4f11
AM
641 if (h->root.type == bfd_link_hash_warning)
642 h = (struct elf_link_hash_entry *) h->root.u.i.link;
643
0f550b3d
L
644 if (h->versioned == unknown)
645 {
646 /* Set versioned if symbol version is unknown. */
647 char *version = strrchr (name, ELF_VER_CHR);
648 if (version)
649 {
650 if (version > name && version[-1] != ELF_VER_CHR)
651 h->versioned = versioned_hidden;
652 else
653 h->versioned = versioned;
654 }
655 }
656
73ec947d
AM
657 /* Symbols defined in a linker script but not referenced anywhere
658 else will have non_elf set. */
659 if (h->non_elf)
660 {
661 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
662 h->non_elf = 0;
663 }
664
00cbee0a 665 switch (h->root.type)
77cfaee6 666 {
00cbee0a
L
667 case bfd_link_hash_defined:
668 case bfd_link_hash_defweak:
669 case bfd_link_hash_common:
670 break;
671 case bfd_link_hash_undefweak:
672 case bfd_link_hash_undefined:
673 /* Since we're defining the symbol, don't let it seem to have not
674 been defined. record_dynamic_symbol and size_dynamic_sections
675 may depend on this. */
4ea42fb7 676 h->root.type = bfd_link_hash_new;
77cfaee6
AM
677 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
678 bfd_link_repair_undef_list (&htab->root);
00cbee0a
L
679 break;
680 case bfd_link_hash_new:
00cbee0a
L
681 break;
682 case bfd_link_hash_indirect:
683 /* We had a versioned symbol in a dynamic library. We make the
a0c8462f 684 the versioned symbol point to this one. */
00cbee0a
L
685 bed = get_elf_backend_data (output_bfd);
686 hv = h;
687 while (hv->root.type == bfd_link_hash_indirect
688 || hv->root.type == bfd_link_hash_warning)
689 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
690 /* We don't need to update h->root.u since linker will set them
691 later. */
692 h->root.type = bfd_link_hash_undefined;
693 hv->root.type = bfd_link_hash_indirect;
694 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
695 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
696 break;
8e2a4f11
AM
697 default:
698 BFD_FAIL ();
c2596ca5 699 return FALSE;
55255dae 700 }
45d6a902
AM
701
702 /* If this symbol is being provided by the linker script, and it is
703 currently defined by a dynamic object, but not by a regular
704 object, then mark it as undefined so that the generic linker will
705 force the correct value. */
706 if (provide
f5385ebf
AM
707 && h->def_dynamic
708 && !h->def_regular)
45d6a902
AM
709 h->root.type = bfd_link_hash_undefined;
710
48e30f52
L
711 /* If this symbol is currently defined by a dynamic object, but not
712 by a regular object, then clear out any version information because
713 the symbol will not be associated with the dynamic object any
714 more. */
715 if (h->def_dynamic && !h->def_regular)
b531344c
MR
716 h->verinfo.verdef = NULL;
717
718 /* Make sure this symbol is not garbage collected. */
719 h->mark = 1;
45d6a902 720
f5385ebf 721 h->def_regular = 1;
45d6a902 722
eb8476a6 723 if (hidden)
fe21a8fc 724 {
91d6fa6a 725 bed = get_elf_backend_data (output_bfd);
b8297068
AM
726 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
727 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
fe21a8fc
L
728 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
729 }
730
6fa3860b
PB
731 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
732 and executables. */
0e1862bb 733 if (!bfd_link_relocatable (info)
6fa3860b
PB
734 && h->dynindx != -1
735 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
736 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
737 h->forced_local = 1;
738
f5385ebf
AM
739 if ((h->def_dynamic
740 || h->ref_dynamic
6b3b0ab8
L
741 || bfd_link_dll (info)
742 || elf_hash_table (info)->is_relocatable_executable)
34a87bb0 743 && !h->forced_local
45d6a902
AM
744 && h->dynindx == -1)
745 {
c152c796 746 if (! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
747 return FALSE;
748
749 /* If this is a weak defined symbol, and we know a corresponding
750 real symbol from the same dynamic object, make sure the real
751 symbol is also made into a dynamic symbol. */
60d67dc8 752 if (h->is_weakalias)
45d6a902 753 {
60d67dc8
AM
754 struct elf_link_hash_entry *def = weakdef (h);
755
756 if (def->dynindx == -1
757 && !bfd_elf_link_record_dynamic_symbol (info, def))
45d6a902
AM
758 return FALSE;
759 }
760 }
761
762 return TRUE;
763}
42751cf3 764
8c58d23b
AM
765/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
766 success, and 2 on a failure caused by attempting to record a symbol
767 in a discarded section, eg. a discarded link-once section symbol. */
768
769int
c152c796
AM
770bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
771 bfd *input_bfd,
772 long input_indx)
8c58d23b 773{
986f0783 774 size_t amt;
8c58d23b
AM
775 struct elf_link_local_dynamic_entry *entry;
776 struct elf_link_hash_table *eht;
777 struct elf_strtab_hash *dynstr;
ef53be89 778 size_t dynstr_index;
8c58d23b
AM
779 char *name;
780 Elf_External_Sym_Shndx eshndx;
781 char esym[sizeof (Elf64_External_Sym)];
782
0eddce27 783 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
784 return 0;
785
786 /* See if the entry exists already. */
787 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
788 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
789 return 1;
790
791 amt = sizeof (*entry);
a50b1753 792 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
8c58d23b
AM
793 if (entry == NULL)
794 return 0;
795
796 /* Go find the symbol, so that we can find it's name. */
797 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 798 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
799 {
800 bfd_release (input_bfd, entry);
801 return 0;
802 }
803
804 if (entry->isym.st_shndx != SHN_UNDEF
4fbb74a6 805 && entry->isym.st_shndx < SHN_LORESERVE)
8c58d23b
AM
806 {
807 asection *s;
808
809 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
810 if (s == NULL || bfd_is_abs_section (s->output_section))
811 {
812 /* We can still bfd_release here as nothing has done another
813 bfd_alloc. We can't do this later in this function. */
814 bfd_release (input_bfd, entry);
815 return 2;
816 }
817 }
818
819 name = (bfd_elf_string_from_elf_section
820 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
821 entry->isym.st_name));
822
823 dynstr = elf_hash_table (info)->dynstr;
824 if (dynstr == NULL)
825 {
826 /* Create a strtab to hold the dynamic symbol names. */
827 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
828 if (dynstr == NULL)
829 return 0;
830 }
831
b34976b6 832 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
ef53be89 833 if (dynstr_index == (size_t) -1)
8c58d23b
AM
834 return 0;
835 entry->isym.st_name = dynstr_index;
836
837 eht = elf_hash_table (info);
838
839 entry->next = eht->dynlocal;
840 eht->dynlocal = entry;
841 entry->input_bfd = input_bfd;
842 entry->input_indx = input_indx;
843 eht->dynsymcount++;
844
845 /* Whatever binding the symbol had before, it's now local. */
846 entry->isym.st_info
847 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
848
849 /* The dynindx will be set at the end of size_dynamic_sections. */
850
851 return 1;
852}
853
30b30c21 854/* Return the dynindex of a local dynamic symbol. */
42751cf3 855
30b30c21 856long
268b6b39
AM
857_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
858 bfd *input_bfd,
859 long input_indx)
30b30c21
RH
860{
861 struct elf_link_local_dynamic_entry *e;
862
863 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
864 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
865 return e->dynindx;
866 return -1;
867}
868
869/* This function is used to renumber the dynamic symbols, if some of
870 them are removed because they are marked as local. This is called
871 via elf_link_hash_traverse. */
872
b34976b6 873static bfd_boolean
268b6b39
AM
874elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
875 void *data)
42751cf3 876{
a50b1753 877 size_t *count = (size_t *) data;
30b30c21 878
6fa3860b
PB
879 if (h->forced_local)
880 return TRUE;
881
882 if (h->dynindx != -1)
883 h->dynindx = ++(*count);
884
885 return TRUE;
886}
887
888
889/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
890 STB_LOCAL binding. */
891
892static bfd_boolean
893elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
894 void *data)
895{
a50b1753 896 size_t *count = (size_t *) data;
6fa3860b 897
6fa3860b
PB
898 if (!h->forced_local)
899 return TRUE;
900
42751cf3 901 if (h->dynindx != -1)
30b30c21
RH
902 h->dynindx = ++(*count);
903
b34976b6 904 return TRUE;
42751cf3 905}
30b30c21 906
aee6f5b4
AO
907/* Return true if the dynamic symbol for a given section should be
908 omitted when creating a shared library. */
909bfd_boolean
d00dd7dc
AM
910_bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
911 struct bfd_link_info *info,
912 asection *p)
aee6f5b4 913{
74541ad4 914 struct elf_link_hash_table *htab;
ca55926c 915 asection *ip;
74541ad4 916
aee6f5b4
AO
917 switch (elf_section_data (p)->this_hdr.sh_type)
918 {
919 case SHT_PROGBITS:
920 case SHT_NOBITS:
921 /* If sh_type is yet undecided, assume it could be
922 SHT_PROGBITS/SHT_NOBITS. */
923 case SHT_NULL:
74541ad4 924 htab = elf_hash_table (info);
74541ad4
AM
925 if (htab->text_index_section != NULL)
926 return p != htab->text_index_section && p != htab->data_index_section;
927
ca55926c 928 return (htab->dynobj != NULL
3d4d4302 929 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
ca55926c 930 && ip->output_section == p);
aee6f5b4
AO
931
932 /* There shouldn't be section relative relocations
933 against any other section. */
934 default:
935 return TRUE;
936 }
937}
938
d00dd7dc
AM
939bfd_boolean
940_bfd_elf_omit_section_dynsym_all
941 (bfd *output_bfd ATTRIBUTE_UNUSED,
942 struct bfd_link_info *info ATTRIBUTE_UNUSED,
943 asection *p ATTRIBUTE_UNUSED)
944{
945 return TRUE;
946}
947
062e2358 948/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
949 symbol for each output section, which come first. Next come symbols
950 which have been forced to local binding. Then all of the back-end
951 allocated local dynamic syms, followed by the rest of the global
63f452a8
AM
952 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
953 (This prevents the early call before elf_backend_init_index_section
954 and strip_excluded_output_sections setting dynindx for sections
955 that are stripped.) */
30b30c21 956
554220db
AM
957static unsigned long
958_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
959 struct bfd_link_info *info,
960 unsigned long *section_sym_count)
30b30c21
RH
961{
962 unsigned long dynsymcount = 0;
63f452a8 963 bfd_boolean do_sec = section_sym_count != NULL;
30b30c21 964
0e1862bb
L
965 if (bfd_link_pic (info)
966 || elf_hash_table (info)->is_relocatable_executable)
30b30c21 967 {
aee6f5b4 968 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
969 asection *p;
970 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 971 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4 972 && (p->flags & SEC_ALLOC) != 0
7f923b7f 973 && elf_hash_table (info)->dynamic_relocs
aee6f5b4 974 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
63f452a8
AM
975 {
976 ++dynsymcount;
977 if (do_sec)
978 elf_section_data (p)->dynindx = dynsymcount;
979 }
980 else if (do_sec)
74541ad4 981 elf_section_data (p)->dynindx = 0;
30b30c21 982 }
63f452a8
AM
983 if (do_sec)
984 *section_sym_count = dynsymcount;
30b30c21 985
6fa3860b
PB
986 elf_link_hash_traverse (elf_hash_table (info),
987 elf_link_renumber_local_hash_table_dynsyms,
988 &dynsymcount);
989
30b30c21
RH
990 if (elf_hash_table (info)->dynlocal)
991 {
992 struct elf_link_local_dynamic_entry *p;
993 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
994 p->dynindx = ++dynsymcount;
995 }
90ac2420 996 elf_hash_table (info)->local_dynsymcount = dynsymcount;
30b30c21
RH
997
998 elf_link_hash_traverse (elf_hash_table (info),
999 elf_link_renumber_hash_table_dynsyms,
1000 &dynsymcount);
1001
d5486c43
L
1002 /* There is an unused NULL entry at the head of the table which we
1003 must account for in our count even if the table is empty since it
1004 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
1005 .dynamic section. */
1006 dynsymcount++;
30b30c21 1007
ccabcbe5
AM
1008 elf_hash_table (info)->dynsymcount = dynsymcount;
1009 return dynsymcount;
30b30c21 1010}
252b5132 1011
54ac0771
L
1012/* Merge st_other field. */
1013
1014static void
1015elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
5160d0f3 1016 unsigned int st_other, asection *sec,
cd3416da 1017 bfd_boolean definition, bfd_boolean dynamic)
54ac0771
L
1018{
1019 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1020
1021 /* If st_other has a processor-specific meaning, specific
cd3416da 1022 code might be needed here. */
54ac0771 1023 if (bed->elf_backend_merge_symbol_attribute)
5160d0f3 1024 (*bed->elf_backend_merge_symbol_attribute) (h, st_other, definition,
54ac0771
L
1025 dynamic);
1026
cd3416da 1027 if (!dynamic)
54ac0771 1028 {
5160d0f3 1029 unsigned symvis = ELF_ST_VISIBILITY (st_other);
cd3416da 1030 unsigned hvis = ELF_ST_VISIBILITY (h->other);
54ac0771 1031
cd3416da
AM
1032 /* Keep the most constraining visibility. Leave the remainder
1033 of the st_other field to elf_backend_merge_symbol_attribute. */
1034 if (symvis - 1 < hvis - 1)
1035 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
54ac0771 1036 }
b8417128 1037 else if (definition
5160d0f3 1038 && ELF_ST_VISIBILITY (st_other) != STV_DEFAULT
b8417128 1039 && (sec->flags & SEC_READONLY) == 0)
6cabe1ea 1040 h->protected_def = 1;
54ac0771
L
1041}
1042
4f3fedcf
AM
1043/* This function is called when we want to merge a new symbol with an
1044 existing symbol. It handles the various cases which arise when we
1045 find a definition in a dynamic object, or when there is already a
1046 definition in a dynamic object. The new symbol is described by
1047 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1048 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1049 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1050 of an old common symbol. We set OVERRIDE if the old symbol is
1051 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1052 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1053 to change. By OK to change, we mean that we shouldn't warn if the
1054 type or size does change. */
45d6a902 1055
8a56bd02 1056static bfd_boolean
268b6b39
AM
1057_bfd_elf_merge_symbol (bfd *abfd,
1058 struct bfd_link_info *info,
1059 const char *name,
1060 Elf_Internal_Sym *sym,
1061 asection **psec,
1062 bfd_vma *pvalue,
4f3fedcf
AM
1063 struct elf_link_hash_entry **sym_hash,
1064 bfd **poldbfd,
37a9e49a 1065 bfd_boolean *pold_weak,
af44c138 1066 unsigned int *pold_alignment,
268b6b39 1067 bfd_boolean *skip,
7ba11550 1068 bfd **override,
268b6b39 1069 bfd_boolean *type_change_ok,
6e33951e
L
1070 bfd_boolean *size_change_ok,
1071 bfd_boolean *matched)
252b5132 1072{
7479dfd4 1073 asection *sec, *oldsec;
45d6a902 1074 struct elf_link_hash_entry *h;
90c984fc 1075 struct elf_link_hash_entry *hi;
45d6a902
AM
1076 struct elf_link_hash_entry *flip;
1077 int bind;
1078 bfd *oldbfd;
1079 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
0a36a439 1080 bfd_boolean newweak, oldweak, newfunc, oldfunc;
a4d8e49b 1081 const struct elf_backend_data *bed;
6e33951e 1082 char *new_version;
93f4de39 1083 bfd_boolean default_sym = *matched;
45d6a902
AM
1084
1085 *skip = FALSE;
7ba11550 1086 *override = NULL;
45d6a902
AM
1087
1088 sec = *psec;
1089 bind = ELF_ST_BIND (sym->st_info);
1090
1091 if (! bfd_is_und_section (sec))
1092 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1093 else
1094 h = ((struct elf_link_hash_entry *)
1095 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1096 if (h == NULL)
1097 return FALSE;
1098 *sym_hash = h;
252b5132 1099
88ba32a0
L
1100 bed = get_elf_backend_data (abfd);
1101
6e33951e 1102 /* NEW_VERSION is the symbol version of the new symbol. */
422f1182 1103 if (h->versioned != unversioned)
6e33951e 1104 {
422f1182
L
1105 /* Symbol version is unknown or versioned. */
1106 new_version = strrchr (name, ELF_VER_CHR);
1107 if (new_version)
1108 {
1109 if (h->versioned == unknown)
1110 {
1111 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1112 h->versioned = versioned_hidden;
1113 else
1114 h->versioned = versioned;
1115 }
1116 new_version += 1;
1117 if (new_version[0] == '\0')
1118 new_version = NULL;
1119 }
1120 else
1121 h->versioned = unversioned;
6e33951e 1122 }
422f1182
L
1123 else
1124 new_version = NULL;
6e33951e 1125
90c984fc
L
1126 /* For merging, we only care about real symbols. But we need to make
1127 sure that indirect symbol dynamic flags are updated. */
1128 hi = h;
45d6a902
AM
1129 while (h->root.type == bfd_link_hash_indirect
1130 || h->root.type == bfd_link_hash_warning)
1131 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1132
6e33951e
L
1133 if (!*matched)
1134 {
1135 if (hi == h || h->root.type == bfd_link_hash_new)
1136 *matched = TRUE;
1137 else
1138 {
ae7683d2 1139 /* OLD_HIDDEN is true if the existing symbol is only visible
6e33951e 1140 to the symbol with the same symbol version. NEW_HIDDEN is
ae7683d2 1141 true if the new symbol is only visible to the symbol with
6e33951e 1142 the same symbol version. */
422f1182
L
1143 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1144 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
6e33951e
L
1145 if (!old_hidden && !new_hidden)
1146 /* The new symbol matches the existing symbol if both
1147 aren't hidden. */
1148 *matched = TRUE;
1149 else
1150 {
1151 /* OLD_VERSION is the symbol version of the existing
1152 symbol. */
422f1182
L
1153 char *old_version;
1154
1155 if (h->versioned >= versioned)
1156 old_version = strrchr (h->root.root.string,
1157 ELF_VER_CHR) + 1;
1158 else
1159 old_version = NULL;
6e33951e
L
1160
1161 /* The new symbol matches the existing symbol if they
1162 have the same symbol version. */
1163 *matched = (old_version == new_version
1164 || (old_version != NULL
1165 && new_version != NULL
1166 && strcmp (old_version, new_version) == 0));
1167 }
1168 }
1169 }
1170
934bce08
AM
1171 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1172 existing symbol. */
1173
1174 oldbfd = NULL;
1175 oldsec = NULL;
1176 switch (h->root.type)
1177 {
1178 default:
1179 break;
1180
1181 case bfd_link_hash_undefined:
1182 case bfd_link_hash_undefweak:
1183 oldbfd = h->root.u.undef.abfd;
1184 break;
1185
1186 case bfd_link_hash_defined:
1187 case bfd_link_hash_defweak:
1188 oldbfd = h->root.u.def.section->owner;
1189 oldsec = h->root.u.def.section;
1190 break;
1191
1192 case bfd_link_hash_common:
1193 oldbfd = h->root.u.c.p->section->owner;
1194 oldsec = h->root.u.c.p->section;
1195 if (pold_alignment)
1196 *pold_alignment = h->root.u.c.p->alignment_power;
1197 break;
1198 }
1199 if (poldbfd && *poldbfd == NULL)
1200 *poldbfd = oldbfd;
1201
1202 /* Differentiate strong and weak symbols. */
1203 newweak = bind == STB_WEAK;
1204 oldweak = (h->root.type == bfd_link_hash_defweak
1205 || h->root.type == bfd_link_hash_undefweak);
1206 if (pold_weak)
1207 *pold_weak = oldweak;
1208
40b36307 1209 /* We have to check it for every instance since the first few may be
ee659f1f 1210 references and not all compilers emit symbol type for undefined
40b36307
L
1211 symbols. */
1212 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1213
ee659f1f
AM
1214 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1215 respectively, is from a dynamic object. */
1216
1217 newdyn = (abfd->flags & DYNAMIC) != 0;
1218
1219 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1220 syms and defined syms in dynamic libraries respectively.
1221 ref_dynamic on the other hand can be set for a symbol defined in
1222 a dynamic library, and def_dynamic may not be set; When the
1223 definition in a dynamic lib is overridden by a definition in the
1224 executable use of the symbol in the dynamic lib becomes a
1225 reference to the executable symbol. */
1226 if (newdyn)
1227 {
1228 if (bfd_is_und_section (sec))
1229 {
1230 if (bind != STB_WEAK)
1231 {
1232 h->ref_dynamic_nonweak = 1;
1233 hi->ref_dynamic_nonweak = 1;
1234 }
1235 }
1236 else
1237 {
6e33951e
L
1238 /* Update the existing symbol only if they match. */
1239 if (*matched)
1240 h->dynamic_def = 1;
ee659f1f
AM
1241 hi->dynamic_def = 1;
1242 }
1243 }
1244
45d6a902
AM
1245 /* If we just created the symbol, mark it as being an ELF symbol.
1246 Other than that, there is nothing to do--there is no merge issue
1247 with a newly defined symbol--so we just return. */
1248
1249 if (h->root.type == bfd_link_hash_new)
252b5132 1250 {
f5385ebf 1251 h->non_elf = 0;
45d6a902
AM
1252 return TRUE;
1253 }
252b5132 1254
45d6a902
AM
1255 /* In cases involving weak versioned symbols, we may wind up trying
1256 to merge a symbol with itself. Catch that here, to avoid the
1257 confusion that results if we try to override a symbol with
1258 itself. The additional tests catch cases like
1259 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1260 dynamic object, which we do want to handle here. */
1261 if (abfd == oldbfd
895fa45f 1262 && (newweak || oldweak)
45d6a902 1263 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1264 || !h->def_regular))
45d6a902
AM
1265 return TRUE;
1266
707bba77 1267 olddyn = FALSE;
45d6a902
AM
1268 if (oldbfd != NULL)
1269 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1270 else if (oldsec != NULL)
45d6a902 1271 {
707bba77 1272 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1273 indices used by MIPS ELF. */
707bba77 1274 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1275 }
252b5132 1276
1a3b5c34
AM
1277 /* Handle a case where plugin_notice won't be called and thus won't
1278 set the non_ir_ref flags on the first pass over symbols. */
1279 if (oldbfd != NULL
1280 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1281 && newdyn != olddyn)
1282 {
1283 h->root.non_ir_ref_dynamic = TRUE;
1284 hi->root.non_ir_ref_dynamic = TRUE;
1285 }
1286
45d6a902
AM
1287 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1288 respectively, appear to be a definition rather than reference. */
1289
707bba77 1290 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1291
707bba77
AM
1292 olddef = (h->root.type != bfd_link_hash_undefined
1293 && h->root.type != bfd_link_hash_undefweak
202ac193 1294 && h->root.type != bfd_link_hash_common);
45d6a902 1295
0a36a439
L
1296 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1297 respectively, appear to be a function. */
1298
1299 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1300 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1301
1302 oldfunc = (h->type != STT_NOTYPE
1303 && bed->is_function_type (h->type));
1304
c5d37467 1305 if (!(newfunc && oldfunc)
5b677558
AM
1306 && ELF_ST_TYPE (sym->st_info) != h->type
1307 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1308 && h->type != STT_NOTYPE
c5d37467
AM
1309 && (newdef || bfd_is_com_section (sec))
1310 && (olddef || h->root.type == bfd_link_hash_common))
580a2b6e 1311 {
c5d37467
AM
1312 /* If creating a default indirect symbol ("foo" or "foo@") from
1313 a dynamic versioned definition ("foo@@") skip doing so if
1314 there is an existing regular definition with a different
1315 type. We don't want, for example, a "time" variable in the
1316 executable overriding a "time" function in a shared library. */
1317 if (newdyn
1318 && !olddyn)
1319 {
1320 *skip = TRUE;
1321 return TRUE;
1322 }
1323
1324 /* When adding a symbol from a regular object file after we have
1325 created indirect symbols, undo the indirection and any
1326 dynamic state. */
1327 if (hi != h
1328 && !newdyn
1329 && olddyn)
1330 {
1331 h = hi;
1332 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1333 h->forced_local = 0;
1334 h->ref_dynamic = 0;
1335 h->def_dynamic = 0;
1336 h->dynamic_def = 0;
1337 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1338 {
1339 h->root.type = bfd_link_hash_undefined;
1340 h->root.u.undef.abfd = abfd;
1341 }
1342 else
1343 {
1344 h->root.type = bfd_link_hash_new;
1345 h->root.u.undef.abfd = NULL;
1346 }
1347 return TRUE;
1348 }
580a2b6e
L
1349 }
1350
4c34aff8
AM
1351 /* Check TLS symbols. We don't check undefined symbols introduced
1352 by "ld -u" which have no type (and oldbfd NULL), and we don't
1353 check symbols from plugins because they also have no type. */
1354 if (oldbfd != NULL
1355 && (oldbfd->flags & BFD_PLUGIN) == 0
1356 && (abfd->flags & BFD_PLUGIN) == 0
1357 && ELF_ST_TYPE (sym->st_info) != h->type
1358 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1359 {
1360 bfd *ntbfd, *tbfd;
1361 bfd_boolean ntdef, tdef;
1362 asection *ntsec, *tsec;
1363
1364 if (h->type == STT_TLS)
1365 {
3b36f7e6 1366 ntbfd = abfd;
7479dfd4
L
1367 ntsec = sec;
1368 ntdef = newdef;
1369 tbfd = oldbfd;
1370 tsec = oldsec;
1371 tdef = olddef;
1372 }
1373 else
1374 {
1375 ntbfd = oldbfd;
1376 ntsec = oldsec;
1377 ntdef = olddef;
1378 tbfd = abfd;
1379 tsec = sec;
1380 tdef = newdef;
1381 }
1382
1383 if (tdef && ntdef)
4eca0228 1384 _bfd_error_handler
695344c0 1385 /* xgettext:c-format */
871b3ab2
AM
1386 (_("%s: TLS definition in %pB section %pA "
1387 "mismatches non-TLS definition in %pB section %pA"),
c08bb8dd 1388 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
7479dfd4 1389 else if (!tdef && !ntdef)
4eca0228 1390 _bfd_error_handler
695344c0 1391 /* xgettext:c-format */
871b3ab2
AM
1392 (_("%s: TLS reference in %pB "
1393 "mismatches non-TLS reference in %pB"),
c08bb8dd 1394 h->root.root.string, tbfd, ntbfd);
7479dfd4 1395 else if (tdef)
4eca0228 1396 _bfd_error_handler
695344c0 1397 /* xgettext:c-format */
871b3ab2
AM
1398 (_("%s: TLS definition in %pB section %pA "
1399 "mismatches non-TLS reference in %pB"),
c08bb8dd 1400 h->root.root.string, tbfd, tsec, ntbfd);
7479dfd4 1401 else
4eca0228 1402 _bfd_error_handler
695344c0 1403 /* xgettext:c-format */
871b3ab2
AM
1404 (_("%s: TLS reference in %pB "
1405 "mismatches non-TLS definition in %pB section %pA"),
c08bb8dd 1406 h->root.root.string, tbfd, ntbfd, ntsec);
7479dfd4
L
1407
1408 bfd_set_error (bfd_error_bad_value);
1409 return FALSE;
1410 }
1411
45d6a902
AM
1412 /* If the old symbol has non-default visibility, we ignore the new
1413 definition from a dynamic object. */
1414 if (newdyn
9c7a29a3 1415 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1416 && !bfd_is_und_section (sec))
1417 {
1418 *skip = TRUE;
1419 /* Make sure this symbol is dynamic. */
f5385ebf 1420 h->ref_dynamic = 1;
90c984fc 1421 hi->ref_dynamic = 1;
45d6a902
AM
1422 /* A protected symbol has external availability. Make sure it is
1423 recorded as dynamic.
1424
1425 FIXME: Should we check type and size for protected symbol? */
1426 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1427 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1428 else
1429 return TRUE;
1430 }
1431 else if (!newdyn
9c7a29a3 1432 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1433 && h->def_dynamic)
45d6a902
AM
1434 {
1435 /* If the new symbol with non-default visibility comes from a
1436 relocatable file and the old definition comes from a dynamic
1437 object, we remove the old definition. */
6c9b78e6 1438 if (hi->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1439 {
1440 /* Handle the case where the old dynamic definition is
1441 default versioned. We need to copy the symbol info from
1442 the symbol with default version to the normal one if it
1443 was referenced before. */
1444 if (h->ref_regular)
1445 {
6c9b78e6 1446 hi->root.type = h->root.type;
d2dee3b2 1447 h->root.type = bfd_link_hash_indirect;
6c9b78e6 1448 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
aed81c4e 1449
6c9b78e6 1450 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
aed81c4e 1451 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
d2dee3b2 1452 {
aed81c4e
MR
1453 /* If the new symbol is hidden or internal, completely undo
1454 any dynamic link state. */
1455 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1456 h->forced_local = 0;
1457 h->ref_dynamic = 0;
d2dee3b2
L
1458 }
1459 else
aed81c4e
MR
1460 h->ref_dynamic = 1;
1461
1462 h->def_dynamic = 0;
aed81c4e
MR
1463 /* FIXME: Should we check type and size for protected symbol? */
1464 h->size = 0;
1465 h->type = 0;
1466
6c9b78e6 1467 h = hi;
d2dee3b2
L
1468 }
1469 else
6c9b78e6 1470 h = hi;
d2dee3b2 1471 }
1de1a317 1472
f5eda473
AM
1473 /* If the old symbol was undefined before, then it will still be
1474 on the undefs list. If the new symbol is undefined or
1475 common, we can't make it bfd_link_hash_new here, because new
1476 undefined or common symbols will be added to the undefs list
1477 by _bfd_generic_link_add_one_symbol. Symbols may not be
1478 added twice to the undefs list. Also, if the new symbol is
1479 undefweak then we don't want to lose the strong undef. */
1480 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317 1481 {
1de1a317 1482 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1483 h->root.u.undef.abfd = abfd;
1484 }
1485 else
1486 {
1487 h->root.type = bfd_link_hash_new;
1488 h->root.u.undef.abfd = NULL;
1489 }
1490
f5eda473 1491 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
252b5132 1492 {
f5eda473
AM
1493 /* If the new symbol is hidden or internal, completely undo
1494 any dynamic link state. */
1495 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1496 h->forced_local = 0;
1497 h->ref_dynamic = 0;
45d6a902 1498 }
f5eda473
AM
1499 else
1500 h->ref_dynamic = 1;
1501 h->def_dynamic = 0;
45d6a902
AM
1502 /* FIXME: Should we check type and size for protected symbol? */
1503 h->size = 0;
1504 h->type = 0;
1505 return TRUE;
1506 }
14a793b2 1507
15b43f48
AM
1508 /* If a new weak symbol definition comes from a regular file and the
1509 old symbol comes from a dynamic library, we treat the new one as
1510 strong. Similarly, an old weak symbol definition from a regular
1511 file is treated as strong when the new symbol comes from a dynamic
1512 library. Further, an old weak symbol from a dynamic library is
1513 treated as strong if the new symbol is from a dynamic library.
1514 This reflects the way glibc's ld.so works.
1515
165f707a
AM
1516 Also allow a weak symbol to override a linker script symbol
1517 defined by an early pass over the script. This is done so the
1518 linker knows the symbol is defined in an object file, for the
1519 DEFINED script function.
1520
15b43f48
AM
1521 Do this before setting *type_change_ok or *size_change_ok so that
1522 we warn properly when dynamic library symbols are overridden. */
1523
165f707a 1524 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
0f8a2703 1525 newweak = FALSE;
15b43f48 1526 if (olddef && newdyn)
0f8a2703
AM
1527 oldweak = FALSE;
1528
d334575b 1529 /* Allow changes between different types of function symbol. */
0a36a439 1530 if (newfunc && oldfunc)
fcb93ecf
PB
1531 *type_change_ok = TRUE;
1532
79349b09
AM
1533 /* It's OK to change the type if either the existing symbol or the
1534 new symbol is weak. A type change is also OK if the old symbol
1535 is undefined and the new symbol is defined. */
252b5132 1536
79349b09
AM
1537 if (oldweak
1538 || newweak
1539 || (newdef
1540 && h->root.type == bfd_link_hash_undefined))
1541 *type_change_ok = TRUE;
1542
1543 /* It's OK to change the size if either the existing symbol or the
1544 new symbol is weak, or if the old symbol is undefined. */
1545
1546 if (*type_change_ok
1547 || h->root.type == bfd_link_hash_undefined)
1548 *size_change_ok = TRUE;
45d6a902 1549
45d6a902
AM
1550 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1551 symbol, respectively, appears to be a common symbol in a dynamic
1552 object. If a symbol appears in an uninitialized section, and is
1553 not weak, and is not a function, then it may be a common symbol
1554 which was resolved when the dynamic object was created. We want
1555 to treat such symbols specially, because they raise special
1556 considerations when setting the symbol size: if the symbol
1557 appears as a common symbol in a regular object, and the size in
1558 the regular object is larger, we must make sure that we use the
1559 larger size. This problematic case can always be avoided in C,
1560 but it must be handled correctly when using Fortran shared
1561 libraries.
1562
1563 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1564 likewise for OLDDYNCOMMON and OLDDEF.
1565
1566 Note that this test is just a heuristic, and that it is quite
1567 possible to have an uninitialized symbol in a shared object which
1568 is really a definition, rather than a common symbol. This could
1569 lead to some minor confusion when the symbol really is a common
1570 symbol in some regular object. However, I think it will be
1571 harmless. */
1572
1573 if (newdyn
1574 && newdef
79349b09 1575 && !newweak
45d6a902
AM
1576 && (sec->flags & SEC_ALLOC) != 0
1577 && (sec->flags & SEC_LOAD) == 0
1578 && sym->st_size > 0
0a36a439 1579 && !newfunc)
45d6a902
AM
1580 newdyncommon = TRUE;
1581 else
1582 newdyncommon = FALSE;
1583
1584 if (olddyn
1585 && olddef
1586 && h->root.type == bfd_link_hash_defined
f5385ebf 1587 && h->def_dynamic
45d6a902
AM
1588 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1589 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1590 && h->size > 0
0a36a439 1591 && !oldfunc)
45d6a902
AM
1592 olddyncommon = TRUE;
1593 else
1594 olddyncommon = FALSE;
1595
a4d8e49b
L
1596 /* We now know everything about the old and new symbols. We ask the
1597 backend to check if we can merge them. */
5d13b3b3
AM
1598 if (bed->merge_symbol != NULL)
1599 {
1600 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1601 return FALSE;
1602 sec = *psec;
1603 }
a4d8e49b 1604
a83ef4d1
L
1605 /* There are multiple definitions of a normal symbol. Skip the
1606 default symbol as well as definition from an IR object. */
93f4de39 1607 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
a83ef4d1
L
1608 && !default_sym && h->def_regular
1609 && !(oldbfd != NULL
1610 && (oldbfd->flags & BFD_PLUGIN) != 0
1611 && (abfd->flags & BFD_PLUGIN) == 0))
93f4de39
RL
1612 {
1613 /* Handle a multiple definition. */
1614 (*info->callbacks->multiple_definition) (info, &h->root,
1615 abfd, sec, *pvalue);
1616 *skip = TRUE;
1617 return TRUE;
1618 }
1619
45d6a902
AM
1620 /* If both the old and the new symbols look like common symbols in a
1621 dynamic object, set the size of the symbol to the larger of the
1622 two. */
1623
1624 if (olddyncommon
1625 && newdyncommon
1626 && sym->st_size != h->size)
1627 {
1628 /* Since we think we have two common symbols, issue a multiple
1629 common warning if desired. Note that we only warn if the
1630 size is different. If the size is the same, we simply let
1631 the old symbol override the new one as normally happens with
1632 symbols defined in dynamic objects. */
1633
1a72702b
AM
1634 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1635 bfd_link_hash_common, sym->st_size);
45d6a902
AM
1636 if (sym->st_size > h->size)
1637 h->size = sym->st_size;
252b5132 1638
45d6a902 1639 *size_change_ok = TRUE;
252b5132
RH
1640 }
1641
45d6a902
AM
1642 /* If we are looking at a dynamic object, and we have found a
1643 definition, we need to see if the symbol was already defined by
1644 some other object. If so, we want to use the existing
1645 definition, and we do not want to report a multiple symbol
1646 definition error; we do this by clobbering *PSEC to be
1647 bfd_und_section_ptr.
1648
1649 We treat a common symbol as a definition if the symbol in the
1650 shared library is a function, since common symbols always
1651 represent variables; this can cause confusion in principle, but
1652 any such confusion would seem to indicate an erroneous program or
1653 shared library. We also permit a common symbol in a regular
8170f769 1654 object to override a weak symbol in a shared object. */
45d6a902
AM
1655
1656 if (newdyn
1657 && newdef
77cfaee6 1658 && (olddef
45d6a902 1659 || (h->root.type == bfd_link_hash_common
8170f769 1660 && (newweak || newfunc))))
45d6a902 1661 {
7ba11550 1662 *override = abfd;
45d6a902
AM
1663 newdef = FALSE;
1664 newdyncommon = FALSE;
252b5132 1665
45d6a902
AM
1666 *psec = sec = bfd_und_section_ptr;
1667 *size_change_ok = TRUE;
252b5132 1668
45d6a902
AM
1669 /* If we get here when the old symbol is a common symbol, then
1670 we are explicitly letting it override a weak symbol or
1671 function in a dynamic object, and we don't want to warn about
1672 a type change. If the old symbol is a defined symbol, a type
1673 change warning may still be appropriate. */
252b5132 1674
45d6a902
AM
1675 if (h->root.type == bfd_link_hash_common)
1676 *type_change_ok = TRUE;
1677 }
1678
1679 /* Handle the special case of an old common symbol merging with a
1680 new symbol which looks like a common symbol in a shared object.
1681 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1682 common symbol, and let _bfd_generic_link_add_one_symbol do the
1683 right thing. */
45d6a902
AM
1684
1685 if (newdyncommon
1686 && h->root.type == bfd_link_hash_common)
1687 {
7ba11550 1688 *override = oldbfd;
45d6a902
AM
1689 newdef = FALSE;
1690 newdyncommon = FALSE;
1691 *pvalue = sym->st_size;
a4d8e49b 1692 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1693 *size_change_ok = TRUE;
1694 }
1695
c5e2cead 1696 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1697 if (newdef && olddef && newweak)
54ac0771 1698 {
35ed3f94 1699 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1700 if (!(oldbfd != NULL
1701 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94 1702 && (abfd->flags & BFD_PLUGIN) == 0))
57fa7b8c
AM
1703 {
1704 newdef = FALSE;
1705 *skip = TRUE;
1706 }
54ac0771
L
1707
1708 /* Merge st_other. If the symbol already has a dynamic index,
1709 but visibility says it should not be visible, turn it into a
1710 local symbol. */
5160d0f3 1711 elf_merge_st_other (abfd, h, sym->st_other, sec, newdef, newdyn);
54ac0771
L
1712 if (h->dynindx != -1)
1713 switch (ELF_ST_VISIBILITY (h->other))
1714 {
1715 case STV_INTERNAL:
1716 case STV_HIDDEN:
1717 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1718 break;
1719 }
1720 }
c5e2cead 1721
45d6a902
AM
1722 /* If the old symbol is from a dynamic object, and the new symbol is
1723 a definition which is not from a dynamic object, then the new
1724 symbol overrides the old symbol. Symbols from regular files
1725 always take precedence over symbols from dynamic objects, even if
1726 they are defined after the dynamic object in the link.
1727
1728 As above, we again permit a common symbol in a regular object to
1729 override a definition in a shared object if the shared object
0f8a2703 1730 symbol is a function or is weak. */
45d6a902
AM
1731
1732 flip = NULL;
77cfaee6 1733 if (!newdyn
45d6a902
AM
1734 && (newdef
1735 || (bfd_is_com_section (sec)
0a36a439 1736 && (oldweak || oldfunc)))
45d6a902
AM
1737 && olddyn
1738 && olddef
f5385ebf 1739 && h->def_dynamic)
45d6a902
AM
1740 {
1741 /* Change the hash table entry to undefined, and let
1742 _bfd_generic_link_add_one_symbol do the right thing with the
1743 new definition. */
1744
1745 h->root.type = bfd_link_hash_undefined;
1746 h->root.u.undef.abfd = h->root.u.def.section->owner;
1747 *size_change_ok = TRUE;
1748
1749 olddef = FALSE;
1750 olddyncommon = FALSE;
1751
1752 /* We again permit a type change when a common symbol may be
1753 overriding a function. */
1754
1755 if (bfd_is_com_section (sec))
0a36a439
L
1756 {
1757 if (oldfunc)
1758 {
1759 /* If a common symbol overrides a function, make sure
1760 that it isn't defined dynamically nor has type
1761 function. */
1762 h->def_dynamic = 0;
1763 h->type = STT_NOTYPE;
1764 }
1765 *type_change_ok = TRUE;
1766 }
45d6a902 1767
6c9b78e6
AM
1768 if (hi->root.type == bfd_link_hash_indirect)
1769 flip = hi;
45d6a902
AM
1770 else
1771 /* This union may have been set to be non-NULL when this symbol
1772 was seen in a dynamic object. We must force the union to be
1773 NULL, so that it is correct for a regular symbol. */
1774 h->verinfo.vertree = NULL;
1775 }
1776
1777 /* Handle the special case of a new common symbol merging with an
1778 old symbol that looks like it might be a common symbol defined in
1779 a shared object. Note that we have already handled the case in
1780 which a new common symbol should simply override the definition
1781 in the shared library. */
1782
1783 if (! newdyn
1784 && bfd_is_com_section (sec)
1785 && olddyncommon)
1786 {
1787 /* It would be best if we could set the hash table entry to a
1788 common symbol, but we don't know what to use for the section
1789 or the alignment. */
1a72702b
AM
1790 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1791 bfd_link_hash_common, sym->st_size);
45d6a902 1792
4cc11e76 1793 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1794 larger, pretend that the new symbol has its size. */
1795
1796 if (h->size > *pvalue)
1797 *pvalue = h->size;
1798
af44c138
L
1799 /* We need to remember the alignment required by the symbol
1800 in the dynamic object. */
1801 BFD_ASSERT (pold_alignment);
1802 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1803
1804 olddef = FALSE;
1805 olddyncommon = FALSE;
1806
1807 h->root.type = bfd_link_hash_undefined;
1808 h->root.u.undef.abfd = h->root.u.def.section->owner;
1809
1810 *size_change_ok = TRUE;
1811 *type_change_ok = TRUE;
1812
6c9b78e6
AM
1813 if (hi->root.type == bfd_link_hash_indirect)
1814 flip = hi;
45d6a902
AM
1815 else
1816 h->verinfo.vertree = NULL;
1817 }
1818
1819 if (flip != NULL)
1820 {
1821 /* Handle the case where we had a versioned symbol in a dynamic
1822 library and now find a definition in a normal object. In this
1823 case, we make the versioned symbol point to the normal one. */
45d6a902 1824 flip->root.type = h->root.type;
00cbee0a 1825 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1826 h->root.type = bfd_link_hash_indirect;
1827 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1828 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1829 if (h->def_dynamic)
45d6a902 1830 {
f5385ebf
AM
1831 h->def_dynamic = 0;
1832 flip->ref_dynamic = 1;
45d6a902
AM
1833 }
1834 }
1835
45d6a902
AM
1836 return TRUE;
1837}
1838
1839/* This function is called to create an indirect symbol from the
1840 default for the symbol with the default version if needed. The
4f3fedcf 1841 symbol is described by H, NAME, SYM, SEC, and VALUE. We
0f8a2703 1842 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1843
28caa186 1844static bfd_boolean
268b6b39
AM
1845_bfd_elf_add_default_symbol (bfd *abfd,
1846 struct bfd_link_info *info,
1847 struct elf_link_hash_entry *h,
1848 const char *name,
1849 Elf_Internal_Sym *sym,
4f3fedcf
AM
1850 asection *sec,
1851 bfd_vma value,
1852 bfd **poldbfd,
9918bff7
AM
1853 bfd_boolean *dynsym,
1854 bfd **override)
45d6a902
AM
1855{
1856 bfd_boolean type_change_ok;
1857 bfd_boolean size_change_ok;
1858 bfd_boolean skip;
1859 char *shortname;
1860 struct elf_link_hash_entry *hi;
1861 struct bfd_link_hash_entry *bh;
9c5bfbb7 1862 const struct elf_backend_data *bed;
45d6a902
AM
1863 bfd_boolean collect;
1864 bfd_boolean dynamic;
9918bff7 1865 bfd *nondef_override;
45d6a902
AM
1866 char *p;
1867 size_t len, shortlen;
ffd65175 1868 asection *tmp_sec;
6e33951e 1869 bfd_boolean matched;
45d6a902 1870
422f1182
L
1871 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1872 return TRUE;
1873
45d6a902
AM
1874 /* If this symbol has a version, and it is the default version, we
1875 create an indirect symbol from the default name to the fully
1876 decorated name. This will cause external references which do not
1877 specify a version to be bound to this version of the symbol. */
1878 p = strchr (name, ELF_VER_CHR);
422f1182
L
1879 if (h->versioned == unknown)
1880 {
1881 if (p == NULL)
1882 {
1883 h->versioned = unversioned;
1884 return TRUE;
1885 }
1886 else
1887 {
1888 if (p[1] != ELF_VER_CHR)
1889 {
1890 h->versioned = versioned_hidden;
1891 return TRUE;
1892 }
1893 else
1894 h->versioned = versioned;
1895 }
1896 }
4373f8af
L
1897 else
1898 {
1899 /* PR ld/19073: We may see an unversioned definition after the
1900 default version. */
1901 if (p == NULL)
1902 return TRUE;
1903 }
45d6a902 1904
45d6a902
AM
1905 bed = get_elf_backend_data (abfd);
1906 collect = bed->collect;
1907 dynamic = (abfd->flags & DYNAMIC) != 0;
1908
1909 shortlen = p - name;
a50b1753 1910 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1911 if (shortname == NULL)
1912 return FALSE;
1913 memcpy (shortname, name, shortlen);
1914 shortname[shortlen] = '\0';
1915
1916 /* We are going to create a new symbol. Merge it with any existing
1917 symbol with this name. For the purposes of the merge, act as
1918 though we were defining the symbol we just defined, although we
1919 actually going to define an indirect symbol. */
1920 type_change_ok = FALSE;
1921 size_change_ok = FALSE;
6e33951e 1922 matched = TRUE;
ffd65175
AM
1923 tmp_sec = sec;
1924 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
9918bff7 1925 &hi, poldbfd, NULL, NULL, &skip, override,
6e33951e 1926 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1927 return FALSE;
1928
1929 if (skip)
1930 goto nondefault;
1931
5fa370e4 1932 if (hi->def_regular || ELF_COMMON_DEF_P (hi))
5b677558
AM
1933 {
1934 /* If the undecorated symbol will have a version added by a
1935 script different to H, then don't indirect to/from the
1936 undecorated symbol. This isn't ideal because we may not yet
1937 have seen symbol versions, if given by a script on the
1938 command line rather than via --version-script. */
1939 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1940 {
1941 bfd_boolean hide;
1942
1943 hi->verinfo.vertree
1944 = bfd_find_version_for_sym (info->version_info,
1945 hi->root.root.string, &hide);
1946 if (hi->verinfo.vertree != NULL && hide)
1947 {
1948 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
69551367 1949 *override = FALSE;
5b677558
AM
1950 goto nondefault;
1951 }
1952 }
1953 if (hi->verinfo.vertree != NULL
1954 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
69551367
AM
1955 {
1956 *override = FALSE;
1957 goto nondefault;
1958 }
5b677558
AM
1959 }
1960
9918bff7 1961 if (!*override)
45d6a902 1962 {
c6e8a9a8 1963 /* Add the default symbol if not performing a relocatable link. */
0e1862bb 1964 if (! bfd_link_relocatable (info))
c6e8a9a8
L
1965 {
1966 bh = &hi->root;
fbcc8baf 1967 if (bh->type == bfd_link_hash_defined
6cc71b82 1968 && bh->u.def.section->owner != NULL
fbcc8baf
L
1969 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1970 {
1971 /* Mark the previous definition from IR object as
1972 undefined so that the generic linker will override
1973 it. */
1974 bh->type = bfd_link_hash_undefined;
1975 bh->u.undef.abfd = bh->u.def.section->owner;
1976 }
c6e8a9a8
L
1977 if (! (_bfd_generic_link_add_one_symbol
1978 (info, abfd, shortname, BSF_INDIRECT,
1979 bfd_ind_section_ptr,
1980 0, name, FALSE, collect, &bh)))
1981 return FALSE;
1982 hi = (struct elf_link_hash_entry *) bh;
1983 }
45d6a902
AM
1984 }
1985 else
1986 {
1987 /* In this case the symbol named SHORTNAME is overriding the
1988 indirect symbol we want to add. We were planning on making
1989 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1990 is the name without a version. NAME is the fully versioned
1991 name, and it is the default version.
1992
1993 Overriding means that we already saw a definition for the
1994 symbol SHORTNAME in a regular object, and it is overriding
1995 the symbol defined in the dynamic object.
1996
1997 When this happens, we actually want to change NAME, the
1998 symbol we just added, to refer to SHORTNAME. This will cause
1999 references to NAME in the shared object to become references
2000 to SHORTNAME in the regular object. This is what we expect
2001 when we override a function in a shared object: that the
2002 references in the shared object will be mapped to the
2003 definition in the regular object. */
2004
2005 while (hi->root.type == bfd_link_hash_indirect
2006 || hi->root.type == bfd_link_hash_warning)
2007 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2008
2009 h->root.type = bfd_link_hash_indirect;
2010 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 2011 if (h->def_dynamic)
45d6a902 2012 {
f5385ebf
AM
2013 h->def_dynamic = 0;
2014 hi->ref_dynamic = 1;
2015 if (hi->ref_regular
2016 || hi->def_regular)
45d6a902 2017 {
c152c796 2018 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
2019 return FALSE;
2020 }
2021 }
2022
2023 /* Now set HI to H, so that the following code will set the
2024 other fields correctly. */
2025 hi = h;
2026 }
2027
fab4a87f
L
2028 /* Check if HI is a warning symbol. */
2029 if (hi->root.type == bfd_link_hash_warning)
2030 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2031
45d6a902
AM
2032 /* If there is a duplicate definition somewhere, then HI may not
2033 point to an indirect symbol. We will have reported an error to
2034 the user in that case. */
2035
2036 if (hi->root.type == bfd_link_hash_indirect)
2037 {
2038 struct elf_link_hash_entry *ht;
2039
45d6a902 2040 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 2041 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902 2042
5160d0f3
AM
2043 /* If we first saw a reference to SHORTNAME with non-default
2044 visibility, merge that visibility to the @@VER symbol. */
2045 elf_merge_st_other (abfd, ht, hi->other, sec, TRUE, dynamic);
2046
68c88cd4
AM
2047 /* A reference to the SHORTNAME symbol from a dynamic library
2048 will be satisfied by the versioned symbol at runtime. In
2049 effect, we have a reference to the versioned symbol. */
2050 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2051 hi->dynamic_def |= ht->dynamic_def;
2052
45d6a902
AM
2053 /* See if the new flags lead us to realize that the symbol must
2054 be dynamic. */
2055 if (! *dynsym)
2056 {
2057 if (! dynamic)
2058 {
0e1862bb 2059 if (! bfd_link_executable (info)
90c984fc 2060 || hi->def_dynamic
f5385ebf 2061 || hi->ref_dynamic)
45d6a902
AM
2062 *dynsym = TRUE;
2063 }
2064 else
2065 {
f5385ebf 2066 if (hi->ref_regular)
45d6a902
AM
2067 *dynsym = TRUE;
2068 }
2069 }
2070 }
2071
2072 /* We also need to define an indirection from the nondefault version
2073 of the symbol. */
2074
dc1e8a47 2075 nondefault:
45d6a902 2076 len = strlen (name);
a50b1753 2077 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
2078 if (shortname == NULL)
2079 return FALSE;
2080 memcpy (shortname, name, shortlen);
2081 memcpy (shortname + shortlen, p + 1, len - shortlen);
2082
2083 /* Once again, merge with any existing symbol. */
2084 type_change_ok = FALSE;
2085 size_change_ok = FALSE;
ffd65175
AM
2086 tmp_sec = sec;
2087 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
9918bff7 2088 &hi, poldbfd, NULL, NULL, &skip, &nondef_override,
6e33951e 2089 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
2090 return FALSE;
2091
2092 if (skip)
726d7d1e
AM
2093 {
2094 if (!dynamic
2095 && h->root.type == bfd_link_hash_defweak
2096 && hi->root.type == bfd_link_hash_defined)
2097 {
2098 /* We are handling a weak sym@@ver and attempting to define
2099 a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2100 new weak sym@ver because there is already a strong sym@ver.
2101 However, sym@ver and sym@@ver are really the same symbol.
2102 The existing strong sym@ver ought to override sym@@ver. */
2103 h->root.type = bfd_link_hash_defined;
2104 h->root.u.def.section = hi->root.u.def.section;
2105 h->root.u.def.value = hi->root.u.def.value;
2106 hi->root.type = bfd_link_hash_indirect;
2107 hi->root.u.i.link = &h->root;
2108 }
2109 else
2110 return TRUE;
2111 }
9918bff7 2112 else if (nondef_override)
45d6a902
AM
2113 {
2114 /* Here SHORTNAME is a versioned name, so we don't expect to see
2115 the type of override we do in the case above unless it is
4cc11e76 2116 overridden by a versioned definition. */
45d6a902
AM
2117 if (hi->root.type != bfd_link_hash_defined
2118 && hi->root.type != bfd_link_hash_defweak)
4eca0228 2119 _bfd_error_handler
695344c0 2120 /* xgettext:c-format */
871b3ab2 2121 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
d003868e 2122 abfd, shortname);
726d7d1e 2123 return TRUE;
45d6a902
AM
2124 }
2125 else
2126 {
2127 bh = &hi->root;
2128 if (! (_bfd_generic_link_add_one_symbol
2129 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 2130 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
2131 return FALSE;
2132 hi = (struct elf_link_hash_entry *) bh;
726d7d1e 2133 }
45d6a902 2134
726d7d1e
AM
2135 /* If there is a duplicate definition somewhere, then HI may not
2136 point to an indirect symbol. We will have reported an error
2137 to the user in that case. */
2138 if (hi->root.type == bfd_link_hash_indirect)
2139 {
2140 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2141 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2142 hi->dynamic_def |= h->dynamic_def;
45d6a902 2143
726d7d1e
AM
2144 /* If we first saw a reference to @VER symbol with
2145 non-default visibility, merge that visibility to the
2146 @@VER symbol. */
2147 elf_merge_st_other (abfd, h, hi->other, sec, TRUE, dynamic);
2148
2149 /* See if the new flags lead us to realize that the symbol
2150 must be dynamic. */
2151 if (! *dynsym)
45d6a902 2152 {
726d7d1e 2153 if (! dynamic)
45d6a902 2154 {
726d7d1e
AM
2155 if (! bfd_link_executable (info)
2156 || hi->ref_dynamic)
2157 *dynsym = TRUE;
2158 }
2159 else
2160 {
2161 if (hi->ref_regular)
2162 *dynsym = TRUE;
45d6a902
AM
2163 }
2164 }
2165 }
2166
2167 return TRUE;
2168}
2169\f
2170/* This routine is used to export all defined symbols into the dynamic
2171 symbol table. It is called via elf_link_hash_traverse. */
2172
28caa186 2173static bfd_boolean
268b6b39 2174_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2175{
a50b1753 2176 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
2177
2178 /* Ignore indirect symbols. These are added by the versioning code. */
2179 if (h->root.type == bfd_link_hash_indirect)
2180 return TRUE;
2181
7686d77d
AM
2182 /* Ignore this if we won't export it. */
2183 if (!eif->info->export_dynamic && !h->dynamic)
2184 return TRUE;
45d6a902
AM
2185
2186 if (h->dynindx == -1
fd91d419
L
2187 && (h->def_regular || h->ref_regular)
2188 && ! bfd_hide_sym_by_version (eif->info->version_info,
2189 h->root.root.string))
45d6a902 2190 {
fd91d419 2191 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 2192 {
fd91d419
L
2193 eif->failed = TRUE;
2194 return FALSE;
45d6a902
AM
2195 }
2196 }
2197
2198 return TRUE;
2199}
2200\f
2201/* Look through the symbols which are defined in other shared
2202 libraries and referenced here. Update the list of version
2203 dependencies. This will be put into the .gnu.version_r section.
2204 This function is called via elf_link_hash_traverse. */
2205
28caa186 2206static bfd_boolean
268b6b39
AM
2207_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2208 void *data)
45d6a902 2209{
a50b1753 2210 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
2211 Elf_Internal_Verneed *t;
2212 Elf_Internal_Vernaux *a;
986f0783 2213 size_t amt;
45d6a902 2214
45d6a902
AM
2215 /* We only care about symbols defined in shared objects with version
2216 information. */
f5385ebf
AM
2217 if (!h->def_dynamic
2218 || h->def_regular
45d6a902 2219 || h->dynindx == -1
7b20f099
AM
2220 || h->verinfo.verdef == NULL
2221 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2222 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
45d6a902
AM
2223 return TRUE;
2224
2225 /* See if we already know about this version. */
28caa186
AM
2226 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2227 t != NULL;
2228 t = t->vn_nextref)
45d6a902
AM
2229 {
2230 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2231 continue;
2232
2233 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2234 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2235 return TRUE;
2236
2237 break;
2238 }
2239
2240 /* This is a new version. Add it to tree we are building. */
2241
2242 if (t == NULL)
2243 {
2244 amt = sizeof *t;
a50b1753 2245 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
2246 if (t == NULL)
2247 {
2248 rinfo->failed = TRUE;
2249 return FALSE;
2250 }
2251
2252 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
2253 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2254 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
2255 }
2256
2257 amt = sizeof *a;
a50b1753 2258 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
2259 if (a == NULL)
2260 {
2261 rinfo->failed = TRUE;
2262 return FALSE;
2263 }
45d6a902
AM
2264
2265 /* Note that we are copying a string pointer here, and testing it
2266 above. If bfd_elf_string_from_elf_section is ever changed to
2267 discard the string data when low in memory, this will have to be
2268 fixed. */
2269 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2270
2271 a->vna_flags = h->verinfo.verdef->vd_flags;
2272 a->vna_nextptr = t->vn_auxptr;
2273
2274 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2275 ++rinfo->vers;
2276
2277 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2278
2279 t->vn_auxptr = a;
2280
2281 return TRUE;
2282}
2283
099bb8fb
L
2284/* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2285 hidden. Set *T_P to NULL if there is no match. */
2286
2287static bfd_boolean
2288_bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2289 struct elf_link_hash_entry *h,
2290 const char *version_p,
2291 struct bfd_elf_version_tree **t_p,
2292 bfd_boolean *hide)
2293{
2294 struct bfd_elf_version_tree *t;
2295
2296 /* Look for the version. If we find it, it is no longer weak. */
2297 for (t = info->version_info; t != NULL; t = t->next)
2298 {
2299 if (strcmp (t->name, version_p) == 0)
2300 {
2301 size_t len;
2302 char *alc;
2303 struct bfd_elf_version_expr *d;
2304
2305 len = version_p - h->root.root.string;
2306 alc = (char *) bfd_malloc (len);
2307 if (alc == NULL)
2308 return FALSE;
2309 memcpy (alc, h->root.root.string, len - 1);
2310 alc[len - 1] = '\0';
2311 if (alc[len - 2] == ELF_VER_CHR)
2312 alc[len - 2] = '\0';
2313
2314 h->verinfo.vertree = t;
2315 t->used = TRUE;
2316 d = NULL;
2317
2318 if (t->globals.list != NULL)
2319 d = (*t->match) (&t->globals, NULL, alc);
2320
2321 /* See if there is anything to force this symbol to
2322 local scope. */
2323 if (d == NULL && t->locals.list != NULL)
2324 {
2325 d = (*t->match) (&t->locals, NULL, alc);
2326 if (d != NULL
2327 && h->dynindx != -1
2328 && ! info->export_dynamic)
2329 *hide = TRUE;
2330 }
2331
2332 free (alc);
2333 break;
2334 }
2335 }
2336
2337 *t_p = t;
2338
2339 return TRUE;
2340}
2341
2342/* Return TRUE if the symbol H is hidden by version script. */
2343
2344bfd_boolean
2345_bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2346 struct elf_link_hash_entry *h)
2347{
2348 const char *p;
2349 bfd_boolean hide = FALSE;
2350 const struct elf_backend_data *bed
2351 = get_elf_backend_data (info->output_bfd);
2352
2353 /* Version script only hides symbols defined in regular objects. */
2354 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2355 return TRUE;
2356
2357 p = strchr (h->root.root.string, ELF_VER_CHR);
2358 if (p != NULL && h->verinfo.vertree == NULL)
2359 {
2360 struct bfd_elf_version_tree *t;
2361
2362 ++p;
2363 if (*p == ELF_VER_CHR)
2364 ++p;
2365
2366 if (*p != '\0'
2367 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2368 && hide)
2369 {
2370 if (hide)
2371 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2372 return TRUE;
2373 }
2374 }
2375
2376 /* If we don't have a version for this symbol, see if we can find
2377 something. */
2378 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2379 {
2380 h->verinfo.vertree
2381 = bfd_find_version_for_sym (info->version_info,
2382 h->root.root.string, &hide);
2383 if (h->verinfo.vertree != NULL && hide)
2384 {
2385 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2386 return TRUE;
2387 }
2388 }
2389
2390 return FALSE;
2391}
2392
45d6a902
AM
2393/* Figure out appropriate versions for all the symbols. We may not
2394 have the version number script until we have read all of the input
2395 files, so until that point we don't know which symbols should be
2396 local. This function is called via elf_link_hash_traverse. */
2397
28caa186 2398static bfd_boolean
268b6b39 2399_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 2400{
28caa186 2401 struct elf_info_failed *sinfo;
45d6a902 2402 struct bfd_link_info *info;
9c5bfbb7 2403 const struct elf_backend_data *bed;
45d6a902
AM
2404 struct elf_info_failed eif;
2405 char *p;
099bb8fb 2406 bfd_boolean hide;
45d6a902 2407
a50b1753 2408 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
2409 info = sinfo->info;
2410
45d6a902
AM
2411 /* Fix the symbol flags. */
2412 eif.failed = FALSE;
2413 eif.info = info;
2414 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2415 {
2416 if (eif.failed)
2417 sinfo->failed = TRUE;
2418 return FALSE;
2419 }
2420
0a640d71
L
2421 bed = get_elf_backend_data (info->output_bfd);
2422
45d6a902
AM
2423 /* We only need version numbers for symbols defined in regular
2424 objects. */
5fa370e4 2425 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
0a640d71
L
2426 {
2427 /* Hide symbols defined in discarded input sections. */
2428 if ((h->root.type == bfd_link_hash_defined
2429 || h->root.type == bfd_link_hash_defweak)
2430 && discarded_section (h->root.u.def.section))
2431 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2432 return TRUE;
2433 }
45d6a902 2434
099bb8fb 2435 hide = FALSE;
45d6a902
AM
2436 p = strchr (h->root.root.string, ELF_VER_CHR);
2437 if (p != NULL && h->verinfo.vertree == NULL)
2438 {
2439 struct bfd_elf_version_tree *t;
45d6a902 2440
45d6a902
AM
2441 ++p;
2442 if (*p == ELF_VER_CHR)
6e33951e 2443 ++p;
45d6a902
AM
2444
2445 /* If there is no version string, we can just return out. */
2446 if (*p == '\0')
6e33951e 2447 return TRUE;
45d6a902 2448
099bb8fb 2449 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
45d6a902 2450 {
099bb8fb
L
2451 sinfo->failed = TRUE;
2452 return FALSE;
45d6a902
AM
2453 }
2454
099bb8fb
L
2455 if (hide)
2456 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2457
45d6a902
AM
2458 /* If we are building an application, we need to create a
2459 version node for this version. */
0e1862bb 2460 if (t == NULL && bfd_link_executable (info))
45d6a902
AM
2461 {
2462 struct bfd_elf_version_tree **pp;
2463 int version_index;
2464
2465 /* If we aren't going to export this symbol, we don't need
2466 to worry about it. */
2467 if (h->dynindx == -1)
2468 return TRUE;
2469
ef53be89
AM
2470 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2471 sizeof *t);
45d6a902
AM
2472 if (t == NULL)
2473 {
2474 sinfo->failed = TRUE;
2475 return FALSE;
2476 }
2477
45d6a902 2478 t->name = p;
45d6a902
AM
2479 t->name_indx = (unsigned int) -1;
2480 t->used = TRUE;
2481
2482 version_index = 1;
2483 /* Don't count anonymous version tag. */
fd91d419
L
2484 if (sinfo->info->version_info != NULL
2485 && sinfo->info->version_info->vernum == 0)
45d6a902 2486 version_index = 0;
fd91d419
L
2487 for (pp = &sinfo->info->version_info;
2488 *pp != NULL;
2489 pp = &(*pp)->next)
45d6a902
AM
2490 ++version_index;
2491 t->vernum = version_index;
2492
2493 *pp = t;
2494
2495 h->verinfo.vertree = t;
2496 }
2497 else if (t == NULL)
2498 {
2499 /* We could not find the version for a symbol when
2500 generating a shared archive. Return an error. */
4eca0228 2501 _bfd_error_handler
695344c0 2502 /* xgettext:c-format */
871b3ab2 2503 (_("%pB: version node not found for symbol %s"),
28caa186 2504 info->output_bfd, h->root.root.string);
45d6a902
AM
2505 bfd_set_error (bfd_error_bad_value);
2506 sinfo->failed = TRUE;
2507 return FALSE;
2508 }
45d6a902
AM
2509 }
2510
2511 /* If we don't have a version for this symbol, see if we can find
2512 something. */
099bb8fb
L
2513 if (!hide
2514 && h->verinfo.vertree == NULL
2515 && sinfo->info->version_info != NULL)
45d6a902 2516 {
fd91d419
L
2517 h->verinfo.vertree
2518 = bfd_find_version_for_sym (sinfo->info->version_info,
2519 h->root.root.string, &hide);
1e8fa21e
AM
2520 if (h->verinfo.vertree != NULL && hide)
2521 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2522 }
2523
2524 return TRUE;
2525}
2526\f
45d6a902
AM
2527/* Read and swap the relocs from the section indicated by SHDR. This
2528 may be either a REL or a RELA section. The relocations are
2529 translated into RELA relocations and stored in INTERNAL_RELOCS,
2530 which should have already been allocated to contain enough space.
2531 The EXTERNAL_RELOCS are a buffer where the external form of the
2532 relocations should be stored.
2533
2534 Returns FALSE if something goes wrong. */
2535
2536static bfd_boolean
268b6b39 2537elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2538 asection *sec,
268b6b39
AM
2539 Elf_Internal_Shdr *shdr,
2540 void *external_relocs,
2541 Elf_Internal_Rela *internal_relocs)
45d6a902 2542{
9c5bfbb7 2543 const struct elf_backend_data *bed;
268b6b39 2544 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2545 const bfd_byte *erela;
2546 const bfd_byte *erelaend;
2547 Elf_Internal_Rela *irela;
243ef1e0
L
2548 Elf_Internal_Shdr *symtab_hdr;
2549 size_t nsyms;
45d6a902 2550
45d6a902
AM
2551 /* Position ourselves at the start of the section. */
2552 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2553 return FALSE;
2554
2555 /* Read the relocations. */
2556 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2557 return FALSE;
2558
243ef1e0 2559 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2560 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2561
45d6a902
AM
2562 bed = get_elf_backend_data (abfd);
2563
2564 /* Convert the external relocations to the internal format. */
2565 if (shdr->sh_entsize == bed->s->sizeof_rel)
2566 swap_in = bed->s->swap_reloc_in;
2567 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2568 swap_in = bed->s->swap_reloca_in;
2569 else
2570 {
2571 bfd_set_error (bfd_error_wrong_format);
2572 return FALSE;
2573 }
2574
a50b1753 2575 erela = (const bfd_byte *) external_relocs;
f55b1e32
AM
2576 /* Setting erelaend like this and comparing with <= handles case of
2577 a fuzzed object with sh_size not a multiple of sh_entsize. */
2578 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
45d6a902 2579 irela = internal_relocs;
f55b1e32 2580 while (erela <= erelaend)
45d6a902 2581 {
243ef1e0
L
2582 bfd_vma r_symndx;
2583
45d6a902 2584 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2585 r_symndx = ELF32_R_SYM (irela->r_info);
2586 if (bed->s->arch_size == 64)
2587 r_symndx >>= 24;
ce98a316
NC
2588 if (nsyms > 0)
2589 {
2590 if ((size_t) r_symndx >= nsyms)
2591 {
4eca0228 2592 _bfd_error_handler
695344c0 2593 /* xgettext:c-format */
2dcf00ce
AM
2594 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2595 " for offset %#" PRIx64 " in section `%pA'"),
2596 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2597 (uint64_t) irela->r_offset, sec);
ce98a316
NC
2598 bfd_set_error (bfd_error_bad_value);
2599 return FALSE;
2600 }
2601 }
cf35638d 2602 else if (r_symndx != STN_UNDEF)
243ef1e0 2603 {
4eca0228 2604 _bfd_error_handler
695344c0 2605 /* xgettext:c-format */
2dcf00ce
AM
2606 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2607 " for offset %#" PRIx64 " in section `%pA'"
ce98a316 2608 " when the object file has no symbol table"),
2dcf00ce
AM
2609 abfd, (uint64_t) r_symndx,
2610 (uint64_t) irela->r_offset, sec);
243ef1e0
L
2611 bfd_set_error (bfd_error_bad_value);
2612 return FALSE;
2613 }
45d6a902
AM
2614 irela += bed->s->int_rels_per_ext_rel;
2615 erela += shdr->sh_entsize;
2616 }
2617
2618 return TRUE;
2619}
2620
2621/* Read and swap the relocs for a section O. They may have been
2622 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2623 not NULL, they are used as buffers to read into. They are known to
2624 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2625 the return value is allocated using either malloc or bfd_alloc,
2626 according to the KEEP_MEMORY argument. If O has two relocation
2627 sections (both REL and RELA relocations), then the REL_HDR
2628 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2629 RELA_HDR relocations. */
45d6a902
AM
2630
2631Elf_Internal_Rela *
268b6b39
AM
2632_bfd_elf_link_read_relocs (bfd *abfd,
2633 asection *o,
2634 void *external_relocs,
2635 Elf_Internal_Rela *internal_relocs,
2636 bfd_boolean keep_memory)
45d6a902 2637{
268b6b39 2638 void *alloc1 = NULL;
45d6a902 2639 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2640 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2641 struct bfd_elf_section_data *esdo = elf_section_data (o);
2642 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2643
d4730f92
BS
2644 if (esdo->relocs != NULL)
2645 return esdo->relocs;
45d6a902
AM
2646
2647 if (o->reloc_count == 0)
2648 return NULL;
2649
45d6a902
AM
2650 if (internal_relocs == NULL)
2651 {
2652 bfd_size_type size;
2653
056bafd4 2654 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
45d6a902 2655 if (keep_memory)
a50b1753 2656 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2657 else
a50b1753 2658 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2659 if (internal_relocs == NULL)
2660 goto error_return;
2661 }
2662
2663 if (external_relocs == NULL)
2664 {
d4730f92
BS
2665 bfd_size_type size = 0;
2666
2667 if (esdo->rel.hdr)
2668 size += esdo->rel.hdr->sh_size;
2669 if (esdo->rela.hdr)
2670 size += esdo->rela.hdr->sh_size;
45d6a902 2671
268b6b39 2672 alloc1 = bfd_malloc (size);
45d6a902
AM
2673 if (alloc1 == NULL)
2674 goto error_return;
2675 external_relocs = alloc1;
2676 }
2677
d4730f92
BS
2678 internal_rela_relocs = internal_relocs;
2679 if (esdo->rel.hdr)
2680 {
2681 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2682 external_relocs,
2683 internal_relocs))
2684 goto error_return;
2685 external_relocs = (((bfd_byte *) external_relocs)
2686 + esdo->rel.hdr->sh_size);
2687 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2688 * bed->s->int_rels_per_ext_rel);
2689 }
2690
2691 if (esdo->rela.hdr
2692 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2693 external_relocs,
2694 internal_rela_relocs)))
45d6a902
AM
2695 goto error_return;
2696
2697 /* Cache the results for next time, if we can. */
2698 if (keep_memory)
d4730f92 2699 esdo->relocs = internal_relocs;
45d6a902 2700
c9594989 2701 free (alloc1);
45d6a902
AM
2702
2703 /* Don't free alloc2, since if it was allocated we are passing it
2704 back (under the name of internal_relocs). */
2705
2706 return internal_relocs;
2707
2708 error_return:
c9594989 2709 free (alloc1);
45d6a902 2710 if (alloc2 != NULL)
4dd07732
AM
2711 {
2712 if (keep_memory)
2713 bfd_release (abfd, alloc2);
2714 else
2715 free (alloc2);
2716 }
45d6a902
AM
2717 return NULL;
2718}
2719
2720/* Compute the size of, and allocate space for, REL_HDR which is the
2721 section header for a section containing relocations for O. */
2722
28caa186 2723static bfd_boolean
9eaff861
AO
2724_bfd_elf_link_size_reloc_section (bfd *abfd,
2725 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2726{
9eaff861 2727 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2728
2729 /* That allows us to calculate the size of the section. */
9eaff861 2730 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2731
2732 /* The contents field must last into write_object_contents, so we
2733 allocate it with bfd_alloc rather than malloc. Also since we
2734 cannot be sure that the contents will actually be filled in,
2735 we zero the allocated space. */
a50b1753 2736 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2737 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2738 return FALSE;
2739
d4730f92 2740 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2741 {
2742 struct elf_link_hash_entry **p;
2743
ca4be51c
AM
2744 p = ((struct elf_link_hash_entry **)
2745 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902
AM
2746 if (p == NULL)
2747 return FALSE;
2748
d4730f92 2749 reldata->hashes = p;
45d6a902
AM
2750 }
2751
2752 return TRUE;
2753}
2754
2755/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2756 originated from the section given by INPUT_REL_HDR) to the
2757 OUTPUT_BFD. */
2758
2759bfd_boolean
268b6b39
AM
2760_bfd_elf_link_output_relocs (bfd *output_bfd,
2761 asection *input_section,
2762 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2763 Elf_Internal_Rela *internal_relocs,
2764 struct elf_link_hash_entry **rel_hash
2765 ATTRIBUTE_UNUSED)
45d6a902
AM
2766{
2767 Elf_Internal_Rela *irela;
2768 Elf_Internal_Rela *irelaend;
2769 bfd_byte *erel;
d4730f92 2770 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2771 asection *output_section;
9c5bfbb7 2772 const struct elf_backend_data *bed;
268b6b39 2773 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2774 struct bfd_elf_section_data *esdo;
45d6a902
AM
2775
2776 output_section = input_section->output_section;
45d6a902 2777
d4730f92
BS
2778 bed = get_elf_backend_data (output_bfd);
2779 esdo = elf_section_data (output_section);
2780 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2781 {
d4730f92
BS
2782 output_reldata = &esdo->rel;
2783 swap_out = bed->s->swap_reloc_out;
45d6a902 2784 }
d4730f92
BS
2785 else if (esdo->rela.hdr
2786 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2787 {
d4730f92
BS
2788 output_reldata = &esdo->rela;
2789 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2790 }
2791 else
2792 {
4eca0228 2793 _bfd_error_handler
695344c0 2794 /* xgettext:c-format */
871b3ab2 2795 (_("%pB: relocation size mismatch in %pB section %pA"),
d003868e 2796 output_bfd, input_section->owner, input_section);
297d8443 2797 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2798 return FALSE;
2799 }
2800
d4730f92
BS
2801 erel = output_reldata->hdr->contents;
2802 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2803 irela = internal_relocs;
2804 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2805 * bed->s->int_rels_per_ext_rel);
2806 while (irela < irelaend)
2807 {
2808 (*swap_out) (output_bfd, irela, erel);
2809 irela += bed->s->int_rels_per_ext_rel;
2810 erel += input_rel_hdr->sh_entsize;
2811 }
2812
2813 /* Bump the counter, so that we know where to add the next set of
2814 relocations. */
d4730f92 2815 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2816
2817 return TRUE;
2818}
2819\f
508c3946
L
2820/* Make weak undefined symbols in PIE dynamic. */
2821
2822bfd_boolean
2823_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2824 struct elf_link_hash_entry *h)
2825{
0e1862bb 2826 if (bfd_link_pie (info)
508c3946
L
2827 && h->dynindx == -1
2828 && h->root.type == bfd_link_hash_undefweak)
2829 return bfd_elf_link_record_dynamic_symbol (info, h);
2830
2831 return TRUE;
2832}
2833
45d6a902
AM
2834/* Fix up the flags for a symbol. This handles various cases which
2835 can only be fixed after all the input files are seen. This is
2836 currently called by both adjust_dynamic_symbol and
2837 assign_sym_version, which is unnecessary but perhaps more robust in
2838 the face of future changes. */
2839
28caa186 2840static bfd_boolean
268b6b39
AM
2841_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2842 struct elf_info_failed *eif)
45d6a902 2843{
33774f08 2844 const struct elf_backend_data *bed;
508c3946 2845
45d6a902
AM
2846 /* If this symbol was mentioned in a non-ELF file, try to set
2847 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2848 permit a non-ELF file to correctly refer to a symbol defined in
2849 an ELF dynamic object. */
f5385ebf 2850 if (h->non_elf)
45d6a902
AM
2851 {
2852 while (h->root.type == bfd_link_hash_indirect)
2853 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2854
2855 if (h->root.type != bfd_link_hash_defined
2856 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2857 {
2858 h->ref_regular = 1;
2859 h->ref_regular_nonweak = 1;
2860 }
45d6a902
AM
2861 else
2862 {
2863 if (h->root.u.def.section->owner != NULL
2864 && (bfd_get_flavour (h->root.u.def.section->owner)
2865 == bfd_target_elf_flavour))
f5385ebf
AM
2866 {
2867 h->ref_regular = 1;
2868 h->ref_regular_nonweak = 1;
2869 }
45d6a902 2870 else
f5385ebf 2871 h->def_regular = 1;
45d6a902
AM
2872 }
2873
2874 if (h->dynindx == -1
f5385ebf
AM
2875 && (h->def_dynamic
2876 || h->ref_dynamic))
45d6a902 2877 {
c152c796 2878 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2879 {
2880 eif->failed = TRUE;
2881 return FALSE;
2882 }
2883 }
2884 }
2885 else
2886 {
f5385ebf 2887 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2888 was first seen in a non-ELF file. Fortunately, if the symbol
2889 was first seen in an ELF file, we're probably OK unless the
2890 symbol was defined in a non-ELF file. Catch that case here.
2891 FIXME: We're still in trouble if the symbol was first seen in
2892 a dynamic object, and then later in a non-ELF regular object. */
2893 if ((h->root.type == bfd_link_hash_defined
2894 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2895 && !h->def_regular
45d6a902
AM
2896 && (h->root.u.def.section->owner != NULL
2897 ? (bfd_get_flavour (h->root.u.def.section->owner)
2898 != bfd_target_elf_flavour)
2899 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2900 && !h->def_dynamic)))
2901 h->def_regular = 1;
45d6a902
AM
2902 }
2903
508c3946 2904 /* Backend specific symbol fixup. */
33774f08
AM
2905 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2906 if (bed->elf_backend_fixup_symbol
2907 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2908 return FALSE;
508c3946 2909
45d6a902
AM
2910 /* If this is a final link, and the symbol was defined as a common
2911 symbol in a regular object file, and there was no definition in
2912 any dynamic object, then the linker will have allocated space for
f5385ebf 2913 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2914 flag will not have been set. */
2915 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2916 && !h->def_regular
2917 && h->ref_regular
2918 && !h->def_dynamic
96f29d96 2919 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2920 h->def_regular = 1;
45d6a902 2921
af0bfb9c
AM
2922 /* Symbols defined in discarded sections shouldn't be dynamic. */
2923 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
2924 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2925
4deb8f71
L
2926 /* If a weak undefined symbol has non-default visibility, we also
2927 hide it from the dynamic linker. */
af0bfb9c
AM
2928 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2929 && h->root.type == bfd_link_hash_undefweak)
4deb8f71
L
2930 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2931
2932 /* A hidden versioned symbol in executable should be forced local if
2933 it is is locally defined, not referenced by shared library and not
2934 exported. */
2935 else if (bfd_link_executable (eif->info)
2936 && h->versioned == versioned_hidden
2937 && !eif->info->export_dynamic
2938 && !h->dynamic
2939 && !h->ref_dynamic
2940 && h->def_regular)
2941 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2942
45d6a902
AM
2943 /* If -Bsymbolic was used (which means to bind references to global
2944 symbols to the definition within the shared object), and this
2945 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2946 need a PLT entry. Likewise, if the symbol has non-default
2947 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2948 will force it local. */
4deb8f71
L
2949 else if (h->needs_plt
2950 && bfd_link_pic (eif->info)
2951 && is_elf_hash_table (eif->info->hash)
2952 && (SYMBOLIC_BIND (eif->info, h)
2953 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2954 && h->def_regular)
45d6a902 2955 {
45d6a902
AM
2956 bfd_boolean force_local;
2957
45d6a902
AM
2958 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2959 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2960 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2961 }
2962
45d6a902
AM
2963 /* If this is a weak defined symbol in a dynamic object, and we know
2964 the real definition in the dynamic object, copy interesting flags
2965 over to the real definition. */
60d67dc8 2966 if (h->is_weakalias)
45d6a902 2967 {
60d67dc8
AM
2968 struct elf_link_hash_entry *def = weakdef (h);
2969
45d6a902
AM
2970 /* If the real definition is defined by a regular object file,
2971 don't do anything special. See the longer description in
5b9d7a9a
AM
2972 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
2973 bfd_link_hash_defined as it was when put on the alias list
2974 then it must have originally been a versioned symbol (for
2975 which a non-versioned indirect symbol is created) and later
2976 a definition for the non-versioned symbol is found. In that
2977 case the indirection is flipped with the versioned symbol
2978 becoming an indirect pointing at the non-versioned symbol.
2979 Thus, not an alias any more. */
2980 if (def->def_regular
2981 || def->root.type != bfd_link_hash_defined)
60d67dc8
AM
2982 {
2983 h = def;
2984 while ((h = h->u.alias) != def)
2985 h->is_weakalias = 0;
2986 }
45d6a902 2987 else
a26587ba 2988 {
4e6b54a6
AM
2989 while (h->root.type == bfd_link_hash_indirect)
2990 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4e6b54a6
AM
2991 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2992 || h->root.type == bfd_link_hash_defweak);
60d67dc8 2993 BFD_ASSERT (def->def_dynamic);
60d67dc8 2994 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
a26587ba 2995 }
45d6a902
AM
2996 }
2997
2998 return TRUE;
2999}
3000
3001/* Make the backend pick a good value for a dynamic symbol. This is
3002 called via elf_link_hash_traverse, and also calls itself
3003 recursively. */
3004
28caa186 3005static bfd_boolean
268b6b39 3006_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 3007{
a50b1753 3008 struct elf_info_failed *eif = (struct elf_info_failed *) data;
559192d8 3009 struct elf_link_hash_table *htab;
9c5bfbb7 3010 const struct elf_backend_data *bed;
45d6a902 3011
0eddce27 3012 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
3013 return FALSE;
3014
45d6a902
AM
3015 /* Ignore indirect symbols. These are added by the versioning code. */
3016 if (h->root.type == bfd_link_hash_indirect)
3017 return TRUE;
3018
3019 /* Fix the symbol flags. */
3020 if (! _bfd_elf_fix_symbol_flags (h, eif))
3021 return FALSE;
3022
559192d8
AM
3023 htab = elf_hash_table (eif->info);
3024 bed = get_elf_backend_data (htab->dynobj);
3025
954b63d4
AM
3026 if (h->root.type == bfd_link_hash_undefweak)
3027 {
3028 if (eif->info->dynamic_undefined_weak == 0)
559192d8 3029 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
954b63d4
AM
3030 else if (eif->info->dynamic_undefined_weak > 0
3031 && h->ref_regular
3032 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3033 && !bfd_hide_sym_by_version (eif->info->version_info,
3034 h->root.root.string))
3035 {
3036 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
3037 {
3038 eif->failed = TRUE;
3039 return FALSE;
3040 }
3041 }
3042 }
3043
45d6a902
AM
3044 /* If this symbol does not require a PLT entry, and it is not
3045 defined by a dynamic object, or is not referenced by a regular
3046 object, ignore it. We do have to handle a weak defined symbol,
3047 even if no regular object refers to it, if we decided to add it
3048 to the dynamic symbol table. FIXME: Do we normally need to worry
3049 about symbols which are defined by one dynamic object and
3050 referenced by another one? */
f5385ebf 3051 if (!h->needs_plt
91e21fb7 3052 && h->type != STT_GNU_IFUNC
f5385ebf
AM
3053 && (h->def_regular
3054 || !h->def_dynamic
3055 || (!h->ref_regular
60d67dc8 3056 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
45d6a902 3057 {
a6aa5195 3058 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
3059 return TRUE;
3060 }
3061
3062 /* If we've already adjusted this symbol, don't do it again. This
3063 can happen via a recursive call. */
f5385ebf 3064 if (h->dynamic_adjusted)
45d6a902
AM
3065 return TRUE;
3066
3067 /* Don't look at this symbol again. Note that we must set this
3068 after checking the above conditions, because we may look at a
3069 symbol once, decide not to do anything, and then get called
3070 recursively later after REF_REGULAR is set below. */
f5385ebf 3071 h->dynamic_adjusted = 1;
45d6a902
AM
3072
3073 /* If this is a weak definition, and we know a real definition, and
3074 the real symbol is not itself defined by a regular object file,
3075 then get a good value for the real definition. We handle the
3076 real symbol first, for the convenience of the backend routine.
3077
3078 Note that there is a confusing case here. If the real definition
3079 is defined by a regular object file, we don't get the real symbol
3080 from the dynamic object, but we do get the weak symbol. If the
3081 processor backend uses a COPY reloc, then if some routine in the
3082 dynamic object changes the real symbol, we will not see that
3083 change in the corresponding weak symbol. This is the way other
3084 ELF linkers work as well, and seems to be a result of the shared
3085 library model.
3086
3087 I will clarify this issue. Most SVR4 shared libraries define the
3088 variable _timezone and define timezone as a weak synonym. The
3089 tzset call changes _timezone. If you write
3090 extern int timezone;
3091 int _timezone = 5;
3092 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3093 you might expect that, since timezone is a synonym for _timezone,
3094 the same number will print both times. However, if the processor
3095 backend uses a COPY reloc, then actually timezone will be copied
3096 into your process image, and, since you define _timezone
3097 yourself, _timezone will not. Thus timezone and _timezone will
3098 wind up at different memory locations. The tzset call will set
3099 _timezone, leaving timezone unchanged. */
3100
60d67dc8 3101 if (h->is_weakalias)
45d6a902 3102 {
60d67dc8
AM
3103 struct elf_link_hash_entry *def = weakdef (h);
3104
ec24dc88 3105 /* If we get to this point, there is an implicit reference to
60d67dc8
AM
3106 the alias by a regular object file via the weak symbol H. */
3107 def->ref_regular = 1;
45d6a902 3108
ec24dc88 3109 /* Ensure that the backend adjust_dynamic_symbol function sees
60d67dc8
AM
3110 the strong alias before H by recursively calling ourselves. */
3111 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
45d6a902
AM
3112 return FALSE;
3113 }
3114
3115 /* If a symbol has no type and no size and does not require a PLT
3116 entry, then we are probably about to do the wrong thing here: we
3117 are probably going to create a COPY reloc for an empty object.
3118 This case can arise when a shared object is built with assembly
3119 code, and the assembly code fails to set the symbol type. */
3120 if (h->size == 0
3121 && h->type == STT_NOTYPE
f5385ebf 3122 && !h->needs_plt)
4eca0228 3123 _bfd_error_handler
45d6a902
AM
3124 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3125 h->root.root.string);
3126
45d6a902
AM
3127 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3128 {
3129 eif->failed = TRUE;
3130 return FALSE;
3131 }
3132
3133 return TRUE;
3134}
3135
027297b7
L
3136/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3137 DYNBSS. */
3138
3139bfd_boolean
6cabe1ea
AM
3140_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3141 struct elf_link_hash_entry *h,
027297b7
L
3142 asection *dynbss)
3143{
91ac5911 3144 unsigned int power_of_two;
027297b7
L
3145 bfd_vma mask;
3146 asection *sec = h->root.u.def.section;
3147
de194d85 3148 /* The section alignment of the definition is the maximum alignment
91ac5911
L
3149 requirement of symbols defined in the section. Since we don't
3150 know the symbol alignment requirement, we start with the
3151 maximum alignment and check low bits of the symbol address
3152 for the minimum alignment. */
fd361982 3153 power_of_two = bfd_section_alignment (sec);
91ac5911
L
3154 mask = ((bfd_vma) 1 << power_of_two) - 1;
3155 while ((h->root.u.def.value & mask) != 0)
3156 {
3157 mask >>= 1;
3158 --power_of_two;
3159 }
027297b7 3160
fd361982 3161 if (power_of_two > bfd_section_alignment (dynbss))
027297b7
L
3162 {
3163 /* Adjust the section alignment if needed. */
fd361982 3164 if (!bfd_set_section_alignment (dynbss, power_of_two))
027297b7
L
3165 return FALSE;
3166 }
3167
91ac5911 3168 /* We make sure that the symbol will be aligned properly. */
027297b7
L
3169 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3170
3171 /* Define the symbol as being at this point in DYNBSS. */
3172 h->root.u.def.section = dynbss;
3173 h->root.u.def.value = dynbss->size;
3174
3175 /* Increment the size of DYNBSS to make room for the symbol. */
3176 dynbss->size += h->size;
3177
f7483970
L
3178 /* No error if extern_protected_data is true. */
3179 if (h->protected_def
889c2a67
L
3180 && (!info->extern_protected_data
3181 || (info->extern_protected_data < 0
3182 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05 3183 info->callbacks->einfo
c1c8c1ef 3184 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
d07a1b05 3185 h->root.root.string);
6cabe1ea 3186
027297b7
L
3187 return TRUE;
3188}
3189
45d6a902
AM
3190/* Adjust all external symbols pointing into SEC_MERGE sections
3191 to reflect the object merging within the sections. */
3192
28caa186 3193static bfd_boolean
268b6b39 3194_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
3195{
3196 asection *sec;
3197
45d6a902
AM
3198 if ((h->root.type == bfd_link_hash_defined
3199 || h->root.type == bfd_link_hash_defweak)
3200 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 3201 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 3202 {
a50b1753 3203 bfd *output_bfd = (bfd *) data;
45d6a902
AM
3204
3205 h->root.u.def.value =
3206 _bfd_merged_section_offset (output_bfd,
3207 &h->root.u.def.section,
3208 elf_section_data (sec)->sec_info,
753731ee 3209 h->root.u.def.value);
45d6a902
AM
3210 }
3211
3212 return TRUE;
3213}
986a241f
RH
3214
3215/* Returns false if the symbol referred to by H should be considered
3216 to resolve local to the current module, and true if it should be
3217 considered to bind dynamically. */
3218
3219bfd_boolean
268b6b39
AM
3220_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3221 struct bfd_link_info *info,
89a2ee5a 3222 bfd_boolean not_local_protected)
986a241f
RH
3223{
3224 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
3225 const struct elf_backend_data *bed;
3226 struct elf_link_hash_table *hash_table;
986a241f
RH
3227
3228 if (h == NULL)
3229 return FALSE;
3230
3231 while (h->root.type == bfd_link_hash_indirect
3232 || h->root.type == bfd_link_hash_warning)
3233 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3234
3235 /* If it was forced local, then clearly it's not dynamic. */
3236 if (h->dynindx == -1)
3237 return FALSE;
f5385ebf 3238 if (h->forced_local)
986a241f
RH
3239 return FALSE;
3240
3241 /* Identify the cases where name binding rules say that a
3242 visible symbol resolves locally. */
0e1862bb
L
3243 binding_stays_local_p = (bfd_link_executable (info)
3244 || SYMBOLIC_BIND (info, h));
986a241f
RH
3245
3246 switch (ELF_ST_VISIBILITY (h->other))
3247 {
3248 case STV_INTERNAL:
3249 case STV_HIDDEN:
3250 return FALSE;
3251
3252 case STV_PROTECTED:
fcb93ecf
PB
3253 hash_table = elf_hash_table (info);
3254 if (!is_elf_hash_table (hash_table))
3255 return FALSE;
3256
3257 bed = get_elf_backend_data (hash_table->dynobj);
3258
986a241f
RH
3259 /* Proper resolution for function pointer equality may require
3260 that these symbols perhaps be resolved dynamically, even though
3261 we should be resolving them to the current module. */
89a2ee5a 3262 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
3263 binding_stays_local_p = TRUE;
3264 break;
3265
3266 default:
986a241f
RH
3267 break;
3268 }
3269
aa37626c 3270 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 3271 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
3272 return TRUE;
3273
986a241f
RH
3274 /* Otherwise, the symbol is dynamic if binding rules don't tell
3275 us that it remains local. */
3276 return !binding_stays_local_p;
3277}
f6c52c13
AM
3278
3279/* Return true if the symbol referred to by H should be considered
3280 to resolve local to the current module, and false otherwise. Differs
3281 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 3282 undefined symbols. The two functions are virtually identical except
0fad2956
MR
3283 for the place where dynindx == -1 is tested. If that test is true,
3284 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3285 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3286 defined symbols.
89a2ee5a
AM
3287 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3288 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3289 treatment of undefined weak symbols. For those that do not make
3290 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
3291
3292bfd_boolean
268b6b39
AM
3293_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3294 struct bfd_link_info *info,
3295 bfd_boolean local_protected)
f6c52c13 3296{
fcb93ecf
PB
3297 const struct elf_backend_data *bed;
3298 struct elf_link_hash_table *hash_table;
3299
f6c52c13
AM
3300 /* If it's a local sym, of course we resolve locally. */
3301 if (h == NULL)
3302 return TRUE;
3303
d95edcac
L
3304 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3305 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3306 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3307 return TRUE;
3308
0fad2956
MR
3309 /* Forced local symbols resolve locally. */
3310 if (h->forced_local)
3311 return TRUE;
3312
7e2294f9
AO
3313 /* Common symbols that become definitions don't get the DEF_REGULAR
3314 flag set, so test it first, and don't bail out. */
3315 if (ELF_COMMON_DEF_P (h))
3316 /* Do nothing. */;
f6c52c13 3317 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
3318 resolve locally. The sym is either undefined or dynamic. */
3319 else if (!h->def_regular)
f6c52c13
AM
3320 return FALSE;
3321
0fad2956 3322 /* Non-dynamic symbols resolve locally. */
f6c52c13
AM
3323 if (h->dynindx == -1)
3324 return TRUE;
3325
3326 /* At this point, we know the symbol is defined and dynamic. In an
3327 executable it must resolve locally, likewise when building symbolic
3328 shared libraries. */
0e1862bb 3329 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
f6c52c13
AM
3330 return TRUE;
3331
3332 /* Now deal with defined dynamic symbols in shared libraries. Ones
3333 with default visibility might not resolve locally. */
3334 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3335 return FALSE;
3336
fcb93ecf
PB
3337 hash_table = elf_hash_table (info);
3338 if (!is_elf_hash_table (hash_table))
3339 return TRUE;
3340
3341 bed = get_elf_backend_data (hash_table->dynobj);
3342
f7483970
L
3343 /* If extern_protected_data is false, STV_PROTECTED non-function
3344 symbols are local. */
889c2a67
L
3345 if ((!info->extern_protected_data
3346 || (info->extern_protected_data < 0
3347 && !bed->extern_protected_data))
3348 && !bed->is_function_type (h->type))
1c16dfa5
L
3349 return TRUE;
3350
f6c52c13 3351 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
3352 symbols be treated as dynamic symbols. If the address of a
3353 function not defined in an executable is set to that function's
3354 plt entry in the executable, then the address of the function in
3355 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
3356 return local_protected;
3357}
e1918d23
AM
3358
3359/* Caches some TLS segment info, and ensures that the TLS segment vma is
3360 aligned. Returns the first TLS output section. */
3361
3362struct bfd_section *
3363_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3364{
3365 struct bfd_section *sec, *tls;
3366 unsigned int align = 0;
3367
3368 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3369 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3370 break;
3371 tls = sec;
3372
3373 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3374 if (sec->alignment_power > align)
3375 align = sec->alignment_power;
3376
3377 elf_hash_table (info)->tls_sec = tls;
3378
fdde2fb6
SH
3379 /* Ensure the alignment of the first section (usually .tdata) is the largest
3380 alignment, so that the tls segment starts aligned. */
e1918d23
AM
3381 if (tls != NULL)
3382 tls->alignment_power = align;
3383
3384 return tls;
3385}
0ad989f9
L
3386
3387/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3388static bfd_boolean
3389is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3390 Elf_Internal_Sym *sym)
3391{
a4d8e49b
L
3392 const struct elf_backend_data *bed;
3393
0ad989f9
L
3394 /* Local symbols do not count, but target specific ones might. */
3395 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3396 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3397 return FALSE;
3398
fcb93ecf 3399 bed = get_elf_backend_data (abfd);
0ad989f9 3400 /* Function symbols do not count. */
fcb93ecf 3401 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
3402 return FALSE;
3403
3404 /* If the section is undefined, then so is the symbol. */
3405 if (sym->st_shndx == SHN_UNDEF)
3406 return FALSE;
3407
3408 /* If the symbol is defined in the common section, then
3409 it is a common definition and so does not count. */
a4d8e49b 3410 if (bed->common_definition (sym))
0ad989f9
L
3411 return FALSE;
3412
3413 /* If the symbol is in a target specific section then we
3414 must rely upon the backend to tell us what it is. */
3415 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3416 /* FIXME - this function is not coded yet:
3417
3418 return _bfd_is_global_symbol_definition (abfd, sym);
3419
3420 Instead for now assume that the definition is not global,
3421 Even if this is wrong, at least the linker will behave
3422 in the same way that it used to do. */
3423 return FALSE;
3424
3425 return TRUE;
3426}
3427
3428/* Search the symbol table of the archive element of the archive ABFD
3429 whose archive map contains a mention of SYMDEF, and determine if
3430 the symbol is defined in this element. */
3431static bfd_boolean
3432elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3433{
3434 Elf_Internal_Shdr * hdr;
ef53be89
AM
3435 size_t symcount;
3436 size_t extsymcount;
3437 size_t extsymoff;
0ad989f9
L
3438 Elf_Internal_Sym *isymbuf;
3439 Elf_Internal_Sym *isym;
3440 Elf_Internal_Sym *isymend;
3441 bfd_boolean result;
3442
3443 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3444 if (abfd == NULL)
3445 return FALSE;
3446
3447 if (! bfd_check_format (abfd, bfd_object))
3448 return FALSE;
3449
7dc3990e
L
3450 /* Select the appropriate symbol table. If we don't know if the
3451 object file is an IR object, give linker LTO plugin a chance to
3452 get the correct symbol table. */
3453 if (abfd->plugin_format == bfd_plugin_yes
08ce1d72 3454#if BFD_SUPPORTS_PLUGINS
7dc3990e
L
3455 || (abfd->plugin_format == bfd_plugin_unknown
3456 && bfd_link_plugin_object_p (abfd))
3457#endif
3458 )
3459 {
3460 /* Use the IR symbol table if the object has been claimed by
3461 plugin. */
3462 abfd = abfd->plugin_dummy_bfd;
3463 hdr = &elf_tdata (abfd)->symtab_hdr;
3464 }
3465 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
0ad989f9
L
3466 hdr = &elf_tdata (abfd)->symtab_hdr;
3467 else
3468 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3469
3470 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3471
3472 /* The sh_info field of the symtab header tells us where the
3473 external symbols start. We don't care about the local symbols. */
3474 if (elf_bad_symtab (abfd))
3475 {
3476 extsymcount = symcount;
3477 extsymoff = 0;
3478 }
3479 else
3480 {
3481 extsymcount = symcount - hdr->sh_info;
3482 extsymoff = hdr->sh_info;
3483 }
3484
3485 if (extsymcount == 0)
3486 return FALSE;
3487
3488 /* Read in the symbol table. */
3489 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3490 NULL, NULL, NULL);
3491 if (isymbuf == NULL)
3492 return FALSE;
3493
3494 /* Scan the symbol table looking for SYMDEF. */
3495 result = FALSE;
3496 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3497 {
3498 const char *name;
3499
3500 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3501 isym->st_name);
3502 if (name == NULL)
3503 break;
3504
3505 if (strcmp (name, symdef->name) == 0)
3506 {
3507 result = is_global_data_symbol_definition (abfd, isym);
3508 break;
3509 }
3510 }
3511
3512 free (isymbuf);
3513
3514 return result;
3515}
3516\f
5a580b3a
AM
3517/* Add an entry to the .dynamic table. */
3518
3519bfd_boolean
3520_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3521 bfd_vma tag,
3522 bfd_vma val)
3523{
3524 struct elf_link_hash_table *hash_table;
3525 const struct elf_backend_data *bed;
3526 asection *s;
3527 bfd_size_type newsize;
3528 bfd_byte *newcontents;
3529 Elf_Internal_Dyn dyn;
3530
3531 hash_table = elf_hash_table (info);
3532 if (! is_elf_hash_table (hash_table))
3533 return FALSE;
3534
7f923b7f
AM
3535 if (tag == DT_RELA || tag == DT_REL)
3536 hash_table->dynamic_relocs = TRUE;
3537
5a580b3a 3538 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3539 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3540 BFD_ASSERT (s != NULL);
3541
eea6121a 3542 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3543 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3544 if (newcontents == NULL)
3545 return FALSE;
3546
3547 dyn.d_tag = tag;
3548 dyn.d_un.d_val = val;
eea6121a 3549 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3550
eea6121a 3551 s->size = newsize;
5a580b3a
AM
3552 s->contents = newcontents;
3553
3554 return TRUE;
3555}
3556
6f6fd151
L
3557/* Strip zero-sized dynamic sections. */
3558
3559bfd_boolean
3560_bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3561{
3562 struct elf_link_hash_table *hash_table;
3563 const struct elf_backend_data *bed;
3564 asection *s, *sdynamic, **pp;
3565 asection *rela_dyn, *rel_dyn;
3566 Elf_Internal_Dyn dyn;
3567 bfd_byte *extdyn, *next;
3568 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3569 bfd_boolean strip_zero_sized;
3570 bfd_boolean strip_zero_sized_plt;
3571
3572 if (bfd_link_relocatable (info))
3573 return TRUE;
3574
3575 hash_table = elf_hash_table (info);
3576 if (!is_elf_hash_table (hash_table))
3577 return FALSE;
3578
3579 if (!hash_table->dynobj)
3580 return TRUE;
3581
3582 sdynamic= bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3583 if (!sdynamic)
3584 return TRUE;
3585
3586 bed = get_elf_backend_data (hash_table->dynobj);
3587 swap_dyn_in = bed->s->swap_dyn_in;
3588
3589 strip_zero_sized = FALSE;
3590 strip_zero_sized_plt = FALSE;
3591
3592 /* Strip zero-sized dynamic sections. */
3593 rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3594 rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3595 for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3596 if (s->size == 0
3597 && (s == rela_dyn
3598 || s == rel_dyn
3599 || s == hash_table->srelplt->output_section
3600 || s == hash_table->splt->output_section))
3601 {
3602 *pp = s->next;
3603 info->output_bfd->section_count--;
3604 strip_zero_sized = TRUE;
3605 if (s == rela_dyn)
3606 s = rela_dyn;
3607 if (s == rel_dyn)
3608 s = rel_dyn;
3609 else if (s == hash_table->splt->output_section)
3610 {
3611 s = hash_table->splt;
3612 strip_zero_sized_plt = TRUE;
3613 }
3614 else
3615 s = hash_table->srelplt;
3616 s->flags |= SEC_EXCLUDE;
3617 s->output_section = bfd_abs_section_ptr;
3618 }
3619 else
3620 pp = &s->next;
3621
3622 if (strip_zero_sized_plt)
3623 for (extdyn = sdynamic->contents;
3624 extdyn < sdynamic->contents + sdynamic->size;
3625 extdyn = next)
3626 {
3627 next = extdyn + bed->s->sizeof_dyn;
3628 swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3629 switch (dyn.d_tag)
3630 {
3631 default:
3632 break;
3633 case DT_JMPREL:
3634 case DT_PLTRELSZ:
3635 case DT_PLTREL:
3636 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3637 the procedure linkage table (the .plt section) has been
3638 removed. */
3639 memmove (extdyn, next,
3640 sdynamic->size - (next - sdynamic->contents));
3641 next = extdyn;
3642 }
3643 }
3644
3645 if (strip_zero_sized)
3646 {
3647 /* Regenerate program headers. */
3648 elf_seg_map (info->output_bfd) = NULL;
3649 return _bfd_elf_map_sections_to_segments (info->output_bfd, info);
3650 }
3651
3652 return TRUE;
3653}
3654
e310298c 3655/* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
5a580b3a
AM
3656 1 if a DT_NEEDED tag already exists, and 0 on success. */
3657
e310298c
AM
3658int
3659bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
5a580b3a
AM
3660{
3661 struct elf_link_hash_table *hash_table;
ef53be89 3662 size_t strindex;
e310298c 3663 const char *soname;
5a580b3a 3664
7e9f0867
AM
3665 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3666 return -1;
3667
5a580b3a 3668 hash_table = elf_hash_table (info);
e310298c 3669 soname = elf_dt_name (abfd);
5a580b3a 3670 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
ef53be89 3671 if (strindex == (size_t) -1)
5a580b3a
AM
3672 return -1;
3673
02be4619 3674 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3675 {
3676 asection *sdyn;
3677 const struct elf_backend_data *bed;
3678 bfd_byte *extdyn;
3679
3680 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3681 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3682 if (sdyn != NULL)
3683 for (extdyn = sdyn->contents;
3684 extdyn < sdyn->contents + sdyn->size;
3685 extdyn += bed->s->sizeof_dyn)
3686 {
3687 Elf_Internal_Dyn dyn;
5a580b3a 3688
7e9f0867
AM
3689 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3690 if (dyn.d_tag == DT_NEEDED
3691 && dyn.d_un.d_val == strindex)
3692 {
3693 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3694 return 1;
3695 }
3696 }
5a580b3a
AM
3697 }
3698
e310298c
AM
3699 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3700 return -1;
7e9f0867 3701
e310298c
AM
3702 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3703 return -1;
5a580b3a
AM
3704
3705 return 0;
3706}
3707
7b15fa7a
AM
3708/* Return true if SONAME is on the needed list between NEEDED and STOP
3709 (or the end of list if STOP is NULL), and needed by a library that
3710 will be loaded. */
3711
010e5ae2 3712static bfd_boolean
7b15fa7a
AM
3713on_needed_list (const char *soname,
3714 struct bfd_link_needed_list *needed,
3715 struct bfd_link_needed_list *stop)
010e5ae2 3716{
7b15fa7a
AM
3717 struct bfd_link_needed_list *look;
3718 for (look = needed; look != stop; look = look->next)
3719 if (strcmp (soname, look->name) == 0
3720 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3721 /* If needed by a library that itself is not directly
3722 needed, recursively check whether that library is
3723 indirectly needed. Since we add DT_NEEDED entries to
3724 the end of the list, library dependencies appear after
3725 the library. Therefore search prior to the current
3726 LOOK, preventing possible infinite recursion. */
3727 || on_needed_list (elf_dt_name (look->by), needed, look)))
010e5ae2
AM
3728 return TRUE;
3729
3730 return FALSE;
3731}
3732
3a3f4bf7 3733/* Sort symbol by value, section, size, and type. */
4ad4eba5
AM
3734static int
3735elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3736{
3737 const struct elf_link_hash_entry *h1;
3738 const struct elf_link_hash_entry *h2;
10b7e05b 3739 bfd_signed_vma vdiff;
3a3f4bf7
AM
3740 int sdiff;
3741 const char *n1;
3742 const char *n2;
5a580b3a
AM
3743
3744 h1 = *(const struct elf_link_hash_entry **) arg1;
3745 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3746 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3747 if (vdiff != 0)
3748 return vdiff > 0 ? 1 : -1;
3a3f4bf7
AM
3749
3750 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3751 if (sdiff != 0)
3752 return sdiff;
3753
3754 /* Sort so that sized symbols are selected over zero size symbols. */
3755 vdiff = h1->size - h2->size;
3756 if (vdiff != 0)
3757 return vdiff > 0 ? 1 : -1;
3758
3759 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3760 if (h1->type != h2->type)
3761 return h1->type - h2->type;
3762
3763 /* If symbols are properly sized and typed, and multiple strong
3764 aliases are not defined in a shared library by the user we
3765 shouldn't get here. Unfortunately linker script symbols like
3766 __bss_start sometimes match a user symbol defined at the start of
3767 .bss without proper size and type. We'd like to preference the
3768 user symbol over reserved system symbols. Sort on leading
3769 underscores. */
3770 n1 = h1->root.root.string;
3771 n2 = h2->root.root.string;
3772 while (*n1 == *n2)
10b7e05b 3773 {
3a3f4bf7
AM
3774 if (*n1 == 0)
3775 break;
3776 ++n1;
3777 ++n2;
10b7e05b 3778 }
3a3f4bf7
AM
3779 if (*n1 == '_')
3780 return -1;
3781 if (*n2 == '_')
3782 return 1;
3783
3784 /* Final sort on name selects user symbols like '_u' over reserved
3785 system symbols like '_Z' and also will avoid qsort instability. */
3786 return *n1 - *n2;
5a580b3a 3787}
4ad4eba5 3788
5a580b3a
AM
3789/* This function is used to adjust offsets into .dynstr for
3790 dynamic symbols. This is called via elf_link_hash_traverse. */
3791
3792static bfd_boolean
3793elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3794{
a50b1753 3795 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3796
5a580b3a
AM
3797 if (h->dynindx != -1)
3798 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3799 return TRUE;
3800}
3801
3802/* Assign string offsets in .dynstr, update all structures referencing
3803 them. */
3804
4ad4eba5
AM
3805static bfd_boolean
3806elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3807{
3808 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3809 struct elf_link_local_dynamic_entry *entry;
3810 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3811 bfd *dynobj = hash_table->dynobj;
3812 asection *sdyn;
3813 bfd_size_type size;
3814 const struct elf_backend_data *bed;
3815 bfd_byte *extdyn;
3816
3817 _bfd_elf_strtab_finalize (dynstr);
3818 size = _bfd_elf_strtab_size (dynstr);
3819
3d16b64e
NA
3820 /* Allow the linker to examine the dynsymtab now it's fully populated. */
3821
3822 if (info->callbacks->examine_strtab)
3823 info->callbacks->examine_strtab (dynstr);
3824
5a580b3a 3825 bed = get_elf_backend_data (dynobj);
3d4d4302 3826 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3827 BFD_ASSERT (sdyn != NULL);
3828
3829 /* Update all .dynamic entries referencing .dynstr strings. */
3830 for (extdyn = sdyn->contents;
eea6121a 3831 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3832 extdyn += bed->s->sizeof_dyn)
3833 {
3834 Elf_Internal_Dyn dyn;
3835
3836 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3837 switch (dyn.d_tag)
3838 {
3839 case DT_STRSZ:
3840 dyn.d_un.d_val = size;
3841 break;
3842 case DT_NEEDED:
3843 case DT_SONAME:
3844 case DT_RPATH:
3845 case DT_RUNPATH:
3846 case DT_FILTER:
3847 case DT_AUXILIARY:
7ee314fa
AM
3848 case DT_AUDIT:
3849 case DT_DEPAUDIT:
5a580b3a
AM
3850 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3851 break;
3852 default:
3853 continue;
3854 }
3855 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3856 }
3857
3858 /* Now update local dynamic symbols. */
3859 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3860 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3861 entry->isym.st_name);
3862
3863 /* And the rest of dynamic symbols. */
3864 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3865
3866 /* Adjust version definitions. */
3867 if (elf_tdata (output_bfd)->cverdefs)
3868 {
3869 asection *s;
3870 bfd_byte *p;
ef53be89 3871 size_t i;
5a580b3a
AM
3872 Elf_Internal_Verdef def;
3873 Elf_Internal_Verdaux defaux;
3874
3d4d4302 3875 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3876 p = s->contents;
3877 do
3878 {
3879 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3880 &def);
3881 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3882 if (def.vd_aux != sizeof (Elf_External_Verdef))
3883 continue;
5a580b3a
AM
3884 for (i = 0; i < def.vd_cnt; ++i)
3885 {
3886 _bfd_elf_swap_verdaux_in (output_bfd,
3887 (Elf_External_Verdaux *) p, &defaux);
3888 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3889 defaux.vda_name);
3890 _bfd_elf_swap_verdaux_out (output_bfd,
3891 &defaux, (Elf_External_Verdaux *) p);
3892 p += sizeof (Elf_External_Verdaux);
3893 }
3894 }
3895 while (def.vd_next);
3896 }
3897
3898 /* Adjust version references. */
3899 if (elf_tdata (output_bfd)->verref)
3900 {
3901 asection *s;
3902 bfd_byte *p;
ef53be89 3903 size_t i;
5a580b3a
AM
3904 Elf_Internal_Verneed need;
3905 Elf_Internal_Vernaux needaux;
3906
3d4d4302 3907 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3908 p = s->contents;
3909 do
3910 {
3911 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3912 &need);
3913 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3914 _bfd_elf_swap_verneed_out (output_bfd, &need,
3915 (Elf_External_Verneed *) p);
3916 p += sizeof (Elf_External_Verneed);
3917 for (i = 0; i < need.vn_cnt; ++i)
3918 {
3919 _bfd_elf_swap_vernaux_in (output_bfd,
3920 (Elf_External_Vernaux *) p, &needaux);
3921 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3922 needaux.vna_name);
3923 _bfd_elf_swap_vernaux_out (output_bfd,
3924 &needaux,
3925 (Elf_External_Vernaux *) p);
3926 p += sizeof (Elf_External_Vernaux);
3927 }
3928 }
3929 while (need.vn_next);
3930 }
3931
3932 return TRUE;
3933}
3934\f
13285a1b
AM
3935/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3936 The default is to only match when the INPUT and OUTPUT are exactly
3937 the same target. */
3938
3939bfd_boolean
3940_bfd_elf_default_relocs_compatible (const bfd_target *input,
3941 const bfd_target *output)
3942{
3943 return input == output;
3944}
3945
3946/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3947 This version is used when different targets for the same architecture
3948 are virtually identical. */
3949
3950bfd_boolean
3951_bfd_elf_relocs_compatible (const bfd_target *input,
3952 const bfd_target *output)
3953{
3954 const struct elf_backend_data *obed, *ibed;
3955
3956 if (input == output)
3957 return TRUE;
3958
3959 ibed = xvec_get_elf_backend_data (input);
3960 obed = xvec_get_elf_backend_data (output);
3961
3962 if (ibed->arch != obed->arch)
3963 return FALSE;
3964
3965 /* If both backends are using this function, deem them compatible. */
3966 return ibed->relocs_compatible == obed->relocs_compatible;
3967}
3968
e5034e59
AM
3969/* Make a special call to the linker "notice" function to tell it that
3970 we are about to handle an as-needed lib, or have finished
1b786873 3971 processing the lib. */
e5034e59
AM
3972
3973bfd_boolean
3974_bfd_elf_notice_as_needed (bfd *ibfd,
3975 struct bfd_link_info *info,
3976 enum notice_asneeded_action act)
3977{
46135103 3978 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3979}
3980
d9689752
L
3981/* Check relocations an ELF object file. */
3982
3983bfd_boolean
3984_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3985{
3986 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3987 struct elf_link_hash_table *htab = elf_hash_table (info);
3988
3989 /* If this object is the same format as the output object, and it is
3990 not a shared library, then let the backend look through the
3991 relocs.
3992
3993 This is required to build global offset table entries and to
3994 arrange for dynamic relocs. It is not required for the
3995 particular common case of linking non PIC code, even when linking
3996 against shared libraries, but unfortunately there is no way of
3997 knowing whether an object file has been compiled PIC or not.
3998 Looking through the relocs is not particularly time consuming.
3999 The problem is that we must either (1) keep the relocs in memory,
4000 which causes the linker to require additional runtime memory or
4001 (2) read the relocs twice from the input file, which wastes time.
4002 This would be a good case for using mmap.
4003
4004 I have no idea how to handle linking PIC code into a file of a
4005 different format. It probably can't be done. */
4006 if ((abfd->flags & DYNAMIC) == 0
4007 && is_elf_hash_table (htab)
4008 && bed->check_relocs != NULL
4009 && elf_object_id (abfd) == elf_hash_table_id (htab)
4010 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4011 {
4012 asection *o;
4013
4014 for (o = abfd->sections; o != NULL; o = o->next)
4015 {
4016 Elf_Internal_Rela *internal_relocs;
4017 bfd_boolean ok;
4018
c4b126b8
L
4019 /* Don't check relocations in excluded sections. Don't do
4020 anything special with non-loaded, non-alloced sections.
4021 In particular, any relocs in such sections should not
4022 affect GOT and PLT reference counting (ie. we don't
4023 allow them to create GOT or PLT entries), there's no
4024 possibility or desire to optimize TLS relocs, and
4025 there's not much point in propagating relocs to shared
4026 libs that the dynamic linker won't relocate. */
4027 if ((o->flags & SEC_ALLOC) == 0
4028 || (o->flags & SEC_RELOC) == 0
5ce03cea 4029 || (o->flags & SEC_EXCLUDE) != 0
d9689752
L
4030 || o->reloc_count == 0
4031 || ((info->strip == strip_all || info->strip == strip_debugger)
4032 && (o->flags & SEC_DEBUGGING) != 0)
4033 || bfd_is_abs_section (o->output_section))
4034 continue;
4035
4036 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4037 info->keep_memory);
4038 if (internal_relocs == NULL)
4039 return FALSE;
4040
4041 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4042
4043 if (elf_section_data (o)->relocs != internal_relocs)
4044 free (internal_relocs);
4045
4046 if (! ok)
4047 return FALSE;
4048 }
4049 }
4050
4051 return TRUE;
4052}
4053
4ad4eba5
AM
4054/* Add symbols from an ELF object file to the linker hash table. */
4055
4056static bfd_boolean
4057elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4058{
a0c402a5 4059 Elf_Internal_Ehdr *ehdr;
4ad4eba5 4060 Elf_Internal_Shdr *hdr;
ef53be89
AM
4061 size_t symcount;
4062 size_t extsymcount;
4063 size_t extsymoff;
4ad4eba5
AM
4064 struct elf_link_hash_entry **sym_hash;
4065 bfd_boolean dynamic;
4066 Elf_External_Versym *extversym = NULL;
be22c732 4067 Elf_External_Versym *extversym_end = NULL;
4ad4eba5
AM
4068 Elf_External_Versym *ever;
4069 struct elf_link_hash_entry *weaks;
4070 struct elf_link_hash_entry **nondeflt_vers = NULL;
ef53be89 4071 size_t nondeflt_vers_cnt = 0;
4ad4eba5
AM
4072 Elf_Internal_Sym *isymbuf = NULL;
4073 Elf_Internal_Sym *isym;
4074 Elf_Internal_Sym *isymend;
4075 const struct elf_backend_data *bed;
4076 bfd_boolean add_needed;
66eb6687 4077 struct elf_link_hash_table *htab;
66eb6687 4078 void *alloc_mark = NULL;
4f87808c
AM
4079 struct bfd_hash_entry **old_table = NULL;
4080 unsigned int old_size = 0;
4081 unsigned int old_count = 0;
66eb6687 4082 void *old_tab = NULL;
66eb6687
AM
4083 void *old_ent;
4084 struct bfd_link_hash_entry *old_undefs = NULL;
4085 struct bfd_link_hash_entry *old_undefs_tail = NULL;
5b677558 4086 void *old_strtab = NULL;
66eb6687 4087 size_t tabsize = 0;
db6a5d5f 4088 asection *s;
29a9f53e 4089 bfd_boolean just_syms;
4ad4eba5 4090
66eb6687 4091 htab = elf_hash_table (info);
4ad4eba5 4092 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
4093
4094 if ((abfd->flags & DYNAMIC) == 0)
4095 dynamic = FALSE;
4096 else
4097 {
4098 dynamic = TRUE;
4099
4100 /* You can't use -r against a dynamic object. Also, there's no
4101 hope of using a dynamic object which does not exactly match
4102 the format of the output file. */
0e1862bb 4103 if (bfd_link_relocatable (info)
66eb6687 4104 || !is_elf_hash_table (htab)
f13a99db 4105 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 4106 {
0e1862bb 4107 if (bfd_link_relocatable (info))
9a0789ec
NC
4108 bfd_set_error (bfd_error_invalid_operation);
4109 else
4110 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
4111 goto error_return;
4112 }
4113 }
4114
a0c402a5
L
4115 ehdr = elf_elfheader (abfd);
4116 if (info->warn_alternate_em
4117 && bed->elf_machine_code != ehdr->e_machine
4118 && ((bed->elf_machine_alt1 != 0
4119 && ehdr->e_machine == bed->elf_machine_alt1)
4120 || (bed->elf_machine_alt2 != 0
4121 && ehdr->e_machine == bed->elf_machine_alt2)))
9793eb77 4122 _bfd_error_handler
695344c0 4123 /* xgettext:c-format */
9793eb77 4124 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
a0c402a5
L
4125 ehdr->e_machine, abfd, bed->elf_machine_code);
4126
4ad4eba5
AM
4127 /* As a GNU extension, any input sections which are named
4128 .gnu.warning.SYMBOL are treated as warning symbols for the given
4129 symbol. This differs from .gnu.warning sections, which generate
4130 warnings when they are included in an output file. */
dd98f8d2 4131 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 4132 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 4133 {
db6a5d5f 4134 const char *name;
4ad4eba5 4135
fd361982 4136 name = bfd_section_name (s);
db6a5d5f 4137 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 4138 {
db6a5d5f
AM
4139 char *msg;
4140 bfd_size_type sz;
4141
4142 name += sizeof ".gnu.warning." - 1;
4143
4144 /* If this is a shared object, then look up the symbol
4145 in the hash table. If it is there, and it is already
4146 been defined, then we will not be using the entry
4147 from this shared object, so we don't need to warn.
4148 FIXME: If we see the definition in a regular object
4149 later on, we will warn, but we shouldn't. The only
4150 fix is to keep track of what warnings we are supposed
4151 to emit, and then handle them all at the end of the
4152 link. */
4153 if (dynamic)
4ad4eba5 4154 {
db6a5d5f
AM
4155 struct elf_link_hash_entry *h;
4156
4157 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
4158
4159 /* FIXME: What about bfd_link_hash_common? */
4160 if (h != NULL
4161 && (h->root.type == bfd_link_hash_defined
4162 || h->root.type == bfd_link_hash_defweak))
4163 continue;
4164 }
4ad4eba5 4165
db6a5d5f
AM
4166 sz = s->size;
4167 msg = (char *) bfd_alloc (abfd, sz + 1);
4168 if (msg == NULL)
4169 goto error_return;
4ad4eba5 4170
db6a5d5f
AM
4171 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4172 goto error_return;
4ad4eba5 4173
db6a5d5f 4174 msg[sz] = '\0';
4ad4eba5 4175
db6a5d5f
AM
4176 if (! (_bfd_generic_link_add_one_symbol
4177 (info, abfd, name, BSF_WARNING, s, 0, msg,
4178 FALSE, bed->collect, NULL)))
4179 goto error_return;
4ad4eba5 4180
0e1862bb 4181 if (bfd_link_executable (info))
db6a5d5f
AM
4182 {
4183 /* Clobber the section size so that the warning does
4184 not get copied into the output file. */
4185 s->size = 0;
11d2f718 4186
db6a5d5f
AM
4187 /* Also set SEC_EXCLUDE, so that symbols defined in
4188 the warning section don't get copied to the output. */
4189 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
4190 }
4191 }
4192 }
4193
29a9f53e
L
4194 just_syms = ((s = abfd->sections) != NULL
4195 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4196
4ad4eba5
AM
4197 add_needed = TRUE;
4198 if (! dynamic)
4199 {
4200 /* If we are creating a shared library, create all the dynamic
4201 sections immediately. We need to attach them to something,
4202 so we attach them to this BFD, provided it is the right
bf89386a
L
4203 format and is not from ld --just-symbols. Always create the
4204 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
4205 are no input BFD's of the same format as the output, we can't
4206 make a shared library. */
4207 if (!just_syms
bf89386a 4208 && (bfd_link_pic (info)
9c1d7a08 4209 || (!bfd_link_relocatable (info)
3c5fce9b 4210 && info->nointerp
9c1d7a08 4211 && (info->export_dynamic || info->dynamic)))
66eb6687 4212 && is_elf_hash_table (htab)
f13a99db 4213 && info->output_bfd->xvec == abfd->xvec
66eb6687 4214 && !htab->dynamic_sections_created)
4ad4eba5
AM
4215 {
4216 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4217 goto error_return;
4218 }
4219 }
66eb6687 4220 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
4221 goto error_return;
4222 else
4223 {
4ad4eba5 4224 const char *soname = NULL;
7ee314fa 4225 char *audit = NULL;
4ad4eba5 4226 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
9acc85a6 4227 const Elf_Internal_Phdr *phdr;
e310298c 4228 struct elf_link_loaded_list *loaded_lib;
4ad4eba5
AM
4229
4230 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 4231 ld shouldn't allow it. */
29a9f53e 4232 if (just_syms)
92fd189d 4233 abort ();
4ad4eba5
AM
4234
4235 /* If this dynamic lib was specified on the command line with
4236 --as-needed in effect, then we don't want to add a DT_NEEDED
4237 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
4238 in by another lib's DT_NEEDED. When --no-add-needed is used
4239 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4240 any dynamic library in DT_NEEDED tags in the dynamic lib at
4241 all. */
4242 add_needed = (elf_dyn_lib_class (abfd)
4243 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4244 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
4245
4246 s = bfd_get_section_by_name (abfd, ".dynamic");
4247 if (s != NULL)
4248 {
4249 bfd_byte *dynbuf;
4250 bfd_byte *extdyn;
cb33740c 4251 unsigned int elfsec;
4ad4eba5
AM
4252 unsigned long shlink;
4253
eea6121a 4254 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194 4255 {
dc1e8a47 4256 error_free_dyn:
f8703194
L
4257 free (dynbuf);
4258 goto error_return;
4259 }
4ad4eba5
AM
4260
4261 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 4262 if (elfsec == SHN_BAD)
4ad4eba5
AM
4263 goto error_free_dyn;
4264 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4265
4266 for (extdyn = dynbuf;
9bff840e 4267 extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
4ad4eba5
AM
4268 extdyn += bed->s->sizeof_dyn)
4269 {
4270 Elf_Internal_Dyn dyn;
4271
4272 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4273 if (dyn.d_tag == DT_SONAME)
4274 {
4275 unsigned int tagv = dyn.d_un.d_val;
4276 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4277 if (soname == NULL)
4278 goto error_free_dyn;
4279 }
4280 if (dyn.d_tag == DT_NEEDED)
4281 {
4282 struct bfd_link_needed_list *n, **pn;
4283 char *fnm, *anm;
4284 unsigned int tagv = dyn.d_un.d_val;
986f0783 4285 size_t amt = sizeof (struct bfd_link_needed_list);
4ad4eba5 4286
a50b1753 4287 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4288 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4289 if (n == NULL || fnm == NULL)
4290 goto error_free_dyn;
4291 amt = strlen (fnm) + 1;
a50b1753 4292 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4293 if (anm == NULL)
4294 goto error_free_dyn;
4295 memcpy (anm, fnm, amt);
4296 n->name = anm;
4297 n->by = abfd;
4298 n->next = NULL;
66eb6687 4299 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4300 ;
4301 *pn = n;
4302 }
4303 if (dyn.d_tag == DT_RUNPATH)
4304 {
4305 struct bfd_link_needed_list *n, **pn;
4306 char *fnm, *anm;
4307 unsigned int tagv = dyn.d_un.d_val;
986f0783 4308 size_t amt = sizeof (struct bfd_link_needed_list);
4ad4eba5 4309
a50b1753 4310 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4311 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4312 if (n == NULL || fnm == NULL)
4313 goto error_free_dyn;
4314 amt = strlen (fnm) + 1;
a50b1753 4315 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4316 if (anm == NULL)
4317 goto error_free_dyn;
4318 memcpy (anm, fnm, amt);
4319 n->name = anm;
4320 n->by = abfd;
4321 n->next = NULL;
4322 for (pn = & runpath;
4323 *pn != NULL;
4324 pn = &(*pn)->next)
4325 ;
4326 *pn = n;
4327 }
4328 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4329 if (!runpath && dyn.d_tag == DT_RPATH)
4330 {
4331 struct bfd_link_needed_list *n, **pn;
4332 char *fnm, *anm;
4333 unsigned int tagv = dyn.d_un.d_val;
986f0783 4334 size_t amt = sizeof (struct bfd_link_needed_list);
4ad4eba5 4335
a50b1753 4336 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4337 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4338 if (n == NULL || fnm == NULL)
4339 goto error_free_dyn;
4340 amt = strlen (fnm) + 1;
a50b1753 4341 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 4342 if (anm == NULL)
f8703194 4343 goto error_free_dyn;
4ad4eba5
AM
4344 memcpy (anm, fnm, amt);
4345 n->name = anm;
4346 n->by = abfd;
4347 n->next = NULL;
4348 for (pn = & rpath;
4349 *pn != NULL;
4350 pn = &(*pn)->next)
4351 ;
4352 *pn = n;
4353 }
7ee314fa
AM
4354 if (dyn.d_tag == DT_AUDIT)
4355 {
4356 unsigned int tagv = dyn.d_un.d_val;
4357 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4358 }
4ad4eba5
AM
4359 }
4360
4361 free (dynbuf);
4362 }
4363
4364 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4365 frees all more recently bfd_alloc'd blocks as well. */
4366 if (runpath)
4367 rpath = runpath;
4368
4369 if (rpath)
4370 {
4371 struct bfd_link_needed_list **pn;
66eb6687 4372 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4373 ;
4374 *pn = rpath;
4375 }
4376
9acc85a6
AM
4377 /* If we have a PT_GNU_RELRO program header, mark as read-only
4378 all sections contained fully therein. This makes relro
4379 shared library sections appear as they will at run-time. */
4380 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
54025d58 4381 while (phdr-- > elf_tdata (abfd)->phdr)
9acc85a6
AM
4382 if (phdr->p_type == PT_GNU_RELRO)
4383 {
4384 for (s = abfd->sections; s != NULL; s = s->next)
502794d4
CE
4385 {
4386 unsigned int opb = bfd_octets_per_byte (abfd, s);
4387
4388 if ((s->flags & SEC_ALLOC) != 0
4389 && s->vma * opb >= phdr->p_vaddr
4390 && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4391 s->flags |= SEC_READONLY;
4392 }
9acc85a6
AM
4393 break;
4394 }
4395
4ad4eba5
AM
4396 /* We do not want to include any of the sections in a dynamic
4397 object in the output file. We hack by simply clobbering the
4398 list of sections in the BFD. This could be handled more
4399 cleanly by, say, a new section flag; the existing
4400 SEC_NEVER_LOAD flag is not the one we want, because that one
4401 still implies that the section takes up space in the output
4402 file. */
4403 bfd_section_list_clear (abfd);
4404
4ad4eba5
AM
4405 /* Find the name to use in a DT_NEEDED entry that refers to this
4406 object. If the object has a DT_SONAME entry, we use it.
4407 Otherwise, if the generic linker stuck something in
4408 elf_dt_name, we use that. Otherwise, we just use the file
4409 name. */
4410 if (soname == NULL || *soname == '\0')
4411 {
4412 soname = elf_dt_name (abfd);
4413 if (soname == NULL || *soname == '\0')
4414 soname = bfd_get_filename (abfd);
4415 }
4416
4417 /* Save the SONAME because sometimes the linker emulation code
4418 will need to know it. */
4419 elf_dt_name (abfd) = soname;
4420
4ad4eba5
AM
4421 /* If we have already included this dynamic object in the
4422 link, just ignore it. There is no reason to include a
4423 particular dynamic object more than once. */
e310298c
AM
4424 for (loaded_lib = htab->dyn_loaded;
4425 loaded_lib != NULL;
4426 loaded_lib = loaded_lib->next)
4427 {
4428 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4429 return TRUE;
4430 }
4431
4432 /* Create dynamic sections for backends that require that be done
4433 before setup_gnu_properties. */
4434 if (add_needed
4435 && !_bfd_elf_link_create_dynamic_sections (abfd, info))
4436 return FALSE;
7ee314fa
AM
4437
4438 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 4439 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
4440 }
4441
4442 /* If this is a dynamic object, we always link against the .dynsym
4443 symbol table, not the .symtab symbol table. The dynamic linker
4444 will only see the .dynsym symbol table, so there is no reason to
4445 look at .symtab for a dynamic object. */
4446
4447 if (! dynamic || elf_dynsymtab (abfd) == 0)
4448 hdr = &elf_tdata (abfd)->symtab_hdr;
4449 else
4450 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4451
4452 symcount = hdr->sh_size / bed->s->sizeof_sym;
4453
4454 /* The sh_info field of the symtab header tells us where the
4455 external symbols start. We don't care about the local symbols at
4456 this point. */
4457 if (elf_bad_symtab (abfd))
4458 {
4459 extsymcount = symcount;
4460 extsymoff = 0;
4461 }
4462 else
4463 {
4464 extsymcount = symcount - hdr->sh_info;
4465 extsymoff = hdr->sh_info;
4466 }
4467
f45794cb 4468 sym_hash = elf_sym_hashes (abfd);
012b2306 4469 if (extsymcount != 0)
4ad4eba5
AM
4470 {
4471 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4472 NULL, NULL, NULL);
4473 if (isymbuf == NULL)
4474 goto error_return;
4475
4ad4eba5 4476 if (sym_hash == NULL)
012b2306
AM
4477 {
4478 /* We store a pointer to the hash table entry for each
4479 external symbol. */
986f0783 4480 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
012b2306
AM
4481 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4482 if (sym_hash == NULL)
4483 goto error_free_sym;
4484 elf_sym_hashes (abfd) = sym_hash;
4485 }
4ad4eba5
AM
4486 }
4487
4488 if (dynamic)
4489 {
4490 /* Read in any version definitions. */
fc0e6df6
PB
4491 if (!_bfd_elf_slurp_version_tables (abfd,
4492 info->default_imported_symver))
4ad4eba5
AM
4493 goto error_free_sym;
4494
4495 /* Read in the symbol versions, but don't bother to convert them
4496 to internal format. */
4497 if (elf_dynversym (abfd) != 0)
4498 {
986f0783
AM
4499 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4500 bfd_size_type amt = versymhdr->sh_size;
4ad4eba5 4501
2bb3687b
AM
4502 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4503 goto error_free_sym;
4504 extversym = (Elf_External_Versym *)
4505 _bfd_malloc_and_read (abfd, amt, amt);
4ad4eba5
AM
4506 if (extversym == NULL)
4507 goto error_free_sym;
986f0783 4508 extversym_end = extversym + amt / sizeof (*extversym);
4ad4eba5
AM
4509 }
4510 }
4511
66eb6687
AM
4512 /* If we are loading an as-needed shared lib, save the symbol table
4513 state before we start adding symbols. If the lib turns out
4514 to be unneeded, restore the state. */
4515 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4516 {
4517 unsigned int i;
4518 size_t entsize;
4519
4520 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4521 {
4522 struct bfd_hash_entry *p;
2de92251 4523 struct elf_link_hash_entry *h;
66eb6687
AM
4524
4525 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
4526 {
4527 h = (struct elf_link_hash_entry *) p;
4528 entsize += htab->root.table.entsize;
4529 if (h->root.type == bfd_link_hash_warning)
7ba11550
AM
4530 {
4531 entsize += htab->root.table.entsize;
4532 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4533 }
4534 if (h->root.type == bfd_link_hash_common)
4535 entsize += sizeof (*h->root.u.c.p);
2de92251 4536 }
66eb6687
AM
4537 }
4538
4539 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 4540 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
4541 if (old_tab == NULL)
4542 goto error_free_vers;
4543
4544 /* Remember the current objalloc pointer, so that all mem for
4545 symbols added can later be reclaimed. */
4546 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4547 if (alloc_mark == NULL)
4548 goto error_free_vers;
4549
5061a885
AM
4550 /* Make a special call to the linker "notice" function to
4551 tell it that we are about to handle an as-needed lib. */
e5034e59 4552 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 4553 goto error_free_vers;
5061a885 4554
f45794cb
AM
4555 /* Clone the symbol table. Remember some pointers into the
4556 symbol table, and dynamic symbol count. */
4557 old_ent = (char *) old_tab + tabsize;
66eb6687 4558 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
4559 old_undefs = htab->root.undefs;
4560 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
4561 old_table = htab->root.table.table;
4562 old_size = htab->root.table.size;
4563 old_count = htab->root.table.count;
e310298c
AM
4564 old_strtab = NULL;
4565 if (htab->dynstr != NULL)
4566 {
4567 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4568 if (old_strtab == NULL)
4569 goto error_free_vers;
4570 }
66eb6687
AM
4571
4572 for (i = 0; i < htab->root.table.size; i++)
4573 {
4574 struct bfd_hash_entry *p;
2de92251 4575 struct elf_link_hash_entry *h;
66eb6687
AM
4576
4577 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4578 {
2de92251 4579 h = (struct elf_link_hash_entry *) p;
7ba11550
AM
4580 memcpy (old_ent, h, htab->root.table.entsize);
4581 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4582 if (h->root.type == bfd_link_hash_warning)
4583 {
7ba11550
AM
4584 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4585 memcpy (old_ent, h, htab->root.table.entsize);
2de92251
AM
4586 old_ent = (char *) old_ent + htab->root.table.entsize;
4587 }
7ba11550
AM
4588 if (h->root.type == bfd_link_hash_common)
4589 {
4590 memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
4591 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
4592 }
66eb6687
AM
4593 }
4594 }
4595 }
4ad4eba5 4596
66eb6687 4597 weaks = NULL;
be22c732
NC
4598 if (extversym == NULL)
4599 ever = NULL;
4600 else if (extversym + extsymoff < extversym_end)
4601 ever = extversym + extsymoff;
4602 else
4603 {
4604 /* xgettext:c-format */
4605 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4606 abfd, (long) extsymoff,
4607 (long) (extversym_end - extversym) / sizeof (* extversym));
4608 bfd_set_error (bfd_error_bad_value);
4609 goto error_free_vers;
4610 }
4611
b4c555cf
ML
4612 if (!bfd_link_relocatable (info)
4613 && abfd->lto_slim_object)
cc5277b1
ML
4614 {
4615 _bfd_error_handler
4616 (_("%pB: plugin needed to handle lto object"), abfd);
4617 }
4618
4ad4eba5
AM
4619 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4620 isym < isymend;
4621 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4622 {
4623 int bind;
4624 bfd_vma value;
af44c138 4625 asection *sec, *new_sec;
4ad4eba5
AM
4626 flagword flags;
4627 const char *name;
4628 struct elf_link_hash_entry *h;
90c984fc 4629 struct elf_link_hash_entry *hi;
4ad4eba5
AM
4630 bfd_boolean definition;
4631 bfd_boolean size_change_ok;
4632 bfd_boolean type_change_ok;
37a9e49a
L
4633 bfd_boolean new_weak;
4634 bfd_boolean old_weak;
7ba11550 4635 bfd *override;
a4d8e49b 4636 bfd_boolean common;
97196564 4637 bfd_boolean discarded;
4ad4eba5 4638 unsigned int old_alignment;
4538d1c7 4639 unsigned int shindex;
4ad4eba5 4640 bfd *old_bfd;
6e33951e 4641 bfd_boolean matched;
4ad4eba5 4642
7ba11550 4643 override = NULL;
4ad4eba5
AM
4644
4645 flags = BSF_NO_FLAGS;
4646 sec = NULL;
4647 value = isym->st_value;
a4d8e49b 4648 common = bed->common_definition (isym);
2980ccad
L
4649 if (common && info->inhibit_common_definition)
4650 {
4651 /* Treat common symbol as undefined for --no-define-common. */
4652 isym->st_shndx = SHN_UNDEF;
4653 common = FALSE;
4654 }
97196564 4655 discarded = FALSE;
4ad4eba5
AM
4656
4657 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 4658 switch (bind)
4ad4eba5 4659 {
3e7a7d11 4660 case STB_LOCAL:
4ad4eba5
AM
4661 /* This should be impossible, since ELF requires that all
4662 global symbols follow all local symbols, and that sh_info
4663 point to the first global symbol. Unfortunately, Irix 5
4664 screws this up. */
fe3fef62
AM
4665 if (elf_bad_symtab (abfd))
4666 continue;
4667
4668 /* If we aren't prepared to handle locals within the globals
4538d1c7
AM
4669 then we'll likely segfault on a NULL symbol hash if the
4670 symbol is ever referenced in relocations. */
4671 shindex = elf_elfheader (abfd)->e_shstrndx;
4672 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4673 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4674 " (>= sh_info of %lu)"),
4675 abfd, name, (long) (isym - isymbuf + extsymoff),
4676 (long) extsymoff);
4677
4678 /* Dynamic object relocations are not processed by ld, so
4679 ld won't run into the problem mentioned above. */
4680 if (dynamic)
4681 continue;
fe3fef62
AM
4682 bfd_set_error (bfd_error_bad_value);
4683 goto error_free_vers;
3e7a7d11
NC
4684
4685 case STB_GLOBAL:
a4d8e49b 4686 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4687 flags = BSF_GLOBAL;
3e7a7d11
NC
4688 break;
4689
4690 case STB_WEAK:
4691 flags = BSF_WEAK;
4692 break;
4693
4694 case STB_GNU_UNIQUE:
4695 flags = BSF_GNU_UNIQUE;
4696 break;
4697
4698 default:
4ad4eba5 4699 /* Leave it up to the processor backend. */
3e7a7d11 4700 break;
4ad4eba5
AM
4701 }
4702
4703 if (isym->st_shndx == SHN_UNDEF)
4704 sec = bfd_und_section_ptr;
cb33740c
AM
4705 else if (isym->st_shndx == SHN_ABS)
4706 sec = bfd_abs_section_ptr;
4707 else if (isym->st_shndx == SHN_COMMON)
4708 {
4709 sec = bfd_com_section_ptr;
4710 /* What ELF calls the size we call the value. What ELF
4711 calls the value we call the alignment. */
4712 value = isym->st_size;
4713 }
4714 else
4ad4eba5
AM
4715 {
4716 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4717 if (sec == NULL)
4718 sec = bfd_abs_section_ptr;
dbaa2011 4719 else if (discarded_section (sec))
529fcb95 4720 {
e5d08002
L
4721 /* Symbols from discarded section are undefined. We keep
4722 its visibility. */
529fcb95 4723 sec = bfd_und_section_ptr;
97196564 4724 discarded = TRUE;
529fcb95
PB
4725 isym->st_shndx = SHN_UNDEF;
4726 }
4ad4eba5
AM
4727 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4728 value -= sec->vma;
4729 }
4ad4eba5
AM
4730
4731 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4732 isym->st_name);
4733 if (name == NULL)
4734 goto error_free_vers;
4735
4736 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4737 && (abfd->flags & BFD_PLUGIN) != 0)
4738 {
4739 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4740
4741 if (xc == NULL)
4742 {
4743 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4744 | SEC_EXCLUDE);
4745 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4746 if (xc == NULL)
4747 goto error_free_vers;
4748 }
4749 sec = xc;
4750 }
4751 else if (isym->st_shndx == SHN_COMMON
4752 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4753 && !bfd_link_relocatable (info))
4ad4eba5
AM
4754 {
4755 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4756
4757 if (tcomm == NULL)
4758 {
02d00247
AM
4759 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4760 | SEC_LINKER_CREATED);
4761 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4762 if (tcomm == NULL)
4ad4eba5
AM
4763 goto error_free_vers;
4764 }
4765 sec = tcomm;
4766 }
66eb6687 4767 else if (bed->elf_add_symbol_hook)
4ad4eba5 4768 {
66eb6687
AM
4769 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4770 &sec, &value))
4ad4eba5
AM
4771 goto error_free_vers;
4772
4773 /* The hook function sets the name to NULL if this symbol
4774 should be skipped for some reason. */
4775 if (name == NULL)
4776 continue;
4777 }
4778
4779 /* Sanity check that all possibilities were handled. */
4780 if (sec == NULL)
4538d1c7 4781 abort ();
4ad4eba5 4782
191c0c42
AM
4783 /* Silently discard TLS symbols from --just-syms. There's
4784 no way to combine a static TLS block with a new TLS block
4785 for this executable. */
4786 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4787 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4788 continue;
4789
4ad4eba5
AM
4790 if (bfd_is_und_section (sec)
4791 || bfd_is_com_section (sec))
4792 definition = FALSE;
4793 else
4794 definition = TRUE;
4795
4796 size_change_ok = FALSE;
66eb6687 4797 type_change_ok = bed->type_change_ok;
37a9e49a 4798 old_weak = FALSE;
6e33951e 4799 matched = FALSE;
4ad4eba5
AM
4800 old_alignment = 0;
4801 old_bfd = NULL;
af44c138 4802 new_sec = sec;
4ad4eba5 4803
66eb6687 4804 if (is_elf_hash_table (htab))
4ad4eba5
AM
4805 {
4806 Elf_Internal_Versym iver;
4807 unsigned int vernum = 0;
4808 bfd_boolean skip;
4809
fc0e6df6 4810 if (ever == NULL)
4ad4eba5 4811 {
fc0e6df6
PB
4812 if (info->default_imported_symver)
4813 /* Use the default symbol version created earlier. */
4814 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4815 else
4816 iver.vs_vers = 0;
4817 }
be22c732
NC
4818 else if (ever >= extversym_end)
4819 {
4820 /* xgettext:c-format */
4821 _bfd_error_handler (_("%pB: not enough version information"),
4822 abfd);
4823 bfd_set_error (bfd_error_bad_value);
4824 goto error_free_vers;
4825 }
fc0e6df6
PB
4826 else
4827 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4828
4829 vernum = iver.vs_vers & VERSYM_VERSION;
4830
4831 /* If this is a hidden symbol, or if it is not version
4832 1, we append the version name to the symbol name.
cc86ff91
EB
4833 However, we do not modify a non-hidden absolute symbol
4834 if it is not a function, because it might be the version
4835 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4836 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4837 || (vernum > 1
4838 && (!bfd_is_abs_section (sec)
4839 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4840 {
4841 const char *verstr;
4842 size_t namelen, verlen, newlen;
4843 char *newname, *p;
4844
4845 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4846 {
fc0e6df6
PB
4847 if (vernum > elf_tdata (abfd)->cverdefs)
4848 verstr = NULL;
4849 else if (vernum > 1)
4850 verstr =
4851 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4852 else
4853 verstr = "";
4ad4eba5 4854
fc0e6df6 4855 if (verstr == NULL)
4ad4eba5 4856 {
4eca0228 4857 _bfd_error_handler
695344c0 4858 /* xgettext:c-format */
871b3ab2 4859 (_("%pB: %s: invalid version %u (max %d)"),
fc0e6df6
PB
4860 abfd, name, vernum,
4861 elf_tdata (abfd)->cverdefs);
4862 bfd_set_error (bfd_error_bad_value);
4863 goto error_free_vers;
4ad4eba5 4864 }
fc0e6df6
PB
4865 }
4866 else
4867 {
4868 /* We cannot simply test for the number of
4869 entries in the VERNEED section since the
4870 numbers for the needed versions do not start
4871 at 0. */
4872 Elf_Internal_Verneed *t;
4873
4874 verstr = NULL;
4875 for (t = elf_tdata (abfd)->verref;
4876 t != NULL;
4877 t = t->vn_nextref)
4ad4eba5 4878 {
fc0e6df6 4879 Elf_Internal_Vernaux *a;
4ad4eba5 4880
fc0e6df6
PB
4881 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4882 {
4883 if (a->vna_other == vernum)
4ad4eba5 4884 {
fc0e6df6
PB
4885 verstr = a->vna_nodename;
4886 break;
4ad4eba5 4887 }
4ad4eba5 4888 }
fc0e6df6
PB
4889 if (a != NULL)
4890 break;
4891 }
4892 if (verstr == NULL)
4893 {
4eca0228 4894 _bfd_error_handler
695344c0 4895 /* xgettext:c-format */
871b3ab2 4896 (_("%pB: %s: invalid needed version %d"),
fc0e6df6
PB
4897 abfd, name, vernum);
4898 bfd_set_error (bfd_error_bad_value);
4899 goto error_free_vers;
4ad4eba5 4900 }
4ad4eba5 4901 }
fc0e6df6
PB
4902
4903 namelen = strlen (name);
4904 verlen = strlen (verstr);
4905 newlen = namelen + verlen + 2;
4906 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4907 && isym->st_shndx != SHN_UNDEF)
4908 ++newlen;
4909
a50b1753 4910 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4911 if (newname == NULL)
4912 goto error_free_vers;
4913 memcpy (newname, name, namelen);
4914 p = newname + namelen;
4915 *p++ = ELF_VER_CHR;
4916 /* If this is a defined non-hidden version symbol,
4917 we add another @ to the name. This indicates the
4918 default version of the symbol. */
4919 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4920 && isym->st_shndx != SHN_UNDEF)
4921 *p++ = ELF_VER_CHR;
4922 memcpy (p, verstr, verlen + 1);
4923
4924 name = newname;
4ad4eba5
AM
4925 }
4926
cd3416da
AM
4927 /* If this symbol has default visibility and the user has
4928 requested we not re-export it, then mark it as hidden. */
a0d49154 4929 if (!bfd_is_und_section (sec)
cd3416da 4930 && !dynamic
ce875075 4931 && abfd->no_export
cd3416da
AM
4932 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4933 isym->st_other = (STV_HIDDEN
4934 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4935
4f3fedcf
AM
4936 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4937 sym_hash, &old_bfd, &old_weak,
4938 &old_alignment, &skip, &override,
6e33951e
L
4939 &type_change_ok, &size_change_ok,
4940 &matched))
4ad4eba5
AM
4941 goto error_free_vers;
4942
4943 if (skip)
4944 continue;
4945
6e33951e
L
4946 /* Override a definition only if the new symbol matches the
4947 existing one. */
4948 if (override && matched)
4ad4eba5
AM
4949 definition = FALSE;
4950
4951 h = *sym_hash;
4952 while (h->root.type == bfd_link_hash_indirect
4953 || h->root.type == bfd_link_hash_warning)
4954 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4955
4ad4eba5 4956 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4957 && vernum > 1
4958 && definition)
4959 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4960 }
4961
4962 if (! (_bfd_generic_link_add_one_symbol
7ba11550
AM
4963 (info, override ? override : abfd, name, flags, sec, value,
4964 NULL, FALSE, bed->collect,
4ad4eba5
AM
4965 (struct bfd_link_hash_entry **) sym_hash)))
4966 goto error_free_vers;
4967
4968 h = *sym_hash;
90c984fc
L
4969 /* We need to make sure that indirect symbol dynamic flags are
4970 updated. */
4971 hi = h;
4ad4eba5
AM
4972 while (h->root.type == bfd_link_hash_indirect
4973 || h->root.type == bfd_link_hash_warning)
4974 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4975
97196564
L
4976 /* Setting the index to -3 tells elf_link_output_extsym that
4977 this symbol is defined in a discarded section. */
4978 if (discarded)
4979 h->indx = -3;
4980
4ad4eba5
AM
4981 *sym_hash = h;
4982
37a9e49a 4983 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4984 if (dynamic
4985 && definition
37a9e49a 4986 && new_weak
fcb93ecf 4987 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4988 && is_elf_hash_table (htab)
60d67dc8 4989 && h->u.alias == NULL)
4ad4eba5
AM
4990 {
4991 /* Keep a list of all weak defined non function symbols from
60d67dc8
AM
4992 a dynamic object, using the alias field. Later in this
4993 function we will set the alias field to the correct
4ad4eba5
AM
4994 value. We only put non-function symbols from dynamic
4995 objects on this list, because that happens to be the only
4996 time we need to know the normal symbol corresponding to a
4997 weak symbol, and the information is time consuming to
60d67dc8 4998 figure out. If the alias field is not already NULL,
4ad4eba5
AM
4999 then this symbol was already defined by some previous
5000 dynamic object, and we will be using that previous
5001 definition anyhow. */
5002
60d67dc8 5003 h->u.alias = weaks;
4ad4eba5 5004 weaks = h;
4ad4eba5
AM
5005 }
5006
5007 /* Set the alignment of a common symbol. */
a4d8e49b 5008 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
5009 && h->root.type == bfd_link_hash_common)
5010 {
5011 unsigned int align;
5012
a4d8e49b 5013 if (common)
af44c138
L
5014 align = bfd_log2 (isym->st_value);
5015 else
5016 {
5017 /* The new symbol is a common symbol in a shared object.
5018 We need to get the alignment from the section. */
5019 align = new_sec->alignment_power;
5020 }
595213d4 5021 if (align > old_alignment)
4ad4eba5
AM
5022 h->root.u.c.p->alignment_power = align;
5023 else
5024 h->root.u.c.p->alignment_power = old_alignment;
5025 }
5026
66eb6687 5027 if (is_elf_hash_table (htab))
4ad4eba5 5028 {
4f3fedcf
AM
5029 /* Set a flag in the hash table entry indicating the type of
5030 reference or definition we just found. A dynamic symbol
5031 is one which is referenced or defined by both a regular
5032 object and a shared object. */
5033 bfd_boolean dynsym = FALSE;
5034
b1a92c63
AM
5035 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5036 if ((abfd->flags & BFD_PLUGIN) != 0)
5037 ;
5038 else if (!dynamic)
4f3fedcf
AM
5039 {
5040 if (! definition)
5041 {
5042 h->ref_regular = 1;
5043 if (bind != STB_WEAK)
5044 h->ref_regular_nonweak = 1;
5045 }
5046 else
5047 {
5048 h->def_regular = 1;
5049 if (h->def_dynamic)
5050 {
5051 h->def_dynamic = 0;
5052 h->ref_dynamic = 1;
5053 }
5054 }
4f3fedcf
AM
5055 }
5056 else
5057 {
5058 if (! definition)
5059 {
5060 h->ref_dynamic = 1;
5061 hi->ref_dynamic = 1;
5062 }
5063 else
5064 {
5065 h->def_dynamic = 1;
5066 hi->def_dynamic = 1;
5067 }
b1a92c63 5068 }
4f3fedcf 5069
b1a92c63
AM
5070 /* If an indirect symbol has been forced local, don't
5071 make the real symbol dynamic. */
5072 if (h != hi && hi->forced_local)
5073 ;
5074 else if (!dynamic)
5075 {
5076 if (bfd_link_dll (info)
5077 || h->def_dynamic
5078 || h->ref_dynamic)
5079 dynsym = TRUE;
5080 }
5081 else
5082 {
5083 if (h->def_regular
5084 || h->ref_regular
5085 || (h->is_weakalias
5086 && weakdef (h)->dynindx != -1))
4f3fedcf
AM
5087 dynsym = TRUE;
5088 }
5089
5090 /* Check to see if we need to add an indirect symbol for
5091 the default name. */
726d7d1e
AM
5092 if ((definition
5093 || (!override && h->root.type == bfd_link_hash_common))
5094 && !(hi != h
5095 && hi->versioned == versioned_hidden))
4f3fedcf 5096 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
9918bff7
AM
5097 sec, value, &old_bfd, &dynsym,
5098 &override))
4f3fedcf 5099 goto error_free_vers;
4ad4eba5
AM
5100
5101 /* Check the alignment when a common symbol is involved. This
5102 can change when a common symbol is overridden by a normal
5103 definition or a common symbol is ignored due to the old
5104 normal definition. We need to make sure the maximum
5105 alignment is maintained. */
a4d8e49b 5106 if ((old_alignment || common)
4ad4eba5
AM
5107 && h->root.type != bfd_link_hash_common)
5108 {
5109 unsigned int common_align;
5110 unsigned int normal_align;
5111 unsigned int symbol_align;
5112 bfd *normal_bfd;
5113 bfd *common_bfd;
5114
3a81e825
AM
5115 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5116 || h->root.type == bfd_link_hash_defweak);
5117
4ad4eba5
AM
5118 symbol_align = ffs (h->root.u.def.value) - 1;
5119 if (h->root.u.def.section->owner != NULL
0616a280
AM
5120 && (h->root.u.def.section->owner->flags
5121 & (DYNAMIC | BFD_PLUGIN)) == 0)
4ad4eba5
AM
5122 {
5123 normal_align = h->root.u.def.section->alignment_power;
5124 if (normal_align > symbol_align)
5125 normal_align = symbol_align;
5126 }
5127 else
5128 normal_align = symbol_align;
5129
5130 if (old_alignment)
5131 {
5132 common_align = old_alignment;
5133 common_bfd = old_bfd;
5134 normal_bfd = abfd;
5135 }
5136 else
5137 {
5138 common_align = bfd_log2 (isym->st_value);
5139 common_bfd = abfd;
5140 normal_bfd = old_bfd;
5141 }
5142
5143 if (normal_align < common_align)
d07676f8
NC
5144 {
5145 /* PR binutils/2735 */
5146 if (normal_bfd == NULL)
4eca0228 5147 _bfd_error_handler
695344c0 5148 /* xgettext:c-format */
9793eb77 5149 (_("warning: alignment %u of common symbol `%s' in %pB is"
871b3ab2 5150 " greater than the alignment (%u) of its section %pA"),
c08bb8dd
AM
5151 1 << common_align, name, common_bfd,
5152 1 << normal_align, h->root.u.def.section);
d07676f8 5153 else
4eca0228 5154 _bfd_error_handler
695344c0 5155 /* xgettext:c-format */
9793eb77 5156 (_("warning: alignment %u of symbol `%s' in %pB"
871b3ab2 5157 " is smaller than %u in %pB"),
c08bb8dd
AM
5158 1 << normal_align, name, normal_bfd,
5159 1 << common_align, common_bfd);
d07676f8 5160 }
4ad4eba5
AM
5161 }
5162
83ad0046 5163 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
5164 if (isym->st_size != 0
5165 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
5166 && (definition || h->size == 0))
5167 {
83ad0046
L
5168 if (h->size != 0
5169 && h->size != isym->st_size
5170 && ! size_change_ok)
4eca0228 5171 _bfd_error_handler
695344c0 5172 /* xgettext:c-format */
9793eb77 5173 (_("warning: size of symbol `%s' changed"
2dcf00ce
AM
5174 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5175 name, (uint64_t) h->size, old_bfd,
5176 (uint64_t) isym->st_size, abfd);
4ad4eba5
AM
5177
5178 h->size = isym->st_size;
5179 }
5180
5181 /* If this is a common symbol, then we always want H->SIZE
5182 to be the size of the common symbol. The code just above
5183 won't fix the size if a common symbol becomes larger. We
5184 don't warn about a size change here, because that is
4f3fedcf 5185 covered by --warn-common. Allow changes between different
fcb93ecf 5186 function types. */
4ad4eba5
AM
5187 if (h->root.type == bfd_link_hash_common)
5188 h->size = h->root.u.c.size;
5189
5190 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
5191 && ((definition && !new_weak)
5192 || (old_weak && h->root.type == bfd_link_hash_common)
5193 || h->type == STT_NOTYPE))
4ad4eba5 5194 {
2955ec4c
L
5195 unsigned int type = ELF_ST_TYPE (isym->st_info);
5196
5197 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5198 symbol. */
5199 if (type == STT_GNU_IFUNC
5200 && (abfd->flags & DYNAMIC) != 0)
5201 type = STT_FUNC;
4ad4eba5 5202
2955ec4c
L
5203 if (h->type != type)
5204 {
5205 if (h->type != STT_NOTYPE && ! type_change_ok)
695344c0 5206 /* xgettext:c-format */
4eca0228 5207 _bfd_error_handler
9793eb77 5208 (_("warning: type of symbol `%s' changed"
871b3ab2 5209 " from %d to %d in %pB"),
c08bb8dd 5210 name, h->type, type, abfd);
2955ec4c
L
5211
5212 h->type = type;
5213 }
4ad4eba5
AM
5214 }
5215
54ac0771 5216 /* Merge st_other field. */
5160d0f3
AM
5217 elf_merge_st_other (abfd, h, isym->st_other, sec,
5218 definition, dynamic);
4ad4eba5 5219
c3df8c14 5220 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
5221 if (definition
5222 && (sec->flags & SEC_DEBUGGING)
5223 && !bfd_link_relocatable (info))
c3df8c14
AM
5224 dynsym = FALSE;
5225
b1a92c63
AM
5226 /* Nor should we make plugin symbols dynamic. */
5227 if ((abfd->flags & BFD_PLUGIN) != 0)
5228 dynsym = FALSE;
5229
35fc36a8 5230 if (definition)
35399224
L
5231 {
5232 h->target_internal = isym->st_target_internal;
5233 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5234 }
35fc36a8 5235
4ad4eba5
AM
5236 if (definition && !dynamic)
5237 {
5238 char *p = strchr (name, ELF_VER_CHR);
5239 if (p != NULL && p[1] != ELF_VER_CHR)
5240 {
5241 /* Queue non-default versions so that .symver x, x@FOO
5242 aliases can be checked. */
66eb6687 5243 if (!nondeflt_vers)
4ad4eba5 5244 {
986f0783
AM
5245 size_t amt = ((isymend - isym + 1)
5246 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
5247 nondeflt_vers
5248 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
5249 if (!nondeflt_vers)
5250 goto error_free_vers;
4ad4eba5 5251 }
66eb6687 5252 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
5253 }
5254 }
5255
b1a92c63 5256 if (dynsym && h->dynindx == -1)
4ad4eba5 5257 {
c152c796 5258 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 5259 goto error_free_vers;
60d67dc8
AM
5260 if (h->is_weakalias
5261 && weakdef (h)->dynindx == -1)
4ad4eba5 5262 {
60d67dc8 5263 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
4ad4eba5
AM
5264 goto error_free_vers;
5265 }
5266 }
1f599d0e 5267 else if (h->dynindx != -1)
4ad4eba5
AM
5268 /* If the symbol already has a dynamic index, but
5269 visibility says it should not be visible, turn it into
5270 a local symbol. */
5271 switch (ELF_ST_VISIBILITY (h->other))
5272 {
5273 case STV_INTERNAL:
5274 case STV_HIDDEN:
5275 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5276 dynsym = FALSE;
5277 break;
5278 }
5279
5280 if (!add_needed
9918bff7 5281 && !override
aef28989 5282 && matched
4ad4eba5 5283 && definition
010e5ae2 5284 && ((dynsym
a896df97 5285 && h->ref_regular_nonweak)
b1a92c63
AM
5286 || (old_bfd != NULL
5287 && (old_bfd->flags & BFD_PLUGIN) != 0
5288 && bind != STB_WEAK)
ffa9430d 5289 || (h->ref_dynamic_nonweak
010e5ae2 5290 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
7b15fa7a
AM
5291 && !on_needed_list (elf_dt_name (abfd),
5292 htab->needed, NULL))))
4ad4eba5 5293 {
4ad4eba5
AM
5294 const char *soname = elf_dt_name (abfd);
5295
16e4ecc0
AM
5296 info->callbacks->minfo ("%!", soname, old_bfd,
5297 h->root.root.string);
5298
4ad4eba5
AM
5299 /* A symbol from a library loaded via DT_NEEDED of some
5300 other library is referenced by a regular object.
e56f61be 5301 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
5302 --no-add-needed is used and the reference was not
5303 a weak one. */
4f3fedcf 5304 if (old_bfd != NULL
b918acf9 5305 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be 5306 {
4eca0228 5307 _bfd_error_handler
695344c0 5308 /* xgettext:c-format */
871b3ab2 5309 (_("%pB: undefined reference to symbol '%s'"),
4f3fedcf 5310 old_bfd, name);
ff5ac77b 5311 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
5312 goto error_free_vers;
5313 }
5314
a50b1753 5315 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 5316 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 5317
e310298c
AM
5318 /* Create dynamic sections for backends that require
5319 that be done before setup_gnu_properties. */
5320 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5321 return FALSE;
4ad4eba5 5322 add_needed = TRUE;
4ad4eba5
AM
5323 }
5324 }
5325 }
5326
a83ef4d1
L
5327 if (info->lto_plugin_active
5328 && !bfd_link_relocatable (info)
5329 && (abfd->flags & BFD_PLUGIN) == 0
5330 && !just_syms
5331 && extsymcount)
5332 {
5333 int r_sym_shift;
5334
5335 if (bed->s->arch_size == 32)
5336 r_sym_shift = 8;
5337 else
5338 r_sym_shift = 32;
5339
5340 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5341 referenced in regular objects so that linker plugin will get
5342 the correct symbol resolution. */
5343
5344 sym_hash = elf_sym_hashes (abfd);
5345 for (s = abfd->sections; s != NULL; s = s->next)
5346 {
5347 Elf_Internal_Rela *internal_relocs;
5348 Elf_Internal_Rela *rel, *relend;
5349
5350 /* Don't check relocations in excluded sections. */
5351 if ((s->flags & SEC_RELOC) == 0
5352 || s->reloc_count == 0
5353 || (s->flags & SEC_EXCLUDE) != 0
5354 || ((info->strip == strip_all
5355 || info->strip == strip_debugger)
5356 && (s->flags & SEC_DEBUGGING) != 0))
5357 continue;
5358
5359 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5360 NULL,
5361 info->keep_memory);
5362 if (internal_relocs == NULL)
5363 goto error_free_vers;
5364
5365 rel = internal_relocs;
5366 relend = rel + s->reloc_count;
5367 for ( ; rel < relend; rel++)
5368 {
5369 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5370 struct elf_link_hash_entry *h;
5371
5372 /* Skip local symbols. */
5373 if (r_symndx < extsymoff)
5374 continue;
5375
5376 h = sym_hash[r_symndx - extsymoff];
5377 if (h != NULL)
5378 h->root.non_ir_ref_regular = 1;
5379 }
5380
5381 if (elf_section_data (s)->relocs != internal_relocs)
5382 free (internal_relocs);
5383 }
5384 }
5385
c9594989
AM
5386 free (extversym);
5387 extversym = NULL;
5388 free (isymbuf);
5389 isymbuf = NULL;
66eb6687
AM
5390
5391 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5392 {
5393 unsigned int i;
5394
5395 /* Restore the symbol table. */
f45794cb
AM
5396 old_ent = (char *) old_tab + tabsize;
5397 memset (elf_sym_hashes (abfd), 0,
5398 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
5399 htab->root.table.table = old_table;
5400 htab->root.table.size = old_size;
5401 htab->root.table.count = old_count;
66eb6687 5402 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
5403 htab->root.undefs = old_undefs;
5404 htab->root.undefs_tail = old_undefs_tail;
e310298c
AM
5405 if (htab->dynstr != NULL)
5406 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5b677558
AM
5407 free (old_strtab);
5408 old_strtab = NULL;
66eb6687
AM
5409 for (i = 0; i < htab->root.table.size; i++)
5410 {
5411 struct bfd_hash_entry *p;
5412 struct elf_link_hash_entry *h;
4070765b 5413 unsigned int non_ir_ref_dynamic;
66eb6687
AM
5414
5415 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5416 {
4070765b 5417 /* Preserve non_ir_ref_dynamic so that this symbol
59fa66c5
L
5418 will be exported when the dynamic lib becomes needed
5419 in the second pass. */
7ba11550
AM
5420 h = (struct elf_link_hash_entry *) p;
5421 if (h->root.type == bfd_link_hash_warning)
5422 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4070765b 5423 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
7ba11550 5424
2de92251 5425 h = (struct elf_link_hash_entry *) p;
7ba11550
AM
5426 memcpy (h, old_ent, htab->root.table.entsize);
5427 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
5428 if (h->root.type == bfd_link_hash_warning)
5429 {
a4542f1b 5430 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7ba11550
AM
5431 memcpy (h, old_ent, htab->root.table.entsize);
5432 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251 5433 }
a4542f1b 5434 if (h->root.type == bfd_link_hash_common)
3e0882af 5435 {
7ba11550
AM
5436 memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5437 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
3e0882af 5438 }
4070765b 5439 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
66eb6687
AM
5440 }
5441 }
5442
5061a885
AM
5443 /* Make a special call to the linker "notice" function to
5444 tell it that symbols added for crefs may need to be removed. */
e5034e59 5445 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 5446 goto error_free_vers;
5061a885 5447
66eb6687
AM
5448 free (old_tab);
5449 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5450 alloc_mark);
c9594989 5451 free (nondeflt_vers);
66eb6687
AM
5452 return TRUE;
5453 }
2de92251 5454
66eb6687
AM
5455 if (old_tab != NULL)
5456 {
e5034e59 5457 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 5458 goto error_free_vers;
66eb6687
AM
5459 free (old_tab);
5460 old_tab = NULL;
5461 }
5462
c6e8a9a8
L
5463 /* Now that all the symbols from this input file are created, if
5464 not performing a relocatable link, handle .symver foo, foo@BAR
5465 such that any relocs against foo become foo@BAR. */
0e1862bb 5466 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5 5467 {
ef53be89 5468 size_t cnt, symidx;
4ad4eba5
AM
5469
5470 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5471 {
5472 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5473 char *shortname, *p;
986f0783 5474 size_t amt;
4ad4eba5
AM
5475
5476 p = strchr (h->root.root.string, ELF_VER_CHR);
5477 if (p == NULL
5478 || (h->root.type != bfd_link_hash_defined
5479 && h->root.type != bfd_link_hash_defweak))
5480 continue;
5481
5482 amt = p - h->root.root.string;
a50b1753 5483 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
5484 if (!shortname)
5485 goto error_free_vers;
4ad4eba5
AM
5486 memcpy (shortname, h->root.root.string, amt);
5487 shortname[amt] = '\0';
5488
5489 hi = (struct elf_link_hash_entry *)
66eb6687 5490 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
5491 FALSE, FALSE, FALSE);
5492 if (hi != NULL
5493 && hi->root.type == h->root.type
5494 && hi->root.u.def.value == h->root.u.def.value
5495 && hi->root.u.def.section == h->root.u.def.section)
5496 {
5497 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5498 hi->root.type = bfd_link_hash_indirect;
5499 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 5500 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
5501 sym_hash = elf_sym_hashes (abfd);
5502 if (sym_hash)
5503 for (symidx = 0; symidx < extsymcount; ++symidx)
5504 if (sym_hash[symidx] == hi)
5505 {
5506 sym_hash[symidx] = h;
5507 break;
5508 }
5509 }
5510 free (shortname);
5511 }
5512 free (nondeflt_vers);
5513 nondeflt_vers = NULL;
5514 }
5515
60d67dc8 5516 /* Now set the alias field correctly for all the weak defined
4ad4eba5
AM
5517 symbols we found. The only way to do this is to search all the
5518 symbols. Since we only need the information for non functions in
5519 dynamic objects, that's the only time we actually put anything on
5520 the list WEAKS. We need this information so that if a regular
5521 object refers to a symbol defined weakly in a dynamic object, the
5522 real symbol in the dynamic object is also put in the dynamic
5523 symbols; we also must arrange for both symbols to point to the
5524 same memory location. We could handle the general case of symbol
5525 aliasing, but a general symbol alias can only be generated in
5526 assembler code, handling it correctly would be very time
5527 consuming, and other ELF linkers don't handle general aliasing
5528 either. */
5529 if (weaks != NULL)
5530 {
5531 struct elf_link_hash_entry **hpp;
5532 struct elf_link_hash_entry **hppend;
5533 struct elf_link_hash_entry **sorted_sym_hash;
5534 struct elf_link_hash_entry *h;
986f0783 5535 size_t sym_count, amt;
4ad4eba5
AM
5536
5537 /* Since we have to search the whole symbol list for each weak
5538 defined symbol, search time for N weak defined symbols will be
5539 O(N^2). Binary search will cut it down to O(NlogN). */
986f0783 5540 amt = extsymcount * sizeof (*sorted_sym_hash);
3a3f4bf7 5541 sorted_sym_hash = bfd_malloc (amt);
4ad4eba5
AM
5542 if (sorted_sym_hash == NULL)
5543 goto error_return;
5544 sym_hash = sorted_sym_hash;
5545 hpp = elf_sym_hashes (abfd);
5546 hppend = hpp + extsymcount;
5547 sym_count = 0;
5548 for (; hpp < hppend; hpp++)
5549 {
5550 h = *hpp;
5551 if (h != NULL
5552 && h->root.type == bfd_link_hash_defined
fcb93ecf 5553 && !bed->is_function_type (h->type))
4ad4eba5
AM
5554 {
5555 *sym_hash = h;
5556 sym_hash++;
5557 sym_count++;
5558 }
5559 }
5560
3a3f4bf7 5561 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
4ad4eba5
AM
5562 elf_sort_symbol);
5563
5564 while (weaks != NULL)
5565 {
5566 struct elf_link_hash_entry *hlook;
5567 asection *slook;
5568 bfd_vma vlook;
ed54588d 5569 size_t i, j, idx = 0;
4ad4eba5
AM
5570
5571 hlook = weaks;
60d67dc8
AM
5572 weaks = hlook->u.alias;
5573 hlook->u.alias = NULL;
4ad4eba5 5574
e3e53eed
AM
5575 if (hlook->root.type != bfd_link_hash_defined
5576 && hlook->root.type != bfd_link_hash_defweak)
5577 continue;
5578
4ad4eba5
AM
5579 slook = hlook->root.u.def.section;
5580 vlook = hlook->root.u.def.value;
5581
4ad4eba5
AM
5582 i = 0;
5583 j = sym_count;
14160578 5584 while (i != j)
4ad4eba5
AM
5585 {
5586 bfd_signed_vma vdiff;
5587 idx = (i + j) / 2;
14160578 5588 h = sorted_sym_hash[idx];
4ad4eba5
AM
5589 vdiff = vlook - h->root.u.def.value;
5590 if (vdiff < 0)
5591 j = idx;
5592 else if (vdiff > 0)
5593 i = idx + 1;
5594 else
5595 {
d3435ae8 5596 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
5597 if (sdiff < 0)
5598 j = idx;
5599 else if (sdiff > 0)
5600 i = idx + 1;
5601 else
14160578 5602 break;
4ad4eba5
AM
5603 }
5604 }
5605
5606 /* We didn't find a value/section match. */
14160578 5607 if (i == j)
4ad4eba5
AM
5608 continue;
5609
14160578
AM
5610 /* With multiple aliases, or when the weak symbol is already
5611 strongly defined, we have multiple matching symbols and
5612 the binary search above may land on any of them. Step
5613 one past the matching symbol(s). */
5614 while (++idx != j)
5615 {
5616 h = sorted_sym_hash[idx];
5617 if (h->root.u.def.section != slook
5618 || h->root.u.def.value != vlook)
5619 break;
5620 }
5621
5622 /* Now look back over the aliases. Since we sorted by size
5623 as well as value and section, we'll choose the one with
5624 the largest size. */
5625 while (idx-- != i)
4ad4eba5 5626 {
14160578 5627 h = sorted_sym_hash[idx];
4ad4eba5
AM
5628
5629 /* Stop if value or section doesn't match. */
14160578
AM
5630 if (h->root.u.def.section != slook
5631 || h->root.u.def.value != vlook)
4ad4eba5
AM
5632 break;
5633 else if (h != hlook)
5634 {
60d67dc8
AM
5635 struct elf_link_hash_entry *t;
5636
5637 hlook->u.alias = h;
5638 hlook->is_weakalias = 1;
5639 t = h;
5640 if (t->u.alias != NULL)
5641 while (t->u.alias != h)
5642 t = t->u.alias;
5643 t->u.alias = hlook;
4ad4eba5
AM
5644
5645 /* If the weak definition is in the list of dynamic
5646 symbols, make sure the real definition is put
5647 there as well. */
5648 if (hlook->dynindx != -1 && h->dynindx == -1)
5649 {
c152c796 5650 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
5651 {
5652 err_free_sym_hash:
5653 free (sorted_sym_hash);
5654 goto error_return;
5655 }
4ad4eba5
AM
5656 }
5657
5658 /* If the real definition is in the list of dynamic
5659 symbols, make sure the weak definition is put
5660 there as well. If we don't do this, then the
5661 dynamic loader might not merge the entries for the
5662 real definition and the weak definition. */
5663 if (h->dynindx != -1 && hlook->dynindx == -1)
5664 {
c152c796 5665 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 5666 goto err_free_sym_hash;
4ad4eba5
AM
5667 }
5668 break;
5669 }
5670 }
5671 }
5672
5673 free (sorted_sym_hash);
5674 }
5675
33177bb1
AM
5676 if (bed->check_directives
5677 && !(*bed->check_directives) (abfd, info))
5678 return FALSE;
85fbca6a 5679
4ad4eba5
AM
5680 /* If this is a non-traditional link, try to optimize the handling
5681 of the .stab/.stabstr sections. */
5682 if (! dynamic
5683 && ! info->traditional_format
66eb6687 5684 && is_elf_hash_table (htab)
4ad4eba5
AM
5685 && (info->strip != strip_all && info->strip != strip_debugger))
5686 {
5687 asection *stabstr;
5688
5689 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5690 if (stabstr != NULL)
5691 {
5692 bfd_size_type string_offset = 0;
5693 asection *stab;
5694
5695 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 5696 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
5697 && (!stab->name[5] ||
5698 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5699 && (stab->flags & SEC_MERGE) == 0
5700 && !bfd_is_abs_section (stab->output_section))
5701 {
5702 struct bfd_elf_section_data *secdata;
5703
5704 secdata = elf_section_data (stab);
66eb6687
AM
5705 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5706 stabstr, &secdata->sec_info,
4ad4eba5
AM
5707 &string_offset))
5708 goto error_return;
5709 if (secdata->sec_info)
dbaa2011 5710 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5711 }
5712 }
5713 }
5714
e310298c 5715 if (dynamic && add_needed)
4ad4eba5
AM
5716 {
5717 /* Add this bfd to the loaded list. */
5718 struct elf_link_loaded_list *n;
5719
ca4be51c 5720 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5721 if (n == NULL)
5722 goto error_return;
5723 n->abfd = abfd;
e310298c
AM
5724 n->next = htab->dyn_loaded;
5725 htab->dyn_loaded = n;
4ad4eba5 5726 }
e310298c
AM
5727 if (dynamic && !add_needed
5728 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
5729 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
4ad4eba5
AM
5730
5731 return TRUE;
5732
5733 error_free_vers:
c9594989
AM
5734 free (old_tab);
5735 free (old_strtab);
5736 free (nondeflt_vers);
5737 free (extversym);
4ad4eba5 5738 error_free_sym:
c9594989 5739 free (isymbuf);
4ad4eba5
AM
5740 error_return:
5741 return FALSE;
5742}
5743
8387904d
AM
5744/* Return the linker hash table entry of a symbol that might be
5745 satisfied by an archive symbol. Return -1 on error. */
5746
5747struct elf_link_hash_entry *
5748_bfd_elf_archive_symbol_lookup (bfd *abfd,
5749 struct bfd_link_info *info,
5750 const char *name)
5751{
5752 struct elf_link_hash_entry *h;
5753 char *p, *copy;
5754 size_t len, first;
5755
2a41f396 5756 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5757 if (h != NULL)
5758 return h;
5759
5760 /* If this is a default version (the name contains @@), look up the
5761 symbol again with only one `@' as well as without the version.
5762 The effect is that references to the symbol with and without the
5763 version will be matched by the default symbol in the archive. */
5764
5765 p = strchr (name, ELF_VER_CHR);
5766 if (p == NULL || p[1] != ELF_VER_CHR)
5767 return h;
5768
5769 /* First check with only one `@'. */
5770 len = strlen (name);
a50b1753 5771 copy = (char *) bfd_alloc (abfd, len);
8387904d 5772 if (copy == NULL)
e99955cd 5773 return (struct elf_link_hash_entry *) -1;
8387904d
AM
5774
5775 first = p - name + 1;
5776 memcpy (copy, name, first);
5777 memcpy (copy + first, name + first + 1, len - first);
5778
2a41f396 5779 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5780 if (h == NULL)
5781 {
5782 /* We also need to check references to the symbol without the
5783 version. */
5784 copy[first - 1] = '\0';
5785 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5786 FALSE, FALSE, TRUE);
8387904d
AM
5787 }
5788
5789 bfd_release (abfd, copy);
5790 return h;
5791}
5792
0ad989f9 5793/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5794 don't use _bfd_generic_link_add_archive_symbols because we need to
5795 handle versioned symbols.
0ad989f9
L
5796
5797 Fortunately, ELF archive handling is simpler than that done by
5798 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5799 oddities. In ELF, if we find a symbol in the archive map, and the
5800 symbol is currently undefined, we know that we must pull in that
5801 object file.
5802
5803 Unfortunately, we do have to make multiple passes over the symbol
5804 table until nothing further is resolved. */
5805
4ad4eba5
AM
5806static bfd_boolean
5807elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5808{
5809 symindex c;
13e570f8 5810 unsigned char *included = NULL;
0ad989f9
L
5811 carsym *symdefs;
5812 bfd_boolean loop;
986f0783 5813 size_t amt;
8387904d
AM
5814 const struct elf_backend_data *bed;
5815 struct elf_link_hash_entry * (*archive_symbol_lookup)
5816 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5817
5818 if (! bfd_has_map (abfd))
5819 {
5820 /* An empty archive is a special case. */
5821 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5822 return TRUE;
5823 bfd_set_error (bfd_error_no_armap);
5824 return FALSE;
5825 }
5826
5827 /* Keep track of all symbols we know to be already defined, and all
5828 files we know to be already included. This is to speed up the
5829 second and subsequent passes. */
5830 c = bfd_ardata (abfd)->symdef_count;
5831 if (c == 0)
5832 return TRUE;
986f0783 5833 amt = c * sizeof (*included);
13e570f8
AM
5834 included = (unsigned char *) bfd_zmalloc (amt);
5835 if (included == NULL)
5836 return FALSE;
0ad989f9
L
5837
5838 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5839 bed = get_elf_backend_data (abfd);
5840 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5841
5842 do
5843 {
5844 file_ptr last;
5845 symindex i;
5846 carsym *symdef;
5847 carsym *symdefend;
5848
5849 loop = FALSE;
5850 last = -1;
5851
5852 symdef = symdefs;
5853 symdefend = symdef + c;
5854 for (i = 0; symdef < symdefend; symdef++, i++)
5855 {
5856 struct elf_link_hash_entry *h;
5857 bfd *element;
5858 struct bfd_link_hash_entry *undefs_tail;
5859 symindex mark;
5860
13e570f8 5861 if (included[i])
0ad989f9
L
5862 continue;
5863 if (symdef->file_offset == last)
5864 {
5865 included[i] = TRUE;
5866 continue;
5867 }
5868
8387904d 5869 h = archive_symbol_lookup (abfd, info, symdef->name);
e99955cd 5870 if (h == (struct elf_link_hash_entry *) -1)
8387904d 5871 goto error_return;
0ad989f9
L
5872
5873 if (h == NULL)
5874 continue;
5875
75cfe082
AM
5876 if (h->root.type == bfd_link_hash_undefined)
5877 {
5878 /* If the archive element has already been loaded then one
5879 of the symbols defined by that element might have been
5880 made undefined due to being in a discarded section. */
5881 if (h->indx == -3)
5882 continue;
5883 }
5884 else if (h->root.type == bfd_link_hash_common)
0ad989f9
L
5885 {
5886 /* We currently have a common symbol. The archive map contains
5887 a reference to this symbol, so we may want to include it. We
5888 only want to include it however, if this archive element
5889 contains a definition of the symbol, not just another common
5890 declaration of it.
5891
5892 Unfortunately some archivers (including GNU ar) will put
5893 declarations of common symbols into their archive maps, as
5894 well as real definitions, so we cannot just go by the archive
5895 map alone. Instead we must read in the element's symbol
5896 table and check that to see what kind of symbol definition
5897 this is. */
5898 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5899 continue;
5900 }
75cfe082 5901 else
0ad989f9
L
5902 {
5903 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5904 /* Symbol must be defined. Don't check it again. */
5905 included[i] = TRUE;
0ad989f9
L
5906 continue;
5907 }
5908
5909 /* We need to include this archive member. */
5910 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5911 if (element == NULL)
5912 goto error_return;
5913
5914 if (! bfd_check_format (element, bfd_object))
5915 goto error_return;
5916
0ad989f9
L
5917 undefs_tail = info->hash->undefs_tail;
5918
0e144ba7
AM
5919 if (!(*info->callbacks
5920 ->add_archive_element) (info, element, symdef->name, &element))
b95a0a31 5921 continue;
0e144ba7 5922 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5923 goto error_return;
5924
5925 /* If there are any new undefined symbols, we need to make
5926 another pass through the archive in order to see whether
5927 they can be defined. FIXME: This isn't perfect, because
5928 common symbols wind up on undefs_tail and because an
5929 undefined symbol which is defined later on in this pass
5930 does not require another pass. This isn't a bug, but it
5931 does make the code less efficient than it could be. */
5932 if (undefs_tail != info->hash->undefs_tail)
5933 loop = TRUE;
5934
5935 /* Look backward to mark all symbols from this object file
5936 which we have already seen in this pass. */
5937 mark = i;
5938 do
5939 {
5940 included[mark] = TRUE;
5941 if (mark == 0)
5942 break;
5943 --mark;
5944 }
5945 while (symdefs[mark].file_offset == symdef->file_offset);
5946
5947 /* We mark subsequent symbols from this object file as we go
5948 on through the loop. */
5949 last = symdef->file_offset;
5950 }
5951 }
5952 while (loop);
5953
0ad989f9 5954 free (included);
0ad989f9
L
5955 return TRUE;
5956
5957 error_return:
c9594989 5958 free (included);
0ad989f9
L
5959 return FALSE;
5960}
4ad4eba5
AM
5961
5962/* Given an ELF BFD, add symbols to the global hash table as
5963 appropriate. */
5964
5965bfd_boolean
5966bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5967{
5968 switch (bfd_get_format (abfd))
5969 {
5970 case bfd_object:
5971 return elf_link_add_object_symbols (abfd, info);
5972 case bfd_archive:
5973 return elf_link_add_archive_symbols (abfd, info);
5974 default:
5975 bfd_set_error (bfd_error_wrong_format);
5976 return FALSE;
5977 }
5978}
5a580b3a 5979\f
14b1c01e
AM
5980struct hash_codes_info
5981{
5982 unsigned long *hashcodes;
5983 bfd_boolean error;
5984};
a0c8462f 5985
5a580b3a
AM
5986/* This function will be called though elf_link_hash_traverse to store
5987 all hash value of the exported symbols in an array. */
5988
5989static bfd_boolean
5990elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5991{
a50b1753 5992 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5993 const char *name;
5a580b3a
AM
5994 unsigned long ha;
5995 char *alc = NULL;
5996
5a580b3a
AM
5997 /* Ignore indirect symbols. These are added by the versioning code. */
5998 if (h->dynindx == -1)
5999 return TRUE;
6000
6001 name = h->root.root.string;
422f1182 6002 if (h->versioned >= versioned)
5a580b3a 6003 {
422f1182
L
6004 char *p = strchr (name, ELF_VER_CHR);
6005 if (p != NULL)
14b1c01e 6006 {
422f1182
L
6007 alc = (char *) bfd_malloc (p - name + 1);
6008 if (alc == NULL)
6009 {
6010 inf->error = TRUE;
6011 return FALSE;
6012 }
6013 memcpy (alc, name, p - name);
6014 alc[p - name] = '\0';
6015 name = alc;
14b1c01e 6016 }
5a580b3a
AM
6017 }
6018
6019 /* Compute the hash value. */
6020 ha = bfd_elf_hash (name);
6021
6022 /* Store the found hash value in the array given as the argument. */
14b1c01e 6023 *(inf->hashcodes)++ = ha;
5a580b3a
AM
6024
6025 /* And store it in the struct so that we can put it in the hash table
6026 later. */
f6e332e6 6027 h->u.elf_hash_value = ha;
5a580b3a 6028
c9594989 6029 free (alc);
5a580b3a
AM
6030 return TRUE;
6031}
6032
fdc90cb4
JJ
6033struct collect_gnu_hash_codes
6034{
6035 bfd *output_bfd;
6036 const struct elf_backend_data *bed;
6037 unsigned long int nsyms;
6038 unsigned long int maskbits;
6039 unsigned long int *hashcodes;
6040 unsigned long int *hashval;
6041 unsigned long int *indx;
6042 unsigned long int *counts;
6043 bfd_vma *bitmask;
6044 bfd_byte *contents;
f16a9783 6045 bfd_size_type xlat;
fdc90cb4
JJ
6046 long int min_dynindx;
6047 unsigned long int bucketcount;
6048 unsigned long int symindx;
6049 long int local_indx;
6050 long int shift1, shift2;
6051 unsigned long int mask;
14b1c01e 6052 bfd_boolean error;
fdc90cb4
JJ
6053};
6054
6055/* This function will be called though elf_link_hash_traverse to store
6056 all hash value of the exported symbols in an array. */
6057
6058static bfd_boolean
6059elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6060{
a50b1753 6061 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 6062 const char *name;
fdc90cb4
JJ
6063 unsigned long ha;
6064 char *alc = NULL;
6065
fdc90cb4
JJ
6066 /* Ignore indirect symbols. These are added by the versioning code. */
6067 if (h->dynindx == -1)
6068 return TRUE;
6069
6070 /* Ignore also local symbols and undefined symbols. */
6071 if (! (*s->bed->elf_hash_symbol) (h))
6072 return TRUE;
6073
6074 name = h->root.root.string;
422f1182 6075 if (h->versioned >= versioned)
fdc90cb4 6076 {
422f1182
L
6077 char *p = strchr (name, ELF_VER_CHR);
6078 if (p != NULL)
14b1c01e 6079 {
422f1182
L
6080 alc = (char *) bfd_malloc (p - name + 1);
6081 if (alc == NULL)
6082 {
6083 s->error = TRUE;
6084 return FALSE;
6085 }
6086 memcpy (alc, name, p - name);
6087 alc[p - name] = '\0';
6088 name = alc;
14b1c01e 6089 }
fdc90cb4
JJ
6090 }
6091
6092 /* Compute the hash value. */
6093 ha = bfd_elf_gnu_hash (name);
6094
6095 /* Store the found hash value in the array for compute_bucket_count,
6096 and also for .dynsym reordering purposes. */
6097 s->hashcodes[s->nsyms] = ha;
6098 s->hashval[h->dynindx] = ha;
6099 ++s->nsyms;
6100 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6101 s->min_dynindx = h->dynindx;
6102
c9594989 6103 free (alc);
fdc90cb4
JJ
6104 return TRUE;
6105}
6106
6107/* This function will be called though elf_link_hash_traverse to do
f16a9783
MS
6108 final dynamic symbol renumbering in case of .gnu.hash.
6109 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6110 to the translation table. */
fdc90cb4
JJ
6111
6112static bfd_boolean
f16a9783 6113elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
fdc90cb4 6114{
a50b1753 6115 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
6116 unsigned long int bucket;
6117 unsigned long int val;
6118
fdc90cb4
JJ
6119 /* Ignore indirect symbols. */
6120 if (h->dynindx == -1)
6121 return TRUE;
6122
6123 /* Ignore also local symbols and undefined symbols. */
6124 if (! (*s->bed->elf_hash_symbol) (h))
6125 {
6126 if (h->dynindx >= s->min_dynindx)
f16a9783
MS
6127 {
6128 if (s->bed->record_xhash_symbol != NULL)
6129 {
6130 (*s->bed->record_xhash_symbol) (h, 0);
6131 s->local_indx++;
6132 }
6133 else
6134 h->dynindx = s->local_indx++;
6135 }
fdc90cb4
JJ
6136 return TRUE;
6137 }
6138
6139 bucket = s->hashval[h->dynindx] % s->bucketcount;
6140 val = (s->hashval[h->dynindx] >> s->shift1)
6141 & ((s->maskbits >> s->shift1) - 1);
6142 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6143 s->bitmask[val]
6144 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6145 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6146 if (s->counts[bucket] == 1)
6147 /* Last element terminates the chain. */
6148 val |= 1;
6149 bfd_put_32 (s->output_bfd, val,
6150 s->contents + (s->indx[bucket] - s->symindx) * 4);
6151 --s->counts[bucket];
f16a9783
MS
6152 if (s->bed->record_xhash_symbol != NULL)
6153 {
6154 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6155
6156 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6157 }
6158 else
6159 h->dynindx = s->indx[bucket]++;
fdc90cb4
JJ
6160 return TRUE;
6161}
6162
6163/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6164
6165bfd_boolean
6166_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6167{
6168 return !(h->forced_local
6169 || h->root.type == bfd_link_hash_undefined
6170 || h->root.type == bfd_link_hash_undefweak
6171 || ((h->root.type == bfd_link_hash_defined
6172 || h->root.type == bfd_link_hash_defweak)
6173 && h->root.u.def.section->output_section == NULL));
6174}
6175
5a580b3a
AM
6176/* Array used to determine the number of hash table buckets to use
6177 based on the number of symbols there are. If there are fewer than
6178 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6179 fewer than 37 we use 17 buckets, and so forth. We never use more
6180 than 32771 buckets. */
6181
6182static const size_t elf_buckets[] =
6183{
6184 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6185 16411, 32771, 0
6186};
6187
6188/* Compute bucket count for hashing table. We do not use a static set
6189 of possible tables sizes anymore. Instead we determine for all
6190 possible reasonable sizes of the table the outcome (i.e., the
6191 number of collisions etc) and choose the best solution. The
6192 weighting functions are not too simple to allow the table to grow
6193 without bounds. Instead one of the weighting factors is the size.
6194 Therefore the result is always a good payoff between few collisions
6195 (= short chain lengths) and table size. */
6196static size_t
b20dd2ce 6197compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
6198 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6199 unsigned long int nsyms,
6200 int gnu_hash)
5a580b3a 6201{
5a580b3a 6202 size_t best_size = 0;
5a580b3a 6203 unsigned long int i;
5a580b3a 6204
5a580b3a
AM
6205 /* We have a problem here. The following code to optimize the table
6206 size requires an integer type with more the 32 bits. If
6207 BFD_HOST_U_64_BIT is set we know about such a type. */
6208#ifdef BFD_HOST_U_64_BIT
6209 if (info->optimize)
6210 {
5a580b3a
AM
6211 size_t minsize;
6212 size_t maxsize;
6213 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 6214 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 6215 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 6216 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 6217 unsigned long int *counts;
d40f3da9 6218 bfd_size_type amt;
0883b6e0 6219 unsigned int no_improvement_count = 0;
5a580b3a
AM
6220
6221 /* Possible optimization parameters: if we have NSYMS symbols we say
6222 that the hashing table must at least have NSYMS/4 and at most
6223 2*NSYMS buckets. */
6224 minsize = nsyms / 4;
6225 if (minsize == 0)
6226 minsize = 1;
6227 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
6228 if (gnu_hash)
6229 {
6230 if (minsize < 2)
6231 minsize = 2;
6232 if ((best_size & 31) == 0)
6233 ++best_size;
6234 }
5a580b3a
AM
6235
6236 /* Create array where we count the collisions in. We must use bfd_malloc
6237 since the size could be large. */
6238 amt = maxsize;
6239 amt *= sizeof (unsigned long int);
a50b1753 6240 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 6241 if (counts == NULL)
fdc90cb4 6242 return 0;
5a580b3a
AM
6243
6244 /* Compute the "optimal" size for the hash table. The criteria is a
6245 minimal chain length. The minor criteria is (of course) the size
6246 of the table. */
6247 for (i = minsize; i < maxsize; ++i)
6248 {
6249 /* Walk through the array of hashcodes and count the collisions. */
6250 BFD_HOST_U_64_BIT max;
6251 unsigned long int j;
6252 unsigned long int fact;
6253
fdc90cb4
JJ
6254 if (gnu_hash && (i & 31) == 0)
6255 continue;
6256
5a580b3a
AM
6257 memset (counts, '\0', i * sizeof (unsigned long int));
6258
6259 /* Determine how often each hash bucket is used. */
6260 for (j = 0; j < nsyms; ++j)
6261 ++counts[hashcodes[j] % i];
6262
6263 /* For the weight function we need some information about the
6264 pagesize on the target. This is information need not be 100%
6265 accurate. Since this information is not available (so far) we
6266 define it here to a reasonable default value. If it is crucial
6267 to have a better value some day simply define this value. */
6268# ifndef BFD_TARGET_PAGESIZE
6269# define BFD_TARGET_PAGESIZE (4096)
6270# endif
6271
fdc90cb4
JJ
6272 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6273 and the chains. */
6274 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
6275
6276# if 1
6277 /* Variant 1: optimize for short chains. We add the squares
6278 of all the chain lengths (which favors many small chain
6279 over a few long chains). */
6280 for (j = 0; j < i; ++j)
6281 max += counts[j] * counts[j];
6282
6283 /* This adds penalties for the overall size of the table. */
fdc90cb4 6284 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6285 max *= fact * fact;
6286# else
6287 /* Variant 2: Optimize a lot more for small table. Here we
6288 also add squares of the size but we also add penalties for
6289 empty slots (the +1 term). */
6290 for (j = 0; j < i; ++j)
6291 max += (1 + counts[j]) * (1 + counts[j]);
6292
6293 /* The overall size of the table is considered, but not as
6294 strong as in variant 1, where it is squared. */
fdc90cb4 6295 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6296 max *= fact;
6297# endif
6298
6299 /* Compare with current best results. */
6300 if (max < best_chlen)
6301 {
6302 best_chlen = max;
6303 best_size = i;
ca4be51c 6304 no_improvement_count = 0;
5a580b3a 6305 }
0883b6e0
NC
6306 /* PR 11843: Avoid futile long searches for the best bucket size
6307 when there are a large number of symbols. */
6308 else if (++no_improvement_count == 100)
6309 break;
5a580b3a
AM
6310 }
6311
6312 free (counts);
6313 }
6314 else
6315#endif /* defined (BFD_HOST_U_64_BIT) */
6316 {
6317 /* This is the fallback solution if no 64bit type is available or if we
6318 are not supposed to spend much time on optimizations. We select the
6319 bucket count using a fixed set of numbers. */
6320 for (i = 0; elf_buckets[i] != 0; i++)
6321 {
6322 best_size = elf_buckets[i];
fdc90cb4 6323 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
6324 break;
6325 }
fdc90cb4
JJ
6326 if (gnu_hash && best_size < 2)
6327 best_size = 2;
5a580b3a
AM
6328 }
6329
5a580b3a
AM
6330 return best_size;
6331}
6332
d0bf826b
AM
6333/* Size any SHT_GROUP section for ld -r. */
6334
6335bfd_boolean
6336_bfd_elf_size_group_sections (struct bfd_link_info *info)
6337{
6338 bfd *ibfd;
57963c05 6339 asection *s;
d0bf826b 6340
c72f2fb2 6341 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b 6342 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
57963c05
AM
6343 && (s = ibfd->sections) != NULL
6344 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
d0bf826b
AM
6345 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6346 return FALSE;
6347 return TRUE;
6348}
6349
04c3a755
NS
6350/* Set a default stack segment size. The value in INFO wins. If it
6351 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6352 undefined it is initialized. */
6353
6354bfd_boolean
6355bfd_elf_stack_segment_size (bfd *output_bfd,
6356 struct bfd_link_info *info,
6357 const char *legacy_symbol,
6358 bfd_vma default_size)
6359{
6360 struct elf_link_hash_entry *h = NULL;
6361
6362 /* Look for legacy symbol. */
6363 if (legacy_symbol)
6364 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6365 FALSE, FALSE, FALSE);
6366 if (h && (h->root.type == bfd_link_hash_defined
6367 || h->root.type == bfd_link_hash_defweak)
6368 && h->def_regular
6369 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6370 {
6371 /* The symbol has no type if specified on the command line. */
6372 h->type = STT_OBJECT;
6373 if (info->stacksize)
695344c0 6374 /* xgettext:c-format */
871b3ab2 6375 _bfd_error_handler (_("%pB: stack size specified and %s set"),
4eca0228 6376 output_bfd, legacy_symbol);
04c3a755 6377 else if (h->root.u.def.section != bfd_abs_section_ptr)
695344c0 6378 /* xgettext:c-format */
871b3ab2 6379 _bfd_error_handler (_("%pB: %s not absolute"),
4eca0228 6380 output_bfd, legacy_symbol);
04c3a755
NS
6381 else
6382 info->stacksize = h->root.u.def.value;
6383 }
6384
6385 if (!info->stacksize)
6386 /* If the user didn't set a size, or explicitly inhibit the
6387 size, set it now. */
6388 info->stacksize = default_size;
6389
6390 /* Provide the legacy symbol, if it is referenced. */
6391 if (h && (h->root.type == bfd_link_hash_undefined
6392 || h->root.type == bfd_link_hash_undefweak))
6393 {
6394 struct bfd_link_hash_entry *bh = NULL;
6395
6396 if (!(_bfd_generic_link_add_one_symbol
6397 (info, output_bfd, legacy_symbol,
6398 BSF_GLOBAL, bfd_abs_section_ptr,
6399 info->stacksize >= 0 ? info->stacksize : 0,
6400 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6401 return FALSE;
6402
6403 h = (struct elf_link_hash_entry *) bh;
6404 h->def_regular = 1;
6405 h->type = STT_OBJECT;
6406 }
6407
6408 return TRUE;
6409}
6410
b531344c
MR
6411/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6412
6413struct elf_gc_sweep_symbol_info
6414{
6415 struct bfd_link_info *info;
6416 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6417 bfd_boolean);
6418};
6419
6420static bfd_boolean
6421elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6422{
6423 if (!h->mark
6424 && (((h->root.type == bfd_link_hash_defined
6425 || h->root.type == bfd_link_hash_defweak)
6426 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6427 && h->root.u.def.section->gc_mark))
6428 || h->root.type == bfd_link_hash_undefined
6429 || h->root.type == bfd_link_hash_undefweak))
6430 {
6431 struct elf_gc_sweep_symbol_info *inf;
6432
6433 inf = (struct elf_gc_sweep_symbol_info *) data;
6434 (*inf->hide_symbol) (inf->info, h, TRUE);
6435 h->def_regular = 0;
6436 h->ref_regular = 0;
6437 h->ref_regular_nonweak = 0;
6438 }
6439
6440 return TRUE;
6441}
6442
5a580b3a
AM
6443/* Set up the sizes and contents of the ELF dynamic sections. This is
6444 called by the ELF linker emulation before_allocation routine. We
6445 must set the sizes of the sections before the linker sets the
6446 addresses of the various sections. */
6447
6448bfd_boolean
6449bfd_elf_size_dynamic_sections (bfd *output_bfd,
6450 const char *soname,
6451 const char *rpath,
6452 const char *filter_shlib,
7ee314fa
AM
6453 const char *audit,
6454 const char *depaudit,
5a580b3a
AM
6455 const char * const *auxiliary_filters,
6456 struct bfd_link_info *info,
fd91d419 6457 asection **sinterpptr)
5a580b3a 6458{
5a580b3a
AM
6459 bfd *dynobj;
6460 const struct elf_backend_data *bed;
5a580b3a
AM
6461
6462 *sinterpptr = NULL;
6463
5a580b3a
AM
6464 if (!is_elf_hash_table (info->hash))
6465 return TRUE;
6466
5a580b3a
AM
6467 dynobj = elf_hash_table (info)->dynobj;
6468
9a2a56cc 6469 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 6470 {
902e9fc7
MR
6471 struct bfd_elf_version_tree *verdefs;
6472 struct elf_info_failed asvinfo;
5a580b3a
AM
6473 struct bfd_elf_version_tree *t;
6474 struct bfd_elf_version_expr *d;
902e9fc7 6475 asection *s;
e6699019 6476 size_t soname_indx;
7ee314fa 6477
5a580b3a
AM
6478 /* If we are supposed to export all symbols into the dynamic symbol
6479 table (this is not the normal case), then do so. */
55255dae 6480 if (info->export_dynamic
0e1862bb 6481 || (bfd_link_executable (info) && info->dynamic))
5a580b3a 6482 {
3d13f3e9
AM
6483 struct elf_info_failed eif;
6484
6485 eif.info = info;
6486 eif.failed = FALSE;
5a580b3a
AM
6487 elf_link_hash_traverse (elf_hash_table (info),
6488 _bfd_elf_export_symbol,
6489 &eif);
6490 if (eif.failed)
6491 return FALSE;
6492 }
6493
e6699019
L
6494 if (soname != NULL)
6495 {
6496 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6497 soname, TRUE);
6498 if (soname_indx == (size_t) -1
6499 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6500 return FALSE;
6501 }
6502 else
6503 soname_indx = (size_t) -1;
6504
5a580b3a 6505 /* Make all global versions with definition. */
fd91d419 6506 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6507 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6508 if (!d->symver && d->literal)
5a580b3a
AM
6509 {
6510 const char *verstr, *name;
6511 size_t namelen, verlen, newlen;
93252b1c 6512 char *newname, *p, leading_char;
5a580b3a
AM
6513 struct elf_link_hash_entry *newh;
6514
93252b1c 6515 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 6516 name = d->pattern;
93252b1c 6517 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
6518 verstr = t->name;
6519 verlen = strlen (verstr);
6520 newlen = namelen + verlen + 3;
6521
a50b1753 6522 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
6523 if (newname == NULL)
6524 return FALSE;
93252b1c
MF
6525 newname[0] = leading_char;
6526 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
6527
6528 /* Check the hidden versioned definition. */
6529 p = newname + namelen;
6530 *p++ = ELF_VER_CHR;
6531 memcpy (p, verstr, verlen + 1);
6532 newh = elf_link_hash_lookup (elf_hash_table (info),
6533 newname, FALSE, FALSE,
6534 FALSE);
6535 if (newh == NULL
6536 || (newh->root.type != bfd_link_hash_defined
6537 && newh->root.type != bfd_link_hash_defweak))
6538 {
6539 /* Check the default versioned definition. */
6540 *p++ = ELF_VER_CHR;
6541 memcpy (p, verstr, verlen + 1);
6542 newh = elf_link_hash_lookup (elf_hash_table (info),
6543 newname, FALSE, FALSE,
6544 FALSE);
6545 }
6546 free (newname);
6547
6548 /* Mark this version if there is a definition and it is
6549 not defined in a shared object. */
6550 if (newh != NULL
f5385ebf 6551 && !newh->def_dynamic
5a580b3a
AM
6552 && (newh->root.type == bfd_link_hash_defined
6553 || newh->root.type == bfd_link_hash_defweak))
6554 d->symver = 1;
6555 }
6556
6557 /* Attach all the symbols to their version information. */
5a580b3a 6558 asvinfo.info = info;
5a580b3a
AM
6559 asvinfo.failed = FALSE;
6560
6561 elf_link_hash_traverse (elf_hash_table (info),
6562 _bfd_elf_link_assign_sym_version,
6563 &asvinfo);
6564 if (asvinfo.failed)
6565 return FALSE;
6566
6567 if (!info->allow_undefined_version)
6568 {
6569 /* Check if all global versions have a definition. */
3d13f3e9 6570 bfd_boolean all_defined = TRUE;
fd91d419 6571 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6572 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6573 if (d->literal && !d->symver && !d->script)
5a580b3a 6574 {
4eca0228 6575 _bfd_error_handler
5a580b3a
AM
6576 (_("%s: undefined version: %s"),
6577 d->pattern, t->name);
6578 all_defined = FALSE;
6579 }
6580
6581 if (!all_defined)
6582 {
6583 bfd_set_error (bfd_error_bad_value);
6584 return FALSE;
6585 }
6586 }
6587
902e9fc7
MR
6588 /* Set up the version definition section. */
6589 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6590 BFD_ASSERT (s != NULL);
5a580b3a 6591
902e9fc7
MR
6592 /* We may have created additional version definitions if we are
6593 just linking a regular application. */
6594 verdefs = info->version_info;
5a580b3a 6595
902e9fc7
MR
6596 /* Skip anonymous version tag. */
6597 if (verdefs != NULL && verdefs->vernum == 0)
6598 verdefs = verdefs->next;
5a580b3a 6599
902e9fc7
MR
6600 if (verdefs == NULL && !info->create_default_symver)
6601 s->flags |= SEC_EXCLUDE;
6602 else
5a580b3a 6603 {
902e9fc7
MR
6604 unsigned int cdefs;
6605 bfd_size_type size;
6606 bfd_byte *p;
6607 Elf_Internal_Verdef def;
6608 Elf_Internal_Verdaux defaux;
6609 struct bfd_link_hash_entry *bh;
6610 struct elf_link_hash_entry *h;
6611 const char *name;
5a580b3a 6612
902e9fc7
MR
6613 cdefs = 0;
6614 size = 0;
5a580b3a 6615
902e9fc7
MR
6616 /* Make space for the base version. */
6617 size += sizeof (Elf_External_Verdef);
6618 size += sizeof (Elf_External_Verdaux);
6619 ++cdefs;
6620
6621 /* Make space for the default version. */
6622 if (info->create_default_symver)
6623 {
6624 size += sizeof (Elf_External_Verdef);
6625 ++cdefs;
3e3b46e5
PB
6626 }
6627
5a580b3a
AM
6628 for (t = verdefs; t != NULL; t = t->next)
6629 {
6630 struct bfd_elf_version_deps *n;
6631
a6cc6b3b
RO
6632 /* Don't emit base version twice. */
6633 if (t->vernum == 0)
6634 continue;
6635
5a580b3a
AM
6636 size += sizeof (Elf_External_Verdef);
6637 size += sizeof (Elf_External_Verdaux);
6638 ++cdefs;
6639
6640 for (n = t->deps; n != NULL; n = n->next)
6641 size += sizeof (Elf_External_Verdaux);
6642 }
6643
eea6121a 6644 s->size = size;
a50b1753 6645 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6646 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6647 return FALSE;
6648
6649 /* Fill in the version definition section. */
6650
6651 p = s->contents;
6652
6653 def.vd_version = VER_DEF_CURRENT;
6654 def.vd_flags = VER_FLG_BASE;
6655 def.vd_ndx = 1;
6656 def.vd_cnt = 1;
3e3b46e5
PB
6657 if (info->create_default_symver)
6658 {
6659 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6660 def.vd_next = sizeof (Elf_External_Verdef);
6661 }
6662 else
6663 {
6664 def.vd_aux = sizeof (Elf_External_Verdef);
6665 def.vd_next = (sizeof (Elf_External_Verdef)
6666 + sizeof (Elf_External_Verdaux));
6667 }
5a580b3a 6668
ef53be89 6669 if (soname_indx != (size_t) -1)
5a580b3a
AM
6670 {
6671 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6672 soname_indx);
6673 def.vd_hash = bfd_elf_hash (soname);
6674 defaux.vda_name = soname_indx;
3e3b46e5 6675 name = soname;
5a580b3a
AM
6676 }
6677 else
6678 {
ef53be89 6679 size_t indx;
5a580b3a 6680
765cf5f6 6681 name = lbasename (bfd_get_filename (output_bfd));
5a580b3a
AM
6682 def.vd_hash = bfd_elf_hash (name);
6683 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6684 name, FALSE);
ef53be89 6685 if (indx == (size_t) -1)
5a580b3a
AM
6686 return FALSE;
6687 defaux.vda_name = indx;
6688 }
6689 defaux.vda_next = 0;
6690
6691 _bfd_elf_swap_verdef_out (output_bfd, &def,
6692 (Elf_External_Verdef *) p);
6693 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6694 if (info->create_default_symver)
6695 {
6696 /* Add a symbol representing this version. */
6697 bh = NULL;
6698 if (! (_bfd_generic_link_add_one_symbol
6699 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6700 0, NULL, FALSE,
6701 get_elf_backend_data (dynobj)->collect, &bh)))
6702 return FALSE;
6703 h = (struct elf_link_hash_entry *) bh;
6704 h->non_elf = 0;
6705 h->def_regular = 1;
6706 h->type = STT_OBJECT;
6707 h->verinfo.vertree = NULL;
6708
6709 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6710 return FALSE;
6711
6712 /* Create a duplicate of the base version with the same
6713 aux block, but different flags. */
6714 def.vd_flags = 0;
6715 def.vd_ndx = 2;
6716 def.vd_aux = sizeof (Elf_External_Verdef);
6717 if (verdefs)
6718 def.vd_next = (sizeof (Elf_External_Verdef)
6719 + sizeof (Elf_External_Verdaux));
6720 else
6721 def.vd_next = 0;
6722 _bfd_elf_swap_verdef_out (output_bfd, &def,
6723 (Elf_External_Verdef *) p);
6724 p += sizeof (Elf_External_Verdef);
6725 }
5a580b3a
AM
6726 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6727 (Elf_External_Verdaux *) p);
6728 p += sizeof (Elf_External_Verdaux);
6729
6730 for (t = verdefs; t != NULL; t = t->next)
6731 {
6732 unsigned int cdeps;
6733 struct bfd_elf_version_deps *n;
5a580b3a 6734
a6cc6b3b
RO
6735 /* Don't emit the base version twice. */
6736 if (t->vernum == 0)
6737 continue;
6738
5a580b3a
AM
6739 cdeps = 0;
6740 for (n = t->deps; n != NULL; n = n->next)
6741 ++cdeps;
6742
6743 /* Add a symbol representing this version. */
6744 bh = NULL;
6745 if (! (_bfd_generic_link_add_one_symbol
6746 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6747 0, NULL, FALSE,
6748 get_elf_backend_data (dynobj)->collect, &bh)))
6749 return FALSE;
6750 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6751 h->non_elf = 0;
6752 h->def_regular = 1;
5a580b3a
AM
6753 h->type = STT_OBJECT;
6754 h->verinfo.vertree = t;
6755
c152c796 6756 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6757 return FALSE;
6758
6759 def.vd_version = VER_DEF_CURRENT;
6760 def.vd_flags = 0;
6761 if (t->globals.list == NULL
6762 && t->locals.list == NULL
6763 && ! t->used)
6764 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6765 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6766 def.vd_cnt = cdeps + 1;
6767 def.vd_hash = bfd_elf_hash (t->name);
6768 def.vd_aux = sizeof (Elf_External_Verdef);
6769 def.vd_next = 0;
a6cc6b3b
RO
6770
6771 /* If a basever node is next, it *must* be the last node in
6772 the chain, otherwise Verdef construction breaks. */
6773 if (t->next != NULL && t->next->vernum == 0)
6774 BFD_ASSERT (t->next->next == NULL);
6775
6776 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6777 def.vd_next = (sizeof (Elf_External_Verdef)
6778 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6779
6780 _bfd_elf_swap_verdef_out (output_bfd, &def,
6781 (Elf_External_Verdef *) p);
6782 p += sizeof (Elf_External_Verdef);
6783
6784 defaux.vda_name = h->dynstr_index;
6785 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6786 h->dynstr_index);
6787 defaux.vda_next = 0;
6788 if (t->deps != NULL)
6789 defaux.vda_next = sizeof (Elf_External_Verdaux);
6790 t->name_indx = defaux.vda_name;
6791
6792 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6793 (Elf_External_Verdaux *) p);
6794 p += sizeof (Elf_External_Verdaux);
6795
6796 for (n = t->deps; n != NULL; n = n->next)
6797 {
6798 if (n->version_needed == NULL)
6799 {
6800 /* This can happen if there was an error in the
6801 version script. */
6802 defaux.vda_name = 0;
6803 }
6804 else
6805 {
6806 defaux.vda_name = n->version_needed->name_indx;
6807 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6808 defaux.vda_name);
6809 }
6810 if (n->next == NULL)
6811 defaux.vda_next = 0;
6812 else
6813 defaux.vda_next = sizeof (Elf_External_Verdaux);
6814
6815 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6816 (Elf_External_Verdaux *) p);
6817 p += sizeof (Elf_External_Verdaux);
6818 }
6819 }
6820
5a580b3a
AM
6821 elf_tdata (output_bfd)->cverdefs = cdefs;
6822 }
902e9fc7
MR
6823 }
6824
6825 bed = get_elf_backend_data (output_bfd);
6826
6827 if (info->gc_sections && bed->can_gc_sections)
6828 {
6829 struct elf_gc_sweep_symbol_info sweep_info;
902e9fc7
MR
6830
6831 /* Remove the symbols that were in the swept sections from the
3d13f3e9 6832 dynamic symbol table. */
902e9fc7
MR
6833 sweep_info.info = info;
6834 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6835 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6836 &sweep_info);
3d13f3e9
AM
6837 }
6838
6839 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6840 {
6841 asection *s;
6842 struct elf_find_verdep_info sinfo;
6843
6844 /* Work out the size of the version reference section. */
6845
6846 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6847 BFD_ASSERT (s != NULL);
902e9fc7 6848
3d13f3e9
AM
6849 sinfo.info = info;
6850 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6851 if (sinfo.vers == 0)
6852 sinfo.vers = 1;
6853 sinfo.failed = FALSE;
6854
6855 elf_link_hash_traverse (elf_hash_table (info),
6856 _bfd_elf_link_find_version_dependencies,
6857 &sinfo);
6858 if (sinfo.failed)
6859 return FALSE;
6860
6861 if (elf_tdata (output_bfd)->verref == NULL)
6862 s->flags |= SEC_EXCLUDE;
6863 else
6864 {
6865 Elf_Internal_Verneed *vn;
6866 unsigned int size;
6867 unsigned int crefs;
6868 bfd_byte *p;
6869
6870 /* Build the version dependency section. */
6871 size = 0;
6872 crefs = 0;
6873 for (vn = elf_tdata (output_bfd)->verref;
6874 vn != NULL;
6875 vn = vn->vn_nextref)
6876 {
6877 Elf_Internal_Vernaux *a;
6878
6879 size += sizeof (Elf_External_Verneed);
6880 ++crefs;
6881 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6882 size += sizeof (Elf_External_Vernaux);
6883 }
6884
6885 s->size = size;
6886 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6887 if (s->contents == NULL)
6888 return FALSE;
6889
6890 p = s->contents;
6891 for (vn = elf_tdata (output_bfd)->verref;
6892 vn != NULL;
6893 vn = vn->vn_nextref)
6894 {
6895 unsigned int caux;
6896 Elf_Internal_Vernaux *a;
6897 size_t indx;
6898
6899 caux = 0;
6900 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6901 ++caux;
6902
6903 vn->vn_version = VER_NEED_CURRENT;
6904 vn->vn_cnt = caux;
6905 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6906 elf_dt_name (vn->vn_bfd) != NULL
6907 ? elf_dt_name (vn->vn_bfd)
765cf5f6
AM
6908 : lbasename (bfd_get_filename
6909 (vn->vn_bfd)),
3d13f3e9
AM
6910 FALSE);
6911 if (indx == (size_t) -1)
6912 return FALSE;
6913 vn->vn_file = indx;
6914 vn->vn_aux = sizeof (Elf_External_Verneed);
6915 if (vn->vn_nextref == NULL)
6916 vn->vn_next = 0;
6917 else
6918 vn->vn_next = (sizeof (Elf_External_Verneed)
6919 + caux * sizeof (Elf_External_Vernaux));
6920
6921 _bfd_elf_swap_verneed_out (output_bfd, vn,
6922 (Elf_External_Verneed *) p);
6923 p += sizeof (Elf_External_Verneed);
6924
6925 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6926 {
6927 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6928 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6929 a->vna_nodename, FALSE);
6930 if (indx == (size_t) -1)
6931 return FALSE;
6932 a->vna_name = indx;
6933 if (a->vna_nextptr == NULL)
6934 a->vna_next = 0;
6935 else
6936 a->vna_next = sizeof (Elf_External_Vernaux);
6937
6938 _bfd_elf_swap_vernaux_out (output_bfd, a,
6939 (Elf_External_Vernaux *) p);
6940 p += sizeof (Elf_External_Vernaux);
6941 }
6942 }
6943
6944 elf_tdata (output_bfd)->cverrefs = crefs;
6945 }
902e9fc7
MR
6946 }
6947
6948 /* Any syms created from now on start with -1 in
6949 got.refcount/offset and plt.refcount/offset. */
6950 elf_hash_table (info)->init_got_refcount
6951 = elf_hash_table (info)->init_got_offset;
6952 elf_hash_table (info)->init_plt_refcount
6953 = elf_hash_table (info)->init_plt_offset;
6954
6955 if (bfd_link_relocatable (info)
6956 && !_bfd_elf_size_group_sections (info))
6957 return FALSE;
6958
6959 /* The backend may have to create some sections regardless of whether
6960 we're dynamic or not. */
6961 if (bed->elf_backend_always_size_sections
6962 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6963 return FALSE;
6964
6965 /* Determine any GNU_STACK segment requirements, after the backend
6966 has had a chance to set a default segment size. */
6967 if (info->execstack)
6968 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6969 else if (info->noexecstack)
6970 elf_stack_flags (output_bfd) = PF_R | PF_W;
6971 else
6972 {
6973 bfd *inputobj;
6974 asection *notesec = NULL;
6975 int exec = 0;
6976
6977 for (inputobj = info->input_bfds;
6978 inputobj;
6979 inputobj = inputobj->link.next)
6980 {
6981 asection *s;
6982
6983 if (inputobj->flags
6984 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6985 continue;
57963c05
AM
6986 s = inputobj->sections;
6987 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6988 continue;
6989
902e9fc7
MR
6990 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6991 if (s)
6992 {
6993 if (s->flags & SEC_CODE)
6994 exec = PF_X;
6995 notesec = s;
6996 }
6997 else if (bed->default_execstack)
6998 exec = PF_X;
6999 }
7000 if (notesec || info->stacksize > 0)
7001 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
7002 if (notesec && exec && bfd_link_relocatable (info)
7003 && notesec->output_section != bfd_abs_section_ptr)
7004 notesec->output_section->flags |= SEC_CODE;
7005 }
7006
7007 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7008 {
7009 struct elf_info_failed eif;
7010 struct elf_link_hash_entry *h;
7011 asection *dynstr;
7012 asection *s;
7013
7014 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
7015 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
7016
902e9fc7
MR
7017 if (info->symbolic)
7018 {
7019 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
7020 return FALSE;
7021 info->flags |= DF_SYMBOLIC;
7022 }
7023
7024 if (rpath != NULL)
7025 {
7026 size_t indx;
7027 bfd_vma tag;
7028
7029 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
7030 TRUE);
7031 if (indx == (size_t) -1)
7032 return FALSE;
7033
7034 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
7035 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
7036 return FALSE;
7037 }
7038
7039 if (filter_shlib != NULL)
7040 {
7041 size_t indx;
7042
7043 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7044 filter_shlib, TRUE);
7045 if (indx == (size_t) -1
7046 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
7047 return FALSE;
7048 }
7049
7050 if (auxiliary_filters != NULL)
7051 {
7052 const char * const *p;
7053
7054 for (p = auxiliary_filters; *p != NULL; p++)
7055 {
7056 size_t indx;
7057
7058 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7059 *p, TRUE);
7060 if (indx == (size_t) -1
7061 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7062 return FALSE;
7063 }
7064 }
7065
7066 if (audit != NULL)
7067 {
7068 size_t indx;
7069
7070 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7071 TRUE);
7072 if (indx == (size_t) -1
7073 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7074 return FALSE;
7075 }
7076
7077 if (depaudit != NULL)
7078 {
7079 size_t indx;
7080
7081 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7082 TRUE);
7083 if (indx == (size_t) -1
7084 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7085 return FALSE;
7086 }
7087
7088 eif.info = info;
7089 eif.failed = FALSE;
7090
7091 /* Find all symbols which were defined in a dynamic object and make
7092 the backend pick a reasonable value for them. */
7093 elf_link_hash_traverse (elf_hash_table (info),
7094 _bfd_elf_adjust_dynamic_symbol,
7095 &eif);
7096 if (eif.failed)
7097 return FALSE;
7098
7099 /* Add some entries to the .dynamic section. We fill in some of the
7100 values later, in bfd_elf_final_link, but we must add the entries
7101 now so that we know the final size of the .dynamic section. */
7102
7103 /* If there are initialization and/or finalization functions to
7104 call then add the corresponding DT_INIT/DT_FINI entries. */
7105 h = (info->init_function
7106 ? elf_link_hash_lookup (elf_hash_table (info),
7107 info->init_function, FALSE,
7108 FALSE, FALSE)
7109 : NULL);
7110 if (h != NULL
7111 && (h->ref_regular
7112 || h->def_regular))
7113 {
7114 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7115 return FALSE;
7116 }
7117 h = (info->fini_function
7118 ? elf_link_hash_lookup (elf_hash_table (info),
7119 info->fini_function, FALSE,
7120 FALSE, FALSE)
7121 : NULL);
7122 if (h != NULL
7123 && (h->ref_regular
7124 || h->def_regular))
7125 {
7126 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7127 return FALSE;
7128 }
7129
7130 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7131 if (s != NULL && s->linker_has_input)
7132 {
7133 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7134 if (! bfd_link_executable (info))
7135 {
7136 bfd *sub;
7137 asection *o;
7138
57963c05
AM
7139 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7140 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7141 && (o = sub->sections) != NULL
7142 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
902e9fc7
MR
7143 for (o = sub->sections; o != NULL; o = o->next)
7144 if (elf_section_data (o)->this_hdr.sh_type
7145 == SHT_PREINIT_ARRAY)
7146 {
7147 _bfd_error_handler
871b3ab2 7148 (_("%pB: .preinit_array section is not allowed in DSO"),
902e9fc7
MR
7149 sub);
7150 break;
7151 }
7152
7153 bfd_set_error (bfd_error_nonrepresentable_section);
7154 return FALSE;
7155 }
7156
7157 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7158 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7159 return FALSE;
7160 }
7161 s = bfd_get_section_by_name (output_bfd, ".init_array");
7162 if (s != NULL && s->linker_has_input)
7163 {
7164 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7165 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7166 return FALSE;
7167 }
7168 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7169 if (s != NULL && s->linker_has_input)
7170 {
7171 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7172 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7173 return FALSE;
7174 }
7175
7176 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7177 /* If .dynstr is excluded from the link, we don't want any of
7178 these tags. Strictly, we should be checking each section
7179 individually; This quick check covers for the case where
7180 someone does a /DISCARD/ : { *(*) }. */
7181 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7182 {
7183 bfd_size_type strsize;
7184
7185 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7186 if ((info->emit_hash
7187 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7188 || (info->emit_gnu_hash
f16a9783
MS
7189 && (bed->record_xhash_symbol == NULL
7190 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
902e9fc7
MR
7191 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7192 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7193 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7194 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6a0a0dd0
VDM
7195 bed->s->sizeof_sym)
7196 || (info->gnu_flags_1
7197 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
7198 info->gnu_flags_1)))
902e9fc7
MR
7199 return FALSE;
7200 }
7201 }
7202
7203 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7204 return FALSE;
7205
7206 /* The backend must work out the sizes of all the other dynamic
7207 sections. */
7208 if (dynobj != NULL
7209 && bed->elf_backend_size_dynamic_sections != NULL
7210 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7211 return FALSE;
7212
7213 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7214 {
902e9fc7
MR
7215 if (elf_tdata (output_bfd)->cverdefs)
7216 {
7217 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7218
7219 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7220 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7221 return FALSE;
7222 }
7223
7224 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7225 {
7226 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7227 return FALSE;
7228 }
7229 else if (info->flags & DF_BIND_NOW)
7230 {
7231 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7232 return FALSE;
7233 }
7234
7235 if (info->flags_1)
7236 {
7237 if (bfd_link_executable (info))
7238 info->flags_1 &= ~ (DF_1_INITFIRST
7239 | DF_1_NODELETE
7240 | DF_1_NOOPEN);
7241 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7242 return FALSE;
7243 }
7244
7245 if (elf_tdata (output_bfd)->cverrefs)
7246 {
7247 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7248
7249 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7250 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7251 return FALSE;
7252 }
5a580b3a 7253
8423293d
AM
7254 if ((elf_tdata (output_bfd)->cverrefs == 0
7255 && elf_tdata (output_bfd)->cverdefs == 0)
63f452a8 7256 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
8423293d 7257 {
902e9fc7
MR
7258 asection *s;
7259
3d4d4302 7260 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
7261 s->flags |= SEC_EXCLUDE;
7262 }
7263 }
7264 return TRUE;
7265}
7266
74541ad4
AM
7267/* Find the first non-excluded output section. We'll use its
7268 section symbol for some emitted relocs. */
7269void
7270_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7271{
7272 asection *s;
f26a3287 7273 asection *found = NULL;
74541ad4
AM
7274
7275 for (s = output_bfd->sections; s != NULL; s = s->next)
7276 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
d00dd7dc 7277 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7278 {
f26a3287
AM
7279 found = s;
7280 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7281 break;
74541ad4 7282 }
f26a3287 7283 elf_hash_table (info)->text_index_section = found;
74541ad4
AM
7284}
7285
7286/* Find two non-excluded output sections, one for code, one for data.
7287 We'll use their section symbols for some emitted relocs. */
7288void
7289_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7290{
7291 asection *s;
f26a3287 7292 asection *found = NULL;
74541ad4 7293
266b05cf 7294 /* Data first, since setting text_index_section changes
7f923b7f 7295 _bfd_elf_omit_section_dynsym_default. */
74541ad4 7296 for (s = output_bfd->sections; s != NULL; s = s->next)
f26a3287
AM
7297 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7298 && !(s->flags & SEC_READONLY)
d00dd7dc 7299 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7300 {
f26a3287
AM
7301 found = s;
7302 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7303 break;
74541ad4 7304 }
f26a3287 7305 elf_hash_table (info)->data_index_section = found;
74541ad4
AM
7306
7307 for (s = output_bfd->sections; s != NULL; s = s->next)
f26a3287
AM
7308 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7309 && (s->flags & SEC_READONLY)
d00dd7dc 7310 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7311 {
f26a3287 7312 found = s;
74541ad4
AM
7313 break;
7314 }
f26a3287 7315 elf_hash_table (info)->text_index_section = found;
74541ad4
AM
7316}
7317
f16a9783
MS
7318#define GNU_HASH_SECTION_NAME(bed) \
7319 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7320
8423293d
AM
7321bfd_boolean
7322bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7323{
74541ad4 7324 const struct elf_backend_data *bed;
23ec1e32 7325 unsigned long section_sym_count;
96d01d93 7326 bfd_size_type dynsymcount = 0;
74541ad4 7327
8423293d
AM
7328 if (!is_elf_hash_table (info->hash))
7329 return TRUE;
7330
74541ad4
AM
7331 bed = get_elf_backend_data (output_bfd);
7332 (*bed->elf_backend_init_index_section) (output_bfd, info);
7333
23ec1e32
MR
7334 /* Assign dynsym indices. In a shared library we generate a section
7335 symbol for each output section, which come first. Next come all
7336 of the back-end allocated local dynamic syms, followed by the rest
7337 of the global symbols.
7338
7339 This is usually not needed for static binaries, however backends
7340 can request to always do it, e.g. the MIPS backend uses dynamic
7341 symbol counts to lay out GOT, which will be produced in the
7342 presence of GOT relocations even in static binaries (holding fixed
7343 data in that case, to satisfy those relocations). */
7344
7345 if (elf_hash_table (info)->dynamic_sections_created
7346 || bed->always_renumber_dynsyms)
7347 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7348 &section_sym_count);
7349
8423293d
AM
7350 if (elf_hash_table (info)->dynamic_sections_created)
7351 {
7352 bfd *dynobj;
8423293d 7353 asection *s;
8423293d
AM
7354 unsigned int dtagcount;
7355
7356 dynobj = elf_hash_table (info)->dynobj;
7357
5a580b3a 7358 /* Work out the size of the symbol version section. */
3d4d4302 7359 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 7360 BFD_ASSERT (s != NULL);
d5486c43 7361 if ((s->flags & SEC_EXCLUDE) == 0)
5a580b3a 7362 {
eea6121a 7363 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 7364 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
7365 if (s->contents == NULL)
7366 return FALSE;
7367
7368 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7369 return FALSE;
7370 }
7371
7372 /* Set the size of the .dynsym and .hash sections. We counted
7373 the number of dynamic symbols in elf_link_add_object_symbols.
7374 We will build the contents of .dynsym and .hash when we build
7375 the final symbol table, because until then we do not know the
7376 correct value to give the symbols. We built the .dynstr
7377 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 7378 s = elf_hash_table (info)->dynsym;
5a580b3a 7379 BFD_ASSERT (s != NULL);
eea6121a 7380 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a 7381
d5486c43
L
7382 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7383 if (s->contents == NULL)
7384 return FALSE;
5a580b3a 7385
d5486c43
L
7386 /* The first entry in .dynsym is a dummy symbol. Clear all the
7387 section syms, in case we don't output them all. */
7388 ++section_sym_count;
7389 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a 7390
fdc90cb4
JJ
7391 elf_hash_table (info)->bucketcount = 0;
7392
5a580b3a
AM
7393 /* Compute the size of the hashing table. As a side effect this
7394 computes the hash values for all the names we export. */
fdc90cb4
JJ
7395 if (info->emit_hash)
7396 {
7397 unsigned long int *hashcodes;
14b1c01e 7398 struct hash_codes_info hashinf;
fdc90cb4
JJ
7399 bfd_size_type amt;
7400 unsigned long int nsyms;
7401 size_t bucketcount;
7402 size_t hash_entry_size;
7403
7404 /* Compute the hash values for all exported symbols. At the same
7405 time store the values in an array so that we could use them for
7406 optimizations. */
7407 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 7408 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
7409 if (hashcodes == NULL)
7410 return FALSE;
14b1c01e
AM
7411 hashinf.hashcodes = hashcodes;
7412 hashinf.error = FALSE;
5a580b3a 7413
fdc90cb4
JJ
7414 /* Put all hash values in HASHCODES. */
7415 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
7416 elf_collect_hash_codes, &hashinf);
7417 if (hashinf.error)
4dd07732
AM
7418 {
7419 free (hashcodes);
7420 return FALSE;
7421 }
5a580b3a 7422
14b1c01e 7423 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
7424 bucketcount
7425 = compute_bucket_count (info, hashcodes, nsyms, 0);
7426 free (hashcodes);
7427
4b48e2f6 7428 if (bucketcount == 0 && nsyms > 0)
fdc90cb4 7429 return FALSE;
5a580b3a 7430
fdc90cb4
JJ
7431 elf_hash_table (info)->bucketcount = bucketcount;
7432
3d4d4302 7433 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
7434 BFD_ASSERT (s != NULL);
7435 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7436 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 7437 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7438 if (s->contents == NULL)
7439 return FALSE;
7440
7441 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7442 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7443 s->contents + hash_entry_size);
7444 }
7445
7446 if (info->emit_gnu_hash)
7447 {
7448 size_t i, cnt;
7449 unsigned char *contents;
7450 struct collect_gnu_hash_codes cinfo;
7451 bfd_size_type amt;
7452 size_t bucketcount;
7453
7454 memset (&cinfo, 0, sizeof (cinfo));
7455
7456 /* Compute the hash values for all exported symbols. At the same
7457 time store the values in an array so that we could use them for
7458 optimizations. */
7459 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 7460 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
7461 if (cinfo.hashcodes == NULL)
7462 return FALSE;
7463
7464 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7465 cinfo.min_dynindx = -1;
7466 cinfo.output_bfd = output_bfd;
7467 cinfo.bed = bed;
7468
7469 /* Put all hash values in HASHCODES. */
7470 elf_link_hash_traverse (elf_hash_table (info),
7471 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 7472 if (cinfo.error)
4dd07732
AM
7473 {
7474 free (cinfo.hashcodes);
7475 return FALSE;
7476 }
fdc90cb4
JJ
7477
7478 bucketcount
7479 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7480
7481 if (bucketcount == 0)
7482 {
7483 free (cinfo.hashcodes);
7484 return FALSE;
7485 }
7486
f16a9783 7487 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
fdc90cb4
JJ
7488 BFD_ASSERT (s != NULL);
7489
7490 if (cinfo.nsyms == 0)
7491 {
f16a9783 7492 /* Empty .gnu.hash or .MIPS.xhash section is special. */
fdc90cb4
JJ
7493 BFD_ASSERT (cinfo.min_dynindx == -1);
7494 free (cinfo.hashcodes);
7495 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 7496 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7497 if (contents == NULL)
7498 return FALSE;
7499 s->contents = contents;
7500 /* 1 empty bucket. */
7501 bfd_put_32 (output_bfd, 1, contents);
7502 /* SYMIDX above the special symbol 0. */
7503 bfd_put_32 (output_bfd, 1, contents + 4);
7504 /* Just one word for bitmask. */
7505 bfd_put_32 (output_bfd, 1, contents + 8);
7506 /* Only hash fn bloom filter. */
7507 bfd_put_32 (output_bfd, 0, contents + 12);
7508 /* No hashes are valid - empty bitmask. */
7509 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7510 /* No hashes in the only bucket. */
7511 bfd_put_32 (output_bfd, 0,
7512 contents + 16 + bed->s->arch_size / 8);
7513 }
7514 else
7515 {
9e6619e2 7516 unsigned long int maskwords, maskbitslog2, x;
0b33793d 7517 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 7518
9e6619e2
AM
7519 x = cinfo.nsyms;
7520 maskbitslog2 = 1;
7521 while ((x >>= 1) != 0)
7522 ++maskbitslog2;
fdc90cb4
JJ
7523 if (maskbitslog2 < 3)
7524 maskbitslog2 = 5;
7525 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7526 maskbitslog2 = maskbitslog2 + 3;
7527 else
7528 maskbitslog2 = maskbitslog2 + 2;
7529 if (bed->s->arch_size == 64)
7530 {
7531 if (maskbitslog2 == 5)
7532 maskbitslog2 = 6;
7533 cinfo.shift1 = 6;
7534 }
7535 else
7536 cinfo.shift1 = 5;
7537 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 7538 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
7539 cinfo.maskbits = 1 << maskbitslog2;
7540 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7541 amt = bucketcount * sizeof (unsigned long int) * 2;
7542 amt += maskwords * sizeof (bfd_vma);
a50b1753 7543 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
7544 if (cinfo.bitmask == NULL)
7545 {
7546 free (cinfo.hashcodes);
7547 return FALSE;
7548 }
7549
a50b1753 7550 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
7551 cinfo.indx = cinfo.counts + bucketcount;
7552 cinfo.symindx = dynsymcount - cinfo.nsyms;
7553 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7554
7555 /* Determine how often each hash bucket is used. */
7556 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7557 for (i = 0; i < cinfo.nsyms; ++i)
7558 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7559
7560 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7561 if (cinfo.counts[i] != 0)
7562 {
7563 cinfo.indx[i] = cnt;
7564 cnt += cinfo.counts[i];
7565 }
7566 BFD_ASSERT (cnt == dynsymcount);
7567 cinfo.bucketcount = bucketcount;
7568 cinfo.local_indx = cinfo.min_dynindx;
7569
7570 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7571 s->size += cinfo.maskbits / 8;
f16a9783
MS
7572 if (bed->record_xhash_symbol != NULL)
7573 s->size += cinfo.nsyms * 4;
a50b1753 7574 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7575 if (contents == NULL)
7576 {
7577 free (cinfo.bitmask);
7578 free (cinfo.hashcodes);
7579 return FALSE;
7580 }
7581
7582 s->contents = contents;
7583 bfd_put_32 (output_bfd, bucketcount, contents);
7584 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7585 bfd_put_32 (output_bfd, maskwords, contents + 8);
7586 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7587 contents += 16 + cinfo.maskbits / 8;
7588
7589 for (i = 0; i < bucketcount; ++i)
7590 {
7591 if (cinfo.counts[i] == 0)
7592 bfd_put_32 (output_bfd, 0, contents);
7593 else
7594 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7595 contents += 4;
7596 }
7597
7598 cinfo.contents = contents;
7599
f16a9783
MS
7600 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7601 /* Renumber dynamic symbols, if populating .gnu.hash section.
7602 If using .MIPS.xhash, populate the translation table. */
fdc90cb4 7603 elf_link_hash_traverse (elf_hash_table (info),
f16a9783 7604 elf_gnu_hash_process_symidx, &cinfo);
fdc90cb4
JJ
7605
7606 contents = s->contents + 16;
7607 for (i = 0; i < maskwords; ++i)
7608 {
7609 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7610 contents);
7611 contents += bed->s->arch_size / 8;
7612 }
7613
7614 free (cinfo.bitmask);
7615 free (cinfo.hashcodes);
7616 }
7617 }
5a580b3a 7618
3d4d4302 7619 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
7620 BFD_ASSERT (s != NULL);
7621
4ad4eba5 7622 elf_finalize_dynstr (output_bfd, info);
5a580b3a 7623
eea6121a 7624 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
7625
7626 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7627 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7628 return FALSE;
7629 }
7630
7631 return TRUE;
7632}
4d269e42 7633\f
4d269e42
AM
7634/* Make sure sec_info_type is cleared if sec_info is cleared too. */
7635
7636static void
7637merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7638 asection *sec)
7639{
dbaa2011
AM
7640 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7641 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
7642}
7643
7644/* Finish SHF_MERGE section merging. */
7645
7646bfd_boolean
630993ec 7647_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
7648{
7649 bfd *ibfd;
7650 asection *sec;
7651
7652 if (!is_elf_hash_table (info->hash))
7653 return FALSE;
7654
c72f2fb2 7655 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
7656 if ((ibfd->flags & DYNAMIC) == 0
7657 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
7658 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7659 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
7660 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7661 if ((sec->flags & SEC_MERGE) != 0
7662 && !bfd_is_abs_section (sec->output_section))
7663 {
7664 struct bfd_elf_section_data *secdata;
7665
7666 secdata = elf_section_data (sec);
630993ec 7667 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
7668 &elf_hash_table (info)->merge_info,
7669 sec, &secdata->sec_info))
7670 return FALSE;
7671 else if (secdata->sec_info)
dbaa2011 7672 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
7673 }
7674
7675 if (elf_hash_table (info)->merge_info != NULL)
630993ec 7676 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
7677 merge_sections_remove_hook);
7678 return TRUE;
7679}
7680
7681/* Create an entry in an ELF linker hash table. */
7682
7683struct bfd_hash_entry *
7684_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7685 struct bfd_hash_table *table,
7686 const char *string)
7687{
7688 /* Allocate the structure if it has not already been allocated by a
7689 subclass. */
7690 if (entry == NULL)
7691 {
a50b1753 7692 entry = (struct bfd_hash_entry *)
ca4be51c 7693 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
7694 if (entry == NULL)
7695 return entry;
7696 }
7697
7698 /* Call the allocation method of the superclass. */
7699 entry = _bfd_link_hash_newfunc (entry, table, string);
7700 if (entry != NULL)
7701 {
7702 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7703 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7704
7705 /* Set local fields. */
7706 ret->indx = -1;
7707 ret->dynindx = -1;
7708 ret->got = htab->init_got_refcount;
7709 ret->plt = htab->init_plt_refcount;
7710 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7711 - offsetof (struct elf_link_hash_entry, size)));
7712 /* Assume that we have been called by a non-ELF symbol reader.
7713 This flag is then reset by the code which reads an ELF input
7714 file. This ensures that a symbol created by a non-ELF symbol
7715 reader will have the flag set correctly. */
7716 ret->non_elf = 1;
7717 }
7718
7719 return entry;
7720}
7721
7722/* Copy data from an indirect symbol to its direct symbol, hiding the
7723 old indirect symbol. Also used for copying flags to a weakdef. */
7724
7725void
7726_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7727 struct elf_link_hash_entry *dir,
7728 struct elf_link_hash_entry *ind)
7729{
7730 struct elf_link_hash_table *htab;
7731
ad172eaa
L
7732 if (ind->dyn_relocs != NULL)
7733 {
7734 if (dir->dyn_relocs != NULL)
7735 {
7736 struct elf_dyn_relocs **pp;
7737 struct elf_dyn_relocs *p;
7738
7739 /* Add reloc counts against the indirect sym to the direct sym
7740 list. Merge any entries against the same section. */
7741 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
7742 {
7743 struct elf_dyn_relocs *q;
7744
7745 for (q = dir->dyn_relocs; q != NULL; q = q->next)
7746 if (q->sec == p->sec)
7747 {
7748 q->pc_count += p->pc_count;
7749 q->count += p->count;
7750 *pp = p->next;
7751 break;
7752 }
7753 if (q == NULL)
7754 pp = &p->next;
7755 }
7756 *pp = dir->dyn_relocs;
7757 }
7758
7759 dir->dyn_relocs = ind->dyn_relocs;
7760 ind->dyn_relocs = NULL;
7761 }
7762
4d269e42 7763 /* Copy down any references that we may have already seen to the
e81830c5 7764 symbol which just became indirect. */
4d269e42 7765
422f1182 7766 if (dir->versioned != versioned_hidden)
e81830c5
AM
7767 dir->ref_dynamic |= ind->ref_dynamic;
7768 dir->ref_regular |= ind->ref_regular;
7769 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7770 dir->non_got_ref |= ind->non_got_ref;
7771 dir->needs_plt |= ind->needs_plt;
7772 dir->pointer_equality_needed |= ind->pointer_equality_needed;
4d269e42
AM
7773
7774 if (ind->root.type != bfd_link_hash_indirect)
7775 return;
7776
7777 /* Copy over the global and procedure linkage table refcount entries.
7778 These may have been already set up by a check_relocs routine. */
7779 htab = elf_hash_table (info);
7780 if (ind->got.refcount > htab->init_got_refcount.refcount)
7781 {
7782 if (dir->got.refcount < 0)
7783 dir->got.refcount = 0;
7784 dir->got.refcount += ind->got.refcount;
7785 ind->got.refcount = htab->init_got_refcount.refcount;
7786 }
7787
7788 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7789 {
7790 if (dir->plt.refcount < 0)
7791 dir->plt.refcount = 0;
7792 dir->plt.refcount += ind->plt.refcount;
7793 ind->plt.refcount = htab->init_plt_refcount.refcount;
7794 }
7795
7796 if (ind->dynindx != -1)
7797 {
7798 if (dir->dynindx != -1)
7799 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7800 dir->dynindx = ind->dynindx;
7801 dir->dynstr_index = ind->dynstr_index;
7802 ind->dynindx = -1;
7803 ind->dynstr_index = 0;
7804 }
7805}
7806
7807void
7808_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7809 struct elf_link_hash_entry *h,
7810 bfd_boolean force_local)
7811{
3aa14d16
L
7812 /* STT_GNU_IFUNC symbol must go through PLT. */
7813 if (h->type != STT_GNU_IFUNC)
7814 {
7815 h->plt = elf_hash_table (info)->init_plt_offset;
7816 h->needs_plt = 0;
7817 }
4d269e42
AM
7818 if (force_local)
7819 {
7820 h->forced_local = 1;
7821 if (h->dynindx != -1)
7822 {
4d269e42
AM
7823 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7824 h->dynstr_index);
641338d8
AM
7825 h->dynindx = -1;
7826 h->dynstr_index = 0;
4d269e42
AM
7827 }
7828 }
7829}
7830
34a87bb0
L
7831/* Hide a symbol. */
7832
7833void
7834_bfd_elf_link_hide_symbol (bfd *output_bfd,
7835 struct bfd_link_info *info,
7836 struct bfd_link_hash_entry *h)
7837{
7838 if (is_elf_hash_table (info->hash))
7839 {
7840 const struct elf_backend_data *bed
7841 = get_elf_backend_data (output_bfd);
7842 struct elf_link_hash_entry *eh
7843 = (struct elf_link_hash_entry *) h;
7844 bed->elf_backend_hide_symbol (info, eh, TRUE);
7845 eh->def_dynamic = 0;
7846 eh->ref_dynamic = 0;
7847 eh->dynamic_def = 0;
7848 }
7849}
7850
7bf52ea2
AM
7851/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7852 caller. */
4d269e42
AM
7853
7854bfd_boolean
7855_bfd_elf_link_hash_table_init
7856 (struct elf_link_hash_table *table,
7857 bfd *abfd,
7858 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7859 struct bfd_hash_table *,
7860 const char *),
4dfe6ac6
NC
7861 unsigned int entsize,
7862 enum elf_target_id target_id)
4d269e42
AM
7863{
7864 bfd_boolean ret;
7865 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7866
4d269e42
AM
7867 table->init_got_refcount.refcount = can_refcount - 1;
7868 table->init_plt_refcount.refcount = can_refcount - 1;
7869 table->init_got_offset.offset = -(bfd_vma) 1;
7870 table->init_plt_offset.offset = -(bfd_vma) 1;
7871 /* The first dynamic symbol is a dummy. */
7872 table->dynsymcount = 1;
7873
7874 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7875
4d269e42 7876 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7877 table->hash_table_id = target_id;
90c14f0c 7878 table->target_os = get_elf_backend_data (abfd)->target_os;
4d269e42
AM
7879
7880 return ret;
7881}
7882
7883/* Create an ELF linker hash table. */
7884
7885struct bfd_link_hash_table *
7886_bfd_elf_link_hash_table_create (bfd *abfd)
7887{
7888 struct elf_link_hash_table *ret;
986f0783 7889 size_t amt = sizeof (struct elf_link_hash_table);
4d269e42 7890
7bf52ea2 7891 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7892 if (ret == NULL)
7893 return NULL;
7894
7895 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7896 sizeof (struct elf_link_hash_entry),
7897 GENERIC_ELF_DATA))
4d269e42
AM
7898 {
7899 free (ret);
7900 return NULL;
7901 }
d495ab0d 7902 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7903
7904 return &ret->root;
7905}
7906
9f7c3e5e
AM
7907/* Destroy an ELF linker hash table. */
7908
7909void
d495ab0d 7910_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7911{
d495ab0d
AM
7912 struct elf_link_hash_table *htab;
7913
7914 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7915 if (htab->dynstr != NULL)
7916 _bfd_elf_strtab_free (htab->dynstr);
7917 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7918 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7919}
7920
4d269e42
AM
7921/* This is a hook for the ELF emulation code in the generic linker to
7922 tell the backend linker what file name to use for the DT_NEEDED
7923 entry for a dynamic object. */
7924
7925void
7926bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7927{
7928 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7929 && bfd_get_format (abfd) == bfd_object)
7930 elf_dt_name (abfd) = name;
7931}
7932
7933int
7934bfd_elf_get_dyn_lib_class (bfd *abfd)
7935{
7936 int lib_class;
7937 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7938 && bfd_get_format (abfd) == bfd_object)
7939 lib_class = elf_dyn_lib_class (abfd);
7940 else
7941 lib_class = 0;
7942 return lib_class;
7943}
7944
7945void
7946bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7947{
7948 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7949 && bfd_get_format (abfd) == bfd_object)
7950 elf_dyn_lib_class (abfd) = lib_class;
7951}
7952
7953/* Get the list of DT_NEEDED entries for a link. This is a hook for
7954 the linker ELF emulation code. */
7955
7956struct bfd_link_needed_list *
7957bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7958 struct bfd_link_info *info)
7959{
7960 if (! is_elf_hash_table (info->hash))
7961 return NULL;
7962 return elf_hash_table (info)->needed;
7963}
7964
7965/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7966 hook for the linker ELF emulation code. */
7967
7968struct bfd_link_needed_list *
7969bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7970 struct bfd_link_info *info)
7971{
7972 if (! is_elf_hash_table (info->hash))
7973 return NULL;
7974 return elf_hash_table (info)->runpath;
7975}
7976
7977/* Get the name actually used for a dynamic object for a link. This
7978 is the SONAME entry if there is one. Otherwise, it is the string
7979 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7980
7981const char *
7982bfd_elf_get_dt_soname (bfd *abfd)
7983{
7984 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7985 && bfd_get_format (abfd) == bfd_object)
7986 return elf_dt_name (abfd);
7987 return NULL;
7988}
7989
7990/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7991 the ELF linker emulation code. */
7992
7993bfd_boolean
7994bfd_elf_get_bfd_needed_list (bfd *abfd,
7995 struct bfd_link_needed_list **pneeded)
7996{
7997 asection *s;
7998 bfd_byte *dynbuf = NULL;
cb33740c 7999 unsigned int elfsec;
4d269e42
AM
8000 unsigned long shlink;
8001 bfd_byte *extdyn, *extdynend;
8002 size_t extdynsize;
8003 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
8004
8005 *pneeded = NULL;
8006
8007 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
8008 || bfd_get_format (abfd) != bfd_object)
8009 return TRUE;
8010
8011 s = bfd_get_section_by_name (abfd, ".dynamic");
8012 if (s == NULL || s->size == 0)
8013 return TRUE;
8014
8015 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
8016 goto error_return;
8017
8018 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 8019 if (elfsec == SHN_BAD)
4d269e42
AM
8020 goto error_return;
8021
8022 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 8023
4d269e42
AM
8024 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
8025 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
8026
8027 extdyn = dynbuf;
8028 extdynend = extdyn + s->size;
8029 for (; extdyn < extdynend; extdyn += extdynsize)
8030 {
8031 Elf_Internal_Dyn dyn;
8032
8033 (*swap_dyn_in) (abfd, extdyn, &dyn);
8034
8035 if (dyn.d_tag == DT_NULL)
8036 break;
8037
8038 if (dyn.d_tag == DT_NEEDED)
8039 {
8040 const char *string;
8041 struct bfd_link_needed_list *l;
8042 unsigned int tagv = dyn.d_un.d_val;
986f0783 8043 size_t amt;
4d269e42
AM
8044
8045 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8046 if (string == NULL)
8047 goto error_return;
8048
8049 amt = sizeof *l;
a50b1753 8050 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
8051 if (l == NULL)
8052 goto error_return;
8053
8054 l->by = abfd;
8055 l->name = string;
8056 l->next = *pneeded;
8057 *pneeded = l;
8058 }
8059 }
8060
8061 free (dynbuf);
8062
8063 return TRUE;
8064
8065 error_return:
c9594989 8066 free (dynbuf);
4d269e42
AM
8067 return FALSE;
8068}
8069
8070struct elf_symbuf_symbol
8071{
8072 unsigned long st_name; /* Symbol name, index in string tbl */
8073 unsigned char st_info; /* Type and binding attributes */
8074 unsigned char st_other; /* Visibilty, and target specific */
8075};
8076
8077struct elf_symbuf_head
8078{
8079 struct elf_symbuf_symbol *ssym;
ef53be89 8080 size_t count;
4d269e42
AM
8081 unsigned int st_shndx;
8082};
8083
8084struct elf_symbol
8085{
8086 union
8087 {
8088 Elf_Internal_Sym *isym;
8089 struct elf_symbuf_symbol *ssym;
dcea6a95 8090 void *p;
4d269e42
AM
8091 } u;
8092 const char *name;
8093};
8094
8095/* Sort references to symbols by ascending section number. */
8096
8097static int
8098elf_sort_elf_symbol (const void *arg1, const void *arg2)
8099{
8100 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8101 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8102
dcea6a95
AM
8103 if (s1->st_shndx != s2->st_shndx)
8104 return s1->st_shndx > s2->st_shndx ? 1 : -1;
8105 /* Final sort by the address of the sym in the symbuf ensures
8106 a stable sort. */
8107 if (s1 != s2)
8108 return s1 > s2 ? 1 : -1;
8109 return 0;
4d269e42
AM
8110}
8111
8112static int
8113elf_sym_name_compare (const void *arg1, const void *arg2)
8114{
8115 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8116 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
dcea6a95
AM
8117 int ret = strcmp (s1->name, s2->name);
8118 if (ret != 0)
8119 return ret;
8120 if (s1->u.p != s2->u.p)
8121 return s1->u.p > s2->u.p ? 1 : -1;
8122 return 0;
4d269e42
AM
8123}
8124
8125static struct elf_symbuf_head *
ef53be89 8126elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
4d269e42 8127{
14b1c01e 8128 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
8129 struct elf_symbuf_symbol *ssym;
8130 struct elf_symbuf_head *ssymbuf, *ssymhead;
446f7ed5 8131 size_t i, shndx_count, total_size, amt;
4d269e42 8132
446f7ed5
AM
8133 amt = symcount * sizeof (*indbuf);
8134 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
4d269e42
AM
8135 if (indbuf == NULL)
8136 return NULL;
8137
994b2513
L
8138 /* NB: When checking if 2 sections define the same set of local and
8139 global symbols, ignore both undefined and section symbols in the
8140 symbol table. */
4d269e42 8141 for (ind = indbuf, i = 0; i < symcount; i++)
994b2513
L
8142 if (isymbuf[i].st_shndx != SHN_UNDEF
8143 && ELF_ST_TYPE (isymbuf[i].st_info) != STT_SECTION)
4d269e42
AM
8144 *ind++ = &isymbuf[i];
8145 indbufend = ind;
8146
8147 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8148 elf_sort_elf_symbol);
8149
8150 shndx_count = 0;
8151 if (indbufend > indbuf)
8152 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8153 if (ind[0]->st_shndx != ind[1]->st_shndx)
8154 shndx_count++;
8155
3ae181ee
L
8156 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8157 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 8158 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
8159 if (ssymbuf == NULL)
8160 {
8161 free (indbuf);
8162 return NULL;
8163 }
8164
3ae181ee 8165 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
8166 ssymbuf->ssym = NULL;
8167 ssymbuf->count = shndx_count;
8168 ssymbuf->st_shndx = 0;
8169 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8170 {
8171 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8172 {
8173 ssymhead++;
8174 ssymhead->ssym = ssym;
8175 ssymhead->count = 0;
8176 ssymhead->st_shndx = (*ind)->st_shndx;
8177 }
8178 ssym->st_name = (*ind)->st_name;
8179 ssym->st_info = (*ind)->st_info;
8180 ssym->st_other = (*ind)->st_other;
8181 ssymhead->count++;
8182 }
ef53be89 8183 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
3ae181ee
L
8184 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
8185 == total_size));
4d269e42
AM
8186
8187 free (indbuf);
8188 return ssymbuf;
8189}
8190
8191/* Check if 2 sections define the same set of local and global
8192 symbols. */
8193
8f317e31 8194static bfd_boolean
4d269e42
AM
8195bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8196 struct bfd_link_info *info)
8197{
8198 bfd *bfd1, *bfd2;
8199 const struct elf_backend_data *bed1, *bed2;
8200 Elf_Internal_Shdr *hdr1, *hdr2;
ef53be89 8201 size_t symcount1, symcount2;
4d269e42
AM
8202 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8203 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8204 Elf_Internal_Sym *isym, *isymend;
8205 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
ef53be89 8206 size_t count1, count2, i;
cb33740c 8207 unsigned int shndx1, shndx2;
4d269e42
AM
8208 bfd_boolean result;
8209
8210 bfd1 = sec1->owner;
8211 bfd2 = sec2->owner;
8212
4d269e42
AM
8213 /* Both sections have to be in ELF. */
8214 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8215 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8216 return FALSE;
8217
8218 if (elf_section_type (sec1) != elf_section_type (sec2))
8219 return FALSE;
8220
4d269e42
AM
8221 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8222 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 8223 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
8224 return FALSE;
8225
8226 bed1 = get_elf_backend_data (bfd1);
8227 bed2 = get_elf_backend_data (bfd2);
8228 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8229 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8230 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8231 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8232
8233 if (symcount1 == 0 || symcount2 == 0)
8234 return FALSE;
8235
8236 result = FALSE;
8237 isymbuf1 = NULL;
8238 isymbuf2 = NULL;
a50b1753
NC
8239 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8240 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
8241
8242 if (ssymbuf1 == NULL)
8243 {
8244 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8245 NULL, NULL, NULL);
8246 if (isymbuf1 == NULL)
8247 goto done;
8248
67411cbf 8249 if (info != NULL && !info->reduce_memory_overheads)
dcea6a95
AM
8250 {
8251 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8252 elf_tdata (bfd1)->symbuf = ssymbuf1;
8253 }
4d269e42
AM
8254 }
8255
8256 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8257 {
8258 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8259 NULL, NULL, NULL);
8260 if (isymbuf2 == NULL)
8261 goto done;
8262
67411cbf 8263 if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
dcea6a95
AM
8264 {
8265 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8266 elf_tdata (bfd2)->symbuf = ssymbuf2;
8267 }
4d269e42
AM
8268 }
8269
8270 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8271 {
8272 /* Optimized faster version. */
ef53be89 8273 size_t lo, hi, mid;
4d269e42
AM
8274 struct elf_symbol *symp;
8275 struct elf_symbuf_symbol *ssym, *ssymend;
8276
8277 lo = 0;
8278 hi = ssymbuf1->count;
8279 ssymbuf1++;
8280 count1 = 0;
8281 while (lo < hi)
8282 {
8283 mid = (lo + hi) / 2;
cb33740c 8284 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 8285 hi = mid;
cb33740c 8286 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
8287 lo = mid + 1;
8288 else
8289 {
8290 count1 = ssymbuf1[mid].count;
8291 ssymbuf1 += mid;
8292 break;
8293 }
8294 }
8295
8296 lo = 0;
8297 hi = ssymbuf2->count;
8298 ssymbuf2++;
8299 count2 = 0;
8300 while (lo < hi)
8301 {
8302 mid = (lo + hi) / 2;
cb33740c 8303 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 8304 hi = mid;
cb33740c 8305 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
8306 lo = mid + 1;
8307 else
8308 {
8309 count2 = ssymbuf2[mid].count;
8310 ssymbuf2 += mid;
8311 break;
8312 }
8313 }
8314
8315 if (count1 == 0 || count2 == 0 || count1 != count2)
8316 goto done;
8317
ca4be51c
AM
8318 symtable1
8319 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8320 symtable2
8321 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
8322 if (symtable1 == NULL || symtable2 == NULL)
8323 goto done;
8324
8325 symp = symtable1;
8326 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
8327 ssym < ssymend; ssym++, symp++)
8328 {
8329 symp->u.ssym = ssym;
8330 symp->name = bfd_elf_string_from_elf_section (bfd1,
8331 hdr1->sh_link,
8332 ssym->st_name);
8333 }
8334
8335 symp = symtable2;
8336 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8337 ssym < ssymend; ssym++, symp++)
8338 {
8339 symp->u.ssym = ssym;
8340 symp->name = bfd_elf_string_from_elf_section (bfd2,
8341 hdr2->sh_link,
8342 ssym->st_name);
8343 }
8344
8345 /* Sort symbol by name. */
8346 qsort (symtable1, count1, sizeof (struct elf_symbol),
8347 elf_sym_name_compare);
8348 qsort (symtable2, count1, sizeof (struct elf_symbol),
8349 elf_sym_name_compare);
8350
8351 for (i = 0; i < count1; i++)
8352 /* Two symbols must have the same binding, type and name. */
8353 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8354 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8355 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8356 goto done;
8357
8358 result = TRUE;
8359 goto done;
8360 }
8361
a50b1753
NC
8362 symtable1 = (struct elf_symbol *)
8363 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8364 symtable2 = (struct elf_symbol *)
8365 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
8366 if (symtable1 == NULL || symtable2 == NULL)
8367 goto done;
8368
8369 /* Count definitions in the section. */
8370 count1 = 0;
8371 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 8372 if (isym->st_shndx == shndx1)
4d269e42
AM
8373 symtable1[count1++].u.isym = isym;
8374
8375 count2 = 0;
8376 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 8377 if (isym->st_shndx == shndx2)
4d269e42
AM
8378 symtable2[count2++].u.isym = isym;
8379
8380 if (count1 == 0 || count2 == 0 || count1 != count2)
8381 goto done;
8382
8383 for (i = 0; i < count1; i++)
8384 symtable1[i].name
8385 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8386 symtable1[i].u.isym->st_name);
8387
8388 for (i = 0; i < count2; i++)
8389 symtable2[i].name
8390 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8391 symtable2[i].u.isym->st_name);
8392
8393 /* Sort symbol by name. */
8394 qsort (symtable1, count1, sizeof (struct elf_symbol),
8395 elf_sym_name_compare);
8396 qsort (symtable2, count1, sizeof (struct elf_symbol),
8397 elf_sym_name_compare);
8398
8399 for (i = 0; i < count1; i++)
8400 /* Two symbols must have the same binding, type and name. */
8401 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8402 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8403 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8404 goto done;
8405
8406 result = TRUE;
8407
dc1e8a47 8408 done:
c9594989
AM
8409 free (symtable1);
8410 free (symtable2);
8411 free (isymbuf1);
8412 free (isymbuf2);
4d269e42
AM
8413
8414 return result;
8415}
8416
8417/* Return TRUE if 2 section types are compatible. */
8418
8419bfd_boolean
8420_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8421 bfd *bbfd, const asection *bsec)
8422{
8423 if (asec == NULL
8424 || bsec == NULL
8425 || abfd->xvec->flavour != bfd_target_elf_flavour
8426 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8427 return TRUE;
8428
8429 return elf_section_type (asec) == elf_section_type (bsec);
8430}
8431\f
c152c796
AM
8432/* Final phase of ELF linker. */
8433
8434/* A structure we use to avoid passing large numbers of arguments. */
8435
8436struct elf_final_link_info
8437{
8438 /* General link information. */
8439 struct bfd_link_info *info;
8440 /* Output BFD. */
8441 bfd *output_bfd;
8442 /* Symbol string table. */
ef10c3ac 8443 struct elf_strtab_hash *symstrtab;
c152c796
AM
8444 /* .hash section. */
8445 asection *hash_sec;
8446 /* symbol version section (.gnu.version). */
8447 asection *symver_sec;
8448 /* Buffer large enough to hold contents of any section. */
8449 bfd_byte *contents;
8450 /* Buffer large enough to hold external relocs of any section. */
8451 void *external_relocs;
8452 /* Buffer large enough to hold internal relocs of any section. */
8453 Elf_Internal_Rela *internal_relocs;
8454 /* Buffer large enough to hold external local symbols of any input
8455 BFD. */
8456 bfd_byte *external_syms;
8457 /* And a buffer for symbol section indices. */
8458 Elf_External_Sym_Shndx *locsym_shndx;
8459 /* Buffer large enough to hold internal local symbols of any input
8460 BFD. */
8461 Elf_Internal_Sym *internal_syms;
8462 /* Array large enough to hold a symbol index for each local symbol
8463 of any input BFD. */
8464 long *indices;
8465 /* Array large enough to hold a section pointer for each local
8466 symbol of any input BFD. */
8467 asection **sections;
ef10c3ac 8468 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 8469 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
8470 /* Number of STT_FILE syms seen. */
8471 size_t filesym_count;
496afd17
L
8472 /* Local symbol hash table. */
8473 struct bfd_hash_table local_hash_table;
c152c796
AM
8474};
8475
496afd17
L
8476struct local_hash_entry
8477{
8478 /* Base hash table entry structure. */
8479 struct bfd_hash_entry root;
8480 /* Size of the local symbol name. */
8481 size_t size;
8482 /* Number of the duplicated local symbol names. */
8483 long count;
8484};
8485
8486/* Create an entry in the local symbol hash table. */
8487
8488static struct bfd_hash_entry *
8489local_hash_newfunc (struct bfd_hash_entry *entry,
8490 struct bfd_hash_table *table,
8491 const char *string)
8492{
8493
8494 /* Allocate the structure if it has not already been allocated by a
8495 subclass. */
8496 if (entry == NULL)
8497 {
8498 entry = bfd_hash_allocate (table,
8499 sizeof (struct local_hash_entry));
8500 if (entry == NULL)
8501 return entry;
8502 }
8503
8504 /* Call the allocation method of the superclass. */
8505 entry = bfd_hash_newfunc (entry, table, string);
8506 if (entry != NULL)
8507 {
8508 ((struct local_hash_entry *) entry)->count = 0;
8509 ((struct local_hash_entry *) entry)->size = 0;
8510 }
8511
8512 return entry;
8513}
8514
c152c796
AM
8515/* This struct is used to pass information to elf_link_output_extsym. */
8516
8517struct elf_outext_info
8518{
8519 bfd_boolean failed;
8520 bfd_boolean localsyms;
34a79995 8521 bfd_boolean file_sym_done;
8b127cbc 8522 struct elf_final_link_info *flinfo;
c152c796
AM
8523};
8524
d9352518
DB
8525
8526/* Support for evaluating a complex relocation.
8527
8528 Complex relocations are generalized, self-describing relocations. The
8529 implementation of them consists of two parts: complex symbols, and the
a0c8462f 8530 relocations themselves.
d9352518 8531
4b69ce9b 8532 The relocations use a reserved elf-wide relocation type code (R_RELC
d9352518
DB
8533 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8534 information (start bit, end bit, word width, etc) into the addend. This
8535 information is extracted from CGEN-generated operand tables within gas.
8536
4b69ce9b 8537 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
d9352518
DB
8538 internal) representing prefix-notation expressions, including but not
8539 limited to those sorts of expressions normally encoded as addends in the
8540 addend field. The symbol mangling format is:
8541
8542 <node> := <literal>
07d6d2b8
AM
8543 | <unary-operator> ':' <node>
8544 | <binary-operator> ':' <node> ':' <node>
d9352518
DB
8545 ;
8546
8547 <literal> := 's' <digits=N> ':' <N character symbol name>
07d6d2b8 8548 | 'S' <digits=N> ':' <N character section name>
d9352518
DB
8549 | '#' <hexdigits>
8550 ;
8551
8552 <binary-operator> := as in C
8553 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8554
8555static void
a0c8462f
AM
8556set_symbol_value (bfd *bfd_with_globals,
8557 Elf_Internal_Sym *isymbuf,
8558 size_t locsymcount,
8559 size_t symidx,
8560 bfd_vma val)
d9352518 8561{
8977835c
AM
8562 struct elf_link_hash_entry **sym_hashes;
8563 struct elf_link_hash_entry *h;
8564 size_t extsymoff = locsymcount;
d9352518 8565
8977835c 8566 if (symidx < locsymcount)
d9352518 8567 {
8977835c
AM
8568 Elf_Internal_Sym *sym;
8569
8570 sym = isymbuf + symidx;
8571 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8572 {
8573 /* It is a local symbol: move it to the
8574 "absolute" section and give it a value. */
8575 sym->st_shndx = SHN_ABS;
8576 sym->st_value = val;
8577 return;
8578 }
8579 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8580 extsymoff = 0;
d9352518 8581 }
8977835c
AM
8582
8583 /* It is a global symbol: set its link type
8584 to "defined" and give it a value. */
8585
8586 sym_hashes = elf_sym_hashes (bfd_with_globals);
8587 h = sym_hashes [symidx - extsymoff];
8588 while (h->root.type == bfd_link_hash_indirect
8589 || h->root.type == bfd_link_hash_warning)
8590 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8591 h->root.type = bfd_link_hash_defined;
8592 h->root.u.def.value = val;
8593 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
8594}
8595
a0c8462f
AM
8596static bfd_boolean
8597resolve_symbol (const char *name,
8598 bfd *input_bfd,
8b127cbc 8599 struct elf_final_link_info *flinfo,
a0c8462f
AM
8600 bfd_vma *result,
8601 Elf_Internal_Sym *isymbuf,
8602 size_t locsymcount)
d9352518 8603{
a0c8462f
AM
8604 Elf_Internal_Sym *sym;
8605 struct bfd_link_hash_entry *global_entry;
8606 const char *candidate = NULL;
8607 Elf_Internal_Shdr *symtab_hdr;
8608 size_t i;
8609
d9352518
DB
8610 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8611
8612 for (i = 0; i < locsymcount; ++ i)
8613 {
8977835c 8614 sym = isymbuf + i;
d9352518
DB
8615
8616 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8617 continue;
8618
8619 candidate = bfd_elf_string_from_elf_section (input_bfd,
8620 symtab_hdr->sh_link,
8621 sym->st_name);
8622#ifdef DEBUG
0f02bbd9
AM
8623 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8624 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
8625#endif
8626 if (candidate && strcmp (candidate, name) == 0)
8627 {
8b127cbc 8628 asection *sec = flinfo->sections [i];
d9352518 8629
0f02bbd9
AM
8630 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8631 *result += sec->output_offset + sec->output_section->vma;
d9352518 8632#ifdef DEBUG
0f02bbd9
AM
8633 printf ("Found symbol with value %8.8lx\n",
8634 (unsigned long) *result);
d9352518
DB
8635#endif
8636 return TRUE;
8637 }
8638 }
8639
8640 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 8641 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 8642 FALSE, FALSE, TRUE);
d9352518
DB
8643 if (!global_entry)
8644 return FALSE;
a0c8462f 8645
d9352518
DB
8646 if (global_entry->type == bfd_link_hash_defined
8647 || global_entry->type == bfd_link_hash_defweak)
8648 {
a0c8462f
AM
8649 *result = (global_entry->u.def.value
8650 + global_entry->u.def.section->output_section->vma
8651 + global_entry->u.def.section->output_offset);
d9352518 8652#ifdef DEBUG
0f02bbd9
AM
8653 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8654 global_entry->root.string, (unsigned long) *result);
d9352518
DB
8655#endif
8656 return TRUE;
a0c8462f 8657 }
d9352518 8658
d9352518
DB
8659 return FALSE;
8660}
8661
37b01f6a
DG
8662/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8663 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8664 names like "foo.end" which is the end address of section "foo". */
07d6d2b8 8665
d9352518 8666static bfd_boolean
a0c8462f
AM
8667resolve_section (const char *name,
8668 asection *sections,
37b01f6a
DG
8669 bfd_vma *result,
8670 bfd * abfd)
d9352518 8671{
a0c8462f
AM
8672 asection *curr;
8673 unsigned int len;
d9352518 8674
a0c8462f 8675 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8676 if (strcmp (curr->name, name) == 0)
8677 {
8678 *result = curr->vma;
8679 return TRUE;
8680 }
8681
8682 /* Hmm. still haven't found it. try pseudo-section names. */
37b01f6a 8683 /* FIXME: This could be coded more efficiently... */
a0c8462f 8684 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8685 {
8686 len = strlen (curr->name);
a0c8462f 8687 if (len > strlen (name))
d9352518
DB
8688 continue;
8689
8690 if (strncmp (curr->name, name, len) == 0)
8691 {
8692 if (strncmp (".end", name + len, 4) == 0)
8693 {
61826503 8694 *result = (curr->vma
bb294208 8695 + curr->size / bfd_octets_per_byte (abfd, curr));
d9352518
DB
8696 return TRUE;
8697 }
8698
8699 /* Insert more pseudo-section names here, if you like. */
8700 }
8701 }
a0c8462f 8702
d9352518
DB
8703 return FALSE;
8704}
8705
8706static void
a0c8462f 8707undefined_reference (const char *reftype, const char *name)
d9352518 8708{
695344c0 8709 /* xgettext:c-format */
a0c8462f
AM
8710 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8711 reftype, name);
4b69ce9b 8712 bfd_set_error (bfd_error_bad_value);
d9352518
DB
8713}
8714
8715static bfd_boolean
a0c8462f
AM
8716eval_symbol (bfd_vma *result,
8717 const char **symp,
8718 bfd *input_bfd,
8b127cbc 8719 struct elf_final_link_info *flinfo,
a0c8462f
AM
8720 bfd_vma dot,
8721 Elf_Internal_Sym *isymbuf,
8722 size_t locsymcount,
8723 int signed_p)
d9352518 8724{
4b93929b
NC
8725 size_t len;
8726 size_t symlen;
a0c8462f
AM
8727 bfd_vma a;
8728 bfd_vma b;
4b93929b 8729 char symbuf[4096];
0f02bbd9 8730 const char *sym = *symp;
a0c8462f
AM
8731 const char *symend;
8732 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
8733
8734 len = strlen (sym);
8735 symend = sym + len;
8736
4b93929b 8737 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
8738 {
8739 bfd_set_error (bfd_error_invalid_operation);
8740 return FALSE;
8741 }
a0c8462f 8742
d9352518
DB
8743 switch (* sym)
8744 {
8745 case '.':
0f02bbd9
AM
8746 *result = dot;
8747 *symp = sym + 1;
d9352518
DB
8748 return TRUE;
8749
8750 case '#':
0f02bbd9
AM
8751 ++sym;
8752 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
8753 return TRUE;
8754
8755 case 'S':
8756 symbol_is_section = TRUE;
1a0670f3 8757 /* Fall through. */
a0c8462f 8758 case 's':
0f02bbd9
AM
8759 ++sym;
8760 symlen = strtol (sym, (char **) symp, 10);
8761 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 8762
4b93929b 8763 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
8764 {
8765 bfd_set_error (bfd_error_invalid_operation);
8766 return FALSE;
8767 }
8768
8769 memcpy (symbuf, sym, symlen);
a0c8462f 8770 symbuf[symlen] = '\0';
0f02bbd9 8771 *symp = sym + symlen;
a0c8462f
AM
8772
8773 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
8774 the symbol as a section, or vice-versa. so we're pretty liberal in our
8775 interpretation here; section means "try section first", not "must be a
8776 section", and likewise with symbol. */
8777
a0c8462f 8778 if (symbol_is_section)
d9352518 8779 {
37b01f6a 8780 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8b127cbc 8781 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8782 isymbuf, locsymcount))
d9352518
DB
8783 {
8784 undefined_reference ("section", symbuf);
8785 return FALSE;
8786 }
a0c8462f
AM
8787 }
8788 else
d9352518 8789 {
8b127cbc 8790 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8791 isymbuf, locsymcount)
8b127cbc 8792 && !resolve_section (symbuf, flinfo->output_bfd->sections,
37b01f6a 8793 result, input_bfd))
d9352518
DB
8794 {
8795 undefined_reference ("symbol", symbuf);
8796 return FALSE;
8797 }
8798 }
8799
8800 return TRUE;
a0c8462f 8801
d9352518
DB
8802 /* All that remains are operators. */
8803
8804#define UNARY_OP(op) \
8805 if (strncmp (sym, #op, strlen (#op)) == 0) \
8806 { \
8807 sym += strlen (#op); \
a0c8462f
AM
8808 if (*sym == ':') \
8809 ++sym; \
0f02bbd9 8810 *symp = sym; \
8b127cbc 8811 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8812 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8813 return FALSE; \
8814 if (signed_p) \
0f02bbd9 8815 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
8816 else \
8817 *result = op a; \
d9352518
DB
8818 return TRUE; \
8819 }
8820
4b69ce9b 8821#define BINARY_OP_HEAD(op) \
d9352518
DB
8822 if (strncmp (sym, #op, strlen (#op)) == 0) \
8823 { \
8824 sym += strlen (#op); \
a0c8462f
AM
8825 if (*sym == ':') \
8826 ++sym; \
0f02bbd9 8827 *symp = sym; \
8b127cbc 8828 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8829 isymbuf, locsymcount, signed_p)) \
a0c8462f 8830 return FALSE; \
0f02bbd9 8831 ++*symp; \
8b127cbc 8832 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 8833 isymbuf, locsymcount, signed_p)) \
4b69ce9b
AM
8834 return FALSE;
8835#define BINARY_OP_TAIL(op) \
a0c8462f 8836 if (signed_p) \
0f02bbd9 8837 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
8838 else \
8839 *result = a op b; \
d9352518
DB
8840 return TRUE; \
8841 }
4b69ce9b 8842#define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
d9352518
DB
8843
8844 default:
8845 UNARY_OP (0-);
4b69ce9b
AM
8846 BINARY_OP_HEAD (<<);
8847 if (b >= sizeof (a) * CHAR_BIT)
8848 {
8849 *result = 0;
8850 return TRUE;
8851 }
8852 signed_p = 0;
8853 BINARY_OP_TAIL (<<);
8854 BINARY_OP_HEAD (>>);
8855 if (b >= sizeof (a) * CHAR_BIT)
8856 {
8857 *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
8858 return TRUE;
8859 }
8860 BINARY_OP_TAIL (>>);
d9352518
DB
8861 BINARY_OP (==);
8862 BINARY_OP (!=);
8863 BINARY_OP (<=);
8864 BINARY_OP (>=);
8865 BINARY_OP (&&);
8866 BINARY_OP (||);
8867 UNARY_OP (~);
8868 UNARY_OP (!);
8869 BINARY_OP (*);
4b69ce9b
AM
8870 BINARY_OP_HEAD (/);
8871 if (b == 0)
8872 {
8873 _bfd_error_handler (_("division by zero"));
8874 bfd_set_error (bfd_error_bad_value);
8875 return FALSE;
8876 }
8877 BINARY_OP_TAIL (/);
8878 BINARY_OP_HEAD (%);
8879 if (b == 0)
8880 {
8881 _bfd_error_handler (_("division by zero"));
8882 bfd_set_error (bfd_error_bad_value);
8883 return FALSE;
8884 }
8885 BINARY_OP_TAIL (%);
d9352518
DB
8886 BINARY_OP (^);
8887 BINARY_OP (|);
8888 BINARY_OP (&);
8889 BINARY_OP (+);
8890 BINARY_OP (-);
8891 BINARY_OP (<);
8892 BINARY_OP (>);
8893#undef UNARY_OP
8894#undef BINARY_OP
8895 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8896 bfd_set_error (bfd_error_invalid_operation);
8897 return FALSE;
8898 }
8899}
8900
d9352518 8901static void
a0c8462f
AM
8902put_value (bfd_vma size,
8903 unsigned long chunksz,
8904 bfd *input_bfd,
8905 bfd_vma x,
8906 bfd_byte *location)
d9352518
DB
8907{
8908 location += (size - chunksz);
8909
41cd1ad1 8910 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
8911 {
8912 switch (chunksz)
8913 {
d9352518
DB
8914 case 1:
8915 bfd_put_8 (input_bfd, x, location);
41cd1ad1 8916 x >>= 8;
d9352518
DB
8917 break;
8918 case 2:
8919 bfd_put_16 (input_bfd, x, location);
41cd1ad1 8920 x >>= 16;
d9352518
DB
8921 break;
8922 case 4:
8923 bfd_put_32 (input_bfd, x, location);
65164438
NC
8924 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8925 x >>= 16;
8926 x >>= 16;
d9352518 8927 break;
d9352518 8928#ifdef BFD64
41cd1ad1 8929 case 8:
d9352518 8930 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
8931 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8932 x >>= 32;
8933 x >>= 32;
8934 break;
d9352518 8935#endif
41cd1ad1
NC
8936 default:
8937 abort ();
d9352518
DB
8938 break;
8939 }
8940 }
8941}
8942
a0c8462f
AM
8943static bfd_vma
8944get_value (bfd_vma size,
8945 unsigned long chunksz,
8946 bfd *input_bfd,
8947 bfd_byte *location)
d9352518 8948{
9b239e0e 8949 int shift;
d9352518
DB
8950 bfd_vma x = 0;
8951
9b239e0e
NC
8952 /* Sanity checks. */
8953 BFD_ASSERT (chunksz <= sizeof (x)
8954 && size >= chunksz
8955 && chunksz != 0
8956 && (size % chunksz) == 0
8957 && input_bfd != NULL
8958 && location != NULL);
8959
8960 if (chunksz == sizeof (x))
8961 {
8962 BFD_ASSERT (size == chunksz);
8963
8964 /* Make sure that we do not perform an undefined shift operation.
8965 We know that size == chunksz so there will only be one iteration
8966 of the loop below. */
8967 shift = 0;
8968 }
8969 else
8970 shift = 8 * chunksz;
8971
a0c8462f 8972 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8973 {
8974 switch (chunksz)
8975 {
d9352518 8976 case 1:
9b239e0e 8977 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8978 break;
8979 case 2:
9b239e0e 8980 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8981 break;
8982 case 4:
9b239e0e 8983 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8984 break;
d9352518 8985#ifdef BFD64
9b239e0e
NC
8986 case 8:
8987 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8988 break;
9b239e0e
NC
8989#endif
8990 default:
8991 abort ();
d9352518
DB
8992 }
8993 }
8994 return x;
8995}
8996
a0c8462f
AM
8997static void
8998decode_complex_addend (unsigned long *start, /* in bits */
8999 unsigned long *oplen, /* in bits */
9000 unsigned long *len, /* in bits */
9001 unsigned long *wordsz, /* in bytes */
9002 unsigned long *chunksz, /* in bytes */
9003 unsigned long *lsb0_p,
9004 unsigned long *signed_p,
9005 unsigned long *trunc_p,
9006 unsigned long encoded)
d9352518 9007{
07d6d2b8
AM
9008 * start = encoded & 0x3F;
9009 * len = (encoded >> 6) & 0x3F;
d9352518
DB
9010 * oplen = (encoded >> 12) & 0x3F;
9011 * wordsz = (encoded >> 18) & 0xF;
9012 * chunksz = (encoded >> 22) & 0xF;
9013 * lsb0_p = (encoded >> 27) & 1;
9014 * signed_p = (encoded >> 28) & 1;
9015 * trunc_p = (encoded >> 29) & 1;
9016}
9017
cdfeee4f 9018bfd_reloc_status_type
0f02bbd9 9019bfd_elf_perform_complex_relocation (bfd *input_bfd,
bb294208 9020 asection *input_section,
0f02bbd9
AM
9021 bfd_byte *contents,
9022 Elf_Internal_Rela *rel,
9023 bfd_vma relocation)
d9352518 9024{
0f02bbd9
AM
9025 bfd_vma shift, x, mask;
9026 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 9027 bfd_reloc_status_type r;
bb294208 9028 bfd_size_type octets;
d9352518
DB
9029
9030 /* Perform this reloc, since it is complex.
9031 (this is not to say that it necessarily refers to a complex
9032 symbol; merely that it is a self-describing CGEN based reloc.
9033 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 9034 word size, etc) encoded within it.). */
d9352518 9035
a0c8462f
AM
9036 decode_complex_addend (&start, &oplen, &len, &wordsz,
9037 &chunksz, &lsb0_p, &signed_p,
9038 &trunc_p, rel->r_addend);
d9352518
DB
9039
9040 mask = (((1L << (len - 1)) - 1) << 1) | 1;
9041
9042 if (lsb0_p)
9043 shift = (start + 1) - len;
9044 else
9045 shift = (8 * wordsz) - (start + len);
9046
bb294208
AM
9047 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
9048 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
d9352518
DB
9049
9050#ifdef DEBUG
9051 printf ("Doing complex reloc: "
9052 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9053 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9054 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9055 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
9056 oplen, (unsigned long) x, (unsigned long) mask,
9057 (unsigned long) relocation);
d9352518
DB
9058#endif
9059
cdfeee4f 9060 r = bfd_reloc_ok;
d9352518 9061 if (! trunc_p)
cdfeee4f
AM
9062 /* Now do an overflow check. */
9063 r = bfd_check_overflow ((signed_p
9064 ? complain_overflow_signed
9065 : complain_overflow_unsigned),
9066 len, 0, (8 * wordsz),
9067 relocation);
a0c8462f 9068
d9352518
DB
9069 /* Do the deed. */
9070 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9071
9072#ifdef DEBUG
9073 printf (" relocation: %8.8lx\n"
9074 " shifted mask: %8.8lx\n"
9075 " shifted/masked reloc: %8.8lx\n"
9076 " result: %8.8lx\n",
9ccb8af9
AM
9077 (unsigned long) relocation, (unsigned long) (mask << shift),
9078 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 9079#endif
bb294208 9080 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
cdfeee4f 9081 return r;
d9352518
DB
9082}
9083
0e287786
AM
9084/* Functions to read r_offset from external (target order) reloc
9085 entry. Faster than bfd_getl32 et al, because we let the compiler
9086 know the value is aligned. */
53df40a4 9087
0e287786
AM
9088static bfd_vma
9089ext32l_r_offset (const void *p)
53df40a4
AM
9090{
9091 union aligned32
9092 {
9093 uint32_t v;
9094 unsigned char c[4];
9095 };
9096 const union aligned32 *a
0e287786 9097 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
9098
9099 uint32_t aval = ( (uint32_t) a->c[0]
9100 | (uint32_t) a->c[1] << 8
9101 | (uint32_t) a->c[2] << 16
9102 | (uint32_t) a->c[3] << 24);
0e287786 9103 return aval;
53df40a4
AM
9104}
9105
0e287786
AM
9106static bfd_vma
9107ext32b_r_offset (const void *p)
53df40a4
AM
9108{
9109 union aligned32
9110 {
9111 uint32_t v;
9112 unsigned char c[4];
9113 };
9114 const union aligned32 *a
0e287786 9115 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
9116
9117 uint32_t aval = ( (uint32_t) a->c[0] << 24
9118 | (uint32_t) a->c[1] << 16
9119 | (uint32_t) a->c[2] << 8
9120 | (uint32_t) a->c[3]);
0e287786 9121 return aval;
53df40a4
AM
9122}
9123
9124#ifdef BFD_HOST_64_BIT
0e287786
AM
9125static bfd_vma
9126ext64l_r_offset (const void *p)
53df40a4
AM
9127{
9128 union aligned64
9129 {
9130 uint64_t v;
9131 unsigned char c[8];
9132 };
9133 const union aligned64 *a
0e287786 9134 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
9135
9136 uint64_t aval = ( (uint64_t) a->c[0]
9137 | (uint64_t) a->c[1] << 8
9138 | (uint64_t) a->c[2] << 16
9139 | (uint64_t) a->c[3] << 24
9140 | (uint64_t) a->c[4] << 32
9141 | (uint64_t) a->c[5] << 40
9142 | (uint64_t) a->c[6] << 48
9143 | (uint64_t) a->c[7] << 56);
0e287786 9144 return aval;
53df40a4
AM
9145}
9146
0e287786
AM
9147static bfd_vma
9148ext64b_r_offset (const void *p)
53df40a4
AM
9149{
9150 union aligned64
9151 {
9152 uint64_t v;
9153 unsigned char c[8];
9154 };
9155 const union aligned64 *a
0e287786 9156 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
9157
9158 uint64_t aval = ( (uint64_t) a->c[0] << 56
9159 | (uint64_t) a->c[1] << 48
9160 | (uint64_t) a->c[2] << 40
9161 | (uint64_t) a->c[3] << 32
9162 | (uint64_t) a->c[4] << 24
9163 | (uint64_t) a->c[5] << 16
9164 | (uint64_t) a->c[6] << 8
9165 | (uint64_t) a->c[7]);
0e287786 9166 return aval;
53df40a4
AM
9167}
9168#endif
9169
c152c796
AM
9170/* When performing a relocatable link, the input relocations are
9171 preserved. But, if they reference global symbols, the indices
d4730f92
BS
9172 referenced must be updated. Update all the relocations found in
9173 RELDATA. */
c152c796 9174
bca6d0e3 9175static bfd_boolean
c152c796 9176elf_link_adjust_relocs (bfd *abfd,
9eaff861 9177 asection *sec,
28dbcedc 9178 struct bfd_elf_section_reloc_data *reldata,
10bbbc1d
NC
9179 bfd_boolean sort,
9180 struct bfd_link_info *info)
c152c796
AM
9181{
9182 unsigned int i;
9183 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9184 bfd_byte *erela;
9185 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9186 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9187 bfd_vma r_type_mask;
9188 int r_sym_shift;
d4730f92
BS
9189 unsigned int count = reldata->count;
9190 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 9191
d4730f92 9192 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
9193 {
9194 swap_in = bed->s->swap_reloc_in;
9195 swap_out = bed->s->swap_reloc_out;
9196 }
d4730f92 9197 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
9198 {
9199 swap_in = bed->s->swap_reloca_in;
9200 swap_out = bed->s->swap_reloca_out;
9201 }
9202 else
9203 abort ();
9204
9205 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9206 abort ();
9207
9208 if (bed->s->arch_size == 32)
9209 {
9210 r_type_mask = 0xff;
9211 r_sym_shift = 8;
9212 }
9213 else
9214 {
9215 r_type_mask = 0xffffffff;
9216 r_sym_shift = 32;
9217 }
9218
d4730f92
BS
9219 erela = reldata->hdr->contents;
9220 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
9221 {
9222 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9223 unsigned int j;
9224
9225 if (*rel_hash == NULL)
9226 continue;
9227
10bbbc1d
NC
9228 if ((*rel_hash)->indx == -2
9229 && info->gc_sections
9230 && ! info->gc_keep_exported)
9231 {
9232 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9793eb77 9233 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
10bbbc1d
NC
9234 abfd, sec,
9235 (*rel_hash)->root.root.string);
9793eb77 9236 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
d42c267e 9237 abfd, sec);
10bbbc1d
NC
9238 bfd_set_error (bfd_error_invalid_operation);
9239 return FALSE;
9240 }
c152c796
AM
9241 BFD_ASSERT ((*rel_hash)->indx >= 0);
9242
9243 (*swap_in) (abfd, erela, irela);
9244 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9245 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9246 | (irela[j].r_info & r_type_mask));
9247 (*swap_out) (abfd, irela, erela);
9248 }
53df40a4 9249
9eaff861
AO
9250 if (bed->elf_backend_update_relocs)
9251 (*bed->elf_backend_update_relocs) (sec, reldata);
9252
0e287786 9253 if (sort && count != 0)
53df40a4 9254 {
0e287786
AM
9255 bfd_vma (*ext_r_off) (const void *);
9256 bfd_vma r_off;
9257 size_t elt_size;
9258 bfd_byte *base, *end, *p, *loc;
bca6d0e3 9259 bfd_byte *buf = NULL;
28dbcedc
AM
9260
9261 if (bed->s->arch_size == 32)
9262 {
9263 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 9264 ext_r_off = ext32l_r_offset;
28dbcedc 9265 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 9266 ext_r_off = ext32b_r_offset;
28dbcedc
AM
9267 else
9268 abort ();
9269 }
53df40a4 9270 else
28dbcedc 9271 {
53df40a4 9272#ifdef BFD_HOST_64_BIT
28dbcedc 9273 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 9274 ext_r_off = ext64l_r_offset;
28dbcedc 9275 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 9276 ext_r_off = ext64b_r_offset;
28dbcedc 9277 else
53df40a4 9278#endif
28dbcedc
AM
9279 abort ();
9280 }
0e287786 9281
bca6d0e3
AM
9282 /* Must use a stable sort here. A modified insertion sort,
9283 since the relocs are mostly sorted already. */
0e287786
AM
9284 elt_size = reldata->hdr->sh_entsize;
9285 base = reldata->hdr->contents;
9286 end = base + count * elt_size;
bca6d0e3 9287 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
9288 abort ();
9289
9290 /* Ensure the first element is lowest. This acts as a sentinel,
9291 speeding the main loop below. */
9292 r_off = (*ext_r_off) (base);
9293 for (p = loc = base; (p += elt_size) < end; )
9294 {
9295 bfd_vma r_off2 = (*ext_r_off) (p);
9296 if (r_off > r_off2)
9297 {
9298 r_off = r_off2;
9299 loc = p;
9300 }
9301 }
9302 if (loc != base)
9303 {
9304 /* Don't just swap *base and *loc as that changes the order
9305 of the original base[0] and base[1] if they happen to
9306 have the same r_offset. */
bca6d0e3
AM
9307 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9308 memcpy (onebuf, loc, elt_size);
0e287786 9309 memmove (base + elt_size, base, loc - base);
bca6d0e3 9310 memcpy (base, onebuf, elt_size);
0e287786
AM
9311 }
9312
b29b8669 9313 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
9314 {
9315 /* base to p is sorted, *p is next to insert. */
9316 r_off = (*ext_r_off) (p);
9317 /* Search the sorted region for location to insert. */
9318 loc = p - elt_size;
9319 while (r_off < (*ext_r_off) (loc))
9320 loc -= elt_size;
9321 loc += elt_size;
9322 if (loc != p)
9323 {
bca6d0e3
AM
9324 /* Chances are there is a run of relocs to insert here,
9325 from one of more input files. Files are not always
9326 linked in order due to the way elf_link_input_bfd is
9327 called. See pr17666. */
9328 size_t sortlen = p - loc;
9329 bfd_vma r_off2 = (*ext_r_off) (loc);
9330 size_t runlen = elt_size;
9331 size_t buf_size = 96 * 1024;
9332 while (p + runlen < end
9333 && (sortlen <= buf_size
9334 || runlen + elt_size <= buf_size)
9335 && r_off2 > (*ext_r_off) (p + runlen))
9336 runlen += elt_size;
9337 if (buf == NULL)
9338 {
9339 buf = bfd_malloc (buf_size);
9340 if (buf == NULL)
9341 return FALSE;
9342 }
9343 if (runlen < sortlen)
9344 {
9345 memcpy (buf, p, runlen);
9346 memmove (loc + runlen, loc, sortlen);
9347 memcpy (loc, buf, runlen);
9348 }
9349 else
9350 {
9351 memcpy (buf, loc, sortlen);
9352 memmove (loc, p, runlen);
9353 memcpy (loc + runlen, buf, sortlen);
9354 }
b29b8669 9355 p += runlen - elt_size;
0e287786
AM
9356 }
9357 }
9358 /* Hashes are no longer valid. */
28dbcedc
AM
9359 free (reldata->hashes);
9360 reldata->hashes = NULL;
bca6d0e3 9361 free (buf);
53df40a4 9362 }
bca6d0e3 9363 return TRUE;
c152c796
AM
9364}
9365
9366struct elf_link_sort_rela
9367{
9368 union {
9369 bfd_vma offset;
9370 bfd_vma sym_mask;
9371 } u;
9372 enum elf_reloc_type_class type;
9373 /* We use this as an array of size int_rels_per_ext_rel. */
9374 Elf_Internal_Rela rela[1];
9375};
9376
dcea6a95
AM
9377/* qsort stability here and for cmp2 is only an issue if multiple
9378 dynamic relocations are emitted at the same address. But targets
9379 that apply a series of dynamic relocations each operating on the
9380 result of the prior relocation can't use -z combreloc as
9381 implemented anyway. Such schemes tend to be broken by sorting on
9382 symbol index. That leaves dynamic NONE relocs as the only other
9383 case where ld might emit multiple relocs at the same address, and
9384 those are only emitted due to target bugs. */
9385
c152c796
AM
9386static int
9387elf_link_sort_cmp1 (const void *A, const void *B)
9388{
a50b1753
NC
9389 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9390 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
9391 int relativea, relativeb;
9392
9393 relativea = a->type == reloc_class_relative;
9394 relativeb = b->type == reloc_class_relative;
9395
9396 if (relativea < relativeb)
9397 return 1;
9398 if (relativea > relativeb)
9399 return -1;
9400 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9401 return -1;
9402 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9403 return 1;
9404 if (a->rela->r_offset < b->rela->r_offset)
9405 return -1;
9406 if (a->rela->r_offset > b->rela->r_offset)
9407 return 1;
9408 return 0;
9409}
9410
9411static int
9412elf_link_sort_cmp2 (const void *A, const void *B)
9413{
a50b1753
NC
9414 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9415 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 9416
7e612e98 9417 if (a->type < b->type)
c152c796 9418 return -1;
7e612e98 9419 if (a->type > b->type)
c152c796 9420 return 1;
7e612e98 9421 if (a->u.offset < b->u.offset)
c152c796 9422 return -1;
7e612e98 9423 if (a->u.offset > b->u.offset)
c152c796
AM
9424 return 1;
9425 if (a->rela->r_offset < b->rela->r_offset)
9426 return -1;
9427 if (a->rela->r_offset > b->rela->r_offset)
9428 return 1;
9429 return 0;
9430}
9431
9432static size_t
9433elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9434{
3410fea8 9435 asection *dynamic_relocs;
fc66a176
L
9436 asection *rela_dyn;
9437 asection *rel_dyn;
c152c796
AM
9438 bfd_size_type count, size;
9439 size_t i, ret, sort_elt, ext_size;
9440 bfd_byte *sort, *s_non_relative, *p;
9441 struct elf_link_sort_rela *sq;
9442 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9443 int i2e = bed->s->int_rels_per_ext_rel;
61826503 9444 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
c152c796
AM
9445 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9446 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9447 struct bfd_link_order *lo;
9448 bfd_vma r_sym_mask;
3410fea8 9449 bfd_boolean use_rela;
c152c796 9450
3410fea8
NC
9451 /* Find a dynamic reloc section. */
9452 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9453 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9454 if (rela_dyn != NULL && rela_dyn->size > 0
9455 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 9456 {
3410fea8
NC
9457 bfd_boolean use_rela_initialised = FALSE;
9458
9459 /* This is just here to stop gcc from complaining.
c8e44c6d 9460 Its initialization checking code is not perfect. */
3410fea8
NC
9461 use_rela = TRUE;
9462
9463 /* Both sections are present. Examine the sizes
9464 of the indirect sections to help us choose. */
9465 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9466 if (lo->type == bfd_indirect_link_order)
9467 {
9468 asection *o = lo->u.indirect.section;
9469
9470 if ((o->size % bed->s->sizeof_rela) == 0)
9471 {
9472 if ((o->size % bed->s->sizeof_rel) == 0)
9473 /* Section size is divisible by both rel and rela sizes.
9474 It is of no help to us. */
9475 ;
9476 else
9477 {
9478 /* Section size is only divisible by rela. */
535b785f 9479 if (use_rela_initialised && !use_rela)
3410fea8 9480 {
9793eb77 9481 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9482 "they are in more than one size"),
9483 abfd);
3410fea8
NC
9484 bfd_set_error (bfd_error_invalid_operation);
9485 return 0;
9486 }
9487 else
9488 {
9489 use_rela = TRUE;
9490 use_rela_initialised = TRUE;
9491 }
9492 }
9493 }
9494 else if ((o->size % bed->s->sizeof_rel) == 0)
9495 {
9496 /* Section size is only divisible by rel. */
535b785f 9497 if (use_rela_initialised && use_rela)
3410fea8 9498 {
9793eb77 9499 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9500 "they are in more than one size"),
9501 abfd);
3410fea8
NC
9502 bfd_set_error (bfd_error_invalid_operation);
9503 return 0;
9504 }
9505 else
9506 {
9507 use_rela = FALSE;
9508 use_rela_initialised = TRUE;
9509 }
9510 }
9511 else
9512 {
c8e44c6d
AM
9513 /* The section size is not divisible by either -
9514 something is wrong. */
9793eb77 9515 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9516 "they are of an unknown size"), abfd);
3410fea8
NC
9517 bfd_set_error (bfd_error_invalid_operation);
9518 return 0;
9519 }
9520 }
9521
9522 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9523 if (lo->type == bfd_indirect_link_order)
9524 {
9525 asection *o = lo->u.indirect.section;
9526
9527 if ((o->size % bed->s->sizeof_rela) == 0)
9528 {
9529 if ((o->size % bed->s->sizeof_rel) == 0)
9530 /* Section size is divisible by both rel and rela sizes.
9531 It is of no help to us. */
9532 ;
9533 else
9534 {
9535 /* Section size is only divisible by rela. */
535b785f 9536 if (use_rela_initialised && !use_rela)
3410fea8 9537 {
9793eb77 9538 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9539 "they are in more than one size"),
9540 abfd);
3410fea8
NC
9541 bfd_set_error (bfd_error_invalid_operation);
9542 return 0;
9543 }
9544 else
9545 {
9546 use_rela = TRUE;
9547 use_rela_initialised = TRUE;
9548 }
9549 }
9550 }
9551 else if ((o->size % bed->s->sizeof_rel) == 0)
9552 {
9553 /* Section size is only divisible by rel. */
535b785f 9554 if (use_rela_initialised && use_rela)
3410fea8 9555 {
9793eb77 9556 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9557 "they are in more than one size"),
9558 abfd);
3410fea8
NC
9559 bfd_set_error (bfd_error_invalid_operation);
9560 return 0;
9561 }
9562 else
9563 {
9564 use_rela = FALSE;
9565 use_rela_initialised = TRUE;
9566 }
9567 }
9568 else
9569 {
c8e44c6d
AM
9570 /* The section size is not divisible by either -
9571 something is wrong. */
9793eb77 9572 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9573 "they are of an unknown size"), abfd);
3410fea8
NC
9574 bfd_set_error (bfd_error_invalid_operation);
9575 return 0;
9576 }
9577 }
9578
9579 if (! use_rela_initialised)
9580 /* Make a guess. */
9581 use_rela = TRUE;
c152c796 9582 }
fc66a176
L
9583 else if (rela_dyn != NULL && rela_dyn->size > 0)
9584 use_rela = TRUE;
9585 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 9586 use_rela = FALSE;
c152c796 9587 else
fc66a176 9588 return 0;
3410fea8
NC
9589
9590 if (use_rela)
c152c796 9591 {
3410fea8 9592 dynamic_relocs = rela_dyn;
c152c796
AM
9593 ext_size = bed->s->sizeof_rela;
9594 swap_in = bed->s->swap_reloca_in;
9595 swap_out = bed->s->swap_reloca_out;
9596 }
3410fea8
NC
9597 else
9598 {
9599 dynamic_relocs = rel_dyn;
9600 ext_size = bed->s->sizeof_rel;
9601 swap_in = bed->s->swap_reloc_in;
9602 swap_out = bed->s->swap_reloc_out;
9603 }
c152c796
AM
9604
9605 size = 0;
3410fea8 9606 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 9607 if (lo->type == bfd_indirect_link_order)
3410fea8 9608 size += lo->u.indirect.section->size;
c152c796 9609
3410fea8 9610 if (size != dynamic_relocs->size)
c152c796
AM
9611 return 0;
9612
9613 sort_elt = (sizeof (struct elf_link_sort_rela)
9614 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
9615
9616 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
9617 if (count == 0)
9618 return 0;
a50b1753 9619 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 9620
c152c796
AM
9621 if (sort == NULL)
9622 {
9623 (*info->callbacks->warning)
9793eb77 9624 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
c152c796
AM
9625 return 0;
9626 }
9627
9628 if (bed->s->arch_size == 32)
9629 r_sym_mask = ~(bfd_vma) 0xff;
9630 else
9631 r_sym_mask = ~(bfd_vma) 0xffffffff;
9632
3410fea8 9633 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9634 if (lo->type == bfd_indirect_link_order)
9635 {
9636 bfd_byte *erel, *erelend;
9637 asection *o = lo->u.indirect.section;
9638
1da212d6
AM
9639 if (o->contents == NULL && o->size != 0)
9640 {
9641 /* This is a reloc section that is being handled as a normal
9642 section. See bfd_section_from_shdr. We can't combine
9643 relocs in this case. */
9644 free (sort);
9645 return 0;
9646 }
c152c796 9647 erel = o->contents;
eea6121a 9648 erelend = o->contents + o->size;
c8e44c6d 9649 p = sort + o->output_offset * opb / ext_size * sort_elt;
3410fea8 9650
c152c796
AM
9651 while (erel < erelend)
9652 {
9653 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 9654
c152c796 9655 (*swap_in) (abfd, erel, s->rela);
7e612e98 9656 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
9657 s->u.sym_mask = r_sym_mask;
9658 p += sort_elt;
9659 erel += ext_size;
9660 }
9661 }
9662
9663 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9664
9665 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9666 {
9667 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9668 if (s->type != reloc_class_relative)
9669 break;
9670 }
9671 ret = i;
9672 s_non_relative = p;
9673
9674 sq = (struct elf_link_sort_rela *) s_non_relative;
9675 for (; i < count; i++, p += sort_elt)
9676 {
9677 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9678 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9679 sq = sp;
9680 sp->u.offset = sq->rela->r_offset;
9681 }
9682
9683 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9684
c8e44c6d
AM
9685 struct elf_link_hash_table *htab = elf_hash_table (info);
9686 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9687 {
9688 /* We have plt relocs in .rela.dyn. */
9689 sq = (struct elf_link_sort_rela *) sort;
9690 for (i = 0; i < count; i++)
9691 if (sq[count - i - 1].type != reloc_class_plt)
9692 break;
9693 if (i != 0 && htab->srelplt->size == i * ext_size)
9694 {
9695 struct bfd_link_order **plo;
9696 /* Put srelplt link_order last. This is so the output_offset
9697 set in the next loop is correct for DT_JMPREL. */
9698 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9699 if ((*plo)->type == bfd_indirect_link_order
9700 && (*plo)->u.indirect.section == htab->srelplt)
9701 {
9702 lo = *plo;
9703 *plo = lo->next;
9704 }
9705 else
9706 plo = &(*plo)->next;
9707 *plo = lo;
9708 lo->next = NULL;
9709 dynamic_relocs->map_tail.link_order = lo;
9710 }
9711 }
9712
9713 p = sort;
3410fea8 9714 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9715 if (lo->type == bfd_indirect_link_order)
9716 {
9717 bfd_byte *erel, *erelend;
9718 asection *o = lo->u.indirect.section;
9719
9720 erel = o->contents;
eea6121a 9721 erelend = o->contents + o->size;
c8e44c6d 9722 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
c152c796
AM
9723 while (erel < erelend)
9724 {
9725 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9726 (*swap_out) (abfd, s->rela, erel);
9727 p += sort_elt;
9728 erel += ext_size;
9729 }
9730 }
9731
9732 free (sort);
3410fea8 9733 *psec = dynamic_relocs;
c152c796
AM
9734 return ret;
9735}
9736
ef10c3ac 9737/* Add a symbol to the output symbol string table. */
c152c796 9738
6e0b88f1 9739static int
ef10c3ac
L
9740elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9741 const char *name,
9742 Elf_Internal_Sym *elfsym,
9743 asection *input_sec,
9744 struct elf_link_hash_entry *h)
c152c796 9745{
6e0b88f1 9746 int (*output_symbol_hook)
c152c796
AM
9747 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9748 struct elf_link_hash_entry *);
ef10c3ac 9749 struct elf_link_hash_table *hash_table;
c152c796 9750 const struct elf_backend_data *bed;
ef10c3ac 9751 bfd_size_type strtabsize;
c152c796 9752
8539e4e8
AM
9753 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9754
8b127cbc 9755 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
9756 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9757 if (output_symbol_hook != NULL)
9758 {
8b127cbc 9759 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
9760 if (ret != 1)
9761 return ret;
c152c796
AM
9762 }
9763
06f44071
AM
9764 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
9765 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
9766 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
9767 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
9768
ef10c3ac
L
9769 if (name == NULL
9770 || *name == '\0'
9771 || (input_sec->flags & SEC_EXCLUDE))
9772 elfsym->st_name = (unsigned long) -1;
c152c796
AM
9773 else
9774 {
ef10c3ac
L
9775 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9776 to get the final offset for st_name. */
3f2e9699 9777 char *versioned_name = (char *) name;
496afd17
L
9778 if (h != NULL)
9779 {
9780 if (h->versioned == versioned && h->def_dynamic)
9781 {
9782 /* Keep only one '@' for versioned symbols defined in
9783 shared objects. */
9784 char *version = strrchr (name, ELF_VER_CHR);
9785 char *base_end = strchr (name, ELF_VER_CHR);
9786 if (version != base_end)
9787 {
9788 size_t base_len;
9789 size_t len = strlen (name);
9790 versioned_name = bfd_alloc (flinfo->output_bfd, len);
9791 if (versioned_name == NULL)
9792 return 0;
9793 base_len = base_end - name;
9794 memcpy (versioned_name, name, base_len);
9795 memcpy (versioned_name + base_len, version,
9796 len - base_len);
9797 }
9798 }
9799 }
9800 else if (flinfo->info->unique_symbol
9801 && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
3f2e9699 9802 {
496afd17
L
9803 struct local_hash_entry *lh;
9804 switch (ELF_ST_TYPE (elfsym->st_info))
3f2e9699 9805 {
496afd17
L
9806 case STT_FILE:
9807 case STT_SECTION:
9808 break;
9809 default:
9810 lh = (struct local_hash_entry *) bfd_hash_lookup
9811 (&flinfo->local_hash_table, name, TRUE, FALSE);
9812 if (lh == NULL)
3f2e9699 9813 return 0;
496afd17
L
9814 if (lh->count)
9815 {
9816 /* Append ".COUNT" to duplicated local symbols. */
9817 size_t count_len;
9818 size_t base_len = lh->size;
9819 char buf[30];
9820 sprintf (buf, "%lx", lh->count);
9821 if (!base_len)
9822 {
9823 base_len = strlen (name);
9824 lh->size = base_len;
9825 }
9826 count_len = strlen (buf);
9827 versioned_name = bfd_alloc (flinfo->output_bfd,
9828 base_len + count_len + 2);
9829 if (versioned_name == NULL)
9830 return 0;
9831 memcpy (versioned_name, name, base_len);
9832 versioned_name[base_len] = '.';
9833 memcpy (versioned_name + base_len + 1, buf,
9834 count_len + 1);
9835 }
9836 lh->count++;
9837 break;
3f2e9699
L
9838 }
9839 }
ef10c3ac
L
9840 elfsym->st_name
9841 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
3f2e9699 9842 versioned_name, FALSE);
c152c796 9843 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 9844 return 0;
c152c796
AM
9845 }
9846
ef10c3ac
L
9847 hash_table = elf_hash_table (flinfo->info);
9848 strtabsize = hash_table->strtabsize;
9849 if (strtabsize <= hash_table->strtabcount)
c152c796 9850 {
ef10c3ac
L
9851 strtabsize += strtabsize;
9852 hash_table->strtabsize = strtabsize;
9853 strtabsize *= sizeof (*hash_table->strtab);
9854 hash_table->strtab
9855 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9856 strtabsize);
9857 if (hash_table->strtab == NULL)
6e0b88f1 9858 return 0;
c152c796 9859 }
ef10c3ac
L
9860 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9861 hash_table->strtab[hash_table->strtabcount].dest_index
9862 = hash_table->strtabcount;
9863 hash_table->strtab[hash_table->strtabcount].destshndx_index
9864 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9865
ed48ec2e 9866 flinfo->output_bfd->symcount += 1;
ef10c3ac
L
9867 hash_table->strtabcount += 1;
9868
9869 return 1;
9870}
9871
9872/* Swap symbols out to the symbol table and flush the output symbols to
9873 the file. */
9874
9875static bfd_boolean
9876elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9877{
9878 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
986f0783 9879 size_t amt;
ef53be89 9880 size_t i;
ef10c3ac
L
9881 const struct elf_backend_data *bed;
9882 bfd_byte *symbuf;
9883 Elf_Internal_Shdr *hdr;
9884 file_ptr pos;
9885 bfd_boolean ret;
9886
9887 if (!hash_table->strtabcount)
9888 return TRUE;
9889
9890 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9891
9892 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9893
ef10c3ac
L
9894 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9895 symbuf = (bfd_byte *) bfd_malloc (amt);
9896 if (symbuf == NULL)
9897 return FALSE;
1b786873 9898
ef10c3ac 9899 if (flinfo->symshndxbuf)
c152c796 9900 {
ef53be89
AM
9901 amt = sizeof (Elf_External_Sym_Shndx);
9902 amt *= bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9903 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9904 if (flinfo->symshndxbuf == NULL)
c152c796 9905 {
ef10c3ac
L
9906 free (symbuf);
9907 return FALSE;
c152c796 9908 }
c152c796
AM
9909 }
9910
3d16b64e 9911 /* Now swap out the symbols. */
ef10c3ac
L
9912 for (i = 0; i < hash_table->strtabcount; i++)
9913 {
9914 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9915 if (elfsym->sym.st_name == (unsigned long) -1)
9916 elfsym->sym.st_name = 0;
9917 else
9918 elfsym->sym.st_name
9919 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9920 elfsym->sym.st_name);
3d16b64e
NA
9921
9922 /* Inform the linker of the addition of this symbol. */
9923
9924 if (flinfo->info->callbacks->ctf_new_symbol)
9925 flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
9926 &elfsym->sym);
9927
ef10c3ac
L
9928 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9929 ((bfd_byte *) symbuf
9930 + (elfsym->dest_index
9931 * bed->s->sizeof_sym)),
9932 (flinfo->symshndxbuf
9933 + elfsym->destshndx_index));
9934 }
9935
9936 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9937 pos = hdr->sh_offset + hdr->sh_size;
9938 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9939 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9940 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9941 {
9942 hdr->sh_size += amt;
9943 ret = TRUE;
9944 }
9945 else
9946 ret = FALSE;
c152c796 9947
ef10c3ac
L
9948 free (symbuf);
9949
9950 free (hash_table->strtab);
9951 hash_table->strtab = NULL;
9952
9953 return ret;
c152c796
AM
9954}
9955
c0d5a53d
L
9956/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9957
9958static bfd_boolean
9959check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9960{
4fbb74a6
AM
9961 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9962 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
9963 {
9964 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 9965 beyond 64k. */
4eca0228 9966 _bfd_error_handler
695344c0 9967 /* xgettext:c-format */
9793eb77 9968 (_("%pB: too many sections: %d (>= %d)"),
4fbb74a6 9969 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
9970 bfd_set_error (bfd_error_nonrepresentable_section);
9971 return FALSE;
9972 }
9973 return TRUE;
9974}
9975
c152c796
AM
9976/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9977 allowing an unsatisfied unversioned symbol in the DSO to match a
9978 versioned symbol that would normally require an explicit version.
9979 We also handle the case that a DSO references a hidden symbol
9980 which may be satisfied by a versioned symbol in another DSO. */
9981
9982static bfd_boolean
9983elf_link_check_versioned_symbol (struct bfd_link_info *info,
9984 const struct elf_backend_data *bed,
9985 struct elf_link_hash_entry *h)
9986{
9987 bfd *abfd;
9988 struct elf_link_loaded_list *loaded;
9989
9990 if (!is_elf_hash_table (info->hash))
9991 return FALSE;
9992
90c984fc
L
9993 /* Check indirect symbol. */
9994 while (h->root.type == bfd_link_hash_indirect)
9995 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9996
c152c796
AM
9997 switch (h->root.type)
9998 {
9999 default:
10000 abfd = NULL;
10001 break;
10002
10003 case bfd_link_hash_undefined:
10004 case bfd_link_hash_undefweak:
10005 abfd = h->root.u.undef.abfd;
f4ab0e2d
L
10006 if (abfd == NULL
10007 || (abfd->flags & DYNAMIC) == 0
e56f61be 10008 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
10009 return FALSE;
10010 break;
10011
10012 case bfd_link_hash_defined:
10013 case bfd_link_hash_defweak:
10014 abfd = h->root.u.def.section->owner;
10015 break;
10016
10017 case bfd_link_hash_common:
10018 abfd = h->root.u.c.p->section->owner;
10019 break;
10020 }
10021 BFD_ASSERT (abfd != NULL);
10022
e310298c 10023 for (loaded = elf_hash_table (info)->dyn_loaded;
c152c796
AM
10024 loaded != NULL;
10025 loaded = loaded->next)
10026 {
10027 bfd *input;
10028 Elf_Internal_Shdr *hdr;
ef53be89
AM
10029 size_t symcount;
10030 size_t extsymcount;
10031 size_t extsymoff;
c152c796
AM
10032 Elf_Internal_Shdr *versymhdr;
10033 Elf_Internal_Sym *isym;
10034 Elf_Internal_Sym *isymend;
10035 Elf_Internal_Sym *isymbuf;
10036 Elf_External_Versym *ever;
10037 Elf_External_Versym *extversym;
10038
10039 input = loaded->abfd;
10040
10041 /* We check each DSO for a possible hidden versioned definition. */
10042 if (input == abfd
c152c796
AM
10043 || elf_dynversym (input) == 0)
10044 continue;
10045
10046 hdr = &elf_tdata (input)->dynsymtab_hdr;
10047
10048 symcount = hdr->sh_size / bed->s->sizeof_sym;
10049 if (elf_bad_symtab (input))
10050 {
10051 extsymcount = symcount;
10052 extsymoff = 0;
10053 }
10054 else
10055 {
10056 extsymcount = symcount - hdr->sh_info;
10057 extsymoff = hdr->sh_info;
10058 }
10059
10060 if (extsymcount == 0)
10061 continue;
10062
10063 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10064 NULL, NULL, NULL);
10065 if (isymbuf == NULL)
10066 return FALSE;
10067
10068 /* Read in any version definitions. */
10069 versymhdr = &elf_tdata (input)->dynversym_hdr;
c152c796 10070 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
2bb3687b
AM
10071 || (extversym = (Elf_External_Versym *)
10072 _bfd_malloc_and_read (input, versymhdr->sh_size,
10073 versymhdr->sh_size)) == NULL)
c152c796 10074 {
c152c796
AM
10075 free (isymbuf);
10076 return FALSE;
10077 }
10078
10079 ever = extversym + extsymoff;
10080 isymend = isymbuf + extsymcount;
10081 for (isym = isymbuf; isym < isymend; isym++, ever++)
10082 {
10083 const char *name;
10084 Elf_Internal_Versym iver;
10085 unsigned short version_index;
10086
10087 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10088 || isym->st_shndx == SHN_UNDEF)
10089 continue;
10090
10091 name = bfd_elf_string_from_elf_section (input,
10092 hdr->sh_link,
10093 isym->st_name);
10094 if (strcmp (name, h->root.root.string) != 0)
10095 continue;
10096
10097 _bfd_elf_swap_versym_in (input, ever, &iver);
10098
d023c380
L
10099 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10100 && !(h->def_regular
10101 && h->forced_local))
c152c796
AM
10102 {
10103 /* If we have a non-hidden versioned sym, then it should
d023c380
L
10104 have provided a definition for the undefined sym unless
10105 it is defined in a non-shared object and forced local.
10106 */
c152c796
AM
10107 abort ();
10108 }
10109
10110 version_index = iver.vs_vers & VERSYM_VERSION;
10111 if (version_index == 1 || version_index == 2)
10112 {
10113 /* This is the base or first version. We can use it. */
10114 free (extversym);
10115 free (isymbuf);
10116 return TRUE;
10117 }
10118 }
10119
10120 free (extversym);
10121 free (isymbuf);
10122 }
10123
10124 return FALSE;
10125}
10126
b8871f35
L
10127/* Convert ELF common symbol TYPE. */
10128
10129static int
10130elf_link_convert_common_type (struct bfd_link_info *info, int type)
10131{
10132 /* Commom symbol can only appear in relocatable link. */
10133 if (!bfd_link_relocatable (info))
10134 abort ();
10135 switch (info->elf_stt_common)
10136 {
10137 case unchanged:
10138 break;
10139 case elf_stt_common:
10140 type = STT_COMMON;
10141 break;
10142 case no_elf_stt_common:
10143 type = STT_OBJECT;
10144 break;
10145 }
10146 return type;
10147}
10148
c152c796
AM
10149/* Add an external symbol to the symbol table. This is called from
10150 the hash table traversal routine. When generating a shared object,
10151 we go through the symbol table twice. The first time we output
10152 anything that might have been forced to local scope in a version
10153 script. The second time we output the symbols that are still
10154 global symbols. */
10155
10156static bfd_boolean
7686d77d 10157elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 10158{
7686d77d 10159 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 10160 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 10161 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
10162 bfd_boolean strip;
10163 Elf_Internal_Sym sym;
10164 asection *input_sec;
10165 const struct elf_backend_data *bed;
6e0b88f1
AM
10166 long indx;
10167 int ret;
b8871f35 10168 unsigned int type;
c152c796
AM
10169
10170 if (h->root.type == bfd_link_hash_warning)
10171 {
10172 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10173 if (h->root.type == bfd_link_hash_new)
10174 return TRUE;
10175 }
10176
10177 /* Decide whether to output this symbol in this pass. */
10178 if (eoinfo->localsyms)
10179 {
4deb8f71 10180 if (!h->forced_local)
c152c796
AM
10181 return TRUE;
10182 }
10183 else
10184 {
4deb8f71 10185 if (h->forced_local)
c152c796
AM
10186 return TRUE;
10187 }
10188
8b127cbc 10189 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 10190
12ac1cf5 10191 if (h->root.type == bfd_link_hash_undefined)
c152c796 10192 {
12ac1cf5
NC
10193 /* If we have an undefined symbol reference here then it must have
10194 come from a shared library that is being linked in. (Undefined
98da7939
L
10195 references in regular files have already been handled unless
10196 they are in unreferenced sections which are removed by garbage
10197 collection). */
12ac1cf5
NC
10198 bfd_boolean ignore_undef = FALSE;
10199
10200 /* Some symbols may be special in that the fact that they're
10201 undefined can be safely ignored - let backend determine that. */
10202 if (bed->elf_backend_ignore_undef_symbol)
10203 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10204
10205 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 10206 if (!ignore_undef
c54f1524 10207 && h->ref_dynamic_nonweak
8b127cbc
AM
10208 && (!h->ref_regular || flinfo->info->gc_sections)
10209 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10210 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
95a51568
FS
10211 {
10212 flinfo->info->callbacks->undefined_symbol
10213 (flinfo->info, h->root.root.string,
10214 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10215 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10216 && !flinfo->info->warn_unresolved_syms);
10217 }
97196564
L
10218
10219 /* Strip a global symbol defined in a discarded section. */
10220 if (h->indx == -3)
10221 return TRUE;
c152c796
AM
10222 }
10223
10224 /* We should also warn if a forced local symbol is referenced from
10225 shared libraries. */
0e1862bb 10226 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
10227 && h->forced_local
10228 && h->ref_dynamic
371a5866 10229 && h->def_regular
f5385ebf 10230 && !h->dynamic_def
ee659f1f 10231 && h->ref_dynamic_nonweak
8b127cbc 10232 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 10233 {
17d078c5
AM
10234 bfd *def_bfd;
10235 const char *msg;
90c984fc
L
10236 struct elf_link_hash_entry *hi = h;
10237
10238 /* Check indirect symbol. */
10239 while (hi->root.type == bfd_link_hash_indirect)
10240 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
10241
10242 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
695344c0 10243 /* xgettext:c-format */
871b3ab2 10244 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
17d078c5 10245 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
695344c0 10246 /* xgettext:c-format */
871b3ab2 10247 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
17d078c5 10248 else
695344c0 10249 /* xgettext:c-format */
871b3ab2 10250 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
8b127cbc 10251 def_bfd = flinfo->output_bfd;
90c984fc
L
10252 if (hi->root.u.def.section != bfd_abs_section_ptr)
10253 def_bfd = hi->root.u.def.section->owner;
c08bb8dd
AM
10254 _bfd_error_handler (msg, flinfo->output_bfd,
10255 h->root.root.string, def_bfd);
17d078c5 10256 bfd_set_error (bfd_error_bad_value);
c152c796
AM
10257 eoinfo->failed = TRUE;
10258 return FALSE;
10259 }
10260
10261 /* We don't want to output symbols that have never been mentioned by
10262 a regular file, or that we have been told to strip. However, if
10263 h->indx is set to -2, the symbol is used by a reloc and we must
10264 output it. */
d983c8c5 10265 strip = FALSE;
c152c796 10266 if (h->indx == -2)
d983c8c5 10267 ;
f5385ebf 10268 else if ((h->def_dynamic
77cfaee6
AM
10269 || h->ref_dynamic
10270 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
10271 && !h->def_regular
10272 && !h->ref_regular)
c152c796 10273 strip = TRUE;
8b127cbc 10274 else if (flinfo->info->strip == strip_all)
c152c796 10275 strip = TRUE;
8b127cbc
AM
10276 else if (flinfo->info->strip == strip_some
10277 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
10278 h->root.root.string, FALSE, FALSE) == NULL)
10279 strip = TRUE;
d56d55e7
AM
10280 else if ((h->root.type == bfd_link_hash_defined
10281 || h->root.type == bfd_link_hash_defweak)
8b127cbc 10282 && ((flinfo->info->strip_discarded
dbaa2011 10283 && discarded_section (h->root.u.def.section))
ca4be51c
AM
10284 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10285 && h->root.u.def.section->owner != NULL
d56d55e7 10286 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 10287 strip = TRUE;
9e2278f5
AM
10288 else if ((h->root.type == bfd_link_hash_undefined
10289 || h->root.type == bfd_link_hash_undefweak)
10290 && h->root.u.undef.abfd != NULL
10291 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10292 strip = TRUE;
c152c796 10293
b8871f35
L
10294 type = h->type;
10295
c152c796 10296 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
10297 nothing else to do. However, if it is a forced local symbol or
10298 an ifunc symbol we need to give the backend finish_dynamic_symbol
10299 function a chance to make it dynamic. */
c152c796
AM
10300 if (strip
10301 && h->dynindx == -1
b8871f35 10302 && type != STT_GNU_IFUNC
f5385ebf 10303 && !h->forced_local)
c152c796
AM
10304 return TRUE;
10305
10306 sym.st_value = 0;
10307 sym.st_size = h->size;
10308 sym.st_other = h->other;
c152c796
AM
10309 switch (h->root.type)
10310 {
10311 default:
10312 case bfd_link_hash_new:
10313 case bfd_link_hash_warning:
10314 abort ();
10315 return FALSE;
10316
10317 case bfd_link_hash_undefined:
10318 case bfd_link_hash_undefweak:
10319 input_sec = bfd_und_section_ptr;
10320 sym.st_shndx = SHN_UNDEF;
10321 break;
10322
10323 case bfd_link_hash_defined:
10324 case bfd_link_hash_defweak:
10325 {
10326 input_sec = h->root.u.def.section;
10327 if (input_sec->output_section != NULL)
10328 {
10329 sym.st_shndx =
8b127cbc 10330 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
10331 input_sec->output_section);
10332 if (sym.st_shndx == SHN_BAD)
10333 {
4eca0228 10334 _bfd_error_handler
695344c0 10335 /* xgettext:c-format */
871b3ab2 10336 (_("%pB: could not find output section %pA for input section %pA"),
8b127cbc 10337 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 10338 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
10339 eoinfo->failed = TRUE;
10340 return FALSE;
10341 }
10342
10343 /* ELF symbols in relocatable files are section relative,
10344 but in nonrelocatable files they are virtual
10345 addresses. */
10346 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 10347 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10348 {
10349 sym.st_value += input_sec->output_section->vma;
10350 if (h->type == STT_TLS)
10351 {
8b127cbc 10352 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
10353 if (tls_sec != NULL)
10354 sym.st_value -= tls_sec->vma;
c152c796
AM
10355 }
10356 }
10357 }
10358 else
10359 {
10360 BFD_ASSERT (input_sec->owner == NULL
10361 || (input_sec->owner->flags & DYNAMIC) != 0);
10362 sym.st_shndx = SHN_UNDEF;
10363 input_sec = bfd_und_section_ptr;
10364 }
10365 }
10366 break;
10367
10368 case bfd_link_hash_common:
10369 input_sec = h->root.u.c.p->section;
a4d8e49b 10370 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
10371 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10372 break;
10373
10374 case bfd_link_hash_indirect:
10375 /* These symbols are created by symbol versioning. They point
10376 to the decorated version of the name. For example, if the
10377 symbol foo@@GNU_1.2 is the default, which should be used when
10378 foo is used with no version, then we add an indirect symbol
10379 foo which points to foo@@GNU_1.2. We ignore these symbols,
10380 since the indirected symbol is already in the hash table. */
10381 return TRUE;
10382 }
10383
b8871f35
L
10384 if (type == STT_COMMON || type == STT_OBJECT)
10385 switch (h->root.type)
10386 {
10387 case bfd_link_hash_common:
10388 type = elf_link_convert_common_type (flinfo->info, type);
10389 break;
10390 case bfd_link_hash_defined:
10391 case bfd_link_hash_defweak:
10392 if (bed->common_definition (&sym))
10393 type = elf_link_convert_common_type (flinfo->info, type);
10394 else
10395 type = STT_OBJECT;
10396 break;
10397 case bfd_link_hash_undefined:
10398 case bfd_link_hash_undefweak:
10399 break;
10400 default:
10401 abort ();
10402 }
10403
4deb8f71 10404 if (h->forced_local)
b8871f35
L
10405 {
10406 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10407 /* Turn off visibility on local symbol. */
10408 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10409 }
10410 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10411 else if (h->unique_global && h->def_regular)
10412 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10413 else if (h->root.type == bfd_link_hash_undefweak
10414 || h->root.type == bfd_link_hash_defweak)
10415 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10416 else
10417 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10418 sym.st_target_internal = h->target_internal;
10419
c152c796
AM
10420 /* Give the processor backend a chance to tweak the symbol value,
10421 and also to finish up anything that needs to be done for this
10422 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 10423 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 10424 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 10425 if ((h->type == STT_GNU_IFUNC
5f35ea9c 10426 && h->def_regular
0e1862bb 10427 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
10428 || ((h->dynindx != -1
10429 || h->forced_local)
0e1862bb 10430 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
10431 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10432 || h->root.type != bfd_link_hash_undefweak))
10433 || !h->forced_local)
8b127cbc 10434 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
10435 {
10436 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 10437 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
10438 {
10439 eoinfo->failed = TRUE;
10440 return FALSE;
10441 }
10442 }
10443
10444 /* If we are marking the symbol as undefined, and there are no
10445 non-weak references to this symbol from a regular object, then
10446 mark the symbol as weak undefined; if there are non-weak
10447 references, mark the symbol as strong. We can't do this earlier,
10448 because it might not be marked as undefined until the
10449 finish_dynamic_symbol routine gets through with it. */
10450 if (sym.st_shndx == SHN_UNDEF
f5385ebf 10451 && h->ref_regular
c152c796
AM
10452 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10453 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10454 {
10455 int bindtype;
b8871f35 10456 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
10457
10458 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10459 if (type == STT_GNU_IFUNC)
10460 type = STT_FUNC;
c152c796 10461
f5385ebf 10462 if (h->ref_regular_nonweak)
c152c796
AM
10463 bindtype = STB_GLOBAL;
10464 else
10465 bindtype = STB_WEAK;
2955ec4c 10466 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
10467 }
10468
bda987c2
CD
10469 /* If this is a symbol defined in a dynamic library, don't use the
10470 symbol size from the dynamic library. Relinking an executable
10471 against a new library may introduce gratuitous changes in the
10472 executable's symbols if we keep the size. */
10473 if (sym.st_shndx == SHN_UNDEF
10474 && !h->def_regular
10475 && h->def_dynamic)
10476 sym.st_size = 0;
10477
c152c796
AM
10478 /* If a non-weak symbol with non-default visibility is not defined
10479 locally, it is a fatal error. */
0e1862bb 10480 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
10481 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10482 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10483 && h->root.type == bfd_link_hash_undefined
f5385ebf 10484 && !h->def_regular)
c152c796 10485 {
17d078c5
AM
10486 const char *msg;
10487
10488 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
695344c0 10489 /* xgettext:c-format */
871b3ab2 10490 msg = _("%pB: protected symbol `%s' isn't defined");
17d078c5 10491 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
695344c0 10492 /* xgettext:c-format */
871b3ab2 10493 msg = _("%pB: internal symbol `%s' isn't defined");
17d078c5 10494 else
695344c0 10495 /* xgettext:c-format */
871b3ab2 10496 msg = _("%pB: hidden symbol `%s' isn't defined");
4eca0228 10497 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 10498 bfd_set_error (bfd_error_bad_value);
c152c796
AM
10499 eoinfo->failed = TRUE;
10500 return FALSE;
10501 }
10502
10503 /* If this symbol should be put in the .dynsym section, then put it
10504 there now. We already know the symbol index. We also fill in
10505 the entry in the .hash section. */
1c2649ed
EB
10506 if (h->dynindx != -1
10507 && elf_hash_table (flinfo->info)->dynamic_sections_created
10508 && elf_hash_table (flinfo->info)->dynsym != NULL
10509 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
c152c796 10510 {
c152c796
AM
10511 bfd_byte *esym;
10512
90c984fc
L
10513 /* Since there is no version information in the dynamic string,
10514 if there is no version info in symbol version section, we will
1659f720 10515 have a run-time problem if not linking executable, referenced
4deb8f71 10516 by shared library, or not bound locally. */
1659f720 10517 if (h->verinfo.verdef == NULL
0e1862bb 10518 && (!bfd_link_executable (flinfo->info)
1659f720
L
10519 || h->ref_dynamic
10520 || !h->def_regular))
90c984fc
L
10521 {
10522 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10523
10524 if (p && p [1] != '\0')
10525 {
4eca0228 10526 _bfd_error_handler
695344c0 10527 /* xgettext:c-format */
9793eb77 10528 (_("%pB: no symbol version section for versioned symbol `%s'"),
90c984fc
L
10529 flinfo->output_bfd, h->root.root.string);
10530 eoinfo->failed = TRUE;
10531 return FALSE;
10532 }
10533 }
10534
c152c796 10535 sym.st_name = h->dynstr_index;
cae1fbbb
L
10536 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10537 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 10538 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
10539 {
10540 eoinfo->failed = TRUE;
10541 return FALSE;
10542 }
3d16b64e
NA
10543
10544 /* Inform the linker of the addition of this symbol. */
10545
10546 if (flinfo->info->callbacks->ctf_new_dynsym)
10547 flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
10548
8b127cbc 10549 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 10550
8b127cbc 10551 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
10552 {
10553 size_t hash_entry_size;
10554 bfd_byte *bucketpos;
10555 bfd_vma chain;
41198d0c
L
10556 size_t bucketcount;
10557 size_t bucket;
10558
8b127cbc 10559 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 10560 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
10561
10562 hash_entry_size
8b127cbc
AM
10563 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10564 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 10565 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
10566 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10567 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10568 bucketpos);
10569 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10570 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
10571 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10572 }
c152c796 10573
8b127cbc 10574 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
10575 {
10576 Elf_Internal_Versym iversym;
10577 Elf_External_Versym *eversym;
10578
5fa370e4 10579 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
c152c796 10580 {
7b20f099
AM
10581 if (h->verinfo.verdef == NULL
10582 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10583 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
eb6e6af8 10584 iversym.vs_vers = 1;
c152c796
AM
10585 else
10586 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10587 }
10588 else
10589 {
10590 if (h->verinfo.vertree == NULL)
10591 iversym.vs_vers = 1;
10592 else
10593 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 10594 if (flinfo->info->create_default_symver)
3e3b46e5 10595 iversym.vs_vers++;
c152c796
AM
10596 }
10597
422f1182 10598 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 10599 defined locally. */
422f1182 10600 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
10601 iversym.vs_vers |= VERSYM_HIDDEN;
10602
8b127cbc 10603 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 10604 eversym += h->dynindx;
8b127cbc 10605 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
10606 }
10607 }
10608
d983c8c5
AM
10609 /* If the symbol is undefined, and we didn't output it to .dynsym,
10610 strip it from .symtab too. Obviously we can't do this for
10611 relocatable output or when needed for --emit-relocs. */
10612 else if (input_sec == bfd_und_section_ptr
10613 && h->indx != -2
66cae560
NC
10614 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10615 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
0e1862bb 10616 && !bfd_link_relocatable (flinfo->info))
d983c8c5 10617 return TRUE;
66cae560 10618
d983c8c5
AM
10619 /* Also strip others that we couldn't earlier due to dynamic symbol
10620 processing. */
10621 if (strip)
10622 return TRUE;
10623 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
10624 return TRUE;
10625
2ec55de3
AM
10626 /* Output a FILE symbol so that following locals are not associated
10627 with the wrong input file. We need one for forced local symbols
10628 if we've seen more than one FILE symbol or when we have exactly
10629 one FILE symbol but global symbols are present in a file other
10630 than the one with the FILE symbol. We also need one if linker
10631 defined symbols are present. In practice these conditions are
10632 always met, so just emit the FILE symbol unconditionally. */
10633 if (eoinfo->localsyms
10634 && !eoinfo->file_sym_done
10635 && eoinfo->flinfo->filesym_count != 0)
10636 {
10637 Elf_Internal_Sym fsym;
10638
10639 memset (&fsym, 0, sizeof (fsym));
10640 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10641 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
10642 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10643 bfd_und_section_ptr, NULL))
2ec55de3
AM
10644 return FALSE;
10645
10646 eoinfo->file_sym_done = TRUE;
10647 }
10648
8b127cbc 10649 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
10650 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10651 input_sec, h);
6e0b88f1 10652 if (ret == 0)
c152c796
AM
10653 {
10654 eoinfo->failed = TRUE;
10655 return FALSE;
10656 }
6e0b88f1
AM
10657 else if (ret == 1)
10658 h->indx = indx;
10659 else if (h->indx == -2)
10660 abort();
c152c796
AM
10661
10662 return TRUE;
10663}
10664
cdd3575c
AM
10665/* Return TRUE if special handling is done for relocs in SEC against
10666 symbols defined in discarded sections. */
10667
c152c796
AM
10668static bfd_boolean
10669elf_section_ignore_discarded_relocs (asection *sec)
10670{
10671 const struct elf_backend_data *bed;
10672
cdd3575c
AM
10673 switch (sec->sec_info_type)
10674 {
dbaa2011
AM
10675 case SEC_INFO_TYPE_STABS:
10676 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 10677 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
10678 return TRUE;
10679 default:
10680 break;
10681 }
c152c796
AM
10682
10683 bed = get_elf_backend_data (sec->owner);
10684 if (bed->elf_backend_ignore_discarded_relocs != NULL
10685 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10686 return TRUE;
10687
10688 return FALSE;
10689}
10690
9e66c942
AM
10691/* Return a mask saying how ld should treat relocations in SEC against
10692 symbols defined in discarded sections. If this function returns
10693 COMPLAIN set, ld will issue a warning message. If this function
10694 returns PRETEND set, and the discarded section was link-once and the
10695 same size as the kept link-once section, ld will pretend that the
10696 symbol was actually defined in the kept section. Otherwise ld will
10697 zero the reloc (at least that is the intent, but some cooperation by
10698 the target dependent code is needed, particularly for REL targets). */
10699
8a696751
AM
10700unsigned int
10701_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 10702{
9e66c942 10703 if (sec->flags & SEC_DEBUGGING)
69d54b1b 10704 return PRETEND;
cdd3575c
AM
10705
10706 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 10707 return 0;
cdd3575c
AM
10708
10709 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 10710 return 0;
cdd3575c 10711
9e66c942 10712 return COMPLAIN | PRETEND;
cdd3575c
AM
10713}
10714
3d7f7666
L
10715/* Find a match between a section and a member of a section group. */
10716
10717static asection *
c0f00686
L
10718match_group_member (asection *sec, asection *group,
10719 struct bfd_link_info *info)
3d7f7666
L
10720{
10721 asection *first = elf_next_in_group (group);
10722 asection *s = first;
10723
10724 while (s != NULL)
10725 {
c0f00686 10726 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
10727 return s;
10728
83180ade 10729 s = elf_next_in_group (s);
3d7f7666
L
10730 if (s == first)
10731 break;
10732 }
10733
10734 return NULL;
10735}
10736
01b3c8ab 10737/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
10738 to replace it. Return the replacement if it is OK. Otherwise return
10739 NULL. */
01b3c8ab
L
10740
10741asection *
c0f00686 10742_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
10743{
10744 asection *kept;
10745
10746 kept = sec->kept_section;
10747 if (kept != NULL)
10748 {
c2370991 10749 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 10750 kept = match_group_member (sec, kept, info);
58349d00
L
10751 if (kept != NULL)
10752 {
10753 if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10754 != (kept->rawsize != 0 ? kept->rawsize : kept->size))
10755 kept = NULL;
10756 else
10757 {
10758 /* Get the real kept section. */
10759 asection *next;
10760 for (next = kept->kept_section;
10761 next != NULL;
10762 next = next->kept_section)
10763 kept = next;
10764 }
10765 }
c2370991 10766 sec->kept_section = kept;
01b3c8ab
L
10767 }
10768 return kept;
10769}
10770
c152c796
AM
10771/* Link an input file into the linker output file. This function
10772 handles all the sections and relocations of the input file at once.
10773 This is so that we only have to read the local symbols once, and
10774 don't have to keep them in memory. */
10775
10776static bfd_boolean
8b127cbc 10777elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 10778{
ece5ef60 10779 int (*relocate_section)
c152c796
AM
10780 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10781 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10782 bfd *output_bfd;
10783 Elf_Internal_Shdr *symtab_hdr;
10784 size_t locsymcount;
10785 size_t extsymoff;
10786 Elf_Internal_Sym *isymbuf;
10787 Elf_Internal_Sym *isym;
10788 Elf_Internal_Sym *isymend;
10789 long *pindex;
10790 asection **ppsection;
10791 asection *o;
10792 const struct elf_backend_data *bed;
c152c796 10793 struct elf_link_hash_entry **sym_hashes;
310fd250
L
10794 bfd_size_type address_size;
10795 bfd_vma r_type_mask;
10796 int r_sym_shift;
ffbc01cc 10797 bfd_boolean have_file_sym = FALSE;
c152c796 10798
8b127cbc 10799 output_bfd = flinfo->output_bfd;
c152c796
AM
10800 bed = get_elf_backend_data (output_bfd);
10801 relocate_section = bed->elf_backend_relocate_section;
10802
10803 /* If this is a dynamic object, we don't want to do anything here:
10804 we don't want the local symbols, and we don't want the section
10805 contents. */
10806 if ((input_bfd->flags & DYNAMIC) != 0)
10807 return TRUE;
10808
c152c796
AM
10809 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10810 if (elf_bad_symtab (input_bfd))
10811 {
10812 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10813 extsymoff = 0;
10814 }
10815 else
10816 {
10817 locsymcount = symtab_hdr->sh_info;
10818 extsymoff = symtab_hdr->sh_info;
10819 }
10820
99fabbc9
JL
10821 /* Enable GNU OSABI features in the output BFD that are used in the input
10822 BFD. */
10823 if (bed->elf_osabi == ELFOSABI_NONE
10824 || bed->elf_osabi == ELFOSABI_GNU
10825 || bed->elf_osabi == ELFOSABI_FREEBSD)
10826 elf_tdata (output_bfd)->has_gnu_osabi
04f89674
L
10827 |= (elf_tdata (input_bfd)->has_gnu_osabi
10828 & (bfd_link_relocatable (flinfo->info)
10829 ? -1 : ~elf_gnu_osabi_retain));
99fabbc9 10830
c152c796
AM
10831 /* Read the local symbols. */
10832 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10833 if (isymbuf == NULL && locsymcount != 0)
10834 {
10835 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
10836 flinfo->internal_syms,
10837 flinfo->external_syms,
10838 flinfo->locsym_shndx);
c152c796
AM
10839 if (isymbuf == NULL)
10840 return FALSE;
10841 }
10842
10843 /* Find local symbol sections and adjust values of symbols in
10844 SEC_MERGE sections. Write out those local symbols we know are
10845 going into the output file. */
10846 isymend = isymbuf + locsymcount;
8b127cbc 10847 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
10848 isym < isymend;
10849 isym++, pindex++, ppsection++)
10850 {
10851 asection *isec;
10852 const char *name;
10853 Elf_Internal_Sym osym;
6e0b88f1
AM
10854 long indx;
10855 int ret;
c152c796
AM
10856
10857 *pindex = -1;
10858
10859 if (elf_bad_symtab (input_bfd))
10860 {
10861 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10862 {
10863 *ppsection = NULL;
10864 continue;
10865 }
10866 }
10867
10868 if (isym->st_shndx == SHN_UNDEF)
10869 isec = bfd_und_section_ptr;
c152c796
AM
10870 else if (isym->st_shndx == SHN_ABS)
10871 isec = bfd_abs_section_ptr;
10872 else if (isym->st_shndx == SHN_COMMON)
10873 isec = bfd_com_section_ptr;
10874 else
10875 {
cb33740c
AM
10876 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10877 if (isec == NULL)
10878 {
10879 /* Don't attempt to output symbols with st_shnx in the
10880 reserved range other than SHN_ABS and SHN_COMMON. */
6835821b 10881 isec = bfd_und_section_ptr;
cb33740c 10882 }
dbaa2011 10883 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
10884 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10885 isym->st_value =
10886 _bfd_merged_section_offset (output_bfd, &isec,
10887 elf_section_data (isec)->sec_info,
10888 isym->st_value);
c152c796
AM
10889 }
10890
10891 *ppsection = isec;
10892
d983c8c5
AM
10893 /* Don't output the first, undefined, symbol. In fact, don't
10894 output any undefined local symbol. */
10895 if (isec == bfd_und_section_ptr)
c152c796
AM
10896 continue;
10897
10898 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10899 {
10900 /* We never output section symbols. Instead, we use the
10901 section symbol of the corresponding section in the output
10902 file. */
10903 continue;
10904 }
10905
10906 /* If we are stripping all symbols, we don't want to output this
10907 one. */
8b127cbc 10908 if (flinfo->info->strip == strip_all)
c152c796
AM
10909 continue;
10910
10911 /* If we are discarding all local symbols, we don't want to
10912 output this one. If we are generating a relocatable output
10913 file, then some of the local symbols may be required by
10914 relocs; we output them below as we discover that they are
10915 needed. */
8b127cbc 10916 if (flinfo->info->discard == discard_all)
c152c796
AM
10917 continue;
10918
10919 /* If this symbol is defined in a section which we are
f02571c5 10920 discarding, we don't need to keep it. */
abf874aa
CL
10921 if (isym->st_shndx != SHN_UNDEF
10922 && isym->st_shndx < SHN_LORESERVE
10923 && isec->output_section == NULL
10924 && flinfo->info->non_contiguous_regions
10925 && flinfo->info->non_contiguous_regions_warnings)
10926 {
10927 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
10928 "discards section `%s' from '%s'\n"),
765cf5f6 10929 isec->name, bfd_get_filename (isec->owner));
abf874aa
CL
10930 continue;
10931 }
10932
f02571c5 10933 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
10934 && isym->st_shndx < SHN_LORESERVE
10935 && bfd_section_removed_from_list (output_bfd,
10936 isec->output_section))
e75a280b
L
10937 continue;
10938
c152c796
AM
10939 /* Get the name of the symbol. */
10940 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10941 isym->st_name);
10942 if (name == NULL)
10943 return FALSE;
10944
10945 /* See if we are discarding symbols with this name. */
8b127cbc
AM
10946 if ((flinfo->info->strip == strip_some
10947 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 10948 == NULL))
8b127cbc 10949 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
10950 && (isec->flags & SEC_MERGE)
10951 && !bfd_link_relocatable (flinfo->info))
8b127cbc 10952 || flinfo->info->discard == discard_l)
c152c796
AM
10953 && bfd_is_local_label_name (input_bfd, name)))
10954 continue;
10955
ffbc01cc
AM
10956 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10957 {
ce875075
AM
10958 if (input_bfd->lto_output)
10959 /* -flto puts a temp file name here. This means builds
10960 are not reproducible. Discard the symbol. */
10961 continue;
ffbc01cc
AM
10962 have_file_sym = TRUE;
10963 flinfo->filesym_count += 1;
10964 }
10965 if (!have_file_sym)
10966 {
10967 /* In the absence of debug info, bfd_find_nearest_line uses
10968 FILE symbols to determine the source file for local
10969 function symbols. Provide a FILE symbol here if input
10970 files lack such, so that their symbols won't be
10971 associated with a previous input file. It's not the
10972 source file, but the best we can do. */
5b4293ba 10973 const char *filename;
ffbc01cc
AM
10974 have_file_sym = TRUE;
10975 flinfo->filesym_count += 1;
10976 memset (&osym, 0, sizeof (osym));
10977 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10978 osym.st_shndx = SHN_ABS;
5b4293ba
AM
10979 if (input_bfd->lto_output)
10980 filename = NULL;
10981 else
10982 filename = lbasename (bfd_get_filename (input_bfd));
10983 if (!elf_link_output_symstrtab (flinfo, filename, &osym,
10984 bfd_abs_section_ptr, NULL))
ffbc01cc
AM
10985 return FALSE;
10986 }
10987
c152c796
AM
10988 osym = *isym;
10989
10990 /* Adjust the section index for the output file. */
10991 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10992 isec->output_section);
10993 if (osym.st_shndx == SHN_BAD)
10994 return FALSE;
10995
c152c796
AM
10996 /* ELF symbols in relocatable files are section relative, but
10997 in executable files they are virtual addresses. Note that
10998 this code assumes that all ELF sections have an associated
10999 BFD section with a reasonable value for output_offset; below
11000 we assume that they also have a reasonable value for
11001 output_section. Any special sections must be set up to meet
11002 these requirements. */
11003 osym.st_value += isec->output_offset;
0e1862bb 11004 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11005 {
11006 osym.st_value += isec->output_section->vma;
11007 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
11008 {
11009 /* STT_TLS symbols are relative to PT_TLS segment base. */
102def4d
AM
11010 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
11011 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
11012 else
11013 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
11014 STT_NOTYPE);
c152c796
AM
11015 }
11016 }
11017
6e0b88f1 11018 indx = bfd_get_symcount (output_bfd);
ef10c3ac 11019 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 11020 if (ret == 0)
c152c796 11021 return FALSE;
6e0b88f1
AM
11022 else if (ret == 1)
11023 *pindex = indx;
c152c796
AM
11024 }
11025
310fd250
L
11026 if (bed->s->arch_size == 32)
11027 {
11028 r_type_mask = 0xff;
11029 r_sym_shift = 8;
11030 address_size = 4;
11031 }
11032 else
11033 {
11034 r_type_mask = 0xffffffff;
11035 r_sym_shift = 32;
11036 address_size = 8;
11037 }
11038
c152c796
AM
11039 /* Relocate the contents of each section. */
11040 sym_hashes = elf_sym_hashes (input_bfd);
11041 for (o = input_bfd->sections; o != NULL; o = o->next)
11042 {
11043 bfd_byte *contents;
11044
11045 if (! o->linker_mark)
11046 {
11047 /* This section was omitted from the link. */
11048 continue;
11049 }
11050
7bdf4127 11051 if (!flinfo->info->resolve_section_groups
bcacc0f5
AM
11052 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
11053 {
11054 /* Deal with the group signature symbol. */
11055 struct bfd_elf_section_data *sec_data = elf_section_data (o);
11056 unsigned long symndx = sec_data->this_hdr.sh_info;
11057 asection *osec = o->output_section;
11058
7bdf4127 11059 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
bcacc0f5
AM
11060 if (symndx >= locsymcount
11061 || (elf_bad_symtab (input_bfd)
8b127cbc 11062 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
11063 {
11064 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
11065 while (h->root.type == bfd_link_hash_indirect
11066 || h->root.type == bfd_link_hash_warning)
11067 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11068 /* Arrange for symbol to be output. */
11069 h->indx = -2;
11070 elf_section_data (osec)->this_hdr.sh_info = -2;
11071 }
11072 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
11073 {
11074 /* We'll use the output section target_index. */
8b127cbc 11075 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
11076 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
11077 }
11078 else
11079 {
8b127cbc 11080 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
11081 {
11082 /* Otherwise output the local symbol now. */
11083 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 11084 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 11085 const char *name;
6e0b88f1
AM
11086 long indx;
11087 int ret;
bcacc0f5
AM
11088
11089 name = bfd_elf_string_from_elf_section (input_bfd,
11090 symtab_hdr->sh_link,
11091 sym.st_name);
11092 if (name == NULL)
11093 return FALSE;
11094
11095 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11096 sec);
11097 if (sym.st_shndx == SHN_BAD)
11098 return FALSE;
11099
11100 sym.st_value += o->output_offset;
11101
6e0b88f1 11102 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
11103 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11104 NULL);
6e0b88f1 11105 if (ret == 0)
bcacc0f5 11106 return FALSE;
6e0b88f1 11107 else if (ret == 1)
8b127cbc 11108 flinfo->indices[symndx] = indx;
6e0b88f1
AM
11109 else
11110 abort ();
bcacc0f5
AM
11111 }
11112 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 11113 = flinfo->indices[symndx];
bcacc0f5
AM
11114 }
11115 }
11116
c152c796 11117 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 11118 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
11119 continue;
11120
11121 if ((o->flags & SEC_LINKER_CREATED) != 0)
11122 {
11123 /* Section was created by _bfd_elf_link_create_dynamic_sections
11124 or somesuch. */
11125 continue;
11126 }
11127
11128 /* Get the contents of the section. They have been cached by a
11129 relaxation routine. Note that o is a section in an input
11130 file, so the contents field will not have been set by any of
11131 the routines which work on output files. */
11132 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
11133 {
11134 contents = elf_section_data (o)->this_hdr.contents;
11135 if (bed->caches_rawsize
11136 && o->rawsize != 0
11137 && o->rawsize < o->size)
11138 {
11139 memcpy (flinfo->contents, contents, o->rawsize);
11140 contents = flinfo->contents;
11141 }
11142 }
c152c796
AM
11143 else
11144 {
8b127cbc 11145 contents = flinfo->contents;
4a114e3e 11146 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
11147 return FALSE;
11148 }
11149
11150 if ((o->flags & SEC_RELOC) != 0)
11151 {
11152 Elf_Internal_Rela *internal_relocs;
0f02bbd9 11153 Elf_Internal_Rela *rel, *relend;
0f02bbd9 11154 int action_discarded;
ece5ef60 11155 int ret;
c152c796
AM
11156
11157 /* Get the swapped relocs. */
11158 internal_relocs
8b127cbc
AM
11159 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
11160 flinfo->internal_relocs, FALSE);
c152c796
AM
11161 if (internal_relocs == NULL
11162 && o->reloc_count > 0)
11163 return FALSE;
11164
310fd250
L
11165 /* We need to reverse-copy input .ctors/.dtors sections if
11166 they are placed in .init_array/.finit_array for output. */
11167 if (o->size > address_size
11168 && ((strncmp (o->name, ".ctors", 6) == 0
11169 && strcmp (o->output_section->name,
11170 ".init_array") == 0)
11171 || (strncmp (o->name, ".dtors", 6) == 0
11172 && strcmp (o->output_section->name,
11173 ".fini_array") == 0))
11174 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 11175 {
056bafd4
MR
11176 if (o->size * bed->s->int_rels_per_ext_rel
11177 != o->reloc_count * address_size)
310fd250 11178 {
4eca0228 11179 _bfd_error_handler
695344c0 11180 /* xgettext:c-format */
871b3ab2 11181 (_("error: %pB: size of section %pA is not "
310fd250
L
11182 "multiple of address size"),
11183 input_bfd, o);
8c6716e5 11184 bfd_set_error (bfd_error_bad_value);
310fd250
L
11185 return FALSE;
11186 }
11187 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
11188 }
11189
0f02bbd9 11190 action_discarded = -1;
c152c796 11191 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
11192 action_discarded = (*bed->action_discarded) (o);
11193
11194 /* Run through the relocs evaluating complex reloc symbols and
11195 looking for relocs against symbols from discarded sections
11196 or section symbols from removed link-once sections.
11197 Complain about relocs against discarded sections. Zero
11198 relocs against removed link-once sections. */
11199
11200 rel = internal_relocs;
056bafd4 11201 relend = rel + o->reloc_count;
0f02bbd9 11202 for ( ; rel < relend; rel++)
c152c796 11203 {
0f02bbd9
AM
11204 unsigned long r_symndx = rel->r_info >> r_sym_shift;
11205 unsigned int s_type;
11206 asection **ps, *sec;
11207 struct elf_link_hash_entry *h = NULL;
11208 const char *sym_name;
c152c796 11209
0f02bbd9
AM
11210 if (r_symndx == STN_UNDEF)
11211 continue;
c152c796 11212
0f02bbd9
AM
11213 if (r_symndx >= locsymcount
11214 || (elf_bad_symtab (input_bfd)
8b127cbc 11215 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
11216 {
11217 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 11218
0f02bbd9
AM
11219 /* Badly formatted input files can contain relocs that
11220 reference non-existant symbols. Check here so that
11221 we do not seg fault. */
11222 if (h == NULL)
c152c796 11223 {
4eca0228 11224 _bfd_error_handler
695344c0 11225 /* xgettext:c-format */
2dcf00ce 11226 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
0f02bbd9 11227 "that references a non-existent global symbol"),
2dcf00ce 11228 input_bfd, (uint64_t) rel->r_info, o);
0f02bbd9
AM
11229 bfd_set_error (bfd_error_bad_value);
11230 return FALSE;
11231 }
3b36f7e6 11232
0f02bbd9
AM
11233 while (h->root.type == bfd_link_hash_indirect
11234 || h->root.type == bfd_link_hash_warning)
11235 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 11236
0f02bbd9 11237 s_type = h->type;
cdd3575c 11238
9e2dec47 11239 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
11240 mark the symbol as undefined. Note that the
11241 linker may attach linker created dynamic sections
11242 to the plugin bfd. Symbols defined in linker
11243 created sections are not plugin symbols. */
bc4e12de 11244 if ((h->root.non_ir_ref_regular
4070765b 11245 || h->root.non_ir_ref_dynamic)
9e2dec47
L
11246 && (h->root.type == bfd_link_hash_defined
11247 || h->root.type == bfd_link_hash_defweak)
11248 && (h->root.u.def.section->flags
11249 & SEC_LINKER_CREATED) == 0
11250 && h->root.u.def.section->owner != NULL
11251 && (h->root.u.def.section->owner->flags
11252 & BFD_PLUGIN) != 0)
11253 {
11254 h->root.type = bfd_link_hash_undefined;
11255 h->root.u.undef.abfd = h->root.u.def.section->owner;
11256 }
11257
0f02bbd9
AM
11258 ps = NULL;
11259 if (h->root.type == bfd_link_hash_defined
11260 || h->root.type == bfd_link_hash_defweak)
11261 ps = &h->root.u.def.section;
11262
11263 sym_name = h->root.root.string;
11264 }
11265 else
11266 {
11267 Elf_Internal_Sym *sym = isymbuf + r_symndx;
11268
11269 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 11270 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
11271 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11272 sym, *ps);
11273 }
c152c796 11274
c301e700 11275 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 11276 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
11277 {
11278 bfd_vma val;
11279 bfd_vma dot = (rel->r_offset
11280 + o->output_offset + o->output_section->vma);
11281#ifdef DEBUG
11282 printf ("Encountered a complex symbol!");
11283 printf (" (input_bfd %s, section %s, reloc %ld\n",
765cf5f6 11284 bfd_get_filename (input_bfd), o->name,
9ccb8af9 11285 (long) (rel - internal_relocs));
0f02bbd9
AM
11286 printf (" symbol: idx %8.8lx, name %s\n",
11287 r_symndx, sym_name);
11288 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11289 (unsigned long) rel->r_info,
11290 (unsigned long) rel->r_offset);
11291#endif
8b127cbc 11292 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
11293 isymbuf, locsymcount, s_type == STT_SRELC))
11294 return FALSE;
11295
11296 /* Symbol evaluated OK. Update to absolute value. */
11297 set_symbol_value (input_bfd, isymbuf, locsymcount,
11298 r_symndx, val);
11299 continue;
11300 }
11301
11302 if (action_discarded != -1 && ps != NULL)
11303 {
cdd3575c
AM
11304 /* Complain if the definition comes from a
11305 discarded section. */
dbaa2011 11306 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 11307 {
cf35638d 11308 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 11309 if (action_discarded & COMPLAIN)
8b127cbc 11310 (*flinfo->info->callbacks->einfo)
695344c0 11311 /* xgettext:c-format */
871b3ab2
AM
11312 (_("%X`%s' referenced in section `%pA' of %pB: "
11313 "defined in discarded section `%pA' of %pB\n"),
e1fffbe6 11314 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 11315
87e5235d 11316 /* Try to do the best we can to support buggy old
e0ae6d6f 11317 versions of gcc. Pretend that the symbol is
87e5235d
AM
11318 really defined in the kept linkonce section.
11319 FIXME: This is quite broken. Modifying the
11320 symbol here means we will be changing all later
e0ae6d6f 11321 uses of the symbol, not just in this section. */
0f02bbd9 11322 if (action_discarded & PRETEND)
87e5235d 11323 {
01b3c8ab
L
11324 asection *kept;
11325
c0f00686 11326 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 11327 flinfo->info);
01b3c8ab 11328 if (kept != NULL)
87e5235d
AM
11329 {
11330 *ps = kept;
11331 continue;
11332 }
11333 }
c152c796
AM
11334 }
11335 }
11336 }
11337
11338 /* Relocate the section by invoking a back end routine.
11339
11340 The back end routine is responsible for adjusting the
11341 section contents as necessary, and (if using Rela relocs
11342 and generating a relocatable output file) adjusting the
11343 reloc addend as necessary.
11344
11345 The back end routine does not have to worry about setting
11346 the reloc address or the reloc symbol index.
11347
11348 The back end routine is given a pointer to the swapped in
11349 internal symbols, and can access the hash table entries
11350 for the external symbols via elf_sym_hashes (input_bfd).
11351
11352 When generating relocatable output, the back end routine
11353 must handle STB_LOCAL/STT_SECTION symbols specially. The
11354 output symbol is going to be a section symbol
11355 corresponding to the output section, which will require
11356 the addend to be adjusted. */
11357
8b127cbc 11358 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
11359 input_bfd, o, contents,
11360 internal_relocs,
11361 isymbuf,
8b127cbc 11362 flinfo->sections);
ece5ef60 11363 if (!ret)
c152c796
AM
11364 return FALSE;
11365
ece5ef60 11366 if (ret == 2
0e1862bb 11367 || bfd_link_relocatable (flinfo->info)
8b127cbc 11368 || flinfo->info->emitrelocations)
c152c796
AM
11369 {
11370 Elf_Internal_Rela *irela;
d4730f92 11371 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
11372 bfd_vma last_offset;
11373 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
11374 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11375 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 11376 unsigned int next_erel;
c152c796 11377 bfd_boolean rela_normal;
d4730f92 11378 struct bfd_elf_section_data *esdi, *esdo;
c152c796 11379
d4730f92
BS
11380 esdi = elf_section_data (o);
11381 esdo = elf_section_data (o->output_section);
11382 rela_normal = FALSE;
c152c796
AM
11383
11384 /* Adjust the reloc addresses and symbol indices. */
11385
11386 irela = internal_relocs;
056bafd4 11387 irelaend = irela + o->reloc_count;
d4730f92
BS
11388 rel_hash = esdo->rel.hashes + esdo->rel.count;
11389 /* We start processing the REL relocs, if any. When we reach
11390 IRELAMID in the loop, we switch to the RELA relocs. */
11391 irelamid = irela;
11392 if (esdi->rel.hdr != NULL)
11393 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11394 * bed->s->int_rels_per_ext_rel);
eac338cf 11395 rel_hash_list = rel_hash;
d4730f92 11396 rela_hash_list = NULL;
c152c796 11397 last_offset = o->output_offset;
0e1862bb 11398 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11399 last_offset += o->output_section->vma;
11400 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11401 {
11402 unsigned long r_symndx;
11403 asection *sec;
11404 Elf_Internal_Sym sym;
11405
11406 if (next_erel == bed->s->int_rels_per_ext_rel)
11407 {
11408 rel_hash++;
11409 next_erel = 0;
11410 }
11411
d4730f92
BS
11412 if (irela == irelamid)
11413 {
11414 rel_hash = esdo->rela.hashes + esdo->rela.count;
11415 rela_hash_list = rel_hash;
11416 rela_normal = bed->rela_normal;
11417 }
11418
c152c796 11419 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 11420 flinfo->info, o,
c152c796
AM
11421 irela->r_offset);
11422 if (irela->r_offset >= (bfd_vma) -2)
11423 {
11424 /* This is a reloc for a deleted entry or somesuch.
11425 Turn it into an R_*_NONE reloc, at the same
11426 offset as the last reloc. elf_eh_frame.c and
e460dd0d 11427 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
11428 being ordered. */
11429 irela->r_offset = last_offset;
11430 irela->r_info = 0;
11431 irela->r_addend = 0;
11432 continue;
11433 }
11434
11435 irela->r_offset += o->output_offset;
11436
11437 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 11438 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11439 irela->r_offset += o->output_section->vma;
11440
11441 last_offset = irela->r_offset;
11442
11443 r_symndx = irela->r_info >> r_sym_shift;
11444 if (r_symndx == STN_UNDEF)
11445 continue;
11446
11447 if (r_symndx >= locsymcount
11448 || (elf_bad_symtab (input_bfd)
8b127cbc 11449 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
11450 {
11451 struct elf_link_hash_entry *rh;
11452 unsigned long indx;
11453
11454 /* This is a reloc against a global symbol. We
11455 have not yet output all the local symbols, so
11456 we do not know the symbol index of any global
11457 symbol. We set the rel_hash entry for this
11458 reloc to point to the global hash table entry
11459 for this symbol. The symbol index is then
ee75fd95 11460 set at the end of bfd_elf_final_link. */
c152c796
AM
11461 indx = r_symndx - extsymoff;
11462 rh = elf_sym_hashes (input_bfd)[indx];
11463 while (rh->root.type == bfd_link_hash_indirect
11464 || rh->root.type == bfd_link_hash_warning)
11465 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11466
11467 /* Setting the index to -2 tells
11468 elf_link_output_extsym that this symbol is
11469 used by a reloc. */
11470 BFD_ASSERT (rh->indx < 0);
11471 rh->indx = -2;
c152c796
AM
11472 *rel_hash = rh;
11473
11474 continue;
11475 }
11476
11477 /* This is a reloc against a local symbol. */
11478
11479 *rel_hash = NULL;
11480 sym = isymbuf[r_symndx];
8b127cbc 11481 sec = flinfo->sections[r_symndx];
c152c796
AM
11482 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11483 {
11484 /* I suppose the backend ought to fill in the
11485 section of any STT_SECTION symbol against a
6a8d1586 11486 processor specific section. */
cf35638d 11487 r_symndx = STN_UNDEF;
6a8d1586
AM
11488 if (bfd_is_abs_section (sec))
11489 ;
c152c796
AM
11490 else if (sec == NULL || sec->owner == NULL)
11491 {
11492 bfd_set_error (bfd_error_bad_value);
11493 return FALSE;
11494 }
11495 else
11496 {
6a8d1586
AM
11497 asection *osec = sec->output_section;
11498
11499 /* If we have discarded a section, the output
11500 section will be the absolute section. In
ab96bf03
AM
11501 case of discarded SEC_MERGE sections, use
11502 the kept section. relocate_section should
11503 have already handled discarded linkonce
11504 sections. */
6a8d1586
AM
11505 if (bfd_is_abs_section (osec)
11506 && sec->kept_section != NULL
11507 && sec->kept_section->output_section != NULL)
11508 {
11509 osec = sec->kept_section->output_section;
11510 irela->r_addend -= osec->vma;
11511 }
11512
11513 if (!bfd_is_abs_section (osec))
11514 {
11515 r_symndx = osec->target_index;
cf35638d 11516 if (r_symndx == STN_UNDEF)
74541ad4 11517 {
051d833a
AM
11518 irela->r_addend += osec->vma;
11519 osec = _bfd_nearby_section (output_bfd, osec,
11520 osec->vma);
11521 irela->r_addend -= osec->vma;
11522 r_symndx = osec->target_index;
74541ad4 11523 }
6a8d1586 11524 }
c152c796
AM
11525 }
11526
11527 /* Adjust the addend according to where the
11528 section winds up in the output section. */
11529 if (rela_normal)
11530 irela->r_addend += sec->output_offset;
11531 }
11532 else
11533 {
8b127cbc 11534 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
11535 {
11536 unsigned long shlink;
11537 const char *name;
11538 asection *osec;
6e0b88f1 11539 long indx;
c152c796 11540
8b127cbc 11541 if (flinfo->info->strip == strip_all)
c152c796
AM
11542 {
11543 /* You can't do ld -r -s. */
11544 bfd_set_error (bfd_error_invalid_operation);
11545 return FALSE;
11546 }
11547
11548 /* This symbol was skipped earlier, but
11549 since it is needed by a reloc, we
11550 must output it now. */
11551 shlink = symtab_hdr->sh_link;
11552 name = (bfd_elf_string_from_elf_section
11553 (input_bfd, shlink, sym.st_name));
11554 if (name == NULL)
11555 return FALSE;
11556
11557 osec = sec->output_section;
11558 sym.st_shndx =
11559 _bfd_elf_section_from_bfd_section (output_bfd,
11560 osec);
11561 if (sym.st_shndx == SHN_BAD)
11562 return FALSE;
11563
11564 sym.st_value += sec->output_offset;
0e1862bb 11565 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11566 {
11567 sym.st_value += osec->vma;
11568 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11569 {
102def4d
AM
11570 struct elf_link_hash_table *htab
11571 = elf_hash_table (flinfo->info);
11572
c152c796
AM
11573 /* STT_TLS symbols are relative to PT_TLS
11574 segment base. */
102def4d
AM
11575 if (htab->tls_sec != NULL)
11576 sym.st_value -= htab->tls_sec->vma;
11577 else
11578 sym.st_info
11579 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11580 STT_NOTYPE);
c152c796
AM
11581 }
11582 }
11583
6e0b88f1 11584 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
11585 ret = elf_link_output_symstrtab (flinfo, name,
11586 &sym, sec,
11587 NULL);
6e0b88f1 11588 if (ret == 0)
c152c796 11589 return FALSE;
6e0b88f1 11590 else if (ret == 1)
8b127cbc 11591 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
11592 else
11593 abort ();
c152c796
AM
11594 }
11595
8b127cbc 11596 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
11597 }
11598
11599 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11600 | (irela->r_info & r_type_mask));
11601 }
11602
11603 /* Swap out the relocs. */
d4730f92
BS
11604 input_rel_hdr = esdi->rel.hdr;
11605 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 11606 {
d4730f92
BS
11607 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11608 input_rel_hdr,
11609 internal_relocs,
11610 rel_hash_list))
11611 return FALSE;
c152c796
AM
11612 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11613 * bed->s->int_rels_per_ext_rel);
eac338cf 11614 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
11615 }
11616
11617 input_rela_hdr = esdi->rela.hdr;
11618 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11619 {
eac338cf 11620 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 11621 input_rela_hdr,
eac338cf 11622 internal_relocs,
d4730f92 11623 rela_hash_list))
c152c796
AM
11624 return FALSE;
11625 }
11626 }
11627 }
11628
11629 /* Write out the modified section contents. */
11630 if (bed->elf_backend_write_section
8b127cbc 11631 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 11632 contents))
c152c796
AM
11633 {
11634 /* Section written out. */
11635 }
11636 else switch (o->sec_info_type)
11637 {
dbaa2011 11638 case SEC_INFO_TYPE_STABS:
c152c796
AM
11639 if (! (_bfd_write_section_stabs
11640 (output_bfd,
8b127cbc 11641 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
11642 o, &elf_section_data (o)->sec_info, contents)))
11643 return FALSE;
11644 break;
dbaa2011 11645 case SEC_INFO_TYPE_MERGE:
c152c796
AM
11646 if (! _bfd_write_merged_section (output_bfd, o,
11647 elf_section_data (o)->sec_info))
11648 return FALSE;
11649 break;
dbaa2011 11650 case SEC_INFO_TYPE_EH_FRAME:
c152c796 11651 {
8b127cbc 11652 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
11653 o, contents))
11654 return FALSE;
11655 }
11656 break;
2f0c68f2
CM
11657 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11658 {
11659 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11660 flinfo->info,
11661 o, contents))
11662 return FALSE;
11663 }
11664 break;
c152c796
AM
11665 default:
11666 {
310fd250
L
11667 if (! (o->flags & SEC_EXCLUDE))
11668 {
11669 file_ptr offset = (file_ptr) o->output_offset;
11670 bfd_size_type todo = o->size;
37b01f6a 11671
bb294208 11672 offset *= bfd_octets_per_byte (output_bfd, o);
37b01f6a 11673
310fd250
L
11674 if ((o->flags & SEC_ELF_REVERSE_COPY))
11675 {
11676 /* Reverse-copy input section to output. */
11677 do
11678 {
11679 todo -= address_size;
11680 if (! bfd_set_section_contents (output_bfd,
11681 o->output_section,
11682 contents + todo,
11683 offset,
11684 address_size))
11685 return FALSE;
11686 if (todo == 0)
11687 break;
11688 offset += address_size;
11689 }
11690 while (1);
11691 }
11692 else if (! bfd_set_section_contents (output_bfd,
11693 o->output_section,
11694 contents,
11695 offset, todo))
11696 return FALSE;
11697 }
c152c796
AM
11698 }
11699 break;
11700 }
11701 }
11702
11703 return TRUE;
11704}
11705
11706/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 11707 requested by the linker, and does not come from any input file. This
c152c796
AM
11708 is used to build constructor and destructor tables when linking
11709 with -Ur. */
11710
11711static bfd_boolean
11712elf_reloc_link_order (bfd *output_bfd,
11713 struct bfd_link_info *info,
11714 asection *output_section,
11715 struct bfd_link_order *link_order)
11716{
11717 reloc_howto_type *howto;
11718 long indx;
11719 bfd_vma offset;
11720 bfd_vma addend;
d4730f92 11721 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
11722 struct elf_link_hash_entry **rel_hash_ptr;
11723 Elf_Internal_Shdr *rel_hdr;
11724 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11725 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11726 bfd_byte *erel;
11727 unsigned int i;
d4730f92 11728 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
11729
11730 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11731 if (howto == NULL)
11732 {
11733 bfd_set_error (bfd_error_bad_value);
11734 return FALSE;
11735 }
11736
11737 addend = link_order->u.reloc.p->addend;
11738
d4730f92
BS
11739 if (esdo->rel.hdr)
11740 reldata = &esdo->rel;
11741 else if (esdo->rela.hdr)
11742 reldata = &esdo->rela;
11743 else
11744 {
11745 reldata = NULL;
11746 BFD_ASSERT (0);
11747 }
11748
c152c796 11749 /* Figure out the symbol index. */
d4730f92 11750 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
11751 if (link_order->type == bfd_section_reloc_link_order)
11752 {
11753 indx = link_order->u.reloc.p->u.section->target_index;
11754 BFD_ASSERT (indx != 0);
11755 *rel_hash_ptr = NULL;
11756 }
11757 else
11758 {
11759 struct elf_link_hash_entry *h;
11760
11761 /* Treat a reloc against a defined symbol as though it were
11762 actually against the section. */
11763 h = ((struct elf_link_hash_entry *)
11764 bfd_wrapped_link_hash_lookup (output_bfd, info,
11765 link_order->u.reloc.p->u.name,
11766 FALSE, FALSE, TRUE));
11767 if (h != NULL
11768 && (h->root.type == bfd_link_hash_defined
11769 || h->root.type == bfd_link_hash_defweak))
11770 {
11771 asection *section;
11772
11773 section = h->root.u.def.section;
11774 indx = section->output_section->target_index;
11775 *rel_hash_ptr = NULL;
11776 /* It seems that we ought to add the symbol value to the
11777 addend here, but in practice it has already been added
11778 because it was passed to constructor_callback. */
11779 addend += section->output_section->vma + section->output_offset;
11780 }
11781 else if (h != NULL)
11782 {
11783 /* Setting the index to -2 tells elf_link_output_extsym that
11784 this symbol is used by a reloc. */
11785 h->indx = -2;
11786 *rel_hash_ptr = h;
11787 indx = 0;
11788 }
11789 else
11790 {
1a72702b
AM
11791 (*info->callbacks->unattached_reloc)
11792 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
c152c796
AM
11793 indx = 0;
11794 }
11795 }
11796
11797 /* If this is an inplace reloc, we must write the addend into the
11798 object file. */
11799 if (howto->partial_inplace && addend != 0)
11800 {
11801 bfd_size_type size;
11802 bfd_reloc_status_type rstat;
11803 bfd_byte *buf;
11804 bfd_boolean ok;
11805 const char *sym_name;
bb294208 11806 bfd_size_type octets;
c152c796 11807
a50b1753
NC
11808 size = (bfd_size_type) bfd_get_reloc_size (howto);
11809 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 11810 if (buf == NULL && size != 0)
c152c796
AM
11811 return FALSE;
11812 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11813 switch (rstat)
11814 {
11815 case bfd_reloc_ok:
11816 break;
11817
11818 default:
11819 case bfd_reloc_outofrange:
11820 abort ();
11821
11822 case bfd_reloc_overflow:
11823 if (link_order->type == bfd_section_reloc_link_order)
fd361982 11824 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
c152c796
AM
11825 else
11826 sym_name = link_order->u.reloc.p->u.name;
1a72702b
AM
11827 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11828 howto->name, addend, NULL, NULL,
11829 (bfd_vma) 0);
c152c796
AM
11830 break;
11831 }
37b01f6a 11832
bb294208
AM
11833 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
11834 output_section);
c152c796 11835 ok = bfd_set_section_contents (output_bfd, output_section, buf,
bb294208 11836 octets, size);
c152c796
AM
11837 free (buf);
11838 if (! ok)
11839 return FALSE;
11840 }
11841
11842 /* The address of a reloc is relative to the section in a
11843 relocatable file, and is a virtual address in an executable
11844 file. */
11845 offset = link_order->offset;
0e1862bb 11846 if (! bfd_link_relocatable (info))
c152c796
AM
11847 offset += output_section->vma;
11848
11849 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11850 {
11851 irel[i].r_offset = offset;
11852 irel[i].r_info = 0;
11853 irel[i].r_addend = 0;
11854 }
11855 if (bed->s->arch_size == 32)
11856 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11857 else
11858 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11859
d4730f92 11860 rel_hdr = reldata->hdr;
c152c796
AM
11861 erel = rel_hdr->contents;
11862 if (rel_hdr->sh_type == SHT_REL)
11863 {
d4730f92 11864 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
11865 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11866 }
11867 else
11868 {
11869 irel[0].r_addend = addend;
d4730f92 11870 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
11871 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11872 }
11873
d4730f92 11874 ++reldata->count;
c152c796
AM
11875
11876 return TRUE;
11877}
11878
76359541
TP
11879/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11880 Returns TRUE upon success, FALSE otherwise. */
11881
11882static bfd_boolean
11883elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11884{
11885 bfd_boolean ret = FALSE;
11886 bfd *implib_bfd;
11887 const struct elf_backend_data *bed;
11888 flagword flags;
11889 enum bfd_architecture arch;
11890 unsigned int mach;
11891 asymbol **sympp = NULL;
11892 long symsize;
11893 long symcount;
11894 long src_count;
11895 elf_symbol_type *osymbuf;
446f7ed5 11896 size_t amt;
76359541
TP
11897
11898 implib_bfd = info->out_implib_bfd;
11899 bed = get_elf_backend_data (abfd);
11900
11901 if (!bfd_set_format (implib_bfd, bfd_object))
11902 return FALSE;
11903
046734ff 11904 /* Use flag from executable but make it a relocatable object. */
76359541
TP
11905 flags = bfd_get_file_flags (abfd);
11906 flags &= ~HAS_RELOC;
11907 if (!bfd_set_start_address (implib_bfd, 0)
046734ff 11908 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
76359541
TP
11909 return FALSE;
11910
11911 /* Copy architecture of output file to import library file. */
11912 arch = bfd_get_arch (abfd);
11913 mach = bfd_get_mach (abfd);
11914 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11915 && (abfd->target_defaulted
11916 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11917 return FALSE;
11918
11919 /* Get symbol table size. */
11920 symsize = bfd_get_symtab_upper_bound (abfd);
11921 if (symsize < 0)
11922 return FALSE;
11923
11924 /* Read in the symbol table. */
ec9bd0a2
AM
11925 sympp = (asymbol **) bfd_malloc (symsize);
11926 if (sympp == NULL)
11927 return FALSE;
11928
76359541
TP
11929 symcount = bfd_canonicalize_symtab (abfd, sympp);
11930 if (symcount < 0)
11931 goto free_sym_buf;
11932
11933 /* Allow the BFD backend to copy any private header data it
11934 understands from the output BFD to the import library BFD. */
11935 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11936 goto free_sym_buf;
11937
11938 /* Filter symbols to appear in the import library. */
11939 if (bed->elf_backend_filter_implib_symbols)
11940 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11941 symcount);
11942 else
11943 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11944 if (symcount == 0)
11945 {
5df1bc57 11946 bfd_set_error (bfd_error_no_symbols);
871b3ab2 11947 _bfd_error_handler (_("%pB: no symbol found for import library"),
4eca0228 11948 implib_bfd);
76359541
TP
11949 goto free_sym_buf;
11950 }
11951
11952
11953 /* Make symbols absolute. */
446f7ed5
AM
11954 amt = symcount * sizeof (*osymbuf);
11955 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
ec9bd0a2
AM
11956 if (osymbuf == NULL)
11957 goto free_sym_buf;
11958
76359541
TP
11959 for (src_count = 0; src_count < symcount; src_count++)
11960 {
11961 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11962 sizeof (*osymbuf));
11963 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11964 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11965 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11966 osymbuf[src_count].internal_elf_sym.st_value =
11967 osymbuf[src_count].symbol.value;
11968 sympp[src_count] = &osymbuf[src_count].symbol;
11969 }
11970
11971 bfd_set_symtab (implib_bfd, sympp, symcount);
11972
11973 /* Allow the BFD backend to copy any private data it understands
11974 from the output BFD to the import library BFD. This is done last
11975 to permit the routine to look at the filtered symbol table. */
11976 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11977 goto free_sym_buf;
11978
11979 if (!bfd_close (implib_bfd))
11980 goto free_sym_buf;
11981
11982 ret = TRUE;
11983
dc1e8a47 11984 free_sym_buf:
76359541
TP
11985 free (sympp);
11986 return ret;
11987}
11988
9f7c3e5e
AM
11989static void
11990elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11991{
11992 asection *o;
11993
11994 if (flinfo->symstrtab != NULL)
ef10c3ac 11995 _bfd_elf_strtab_free (flinfo->symstrtab);
c9594989
AM
11996 free (flinfo->contents);
11997 free (flinfo->external_relocs);
11998 free (flinfo->internal_relocs);
11999 free (flinfo->external_syms);
12000 free (flinfo->locsym_shndx);
12001 free (flinfo->internal_syms);
12002 free (flinfo->indices);
12003 free (flinfo->sections);
12004 if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
9f7c3e5e
AM
12005 free (flinfo->symshndxbuf);
12006 for (o = obfd->sections; o != NULL; o = o->next)
12007 {
12008 struct bfd_elf_section_data *esdo = elf_section_data (o);
c9594989
AM
12009 free (esdo->rel.hashes);
12010 free (esdo->rela.hashes);
9f7c3e5e
AM
12011 }
12012}
0b52efa6 12013
c152c796
AM
12014/* Do the final step of an ELF link. */
12015
12016bfd_boolean
12017bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
12018{
12019 bfd_boolean dynamic;
12020 bfd_boolean emit_relocs;
12021 bfd *dynobj;
8b127cbc 12022 struct elf_final_link_info flinfo;
91d6fa6a
NC
12023 asection *o;
12024 struct bfd_link_order *p;
12025 bfd *sub;
c152c796
AM
12026 bfd_size_type max_contents_size;
12027 bfd_size_type max_external_reloc_size;
12028 bfd_size_type max_internal_reloc_count;
12029 bfd_size_type max_sym_count;
12030 bfd_size_type max_sym_shndx_count;
c152c796
AM
12031 Elf_Internal_Sym elfsym;
12032 unsigned int i;
12033 Elf_Internal_Shdr *symtab_hdr;
12034 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
12035 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12036 struct elf_outext_info eoinfo;
12037 bfd_boolean merged;
12038 size_t relativecount = 0;
12039 asection *reldyn = 0;
12040 bfd_size_type amt;
104d59d1
JM
12041 asection *attr_section = NULL;
12042 bfd_vma attr_size = 0;
12043 const char *std_attrs_section;
64f52338 12044 struct elf_link_hash_table *htab = elf_hash_table (info);
4c6ee646 12045 bfd_boolean sections_removed;
496afd17 12046 bfd_boolean ret;
c152c796 12047
64f52338 12048 if (!is_elf_hash_table (htab))
c152c796
AM
12049 return FALSE;
12050
0e1862bb 12051 if (bfd_link_pic (info))
c152c796
AM
12052 abfd->flags |= DYNAMIC;
12053
64f52338
AM
12054 dynamic = htab->dynamic_sections_created;
12055 dynobj = htab->dynobj;
c152c796 12056
0e1862bb 12057 emit_relocs = (bfd_link_relocatable (info)
a4676736 12058 || info->emitrelocations);
c152c796 12059
496afd17 12060 memset (&flinfo, 0, sizeof (flinfo));
8b127cbc
AM
12061 flinfo.info = info;
12062 flinfo.output_bfd = abfd;
ef10c3ac 12063 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 12064 if (flinfo.symstrtab == NULL)
c152c796
AM
12065 return FALSE;
12066
12067 if (! dynamic)
12068 {
8b127cbc
AM
12069 flinfo.hash_sec = NULL;
12070 flinfo.symver_sec = NULL;
c152c796
AM
12071 }
12072 else
12073 {
3d4d4302 12074 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 12075 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 12076 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
12077 /* Note that it is OK if symver_sec is NULL. */
12078 }
12079
496afd17
L
12080 if (info->unique_symbol
12081 && !bfd_hash_table_init (&flinfo.local_hash_table,
12082 local_hash_newfunc,
12083 sizeof (struct local_hash_entry)))
12084 return FALSE;
c152c796 12085
104d59d1
JM
12086 /* The object attributes have been merged. Remove the input
12087 sections from the link, and set the contents of the output
1ff6de03 12088 section. */
4c6ee646 12089 sections_removed = FALSE;
104d59d1
JM
12090 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
12091 for (o = abfd->sections; o != NULL; o = o->next)
12092 {
5270eddc 12093 bfd_boolean remove_section = FALSE;
b8a6ced7 12094
104d59d1
JM
12095 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
12096 || strcmp (o->name, ".gnu.attributes") == 0)
12097 {
12098 for (p = o->map_head.link_order; p != NULL; p = p->next)
12099 {
12100 asection *input_section;
12101
12102 if (p->type != bfd_indirect_link_order)
12103 continue;
12104 input_section = p->u.indirect.section;
12105 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12106 elf_link_input_bfd ignores this section. */
12107 input_section->flags &= ~SEC_HAS_CONTENTS;
12108 }
a0c8462f 12109
104d59d1 12110 attr_size = bfd_elf_obj_attr_size (abfd);
fd361982 12111 bfd_set_section_size (o, attr_size);
b8a6ced7
AM
12112 /* Skip this section later on. */
12113 o->map_head.link_order = NULL;
104d59d1 12114 if (attr_size)
b8a6ced7 12115 attr_section = o;
104d59d1 12116 else
5270eddc 12117 remove_section = TRUE;
104d59d1 12118 }
6e5e9d58
AM
12119 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12120 {
12121 /* Remove empty group section from linker output. */
5270eddc 12122 remove_section = TRUE;
b8a6ced7 12123 }
5270eddc 12124 if (remove_section)
b8a6ced7 12125 {
6e5e9d58
AM
12126 o->flags |= SEC_EXCLUDE;
12127 bfd_section_list_remove (abfd, o);
12128 abfd->section_count--;
4c6ee646 12129 sections_removed = TRUE;
6e5e9d58 12130 }
104d59d1 12131 }
4c6ee646
AM
12132 if (sections_removed)
12133 _bfd_fix_excluded_sec_syms (abfd, info);
104d59d1 12134
c152c796
AM
12135 /* Count up the number of relocations we will output for each output
12136 section, so that we know the sizes of the reloc sections. We
12137 also figure out some maximum sizes. */
12138 max_contents_size = 0;
12139 max_external_reloc_size = 0;
12140 max_internal_reloc_count = 0;
12141 max_sym_count = 0;
12142 max_sym_shndx_count = 0;
12143 merged = FALSE;
12144 for (o = abfd->sections; o != NULL; o = o->next)
12145 {
12146 struct bfd_elf_section_data *esdo = elf_section_data (o);
12147 o->reloc_count = 0;
12148
8423293d 12149 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
12150 {
12151 unsigned int reloc_count = 0;
9eaff861 12152 unsigned int additional_reloc_count = 0;
c152c796 12153 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
12154
12155 if (p->type == bfd_section_reloc_link_order
12156 || p->type == bfd_symbol_reloc_link_order)
12157 reloc_count = 1;
12158 else if (p->type == bfd_indirect_link_order)
12159 {
12160 asection *sec;
12161
12162 sec = p->u.indirect.section;
c152c796
AM
12163
12164 /* Mark all sections which are to be included in the
12165 link. This will normally be every section. We need
12166 to do this so that we can identify any sections which
12167 the linker has decided to not include. */
12168 sec->linker_mark = TRUE;
12169
12170 if (sec->flags & SEC_MERGE)
12171 merged = TRUE;
12172
eea6121a
AM
12173 if (sec->rawsize > max_contents_size)
12174 max_contents_size = sec->rawsize;
12175 if (sec->size > max_contents_size)
12176 max_contents_size = sec->size;
c152c796 12177
c152c796
AM
12178 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12179 && (sec->owner->flags & DYNAMIC) == 0)
12180 {
12181 size_t sym_count;
12182
a961cdd5
AM
12183 /* We are interested in just local symbols, not all
12184 symbols. */
c152c796
AM
12185 if (elf_bad_symtab (sec->owner))
12186 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12187 / bed->s->sizeof_sym);
12188 else
12189 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12190
12191 if (sym_count > max_sym_count)
12192 max_sym_count = sym_count;
12193
12194 if (sym_count > max_sym_shndx_count
6a40cf0c 12195 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
12196 max_sym_shndx_count = sym_count;
12197
a961cdd5
AM
12198 if (esdo->this_hdr.sh_type == SHT_REL
12199 || esdo->this_hdr.sh_type == SHT_RELA)
12200 /* Some backends use reloc_count in relocation sections
12201 to count particular types of relocs. Of course,
12202 reloc sections themselves can't have relocations. */
12203 ;
12204 else if (emit_relocs)
12205 {
12206 reloc_count = sec->reloc_count;
12207 if (bed->elf_backend_count_additional_relocs)
12208 {
12209 int c;
12210 c = (*bed->elf_backend_count_additional_relocs) (sec);
12211 additional_reloc_count += c;
12212 }
12213 }
12214 else if (bed->elf_backend_count_relocs)
12215 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12216
12217 esdi = elf_section_data (sec);
12218
c152c796
AM
12219 if ((sec->flags & SEC_RELOC) != 0)
12220 {
d4730f92 12221 size_t ext_size = 0;
c152c796 12222
d4730f92
BS
12223 if (esdi->rel.hdr != NULL)
12224 ext_size = esdi->rel.hdr->sh_size;
12225 if (esdi->rela.hdr != NULL)
12226 ext_size += esdi->rela.hdr->sh_size;
7326c758 12227
c152c796
AM
12228 if (ext_size > max_external_reloc_size)
12229 max_external_reloc_size = ext_size;
12230 if (sec->reloc_count > max_internal_reloc_count)
12231 max_internal_reloc_count = sec->reloc_count;
12232 }
12233 }
12234 }
12235
12236 if (reloc_count == 0)
12237 continue;
12238
9eaff861 12239 reloc_count += additional_reloc_count;
c152c796
AM
12240 o->reloc_count += reloc_count;
12241
0e1862bb 12242 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 12243 {
d4730f92 12244 if (esdi->rel.hdr)
9eaff861 12245 {
491d01d3 12246 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
9eaff861
AO
12247 esdo->rel.count += additional_reloc_count;
12248 }
d4730f92 12249 if (esdi->rela.hdr)
9eaff861 12250 {
491d01d3 12251 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
9eaff861
AO
12252 esdo->rela.count += additional_reloc_count;
12253 }
d4730f92
BS
12254 }
12255 else
12256 {
12257 if (o->use_rela_p)
12258 esdo->rela.count += reloc_count;
2c2b4ed4 12259 else
d4730f92 12260 esdo->rel.count += reloc_count;
c152c796 12261 }
c152c796
AM
12262 }
12263
9eaff861 12264 if (o->reloc_count > 0)
c152c796
AM
12265 o->flags |= SEC_RELOC;
12266 else
12267 {
12268 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12269 set it (this is probably a bug) and if it is set
12270 assign_section_numbers will create a reloc section. */
12271 o->flags &=~ SEC_RELOC;
12272 }
12273
12274 /* If the SEC_ALLOC flag is not set, force the section VMA to
12275 zero. This is done in elf_fake_sections as well, but forcing
12276 the VMA to 0 here will ensure that relocs against these
12277 sections are handled correctly. */
12278 if ((o->flags & SEC_ALLOC) == 0
12279 && ! o->user_set_vma)
12280 o->vma = 0;
12281 }
12282
0e1862bb 12283 if (! bfd_link_relocatable (info) && merged)
64f52338 12284 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
c152c796
AM
12285
12286 /* Figure out the file positions for everything but the symbol table
12287 and the relocs. We set symcount to force assign_section_numbers
12288 to create a symbol table. */
ed48ec2e 12289 abfd->symcount = info->strip != strip_all || emit_relocs;
c152c796
AM
12290 BFD_ASSERT (! abfd->output_has_begun);
12291 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12292 goto error_return;
12293
ee75fd95 12294 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
12295 for (o = abfd->sections; o != NULL; o = o->next)
12296 {
d4730f92 12297 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
12298 if ((o->flags & SEC_RELOC) != 0)
12299 {
d4730f92 12300 if (esdo->rel.hdr
9eaff861 12301 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
12302 goto error_return;
12303
d4730f92 12304 if (esdo->rela.hdr
9eaff861 12305 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
12306 goto error_return;
12307 }
12308
48db3297
AM
12309 /* _bfd_elf_compute_section_file_positions makes temporary use
12310 of target_index. Reset it. */
12311 o->target_index = 0;
12312
c152c796
AM
12313 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12314 to count upwards while actually outputting the relocations. */
d4730f92
BS
12315 esdo->rel.count = 0;
12316 esdo->rela.count = 0;
0ce398f1 12317
1ff6de03
NA
12318 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12319 && !bfd_section_is_ctf (o))
0ce398f1
L
12320 {
12321 /* Cache the section contents so that they can be compressed
12322 later. Use bfd_malloc since it will be freed by
12323 bfd_compress_section_contents. */
12324 unsigned char *contents = esdo->this_hdr.contents;
12325 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
12326 abort ();
12327 contents
12328 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12329 if (contents == NULL)
12330 goto error_return;
12331 esdo->this_hdr.contents = contents;
12332 }
c152c796
AM
12333 }
12334
1ff6de03
NA
12335 /* We have now assigned file positions for all the sections except .symtab,
12336 .strtab, and non-loaded reloc and compressed debugging sections. We start
12337 the .symtab section at the current file position, and write directly to it.
12338 We build the .strtab section in memory. */
ed48ec2e 12339 abfd->symcount = 0;
c152c796
AM
12340 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12341 /* sh_name is set in prep_headers. */
12342 symtab_hdr->sh_type = SHT_SYMTAB;
12343 /* sh_flags, sh_addr and sh_size all start off zero. */
12344 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12345 /* sh_link is set in assign_section_numbers. */
12346 /* sh_info is set below. */
12347 /* sh_offset is set just below. */
72de5009 12348 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 12349
ef10c3ac
L
12350 if (max_sym_count < 20)
12351 max_sym_count = 20;
64f52338 12352 htab->strtabsize = max_sym_count;
ef10c3ac 12353 amt = max_sym_count * sizeof (struct elf_sym_strtab);
64f52338
AM
12354 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12355 if (htab->strtab == NULL)
c152c796 12356 goto error_return;
ef10c3ac
L
12357 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12358 flinfo.symshndxbuf
12359 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12360 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 12361
8539e4e8 12362 if (info->strip != strip_all || emit_relocs)
c152c796 12363 {
8539e4e8
AM
12364 file_ptr off = elf_next_file_pos (abfd);
12365
12366 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
12367
12368 /* Note that at this point elf_next_file_pos (abfd) is
12369 incorrect. We do not yet know the size of the .symtab section.
12370 We correct next_file_pos below, after we do know the size. */
12371
12372 /* Start writing out the symbol table. The first symbol is always a
12373 dummy symbol. */
c152c796
AM
12374 elfsym.st_value = 0;
12375 elfsym.st_size = 0;
12376 elfsym.st_info = 0;
12377 elfsym.st_other = 0;
12378 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 12379 elfsym.st_target_internal = 0;
ef10c3ac
L
12380 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12381 bfd_und_section_ptr, NULL) != 1)
c152c796 12382 goto error_return;
c152c796 12383
d1bcae83
L
12384 /* Output a symbol for each section if asked or they are used for
12385 relocs. These symbols usually have no names. We store the
12386 index of each one in the index field of the section, so that
12387 we can find it again when outputting relocs. */
c77cb2a0 12388
d1bcae83 12389 if (bfd_keep_unused_section_symbols (abfd) || emit_relocs)
c152c796 12390 {
d1bcae83
L
12391 bfd_boolean name_local_sections
12392 = (bed->elf_backend_name_local_section_symbols
12393 && bed->elf_backend_name_local_section_symbols (abfd));
12394 const char *name = NULL;
12395
12396 elfsym.st_size = 0;
12397 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12398 elfsym.st_other = 0;
12399 elfsym.st_value = 0;
12400 elfsym.st_target_internal = 0;
12401 for (i = 1; i < elf_numsections (abfd); i++)
f0b5bb34 12402 {
d1bcae83
L
12403 o = bfd_section_from_elf_index (abfd, i);
12404 if (o != NULL)
12405 {
12406 o->target_index = bfd_get_symcount (abfd);
12407 elfsym.st_shndx = i;
12408 if (!bfd_link_relocatable (info))
12409 elfsym.st_value = o->vma;
12410 if (name_local_sections)
12411 name = o->name;
12412 if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
12413 NULL) != 1)
12414 goto error_return;
12415 }
f0b5bb34 12416 }
c152c796
AM
12417 }
12418 }
12419
3f1b17bb
MR
12420 /* On some targets like Irix 5 the symbol split between local and global
12421 ones recorded in the sh_info field needs to be done between section
12422 and all other symbols. */
12423 if (bed->elf_backend_elfsym_local_is_section
12424 && bed->elf_backend_elfsym_local_is_section (abfd))
12425 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12426
c152c796
AM
12427 /* Allocate some memory to hold information read in from the input
12428 files. */
12429 if (max_contents_size != 0)
12430 {
8b127cbc
AM
12431 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12432 if (flinfo.contents == NULL)
c152c796
AM
12433 goto error_return;
12434 }
12435
12436 if (max_external_reloc_size != 0)
12437 {
8b127cbc
AM
12438 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12439 if (flinfo.external_relocs == NULL)
c152c796
AM
12440 goto error_return;
12441 }
12442
12443 if (max_internal_reloc_count != 0)
12444 {
056bafd4 12445 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
8b127cbc
AM
12446 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12447 if (flinfo.internal_relocs == NULL)
c152c796
AM
12448 goto error_return;
12449 }
12450
12451 if (max_sym_count != 0)
12452 {
12453 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
12454 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12455 if (flinfo.external_syms == NULL)
c152c796
AM
12456 goto error_return;
12457
12458 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
12459 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12460 if (flinfo.internal_syms == NULL)
c152c796
AM
12461 goto error_return;
12462
12463 amt = max_sym_count * sizeof (long);
8b127cbc
AM
12464 flinfo.indices = (long int *) bfd_malloc (amt);
12465 if (flinfo.indices == NULL)
c152c796
AM
12466 goto error_return;
12467
12468 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
12469 flinfo.sections = (asection **) bfd_malloc (amt);
12470 if (flinfo.sections == NULL)
c152c796
AM
12471 goto error_return;
12472 }
12473
12474 if (max_sym_shndx_count != 0)
12475 {
12476 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
12477 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12478 if (flinfo.locsym_shndx == NULL)
c152c796
AM
12479 goto error_return;
12480 }
12481
64f52338 12482 if (htab->tls_sec)
c152c796 12483 {
66631823 12484 bfd_vma base, end = 0; /* Both bytes. */
c152c796
AM
12485 asection *sec;
12486
64f52338 12487 for (sec = htab->tls_sec;
c152c796
AM
12488 sec && (sec->flags & SEC_THREAD_LOCAL);
12489 sec = sec->next)
12490 {
3a800eb9 12491 bfd_size_type size = sec->size;
66631823 12492 unsigned int opb = bfd_octets_per_byte (abfd, sec);
c152c796 12493
3a800eb9
AM
12494 if (size == 0
12495 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 12496 {
91d6fa6a
NC
12497 struct bfd_link_order *ord = sec->map_tail.link_order;
12498
12499 if (ord != NULL)
66631823 12500 size = ord->offset * opb + ord->size;
c152c796 12501 }
66631823 12502 end = sec->vma + size / opb;
c152c796 12503 }
64f52338 12504 base = htab->tls_sec->vma;
7dc98aea
RO
12505 /* Only align end of TLS section if static TLS doesn't have special
12506 alignment requirements. */
12507 if (bed->static_tls_alignment == 1)
64f52338
AM
12508 end = align_power (end, htab->tls_sec->alignment_power);
12509 htab->tls_size = end - base;
c152c796
AM
12510 }
12511
2f0c68f2
CM
12512 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12513 return FALSE;
12514
c152c796
AM
12515 /* Since ELF permits relocations to be against local symbols, we
12516 must have the local symbols available when we do the relocations.
12517 Since we would rather only read the local symbols once, and we
12518 would rather not keep them in memory, we handle all the
12519 relocations for a single input file at the same time.
12520
12521 Unfortunately, there is no way to know the total number of local
12522 symbols until we have seen all of them, and the local symbol
12523 indices precede the global symbol indices. This means that when
12524 we are generating relocatable output, and we see a reloc against
12525 a global symbol, we can not know the symbol index until we have
12526 finished examining all the local symbols to see which ones we are
12527 going to output. To deal with this, we keep the relocations in
12528 memory, and don't output them until the end of the link. This is
12529 an unfortunate waste of memory, but I don't see a good way around
12530 it. Fortunately, it only happens when performing a relocatable
12531 link, which is not the common case. FIXME: If keep_memory is set
12532 we could write the relocs out and then read them again; I don't
12533 know how bad the memory loss will be. */
12534
c72f2fb2 12535 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12536 sub->output_has_begun = FALSE;
12537 for (o = abfd->sections; o != NULL; o = o->next)
12538 {
8423293d 12539 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
12540 {
12541 if (p->type == bfd_indirect_link_order
12542 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12543 == bfd_target_elf_flavour)
12544 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12545 {
12546 if (! sub->output_has_begun)
12547 {
8b127cbc 12548 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
12549 goto error_return;
12550 sub->output_has_begun = TRUE;
12551 }
12552 }
12553 else if (p->type == bfd_section_reloc_link_order
12554 || p->type == bfd_symbol_reloc_link_order)
12555 {
12556 if (! elf_reloc_link_order (abfd, info, o, p))
12557 goto error_return;
12558 }
12559 else
12560 {
12561 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
12562 {
12563 if (p->type == bfd_indirect_link_order
12564 && (bfd_get_flavour (sub)
12565 == bfd_target_elf_flavour)
12566 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12567 != bed->s->elfclass))
12568 {
12569 const char *iclass, *oclass;
12570
aebf9be7 12571 switch (bed->s->elfclass)
351f65ca 12572 {
aebf9be7
NC
12573 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12574 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12575 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12576 default: abort ();
351f65ca 12577 }
aebf9be7
NC
12578
12579 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 12580 {
aebf9be7
NC
12581 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12582 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12583 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12584 default: abort ();
351f65ca
L
12585 }
12586
12587 bfd_set_error (bfd_error_wrong_format);
4eca0228 12588 _bfd_error_handler
695344c0 12589 /* xgettext:c-format */
871b3ab2 12590 (_("%pB: file class %s incompatible with %s"),
351f65ca
L
12591 sub, iclass, oclass);
12592 }
12593
12594 goto error_return;
12595 }
c152c796
AM
12596 }
12597 }
12598 }
12599
c0f00686
L
12600 /* Free symbol buffer if needed. */
12601 if (!info->reduce_memory_overheads)
12602 {
c72f2fb2 12603 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c9594989 12604 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
c0f00686
L
12605 {
12606 free (elf_tdata (sub)->symbuf);
12607 elf_tdata (sub)->symbuf = NULL;
12608 }
12609 }
12610
496afd17
L
12611 ret = TRUE;
12612
c152c796
AM
12613 /* Output any global symbols that got converted to local in a
12614 version script or due to symbol visibility. We do this in a
12615 separate step since ELF requires all local symbols to appear
12616 prior to any global symbols. FIXME: We should only do this if
12617 some global symbols were, in fact, converted to become local.
12618 FIXME: Will this work correctly with the Irix 5 linker? */
12619 eoinfo.failed = FALSE;
8b127cbc 12620 eoinfo.flinfo = &flinfo;
c152c796 12621 eoinfo.localsyms = TRUE;
34a79995 12622 eoinfo.file_sym_done = FALSE;
7686d77d 12623 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796 12624 if (eoinfo.failed)
496afd17
L
12625 {
12626 ret = FALSE;
12627 goto return_local_hash_table;
12628 }
c152c796 12629
4e617b1e
PB
12630 /* If backend needs to output some local symbols not present in the hash
12631 table, do it now. */
8539e4e8
AM
12632 if (bed->elf_backend_output_arch_local_syms
12633 && (info->strip != strip_all || emit_relocs))
4e617b1e 12634 {
6e0b88f1 12635 typedef int (*out_sym_func)
4e617b1e
PB
12636 (void *, const char *, Elf_Internal_Sym *, asection *,
12637 struct elf_link_hash_entry *);
12638
12639 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
12640 (abfd, info, &flinfo,
12641 (out_sym_func) elf_link_output_symstrtab)))
496afd17
L
12642 {
12643 ret = FALSE;
12644 goto return_local_hash_table;
12645 }
4e617b1e
PB
12646 }
12647
c152c796
AM
12648 /* That wrote out all the local symbols. Finish up the symbol table
12649 with the global symbols. Even if we want to strip everything we
12650 can, we still need to deal with those global symbols that got
12651 converted to local in a version script. */
12652
12653 /* The sh_info field records the index of the first non local symbol. */
3f1b17bb
MR
12654 if (!symtab_hdr->sh_info)
12655 symtab_hdr->sh_info = bfd_get_symcount (abfd);
c152c796
AM
12656
12657 if (dynamic
64f52338
AM
12658 && htab->dynsym != NULL
12659 && htab->dynsym->output_section != bfd_abs_section_ptr)
c152c796
AM
12660 {
12661 Elf_Internal_Sym sym;
64f52338 12662 bfd_byte *dynsym = htab->dynsym->contents;
90ac2420 12663
64f52338
AM
12664 o = htab->dynsym->output_section;
12665 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
c152c796
AM
12666
12667 /* Write out the section symbols for the output sections. */
0e1862bb 12668 if (bfd_link_pic (info)
64f52338 12669 || htab->is_relocatable_executable)
c152c796
AM
12670 {
12671 asection *s;
12672
12673 sym.st_size = 0;
12674 sym.st_name = 0;
12675 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12676 sym.st_other = 0;
35fc36a8 12677 sym.st_target_internal = 0;
c152c796
AM
12678
12679 for (s = abfd->sections; s != NULL; s = s->next)
12680 {
12681 int indx;
12682 bfd_byte *dest;
12683 long dynindx;
12684
c152c796 12685 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
12686 if (dynindx <= 0)
12687 continue;
12688 indx = elf_section_data (s)->this_idx;
c152c796
AM
12689 BFD_ASSERT (indx > 0);
12690 sym.st_shndx = indx;
c0d5a53d 12691 if (! check_dynsym (abfd, &sym))
496afd17
L
12692 {
12693 ret = FALSE;
12694 goto return_local_hash_table;
12695 }
c152c796
AM
12696 sym.st_value = s->vma;
12697 dest = dynsym + dynindx * bed->s->sizeof_sym;
3d16b64e
NA
12698
12699 /* Inform the linker of the addition of this symbol. */
12700
12701 if (info->callbacks->ctf_new_dynsym)
12702 info->callbacks->ctf_new_dynsym (dynindx, &sym);
12703
c152c796
AM
12704 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12705 }
c152c796
AM
12706 }
12707
12708 /* Write out the local dynsyms. */
64f52338 12709 if (htab->dynlocal)
c152c796
AM
12710 {
12711 struct elf_link_local_dynamic_entry *e;
64f52338 12712 for (e = htab->dynlocal; e ; e = e->next)
c152c796
AM
12713 {
12714 asection *s;
12715 bfd_byte *dest;
12716
935bd1e0 12717 /* Copy the internal symbol and turn off visibility.
c152c796
AM
12718 Note that we saved a word of storage and overwrote
12719 the original st_name with the dynstr_index. */
12720 sym = e->isym;
935bd1e0 12721 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
4d68fd75 12722 sym.st_shndx = SHN_UNDEF;
c152c796 12723
cb33740c
AM
12724 s = bfd_section_from_elf_index (e->input_bfd,
12725 e->isym.st_shndx);
4d68fd75
AM
12726 if (s != NULL
12727 && s->output_section != NULL
12728 && elf_section_data (s->output_section) != NULL)
c152c796 12729 {
c152c796
AM
12730 sym.st_shndx =
12731 elf_section_data (s->output_section)->this_idx;
c0d5a53d 12732 if (! check_dynsym (abfd, &sym))
496afd17
L
12733 {
12734 ret = FALSE;
12735 goto return_local_hash_table;
12736 }
c152c796
AM
12737 sym.st_value = (s->output_section->vma
12738 + s->output_offset
12739 + e->isym.st_value);
12740 }
12741
3d16b64e
NA
12742 /* Inform the linker of the addition of this symbol. */
12743
12744 if (info->callbacks->ctf_new_dynsym)
12745 info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
12746
c152c796
AM
12747 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12748 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12749 }
12750 }
c152c796
AM
12751 }
12752
12753 /* We get the global symbols from the hash table. */
12754 eoinfo.failed = FALSE;
12755 eoinfo.localsyms = FALSE;
8b127cbc 12756 eoinfo.flinfo = &flinfo;
7686d77d 12757 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796 12758 if (eoinfo.failed)
496afd17
L
12759 {
12760 ret = FALSE;
12761 goto return_local_hash_table;
12762 }
c152c796
AM
12763
12764 /* If backend needs to output some symbols not present in the hash
12765 table, do it now. */
8539e4e8
AM
12766 if (bed->elf_backend_output_arch_syms
12767 && (info->strip != strip_all || emit_relocs))
c152c796 12768 {
6e0b88f1 12769 typedef int (*out_sym_func)
c152c796
AM
12770 (void *, const char *, Elf_Internal_Sym *, asection *,
12771 struct elf_link_hash_entry *);
12772
12773 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
12774 (abfd, info, &flinfo,
12775 (out_sym_func) elf_link_output_symstrtab)))
496afd17
L
12776 {
12777 ret = FALSE;
12778 goto return_local_hash_table;
12779 }
c152c796
AM
12780 }
12781
ef10c3ac
L
12782 /* Finalize the .strtab section. */
12783 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12784
12785 /* Swap out the .strtab section. */
12786 if (!elf_link_swap_symbols_out (&flinfo))
496afd17
L
12787 {
12788 ret = FALSE;
12789 goto return_local_hash_table;
12790 }
c152c796
AM
12791
12792 /* Now we know the size of the symtab section. */
c152c796
AM
12793 if (bfd_get_symcount (abfd) > 0)
12794 {
ee3b52e9
L
12795 /* Finish up and write out the symbol string table (.strtab)
12796 section. */
ad32986f 12797 Elf_Internal_Shdr *symstrtab_hdr = NULL;
8539e4e8
AM
12798 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12799
ad32986f 12800 if (elf_symtab_shndx_list (abfd))
8539e4e8 12801 {
ad32986f 12802 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8539e4e8 12803
ad32986f
NC
12804 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12805 {
12806 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12807 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12808 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12809 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12810 symtab_shndx_hdr->sh_size = amt;
8539e4e8 12811
ad32986f
NC
12812 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12813 off, TRUE);
12814
12815 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12816 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
496afd17
L
12817 {
12818 ret = FALSE;
12819 goto return_local_hash_table;
12820 }
ad32986f 12821 }
8539e4e8 12822 }
ee3b52e9
L
12823
12824 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12825 /* sh_name was set in prep_headers. */
12826 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 12827 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 12828 symstrtab_hdr->sh_addr = 0;
ef10c3ac 12829 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
12830 symstrtab_hdr->sh_entsize = 0;
12831 symstrtab_hdr->sh_link = 0;
12832 symstrtab_hdr->sh_info = 0;
12833 /* sh_offset is set just below. */
12834 symstrtab_hdr->sh_addralign = 1;
12835
12836 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12837 off, TRUE);
12838 elf_next_file_pos (abfd) = off;
12839
c152c796 12840 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 12841 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
496afd17
L
12842 {
12843 ret = FALSE;
12844 goto return_local_hash_table;
12845 }
c152c796
AM
12846 }
12847
76359541
TP
12848 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12849 {
871b3ab2 12850 _bfd_error_handler (_("%pB: failed to generate import library"),
4eca0228 12851 info->out_implib_bfd);
496afd17
L
12852 ret = FALSE;
12853 goto return_local_hash_table;
76359541
TP
12854 }
12855
c152c796
AM
12856 /* Adjust the relocs to have the correct symbol indices. */
12857 for (o = abfd->sections; o != NULL; o = o->next)
12858 {
d4730f92 12859 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 12860 bfd_boolean sort;
10bbbc1d 12861
c152c796
AM
12862 if ((o->flags & SEC_RELOC) == 0)
12863 continue;
12864
28dbcedc 12865 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3 12866 if (esdo->rel.hdr != NULL
10bbbc1d 12867 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
496afd17
L
12868 {
12869 ret = FALSE;
12870 goto return_local_hash_table;
12871 }
bca6d0e3 12872 if (esdo->rela.hdr != NULL
10bbbc1d 12873 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
496afd17
L
12874 {
12875 ret = FALSE;
12876 goto return_local_hash_table;
12877 }
c152c796
AM
12878
12879 /* Set the reloc_count field to 0 to prevent write_relocs from
12880 trying to swap the relocs out itself. */
12881 o->reloc_count = 0;
12882 }
12883
12884 if (dynamic && info->combreloc && dynobj != NULL)
12885 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12886
12887 /* If we are linking against a dynamic object, or generating a
12888 shared library, finish up the dynamic linking information. */
12889 if (dynamic)
12890 {
12891 bfd_byte *dyncon, *dynconend;
12892
12893 /* Fix up .dynamic entries. */
3d4d4302 12894 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
12895 BFD_ASSERT (o != NULL);
12896
12897 dyncon = o->contents;
eea6121a 12898 dynconend = o->contents + o->size;
c152c796
AM
12899 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12900 {
12901 Elf_Internal_Dyn dyn;
12902 const char *name;
12903 unsigned int type;
64487780
AM
12904 bfd_size_type sh_size;
12905 bfd_vma sh_addr;
c152c796
AM
12906
12907 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12908
12909 switch (dyn.d_tag)
12910 {
12911 default:
12912 continue;
12913 case DT_NULL:
12914 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12915 {
12916 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12917 {
12918 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12919 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12920 default: continue;
12921 }
12922 dyn.d_un.d_val = relativecount;
12923 relativecount = 0;
12924 break;
12925 }
12926 continue;
12927
12928 case DT_INIT:
12929 name = info->init_function;
12930 goto get_sym;
12931 case DT_FINI:
12932 name = info->fini_function;
12933 get_sym:
12934 {
12935 struct elf_link_hash_entry *h;
12936
64f52338 12937 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
c152c796
AM
12938 if (h != NULL
12939 && (h->root.type == bfd_link_hash_defined
12940 || h->root.type == bfd_link_hash_defweak))
12941 {
bef26483 12942 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
12943 o = h->root.u.def.section;
12944 if (o->output_section != NULL)
bef26483 12945 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
12946 + o->output_offset);
12947 else
12948 {
12949 /* The symbol is imported from another shared
12950 library and does not apply to this one. */
bef26483 12951 dyn.d_un.d_ptr = 0;
c152c796
AM
12952 }
12953 break;
12954 }
12955 }
12956 continue;
12957
12958 case DT_PREINIT_ARRAYSZ:
12959 name = ".preinit_array";
4ade44b7 12960 goto get_out_size;
c152c796
AM
12961 case DT_INIT_ARRAYSZ:
12962 name = ".init_array";
4ade44b7 12963 goto get_out_size;
c152c796
AM
12964 case DT_FINI_ARRAYSZ:
12965 name = ".fini_array";
4ade44b7 12966 get_out_size:
c152c796
AM
12967 o = bfd_get_section_by_name (abfd, name);
12968 if (o == NULL)
12969 {
4eca0228 12970 _bfd_error_handler
4ade44b7 12971 (_("could not find section %s"), name);
c152c796
AM
12972 goto error_return;
12973 }
eea6121a 12974 if (o->size == 0)
4eca0228 12975 _bfd_error_handler
c152c796 12976 (_("warning: %s section has zero size"), name);
eea6121a 12977 dyn.d_un.d_val = o->size;
c152c796
AM
12978 break;
12979
12980 case DT_PREINIT_ARRAY:
12981 name = ".preinit_array";
4ade44b7 12982 goto get_out_vma;
c152c796
AM
12983 case DT_INIT_ARRAY:
12984 name = ".init_array";
4ade44b7 12985 goto get_out_vma;
c152c796
AM
12986 case DT_FINI_ARRAY:
12987 name = ".fini_array";
4ade44b7
AM
12988 get_out_vma:
12989 o = bfd_get_section_by_name (abfd, name);
12990 goto do_vma;
c152c796
AM
12991
12992 case DT_HASH:
12993 name = ".hash";
12994 goto get_vma;
fdc90cb4
JJ
12995 case DT_GNU_HASH:
12996 name = ".gnu.hash";
12997 goto get_vma;
c152c796
AM
12998 case DT_STRTAB:
12999 name = ".dynstr";
13000 goto get_vma;
13001 case DT_SYMTAB:
13002 name = ".dynsym";
13003 goto get_vma;
13004 case DT_VERDEF:
13005 name = ".gnu.version_d";
13006 goto get_vma;
13007 case DT_VERNEED:
13008 name = ".gnu.version_r";
13009 goto get_vma;
13010 case DT_VERSYM:
13011 name = ".gnu.version";
13012 get_vma:
4ade44b7
AM
13013 o = bfd_get_linker_section (dynobj, name);
13014 do_vma:
b3293efa 13015 if (o == NULL || bfd_is_abs_section (o->output_section))
c152c796 13016 {
4eca0228 13017 _bfd_error_handler
4ade44b7 13018 (_("could not find section %s"), name);
c152c796
AM
13019 goto error_return;
13020 }
894891db
NC
13021 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13022 {
4eca0228 13023 _bfd_error_handler
894891db
NC
13024 (_("warning: section '%s' is being made into a note"), name);
13025 bfd_set_error (bfd_error_nonrepresentable_section);
13026 goto error_return;
13027 }
4ade44b7 13028 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
c152c796
AM
13029 break;
13030
13031 case DT_REL:
13032 case DT_RELA:
13033 case DT_RELSZ:
13034 case DT_RELASZ:
13035 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13036 type = SHT_REL;
13037 else
13038 type = SHT_RELA;
64487780
AM
13039 sh_size = 0;
13040 sh_addr = 0;
c152c796
AM
13041 for (i = 1; i < elf_numsections (abfd); i++)
13042 {
13043 Elf_Internal_Shdr *hdr;
13044
13045 hdr = elf_elfsections (abfd)[i];
13046 if (hdr->sh_type == type
13047 && (hdr->sh_flags & SHF_ALLOC) != 0)
13048 {
64487780
AM
13049 sh_size += hdr->sh_size;
13050 if (sh_addr == 0
13051 || sh_addr > hdr->sh_addr)
13052 sh_addr = hdr->sh_addr;
c152c796
AM
13053 }
13054 }
64487780 13055
64f52338
AM
13056 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
13057 {
66631823
CE
13058 unsigned int opb = bfd_octets_per_byte (abfd, o);
13059
64f52338
AM
13060 /* Don't count procedure linkage table relocs in the
13061 overall reloc count. */
64487780
AM
13062 sh_size -= htab->srelplt->size;
13063 if (sh_size == 0)
13064 /* If the size is zero, make the address zero too.
13065 This is to avoid a glibc bug. If the backend
13066 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13067 zero, then we'll put DT_RELA at the end of
13068 DT_JMPREL. glibc will interpret the end of
13069 DT_RELA matching the end of DT_JMPREL as the
13070 case where DT_RELA includes DT_JMPREL, and for
13071 LD_BIND_NOW will decide that processing DT_RELA
13072 will process the PLT relocs too. Net result:
13073 No PLT relocs applied. */
13074 sh_addr = 0;
13075
64f52338
AM
13076 /* If .rela.plt is the first .rela section, exclude
13077 it from DT_RELA. */
64487780 13078 else if (sh_addr == (htab->srelplt->output_section->vma
66631823 13079 + htab->srelplt->output_offset) * opb)
64487780 13080 sh_addr += htab->srelplt->size;
64f52338 13081 }
64487780
AM
13082
13083 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13084 dyn.d_un.d_val = sh_size;
13085 else
13086 dyn.d_un.d_ptr = sh_addr;
c152c796
AM
13087 break;
13088 }
13089 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13090 }
13091 }
13092
13093 /* If we have created any dynamic sections, then output them. */
13094 if (dynobj != NULL)
13095 {
13096 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
13097 goto error_return;
13098
943284cc 13099 /* Check for DT_TEXTREL (late, in case the backend removes it). */
a6dbf402 13100 if (bfd_link_textrel_check (info)
3d4d4302 13101 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
13102 {
13103 bfd_byte *dyncon, *dynconend;
13104
943284cc
DJ
13105 dyncon = o->contents;
13106 dynconend = o->contents + o->size;
13107 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13108 {
13109 Elf_Internal_Dyn dyn;
13110
13111 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13112
13113 if (dyn.d_tag == DT_TEXTREL)
13114 {
a6dbf402 13115 if (info->textrel_check == textrel_check_error)
c192a133 13116 info->callbacks->einfo
9793eb77 13117 (_("%P%X: read-only segment has dynamic relocations\n"));
a6dbf402
L
13118 else if (bfd_link_dll (info))
13119 info->callbacks->einfo
13120 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
c192a133
AM
13121 else
13122 info->callbacks->einfo
a6dbf402 13123 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
943284cc
DJ
13124 break;
13125 }
13126 }
13127 }
13128
c152c796
AM
13129 for (o = dynobj->sections; o != NULL; o = o->next)
13130 {
13131 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 13132 || o->size == 0
c152c796
AM
13133 || o->output_section == bfd_abs_section_ptr)
13134 continue;
13135 if ((o->flags & SEC_LINKER_CREATED) == 0)
13136 {
13137 /* At this point, we are only interested in sections
13138 created by _bfd_elf_link_create_dynamic_sections. */
13139 continue;
13140 }
64f52338 13141 if (htab->stab_info.stabstr == o)
3722b82f 13142 continue;
64f52338 13143 if (htab->eh_info.hdr_sec == o)
eea6121a 13144 continue;
3d4d4302 13145 if (strcmp (o->name, ".dynstr") != 0)
c152c796 13146 {
bb294208
AM
13147 bfd_size_type octets = ((file_ptr) o->output_offset
13148 * bfd_octets_per_byte (abfd, o));
13149 if (!bfd_set_section_contents (abfd, o->output_section,
13150 o->contents, octets, o->size))
c152c796
AM
13151 goto error_return;
13152 }
13153 else
13154 {
13155 /* The contents of the .dynstr section are actually in a
13156 stringtab. */
8539e4e8
AM
13157 file_ptr off;
13158
c152c796
AM
13159 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13160 if (bfd_seek (abfd, off, SEEK_SET) != 0
64f52338 13161 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
c152c796
AM
13162 goto error_return;
13163 }
13164 }
13165 }
13166
7bdf4127 13167 if (!info->resolve_section_groups)
c152c796
AM
13168 {
13169 bfd_boolean failed = FALSE;
13170
7bdf4127 13171 BFD_ASSERT (bfd_link_relocatable (info));
c152c796
AM
13172 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13173 if (failed)
13174 goto error_return;
13175 }
13176
13177 /* If we have optimized stabs strings, output them. */
64f52338 13178 if (htab->stab_info.stabstr != NULL)
c152c796 13179 {
64f52338 13180 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
c152c796
AM
13181 goto error_return;
13182 }
13183
9f7c3e5e
AM
13184 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13185 goto error_return;
c152c796 13186
1ff6de03
NA
13187 if (info->callbacks->emit_ctf)
13188 info->callbacks->emit_ctf ();
13189
9f7c3e5e 13190 elf_final_link_free (abfd, &flinfo);
c152c796 13191
104d59d1
JM
13192 if (attr_section)
13193 {
a50b1753 13194 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 13195 if (contents == NULL)
496afd17
L
13196 {
13197 /* Bail out and fail. */
13198 ret = FALSE;
13199 goto return_local_hash_table;
13200 }
104d59d1
JM
13201 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
13202 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
13203 free (contents);
13204 }
13205
496afd17
L
13206 return_local_hash_table:
13207 if (info->unique_symbol)
13208 bfd_hash_table_free (&flinfo.local_hash_table);
13209 return ret;
c152c796
AM
13210
13211 error_return:
9f7c3e5e 13212 elf_final_link_free (abfd, &flinfo);
496afd17
L
13213 ret = FALSE;
13214 goto return_local_hash_table;
c152c796
AM
13215}
13216\f
5241d853
RS
13217/* Initialize COOKIE for input bfd ABFD. */
13218
13219static bfd_boolean
13220init_reloc_cookie (struct elf_reloc_cookie *cookie,
13221 struct bfd_link_info *info, bfd *abfd)
13222{
13223 Elf_Internal_Shdr *symtab_hdr;
13224 const struct elf_backend_data *bed;
13225
13226 bed = get_elf_backend_data (abfd);
13227 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13228
13229 cookie->abfd = abfd;
13230 cookie->sym_hashes = elf_sym_hashes (abfd);
13231 cookie->bad_symtab = elf_bad_symtab (abfd);
13232 if (cookie->bad_symtab)
13233 {
13234 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13235 cookie->extsymoff = 0;
13236 }
13237 else
13238 {
13239 cookie->locsymcount = symtab_hdr->sh_info;
13240 cookie->extsymoff = symtab_hdr->sh_info;
13241 }
13242
13243 if (bed->s->arch_size == 32)
13244 cookie->r_sym_shift = 8;
13245 else
13246 cookie->r_sym_shift = 32;
13247
13248 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13249 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13250 {
13251 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13252 cookie->locsymcount, 0,
13253 NULL, NULL, NULL);
13254 if (cookie->locsyms == NULL)
13255 {
13256 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
13257 return FALSE;
13258 }
13259 if (info->keep_memory)
13260 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13261 }
13262 return TRUE;
13263}
13264
13265/* Free the memory allocated by init_reloc_cookie, if appropriate. */
13266
13267static void
13268fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13269{
13270 Elf_Internal_Shdr *symtab_hdr;
13271
13272 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
c9594989 13273 if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
5241d853
RS
13274 free (cookie->locsyms);
13275}
13276
13277/* Initialize the relocation information in COOKIE for input section SEC
13278 of input bfd ABFD. */
13279
13280static bfd_boolean
13281init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13282 struct bfd_link_info *info, bfd *abfd,
13283 asection *sec)
13284{
5241d853
RS
13285 if (sec->reloc_count == 0)
13286 {
13287 cookie->rels = NULL;
13288 cookie->relend = NULL;
13289 }
13290 else
13291 {
5241d853
RS
13292 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
13293 info->keep_memory);
13294 if (cookie->rels == NULL)
13295 return FALSE;
13296 cookie->rel = cookie->rels;
056bafd4 13297 cookie->relend = cookie->rels + sec->reloc_count;
5241d853
RS
13298 }
13299 cookie->rel = cookie->rels;
13300 return TRUE;
13301}
13302
13303/* Free the memory allocated by init_reloc_cookie_rels,
13304 if appropriate. */
13305
13306static void
13307fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13308 asection *sec)
13309{
c9594989 13310 if (elf_section_data (sec)->relocs != cookie->rels)
5241d853
RS
13311 free (cookie->rels);
13312}
13313
13314/* Initialize the whole of COOKIE for input section SEC. */
13315
13316static bfd_boolean
13317init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13318 struct bfd_link_info *info,
13319 asection *sec)
13320{
13321 if (!init_reloc_cookie (cookie, info, sec->owner))
13322 goto error1;
13323 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13324 goto error2;
13325 return TRUE;
13326
13327 error2:
13328 fini_reloc_cookie (cookie, sec->owner);
13329 error1:
13330 return FALSE;
13331}
13332
13333/* Free the memory allocated by init_reloc_cookie_for_section,
13334 if appropriate. */
13335
13336static void
13337fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13338 asection *sec)
13339{
13340 fini_reloc_cookie_rels (cookie, sec);
13341 fini_reloc_cookie (cookie, sec->owner);
13342}
13343\f
c152c796
AM
13344/* Garbage collect unused sections. */
13345
07adf181
AM
13346/* Default gc_mark_hook. */
13347
13348asection *
13349_bfd_elf_gc_mark_hook (asection *sec,
13350 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13351 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13352 struct elf_link_hash_entry *h,
13353 Elf_Internal_Sym *sym)
13354{
13355 if (h != NULL)
13356 {
13357 switch (h->root.type)
13358 {
13359 case bfd_link_hash_defined:
13360 case bfd_link_hash_defweak:
13361 return h->root.u.def.section;
13362
13363 case bfd_link_hash_common:
13364 return h->root.u.c.p->section;
13365
13366 default:
13367 break;
13368 }
13369 }
13370 else
13371 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13372
13373 return NULL;
13374}
13375
9e223787 13376/* Return the debug definition section. */
b7c871ed
L
13377
13378static asection *
13379elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13380 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13381 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13382 struct elf_link_hash_entry *h,
9e223787 13383 Elf_Internal_Sym *sym)
b7c871ed 13384{
9e223787
L
13385 if (h != NULL)
13386 {
13387 /* Return the global debug definition section. */
13388 if ((h->root.type == bfd_link_hash_defined
13389 || h->root.type == bfd_link_hash_defweak)
13390 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13391 return h->root.u.def.section;
13392 }
13393 else
13394 {
13395 /* Return the local debug definition section. */
13396 asection *isec = bfd_section_from_elf_index (sec->owner,
13397 sym->st_shndx);
13398 if ((isec->flags & SEC_DEBUGGING) != 0)
13399 return isec;
13400 }
b7c871ed
L
13401
13402 return NULL;
13403}
13404
5241d853
RS
13405/* COOKIE->rel describes a relocation against section SEC, which is
13406 a section we've decided to keep. Return the section that contains
13407 the relocation symbol, or NULL if no section contains it. */
13408
13409asection *
13410_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13411 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
13412 struct elf_reloc_cookie *cookie,
13413 bfd_boolean *start_stop)
5241d853
RS
13414{
13415 unsigned long r_symndx;
3024a17a 13416 struct elf_link_hash_entry *h, *hw;
5241d853
RS
13417
13418 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 13419 if (r_symndx == STN_UNDEF)
5241d853
RS
13420 return NULL;
13421
13422 if (r_symndx >= cookie->locsymcount
13423 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13424 {
13425 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
13426 if (h == NULL)
13427 {
871b3ab2 13428 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
263ddf68
L
13429 sec->owner);
13430 return NULL;
13431 }
5241d853
RS
13432 while (h->root.type == bfd_link_hash_indirect
13433 || h->root.type == bfd_link_hash_warning)
13434 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 13435 h->mark = 1;
3024a17a
AM
13436 /* Keep all aliases of the symbol too. If an object symbol
13437 needs to be copied into .dynbss then all of its aliases
13438 should be present as dynamic symbols, not just the one used
13439 on the copy relocation. */
13440 hw = h;
13441 while (hw->is_weakalias)
13442 {
13443 hw = hw->u.alias;
13444 hw->mark = 1;
13445 }
1cce69b9 13446
a6a4679f 13447 if (start_stop != NULL)
1cce69b9 13448 {
7dba9362
AM
13449 /* To work around a glibc bug, mark XXX input sections
13450 when there is a reference to __start_XXX or __stop_XXX
13451 symbols. */
cbd0eecf 13452 if (h->start_stop)
1cce69b9 13453 {
cbd0eecf 13454 asection *s = h->u2.start_stop_section;
a6a4679f
AM
13455 *start_stop = !s->gc_mark;
13456 return s;
1cce69b9
AM
13457 }
13458 }
13459
5241d853
RS
13460 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13461 }
13462
13463 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13464 &cookie->locsyms[r_symndx]);
13465}
13466
13467/* COOKIE->rel describes a relocation against section SEC, which is
13468 a section we've decided to keep. Mark the section that contains
9d0a14d3 13469 the relocation symbol. */
5241d853
RS
13470
13471bfd_boolean
13472_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13473 asection *sec,
13474 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 13475 struct elf_reloc_cookie *cookie)
5241d853
RS
13476{
13477 asection *rsec;
1cce69b9 13478 bfd_boolean start_stop = FALSE;
5241d853 13479
1cce69b9
AM
13480 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13481 while (rsec != NULL)
5241d853 13482 {
1cce69b9
AM
13483 if (!rsec->gc_mark)
13484 {
13485 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13486 || (rsec->owner->flags & DYNAMIC) != 0)
13487 rsec->gc_mark = 1;
13488 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13489 return FALSE;
13490 }
13491 if (!start_stop)
13492 break;
199af150 13493 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
13494 }
13495 return TRUE;
13496}
13497
07adf181
AM
13498/* The mark phase of garbage collection. For a given section, mark
13499 it and any sections in this section's group, and all the sections
13500 which define symbols to which it refers. */
13501
ccfa59ea
AM
13502bfd_boolean
13503_bfd_elf_gc_mark (struct bfd_link_info *info,
13504 asection *sec,
6a5bb875 13505 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
13506{
13507 bfd_boolean ret;
9d0a14d3 13508 asection *group_sec, *eh_frame;
c152c796
AM
13509
13510 sec->gc_mark = 1;
13511
13512 /* Mark all the sections in the group. */
13513 group_sec = elf_section_data (sec)->next_in_group;
13514 if (group_sec && !group_sec->gc_mark)
ccfa59ea 13515 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
13516 return FALSE;
13517
13518 /* Look through the section relocs. */
13519 ret = TRUE;
9d0a14d3
RS
13520 eh_frame = elf_eh_frame_section (sec->owner);
13521 if ((sec->flags & SEC_RELOC) != 0
13522 && sec->reloc_count > 0
13523 && sec != eh_frame)
c152c796 13524 {
5241d853 13525 struct elf_reloc_cookie cookie;
c152c796 13526
5241d853
RS
13527 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13528 ret = FALSE;
c152c796 13529 else
c152c796 13530 {
5241d853 13531 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 13532 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
13533 {
13534 ret = FALSE;
13535 break;
13536 }
13537 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
13538 }
13539 }
9d0a14d3
RS
13540
13541 if (ret && eh_frame && elf_fde_list (sec))
13542 {
13543 struct elf_reloc_cookie cookie;
13544
13545 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13546 ret = FALSE;
13547 else
13548 {
13549 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13550 gc_mark_hook, &cookie))
13551 ret = FALSE;
13552 fini_reloc_cookie_for_section (&cookie, eh_frame);
13553 }
13554 }
13555
2f0c68f2
CM
13556 eh_frame = elf_section_eh_frame_entry (sec);
13557 if (ret && eh_frame && !eh_frame->gc_mark)
13558 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13559 ret = FALSE;
13560
c152c796
AM
13561 return ret;
13562}
13563
3c758495
TG
13564/* Scan and mark sections in a special or debug section group. */
13565
13566static void
13567_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13568{
13569 /* Point to first section of section group. */
13570 asection *ssec;
13571 /* Used to iterate the section group. */
13572 asection *msec;
13573
13574 bfd_boolean is_special_grp = TRUE;
13575 bfd_boolean is_debug_grp = TRUE;
13576
13577 /* First scan to see if group contains any section other than debug
13578 and special section. */
13579 ssec = msec = elf_next_in_group (grp);
13580 do
13581 {
13582 if ((msec->flags & SEC_DEBUGGING) == 0)
13583 is_debug_grp = FALSE;
13584
13585 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13586 is_special_grp = FALSE;
13587
13588 msec = elf_next_in_group (msec);
13589 }
13590 while (msec != ssec);
13591
13592 /* If this is a pure debug section group or pure special section group,
13593 keep all sections in this group. */
13594 if (is_debug_grp || is_special_grp)
13595 {
13596 do
13597 {
13598 msec->gc_mark = 1;
13599 msec = elf_next_in_group (msec);
13600 }
13601 while (msec != ssec);
13602 }
13603}
13604
7f6ab9f8
AM
13605/* Keep debug and special sections. */
13606
13607bfd_boolean
13608_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
b7d07216 13609 elf_gc_mark_hook_fn mark_hook)
7f6ab9f8
AM
13610{
13611 bfd *ibfd;
13612
c72f2fb2 13613 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
13614 {
13615 asection *isec;
13616 bfd_boolean some_kept;
b40bf0a2 13617 bfd_boolean debug_frag_seen;
b7c871ed 13618 bfd_boolean has_kept_debug_info;
7f6ab9f8
AM
13619
13620 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13621 continue;
57963c05
AM
13622 isec = ibfd->sections;
13623 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13624 continue;
7f6ab9f8 13625
b40bf0a2
NC
13626 /* Ensure all linker created sections are kept,
13627 see if any other section is already marked,
13628 and note if we have any fragmented debug sections. */
b7c871ed 13629 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
7f6ab9f8
AM
13630 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13631 {
13632 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13633 isec->gc_mark = 1;
eb026f09
AM
13634 else if (isec->gc_mark
13635 && (isec->flags & SEC_ALLOC) != 0
13636 && elf_section_type (isec) != SHT_NOTE)
7f6ab9f8 13637 some_kept = TRUE;
b7d07216
L
13638 else
13639 {
13640 /* Since all sections, except for backend specific ones,
13641 have been garbage collected, call mark_hook on this
13642 section if any of its linked-to sections is marked. */
def97fb9
AM
13643 asection *linked_to_sec;
13644 for (linked_to_sec = elf_linked_to_section (isec);
13645 linked_to_sec != NULL && !linked_to_sec->linker_mark;
b7d07216 13646 linked_to_sec = elf_linked_to_section (linked_to_sec))
def97fb9
AM
13647 {
13648 if (linked_to_sec->gc_mark)
13649 {
13650 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
13651 return FALSE;
13652 break;
13653 }
13654 linked_to_sec->linker_mark = 1;
13655 }
13656 for (linked_to_sec = elf_linked_to_section (isec);
13657 linked_to_sec != NULL && linked_to_sec->linker_mark;
13658 linked_to_sec = elf_linked_to_section (linked_to_sec))
13659 linked_to_sec->linker_mark = 0;
b7d07216 13660 }
b40bf0a2 13661
535b785f 13662 if (!debug_frag_seen
b40bf0a2
NC
13663 && (isec->flags & SEC_DEBUGGING)
13664 && CONST_STRNEQ (isec->name, ".debug_line."))
13665 debug_frag_seen = TRUE;
5242a0a0
L
13666 else if (strcmp (bfd_section_name (isec),
13667 "__patchable_function_entries") == 0
13668 && elf_linked_to_section (isec) == NULL)
13669 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
13670 "need linked-to section "
13671 "for --gc-sections\n"),
13672 isec->owner, isec);
7f6ab9f8
AM
13673 }
13674
eb026f09
AM
13675 /* If no non-note alloc section in this file will be kept, then
13676 we can toss out the debug and special sections. */
7f6ab9f8
AM
13677 if (!some_kept)
13678 continue;
13679
13680 /* Keep debug and special sections like .comment when they are
3c758495 13681 not part of a group. Also keep section groups that contain
b7d07216
L
13682 just debug sections or special sections. NB: Sections with
13683 linked-to section has been handled above. */
7f6ab9f8 13684 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
13685 {
13686 if ((isec->flags & SEC_GROUP) != 0)
13687 _bfd_elf_gc_mark_debug_special_section_group (isec);
13688 else if (((isec->flags & SEC_DEBUGGING) != 0
13689 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
b7d07216
L
13690 && elf_next_in_group (isec) == NULL
13691 && elf_linked_to_section (isec) == NULL)
3c758495 13692 isec->gc_mark = 1;
b7c871ed
L
13693 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13694 has_kept_debug_info = TRUE;
3c758495 13695 }
b40bf0a2 13696
b40bf0a2
NC
13697 /* Look for CODE sections which are going to be discarded,
13698 and find and discard any fragmented debug sections which
13699 are associated with that code section. */
b7c871ed
L
13700 if (debug_frag_seen)
13701 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13702 if ((isec->flags & SEC_CODE) != 0
13703 && isec->gc_mark == 0)
13704 {
13705 unsigned int ilen;
13706 asection *dsec;
b40bf0a2 13707
b7c871ed 13708 ilen = strlen (isec->name);
b40bf0a2 13709
b7c871ed 13710 /* Association is determined by the name of the debug
07d6d2b8 13711 section containing the name of the code section as
b7c871ed
L
13712 a suffix. For example .debug_line.text.foo is a
13713 debug section associated with .text.foo. */
13714 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13715 {
13716 unsigned int dlen;
b40bf0a2 13717
b7c871ed
L
13718 if (dsec->gc_mark == 0
13719 || (dsec->flags & SEC_DEBUGGING) == 0)
13720 continue;
b40bf0a2 13721
b7c871ed 13722 dlen = strlen (dsec->name);
b40bf0a2 13723
b7c871ed
L
13724 if (dlen > ilen
13725 && strncmp (dsec->name + (dlen - ilen),
13726 isec->name, ilen) == 0)
b40bf0a2 13727 dsec->gc_mark = 0;
b7c871ed 13728 }
b40bf0a2 13729 }
b7c871ed
L
13730
13731 /* Mark debug sections referenced by kept debug sections. */
13732 if (has_kept_debug_info)
13733 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13734 if (isec->gc_mark
13735 && (isec->flags & SEC_DEBUGGING) != 0)
13736 if (!_bfd_elf_gc_mark (info, isec,
13737 elf_gc_mark_debug_section))
13738 return FALSE;
7f6ab9f8
AM
13739 }
13740 return TRUE;
13741}
13742
c152c796 13743static bfd_boolean
ccabcbe5 13744elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
13745{
13746 bfd *sub;
ccabcbe5 13747 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
c152c796 13748
c72f2fb2 13749 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13750 {
13751 asection *o;
13752
b19a8f85 13753 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 13754 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
b19a8f85 13755 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796 13756 continue;
57963c05
AM
13757 o = sub->sections;
13758 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13759 continue;
c152c796
AM
13760
13761 for (o = sub->sections; o != NULL; o = o->next)
13762 {
a33dafc3
L
13763 /* When any section in a section group is kept, we keep all
13764 sections in the section group. If the first member of
13765 the section group is excluded, we will also exclude the
13766 group section. */
13767 if (o->flags & SEC_GROUP)
13768 {
13769 asection *first = elf_next_in_group (o);
13770 o->gc_mark = first->gc_mark;
13771 }
c152c796 13772
1e7eae0d 13773 if (o->gc_mark)
c152c796
AM
13774 continue;
13775
13776 /* Skip sweeping sections already excluded. */
13777 if (o->flags & SEC_EXCLUDE)
13778 continue;
13779
13780 /* Since this is early in the link process, it is simple
13781 to remove a section from the output. */
13782 o->flags |= SEC_EXCLUDE;
13783
c55fe096 13784 if (info->print_gc_sections && o->size != 0)
695344c0 13785 /* xgettext:c-format */
9793eb77 13786 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
c08bb8dd 13787 o, sub);
c152c796
AM
13788 }
13789 }
13790
c152c796
AM
13791 return TRUE;
13792}
13793
13794/* Propagate collected vtable information. This is called through
13795 elf_link_hash_traverse. */
13796
13797static bfd_boolean
13798elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13799{
c152c796 13800 /* Those that are not vtables. */
cbd0eecf
L
13801 if (h->start_stop
13802 || h->u2.vtable == NULL
13803 || h->u2.vtable->parent == NULL)
c152c796
AM
13804 return TRUE;
13805
13806 /* Those vtables that do not have parents, we cannot merge. */
cbd0eecf 13807 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
13808 return TRUE;
13809
13810 /* If we've already been done, exit. */
cbd0eecf 13811 if (h->u2.vtable->used && h->u2.vtable->used[-1])
c152c796
AM
13812 return TRUE;
13813
13814 /* Make sure the parent's table is up to date. */
cbd0eecf 13815 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
c152c796 13816
cbd0eecf 13817 if (h->u2.vtable->used == NULL)
c152c796
AM
13818 {
13819 /* None of this table's entries were referenced. Re-use the
13820 parent's table. */
cbd0eecf
L
13821 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13822 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
c152c796
AM
13823 }
13824 else
13825 {
13826 size_t n;
13827 bfd_boolean *cu, *pu;
13828
13829 /* Or the parent's entries into ours. */
cbd0eecf 13830 cu = h->u2.vtable->used;
c152c796 13831 cu[-1] = TRUE;
cbd0eecf 13832 pu = h->u2.vtable->parent->u2.vtable->used;
c152c796
AM
13833 if (pu != NULL)
13834 {
13835 const struct elf_backend_data *bed;
13836 unsigned int log_file_align;
13837
13838 bed = get_elf_backend_data (h->root.u.def.section->owner);
13839 log_file_align = bed->s->log_file_align;
cbd0eecf 13840 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
c152c796
AM
13841 while (n--)
13842 {
13843 if (*pu)
13844 *cu = TRUE;
13845 pu++;
13846 cu++;
13847 }
13848 }
13849 }
13850
13851 return TRUE;
13852}
13853
13854static bfd_boolean
13855elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13856{
13857 asection *sec;
13858 bfd_vma hstart, hend;
13859 Elf_Internal_Rela *relstart, *relend, *rel;
13860 const struct elf_backend_data *bed;
13861 unsigned int log_file_align;
13862
c152c796
AM
13863 /* Take care of both those symbols that do not describe vtables as
13864 well as those that are not loaded. */
cbd0eecf
L
13865 if (h->start_stop
13866 || h->u2.vtable == NULL
13867 || h->u2.vtable->parent == NULL)
c152c796
AM
13868 return TRUE;
13869
13870 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13871 || h->root.type == bfd_link_hash_defweak);
13872
13873 sec = h->root.u.def.section;
13874 hstart = h->root.u.def.value;
13875 hend = hstart + h->size;
13876
13877 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13878 if (!relstart)
13879 return *(bfd_boolean *) okp = FALSE;
13880 bed = get_elf_backend_data (sec->owner);
13881 log_file_align = bed->s->log_file_align;
13882
056bafd4 13883 relend = relstart + sec->reloc_count;
c152c796
AM
13884
13885 for (rel = relstart; rel < relend; ++rel)
13886 if (rel->r_offset >= hstart && rel->r_offset < hend)
13887 {
13888 /* If the entry is in use, do nothing. */
cbd0eecf
L
13889 if (h->u2.vtable->used
13890 && (rel->r_offset - hstart) < h->u2.vtable->size)
c152c796
AM
13891 {
13892 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
cbd0eecf 13893 if (h->u2.vtable->used[entry])
c152c796
AM
13894 continue;
13895 }
13896 /* Otherwise, kill it. */
13897 rel->r_offset = rel->r_info = rel->r_addend = 0;
13898 }
13899
13900 return TRUE;
13901}
13902
87538722
AM
13903/* Mark sections containing dynamically referenced symbols. When
13904 building shared libraries, we must assume that any visible symbol is
13905 referenced. */
715df9b8 13906
64d03ab5
AM
13907bfd_boolean
13908bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 13909{
87538722 13910 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 13911 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 13912
715df9b8
EB
13913 if ((h->root.type == bfd_link_hash_defined
13914 || h->root.type == bfd_link_hash_defweak)
d664fd41 13915 && ((h->ref_dynamic && !h->forced_local)
c4621b33 13916 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 13917 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 13918 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 13919 && (!bfd_link_executable (info)
22185505 13920 || info->gc_keep_exported
b407645f
AM
13921 || info->export_dynamic
13922 || (h->dynamic
13923 && d != NULL
13924 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 13925 && (h->versioned >= versioned
54e8959c
L
13926 || !bfd_hide_sym_by_version (info->version_info,
13927 h->root.root.string)))))
715df9b8
EB
13928 h->root.u.def.section->flags |= SEC_KEEP;
13929
13930 return TRUE;
13931}
3b36f7e6 13932
74f0fb50
AM
13933/* Keep all sections containing symbols undefined on the command-line,
13934 and the section containing the entry symbol. */
13935
13936void
13937_bfd_elf_gc_keep (struct bfd_link_info *info)
13938{
13939 struct bfd_sym_chain *sym;
13940
13941 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13942 {
13943 struct elf_link_hash_entry *h;
13944
13945 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13946 FALSE, FALSE, FALSE);
13947
13948 if (h != NULL
13949 && (h->root.type == bfd_link_hash_defined
13950 || h->root.type == bfd_link_hash_defweak)
2f5541f3 13951 && !bfd_is_const_section (h->root.u.def.section))
74f0fb50
AM
13952 h->root.u.def.section->flags |= SEC_KEEP;
13953 }
13954}
13955
2f0c68f2
CM
13956bfd_boolean
13957bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13958 struct bfd_link_info *info)
13959{
13960 bfd *ibfd = info->input_bfds;
13961
13962 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13963 {
13964 asection *sec;
13965 struct elf_reloc_cookie cookie;
13966
13967 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13968 continue;
57963c05
AM
13969 sec = ibfd->sections;
13970 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13971 continue;
2f0c68f2
CM
13972
13973 if (!init_reloc_cookie (&cookie, info, ibfd))
13974 return FALSE;
13975
13976 for (sec = ibfd->sections; sec; sec = sec->next)
13977 {
fd361982 13978 if (CONST_STRNEQ (bfd_section_name (sec), ".eh_frame_entry")
2f0c68f2
CM
13979 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13980 {
13981 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13982 fini_reloc_cookie_rels (&cookie, sec);
13983 }
13984 }
13985 }
13986 return TRUE;
13987}
13988
c152c796
AM
13989/* Do mark and sweep of unused sections. */
13990
13991bfd_boolean
13992bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13993{
13994 bfd_boolean ok = TRUE;
13995 bfd *sub;
6a5bb875 13996 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 13997 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 13998 struct elf_link_hash_table *htab;
c152c796 13999
64d03ab5 14000 if (!bed->can_gc_sections
715df9b8 14001 || !is_elf_hash_table (info->hash))
c152c796 14002 {
9793eb77 14003 _bfd_error_handler(_("warning: gc-sections option ignored"));
c152c796
AM
14004 return TRUE;
14005 }
14006
74f0fb50 14007 bed->gc_keep (info);
da44f4e5 14008 htab = elf_hash_table (info);
74f0fb50 14009
9d0a14d3
RS
14010 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14011 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
14012 for (sub = info->input_bfds;
14013 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14014 sub = sub->link.next)
9d0a14d3
RS
14015 {
14016 asection *sec;
14017 struct elf_reloc_cookie cookie;
14018
57963c05
AM
14019 sec = sub->sections;
14020 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14021 continue;
9d0a14d3 14022 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 14023 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
14024 {
14025 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
14026 if (elf_section_data (sec)->sec_info
14027 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
14028 elf_eh_frame_section (sub) = sec;
14029 fini_reloc_cookie_for_section (&cookie, sec);
199af150 14030 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
14031 }
14032 }
9d0a14d3 14033
c152c796 14034 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 14035 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
14036 if (!ok)
14037 return FALSE;
14038
14039 /* Kill the vtable relocations that were not used. */
da44f4e5 14040 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
14041 if (!ok)
14042 return FALSE;
14043
715df9b8 14044 /* Mark dynamically referenced symbols. */
22185505 14045 if (htab->dynamic_sections_created || info->gc_keep_exported)
da44f4e5 14046 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 14047
715df9b8 14048 /* Grovel through relocs to find out who stays ... */
64d03ab5 14049 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 14050 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
14051 {
14052 asection *o;
14053
b19a8f85 14054 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 14055 || elf_object_id (sub) != elf_hash_table_id (htab)
b19a8f85 14056 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
14057 continue;
14058
57963c05
AM
14059 o = sub->sections;
14060 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14061 continue;
14062
7f6ab9f8
AM
14063 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14064 Also treat note sections as a root, if the section is not part
8b6f4cd3
L
14065 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14066 well as FINI_ARRAY sections for ld -r. */
c152c796 14067 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
14068 if (!o->gc_mark
14069 && (o->flags & SEC_EXCLUDE) == 0
24007750 14070 && ((o->flags & SEC_KEEP) != 0
8b6f4cd3
L
14071 || (bfd_link_relocatable (info)
14072 && ((elf_section_data (o)->this_hdr.sh_type
14073 == SHT_PREINIT_ARRAY)
14074 || (elf_section_data (o)->this_hdr.sh_type
14075 == SHT_INIT_ARRAY)
14076 || (elf_section_data (o)->this_hdr.sh_type
14077 == SHT_FINI_ARRAY)))
7f6ab9f8 14078 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
7026832e 14079 && elf_next_in_group (o) == NULL
99fabbc9
JL
14080 && elf_linked_to_section (o) == NULL)
14081 || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14082 && (elf_section_flags (o) & SHF_GNU_RETAIN))))
7f6ab9f8
AM
14083 {
14084 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
14085 return FALSE;
14086 }
c152c796
AM
14087 }
14088
6a5bb875 14089 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 14090 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 14091
c152c796 14092 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 14093 return elf_gc_sweep (abfd, info);
c152c796
AM
14094}
14095\f
14096/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14097
14098bfd_boolean
14099bfd_elf_gc_record_vtinherit (bfd *abfd,
14100 asection *sec,
14101 struct elf_link_hash_entry *h,
14102 bfd_vma offset)
14103{
14104 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14105 struct elf_link_hash_entry **search, *child;
ef53be89 14106 size_t extsymcount;
c152c796
AM
14107 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14108
14109 /* The sh_info field of the symtab header tells us where the
14110 external symbols start. We don't care about the local symbols at
14111 this point. */
14112 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
14113 if (!elf_bad_symtab (abfd))
14114 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
14115
14116 sym_hashes = elf_sym_hashes (abfd);
14117 sym_hashes_end = sym_hashes + extsymcount;
14118
14119 /* Hunt down the child symbol, which is in this section at the same
14120 offset as the relocation. */
14121 for (search = sym_hashes; search != sym_hashes_end; ++search)
14122 {
14123 if ((child = *search) != NULL
14124 && (child->root.type == bfd_link_hash_defined
14125 || child->root.type == bfd_link_hash_defweak)
14126 && child->root.u.def.section == sec
14127 && child->root.u.def.value == offset)
14128 goto win;
14129 }
14130
695344c0 14131 /* xgettext:c-format */
9793eb77 14132 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
2dcf00ce 14133 abfd, sec, (uint64_t) offset);
c152c796
AM
14134 bfd_set_error (bfd_error_invalid_operation);
14135 return FALSE;
14136
14137 win:
cbd0eecf 14138 if (!child->u2.vtable)
f6e332e6 14139 {
cbd0eecf
L
14140 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
14141 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
14142 if (!child->u2.vtable)
f6e332e6
AM
14143 return FALSE;
14144 }
c152c796
AM
14145 if (!h)
14146 {
14147 /* This *should* only be the absolute section. It could potentially
14148 be that someone has defined a non-global vtable though, which
14149 would be bad. It isn't worth paging in the local symbols to be
14150 sure though; that case should simply be handled by the assembler. */
14151
cbd0eecf 14152 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
14153 }
14154 else
cbd0eecf 14155 child->u2.vtable->parent = h;
c152c796
AM
14156
14157 return TRUE;
14158}
14159
14160/* Called from check_relocs to record the existence of a VTENTRY reloc. */
14161
14162bfd_boolean
a0ea3a14 14163bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
c152c796
AM
14164 struct elf_link_hash_entry *h,
14165 bfd_vma addend)
14166{
14167 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14168 unsigned int log_file_align = bed->s->log_file_align;
14169
a0ea3a14
L
14170 if (!h)
14171 {
14172 /* xgettext:c-format */
14173 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14174 abfd, sec);
14175 bfd_set_error (bfd_error_bad_value);
14176 return FALSE;
14177 }
14178
cbd0eecf 14179 if (!h->u2.vtable)
f6e332e6 14180 {
cbd0eecf
L
14181 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14182 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14183 if (!h->u2.vtable)
f6e332e6
AM
14184 return FALSE;
14185 }
14186
cbd0eecf 14187 if (addend >= h->u2.vtable->size)
c152c796
AM
14188 {
14189 size_t size, bytes, file_align;
cbd0eecf 14190 bfd_boolean *ptr = h->u2.vtable->used;
c152c796
AM
14191
14192 /* While the symbol is undefined, we have to be prepared to handle
14193 a zero size. */
14194 file_align = 1 << log_file_align;
14195 if (h->root.type == bfd_link_hash_undefined)
14196 size = addend + file_align;
14197 else
14198 {
14199 size = h->size;
14200 if (addend >= size)
14201 {
14202 /* Oops! We've got a reference past the defined end of
14203 the table. This is probably a bug -- shall we warn? */
14204 size = addend + file_align;
14205 }
14206 }
14207 size = (size + file_align - 1) & -file_align;
14208
14209 /* Allocate one extra entry for use as a "done" flag for the
14210 consolidation pass. */
14211 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
14212
14213 if (ptr)
14214 {
a50b1753 14215 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
14216
14217 if (ptr != NULL)
14218 {
14219 size_t oldbytes;
14220
cbd0eecf 14221 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
c152c796
AM
14222 * sizeof (bfd_boolean));
14223 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14224 }
14225 }
14226 else
a50b1753 14227 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
14228
14229 if (ptr == NULL)
14230 return FALSE;
14231
14232 /* And arrange for that done flag to be at index -1. */
cbd0eecf
L
14233 h->u2.vtable->used = ptr + 1;
14234 h->u2.vtable->size = size;
c152c796
AM
14235 }
14236
cbd0eecf 14237 h->u2.vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
14238
14239 return TRUE;
14240}
14241
ae17ab41
CM
14242/* Map an ELF section header flag to its corresponding string. */
14243typedef struct
14244{
14245 char *flag_name;
14246 flagword flag_value;
14247} elf_flags_to_name_table;
14248
3f75e1d6 14249static const elf_flags_to_name_table elf_flags_to_names [] =
ae17ab41
CM
14250{
14251 { "SHF_WRITE", SHF_WRITE },
14252 { "SHF_ALLOC", SHF_ALLOC },
14253 { "SHF_EXECINSTR", SHF_EXECINSTR },
14254 { "SHF_MERGE", SHF_MERGE },
14255 { "SHF_STRINGS", SHF_STRINGS },
14256 { "SHF_INFO_LINK", SHF_INFO_LINK},
14257 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14258 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14259 { "SHF_GROUP", SHF_GROUP },
14260 { "SHF_TLS", SHF_TLS },
14261 { "SHF_MASKOS", SHF_MASKOS },
14262 { "SHF_EXCLUDE", SHF_EXCLUDE },
14263};
14264
b9c361e0
JL
14265/* Returns TRUE if the section is to be included, otherwise FALSE. */
14266bfd_boolean
ae17ab41 14267bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 14268 struct flag_info *flaginfo,
b9c361e0 14269 asection *section)
ae17ab41 14270{
8b127cbc 14271 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 14272
8b127cbc 14273 if (!flaginfo->flags_initialized)
ae17ab41 14274 {
8b127cbc
AM
14275 bfd *obfd = info->output_bfd;
14276 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14277 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
14278 int with_hex = 0;
14279 int without_hex = 0;
14280
8b127cbc 14281 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 14282 {
b9c361e0 14283 unsigned i;
8b127cbc 14284 flagword (*lookup) (char *);
ae17ab41 14285
8b127cbc
AM
14286 lookup = bed->elf_backend_lookup_section_flags_hook;
14287 if (lookup != NULL)
ae17ab41 14288 {
8b127cbc 14289 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
14290
14291 if (hexval != 0)
14292 {
14293 if (tf->with == with_flags)
14294 with_hex |= hexval;
14295 else if (tf->with == without_flags)
14296 without_hex |= hexval;
14297 tf->valid = TRUE;
14298 continue;
14299 }
ae17ab41 14300 }
8b127cbc 14301 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 14302 {
8b127cbc 14303 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
14304 {
14305 if (tf->with == with_flags)
14306 with_hex |= elf_flags_to_names[i].flag_value;
14307 else if (tf->with == without_flags)
14308 without_hex |= elf_flags_to_names[i].flag_value;
14309 tf->valid = TRUE;
14310 break;
14311 }
14312 }
8b127cbc 14313 if (!tf->valid)
b9c361e0 14314 {
68ffbac6 14315 info->callbacks->einfo
9793eb77 14316 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 14317 return FALSE;
ae17ab41
CM
14318 }
14319 }
8b127cbc
AM
14320 flaginfo->flags_initialized = TRUE;
14321 flaginfo->only_with_flags |= with_hex;
14322 flaginfo->not_with_flags |= without_hex;
ae17ab41 14323 }
ae17ab41 14324
8b127cbc 14325 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
14326 return FALSE;
14327
8b127cbc 14328 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
14329 return FALSE;
14330
14331 return TRUE;
ae17ab41
CM
14332}
14333
c152c796
AM
14334struct alloc_got_off_arg {
14335 bfd_vma gotoff;
10455f89 14336 struct bfd_link_info *info;
c152c796
AM
14337};
14338
14339/* We need a special top-level link routine to convert got reference counts
14340 to real got offsets. */
14341
14342static bfd_boolean
14343elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14344{
a50b1753 14345 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
14346 bfd *obfd = gofarg->info->output_bfd;
14347 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 14348
c152c796
AM
14349 if (h->got.refcount > 0)
14350 {
14351 h->got.offset = gofarg->gotoff;
10455f89 14352 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
14353 }
14354 else
14355 h->got.offset = (bfd_vma) -1;
14356
14357 return TRUE;
14358}
14359
14360/* And an accompanying bit to work out final got entry offsets once
14361 we're done. Should be called from final_link. */
14362
14363bfd_boolean
14364bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14365 struct bfd_link_info *info)
14366{
14367 bfd *i;
14368 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14369 bfd_vma gotoff;
c152c796
AM
14370 struct alloc_got_off_arg gofarg;
14371
10455f89
HPN
14372 BFD_ASSERT (abfd == info->output_bfd);
14373
c152c796
AM
14374 if (! is_elf_hash_table (info->hash))
14375 return FALSE;
14376
14377 /* The GOT offset is relative to the .got section, but the GOT header is
14378 put into the .got.plt section, if the backend uses it. */
14379 if (bed->want_got_plt)
14380 gotoff = 0;
14381 else
14382 gotoff = bed->got_header_size;
14383
14384 /* Do the local .got entries first. */
c72f2fb2 14385 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
14386 {
14387 bfd_signed_vma *local_got;
ef53be89 14388 size_t j, locsymcount;
c152c796
AM
14389 Elf_Internal_Shdr *symtab_hdr;
14390
14391 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14392 continue;
14393
14394 local_got = elf_local_got_refcounts (i);
14395 if (!local_got)
14396 continue;
14397
14398 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14399 if (elf_bad_symtab (i))
14400 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14401 else
14402 locsymcount = symtab_hdr->sh_info;
14403
14404 for (j = 0; j < locsymcount; ++j)
14405 {
14406 if (local_got[j] > 0)
14407 {
14408 local_got[j] = gotoff;
10455f89 14409 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
14410 }
14411 else
14412 local_got[j] = (bfd_vma) -1;
14413 }
14414 }
14415
14416 /* Then the global .got entries. .plt refcounts are handled by
14417 adjust_dynamic_symbol */
14418 gofarg.gotoff = gotoff;
10455f89 14419 gofarg.info = info;
c152c796
AM
14420 elf_link_hash_traverse (elf_hash_table (info),
14421 elf_gc_allocate_got_offsets,
14422 &gofarg);
14423 return TRUE;
14424}
14425
14426/* Many folk need no more in the way of final link than this, once
14427 got entry reference counting is enabled. */
14428
14429bfd_boolean
14430bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14431{
14432 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14433 return FALSE;
14434
14435 /* Invoke the regular ELF backend linker to do all the work. */
14436 return bfd_elf_final_link (abfd, info);
14437}
14438
14439bfd_boolean
14440bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14441{
a50b1753 14442 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
14443
14444 if (rcookie->bad_symtab)
14445 rcookie->rel = rcookie->rels;
14446
14447 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14448 {
14449 unsigned long r_symndx;
14450
14451 if (! rcookie->bad_symtab)
14452 if (rcookie->rel->r_offset > offset)
14453 return FALSE;
14454 if (rcookie->rel->r_offset != offset)
14455 continue;
14456
14457 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 14458 if (r_symndx == STN_UNDEF)
c152c796
AM
14459 return TRUE;
14460
14461 if (r_symndx >= rcookie->locsymcount
14462 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14463 {
14464 struct elf_link_hash_entry *h;
14465
14466 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14467
14468 while (h->root.type == bfd_link_hash_indirect
14469 || h->root.type == bfd_link_hash_warning)
14470 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14471
14472 if ((h->root.type == bfd_link_hash_defined
14473 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
14474 && (h->root.u.def.section->owner != rcookie->abfd
14475 || h->root.u.def.section->kept_section != NULL
14476 || discarded_section (h->root.u.def.section)))
c152c796 14477 return TRUE;
c152c796
AM
14478 }
14479 else
14480 {
14481 /* It's not a relocation against a global symbol,
14482 but it could be a relocation against a local
14483 symbol for a discarded section. */
14484 asection *isec;
14485 Elf_Internal_Sym *isym;
14486
14487 /* Need to: get the symbol; get the section. */
14488 isym = &rcookie->locsyms[r_symndx];
cb33740c 14489 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
14490 if (isec != NULL
14491 && (isec->kept_section != NULL
14492 || discarded_section (isec)))
cb33740c 14493 return TRUE;
c152c796
AM
14494 }
14495 return FALSE;
14496 }
14497 return FALSE;
14498}
14499
14500/* Discard unneeded references to discarded sections.
75938853
AM
14501 Returns -1 on error, 1 if any section's size was changed, 0 if
14502 nothing changed. This function assumes that the relocations are in
14503 sorted order, which is true for all known assemblers. */
c152c796 14504
75938853 14505int
c152c796
AM
14506bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14507{
14508 struct elf_reloc_cookie cookie;
18cd5bce 14509 asection *o;
c152c796 14510 bfd *abfd;
75938853 14511 int changed = 0;
c152c796
AM
14512
14513 if (info->traditional_format
14514 || !is_elf_hash_table (info->hash))
75938853 14515 return 0;
c152c796 14516
18cd5bce
AM
14517 o = bfd_get_section_by_name (output_bfd, ".stab");
14518 if (o != NULL)
c152c796 14519 {
18cd5bce 14520 asection *i;
c152c796 14521
18cd5bce 14522 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 14523 {
18cd5bce
AM
14524 if (i->size == 0
14525 || i->reloc_count == 0
14526 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14527 continue;
c152c796 14528
18cd5bce
AM
14529 abfd = i->owner;
14530 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14531 continue;
c152c796 14532
18cd5bce 14533 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14534 return -1;
c152c796 14535
18cd5bce
AM
14536 if (_bfd_discard_section_stabs (abfd, i,
14537 elf_section_data (i)->sec_info,
5241d853
RS
14538 bfd_elf_reloc_symbol_deleted_p,
14539 &cookie))
75938853 14540 changed = 1;
18cd5bce
AM
14541
14542 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14543 }
18cd5bce
AM
14544 }
14545
2f0c68f2
CM
14546 o = NULL;
14547 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14548 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
14549 if (o != NULL)
14550 {
14551 asection *i;
d7153c4a 14552 int eh_changed = 0;
66631823 14553 unsigned int eh_alignment; /* Octets. */
c152c796 14554
18cd5bce 14555 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 14556 {
18cd5bce
AM
14557 if (i->size == 0)
14558 continue;
14559
14560 abfd = i->owner;
14561 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14562 continue;
14563
14564 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14565 return -1;
18cd5bce
AM
14566
14567 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14568 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
14569 bfd_elf_reloc_symbol_deleted_p,
14570 &cookie))
d7153c4a
AM
14571 {
14572 eh_changed = 1;
14573 if (i->size != i->rawsize)
14574 changed = 1;
14575 }
18cd5bce
AM
14576
14577 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14578 }
9866ffe2 14579
66631823
CE
14580 eh_alignment = ((1 << o->alignment_power)
14581 * bfd_octets_per_byte (output_bfd, o));
9866ffe2
AM
14582 /* Skip over zero terminator, and prevent empty sections from
14583 adding alignment padding at the end. */
14584 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14585 if (i->size == 0)
14586 i->flags |= SEC_EXCLUDE;
14587 else if (i->size > 4)
14588 break;
14589 /* The last non-empty eh_frame section doesn't need padding. */
14590 if (i != NULL)
14591 i = i->map_tail.s;
14592 /* Any prior sections must pad the last FDE out to the output
14593 section alignment. Otherwise we might have zero padding
14594 between sections, which would be seen as a terminator. */
14595 for (; i != NULL; i = i->map_tail.s)
14596 if (i->size == 4)
14597 /* All but the last zero terminator should have been removed. */
14598 BFD_FAIL ();
14599 else
14600 {
14601 bfd_size_type size
14602 = (i->size + eh_alignment - 1) & -eh_alignment;
14603 if (i->size != size)
af471f82 14604 {
9866ffe2
AM
14605 i->size = size;
14606 changed = 1;
14607 eh_changed = 1;
af471f82 14608 }
9866ffe2 14609 }
d7153c4a
AM
14610 if (eh_changed)
14611 elf_link_hash_traverse (elf_hash_table (info),
14612 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
18cd5bce 14613 }
c152c796 14614
18cd5bce
AM
14615 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14616 {
14617 const struct elf_backend_data *bed;
57963c05 14618 asection *s;
c152c796 14619
18cd5bce
AM
14620 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14621 continue;
57963c05
AM
14622 s = abfd->sections;
14623 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14624 continue;
18cd5bce
AM
14625
14626 bed = get_elf_backend_data (abfd);
14627
14628 if (bed->elf_backend_discard_info != NULL)
14629 {
14630 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 14631 return -1;
18cd5bce
AM
14632
14633 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 14634 changed = 1;
18cd5bce
AM
14635
14636 fini_reloc_cookie (&cookie, abfd);
14637 }
c152c796
AM
14638 }
14639
2f0c68f2
CM
14640 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14641 _bfd_elf_end_eh_frame_parsing (info);
14642
14643 if (info->eh_frame_hdr_type
0e1862bb 14644 && !bfd_link_relocatable (info)
c152c796 14645 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 14646 changed = 1;
c152c796 14647
75938853 14648 return changed;
c152c796 14649}
082b7297 14650
43e1669b 14651bfd_boolean
0c511000 14652_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 14653 asection *sec,
c0f00686 14654 struct bfd_link_info *info)
082b7297
L
14655{
14656 flagword flags;
c77ec726 14657 const char *name, *key;
082b7297
L
14658 struct bfd_section_already_linked *l;
14659 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 14660
c77ec726
AM
14661 if (sec->output_section == bfd_abs_section_ptr)
14662 return FALSE;
0c511000 14663
c77ec726 14664 flags = sec->flags;
0c511000 14665
c77ec726
AM
14666 /* Return if it isn't a linkonce section. A comdat group section
14667 also has SEC_LINK_ONCE set. */
14668 if ((flags & SEC_LINK_ONCE) == 0)
14669 return FALSE;
0c511000 14670
c77ec726
AM
14671 /* Don't put group member sections on our list of already linked
14672 sections. They are handled as a group via their group section. */
14673 if (elf_sec_group (sec) != NULL)
14674 return FALSE;
0c511000 14675
c77ec726
AM
14676 /* For a SHT_GROUP section, use the group signature as the key. */
14677 name = sec->name;
14678 if ((flags & SEC_GROUP) != 0
14679 && elf_next_in_group (sec) != NULL
14680 && elf_group_name (elf_next_in_group (sec)) != NULL)
14681 key = elf_group_name (elf_next_in_group (sec));
14682 else
14683 {
14684 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 14685 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
14686 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14687 key++;
0c511000 14688 else
c77ec726
AM
14689 /* Must be a user linkonce section that doesn't follow gcc's
14690 naming convention. In this case we won't be matching
14691 single member groups. */
14692 key = name;
0c511000 14693 }
6d2cd210 14694
c77ec726 14695 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
14696
14697 for (l = already_linked_list->entry; l != NULL; l = l->next)
14698 {
c2370991 14699 /* We may have 2 different types of sections on the list: group
c77ec726
AM
14700 sections with a signature of <key> (<key> is some string),
14701 and linkonce sections named .gnu.linkonce.<type>.<key>.
14702 Match like sections. LTO plugin sections are an exception.
14703 They are always named .gnu.linkonce.t.<key> and match either
14704 type of section. */
14705 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14706 && ((flags & SEC_GROUP) != 0
14707 || strcmp (name, l->sec->name) == 0))
e8a83e93
MB
14708 || (l->sec->owner->flags & BFD_PLUGIN) != 0
14709 || (sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
14710 {
14711 /* The section has already been linked. See if we should
6d2cd210 14712 issue a warning. */
c77ec726
AM
14713 if (!_bfd_handle_already_linked (sec, l, info))
14714 return FALSE;
082b7297 14715
c77ec726 14716 if (flags & SEC_GROUP)
3d7f7666 14717 {
c77ec726
AM
14718 asection *first = elf_next_in_group (sec);
14719 asection *s = first;
3d7f7666 14720
c77ec726 14721 while (s != NULL)
3d7f7666 14722 {
c77ec726
AM
14723 s->output_section = bfd_abs_section_ptr;
14724 /* Record which group discards it. */
14725 s->kept_section = l->sec;
14726 s = elf_next_in_group (s);
14727 /* These lists are circular. */
14728 if (s == first)
14729 break;
3d7f7666
L
14730 }
14731 }
082b7297 14732
43e1669b 14733 return TRUE;
082b7297
L
14734 }
14735 }
14736
c77ec726
AM
14737 /* A single member comdat group section may be discarded by a
14738 linkonce section and vice versa. */
14739 if ((flags & SEC_GROUP) != 0)
3d7f7666 14740 {
c77ec726 14741 asection *first = elf_next_in_group (sec);
c2370991 14742
c77ec726
AM
14743 if (first != NULL && elf_next_in_group (first) == first)
14744 /* Check this single member group against linkonce sections. */
14745 for (l = already_linked_list->entry; l != NULL; l = l->next)
14746 if ((l->sec->flags & SEC_GROUP) == 0
14747 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14748 {
14749 first->output_section = bfd_abs_section_ptr;
14750 first->kept_section = l->sec;
14751 sec->output_section = bfd_abs_section_ptr;
14752 break;
14753 }
14754 }
14755 else
14756 /* Check this linkonce section against single member groups. */
14757 for (l = already_linked_list->entry; l != NULL; l = l->next)
14758 if (l->sec->flags & SEC_GROUP)
6d2cd210 14759 {
c77ec726 14760 asection *first = elf_next_in_group (l->sec);
6d2cd210 14761
c77ec726
AM
14762 if (first != NULL
14763 && elf_next_in_group (first) == first
14764 && bfd_elf_match_symbols_in_sections (first, sec, info))
14765 {
14766 sec->output_section = bfd_abs_section_ptr;
14767 sec->kept_section = first;
14768 break;
14769 }
6d2cd210 14770 }
0c511000 14771
c77ec726
AM
14772 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14773 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14774 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14775 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14776 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14777 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14778 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14779 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14780 The reverse order cannot happen as there is never a bfd with only the
14781 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14782 matter as here were are looking only for cross-bfd sections. */
14783
14784 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14785 for (l = already_linked_list->entry; l != NULL; l = l->next)
14786 if ((l->sec->flags & SEC_GROUP) == 0
14787 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14788 {
14789 if (abfd != l->sec->owner)
14790 sec->output_section = bfd_abs_section_ptr;
14791 break;
14792 }
80c29487 14793
082b7297 14794 /* This is the first section with this name. Record it. */
c77ec726 14795 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 14796 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 14797 return sec->output_section == bfd_abs_section_ptr;
082b7297 14798}
81e1b023 14799
a4d8e49b
L
14800bfd_boolean
14801_bfd_elf_common_definition (Elf_Internal_Sym *sym)
14802{
14803 return sym->st_shndx == SHN_COMMON;
14804}
14805
14806unsigned int
14807_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14808{
14809 return SHN_COMMON;
14810}
14811
14812asection *
14813_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14814{
14815 return bfd_com_section_ptr;
14816}
10455f89
HPN
14817
14818bfd_vma
14819_bfd_elf_default_got_elt_size (bfd *abfd,
14820 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14821 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14822 bfd *ibfd ATTRIBUTE_UNUSED,
14823 unsigned long symndx ATTRIBUTE_UNUSED)
14824{
14825 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14826 return bed->s->arch_size / 8;
14827}
83bac4b0
NC
14828
14829/* Routines to support the creation of dynamic relocs. */
14830
83bac4b0
NC
14831/* Returns the name of the dynamic reloc section associated with SEC. */
14832
14833static const char *
14834get_dynamic_reloc_section_name (bfd * abfd,
14835 asection * sec,
14836 bfd_boolean is_rela)
14837{
ddcf1fcf 14838 char *name;
fd361982 14839 const char *old_name = bfd_section_name (sec);
ddcf1fcf 14840 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 14841
ddcf1fcf 14842 if (old_name == NULL)
83bac4b0
NC
14843 return NULL;
14844
ddcf1fcf 14845 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 14846 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
14847
14848 return name;
14849}
14850
14851/* Returns the dynamic reloc section associated with SEC.
14852 If necessary compute the name of the dynamic reloc section based
14853 on SEC's name (looked up in ABFD's string table) and the setting
14854 of IS_RELA. */
14855
14856asection *
14857_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14858 asection * sec,
14859 bfd_boolean is_rela)
14860{
14861 asection * reloc_sec = elf_section_data (sec)->sreloc;
14862
14863 if (reloc_sec == NULL)
14864 {
14865 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14866
14867 if (name != NULL)
14868 {
3d4d4302 14869 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
14870
14871 if (reloc_sec != NULL)
14872 elf_section_data (sec)->sreloc = reloc_sec;
14873 }
14874 }
14875
14876 return reloc_sec;
14877}
14878
14879/* Returns the dynamic reloc section associated with SEC. If the
14880 section does not exist it is created and attached to the DYNOBJ
14881 bfd and stored in the SRELOC field of SEC's elf_section_data
14882 structure.
f8076f98 14883
83bac4b0
NC
14884 ALIGNMENT is the alignment for the newly created section and
14885 IS_RELA defines whether the name should be .rela.<SEC's name>
14886 or .rel.<SEC's name>. The section name is looked up in the
14887 string table associated with ABFD. */
14888
14889asection *
ca4be51c
AM
14890_bfd_elf_make_dynamic_reloc_section (asection *sec,
14891 bfd *dynobj,
14892 unsigned int alignment,
14893 bfd *abfd,
14894 bfd_boolean is_rela)
83bac4b0
NC
14895{
14896 asection * reloc_sec = elf_section_data (sec)->sreloc;
14897
14898 if (reloc_sec == NULL)
14899 {
14900 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14901
14902 if (name == NULL)
14903 return NULL;
14904
3d4d4302 14905 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
14906
14907 if (reloc_sec == NULL)
14908 {
3d4d4302
AM
14909 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14910 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
14911 if ((sec->flags & SEC_ALLOC) != 0)
14912 flags |= SEC_ALLOC | SEC_LOAD;
14913
3d4d4302 14914 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
14915 if (reloc_sec != NULL)
14916 {
8877b5e5
AM
14917 /* _bfd_elf_get_sec_type_attr chooses a section type by
14918 name. Override as it may be wrong, eg. for a user
14919 section named "auto" we'll get ".relauto" which is
14920 seen to be a .rela section. */
14921 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
fd361982 14922 if (!bfd_set_section_alignment (reloc_sec, alignment))
83bac4b0
NC
14923 reloc_sec = NULL;
14924 }
14925 }
14926
14927 elf_section_data (sec)->sreloc = reloc_sec;
14928 }
14929
14930 return reloc_sec;
14931}
1338dd10 14932
bffebb6b
AM
14933/* Copy the ELF symbol type and other attributes for a linker script
14934 assignment from HSRC to HDEST. Generally this should be treated as
14935 if we found a strong non-dynamic definition for HDEST (except that
14936 ld ignores multiple definition errors). */
1338dd10 14937void
bffebb6b
AM
14938_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14939 struct bfd_link_hash_entry *hdest,
14940 struct bfd_link_hash_entry *hsrc)
1338dd10 14941{
bffebb6b
AM
14942 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14943 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14944 Elf_Internal_Sym isym;
1338dd10
PB
14945
14946 ehdest->type = ehsrc->type;
35fc36a8 14947 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
14948
14949 isym.st_other = ehsrc->other;
5160d0f3 14950 elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, TRUE, FALSE);
1338dd10 14951}
351f65ca
L
14952
14953/* Append a RELA relocation REL to section S in BFD. */
14954
14955void
14956elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14957{
14958 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14959 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14960 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14961 bed->s->swap_reloca_out (abfd, rel, loc);
14962}
14963
14964/* Append a REL relocation REL to section S in BFD. */
14965
14966void
14967elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14968{
14969 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14970 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14971 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 14972 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 14973}
7dba9362
AM
14974
14975/* Define __start, __stop, .startof. or .sizeof. symbol. */
14976
14977struct bfd_link_hash_entry *
14978bfd_elf_define_start_stop (struct bfd_link_info *info,
14979 const char *symbol, asection *sec)
14980{
487b6440 14981 struct elf_link_hash_entry *h;
7dba9362 14982
487b6440
AM
14983 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14984 FALSE, FALSE, TRUE);
e1b5d517 14985 /* NB: Common symbols will be turned into definition later. */
487b6440
AM
14986 if (h != NULL
14987 && (h->root.type == bfd_link_hash_undefined
14988 || h->root.type == bfd_link_hash_undefweak
e1b5d517
L
14989 || ((h->ref_regular || h->def_dynamic)
14990 && !h->def_regular
14991 && h->root.type != bfd_link_hash_common)))
7dba9362 14992 {
bf3077a6 14993 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
e1b5d517 14994 h->verinfo.verdef = NULL;
487b6440
AM
14995 h->root.type = bfd_link_hash_defined;
14996 h->root.u.def.section = sec;
14997 h->root.u.def.value = 0;
14998 h->def_regular = 1;
14999 h->def_dynamic = 0;
15000 h->start_stop = 1;
15001 h->u2.start_stop_section = sec;
15002 if (symbol[0] == '.')
15003 {
15004 /* .startof. and .sizeof. symbols are local. */
559192d8
AM
15005 const struct elf_backend_data *bed;
15006 bed = get_elf_backend_data (info->output_bfd);
15007 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
487b6440 15008 }
36b8fda5
AM
15009 else
15010 {
15011 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
cae64165
RM
15012 h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15013 | info->start_stop_visibility);
bf3077a6 15014 if (was_dynamic)
36b8fda5
AM
15015 bfd_elf_link_record_dynamic_symbol (info, h);
15016 }
487b6440 15017 return &h->root;
7dba9362 15018 }
487b6440 15019 return NULL;
7dba9362 15020}
5dbc8b37
L
15021
15022/* Find dynamic relocs for H that apply to read-only sections. */
15023
15024asection *
15025_bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15026{
15027 struct elf_dyn_relocs *p;
15028
15029 for (p = h->dyn_relocs; p != NULL; p = p->next)
15030 {
15031 asection *s = p->sec->output_section;
15032
15033 if (s != NULL && (s->flags & SEC_READONLY) != 0)
15034 return p->sec;
15035 }
15036 return NULL;
15037}
d49e5065
L
15038
15039/* Set DF_TEXTREL if we find any dynamic relocs that apply to
15040 read-only sections. */
15041
15042bfd_boolean
15043_bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15044{
15045 asection *sec;
15046
15047 if (h->root.type == bfd_link_hash_indirect)
15048 return TRUE;
15049
15050 sec = _bfd_elf_readonly_dynrelocs (h);
15051 if (sec != NULL)
15052 {
15053 struct bfd_link_info *info = (struct bfd_link_info *) inf;
15054
15055 info->flags |= DF_TEXTREL;
15056 /* xgettext:c-format */
15057 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15058 "in read-only section `%pA'\n"),
15059 sec->owner, h->root.root.string, sec);
15060
15061 if (bfd_link_textrel_check (info))
15062 /* xgettext:c-format */
15063 info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15064 "in read-only section `%pA'\n"),
15065 sec->owner, h->root.root.string, sec);
15066
15067 /* Not an error, just cut short the traversal. */
15068 return FALSE;
15069 }
15070 return TRUE;
15071}
3084d7a2
L
15072
15073/* Add dynamic tags. */
15074
15075bfd_boolean
15076_bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
15077 bfd_boolean need_dynamic_reloc)
15078{
15079 struct elf_link_hash_table *htab = elf_hash_table (info);
15080
15081 if (htab->dynamic_sections_created)
15082 {
15083 /* Add some entries to the .dynamic section. We fill in the
15084 values later, in finish_dynamic_sections, but we must add
15085 the entries now so that we get the correct size for the
15086 .dynamic section. The DT_DEBUG entry is filled in by the
15087 dynamic linker and used by the debugger. */
15088#define add_dynamic_entry(TAG, VAL) \
15089 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15090
15091 const struct elf_backend_data *bed
15092 = get_elf_backend_data (output_bfd);
15093
15094 if (bfd_link_executable (info))
15095 {
15096 if (!add_dynamic_entry (DT_DEBUG, 0))
15097 return FALSE;
15098 }
15099
15100 if (htab->dt_pltgot_required || htab->splt->size != 0)
15101 {
15102 /* DT_PLTGOT is used by prelink even if there is no PLT
15103 relocation. */
15104 if (!add_dynamic_entry (DT_PLTGOT, 0))
15105 return FALSE;
15106 }
15107
15108 if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15109 {
15110 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15111 || !add_dynamic_entry (DT_PLTREL,
15112 (bed->rela_plts_and_copies_p
15113 ? DT_RELA : DT_REL))
15114 || !add_dynamic_entry (DT_JMPREL, 0))
15115 return FALSE;
15116 }
15117
15118 if (htab->tlsdesc_plt
15119 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15120 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
15121 return FALSE;
15122
15123 if (need_dynamic_reloc)
15124 {
15125 if (bed->rela_plts_and_copies_p)
15126 {
15127 if (!add_dynamic_entry (DT_RELA, 0)
15128 || !add_dynamic_entry (DT_RELASZ, 0)
15129 || !add_dynamic_entry (DT_RELAENT,
15130 bed->s->sizeof_rela))
15131 return FALSE;
15132 }
15133 else
15134 {
15135 if (!add_dynamic_entry (DT_REL, 0)
15136 || !add_dynamic_entry (DT_RELSZ, 0)
15137 || !add_dynamic_entry (DT_RELENT,
15138 bed->s->sizeof_rel))
15139 return FALSE;
15140 }
15141
15142 /* If any dynamic relocs apply to a read-only section,
15143 then we need a DT_TEXTREL entry. */
15144 if ((info->flags & DF_TEXTREL) == 0)
15145 elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15146 info);
15147
15148 if ((info->flags & DF_TEXTREL) != 0)
15149 {
15150 if (htab->ifunc_resolvers)
15151 info->callbacks->einfo
15152 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15153 "may result in a segfault at runtime; recompile with %s\n"),
15154 bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15155
15156 if (!add_dynamic_entry (DT_TEXTREL, 0))
15157 return FALSE;
15158 }
15159 }
15160 }
15161#undef add_dynamic_entry
15162
15163 return TRUE;
15164}