[gdb/testsuite] Fix gdb.cp/nested-types.exp with check-read1
[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 35#include <limits.h>
4b69ce9b
AM
36#ifndef CHAR_BIT
37#define CHAR_BIT 8
38#endif
39
28caa186
AM
40/* This struct is used to pass information to routines called via
41 elf_link_hash_traverse which must return failure. */
42
43struct elf_info_failed
44{
45 struct bfd_link_info *info;
0a1b45a2 46 bool failed;
28caa186
AM
47};
48
49/* This structure is used to pass information to
50 _bfd_elf_link_find_version_dependencies. */
51
52struct elf_find_verdep_info
53{
54 /* General link information. */
55 struct bfd_link_info *info;
56 /* The number of dependencies. */
57 unsigned int vers;
58 /* Whether we had a failure. */
0a1b45a2 59 bool failed;
28caa186
AM
60};
61
0a1b45a2 62static bool _bfd_elf_fix_symbol_flags
28caa186
AM
63 (struct elf_link_hash_entry *, struct elf_info_failed *);
64
2f0c68f2
CM
65asection *
66_bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
67 unsigned long r_symndx,
0a1b45a2 68 bool discard)
2f0c68f2
CM
69{
70 if (r_symndx >= cookie->locsymcount
71 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
72 {
73 struct elf_link_hash_entry *h;
74
75 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
76
77 while (h->root.type == bfd_link_hash_indirect
78 || h->root.type == bfd_link_hash_warning)
79 h = (struct elf_link_hash_entry *) h->root.u.i.link;
80
81 if ((h->root.type == bfd_link_hash_defined
82 || h->root.type == bfd_link_hash_defweak)
83 && discarded_section (h->root.u.def.section))
07d6d2b8 84 return h->root.u.def.section;
2f0c68f2
CM
85 else
86 return NULL;
87 }
88 else
89 {
90 /* It's not a relocation against a global symbol,
91 but it could be a relocation against a local
92 symbol for a discarded section. */
93 asection *isec;
94 Elf_Internal_Sym *isym;
95
96 /* Need to: get the symbol; get the section. */
97 isym = &cookie->locsyms[r_symndx];
98 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
99 if (isec != NULL
100 && discard ? discarded_section (isec) : 1)
101 return isec;
102 }
103 return NULL;
104}
105
d98685ac
AM
106/* Define a symbol in a dynamic linkage section. */
107
108struct elf_link_hash_entry *
109_bfd_elf_define_linkage_sym (bfd *abfd,
110 struct bfd_link_info *info,
111 asection *sec,
112 const char *name)
113{
114 struct elf_link_hash_entry *h;
115 struct bfd_link_hash_entry *bh;
ccabcbe5 116 const struct elf_backend_data *bed;
d98685ac 117
0a1b45a2 118 h = elf_link_hash_lookup (elf_hash_table (info), name, false, false, false);
d98685ac
AM
119 if (h != NULL)
120 {
121 /* Zap symbol defined in an as-needed lib that wasn't linked.
122 This is a symptom of a larger problem: Absolute symbols
123 defined in shared libraries can't be overridden, because we
124 lose the link to the bfd which is via the symbol section. */
125 h->root.type = bfd_link_hash_new;
ad32986f 126 bh = &h->root;
d98685ac 127 }
ad32986f
NC
128 else
129 bh = NULL;
d98685ac 130
cf18fda4 131 bed = get_elf_backend_data (abfd);
d98685ac 132 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
0a1b45a2 133 sec, 0, NULL, false, bed->collect,
d98685ac
AM
134 &bh))
135 return NULL;
136 h = (struct elf_link_hash_entry *) bh;
ad32986f 137 BFD_ASSERT (h != NULL);
d98685ac 138 h->def_regular = 1;
e28df02b 139 h->non_elf = 0;
12b2843a 140 h->root.linker_def = 1;
d98685ac 141 h->type = STT_OBJECT;
00b7642b
AM
142 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
143 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
d98685ac 144
0a1b45a2 145 (*bed->elf_backend_hide_symbol) (info, h, true);
d98685ac
AM
146 return h;
147}
148
0a1b45a2 149bool
268b6b39 150_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
151{
152 flagword flags;
aad5d350 153 asection *s;
252b5132 154 struct elf_link_hash_entry *h;
9c5bfbb7 155 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 156 struct elf_link_hash_table *htab = elf_hash_table (info);
252b5132
RH
157
158 /* This function may be called more than once. */
ce558b89 159 if (htab->sgot != NULL)
0a1b45a2 160 return true;
252b5132 161
e5a52504 162 flags = bed->dynamic_sec_flags;
252b5132 163
14b2f831
AM
164 s = bfd_make_section_anyway_with_flags (abfd,
165 (bed->rela_plts_and_copies_p
166 ? ".rela.got" : ".rel.got"),
167 (bed->dynamic_sec_flags
168 | SEC_READONLY));
6de2ae4a 169 if (s == NULL
fd361982 170 || !bfd_set_section_alignment (s, bed->s->log_file_align))
0a1b45a2 171 return false;
6de2ae4a 172 htab->srelgot = s;
252b5132 173
14b2f831 174 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
64e77c6d 175 if (s == NULL
fd361982 176 || !bfd_set_section_alignment (s, bed->s->log_file_align))
0a1b45a2 177 return false;
64e77c6d
L
178 htab->sgot = s;
179
252b5132
RH
180 if (bed->want_got_plt)
181 {
14b2f831 182 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
252b5132 183 if (s == NULL
fd361982 184 || !bfd_set_section_alignment (s, bed->s->log_file_align))
0a1b45a2 185 return false;
6de2ae4a 186 htab->sgotplt = s;
252b5132
RH
187 }
188
64e77c6d
L
189 /* The first bit of the global offset table is the header. */
190 s->size += bed->got_header_size;
191
2517a57f
AM
192 if (bed->want_got_sym)
193 {
194 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
195 (or .got.plt) section. We don't do this in the linker script
196 because we don't want to define the symbol if we are not creating
197 a global offset table. */
6de2ae4a
L
198 h = _bfd_elf_define_linkage_sym (abfd, info, s,
199 "_GLOBAL_OFFSET_TABLE_");
2517a57f 200 elf_hash_table (info)->hgot = h;
d98685ac 201 if (h == NULL)
0a1b45a2 202 return false;
2517a57f 203 }
252b5132 204
0a1b45a2 205 return true;
252b5132
RH
206}
207\f
7e9f0867 208/* Create a strtab to hold the dynamic symbol names. */
0a1b45a2 209static bool
7e9f0867
AM
210_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
211{
212 struct elf_link_hash_table *hash_table;
213
214 hash_table = elf_hash_table (info);
215 if (hash_table->dynobj == NULL)
6cd255ca
L
216 {
217 /* We may not set dynobj, an input file holding linker created
218 dynamic sections to abfd, which may be a dynamic object with
219 its own dynamic sections. We need to find a normal input file
220 to hold linker created sections if possible. */
221 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
222 {
223 bfd *ibfd;
57963c05 224 asection *s;
6cd255ca 225 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
6645479e 226 if ((ibfd->flags
57963c05
AM
227 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
228 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
4de5434b 229 && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
57963c05
AM
230 && !((s = ibfd->sections) != NULL
231 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
6cd255ca
L
232 {
233 abfd = ibfd;
234 break;
235 }
236 }
237 hash_table->dynobj = abfd;
238 }
7e9f0867
AM
239
240 if (hash_table->dynstr == NULL)
241 {
242 hash_table->dynstr = _bfd_elf_strtab_init ();
243 if (hash_table->dynstr == NULL)
0a1b45a2 244 return false;
7e9f0867 245 }
0a1b45a2 246 return true;
7e9f0867
AM
247}
248
45d6a902
AM
249/* Create some sections which will be filled in with dynamic linking
250 information. ABFD is an input file which requires dynamic sections
251 to be created. The dynamic sections take up virtual memory space
252 when the final executable is run, so we need to create them before
253 addresses are assigned to the output sections. We work out the
254 actual contents and size of these sections later. */
252b5132 255
0a1b45a2 256bool
268b6b39 257_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
252b5132 258{
45d6a902 259 flagword flags;
91d6fa6a 260 asection *s;
9c5bfbb7 261 const struct elf_backend_data *bed;
9637f6ef 262 struct elf_link_hash_entry *h;
252b5132 263
0eddce27 264 if (! is_elf_hash_table (info->hash))
0a1b45a2 265 return false;
45d6a902
AM
266
267 if (elf_hash_table (info)->dynamic_sections_created)
0a1b45a2 268 return true;
45d6a902 269
7e9f0867 270 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
0a1b45a2 271 return false;
45d6a902 272
7e9f0867 273 abfd = elf_hash_table (info)->dynobj;
e5a52504
MM
274 bed = get_elf_backend_data (abfd);
275
276 flags = bed->dynamic_sec_flags;
45d6a902
AM
277
278 /* A dynamically linked executable has a .interp section, but a
279 shared library does not. */
9b8b325a 280 if (bfd_link_executable (info) && !info->nointerp)
252b5132 281 {
14b2f831
AM
282 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
283 flags | SEC_READONLY);
3496cb2a 284 if (s == NULL)
0a1b45a2 285 return false;
45d6a902 286 }
bb0deeff 287
45d6a902
AM
288 /* Create sections to hold version informations. These are removed
289 if they are not needed. */
14b2f831
AM
290 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
291 flags | SEC_READONLY);
45d6a902 292 if (s == NULL
fd361982 293 || !bfd_set_section_alignment (s, bed->s->log_file_align))
0a1b45a2 294 return false;
45d6a902 295
14b2f831
AM
296 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
297 flags | SEC_READONLY);
45d6a902 298 if (s == NULL
fd361982 299 || !bfd_set_section_alignment (s, 1))
0a1b45a2 300 return false;
45d6a902 301
14b2f831
AM
302 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
303 flags | SEC_READONLY);
45d6a902 304 if (s == NULL
fd361982 305 || !bfd_set_section_alignment (s, bed->s->log_file_align))
0a1b45a2 306 return false;
45d6a902 307
14b2f831
AM
308 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
309 flags | SEC_READONLY);
45d6a902 310 if (s == NULL
fd361982 311 || !bfd_set_section_alignment (s, bed->s->log_file_align))
0a1b45a2 312 return false;
cae1fbbb 313 elf_hash_table (info)->dynsym = s;
45d6a902 314
14b2f831
AM
315 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
316 flags | SEC_READONLY);
3496cb2a 317 if (s == NULL)
0a1b45a2 318 return false;
45d6a902 319
14b2f831 320 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
45d6a902 321 if (s == NULL
fd361982 322 || !bfd_set_section_alignment (s, bed->s->log_file_align))
0a1b45a2 323 return false;
45d6a902
AM
324
325 /* The special symbol _DYNAMIC is always set to the start of the
77cfaee6
AM
326 .dynamic section. We could set _DYNAMIC in a linker script, but we
327 only want to define it if we are, in fact, creating a .dynamic
328 section. We don't want to define it if there is no .dynamic
329 section, since on some ELF platforms the start up code examines it
330 to decide how to initialize the process. */
9637f6ef
L
331 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
332 elf_hash_table (info)->hdynamic = h;
333 if (h == NULL)
0a1b45a2 334 return false;
45d6a902 335
fdc90cb4
JJ
336 if (info->emit_hash)
337 {
14b2f831
AM
338 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
339 flags | SEC_READONLY);
fdc90cb4 340 if (s == NULL
fd361982 341 || !bfd_set_section_alignment (s, bed->s->log_file_align))
0a1b45a2 342 return false;
fdc90cb4
JJ
343 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
344 }
345
f16a9783 346 if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL)
fdc90cb4 347 {
14b2f831
AM
348 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
349 flags | SEC_READONLY);
fdc90cb4 350 if (s == NULL
fd361982 351 || !bfd_set_section_alignment (s, bed->s->log_file_align))
0a1b45a2 352 return false;
fdc90cb4
JJ
353 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
354 4 32-bit words followed by variable count of 64-bit words, then
355 variable count of 32-bit words. */
356 if (bed->s->arch_size == 64)
357 elf_section_data (s)->this_hdr.sh_entsize = 0;
358 else
359 elf_section_data (s)->this_hdr.sh_entsize = 4;
360 }
45d6a902
AM
361
362 /* Let the backend create the rest of the sections. This lets the
363 backend set the right flags. The backend will normally create
364 the .got and .plt sections. */
894891db
NC
365 if (bed->elf_backend_create_dynamic_sections == NULL
366 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
0a1b45a2 367 return false;
45d6a902 368
0a1b45a2 369 elf_hash_table (info)->dynamic_sections_created = true;
45d6a902 370
0a1b45a2 371 return true;
45d6a902
AM
372}
373
374/* Create dynamic sections when linking against a dynamic object. */
375
0a1b45a2 376bool
268b6b39 377_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
45d6a902
AM
378{
379 flagword flags, pltflags;
7325306f 380 struct elf_link_hash_entry *h;
45d6a902 381 asection *s;
9c5bfbb7 382 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 383 struct elf_link_hash_table *htab = elf_hash_table (info);
45d6a902 384
252b5132
RH
385 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
386 .rel[a].bss sections. */
e5a52504 387 flags = bed->dynamic_sec_flags;
252b5132
RH
388
389 pltflags = flags;
252b5132 390 if (bed->plt_not_loaded)
6df4d94c
MM
391 /* We do not clear SEC_ALLOC here because we still want the OS to
392 allocate space for the section; it's just that there's nothing
393 to read in from the object file. */
5d1634d7 394 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
6df4d94c
MM
395 else
396 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
252b5132
RH
397 if (bed->plt_readonly)
398 pltflags |= SEC_READONLY;
399
14b2f831 400 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
252b5132 401 if (s == NULL
fd361982 402 || !bfd_set_section_alignment (s, bed->plt_alignment))
0a1b45a2 403 return false;
6de2ae4a 404 htab->splt = s;
252b5132 405
d98685ac
AM
406 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
407 .plt section. */
7325306f
RS
408 if (bed->want_plt_sym)
409 {
410 h = _bfd_elf_define_linkage_sym (abfd, info, s,
411 "_PROCEDURE_LINKAGE_TABLE_");
412 elf_hash_table (info)->hplt = h;
413 if (h == NULL)
0a1b45a2 414 return false;
7325306f 415 }
252b5132 416
14b2f831
AM
417 s = bfd_make_section_anyway_with_flags (abfd,
418 (bed->rela_plts_and_copies_p
419 ? ".rela.plt" : ".rel.plt"),
420 flags | SEC_READONLY);
252b5132 421 if (s == NULL
fd361982 422 || !bfd_set_section_alignment (s, bed->s->log_file_align))
0a1b45a2 423 return false;
6de2ae4a 424 htab->srelplt = s;
252b5132
RH
425
426 if (! _bfd_elf_create_got_section (abfd, info))
0a1b45a2 427 return false;
252b5132 428
3018b441
RH
429 if (bed->want_dynbss)
430 {
431 /* The .dynbss section is a place to put symbols which are defined
432 by dynamic objects, are referenced by regular objects, and are
433 not functions. We must allocate space for them in the process
434 image and use a R_*_COPY reloc to tell the dynamic linker to
435 initialize them at run time. The linker script puts the .dynbss
436 section into the .bss section of the final image. */
14b2f831 437 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
afbf7e8e 438 SEC_ALLOC | SEC_LINKER_CREATED);
3496cb2a 439 if (s == NULL)
0a1b45a2 440 return false;
9d19e4fd 441 htab->sdynbss = s;
252b5132 442
5474d94f
AM
443 if (bed->want_dynrelro)
444 {
445 /* Similarly, but for symbols that were originally in read-only
afbf7e8e
AM
446 sections. This section doesn't really need to have contents,
447 but make it like other .data.rel.ro sections. */
5474d94f 448 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
afbf7e8e 449 flags);
5474d94f 450 if (s == NULL)
0a1b45a2 451 return false;
5474d94f
AM
452 htab->sdynrelro = s;
453 }
454
3018b441 455 /* The .rel[a].bss section holds copy relocs. This section is not
77cfaee6
AM
456 normally needed. We need to create it here, though, so that the
457 linker will map it to an output section. We can't just create it
458 only if we need it, because we will not know whether we need it
459 until we have seen all the input files, and the first time the
460 main linker code calls BFD after examining all the input files
461 (size_dynamic_sections) the input sections have already been
462 mapped to the output sections. If the section turns out not to
463 be needed, we can discard it later. We will never need this
464 section when generating a shared object, since they do not use
465 copy relocs. */
9d19e4fd 466 if (bfd_link_executable (info))
3018b441 467 {
14b2f831
AM
468 s = bfd_make_section_anyway_with_flags (abfd,
469 (bed->rela_plts_and_copies_p
470 ? ".rela.bss" : ".rel.bss"),
471 flags | SEC_READONLY);
3018b441 472 if (s == NULL
fd361982 473 || !bfd_set_section_alignment (s, bed->s->log_file_align))
0a1b45a2 474 return false;
9d19e4fd 475 htab->srelbss = s;
5474d94f
AM
476
477 if (bed->want_dynrelro)
478 {
479 s = (bfd_make_section_anyway_with_flags
480 (abfd, (bed->rela_plts_and_copies_p
481 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
482 flags | SEC_READONLY));
483 if (s == NULL
fd361982 484 || !bfd_set_section_alignment (s, bed->s->log_file_align))
0a1b45a2 485 return false;
5474d94f
AM
486 htab->sreldynrelro = s;
487 }
3018b441 488 }
252b5132
RH
489 }
490
0a1b45a2 491 return true;
252b5132
RH
492}
493\f
252b5132
RH
494/* Record a new dynamic symbol. We record the dynamic symbols as we
495 read the input files, since we need to have a list of all of them
496 before we can determine the final sizes of the output sections.
497 Note that we may actually call this function even though we are not
498 going to output any dynamic symbols; in some cases we know that a
499 symbol should be in the dynamic symbol table, but only if there is
500 one. */
501
0a1b45a2 502bool
c152c796
AM
503bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
504 struct elf_link_hash_entry *h)
252b5132
RH
505{
506 if (h->dynindx == -1)
507 {
2b0f7ef9 508 struct elf_strtab_hash *dynstr;
68b6ddd0 509 char *p;
252b5132 510 const char *name;
ef53be89 511 size_t indx;
252b5132 512
a896df97
AM
513 if (h->root.type == bfd_link_hash_defined
514 || h->root.type == bfd_link_hash_defweak)
515 {
516 /* An IR symbol should not be made dynamic. */
517 if (h->root.u.def.section != NULL
518 && h->root.u.def.section->owner != NULL
519 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)
0a1b45a2 520 return true;
a896df97
AM
521 }
522
7a13edea
NC
523 /* XXX: The ABI draft says the linker must turn hidden and
524 internal symbols into STB_LOCAL symbols when producing the
525 DSO. However, if ld.so honors st_other in the dynamic table,
526 this would not be necessary. */
527 switch (ELF_ST_VISIBILITY (h->other))
528 {
529 case STV_INTERNAL:
530 case STV_HIDDEN:
9d6eee78
L
531 if (h->root.type != bfd_link_hash_undefined
532 && h->root.type != bfd_link_hash_undefweak)
38048eb9 533 {
f5385ebf 534 h->forced_local = 1;
c8bad65e
AM
535 if (!elf_hash_table (info)->is_relocatable_executable
536 || ((h->root.type == bfd_link_hash_defined
537 || h->root.type == bfd_link_hash_defweak)
c4566785 538 && h->root.u.def.section->owner != NULL
c8bad65e
AM
539 && h->root.u.def.section->owner->no_export)
540 || (h->root.type == bfd_link_hash_common
c4566785 541 && h->root.u.c.p->section->owner != NULL
c8bad65e 542 && h->root.u.c.p->section->owner->no_export))
0a1b45a2 543 return true;
7a13edea 544 }
0444bdd4 545
7a13edea
NC
546 default:
547 break;
548 }
549
252b5132
RH
550 h->dynindx = elf_hash_table (info)->dynsymcount;
551 ++elf_hash_table (info)->dynsymcount;
552
553 dynstr = elf_hash_table (info)->dynstr;
554 if (dynstr == NULL)
555 {
556 /* Create a strtab to hold the dynamic symbol names. */
2b0f7ef9 557 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
252b5132 558 if (dynstr == NULL)
0a1b45a2 559 return false;
252b5132
RH
560 }
561
562 /* We don't put any version information in the dynamic string
aad5d350 563 table. */
252b5132
RH
564 name = h->root.root.string;
565 p = strchr (name, ELF_VER_CHR);
68b6ddd0
AM
566 if (p != NULL)
567 /* We know that the p points into writable memory. In fact,
568 there are only a few symbols that have read-only names, being
569 those like _GLOBAL_OFFSET_TABLE_ that are created specially
570 by the backends. Most symbols will have names pointing into
571 an ELF string table read from a file, or to objalloc memory. */
572 *p = 0;
573
574 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
575
576 if (p != NULL)
577 *p = ELF_VER_CHR;
252b5132 578
ef53be89 579 if (indx == (size_t) -1)
0a1b45a2 580 return false;
252b5132
RH
581 h->dynstr_index = indx;
582 }
583
0a1b45a2 584 return true;
252b5132 585}
45d6a902 586\f
55255dae
L
587/* Mark a symbol dynamic. */
588
28caa186 589static void
55255dae 590bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
40b36307
L
591 struct elf_link_hash_entry *h,
592 Elf_Internal_Sym *sym)
55255dae 593{
40b36307 594 struct bfd_elf_dynamic_list *d = info->dynamic_list;
55255dae 595
40b36307 596 /* It may be called more than once on the same H. */
0e1862bb 597 if(h->dynamic || bfd_link_relocatable (info))
55255dae
L
598 return;
599
40b36307
L
600 if ((info->dynamic_data
601 && (h->type == STT_OBJECT
b8871f35 602 || h->type == STT_COMMON
40b36307 603 || (sym != NULL
b8871f35
L
604 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
605 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
a0c8462f 606 || (d != NULL
73ec947d 607 && h->non_elf
40b36307 608 && (*d->match) (&d->head, NULL, h->root.root.string)))
416c34d6
L
609 {
610 h->dynamic = 1;
611 /* NB: If a symbol is made dynamic by --dynamic-list, it has
612 non-IR reference. */
613 h->root.non_ir_ref_dynamic = 1;
614 }
55255dae
L
615}
616
45d6a902
AM
617/* Record an assignment to a symbol made by a linker script. We need
618 this in case some dynamic object refers to this symbol. */
619
0a1b45a2 620bool
fe21a8fc
L
621bfd_elf_record_link_assignment (bfd *output_bfd,
622 struct bfd_link_info *info,
268b6b39 623 const char *name,
0a1b45a2
AM
624 bool provide,
625 bool hidden)
45d6a902 626{
00cbee0a 627 struct elf_link_hash_entry *h, *hv;
4ea42fb7 628 struct elf_link_hash_table *htab;
00cbee0a 629 const struct elf_backend_data *bed;
45d6a902 630
0eddce27 631 if (!is_elf_hash_table (info->hash))
0a1b45a2 632 return true;
45d6a902 633
4ea42fb7 634 htab = elf_hash_table (info);
0a1b45a2 635 h = elf_link_hash_lookup (htab, name, !provide, true, false);
45d6a902 636 if (h == NULL)
4ea42fb7 637 return provide;
45d6a902 638
8e2a4f11
AM
639 if (h->root.type == bfd_link_hash_warning)
640 h = (struct elf_link_hash_entry *) h->root.u.i.link;
641
0f550b3d
L
642 if (h->versioned == unknown)
643 {
644 /* Set versioned if symbol version is unknown. */
645 char *version = strrchr (name, ELF_VER_CHR);
646 if (version)
647 {
648 if (version > name && version[-1] != ELF_VER_CHR)
649 h->versioned = versioned_hidden;
650 else
651 h->versioned = versioned;
652 }
653 }
654
73ec947d
AM
655 /* Symbols defined in a linker script but not referenced anywhere
656 else will have non_elf set. */
657 if (h->non_elf)
658 {
659 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
660 h->non_elf = 0;
661 }
662
00cbee0a 663 switch (h->root.type)
77cfaee6 664 {
00cbee0a
L
665 case bfd_link_hash_defined:
666 case bfd_link_hash_defweak:
667 case bfd_link_hash_common:
668 break;
669 case bfd_link_hash_undefweak:
670 case bfd_link_hash_undefined:
671 /* Since we're defining the symbol, don't let it seem to have not
672 been defined. record_dynamic_symbol and size_dynamic_sections
673 may depend on this. */
4ea42fb7 674 h->root.type = bfd_link_hash_new;
77cfaee6
AM
675 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
676 bfd_link_repair_undef_list (&htab->root);
00cbee0a
L
677 break;
678 case bfd_link_hash_new:
00cbee0a
L
679 break;
680 case bfd_link_hash_indirect:
681 /* We had a versioned symbol in a dynamic library. We make the
a0c8462f 682 the versioned symbol point to this one. */
00cbee0a
L
683 bed = get_elf_backend_data (output_bfd);
684 hv = h;
685 while (hv->root.type == bfd_link_hash_indirect
686 || hv->root.type == bfd_link_hash_warning)
687 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
688 /* We don't need to update h->root.u since linker will set them
689 later. */
690 h->root.type = bfd_link_hash_undefined;
691 hv->root.type = bfd_link_hash_indirect;
692 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
693 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
694 break;
8e2a4f11
AM
695 default:
696 BFD_FAIL ();
0a1b45a2 697 return false;
55255dae 698 }
45d6a902
AM
699
700 /* If this symbol is being provided by the linker script, and it is
701 currently defined by a dynamic object, but not by a regular
702 object, then mark it as undefined so that the generic linker will
703 force the correct value. */
704 if (provide
f5385ebf
AM
705 && h->def_dynamic
706 && !h->def_regular)
45d6a902
AM
707 h->root.type = bfd_link_hash_undefined;
708
48e30f52
L
709 /* If this symbol is currently defined by a dynamic object, but not
710 by a regular object, then clear out any version information because
711 the symbol will not be associated with the dynamic object any
712 more. */
713 if (h->def_dynamic && !h->def_regular)
b531344c
MR
714 h->verinfo.verdef = NULL;
715
716 /* Make sure this symbol is not garbage collected. */
717 h->mark = 1;
45d6a902 718
f5385ebf 719 h->def_regular = 1;
45d6a902 720
eb8476a6 721 if (hidden)
fe21a8fc 722 {
91d6fa6a 723 bed = get_elf_backend_data (output_bfd);
b8297068
AM
724 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
725 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
0a1b45a2 726 (*bed->elf_backend_hide_symbol) (info, h, true);
fe21a8fc
L
727 }
728
6fa3860b
PB
729 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
730 and executables. */
0e1862bb 731 if (!bfd_link_relocatable (info)
6fa3860b
PB
732 && h->dynindx != -1
733 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
734 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
735 h->forced_local = 1;
736
f5385ebf
AM
737 if ((h->def_dynamic
738 || h->ref_dynamic
6b3b0ab8
L
739 || bfd_link_dll (info)
740 || elf_hash_table (info)->is_relocatable_executable)
34a87bb0 741 && !h->forced_local
45d6a902
AM
742 && h->dynindx == -1)
743 {
c152c796 744 if (! bfd_elf_link_record_dynamic_symbol (info, h))
0a1b45a2 745 return false;
45d6a902
AM
746
747 /* If this is a weak defined symbol, and we know a corresponding
748 real symbol from the same dynamic object, make sure the real
749 symbol is also made into a dynamic symbol. */
60d67dc8 750 if (h->is_weakalias)
45d6a902 751 {
60d67dc8
AM
752 struct elf_link_hash_entry *def = weakdef (h);
753
754 if (def->dynindx == -1
755 && !bfd_elf_link_record_dynamic_symbol (info, def))
0a1b45a2 756 return false;
45d6a902
AM
757 }
758 }
759
0a1b45a2 760 return true;
45d6a902 761}
42751cf3 762
8c58d23b
AM
763/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
764 success, and 2 on a failure caused by attempting to record a symbol
765 in a discarded section, eg. a discarded link-once section symbol. */
766
767int
c152c796
AM
768bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
769 bfd *input_bfd,
770 long input_indx)
8c58d23b 771{
986f0783 772 size_t amt;
8c58d23b
AM
773 struct elf_link_local_dynamic_entry *entry;
774 struct elf_link_hash_table *eht;
775 struct elf_strtab_hash *dynstr;
ef53be89 776 size_t dynstr_index;
8c58d23b
AM
777 char *name;
778 Elf_External_Sym_Shndx eshndx;
779 char esym[sizeof (Elf64_External_Sym)];
780
0eddce27 781 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
782 return 0;
783
784 /* See if the entry exists already. */
785 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
786 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
787 return 1;
788
789 amt = sizeof (*entry);
a50b1753 790 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
8c58d23b
AM
791 if (entry == NULL)
792 return 0;
793
794 /* Go find the symbol, so that we can find it's name. */
795 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 796 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
797 {
798 bfd_release (input_bfd, entry);
799 return 0;
800 }
801
802 if (entry->isym.st_shndx != SHN_UNDEF
4fbb74a6 803 && entry->isym.st_shndx < SHN_LORESERVE)
8c58d23b
AM
804 {
805 asection *s;
806
807 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
808 if (s == NULL || bfd_is_abs_section (s->output_section))
809 {
810 /* We can still bfd_release here as nothing has done another
811 bfd_alloc. We can't do this later in this function. */
812 bfd_release (input_bfd, entry);
813 return 2;
814 }
815 }
816
817 name = (bfd_elf_string_from_elf_section
818 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
819 entry->isym.st_name));
820
821 dynstr = elf_hash_table (info)->dynstr;
822 if (dynstr == NULL)
823 {
824 /* Create a strtab to hold the dynamic symbol names. */
825 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
826 if (dynstr == NULL)
827 return 0;
828 }
829
0a1b45a2 830 dynstr_index = _bfd_elf_strtab_add (dynstr, name, false);
ef53be89 831 if (dynstr_index == (size_t) -1)
8c58d23b
AM
832 return 0;
833 entry->isym.st_name = dynstr_index;
834
835 eht = elf_hash_table (info);
836
837 entry->next = eht->dynlocal;
838 eht->dynlocal = entry;
839 entry->input_bfd = input_bfd;
840 entry->input_indx = input_indx;
841 eht->dynsymcount++;
842
843 /* Whatever binding the symbol had before, it's now local. */
844 entry->isym.st_info
845 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
846
847 /* The dynindx will be set at the end of size_dynamic_sections. */
848
849 return 1;
850}
851
30b30c21 852/* Return the dynindex of a local dynamic symbol. */
42751cf3 853
30b30c21 854long
268b6b39
AM
855_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
856 bfd *input_bfd,
857 long input_indx)
30b30c21
RH
858{
859 struct elf_link_local_dynamic_entry *e;
860
861 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
862 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
863 return e->dynindx;
864 return -1;
865}
866
867/* This function is used to renumber the dynamic symbols, if some of
868 them are removed because they are marked as local. This is called
869 via elf_link_hash_traverse. */
870
0a1b45a2 871static bool
268b6b39
AM
872elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
873 void *data)
42751cf3 874{
a50b1753 875 size_t *count = (size_t *) data;
30b30c21 876
6fa3860b 877 if (h->forced_local)
0a1b45a2 878 return true;
6fa3860b
PB
879
880 if (h->dynindx != -1)
881 h->dynindx = ++(*count);
882
0a1b45a2 883 return true;
6fa3860b
PB
884}
885
886
887/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
888 STB_LOCAL binding. */
889
0a1b45a2 890static bool
6fa3860b
PB
891elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
892 void *data)
893{
a50b1753 894 size_t *count = (size_t *) data;
6fa3860b 895
6fa3860b 896 if (!h->forced_local)
0a1b45a2 897 return true;
6fa3860b 898
42751cf3 899 if (h->dynindx != -1)
30b30c21
RH
900 h->dynindx = ++(*count);
901
0a1b45a2 902 return true;
42751cf3 903}
30b30c21 904
aee6f5b4
AO
905/* Return true if the dynamic symbol for a given section should be
906 omitted when creating a shared library. */
0a1b45a2 907bool
d00dd7dc
AM
908_bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
909 struct bfd_link_info *info,
910 asection *p)
aee6f5b4 911{
74541ad4 912 struct elf_link_hash_table *htab;
ca55926c 913 asection *ip;
74541ad4 914
aee6f5b4
AO
915 switch (elf_section_data (p)->this_hdr.sh_type)
916 {
917 case SHT_PROGBITS:
918 case SHT_NOBITS:
919 /* If sh_type is yet undecided, assume it could be
920 SHT_PROGBITS/SHT_NOBITS. */
921 case SHT_NULL:
74541ad4 922 htab = elf_hash_table (info);
74541ad4
AM
923 if (htab->text_index_section != NULL)
924 return p != htab->text_index_section && p != htab->data_index_section;
925
ca55926c 926 return (htab->dynobj != NULL
3d4d4302 927 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
ca55926c 928 && ip->output_section == p);
aee6f5b4
AO
929
930 /* There shouldn't be section relative relocations
931 against any other section. */
932 default:
0a1b45a2 933 return true;
aee6f5b4
AO
934 }
935}
936
0a1b45a2 937bool
d00dd7dc
AM
938_bfd_elf_omit_section_dynsym_all
939 (bfd *output_bfd ATTRIBUTE_UNUSED,
940 struct bfd_link_info *info ATTRIBUTE_UNUSED,
941 asection *p ATTRIBUTE_UNUSED)
942{
0a1b45a2 943 return true;
d00dd7dc
AM
944}
945
062e2358 946/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
947 symbol for each output section, which come first. Next come symbols
948 which have been forced to local binding. Then all of the back-end
949 allocated local dynamic syms, followed by the rest of the global
63f452a8
AM
950 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
951 (This prevents the early call before elf_backend_init_index_section
952 and strip_excluded_output_sections setting dynindx for sections
953 that are stripped.) */
30b30c21 954
554220db
AM
955static unsigned long
956_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
957 struct bfd_link_info *info,
958 unsigned long *section_sym_count)
30b30c21
RH
959{
960 unsigned long dynsymcount = 0;
0a1b45a2 961 bool do_sec = section_sym_count != NULL;
30b30c21 962
0e1862bb
L
963 if (bfd_link_pic (info)
964 || elf_hash_table (info)->is_relocatable_executable)
30b30c21 965 {
aee6f5b4 966 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
967 asection *p;
968 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 969 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4 970 && (p->flags & SEC_ALLOC) != 0
7f923b7f 971 && elf_hash_table (info)->dynamic_relocs
aee6f5b4 972 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
63f452a8
AM
973 {
974 ++dynsymcount;
975 if (do_sec)
976 elf_section_data (p)->dynindx = dynsymcount;
977 }
978 else if (do_sec)
74541ad4 979 elf_section_data (p)->dynindx = 0;
30b30c21 980 }
63f452a8
AM
981 if (do_sec)
982 *section_sym_count = dynsymcount;
30b30c21 983
6fa3860b
PB
984 elf_link_hash_traverse (elf_hash_table (info),
985 elf_link_renumber_local_hash_table_dynsyms,
986 &dynsymcount);
987
30b30c21
RH
988 if (elf_hash_table (info)->dynlocal)
989 {
990 struct elf_link_local_dynamic_entry *p;
991 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
992 p->dynindx = ++dynsymcount;
993 }
90ac2420 994 elf_hash_table (info)->local_dynsymcount = dynsymcount;
30b30c21
RH
995
996 elf_link_hash_traverse (elf_hash_table (info),
997 elf_link_renumber_hash_table_dynsyms,
998 &dynsymcount);
999
d5486c43
L
1000 /* There is an unused NULL entry at the head of the table which we
1001 must account for in our count even if the table is empty since it
1002 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
1003 .dynamic section. */
1004 dynsymcount++;
30b30c21 1005
ccabcbe5
AM
1006 elf_hash_table (info)->dynsymcount = dynsymcount;
1007 return dynsymcount;
30b30c21 1008}
252b5132 1009
54ac0771
L
1010/* Merge st_other field. */
1011
1012static void
1013elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
5160d0f3 1014 unsigned int st_other, asection *sec,
0a1b45a2 1015 bool definition, bool dynamic)
54ac0771
L
1016{
1017 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1018
1019 /* If st_other has a processor-specific meaning, specific
cd3416da 1020 code might be needed here. */
54ac0771 1021 if (bed->elf_backend_merge_symbol_attribute)
5160d0f3 1022 (*bed->elf_backend_merge_symbol_attribute) (h, st_other, definition,
54ac0771
L
1023 dynamic);
1024
cd3416da 1025 if (!dynamic)
54ac0771 1026 {
5160d0f3 1027 unsigned symvis = ELF_ST_VISIBILITY (st_other);
cd3416da 1028 unsigned hvis = ELF_ST_VISIBILITY (h->other);
54ac0771 1029
cd3416da
AM
1030 /* Keep the most constraining visibility. Leave the remainder
1031 of the st_other field to elf_backend_merge_symbol_attribute. */
1032 if (symvis - 1 < hvis - 1)
1033 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
54ac0771 1034 }
b8417128 1035 else if (definition
5160d0f3 1036 && ELF_ST_VISIBILITY (st_other) != STV_DEFAULT
b8417128 1037 && (sec->flags & SEC_READONLY) == 0)
6cabe1ea 1038 h->protected_def = 1;
54ac0771
L
1039}
1040
4f3fedcf
AM
1041/* This function is called when we want to merge a new symbol with an
1042 existing symbol. It handles the various cases which arise when we
1043 find a definition in a dynamic object, or when there is already a
1044 definition in a dynamic object. The new symbol is described by
1045 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1046 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1047 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1048 of an old common symbol. We set OVERRIDE if the old symbol is
1049 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1050 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1051 to change. By OK to change, we mean that we shouldn't warn if the
1052 type or size does change. */
45d6a902 1053
0a1b45a2 1054static bool
268b6b39
AM
1055_bfd_elf_merge_symbol (bfd *abfd,
1056 struct bfd_link_info *info,
1057 const char *name,
1058 Elf_Internal_Sym *sym,
1059 asection **psec,
1060 bfd_vma *pvalue,
4f3fedcf
AM
1061 struct elf_link_hash_entry **sym_hash,
1062 bfd **poldbfd,
0a1b45a2 1063 bool *pold_weak,
af44c138 1064 unsigned int *pold_alignment,
0a1b45a2 1065 bool *skip,
7ba11550 1066 bfd **override,
0a1b45a2
AM
1067 bool *type_change_ok,
1068 bool *size_change_ok,
1069 bool *matched)
252b5132 1070{
7479dfd4 1071 asection *sec, *oldsec;
45d6a902 1072 struct elf_link_hash_entry *h;
90c984fc 1073 struct elf_link_hash_entry *hi;
45d6a902
AM
1074 struct elf_link_hash_entry *flip;
1075 int bind;
1076 bfd *oldbfd;
0a1b45a2
AM
1077 bool newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1078 bool newweak, oldweak, newfunc, oldfunc;
a4d8e49b 1079 const struct elf_backend_data *bed;
6e33951e 1080 char *new_version;
0a1b45a2 1081 bool default_sym = *matched;
45d6a902 1082
0a1b45a2 1083 *skip = false;
7ba11550 1084 *override = NULL;
45d6a902
AM
1085
1086 sec = *psec;
1087 bind = ELF_ST_BIND (sym->st_info);
1088
1089 if (! bfd_is_und_section (sec))
0a1b45a2 1090 h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
45d6a902
AM
1091 else
1092 h = ((struct elf_link_hash_entry *)
0a1b45a2 1093 bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
45d6a902 1094 if (h == NULL)
0a1b45a2 1095 return false;
45d6a902 1096 *sym_hash = h;
252b5132 1097
88ba32a0
L
1098 bed = get_elf_backend_data (abfd);
1099
6e33951e 1100 /* NEW_VERSION is the symbol version of the new symbol. */
422f1182 1101 if (h->versioned != unversioned)
6e33951e 1102 {
422f1182
L
1103 /* Symbol version is unknown or versioned. */
1104 new_version = strrchr (name, ELF_VER_CHR);
1105 if (new_version)
1106 {
1107 if (h->versioned == unknown)
1108 {
1109 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1110 h->versioned = versioned_hidden;
1111 else
1112 h->versioned = versioned;
1113 }
1114 new_version += 1;
1115 if (new_version[0] == '\0')
1116 new_version = NULL;
1117 }
1118 else
1119 h->versioned = unversioned;
6e33951e 1120 }
422f1182
L
1121 else
1122 new_version = NULL;
6e33951e 1123
90c984fc
L
1124 /* For merging, we only care about real symbols. But we need to make
1125 sure that indirect symbol dynamic flags are updated. */
1126 hi = h;
45d6a902
AM
1127 while (h->root.type == bfd_link_hash_indirect
1128 || h->root.type == bfd_link_hash_warning)
1129 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1130
6e33951e
L
1131 if (!*matched)
1132 {
1133 if (hi == h || h->root.type == bfd_link_hash_new)
0a1b45a2 1134 *matched = true;
6e33951e
L
1135 else
1136 {
ae7683d2 1137 /* OLD_HIDDEN is true if the existing symbol is only visible
6e33951e 1138 to the symbol with the same symbol version. NEW_HIDDEN is
ae7683d2 1139 true if the new symbol is only visible to the symbol with
6e33951e 1140 the same symbol version. */
0a1b45a2
AM
1141 bool old_hidden = h->versioned == versioned_hidden;
1142 bool new_hidden = hi->versioned == versioned_hidden;
6e33951e
L
1143 if (!old_hidden && !new_hidden)
1144 /* The new symbol matches the existing symbol if both
1145 aren't hidden. */
0a1b45a2 1146 *matched = true;
6e33951e
L
1147 else
1148 {
1149 /* OLD_VERSION is the symbol version of the existing
1150 symbol. */
422f1182
L
1151 char *old_version;
1152
1153 if (h->versioned >= versioned)
1154 old_version = strrchr (h->root.root.string,
1155 ELF_VER_CHR) + 1;
1156 else
1157 old_version = NULL;
6e33951e
L
1158
1159 /* The new symbol matches the existing symbol if they
1160 have the same symbol version. */
1161 *matched = (old_version == new_version
1162 || (old_version != NULL
1163 && new_version != NULL
1164 && strcmp (old_version, new_version) == 0));
1165 }
1166 }
1167 }
1168
934bce08
AM
1169 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1170 existing symbol. */
1171
1172 oldbfd = NULL;
1173 oldsec = NULL;
1174 switch (h->root.type)
1175 {
1176 default:
1177 break;
1178
1179 case bfd_link_hash_undefined:
1180 case bfd_link_hash_undefweak:
1181 oldbfd = h->root.u.undef.abfd;
1182 break;
1183
1184 case bfd_link_hash_defined:
1185 case bfd_link_hash_defweak:
1186 oldbfd = h->root.u.def.section->owner;
1187 oldsec = h->root.u.def.section;
1188 break;
1189
1190 case bfd_link_hash_common:
1191 oldbfd = h->root.u.c.p->section->owner;
1192 oldsec = h->root.u.c.p->section;
1193 if (pold_alignment)
1194 *pold_alignment = h->root.u.c.p->alignment_power;
1195 break;
1196 }
1197 if (poldbfd && *poldbfd == NULL)
1198 *poldbfd = oldbfd;
1199
1200 /* Differentiate strong and weak symbols. */
1201 newweak = bind == STB_WEAK;
1202 oldweak = (h->root.type == bfd_link_hash_defweak
1203 || h->root.type == bfd_link_hash_undefweak);
1204 if (pold_weak)
1205 *pold_weak = oldweak;
1206
40b36307 1207 /* We have to check it for every instance since the first few may be
ee659f1f 1208 references and not all compilers emit symbol type for undefined
40b36307
L
1209 symbols. */
1210 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1211
ee659f1f
AM
1212 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1213 respectively, is from a dynamic object. */
1214
1215 newdyn = (abfd->flags & DYNAMIC) != 0;
1216
1217 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1218 syms and defined syms in dynamic libraries respectively.
1219 ref_dynamic on the other hand can be set for a symbol defined in
1220 a dynamic library, and def_dynamic may not be set; When the
1221 definition in a dynamic lib is overridden by a definition in the
1222 executable use of the symbol in the dynamic lib becomes a
1223 reference to the executable symbol. */
1224 if (newdyn)
1225 {
1226 if (bfd_is_und_section (sec))
1227 {
1228 if (bind != STB_WEAK)
1229 {
1230 h->ref_dynamic_nonweak = 1;
1231 hi->ref_dynamic_nonweak = 1;
1232 }
1233 }
1234 else
1235 {
6e33951e
L
1236 /* Update the existing symbol only if they match. */
1237 if (*matched)
1238 h->dynamic_def = 1;
ee659f1f
AM
1239 hi->dynamic_def = 1;
1240 }
1241 }
1242
45d6a902
AM
1243 /* If we just created the symbol, mark it as being an ELF symbol.
1244 Other than that, there is nothing to do--there is no merge issue
1245 with a newly defined symbol--so we just return. */
1246
1247 if (h->root.type == bfd_link_hash_new)
252b5132 1248 {
f5385ebf 1249 h->non_elf = 0;
0a1b45a2 1250 return true;
45d6a902 1251 }
252b5132 1252
45d6a902
AM
1253 /* In cases involving weak versioned symbols, we may wind up trying
1254 to merge a symbol with itself. Catch that here, to avoid the
1255 confusion that results if we try to override a symbol with
1256 itself. The additional tests catch cases like
1257 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1258 dynamic object, which we do want to handle here. */
1259 if (abfd == oldbfd
895fa45f 1260 && (newweak || oldweak)
45d6a902 1261 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1262 || !h->def_regular))
0a1b45a2 1263 return true;
45d6a902 1264
0a1b45a2 1265 olddyn = false;
45d6a902
AM
1266 if (oldbfd != NULL)
1267 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1268 else if (oldsec != NULL)
45d6a902 1269 {
707bba77 1270 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1271 indices used by MIPS ELF. */
707bba77 1272 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1273 }
252b5132 1274
1a3b5c34
AM
1275 /* Handle a case where plugin_notice won't be called and thus won't
1276 set the non_ir_ref flags on the first pass over symbols. */
1277 if (oldbfd != NULL
1278 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1279 && newdyn != olddyn)
1280 {
0a1b45a2
AM
1281 h->root.non_ir_ref_dynamic = true;
1282 hi->root.non_ir_ref_dynamic = true;
1a3b5c34
AM
1283 }
1284
45d6a902
AM
1285 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1286 respectively, appear to be a definition rather than reference. */
1287
707bba77 1288 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1289
707bba77
AM
1290 olddef = (h->root.type != bfd_link_hash_undefined
1291 && h->root.type != bfd_link_hash_undefweak
202ac193 1292 && h->root.type != bfd_link_hash_common);
45d6a902 1293
0a36a439
L
1294 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1295 respectively, appear to be a function. */
1296
1297 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1298 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1299
1300 oldfunc = (h->type != STT_NOTYPE
1301 && bed->is_function_type (h->type));
1302
c5d37467 1303 if (!(newfunc && oldfunc)
5b677558
AM
1304 && ELF_ST_TYPE (sym->st_info) != h->type
1305 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1306 && h->type != STT_NOTYPE
c5d37467
AM
1307 && (newdef || bfd_is_com_section (sec))
1308 && (olddef || h->root.type == bfd_link_hash_common))
580a2b6e 1309 {
c5d37467
AM
1310 /* If creating a default indirect symbol ("foo" or "foo@") from
1311 a dynamic versioned definition ("foo@@") skip doing so if
1312 there is an existing regular definition with a different
1313 type. We don't want, for example, a "time" variable in the
1314 executable overriding a "time" function in a shared library. */
1315 if (newdyn
1316 && !olddyn)
1317 {
0a1b45a2
AM
1318 *skip = true;
1319 return true;
c5d37467
AM
1320 }
1321
1322 /* When adding a symbol from a regular object file after we have
1323 created indirect symbols, undo the indirection and any
1324 dynamic state. */
1325 if (hi != h
1326 && !newdyn
1327 && olddyn)
1328 {
1329 h = hi;
0a1b45a2 1330 (*bed->elf_backend_hide_symbol) (info, h, true);
c5d37467
AM
1331 h->forced_local = 0;
1332 h->ref_dynamic = 0;
1333 h->def_dynamic = 0;
1334 h->dynamic_def = 0;
1335 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1336 {
1337 h->root.type = bfd_link_hash_undefined;
1338 h->root.u.undef.abfd = abfd;
1339 }
1340 else
1341 {
1342 h->root.type = bfd_link_hash_new;
1343 h->root.u.undef.abfd = NULL;
1344 }
0a1b45a2 1345 return true;
c5d37467 1346 }
580a2b6e
L
1347 }
1348
4c34aff8
AM
1349 /* Check TLS symbols. We don't check undefined symbols introduced
1350 by "ld -u" which have no type (and oldbfd NULL), and we don't
1351 check symbols from plugins because they also have no type. */
1352 if (oldbfd != NULL
1353 && (oldbfd->flags & BFD_PLUGIN) == 0
1354 && (abfd->flags & BFD_PLUGIN) == 0
1355 && ELF_ST_TYPE (sym->st_info) != h->type
1356 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1357 {
1358 bfd *ntbfd, *tbfd;
0a1b45a2 1359 bool ntdef, tdef;
7479dfd4
L
1360 asection *ntsec, *tsec;
1361
1362 if (h->type == STT_TLS)
1363 {
3b36f7e6 1364 ntbfd = abfd;
7479dfd4
L
1365 ntsec = sec;
1366 ntdef = newdef;
1367 tbfd = oldbfd;
1368 tsec = oldsec;
1369 tdef = olddef;
1370 }
1371 else
1372 {
1373 ntbfd = oldbfd;
1374 ntsec = oldsec;
1375 ntdef = olddef;
1376 tbfd = abfd;
1377 tsec = sec;
1378 tdef = newdef;
1379 }
1380
1381 if (tdef && ntdef)
4eca0228 1382 _bfd_error_handler
695344c0 1383 /* xgettext:c-format */
871b3ab2
AM
1384 (_("%s: TLS definition in %pB section %pA "
1385 "mismatches non-TLS definition in %pB section %pA"),
c08bb8dd 1386 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
7479dfd4 1387 else if (!tdef && !ntdef)
4eca0228 1388 _bfd_error_handler
695344c0 1389 /* xgettext:c-format */
871b3ab2
AM
1390 (_("%s: TLS reference in %pB "
1391 "mismatches non-TLS reference in %pB"),
c08bb8dd 1392 h->root.root.string, tbfd, ntbfd);
7479dfd4 1393 else if (tdef)
4eca0228 1394 _bfd_error_handler
695344c0 1395 /* xgettext:c-format */
871b3ab2
AM
1396 (_("%s: TLS definition in %pB section %pA "
1397 "mismatches non-TLS reference in %pB"),
c08bb8dd 1398 h->root.root.string, tbfd, tsec, ntbfd);
7479dfd4 1399 else
4eca0228 1400 _bfd_error_handler
695344c0 1401 /* xgettext:c-format */
871b3ab2
AM
1402 (_("%s: TLS reference in %pB "
1403 "mismatches non-TLS definition in %pB section %pA"),
c08bb8dd 1404 h->root.root.string, tbfd, ntbfd, ntsec);
7479dfd4
L
1405
1406 bfd_set_error (bfd_error_bad_value);
0a1b45a2 1407 return false;
7479dfd4
L
1408 }
1409
45d6a902
AM
1410 /* If the old symbol has non-default visibility, we ignore the new
1411 definition from a dynamic object. */
1412 if (newdyn
9c7a29a3 1413 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1414 && !bfd_is_und_section (sec))
1415 {
0a1b45a2 1416 *skip = true;
45d6a902 1417 /* Make sure this symbol is dynamic. */
f5385ebf 1418 h->ref_dynamic = 1;
90c984fc 1419 hi->ref_dynamic = 1;
45d6a902
AM
1420 /* A protected symbol has external availability. Make sure it is
1421 recorded as dynamic.
1422
1423 FIXME: Should we check type and size for protected symbol? */
1424 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1425 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902 1426 else
0a1b45a2 1427 return true;
45d6a902
AM
1428 }
1429 else if (!newdyn
9c7a29a3 1430 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1431 && h->def_dynamic)
45d6a902
AM
1432 {
1433 /* If the new symbol with non-default visibility comes from a
1434 relocatable file and the old definition comes from a dynamic
1435 object, we remove the old definition. */
6c9b78e6 1436 if (hi->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1437 {
1438 /* Handle the case where the old dynamic definition is
1439 default versioned. We need to copy the symbol info from
1440 the symbol with default version to the normal one if it
1441 was referenced before. */
1442 if (h->ref_regular)
1443 {
6c9b78e6 1444 hi->root.type = h->root.type;
d2dee3b2 1445 h->root.type = bfd_link_hash_indirect;
6c9b78e6 1446 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
aed81c4e 1447
6c9b78e6 1448 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
aed81c4e 1449 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
d2dee3b2 1450 {
aed81c4e
MR
1451 /* If the new symbol is hidden or internal, completely undo
1452 any dynamic link state. */
0a1b45a2 1453 (*bed->elf_backend_hide_symbol) (info, h, true);
aed81c4e
MR
1454 h->forced_local = 0;
1455 h->ref_dynamic = 0;
d2dee3b2
L
1456 }
1457 else
aed81c4e
MR
1458 h->ref_dynamic = 1;
1459
1460 h->def_dynamic = 0;
aed81c4e
MR
1461 /* FIXME: Should we check type and size for protected symbol? */
1462 h->size = 0;
1463 h->type = 0;
1464
6c9b78e6 1465 h = hi;
d2dee3b2
L
1466 }
1467 else
6c9b78e6 1468 h = hi;
d2dee3b2 1469 }
1de1a317 1470
f5eda473
AM
1471 /* If the old symbol was undefined before, then it will still be
1472 on the undefs list. If the new symbol is undefined or
1473 common, we can't make it bfd_link_hash_new here, because new
1474 undefined or common symbols will be added to the undefs list
1475 by _bfd_generic_link_add_one_symbol. Symbols may not be
1476 added twice to the undefs list. Also, if the new symbol is
1477 undefweak then we don't want to lose the strong undef. */
1478 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317 1479 {
1de1a317 1480 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1481 h->root.u.undef.abfd = abfd;
1482 }
1483 else
1484 {
1485 h->root.type = bfd_link_hash_new;
1486 h->root.u.undef.abfd = NULL;
1487 }
1488
f5eda473 1489 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
252b5132 1490 {
f5eda473
AM
1491 /* If the new symbol is hidden or internal, completely undo
1492 any dynamic link state. */
0a1b45a2 1493 (*bed->elf_backend_hide_symbol) (info, h, true);
f5eda473
AM
1494 h->forced_local = 0;
1495 h->ref_dynamic = 0;
45d6a902 1496 }
f5eda473
AM
1497 else
1498 h->ref_dynamic = 1;
1499 h->def_dynamic = 0;
45d6a902
AM
1500 /* FIXME: Should we check type and size for protected symbol? */
1501 h->size = 0;
1502 h->type = 0;
0a1b45a2 1503 return true;
45d6a902 1504 }
14a793b2 1505
15b43f48
AM
1506 /* If a new weak symbol definition comes from a regular file and the
1507 old symbol comes from a dynamic library, we treat the new one as
1508 strong. Similarly, an old weak symbol definition from a regular
1509 file is treated as strong when the new symbol comes from a dynamic
1510 library. Further, an old weak symbol from a dynamic library is
1511 treated as strong if the new symbol is from a dynamic library.
1512 This reflects the way glibc's ld.so works.
1513
165f707a
AM
1514 Also allow a weak symbol to override a linker script symbol
1515 defined by an early pass over the script. This is done so the
1516 linker knows the symbol is defined in an object file, for the
1517 DEFINED script function.
1518
15b43f48
AM
1519 Do this before setting *type_change_ok or *size_change_ok so that
1520 we warn properly when dynamic library symbols are overridden. */
1521
165f707a 1522 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
0a1b45a2 1523 newweak = false;
15b43f48 1524 if (olddef && newdyn)
0a1b45a2 1525 oldweak = false;
0f8a2703 1526
d334575b 1527 /* Allow changes between different types of function symbol. */
0a36a439 1528 if (newfunc && oldfunc)
0a1b45a2 1529 *type_change_ok = true;
fcb93ecf 1530
79349b09
AM
1531 /* It's OK to change the type if either the existing symbol or the
1532 new symbol is weak. A type change is also OK if the old symbol
1533 is undefined and the new symbol is defined. */
252b5132 1534
79349b09
AM
1535 if (oldweak
1536 || newweak
1537 || (newdef
1538 && h->root.type == bfd_link_hash_undefined))
0a1b45a2 1539 *type_change_ok = true;
79349b09
AM
1540
1541 /* It's OK to change the size if either the existing symbol or the
1542 new symbol is weak, or if the old symbol is undefined. */
1543
1544 if (*type_change_ok
1545 || h->root.type == bfd_link_hash_undefined)
0a1b45a2 1546 *size_change_ok = true;
45d6a902 1547
45d6a902
AM
1548 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1549 symbol, respectively, appears to be a common symbol in a dynamic
1550 object. If a symbol appears in an uninitialized section, and is
1551 not weak, and is not a function, then it may be a common symbol
1552 which was resolved when the dynamic object was created. We want
1553 to treat such symbols specially, because they raise special
1554 considerations when setting the symbol size: if the symbol
1555 appears as a common symbol in a regular object, and the size in
1556 the regular object is larger, we must make sure that we use the
1557 larger size. This problematic case can always be avoided in C,
1558 but it must be handled correctly when using Fortran shared
1559 libraries.
1560
1561 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1562 likewise for OLDDYNCOMMON and OLDDEF.
1563
1564 Note that this test is just a heuristic, and that it is quite
1565 possible to have an uninitialized symbol in a shared object which
1566 is really a definition, rather than a common symbol. This could
1567 lead to some minor confusion when the symbol really is a common
1568 symbol in some regular object. However, I think it will be
1569 harmless. */
1570
1571 if (newdyn
1572 && newdef
79349b09 1573 && !newweak
45d6a902
AM
1574 && (sec->flags & SEC_ALLOC) != 0
1575 && (sec->flags & SEC_LOAD) == 0
1576 && sym->st_size > 0
0a36a439 1577 && !newfunc)
0a1b45a2 1578 newdyncommon = true;
45d6a902 1579 else
0a1b45a2 1580 newdyncommon = false;
45d6a902
AM
1581
1582 if (olddyn
1583 && olddef
1584 && h->root.type == bfd_link_hash_defined
f5385ebf 1585 && h->def_dynamic
45d6a902
AM
1586 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1587 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1588 && h->size > 0
0a36a439 1589 && !oldfunc)
0a1b45a2 1590 olddyncommon = true;
45d6a902 1591 else
0a1b45a2 1592 olddyncommon = false;
45d6a902 1593
a4d8e49b
L
1594 /* We now know everything about the old and new symbols. We ask the
1595 backend to check if we can merge them. */
5d13b3b3
AM
1596 if (bed->merge_symbol != NULL)
1597 {
1598 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
0a1b45a2 1599 return false;
5d13b3b3
AM
1600 sec = *psec;
1601 }
a4d8e49b 1602
a83ef4d1
L
1603 /* There are multiple definitions of a normal symbol. Skip the
1604 default symbol as well as definition from an IR object. */
93f4de39 1605 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
a83ef4d1
L
1606 && !default_sym && h->def_regular
1607 && !(oldbfd != NULL
1608 && (oldbfd->flags & BFD_PLUGIN) != 0
1609 && (abfd->flags & BFD_PLUGIN) == 0))
93f4de39
RL
1610 {
1611 /* Handle a multiple definition. */
1612 (*info->callbacks->multiple_definition) (info, &h->root,
1613 abfd, sec, *pvalue);
0a1b45a2
AM
1614 *skip = true;
1615 return true;
93f4de39
RL
1616 }
1617
45d6a902
AM
1618 /* If both the old and the new symbols look like common symbols in a
1619 dynamic object, set the size of the symbol to the larger of the
1620 two. */
1621
1622 if (olddyncommon
1623 && newdyncommon
1624 && sym->st_size != h->size)
1625 {
1626 /* Since we think we have two common symbols, issue a multiple
1627 common warning if desired. Note that we only warn if the
1628 size is different. If the size is the same, we simply let
1629 the old symbol override the new one as normally happens with
1630 symbols defined in dynamic objects. */
1631
1a72702b
AM
1632 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1633 bfd_link_hash_common, sym->st_size);
45d6a902
AM
1634 if (sym->st_size > h->size)
1635 h->size = sym->st_size;
252b5132 1636
0a1b45a2 1637 *size_change_ok = true;
252b5132
RH
1638 }
1639
45d6a902
AM
1640 /* If we are looking at a dynamic object, and we have found a
1641 definition, we need to see if the symbol was already defined by
1642 some other object. If so, we want to use the existing
1643 definition, and we do not want to report a multiple symbol
1644 definition error; we do this by clobbering *PSEC to be
1645 bfd_und_section_ptr.
1646
1647 We treat a common symbol as a definition if the symbol in the
1648 shared library is a function, since common symbols always
1649 represent variables; this can cause confusion in principle, but
1650 any such confusion would seem to indicate an erroneous program or
1651 shared library. We also permit a common symbol in a regular
8170f769 1652 object to override a weak symbol in a shared object. */
45d6a902
AM
1653
1654 if (newdyn
1655 && newdef
77cfaee6 1656 && (olddef
45d6a902 1657 || (h->root.type == bfd_link_hash_common
8170f769 1658 && (newweak || newfunc))))
45d6a902 1659 {
7ba11550 1660 *override = abfd;
0a1b45a2
AM
1661 newdef = false;
1662 newdyncommon = false;
252b5132 1663
45d6a902 1664 *psec = sec = bfd_und_section_ptr;
0a1b45a2 1665 *size_change_ok = true;
252b5132 1666
45d6a902
AM
1667 /* If we get here when the old symbol is a common symbol, then
1668 we are explicitly letting it override a weak symbol or
1669 function in a dynamic object, and we don't want to warn about
1670 a type change. If the old symbol is a defined symbol, a type
1671 change warning may still be appropriate. */
252b5132 1672
45d6a902 1673 if (h->root.type == bfd_link_hash_common)
0a1b45a2 1674 *type_change_ok = true;
45d6a902
AM
1675 }
1676
1677 /* Handle the special case of an old common symbol merging with a
1678 new symbol which looks like a common symbol in a shared object.
1679 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1680 common symbol, and let _bfd_generic_link_add_one_symbol do the
1681 right thing. */
45d6a902
AM
1682
1683 if (newdyncommon
1684 && h->root.type == bfd_link_hash_common)
1685 {
7ba11550 1686 *override = oldbfd;
0a1b45a2
AM
1687 newdef = false;
1688 newdyncommon = false;
45d6a902 1689 *pvalue = sym->st_size;
a4d8e49b 1690 *psec = sec = bed->common_section (oldsec);
0a1b45a2 1691 *size_change_ok = true;
45d6a902
AM
1692 }
1693
c5e2cead 1694 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1695 if (newdef && olddef && newweak)
54ac0771 1696 {
35ed3f94 1697 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1698 if (!(oldbfd != NULL
1699 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94 1700 && (abfd->flags & BFD_PLUGIN) == 0))
57fa7b8c 1701 {
0a1b45a2
AM
1702 newdef = false;
1703 *skip = true;
57fa7b8c 1704 }
54ac0771
L
1705
1706 /* Merge st_other. If the symbol already has a dynamic index,
1707 but visibility says it should not be visible, turn it into a
1708 local symbol. */
5160d0f3 1709 elf_merge_st_other (abfd, h, sym->st_other, sec, newdef, newdyn);
54ac0771
L
1710 if (h->dynindx != -1)
1711 switch (ELF_ST_VISIBILITY (h->other))
1712 {
1713 case STV_INTERNAL:
1714 case STV_HIDDEN:
0a1b45a2 1715 (*bed->elf_backend_hide_symbol) (info, h, true);
54ac0771
L
1716 break;
1717 }
1718 }
c5e2cead 1719
45d6a902
AM
1720 /* If the old symbol is from a dynamic object, and the new symbol is
1721 a definition which is not from a dynamic object, then the new
1722 symbol overrides the old symbol. Symbols from regular files
1723 always take precedence over symbols from dynamic objects, even if
1724 they are defined after the dynamic object in the link.
1725
1726 As above, we again permit a common symbol in a regular object to
1727 override a definition in a shared object if the shared object
0f8a2703 1728 symbol is a function or is weak. */
45d6a902
AM
1729
1730 flip = NULL;
77cfaee6 1731 if (!newdyn
45d6a902
AM
1732 && (newdef
1733 || (bfd_is_com_section (sec)
0a36a439 1734 && (oldweak || oldfunc)))
45d6a902
AM
1735 && olddyn
1736 && olddef
f5385ebf 1737 && h->def_dynamic)
45d6a902
AM
1738 {
1739 /* Change the hash table entry to undefined, and let
1740 _bfd_generic_link_add_one_symbol do the right thing with the
1741 new definition. */
1742
1743 h->root.type = bfd_link_hash_undefined;
1744 h->root.u.undef.abfd = h->root.u.def.section->owner;
0a1b45a2 1745 *size_change_ok = true;
45d6a902 1746
0a1b45a2
AM
1747 olddef = false;
1748 olddyncommon = false;
45d6a902
AM
1749
1750 /* We again permit a type change when a common symbol may be
1751 overriding a function. */
1752
1753 if (bfd_is_com_section (sec))
0a36a439
L
1754 {
1755 if (oldfunc)
1756 {
1757 /* If a common symbol overrides a function, make sure
1758 that it isn't defined dynamically nor has type
1759 function. */
1760 h->def_dynamic = 0;
1761 h->type = STT_NOTYPE;
1762 }
0a1b45a2 1763 *type_change_ok = true;
0a36a439 1764 }
45d6a902 1765
6c9b78e6
AM
1766 if (hi->root.type == bfd_link_hash_indirect)
1767 flip = hi;
45d6a902
AM
1768 else
1769 /* This union may have been set to be non-NULL when this symbol
1770 was seen in a dynamic object. We must force the union to be
1771 NULL, so that it is correct for a regular symbol. */
1772 h->verinfo.vertree = NULL;
1773 }
1774
1775 /* Handle the special case of a new common symbol merging with an
1776 old symbol that looks like it might be a common symbol defined in
1777 a shared object. Note that we have already handled the case in
1778 which a new common symbol should simply override the definition
1779 in the shared library. */
1780
1781 if (! newdyn
1782 && bfd_is_com_section (sec)
1783 && olddyncommon)
1784 {
1785 /* It would be best if we could set the hash table entry to a
1786 common symbol, but we don't know what to use for the section
1787 or the alignment. */
1a72702b
AM
1788 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1789 bfd_link_hash_common, sym->st_size);
45d6a902 1790
4cc11e76 1791 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1792 larger, pretend that the new symbol has its size. */
1793
1794 if (h->size > *pvalue)
1795 *pvalue = h->size;
1796
af44c138
L
1797 /* We need to remember the alignment required by the symbol
1798 in the dynamic object. */
1799 BFD_ASSERT (pold_alignment);
1800 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902 1801
0a1b45a2
AM
1802 olddef = false;
1803 olddyncommon = false;
45d6a902
AM
1804
1805 h->root.type = bfd_link_hash_undefined;
1806 h->root.u.undef.abfd = h->root.u.def.section->owner;
1807
0a1b45a2
AM
1808 *size_change_ok = true;
1809 *type_change_ok = true;
45d6a902 1810
6c9b78e6
AM
1811 if (hi->root.type == bfd_link_hash_indirect)
1812 flip = hi;
45d6a902
AM
1813 else
1814 h->verinfo.vertree = NULL;
1815 }
1816
1817 if (flip != NULL)
1818 {
1819 /* Handle the case where we had a versioned symbol in a dynamic
1820 library and now find a definition in a normal object. In this
1821 case, we make the versioned symbol point to the normal one. */
45d6a902 1822 flip->root.type = h->root.type;
00cbee0a 1823 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1824 h->root.type = bfd_link_hash_indirect;
1825 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1826 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1827 if (h->def_dynamic)
45d6a902 1828 {
f5385ebf
AM
1829 h->def_dynamic = 0;
1830 flip->ref_dynamic = 1;
45d6a902
AM
1831 }
1832 }
1833
0a1b45a2 1834 return true;
45d6a902
AM
1835}
1836
1837/* This function is called to create an indirect symbol from the
1838 default for the symbol with the default version if needed. The
4f3fedcf 1839 symbol is described by H, NAME, SYM, SEC, and VALUE. We
0f8a2703 1840 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1841
0a1b45a2 1842static bool
268b6b39
AM
1843_bfd_elf_add_default_symbol (bfd *abfd,
1844 struct bfd_link_info *info,
1845 struct elf_link_hash_entry *h,
1846 const char *name,
1847 Elf_Internal_Sym *sym,
4f3fedcf
AM
1848 asection *sec,
1849 bfd_vma value,
1850 bfd **poldbfd,
0a1b45a2 1851 bool *dynsym)
45d6a902 1852{
0a1b45a2
AM
1853 bool type_change_ok;
1854 bool size_change_ok;
1855 bool skip;
45d6a902
AM
1856 char *shortname;
1857 struct elf_link_hash_entry *hi;
1858 struct bfd_link_hash_entry *bh;
9c5bfbb7 1859 const struct elf_backend_data *bed;
0a1b45a2
AM
1860 bool collect;
1861 bool dynamic;
f01fb44c 1862 bfd *override;
45d6a902
AM
1863 char *p;
1864 size_t len, shortlen;
ffd65175 1865 asection *tmp_sec;
0a1b45a2 1866 bool matched;
45d6a902 1867
422f1182 1868 if (h->versioned == unversioned || h->versioned == versioned_hidden)
0a1b45a2 1869 return true;
422f1182 1870
45d6a902
AM
1871 /* If this symbol has a version, and it is the default version, we
1872 create an indirect symbol from the default name to the fully
1873 decorated name. This will cause external references which do not
1874 specify a version to be bound to this version of the symbol. */
1875 p = strchr (name, ELF_VER_CHR);
422f1182
L
1876 if (h->versioned == unknown)
1877 {
1878 if (p == NULL)
1879 {
1880 h->versioned = unversioned;
0a1b45a2 1881 return true;
422f1182
L
1882 }
1883 else
1884 {
1885 if (p[1] != ELF_VER_CHR)
1886 {
1887 h->versioned = versioned_hidden;
0a1b45a2 1888 return true;
422f1182
L
1889 }
1890 else
1891 h->versioned = versioned;
1892 }
1893 }
4373f8af
L
1894 else
1895 {
1896 /* PR ld/19073: We may see an unversioned definition after the
1897 default version. */
1898 if (p == NULL)
0a1b45a2 1899 return true;
4373f8af 1900 }
45d6a902 1901
45d6a902
AM
1902 bed = get_elf_backend_data (abfd);
1903 collect = bed->collect;
1904 dynamic = (abfd->flags & DYNAMIC) != 0;
1905
1906 shortlen = p - name;
a50b1753 1907 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902 1908 if (shortname == NULL)
0a1b45a2 1909 return false;
45d6a902
AM
1910 memcpy (shortname, name, shortlen);
1911 shortname[shortlen] = '\0';
1912
1913 /* We are going to create a new symbol. Merge it with any existing
1914 symbol with this name. For the purposes of the merge, act as
1915 though we were defining the symbol we just defined, although we
1916 actually going to define an indirect symbol. */
0a1b45a2
AM
1917 type_change_ok = false;
1918 size_change_ok = false;
1919 matched = true;
ffd65175
AM
1920 tmp_sec = sec;
1921 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
f01fb44c 1922 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1923 &type_change_ok, &size_change_ok, &matched))
0a1b45a2 1924 return false;
45d6a902
AM
1925
1926 if (skip)
1927 goto nondefault;
1928
5fa370e4 1929 if (hi->def_regular || ELF_COMMON_DEF_P (hi))
5b677558
AM
1930 {
1931 /* If the undecorated symbol will have a version added by a
1932 script different to H, then don't indirect to/from the
1933 undecorated symbol. This isn't ideal because we may not yet
1934 have seen symbol versions, if given by a script on the
1935 command line rather than via --version-script. */
1936 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1937 {
0a1b45a2 1938 bool hide;
5b677558
AM
1939
1940 hi->verinfo.vertree
1941 = bfd_find_version_for_sym (info->version_info,
1942 hi->root.root.string, &hide);
1943 if (hi->verinfo.vertree != NULL && hide)
1944 {
0a1b45a2 1945 (*bed->elf_backend_hide_symbol) (info, hi, true);
5b677558
AM
1946 goto nondefault;
1947 }
1948 }
1949 if (hi->verinfo.vertree != NULL
1950 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
f01fb44c 1951 goto nondefault;
5b677558
AM
1952 }
1953
f01fb44c 1954 if (! override)
45d6a902 1955 {
c6e8a9a8 1956 /* Add the default symbol if not performing a relocatable link. */
0e1862bb 1957 if (! bfd_link_relocatable (info))
c6e8a9a8
L
1958 {
1959 bh = &hi->root;
fbcc8baf 1960 if (bh->type == bfd_link_hash_defined
6cc71b82 1961 && bh->u.def.section->owner != NULL
fbcc8baf
L
1962 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1963 {
1964 /* Mark the previous definition from IR object as
1965 undefined so that the generic linker will override
1966 it. */
1967 bh->type = bfd_link_hash_undefined;
1968 bh->u.undef.abfd = bh->u.def.section->owner;
1969 }
c6e8a9a8
L
1970 if (! (_bfd_generic_link_add_one_symbol
1971 (info, abfd, shortname, BSF_INDIRECT,
1972 bfd_ind_section_ptr,
0a1b45a2
AM
1973 0, name, false, collect, &bh)))
1974 return false;
c6e8a9a8
L
1975 hi = (struct elf_link_hash_entry *) bh;
1976 }
45d6a902
AM
1977 }
1978 else
1979 {
1980 /* In this case the symbol named SHORTNAME is overriding the
1981 indirect symbol we want to add. We were planning on making
1982 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1983 is the name without a version. NAME is the fully versioned
1984 name, and it is the default version.
1985
1986 Overriding means that we already saw a definition for the
1987 symbol SHORTNAME in a regular object, and it is overriding
1988 the symbol defined in the dynamic object.
1989
1990 When this happens, we actually want to change NAME, the
1991 symbol we just added, to refer to SHORTNAME. This will cause
1992 references to NAME in the shared object to become references
1993 to SHORTNAME in the regular object. This is what we expect
1994 when we override a function in a shared object: that the
1995 references in the shared object will be mapped to the
1996 definition in the regular object. */
1997
1998 while (hi->root.type == bfd_link_hash_indirect
1999 || hi->root.type == bfd_link_hash_warning)
2000 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2001
2002 h->root.type = bfd_link_hash_indirect;
2003 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 2004 if (h->def_dynamic)
45d6a902 2005 {
f5385ebf
AM
2006 h->def_dynamic = 0;
2007 hi->ref_dynamic = 1;
2008 if (hi->ref_regular
2009 || hi->def_regular)
45d6a902 2010 {
c152c796 2011 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
0a1b45a2 2012 return false;
45d6a902
AM
2013 }
2014 }
2015
2016 /* Now set HI to H, so that the following code will set the
2017 other fields correctly. */
2018 hi = h;
2019 }
2020
fab4a87f
L
2021 /* Check if HI is a warning symbol. */
2022 if (hi->root.type == bfd_link_hash_warning)
2023 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2024
45d6a902
AM
2025 /* If there is a duplicate definition somewhere, then HI may not
2026 point to an indirect symbol. We will have reported an error to
2027 the user in that case. */
2028
2029 if (hi->root.type == bfd_link_hash_indirect)
2030 {
2031 struct elf_link_hash_entry *ht;
2032
45d6a902 2033 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 2034 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902 2035
5160d0f3
AM
2036 /* If we first saw a reference to SHORTNAME with non-default
2037 visibility, merge that visibility to the @@VER symbol. */
0a1b45a2 2038 elf_merge_st_other (abfd, ht, hi->other, sec, true, dynamic);
5160d0f3 2039
68c88cd4
AM
2040 /* A reference to the SHORTNAME symbol from a dynamic library
2041 will be satisfied by the versioned symbol at runtime. In
2042 effect, we have a reference to the versioned symbol. */
2043 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2044 hi->dynamic_def |= ht->dynamic_def;
2045
45d6a902
AM
2046 /* See if the new flags lead us to realize that the symbol must
2047 be dynamic. */
2048 if (! *dynsym)
2049 {
2050 if (! dynamic)
2051 {
0e1862bb 2052 if (! bfd_link_executable (info)
90c984fc 2053 || hi->def_dynamic
f5385ebf 2054 || hi->ref_dynamic)
0a1b45a2 2055 *dynsym = true;
45d6a902
AM
2056 }
2057 else
2058 {
f5385ebf 2059 if (hi->ref_regular)
0a1b45a2 2060 *dynsym = true;
45d6a902
AM
2061 }
2062 }
2063 }
2064
2065 /* We also need to define an indirection from the nondefault version
2066 of the symbol. */
2067
dc1e8a47 2068 nondefault:
45d6a902 2069 len = strlen (name);
a50b1753 2070 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902 2071 if (shortname == NULL)
0a1b45a2 2072 return false;
45d6a902
AM
2073 memcpy (shortname, name, shortlen);
2074 memcpy (shortname + shortlen, p + 1, len - shortlen);
2075
2076 /* Once again, merge with any existing symbol. */
0a1b45a2
AM
2077 type_change_ok = false;
2078 size_change_ok = false;
ffd65175
AM
2079 tmp_sec = sec;
2080 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
f01fb44c 2081 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 2082 &type_change_ok, &size_change_ok, &matched))
0a1b45a2 2083 return false;
45d6a902
AM
2084
2085 if (skip)
726d7d1e
AM
2086 {
2087 if (!dynamic
2088 && h->root.type == bfd_link_hash_defweak
2089 && hi->root.type == bfd_link_hash_defined)
2090 {
2091 /* We are handling a weak sym@@ver and attempting to define
2092 a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2093 new weak sym@ver because there is already a strong sym@ver.
2094 However, sym@ver and sym@@ver are really the same symbol.
2095 The existing strong sym@ver ought to override sym@@ver. */
2096 h->root.type = bfd_link_hash_defined;
2097 h->root.u.def.section = hi->root.u.def.section;
2098 h->root.u.def.value = hi->root.u.def.value;
2099 hi->root.type = bfd_link_hash_indirect;
2100 hi->root.u.i.link = &h->root;
2101 }
2102 else
0a1b45a2 2103 return true;
726d7d1e 2104 }
f01fb44c 2105 else if (override)
45d6a902
AM
2106 {
2107 /* Here SHORTNAME is a versioned name, so we don't expect to see
2108 the type of override we do in the case above unless it is
4cc11e76 2109 overridden by a versioned definition. */
45d6a902
AM
2110 if (hi->root.type != bfd_link_hash_defined
2111 && hi->root.type != bfd_link_hash_defweak)
4eca0228 2112 _bfd_error_handler
695344c0 2113 /* xgettext:c-format */
871b3ab2 2114 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
d003868e 2115 abfd, shortname);
0a1b45a2 2116 return true;
45d6a902
AM
2117 }
2118 else
2119 {
2120 bh = &hi->root;
2121 if (! (_bfd_generic_link_add_one_symbol
2122 (info, abfd, shortname, BSF_INDIRECT,
0a1b45a2
AM
2123 bfd_ind_section_ptr, 0, name, false, collect, &bh)))
2124 return false;
45d6a902 2125 hi = (struct elf_link_hash_entry *) bh;
726d7d1e 2126 }
45d6a902 2127
726d7d1e
AM
2128 /* If there is a duplicate definition somewhere, then HI may not
2129 point to an indirect symbol. We will have reported an error
2130 to the user in that case. */
2131 if (hi->root.type == bfd_link_hash_indirect)
2132 {
2133 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2134 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2135 hi->dynamic_def |= h->dynamic_def;
45d6a902 2136
726d7d1e
AM
2137 /* If we first saw a reference to @VER symbol with
2138 non-default visibility, merge that visibility to the
2139 @@VER symbol. */
0a1b45a2 2140 elf_merge_st_other (abfd, h, hi->other, sec, true, dynamic);
726d7d1e
AM
2141
2142 /* See if the new flags lead us to realize that the symbol
2143 must be dynamic. */
2144 if (! *dynsym)
45d6a902 2145 {
726d7d1e 2146 if (! dynamic)
45d6a902 2147 {
726d7d1e
AM
2148 if (! bfd_link_executable (info)
2149 || hi->ref_dynamic)
0a1b45a2 2150 *dynsym = true;
726d7d1e
AM
2151 }
2152 else
2153 {
2154 if (hi->ref_regular)
0a1b45a2 2155 *dynsym = true;
45d6a902
AM
2156 }
2157 }
2158 }
2159
0a1b45a2 2160 return true;
45d6a902
AM
2161}
2162\f
2163/* This routine is used to export all defined symbols into the dynamic
2164 symbol table. It is called via elf_link_hash_traverse. */
2165
0a1b45a2 2166static bool
268b6b39 2167_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2168{
a50b1753 2169 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
2170
2171 /* Ignore indirect symbols. These are added by the versioning code. */
2172 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 2173 return true;
45d6a902 2174
7686d77d
AM
2175 /* Ignore this if we won't export it. */
2176 if (!eif->info->export_dynamic && !h->dynamic)
0a1b45a2 2177 return true;
45d6a902
AM
2178
2179 if (h->dynindx == -1
fd91d419
L
2180 && (h->def_regular || h->ref_regular)
2181 && ! bfd_hide_sym_by_version (eif->info->version_info,
2182 h->root.root.string))
45d6a902 2183 {
fd91d419 2184 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 2185 {
0a1b45a2
AM
2186 eif->failed = true;
2187 return false;
45d6a902
AM
2188 }
2189 }
2190
0a1b45a2 2191 return true;
45d6a902
AM
2192}
2193\f
2194/* Look through the symbols which are defined in other shared
2195 libraries and referenced here. Update the list of version
2196 dependencies. This will be put into the .gnu.version_r section.
2197 This function is called via elf_link_hash_traverse. */
2198
0a1b45a2 2199static bool
268b6b39
AM
2200_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2201 void *data)
45d6a902 2202{
a50b1753 2203 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
2204 Elf_Internal_Verneed *t;
2205 Elf_Internal_Vernaux *a;
986f0783 2206 size_t amt;
45d6a902 2207
45d6a902
AM
2208 /* We only care about symbols defined in shared objects with version
2209 information. */
f5385ebf
AM
2210 if (!h->def_dynamic
2211 || h->def_regular
45d6a902 2212 || h->dynindx == -1
7b20f099
AM
2213 || h->verinfo.verdef == NULL
2214 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2215 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
0a1b45a2 2216 return true;
45d6a902
AM
2217
2218 /* See if we already know about this version. */
28caa186
AM
2219 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2220 t != NULL;
2221 t = t->vn_nextref)
45d6a902
AM
2222 {
2223 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2224 continue;
2225
2226 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2227 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
0a1b45a2 2228 return true;
45d6a902
AM
2229
2230 break;
2231 }
2232
2233 /* This is a new version. Add it to tree we are building. */
2234
2235 if (t == NULL)
2236 {
2237 amt = sizeof *t;
a50b1753 2238 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
2239 if (t == NULL)
2240 {
0a1b45a2
AM
2241 rinfo->failed = true;
2242 return false;
45d6a902
AM
2243 }
2244
2245 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
2246 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2247 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
2248 }
2249
2250 amt = sizeof *a;
a50b1753 2251 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
2252 if (a == NULL)
2253 {
0a1b45a2
AM
2254 rinfo->failed = true;
2255 return false;
14b1c01e 2256 }
45d6a902
AM
2257
2258 /* Note that we are copying a string pointer here, and testing it
2259 above. If bfd_elf_string_from_elf_section is ever changed to
2260 discard the string data when low in memory, this will have to be
2261 fixed. */
2262 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2263
2264 a->vna_flags = h->verinfo.verdef->vd_flags;
2265 a->vna_nextptr = t->vn_auxptr;
2266
2267 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2268 ++rinfo->vers;
2269
2270 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2271
2272 t->vn_auxptr = a;
2273
0a1b45a2 2274 return true;
45d6a902
AM
2275}
2276
099bb8fb
L
2277/* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2278 hidden. Set *T_P to NULL if there is no match. */
2279
0a1b45a2 2280static bool
099bb8fb
L
2281_bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2282 struct elf_link_hash_entry *h,
2283 const char *version_p,
2284 struct bfd_elf_version_tree **t_p,
0a1b45a2 2285 bool *hide)
099bb8fb
L
2286{
2287 struct bfd_elf_version_tree *t;
2288
2289 /* Look for the version. If we find it, it is no longer weak. */
2290 for (t = info->version_info; t != NULL; t = t->next)
2291 {
2292 if (strcmp (t->name, version_p) == 0)
2293 {
2294 size_t len;
2295 char *alc;
2296 struct bfd_elf_version_expr *d;
2297
2298 len = version_p - h->root.root.string;
2299 alc = (char *) bfd_malloc (len);
2300 if (alc == NULL)
0a1b45a2 2301 return false;
099bb8fb
L
2302 memcpy (alc, h->root.root.string, len - 1);
2303 alc[len - 1] = '\0';
2304 if (alc[len - 2] == ELF_VER_CHR)
2305 alc[len - 2] = '\0';
2306
2307 h->verinfo.vertree = t;
0a1b45a2 2308 t->used = true;
099bb8fb
L
2309 d = NULL;
2310
2311 if (t->globals.list != NULL)
2312 d = (*t->match) (&t->globals, NULL, alc);
2313
2314 /* See if there is anything to force this symbol to
2315 local scope. */
2316 if (d == NULL && t->locals.list != NULL)
2317 {
2318 d = (*t->match) (&t->locals, NULL, alc);
2319 if (d != NULL
2320 && h->dynindx != -1
2321 && ! info->export_dynamic)
0a1b45a2 2322 *hide = true;
099bb8fb
L
2323 }
2324
2325 free (alc);
2326 break;
2327 }
2328 }
2329
2330 *t_p = t;
2331
0a1b45a2 2332 return true;
099bb8fb
L
2333}
2334
2335/* Return TRUE if the symbol H is hidden by version script. */
2336
0a1b45a2 2337bool
099bb8fb
L
2338_bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2339 struct elf_link_hash_entry *h)
2340{
2341 const char *p;
0a1b45a2 2342 bool hide = false;
099bb8fb
L
2343 const struct elf_backend_data *bed
2344 = get_elf_backend_data (info->output_bfd);
2345
2346 /* Version script only hides symbols defined in regular objects. */
2347 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
0a1b45a2 2348 return true;
099bb8fb
L
2349
2350 p = strchr (h->root.root.string, ELF_VER_CHR);
2351 if (p != NULL && h->verinfo.vertree == NULL)
2352 {
2353 struct bfd_elf_version_tree *t;
2354
2355 ++p;
2356 if (*p == ELF_VER_CHR)
2357 ++p;
2358
2359 if (*p != '\0'
2360 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2361 && hide)
2362 {
2363 if (hide)
0a1b45a2
AM
2364 (*bed->elf_backend_hide_symbol) (info, h, true);
2365 return true;
099bb8fb
L
2366 }
2367 }
2368
2369 /* If we don't have a version for this symbol, see if we can find
2370 something. */
2371 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2372 {
2373 h->verinfo.vertree
2374 = bfd_find_version_for_sym (info->version_info,
2375 h->root.root.string, &hide);
2376 if (h->verinfo.vertree != NULL && hide)
2377 {
0a1b45a2
AM
2378 (*bed->elf_backend_hide_symbol) (info, h, true);
2379 return true;
099bb8fb
L
2380 }
2381 }
2382
0a1b45a2 2383 return false;
099bb8fb
L
2384}
2385
45d6a902
AM
2386/* Figure out appropriate versions for all the symbols. We may not
2387 have the version number script until we have read all of the input
2388 files, so until that point we don't know which symbols should be
2389 local. This function is called via elf_link_hash_traverse. */
2390
0a1b45a2 2391static bool
268b6b39 2392_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 2393{
28caa186 2394 struct elf_info_failed *sinfo;
45d6a902 2395 struct bfd_link_info *info;
9c5bfbb7 2396 const struct elf_backend_data *bed;
45d6a902
AM
2397 struct elf_info_failed eif;
2398 char *p;
0a1b45a2 2399 bool hide;
45d6a902 2400
a50b1753 2401 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
2402 info = sinfo->info;
2403
45d6a902 2404 /* Fix the symbol flags. */
0a1b45a2 2405 eif.failed = false;
45d6a902
AM
2406 eif.info = info;
2407 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2408 {
2409 if (eif.failed)
0a1b45a2
AM
2410 sinfo->failed = true;
2411 return false;
45d6a902
AM
2412 }
2413
0a640d71
L
2414 bed = get_elf_backend_data (info->output_bfd);
2415
45d6a902
AM
2416 /* We only need version numbers for symbols defined in regular
2417 objects. */
5fa370e4 2418 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
0a640d71
L
2419 {
2420 /* Hide symbols defined in discarded input sections. */
2421 if ((h->root.type == bfd_link_hash_defined
2422 || h->root.type == bfd_link_hash_defweak)
2423 && discarded_section (h->root.u.def.section))
0a1b45a2
AM
2424 (*bed->elf_backend_hide_symbol) (info, h, true);
2425 return true;
0a640d71 2426 }
45d6a902 2427
0a1b45a2 2428 hide = false;
45d6a902
AM
2429 p = strchr (h->root.root.string, ELF_VER_CHR);
2430 if (p != NULL && h->verinfo.vertree == NULL)
2431 {
2432 struct bfd_elf_version_tree *t;
45d6a902 2433
45d6a902
AM
2434 ++p;
2435 if (*p == ELF_VER_CHR)
6e33951e 2436 ++p;
45d6a902
AM
2437
2438 /* If there is no version string, we can just return out. */
2439 if (*p == '\0')
0a1b45a2 2440 return true;
45d6a902 2441
099bb8fb 2442 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
45d6a902 2443 {
0a1b45a2
AM
2444 sinfo->failed = true;
2445 return false;
45d6a902
AM
2446 }
2447
099bb8fb 2448 if (hide)
0a1b45a2 2449 (*bed->elf_backend_hide_symbol) (info, h, true);
099bb8fb 2450
45d6a902
AM
2451 /* If we are building an application, we need to create a
2452 version node for this version. */
0e1862bb 2453 if (t == NULL && bfd_link_executable (info))
45d6a902
AM
2454 {
2455 struct bfd_elf_version_tree **pp;
2456 int version_index;
2457
2458 /* If we aren't going to export this symbol, we don't need
2459 to worry about it. */
2460 if (h->dynindx == -1)
0a1b45a2 2461 return true;
45d6a902 2462
ef53be89
AM
2463 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2464 sizeof *t);
45d6a902
AM
2465 if (t == NULL)
2466 {
0a1b45a2
AM
2467 sinfo->failed = true;
2468 return false;
45d6a902
AM
2469 }
2470
45d6a902 2471 t->name = p;
45d6a902 2472 t->name_indx = (unsigned int) -1;
0a1b45a2 2473 t->used = true;
45d6a902
AM
2474
2475 version_index = 1;
2476 /* Don't count anonymous version tag. */
fd91d419
L
2477 if (sinfo->info->version_info != NULL
2478 && sinfo->info->version_info->vernum == 0)
45d6a902 2479 version_index = 0;
fd91d419
L
2480 for (pp = &sinfo->info->version_info;
2481 *pp != NULL;
2482 pp = &(*pp)->next)
45d6a902
AM
2483 ++version_index;
2484 t->vernum = version_index;
2485
2486 *pp = t;
2487
2488 h->verinfo.vertree = t;
2489 }
2490 else if (t == NULL)
2491 {
2492 /* We could not find the version for a symbol when
2493 generating a shared archive. Return an error. */
4eca0228 2494 _bfd_error_handler
695344c0 2495 /* xgettext:c-format */
871b3ab2 2496 (_("%pB: version node not found for symbol %s"),
28caa186 2497 info->output_bfd, h->root.root.string);
45d6a902 2498 bfd_set_error (bfd_error_bad_value);
0a1b45a2
AM
2499 sinfo->failed = true;
2500 return false;
45d6a902 2501 }
45d6a902
AM
2502 }
2503
2504 /* If we don't have a version for this symbol, see if we can find
2505 something. */
099bb8fb
L
2506 if (!hide
2507 && h->verinfo.vertree == NULL
2508 && sinfo->info->version_info != NULL)
45d6a902 2509 {
fd91d419
L
2510 h->verinfo.vertree
2511 = bfd_find_version_for_sym (sinfo->info->version_info,
2512 h->root.root.string, &hide);
1e8fa21e 2513 if (h->verinfo.vertree != NULL && hide)
0a1b45a2 2514 (*bed->elf_backend_hide_symbol) (info, h, true);
45d6a902
AM
2515 }
2516
0a1b45a2 2517 return true;
45d6a902
AM
2518}
2519\f
45d6a902
AM
2520/* Read and swap the relocs from the section indicated by SHDR. This
2521 may be either a REL or a RELA section. The relocations are
2522 translated into RELA relocations and stored in INTERNAL_RELOCS,
2523 which should have already been allocated to contain enough space.
2524 The EXTERNAL_RELOCS are a buffer where the external form of the
2525 relocations should be stored.
2526
2527 Returns FALSE if something goes wrong. */
2528
0a1b45a2 2529static bool
268b6b39 2530elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2531 asection *sec,
268b6b39
AM
2532 Elf_Internal_Shdr *shdr,
2533 void *external_relocs,
2534 Elf_Internal_Rela *internal_relocs)
45d6a902 2535{
9c5bfbb7 2536 const struct elf_backend_data *bed;
268b6b39 2537 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2538 const bfd_byte *erela;
2539 const bfd_byte *erelaend;
2540 Elf_Internal_Rela *irela;
243ef1e0
L
2541 Elf_Internal_Shdr *symtab_hdr;
2542 size_t nsyms;
45d6a902 2543
45d6a902
AM
2544 /* Position ourselves at the start of the section. */
2545 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
0a1b45a2 2546 return false;
45d6a902
AM
2547
2548 /* Read the relocations. */
2549 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
0a1b45a2 2550 return false;
45d6a902 2551
243ef1e0 2552 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2553 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2554
45d6a902
AM
2555 bed = get_elf_backend_data (abfd);
2556
2557 /* Convert the external relocations to the internal format. */
2558 if (shdr->sh_entsize == bed->s->sizeof_rel)
2559 swap_in = bed->s->swap_reloc_in;
2560 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2561 swap_in = bed->s->swap_reloca_in;
2562 else
2563 {
2564 bfd_set_error (bfd_error_wrong_format);
0a1b45a2 2565 return false;
45d6a902
AM
2566 }
2567
a50b1753 2568 erela = (const bfd_byte *) external_relocs;
f55b1e32
AM
2569 /* Setting erelaend like this and comparing with <= handles case of
2570 a fuzzed object with sh_size not a multiple of sh_entsize. */
2571 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
45d6a902 2572 irela = internal_relocs;
f55b1e32 2573 while (erela <= erelaend)
45d6a902 2574 {
243ef1e0
L
2575 bfd_vma r_symndx;
2576
45d6a902 2577 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2578 r_symndx = ELF32_R_SYM (irela->r_info);
2579 if (bed->s->arch_size == 64)
2580 r_symndx >>= 24;
ce98a316
NC
2581 if (nsyms > 0)
2582 {
2583 if ((size_t) r_symndx >= nsyms)
2584 {
4eca0228 2585 _bfd_error_handler
695344c0 2586 /* xgettext:c-format */
2dcf00ce
AM
2587 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2588 " for offset %#" PRIx64 " in section `%pA'"),
2589 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2590 (uint64_t) irela->r_offset, sec);
ce98a316 2591 bfd_set_error (bfd_error_bad_value);
0a1b45a2 2592 return false;
ce98a316
NC
2593 }
2594 }
cf35638d 2595 else if (r_symndx != STN_UNDEF)
243ef1e0 2596 {
4eca0228 2597 _bfd_error_handler
695344c0 2598 /* xgettext:c-format */
2dcf00ce
AM
2599 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2600 " for offset %#" PRIx64 " in section `%pA'"
ce98a316 2601 " when the object file has no symbol table"),
2dcf00ce
AM
2602 abfd, (uint64_t) r_symndx,
2603 (uint64_t) irela->r_offset, sec);
243ef1e0 2604 bfd_set_error (bfd_error_bad_value);
0a1b45a2 2605 return false;
243ef1e0 2606 }
45d6a902
AM
2607 irela += bed->s->int_rels_per_ext_rel;
2608 erela += shdr->sh_entsize;
2609 }
2610
0a1b45a2 2611 return true;
45d6a902
AM
2612}
2613
2614/* Read and swap the relocs for a section O. They may have been
2615 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2616 not NULL, they are used as buffers to read into. They are known to
2617 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2618 the return value is allocated using either malloc or bfd_alloc,
2619 according to the KEEP_MEMORY argument. If O has two relocation
2620 sections (both REL and RELA relocations), then the REL_HDR
2621 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2622 RELA_HDR relocations. */
45d6a902
AM
2623
2624Elf_Internal_Rela *
268b6b39
AM
2625_bfd_elf_link_read_relocs (bfd *abfd,
2626 asection *o,
2627 void *external_relocs,
2628 Elf_Internal_Rela *internal_relocs,
0a1b45a2 2629 bool keep_memory)
45d6a902 2630{
268b6b39 2631 void *alloc1 = NULL;
45d6a902 2632 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2633 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2634 struct bfd_elf_section_data *esdo = elf_section_data (o);
2635 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2636
d4730f92
BS
2637 if (esdo->relocs != NULL)
2638 return esdo->relocs;
45d6a902
AM
2639
2640 if (o->reloc_count == 0)
2641 return NULL;
2642
45d6a902
AM
2643 if (internal_relocs == NULL)
2644 {
2645 bfd_size_type size;
2646
056bafd4 2647 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
45d6a902 2648 if (keep_memory)
a50b1753 2649 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2650 else
a50b1753 2651 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2652 if (internal_relocs == NULL)
2653 goto error_return;
2654 }
2655
2656 if (external_relocs == NULL)
2657 {
d4730f92
BS
2658 bfd_size_type size = 0;
2659
2660 if (esdo->rel.hdr)
2661 size += esdo->rel.hdr->sh_size;
2662 if (esdo->rela.hdr)
2663 size += esdo->rela.hdr->sh_size;
45d6a902 2664
268b6b39 2665 alloc1 = bfd_malloc (size);
45d6a902
AM
2666 if (alloc1 == NULL)
2667 goto error_return;
2668 external_relocs = alloc1;
2669 }
2670
d4730f92
BS
2671 internal_rela_relocs = internal_relocs;
2672 if (esdo->rel.hdr)
2673 {
2674 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2675 external_relocs,
2676 internal_relocs))
2677 goto error_return;
2678 external_relocs = (((bfd_byte *) external_relocs)
2679 + esdo->rel.hdr->sh_size);
2680 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2681 * bed->s->int_rels_per_ext_rel);
2682 }
2683
2684 if (esdo->rela.hdr
2685 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2686 external_relocs,
2687 internal_rela_relocs)))
45d6a902
AM
2688 goto error_return;
2689
2690 /* Cache the results for next time, if we can. */
2691 if (keep_memory)
d4730f92 2692 esdo->relocs = internal_relocs;
45d6a902 2693
c9594989 2694 free (alloc1);
45d6a902
AM
2695
2696 /* Don't free alloc2, since if it was allocated we are passing it
2697 back (under the name of internal_relocs). */
2698
2699 return internal_relocs;
2700
2701 error_return:
c9594989 2702 free (alloc1);
45d6a902 2703 if (alloc2 != NULL)
4dd07732
AM
2704 {
2705 if (keep_memory)
2706 bfd_release (abfd, alloc2);
2707 else
2708 free (alloc2);
2709 }
45d6a902
AM
2710 return NULL;
2711}
2712
2713/* Compute the size of, and allocate space for, REL_HDR which is the
2714 section header for a section containing relocations for O. */
2715
0a1b45a2 2716static bool
9eaff861
AO
2717_bfd_elf_link_size_reloc_section (bfd *abfd,
2718 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2719{
9eaff861 2720 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2721
2722 /* That allows us to calculate the size of the section. */
9eaff861 2723 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2724
2725 /* The contents field must last into write_object_contents, so we
2726 allocate it with bfd_alloc rather than malloc. Also since we
2727 cannot be sure that the contents will actually be filled in,
2728 we zero the allocated space. */
a50b1753 2729 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902 2730 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
0a1b45a2 2731 return false;
45d6a902 2732
d4730f92 2733 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2734 {
2735 struct elf_link_hash_entry **p;
2736
ca4be51c
AM
2737 p = ((struct elf_link_hash_entry **)
2738 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902 2739 if (p == NULL)
0a1b45a2 2740 return false;
45d6a902 2741
d4730f92 2742 reldata->hashes = p;
45d6a902
AM
2743 }
2744
0a1b45a2 2745 return true;
45d6a902
AM
2746}
2747
2748/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2749 originated from the section given by INPUT_REL_HDR) to the
2750 OUTPUT_BFD. */
2751
0a1b45a2 2752bool
268b6b39
AM
2753_bfd_elf_link_output_relocs (bfd *output_bfd,
2754 asection *input_section,
2755 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2756 Elf_Internal_Rela *internal_relocs,
2757 struct elf_link_hash_entry **rel_hash
2758 ATTRIBUTE_UNUSED)
45d6a902
AM
2759{
2760 Elf_Internal_Rela *irela;
2761 Elf_Internal_Rela *irelaend;
2762 bfd_byte *erel;
d4730f92 2763 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2764 asection *output_section;
9c5bfbb7 2765 const struct elf_backend_data *bed;
268b6b39 2766 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2767 struct bfd_elf_section_data *esdo;
45d6a902
AM
2768
2769 output_section = input_section->output_section;
45d6a902 2770
d4730f92
BS
2771 bed = get_elf_backend_data (output_bfd);
2772 esdo = elf_section_data (output_section);
2773 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2774 {
d4730f92
BS
2775 output_reldata = &esdo->rel;
2776 swap_out = bed->s->swap_reloc_out;
45d6a902 2777 }
d4730f92
BS
2778 else if (esdo->rela.hdr
2779 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2780 {
d4730f92
BS
2781 output_reldata = &esdo->rela;
2782 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2783 }
2784 else
2785 {
4eca0228 2786 _bfd_error_handler
695344c0 2787 /* xgettext:c-format */
871b3ab2 2788 (_("%pB: relocation size mismatch in %pB section %pA"),
d003868e 2789 output_bfd, input_section->owner, input_section);
297d8443 2790 bfd_set_error (bfd_error_wrong_format);
0a1b45a2 2791 return false;
45d6a902
AM
2792 }
2793
d4730f92
BS
2794 erel = output_reldata->hdr->contents;
2795 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2796 irela = internal_relocs;
2797 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2798 * bed->s->int_rels_per_ext_rel);
2799 while (irela < irelaend)
2800 {
2801 (*swap_out) (output_bfd, irela, erel);
2802 irela += bed->s->int_rels_per_ext_rel;
2803 erel += input_rel_hdr->sh_entsize;
2804 }
2805
2806 /* Bump the counter, so that we know where to add the next set of
2807 relocations. */
d4730f92 2808 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902 2809
0a1b45a2 2810 return true;
45d6a902
AM
2811}
2812\f
508c3946
L
2813/* Make weak undefined symbols in PIE dynamic. */
2814
0a1b45a2 2815bool
508c3946
L
2816_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2817 struct elf_link_hash_entry *h)
2818{
0e1862bb 2819 if (bfd_link_pie (info)
508c3946
L
2820 && h->dynindx == -1
2821 && h->root.type == bfd_link_hash_undefweak)
2822 return bfd_elf_link_record_dynamic_symbol (info, h);
2823
0a1b45a2 2824 return true;
508c3946
L
2825}
2826
45d6a902
AM
2827/* Fix up the flags for a symbol. This handles various cases which
2828 can only be fixed after all the input files are seen. This is
2829 currently called by both adjust_dynamic_symbol and
2830 assign_sym_version, which is unnecessary but perhaps more robust in
2831 the face of future changes. */
2832
0a1b45a2 2833static bool
268b6b39
AM
2834_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2835 struct elf_info_failed *eif)
45d6a902 2836{
33774f08 2837 const struct elf_backend_data *bed;
508c3946 2838
45d6a902
AM
2839 /* If this symbol was mentioned in a non-ELF file, try to set
2840 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2841 permit a non-ELF file to correctly refer to a symbol defined in
2842 an ELF dynamic object. */
f5385ebf 2843 if (h->non_elf)
45d6a902
AM
2844 {
2845 while (h->root.type == bfd_link_hash_indirect)
2846 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2847
2848 if (h->root.type != bfd_link_hash_defined
2849 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2850 {
2851 h->ref_regular = 1;
2852 h->ref_regular_nonweak = 1;
2853 }
45d6a902
AM
2854 else
2855 {
2856 if (h->root.u.def.section->owner != NULL
2857 && (bfd_get_flavour (h->root.u.def.section->owner)
2858 == bfd_target_elf_flavour))
f5385ebf
AM
2859 {
2860 h->ref_regular = 1;
2861 h->ref_regular_nonweak = 1;
2862 }
45d6a902 2863 else
f5385ebf 2864 h->def_regular = 1;
45d6a902
AM
2865 }
2866
2867 if (h->dynindx == -1
f5385ebf
AM
2868 && (h->def_dynamic
2869 || h->ref_dynamic))
45d6a902 2870 {
c152c796 2871 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 2872 {
0a1b45a2
AM
2873 eif->failed = true;
2874 return false;
45d6a902
AM
2875 }
2876 }
2877 }
2878 else
2879 {
f5385ebf 2880 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2881 was first seen in a non-ELF file. Fortunately, if the symbol
2882 was first seen in an ELF file, we're probably OK unless the
2883 symbol was defined in a non-ELF file. Catch that case here.
2884 FIXME: We're still in trouble if the symbol was first seen in
2885 a dynamic object, and then later in a non-ELF regular object. */
2886 if ((h->root.type == bfd_link_hash_defined
2887 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2888 && !h->def_regular
45d6a902
AM
2889 && (h->root.u.def.section->owner != NULL
2890 ? (bfd_get_flavour (h->root.u.def.section->owner)
2891 != bfd_target_elf_flavour)
2892 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2893 && !h->def_dynamic)))
2894 h->def_regular = 1;
45d6a902
AM
2895 }
2896
508c3946 2897 /* Backend specific symbol fixup. */
33774f08
AM
2898 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2899 if (bed->elf_backend_fixup_symbol
2900 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
0a1b45a2 2901 return false;
508c3946 2902
45d6a902
AM
2903 /* If this is a final link, and the symbol was defined as a common
2904 symbol in a regular object file, and there was no definition in
2905 any dynamic object, then the linker will have allocated space for
f5385ebf 2906 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2907 flag will not have been set. */
2908 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2909 && !h->def_regular
2910 && h->ref_regular
2911 && !h->def_dynamic
96f29d96 2912 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2913 h->def_regular = 1;
45d6a902 2914
af0bfb9c
AM
2915 /* Symbols defined in discarded sections shouldn't be dynamic. */
2916 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
0a1b45a2 2917 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
af0bfb9c 2918
4deb8f71
L
2919 /* If a weak undefined symbol has non-default visibility, we also
2920 hide it from the dynamic linker. */
af0bfb9c
AM
2921 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2922 && h->root.type == bfd_link_hash_undefweak)
0a1b45a2 2923 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
4deb8f71
L
2924
2925 /* A hidden versioned symbol in executable should be forced local if
2926 it is is locally defined, not referenced by shared library and not
2927 exported. */
2928 else if (bfd_link_executable (eif->info)
2929 && h->versioned == versioned_hidden
2930 && !eif->info->export_dynamic
2931 && !h->dynamic
2932 && !h->ref_dynamic
2933 && h->def_regular)
0a1b45a2 2934 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
4deb8f71 2935
45d6a902
AM
2936 /* If -Bsymbolic was used (which means to bind references to global
2937 symbols to the definition within the shared object), and this
2938 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2939 need a PLT entry. Likewise, if the symbol has non-default
2940 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2941 will force it local. */
4deb8f71
L
2942 else if (h->needs_plt
2943 && bfd_link_pic (eif->info)
2944 && is_elf_hash_table (eif->info->hash)
2945 && (SYMBOLIC_BIND (eif->info, h)
2946 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2947 && h->def_regular)
45d6a902 2948 {
0a1b45a2 2949 bool force_local;
45d6a902 2950
45d6a902
AM
2951 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2952 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2953 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2954 }
2955
45d6a902
AM
2956 /* If this is a weak defined symbol in a dynamic object, and we know
2957 the real definition in the dynamic object, copy interesting flags
2958 over to the real definition. */
60d67dc8 2959 if (h->is_weakalias)
45d6a902 2960 {
60d67dc8
AM
2961 struct elf_link_hash_entry *def = weakdef (h);
2962
45d6a902
AM
2963 /* If the real definition is defined by a regular object file,
2964 don't do anything special. See the longer description in
5b9d7a9a
AM
2965 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
2966 bfd_link_hash_defined as it was when put on the alias list
2967 then it must have originally been a versioned symbol (for
2968 which a non-versioned indirect symbol is created) and later
2969 a definition for the non-versioned symbol is found. In that
2970 case the indirection is flipped with the versioned symbol
2971 becoming an indirect pointing at the non-versioned symbol.
2972 Thus, not an alias any more. */
2973 if (def->def_regular
2974 || def->root.type != bfd_link_hash_defined)
60d67dc8
AM
2975 {
2976 h = def;
2977 while ((h = h->u.alias) != def)
2978 h->is_weakalias = 0;
2979 }
45d6a902 2980 else
a26587ba 2981 {
4e6b54a6
AM
2982 while (h->root.type == bfd_link_hash_indirect)
2983 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4e6b54a6
AM
2984 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2985 || h->root.type == bfd_link_hash_defweak);
60d67dc8 2986 BFD_ASSERT (def->def_dynamic);
60d67dc8 2987 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
a26587ba 2988 }
45d6a902
AM
2989 }
2990
0a1b45a2 2991 return true;
45d6a902
AM
2992}
2993
2994/* Make the backend pick a good value for a dynamic symbol. This is
2995 called via elf_link_hash_traverse, and also calls itself
2996 recursively. */
2997
0a1b45a2 2998static bool
268b6b39 2999_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 3000{
a50b1753 3001 struct elf_info_failed *eif = (struct elf_info_failed *) data;
559192d8 3002 struct elf_link_hash_table *htab;
9c5bfbb7 3003 const struct elf_backend_data *bed;
45d6a902 3004
0eddce27 3005 if (! is_elf_hash_table (eif->info->hash))
0a1b45a2 3006 return false;
45d6a902 3007
45d6a902
AM
3008 /* Ignore indirect symbols. These are added by the versioning code. */
3009 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 3010 return true;
45d6a902
AM
3011
3012 /* Fix the symbol flags. */
3013 if (! _bfd_elf_fix_symbol_flags (h, eif))
0a1b45a2 3014 return false;
45d6a902 3015
559192d8
AM
3016 htab = elf_hash_table (eif->info);
3017 bed = get_elf_backend_data (htab->dynobj);
3018
954b63d4
AM
3019 if (h->root.type == bfd_link_hash_undefweak)
3020 {
3021 if (eif->info->dynamic_undefined_weak == 0)
0a1b45a2 3022 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
954b63d4
AM
3023 else if (eif->info->dynamic_undefined_weak > 0
3024 && h->ref_regular
3025 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3026 && !bfd_hide_sym_by_version (eif->info->version_info,
3027 h->root.root.string))
3028 {
3029 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
3030 {
0a1b45a2
AM
3031 eif->failed = true;
3032 return false;
954b63d4
AM
3033 }
3034 }
3035 }
3036
45d6a902
AM
3037 /* If this symbol does not require a PLT entry, and it is not
3038 defined by a dynamic object, or is not referenced by a regular
3039 object, ignore it. We do have to handle a weak defined symbol,
3040 even if no regular object refers to it, if we decided to add it
3041 to the dynamic symbol table. FIXME: Do we normally need to worry
3042 about symbols which are defined by one dynamic object and
3043 referenced by another one? */
f5385ebf 3044 if (!h->needs_plt
91e21fb7 3045 && h->type != STT_GNU_IFUNC
f5385ebf
AM
3046 && (h->def_regular
3047 || !h->def_dynamic
3048 || (!h->ref_regular
60d67dc8 3049 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
45d6a902 3050 {
a6aa5195 3051 h->plt = elf_hash_table (eif->info)->init_plt_offset;
0a1b45a2 3052 return true;
45d6a902
AM
3053 }
3054
3055 /* If we've already adjusted this symbol, don't do it again. This
3056 can happen via a recursive call. */
f5385ebf 3057 if (h->dynamic_adjusted)
0a1b45a2 3058 return true;
45d6a902
AM
3059
3060 /* Don't look at this symbol again. Note that we must set this
3061 after checking the above conditions, because we may look at a
3062 symbol once, decide not to do anything, and then get called
3063 recursively later after REF_REGULAR is set below. */
f5385ebf 3064 h->dynamic_adjusted = 1;
45d6a902
AM
3065
3066 /* If this is a weak definition, and we know a real definition, and
3067 the real symbol is not itself defined by a regular object file,
3068 then get a good value for the real definition. We handle the
3069 real symbol first, for the convenience of the backend routine.
3070
3071 Note that there is a confusing case here. If the real definition
3072 is defined by a regular object file, we don't get the real symbol
3073 from the dynamic object, but we do get the weak symbol. If the
3074 processor backend uses a COPY reloc, then if some routine in the
3075 dynamic object changes the real symbol, we will not see that
3076 change in the corresponding weak symbol. This is the way other
3077 ELF linkers work as well, and seems to be a result of the shared
3078 library model.
3079
3080 I will clarify this issue. Most SVR4 shared libraries define the
3081 variable _timezone and define timezone as a weak synonym. The
3082 tzset call changes _timezone. If you write
3083 extern int timezone;
3084 int _timezone = 5;
3085 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3086 you might expect that, since timezone is a synonym for _timezone,
3087 the same number will print both times. However, if the processor
3088 backend uses a COPY reloc, then actually timezone will be copied
3089 into your process image, and, since you define _timezone
3090 yourself, _timezone will not. Thus timezone and _timezone will
3091 wind up at different memory locations. The tzset call will set
3092 _timezone, leaving timezone unchanged. */
3093
60d67dc8 3094 if (h->is_weakalias)
45d6a902 3095 {
60d67dc8
AM
3096 struct elf_link_hash_entry *def = weakdef (h);
3097
ec24dc88 3098 /* If we get to this point, there is an implicit reference to
60d67dc8
AM
3099 the alias by a regular object file via the weak symbol H. */
3100 def->ref_regular = 1;
45d6a902 3101
ec24dc88 3102 /* Ensure that the backend adjust_dynamic_symbol function sees
60d67dc8
AM
3103 the strong alias before H by recursively calling ourselves. */
3104 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
0a1b45a2 3105 return false;
45d6a902
AM
3106 }
3107
3108 /* If a symbol has no type and no size and does not require a PLT
3109 entry, then we are probably about to do the wrong thing here: we
3110 are probably going to create a COPY reloc for an empty object.
3111 This case can arise when a shared object is built with assembly
3112 code, and the assembly code fails to set the symbol type. */
3113 if (h->size == 0
3114 && h->type == STT_NOTYPE
f5385ebf 3115 && !h->needs_plt)
4eca0228 3116 _bfd_error_handler
45d6a902
AM
3117 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3118 h->root.root.string);
3119
45d6a902
AM
3120 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3121 {
0a1b45a2
AM
3122 eif->failed = true;
3123 return false;
45d6a902
AM
3124 }
3125
0a1b45a2 3126 return true;
45d6a902
AM
3127}
3128
027297b7
L
3129/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3130 DYNBSS. */
3131
0a1b45a2 3132bool
6cabe1ea
AM
3133_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3134 struct elf_link_hash_entry *h,
027297b7
L
3135 asection *dynbss)
3136{
91ac5911 3137 unsigned int power_of_two;
027297b7
L
3138 bfd_vma mask;
3139 asection *sec = h->root.u.def.section;
3140
de194d85 3141 /* The section alignment of the definition is the maximum alignment
91ac5911
L
3142 requirement of symbols defined in the section. Since we don't
3143 know the symbol alignment requirement, we start with the
3144 maximum alignment and check low bits of the symbol address
3145 for the minimum alignment. */
fd361982 3146 power_of_two = bfd_section_alignment (sec);
91ac5911
L
3147 mask = ((bfd_vma) 1 << power_of_two) - 1;
3148 while ((h->root.u.def.value & mask) != 0)
3149 {
3150 mask >>= 1;
3151 --power_of_two;
3152 }
027297b7 3153
fd361982 3154 if (power_of_two > bfd_section_alignment (dynbss))
027297b7
L
3155 {
3156 /* Adjust the section alignment if needed. */
fd361982 3157 if (!bfd_set_section_alignment (dynbss, power_of_two))
0a1b45a2 3158 return false;
027297b7
L
3159 }
3160
91ac5911 3161 /* We make sure that the symbol will be aligned properly. */
027297b7
L
3162 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3163
3164 /* Define the symbol as being at this point in DYNBSS. */
3165 h->root.u.def.section = dynbss;
3166 h->root.u.def.value = dynbss->size;
3167
3168 /* Increment the size of DYNBSS to make room for the symbol. */
3169 dynbss->size += h->size;
3170
f7483970
L
3171 /* No error if extern_protected_data is true. */
3172 if (h->protected_def
889c2a67
L
3173 && (!info->extern_protected_data
3174 || (info->extern_protected_data < 0
3175 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05 3176 info->callbacks->einfo
c1c8c1ef 3177 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
d07a1b05 3178 h->root.root.string);
6cabe1ea 3179
0a1b45a2 3180 return true;
027297b7
L
3181}
3182
45d6a902
AM
3183/* Adjust all external symbols pointing into SEC_MERGE sections
3184 to reflect the object merging within the sections. */
3185
0a1b45a2 3186static bool
268b6b39 3187_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
3188{
3189 asection *sec;
3190
45d6a902
AM
3191 if ((h->root.type == bfd_link_hash_defined
3192 || h->root.type == bfd_link_hash_defweak)
3193 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 3194 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 3195 {
a50b1753 3196 bfd *output_bfd = (bfd *) data;
45d6a902
AM
3197
3198 h->root.u.def.value =
3199 _bfd_merged_section_offset (output_bfd,
3200 &h->root.u.def.section,
3201 elf_section_data (sec)->sec_info,
753731ee 3202 h->root.u.def.value);
45d6a902
AM
3203 }
3204
0a1b45a2 3205 return true;
45d6a902 3206}
986a241f
RH
3207
3208/* Returns false if the symbol referred to by H should be considered
3209 to resolve local to the current module, and true if it should be
3210 considered to bind dynamically. */
3211
0a1b45a2 3212bool
268b6b39
AM
3213_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3214 struct bfd_link_info *info,
0a1b45a2 3215 bool not_local_protected)
986a241f 3216{
0a1b45a2 3217 bool binding_stays_local_p;
fcb93ecf
PB
3218 const struct elf_backend_data *bed;
3219 struct elf_link_hash_table *hash_table;
986a241f
RH
3220
3221 if (h == NULL)
0a1b45a2 3222 return false;
986a241f
RH
3223
3224 while (h->root.type == bfd_link_hash_indirect
3225 || h->root.type == bfd_link_hash_warning)
3226 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3227
3228 /* If it was forced local, then clearly it's not dynamic. */
3229 if (h->dynindx == -1)
0a1b45a2 3230 return false;
f5385ebf 3231 if (h->forced_local)
0a1b45a2 3232 return false;
986a241f
RH
3233
3234 /* Identify the cases where name binding rules say that a
3235 visible symbol resolves locally. */
0e1862bb
L
3236 binding_stays_local_p = (bfd_link_executable (info)
3237 || SYMBOLIC_BIND (info, h));
986a241f
RH
3238
3239 switch (ELF_ST_VISIBILITY (h->other))
3240 {
3241 case STV_INTERNAL:
3242 case STV_HIDDEN:
0a1b45a2 3243 return false;
986a241f
RH
3244
3245 case STV_PROTECTED:
fcb93ecf 3246 hash_table = elf_hash_table (info);
2cc15b10 3247 if (!is_elf_hash_table (&hash_table->root))
0a1b45a2 3248 return false;
fcb93ecf
PB
3249
3250 bed = get_elf_backend_data (hash_table->dynobj);
3251
986a241f
RH
3252 /* Proper resolution for function pointer equality may require
3253 that these symbols perhaps be resolved dynamically, even though
3254 we should be resolving them to the current module. */
89a2ee5a 3255 if (!not_local_protected || !bed->is_function_type (h->type))
0a1b45a2 3256 binding_stays_local_p = true;
986a241f
RH
3257 break;
3258
3259 default:
986a241f
RH
3260 break;
3261 }
3262
aa37626c 3263 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 3264 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
0a1b45a2 3265 return true;
aa37626c 3266
986a241f
RH
3267 /* Otherwise, the symbol is dynamic if binding rules don't tell
3268 us that it remains local. */
3269 return !binding_stays_local_p;
3270}
f6c52c13
AM
3271
3272/* Return true if the symbol referred to by H should be considered
3273 to resolve local to the current module, and false otherwise. Differs
3274 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 3275 undefined symbols. The two functions are virtually identical except
0fad2956
MR
3276 for the place where dynindx == -1 is tested. If that test is true,
3277 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3278 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3279 defined symbols.
89a2ee5a
AM
3280 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3281 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3282 treatment of undefined weak symbols. For those that do not make
3283 undefined weak symbols dynamic, both functions may return false. */
f6c52c13 3284
0a1b45a2 3285bool
268b6b39
AM
3286_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3287 struct bfd_link_info *info,
0a1b45a2 3288 bool local_protected)
f6c52c13 3289{
fcb93ecf
PB
3290 const struct elf_backend_data *bed;
3291 struct elf_link_hash_table *hash_table;
3292
f6c52c13
AM
3293 /* If it's a local sym, of course we resolve locally. */
3294 if (h == NULL)
0a1b45a2 3295 return true;
f6c52c13 3296
d95edcac
L
3297 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3298 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3299 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
0a1b45a2 3300 return true;
d95edcac 3301
0fad2956
MR
3302 /* Forced local symbols resolve locally. */
3303 if (h->forced_local)
0a1b45a2 3304 return true;
0fad2956 3305
7e2294f9
AO
3306 /* Common symbols that become definitions don't get the DEF_REGULAR
3307 flag set, so test it first, and don't bail out. */
3308 if (ELF_COMMON_DEF_P (h))
3309 /* Do nothing. */;
f6c52c13 3310 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
3311 resolve locally. The sym is either undefined or dynamic. */
3312 else if (!h->def_regular)
0a1b45a2 3313 return false;
f6c52c13 3314
0fad2956 3315 /* Non-dynamic symbols resolve locally. */
f6c52c13 3316 if (h->dynindx == -1)
0a1b45a2 3317 return true;
f6c52c13
AM
3318
3319 /* At this point, we know the symbol is defined and dynamic. In an
3320 executable it must resolve locally, likewise when building symbolic
3321 shared libraries. */
0e1862bb 3322 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
0a1b45a2 3323 return true;
f6c52c13
AM
3324
3325 /* Now deal with defined dynamic symbols in shared libraries. Ones
3326 with default visibility might not resolve locally. */
3327 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
0a1b45a2 3328 return false;
f6c52c13 3329
fcb93ecf 3330 hash_table = elf_hash_table (info);
2cc15b10 3331 if (!is_elf_hash_table (&hash_table->root))
0a1b45a2 3332 return true;
fcb93ecf
PB
3333
3334 bed = get_elf_backend_data (hash_table->dynobj);
3335
f7483970
L
3336 /* If extern_protected_data is false, STV_PROTECTED non-function
3337 symbols are local. */
889c2a67
L
3338 if ((!info->extern_protected_data
3339 || (info->extern_protected_data < 0
3340 && !bed->extern_protected_data))
3341 && !bed->is_function_type (h->type))
0a1b45a2 3342 return true;
1c16dfa5 3343
f6c52c13 3344 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
3345 symbols be treated as dynamic symbols. If the address of a
3346 function not defined in an executable is set to that function's
3347 plt entry in the executable, then the address of the function in
3348 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
3349 return local_protected;
3350}
e1918d23
AM
3351
3352/* Caches some TLS segment info, and ensures that the TLS segment vma is
3353 aligned. Returns the first TLS output section. */
3354
3355struct bfd_section *
3356_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3357{
3358 struct bfd_section *sec, *tls;
3359 unsigned int align = 0;
3360
3361 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3362 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3363 break;
3364 tls = sec;
3365
3366 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3367 if (sec->alignment_power > align)
3368 align = sec->alignment_power;
3369
3370 elf_hash_table (info)->tls_sec = tls;
3371
fdde2fb6
SH
3372 /* Ensure the alignment of the first section (usually .tdata) is the largest
3373 alignment, so that the tls segment starts aligned. */
e1918d23
AM
3374 if (tls != NULL)
3375 tls->alignment_power = align;
3376
3377 return tls;
3378}
0ad989f9
L
3379
3380/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
0a1b45a2 3381static bool
0ad989f9
L
3382is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3383 Elf_Internal_Sym *sym)
3384{
a4d8e49b
L
3385 const struct elf_backend_data *bed;
3386
0ad989f9
L
3387 /* Local symbols do not count, but target specific ones might. */
3388 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3389 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
0a1b45a2 3390 return false;
0ad989f9 3391
fcb93ecf 3392 bed = get_elf_backend_data (abfd);
0ad989f9 3393 /* Function symbols do not count. */
fcb93ecf 3394 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0a1b45a2 3395 return false;
0ad989f9
L
3396
3397 /* If the section is undefined, then so is the symbol. */
3398 if (sym->st_shndx == SHN_UNDEF)
0a1b45a2 3399 return false;
0ad989f9
L
3400
3401 /* If the symbol is defined in the common section, then
3402 it is a common definition and so does not count. */
a4d8e49b 3403 if (bed->common_definition (sym))
0a1b45a2 3404 return false;
0ad989f9
L
3405
3406 /* If the symbol is in a target specific section then we
3407 must rely upon the backend to tell us what it is. */
3408 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3409 /* FIXME - this function is not coded yet:
3410
3411 return _bfd_is_global_symbol_definition (abfd, sym);
3412
3413 Instead for now assume that the definition is not global,
3414 Even if this is wrong, at least the linker will behave
3415 in the same way that it used to do. */
0a1b45a2 3416 return false;
0ad989f9 3417
0a1b45a2 3418 return true;
0ad989f9
L
3419}
3420
3421/* Search the symbol table of the archive element of the archive ABFD
3422 whose archive map contains a mention of SYMDEF, and determine if
3423 the symbol is defined in this element. */
0a1b45a2 3424static bool
0ad989f9
L
3425elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3426{
3427 Elf_Internal_Shdr * hdr;
ef53be89
AM
3428 size_t symcount;
3429 size_t extsymcount;
3430 size_t extsymoff;
0ad989f9
L
3431 Elf_Internal_Sym *isymbuf;
3432 Elf_Internal_Sym *isym;
3433 Elf_Internal_Sym *isymend;
0a1b45a2 3434 bool result;
0ad989f9
L
3435
3436 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3437 if (abfd == NULL)
0a1b45a2 3438 return false;
0ad989f9
L
3439
3440 if (! bfd_check_format (abfd, bfd_object))
0a1b45a2 3441 return false;
0ad989f9 3442
7dc3990e
L
3443 /* Select the appropriate symbol table. If we don't know if the
3444 object file is an IR object, give linker LTO plugin a chance to
3445 get the correct symbol table. */
3446 if (abfd->plugin_format == bfd_plugin_yes
08ce1d72 3447#if BFD_SUPPORTS_PLUGINS
7dc3990e
L
3448 || (abfd->plugin_format == bfd_plugin_unknown
3449 && bfd_link_plugin_object_p (abfd))
3450#endif
3451 )
3452 {
3453 /* Use the IR symbol table if the object has been claimed by
3454 plugin. */
3455 abfd = abfd->plugin_dummy_bfd;
3456 hdr = &elf_tdata (abfd)->symtab_hdr;
3457 }
3458 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
0ad989f9
L
3459 hdr = &elf_tdata (abfd)->symtab_hdr;
3460 else
3461 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3462
3463 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3464
3465 /* The sh_info field of the symtab header tells us where the
3466 external symbols start. We don't care about the local symbols. */
3467 if (elf_bad_symtab (abfd))
3468 {
3469 extsymcount = symcount;
3470 extsymoff = 0;
3471 }
3472 else
3473 {
3474 extsymcount = symcount - hdr->sh_info;
3475 extsymoff = hdr->sh_info;
3476 }
3477
3478 if (extsymcount == 0)
0a1b45a2 3479 return false;
0ad989f9
L
3480
3481 /* Read in the symbol table. */
3482 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3483 NULL, NULL, NULL);
3484 if (isymbuf == NULL)
0a1b45a2 3485 return false;
0ad989f9
L
3486
3487 /* Scan the symbol table looking for SYMDEF. */
0a1b45a2 3488 result = false;
0ad989f9
L
3489 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3490 {
3491 const char *name;
3492
3493 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3494 isym->st_name);
3495 if (name == NULL)
3496 break;
3497
3498 if (strcmp (name, symdef->name) == 0)
3499 {
3500 result = is_global_data_symbol_definition (abfd, isym);
3501 break;
3502 }
3503 }
3504
3505 free (isymbuf);
3506
3507 return result;
3508}
3509\f
5a580b3a
AM
3510/* Add an entry to the .dynamic table. */
3511
0a1b45a2 3512bool
5a580b3a
AM
3513_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3514 bfd_vma tag,
3515 bfd_vma val)
3516{
3517 struct elf_link_hash_table *hash_table;
3518 const struct elf_backend_data *bed;
3519 asection *s;
3520 bfd_size_type newsize;
3521 bfd_byte *newcontents;
3522 Elf_Internal_Dyn dyn;
3523
3524 hash_table = elf_hash_table (info);
2cc15b10 3525 if (! is_elf_hash_table (&hash_table->root))
0a1b45a2 3526 return false;
5a580b3a 3527
7f923b7f 3528 if (tag == DT_RELA || tag == DT_REL)
0a1b45a2 3529 hash_table->dynamic_relocs = true;
7f923b7f 3530
5a580b3a 3531 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3532 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3533 BFD_ASSERT (s != NULL);
3534
eea6121a 3535 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3536 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a 3537 if (newcontents == NULL)
0a1b45a2 3538 return false;
5a580b3a
AM
3539
3540 dyn.d_tag = tag;
3541 dyn.d_un.d_val = val;
eea6121a 3542 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3543
eea6121a 3544 s->size = newsize;
5a580b3a
AM
3545 s->contents = newcontents;
3546
0a1b45a2 3547 return true;
5a580b3a
AM
3548}
3549
6f6fd151
L
3550/* Strip zero-sized dynamic sections. */
3551
0a1b45a2 3552bool
6f6fd151
L
3553_bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3554{
3555 struct elf_link_hash_table *hash_table;
3556 const struct elf_backend_data *bed;
3557 asection *s, *sdynamic, **pp;
3558 asection *rela_dyn, *rel_dyn;
3559 Elf_Internal_Dyn dyn;
3560 bfd_byte *extdyn, *next;
3561 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
0a1b45a2
AM
3562 bool strip_zero_sized;
3563 bool strip_zero_sized_plt;
6f6fd151
L
3564
3565 if (bfd_link_relocatable (info))
0a1b45a2 3566 return true;
6f6fd151
L
3567
3568 hash_table = elf_hash_table (info);
2cc15b10 3569 if (!is_elf_hash_table (&hash_table->root))
0a1b45a2 3570 return false;
6f6fd151
L
3571
3572 if (!hash_table->dynobj)
0a1b45a2 3573 return true;
6f6fd151
L
3574
3575 sdynamic= bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3576 if (!sdynamic)
0a1b45a2 3577 return true;
6f6fd151
L
3578
3579 bed = get_elf_backend_data (hash_table->dynobj);
3580 swap_dyn_in = bed->s->swap_dyn_in;
3581
0a1b45a2
AM
3582 strip_zero_sized = false;
3583 strip_zero_sized_plt = false;
6f6fd151
L
3584
3585 /* Strip zero-sized dynamic sections. */
3586 rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3587 rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3588 for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3589 if (s->size == 0
3590 && (s == rela_dyn
3591 || s == rel_dyn
3592 || s == hash_table->srelplt->output_section
3593 || s == hash_table->splt->output_section))
3594 {
3595 *pp = s->next;
3596 info->output_bfd->section_count--;
0a1b45a2 3597 strip_zero_sized = true;
6f6fd151
L
3598 if (s == rela_dyn)
3599 s = rela_dyn;
3600 if (s == rel_dyn)
3601 s = rel_dyn;
3602 else if (s == hash_table->splt->output_section)
3603 {
3604 s = hash_table->splt;
0a1b45a2 3605 strip_zero_sized_plt = true;
6f6fd151
L
3606 }
3607 else
3608 s = hash_table->srelplt;
3609 s->flags |= SEC_EXCLUDE;
3610 s->output_section = bfd_abs_section_ptr;
3611 }
3612 else
3613 pp = &s->next;
3614
3615 if (strip_zero_sized_plt)
3616 for (extdyn = sdynamic->contents;
3617 extdyn < sdynamic->contents + sdynamic->size;
3618 extdyn = next)
3619 {
3620 next = extdyn + bed->s->sizeof_dyn;
3621 swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3622 switch (dyn.d_tag)
3623 {
3624 default:
3625 break;
3626 case DT_JMPREL:
3627 case DT_PLTRELSZ:
3628 case DT_PLTREL:
3629 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3630 the procedure linkage table (the .plt section) has been
3631 removed. */
3632 memmove (extdyn, next,
3633 sdynamic->size - (next - sdynamic->contents));
3634 next = extdyn;
3635 }
3636 }
3637
3638 if (strip_zero_sized)
3639 {
3640 /* Regenerate program headers. */
3641 elf_seg_map (info->output_bfd) = NULL;
3642 return _bfd_elf_map_sections_to_segments (info->output_bfd, info);
3643 }
3644
0a1b45a2 3645 return true;
6f6fd151
L
3646}
3647
e310298c 3648/* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
5a580b3a
AM
3649 1 if a DT_NEEDED tag already exists, and 0 on success. */
3650
e310298c
AM
3651int
3652bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
5a580b3a
AM
3653{
3654 struct elf_link_hash_table *hash_table;
ef53be89 3655 size_t strindex;
e310298c 3656 const char *soname;
5a580b3a 3657
7e9f0867
AM
3658 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3659 return -1;
3660
5a580b3a 3661 hash_table = elf_hash_table (info);
e310298c 3662 soname = elf_dt_name (abfd);
0a1b45a2 3663 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, false);
ef53be89 3664 if (strindex == (size_t) -1)
5a580b3a
AM
3665 return -1;
3666
02be4619 3667 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3668 {
3669 asection *sdyn;
3670 const struct elf_backend_data *bed;
3671 bfd_byte *extdyn;
3672
3673 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3674 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3675 if (sdyn != NULL)
3676 for (extdyn = sdyn->contents;
3677 extdyn < sdyn->contents + sdyn->size;
3678 extdyn += bed->s->sizeof_dyn)
3679 {
3680 Elf_Internal_Dyn dyn;
5a580b3a 3681
7e9f0867
AM
3682 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3683 if (dyn.d_tag == DT_NEEDED
3684 && dyn.d_un.d_val == strindex)
3685 {
3686 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3687 return 1;
3688 }
3689 }
5a580b3a
AM
3690 }
3691
e310298c
AM
3692 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3693 return -1;
7e9f0867 3694
e310298c
AM
3695 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3696 return -1;
5a580b3a
AM
3697
3698 return 0;
3699}
3700
7b15fa7a
AM
3701/* Return true if SONAME is on the needed list between NEEDED and STOP
3702 (or the end of list if STOP is NULL), and needed by a library that
3703 will be loaded. */
3704
0a1b45a2 3705static bool
7b15fa7a
AM
3706on_needed_list (const char *soname,
3707 struct bfd_link_needed_list *needed,
3708 struct bfd_link_needed_list *stop)
010e5ae2 3709{
7b15fa7a
AM
3710 struct bfd_link_needed_list *look;
3711 for (look = needed; look != stop; look = look->next)
3712 if (strcmp (soname, look->name) == 0
3713 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3714 /* If needed by a library that itself is not directly
3715 needed, recursively check whether that library is
3716 indirectly needed. Since we add DT_NEEDED entries to
3717 the end of the list, library dependencies appear after
3718 the library. Therefore search prior to the current
3719 LOOK, preventing possible infinite recursion. */
3720 || on_needed_list (elf_dt_name (look->by), needed, look)))
0a1b45a2 3721 return true;
010e5ae2 3722
0a1b45a2 3723 return false;
010e5ae2
AM
3724}
3725
3a3f4bf7 3726/* Sort symbol by value, section, size, and type. */
4ad4eba5
AM
3727static int
3728elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3729{
3730 const struct elf_link_hash_entry *h1;
3731 const struct elf_link_hash_entry *h2;
10b7e05b 3732 bfd_signed_vma vdiff;
3a3f4bf7
AM
3733 int sdiff;
3734 const char *n1;
3735 const char *n2;
5a580b3a
AM
3736
3737 h1 = *(const struct elf_link_hash_entry **) arg1;
3738 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3739 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3740 if (vdiff != 0)
3741 return vdiff > 0 ? 1 : -1;
3a3f4bf7
AM
3742
3743 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3744 if (sdiff != 0)
3745 return sdiff;
3746
3747 /* Sort so that sized symbols are selected over zero size symbols. */
3748 vdiff = h1->size - h2->size;
3749 if (vdiff != 0)
3750 return vdiff > 0 ? 1 : -1;
3751
3752 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3753 if (h1->type != h2->type)
3754 return h1->type - h2->type;
3755
3756 /* If symbols are properly sized and typed, and multiple strong
3757 aliases are not defined in a shared library by the user we
3758 shouldn't get here. Unfortunately linker script symbols like
3759 __bss_start sometimes match a user symbol defined at the start of
3760 .bss without proper size and type. We'd like to preference the
3761 user symbol over reserved system symbols. Sort on leading
3762 underscores. */
3763 n1 = h1->root.root.string;
3764 n2 = h2->root.root.string;
3765 while (*n1 == *n2)
10b7e05b 3766 {
3a3f4bf7
AM
3767 if (*n1 == 0)
3768 break;
3769 ++n1;
3770 ++n2;
10b7e05b 3771 }
3a3f4bf7
AM
3772 if (*n1 == '_')
3773 return -1;
3774 if (*n2 == '_')
3775 return 1;
3776
3777 /* Final sort on name selects user symbols like '_u' over reserved
3778 system symbols like '_Z' and also will avoid qsort instability. */
3779 return *n1 - *n2;
5a580b3a 3780}
4ad4eba5 3781
5a580b3a
AM
3782/* This function is used to adjust offsets into .dynstr for
3783 dynamic symbols. This is called via elf_link_hash_traverse. */
3784
0a1b45a2 3785static bool
5a580b3a
AM
3786elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3787{
a50b1753 3788 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3789
5a580b3a
AM
3790 if (h->dynindx != -1)
3791 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
0a1b45a2 3792 return true;
5a580b3a
AM
3793}
3794
3795/* Assign string offsets in .dynstr, update all structures referencing
3796 them. */
3797
0a1b45a2 3798static bool
4ad4eba5 3799elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3800{
3801 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3802 struct elf_link_local_dynamic_entry *entry;
3803 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3804 bfd *dynobj = hash_table->dynobj;
3805 asection *sdyn;
3806 bfd_size_type size;
3807 const struct elf_backend_data *bed;
3808 bfd_byte *extdyn;
3809
3810 _bfd_elf_strtab_finalize (dynstr);
3811 size = _bfd_elf_strtab_size (dynstr);
3812
3d16b64e
NA
3813 /* Allow the linker to examine the dynsymtab now it's fully populated. */
3814
3815 if (info->callbacks->examine_strtab)
3816 info->callbacks->examine_strtab (dynstr);
3817
5a580b3a 3818 bed = get_elf_backend_data (dynobj);
3d4d4302 3819 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3820 BFD_ASSERT (sdyn != NULL);
3821
3822 /* Update all .dynamic entries referencing .dynstr strings. */
3823 for (extdyn = sdyn->contents;
eea6121a 3824 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3825 extdyn += bed->s->sizeof_dyn)
3826 {
3827 Elf_Internal_Dyn dyn;
3828
3829 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3830 switch (dyn.d_tag)
3831 {
3832 case DT_STRSZ:
3833 dyn.d_un.d_val = size;
3834 break;
3835 case DT_NEEDED:
3836 case DT_SONAME:
3837 case DT_RPATH:
3838 case DT_RUNPATH:
3839 case DT_FILTER:
3840 case DT_AUXILIARY:
7ee314fa
AM
3841 case DT_AUDIT:
3842 case DT_DEPAUDIT:
5a580b3a
AM
3843 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3844 break;
3845 default:
3846 continue;
3847 }
3848 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3849 }
3850
3851 /* Now update local dynamic symbols. */
3852 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3853 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3854 entry->isym.st_name);
3855
3856 /* And the rest of dynamic symbols. */
3857 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3858
3859 /* Adjust version definitions. */
3860 if (elf_tdata (output_bfd)->cverdefs)
3861 {
3862 asection *s;
3863 bfd_byte *p;
ef53be89 3864 size_t i;
5a580b3a
AM
3865 Elf_Internal_Verdef def;
3866 Elf_Internal_Verdaux defaux;
3867
3d4d4302 3868 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3869 p = s->contents;
3870 do
3871 {
3872 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3873 &def);
3874 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3875 if (def.vd_aux != sizeof (Elf_External_Verdef))
3876 continue;
5a580b3a
AM
3877 for (i = 0; i < def.vd_cnt; ++i)
3878 {
3879 _bfd_elf_swap_verdaux_in (output_bfd,
3880 (Elf_External_Verdaux *) p, &defaux);
3881 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3882 defaux.vda_name);
3883 _bfd_elf_swap_verdaux_out (output_bfd,
3884 &defaux, (Elf_External_Verdaux *) p);
3885 p += sizeof (Elf_External_Verdaux);
3886 }
3887 }
3888 while (def.vd_next);
3889 }
3890
3891 /* Adjust version references. */
3892 if (elf_tdata (output_bfd)->verref)
3893 {
3894 asection *s;
3895 bfd_byte *p;
ef53be89 3896 size_t i;
5a580b3a
AM
3897 Elf_Internal_Verneed need;
3898 Elf_Internal_Vernaux needaux;
3899
3d4d4302 3900 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3901 p = s->contents;
3902 do
3903 {
3904 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3905 &need);
3906 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3907 _bfd_elf_swap_verneed_out (output_bfd, &need,
3908 (Elf_External_Verneed *) p);
3909 p += sizeof (Elf_External_Verneed);
3910 for (i = 0; i < need.vn_cnt; ++i)
3911 {
3912 _bfd_elf_swap_vernaux_in (output_bfd,
3913 (Elf_External_Vernaux *) p, &needaux);
3914 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3915 needaux.vna_name);
3916 _bfd_elf_swap_vernaux_out (output_bfd,
3917 &needaux,
3918 (Elf_External_Vernaux *) p);
3919 p += sizeof (Elf_External_Vernaux);
3920 }
3921 }
3922 while (need.vn_next);
3923 }
3924
0a1b45a2 3925 return true;
5a580b3a
AM
3926}
3927\f
13285a1b
AM
3928/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3929 The default is to only match when the INPUT and OUTPUT are exactly
3930 the same target. */
3931
0a1b45a2 3932bool
13285a1b
AM
3933_bfd_elf_default_relocs_compatible (const bfd_target *input,
3934 const bfd_target *output)
3935{
3936 return input == output;
3937}
3938
3939/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3940 This version is used when different targets for the same architecture
3941 are virtually identical. */
3942
0a1b45a2 3943bool
13285a1b
AM
3944_bfd_elf_relocs_compatible (const bfd_target *input,
3945 const bfd_target *output)
3946{
3947 const struct elf_backend_data *obed, *ibed;
3948
3949 if (input == output)
0a1b45a2 3950 return true;
13285a1b
AM
3951
3952 ibed = xvec_get_elf_backend_data (input);
3953 obed = xvec_get_elf_backend_data (output);
3954
3955 if (ibed->arch != obed->arch)
0a1b45a2 3956 return false;
13285a1b
AM
3957
3958 /* If both backends are using this function, deem them compatible. */
3959 return ibed->relocs_compatible == obed->relocs_compatible;
3960}
3961
e5034e59
AM
3962/* Make a special call to the linker "notice" function to tell it that
3963 we are about to handle an as-needed lib, or have finished
1b786873 3964 processing the lib. */
e5034e59 3965
0a1b45a2 3966bool
e5034e59
AM
3967_bfd_elf_notice_as_needed (bfd *ibfd,
3968 struct bfd_link_info *info,
3969 enum notice_asneeded_action act)
3970{
46135103 3971 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3972}
3973
d9689752
L
3974/* Check relocations an ELF object file. */
3975
0a1b45a2 3976bool
d9689752
L
3977_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3978{
3979 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3980 struct elf_link_hash_table *htab = elf_hash_table (info);
3981
3982 /* If this object is the same format as the output object, and it is
3983 not a shared library, then let the backend look through the
3984 relocs.
3985
3986 This is required to build global offset table entries and to
3987 arrange for dynamic relocs. It is not required for the
3988 particular common case of linking non PIC code, even when linking
3989 against shared libraries, but unfortunately there is no way of
3990 knowing whether an object file has been compiled PIC or not.
3991 Looking through the relocs is not particularly time consuming.
3992 The problem is that we must either (1) keep the relocs in memory,
3993 which causes the linker to require additional runtime memory or
3994 (2) read the relocs twice from the input file, which wastes time.
3995 This would be a good case for using mmap.
3996
3997 I have no idea how to handle linking PIC code into a file of a
3998 different format. It probably can't be done. */
3999 if ((abfd->flags & DYNAMIC) == 0
2cc15b10 4000 && is_elf_hash_table (&htab->root)
d9689752
L
4001 && bed->check_relocs != NULL
4002 && elf_object_id (abfd) == elf_hash_table_id (htab)
4003 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4004 {
4005 asection *o;
4006
4007 for (o = abfd->sections; o != NULL; o = o->next)
4008 {
4009 Elf_Internal_Rela *internal_relocs;
0a1b45a2 4010 bool ok;
d9689752 4011
c4b126b8
L
4012 /* Don't check relocations in excluded sections. Don't do
4013 anything special with non-loaded, non-alloced sections.
4014 In particular, any relocs in such sections should not
4015 affect GOT and PLT reference counting (ie. we don't
4016 allow them to create GOT or PLT entries), there's no
4017 possibility or desire to optimize TLS relocs, and
4018 there's not much point in propagating relocs to shared
4019 libs that the dynamic linker won't relocate. */
4020 if ((o->flags & SEC_ALLOC) == 0
4021 || (o->flags & SEC_RELOC) == 0
5ce03cea 4022 || (o->flags & SEC_EXCLUDE) != 0
d9689752
L
4023 || o->reloc_count == 0
4024 || ((info->strip == strip_all || info->strip == strip_debugger)
4025 && (o->flags & SEC_DEBUGGING) != 0)
4026 || bfd_is_abs_section (o->output_section))
4027 continue;
4028
4029 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4030 info->keep_memory);
4031 if (internal_relocs == NULL)
0a1b45a2 4032 return false;
d9689752
L
4033
4034 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4035
4036 if (elf_section_data (o)->relocs != internal_relocs)
4037 free (internal_relocs);
4038
4039 if (! ok)
0a1b45a2 4040 return false;
d9689752
L
4041 }
4042 }
4043
0a1b45a2 4044 return true;
d9689752
L
4045}
4046
4ad4eba5
AM
4047/* Add symbols from an ELF object file to the linker hash table. */
4048
0a1b45a2 4049static bool
4ad4eba5
AM
4050elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4051{
a0c402a5 4052 Elf_Internal_Ehdr *ehdr;
4ad4eba5 4053 Elf_Internal_Shdr *hdr;
ef53be89
AM
4054 size_t symcount;
4055 size_t extsymcount;
4056 size_t extsymoff;
4ad4eba5 4057 struct elf_link_hash_entry **sym_hash;
0a1b45a2 4058 bool dynamic;
4ad4eba5 4059 Elf_External_Versym *extversym = NULL;
be22c732 4060 Elf_External_Versym *extversym_end = NULL;
4ad4eba5
AM
4061 Elf_External_Versym *ever;
4062 struct elf_link_hash_entry *weaks;
4063 struct elf_link_hash_entry **nondeflt_vers = NULL;
ef53be89 4064 size_t nondeflt_vers_cnt = 0;
4ad4eba5
AM
4065 Elf_Internal_Sym *isymbuf = NULL;
4066 Elf_Internal_Sym *isym;
4067 Elf_Internal_Sym *isymend;
4068 const struct elf_backend_data *bed;
0a1b45a2 4069 bool add_needed;
66eb6687 4070 struct elf_link_hash_table *htab;
66eb6687 4071 void *alloc_mark = NULL;
4f87808c
AM
4072 struct bfd_hash_entry **old_table = NULL;
4073 unsigned int old_size = 0;
4074 unsigned int old_count = 0;
66eb6687 4075 void *old_tab = NULL;
66eb6687
AM
4076 void *old_ent;
4077 struct bfd_link_hash_entry *old_undefs = NULL;
4078 struct bfd_link_hash_entry *old_undefs_tail = NULL;
5b677558 4079 void *old_strtab = NULL;
66eb6687 4080 size_t tabsize = 0;
db6a5d5f 4081 asection *s;
0a1b45a2 4082 bool just_syms;
4ad4eba5 4083
66eb6687 4084 htab = elf_hash_table (info);
4ad4eba5 4085 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
4086
4087 if ((abfd->flags & DYNAMIC) == 0)
0a1b45a2 4088 dynamic = false;
4ad4eba5
AM
4089 else
4090 {
0a1b45a2 4091 dynamic = true;
4ad4eba5
AM
4092
4093 /* You can't use -r against a dynamic object. Also, there's no
4094 hope of using a dynamic object which does not exactly match
4095 the format of the output file. */
0e1862bb 4096 if (bfd_link_relocatable (info)
2cc15b10 4097 || !is_elf_hash_table (&htab->root)
f13a99db 4098 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 4099 {
0e1862bb 4100 if (bfd_link_relocatable (info))
9a0789ec
NC
4101 bfd_set_error (bfd_error_invalid_operation);
4102 else
4103 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
4104 goto error_return;
4105 }
4106 }
4107
a0c402a5
L
4108 ehdr = elf_elfheader (abfd);
4109 if (info->warn_alternate_em
4110 && bed->elf_machine_code != ehdr->e_machine
4111 && ((bed->elf_machine_alt1 != 0
4112 && ehdr->e_machine == bed->elf_machine_alt1)
4113 || (bed->elf_machine_alt2 != 0
4114 && ehdr->e_machine == bed->elf_machine_alt2)))
9793eb77 4115 _bfd_error_handler
695344c0 4116 /* xgettext:c-format */
9793eb77 4117 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
a0c402a5
L
4118 ehdr->e_machine, abfd, bed->elf_machine_code);
4119
4ad4eba5
AM
4120 /* As a GNU extension, any input sections which are named
4121 .gnu.warning.SYMBOL are treated as warning symbols for the given
4122 symbol. This differs from .gnu.warning sections, which generate
4123 warnings when they are included in an output file. */
dd98f8d2 4124 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 4125 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 4126 {
db6a5d5f 4127 const char *name;
4ad4eba5 4128
fd361982 4129 name = bfd_section_name (s);
08dedd66 4130 if (startswith (name, ".gnu.warning."))
4ad4eba5 4131 {
db6a5d5f
AM
4132 char *msg;
4133 bfd_size_type sz;
4134
4135 name += sizeof ".gnu.warning." - 1;
4136
4137 /* If this is a shared object, then look up the symbol
4138 in the hash table. If it is there, and it is already
4139 been defined, then we will not be using the entry
4140 from this shared object, so we don't need to warn.
4141 FIXME: If we see the definition in a regular object
4142 later on, we will warn, but we shouldn't. The only
4143 fix is to keep track of what warnings we are supposed
4144 to emit, and then handle them all at the end of the
4145 link. */
4146 if (dynamic)
4ad4eba5 4147 {
db6a5d5f
AM
4148 struct elf_link_hash_entry *h;
4149
0a1b45a2 4150 h = elf_link_hash_lookup (htab, name, false, false, true);
db6a5d5f
AM
4151
4152 /* FIXME: What about bfd_link_hash_common? */
4153 if (h != NULL
4154 && (h->root.type == bfd_link_hash_defined
4155 || h->root.type == bfd_link_hash_defweak))
4156 continue;
4157 }
4ad4eba5 4158
db6a5d5f
AM
4159 sz = s->size;
4160 msg = (char *) bfd_alloc (abfd, sz + 1);
4161 if (msg == NULL)
4162 goto error_return;
4ad4eba5 4163
db6a5d5f
AM
4164 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4165 goto error_return;
4ad4eba5 4166
db6a5d5f 4167 msg[sz] = '\0';
4ad4eba5 4168
db6a5d5f
AM
4169 if (! (_bfd_generic_link_add_one_symbol
4170 (info, abfd, name, BSF_WARNING, s, 0, msg,
0a1b45a2 4171 false, bed->collect, NULL)))
db6a5d5f 4172 goto error_return;
4ad4eba5 4173
0e1862bb 4174 if (bfd_link_executable (info))
db6a5d5f
AM
4175 {
4176 /* Clobber the section size so that the warning does
4177 not get copied into the output file. */
4178 s->size = 0;
11d2f718 4179
db6a5d5f
AM
4180 /* Also set SEC_EXCLUDE, so that symbols defined in
4181 the warning section don't get copied to the output. */
4182 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
4183 }
4184 }
4185 }
4186
29a9f53e
L
4187 just_syms = ((s = abfd->sections) != NULL
4188 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4189
0a1b45a2 4190 add_needed = true;
4ad4eba5
AM
4191 if (! dynamic)
4192 {
4193 /* If we are creating a shared library, create all the dynamic
4194 sections immediately. We need to attach them to something,
4195 so we attach them to this BFD, provided it is the right
bf89386a
L
4196 format and is not from ld --just-symbols. Always create the
4197 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
4198 are no input BFD's of the same format as the output, we can't
4199 make a shared library. */
4200 if (!just_syms
bf89386a 4201 && (bfd_link_pic (info)
9c1d7a08 4202 || (!bfd_link_relocatable (info)
3c5fce9b 4203 && info->nointerp
9c1d7a08 4204 && (info->export_dynamic || info->dynamic)))
2cc15b10 4205 && is_elf_hash_table (&htab->root)
f13a99db 4206 && info->output_bfd->xvec == abfd->xvec
66eb6687 4207 && !htab->dynamic_sections_created)
4ad4eba5
AM
4208 {
4209 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4210 goto error_return;
4211 }
4212 }
2cc15b10 4213 else if (!is_elf_hash_table (&htab->root))
4ad4eba5
AM
4214 goto error_return;
4215 else
4216 {
4ad4eba5 4217 const char *soname = NULL;
7ee314fa 4218 char *audit = NULL;
4ad4eba5 4219 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
9acc85a6 4220 const Elf_Internal_Phdr *phdr;
e310298c 4221 struct elf_link_loaded_list *loaded_lib;
4ad4eba5
AM
4222
4223 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 4224 ld shouldn't allow it. */
29a9f53e 4225 if (just_syms)
92fd189d 4226 abort ();
4ad4eba5
AM
4227
4228 /* If this dynamic lib was specified on the command line with
4229 --as-needed in effect, then we don't want to add a DT_NEEDED
4230 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
4231 in by another lib's DT_NEEDED. When --no-add-needed is used
4232 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4233 any dynamic library in DT_NEEDED tags in the dynamic lib at
4234 all. */
4235 add_needed = (elf_dyn_lib_class (abfd)
4236 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4237 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
4238
4239 s = bfd_get_section_by_name (abfd, ".dynamic");
4240 if (s != NULL)
4241 {
4242 bfd_byte *dynbuf;
4243 bfd_byte *extdyn;
cb33740c 4244 unsigned int elfsec;
4ad4eba5
AM
4245 unsigned long shlink;
4246
eea6121a 4247 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194 4248 {
dc1e8a47 4249 error_free_dyn:
f8703194
L
4250 free (dynbuf);
4251 goto error_return;
4252 }
4ad4eba5
AM
4253
4254 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 4255 if (elfsec == SHN_BAD)
4ad4eba5
AM
4256 goto error_free_dyn;
4257 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4258
4259 for (extdyn = dynbuf;
9bff840e 4260 extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
4ad4eba5
AM
4261 extdyn += bed->s->sizeof_dyn)
4262 {
4263 Elf_Internal_Dyn dyn;
4264
4265 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4266 if (dyn.d_tag == DT_SONAME)
4267 {
4268 unsigned int tagv = dyn.d_un.d_val;
4269 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4270 if (soname == NULL)
4271 goto error_free_dyn;
4272 }
4273 if (dyn.d_tag == DT_NEEDED)
4274 {
4275 struct bfd_link_needed_list *n, **pn;
4276 char *fnm, *anm;
4277 unsigned int tagv = dyn.d_un.d_val;
986f0783 4278 size_t amt = sizeof (struct bfd_link_needed_list);
4ad4eba5 4279
a50b1753 4280 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4281 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4282 if (n == NULL || fnm == NULL)
4283 goto error_free_dyn;
4284 amt = strlen (fnm) + 1;
a50b1753 4285 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4286 if (anm == NULL)
4287 goto error_free_dyn;
4288 memcpy (anm, fnm, amt);
4289 n->name = anm;
4290 n->by = abfd;
4291 n->next = NULL;
66eb6687 4292 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4293 ;
4294 *pn = n;
4295 }
4296 if (dyn.d_tag == DT_RUNPATH)
4297 {
4298 struct bfd_link_needed_list *n, **pn;
4299 char *fnm, *anm;
4300 unsigned int tagv = dyn.d_un.d_val;
986f0783 4301 size_t amt = sizeof (struct bfd_link_needed_list);
4ad4eba5 4302
a50b1753 4303 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4304 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4305 if (n == NULL || fnm == NULL)
4306 goto error_free_dyn;
4307 amt = strlen (fnm) + 1;
a50b1753 4308 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4309 if (anm == NULL)
4310 goto error_free_dyn;
4311 memcpy (anm, fnm, amt);
4312 n->name = anm;
4313 n->by = abfd;
4314 n->next = NULL;
4315 for (pn = & runpath;
4316 *pn != NULL;
4317 pn = &(*pn)->next)
4318 ;
4319 *pn = n;
4320 }
4321 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4322 if (!runpath && dyn.d_tag == DT_RPATH)
4323 {
4324 struct bfd_link_needed_list *n, **pn;
4325 char *fnm, *anm;
4326 unsigned int tagv = dyn.d_un.d_val;
986f0783 4327 size_t amt = sizeof (struct bfd_link_needed_list);
4ad4eba5 4328
a50b1753 4329 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4330 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4331 if (n == NULL || fnm == NULL)
4332 goto error_free_dyn;
4333 amt = strlen (fnm) + 1;
a50b1753 4334 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 4335 if (anm == NULL)
f8703194 4336 goto error_free_dyn;
4ad4eba5
AM
4337 memcpy (anm, fnm, amt);
4338 n->name = anm;
4339 n->by = abfd;
4340 n->next = NULL;
4341 for (pn = & rpath;
4342 *pn != NULL;
4343 pn = &(*pn)->next)
4344 ;
4345 *pn = n;
4346 }
7ee314fa
AM
4347 if (dyn.d_tag == DT_AUDIT)
4348 {
4349 unsigned int tagv = dyn.d_un.d_val;
4350 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4351 }
4ad4eba5
AM
4352 }
4353
4354 free (dynbuf);
4355 }
4356
4357 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4358 frees all more recently bfd_alloc'd blocks as well. */
4359 if (runpath)
4360 rpath = runpath;
4361
4362 if (rpath)
4363 {
4364 struct bfd_link_needed_list **pn;
66eb6687 4365 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4366 ;
4367 *pn = rpath;
4368 }
4369
9acc85a6
AM
4370 /* If we have a PT_GNU_RELRO program header, mark as read-only
4371 all sections contained fully therein. This makes relro
4372 shared library sections appear as they will at run-time. */
4373 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
54025d58 4374 while (phdr-- > elf_tdata (abfd)->phdr)
9acc85a6
AM
4375 if (phdr->p_type == PT_GNU_RELRO)
4376 {
4377 for (s = abfd->sections; s != NULL; s = s->next)
502794d4
CE
4378 {
4379 unsigned int opb = bfd_octets_per_byte (abfd, s);
4380
4381 if ((s->flags & SEC_ALLOC) != 0
4382 && s->vma * opb >= phdr->p_vaddr
4383 && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4384 s->flags |= SEC_READONLY;
4385 }
9acc85a6
AM
4386 break;
4387 }
4388
4ad4eba5
AM
4389 /* We do not want to include any of the sections in a dynamic
4390 object in the output file. We hack by simply clobbering the
4391 list of sections in the BFD. This could be handled more
4392 cleanly by, say, a new section flag; the existing
4393 SEC_NEVER_LOAD flag is not the one we want, because that one
4394 still implies that the section takes up space in the output
4395 file. */
4396 bfd_section_list_clear (abfd);
4397
4ad4eba5
AM
4398 /* Find the name to use in a DT_NEEDED entry that refers to this
4399 object. If the object has a DT_SONAME entry, we use it.
4400 Otherwise, if the generic linker stuck something in
4401 elf_dt_name, we use that. Otherwise, we just use the file
4402 name. */
4403 if (soname == NULL || *soname == '\0')
4404 {
4405 soname = elf_dt_name (abfd);
4406 if (soname == NULL || *soname == '\0')
4407 soname = bfd_get_filename (abfd);
4408 }
4409
4410 /* Save the SONAME because sometimes the linker emulation code
4411 will need to know it. */
4412 elf_dt_name (abfd) = soname;
4413
4ad4eba5
AM
4414 /* If we have already included this dynamic object in the
4415 link, just ignore it. There is no reason to include a
4416 particular dynamic object more than once. */
e310298c
AM
4417 for (loaded_lib = htab->dyn_loaded;
4418 loaded_lib != NULL;
4419 loaded_lib = loaded_lib->next)
4420 {
4421 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
0a1b45a2 4422 return true;
e310298c
AM
4423 }
4424
4425 /* Create dynamic sections for backends that require that be done
4426 before setup_gnu_properties. */
4427 if (add_needed
4428 && !_bfd_elf_link_create_dynamic_sections (abfd, info))
0a1b45a2 4429 return false;
7ee314fa
AM
4430
4431 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 4432 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
4433 }
4434
4435 /* If this is a dynamic object, we always link against the .dynsym
4436 symbol table, not the .symtab symbol table. The dynamic linker
4437 will only see the .dynsym symbol table, so there is no reason to
4438 look at .symtab for a dynamic object. */
4439
4440 if (! dynamic || elf_dynsymtab (abfd) == 0)
4441 hdr = &elf_tdata (abfd)->symtab_hdr;
4442 else
4443 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4444
4445 symcount = hdr->sh_size / bed->s->sizeof_sym;
4446
4447 /* The sh_info field of the symtab header tells us where the
4448 external symbols start. We don't care about the local symbols at
4449 this point. */
4450 if (elf_bad_symtab (abfd))
4451 {
4452 extsymcount = symcount;
4453 extsymoff = 0;
4454 }
4455 else
4456 {
4457 extsymcount = symcount - hdr->sh_info;
4458 extsymoff = hdr->sh_info;
4459 }
4460
f45794cb 4461 sym_hash = elf_sym_hashes (abfd);
012b2306 4462 if (extsymcount != 0)
4ad4eba5
AM
4463 {
4464 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4465 NULL, NULL, NULL);
4466 if (isymbuf == NULL)
4467 goto error_return;
4468
4ad4eba5 4469 if (sym_hash == NULL)
012b2306
AM
4470 {
4471 /* We store a pointer to the hash table entry for each
4472 external symbol. */
986f0783 4473 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
012b2306
AM
4474 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4475 if (sym_hash == NULL)
4476 goto error_free_sym;
4477 elf_sym_hashes (abfd) = sym_hash;
4478 }
4ad4eba5
AM
4479 }
4480
4481 if (dynamic)
4482 {
4483 /* Read in any version definitions. */
fc0e6df6
PB
4484 if (!_bfd_elf_slurp_version_tables (abfd,
4485 info->default_imported_symver))
4ad4eba5
AM
4486 goto error_free_sym;
4487
4488 /* Read in the symbol versions, but don't bother to convert them
4489 to internal format. */
4490 if (elf_dynversym (abfd) != 0)
4491 {
986f0783
AM
4492 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4493 bfd_size_type amt = versymhdr->sh_size;
4ad4eba5 4494
2bb3687b
AM
4495 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4496 goto error_free_sym;
4497 extversym = (Elf_External_Versym *)
4498 _bfd_malloc_and_read (abfd, amt, amt);
4ad4eba5
AM
4499 if (extversym == NULL)
4500 goto error_free_sym;
986f0783 4501 extversym_end = extversym + amt / sizeof (*extversym);
4ad4eba5
AM
4502 }
4503 }
4504
66eb6687
AM
4505 /* If we are loading an as-needed shared lib, save the symbol table
4506 state before we start adding symbols. If the lib turns out
4507 to be unneeded, restore the state. */
4508 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4509 {
4510 unsigned int i;
4511 size_t entsize;
4512
4513 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4514 {
4515 struct bfd_hash_entry *p;
2de92251 4516 struct elf_link_hash_entry *h;
66eb6687
AM
4517
4518 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
4519 {
4520 h = (struct elf_link_hash_entry *) p;
4521 entsize += htab->root.table.entsize;
4522 if (h->root.type == bfd_link_hash_warning)
7ba11550
AM
4523 {
4524 entsize += htab->root.table.entsize;
4525 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4526 }
4527 if (h->root.type == bfd_link_hash_common)
4528 entsize += sizeof (*h->root.u.c.p);
2de92251 4529 }
66eb6687
AM
4530 }
4531
4532 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 4533 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
4534 if (old_tab == NULL)
4535 goto error_free_vers;
4536
4537 /* Remember the current objalloc pointer, so that all mem for
4538 symbols added can later be reclaimed. */
4539 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4540 if (alloc_mark == NULL)
4541 goto error_free_vers;
4542
5061a885
AM
4543 /* Make a special call to the linker "notice" function to
4544 tell it that we are about to handle an as-needed lib. */
e5034e59 4545 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 4546 goto error_free_vers;
5061a885 4547
f45794cb
AM
4548 /* Clone the symbol table. Remember some pointers into the
4549 symbol table, and dynamic symbol count. */
4550 old_ent = (char *) old_tab + tabsize;
66eb6687 4551 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
4552 old_undefs = htab->root.undefs;
4553 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
4554 old_table = htab->root.table.table;
4555 old_size = htab->root.table.size;
4556 old_count = htab->root.table.count;
e310298c
AM
4557 old_strtab = NULL;
4558 if (htab->dynstr != NULL)
4559 {
4560 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4561 if (old_strtab == NULL)
4562 goto error_free_vers;
4563 }
66eb6687
AM
4564
4565 for (i = 0; i < htab->root.table.size; i++)
4566 {
4567 struct bfd_hash_entry *p;
2de92251 4568 struct elf_link_hash_entry *h;
66eb6687
AM
4569
4570 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4571 {
2de92251 4572 h = (struct elf_link_hash_entry *) p;
7ba11550
AM
4573 memcpy (old_ent, h, htab->root.table.entsize);
4574 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4575 if (h->root.type == bfd_link_hash_warning)
4576 {
7ba11550
AM
4577 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4578 memcpy (old_ent, h, htab->root.table.entsize);
2de92251
AM
4579 old_ent = (char *) old_ent + htab->root.table.entsize;
4580 }
7ba11550
AM
4581 if (h->root.type == bfd_link_hash_common)
4582 {
4583 memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
4584 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
4585 }
66eb6687
AM
4586 }
4587 }
4588 }
4ad4eba5 4589
66eb6687 4590 weaks = NULL;
be22c732
NC
4591 if (extversym == NULL)
4592 ever = NULL;
4593 else if (extversym + extsymoff < extversym_end)
4594 ever = extversym + extsymoff;
4595 else
4596 {
4597 /* xgettext:c-format */
4598 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4599 abfd, (long) extsymoff,
4600 (long) (extversym_end - extversym) / sizeof (* extversym));
4601 bfd_set_error (bfd_error_bad_value);
4602 goto error_free_vers;
4603 }
4604
b4c555cf
ML
4605 if (!bfd_link_relocatable (info)
4606 && abfd->lto_slim_object)
cc5277b1
ML
4607 {
4608 _bfd_error_handler
4609 (_("%pB: plugin needed to handle lto object"), abfd);
4610 }
4611
4ad4eba5
AM
4612 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4613 isym < isymend;
4614 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4615 {
4616 int bind;
4617 bfd_vma value;
af44c138 4618 asection *sec, *new_sec;
4ad4eba5
AM
4619 flagword flags;
4620 const char *name;
4621 struct elf_link_hash_entry *h;
90c984fc 4622 struct elf_link_hash_entry *hi;
0a1b45a2
AM
4623 bool definition;
4624 bool size_change_ok;
4625 bool type_change_ok;
4626 bool new_weak;
4627 bool old_weak;
7ba11550 4628 bfd *override;
0a1b45a2
AM
4629 bool common;
4630 bool discarded;
4ad4eba5 4631 unsigned int old_alignment;
4538d1c7 4632 unsigned int shindex;
4ad4eba5 4633 bfd *old_bfd;
0a1b45a2 4634 bool matched;
4ad4eba5 4635
7ba11550 4636 override = NULL;
4ad4eba5
AM
4637
4638 flags = BSF_NO_FLAGS;
4639 sec = NULL;
4640 value = isym->st_value;
a4d8e49b 4641 common = bed->common_definition (isym);
2980ccad
L
4642 if (common && info->inhibit_common_definition)
4643 {
4644 /* Treat common symbol as undefined for --no-define-common. */
4645 isym->st_shndx = SHN_UNDEF;
0a1b45a2 4646 common = false;
2980ccad 4647 }
0a1b45a2 4648 discarded = false;
4ad4eba5
AM
4649
4650 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 4651 switch (bind)
4ad4eba5 4652 {
3e7a7d11 4653 case STB_LOCAL:
4ad4eba5
AM
4654 /* This should be impossible, since ELF requires that all
4655 global symbols follow all local symbols, and that sh_info
4656 point to the first global symbol. Unfortunately, Irix 5
4657 screws this up. */
fe3fef62
AM
4658 if (elf_bad_symtab (abfd))
4659 continue;
4660
4661 /* If we aren't prepared to handle locals within the globals
4538d1c7
AM
4662 then we'll likely segfault on a NULL symbol hash if the
4663 symbol is ever referenced in relocations. */
4664 shindex = elf_elfheader (abfd)->e_shstrndx;
4665 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4666 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4667 " (>= sh_info of %lu)"),
4668 abfd, name, (long) (isym - isymbuf + extsymoff),
4669 (long) extsymoff);
4670
4671 /* Dynamic object relocations are not processed by ld, so
4672 ld won't run into the problem mentioned above. */
4673 if (dynamic)
4674 continue;
fe3fef62
AM
4675 bfd_set_error (bfd_error_bad_value);
4676 goto error_free_vers;
3e7a7d11
NC
4677
4678 case STB_GLOBAL:
a4d8e49b 4679 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4680 flags = BSF_GLOBAL;
3e7a7d11
NC
4681 break;
4682
4683 case STB_WEAK:
4684 flags = BSF_WEAK;
4685 break;
4686
4687 case STB_GNU_UNIQUE:
4688 flags = BSF_GNU_UNIQUE;
4689 break;
4690
4691 default:
4ad4eba5 4692 /* Leave it up to the processor backend. */
3e7a7d11 4693 break;
4ad4eba5
AM
4694 }
4695
4696 if (isym->st_shndx == SHN_UNDEF)
4697 sec = bfd_und_section_ptr;
cb33740c
AM
4698 else if (isym->st_shndx == SHN_ABS)
4699 sec = bfd_abs_section_ptr;
4700 else if (isym->st_shndx == SHN_COMMON)
4701 {
4702 sec = bfd_com_section_ptr;
4703 /* What ELF calls the size we call the value. What ELF
4704 calls the value we call the alignment. */
4705 value = isym->st_size;
4706 }
4707 else
4ad4eba5
AM
4708 {
4709 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4710 if (sec == NULL)
4711 sec = bfd_abs_section_ptr;
dbaa2011 4712 else if (discarded_section (sec))
529fcb95 4713 {
e5d08002
L
4714 /* Symbols from discarded section are undefined. We keep
4715 its visibility. */
529fcb95 4716 sec = bfd_und_section_ptr;
0a1b45a2 4717 discarded = true;
529fcb95
PB
4718 isym->st_shndx = SHN_UNDEF;
4719 }
4ad4eba5
AM
4720 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4721 value -= sec->vma;
4722 }
4ad4eba5
AM
4723
4724 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4725 isym->st_name);
4726 if (name == NULL)
4727 goto error_free_vers;
4728
4729 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4730 && (abfd->flags & BFD_PLUGIN) != 0)
4731 {
4732 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4733
4734 if (xc == NULL)
4735 {
4736 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4737 | SEC_EXCLUDE);
4738 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4739 if (xc == NULL)
4740 goto error_free_vers;
4741 }
4742 sec = xc;
4743 }
4744 else if (isym->st_shndx == SHN_COMMON
4745 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4746 && !bfd_link_relocatable (info))
4ad4eba5
AM
4747 {
4748 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4749
4750 if (tcomm == NULL)
4751 {
02d00247
AM
4752 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4753 | SEC_LINKER_CREATED);
4754 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4755 if (tcomm == NULL)
4ad4eba5
AM
4756 goto error_free_vers;
4757 }
4758 sec = tcomm;
4759 }
66eb6687 4760 else if (bed->elf_add_symbol_hook)
4ad4eba5 4761 {
66eb6687
AM
4762 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4763 &sec, &value))
4ad4eba5
AM
4764 goto error_free_vers;
4765
4766 /* The hook function sets the name to NULL if this symbol
4767 should be skipped for some reason. */
4768 if (name == NULL)
4769 continue;
4770 }
4771
4772 /* Sanity check that all possibilities were handled. */
4773 if (sec == NULL)
4538d1c7 4774 abort ();
4ad4eba5 4775
191c0c42
AM
4776 /* Silently discard TLS symbols from --just-syms. There's
4777 no way to combine a static TLS block with a new TLS block
4778 for this executable. */
4779 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4780 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4781 continue;
4782
4ad4eba5
AM
4783 if (bfd_is_und_section (sec)
4784 || bfd_is_com_section (sec))
0a1b45a2 4785 definition = false;
4ad4eba5 4786 else
0a1b45a2 4787 definition = true;
4ad4eba5 4788
0a1b45a2 4789 size_change_ok = false;
66eb6687 4790 type_change_ok = bed->type_change_ok;
0a1b45a2
AM
4791 old_weak = false;
4792 matched = false;
4ad4eba5
AM
4793 old_alignment = 0;
4794 old_bfd = NULL;
af44c138 4795 new_sec = sec;
4ad4eba5 4796
2cc15b10 4797 if (is_elf_hash_table (&htab->root))
4ad4eba5
AM
4798 {
4799 Elf_Internal_Versym iver;
4800 unsigned int vernum = 0;
0a1b45a2 4801 bool skip;
4ad4eba5 4802
fc0e6df6 4803 if (ever == NULL)
4ad4eba5 4804 {
fc0e6df6
PB
4805 if (info->default_imported_symver)
4806 /* Use the default symbol version created earlier. */
4807 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4808 else
4809 iver.vs_vers = 0;
4810 }
be22c732
NC
4811 else if (ever >= extversym_end)
4812 {
4813 /* xgettext:c-format */
4814 _bfd_error_handler (_("%pB: not enough version information"),
4815 abfd);
4816 bfd_set_error (bfd_error_bad_value);
4817 goto error_free_vers;
4818 }
fc0e6df6
PB
4819 else
4820 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4821
4822 vernum = iver.vs_vers & VERSYM_VERSION;
4823
4824 /* If this is a hidden symbol, or if it is not version
4825 1, we append the version name to the symbol name.
cc86ff91
EB
4826 However, we do not modify a non-hidden absolute symbol
4827 if it is not a function, because it might be the version
4828 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4829 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4830 || (vernum > 1
4831 && (!bfd_is_abs_section (sec)
4832 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4833 {
4834 const char *verstr;
4835 size_t namelen, verlen, newlen;
4836 char *newname, *p;
4837
4838 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4839 {
fc0e6df6
PB
4840 if (vernum > elf_tdata (abfd)->cverdefs)
4841 verstr = NULL;
4842 else if (vernum > 1)
4843 verstr =
4844 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4845 else
4846 verstr = "";
4ad4eba5 4847
fc0e6df6 4848 if (verstr == NULL)
4ad4eba5 4849 {
4eca0228 4850 _bfd_error_handler
695344c0 4851 /* xgettext:c-format */
871b3ab2 4852 (_("%pB: %s: invalid version %u (max %d)"),
fc0e6df6
PB
4853 abfd, name, vernum,
4854 elf_tdata (abfd)->cverdefs);
4855 bfd_set_error (bfd_error_bad_value);
4856 goto error_free_vers;
4ad4eba5 4857 }
fc0e6df6
PB
4858 }
4859 else
4860 {
4861 /* We cannot simply test for the number of
4862 entries in the VERNEED section since the
4863 numbers for the needed versions do not start
4864 at 0. */
4865 Elf_Internal_Verneed *t;
4866
4867 verstr = NULL;
4868 for (t = elf_tdata (abfd)->verref;
4869 t != NULL;
4870 t = t->vn_nextref)
4ad4eba5 4871 {
fc0e6df6 4872 Elf_Internal_Vernaux *a;
4ad4eba5 4873
fc0e6df6
PB
4874 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4875 {
4876 if (a->vna_other == vernum)
4ad4eba5 4877 {
fc0e6df6
PB
4878 verstr = a->vna_nodename;
4879 break;
4ad4eba5 4880 }
4ad4eba5 4881 }
fc0e6df6
PB
4882 if (a != NULL)
4883 break;
4884 }
4885 if (verstr == NULL)
4886 {
4eca0228 4887 _bfd_error_handler
695344c0 4888 /* xgettext:c-format */
871b3ab2 4889 (_("%pB: %s: invalid needed version %d"),
fc0e6df6
PB
4890 abfd, name, vernum);
4891 bfd_set_error (bfd_error_bad_value);
4892 goto error_free_vers;
4ad4eba5 4893 }
4ad4eba5 4894 }
fc0e6df6
PB
4895
4896 namelen = strlen (name);
4897 verlen = strlen (verstr);
4898 newlen = namelen + verlen + 2;
4899 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4900 && isym->st_shndx != SHN_UNDEF)
4901 ++newlen;
4902
a50b1753 4903 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4904 if (newname == NULL)
4905 goto error_free_vers;
4906 memcpy (newname, name, namelen);
4907 p = newname + namelen;
4908 *p++ = ELF_VER_CHR;
4909 /* If this is a defined non-hidden version symbol,
4910 we add another @ to the name. This indicates the
4911 default version of the symbol. */
4912 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4913 && isym->st_shndx != SHN_UNDEF)
4914 *p++ = ELF_VER_CHR;
4915 memcpy (p, verstr, verlen + 1);
4916
4917 name = newname;
4ad4eba5
AM
4918 }
4919
cd3416da
AM
4920 /* If this symbol has default visibility and the user has
4921 requested we not re-export it, then mark it as hidden. */
a0d49154 4922 if (!bfd_is_und_section (sec)
cd3416da 4923 && !dynamic
ce875075 4924 && abfd->no_export
cd3416da
AM
4925 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4926 isym->st_other = (STV_HIDDEN
4927 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4928
4f3fedcf
AM
4929 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4930 sym_hash, &old_bfd, &old_weak,
4931 &old_alignment, &skip, &override,
6e33951e
L
4932 &type_change_ok, &size_change_ok,
4933 &matched))
4ad4eba5
AM
4934 goto error_free_vers;
4935
4936 if (skip)
4937 continue;
4938
6e33951e
L
4939 /* Override a definition only if the new symbol matches the
4940 existing one. */
4941 if (override && matched)
0a1b45a2 4942 definition = false;
4ad4eba5
AM
4943
4944 h = *sym_hash;
4945 while (h->root.type == bfd_link_hash_indirect
4946 || h->root.type == bfd_link_hash_warning)
4947 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4948
4ad4eba5 4949 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4950 && vernum > 1
4951 && definition)
4952 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4953 }
4954
4955 if (! (_bfd_generic_link_add_one_symbol
7ba11550 4956 (info, override ? override : abfd, name, flags, sec, value,
0a1b45a2 4957 NULL, false, bed->collect,
4ad4eba5
AM
4958 (struct bfd_link_hash_entry **) sym_hash)))
4959 goto error_free_vers;
4960
4961 h = *sym_hash;
90c984fc
L
4962 /* We need to make sure that indirect symbol dynamic flags are
4963 updated. */
4964 hi = h;
4ad4eba5
AM
4965 while (h->root.type == bfd_link_hash_indirect
4966 || h->root.type == bfd_link_hash_warning)
4967 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4968
d1cbe007
AM
4969 *sym_hash = h;
4970
97196564
L
4971 /* Setting the index to -3 tells elf_link_output_extsym that
4972 this symbol is defined in a discarded section. */
2cc15b10 4973 if (discarded && is_elf_hash_table (&htab->root))
97196564
L
4974 h->indx = -3;
4975
37a9e49a 4976 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4977 if (dynamic
4978 && definition
37a9e49a 4979 && new_weak
fcb93ecf 4980 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
2cc15b10 4981 && is_elf_hash_table (&htab->root)
60d67dc8 4982 && h->u.alias == NULL)
4ad4eba5
AM
4983 {
4984 /* Keep a list of all weak defined non function symbols from
60d67dc8
AM
4985 a dynamic object, using the alias field. Later in this
4986 function we will set the alias field to the correct
4ad4eba5
AM
4987 value. We only put non-function symbols from dynamic
4988 objects on this list, because that happens to be the only
4989 time we need to know the normal symbol corresponding to a
4990 weak symbol, and the information is time consuming to
60d67dc8 4991 figure out. If the alias field is not already NULL,
4ad4eba5
AM
4992 then this symbol was already defined by some previous
4993 dynamic object, and we will be using that previous
4994 definition anyhow. */
4995
60d67dc8 4996 h->u.alias = weaks;
4ad4eba5 4997 weaks = h;
4ad4eba5
AM
4998 }
4999
5000 /* Set the alignment of a common symbol. */
a4d8e49b 5001 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
5002 && h->root.type == bfd_link_hash_common)
5003 {
5004 unsigned int align;
5005
a4d8e49b 5006 if (common)
af44c138
L
5007 align = bfd_log2 (isym->st_value);
5008 else
5009 {
5010 /* The new symbol is a common symbol in a shared object.
5011 We need to get the alignment from the section. */
5012 align = new_sec->alignment_power;
5013 }
595213d4 5014 if (align > old_alignment)
4ad4eba5
AM
5015 h->root.u.c.p->alignment_power = align;
5016 else
5017 h->root.u.c.p->alignment_power = old_alignment;
5018 }
5019
2cc15b10 5020 if (is_elf_hash_table (&htab->root))
4ad4eba5 5021 {
4f3fedcf
AM
5022 /* Set a flag in the hash table entry indicating the type of
5023 reference or definition we just found. A dynamic symbol
5024 is one which is referenced or defined by both a regular
5025 object and a shared object. */
0a1b45a2 5026 bool dynsym = false;
4f3fedcf 5027
b1a92c63
AM
5028 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5029 if ((abfd->flags & BFD_PLUGIN) != 0)
bbaddd4b
AM
5030 {
5031 /* Except for this flag to track nonweak references. */
5032 if (!definition
5033 && bind != STB_WEAK)
5034 h->ref_ir_nonweak = 1;
5035 }
b1a92c63 5036 else if (!dynamic)
4f3fedcf
AM
5037 {
5038 if (! definition)
5039 {
5040 h->ref_regular = 1;
5041 if (bind != STB_WEAK)
5042 h->ref_regular_nonweak = 1;
5043 }
5044 else
5045 {
5046 h->def_regular = 1;
5047 if (h->def_dynamic)
5048 {
5049 h->def_dynamic = 0;
5050 h->ref_dynamic = 1;
5051 }
5052 }
4f3fedcf
AM
5053 }
5054 else
5055 {
5056 if (! definition)
5057 {
5058 h->ref_dynamic = 1;
5059 hi->ref_dynamic = 1;
5060 }
5061 else
5062 {
5063 h->def_dynamic = 1;
5064 hi->def_dynamic = 1;
5065 }
b1a92c63 5066 }
4f3fedcf 5067
b1a92c63
AM
5068 /* If an indirect symbol has been forced local, don't
5069 make the real symbol dynamic. */
5070 if (h != hi && hi->forced_local)
5071 ;
5072 else if (!dynamic)
5073 {
5074 if (bfd_link_dll (info)
5075 || h->def_dynamic
5076 || h->ref_dynamic)
0a1b45a2 5077 dynsym = true;
b1a92c63
AM
5078 }
5079 else
5080 {
5081 if (h->def_regular
5082 || h->ref_regular
5083 || (h->is_weakalias
5084 && weakdef (h)->dynindx != -1))
0a1b45a2 5085 dynsym = true;
4f3fedcf
AM
5086 }
5087
5088 /* Check to see if we need to add an indirect symbol for
5089 the default name. */
726d7d1e
AM
5090 if ((definition
5091 || (!override && h->root.type == bfd_link_hash_common))
5092 && !(hi != h
5093 && hi->versioned == versioned_hidden))
4f3fedcf 5094 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
f01fb44c 5095 sec, value, &old_bfd, &dynsym))
4f3fedcf 5096 goto error_free_vers;
4ad4eba5
AM
5097
5098 /* Check the alignment when a common symbol is involved. This
5099 can change when a common symbol is overridden by a normal
5100 definition or a common symbol is ignored due to the old
5101 normal definition. We need to make sure the maximum
5102 alignment is maintained. */
a4d8e49b 5103 if ((old_alignment || common)
4ad4eba5
AM
5104 && h->root.type != bfd_link_hash_common)
5105 {
5106 unsigned int common_align;
5107 unsigned int normal_align;
5108 unsigned int symbol_align;
5109 bfd *normal_bfd;
5110 bfd *common_bfd;
5111
3a81e825
AM
5112 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5113 || h->root.type == bfd_link_hash_defweak);
5114
4ad4eba5
AM
5115 symbol_align = ffs (h->root.u.def.value) - 1;
5116 if (h->root.u.def.section->owner != NULL
0616a280
AM
5117 && (h->root.u.def.section->owner->flags
5118 & (DYNAMIC | BFD_PLUGIN)) == 0)
4ad4eba5
AM
5119 {
5120 normal_align = h->root.u.def.section->alignment_power;
5121 if (normal_align > symbol_align)
5122 normal_align = symbol_align;
5123 }
5124 else
5125 normal_align = symbol_align;
5126
5127 if (old_alignment)
5128 {
5129 common_align = old_alignment;
5130 common_bfd = old_bfd;
5131 normal_bfd = abfd;
5132 }
5133 else
5134 {
5135 common_align = bfd_log2 (isym->st_value);
5136 common_bfd = abfd;
5137 normal_bfd = old_bfd;
5138 }
5139
5140 if (normal_align < common_align)
d07676f8
NC
5141 {
5142 /* PR binutils/2735 */
5143 if (normal_bfd == NULL)
4eca0228 5144 _bfd_error_handler
695344c0 5145 /* xgettext:c-format */
9793eb77 5146 (_("warning: alignment %u of common symbol `%s' in %pB is"
871b3ab2 5147 " greater than the alignment (%u) of its section %pA"),
c08bb8dd
AM
5148 1 << common_align, name, common_bfd,
5149 1 << normal_align, h->root.u.def.section);
d07676f8 5150 else
4eca0228 5151 _bfd_error_handler
695344c0 5152 /* xgettext:c-format */
9793eb77 5153 (_("warning: alignment %u of symbol `%s' in %pB"
871b3ab2 5154 " is smaller than %u in %pB"),
c08bb8dd
AM
5155 1 << normal_align, name, normal_bfd,
5156 1 << common_align, common_bfd);
d07676f8 5157 }
4ad4eba5
AM
5158 }
5159
83ad0046 5160 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
5161 if (isym->st_size != 0
5162 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
5163 && (definition || h->size == 0))
5164 {
83ad0046
L
5165 if (h->size != 0
5166 && h->size != isym->st_size
5167 && ! size_change_ok)
4eca0228 5168 _bfd_error_handler
695344c0 5169 /* xgettext:c-format */
9793eb77 5170 (_("warning: size of symbol `%s' changed"
2dcf00ce
AM
5171 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5172 name, (uint64_t) h->size, old_bfd,
5173 (uint64_t) isym->st_size, abfd);
4ad4eba5
AM
5174
5175 h->size = isym->st_size;
5176 }
5177
5178 /* If this is a common symbol, then we always want H->SIZE
5179 to be the size of the common symbol. The code just above
5180 won't fix the size if a common symbol becomes larger. We
5181 don't warn about a size change here, because that is
4f3fedcf 5182 covered by --warn-common. Allow changes between different
fcb93ecf 5183 function types. */
4ad4eba5
AM
5184 if (h->root.type == bfd_link_hash_common)
5185 h->size = h->root.u.c.size;
5186
5187 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
5188 && ((definition && !new_weak)
5189 || (old_weak && h->root.type == bfd_link_hash_common)
5190 || h->type == STT_NOTYPE))
4ad4eba5 5191 {
2955ec4c
L
5192 unsigned int type = ELF_ST_TYPE (isym->st_info);
5193
5194 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5195 symbol. */
5196 if (type == STT_GNU_IFUNC
5197 && (abfd->flags & DYNAMIC) != 0)
5198 type = STT_FUNC;
4ad4eba5 5199
2955ec4c
L
5200 if (h->type != type)
5201 {
5202 if (h->type != STT_NOTYPE && ! type_change_ok)
695344c0 5203 /* xgettext:c-format */
4eca0228 5204 _bfd_error_handler
9793eb77 5205 (_("warning: type of symbol `%s' changed"
871b3ab2 5206 " from %d to %d in %pB"),
c08bb8dd 5207 name, h->type, type, abfd);
2955ec4c
L
5208
5209 h->type = type;
5210 }
4ad4eba5
AM
5211 }
5212
54ac0771 5213 /* Merge st_other field. */
5160d0f3
AM
5214 elf_merge_st_other (abfd, h, isym->st_other, sec,
5215 definition, dynamic);
4ad4eba5 5216
c3df8c14 5217 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
5218 if (definition
5219 && (sec->flags & SEC_DEBUGGING)
5220 && !bfd_link_relocatable (info))
0a1b45a2 5221 dynsym = false;
c3df8c14 5222
b1a92c63
AM
5223 /* Nor should we make plugin symbols dynamic. */
5224 if ((abfd->flags & BFD_PLUGIN) != 0)
0a1b45a2 5225 dynsym = false;
b1a92c63 5226
35fc36a8 5227 if (definition)
35399224
L
5228 {
5229 h->target_internal = isym->st_target_internal;
5230 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5231 }
35fc36a8 5232
4ad4eba5
AM
5233 if (definition && !dynamic)
5234 {
5235 char *p = strchr (name, ELF_VER_CHR);
5236 if (p != NULL && p[1] != ELF_VER_CHR)
5237 {
5238 /* Queue non-default versions so that .symver x, x@FOO
5239 aliases can be checked. */
66eb6687 5240 if (!nondeflt_vers)
4ad4eba5 5241 {
986f0783
AM
5242 size_t amt = ((isymend - isym + 1)
5243 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
5244 nondeflt_vers
5245 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
5246 if (!nondeflt_vers)
5247 goto error_free_vers;
4ad4eba5 5248 }
66eb6687 5249 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
5250 }
5251 }
5252
b1a92c63 5253 if (dynsym && h->dynindx == -1)
4ad4eba5 5254 {
c152c796 5255 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 5256 goto error_free_vers;
60d67dc8
AM
5257 if (h->is_weakalias
5258 && weakdef (h)->dynindx == -1)
4ad4eba5 5259 {
60d67dc8 5260 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
4ad4eba5
AM
5261 goto error_free_vers;
5262 }
5263 }
1f599d0e 5264 else if (h->dynindx != -1)
4ad4eba5
AM
5265 /* If the symbol already has a dynamic index, but
5266 visibility says it should not be visible, turn it into
5267 a local symbol. */
5268 switch (ELF_ST_VISIBILITY (h->other))
5269 {
5270 case STV_INTERNAL:
5271 case STV_HIDDEN:
0a1b45a2
AM
5272 (*bed->elf_backend_hide_symbol) (info, h, true);
5273 dynsym = false;
4ad4eba5
AM
5274 break;
5275 }
5276
5277 if (!add_needed
aef28989 5278 && matched
4ad4eba5 5279 && definition
f01fb44c 5280 && h->root.type != bfd_link_hash_indirect
010e5ae2 5281 && ((dynsym
a896df97 5282 && h->ref_regular_nonweak)
b1a92c63
AM
5283 || (old_bfd != NULL
5284 && (old_bfd->flags & BFD_PLUGIN) != 0
bbaddd4b
AM
5285 && h->ref_ir_nonweak
5286 && !info->lto_all_symbols_read)
ffa9430d 5287 || (h->ref_dynamic_nonweak
010e5ae2 5288 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
7b15fa7a
AM
5289 && !on_needed_list (elf_dt_name (abfd),
5290 htab->needed, NULL))))
4ad4eba5 5291 {
4ad4eba5
AM
5292 const char *soname = elf_dt_name (abfd);
5293
16e4ecc0
AM
5294 info->callbacks->minfo ("%!", soname, old_bfd,
5295 h->root.root.string);
5296
4ad4eba5
AM
5297 /* A symbol from a library loaded via DT_NEEDED of some
5298 other library is referenced by a regular object.
e56f61be 5299 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
5300 --no-add-needed is used and the reference was not
5301 a weak one. */
4f3fedcf 5302 if (old_bfd != NULL
b918acf9 5303 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be 5304 {
4eca0228 5305 _bfd_error_handler
695344c0 5306 /* xgettext:c-format */
871b3ab2 5307 (_("%pB: undefined reference to symbol '%s'"),
4f3fedcf 5308 old_bfd, name);
ff5ac77b 5309 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
5310 goto error_free_vers;
5311 }
5312
a50b1753 5313 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 5314 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 5315
e310298c
AM
5316 /* Create dynamic sections for backends that require
5317 that be done before setup_gnu_properties. */
5318 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
0a1b45a2
AM
5319 return false;
5320 add_needed = true;
4ad4eba5
AM
5321 }
5322 }
5323 }
5324
a83ef4d1
L
5325 if (info->lto_plugin_active
5326 && !bfd_link_relocatable (info)
5327 && (abfd->flags & BFD_PLUGIN) == 0
5328 && !just_syms
5329 && extsymcount)
5330 {
5331 int r_sym_shift;
5332
5333 if (bed->s->arch_size == 32)
5334 r_sym_shift = 8;
5335 else
5336 r_sym_shift = 32;
5337
5338 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5339 referenced in regular objects so that linker plugin will get
5340 the correct symbol resolution. */
5341
5342 sym_hash = elf_sym_hashes (abfd);
5343 for (s = abfd->sections; s != NULL; s = s->next)
5344 {
5345 Elf_Internal_Rela *internal_relocs;
5346 Elf_Internal_Rela *rel, *relend;
5347
5348 /* Don't check relocations in excluded sections. */
5349 if ((s->flags & SEC_RELOC) == 0
5350 || s->reloc_count == 0
5351 || (s->flags & SEC_EXCLUDE) != 0
5352 || ((info->strip == strip_all
5353 || info->strip == strip_debugger)
5354 && (s->flags & SEC_DEBUGGING) != 0))
5355 continue;
5356
5357 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5358 NULL,
5359 info->keep_memory);
5360 if (internal_relocs == NULL)
5361 goto error_free_vers;
5362
5363 rel = internal_relocs;
5364 relend = rel + s->reloc_count;
5365 for ( ; rel < relend; rel++)
5366 {
5367 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5368 struct elf_link_hash_entry *h;
5369
5370 /* Skip local symbols. */
5371 if (r_symndx < extsymoff)
5372 continue;
5373
5374 h = sym_hash[r_symndx - extsymoff];
5375 if (h != NULL)
5376 h->root.non_ir_ref_regular = 1;
5377 }
5378
5379 if (elf_section_data (s)->relocs != internal_relocs)
5380 free (internal_relocs);
5381 }
5382 }
5383
c9594989
AM
5384 free (extversym);
5385 extversym = NULL;
5386 free (isymbuf);
5387 isymbuf = NULL;
66eb6687
AM
5388
5389 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5390 {
5391 unsigned int i;
5392
5393 /* Restore the symbol table. */
f45794cb
AM
5394 old_ent = (char *) old_tab + tabsize;
5395 memset (elf_sym_hashes (abfd), 0,
5396 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
5397 htab->root.table.table = old_table;
5398 htab->root.table.size = old_size;
5399 htab->root.table.count = old_count;
66eb6687 5400 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
5401 htab->root.undefs = old_undefs;
5402 htab->root.undefs_tail = old_undefs_tail;
e310298c
AM
5403 if (htab->dynstr != NULL)
5404 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5b677558
AM
5405 free (old_strtab);
5406 old_strtab = NULL;
66eb6687
AM
5407 for (i = 0; i < htab->root.table.size; i++)
5408 {
5409 struct bfd_hash_entry *p;
5410 struct elf_link_hash_entry *h;
4070765b 5411 unsigned int non_ir_ref_dynamic;
66eb6687
AM
5412
5413 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5414 {
4070765b 5415 /* Preserve non_ir_ref_dynamic so that this symbol
59fa66c5
L
5416 will be exported when the dynamic lib becomes needed
5417 in the second pass. */
7ba11550
AM
5418 h = (struct elf_link_hash_entry *) p;
5419 if (h->root.type == bfd_link_hash_warning)
5420 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4070765b 5421 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
7ba11550 5422
2de92251 5423 h = (struct elf_link_hash_entry *) p;
7ba11550
AM
5424 memcpy (h, old_ent, htab->root.table.entsize);
5425 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
5426 if (h->root.type == bfd_link_hash_warning)
5427 {
a4542f1b 5428 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7ba11550
AM
5429 memcpy (h, old_ent, htab->root.table.entsize);
5430 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251 5431 }
a4542f1b 5432 if (h->root.type == bfd_link_hash_common)
3e0882af 5433 {
7ba11550
AM
5434 memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5435 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
3e0882af 5436 }
4070765b 5437 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
66eb6687
AM
5438 }
5439 }
5440
5061a885
AM
5441 /* Make a special call to the linker "notice" function to
5442 tell it that symbols added for crefs may need to be removed. */
e5034e59 5443 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 5444 goto error_free_vers;
5061a885 5445
66eb6687
AM
5446 free (old_tab);
5447 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5448 alloc_mark);
c9594989 5449 free (nondeflt_vers);
0a1b45a2 5450 return true;
66eb6687 5451 }
2de92251 5452
66eb6687
AM
5453 if (old_tab != NULL)
5454 {
e5034e59 5455 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 5456 goto error_free_vers;
66eb6687
AM
5457 free (old_tab);
5458 old_tab = NULL;
5459 }
5460
c6e8a9a8
L
5461 /* Now that all the symbols from this input file are created, if
5462 not performing a relocatable link, handle .symver foo, foo@BAR
5463 such that any relocs against foo become foo@BAR. */
0e1862bb 5464 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5 5465 {
ef53be89 5466 size_t cnt, symidx;
4ad4eba5
AM
5467
5468 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5469 {
5470 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5471 char *shortname, *p;
986f0783 5472 size_t amt;
4ad4eba5
AM
5473
5474 p = strchr (h->root.root.string, ELF_VER_CHR);
5475 if (p == NULL
5476 || (h->root.type != bfd_link_hash_defined
5477 && h->root.type != bfd_link_hash_defweak))
5478 continue;
5479
5480 amt = p - h->root.root.string;
a50b1753 5481 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
5482 if (!shortname)
5483 goto error_free_vers;
4ad4eba5
AM
5484 memcpy (shortname, h->root.root.string, amt);
5485 shortname[amt] = '\0';
5486
5487 hi = (struct elf_link_hash_entry *)
66eb6687 5488 bfd_link_hash_lookup (&htab->root, shortname,
0a1b45a2 5489 false, false, false);
4ad4eba5
AM
5490 if (hi != NULL
5491 && hi->root.type == h->root.type
5492 && hi->root.u.def.value == h->root.u.def.value
5493 && hi->root.u.def.section == h->root.u.def.section)
5494 {
0a1b45a2 5495 (*bed->elf_backend_hide_symbol) (info, hi, true);
4ad4eba5
AM
5496 hi->root.type = bfd_link_hash_indirect;
5497 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 5498 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
5499 sym_hash = elf_sym_hashes (abfd);
5500 if (sym_hash)
5501 for (symidx = 0; symidx < extsymcount; ++symidx)
5502 if (sym_hash[symidx] == hi)
5503 {
5504 sym_hash[symidx] = h;
5505 break;
5506 }
5507 }
5508 free (shortname);
5509 }
5510 free (nondeflt_vers);
5511 nondeflt_vers = NULL;
5512 }
5513
60d67dc8 5514 /* Now set the alias field correctly for all the weak defined
4ad4eba5
AM
5515 symbols we found. The only way to do this is to search all the
5516 symbols. Since we only need the information for non functions in
5517 dynamic objects, that's the only time we actually put anything on
5518 the list WEAKS. We need this information so that if a regular
5519 object refers to a symbol defined weakly in a dynamic object, the
5520 real symbol in the dynamic object is also put in the dynamic
5521 symbols; we also must arrange for both symbols to point to the
5522 same memory location. We could handle the general case of symbol
5523 aliasing, but a general symbol alias can only be generated in
5524 assembler code, handling it correctly would be very time
5525 consuming, and other ELF linkers don't handle general aliasing
5526 either. */
5527 if (weaks != NULL)
5528 {
5529 struct elf_link_hash_entry **hpp;
5530 struct elf_link_hash_entry **hppend;
5531 struct elf_link_hash_entry **sorted_sym_hash;
5532 struct elf_link_hash_entry *h;
986f0783 5533 size_t sym_count, amt;
4ad4eba5
AM
5534
5535 /* Since we have to search the whole symbol list for each weak
5536 defined symbol, search time for N weak defined symbols will be
5537 O(N^2). Binary search will cut it down to O(NlogN). */
986f0783 5538 amt = extsymcount * sizeof (*sorted_sym_hash);
3a3f4bf7 5539 sorted_sym_hash = bfd_malloc (amt);
4ad4eba5
AM
5540 if (sorted_sym_hash == NULL)
5541 goto error_return;
5542 sym_hash = sorted_sym_hash;
5543 hpp = elf_sym_hashes (abfd);
5544 hppend = hpp + extsymcount;
5545 sym_count = 0;
5546 for (; hpp < hppend; hpp++)
5547 {
5548 h = *hpp;
5549 if (h != NULL
5550 && h->root.type == bfd_link_hash_defined
fcb93ecf 5551 && !bed->is_function_type (h->type))
4ad4eba5
AM
5552 {
5553 *sym_hash = h;
5554 sym_hash++;
5555 sym_count++;
5556 }
5557 }
5558
3a3f4bf7 5559 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
4ad4eba5
AM
5560 elf_sort_symbol);
5561
5562 while (weaks != NULL)
5563 {
5564 struct elf_link_hash_entry *hlook;
5565 asection *slook;
5566 bfd_vma vlook;
ed54588d 5567 size_t i, j, idx = 0;
4ad4eba5
AM
5568
5569 hlook = weaks;
60d67dc8
AM
5570 weaks = hlook->u.alias;
5571 hlook->u.alias = NULL;
4ad4eba5 5572
e3e53eed
AM
5573 if (hlook->root.type != bfd_link_hash_defined
5574 && hlook->root.type != bfd_link_hash_defweak)
5575 continue;
5576
4ad4eba5
AM
5577 slook = hlook->root.u.def.section;
5578 vlook = hlook->root.u.def.value;
5579
4ad4eba5
AM
5580 i = 0;
5581 j = sym_count;
14160578 5582 while (i != j)
4ad4eba5
AM
5583 {
5584 bfd_signed_vma vdiff;
5585 idx = (i + j) / 2;
14160578 5586 h = sorted_sym_hash[idx];
4ad4eba5
AM
5587 vdiff = vlook - h->root.u.def.value;
5588 if (vdiff < 0)
5589 j = idx;
5590 else if (vdiff > 0)
5591 i = idx + 1;
5592 else
5593 {
d3435ae8 5594 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
5595 if (sdiff < 0)
5596 j = idx;
5597 else if (sdiff > 0)
5598 i = idx + 1;
5599 else
14160578 5600 break;
4ad4eba5
AM
5601 }
5602 }
5603
5604 /* We didn't find a value/section match. */
14160578 5605 if (i == j)
4ad4eba5
AM
5606 continue;
5607
14160578
AM
5608 /* With multiple aliases, or when the weak symbol is already
5609 strongly defined, we have multiple matching symbols and
5610 the binary search above may land on any of them. Step
5611 one past the matching symbol(s). */
5612 while (++idx != j)
5613 {
5614 h = sorted_sym_hash[idx];
5615 if (h->root.u.def.section != slook
5616 || h->root.u.def.value != vlook)
5617 break;
5618 }
5619
5620 /* Now look back over the aliases. Since we sorted by size
5621 as well as value and section, we'll choose the one with
5622 the largest size. */
5623 while (idx-- != i)
4ad4eba5 5624 {
14160578 5625 h = sorted_sym_hash[idx];
4ad4eba5
AM
5626
5627 /* Stop if value or section doesn't match. */
14160578
AM
5628 if (h->root.u.def.section != slook
5629 || h->root.u.def.value != vlook)
4ad4eba5
AM
5630 break;
5631 else if (h != hlook)
5632 {
60d67dc8
AM
5633 struct elf_link_hash_entry *t;
5634
5635 hlook->u.alias = h;
5636 hlook->is_weakalias = 1;
5637 t = h;
5638 if (t->u.alias != NULL)
5639 while (t->u.alias != h)
5640 t = t->u.alias;
5641 t->u.alias = hlook;
4ad4eba5
AM
5642
5643 /* If the weak definition is in the list of dynamic
5644 symbols, make sure the real definition is put
5645 there as well. */
5646 if (hlook->dynindx != -1 && h->dynindx == -1)
5647 {
c152c796 5648 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
5649 {
5650 err_free_sym_hash:
5651 free (sorted_sym_hash);
5652 goto error_return;
5653 }
4ad4eba5
AM
5654 }
5655
5656 /* If the real definition is in the list of dynamic
5657 symbols, make sure the weak definition is put
5658 there as well. If we don't do this, then the
5659 dynamic loader might not merge the entries for the
5660 real definition and the weak definition. */
5661 if (h->dynindx != -1 && hlook->dynindx == -1)
5662 {
c152c796 5663 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 5664 goto err_free_sym_hash;
4ad4eba5
AM
5665 }
5666 break;
5667 }
5668 }
5669 }
5670
5671 free (sorted_sym_hash);
5672 }
5673
33177bb1
AM
5674 if (bed->check_directives
5675 && !(*bed->check_directives) (abfd, info))
0a1b45a2 5676 return false;
85fbca6a 5677
4ad4eba5
AM
5678 /* If this is a non-traditional link, try to optimize the handling
5679 of the .stab/.stabstr sections. */
5680 if (! dynamic
5681 && ! info->traditional_format
2cc15b10 5682 && is_elf_hash_table (&htab->root)
4ad4eba5
AM
5683 && (info->strip != strip_all && info->strip != strip_debugger))
5684 {
5685 asection *stabstr;
5686
5687 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5688 if (stabstr != NULL)
5689 {
5690 bfd_size_type string_offset = 0;
5691 asection *stab;
5692
5693 for (stab = abfd->sections; stab; stab = stab->next)
08dedd66 5694 if (startswith (stab->name, ".stab")
4ad4eba5
AM
5695 && (!stab->name[5] ||
5696 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5697 && (stab->flags & SEC_MERGE) == 0
5698 && !bfd_is_abs_section (stab->output_section))
5699 {
5700 struct bfd_elf_section_data *secdata;
5701
5702 secdata = elf_section_data (stab);
66eb6687
AM
5703 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5704 stabstr, &secdata->sec_info,
4ad4eba5
AM
5705 &string_offset))
5706 goto error_return;
5707 if (secdata->sec_info)
dbaa2011 5708 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5709 }
5710 }
5711 }
5712
e310298c 5713 if (dynamic && add_needed)
4ad4eba5
AM
5714 {
5715 /* Add this bfd to the loaded list. */
5716 struct elf_link_loaded_list *n;
5717
ca4be51c 5718 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5719 if (n == NULL)
5720 goto error_return;
5721 n->abfd = abfd;
e310298c
AM
5722 n->next = htab->dyn_loaded;
5723 htab->dyn_loaded = n;
4ad4eba5 5724 }
e310298c
AM
5725 if (dynamic && !add_needed
5726 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
5727 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
4ad4eba5 5728
0a1b45a2 5729 return true;
4ad4eba5
AM
5730
5731 error_free_vers:
c9594989
AM
5732 free (old_tab);
5733 free (old_strtab);
5734 free (nondeflt_vers);
5735 free (extversym);
4ad4eba5 5736 error_free_sym:
c9594989 5737 free (isymbuf);
4ad4eba5 5738 error_return:
0a1b45a2 5739 return false;
4ad4eba5
AM
5740}
5741
8387904d
AM
5742/* Return the linker hash table entry of a symbol that might be
5743 satisfied by an archive symbol. Return -1 on error. */
5744
b585e899 5745struct bfd_link_hash_entry *
8387904d
AM
5746_bfd_elf_archive_symbol_lookup (bfd *abfd,
5747 struct bfd_link_info *info,
5748 const char *name)
5749{
b585e899 5750 struct bfd_link_hash_entry *h;
8387904d
AM
5751 char *p, *copy;
5752 size_t len, first;
5753
b585e899 5754 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
8387904d
AM
5755 if (h != NULL)
5756 return h;
5757
5758 /* If this is a default version (the name contains @@), look up the
5759 symbol again with only one `@' as well as without the version.
5760 The effect is that references to the symbol with and without the
5761 version will be matched by the default symbol in the archive. */
5762
5763 p = strchr (name, ELF_VER_CHR);
5764 if (p == NULL || p[1] != ELF_VER_CHR)
5765 return h;
5766
5767 /* First check with only one `@'. */
5768 len = strlen (name);
a50b1753 5769 copy = (char *) bfd_alloc (abfd, len);
8387904d 5770 if (copy == NULL)
b585e899 5771 return (struct bfd_link_hash_entry *) -1;
8387904d
AM
5772
5773 first = p - name + 1;
5774 memcpy (copy, name, first);
5775 memcpy (copy + first, name + first + 1, len - first);
5776
b585e899 5777 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
8387904d
AM
5778 if (h == NULL)
5779 {
5780 /* We also need to check references to the symbol without the
5781 version. */
5782 copy[first - 1] = '\0';
b585e899 5783 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
8387904d
AM
5784 }
5785
5786 bfd_release (abfd, copy);
5787 return h;
5788}
5789
0ad989f9 5790/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5791 don't use _bfd_generic_link_add_archive_symbols because we need to
5792 handle versioned symbols.
0ad989f9
L
5793
5794 Fortunately, ELF archive handling is simpler than that done by
5795 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5796 oddities. In ELF, if we find a symbol in the archive map, and the
5797 symbol is currently undefined, we know that we must pull in that
5798 object file.
5799
5800 Unfortunately, we do have to make multiple passes over the symbol
5801 table until nothing further is resolved. */
5802
0a1b45a2 5803static bool
4ad4eba5 5804elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5805{
5806 symindex c;
13e570f8 5807 unsigned char *included = NULL;
0ad989f9 5808 carsym *symdefs;
0a1b45a2 5809 bool loop;
986f0783 5810 size_t amt;
8387904d 5811 const struct elf_backend_data *bed;
b585e899 5812 struct bfd_link_hash_entry * (*archive_symbol_lookup)
8387904d 5813 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5814
5815 if (! bfd_has_map (abfd))
5816 {
5817 /* An empty archive is a special case. */
5818 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
0a1b45a2 5819 return true;
0ad989f9 5820 bfd_set_error (bfd_error_no_armap);
0a1b45a2 5821 return false;
0ad989f9
L
5822 }
5823
5824 /* Keep track of all symbols we know to be already defined, and all
5825 files we know to be already included. This is to speed up the
5826 second and subsequent passes. */
5827 c = bfd_ardata (abfd)->symdef_count;
5828 if (c == 0)
0a1b45a2 5829 return true;
986f0783 5830 amt = c * sizeof (*included);
13e570f8
AM
5831 included = (unsigned char *) bfd_zmalloc (amt);
5832 if (included == NULL)
0a1b45a2 5833 return false;
0ad989f9
L
5834
5835 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5836 bed = get_elf_backend_data (abfd);
5837 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5838
5839 do
5840 {
5841 file_ptr last;
5842 symindex i;
5843 carsym *symdef;
5844 carsym *symdefend;
5845
0a1b45a2 5846 loop = false;
0ad989f9
L
5847 last = -1;
5848
5849 symdef = symdefs;
5850 symdefend = symdef + c;
5851 for (i = 0; symdef < symdefend; symdef++, i++)
5852 {
b585e899 5853 struct bfd_link_hash_entry *h;
0ad989f9
L
5854 bfd *element;
5855 struct bfd_link_hash_entry *undefs_tail;
5856 symindex mark;
5857
13e570f8 5858 if (included[i])
0ad989f9
L
5859 continue;
5860 if (symdef->file_offset == last)
5861 {
0a1b45a2 5862 included[i] = true;
0ad989f9
L
5863 continue;
5864 }
5865
8387904d 5866 h = archive_symbol_lookup (abfd, info, symdef->name);
b585e899 5867 if (h == (struct bfd_link_hash_entry *) -1)
8387904d 5868 goto error_return;
0ad989f9
L
5869
5870 if (h == NULL)
5871 continue;
5872
b585e899 5873 if (h->type == bfd_link_hash_undefined)
75cfe082
AM
5874 {
5875 /* If the archive element has already been loaded then one
5876 of the symbols defined by that element might have been
5877 made undefined due to being in a discarded section. */
b585e899
AM
5878 if (is_elf_hash_table (info->hash)
5879 && ((struct elf_link_hash_entry *) h)->indx == -3)
75cfe082
AM
5880 continue;
5881 }
b585e899 5882 else if (h->type == bfd_link_hash_common)
0ad989f9
L
5883 {
5884 /* We currently have a common symbol. The archive map contains
5885 a reference to this symbol, so we may want to include it. We
5886 only want to include it however, if this archive element
5887 contains a definition of the symbol, not just another common
5888 declaration of it.
5889
5890 Unfortunately some archivers (including GNU ar) will put
5891 declarations of common symbols into their archive maps, as
5892 well as real definitions, so we cannot just go by the archive
5893 map alone. Instead we must read in the element's symbol
5894 table and check that to see what kind of symbol definition
5895 this is. */
5896 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5897 continue;
5898 }
75cfe082 5899 else
0ad989f9 5900 {
b585e899 5901 if (h->type != bfd_link_hash_undefweak)
13e570f8 5902 /* Symbol must be defined. Don't check it again. */
0a1b45a2 5903 included[i] = true;
0ad989f9
L
5904 continue;
5905 }
5906
5907 /* We need to include this archive member. */
5908 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5909 if (element == NULL)
5910 goto error_return;
5911
5912 if (! bfd_check_format (element, bfd_object))
5913 goto error_return;
5914
0ad989f9
L
5915 undefs_tail = info->hash->undefs_tail;
5916
0e144ba7
AM
5917 if (!(*info->callbacks
5918 ->add_archive_element) (info, element, symdef->name, &element))
b95a0a31 5919 continue;
0e144ba7 5920 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5921 goto error_return;
5922
5923 /* If there are any new undefined symbols, we need to make
5924 another pass through the archive in order to see whether
5925 they can be defined. FIXME: This isn't perfect, because
5926 common symbols wind up on undefs_tail and because an
5927 undefined symbol which is defined later on in this pass
5928 does not require another pass. This isn't a bug, but it
5929 does make the code less efficient than it could be. */
5930 if (undefs_tail != info->hash->undefs_tail)
0a1b45a2 5931 loop = true;
0ad989f9
L
5932
5933 /* Look backward to mark all symbols from this object file
5934 which we have already seen in this pass. */
5935 mark = i;
5936 do
5937 {
0a1b45a2 5938 included[mark] = true;
0ad989f9
L
5939 if (mark == 0)
5940 break;
5941 --mark;
5942 }
5943 while (symdefs[mark].file_offset == symdef->file_offset);
5944
5945 /* We mark subsequent symbols from this object file as we go
5946 on through the loop. */
5947 last = symdef->file_offset;
5948 }
5949 }
5950 while (loop);
5951
0ad989f9 5952 free (included);
0a1b45a2 5953 return true;
0ad989f9
L
5954
5955 error_return:
c9594989 5956 free (included);
0a1b45a2 5957 return false;
0ad989f9 5958}
4ad4eba5
AM
5959
5960/* Given an ELF BFD, add symbols to the global hash table as
5961 appropriate. */
5962
0a1b45a2 5963bool
4ad4eba5
AM
5964bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5965{
5966 switch (bfd_get_format (abfd))
5967 {
5968 case bfd_object:
5969 return elf_link_add_object_symbols (abfd, info);
5970 case bfd_archive:
5971 return elf_link_add_archive_symbols (abfd, info);
5972 default:
5973 bfd_set_error (bfd_error_wrong_format);
0a1b45a2 5974 return false;
4ad4eba5
AM
5975 }
5976}
5a580b3a 5977\f
14b1c01e
AM
5978struct hash_codes_info
5979{
5980 unsigned long *hashcodes;
0a1b45a2 5981 bool error;
14b1c01e 5982};
a0c8462f 5983
5a580b3a
AM
5984/* This function will be called though elf_link_hash_traverse to store
5985 all hash value of the exported symbols in an array. */
5986
0a1b45a2 5987static bool
5a580b3a
AM
5988elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5989{
a50b1753 5990 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5991 const char *name;
5a580b3a
AM
5992 unsigned long ha;
5993 char *alc = NULL;
5994
5a580b3a
AM
5995 /* Ignore indirect symbols. These are added by the versioning code. */
5996 if (h->dynindx == -1)
0a1b45a2 5997 return true;
5a580b3a
AM
5998
5999 name = h->root.root.string;
422f1182 6000 if (h->versioned >= versioned)
5a580b3a 6001 {
422f1182
L
6002 char *p = strchr (name, ELF_VER_CHR);
6003 if (p != NULL)
14b1c01e 6004 {
422f1182
L
6005 alc = (char *) bfd_malloc (p - name + 1);
6006 if (alc == NULL)
6007 {
0a1b45a2
AM
6008 inf->error = true;
6009 return false;
422f1182
L
6010 }
6011 memcpy (alc, name, p - name);
6012 alc[p - name] = '\0';
6013 name = alc;
14b1c01e 6014 }
5a580b3a
AM
6015 }
6016
6017 /* Compute the hash value. */
6018 ha = bfd_elf_hash (name);
6019
6020 /* Store the found hash value in the array given as the argument. */
14b1c01e 6021 *(inf->hashcodes)++ = ha;
5a580b3a
AM
6022
6023 /* And store it in the struct so that we can put it in the hash table
6024 later. */
f6e332e6 6025 h->u.elf_hash_value = ha;
5a580b3a 6026
c9594989 6027 free (alc);
0a1b45a2 6028 return true;
5a580b3a
AM
6029}
6030
fdc90cb4
JJ
6031struct collect_gnu_hash_codes
6032{
6033 bfd *output_bfd;
6034 const struct elf_backend_data *bed;
6035 unsigned long int nsyms;
6036 unsigned long int maskbits;
6037 unsigned long int *hashcodes;
6038 unsigned long int *hashval;
6039 unsigned long int *indx;
6040 unsigned long int *counts;
6041 bfd_vma *bitmask;
6042 bfd_byte *contents;
f16a9783 6043 bfd_size_type xlat;
fdc90cb4
JJ
6044 long int min_dynindx;
6045 unsigned long int bucketcount;
6046 unsigned long int symindx;
6047 long int local_indx;
6048 long int shift1, shift2;
6049 unsigned long int mask;
0a1b45a2 6050 bool error;
fdc90cb4
JJ
6051};
6052
6053/* This function will be called though elf_link_hash_traverse to store
6054 all hash value of the exported symbols in an array. */
6055
0a1b45a2 6056static bool
fdc90cb4
JJ
6057elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6058{
a50b1753 6059 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 6060 const char *name;
fdc90cb4
JJ
6061 unsigned long ha;
6062 char *alc = NULL;
6063
fdc90cb4
JJ
6064 /* Ignore indirect symbols. These are added by the versioning code. */
6065 if (h->dynindx == -1)
0a1b45a2 6066 return true;
fdc90cb4
JJ
6067
6068 /* Ignore also local symbols and undefined symbols. */
6069 if (! (*s->bed->elf_hash_symbol) (h))
0a1b45a2 6070 return true;
fdc90cb4
JJ
6071
6072 name = h->root.root.string;
422f1182 6073 if (h->versioned >= versioned)
fdc90cb4 6074 {
422f1182
L
6075 char *p = strchr (name, ELF_VER_CHR);
6076 if (p != NULL)
14b1c01e 6077 {
422f1182
L
6078 alc = (char *) bfd_malloc (p - name + 1);
6079 if (alc == NULL)
6080 {
0a1b45a2
AM
6081 s->error = true;
6082 return false;
422f1182
L
6083 }
6084 memcpy (alc, name, p - name);
6085 alc[p - name] = '\0';
6086 name = alc;
14b1c01e 6087 }
fdc90cb4
JJ
6088 }
6089
6090 /* Compute the hash value. */
6091 ha = bfd_elf_gnu_hash (name);
6092
6093 /* Store the found hash value in the array for compute_bucket_count,
6094 and also for .dynsym reordering purposes. */
6095 s->hashcodes[s->nsyms] = ha;
6096 s->hashval[h->dynindx] = ha;
6097 ++s->nsyms;
6098 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6099 s->min_dynindx = h->dynindx;
6100
c9594989 6101 free (alc);
0a1b45a2 6102 return true;
fdc90cb4
JJ
6103}
6104
6105/* This function will be called though elf_link_hash_traverse to do
f16a9783
MS
6106 final dynamic symbol renumbering in case of .gnu.hash.
6107 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6108 to the translation table. */
fdc90cb4 6109
0a1b45a2 6110static bool
f16a9783 6111elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
fdc90cb4 6112{
a50b1753 6113 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
6114 unsigned long int bucket;
6115 unsigned long int val;
6116
fdc90cb4
JJ
6117 /* Ignore indirect symbols. */
6118 if (h->dynindx == -1)
0a1b45a2 6119 return true;
fdc90cb4
JJ
6120
6121 /* Ignore also local symbols and undefined symbols. */
6122 if (! (*s->bed->elf_hash_symbol) (h))
6123 {
6124 if (h->dynindx >= s->min_dynindx)
f16a9783
MS
6125 {
6126 if (s->bed->record_xhash_symbol != NULL)
6127 {
6128 (*s->bed->record_xhash_symbol) (h, 0);
6129 s->local_indx++;
6130 }
6131 else
6132 h->dynindx = s->local_indx++;
6133 }
0a1b45a2 6134 return true;
fdc90cb4
JJ
6135 }
6136
6137 bucket = s->hashval[h->dynindx] % s->bucketcount;
6138 val = (s->hashval[h->dynindx] >> s->shift1)
6139 & ((s->maskbits >> s->shift1) - 1);
6140 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6141 s->bitmask[val]
6142 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6143 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6144 if (s->counts[bucket] == 1)
6145 /* Last element terminates the chain. */
6146 val |= 1;
6147 bfd_put_32 (s->output_bfd, val,
6148 s->contents + (s->indx[bucket] - s->symindx) * 4);
6149 --s->counts[bucket];
f16a9783
MS
6150 if (s->bed->record_xhash_symbol != NULL)
6151 {
6152 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6153
6154 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6155 }
6156 else
6157 h->dynindx = s->indx[bucket]++;
0a1b45a2 6158 return true;
fdc90cb4
JJ
6159}
6160
6161/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6162
0a1b45a2 6163bool
fdc90cb4
JJ
6164_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6165{
6166 return !(h->forced_local
6167 || h->root.type == bfd_link_hash_undefined
6168 || h->root.type == bfd_link_hash_undefweak
6169 || ((h->root.type == bfd_link_hash_defined
6170 || h->root.type == bfd_link_hash_defweak)
6171 && h->root.u.def.section->output_section == NULL));
6172}
6173
5a580b3a
AM
6174/* Array used to determine the number of hash table buckets to use
6175 based on the number of symbols there are. If there are fewer than
6176 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6177 fewer than 37 we use 17 buckets, and so forth. We never use more
6178 than 32771 buckets. */
6179
6180static const size_t elf_buckets[] =
6181{
6182 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6183 16411, 32771, 0
6184};
6185
6186/* Compute bucket count for hashing table. We do not use a static set
6187 of possible tables sizes anymore. Instead we determine for all
6188 possible reasonable sizes of the table the outcome (i.e., the
6189 number of collisions etc) and choose the best solution. The
6190 weighting functions are not too simple to allow the table to grow
6191 without bounds. Instead one of the weighting factors is the size.
6192 Therefore the result is always a good payoff between few collisions
6193 (= short chain lengths) and table size. */
6194static size_t
b20dd2ce 6195compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
6196 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6197 unsigned long int nsyms,
6198 int gnu_hash)
5a580b3a 6199{
5a580b3a 6200 size_t best_size = 0;
5a580b3a 6201 unsigned long int i;
5a580b3a 6202
5a580b3a
AM
6203 /* We have a problem here. The following code to optimize the table
6204 size requires an integer type with more the 32 bits. If
6205 BFD_HOST_U_64_BIT is set we know about such a type. */
6206#ifdef BFD_HOST_U_64_BIT
6207 if (info->optimize)
6208 {
5a580b3a
AM
6209 size_t minsize;
6210 size_t maxsize;
6211 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 6212 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 6213 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 6214 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 6215 unsigned long int *counts;
d40f3da9 6216 bfd_size_type amt;
0883b6e0 6217 unsigned int no_improvement_count = 0;
5a580b3a
AM
6218
6219 /* Possible optimization parameters: if we have NSYMS symbols we say
6220 that the hashing table must at least have NSYMS/4 and at most
6221 2*NSYMS buckets. */
6222 minsize = nsyms / 4;
6223 if (minsize == 0)
6224 minsize = 1;
6225 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
6226 if (gnu_hash)
6227 {
6228 if (minsize < 2)
6229 minsize = 2;
6230 if ((best_size & 31) == 0)
6231 ++best_size;
6232 }
5a580b3a
AM
6233
6234 /* Create array where we count the collisions in. We must use bfd_malloc
6235 since the size could be large. */
6236 amt = maxsize;
6237 amt *= sizeof (unsigned long int);
a50b1753 6238 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 6239 if (counts == NULL)
fdc90cb4 6240 return 0;
5a580b3a
AM
6241
6242 /* Compute the "optimal" size for the hash table. The criteria is a
6243 minimal chain length. The minor criteria is (of course) the size
6244 of the table. */
6245 for (i = minsize; i < maxsize; ++i)
6246 {
6247 /* Walk through the array of hashcodes and count the collisions. */
6248 BFD_HOST_U_64_BIT max;
6249 unsigned long int j;
6250 unsigned long int fact;
6251
fdc90cb4
JJ
6252 if (gnu_hash && (i & 31) == 0)
6253 continue;
6254
5a580b3a
AM
6255 memset (counts, '\0', i * sizeof (unsigned long int));
6256
6257 /* Determine how often each hash bucket is used. */
6258 for (j = 0; j < nsyms; ++j)
6259 ++counts[hashcodes[j] % i];
6260
6261 /* For the weight function we need some information about the
6262 pagesize on the target. This is information need not be 100%
6263 accurate. Since this information is not available (so far) we
6264 define it here to a reasonable default value. If it is crucial
6265 to have a better value some day simply define this value. */
6266# ifndef BFD_TARGET_PAGESIZE
6267# define BFD_TARGET_PAGESIZE (4096)
6268# endif
6269
fdc90cb4
JJ
6270 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6271 and the chains. */
6272 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
6273
6274# if 1
6275 /* Variant 1: optimize for short chains. We add the squares
6276 of all the chain lengths (which favors many small chain
6277 over a few long chains). */
6278 for (j = 0; j < i; ++j)
6279 max += counts[j] * counts[j];
6280
6281 /* This adds penalties for the overall size of the table. */
fdc90cb4 6282 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6283 max *= fact * fact;
6284# else
6285 /* Variant 2: Optimize a lot more for small table. Here we
6286 also add squares of the size but we also add penalties for
6287 empty slots (the +1 term). */
6288 for (j = 0; j < i; ++j)
6289 max += (1 + counts[j]) * (1 + counts[j]);
6290
6291 /* The overall size of the table is considered, but not as
6292 strong as in variant 1, where it is squared. */
fdc90cb4 6293 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6294 max *= fact;
6295# endif
6296
6297 /* Compare with current best results. */
6298 if (max < best_chlen)
6299 {
6300 best_chlen = max;
6301 best_size = i;
ca4be51c 6302 no_improvement_count = 0;
5a580b3a 6303 }
0883b6e0
NC
6304 /* PR 11843: Avoid futile long searches for the best bucket size
6305 when there are a large number of symbols. */
6306 else if (++no_improvement_count == 100)
6307 break;
5a580b3a
AM
6308 }
6309
6310 free (counts);
6311 }
6312 else
6313#endif /* defined (BFD_HOST_U_64_BIT) */
6314 {
6315 /* This is the fallback solution if no 64bit type is available or if we
6316 are not supposed to spend much time on optimizations. We select the
6317 bucket count using a fixed set of numbers. */
6318 for (i = 0; elf_buckets[i] != 0; i++)
6319 {
6320 best_size = elf_buckets[i];
fdc90cb4 6321 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
6322 break;
6323 }
fdc90cb4
JJ
6324 if (gnu_hash && best_size < 2)
6325 best_size = 2;
5a580b3a
AM
6326 }
6327
5a580b3a
AM
6328 return best_size;
6329}
6330
d0bf826b
AM
6331/* Size any SHT_GROUP section for ld -r. */
6332
0a1b45a2 6333bool
d0bf826b
AM
6334_bfd_elf_size_group_sections (struct bfd_link_info *info)
6335{
6336 bfd *ibfd;
57963c05 6337 asection *s;
d0bf826b 6338
c72f2fb2 6339 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b 6340 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
57963c05
AM
6341 && (s = ibfd->sections) != NULL
6342 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
d0bf826b 6343 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
0a1b45a2
AM
6344 return false;
6345 return true;
d0bf826b
AM
6346}
6347
04c3a755
NS
6348/* Set a default stack segment size. The value in INFO wins. If it
6349 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6350 undefined it is initialized. */
6351
0a1b45a2 6352bool
04c3a755
NS
6353bfd_elf_stack_segment_size (bfd *output_bfd,
6354 struct bfd_link_info *info,
6355 const char *legacy_symbol,
6356 bfd_vma default_size)
6357{
6358 struct elf_link_hash_entry *h = NULL;
6359
6360 /* Look for legacy symbol. */
6361 if (legacy_symbol)
6362 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
0a1b45a2 6363 false, false, false);
04c3a755
NS
6364 if (h && (h->root.type == bfd_link_hash_defined
6365 || h->root.type == bfd_link_hash_defweak)
6366 && h->def_regular
6367 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6368 {
6369 /* The symbol has no type if specified on the command line. */
6370 h->type = STT_OBJECT;
6371 if (info->stacksize)
695344c0 6372 /* xgettext:c-format */
871b3ab2 6373 _bfd_error_handler (_("%pB: stack size specified and %s set"),
4eca0228 6374 output_bfd, legacy_symbol);
04c3a755 6375 else if (h->root.u.def.section != bfd_abs_section_ptr)
695344c0 6376 /* xgettext:c-format */
871b3ab2 6377 _bfd_error_handler (_("%pB: %s not absolute"),
4eca0228 6378 output_bfd, legacy_symbol);
04c3a755
NS
6379 else
6380 info->stacksize = h->root.u.def.value;
6381 }
6382
6383 if (!info->stacksize)
6384 /* If the user didn't set a size, or explicitly inhibit the
6385 size, set it now. */
6386 info->stacksize = default_size;
6387
6388 /* Provide the legacy symbol, if it is referenced. */
6389 if (h && (h->root.type == bfd_link_hash_undefined
6390 || h->root.type == bfd_link_hash_undefweak))
6391 {
6392 struct bfd_link_hash_entry *bh = NULL;
6393
6394 if (!(_bfd_generic_link_add_one_symbol
6395 (info, output_bfd, legacy_symbol,
6396 BSF_GLOBAL, bfd_abs_section_ptr,
6397 info->stacksize >= 0 ? info->stacksize : 0,
0a1b45a2
AM
6398 NULL, false, get_elf_backend_data (output_bfd)->collect, &bh)))
6399 return false;
04c3a755
NS
6400
6401 h = (struct elf_link_hash_entry *) bh;
6402 h->def_regular = 1;
6403 h->type = STT_OBJECT;
6404 }
6405
0a1b45a2 6406 return true;
04c3a755
NS
6407}
6408
b531344c
MR
6409/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6410
6411struct elf_gc_sweep_symbol_info
6412{
6413 struct bfd_link_info *info;
6414 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
0a1b45a2 6415 bool);
b531344c
MR
6416};
6417
0a1b45a2 6418static bool
b531344c
MR
6419elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6420{
6421 if (!h->mark
6422 && (((h->root.type == bfd_link_hash_defined
6423 || h->root.type == bfd_link_hash_defweak)
6424 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6425 && h->root.u.def.section->gc_mark))
6426 || h->root.type == bfd_link_hash_undefined
6427 || h->root.type == bfd_link_hash_undefweak))
6428 {
6429 struct elf_gc_sweep_symbol_info *inf;
6430
6431 inf = (struct elf_gc_sweep_symbol_info *) data;
0a1b45a2 6432 (*inf->hide_symbol) (inf->info, h, true);
b531344c
MR
6433 h->def_regular = 0;
6434 h->ref_regular = 0;
6435 h->ref_regular_nonweak = 0;
6436 }
6437
0a1b45a2 6438 return true;
b531344c
MR
6439}
6440
5a580b3a
AM
6441/* Set up the sizes and contents of the ELF dynamic sections. This is
6442 called by the ELF linker emulation before_allocation routine. We
6443 must set the sizes of the sections before the linker sets the
6444 addresses of the various sections. */
6445
0a1b45a2 6446bool
5a580b3a
AM
6447bfd_elf_size_dynamic_sections (bfd *output_bfd,
6448 const char *soname,
6449 const char *rpath,
6450 const char *filter_shlib,
7ee314fa
AM
6451 const char *audit,
6452 const char *depaudit,
5a580b3a
AM
6453 const char * const *auxiliary_filters,
6454 struct bfd_link_info *info,
fd91d419 6455 asection **sinterpptr)
5a580b3a 6456{
5a580b3a
AM
6457 bfd *dynobj;
6458 const struct elf_backend_data *bed;
5a580b3a
AM
6459
6460 *sinterpptr = NULL;
6461
5a580b3a 6462 if (!is_elf_hash_table (info->hash))
0a1b45a2 6463 return true;
5a580b3a 6464
5a580b3a
AM
6465 dynobj = elf_hash_table (info)->dynobj;
6466
9a2a56cc 6467 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 6468 {
902e9fc7
MR
6469 struct bfd_elf_version_tree *verdefs;
6470 struct elf_info_failed asvinfo;
5a580b3a
AM
6471 struct bfd_elf_version_tree *t;
6472 struct bfd_elf_version_expr *d;
902e9fc7 6473 asection *s;
e6699019 6474 size_t soname_indx;
7ee314fa 6475
5a580b3a
AM
6476 /* If we are supposed to export all symbols into the dynamic symbol
6477 table (this is not the normal case), then do so. */
55255dae 6478 if (info->export_dynamic
0e1862bb 6479 || (bfd_link_executable (info) && info->dynamic))
5a580b3a 6480 {
3d13f3e9
AM
6481 struct elf_info_failed eif;
6482
6483 eif.info = info;
0a1b45a2 6484 eif.failed = false;
5a580b3a
AM
6485 elf_link_hash_traverse (elf_hash_table (info),
6486 _bfd_elf_export_symbol,
6487 &eif);
6488 if (eif.failed)
0a1b45a2 6489 return false;
5a580b3a
AM
6490 }
6491
e6699019
L
6492 if (soname != NULL)
6493 {
6494 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
0a1b45a2 6495 soname, true);
e6699019
L
6496 if (soname_indx == (size_t) -1
6497 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
0a1b45a2 6498 return false;
e6699019
L
6499 }
6500 else
6501 soname_indx = (size_t) -1;
6502
5a580b3a 6503 /* Make all global versions with definition. */
fd91d419 6504 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6505 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6506 if (!d->symver && d->literal)
5a580b3a
AM
6507 {
6508 const char *verstr, *name;
6509 size_t namelen, verlen, newlen;
93252b1c 6510 char *newname, *p, leading_char;
5a580b3a
AM
6511 struct elf_link_hash_entry *newh;
6512
93252b1c 6513 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 6514 name = d->pattern;
93252b1c 6515 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
6516 verstr = t->name;
6517 verlen = strlen (verstr);
6518 newlen = namelen + verlen + 3;
6519
a50b1753 6520 newname = (char *) bfd_malloc (newlen);
5a580b3a 6521 if (newname == NULL)
0a1b45a2 6522 return false;
93252b1c
MF
6523 newname[0] = leading_char;
6524 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
6525
6526 /* Check the hidden versioned definition. */
6527 p = newname + namelen;
6528 *p++ = ELF_VER_CHR;
6529 memcpy (p, verstr, verlen + 1);
6530 newh = elf_link_hash_lookup (elf_hash_table (info),
0a1b45a2
AM
6531 newname, false, false,
6532 false);
5a580b3a
AM
6533 if (newh == NULL
6534 || (newh->root.type != bfd_link_hash_defined
6535 && newh->root.type != bfd_link_hash_defweak))
6536 {
6537 /* Check the default versioned definition. */
6538 *p++ = ELF_VER_CHR;
6539 memcpy (p, verstr, verlen + 1);
6540 newh = elf_link_hash_lookup (elf_hash_table (info),
0a1b45a2
AM
6541 newname, false, false,
6542 false);
5a580b3a
AM
6543 }
6544 free (newname);
6545
6546 /* Mark this version if there is a definition and it is
6547 not defined in a shared object. */
6548 if (newh != NULL
f5385ebf 6549 && !newh->def_dynamic
5a580b3a
AM
6550 && (newh->root.type == bfd_link_hash_defined
6551 || newh->root.type == bfd_link_hash_defweak))
6552 d->symver = 1;
6553 }
6554
6555 /* Attach all the symbols to their version information. */
5a580b3a 6556 asvinfo.info = info;
0a1b45a2 6557 asvinfo.failed = false;
5a580b3a
AM
6558
6559 elf_link_hash_traverse (elf_hash_table (info),
6560 _bfd_elf_link_assign_sym_version,
6561 &asvinfo);
6562 if (asvinfo.failed)
0a1b45a2 6563 return false;
5a580b3a
AM
6564
6565 if (!info->allow_undefined_version)
6566 {
6567 /* Check if all global versions have a definition. */
0a1b45a2 6568 bool all_defined = true;
fd91d419 6569 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6570 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6571 if (d->literal && !d->symver && !d->script)
5a580b3a 6572 {
4eca0228 6573 _bfd_error_handler
5a580b3a
AM
6574 (_("%s: undefined version: %s"),
6575 d->pattern, t->name);
0a1b45a2 6576 all_defined = false;
5a580b3a
AM
6577 }
6578
6579 if (!all_defined)
6580 {
6581 bfd_set_error (bfd_error_bad_value);
0a1b45a2 6582 return false;
5a580b3a
AM
6583 }
6584 }
6585
902e9fc7
MR
6586 /* Set up the version definition section. */
6587 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6588 BFD_ASSERT (s != NULL);
5a580b3a 6589
902e9fc7
MR
6590 /* We may have created additional version definitions if we are
6591 just linking a regular application. */
6592 verdefs = info->version_info;
5a580b3a 6593
902e9fc7
MR
6594 /* Skip anonymous version tag. */
6595 if (verdefs != NULL && verdefs->vernum == 0)
6596 verdefs = verdefs->next;
5a580b3a 6597
902e9fc7
MR
6598 if (verdefs == NULL && !info->create_default_symver)
6599 s->flags |= SEC_EXCLUDE;
6600 else
5a580b3a 6601 {
902e9fc7
MR
6602 unsigned int cdefs;
6603 bfd_size_type size;
6604 bfd_byte *p;
6605 Elf_Internal_Verdef def;
6606 Elf_Internal_Verdaux defaux;
6607 struct bfd_link_hash_entry *bh;
6608 struct elf_link_hash_entry *h;
6609 const char *name;
5a580b3a 6610
902e9fc7
MR
6611 cdefs = 0;
6612 size = 0;
5a580b3a 6613
902e9fc7
MR
6614 /* Make space for the base version. */
6615 size += sizeof (Elf_External_Verdef);
6616 size += sizeof (Elf_External_Verdaux);
6617 ++cdefs;
6618
6619 /* Make space for the default version. */
6620 if (info->create_default_symver)
6621 {
6622 size += sizeof (Elf_External_Verdef);
6623 ++cdefs;
3e3b46e5
PB
6624 }
6625
5a580b3a
AM
6626 for (t = verdefs; t != NULL; t = t->next)
6627 {
6628 struct bfd_elf_version_deps *n;
6629
a6cc6b3b
RO
6630 /* Don't emit base version twice. */
6631 if (t->vernum == 0)
6632 continue;
6633
5a580b3a
AM
6634 size += sizeof (Elf_External_Verdef);
6635 size += sizeof (Elf_External_Verdaux);
6636 ++cdefs;
6637
6638 for (n = t->deps; n != NULL; n = n->next)
6639 size += sizeof (Elf_External_Verdaux);
6640 }
6641
eea6121a 6642 s->size = size;
a50b1753 6643 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6644 if (s->contents == NULL && s->size != 0)
0a1b45a2 6645 return false;
5a580b3a
AM
6646
6647 /* Fill in the version definition section. */
6648
6649 p = s->contents;
6650
6651 def.vd_version = VER_DEF_CURRENT;
6652 def.vd_flags = VER_FLG_BASE;
6653 def.vd_ndx = 1;
6654 def.vd_cnt = 1;
3e3b46e5
PB
6655 if (info->create_default_symver)
6656 {
6657 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6658 def.vd_next = sizeof (Elf_External_Verdef);
6659 }
6660 else
6661 {
6662 def.vd_aux = sizeof (Elf_External_Verdef);
6663 def.vd_next = (sizeof (Elf_External_Verdef)
6664 + sizeof (Elf_External_Verdaux));
6665 }
5a580b3a 6666
ef53be89 6667 if (soname_indx != (size_t) -1)
5a580b3a
AM
6668 {
6669 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6670 soname_indx);
6671 def.vd_hash = bfd_elf_hash (soname);
6672 defaux.vda_name = soname_indx;
3e3b46e5 6673 name = soname;
5a580b3a
AM
6674 }
6675 else
6676 {
ef53be89 6677 size_t indx;
5a580b3a 6678
765cf5f6 6679 name = lbasename (bfd_get_filename (output_bfd));
5a580b3a
AM
6680 def.vd_hash = bfd_elf_hash (name);
6681 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
0a1b45a2 6682 name, false);
ef53be89 6683 if (indx == (size_t) -1)
0a1b45a2 6684 return false;
5a580b3a
AM
6685 defaux.vda_name = indx;
6686 }
6687 defaux.vda_next = 0;
6688
6689 _bfd_elf_swap_verdef_out (output_bfd, &def,
6690 (Elf_External_Verdef *) p);
6691 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6692 if (info->create_default_symver)
6693 {
6694 /* Add a symbol representing this version. */
6695 bh = NULL;
6696 if (! (_bfd_generic_link_add_one_symbol
6697 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
0a1b45a2 6698 0, NULL, false,
3e3b46e5 6699 get_elf_backend_data (dynobj)->collect, &bh)))
0a1b45a2 6700 return false;
3e3b46e5
PB
6701 h = (struct elf_link_hash_entry *) bh;
6702 h->non_elf = 0;
6703 h->def_regular = 1;
6704 h->type = STT_OBJECT;
6705 h->verinfo.vertree = NULL;
6706
6707 if (! bfd_elf_link_record_dynamic_symbol (info, h))
0a1b45a2 6708 return false;
3e3b46e5
PB
6709
6710 /* Create a duplicate of the base version with the same
6711 aux block, but different flags. */
6712 def.vd_flags = 0;
6713 def.vd_ndx = 2;
6714 def.vd_aux = sizeof (Elf_External_Verdef);
6715 if (verdefs)
6716 def.vd_next = (sizeof (Elf_External_Verdef)
6717 + sizeof (Elf_External_Verdaux));
6718 else
6719 def.vd_next = 0;
6720 _bfd_elf_swap_verdef_out (output_bfd, &def,
6721 (Elf_External_Verdef *) p);
6722 p += sizeof (Elf_External_Verdef);
6723 }
5a580b3a
AM
6724 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6725 (Elf_External_Verdaux *) p);
6726 p += sizeof (Elf_External_Verdaux);
6727
6728 for (t = verdefs; t != NULL; t = t->next)
6729 {
6730 unsigned int cdeps;
6731 struct bfd_elf_version_deps *n;
5a580b3a 6732
a6cc6b3b
RO
6733 /* Don't emit the base version twice. */
6734 if (t->vernum == 0)
6735 continue;
6736
5a580b3a
AM
6737 cdeps = 0;
6738 for (n = t->deps; n != NULL; n = n->next)
6739 ++cdeps;
6740
6741 /* Add a symbol representing this version. */
6742 bh = NULL;
6743 if (! (_bfd_generic_link_add_one_symbol
6744 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
0a1b45a2 6745 0, NULL, false,
5a580b3a 6746 get_elf_backend_data (dynobj)->collect, &bh)))
0a1b45a2 6747 return false;
5a580b3a 6748 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6749 h->non_elf = 0;
6750 h->def_regular = 1;
5a580b3a
AM
6751 h->type = STT_OBJECT;
6752 h->verinfo.vertree = t;
6753
c152c796 6754 if (! bfd_elf_link_record_dynamic_symbol (info, h))
0a1b45a2 6755 return false;
5a580b3a
AM
6756
6757 def.vd_version = VER_DEF_CURRENT;
6758 def.vd_flags = 0;
6759 if (t->globals.list == NULL
6760 && t->locals.list == NULL
6761 && ! t->used)
6762 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6763 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6764 def.vd_cnt = cdeps + 1;
6765 def.vd_hash = bfd_elf_hash (t->name);
6766 def.vd_aux = sizeof (Elf_External_Verdef);
6767 def.vd_next = 0;
a6cc6b3b
RO
6768
6769 /* If a basever node is next, it *must* be the last node in
6770 the chain, otherwise Verdef construction breaks. */
6771 if (t->next != NULL && t->next->vernum == 0)
6772 BFD_ASSERT (t->next->next == NULL);
6773
6774 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6775 def.vd_next = (sizeof (Elf_External_Verdef)
6776 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6777
6778 _bfd_elf_swap_verdef_out (output_bfd, &def,
6779 (Elf_External_Verdef *) p);
6780 p += sizeof (Elf_External_Verdef);
6781
6782 defaux.vda_name = h->dynstr_index;
6783 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6784 h->dynstr_index);
6785 defaux.vda_next = 0;
6786 if (t->deps != NULL)
6787 defaux.vda_next = sizeof (Elf_External_Verdaux);
6788 t->name_indx = defaux.vda_name;
6789
6790 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6791 (Elf_External_Verdaux *) p);
6792 p += sizeof (Elf_External_Verdaux);
6793
6794 for (n = t->deps; n != NULL; n = n->next)
6795 {
6796 if (n->version_needed == NULL)
6797 {
6798 /* This can happen if there was an error in the
6799 version script. */
6800 defaux.vda_name = 0;
6801 }
6802 else
6803 {
6804 defaux.vda_name = n->version_needed->name_indx;
6805 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6806 defaux.vda_name);
6807 }
6808 if (n->next == NULL)
6809 defaux.vda_next = 0;
6810 else
6811 defaux.vda_next = sizeof (Elf_External_Verdaux);
6812
6813 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6814 (Elf_External_Verdaux *) p);
6815 p += sizeof (Elf_External_Verdaux);
6816 }
6817 }
6818
5a580b3a
AM
6819 elf_tdata (output_bfd)->cverdefs = cdefs;
6820 }
902e9fc7
MR
6821 }
6822
6823 bed = get_elf_backend_data (output_bfd);
6824
6825 if (info->gc_sections && bed->can_gc_sections)
6826 {
6827 struct elf_gc_sweep_symbol_info sweep_info;
902e9fc7
MR
6828
6829 /* Remove the symbols that were in the swept sections from the
3d13f3e9 6830 dynamic symbol table. */
902e9fc7
MR
6831 sweep_info.info = info;
6832 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6833 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6834 &sweep_info);
3d13f3e9
AM
6835 }
6836
6837 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6838 {
6839 asection *s;
6840 struct elf_find_verdep_info sinfo;
6841
6842 /* Work out the size of the version reference section. */
6843
6844 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6845 BFD_ASSERT (s != NULL);
902e9fc7 6846
3d13f3e9
AM
6847 sinfo.info = info;
6848 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6849 if (sinfo.vers == 0)
6850 sinfo.vers = 1;
0a1b45a2 6851 sinfo.failed = false;
3d13f3e9
AM
6852
6853 elf_link_hash_traverse (elf_hash_table (info),
6854 _bfd_elf_link_find_version_dependencies,
6855 &sinfo);
6856 if (sinfo.failed)
0a1b45a2 6857 return false;
3d13f3e9
AM
6858
6859 if (elf_tdata (output_bfd)->verref == NULL)
6860 s->flags |= SEC_EXCLUDE;
6861 else
6862 {
6863 Elf_Internal_Verneed *vn;
6864 unsigned int size;
6865 unsigned int crefs;
6866 bfd_byte *p;
6867
6868 /* Build the version dependency section. */
6869 size = 0;
6870 crefs = 0;
6871 for (vn = elf_tdata (output_bfd)->verref;
6872 vn != NULL;
6873 vn = vn->vn_nextref)
6874 {
6875 Elf_Internal_Vernaux *a;
6876
6877 size += sizeof (Elf_External_Verneed);
6878 ++crefs;
6879 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6880 size += sizeof (Elf_External_Vernaux);
6881 }
6882
6883 s->size = size;
6884 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6885 if (s->contents == NULL)
0a1b45a2 6886 return false;
3d13f3e9
AM
6887
6888 p = s->contents;
6889 for (vn = elf_tdata (output_bfd)->verref;
6890 vn != NULL;
6891 vn = vn->vn_nextref)
6892 {
6893 unsigned int caux;
6894 Elf_Internal_Vernaux *a;
6895 size_t indx;
6896
6897 caux = 0;
6898 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6899 ++caux;
6900
6901 vn->vn_version = VER_NEED_CURRENT;
6902 vn->vn_cnt = caux;
6903 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6904 elf_dt_name (vn->vn_bfd) != NULL
6905 ? elf_dt_name (vn->vn_bfd)
765cf5f6
AM
6906 : lbasename (bfd_get_filename
6907 (vn->vn_bfd)),
0a1b45a2 6908 false);
3d13f3e9 6909 if (indx == (size_t) -1)
0a1b45a2 6910 return false;
3d13f3e9
AM
6911 vn->vn_file = indx;
6912 vn->vn_aux = sizeof (Elf_External_Verneed);
6913 if (vn->vn_nextref == NULL)
6914 vn->vn_next = 0;
6915 else
6916 vn->vn_next = (sizeof (Elf_External_Verneed)
6917 + caux * sizeof (Elf_External_Vernaux));
6918
6919 _bfd_elf_swap_verneed_out (output_bfd, vn,
6920 (Elf_External_Verneed *) p);
6921 p += sizeof (Elf_External_Verneed);
6922
6923 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6924 {
6925 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6926 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
0a1b45a2 6927 a->vna_nodename, false);
3d13f3e9 6928 if (indx == (size_t) -1)
0a1b45a2 6929 return false;
3d13f3e9
AM
6930 a->vna_name = indx;
6931 if (a->vna_nextptr == NULL)
6932 a->vna_next = 0;
6933 else
6934 a->vna_next = sizeof (Elf_External_Vernaux);
6935
6936 _bfd_elf_swap_vernaux_out (output_bfd, a,
6937 (Elf_External_Vernaux *) p);
6938 p += sizeof (Elf_External_Vernaux);
6939 }
6940 }
6941
6942 elf_tdata (output_bfd)->cverrefs = crefs;
6943 }
902e9fc7
MR
6944 }
6945
6946 /* Any syms created from now on start with -1 in
6947 got.refcount/offset and plt.refcount/offset. */
6948 elf_hash_table (info)->init_got_refcount
6949 = elf_hash_table (info)->init_got_offset;
6950 elf_hash_table (info)->init_plt_refcount
6951 = elf_hash_table (info)->init_plt_offset;
6952
6953 if (bfd_link_relocatable (info)
6954 && !_bfd_elf_size_group_sections (info))
0a1b45a2 6955 return false;
902e9fc7
MR
6956
6957 /* The backend may have to create some sections regardless of whether
6958 we're dynamic or not. */
6959 if (bed->elf_backend_always_size_sections
6960 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
0a1b45a2 6961 return false;
902e9fc7
MR
6962
6963 /* Determine any GNU_STACK segment requirements, after the backend
6964 has had a chance to set a default segment size. */
6965 if (info->execstack)
6966 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6967 else if (info->noexecstack)
6968 elf_stack_flags (output_bfd) = PF_R | PF_W;
6969 else
6970 {
6971 bfd *inputobj;
6972 asection *notesec = NULL;
6973 int exec = 0;
6974
6975 for (inputobj = info->input_bfds;
6976 inputobj;
6977 inputobj = inputobj->link.next)
6978 {
6979 asection *s;
6980
6981 if (inputobj->flags
6982 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6983 continue;
57963c05
AM
6984 s = inputobj->sections;
6985 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6986 continue;
6987
902e9fc7
MR
6988 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6989 if (s)
6990 {
6991 if (s->flags & SEC_CODE)
6992 exec = PF_X;
6993 notesec = s;
6994 }
6995 else if (bed->default_execstack)
6996 exec = PF_X;
6997 }
6998 if (notesec || info->stacksize > 0)
6999 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
7000 if (notesec && exec && bfd_link_relocatable (info)
7001 && notesec->output_section != bfd_abs_section_ptr)
7002 notesec->output_section->flags |= SEC_CODE;
7003 }
7004
7005 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7006 {
7007 struct elf_info_failed eif;
7008 struct elf_link_hash_entry *h;
7009 asection *dynstr;
7010 asection *s;
7011
7012 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
7013 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
7014
902e9fc7
MR
7015 if (info->symbolic)
7016 {
7017 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
0a1b45a2 7018 return false;
902e9fc7
MR
7019 info->flags |= DF_SYMBOLIC;
7020 }
7021
7022 if (rpath != NULL)
7023 {
7024 size_t indx;
7025 bfd_vma tag;
7026
7027 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
0a1b45a2 7028 true);
902e9fc7 7029 if (indx == (size_t) -1)
0a1b45a2 7030 return false;
902e9fc7
MR
7031
7032 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
7033 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
0a1b45a2 7034 return false;
902e9fc7
MR
7035 }
7036
7037 if (filter_shlib != NULL)
7038 {
7039 size_t indx;
7040
7041 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
0a1b45a2 7042 filter_shlib, true);
902e9fc7
MR
7043 if (indx == (size_t) -1
7044 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
0a1b45a2 7045 return false;
902e9fc7
MR
7046 }
7047
7048 if (auxiliary_filters != NULL)
7049 {
7050 const char * const *p;
7051
7052 for (p = auxiliary_filters; *p != NULL; p++)
7053 {
7054 size_t indx;
7055
7056 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
0a1b45a2 7057 *p, true);
902e9fc7
MR
7058 if (indx == (size_t) -1
7059 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
0a1b45a2 7060 return false;
902e9fc7
MR
7061 }
7062 }
7063
7064 if (audit != NULL)
7065 {
7066 size_t indx;
7067
7068 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
0a1b45a2 7069 true);
902e9fc7
MR
7070 if (indx == (size_t) -1
7071 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
0a1b45a2 7072 return false;
902e9fc7
MR
7073 }
7074
7075 if (depaudit != NULL)
7076 {
7077 size_t indx;
7078
7079 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
0a1b45a2 7080 true);
902e9fc7
MR
7081 if (indx == (size_t) -1
7082 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
0a1b45a2 7083 return false;
902e9fc7
MR
7084 }
7085
7086 eif.info = info;
0a1b45a2 7087 eif.failed = false;
902e9fc7
MR
7088
7089 /* Find all symbols which were defined in a dynamic object and make
7090 the backend pick a reasonable value for them. */
7091 elf_link_hash_traverse (elf_hash_table (info),
7092 _bfd_elf_adjust_dynamic_symbol,
7093 &eif);
7094 if (eif.failed)
0a1b45a2 7095 return false;
902e9fc7
MR
7096
7097 /* Add some entries to the .dynamic section. We fill in some of the
7098 values later, in bfd_elf_final_link, but we must add the entries
7099 now so that we know the final size of the .dynamic section. */
7100
7101 /* If there are initialization and/or finalization functions to
7102 call then add the corresponding DT_INIT/DT_FINI entries. */
7103 h = (info->init_function
7104 ? elf_link_hash_lookup (elf_hash_table (info),
0a1b45a2
AM
7105 info->init_function, false,
7106 false, false)
902e9fc7
MR
7107 : NULL);
7108 if (h != NULL
7109 && (h->ref_regular
7110 || h->def_regular))
7111 {
7112 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
0a1b45a2 7113 return false;
902e9fc7
MR
7114 }
7115 h = (info->fini_function
7116 ? elf_link_hash_lookup (elf_hash_table (info),
0a1b45a2
AM
7117 info->fini_function, false,
7118 false, false)
902e9fc7
MR
7119 : NULL);
7120 if (h != NULL
7121 && (h->ref_regular
7122 || h->def_regular))
7123 {
7124 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
0a1b45a2 7125 return false;
902e9fc7
MR
7126 }
7127
7128 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7129 if (s != NULL && s->linker_has_input)
7130 {
7131 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7132 if (! bfd_link_executable (info))
7133 {
7134 bfd *sub;
7135 asection *o;
7136
57963c05
AM
7137 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7138 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7139 && (o = sub->sections) != NULL
7140 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
902e9fc7
MR
7141 for (o = sub->sections; o != NULL; o = o->next)
7142 if (elf_section_data (o)->this_hdr.sh_type
7143 == SHT_PREINIT_ARRAY)
7144 {
7145 _bfd_error_handler
871b3ab2 7146 (_("%pB: .preinit_array section is not allowed in DSO"),
902e9fc7
MR
7147 sub);
7148 break;
7149 }
7150
7151 bfd_set_error (bfd_error_nonrepresentable_section);
0a1b45a2 7152 return false;
902e9fc7
MR
7153 }
7154
7155 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7156 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
0a1b45a2 7157 return false;
902e9fc7
MR
7158 }
7159 s = bfd_get_section_by_name (output_bfd, ".init_array");
7160 if (s != NULL && s->linker_has_input)
7161 {
7162 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7163 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
0a1b45a2 7164 return false;
902e9fc7
MR
7165 }
7166 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7167 if (s != NULL && s->linker_has_input)
7168 {
7169 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7170 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
0a1b45a2 7171 return false;
902e9fc7
MR
7172 }
7173
7174 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7175 /* If .dynstr is excluded from the link, we don't want any of
7176 these tags. Strictly, we should be checking each section
7177 individually; This quick check covers for the case where
7178 someone does a /DISCARD/ : { *(*) }. */
7179 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7180 {
7181 bfd_size_type strsize;
7182
7183 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7184 if ((info->emit_hash
7185 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7186 || (info->emit_gnu_hash
f16a9783
MS
7187 && (bed->record_xhash_symbol == NULL
7188 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
902e9fc7
MR
7189 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7190 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7191 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7192 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6a0a0dd0
VDM
7193 bed->s->sizeof_sym)
7194 || (info->gnu_flags_1
7195 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
7196 info->gnu_flags_1)))
0a1b45a2 7197 return false;
902e9fc7
MR
7198 }
7199 }
7200
7201 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
0a1b45a2 7202 return false;
902e9fc7
MR
7203
7204 /* The backend must work out the sizes of all the other dynamic
7205 sections. */
7206 if (dynobj != NULL
7207 && bed->elf_backend_size_dynamic_sections != NULL
7208 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
0a1b45a2 7209 return false;
902e9fc7
MR
7210
7211 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7212 {
902e9fc7
MR
7213 if (elf_tdata (output_bfd)->cverdefs)
7214 {
7215 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7216
7217 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7218 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
0a1b45a2 7219 return false;
902e9fc7
MR
7220 }
7221
7222 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7223 {
7224 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
0a1b45a2 7225 return false;
902e9fc7
MR
7226 }
7227 else if (info->flags & DF_BIND_NOW)
7228 {
7229 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
0a1b45a2 7230 return false;
902e9fc7
MR
7231 }
7232
7233 if (info->flags_1)
7234 {
7235 if (bfd_link_executable (info))
7236 info->flags_1 &= ~ (DF_1_INITFIRST
7237 | DF_1_NODELETE
7238 | DF_1_NOOPEN);
7239 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
0a1b45a2 7240 return false;
902e9fc7
MR
7241 }
7242
7243 if (elf_tdata (output_bfd)->cverrefs)
7244 {
7245 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7246
7247 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7248 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
0a1b45a2 7249 return false;
902e9fc7 7250 }
5a580b3a 7251
8423293d
AM
7252 if ((elf_tdata (output_bfd)->cverrefs == 0
7253 && elf_tdata (output_bfd)->cverdefs == 0)
63f452a8 7254 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
8423293d 7255 {
902e9fc7
MR
7256 asection *s;
7257
3d4d4302 7258 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
7259 s->flags |= SEC_EXCLUDE;
7260 }
7261 }
0a1b45a2 7262 return true;
8423293d
AM
7263}
7264
74541ad4
AM
7265/* Find the first non-excluded output section. We'll use its
7266 section symbol for some emitted relocs. */
7267void
7268_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7269{
7270 asection *s;
f26a3287 7271 asection *found = NULL;
74541ad4
AM
7272
7273 for (s = output_bfd->sections; s != NULL; s = s->next)
7274 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
d00dd7dc 7275 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7276 {
f26a3287
AM
7277 found = s;
7278 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7279 break;
74541ad4 7280 }
f26a3287 7281 elf_hash_table (info)->text_index_section = found;
74541ad4
AM
7282}
7283
7284/* Find two non-excluded output sections, one for code, one for data.
7285 We'll use their section symbols for some emitted relocs. */
7286void
7287_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7288{
7289 asection *s;
f26a3287 7290 asection *found = NULL;
74541ad4 7291
266b05cf 7292 /* Data first, since setting text_index_section changes
7f923b7f 7293 _bfd_elf_omit_section_dynsym_default. */
74541ad4 7294 for (s = output_bfd->sections; s != NULL; s = s->next)
f26a3287
AM
7295 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7296 && !(s->flags & SEC_READONLY)
d00dd7dc 7297 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7298 {
f26a3287
AM
7299 found = s;
7300 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7301 break;
74541ad4 7302 }
f26a3287 7303 elf_hash_table (info)->data_index_section = found;
74541ad4
AM
7304
7305 for (s = output_bfd->sections; s != NULL; s = s->next)
f26a3287
AM
7306 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7307 && (s->flags & SEC_READONLY)
d00dd7dc 7308 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7309 {
f26a3287 7310 found = s;
74541ad4
AM
7311 break;
7312 }
f26a3287 7313 elf_hash_table (info)->text_index_section = found;
74541ad4
AM
7314}
7315
f16a9783
MS
7316#define GNU_HASH_SECTION_NAME(bed) \
7317 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7318
0a1b45a2 7319bool
8423293d
AM
7320bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7321{
74541ad4 7322 const struct elf_backend_data *bed;
23ec1e32 7323 unsigned long section_sym_count;
96d01d93 7324 bfd_size_type dynsymcount = 0;
74541ad4 7325
8423293d 7326 if (!is_elf_hash_table (info->hash))
0a1b45a2 7327 return true;
8423293d 7328
74541ad4
AM
7329 bed = get_elf_backend_data (output_bfd);
7330 (*bed->elf_backend_init_index_section) (output_bfd, info);
7331
23ec1e32
MR
7332 /* Assign dynsym indices. In a shared library we generate a section
7333 symbol for each output section, which come first. Next come all
7334 of the back-end allocated local dynamic syms, followed by the rest
7335 of the global symbols.
7336
7337 This is usually not needed for static binaries, however backends
7338 can request to always do it, e.g. the MIPS backend uses dynamic
7339 symbol counts to lay out GOT, which will be produced in the
7340 presence of GOT relocations even in static binaries (holding fixed
7341 data in that case, to satisfy those relocations). */
7342
7343 if (elf_hash_table (info)->dynamic_sections_created
7344 || bed->always_renumber_dynsyms)
7345 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7346 &section_sym_count);
7347
8423293d
AM
7348 if (elf_hash_table (info)->dynamic_sections_created)
7349 {
7350 bfd *dynobj;
8423293d 7351 asection *s;
8423293d
AM
7352 unsigned int dtagcount;
7353
7354 dynobj = elf_hash_table (info)->dynobj;
7355
5a580b3a 7356 /* Work out the size of the symbol version section. */
3d4d4302 7357 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 7358 BFD_ASSERT (s != NULL);
d5486c43 7359 if ((s->flags & SEC_EXCLUDE) == 0)
5a580b3a 7360 {
eea6121a 7361 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 7362 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a 7363 if (s->contents == NULL)
0a1b45a2 7364 return false;
5a580b3a
AM
7365
7366 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
0a1b45a2 7367 return false;
5a580b3a
AM
7368 }
7369
7370 /* Set the size of the .dynsym and .hash sections. We counted
7371 the number of dynamic symbols in elf_link_add_object_symbols.
7372 We will build the contents of .dynsym and .hash when we build
7373 the final symbol table, because until then we do not know the
7374 correct value to give the symbols. We built the .dynstr
7375 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 7376 s = elf_hash_table (info)->dynsym;
5a580b3a 7377 BFD_ASSERT (s != NULL);
eea6121a 7378 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a 7379
d5486c43
L
7380 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7381 if (s->contents == NULL)
0a1b45a2 7382 return false;
5a580b3a 7383
d5486c43
L
7384 /* The first entry in .dynsym is a dummy symbol. Clear all the
7385 section syms, in case we don't output them all. */
7386 ++section_sym_count;
7387 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a 7388
fdc90cb4
JJ
7389 elf_hash_table (info)->bucketcount = 0;
7390
5a580b3a
AM
7391 /* Compute the size of the hashing table. As a side effect this
7392 computes the hash values for all the names we export. */
fdc90cb4
JJ
7393 if (info->emit_hash)
7394 {
7395 unsigned long int *hashcodes;
14b1c01e 7396 struct hash_codes_info hashinf;
fdc90cb4
JJ
7397 bfd_size_type amt;
7398 unsigned long int nsyms;
7399 size_t bucketcount;
7400 size_t hash_entry_size;
7401
7402 /* Compute the hash values for all exported symbols. At the same
7403 time store the values in an array so that we could use them for
7404 optimizations. */
7405 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 7406 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4 7407 if (hashcodes == NULL)
0a1b45a2 7408 return false;
14b1c01e 7409 hashinf.hashcodes = hashcodes;
0a1b45a2 7410 hashinf.error = false;
5a580b3a 7411
fdc90cb4
JJ
7412 /* Put all hash values in HASHCODES. */
7413 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
7414 elf_collect_hash_codes, &hashinf);
7415 if (hashinf.error)
4dd07732
AM
7416 {
7417 free (hashcodes);
0a1b45a2 7418 return false;
4dd07732 7419 }
5a580b3a 7420
14b1c01e 7421 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
7422 bucketcount
7423 = compute_bucket_count (info, hashcodes, nsyms, 0);
7424 free (hashcodes);
7425
4b48e2f6 7426 if (bucketcount == 0 && nsyms > 0)
0a1b45a2 7427 return false;
5a580b3a 7428
fdc90cb4
JJ
7429 elf_hash_table (info)->bucketcount = bucketcount;
7430
3d4d4302 7431 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
7432 BFD_ASSERT (s != NULL);
7433 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7434 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 7435 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4 7436 if (s->contents == NULL)
0a1b45a2 7437 return false;
fdc90cb4
JJ
7438
7439 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7440 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7441 s->contents + hash_entry_size);
7442 }
7443
7444 if (info->emit_gnu_hash)
7445 {
7446 size_t i, cnt;
7447 unsigned char *contents;
7448 struct collect_gnu_hash_codes cinfo;
7449 bfd_size_type amt;
7450 size_t bucketcount;
7451
7452 memset (&cinfo, 0, sizeof (cinfo));
7453
7454 /* Compute the hash values for all exported symbols. At the same
7455 time store the values in an array so that we could use them for
7456 optimizations. */
7457 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 7458 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4 7459 if (cinfo.hashcodes == NULL)
0a1b45a2 7460 return false;
fdc90cb4
JJ
7461
7462 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7463 cinfo.min_dynindx = -1;
7464 cinfo.output_bfd = output_bfd;
7465 cinfo.bed = bed;
7466
7467 /* Put all hash values in HASHCODES. */
7468 elf_link_hash_traverse (elf_hash_table (info),
7469 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 7470 if (cinfo.error)
4dd07732
AM
7471 {
7472 free (cinfo.hashcodes);
0a1b45a2 7473 return false;
4dd07732 7474 }
fdc90cb4
JJ
7475
7476 bucketcount
7477 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7478
7479 if (bucketcount == 0)
7480 {
7481 free (cinfo.hashcodes);
0a1b45a2 7482 return false;
fdc90cb4
JJ
7483 }
7484
f16a9783 7485 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
fdc90cb4
JJ
7486 BFD_ASSERT (s != NULL);
7487
7488 if (cinfo.nsyms == 0)
7489 {
f16a9783 7490 /* Empty .gnu.hash or .MIPS.xhash section is special. */
fdc90cb4
JJ
7491 BFD_ASSERT (cinfo.min_dynindx == -1);
7492 free (cinfo.hashcodes);
7493 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 7494 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4 7495 if (contents == NULL)
0a1b45a2 7496 return false;
fdc90cb4
JJ
7497 s->contents = contents;
7498 /* 1 empty bucket. */
7499 bfd_put_32 (output_bfd, 1, contents);
7500 /* SYMIDX above the special symbol 0. */
7501 bfd_put_32 (output_bfd, 1, contents + 4);
7502 /* Just one word for bitmask. */
7503 bfd_put_32 (output_bfd, 1, contents + 8);
7504 /* Only hash fn bloom filter. */
7505 bfd_put_32 (output_bfd, 0, contents + 12);
7506 /* No hashes are valid - empty bitmask. */
7507 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7508 /* No hashes in the only bucket. */
7509 bfd_put_32 (output_bfd, 0,
7510 contents + 16 + bed->s->arch_size / 8);
7511 }
7512 else
7513 {
9e6619e2 7514 unsigned long int maskwords, maskbitslog2, x;
0b33793d 7515 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 7516
9e6619e2
AM
7517 x = cinfo.nsyms;
7518 maskbitslog2 = 1;
7519 while ((x >>= 1) != 0)
7520 ++maskbitslog2;
fdc90cb4
JJ
7521 if (maskbitslog2 < 3)
7522 maskbitslog2 = 5;
7523 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7524 maskbitslog2 = maskbitslog2 + 3;
7525 else
7526 maskbitslog2 = maskbitslog2 + 2;
7527 if (bed->s->arch_size == 64)
7528 {
7529 if (maskbitslog2 == 5)
7530 maskbitslog2 = 6;
7531 cinfo.shift1 = 6;
7532 }
7533 else
7534 cinfo.shift1 = 5;
7535 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 7536 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
7537 cinfo.maskbits = 1 << maskbitslog2;
7538 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7539 amt = bucketcount * sizeof (unsigned long int) * 2;
7540 amt += maskwords * sizeof (bfd_vma);
a50b1753 7541 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
7542 if (cinfo.bitmask == NULL)
7543 {
7544 free (cinfo.hashcodes);
0a1b45a2 7545 return false;
fdc90cb4
JJ
7546 }
7547
a50b1753 7548 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
7549 cinfo.indx = cinfo.counts + bucketcount;
7550 cinfo.symindx = dynsymcount - cinfo.nsyms;
7551 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7552
7553 /* Determine how often each hash bucket is used. */
7554 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7555 for (i = 0; i < cinfo.nsyms; ++i)
7556 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7557
7558 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7559 if (cinfo.counts[i] != 0)
7560 {
7561 cinfo.indx[i] = cnt;
7562 cnt += cinfo.counts[i];
7563 }
7564 BFD_ASSERT (cnt == dynsymcount);
7565 cinfo.bucketcount = bucketcount;
7566 cinfo.local_indx = cinfo.min_dynindx;
7567
7568 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7569 s->size += cinfo.maskbits / 8;
f16a9783
MS
7570 if (bed->record_xhash_symbol != NULL)
7571 s->size += cinfo.nsyms * 4;
a50b1753 7572 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7573 if (contents == NULL)
7574 {
7575 free (cinfo.bitmask);
7576 free (cinfo.hashcodes);
0a1b45a2 7577 return false;
fdc90cb4
JJ
7578 }
7579
7580 s->contents = contents;
7581 bfd_put_32 (output_bfd, bucketcount, contents);
7582 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7583 bfd_put_32 (output_bfd, maskwords, contents + 8);
7584 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7585 contents += 16 + cinfo.maskbits / 8;
7586
7587 for (i = 0; i < bucketcount; ++i)
7588 {
7589 if (cinfo.counts[i] == 0)
7590 bfd_put_32 (output_bfd, 0, contents);
7591 else
7592 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7593 contents += 4;
7594 }
7595
7596 cinfo.contents = contents;
7597
f16a9783
MS
7598 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7599 /* Renumber dynamic symbols, if populating .gnu.hash section.
7600 If using .MIPS.xhash, populate the translation table. */
fdc90cb4 7601 elf_link_hash_traverse (elf_hash_table (info),
f16a9783 7602 elf_gnu_hash_process_symidx, &cinfo);
fdc90cb4
JJ
7603
7604 contents = s->contents + 16;
7605 for (i = 0; i < maskwords; ++i)
7606 {
7607 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7608 contents);
7609 contents += bed->s->arch_size / 8;
7610 }
7611
7612 free (cinfo.bitmask);
7613 free (cinfo.hashcodes);
7614 }
7615 }
5a580b3a 7616
3d4d4302 7617 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
7618 BFD_ASSERT (s != NULL);
7619
4ad4eba5 7620 elf_finalize_dynstr (output_bfd, info);
5a580b3a 7621
eea6121a 7622 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
7623
7624 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7625 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
0a1b45a2 7626 return false;
5a580b3a
AM
7627 }
7628
0a1b45a2 7629 return true;
5a580b3a 7630}
4d269e42 7631\f
4d269e42
AM
7632/* Make sure sec_info_type is cleared if sec_info is cleared too. */
7633
7634static void
7635merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7636 asection *sec)
7637{
dbaa2011
AM
7638 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7639 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
7640}
7641
7642/* Finish SHF_MERGE section merging. */
7643
0a1b45a2 7644bool
630993ec 7645_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
7646{
7647 bfd *ibfd;
7648 asection *sec;
7649
7650 if (!is_elf_hash_table (info->hash))
0a1b45a2 7651 return false;
4d269e42 7652
c72f2fb2 7653 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
7654 if ((ibfd->flags & DYNAMIC) == 0
7655 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
7656 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7657 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
7658 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7659 if ((sec->flags & SEC_MERGE) != 0
7660 && !bfd_is_abs_section (sec->output_section))
7661 {
7662 struct bfd_elf_section_data *secdata;
7663
7664 secdata = elf_section_data (sec);
630993ec 7665 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
7666 &elf_hash_table (info)->merge_info,
7667 sec, &secdata->sec_info))
0a1b45a2 7668 return false;
4d269e42 7669 else if (secdata->sec_info)
dbaa2011 7670 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
7671 }
7672
7673 if (elf_hash_table (info)->merge_info != NULL)
630993ec 7674 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42 7675 merge_sections_remove_hook);
0a1b45a2 7676 return true;
4d269e42
AM
7677}
7678
7679/* Create an entry in an ELF linker hash table. */
7680
7681struct bfd_hash_entry *
7682_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7683 struct bfd_hash_table *table,
7684 const char *string)
7685{
7686 /* Allocate the structure if it has not already been allocated by a
7687 subclass. */
7688 if (entry == NULL)
7689 {
a50b1753 7690 entry = (struct bfd_hash_entry *)
ca4be51c 7691 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
7692 if (entry == NULL)
7693 return entry;
7694 }
7695
7696 /* Call the allocation method of the superclass. */
7697 entry = _bfd_link_hash_newfunc (entry, table, string);
7698 if (entry != NULL)
7699 {
7700 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7701 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7702
7703 /* Set local fields. */
7704 ret->indx = -1;
7705 ret->dynindx = -1;
7706 ret->got = htab->init_got_refcount;
7707 ret->plt = htab->init_plt_refcount;
7708 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7709 - offsetof (struct elf_link_hash_entry, size)));
7710 /* Assume that we have been called by a non-ELF symbol reader.
7711 This flag is then reset by the code which reads an ELF input
7712 file. This ensures that a symbol created by a non-ELF symbol
7713 reader will have the flag set correctly. */
7714 ret->non_elf = 1;
7715 }
7716
7717 return entry;
7718}
7719
7720/* Copy data from an indirect symbol to its direct symbol, hiding the
7721 old indirect symbol. Also used for copying flags to a weakdef. */
7722
7723void
7724_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7725 struct elf_link_hash_entry *dir,
7726 struct elf_link_hash_entry *ind)
7727{
7728 struct elf_link_hash_table *htab;
7729
ad172eaa
L
7730 if (ind->dyn_relocs != NULL)
7731 {
7732 if (dir->dyn_relocs != NULL)
7733 {
7734 struct elf_dyn_relocs **pp;
7735 struct elf_dyn_relocs *p;
7736
7737 /* Add reloc counts against the indirect sym to the direct sym
7738 list. Merge any entries against the same section. */
7739 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
7740 {
7741 struct elf_dyn_relocs *q;
7742
7743 for (q = dir->dyn_relocs; q != NULL; q = q->next)
7744 if (q->sec == p->sec)
7745 {
7746 q->pc_count += p->pc_count;
7747 q->count += p->count;
7748 *pp = p->next;
7749 break;
7750 }
7751 if (q == NULL)
7752 pp = &p->next;
7753 }
7754 *pp = dir->dyn_relocs;
7755 }
7756
7757 dir->dyn_relocs = ind->dyn_relocs;
7758 ind->dyn_relocs = NULL;
7759 }
7760
4d269e42 7761 /* Copy down any references that we may have already seen to the
e81830c5 7762 symbol which just became indirect. */
4d269e42 7763
422f1182 7764 if (dir->versioned != versioned_hidden)
e81830c5
AM
7765 dir->ref_dynamic |= ind->ref_dynamic;
7766 dir->ref_regular |= ind->ref_regular;
7767 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7768 dir->non_got_ref |= ind->non_got_ref;
7769 dir->needs_plt |= ind->needs_plt;
7770 dir->pointer_equality_needed |= ind->pointer_equality_needed;
4d269e42
AM
7771
7772 if (ind->root.type != bfd_link_hash_indirect)
7773 return;
7774
7775 /* Copy over the global and procedure linkage table refcount entries.
7776 These may have been already set up by a check_relocs routine. */
7777 htab = elf_hash_table (info);
7778 if (ind->got.refcount > htab->init_got_refcount.refcount)
7779 {
7780 if (dir->got.refcount < 0)
7781 dir->got.refcount = 0;
7782 dir->got.refcount += ind->got.refcount;
7783 ind->got.refcount = htab->init_got_refcount.refcount;
7784 }
7785
7786 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7787 {
7788 if (dir->plt.refcount < 0)
7789 dir->plt.refcount = 0;
7790 dir->plt.refcount += ind->plt.refcount;
7791 ind->plt.refcount = htab->init_plt_refcount.refcount;
7792 }
7793
7794 if (ind->dynindx != -1)
7795 {
7796 if (dir->dynindx != -1)
7797 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7798 dir->dynindx = ind->dynindx;
7799 dir->dynstr_index = ind->dynstr_index;
7800 ind->dynindx = -1;
7801 ind->dynstr_index = 0;
7802 }
7803}
7804
7805void
7806_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7807 struct elf_link_hash_entry *h,
0a1b45a2 7808 bool force_local)
4d269e42 7809{
3aa14d16
L
7810 /* STT_GNU_IFUNC symbol must go through PLT. */
7811 if (h->type != STT_GNU_IFUNC)
7812 {
7813 h->plt = elf_hash_table (info)->init_plt_offset;
7814 h->needs_plt = 0;
7815 }
4d269e42
AM
7816 if (force_local)
7817 {
7818 h->forced_local = 1;
7819 if (h->dynindx != -1)
7820 {
4d269e42
AM
7821 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7822 h->dynstr_index);
641338d8
AM
7823 h->dynindx = -1;
7824 h->dynstr_index = 0;
4d269e42
AM
7825 }
7826 }
7827}
7828
34a87bb0
L
7829/* Hide a symbol. */
7830
7831void
7832_bfd_elf_link_hide_symbol (bfd *output_bfd,
7833 struct bfd_link_info *info,
7834 struct bfd_link_hash_entry *h)
7835{
7836 if (is_elf_hash_table (info->hash))
7837 {
7838 const struct elf_backend_data *bed
7839 = get_elf_backend_data (output_bfd);
7840 struct elf_link_hash_entry *eh
7841 = (struct elf_link_hash_entry *) h;
0a1b45a2 7842 bed->elf_backend_hide_symbol (info, eh, true);
34a87bb0
L
7843 eh->def_dynamic = 0;
7844 eh->ref_dynamic = 0;
7845 eh->dynamic_def = 0;
7846 }
7847}
7848
7bf52ea2
AM
7849/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7850 caller. */
4d269e42 7851
0a1b45a2 7852bool
4d269e42
AM
7853_bfd_elf_link_hash_table_init
7854 (struct elf_link_hash_table *table,
7855 bfd *abfd,
7856 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7857 struct bfd_hash_table *,
7858 const char *),
4dfe6ac6
NC
7859 unsigned int entsize,
7860 enum elf_target_id target_id)
4d269e42 7861{
0a1b45a2 7862 bool ret;
4d269e42
AM
7863 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7864
4d269e42
AM
7865 table->init_got_refcount.refcount = can_refcount - 1;
7866 table->init_plt_refcount.refcount = can_refcount - 1;
7867 table->init_got_offset.offset = -(bfd_vma) 1;
7868 table->init_plt_offset.offset = -(bfd_vma) 1;
7869 /* The first dynamic symbol is a dummy. */
7870 table->dynsymcount = 1;
7871
7872 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7873
4d269e42 7874 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7875 table->hash_table_id = target_id;
90c14f0c 7876 table->target_os = get_elf_backend_data (abfd)->target_os;
4d269e42
AM
7877
7878 return ret;
7879}
7880
7881/* Create an ELF linker hash table. */
7882
7883struct bfd_link_hash_table *
7884_bfd_elf_link_hash_table_create (bfd *abfd)
7885{
7886 struct elf_link_hash_table *ret;
986f0783 7887 size_t amt = sizeof (struct elf_link_hash_table);
4d269e42 7888
7bf52ea2 7889 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7890 if (ret == NULL)
7891 return NULL;
7892
7893 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7894 sizeof (struct elf_link_hash_entry),
7895 GENERIC_ELF_DATA))
4d269e42
AM
7896 {
7897 free (ret);
7898 return NULL;
7899 }
d495ab0d 7900 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7901
7902 return &ret->root;
7903}
7904
9f7c3e5e
AM
7905/* Destroy an ELF linker hash table. */
7906
7907void
d495ab0d 7908_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7909{
d495ab0d
AM
7910 struct elf_link_hash_table *htab;
7911
7912 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7913 if (htab->dynstr != NULL)
7914 _bfd_elf_strtab_free (htab->dynstr);
7915 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7916 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7917}
7918
4d269e42
AM
7919/* This is a hook for the ELF emulation code in the generic linker to
7920 tell the backend linker what file name to use for the DT_NEEDED
7921 entry for a dynamic object. */
7922
7923void
7924bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7925{
7926 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7927 && bfd_get_format (abfd) == bfd_object)
7928 elf_dt_name (abfd) = name;
7929}
7930
7931int
7932bfd_elf_get_dyn_lib_class (bfd *abfd)
7933{
7934 int lib_class;
7935 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7936 && bfd_get_format (abfd) == bfd_object)
7937 lib_class = elf_dyn_lib_class (abfd);
7938 else
7939 lib_class = 0;
7940 return lib_class;
7941}
7942
7943void
7944bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7945{
7946 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7947 && bfd_get_format (abfd) == bfd_object)
7948 elf_dyn_lib_class (abfd) = lib_class;
7949}
7950
7951/* Get the list of DT_NEEDED entries for a link. This is a hook for
7952 the linker ELF emulation code. */
7953
7954struct bfd_link_needed_list *
7955bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7956 struct bfd_link_info *info)
7957{
7958 if (! is_elf_hash_table (info->hash))
7959 return NULL;
7960 return elf_hash_table (info)->needed;
7961}
7962
7963/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7964 hook for the linker ELF emulation code. */
7965
7966struct bfd_link_needed_list *
7967bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7968 struct bfd_link_info *info)
7969{
7970 if (! is_elf_hash_table (info->hash))
7971 return NULL;
7972 return elf_hash_table (info)->runpath;
7973}
7974
7975/* Get the name actually used for a dynamic object for a link. This
7976 is the SONAME entry if there is one. Otherwise, it is the string
7977 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7978
7979const char *
7980bfd_elf_get_dt_soname (bfd *abfd)
7981{
7982 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7983 && bfd_get_format (abfd) == bfd_object)
7984 return elf_dt_name (abfd);
7985 return NULL;
7986}
7987
7988/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7989 the ELF linker emulation code. */
7990
0a1b45a2 7991bool
4d269e42
AM
7992bfd_elf_get_bfd_needed_list (bfd *abfd,
7993 struct bfd_link_needed_list **pneeded)
7994{
7995 asection *s;
7996 bfd_byte *dynbuf = NULL;
cb33740c 7997 unsigned int elfsec;
4d269e42
AM
7998 unsigned long shlink;
7999 bfd_byte *extdyn, *extdynend;
8000 size_t extdynsize;
8001 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
8002
8003 *pneeded = NULL;
8004
8005 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
8006 || bfd_get_format (abfd) != bfd_object)
0a1b45a2 8007 return true;
4d269e42
AM
8008
8009 s = bfd_get_section_by_name (abfd, ".dynamic");
8010 if (s == NULL || s->size == 0)
0a1b45a2 8011 return true;
4d269e42
AM
8012
8013 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
8014 goto error_return;
8015
8016 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 8017 if (elfsec == SHN_BAD)
4d269e42
AM
8018 goto error_return;
8019
8020 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 8021
4d269e42
AM
8022 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
8023 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
8024
8025 extdyn = dynbuf;
8026 extdynend = extdyn + s->size;
8027 for (; extdyn < extdynend; extdyn += extdynsize)
8028 {
8029 Elf_Internal_Dyn dyn;
8030
8031 (*swap_dyn_in) (abfd, extdyn, &dyn);
8032
8033 if (dyn.d_tag == DT_NULL)
8034 break;
8035
8036 if (dyn.d_tag == DT_NEEDED)
8037 {
8038 const char *string;
8039 struct bfd_link_needed_list *l;
8040 unsigned int tagv = dyn.d_un.d_val;
986f0783 8041 size_t amt;
4d269e42
AM
8042
8043 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8044 if (string == NULL)
8045 goto error_return;
8046
8047 amt = sizeof *l;
a50b1753 8048 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
8049 if (l == NULL)
8050 goto error_return;
8051
8052 l->by = abfd;
8053 l->name = string;
8054 l->next = *pneeded;
8055 *pneeded = l;
8056 }
8057 }
8058
8059 free (dynbuf);
8060
0a1b45a2 8061 return true;
4d269e42
AM
8062
8063 error_return:
c9594989 8064 free (dynbuf);
0a1b45a2 8065 return false;
4d269e42
AM
8066}
8067
8068struct elf_symbuf_symbol
8069{
8070 unsigned long st_name; /* Symbol name, index in string tbl */
8071 unsigned char st_info; /* Type and binding attributes */
8072 unsigned char st_other; /* Visibilty, and target specific */
8073};
8074
8075struct elf_symbuf_head
8076{
8077 struct elf_symbuf_symbol *ssym;
ef53be89 8078 size_t count;
4d269e42
AM
8079 unsigned int st_shndx;
8080};
8081
8082struct elf_symbol
8083{
8084 union
8085 {
8086 Elf_Internal_Sym *isym;
8087 struct elf_symbuf_symbol *ssym;
dcea6a95 8088 void *p;
4d269e42
AM
8089 } u;
8090 const char *name;
8091};
8092
8093/* Sort references to symbols by ascending section number. */
8094
8095static int
8096elf_sort_elf_symbol (const void *arg1, const void *arg2)
8097{
8098 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8099 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8100
dcea6a95
AM
8101 if (s1->st_shndx != s2->st_shndx)
8102 return s1->st_shndx > s2->st_shndx ? 1 : -1;
8103 /* Final sort by the address of the sym in the symbuf ensures
8104 a stable sort. */
8105 if (s1 != s2)
8106 return s1 > s2 ? 1 : -1;
8107 return 0;
4d269e42
AM
8108}
8109
8110static int
8111elf_sym_name_compare (const void *arg1, const void *arg2)
8112{
8113 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8114 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
dcea6a95
AM
8115 int ret = strcmp (s1->name, s2->name);
8116 if (ret != 0)
8117 return ret;
8118 if (s1->u.p != s2->u.p)
8119 return s1->u.p > s2->u.p ? 1 : -1;
8120 return 0;
4d269e42
AM
8121}
8122
8123static struct elf_symbuf_head *
ef53be89 8124elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
4d269e42 8125{
14b1c01e 8126 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
8127 struct elf_symbuf_symbol *ssym;
8128 struct elf_symbuf_head *ssymbuf, *ssymhead;
446f7ed5 8129 size_t i, shndx_count, total_size, amt;
4d269e42 8130
446f7ed5
AM
8131 amt = symcount * sizeof (*indbuf);
8132 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
4d269e42
AM
8133 if (indbuf == NULL)
8134 return NULL;
8135
8136 for (ind = indbuf, i = 0; i < symcount; i++)
3818d4ab 8137 if (isymbuf[i].st_shndx != SHN_UNDEF)
4d269e42
AM
8138 *ind++ = &isymbuf[i];
8139 indbufend = ind;
8140
8141 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8142 elf_sort_elf_symbol);
8143
8144 shndx_count = 0;
8145 if (indbufend > indbuf)
8146 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8147 if (ind[0]->st_shndx != ind[1]->st_shndx)
8148 shndx_count++;
8149
3ae181ee
L
8150 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8151 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 8152 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
8153 if (ssymbuf == NULL)
8154 {
8155 free (indbuf);
8156 return NULL;
8157 }
8158
3ae181ee 8159 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
8160 ssymbuf->ssym = NULL;
8161 ssymbuf->count = shndx_count;
8162 ssymbuf->st_shndx = 0;
8163 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8164 {
8165 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8166 {
8167 ssymhead++;
8168 ssymhead->ssym = ssym;
8169 ssymhead->count = 0;
8170 ssymhead->st_shndx = (*ind)->st_shndx;
8171 }
8172 ssym->st_name = (*ind)->st_name;
8173 ssym->st_info = (*ind)->st_info;
8174 ssym->st_other = (*ind)->st_other;
8175 ssymhead->count++;
8176 }
ef53be89 8177 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
3ae181ee
L
8178 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
8179 == total_size));
4d269e42
AM
8180
8181 free (indbuf);
8182 return ssymbuf;
8183}
8184
8185/* Check if 2 sections define the same set of local and global
8186 symbols. */
8187
0a1b45a2 8188static bool
4d269e42
AM
8189bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8190 struct bfd_link_info *info)
8191{
8192 bfd *bfd1, *bfd2;
8193 const struct elf_backend_data *bed1, *bed2;
8194 Elf_Internal_Shdr *hdr1, *hdr2;
ef53be89 8195 size_t symcount1, symcount2;
4d269e42
AM
8196 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8197 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8198 Elf_Internal_Sym *isym, *isymend;
8199 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
3818d4ab 8200 size_t count1, count2, sec_count1, sec_count2, i;
cb33740c 8201 unsigned int shndx1, shndx2;
0a1b45a2
AM
8202 bool result;
8203 bool ignore_section_symbol_p;
4d269e42
AM
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)
0a1b45a2 8211 return false;
4d269e42
AM
8212
8213 if (elf_section_type (sec1) != elf_section_type (sec2))
0a1b45a2 8214 return false;
4d269e42 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)
0a1b45a2 8219 return false;
4d269e42
AM
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)
0a1b45a2 8229 return false;
4d269e42 8230
0a1b45a2 8231 result = false;
4d269e42
AM
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 8236
3818d4ab
L
8237 /* Ignore section symbols only when matching non-debugging sections
8238 or linkonce section with comdat section. */
8239 ignore_section_symbol_p
8240 = ((sec1->flags & SEC_DEBUGGING) == 0
8241 || ((elf_section_flags (sec1) & SHF_GROUP)
8242 != (elf_section_flags (sec2) & SHF_GROUP)));
8243
4d269e42
AM
8244 if (ssymbuf1 == NULL)
8245 {
8246 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8247 NULL, NULL, NULL);
8248 if (isymbuf1 == NULL)
8249 goto done;
8250
67411cbf 8251 if (info != NULL && !info->reduce_memory_overheads)
dcea6a95
AM
8252 {
8253 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8254 elf_tdata (bfd1)->symbuf = ssymbuf1;
8255 }
4d269e42
AM
8256 }
8257
8258 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8259 {
8260 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8261 NULL, NULL, NULL);
8262 if (isymbuf2 == NULL)
8263 goto done;
8264
67411cbf 8265 if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
dcea6a95
AM
8266 {
8267 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8268 elf_tdata (bfd2)->symbuf = ssymbuf2;
8269 }
4d269e42
AM
8270 }
8271
8272 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8273 {
8274 /* Optimized faster version. */
ef53be89 8275 size_t lo, hi, mid;
4d269e42
AM
8276 struct elf_symbol *symp;
8277 struct elf_symbuf_symbol *ssym, *ssymend;
8278
8279 lo = 0;
8280 hi = ssymbuf1->count;
8281 ssymbuf1++;
8282 count1 = 0;
3818d4ab 8283 sec_count1 = 0;
4d269e42
AM
8284 while (lo < hi)
8285 {
8286 mid = (lo + hi) / 2;
cb33740c 8287 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 8288 hi = mid;
cb33740c 8289 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
8290 lo = mid + 1;
8291 else
8292 {
8293 count1 = ssymbuf1[mid].count;
8294 ssymbuf1 += mid;
8295 break;
8296 }
8297 }
3818d4ab
L
8298 if (ignore_section_symbol_p)
8299 {
8300 for (i = 0; i < count1; i++)
8301 if (ELF_ST_TYPE (ssymbuf1->ssym[i].st_info) == STT_SECTION)
8302 sec_count1++;
8303 count1 -= sec_count1;
8304 }
4d269e42
AM
8305
8306 lo = 0;
8307 hi = ssymbuf2->count;
8308 ssymbuf2++;
8309 count2 = 0;
3818d4ab 8310 sec_count2 = 0;
4d269e42
AM
8311 while (lo < hi)
8312 {
8313 mid = (lo + hi) / 2;
cb33740c 8314 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 8315 hi = mid;
cb33740c 8316 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
8317 lo = mid + 1;
8318 else
8319 {
8320 count2 = ssymbuf2[mid].count;
8321 ssymbuf2 += mid;
8322 break;
8323 }
8324 }
3818d4ab
L
8325 if (ignore_section_symbol_p)
8326 {
8327 for (i = 0; i < count2; i++)
8328 if (ELF_ST_TYPE (ssymbuf2->ssym[i].st_info) == STT_SECTION)
8329 sec_count2++;
8330 count2 -= sec_count2;
8331 }
4d269e42
AM
8332
8333 if (count1 == 0 || count2 == 0 || count1 != count2)
8334 goto done;
8335
ca4be51c
AM
8336 symtable1
8337 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8338 symtable2
8339 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
8340 if (symtable1 == NULL || symtable2 == NULL)
8341 goto done;
8342
8343 symp = symtable1;
3818d4ab
L
8344 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1 + sec_count1;
8345 ssym < ssymend; ssym++)
8346 if (sec_count1 == 0
8347 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8348 {
8349 symp->u.ssym = ssym;
8350 symp->name = bfd_elf_string_from_elf_section (bfd1,
8351 hdr1->sh_link,
8352 ssym->st_name);
8353 symp++;
8354 }
4d269e42
AM
8355
8356 symp = symtable2;
3818d4ab
L
8357 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2 + sec_count2;
8358 ssym < ssymend; ssym++)
8359 if (sec_count2 == 0
8360 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8361 {
8362 symp->u.ssym = ssym;
8363 symp->name = bfd_elf_string_from_elf_section (bfd2,
8364 hdr2->sh_link,
8365 ssym->st_name);
8366 symp++;
8367 }
4d269e42
AM
8368
8369 /* Sort symbol by name. */
8370 qsort (symtable1, count1, sizeof (struct elf_symbol),
8371 elf_sym_name_compare);
8372 qsort (symtable2, count1, sizeof (struct elf_symbol),
8373 elf_sym_name_compare);
8374
8375 for (i = 0; i < count1; i++)
8376 /* Two symbols must have the same binding, type and name. */
8377 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8378 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8379 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8380 goto done;
8381
0a1b45a2 8382 result = true;
4d269e42
AM
8383 goto done;
8384 }
8385
a50b1753
NC
8386 symtable1 = (struct elf_symbol *)
8387 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8388 symtable2 = (struct elf_symbol *)
8389 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
8390 if (symtable1 == NULL || symtable2 == NULL)
8391 goto done;
8392
8393 /* Count definitions in the section. */
8394 count1 = 0;
8395 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
3818d4ab
L
8396 if (isym->st_shndx == shndx1
8397 && (!ignore_section_symbol_p
8398 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
4d269e42
AM
8399 symtable1[count1++].u.isym = isym;
8400
8401 count2 = 0;
8402 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
3818d4ab
L
8403 if (isym->st_shndx == shndx2
8404 && (!ignore_section_symbol_p
8405 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
4d269e42
AM
8406 symtable2[count2++].u.isym = isym;
8407
8408 if (count1 == 0 || count2 == 0 || count1 != count2)
8409 goto done;
8410
8411 for (i = 0; i < count1; i++)
8412 symtable1[i].name
8413 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8414 symtable1[i].u.isym->st_name);
8415
8416 for (i = 0; i < count2; i++)
8417 symtable2[i].name
8418 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8419 symtable2[i].u.isym->st_name);
8420
8421 /* Sort symbol by name. */
8422 qsort (symtable1, count1, sizeof (struct elf_symbol),
8423 elf_sym_name_compare);
8424 qsort (symtable2, count1, sizeof (struct elf_symbol),
8425 elf_sym_name_compare);
8426
8427 for (i = 0; i < count1; i++)
8428 /* Two symbols must have the same binding, type and name. */
8429 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8430 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8431 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8432 goto done;
8433
0a1b45a2 8434 result = true;
4d269e42 8435
dc1e8a47 8436 done:
c9594989
AM
8437 free (symtable1);
8438 free (symtable2);
8439 free (isymbuf1);
8440 free (isymbuf2);
4d269e42
AM
8441
8442 return result;
8443}
8444
8445/* Return TRUE if 2 section types are compatible. */
8446
0a1b45a2 8447bool
4d269e42
AM
8448_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8449 bfd *bbfd, const asection *bsec)
8450{
8451 if (asec == NULL
8452 || bsec == NULL
8453 || abfd->xvec->flavour != bfd_target_elf_flavour
8454 || bbfd->xvec->flavour != bfd_target_elf_flavour)
0a1b45a2 8455 return true;
4d269e42
AM
8456
8457 return elf_section_type (asec) == elf_section_type (bsec);
8458}
8459\f
c152c796
AM
8460/* Final phase of ELF linker. */
8461
8462/* A structure we use to avoid passing large numbers of arguments. */
8463
8464struct elf_final_link_info
8465{
8466 /* General link information. */
8467 struct bfd_link_info *info;
8468 /* Output BFD. */
8469 bfd *output_bfd;
8470 /* Symbol string table. */
ef10c3ac 8471 struct elf_strtab_hash *symstrtab;
c152c796
AM
8472 /* .hash section. */
8473 asection *hash_sec;
8474 /* symbol version section (.gnu.version). */
8475 asection *symver_sec;
8476 /* Buffer large enough to hold contents of any section. */
8477 bfd_byte *contents;
8478 /* Buffer large enough to hold external relocs of any section. */
8479 void *external_relocs;
8480 /* Buffer large enough to hold internal relocs of any section. */
8481 Elf_Internal_Rela *internal_relocs;
8482 /* Buffer large enough to hold external local symbols of any input
8483 BFD. */
8484 bfd_byte *external_syms;
8485 /* And a buffer for symbol section indices. */
8486 Elf_External_Sym_Shndx *locsym_shndx;
8487 /* Buffer large enough to hold internal local symbols of any input
8488 BFD. */
8489 Elf_Internal_Sym *internal_syms;
8490 /* Array large enough to hold a symbol index for each local symbol
8491 of any input BFD. */
8492 long *indices;
8493 /* Array large enough to hold a section pointer for each local
8494 symbol of any input BFD. */
8495 asection **sections;
ef10c3ac 8496 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 8497 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
8498 /* Number of STT_FILE syms seen. */
8499 size_t filesym_count;
496afd17
L
8500 /* Local symbol hash table. */
8501 struct bfd_hash_table local_hash_table;
c152c796
AM
8502};
8503
496afd17
L
8504struct local_hash_entry
8505{
8506 /* Base hash table entry structure. */
8507 struct bfd_hash_entry root;
8508 /* Size of the local symbol name. */
8509 size_t size;
8510 /* Number of the duplicated local symbol names. */
8511 long count;
8512};
8513
8514/* Create an entry in the local symbol hash table. */
8515
8516static struct bfd_hash_entry *
8517local_hash_newfunc (struct bfd_hash_entry *entry,
8518 struct bfd_hash_table *table,
8519 const char *string)
8520{
8521
8522 /* Allocate the structure if it has not already been allocated by a
8523 subclass. */
8524 if (entry == NULL)
8525 {
8526 entry = bfd_hash_allocate (table,
8527 sizeof (struct local_hash_entry));
8528 if (entry == NULL)
8529 return entry;
8530 }
8531
8532 /* Call the allocation method of the superclass. */
8533 entry = bfd_hash_newfunc (entry, table, string);
8534 if (entry != NULL)
8535 {
8536 ((struct local_hash_entry *) entry)->count = 0;
8537 ((struct local_hash_entry *) entry)->size = 0;
8538 }
8539
8540 return entry;
8541}
8542
c152c796
AM
8543/* This struct is used to pass information to elf_link_output_extsym. */
8544
8545struct elf_outext_info
8546{
0a1b45a2
AM
8547 bool failed;
8548 bool localsyms;
8549 bool file_sym_done;
8b127cbc 8550 struct elf_final_link_info *flinfo;
c152c796
AM
8551};
8552
d9352518
DB
8553
8554/* Support for evaluating a complex relocation.
8555
8556 Complex relocations are generalized, self-describing relocations. The
8557 implementation of them consists of two parts: complex symbols, and the
a0c8462f 8558 relocations themselves.
d9352518 8559
4b69ce9b 8560 The relocations use a reserved elf-wide relocation type code (R_RELC
d9352518
DB
8561 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8562 information (start bit, end bit, word width, etc) into the addend. This
8563 information is extracted from CGEN-generated operand tables within gas.
8564
4b69ce9b 8565 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
d9352518
DB
8566 internal) representing prefix-notation expressions, including but not
8567 limited to those sorts of expressions normally encoded as addends in the
8568 addend field. The symbol mangling format is:
8569
8570 <node> := <literal>
07d6d2b8
AM
8571 | <unary-operator> ':' <node>
8572 | <binary-operator> ':' <node> ':' <node>
d9352518
DB
8573 ;
8574
8575 <literal> := 's' <digits=N> ':' <N character symbol name>
07d6d2b8 8576 | 'S' <digits=N> ':' <N character section name>
d9352518
DB
8577 | '#' <hexdigits>
8578 ;
8579
8580 <binary-operator> := as in C
8581 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8582
8583static void
a0c8462f
AM
8584set_symbol_value (bfd *bfd_with_globals,
8585 Elf_Internal_Sym *isymbuf,
8586 size_t locsymcount,
8587 size_t symidx,
8588 bfd_vma val)
d9352518 8589{
8977835c
AM
8590 struct elf_link_hash_entry **sym_hashes;
8591 struct elf_link_hash_entry *h;
8592 size_t extsymoff = locsymcount;
d9352518 8593
8977835c 8594 if (symidx < locsymcount)
d9352518 8595 {
8977835c
AM
8596 Elf_Internal_Sym *sym;
8597
8598 sym = isymbuf + symidx;
8599 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8600 {
8601 /* It is a local symbol: move it to the
8602 "absolute" section and give it a value. */
8603 sym->st_shndx = SHN_ABS;
8604 sym->st_value = val;
8605 return;
8606 }
8607 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8608 extsymoff = 0;
d9352518 8609 }
8977835c
AM
8610
8611 /* It is a global symbol: set its link type
8612 to "defined" and give it a value. */
8613
8614 sym_hashes = elf_sym_hashes (bfd_with_globals);
8615 h = sym_hashes [symidx - extsymoff];
8616 while (h->root.type == bfd_link_hash_indirect
8617 || h->root.type == bfd_link_hash_warning)
8618 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8619 h->root.type = bfd_link_hash_defined;
8620 h->root.u.def.value = val;
8621 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
8622}
8623
0a1b45a2 8624static bool
a0c8462f
AM
8625resolve_symbol (const char *name,
8626 bfd *input_bfd,
8b127cbc 8627 struct elf_final_link_info *flinfo,
a0c8462f
AM
8628 bfd_vma *result,
8629 Elf_Internal_Sym *isymbuf,
8630 size_t locsymcount)
d9352518 8631{
a0c8462f
AM
8632 Elf_Internal_Sym *sym;
8633 struct bfd_link_hash_entry *global_entry;
8634 const char *candidate = NULL;
8635 Elf_Internal_Shdr *symtab_hdr;
8636 size_t i;
8637
d9352518
DB
8638 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8639
8640 for (i = 0; i < locsymcount; ++ i)
8641 {
8977835c 8642 sym = isymbuf + i;
d9352518
DB
8643
8644 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8645 continue;
8646
8647 candidate = bfd_elf_string_from_elf_section (input_bfd,
8648 symtab_hdr->sh_link,
8649 sym->st_name);
8650#ifdef DEBUG
0f02bbd9
AM
8651 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8652 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
8653#endif
8654 if (candidate && strcmp (candidate, name) == 0)
8655 {
8b127cbc 8656 asection *sec = flinfo->sections [i];
d9352518 8657
0f02bbd9
AM
8658 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8659 *result += sec->output_offset + sec->output_section->vma;
d9352518 8660#ifdef DEBUG
0f02bbd9
AM
8661 printf ("Found symbol with value %8.8lx\n",
8662 (unsigned long) *result);
d9352518 8663#endif
0a1b45a2 8664 return true;
d9352518
DB
8665 }
8666 }
8667
8668 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 8669 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
0a1b45a2 8670 false, false, true);
d9352518 8671 if (!global_entry)
0a1b45a2 8672 return false;
a0c8462f 8673
d9352518
DB
8674 if (global_entry->type == bfd_link_hash_defined
8675 || global_entry->type == bfd_link_hash_defweak)
8676 {
a0c8462f
AM
8677 *result = (global_entry->u.def.value
8678 + global_entry->u.def.section->output_section->vma
8679 + global_entry->u.def.section->output_offset);
d9352518 8680#ifdef DEBUG
0f02bbd9
AM
8681 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8682 global_entry->root.string, (unsigned long) *result);
d9352518 8683#endif
0a1b45a2 8684 return true;
a0c8462f 8685 }
d9352518 8686
0a1b45a2 8687 return false;
d9352518
DB
8688}
8689
37b01f6a
DG
8690/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8691 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8692 names like "foo.end" which is the end address of section "foo". */
07d6d2b8 8693
0a1b45a2 8694static bool
a0c8462f
AM
8695resolve_section (const char *name,
8696 asection *sections,
37b01f6a
DG
8697 bfd_vma *result,
8698 bfd * abfd)
d9352518 8699{
a0c8462f
AM
8700 asection *curr;
8701 unsigned int len;
d9352518 8702
a0c8462f 8703 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8704 if (strcmp (curr->name, name) == 0)
8705 {
8706 *result = curr->vma;
0a1b45a2 8707 return true;
d9352518
DB
8708 }
8709
8710 /* Hmm. still haven't found it. try pseudo-section names. */
37b01f6a 8711 /* FIXME: This could be coded more efficiently... */
a0c8462f 8712 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8713 {
8714 len = strlen (curr->name);
a0c8462f 8715 if (len > strlen (name))
d9352518
DB
8716 continue;
8717
8718 if (strncmp (curr->name, name, len) == 0)
8719 {
3f3328b8 8720 if (startswith (name + len, ".end"))
d9352518 8721 {
61826503 8722 *result = (curr->vma
bb294208 8723 + curr->size / bfd_octets_per_byte (abfd, curr));
0a1b45a2 8724 return true;
d9352518
DB
8725 }
8726
8727 /* Insert more pseudo-section names here, if you like. */
8728 }
8729 }
a0c8462f 8730
0a1b45a2 8731 return false;
d9352518
DB
8732}
8733
8734static void
a0c8462f 8735undefined_reference (const char *reftype, const char *name)
d9352518 8736{
695344c0 8737 /* xgettext:c-format */
a0c8462f
AM
8738 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8739 reftype, name);
4b69ce9b 8740 bfd_set_error (bfd_error_bad_value);
d9352518
DB
8741}
8742
0a1b45a2 8743static bool
a0c8462f
AM
8744eval_symbol (bfd_vma *result,
8745 const char **symp,
8746 bfd *input_bfd,
8b127cbc 8747 struct elf_final_link_info *flinfo,
a0c8462f
AM
8748 bfd_vma dot,
8749 Elf_Internal_Sym *isymbuf,
8750 size_t locsymcount,
8751 int signed_p)
d9352518 8752{
4b93929b
NC
8753 size_t len;
8754 size_t symlen;
a0c8462f
AM
8755 bfd_vma a;
8756 bfd_vma b;
4b93929b 8757 char symbuf[4096];
0f02bbd9 8758 const char *sym = *symp;
a0c8462f 8759 const char *symend;
0a1b45a2 8760 bool symbol_is_section = false;
d9352518
DB
8761
8762 len = strlen (sym);
8763 symend = sym + len;
8764
4b93929b 8765 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
8766 {
8767 bfd_set_error (bfd_error_invalid_operation);
0a1b45a2 8768 return false;
d9352518 8769 }
a0c8462f 8770
d9352518
DB
8771 switch (* sym)
8772 {
8773 case '.':
0f02bbd9
AM
8774 *result = dot;
8775 *symp = sym + 1;
0a1b45a2 8776 return true;
d9352518
DB
8777
8778 case '#':
0f02bbd9
AM
8779 ++sym;
8780 *result = strtoul (sym, (char **) symp, 16);
0a1b45a2 8781 return true;
d9352518
DB
8782
8783 case 'S':
0a1b45a2 8784 symbol_is_section = true;
1a0670f3 8785 /* Fall through. */
a0c8462f 8786 case 's':
0f02bbd9
AM
8787 ++sym;
8788 symlen = strtol (sym, (char **) symp, 10);
8789 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 8790
4b93929b 8791 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
8792 {
8793 bfd_set_error (bfd_error_invalid_operation);
0a1b45a2 8794 return false;
d9352518
DB
8795 }
8796
8797 memcpy (symbuf, sym, symlen);
a0c8462f 8798 symbuf[symlen] = '\0';
0f02bbd9 8799 *symp = sym + symlen;
a0c8462f
AM
8800
8801 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
8802 the symbol as a section, or vice-versa. so we're pretty liberal in our
8803 interpretation here; section means "try section first", not "must be a
8804 section", and likewise with symbol. */
8805
a0c8462f 8806 if (symbol_is_section)
d9352518 8807 {
37b01f6a 8808 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8b127cbc 8809 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8810 isymbuf, locsymcount))
d9352518
DB
8811 {
8812 undefined_reference ("section", symbuf);
0a1b45a2 8813 return false;
d9352518 8814 }
a0c8462f
AM
8815 }
8816 else
d9352518 8817 {
8b127cbc 8818 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8819 isymbuf, locsymcount)
8b127cbc 8820 && !resolve_section (symbuf, flinfo->output_bfd->sections,
37b01f6a 8821 result, input_bfd))
d9352518
DB
8822 {
8823 undefined_reference ("symbol", symbuf);
0a1b45a2 8824 return false;
d9352518
DB
8825 }
8826 }
8827
0a1b45a2 8828 return true;
a0c8462f 8829
d9352518
DB
8830 /* All that remains are operators. */
8831
8832#define UNARY_OP(op) \
3f3328b8 8833 if (startswith (sym, #op)) \
d9352518
DB
8834 { \
8835 sym += strlen (#op); \
a0c8462f
AM
8836 if (*sym == ':') \
8837 ++sym; \
0f02bbd9 8838 *symp = sym; \
8b127cbc 8839 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8840 isymbuf, locsymcount, signed_p)) \
0a1b45a2 8841 return false; \
a0c8462f 8842 if (signed_p) \
0f02bbd9 8843 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
8844 else \
8845 *result = op a; \
0a1b45a2 8846 return true; \
d9352518
DB
8847 }
8848
4b69ce9b 8849#define BINARY_OP_HEAD(op) \
3f3328b8 8850 if (startswith (sym, #op)) \
d9352518
DB
8851 { \
8852 sym += strlen (#op); \
a0c8462f
AM
8853 if (*sym == ':') \
8854 ++sym; \
0f02bbd9 8855 *symp = sym; \
8b127cbc 8856 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8857 isymbuf, locsymcount, signed_p)) \
0a1b45a2 8858 return false; \
0f02bbd9 8859 ++*symp; \
8b127cbc 8860 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 8861 isymbuf, locsymcount, signed_p)) \
0a1b45a2 8862 return false;
4b69ce9b 8863#define BINARY_OP_TAIL(op) \
a0c8462f 8864 if (signed_p) \
0f02bbd9 8865 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
8866 else \
8867 *result = a op b; \
0a1b45a2 8868 return true; \
d9352518 8869 }
4b69ce9b 8870#define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
d9352518
DB
8871
8872 default:
8873 UNARY_OP (0-);
4b69ce9b
AM
8874 BINARY_OP_HEAD (<<);
8875 if (b >= sizeof (a) * CHAR_BIT)
8876 {
8877 *result = 0;
0a1b45a2 8878 return true;
4b69ce9b
AM
8879 }
8880 signed_p = 0;
8881 BINARY_OP_TAIL (<<);
8882 BINARY_OP_HEAD (>>);
8883 if (b >= sizeof (a) * CHAR_BIT)
8884 {
8885 *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
0a1b45a2 8886 return true;
4b69ce9b
AM
8887 }
8888 BINARY_OP_TAIL (>>);
d9352518
DB
8889 BINARY_OP (==);
8890 BINARY_OP (!=);
8891 BINARY_OP (<=);
8892 BINARY_OP (>=);
8893 BINARY_OP (&&);
8894 BINARY_OP (||);
8895 UNARY_OP (~);
8896 UNARY_OP (!);
8897 BINARY_OP (*);
4b69ce9b
AM
8898 BINARY_OP_HEAD (/);
8899 if (b == 0)
8900 {
8901 _bfd_error_handler (_("division by zero"));
8902 bfd_set_error (bfd_error_bad_value);
0a1b45a2 8903 return false;
4b69ce9b
AM
8904 }
8905 BINARY_OP_TAIL (/);
8906 BINARY_OP_HEAD (%);
8907 if (b == 0)
8908 {
8909 _bfd_error_handler (_("division by zero"));
8910 bfd_set_error (bfd_error_bad_value);
0a1b45a2 8911 return false;
4b69ce9b
AM
8912 }
8913 BINARY_OP_TAIL (%);
d9352518
DB
8914 BINARY_OP (^);
8915 BINARY_OP (|);
8916 BINARY_OP (&);
8917 BINARY_OP (+);
8918 BINARY_OP (-);
8919 BINARY_OP (<);
8920 BINARY_OP (>);
8921#undef UNARY_OP
8922#undef BINARY_OP
8923 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8924 bfd_set_error (bfd_error_invalid_operation);
0a1b45a2 8925 return false;
d9352518
DB
8926 }
8927}
8928
d9352518 8929static void
a0c8462f
AM
8930put_value (bfd_vma size,
8931 unsigned long chunksz,
8932 bfd *input_bfd,
8933 bfd_vma x,
8934 bfd_byte *location)
d9352518
DB
8935{
8936 location += (size - chunksz);
8937
41cd1ad1 8938 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
8939 {
8940 switch (chunksz)
8941 {
d9352518
DB
8942 case 1:
8943 bfd_put_8 (input_bfd, x, location);
41cd1ad1 8944 x >>= 8;
d9352518
DB
8945 break;
8946 case 2:
8947 bfd_put_16 (input_bfd, x, location);
41cd1ad1 8948 x >>= 16;
d9352518
DB
8949 break;
8950 case 4:
8951 bfd_put_32 (input_bfd, x, location);
65164438
NC
8952 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8953 x >>= 16;
8954 x >>= 16;
d9352518 8955 break;
d9352518 8956#ifdef BFD64
41cd1ad1 8957 case 8:
d9352518 8958 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
8959 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8960 x >>= 32;
8961 x >>= 32;
8962 break;
d9352518 8963#endif
41cd1ad1
NC
8964 default:
8965 abort ();
d9352518
DB
8966 break;
8967 }
8968 }
8969}
8970
a0c8462f
AM
8971static bfd_vma
8972get_value (bfd_vma size,
8973 unsigned long chunksz,
8974 bfd *input_bfd,
8975 bfd_byte *location)
d9352518 8976{
9b239e0e 8977 int shift;
d9352518
DB
8978 bfd_vma x = 0;
8979
9b239e0e
NC
8980 /* Sanity checks. */
8981 BFD_ASSERT (chunksz <= sizeof (x)
8982 && size >= chunksz
8983 && chunksz != 0
8984 && (size % chunksz) == 0
8985 && input_bfd != NULL
8986 && location != NULL);
8987
8988 if (chunksz == sizeof (x))
8989 {
8990 BFD_ASSERT (size == chunksz);
8991
8992 /* Make sure that we do not perform an undefined shift operation.
8993 We know that size == chunksz so there will only be one iteration
8994 of the loop below. */
8995 shift = 0;
8996 }
8997 else
8998 shift = 8 * chunksz;
8999
a0c8462f 9000 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
9001 {
9002 switch (chunksz)
9003 {
d9352518 9004 case 1:
9b239e0e 9005 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
9006 break;
9007 case 2:
9b239e0e 9008 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
9009 break;
9010 case 4:
9b239e0e 9011 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 9012 break;
d9352518 9013#ifdef BFD64
9b239e0e
NC
9014 case 8:
9015 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 9016 break;
9b239e0e
NC
9017#endif
9018 default:
9019 abort ();
d9352518
DB
9020 }
9021 }
9022 return x;
9023}
9024
a0c8462f
AM
9025static void
9026decode_complex_addend (unsigned long *start, /* in bits */
9027 unsigned long *oplen, /* in bits */
9028 unsigned long *len, /* in bits */
9029 unsigned long *wordsz, /* in bytes */
9030 unsigned long *chunksz, /* in bytes */
9031 unsigned long *lsb0_p,
9032 unsigned long *signed_p,
9033 unsigned long *trunc_p,
9034 unsigned long encoded)
d9352518 9035{
07d6d2b8
AM
9036 * start = encoded & 0x3F;
9037 * len = (encoded >> 6) & 0x3F;
d9352518
DB
9038 * oplen = (encoded >> 12) & 0x3F;
9039 * wordsz = (encoded >> 18) & 0xF;
9040 * chunksz = (encoded >> 22) & 0xF;
9041 * lsb0_p = (encoded >> 27) & 1;
9042 * signed_p = (encoded >> 28) & 1;
9043 * trunc_p = (encoded >> 29) & 1;
9044}
9045
cdfeee4f 9046bfd_reloc_status_type
0f02bbd9 9047bfd_elf_perform_complex_relocation (bfd *input_bfd,
bb294208 9048 asection *input_section,
0f02bbd9
AM
9049 bfd_byte *contents,
9050 Elf_Internal_Rela *rel,
9051 bfd_vma relocation)
d9352518 9052{
0f02bbd9
AM
9053 bfd_vma shift, x, mask;
9054 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 9055 bfd_reloc_status_type r;
bb294208 9056 bfd_size_type octets;
d9352518
DB
9057
9058 /* Perform this reloc, since it is complex.
9059 (this is not to say that it necessarily refers to a complex
9060 symbol; merely that it is a self-describing CGEN based reloc.
9061 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 9062 word size, etc) encoded within it.). */
d9352518 9063
a0c8462f
AM
9064 decode_complex_addend (&start, &oplen, &len, &wordsz,
9065 &chunksz, &lsb0_p, &signed_p,
9066 &trunc_p, rel->r_addend);
d9352518
DB
9067
9068 mask = (((1L << (len - 1)) - 1) << 1) | 1;
9069
9070 if (lsb0_p)
9071 shift = (start + 1) - len;
9072 else
9073 shift = (8 * wordsz) - (start + len);
9074
bb294208
AM
9075 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
9076 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
d9352518
DB
9077
9078#ifdef DEBUG
9079 printf ("Doing complex reloc: "
9080 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9081 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9082 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9083 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
9084 oplen, (unsigned long) x, (unsigned long) mask,
9085 (unsigned long) relocation);
d9352518
DB
9086#endif
9087
cdfeee4f 9088 r = bfd_reloc_ok;
d9352518 9089 if (! trunc_p)
cdfeee4f
AM
9090 /* Now do an overflow check. */
9091 r = bfd_check_overflow ((signed_p
9092 ? complain_overflow_signed
9093 : complain_overflow_unsigned),
9094 len, 0, (8 * wordsz),
9095 relocation);
a0c8462f 9096
d9352518
DB
9097 /* Do the deed. */
9098 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9099
9100#ifdef DEBUG
9101 printf (" relocation: %8.8lx\n"
9102 " shifted mask: %8.8lx\n"
9103 " shifted/masked reloc: %8.8lx\n"
9104 " result: %8.8lx\n",
9ccb8af9
AM
9105 (unsigned long) relocation, (unsigned long) (mask << shift),
9106 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 9107#endif
bb294208 9108 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
cdfeee4f 9109 return r;
d9352518
DB
9110}
9111
0e287786
AM
9112/* Functions to read r_offset from external (target order) reloc
9113 entry. Faster than bfd_getl32 et al, because we let the compiler
9114 know the value is aligned. */
53df40a4 9115
0e287786
AM
9116static bfd_vma
9117ext32l_r_offset (const void *p)
53df40a4
AM
9118{
9119 union aligned32
9120 {
9121 uint32_t v;
9122 unsigned char c[4];
9123 };
9124 const union aligned32 *a
0e287786 9125 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
9126
9127 uint32_t aval = ( (uint32_t) a->c[0]
9128 | (uint32_t) a->c[1] << 8
9129 | (uint32_t) a->c[2] << 16
9130 | (uint32_t) a->c[3] << 24);
0e287786 9131 return aval;
53df40a4
AM
9132}
9133
0e287786
AM
9134static bfd_vma
9135ext32b_r_offset (const void *p)
53df40a4
AM
9136{
9137 union aligned32
9138 {
9139 uint32_t v;
9140 unsigned char c[4];
9141 };
9142 const union aligned32 *a
0e287786 9143 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
9144
9145 uint32_t aval = ( (uint32_t) a->c[0] << 24
9146 | (uint32_t) a->c[1] << 16
9147 | (uint32_t) a->c[2] << 8
9148 | (uint32_t) a->c[3]);
0e287786 9149 return aval;
53df40a4
AM
9150}
9151
9152#ifdef BFD_HOST_64_BIT
0e287786
AM
9153static bfd_vma
9154ext64l_r_offset (const void *p)
53df40a4
AM
9155{
9156 union aligned64
9157 {
9158 uint64_t v;
9159 unsigned char c[8];
9160 };
9161 const union aligned64 *a
0e287786 9162 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
9163
9164 uint64_t aval = ( (uint64_t) a->c[0]
9165 | (uint64_t) a->c[1] << 8
9166 | (uint64_t) a->c[2] << 16
9167 | (uint64_t) a->c[3] << 24
9168 | (uint64_t) a->c[4] << 32
9169 | (uint64_t) a->c[5] << 40
9170 | (uint64_t) a->c[6] << 48
9171 | (uint64_t) a->c[7] << 56);
0e287786 9172 return aval;
53df40a4
AM
9173}
9174
0e287786
AM
9175static bfd_vma
9176ext64b_r_offset (const void *p)
53df40a4
AM
9177{
9178 union aligned64
9179 {
9180 uint64_t v;
9181 unsigned char c[8];
9182 };
9183 const union aligned64 *a
0e287786 9184 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
9185
9186 uint64_t aval = ( (uint64_t) a->c[0] << 56
9187 | (uint64_t) a->c[1] << 48
9188 | (uint64_t) a->c[2] << 40
9189 | (uint64_t) a->c[3] << 32
9190 | (uint64_t) a->c[4] << 24
9191 | (uint64_t) a->c[5] << 16
9192 | (uint64_t) a->c[6] << 8
9193 | (uint64_t) a->c[7]);
0e287786 9194 return aval;
53df40a4
AM
9195}
9196#endif
9197
c152c796
AM
9198/* When performing a relocatable link, the input relocations are
9199 preserved. But, if they reference global symbols, the indices
d4730f92
BS
9200 referenced must be updated. Update all the relocations found in
9201 RELDATA. */
c152c796 9202
0a1b45a2 9203static bool
c152c796 9204elf_link_adjust_relocs (bfd *abfd,
9eaff861 9205 asection *sec,
28dbcedc 9206 struct bfd_elf_section_reloc_data *reldata,
0a1b45a2 9207 bool sort,
10bbbc1d 9208 struct bfd_link_info *info)
c152c796
AM
9209{
9210 unsigned int i;
9211 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9212 bfd_byte *erela;
9213 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9214 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9215 bfd_vma r_type_mask;
9216 int r_sym_shift;
d4730f92
BS
9217 unsigned int count = reldata->count;
9218 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 9219
d4730f92 9220 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
9221 {
9222 swap_in = bed->s->swap_reloc_in;
9223 swap_out = bed->s->swap_reloc_out;
9224 }
d4730f92 9225 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
9226 {
9227 swap_in = bed->s->swap_reloca_in;
9228 swap_out = bed->s->swap_reloca_out;
9229 }
9230 else
9231 abort ();
9232
9233 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9234 abort ();
9235
9236 if (bed->s->arch_size == 32)
9237 {
9238 r_type_mask = 0xff;
9239 r_sym_shift = 8;
9240 }
9241 else
9242 {
9243 r_type_mask = 0xffffffff;
9244 r_sym_shift = 32;
9245 }
9246
d4730f92
BS
9247 erela = reldata->hdr->contents;
9248 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
9249 {
9250 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9251 unsigned int j;
9252
9253 if (*rel_hash == NULL)
9254 continue;
9255
10bbbc1d
NC
9256 if ((*rel_hash)->indx == -2
9257 && info->gc_sections
9258 && ! info->gc_keep_exported)
9259 {
9260 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9793eb77 9261 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
10bbbc1d
NC
9262 abfd, sec,
9263 (*rel_hash)->root.root.string);
9793eb77 9264 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
d42c267e 9265 abfd, sec);
10bbbc1d 9266 bfd_set_error (bfd_error_invalid_operation);
0a1b45a2 9267 return false;
10bbbc1d 9268 }
c152c796
AM
9269 BFD_ASSERT ((*rel_hash)->indx >= 0);
9270
9271 (*swap_in) (abfd, erela, irela);
9272 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9273 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9274 | (irela[j].r_info & r_type_mask));
9275 (*swap_out) (abfd, irela, erela);
9276 }
53df40a4 9277
9eaff861
AO
9278 if (bed->elf_backend_update_relocs)
9279 (*bed->elf_backend_update_relocs) (sec, reldata);
9280
0e287786 9281 if (sort && count != 0)
53df40a4 9282 {
0e287786
AM
9283 bfd_vma (*ext_r_off) (const void *);
9284 bfd_vma r_off;
9285 size_t elt_size;
9286 bfd_byte *base, *end, *p, *loc;
bca6d0e3 9287 bfd_byte *buf = NULL;
28dbcedc
AM
9288
9289 if (bed->s->arch_size == 32)
9290 {
9291 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 9292 ext_r_off = ext32l_r_offset;
28dbcedc 9293 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 9294 ext_r_off = ext32b_r_offset;
28dbcedc
AM
9295 else
9296 abort ();
9297 }
53df40a4 9298 else
28dbcedc 9299 {
53df40a4 9300#ifdef BFD_HOST_64_BIT
28dbcedc 9301 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 9302 ext_r_off = ext64l_r_offset;
28dbcedc 9303 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 9304 ext_r_off = ext64b_r_offset;
28dbcedc 9305 else
53df40a4 9306#endif
28dbcedc
AM
9307 abort ();
9308 }
0e287786 9309
bca6d0e3
AM
9310 /* Must use a stable sort here. A modified insertion sort,
9311 since the relocs are mostly sorted already. */
0e287786
AM
9312 elt_size = reldata->hdr->sh_entsize;
9313 base = reldata->hdr->contents;
9314 end = base + count * elt_size;
bca6d0e3 9315 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
9316 abort ();
9317
9318 /* Ensure the first element is lowest. This acts as a sentinel,
9319 speeding the main loop below. */
9320 r_off = (*ext_r_off) (base);
9321 for (p = loc = base; (p += elt_size) < end; )
9322 {
9323 bfd_vma r_off2 = (*ext_r_off) (p);
9324 if (r_off > r_off2)
9325 {
9326 r_off = r_off2;
9327 loc = p;
9328 }
9329 }
9330 if (loc != base)
9331 {
9332 /* Don't just swap *base and *loc as that changes the order
9333 of the original base[0] and base[1] if they happen to
9334 have the same r_offset. */
bca6d0e3
AM
9335 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9336 memcpy (onebuf, loc, elt_size);
0e287786 9337 memmove (base + elt_size, base, loc - base);
bca6d0e3 9338 memcpy (base, onebuf, elt_size);
0e287786
AM
9339 }
9340
b29b8669 9341 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
9342 {
9343 /* base to p is sorted, *p is next to insert. */
9344 r_off = (*ext_r_off) (p);
9345 /* Search the sorted region for location to insert. */
9346 loc = p - elt_size;
9347 while (r_off < (*ext_r_off) (loc))
9348 loc -= elt_size;
9349 loc += elt_size;
9350 if (loc != p)
9351 {
bca6d0e3
AM
9352 /* Chances are there is a run of relocs to insert here,
9353 from one of more input files. Files are not always
9354 linked in order due to the way elf_link_input_bfd is
9355 called. See pr17666. */
9356 size_t sortlen = p - loc;
9357 bfd_vma r_off2 = (*ext_r_off) (loc);
9358 size_t runlen = elt_size;
9359 size_t buf_size = 96 * 1024;
9360 while (p + runlen < end
9361 && (sortlen <= buf_size
9362 || runlen + elt_size <= buf_size)
9363 && r_off2 > (*ext_r_off) (p + runlen))
9364 runlen += elt_size;
9365 if (buf == NULL)
9366 {
9367 buf = bfd_malloc (buf_size);
9368 if (buf == NULL)
0a1b45a2 9369 return false;
bca6d0e3
AM
9370 }
9371 if (runlen < sortlen)
9372 {
9373 memcpy (buf, p, runlen);
9374 memmove (loc + runlen, loc, sortlen);
9375 memcpy (loc, buf, runlen);
9376 }
9377 else
9378 {
9379 memcpy (buf, loc, sortlen);
9380 memmove (loc, p, runlen);
9381 memcpy (loc + runlen, buf, sortlen);
9382 }
b29b8669 9383 p += runlen - elt_size;
0e287786
AM
9384 }
9385 }
9386 /* Hashes are no longer valid. */
28dbcedc
AM
9387 free (reldata->hashes);
9388 reldata->hashes = NULL;
bca6d0e3 9389 free (buf);
53df40a4 9390 }
0a1b45a2 9391 return true;
c152c796
AM
9392}
9393
9394struct elf_link_sort_rela
9395{
9396 union {
9397 bfd_vma offset;
9398 bfd_vma sym_mask;
9399 } u;
9400 enum elf_reloc_type_class type;
9401 /* We use this as an array of size int_rels_per_ext_rel. */
9402 Elf_Internal_Rela rela[1];
9403};
9404
dcea6a95
AM
9405/* qsort stability here and for cmp2 is only an issue if multiple
9406 dynamic relocations are emitted at the same address. But targets
9407 that apply a series of dynamic relocations each operating on the
9408 result of the prior relocation can't use -z combreloc as
9409 implemented anyway. Such schemes tend to be broken by sorting on
9410 symbol index. That leaves dynamic NONE relocs as the only other
9411 case where ld might emit multiple relocs at the same address, and
9412 those are only emitted due to target bugs. */
9413
c152c796
AM
9414static int
9415elf_link_sort_cmp1 (const void *A, const void *B)
9416{
a50b1753
NC
9417 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9418 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
9419 int relativea, relativeb;
9420
9421 relativea = a->type == reloc_class_relative;
9422 relativeb = b->type == reloc_class_relative;
9423
9424 if (relativea < relativeb)
9425 return 1;
9426 if (relativea > relativeb)
9427 return -1;
9428 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9429 return -1;
9430 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9431 return 1;
9432 if (a->rela->r_offset < b->rela->r_offset)
9433 return -1;
9434 if (a->rela->r_offset > b->rela->r_offset)
9435 return 1;
9436 return 0;
9437}
9438
9439static int
9440elf_link_sort_cmp2 (const void *A, const void *B)
9441{
a50b1753
NC
9442 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9443 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 9444
7e612e98 9445 if (a->type < b->type)
c152c796 9446 return -1;
7e612e98 9447 if (a->type > b->type)
c152c796 9448 return 1;
7e612e98 9449 if (a->u.offset < b->u.offset)
c152c796 9450 return -1;
7e612e98 9451 if (a->u.offset > b->u.offset)
c152c796
AM
9452 return 1;
9453 if (a->rela->r_offset < b->rela->r_offset)
9454 return -1;
9455 if (a->rela->r_offset > b->rela->r_offset)
9456 return 1;
9457 return 0;
9458}
9459
9460static size_t
9461elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9462{
3410fea8 9463 asection *dynamic_relocs;
fc66a176
L
9464 asection *rela_dyn;
9465 asection *rel_dyn;
c152c796
AM
9466 bfd_size_type count, size;
9467 size_t i, ret, sort_elt, ext_size;
9468 bfd_byte *sort, *s_non_relative, *p;
9469 struct elf_link_sort_rela *sq;
9470 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9471 int i2e = bed->s->int_rels_per_ext_rel;
61826503 9472 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
c152c796
AM
9473 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9474 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9475 struct bfd_link_order *lo;
9476 bfd_vma r_sym_mask;
0a1b45a2 9477 bool use_rela;
c152c796 9478
3410fea8
NC
9479 /* Find a dynamic reloc section. */
9480 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9481 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9482 if (rela_dyn != NULL && rela_dyn->size > 0
9483 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 9484 {
0a1b45a2 9485 bool use_rela_initialised = false;
3410fea8
NC
9486
9487 /* This is just here to stop gcc from complaining.
c8e44c6d 9488 Its initialization checking code is not perfect. */
0a1b45a2 9489 use_rela = true;
3410fea8
NC
9490
9491 /* Both sections are present. Examine the sizes
9492 of the indirect sections to help us choose. */
9493 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9494 if (lo->type == bfd_indirect_link_order)
9495 {
9496 asection *o = lo->u.indirect.section;
9497
9498 if ((o->size % bed->s->sizeof_rela) == 0)
9499 {
9500 if ((o->size % bed->s->sizeof_rel) == 0)
9501 /* Section size is divisible by both rel and rela sizes.
9502 It is of no help to us. */
9503 ;
9504 else
9505 {
9506 /* Section size is only divisible by rela. */
535b785f 9507 if (use_rela_initialised && !use_rela)
3410fea8 9508 {
9793eb77 9509 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9510 "they are in more than one size"),
9511 abfd);
3410fea8
NC
9512 bfd_set_error (bfd_error_invalid_operation);
9513 return 0;
9514 }
9515 else
9516 {
0a1b45a2
AM
9517 use_rela = true;
9518 use_rela_initialised = true;
3410fea8
NC
9519 }
9520 }
9521 }
9522 else if ((o->size % bed->s->sizeof_rel) == 0)
9523 {
9524 /* Section size is only divisible by rel. */
535b785f 9525 if (use_rela_initialised && use_rela)
3410fea8 9526 {
9793eb77 9527 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9528 "they are in more than one size"),
9529 abfd);
3410fea8
NC
9530 bfd_set_error (bfd_error_invalid_operation);
9531 return 0;
9532 }
9533 else
9534 {
0a1b45a2
AM
9535 use_rela = false;
9536 use_rela_initialised = true;
3410fea8
NC
9537 }
9538 }
9539 else
9540 {
c8e44c6d
AM
9541 /* The section size is not divisible by either -
9542 something is wrong. */
9793eb77 9543 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9544 "they are of an unknown size"), abfd);
3410fea8
NC
9545 bfd_set_error (bfd_error_invalid_operation);
9546 return 0;
9547 }
9548 }
9549
9550 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9551 if (lo->type == bfd_indirect_link_order)
9552 {
9553 asection *o = lo->u.indirect.section;
9554
9555 if ((o->size % bed->s->sizeof_rela) == 0)
9556 {
9557 if ((o->size % bed->s->sizeof_rel) == 0)
9558 /* Section size is divisible by both rel and rela sizes.
9559 It is of no help to us. */
9560 ;
9561 else
9562 {
9563 /* Section size is only divisible by rela. */
535b785f 9564 if (use_rela_initialised && !use_rela)
3410fea8 9565 {
9793eb77 9566 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9567 "they are in more than one size"),
9568 abfd);
3410fea8
NC
9569 bfd_set_error (bfd_error_invalid_operation);
9570 return 0;
9571 }
9572 else
9573 {
0a1b45a2
AM
9574 use_rela = true;
9575 use_rela_initialised = true;
3410fea8
NC
9576 }
9577 }
9578 }
9579 else if ((o->size % bed->s->sizeof_rel) == 0)
9580 {
9581 /* Section size is only divisible by rel. */
535b785f 9582 if (use_rela_initialised && use_rela)
3410fea8 9583 {
9793eb77 9584 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9585 "they are in more than one size"),
9586 abfd);
3410fea8
NC
9587 bfd_set_error (bfd_error_invalid_operation);
9588 return 0;
9589 }
9590 else
9591 {
0a1b45a2
AM
9592 use_rela = false;
9593 use_rela_initialised = true;
3410fea8
NC
9594 }
9595 }
9596 else
9597 {
c8e44c6d
AM
9598 /* The section size is not divisible by either -
9599 something is wrong. */
9793eb77 9600 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9601 "they are of an unknown size"), abfd);
3410fea8
NC
9602 bfd_set_error (bfd_error_invalid_operation);
9603 return 0;
9604 }
9605 }
9606
9607 if (! use_rela_initialised)
9608 /* Make a guess. */
0a1b45a2 9609 use_rela = true;
c152c796 9610 }
fc66a176 9611 else if (rela_dyn != NULL && rela_dyn->size > 0)
0a1b45a2 9612 use_rela = true;
fc66a176 9613 else if (rel_dyn != NULL && rel_dyn->size > 0)
0a1b45a2 9614 use_rela = false;
c152c796 9615 else
fc66a176 9616 return 0;
3410fea8
NC
9617
9618 if (use_rela)
c152c796 9619 {
3410fea8 9620 dynamic_relocs = rela_dyn;
c152c796
AM
9621 ext_size = bed->s->sizeof_rela;
9622 swap_in = bed->s->swap_reloca_in;
9623 swap_out = bed->s->swap_reloca_out;
9624 }
3410fea8
NC
9625 else
9626 {
9627 dynamic_relocs = rel_dyn;
9628 ext_size = bed->s->sizeof_rel;
9629 swap_in = bed->s->swap_reloc_in;
9630 swap_out = bed->s->swap_reloc_out;
9631 }
c152c796
AM
9632
9633 size = 0;
3410fea8 9634 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 9635 if (lo->type == bfd_indirect_link_order)
3410fea8 9636 size += lo->u.indirect.section->size;
c152c796 9637
3410fea8 9638 if (size != dynamic_relocs->size)
c152c796
AM
9639 return 0;
9640
9641 sort_elt = (sizeof (struct elf_link_sort_rela)
9642 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
9643
9644 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
9645 if (count == 0)
9646 return 0;
a50b1753 9647 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 9648
c152c796
AM
9649 if (sort == NULL)
9650 {
9651 (*info->callbacks->warning)
9793eb77 9652 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
c152c796
AM
9653 return 0;
9654 }
9655
9656 if (bed->s->arch_size == 32)
9657 r_sym_mask = ~(bfd_vma) 0xff;
9658 else
9659 r_sym_mask = ~(bfd_vma) 0xffffffff;
9660
3410fea8 9661 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9662 if (lo->type == bfd_indirect_link_order)
9663 {
9664 bfd_byte *erel, *erelend;
9665 asection *o = lo->u.indirect.section;
9666
1da212d6
AM
9667 if (o->contents == NULL && o->size != 0)
9668 {
9669 /* This is a reloc section that is being handled as a normal
9670 section. See bfd_section_from_shdr. We can't combine
9671 relocs in this case. */
9672 free (sort);
9673 return 0;
9674 }
c152c796 9675 erel = o->contents;
eea6121a 9676 erelend = o->contents + o->size;
c8e44c6d 9677 p = sort + o->output_offset * opb / ext_size * sort_elt;
3410fea8 9678
c152c796
AM
9679 while (erel < erelend)
9680 {
9681 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 9682
c152c796 9683 (*swap_in) (abfd, erel, s->rela);
7e612e98 9684 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
9685 s->u.sym_mask = r_sym_mask;
9686 p += sort_elt;
9687 erel += ext_size;
9688 }
9689 }
9690
9691 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9692
9693 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9694 {
9695 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9696 if (s->type != reloc_class_relative)
9697 break;
9698 }
9699 ret = i;
9700 s_non_relative = p;
9701
9702 sq = (struct elf_link_sort_rela *) s_non_relative;
9703 for (; i < count; i++, p += sort_elt)
9704 {
9705 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9706 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9707 sq = sp;
9708 sp->u.offset = sq->rela->r_offset;
9709 }
9710
9711 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9712
c8e44c6d
AM
9713 struct elf_link_hash_table *htab = elf_hash_table (info);
9714 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9715 {
9716 /* We have plt relocs in .rela.dyn. */
9717 sq = (struct elf_link_sort_rela *) sort;
9718 for (i = 0; i < count; i++)
9719 if (sq[count - i - 1].type != reloc_class_plt)
9720 break;
9721 if (i != 0 && htab->srelplt->size == i * ext_size)
9722 {
9723 struct bfd_link_order **plo;
9724 /* Put srelplt link_order last. This is so the output_offset
9725 set in the next loop is correct for DT_JMPREL. */
9726 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9727 if ((*plo)->type == bfd_indirect_link_order
9728 && (*plo)->u.indirect.section == htab->srelplt)
9729 {
9730 lo = *plo;
9731 *plo = lo->next;
9732 }
9733 else
9734 plo = &(*plo)->next;
9735 *plo = lo;
9736 lo->next = NULL;
9737 dynamic_relocs->map_tail.link_order = lo;
9738 }
9739 }
9740
9741 p = sort;
3410fea8 9742 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9743 if (lo->type == bfd_indirect_link_order)
9744 {
9745 bfd_byte *erel, *erelend;
9746 asection *o = lo->u.indirect.section;
9747
9748 erel = o->contents;
eea6121a 9749 erelend = o->contents + o->size;
c8e44c6d 9750 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
c152c796
AM
9751 while (erel < erelend)
9752 {
9753 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9754 (*swap_out) (abfd, s->rela, erel);
9755 p += sort_elt;
9756 erel += ext_size;
9757 }
9758 }
9759
9760 free (sort);
3410fea8 9761 *psec = dynamic_relocs;
c152c796
AM
9762 return ret;
9763}
9764
ef10c3ac 9765/* Add a symbol to the output symbol string table. */
c152c796 9766
6e0b88f1 9767static int
37bb890f 9768elf_link_output_symstrtab (void *finf,
ef10c3ac
L
9769 const char *name,
9770 Elf_Internal_Sym *elfsym,
9771 asection *input_sec,
9772 struct elf_link_hash_entry *h)
c152c796 9773{
37bb890f 9774 struct elf_final_link_info *flinfo = finf;
6e0b88f1 9775 int (*output_symbol_hook)
c152c796
AM
9776 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9777 struct elf_link_hash_entry *);
ef10c3ac 9778 struct elf_link_hash_table *hash_table;
c152c796 9779 const struct elf_backend_data *bed;
ef10c3ac 9780 bfd_size_type strtabsize;
c152c796 9781
8539e4e8
AM
9782 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9783
8b127cbc 9784 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
9785 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9786 if (output_symbol_hook != NULL)
9787 {
8b127cbc 9788 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
9789 if (ret != 1)
9790 return ret;
c152c796
AM
9791 }
9792
06f44071
AM
9793 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
9794 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
9795 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
9796 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
9797
ef10c3ac
L
9798 if (name == NULL
9799 || *name == '\0'
9800 || (input_sec->flags & SEC_EXCLUDE))
9801 elfsym->st_name = (unsigned long) -1;
c152c796
AM
9802 else
9803 {
ef10c3ac
L
9804 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9805 to get the final offset for st_name. */
3f2e9699 9806 char *versioned_name = (char *) name;
496afd17
L
9807 if (h != NULL)
9808 {
9809 if (h->versioned == versioned && h->def_dynamic)
9810 {
9811 /* Keep only one '@' for versioned symbols defined in
9812 shared objects. */
9813 char *version = strrchr (name, ELF_VER_CHR);
9814 char *base_end = strchr (name, ELF_VER_CHR);
9815 if (version != base_end)
9816 {
9817 size_t base_len;
9818 size_t len = strlen (name);
9819 versioned_name = bfd_alloc (flinfo->output_bfd, len);
9820 if (versioned_name == NULL)
9821 return 0;
9822 base_len = base_end - name;
9823 memcpy (versioned_name, name, base_len);
9824 memcpy (versioned_name + base_len, version,
9825 len - base_len);
9826 }
9827 }
9828 }
9829 else if (flinfo->info->unique_symbol
9830 && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
3f2e9699 9831 {
496afd17 9832 struct local_hash_entry *lh;
4467df35
L
9833 size_t count_len;
9834 size_t base_len;
9835 char buf[30];
496afd17 9836 switch (ELF_ST_TYPE (elfsym->st_info))
3f2e9699 9837 {
496afd17
L
9838 case STT_FILE:
9839 case STT_SECTION:
9840 break;
9841 default:
9842 lh = (struct local_hash_entry *) bfd_hash_lookup
0a1b45a2 9843 (&flinfo->local_hash_table, name, true, false);
496afd17 9844 if (lh == NULL)
3f2e9699 9845 return 0;
4467df35
L
9846 /* Always append ".COUNT" to local symbols to avoid
9847 potential conflicts with local symbol "XXX.COUNT". */
9848 sprintf (buf, "%lx", lh->count);
9849 base_len = lh->size;
9850 if (!base_len)
496afd17 9851 {
4467df35
L
9852 base_len = strlen (name);
9853 lh->size = base_len;
496afd17 9854 }
4467df35
L
9855 count_len = strlen (buf);
9856 versioned_name = bfd_alloc (flinfo->output_bfd,
9857 base_len + count_len + 2);
9858 if (versioned_name == NULL)
9859 return 0;
9860 memcpy (versioned_name, name, base_len);
9861 versioned_name[base_len] = '.';
9862 memcpy (versioned_name + base_len + 1, buf,
9863 count_len + 1);
496afd17
L
9864 lh->count++;
9865 break;
3f2e9699
L
9866 }
9867 }
ef10c3ac
L
9868 elfsym->st_name
9869 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
0a1b45a2 9870 versioned_name, false);
c152c796 9871 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 9872 return 0;
c152c796
AM
9873 }
9874
ef10c3ac
L
9875 hash_table = elf_hash_table (flinfo->info);
9876 strtabsize = hash_table->strtabsize;
9877 if (strtabsize <= hash_table->strtabcount)
c152c796 9878 {
ef10c3ac
L
9879 strtabsize += strtabsize;
9880 hash_table->strtabsize = strtabsize;
9881 strtabsize *= sizeof (*hash_table->strtab);
9882 hash_table->strtab
9883 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9884 strtabsize);
9885 if (hash_table->strtab == NULL)
6e0b88f1 9886 return 0;
c152c796 9887 }
ef10c3ac
L
9888 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9889 hash_table->strtab[hash_table->strtabcount].dest_index
9890 = hash_table->strtabcount;
9891 hash_table->strtab[hash_table->strtabcount].destshndx_index
9892 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9893
ed48ec2e 9894 flinfo->output_bfd->symcount += 1;
ef10c3ac
L
9895 hash_table->strtabcount += 1;
9896
9897 return 1;
9898}
9899
9900/* Swap symbols out to the symbol table and flush the output symbols to
9901 the file. */
9902
0a1b45a2 9903static bool
ef10c3ac
L
9904elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9905{
9906 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
986f0783 9907 size_t amt;
ef53be89 9908 size_t i;
ef10c3ac
L
9909 const struct elf_backend_data *bed;
9910 bfd_byte *symbuf;
9911 Elf_Internal_Shdr *hdr;
9912 file_ptr pos;
0a1b45a2 9913 bool ret;
ef10c3ac
L
9914
9915 if (!hash_table->strtabcount)
0a1b45a2 9916 return true;
ef10c3ac
L
9917
9918 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9919
9920 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9921
ef10c3ac
L
9922 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9923 symbuf = (bfd_byte *) bfd_malloc (amt);
9924 if (symbuf == NULL)
0a1b45a2 9925 return false;
1b786873 9926
ef10c3ac 9927 if (flinfo->symshndxbuf)
c152c796 9928 {
ef53be89
AM
9929 amt = sizeof (Elf_External_Sym_Shndx);
9930 amt *= bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9931 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9932 if (flinfo->symshndxbuf == NULL)
c152c796 9933 {
ef10c3ac 9934 free (symbuf);
0a1b45a2 9935 return false;
c152c796 9936 }
c152c796
AM
9937 }
9938
3d16b64e 9939 /* Now swap out the symbols. */
ef10c3ac
L
9940 for (i = 0; i < hash_table->strtabcount; i++)
9941 {
9942 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9943 if (elfsym->sym.st_name == (unsigned long) -1)
9944 elfsym->sym.st_name = 0;
9945 else
9946 elfsym->sym.st_name
9947 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9948 elfsym->sym.st_name);
3d16b64e
NA
9949
9950 /* Inform the linker of the addition of this symbol. */
9951
9952 if (flinfo->info->callbacks->ctf_new_symbol)
9953 flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
9954 &elfsym->sym);
9955
ef10c3ac
L
9956 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9957 ((bfd_byte *) symbuf
9958 + (elfsym->dest_index
9959 * bed->s->sizeof_sym)),
9960 (flinfo->symshndxbuf
9961 + elfsym->destshndx_index));
9962 }
9963
9964 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9965 pos = hdr->sh_offset + hdr->sh_size;
9966 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9967 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9968 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9969 {
9970 hdr->sh_size += amt;
0a1b45a2 9971 ret = true;
ef10c3ac
L
9972 }
9973 else
0a1b45a2 9974 ret = false;
c152c796 9975
ef10c3ac
L
9976 free (symbuf);
9977
9978 free (hash_table->strtab);
9979 hash_table->strtab = NULL;
9980
9981 return ret;
c152c796
AM
9982}
9983
c0d5a53d
L
9984/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9985
0a1b45a2 9986static bool
c0d5a53d
L
9987check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9988{
4fbb74a6
AM
9989 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9990 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
9991 {
9992 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 9993 beyond 64k. */
4eca0228 9994 _bfd_error_handler
695344c0 9995 /* xgettext:c-format */
9793eb77 9996 (_("%pB: too many sections: %d (>= %d)"),
4fbb74a6 9997 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d 9998 bfd_set_error (bfd_error_nonrepresentable_section);
0a1b45a2 9999 return false;
c0d5a53d 10000 }
0a1b45a2 10001 return true;
c0d5a53d
L
10002}
10003
c152c796
AM
10004/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10005 allowing an unsatisfied unversioned symbol in the DSO to match a
10006 versioned symbol that would normally require an explicit version.
10007 We also handle the case that a DSO references a hidden symbol
10008 which may be satisfied by a versioned symbol in another DSO. */
10009
0a1b45a2 10010static bool
c152c796
AM
10011elf_link_check_versioned_symbol (struct bfd_link_info *info,
10012 const struct elf_backend_data *bed,
10013 struct elf_link_hash_entry *h)
10014{
10015 bfd *abfd;
10016 struct elf_link_loaded_list *loaded;
10017
10018 if (!is_elf_hash_table (info->hash))
0a1b45a2 10019 return false;
c152c796 10020
90c984fc
L
10021 /* Check indirect symbol. */
10022 while (h->root.type == bfd_link_hash_indirect)
10023 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10024
c152c796
AM
10025 switch (h->root.type)
10026 {
10027 default:
10028 abfd = NULL;
10029 break;
10030
10031 case bfd_link_hash_undefined:
10032 case bfd_link_hash_undefweak:
10033 abfd = h->root.u.undef.abfd;
f4ab0e2d
L
10034 if (abfd == NULL
10035 || (abfd->flags & DYNAMIC) == 0
e56f61be 10036 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
0a1b45a2 10037 return false;
c152c796
AM
10038 break;
10039
10040 case bfd_link_hash_defined:
10041 case bfd_link_hash_defweak:
10042 abfd = h->root.u.def.section->owner;
10043 break;
10044
10045 case bfd_link_hash_common:
10046 abfd = h->root.u.c.p->section->owner;
10047 break;
10048 }
10049 BFD_ASSERT (abfd != NULL);
10050
e310298c 10051 for (loaded = elf_hash_table (info)->dyn_loaded;
c152c796
AM
10052 loaded != NULL;
10053 loaded = loaded->next)
10054 {
10055 bfd *input;
10056 Elf_Internal_Shdr *hdr;
ef53be89
AM
10057 size_t symcount;
10058 size_t extsymcount;
10059 size_t extsymoff;
c152c796
AM
10060 Elf_Internal_Shdr *versymhdr;
10061 Elf_Internal_Sym *isym;
10062 Elf_Internal_Sym *isymend;
10063 Elf_Internal_Sym *isymbuf;
10064 Elf_External_Versym *ever;
10065 Elf_External_Versym *extversym;
10066
10067 input = loaded->abfd;
10068
10069 /* We check each DSO for a possible hidden versioned definition. */
10070 if (input == abfd
c152c796
AM
10071 || elf_dynversym (input) == 0)
10072 continue;
10073
10074 hdr = &elf_tdata (input)->dynsymtab_hdr;
10075
10076 symcount = hdr->sh_size / bed->s->sizeof_sym;
10077 if (elf_bad_symtab (input))
10078 {
10079 extsymcount = symcount;
10080 extsymoff = 0;
10081 }
10082 else
10083 {
10084 extsymcount = symcount - hdr->sh_info;
10085 extsymoff = hdr->sh_info;
10086 }
10087
10088 if (extsymcount == 0)
10089 continue;
10090
10091 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10092 NULL, NULL, NULL);
10093 if (isymbuf == NULL)
0a1b45a2 10094 return false;
c152c796
AM
10095
10096 /* Read in any version definitions. */
10097 versymhdr = &elf_tdata (input)->dynversym_hdr;
c152c796 10098 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
2bb3687b
AM
10099 || (extversym = (Elf_External_Versym *)
10100 _bfd_malloc_and_read (input, versymhdr->sh_size,
10101 versymhdr->sh_size)) == NULL)
c152c796 10102 {
c152c796 10103 free (isymbuf);
0a1b45a2 10104 return false;
c152c796
AM
10105 }
10106
10107 ever = extversym + extsymoff;
10108 isymend = isymbuf + extsymcount;
10109 for (isym = isymbuf; isym < isymend; isym++, ever++)
10110 {
10111 const char *name;
10112 Elf_Internal_Versym iver;
10113 unsigned short version_index;
10114
10115 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10116 || isym->st_shndx == SHN_UNDEF)
10117 continue;
10118
10119 name = bfd_elf_string_from_elf_section (input,
10120 hdr->sh_link,
10121 isym->st_name);
10122 if (strcmp (name, h->root.root.string) != 0)
10123 continue;
10124
10125 _bfd_elf_swap_versym_in (input, ever, &iver);
10126
d023c380
L
10127 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10128 && !(h->def_regular
10129 && h->forced_local))
c152c796
AM
10130 {
10131 /* If we have a non-hidden versioned sym, then it should
d023c380
L
10132 have provided a definition for the undefined sym unless
10133 it is defined in a non-shared object and forced local.
10134 */
c152c796
AM
10135 abort ();
10136 }
10137
10138 version_index = iver.vs_vers & VERSYM_VERSION;
10139 if (version_index == 1 || version_index == 2)
10140 {
10141 /* This is the base or first version. We can use it. */
10142 free (extversym);
10143 free (isymbuf);
0a1b45a2 10144 return true;
c152c796
AM
10145 }
10146 }
10147
10148 free (extversym);
10149 free (isymbuf);
10150 }
10151
0a1b45a2 10152 return false;
c152c796
AM
10153}
10154
b8871f35
L
10155/* Convert ELF common symbol TYPE. */
10156
10157static int
10158elf_link_convert_common_type (struct bfd_link_info *info, int type)
10159{
10160 /* Commom symbol can only appear in relocatable link. */
10161 if (!bfd_link_relocatable (info))
10162 abort ();
10163 switch (info->elf_stt_common)
10164 {
10165 case unchanged:
10166 break;
10167 case elf_stt_common:
10168 type = STT_COMMON;
10169 break;
10170 case no_elf_stt_common:
10171 type = STT_OBJECT;
10172 break;
10173 }
10174 return type;
10175}
10176
c152c796
AM
10177/* Add an external symbol to the symbol table. This is called from
10178 the hash table traversal routine. When generating a shared object,
10179 we go through the symbol table twice. The first time we output
10180 anything that might have been forced to local scope in a version
10181 script. The second time we output the symbols that are still
10182 global symbols. */
10183
0a1b45a2 10184static bool
7686d77d 10185elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 10186{
7686d77d 10187 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 10188 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 10189 struct elf_final_link_info *flinfo = eoinfo->flinfo;
0a1b45a2 10190 bool strip;
c152c796
AM
10191 Elf_Internal_Sym sym;
10192 asection *input_sec;
10193 const struct elf_backend_data *bed;
6e0b88f1
AM
10194 long indx;
10195 int ret;
b8871f35 10196 unsigned int type;
c152c796
AM
10197
10198 if (h->root.type == bfd_link_hash_warning)
10199 {
10200 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10201 if (h->root.type == bfd_link_hash_new)
0a1b45a2 10202 return true;
c152c796
AM
10203 }
10204
10205 /* Decide whether to output this symbol in this pass. */
10206 if (eoinfo->localsyms)
10207 {
4deb8f71 10208 if (!h->forced_local)
0a1b45a2 10209 return true;
c152c796
AM
10210 }
10211 else
10212 {
4deb8f71 10213 if (h->forced_local)
0a1b45a2 10214 return true;
c152c796
AM
10215 }
10216
8b127cbc 10217 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 10218
12ac1cf5 10219 if (h->root.type == bfd_link_hash_undefined)
c152c796 10220 {
12ac1cf5
NC
10221 /* If we have an undefined symbol reference here then it must have
10222 come from a shared library that is being linked in. (Undefined
98da7939
L
10223 references in regular files have already been handled unless
10224 they are in unreferenced sections which are removed by garbage
10225 collection). */
0a1b45a2 10226 bool ignore_undef = false;
12ac1cf5
NC
10227
10228 /* Some symbols may be special in that the fact that they're
10229 undefined can be safely ignored - let backend determine that. */
10230 if (bed->elf_backend_ignore_undef_symbol)
10231 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10232
10233 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 10234 if (!ignore_undef
c54f1524 10235 && h->ref_dynamic_nonweak
8b127cbc
AM
10236 && (!h->ref_regular || flinfo->info->gc_sections)
10237 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10238 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
95a51568
FS
10239 {
10240 flinfo->info->callbacks->undefined_symbol
10241 (flinfo->info, h->root.root.string,
10242 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10243 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10244 && !flinfo->info->warn_unresolved_syms);
10245 }
97196564
L
10246
10247 /* Strip a global symbol defined in a discarded section. */
10248 if (h->indx == -3)
0a1b45a2 10249 return true;
c152c796
AM
10250 }
10251
10252 /* We should also warn if a forced local symbol is referenced from
10253 shared libraries. */
0e1862bb 10254 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
10255 && h->forced_local
10256 && h->ref_dynamic
371a5866 10257 && h->def_regular
f5385ebf 10258 && !h->dynamic_def
ee659f1f 10259 && h->ref_dynamic_nonweak
8b127cbc 10260 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 10261 {
17d078c5
AM
10262 bfd *def_bfd;
10263 const char *msg;
90c984fc
L
10264 struct elf_link_hash_entry *hi = h;
10265
10266 /* Check indirect symbol. */
10267 while (hi->root.type == bfd_link_hash_indirect)
10268 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
10269
10270 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
695344c0 10271 /* xgettext:c-format */
871b3ab2 10272 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
17d078c5 10273 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
695344c0 10274 /* xgettext:c-format */
871b3ab2 10275 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
17d078c5 10276 else
695344c0 10277 /* xgettext:c-format */
871b3ab2 10278 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
8b127cbc 10279 def_bfd = flinfo->output_bfd;
90c984fc
L
10280 if (hi->root.u.def.section != bfd_abs_section_ptr)
10281 def_bfd = hi->root.u.def.section->owner;
c08bb8dd
AM
10282 _bfd_error_handler (msg, flinfo->output_bfd,
10283 h->root.root.string, def_bfd);
17d078c5 10284 bfd_set_error (bfd_error_bad_value);
0a1b45a2
AM
10285 eoinfo->failed = true;
10286 return false;
c152c796
AM
10287 }
10288
10289 /* We don't want to output symbols that have never been mentioned by
10290 a regular file, or that we have been told to strip. However, if
10291 h->indx is set to -2, the symbol is used by a reloc and we must
10292 output it. */
0a1b45a2 10293 strip = false;
c152c796 10294 if (h->indx == -2)
d983c8c5 10295 ;
f5385ebf 10296 else if ((h->def_dynamic
77cfaee6
AM
10297 || h->ref_dynamic
10298 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
10299 && !h->def_regular
10300 && !h->ref_regular)
0a1b45a2 10301 strip = true;
8b127cbc 10302 else if (flinfo->info->strip == strip_all)
0a1b45a2 10303 strip = true;
8b127cbc
AM
10304 else if (flinfo->info->strip == strip_some
10305 && bfd_hash_lookup (flinfo->info->keep_hash,
0a1b45a2
AM
10306 h->root.root.string, false, false) == NULL)
10307 strip = true;
d56d55e7
AM
10308 else if ((h->root.type == bfd_link_hash_defined
10309 || h->root.type == bfd_link_hash_defweak)
8b127cbc 10310 && ((flinfo->info->strip_discarded
dbaa2011 10311 && discarded_section (h->root.u.def.section))
ca4be51c
AM
10312 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10313 && h->root.u.def.section->owner != NULL
d56d55e7 10314 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
0a1b45a2 10315 strip = true;
9e2278f5
AM
10316 else if ((h->root.type == bfd_link_hash_undefined
10317 || h->root.type == bfd_link_hash_undefweak)
10318 && h->root.u.undef.abfd != NULL
10319 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
0a1b45a2 10320 strip = true;
c152c796 10321
b8871f35
L
10322 type = h->type;
10323
c152c796 10324 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
10325 nothing else to do. However, if it is a forced local symbol or
10326 an ifunc symbol we need to give the backend finish_dynamic_symbol
10327 function a chance to make it dynamic. */
c152c796
AM
10328 if (strip
10329 && h->dynindx == -1
b8871f35 10330 && type != STT_GNU_IFUNC
f5385ebf 10331 && !h->forced_local)
0a1b45a2 10332 return true;
c152c796
AM
10333
10334 sym.st_value = 0;
10335 sym.st_size = h->size;
10336 sym.st_other = h->other;
c152c796
AM
10337 switch (h->root.type)
10338 {
10339 default:
10340 case bfd_link_hash_new:
10341 case bfd_link_hash_warning:
10342 abort ();
0a1b45a2 10343 return false;
c152c796
AM
10344
10345 case bfd_link_hash_undefined:
10346 case bfd_link_hash_undefweak:
10347 input_sec = bfd_und_section_ptr;
10348 sym.st_shndx = SHN_UNDEF;
10349 break;
10350
10351 case bfd_link_hash_defined:
10352 case bfd_link_hash_defweak:
10353 {
10354 input_sec = h->root.u.def.section;
10355 if (input_sec->output_section != NULL)
10356 {
10357 sym.st_shndx =
8b127cbc 10358 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
10359 input_sec->output_section);
10360 if (sym.st_shndx == SHN_BAD)
10361 {
4eca0228 10362 _bfd_error_handler
695344c0 10363 /* xgettext:c-format */
871b3ab2 10364 (_("%pB: could not find output section %pA for input section %pA"),
8b127cbc 10365 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 10366 bfd_set_error (bfd_error_nonrepresentable_section);
0a1b45a2
AM
10367 eoinfo->failed = true;
10368 return false;
c152c796
AM
10369 }
10370
10371 /* ELF symbols in relocatable files are section relative,
10372 but in nonrelocatable files they are virtual
10373 addresses. */
10374 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 10375 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10376 {
10377 sym.st_value += input_sec->output_section->vma;
10378 if (h->type == STT_TLS)
10379 {
8b127cbc 10380 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
10381 if (tls_sec != NULL)
10382 sym.st_value -= tls_sec->vma;
c152c796
AM
10383 }
10384 }
10385 }
10386 else
10387 {
10388 BFD_ASSERT (input_sec->owner == NULL
10389 || (input_sec->owner->flags & DYNAMIC) != 0);
10390 sym.st_shndx = SHN_UNDEF;
10391 input_sec = bfd_und_section_ptr;
10392 }
10393 }
10394 break;
10395
10396 case bfd_link_hash_common:
10397 input_sec = h->root.u.c.p->section;
a4d8e49b 10398 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
10399 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10400 break;
10401
10402 case bfd_link_hash_indirect:
10403 /* These symbols are created by symbol versioning. They point
10404 to the decorated version of the name. For example, if the
10405 symbol foo@@GNU_1.2 is the default, which should be used when
10406 foo is used with no version, then we add an indirect symbol
10407 foo which points to foo@@GNU_1.2. We ignore these symbols,
10408 since the indirected symbol is already in the hash table. */
0a1b45a2 10409 return true;
c152c796
AM
10410 }
10411
b8871f35
L
10412 if (type == STT_COMMON || type == STT_OBJECT)
10413 switch (h->root.type)
10414 {
10415 case bfd_link_hash_common:
10416 type = elf_link_convert_common_type (flinfo->info, type);
10417 break;
10418 case bfd_link_hash_defined:
10419 case bfd_link_hash_defweak:
10420 if (bed->common_definition (&sym))
10421 type = elf_link_convert_common_type (flinfo->info, type);
10422 else
10423 type = STT_OBJECT;
10424 break;
10425 case bfd_link_hash_undefined:
10426 case bfd_link_hash_undefweak:
10427 break;
10428 default:
10429 abort ();
10430 }
10431
4deb8f71 10432 if (h->forced_local)
b8871f35
L
10433 {
10434 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10435 /* Turn off visibility on local symbol. */
10436 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10437 }
10438 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10439 else if (h->unique_global && h->def_regular)
10440 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10441 else if (h->root.type == bfd_link_hash_undefweak
10442 || h->root.type == bfd_link_hash_defweak)
10443 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10444 else
10445 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10446 sym.st_target_internal = h->target_internal;
10447
c152c796
AM
10448 /* Give the processor backend a chance to tweak the symbol value,
10449 and also to finish up anything that needs to be done for this
10450 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 10451 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 10452 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 10453 if ((h->type == STT_GNU_IFUNC
5f35ea9c 10454 && h->def_regular
0e1862bb 10455 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
10456 || ((h->dynindx != -1
10457 || h->forced_local)
0e1862bb 10458 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
10459 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10460 || h->root.type != bfd_link_hash_undefweak))
10461 || !h->forced_local)
8b127cbc 10462 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
10463 {
10464 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 10465 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796 10466 {
0a1b45a2
AM
10467 eoinfo->failed = true;
10468 return false;
c152c796
AM
10469 }
10470 }
10471
10472 /* If we are marking the symbol as undefined, and there are no
10473 non-weak references to this symbol from a regular object, then
10474 mark the symbol as weak undefined; if there are non-weak
10475 references, mark the symbol as strong. We can't do this earlier,
10476 because it might not be marked as undefined until the
10477 finish_dynamic_symbol routine gets through with it. */
10478 if (sym.st_shndx == SHN_UNDEF
f5385ebf 10479 && h->ref_regular
c152c796
AM
10480 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10481 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10482 {
10483 int bindtype;
b8871f35 10484 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
10485
10486 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10487 if (type == STT_GNU_IFUNC)
10488 type = STT_FUNC;
c152c796 10489
f5385ebf 10490 if (h->ref_regular_nonweak)
c152c796
AM
10491 bindtype = STB_GLOBAL;
10492 else
10493 bindtype = STB_WEAK;
2955ec4c 10494 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
10495 }
10496
bda987c2
CD
10497 /* If this is a symbol defined in a dynamic library, don't use the
10498 symbol size from the dynamic library. Relinking an executable
10499 against a new library may introduce gratuitous changes in the
10500 executable's symbols if we keep the size. */
10501 if (sym.st_shndx == SHN_UNDEF
10502 && !h->def_regular
10503 && h->def_dynamic)
10504 sym.st_size = 0;
10505
c152c796
AM
10506 /* If a non-weak symbol with non-default visibility is not defined
10507 locally, it is a fatal error. */
0e1862bb 10508 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
10509 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10510 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10511 && h->root.type == bfd_link_hash_undefined
f5385ebf 10512 && !h->def_regular)
c152c796 10513 {
17d078c5
AM
10514 const char *msg;
10515
10516 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
695344c0 10517 /* xgettext:c-format */
871b3ab2 10518 msg = _("%pB: protected symbol `%s' isn't defined");
17d078c5 10519 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
695344c0 10520 /* xgettext:c-format */
871b3ab2 10521 msg = _("%pB: internal symbol `%s' isn't defined");
17d078c5 10522 else
695344c0 10523 /* xgettext:c-format */
871b3ab2 10524 msg = _("%pB: hidden symbol `%s' isn't defined");
4eca0228 10525 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 10526 bfd_set_error (bfd_error_bad_value);
0a1b45a2
AM
10527 eoinfo->failed = true;
10528 return false;
c152c796
AM
10529 }
10530
10531 /* If this symbol should be put in the .dynsym section, then put it
10532 there now. We already know the symbol index. We also fill in
10533 the entry in the .hash section. */
1c2649ed
EB
10534 if (h->dynindx != -1
10535 && elf_hash_table (flinfo->info)->dynamic_sections_created
10536 && elf_hash_table (flinfo->info)->dynsym != NULL
10537 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
c152c796 10538 {
c152c796
AM
10539 bfd_byte *esym;
10540
90c984fc
L
10541 /* Since there is no version information in the dynamic string,
10542 if there is no version info in symbol version section, we will
1659f720 10543 have a run-time problem if not linking executable, referenced
4deb8f71 10544 by shared library, or not bound locally. */
1659f720 10545 if (h->verinfo.verdef == NULL
0e1862bb 10546 && (!bfd_link_executable (flinfo->info)
1659f720
L
10547 || h->ref_dynamic
10548 || !h->def_regular))
90c984fc
L
10549 {
10550 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10551
10552 if (p && p [1] != '\0')
10553 {
4eca0228 10554 _bfd_error_handler
695344c0 10555 /* xgettext:c-format */
9793eb77 10556 (_("%pB: no symbol version section for versioned symbol `%s'"),
90c984fc 10557 flinfo->output_bfd, h->root.root.string);
0a1b45a2
AM
10558 eoinfo->failed = true;
10559 return false;
90c984fc
L
10560 }
10561 }
10562
c152c796 10563 sym.st_name = h->dynstr_index;
cae1fbbb
L
10564 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10565 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 10566 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d 10567 {
0a1b45a2
AM
10568 eoinfo->failed = true;
10569 return false;
c0d5a53d 10570 }
3d16b64e
NA
10571
10572 /* Inform the linker of the addition of this symbol. */
10573
10574 if (flinfo->info->callbacks->ctf_new_dynsym)
10575 flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
10576
8b127cbc 10577 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 10578
8b127cbc 10579 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
10580 {
10581 size_t hash_entry_size;
10582 bfd_byte *bucketpos;
10583 bfd_vma chain;
41198d0c
L
10584 size_t bucketcount;
10585 size_t bucket;
10586
8b127cbc 10587 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 10588 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
10589
10590 hash_entry_size
8b127cbc
AM
10591 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10592 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 10593 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
10594 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10595 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10596 bucketpos);
10597 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10598 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
10599 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10600 }
c152c796 10601
8b127cbc 10602 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
10603 {
10604 Elf_Internal_Versym iversym;
10605 Elf_External_Versym *eversym;
10606
5fa370e4 10607 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
c152c796 10608 {
7b20f099
AM
10609 if (h->verinfo.verdef == NULL
10610 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10611 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
eb6e6af8 10612 iversym.vs_vers = 1;
c152c796
AM
10613 else
10614 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10615 }
10616 else
10617 {
10618 if (h->verinfo.vertree == NULL)
10619 iversym.vs_vers = 1;
10620 else
10621 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 10622 if (flinfo->info->create_default_symver)
3e3b46e5 10623 iversym.vs_vers++;
c152c796
AM
10624 }
10625
422f1182 10626 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 10627 defined locally. */
422f1182 10628 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
10629 iversym.vs_vers |= VERSYM_HIDDEN;
10630
8b127cbc 10631 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 10632 eversym += h->dynindx;
8b127cbc 10633 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
10634 }
10635 }
10636
d983c8c5
AM
10637 /* If the symbol is undefined, and we didn't output it to .dynsym,
10638 strip it from .symtab too. Obviously we can't do this for
10639 relocatable output or when needed for --emit-relocs. */
10640 else if (input_sec == bfd_und_section_ptr
10641 && h->indx != -2
66cae560
NC
10642 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10643 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
0e1862bb 10644 && !bfd_link_relocatable (flinfo->info))
0a1b45a2 10645 return true;
66cae560 10646
d983c8c5
AM
10647 /* Also strip others that we couldn't earlier due to dynamic symbol
10648 processing. */
10649 if (strip)
0a1b45a2 10650 return true;
d983c8c5 10651 if ((input_sec->flags & SEC_EXCLUDE) != 0)
0a1b45a2 10652 return true;
c152c796 10653
2ec55de3
AM
10654 /* Output a FILE symbol so that following locals are not associated
10655 with the wrong input file. We need one for forced local symbols
10656 if we've seen more than one FILE symbol or when we have exactly
10657 one FILE symbol but global symbols are present in a file other
10658 than the one with the FILE symbol. We also need one if linker
10659 defined symbols are present. In practice these conditions are
10660 always met, so just emit the FILE symbol unconditionally. */
10661 if (eoinfo->localsyms
10662 && !eoinfo->file_sym_done
10663 && eoinfo->flinfo->filesym_count != 0)
10664 {
10665 Elf_Internal_Sym fsym;
10666
10667 memset (&fsym, 0, sizeof (fsym));
10668 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10669 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
10670 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10671 bfd_und_section_ptr, NULL))
0a1b45a2 10672 return false;
2ec55de3 10673
0a1b45a2 10674 eoinfo->file_sym_done = true;
2ec55de3
AM
10675 }
10676
8b127cbc 10677 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
10678 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10679 input_sec, h);
6e0b88f1 10680 if (ret == 0)
c152c796 10681 {
0a1b45a2
AM
10682 eoinfo->failed = true;
10683 return false;
c152c796 10684 }
6e0b88f1
AM
10685 else if (ret == 1)
10686 h->indx = indx;
10687 else if (h->indx == -2)
10688 abort();
c152c796 10689
0a1b45a2 10690 return true;
c152c796
AM
10691}
10692
cdd3575c
AM
10693/* Return TRUE if special handling is done for relocs in SEC against
10694 symbols defined in discarded sections. */
10695
0a1b45a2 10696static bool
c152c796
AM
10697elf_section_ignore_discarded_relocs (asection *sec)
10698{
10699 const struct elf_backend_data *bed;
10700
cdd3575c
AM
10701 switch (sec->sec_info_type)
10702 {
dbaa2011
AM
10703 case SEC_INFO_TYPE_STABS:
10704 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 10705 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
0a1b45a2 10706 return true;
cdd3575c
AM
10707 default:
10708 break;
10709 }
c152c796
AM
10710
10711 bed = get_elf_backend_data (sec->owner);
10712 if (bed->elf_backend_ignore_discarded_relocs != NULL
10713 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
0a1b45a2 10714 return true;
c152c796 10715
0a1b45a2 10716 return false;
c152c796
AM
10717}
10718
9e66c942
AM
10719/* Return a mask saying how ld should treat relocations in SEC against
10720 symbols defined in discarded sections. If this function returns
10721 COMPLAIN set, ld will issue a warning message. If this function
10722 returns PRETEND set, and the discarded section was link-once and the
10723 same size as the kept link-once section, ld will pretend that the
10724 symbol was actually defined in the kept section. Otherwise ld will
10725 zero the reloc (at least that is the intent, but some cooperation by
10726 the target dependent code is needed, particularly for REL targets). */
10727
8a696751
AM
10728unsigned int
10729_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 10730{
9e66c942 10731 if (sec->flags & SEC_DEBUGGING)
69d54b1b 10732 return PRETEND;
cdd3575c
AM
10733
10734 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 10735 return 0;
cdd3575c
AM
10736
10737 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 10738 return 0;
cdd3575c 10739
9e66c942 10740 return COMPLAIN | PRETEND;
cdd3575c
AM
10741}
10742
3d7f7666
L
10743/* Find a match between a section and a member of a section group. */
10744
10745static asection *
c0f00686
L
10746match_group_member (asection *sec, asection *group,
10747 struct bfd_link_info *info)
3d7f7666
L
10748{
10749 asection *first = elf_next_in_group (group);
10750 asection *s = first;
10751
10752 while (s != NULL)
10753 {
c0f00686 10754 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
10755 return s;
10756
83180ade 10757 s = elf_next_in_group (s);
3d7f7666
L
10758 if (s == first)
10759 break;
10760 }
10761
10762 return NULL;
10763}
10764
01b3c8ab 10765/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
10766 to replace it. Return the replacement if it is OK. Otherwise return
10767 NULL. */
01b3c8ab
L
10768
10769asection *
c0f00686 10770_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
10771{
10772 asection *kept;
10773
10774 kept = sec->kept_section;
10775 if (kept != NULL)
10776 {
c2370991 10777 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 10778 kept = match_group_member (sec, kept, info);
58349d00
L
10779 if (kept != NULL)
10780 {
10781 if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10782 != (kept->rawsize != 0 ? kept->rawsize : kept->size))
10783 kept = NULL;
10784 else
10785 {
10786 /* Get the real kept section. */
10787 asection *next;
10788 for (next = kept->kept_section;
10789 next != NULL;
10790 next = next->kept_section)
10791 kept = next;
10792 }
10793 }
c2370991 10794 sec->kept_section = kept;
01b3c8ab
L
10795 }
10796 return kept;
10797}
10798
c152c796
AM
10799/* Link an input file into the linker output file. This function
10800 handles all the sections and relocations of the input file at once.
10801 This is so that we only have to read the local symbols once, and
10802 don't have to keep them in memory. */
10803
0a1b45a2 10804static bool
8b127cbc 10805elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 10806{
ece5ef60 10807 int (*relocate_section)
c152c796
AM
10808 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10809 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10810 bfd *output_bfd;
10811 Elf_Internal_Shdr *symtab_hdr;
10812 size_t locsymcount;
10813 size_t extsymoff;
10814 Elf_Internal_Sym *isymbuf;
10815 Elf_Internal_Sym *isym;
10816 Elf_Internal_Sym *isymend;
10817 long *pindex;
10818 asection **ppsection;
10819 asection *o;
10820 const struct elf_backend_data *bed;
c152c796 10821 struct elf_link_hash_entry **sym_hashes;
310fd250
L
10822 bfd_size_type address_size;
10823 bfd_vma r_type_mask;
10824 int r_sym_shift;
0a1b45a2 10825 bool have_file_sym = false;
c152c796 10826
8b127cbc 10827 output_bfd = flinfo->output_bfd;
c152c796
AM
10828 bed = get_elf_backend_data (output_bfd);
10829 relocate_section = bed->elf_backend_relocate_section;
10830
10831 /* If this is a dynamic object, we don't want to do anything here:
10832 we don't want the local symbols, and we don't want the section
10833 contents. */
10834 if ((input_bfd->flags & DYNAMIC) != 0)
0a1b45a2 10835 return true;
c152c796 10836
c152c796
AM
10837 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10838 if (elf_bad_symtab (input_bfd))
10839 {
10840 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10841 extsymoff = 0;
10842 }
10843 else
10844 {
10845 locsymcount = symtab_hdr->sh_info;
10846 extsymoff = symtab_hdr->sh_info;
10847 }
10848
99fabbc9
JL
10849 /* Enable GNU OSABI features in the output BFD that are used in the input
10850 BFD. */
10851 if (bed->elf_osabi == ELFOSABI_NONE
10852 || bed->elf_osabi == ELFOSABI_GNU
10853 || bed->elf_osabi == ELFOSABI_FREEBSD)
10854 elf_tdata (output_bfd)->has_gnu_osabi
04f89674
L
10855 |= (elf_tdata (input_bfd)->has_gnu_osabi
10856 & (bfd_link_relocatable (flinfo->info)
10857 ? -1 : ~elf_gnu_osabi_retain));
99fabbc9 10858
c152c796
AM
10859 /* Read the local symbols. */
10860 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10861 if (isymbuf == NULL && locsymcount != 0)
10862 {
10863 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
10864 flinfo->internal_syms,
10865 flinfo->external_syms,
10866 flinfo->locsym_shndx);
c152c796 10867 if (isymbuf == NULL)
0a1b45a2 10868 return false;
c152c796
AM
10869 }
10870
10871 /* Find local symbol sections and adjust values of symbols in
10872 SEC_MERGE sections. Write out those local symbols we know are
10873 going into the output file. */
10874 isymend = isymbuf + locsymcount;
8b127cbc 10875 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
10876 isym < isymend;
10877 isym++, pindex++, ppsection++)
10878 {
10879 asection *isec;
10880 const char *name;
10881 Elf_Internal_Sym osym;
6e0b88f1
AM
10882 long indx;
10883 int ret;
c152c796
AM
10884
10885 *pindex = -1;
10886
10887 if (elf_bad_symtab (input_bfd))
10888 {
10889 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10890 {
10891 *ppsection = NULL;
10892 continue;
10893 }
10894 }
10895
10896 if (isym->st_shndx == SHN_UNDEF)
10897 isec = bfd_und_section_ptr;
c152c796
AM
10898 else if (isym->st_shndx == SHN_ABS)
10899 isec = bfd_abs_section_ptr;
10900 else if (isym->st_shndx == SHN_COMMON)
10901 isec = bfd_com_section_ptr;
10902 else
10903 {
cb33740c
AM
10904 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10905 if (isec == NULL)
10906 {
10907 /* Don't attempt to output symbols with st_shnx in the
10908 reserved range other than SHN_ABS and SHN_COMMON. */
6835821b 10909 isec = bfd_und_section_ptr;
cb33740c 10910 }
dbaa2011 10911 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
10912 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10913 isym->st_value =
10914 _bfd_merged_section_offset (output_bfd, &isec,
10915 elf_section_data (isec)->sec_info,
10916 isym->st_value);
c152c796
AM
10917 }
10918
10919 *ppsection = isec;
10920
d983c8c5
AM
10921 /* Don't output the first, undefined, symbol. In fact, don't
10922 output any undefined local symbol. */
10923 if (isec == bfd_und_section_ptr)
c152c796
AM
10924 continue;
10925
10926 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10927 {
10928 /* We never output section symbols. Instead, we use the
10929 section symbol of the corresponding section in the output
10930 file. */
10931 continue;
10932 }
10933
10934 /* If we are stripping all symbols, we don't want to output this
10935 one. */
8b127cbc 10936 if (flinfo->info->strip == strip_all)
c152c796
AM
10937 continue;
10938
10939 /* If we are discarding all local symbols, we don't want to
10940 output this one. If we are generating a relocatable output
10941 file, then some of the local symbols may be required by
10942 relocs; we output them below as we discover that they are
10943 needed. */
8b127cbc 10944 if (flinfo->info->discard == discard_all)
c152c796
AM
10945 continue;
10946
10947 /* If this symbol is defined in a section which we are
f02571c5 10948 discarding, we don't need to keep it. */
abf874aa
CL
10949 if (isym->st_shndx != SHN_UNDEF
10950 && isym->st_shndx < SHN_LORESERVE
10951 && isec->output_section == NULL
10952 && flinfo->info->non_contiguous_regions
10953 && flinfo->info->non_contiguous_regions_warnings)
10954 {
10955 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
10956 "discards section `%s' from '%s'\n"),
765cf5f6 10957 isec->name, bfd_get_filename (isec->owner));
abf874aa
CL
10958 continue;
10959 }
10960
f02571c5 10961 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
10962 && isym->st_shndx < SHN_LORESERVE
10963 && bfd_section_removed_from_list (output_bfd,
10964 isec->output_section))
e75a280b
L
10965 continue;
10966
c152c796
AM
10967 /* Get the name of the symbol. */
10968 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10969 isym->st_name);
10970 if (name == NULL)
0a1b45a2 10971 return false;
c152c796
AM
10972
10973 /* See if we are discarding symbols with this name. */
8b127cbc 10974 if ((flinfo->info->strip == strip_some
0a1b45a2 10975 && (bfd_hash_lookup (flinfo->info->keep_hash, name, false, false)
c152c796 10976 == NULL))
8b127cbc 10977 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
10978 && (isec->flags & SEC_MERGE)
10979 && !bfd_link_relocatable (flinfo->info))
8b127cbc 10980 || flinfo->info->discard == discard_l)
c152c796
AM
10981 && bfd_is_local_label_name (input_bfd, name)))
10982 continue;
10983
ffbc01cc
AM
10984 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10985 {
ce875075
AM
10986 if (input_bfd->lto_output)
10987 /* -flto puts a temp file name here. This means builds
10988 are not reproducible. Discard the symbol. */
10989 continue;
0a1b45a2 10990 have_file_sym = true;
ffbc01cc
AM
10991 flinfo->filesym_count += 1;
10992 }
10993 if (!have_file_sym)
10994 {
10995 /* In the absence of debug info, bfd_find_nearest_line uses
10996 FILE symbols to determine the source file for local
10997 function symbols. Provide a FILE symbol here if input
10998 files lack such, so that their symbols won't be
10999 associated with a previous input file. It's not the
11000 source file, but the best we can do. */
5b4293ba 11001 const char *filename;
0a1b45a2 11002 have_file_sym = true;
ffbc01cc
AM
11003 flinfo->filesym_count += 1;
11004 memset (&osym, 0, sizeof (osym));
11005 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11006 osym.st_shndx = SHN_ABS;
5b4293ba
AM
11007 if (input_bfd->lto_output)
11008 filename = NULL;
11009 else
11010 filename = lbasename (bfd_get_filename (input_bfd));
11011 if (!elf_link_output_symstrtab (flinfo, filename, &osym,
11012 bfd_abs_section_ptr, NULL))
0a1b45a2 11013 return false;
ffbc01cc
AM
11014 }
11015
c152c796
AM
11016 osym = *isym;
11017
11018 /* Adjust the section index for the output file. */
11019 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11020 isec->output_section);
11021 if (osym.st_shndx == SHN_BAD)
0a1b45a2 11022 return false;
c152c796 11023
c152c796
AM
11024 /* ELF symbols in relocatable files are section relative, but
11025 in executable files they are virtual addresses. Note that
11026 this code assumes that all ELF sections have an associated
11027 BFD section with a reasonable value for output_offset; below
11028 we assume that they also have a reasonable value for
11029 output_section. Any special sections must be set up to meet
11030 these requirements. */
11031 osym.st_value += isec->output_offset;
0e1862bb 11032 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11033 {
11034 osym.st_value += isec->output_section->vma;
11035 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
11036 {
11037 /* STT_TLS symbols are relative to PT_TLS segment base. */
102def4d
AM
11038 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
11039 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
11040 else
11041 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
11042 STT_NOTYPE);
c152c796
AM
11043 }
11044 }
11045
6e0b88f1 11046 indx = bfd_get_symcount (output_bfd);
ef10c3ac 11047 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 11048 if (ret == 0)
0a1b45a2 11049 return false;
6e0b88f1
AM
11050 else if (ret == 1)
11051 *pindex = indx;
c152c796
AM
11052 }
11053
310fd250
L
11054 if (bed->s->arch_size == 32)
11055 {
11056 r_type_mask = 0xff;
11057 r_sym_shift = 8;
11058 address_size = 4;
11059 }
11060 else
11061 {
11062 r_type_mask = 0xffffffff;
11063 r_sym_shift = 32;
11064 address_size = 8;
11065 }
11066
c152c796
AM
11067 /* Relocate the contents of each section. */
11068 sym_hashes = elf_sym_hashes (input_bfd);
11069 for (o = input_bfd->sections; o != NULL; o = o->next)
11070 {
11071 bfd_byte *contents;
11072
11073 if (! o->linker_mark)
11074 {
11075 /* This section was omitted from the link. */
11076 continue;
11077 }
11078
7bdf4127 11079 if (!flinfo->info->resolve_section_groups
bcacc0f5
AM
11080 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
11081 {
11082 /* Deal with the group signature symbol. */
11083 struct bfd_elf_section_data *sec_data = elf_section_data (o);
11084 unsigned long symndx = sec_data->this_hdr.sh_info;
11085 asection *osec = o->output_section;
11086
7bdf4127 11087 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
bcacc0f5
AM
11088 if (symndx >= locsymcount
11089 || (elf_bad_symtab (input_bfd)
8b127cbc 11090 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
11091 {
11092 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
11093 while (h->root.type == bfd_link_hash_indirect
11094 || h->root.type == bfd_link_hash_warning)
11095 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11096 /* Arrange for symbol to be output. */
11097 h->indx = -2;
11098 elf_section_data (osec)->this_hdr.sh_info = -2;
11099 }
11100 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
11101 {
11102 /* We'll use the output section target_index. */
8b127cbc 11103 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
11104 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
11105 }
11106 else
11107 {
8b127cbc 11108 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
11109 {
11110 /* Otherwise output the local symbol now. */
11111 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 11112 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 11113 const char *name;
6e0b88f1
AM
11114 long indx;
11115 int ret;
bcacc0f5
AM
11116
11117 name = bfd_elf_string_from_elf_section (input_bfd,
11118 symtab_hdr->sh_link,
11119 sym.st_name);
11120 if (name == NULL)
0a1b45a2 11121 return false;
bcacc0f5
AM
11122
11123 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11124 sec);
11125 if (sym.st_shndx == SHN_BAD)
0a1b45a2 11126 return false;
bcacc0f5
AM
11127
11128 sym.st_value += o->output_offset;
11129
6e0b88f1 11130 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
11131 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11132 NULL);
6e0b88f1 11133 if (ret == 0)
0a1b45a2 11134 return false;
6e0b88f1 11135 else if (ret == 1)
8b127cbc 11136 flinfo->indices[symndx] = indx;
6e0b88f1
AM
11137 else
11138 abort ();
bcacc0f5
AM
11139 }
11140 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 11141 = flinfo->indices[symndx];
bcacc0f5
AM
11142 }
11143 }
11144
c152c796 11145 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 11146 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
11147 continue;
11148
11149 if ((o->flags & SEC_LINKER_CREATED) != 0)
11150 {
11151 /* Section was created by _bfd_elf_link_create_dynamic_sections
11152 or somesuch. */
11153 continue;
11154 }
11155
11156 /* Get the contents of the section. They have been cached by a
11157 relaxation routine. Note that o is a section in an input
11158 file, so the contents field will not have been set by any of
11159 the routines which work on output files. */
11160 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
11161 {
11162 contents = elf_section_data (o)->this_hdr.contents;
11163 if (bed->caches_rawsize
11164 && o->rawsize != 0
11165 && o->rawsize < o->size)
11166 {
11167 memcpy (flinfo->contents, contents, o->rawsize);
11168 contents = flinfo->contents;
11169 }
11170 }
c152c796
AM
11171 else
11172 {
8b127cbc 11173 contents = flinfo->contents;
4a114e3e 11174 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
0a1b45a2 11175 return false;
c152c796
AM
11176 }
11177
11178 if ((o->flags & SEC_RELOC) != 0)
11179 {
11180 Elf_Internal_Rela *internal_relocs;
0f02bbd9 11181 Elf_Internal_Rela *rel, *relend;
0f02bbd9 11182 int action_discarded;
ece5ef60 11183 int ret;
c152c796
AM
11184
11185 /* Get the swapped relocs. */
11186 internal_relocs
8b127cbc 11187 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
0a1b45a2 11188 flinfo->internal_relocs, false);
c152c796
AM
11189 if (internal_relocs == NULL
11190 && o->reloc_count > 0)
0a1b45a2 11191 return false;
c152c796 11192
310fd250
L
11193 /* We need to reverse-copy input .ctors/.dtors sections if
11194 they are placed in .init_array/.finit_array for output. */
11195 if (o->size > address_size
3f3328b8 11196 && ((startswith (o->name, ".ctors")
310fd250
L
11197 && strcmp (o->output_section->name,
11198 ".init_array") == 0)
3f3328b8 11199 || (startswith (o->name, ".dtors")
310fd250
L
11200 && strcmp (o->output_section->name,
11201 ".fini_array") == 0))
11202 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 11203 {
056bafd4
MR
11204 if (o->size * bed->s->int_rels_per_ext_rel
11205 != o->reloc_count * address_size)
310fd250 11206 {
4eca0228 11207 _bfd_error_handler
695344c0 11208 /* xgettext:c-format */
871b3ab2 11209 (_("error: %pB: size of section %pA is not "
310fd250
L
11210 "multiple of address size"),
11211 input_bfd, o);
8c6716e5 11212 bfd_set_error (bfd_error_bad_value);
0a1b45a2 11213 return false;
310fd250
L
11214 }
11215 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
11216 }
11217
0f02bbd9 11218 action_discarded = -1;
c152c796 11219 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
11220 action_discarded = (*bed->action_discarded) (o);
11221
11222 /* Run through the relocs evaluating complex reloc symbols and
11223 looking for relocs against symbols from discarded sections
11224 or section symbols from removed link-once sections.
11225 Complain about relocs against discarded sections. Zero
11226 relocs against removed link-once sections. */
11227
11228 rel = internal_relocs;
056bafd4 11229 relend = rel + o->reloc_count;
0f02bbd9 11230 for ( ; rel < relend; rel++)
c152c796 11231 {
0f02bbd9
AM
11232 unsigned long r_symndx = rel->r_info >> r_sym_shift;
11233 unsigned int s_type;
11234 asection **ps, *sec;
11235 struct elf_link_hash_entry *h = NULL;
11236 const char *sym_name;
c152c796 11237
0f02bbd9
AM
11238 if (r_symndx == STN_UNDEF)
11239 continue;
c152c796 11240
0f02bbd9
AM
11241 if (r_symndx >= locsymcount
11242 || (elf_bad_symtab (input_bfd)
8b127cbc 11243 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
11244 {
11245 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 11246
0f02bbd9
AM
11247 /* Badly formatted input files can contain relocs that
11248 reference non-existant symbols. Check here so that
11249 we do not seg fault. */
11250 if (h == NULL)
c152c796 11251 {
4eca0228 11252 _bfd_error_handler
695344c0 11253 /* xgettext:c-format */
2dcf00ce 11254 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
0f02bbd9 11255 "that references a non-existent global symbol"),
2dcf00ce 11256 input_bfd, (uint64_t) rel->r_info, o);
0f02bbd9 11257 bfd_set_error (bfd_error_bad_value);
0a1b45a2 11258 return false;
0f02bbd9 11259 }
3b36f7e6 11260
0f02bbd9
AM
11261 while (h->root.type == bfd_link_hash_indirect
11262 || h->root.type == bfd_link_hash_warning)
11263 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 11264
0f02bbd9 11265 s_type = h->type;
cdd3575c 11266
9e2dec47 11267 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
11268 mark the symbol as undefined. Note that the
11269 linker may attach linker created dynamic sections
11270 to the plugin bfd. Symbols defined in linker
11271 created sections are not plugin symbols. */
bc4e12de 11272 if ((h->root.non_ir_ref_regular
4070765b 11273 || h->root.non_ir_ref_dynamic)
9e2dec47
L
11274 && (h->root.type == bfd_link_hash_defined
11275 || h->root.type == bfd_link_hash_defweak)
11276 && (h->root.u.def.section->flags
11277 & SEC_LINKER_CREATED) == 0
11278 && h->root.u.def.section->owner != NULL
11279 && (h->root.u.def.section->owner->flags
11280 & BFD_PLUGIN) != 0)
11281 {
11282 h->root.type = bfd_link_hash_undefined;
11283 h->root.u.undef.abfd = h->root.u.def.section->owner;
11284 }
11285
0f02bbd9
AM
11286 ps = NULL;
11287 if (h->root.type == bfd_link_hash_defined
11288 || h->root.type == bfd_link_hash_defweak)
11289 ps = &h->root.u.def.section;
11290
11291 sym_name = h->root.root.string;
11292 }
11293 else
11294 {
11295 Elf_Internal_Sym *sym = isymbuf + r_symndx;
11296
11297 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 11298 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
11299 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11300 sym, *ps);
11301 }
c152c796 11302
c301e700 11303 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 11304 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
11305 {
11306 bfd_vma val;
11307 bfd_vma dot = (rel->r_offset
11308 + o->output_offset + o->output_section->vma);
11309#ifdef DEBUG
11310 printf ("Encountered a complex symbol!");
11311 printf (" (input_bfd %s, section %s, reloc %ld\n",
765cf5f6 11312 bfd_get_filename (input_bfd), o->name,
9ccb8af9 11313 (long) (rel - internal_relocs));
0f02bbd9
AM
11314 printf (" symbol: idx %8.8lx, name %s\n",
11315 r_symndx, sym_name);
11316 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11317 (unsigned long) rel->r_info,
11318 (unsigned long) rel->r_offset);
11319#endif
8b127cbc 11320 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9 11321 isymbuf, locsymcount, s_type == STT_SRELC))
0a1b45a2 11322 return false;
0f02bbd9
AM
11323
11324 /* Symbol evaluated OK. Update to absolute value. */
11325 set_symbol_value (input_bfd, isymbuf, locsymcount,
11326 r_symndx, val);
11327 continue;
11328 }
11329
11330 if (action_discarded != -1 && ps != NULL)
11331 {
cdd3575c
AM
11332 /* Complain if the definition comes from a
11333 discarded section. */
dbaa2011 11334 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 11335 {
cf35638d 11336 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 11337 if (action_discarded & COMPLAIN)
8b127cbc 11338 (*flinfo->info->callbacks->einfo)
695344c0 11339 /* xgettext:c-format */
871b3ab2
AM
11340 (_("%X`%s' referenced in section `%pA' of %pB: "
11341 "defined in discarded section `%pA' of %pB\n"),
e1fffbe6 11342 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 11343
87e5235d 11344 /* Try to do the best we can to support buggy old
e0ae6d6f 11345 versions of gcc. Pretend that the symbol is
87e5235d
AM
11346 really defined in the kept linkonce section.
11347 FIXME: This is quite broken. Modifying the
11348 symbol here means we will be changing all later
e0ae6d6f 11349 uses of the symbol, not just in this section. */
0f02bbd9 11350 if (action_discarded & PRETEND)
87e5235d 11351 {
01b3c8ab
L
11352 asection *kept;
11353
c0f00686 11354 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 11355 flinfo->info);
01b3c8ab 11356 if (kept != NULL)
87e5235d
AM
11357 {
11358 *ps = kept;
11359 continue;
11360 }
11361 }
c152c796
AM
11362 }
11363 }
11364 }
11365
11366 /* Relocate the section by invoking a back end routine.
11367
11368 The back end routine is responsible for adjusting the
11369 section contents as necessary, and (if using Rela relocs
11370 and generating a relocatable output file) adjusting the
11371 reloc addend as necessary.
11372
11373 The back end routine does not have to worry about setting
11374 the reloc address or the reloc symbol index.
11375
11376 The back end routine is given a pointer to the swapped in
11377 internal symbols, and can access the hash table entries
11378 for the external symbols via elf_sym_hashes (input_bfd).
11379
11380 When generating relocatable output, the back end routine
11381 must handle STB_LOCAL/STT_SECTION symbols specially. The
11382 output symbol is going to be a section symbol
11383 corresponding to the output section, which will require
11384 the addend to be adjusted. */
11385
8b127cbc 11386 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
11387 input_bfd, o, contents,
11388 internal_relocs,
11389 isymbuf,
8b127cbc 11390 flinfo->sections);
ece5ef60 11391 if (!ret)
0a1b45a2 11392 return false;
c152c796 11393
ece5ef60 11394 if (ret == 2
0e1862bb 11395 || bfd_link_relocatable (flinfo->info)
8b127cbc 11396 || flinfo->info->emitrelocations)
c152c796
AM
11397 {
11398 Elf_Internal_Rela *irela;
d4730f92 11399 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
11400 bfd_vma last_offset;
11401 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
11402 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11403 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 11404 unsigned int next_erel;
0a1b45a2 11405 bool rela_normal;
d4730f92 11406 struct bfd_elf_section_data *esdi, *esdo;
c152c796 11407
d4730f92
BS
11408 esdi = elf_section_data (o);
11409 esdo = elf_section_data (o->output_section);
0a1b45a2 11410 rela_normal = false;
c152c796
AM
11411
11412 /* Adjust the reloc addresses and symbol indices. */
11413
11414 irela = internal_relocs;
056bafd4 11415 irelaend = irela + o->reloc_count;
d4730f92
BS
11416 rel_hash = esdo->rel.hashes + esdo->rel.count;
11417 /* We start processing the REL relocs, if any. When we reach
11418 IRELAMID in the loop, we switch to the RELA relocs. */
11419 irelamid = irela;
11420 if (esdi->rel.hdr != NULL)
11421 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11422 * bed->s->int_rels_per_ext_rel);
eac338cf 11423 rel_hash_list = rel_hash;
d4730f92 11424 rela_hash_list = NULL;
c152c796 11425 last_offset = o->output_offset;
0e1862bb 11426 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11427 last_offset += o->output_section->vma;
11428 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11429 {
11430 unsigned long r_symndx;
11431 asection *sec;
11432 Elf_Internal_Sym sym;
11433
11434 if (next_erel == bed->s->int_rels_per_ext_rel)
11435 {
11436 rel_hash++;
11437 next_erel = 0;
11438 }
11439
d4730f92
BS
11440 if (irela == irelamid)
11441 {
11442 rel_hash = esdo->rela.hashes + esdo->rela.count;
11443 rela_hash_list = rel_hash;
11444 rela_normal = bed->rela_normal;
11445 }
11446
c152c796 11447 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 11448 flinfo->info, o,
c152c796
AM
11449 irela->r_offset);
11450 if (irela->r_offset >= (bfd_vma) -2)
11451 {
11452 /* This is a reloc for a deleted entry or somesuch.
11453 Turn it into an R_*_NONE reloc, at the same
11454 offset as the last reloc. elf_eh_frame.c and
e460dd0d 11455 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
11456 being ordered. */
11457 irela->r_offset = last_offset;
11458 irela->r_info = 0;
11459 irela->r_addend = 0;
11460 continue;
11461 }
11462
11463 irela->r_offset += o->output_offset;
11464
11465 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 11466 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11467 irela->r_offset += o->output_section->vma;
11468
11469 last_offset = irela->r_offset;
11470
11471 r_symndx = irela->r_info >> r_sym_shift;
11472 if (r_symndx == STN_UNDEF)
11473 continue;
11474
11475 if (r_symndx >= locsymcount
11476 || (elf_bad_symtab (input_bfd)
8b127cbc 11477 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
11478 {
11479 struct elf_link_hash_entry *rh;
11480 unsigned long indx;
11481
11482 /* This is a reloc against a global symbol. We
11483 have not yet output all the local symbols, so
11484 we do not know the symbol index of any global
11485 symbol. We set the rel_hash entry for this
11486 reloc to point to the global hash table entry
11487 for this symbol. The symbol index is then
ee75fd95 11488 set at the end of bfd_elf_final_link. */
c152c796
AM
11489 indx = r_symndx - extsymoff;
11490 rh = elf_sym_hashes (input_bfd)[indx];
11491 while (rh->root.type == bfd_link_hash_indirect
11492 || rh->root.type == bfd_link_hash_warning)
11493 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11494
11495 /* Setting the index to -2 tells
11496 elf_link_output_extsym that this symbol is
11497 used by a reloc. */
11498 BFD_ASSERT (rh->indx < 0);
11499 rh->indx = -2;
c152c796
AM
11500 *rel_hash = rh;
11501
11502 continue;
11503 }
11504
11505 /* This is a reloc against a local symbol. */
11506
11507 *rel_hash = NULL;
11508 sym = isymbuf[r_symndx];
8b127cbc 11509 sec = flinfo->sections[r_symndx];
c152c796
AM
11510 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11511 {
11512 /* I suppose the backend ought to fill in the
11513 section of any STT_SECTION symbol against a
6a8d1586 11514 processor specific section. */
cf35638d 11515 r_symndx = STN_UNDEF;
6a8d1586
AM
11516 if (bfd_is_abs_section (sec))
11517 ;
c152c796
AM
11518 else if (sec == NULL || sec->owner == NULL)
11519 {
11520 bfd_set_error (bfd_error_bad_value);
0a1b45a2 11521 return false;
c152c796
AM
11522 }
11523 else
11524 {
6a8d1586
AM
11525 asection *osec = sec->output_section;
11526
11527 /* If we have discarded a section, the output
11528 section will be the absolute section. In
ab96bf03
AM
11529 case of discarded SEC_MERGE sections, use
11530 the kept section. relocate_section should
11531 have already handled discarded linkonce
11532 sections. */
6a8d1586
AM
11533 if (bfd_is_abs_section (osec)
11534 && sec->kept_section != NULL
11535 && sec->kept_section->output_section != NULL)
11536 {
11537 osec = sec->kept_section->output_section;
11538 irela->r_addend -= osec->vma;
11539 }
11540
11541 if (!bfd_is_abs_section (osec))
11542 {
11543 r_symndx = osec->target_index;
cf35638d 11544 if (r_symndx == STN_UNDEF)
74541ad4 11545 {
051d833a
AM
11546 irela->r_addend += osec->vma;
11547 osec = _bfd_nearby_section (output_bfd, osec,
11548 osec->vma);
11549 irela->r_addend -= osec->vma;
11550 r_symndx = osec->target_index;
74541ad4 11551 }
6a8d1586 11552 }
c152c796
AM
11553 }
11554
11555 /* Adjust the addend according to where the
11556 section winds up in the output section. */
11557 if (rela_normal)
11558 irela->r_addend += sec->output_offset;
11559 }
11560 else
11561 {
8b127cbc 11562 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
11563 {
11564 unsigned long shlink;
11565 const char *name;
11566 asection *osec;
6e0b88f1 11567 long indx;
c152c796 11568
8b127cbc 11569 if (flinfo->info->strip == strip_all)
c152c796
AM
11570 {
11571 /* You can't do ld -r -s. */
11572 bfd_set_error (bfd_error_invalid_operation);
0a1b45a2 11573 return false;
c152c796
AM
11574 }
11575
11576 /* This symbol was skipped earlier, but
11577 since it is needed by a reloc, we
11578 must output it now. */
11579 shlink = symtab_hdr->sh_link;
11580 name = (bfd_elf_string_from_elf_section
11581 (input_bfd, shlink, sym.st_name));
11582 if (name == NULL)
0a1b45a2 11583 return false;
c152c796
AM
11584
11585 osec = sec->output_section;
11586 sym.st_shndx =
11587 _bfd_elf_section_from_bfd_section (output_bfd,
11588 osec);
11589 if (sym.st_shndx == SHN_BAD)
0a1b45a2 11590 return false;
c152c796
AM
11591
11592 sym.st_value += sec->output_offset;
0e1862bb 11593 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11594 {
11595 sym.st_value += osec->vma;
11596 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11597 {
102def4d
AM
11598 struct elf_link_hash_table *htab
11599 = elf_hash_table (flinfo->info);
11600
c152c796
AM
11601 /* STT_TLS symbols are relative to PT_TLS
11602 segment base. */
102def4d
AM
11603 if (htab->tls_sec != NULL)
11604 sym.st_value -= htab->tls_sec->vma;
11605 else
11606 sym.st_info
11607 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11608 STT_NOTYPE);
c152c796
AM
11609 }
11610 }
11611
6e0b88f1 11612 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
11613 ret = elf_link_output_symstrtab (flinfo, name,
11614 &sym, sec,
11615 NULL);
6e0b88f1 11616 if (ret == 0)
0a1b45a2 11617 return false;
6e0b88f1 11618 else if (ret == 1)
8b127cbc 11619 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
11620 else
11621 abort ();
c152c796
AM
11622 }
11623
8b127cbc 11624 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
11625 }
11626
11627 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11628 | (irela->r_info & r_type_mask));
11629 }
11630
11631 /* Swap out the relocs. */
d4730f92
BS
11632 input_rel_hdr = esdi->rel.hdr;
11633 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 11634 {
d4730f92
BS
11635 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11636 input_rel_hdr,
11637 internal_relocs,
11638 rel_hash_list))
0a1b45a2 11639 return false;
c152c796
AM
11640 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11641 * bed->s->int_rels_per_ext_rel);
eac338cf 11642 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
11643 }
11644
11645 input_rela_hdr = esdi->rela.hdr;
11646 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11647 {
eac338cf 11648 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 11649 input_rela_hdr,
eac338cf 11650 internal_relocs,
d4730f92 11651 rela_hash_list))
0a1b45a2 11652 return false;
c152c796
AM
11653 }
11654 }
11655 }
11656
11657 /* Write out the modified section contents. */
11658 if (bed->elf_backend_write_section
8b127cbc 11659 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 11660 contents))
c152c796
AM
11661 {
11662 /* Section written out. */
11663 }
11664 else switch (o->sec_info_type)
11665 {
dbaa2011 11666 case SEC_INFO_TYPE_STABS:
c152c796
AM
11667 if (! (_bfd_write_section_stabs
11668 (output_bfd,
8b127cbc 11669 &elf_hash_table (flinfo->info)->stab_info,
c152c796 11670 o, &elf_section_data (o)->sec_info, contents)))
0a1b45a2 11671 return false;
c152c796 11672 break;
dbaa2011 11673 case SEC_INFO_TYPE_MERGE:
c152c796
AM
11674 if (! _bfd_write_merged_section (output_bfd, o,
11675 elf_section_data (o)->sec_info))
0a1b45a2 11676 return false;
c152c796 11677 break;
dbaa2011 11678 case SEC_INFO_TYPE_EH_FRAME:
c152c796 11679 {
8b127cbc 11680 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796 11681 o, contents))
0a1b45a2 11682 return false;
c152c796
AM
11683 }
11684 break;
2f0c68f2
CM
11685 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11686 {
11687 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11688 flinfo->info,
11689 o, contents))
0a1b45a2 11690 return false;
2f0c68f2
CM
11691 }
11692 break;
c152c796
AM
11693 default:
11694 {
310fd250
L
11695 if (! (o->flags & SEC_EXCLUDE))
11696 {
11697 file_ptr offset = (file_ptr) o->output_offset;
11698 bfd_size_type todo = o->size;
37b01f6a 11699
bb294208 11700 offset *= bfd_octets_per_byte (output_bfd, o);
37b01f6a 11701
310fd250
L
11702 if ((o->flags & SEC_ELF_REVERSE_COPY))
11703 {
11704 /* Reverse-copy input section to output. */
11705 do
11706 {
11707 todo -= address_size;
11708 if (! bfd_set_section_contents (output_bfd,
11709 o->output_section,
11710 contents + todo,
11711 offset,
11712 address_size))
0a1b45a2 11713 return false;
310fd250
L
11714 if (todo == 0)
11715 break;
11716 offset += address_size;
11717 }
11718 while (1);
11719 }
11720 else if (! bfd_set_section_contents (output_bfd,
11721 o->output_section,
11722 contents,
11723 offset, todo))
0a1b45a2 11724 return false;
310fd250 11725 }
c152c796
AM
11726 }
11727 break;
11728 }
11729 }
11730
0a1b45a2 11731 return true;
c152c796
AM
11732}
11733
11734/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 11735 requested by the linker, and does not come from any input file. This
c152c796
AM
11736 is used to build constructor and destructor tables when linking
11737 with -Ur. */
11738
0a1b45a2 11739static bool
c152c796
AM
11740elf_reloc_link_order (bfd *output_bfd,
11741 struct bfd_link_info *info,
11742 asection *output_section,
11743 struct bfd_link_order *link_order)
11744{
11745 reloc_howto_type *howto;
11746 long indx;
11747 bfd_vma offset;
11748 bfd_vma addend;
d4730f92 11749 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
11750 struct elf_link_hash_entry **rel_hash_ptr;
11751 Elf_Internal_Shdr *rel_hdr;
11752 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11753 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11754 bfd_byte *erel;
11755 unsigned int i;
d4730f92 11756 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
11757
11758 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11759 if (howto == NULL)
11760 {
11761 bfd_set_error (bfd_error_bad_value);
0a1b45a2 11762 return false;
c152c796
AM
11763 }
11764
11765 addend = link_order->u.reloc.p->addend;
11766
d4730f92
BS
11767 if (esdo->rel.hdr)
11768 reldata = &esdo->rel;
11769 else if (esdo->rela.hdr)
11770 reldata = &esdo->rela;
11771 else
11772 {
11773 reldata = NULL;
11774 BFD_ASSERT (0);
11775 }
11776
c152c796 11777 /* Figure out the symbol index. */
d4730f92 11778 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
11779 if (link_order->type == bfd_section_reloc_link_order)
11780 {
11781 indx = link_order->u.reloc.p->u.section->target_index;
11782 BFD_ASSERT (indx != 0);
11783 *rel_hash_ptr = NULL;
11784 }
11785 else
11786 {
11787 struct elf_link_hash_entry *h;
11788
11789 /* Treat a reloc against a defined symbol as though it were
11790 actually against the section. */
11791 h = ((struct elf_link_hash_entry *)
11792 bfd_wrapped_link_hash_lookup (output_bfd, info,
11793 link_order->u.reloc.p->u.name,
0a1b45a2 11794 false, false, true));
c152c796
AM
11795 if (h != NULL
11796 && (h->root.type == bfd_link_hash_defined
11797 || h->root.type == bfd_link_hash_defweak))
11798 {
11799 asection *section;
11800
11801 section = h->root.u.def.section;
11802 indx = section->output_section->target_index;
11803 *rel_hash_ptr = NULL;
11804 /* It seems that we ought to add the symbol value to the
11805 addend here, but in practice it has already been added
11806 because it was passed to constructor_callback. */
11807 addend += section->output_section->vma + section->output_offset;
11808 }
11809 else if (h != NULL)
11810 {
11811 /* Setting the index to -2 tells elf_link_output_extsym that
11812 this symbol is used by a reloc. */
11813 h->indx = -2;
11814 *rel_hash_ptr = h;
11815 indx = 0;
11816 }
11817 else
11818 {
1a72702b
AM
11819 (*info->callbacks->unattached_reloc)
11820 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
c152c796
AM
11821 indx = 0;
11822 }
11823 }
11824
11825 /* If this is an inplace reloc, we must write the addend into the
11826 object file. */
11827 if (howto->partial_inplace && addend != 0)
11828 {
11829 bfd_size_type size;
11830 bfd_reloc_status_type rstat;
11831 bfd_byte *buf;
0a1b45a2 11832 bool ok;
c152c796 11833 const char *sym_name;
bb294208 11834 bfd_size_type octets;
c152c796 11835
a50b1753
NC
11836 size = (bfd_size_type) bfd_get_reloc_size (howto);
11837 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 11838 if (buf == NULL && size != 0)
0a1b45a2 11839 return false;
c152c796
AM
11840 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11841 switch (rstat)
11842 {
11843 case bfd_reloc_ok:
11844 break;
11845
11846 default:
11847 case bfd_reloc_outofrange:
11848 abort ();
11849
11850 case bfd_reloc_overflow:
11851 if (link_order->type == bfd_section_reloc_link_order)
fd361982 11852 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
c152c796
AM
11853 else
11854 sym_name = link_order->u.reloc.p->u.name;
1a72702b
AM
11855 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11856 howto->name, addend, NULL, NULL,
11857 (bfd_vma) 0);
c152c796
AM
11858 break;
11859 }
37b01f6a 11860
bb294208
AM
11861 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
11862 output_section);
c152c796 11863 ok = bfd_set_section_contents (output_bfd, output_section, buf,
bb294208 11864 octets, size);
c152c796
AM
11865 free (buf);
11866 if (! ok)
0a1b45a2 11867 return false;
c152c796
AM
11868 }
11869
11870 /* The address of a reloc is relative to the section in a
11871 relocatable file, and is a virtual address in an executable
11872 file. */
11873 offset = link_order->offset;
0e1862bb 11874 if (! bfd_link_relocatable (info))
c152c796
AM
11875 offset += output_section->vma;
11876
11877 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11878 {
11879 irel[i].r_offset = offset;
11880 irel[i].r_info = 0;
11881 irel[i].r_addend = 0;
11882 }
11883 if (bed->s->arch_size == 32)
11884 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11885 else
11886 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11887
d4730f92 11888 rel_hdr = reldata->hdr;
c152c796
AM
11889 erel = rel_hdr->contents;
11890 if (rel_hdr->sh_type == SHT_REL)
11891 {
d4730f92 11892 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
11893 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11894 }
11895 else
11896 {
11897 irel[0].r_addend = addend;
d4730f92 11898 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
11899 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11900 }
11901
d4730f92 11902 ++reldata->count;
c152c796 11903
0a1b45a2 11904 return true;
c152c796
AM
11905}
11906
76359541
TP
11907/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11908 Returns TRUE upon success, FALSE otherwise. */
11909
0a1b45a2 11910static bool
76359541
TP
11911elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11912{
0a1b45a2 11913 bool ret = false;
76359541
TP
11914 bfd *implib_bfd;
11915 const struct elf_backend_data *bed;
11916 flagword flags;
11917 enum bfd_architecture arch;
11918 unsigned int mach;
11919 asymbol **sympp = NULL;
11920 long symsize;
11921 long symcount;
11922 long src_count;
11923 elf_symbol_type *osymbuf;
446f7ed5 11924 size_t amt;
76359541
TP
11925
11926 implib_bfd = info->out_implib_bfd;
11927 bed = get_elf_backend_data (abfd);
11928
11929 if (!bfd_set_format (implib_bfd, bfd_object))
0a1b45a2 11930 return false;
76359541 11931
046734ff 11932 /* Use flag from executable but make it a relocatable object. */
76359541
TP
11933 flags = bfd_get_file_flags (abfd);
11934 flags &= ~HAS_RELOC;
11935 if (!bfd_set_start_address (implib_bfd, 0)
046734ff 11936 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
0a1b45a2 11937 return false;
76359541
TP
11938
11939 /* Copy architecture of output file to import library file. */
11940 arch = bfd_get_arch (abfd);
11941 mach = bfd_get_mach (abfd);
11942 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11943 && (abfd->target_defaulted
11944 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
0a1b45a2 11945 return false;
76359541
TP
11946
11947 /* Get symbol table size. */
11948 symsize = bfd_get_symtab_upper_bound (abfd);
11949 if (symsize < 0)
0a1b45a2 11950 return false;
76359541
TP
11951
11952 /* Read in the symbol table. */
ec9bd0a2
AM
11953 sympp = (asymbol **) bfd_malloc (symsize);
11954 if (sympp == NULL)
0a1b45a2 11955 return false;
ec9bd0a2 11956
76359541
TP
11957 symcount = bfd_canonicalize_symtab (abfd, sympp);
11958 if (symcount < 0)
11959 goto free_sym_buf;
11960
11961 /* Allow the BFD backend to copy any private header data it
11962 understands from the output BFD to the import library BFD. */
11963 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11964 goto free_sym_buf;
11965
11966 /* Filter symbols to appear in the import library. */
11967 if (bed->elf_backend_filter_implib_symbols)
11968 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11969 symcount);
11970 else
11971 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11972 if (symcount == 0)
11973 {
5df1bc57 11974 bfd_set_error (bfd_error_no_symbols);
871b3ab2 11975 _bfd_error_handler (_("%pB: no symbol found for import library"),
4eca0228 11976 implib_bfd);
76359541
TP
11977 goto free_sym_buf;
11978 }
11979
11980
11981 /* Make symbols absolute. */
446f7ed5
AM
11982 amt = symcount * sizeof (*osymbuf);
11983 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
ec9bd0a2
AM
11984 if (osymbuf == NULL)
11985 goto free_sym_buf;
11986
76359541
TP
11987 for (src_count = 0; src_count < symcount; src_count++)
11988 {
11989 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11990 sizeof (*osymbuf));
11991 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11992 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11993 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11994 osymbuf[src_count].internal_elf_sym.st_value =
11995 osymbuf[src_count].symbol.value;
11996 sympp[src_count] = &osymbuf[src_count].symbol;
11997 }
11998
11999 bfd_set_symtab (implib_bfd, sympp, symcount);
12000
12001 /* Allow the BFD backend to copy any private data it understands
12002 from the output BFD to the import library BFD. This is done last
12003 to permit the routine to look at the filtered symbol table. */
12004 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
12005 goto free_sym_buf;
12006
12007 if (!bfd_close (implib_bfd))
12008 goto free_sym_buf;
12009
0a1b45a2 12010 ret = true;
76359541 12011
dc1e8a47 12012 free_sym_buf:
76359541
TP
12013 free (sympp);
12014 return ret;
12015}
12016
9f7c3e5e
AM
12017static void
12018elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
12019{
12020 asection *o;
12021
12022 if (flinfo->symstrtab != NULL)
ef10c3ac 12023 _bfd_elf_strtab_free (flinfo->symstrtab);
c9594989
AM
12024 free (flinfo->contents);
12025 free (flinfo->external_relocs);
12026 free (flinfo->internal_relocs);
12027 free (flinfo->external_syms);
12028 free (flinfo->locsym_shndx);
12029 free (flinfo->internal_syms);
12030 free (flinfo->indices);
12031 free (flinfo->sections);
12032 if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
9f7c3e5e
AM
12033 free (flinfo->symshndxbuf);
12034 for (o = obfd->sections; o != NULL; o = o->next)
12035 {
12036 struct bfd_elf_section_data *esdo = elf_section_data (o);
c9594989
AM
12037 free (esdo->rel.hashes);
12038 free (esdo->rela.hashes);
9f7c3e5e
AM
12039 }
12040}
0b52efa6 12041
c152c796
AM
12042/* Do the final step of an ELF link. */
12043
0a1b45a2 12044bool
c152c796
AM
12045bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
12046{
0a1b45a2
AM
12047 bool dynamic;
12048 bool emit_relocs;
c152c796 12049 bfd *dynobj;
8b127cbc 12050 struct elf_final_link_info flinfo;
91d6fa6a
NC
12051 asection *o;
12052 struct bfd_link_order *p;
12053 bfd *sub;
c152c796
AM
12054 bfd_size_type max_contents_size;
12055 bfd_size_type max_external_reloc_size;
12056 bfd_size_type max_internal_reloc_count;
12057 bfd_size_type max_sym_count;
12058 bfd_size_type max_sym_shndx_count;
c152c796
AM
12059 Elf_Internal_Sym elfsym;
12060 unsigned int i;
12061 Elf_Internal_Shdr *symtab_hdr;
12062 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
12063 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12064 struct elf_outext_info eoinfo;
0a1b45a2 12065 bool merged;
c152c796
AM
12066 size_t relativecount = 0;
12067 asection *reldyn = 0;
12068 bfd_size_type amt;
104d59d1
JM
12069 asection *attr_section = NULL;
12070 bfd_vma attr_size = 0;
12071 const char *std_attrs_section;
64f52338 12072 struct elf_link_hash_table *htab = elf_hash_table (info);
0a1b45a2
AM
12073 bool sections_removed;
12074 bool ret;
c152c796 12075
2cc15b10 12076 if (!is_elf_hash_table (&htab->root))
0a1b45a2 12077 return false;
c152c796 12078
0e1862bb 12079 if (bfd_link_pic (info))
c152c796
AM
12080 abfd->flags |= DYNAMIC;
12081
64f52338
AM
12082 dynamic = htab->dynamic_sections_created;
12083 dynobj = htab->dynobj;
c152c796 12084
0e1862bb 12085 emit_relocs = (bfd_link_relocatable (info)
a4676736 12086 || info->emitrelocations);
c152c796 12087
496afd17 12088 memset (&flinfo, 0, sizeof (flinfo));
8b127cbc
AM
12089 flinfo.info = info;
12090 flinfo.output_bfd = abfd;
ef10c3ac 12091 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 12092 if (flinfo.symstrtab == NULL)
0a1b45a2 12093 return false;
c152c796
AM
12094
12095 if (! dynamic)
12096 {
8b127cbc
AM
12097 flinfo.hash_sec = NULL;
12098 flinfo.symver_sec = NULL;
c152c796
AM
12099 }
12100 else
12101 {
3d4d4302 12102 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 12103 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 12104 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
12105 /* Note that it is OK if symver_sec is NULL. */
12106 }
12107
496afd17
L
12108 if (info->unique_symbol
12109 && !bfd_hash_table_init (&flinfo.local_hash_table,
12110 local_hash_newfunc,
12111 sizeof (struct local_hash_entry)))
0a1b45a2 12112 return false;
c152c796 12113
104d59d1
JM
12114 /* The object attributes have been merged. Remove the input
12115 sections from the link, and set the contents of the output
1ff6de03 12116 section. */
0a1b45a2 12117 sections_removed = false;
104d59d1
JM
12118 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
12119 for (o = abfd->sections; o != NULL; o = o->next)
12120 {
0a1b45a2 12121 bool remove_section = false;
b8a6ced7 12122
104d59d1
JM
12123 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
12124 || strcmp (o->name, ".gnu.attributes") == 0)
12125 {
12126 for (p = o->map_head.link_order; p != NULL; p = p->next)
12127 {
12128 asection *input_section;
12129
12130 if (p->type != bfd_indirect_link_order)
12131 continue;
12132 input_section = p->u.indirect.section;
12133 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12134 elf_link_input_bfd ignores this section. */
12135 input_section->flags &= ~SEC_HAS_CONTENTS;
12136 }
a0c8462f 12137
104d59d1 12138 attr_size = bfd_elf_obj_attr_size (abfd);
fd361982 12139 bfd_set_section_size (o, attr_size);
b8a6ced7
AM
12140 /* Skip this section later on. */
12141 o->map_head.link_order = NULL;
104d59d1 12142 if (attr_size)
b8a6ced7 12143 attr_section = o;
104d59d1 12144 else
0a1b45a2 12145 remove_section = true;
104d59d1 12146 }
6e5e9d58
AM
12147 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12148 {
12149 /* Remove empty group section from linker output. */
0a1b45a2 12150 remove_section = true;
b8a6ced7 12151 }
5270eddc 12152 if (remove_section)
b8a6ced7 12153 {
6e5e9d58
AM
12154 o->flags |= SEC_EXCLUDE;
12155 bfd_section_list_remove (abfd, o);
12156 abfd->section_count--;
0a1b45a2 12157 sections_removed = true;
6e5e9d58 12158 }
104d59d1 12159 }
4c6ee646
AM
12160 if (sections_removed)
12161 _bfd_fix_excluded_sec_syms (abfd, info);
104d59d1 12162
c152c796
AM
12163 /* Count up the number of relocations we will output for each output
12164 section, so that we know the sizes of the reloc sections. We
12165 also figure out some maximum sizes. */
12166 max_contents_size = 0;
12167 max_external_reloc_size = 0;
12168 max_internal_reloc_count = 0;
12169 max_sym_count = 0;
12170 max_sym_shndx_count = 0;
0a1b45a2 12171 merged = false;
c152c796
AM
12172 for (o = abfd->sections; o != NULL; o = o->next)
12173 {
12174 struct bfd_elf_section_data *esdo = elf_section_data (o);
12175 o->reloc_count = 0;
12176
8423293d 12177 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
12178 {
12179 unsigned int reloc_count = 0;
9eaff861 12180 unsigned int additional_reloc_count = 0;
c152c796 12181 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
12182
12183 if (p->type == bfd_section_reloc_link_order
12184 || p->type == bfd_symbol_reloc_link_order)
12185 reloc_count = 1;
12186 else if (p->type == bfd_indirect_link_order)
12187 {
12188 asection *sec;
12189
12190 sec = p->u.indirect.section;
c152c796
AM
12191
12192 /* Mark all sections which are to be included in the
12193 link. This will normally be every section. We need
12194 to do this so that we can identify any sections which
12195 the linker has decided to not include. */
0a1b45a2 12196 sec->linker_mark = true;
c152c796
AM
12197
12198 if (sec->flags & SEC_MERGE)
0a1b45a2 12199 merged = true;
c152c796 12200
eea6121a
AM
12201 if (sec->rawsize > max_contents_size)
12202 max_contents_size = sec->rawsize;
12203 if (sec->size > max_contents_size)
12204 max_contents_size = sec->size;
c152c796 12205
c152c796
AM
12206 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12207 && (sec->owner->flags & DYNAMIC) == 0)
12208 {
12209 size_t sym_count;
12210
a961cdd5
AM
12211 /* We are interested in just local symbols, not all
12212 symbols. */
c152c796
AM
12213 if (elf_bad_symtab (sec->owner))
12214 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12215 / bed->s->sizeof_sym);
12216 else
12217 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12218
12219 if (sym_count > max_sym_count)
12220 max_sym_count = sym_count;
12221
12222 if (sym_count > max_sym_shndx_count
6a40cf0c 12223 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
12224 max_sym_shndx_count = sym_count;
12225
a961cdd5
AM
12226 if (esdo->this_hdr.sh_type == SHT_REL
12227 || esdo->this_hdr.sh_type == SHT_RELA)
12228 /* Some backends use reloc_count in relocation sections
12229 to count particular types of relocs. Of course,
12230 reloc sections themselves can't have relocations. */
12231 ;
12232 else if (emit_relocs)
12233 {
12234 reloc_count = sec->reloc_count;
12235 if (bed->elf_backend_count_additional_relocs)
12236 {
12237 int c;
12238 c = (*bed->elf_backend_count_additional_relocs) (sec);
12239 additional_reloc_count += c;
12240 }
12241 }
12242 else if (bed->elf_backend_count_relocs)
12243 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12244
12245 esdi = elf_section_data (sec);
12246
c152c796
AM
12247 if ((sec->flags & SEC_RELOC) != 0)
12248 {
d4730f92 12249 size_t ext_size = 0;
c152c796 12250
d4730f92
BS
12251 if (esdi->rel.hdr != NULL)
12252 ext_size = esdi->rel.hdr->sh_size;
12253 if (esdi->rela.hdr != NULL)
12254 ext_size += esdi->rela.hdr->sh_size;
7326c758 12255
c152c796
AM
12256 if (ext_size > max_external_reloc_size)
12257 max_external_reloc_size = ext_size;
12258 if (sec->reloc_count > max_internal_reloc_count)
12259 max_internal_reloc_count = sec->reloc_count;
12260 }
12261 }
12262 }
12263
12264 if (reloc_count == 0)
12265 continue;
12266
9eaff861 12267 reloc_count += additional_reloc_count;
c152c796
AM
12268 o->reloc_count += reloc_count;
12269
0e1862bb 12270 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 12271 {
d4730f92 12272 if (esdi->rel.hdr)
9eaff861 12273 {
491d01d3 12274 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
9eaff861
AO
12275 esdo->rel.count += additional_reloc_count;
12276 }
d4730f92 12277 if (esdi->rela.hdr)
9eaff861 12278 {
491d01d3 12279 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
9eaff861
AO
12280 esdo->rela.count += additional_reloc_count;
12281 }
d4730f92
BS
12282 }
12283 else
12284 {
12285 if (o->use_rela_p)
12286 esdo->rela.count += reloc_count;
2c2b4ed4 12287 else
d4730f92 12288 esdo->rel.count += reloc_count;
c152c796 12289 }
c152c796
AM
12290 }
12291
9eaff861 12292 if (o->reloc_count > 0)
c152c796
AM
12293 o->flags |= SEC_RELOC;
12294 else
12295 {
12296 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12297 set it (this is probably a bug) and if it is set
12298 assign_section_numbers will create a reloc section. */
12299 o->flags &=~ SEC_RELOC;
12300 }
12301
12302 /* If the SEC_ALLOC flag is not set, force the section VMA to
12303 zero. This is done in elf_fake_sections as well, but forcing
12304 the VMA to 0 here will ensure that relocs against these
12305 sections are handled correctly. */
12306 if ((o->flags & SEC_ALLOC) == 0
12307 && ! o->user_set_vma)
12308 o->vma = 0;
12309 }
12310
0e1862bb 12311 if (! bfd_link_relocatable (info) && merged)
64f52338 12312 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
c152c796
AM
12313
12314 /* Figure out the file positions for everything but the symbol table
12315 and the relocs. We set symcount to force assign_section_numbers
12316 to create a symbol table. */
ed48ec2e 12317 abfd->symcount = info->strip != strip_all || emit_relocs;
c152c796
AM
12318 BFD_ASSERT (! abfd->output_has_begun);
12319 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12320 goto error_return;
12321
ee75fd95 12322 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
12323 for (o = abfd->sections; o != NULL; o = o->next)
12324 {
d4730f92 12325 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
12326 if ((o->flags & SEC_RELOC) != 0)
12327 {
d4730f92 12328 if (esdo->rel.hdr
9eaff861 12329 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
12330 goto error_return;
12331
d4730f92 12332 if (esdo->rela.hdr
9eaff861 12333 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
12334 goto error_return;
12335 }
12336
48db3297
AM
12337 /* _bfd_elf_compute_section_file_positions makes temporary use
12338 of target_index. Reset it. */
12339 o->target_index = 0;
12340
c152c796
AM
12341 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12342 to count upwards while actually outputting the relocations. */
d4730f92
BS
12343 esdo->rel.count = 0;
12344 esdo->rela.count = 0;
0ce398f1 12345
1ff6de03
NA
12346 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12347 && !bfd_section_is_ctf (o))
0ce398f1
L
12348 {
12349 /* Cache the section contents so that they can be compressed
12350 later. Use bfd_malloc since it will be freed by
12351 bfd_compress_section_contents. */
12352 unsigned char *contents = esdo->this_hdr.contents;
12353 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
12354 abort ();
12355 contents
12356 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12357 if (contents == NULL)
12358 goto error_return;
12359 esdo->this_hdr.contents = contents;
12360 }
c152c796
AM
12361 }
12362
1ff6de03
NA
12363 /* We have now assigned file positions for all the sections except .symtab,
12364 .strtab, and non-loaded reloc and compressed debugging sections. We start
12365 the .symtab section at the current file position, and write directly to it.
12366 We build the .strtab section in memory. */
ed48ec2e 12367 abfd->symcount = 0;
c152c796
AM
12368 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12369 /* sh_name is set in prep_headers. */
12370 symtab_hdr->sh_type = SHT_SYMTAB;
12371 /* sh_flags, sh_addr and sh_size all start off zero. */
12372 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12373 /* sh_link is set in assign_section_numbers. */
12374 /* sh_info is set below. */
12375 /* sh_offset is set just below. */
72de5009 12376 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 12377
ef10c3ac
L
12378 if (max_sym_count < 20)
12379 max_sym_count = 20;
64f52338 12380 htab->strtabsize = max_sym_count;
ef10c3ac 12381 amt = max_sym_count * sizeof (struct elf_sym_strtab);
64f52338
AM
12382 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12383 if (htab->strtab == NULL)
c152c796 12384 goto error_return;
ef10c3ac
L
12385 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12386 flinfo.symshndxbuf
12387 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12388 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 12389
8539e4e8 12390 if (info->strip != strip_all || emit_relocs)
c152c796 12391 {
8539e4e8
AM
12392 file_ptr off = elf_next_file_pos (abfd);
12393
0a1b45a2 12394 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true);
8539e4e8
AM
12395
12396 /* Note that at this point elf_next_file_pos (abfd) is
12397 incorrect. We do not yet know the size of the .symtab section.
12398 We correct next_file_pos below, after we do know the size. */
12399
12400 /* Start writing out the symbol table. The first symbol is always a
12401 dummy symbol. */
c152c796
AM
12402 elfsym.st_value = 0;
12403 elfsym.st_size = 0;
12404 elfsym.st_info = 0;
12405 elfsym.st_other = 0;
12406 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 12407 elfsym.st_target_internal = 0;
ef10c3ac
L
12408 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12409 bfd_und_section_ptr, NULL) != 1)
c152c796 12410 goto error_return;
c152c796 12411
d1bcae83
L
12412 /* Output a symbol for each section if asked or they are used for
12413 relocs. These symbols usually have no names. We store the
12414 index of each one in the index field of the section, so that
12415 we can find it again when outputting relocs. */
c77cb2a0 12416
d1bcae83 12417 if (bfd_keep_unused_section_symbols (abfd) || emit_relocs)
c152c796 12418 {
0a1b45a2 12419 bool name_local_sections
d1bcae83
L
12420 = (bed->elf_backend_name_local_section_symbols
12421 && bed->elf_backend_name_local_section_symbols (abfd));
12422 const char *name = NULL;
12423
12424 elfsym.st_size = 0;
12425 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12426 elfsym.st_other = 0;
12427 elfsym.st_value = 0;
12428 elfsym.st_target_internal = 0;
12429 for (i = 1; i < elf_numsections (abfd); i++)
f0b5bb34 12430 {
d1bcae83
L
12431 o = bfd_section_from_elf_index (abfd, i);
12432 if (o != NULL)
12433 {
12434 o->target_index = bfd_get_symcount (abfd);
12435 elfsym.st_shndx = i;
12436 if (!bfd_link_relocatable (info))
12437 elfsym.st_value = o->vma;
12438 if (name_local_sections)
12439 name = o->name;
12440 if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
12441 NULL) != 1)
12442 goto error_return;
12443 }
f0b5bb34 12444 }
c152c796
AM
12445 }
12446 }
12447
3f1b17bb
MR
12448 /* On some targets like Irix 5 the symbol split between local and global
12449 ones recorded in the sh_info field needs to be done between section
12450 and all other symbols. */
12451 if (bed->elf_backend_elfsym_local_is_section
12452 && bed->elf_backend_elfsym_local_is_section (abfd))
12453 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12454
c152c796
AM
12455 /* Allocate some memory to hold information read in from the input
12456 files. */
12457 if (max_contents_size != 0)
12458 {
8b127cbc
AM
12459 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12460 if (flinfo.contents == NULL)
c152c796
AM
12461 goto error_return;
12462 }
12463
12464 if (max_external_reloc_size != 0)
12465 {
8b127cbc
AM
12466 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12467 if (flinfo.external_relocs == NULL)
c152c796
AM
12468 goto error_return;
12469 }
12470
12471 if (max_internal_reloc_count != 0)
12472 {
056bafd4 12473 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
8b127cbc
AM
12474 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12475 if (flinfo.internal_relocs == NULL)
c152c796
AM
12476 goto error_return;
12477 }
12478
12479 if (max_sym_count != 0)
12480 {
12481 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
12482 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12483 if (flinfo.external_syms == NULL)
c152c796
AM
12484 goto error_return;
12485
12486 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
12487 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12488 if (flinfo.internal_syms == NULL)
c152c796
AM
12489 goto error_return;
12490
12491 amt = max_sym_count * sizeof (long);
8b127cbc
AM
12492 flinfo.indices = (long int *) bfd_malloc (amt);
12493 if (flinfo.indices == NULL)
c152c796
AM
12494 goto error_return;
12495
12496 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
12497 flinfo.sections = (asection **) bfd_malloc (amt);
12498 if (flinfo.sections == NULL)
c152c796
AM
12499 goto error_return;
12500 }
12501
12502 if (max_sym_shndx_count != 0)
12503 {
12504 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
12505 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12506 if (flinfo.locsym_shndx == NULL)
c152c796
AM
12507 goto error_return;
12508 }
12509
64f52338 12510 if (htab->tls_sec)
c152c796 12511 {
66631823 12512 bfd_vma base, end = 0; /* Both bytes. */
c152c796
AM
12513 asection *sec;
12514
64f52338 12515 for (sec = htab->tls_sec;
c152c796
AM
12516 sec && (sec->flags & SEC_THREAD_LOCAL);
12517 sec = sec->next)
12518 {
3a800eb9 12519 bfd_size_type size = sec->size;
66631823 12520 unsigned int opb = bfd_octets_per_byte (abfd, sec);
c152c796 12521
3a800eb9
AM
12522 if (size == 0
12523 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 12524 {
91d6fa6a
NC
12525 struct bfd_link_order *ord = sec->map_tail.link_order;
12526
12527 if (ord != NULL)
66631823 12528 size = ord->offset * opb + ord->size;
c152c796 12529 }
66631823 12530 end = sec->vma + size / opb;
c152c796 12531 }
64f52338 12532 base = htab->tls_sec->vma;
7dc98aea
RO
12533 /* Only align end of TLS section if static TLS doesn't have special
12534 alignment requirements. */
12535 if (bed->static_tls_alignment == 1)
64f52338
AM
12536 end = align_power (end, htab->tls_sec->alignment_power);
12537 htab->tls_size = end - base;
c152c796
AM
12538 }
12539
2f0c68f2 12540 if (!_bfd_elf_fixup_eh_frame_hdr (info))
0a1b45a2 12541 return false;
2f0c68f2 12542
c152c796
AM
12543 /* Since ELF permits relocations to be against local symbols, we
12544 must have the local symbols available when we do the relocations.
12545 Since we would rather only read the local symbols once, and we
12546 would rather not keep them in memory, we handle all the
12547 relocations for a single input file at the same time.
12548
12549 Unfortunately, there is no way to know the total number of local
12550 symbols until we have seen all of them, and the local symbol
12551 indices precede the global symbol indices. This means that when
12552 we are generating relocatable output, and we see a reloc against
12553 a global symbol, we can not know the symbol index until we have
12554 finished examining all the local symbols to see which ones we are
12555 going to output. To deal with this, we keep the relocations in
12556 memory, and don't output them until the end of the link. This is
12557 an unfortunate waste of memory, but I don't see a good way around
12558 it. Fortunately, it only happens when performing a relocatable
12559 link, which is not the common case. FIXME: If keep_memory is set
12560 we could write the relocs out and then read them again; I don't
12561 know how bad the memory loss will be. */
12562
c72f2fb2 12563 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
0a1b45a2 12564 sub->output_has_begun = false;
c152c796
AM
12565 for (o = abfd->sections; o != NULL; o = o->next)
12566 {
8423293d 12567 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
12568 {
12569 if (p->type == bfd_indirect_link_order
12570 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12571 == bfd_target_elf_flavour)
12572 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12573 {
12574 if (! sub->output_has_begun)
12575 {
8b127cbc 12576 if (! elf_link_input_bfd (&flinfo, sub))
c152c796 12577 goto error_return;
0a1b45a2 12578 sub->output_has_begun = true;
c152c796
AM
12579 }
12580 }
12581 else if (p->type == bfd_section_reloc_link_order
12582 || p->type == bfd_symbol_reloc_link_order)
12583 {
12584 if (! elf_reloc_link_order (abfd, info, o, p))
12585 goto error_return;
12586 }
12587 else
12588 {
12589 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
12590 {
12591 if (p->type == bfd_indirect_link_order
12592 && (bfd_get_flavour (sub)
12593 == bfd_target_elf_flavour)
12594 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12595 != bed->s->elfclass))
12596 {
12597 const char *iclass, *oclass;
12598
aebf9be7 12599 switch (bed->s->elfclass)
351f65ca 12600 {
aebf9be7
NC
12601 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12602 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12603 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12604 default: abort ();
351f65ca 12605 }
aebf9be7
NC
12606
12607 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 12608 {
aebf9be7
NC
12609 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12610 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12611 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12612 default: abort ();
351f65ca
L
12613 }
12614
12615 bfd_set_error (bfd_error_wrong_format);
4eca0228 12616 _bfd_error_handler
695344c0 12617 /* xgettext:c-format */
871b3ab2 12618 (_("%pB: file class %s incompatible with %s"),
351f65ca
L
12619 sub, iclass, oclass);
12620 }
12621
12622 goto error_return;
12623 }
c152c796
AM
12624 }
12625 }
12626 }
12627
c0f00686
L
12628 /* Free symbol buffer if needed. */
12629 if (!info->reduce_memory_overheads)
12630 {
c72f2fb2 12631 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c9594989 12632 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
c0f00686
L
12633 {
12634 free (elf_tdata (sub)->symbuf);
12635 elf_tdata (sub)->symbuf = NULL;
12636 }
12637 }
12638
0a1b45a2 12639 ret = true;
496afd17 12640
c152c796
AM
12641 /* Output any global symbols that got converted to local in a
12642 version script or due to symbol visibility. We do this in a
12643 separate step since ELF requires all local symbols to appear
12644 prior to any global symbols. FIXME: We should only do this if
12645 some global symbols were, in fact, converted to become local.
12646 FIXME: Will this work correctly with the Irix 5 linker? */
0a1b45a2 12647 eoinfo.failed = false;
8b127cbc 12648 eoinfo.flinfo = &flinfo;
0a1b45a2
AM
12649 eoinfo.localsyms = true;
12650 eoinfo.file_sym_done = false;
7686d77d 12651 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796 12652 if (eoinfo.failed)
496afd17 12653 {
0a1b45a2 12654 ret = false;
496afd17
L
12655 goto return_local_hash_table;
12656 }
c152c796 12657
4e617b1e
PB
12658 /* If backend needs to output some local symbols not present in the hash
12659 table, do it now. */
8539e4e8
AM
12660 if (bed->elf_backend_output_arch_local_syms
12661 && (info->strip != strip_all || emit_relocs))
4e617b1e 12662 {
4e617b1e 12663 if (! ((*bed->elf_backend_output_arch_local_syms)
37bb890f 12664 (abfd, info, &flinfo, elf_link_output_symstrtab)))
496afd17 12665 {
0a1b45a2 12666 ret = false;
496afd17
L
12667 goto return_local_hash_table;
12668 }
4e617b1e
PB
12669 }
12670
c152c796
AM
12671 /* That wrote out all the local symbols. Finish up the symbol table
12672 with the global symbols. Even if we want to strip everything we
12673 can, we still need to deal with those global symbols that got
12674 converted to local in a version script. */
12675
12676 /* The sh_info field records the index of the first non local symbol. */
3f1b17bb
MR
12677 if (!symtab_hdr->sh_info)
12678 symtab_hdr->sh_info = bfd_get_symcount (abfd);
c152c796
AM
12679
12680 if (dynamic
64f52338
AM
12681 && htab->dynsym != NULL
12682 && htab->dynsym->output_section != bfd_abs_section_ptr)
c152c796
AM
12683 {
12684 Elf_Internal_Sym sym;
64f52338 12685 bfd_byte *dynsym = htab->dynsym->contents;
90ac2420 12686
64f52338
AM
12687 o = htab->dynsym->output_section;
12688 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
c152c796
AM
12689
12690 /* Write out the section symbols for the output sections. */
0e1862bb 12691 if (bfd_link_pic (info)
64f52338 12692 || htab->is_relocatable_executable)
c152c796
AM
12693 {
12694 asection *s;
12695
12696 sym.st_size = 0;
12697 sym.st_name = 0;
12698 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12699 sym.st_other = 0;
35fc36a8 12700 sym.st_target_internal = 0;
c152c796
AM
12701
12702 for (s = abfd->sections; s != NULL; s = s->next)
12703 {
12704 int indx;
12705 bfd_byte *dest;
12706 long dynindx;
12707
c152c796 12708 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
12709 if (dynindx <= 0)
12710 continue;
12711 indx = elf_section_data (s)->this_idx;
c152c796
AM
12712 BFD_ASSERT (indx > 0);
12713 sym.st_shndx = indx;
c0d5a53d 12714 if (! check_dynsym (abfd, &sym))
496afd17 12715 {
0a1b45a2 12716 ret = false;
496afd17
L
12717 goto return_local_hash_table;
12718 }
c152c796
AM
12719 sym.st_value = s->vma;
12720 dest = dynsym + dynindx * bed->s->sizeof_sym;
3d16b64e
NA
12721
12722 /* Inform the linker of the addition of this symbol. */
12723
12724 if (info->callbacks->ctf_new_dynsym)
12725 info->callbacks->ctf_new_dynsym (dynindx, &sym);
12726
c152c796
AM
12727 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12728 }
c152c796
AM
12729 }
12730
12731 /* Write out the local dynsyms. */
64f52338 12732 if (htab->dynlocal)
c152c796
AM
12733 {
12734 struct elf_link_local_dynamic_entry *e;
64f52338 12735 for (e = htab->dynlocal; e ; e = e->next)
c152c796
AM
12736 {
12737 asection *s;
12738 bfd_byte *dest;
12739
935bd1e0 12740 /* Copy the internal symbol and turn off visibility.
c152c796
AM
12741 Note that we saved a word of storage and overwrote
12742 the original st_name with the dynstr_index. */
12743 sym = e->isym;
935bd1e0 12744 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
4d68fd75 12745 sym.st_shndx = SHN_UNDEF;
c152c796 12746
cb33740c
AM
12747 s = bfd_section_from_elf_index (e->input_bfd,
12748 e->isym.st_shndx);
4d68fd75
AM
12749 if (s != NULL
12750 && s->output_section != NULL
12751 && elf_section_data (s->output_section) != NULL)
c152c796 12752 {
c152c796
AM
12753 sym.st_shndx =
12754 elf_section_data (s->output_section)->this_idx;
c0d5a53d 12755 if (! check_dynsym (abfd, &sym))
496afd17 12756 {
0a1b45a2 12757 ret = false;
496afd17
L
12758 goto return_local_hash_table;
12759 }
c152c796
AM
12760 sym.st_value = (s->output_section->vma
12761 + s->output_offset
12762 + e->isym.st_value);
12763 }
12764
3d16b64e
NA
12765 /* Inform the linker of the addition of this symbol. */
12766
12767 if (info->callbacks->ctf_new_dynsym)
12768 info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
12769
c152c796
AM
12770 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12771 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12772 }
12773 }
c152c796
AM
12774 }
12775
12776 /* We get the global symbols from the hash table. */
0a1b45a2
AM
12777 eoinfo.failed = false;
12778 eoinfo.localsyms = false;
8b127cbc 12779 eoinfo.flinfo = &flinfo;
7686d77d 12780 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796 12781 if (eoinfo.failed)
496afd17 12782 {
0a1b45a2 12783 ret = false;
496afd17
L
12784 goto return_local_hash_table;
12785 }
c152c796
AM
12786
12787 /* If backend needs to output some symbols not present in the hash
12788 table, do it now. */
8539e4e8
AM
12789 if (bed->elf_backend_output_arch_syms
12790 && (info->strip != strip_all || emit_relocs))
c152c796 12791 {
c152c796 12792 if (! ((*bed->elf_backend_output_arch_syms)
37bb890f 12793 (abfd, info, &flinfo, elf_link_output_symstrtab)))
496afd17 12794 {
0a1b45a2 12795 ret = false;
496afd17
L
12796 goto return_local_hash_table;
12797 }
c152c796
AM
12798 }
12799
ef10c3ac
L
12800 /* Finalize the .strtab section. */
12801 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12802
12803 /* Swap out the .strtab section. */
12804 if (!elf_link_swap_symbols_out (&flinfo))
496afd17 12805 {
0a1b45a2 12806 ret = false;
496afd17
L
12807 goto return_local_hash_table;
12808 }
c152c796
AM
12809
12810 /* Now we know the size of the symtab section. */
c152c796
AM
12811 if (bfd_get_symcount (abfd) > 0)
12812 {
ee3b52e9
L
12813 /* Finish up and write out the symbol string table (.strtab)
12814 section. */
ad32986f 12815 Elf_Internal_Shdr *symstrtab_hdr = NULL;
8539e4e8
AM
12816 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12817
ad32986f 12818 if (elf_symtab_shndx_list (abfd))
8539e4e8 12819 {
ad32986f 12820 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8539e4e8 12821
ad32986f
NC
12822 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12823 {
12824 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12825 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12826 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12827 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12828 symtab_shndx_hdr->sh_size = amt;
8539e4e8 12829
ad32986f 12830 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
0a1b45a2 12831 off, true);
ad32986f
NC
12832
12833 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12834 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
496afd17 12835 {
0a1b45a2 12836 ret = false;
496afd17
L
12837 goto return_local_hash_table;
12838 }
ad32986f 12839 }
8539e4e8 12840 }
ee3b52e9
L
12841
12842 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12843 /* sh_name was set in prep_headers. */
12844 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 12845 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 12846 symstrtab_hdr->sh_addr = 0;
ef10c3ac 12847 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
12848 symstrtab_hdr->sh_entsize = 0;
12849 symstrtab_hdr->sh_link = 0;
12850 symstrtab_hdr->sh_info = 0;
12851 /* sh_offset is set just below. */
12852 symstrtab_hdr->sh_addralign = 1;
12853
12854 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
0a1b45a2 12855 off, true);
ee3b52e9
L
12856 elf_next_file_pos (abfd) = off;
12857
c152c796 12858 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 12859 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
496afd17 12860 {
0a1b45a2 12861 ret = false;
496afd17
L
12862 goto return_local_hash_table;
12863 }
c152c796
AM
12864 }
12865
76359541
TP
12866 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12867 {
871b3ab2 12868 _bfd_error_handler (_("%pB: failed to generate import library"),
4eca0228 12869 info->out_implib_bfd);
0a1b45a2 12870 ret = false;
496afd17 12871 goto return_local_hash_table;
76359541
TP
12872 }
12873
c152c796
AM
12874 /* Adjust the relocs to have the correct symbol indices. */
12875 for (o = abfd->sections; o != NULL; o = o->next)
12876 {
d4730f92 12877 struct bfd_elf_section_data *esdo = elf_section_data (o);
0a1b45a2 12878 bool sort;
10bbbc1d 12879
c152c796
AM
12880 if ((o->flags & SEC_RELOC) == 0)
12881 continue;
12882
28dbcedc 12883 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3 12884 if (esdo->rel.hdr != NULL
10bbbc1d 12885 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
496afd17 12886 {
0a1b45a2 12887 ret = false;
496afd17
L
12888 goto return_local_hash_table;
12889 }
bca6d0e3 12890 if (esdo->rela.hdr != NULL
10bbbc1d 12891 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
496afd17 12892 {
0a1b45a2 12893 ret = false;
496afd17
L
12894 goto return_local_hash_table;
12895 }
c152c796
AM
12896
12897 /* Set the reloc_count field to 0 to prevent write_relocs from
12898 trying to swap the relocs out itself. */
12899 o->reloc_count = 0;
12900 }
12901
12902 if (dynamic && info->combreloc && dynobj != NULL)
12903 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12904
12905 /* If we are linking against a dynamic object, or generating a
12906 shared library, finish up the dynamic linking information. */
12907 if (dynamic)
12908 {
12909 bfd_byte *dyncon, *dynconend;
12910
12911 /* Fix up .dynamic entries. */
3d4d4302 12912 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
12913 BFD_ASSERT (o != NULL);
12914
12915 dyncon = o->contents;
eea6121a 12916 dynconend = o->contents + o->size;
c152c796
AM
12917 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12918 {
12919 Elf_Internal_Dyn dyn;
12920 const char *name;
12921 unsigned int type;
64487780
AM
12922 bfd_size_type sh_size;
12923 bfd_vma sh_addr;
c152c796
AM
12924
12925 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12926
12927 switch (dyn.d_tag)
12928 {
12929 default:
12930 continue;
12931 case DT_NULL:
12932 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12933 {
12934 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12935 {
12936 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12937 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12938 default: continue;
12939 }
12940 dyn.d_un.d_val = relativecount;
12941 relativecount = 0;
12942 break;
12943 }
12944 continue;
12945
12946 case DT_INIT:
12947 name = info->init_function;
12948 goto get_sym;
12949 case DT_FINI:
12950 name = info->fini_function;
12951 get_sym:
12952 {
12953 struct elf_link_hash_entry *h;
12954
0a1b45a2 12955 h = elf_link_hash_lookup (htab, name, false, false, true);
c152c796
AM
12956 if (h != NULL
12957 && (h->root.type == bfd_link_hash_defined
12958 || h->root.type == bfd_link_hash_defweak))
12959 {
bef26483 12960 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
12961 o = h->root.u.def.section;
12962 if (o->output_section != NULL)
bef26483 12963 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
12964 + o->output_offset);
12965 else
12966 {
12967 /* The symbol is imported from another shared
12968 library and does not apply to this one. */
bef26483 12969 dyn.d_un.d_ptr = 0;
c152c796
AM
12970 }
12971 break;
12972 }
12973 }
12974 continue;
12975
12976 case DT_PREINIT_ARRAYSZ:
12977 name = ".preinit_array";
4ade44b7 12978 goto get_out_size;
c152c796
AM
12979 case DT_INIT_ARRAYSZ:
12980 name = ".init_array";
4ade44b7 12981 goto get_out_size;
c152c796
AM
12982 case DT_FINI_ARRAYSZ:
12983 name = ".fini_array";
4ade44b7 12984 get_out_size:
c152c796
AM
12985 o = bfd_get_section_by_name (abfd, name);
12986 if (o == NULL)
12987 {
4eca0228 12988 _bfd_error_handler
4ade44b7 12989 (_("could not find section %s"), name);
c152c796
AM
12990 goto error_return;
12991 }
eea6121a 12992 if (o->size == 0)
4eca0228 12993 _bfd_error_handler
c152c796 12994 (_("warning: %s section has zero size"), name);
eea6121a 12995 dyn.d_un.d_val = o->size;
c152c796
AM
12996 break;
12997
12998 case DT_PREINIT_ARRAY:
12999 name = ".preinit_array";
4ade44b7 13000 goto get_out_vma;
c152c796
AM
13001 case DT_INIT_ARRAY:
13002 name = ".init_array";
4ade44b7 13003 goto get_out_vma;
c152c796
AM
13004 case DT_FINI_ARRAY:
13005 name = ".fini_array";
4ade44b7
AM
13006 get_out_vma:
13007 o = bfd_get_section_by_name (abfd, name);
13008 goto do_vma;
c152c796
AM
13009
13010 case DT_HASH:
13011 name = ".hash";
13012 goto get_vma;
fdc90cb4
JJ
13013 case DT_GNU_HASH:
13014 name = ".gnu.hash";
13015 goto get_vma;
c152c796
AM
13016 case DT_STRTAB:
13017 name = ".dynstr";
13018 goto get_vma;
13019 case DT_SYMTAB:
13020 name = ".dynsym";
13021 goto get_vma;
13022 case DT_VERDEF:
13023 name = ".gnu.version_d";
13024 goto get_vma;
13025 case DT_VERNEED:
13026 name = ".gnu.version_r";
13027 goto get_vma;
13028 case DT_VERSYM:
13029 name = ".gnu.version";
13030 get_vma:
4ade44b7
AM
13031 o = bfd_get_linker_section (dynobj, name);
13032 do_vma:
b3293efa 13033 if (o == NULL || bfd_is_abs_section (o->output_section))
c152c796 13034 {
4eca0228 13035 _bfd_error_handler
4ade44b7 13036 (_("could not find section %s"), name);
c152c796
AM
13037 goto error_return;
13038 }
894891db
NC
13039 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13040 {
4eca0228 13041 _bfd_error_handler
894891db
NC
13042 (_("warning: section '%s' is being made into a note"), name);
13043 bfd_set_error (bfd_error_nonrepresentable_section);
13044 goto error_return;
13045 }
4ade44b7 13046 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
c152c796
AM
13047 break;
13048
13049 case DT_REL:
13050 case DT_RELA:
13051 case DT_RELSZ:
13052 case DT_RELASZ:
13053 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13054 type = SHT_REL;
13055 else
13056 type = SHT_RELA;
64487780
AM
13057 sh_size = 0;
13058 sh_addr = 0;
c152c796
AM
13059 for (i = 1; i < elf_numsections (abfd); i++)
13060 {
13061 Elf_Internal_Shdr *hdr;
13062
13063 hdr = elf_elfsections (abfd)[i];
13064 if (hdr->sh_type == type
13065 && (hdr->sh_flags & SHF_ALLOC) != 0)
13066 {
64487780
AM
13067 sh_size += hdr->sh_size;
13068 if (sh_addr == 0
13069 || sh_addr > hdr->sh_addr)
13070 sh_addr = hdr->sh_addr;
c152c796
AM
13071 }
13072 }
64487780 13073
64f52338
AM
13074 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
13075 {
66631823
CE
13076 unsigned int opb = bfd_octets_per_byte (abfd, o);
13077
64f52338
AM
13078 /* Don't count procedure linkage table relocs in the
13079 overall reloc count. */
64487780
AM
13080 sh_size -= htab->srelplt->size;
13081 if (sh_size == 0)
13082 /* If the size is zero, make the address zero too.
13083 This is to avoid a glibc bug. If the backend
13084 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13085 zero, then we'll put DT_RELA at the end of
13086 DT_JMPREL. glibc will interpret the end of
13087 DT_RELA matching the end of DT_JMPREL as the
13088 case where DT_RELA includes DT_JMPREL, and for
13089 LD_BIND_NOW will decide that processing DT_RELA
13090 will process the PLT relocs too. Net result:
13091 No PLT relocs applied. */
13092 sh_addr = 0;
13093
64f52338
AM
13094 /* If .rela.plt is the first .rela section, exclude
13095 it from DT_RELA. */
64487780 13096 else if (sh_addr == (htab->srelplt->output_section->vma
66631823 13097 + htab->srelplt->output_offset) * opb)
64487780 13098 sh_addr += htab->srelplt->size;
64f52338 13099 }
64487780
AM
13100
13101 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13102 dyn.d_un.d_val = sh_size;
13103 else
13104 dyn.d_un.d_ptr = sh_addr;
c152c796
AM
13105 break;
13106 }
13107 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13108 }
13109 }
13110
13111 /* If we have created any dynamic sections, then output them. */
13112 if (dynobj != NULL)
13113 {
13114 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
13115 goto error_return;
13116
943284cc 13117 /* Check for DT_TEXTREL (late, in case the backend removes it). */
a6dbf402 13118 if (bfd_link_textrel_check (info)
3d4d4302 13119 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
13120 {
13121 bfd_byte *dyncon, *dynconend;
13122
943284cc
DJ
13123 dyncon = o->contents;
13124 dynconend = o->contents + o->size;
13125 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13126 {
13127 Elf_Internal_Dyn dyn;
13128
13129 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13130
13131 if (dyn.d_tag == DT_TEXTREL)
13132 {
a6dbf402 13133 if (info->textrel_check == textrel_check_error)
c192a133 13134 info->callbacks->einfo
9793eb77 13135 (_("%P%X: read-only segment has dynamic relocations\n"));
a6dbf402
L
13136 else if (bfd_link_dll (info))
13137 info->callbacks->einfo
13138 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
c192a133
AM
13139 else
13140 info->callbacks->einfo
a6dbf402 13141 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
943284cc
DJ
13142 break;
13143 }
13144 }
13145 }
13146
c152c796
AM
13147 for (o = dynobj->sections; o != NULL; o = o->next)
13148 {
13149 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 13150 || o->size == 0
c152c796
AM
13151 || o->output_section == bfd_abs_section_ptr)
13152 continue;
13153 if ((o->flags & SEC_LINKER_CREATED) == 0)
13154 {
13155 /* At this point, we are only interested in sections
13156 created by _bfd_elf_link_create_dynamic_sections. */
13157 continue;
13158 }
64f52338 13159 if (htab->stab_info.stabstr == o)
3722b82f 13160 continue;
64f52338 13161 if (htab->eh_info.hdr_sec == o)
eea6121a 13162 continue;
3d4d4302 13163 if (strcmp (o->name, ".dynstr") != 0)
c152c796 13164 {
bb294208
AM
13165 bfd_size_type octets = ((file_ptr) o->output_offset
13166 * bfd_octets_per_byte (abfd, o));
13167 if (!bfd_set_section_contents (abfd, o->output_section,
13168 o->contents, octets, o->size))
c152c796
AM
13169 goto error_return;
13170 }
13171 else
13172 {
13173 /* The contents of the .dynstr section are actually in a
13174 stringtab. */
8539e4e8
AM
13175 file_ptr off;
13176
c152c796
AM
13177 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13178 if (bfd_seek (abfd, off, SEEK_SET) != 0
64f52338 13179 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
c152c796
AM
13180 goto error_return;
13181 }
13182 }
13183 }
13184
7bdf4127 13185 if (!info->resolve_section_groups)
c152c796 13186 {
0a1b45a2 13187 bool failed = false;
c152c796 13188
7bdf4127 13189 BFD_ASSERT (bfd_link_relocatable (info));
c152c796
AM
13190 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13191 if (failed)
13192 goto error_return;
13193 }
13194
13195 /* If we have optimized stabs strings, output them. */
64f52338 13196 if (htab->stab_info.stabstr != NULL)
c152c796 13197 {
64f52338 13198 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
c152c796
AM
13199 goto error_return;
13200 }
13201
9f7c3e5e
AM
13202 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13203 goto error_return;
c152c796 13204
1ff6de03
NA
13205 if (info->callbacks->emit_ctf)
13206 info->callbacks->emit_ctf ();
13207
9f7c3e5e 13208 elf_final_link_free (abfd, &flinfo);
c152c796 13209
104d59d1
JM
13210 if (attr_section)
13211 {
a50b1753 13212 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 13213 if (contents == NULL)
496afd17
L
13214 {
13215 /* Bail out and fail. */
0a1b45a2 13216 ret = false;
496afd17
L
13217 goto return_local_hash_table;
13218 }
104d59d1
JM
13219 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
13220 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
13221 free (contents);
13222 }
13223
496afd17
L
13224 return_local_hash_table:
13225 if (info->unique_symbol)
13226 bfd_hash_table_free (&flinfo.local_hash_table);
13227 return ret;
c152c796
AM
13228
13229 error_return:
9f7c3e5e 13230 elf_final_link_free (abfd, &flinfo);
0a1b45a2 13231 ret = false;
496afd17 13232 goto return_local_hash_table;
c152c796
AM
13233}
13234\f
5241d853
RS
13235/* Initialize COOKIE for input bfd ABFD. */
13236
0a1b45a2 13237static bool
5241d853
RS
13238init_reloc_cookie (struct elf_reloc_cookie *cookie,
13239 struct bfd_link_info *info, bfd *abfd)
13240{
13241 Elf_Internal_Shdr *symtab_hdr;
13242 const struct elf_backend_data *bed;
13243
13244 bed = get_elf_backend_data (abfd);
13245 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13246
13247 cookie->abfd = abfd;
13248 cookie->sym_hashes = elf_sym_hashes (abfd);
13249 cookie->bad_symtab = elf_bad_symtab (abfd);
13250 if (cookie->bad_symtab)
13251 {
13252 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13253 cookie->extsymoff = 0;
13254 }
13255 else
13256 {
13257 cookie->locsymcount = symtab_hdr->sh_info;
13258 cookie->extsymoff = symtab_hdr->sh_info;
13259 }
13260
13261 if (bed->s->arch_size == 32)
13262 cookie->r_sym_shift = 8;
13263 else
13264 cookie->r_sym_shift = 32;
13265
13266 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13267 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13268 {
13269 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13270 cookie->locsymcount, 0,
13271 NULL, NULL, NULL);
13272 if (cookie->locsyms == NULL)
13273 {
13274 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
0a1b45a2 13275 return false;
5241d853
RS
13276 }
13277 if (info->keep_memory)
13278 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13279 }
0a1b45a2 13280 return true;
5241d853
RS
13281}
13282
13283/* Free the memory allocated by init_reloc_cookie, if appropriate. */
13284
13285static void
13286fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13287{
13288 Elf_Internal_Shdr *symtab_hdr;
13289
13290 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
c9594989 13291 if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
5241d853
RS
13292 free (cookie->locsyms);
13293}
13294
13295/* Initialize the relocation information in COOKIE for input section SEC
13296 of input bfd ABFD. */
13297
0a1b45a2 13298static bool
5241d853
RS
13299init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13300 struct bfd_link_info *info, bfd *abfd,
13301 asection *sec)
13302{
5241d853
RS
13303 if (sec->reloc_count == 0)
13304 {
13305 cookie->rels = NULL;
13306 cookie->relend = NULL;
13307 }
13308 else
13309 {
5241d853
RS
13310 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
13311 info->keep_memory);
13312 if (cookie->rels == NULL)
0a1b45a2 13313 return false;
5241d853 13314 cookie->rel = cookie->rels;
056bafd4 13315 cookie->relend = cookie->rels + sec->reloc_count;
5241d853
RS
13316 }
13317 cookie->rel = cookie->rels;
0a1b45a2 13318 return true;
5241d853
RS
13319}
13320
13321/* Free the memory allocated by init_reloc_cookie_rels,
13322 if appropriate. */
13323
13324static void
13325fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13326 asection *sec)
13327{
c9594989 13328 if (elf_section_data (sec)->relocs != cookie->rels)
5241d853
RS
13329 free (cookie->rels);
13330}
13331
13332/* Initialize the whole of COOKIE for input section SEC. */
13333
0a1b45a2 13334static bool
5241d853
RS
13335init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13336 struct bfd_link_info *info,
13337 asection *sec)
13338{
13339 if (!init_reloc_cookie (cookie, info, sec->owner))
13340 goto error1;
13341 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13342 goto error2;
0a1b45a2 13343 return true;
5241d853
RS
13344
13345 error2:
13346 fini_reloc_cookie (cookie, sec->owner);
13347 error1:
0a1b45a2 13348 return false;
5241d853
RS
13349}
13350
13351/* Free the memory allocated by init_reloc_cookie_for_section,
13352 if appropriate. */
13353
13354static void
13355fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13356 asection *sec)
13357{
13358 fini_reloc_cookie_rels (cookie, sec);
13359 fini_reloc_cookie (cookie, sec->owner);
13360}
13361\f
c152c796
AM
13362/* Garbage collect unused sections. */
13363
07adf181
AM
13364/* Default gc_mark_hook. */
13365
13366asection *
13367_bfd_elf_gc_mark_hook (asection *sec,
13368 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13369 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13370 struct elf_link_hash_entry *h,
13371 Elf_Internal_Sym *sym)
13372{
13373 if (h != NULL)
13374 {
13375 switch (h->root.type)
13376 {
13377 case bfd_link_hash_defined:
13378 case bfd_link_hash_defweak:
13379 return h->root.u.def.section;
13380
13381 case bfd_link_hash_common:
13382 return h->root.u.c.p->section;
13383
13384 default:
13385 break;
13386 }
13387 }
13388 else
13389 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13390
13391 return NULL;
13392}
13393
9e223787 13394/* Return the debug definition section. */
b7c871ed
L
13395
13396static asection *
13397elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13398 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13399 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13400 struct elf_link_hash_entry *h,
9e223787 13401 Elf_Internal_Sym *sym)
b7c871ed 13402{
9e223787
L
13403 if (h != NULL)
13404 {
13405 /* Return the global debug definition section. */
13406 if ((h->root.type == bfd_link_hash_defined
13407 || h->root.type == bfd_link_hash_defweak)
13408 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13409 return h->root.u.def.section;
13410 }
13411 else
13412 {
13413 /* Return the local debug definition section. */
13414 asection *isec = bfd_section_from_elf_index (sec->owner,
13415 sym->st_shndx);
13416 if ((isec->flags & SEC_DEBUGGING) != 0)
13417 return isec;
13418 }
b7c871ed
L
13419
13420 return NULL;
13421}
13422
5241d853
RS
13423/* COOKIE->rel describes a relocation against section SEC, which is
13424 a section we've decided to keep. Return the section that contains
13425 the relocation symbol, or NULL if no section contains it. */
13426
13427asection *
13428_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13429 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9 13430 struct elf_reloc_cookie *cookie,
0a1b45a2 13431 bool *start_stop)
5241d853
RS
13432{
13433 unsigned long r_symndx;
3024a17a 13434 struct elf_link_hash_entry *h, *hw;
5241d853
RS
13435
13436 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 13437 if (r_symndx == STN_UNDEF)
5241d853
RS
13438 return NULL;
13439
13440 if (r_symndx >= cookie->locsymcount
13441 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13442 {
0a1b45a2 13443 bool was_marked;
5789f845 13444
5241d853 13445 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
13446 if (h == NULL)
13447 {
871b3ab2 13448 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
263ddf68
L
13449 sec->owner);
13450 return NULL;
13451 }
5241d853
RS
13452 while (h->root.type == bfd_link_hash_indirect
13453 || h->root.type == bfd_link_hash_warning)
13454 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5789f845
AM
13455
13456 was_marked = h->mark;
1d5316ab 13457 h->mark = 1;
3024a17a
AM
13458 /* Keep all aliases of the symbol too. If an object symbol
13459 needs to be copied into .dynbss then all of its aliases
13460 should be present as dynamic symbols, not just the one used
13461 on the copy relocation. */
13462 hw = h;
13463 while (hw->is_weakalias)
13464 {
13465 hw = hw->u.alias;
13466 hw->mark = 1;
13467 }
1cce69b9 13468
5789f845 13469 if (!was_marked && h->start_stop && !h->root.ldscript_def)
1cce69b9 13470 {
8ee10e86
AM
13471 if (info->start_stop_gc)
13472 return NULL;
13473
7dba9362
AM
13474 /* To work around a glibc bug, mark XXX input sections
13475 when there is a reference to __start_XXX or __stop_XXX
13476 symbols. */
8ee10e86 13477 else if (start_stop != NULL)
1cce69b9 13478 {
cbd0eecf 13479 asection *s = h->u2.start_stop_section;
0a1b45a2 13480 *start_stop = true;
a6a4679f 13481 return s;
1cce69b9
AM
13482 }
13483 }
13484
5241d853
RS
13485 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13486 }
13487
13488 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13489 &cookie->locsyms[r_symndx]);
13490}
13491
13492/* COOKIE->rel describes a relocation against section SEC, which is
13493 a section we've decided to keep. Mark the section that contains
9d0a14d3 13494 the relocation symbol. */
5241d853 13495
0a1b45a2 13496bool
5241d853
RS
13497_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13498 asection *sec,
13499 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 13500 struct elf_reloc_cookie *cookie)
5241d853
RS
13501{
13502 asection *rsec;
0a1b45a2 13503 bool start_stop = false;
5241d853 13504
1cce69b9
AM
13505 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13506 while (rsec != NULL)
5241d853 13507 {
1cce69b9
AM
13508 if (!rsec->gc_mark)
13509 {
13510 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13511 || (rsec->owner->flags & DYNAMIC) != 0)
13512 rsec->gc_mark = 1;
13513 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
0a1b45a2 13514 return false;
1cce69b9
AM
13515 }
13516 if (!start_stop)
13517 break;
199af150 13518 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853 13519 }
0a1b45a2 13520 return true;
5241d853
RS
13521}
13522
07adf181
AM
13523/* The mark phase of garbage collection. For a given section, mark
13524 it and any sections in this section's group, and all the sections
13525 which define symbols to which it refers. */
13526
0a1b45a2 13527bool
ccfa59ea
AM
13528_bfd_elf_gc_mark (struct bfd_link_info *info,
13529 asection *sec,
6a5bb875 13530 elf_gc_mark_hook_fn gc_mark_hook)
c152c796 13531{
0a1b45a2 13532 bool ret;
9d0a14d3 13533 asection *group_sec, *eh_frame;
c152c796
AM
13534
13535 sec->gc_mark = 1;
13536
13537 /* Mark all the sections in the group. */
13538 group_sec = elf_section_data (sec)->next_in_group;
13539 if (group_sec && !group_sec->gc_mark)
ccfa59ea 13540 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
0a1b45a2 13541 return false;
c152c796
AM
13542
13543 /* Look through the section relocs. */
0a1b45a2 13544 ret = true;
9d0a14d3
RS
13545 eh_frame = elf_eh_frame_section (sec->owner);
13546 if ((sec->flags & SEC_RELOC) != 0
13547 && sec->reloc_count > 0
13548 && sec != eh_frame)
c152c796 13549 {
5241d853 13550 struct elf_reloc_cookie cookie;
c152c796 13551
5241d853 13552 if (!init_reloc_cookie_for_section (&cookie, info, sec))
0a1b45a2 13553 ret = false;
c152c796 13554 else
c152c796 13555 {
5241d853 13556 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 13557 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853 13558 {
0a1b45a2 13559 ret = false;
5241d853
RS
13560 break;
13561 }
13562 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
13563 }
13564 }
9d0a14d3
RS
13565
13566 if (ret && eh_frame && elf_fde_list (sec))
13567 {
13568 struct elf_reloc_cookie cookie;
13569
13570 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
0a1b45a2 13571 ret = false;
9d0a14d3
RS
13572 else
13573 {
13574 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13575 gc_mark_hook, &cookie))
0a1b45a2 13576 ret = false;
9d0a14d3
RS
13577 fini_reloc_cookie_for_section (&cookie, eh_frame);
13578 }
13579 }
13580
2f0c68f2
CM
13581 eh_frame = elf_section_eh_frame_entry (sec);
13582 if (ret && eh_frame && !eh_frame->gc_mark)
13583 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
0a1b45a2 13584 ret = false;
2f0c68f2 13585
c152c796
AM
13586 return ret;
13587}
13588
3c758495
TG
13589/* Scan and mark sections in a special or debug section group. */
13590
13591static void
13592_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13593{
13594 /* Point to first section of section group. */
13595 asection *ssec;
13596 /* Used to iterate the section group. */
13597 asection *msec;
13598
0a1b45a2
AM
13599 bool is_special_grp = true;
13600 bool is_debug_grp = true;
3c758495
TG
13601
13602 /* First scan to see if group contains any section other than debug
13603 and special section. */
13604 ssec = msec = elf_next_in_group (grp);
13605 do
13606 {
13607 if ((msec->flags & SEC_DEBUGGING) == 0)
0a1b45a2 13608 is_debug_grp = false;
3c758495
TG
13609
13610 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
0a1b45a2 13611 is_special_grp = false;
3c758495
TG
13612
13613 msec = elf_next_in_group (msec);
13614 }
13615 while (msec != ssec);
13616
13617 /* If this is a pure debug section group or pure special section group,
13618 keep all sections in this group. */
13619 if (is_debug_grp || is_special_grp)
13620 {
13621 do
13622 {
13623 msec->gc_mark = 1;
13624 msec = elf_next_in_group (msec);
13625 }
13626 while (msec != ssec);
13627 }
13628}
13629
7f6ab9f8
AM
13630/* Keep debug and special sections. */
13631
0a1b45a2 13632bool
7f6ab9f8 13633_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
b7d07216 13634 elf_gc_mark_hook_fn mark_hook)
7f6ab9f8
AM
13635{
13636 bfd *ibfd;
13637
c72f2fb2 13638 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
13639 {
13640 asection *isec;
0a1b45a2
AM
13641 bool some_kept;
13642 bool debug_frag_seen;
13643 bool has_kept_debug_info;
7f6ab9f8
AM
13644
13645 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13646 continue;
57963c05
AM
13647 isec = ibfd->sections;
13648 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13649 continue;
7f6ab9f8 13650
b40bf0a2
NC
13651 /* Ensure all linker created sections are kept,
13652 see if any other section is already marked,
13653 and note if we have any fragmented debug sections. */
0a1b45a2 13654 debug_frag_seen = some_kept = has_kept_debug_info = false;
7f6ab9f8
AM
13655 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13656 {
13657 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13658 isec->gc_mark = 1;
eb026f09
AM
13659 else if (isec->gc_mark
13660 && (isec->flags & SEC_ALLOC) != 0
13661 && elf_section_type (isec) != SHT_NOTE)
0a1b45a2 13662 some_kept = true;
b7d07216
L
13663 else
13664 {
13665 /* Since all sections, except for backend specific ones,
13666 have been garbage collected, call mark_hook on this
13667 section if any of its linked-to sections is marked. */
def97fb9
AM
13668 asection *linked_to_sec;
13669 for (linked_to_sec = elf_linked_to_section (isec);
13670 linked_to_sec != NULL && !linked_to_sec->linker_mark;
b7d07216 13671 linked_to_sec = elf_linked_to_section (linked_to_sec))
def97fb9
AM
13672 {
13673 if (linked_to_sec->gc_mark)
13674 {
13675 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
0a1b45a2 13676 return false;
def97fb9
AM
13677 break;
13678 }
13679 linked_to_sec->linker_mark = 1;
13680 }
13681 for (linked_to_sec = elf_linked_to_section (isec);
13682 linked_to_sec != NULL && linked_to_sec->linker_mark;
13683 linked_to_sec = elf_linked_to_section (linked_to_sec))
13684 linked_to_sec->linker_mark = 0;
b7d07216 13685 }
b40bf0a2 13686
535b785f 13687 if (!debug_frag_seen
b40bf0a2 13688 && (isec->flags & SEC_DEBUGGING)
08dedd66 13689 && startswith (isec->name, ".debug_line."))
0a1b45a2 13690 debug_frag_seen = true;
5242a0a0
L
13691 else if (strcmp (bfd_section_name (isec),
13692 "__patchable_function_entries") == 0
13693 && elf_linked_to_section (isec) == NULL)
13694 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
13695 "need linked-to section "
13696 "for --gc-sections\n"),
13697 isec->owner, isec);
7f6ab9f8
AM
13698 }
13699
eb026f09
AM
13700 /* If no non-note alloc section in this file will be kept, then
13701 we can toss out the debug and special sections. */
7f6ab9f8
AM
13702 if (!some_kept)
13703 continue;
13704
13705 /* Keep debug and special sections like .comment when they are
3c758495 13706 not part of a group. Also keep section groups that contain
b7d07216
L
13707 just debug sections or special sections. NB: Sections with
13708 linked-to section has been handled above. */
7f6ab9f8 13709 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
13710 {
13711 if ((isec->flags & SEC_GROUP) != 0)
13712 _bfd_elf_gc_mark_debug_special_section_group (isec);
13713 else if (((isec->flags & SEC_DEBUGGING) != 0
13714 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
b7d07216
L
13715 && elf_next_in_group (isec) == NULL
13716 && elf_linked_to_section (isec) == NULL)
3c758495 13717 isec->gc_mark = 1;
b7c871ed 13718 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
0a1b45a2 13719 has_kept_debug_info = true;
3c758495 13720 }
b40bf0a2 13721
b40bf0a2
NC
13722 /* Look for CODE sections which are going to be discarded,
13723 and find and discard any fragmented debug sections which
13724 are associated with that code section. */
b7c871ed
L
13725 if (debug_frag_seen)
13726 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13727 if ((isec->flags & SEC_CODE) != 0
13728 && isec->gc_mark == 0)
13729 {
13730 unsigned int ilen;
13731 asection *dsec;
b40bf0a2 13732
b7c871ed 13733 ilen = strlen (isec->name);
b40bf0a2 13734
b7c871ed 13735 /* Association is determined by the name of the debug
07d6d2b8 13736 section containing the name of the code section as
b7c871ed
L
13737 a suffix. For example .debug_line.text.foo is a
13738 debug section associated with .text.foo. */
13739 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13740 {
13741 unsigned int dlen;
b40bf0a2 13742
b7c871ed
L
13743 if (dsec->gc_mark == 0
13744 || (dsec->flags & SEC_DEBUGGING) == 0)
13745 continue;
b40bf0a2 13746
b7c871ed 13747 dlen = strlen (dsec->name);
b40bf0a2 13748
b7c871ed
L
13749 if (dlen > ilen
13750 && strncmp (dsec->name + (dlen - ilen),
13751 isec->name, ilen) == 0)
b40bf0a2 13752 dsec->gc_mark = 0;
b7c871ed 13753 }
b40bf0a2 13754 }
b7c871ed
L
13755
13756 /* Mark debug sections referenced by kept debug sections. */
13757 if (has_kept_debug_info)
13758 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13759 if (isec->gc_mark
13760 && (isec->flags & SEC_DEBUGGING) != 0)
13761 if (!_bfd_elf_gc_mark (info, isec,
13762 elf_gc_mark_debug_section))
0a1b45a2 13763 return false;
7f6ab9f8 13764 }
0a1b45a2 13765 return true;
7f6ab9f8
AM
13766}
13767
0a1b45a2 13768static bool
ccabcbe5 13769elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
13770{
13771 bfd *sub;
ccabcbe5 13772 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
c152c796 13773
c72f2fb2 13774 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13775 {
13776 asection *o;
13777
b19a8f85 13778 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 13779 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
b19a8f85 13780 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796 13781 continue;
57963c05
AM
13782 o = sub->sections;
13783 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13784 continue;
c152c796
AM
13785
13786 for (o = sub->sections; o != NULL; o = o->next)
13787 {
a33dafc3
L
13788 /* When any section in a section group is kept, we keep all
13789 sections in the section group. If the first member of
13790 the section group is excluded, we will also exclude the
13791 group section. */
13792 if (o->flags & SEC_GROUP)
13793 {
13794 asection *first = elf_next_in_group (o);
13795 o->gc_mark = first->gc_mark;
13796 }
c152c796 13797
1e7eae0d 13798 if (o->gc_mark)
c152c796
AM
13799 continue;
13800
13801 /* Skip sweeping sections already excluded. */
13802 if (o->flags & SEC_EXCLUDE)
13803 continue;
13804
13805 /* Since this is early in the link process, it is simple
13806 to remove a section from the output. */
13807 o->flags |= SEC_EXCLUDE;
13808
c55fe096 13809 if (info->print_gc_sections && o->size != 0)
695344c0 13810 /* xgettext:c-format */
9793eb77 13811 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
c08bb8dd 13812 o, sub);
c152c796
AM
13813 }
13814 }
13815
0a1b45a2 13816 return true;
c152c796
AM
13817}
13818
13819/* Propagate collected vtable information. This is called through
13820 elf_link_hash_traverse. */
13821
0a1b45a2 13822static bool
c152c796
AM
13823elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13824{
c152c796 13825 /* Those that are not vtables. */
cbd0eecf
L
13826 if (h->start_stop
13827 || h->u2.vtable == NULL
13828 || h->u2.vtable->parent == NULL)
0a1b45a2 13829 return true;
c152c796
AM
13830
13831 /* Those vtables that do not have parents, we cannot merge. */
cbd0eecf 13832 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
0a1b45a2 13833 return true;
c152c796
AM
13834
13835 /* If we've already been done, exit. */
cbd0eecf 13836 if (h->u2.vtable->used && h->u2.vtable->used[-1])
0a1b45a2 13837 return true;
c152c796
AM
13838
13839 /* Make sure the parent's table is up to date. */
cbd0eecf 13840 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
c152c796 13841
cbd0eecf 13842 if (h->u2.vtable->used == NULL)
c152c796
AM
13843 {
13844 /* None of this table's entries were referenced. Re-use the
13845 parent's table. */
cbd0eecf
L
13846 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13847 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
c152c796
AM
13848 }
13849 else
13850 {
13851 size_t n;
0a1b45a2 13852 bool *cu, *pu;
c152c796
AM
13853
13854 /* Or the parent's entries into ours. */
cbd0eecf 13855 cu = h->u2.vtable->used;
0a1b45a2 13856 cu[-1] = true;
cbd0eecf 13857 pu = h->u2.vtable->parent->u2.vtable->used;
c152c796
AM
13858 if (pu != NULL)
13859 {
13860 const struct elf_backend_data *bed;
13861 unsigned int log_file_align;
13862
13863 bed = get_elf_backend_data (h->root.u.def.section->owner);
13864 log_file_align = bed->s->log_file_align;
cbd0eecf 13865 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
c152c796
AM
13866 while (n--)
13867 {
13868 if (*pu)
0a1b45a2 13869 *cu = true;
c152c796
AM
13870 pu++;
13871 cu++;
13872 }
13873 }
13874 }
13875
0a1b45a2 13876 return true;
c152c796
AM
13877}
13878
0a1b45a2 13879static bool
c152c796
AM
13880elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13881{
13882 asection *sec;
13883 bfd_vma hstart, hend;
13884 Elf_Internal_Rela *relstart, *relend, *rel;
13885 const struct elf_backend_data *bed;
13886 unsigned int log_file_align;
13887
c152c796
AM
13888 /* Take care of both those symbols that do not describe vtables as
13889 well as those that are not loaded. */
cbd0eecf
L
13890 if (h->start_stop
13891 || h->u2.vtable == NULL
13892 || h->u2.vtable->parent == NULL)
0a1b45a2 13893 return true;
c152c796
AM
13894
13895 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13896 || h->root.type == bfd_link_hash_defweak);
13897
13898 sec = h->root.u.def.section;
13899 hstart = h->root.u.def.value;
13900 hend = hstart + h->size;
13901
0a1b45a2 13902 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, true);
c152c796 13903 if (!relstart)
0a1b45a2 13904 return *(bool *) okp = false;
c152c796
AM
13905 bed = get_elf_backend_data (sec->owner);
13906 log_file_align = bed->s->log_file_align;
13907
056bafd4 13908 relend = relstart + sec->reloc_count;
c152c796
AM
13909
13910 for (rel = relstart; rel < relend; ++rel)
13911 if (rel->r_offset >= hstart && rel->r_offset < hend)
13912 {
13913 /* If the entry is in use, do nothing. */
cbd0eecf
L
13914 if (h->u2.vtable->used
13915 && (rel->r_offset - hstart) < h->u2.vtable->size)
c152c796
AM
13916 {
13917 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
cbd0eecf 13918 if (h->u2.vtable->used[entry])
c152c796
AM
13919 continue;
13920 }
13921 /* Otherwise, kill it. */
13922 rel->r_offset = rel->r_info = rel->r_addend = 0;
13923 }
13924
0a1b45a2 13925 return true;
c152c796
AM
13926}
13927
87538722
AM
13928/* Mark sections containing dynamically referenced symbols. When
13929 building shared libraries, we must assume that any visible symbol is
13930 referenced. */
715df9b8 13931
0a1b45a2 13932bool
64d03ab5 13933bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 13934{
87538722 13935 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 13936 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 13937
715df9b8
EB
13938 if ((h->root.type == bfd_link_hash_defined
13939 || h->root.type == bfd_link_hash_defweak)
8ee10e86
AM
13940 && (!h->start_stop
13941 || h->root.ldscript_def
13942 || !info->start_stop_gc)
d664fd41 13943 && ((h->ref_dynamic && !h->forced_local)
c4621b33 13944 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 13945 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 13946 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 13947 && (!bfd_link_executable (info)
22185505 13948 || info->gc_keep_exported
b407645f
AM
13949 || info->export_dynamic
13950 || (h->dynamic
13951 && d != NULL
13952 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 13953 && (h->versioned >= versioned
54e8959c
L
13954 || !bfd_hide_sym_by_version (info->version_info,
13955 h->root.root.string)))))
715df9b8
EB
13956 h->root.u.def.section->flags |= SEC_KEEP;
13957
0a1b45a2 13958 return true;
715df9b8 13959}
3b36f7e6 13960
74f0fb50
AM
13961/* Keep all sections containing symbols undefined on the command-line,
13962 and the section containing the entry symbol. */
13963
13964void
13965_bfd_elf_gc_keep (struct bfd_link_info *info)
13966{
13967 struct bfd_sym_chain *sym;
13968
13969 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13970 {
13971 struct elf_link_hash_entry *h;
13972
13973 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
0a1b45a2 13974 false, false, false);
74f0fb50
AM
13975
13976 if (h != NULL
13977 && (h->root.type == bfd_link_hash_defined
13978 || h->root.type == bfd_link_hash_defweak)
2f5541f3 13979 && !bfd_is_const_section (h->root.u.def.section))
74f0fb50
AM
13980 h->root.u.def.section->flags |= SEC_KEEP;
13981 }
13982}
13983
0a1b45a2 13984bool
2f0c68f2
CM
13985bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13986 struct bfd_link_info *info)
13987{
13988 bfd *ibfd = info->input_bfds;
13989
13990 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13991 {
13992 asection *sec;
13993 struct elf_reloc_cookie cookie;
13994
13995 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13996 continue;
57963c05
AM
13997 sec = ibfd->sections;
13998 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13999 continue;
2f0c68f2
CM
14000
14001 if (!init_reloc_cookie (&cookie, info, ibfd))
0a1b45a2 14002 return false;
2f0c68f2
CM
14003
14004 for (sec = ibfd->sections; sec; sec = sec->next)
14005 {
08dedd66 14006 if (startswith (bfd_section_name (sec), ".eh_frame_entry")
2f0c68f2
CM
14007 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
14008 {
14009 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
14010 fini_reloc_cookie_rels (&cookie, sec);
14011 }
14012 }
14013 }
0a1b45a2 14014 return true;
2f0c68f2
CM
14015}
14016
c152c796
AM
14017/* Do mark and sweep of unused sections. */
14018
0a1b45a2 14019bool
c152c796
AM
14020bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
14021{
0a1b45a2 14022 bool ok = true;
c152c796 14023 bfd *sub;
6a5bb875 14024 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 14025 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 14026 struct elf_link_hash_table *htab;
c152c796 14027
64d03ab5 14028 if (!bed->can_gc_sections
715df9b8 14029 || !is_elf_hash_table (info->hash))
c152c796 14030 {
9793eb77 14031 _bfd_error_handler(_("warning: gc-sections option ignored"));
0a1b45a2 14032 return true;
c152c796
AM
14033 }
14034
74f0fb50 14035 bed->gc_keep (info);
da44f4e5 14036 htab = elf_hash_table (info);
74f0fb50 14037
9d0a14d3
RS
14038 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14039 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
14040 for (sub = info->input_bfds;
14041 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14042 sub = sub->link.next)
9d0a14d3
RS
14043 {
14044 asection *sec;
14045 struct elf_reloc_cookie cookie;
14046
57963c05
AM
14047 sec = sub->sections;
14048 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14049 continue;
9d0a14d3 14050 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 14051 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
14052 {
14053 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
14054 if (elf_section_data (sec)->sec_info
14055 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
14056 elf_eh_frame_section (sub) = sec;
14057 fini_reloc_cookie_for_section (&cookie, sec);
199af150 14058 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
14059 }
14060 }
9d0a14d3 14061
c152c796 14062 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 14063 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796 14064 if (!ok)
0a1b45a2 14065 return false;
c152c796
AM
14066
14067 /* Kill the vtable relocations that were not used. */
da44f4e5 14068 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796 14069 if (!ok)
0a1b45a2 14070 return false;
c152c796 14071
715df9b8 14072 /* Mark dynamically referenced symbols. */
22185505 14073 if (htab->dynamic_sections_created || info->gc_keep_exported)
da44f4e5 14074 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 14075
715df9b8 14076 /* Grovel through relocs to find out who stays ... */
64d03ab5 14077 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 14078 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
14079 {
14080 asection *o;
14081
b19a8f85 14082 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 14083 || elf_object_id (sub) != elf_hash_table_id (htab)
b19a8f85 14084 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
14085 continue;
14086
57963c05
AM
14087 o = sub->sections;
14088 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14089 continue;
14090
7f6ab9f8
AM
14091 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14092 Also treat note sections as a root, if the section is not part
8b6f4cd3
L
14093 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14094 well as FINI_ARRAY sections for ld -r. */
c152c796 14095 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
14096 if (!o->gc_mark
14097 && (o->flags & SEC_EXCLUDE) == 0
24007750 14098 && ((o->flags & SEC_KEEP) != 0
8b6f4cd3
L
14099 || (bfd_link_relocatable (info)
14100 && ((elf_section_data (o)->this_hdr.sh_type
14101 == SHT_PREINIT_ARRAY)
14102 || (elf_section_data (o)->this_hdr.sh_type
14103 == SHT_INIT_ARRAY)
14104 || (elf_section_data (o)->this_hdr.sh_type
14105 == SHT_FINI_ARRAY)))
7f6ab9f8 14106 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
7026832e 14107 && elf_next_in_group (o) == NULL
99fabbc9
JL
14108 && elf_linked_to_section (o) == NULL)
14109 || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14110 && (elf_section_flags (o) & SHF_GNU_RETAIN))))
7f6ab9f8
AM
14111 {
14112 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
0a1b45a2 14113 return false;
7f6ab9f8 14114 }
c152c796
AM
14115 }
14116
6a5bb875 14117 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 14118 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 14119
c152c796 14120 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 14121 return elf_gc_sweep (abfd, info);
c152c796
AM
14122}
14123\f
14124/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14125
0a1b45a2 14126bool
c152c796
AM
14127bfd_elf_gc_record_vtinherit (bfd *abfd,
14128 asection *sec,
14129 struct elf_link_hash_entry *h,
14130 bfd_vma offset)
14131{
14132 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14133 struct elf_link_hash_entry **search, *child;
ef53be89 14134 size_t extsymcount;
c152c796
AM
14135 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14136
14137 /* The sh_info field of the symtab header tells us where the
14138 external symbols start. We don't care about the local symbols at
14139 this point. */
14140 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
14141 if (!elf_bad_symtab (abfd))
14142 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
14143
14144 sym_hashes = elf_sym_hashes (abfd);
14145 sym_hashes_end = sym_hashes + extsymcount;
14146
14147 /* Hunt down the child symbol, which is in this section at the same
14148 offset as the relocation. */
14149 for (search = sym_hashes; search != sym_hashes_end; ++search)
14150 {
14151 if ((child = *search) != NULL
14152 && (child->root.type == bfd_link_hash_defined
14153 || child->root.type == bfd_link_hash_defweak)
14154 && child->root.u.def.section == sec
14155 && child->root.u.def.value == offset)
14156 goto win;
14157 }
14158
695344c0 14159 /* xgettext:c-format */
9793eb77 14160 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
2dcf00ce 14161 abfd, sec, (uint64_t) offset);
c152c796 14162 bfd_set_error (bfd_error_invalid_operation);
0a1b45a2 14163 return false;
c152c796
AM
14164
14165 win:
cbd0eecf 14166 if (!child->u2.vtable)
f6e332e6 14167 {
cbd0eecf
L
14168 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
14169 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
14170 if (!child->u2.vtable)
0a1b45a2 14171 return false;
f6e332e6 14172 }
c152c796
AM
14173 if (!h)
14174 {
14175 /* This *should* only be the absolute section. It could potentially
14176 be that someone has defined a non-global vtable though, which
14177 would be bad. It isn't worth paging in the local symbols to be
14178 sure though; that case should simply be handled by the assembler. */
14179
cbd0eecf 14180 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
14181 }
14182 else
cbd0eecf 14183 child->u2.vtable->parent = h;
c152c796 14184
0a1b45a2 14185 return true;
c152c796
AM
14186}
14187
14188/* Called from check_relocs to record the existence of a VTENTRY reloc. */
14189
0a1b45a2 14190bool
a0ea3a14 14191bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
c152c796
AM
14192 struct elf_link_hash_entry *h,
14193 bfd_vma addend)
14194{
14195 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14196 unsigned int log_file_align = bed->s->log_file_align;
14197
a0ea3a14
L
14198 if (!h)
14199 {
14200 /* xgettext:c-format */
14201 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14202 abfd, sec);
14203 bfd_set_error (bfd_error_bad_value);
0a1b45a2 14204 return false;
a0ea3a14
L
14205 }
14206
cbd0eecf 14207 if (!h->u2.vtable)
f6e332e6 14208 {
cbd0eecf
L
14209 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14210 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14211 if (!h->u2.vtable)
0a1b45a2 14212 return false;
f6e332e6
AM
14213 }
14214
cbd0eecf 14215 if (addend >= h->u2.vtable->size)
c152c796
AM
14216 {
14217 size_t size, bytes, file_align;
0a1b45a2 14218 bool *ptr = h->u2.vtable->used;
c152c796
AM
14219
14220 /* While the symbol is undefined, we have to be prepared to handle
14221 a zero size. */
14222 file_align = 1 << log_file_align;
14223 if (h->root.type == bfd_link_hash_undefined)
14224 size = addend + file_align;
14225 else
14226 {
14227 size = h->size;
14228 if (addend >= size)
14229 {
14230 /* Oops! We've got a reference past the defined end of
14231 the table. This is probably a bug -- shall we warn? */
14232 size = addend + file_align;
14233 }
14234 }
14235 size = (size + file_align - 1) & -file_align;
14236
14237 /* Allocate one extra entry for use as a "done" flag for the
14238 consolidation pass. */
0a1b45a2 14239 bytes = ((size >> log_file_align) + 1) * sizeof (bool);
c152c796
AM
14240
14241 if (ptr)
14242 {
0a1b45a2 14243 ptr = (bool *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
14244
14245 if (ptr != NULL)
14246 {
14247 size_t oldbytes;
14248
cbd0eecf 14249 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
0a1b45a2 14250 * sizeof (bool));
c152c796
AM
14251 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14252 }
14253 }
14254 else
0a1b45a2 14255 ptr = (bool *) bfd_zmalloc (bytes);
c152c796
AM
14256
14257 if (ptr == NULL)
0a1b45a2 14258 return false;
c152c796
AM
14259
14260 /* And arrange for that done flag to be at index -1. */
cbd0eecf
L
14261 h->u2.vtable->used = ptr + 1;
14262 h->u2.vtable->size = size;
c152c796
AM
14263 }
14264
0a1b45a2 14265 h->u2.vtable->used[addend >> log_file_align] = true;
c152c796 14266
0a1b45a2 14267 return true;
c152c796
AM
14268}
14269
ae17ab41
CM
14270/* Map an ELF section header flag to its corresponding string. */
14271typedef struct
14272{
14273 char *flag_name;
14274 flagword flag_value;
14275} elf_flags_to_name_table;
14276
3f75e1d6 14277static const elf_flags_to_name_table elf_flags_to_names [] =
ae17ab41
CM
14278{
14279 { "SHF_WRITE", SHF_WRITE },
14280 { "SHF_ALLOC", SHF_ALLOC },
14281 { "SHF_EXECINSTR", SHF_EXECINSTR },
14282 { "SHF_MERGE", SHF_MERGE },
14283 { "SHF_STRINGS", SHF_STRINGS },
14284 { "SHF_INFO_LINK", SHF_INFO_LINK},
14285 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14286 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14287 { "SHF_GROUP", SHF_GROUP },
14288 { "SHF_TLS", SHF_TLS },
14289 { "SHF_MASKOS", SHF_MASKOS },
14290 { "SHF_EXCLUDE", SHF_EXCLUDE },
14291};
14292
b9c361e0 14293/* Returns TRUE if the section is to be included, otherwise FALSE. */
0a1b45a2 14294bool
ae17ab41 14295bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 14296 struct flag_info *flaginfo,
b9c361e0 14297 asection *section)
ae17ab41 14298{
8b127cbc 14299 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 14300
8b127cbc 14301 if (!flaginfo->flags_initialized)
ae17ab41 14302 {
8b127cbc
AM
14303 bfd *obfd = info->output_bfd;
14304 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14305 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
14306 int with_hex = 0;
14307 int without_hex = 0;
14308
8b127cbc 14309 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 14310 {
b9c361e0 14311 unsigned i;
8b127cbc 14312 flagword (*lookup) (char *);
ae17ab41 14313
8b127cbc
AM
14314 lookup = bed->elf_backend_lookup_section_flags_hook;
14315 if (lookup != NULL)
ae17ab41 14316 {
8b127cbc 14317 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
14318
14319 if (hexval != 0)
14320 {
14321 if (tf->with == with_flags)
14322 with_hex |= hexval;
14323 else if (tf->with == without_flags)
14324 without_hex |= hexval;
0a1b45a2 14325 tf->valid = true;
b9c361e0
JL
14326 continue;
14327 }
ae17ab41 14328 }
8b127cbc 14329 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 14330 {
8b127cbc 14331 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
14332 {
14333 if (tf->with == with_flags)
14334 with_hex |= elf_flags_to_names[i].flag_value;
14335 else if (tf->with == without_flags)
14336 without_hex |= elf_flags_to_names[i].flag_value;
0a1b45a2 14337 tf->valid = true;
b9c361e0
JL
14338 break;
14339 }
14340 }
8b127cbc 14341 if (!tf->valid)
b9c361e0 14342 {
68ffbac6 14343 info->callbacks->einfo
9793eb77 14344 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
0a1b45a2 14345 return false;
ae17ab41
CM
14346 }
14347 }
0a1b45a2 14348 flaginfo->flags_initialized = true;
8b127cbc
AM
14349 flaginfo->only_with_flags |= with_hex;
14350 flaginfo->not_with_flags |= without_hex;
ae17ab41 14351 }
ae17ab41 14352
8b127cbc 14353 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
0a1b45a2 14354 return false;
b9c361e0 14355
8b127cbc 14356 if ((flaginfo->not_with_flags & sh_flags) != 0)
0a1b45a2 14357 return false;
b9c361e0 14358
0a1b45a2 14359 return true;
ae17ab41
CM
14360}
14361
c152c796
AM
14362struct alloc_got_off_arg {
14363 bfd_vma gotoff;
10455f89 14364 struct bfd_link_info *info;
c152c796
AM
14365};
14366
14367/* We need a special top-level link routine to convert got reference counts
14368 to real got offsets. */
14369
0a1b45a2 14370static bool
c152c796
AM
14371elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14372{
a50b1753 14373 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
14374 bfd *obfd = gofarg->info->output_bfd;
14375 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 14376
c152c796
AM
14377 if (h->got.refcount > 0)
14378 {
14379 h->got.offset = gofarg->gotoff;
10455f89 14380 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
14381 }
14382 else
14383 h->got.offset = (bfd_vma) -1;
14384
0a1b45a2 14385 return true;
c152c796
AM
14386}
14387
14388/* And an accompanying bit to work out final got entry offsets once
14389 we're done. Should be called from final_link. */
14390
0a1b45a2 14391bool
c152c796
AM
14392bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14393 struct bfd_link_info *info)
14394{
14395 bfd *i;
14396 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14397 bfd_vma gotoff;
c152c796
AM
14398 struct alloc_got_off_arg gofarg;
14399
10455f89
HPN
14400 BFD_ASSERT (abfd == info->output_bfd);
14401
c152c796 14402 if (! is_elf_hash_table (info->hash))
0a1b45a2 14403 return false;
c152c796
AM
14404
14405 /* The GOT offset is relative to the .got section, but the GOT header is
14406 put into the .got.plt section, if the backend uses it. */
14407 if (bed->want_got_plt)
14408 gotoff = 0;
14409 else
14410 gotoff = bed->got_header_size;
14411
14412 /* Do the local .got entries first. */
c72f2fb2 14413 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
14414 {
14415 bfd_signed_vma *local_got;
ef53be89 14416 size_t j, locsymcount;
c152c796
AM
14417 Elf_Internal_Shdr *symtab_hdr;
14418
14419 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14420 continue;
14421
14422 local_got = elf_local_got_refcounts (i);
14423 if (!local_got)
14424 continue;
14425
14426 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14427 if (elf_bad_symtab (i))
14428 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14429 else
14430 locsymcount = symtab_hdr->sh_info;
14431
14432 for (j = 0; j < locsymcount; ++j)
14433 {
14434 if (local_got[j] > 0)
14435 {
14436 local_got[j] = gotoff;
10455f89 14437 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
14438 }
14439 else
14440 local_got[j] = (bfd_vma) -1;
14441 }
14442 }
14443
14444 /* Then the global .got entries. .plt refcounts are handled by
14445 adjust_dynamic_symbol */
14446 gofarg.gotoff = gotoff;
10455f89 14447 gofarg.info = info;
c152c796
AM
14448 elf_link_hash_traverse (elf_hash_table (info),
14449 elf_gc_allocate_got_offsets,
14450 &gofarg);
0a1b45a2 14451 return true;
c152c796
AM
14452}
14453
14454/* Many folk need no more in the way of final link than this, once
14455 got entry reference counting is enabled. */
14456
0a1b45a2 14457bool
c152c796
AM
14458bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14459{
14460 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
0a1b45a2 14461 return false;
c152c796
AM
14462
14463 /* Invoke the regular ELF backend linker to do all the work. */
14464 return bfd_elf_final_link (abfd, info);
14465}
14466
0a1b45a2 14467bool
c152c796
AM
14468bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14469{
a50b1753 14470 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
14471
14472 if (rcookie->bad_symtab)
14473 rcookie->rel = rcookie->rels;
14474
14475 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14476 {
14477 unsigned long r_symndx;
14478
14479 if (! rcookie->bad_symtab)
14480 if (rcookie->rel->r_offset > offset)
0a1b45a2 14481 return false;
c152c796
AM
14482 if (rcookie->rel->r_offset != offset)
14483 continue;
14484
14485 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 14486 if (r_symndx == STN_UNDEF)
0a1b45a2 14487 return true;
c152c796
AM
14488
14489 if (r_symndx >= rcookie->locsymcount
14490 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14491 {
14492 struct elf_link_hash_entry *h;
14493
14494 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14495
14496 while (h->root.type == bfd_link_hash_indirect
14497 || h->root.type == bfd_link_hash_warning)
14498 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14499
14500 if ((h->root.type == bfd_link_hash_defined
14501 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
14502 && (h->root.u.def.section->owner != rcookie->abfd
14503 || h->root.u.def.section->kept_section != NULL
14504 || discarded_section (h->root.u.def.section)))
0a1b45a2 14505 return true;
c152c796
AM
14506 }
14507 else
14508 {
14509 /* It's not a relocation against a global symbol,
14510 but it could be a relocation against a local
14511 symbol for a discarded section. */
14512 asection *isec;
14513 Elf_Internal_Sym *isym;
14514
14515 /* Need to: get the symbol; get the section. */
14516 isym = &rcookie->locsyms[r_symndx];
cb33740c 14517 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
14518 if (isec != NULL
14519 && (isec->kept_section != NULL
14520 || discarded_section (isec)))
0a1b45a2 14521 return true;
c152c796 14522 }
0a1b45a2 14523 return false;
c152c796 14524 }
0a1b45a2 14525 return false;
c152c796
AM
14526}
14527
14528/* Discard unneeded references to discarded sections.
75938853
AM
14529 Returns -1 on error, 1 if any section's size was changed, 0 if
14530 nothing changed. This function assumes that the relocations are in
14531 sorted order, which is true for all known assemblers. */
c152c796 14532
75938853 14533int
c152c796
AM
14534bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14535{
14536 struct elf_reloc_cookie cookie;
18cd5bce 14537 asection *o;
c152c796 14538 bfd *abfd;
75938853 14539 int changed = 0;
c152c796
AM
14540
14541 if (info->traditional_format
14542 || !is_elf_hash_table (info->hash))
75938853 14543 return 0;
c152c796 14544
18cd5bce
AM
14545 o = bfd_get_section_by_name (output_bfd, ".stab");
14546 if (o != NULL)
c152c796 14547 {
18cd5bce 14548 asection *i;
c152c796 14549
18cd5bce 14550 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 14551 {
18cd5bce
AM
14552 if (i->size == 0
14553 || i->reloc_count == 0
14554 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14555 continue;
c152c796 14556
18cd5bce
AM
14557 abfd = i->owner;
14558 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14559 continue;
c152c796 14560
18cd5bce 14561 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14562 return -1;
c152c796 14563
18cd5bce
AM
14564 if (_bfd_discard_section_stabs (abfd, i,
14565 elf_section_data (i)->sec_info,
5241d853
RS
14566 bfd_elf_reloc_symbol_deleted_p,
14567 &cookie))
75938853 14568 changed = 1;
18cd5bce
AM
14569
14570 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14571 }
18cd5bce
AM
14572 }
14573
2f0c68f2
CM
14574 o = NULL;
14575 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14576 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
14577 if (o != NULL)
14578 {
14579 asection *i;
d7153c4a 14580 int eh_changed = 0;
66631823 14581 unsigned int eh_alignment; /* Octets. */
c152c796 14582
18cd5bce 14583 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 14584 {
18cd5bce
AM
14585 if (i->size == 0)
14586 continue;
14587
14588 abfd = i->owner;
14589 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14590 continue;
14591
14592 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14593 return -1;
18cd5bce
AM
14594
14595 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14596 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
14597 bfd_elf_reloc_symbol_deleted_p,
14598 &cookie))
d7153c4a
AM
14599 {
14600 eh_changed = 1;
14601 if (i->size != i->rawsize)
14602 changed = 1;
14603 }
18cd5bce
AM
14604
14605 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14606 }
9866ffe2 14607
66631823
CE
14608 eh_alignment = ((1 << o->alignment_power)
14609 * bfd_octets_per_byte (output_bfd, o));
9866ffe2
AM
14610 /* Skip over zero terminator, and prevent empty sections from
14611 adding alignment padding at the end. */
14612 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14613 if (i->size == 0)
14614 i->flags |= SEC_EXCLUDE;
14615 else if (i->size > 4)
14616 break;
14617 /* The last non-empty eh_frame section doesn't need padding. */
14618 if (i != NULL)
14619 i = i->map_tail.s;
14620 /* Any prior sections must pad the last FDE out to the output
14621 section alignment. Otherwise we might have zero padding
14622 between sections, which would be seen as a terminator. */
14623 for (; i != NULL; i = i->map_tail.s)
14624 if (i->size == 4)
14625 /* All but the last zero terminator should have been removed. */
14626 BFD_FAIL ();
14627 else
14628 {
14629 bfd_size_type size
14630 = (i->size + eh_alignment - 1) & -eh_alignment;
14631 if (i->size != size)
af471f82 14632 {
9866ffe2
AM
14633 i->size = size;
14634 changed = 1;
14635 eh_changed = 1;
af471f82 14636 }
9866ffe2 14637 }
d7153c4a
AM
14638 if (eh_changed)
14639 elf_link_hash_traverse (elf_hash_table (info),
14640 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
18cd5bce 14641 }
c152c796 14642
18cd5bce
AM
14643 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14644 {
14645 const struct elf_backend_data *bed;
57963c05 14646 asection *s;
c152c796 14647
18cd5bce
AM
14648 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14649 continue;
57963c05
AM
14650 s = abfd->sections;
14651 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14652 continue;
18cd5bce
AM
14653
14654 bed = get_elf_backend_data (abfd);
14655
14656 if (bed->elf_backend_discard_info != NULL)
14657 {
14658 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 14659 return -1;
18cd5bce
AM
14660
14661 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 14662 changed = 1;
18cd5bce
AM
14663
14664 fini_reloc_cookie (&cookie, abfd);
14665 }
c152c796
AM
14666 }
14667
2f0c68f2
CM
14668 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14669 _bfd_elf_end_eh_frame_parsing (info);
14670
14671 if (info->eh_frame_hdr_type
0e1862bb 14672 && !bfd_link_relocatable (info)
c152c796 14673 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 14674 changed = 1;
c152c796 14675
75938853 14676 return changed;
c152c796 14677}
082b7297 14678
0a1b45a2 14679bool
0c511000 14680_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 14681 asection *sec,
c0f00686 14682 struct bfd_link_info *info)
082b7297
L
14683{
14684 flagword flags;
c77ec726 14685 const char *name, *key;
082b7297
L
14686 struct bfd_section_already_linked *l;
14687 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 14688
c77ec726 14689 if (sec->output_section == bfd_abs_section_ptr)
0a1b45a2 14690 return false;
0c511000 14691
c77ec726 14692 flags = sec->flags;
0c511000 14693
c77ec726
AM
14694 /* Return if it isn't a linkonce section. A comdat group section
14695 also has SEC_LINK_ONCE set. */
14696 if ((flags & SEC_LINK_ONCE) == 0)
0a1b45a2 14697 return false;
0c511000 14698
c77ec726
AM
14699 /* Don't put group member sections on our list of already linked
14700 sections. They are handled as a group via their group section. */
14701 if (elf_sec_group (sec) != NULL)
0a1b45a2 14702 return false;
0c511000 14703
c77ec726
AM
14704 /* For a SHT_GROUP section, use the group signature as the key. */
14705 name = sec->name;
14706 if ((flags & SEC_GROUP) != 0
14707 && elf_next_in_group (sec) != NULL
14708 && elf_group_name (elf_next_in_group (sec)) != NULL)
14709 key = elf_group_name (elf_next_in_group (sec));
14710 else
14711 {
14712 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
08dedd66 14713 if (startswith (name, ".gnu.linkonce.")
c77ec726
AM
14714 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14715 key++;
0c511000 14716 else
c77ec726
AM
14717 /* Must be a user linkonce section that doesn't follow gcc's
14718 naming convention. In this case we won't be matching
14719 single member groups. */
14720 key = name;
0c511000 14721 }
6d2cd210 14722
c77ec726 14723 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
14724
14725 for (l = already_linked_list->entry; l != NULL; l = l->next)
14726 {
c2370991 14727 /* We may have 2 different types of sections on the list: group
c77ec726
AM
14728 sections with a signature of <key> (<key> is some string),
14729 and linkonce sections named .gnu.linkonce.<type>.<key>.
14730 Match like sections. LTO plugin sections are an exception.
14731 They are always named .gnu.linkonce.t.<key> and match either
14732 type of section. */
14733 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14734 && ((flags & SEC_GROUP) != 0
14735 || strcmp (name, l->sec->name) == 0))
e8a83e93
MB
14736 || (l->sec->owner->flags & BFD_PLUGIN) != 0
14737 || (sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
14738 {
14739 /* The section has already been linked. See if we should
6d2cd210 14740 issue a warning. */
c77ec726 14741 if (!_bfd_handle_already_linked (sec, l, info))
0a1b45a2 14742 return false;
082b7297 14743
c77ec726 14744 if (flags & SEC_GROUP)
3d7f7666 14745 {
c77ec726
AM
14746 asection *first = elf_next_in_group (sec);
14747 asection *s = first;
3d7f7666 14748
c77ec726 14749 while (s != NULL)
3d7f7666 14750 {
c77ec726
AM
14751 s->output_section = bfd_abs_section_ptr;
14752 /* Record which group discards it. */
14753 s->kept_section = l->sec;
14754 s = elf_next_in_group (s);
14755 /* These lists are circular. */
14756 if (s == first)
14757 break;
3d7f7666
L
14758 }
14759 }
082b7297 14760
0a1b45a2 14761 return true;
082b7297
L
14762 }
14763 }
14764
c77ec726
AM
14765 /* A single member comdat group section may be discarded by a
14766 linkonce section and vice versa. */
14767 if ((flags & SEC_GROUP) != 0)
3d7f7666 14768 {
c77ec726 14769 asection *first = elf_next_in_group (sec);
c2370991 14770
c77ec726
AM
14771 if (first != NULL && elf_next_in_group (first) == first)
14772 /* Check this single member group against linkonce sections. */
14773 for (l = already_linked_list->entry; l != NULL; l = l->next)
14774 if ((l->sec->flags & SEC_GROUP) == 0
14775 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14776 {
14777 first->output_section = bfd_abs_section_ptr;
14778 first->kept_section = l->sec;
14779 sec->output_section = bfd_abs_section_ptr;
14780 break;
14781 }
14782 }
14783 else
14784 /* Check this linkonce section against single member groups. */
14785 for (l = already_linked_list->entry; l != NULL; l = l->next)
14786 if (l->sec->flags & SEC_GROUP)
6d2cd210 14787 {
c77ec726 14788 asection *first = elf_next_in_group (l->sec);
6d2cd210 14789
c77ec726
AM
14790 if (first != NULL
14791 && elf_next_in_group (first) == first
14792 && bfd_elf_match_symbols_in_sections (first, sec, info))
14793 {
14794 sec->output_section = bfd_abs_section_ptr;
14795 sec->kept_section = first;
14796 break;
14797 }
6d2cd210 14798 }
0c511000 14799
c77ec726
AM
14800 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14801 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14802 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14803 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14804 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14805 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14806 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14807 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14808 The reverse order cannot happen as there is never a bfd with only the
14809 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14810 matter as here were are looking only for cross-bfd sections. */
14811
08dedd66 14812 if ((flags & SEC_GROUP) == 0 && startswith (name, ".gnu.linkonce.r."))
c77ec726
AM
14813 for (l = already_linked_list->entry; l != NULL; l = l->next)
14814 if ((l->sec->flags & SEC_GROUP) == 0
08dedd66 14815 && startswith (l->sec->name, ".gnu.linkonce.t."))
c77ec726
AM
14816 {
14817 if (abfd != l->sec->owner)
14818 sec->output_section = bfd_abs_section_ptr;
14819 break;
14820 }
80c29487 14821
082b7297 14822 /* This is the first section with this name. Record it. */
c77ec726 14823 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 14824 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 14825 return sec->output_section == bfd_abs_section_ptr;
082b7297 14826}
81e1b023 14827
0a1b45a2 14828bool
a4d8e49b
L
14829_bfd_elf_common_definition (Elf_Internal_Sym *sym)
14830{
14831 return sym->st_shndx == SHN_COMMON;
14832}
14833
14834unsigned int
14835_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14836{
14837 return SHN_COMMON;
14838}
14839
14840asection *
14841_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14842{
14843 return bfd_com_section_ptr;
14844}
10455f89
HPN
14845
14846bfd_vma
14847_bfd_elf_default_got_elt_size (bfd *abfd,
14848 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14849 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14850 bfd *ibfd ATTRIBUTE_UNUSED,
14851 unsigned long symndx ATTRIBUTE_UNUSED)
14852{
14853 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14854 return bed->s->arch_size / 8;
14855}
83bac4b0
NC
14856
14857/* Routines to support the creation of dynamic relocs. */
14858
83bac4b0
NC
14859/* Returns the name of the dynamic reloc section associated with SEC. */
14860
14861static const char *
14862get_dynamic_reloc_section_name (bfd * abfd,
14863 asection * sec,
0a1b45a2 14864 bool is_rela)
83bac4b0 14865{
ddcf1fcf 14866 char *name;
fd361982 14867 const char *old_name = bfd_section_name (sec);
ddcf1fcf 14868 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 14869
ddcf1fcf 14870 if (old_name == NULL)
83bac4b0
NC
14871 return NULL;
14872
ddcf1fcf 14873 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 14874 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
14875
14876 return name;
14877}
14878
14879/* Returns the dynamic reloc section associated with SEC.
14880 If necessary compute the name of the dynamic reloc section based
14881 on SEC's name (looked up in ABFD's string table) and the setting
14882 of IS_RELA. */
14883
14884asection *
0a1b45a2
AM
14885_bfd_elf_get_dynamic_reloc_section (bfd *abfd,
14886 asection *sec,
14887 bool is_rela)
83bac4b0 14888{
0a1b45a2 14889 asection *reloc_sec = elf_section_data (sec)->sreloc;
83bac4b0
NC
14890
14891 if (reloc_sec == NULL)
14892 {
0a1b45a2 14893 const char *name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
83bac4b0
NC
14894
14895 if (name != NULL)
14896 {
3d4d4302 14897 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
14898
14899 if (reloc_sec != NULL)
14900 elf_section_data (sec)->sreloc = reloc_sec;
14901 }
14902 }
14903
14904 return reloc_sec;
14905}
14906
14907/* Returns the dynamic reloc section associated with SEC. If the
14908 section does not exist it is created and attached to the DYNOBJ
14909 bfd and stored in the SRELOC field of SEC's elf_section_data
14910 structure.
f8076f98 14911
83bac4b0
NC
14912 ALIGNMENT is the alignment for the newly created section and
14913 IS_RELA defines whether the name should be .rela.<SEC's name>
14914 or .rel.<SEC's name>. The section name is looked up in the
14915 string table associated with ABFD. */
14916
14917asection *
ca4be51c
AM
14918_bfd_elf_make_dynamic_reloc_section (asection *sec,
14919 bfd *dynobj,
14920 unsigned int alignment,
14921 bfd *abfd,
0a1b45a2 14922 bool is_rela)
83bac4b0
NC
14923{
14924 asection * reloc_sec = elf_section_data (sec)->sreloc;
14925
14926 if (reloc_sec == NULL)
14927 {
14928 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14929
14930 if (name == NULL)
14931 return NULL;
14932
3d4d4302 14933 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
14934
14935 if (reloc_sec == NULL)
14936 {
3d4d4302
AM
14937 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14938 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
14939 if ((sec->flags & SEC_ALLOC) != 0)
14940 flags |= SEC_ALLOC | SEC_LOAD;
14941
3d4d4302 14942 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
14943 if (reloc_sec != NULL)
14944 {
8877b5e5
AM
14945 /* _bfd_elf_get_sec_type_attr chooses a section type by
14946 name. Override as it may be wrong, eg. for a user
14947 section named "auto" we'll get ".relauto" which is
14948 seen to be a .rela section. */
14949 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
fd361982 14950 if (!bfd_set_section_alignment (reloc_sec, alignment))
83bac4b0
NC
14951 reloc_sec = NULL;
14952 }
14953 }
14954
14955 elf_section_data (sec)->sreloc = reloc_sec;
14956 }
14957
14958 return reloc_sec;
14959}
1338dd10 14960
bffebb6b
AM
14961/* Copy the ELF symbol type and other attributes for a linker script
14962 assignment from HSRC to HDEST. Generally this should be treated as
14963 if we found a strong non-dynamic definition for HDEST (except that
14964 ld ignores multiple definition errors). */
1338dd10 14965void
bffebb6b
AM
14966_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14967 struct bfd_link_hash_entry *hdest,
14968 struct bfd_link_hash_entry *hsrc)
1338dd10 14969{
bffebb6b
AM
14970 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14971 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14972 Elf_Internal_Sym isym;
1338dd10
PB
14973
14974 ehdest->type = ehsrc->type;
35fc36a8 14975 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
14976
14977 isym.st_other = ehsrc->other;
0a1b45a2 14978 elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, true, false);
1338dd10 14979}
351f65ca
L
14980
14981/* Append a RELA relocation REL to section S in BFD. */
14982
14983void
14984elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14985{
14986 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14987 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14988 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14989 bed->s->swap_reloca_out (abfd, rel, loc);
14990}
14991
14992/* Append a REL relocation REL to section S in BFD. */
14993
14994void
14995elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14996{
14997 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14998 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14999 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 15000 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 15001}
7dba9362
AM
15002
15003/* Define __start, __stop, .startof. or .sizeof. symbol. */
15004
15005struct bfd_link_hash_entry *
15006bfd_elf_define_start_stop (struct bfd_link_info *info,
15007 const char *symbol, asection *sec)
15008{
487b6440 15009 struct elf_link_hash_entry *h;
7dba9362 15010
487b6440 15011 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
0a1b45a2 15012 false, false, true);
e1b5d517 15013 /* NB: Common symbols will be turned into definition later. */
487b6440 15014 if (h != NULL
8ee10e86 15015 && !h->root.ldscript_def
487b6440
AM
15016 && (h->root.type == bfd_link_hash_undefined
15017 || h->root.type == bfd_link_hash_undefweak
e1b5d517
L
15018 || ((h->ref_regular || h->def_dynamic)
15019 && !h->def_regular
15020 && h->root.type != bfd_link_hash_common)))
7dba9362 15021 {
0a1b45a2 15022 bool was_dynamic = h->ref_dynamic || h->def_dynamic;
e1b5d517 15023 h->verinfo.verdef = NULL;
487b6440
AM
15024 h->root.type = bfd_link_hash_defined;
15025 h->root.u.def.section = sec;
15026 h->root.u.def.value = 0;
15027 h->def_regular = 1;
15028 h->def_dynamic = 0;
15029 h->start_stop = 1;
15030 h->u2.start_stop_section = sec;
15031 if (symbol[0] == '.')
15032 {
15033 /* .startof. and .sizeof. symbols are local. */
559192d8
AM
15034 const struct elf_backend_data *bed;
15035 bed = get_elf_backend_data (info->output_bfd);
0a1b45a2 15036 (*bed->elf_backend_hide_symbol) (info, h, true);
487b6440 15037 }
36b8fda5
AM
15038 else
15039 {
15040 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
cae64165
RM
15041 h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15042 | info->start_stop_visibility);
bf3077a6 15043 if (was_dynamic)
36b8fda5
AM
15044 bfd_elf_link_record_dynamic_symbol (info, h);
15045 }
487b6440 15046 return &h->root;
7dba9362 15047 }
487b6440 15048 return NULL;
7dba9362 15049}
5dbc8b37
L
15050
15051/* Find dynamic relocs for H that apply to read-only sections. */
15052
15053asection *
15054_bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15055{
15056 struct elf_dyn_relocs *p;
15057
15058 for (p = h->dyn_relocs; p != NULL; p = p->next)
15059 {
15060 asection *s = p->sec->output_section;
15061
15062 if (s != NULL && (s->flags & SEC_READONLY) != 0)
15063 return p->sec;
15064 }
15065 return NULL;
15066}
d49e5065
L
15067
15068/* Set DF_TEXTREL if we find any dynamic relocs that apply to
15069 read-only sections. */
15070
0a1b45a2 15071bool
d49e5065
L
15072_bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15073{
15074 asection *sec;
15075
15076 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 15077 return true;
d49e5065
L
15078
15079 sec = _bfd_elf_readonly_dynrelocs (h);
15080 if (sec != NULL)
15081 {
15082 struct bfd_link_info *info = (struct bfd_link_info *) inf;
15083
15084 info->flags |= DF_TEXTREL;
15085 /* xgettext:c-format */
15086 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15087 "in read-only section `%pA'\n"),
15088 sec->owner, h->root.root.string, sec);
15089
15090 if (bfd_link_textrel_check (info))
15091 /* xgettext:c-format */
15092 info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15093 "in read-only section `%pA'\n"),
15094 sec->owner, h->root.root.string, sec);
15095
15096 /* Not an error, just cut short the traversal. */
0a1b45a2 15097 return false;
d49e5065 15098 }
0a1b45a2 15099 return true;
d49e5065 15100}
3084d7a2
L
15101
15102/* Add dynamic tags. */
15103
0a1b45a2 15104bool
3084d7a2 15105_bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
0a1b45a2 15106 bool need_dynamic_reloc)
3084d7a2
L
15107{
15108 struct elf_link_hash_table *htab = elf_hash_table (info);
15109
15110 if (htab->dynamic_sections_created)
15111 {
15112 /* Add some entries to the .dynamic section. We fill in the
15113 values later, in finish_dynamic_sections, but we must add
15114 the entries now so that we get the correct size for the
15115 .dynamic section. The DT_DEBUG entry is filled in by the
15116 dynamic linker and used by the debugger. */
15117#define add_dynamic_entry(TAG, VAL) \
15118 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15119
15120 const struct elf_backend_data *bed
15121 = get_elf_backend_data (output_bfd);
15122
15123 if (bfd_link_executable (info))
15124 {
15125 if (!add_dynamic_entry (DT_DEBUG, 0))
0a1b45a2 15126 return false;
3084d7a2
L
15127 }
15128
15129 if (htab->dt_pltgot_required || htab->splt->size != 0)
15130 {
15131 /* DT_PLTGOT is used by prelink even if there is no PLT
15132 relocation. */
15133 if (!add_dynamic_entry (DT_PLTGOT, 0))
0a1b45a2 15134 return false;
3084d7a2
L
15135 }
15136
15137 if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15138 {
15139 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15140 || !add_dynamic_entry (DT_PLTREL,
15141 (bed->rela_plts_and_copies_p
15142 ? DT_RELA : DT_REL))
15143 || !add_dynamic_entry (DT_JMPREL, 0))
0a1b45a2 15144 return false;
3084d7a2
L
15145 }
15146
15147 if (htab->tlsdesc_plt
15148 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15149 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
0a1b45a2 15150 return false;
3084d7a2
L
15151
15152 if (need_dynamic_reloc)
15153 {
15154 if (bed->rela_plts_and_copies_p)
15155 {
15156 if (!add_dynamic_entry (DT_RELA, 0)
15157 || !add_dynamic_entry (DT_RELASZ, 0)
15158 || !add_dynamic_entry (DT_RELAENT,
15159 bed->s->sizeof_rela))
0a1b45a2 15160 return false;
3084d7a2
L
15161 }
15162 else
15163 {
15164 if (!add_dynamic_entry (DT_REL, 0)
15165 || !add_dynamic_entry (DT_RELSZ, 0)
15166 || !add_dynamic_entry (DT_RELENT,
15167 bed->s->sizeof_rel))
0a1b45a2 15168 return false;
3084d7a2
L
15169 }
15170
15171 /* If any dynamic relocs apply to a read-only section,
15172 then we need a DT_TEXTREL entry. */
15173 if ((info->flags & DF_TEXTREL) == 0)
15174 elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15175 info);
15176
15177 if ((info->flags & DF_TEXTREL) != 0)
15178 {
15179 if (htab->ifunc_resolvers)
15180 info->callbacks->einfo
15181 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15182 "may result in a segfault at runtime; recompile with %s\n"),
15183 bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15184
15185 if (!add_dynamic_entry (DT_TEXTREL, 0))
0a1b45a2 15186 return false;
3084d7a2
L
15187 }
15188 }
15189 }
15190#undef add_dynamic_entry
15191
0a1b45a2 15192 return true;
3084d7a2 15193}
This page took 4.259304 seconds and 4 git commands to generate.