Automatic date update in version.in
[deliverable/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,
f01fb44c 1853 bfd_boolean *dynsym)
45d6a902
AM
1854{
1855 bfd_boolean type_change_ok;
1856 bfd_boolean size_change_ok;
1857 bfd_boolean skip;
1858 char *shortname;
1859 struct elf_link_hash_entry *hi;
1860 struct bfd_link_hash_entry *bh;
9c5bfbb7 1861 const struct elf_backend_data *bed;
45d6a902
AM
1862 bfd_boolean collect;
1863 bfd_boolean dynamic;
f01fb44c 1864 bfd *override;
45d6a902
AM
1865 char *p;
1866 size_t len, shortlen;
ffd65175 1867 asection *tmp_sec;
6e33951e 1868 bfd_boolean matched;
45d6a902 1869
422f1182
L
1870 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1871 return TRUE;
1872
45d6a902
AM
1873 /* If this symbol has a version, and it is the default version, we
1874 create an indirect symbol from the default name to the fully
1875 decorated name. This will cause external references which do not
1876 specify a version to be bound to this version of the symbol. */
1877 p = strchr (name, ELF_VER_CHR);
422f1182
L
1878 if (h->versioned == unknown)
1879 {
1880 if (p == NULL)
1881 {
1882 h->versioned = unversioned;
1883 return TRUE;
1884 }
1885 else
1886 {
1887 if (p[1] != ELF_VER_CHR)
1888 {
1889 h->versioned = versioned_hidden;
1890 return TRUE;
1891 }
1892 else
1893 h->versioned = versioned;
1894 }
1895 }
4373f8af
L
1896 else
1897 {
1898 /* PR ld/19073: We may see an unversioned definition after the
1899 default version. */
1900 if (p == NULL)
1901 return TRUE;
1902 }
45d6a902 1903
45d6a902
AM
1904 bed = get_elf_backend_data (abfd);
1905 collect = bed->collect;
1906 dynamic = (abfd->flags & DYNAMIC) != 0;
1907
1908 shortlen = p - name;
a50b1753 1909 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1910 if (shortname == NULL)
1911 return FALSE;
1912 memcpy (shortname, name, shortlen);
1913 shortname[shortlen] = '\0';
1914
1915 /* We are going to create a new symbol. Merge it with any existing
1916 symbol with this name. For the purposes of the merge, act as
1917 though we were defining the symbol we just defined, although we
1918 actually going to define an indirect symbol. */
1919 type_change_ok = FALSE;
1920 size_change_ok = FALSE;
6e33951e 1921 matched = TRUE;
ffd65175
AM
1922 tmp_sec = sec;
1923 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
f01fb44c 1924 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1925 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1926 return FALSE;
1927
1928 if (skip)
1929 goto nondefault;
1930
5fa370e4 1931 if (hi->def_regular || ELF_COMMON_DEF_P (hi))
5b677558
AM
1932 {
1933 /* If the undecorated symbol will have a version added by a
1934 script different to H, then don't indirect to/from the
1935 undecorated symbol. This isn't ideal because we may not yet
1936 have seen symbol versions, if given by a script on the
1937 command line rather than via --version-script. */
1938 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1939 {
1940 bfd_boolean hide;
1941
1942 hi->verinfo.vertree
1943 = bfd_find_version_for_sym (info->version_info,
1944 hi->root.root.string, &hide);
1945 if (hi->verinfo.vertree != NULL && hide)
1946 {
1947 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1948 goto nondefault;
1949 }
1950 }
1951 if (hi->verinfo.vertree != NULL
1952 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
f01fb44c 1953 goto nondefault;
5b677558
AM
1954 }
1955
f01fb44c 1956 if (! override)
45d6a902 1957 {
c6e8a9a8 1958 /* Add the default symbol if not performing a relocatable link. */
0e1862bb 1959 if (! bfd_link_relocatable (info))
c6e8a9a8
L
1960 {
1961 bh = &hi->root;
fbcc8baf 1962 if (bh->type == bfd_link_hash_defined
6cc71b82 1963 && bh->u.def.section->owner != NULL
fbcc8baf
L
1964 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1965 {
1966 /* Mark the previous definition from IR object as
1967 undefined so that the generic linker will override
1968 it. */
1969 bh->type = bfd_link_hash_undefined;
1970 bh->u.undef.abfd = bh->u.def.section->owner;
1971 }
c6e8a9a8
L
1972 if (! (_bfd_generic_link_add_one_symbol
1973 (info, abfd, shortname, BSF_INDIRECT,
1974 bfd_ind_section_ptr,
1975 0, name, FALSE, collect, &bh)))
1976 return FALSE;
1977 hi = (struct elf_link_hash_entry *) bh;
1978 }
45d6a902
AM
1979 }
1980 else
1981 {
1982 /* In this case the symbol named SHORTNAME is overriding the
1983 indirect symbol we want to add. We were planning on making
1984 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1985 is the name without a version. NAME is the fully versioned
1986 name, and it is the default version.
1987
1988 Overriding means that we already saw a definition for the
1989 symbol SHORTNAME in a regular object, and it is overriding
1990 the symbol defined in the dynamic object.
1991
1992 When this happens, we actually want to change NAME, the
1993 symbol we just added, to refer to SHORTNAME. This will cause
1994 references to NAME in the shared object to become references
1995 to SHORTNAME in the regular object. This is what we expect
1996 when we override a function in a shared object: that the
1997 references in the shared object will be mapped to the
1998 definition in the regular object. */
1999
2000 while (hi->root.type == bfd_link_hash_indirect
2001 || hi->root.type == bfd_link_hash_warning)
2002 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2003
2004 h->root.type = bfd_link_hash_indirect;
2005 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 2006 if (h->def_dynamic)
45d6a902 2007 {
f5385ebf
AM
2008 h->def_dynamic = 0;
2009 hi->ref_dynamic = 1;
2010 if (hi->ref_regular
2011 || hi->def_regular)
45d6a902 2012 {
c152c796 2013 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
2014 return FALSE;
2015 }
2016 }
2017
2018 /* Now set HI to H, so that the following code will set the
2019 other fields correctly. */
2020 hi = h;
2021 }
2022
fab4a87f
L
2023 /* Check if HI is a warning symbol. */
2024 if (hi->root.type == bfd_link_hash_warning)
2025 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2026
45d6a902
AM
2027 /* If there is a duplicate definition somewhere, then HI may not
2028 point to an indirect symbol. We will have reported an error to
2029 the user in that case. */
2030
2031 if (hi->root.type == bfd_link_hash_indirect)
2032 {
2033 struct elf_link_hash_entry *ht;
2034
45d6a902 2035 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 2036 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902 2037
5160d0f3
AM
2038 /* If we first saw a reference to SHORTNAME with non-default
2039 visibility, merge that visibility to the @@VER symbol. */
2040 elf_merge_st_other (abfd, ht, hi->other, sec, TRUE, dynamic);
2041
68c88cd4
AM
2042 /* A reference to the SHORTNAME symbol from a dynamic library
2043 will be satisfied by the versioned symbol at runtime. In
2044 effect, we have a reference to the versioned symbol. */
2045 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2046 hi->dynamic_def |= ht->dynamic_def;
2047
45d6a902
AM
2048 /* See if the new flags lead us to realize that the symbol must
2049 be dynamic. */
2050 if (! *dynsym)
2051 {
2052 if (! dynamic)
2053 {
0e1862bb 2054 if (! bfd_link_executable (info)
90c984fc 2055 || hi->def_dynamic
f5385ebf 2056 || hi->ref_dynamic)
45d6a902
AM
2057 *dynsym = TRUE;
2058 }
2059 else
2060 {
f5385ebf 2061 if (hi->ref_regular)
45d6a902
AM
2062 *dynsym = TRUE;
2063 }
2064 }
2065 }
2066
2067 /* We also need to define an indirection from the nondefault version
2068 of the symbol. */
2069
dc1e8a47 2070 nondefault:
45d6a902 2071 len = strlen (name);
a50b1753 2072 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
2073 if (shortname == NULL)
2074 return FALSE;
2075 memcpy (shortname, name, shortlen);
2076 memcpy (shortname + shortlen, p + 1, len - shortlen);
2077
2078 /* Once again, merge with any existing symbol. */
2079 type_change_ok = FALSE;
2080 size_change_ok = FALSE;
ffd65175
AM
2081 tmp_sec = sec;
2082 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
f01fb44c 2083 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 2084 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
2085 return FALSE;
2086
2087 if (skip)
726d7d1e
AM
2088 {
2089 if (!dynamic
2090 && h->root.type == bfd_link_hash_defweak
2091 && hi->root.type == bfd_link_hash_defined)
2092 {
2093 /* We are handling a weak sym@@ver and attempting to define
2094 a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2095 new weak sym@ver because there is already a strong sym@ver.
2096 However, sym@ver and sym@@ver are really the same symbol.
2097 The existing strong sym@ver ought to override sym@@ver. */
2098 h->root.type = bfd_link_hash_defined;
2099 h->root.u.def.section = hi->root.u.def.section;
2100 h->root.u.def.value = hi->root.u.def.value;
2101 hi->root.type = bfd_link_hash_indirect;
2102 hi->root.u.i.link = &h->root;
2103 }
2104 else
2105 return TRUE;
2106 }
f01fb44c 2107 else if (override)
45d6a902
AM
2108 {
2109 /* Here SHORTNAME is a versioned name, so we don't expect to see
2110 the type of override we do in the case above unless it is
4cc11e76 2111 overridden by a versioned definition. */
45d6a902
AM
2112 if (hi->root.type != bfd_link_hash_defined
2113 && hi->root.type != bfd_link_hash_defweak)
4eca0228 2114 _bfd_error_handler
695344c0 2115 /* xgettext:c-format */
871b3ab2 2116 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
d003868e 2117 abfd, shortname);
726d7d1e 2118 return TRUE;
45d6a902
AM
2119 }
2120 else
2121 {
2122 bh = &hi->root;
2123 if (! (_bfd_generic_link_add_one_symbol
2124 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 2125 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
2126 return FALSE;
2127 hi = (struct elf_link_hash_entry *) bh;
726d7d1e 2128 }
45d6a902 2129
726d7d1e
AM
2130 /* If there is a duplicate definition somewhere, then HI may not
2131 point to an indirect symbol. We will have reported an error
2132 to the user in that case. */
2133 if (hi->root.type == bfd_link_hash_indirect)
2134 {
2135 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2136 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2137 hi->dynamic_def |= h->dynamic_def;
45d6a902 2138
726d7d1e
AM
2139 /* If we first saw a reference to @VER symbol with
2140 non-default visibility, merge that visibility to the
2141 @@VER symbol. */
2142 elf_merge_st_other (abfd, h, hi->other, sec, TRUE, dynamic);
2143
2144 /* See if the new flags lead us to realize that the symbol
2145 must be dynamic. */
2146 if (! *dynsym)
45d6a902 2147 {
726d7d1e 2148 if (! dynamic)
45d6a902 2149 {
726d7d1e
AM
2150 if (! bfd_link_executable (info)
2151 || hi->ref_dynamic)
2152 *dynsym = TRUE;
2153 }
2154 else
2155 {
2156 if (hi->ref_regular)
2157 *dynsym = TRUE;
45d6a902
AM
2158 }
2159 }
2160 }
2161
2162 return TRUE;
2163}
2164\f
2165/* This routine is used to export all defined symbols into the dynamic
2166 symbol table. It is called via elf_link_hash_traverse. */
2167
28caa186 2168static bfd_boolean
268b6b39 2169_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2170{
a50b1753 2171 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
2172
2173 /* Ignore indirect symbols. These are added by the versioning code. */
2174 if (h->root.type == bfd_link_hash_indirect)
2175 return TRUE;
2176
7686d77d
AM
2177 /* Ignore this if we won't export it. */
2178 if (!eif->info->export_dynamic && !h->dynamic)
2179 return TRUE;
45d6a902
AM
2180
2181 if (h->dynindx == -1
fd91d419
L
2182 && (h->def_regular || h->ref_regular)
2183 && ! bfd_hide_sym_by_version (eif->info->version_info,
2184 h->root.root.string))
45d6a902 2185 {
fd91d419 2186 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 2187 {
fd91d419
L
2188 eif->failed = TRUE;
2189 return FALSE;
45d6a902
AM
2190 }
2191 }
2192
2193 return TRUE;
2194}
2195\f
2196/* Look through the symbols which are defined in other shared
2197 libraries and referenced here. Update the list of version
2198 dependencies. This will be put into the .gnu.version_r section.
2199 This function is called via elf_link_hash_traverse. */
2200
28caa186 2201static bfd_boolean
268b6b39
AM
2202_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2203 void *data)
45d6a902 2204{
a50b1753 2205 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
2206 Elf_Internal_Verneed *t;
2207 Elf_Internal_Vernaux *a;
986f0783 2208 size_t amt;
45d6a902 2209
45d6a902
AM
2210 /* We only care about symbols defined in shared objects with version
2211 information. */
f5385ebf
AM
2212 if (!h->def_dynamic
2213 || h->def_regular
45d6a902 2214 || h->dynindx == -1
7b20f099
AM
2215 || h->verinfo.verdef == NULL
2216 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2217 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
45d6a902
AM
2218 return TRUE;
2219
2220 /* See if we already know about this version. */
28caa186
AM
2221 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2222 t != NULL;
2223 t = t->vn_nextref)
45d6a902
AM
2224 {
2225 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2226 continue;
2227
2228 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2229 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2230 return TRUE;
2231
2232 break;
2233 }
2234
2235 /* This is a new version. Add it to tree we are building. */
2236
2237 if (t == NULL)
2238 {
2239 amt = sizeof *t;
a50b1753 2240 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
2241 if (t == NULL)
2242 {
2243 rinfo->failed = TRUE;
2244 return FALSE;
2245 }
2246
2247 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
2248 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2249 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
2250 }
2251
2252 amt = sizeof *a;
a50b1753 2253 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
2254 if (a == NULL)
2255 {
2256 rinfo->failed = TRUE;
2257 return FALSE;
2258 }
45d6a902
AM
2259
2260 /* Note that we are copying a string pointer here, and testing it
2261 above. If bfd_elf_string_from_elf_section is ever changed to
2262 discard the string data when low in memory, this will have to be
2263 fixed. */
2264 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2265
2266 a->vna_flags = h->verinfo.verdef->vd_flags;
2267 a->vna_nextptr = t->vn_auxptr;
2268
2269 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2270 ++rinfo->vers;
2271
2272 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2273
2274 t->vn_auxptr = a;
2275
2276 return TRUE;
2277}
2278
099bb8fb
L
2279/* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2280 hidden. Set *T_P to NULL if there is no match. */
2281
2282static bfd_boolean
2283_bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2284 struct elf_link_hash_entry *h,
2285 const char *version_p,
2286 struct bfd_elf_version_tree **t_p,
2287 bfd_boolean *hide)
2288{
2289 struct bfd_elf_version_tree *t;
2290
2291 /* Look for the version. If we find it, it is no longer weak. */
2292 for (t = info->version_info; t != NULL; t = t->next)
2293 {
2294 if (strcmp (t->name, version_p) == 0)
2295 {
2296 size_t len;
2297 char *alc;
2298 struct bfd_elf_version_expr *d;
2299
2300 len = version_p - h->root.root.string;
2301 alc = (char *) bfd_malloc (len);
2302 if (alc == NULL)
2303 return FALSE;
2304 memcpy (alc, h->root.root.string, len - 1);
2305 alc[len - 1] = '\0';
2306 if (alc[len - 2] == ELF_VER_CHR)
2307 alc[len - 2] = '\0';
2308
2309 h->verinfo.vertree = t;
2310 t->used = TRUE;
2311 d = NULL;
2312
2313 if (t->globals.list != NULL)
2314 d = (*t->match) (&t->globals, NULL, alc);
2315
2316 /* See if there is anything to force this symbol to
2317 local scope. */
2318 if (d == NULL && t->locals.list != NULL)
2319 {
2320 d = (*t->match) (&t->locals, NULL, alc);
2321 if (d != NULL
2322 && h->dynindx != -1
2323 && ! info->export_dynamic)
2324 *hide = TRUE;
2325 }
2326
2327 free (alc);
2328 break;
2329 }
2330 }
2331
2332 *t_p = t;
2333
2334 return TRUE;
2335}
2336
2337/* Return TRUE if the symbol H is hidden by version script. */
2338
2339bfd_boolean
2340_bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2341 struct elf_link_hash_entry *h)
2342{
2343 const char *p;
2344 bfd_boolean hide = FALSE;
2345 const struct elf_backend_data *bed
2346 = get_elf_backend_data (info->output_bfd);
2347
2348 /* Version script only hides symbols defined in regular objects. */
2349 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2350 return TRUE;
2351
2352 p = strchr (h->root.root.string, ELF_VER_CHR);
2353 if (p != NULL && h->verinfo.vertree == NULL)
2354 {
2355 struct bfd_elf_version_tree *t;
2356
2357 ++p;
2358 if (*p == ELF_VER_CHR)
2359 ++p;
2360
2361 if (*p != '\0'
2362 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2363 && hide)
2364 {
2365 if (hide)
2366 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2367 return TRUE;
2368 }
2369 }
2370
2371 /* If we don't have a version for this symbol, see if we can find
2372 something. */
2373 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2374 {
2375 h->verinfo.vertree
2376 = bfd_find_version_for_sym (info->version_info,
2377 h->root.root.string, &hide);
2378 if (h->verinfo.vertree != NULL && hide)
2379 {
2380 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2381 return TRUE;
2382 }
2383 }
2384
2385 return FALSE;
2386}
2387
45d6a902
AM
2388/* Figure out appropriate versions for all the symbols. We may not
2389 have the version number script until we have read all of the input
2390 files, so until that point we don't know which symbols should be
2391 local. This function is called via elf_link_hash_traverse. */
2392
28caa186 2393static bfd_boolean
268b6b39 2394_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 2395{
28caa186 2396 struct elf_info_failed *sinfo;
45d6a902 2397 struct bfd_link_info *info;
9c5bfbb7 2398 const struct elf_backend_data *bed;
45d6a902
AM
2399 struct elf_info_failed eif;
2400 char *p;
099bb8fb 2401 bfd_boolean hide;
45d6a902 2402
a50b1753 2403 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
2404 info = sinfo->info;
2405
45d6a902
AM
2406 /* Fix the symbol flags. */
2407 eif.failed = FALSE;
2408 eif.info = info;
2409 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2410 {
2411 if (eif.failed)
2412 sinfo->failed = TRUE;
2413 return FALSE;
2414 }
2415
0a640d71
L
2416 bed = get_elf_backend_data (info->output_bfd);
2417
45d6a902
AM
2418 /* We only need version numbers for symbols defined in regular
2419 objects. */
5fa370e4 2420 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
0a640d71
L
2421 {
2422 /* Hide symbols defined in discarded input sections. */
2423 if ((h->root.type == bfd_link_hash_defined
2424 || h->root.type == bfd_link_hash_defweak)
2425 && discarded_section (h->root.u.def.section))
2426 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2427 return TRUE;
2428 }
45d6a902 2429
099bb8fb 2430 hide = FALSE;
45d6a902
AM
2431 p = strchr (h->root.root.string, ELF_VER_CHR);
2432 if (p != NULL && h->verinfo.vertree == NULL)
2433 {
2434 struct bfd_elf_version_tree *t;
45d6a902 2435
45d6a902
AM
2436 ++p;
2437 if (*p == ELF_VER_CHR)
6e33951e 2438 ++p;
45d6a902
AM
2439
2440 /* If there is no version string, we can just return out. */
2441 if (*p == '\0')
6e33951e 2442 return TRUE;
45d6a902 2443
099bb8fb 2444 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
45d6a902 2445 {
099bb8fb
L
2446 sinfo->failed = TRUE;
2447 return FALSE;
45d6a902
AM
2448 }
2449
099bb8fb
L
2450 if (hide)
2451 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2452
45d6a902
AM
2453 /* If we are building an application, we need to create a
2454 version node for this version. */
0e1862bb 2455 if (t == NULL && bfd_link_executable (info))
45d6a902
AM
2456 {
2457 struct bfd_elf_version_tree **pp;
2458 int version_index;
2459
2460 /* If we aren't going to export this symbol, we don't need
2461 to worry about it. */
2462 if (h->dynindx == -1)
2463 return TRUE;
2464
ef53be89
AM
2465 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2466 sizeof *t);
45d6a902
AM
2467 if (t == NULL)
2468 {
2469 sinfo->failed = TRUE;
2470 return FALSE;
2471 }
2472
45d6a902 2473 t->name = p;
45d6a902
AM
2474 t->name_indx = (unsigned int) -1;
2475 t->used = TRUE;
2476
2477 version_index = 1;
2478 /* Don't count anonymous version tag. */
fd91d419
L
2479 if (sinfo->info->version_info != NULL
2480 && sinfo->info->version_info->vernum == 0)
45d6a902 2481 version_index = 0;
fd91d419
L
2482 for (pp = &sinfo->info->version_info;
2483 *pp != NULL;
2484 pp = &(*pp)->next)
45d6a902
AM
2485 ++version_index;
2486 t->vernum = version_index;
2487
2488 *pp = t;
2489
2490 h->verinfo.vertree = t;
2491 }
2492 else if (t == NULL)
2493 {
2494 /* We could not find the version for a symbol when
2495 generating a shared archive. Return an error. */
4eca0228 2496 _bfd_error_handler
695344c0 2497 /* xgettext:c-format */
871b3ab2 2498 (_("%pB: version node not found for symbol %s"),
28caa186 2499 info->output_bfd, h->root.root.string);
45d6a902
AM
2500 bfd_set_error (bfd_error_bad_value);
2501 sinfo->failed = TRUE;
2502 return FALSE;
2503 }
45d6a902
AM
2504 }
2505
2506 /* If we don't have a version for this symbol, see if we can find
2507 something. */
099bb8fb
L
2508 if (!hide
2509 && h->verinfo.vertree == NULL
2510 && sinfo->info->version_info != NULL)
45d6a902 2511 {
fd91d419
L
2512 h->verinfo.vertree
2513 = bfd_find_version_for_sym (sinfo->info->version_info,
2514 h->root.root.string, &hide);
1e8fa21e
AM
2515 if (h->verinfo.vertree != NULL && hide)
2516 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2517 }
2518
2519 return TRUE;
2520}
2521\f
45d6a902
AM
2522/* Read and swap the relocs from the section indicated by SHDR. This
2523 may be either a REL or a RELA section. The relocations are
2524 translated into RELA relocations and stored in INTERNAL_RELOCS,
2525 which should have already been allocated to contain enough space.
2526 The EXTERNAL_RELOCS are a buffer where the external form of the
2527 relocations should be stored.
2528
2529 Returns FALSE if something goes wrong. */
2530
2531static bfd_boolean
268b6b39 2532elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2533 asection *sec,
268b6b39
AM
2534 Elf_Internal_Shdr *shdr,
2535 void *external_relocs,
2536 Elf_Internal_Rela *internal_relocs)
45d6a902 2537{
9c5bfbb7 2538 const struct elf_backend_data *bed;
268b6b39 2539 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2540 const bfd_byte *erela;
2541 const bfd_byte *erelaend;
2542 Elf_Internal_Rela *irela;
243ef1e0
L
2543 Elf_Internal_Shdr *symtab_hdr;
2544 size_t nsyms;
45d6a902 2545
45d6a902
AM
2546 /* Position ourselves at the start of the section. */
2547 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2548 return FALSE;
2549
2550 /* Read the relocations. */
2551 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2552 return FALSE;
2553
243ef1e0 2554 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2555 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2556
45d6a902
AM
2557 bed = get_elf_backend_data (abfd);
2558
2559 /* Convert the external relocations to the internal format. */
2560 if (shdr->sh_entsize == bed->s->sizeof_rel)
2561 swap_in = bed->s->swap_reloc_in;
2562 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2563 swap_in = bed->s->swap_reloca_in;
2564 else
2565 {
2566 bfd_set_error (bfd_error_wrong_format);
2567 return FALSE;
2568 }
2569
a50b1753 2570 erela = (const bfd_byte *) external_relocs;
f55b1e32
AM
2571 /* Setting erelaend like this and comparing with <= handles case of
2572 a fuzzed object with sh_size not a multiple of sh_entsize. */
2573 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
45d6a902 2574 irela = internal_relocs;
f55b1e32 2575 while (erela <= erelaend)
45d6a902 2576 {
243ef1e0
L
2577 bfd_vma r_symndx;
2578
45d6a902 2579 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2580 r_symndx = ELF32_R_SYM (irela->r_info);
2581 if (bed->s->arch_size == 64)
2582 r_symndx >>= 24;
ce98a316
NC
2583 if (nsyms > 0)
2584 {
2585 if ((size_t) r_symndx >= nsyms)
2586 {
4eca0228 2587 _bfd_error_handler
695344c0 2588 /* xgettext:c-format */
2dcf00ce
AM
2589 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2590 " for offset %#" PRIx64 " in section `%pA'"),
2591 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2592 (uint64_t) irela->r_offset, sec);
ce98a316
NC
2593 bfd_set_error (bfd_error_bad_value);
2594 return FALSE;
2595 }
2596 }
cf35638d 2597 else if (r_symndx != STN_UNDEF)
243ef1e0 2598 {
4eca0228 2599 _bfd_error_handler
695344c0 2600 /* xgettext:c-format */
2dcf00ce
AM
2601 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2602 " for offset %#" PRIx64 " in section `%pA'"
ce98a316 2603 " when the object file has no symbol table"),
2dcf00ce
AM
2604 abfd, (uint64_t) r_symndx,
2605 (uint64_t) irela->r_offset, sec);
243ef1e0
L
2606 bfd_set_error (bfd_error_bad_value);
2607 return FALSE;
2608 }
45d6a902
AM
2609 irela += bed->s->int_rels_per_ext_rel;
2610 erela += shdr->sh_entsize;
2611 }
2612
2613 return TRUE;
2614}
2615
2616/* Read and swap the relocs for a section O. They may have been
2617 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2618 not NULL, they are used as buffers to read into. They are known to
2619 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2620 the return value is allocated using either malloc or bfd_alloc,
2621 according to the KEEP_MEMORY argument. If O has two relocation
2622 sections (both REL and RELA relocations), then the REL_HDR
2623 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2624 RELA_HDR relocations. */
45d6a902
AM
2625
2626Elf_Internal_Rela *
268b6b39
AM
2627_bfd_elf_link_read_relocs (bfd *abfd,
2628 asection *o,
2629 void *external_relocs,
2630 Elf_Internal_Rela *internal_relocs,
2631 bfd_boolean keep_memory)
45d6a902 2632{
268b6b39 2633 void *alloc1 = NULL;
45d6a902 2634 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2635 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2636 struct bfd_elf_section_data *esdo = elf_section_data (o);
2637 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2638
d4730f92
BS
2639 if (esdo->relocs != NULL)
2640 return esdo->relocs;
45d6a902
AM
2641
2642 if (o->reloc_count == 0)
2643 return NULL;
2644
45d6a902
AM
2645 if (internal_relocs == NULL)
2646 {
2647 bfd_size_type size;
2648
056bafd4 2649 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
45d6a902 2650 if (keep_memory)
a50b1753 2651 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2652 else
a50b1753 2653 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2654 if (internal_relocs == NULL)
2655 goto error_return;
2656 }
2657
2658 if (external_relocs == NULL)
2659 {
d4730f92
BS
2660 bfd_size_type size = 0;
2661
2662 if (esdo->rel.hdr)
2663 size += esdo->rel.hdr->sh_size;
2664 if (esdo->rela.hdr)
2665 size += esdo->rela.hdr->sh_size;
45d6a902 2666
268b6b39 2667 alloc1 = bfd_malloc (size);
45d6a902
AM
2668 if (alloc1 == NULL)
2669 goto error_return;
2670 external_relocs = alloc1;
2671 }
2672
d4730f92
BS
2673 internal_rela_relocs = internal_relocs;
2674 if (esdo->rel.hdr)
2675 {
2676 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2677 external_relocs,
2678 internal_relocs))
2679 goto error_return;
2680 external_relocs = (((bfd_byte *) external_relocs)
2681 + esdo->rel.hdr->sh_size);
2682 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2683 * bed->s->int_rels_per_ext_rel);
2684 }
2685
2686 if (esdo->rela.hdr
2687 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2688 external_relocs,
2689 internal_rela_relocs)))
45d6a902
AM
2690 goto error_return;
2691
2692 /* Cache the results for next time, if we can. */
2693 if (keep_memory)
d4730f92 2694 esdo->relocs = internal_relocs;
45d6a902 2695
c9594989 2696 free (alloc1);
45d6a902
AM
2697
2698 /* Don't free alloc2, since if it was allocated we are passing it
2699 back (under the name of internal_relocs). */
2700
2701 return internal_relocs;
2702
2703 error_return:
c9594989 2704 free (alloc1);
45d6a902 2705 if (alloc2 != NULL)
4dd07732
AM
2706 {
2707 if (keep_memory)
2708 bfd_release (abfd, alloc2);
2709 else
2710 free (alloc2);
2711 }
45d6a902
AM
2712 return NULL;
2713}
2714
2715/* Compute the size of, and allocate space for, REL_HDR which is the
2716 section header for a section containing relocations for O. */
2717
28caa186 2718static bfd_boolean
9eaff861
AO
2719_bfd_elf_link_size_reloc_section (bfd *abfd,
2720 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2721{
9eaff861 2722 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2723
2724 /* That allows us to calculate the size of the section. */
9eaff861 2725 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2726
2727 /* The contents field must last into write_object_contents, so we
2728 allocate it with bfd_alloc rather than malloc. Also since we
2729 cannot be sure that the contents will actually be filled in,
2730 we zero the allocated space. */
a50b1753 2731 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2732 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2733 return FALSE;
2734
d4730f92 2735 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2736 {
2737 struct elf_link_hash_entry **p;
2738
ca4be51c
AM
2739 p = ((struct elf_link_hash_entry **)
2740 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902
AM
2741 if (p == NULL)
2742 return FALSE;
2743
d4730f92 2744 reldata->hashes = p;
45d6a902
AM
2745 }
2746
2747 return TRUE;
2748}
2749
2750/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2751 originated from the section given by INPUT_REL_HDR) to the
2752 OUTPUT_BFD. */
2753
2754bfd_boolean
268b6b39
AM
2755_bfd_elf_link_output_relocs (bfd *output_bfd,
2756 asection *input_section,
2757 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2758 Elf_Internal_Rela *internal_relocs,
2759 struct elf_link_hash_entry **rel_hash
2760 ATTRIBUTE_UNUSED)
45d6a902
AM
2761{
2762 Elf_Internal_Rela *irela;
2763 Elf_Internal_Rela *irelaend;
2764 bfd_byte *erel;
d4730f92 2765 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2766 asection *output_section;
9c5bfbb7 2767 const struct elf_backend_data *bed;
268b6b39 2768 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2769 struct bfd_elf_section_data *esdo;
45d6a902
AM
2770
2771 output_section = input_section->output_section;
45d6a902 2772
d4730f92
BS
2773 bed = get_elf_backend_data (output_bfd);
2774 esdo = elf_section_data (output_section);
2775 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2776 {
d4730f92
BS
2777 output_reldata = &esdo->rel;
2778 swap_out = bed->s->swap_reloc_out;
45d6a902 2779 }
d4730f92
BS
2780 else if (esdo->rela.hdr
2781 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2782 {
d4730f92
BS
2783 output_reldata = &esdo->rela;
2784 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2785 }
2786 else
2787 {
4eca0228 2788 _bfd_error_handler
695344c0 2789 /* xgettext:c-format */
871b3ab2 2790 (_("%pB: relocation size mismatch in %pB section %pA"),
d003868e 2791 output_bfd, input_section->owner, input_section);
297d8443 2792 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2793 return FALSE;
2794 }
2795
d4730f92
BS
2796 erel = output_reldata->hdr->contents;
2797 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2798 irela = internal_relocs;
2799 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2800 * bed->s->int_rels_per_ext_rel);
2801 while (irela < irelaend)
2802 {
2803 (*swap_out) (output_bfd, irela, erel);
2804 irela += bed->s->int_rels_per_ext_rel;
2805 erel += input_rel_hdr->sh_entsize;
2806 }
2807
2808 /* Bump the counter, so that we know where to add the next set of
2809 relocations. */
d4730f92 2810 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2811
2812 return TRUE;
2813}
2814\f
508c3946
L
2815/* Make weak undefined symbols in PIE dynamic. */
2816
2817bfd_boolean
2818_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2819 struct elf_link_hash_entry *h)
2820{
0e1862bb 2821 if (bfd_link_pie (info)
508c3946
L
2822 && h->dynindx == -1
2823 && h->root.type == bfd_link_hash_undefweak)
2824 return bfd_elf_link_record_dynamic_symbol (info, h);
2825
2826 return TRUE;
2827}
2828
45d6a902
AM
2829/* Fix up the flags for a symbol. This handles various cases which
2830 can only be fixed after all the input files are seen. This is
2831 currently called by both adjust_dynamic_symbol and
2832 assign_sym_version, which is unnecessary but perhaps more robust in
2833 the face of future changes. */
2834
28caa186 2835static bfd_boolean
268b6b39
AM
2836_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2837 struct elf_info_failed *eif)
45d6a902 2838{
33774f08 2839 const struct elf_backend_data *bed;
508c3946 2840
45d6a902
AM
2841 /* If this symbol was mentioned in a non-ELF file, try to set
2842 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2843 permit a non-ELF file to correctly refer to a symbol defined in
2844 an ELF dynamic object. */
f5385ebf 2845 if (h->non_elf)
45d6a902
AM
2846 {
2847 while (h->root.type == bfd_link_hash_indirect)
2848 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2849
2850 if (h->root.type != bfd_link_hash_defined
2851 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2852 {
2853 h->ref_regular = 1;
2854 h->ref_regular_nonweak = 1;
2855 }
45d6a902
AM
2856 else
2857 {
2858 if (h->root.u.def.section->owner != NULL
2859 && (bfd_get_flavour (h->root.u.def.section->owner)
2860 == bfd_target_elf_flavour))
f5385ebf
AM
2861 {
2862 h->ref_regular = 1;
2863 h->ref_regular_nonweak = 1;
2864 }
45d6a902 2865 else
f5385ebf 2866 h->def_regular = 1;
45d6a902
AM
2867 }
2868
2869 if (h->dynindx == -1
f5385ebf
AM
2870 && (h->def_dynamic
2871 || h->ref_dynamic))
45d6a902 2872 {
c152c796 2873 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2874 {
2875 eif->failed = TRUE;
2876 return FALSE;
2877 }
2878 }
2879 }
2880 else
2881 {
f5385ebf 2882 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2883 was first seen in a non-ELF file. Fortunately, if the symbol
2884 was first seen in an ELF file, we're probably OK unless the
2885 symbol was defined in a non-ELF file. Catch that case here.
2886 FIXME: We're still in trouble if the symbol was first seen in
2887 a dynamic object, and then later in a non-ELF regular object. */
2888 if ((h->root.type == bfd_link_hash_defined
2889 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2890 && !h->def_regular
45d6a902
AM
2891 && (h->root.u.def.section->owner != NULL
2892 ? (bfd_get_flavour (h->root.u.def.section->owner)
2893 != bfd_target_elf_flavour)
2894 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2895 && !h->def_dynamic)))
2896 h->def_regular = 1;
45d6a902
AM
2897 }
2898
508c3946 2899 /* Backend specific symbol fixup. */
33774f08
AM
2900 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2901 if (bed->elf_backend_fixup_symbol
2902 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2903 return FALSE;
508c3946 2904
45d6a902
AM
2905 /* If this is a final link, and the symbol was defined as a common
2906 symbol in a regular object file, and there was no definition in
2907 any dynamic object, then the linker will have allocated space for
f5385ebf 2908 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2909 flag will not have been set. */
2910 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2911 && !h->def_regular
2912 && h->ref_regular
2913 && !h->def_dynamic
96f29d96 2914 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2915 h->def_regular = 1;
45d6a902 2916
af0bfb9c
AM
2917 /* Symbols defined in discarded sections shouldn't be dynamic. */
2918 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
2919 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2920
4deb8f71
L
2921 /* If a weak undefined symbol has non-default visibility, we also
2922 hide it from the dynamic linker. */
af0bfb9c
AM
2923 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2924 && h->root.type == bfd_link_hash_undefweak)
4deb8f71
L
2925 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2926
2927 /* A hidden versioned symbol in executable should be forced local if
2928 it is is locally defined, not referenced by shared library and not
2929 exported. */
2930 else if (bfd_link_executable (eif->info)
2931 && h->versioned == versioned_hidden
2932 && !eif->info->export_dynamic
2933 && !h->dynamic
2934 && !h->ref_dynamic
2935 && h->def_regular)
2936 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2937
45d6a902
AM
2938 /* If -Bsymbolic was used (which means to bind references to global
2939 symbols to the definition within the shared object), and this
2940 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2941 need a PLT entry. Likewise, if the symbol has non-default
2942 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2943 will force it local. */
4deb8f71
L
2944 else if (h->needs_plt
2945 && bfd_link_pic (eif->info)
2946 && is_elf_hash_table (eif->info->hash)
2947 && (SYMBOLIC_BIND (eif->info, h)
2948 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2949 && h->def_regular)
45d6a902 2950 {
45d6a902
AM
2951 bfd_boolean force_local;
2952
45d6a902
AM
2953 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2954 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2955 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2956 }
2957
45d6a902
AM
2958 /* If this is a weak defined symbol in a dynamic object, and we know
2959 the real definition in the dynamic object, copy interesting flags
2960 over to the real definition. */
60d67dc8 2961 if (h->is_weakalias)
45d6a902 2962 {
60d67dc8
AM
2963 struct elf_link_hash_entry *def = weakdef (h);
2964
45d6a902
AM
2965 /* If the real definition is defined by a regular object file,
2966 don't do anything special. See the longer description in
5b9d7a9a
AM
2967 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
2968 bfd_link_hash_defined as it was when put on the alias list
2969 then it must have originally been a versioned symbol (for
2970 which a non-versioned indirect symbol is created) and later
2971 a definition for the non-versioned symbol is found. In that
2972 case the indirection is flipped with the versioned symbol
2973 becoming an indirect pointing at the non-versioned symbol.
2974 Thus, not an alias any more. */
2975 if (def->def_regular
2976 || def->root.type != bfd_link_hash_defined)
60d67dc8
AM
2977 {
2978 h = def;
2979 while ((h = h->u.alias) != def)
2980 h->is_weakalias = 0;
2981 }
45d6a902 2982 else
a26587ba 2983 {
4e6b54a6
AM
2984 while (h->root.type == bfd_link_hash_indirect)
2985 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4e6b54a6
AM
2986 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2987 || h->root.type == bfd_link_hash_defweak);
60d67dc8 2988 BFD_ASSERT (def->def_dynamic);
60d67dc8 2989 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
a26587ba 2990 }
45d6a902
AM
2991 }
2992
2993 return TRUE;
2994}
2995
2996/* Make the backend pick a good value for a dynamic symbol. This is
2997 called via elf_link_hash_traverse, and also calls itself
2998 recursively. */
2999
28caa186 3000static bfd_boolean
268b6b39 3001_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 3002{
a50b1753 3003 struct elf_info_failed *eif = (struct elf_info_failed *) data;
559192d8 3004 struct elf_link_hash_table *htab;
9c5bfbb7 3005 const struct elf_backend_data *bed;
45d6a902 3006
0eddce27 3007 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
3008 return FALSE;
3009
45d6a902
AM
3010 /* Ignore indirect symbols. These are added by the versioning code. */
3011 if (h->root.type == bfd_link_hash_indirect)
3012 return TRUE;
3013
3014 /* Fix the symbol flags. */
3015 if (! _bfd_elf_fix_symbol_flags (h, eif))
3016 return FALSE;
3017
559192d8
AM
3018 htab = elf_hash_table (eif->info);
3019 bed = get_elf_backend_data (htab->dynobj);
3020
954b63d4
AM
3021 if (h->root.type == bfd_link_hash_undefweak)
3022 {
3023 if (eif->info->dynamic_undefined_weak == 0)
559192d8 3024 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
954b63d4
AM
3025 else if (eif->info->dynamic_undefined_weak > 0
3026 && h->ref_regular
3027 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3028 && !bfd_hide_sym_by_version (eif->info->version_info,
3029 h->root.root.string))
3030 {
3031 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
3032 {
3033 eif->failed = TRUE;
3034 return FALSE;
3035 }
3036 }
3037 }
3038
45d6a902
AM
3039 /* If this symbol does not require a PLT entry, and it is not
3040 defined by a dynamic object, or is not referenced by a regular
3041 object, ignore it. We do have to handle a weak defined symbol,
3042 even if no regular object refers to it, if we decided to add it
3043 to the dynamic symbol table. FIXME: Do we normally need to worry
3044 about symbols which are defined by one dynamic object and
3045 referenced by another one? */
f5385ebf 3046 if (!h->needs_plt
91e21fb7 3047 && h->type != STT_GNU_IFUNC
f5385ebf
AM
3048 && (h->def_regular
3049 || !h->def_dynamic
3050 || (!h->ref_regular
60d67dc8 3051 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
45d6a902 3052 {
a6aa5195 3053 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
3054 return TRUE;
3055 }
3056
3057 /* If we've already adjusted this symbol, don't do it again. This
3058 can happen via a recursive call. */
f5385ebf 3059 if (h->dynamic_adjusted)
45d6a902
AM
3060 return TRUE;
3061
3062 /* Don't look at this symbol again. Note that we must set this
3063 after checking the above conditions, because we may look at a
3064 symbol once, decide not to do anything, and then get called
3065 recursively later after REF_REGULAR is set below. */
f5385ebf 3066 h->dynamic_adjusted = 1;
45d6a902
AM
3067
3068 /* If this is a weak definition, and we know a real definition, and
3069 the real symbol is not itself defined by a regular object file,
3070 then get a good value for the real definition. We handle the
3071 real symbol first, for the convenience of the backend routine.
3072
3073 Note that there is a confusing case here. If the real definition
3074 is defined by a regular object file, we don't get the real symbol
3075 from the dynamic object, but we do get the weak symbol. If the
3076 processor backend uses a COPY reloc, then if some routine in the
3077 dynamic object changes the real symbol, we will not see that
3078 change in the corresponding weak symbol. This is the way other
3079 ELF linkers work as well, and seems to be a result of the shared
3080 library model.
3081
3082 I will clarify this issue. Most SVR4 shared libraries define the
3083 variable _timezone and define timezone as a weak synonym. The
3084 tzset call changes _timezone. If you write
3085 extern int timezone;
3086 int _timezone = 5;
3087 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3088 you might expect that, since timezone is a synonym for _timezone,
3089 the same number will print both times. However, if the processor
3090 backend uses a COPY reloc, then actually timezone will be copied
3091 into your process image, and, since you define _timezone
3092 yourself, _timezone will not. Thus timezone and _timezone will
3093 wind up at different memory locations. The tzset call will set
3094 _timezone, leaving timezone unchanged. */
3095
60d67dc8 3096 if (h->is_weakalias)
45d6a902 3097 {
60d67dc8
AM
3098 struct elf_link_hash_entry *def = weakdef (h);
3099
ec24dc88 3100 /* If we get to this point, there is an implicit reference to
60d67dc8
AM
3101 the alias by a regular object file via the weak symbol H. */
3102 def->ref_regular = 1;
45d6a902 3103
ec24dc88 3104 /* Ensure that the backend adjust_dynamic_symbol function sees
60d67dc8
AM
3105 the strong alias before H by recursively calling ourselves. */
3106 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
45d6a902
AM
3107 return FALSE;
3108 }
3109
3110 /* If a symbol has no type and no size and does not require a PLT
3111 entry, then we are probably about to do the wrong thing here: we
3112 are probably going to create a COPY reloc for an empty object.
3113 This case can arise when a shared object is built with assembly
3114 code, and the assembly code fails to set the symbol type. */
3115 if (h->size == 0
3116 && h->type == STT_NOTYPE
f5385ebf 3117 && !h->needs_plt)
4eca0228 3118 _bfd_error_handler
45d6a902
AM
3119 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3120 h->root.root.string);
3121
45d6a902
AM
3122 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3123 {
3124 eif->failed = TRUE;
3125 return FALSE;
3126 }
3127
3128 return TRUE;
3129}
3130
027297b7
L
3131/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3132 DYNBSS. */
3133
3134bfd_boolean
6cabe1ea
AM
3135_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3136 struct elf_link_hash_entry *h,
027297b7
L
3137 asection *dynbss)
3138{
91ac5911 3139 unsigned int power_of_two;
027297b7
L
3140 bfd_vma mask;
3141 asection *sec = h->root.u.def.section;
3142
de194d85 3143 /* The section alignment of the definition is the maximum alignment
91ac5911
L
3144 requirement of symbols defined in the section. Since we don't
3145 know the symbol alignment requirement, we start with the
3146 maximum alignment and check low bits of the symbol address
3147 for the minimum alignment. */
fd361982 3148 power_of_two = bfd_section_alignment (sec);
91ac5911
L
3149 mask = ((bfd_vma) 1 << power_of_two) - 1;
3150 while ((h->root.u.def.value & mask) != 0)
3151 {
3152 mask >>= 1;
3153 --power_of_two;
3154 }
027297b7 3155
fd361982 3156 if (power_of_two > bfd_section_alignment (dynbss))
027297b7
L
3157 {
3158 /* Adjust the section alignment if needed. */
fd361982 3159 if (!bfd_set_section_alignment (dynbss, power_of_two))
027297b7
L
3160 return FALSE;
3161 }
3162
91ac5911 3163 /* We make sure that the symbol will be aligned properly. */
027297b7
L
3164 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3165
3166 /* Define the symbol as being at this point in DYNBSS. */
3167 h->root.u.def.section = dynbss;
3168 h->root.u.def.value = dynbss->size;
3169
3170 /* Increment the size of DYNBSS to make room for the symbol. */
3171 dynbss->size += h->size;
3172
f7483970
L
3173 /* No error if extern_protected_data is true. */
3174 if (h->protected_def
889c2a67
L
3175 && (!info->extern_protected_data
3176 || (info->extern_protected_data < 0
3177 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05 3178 info->callbacks->einfo
c1c8c1ef 3179 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
d07a1b05 3180 h->root.root.string);
6cabe1ea 3181
027297b7
L
3182 return TRUE;
3183}
3184
45d6a902
AM
3185/* Adjust all external symbols pointing into SEC_MERGE sections
3186 to reflect the object merging within the sections. */
3187
28caa186 3188static bfd_boolean
268b6b39 3189_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
3190{
3191 asection *sec;
3192
45d6a902
AM
3193 if ((h->root.type == bfd_link_hash_defined
3194 || h->root.type == bfd_link_hash_defweak)
3195 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 3196 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 3197 {
a50b1753 3198 bfd *output_bfd = (bfd *) data;
45d6a902
AM
3199
3200 h->root.u.def.value =
3201 _bfd_merged_section_offset (output_bfd,
3202 &h->root.u.def.section,
3203 elf_section_data (sec)->sec_info,
753731ee 3204 h->root.u.def.value);
45d6a902
AM
3205 }
3206
3207 return TRUE;
3208}
986a241f
RH
3209
3210/* Returns false if the symbol referred to by H should be considered
3211 to resolve local to the current module, and true if it should be
3212 considered to bind dynamically. */
3213
3214bfd_boolean
268b6b39
AM
3215_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3216 struct bfd_link_info *info,
89a2ee5a 3217 bfd_boolean not_local_protected)
986a241f
RH
3218{
3219 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
3220 const struct elf_backend_data *bed;
3221 struct elf_link_hash_table *hash_table;
986a241f
RH
3222
3223 if (h == NULL)
3224 return FALSE;
3225
3226 while (h->root.type == bfd_link_hash_indirect
3227 || h->root.type == bfd_link_hash_warning)
3228 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3229
3230 /* If it was forced local, then clearly it's not dynamic. */
3231 if (h->dynindx == -1)
3232 return FALSE;
f5385ebf 3233 if (h->forced_local)
986a241f
RH
3234 return FALSE;
3235
3236 /* Identify the cases where name binding rules say that a
3237 visible symbol resolves locally. */
0e1862bb
L
3238 binding_stays_local_p = (bfd_link_executable (info)
3239 || SYMBOLIC_BIND (info, h));
986a241f
RH
3240
3241 switch (ELF_ST_VISIBILITY (h->other))
3242 {
3243 case STV_INTERNAL:
3244 case STV_HIDDEN:
3245 return FALSE;
3246
3247 case STV_PROTECTED:
fcb93ecf
PB
3248 hash_table = elf_hash_table (info);
3249 if (!is_elf_hash_table (hash_table))
3250 return FALSE;
3251
3252 bed = get_elf_backend_data (hash_table->dynobj);
3253
986a241f
RH
3254 /* Proper resolution for function pointer equality may require
3255 that these symbols perhaps be resolved dynamically, even though
3256 we should be resolving them to the current module. */
89a2ee5a 3257 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
3258 binding_stays_local_p = TRUE;
3259 break;
3260
3261 default:
986a241f
RH
3262 break;
3263 }
3264
aa37626c 3265 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 3266 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
3267 return TRUE;
3268
986a241f
RH
3269 /* Otherwise, the symbol is dynamic if binding rules don't tell
3270 us that it remains local. */
3271 return !binding_stays_local_p;
3272}
f6c52c13
AM
3273
3274/* Return true if the symbol referred to by H should be considered
3275 to resolve local to the current module, and false otherwise. Differs
3276 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 3277 undefined symbols. The two functions are virtually identical except
0fad2956
MR
3278 for the place where dynindx == -1 is tested. If that test is true,
3279 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3280 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3281 defined symbols.
89a2ee5a
AM
3282 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3283 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3284 treatment of undefined weak symbols. For those that do not make
3285 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
3286
3287bfd_boolean
268b6b39
AM
3288_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3289 struct bfd_link_info *info,
3290 bfd_boolean local_protected)
f6c52c13 3291{
fcb93ecf
PB
3292 const struct elf_backend_data *bed;
3293 struct elf_link_hash_table *hash_table;
3294
f6c52c13
AM
3295 /* If it's a local sym, of course we resolve locally. */
3296 if (h == NULL)
3297 return TRUE;
3298
d95edcac
L
3299 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3300 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3301 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3302 return TRUE;
3303
0fad2956
MR
3304 /* Forced local symbols resolve locally. */
3305 if (h->forced_local)
3306 return TRUE;
3307
7e2294f9
AO
3308 /* Common symbols that become definitions don't get the DEF_REGULAR
3309 flag set, so test it first, and don't bail out. */
3310 if (ELF_COMMON_DEF_P (h))
3311 /* Do nothing. */;
f6c52c13 3312 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
3313 resolve locally. The sym is either undefined or dynamic. */
3314 else if (!h->def_regular)
f6c52c13
AM
3315 return FALSE;
3316
0fad2956 3317 /* Non-dynamic symbols resolve locally. */
f6c52c13
AM
3318 if (h->dynindx == -1)
3319 return TRUE;
3320
3321 /* At this point, we know the symbol is defined and dynamic. In an
3322 executable it must resolve locally, likewise when building symbolic
3323 shared libraries. */
0e1862bb 3324 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
f6c52c13
AM
3325 return TRUE;
3326
3327 /* Now deal with defined dynamic symbols in shared libraries. Ones
3328 with default visibility might not resolve locally. */
3329 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3330 return FALSE;
3331
fcb93ecf
PB
3332 hash_table = elf_hash_table (info);
3333 if (!is_elf_hash_table (hash_table))
3334 return TRUE;
3335
3336 bed = get_elf_backend_data (hash_table->dynobj);
3337
f7483970
L
3338 /* If extern_protected_data is false, STV_PROTECTED non-function
3339 symbols are local. */
889c2a67
L
3340 if ((!info->extern_protected_data
3341 || (info->extern_protected_data < 0
3342 && !bed->extern_protected_data))
3343 && !bed->is_function_type (h->type))
1c16dfa5
L
3344 return TRUE;
3345
f6c52c13 3346 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
3347 symbols be treated as dynamic symbols. If the address of a
3348 function not defined in an executable is set to that function's
3349 plt entry in the executable, then the address of the function in
3350 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
3351 return local_protected;
3352}
e1918d23
AM
3353
3354/* Caches some TLS segment info, and ensures that the TLS segment vma is
3355 aligned. Returns the first TLS output section. */
3356
3357struct bfd_section *
3358_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3359{
3360 struct bfd_section *sec, *tls;
3361 unsigned int align = 0;
3362
3363 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3364 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3365 break;
3366 tls = sec;
3367
3368 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3369 if (sec->alignment_power > align)
3370 align = sec->alignment_power;
3371
3372 elf_hash_table (info)->tls_sec = tls;
3373
fdde2fb6
SH
3374 /* Ensure the alignment of the first section (usually .tdata) is the largest
3375 alignment, so that the tls segment starts aligned. */
e1918d23
AM
3376 if (tls != NULL)
3377 tls->alignment_power = align;
3378
3379 return tls;
3380}
0ad989f9
L
3381
3382/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3383static bfd_boolean
3384is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3385 Elf_Internal_Sym *sym)
3386{
a4d8e49b
L
3387 const struct elf_backend_data *bed;
3388
0ad989f9
L
3389 /* Local symbols do not count, but target specific ones might. */
3390 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3391 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3392 return FALSE;
3393
fcb93ecf 3394 bed = get_elf_backend_data (abfd);
0ad989f9 3395 /* Function symbols do not count. */
fcb93ecf 3396 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
3397 return FALSE;
3398
3399 /* If the section is undefined, then so is the symbol. */
3400 if (sym->st_shndx == SHN_UNDEF)
3401 return FALSE;
3402
3403 /* If the symbol is defined in the common section, then
3404 it is a common definition and so does not count. */
a4d8e49b 3405 if (bed->common_definition (sym))
0ad989f9
L
3406 return FALSE;
3407
3408 /* If the symbol is in a target specific section then we
3409 must rely upon the backend to tell us what it is. */
3410 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3411 /* FIXME - this function is not coded yet:
3412
3413 return _bfd_is_global_symbol_definition (abfd, sym);
3414
3415 Instead for now assume that the definition is not global,
3416 Even if this is wrong, at least the linker will behave
3417 in the same way that it used to do. */
3418 return FALSE;
3419
3420 return TRUE;
3421}
3422
3423/* Search the symbol table of the archive element of the archive ABFD
3424 whose archive map contains a mention of SYMDEF, and determine if
3425 the symbol is defined in this element. */
3426static bfd_boolean
3427elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3428{
3429 Elf_Internal_Shdr * hdr;
ef53be89
AM
3430 size_t symcount;
3431 size_t extsymcount;
3432 size_t extsymoff;
0ad989f9
L
3433 Elf_Internal_Sym *isymbuf;
3434 Elf_Internal_Sym *isym;
3435 Elf_Internal_Sym *isymend;
3436 bfd_boolean result;
3437
3438 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3439 if (abfd == NULL)
3440 return FALSE;
3441
3442 if (! bfd_check_format (abfd, bfd_object))
3443 return FALSE;
3444
7dc3990e
L
3445 /* Select the appropriate symbol table. If we don't know if the
3446 object file is an IR object, give linker LTO plugin a chance to
3447 get the correct symbol table. */
3448 if (abfd->plugin_format == bfd_plugin_yes
08ce1d72 3449#if BFD_SUPPORTS_PLUGINS
7dc3990e
L
3450 || (abfd->plugin_format == bfd_plugin_unknown
3451 && bfd_link_plugin_object_p (abfd))
3452#endif
3453 )
3454 {
3455 /* Use the IR symbol table if the object has been claimed by
3456 plugin. */
3457 abfd = abfd->plugin_dummy_bfd;
3458 hdr = &elf_tdata (abfd)->symtab_hdr;
3459 }
3460 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
0ad989f9
L
3461 hdr = &elf_tdata (abfd)->symtab_hdr;
3462 else
3463 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3464
3465 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3466
3467 /* The sh_info field of the symtab header tells us where the
3468 external symbols start. We don't care about the local symbols. */
3469 if (elf_bad_symtab (abfd))
3470 {
3471 extsymcount = symcount;
3472 extsymoff = 0;
3473 }
3474 else
3475 {
3476 extsymcount = symcount - hdr->sh_info;
3477 extsymoff = hdr->sh_info;
3478 }
3479
3480 if (extsymcount == 0)
3481 return FALSE;
3482
3483 /* Read in the symbol table. */
3484 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3485 NULL, NULL, NULL);
3486 if (isymbuf == NULL)
3487 return FALSE;
3488
3489 /* Scan the symbol table looking for SYMDEF. */
3490 result = FALSE;
3491 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3492 {
3493 const char *name;
3494
3495 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3496 isym->st_name);
3497 if (name == NULL)
3498 break;
3499
3500 if (strcmp (name, symdef->name) == 0)
3501 {
3502 result = is_global_data_symbol_definition (abfd, isym);
3503 break;
3504 }
3505 }
3506
3507 free (isymbuf);
3508
3509 return result;
3510}
3511\f
5a580b3a
AM
3512/* Add an entry to the .dynamic table. */
3513
3514bfd_boolean
3515_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3516 bfd_vma tag,
3517 bfd_vma val)
3518{
3519 struct elf_link_hash_table *hash_table;
3520 const struct elf_backend_data *bed;
3521 asection *s;
3522 bfd_size_type newsize;
3523 bfd_byte *newcontents;
3524 Elf_Internal_Dyn dyn;
3525
3526 hash_table = elf_hash_table (info);
3527 if (! is_elf_hash_table (hash_table))
3528 return FALSE;
3529
7f923b7f
AM
3530 if (tag == DT_RELA || tag == DT_REL)
3531 hash_table->dynamic_relocs = TRUE;
3532
5a580b3a 3533 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3534 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3535 BFD_ASSERT (s != NULL);
3536
eea6121a 3537 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3538 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3539 if (newcontents == NULL)
3540 return FALSE;
3541
3542 dyn.d_tag = tag;
3543 dyn.d_un.d_val = val;
eea6121a 3544 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3545
eea6121a 3546 s->size = newsize;
5a580b3a
AM
3547 s->contents = newcontents;
3548
3549 return TRUE;
3550}
3551
6f6fd151
L
3552/* Strip zero-sized dynamic sections. */
3553
3554bfd_boolean
3555_bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3556{
3557 struct elf_link_hash_table *hash_table;
3558 const struct elf_backend_data *bed;
3559 asection *s, *sdynamic, **pp;
3560 asection *rela_dyn, *rel_dyn;
3561 Elf_Internal_Dyn dyn;
3562 bfd_byte *extdyn, *next;
3563 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3564 bfd_boolean strip_zero_sized;
3565 bfd_boolean strip_zero_sized_plt;
3566
3567 if (bfd_link_relocatable (info))
3568 return TRUE;
3569
3570 hash_table = elf_hash_table (info);
3571 if (!is_elf_hash_table (hash_table))
3572 return FALSE;
3573
3574 if (!hash_table->dynobj)
3575 return TRUE;
3576
3577 sdynamic= bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3578 if (!sdynamic)
3579 return TRUE;
3580
3581 bed = get_elf_backend_data (hash_table->dynobj);
3582 swap_dyn_in = bed->s->swap_dyn_in;
3583
3584 strip_zero_sized = FALSE;
3585 strip_zero_sized_plt = FALSE;
3586
3587 /* Strip zero-sized dynamic sections. */
3588 rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3589 rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3590 for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3591 if (s->size == 0
3592 && (s == rela_dyn
3593 || s == rel_dyn
3594 || s == hash_table->srelplt->output_section
3595 || s == hash_table->splt->output_section))
3596 {
3597 *pp = s->next;
3598 info->output_bfd->section_count--;
3599 strip_zero_sized = TRUE;
3600 if (s == rela_dyn)
3601 s = rela_dyn;
3602 if (s == rel_dyn)
3603 s = rel_dyn;
3604 else if (s == hash_table->splt->output_section)
3605 {
3606 s = hash_table->splt;
3607 strip_zero_sized_plt = TRUE;
3608 }
3609 else
3610 s = hash_table->srelplt;
3611 s->flags |= SEC_EXCLUDE;
3612 s->output_section = bfd_abs_section_ptr;
3613 }
3614 else
3615 pp = &s->next;
3616
3617 if (strip_zero_sized_plt)
3618 for (extdyn = sdynamic->contents;
3619 extdyn < sdynamic->contents + sdynamic->size;
3620 extdyn = next)
3621 {
3622 next = extdyn + bed->s->sizeof_dyn;
3623 swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3624 switch (dyn.d_tag)
3625 {
3626 default:
3627 break;
3628 case DT_JMPREL:
3629 case DT_PLTRELSZ:
3630 case DT_PLTREL:
3631 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3632 the procedure linkage table (the .plt section) has been
3633 removed. */
3634 memmove (extdyn, next,
3635 sdynamic->size - (next - sdynamic->contents));
3636 next = extdyn;
3637 }
3638 }
3639
3640 if (strip_zero_sized)
3641 {
3642 /* Regenerate program headers. */
3643 elf_seg_map (info->output_bfd) = NULL;
3644 return _bfd_elf_map_sections_to_segments (info->output_bfd, info);
3645 }
3646
3647 return TRUE;
3648}
3649
e310298c 3650/* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
5a580b3a
AM
3651 1 if a DT_NEEDED tag already exists, and 0 on success. */
3652
e310298c
AM
3653int
3654bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
5a580b3a
AM
3655{
3656 struct elf_link_hash_table *hash_table;
ef53be89 3657 size_t strindex;
e310298c 3658 const char *soname;
5a580b3a 3659
7e9f0867
AM
3660 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3661 return -1;
3662
5a580b3a 3663 hash_table = elf_hash_table (info);
e310298c 3664 soname = elf_dt_name (abfd);
5a580b3a 3665 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
ef53be89 3666 if (strindex == (size_t) -1)
5a580b3a
AM
3667 return -1;
3668
02be4619 3669 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3670 {
3671 asection *sdyn;
3672 const struct elf_backend_data *bed;
3673 bfd_byte *extdyn;
3674
3675 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3676 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3677 if (sdyn != NULL)
3678 for (extdyn = sdyn->contents;
3679 extdyn < sdyn->contents + sdyn->size;
3680 extdyn += bed->s->sizeof_dyn)
3681 {
3682 Elf_Internal_Dyn dyn;
5a580b3a 3683
7e9f0867
AM
3684 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3685 if (dyn.d_tag == DT_NEEDED
3686 && dyn.d_un.d_val == strindex)
3687 {
3688 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3689 return 1;
3690 }
3691 }
5a580b3a
AM
3692 }
3693
e310298c
AM
3694 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3695 return -1;
7e9f0867 3696
e310298c
AM
3697 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3698 return -1;
5a580b3a
AM
3699
3700 return 0;
3701}
3702
7b15fa7a
AM
3703/* Return true if SONAME is on the needed list between NEEDED and STOP
3704 (or the end of list if STOP is NULL), and needed by a library that
3705 will be loaded. */
3706
010e5ae2 3707static bfd_boolean
7b15fa7a
AM
3708on_needed_list (const char *soname,
3709 struct bfd_link_needed_list *needed,
3710 struct bfd_link_needed_list *stop)
010e5ae2 3711{
7b15fa7a
AM
3712 struct bfd_link_needed_list *look;
3713 for (look = needed; look != stop; look = look->next)
3714 if (strcmp (soname, look->name) == 0
3715 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3716 /* If needed by a library that itself is not directly
3717 needed, recursively check whether that library is
3718 indirectly needed. Since we add DT_NEEDED entries to
3719 the end of the list, library dependencies appear after
3720 the library. Therefore search prior to the current
3721 LOOK, preventing possible infinite recursion. */
3722 || on_needed_list (elf_dt_name (look->by), needed, look)))
010e5ae2
AM
3723 return TRUE;
3724
3725 return FALSE;
3726}
3727
3a3f4bf7 3728/* Sort symbol by value, section, size, and type. */
4ad4eba5
AM
3729static int
3730elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3731{
3732 const struct elf_link_hash_entry *h1;
3733 const struct elf_link_hash_entry *h2;
10b7e05b 3734 bfd_signed_vma vdiff;
3a3f4bf7
AM
3735 int sdiff;
3736 const char *n1;
3737 const char *n2;
5a580b3a
AM
3738
3739 h1 = *(const struct elf_link_hash_entry **) arg1;
3740 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3741 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3742 if (vdiff != 0)
3743 return vdiff > 0 ? 1 : -1;
3a3f4bf7
AM
3744
3745 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3746 if (sdiff != 0)
3747 return sdiff;
3748
3749 /* Sort so that sized symbols are selected over zero size symbols. */
3750 vdiff = h1->size - h2->size;
3751 if (vdiff != 0)
3752 return vdiff > 0 ? 1 : -1;
3753
3754 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3755 if (h1->type != h2->type)
3756 return h1->type - h2->type;
3757
3758 /* If symbols are properly sized and typed, and multiple strong
3759 aliases are not defined in a shared library by the user we
3760 shouldn't get here. Unfortunately linker script symbols like
3761 __bss_start sometimes match a user symbol defined at the start of
3762 .bss without proper size and type. We'd like to preference the
3763 user symbol over reserved system symbols. Sort on leading
3764 underscores. */
3765 n1 = h1->root.root.string;
3766 n2 = h2->root.root.string;
3767 while (*n1 == *n2)
10b7e05b 3768 {
3a3f4bf7
AM
3769 if (*n1 == 0)
3770 break;
3771 ++n1;
3772 ++n2;
10b7e05b 3773 }
3a3f4bf7
AM
3774 if (*n1 == '_')
3775 return -1;
3776 if (*n2 == '_')
3777 return 1;
3778
3779 /* Final sort on name selects user symbols like '_u' over reserved
3780 system symbols like '_Z' and also will avoid qsort instability. */
3781 return *n1 - *n2;
5a580b3a 3782}
4ad4eba5 3783
5a580b3a
AM
3784/* This function is used to adjust offsets into .dynstr for
3785 dynamic symbols. This is called via elf_link_hash_traverse. */
3786
3787static bfd_boolean
3788elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3789{
a50b1753 3790 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3791
5a580b3a
AM
3792 if (h->dynindx != -1)
3793 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3794 return TRUE;
3795}
3796
3797/* Assign string offsets in .dynstr, update all structures referencing
3798 them. */
3799
4ad4eba5
AM
3800static bfd_boolean
3801elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3802{
3803 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3804 struct elf_link_local_dynamic_entry *entry;
3805 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3806 bfd *dynobj = hash_table->dynobj;
3807 asection *sdyn;
3808 bfd_size_type size;
3809 const struct elf_backend_data *bed;
3810 bfd_byte *extdyn;
3811
3812 _bfd_elf_strtab_finalize (dynstr);
3813 size = _bfd_elf_strtab_size (dynstr);
3814
3d16b64e
NA
3815 /* Allow the linker to examine the dynsymtab now it's fully populated. */
3816
3817 if (info->callbacks->examine_strtab)
3818 info->callbacks->examine_strtab (dynstr);
3819
5a580b3a 3820 bed = get_elf_backend_data (dynobj);
3d4d4302 3821 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3822 BFD_ASSERT (sdyn != NULL);
3823
3824 /* Update all .dynamic entries referencing .dynstr strings. */
3825 for (extdyn = sdyn->contents;
eea6121a 3826 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3827 extdyn += bed->s->sizeof_dyn)
3828 {
3829 Elf_Internal_Dyn dyn;
3830
3831 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3832 switch (dyn.d_tag)
3833 {
3834 case DT_STRSZ:
3835 dyn.d_un.d_val = size;
3836 break;
3837 case DT_NEEDED:
3838 case DT_SONAME:
3839 case DT_RPATH:
3840 case DT_RUNPATH:
3841 case DT_FILTER:
3842 case DT_AUXILIARY:
7ee314fa
AM
3843 case DT_AUDIT:
3844 case DT_DEPAUDIT:
5a580b3a
AM
3845 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3846 break;
3847 default:
3848 continue;
3849 }
3850 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3851 }
3852
3853 /* Now update local dynamic symbols. */
3854 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3855 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3856 entry->isym.st_name);
3857
3858 /* And the rest of dynamic symbols. */
3859 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3860
3861 /* Adjust version definitions. */
3862 if (elf_tdata (output_bfd)->cverdefs)
3863 {
3864 asection *s;
3865 bfd_byte *p;
ef53be89 3866 size_t i;
5a580b3a
AM
3867 Elf_Internal_Verdef def;
3868 Elf_Internal_Verdaux defaux;
3869
3d4d4302 3870 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3871 p = s->contents;
3872 do
3873 {
3874 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3875 &def);
3876 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3877 if (def.vd_aux != sizeof (Elf_External_Verdef))
3878 continue;
5a580b3a
AM
3879 for (i = 0; i < def.vd_cnt; ++i)
3880 {
3881 _bfd_elf_swap_verdaux_in (output_bfd,
3882 (Elf_External_Verdaux *) p, &defaux);
3883 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3884 defaux.vda_name);
3885 _bfd_elf_swap_verdaux_out (output_bfd,
3886 &defaux, (Elf_External_Verdaux *) p);
3887 p += sizeof (Elf_External_Verdaux);
3888 }
3889 }
3890 while (def.vd_next);
3891 }
3892
3893 /* Adjust version references. */
3894 if (elf_tdata (output_bfd)->verref)
3895 {
3896 asection *s;
3897 bfd_byte *p;
ef53be89 3898 size_t i;
5a580b3a
AM
3899 Elf_Internal_Verneed need;
3900 Elf_Internal_Vernaux needaux;
3901
3d4d4302 3902 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3903 p = s->contents;
3904 do
3905 {
3906 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3907 &need);
3908 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3909 _bfd_elf_swap_verneed_out (output_bfd, &need,
3910 (Elf_External_Verneed *) p);
3911 p += sizeof (Elf_External_Verneed);
3912 for (i = 0; i < need.vn_cnt; ++i)
3913 {
3914 _bfd_elf_swap_vernaux_in (output_bfd,
3915 (Elf_External_Vernaux *) p, &needaux);
3916 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3917 needaux.vna_name);
3918 _bfd_elf_swap_vernaux_out (output_bfd,
3919 &needaux,
3920 (Elf_External_Vernaux *) p);
3921 p += sizeof (Elf_External_Vernaux);
3922 }
3923 }
3924 while (need.vn_next);
3925 }
3926
3927 return TRUE;
3928}
3929\f
13285a1b
AM
3930/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3931 The default is to only match when the INPUT and OUTPUT are exactly
3932 the same target. */
3933
3934bfd_boolean
3935_bfd_elf_default_relocs_compatible (const bfd_target *input,
3936 const bfd_target *output)
3937{
3938 return input == output;
3939}
3940
3941/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3942 This version is used when different targets for the same architecture
3943 are virtually identical. */
3944
3945bfd_boolean
3946_bfd_elf_relocs_compatible (const bfd_target *input,
3947 const bfd_target *output)
3948{
3949 const struct elf_backend_data *obed, *ibed;
3950
3951 if (input == output)
3952 return TRUE;
3953
3954 ibed = xvec_get_elf_backend_data (input);
3955 obed = xvec_get_elf_backend_data (output);
3956
3957 if (ibed->arch != obed->arch)
3958 return FALSE;
3959
3960 /* If both backends are using this function, deem them compatible. */
3961 return ibed->relocs_compatible == obed->relocs_compatible;
3962}
3963
e5034e59
AM
3964/* Make a special call to the linker "notice" function to tell it that
3965 we are about to handle an as-needed lib, or have finished
1b786873 3966 processing the lib. */
e5034e59
AM
3967
3968bfd_boolean
3969_bfd_elf_notice_as_needed (bfd *ibfd,
3970 struct bfd_link_info *info,
3971 enum notice_asneeded_action act)
3972{
46135103 3973 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3974}
3975
d9689752
L
3976/* Check relocations an ELF object file. */
3977
3978bfd_boolean
3979_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3980{
3981 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3982 struct elf_link_hash_table *htab = elf_hash_table (info);
3983
3984 /* If this object is the same format as the output object, and it is
3985 not a shared library, then let the backend look through the
3986 relocs.
3987
3988 This is required to build global offset table entries and to
3989 arrange for dynamic relocs. It is not required for the
3990 particular common case of linking non PIC code, even when linking
3991 against shared libraries, but unfortunately there is no way of
3992 knowing whether an object file has been compiled PIC or not.
3993 Looking through the relocs is not particularly time consuming.
3994 The problem is that we must either (1) keep the relocs in memory,
3995 which causes the linker to require additional runtime memory or
3996 (2) read the relocs twice from the input file, which wastes time.
3997 This would be a good case for using mmap.
3998
3999 I have no idea how to handle linking PIC code into a file of a
4000 different format. It probably can't be done. */
4001 if ((abfd->flags & DYNAMIC) == 0
4002 && is_elf_hash_table (htab)
4003 && bed->check_relocs != NULL
4004 && elf_object_id (abfd) == elf_hash_table_id (htab)
4005 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4006 {
4007 asection *o;
4008
4009 for (o = abfd->sections; o != NULL; o = o->next)
4010 {
4011 Elf_Internal_Rela *internal_relocs;
4012 bfd_boolean ok;
4013
c4b126b8
L
4014 /* Don't check relocations in excluded sections. Don't do
4015 anything special with non-loaded, non-alloced sections.
4016 In particular, any relocs in such sections should not
4017 affect GOT and PLT reference counting (ie. we don't
4018 allow them to create GOT or PLT entries), there's no
4019 possibility or desire to optimize TLS relocs, and
4020 there's not much point in propagating relocs to shared
4021 libs that the dynamic linker won't relocate. */
4022 if ((o->flags & SEC_ALLOC) == 0
4023 || (o->flags & SEC_RELOC) == 0
5ce03cea 4024 || (o->flags & SEC_EXCLUDE) != 0
d9689752
L
4025 || o->reloc_count == 0
4026 || ((info->strip == strip_all || info->strip == strip_debugger)
4027 && (o->flags & SEC_DEBUGGING) != 0)
4028 || bfd_is_abs_section (o->output_section))
4029 continue;
4030
4031 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4032 info->keep_memory);
4033 if (internal_relocs == NULL)
4034 return FALSE;
4035
4036 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4037
4038 if (elf_section_data (o)->relocs != internal_relocs)
4039 free (internal_relocs);
4040
4041 if (! ok)
4042 return FALSE;
4043 }
4044 }
4045
4046 return TRUE;
4047}
4048
4ad4eba5
AM
4049/* Add symbols from an ELF object file to the linker hash table. */
4050
4051static bfd_boolean
4052elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4053{
a0c402a5 4054 Elf_Internal_Ehdr *ehdr;
4ad4eba5 4055 Elf_Internal_Shdr *hdr;
ef53be89
AM
4056 size_t symcount;
4057 size_t extsymcount;
4058 size_t extsymoff;
4ad4eba5
AM
4059 struct elf_link_hash_entry **sym_hash;
4060 bfd_boolean dynamic;
4061 Elf_External_Versym *extversym = NULL;
be22c732 4062 Elf_External_Versym *extversym_end = NULL;
4ad4eba5
AM
4063 Elf_External_Versym *ever;
4064 struct elf_link_hash_entry *weaks;
4065 struct elf_link_hash_entry **nondeflt_vers = NULL;
ef53be89 4066 size_t nondeflt_vers_cnt = 0;
4ad4eba5
AM
4067 Elf_Internal_Sym *isymbuf = NULL;
4068 Elf_Internal_Sym *isym;
4069 Elf_Internal_Sym *isymend;
4070 const struct elf_backend_data *bed;
4071 bfd_boolean add_needed;
66eb6687 4072 struct elf_link_hash_table *htab;
66eb6687 4073 void *alloc_mark = NULL;
4f87808c
AM
4074 struct bfd_hash_entry **old_table = NULL;
4075 unsigned int old_size = 0;
4076 unsigned int old_count = 0;
66eb6687 4077 void *old_tab = NULL;
66eb6687
AM
4078 void *old_ent;
4079 struct bfd_link_hash_entry *old_undefs = NULL;
4080 struct bfd_link_hash_entry *old_undefs_tail = NULL;
5b677558 4081 void *old_strtab = NULL;
66eb6687 4082 size_t tabsize = 0;
db6a5d5f 4083 asection *s;
29a9f53e 4084 bfd_boolean just_syms;
4ad4eba5 4085
66eb6687 4086 htab = elf_hash_table (info);
4ad4eba5 4087 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
4088
4089 if ((abfd->flags & DYNAMIC) == 0)
4090 dynamic = FALSE;
4091 else
4092 {
4093 dynamic = TRUE;
4094
4095 /* You can't use -r against a dynamic object. Also, there's no
4096 hope of using a dynamic object which does not exactly match
4097 the format of the output file. */
0e1862bb 4098 if (bfd_link_relocatable (info)
66eb6687 4099 || !is_elf_hash_table (htab)
f13a99db 4100 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 4101 {
0e1862bb 4102 if (bfd_link_relocatable (info))
9a0789ec
NC
4103 bfd_set_error (bfd_error_invalid_operation);
4104 else
4105 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
4106 goto error_return;
4107 }
4108 }
4109
a0c402a5
L
4110 ehdr = elf_elfheader (abfd);
4111 if (info->warn_alternate_em
4112 && bed->elf_machine_code != ehdr->e_machine
4113 && ((bed->elf_machine_alt1 != 0
4114 && ehdr->e_machine == bed->elf_machine_alt1)
4115 || (bed->elf_machine_alt2 != 0
4116 && ehdr->e_machine == bed->elf_machine_alt2)))
9793eb77 4117 _bfd_error_handler
695344c0 4118 /* xgettext:c-format */
9793eb77 4119 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
a0c402a5
L
4120 ehdr->e_machine, abfd, bed->elf_machine_code);
4121
4ad4eba5
AM
4122 /* As a GNU extension, any input sections which are named
4123 .gnu.warning.SYMBOL are treated as warning symbols for the given
4124 symbol. This differs from .gnu.warning sections, which generate
4125 warnings when they are included in an output file. */
dd98f8d2 4126 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 4127 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 4128 {
db6a5d5f 4129 const char *name;
4ad4eba5 4130
fd361982 4131 name = bfd_section_name (s);
db6a5d5f 4132 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 4133 {
db6a5d5f
AM
4134 char *msg;
4135 bfd_size_type sz;
4136
4137 name += sizeof ".gnu.warning." - 1;
4138
4139 /* If this is a shared object, then look up the symbol
4140 in the hash table. If it is there, and it is already
4141 been defined, then we will not be using the entry
4142 from this shared object, so we don't need to warn.
4143 FIXME: If we see the definition in a regular object
4144 later on, we will warn, but we shouldn't. The only
4145 fix is to keep track of what warnings we are supposed
4146 to emit, and then handle them all at the end of the
4147 link. */
4148 if (dynamic)
4ad4eba5 4149 {
db6a5d5f
AM
4150 struct elf_link_hash_entry *h;
4151
4152 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
4153
4154 /* FIXME: What about bfd_link_hash_common? */
4155 if (h != NULL
4156 && (h->root.type == bfd_link_hash_defined
4157 || h->root.type == bfd_link_hash_defweak))
4158 continue;
4159 }
4ad4eba5 4160
db6a5d5f
AM
4161 sz = s->size;
4162 msg = (char *) bfd_alloc (abfd, sz + 1);
4163 if (msg == NULL)
4164 goto error_return;
4ad4eba5 4165
db6a5d5f
AM
4166 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4167 goto error_return;
4ad4eba5 4168
db6a5d5f 4169 msg[sz] = '\0';
4ad4eba5 4170
db6a5d5f
AM
4171 if (! (_bfd_generic_link_add_one_symbol
4172 (info, abfd, name, BSF_WARNING, s, 0, msg,
4173 FALSE, bed->collect, NULL)))
4174 goto error_return;
4ad4eba5 4175
0e1862bb 4176 if (bfd_link_executable (info))
db6a5d5f
AM
4177 {
4178 /* Clobber the section size so that the warning does
4179 not get copied into the output file. */
4180 s->size = 0;
11d2f718 4181
db6a5d5f
AM
4182 /* Also set SEC_EXCLUDE, so that symbols defined in
4183 the warning section don't get copied to the output. */
4184 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
4185 }
4186 }
4187 }
4188
29a9f53e
L
4189 just_syms = ((s = abfd->sections) != NULL
4190 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4191
4ad4eba5
AM
4192 add_needed = TRUE;
4193 if (! dynamic)
4194 {
4195 /* If we are creating a shared library, create all the dynamic
4196 sections immediately. We need to attach them to something,
4197 so we attach them to this BFD, provided it is the right
bf89386a
L
4198 format and is not from ld --just-symbols. Always create the
4199 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
4200 are no input BFD's of the same format as the output, we can't
4201 make a shared library. */
4202 if (!just_syms
bf89386a 4203 && (bfd_link_pic (info)
9c1d7a08 4204 || (!bfd_link_relocatable (info)
3c5fce9b 4205 && info->nointerp
9c1d7a08 4206 && (info->export_dynamic || info->dynamic)))
66eb6687 4207 && is_elf_hash_table (htab)
f13a99db 4208 && info->output_bfd->xvec == abfd->xvec
66eb6687 4209 && !htab->dynamic_sections_created)
4ad4eba5
AM
4210 {
4211 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4212 goto error_return;
4213 }
4214 }
66eb6687 4215 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
4216 goto error_return;
4217 else
4218 {
4ad4eba5 4219 const char *soname = NULL;
7ee314fa 4220 char *audit = NULL;
4ad4eba5 4221 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
9acc85a6 4222 const Elf_Internal_Phdr *phdr;
e310298c 4223 struct elf_link_loaded_list *loaded_lib;
4ad4eba5
AM
4224
4225 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 4226 ld shouldn't allow it. */
29a9f53e 4227 if (just_syms)
92fd189d 4228 abort ();
4ad4eba5
AM
4229
4230 /* If this dynamic lib was specified on the command line with
4231 --as-needed in effect, then we don't want to add a DT_NEEDED
4232 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
4233 in by another lib's DT_NEEDED. When --no-add-needed is used
4234 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4235 any dynamic library in DT_NEEDED tags in the dynamic lib at
4236 all. */
4237 add_needed = (elf_dyn_lib_class (abfd)
4238 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4239 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
4240
4241 s = bfd_get_section_by_name (abfd, ".dynamic");
4242 if (s != NULL)
4243 {
4244 bfd_byte *dynbuf;
4245 bfd_byte *extdyn;
cb33740c 4246 unsigned int elfsec;
4ad4eba5
AM
4247 unsigned long shlink;
4248
eea6121a 4249 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194 4250 {
dc1e8a47 4251 error_free_dyn:
f8703194
L
4252 free (dynbuf);
4253 goto error_return;
4254 }
4ad4eba5
AM
4255
4256 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 4257 if (elfsec == SHN_BAD)
4ad4eba5
AM
4258 goto error_free_dyn;
4259 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4260
4261 for (extdyn = dynbuf;
9bff840e 4262 extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
4ad4eba5
AM
4263 extdyn += bed->s->sizeof_dyn)
4264 {
4265 Elf_Internal_Dyn dyn;
4266
4267 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4268 if (dyn.d_tag == DT_SONAME)
4269 {
4270 unsigned int tagv = dyn.d_un.d_val;
4271 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4272 if (soname == NULL)
4273 goto error_free_dyn;
4274 }
4275 if (dyn.d_tag == DT_NEEDED)
4276 {
4277 struct bfd_link_needed_list *n, **pn;
4278 char *fnm, *anm;
4279 unsigned int tagv = dyn.d_un.d_val;
986f0783 4280 size_t amt = sizeof (struct bfd_link_needed_list);
4ad4eba5 4281
a50b1753 4282 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4283 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4284 if (n == NULL || fnm == NULL)
4285 goto error_free_dyn;
4286 amt = strlen (fnm) + 1;
a50b1753 4287 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4288 if (anm == NULL)
4289 goto error_free_dyn;
4290 memcpy (anm, fnm, amt);
4291 n->name = anm;
4292 n->by = abfd;
4293 n->next = NULL;
66eb6687 4294 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4295 ;
4296 *pn = n;
4297 }
4298 if (dyn.d_tag == DT_RUNPATH)
4299 {
4300 struct bfd_link_needed_list *n, **pn;
4301 char *fnm, *anm;
4302 unsigned int tagv = dyn.d_un.d_val;
986f0783 4303 size_t amt = sizeof (struct bfd_link_needed_list);
4ad4eba5 4304
a50b1753 4305 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4306 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4307 if (n == NULL || fnm == NULL)
4308 goto error_free_dyn;
4309 amt = strlen (fnm) + 1;
a50b1753 4310 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4311 if (anm == NULL)
4312 goto error_free_dyn;
4313 memcpy (anm, fnm, amt);
4314 n->name = anm;
4315 n->by = abfd;
4316 n->next = NULL;
4317 for (pn = & runpath;
4318 *pn != NULL;
4319 pn = &(*pn)->next)
4320 ;
4321 *pn = n;
4322 }
4323 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4324 if (!runpath && dyn.d_tag == DT_RPATH)
4325 {
4326 struct bfd_link_needed_list *n, **pn;
4327 char *fnm, *anm;
4328 unsigned int tagv = dyn.d_un.d_val;
986f0783 4329 size_t amt = sizeof (struct bfd_link_needed_list);
4ad4eba5 4330
a50b1753 4331 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4332 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4333 if (n == NULL || fnm == NULL)
4334 goto error_free_dyn;
4335 amt = strlen (fnm) + 1;
a50b1753 4336 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 4337 if (anm == NULL)
f8703194 4338 goto error_free_dyn;
4ad4eba5
AM
4339 memcpy (anm, fnm, amt);
4340 n->name = anm;
4341 n->by = abfd;
4342 n->next = NULL;
4343 for (pn = & rpath;
4344 *pn != NULL;
4345 pn = &(*pn)->next)
4346 ;
4347 *pn = n;
4348 }
7ee314fa
AM
4349 if (dyn.d_tag == DT_AUDIT)
4350 {
4351 unsigned int tagv = dyn.d_un.d_val;
4352 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4353 }
4ad4eba5
AM
4354 }
4355
4356 free (dynbuf);
4357 }
4358
4359 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4360 frees all more recently bfd_alloc'd blocks as well. */
4361 if (runpath)
4362 rpath = runpath;
4363
4364 if (rpath)
4365 {
4366 struct bfd_link_needed_list **pn;
66eb6687 4367 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4368 ;
4369 *pn = rpath;
4370 }
4371
9acc85a6
AM
4372 /* If we have a PT_GNU_RELRO program header, mark as read-only
4373 all sections contained fully therein. This makes relro
4374 shared library sections appear as they will at run-time. */
4375 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
54025d58 4376 while (phdr-- > elf_tdata (abfd)->phdr)
9acc85a6
AM
4377 if (phdr->p_type == PT_GNU_RELRO)
4378 {
4379 for (s = abfd->sections; s != NULL; s = s->next)
502794d4
CE
4380 {
4381 unsigned int opb = bfd_octets_per_byte (abfd, s);
4382
4383 if ((s->flags & SEC_ALLOC) != 0
4384 && s->vma * opb >= phdr->p_vaddr
4385 && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4386 s->flags |= SEC_READONLY;
4387 }
9acc85a6
AM
4388 break;
4389 }
4390
4ad4eba5
AM
4391 /* We do not want to include any of the sections in a dynamic
4392 object in the output file. We hack by simply clobbering the
4393 list of sections in the BFD. This could be handled more
4394 cleanly by, say, a new section flag; the existing
4395 SEC_NEVER_LOAD flag is not the one we want, because that one
4396 still implies that the section takes up space in the output
4397 file. */
4398 bfd_section_list_clear (abfd);
4399
4ad4eba5
AM
4400 /* Find the name to use in a DT_NEEDED entry that refers to this
4401 object. If the object has a DT_SONAME entry, we use it.
4402 Otherwise, if the generic linker stuck something in
4403 elf_dt_name, we use that. Otherwise, we just use the file
4404 name. */
4405 if (soname == NULL || *soname == '\0')
4406 {
4407 soname = elf_dt_name (abfd);
4408 if (soname == NULL || *soname == '\0')
4409 soname = bfd_get_filename (abfd);
4410 }
4411
4412 /* Save the SONAME because sometimes the linker emulation code
4413 will need to know it. */
4414 elf_dt_name (abfd) = soname;
4415
4ad4eba5
AM
4416 /* If we have already included this dynamic object in the
4417 link, just ignore it. There is no reason to include a
4418 particular dynamic object more than once. */
e310298c
AM
4419 for (loaded_lib = htab->dyn_loaded;
4420 loaded_lib != NULL;
4421 loaded_lib = loaded_lib->next)
4422 {
4423 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4424 return TRUE;
4425 }
4426
4427 /* Create dynamic sections for backends that require that be done
4428 before setup_gnu_properties. */
4429 if (add_needed
4430 && !_bfd_elf_link_create_dynamic_sections (abfd, info))
4431 return FALSE;
7ee314fa
AM
4432
4433 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 4434 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
4435 }
4436
4437 /* If this is a dynamic object, we always link against the .dynsym
4438 symbol table, not the .symtab symbol table. The dynamic linker
4439 will only see the .dynsym symbol table, so there is no reason to
4440 look at .symtab for a dynamic object. */
4441
4442 if (! dynamic || elf_dynsymtab (abfd) == 0)
4443 hdr = &elf_tdata (abfd)->symtab_hdr;
4444 else
4445 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4446
4447 symcount = hdr->sh_size / bed->s->sizeof_sym;
4448
4449 /* The sh_info field of the symtab header tells us where the
4450 external symbols start. We don't care about the local symbols at
4451 this point. */
4452 if (elf_bad_symtab (abfd))
4453 {
4454 extsymcount = symcount;
4455 extsymoff = 0;
4456 }
4457 else
4458 {
4459 extsymcount = symcount - hdr->sh_info;
4460 extsymoff = hdr->sh_info;
4461 }
4462
f45794cb 4463 sym_hash = elf_sym_hashes (abfd);
012b2306 4464 if (extsymcount != 0)
4ad4eba5
AM
4465 {
4466 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4467 NULL, NULL, NULL);
4468 if (isymbuf == NULL)
4469 goto error_return;
4470
4ad4eba5 4471 if (sym_hash == NULL)
012b2306
AM
4472 {
4473 /* We store a pointer to the hash table entry for each
4474 external symbol. */
986f0783 4475 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
012b2306
AM
4476 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4477 if (sym_hash == NULL)
4478 goto error_free_sym;
4479 elf_sym_hashes (abfd) = sym_hash;
4480 }
4ad4eba5
AM
4481 }
4482
4483 if (dynamic)
4484 {
4485 /* Read in any version definitions. */
fc0e6df6
PB
4486 if (!_bfd_elf_slurp_version_tables (abfd,
4487 info->default_imported_symver))
4ad4eba5
AM
4488 goto error_free_sym;
4489
4490 /* Read in the symbol versions, but don't bother to convert them
4491 to internal format. */
4492 if (elf_dynversym (abfd) != 0)
4493 {
986f0783
AM
4494 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4495 bfd_size_type amt = versymhdr->sh_size;
4ad4eba5 4496
2bb3687b
AM
4497 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4498 goto error_free_sym;
4499 extversym = (Elf_External_Versym *)
4500 _bfd_malloc_and_read (abfd, amt, amt);
4ad4eba5
AM
4501 if (extversym == NULL)
4502 goto error_free_sym;
986f0783 4503 extversym_end = extversym + amt / sizeof (*extversym);
4ad4eba5
AM
4504 }
4505 }
4506
66eb6687
AM
4507 /* If we are loading an as-needed shared lib, save the symbol table
4508 state before we start adding symbols. If the lib turns out
4509 to be unneeded, restore the state. */
4510 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4511 {
4512 unsigned int i;
4513 size_t entsize;
4514
4515 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4516 {
4517 struct bfd_hash_entry *p;
2de92251 4518 struct elf_link_hash_entry *h;
66eb6687
AM
4519
4520 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
4521 {
4522 h = (struct elf_link_hash_entry *) p;
4523 entsize += htab->root.table.entsize;
4524 if (h->root.type == bfd_link_hash_warning)
7ba11550
AM
4525 {
4526 entsize += htab->root.table.entsize;
4527 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4528 }
4529 if (h->root.type == bfd_link_hash_common)
4530 entsize += sizeof (*h->root.u.c.p);
2de92251 4531 }
66eb6687
AM
4532 }
4533
4534 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 4535 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
4536 if (old_tab == NULL)
4537 goto error_free_vers;
4538
4539 /* Remember the current objalloc pointer, so that all mem for
4540 symbols added can later be reclaimed. */
4541 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4542 if (alloc_mark == NULL)
4543 goto error_free_vers;
4544
5061a885
AM
4545 /* Make a special call to the linker "notice" function to
4546 tell it that we are about to handle an as-needed lib. */
e5034e59 4547 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 4548 goto error_free_vers;
5061a885 4549
f45794cb
AM
4550 /* Clone the symbol table. Remember some pointers into the
4551 symbol table, and dynamic symbol count. */
4552 old_ent = (char *) old_tab + tabsize;
66eb6687 4553 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
4554 old_undefs = htab->root.undefs;
4555 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
4556 old_table = htab->root.table.table;
4557 old_size = htab->root.table.size;
4558 old_count = htab->root.table.count;
e310298c
AM
4559 old_strtab = NULL;
4560 if (htab->dynstr != NULL)
4561 {
4562 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4563 if (old_strtab == NULL)
4564 goto error_free_vers;
4565 }
66eb6687
AM
4566
4567 for (i = 0; i < htab->root.table.size; i++)
4568 {
4569 struct bfd_hash_entry *p;
2de92251 4570 struct elf_link_hash_entry *h;
66eb6687
AM
4571
4572 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4573 {
2de92251 4574 h = (struct elf_link_hash_entry *) p;
7ba11550
AM
4575 memcpy (old_ent, h, htab->root.table.entsize);
4576 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4577 if (h->root.type == bfd_link_hash_warning)
4578 {
7ba11550
AM
4579 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4580 memcpy (old_ent, h, htab->root.table.entsize);
2de92251
AM
4581 old_ent = (char *) old_ent + htab->root.table.entsize;
4582 }
7ba11550
AM
4583 if (h->root.type == bfd_link_hash_common)
4584 {
4585 memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
4586 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
4587 }
66eb6687
AM
4588 }
4589 }
4590 }
4ad4eba5 4591
66eb6687 4592 weaks = NULL;
be22c732
NC
4593 if (extversym == NULL)
4594 ever = NULL;
4595 else if (extversym + extsymoff < extversym_end)
4596 ever = extversym + extsymoff;
4597 else
4598 {
4599 /* xgettext:c-format */
4600 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4601 abfd, (long) extsymoff,
4602 (long) (extversym_end - extversym) / sizeof (* extversym));
4603 bfd_set_error (bfd_error_bad_value);
4604 goto error_free_vers;
4605 }
4606
b4c555cf
ML
4607 if (!bfd_link_relocatable (info)
4608 && abfd->lto_slim_object)
cc5277b1
ML
4609 {
4610 _bfd_error_handler
4611 (_("%pB: plugin needed to handle lto object"), abfd);
4612 }
4613
4ad4eba5
AM
4614 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4615 isym < isymend;
4616 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4617 {
4618 int bind;
4619 bfd_vma value;
af44c138 4620 asection *sec, *new_sec;
4ad4eba5
AM
4621 flagword flags;
4622 const char *name;
4623 struct elf_link_hash_entry *h;
90c984fc 4624 struct elf_link_hash_entry *hi;
4ad4eba5
AM
4625 bfd_boolean definition;
4626 bfd_boolean size_change_ok;
4627 bfd_boolean type_change_ok;
37a9e49a
L
4628 bfd_boolean new_weak;
4629 bfd_boolean old_weak;
7ba11550 4630 bfd *override;
a4d8e49b 4631 bfd_boolean common;
97196564 4632 bfd_boolean discarded;
4ad4eba5 4633 unsigned int old_alignment;
4538d1c7 4634 unsigned int shindex;
4ad4eba5 4635 bfd *old_bfd;
6e33951e 4636 bfd_boolean matched;
4ad4eba5 4637
7ba11550 4638 override = NULL;
4ad4eba5
AM
4639
4640 flags = BSF_NO_FLAGS;
4641 sec = NULL;
4642 value = isym->st_value;
a4d8e49b 4643 common = bed->common_definition (isym);
2980ccad
L
4644 if (common && info->inhibit_common_definition)
4645 {
4646 /* Treat common symbol as undefined for --no-define-common. */
4647 isym->st_shndx = SHN_UNDEF;
4648 common = FALSE;
4649 }
97196564 4650 discarded = FALSE;
4ad4eba5
AM
4651
4652 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 4653 switch (bind)
4ad4eba5 4654 {
3e7a7d11 4655 case STB_LOCAL:
4ad4eba5
AM
4656 /* This should be impossible, since ELF requires that all
4657 global symbols follow all local symbols, and that sh_info
4658 point to the first global symbol. Unfortunately, Irix 5
4659 screws this up. */
fe3fef62
AM
4660 if (elf_bad_symtab (abfd))
4661 continue;
4662
4663 /* If we aren't prepared to handle locals within the globals
4538d1c7
AM
4664 then we'll likely segfault on a NULL symbol hash if the
4665 symbol is ever referenced in relocations. */
4666 shindex = elf_elfheader (abfd)->e_shstrndx;
4667 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4668 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4669 " (>= sh_info of %lu)"),
4670 abfd, name, (long) (isym - isymbuf + extsymoff),
4671 (long) extsymoff);
4672
4673 /* Dynamic object relocations are not processed by ld, so
4674 ld won't run into the problem mentioned above. */
4675 if (dynamic)
4676 continue;
fe3fef62
AM
4677 bfd_set_error (bfd_error_bad_value);
4678 goto error_free_vers;
3e7a7d11
NC
4679
4680 case STB_GLOBAL:
a4d8e49b 4681 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4682 flags = BSF_GLOBAL;
3e7a7d11
NC
4683 break;
4684
4685 case STB_WEAK:
4686 flags = BSF_WEAK;
4687 break;
4688
4689 case STB_GNU_UNIQUE:
4690 flags = BSF_GNU_UNIQUE;
4691 break;
4692
4693 default:
4ad4eba5 4694 /* Leave it up to the processor backend. */
3e7a7d11 4695 break;
4ad4eba5
AM
4696 }
4697
4698 if (isym->st_shndx == SHN_UNDEF)
4699 sec = bfd_und_section_ptr;
cb33740c
AM
4700 else if (isym->st_shndx == SHN_ABS)
4701 sec = bfd_abs_section_ptr;
4702 else if (isym->st_shndx == SHN_COMMON)
4703 {
4704 sec = bfd_com_section_ptr;
4705 /* What ELF calls the size we call the value. What ELF
4706 calls the value we call the alignment. */
4707 value = isym->st_size;
4708 }
4709 else
4ad4eba5
AM
4710 {
4711 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4712 if (sec == NULL)
4713 sec = bfd_abs_section_ptr;
dbaa2011 4714 else if (discarded_section (sec))
529fcb95 4715 {
e5d08002
L
4716 /* Symbols from discarded section are undefined. We keep
4717 its visibility. */
529fcb95 4718 sec = bfd_und_section_ptr;
97196564 4719 discarded = TRUE;
529fcb95
PB
4720 isym->st_shndx = SHN_UNDEF;
4721 }
4ad4eba5
AM
4722 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4723 value -= sec->vma;
4724 }
4ad4eba5
AM
4725
4726 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4727 isym->st_name);
4728 if (name == NULL)
4729 goto error_free_vers;
4730
4731 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4732 && (abfd->flags & BFD_PLUGIN) != 0)
4733 {
4734 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4735
4736 if (xc == NULL)
4737 {
4738 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4739 | SEC_EXCLUDE);
4740 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4741 if (xc == NULL)
4742 goto error_free_vers;
4743 }
4744 sec = xc;
4745 }
4746 else if (isym->st_shndx == SHN_COMMON
4747 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4748 && !bfd_link_relocatable (info))
4ad4eba5
AM
4749 {
4750 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4751
4752 if (tcomm == NULL)
4753 {
02d00247
AM
4754 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4755 | SEC_LINKER_CREATED);
4756 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4757 if (tcomm == NULL)
4ad4eba5
AM
4758 goto error_free_vers;
4759 }
4760 sec = tcomm;
4761 }
66eb6687 4762 else if (bed->elf_add_symbol_hook)
4ad4eba5 4763 {
66eb6687
AM
4764 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4765 &sec, &value))
4ad4eba5
AM
4766 goto error_free_vers;
4767
4768 /* The hook function sets the name to NULL if this symbol
4769 should be skipped for some reason. */
4770 if (name == NULL)
4771 continue;
4772 }
4773
4774 /* Sanity check that all possibilities were handled. */
4775 if (sec == NULL)
4538d1c7 4776 abort ();
4ad4eba5 4777
191c0c42
AM
4778 /* Silently discard TLS symbols from --just-syms. There's
4779 no way to combine a static TLS block with a new TLS block
4780 for this executable. */
4781 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4782 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4783 continue;
4784
4ad4eba5
AM
4785 if (bfd_is_und_section (sec)
4786 || bfd_is_com_section (sec))
4787 definition = FALSE;
4788 else
4789 definition = TRUE;
4790
4791 size_change_ok = FALSE;
66eb6687 4792 type_change_ok = bed->type_change_ok;
37a9e49a 4793 old_weak = FALSE;
6e33951e 4794 matched = FALSE;
4ad4eba5
AM
4795 old_alignment = 0;
4796 old_bfd = NULL;
af44c138 4797 new_sec = sec;
4ad4eba5 4798
66eb6687 4799 if (is_elf_hash_table (htab))
4ad4eba5
AM
4800 {
4801 Elf_Internal_Versym iver;
4802 unsigned int vernum = 0;
4803 bfd_boolean skip;
4804
fc0e6df6 4805 if (ever == NULL)
4ad4eba5 4806 {
fc0e6df6
PB
4807 if (info->default_imported_symver)
4808 /* Use the default symbol version created earlier. */
4809 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4810 else
4811 iver.vs_vers = 0;
4812 }
be22c732
NC
4813 else if (ever >= extversym_end)
4814 {
4815 /* xgettext:c-format */
4816 _bfd_error_handler (_("%pB: not enough version information"),
4817 abfd);
4818 bfd_set_error (bfd_error_bad_value);
4819 goto error_free_vers;
4820 }
fc0e6df6
PB
4821 else
4822 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4823
4824 vernum = iver.vs_vers & VERSYM_VERSION;
4825
4826 /* If this is a hidden symbol, or if it is not version
4827 1, we append the version name to the symbol name.
cc86ff91
EB
4828 However, we do not modify a non-hidden absolute symbol
4829 if it is not a function, because it might be the version
4830 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4831 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4832 || (vernum > 1
4833 && (!bfd_is_abs_section (sec)
4834 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4835 {
4836 const char *verstr;
4837 size_t namelen, verlen, newlen;
4838 char *newname, *p;
4839
4840 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4841 {
fc0e6df6
PB
4842 if (vernum > elf_tdata (abfd)->cverdefs)
4843 verstr = NULL;
4844 else if (vernum > 1)
4845 verstr =
4846 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4847 else
4848 verstr = "";
4ad4eba5 4849
fc0e6df6 4850 if (verstr == NULL)
4ad4eba5 4851 {
4eca0228 4852 _bfd_error_handler
695344c0 4853 /* xgettext:c-format */
871b3ab2 4854 (_("%pB: %s: invalid version %u (max %d)"),
fc0e6df6
PB
4855 abfd, name, vernum,
4856 elf_tdata (abfd)->cverdefs);
4857 bfd_set_error (bfd_error_bad_value);
4858 goto error_free_vers;
4ad4eba5 4859 }
fc0e6df6
PB
4860 }
4861 else
4862 {
4863 /* We cannot simply test for the number of
4864 entries in the VERNEED section since the
4865 numbers for the needed versions do not start
4866 at 0. */
4867 Elf_Internal_Verneed *t;
4868
4869 verstr = NULL;
4870 for (t = elf_tdata (abfd)->verref;
4871 t != NULL;
4872 t = t->vn_nextref)
4ad4eba5 4873 {
fc0e6df6 4874 Elf_Internal_Vernaux *a;
4ad4eba5 4875
fc0e6df6
PB
4876 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4877 {
4878 if (a->vna_other == vernum)
4ad4eba5 4879 {
fc0e6df6
PB
4880 verstr = a->vna_nodename;
4881 break;
4ad4eba5 4882 }
4ad4eba5 4883 }
fc0e6df6
PB
4884 if (a != NULL)
4885 break;
4886 }
4887 if (verstr == NULL)
4888 {
4eca0228 4889 _bfd_error_handler
695344c0 4890 /* xgettext:c-format */
871b3ab2 4891 (_("%pB: %s: invalid needed version %d"),
fc0e6df6
PB
4892 abfd, name, vernum);
4893 bfd_set_error (bfd_error_bad_value);
4894 goto error_free_vers;
4ad4eba5 4895 }
4ad4eba5 4896 }
fc0e6df6
PB
4897
4898 namelen = strlen (name);
4899 verlen = strlen (verstr);
4900 newlen = namelen + verlen + 2;
4901 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4902 && isym->st_shndx != SHN_UNDEF)
4903 ++newlen;
4904
a50b1753 4905 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4906 if (newname == NULL)
4907 goto error_free_vers;
4908 memcpy (newname, name, namelen);
4909 p = newname + namelen;
4910 *p++ = ELF_VER_CHR;
4911 /* If this is a defined non-hidden version symbol,
4912 we add another @ to the name. This indicates the
4913 default version of the symbol. */
4914 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4915 && isym->st_shndx != SHN_UNDEF)
4916 *p++ = ELF_VER_CHR;
4917 memcpy (p, verstr, verlen + 1);
4918
4919 name = newname;
4ad4eba5
AM
4920 }
4921
cd3416da
AM
4922 /* If this symbol has default visibility and the user has
4923 requested we not re-export it, then mark it as hidden. */
a0d49154 4924 if (!bfd_is_und_section (sec)
cd3416da 4925 && !dynamic
ce875075 4926 && abfd->no_export
cd3416da
AM
4927 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4928 isym->st_other = (STV_HIDDEN
4929 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4930
4f3fedcf
AM
4931 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4932 sym_hash, &old_bfd, &old_weak,
4933 &old_alignment, &skip, &override,
6e33951e
L
4934 &type_change_ok, &size_change_ok,
4935 &matched))
4ad4eba5
AM
4936 goto error_free_vers;
4937
4938 if (skip)
4939 continue;
4940
6e33951e
L
4941 /* Override a definition only if the new symbol matches the
4942 existing one. */
4943 if (override && matched)
4ad4eba5
AM
4944 definition = FALSE;
4945
4946 h = *sym_hash;
4947 while (h->root.type == bfd_link_hash_indirect
4948 || h->root.type == bfd_link_hash_warning)
4949 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4950
4ad4eba5 4951 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4952 && vernum > 1
4953 && definition)
4954 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4955 }
4956
4957 if (! (_bfd_generic_link_add_one_symbol
7ba11550
AM
4958 (info, override ? override : abfd, name, flags, sec, value,
4959 NULL, FALSE, bed->collect,
4ad4eba5
AM
4960 (struct bfd_link_hash_entry **) sym_hash)))
4961 goto error_free_vers;
4962
4963 h = *sym_hash;
90c984fc
L
4964 /* We need to make sure that indirect symbol dynamic flags are
4965 updated. */
4966 hi = h;
4ad4eba5
AM
4967 while (h->root.type == bfd_link_hash_indirect
4968 || h->root.type == bfd_link_hash_warning)
4969 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4970
97196564
L
4971 /* Setting the index to -3 tells elf_link_output_extsym that
4972 this symbol is defined in a discarded section. */
4973 if (discarded)
4974 h->indx = -3;
4975
4ad4eba5
AM
4976 *sym_hash = h;
4977
37a9e49a 4978 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4979 if (dynamic
4980 && definition
37a9e49a 4981 && new_weak
fcb93ecf 4982 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4983 && is_elf_hash_table (htab)
60d67dc8 4984 && h->u.alias == NULL)
4ad4eba5
AM
4985 {
4986 /* Keep a list of all weak defined non function symbols from
60d67dc8
AM
4987 a dynamic object, using the alias field. Later in this
4988 function we will set the alias field to the correct
4ad4eba5
AM
4989 value. We only put non-function symbols from dynamic
4990 objects on this list, because that happens to be the only
4991 time we need to know the normal symbol corresponding to a
4992 weak symbol, and the information is time consuming to
60d67dc8 4993 figure out. If the alias field is not already NULL,
4ad4eba5
AM
4994 then this symbol was already defined by some previous
4995 dynamic object, and we will be using that previous
4996 definition anyhow. */
4997
60d67dc8 4998 h->u.alias = weaks;
4ad4eba5 4999 weaks = h;
4ad4eba5
AM
5000 }
5001
5002 /* Set the alignment of a common symbol. */
a4d8e49b 5003 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
5004 && h->root.type == bfd_link_hash_common)
5005 {
5006 unsigned int align;
5007
a4d8e49b 5008 if (common)
af44c138
L
5009 align = bfd_log2 (isym->st_value);
5010 else
5011 {
5012 /* The new symbol is a common symbol in a shared object.
5013 We need to get the alignment from the section. */
5014 align = new_sec->alignment_power;
5015 }
595213d4 5016 if (align > old_alignment)
4ad4eba5
AM
5017 h->root.u.c.p->alignment_power = align;
5018 else
5019 h->root.u.c.p->alignment_power = old_alignment;
5020 }
5021
66eb6687 5022 if (is_elf_hash_table (htab))
4ad4eba5 5023 {
4f3fedcf
AM
5024 /* Set a flag in the hash table entry indicating the type of
5025 reference or definition we just found. A dynamic symbol
5026 is one which is referenced or defined by both a regular
5027 object and a shared object. */
5028 bfd_boolean dynsym = FALSE;
5029
b1a92c63
AM
5030 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5031 if ((abfd->flags & BFD_PLUGIN) != 0)
5032 ;
5033 else if (!dynamic)
4f3fedcf
AM
5034 {
5035 if (! definition)
5036 {
5037 h->ref_regular = 1;
5038 if (bind != STB_WEAK)
5039 h->ref_regular_nonweak = 1;
5040 }
5041 else
5042 {
5043 h->def_regular = 1;
5044 if (h->def_dynamic)
5045 {
5046 h->def_dynamic = 0;
5047 h->ref_dynamic = 1;
5048 }
5049 }
4f3fedcf
AM
5050 }
5051 else
5052 {
5053 if (! definition)
5054 {
5055 h->ref_dynamic = 1;
5056 hi->ref_dynamic = 1;
5057 }
5058 else
5059 {
5060 h->def_dynamic = 1;
5061 hi->def_dynamic = 1;
5062 }
b1a92c63 5063 }
4f3fedcf 5064
b1a92c63
AM
5065 /* If an indirect symbol has been forced local, don't
5066 make the real symbol dynamic. */
5067 if (h != hi && hi->forced_local)
5068 ;
5069 else if (!dynamic)
5070 {
5071 if (bfd_link_dll (info)
5072 || h->def_dynamic
5073 || h->ref_dynamic)
5074 dynsym = TRUE;
5075 }
5076 else
5077 {
5078 if (h->def_regular
5079 || h->ref_regular
5080 || (h->is_weakalias
5081 && weakdef (h)->dynindx != -1))
4f3fedcf
AM
5082 dynsym = TRUE;
5083 }
5084
5085 /* Check to see if we need to add an indirect symbol for
5086 the default name. */
726d7d1e
AM
5087 if ((definition
5088 || (!override && h->root.type == bfd_link_hash_common))
5089 && !(hi != h
5090 && hi->versioned == versioned_hidden))
4f3fedcf 5091 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
f01fb44c 5092 sec, value, &old_bfd, &dynsym))
4f3fedcf 5093 goto error_free_vers;
4ad4eba5
AM
5094
5095 /* Check the alignment when a common symbol is involved. This
5096 can change when a common symbol is overridden by a normal
5097 definition or a common symbol is ignored due to the old
5098 normal definition. We need to make sure the maximum
5099 alignment is maintained. */
a4d8e49b 5100 if ((old_alignment || common)
4ad4eba5
AM
5101 && h->root.type != bfd_link_hash_common)
5102 {
5103 unsigned int common_align;
5104 unsigned int normal_align;
5105 unsigned int symbol_align;
5106 bfd *normal_bfd;
5107 bfd *common_bfd;
5108
3a81e825
AM
5109 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5110 || h->root.type == bfd_link_hash_defweak);
5111
4ad4eba5
AM
5112 symbol_align = ffs (h->root.u.def.value) - 1;
5113 if (h->root.u.def.section->owner != NULL
0616a280
AM
5114 && (h->root.u.def.section->owner->flags
5115 & (DYNAMIC | BFD_PLUGIN)) == 0)
4ad4eba5
AM
5116 {
5117 normal_align = h->root.u.def.section->alignment_power;
5118 if (normal_align > symbol_align)
5119 normal_align = symbol_align;
5120 }
5121 else
5122 normal_align = symbol_align;
5123
5124 if (old_alignment)
5125 {
5126 common_align = old_alignment;
5127 common_bfd = old_bfd;
5128 normal_bfd = abfd;
5129 }
5130 else
5131 {
5132 common_align = bfd_log2 (isym->st_value);
5133 common_bfd = abfd;
5134 normal_bfd = old_bfd;
5135 }
5136
5137 if (normal_align < common_align)
d07676f8
NC
5138 {
5139 /* PR binutils/2735 */
5140 if (normal_bfd == NULL)
4eca0228 5141 _bfd_error_handler
695344c0 5142 /* xgettext:c-format */
9793eb77 5143 (_("warning: alignment %u of common symbol `%s' in %pB is"
871b3ab2 5144 " greater than the alignment (%u) of its section %pA"),
c08bb8dd
AM
5145 1 << common_align, name, common_bfd,
5146 1 << normal_align, h->root.u.def.section);
d07676f8 5147 else
4eca0228 5148 _bfd_error_handler
695344c0 5149 /* xgettext:c-format */
9793eb77 5150 (_("warning: alignment %u of symbol `%s' in %pB"
871b3ab2 5151 " is smaller than %u in %pB"),
c08bb8dd
AM
5152 1 << normal_align, name, normal_bfd,
5153 1 << common_align, common_bfd);
d07676f8 5154 }
4ad4eba5
AM
5155 }
5156
83ad0046 5157 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
5158 if (isym->st_size != 0
5159 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
5160 && (definition || h->size == 0))
5161 {
83ad0046
L
5162 if (h->size != 0
5163 && h->size != isym->st_size
5164 && ! size_change_ok)
4eca0228 5165 _bfd_error_handler
695344c0 5166 /* xgettext:c-format */
9793eb77 5167 (_("warning: size of symbol `%s' changed"
2dcf00ce
AM
5168 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5169 name, (uint64_t) h->size, old_bfd,
5170 (uint64_t) isym->st_size, abfd);
4ad4eba5
AM
5171
5172 h->size = isym->st_size;
5173 }
5174
5175 /* If this is a common symbol, then we always want H->SIZE
5176 to be the size of the common symbol. The code just above
5177 won't fix the size if a common symbol becomes larger. We
5178 don't warn about a size change here, because that is
4f3fedcf 5179 covered by --warn-common. Allow changes between different
fcb93ecf 5180 function types. */
4ad4eba5
AM
5181 if (h->root.type == bfd_link_hash_common)
5182 h->size = h->root.u.c.size;
5183
5184 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
5185 && ((definition && !new_weak)
5186 || (old_weak && h->root.type == bfd_link_hash_common)
5187 || h->type == STT_NOTYPE))
4ad4eba5 5188 {
2955ec4c
L
5189 unsigned int type = ELF_ST_TYPE (isym->st_info);
5190
5191 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5192 symbol. */
5193 if (type == STT_GNU_IFUNC
5194 && (abfd->flags & DYNAMIC) != 0)
5195 type = STT_FUNC;
4ad4eba5 5196
2955ec4c
L
5197 if (h->type != type)
5198 {
5199 if (h->type != STT_NOTYPE && ! type_change_ok)
695344c0 5200 /* xgettext:c-format */
4eca0228 5201 _bfd_error_handler
9793eb77 5202 (_("warning: type of symbol `%s' changed"
871b3ab2 5203 " from %d to %d in %pB"),
c08bb8dd 5204 name, h->type, type, abfd);
2955ec4c
L
5205
5206 h->type = type;
5207 }
4ad4eba5
AM
5208 }
5209
54ac0771 5210 /* Merge st_other field. */
5160d0f3
AM
5211 elf_merge_st_other (abfd, h, isym->st_other, sec,
5212 definition, dynamic);
4ad4eba5 5213
c3df8c14 5214 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
5215 if (definition
5216 && (sec->flags & SEC_DEBUGGING)
5217 && !bfd_link_relocatable (info))
c3df8c14
AM
5218 dynsym = FALSE;
5219
b1a92c63
AM
5220 /* Nor should we make plugin symbols dynamic. */
5221 if ((abfd->flags & BFD_PLUGIN) != 0)
5222 dynsym = FALSE;
5223
35fc36a8 5224 if (definition)
35399224
L
5225 {
5226 h->target_internal = isym->st_target_internal;
5227 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5228 }
35fc36a8 5229
4ad4eba5
AM
5230 if (definition && !dynamic)
5231 {
5232 char *p = strchr (name, ELF_VER_CHR);
5233 if (p != NULL && p[1] != ELF_VER_CHR)
5234 {
5235 /* Queue non-default versions so that .symver x, x@FOO
5236 aliases can be checked. */
66eb6687 5237 if (!nondeflt_vers)
4ad4eba5 5238 {
986f0783
AM
5239 size_t amt = ((isymend - isym + 1)
5240 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
5241 nondeflt_vers
5242 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
5243 if (!nondeflt_vers)
5244 goto error_free_vers;
4ad4eba5 5245 }
66eb6687 5246 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
5247 }
5248 }
5249
b1a92c63 5250 if (dynsym && h->dynindx == -1)
4ad4eba5 5251 {
c152c796 5252 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 5253 goto error_free_vers;
60d67dc8
AM
5254 if (h->is_weakalias
5255 && weakdef (h)->dynindx == -1)
4ad4eba5 5256 {
60d67dc8 5257 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
4ad4eba5
AM
5258 goto error_free_vers;
5259 }
5260 }
1f599d0e 5261 else if (h->dynindx != -1)
4ad4eba5
AM
5262 /* If the symbol already has a dynamic index, but
5263 visibility says it should not be visible, turn it into
5264 a local symbol. */
5265 switch (ELF_ST_VISIBILITY (h->other))
5266 {
5267 case STV_INTERNAL:
5268 case STV_HIDDEN:
5269 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5270 dynsym = FALSE;
5271 break;
5272 }
5273
5274 if (!add_needed
aef28989 5275 && matched
4ad4eba5 5276 && definition
f01fb44c 5277 && h->root.type != bfd_link_hash_indirect
010e5ae2 5278 && ((dynsym
a896df97 5279 && h->ref_regular_nonweak)
b1a92c63
AM
5280 || (old_bfd != NULL
5281 && (old_bfd->flags & BFD_PLUGIN) != 0
7d409ac0 5282 && !info->lto_all_symbols_read
b1a92c63 5283 && bind != STB_WEAK)
ffa9430d 5284 || (h->ref_dynamic_nonweak
010e5ae2 5285 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
7b15fa7a
AM
5286 && !on_needed_list (elf_dt_name (abfd),
5287 htab->needed, NULL))))
4ad4eba5 5288 {
4ad4eba5
AM
5289 const char *soname = elf_dt_name (abfd);
5290
16e4ecc0
AM
5291 info->callbacks->minfo ("%!", soname, old_bfd,
5292 h->root.root.string);
5293
4ad4eba5
AM
5294 /* A symbol from a library loaded via DT_NEEDED of some
5295 other library is referenced by a regular object.
e56f61be 5296 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
5297 --no-add-needed is used and the reference was not
5298 a weak one. */
4f3fedcf 5299 if (old_bfd != NULL
b918acf9 5300 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be 5301 {
4eca0228 5302 _bfd_error_handler
695344c0 5303 /* xgettext:c-format */
871b3ab2 5304 (_("%pB: undefined reference to symbol '%s'"),
4f3fedcf 5305 old_bfd, name);
ff5ac77b 5306 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
5307 goto error_free_vers;
5308 }
5309
a50b1753 5310 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 5311 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 5312
e310298c
AM
5313 /* Create dynamic sections for backends that require
5314 that be done before setup_gnu_properties. */
5315 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5316 return FALSE;
4ad4eba5 5317 add_needed = TRUE;
4ad4eba5
AM
5318 }
5319 }
5320 }
5321
a83ef4d1
L
5322 if (info->lto_plugin_active
5323 && !bfd_link_relocatable (info)
5324 && (abfd->flags & BFD_PLUGIN) == 0
5325 && !just_syms
5326 && extsymcount)
5327 {
5328 int r_sym_shift;
5329
5330 if (bed->s->arch_size == 32)
5331 r_sym_shift = 8;
5332 else
5333 r_sym_shift = 32;
5334
5335 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5336 referenced in regular objects so that linker plugin will get
5337 the correct symbol resolution. */
5338
5339 sym_hash = elf_sym_hashes (abfd);
5340 for (s = abfd->sections; s != NULL; s = s->next)
5341 {
5342 Elf_Internal_Rela *internal_relocs;
5343 Elf_Internal_Rela *rel, *relend;
5344
5345 /* Don't check relocations in excluded sections. */
5346 if ((s->flags & SEC_RELOC) == 0
5347 || s->reloc_count == 0
5348 || (s->flags & SEC_EXCLUDE) != 0
5349 || ((info->strip == strip_all
5350 || info->strip == strip_debugger)
5351 && (s->flags & SEC_DEBUGGING) != 0))
5352 continue;
5353
5354 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5355 NULL,
5356 info->keep_memory);
5357 if (internal_relocs == NULL)
5358 goto error_free_vers;
5359
5360 rel = internal_relocs;
5361 relend = rel + s->reloc_count;
5362 for ( ; rel < relend; rel++)
5363 {
5364 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5365 struct elf_link_hash_entry *h;
5366
5367 /* Skip local symbols. */
5368 if (r_symndx < extsymoff)
5369 continue;
5370
5371 h = sym_hash[r_symndx - extsymoff];
5372 if (h != NULL)
5373 h->root.non_ir_ref_regular = 1;
5374 }
5375
5376 if (elf_section_data (s)->relocs != internal_relocs)
5377 free (internal_relocs);
5378 }
5379 }
5380
c9594989
AM
5381 free (extversym);
5382 extversym = NULL;
5383 free (isymbuf);
5384 isymbuf = NULL;
66eb6687
AM
5385
5386 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5387 {
5388 unsigned int i;
5389
5390 /* Restore the symbol table. */
f45794cb
AM
5391 old_ent = (char *) old_tab + tabsize;
5392 memset (elf_sym_hashes (abfd), 0,
5393 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
5394 htab->root.table.table = old_table;
5395 htab->root.table.size = old_size;
5396 htab->root.table.count = old_count;
66eb6687 5397 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
5398 htab->root.undefs = old_undefs;
5399 htab->root.undefs_tail = old_undefs_tail;
e310298c
AM
5400 if (htab->dynstr != NULL)
5401 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5b677558
AM
5402 free (old_strtab);
5403 old_strtab = NULL;
66eb6687
AM
5404 for (i = 0; i < htab->root.table.size; i++)
5405 {
5406 struct bfd_hash_entry *p;
5407 struct elf_link_hash_entry *h;
4070765b 5408 unsigned int non_ir_ref_dynamic;
66eb6687
AM
5409
5410 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5411 {
4070765b 5412 /* Preserve non_ir_ref_dynamic so that this symbol
59fa66c5
L
5413 will be exported when the dynamic lib becomes needed
5414 in the second pass. */
7ba11550
AM
5415 h = (struct elf_link_hash_entry *) p;
5416 if (h->root.type == bfd_link_hash_warning)
5417 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4070765b 5418 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
7ba11550 5419
2de92251 5420 h = (struct elf_link_hash_entry *) p;
7ba11550
AM
5421 memcpy (h, old_ent, htab->root.table.entsize);
5422 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
5423 if (h->root.type == bfd_link_hash_warning)
5424 {
a4542f1b 5425 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7ba11550
AM
5426 memcpy (h, old_ent, htab->root.table.entsize);
5427 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251 5428 }
a4542f1b 5429 if (h->root.type == bfd_link_hash_common)
3e0882af 5430 {
7ba11550
AM
5431 memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5432 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
3e0882af 5433 }
4070765b 5434 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
66eb6687
AM
5435 }
5436 }
5437
5061a885
AM
5438 /* Make a special call to the linker "notice" function to
5439 tell it that symbols added for crefs may need to be removed. */
e5034e59 5440 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 5441 goto error_free_vers;
5061a885 5442
66eb6687
AM
5443 free (old_tab);
5444 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5445 alloc_mark);
c9594989 5446 free (nondeflt_vers);
66eb6687
AM
5447 return TRUE;
5448 }
2de92251 5449
66eb6687
AM
5450 if (old_tab != NULL)
5451 {
e5034e59 5452 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 5453 goto error_free_vers;
66eb6687
AM
5454 free (old_tab);
5455 old_tab = NULL;
5456 }
5457
c6e8a9a8
L
5458 /* Now that all the symbols from this input file are created, if
5459 not performing a relocatable link, handle .symver foo, foo@BAR
5460 such that any relocs against foo become foo@BAR. */
0e1862bb 5461 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5 5462 {
ef53be89 5463 size_t cnt, symidx;
4ad4eba5
AM
5464
5465 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5466 {
5467 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5468 char *shortname, *p;
986f0783 5469 size_t amt;
4ad4eba5
AM
5470
5471 p = strchr (h->root.root.string, ELF_VER_CHR);
5472 if (p == NULL
5473 || (h->root.type != bfd_link_hash_defined
5474 && h->root.type != bfd_link_hash_defweak))
5475 continue;
5476
5477 amt = p - h->root.root.string;
a50b1753 5478 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
5479 if (!shortname)
5480 goto error_free_vers;
4ad4eba5
AM
5481 memcpy (shortname, h->root.root.string, amt);
5482 shortname[amt] = '\0';
5483
5484 hi = (struct elf_link_hash_entry *)
66eb6687 5485 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
5486 FALSE, FALSE, FALSE);
5487 if (hi != NULL
5488 && hi->root.type == h->root.type
5489 && hi->root.u.def.value == h->root.u.def.value
5490 && hi->root.u.def.section == h->root.u.def.section)
5491 {
5492 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5493 hi->root.type = bfd_link_hash_indirect;
5494 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 5495 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
5496 sym_hash = elf_sym_hashes (abfd);
5497 if (sym_hash)
5498 for (symidx = 0; symidx < extsymcount; ++symidx)
5499 if (sym_hash[symidx] == hi)
5500 {
5501 sym_hash[symidx] = h;
5502 break;
5503 }
5504 }
5505 free (shortname);
5506 }
5507 free (nondeflt_vers);
5508 nondeflt_vers = NULL;
5509 }
5510
60d67dc8 5511 /* Now set the alias field correctly for all the weak defined
4ad4eba5
AM
5512 symbols we found. The only way to do this is to search all the
5513 symbols. Since we only need the information for non functions in
5514 dynamic objects, that's the only time we actually put anything on
5515 the list WEAKS. We need this information so that if a regular
5516 object refers to a symbol defined weakly in a dynamic object, the
5517 real symbol in the dynamic object is also put in the dynamic
5518 symbols; we also must arrange for both symbols to point to the
5519 same memory location. We could handle the general case of symbol
5520 aliasing, but a general symbol alias can only be generated in
5521 assembler code, handling it correctly would be very time
5522 consuming, and other ELF linkers don't handle general aliasing
5523 either. */
5524 if (weaks != NULL)
5525 {
5526 struct elf_link_hash_entry **hpp;
5527 struct elf_link_hash_entry **hppend;
5528 struct elf_link_hash_entry **sorted_sym_hash;
5529 struct elf_link_hash_entry *h;
986f0783 5530 size_t sym_count, amt;
4ad4eba5
AM
5531
5532 /* Since we have to search the whole symbol list for each weak
5533 defined symbol, search time for N weak defined symbols will be
5534 O(N^2). Binary search will cut it down to O(NlogN). */
986f0783 5535 amt = extsymcount * sizeof (*sorted_sym_hash);
3a3f4bf7 5536 sorted_sym_hash = bfd_malloc (amt);
4ad4eba5
AM
5537 if (sorted_sym_hash == NULL)
5538 goto error_return;
5539 sym_hash = sorted_sym_hash;
5540 hpp = elf_sym_hashes (abfd);
5541 hppend = hpp + extsymcount;
5542 sym_count = 0;
5543 for (; hpp < hppend; hpp++)
5544 {
5545 h = *hpp;
5546 if (h != NULL
5547 && h->root.type == bfd_link_hash_defined
fcb93ecf 5548 && !bed->is_function_type (h->type))
4ad4eba5
AM
5549 {
5550 *sym_hash = h;
5551 sym_hash++;
5552 sym_count++;
5553 }
5554 }
5555
3a3f4bf7 5556 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
4ad4eba5
AM
5557 elf_sort_symbol);
5558
5559 while (weaks != NULL)
5560 {
5561 struct elf_link_hash_entry *hlook;
5562 asection *slook;
5563 bfd_vma vlook;
ed54588d 5564 size_t i, j, idx = 0;
4ad4eba5
AM
5565
5566 hlook = weaks;
60d67dc8
AM
5567 weaks = hlook->u.alias;
5568 hlook->u.alias = NULL;
4ad4eba5 5569
e3e53eed
AM
5570 if (hlook->root.type != bfd_link_hash_defined
5571 && hlook->root.type != bfd_link_hash_defweak)
5572 continue;
5573
4ad4eba5
AM
5574 slook = hlook->root.u.def.section;
5575 vlook = hlook->root.u.def.value;
5576
4ad4eba5
AM
5577 i = 0;
5578 j = sym_count;
14160578 5579 while (i != j)
4ad4eba5
AM
5580 {
5581 bfd_signed_vma vdiff;
5582 idx = (i + j) / 2;
14160578 5583 h = sorted_sym_hash[idx];
4ad4eba5
AM
5584 vdiff = vlook - h->root.u.def.value;
5585 if (vdiff < 0)
5586 j = idx;
5587 else if (vdiff > 0)
5588 i = idx + 1;
5589 else
5590 {
d3435ae8 5591 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
5592 if (sdiff < 0)
5593 j = idx;
5594 else if (sdiff > 0)
5595 i = idx + 1;
5596 else
14160578 5597 break;
4ad4eba5
AM
5598 }
5599 }
5600
5601 /* We didn't find a value/section match. */
14160578 5602 if (i == j)
4ad4eba5
AM
5603 continue;
5604
14160578
AM
5605 /* With multiple aliases, or when the weak symbol is already
5606 strongly defined, we have multiple matching symbols and
5607 the binary search above may land on any of them. Step
5608 one past the matching symbol(s). */
5609 while (++idx != j)
5610 {
5611 h = sorted_sym_hash[idx];
5612 if (h->root.u.def.section != slook
5613 || h->root.u.def.value != vlook)
5614 break;
5615 }
5616
5617 /* Now look back over the aliases. Since we sorted by size
5618 as well as value and section, we'll choose the one with
5619 the largest size. */
5620 while (idx-- != i)
4ad4eba5 5621 {
14160578 5622 h = sorted_sym_hash[idx];
4ad4eba5
AM
5623
5624 /* Stop if value or section doesn't match. */
14160578
AM
5625 if (h->root.u.def.section != slook
5626 || h->root.u.def.value != vlook)
4ad4eba5
AM
5627 break;
5628 else if (h != hlook)
5629 {
60d67dc8
AM
5630 struct elf_link_hash_entry *t;
5631
5632 hlook->u.alias = h;
5633 hlook->is_weakalias = 1;
5634 t = h;
5635 if (t->u.alias != NULL)
5636 while (t->u.alias != h)
5637 t = t->u.alias;
5638 t->u.alias = hlook;
4ad4eba5
AM
5639
5640 /* If the weak definition is in the list of dynamic
5641 symbols, make sure the real definition is put
5642 there as well. */
5643 if (hlook->dynindx != -1 && h->dynindx == -1)
5644 {
c152c796 5645 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
5646 {
5647 err_free_sym_hash:
5648 free (sorted_sym_hash);
5649 goto error_return;
5650 }
4ad4eba5
AM
5651 }
5652
5653 /* If the real definition is in the list of dynamic
5654 symbols, make sure the weak definition is put
5655 there as well. If we don't do this, then the
5656 dynamic loader might not merge the entries for the
5657 real definition and the weak definition. */
5658 if (h->dynindx != -1 && hlook->dynindx == -1)
5659 {
c152c796 5660 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 5661 goto err_free_sym_hash;
4ad4eba5
AM
5662 }
5663 break;
5664 }
5665 }
5666 }
5667
5668 free (sorted_sym_hash);
5669 }
5670
33177bb1
AM
5671 if (bed->check_directives
5672 && !(*bed->check_directives) (abfd, info))
5673 return FALSE;
85fbca6a 5674
4ad4eba5
AM
5675 /* If this is a non-traditional link, try to optimize the handling
5676 of the .stab/.stabstr sections. */
5677 if (! dynamic
5678 && ! info->traditional_format
66eb6687 5679 && is_elf_hash_table (htab)
4ad4eba5
AM
5680 && (info->strip != strip_all && info->strip != strip_debugger))
5681 {
5682 asection *stabstr;
5683
5684 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5685 if (stabstr != NULL)
5686 {
5687 bfd_size_type string_offset = 0;
5688 asection *stab;
5689
5690 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 5691 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
5692 && (!stab->name[5] ||
5693 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5694 && (stab->flags & SEC_MERGE) == 0
5695 && !bfd_is_abs_section (stab->output_section))
5696 {
5697 struct bfd_elf_section_data *secdata;
5698
5699 secdata = elf_section_data (stab);
66eb6687
AM
5700 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5701 stabstr, &secdata->sec_info,
4ad4eba5
AM
5702 &string_offset))
5703 goto error_return;
5704 if (secdata->sec_info)
dbaa2011 5705 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5706 }
5707 }
5708 }
5709
e310298c 5710 if (dynamic && add_needed)
4ad4eba5
AM
5711 {
5712 /* Add this bfd to the loaded list. */
5713 struct elf_link_loaded_list *n;
5714
ca4be51c 5715 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5716 if (n == NULL)
5717 goto error_return;
5718 n->abfd = abfd;
e310298c
AM
5719 n->next = htab->dyn_loaded;
5720 htab->dyn_loaded = n;
4ad4eba5 5721 }
e310298c
AM
5722 if (dynamic && !add_needed
5723 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
5724 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
4ad4eba5
AM
5725
5726 return TRUE;
5727
5728 error_free_vers:
c9594989
AM
5729 free (old_tab);
5730 free (old_strtab);
5731 free (nondeflt_vers);
5732 free (extversym);
4ad4eba5 5733 error_free_sym:
c9594989 5734 free (isymbuf);
4ad4eba5
AM
5735 error_return:
5736 return FALSE;
5737}
5738
8387904d
AM
5739/* Return the linker hash table entry of a symbol that might be
5740 satisfied by an archive symbol. Return -1 on error. */
5741
5742struct elf_link_hash_entry *
5743_bfd_elf_archive_symbol_lookup (bfd *abfd,
5744 struct bfd_link_info *info,
5745 const char *name)
5746{
5747 struct elf_link_hash_entry *h;
5748 char *p, *copy;
5749 size_t len, first;
5750
2a41f396 5751 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5752 if (h != NULL)
5753 return h;
5754
5755 /* If this is a default version (the name contains @@), look up the
5756 symbol again with only one `@' as well as without the version.
5757 The effect is that references to the symbol with and without the
5758 version will be matched by the default symbol in the archive. */
5759
5760 p = strchr (name, ELF_VER_CHR);
5761 if (p == NULL || p[1] != ELF_VER_CHR)
5762 return h;
5763
5764 /* First check with only one `@'. */
5765 len = strlen (name);
a50b1753 5766 copy = (char *) bfd_alloc (abfd, len);
8387904d 5767 if (copy == NULL)
e99955cd 5768 return (struct elf_link_hash_entry *) -1;
8387904d
AM
5769
5770 first = p - name + 1;
5771 memcpy (copy, name, first);
5772 memcpy (copy + first, name + first + 1, len - first);
5773
2a41f396 5774 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5775 if (h == NULL)
5776 {
5777 /* We also need to check references to the symbol without the
5778 version. */
5779 copy[first - 1] = '\0';
5780 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5781 FALSE, FALSE, TRUE);
8387904d
AM
5782 }
5783
5784 bfd_release (abfd, copy);
5785 return h;
5786}
5787
0ad989f9 5788/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5789 don't use _bfd_generic_link_add_archive_symbols because we need to
5790 handle versioned symbols.
0ad989f9
L
5791
5792 Fortunately, ELF archive handling is simpler than that done by
5793 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5794 oddities. In ELF, if we find a symbol in the archive map, and the
5795 symbol is currently undefined, we know that we must pull in that
5796 object file.
5797
5798 Unfortunately, we do have to make multiple passes over the symbol
5799 table until nothing further is resolved. */
5800
4ad4eba5
AM
5801static bfd_boolean
5802elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5803{
5804 symindex c;
13e570f8 5805 unsigned char *included = NULL;
0ad989f9
L
5806 carsym *symdefs;
5807 bfd_boolean loop;
986f0783 5808 size_t amt;
8387904d
AM
5809 const struct elf_backend_data *bed;
5810 struct elf_link_hash_entry * (*archive_symbol_lookup)
5811 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5812
5813 if (! bfd_has_map (abfd))
5814 {
5815 /* An empty archive is a special case. */
5816 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5817 return TRUE;
5818 bfd_set_error (bfd_error_no_armap);
5819 return FALSE;
5820 }
5821
5822 /* Keep track of all symbols we know to be already defined, and all
5823 files we know to be already included. This is to speed up the
5824 second and subsequent passes. */
5825 c = bfd_ardata (abfd)->symdef_count;
5826 if (c == 0)
5827 return TRUE;
986f0783 5828 amt = c * sizeof (*included);
13e570f8
AM
5829 included = (unsigned char *) bfd_zmalloc (amt);
5830 if (included == NULL)
5831 return FALSE;
0ad989f9
L
5832
5833 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5834 bed = get_elf_backend_data (abfd);
5835 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5836
5837 do
5838 {
5839 file_ptr last;
5840 symindex i;
5841 carsym *symdef;
5842 carsym *symdefend;
5843
5844 loop = FALSE;
5845 last = -1;
5846
5847 symdef = symdefs;
5848 symdefend = symdef + c;
5849 for (i = 0; symdef < symdefend; symdef++, i++)
5850 {
5851 struct elf_link_hash_entry *h;
5852 bfd *element;
5853 struct bfd_link_hash_entry *undefs_tail;
5854 symindex mark;
5855
13e570f8 5856 if (included[i])
0ad989f9
L
5857 continue;
5858 if (symdef->file_offset == last)
5859 {
5860 included[i] = TRUE;
5861 continue;
5862 }
5863
8387904d 5864 h = archive_symbol_lookup (abfd, info, symdef->name);
e99955cd 5865 if (h == (struct elf_link_hash_entry *) -1)
8387904d 5866 goto error_return;
0ad989f9
L
5867
5868 if (h == NULL)
5869 continue;
5870
75cfe082
AM
5871 if (h->root.type == bfd_link_hash_undefined)
5872 {
5873 /* If the archive element has already been loaded then one
5874 of the symbols defined by that element might have been
5875 made undefined due to being in a discarded section. */
5876 if (h->indx == -3)
5877 continue;
5878 }
5879 else if (h->root.type == bfd_link_hash_common)
0ad989f9
L
5880 {
5881 /* We currently have a common symbol. The archive map contains
5882 a reference to this symbol, so we may want to include it. We
5883 only want to include it however, if this archive element
5884 contains a definition of the symbol, not just another common
5885 declaration of it.
5886
5887 Unfortunately some archivers (including GNU ar) will put
5888 declarations of common symbols into their archive maps, as
5889 well as real definitions, so we cannot just go by the archive
5890 map alone. Instead we must read in the element's symbol
5891 table and check that to see what kind of symbol definition
5892 this is. */
5893 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5894 continue;
5895 }
75cfe082 5896 else
0ad989f9
L
5897 {
5898 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5899 /* Symbol must be defined. Don't check it again. */
5900 included[i] = TRUE;
0ad989f9
L
5901 continue;
5902 }
5903
5904 /* We need to include this archive member. */
5905 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5906 if (element == NULL)
5907 goto error_return;
5908
5909 if (! bfd_check_format (element, bfd_object))
5910 goto error_return;
5911
0ad989f9
L
5912 undefs_tail = info->hash->undefs_tail;
5913
0e144ba7
AM
5914 if (!(*info->callbacks
5915 ->add_archive_element) (info, element, symdef->name, &element))
b95a0a31 5916 continue;
0e144ba7 5917 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5918 goto error_return;
5919
5920 /* If there are any new undefined symbols, we need to make
5921 another pass through the archive in order to see whether
5922 they can be defined. FIXME: This isn't perfect, because
5923 common symbols wind up on undefs_tail and because an
5924 undefined symbol which is defined later on in this pass
5925 does not require another pass. This isn't a bug, but it
5926 does make the code less efficient than it could be. */
5927 if (undefs_tail != info->hash->undefs_tail)
5928 loop = TRUE;
5929
5930 /* Look backward to mark all symbols from this object file
5931 which we have already seen in this pass. */
5932 mark = i;
5933 do
5934 {
5935 included[mark] = TRUE;
5936 if (mark == 0)
5937 break;
5938 --mark;
5939 }
5940 while (symdefs[mark].file_offset == symdef->file_offset);
5941
5942 /* We mark subsequent symbols from this object file as we go
5943 on through the loop. */
5944 last = symdef->file_offset;
5945 }
5946 }
5947 while (loop);
5948
0ad989f9 5949 free (included);
0ad989f9
L
5950 return TRUE;
5951
5952 error_return:
c9594989 5953 free (included);
0ad989f9
L
5954 return FALSE;
5955}
4ad4eba5
AM
5956
5957/* Given an ELF BFD, add symbols to the global hash table as
5958 appropriate. */
5959
5960bfd_boolean
5961bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5962{
5963 switch (bfd_get_format (abfd))
5964 {
5965 case bfd_object:
5966 return elf_link_add_object_symbols (abfd, info);
5967 case bfd_archive:
5968 return elf_link_add_archive_symbols (abfd, info);
5969 default:
5970 bfd_set_error (bfd_error_wrong_format);
5971 return FALSE;
5972 }
5973}
5a580b3a 5974\f
14b1c01e
AM
5975struct hash_codes_info
5976{
5977 unsigned long *hashcodes;
5978 bfd_boolean error;
5979};
a0c8462f 5980
5a580b3a
AM
5981/* This function will be called though elf_link_hash_traverse to store
5982 all hash value of the exported symbols in an array. */
5983
5984static bfd_boolean
5985elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5986{
a50b1753 5987 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5988 const char *name;
5a580b3a
AM
5989 unsigned long ha;
5990 char *alc = NULL;
5991
5a580b3a
AM
5992 /* Ignore indirect symbols. These are added by the versioning code. */
5993 if (h->dynindx == -1)
5994 return TRUE;
5995
5996 name = h->root.root.string;
422f1182 5997 if (h->versioned >= versioned)
5a580b3a 5998 {
422f1182
L
5999 char *p = strchr (name, ELF_VER_CHR);
6000 if (p != NULL)
14b1c01e 6001 {
422f1182
L
6002 alc = (char *) bfd_malloc (p - name + 1);
6003 if (alc == NULL)
6004 {
6005 inf->error = TRUE;
6006 return FALSE;
6007 }
6008 memcpy (alc, name, p - name);
6009 alc[p - name] = '\0';
6010 name = alc;
14b1c01e 6011 }
5a580b3a
AM
6012 }
6013
6014 /* Compute the hash value. */
6015 ha = bfd_elf_hash (name);
6016
6017 /* Store the found hash value in the array given as the argument. */
14b1c01e 6018 *(inf->hashcodes)++ = ha;
5a580b3a
AM
6019
6020 /* And store it in the struct so that we can put it in the hash table
6021 later. */
f6e332e6 6022 h->u.elf_hash_value = ha;
5a580b3a 6023
c9594989 6024 free (alc);
5a580b3a
AM
6025 return TRUE;
6026}
6027
fdc90cb4
JJ
6028struct collect_gnu_hash_codes
6029{
6030 bfd *output_bfd;
6031 const struct elf_backend_data *bed;
6032 unsigned long int nsyms;
6033 unsigned long int maskbits;
6034 unsigned long int *hashcodes;
6035 unsigned long int *hashval;
6036 unsigned long int *indx;
6037 unsigned long int *counts;
6038 bfd_vma *bitmask;
6039 bfd_byte *contents;
f16a9783 6040 bfd_size_type xlat;
fdc90cb4
JJ
6041 long int min_dynindx;
6042 unsigned long int bucketcount;
6043 unsigned long int symindx;
6044 long int local_indx;
6045 long int shift1, shift2;
6046 unsigned long int mask;
14b1c01e 6047 bfd_boolean error;
fdc90cb4
JJ
6048};
6049
6050/* This function will be called though elf_link_hash_traverse to store
6051 all hash value of the exported symbols in an array. */
6052
6053static bfd_boolean
6054elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6055{
a50b1753 6056 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 6057 const char *name;
fdc90cb4
JJ
6058 unsigned long ha;
6059 char *alc = NULL;
6060
fdc90cb4
JJ
6061 /* Ignore indirect symbols. These are added by the versioning code. */
6062 if (h->dynindx == -1)
6063 return TRUE;
6064
6065 /* Ignore also local symbols and undefined symbols. */
6066 if (! (*s->bed->elf_hash_symbol) (h))
6067 return TRUE;
6068
6069 name = h->root.root.string;
422f1182 6070 if (h->versioned >= versioned)
fdc90cb4 6071 {
422f1182
L
6072 char *p = strchr (name, ELF_VER_CHR);
6073 if (p != NULL)
14b1c01e 6074 {
422f1182
L
6075 alc = (char *) bfd_malloc (p - name + 1);
6076 if (alc == NULL)
6077 {
6078 s->error = TRUE;
6079 return FALSE;
6080 }
6081 memcpy (alc, name, p - name);
6082 alc[p - name] = '\0';
6083 name = alc;
14b1c01e 6084 }
fdc90cb4
JJ
6085 }
6086
6087 /* Compute the hash value. */
6088 ha = bfd_elf_gnu_hash (name);
6089
6090 /* Store the found hash value in the array for compute_bucket_count,
6091 and also for .dynsym reordering purposes. */
6092 s->hashcodes[s->nsyms] = ha;
6093 s->hashval[h->dynindx] = ha;
6094 ++s->nsyms;
6095 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6096 s->min_dynindx = h->dynindx;
6097
c9594989 6098 free (alc);
fdc90cb4
JJ
6099 return TRUE;
6100}
6101
6102/* This function will be called though elf_link_hash_traverse to do
f16a9783
MS
6103 final dynamic symbol renumbering in case of .gnu.hash.
6104 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6105 to the translation table. */
fdc90cb4
JJ
6106
6107static bfd_boolean
f16a9783 6108elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
fdc90cb4 6109{
a50b1753 6110 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
6111 unsigned long int bucket;
6112 unsigned long int val;
6113
fdc90cb4
JJ
6114 /* Ignore indirect symbols. */
6115 if (h->dynindx == -1)
6116 return TRUE;
6117
6118 /* Ignore also local symbols and undefined symbols. */
6119 if (! (*s->bed->elf_hash_symbol) (h))
6120 {
6121 if (h->dynindx >= s->min_dynindx)
f16a9783
MS
6122 {
6123 if (s->bed->record_xhash_symbol != NULL)
6124 {
6125 (*s->bed->record_xhash_symbol) (h, 0);
6126 s->local_indx++;
6127 }
6128 else
6129 h->dynindx = s->local_indx++;
6130 }
fdc90cb4
JJ
6131 return TRUE;
6132 }
6133
6134 bucket = s->hashval[h->dynindx] % s->bucketcount;
6135 val = (s->hashval[h->dynindx] >> s->shift1)
6136 & ((s->maskbits >> s->shift1) - 1);
6137 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6138 s->bitmask[val]
6139 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6140 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6141 if (s->counts[bucket] == 1)
6142 /* Last element terminates the chain. */
6143 val |= 1;
6144 bfd_put_32 (s->output_bfd, val,
6145 s->contents + (s->indx[bucket] - s->symindx) * 4);
6146 --s->counts[bucket];
f16a9783
MS
6147 if (s->bed->record_xhash_symbol != NULL)
6148 {
6149 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6150
6151 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6152 }
6153 else
6154 h->dynindx = s->indx[bucket]++;
fdc90cb4
JJ
6155 return TRUE;
6156}
6157
6158/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6159
6160bfd_boolean
6161_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6162{
6163 return !(h->forced_local
6164 || h->root.type == bfd_link_hash_undefined
6165 || h->root.type == bfd_link_hash_undefweak
6166 || ((h->root.type == bfd_link_hash_defined
6167 || h->root.type == bfd_link_hash_defweak)
6168 && h->root.u.def.section->output_section == NULL));
6169}
6170
5a580b3a
AM
6171/* Array used to determine the number of hash table buckets to use
6172 based on the number of symbols there are. If there are fewer than
6173 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6174 fewer than 37 we use 17 buckets, and so forth. We never use more
6175 than 32771 buckets. */
6176
6177static const size_t elf_buckets[] =
6178{
6179 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6180 16411, 32771, 0
6181};
6182
6183/* Compute bucket count for hashing table. We do not use a static set
6184 of possible tables sizes anymore. Instead we determine for all
6185 possible reasonable sizes of the table the outcome (i.e., the
6186 number of collisions etc) and choose the best solution. The
6187 weighting functions are not too simple to allow the table to grow
6188 without bounds. Instead one of the weighting factors is the size.
6189 Therefore the result is always a good payoff between few collisions
6190 (= short chain lengths) and table size. */
6191static size_t
b20dd2ce 6192compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
6193 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6194 unsigned long int nsyms,
6195 int gnu_hash)
5a580b3a 6196{
5a580b3a 6197 size_t best_size = 0;
5a580b3a 6198 unsigned long int i;
5a580b3a 6199
5a580b3a
AM
6200 /* We have a problem here. The following code to optimize the table
6201 size requires an integer type with more the 32 bits. If
6202 BFD_HOST_U_64_BIT is set we know about such a type. */
6203#ifdef BFD_HOST_U_64_BIT
6204 if (info->optimize)
6205 {
5a580b3a
AM
6206 size_t minsize;
6207 size_t maxsize;
6208 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 6209 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 6210 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 6211 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 6212 unsigned long int *counts;
d40f3da9 6213 bfd_size_type amt;
0883b6e0 6214 unsigned int no_improvement_count = 0;
5a580b3a
AM
6215
6216 /* Possible optimization parameters: if we have NSYMS symbols we say
6217 that the hashing table must at least have NSYMS/4 and at most
6218 2*NSYMS buckets. */
6219 minsize = nsyms / 4;
6220 if (minsize == 0)
6221 minsize = 1;
6222 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
6223 if (gnu_hash)
6224 {
6225 if (minsize < 2)
6226 minsize = 2;
6227 if ((best_size & 31) == 0)
6228 ++best_size;
6229 }
5a580b3a
AM
6230
6231 /* Create array where we count the collisions in. We must use bfd_malloc
6232 since the size could be large. */
6233 amt = maxsize;
6234 amt *= sizeof (unsigned long int);
a50b1753 6235 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 6236 if (counts == NULL)
fdc90cb4 6237 return 0;
5a580b3a
AM
6238
6239 /* Compute the "optimal" size for the hash table. The criteria is a
6240 minimal chain length. The minor criteria is (of course) the size
6241 of the table. */
6242 for (i = minsize; i < maxsize; ++i)
6243 {
6244 /* Walk through the array of hashcodes and count the collisions. */
6245 BFD_HOST_U_64_BIT max;
6246 unsigned long int j;
6247 unsigned long int fact;
6248
fdc90cb4
JJ
6249 if (gnu_hash && (i & 31) == 0)
6250 continue;
6251
5a580b3a
AM
6252 memset (counts, '\0', i * sizeof (unsigned long int));
6253
6254 /* Determine how often each hash bucket is used. */
6255 for (j = 0; j < nsyms; ++j)
6256 ++counts[hashcodes[j] % i];
6257
6258 /* For the weight function we need some information about the
6259 pagesize on the target. This is information need not be 100%
6260 accurate. Since this information is not available (so far) we
6261 define it here to a reasonable default value. If it is crucial
6262 to have a better value some day simply define this value. */
6263# ifndef BFD_TARGET_PAGESIZE
6264# define BFD_TARGET_PAGESIZE (4096)
6265# endif
6266
fdc90cb4
JJ
6267 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6268 and the chains. */
6269 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
6270
6271# if 1
6272 /* Variant 1: optimize for short chains. We add the squares
6273 of all the chain lengths (which favors many small chain
6274 over a few long chains). */
6275 for (j = 0; j < i; ++j)
6276 max += counts[j] * counts[j];
6277
6278 /* This adds penalties for the overall size of the table. */
fdc90cb4 6279 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6280 max *= fact * fact;
6281# else
6282 /* Variant 2: Optimize a lot more for small table. Here we
6283 also add squares of the size but we also add penalties for
6284 empty slots (the +1 term). */
6285 for (j = 0; j < i; ++j)
6286 max += (1 + counts[j]) * (1 + counts[j]);
6287
6288 /* The overall size of the table is considered, but not as
6289 strong as in variant 1, where it is squared. */
fdc90cb4 6290 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6291 max *= fact;
6292# endif
6293
6294 /* Compare with current best results. */
6295 if (max < best_chlen)
6296 {
6297 best_chlen = max;
6298 best_size = i;
ca4be51c 6299 no_improvement_count = 0;
5a580b3a 6300 }
0883b6e0
NC
6301 /* PR 11843: Avoid futile long searches for the best bucket size
6302 when there are a large number of symbols. */
6303 else if (++no_improvement_count == 100)
6304 break;
5a580b3a
AM
6305 }
6306
6307 free (counts);
6308 }
6309 else
6310#endif /* defined (BFD_HOST_U_64_BIT) */
6311 {
6312 /* This is the fallback solution if no 64bit type is available or if we
6313 are not supposed to spend much time on optimizations. We select the
6314 bucket count using a fixed set of numbers. */
6315 for (i = 0; elf_buckets[i] != 0; i++)
6316 {
6317 best_size = elf_buckets[i];
fdc90cb4 6318 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
6319 break;
6320 }
fdc90cb4
JJ
6321 if (gnu_hash && best_size < 2)
6322 best_size = 2;
5a580b3a
AM
6323 }
6324
5a580b3a
AM
6325 return best_size;
6326}
6327
d0bf826b
AM
6328/* Size any SHT_GROUP section for ld -r. */
6329
6330bfd_boolean
6331_bfd_elf_size_group_sections (struct bfd_link_info *info)
6332{
6333 bfd *ibfd;
57963c05 6334 asection *s;
d0bf826b 6335
c72f2fb2 6336 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b 6337 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
57963c05
AM
6338 && (s = ibfd->sections) != NULL
6339 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
d0bf826b
AM
6340 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6341 return FALSE;
6342 return TRUE;
6343}
6344
04c3a755
NS
6345/* Set a default stack segment size. The value in INFO wins. If it
6346 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6347 undefined it is initialized. */
6348
6349bfd_boolean
6350bfd_elf_stack_segment_size (bfd *output_bfd,
6351 struct bfd_link_info *info,
6352 const char *legacy_symbol,
6353 bfd_vma default_size)
6354{
6355 struct elf_link_hash_entry *h = NULL;
6356
6357 /* Look for legacy symbol. */
6358 if (legacy_symbol)
6359 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6360 FALSE, FALSE, FALSE);
6361 if (h && (h->root.type == bfd_link_hash_defined
6362 || h->root.type == bfd_link_hash_defweak)
6363 && h->def_regular
6364 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6365 {
6366 /* The symbol has no type if specified on the command line. */
6367 h->type = STT_OBJECT;
6368 if (info->stacksize)
695344c0 6369 /* xgettext:c-format */
871b3ab2 6370 _bfd_error_handler (_("%pB: stack size specified and %s set"),
4eca0228 6371 output_bfd, legacy_symbol);
04c3a755 6372 else if (h->root.u.def.section != bfd_abs_section_ptr)
695344c0 6373 /* xgettext:c-format */
871b3ab2 6374 _bfd_error_handler (_("%pB: %s not absolute"),
4eca0228 6375 output_bfd, legacy_symbol);
04c3a755
NS
6376 else
6377 info->stacksize = h->root.u.def.value;
6378 }
6379
6380 if (!info->stacksize)
6381 /* If the user didn't set a size, or explicitly inhibit the
6382 size, set it now. */
6383 info->stacksize = default_size;
6384
6385 /* Provide the legacy symbol, if it is referenced. */
6386 if (h && (h->root.type == bfd_link_hash_undefined
6387 || h->root.type == bfd_link_hash_undefweak))
6388 {
6389 struct bfd_link_hash_entry *bh = NULL;
6390
6391 if (!(_bfd_generic_link_add_one_symbol
6392 (info, output_bfd, legacy_symbol,
6393 BSF_GLOBAL, bfd_abs_section_ptr,
6394 info->stacksize >= 0 ? info->stacksize : 0,
6395 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6396 return FALSE;
6397
6398 h = (struct elf_link_hash_entry *) bh;
6399 h->def_regular = 1;
6400 h->type = STT_OBJECT;
6401 }
6402
6403 return TRUE;
6404}
6405
b531344c
MR
6406/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6407
6408struct elf_gc_sweep_symbol_info
6409{
6410 struct bfd_link_info *info;
6411 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6412 bfd_boolean);
6413};
6414
6415static bfd_boolean
6416elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6417{
6418 if (!h->mark
6419 && (((h->root.type == bfd_link_hash_defined
6420 || h->root.type == bfd_link_hash_defweak)
6421 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6422 && h->root.u.def.section->gc_mark))
6423 || h->root.type == bfd_link_hash_undefined
6424 || h->root.type == bfd_link_hash_undefweak))
6425 {
6426 struct elf_gc_sweep_symbol_info *inf;
6427
6428 inf = (struct elf_gc_sweep_symbol_info *) data;
6429 (*inf->hide_symbol) (inf->info, h, TRUE);
6430 h->def_regular = 0;
6431 h->ref_regular = 0;
6432 h->ref_regular_nonweak = 0;
6433 }
6434
6435 return TRUE;
6436}
6437
5a580b3a
AM
6438/* Set up the sizes and contents of the ELF dynamic sections. This is
6439 called by the ELF linker emulation before_allocation routine. We
6440 must set the sizes of the sections before the linker sets the
6441 addresses of the various sections. */
6442
6443bfd_boolean
6444bfd_elf_size_dynamic_sections (bfd *output_bfd,
6445 const char *soname,
6446 const char *rpath,
6447 const char *filter_shlib,
7ee314fa
AM
6448 const char *audit,
6449 const char *depaudit,
5a580b3a
AM
6450 const char * const *auxiliary_filters,
6451 struct bfd_link_info *info,
fd91d419 6452 asection **sinterpptr)
5a580b3a 6453{
5a580b3a
AM
6454 bfd *dynobj;
6455 const struct elf_backend_data *bed;
5a580b3a
AM
6456
6457 *sinterpptr = NULL;
6458
5a580b3a
AM
6459 if (!is_elf_hash_table (info->hash))
6460 return TRUE;
6461
5a580b3a
AM
6462 dynobj = elf_hash_table (info)->dynobj;
6463
9a2a56cc 6464 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 6465 {
902e9fc7
MR
6466 struct bfd_elf_version_tree *verdefs;
6467 struct elf_info_failed asvinfo;
5a580b3a
AM
6468 struct bfd_elf_version_tree *t;
6469 struct bfd_elf_version_expr *d;
902e9fc7 6470 asection *s;
e6699019 6471 size_t soname_indx;
7ee314fa 6472
5a580b3a
AM
6473 /* If we are supposed to export all symbols into the dynamic symbol
6474 table (this is not the normal case), then do so. */
55255dae 6475 if (info->export_dynamic
0e1862bb 6476 || (bfd_link_executable (info) && info->dynamic))
5a580b3a 6477 {
3d13f3e9
AM
6478 struct elf_info_failed eif;
6479
6480 eif.info = info;
6481 eif.failed = FALSE;
5a580b3a
AM
6482 elf_link_hash_traverse (elf_hash_table (info),
6483 _bfd_elf_export_symbol,
6484 &eif);
6485 if (eif.failed)
6486 return FALSE;
6487 }
6488
e6699019
L
6489 if (soname != NULL)
6490 {
6491 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6492 soname, TRUE);
6493 if (soname_indx == (size_t) -1
6494 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6495 return FALSE;
6496 }
6497 else
6498 soname_indx = (size_t) -1;
6499
5a580b3a 6500 /* Make all global versions with definition. */
fd91d419 6501 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6502 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6503 if (!d->symver && d->literal)
5a580b3a
AM
6504 {
6505 const char *verstr, *name;
6506 size_t namelen, verlen, newlen;
93252b1c 6507 char *newname, *p, leading_char;
5a580b3a
AM
6508 struct elf_link_hash_entry *newh;
6509
93252b1c 6510 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 6511 name = d->pattern;
93252b1c 6512 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
6513 verstr = t->name;
6514 verlen = strlen (verstr);
6515 newlen = namelen + verlen + 3;
6516
a50b1753 6517 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
6518 if (newname == NULL)
6519 return FALSE;
93252b1c
MF
6520 newname[0] = leading_char;
6521 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
6522
6523 /* Check the hidden versioned definition. */
6524 p = newname + namelen;
6525 *p++ = ELF_VER_CHR;
6526 memcpy (p, verstr, verlen + 1);
6527 newh = elf_link_hash_lookup (elf_hash_table (info),
6528 newname, FALSE, FALSE,
6529 FALSE);
6530 if (newh == NULL
6531 || (newh->root.type != bfd_link_hash_defined
6532 && newh->root.type != bfd_link_hash_defweak))
6533 {
6534 /* Check the default versioned definition. */
6535 *p++ = ELF_VER_CHR;
6536 memcpy (p, verstr, verlen + 1);
6537 newh = elf_link_hash_lookup (elf_hash_table (info),
6538 newname, FALSE, FALSE,
6539 FALSE);
6540 }
6541 free (newname);
6542
6543 /* Mark this version if there is a definition and it is
6544 not defined in a shared object. */
6545 if (newh != NULL
f5385ebf 6546 && !newh->def_dynamic
5a580b3a
AM
6547 && (newh->root.type == bfd_link_hash_defined
6548 || newh->root.type == bfd_link_hash_defweak))
6549 d->symver = 1;
6550 }
6551
6552 /* Attach all the symbols to their version information. */
5a580b3a 6553 asvinfo.info = info;
5a580b3a
AM
6554 asvinfo.failed = FALSE;
6555
6556 elf_link_hash_traverse (elf_hash_table (info),
6557 _bfd_elf_link_assign_sym_version,
6558 &asvinfo);
6559 if (asvinfo.failed)
6560 return FALSE;
6561
6562 if (!info->allow_undefined_version)
6563 {
6564 /* Check if all global versions have a definition. */
3d13f3e9 6565 bfd_boolean all_defined = TRUE;
fd91d419 6566 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6567 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6568 if (d->literal && !d->symver && !d->script)
5a580b3a 6569 {
4eca0228 6570 _bfd_error_handler
5a580b3a
AM
6571 (_("%s: undefined version: %s"),
6572 d->pattern, t->name);
6573 all_defined = FALSE;
6574 }
6575
6576 if (!all_defined)
6577 {
6578 bfd_set_error (bfd_error_bad_value);
6579 return FALSE;
6580 }
6581 }
6582
902e9fc7
MR
6583 /* Set up the version definition section. */
6584 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6585 BFD_ASSERT (s != NULL);
5a580b3a 6586
902e9fc7
MR
6587 /* We may have created additional version definitions if we are
6588 just linking a regular application. */
6589 verdefs = info->version_info;
5a580b3a 6590
902e9fc7
MR
6591 /* Skip anonymous version tag. */
6592 if (verdefs != NULL && verdefs->vernum == 0)
6593 verdefs = verdefs->next;
5a580b3a 6594
902e9fc7
MR
6595 if (verdefs == NULL && !info->create_default_symver)
6596 s->flags |= SEC_EXCLUDE;
6597 else
5a580b3a 6598 {
902e9fc7
MR
6599 unsigned int cdefs;
6600 bfd_size_type size;
6601 bfd_byte *p;
6602 Elf_Internal_Verdef def;
6603 Elf_Internal_Verdaux defaux;
6604 struct bfd_link_hash_entry *bh;
6605 struct elf_link_hash_entry *h;
6606 const char *name;
5a580b3a 6607
902e9fc7
MR
6608 cdefs = 0;
6609 size = 0;
5a580b3a 6610
902e9fc7
MR
6611 /* Make space for the base version. */
6612 size += sizeof (Elf_External_Verdef);
6613 size += sizeof (Elf_External_Verdaux);
6614 ++cdefs;
6615
6616 /* Make space for the default version. */
6617 if (info->create_default_symver)
6618 {
6619 size += sizeof (Elf_External_Verdef);
6620 ++cdefs;
3e3b46e5
PB
6621 }
6622
5a580b3a
AM
6623 for (t = verdefs; t != NULL; t = t->next)
6624 {
6625 struct bfd_elf_version_deps *n;
6626
a6cc6b3b
RO
6627 /* Don't emit base version twice. */
6628 if (t->vernum == 0)
6629 continue;
6630
5a580b3a
AM
6631 size += sizeof (Elf_External_Verdef);
6632 size += sizeof (Elf_External_Verdaux);
6633 ++cdefs;
6634
6635 for (n = t->deps; n != NULL; n = n->next)
6636 size += sizeof (Elf_External_Verdaux);
6637 }
6638
eea6121a 6639 s->size = size;
a50b1753 6640 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6641 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6642 return FALSE;
6643
6644 /* Fill in the version definition section. */
6645
6646 p = s->contents;
6647
6648 def.vd_version = VER_DEF_CURRENT;
6649 def.vd_flags = VER_FLG_BASE;
6650 def.vd_ndx = 1;
6651 def.vd_cnt = 1;
3e3b46e5
PB
6652 if (info->create_default_symver)
6653 {
6654 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6655 def.vd_next = sizeof (Elf_External_Verdef);
6656 }
6657 else
6658 {
6659 def.vd_aux = sizeof (Elf_External_Verdef);
6660 def.vd_next = (sizeof (Elf_External_Verdef)
6661 + sizeof (Elf_External_Verdaux));
6662 }
5a580b3a 6663
ef53be89 6664 if (soname_indx != (size_t) -1)
5a580b3a
AM
6665 {
6666 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6667 soname_indx);
6668 def.vd_hash = bfd_elf_hash (soname);
6669 defaux.vda_name = soname_indx;
3e3b46e5 6670 name = soname;
5a580b3a
AM
6671 }
6672 else
6673 {
ef53be89 6674 size_t indx;
5a580b3a 6675
765cf5f6 6676 name = lbasename (bfd_get_filename (output_bfd));
5a580b3a
AM
6677 def.vd_hash = bfd_elf_hash (name);
6678 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6679 name, FALSE);
ef53be89 6680 if (indx == (size_t) -1)
5a580b3a
AM
6681 return FALSE;
6682 defaux.vda_name = indx;
6683 }
6684 defaux.vda_next = 0;
6685
6686 _bfd_elf_swap_verdef_out (output_bfd, &def,
6687 (Elf_External_Verdef *) p);
6688 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6689 if (info->create_default_symver)
6690 {
6691 /* Add a symbol representing this version. */
6692 bh = NULL;
6693 if (! (_bfd_generic_link_add_one_symbol
6694 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6695 0, NULL, FALSE,
6696 get_elf_backend_data (dynobj)->collect, &bh)))
6697 return FALSE;
6698 h = (struct elf_link_hash_entry *) bh;
6699 h->non_elf = 0;
6700 h->def_regular = 1;
6701 h->type = STT_OBJECT;
6702 h->verinfo.vertree = NULL;
6703
6704 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6705 return FALSE;
6706
6707 /* Create a duplicate of the base version with the same
6708 aux block, but different flags. */
6709 def.vd_flags = 0;
6710 def.vd_ndx = 2;
6711 def.vd_aux = sizeof (Elf_External_Verdef);
6712 if (verdefs)
6713 def.vd_next = (sizeof (Elf_External_Verdef)
6714 + sizeof (Elf_External_Verdaux));
6715 else
6716 def.vd_next = 0;
6717 _bfd_elf_swap_verdef_out (output_bfd, &def,
6718 (Elf_External_Verdef *) p);
6719 p += sizeof (Elf_External_Verdef);
6720 }
5a580b3a
AM
6721 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6722 (Elf_External_Verdaux *) p);
6723 p += sizeof (Elf_External_Verdaux);
6724
6725 for (t = verdefs; t != NULL; t = t->next)
6726 {
6727 unsigned int cdeps;
6728 struct bfd_elf_version_deps *n;
5a580b3a 6729
a6cc6b3b
RO
6730 /* Don't emit the base version twice. */
6731 if (t->vernum == 0)
6732 continue;
6733
5a580b3a
AM
6734 cdeps = 0;
6735 for (n = t->deps; n != NULL; n = n->next)
6736 ++cdeps;
6737
6738 /* Add a symbol representing this version. */
6739 bh = NULL;
6740 if (! (_bfd_generic_link_add_one_symbol
6741 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6742 0, NULL, FALSE,
6743 get_elf_backend_data (dynobj)->collect, &bh)))
6744 return FALSE;
6745 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6746 h->non_elf = 0;
6747 h->def_regular = 1;
5a580b3a
AM
6748 h->type = STT_OBJECT;
6749 h->verinfo.vertree = t;
6750
c152c796 6751 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6752 return FALSE;
6753
6754 def.vd_version = VER_DEF_CURRENT;
6755 def.vd_flags = 0;
6756 if (t->globals.list == NULL
6757 && t->locals.list == NULL
6758 && ! t->used)
6759 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6760 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6761 def.vd_cnt = cdeps + 1;
6762 def.vd_hash = bfd_elf_hash (t->name);
6763 def.vd_aux = sizeof (Elf_External_Verdef);
6764 def.vd_next = 0;
a6cc6b3b
RO
6765
6766 /* If a basever node is next, it *must* be the last node in
6767 the chain, otherwise Verdef construction breaks. */
6768 if (t->next != NULL && t->next->vernum == 0)
6769 BFD_ASSERT (t->next->next == NULL);
6770
6771 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6772 def.vd_next = (sizeof (Elf_External_Verdef)
6773 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6774
6775 _bfd_elf_swap_verdef_out (output_bfd, &def,
6776 (Elf_External_Verdef *) p);
6777 p += sizeof (Elf_External_Verdef);
6778
6779 defaux.vda_name = h->dynstr_index;
6780 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6781 h->dynstr_index);
6782 defaux.vda_next = 0;
6783 if (t->deps != NULL)
6784 defaux.vda_next = sizeof (Elf_External_Verdaux);
6785 t->name_indx = defaux.vda_name;
6786
6787 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6788 (Elf_External_Verdaux *) p);
6789 p += sizeof (Elf_External_Verdaux);
6790
6791 for (n = t->deps; n != NULL; n = n->next)
6792 {
6793 if (n->version_needed == NULL)
6794 {
6795 /* This can happen if there was an error in the
6796 version script. */
6797 defaux.vda_name = 0;
6798 }
6799 else
6800 {
6801 defaux.vda_name = n->version_needed->name_indx;
6802 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6803 defaux.vda_name);
6804 }
6805 if (n->next == NULL)
6806 defaux.vda_next = 0;
6807 else
6808 defaux.vda_next = sizeof (Elf_External_Verdaux);
6809
6810 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6811 (Elf_External_Verdaux *) p);
6812 p += sizeof (Elf_External_Verdaux);
6813 }
6814 }
6815
5a580b3a
AM
6816 elf_tdata (output_bfd)->cverdefs = cdefs;
6817 }
902e9fc7
MR
6818 }
6819
6820 bed = get_elf_backend_data (output_bfd);
6821
6822 if (info->gc_sections && bed->can_gc_sections)
6823 {
6824 struct elf_gc_sweep_symbol_info sweep_info;
902e9fc7
MR
6825
6826 /* Remove the symbols that were in the swept sections from the
3d13f3e9 6827 dynamic symbol table. */
902e9fc7
MR
6828 sweep_info.info = info;
6829 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6830 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6831 &sweep_info);
3d13f3e9
AM
6832 }
6833
6834 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6835 {
6836 asection *s;
6837 struct elf_find_verdep_info sinfo;
6838
6839 /* Work out the size of the version reference section. */
6840
6841 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6842 BFD_ASSERT (s != NULL);
902e9fc7 6843
3d13f3e9
AM
6844 sinfo.info = info;
6845 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6846 if (sinfo.vers == 0)
6847 sinfo.vers = 1;
6848 sinfo.failed = FALSE;
6849
6850 elf_link_hash_traverse (elf_hash_table (info),
6851 _bfd_elf_link_find_version_dependencies,
6852 &sinfo);
6853 if (sinfo.failed)
6854 return FALSE;
6855
6856 if (elf_tdata (output_bfd)->verref == NULL)
6857 s->flags |= SEC_EXCLUDE;
6858 else
6859 {
6860 Elf_Internal_Verneed *vn;
6861 unsigned int size;
6862 unsigned int crefs;
6863 bfd_byte *p;
6864
6865 /* Build the version dependency section. */
6866 size = 0;
6867 crefs = 0;
6868 for (vn = elf_tdata (output_bfd)->verref;
6869 vn != NULL;
6870 vn = vn->vn_nextref)
6871 {
6872 Elf_Internal_Vernaux *a;
6873
6874 size += sizeof (Elf_External_Verneed);
6875 ++crefs;
6876 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6877 size += sizeof (Elf_External_Vernaux);
6878 }
6879
6880 s->size = size;
6881 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6882 if (s->contents == NULL)
6883 return FALSE;
6884
6885 p = s->contents;
6886 for (vn = elf_tdata (output_bfd)->verref;
6887 vn != NULL;
6888 vn = vn->vn_nextref)
6889 {
6890 unsigned int caux;
6891 Elf_Internal_Vernaux *a;
6892 size_t indx;
6893
6894 caux = 0;
6895 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6896 ++caux;
6897
6898 vn->vn_version = VER_NEED_CURRENT;
6899 vn->vn_cnt = caux;
6900 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6901 elf_dt_name (vn->vn_bfd) != NULL
6902 ? elf_dt_name (vn->vn_bfd)
765cf5f6
AM
6903 : lbasename (bfd_get_filename
6904 (vn->vn_bfd)),
3d13f3e9
AM
6905 FALSE);
6906 if (indx == (size_t) -1)
6907 return FALSE;
6908 vn->vn_file = indx;
6909 vn->vn_aux = sizeof (Elf_External_Verneed);
6910 if (vn->vn_nextref == NULL)
6911 vn->vn_next = 0;
6912 else
6913 vn->vn_next = (sizeof (Elf_External_Verneed)
6914 + caux * sizeof (Elf_External_Vernaux));
6915
6916 _bfd_elf_swap_verneed_out (output_bfd, vn,
6917 (Elf_External_Verneed *) p);
6918 p += sizeof (Elf_External_Verneed);
6919
6920 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6921 {
6922 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6923 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6924 a->vna_nodename, FALSE);
6925 if (indx == (size_t) -1)
6926 return FALSE;
6927 a->vna_name = indx;
6928 if (a->vna_nextptr == NULL)
6929 a->vna_next = 0;
6930 else
6931 a->vna_next = sizeof (Elf_External_Vernaux);
6932
6933 _bfd_elf_swap_vernaux_out (output_bfd, a,
6934 (Elf_External_Vernaux *) p);
6935 p += sizeof (Elf_External_Vernaux);
6936 }
6937 }
6938
6939 elf_tdata (output_bfd)->cverrefs = crefs;
6940 }
902e9fc7
MR
6941 }
6942
6943 /* Any syms created from now on start with -1 in
6944 got.refcount/offset and plt.refcount/offset. */
6945 elf_hash_table (info)->init_got_refcount
6946 = elf_hash_table (info)->init_got_offset;
6947 elf_hash_table (info)->init_plt_refcount
6948 = elf_hash_table (info)->init_plt_offset;
6949
6950 if (bfd_link_relocatable (info)
6951 && !_bfd_elf_size_group_sections (info))
6952 return FALSE;
6953
6954 /* The backend may have to create some sections regardless of whether
6955 we're dynamic or not. */
6956 if (bed->elf_backend_always_size_sections
6957 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6958 return FALSE;
6959
6960 /* Determine any GNU_STACK segment requirements, after the backend
6961 has had a chance to set a default segment size. */
6962 if (info->execstack)
6963 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6964 else if (info->noexecstack)
6965 elf_stack_flags (output_bfd) = PF_R | PF_W;
6966 else
6967 {
6968 bfd *inputobj;
6969 asection *notesec = NULL;
6970 int exec = 0;
6971
6972 for (inputobj = info->input_bfds;
6973 inputobj;
6974 inputobj = inputobj->link.next)
6975 {
6976 asection *s;
6977
6978 if (inputobj->flags
6979 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6980 continue;
57963c05
AM
6981 s = inputobj->sections;
6982 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6983 continue;
6984
902e9fc7
MR
6985 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6986 if (s)
6987 {
6988 if (s->flags & SEC_CODE)
6989 exec = PF_X;
6990 notesec = s;
6991 }
6992 else if (bed->default_execstack)
6993 exec = PF_X;
6994 }
6995 if (notesec || info->stacksize > 0)
6996 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6997 if (notesec && exec && bfd_link_relocatable (info)
6998 && notesec->output_section != bfd_abs_section_ptr)
6999 notesec->output_section->flags |= SEC_CODE;
7000 }
7001
7002 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7003 {
7004 struct elf_info_failed eif;
7005 struct elf_link_hash_entry *h;
7006 asection *dynstr;
7007 asection *s;
7008
7009 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
7010 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
7011
902e9fc7
MR
7012 if (info->symbolic)
7013 {
7014 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
7015 return FALSE;
7016 info->flags |= DF_SYMBOLIC;
7017 }
7018
7019 if (rpath != NULL)
7020 {
7021 size_t indx;
7022 bfd_vma tag;
7023
7024 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
7025 TRUE);
7026 if (indx == (size_t) -1)
7027 return FALSE;
7028
7029 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
7030 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
7031 return FALSE;
7032 }
7033
7034 if (filter_shlib != NULL)
7035 {
7036 size_t indx;
7037
7038 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7039 filter_shlib, TRUE);
7040 if (indx == (size_t) -1
7041 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
7042 return FALSE;
7043 }
7044
7045 if (auxiliary_filters != NULL)
7046 {
7047 const char * const *p;
7048
7049 for (p = auxiliary_filters; *p != NULL; p++)
7050 {
7051 size_t indx;
7052
7053 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7054 *p, TRUE);
7055 if (indx == (size_t) -1
7056 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7057 return FALSE;
7058 }
7059 }
7060
7061 if (audit != NULL)
7062 {
7063 size_t indx;
7064
7065 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7066 TRUE);
7067 if (indx == (size_t) -1
7068 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7069 return FALSE;
7070 }
7071
7072 if (depaudit != NULL)
7073 {
7074 size_t indx;
7075
7076 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7077 TRUE);
7078 if (indx == (size_t) -1
7079 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7080 return FALSE;
7081 }
7082
7083 eif.info = info;
7084 eif.failed = FALSE;
7085
7086 /* Find all symbols which were defined in a dynamic object and make
7087 the backend pick a reasonable value for them. */
7088 elf_link_hash_traverse (elf_hash_table (info),
7089 _bfd_elf_adjust_dynamic_symbol,
7090 &eif);
7091 if (eif.failed)
7092 return FALSE;
7093
7094 /* Add some entries to the .dynamic section. We fill in some of the
7095 values later, in bfd_elf_final_link, but we must add the entries
7096 now so that we know the final size of the .dynamic section. */
7097
7098 /* If there are initialization and/or finalization functions to
7099 call then add the corresponding DT_INIT/DT_FINI entries. */
7100 h = (info->init_function
7101 ? elf_link_hash_lookup (elf_hash_table (info),
7102 info->init_function, FALSE,
7103 FALSE, FALSE)
7104 : NULL);
7105 if (h != NULL
7106 && (h->ref_regular
7107 || h->def_regular))
7108 {
7109 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7110 return FALSE;
7111 }
7112 h = (info->fini_function
7113 ? elf_link_hash_lookup (elf_hash_table (info),
7114 info->fini_function, FALSE,
7115 FALSE, FALSE)
7116 : NULL);
7117 if (h != NULL
7118 && (h->ref_regular
7119 || h->def_regular))
7120 {
7121 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7122 return FALSE;
7123 }
7124
7125 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7126 if (s != NULL && s->linker_has_input)
7127 {
7128 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7129 if (! bfd_link_executable (info))
7130 {
7131 bfd *sub;
7132 asection *o;
7133
57963c05
AM
7134 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7135 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7136 && (o = sub->sections) != NULL
7137 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
902e9fc7
MR
7138 for (o = sub->sections; o != NULL; o = o->next)
7139 if (elf_section_data (o)->this_hdr.sh_type
7140 == SHT_PREINIT_ARRAY)
7141 {
7142 _bfd_error_handler
871b3ab2 7143 (_("%pB: .preinit_array section is not allowed in DSO"),
902e9fc7
MR
7144 sub);
7145 break;
7146 }
7147
7148 bfd_set_error (bfd_error_nonrepresentable_section);
7149 return FALSE;
7150 }
7151
7152 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7153 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7154 return FALSE;
7155 }
7156 s = bfd_get_section_by_name (output_bfd, ".init_array");
7157 if (s != NULL && s->linker_has_input)
7158 {
7159 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7160 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7161 return FALSE;
7162 }
7163 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7164 if (s != NULL && s->linker_has_input)
7165 {
7166 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7167 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7168 return FALSE;
7169 }
7170
7171 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7172 /* If .dynstr is excluded from the link, we don't want any of
7173 these tags. Strictly, we should be checking each section
7174 individually; This quick check covers for the case where
7175 someone does a /DISCARD/ : { *(*) }. */
7176 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7177 {
7178 bfd_size_type strsize;
7179
7180 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7181 if ((info->emit_hash
7182 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7183 || (info->emit_gnu_hash
f16a9783
MS
7184 && (bed->record_xhash_symbol == NULL
7185 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
902e9fc7
MR
7186 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7187 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7188 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7189 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6a0a0dd0
VDM
7190 bed->s->sizeof_sym)
7191 || (info->gnu_flags_1
7192 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
7193 info->gnu_flags_1)))
902e9fc7
MR
7194 return FALSE;
7195 }
7196 }
7197
7198 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7199 return FALSE;
7200
7201 /* The backend must work out the sizes of all the other dynamic
7202 sections. */
7203 if (dynobj != NULL
7204 && bed->elf_backend_size_dynamic_sections != NULL
7205 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7206 return FALSE;
7207
7208 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7209 {
902e9fc7
MR
7210 if (elf_tdata (output_bfd)->cverdefs)
7211 {
7212 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7213
7214 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7215 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7216 return FALSE;
7217 }
7218
7219 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7220 {
7221 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7222 return FALSE;
7223 }
7224 else if (info->flags & DF_BIND_NOW)
7225 {
7226 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7227 return FALSE;
7228 }
7229
7230 if (info->flags_1)
7231 {
7232 if (bfd_link_executable (info))
7233 info->flags_1 &= ~ (DF_1_INITFIRST
7234 | DF_1_NODELETE
7235 | DF_1_NOOPEN);
7236 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7237 return FALSE;
7238 }
7239
7240 if (elf_tdata (output_bfd)->cverrefs)
7241 {
7242 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7243
7244 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7245 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7246 return FALSE;
7247 }
5a580b3a 7248
8423293d
AM
7249 if ((elf_tdata (output_bfd)->cverrefs == 0
7250 && elf_tdata (output_bfd)->cverdefs == 0)
63f452a8 7251 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
8423293d 7252 {
902e9fc7
MR
7253 asection *s;
7254
3d4d4302 7255 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
7256 s->flags |= SEC_EXCLUDE;
7257 }
7258 }
7259 return TRUE;
7260}
7261
74541ad4
AM
7262/* Find the first non-excluded output section. We'll use its
7263 section symbol for some emitted relocs. */
7264void
7265_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7266{
7267 asection *s;
f26a3287 7268 asection *found = NULL;
74541ad4
AM
7269
7270 for (s = output_bfd->sections; s != NULL; s = s->next)
7271 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
d00dd7dc 7272 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7273 {
f26a3287
AM
7274 found = s;
7275 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7276 break;
74541ad4 7277 }
f26a3287 7278 elf_hash_table (info)->text_index_section = found;
74541ad4
AM
7279}
7280
7281/* Find two non-excluded output sections, one for code, one for data.
7282 We'll use their section symbols for some emitted relocs. */
7283void
7284_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7285{
7286 asection *s;
f26a3287 7287 asection *found = NULL;
74541ad4 7288
266b05cf 7289 /* Data first, since setting text_index_section changes
7f923b7f 7290 _bfd_elf_omit_section_dynsym_default. */
74541ad4 7291 for (s = output_bfd->sections; s != NULL; s = s->next)
f26a3287
AM
7292 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7293 && !(s->flags & SEC_READONLY)
d00dd7dc 7294 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7295 {
f26a3287
AM
7296 found = s;
7297 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7298 break;
74541ad4 7299 }
f26a3287 7300 elf_hash_table (info)->data_index_section = found;
74541ad4
AM
7301
7302 for (s = output_bfd->sections; s != NULL; s = s->next)
f26a3287
AM
7303 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7304 && (s->flags & SEC_READONLY)
d00dd7dc 7305 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7306 {
f26a3287 7307 found = s;
74541ad4
AM
7308 break;
7309 }
f26a3287 7310 elf_hash_table (info)->text_index_section = found;
74541ad4
AM
7311}
7312
f16a9783
MS
7313#define GNU_HASH_SECTION_NAME(bed) \
7314 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7315
8423293d
AM
7316bfd_boolean
7317bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7318{
74541ad4 7319 const struct elf_backend_data *bed;
23ec1e32 7320 unsigned long section_sym_count;
96d01d93 7321 bfd_size_type dynsymcount = 0;
74541ad4 7322
8423293d
AM
7323 if (!is_elf_hash_table (info->hash))
7324 return TRUE;
7325
74541ad4
AM
7326 bed = get_elf_backend_data (output_bfd);
7327 (*bed->elf_backend_init_index_section) (output_bfd, info);
7328
23ec1e32
MR
7329 /* Assign dynsym indices. In a shared library we generate a section
7330 symbol for each output section, which come first. Next come all
7331 of the back-end allocated local dynamic syms, followed by the rest
7332 of the global symbols.
7333
7334 This is usually not needed for static binaries, however backends
7335 can request to always do it, e.g. the MIPS backend uses dynamic
7336 symbol counts to lay out GOT, which will be produced in the
7337 presence of GOT relocations even in static binaries (holding fixed
7338 data in that case, to satisfy those relocations). */
7339
7340 if (elf_hash_table (info)->dynamic_sections_created
7341 || bed->always_renumber_dynsyms)
7342 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7343 &section_sym_count);
7344
8423293d
AM
7345 if (elf_hash_table (info)->dynamic_sections_created)
7346 {
7347 bfd *dynobj;
8423293d 7348 asection *s;
8423293d
AM
7349 unsigned int dtagcount;
7350
7351 dynobj = elf_hash_table (info)->dynobj;
7352
5a580b3a 7353 /* Work out the size of the symbol version section. */
3d4d4302 7354 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 7355 BFD_ASSERT (s != NULL);
d5486c43 7356 if ((s->flags & SEC_EXCLUDE) == 0)
5a580b3a 7357 {
eea6121a 7358 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 7359 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
7360 if (s->contents == NULL)
7361 return FALSE;
7362
7363 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7364 return FALSE;
7365 }
7366
7367 /* Set the size of the .dynsym and .hash sections. We counted
7368 the number of dynamic symbols in elf_link_add_object_symbols.
7369 We will build the contents of .dynsym and .hash when we build
7370 the final symbol table, because until then we do not know the
7371 correct value to give the symbols. We built the .dynstr
7372 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 7373 s = elf_hash_table (info)->dynsym;
5a580b3a 7374 BFD_ASSERT (s != NULL);
eea6121a 7375 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a 7376
d5486c43
L
7377 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7378 if (s->contents == NULL)
7379 return FALSE;
5a580b3a 7380
d5486c43
L
7381 /* The first entry in .dynsym is a dummy symbol. Clear all the
7382 section syms, in case we don't output them all. */
7383 ++section_sym_count;
7384 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a 7385
fdc90cb4
JJ
7386 elf_hash_table (info)->bucketcount = 0;
7387
5a580b3a
AM
7388 /* Compute the size of the hashing table. As a side effect this
7389 computes the hash values for all the names we export. */
fdc90cb4
JJ
7390 if (info->emit_hash)
7391 {
7392 unsigned long int *hashcodes;
14b1c01e 7393 struct hash_codes_info hashinf;
fdc90cb4
JJ
7394 bfd_size_type amt;
7395 unsigned long int nsyms;
7396 size_t bucketcount;
7397 size_t hash_entry_size;
7398
7399 /* Compute the hash values for all exported symbols. At the same
7400 time store the values in an array so that we could use them for
7401 optimizations. */
7402 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 7403 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
7404 if (hashcodes == NULL)
7405 return FALSE;
14b1c01e
AM
7406 hashinf.hashcodes = hashcodes;
7407 hashinf.error = FALSE;
5a580b3a 7408
fdc90cb4
JJ
7409 /* Put all hash values in HASHCODES. */
7410 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
7411 elf_collect_hash_codes, &hashinf);
7412 if (hashinf.error)
4dd07732
AM
7413 {
7414 free (hashcodes);
7415 return FALSE;
7416 }
5a580b3a 7417
14b1c01e 7418 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
7419 bucketcount
7420 = compute_bucket_count (info, hashcodes, nsyms, 0);
7421 free (hashcodes);
7422
4b48e2f6 7423 if (bucketcount == 0 && nsyms > 0)
fdc90cb4 7424 return FALSE;
5a580b3a 7425
fdc90cb4
JJ
7426 elf_hash_table (info)->bucketcount = bucketcount;
7427
3d4d4302 7428 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
7429 BFD_ASSERT (s != NULL);
7430 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7431 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 7432 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7433 if (s->contents == NULL)
7434 return FALSE;
7435
7436 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7437 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7438 s->contents + hash_entry_size);
7439 }
7440
7441 if (info->emit_gnu_hash)
7442 {
7443 size_t i, cnt;
7444 unsigned char *contents;
7445 struct collect_gnu_hash_codes cinfo;
7446 bfd_size_type amt;
7447 size_t bucketcount;
7448
7449 memset (&cinfo, 0, sizeof (cinfo));
7450
7451 /* Compute the hash values for all exported symbols. At the same
7452 time store the values in an array so that we could use them for
7453 optimizations. */
7454 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 7455 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
7456 if (cinfo.hashcodes == NULL)
7457 return FALSE;
7458
7459 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7460 cinfo.min_dynindx = -1;
7461 cinfo.output_bfd = output_bfd;
7462 cinfo.bed = bed;
7463
7464 /* Put all hash values in HASHCODES. */
7465 elf_link_hash_traverse (elf_hash_table (info),
7466 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 7467 if (cinfo.error)
4dd07732
AM
7468 {
7469 free (cinfo.hashcodes);
7470 return FALSE;
7471 }
fdc90cb4
JJ
7472
7473 bucketcount
7474 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7475
7476 if (bucketcount == 0)
7477 {
7478 free (cinfo.hashcodes);
7479 return FALSE;
7480 }
7481
f16a9783 7482 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
fdc90cb4
JJ
7483 BFD_ASSERT (s != NULL);
7484
7485 if (cinfo.nsyms == 0)
7486 {
f16a9783 7487 /* Empty .gnu.hash or .MIPS.xhash section is special. */
fdc90cb4
JJ
7488 BFD_ASSERT (cinfo.min_dynindx == -1);
7489 free (cinfo.hashcodes);
7490 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 7491 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7492 if (contents == NULL)
7493 return FALSE;
7494 s->contents = contents;
7495 /* 1 empty bucket. */
7496 bfd_put_32 (output_bfd, 1, contents);
7497 /* SYMIDX above the special symbol 0. */
7498 bfd_put_32 (output_bfd, 1, contents + 4);
7499 /* Just one word for bitmask. */
7500 bfd_put_32 (output_bfd, 1, contents + 8);
7501 /* Only hash fn bloom filter. */
7502 bfd_put_32 (output_bfd, 0, contents + 12);
7503 /* No hashes are valid - empty bitmask. */
7504 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7505 /* No hashes in the only bucket. */
7506 bfd_put_32 (output_bfd, 0,
7507 contents + 16 + bed->s->arch_size / 8);
7508 }
7509 else
7510 {
9e6619e2 7511 unsigned long int maskwords, maskbitslog2, x;
0b33793d 7512 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 7513
9e6619e2
AM
7514 x = cinfo.nsyms;
7515 maskbitslog2 = 1;
7516 while ((x >>= 1) != 0)
7517 ++maskbitslog2;
fdc90cb4
JJ
7518 if (maskbitslog2 < 3)
7519 maskbitslog2 = 5;
7520 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7521 maskbitslog2 = maskbitslog2 + 3;
7522 else
7523 maskbitslog2 = maskbitslog2 + 2;
7524 if (bed->s->arch_size == 64)
7525 {
7526 if (maskbitslog2 == 5)
7527 maskbitslog2 = 6;
7528 cinfo.shift1 = 6;
7529 }
7530 else
7531 cinfo.shift1 = 5;
7532 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 7533 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
7534 cinfo.maskbits = 1 << maskbitslog2;
7535 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7536 amt = bucketcount * sizeof (unsigned long int) * 2;
7537 amt += maskwords * sizeof (bfd_vma);
a50b1753 7538 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
7539 if (cinfo.bitmask == NULL)
7540 {
7541 free (cinfo.hashcodes);
7542 return FALSE;
7543 }
7544
a50b1753 7545 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
7546 cinfo.indx = cinfo.counts + bucketcount;
7547 cinfo.symindx = dynsymcount - cinfo.nsyms;
7548 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7549
7550 /* Determine how often each hash bucket is used. */
7551 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7552 for (i = 0; i < cinfo.nsyms; ++i)
7553 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7554
7555 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7556 if (cinfo.counts[i] != 0)
7557 {
7558 cinfo.indx[i] = cnt;
7559 cnt += cinfo.counts[i];
7560 }
7561 BFD_ASSERT (cnt == dynsymcount);
7562 cinfo.bucketcount = bucketcount;
7563 cinfo.local_indx = cinfo.min_dynindx;
7564
7565 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7566 s->size += cinfo.maskbits / 8;
f16a9783
MS
7567 if (bed->record_xhash_symbol != NULL)
7568 s->size += cinfo.nsyms * 4;
a50b1753 7569 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7570 if (contents == NULL)
7571 {
7572 free (cinfo.bitmask);
7573 free (cinfo.hashcodes);
7574 return FALSE;
7575 }
7576
7577 s->contents = contents;
7578 bfd_put_32 (output_bfd, bucketcount, contents);
7579 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7580 bfd_put_32 (output_bfd, maskwords, contents + 8);
7581 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7582 contents += 16 + cinfo.maskbits / 8;
7583
7584 for (i = 0; i < bucketcount; ++i)
7585 {
7586 if (cinfo.counts[i] == 0)
7587 bfd_put_32 (output_bfd, 0, contents);
7588 else
7589 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7590 contents += 4;
7591 }
7592
7593 cinfo.contents = contents;
7594
f16a9783
MS
7595 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7596 /* Renumber dynamic symbols, if populating .gnu.hash section.
7597 If using .MIPS.xhash, populate the translation table. */
fdc90cb4 7598 elf_link_hash_traverse (elf_hash_table (info),
f16a9783 7599 elf_gnu_hash_process_symidx, &cinfo);
fdc90cb4
JJ
7600
7601 contents = s->contents + 16;
7602 for (i = 0; i < maskwords; ++i)
7603 {
7604 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7605 contents);
7606 contents += bed->s->arch_size / 8;
7607 }
7608
7609 free (cinfo.bitmask);
7610 free (cinfo.hashcodes);
7611 }
7612 }
5a580b3a 7613
3d4d4302 7614 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
7615 BFD_ASSERT (s != NULL);
7616
4ad4eba5 7617 elf_finalize_dynstr (output_bfd, info);
5a580b3a 7618
eea6121a 7619 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
7620
7621 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7622 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7623 return FALSE;
7624 }
7625
7626 return TRUE;
7627}
4d269e42 7628\f
4d269e42
AM
7629/* Make sure sec_info_type is cleared if sec_info is cleared too. */
7630
7631static void
7632merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7633 asection *sec)
7634{
dbaa2011
AM
7635 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7636 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
7637}
7638
7639/* Finish SHF_MERGE section merging. */
7640
7641bfd_boolean
630993ec 7642_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
7643{
7644 bfd *ibfd;
7645 asection *sec;
7646
7647 if (!is_elf_hash_table (info->hash))
7648 return FALSE;
7649
c72f2fb2 7650 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
7651 if ((ibfd->flags & DYNAMIC) == 0
7652 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
7653 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7654 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
7655 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7656 if ((sec->flags & SEC_MERGE) != 0
7657 && !bfd_is_abs_section (sec->output_section))
7658 {
7659 struct bfd_elf_section_data *secdata;
7660
7661 secdata = elf_section_data (sec);
630993ec 7662 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
7663 &elf_hash_table (info)->merge_info,
7664 sec, &secdata->sec_info))
7665 return FALSE;
7666 else if (secdata->sec_info)
dbaa2011 7667 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
7668 }
7669
7670 if (elf_hash_table (info)->merge_info != NULL)
630993ec 7671 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
7672 merge_sections_remove_hook);
7673 return TRUE;
7674}
7675
7676/* Create an entry in an ELF linker hash table. */
7677
7678struct bfd_hash_entry *
7679_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7680 struct bfd_hash_table *table,
7681 const char *string)
7682{
7683 /* Allocate the structure if it has not already been allocated by a
7684 subclass. */
7685 if (entry == NULL)
7686 {
a50b1753 7687 entry = (struct bfd_hash_entry *)
ca4be51c 7688 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
7689 if (entry == NULL)
7690 return entry;
7691 }
7692
7693 /* Call the allocation method of the superclass. */
7694 entry = _bfd_link_hash_newfunc (entry, table, string);
7695 if (entry != NULL)
7696 {
7697 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7698 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7699
7700 /* Set local fields. */
7701 ret->indx = -1;
7702 ret->dynindx = -1;
7703 ret->got = htab->init_got_refcount;
7704 ret->plt = htab->init_plt_refcount;
7705 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7706 - offsetof (struct elf_link_hash_entry, size)));
7707 /* Assume that we have been called by a non-ELF symbol reader.
7708 This flag is then reset by the code which reads an ELF input
7709 file. This ensures that a symbol created by a non-ELF symbol
7710 reader will have the flag set correctly. */
7711 ret->non_elf = 1;
7712 }
7713
7714 return entry;
7715}
7716
7717/* Copy data from an indirect symbol to its direct symbol, hiding the
7718 old indirect symbol. Also used for copying flags to a weakdef. */
7719
7720void
7721_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7722 struct elf_link_hash_entry *dir,
7723 struct elf_link_hash_entry *ind)
7724{
7725 struct elf_link_hash_table *htab;
7726
ad172eaa
L
7727 if (ind->dyn_relocs != NULL)
7728 {
7729 if (dir->dyn_relocs != NULL)
7730 {
7731 struct elf_dyn_relocs **pp;
7732 struct elf_dyn_relocs *p;
7733
7734 /* Add reloc counts against the indirect sym to the direct sym
7735 list. Merge any entries against the same section. */
7736 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
7737 {
7738 struct elf_dyn_relocs *q;
7739
7740 for (q = dir->dyn_relocs; q != NULL; q = q->next)
7741 if (q->sec == p->sec)
7742 {
7743 q->pc_count += p->pc_count;
7744 q->count += p->count;
7745 *pp = p->next;
7746 break;
7747 }
7748 if (q == NULL)
7749 pp = &p->next;
7750 }
7751 *pp = dir->dyn_relocs;
7752 }
7753
7754 dir->dyn_relocs = ind->dyn_relocs;
7755 ind->dyn_relocs = NULL;
7756 }
7757
4d269e42 7758 /* Copy down any references that we may have already seen to the
e81830c5 7759 symbol which just became indirect. */
4d269e42 7760
422f1182 7761 if (dir->versioned != versioned_hidden)
e81830c5
AM
7762 dir->ref_dynamic |= ind->ref_dynamic;
7763 dir->ref_regular |= ind->ref_regular;
7764 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7765 dir->non_got_ref |= ind->non_got_ref;
7766 dir->needs_plt |= ind->needs_plt;
7767 dir->pointer_equality_needed |= ind->pointer_equality_needed;
4d269e42
AM
7768
7769 if (ind->root.type != bfd_link_hash_indirect)
7770 return;
7771
7772 /* Copy over the global and procedure linkage table refcount entries.
7773 These may have been already set up by a check_relocs routine. */
7774 htab = elf_hash_table (info);
7775 if (ind->got.refcount > htab->init_got_refcount.refcount)
7776 {
7777 if (dir->got.refcount < 0)
7778 dir->got.refcount = 0;
7779 dir->got.refcount += ind->got.refcount;
7780 ind->got.refcount = htab->init_got_refcount.refcount;
7781 }
7782
7783 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7784 {
7785 if (dir->plt.refcount < 0)
7786 dir->plt.refcount = 0;
7787 dir->plt.refcount += ind->plt.refcount;
7788 ind->plt.refcount = htab->init_plt_refcount.refcount;
7789 }
7790
7791 if (ind->dynindx != -1)
7792 {
7793 if (dir->dynindx != -1)
7794 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7795 dir->dynindx = ind->dynindx;
7796 dir->dynstr_index = ind->dynstr_index;
7797 ind->dynindx = -1;
7798 ind->dynstr_index = 0;
7799 }
7800}
7801
7802void
7803_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7804 struct elf_link_hash_entry *h,
7805 bfd_boolean force_local)
7806{
3aa14d16
L
7807 /* STT_GNU_IFUNC symbol must go through PLT. */
7808 if (h->type != STT_GNU_IFUNC)
7809 {
7810 h->plt = elf_hash_table (info)->init_plt_offset;
7811 h->needs_plt = 0;
7812 }
4d269e42
AM
7813 if (force_local)
7814 {
7815 h->forced_local = 1;
7816 if (h->dynindx != -1)
7817 {
4d269e42
AM
7818 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7819 h->dynstr_index);
641338d8
AM
7820 h->dynindx = -1;
7821 h->dynstr_index = 0;
4d269e42
AM
7822 }
7823 }
7824}
7825
34a87bb0
L
7826/* Hide a symbol. */
7827
7828void
7829_bfd_elf_link_hide_symbol (bfd *output_bfd,
7830 struct bfd_link_info *info,
7831 struct bfd_link_hash_entry *h)
7832{
7833 if (is_elf_hash_table (info->hash))
7834 {
7835 const struct elf_backend_data *bed
7836 = get_elf_backend_data (output_bfd);
7837 struct elf_link_hash_entry *eh
7838 = (struct elf_link_hash_entry *) h;
7839 bed->elf_backend_hide_symbol (info, eh, TRUE);
7840 eh->def_dynamic = 0;
7841 eh->ref_dynamic = 0;
7842 eh->dynamic_def = 0;
7843 }
7844}
7845
7bf52ea2
AM
7846/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7847 caller. */
4d269e42
AM
7848
7849bfd_boolean
7850_bfd_elf_link_hash_table_init
7851 (struct elf_link_hash_table *table,
7852 bfd *abfd,
7853 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7854 struct bfd_hash_table *,
7855 const char *),
4dfe6ac6
NC
7856 unsigned int entsize,
7857 enum elf_target_id target_id)
4d269e42
AM
7858{
7859 bfd_boolean ret;
7860 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7861
4d269e42
AM
7862 table->init_got_refcount.refcount = can_refcount - 1;
7863 table->init_plt_refcount.refcount = can_refcount - 1;
7864 table->init_got_offset.offset = -(bfd_vma) 1;
7865 table->init_plt_offset.offset = -(bfd_vma) 1;
7866 /* The first dynamic symbol is a dummy. */
7867 table->dynsymcount = 1;
7868
7869 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7870
4d269e42 7871 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7872 table->hash_table_id = target_id;
90c14f0c 7873 table->target_os = get_elf_backend_data (abfd)->target_os;
4d269e42
AM
7874
7875 return ret;
7876}
7877
7878/* Create an ELF linker hash table. */
7879
7880struct bfd_link_hash_table *
7881_bfd_elf_link_hash_table_create (bfd *abfd)
7882{
7883 struct elf_link_hash_table *ret;
986f0783 7884 size_t amt = sizeof (struct elf_link_hash_table);
4d269e42 7885
7bf52ea2 7886 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7887 if (ret == NULL)
7888 return NULL;
7889
7890 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7891 sizeof (struct elf_link_hash_entry),
7892 GENERIC_ELF_DATA))
4d269e42
AM
7893 {
7894 free (ret);
7895 return NULL;
7896 }
d495ab0d 7897 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7898
7899 return &ret->root;
7900}
7901
9f7c3e5e
AM
7902/* Destroy an ELF linker hash table. */
7903
7904void
d495ab0d 7905_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7906{
d495ab0d
AM
7907 struct elf_link_hash_table *htab;
7908
7909 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7910 if (htab->dynstr != NULL)
7911 _bfd_elf_strtab_free (htab->dynstr);
7912 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7913 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7914}
7915
4d269e42
AM
7916/* This is a hook for the ELF emulation code in the generic linker to
7917 tell the backend linker what file name to use for the DT_NEEDED
7918 entry for a dynamic object. */
7919
7920void
7921bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7922{
7923 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7924 && bfd_get_format (abfd) == bfd_object)
7925 elf_dt_name (abfd) = name;
7926}
7927
7928int
7929bfd_elf_get_dyn_lib_class (bfd *abfd)
7930{
7931 int lib_class;
7932 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7933 && bfd_get_format (abfd) == bfd_object)
7934 lib_class = elf_dyn_lib_class (abfd);
7935 else
7936 lib_class = 0;
7937 return lib_class;
7938}
7939
7940void
7941bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7942{
7943 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7944 && bfd_get_format (abfd) == bfd_object)
7945 elf_dyn_lib_class (abfd) = lib_class;
7946}
7947
7948/* Get the list of DT_NEEDED entries for a link. This is a hook for
7949 the linker ELF emulation code. */
7950
7951struct bfd_link_needed_list *
7952bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7953 struct bfd_link_info *info)
7954{
7955 if (! is_elf_hash_table (info->hash))
7956 return NULL;
7957 return elf_hash_table (info)->needed;
7958}
7959
7960/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7961 hook for the linker ELF emulation code. */
7962
7963struct bfd_link_needed_list *
7964bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7965 struct bfd_link_info *info)
7966{
7967 if (! is_elf_hash_table (info->hash))
7968 return NULL;
7969 return elf_hash_table (info)->runpath;
7970}
7971
7972/* Get the name actually used for a dynamic object for a link. This
7973 is the SONAME entry if there is one. Otherwise, it is the string
7974 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7975
7976const char *
7977bfd_elf_get_dt_soname (bfd *abfd)
7978{
7979 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7980 && bfd_get_format (abfd) == bfd_object)
7981 return elf_dt_name (abfd);
7982 return NULL;
7983}
7984
7985/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7986 the ELF linker emulation code. */
7987
7988bfd_boolean
7989bfd_elf_get_bfd_needed_list (bfd *abfd,
7990 struct bfd_link_needed_list **pneeded)
7991{
7992 asection *s;
7993 bfd_byte *dynbuf = NULL;
cb33740c 7994 unsigned int elfsec;
4d269e42
AM
7995 unsigned long shlink;
7996 bfd_byte *extdyn, *extdynend;
7997 size_t extdynsize;
7998 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7999
8000 *pneeded = NULL;
8001
8002 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
8003 || bfd_get_format (abfd) != bfd_object)
8004 return TRUE;
8005
8006 s = bfd_get_section_by_name (abfd, ".dynamic");
8007 if (s == NULL || s->size == 0)
8008 return TRUE;
8009
8010 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
8011 goto error_return;
8012
8013 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 8014 if (elfsec == SHN_BAD)
4d269e42
AM
8015 goto error_return;
8016
8017 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 8018
4d269e42
AM
8019 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
8020 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
8021
8022 extdyn = dynbuf;
8023 extdynend = extdyn + s->size;
8024 for (; extdyn < extdynend; extdyn += extdynsize)
8025 {
8026 Elf_Internal_Dyn dyn;
8027
8028 (*swap_dyn_in) (abfd, extdyn, &dyn);
8029
8030 if (dyn.d_tag == DT_NULL)
8031 break;
8032
8033 if (dyn.d_tag == DT_NEEDED)
8034 {
8035 const char *string;
8036 struct bfd_link_needed_list *l;
8037 unsigned int tagv = dyn.d_un.d_val;
986f0783 8038 size_t amt;
4d269e42
AM
8039
8040 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8041 if (string == NULL)
8042 goto error_return;
8043
8044 amt = sizeof *l;
a50b1753 8045 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
8046 if (l == NULL)
8047 goto error_return;
8048
8049 l->by = abfd;
8050 l->name = string;
8051 l->next = *pneeded;
8052 *pneeded = l;
8053 }
8054 }
8055
8056 free (dynbuf);
8057
8058 return TRUE;
8059
8060 error_return:
c9594989 8061 free (dynbuf);
4d269e42
AM
8062 return FALSE;
8063}
8064
8065struct elf_symbuf_symbol
8066{
8067 unsigned long st_name; /* Symbol name, index in string tbl */
8068 unsigned char st_info; /* Type and binding attributes */
8069 unsigned char st_other; /* Visibilty, and target specific */
8070};
8071
8072struct elf_symbuf_head
8073{
8074 struct elf_symbuf_symbol *ssym;
ef53be89 8075 size_t count;
4d269e42
AM
8076 unsigned int st_shndx;
8077};
8078
8079struct elf_symbol
8080{
8081 union
8082 {
8083 Elf_Internal_Sym *isym;
8084 struct elf_symbuf_symbol *ssym;
dcea6a95 8085 void *p;
4d269e42
AM
8086 } u;
8087 const char *name;
8088};
8089
8090/* Sort references to symbols by ascending section number. */
8091
8092static int
8093elf_sort_elf_symbol (const void *arg1, const void *arg2)
8094{
8095 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8096 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8097
dcea6a95
AM
8098 if (s1->st_shndx != s2->st_shndx)
8099 return s1->st_shndx > s2->st_shndx ? 1 : -1;
8100 /* Final sort by the address of the sym in the symbuf ensures
8101 a stable sort. */
8102 if (s1 != s2)
8103 return s1 > s2 ? 1 : -1;
8104 return 0;
4d269e42
AM
8105}
8106
8107static int
8108elf_sym_name_compare (const void *arg1, const void *arg2)
8109{
8110 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8111 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
dcea6a95
AM
8112 int ret = strcmp (s1->name, s2->name);
8113 if (ret != 0)
8114 return ret;
8115 if (s1->u.p != s2->u.p)
8116 return s1->u.p > s2->u.p ? 1 : -1;
8117 return 0;
4d269e42
AM
8118}
8119
8120static struct elf_symbuf_head *
ef53be89 8121elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
4d269e42 8122{
14b1c01e 8123 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
8124 struct elf_symbuf_symbol *ssym;
8125 struct elf_symbuf_head *ssymbuf, *ssymhead;
446f7ed5 8126 size_t i, shndx_count, total_size, amt;
4d269e42 8127
446f7ed5
AM
8128 amt = symcount * sizeof (*indbuf);
8129 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
4d269e42
AM
8130 if (indbuf == NULL)
8131 return NULL;
8132
994b2513
L
8133 /* NB: When checking if 2 sections define the same set of local and
8134 global symbols, ignore both undefined and section symbols in the
8135 symbol table. */
4d269e42 8136 for (ind = indbuf, i = 0; i < symcount; i++)
994b2513
L
8137 if (isymbuf[i].st_shndx != SHN_UNDEF
8138 && ELF_ST_TYPE (isymbuf[i].st_info) != STT_SECTION)
4d269e42
AM
8139 *ind++ = &isymbuf[i];
8140 indbufend = ind;
8141
8142 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8143 elf_sort_elf_symbol);
8144
8145 shndx_count = 0;
8146 if (indbufend > indbuf)
8147 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8148 if (ind[0]->st_shndx != ind[1]->st_shndx)
8149 shndx_count++;
8150
3ae181ee
L
8151 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8152 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 8153 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
8154 if (ssymbuf == NULL)
8155 {
8156 free (indbuf);
8157 return NULL;
8158 }
8159
3ae181ee 8160 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
8161 ssymbuf->ssym = NULL;
8162 ssymbuf->count = shndx_count;
8163 ssymbuf->st_shndx = 0;
8164 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8165 {
8166 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8167 {
8168 ssymhead++;
8169 ssymhead->ssym = ssym;
8170 ssymhead->count = 0;
8171 ssymhead->st_shndx = (*ind)->st_shndx;
8172 }
8173 ssym->st_name = (*ind)->st_name;
8174 ssym->st_info = (*ind)->st_info;
8175 ssym->st_other = (*ind)->st_other;
8176 ssymhead->count++;
8177 }
ef53be89 8178 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
3ae181ee
L
8179 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
8180 == total_size));
4d269e42
AM
8181
8182 free (indbuf);
8183 return ssymbuf;
8184}
8185
8186/* Check if 2 sections define the same set of local and global
8187 symbols. */
8188
8f317e31 8189static bfd_boolean
4d269e42
AM
8190bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8191 struct bfd_link_info *info)
8192{
8193 bfd *bfd1, *bfd2;
8194 const struct elf_backend_data *bed1, *bed2;
8195 Elf_Internal_Shdr *hdr1, *hdr2;
ef53be89 8196 size_t symcount1, symcount2;
4d269e42
AM
8197 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8198 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8199 Elf_Internal_Sym *isym, *isymend;
8200 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
ef53be89 8201 size_t count1, count2, i;
cb33740c 8202 unsigned int shndx1, shndx2;
4d269e42
AM
8203 bfd_boolean result;
8204
8205 bfd1 = sec1->owner;
8206 bfd2 = sec2->owner;
8207
4d269e42
AM
8208 /* Both sections have to be in ELF. */
8209 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8210 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8211 return FALSE;
8212
8213 if (elf_section_type (sec1) != elf_section_type (sec2))
8214 return FALSE;
8215
4d269e42
AM
8216 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8217 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 8218 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
8219 return FALSE;
8220
8221 bed1 = get_elf_backend_data (bfd1);
8222 bed2 = get_elf_backend_data (bfd2);
8223 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8224 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8225 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8226 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8227
8228 if (symcount1 == 0 || symcount2 == 0)
8229 return FALSE;
8230
8231 result = FALSE;
8232 isymbuf1 = NULL;
8233 isymbuf2 = NULL;
a50b1753
NC
8234 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8235 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
8236
8237 if (ssymbuf1 == NULL)
8238 {
8239 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8240 NULL, NULL, NULL);
8241 if (isymbuf1 == NULL)
8242 goto done;
8243
67411cbf 8244 if (info != NULL && !info->reduce_memory_overheads)
dcea6a95
AM
8245 {
8246 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8247 elf_tdata (bfd1)->symbuf = ssymbuf1;
8248 }
4d269e42
AM
8249 }
8250
8251 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8252 {
8253 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8254 NULL, NULL, NULL);
8255 if (isymbuf2 == NULL)
8256 goto done;
8257
67411cbf 8258 if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
dcea6a95
AM
8259 {
8260 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8261 elf_tdata (bfd2)->symbuf = ssymbuf2;
8262 }
4d269e42
AM
8263 }
8264
8265 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8266 {
8267 /* Optimized faster version. */
ef53be89 8268 size_t lo, hi, mid;
4d269e42
AM
8269 struct elf_symbol *symp;
8270 struct elf_symbuf_symbol *ssym, *ssymend;
8271
8272 lo = 0;
8273 hi = ssymbuf1->count;
8274 ssymbuf1++;
8275 count1 = 0;
8276 while (lo < hi)
8277 {
8278 mid = (lo + hi) / 2;
cb33740c 8279 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 8280 hi = mid;
cb33740c 8281 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
8282 lo = mid + 1;
8283 else
8284 {
8285 count1 = ssymbuf1[mid].count;
8286 ssymbuf1 += mid;
8287 break;
8288 }
8289 }
8290
8291 lo = 0;
8292 hi = ssymbuf2->count;
8293 ssymbuf2++;
8294 count2 = 0;
8295 while (lo < hi)
8296 {
8297 mid = (lo + hi) / 2;
cb33740c 8298 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 8299 hi = mid;
cb33740c 8300 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
8301 lo = mid + 1;
8302 else
8303 {
8304 count2 = ssymbuf2[mid].count;
8305 ssymbuf2 += mid;
8306 break;
8307 }
8308 }
8309
8310 if (count1 == 0 || count2 == 0 || count1 != count2)
8311 goto done;
8312
ca4be51c
AM
8313 symtable1
8314 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8315 symtable2
8316 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
8317 if (symtable1 == NULL || symtable2 == NULL)
8318 goto done;
8319
8320 symp = symtable1;
8321 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
8322 ssym < ssymend; ssym++, symp++)
8323 {
8324 symp->u.ssym = ssym;
8325 symp->name = bfd_elf_string_from_elf_section (bfd1,
8326 hdr1->sh_link,
8327 ssym->st_name);
8328 }
8329
8330 symp = symtable2;
8331 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8332 ssym < ssymend; ssym++, symp++)
8333 {
8334 symp->u.ssym = ssym;
8335 symp->name = bfd_elf_string_from_elf_section (bfd2,
8336 hdr2->sh_link,
8337 ssym->st_name);
8338 }
8339
8340 /* Sort symbol by name. */
8341 qsort (symtable1, count1, sizeof (struct elf_symbol),
8342 elf_sym_name_compare);
8343 qsort (symtable2, count1, sizeof (struct elf_symbol),
8344 elf_sym_name_compare);
8345
8346 for (i = 0; i < count1; i++)
8347 /* Two symbols must have the same binding, type and name. */
8348 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8349 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8350 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8351 goto done;
8352
8353 result = TRUE;
8354 goto done;
8355 }
8356
a50b1753
NC
8357 symtable1 = (struct elf_symbol *)
8358 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8359 symtable2 = (struct elf_symbol *)
8360 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
8361 if (symtable1 == NULL || symtable2 == NULL)
8362 goto done;
8363
8364 /* Count definitions in the section. */
8365 count1 = 0;
8366 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 8367 if (isym->st_shndx == shndx1)
4d269e42
AM
8368 symtable1[count1++].u.isym = isym;
8369
8370 count2 = 0;
8371 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 8372 if (isym->st_shndx == shndx2)
4d269e42
AM
8373 symtable2[count2++].u.isym = isym;
8374
8375 if (count1 == 0 || count2 == 0 || count1 != count2)
8376 goto done;
8377
8378 for (i = 0; i < count1; i++)
8379 symtable1[i].name
8380 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8381 symtable1[i].u.isym->st_name);
8382
8383 for (i = 0; i < count2; i++)
8384 symtable2[i].name
8385 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8386 symtable2[i].u.isym->st_name);
8387
8388 /* Sort symbol by name. */
8389 qsort (symtable1, count1, sizeof (struct elf_symbol),
8390 elf_sym_name_compare);
8391 qsort (symtable2, count1, sizeof (struct elf_symbol),
8392 elf_sym_name_compare);
8393
8394 for (i = 0; i < count1; i++)
8395 /* Two symbols must have the same binding, type and name. */
8396 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8397 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8398 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8399 goto done;
8400
8401 result = TRUE;
8402
dc1e8a47 8403 done:
c9594989
AM
8404 free (symtable1);
8405 free (symtable2);
8406 free (isymbuf1);
8407 free (isymbuf2);
4d269e42
AM
8408
8409 return result;
8410}
8411
8412/* Return TRUE if 2 section types are compatible. */
8413
8414bfd_boolean
8415_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8416 bfd *bbfd, const asection *bsec)
8417{
8418 if (asec == NULL
8419 || bsec == NULL
8420 || abfd->xvec->flavour != bfd_target_elf_flavour
8421 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8422 return TRUE;
8423
8424 return elf_section_type (asec) == elf_section_type (bsec);
8425}
8426\f
c152c796
AM
8427/* Final phase of ELF linker. */
8428
8429/* A structure we use to avoid passing large numbers of arguments. */
8430
8431struct elf_final_link_info
8432{
8433 /* General link information. */
8434 struct bfd_link_info *info;
8435 /* Output BFD. */
8436 bfd *output_bfd;
8437 /* Symbol string table. */
ef10c3ac 8438 struct elf_strtab_hash *symstrtab;
c152c796
AM
8439 /* .hash section. */
8440 asection *hash_sec;
8441 /* symbol version section (.gnu.version). */
8442 asection *symver_sec;
8443 /* Buffer large enough to hold contents of any section. */
8444 bfd_byte *contents;
8445 /* Buffer large enough to hold external relocs of any section. */
8446 void *external_relocs;
8447 /* Buffer large enough to hold internal relocs of any section. */
8448 Elf_Internal_Rela *internal_relocs;
8449 /* Buffer large enough to hold external local symbols of any input
8450 BFD. */
8451 bfd_byte *external_syms;
8452 /* And a buffer for symbol section indices. */
8453 Elf_External_Sym_Shndx *locsym_shndx;
8454 /* Buffer large enough to hold internal local symbols of any input
8455 BFD. */
8456 Elf_Internal_Sym *internal_syms;
8457 /* Array large enough to hold a symbol index for each local symbol
8458 of any input BFD. */
8459 long *indices;
8460 /* Array large enough to hold a section pointer for each local
8461 symbol of any input BFD. */
8462 asection **sections;
ef10c3ac 8463 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 8464 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
8465 /* Number of STT_FILE syms seen. */
8466 size_t filesym_count;
496afd17
L
8467 /* Local symbol hash table. */
8468 struct bfd_hash_table local_hash_table;
c152c796
AM
8469};
8470
496afd17
L
8471struct local_hash_entry
8472{
8473 /* Base hash table entry structure. */
8474 struct bfd_hash_entry root;
8475 /* Size of the local symbol name. */
8476 size_t size;
8477 /* Number of the duplicated local symbol names. */
8478 long count;
8479};
8480
8481/* Create an entry in the local symbol hash table. */
8482
8483static struct bfd_hash_entry *
8484local_hash_newfunc (struct bfd_hash_entry *entry,
8485 struct bfd_hash_table *table,
8486 const char *string)
8487{
8488
8489 /* Allocate the structure if it has not already been allocated by a
8490 subclass. */
8491 if (entry == NULL)
8492 {
8493 entry = bfd_hash_allocate (table,
8494 sizeof (struct local_hash_entry));
8495 if (entry == NULL)
8496 return entry;
8497 }
8498
8499 /* Call the allocation method of the superclass. */
8500 entry = bfd_hash_newfunc (entry, table, string);
8501 if (entry != NULL)
8502 {
8503 ((struct local_hash_entry *) entry)->count = 0;
8504 ((struct local_hash_entry *) entry)->size = 0;
8505 }
8506
8507 return entry;
8508}
8509
c152c796
AM
8510/* This struct is used to pass information to elf_link_output_extsym. */
8511
8512struct elf_outext_info
8513{
8514 bfd_boolean failed;
8515 bfd_boolean localsyms;
34a79995 8516 bfd_boolean file_sym_done;
8b127cbc 8517 struct elf_final_link_info *flinfo;
c152c796
AM
8518};
8519
d9352518
DB
8520
8521/* Support for evaluating a complex relocation.
8522
8523 Complex relocations are generalized, self-describing relocations. The
8524 implementation of them consists of two parts: complex symbols, and the
a0c8462f 8525 relocations themselves.
d9352518 8526
4b69ce9b 8527 The relocations use a reserved elf-wide relocation type code (R_RELC
d9352518
DB
8528 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8529 information (start bit, end bit, word width, etc) into the addend. This
8530 information is extracted from CGEN-generated operand tables within gas.
8531
4b69ce9b 8532 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
d9352518
DB
8533 internal) representing prefix-notation expressions, including but not
8534 limited to those sorts of expressions normally encoded as addends in the
8535 addend field. The symbol mangling format is:
8536
8537 <node> := <literal>
07d6d2b8
AM
8538 | <unary-operator> ':' <node>
8539 | <binary-operator> ':' <node> ':' <node>
d9352518
DB
8540 ;
8541
8542 <literal> := 's' <digits=N> ':' <N character symbol name>
07d6d2b8 8543 | 'S' <digits=N> ':' <N character section name>
d9352518
DB
8544 | '#' <hexdigits>
8545 ;
8546
8547 <binary-operator> := as in C
8548 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8549
8550static void
a0c8462f
AM
8551set_symbol_value (bfd *bfd_with_globals,
8552 Elf_Internal_Sym *isymbuf,
8553 size_t locsymcount,
8554 size_t symidx,
8555 bfd_vma val)
d9352518 8556{
8977835c
AM
8557 struct elf_link_hash_entry **sym_hashes;
8558 struct elf_link_hash_entry *h;
8559 size_t extsymoff = locsymcount;
d9352518 8560
8977835c 8561 if (symidx < locsymcount)
d9352518 8562 {
8977835c
AM
8563 Elf_Internal_Sym *sym;
8564
8565 sym = isymbuf + symidx;
8566 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8567 {
8568 /* It is a local symbol: move it to the
8569 "absolute" section and give it a value. */
8570 sym->st_shndx = SHN_ABS;
8571 sym->st_value = val;
8572 return;
8573 }
8574 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8575 extsymoff = 0;
d9352518 8576 }
8977835c
AM
8577
8578 /* It is a global symbol: set its link type
8579 to "defined" and give it a value. */
8580
8581 sym_hashes = elf_sym_hashes (bfd_with_globals);
8582 h = sym_hashes [symidx - extsymoff];
8583 while (h->root.type == bfd_link_hash_indirect
8584 || h->root.type == bfd_link_hash_warning)
8585 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8586 h->root.type = bfd_link_hash_defined;
8587 h->root.u.def.value = val;
8588 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
8589}
8590
a0c8462f
AM
8591static bfd_boolean
8592resolve_symbol (const char *name,
8593 bfd *input_bfd,
8b127cbc 8594 struct elf_final_link_info *flinfo,
a0c8462f
AM
8595 bfd_vma *result,
8596 Elf_Internal_Sym *isymbuf,
8597 size_t locsymcount)
d9352518 8598{
a0c8462f
AM
8599 Elf_Internal_Sym *sym;
8600 struct bfd_link_hash_entry *global_entry;
8601 const char *candidate = NULL;
8602 Elf_Internal_Shdr *symtab_hdr;
8603 size_t i;
8604
d9352518
DB
8605 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8606
8607 for (i = 0; i < locsymcount; ++ i)
8608 {
8977835c 8609 sym = isymbuf + i;
d9352518
DB
8610
8611 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8612 continue;
8613
8614 candidate = bfd_elf_string_from_elf_section (input_bfd,
8615 symtab_hdr->sh_link,
8616 sym->st_name);
8617#ifdef DEBUG
0f02bbd9
AM
8618 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8619 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
8620#endif
8621 if (candidate && strcmp (candidate, name) == 0)
8622 {
8b127cbc 8623 asection *sec = flinfo->sections [i];
d9352518 8624
0f02bbd9
AM
8625 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8626 *result += sec->output_offset + sec->output_section->vma;
d9352518 8627#ifdef DEBUG
0f02bbd9
AM
8628 printf ("Found symbol with value %8.8lx\n",
8629 (unsigned long) *result);
d9352518
DB
8630#endif
8631 return TRUE;
8632 }
8633 }
8634
8635 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 8636 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 8637 FALSE, FALSE, TRUE);
d9352518
DB
8638 if (!global_entry)
8639 return FALSE;
a0c8462f 8640
d9352518
DB
8641 if (global_entry->type == bfd_link_hash_defined
8642 || global_entry->type == bfd_link_hash_defweak)
8643 {
a0c8462f
AM
8644 *result = (global_entry->u.def.value
8645 + global_entry->u.def.section->output_section->vma
8646 + global_entry->u.def.section->output_offset);
d9352518 8647#ifdef DEBUG
0f02bbd9
AM
8648 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8649 global_entry->root.string, (unsigned long) *result);
d9352518
DB
8650#endif
8651 return TRUE;
a0c8462f 8652 }
d9352518 8653
d9352518
DB
8654 return FALSE;
8655}
8656
37b01f6a
DG
8657/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8658 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8659 names like "foo.end" which is the end address of section "foo". */
07d6d2b8 8660
d9352518 8661static bfd_boolean
a0c8462f
AM
8662resolve_section (const char *name,
8663 asection *sections,
37b01f6a
DG
8664 bfd_vma *result,
8665 bfd * abfd)
d9352518 8666{
a0c8462f
AM
8667 asection *curr;
8668 unsigned int len;
d9352518 8669
a0c8462f 8670 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8671 if (strcmp (curr->name, name) == 0)
8672 {
8673 *result = curr->vma;
8674 return TRUE;
8675 }
8676
8677 /* Hmm. still haven't found it. try pseudo-section names. */
37b01f6a 8678 /* FIXME: This could be coded more efficiently... */
a0c8462f 8679 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8680 {
8681 len = strlen (curr->name);
a0c8462f 8682 if (len > strlen (name))
d9352518
DB
8683 continue;
8684
8685 if (strncmp (curr->name, name, len) == 0)
8686 {
8687 if (strncmp (".end", name + len, 4) == 0)
8688 {
61826503 8689 *result = (curr->vma
bb294208 8690 + curr->size / bfd_octets_per_byte (abfd, curr));
d9352518
DB
8691 return TRUE;
8692 }
8693
8694 /* Insert more pseudo-section names here, if you like. */
8695 }
8696 }
a0c8462f 8697
d9352518
DB
8698 return FALSE;
8699}
8700
8701static void
a0c8462f 8702undefined_reference (const char *reftype, const char *name)
d9352518 8703{
695344c0 8704 /* xgettext:c-format */
a0c8462f
AM
8705 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8706 reftype, name);
4b69ce9b 8707 bfd_set_error (bfd_error_bad_value);
d9352518
DB
8708}
8709
8710static bfd_boolean
a0c8462f
AM
8711eval_symbol (bfd_vma *result,
8712 const char **symp,
8713 bfd *input_bfd,
8b127cbc 8714 struct elf_final_link_info *flinfo,
a0c8462f
AM
8715 bfd_vma dot,
8716 Elf_Internal_Sym *isymbuf,
8717 size_t locsymcount,
8718 int signed_p)
d9352518 8719{
4b93929b
NC
8720 size_t len;
8721 size_t symlen;
a0c8462f
AM
8722 bfd_vma a;
8723 bfd_vma b;
4b93929b 8724 char symbuf[4096];
0f02bbd9 8725 const char *sym = *symp;
a0c8462f
AM
8726 const char *symend;
8727 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
8728
8729 len = strlen (sym);
8730 symend = sym + len;
8731
4b93929b 8732 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
8733 {
8734 bfd_set_error (bfd_error_invalid_operation);
8735 return FALSE;
8736 }
a0c8462f 8737
d9352518
DB
8738 switch (* sym)
8739 {
8740 case '.':
0f02bbd9
AM
8741 *result = dot;
8742 *symp = sym + 1;
d9352518
DB
8743 return TRUE;
8744
8745 case '#':
0f02bbd9
AM
8746 ++sym;
8747 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
8748 return TRUE;
8749
8750 case 'S':
8751 symbol_is_section = TRUE;
1a0670f3 8752 /* Fall through. */
a0c8462f 8753 case 's':
0f02bbd9
AM
8754 ++sym;
8755 symlen = strtol (sym, (char **) symp, 10);
8756 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 8757
4b93929b 8758 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
8759 {
8760 bfd_set_error (bfd_error_invalid_operation);
8761 return FALSE;
8762 }
8763
8764 memcpy (symbuf, sym, symlen);
a0c8462f 8765 symbuf[symlen] = '\0';
0f02bbd9 8766 *symp = sym + symlen;
a0c8462f
AM
8767
8768 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
8769 the symbol as a section, or vice-versa. so we're pretty liberal in our
8770 interpretation here; section means "try section first", not "must be a
8771 section", and likewise with symbol. */
8772
a0c8462f 8773 if (symbol_is_section)
d9352518 8774 {
37b01f6a 8775 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8b127cbc 8776 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8777 isymbuf, locsymcount))
d9352518
DB
8778 {
8779 undefined_reference ("section", symbuf);
8780 return FALSE;
8781 }
a0c8462f
AM
8782 }
8783 else
d9352518 8784 {
8b127cbc 8785 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8786 isymbuf, locsymcount)
8b127cbc 8787 && !resolve_section (symbuf, flinfo->output_bfd->sections,
37b01f6a 8788 result, input_bfd))
d9352518
DB
8789 {
8790 undefined_reference ("symbol", symbuf);
8791 return FALSE;
8792 }
8793 }
8794
8795 return TRUE;
a0c8462f 8796
d9352518
DB
8797 /* All that remains are operators. */
8798
8799#define UNARY_OP(op) \
8800 if (strncmp (sym, #op, strlen (#op)) == 0) \
8801 { \
8802 sym += strlen (#op); \
a0c8462f
AM
8803 if (*sym == ':') \
8804 ++sym; \
0f02bbd9 8805 *symp = sym; \
8b127cbc 8806 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8807 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8808 return FALSE; \
8809 if (signed_p) \
0f02bbd9 8810 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
8811 else \
8812 *result = op a; \
d9352518
DB
8813 return TRUE; \
8814 }
8815
4b69ce9b 8816#define BINARY_OP_HEAD(op) \
d9352518
DB
8817 if (strncmp (sym, #op, strlen (#op)) == 0) \
8818 { \
8819 sym += strlen (#op); \
a0c8462f
AM
8820 if (*sym == ':') \
8821 ++sym; \
0f02bbd9 8822 *symp = sym; \
8b127cbc 8823 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8824 isymbuf, locsymcount, signed_p)) \
a0c8462f 8825 return FALSE; \
0f02bbd9 8826 ++*symp; \
8b127cbc 8827 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 8828 isymbuf, locsymcount, signed_p)) \
4b69ce9b
AM
8829 return FALSE;
8830#define BINARY_OP_TAIL(op) \
a0c8462f 8831 if (signed_p) \
0f02bbd9 8832 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
8833 else \
8834 *result = a op b; \
d9352518
DB
8835 return TRUE; \
8836 }
4b69ce9b 8837#define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
d9352518
DB
8838
8839 default:
8840 UNARY_OP (0-);
4b69ce9b
AM
8841 BINARY_OP_HEAD (<<);
8842 if (b >= sizeof (a) * CHAR_BIT)
8843 {
8844 *result = 0;
8845 return TRUE;
8846 }
8847 signed_p = 0;
8848 BINARY_OP_TAIL (<<);
8849 BINARY_OP_HEAD (>>);
8850 if (b >= sizeof (a) * CHAR_BIT)
8851 {
8852 *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
8853 return TRUE;
8854 }
8855 BINARY_OP_TAIL (>>);
d9352518
DB
8856 BINARY_OP (==);
8857 BINARY_OP (!=);
8858 BINARY_OP (<=);
8859 BINARY_OP (>=);
8860 BINARY_OP (&&);
8861 BINARY_OP (||);
8862 UNARY_OP (~);
8863 UNARY_OP (!);
8864 BINARY_OP (*);
4b69ce9b
AM
8865 BINARY_OP_HEAD (/);
8866 if (b == 0)
8867 {
8868 _bfd_error_handler (_("division by zero"));
8869 bfd_set_error (bfd_error_bad_value);
8870 return FALSE;
8871 }
8872 BINARY_OP_TAIL (/);
8873 BINARY_OP_HEAD (%);
8874 if (b == 0)
8875 {
8876 _bfd_error_handler (_("division by zero"));
8877 bfd_set_error (bfd_error_bad_value);
8878 return FALSE;
8879 }
8880 BINARY_OP_TAIL (%);
d9352518
DB
8881 BINARY_OP (^);
8882 BINARY_OP (|);
8883 BINARY_OP (&);
8884 BINARY_OP (+);
8885 BINARY_OP (-);
8886 BINARY_OP (<);
8887 BINARY_OP (>);
8888#undef UNARY_OP
8889#undef BINARY_OP
8890 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8891 bfd_set_error (bfd_error_invalid_operation);
8892 return FALSE;
8893 }
8894}
8895
d9352518 8896static void
a0c8462f
AM
8897put_value (bfd_vma size,
8898 unsigned long chunksz,
8899 bfd *input_bfd,
8900 bfd_vma x,
8901 bfd_byte *location)
d9352518
DB
8902{
8903 location += (size - chunksz);
8904
41cd1ad1 8905 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
8906 {
8907 switch (chunksz)
8908 {
d9352518
DB
8909 case 1:
8910 bfd_put_8 (input_bfd, x, location);
41cd1ad1 8911 x >>= 8;
d9352518
DB
8912 break;
8913 case 2:
8914 bfd_put_16 (input_bfd, x, location);
41cd1ad1 8915 x >>= 16;
d9352518
DB
8916 break;
8917 case 4:
8918 bfd_put_32 (input_bfd, x, location);
65164438
NC
8919 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8920 x >>= 16;
8921 x >>= 16;
d9352518 8922 break;
d9352518 8923#ifdef BFD64
41cd1ad1 8924 case 8:
d9352518 8925 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
8926 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8927 x >>= 32;
8928 x >>= 32;
8929 break;
d9352518 8930#endif
41cd1ad1
NC
8931 default:
8932 abort ();
d9352518
DB
8933 break;
8934 }
8935 }
8936}
8937
a0c8462f
AM
8938static bfd_vma
8939get_value (bfd_vma size,
8940 unsigned long chunksz,
8941 bfd *input_bfd,
8942 bfd_byte *location)
d9352518 8943{
9b239e0e 8944 int shift;
d9352518
DB
8945 bfd_vma x = 0;
8946
9b239e0e
NC
8947 /* Sanity checks. */
8948 BFD_ASSERT (chunksz <= sizeof (x)
8949 && size >= chunksz
8950 && chunksz != 0
8951 && (size % chunksz) == 0
8952 && input_bfd != NULL
8953 && location != NULL);
8954
8955 if (chunksz == sizeof (x))
8956 {
8957 BFD_ASSERT (size == chunksz);
8958
8959 /* Make sure that we do not perform an undefined shift operation.
8960 We know that size == chunksz so there will only be one iteration
8961 of the loop below. */
8962 shift = 0;
8963 }
8964 else
8965 shift = 8 * chunksz;
8966
a0c8462f 8967 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8968 {
8969 switch (chunksz)
8970 {
d9352518 8971 case 1:
9b239e0e 8972 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8973 break;
8974 case 2:
9b239e0e 8975 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8976 break;
8977 case 4:
9b239e0e 8978 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8979 break;
d9352518 8980#ifdef BFD64
9b239e0e
NC
8981 case 8:
8982 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8983 break;
9b239e0e
NC
8984#endif
8985 default:
8986 abort ();
d9352518
DB
8987 }
8988 }
8989 return x;
8990}
8991
a0c8462f
AM
8992static void
8993decode_complex_addend (unsigned long *start, /* in bits */
8994 unsigned long *oplen, /* in bits */
8995 unsigned long *len, /* in bits */
8996 unsigned long *wordsz, /* in bytes */
8997 unsigned long *chunksz, /* in bytes */
8998 unsigned long *lsb0_p,
8999 unsigned long *signed_p,
9000 unsigned long *trunc_p,
9001 unsigned long encoded)
d9352518 9002{
07d6d2b8
AM
9003 * start = encoded & 0x3F;
9004 * len = (encoded >> 6) & 0x3F;
d9352518
DB
9005 * oplen = (encoded >> 12) & 0x3F;
9006 * wordsz = (encoded >> 18) & 0xF;
9007 * chunksz = (encoded >> 22) & 0xF;
9008 * lsb0_p = (encoded >> 27) & 1;
9009 * signed_p = (encoded >> 28) & 1;
9010 * trunc_p = (encoded >> 29) & 1;
9011}
9012
cdfeee4f 9013bfd_reloc_status_type
0f02bbd9 9014bfd_elf_perform_complex_relocation (bfd *input_bfd,
bb294208 9015 asection *input_section,
0f02bbd9
AM
9016 bfd_byte *contents,
9017 Elf_Internal_Rela *rel,
9018 bfd_vma relocation)
d9352518 9019{
0f02bbd9
AM
9020 bfd_vma shift, x, mask;
9021 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 9022 bfd_reloc_status_type r;
bb294208 9023 bfd_size_type octets;
d9352518
DB
9024
9025 /* Perform this reloc, since it is complex.
9026 (this is not to say that it necessarily refers to a complex
9027 symbol; merely that it is a self-describing CGEN based reloc.
9028 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 9029 word size, etc) encoded within it.). */
d9352518 9030
a0c8462f
AM
9031 decode_complex_addend (&start, &oplen, &len, &wordsz,
9032 &chunksz, &lsb0_p, &signed_p,
9033 &trunc_p, rel->r_addend);
d9352518
DB
9034
9035 mask = (((1L << (len - 1)) - 1) << 1) | 1;
9036
9037 if (lsb0_p)
9038 shift = (start + 1) - len;
9039 else
9040 shift = (8 * wordsz) - (start + len);
9041
bb294208
AM
9042 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
9043 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
d9352518
DB
9044
9045#ifdef DEBUG
9046 printf ("Doing complex reloc: "
9047 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9048 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9049 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9050 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
9051 oplen, (unsigned long) x, (unsigned long) mask,
9052 (unsigned long) relocation);
d9352518
DB
9053#endif
9054
cdfeee4f 9055 r = bfd_reloc_ok;
d9352518 9056 if (! trunc_p)
cdfeee4f
AM
9057 /* Now do an overflow check. */
9058 r = bfd_check_overflow ((signed_p
9059 ? complain_overflow_signed
9060 : complain_overflow_unsigned),
9061 len, 0, (8 * wordsz),
9062 relocation);
a0c8462f 9063
d9352518
DB
9064 /* Do the deed. */
9065 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9066
9067#ifdef DEBUG
9068 printf (" relocation: %8.8lx\n"
9069 " shifted mask: %8.8lx\n"
9070 " shifted/masked reloc: %8.8lx\n"
9071 " result: %8.8lx\n",
9ccb8af9
AM
9072 (unsigned long) relocation, (unsigned long) (mask << shift),
9073 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 9074#endif
bb294208 9075 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
cdfeee4f 9076 return r;
d9352518
DB
9077}
9078
0e287786
AM
9079/* Functions to read r_offset from external (target order) reloc
9080 entry. Faster than bfd_getl32 et al, because we let the compiler
9081 know the value is aligned. */
53df40a4 9082
0e287786
AM
9083static bfd_vma
9084ext32l_r_offset (const void *p)
53df40a4
AM
9085{
9086 union aligned32
9087 {
9088 uint32_t v;
9089 unsigned char c[4];
9090 };
9091 const union aligned32 *a
0e287786 9092 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
9093
9094 uint32_t aval = ( (uint32_t) a->c[0]
9095 | (uint32_t) a->c[1] << 8
9096 | (uint32_t) a->c[2] << 16
9097 | (uint32_t) a->c[3] << 24);
0e287786 9098 return aval;
53df40a4
AM
9099}
9100
0e287786
AM
9101static bfd_vma
9102ext32b_r_offset (const void *p)
53df40a4
AM
9103{
9104 union aligned32
9105 {
9106 uint32_t v;
9107 unsigned char c[4];
9108 };
9109 const union aligned32 *a
0e287786 9110 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
9111
9112 uint32_t aval = ( (uint32_t) a->c[0] << 24
9113 | (uint32_t) a->c[1] << 16
9114 | (uint32_t) a->c[2] << 8
9115 | (uint32_t) a->c[3]);
0e287786 9116 return aval;
53df40a4
AM
9117}
9118
9119#ifdef BFD_HOST_64_BIT
0e287786
AM
9120static bfd_vma
9121ext64l_r_offset (const void *p)
53df40a4
AM
9122{
9123 union aligned64
9124 {
9125 uint64_t v;
9126 unsigned char c[8];
9127 };
9128 const union aligned64 *a
0e287786 9129 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
9130
9131 uint64_t aval = ( (uint64_t) a->c[0]
9132 | (uint64_t) a->c[1] << 8
9133 | (uint64_t) a->c[2] << 16
9134 | (uint64_t) a->c[3] << 24
9135 | (uint64_t) a->c[4] << 32
9136 | (uint64_t) a->c[5] << 40
9137 | (uint64_t) a->c[6] << 48
9138 | (uint64_t) a->c[7] << 56);
0e287786 9139 return aval;
53df40a4
AM
9140}
9141
0e287786
AM
9142static bfd_vma
9143ext64b_r_offset (const void *p)
53df40a4
AM
9144{
9145 union aligned64
9146 {
9147 uint64_t v;
9148 unsigned char c[8];
9149 };
9150 const union aligned64 *a
0e287786 9151 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
9152
9153 uint64_t aval = ( (uint64_t) a->c[0] << 56
9154 | (uint64_t) a->c[1] << 48
9155 | (uint64_t) a->c[2] << 40
9156 | (uint64_t) a->c[3] << 32
9157 | (uint64_t) a->c[4] << 24
9158 | (uint64_t) a->c[5] << 16
9159 | (uint64_t) a->c[6] << 8
9160 | (uint64_t) a->c[7]);
0e287786 9161 return aval;
53df40a4
AM
9162}
9163#endif
9164
c152c796
AM
9165/* When performing a relocatable link, the input relocations are
9166 preserved. But, if they reference global symbols, the indices
d4730f92
BS
9167 referenced must be updated. Update all the relocations found in
9168 RELDATA. */
c152c796 9169
bca6d0e3 9170static bfd_boolean
c152c796 9171elf_link_adjust_relocs (bfd *abfd,
9eaff861 9172 asection *sec,
28dbcedc 9173 struct bfd_elf_section_reloc_data *reldata,
10bbbc1d
NC
9174 bfd_boolean sort,
9175 struct bfd_link_info *info)
c152c796
AM
9176{
9177 unsigned int i;
9178 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9179 bfd_byte *erela;
9180 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9181 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9182 bfd_vma r_type_mask;
9183 int r_sym_shift;
d4730f92
BS
9184 unsigned int count = reldata->count;
9185 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 9186
d4730f92 9187 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
9188 {
9189 swap_in = bed->s->swap_reloc_in;
9190 swap_out = bed->s->swap_reloc_out;
9191 }
d4730f92 9192 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
9193 {
9194 swap_in = bed->s->swap_reloca_in;
9195 swap_out = bed->s->swap_reloca_out;
9196 }
9197 else
9198 abort ();
9199
9200 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9201 abort ();
9202
9203 if (bed->s->arch_size == 32)
9204 {
9205 r_type_mask = 0xff;
9206 r_sym_shift = 8;
9207 }
9208 else
9209 {
9210 r_type_mask = 0xffffffff;
9211 r_sym_shift = 32;
9212 }
9213
d4730f92
BS
9214 erela = reldata->hdr->contents;
9215 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
9216 {
9217 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9218 unsigned int j;
9219
9220 if (*rel_hash == NULL)
9221 continue;
9222
10bbbc1d
NC
9223 if ((*rel_hash)->indx == -2
9224 && info->gc_sections
9225 && ! info->gc_keep_exported)
9226 {
9227 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9793eb77 9228 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
10bbbc1d
NC
9229 abfd, sec,
9230 (*rel_hash)->root.root.string);
9793eb77 9231 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
d42c267e 9232 abfd, sec);
10bbbc1d
NC
9233 bfd_set_error (bfd_error_invalid_operation);
9234 return FALSE;
9235 }
c152c796
AM
9236 BFD_ASSERT ((*rel_hash)->indx >= 0);
9237
9238 (*swap_in) (abfd, erela, irela);
9239 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9240 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9241 | (irela[j].r_info & r_type_mask));
9242 (*swap_out) (abfd, irela, erela);
9243 }
53df40a4 9244
9eaff861
AO
9245 if (bed->elf_backend_update_relocs)
9246 (*bed->elf_backend_update_relocs) (sec, reldata);
9247
0e287786 9248 if (sort && count != 0)
53df40a4 9249 {
0e287786
AM
9250 bfd_vma (*ext_r_off) (const void *);
9251 bfd_vma r_off;
9252 size_t elt_size;
9253 bfd_byte *base, *end, *p, *loc;
bca6d0e3 9254 bfd_byte *buf = NULL;
28dbcedc
AM
9255
9256 if (bed->s->arch_size == 32)
9257 {
9258 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 9259 ext_r_off = ext32l_r_offset;
28dbcedc 9260 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 9261 ext_r_off = ext32b_r_offset;
28dbcedc
AM
9262 else
9263 abort ();
9264 }
53df40a4 9265 else
28dbcedc 9266 {
53df40a4 9267#ifdef BFD_HOST_64_BIT
28dbcedc 9268 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 9269 ext_r_off = ext64l_r_offset;
28dbcedc 9270 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 9271 ext_r_off = ext64b_r_offset;
28dbcedc 9272 else
53df40a4 9273#endif
28dbcedc
AM
9274 abort ();
9275 }
0e287786 9276
bca6d0e3
AM
9277 /* Must use a stable sort here. A modified insertion sort,
9278 since the relocs are mostly sorted already. */
0e287786
AM
9279 elt_size = reldata->hdr->sh_entsize;
9280 base = reldata->hdr->contents;
9281 end = base + count * elt_size;
bca6d0e3 9282 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
9283 abort ();
9284
9285 /* Ensure the first element is lowest. This acts as a sentinel,
9286 speeding the main loop below. */
9287 r_off = (*ext_r_off) (base);
9288 for (p = loc = base; (p += elt_size) < end; )
9289 {
9290 bfd_vma r_off2 = (*ext_r_off) (p);
9291 if (r_off > r_off2)
9292 {
9293 r_off = r_off2;
9294 loc = p;
9295 }
9296 }
9297 if (loc != base)
9298 {
9299 /* Don't just swap *base and *loc as that changes the order
9300 of the original base[0] and base[1] if they happen to
9301 have the same r_offset. */
bca6d0e3
AM
9302 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9303 memcpy (onebuf, loc, elt_size);
0e287786 9304 memmove (base + elt_size, base, loc - base);
bca6d0e3 9305 memcpy (base, onebuf, elt_size);
0e287786
AM
9306 }
9307
b29b8669 9308 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
9309 {
9310 /* base to p is sorted, *p is next to insert. */
9311 r_off = (*ext_r_off) (p);
9312 /* Search the sorted region for location to insert. */
9313 loc = p - elt_size;
9314 while (r_off < (*ext_r_off) (loc))
9315 loc -= elt_size;
9316 loc += elt_size;
9317 if (loc != p)
9318 {
bca6d0e3
AM
9319 /* Chances are there is a run of relocs to insert here,
9320 from one of more input files. Files are not always
9321 linked in order due to the way elf_link_input_bfd is
9322 called. See pr17666. */
9323 size_t sortlen = p - loc;
9324 bfd_vma r_off2 = (*ext_r_off) (loc);
9325 size_t runlen = elt_size;
9326 size_t buf_size = 96 * 1024;
9327 while (p + runlen < end
9328 && (sortlen <= buf_size
9329 || runlen + elt_size <= buf_size)
9330 && r_off2 > (*ext_r_off) (p + runlen))
9331 runlen += elt_size;
9332 if (buf == NULL)
9333 {
9334 buf = bfd_malloc (buf_size);
9335 if (buf == NULL)
9336 return FALSE;
9337 }
9338 if (runlen < sortlen)
9339 {
9340 memcpy (buf, p, runlen);
9341 memmove (loc + runlen, loc, sortlen);
9342 memcpy (loc, buf, runlen);
9343 }
9344 else
9345 {
9346 memcpy (buf, loc, sortlen);
9347 memmove (loc, p, runlen);
9348 memcpy (loc + runlen, buf, sortlen);
9349 }
b29b8669 9350 p += runlen - elt_size;
0e287786
AM
9351 }
9352 }
9353 /* Hashes are no longer valid. */
28dbcedc
AM
9354 free (reldata->hashes);
9355 reldata->hashes = NULL;
bca6d0e3 9356 free (buf);
53df40a4 9357 }
bca6d0e3 9358 return TRUE;
c152c796
AM
9359}
9360
9361struct elf_link_sort_rela
9362{
9363 union {
9364 bfd_vma offset;
9365 bfd_vma sym_mask;
9366 } u;
9367 enum elf_reloc_type_class type;
9368 /* We use this as an array of size int_rels_per_ext_rel. */
9369 Elf_Internal_Rela rela[1];
9370};
9371
dcea6a95
AM
9372/* qsort stability here and for cmp2 is only an issue if multiple
9373 dynamic relocations are emitted at the same address. But targets
9374 that apply a series of dynamic relocations each operating on the
9375 result of the prior relocation can't use -z combreloc as
9376 implemented anyway. Such schemes tend to be broken by sorting on
9377 symbol index. That leaves dynamic NONE relocs as the only other
9378 case where ld might emit multiple relocs at the same address, and
9379 those are only emitted due to target bugs. */
9380
c152c796
AM
9381static int
9382elf_link_sort_cmp1 (const void *A, const void *B)
9383{
a50b1753
NC
9384 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9385 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
9386 int relativea, relativeb;
9387
9388 relativea = a->type == reloc_class_relative;
9389 relativeb = b->type == reloc_class_relative;
9390
9391 if (relativea < relativeb)
9392 return 1;
9393 if (relativea > relativeb)
9394 return -1;
9395 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9396 return -1;
9397 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9398 return 1;
9399 if (a->rela->r_offset < b->rela->r_offset)
9400 return -1;
9401 if (a->rela->r_offset > b->rela->r_offset)
9402 return 1;
9403 return 0;
9404}
9405
9406static int
9407elf_link_sort_cmp2 (const void *A, const void *B)
9408{
a50b1753
NC
9409 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9410 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 9411
7e612e98 9412 if (a->type < b->type)
c152c796 9413 return -1;
7e612e98 9414 if (a->type > b->type)
c152c796 9415 return 1;
7e612e98 9416 if (a->u.offset < b->u.offset)
c152c796 9417 return -1;
7e612e98 9418 if (a->u.offset > b->u.offset)
c152c796
AM
9419 return 1;
9420 if (a->rela->r_offset < b->rela->r_offset)
9421 return -1;
9422 if (a->rela->r_offset > b->rela->r_offset)
9423 return 1;
9424 return 0;
9425}
9426
9427static size_t
9428elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9429{
3410fea8 9430 asection *dynamic_relocs;
fc66a176
L
9431 asection *rela_dyn;
9432 asection *rel_dyn;
c152c796
AM
9433 bfd_size_type count, size;
9434 size_t i, ret, sort_elt, ext_size;
9435 bfd_byte *sort, *s_non_relative, *p;
9436 struct elf_link_sort_rela *sq;
9437 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9438 int i2e = bed->s->int_rels_per_ext_rel;
61826503 9439 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
c152c796
AM
9440 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9441 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9442 struct bfd_link_order *lo;
9443 bfd_vma r_sym_mask;
3410fea8 9444 bfd_boolean use_rela;
c152c796 9445
3410fea8
NC
9446 /* Find a dynamic reloc section. */
9447 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9448 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9449 if (rela_dyn != NULL && rela_dyn->size > 0
9450 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 9451 {
3410fea8
NC
9452 bfd_boolean use_rela_initialised = FALSE;
9453
9454 /* This is just here to stop gcc from complaining.
c8e44c6d 9455 Its initialization checking code is not perfect. */
3410fea8
NC
9456 use_rela = TRUE;
9457
9458 /* Both sections are present. Examine the sizes
9459 of the indirect sections to help us choose. */
9460 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9461 if (lo->type == bfd_indirect_link_order)
9462 {
9463 asection *o = lo->u.indirect.section;
9464
9465 if ((o->size % bed->s->sizeof_rela) == 0)
9466 {
9467 if ((o->size % bed->s->sizeof_rel) == 0)
9468 /* Section size is divisible by both rel and rela sizes.
9469 It is of no help to us. */
9470 ;
9471 else
9472 {
9473 /* Section size is only divisible by rela. */
535b785f 9474 if (use_rela_initialised && !use_rela)
3410fea8 9475 {
9793eb77 9476 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9477 "they are in more than one size"),
9478 abfd);
3410fea8
NC
9479 bfd_set_error (bfd_error_invalid_operation);
9480 return 0;
9481 }
9482 else
9483 {
9484 use_rela = TRUE;
9485 use_rela_initialised = TRUE;
9486 }
9487 }
9488 }
9489 else if ((o->size % bed->s->sizeof_rel) == 0)
9490 {
9491 /* Section size is only divisible by rel. */
535b785f 9492 if (use_rela_initialised && use_rela)
3410fea8 9493 {
9793eb77 9494 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9495 "they are in more than one size"),
9496 abfd);
3410fea8
NC
9497 bfd_set_error (bfd_error_invalid_operation);
9498 return 0;
9499 }
9500 else
9501 {
9502 use_rela = FALSE;
9503 use_rela_initialised = TRUE;
9504 }
9505 }
9506 else
9507 {
c8e44c6d
AM
9508 /* The section size is not divisible by either -
9509 something is wrong. */
9793eb77 9510 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9511 "they are of an unknown size"), abfd);
3410fea8
NC
9512 bfd_set_error (bfd_error_invalid_operation);
9513 return 0;
9514 }
9515 }
9516
9517 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9518 if (lo->type == bfd_indirect_link_order)
9519 {
9520 asection *o = lo->u.indirect.section;
9521
9522 if ((o->size % bed->s->sizeof_rela) == 0)
9523 {
9524 if ((o->size % bed->s->sizeof_rel) == 0)
9525 /* Section size is divisible by both rel and rela sizes.
9526 It is of no help to us. */
9527 ;
9528 else
9529 {
9530 /* Section size is only divisible by rela. */
535b785f 9531 if (use_rela_initialised && !use_rela)
3410fea8 9532 {
9793eb77 9533 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9534 "they are in more than one size"),
9535 abfd);
3410fea8
NC
9536 bfd_set_error (bfd_error_invalid_operation);
9537 return 0;
9538 }
9539 else
9540 {
9541 use_rela = TRUE;
9542 use_rela_initialised = TRUE;
9543 }
9544 }
9545 }
9546 else if ((o->size % bed->s->sizeof_rel) == 0)
9547 {
9548 /* Section size is only divisible by rel. */
535b785f 9549 if (use_rela_initialised && use_rela)
3410fea8 9550 {
9793eb77 9551 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9552 "they are in more than one size"),
9553 abfd);
3410fea8
NC
9554 bfd_set_error (bfd_error_invalid_operation);
9555 return 0;
9556 }
9557 else
9558 {
9559 use_rela = FALSE;
9560 use_rela_initialised = TRUE;
9561 }
9562 }
9563 else
9564 {
c8e44c6d
AM
9565 /* The section size is not divisible by either -
9566 something is wrong. */
9793eb77 9567 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9568 "they are of an unknown size"), abfd);
3410fea8
NC
9569 bfd_set_error (bfd_error_invalid_operation);
9570 return 0;
9571 }
9572 }
9573
9574 if (! use_rela_initialised)
9575 /* Make a guess. */
9576 use_rela = TRUE;
c152c796 9577 }
fc66a176
L
9578 else if (rela_dyn != NULL && rela_dyn->size > 0)
9579 use_rela = TRUE;
9580 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 9581 use_rela = FALSE;
c152c796 9582 else
fc66a176 9583 return 0;
3410fea8
NC
9584
9585 if (use_rela)
c152c796 9586 {
3410fea8 9587 dynamic_relocs = rela_dyn;
c152c796
AM
9588 ext_size = bed->s->sizeof_rela;
9589 swap_in = bed->s->swap_reloca_in;
9590 swap_out = bed->s->swap_reloca_out;
9591 }
3410fea8
NC
9592 else
9593 {
9594 dynamic_relocs = rel_dyn;
9595 ext_size = bed->s->sizeof_rel;
9596 swap_in = bed->s->swap_reloc_in;
9597 swap_out = bed->s->swap_reloc_out;
9598 }
c152c796
AM
9599
9600 size = 0;
3410fea8 9601 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 9602 if (lo->type == bfd_indirect_link_order)
3410fea8 9603 size += lo->u.indirect.section->size;
c152c796 9604
3410fea8 9605 if (size != dynamic_relocs->size)
c152c796
AM
9606 return 0;
9607
9608 sort_elt = (sizeof (struct elf_link_sort_rela)
9609 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
9610
9611 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
9612 if (count == 0)
9613 return 0;
a50b1753 9614 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 9615
c152c796
AM
9616 if (sort == NULL)
9617 {
9618 (*info->callbacks->warning)
9793eb77 9619 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
c152c796
AM
9620 return 0;
9621 }
9622
9623 if (bed->s->arch_size == 32)
9624 r_sym_mask = ~(bfd_vma) 0xff;
9625 else
9626 r_sym_mask = ~(bfd_vma) 0xffffffff;
9627
3410fea8 9628 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9629 if (lo->type == bfd_indirect_link_order)
9630 {
9631 bfd_byte *erel, *erelend;
9632 asection *o = lo->u.indirect.section;
9633
1da212d6
AM
9634 if (o->contents == NULL && o->size != 0)
9635 {
9636 /* This is a reloc section that is being handled as a normal
9637 section. See bfd_section_from_shdr. We can't combine
9638 relocs in this case. */
9639 free (sort);
9640 return 0;
9641 }
c152c796 9642 erel = o->contents;
eea6121a 9643 erelend = o->contents + o->size;
c8e44c6d 9644 p = sort + o->output_offset * opb / ext_size * sort_elt;
3410fea8 9645
c152c796
AM
9646 while (erel < erelend)
9647 {
9648 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 9649
c152c796 9650 (*swap_in) (abfd, erel, s->rela);
7e612e98 9651 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
9652 s->u.sym_mask = r_sym_mask;
9653 p += sort_elt;
9654 erel += ext_size;
9655 }
9656 }
9657
9658 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9659
9660 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9661 {
9662 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9663 if (s->type != reloc_class_relative)
9664 break;
9665 }
9666 ret = i;
9667 s_non_relative = p;
9668
9669 sq = (struct elf_link_sort_rela *) s_non_relative;
9670 for (; i < count; i++, p += sort_elt)
9671 {
9672 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9673 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9674 sq = sp;
9675 sp->u.offset = sq->rela->r_offset;
9676 }
9677
9678 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9679
c8e44c6d
AM
9680 struct elf_link_hash_table *htab = elf_hash_table (info);
9681 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9682 {
9683 /* We have plt relocs in .rela.dyn. */
9684 sq = (struct elf_link_sort_rela *) sort;
9685 for (i = 0; i < count; i++)
9686 if (sq[count - i - 1].type != reloc_class_plt)
9687 break;
9688 if (i != 0 && htab->srelplt->size == i * ext_size)
9689 {
9690 struct bfd_link_order **plo;
9691 /* Put srelplt link_order last. This is so the output_offset
9692 set in the next loop is correct for DT_JMPREL. */
9693 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9694 if ((*plo)->type == bfd_indirect_link_order
9695 && (*plo)->u.indirect.section == htab->srelplt)
9696 {
9697 lo = *plo;
9698 *plo = lo->next;
9699 }
9700 else
9701 plo = &(*plo)->next;
9702 *plo = lo;
9703 lo->next = NULL;
9704 dynamic_relocs->map_tail.link_order = lo;
9705 }
9706 }
9707
9708 p = sort;
3410fea8 9709 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9710 if (lo->type == bfd_indirect_link_order)
9711 {
9712 bfd_byte *erel, *erelend;
9713 asection *o = lo->u.indirect.section;
9714
9715 erel = o->contents;
eea6121a 9716 erelend = o->contents + o->size;
c8e44c6d 9717 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
c152c796
AM
9718 while (erel < erelend)
9719 {
9720 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9721 (*swap_out) (abfd, s->rela, erel);
9722 p += sort_elt;
9723 erel += ext_size;
9724 }
9725 }
9726
9727 free (sort);
3410fea8 9728 *psec = dynamic_relocs;
c152c796
AM
9729 return ret;
9730}
9731
ef10c3ac 9732/* Add a symbol to the output symbol string table. */
c152c796 9733
6e0b88f1 9734static int
ef10c3ac
L
9735elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9736 const char *name,
9737 Elf_Internal_Sym *elfsym,
9738 asection *input_sec,
9739 struct elf_link_hash_entry *h)
c152c796 9740{
6e0b88f1 9741 int (*output_symbol_hook)
c152c796
AM
9742 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9743 struct elf_link_hash_entry *);
ef10c3ac 9744 struct elf_link_hash_table *hash_table;
c152c796 9745 const struct elf_backend_data *bed;
ef10c3ac 9746 bfd_size_type strtabsize;
c152c796 9747
8539e4e8
AM
9748 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9749
8b127cbc 9750 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
9751 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9752 if (output_symbol_hook != NULL)
9753 {
8b127cbc 9754 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
9755 if (ret != 1)
9756 return ret;
c152c796
AM
9757 }
9758
06f44071
AM
9759 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
9760 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
9761 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
9762 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
9763
ef10c3ac
L
9764 if (name == NULL
9765 || *name == '\0'
9766 || (input_sec->flags & SEC_EXCLUDE))
9767 elfsym->st_name = (unsigned long) -1;
c152c796
AM
9768 else
9769 {
ef10c3ac
L
9770 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9771 to get the final offset for st_name. */
3f2e9699 9772 char *versioned_name = (char *) name;
496afd17
L
9773 if (h != NULL)
9774 {
9775 if (h->versioned == versioned && h->def_dynamic)
9776 {
9777 /* Keep only one '@' for versioned symbols defined in
9778 shared objects. */
9779 char *version = strrchr (name, ELF_VER_CHR);
9780 char *base_end = strchr (name, ELF_VER_CHR);
9781 if (version != base_end)
9782 {
9783 size_t base_len;
9784 size_t len = strlen (name);
9785 versioned_name = bfd_alloc (flinfo->output_bfd, len);
9786 if (versioned_name == NULL)
9787 return 0;
9788 base_len = base_end - name;
9789 memcpy (versioned_name, name, base_len);
9790 memcpy (versioned_name + base_len, version,
9791 len - base_len);
9792 }
9793 }
9794 }
9795 else if (flinfo->info->unique_symbol
9796 && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
3f2e9699 9797 {
496afd17
L
9798 struct local_hash_entry *lh;
9799 switch (ELF_ST_TYPE (elfsym->st_info))
3f2e9699 9800 {
496afd17
L
9801 case STT_FILE:
9802 case STT_SECTION:
9803 break;
9804 default:
9805 lh = (struct local_hash_entry *) bfd_hash_lookup
9806 (&flinfo->local_hash_table, name, TRUE, FALSE);
9807 if (lh == NULL)
3f2e9699 9808 return 0;
496afd17
L
9809 if (lh->count)
9810 {
9811 /* Append ".COUNT" to duplicated local symbols. */
9812 size_t count_len;
9813 size_t base_len = lh->size;
9814 char buf[30];
9815 sprintf (buf, "%lx", lh->count);
9816 if (!base_len)
9817 {
9818 base_len = strlen (name);
9819 lh->size = base_len;
9820 }
9821 count_len = strlen (buf);
9822 versioned_name = bfd_alloc (flinfo->output_bfd,
9823 base_len + count_len + 2);
9824 if (versioned_name == NULL)
9825 return 0;
9826 memcpy (versioned_name, name, base_len);
9827 versioned_name[base_len] = '.';
9828 memcpy (versioned_name + base_len + 1, buf,
9829 count_len + 1);
9830 }
9831 lh->count++;
9832 break;
3f2e9699
L
9833 }
9834 }
ef10c3ac
L
9835 elfsym->st_name
9836 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
3f2e9699 9837 versioned_name, FALSE);
c152c796 9838 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 9839 return 0;
c152c796
AM
9840 }
9841
ef10c3ac
L
9842 hash_table = elf_hash_table (flinfo->info);
9843 strtabsize = hash_table->strtabsize;
9844 if (strtabsize <= hash_table->strtabcount)
c152c796 9845 {
ef10c3ac
L
9846 strtabsize += strtabsize;
9847 hash_table->strtabsize = strtabsize;
9848 strtabsize *= sizeof (*hash_table->strtab);
9849 hash_table->strtab
9850 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9851 strtabsize);
9852 if (hash_table->strtab == NULL)
6e0b88f1 9853 return 0;
c152c796 9854 }
ef10c3ac
L
9855 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9856 hash_table->strtab[hash_table->strtabcount].dest_index
9857 = hash_table->strtabcount;
9858 hash_table->strtab[hash_table->strtabcount].destshndx_index
9859 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9860
ed48ec2e 9861 flinfo->output_bfd->symcount += 1;
ef10c3ac
L
9862 hash_table->strtabcount += 1;
9863
9864 return 1;
9865}
9866
9867/* Swap symbols out to the symbol table and flush the output symbols to
9868 the file. */
9869
9870static bfd_boolean
9871elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9872{
9873 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
986f0783 9874 size_t amt;
ef53be89 9875 size_t i;
ef10c3ac
L
9876 const struct elf_backend_data *bed;
9877 bfd_byte *symbuf;
9878 Elf_Internal_Shdr *hdr;
9879 file_ptr pos;
9880 bfd_boolean ret;
9881
9882 if (!hash_table->strtabcount)
9883 return TRUE;
9884
9885 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9886
9887 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9888
ef10c3ac
L
9889 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9890 symbuf = (bfd_byte *) bfd_malloc (amt);
9891 if (symbuf == NULL)
9892 return FALSE;
1b786873 9893
ef10c3ac 9894 if (flinfo->symshndxbuf)
c152c796 9895 {
ef53be89
AM
9896 amt = sizeof (Elf_External_Sym_Shndx);
9897 amt *= bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9898 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9899 if (flinfo->symshndxbuf == NULL)
c152c796 9900 {
ef10c3ac
L
9901 free (symbuf);
9902 return FALSE;
c152c796 9903 }
c152c796
AM
9904 }
9905
3d16b64e 9906 /* Now swap out the symbols. */
ef10c3ac
L
9907 for (i = 0; i < hash_table->strtabcount; i++)
9908 {
9909 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9910 if (elfsym->sym.st_name == (unsigned long) -1)
9911 elfsym->sym.st_name = 0;
9912 else
9913 elfsym->sym.st_name
9914 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9915 elfsym->sym.st_name);
3d16b64e
NA
9916
9917 /* Inform the linker of the addition of this symbol. */
9918
9919 if (flinfo->info->callbacks->ctf_new_symbol)
9920 flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
9921 &elfsym->sym);
9922
ef10c3ac
L
9923 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9924 ((bfd_byte *) symbuf
9925 + (elfsym->dest_index
9926 * bed->s->sizeof_sym)),
9927 (flinfo->symshndxbuf
9928 + elfsym->destshndx_index));
9929 }
9930
9931 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9932 pos = hdr->sh_offset + hdr->sh_size;
9933 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9934 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9935 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9936 {
9937 hdr->sh_size += amt;
9938 ret = TRUE;
9939 }
9940 else
9941 ret = FALSE;
c152c796 9942
ef10c3ac
L
9943 free (symbuf);
9944
9945 free (hash_table->strtab);
9946 hash_table->strtab = NULL;
9947
9948 return ret;
c152c796
AM
9949}
9950
c0d5a53d
L
9951/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9952
9953static bfd_boolean
9954check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9955{
4fbb74a6
AM
9956 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9957 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
9958 {
9959 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 9960 beyond 64k. */
4eca0228 9961 _bfd_error_handler
695344c0 9962 /* xgettext:c-format */
9793eb77 9963 (_("%pB: too many sections: %d (>= %d)"),
4fbb74a6 9964 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
9965 bfd_set_error (bfd_error_nonrepresentable_section);
9966 return FALSE;
9967 }
9968 return TRUE;
9969}
9970
c152c796
AM
9971/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9972 allowing an unsatisfied unversioned symbol in the DSO to match a
9973 versioned symbol that would normally require an explicit version.
9974 We also handle the case that a DSO references a hidden symbol
9975 which may be satisfied by a versioned symbol in another DSO. */
9976
9977static bfd_boolean
9978elf_link_check_versioned_symbol (struct bfd_link_info *info,
9979 const struct elf_backend_data *bed,
9980 struct elf_link_hash_entry *h)
9981{
9982 bfd *abfd;
9983 struct elf_link_loaded_list *loaded;
9984
9985 if (!is_elf_hash_table (info->hash))
9986 return FALSE;
9987
90c984fc
L
9988 /* Check indirect symbol. */
9989 while (h->root.type == bfd_link_hash_indirect)
9990 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9991
c152c796
AM
9992 switch (h->root.type)
9993 {
9994 default:
9995 abfd = NULL;
9996 break;
9997
9998 case bfd_link_hash_undefined:
9999 case bfd_link_hash_undefweak:
10000 abfd = h->root.u.undef.abfd;
f4ab0e2d
L
10001 if (abfd == NULL
10002 || (abfd->flags & DYNAMIC) == 0
e56f61be 10003 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
10004 return FALSE;
10005 break;
10006
10007 case bfd_link_hash_defined:
10008 case bfd_link_hash_defweak:
10009 abfd = h->root.u.def.section->owner;
10010 break;
10011
10012 case bfd_link_hash_common:
10013 abfd = h->root.u.c.p->section->owner;
10014 break;
10015 }
10016 BFD_ASSERT (abfd != NULL);
10017
e310298c 10018 for (loaded = elf_hash_table (info)->dyn_loaded;
c152c796
AM
10019 loaded != NULL;
10020 loaded = loaded->next)
10021 {
10022 bfd *input;
10023 Elf_Internal_Shdr *hdr;
ef53be89
AM
10024 size_t symcount;
10025 size_t extsymcount;
10026 size_t extsymoff;
c152c796
AM
10027 Elf_Internal_Shdr *versymhdr;
10028 Elf_Internal_Sym *isym;
10029 Elf_Internal_Sym *isymend;
10030 Elf_Internal_Sym *isymbuf;
10031 Elf_External_Versym *ever;
10032 Elf_External_Versym *extversym;
10033
10034 input = loaded->abfd;
10035
10036 /* We check each DSO for a possible hidden versioned definition. */
10037 if (input == abfd
c152c796
AM
10038 || elf_dynversym (input) == 0)
10039 continue;
10040
10041 hdr = &elf_tdata (input)->dynsymtab_hdr;
10042
10043 symcount = hdr->sh_size / bed->s->sizeof_sym;
10044 if (elf_bad_symtab (input))
10045 {
10046 extsymcount = symcount;
10047 extsymoff = 0;
10048 }
10049 else
10050 {
10051 extsymcount = symcount - hdr->sh_info;
10052 extsymoff = hdr->sh_info;
10053 }
10054
10055 if (extsymcount == 0)
10056 continue;
10057
10058 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10059 NULL, NULL, NULL);
10060 if (isymbuf == NULL)
10061 return FALSE;
10062
10063 /* Read in any version definitions. */
10064 versymhdr = &elf_tdata (input)->dynversym_hdr;
c152c796 10065 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
2bb3687b
AM
10066 || (extversym = (Elf_External_Versym *)
10067 _bfd_malloc_and_read (input, versymhdr->sh_size,
10068 versymhdr->sh_size)) == NULL)
c152c796 10069 {
c152c796
AM
10070 free (isymbuf);
10071 return FALSE;
10072 }
10073
10074 ever = extversym + extsymoff;
10075 isymend = isymbuf + extsymcount;
10076 for (isym = isymbuf; isym < isymend; isym++, ever++)
10077 {
10078 const char *name;
10079 Elf_Internal_Versym iver;
10080 unsigned short version_index;
10081
10082 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10083 || isym->st_shndx == SHN_UNDEF)
10084 continue;
10085
10086 name = bfd_elf_string_from_elf_section (input,
10087 hdr->sh_link,
10088 isym->st_name);
10089 if (strcmp (name, h->root.root.string) != 0)
10090 continue;
10091
10092 _bfd_elf_swap_versym_in (input, ever, &iver);
10093
d023c380
L
10094 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10095 && !(h->def_regular
10096 && h->forced_local))
c152c796
AM
10097 {
10098 /* If we have a non-hidden versioned sym, then it should
d023c380
L
10099 have provided a definition for the undefined sym unless
10100 it is defined in a non-shared object and forced local.
10101 */
c152c796
AM
10102 abort ();
10103 }
10104
10105 version_index = iver.vs_vers & VERSYM_VERSION;
10106 if (version_index == 1 || version_index == 2)
10107 {
10108 /* This is the base or first version. We can use it. */
10109 free (extversym);
10110 free (isymbuf);
10111 return TRUE;
10112 }
10113 }
10114
10115 free (extversym);
10116 free (isymbuf);
10117 }
10118
10119 return FALSE;
10120}
10121
b8871f35
L
10122/* Convert ELF common symbol TYPE. */
10123
10124static int
10125elf_link_convert_common_type (struct bfd_link_info *info, int type)
10126{
10127 /* Commom symbol can only appear in relocatable link. */
10128 if (!bfd_link_relocatable (info))
10129 abort ();
10130 switch (info->elf_stt_common)
10131 {
10132 case unchanged:
10133 break;
10134 case elf_stt_common:
10135 type = STT_COMMON;
10136 break;
10137 case no_elf_stt_common:
10138 type = STT_OBJECT;
10139 break;
10140 }
10141 return type;
10142}
10143
c152c796
AM
10144/* Add an external symbol to the symbol table. This is called from
10145 the hash table traversal routine. When generating a shared object,
10146 we go through the symbol table twice. The first time we output
10147 anything that might have been forced to local scope in a version
10148 script. The second time we output the symbols that are still
10149 global symbols. */
10150
10151static bfd_boolean
7686d77d 10152elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 10153{
7686d77d 10154 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 10155 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 10156 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
10157 bfd_boolean strip;
10158 Elf_Internal_Sym sym;
10159 asection *input_sec;
10160 const struct elf_backend_data *bed;
6e0b88f1
AM
10161 long indx;
10162 int ret;
b8871f35 10163 unsigned int type;
c152c796
AM
10164
10165 if (h->root.type == bfd_link_hash_warning)
10166 {
10167 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10168 if (h->root.type == bfd_link_hash_new)
10169 return TRUE;
10170 }
10171
10172 /* Decide whether to output this symbol in this pass. */
10173 if (eoinfo->localsyms)
10174 {
4deb8f71 10175 if (!h->forced_local)
c152c796
AM
10176 return TRUE;
10177 }
10178 else
10179 {
4deb8f71 10180 if (h->forced_local)
c152c796
AM
10181 return TRUE;
10182 }
10183
8b127cbc 10184 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 10185
12ac1cf5 10186 if (h->root.type == bfd_link_hash_undefined)
c152c796 10187 {
12ac1cf5
NC
10188 /* If we have an undefined symbol reference here then it must have
10189 come from a shared library that is being linked in. (Undefined
98da7939
L
10190 references in regular files have already been handled unless
10191 they are in unreferenced sections which are removed by garbage
10192 collection). */
12ac1cf5
NC
10193 bfd_boolean ignore_undef = FALSE;
10194
10195 /* Some symbols may be special in that the fact that they're
10196 undefined can be safely ignored - let backend determine that. */
10197 if (bed->elf_backend_ignore_undef_symbol)
10198 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10199
10200 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 10201 if (!ignore_undef
c54f1524 10202 && h->ref_dynamic_nonweak
8b127cbc
AM
10203 && (!h->ref_regular || flinfo->info->gc_sections)
10204 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10205 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
95a51568
FS
10206 {
10207 flinfo->info->callbacks->undefined_symbol
10208 (flinfo->info, h->root.root.string,
10209 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10210 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10211 && !flinfo->info->warn_unresolved_syms);
10212 }
97196564
L
10213
10214 /* Strip a global symbol defined in a discarded section. */
10215 if (h->indx == -3)
10216 return TRUE;
c152c796
AM
10217 }
10218
10219 /* We should also warn if a forced local symbol is referenced from
10220 shared libraries. */
0e1862bb 10221 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
10222 && h->forced_local
10223 && h->ref_dynamic
371a5866 10224 && h->def_regular
f5385ebf 10225 && !h->dynamic_def
ee659f1f 10226 && h->ref_dynamic_nonweak
8b127cbc 10227 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 10228 {
17d078c5
AM
10229 bfd *def_bfd;
10230 const char *msg;
90c984fc
L
10231 struct elf_link_hash_entry *hi = h;
10232
10233 /* Check indirect symbol. */
10234 while (hi->root.type == bfd_link_hash_indirect)
10235 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
10236
10237 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
695344c0 10238 /* xgettext:c-format */
871b3ab2 10239 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
17d078c5 10240 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
695344c0 10241 /* xgettext:c-format */
871b3ab2 10242 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
17d078c5 10243 else
695344c0 10244 /* xgettext:c-format */
871b3ab2 10245 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
8b127cbc 10246 def_bfd = flinfo->output_bfd;
90c984fc
L
10247 if (hi->root.u.def.section != bfd_abs_section_ptr)
10248 def_bfd = hi->root.u.def.section->owner;
c08bb8dd
AM
10249 _bfd_error_handler (msg, flinfo->output_bfd,
10250 h->root.root.string, def_bfd);
17d078c5 10251 bfd_set_error (bfd_error_bad_value);
c152c796
AM
10252 eoinfo->failed = TRUE;
10253 return FALSE;
10254 }
10255
10256 /* We don't want to output symbols that have never been mentioned by
10257 a regular file, or that we have been told to strip. However, if
10258 h->indx is set to -2, the symbol is used by a reloc and we must
10259 output it. */
d983c8c5 10260 strip = FALSE;
c152c796 10261 if (h->indx == -2)
d983c8c5 10262 ;
f5385ebf 10263 else if ((h->def_dynamic
77cfaee6
AM
10264 || h->ref_dynamic
10265 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
10266 && !h->def_regular
10267 && !h->ref_regular)
c152c796 10268 strip = TRUE;
8b127cbc 10269 else if (flinfo->info->strip == strip_all)
c152c796 10270 strip = TRUE;
8b127cbc
AM
10271 else if (flinfo->info->strip == strip_some
10272 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
10273 h->root.root.string, FALSE, FALSE) == NULL)
10274 strip = TRUE;
d56d55e7
AM
10275 else if ((h->root.type == bfd_link_hash_defined
10276 || h->root.type == bfd_link_hash_defweak)
8b127cbc 10277 && ((flinfo->info->strip_discarded
dbaa2011 10278 && discarded_section (h->root.u.def.section))
ca4be51c
AM
10279 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10280 && h->root.u.def.section->owner != NULL
d56d55e7 10281 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 10282 strip = TRUE;
9e2278f5
AM
10283 else if ((h->root.type == bfd_link_hash_undefined
10284 || h->root.type == bfd_link_hash_undefweak)
10285 && h->root.u.undef.abfd != NULL
10286 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10287 strip = TRUE;
c152c796 10288
b8871f35
L
10289 type = h->type;
10290
c152c796 10291 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
10292 nothing else to do. However, if it is a forced local symbol or
10293 an ifunc symbol we need to give the backend finish_dynamic_symbol
10294 function a chance to make it dynamic. */
c152c796
AM
10295 if (strip
10296 && h->dynindx == -1
b8871f35 10297 && type != STT_GNU_IFUNC
f5385ebf 10298 && !h->forced_local)
c152c796
AM
10299 return TRUE;
10300
10301 sym.st_value = 0;
10302 sym.st_size = h->size;
10303 sym.st_other = h->other;
c152c796
AM
10304 switch (h->root.type)
10305 {
10306 default:
10307 case bfd_link_hash_new:
10308 case bfd_link_hash_warning:
10309 abort ();
10310 return FALSE;
10311
10312 case bfd_link_hash_undefined:
10313 case bfd_link_hash_undefweak:
10314 input_sec = bfd_und_section_ptr;
10315 sym.st_shndx = SHN_UNDEF;
10316 break;
10317
10318 case bfd_link_hash_defined:
10319 case bfd_link_hash_defweak:
10320 {
10321 input_sec = h->root.u.def.section;
10322 if (input_sec->output_section != NULL)
10323 {
10324 sym.st_shndx =
8b127cbc 10325 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
10326 input_sec->output_section);
10327 if (sym.st_shndx == SHN_BAD)
10328 {
4eca0228 10329 _bfd_error_handler
695344c0 10330 /* xgettext:c-format */
871b3ab2 10331 (_("%pB: could not find output section %pA for input section %pA"),
8b127cbc 10332 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 10333 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
10334 eoinfo->failed = TRUE;
10335 return FALSE;
10336 }
10337
10338 /* ELF symbols in relocatable files are section relative,
10339 but in nonrelocatable files they are virtual
10340 addresses. */
10341 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 10342 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10343 {
10344 sym.st_value += input_sec->output_section->vma;
10345 if (h->type == STT_TLS)
10346 {
8b127cbc 10347 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
10348 if (tls_sec != NULL)
10349 sym.st_value -= tls_sec->vma;
c152c796
AM
10350 }
10351 }
10352 }
10353 else
10354 {
10355 BFD_ASSERT (input_sec->owner == NULL
10356 || (input_sec->owner->flags & DYNAMIC) != 0);
10357 sym.st_shndx = SHN_UNDEF;
10358 input_sec = bfd_und_section_ptr;
10359 }
10360 }
10361 break;
10362
10363 case bfd_link_hash_common:
10364 input_sec = h->root.u.c.p->section;
a4d8e49b 10365 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
10366 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10367 break;
10368
10369 case bfd_link_hash_indirect:
10370 /* These symbols are created by symbol versioning. They point
10371 to the decorated version of the name. For example, if the
10372 symbol foo@@GNU_1.2 is the default, which should be used when
10373 foo is used with no version, then we add an indirect symbol
10374 foo which points to foo@@GNU_1.2. We ignore these symbols,
10375 since the indirected symbol is already in the hash table. */
10376 return TRUE;
10377 }
10378
b8871f35
L
10379 if (type == STT_COMMON || type == STT_OBJECT)
10380 switch (h->root.type)
10381 {
10382 case bfd_link_hash_common:
10383 type = elf_link_convert_common_type (flinfo->info, type);
10384 break;
10385 case bfd_link_hash_defined:
10386 case bfd_link_hash_defweak:
10387 if (bed->common_definition (&sym))
10388 type = elf_link_convert_common_type (flinfo->info, type);
10389 else
10390 type = STT_OBJECT;
10391 break;
10392 case bfd_link_hash_undefined:
10393 case bfd_link_hash_undefweak:
10394 break;
10395 default:
10396 abort ();
10397 }
10398
4deb8f71 10399 if (h->forced_local)
b8871f35
L
10400 {
10401 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10402 /* Turn off visibility on local symbol. */
10403 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10404 }
10405 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10406 else if (h->unique_global && h->def_regular)
10407 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10408 else if (h->root.type == bfd_link_hash_undefweak
10409 || h->root.type == bfd_link_hash_defweak)
10410 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10411 else
10412 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10413 sym.st_target_internal = h->target_internal;
10414
c152c796
AM
10415 /* Give the processor backend a chance to tweak the symbol value,
10416 and also to finish up anything that needs to be done for this
10417 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 10418 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 10419 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 10420 if ((h->type == STT_GNU_IFUNC
5f35ea9c 10421 && h->def_regular
0e1862bb 10422 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
10423 || ((h->dynindx != -1
10424 || h->forced_local)
0e1862bb 10425 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
10426 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10427 || h->root.type != bfd_link_hash_undefweak))
10428 || !h->forced_local)
8b127cbc 10429 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
10430 {
10431 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 10432 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
10433 {
10434 eoinfo->failed = TRUE;
10435 return FALSE;
10436 }
10437 }
10438
10439 /* If we are marking the symbol as undefined, and there are no
10440 non-weak references to this symbol from a regular object, then
10441 mark the symbol as weak undefined; if there are non-weak
10442 references, mark the symbol as strong. We can't do this earlier,
10443 because it might not be marked as undefined until the
10444 finish_dynamic_symbol routine gets through with it. */
10445 if (sym.st_shndx == SHN_UNDEF
f5385ebf 10446 && h->ref_regular
c152c796
AM
10447 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10448 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10449 {
10450 int bindtype;
b8871f35 10451 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
10452
10453 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10454 if (type == STT_GNU_IFUNC)
10455 type = STT_FUNC;
c152c796 10456
f5385ebf 10457 if (h->ref_regular_nonweak)
c152c796
AM
10458 bindtype = STB_GLOBAL;
10459 else
10460 bindtype = STB_WEAK;
2955ec4c 10461 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
10462 }
10463
bda987c2
CD
10464 /* If this is a symbol defined in a dynamic library, don't use the
10465 symbol size from the dynamic library. Relinking an executable
10466 against a new library may introduce gratuitous changes in the
10467 executable's symbols if we keep the size. */
10468 if (sym.st_shndx == SHN_UNDEF
10469 && !h->def_regular
10470 && h->def_dynamic)
10471 sym.st_size = 0;
10472
c152c796
AM
10473 /* If a non-weak symbol with non-default visibility is not defined
10474 locally, it is a fatal error. */
0e1862bb 10475 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
10476 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10477 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10478 && h->root.type == bfd_link_hash_undefined
f5385ebf 10479 && !h->def_regular)
c152c796 10480 {
17d078c5
AM
10481 const char *msg;
10482
10483 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
695344c0 10484 /* xgettext:c-format */
871b3ab2 10485 msg = _("%pB: protected symbol `%s' isn't defined");
17d078c5 10486 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
695344c0 10487 /* xgettext:c-format */
871b3ab2 10488 msg = _("%pB: internal symbol `%s' isn't defined");
17d078c5 10489 else
695344c0 10490 /* xgettext:c-format */
871b3ab2 10491 msg = _("%pB: hidden symbol `%s' isn't defined");
4eca0228 10492 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 10493 bfd_set_error (bfd_error_bad_value);
c152c796
AM
10494 eoinfo->failed = TRUE;
10495 return FALSE;
10496 }
10497
10498 /* If this symbol should be put in the .dynsym section, then put it
10499 there now. We already know the symbol index. We also fill in
10500 the entry in the .hash section. */
1c2649ed
EB
10501 if (h->dynindx != -1
10502 && elf_hash_table (flinfo->info)->dynamic_sections_created
10503 && elf_hash_table (flinfo->info)->dynsym != NULL
10504 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
c152c796 10505 {
c152c796
AM
10506 bfd_byte *esym;
10507
90c984fc
L
10508 /* Since there is no version information in the dynamic string,
10509 if there is no version info in symbol version section, we will
1659f720 10510 have a run-time problem if not linking executable, referenced
4deb8f71 10511 by shared library, or not bound locally. */
1659f720 10512 if (h->verinfo.verdef == NULL
0e1862bb 10513 && (!bfd_link_executable (flinfo->info)
1659f720
L
10514 || h->ref_dynamic
10515 || !h->def_regular))
90c984fc
L
10516 {
10517 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10518
10519 if (p && p [1] != '\0')
10520 {
4eca0228 10521 _bfd_error_handler
695344c0 10522 /* xgettext:c-format */
9793eb77 10523 (_("%pB: no symbol version section for versioned symbol `%s'"),
90c984fc
L
10524 flinfo->output_bfd, h->root.root.string);
10525 eoinfo->failed = TRUE;
10526 return FALSE;
10527 }
10528 }
10529
c152c796 10530 sym.st_name = h->dynstr_index;
cae1fbbb
L
10531 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10532 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 10533 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
10534 {
10535 eoinfo->failed = TRUE;
10536 return FALSE;
10537 }
3d16b64e
NA
10538
10539 /* Inform the linker of the addition of this symbol. */
10540
10541 if (flinfo->info->callbacks->ctf_new_dynsym)
10542 flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
10543
8b127cbc 10544 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 10545
8b127cbc 10546 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
10547 {
10548 size_t hash_entry_size;
10549 bfd_byte *bucketpos;
10550 bfd_vma chain;
41198d0c
L
10551 size_t bucketcount;
10552 size_t bucket;
10553
8b127cbc 10554 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 10555 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
10556
10557 hash_entry_size
8b127cbc
AM
10558 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10559 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 10560 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
10561 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10562 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10563 bucketpos);
10564 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10565 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
10566 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10567 }
c152c796 10568
8b127cbc 10569 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
10570 {
10571 Elf_Internal_Versym iversym;
10572 Elf_External_Versym *eversym;
10573
5fa370e4 10574 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
c152c796 10575 {
7b20f099
AM
10576 if (h->verinfo.verdef == NULL
10577 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10578 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
eb6e6af8 10579 iversym.vs_vers = 1;
c152c796
AM
10580 else
10581 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10582 }
10583 else
10584 {
10585 if (h->verinfo.vertree == NULL)
10586 iversym.vs_vers = 1;
10587 else
10588 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 10589 if (flinfo->info->create_default_symver)
3e3b46e5 10590 iversym.vs_vers++;
c152c796
AM
10591 }
10592
422f1182 10593 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 10594 defined locally. */
422f1182 10595 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
10596 iversym.vs_vers |= VERSYM_HIDDEN;
10597
8b127cbc 10598 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 10599 eversym += h->dynindx;
8b127cbc 10600 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
10601 }
10602 }
10603
d983c8c5
AM
10604 /* If the symbol is undefined, and we didn't output it to .dynsym,
10605 strip it from .symtab too. Obviously we can't do this for
10606 relocatable output or when needed for --emit-relocs. */
10607 else if (input_sec == bfd_und_section_ptr
10608 && h->indx != -2
66cae560
NC
10609 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10610 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
0e1862bb 10611 && !bfd_link_relocatable (flinfo->info))
d983c8c5 10612 return TRUE;
66cae560 10613
d983c8c5
AM
10614 /* Also strip others that we couldn't earlier due to dynamic symbol
10615 processing. */
10616 if (strip)
10617 return TRUE;
10618 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
10619 return TRUE;
10620
2ec55de3
AM
10621 /* Output a FILE symbol so that following locals are not associated
10622 with the wrong input file. We need one for forced local symbols
10623 if we've seen more than one FILE symbol or when we have exactly
10624 one FILE symbol but global symbols are present in a file other
10625 than the one with the FILE symbol. We also need one if linker
10626 defined symbols are present. In practice these conditions are
10627 always met, so just emit the FILE symbol unconditionally. */
10628 if (eoinfo->localsyms
10629 && !eoinfo->file_sym_done
10630 && eoinfo->flinfo->filesym_count != 0)
10631 {
10632 Elf_Internal_Sym fsym;
10633
10634 memset (&fsym, 0, sizeof (fsym));
10635 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10636 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
10637 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10638 bfd_und_section_ptr, NULL))
2ec55de3
AM
10639 return FALSE;
10640
10641 eoinfo->file_sym_done = TRUE;
10642 }
10643
8b127cbc 10644 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
10645 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10646 input_sec, h);
6e0b88f1 10647 if (ret == 0)
c152c796
AM
10648 {
10649 eoinfo->failed = TRUE;
10650 return FALSE;
10651 }
6e0b88f1
AM
10652 else if (ret == 1)
10653 h->indx = indx;
10654 else if (h->indx == -2)
10655 abort();
c152c796
AM
10656
10657 return TRUE;
10658}
10659
cdd3575c
AM
10660/* Return TRUE if special handling is done for relocs in SEC against
10661 symbols defined in discarded sections. */
10662
c152c796
AM
10663static bfd_boolean
10664elf_section_ignore_discarded_relocs (asection *sec)
10665{
10666 const struct elf_backend_data *bed;
10667
cdd3575c
AM
10668 switch (sec->sec_info_type)
10669 {
dbaa2011
AM
10670 case SEC_INFO_TYPE_STABS:
10671 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 10672 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
10673 return TRUE;
10674 default:
10675 break;
10676 }
c152c796
AM
10677
10678 bed = get_elf_backend_data (sec->owner);
10679 if (bed->elf_backend_ignore_discarded_relocs != NULL
10680 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10681 return TRUE;
10682
10683 return FALSE;
10684}
10685
9e66c942
AM
10686/* Return a mask saying how ld should treat relocations in SEC against
10687 symbols defined in discarded sections. If this function returns
10688 COMPLAIN set, ld will issue a warning message. If this function
10689 returns PRETEND set, and the discarded section was link-once and the
10690 same size as the kept link-once section, ld will pretend that the
10691 symbol was actually defined in the kept section. Otherwise ld will
10692 zero the reloc (at least that is the intent, but some cooperation by
10693 the target dependent code is needed, particularly for REL targets). */
10694
8a696751
AM
10695unsigned int
10696_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 10697{
9e66c942 10698 if (sec->flags & SEC_DEBUGGING)
69d54b1b 10699 return PRETEND;
cdd3575c
AM
10700
10701 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 10702 return 0;
cdd3575c
AM
10703
10704 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 10705 return 0;
cdd3575c 10706
9e66c942 10707 return COMPLAIN | PRETEND;
cdd3575c
AM
10708}
10709
3d7f7666
L
10710/* Find a match between a section and a member of a section group. */
10711
10712static asection *
c0f00686
L
10713match_group_member (asection *sec, asection *group,
10714 struct bfd_link_info *info)
3d7f7666
L
10715{
10716 asection *first = elf_next_in_group (group);
10717 asection *s = first;
10718
10719 while (s != NULL)
10720 {
c0f00686 10721 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
10722 return s;
10723
83180ade 10724 s = elf_next_in_group (s);
3d7f7666
L
10725 if (s == first)
10726 break;
10727 }
10728
10729 return NULL;
10730}
10731
01b3c8ab 10732/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
10733 to replace it. Return the replacement if it is OK. Otherwise return
10734 NULL. */
01b3c8ab
L
10735
10736asection *
c0f00686 10737_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
10738{
10739 asection *kept;
10740
10741 kept = sec->kept_section;
10742 if (kept != NULL)
10743 {
c2370991 10744 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 10745 kept = match_group_member (sec, kept, info);
58349d00
L
10746 if (kept != NULL)
10747 {
10748 if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10749 != (kept->rawsize != 0 ? kept->rawsize : kept->size))
10750 kept = NULL;
10751 else
10752 {
10753 /* Get the real kept section. */
10754 asection *next;
10755 for (next = kept->kept_section;
10756 next != NULL;
10757 next = next->kept_section)
10758 kept = next;
10759 }
10760 }
c2370991 10761 sec->kept_section = kept;
01b3c8ab
L
10762 }
10763 return kept;
10764}
10765
c152c796
AM
10766/* Link an input file into the linker output file. This function
10767 handles all the sections and relocations of the input file at once.
10768 This is so that we only have to read the local symbols once, and
10769 don't have to keep them in memory. */
10770
10771static bfd_boolean
8b127cbc 10772elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 10773{
ece5ef60 10774 int (*relocate_section)
c152c796
AM
10775 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10776 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10777 bfd *output_bfd;
10778 Elf_Internal_Shdr *symtab_hdr;
10779 size_t locsymcount;
10780 size_t extsymoff;
10781 Elf_Internal_Sym *isymbuf;
10782 Elf_Internal_Sym *isym;
10783 Elf_Internal_Sym *isymend;
10784 long *pindex;
10785 asection **ppsection;
10786 asection *o;
10787 const struct elf_backend_data *bed;
c152c796 10788 struct elf_link_hash_entry **sym_hashes;
310fd250
L
10789 bfd_size_type address_size;
10790 bfd_vma r_type_mask;
10791 int r_sym_shift;
ffbc01cc 10792 bfd_boolean have_file_sym = FALSE;
c152c796 10793
8b127cbc 10794 output_bfd = flinfo->output_bfd;
c152c796
AM
10795 bed = get_elf_backend_data (output_bfd);
10796 relocate_section = bed->elf_backend_relocate_section;
10797
10798 /* If this is a dynamic object, we don't want to do anything here:
10799 we don't want the local symbols, and we don't want the section
10800 contents. */
10801 if ((input_bfd->flags & DYNAMIC) != 0)
10802 return TRUE;
10803
c152c796
AM
10804 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10805 if (elf_bad_symtab (input_bfd))
10806 {
10807 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10808 extsymoff = 0;
10809 }
10810 else
10811 {
10812 locsymcount = symtab_hdr->sh_info;
10813 extsymoff = symtab_hdr->sh_info;
10814 }
10815
99fabbc9
JL
10816 /* Enable GNU OSABI features in the output BFD that are used in the input
10817 BFD. */
10818 if (bed->elf_osabi == ELFOSABI_NONE
10819 || bed->elf_osabi == ELFOSABI_GNU
10820 || bed->elf_osabi == ELFOSABI_FREEBSD)
10821 elf_tdata (output_bfd)->has_gnu_osabi
04f89674
L
10822 |= (elf_tdata (input_bfd)->has_gnu_osabi
10823 & (bfd_link_relocatable (flinfo->info)
10824 ? -1 : ~elf_gnu_osabi_retain));
99fabbc9 10825
c152c796
AM
10826 /* Read the local symbols. */
10827 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10828 if (isymbuf == NULL && locsymcount != 0)
10829 {
10830 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
10831 flinfo->internal_syms,
10832 flinfo->external_syms,
10833 flinfo->locsym_shndx);
c152c796
AM
10834 if (isymbuf == NULL)
10835 return FALSE;
10836 }
10837
10838 /* Find local symbol sections and adjust values of symbols in
10839 SEC_MERGE sections. Write out those local symbols we know are
10840 going into the output file. */
10841 isymend = isymbuf + locsymcount;
8b127cbc 10842 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
10843 isym < isymend;
10844 isym++, pindex++, ppsection++)
10845 {
10846 asection *isec;
10847 const char *name;
10848 Elf_Internal_Sym osym;
6e0b88f1
AM
10849 long indx;
10850 int ret;
c152c796
AM
10851
10852 *pindex = -1;
10853
10854 if (elf_bad_symtab (input_bfd))
10855 {
10856 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10857 {
10858 *ppsection = NULL;
10859 continue;
10860 }
10861 }
10862
10863 if (isym->st_shndx == SHN_UNDEF)
10864 isec = bfd_und_section_ptr;
c152c796
AM
10865 else if (isym->st_shndx == SHN_ABS)
10866 isec = bfd_abs_section_ptr;
10867 else if (isym->st_shndx == SHN_COMMON)
10868 isec = bfd_com_section_ptr;
10869 else
10870 {
cb33740c
AM
10871 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10872 if (isec == NULL)
10873 {
10874 /* Don't attempt to output symbols with st_shnx in the
10875 reserved range other than SHN_ABS and SHN_COMMON. */
6835821b 10876 isec = bfd_und_section_ptr;
cb33740c 10877 }
dbaa2011 10878 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
10879 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10880 isym->st_value =
10881 _bfd_merged_section_offset (output_bfd, &isec,
10882 elf_section_data (isec)->sec_info,
10883 isym->st_value);
c152c796
AM
10884 }
10885
10886 *ppsection = isec;
10887
d983c8c5
AM
10888 /* Don't output the first, undefined, symbol. In fact, don't
10889 output any undefined local symbol. */
10890 if (isec == bfd_und_section_ptr)
c152c796
AM
10891 continue;
10892
10893 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10894 {
10895 /* We never output section symbols. Instead, we use the
10896 section symbol of the corresponding section in the output
10897 file. */
10898 continue;
10899 }
10900
10901 /* If we are stripping all symbols, we don't want to output this
10902 one. */
8b127cbc 10903 if (flinfo->info->strip == strip_all)
c152c796
AM
10904 continue;
10905
10906 /* If we are discarding all local symbols, we don't want to
10907 output this one. If we are generating a relocatable output
10908 file, then some of the local symbols may be required by
10909 relocs; we output them below as we discover that they are
10910 needed. */
8b127cbc 10911 if (flinfo->info->discard == discard_all)
c152c796
AM
10912 continue;
10913
10914 /* If this symbol is defined in a section which we are
f02571c5 10915 discarding, we don't need to keep it. */
abf874aa
CL
10916 if (isym->st_shndx != SHN_UNDEF
10917 && isym->st_shndx < SHN_LORESERVE
10918 && isec->output_section == NULL
10919 && flinfo->info->non_contiguous_regions
10920 && flinfo->info->non_contiguous_regions_warnings)
10921 {
10922 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
10923 "discards section `%s' from '%s'\n"),
765cf5f6 10924 isec->name, bfd_get_filename (isec->owner));
abf874aa
CL
10925 continue;
10926 }
10927
f02571c5 10928 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
10929 && isym->st_shndx < SHN_LORESERVE
10930 && bfd_section_removed_from_list (output_bfd,
10931 isec->output_section))
e75a280b
L
10932 continue;
10933
c152c796
AM
10934 /* Get the name of the symbol. */
10935 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10936 isym->st_name);
10937 if (name == NULL)
10938 return FALSE;
10939
10940 /* See if we are discarding symbols with this name. */
8b127cbc
AM
10941 if ((flinfo->info->strip == strip_some
10942 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 10943 == NULL))
8b127cbc 10944 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
10945 && (isec->flags & SEC_MERGE)
10946 && !bfd_link_relocatable (flinfo->info))
8b127cbc 10947 || flinfo->info->discard == discard_l)
c152c796
AM
10948 && bfd_is_local_label_name (input_bfd, name)))
10949 continue;
10950
ffbc01cc
AM
10951 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10952 {
ce875075
AM
10953 if (input_bfd->lto_output)
10954 /* -flto puts a temp file name here. This means builds
10955 are not reproducible. Discard the symbol. */
10956 continue;
ffbc01cc
AM
10957 have_file_sym = TRUE;
10958 flinfo->filesym_count += 1;
10959 }
10960 if (!have_file_sym)
10961 {
10962 /* In the absence of debug info, bfd_find_nearest_line uses
10963 FILE symbols to determine the source file for local
10964 function symbols. Provide a FILE symbol here if input
10965 files lack such, so that their symbols won't be
10966 associated with a previous input file. It's not the
10967 source file, but the best we can do. */
5b4293ba 10968 const char *filename;
ffbc01cc
AM
10969 have_file_sym = TRUE;
10970 flinfo->filesym_count += 1;
10971 memset (&osym, 0, sizeof (osym));
10972 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10973 osym.st_shndx = SHN_ABS;
5b4293ba
AM
10974 if (input_bfd->lto_output)
10975 filename = NULL;
10976 else
10977 filename = lbasename (bfd_get_filename (input_bfd));
10978 if (!elf_link_output_symstrtab (flinfo, filename, &osym,
10979 bfd_abs_section_ptr, NULL))
ffbc01cc
AM
10980 return FALSE;
10981 }
10982
c152c796
AM
10983 osym = *isym;
10984
10985 /* Adjust the section index for the output file. */
10986 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10987 isec->output_section);
10988 if (osym.st_shndx == SHN_BAD)
10989 return FALSE;
10990
c152c796
AM
10991 /* ELF symbols in relocatable files are section relative, but
10992 in executable files they are virtual addresses. Note that
10993 this code assumes that all ELF sections have an associated
10994 BFD section with a reasonable value for output_offset; below
10995 we assume that they also have a reasonable value for
10996 output_section. Any special sections must be set up to meet
10997 these requirements. */
10998 osym.st_value += isec->output_offset;
0e1862bb 10999 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11000 {
11001 osym.st_value += isec->output_section->vma;
11002 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
11003 {
11004 /* STT_TLS symbols are relative to PT_TLS segment base. */
102def4d
AM
11005 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
11006 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
11007 else
11008 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
11009 STT_NOTYPE);
c152c796
AM
11010 }
11011 }
11012
6e0b88f1 11013 indx = bfd_get_symcount (output_bfd);
ef10c3ac 11014 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 11015 if (ret == 0)
c152c796 11016 return FALSE;
6e0b88f1
AM
11017 else if (ret == 1)
11018 *pindex = indx;
c152c796
AM
11019 }
11020
310fd250
L
11021 if (bed->s->arch_size == 32)
11022 {
11023 r_type_mask = 0xff;
11024 r_sym_shift = 8;
11025 address_size = 4;
11026 }
11027 else
11028 {
11029 r_type_mask = 0xffffffff;
11030 r_sym_shift = 32;
11031 address_size = 8;
11032 }
11033
c152c796
AM
11034 /* Relocate the contents of each section. */
11035 sym_hashes = elf_sym_hashes (input_bfd);
11036 for (o = input_bfd->sections; o != NULL; o = o->next)
11037 {
11038 bfd_byte *contents;
11039
11040 if (! o->linker_mark)
11041 {
11042 /* This section was omitted from the link. */
11043 continue;
11044 }
11045
7bdf4127 11046 if (!flinfo->info->resolve_section_groups
bcacc0f5
AM
11047 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
11048 {
11049 /* Deal with the group signature symbol. */
11050 struct bfd_elf_section_data *sec_data = elf_section_data (o);
11051 unsigned long symndx = sec_data->this_hdr.sh_info;
11052 asection *osec = o->output_section;
11053
7bdf4127 11054 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
bcacc0f5
AM
11055 if (symndx >= locsymcount
11056 || (elf_bad_symtab (input_bfd)
8b127cbc 11057 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
11058 {
11059 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
11060 while (h->root.type == bfd_link_hash_indirect
11061 || h->root.type == bfd_link_hash_warning)
11062 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11063 /* Arrange for symbol to be output. */
11064 h->indx = -2;
11065 elf_section_data (osec)->this_hdr.sh_info = -2;
11066 }
11067 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
11068 {
11069 /* We'll use the output section target_index. */
8b127cbc 11070 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
11071 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
11072 }
11073 else
11074 {
8b127cbc 11075 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
11076 {
11077 /* Otherwise output the local symbol now. */
11078 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 11079 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 11080 const char *name;
6e0b88f1
AM
11081 long indx;
11082 int ret;
bcacc0f5
AM
11083
11084 name = bfd_elf_string_from_elf_section (input_bfd,
11085 symtab_hdr->sh_link,
11086 sym.st_name);
11087 if (name == NULL)
11088 return FALSE;
11089
11090 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11091 sec);
11092 if (sym.st_shndx == SHN_BAD)
11093 return FALSE;
11094
11095 sym.st_value += o->output_offset;
11096
6e0b88f1 11097 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
11098 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11099 NULL);
6e0b88f1 11100 if (ret == 0)
bcacc0f5 11101 return FALSE;
6e0b88f1 11102 else if (ret == 1)
8b127cbc 11103 flinfo->indices[symndx] = indx;
6e0b88f1
AM
11104 else
11105 abort ();
bcacc0f5
AM
11106 }
11107 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 11108 = flinfo->indices[symndx];
bcacc0f5
AM
11109 }
11110 }
11111
c152c796 11112 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 11113 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
11114 continue;
11115
11116 if ((o->flags & SEC_LINKER_CREATED) != 0)
11117 {
11118 /* Section was created by _bfd_elf_link_create_dynamic_sections
11119 or somesuch. */
11120 continue;
11121 }
11122
11123 /* Get the contents of the section. They have been cached by a
11124 relaxation routine. Note that o is a section in an input
11125 file, so the contents field will not have been set by any of
11126 the routines which work on output files. */
11127 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
11128 {
11129 contents = elf_section_data (o)->this_hdr.contents;
11130 if (bed->caches_rawsize
11131 && o->rawsize != 0
11132 && o->rawsize < o->size)
11133 {
11134 memcpy (flinfo->contents, contents, o->rawsize);
11135 contents = flinfo->contents;
11136 }
11137 }
c152c796
AM
11138 else
11139 {
8b127cbc 11140 contents = flinfo->contents;
4a114e3e 11141 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
11142 return FALSE;
11143 }
11144
11145 if ((o->flags & SEC_RELOC) != 0)
11146 {
11147 Elf_Internal_Rela *internal_relocs;
0f02bbd9 11148 Elf_Internal_Rela *rel, *relend;
0f02bbd9 11149 int action_discarded;
ece5ef60 11150 int ret;
c152c796
AM
11151
11152 /* Get the swapped relocs. */
11153 internal_relocs
8b127cbc
AM
11154 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
11155 flinfo->internal_relocs, FALSE);
c152c796
AM
11156 if (internal_relocs == NULL
11157 && o->reloc_count > 0)
11158 return FALSE;
11159
310fd250
L
11160 /* We need to reverse-copy input .ctors/.dtors sections if
11161 they are placed in .init_array/.finit_array for output. */
11162 if (o->size > address_size
11163 && ((strncmp (o->name, ".ctors", 6) == 0
11164 && strcmp (o->output_section->name,
11165 ".init_array") == 0)
11166 || (strncmp (o->name, ".dtors", 6) == 0
11167 && strcmp (o->output_section->name,
11168 ".fini_array") == 0))
11169 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 11170 {
056bafd4
MR
11171 if (o->size * bed->s->int_rels_per_ext_rel
11172 != o->reloc_count * address_size)
310fd250 11173 {
4eca0228 11174 _bfd_error_handler
695344c0 11175 /* xgettext:c-format */
871b3ab2 11176 (_("error: %pB: size of section %pA is not "
310fd250
L
11177 "multiple of address size"),
11178 input_bfd, o);
8c6716e5 11179 bfd_set_error (bfd_error_bad_value);
310fd250
L
11180 return FALSE;
11181 }
11182 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
11183 }
11184
0f02bbd9 11185 action_discarded = -1;
c152c796 11186 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
11187 action_discarded = (*bed->action_discarded) (o);
11188
11189 /* Run through the relocs evaluating complex reloc symbols and
11190 looking for relocs against symbols from discarded sections
11191 or section symbols from removed link-once sections.
11192 Complain about relocs against discarded sections. Zero
11193 relocs against removed link-once sections. */
11194
11195 rel = internal_relocs;
056bafd4 11196 relend = rel + o->reloc_count;
0f02bbd9 11197 for ( ; rel < relend; rel++)
c152c796 11198 {
0f02bbd9
AM
11199 unsigned long r_symndx = rel->r_info >> r_sym_shift;
11200 unsigned int s_type;
11201 asection **ps, *sec;
11202 struct elf_link_hash_entry *h = NULL;
11203 const char *sym_name;
c152c796 11204
0f02bbd9
AM
11205 if (r_symndx == STN_UNDEF)
11206 continue;
c152c796 11207
0f02bbd9
AM
11208 if (r_symndx >= locsymcount
11209 || (elf_bad_symtab (input_bfd)
8b127cbc 11210 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
11211 {
11212 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 11213
0f02bbd9
AM
11214 /* Badly formatted input files can contain relocs that
11215 reference non-existant symbols. Check here so that
11216 we do not seg fault. */
11217 if (h == NULL)
c152c796 11218 {
4eca0228 11219 _bfd_error_handler
695344c0 11220 /* xgettext:c-format */
2dcf00ce 11221 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
0f02bbd9 11222 "that references a non-existent global symbol"),
2dcf00ce 11223 input_bfd, (uint64_t) rel->r_info, o);
0f02bbd9
AM
11224 bfd_set_error (bfd_error_bad_value);
11225 return FALSE;
11226 }
3b36f7e6 11227
0f02bbd9
AM
11228 while (h->root.type == bfd_link_hash_indirect
11229 || h->root.type == bfd_link_hash_warning)
11230 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 11231
0f02bbd9 11232 s_type = h->type;
cdd3575c 11233
9e2dec47 11234 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
11235 mark the symbol as undefined. Note that the
11236 linker may attach linker created dynamic sections
11237 to the plugin bfd. Symbols defined in linker
11238 created sections are not plugin symbols. */
bc4e12de 11239 if ((h->root.non_ir_ref_regular
4070765b 11240 || h->root.non_ir_ref_dynamic)
9e2dec47
L
11241 && (h->root.type == bfd_link_hash_defined
11242 || h->root.type == bfd_link_hash_defweak)
11243 && (h->root.u.def.section->flags
11244 & SEC_LINKER_CREATED) == 0
11245 && h->root.u.def.section->owner != NULL
11246 && (h->root.u.def.section->owner->flags
11247 & BFD_PLUGIN) != 0)
11248 {
11249 h->root.type = bfd_link_hash_undefined;
11250 h->root.u.undef.abfd = h->root.u.def.section->owner;
11251 }
11252
0f02bbd9
AM
11253 ps = NULL;
11254 if (h->root.type == bfd_link_hash_defined
11255 || h->root.type == bfd_link_hash_defweak)
11256 ps = &h->root.u.def.section;
11257
11258 sym_name = h->root.root.string;
11259 }
11260 else
11261 {
11262 Elf_Internal_Sym *sym = isymbuf + r_symndx;
11263
11264 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 11265 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
11266 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11267 sym, *ps);
11268 }
c152c796 11269
c301e700 11270 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 11271 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
11272 {
11273 bfd_vma val;
11274 bfd_vma dot = (rel->r_offset
11275 + o->output_offset + o->output_section->vma);
11276#ifdef DEBUG
11277 printf ("Encountered a complex symbol!");
11278 printf (" (input_bfd %s, section %s, reloc %ld\n",
765cf5f6 11279 bfd_get_filename (input_bfd), o->name,
9ccb8af9 11280 (long) (rel - internal_relocs));
0f02bbd9
AM
11281 printf (" symbol: idx %8.8lx, name %s\n",
11282 r_symndx, sym_name);
11283 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11284 (unsigned long) rel->r_info,
11285 (unsigned long) rel->r_offset);
11286#endif
8b127cbc 11287 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
11288 isymbuf, locsymcount, s_type == STT_SRELC))
11289 return FALSE;
11290
11291 /* Symbol evaluated OK. Update to absolute value. */
11292 set_symbol_value (input_bfd, isymbuf, locsymcount,
11293 r_symndx, val);
11294 continue;
11295 }
11296
11297 if (action_discarded != -1 && ps != NULL)
11298 {
cdd3575c
AM
11299 /* Complain if the definition comes from a
11300 discarded section. */
dbaa2011 11301 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 11302 {
cf35638d 11303 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 11304 if (action_discarded & COMPLAIN)
8b127cbc 11305 (*flinfo->info->callbacks->einfo)
695344c0 11306 /* xgettext:c-format */
871b3ab2
AM
11307 (_("%X`%s' referenced in section `%pA' of %pB: "
11308 "defined in discarded section `%pA' of %pB\n"),
e1fffbe6 11309 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 11310
87e5235d 11311 /* Try to do the best we can to support buggy old
e0ae6d6f 11312 versions of gcc. Pretend that the symbol is
87e5235d
AM
11313 really defined in the kept linkonce section.
11314 FIXME: This is quite broken. Modifying the
11315 symbol here means we will be changing all later
e0ae6d6f 11316 uses of the symbol, not just in this section. */
0f02bbd9 11317 if (action_discarded & PRETEND)
87e5235d 11318 {
01b3c8ab
L
11319 asection *kept;
11320
c0f00686 11321 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 11322 flinfo->info);
01b3c8ab 11323 if (kept != NULL)
87e5235d
AM
11324 {
11325 *ps = kept;
11326 continue;
11327 }
11328 }
c152c796
AM
11329 }
11330 }
11331 }
11332
11333 /* Relocate the section by invoking a back end routine.
11334
11335 The back end routine is responsible for adjusting the
11336 section contents as necessary, and (if using Rela relocs
11337 and generating a relocatable output file) adjusting the
11338 reloc addend as necessary.
11339
11340 The back end routine does not have to worry about setting
11341 the reloc address or the reloc symbol index.
11342
11343 The back end routine is given a pointer to the swapped in
11344 internal symbols, and can access the hash table entries
11345 for the external symbols via elf_sym_hashes (input_bfd).
11346
11347 When generating relocatable output, the back end routine
11348 must handle STB_LOCAL/STT_SECTION symbols specially. The
11349 output symbol is going to be a section symbol
11350 corresponding to the output section, which will require
11351 the addend to be adjusted. */
11352
8b127cbc 11353 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
11354 input_bfd, o, contents,
11355 internal_relocs,
11356 isymbuf,
8b127cbc 11357 flinfo->sections);
ece5ef60 11358 if (!ret)
c152c796
AM
11359 return FALSE;
11360
ece5ef60 11361 if (ret == 2
0e1862bb 11362 || bfd_link_relocatable (flinfo->info)
8b127cbc 11363 || flinfo->info->emitrelocations)
c152c796
AM
11364 {
11365 Elf_Internal_Rela *irela;
d4730f92 11366 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
11367 bfd_vma last_offset;
11368 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
11369 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11370 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 11371 unsigned int next_erel;
c152c796 11372 bfd_boolean rela_normal;
d4730f92 11373 struct bfd_elf_section_data *esdi, *esdo;
c152c796 11374
d4730f92
BS
11375 esdi = elf_section_data (o);
11376 esdo = elf_section_data (o->output_section);
11377 rela_normal = FALSE;
c152c796
AM
11378
11379 /* Adjust the reloc addresses and symbol indices. */
11380
11381 irela = internal_relocs;
056bafd4 11382 irelaend = irela + o->reloc_count;
d4730f92
BS
11383 rel_hash = esdo->rel.hashes + esdo->rel.count;
11384 /* We start processing the REL relocs, if any. When we reach
11385 IRELAMID in the loop, we switch to the RELA relocs. */
11386 irelamid = irela;
11387 if (esdi->rel.hdr != NULL)
11388 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11389 * bed->s->int_rels_per_ext_rel);
eac338cf 11390 rel_hash_list = rel_hash;
d4730f92 11391 rela_hash_list = NULL;
c152c796 11392 last_offset = o->output_offset;
0e1862bb 11393 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11394 last_offset += o->output_section->vma;
11395 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11396 {
11397 unsigned long r_symndx;
11398 asection *sec;
11399 Elf_Internal_Sym sym;
11400
11401 if (next_erel == bed->s->int_rels_per_ext_rel)
11402 {
11403 rel_hash++;
11404 next_erel = 0;
11405 }
11406
d4730f92
BS
11407 if (irela == irelamid)
11408 {
11409 rel_hash = esdo->rela.hashes + esdo->rela.count;
11410 rela_hash_list = rel_hash;
11411 rela_normal = bed->rela_normal;
11412 }
11413
c152c796 11414 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 11415 flinfo->info, o,
c152c796
AM
11416 irela->r_offset);
11417 if (irela->r_offset >= (bfd_vma) -2)
11418 {
11419 /* This is a reloc for a deleted entry or somesuch.
11420 Turn it into an R_*_NONE reloc, at the same
11421 offset as the last reloc. elf_eh_frame.c and
e460dd0d 11422 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
11423 being ordered. */
11424 irela->r_offset = last_offset;
11425 irela->r_info = 0;
11426 irela->r_addend = 0;
11427 continue;
11428 }
11429
11430 irela->r_offset += o->output_offset;
11431
11432 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 11433 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11434 irela->r_offset += o->output_section->vma;
11435
11436 last_offset = irela->r_offset;
11437
11438 r_symndx = irela->r_info >> r_sym_shift;
11439 if (r_symndx == STN_UNDEF)
11440 continue;
11441
11442 if (r_symndx >= locsymcount
11443 || (elf_bad_symtab (input_bfd)
8b127cbc 11444 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
11445 {
11446 struct elf_link_hash_entry *rh;
11447 unsigned long indx;
11448
11449 /* This is a reloc against a global symbol. We
11450 have not yet output all the local symbols, so
11451 we do not know the symbol index of any global
11452 symbol. We set the rel_hash entry for this
11453 reloc to point to the global hash table entry
11454 for this symbol. The symbol index is then
ee75fd95 11455 set at the end of bfd_elf_final_link. */
c152c796
AM
11456 indx = r_symndx - extsymoff;
11457 rh = elf_sym_hashes (input_bfd)[indx];
11458 while (rh->root.type == bfd_link_hash_indirect
11459 || rh->root.type == bfd_link_hash_warning)
11460 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11461
11462 /* Setting the index to -2 tells
11463 elf_link_output_extsym that this symbol is
11464 used by a reloc. */
11465 BFD_ASSERT (rh->indx < 0);
11466 rh->indx = -2;
c152c796
AM
11467 *rel_hash = rh;
11468
11469 continue;
11470 }
11471
11472 /* This is a reloc against a local symbol. */
11473
11474 *rel_hash = NULL;
11475 sym = isymbuf[r_symndx];
8b127cbc 11476 sec = flinfo->sections[r_symndx];
c152c796
AM
11477 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11478 {
11479 /* I suppose the backend ought to fill in the
11480 section of any STT_SECTION symbol against a
6a8d1586 11481 processor specific section. */
cf35638d 11482 r_symndx = STN_UNDEF;
6a8d1586
AM
11483 if (bfd_is_abs_section (sec))
11484 ;
c152c796
AM
11485 else if (sec == NULL || sec->owner == NULL)
11486 {
11487 bfd_set_error (bfd_error_bad_value);
11488 return FALSE;
11489 }
11490 else
11491 {
6a8d1586
AM
11492 asection *osec = sec->output_section;
11493
11494 /* If we have discarded a section, the output
11495 section will be the absolute section. In
ab96bf03
AM
11496 case of discarded SEC_MERGE sections, use
11497 the kept section. relocate_section should
11498 have already handled discarded linkonce
11499 sections. */
6a8d1586
AM
11500 if (bfd_is_abs_section (osec)
11501 && sec->kept_section != NULL
11502 && sec->kept_section->output_section != NULL)
11503 {
11504 osec = sec->kept_section->output_section;
11505 irela->r_addend -= osec->vma;
11506 }
11507
11508 if (!bfd_is_abs_section (osec))
11509 {
11510 r_symndx = osec->target_index;
cf35638d 11511 if (r_symndx == STN_UNDEF)
74541ad4 11512 {
051d833a
AM
11513 irela->r_addend += osec->vma;
11514 osec = _bfd_nearby_section (output_bfd, osec,
11515 osec->vma);
11516 irela->r_addend -= osec->vma;
11517 r_symndx = osec->target_index;
74541ad4 11518 }
6a8d1586 11519 }
c152c796
AM
11520 }
11521
11522 /* Adjust the addend according to where the
11523 section winds up in the output section. */
11524 if (rela_normal)
11525 irela->r_addend += sec->output_offset;
11526 }
11527 else
11528 {
8b127cbc 11529 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
11530 {
11531 unsigned long shlink;
11532 const char *name;
11533 asection *osec;
6e0b88f1 11534 long indx;
c152c796 11535
8b127cbc 11536 if (flinfo->info->strip == strip_all)
c152c796
AM
11537 {
11538 /* You can't do ld -r -s. */
11539 bfd_set_error (bfd_error_invalid_operation);
11540 return FALSE;
11541 }
11542
11543 /* This symbol was skipped earlier, but
11544 since it is needed by a reloc, we
11545 must output it now. */
11546 shlink = symtab_hdr->sh_link;
11547 name = (bfd_elf_string_from_elf_section
11548 (input_bfd, shlink, sym.st_name));
11549 if (name == NULL)
11550 return FALSE;
11551
11552 osec = sec->output_section;
11553 sym.st_shndx =
11554 _bfd_elf_section_from_bfd_section (output_bfd,
11555 osec);
11556 if (sym.st_shndx == SHN_BAD)
11557 return FALSE;
11558
11559 sym.st_value += sec->output_offset;
0e1862bb 11560 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11561 {
11562 sym.st_value += osec->vma;
11563 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11564 {
102def4d
AM
11565 struct elf_link_hash_table *htab
11566 = elf_hash_table (flinfo->info);
11567
c152c796
AM
11568 /* STT_TLS symbols are relative to PT_TLS
11569 segment base. */
102def4d
AM
11570 if (htab->tls_sec != NULL)
11571 sym.st_value -= htab->tls_sec->vma;
11572 else
11573 sym.st_info
11574 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11575 STT_NOTYPE);
c152c796
AM
11576 }
11577 }
11578
6e0b88f1 11579 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
11580 ret = elf_link_output_symstrtab (flinfo, name,
11581 &sym, sec,
11582 NULL);
6e0b88f1 11583 if (ret == 0)
c152c796 11584 return FALSE;
6e0b88f1 11585 else if (ret == 1)
8b127cbc 11586 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
11587 else
11588 abort ();
c152c796
AM
11589 }
11590
8b127cbc 11591 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
11592 }
11593
11594 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11595 | (irela->r_info & r_type_mask));
11596 }
11597
11598 /* Swap out the relocs. */
d4730f92
BS
11599 input_rel_hdr = esdi->rel.hdr;
11600 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 11601 {
d4730f92
BS
11602 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11603 input_rel_hdr,
11604 internal_relocs,
11605 rel_hash_list))
11606 return FALSE;
c152c796
AM
11607 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11608 * bed->s->int_rels_per_ext_rel);
eac338cf 11609 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
11610 }
11611
11612 input_rela_hdr = esdi->rela.hdr;
11613 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11614 {
eac338cf 11615 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 11616 input_rela_hdr,
eac338cf 11617 internal_relocs,
d4730f92 11618 rela_hash_list))
c152c796
AM
11619 return FALSE;
11620 }
11621 }
11622 }
11623
11624 /* Write out the modified section contents. */
11625 if (bed->elf_backend_write_section
8b127cbc 11626 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 11627 contents))
c152c796
AM
11628 {
11629 /* Section written out. */
11630 }
11631 else switch (o->sec_info_type)
11632 {
dbaa2011 11633 case SEC_INFO_TYPE_STABS:
c152c796
AM
11634 if (! (_bfd_write_section_stabs
11635 (output_bfd,
8b127cbc 11636 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
11637 o, &elf_section_data (o)->sec_info, contents)))
11638 return FALSE;
11639 break;
dbaa2011 11640 case SEC_INFO_TYPE_MERGE:
c152c796
AM
11641 if (! _bfd_write_merged_section (output_bfd, o,
11642 elf_section_data (o)->sec_info))
11643 return FALSE;
11644 break;
dbaa2011 11645 case SEC_INFO_TYPE_EH_FRAME:
c152c796 11646 {
8b127cbc 11647 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
11648 o, contents))
11649 return FALSE;
11650 }
11651 break;
2f0c68f2
CM
11652 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11653 {
11654 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11655 flinfo->info,
11656 o, contents))
11657 return FALSE;
11658 }
11659 break;
c152c796
AM
11660 default:
11661 {
310fd250
L
11662 if (! (o->flags & SEC_EXCLUDE))
11663 {
11664 file_ptr offset = (file_ptr) o->output_offset;
11665 bfd_size_type todo = o->size;
37b01f6a 11666
bb294208 11667 offset *= bfd_octets_per_byte (output_bfd, o);
37b01f6a 11668
310fd250
L
11669 if ((o->flags & SEC_ELF_REVERSE_COPY))
11670 {
11671 /* Reverse-copy input section to output. */
11672 do
11673 {
11674 todo -= address_size;
11675 if (! bfd_set_section_contents (output_bfd,
11676 o->output_section,
11677 contents + todo,
11678 offset,
11679 address_size))
11680 return FALSE;
11681 if (todo == 0)
11682 break;
11683 offset += address_size;
11684 }
11685 while (1);
11686 }
11687 else if (! bfd_set_section_contents (output_bfd,
11688 o->output_section,
11689 contents,
11690 offset, todo))
11691 return FALSE;
11692 }
c152c796
AM
11693 }
11694 break;
11695 }
11696 }
11697
11698 return TRUE;
11699}
11700
11701/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 11702 requested by the linker, and does not come from any input file. This
c152c796
AM
11703 is used to build constructor and destructor tables when linking
11704 with -Ur. */
11705
11706static bfd_boolean
11707elf_reloc_link_order (bfd *output_bfd,
11708 struct bfd_link_info *info,
11709 asection *output_section,
11710 struct bfd_link_order *link_order)
11711{
11712 reloc_howto_type *howto;
11713 long indx;
11714 bfd_vma offset;
11715 bfd_vma addend;
d4730f92 11716 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
11717 struct elf_link_hash_entry **rel_hash_ptr;
11718 Elf_Internal_Shdr *rel_hdr;
11719 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11720 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11721 bfd_byte *erel;
11722 unsigned int i;
d4730f92 11723 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
11724
11725 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11726 if (howto == NULL)
11727 {
11728 bfd_set_error (bfd_error_bad_value);
11729 return FALSE;
11730 }
11731
11732 addend = link_order->u.reloc.p->addend;
11733
d4730f92
BS
11734 if (esdo->rel.hdr)
11735 reldata = &esdo->rel;
11736 else if (esdo->rela.hdr)
11737 reldata = &esdo->rela;
11738 else
11739 {
11740 reldata = NULL;
11741 BFD_ASSERT (0);
11742 }
11743
c152c796 11744 /* Figure out the symbol index. */
d4730f92 11745 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
11746 if (link_order->type == bfd_section_reloc_link_order)
11747 {
11748 indx = link_order->u.reloc.p->u.section->target_index;
11749 BFD_ASSERT (indx != 0);
11750 *rel_hash_ptr = NULL;
11751 }
11752 else
11753 {
11754 struct elf_link_hash_entry *h;
11755
11756 /* Treat a reloc against a defined symbol as though it were
11757 actually against the section. */
11758 h = ((struct elf_link_hash_entry *)
11759 bfd_wrapped_link_hash_lookup (output_bfd, info,
11760 link_order->u.reloc.p->u.name,
11761 FALSE, FALSE, TRUE));
11762 if (h != NULL
11763 && (h->root.type == bfd_link_hash_defined
11764 || h->root.type == bfd_link_hash_defweak))
11765 {
11766 asection *section;
11767
11768 section = h->root.u.def.section;
11769 indx = section->output_section->target_index;
11770 *rel_hash_ptr = NULL;
11771 /* It seems that we ought to add the symbol value to the
11772 addend here, but in practice it has already been added
11773 because it was passed to constructor_callback. */
11774 addend += section->output_section->vma + section->output_offset;
11775 }
11776 else if (h != NULL)
11777 {
11778 /* Setting the index to -2 tells elf_link_output_extsym that
11779 this symbol is used by a reloc. */
11780 h->indx = -2;
11781 *rel_hash_ptr = h;
11782 indx = 0;
11783 }
11784 else
11785 {
1a72702b
AM
11786 (*info->callbacks->unattached_reloc)
11787 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
c152c796
AM
11788 indx = 0;
11789 }
11790 }
11791
11792 /* If this is an inplace reloc, we must write the addend into the
11793 object file. */
11794 if (howto->partial_inplace && addend != 0)
11795 {
11796 bfd_size_type size;
11797 bfd_reloc_status_type rstat;
11798 bfd_byte *buf;
11799 bfd_boolean ok;
11800 const char *sym_name;
bb294208 11801 bfd_size_type octets;
c152c796 11802
a50b1753
NC
11803 size = (bfd_size_type) bfd_get_reloc_size (howto);
11804 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 11805 if (buf == NULL && size != 0)
c152c796
AM
11806 return FALSE;
11807 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11808 switch (rstat)
11809 {
11810 case bfd_reloc_ok:
11811 break;
11812
11813 default:
11814 case bfd_reloc_outofrange:
11815 abort ();
11816
11817 case bfd_reloc_overflow:
11818 if (link_order->type == bfd_section_reloc_link_order)
fd361982 11819 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
c152c796
AM
11820 else
11821 sym_name = link_order->u.reloc.p->u.name;
1a72702b
AM
11822 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11823 howto->name, addend, NULL, NULL,
11824 (bfd_vma) 0);
c152c796
AM
11825 break;
11826 }
37b01f6a 11827
bb294208
AM
11828 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
11829 output_section);
c152c796 11830 ok = bfd_set_section_contents (output_bfd, output_section, buf,
bb294208 11831 octets, size);
c152c796
AM
11832 free (buf);
11833 if (! ok)
11834 return FALSE;
11835 }
11836
11837 /* The address of a reloc is relative to the section in a
11838 relocatable file, and is a virtual address in an executable
11839 file. */
11840 offset = link_order->offset;
0e1862bb 11841 if (! bfd_link_relocatable (info))
c152c796
AM
11842 offset += output_section->vma;
11843
11844 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11845 {
11846 irel[i].r_offset = offset;
11847 irel[i].r_info = 0;
11848 irel[i].r_addend = 0;
11849 }
11850 if (bed->s->arch_size == 32)
11851 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11852 else
11853 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11854
d4730f92 11855 rel_hdr = reldata->hdr;
c152c796
AM
11856 erel = rel_hdr->contents;
11857 if (rel_hdr->sh_type == SHT_REL)
11858 {
d4730f92 11859 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
11860 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11861 }
11862 else
11863 {
11864 irel[0].r_addend = addend;
d4730f92 11865 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
11866 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11867 }
11868
d4730f92 11869 ++reldata->count;
c152c796
AM
11870
11871 return TRUE;
11872}
11873
76359541
TP
11874/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11875 Returns TRUE upon success, FALSE otherwise. */
11876
11877static bfd_boolean
11878elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11879{
11880 bfd_boolean ret = FALSE;
11881 bfd *implib_bfd;
11882 const struct elf_backend_data *bed;
11883 flagword flags;
11884 enum bfd_architecture arch;
11885 unsigned int mach;
11886 asymbol **sympp = NULL;
11887 long symsize;
11888 long symcount;
11889 long src_count;
11890 elf_symbol_type *osymbuf;
446f7ed5 11891 size_t amt;
76359541
TP
11892
11893 implib_bfd = info->out_implib_bfd;
11894 bed = get_elf_backend_data (abfd);
11895
11896 if (!bfd_set_format (implib_bfd, bfd_object))
11897 return FALSE;
11898
046734ff 11899 /* Use flag from executable but make it a relocatable object. */
76359541
TP
11900 flags = bfd_get_file_flags (abfd);
11901 flags &= ~HAS_RELOC;
11902 if (!bfd_set_start_address (implib_bfd, 0)
046734ff 11903 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
76359541
TP
11904 return FALSE;
11905
11906 /* Copy architecture of output file to import library file. */
11907 arch = bfd_get_arch (abfd);
11908 mach = bfd_get_mach (abfd);
11909 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11910 && (abfd->target_defaulted
11911 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11912 return FALSE;
11913
11914 /* Get symbol table size. */
11915 symsize = bfd_get_symtab_upper_bound (abfd);
11916 if (symsize < 0)
11917 return FALSE;
11918
11919 /* Read in the symbol table. */
ec9bd0a2
AM
11920 sympp = (asymbol **) bfd_malloc (symsize);
11921 if (sympp == NULL)
11922 return FALSE;
11923
76359541
TP
11924 symcount = bfd_canonicalize_symtab (abfd, sympp);
11925 if (symcount < 0)
11926 goto free_sym_buf;
11927
11928 /* Allow the BFD backend to copy any private header data it
11929 understands from the output BFD to the import library BFD. */
11930 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11931 goto free_sym_buf;
11932
11933 /* Filter symbols to appear in the import library. */
11934 if (bed->elf_backend_filter_implib_symbols)
11935 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11936 symcount);
11937 else
11938 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11939 if (symcount == 0)
11940 {
5df1bc57 11941 bfd_set_error (bfd_error_no_symbols);
871b3ab2 11942 _bfd_error_handler (_("%pB: no symbol found for import library"),
4eca0228 11943 implib_bfd);
76359541
TP
11944 goto free_sym_buf;
11945 }
11946
11947
11948 /* Make symbols absolute. */
446f7ed5
AM
11949 amt = symcount * sizeof (*osymbuf);
11950 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
ec9bd0a2
AM
11951 if (osymbuf == NULL)
11952 goto free_sym_buf;
11953
76359541
TP
11954 for (src_count = 0; src_count < symcount; src_count++)
11955 {
11956 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11957 sizeof (*osymbuf));
11958 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11959 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11960 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11961 osymbuf[src_count].internal_elf_sym.st_value =
11962 osymbuf[src_count].symbol.value;
11963 sympp[src_count] = &osymbuf[src_count].symbol;
11964 }
11965
11966 bfd_set_symtab (implib_bfd, sympp, symcount);
11967
11968 /* Allow the BFD backend to copy any private data it understands
11969 from the output BFD to the import library BFD. This is done last
11970 to permit the routine to look at the filtered symbol table. */
11971 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11972 goto free_sym_buf;
11973
11974 if (!bfd_close (implib_bfd))
11975 goto free_sym_buf;
11976
11977 ret = TRUE;
11978
dc1e8a47 11979 free_sym_buf:
76359541
TP
11980 free (sympp);
11981 return ret;
11982}
11983
9f7c3e5e
AM
11984static void
11985elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11986{
11987 asection *o;
11988
11989 if (flinfo->symstrtab != NULL)
ef10c3ac 11990 _bfd_elf_strtab_free (flinfo->symstrtab);
c9594989
AM
11991 free (flinfo->contents);
11992 free (flinfo->external_relocs);
11993 free (flinfo->internal_relocs);
11994 free (flinfo->external_syms);
11995 free (flinfo->locsym_shndx);
11996 free (flinfo->internal_syms);
11997 free (flinfo->indices);
11998 free (flinfo->sections);
11999 if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
9f7c3e5e
AM
12000 free (flinfo->symshndxbuf);
12001 for (o = obfd->sections; o != NULL; o = o->next)
12002 {
12003 struct bfd_elf_section_data *esdo = elf_section_data (o);
c9594989
AM
12004 free (esdo->rel.hashes);
12005 free (esdo->rela.hashes);
9f7c3e5e
AM
12006 }
12007}
0b52efa6 12008
c152c796
AM
12009/* Do the final step of an ELF link. */
12010
12011bfd_boolean
12012bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
12013{
12014 bfd_boolean dynamic;
12015 bfd_boolean emit_relocs;
12016 bfd *dynobj;
8b127cbc 12017 struct elf_final_link_info flinfo;
91d6fa6a
NC
12018 asection *o;
12019 struct bfd_link_order *p;
12020 bfd *sub;
c152c796
AM
12021 bfd_size_type max_contents_size;
12022 bfd_size_type max_external_reloc_size;
12023 bfd_size_type max_internal_reloc_count;
12024 bfd_size_type max_sym_count;
12025 bfd_size_type max_sym_shndx_count;
c152c796
AM
12026 Elf_Internal_Sym elfsym;
12027 unsigned int i;
12028 Elf_Internal_Shdr *symtab_hdr;
12029 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
12030 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12031 struct elf_outext_info eoinfo;
12032 bfd_boolean merged;
12033 size_t relativecount = 0;
12034 asection *reldyn = 0;
12035 bfd_size_type amt;
104d59d1
JM
12036 asection *attr_section = NULL;
12037 bfd_vma attr_size = 0;
12038 const char *std_attrs_section;
64f52338 12039 struct elf_link_hash_table *htab = elf_hash_table (info);
4c6ee646 12040 bfd_boolean sections_removed;
496afd17 12041 bfd_boolean ret;
c152c796 12042
64f52338 12043 if (!is_elf_hash_table (htab))
c152c796
AM
12044 return FALSE;
12045
0e1862bb 12046 if (bfd_link_pic (info))
c152c796
AM
12047 abfd->flags |= DYNAMIC;
12048
64f52338
AM
12049 dynamic = htab->dynamic_sections_created;
12050 dynobj = htab->dynobj;
c152c796 12051
0e1862bb 12052 emit_relocs = (bfd_link_relocatable (info)
a4676736 12053 || info->emitrelocations);
c152c796 12054
496afd17 12055 memset (&flinfo, 0, sizeof (flinfo));
8b127cbc
AM
12056 flinfo.info = info;
12057 flinfo.output_bfd = abfd;
ef10c3ac 12058 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 12059 if (flinfo.symstrtab == NULL)
c152c796
AM
12060 return FALSE;
12061
12062 if (! dynamic)
12063 {
8b127cbc
AM
12064 flinfo.hash_sec = NULL;
12065 flinfo.symver_sec = NULL;
c152c796
AM
12066 }
12067 else
12068 {
3d4d4302 12069 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 12070 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 12071 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
12072 /* Note that it is OK if symver_sec is NULL. */
12073 }
12074
496afd17
L
12075 if (info->unique_symbol
12076 && !bfd_hash_table_init (&flinfo.local_hash_table,
12077 local_hash_newfunc,
12078 sizeof (struct local_hash_entry)))
12079 return FALSE;
c152c796 12080
104d59d1
JM
12081 /* The object attributes have been merged. Remove the input
12082 sections from the link, and set the contents of the output
1ff6de03 12083 section. */
4c6ee646 12084 sections_removed = FALSE;
104d59d1
JM
12085 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
12086 for (o = abfd->sections; o != NULL; o = o->next)
12087 {
5270eddc 12088 bfd_boolean remove_section = FALSE;
b8a6ced7 12089
104d59d1
JM
12090 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
12091 || strcmp (o->name, ".gnu.attributes") == 0)
12092 {
12093 for (p = o->map_head.link_order; p != NULL; p = p->next)
12094 {
12095 asection *input_section;
12096
12097 if (p->type != bfd_indirect_link_order)
12098 continue;
12099 input_section = p->u.indirect.section;
12100 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12101 elf_link_input_bfd ignores this section. */
12102 input_section->flags &= ~SEC_HAS_CONTENTS;
12103 }
a0c8462f 12104
104d59d1 12105 attr_size = bfd_elf_obj_attr_size (abfd);
fd361982 12106 bfd_set_section_size (o, attr_size);
b8a6ced7
AM
12107 /* Skip this section later on. */
12108 o->map_head.link_order = NULL;
104d59d1 12109 if (attr_size)
b8a6ced7 12110 attr_section = o;
104d59d1 12111 else
5270eddc 12112 remove_section = TRUE;
104d59d1 12113 }
6e5e9d58
AM
12114 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12115 {
12116 /* Remove empty group section from linker output. */
5270eddc 12117 remove_section = TRUE;
b8a6ced7 12118 }
5270eddc 12119 if (remove_section)
b8a6ced7 12120 {
6e5e9d58
AM
12121 o->flags |= SEC_EXCLUDE;
12122 bfd_section_list_remove (abfd, o);
12123 abfd->section_count--;
4c6ee646 12124 sections_removed = TRUE;
6e5e9d58 12125 }
104d59d1 12126 }
4c6ee646
AM
12127 if (sections_removed)
12128 _bfd_fix_excluded_sec_syms (abfd, info);
104d59d1 12129
c152c796
AM
12130 /* Count up the number of relocations we will output for each output
12131 section, so that we know the sizes of the reloc sections. We
12132 also figure out some maximum sizes. */
12133 max_contents_size = 0;
12134 max_external_reloc_size = 0;
12135 max_internal_reloc_count = 0;
12136 max_sym_count = 0;
12137 max_sym_shndx_count = 0;
12138 merged = FALSE;
12139 for (o = abfd->sections; o != NULL; o = o->next)
12140 {
12141 struct bfd_elf_section_data *esdo = elf_section_data (o);
12142 o->reloc_count = 0;
12143
8423293d 12144 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
12145 {
12146 unsigned int reloc_count = 0;
9eaff861 12147 unsigned int additional_reloc_count = 0;
c152c796 12148 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
12149
12150 if (p->type == bfd_section_reloc_link_order
12151 || p->type == bfd_symbol_reloc_link_order)
12152 reloc_count = 1;
12153 else if (p->type == bfd_indirect_link_order)
12154 {
12155 asection *sec;
12156
12157 sec = p->u.indirect.section;
c152c796
AM
12158
12159 /* Mark all sections which are to be included in the
12160 link. This will normally be every section. We need
12161 to do this so that we can identify any sections which
12162 the linker has decided to not include. */
12163 sec->linker_mark = TRUE;
12164
12165 if (sec->flags & SEC_MERGE)
12166 merged = TRUE;
12167
eea6121a
AM
12168 if (sec->rawsize > max_contents_size)
12169 max_contents_size = sec->rawsize;
12170 if (sec->size > max_contents_size)
12171 max_contents_size = sec->size;
c152c796 12172
c152c796
AM
12173 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12174 && (sec->owner->flags & DYNAMIC) == 0)
12175 {
12176 size_t sym_count;
12177
a961cdd5
AM
12178 /* We are interested in just local symbols, not all
12179 symbols. */
c152c796
AM
12180 if (elf_bad_symtab (sec->owner))
12181 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12182 / bed->s->sizeof_sym);
12183 else
12184 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12185
12186 if (sym_count > max_sym_count)
12187 max_sym_count = sym_count;
12188
12189 if (sym_count > max_sym_shndx_count
6a40cf0c 12190 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
12191 max_sym_shndx_count = sym_count;
12192
a961cdd5
AM
12193 if (esdo->this_hdr.sh_type == SHT_REL
12194 || esdo->this_hdr.sh_type == SHT_RELA)
12195 /* Some backends use reloc_count in relocation sections
12196 to count particular types of relocs. Of course,
12197 reloc sections themselves can't have relocations. */
12198 ;
12199 else if (emit_relocs)
12200 {
12201 reloc_count = sec->reloc_count;
12202 if (bed->elf_backend_count_additional_relocs)
12203 {
12204 int c;
12205 c = (*bed->elf_backend_count_additional_relocs) (sec);
12206 additional_reloc_count += c;
12207 }
12208 }
12209 else if (bed->elf_backend_count_relocs)
12210 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12211
12212 esdi = elf_section_data (sec);
12213
c152c796
AM
12214 if ((sec->flags & SEC_RELOC) != 0)
12215 {
d4730f92 12216 size_t ext_size = 0;
c152c796 12217
d4730f92
BS
12218 if (esdi->rel.hdr != NULL)
12219 ext_size = esdi->rel.hdr->sh_size;
12220 if (esdi->rela.hdr != NULL)
12221 ext_size += esdi->rela.hdr->sh_size;
7326c758 12222
c152c796
AM
12223 if (ext_size > max_external_reloc_size)
12224 max_external_reloc_size = ext_size;
12225 if (sec->reloc_count > max_internal_reloc_count)
12226 max_internal_reloc_count = sec->reloc_count;
12227 }
12228 }
12229 }
12230
12231 if (reloc_count == 0)
12232 continue;
12233
9eaff861 12234 reloc_count += additional_reloc_count;
c152c796
AM
12235 o->reloc_count += reloc_count;
12236
0e1862bb 12237 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 12238 {
d4730f92 12239 if (esdi->rel.hdr)
9eaff861 12240 {
491d01d3 12241 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
9eaff861
AO
12242 esdo->rel.count += additional_reloc_count;
12243 }
d4730f92 12244 if (esdi->rela.hdr)
9eaff861 12245 {
491d01d3 12246 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
9eaff861
AO
12247 esdo->rela.count += additional_reloc_count;
12248 }
d4730f92
BS
12249 }
12250 else
12251 {
12252 if (o->use_rela_p)
12253 esdo->rela.count += reloc_count;
2c2b4ed4 12254 else
d4730f92 12255 esdo->rel.count += reloc_count;
c152c796 12256 }
c152c796
AM
12257 }
12258
9eaff861 12259 if (o->reloc_count > 0)
c152c796
AM
12260 o->flags |= SEC_RELOC;
12261 else
12262 {
12263 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12264 set it (this is probably a bug) and if it is set
12265 assign_section_numbers will create a reloc section. */
12266 o->flags &=~ SEC_RELOC;
12267 }
12268
12269 /* If the SEC_ALLOC flag is not set, force the section VMA to
12270 zero. This is done in elf_fake_sections as well, but forcing
12271 the VMA to 0 here will ensure that relocs against these
12272 sections are handled correctly. */
12273 if ((o->flags & SEC_ALLOC) == 0
12274 && ! o->user_set_vma)
12275 o->vma = 0;
12276 }
12277
0e1862bb 12278 if (! bfd_link_relocatable (info) && merged)
64f52338 12279 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
c152c796
AM
12280
12281 /* Figure out the file positions for everything but the symbol table
12282 and the relocs. We set symcount to force assign_section_numbers
12283 to create a symbol table. */
ed48ec2e 12284 abfd->symcount = info->strip != strip_all || emit_relocs;
c152c796
AM
12285 BFD_ASSERT (! abfd->output_has_begun);
12286 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12287 goto error_return;
12288
ee75fd95 12289 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
12290 for (o = abfd->sections; o != NULL; o = o->next)
12291 {
d4730f92 12292 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
12293 if ((o->flags & SEC_RELOC) != 0)
12294 {
d4730f92 12295 if (esdo->rel.hdr
9eaff861 12296 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
12297 goto error_return;
12298
d4730f92 12299 if (esdo->rela.hdr
9eaff861 12300 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
12301 goto error_return;
12302 }
12303
48db3297
AM
12304 /* _bfd_elf_compute_section_file_positions makes temporary use
12305 of target_index. Reset it. */
12306 o->target_index = 0;
12307
c152c796
AM
12308 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12309 to count upwards while actually outputting the relocations. */
d4730f92
BS
12310 esdo->rel.count = 0;
12311 esdo->rela.count = 0;
0ce398f1 12312
1ff6de03
NA
12313 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12314 && !bfd_section_is_ctf (o))
0ce398f1
L
12315 {
12316 /* Cache the section contents so that they can be compressed
12317 later. Use bfd_malloc since it will be freed by
12318 bfd_compress_section_contents. */
12319 unsigned char *contents = esdo->this_hdr.contents;
12320 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
12321 abort ();
12322 contents
12323 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12324 if (contents == NULL)
12325 goto error_return;
12326 esdo->this_hdr.contents = contents;
12327 }
c152c796
AM
12328 }
12329
1ff6de03
NA
12330 /* We have now assigned file positions for all the sections except .symtab,
12331 .strtab, and non-loaded reloc and compressed debugging sections. We start
12332 the .symtab section at the current file position, and write directly to it.
12333 We build the .strtab section in memory. */
ed48ec2e 12334 abfd->symcount = 0;
c152c796
AM
12335 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12336 /* sh_name is set in prep_headers. */
12337 symtab_hdr->sh_type = SHT_SYMTAB;
12338 /* sh_flags, sh_addr and sh_size all start off zero. */
12339 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12340 /* sh_link is set in assign_section_numbers. */
12341 /* sh_info is set below. */
12342 /* sh_offset is set just below. */
72de5009 12343 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 12344
ef10c3ac
L
12345 if (max_sym_count < 20)
12346 max_sym_count = 20;
64f52338 12347 htab->strtabsize = max_sym_count;
ef10c3ac 12348 amt = max_sym_count * sizeof (struct elf_sym_strtab);
64f52338
AM
12349 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12350 if (htab->strtab == NULL)
c152c796 12351 goto error_return;
ef10c3ac
L
12352 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12353 flinfo.symshndxbuf
12354 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12355 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 12356
8539e4e8 12357 if (info->strip != strip_all || emit_relocs)
c152c796 12358 {
8539e4e8
AM
12359 file_ptr off = elf_next_file_pos (abfd);
12360
12361 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
12362
12363 /* Note that at this point elf_next_file_pos (abfd) is
12364 incorrect. We do not yet know the size of the .symtab section.
12365 We correct next_file_pos below, after we do know the size. */
12366
12367 /* Start writing out the symbol table. The first symbol is always a
12368 dummy symbol. */
c152c796
AM
12369 elfsym.st_value = 0;
12370 elfsym.st_size = 0;
12371 elfsym.st_info = 0;
12372 elfsym.st_other = 0;
12373 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 12374 elfsym.st_target_internal = 0;
ef10c3ac
L
12375 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12376 bfd_und_section_ptr, NULL) != 1)
c152c796 12377 goto error_return;
c152c796 12378
d1bcae83
L
12379 /* Output a symbol for each section if asked or they are used for
12380 relocs. These symbols usually have no names. We store the
12381 index of each one in the index field of the section, so that
12382 we can find it again when outputting relocs. */
c77cb2a0 12383
d1bcae83 12384 if (bfd_keep_unused_section_symbols (abfd) || emit_relocs)
c152c796 12385 {
d1bcae83
L
12386 bfd_boolean name_local_sections
12387 = (bed->elf_backend_name_local_section_symbols
12388 && bed->elf_backend_name_local_section_symbols (abfd));
12389 const char *name = NULL;
12390
12391 elfsym.st_size = 0;
12392 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12393 elfsym.st_other = 0;
12394 elfsym.st_value = 0;
12395 elfsym.st_target_internal = 0;
12396 for (i = 1; i < elf_numsections (abfd); i++)
f0b5bb34 12397 {
d1bcae83
L
12398 o = bfd_section_from_elf_index (abfd, i);
12399 if (o != NULL)
12400 {
12401 o->target_index = bfd_get_symcount (abfd);
12402 elfsym.st_shndx = i;
12403 if (!bfd_link_relocatable (info))
12404 elfsym.st_value = o->vma;
12405 if (name_local_sections)
12406 name = o->name;
12407 if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
12408 NULL) != 1)
12409 goto error_return;
12410 }
f0b5bb34 12411 }
c152c796
AM
12412 }
12413 }
12414
3f1b17bb
MR
12415 /* On some targets like Irix 5 the symbol split between local and global
12416 ones recorded in the sh_info field needs to be done between section
12417 and all other symbols. */
12418 if (bed->elf_backend_elfsym_local_is_section
12419 && bed->elf_backend_elfsym_local_is_section (abfd))
12420 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12421
c152c796
AM
12422 /* Allocate some memory to hold information read in from the input
12423 files. */
12424 if (max_contents_size != 0)
12425 {
8b127cbc
AM
12426 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12427 if (flinfo.contents == NULL)
c152c796
AM
12428 goto error_return;
12429 }
12430
12431 if (max_external_reloc_size != 0)
12432 {
8b127cbc
AM
12433 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12434 if (flinfo.external_relocs == NULL)
c152c796
AM
12435 goto error_return;
12436 }
12437
12438 if (max_internal_reloc_count != 0)
12439 {
056bafd4 12440 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
8b127cbc
AM
12441 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12442 if (flinfo.internal_relocs == NULL)
c152c796
AM
12443 goto error_return;
12444 }
12445
12446 if (max_sym_count != 0)
12447 {
12448 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
12449 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12450 if (flinfo.external_syms == NULL)
c152c796
AM
12451 goto error_return;
12452
12453 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
12454 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12455 if (flinfo.internal_syms == NULL)
c152c796
AM
12456 goto error_return;
12457
12458 amt = max_sym_count * sizeof (long);
8b127cbc
AM
12459 flinfo.indices = (long int *) bfd_malloc (amt);
12460 if (flinfo.indices == NULL)
c152c796
AM
12461 goto error_return;
12462
12463 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
12464 flinfo.sections = (asection **) bfd_malloc (amt);
12465 if (flinfo.sections == NULL)
c152c796
AM
12466 goto error_return;
12467 }
12468
12469 if (max_sym_shndx_count != 0)
12470 {
12471 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
12472 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12473 if (flinfo.locsym_shndx == NULL)
c152c796
AM
12474 goto error_return;
12475 }
12476
64f52338 12477 if (htab->tls_sec)
c152c796 12478 {
66631823 12479 bfd_vma base, end = 0; /* Both bytes. */
c152c796
AM
12480 asection *sec;
12481
64f52338 12482 for (sec = htab->tls_sec;
c152c796
AM
12483 sec && (sec->flags & SEC_THREAD_LOCAL);
12484 sec = sec->next)
12485 {
3a800eb9 12486 bfd_size_type size = sec->size;
66631823 12487 unsigned int opb = bfd_octets_per_byte (abfd, sec);
c152c796 12488
3a800eb9
AM
12489 if (size == 0
12490 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 12491 {
91d6fa6a
NC
12492 struct bfd_link_order *ord = sec->map_tail.link_order;
12493
12494 if (ord != NULL)
66631823 12495 size = ord->offset * opb + ord->size;
c152c796 12496 }
66631823 12497 end = sec->vma + size / opb;
c152c796 12498 }
64f52338 12499 base = htab->tls_sec->vma;
7dc98aea
RO
12500 /* Only align end of TLS section if static TLS doesn't have special
12501 alignment requirements. */
12502 if (bed->static_tls_alignment == 1)
64f52338
AM
12503 end = align_power (end, htab->tls_sec->alignment_power);
12504 htab->tls_size = end - base;
c152c796
AM
12505 }
12506
2f0c68f2
CM
12507 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12508 return FALSE;
12509
c152c796
AM
12510 /* Since ELF permits relocations to be against local symbols, we
12511 must have the local symbols available when we do the relocations.
12512 Since we would rather only read the local symbols once, and we
12513 would rather not keep them in memory, we handle all the
12514 relocations for a single input file at the same time.
12515
12516 Unfortunately, there is no way to know the total number of local
12517 symbols until we have seen all of them, and the local symbol
12518 indices precede the global symbol indices. This means that when
12519 we are generating relocatable output, and we see a reloc against
12520 a global symbol, we can not know the symbol index until we have
12521 finished examining all the local symbols to see which ones we are
12522 going to output. To deal with this, we keep the relocations in
12523 memory, and don't output them until the end of the link. This is
12524 an unfortunate waste of memory, but I don't see a good way around
12525 it. Fortunately, it only happens when performing a relocatable
12526 link, which is not the common case. FIXME: If keep_memory is set
12527 we could write the relocs out and then read them again; I don't
12528 know how bad the memory loss will be. */
12529
c72f2fb2 12530 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12531 sub->output_has_begun = FALSE;
12532 for (o = abfd->sections; o != NULL; o = o->next)
12533 {
8423293d 12534 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
12535 {
12536 if (p->type == bfd_indirect_link_order
12537 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12538 == bfd_target_elf_flavour)
12539 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12540 {
12541 if (! sub->output_has_begun)
12542 {
8b127cbc 12543 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
12544 goto error_return;
12545 sub->output_has_begun = TRUE;
12546 }
12547 }
12548 else if (p->type == bfd_section_reloc_link_order
12549 || p->type == bfd_symbol_reloc_link_order)
12550 {
12551 if (! elf_reloc_link_order (abfd, info, o, p))
12552 goto error_return;
12553 }
12554 else
12555 {
12556 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
12557 {
12558 if (p->type == bfd_indirect_link_order
12559 && (bfd_get_flavour (sub)
12560 == bfd_target_elf_flavour)
12561 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12562 != bed->s->elfclass))
12563 {
12564 const char *iclass, *oclass;
12565
aebf9be7 12566 switch (bed->s->elfclass)
351f65ca 12567 {
aebf9be7
NC
12568 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12569 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12570 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12571 default: abort ();
351f65ca 12572 }
aebf9be7
NC
12573
12574 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 12575 {
aebf9be7
NC
12576 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12577 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12578 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12579 default: abort ();
351f65ca
L
12580 }
12581
12582 bfd_set_error (bfd_error_wrong_format);
4eca0228 12583 _bfd_error_handler
695344c0 12584 /* xgettext:c-format */
871b3ab2 12585 (_("%pB: file class %s incompatible with %s"),
351f65ca
L
12586 sub, iclass, oclass);
12587 }
12588
12589 goto error_return;
12590 }
c152c796
AM
12591 }
12592 }
12593 }
12594
c0f00686
L
12595 /* Free symbol buffer if needed. */
12596 if (!info->reduce_memory_overheads)
12597 {
c72f2fb2 12598 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c9594989 12599 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
c0f00686
L
12600 {
12601 free (elf_tdata (sub)->symbuf);
12602 elf_tdata (sub)->symbuf = NULL;
12603 }
12604 }
12605
496afd17
L
12606 ret = TRUE;
12607
c152c796
AM
12608 /* Output any global symbols that got converted to local in a
12609 version script or due to symbol visibility. We do this in a
12610 separate step since ELF requires all local symbols to appear
12611 prior to any global symbols. FIXME: We should only do this if
12612 some global symbols were, in fact, converted to become local.
12613 FIXME: Will this work correctly with the Irix 5 linker? */
12614 eoinfo.failed = FALSE;
8b127cbc 12615 eoinfo.flinfo = &flinfo;
c152c796 12616 eoinfo.localsyms = TRUE;
34a79995 12617 eoinfo.file_sym_done = FALSE;
7686d77d 12618 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796 12619 if (eoinfo.failed)
496afd17
L
12620 {
12621 ret = FALSE;
12622 goto return_local_hash_table;
12623 }
c152c796 12624
4e617b1e
PB
12625 /* If backend needs to output some local symbols not present in the hash
12626 table, do it now. */
8539e4e8
AM
12627 if (bed->elf_backend_output_arch_local_syms
12628 && (info->strip != strip_all || emit_relocs))
4e617b1e 12629 {
6e0b88f1 12630 typedef int (*out_sym_func)
4e617b1e
PB
12631 (void *, const char *, Elf_Internal_Sym *, asection *,
12632 struct elf_link_hash_entry *);
12633
12634 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
12635 (abfd, info, &flinfo,
12636 (out_sym_func) elf_link_output_symstrtab)))
496afd17
L
12637 {
12638 ret = FALSE;
12639 goto return_local_hash_table;
12640 }
4e617b1e
PB
12641 }
12642
c152c796
AM
12643 /* That wrote out all the local symbols. Finish up the symbol table
12644 with the global symbols. Even if we want to strip everything we
12645 can, we still need to deal with those global symbols that got
12646 converted to local in a version script. */
12647
12648 /* The sh_info field records the index of the first non local symbol. */
3f1b17bb
MR
12649 if (!symtab_hdr->sh_info)
12650 symtab_hdr->sh_info = bfd_get_symcount (abfd);
c152c796
AM
12651
12652 if (dynamic
64f52338
AM
12653 && htab->dynsym != NULL
12654 && htab->dynsym->output_section != bfd_abs_section_ptr)
c152c796
AM
12655 {
12656 Elf_Internal_Sym sym;
64f52338 12657 bfd_byte *dynsym = htab->dynsym->contents;
90ac2420 12658
64f52338
AM
12659 o = htab->dynsym->output_section;
12660 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
c152c796
AM
12661
12662 /* Write out the section symbols for the output sections. */
0e1862bb 12663 if (bfd_link_pic (info)
64f52338 12664 || htab->is_relocatable_executable)
c152c796
AM
12665 {
12666 asection *s;
12667
12668 sym.st_size = 0;
12669 sym.st_name = 0;
12670 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12671 sym.st_other = 0;
35fc36a8 12672 sym.st_target_internal = 0;
c152c796
AM
12673
12674 for (s = abfd->sections; s != NULL; s = s->next)
12675 {
12676 int indx;
12677 bfd_byte *dest;
12678 long dynindx;
12679
c152c796 12680 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
12681 if (dynindx <= 0)
12682 continue;
12683 indx = elf_section_data (s)->this_idx;
c152c796
AM
12684 BFD_ASSERT (indx > 0);
12685 sym.st_shndx = indx;
c0d5a53d 12686 if (! check_dynsym (abfd, &sym))
496afd17
L
12687 {
12688 ret = FALSE;
12689 goto return_local_hash_table;
12690 }
c152c796
AM
12691 sym.st_value = s->vma;
12692 dest = dynsym + dynindx * bed->s->sizeof_sym;
3d16b64e
NA
12693
12694 /* Inform the linker of the addition of this symbol. */
12695
12696 if (info->callbacks->ctf_new_dynsym)
12697 info->callbacks->ctf_new_dynsym (dynindx, &sym);
12698
c152c796
AM
12699 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12700 }
c152c796
AM
12701 }
12702
12703 /* Write out the local dynsyms. */
64f52338 12704 if (htab->dynlocal)
c152c796
AM
12705 {
12706 struct elf_link_local_dynamic_entry *e;
64f52338 12707 for (e = htab->dynlocal; e ; e = e->next)
c152c796
AM
12708 {
12709 asection *s;
12710 bfd_byte *dest;
12711
935bd1e0 12712 /* Copy the internal symbol and turn off visibility.
c152c796
AM
12713 Note that we saved a word of storage and overwrote
12714 the original st_name with the dynstr_index. */
12715 sym = e->isym;
935bd1e0 12716 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
4d68fd75 12717 sym.st_shndx = SHN_UNDEF;
c152c796 12718
cb33740c
AM
12719 s = bfd_section_from_elf_index (e->input_bfd,
12720 e->isym.st_shndx);
4d68fd75
AM
12721 if (s != NULL
12722 && s->output_section != NULL
12723 && elf_section_data (s->output_section) != NULL)
c152c796 12724 {
c152c796
AM
12725 sym.st_shndx =
12726 elf_section_data (s->output_section)->this_idx;
c0d5a53d 12727 if (! check_dynsym (abfd, &sym))
496afd17
L
12728 {
12729 ret = FALSE;
12730 goto return_local_hash_table;
12731 }
c152c796
AM
12732 sym.st_value = (s->output_section->vma
12733 + s->output_offset
12734 + e->isym.st_value);
12735 }
12736
3d16b64e
NA
12737 /* Inform the linker of the addition of this symbol. */
12738
12739 if (info->callbacks->ctf_new_dynsym)
12740 info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
12741
c152c796
AM
12742 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12743 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12744 }
12745 }
c152c796
AM
12746 }
12747
12748 /* We get the global symbols from the hash table. */
12749 eoinfo.failed = FALSE;
12750 eoinfo.localsyms = FALSE;
8b127cbc 12751 eoinfo.flinfo = &flinfo;
7686d77d 12752 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796 12753 if (eoinfo.failed)
496afd17
L
12754 {
12755 ret = FALSE;
12756 goto return_local_hash_table;
12757 }
c152c796
AM
12758
12759 /* If backend needs to output some symbols not present in the hash
12760 table, do it now. */
8539e4e8
AM
12761 if (bed->elf_backend_output_arch_syms
12762 && (info->strip != strip_all || emit_relocs))
c152c796 12763 {
6e0b88f1 12764 typedef int (*out_sym_func)
c152c796
AM
12765 (void *, const char *, Elf_Internal_Sym *, asection *,
12766 struct elf_link_hash_entry *);
12767
12768 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
12769 (abfd, info, &flinfo,
12770 (out_sym_func) elf_link_output_symstrtab)))
496afd17
L
12771 {
12772 ret = FALSE;
12773 goto return_local_hash_table;
12774 }
c152c796
AM
12775 }
12776
ef10c3ac
L
12777 /* Finalize the .strtab section. */
12778 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12779
12780 /* Swap out the .strtab section. */
12781 if (!elf_link_swap_symbols_out (&flinfo))
496afd17
L
12782 {
12783 ret = FALSE;
12784 goto return_local_hash_table;
12785 }
c152c796
AM
12786
12787 /* Now we know the size of the symtab section. */
c152c796
AM
12788 if (bfd_get_symcount (abfd) > 0)
12789 {
ee3b52e9
L
12790 /* Finish up and write out the symbol string table (.strtab)
12791 section. */
ad32986f 12792 Elf_Internal_Shdr *symstrtab_hdr = NULL;
8539e4e8
AM
12793 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12794
ad32986f 12795 if (elf_symtab_shndx_list (abfd))
8539e4e8 12796 {
ad32986f 12797 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8539e4e8 12798
ad32986f
NC
12799 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12800 {
12801 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12802 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12803 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12804 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12805 symtab_shndx_hdr->sh_size = amt;
8539e4e8 12806
ad32986f
NC
12807 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12808 off, TRUE);
12809
12810 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12811 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
496afd17
L
12812 {
12813 ret = FALSE;
12814 goto return_local_hash_table;
12815 }
ad32986f 12816 }
8539e4e8 12817 }
ee3b52e9
L
12818
12819 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12820 /* sh_name was set in prep_headers. */
12821 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 12822 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 12823 symstrtab_hdr->sh_addr = 0;
ef10c3ac 12824 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
12825 symstrtab_hdr->sh_entsize = 0;
12826 symstrtab_hdr->sh_link = 0;
12827 symstrtab_hdr->sh_info = 0;
12828 /* sh_offset is set just below. */
12829 symstrtab_hdr->sh_addralign = 1;
12830
12831 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12832 off, TRUE);
12833 elf_next_file_pos (abfd) = off;
12834
c152c796 12835 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 12836 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
496afd17
L
12837 {
12838 ret = FALSE;
12839 goto return_local_hash_table;
12840 }
c152c796
AM
12841 }
12842
76359541
TP
12843 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12844 {
871b3ab2 12845 _bfd_error_handler (_("%pB: failed to generate import library"),
4eca0228 12846 info->out_implib_bfd);
496afd17
L
12847 ret = FALSE;
12848 goto return_local_hash_table;
76359541
TP
12849 }
12850
c152c796
AM
12851 /* Adjust the relocs to have the correct symbol indices. */
12852 for (o = abfd->sections; o != NULL; o = o->next)
12853 {
d4730f92 12854 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 12855 bfd_boolean sort;
10bbbc1d 12856
c152c796
AM
12857 if ((o->flags & SEC_RELOC) == 0)
12858 continue;
12859
28dbcedc 12860 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3 12861 if (esdo->rel.hdr != NULL
10bbbc1d 12862 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
496afd17
L
12863 {
12864 ret = FALSE;
12865 goto return_local_hash_table;
12866 }
bca6d0e3 12867 if (esdo->rela.hdr != NULL
10bbbc1d 12868 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
496afd17
L
12869 {
12870 ret = FALSE;
12871 goto return_local_hash_table;
12872 }
c152c796
AM
12873
12874 /* Set the reloc_count field to 0 to prevent write_relocs from
12875 trying to swap the relocs out itself. */
12876 o->reloc_count = 0;
12877 }
12878
12879 if (dynamic && info->combreloc && dynobj != NULL)
12880 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12881
12882 /* If we are linking against a dynamic object, or generating a
12883 shared library, finish up the dynamic linking information. */
12884 if (dynamic)
12885 {
12886 bfd_byte *dyncon, *dynconend;
12887
12888 /* Fix up .dynamic entries. */
3d4d4302 12889 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
12890 BFD_ASSERT (o != NULL);
12891
12892 dyncon = o->contents;
eea6121a 12893 dynconend = o->contents + o->size;
c152c796
AM
12894 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12895 {
12896 Elf_Internal_Dyn dyn;
12897 const char *name;
12898 unsigned int type;
64487780
AM
12899 bfd_size_type sh_size;
12900 bfd_vma sh_addr;
c152c796
AM
12901
12902 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12903
12904 switch (dyn.d_tag)
12905 {
12906 default:
12907 continue;
12908 case DT_NULL:
12909 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12910 {
12911 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12912 {
12913 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12914 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12915 default: continue;
12916 }
12917 dyn.d_un.d_val = relativecount;
12918 relativecount = 0;
12919 break;
12920 }
12921 continue;
12922
12923 case DT_INIT:
12924 name = info->init_function;
12925 goto get_sym;
12926 case DT_FINI:
12927 name = info->fini_function;
12928 get_sym:
12929 {
12930 struct elf_link_hash_entry *h;
12931
64f52338 12932 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
c152c796
AM
12933 if (h != NULL
12934 && (h->root.type == bfd_link_hash_defined
12935 || h->root.type == bfd_link_hash_defweak))
12936 {
bef26483 12937 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
12938 o = h->root.u.def.section;
12939 if (o->output_section != NULL)
bef26483 12940 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
12941 + o->output_offset);
12942 else
12943 {
12944 /* The symbol is imported from another shared
12945 library and does not apply to this one. */
bef26483 12946 dyn.d_un.d_ptr = 0;
c152c796
AM
12947 }
12948 break;
12949 }
12950 }
12951 continue;
12952
12953 case DT_PREINIT_ARRAYSZ:
12954 name = ".preinit_array";
4ade44b7 12955 goto get_out_size;
c152c796
AM
12956 case DT_INIT_ARRAYSZ:
12957 name = ".init_array";
4ade44b7 12958 goto get_out_size;
c152c796
AM
12959 case DT_FINI_ARRAYSZ:
12960 name = ".fini_array";
4ade44b7 12961 get_out_size:
c152c796
AM
12962 o = bfd_get_section_by_name (abfd, name);
12963 if (o == NULL)
12964 {
4eca0228 12965 _bfd_error_handler
4ade44b7 12966 (_("could not find section %s"), name);
c152c796
AM
12967 goto error_return;
12968 }
eea6121a 12969 if (o->size == 0)
4eca0228 12970 _bfd_error_handler
c152c796 12971 (_("warning: %s section has zero size"), name);
eea6121a 12972 dyn.d_un.d_val = o->size;
c152c796
AM
12973 break;
12974
12975 case DT_PREINIT_ARRAY:
12976 name = ".preinit_array";
4ade44b7 12977 goto get_out_vma;
c152c796
AM
12978 case DT_INIT_ARRAY:
12979 name = ".init_array";
4ade44b7 12980 goto get_out_vma;
c152c796
AM
12981 case DT_FINI_ARRAY:
12982 name = ".fini_array";
4ade44b7
AM
12983 get_out_vma:
12984 o = bfd_get_section_by_name (abfd, name);
12985 goto do_vma;
c152c796
AM
12986
12987 case DT_HASH:
12988 name = ".hash";
12989 goto get_vma;
fdc90cb4
JJ
12990 case DT_GNU_HASH:
12991 name = ".gnu.hash";
12992 goto get_vma;
c152c796
AM
12993 case DT_STRTAB:
12994 name = ".dynstr";
12995 goto get_vma;
12996 case DT_SYMTAB:
12997 name = ".dynsym";
12998 goto get_vma;
12999 case DT_VERDEF:
13000 name = ".gnu.version_d";
13001 goto get_vma;
13002 case DT_VERNEED:
13003 name = ".gnu.version_r";
13004 goto get_vma;
13005 case DT_VERSYM:
13006 name = ".gnu.version";
13007 get_vma:
4ade44b7
AM
13008 o = bfd_get_linker_section (dynobj, name);
13009 do_vma:
b3293efa 13010 if (o == NULL || bfd_is_abs_section (o->output_section))
c152c796 13011 {
4eca0228 13012 _bfd_error_handler
4ade44b7 13013 (_("could not find section %s"), name);
c152c796
AM
13014 goto error_return;
13015 }
894891db
NC
13016 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13017 {
4eca0228 13018 _bfd_error_handler
894891db
NC
13019 (_("warning: section '%s' is being made into a note"), name);
13020 bfd_set_error (bfd_error_nonrepresentable_section);
13021 goto error_return;
13022 }
4ade44b7 13023 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
c152c796
AM
13024 break;
13025
13026 case DT_REL:
13027 case DT_RELA:
13028 case DT_RELSZ:
13029 case DT_RELASZ:
13030 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13031 type = SHT_REL;
13032 else
13033 type = SHT_RELA;
64487780
AM
13034 sh_size = 0;
13035 sh_addr = 0;
c152c796
AM
13036 for (i = 1; i < elf_numsections (abfd); i++)
13037 {
13038 Elf_Internal_Shdr *hdr;
13039
13040 hdr = elf_elfsections (abfd)[i];
13041 if (hdr->sh_type == type
13042 && (hdr->sh_flags & SHF_ALLOC) != 0)
13043 {
64487780
AM
13044 sh_size += hdr->sh_size;
13045 if (sh_addr == 0
13046 || sh_addr > hdr->sh_addr)
13047 sh_addr = hdr->sh_addr;
c152c796
AM
13048 }
13049 }
64487780 13050
64f52338
AM
13051 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
13052 {
66631823
CE
13053 unsigned int opb = bfd_octets_per_byte (abfd, o);
13054
64f52338
AM
13055 /* Don't count procedure linkage table relocs in the
13056 overall reloc count. */
64487780
AM
13057 sh_size -= htab->srelplt->size;
13058 if (sh_size == 0)
13059 /* If the size is zero, make the address zero too.
13060 This is to avoid a glibc bug. If the backend
13061 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13062 zero, then we'll put DT_RELA at the end of
13063 DT_JMPREL. glibc will interpret the end of
13064 DT_RELA matching the end of DT_JMPREL as the
13065 case where DT_RELA includes DT_JMPREL, and for
13066 LD_BIND_NOW will decide that processing DT_RELA
13067 will process the PLT relocs too. Net result:
13068 No PLT relocs applied. */
13069 sh_addr = 0;
13070
64f52338
AM
13071 /* If .rela.plt is the first .rela section, exclude
13072 it from DT_RELA. */
64487780 13073 else if (sh_addr == (htab->srelplt->output_section->vma
66631823 13074 + htab->srelplt->output_offset) * opb)
64487780 13075 sh_addr += htab->srelplt->size;
64f52338 13076 }
64487780
AM
13077
13078 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13079 dyn.d_un.d_val = sh_size;
13080 else
13081 dyn.d_un.d_ptr = sh_addr;
c152c796
AM
13082 break;
13083 }
13084 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13085 }
13086 }
13087
13088 /* If we have created any dynamic sections, then output them. */
13089 if (dynobj != NULL)
13090 {
13091 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
13092 goto error_return;
13093
943284cc 13094 /* Check for DT_TEXTREL (late, in case the backend removes it). */
a6dbf402 13095 if (bfd_link_textrel_check (info)
3d4d4302 13096 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
13097 {
13098 bfd_byte *dyncon, *dynconend;
13099
943284cc
DJ
13100 dyncon = o->contents;
13101 dynconend = o->contents + o->size;
13102 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13103 {
13104 Elf_Internal_Dyn dyn;
13105
13106 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13107
13108 if (dyn.d_tag == DT_TEXTREL)
13109 {
a6dbf402 13110 if (info->textrel_check == textrel_check_error)
c192a133 13111 info->callbacks->einfo
9793eb77 13112 (_("%P%X: read-only segment has dynamic relocations\n"));
a6dbf402
L
13113 else if (bfd_link_dll (info))
13114 info->callbacks->einfo
13115 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
c192a133
AM
13116 else
13117 info->callbacks->einfo
a6dbf402 13118 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
943284cc
DJ
13119 break;
13120 }
13121 }
13122 }
13123
c152c796
AM
13124 for (o = dynobj->sections; o != NULL; o = o->next)
13125 {
13126 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 13127 || o->size == 0
c152c796
AM
13128 || o->output_section == bfd_abs_section_ptr)
13129 continue;
13130 if ((o->flags & SEC_LINKER_CREATED) == 0)
13131 {
13132 /* At this point, we are only interested in sections
13133 created by _bfd_elf_link_create_dynamic_sections. */
13134 continue;
13135 }
64f52338 13136 if (htab->stab_info.stabstr == o)
3722b82f 13137 continue;
64f52338 13138 if (htab->eh_info.hdr_sec == o)
eea6121a 13139 continue;
3d4d4302 13140 if (strcmp (o->name, ".dynstr") != 0)
c152c796 13141 {
bb294208
AM
13142 bfd_size_type octets = ((file_ptr) o->output_offset
13143 * bfd_octets_per_byte (abfd, o));
13144 if (!bfd_set_section_contents (abfd, o->output_section,
13145 o->contents, octets, o->size))
c152c796
AM
13146 goto error_return;
13147 }
13148 else
13149 {
13150 /* The contents of the .dynstr section are actually in a
13151 stringtab. */
8539e4e8
AM
13152 file_ptr off;
13153
c152c796
AM
13154 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13155 if (bfd_seek (abfd, off, SEEK_SET) != 0
64f52338 13156 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
c152c796
AM
13157 goto error_return;
13158 }
13159 }
13160 }
13161
7bdf4127 13162 if (!info->resolve_section_groups)
c152c796
AM
13163 {
13164 bfd_boolean failed = FALSE;
13165
7bdf4127 13166 BFD_ASSERT (bfd_link_relocatable (info));
c152c796
AM
13167 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13168 if (failed)
13169 goto error_return;
13170 }
13171
13172 /* If we have optimized stabs strings, output them. */
64f52338 13173 if (htab->stab_info.stabstr != NULL)
c152c796 13174 {
64f52338 13175 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
c152c796
AM
13176 goto error_return;
13177 }
13178
9f7c3e5e
AM
13179 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13180 goto error_return;
c152c796 13181
1ff6de03
NA
13182 if (info->callbacks->emit_ctf)
13183 info->callbacks->emit_ctf ();
13184
9f7c3e5e 13185 elf_final_link_free (abfd, &flinfo);
c152c796 13186
104d59d1
JM
13187 if (attr_section)
13188 {
a50b1753 13189 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 13190 if (contents == NULL)
496afd17
L
13191 {
13192 /* Bail out and fail. */
13193 ret = FALSE;
13194 goto return_local_hash_table;
13195 }
104d59d1
JM
13196 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
13197 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
13198 free (contents);
13199 }
13200
496afd17
L
13201 return_local_hash_table:
13202 if (info->unique_symbol)
13203 bfd_hash_table_free (&flinfo.local_hash_table);
13204 return ret;
c152c796
AM
13205
13206 error_return:
9f7c3e5e 13207 elf_final_link_free (abfd, &flinfo);
496afd17
L
13208 ret = FALSE;
13209 goto return_local_hash_table;
c152c796
AM
13210}
13211\f
5241d853
RS
13212/* Initialize COOKIE for input bfd ABFD. */
13213
13214static bfd_boolean
13215init_reloc_cookie (struct elf_reloc_cookie *cookie,
13216 struct bfd_link_info *info, bfd *abfd)
13217{
13218 Elf_Internal_Shdr *symtab_hdr;
13219 const struct elf_backend_data *bed;
13220
13221 bed = get_elf_backend_data (abfd);
13222 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13223
13224 cookie->abfd = abfd;
13225 cookie->sym_hashes = elf_sym_hashes (abfd);
13226 cookie->bad_symtab = elf_bad_symtab (abfd);
13227 if (cookie->bad_symtab)
13228 {
13229 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13230 cookie->extsymoff = 0;
13231 }
13232 else
13233 {
13234 cookie->locsymcount = symtab_hdr->sh_info;
13235 cookie->extsymoff = symtab_hdr->sh_info;
13236 }
13237
13238 if (bed->s->arch_size == 32)
13239 cookie->r_sym_shift = 8;
13240 else
13241 cookie->r_sym_shift = 32;
13242
13243 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13244 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13245 {
13246 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13247 cookie->locsymcount, 0,
13248 NULL, NULL, NULL);
13249 if (cookie->locsyms == NULL)
13250 {
13251 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
13252 return FALSE;
13253 }
13254 if (info->keep_memory)
13255 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13256 }
13257 return TRUE;
13258}
13259
13260/* Free the memory allocated by init_reloc_cookie, if appropriate. */
13261
13262static void
13263fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13264{
13265 Elf_Internal_Shdr *symtab_hdr;
13266
13267 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
c9594989 13268 if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
5241d853
RS
13269 free (cookie->locsyms);
13270}
13271
13272/* Initialize the relocation information in COOKIE for input section SEC
13273 of input bfd ABFD. */
13274
13275static bfd_boolean
13276init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13277 struct bfd_link_info *info, bfd *abfd,
13278 asection *sec)
13279{
5241d853
RS
13280 if (sec->reloc_count == 0)
13281 {
13282 cookie->rels = NULL;
13283 cookie->relend = NULL;
13284 }
13285 else
13286 {
5241d853
RS
13287 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
13288 info->keep_memory);
13289 if (cookie->rels == NULL)
13290 return FALSE;
13291 cookie->rel = cookie->rels;
056bafd4 13292 cookie->relend = cookie->rels + sec->reloc_count;
5241d853
RS
13293 }
13294 cookie->rel = cookie->rels;
13295 return TRUE;
13296}
13297
13298/* Free the memory allocated by init_reloc_cookie_rels,
13299 if appropriate. */
13300
13301static void
13302fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13303 asection *sec)
13304{
c9594989 13305 if (elf_section_data (sec)->relocs != cookie->rels)
5241d853
RS
13306 free (cookie->rels);
13307}
13308
13309/* Initialize the whole of COOKIE for input section SEC. */
13310
13311static bfd_boolean
13312init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13313 struct bfd_link_info *info,
13314 asection *sec)
13315{
13316 if (!init_reloc_cookie (cookie, info, sec->owner))
13317 goto error1;
13318 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13319 goto error2;
13320 return TRUE;
13321
13322 error2:
13323 fini_reloc_cookie (cookie, sec->owner);
13324 error1:
13325 return FALSE;
13326}
13327
13328/* Free the memory allocated by init_reloc_cookie_for_section,
13329 if appropriate. */
13330
13331static void
13332fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13333 asection *sec)
13334{
13335 fini_reloc_cookie_rels (cookie, sec);
13336 fini_reloc_cookie (cookie, sec->owner);
13337}
13338\f
c152c796
AM
13339/* Garbage collect unused sections. */
13340
07adf181
AM
13341/* Default gc_mark_hook. */
13342
13343asection *
13344_bfd_elf_gc_mark_hook (asection *sec,
13345 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13346 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13347 struct elf_link_hash_entry *h,
13348 Elf_Internal_Sym *sym)
13349{
13350 if (h != NULL)
13351 {
13352 switch (h->root.type)
13353 {
13354 case bfd_link_hash_defined:
13355 case bfd_link_hash_defweak:
13356 return h->root.u.def.section;
13357
13358 case bfd_link_hash_common:
13359 return h->root.u.c.p->section;
13360
13361 default:
13362 break;
13363 }
13364 }
13365 else
13366 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13367
13368 return NULL;
13369}
13370
9e223787 13371/* Return the debug definition section. */
b7c871ed
L
13372
13373static asection *
13374elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13375 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13376 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13377 struct elf_link_hash_entry *h,
9e223787 13378 Elf_Internal_Sym *sym)
b7c871ed 13379{
9e223787
L
13380 if (h != NULL)
13381 {
13382 /* Return the global debug definition section. */
13383 if ((h->root.type == bfd_link_hash_defined
13384 || h->root.type == bfd_link_hash_defweak)
13385 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13386 return h->root.u.def.section;
13387 }
13388 else
13389 {
13390 /* Return the local debug definition section. */
13391 asection *isec = bfd_section_from_elf_index (sec->owner,
13392 sym->st_shndx);
13393 if ((isec->flags & SEC_DEBUGGING) != 0)
13394 return isec;
13395 }
b7c871ed
L
13396
13397 return NULL;
13398}
13399
5241d853
RS
13400/* COOKIE->rel describes a relocation against section SEC, which is
13401 a section we've decided to keep. Return the section that contains
13402 the relocation symbol, or NULL if no section contains it. */
13403
13404asection *
13405_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13406 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
13407 struct elf_reloc_cookie *cookie,
13408 bfd_boolean *start_stop)
5241d853
RS
13409{
13410 unsigned long r_symndx;
3024a17a 13411 struct elf_link_hash_entry *h, *hw;
5241d853
RS
13412
13413 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 13414 if (r_symndx == STN_UNDEF)
5241d853
RS
13415 return NULL;
13416
13417 if (r_symndx >= cookie->locsymcount
13418 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13419 {
13420 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
13421 if (h == NULL)
13422 {
871b3ab2 13423 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
263ddf68
L
13424 sec->owner);
13425 return NULL;
13426 }
5241d853
RS
13427 while (h->root.type == bfd_link_hash_indirect
13428 || h->root.type == bfd_link_hash_warning)
13429 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 13430 h->mark = 1;
3024a17a
AM
13431 /* Keep all aliases of the symbol too. If an object symbol
13432 needs to be copied into .dynbss then all of its aliases
13433 should be present as dynamic symbols, not just the one used
13434 on the copy relocation. */
13435 hw = h;
13436 while (hw->is_weakalias)
13437 {
13438 hw = hw->u.alias;
13439 hw->mark = 1;
13440 }
1cce69b9 13441
a6a4679f 13442 if (start_stop != NULL)
1cce69b9 13443 {
7dba9362
AM
13444 /* To work around a glibc bug, mark XXX input sections
13445 when there is a reference to __start_XXX or __stop_XXX
13446 symbols. */
cbd0eecf 13447 if (h->start_stop)
1cce69b9 13448 {
cbd0eecf 13449 asection *s = h->u2.start_stop_section;
a6a4679f
AM
13450 *start_stop = !s->gc_mark;
13451 return s;
1cce69b9
AM
13452 }
13453 }
13454
5241d853
RS
13455 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13456 }
13457
13458 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13459 &cookie->locsyms[r_symndx]);
13460}
13461
13462/* COOKIE->rel describes a relocation against section SEC, which is
13463 a section we've decided to keep. Mark the section that contains
9d0a14d3 13464 the relocation symbol. */
5241d853
RS
13465
13466bfd_boolean
13467_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13468 asection *sec,
13469 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 13470 struct elf_reloc_cookie *cookie)
5241d853
RS
13471{
13472 asection *rsec;
1cce69b9 13473 bfd_boolean start_stop = FALSE;
5241d853 13474
1cce69b9
AM
13475 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13476 while (rsec != NULL)
5241d853 13477 {
1cce69b9
AM
13478 if (!rsec->gc_mark)
13479 {
13480 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13481 || (rsec->owner->flags & DYNAMIC) != 0)
13482 rsec->gc_mark = 1;
13483 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13484 return FALSE;
13485 }
13486 if (!start_stop)
13487 break;
199af150 13488 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
13489 }
13490 return TRUE;
13491}
13492
07adf181
AM
13493/* The mark phase of garbage collection. For a given section, mark
13494 it and any sections in this section's group, and all the sections
13495 which define symbols to which it refers. */
13496
ccfa59ea
AM
13497bfd_boolean
13498_bfd_elf_gc_mark (struct bfd_link_info *info,
13499 asection *sec,
6a5bb875 13500 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
13501{
13502 bfd_boolean ret;
9d0a14d3 13503 asection *group_sec, *eh_frame;
c152c796
AM
13504
13505 sec->gc_mark = 1;
13506
13507 /* Mark all the sections in the group. */
13508 group_sec = elf_section_data (sec)->next_in_group;
13509 if (group_sec && !group_sec->gc_mark)
ccfa59ea 13510 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
13511 return FALSE;
13512
13513 /* Look through the section relocs. */
13514 ret = TRUE;
9d0a14d3
RS
13515 eh_frame = elf_eh_frame_section (sec->owner);
13516 if ((sec->flags & SEC_RELOC) != 0
13517 && sec->reloc_count > 0
13518 && sec != eh_frame)
c152c796 13519 {
5241d853 13520 struct elf_reloc_cookie cookie;
c152c796 13521
5241d853
RS
13522 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13523 ret = FALSE;
c152c796 13524 else
c152c796 13525 {
5241d853 13526 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 13527 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
13528 {
13529 ret = FALSE;
13530 break;
13531 }
13532 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
13533 }
13534 }
9d0a14d3
RS
13535
13536 if (ret && eh_frame && elf_fde_list (sec))
13537 {
13538 struct elf_reloc_cookie cookie;
13539
13540 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13541 ret = FALSE;
13542 else
13543 {
13544 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13545 gc_mark_hook, &cookie))
13546 ret = FALSE;
13547 fini_reloc_cookie_for_section (&cookie, eh_frame);
13548 }
13549 }
13550
2f0c68f2
CM
13551 eh_frame = elf_section_eh_frame_entry (sec);
13552 if (ret && eh_frame && !eh_frame->gc_mark)
13553 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13554 ret = FALSE;
13555
c152c796
AM
13556 return ret;
13557}
13558
3c758495
TG
13559/* Scan and mark sections in a special or debug section group. */
13560
13561static void
13562_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13563{
13564 /* Point to first section of section group. */
13565 asection *ssec;
13566 /* Used to iterate the section group. */
13567 asection *msec;
13568
13569 bfd_boolean is_special_grp = TRUE;
13570 bfd_boolean is_debug_grp = TRUE;
13571
13572 /* First scan to see if group contains any section other than debug
13573 and special section. */
13574 ssec = msec = elf_next_in_group (grp);
13575 do
13576 {
13577 if ((msec->flags & SEC_DEBUGGING) == 0)
13578 is_debug_grp = FALSE;
13579
13580 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13581 is_special_grp = FALSE;
13582
13583 msec = elf_next_in_group (msec);
13584 }
13585 while (msec != ssec);
13586
13587 /* If this is a pure debug section group or pure special section group,
13588 keep all sections in this group. */
13589 if (is_debug_grp || is_special_grp)
13590 {
13591 do
13592 {
13593 msec->gc_mark = 1;
13594 msec = elf_next_in_group (msec);
13595 }
13596 while (msec != ssec);
13597 }
13598}
13599
7f6ab9f8
AM
13600/* Keep debug and special sections. */
13601
13602bfd_boolean
13603_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
b7d07216 13604 elf_gc_mark_hook_fn mark_hook)
7f6ab9f8
AM
13605{
13606 bfd *ibfd;
13607
c72f2fb2 13608 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
13609 {
13610 asection *isec;
13611 bfd_boolean some_kept;
b40bf0a2 13612 bfd_boolean debug_frag_seen;
b7c871ed 13613 bfd_boolean has_kept_debug_info;
7f6ab9f8
AM
13614
13615 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13616 continue;
57963c05
AM
13617 isec = ibfd->sections;
13618 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13619 continue;
7f6ab9f8 13620
b40bf0a2
NC
13621 /* Ensure all linker created sections are kept,
13622 see if any other section is already marked,
13623 and note if we have any fragmented debug sections. */
b7c871ed 13624 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
7f6ab9f8
AM
13625 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13626 {
13627 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13628 isec->gc_mark = 1;
eb026f09
AM
13629 else if (isec->gc_mark
13630 && (isec->flags & SEC_ALLOC) != 0
13631 && elf_section_type (isec) != SHT_NOTE)
7f6ab9f8 13632 some_kept = TRUE;
b7d07216
L
13633 else
13634 {
13635 /* Since all sections, except for backend specific ones,
13636 have been garbage collected, call mark_hook on this
13637 section if any of its linked-to sections is marked. */
def97fb9
AM
13638 asection *linked_to_sec;
13639 for (linked_to_sec = elf_linked_to_section (isec);
13640 linked_to_sec != NULL && !linked_to_sec->linker_mark;
b7d07216 13641 linked_to_sec = elf_linked_to_section (linked_to_sec))
def97fb9
AM
13642 {
13643 if (linked_to_sec->gc_mark)
13644 {
13645 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
13646 return FALSE;
13647 break;
13648 }
13649 linked_to_sec->linker_mark = 1;
13650 }
13651 for (linked_to_sec = elf_linked_to_section (isec);
13652 linked_to_sec != NULL && linked_to_sec->linker_mark;
13653 linked_to_sec = elf_linked_to_section (linked_to_sec))
13654 linked_to_sec->linker_mark = 0;
b7d07216 13655 }
b40bf0a2 13656
535b785f 13657 if (!debug_frag_seen
b40bf0a2
NC
13658 && (isec->flags & SEC_DEBUGGING)
13659 && CONST_STRNEQ (isec->name, ".debug_line."))
13660 debug_frag_seen = TRUE;
5242a0a0
L
13661 else if (strcmp (bfd_section_name (isec),
13662 "__patchable_function_entries") == 0
13663 && elf_linked_to_section (isec) == NULL)
13664 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
13665 "need linked-to section "
13666 "for --gc-sections\n"),
13667 isec->owner, isec);
7f6ab9f8
AM
13668 }
13669
eb026f09
AM
13670 /* If no non-note alloc section in this file will be kept, then
13671 we can toss out the debug and special sections. */
7f6ab9f8
AM
13672 if (!some_kept)
13673 continue;
13674
13675 /* Keep debug and special sections like .comment when they are
3c758495 13676 not part of a group. Also keep section groups that contain
b7d07216
L
13677 just debug sections or special sections. NB: Sections with
13678 linked-to section has been handled above. */
7f6ab9f8 13679 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
13680 {
13681 if ((isec->flags & SEC_GROUP) != 0)
13682 _bfd_elf_gc_mark_debug_special_section_group (isec);
13683 else if (((isec->flags & SEC_DEBUGGING) != 0
13684 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
b7d07216
L
13685 && elf_next_in_group (isec) == NULL
13686 && elf_linked_to_section (isec) == NULL)
3c758495 13687 isec->gc_mark = 1;
b7c871ed
L
13688 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13689 has_kept_debug_info = TRUE;
3c758495 13690 }
b40bf0a2 13691
b40bf0a2
NC
13692 /* Look for CODE sections which are going to be discarded,
13693 and find and discard any fragmented debug sections which
13694 are associated with that code section. */
b7c871ed
L
13695 if (debug_frag_seen)
13696 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13697 if ((isec->flags & SEC_CODE) != 0
13698 && isec->gc_mark == 0)
13699 {
13700 unsigned int ilen;
13701 asection *dsec;
b40bf0a2 13702
b7c871ed 13703 ilen = strlen (isec->name);
b40bf0a2 13704
b7c871ed 13705 /* Association is determined by the name of the debug
07d6d2b8 13706 section containing the name of the code section as
b7c871ed
L
13707 a suffix. For example .debug_line.text.foo is a
13708 debug section associated with .text.foo. */
13709 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13710 {
13711 unsigned int dlen;
b40bf0a2 13712
b7c871ed
L
13713 if (dsec->gc_mark == 0
13714 || (dsec->flags & SEC_DEBUGGING) == 0)
13715 continue;
b40bf0a2 13716
b7c871ed 13717 dlen = strlen (dsec->name);
b40bf0a2 13718
b7c871ed
L
13719 if (dlen > ilen
13720 && strncmp (dsec->name + (dlen - ilen),
13721 isec->name, ilen) == 0)
b40bf0a2 13722 dsec->gc_mark = 0;
b7c871ed 13723 }
b40bf0a2 13724 }
b7c871ed
L
13725
13726 /* Mark debug sections referenced by kept debug sections. */
13727 if (has_kept_debug_info)
13728 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13729 if (isec->gc_mark
13730 && (isec->flags & SEC_DEBUGGING) != 0)
13731 if (!_bfd_elf_gc_mark (info, isec,
13732 elf_gc_mark_debug_section))
13733 return FALSE;
7f6ab9f8
AM
13734 }
13735 return TRUE;
13736}
13737
c152c796 13738static bfd_boolean
ccabcbe5 13739elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
13740{
13741 bfd *sub;
ccabcbe5 13742 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
c152c796 13743
c72f2fb2 13744 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13745 {
13746 asection *o;
13747
b19a8f85 13748 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 13749 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
b19a8f85 13750 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796 13751 continue;
57963c05
AM
13752 o = sub->sections;
13753 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13754 continue;
c152c796
AM
13755
13756 for (o = sub->sections; o != NULL; o = o->next)
13757 {
a33dafc3
L
13758 /* When any section in a section group is kept, we keep all
13759 sections in the section group. If the first member of
13760 the section group is excluded, we will also exclude the
13761 group section. */
13762 if (o->flags & SEC_GROUP)
13763 {
13764 asection *first = elf_next_in_group (o);
13765 o->gc_mark = first->gc_mark;
13766 }
c152c796 13767
1e7eae0d 13768 if (o->gc_mark)
c152c796
AM
13769 continue;
13770
13771 /* Skip sweeping sections already excluded. */
13772 if (o->flags & SEC_EXCLUDE)
13773 continue;
13774
13775 /* Since this is early in the link process, it is simple
13776 to remove a section from the output. */
13777 o->flags |= SEC_EXCLUDE;
13778
c55fe096 13779 if (info->print_gc_sections && o->size != 0)
695344c0 13780 /* xgettext:c-format */
9793eb77 13781 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
c08bb8dd 13782 o, sub);
c152c796
AM
13783 }
13784 }
13785
c152c796
AM
13786 return TRUE;
13787}
13788
13789/* Propagate collected vtable information. This is called through
13790 elf_link_hash_traverse. */
13791
13792static bfd_boolean
13793elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13794{
c152c796 13795 /* Those that are not vtables. */
cbd0eecf
L
13796 if (h->start_stop
13797 || h->u2.vtable == NULL
13798 || h->u2.vtable->parent == NULL)
c152c796
AM
13799 return TRUE;
13800
13801 /* Those vtables that do not have parents, we cannot merge. */
cbd0eecf 13802 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
13803 return TRUE;
13804
13805 /* If we've already been done, exit. */
cbd0eecf 13806 if (h->u2.vtable->used && h->u2.vtable->used[-1])
c152c796
AM
13807 return TRUE;
13808
13809 /* Make sure the parent's table is up to date. */
cbd0eecf 13810 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
c152c796 13811
cbd0eecf 13812 if (h->u2.vtable->used == NULL)
c152c796
AM
13813 {
13814 /* None of this table's entries were referenced. Re-use the
13815 parent's table. */
cbd0eecf
L
13816 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13817 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
c152c796
AM
13818 }
13819 else
13820 {
13821 size_t n;
13822 bfd_boolean *cu, *pu;
13823
13824 /* Or the parent's entries into ours. */
cbd0eecf 13825 cu = h->u2.vtable->used;
c152c796 13826 cu[-1] = TRUE;
cbd0eecf 13827 pu = h->u2.vtable->parent->u2.vtable->used;
c152c796
AM
13828 if (pu != NULL)
13829 {
13830 const struct elf_backend_data *bed;
13831 unsigned int log_file_align;
13832
13833 bed = get_elf_backend_data (h->root.u.def.section->owner);
13834 log_file_align = bed->s->log_file_align;
cbd0eecf 13835 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
c152c796
AM
13836 while (n--)
13837 {
13838 if (*pu)
13839 *cu = TRUE;
13840 pu++;
13841 cu++;
13842 }
13843 }
13844 }
13845
13846 return TRUE;
13847}
13848
13849static bfd_boolean
13850elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13851{
13852 asection *sec;
13853 bfd_vma hstart, hend;
13854 Elf_Internal_Rela *relstart, *relend, *rel;
13855 const struct elf_backend_data *bed;
13856 unsigned int log_file_align;
13857
c152c796
AM
13858 /* Take care of both those symbols that do not describe vtables as
13859 well as those that are not loaded. */
cbd0eecf
L
13860 if (h->start_stop
13861 || h->u2.vtable == NULL
13862 || h->u2.vtable->parent == NULL)
c152c796
AM
13863 return TRUE;
13864
13865 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13866 || h->root.type == bfd_link_hash_defweak);
13867
13868 sec = h->root.u.def.section;
13869 hstart = h->root.u.def.value;
13870 hend = hstart + h->size;
13871
13872 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13873 if (!relstart)
13874 return *(bfd_boolean *) okp = FALSE;
13875 bed = get_elf_backend_data (sec->owner);
13876 log_file_align = bed->s->log_file_align;
13877
056bafd4 13878 relend = relstart + sec->reloc_count;
c152c796
AM
13879
13880 for (rel = relstart; rel < relend; ++rel)
13881 if (rel->r_offset >= hstart && rel->r_offset < hend)
13882 {
13883 /* If the entry is in use, do nothing. */
cbd0eecf
L
13884 if (h->u2.vtable->used
13885 && (rel->r_offset - hstart) < h->u2.vtable->size)
c152c796
AM
13886 {
13887 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
cbd0eecf 13888 if (h->u2.vtable->used[entry])
c152c796
AM
13889 continue;
13890 }
13891 /* Otherwise, kill it. */
13892 rel->r_offset = rel->r_info = rel->r_addend = 0;
13893 }
13894
13895 return TRUE;
13896}
13897
87538722
AM
13898/* Mark sections containing dynamically referenced symbols. When
13899 building shared libraries, we must assume that any visible symbol is
13900 referenced. */
715df9b8 13901
64d03ab5
AM
13902bfd_boolean
13903bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 13904{
87538722 13905 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 13906 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 13907
715df9b8
EB
13908 if ((h->root.type == bfd_link_hash_defined
13909 || h->root.type == bfd_link_hash_defweak)
d664fd41 13910 && ((h->ref_dynamic && !h->forced_local)
c4621b33 13911 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 13912 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 13913 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 13914 && (!bfd_link_executable (info)
22185505 13915 || info->gc_keep_exported
b407645f
AM
13916 || info->export_dynamic
13917 || (h->dynamic
13918 && d != NULL
13919 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 13920 && (h->versioned >= versioned
54e8959c
L
13921 || !bfd_hide_sym_by_version (info->version_info,
13922 h->root.root.string)))))
715df9b8
EB
13923 h->root.u.def.section->flags |= SEC_KEEP;
13924
13925 return TRUE;
13926}
3b36f7e6 13927
74f0fb50
AM
13928/* Keep all sections containing symbols undefined on the command-line,
13929 and the section containing the entry symbol. */
13930
13931void
13932_bfd_elf_gc_keep (struct bfd_link_info *info)
13933{
13934 struct bfd_sym_chain *sym;
13935
13936 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13937 {
13938 struct elf_link_hash_entry *h;
13939
13940 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13941 FALSE, FALSE, FALSE);
13942
13943 if (h != NULL
13944 && (h->root.type == bfd_link_hash_defined
13945 || h->root.type == bfd_link_hash_defweak)
2f5541f3 13946 && !bfd_is_const_section (h->root.u.def.section))
74f0fb50
AM
13947 h->root.u.def.section->flags |= SEC_KEEP;
13948 }
13949}
13950
2f0c68f2
CM
13951bfd_boolean
13952bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13953 struct bfd_link_info *info)
13954{
13955 bfd *ibfd = info->input_bfds;
13956
13957 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13958 {
13959 asection *sec;
13960 struct elf_reloc_cookie cookie;
13961
13962 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13963 continue;
57963c05
AM
13964 sec = ibfd->sections;
13965 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13966 continue;
2f0c68f2
CM
13967
13968 if (!init_reloc_cookie (&cookie, info, ibfd))
13969 return FALSE;
13970
13971 for (sec = ibfd->sections; sec; sec = sec->next)
13972 {
fd361982 13973 if (CONST_STRNEQ (bfd_section_name (sec), ".eh_frame_entry")
2f0c68f2
CM
13974 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13975 {
13976 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13977 fini_reloc_cookie_rels (&cookie, sec);
13978 }
13979 }
13980 }
13981 return TRUE;
13982}
13983
c152c796
AM
13984/* Do mark and sweep of unused sections. */
13985
13986bfd_boolean
13987bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13988{
13989 bfd_boolean ok = TRUE;
13990 bfd *sub;
6a5bb875 13991 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 13992 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 13993 struct elf_link_hash_table *htab;
c152c796 13994
64d03ab5 13995 if (!bed->can_gc_sections
715df9b8 13996 || !is_elf_hash_table (info->hash))
c152c796 13997 {
9793eb77 13998 _bfd_error_handler(_("warning: gc-sections option ignored"));
c152c796
AM
13999 return TRUE;
14000 }
14001
74f0fb50 14002 bed->gc_keep (info);
da44f4e5 14003 htab = elf_hash_table (info);
74f0fb50 14004
9d0a14d3
RS
14005 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14006 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
14007 for (sub = info->input_bfds;
14008 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14009 sub = sub->link.next)
9d0a14d3
RS
14010 {
14011 asection *sec;
14012 struct elf_reloc_cookie cookie;
14013
57963c05
AM
14014 sec = sub->sections;
14015 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14016 continue;
9d0a14d3 14017 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 14018 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
14019 {
14020 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
14021 if (elf_section_data (sec)->sec_info
14022 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
14023 elf_eh_frame_section (sub) = sec;
14024 fini_reloc_cookie_for_section (&cookie, sec);
199af150 14025 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
14026 }
14027 }
9d0a14d3 14028
c152c796 14029 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 14030 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
14031 if (!ok)
14032 return FALSE;
14033
14034 /* Kill the vtable relocations that were not used. */
da44f4e5 14035 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
14036 if (!ok)
14037 return FALSE;
14038
715df9b8 14039 /* Mark dynamically referenced symbols. */
22185505 14040 if (htab->dynamic_sections_created || info->gc_keep_exported)
da44f4e5 14041 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 14042
715df9b8 14043 /* Grovel through relocs to find out who stays ... */
64d03ab5 14044 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 14045 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
14046 {
14047 asection *o;
14048
b19a8f85 14049 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 14050 || elf_object_id (sub) != elf_hash_table_id (htab)
b19a8f85 14051 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
14052 continue;
14053
57963c05
AM
14054 o = sub->sections;
14055 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14056 continue;
14057
7f6ab9f8
AM
14058 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14059 Also treat note sections as a root, if the section is not part
8b6f4cd3
L
14060 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14061 well as FINI_ARRAY sections for ld -r. */
c152c796 14062 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
14063 if (!o->gc_mark
14064 && (o->flags & SEC_EXCLUDE) == 0
24007750 14065 && ((o->flags & SEC_KEEP) != 0
8b6f4cd3
L
14066 || (bfd_link_relocatable (info)
14067 && ((elf_section_data (o)->this_hdr.sh_type
14068 == SHT_PREINIT_ARRAY)
14069 || (elf_section_data (o)->this_hdr.sh_type
14070 == SHT_INIT_ARRAY)
14071 || (elf_section_data (o)->this_hdr.sh_type
14072 == SHT_FINI_ARRAY)))
7f6ab9f8 14073 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
7026832e 14074 && elf_next_in_group (o) == NULL
99fabbc9
JL
14075 && elf_linked_to_section (o) == NULL)
14076 || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14077 && (elf_section_flags (o) & SHF_GNU_RETAIN))))
7f6ab9f8
AM
14078 {
14079 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
14080 return FALSE;
14081 }
c152c796
AM
14082 }
14083
6a5bb875 14084 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 14085 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 14086
c152c796 14087 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 14088 return elf_gc_sweep (abfd, info);
c152c796
AM
14089}
14090\f
14091/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14092
14093bfd_boolean
14094bfd_elf_gc_record_vtinherit (bfd *abfd,
14095 asection *sec,
14096 struct elf_link_hash_entry *h,
14097 bfd_vma offset)
14098{
14099 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14100 struct elf_link_hash_entry **search, *child;
ef53be89 14101 size_t extsymcount;
c152c796
AM
14102 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14103
14104 /* The sh_info field of the symtab header tells us where the
14105 external symbols start. We don't care about the local symbols at
14106 this point. */
14107 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
14108 if (!elf_bad_symtab (abfd))
14109 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
14110
14111 sym_hashes = elf_sym_hashes (abfd);
14112 sym_hashes_end = sym_hashes + extsymcount;
14113
14114 /* Hunt down the child symbol, which is in this section at the same
14115 offset as the relocation. */
14116 for (search = sym_hashes; search != sym_hashes_end; ++search)
14117 {
14118 if ((child = *search) != NULL
14119 && (child->root.type == bfd_link_hash_defined
14120 || child->root.type == bfd_link_hash_defweak)
14121 && child->root.u.def.section == sec
14122 && child->root.u.def.value == offset)
14123 goto win;
14124 }
14125
695344c0 14126 /* xgettext:c-format */
9793eb77 14127 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
2dcf00ce 14128 abfd, sec, (uint64_t) offset);
c152c796
AM
14129 bfd_set_error (bfd_error_invalid_operation);
14130 return FALSE;
14131
14132 win:
cbd0eecf 14133 if (!child->u2.vtable)
f6e332e6 14134 {
cbd0eecf
L
14135 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
14136 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
14137 if (!child->u2.vtable)
f6e332e6
AM
14138 return FALSE;
14139 }
c152c796
AM
14140 if (!h)
14141 {
14142 /* This *should* only be the absolute section. It could potentially
14143 be that someone has defined a non-global vtable though, which
14144 would be bad. It isn't worth paging in the local symbols to be
14145 sure though; that case should simply be handled by the assembler. */
14146
cbd0eecf 14147 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
14148 }
14149 else
cbd0eecf 14150 child->u2.vtable->parent = h;
c152c796
AM
14151
14152 return TRUE;
14153}
14154
14155/* Called from check_relocs to record the existence of a VTENTRY reloc. */
14156
14157bfd_boolean
a0ea3a14 14158bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
c152c796
AM
14159 struct elf_link_hash_entry *h,
14160 bfd_vma addend)
14161{
14162 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14163 unsigned int log_file_align = bed->s->log_file_align;
14164
a0ea3a14
L
14165 if (!h)
14166 {
14167 /* xgettext:c-format */
14168 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14169 abfd, sec);
14170 bfd_set_error (bfd_error_bad_value);
14171 return FALSE;
14172 }
14173
cbd0eecf 14174 if (!h->u2.vtable)
f6e332e6 14175 {
cbd0eecf
L
14176 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14177 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14178 if (!h->u2.vtable)
f6e332e6
AM
14179 return FALSE;
14180 }
14181
cbd0eecf 14182 if (addend >= h->u2.vtable->size)
c152c796
AM
14183 {
14184 size_t size, bytes, file_align;
cbd0eecf 14185 bfd_boolean *ptr = h->u2.vtable->used;
c152c796
AM
14186
14187 /* While the symbol is undefined, we have to be prepared to handle
14188 a zero size. */
14189 file_align = 1 << log_file_align;
14190 if (h->root.type == bfd_link_hash_undefined)
14191 size = addend + file_align;
14192 else
14193 {
14194 size = h->size;
14195 if (addend >= size)
14196 {
14197 /* Oops! We've got a reference past the defined end of
14198 the table. This is probably a bug -- shall we warn? */
14199 size = addend + file_align;
14200 }
14201 }
14202 size = (size + file_align - 1) & -file_align;
14203
14204 /* Allocate one extra entry for use as a "done" flag for the
14205 consolidation pass. */
14206 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
14207
14208 if (ptr)
14209 {
a50b1753 14210 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
14211
14212 if (ptr != NULL)
14213 {
14214 size_t oldbytes;
14215
cbd0eecf 14216 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
c152c796
AM
14217 * sizeof (bfd_boolean));
14218 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14219 }
14220 }
14221 else
a50b1753 14222 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
14223
14224 if (ptr == NULL)
14225 return FALSE;
14226
14227 /* And arrange for that done flag to be at index -1. */
cbd0eecf
L
14228 h->u2.vtable->used = ptr + 1;
14229 h->u2.vtable->size = size;
c152c796
AM
14230 }
14231
cbd0eecf 14232 h->u2.vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
14233
14234 return TRUE;
14235}
14236
ae17ab41
CM
14237/* Map an ELF section header flag to its corresponding string. */
14238typedef struct
14239{
14240 char *flag_name;
14241 flagword flag_value;
14242} elf_flags_to_name_table;
14243
3f75e1d6 14244static const elf_flags_to_name_table elf_flags_to_names [] =
ae17ab41
CM
14245{
14246 { "SHF_WRITE", SHF_WRITE },
14247 { "SHF_ALLOC", SHF_ALLOC },
14248 { "SHF_EXECINSTR", SHF_EXECINSTR },
14249 { "SHF_MERGE", SHF_MERGE },
14250 { "SHF_STRINGS", SHF_STRINGS },
14251 { "SHF_INFO_LINK", SHF_INFO_LINK},
14252 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14253 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14254 { "SHF_GROUP", SHF_GROUP },
14255 { "SHF_TLS", SHF_TLS },
14256 { "SHF_MASKOS", SHF_MASKOS },
14257 { "SHF_EXCLUDE", SHF_EXCLUDE },
14258};
14259
b9c361e0
JL
14260/* Returns TRUE if the section is to be included, otherwise FALSE. */
14261bfd_boolean
ae17ab41 14262bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 14263 struct flag_info *flaginfo,
b9c361e0 14264 asection *section)
ae17ab41 14265{
8b127cbc 14266 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 14267
8b127cbc 14268 if (!flaginfo->flags_initialized)
ae17ab41 14269 {
8b127cbc
AM
14270 bfd *obfd = info->output_bfd;
14271 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14272 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
14273 int with_hex = 0;
14274 int without_hex = 0;
14275
8b127cbc 14276 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 14277 {
b9c361e0 14278 unsigned i;
8b127cbc 14279 flagword (*lookup) (char *);
ae17ab41 14280
8b127cbc
AM
14281 lookup = bed->elf_backend_lookup_section_flags_hook;
14282 if (lookup != NULL)
ae17ab41 14283 {
8b127cbc 14284 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
14285
14286 if (hexval != 0)
14287 {
14288 if (tf->with == with_flags)
14289 with_hex |= hexval;
14290 else if (tf->with == without_flags)
14291 without_hex |= hexval;
14292 tf->valid = TRUE;
14293 continue;
14294 }
ae17ab41 14295 }
8b127cbc 14296 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 14297 {
8b127cbc 14298 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
14299 {
14300 if (tf->with == with_flags)
14301 with_hex |= elf_flags_to_names[i].flag_value;
14302 else if (tf->with == without_flags)
14303 without_hex |= elf_flags_to_names[i].flag_value;
14304 tf->valid = TRUE;
14305 break;
14306 }
14307 }
8b127cbc 14308 if (!tf->valid)
b9c361e0 14309 {
68ffbac6 14310 info->callbacks->einfo
9793eb77 14311 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 14312 return FALSE;
ae17ab41
CM
14313 }
14314 }
8b127cbc
AM
14315 flaginfo->flags_initialized = TRUE;
14316 flaginfo->only_with_flags |= with_hex;
14317 flaginfo->not_with_flags |= without_hex;
ae17ab41 14318 }
ae17ab41 14319
8b127cbc 14320 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
14321 return FALSE;
14322
8b127cbc 14323 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
14324 return FALSE;
14325
14326 return TRUE;
ae17ab41
CM
14327}
14328
c152c796
AM
14329struct alloc_got_off_arg {
14330 bfd_vma gotoff;
10455f89 14331 struct bfd_link_info *info;
c152c796
AM
14332};
14333
14334/* We need a special top-level link routine to convert got reference counts
14335 to real got offsets. */
14336
14337static bfd_boolean
14338elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14339{
a50b1753 14340 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
14341 bfd *obfd = gofarg->info->output_bfd;
14342 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 14343
c152c796
AM
14344 if (h->got.refcount > 0)
14345 {
14346 h->got.offset = gofarg->gotoff;
10455f89 14347 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
14348 }
14349 else
14350 h->got.offset = (bfd_vma) -1;
14351
14352 return TRUE;
14353}
14354
14355/* And an accompanying bit to work out final got entry offsets once
14356 we're done. Should be called from final_link. */
14357
14358bfd_boolean
14359bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14360 struct bfd_link_info *info)
14361{
14362 bfd *i;
14363 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14364 bfd_vma gotoff;
c152c796
AM
14365 struct alloc_got_off_arg gofarg;
14366
10455f89
HPN
14367 BFD_ASSERT (abfd == info->output_bfd);
14368
c152c796
AM
14369 if (! is_elf_hash_table (info->hash))
14370 return FALSE;
14371
14372 /* The GOT offset is relative to the .got section, but the GOT header is
14373 put into the .got.plt section, if the backend uses it. */
14374 if (bed->want_got_plt)
14375 gotoff = 0;
14376 else
14377 gotoff = bed->got_header_size;
14378
14379 /* Do the local .got entries first. */
c72f2fb2 14380 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
14381 {
14382 bfd_signed_vma *local_got;
ef53be89 14383 size_t j, locsymcount;
c152c796
AM
14384 Elf_Internal_Shdr *symtab_hdr;
14385
14386 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14387 continue;
14388
14389 local_got = elf_local_got_refcounts (i);
14390 if (!local_got)
14391 continue;
14392
14393 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14394 if (elf_bad_symtab (i))
14395 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14396 else
14397 locsymcount = symtab_hdr->sh_info;
14398
14399 for (j = 0; j < locsymcount; ++j)
14400 {
14401 if (local_got[j] > 0)
14402 {
14403 local_got[j] = gotoff;
10455f89 14404 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
14405 }
14406 else
14407 local_got[j] = (bfd_vma) -1;
14408 }
14409 }
14410
14411 /* Then the global .got entries. .plt refcounts are handled by
14412 adjust_dynamic_symbol */
14413 gofarg.gotoff = gotoff;
10455f89 14414 gofarg.info = info;
c152c796
AM
14415 elf_link_hash_traverse (elf_hash_table (info),
14416 elf_gc_allocate_got_offsets,
14417 &gofarg);
14418 return TRUE;
14419}
14420
14421/* Many folk need no more in the way of final link than this, once
14422 got entry reference counting is enabled. */
14423
14424bfd_boolean
14425bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14426{
14427 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14428 return FALSE;
14429
14430 /* Invoke the regular ELF backend linker to do all the work. */
14431 return bfd_elf_final_link (abfd, info);
14432}
14433
14434bfd_boolean
14435bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14436{
a50b1753 14437 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
14438
14439 if (rcookie->bad_symtab)
14440 rcookie->rel = rcookie->rels;
14441
14442 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14443 {
14444 unsigned long r_symndx;
14445
14446 if (! rcookie->bad_symtab)
14447 if (rcookie->rel->r_offset > offset)
14448 return FALSE;
14449 if (rcookie->rel->r_offset != offset)
14450 continue;
14451
14452 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 14453 if (r_symndx == STN_UNDEF)
c152c796
AM
14454 return TRUE;
14455
14456 if (r_symndx >= rcookie->locsymcount
14457 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14458 {
14459 struct elf_link_hash_entry *h;
14460
14461 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14462
14463 while (h->root.type == bfd_link_hash_indirect
14464 || h->root.type == bfd_link_hash_warning)
14465 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14466
14467 if ((h->root.type == bfd_link_hash_defined
14468 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
14469 && (h->root.u.def.section->owner != rcookie->abfd
14470 || h->root.u.def.section->kept_section != NULL
14471 || discarded_section (h->root.u.def.section)))
c152c796 14472 return TRUE;
c152c796
AM
14473 }
14474 else
14475 {
14476 /* It's not a relocation against a global symbol,
14477 but it could be a relocation against a local
14478 symbol for a discarded section. */
14479 asection *isec;
14480 Elf_Internal_Sym *isym;
14481
14482 /* Need to: get the symbol; get the section. */
14483 isym = &rcookie->locsyms[r_symndx];
cb33740c 14484 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
14485 if (isec != NULL
14486 && (isec->kept_section != NULL
14487 || discarded_section (isec)))
cb33740c 14488 return TRUE;
c152c796
AM
14489 }
14490 return FALSE;
14491 }
14492 return FALSE;
14493}
14494
14495/* Discard unneeded references to discarded sections.
75938853
AM
14496 Returns -1 on error, 1 if any section's size was changed, 0 if
14497 nothing changed. This function assumes that the relocations are in
14498 sorted order, which is true for all known assemblers. */
c152c796 14499
75938853 14500int
c152c796
AM
14501bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14502{
14503 struct elf_reloc_cookie cookie;
18cd5bce 14504 asection *o;
c152c796 14505 bfd *abfd;
75938853 14506 int changed = 0;
c152c796
AM
14507
14508 if (info->traditional_format
14509 || !is_elf_hash_table (info->hash))
75938853 14510 return 0;
c152c796 14511
18cd5bce
AM
14512 o = bfd_get_section_by_name (output_bfd, ".stab");
14513 if (o != NULL)
c152c796 14514 {
18cd5bce 14515 asection *i;
c152c796 14516
18cd5bce 14517 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 14518 {
18cd5bce
AM
14519 if (i->size == 0
14520 || i->reloc_count == 0
14521 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14522 continue;
c152c796 14523
18cd5bce
AM
14524 abfd = i->owner;
14525 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14526 continue;
c152c796 14527
18cd5bce 14528 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14529 return -1;
c152c796 14530
18cd5bce
AM
14531 if (_bfd_discard_section_stabs (abfd, i,
14532 elf_section_data (i)->sec_info,
5241d853
RS
14533 bfd_elf_reloc_symbol_deleted_p,
14534 &cookie))
75938853 14535 changed = 1;
18cd5bce
AM
14536
14537 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14538 }
18cd5bce
AM
14539 }
14540
2f0c68f2
CM
14541 o = NULL;
14542 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14543 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
14544 if (o != NULL)
14545 {
14546 asection *i;
d7153c4a 14547 int eh_changed = 0;
66631823 14548 unsigned int eh_alignment; /* Octets. */
c152c796 14549
18cd5bce 14550 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 14551 {
18cd5bce
AM
14552 if (i->size == 0)
14553 continue;
14554
14555 abfd = i->owner;
14556 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14557 continue;
14558
14559 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14560 return -1;
18cd5bce
AM
14561
14562 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14563 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
14564 bfd_elf_reloc_symbol_deleted_p,
14565 &cookie))
d7153c4a
AM
14566 {
14567 eh_changed = 1;
14568 if (i->size != i->rawsize)
14569 changed = 1;
14570 }
18cd5bce
AM
14571
14572 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14573 }
9866ffe2 14574
66631823
CE
14575 eh_alignment = ((1 << o->alignment_power)
14576 * bfd_octets_per_byte (output_bfd, o));
9866ffe2
AM
14577 /* Skip over zero terminator, and prevent empty sections from
14578 adding alignment padding at the end. */
14579 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14580 if (i->size == 0)
14581 i->flags |= SEC_EXCLUDE;
14582 else if (i->size > 4)
14583 break;
14584 /* The last non-empty eh_frame section doesn't need padding. */
14585 if (i != NULL)
14586 i = i->map_tail.s;
14587 /* Any prior sections must pad the last FDE out to the output
14588 section alignment. Otherwise we might have zero padding
14589 between sections, which would be seen as a terminator. */
14590 for (; i != NULL; i = i->map_tail.s)
14591 if (i->size == 4)
14592 /* All but the last zero terminator should have been removed. */
14593 BFD_FAIL ();
14594 else
14595 {
14596 bfd_size_type size
14597 = (i->size + eh_alignment - 1) & -eh_alignment;
14598 if (i->size != size)
af471f82 14599 {
9866ffe2
AM
14600 i->size = size;
14601 changed = 1;
14602 eh_changed = 1;
af471f82 14603 }
9866ffe2 14604 }
d7153c4a
AM
14605 if (eh_changed)
14606 elf_link_hash_traverse (elf_hash_table (info),
14607 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
18cd5bce 14608 }
c152c796 14609
18cd5bce
AM
14610 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14611 {
14612 const struct elf_backend_data *bed;
57963c05 14613 asection *s;
c152c796 14614
18cd5bce
AM
14615 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14616 continue;
57963c05
AM
14617 s = abfd->sections;
14618 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14619 continue;
18cd5bce
AM
14620
14621 bed = get_elf_backend_data (abfd);
14622
14623 if (bed->elf_backend_discard_info != NULL)
14624 {
14625 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 14626 return -1;
18cd5bce
AM
14627
14628 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 14629 changed = 1;
18cd5bce
AM
14630
14631 fini_reloc_cookie (&cookie, abfd);
14632 }
c152c796
AM
14633 }
14634
2f0c68f2
CM
14635 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14636 _bfd_elf_end_eh_frame_parsing (info);
14637
14638 if (info->eh_frame_hdr_type
0e1862bb 14639 && !bfd_link_relocatable (info)
c152c796 14640 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 14641 changed = 1;
c152c796 14642
75938853 14643 return changed;
c152c796 14644}
082b7297 14645
43e1669b 14646bfd_boolean
0c511000 14647_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 14648 asection *sec,
c0f00686 14649 struct bfd_link_info *info)
082b7297
L
14650{
14651 flagword flags;
c77ec726 14652 const char *name, *key;
082b7297
L
14653 struct bfd_section_already_linked *l;
14654 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 14655
c77ec726
AM
14656 if (sec->output_section == bfd_abs_section_ptr)
14657 return FALSE;
0c511000 14658
c77ec726 14659 flags = sec->flags;
0c511000 14660
c77ec726
AM
14661 /* Return if it isn't a linkonce section. A comdat group section
14662 also has SEC_LINK_ONCE set. */
14663 if ((flags & SEC_LINK_ONCE) == 0)
14664 return FALSE;
0c511000 14665
c77ec726
AM
14666 /* Don't put group member sections on our list of already linked
14667 sections. They are handled as a group via their group section. */
14668 if (elf_sec_group (sec) != NULL)
14669 return FALSE;
0c511000 14670
c77ec726
AM
14671 /* For a SHT_GROUP section, use the group signature as the key. */
14672 name = sec->name;
14673 if ((flags & SEC_GROUP) != 0
14674 && elf_next_in_group (sec) != NULL
14675 && elf_group_name (elf_next_in_group (sec)) != NULL)
14676 key = elf_group_name (elf_next_in_group (sec));
14677 else
14678 {
14679 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 14680 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
14681 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14682 key++;
0c511000 14683 else
c77ec726
AM
14684 /* Must be a user linkonce section that doesn't follow gcc's
14685 naming convention. In this case we won't be matching
14686 single member groups. */
14687 key = name;
0c511000 14688 }
6d2cd210 14689
c77ec726 14690 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
14691
14692 for (l = already_linked_list->entry; l != NULL; l = l->next)
14693 {
c2370991 14694 /* We may have 2 different types of sections on the list: group
c77ec726
AM
14695 sections with a signature of <key> (<key> is some string),
14696 and linkonce sections named .gnu.linkonce.<type>.<key>.
14697 Match like sections. LTO plugin sections are an exception.
14698 They are always named .gnu.linkonce.t.<key> and match either
14699 type of section. */
14700 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14701 && ((flags & SEC_GROUP) != 0
14702 || strcmp (name, l->sec->name) == 0))
e8a83e93
MB
14703 || (l->sec->owner->flags & BFD_PLUGIN) != 0
14704 || (sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
14705 {
14706 /* The section has already been linked. See if we should
6d2cd210 14707 issue a warning. */
c77ec726
AM
14708 if (!_bfd_handle_already_linked (sec, l, info))
14709 return FALSE;
082b7297 14710
c77ec726 14711 if (flags & SEC_GROUP)
3d7f7666 14712 {
c77ec726
AM
14713 asection *first = elf_next_in_group (sec);
14714 asection *s = first;
3d7f7666 14715
c77ec726 14716 while (s != NULL)
3d7f7666 14717 {
c77ec726
AM
14718 s->output_section = bfd_abs_section_ptr;
14719 /* Record which group discards it. */
14720 s->kept_section = l->sec;
14721 s = elf_next_in_group (s);
14722 /* These lists are circular. */
14723 if (s == first)
14724 break;
3d7f7666
L
14725 }
14726 }
082b7297 14727
43e1669b 14728 return TRUE;
082b7297
L
14729 }
14730 }
14731
c77ec726
AM
14732 /* A single member comdat group section may be discarded by a
14733 linkonce section and vice versa. */
14734 if ((flags & SEC_GROUP) != 0)
3d7f7666 14735 {
c77ec726 14736 asection *first = elf_next_in_group (sec);
c2370991 14737
c77ec726
AM
14738 if (first != NULL && elf_next_in_group (first) == first)
14739 /* Check this single member group against linkonce sections. */
14740 for (l = already_linked_list->entry; l != NULL; l = l->next)
14741 if ((l->sec->flags & SEC_GROUP) == 0
14742 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14743 {
14744 first->output_section = bfd_abs_section_ptr;
14745 first->kept_section = l->sec;
14746 sec->output_section = bfd_abs_section_ptr;
14747 break;
14748 }
14749 }
14750 else
14751 /* Check this linkonce section against single member groups. */
14752 for (l = already_linked_list->entry; l != NULL; l = l->next)
14753 if (l->sec->flags & SEC_GROUP)
6d2cd210 14754 {
c77ec726 14755 asection *first = elf_next_in_group (l->sec);
6d2cd210 14756
c77ec726
AM
14757 if (first != NULL
14758 && elf_next_in_group (first) == first
14759 && bfd_elf_match_symbols_in_sections (first, sec, info))
14760 {
14761 sec->output_section = bfd_abs_section_ptr;
14762 sec->kept_section = first;
14763 break;
14764 }
6d2cd210 14765 }
0c511000 14766
c77ec726
AM
14767 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14768 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14769 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14770 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14771 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14772 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14773 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14774 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14775 The reverse order cannot happen as there is never a bfd with only the
14776 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14777 matter as here were are looking only for cross-bfd sections. */
14778
14779 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14780 for (l = already_linked_list->entry; l != NULL; l = l->next)
14781 if ((l->sec->flags & SEC_GROUP) == 0
14782 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14783 {
14784 if (abfd != l->sec->owner)
14785 sec->output_section = bfd_abs_section_ptr;
14786 break;
14787 }
80c29487 14788
082b7297 14789 /* This is the first section with this name. Record it. */
c77ec726 14790 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 14791 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 14792 return sec->output_section == bfd_abs_section_ptr;
082b7297 14793}
81e1b023 14794
a4d8e49b
L
14795bfd_boolean
14796_bfd_elf_common_definition (Elf_Internal_Sym *sym)
14797{
14798 return sym->st_shndx == SHN_COMMON;
14799}
14800
14801unsigned int
14802_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14803{
14804 return SHN_COMMON;
14805}
14806
14807asection *
14808_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14809{
14810 return bfd_com_section_ptr;
14811}
10455f89
HPN
14812
14813bfd_vma
14814_bfd_elf_default_got_elt_size (bfd *abfd,
14815 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14816 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14817 bfd *ibfd ATTRIBUTE_UNUSED,
14818 unsigned long symndx ATTRIBUTE_UNUSED)
14819{
14820 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14821 return bed->s->arch_size / 8;
14822}
83bac4b0
NC
14823
14824/* Routines to support the creation of dynamic relocs. */
14825
83bac4b0
NC
14826/* Returns the name of the dynamic reloc section associated with SEC. */
14827
14828static const char *
14829get_dynamic_reloc_section_name (bfd * abfd,
14830 asection * sec,
14831 bfd_boolean is_rela)
14832{
ddcf1fcf 14833 char *name;
fd361982 14834 const char *old_name = bfd_section_name (sec);
ddcf1fcf 14835 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 14836
ddcf1fcf 14837 if (old_name == NULL)
83bac4b0
NC
14838 return NULL;
14839
ddcf1fcf 14840 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 14841 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
14842
14843 return name;
14844}
14845
14846/* Returns the dynamic reloc section associated with SEC.
14847 If necessary compute the name of the dynamic reloc section based
14848 on SEC's name (looked up in ABFD's string table) and the setting
14849 of IS_RELA. */
14850
14851asection *
14852_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14853 asection * sec,
14854 bfd_boolean is_rela)
14855{
14856 asection * reloc_sec = elf_section_data (sec)->sreloc;
14857
14858 if (reloc_sec == NULL)
14859 {
14860 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14861
14862 if (name != NULL)
14863 {
3d4d4302 14864 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
14865
14866 if (reloc_sec != NULL)
14867 elf_section_data (sec)->sreloc = reloc_sec;
14868 }
14869 }
14870
14871 return reloc_sec;
14872}
14873
14874/* Returns the dynamic reloc section associated with SEC. If the
14875 section does not exist it is created and attached to the DYNOBJ
14876 bfd and stored in the SRELOC field of SEC's elf_section_data
14877 structure.
f8076f98 14878
83bac4b0
NC
14879 ALIGNMENT is the alignment for the newly created section and
14880 IS_RELA defines whether the name should be .rela.<SEC's name>
14881 or .rel.<SEC's name>. The section name is looked up in the
14882 string table associated with ABFD. */
14883
14884asection *
ca4be51c
AM
14885_bfd_elf_make_dynamic_reloc_section (asection *sec,
14886 bfd *dynobj,
14887 unsigned int alignment,
14888 bfd *abfd,
14889 bfd_boolean is_rela)
83bac4b0
NC
14890{
14891 asection * reloc_sec = elf_section_data (sec)->sreloc;
14892
14893 if (reloc_sec == NULL)
14894 {
14895 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14896
14897 if (name == NULL)
14898 return NULL;
14899
3d4d4302 14900 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
14901
14902 if (reloc_sec == NULL)
14903 {
3d4d4302
AM
14904 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14905 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
14906 if ((sec->flags & SEC_ALLOC) != 0)
14907 flags |= SEC_ALLOC | SEC_LOAD;
14908
3d4d4302 14909 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
14910 if (reloc_sec != NULL)
14911 {
8877b5e5
AM
14912 /* _bfd_elf_get_sec_type_attr chooses a section type by
14913 name. Override as it may be wrong, eg. for a user
14914 section named "auto" we'll get ".relauto" which is
14915 seen to be a .rela section. */
14916 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
fd361982 14917 if (!bfd_set_section_alignment (reloc_sec, alignment))
83bac4b0
NC
14918 reloc_sec = NULL;
14919 }
14920 }
14921
14922 elf_section_data (sec)->sreloc = reloc_sec;
14923 }
14924
14925 return reloc_sec;
14926}
1338dd10 14927
bffebb6b
AM
14928/* Copy the ELF symbol type and other attributes for a linker script
14929 assignment from HSRC to HDEST. Generally this should be treated as
14930 if we found a strong non-dynamic definition for HDEST (except that
14931 ld ignores multiple definition errors). */
1338dd10 14932void
bffebb6b
AM
14933_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14934 struct bfd_link_hash_entry *hdest,
14935 struct bfd_link_hash_entry *hsrc)
1338dd10 14936{
bffebb6b
AM
14937 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14938 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14939 Elf_Internal_Sym isym;
1338dd10
PB
14940
14941 ehdest->type = ehsrc->type;
35fc36a8 14942 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
14943
14944 isym.st_other = ehsrc->other;
5160d0f3 14945 elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, TRUE, FALSE);
1338dd10 14946}
351f65ca
L
14947
14948/* Append a RELA relocation REL to section S in BFD. */
14949
14950void
14951elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14952{
14953 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14954 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14955 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14956 bed->s->swap_reloca_out (abfd, rel, loc);
14957}
14958
14959/* Append a REL relocation REL to section S in BFD. */
14960
14961void
14962elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14963{
14964 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14965 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14966 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 14967 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 14968}
7dba9362
AM
14969
14970/* Define __start, __stop, .startof. or .sizeof. symbol. */
14971
14972struct bfd_link_hash_entry *
14973bfd_elf_define_start_stop (struct bfd_link_info *info,
14974 const char *symbol, asection *sec)
14975{
487b6440 14976 struct elf_link_hash_entry *h;
7dba9362 14977
487b6440
AM
14978 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14979 FALSE, FALSE, TRUE);
e1b5d517 14980 /* NB: Common symbols will be turned into definition later. */
487b6440
AM
14981 if (h != NULL
14982 && (h->root.type == bfd_link_hash_undefined
14983 || h->root.type == bfd_link_hash_undefweak
e1b5d517
L
14984 || ((h->ref_regular || h->def_dynamic)
14985 && !h->def_regular
14986 && h->root.type != bfd_link_hash_common)))
7dba9362 14987 {
bf3077a6 14988 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
e1b5d517 14989 h->verinfo.verdef = NULL;
487b6440
AM
14990 h->root.type = bfd_link_hash_defined;
14991 h->root.u.def.section = sec;
14992 h->root.u.def.value = 0;
14993 h->def_regular = 1;
14994 h->def_dynamic = 0;
14995 h->start_stop = 1;
14996 h->u2.start_stop_section = sec;
14997 if (symbol[0] == '.')
14998 {
14999 /* .startof. and .sizeof. symbols are local. */
559192d8
AM
15000 const struct elf_backend_data *bed;
15001 bed = get_elf_backend_data (info->output_bfd);
15002 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
487b6440 15003 }
36b8fda5
AM
15004 else
15005 {
15006 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
cae64165
RM
15007 h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15008 | info->start_stop_visibility);
bf3077a6 15009 if (was_dynamic)
36b8fda5
AM
15010 bfd_elf_link_record_dynamic_symbol (info, h);
15011 }
487b6440 15012 return &h->root;
7dba9362 15013 }
487b6440 15014 return NULL;
7dba9362 15015}
5dbc8b37
L
15016
15017/* Find dynamic relocs for H that apply to read-only sections. */
15018
15019asection *
15020_bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15021{
15022 struct elf_dyn_relocs *p;
15023
15024 for (p = h->dyn_relocs; p != NULL; p = p->next)
15025 {
15026 asection *s = p->sec->output_section;
15027
15028 if (s != NULL && (s->flags & SEC_READONLY) != 0)
15029 return p->sec;
15030 }
15031 return NULL;
15032}
d49e5065
L
15033
15034/* Set DF_TEXTREL if we find any dynamic relocs that apply to
15035 read-only sections. */
15036
15037bfd_boolean
15038_bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15039{
15040 asection *sec;
15041
15042 if (h->root.type == bfd_link_hash_indirect)
15043 return TRUE;
15044
15045 sec = _bfd_elf_readonly_dynrelocs (h);
15046 if (sec != NULL)
15047 {
15048 struct bfd_link_info *info = (struct bfd_link_info *) inf;
15049
15050 info->flags |= DF_TEXTREL;
15051 /* xgettext:c-format */
15052 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15053 "in read-only section `%pA'\n"),
15054 sec->owner, h->root.root.string, sec);
15055
15056 if (bfd_link_textrel_check (info))
15057 /* xgettext:c-format */
15058 info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15059 "in read-only section `%pA'\n"),
15060 sec->owner, h->root.root.string, sec);
15061
15062 /* Not an error, just cut short the traversal. */
15063 return FALSE;
15064 }
15065 return TRUE;
15066}
3084d7a2
L
15067
15068/* Add dynamic tags. */
15069
15070bfd_boolean
15071_bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
15072 bfd_boolean need_dynamic_reloc)
15073{
15074 struct elf_link_hash_table *htab = elf_hash_table (info);
15075
15076 if (htab->dynamic_sections_created)
15077 {
15078 /* Add some entries to the .dynamic section. We fill in the
15079 values later, in finish_dynamic_sections, but we must add
15080 the entries now so that we get the correct size for the
15081 .dynamic section. The DT_DEBUG entry is filled in by the
15082 dynamic linker and used by the debugger. */
15083#define add_dynamic_entry(TAG, VAL) \
15084 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15085
15086 const struct elf_backend_data *bed
15087 = get_elf_backend_data (output_bfd);
15088
15089 if (bfd_link_executable (info))
15090 {
15091 if (!add_dynamic_entry (DT_DEBUG, 0))
15092 return FALSE;
15093 }
15094
15095 if (htab->dt_pltgot_required || htab->splt->size != 0)
15096 {
15097 /* DT_PLTGOT is used by prelink even if there is no PLT
15098 relocation. */
15099 if (!add_dynamic_entry (DT_PLTGOT, 0))
15100 return FALSE;
15101 }
15102
15103 if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15104 {
15105 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15106 || !add_dynamic_entry (DT_PLTREL,
15107 (bed->rela_plts_and_copies_p
15108 ? DT_RELA : DT_REL))
15109 || !add_dynamic_entry (DT_JMPREL, 0))
15110 return FALSE;
15111 }
15112
15113 if (htab->tlsdesc_plt
15114 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15115 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
15116 return FALSE;
15117
15118 if (need_dynamic_reloc)
15119 {
15120 if (bed->rela_plts_and_copies_p)
15121 {
15122 if (!add_dynamic_entry (DT_RELA, 0)
15123 || !add_dynamic_entry (DT_RELASZ, 0)
15124 || !add_dynamic_entry (DT_RELAENT,
15125 bed->s->sizeof_rela))
15126 return FALSE;
15127 }
15128 else
15129 {
15130 if (!add_dynamic_entry (DT_REL, 0)
15131 || !add_dynamic_entry (DT_RELSZ, 0)
15132 || !add_dynamic_entry (DT_RELENT,
15133 bed->s->sizeof_rel))
15134 return FALSE;
15135 }
15136
15137 /* If any dynamic relocs apply to a read-only section,
15138 then we need a DT_TEXTREL entry. */
15139 if ((info->flags & DF_TEXTREL) == 0)
15140 elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15141 info);
15142
15143 if ((info->flags & DF_TEXTREL) != 0)
15144 {
15145 if (htab->ifunc_resolvers)
15146 info->callbacks->einfo
15147 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15148 "may result in a segfault at runtime; recompile with %s\n"),
15149 bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15150
15151 if (!add_dynamic_entry (DT_TEXTREL, 0))
15152 return FALSE;
15153 }
15154 }
15155 }
15156#undef add_dynamic_entry
15157
15158 return TRUE;
15159}
This page took 3.61977 seconds and 4 git commands to generate.