]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elflink.c
elf: Add GNU_PROPERTY_1_NEEDED check
[thirdparty/binutils-gdb.git] / bfd / elflink.c
CommitLineData
252b5132 1/* ELF linking support for BFD.
250d07de 2 Copyright (C) 1995-2021 Free Software Foundation, Inc.
252b5132 3
8fdd7217 4 This file is part of BFD, the Binary File Descriptor library.
252b5132 5
8fdd7217
NC
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
cd123cb7 8 the Free Software Foundation; either version 3 of the License, or
8fdd7217 9 (at your option) any later version.
252b5132 10
8fdd7217
NC
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
252b5132 15
8fdd7217
NC
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
cd123cb7
NC
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
252b5132 20
252b5132 21#include "sysdep.h"
3db64b00 22#include "bfd.h"
252b5132
RH
23#include "bfdlink.h"
24#include "libbfd.h"
25#define ARCH_SIZE 0
26#include "elf-bfd.h"
4ad4eba5 27#include "safe-ctype.h"
ccf2f652 28#include "libiberty.h"
66eb6687 29#include "objalloc.h"
08ce1d72 30#if BFD_SUPPORTS_PLUGINS
7d0b9ebc 31#include "plugin-api.h"
7dc3990e
L
32#include "plugin.h"
33#endif
252b5132 34
4b69ce9b 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
a8dde0a2
L
2622 RELA_HDR relocations. If INFO isn't NULL and KEEP_MEMORY is true,
2623 update cache_size. */
45d6a902
AM
2624
2625Elf_Internal_Rela *
a8dde0a2
L
2626_bfd_elf_link_info_read_relocs (bfd *abfd,
2627 struct bfd_link_info *info,
2628 asection *o,
2629 void *external_relocs,
2630 Elf_Internal_Rela *internal_relocs,
2631 bool keep_memory)
45d6a902 2632{
268b6b39 2633 void *alloc1 = NULL;
45d6a902 2634 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2635 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2636 struct bfd_elf_section_data *esdo = elf_section_data (o);
2637 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2638
d4730f92
BS
2639 if (esdo->relocs != NULL)
2640 return esdo->relocs;
45d6a902
AM
2641
2642 if (o->reloc_count == 0)
2643 return NULL;
2644
45d6a902
AM
2645 if (internal_relocs == NULL)
2646 {
2647 bfd_size_type size;
2648
056bafd4 2649 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
45d6a902 2650 if (keep_memory)
a8dde0a2
L
2651 {
2652 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2653 if (info)
2654 info->cache_size += size;
2655 }
45d6a902 2656 else
a50b1753 2657 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2658 if (internal_relocs == NULL)
2659 goto error_return;
2660 }
2661
2662 if (external_relocs == NULL)
2663 {
d4730f92
BS
2664 bfd_size_type size = 0;
2665
2666 if (esdo->rel.hdr)
2667 size += esdo->rel.hdr->sh_size;
2668 if (esdo->rela.hdr)
2669 size += esdo->rela.hdr->sh_size;
45d6a902 2670
268b6b39 2671 alloc1 = bfd_malloc (size);
45d6a902
AM
2672 if (alloc1 == NULL)
2673 goto error_return;
2674 external_relocs = alloc1;
2675 }
2676
d4730f92
BS
2677 internal_rela_relocs = internal_relocs;
2678 if (esdo->rel.hdr)
2679 {
2680 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2681 external_relocs,
2682 internal_relocs))
2683 goto error_return;
2684 external_relocs = (((bfd_byte *) external_relocs)
2685 + esdo->rel.hdr->sh_size);
2686 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2687 * bed->s->int_rels_per_ext_rel);
2688 }
2689
2690 if (esdo->rela.hdr
2691 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2692 external_relocs,
2693 internal_rela_relocs)))
45d6a902
AM
2694 goto error_return;
2695
2696 /* Cache the results for next time, if we can. */
2697 if (keep_memory)
d4730f92 2698 esdo->relocs = internal_relocs;
45d6a902 2699
c9594989 2700 free (alloc1);
45d6a902
AM
2701
2702 /* Don't free alloc2, since if it was allocated we are passing it
2703 back (under the name of internal_relocs). */
2704
2705 return internal_relocs;
2706
2707 error_return:
c9594989 2708 free (alloc1);
45d6a902 2709 if (alloc2 != NULL)
4dd07732
AM
2710 {
2711 if (keep_memory)
2712 bfd_release (abfd, alloc2);
2713 else
2714 free (alloc2);
2715 }
45d6a902
AM
2716 return NULL;
2717}
2718
a8dde0a2
L
2719/* This is similar to _bfd_elf_link_info_read_relocs, except for that
2720 NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
2721 struct bfd_link_info. */
2722
2723Elf_Internal_Rela *
2724_bfd_elf_link_read_relocs (bfd *abfd,
2725 asection *o,
2726 void *external_relocs,
2727 Elf_Internal_Rela *internal_relocs,
2728 bool keep_memory)
2729{
2730 return _bfd_elf_link_info_read_relocs (abfd, NULL, o, external_relocs,
2731 internal_relocs, keep_memory);
2732
2733}
2734
45d6a902
AM
2735/* Compute the size of, and allocate space for, REL_HDR which is the
2736 section header for a section containing relocations for O. */
2737
0a1b45a2 2738static bool
9eaff861
AO
2739_bfd_elf_link_size_reloc_section (bfd *abfd,
2740 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2741{
9eaff861 2742 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2743
2744 /* That allows us to calculate the size of the section. */
9eaff861 2745 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2746
2747 /* The contents field must last into write_object_contents, so we
2748 allocate it with bfd_alloc rather than malloc. Also since we
2749 cannot be sure that the contents will actually be filled in,
2750 we zero the allocated space. */
a50b1753 2751 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902 2752 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
0a1b45a2 2753 return false;
45d6a902 2754
d4730f92 2755 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2756 {
2757 struct elf_link_hash_entry **p;
2758
ca4be51c
AM
2759 p = ((struct elf_link_hash_entry **)
2760 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902 2761 if (p == NULL)
0a1b45a2 2762 return false;
45d6a902 2763
d4730f92 2764 reldata->hashes = p;
45d6a902
AM
2765 }
2766
0a1b45a2 2767 return true;
45d6a902
AM
2768}
2769
2770/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2771 originated from the section given by INPUT_REL_HDR) to the
2772 OUTPUT_BFD. */
2773
0a1b45a2 2774bool
268b6b39
AM
2775_bfd_elf_link_output_relocs (bfd *output_bfd,
2776 asection *input_section,
2777 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2778 Elf_Internal_Rela *internal_relocs,
2779 struct elf_link_hash_entry **rel_hash
2780 ATTRIBUTE_UNUSED)
45d6a902
AM
2781{
2782 Elf_Internal_Rela *irela;
2783 Elf_Internal_Rela *irelaend;
2784 bfd_byte *erel;
d4730f92 2785 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2786 asection *output_section;
9c5bfbb7 2787 const struct elf_backend_data *bed;
268b6b39 2788 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2789 struct bfd_elf_section_data *esdo;
45d6a902
AM
2790
2791 output_section = input_section->output_section;
45d6a902 2792
d4730f92
BS
2793 bed = get_elf_backend_data (output_bfd);
2794 esdo = elf_section_data (output_section);
2795 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2796 {
d4730f92
BS
2797 output_reldata = &esdo->rel;
2798 swap_out = bed->s->swap_reloc_out;
45d6a902 2799 }
d4730f92
BS
2800 else if (esdo->rela.hdr
2801 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2802 {
d4730f92
BS
2803 output_reldata = &esdo->rela;
2804 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2805 }
2806 else
2807 {
4eca0228 2808 _bfd_error_handler
695344c0 2809 /* xgettext:c-format */
871b3ab2 2810 (_("%pB: relocation size mismatch in %pB section %pA"),
d003868e 2811 output_bfd, input_section->owner, input_section);
297d8443 2812 bfd_set_error (bfd_error_wrong_format);
0a1b45a2 2813 return false;
45d6a902
AM
2814 }
2815
d4730f92
BS
2816 erel = output_reldata->hdr->contents;
2817 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2818 irela = internal_relocs;
2819 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2820 * bed->s->int_rels_per_ext_rel);
2821 while (irela < irelaend)
2822 {
2823 (*swap_out) (output_bfd, irela, erel);
2824 irela += bed->s->int_rels_per_ext_rel;
2825 erel += input_rel_hdr->sh_entsize;
2826 }
2827
2828 /* Bump the counter, so that we know where to add the next set of
2829 relocations. */
d4730f92 2830 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902 2831
0a1b45a2 2832 return true;
45d6a902
AM
2833}
2834\f
508c3946
L
2835/* Make weak undefined symbols in PIE dynamic. */
2836
0a1b45a2 2837bool
508c3946
L
2838_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2839 struct elf_link_hash_entry *h)
2840{
0e1862bb 2841 if (bfd_link_pie (info)
508c3946
L
2842 && h->dynindx == -1
2843 && h->root.type == bfd_link_hash_undefweak)
2844 return bfd_elf_link_record_dynamic_symbol (info, h);
2845
0a1b45a2 2846 return true;
508c3946
L
2847}
2848
45d6a902
AM
2849/* Fix up the flags for a symbol. This handles various cases which
2850 can only be fixed after all the input files are seen. This is
2851 currently called by both adjust_dynamic_symbol and
2852 assign_sym_version, which is unnecessary but perhaps more robust in
2853 the face of future changes. */
2854
0a1b45a2 2855static bool
268b6b39
AM
2856_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2857 struct elf_info_failed *eif)
45d6a902 2858{
33774f08 2859 const struct elf_backend_data *bed;
508c3946 2860
45d6a902
AM
2861 /* If this symbol was mentioned in a non-ELF file, try to set
2862 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2863 permit a non-ELF file to correctly refer to a symbol defined in
2864 an ELF dynamic object. */
f5385ebf 2865 if (h->non_elf)
45d6a902
AM
2866 {
2867 while (h->root.type == bfd_link_hash_indirect)
2868 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2869
2870 if (h->root.type != bfd_link_hash_defined
2871 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2872 {
2873 h->ref_regular = 1;
2874 h->ref_regular_nonweak = 1;
2875 }
45d6a902
AM
2876 else
2877 {
2878 if (h->root.u.def.section->owner != NULL
2879 && (bfd_get_flavour (h->root.u.def.section->owner)
2880 == bfd_target_elf_flavour))
f5385ebf
AM
2881 {
2882 h->ref_regular = 1;
2883 h->ref_regular_nonweak = 1;
2884 }
45d6a902 2885 else
f5385ebf 2886 h->def_regular = 1;
45d6a902
AM
2887 }
2888
2889 if (h->dynindx == -1
f5385ebf
AM
2890 && (h->def_dynamic
2891 || h->ref_dynamic))
45d6a902 2892 {
c152c796 2893 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 2894 {
0a1b45a2
AM
2895 eif->failed = true;
2896 return false;
45d6a902
AM
2897 }
2898 }
2899 }
2900 else
2901 {
f5385ebf 2902 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2903 was first seen in a non-ELF file. Fortunately, if the symbol
2904 was first seen in an ELF file, we're probably OK unless the
2905 symbol was defined in a non-ELF file. Catch that case here.
2906 FIXME: We're still in trouble if the symbol was first seen in
2907 a dynamic object, and then later in a non-ELF regular object. */
2908 if ((h->root.type == bfd_link_hash_defined
2909 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2910 && !h->def_regular
45d6a902
AM
2911 && (h->root.u.def.section->owner != NULL
2912 ? (bfd_get_flavour (h->root.u.def.section->owner)
2913 != bfd_target_elf_flavour)
2914 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2915 && !h->def_dynamic)))
2916 h->def_regular = 1;
45d6a902
AM
2917 }
2918
508c3946 2919 /* Backend specific symbol fixup. */
33774f08
AM
2920 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2921 if (bed->elf_backend_fixup_symbol
2922 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
0a1b45a2 2923 return false;
508c3946 2924
45d6a902
AM
2925 /* If this is a final link, and the symbol was defined as a common
2926 symbol in a regular object file, and there was no definition in
2927 any dynamic object, then the linker will have allocated space for
f5385ebf 2928 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2929 flag will not have been set. */
2930 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2931 && !h->def_regular
2932 && h->ref_regular
2933 && !h->def_dynamic
96f29d96 2934 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2935 h->def_regular = 1;
45d6a902 2936
af0bfb9c
AM
2937 /* Symbols defined in discarded sections shouldn't be dynamic. */
2938 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
0a1b45a2 2939 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
af0bfb9c 2940
4deb8f71
L
2941 /* If a weak undefined symbol has non-default visibility, we also
2942 hide it from the dynamic linker. */
af0bfb9c
AM
2943 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2944 && h->root.type == bfd_link_hash_undefweak)
0a1b45a2 2945 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
4deb8f71
L
2946
2947 /* A hidden versioned symbol in executable should be forced local if
2948 it is is locally defined, not referenced by shared library and not
2949 exported. */
2950 else if (bfd_link_executable (eif->info)
2951 && h->versioned == versioned_hidden
2952 && !eif->info->export_dynamic
2953 && !h->dynamic
2954 && !h->ref_dynamic
2955 && h->def_regular)
0a1b45a2 2956 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
4deb8f71 2957
45d6a902
AM
2958 /* If -Bsymbolic was used (which means to bind references to global
2959 symbols to the definition within the shared object), and this
2960 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2961 need a PLT entry. Likewise, if the symbol has non-default
2962 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2963 will force it local. */
4deb8f71
L
2964 else if (h->needs_plt
2965 && bfd_link_pic (eif->info)
2966 && is_elf_hash_table (eif->info->hash)
2967 && (SYMBOLIC_BIND (eif->info, h)
2968 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2969 && h->def_regular)
45d6a902 2970 {
0a1b45a2 2971 bool force_local;
45d6a902 2972
45d6a902
AM
2973 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2974 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2975 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2976 }
2977
45d6a902
AM
2978 /* If this is a weak defined symbol in a dynamic object, and we know
2979 the real definition in the dynamic object, copy interesting flags
2980 over to the real definition. */
60d67dc8 2981 if (h->is_weakalias)
45d6a902 2982 {
60d67dc8
AM
2983 struct elf_link_hash_entry *def = weakdef (h);
2984
45d6a902
AM
2985 /* If the real definition is defined by a regular object file,
2986 don't do anything special. See the longer description in
5b9d7a9a
AM
2987 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
2988 bfd_link_hash_defined as it was when put on the alias list
2989 then it must have originally been a versioned symbol (for
2990 which a non-versioned indirect symbol is created) and later
2991 a definition for the non-versioned symbol is found. In that
2992 case the indirection is flipped with the versioned symbol
2993 becoming an indirect pointing at the non-versioned symbol.
2994 Thus, not an alias any more. */
2995 if (def->def_regular
2996 || def->root.type != bfd_link_hash_defined)
60d67dc8
AM
2997 {
2998 h = def;
2999 while ((h = h->u.alias) != def)
3000 h->is_weakalias = 0;
3001 }
45d6a902 3002 else
a26587ba 3003 {
4e6b54a6
AM
3004 while (h->root.type == bfd_link_hash_indirect)
3005 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4e6b54a6
AM
3006 BFD_ASSERT (h->root.type == bfd_link_hash_defined
3007 || h->root.type == bfd_link_hash_defweak);
60d67dc8 3008 BFD_ASSERT (def->def_dynamic);
60d67dc8 3009 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
a26587ba 3010 }
45d6a902
AM
3011 }
3012
0a1b45a2 3013 return true;
45d6a902
AM
3014}
3015
3016/* Make the backend pick a good value for a dynamic symbol. This is
3017 called via elf_link_hash_traverse, and also calls itself
3018 recursively. */
3019
0a1b45a2 3020static bool
268b6b39 3021_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 3022{
a50b1753 3023 struct elf_info_failed *eif = (struct elf_info_failed *) data;
559192d8 3024 struct elf_link_hash_table *htab;
9c5bfbb7 3025 const struct elf_backend_data *bed;
45d6a902 3026
0eddce27 3027 if (! is_elf_hash_table (eif->info->hash))
0a1b45a2 3028 return false;
45d6a902 3029
45d6a902
AM
3030 /* Ignore indirect symbols. These are added by the versioning code. */
3031 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 3032 return true;
45d6a902
AM
3033
3034 /* Fix the symbol flags. */
3035 if (! _bfd_elf_fix_symbol_flags (h, eif))
0a1b45a2 3036 return false;
45d6a902 3037
559192d8
AM
3038 htab = elf_hash_table (eif->info);
3039 bed = get_elf_backend_data (htab->dynobj);
3040
954b63d4
AM
3041 if (h->root.type == bfd_link_hash_undefweak)
3042 {
3043 if (eif->info->dynamic_undefined_weak == 0)
0a1b45a2 3044 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
954b63d4
AM
3045 else if (eif->info->dynamic_undefined_weak > 0
3046 && h->ref_regular
3047 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3048 && !bfd_hide_sym_by_version (eif->info->version_info,
3049 h->root.root.string))
3050 {
3051 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
3052 {
0a1b45a2
AM
3053 eif->failed = true;
3054 return false;
954b63d4
AM
3055 }
3056 }
3057 }
3058
45d6a902
AM
3059 /* If this symbol does not require a PLT entry, and it is not
3060 defined by a dynamic object, or is not referenced by a regular
3061 object, ignore it. We do have to handle a weak defined symbol,
3062 even if no regular object refers to it, if we decided to add it
3063 to the dynamic symbol table. FIXME: Do we normally need to worry
3064 about symbols which are defined by one dynamic object and
3065 referenced by another one? */
f5385ebf 3066 if (!h->needs_plt
91e21fb7 3067 && h->type != STT_GNU_IFUNC
f5385ebf
AM
3068 && (h->def_regular
3069 || !h->def_dynamic
3070 || (!h->ref_regular
60d67dc8 3071 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
45d6a902 3072 {
a6aa5195 3073 h->plt = elf_hash_table (eif->info)->init_plt_offset;
0a1b45a2 3074 return true;
45d6a902
AM
3075 }
3076
3077 /* If we've already adjusted this symbol, don't do it again. This
3078 can happen via a recursive call. */
f5385ebf 3079 if (h->dynamic_adjusted)
0a1b45a2 3080 return true;
45d6a902
AM
3081
3082 /* Don't look at this symbol again. Note that we must set this
3083 after checking the above conditions, because we may look at a
3084 symbol once, decide not to do anything, and then get called
3085 recursively later after REF_REGULAR is set below. */
f5385ebf 3086 h->dynamic_adjusted = 1;
45d6a902
AM
3087
3088 /* If this is a weak definition, and we know a real definition, and
3089 the real symbol is not itself defined by a regular object file,
3090 then get a good value for the real definition. We handle the
3091 real symbol first, for the convenience of the backend routine.
3092
3093 Note that there is a confusing case here. If the real definition
3094 is defined by a regular object file, we don't get the real symbol
3095 from the dynamic object, but we do get the weak symbol. If the
3096 processor backend uses a COPY reloc, then if some routine in the
3097 dynamic object changes the real symbol, we will not see that
3098 change in the corresponding weak symbol. This is the way other
3099 ELF linkers work as well, and seems to be a result of the shared
3100 library model.
3101
3102 I will clarify this issue. Most SVR4 shared libraries define the
3103 variable _timezone and define timezone as a weak synonym. The
3104 tzset call changes _timezone. If you write
3105 extern int timezone;
3106 int _timezone = 5;
3107 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3108 you might expect that, since timezone is a synonym for _timezone,
3109 the same number will print both times. However, if the processor
3110 backend uses a COPY reloc, then actually timezone will be copied
3111 into your process image, and, since you define _timezone
3112 yourself, _timezone will not. Thus timezone and _timezone will
3113 wind up at different memory locations. The tzset call will set
3114 _timezone, leaving timezone unchanged. */
3115
60d67dc8 3116 if (h->is_weakalias)
45d6a902 3117 {
60d67dc8
AM
3118 struct elf_link_hash_entry *def = weakdef (h);
3119
ec24dc88 3120 /* If we get to this point, there is an implicit reference to
60d67dc8
AM
3121 the alias by a regular object file via the weak symbol H. */
3122 def->ref_regular = 1;
45d6a902 3123
ec24dc88 3124 /* Ensure that the backend adjust_dynamic_symbol function sees
60d67dc8
AM
3125 the strong alias before H by recursively calling ourselves. */
3126 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
0a1b45a2 3127 return false;
45d6a902
AM
3128 }
3129
3130 /* If a symbol has no type and no size and does not require a PLT
3131 entry, then we are probably about to do the wrong thing here: we
3132 are probably going to create a COPY reloc for an empty object.
3133 This case can arise when a shared object is built with assembly
3134 code, and the assembly code fails to set the symbol type. */
3135 if (h->size == 0
3136 && h->type == STT_NOTYPE
f5385ebf 3137 && !h->needs_plt)
4eca0228 3138 _bfd_error_handler
45d6a902
AM
3139 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3140 h->root.root.string);
3141
45d6a902
AM
3142 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3143 {
0a1b45a2
AM
3144 eif->failed = true;
3145 return false;
45d6a902
AM
3146 }
3147
0a1b45a2 3148 return true;
45d6a902
AM
3149}
3150
027297b7
L
3151/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3152 DYNBSS. */
3153
0a1b45a2 3154bool
6cabe1ea
AM
3155_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3156 struct elf_link_hash_entry *h,
027297b7
L
3157 asection *dynbss)
3158{
91ac5911 3159 unsigned int power_of_two;
027297b7
L
3160 bfd_vma mask;
3161 asection *sec = h->root.u.def.section;
3162
de194d85 3163 /* The section alignment of the definition is the maximum alignment
91ac5911
L
3164 requirement of symbols defined in the section. Since we don't
3165 know the symbol alignment requirement, we start with the
3166 maximum alignment and check low bits of the symbol address
3167 for the minimum alignment. */
fd361982 3168 power_of_two = bfd_section_alignment (sec);
91ac5911
L
3169 mask = ((bfd_vma) 1 << power_of_two) - 1;
3170 while ((h->root.u.def.value & mask) != 0)
3171 {
3172 mask >>= 1;
3173 --power_of_two;
3174 }
027297b7 3175
fd361982 3176 if (power_of_two > bfd_section_alignment (dynbss))
027297b7
L
3177 {
3178 /* Adjust the section alignment if needed. */
fd361982 3179 if (!bfd_set_section_alignment (dynbss, power_of_two))
0a1b45a2 3180 return false;
027297b7
L
3181 }
3182
91ac5911 3183 /* We make sure that the symbol will be aligned properly. */
027297b7
L
3184 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3185
3186 /* Define the symbol as being at this point in DYNBSS. */
3187 h->root.u.def.section = dynbss;
3188 h->root.u.def.value = dynbss->size;
3189
3190 /* Increment the size of DYNBSS to make room for the symbol. */
3191 dynbss->size += h->size;
3192
f7483970
L
3193 /* No error if extern_protected_data is true. */
3194 if (h->protected_def
889c2a67
L
3195 && (!info->extern_protected_data
3196 || (info->extern_protected_data < 0
3197 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05 3198 info->callbacks->einfo
c1c8c1ef 3199 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
d07a1b05 3200 h->root.root.string);
6cabe1ea 3201
0a1b45a2 3202 return true;
027297b7
L
3203}
3204
45d6a902
AM
3205/* Adjust all external symbols pointing into SEC_MERGE sections
3206 to reflect the object merging within the sections. */
3207
0a1b45a2 3208static bool
268b6b39 3209_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
3210{
3211 asection *sec;
3212
45d6a902
AM
3213 if ((h->root.type == bfd_link_hash_defined
3214 || h->root.type == bfd_link_hash_defweak)
3215 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 3216 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 3217 {
a50b1753 3218 bfd *output_bfd = (bfd *) data;
45d6a902
AM
3219
3220 h->root.u.def.value =
3221 _bfd_merged_section_offset (output_bfd,
3222 &h->root.u.def.section,
3223 elf_section_data (sec)->sec_info,
753731ee 3224 h->root.u.def.value);
45d6a902
AM
3225 }
3226
0a1b45a2 3227 return true;
45d6a902 3228}
986a241f
RH
3229
3230/* Returns false if the symbol referred to by H should be considered
3231 to resolve local to the current module, and true if it should be
3232 considered to bind dynamically. */
3233
0a1b45a2 3234bool
268b6b39
AM
3235_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3236 struct bfd_link_info *info,
0a1b45a2 3237 bool not_local_protected)
986a241f 3238{
0a1b45a2 3239 bool binding_stays_local_p;
fcb93ecf
PB
3240 const struct elf_backend_data *bed;
3241 struct elf_link_hash_table *hash_table;
986a241f
RH
3242
3243 if (h == NULL)
0a1b45a2 3244 return false;
986a241f
RH
3245
3246 while (h->root.type == bfd_link_hash_indirect
3247 || h->root.type == bfd_link_hash_warning)
3248 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3249
3250 /* If it was forced local, then clearly it's not dynamic. */
3251 if (h->dynindx == -1)
0a1b45a2 3252 return false;
f5385ebf 3253 if (h->forced_local)
0a1b45a2 3254 return false;
986a241f
RH
3255
3256 /* Identify the cases where name binding rules say that a
3257 visible symbol resolves locally. */
0e1862bb
L
3258 binding_stays_local_p = (bfd_link_executable (info)
3259 || SYMBOLIC_BIND (info, h));
986a241f
RH
3260
3261 switch (ELF_ST_VISIBILITY (h->other))
3262 {
3263 case STV_INTERNAL:
3264 case STV_HIDDEN:
0a1b45a2 3265 return false;
986a241f
RH
3266
3267 case STV_PROTECTED:
fcb93ecf 3268 hash_table = elf_hash_table (info);
2cc15b10 3269 if (!is_elf_hash_table (&hash_table->root))
0a1b45a2 3270 return false;
fcb93ecf
PB
3271
3272 bed = get_elf_backend_data (hash_table->dynobj);
3273
986a241f
RH
3274 /* Proper resolution for function pointer equality may require
3275 that these symbols perhaps be resolved dynamically, even though
3276 we should be resolving them to the current module. */
89a2ee5a 3277 if (!not_local_protected || !bed->is_function_type (h->type))
0a1b45a2 3278 binding_stays_local_p = true;
986a241f
RH
3279 break;
3280
3281 default:
986a241f
RH
3282 break;
3283 }
3284
aa37626c 3285 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 3286 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
0a1b45a2 3287 return true;
aa37626c 3288
986a241f
RH
3289 /* Otherwise, the symbol is dynamic if binding rules don't tell
3290 us that it remains local. */
3291 return !binding_stays_local_p;
3292}
f6c52c13
AM
3293
3294/* Return true if the symbol referred to by H should be considered
3295 to resolve local to the current module, and false otherwise. Differs
3296 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 3297 undefined symbols. The two functions are virtually identical except
0fad2956
MR
3298 for the place where dynindx == -1 is tested. If that test is true,
3299 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3300 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3301 defined symbols.
89a2ee5a
AM
3302 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3303 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3304 treatment of undefined weak symbols. For those that do not make
3305 undefined weak symbols dynamic, both functions may return false. */
f6c52c13 3306
0a1b45a2 3307bool
268b6b39
AM
3308_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3309 struct bfd_link_info *info,
0a1b45a2 3310 bool local_protected)
f6c52c13 3311{
fcb93ecf
PB
3312 const struct elf_backend_data *bed;
3313 struct elf_link_hash_table *hash_table;
3314
f6c52c13
AM
3315 /* If it's a local sym, of course we resolve locally. */
3316 if (h == NULL)
0a1b45a2 3317 return true;
f6c52c13 3318
d95edcac
L
3319 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3320 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3321 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
0a1b45a2 3322 return true;
d95edcac 3323
0fad2956
MR
3324 /* Forced local symbols resolve locally. */
3325 if (h->forced_local)
0a1b45a2 3326 return true;
0fad2956 3327
7e2294f9
AO
3328 /* Common symbols that become definitions don't get the DEF_REGULAR
3329 flag set, so test it first, and don't bail out. */
3330 if (ELF_COMMON_DEF_P (h))
3331 /* Do nothing. */;
f6c52c13 3332 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
3333 resolve locally. The sym is either undefined or dynamic. */
3334 else if (!h->def_regular)
0a1b45a2 3335 return false;
f6c52c13 3336
0fad2956 3337 /* Non-dynamic symbols resolve locally. */
f6c52c13 3338 if (h->dynindx == -1)
0a1b45a2 3339 return true;
f6c52c13
AM
3340
3341 /* At this point, we know the symbol is defined and dynamic. In an
3342 executable it must resolve locally, likewise when building symbolic
3343 shared libraries. */
0e1862bb 3344 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
0a1b45a2 3345 return true;
f6c52c13
AM
3346
3347 /* Now deal with defined dynamic symbols in shared libraries. Ones
3348 with default visibility might not resolve locally. */
3349 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
0a1b45a2 3350 return false;
f6c52c13 3351
fcb93ecf 3352 hash_table = elf_hash_table (info);
2cc15b10 3353 if (!is_elf_hash_table (&hash_table->root))
0a1b45a2 3354 return true;
fcb93ecf 3355
6f365fda
L
3356 /* STV_PROTECTED symbols with indirect external access are local. */
3357 if (info->indirect_extern_access > 0)
3358 return true;
3359
fcb93ecf
PB
3360 bed = get_elf_backend_data (hash_table->dynobj);
3361
f7483970
L
3362 /* If extern_protected_data is false, STV_PROTECTED non-function
3363 symbols are local. */
889c2a67
L
3364 if ((!info->extern_protected_data
3365 || (info->extern_protected_data < 0
3366 && !bed->extern_protected_data))
3367 && !bed->is_function_type (h->type))
0a1b45a2 3368 return true;
1c16dfa5 3369
f6c52c13 3370 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
3371 symbols be treated as dynamic symbols. If the address of a
3372 function not defined in an executable is set to that function's
3373 plt entry in the executable, then the address of the function in
3374 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
3375 return local_protected;
3376}
e1918d23
AM
3377
3378/* Caches some TLS segment info, and ensures that the TLS segment vma is
3379 aligned. Returns the first TLS output section. */
3380
3381struct bfd_section *
3382_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3383{
3384 struct bfd_section *sec, *tls;
3385 unsigned int align = 0;
3386
3387 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3388 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3389 break;
3390 tls = sec;
3391
3392 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3393 if (sec->alignment_power > align)
3394 align = sec->alignment_power;
3395
3396 elf_hash_table (info)->tls_sec = tls;
3397
fdde2fb6
SH
3398 /* Ensure the alignment of the first section (usually .tdata) is the largest
3399 alignment, so that the tls segment starts aligned. */
e1918d23
AM
3400 if (tls != NULL)
3401 tls->alignment_power = align;
3402
3403 return tls;
3404}
0ad989f9
L
3405
3406/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
0a1b45a2 3407static bool
0ad989f9
L
3408is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3409 Elf_Internal_Sym *sym)
3410{
a4d8e49b
L
3411 const struct elf_backend_data *bed;
3412
0ad989f9
L
3413 /* Local symbols do not count, but target specific ones might. */
3414 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3415 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
0a1b45a2 3416 return false;
0ad989f9 3417
fcb93ecf 3418 bed = get_elf_backend_data (abfd);
0ad989f9 3419 /* Function symbols do not count. */
fcb93ecf 3420 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0a1b45a2 3421 return false;
0ad989f9
L
3422
3423 /* If the section is undefined, then so is the symbol. */
3424 if (sym->st_shndx == SHN_UNDEF)
0a1b45a2 3425 return false;
0ad989f9
L
3426
3427 /* If the symbol is defined in the common section, then
3428 it is a common definition and so does not count. */
a4d8e49b 3429 if (bed->common_definition (sym))
0a1b45a2 3430 return false;
0ad989f9
L
3431
3432 /* If the symbol is in a target specific section then we
3433 must rely upon the backend to tell us what it is. */
3434 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3435 /* FIXME - this function is not coded yet:
3436
3437 return _bfd_is_global_symbol_definition (abfd, sym);
3438
3439 Instead for now assume that the definition is not global,
3440 Even if this is wrong, at least the linker will behave
3441 in the same way that it used to do. */
0a1b45a2 3442 return false;
0ad989f9 3443
0a1b45a2 3444 return true;
0ad989f9
L
3445}
3446
3447/* Search the symbol table of the archive element of the archive ABFD
3448 whose archive map contains a mention of SYMDEF, and determine if
3449 the symbol is defined in this element. */
0a1b45a2 3450static bool
0ad989f9
L
3451elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3452{
3453 Elf_Internal_Shdr * hdr;
ef53be89
AM
3454 size_t symcount;
3455 size_t extsymcount;
3456 size_t extsymoff;
0ad989f9
L
3457 Elf_Internal_Sym *isymbuf;
3458 Elf_Internal_Sym *isym;
3459 Elf_Internal_Sym *isymend;
0a1b45a2 3460 bool result;
0ad989f9
L
3461
3462 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3463 if (abfd == NULL)
0a1b45a2 3464 return false;
0ad989f9
L
3465
3466 if (! bfd_check_format (abfd, bfd_object))
0a1b45a2 3467 return false;
0ad989f9 3468
7dc3990e
L
3469 /* Select the appropriate symbol table. If we don't know if the
3470 object file is an IR object, give linker LTO plugin a chance to
3471 get the correct symbol table. */
3472 if (abfd->plugin_format == bfd_plugin_yes
08ce1d72 3473#if BFD_SUPPORTS_PLUGINS
7dc3990e
L
3474 || (abfd->plugin_format == bfd_plugin_unknown
3475 && bfd_link_plugin_object_p (abfd))
3476#endif
3477 )
3478 {
3479 /* Use the IR symbol table if the object has been claimed by
3480 plugin. */
3481 abfd = abfd->plugin_dummy_bfd;
3482 hdr = &elf_tdata (abfd)->symtab_hdr;
3483 }
3484 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
0ad989f9
L
3485 hdr = &elf_tdata (abfd)->symtab_hdr;
3486 else
3487 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3488
3489 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3490
3491 /* The sh_info field of the symtab header tells us where the
3492 external symbols start. We don't care about the local symbols. */
3493 if (elf_bad_symtab (abfd))
3494 {
3495 extsymcount = symcount;
3496 extsymoff = 0;
3497 }
3498 else
3499 {
3500 extsymcount = symcount - hdr->sh_info;
3501 extsymoff = hdr->sh_info;
3502 }
3503
3504 if (extsymcount == 0)
0a1b45a2 3505 return false;
0ad989f9
L
3506
3507 /* Read in the symbol table. */
3508 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3509 NULL, NULL, NULL);
3510 if (isymbuf == NULL)
0a1b45a2 3511 return false;
0ad989f9
L
3512
3513 /* Scan the symbol table looking for SYMDEF. */
0a1b45a2 3514 result = false;
0ad989f9
L
3515 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3516 {
3517 const char *name;
3518
3519 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3520 isym->st_name);
3521 if (name == NULL)
3522 break;
3523
3524 if (strcmp (name, symdef->name) == 0)
3525 {
3526 result = is_global_data_symbol_definition (abfd, isym);
3527 break;
3528 }
3529 }
3530
3531 free (isymbuf);
3532
3533 return result;
3534}
3535\f
5a580b3a
AM
3536/* Add an entry to the .dynamic table. */
3537
0a1b45a2 3538bool
5a580b3a
AM
3539_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3540 bfd_vma tag,
3541 bfd_vma val)
3542{
3543 struct elf_link_hash_table *hash_table;
3544 const struct elf_backend_data *bed;
3545 asection *s;
3546 bfd_size_type newsize;
3547 bfd_byte *newcontents;
3548 Elf_Internal_Dyn dyn;
3549
3550 hash_table = elf_hash_table (info);
2cc15b10 3551 if (! is_elf_hash_table (&hash_table->root))
0a1b45a2 3552 return false;
5a580b3a 3553
7f923b7f 3554 if (tag == DT_RELA || tag == DT_REL)
0a1b45a2 3555 hash_table->dynamic_relocs = true;
7f923b7f 3556
5a580b3a 3557 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3558 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3559 BFD_ASSERT (s != NULL);
3560
eea6121a 3561 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3562 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a 3563 if (newcontents == NULL)
0a1b45a2 3564 return false;
5a580b3a
AM
3565
3566 dyn.d_tag = tag;
3567 dyn.d_un.d_val = val;
eea6121a 3568 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3569
eea6121a 3570 s->size = newsize;
5a580b3a
AM
3571 s->contents = newcontents;
3572
0a1b45a2 3573 return true;
5a580b3a
AM
3574}
3575
6f6fd151
L
3576/* Strip zero-sized dynamic sections. */
3577
0a1b45a2 3578bool
6f6fd151
L
3579_bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3580{
3581 struct elf_link_hash_table *hash_table;
3582 const struct elf_backend_data *bed;
3583 asection *s, *sdynamic, **pp;
3584 asection *rela_dyn, *rel_dyn;
3585 Elf_Internal_Dyn dyn;
3586 bfd_byte *extdyn, *next;
3587 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
0a1b45a2
AM
3588 bool strip_zero_sized;
3589 bool strip_zero_sized_plt;
6f6fd151
L
3590
3591 if (bfd_link_relocatable (info))
0a1b45a2 3592 return true;
6f6fd151
L
3593
3594 hash_table = elf_hash_table (info);
2cc15b10 3595 if (!is_elf_hash_table (&hash_table->root))
0a1b45a2 3596 return false;
6f6fd151
L
3597
3598 if (!hash_table->dynobj)
0a1b45a2 3599 return true;
6f6fd151
L
3600
3601 sdynamic= bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3602 if (!sdynamic)
0a1b45a2 3603 return true;
6f6fd151
L
3604
3605 bed = get_elf_backend_data (hash_table->dynobj);
3606 swap_dyn_in = bed->s->swap_dyn_in;
3607
0a1b45a2
AM
3608 strip_zero_sized = false;
3609 strip_zero_sized_plt = false;
6f6fd151
L
3610
3611 /* Strip zero-sized dynamic sections. */
3612 rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3613 rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3614 for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3615 if (s->size == 0
3616 && (s == rela_dyn
3617 || s == rel_dyn
3618 || s == hash_table->srelplt->output_section
3619 || s == hash_table->splt->output_section))
3620 {
3621 *pp = s->next;
3622 info->output_bfd->section_count--;
0a1b45a2 3623 strip_zero_sized = true;
6f6fd151
L
3624 if (s == rela_dyn)
3625 s = rela_dyn;
3626 if (s == rel_dyn)
3627 s = rel_dyn;
3628 else if (s == hash_table->splt->output_section)
3629 {
3630 s = hash_table->splt;
0a1b45a2 3631 strip_zero_sized_plt = true;
6f6fd151
L
3632 }
3633 else
3634 s = hash_table->srelplt;
3635 s->flags |= SEC_EXCLUDE;
3636 s->output_section = bfd_abs_section_ptr;
3637 }
3638 else
3639 pp = &s->next;
3640
3641 if (strip_zero_sized_plt)
3642 for (extdyn = sdynamic->contents;
3643 extdyn < sdynamic->contents + sdynamic->size;
3644 extdyn = next)
3645 {
3646 next = extdyn + bed->s->sizeof_dyn;
3647 swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3648 switch (dyn.d_tag)
3649 {
3650 default:
3651 break;
3652 case DT_JMPREL:
3653 case DT_PLTRELSZ:
3654 case DT_PLTREL:
3655 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3656 the procedure linkage table (the .plt section) has been
3657 removed. */
3658 memmove (extdyn, next,
3659 sdynamic->size - (next - sdynamic->contents));
3660 next = extdyn;
3661 }
3662 }
3663
3664 if (strip_zero_sized)
3665 {
3666 /* Regenerate program headers. */
3667 elf_seg_map (info->output_bfd) = NULL;
3668 return _bfd_elf_map_sections_to_segments (info->output_bfd, info);
3669 }
3670
0a1b45a2 3671 return true;
6f6fd151
L
3672}
3673
e310298c 3674/* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
5a580b3a
AM
3675 1 if a DT_NEEDED tag already exists, and 0 on success. */
3676
e310298c
AM
3677int
3678bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
5a580b3a
AM
3679{
3680 struct elf_link_hash_table *hash_table;
ef53be89 3681 size_t strindex;
e310298c 3682 const char *soname;
5a580b3a 3683
7e9f0867
AM
3684 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3685 return -1;
3686
5a580b3a 3687 hash_table = elf_hash_table (info);
e310298c 3688 soname = elf_dt_name (abfd);
0a1b45a2 3689 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, false);
ef53be89 3690 if (strindex == (size_t) -1)
5a580b3a
AM
3691 return -1;
3692
02be4619 3693 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3694 {
3695 asection *sdyn;
3696 const struct elf_backend_data *bed;
3697 bfd_byte *extdyn;
3698
3699 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3700 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3701 if (sdyn != NULL)
3702 for (extdyn = sdyn->contents;
3703 extdyn < sdyn->contents + sdyn->size;
3704 extdyn += bed->s->sizeof_dyn)
3705 {
3706 Elf_Internal_Dyn dyn;
5a580b3a 3707
7e9f0867
AM
3708 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3709 if (dyn.d_tag == DT_NEEDED
3710 && dyn.d_un.d_val == strindex)
3711 {
3712 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3713 return 1;
3714 }
3715 }
5a580b3a
AM
3716 }
3717
e310298c
AM
3718 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3719 return -1;
7e9f0867 3720
e310298c
AM
3721 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3722 return -1;
5a580b3a
AM
3723
3724 return 0;
3725}
3726
7b15fa7a
AM
3727/* Return true if SONAME is on the needed list between NEEDED and STOP
3728 (or the end of list if STOP is NULL), and needed by a library that
3729 will be loaded. */
3730
0a1b45a2 3731static bool
7b15fa7a
AM
3732on_needed_list (const char *soname,
3733 struct bfd_link_needed_list *needed,
3734 struct bfd_link_needed_list *stop)
010e5ae2 3735{
7b15fa7a
AM
3736 struct bfd_link_needed_list *look;
3737 for (look = needed; look != stop; look = look->next)
3738 if (strcmp (soname, look->name) == 0
3739 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3740 /* If needed by a library that itself is not directly
3741 needed, recursively check whether that library is
3742 indirectly needed. Since we add DT_NEEDED entries to
3743 the end of the list, library dependencies appear after
3744 the library. Therefore search prior to the current
3745 LOOK, preventing possible infinite recursion. */
3746 || on_needed_list (elf_dt_name (look->by), needed, look)))
0a1b45a2 3747 return true;
010e5ae2 3748
0a1b45a2 3749 return false;
010e5ae2
AM
3750}
3751
3a3f4bf7 3752/* Sort symbol by value, section, size, and type. */
4ad4eba5
AM
3753static int
3754elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3755{
3756 const struct elf_link_hash_entry *h1;
3757 const struct elf_link_hash_entry *h2;
10b7e05b 3758 bfd_signed_vma vdiff;
3a3f4bf7
AM
3759 int sdiff;
3760 const char *n1;
3761 const char *n2;
5a580b3a
AM
3762
3763 h1 = *(const struct elf_link_hash_entry **) arg1;
3764 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3765 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3766 if (vdiff != 0)
3767 return vdiff > 0 ? 1 : -1;
3a3f4bf7
AM
3768
3769 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3770 if (sdiff != 0)
3771 return sdiff;
3772
3773 /* Sort so that sized symbols are selected over zero size symbols. */
3774 vdiff = h1->size - h2->size;
3775 if (vdiff != 0)
3776 return vdiff > 0 ? 1 : -1;
3777
3778 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3779 if (h1->type != h2->type)
3780 return h1->type - h2->type;
3781
3782 /* If symbols are properly sized and typed, and multiple strong
3783 aliases are not defined in a shared library by the user we
3784 shouldn't get here. Unfortunately linker script symbols like
3785 __bss_start sometimes match a user symbol defined at the start of
3786 .bss without proper size and type. We'd like to preference the
3787 user symbol over reserved system symbols. Sort on leading
3788 underscores. */
3789 n1 = h1->root.root.string;
3790 n2 = h2->root.root.string;
3791 while (*n1 == *n2)
10b7e05b 3792 {
3a3f4bf7
AM
3793 if (*n1 == 0)
3794 break;
3795 ++n1;
3796 ++n2;
10b7e05b 3797 }
3a3f4bf7
AM
3798 if (*n1 == '_')
3799 return -1;
3800 if (*n2 == '_')
3801 return 1;
3802
3803 /* Final sort on name selects user symbols like '_u' over reserved
3804 system symbols like '_Z' and also will avoid qsort instability. */
3805 return *n1 - *n2;
5a580b3a 3806}
4ad4eba5 3807
5a580b3a
AM
3808/* This function is used to adjust offsets into .dynstr for
3809 dynamic symbols. This is called via elf_link_hash_traverse. */
3810
0a1b45a2 3811static bool
5a580b3a
AM
3812elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3813{
a50b1753 3814 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3815
5a580b3a
AM
3816 if (h->dynindx != -1)
3817 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
0a1b45a2 3818 return true;
5a580b3a
AM
3819}
3820
3821/* Assign string offsets in .dynstr, update all structures referencing
3822 them. */
3823
0a1b45a2 3824static bool
4ad4eba5 3825elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3826{
3827 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3828 struct elf_link_local_dynamic_entry *entry;
3829 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3830 bfd *dynobj = hash_table->dynobj;
3831 asection *sdyn;
3832 bfd_size_type size;
3833 const struct elf_backend_data *bed;
3834 bfd_byte *extdyn;
3835
3836 _bfd_elf_strtab_finalize (dynstr);
3837 size = _bfd_elf_strtab_size (dynstr);
3838
3d16b64e
NA
3839 /* Allow the linker to examine the dynsymtab now it's fully populated. */
3840
3841 if (info->callbacks->examine_strtab)
3842 info->callbacks->examine_strtab (dynstr);
3843
5a580b3a 3844 bed = get_elf_backend_data (dynobj);
3d4d4302 3845 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3846 BFD_ASSERT (sdyn != NULL);
3847
3848 /* Update all .dynamic entries referencing .dynstr strings. */
3849 for (extdyn = sdyn->contents;
eea6121a 3850 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3851 extdyn += bed->s->sizeof_dyn)
3852 {
3853 Elf_Internal_Dyn dyn;
3854
3855 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3856 switch (dyn.d_tag)
3857 {
3858 case DT_STRSZ:
3859 dyn.d_un.d_val = size;
3860 break;
3861 case DT_NEEDED:
3862 case DT_SONAME:
3863 case DT_RPATH:
3864 case DT_RUNPATH:
3865 case DT_FILTER:
3866 case DT_AUXILIARY:
7ee314fa
AM
3867 case DT_AUDIT:
3868 case DT_DEPAUDIT:
5a580b3a
AM
3869 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3870 break;
3871 default:
3872 continue;
3873 }
3874 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3875 }
3876
3877 /* Now update local dynamic symbols. */
3878 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3879 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3880 entry->isym.st_name);
3881
3882 /* And the rest of dynamic symbols. */
3883 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3884
3885 /* Adjust version definitions. */
3886 if (elf_tdata (output_bfd)->cverdefs)
3887 {
3888 asection *s;
3889 bfd_byte *p;
ef53be89 3890 size_t i;
5a580b3a
AM
3891 Elf_Internal_Verdef def;
3892 Elf_Internal_Verdaux defaux;
3893
3d4d4302 3894 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3895 p = s->contents;
3896 do
3897 {
3898 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3899 &def);
3900 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3901 if (def.vd_aux != sizeof (Elf_External_Verdef))
3902 continue;
5a580b3a
AM
3903 for (i = 0; i < def.vd_cnt; ++i)
3904 {
3905 _bfd_elf_swap_verdaux_in (output_bfd,
3906 (Elf_External_Verdaux *) p, &defaux);
3907 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3908 defaux.vda_name);
3909 _bfd_elf_swap_verdaux_out (output_bfd,
3910 &defaux, (Elf_External_Verdaux *) p);
3911 p += sizeof (Elf_External_Verdaux);
3912 }
3913 }
3914 while (def.vd_next);
3915 }
3916
3917 /* Adjust version references. */
3918 if (elf_tdata (output_bfd)->verref)
3919 {
3920 asection *s;
3921 bfd_byte *p;
ef53be89 3922 size_t i;
5a580b3a
AM
3923 Elf_Internal_Verneed need;
3924 Elf_Internal_Vernaux needaux;
3925
3d4d4302 3926 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3927 p = s->contents;
3928 do
3929 {
3930 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3931 &need);
3932 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3933 _bfd_elf_swap_verneed_out (output_bfd, &need,
3934 (Elf_External_Verneed *) p);
3935 p += sizeof (Elf_External_Verneed);
3936 for (i = 0; i < need.vn_cnt; ++i)
3937 {
3938 _bfd_elf_swap_vernaux_in (output_bfd,
3939 (Elf_External_Vernaux *) p, &needaux);
3940 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3941 needaux.vna_name);
3942 _bfd_elf_swap_vernaux_out (output_bfd,
3943 &needaux,
3944 (Elf_External_Vernaux *) p);
3945 p += sizeof (Elf_External_Vernaux);
3946 }
3947 }
3948 while (need.vn_next);
3949 }
3950
0a1b45a2 3951 return true;
5a580b3a
AM
3952}
3953\f
13285a1b
AM
3954/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3955 The default is to only match when the INPUT and OUTPUT are exactly
3956 the same target. */
3957
0a1b45a2 3958bool
13285a1b
AM
3959_bfd_elf_default_relocs_compatible (const bfd_target *input,
3960 const bfd_target *output)
3961{
3962 return input == output;
3963}
3964
3965/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3966 This version is used when different targets for the same architecture
3967 are virtually identical. */
3968
0a1b45a2 3969bool
13285a1b
AM
3970_bfd_elf_relocs_compatible (const bfd_target *input,
3971 const bfd_target *output)
3972{
3973 const struct elf_backend_data *obed, *ibed;
3974
3975 if (input == output)
0a1b45a2 3976 return true;
13285a1b
AM
3977
3978 ibed = xvec_get_elf_backend_data (input);
3979 obed = xvec_get_elf_backend_data (output);
3980
3981 if (ibed->arch != obed->arch)
0a1b45a2 3982 return false;
13285a1b
AM
3983
3984 /* If both backends are using this function, deem them compatible. */
3985 return ibed->relocs_compatible == obed->relocs_compatible;
3986}
3987
e5034e59
AM
3988/* Make a special call to the linker "notice" function to tell it that
3989 we are about to handle an as-needed lib, or have finished
1b786873 3990 processing the lib. */
e5034e59 3991
0a1b45a2 3992bool
e5034e59
AM
3993_bfd_elf_notice_as_needed (bfd *ibfd,
3994 struct bfd_link_info *info,
3995 enum notice_asneeded_action act)
3996{
46135103 3997 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3998}
3999
d9689752
L
4000/* Check relocations an ELF object file. */
4001
0a1b45a2 4002bool
d9689752
L
4003_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
4004{
4005 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4006 struct elf_link_hash_table *htab = elf_hash_table (info);
4007
4008 /* If this object is the same format as the output object, and it is
4009 not a shared library, then let the backend look through the
4010 relocs.
4011
4012 This is required to build global offset table entries and to
4013 arrange for dynamic relocs. It is not required for the
4014 particular common case of linking non PIC code, even when linking
4015 against shared libraries, but unfortunately there is no way of
4016 knowing whether an object file has been compiled PIC or not.
4017 Looking through the relocs is not particularly time consuming.
4018 The problem is that we must either (1) keep the relocs in memory,
4019 which causes the linker to require additional runtime memory or
4020 (2) read the relocs twice from the input file, which wastes time.
4021 This would be a good case for using mmap.
4022
4023 I have no idea how to handle linking PIC code into a file of a
4024 different format. It probably can't be done. */
4025 if ((abfd->flags & DYNAMIC) == 0
2cc15b10 4026 && is_elf_hash_table (&htab->root)
d9689752
L
4027 && bed->check_relocs != NULL
4028 && elf_object_id (abfd) == elf_hash_table_id (htab)
4029 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4030 {
4031 asection *o;
4032
4033 for (o = abfd->sections; o != NULL; o = o->next)
4034 {
4035 Elf_Internal_Rela *internal_relocs;
0a1b45a2 4036 bool ok;
d9689752 4037
c4b126b8
L
4038 /* Don't check relocations in excluded sections. Don't do
4039 anything special with non-loaded, non-alloced sections.
4040 In particular, any relocs in such sections should not
4041 affect GOT and PLT reference counting (ie. we don't
4042 allow them to create GOT or PLT entries), there's no
4043 possibility or desire to optimize TLS relocs, and
4044 there's not much point in propagating relocs to shared
4045 libs that the dynamic linker won't relocate. */
4046 if ((o->flags & SEC_ALLOC) == 0
4047 || (o->flags & SEC_RELOC) == 0
5ce03cea 4048 || (o->flags & SEC_EXCLUDE) != 0
d9689752
L
4049 || o->reloc_count == 0
4050 || ((info->strip == strip_all || info->strip == strip_debugger)
4051 && (o->flags & SEC_DEBUGGING) != 0)
4052 || bfd_is_abs_section (o->output_section))
4053 continue;
4054
a8dde0a2
L
4055 internal_relocs = _bfd_elf_link_info_read_relocs (abfd, info,
4056 o, NULL,
4057 NULL,
4058 _bfd_link_keep_memory (info));
d9689752 4059 if (internal_relocs == NULL)
0a1b45a2 4060 return false;
d9689752
L
4061
4062 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4063
4064 if (elf_section_data (o)->relocs != internal_relocs)
4065 free (internal_relocs);
4066
4067 if (! ok)
0a1b45a2 4068 return false;
d9689752
L
4069 }
4070 }
4071
0a1b45a2 4072 return true;
d9689752
L
4073}
4074
4ad4eba5
AM
4075/* Add symbols from an ELF object file to the linker hash table. */
4076
0a1b45a2 4077static bool
4ad4eba5
AM
4078elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4079{
a0c402a5 4080 Elf_Internal_Ehdr *ehdr;
4ad4eba5 4081 Elf_Internal_Shdr *hdr;
ef53be89
AM
4082 size_t symcount;
4083 size_t extsymcount;
4084 size_t extsymoff;
4ad4eba5 4085 struct elf_link_hash_entry **sym_hash;
0a1b45a2 4086 bool dynamic;
4ad4eba5 4087 Elf_External_Versym *extversym = NULL;
be22c732 4088 Elf_External_Versym *extversym_end = NULL;
4ad4eba5
AM
4089 Elf_External_Versym *ever;
4090 struct elf_link_hash_entry *weaks;
4091 struct elf_link_hash_entry **nondeflt_vers = NULL;
ef53be89 4092 size_t nondeflt_vers_cnt = 0;
4ad4eba5
AM
4093 Elf_Internal_Sym *isymbuf = NULL;
4094 Elf_Internal_Sym *isym;
4095 Elf_Internal_Sym *isymend;
4096 const struct elf_backend_data *bed;
0a1b45a2 4097 bool add_needed;
66eb6687 4098 struct elf_link_hash_table *htab;
66eb6687 4099 void *alloc_mark = NULL;
4f87808c
AM
4100 struct bfd_hash_entry **old_table = NULL;
4101 unsigned int old_size = 0;
4102 unsigned int old_count = 0;
66eb6687 4103 void *old_tab = NULL;
66eb6687
AM
4104 void *old_ent;
4105 struct bfd_link_hash_entry *old_undefs = NULL;
4106 struct bfd_link_hash_entry *old_undefs_tail = NULL;
5b677558 4107 void *old_strtab = NULL;
66eb6687 4108 size_t tabsize = 0;
db6a5d5f 4109 asection *s;
0a1b45a2 4110 bool just_syms;
4ad4eba5 4111
66eb6687 4112 htab = elf_hash_table (info);
4ad4eba5 4113 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
4114
4115 if ((abfd->flags & DYNAMIC) == 0)
0a1b45a2 4116 dynamic = false;
4ad4eba5
AM
4117 else
4118 {
0a1b45a2 4119 dynamic = true;
4ad4eba5
AM
4120
4121 /* You can't use -r against a dynamic object. Also, there's no
4122 hope of using a dynamic object which does not exactly match
4123 the format of the output file. */
0e1862bb 4124 if (bfd_link_relocatable (info)
2cc15b10 4125 || !is_elf_hash_table (&htab->root)
f13a99db 4126 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 4127 {
0e1862bb 4128 if (bfd_link_relocatable (info))
9a0789ec
NC
4129 bfd_set_error (bfd_error_invalid_operation);
4130 else
4131 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
4132 goto error_return;
4133 }
4134 }
4135
a0c402a5
L
4136 ehdr = elf_elfheader (abfd);
4137 if (info->warn_alternate_em
4138 && bed->elf_machine_code != ehdr->e_machine
4139 && ((bed->elf_machine_alt1 != 0
4140 && ehdr->e_machine == bed->elf_machine_alt1)
4141 || (bed->elf_machine_alt2 != 0
4142 && ehdr->e_machine == bed->elf_machine_alt2)))
9793eb77 4143 _bfd_error_handler
695344c0 4144 /* xgettext:c-format */
9793eb77 4145 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
a0c402a5
L
4146 ehdr->e_machine, abfd, bed->elf_machine_code);
4147
4ad4eba5
AM
4148 /* As a GNU extension, any input sections which are named
4149 .gnu.warning.SYMBOL are treated as warning symbols for the given
4150 symbol. This differs from .gnu.warning sections, which generate
4151 warnings when they are included in an output file. */
dd98f8d2 4152 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 4153 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 4154 {
db6a5d5f 4155 const char *name;
4ad4eba5 4156
fd361982 4157 name = bfd_section_name (s);
08dedd66 4158 if (startswith (name, ".gnu.warning."))
4ad4eba5 4159 {
db6a5d5f
AM
4160 char *msg;
4161 bfd_size_type sz;
4162
4163 name += sizeof ".gnu.warning." - 1;
4164
4165 /* If this is a shared object, then look up the symbol
4166 in the hash table. If it is there, and it is already
4167 been defined, then we will not be using the entry
4168 from this shared object, so we don't need to warn.
4169 FIXME: If we see the definition in a regular object
4170 later on, we will warn, but we shouldn't. The only
4171 fix is to keep track of what warnings we are supposed
4172 to emit, and then handle them all at the end of the
4173 link. */
4174 if (dynamic)
4ad4eba5 4175 {
db6a5d5f
AM
4176 struct elf_link_hash_entry *h;
4177
0a1b45a2 4178 h = elf_link_hash_lookup (htab, name, false, false, true);
db6a5d5f
AM
4179
4180 /* FIXME: What about bfd_link_hash_common? */
4181 if (h != NULL
4182 && (h->root.type == bfd_link_hash_defined
4183 || h->root.type == bfd_link_hash_defweak))
4184 continue;
4185 }
4ad4eba5 4186
db6a5d5f
AM
4187 sz = s->size;
4188 msg = (char *) bfd_alloc (abfd, sz + 1);
4189 if (msg == NULL)
4190 goto error_return;
4ad4eba5 4191
db6a5d5f
AM
4192 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4193 goto error_return;
4ad4eba5 4194
db6a5d5f 4195 msg[sz] = '\0';
4ad4eba5 4196
db6a5d5f
AM
4197 if (! (_bfd_generic_link_add_one_symbol
4198 (info, abfd, name, BSF_WARNING, s, 0, msg,
0a1b45a2 4199 false, bed->collect, NULL)))
db6a5d5f 4200 goto error_return;
4ad4eba5 4201
0e1862bb 4202 if (bfd_link_executable (info))
db6a5d5f
AM
4203 {
4204 /* Clobber the section size so that the warning does
4205 not get copied into the output file. */
4206 s->size = 0;
11d2f718 4207
db6a5d5f
AM
4208 /* Also set SEC_EXCLUDE, so that symbols defined in
4209 the warning section don't get copied to the output. */
4210 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
4211 }
4212 }
4213 }
4214
29a9f53e
L
4215 just_syms = ((s = abfd->sections) != NULL
4216 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4217
0a1b45a2 4218 add_needed = true;
4ad4eba5
AM
4219 if (! dynamic)
4220 {
4221 /* If we are creating a shared library, create all the dynamic
4222 sections immediately. We need to attach them to something,
4223 so we attach them to this BFD, provided it is the right
bf89386a
L
4224 format and is not from ld --just-symbols. Always create the
4225 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
4226 are no input BFD's of the same format as the output, we can't
4227 make a shared library. */
4228 if (!just_syms
bf89386a 4229 && (bfd_link_pic (info)
9c1d7a08 4230 || (!bfd_link_relocatable (info)
3c5fce9b 4231 && info->nointerp
9c1d7a08 4232 && (info->export_dynamic || info->dynamic)))
2cc15b10 4233 && is_elf_hash_table (&htab->root)
f13a99db 4234 && info->output_bfd->xvec == abfd->xvec
66eb6687 4235 && !htab->dynamic_sections_created)
4ad4eba5
AM
4236 {
4237 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4238 goto error_return;
4239 }
4240 }
2cc15b10 4241 else if (!is_elf_hash_table (&htab->root))
4ad4eba5
AM
4242 goto error_return;
4243 else
4244 {
4ad4eba5 4245 const char *soname = NULL;
7ee314fa 4246 char *audit = NULL;
4ad4eba5 4247 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
9acc85a6 4248 const Elf_Internal_Phdr *phdr;
e310298c 4249 struct elf_link_loaded_list *loaded_lib;
4ad4eba5
AM
4250
4251 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 4252 ld shouldn't allow it. */
29a9f53e 4253 if (just_syms)
92fd189d 4254 abort ();
4ad4eba5
AM
4255
4256 /* If this dynamic lib was specified on the command line with
4257 --as-needed in effect, then we don't want to add a DT_NEEDED
4258 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
4259 in by another lib's DT_NEEDED. When --no-add-needed is used
4260 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4261 any dynamic library in DT_NEEDED tags in the dynamic lib at
4262 all. */
4263 add_needed = (elf_dyn_lib_class (abfd)
4264 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4265 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
4266
4267 s = bfd_get_section_by_name (abfd, ".dynamic");
4268 if (s != NULL)
4269 {
4270 bfd_byte *dynbuf;
4271 bfd_byte *extdyn;
cb33740c 4272 unsigned int elfsec;
4ad4eba5
AM
4273 unsigned long shlink;
4274
eea6121a 4275 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194 4276 {
dc1e8a47 4277 error_free_dyn:
f8703194
L
4278 free (dynbuf);
4279 goto error_return;
4280 }
4ad4eba5
AM
4281
4282 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 4283 if (elfsec == SHN_BAD)
4ad4eba5
AM
4284 goto error_free_dyn;
4285 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4286
4287 for (extdyn = dynbuf;
9bff840e 4288 extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
4ad4eba5
AM
4289 extdyn += bed->s->sizeof_dyn)
4290 {
4291 Elf_Internal_Dyn dyn;
4292
4293 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4294 if (dyn.d_tag == DT_SONAME)
4295 {
4296 unsigned int tagv = dyn.d_un.d_val;
4297 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4298 if (soname == NULL)
4299 goto error_free_dyn;
4300 }
4301 if (dyn.d_tag == DT_NEEDED)
4302 {
4303 struct bfd_link_needed_list *n, **pn;
4304 char *fnm, *anm;
4305 unsigned int tagv = dyn.d_un.d_val;
986f0783 4306 size_t amt = sizeof (struct bfd_link_needed_list);
4ad4eba5 4307
a50b1753 4308 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4309 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4310 if (n == NULL || fnm == NULL)
4311 goto error_free_dyn;
4312 amt = strlen (fnm) + 1;
a50b1753 4313 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4314 if (anm == NULL)
4315 goto error_free_dyn;
4316 memcpy (anm, fnm, amt);
4317 n->name = anm;
4318 n->by = abfd;
4319 n->next = NULL;
66eb6687 4320 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4321 ;
4322 *pn = n;
4323 }
4324 if (dyn.d_tag == DT_RUNPATH)
4325 {
4326 struct bfd_link_needed_list *n, **pn;
4327 char *fnm, *anm;
4328 unsigned int tagv = dyn.d_un.d_val;
986f0783 4329 size_t amt = sizeof (struct bfd_link_needed_list);
4ad4eba5 4330
a50b1753 4331 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4332 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4333 if (n == NULL || fnm == NULL)
4334 goto error_free_dyn;
4335 amt = strlen (fnm) + 1;
a50b1753 4336 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4337 if (anm == NULL)
4338 goto error_free_dyn;
4339 memcpy (anm, fnm, amt);
4340 n->name = anm;
4341 n->by = abfd;
4342 n->next = NULL;
4343 for (pn = & runpath;
4344 *pn != NULL;
4345 pn = &(*pn)->next)
4346 ;
4347 *pn = n;
4348 }
4349 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4350 if (!runpath && dyn.d_tag == DT_RPATH)
4351 {
4352 struct bfd_link_needed_list *n, **pn;
4353 char *fnm, *anm;
4354 unsigned int tagv = dyn.d_un.d_val;
986f0783 4355 size_t amt = sizeof (struct bfd_link_needed_list);
4ad4eba5 4356
a50b1753 4357 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4358 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4359 if (n == NULL || fnm == NULL)
4360 goto error_free_dyn;
4361 amt = strlen (fnm) + 1;
a50b1753 4362 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 4363 if (anm == NULL)
f8703194 4364 goto error_free_dyn;
4ad4eba5
AM
4365 memcpy (anm, fnm, amt);
4366 n->name = anm;
4367 n->by = abfd;
4368 n->next = NULL;
4369 for (pn = & rpath;
4370 *pn != NULL;
4371 pn = &(*pn)->next)
4372 ;
4373 *pn = n;
4374 }
7ee314fa
AM
4375 if (dyn.d_tag == DT_AUDIT)
4376 {
4377 unsigned int tagv = dyn.d_un.d_val;
4378 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4379 }
f64b9b13
AM
4380 if (dyn.d_tag == DT_FLAGS_1)
4381 elf_tdata (abfd)->is_pie = (dyn.d_un.d_val & DF_1_PIE) != 0;
4ad4eba5
AM
4382 }
4383
4384 free (dynbuf);
4385 }
4386
4387 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4388 frees all more recently bfd_alloc'd blocks as well. */
4389 if (runpath)
4390 rpath = runpath;
4391
4392 if (rpath)
4393 {
4394 struct bfd_link_needed_list **pn;
66eb6687 4395 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4396 ;
4397 *pn = rpath;
4398 }
4399
9acc85a6
AM
4400 /* If we have a PT_GNU_RELRO program header, mark as read-only
4401 all sections contained fully therein. This makes relro
4402 shared library sections appear as they will at run-time. */
4403 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
54025d58 4404 while (phdr-- > elf_tdata (abfd)->phdr)
9acc85a6
AM
4405 if (phdr->p_type == PT_GNU_RELRO)
4406 {
4407 for (s = abfd->sections; s != NULL; s = s->next)
502794d4
CE
4408 {
4409 unsigned int opb = bfd_octets_per_byte (abfd, s);
4410
4411 if ((s->flags & SEC_ALLOC) != 0
4412 && s->vma * opb >= phdr->p_vaddr
4413 && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4414 s->flags |= SEC_READONLY;
4415 }
9acc85a6
AM
4416 break;
4417 }
4418
4ad4eba5
AM
4419 /* We do not want to include any of the sections in a dynamic
4420 object in the output file. We hack by simply clobbering the
4421 list of sections in the BFD. This could be handled more
4422 cleanly by, say, a new section flag; the existing
4423 SEC_NEVER_LOAD flag is not the one we want, because that one
4424 still implies that the section takes up space in the output
4425 file. */
4426 bfd_section_list_clear (abfd);
4427
4ad4eba5
AM
4428 /* Find the name to use in a DT_NEEDED entry that refers to this
4429 object. If the object has a DT_SONAME entry, we use it.
4430 Otherwise, if the generic linker stuck something in
4431 elf_dt_name, we use that. Otherwise, we just use the file
4432 name. */
4433 if (soname == NULL || *soname == '\0')
4434 {
4435 soname = elf_dt_name (abfd);
4436 if (soname == NULL || *soname == '\0')
4437 soname = bfd_get_filename (abfd);
4438 }
4439
4440 /* Save the SONAME because sometimes the linker emulation code
4441 will need to know it. */
4442 elf_dt_name (abfd) = soname;
4443
4ad4eba5
AM
4444 /* If we have already included this dynamic object in the
4445 link, just ignore it. There is no reason to include a
4446 particular dynamic object more than once. */
e310298c
AM
4447 for (loaded_lib = htab->dyn_loaded;
4448 loaded_lib != NULL;
4449 loaded_lib = loaded_lib->next)
4450 {
4451 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
0a1b45a2 4452 return true;
e310298c
AM
4453 }
4454
4455 /* Create dynamic sections for backends that require that be done
4456 before setup_gnu_properties. */
4457 if (add_needed
4458 && !_bfd_elf_link_create_dynamic_sections (abfd, info))
0a1b45a2 4459 return false;
7ee314fa
AM
4460
4461 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 4462 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
4463 }
4464
4465 /* If this is a dynamic object, we always link against the .dynsym
4466 symbol table, not the .symtab symbol table. The dynamic linker
4467 will only see the .dynsym symbol table, so there is no reason to
4468 look at .symtab for a dynamic object. */
4469
4470 if (! dynamic || elf_dynsymtab (abfd) == 0)
4471 hdr = &elf_tdata (abfd)->symtab_hdr;
4472 else
4473 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4474
4475 symcount = hdr->sh_size / bed->s->sizeof_sym;
4476
4477 /* The sh_info field of the symtab header tells us where the
4478 external symbols start. We don't care about the local symbols at
4479 this point. */
4480 if (elf_bad_symtab (abfd))
4481 {
4482 extsymcount = symcount;
4483 extsymoff = 0;
4484 }
4485 else
4486 {
4487 extsymcount = symcount - hdr->sh_info;
4488 extsymoff = hdr->sh_info;
4489 }
4490
f45794cb 4491 sym_hash = elf_sym_hashes (abfd);
012b2306 4492 if (extsymcount != 0)
4ad4eba5
AM
4493 {
4494 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4495 NULL, NULL, NULL);
4496 if (isymbuf == NULL)
4497 goto error_return;
4498
4ad4eba5 4499 if (sym_hash == NULL)
012b2306
AM
4500 {
4501 /* We store a pointer to the hash table entry for each
4502 external symbol. */
986f0783 4503 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
012b2306
AM
4504 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4505 if (sym_hash == NULL)
4506 goto error_free_sym;
4507 elf_sym_hashes (abfd) = sym_hash;
4508 }
4ad4eba5
AM
4509 }
4510
4511 if (dynamic)
4512 {
4513 /* Read in any version definitions. */
fc0e6df6
PB
4514 if (!_bfd_elf_slurp_version_tables (abfd,
4515 info->default_imported_symver))
4ad4eba5
AM
4516 goto error_free_sym;
4517
4518 /* Read in the symbol versions, but don't bother to convert them
4519 to internal format. */
4520 if (elf_dynversym (abfd) != 0)
4521 {
986f0783
AM
4522 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4523 bfd_size_type amt = versymhdr->sh_size;
4ad4eba5 4524
2bb3687b
AM
4525 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4526 goto error_free_sym;
4527 extversym = (Elf_External_Versym *)
4528 _bfd_malloc_and_read (abfd, amt, amt);
4ad4eba5
AM
4529 if (extversym == NULL)
4530 goto error_free_sym;
986f0783 4531 extversym_end = extversym + amt / sizeof (*extversym);
4ad4eba5
AM
4532 }
4533 }
4534
66eb6687
AM
4535 /* If we are loading an as-needed shared lib, save the symbol table
4536 state before we start adding symbols. If the lib turns out
4537 to be unneeded, restore the state. */
4538 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4539 {
4540 unsigned int i;
4541 size_t entsize;
4542
4543 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4544 {
4545 struct bfd_hash_entry *p;
2de92251 4546 struct elf_link_hash_entry *h;
66eb6687
AM
4547
4548 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
4549 {
4550 h = (struct elf_link_hash_entry *) p;
4551 entsize += htab->root.table.entsize;
4552 if (h->root.type == bfd_link_hash_warning)
7ba11550
AM
4553 {
4554 entsize += htab->root.table.entsize;
4555 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4556 }
4557 if (h->root.type == bfd_link_hash_common)
4558 entsize += sizeof (*h->root.u.c.p);
2de92251 4559 }
66eb6687
AM
4560 }
4561
4562 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 4563 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
4564 if (old_tab == NULL)
4565 goto error_free_vers;
4566
4567 /* Remember the current objalloc pointer, so that all mem for
4568 symbols added can later be reclaimed. */
4569 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4570 if (alloc_mark == NULL)
4571 goto error_free_vers;
4572
5061a885
AM
4573 /* Make a special call to the linker "notice" function to
4574 tell it that we are about to handle an as-needed lib. */
e5034e59 4575 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 4576 goto error_free_vers;
5061a885 4577
f45794cb
AM
4578 /* Clone the symbol table. Remember some pointers into the
4579 symbol table, and dynamic symbol count. */
4580 old_ent = (char *) old_tab + tabsize;
66eb6687 4581 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
4582 old_undefs = htab->root.undefs;
4583 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
4584 old_table = htab->root.table.table;
4585 old_size = htab->root.table.size;
4586 old_count = htab->root.table.count;
e310298c
AM
4587 old_strtab = NULL;
4588 if (htab->dynstr != NULL)
4589 {
4590 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4591 if (old_strtab == NULL)
4592 goto error_free_vers;
4593 }
66eb6687
AM
4594
4595 for (i = 0; i < htab->root.table.size; i++)
4596 {
4597 struct bfd_hash_entry *p;
2de92251 4598 struct elf_link_hash_entry *h;
66eb6687
AM
4599
4600 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4601 {
2de92251 4602 h = (struct elf_link_hash_entry *) p;
7ba11550
AM
4603 memcpy (old_ent, h, htab->root.table.entsize);
4604 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4605 if (h->root.type == bfd_link_hash_warning)
4606 {
7ba11550
AM
4607 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4608 memcpy (old_ent, h, htab->root.table.entsize);
2de92251
AM
4609 old_ent = (char *) old_ent + htab->root.table.entsize;
4610 }
7ba11550
AM
4611 if (h->root.type == bfd_link_hash_common)
4612 {
4613 memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
4614 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
4615 }
66eb6687
AM
4616 }
4617 }
4618 }
4ad4eba5 4619
66eb6687 4620 weaks = NULL;
be22c732
NC
4621 if (extversym == NULL)
4622 ever = NULL;
4623 else if (extversym + extsymoff < extversym_end)
4624 ever = extversym + extsymoff;
4625 else
4626 {
4627 /* xgettext:c-format */
4628 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4629 abfd, (long) extsymoff,
4630 (long) (extversym_end - extversym) / sizeof (* extversym));
4631 bfd_set_error (bfd_error_bad_value);
4632 goto error_free_vers;
4633 }
4634
b4c555cf
ML
4635 if (!bfd_link_relocatable (info)
4636 && abfd->lto_slim_object)
cc5277b1
ML
4637 {
4638 _bfd_error_handler
4639 (_("%pB: plugin needed to handle lto object"), abfd);
4640 }
4641
4ad4eba5
AM
4642 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4643 isym < isymend;
4644 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4645 {
4646 int bind;
4647 bfd_vma value;
af44c138 4648 asection *sec, *new_sec;
4ad4eba5
AM
4649 flagword flags;
4650 const char *name;
4651 struct elf_link_hash_entry *h;
90c984fc 4652 struct elf_link_hash_entry *hi;
0a1b45a2
AM
4653 bool definition;
4654 bool size_change_ok;
4655 bool type_change_ok;
4656 bool new_weak;
4657 bool old_weak;
7ba11550 4658 bfd *override;
0a1b45a2
AM
4659 bool common;
4660 bool discarded;
4ad4eba5 4661 unsigned int old_alignment;
4538d1c7 4662 unsigned int shindex;
4ad4eba5 4663 bfd *old_bfd;
0a1b45a2 4664 bool matched;
4ad4eba5 4665
7ba11550 4666 override = NULL;
4ad4eba5
AM
4667
4668 flags = BSF_NO_FLAGS;
4669 sec = NULL;
4670 value = isym->st_value;
a4d8e49b 4671 common = bed->common_definition (isym);
2980ccad
L
4672 if (common && info->inhibit_common_definition)
4673 {
4674 /* Treat common symbol as undefined for --no-define-common. */
4675 isym->st_shndx = SHN_UNDEF;
0a1b45a2 4676 common = false;
2980ccad 4677 }
0a1b45a2 4678 discarded = false;
4ad4eba5
AM
4679
4680 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 4681 switch (bind)
4ad4eba5 4682 {
3e7a7d11 4683 case STB_LOCAL:
4ad4eba5
AM
4684 /* This should be impossible, since ELF requires that all
4685 global symbols follow all local symbols, and that sh_info
4686 point to the first global symbol. Unfortunately, Irix 5
4687 screws this up. */
fe3fef62
AM
4688 if (elf_bad_symtab (abfd))
4689 continue;
4690
4691 /* If we aren't prepared to handle locals within the globals
4538d1c7
AM
4692 then we'll likely segfault on a NULL symbol hash if the
4693 symbol is ever referenced in relocations. */
4694 shindex = elf_elfheader (abfd)->e_shstrndx;
4695 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4696 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4697 " (>= sh_info of %lu)"),
4698 abfd, name, (long) (isym - isymbuf + extsymoff),
4699 (long) extsymoff);
4700
4701 /* Dynamic object relocations are not processed by ld, so
4702 ld won't run into the problem mentioned above. */
4703 if (dynamic)
4704 continue;
fe3fef62
AM
4705 bfd_set_error (bfd_error_bad_value);
4706 goto error_free_vers;
3e7a7d11
NC
4707
4708 case STB_GLOBAL:
a4d8e49b 4709 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4710 flags = BSF_GLOBAL;
3e7a7d11
NC
4711 break;
4712
4713 case STB_WEAK:
4714 flags = BSF_WEAK;
4715 break;
4716
4717 case STB_GNU_UNIQUE:
4718 flags = BSF_GNU_UNIQUE;
4719 break;
4720
4721 default:
4ad4eba5 4722 /* Leave it up to the processor backend. */
3e7a7d11 4723 break;
4ad4eba5
AM
4724 }
4725
4726 if (isym->st_shndx == SHN_UNDEF)
4727 sec = bfd_und_section_ptr;
cb33740c
AM
4728 else if (isym->st_shndx == SHN_ABS)
4729 sec = bfd_abs_section_ptr;
4730 else if (isym->st_shndx == SHN_COMMON)
4731 {
4732 sec = bfd_com_section_ptr;
4733 /* What ELF calls the size we call the value. What ELF
4734 calls the value we call the alignment. */
4735 value = isym->st_size;
4736 }
4737 else
4ad4eba5
AM
4738 {
4739 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4740 if (sec == NULL)
4741 sec = bfd_abs_section_ptr;
dbaa2011 4742 else if (discarded_section (sec))
529fcb95 4743 {
e5d08002
L
4744 /* Symbols from discarded section are undefined. We keep
4745 its visibility. */
529fcb95 4746 sec = bfd_und_section_ptr;
0a1b45a2 4747 discarded = true;
529fcb95
PB
4748 isym->st_shndx = SHN_UNDEF;
4749 }
4ad4eba5
AM
4750 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4751 value -= sec->vma;
4752 }
4ad4eba5
AM
4753
4754 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4755 isym->st_name);
4756 if (name == NULL)
4757 goto error_free_vers;
4758
4759 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4760 && (abfd->flags & BFD_PLUGIN) != 0)
4761 {
4762 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4763
4764 if (xc == NULL)
4765 {
4766 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4767 | SEC_EXCLUDE);
4768 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4769 if (xc == NULL)
4770 goto error_free_vers;
4771 }
4772 sec = xc;
4773 }
4774 else if (isym->st_shndx == SHN_COMMON
4775 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4776 && !bfd_link_relocatable (info))
4ad4eba5
AM
4777 {
4778 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4779
4780 if (tcomm == NULL)
4781 {
02d00247
AM
4782 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4783 | SEC_LINKER_CREATED);
4784 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4785 if (tcomm == NULL)
4ad4eba5
AM
4786 goto error_free_vers;
4787 }
4788 sec = tcomm;
4789 }
66eb6687 4790 else if (bed->elf_add_symbol_hook)
4ad4eba5 4791 {
66eb6687
AM
4792 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4793 &sec, &value))
4ad4eba5
AM
4794 goto error_free_vers;
4795
4796 /* The hook function sets the name to NULL if this symbol
4797 should be skipped for some reason. */
4798 if (name == NULL)
4799 continue;
4800 }
4801
4802 /* Sanity check that all possibilities were handled. */
4803 if (sec == NULL)
4538d1c7 4804 abort ();
4ad4eba5 4805
191c0c42
AM
4806 /* Silently discard TLS symbols from --just-syms. There's
4807 no way to combine a static TLS block with a new TLS block
4808 for this executable. */
4809 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4810 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4811 continue;
4812
4ad4eba5
AM
4813 if (bfd_is_und_section (sec)
4814 || bfd_is_com_section (sec))
0a1b45a2 4815 definition = false;
4ad4eba5 4816 else
0a1b45a2 4817 definition = true;
4ad4eba5 4818
0a1b45a2 4819 size_change_ok = false;
66eb6687 4820 type_change_ok = bed->type_change_ok;
0a1b45a2
AM
4821 old_weak = false;
4822 matched = false;
4ad4eba5
AM
4823 old_alignment = 0;
4824 old_bfd = NULL;
af44c138 4825 new_sec = sec;
4ad4eba5 4826
2cc15b10 4827 if (is_elf_hash_table (&htab->root))
4ad4eba5
AM
4828 {
4829 Elf_Internal_Versym iver;
4830 unsigned int vernum = 0;
0a1b45a2 4831 bool skip;
4ad4eba5 4832
fc0e6df6 4833 if (ever == NULL)
4ad4eba5 4834 {
fc0e6df6
PB
4835 if (info->default_imported_symver)
4836 /* Use the default symbol version created earlier. */
4837 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4838 else
4839 iver.vs_vers = 0;
4840 }
be22c732
NC
4841 else if (ever >= extversym_end)
4842 {
4843 /* xgettext:c-format */
4844 _bfd_error_handler (_("%pB: not enough version information"),
4845 abfd);
4846 bfd_set_error (bfd_error_bad_value);
4847 goto error_free_vers;
4848 }
fc0e6df6
PB
4849 else
4850 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4851
4852 vernum = iver.vs_vers & VERSYM_VERSION;
4853
4854 /* If this is a hidden symbol, or if it is not version
4855 1, we append the version name to the symbol name.
cc86ff91
EB
4856 However, we do not modify a non-hidden absolute symbol
4857 if it is not a function, because it might be the version
4858 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4859 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4860 || (vernum > 1
4861 && (!bfd_is_abs_section (sec)
4862 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4863 {
4864 const char *verstr;
4865 size_t namelen, verlen, newlen;
4866 char *newname, *p;
4867
4868 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4869 {
fc0e6df6
PB
4870 if (vernum > elf_tdata (abfd)->cverdefs)
4871 verstr = NULL;
4872 else if (vernum > 1)
4873 verstr =
4874 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4875 else
4876 verstr = "";
4ad4eba5 4877
fc0e6df6 4878 if (verstr == NULL)
4ad4eba5 4879 {
4eca0228 4880 _bfd_error_handler
695344c0 4881 /* xgettext:c-format */
871b3ab2 4882 (_("%pB: %s: invalid version %u (max %d)"),
fc0e6df6
PB
4883 abfd, name, vernum,
4884 elf_tdata (abfd)->cverdefs);
4885 bfd_set_error (bfd_error_bad_value);
4886 goto error_free_vers;
4ad4eba5 4887 }
fc0e6df6
PB
4888 }
4889 else
4890 {
4891 /* We cannot simply test for the number of
4892 entries in the VERNEED section since the
4893 numbers for the needed versions do not start
4894 at 0. */
4895 Elf_Internal_Verneed *t;
4896
4897 verstr = NULL;
4898 for (t = elf_tdata (abfd)->verref;
4899 t != NULL;
4900 t = t->vn_nextref)
4ad4eba5 4901 {
fc0e6df6 4902 Elf_Internal_Vernaux *a;
4ad4eba5 4903
fc0e6df6
PB
4904 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4905 {
4906 if (a->vna_other == vernum)
4ad4eba5 4907 {
fc0e6df6
PB
4908 verstr = a->vna_nodename;
4909 break;
4ad4eba5 4910 }
4ad4eba5 4911 }
fc0e6df6
PB
4912 if (a != NULL)
4913 break;
4914 }
4915 if (verstr == NULL)
4916 {
4eca0228 4917 _bfd_error_handler
695344c0 4918 /* xgettext:c-format */
871b3ab2 4919 (_("%pB: %s: invalid needed version %d"),
fc0e6df6
PB
4920 abfd, name, vernum);
4921 bfd_set_error (bfd_error_bad_value);
4922 goto error_free_vers;
4ad4eba5 4923 }
4ad4eba5 4924 }
fc0e6df6
PB
4925
4926 namelen = strlen (name);
4927 verlen = strlen (verstr);
4928 newlen = namelen + verlen + 2;
4929 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4930 && isym->st_shndx != SHN_UNDEF)
4931 ++newlen;
4932
a50b1753 4933 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4934 if (newname == NULL)
4935 goto error_free_vers;
4936 memcpy (newname, name, namelen);
4937 p = newname + namelen;
4938 *p++ = ELF_VER_CHR;
4939 /* If this is a defined non-hidden version symbol,
4940 we add another @ to the name. This indicates the
4941 default version of the symbol. */
4942 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4943 && isym->st_shndx != SHN_UNDEF)
4944 *p++ = ELF_VER_CHR;
4945 memcpy (p, verstr, verlen + 1);
4946
4947 name = newname;
4ad4eba5
AM
4948 }
4949
cd3416da
AM
4950 /* If this symbol has default visibility and the user has
4951 requested we not re-export it, then mark it as hidden. */
a0d49154 4952 if (!bfd_is_und_section (sec)
cd3416da 4953 && !dynamic
ce875075 4954 && abfd->no_export
cd3416da
AM
4955 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4956 isym->st_other = (STV_HIDDEN
4957 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4958
4f3fedcf
AM
4959 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4960 sym_hash, &old_bfd, &old_weak,
4961 &old_alignment, &skip, &override,
6e33951e
L
4962 &type_change_ok, &size_change_ok,
4963 &matched))
4ad4eba5
AM
4964 goto error_free_vers;
4965
4966 if (skip)
4967 continue;
4968
6e33951e
L
4969 /* Override a definition only if the new symbol matches the
4970 existing one. */
4971 if (override && matched)
0a1b45a2 4972 definition = false;
4ad4eba5
AM
4973
4974 h = *sym_hash;
4975 while (h->root.type == bfd_link_hash_indirect
4976 || h->root.type == bfd_link_hash_warning)
4977 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4978
4ad4eba5 4979 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4980 && vernum > 1
4981 && definition)
4982 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4983 }
4984
4985 if (! (_bfd_generic_link_add_one_symbol
7ba11550 4986 (info, override ? override : abfd, name, flags, sec, value,
0a1b45a2 4987 NULL, false, bed->collect,
4ad4eba5
AM
4988 (struct bfd_link_hash_entry **) sym_hash)))
4989 goto error_free_vers;
4990
4991 h = *sym_hash;
90c984fc
L
4992 /* We need to make sure that indirect symbol dynamic flags are
4993 updated. */
4994 hi = h;
4ad4eba5
AM
4995 while (h->root.type == bfd_link_hash_indirect
4996 || h->root.type == bfd_link_hash_warning)
4997 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4998
d1cbe007
AM
4999 *sym_hash = h;
5000
97196564
L
5001 /* Setting the index to -3 tells elf_link_output_extsym that
5002 this symbol is defined in a discarded section. */
2cc15b10 5003 if (discarded && is_elf_hash_table (&htab->root))
97196564
L
5004 h->indx = -3;
5005
37a9e49a 5006 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
5007 if (dynamic
5008 && definition
37a9e49a 5009 && new_weak
fcb93ecf 5010 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
2cc15b10 5011 && is_elf_hash_table (&htab->root)
60d67dc8 5012 && h->u.alias == NULL)
4ad4eba5
AM
5013 {
5014 /* Keep a list of all weak defined non function symbols from
60d67dc8
AM
5015 a dynamic object, using the alias field. Later in this
5016 function we will set the alias field to the correct
4ad4eba5
AM
5017 value. We only put non-function symbols from dynamic
5018 objects on this list, because that happens to be the only
5019 time we need to know the normal symbol corresponding to a
5020 weak symbol, and the information is time consuming to
60d67dc8 5021 figure out. If the alias field is not already NULL,
4ad4eba5
AM
5022 then this symbol was already defined by some previous
5023 dynamic object, and we will be using that previous
5024 definition anyhow. */
5025
60d67dc8 5026 h->u.alias = weaks;
4ad4eba5 5027 weaks = h;
4ad4eba5
AM
5028 }
5029
5030 /* Set the alignment of a common symbol. */
a4d8e49b 5031 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
5032 && h->root.type == bfd_link_hash_common)
5033 {
5034 unsigned int align;
5035
a4d8e49b 5036 if (common)
af44c138
L
5037 align = bfd_log2 (isym->st_value);
5038 else
5039 {
5040 /* The new symbol is a common symbol in a shared object.
5041 We need to get the alignment from the section. */
5042 align = new_sec->alignment_power;
5043 }
595213d4 5044 if (align > old_alignment)
4ad4eba5
AM
5045 h->root.u.c.p->alignment_power = align;
5046 else
5047 h->root.u.c.p->alignment_power = old_alignment;
5048 }
5049
2cc15b10 5050 if (is_elf_hash_table (&htab->root))
4ad4eba5 5051 {
4f3fedcf
AM
5052 /* Set a flag in the hash table entry indicating the type of
5053 reference or definition we just found. A dynamic symbol
5054 is one which is referenced or defined by both a regular
5055 object and a shared object. */
0a1b45a2 5056 bool dynsym = false;
4f3fedcf 5057
b1a92c63
AM
5058 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5059 if ((abfd->flags & BFD_PLUGIN) != 0)
bbaddd4b
AM
5060 {
5061 /* Except for this flag to track nonweak references. */
5062 if (!definition
5063 && bind != STB_WEAK)
5064 h->ref_ir_nonweak = 1;
5065 }
b1a92c63 5066 else if (!dynamic)
4f3fedcf
AM
5067 {
5068 if (! definition)
5069 {
5070 h->ref_regular = 1;
5071 if (bind != STB_WEAK)
5072 h->ref_regular_nonweak = 1;
5073 }
5074 else
5075 {
5076 h->def_regular = 1;
5077 if (h->def_dynamic)
5078 {
5079 h->def_dynamic = 0;
5080 h->ref_dynamic = 1;
5081 }
5082 }
4f3fedcf
AM
5083 }
5084 else
5085 {
5086 if (! definition)
5087 {
5088 h->ref_dynamic = 1;
5089 hi->ref_dynamic = 1;
5090 }
5091 else
5092 {
5093 h->def_dynamic = 1;
5094 hi->def_dynamic = 1;
5095 }
b1a92c63 5096 }
4f3fedcf 5097
b1a92c63
AM
5098 /* If an indirect symbol has been forced local, don't
5099 make the real symbol dynamic. */
5100 if (h != hi && hi->forced_local)
5101 ;
5102 else if (!dynamic)
5103 {
5104 if (bfd_link_dll (info)
5105 || h->def_dynamic
5106 || h->ref_dynamic)
0a1b45a2 5107 dynsym = true;
b1a92c63
AM
5108 }
5109 else
5110 {
5111 if (h->def_regular
5112 || h->ref_regular
5113 || (h->is_weakalias
5114 && weakdef (h)->dynindx != -1))
0a1b45a2 5115 dynsym = true;
4f3fedcf
AM
5116 }
5117
5118 /* Check to see if we need to add an indirect symbol for
5119 the default name. */
726d7d1e
AM
5120 if ((definition
5121 || (!override && h->root.type == bfd_link_hash_common))
5122 && !(hi != h
5123 && hi->versioned == versioned_hidden))
4f3fedcf 5124 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
f01fb44c 5125 sec, value, &old_bfd, &dynsym))
4f3fedcf 5126 goto error_free_vers;
4ad4eba5
AM
5127
5128 /* Check the alignment when a common symbol is involved. This
5129 can change when a common symbol is overridden by a normal
5130 definition or a common symbol is ignored due to the old
5131 normal definition. We need to make sure the maximum
5132 alignment is maintained. */
a4d8e49b 5133 if ((old_alignment || common)
4ad4eba5
AM
5134 && h->root.type != bfd_link_hash_common)
5135 {
5136 unsigned int common_align;
5137 unsigned int normal_align;
5138 unsigned int symbol_align;
5139 bfd *normal_bfd;
5140 bfd *common_bfd;
5141
3a81e825
AM
5142 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5143 || h->root.type == bfd_link_hash_defweak);
5144
4ad4eba5
AM
5145 symbol_align = ffs (h->root.u.def.value) - 1;
5146 if (h->root.u.def.section->owner != NULL
0616a280
AM
5147 && (h->root.u.def.section->owner->flags
5148 & (DYNAMIC | BFD_PLUGIN)) == 0)
4ad4eba5
AM
5149 {
5150 normal_align = h->root.u.def.section->alignment_power;
5151 if (normal_align > symbol_align)
5152 normal_align = symbol_align;
5153 }
5154 else
5155 normal_align = symbol_align;
5156
5157 if (old_alignment)
5158 {
5159 common_align = old_alignment;
5160 common_bfd = old_bfd;
5161 normal_bfd = abfd;
5162 }
5163 else
5164 {
5165 common_align = bfd_log2 (isym->st_value);
5166 common_bfd = abfd;
5167 normal_bfd = old_bfd;
5168 }
5169
5170 if (normal_align < common_align)
d07676f8
NC
5171 {
5172 /* PR binutils/2735 */
5173 if (normal_bfd == NULL)
4eca0228 5174 _bfd_error_handler
695344c0 5175 /* xgettext:c-format */
9793eb77 5176 (_("warning: alignment %u of common symbol `%s' in %pB is"
871b3ab2 5177 " greater than the alignment (%u) of its section %pA"),
c08bb8dd
AM
5178 1 << common_align, name, common_bfd,
5179 1 << normal_align, h->root.u.def.section);
d07676f8 5180 else
4eca0228 5181 _bfd_error_handler
695344c0 5182 /* xgettext:c-format */
9793eb77 5183 (_("warning: alignment %u of symbol `%s' in %pB"
871b3ab2 5184 " is smaller than %u in %pB"),
c08bb8dd
AM
5185 1 << normal_align, name, normal_bfd,
5186 1 << common_align, common_bfd);
d07676f8 5187 }
4ad4eba5
AM
5188 }
5189
83ad0046 5190 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
5191 if (isym->st_size != 0
5192 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
5193 && (definition || h->size == 0))
5194 {
83ad0046
L
5195 if (h->size != 0
5196 && h->size != isym->st_size
5197 && ! size_change_ok)
4eca0228 5198 _bfd_error_handler
695344c0 5199 /* xgettext:c-format */
9793eb77 5200 (_("warning: size of symbol `%s' changed"
2dcf00ce
AM
5201 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5202 name, (uint64_t) h->size, old_bfd,
5203 (uint64_t) isym->st_size, abfd);
4ad4eba5
AM
5204
5205 h->size = isym->st_size;
5206 }
5207
5208 /* If this is a common symbol, then we always want H->SIZE
5209 to be the size of the common symbol. The code just above
5210 won't fix the size if a common symbol becomes larger. We
5211 don't warn about a size change here, because that is
4f3fedcf 5212 covered by --warn-common. Allow changes between different
fcb93ecf 5213 function types. */
4ad4eba5
AM
5214 if (h->root.type == bfd_link_hash_common)
5215 h->size = h->root.u.c.size;
5216
5217 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
5218 && ((definition && !new_weak)
5219 || (old_weak && h->root.type == bfd_link_hash_common)
5220 || h->type == STT_NOTYPE))
4ad4eba5 5221 {
2955ec4c
L
5222 unsigned int type = ELF_ST_TYPE (isym->st_info);
5223
5224 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5225 symbol. */
5226 if (type == STT_GNU_IFUNC
5227 && (abfd->flags & DYNAMIC) != 0)
5228 type = STT_FUNC;
4ad4eba5 5229
2955ec4c
L
5230 if (h->type != type)
5231 {
5232 if (h->type != STT_NOTYPE && ! type_change_ok)
695344c0 5233 /* xgettext:c-format */
4eca0228 5234 _bfd_error_handler
9793eb77 5235 (_("warning: type of symbol `%s' changed"
871b3ab2 5236 " from %d to %d in %pB"),
c08bb8dd 5237 name, h->type, type, abfd);
2955ec4c
L
5238
5239 h->type = type;
5240 }
4ad4eba5
AM
5241 }
5242
54ac0771 5243 /* Merge st_other field. */
5160d0f3
AM
5244 elf_merge_st_other (abfd, h, isym->st_other, sec,
5245 definition, dynamic);
4ad4eba5 5246
c3df8c14 5247 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
5248 if (definition
5249 && (sec->flags & SEC_DEBUGGING)
5250 && !bfd_link_relocatable (info))
0a1b45a2 5251 dynsym = false;
c3df8c14 5252
b1a92c63
AM
5253 /* Nor should we make plugin symbols dynamic. */
5254 if ((abfd->flags & BFD_PLUGIN) != 0)
0a1b45a2 5255 dynsym = false;
b1a92c63 5256
35fc36a8 5257 if (definition)
35399224
L
5258 {
5259 h->target_internal = isym->st_target_internal;
5260 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5261 }
35fc36a8 5262
4ad4eba5
AM
5263 if (definition && !dynamic)
5264 {
5265 char *p = strchr (name, ELF_VER_CHR);
5266 if (p != NULL && p[1] != ELF_VER_CHR)
5267 {
5268 /* Queue non-default versions so that .symver x, x@FOO
5269 aliases can be checked. */
66eb6687 5270 if (!nondeflt_vers)
4ad4eba5 5271 {
986f0783
AM
5272 size_t amt = ((isymend - isym + 1)
5273 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
5274 nondeflt_vers
5275 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
5276 if (!nondeflt_vers)
5277 goto error_free_vers;
4ad4eba5 5278 }
66eb6687 5279 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
5280 }
5281 }
5282
b1a92c63 5283 if (dynsym && h->dynindx == -1)
4ad4eba5 5284 {
c152c796 5285 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 5286 goto error_free_vers;
60d67dc8
AM
5287 if (h->is_weakalias
5288 && weakdef (h)->dynindx == -1)
4ad4eba5 5289 {
60d67dc8 5290 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
4ad4eba5
AM
5291 goto error_free_vers;
5292 }
5293 }
1f599d0e 5294 else if (h->dynindx != -1)
4ad4eba5
AM
5295 /* If the symbol already has a dynamic index, but
5296 visibility says it should not be visible, turn it into
5297 a local symbol. */
5298 switch (ELF_ST_VISIBILITY (h->other))
5299 {
5300 case STV_INTERNAL:
5301 case STV_HIDDEN:
0a1b45a2
AM
5302 (*bed->elf_backend_hide_symbol) (info, h, true);
5303 dynsym = false;
4ad4eba5
AM
5304 break;
5305 }
5306
5307 if (!add_needed
aef28989 5308 && matched
4ad4eba5 5309 && definition
f01fb44c 5310 && h->root.type != bfd_link_hash_indirect
010e5ae2 5311 && ((dynsym
a896df97 5312 && h->ref_regular_nonweak)
b1a92c63
AM
5313 || (old_bfd != NULL
5314 && (old_bfd->flags & BFD_PLUGIN) != 0
bbaddd4b
AM
5315 && h->ref_ir_nonweak
5316 && !info->lto_all_symbols_read)
ffa9430d 5317 || (h->ref_dynamic_nonweak
010e5ae2 5318 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
7b15fa7a
AM
5319 && !on_needed_list (elf_dt_name (abfd),
5320 htab->needed, NULL))))
4ad4eba5 5321 {
4ad4eba5
AM
5322 const char *soname = elf_dt_name (abfd);
5323
16e4ecc0
AM
5324 info->callbacks->minfo ("%!", soname, old_bfd,
5325 h->root.root.string);
5326
4ad4eba5
AM
5327 /* A symbol from a library loaded via DT_NEEDED of some
5328 other library is referenced by a regular object.
e56f61be 5329 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
5330 --no-add-needed is used and the reference was not
5331 a weak one. */
4f3fedcf 5332 if (old_bfd != NULL
b918acf9 5333 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be 5334 {
4eca0228 5335 _bfd_error_handler
695344c0 5336 /* xgettext:c-format */
871b3ab2 5337 (_("%pB: undefined reference to symbol '%s'"),
4f3fedcf 5338 old_bfd, name);
ff5ac77b 5339 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
5340 goto error_free_vers;
5341 }
5342
a50b1753 5343 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 5344 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 5345
e310298c
AM
5346 /* Create dynamic sections for backends that require
5347 that be done before setup_gnu_properties. */
5348 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
0a1b45a2
AM
5349 return false;
5350 add_needed = true;
4ad4eba5
AM
5351 }
5352 }
5353 }
5354
a83ef4d1
L
5355 if (info->lto_plugin_active
5356 && !bfd_link_relocatable (info)
5357 && (abfd->flags & BFD_PLUGIN) == 0
5358 && !just_syms
5359 && extsymcount)
5360 {
5361 int r_sym_shift;
5362
5363 if (bed->s->arch_size == 32)
5364 r_sym_shift = 8;
5365 else
5366 r_sym_shift = 32;
5367
5368 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5369 referenced in regular objects so that linker plugin will get
5370 the correct symbol resolution. */
5371
5372 sym_hash = elf_sym_hashes (abfd);
5373 for (s = abfd->sections; s != NULL; s = s->next)
5374 {
5375 Elf_Internal_Rela *internal_relocs;
5376 Elf_Internal_Rela *rel, *relend;
5377
5378 /* Don't check relocations in excluded sections. */
5379 if ((s->flags & SEC_RELOC) == 0
5380 || s->reloc_count == 0
5381 || (s->flags & SEC_EXCLUDE) != 0
5382 || ((info->strip == strip_all
5383 || info->strip == strip_debugger)
5384 && (s->flags & SEC_DEBUGGING) != 0))
5385 continue;
5386
a8dde0a2
L
5387 internal_relocs = _bfd_elf_link_info_read_relocs (abfd, info,
5388 s, NULL,
5389 NULL,
5390 _bfd_link_keep_memory (info));
a83ef4d1
L
5391 if (internal_relocs == NULL)
5392 goto error_free_vers;
5393
5394 rel = internal_relocs;
5395 relend = rel + s->reloc_count;
5396 for ( ; rel < relend; rel++)
5397 {
5398 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5399 struct elf_link_hash_entry *h;
5400
5401 /* Skip local symbols. */
5402 if (r_symndx < extsymoff)
5403 continue;
5404
5405 h = sym_hash[r_symndx - extsymoff];
5406 if (h != NULL)
5407 h->root.non_ir_ref_regular = 1;
5408 }
5409
5410 if (elf_section_data (s)->relocs != internal_relocs)
5411 free (internal_relocs);
5412 }
5413 }
5414
c9594989
AM
5415 free (extversym);
5416 extversym = NULL;
5417 free (isymbuf);
5418 isymbuf = NULL;
66eb6687
AM
5419
5420 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5421 {
5422 unsigned int i;
5423
5424 /* Restore the symbol table. */
f45794cb
AM
5425 old_ent = (char *) old_tab + tabsize;
5426 memset (elf_sym_hashes (abfd), 0,
5427 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
5428 htab->root.table.table = old_table;
5429 htab->root.table.size = old_size;
5430 htab->root.table.count = old_count;
66eb6687 5431 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
5432 htab->root.undefs = old_undefs;
5433 htab->root.undefs_tail = old_undefs_tail;
e310298c
AM
5434 if (htab->dynstr != NULL)
5435 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5b677558
AM
5436 free (old_strtab);
5437 old_strtab = NULL;
66eb6687
AM
5438 for (i = 0; i < htab->root.table.size; i++)
5439 {
5440 struct bfd_hash_entry *p;
5441 struct elf_link_hash_entry *h;
4070765b 5442 unsigned int non_ir_ref_dynamic;
66eb6687
AM
5443
5444 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5445 {
4070765b 5446 /* Preserve non_ir_ref_dynamic so that this symbol
59fa66c5
L
5447 will be exported when the dynamic lib becomes needed
5448 in the second pass. */
7ba11550
AM
5449 h = (struct elf_link_hash_entry *) p;
5450 if (h->root.type == bfd_link_hash_warning)
5451 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4070765b 5452 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
7ba11550 5453
2de92251 5454 h = (struct elf_link_hash_entry *) p;
7ba11550
AM
5455 memcpy (h, old_ent, htab->root.table.entsize);
5456 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
5457 if (h->root.type == bfd_link_hash_warning)
5458 {
a4542f1b 5459 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7ba11550
AM
5460 memcpy (h, old_ent, htab->root.table.entsize);
5461 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251 5462 }
a4542f1b 5463 if (h->root.type == bfd_link_hash_common)
3e0882af 5464 {
7ba11550
AM
5465 memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5466 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
3e0882af 5467 }
4070765b 5468 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
66eb6687
AM
5469 }
5470 }
5471
5061a885
AM
5472 /* Make a special call to the linker "notice" function to
5473 tell it that symbols added for crefs may need to be removed. */
e5034e59 5474 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 5475 goto error_free_vers;
5061a885 5476
66eb6687
AM
5477 free (old_tab);
5478 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5479 alloc_mark);
c9594989 5480 free (nondeflt_vers);
0a1b45a2 5481 return true;
66eb6687 5482 }
2de92251 5483
66eb6687
AM
5484 if (old_tab != NULL)
5485 {
e5034e59 5486 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 5487 goto error_free_vers;
66eb6687
AM
5488 free (old_tab);
5489 old_tab = NULL;
5490 }
5491
c6e8a9a8
L
5492 /* Now that all the symbols from this input file are created, if
5493 not performing a relocatable link, handle .symver foo, foo@BAR
5494 such that any relocs against foo become foo@BAR. */
0e1862bb 5495 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5 5496 {
ef53be89 5497 size_t cnt, symidx;
4ad4eba5
AM
5498
5499 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5500 {
5501 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5502 char *shortname, *p;
986f0783 5503 size_t amt;
4ad4eba5
AM
5504
5505 p = strchr (h->root.root.string, ELF_VER_CHR);
5506 if (p == NULL
5507 || (h->root.type != bfd_link_hash_defined
5508 && h->root.type != bfd_link_hash_defweak))
5509 continue;
5510
5511 amt = p - h->root.root.string;
a50b1753 5512 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
5513 if (!shortname)
5514 goto error_free_vers;
4ad4eba5
AM
5515 memcpy (shortname, h->root.root.string, amt);
5516 shortname[amt] = '\0';
5517
5518 hi = (struct elf_link_hash_entry *)
66eb6687 5519 bfd_link_hash_lookup (&htab->root, shortname,
0a1b45a2 5520 false, false, false);
4ad4eba5
AM
5521 if (hi != NULL
5522 && hi->root.type == h->root.type
5523 && hi->root.u.def.value == h->root.u.def.value
5524 && hi->root.u.def.section == h->root.u.def.section)
5525 {
0a1b45a2 5526 (*bed->elf_backend_hide_symbol) (info, hi, true);
4ad4eba5
AM
5527 hi->root.type = bfd_link_hash_indirect;
5528 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 5529 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
5530 sym_hash = elf_sym_hashes (abfd);
5531 if (sym_hash)
5532 for (symidx = 0; symidx < extsymcount; ++symidx)
5533 if (sym_hash[symidx] == hi)
5534 {
5535 sym_hash[symidx] = h;
5536 break;
5537 }
5538 }
5539 free (shortname);
5540 }
5541 free (nondeflt_vers);
5542 nondeflt_vers = NULL;
5543 }
5544
60d67dc8 5545 /* Now set the alias field correctly for all the weak defined
4ad4eba5
AM
5546 symbols we found. The only way to do this is to search all the
5547 symbols. Since we only need the information for non functions in
5548 dynamic objects, that's the only time we actually put anything on
5549 the list WEAKS. We need this information so that if a regular
5550 object refers to a symbol defined weakly in a dynamic object, the
5551 real symbol in the dynamic object is also put in the dynamic
5552 symbols; we also must arrange for both symbols to point to the
5553 same memory location. We could handle the general case of symbol
5554 aliasing, but a general symbol alias can only be generated in
5555 assembler code, handling it correctly would be very time
5556 consuming, and other ELF linkers don't handle general aliasing
5557 either. */
5558 if (weaks != NULL)
5559 {
5560 struct elf_link_hash_entry **hpp;
5561 struct elf_link_hash_entry **hppend;
5562 struct elf_link_hash_entry **sorted_sym_hash;
5563 struct elf_link_hash_entry *h;
986f0783 5564 size_t sym_count, amt;
4ad4eba5
AM
5565
5566 /* Since we have to search the whole symbol list for each weak
5567 defined symbol, search time for N weak defined symbols will be
5568 O(N^2). Binary search will cut it down to O(NlogN). */
986f0783 5569 amt = extsymcount * sizeof (*sorted_sym_hash);
3a3f4bf7 5570 sorted_sym_hash = bfd_malloc (amt);
4ad4eba5
AM
5571 if (sorted_sym_hash == NULL)
5572 goto error_return;
5573 sym_hash = sorted_sym_hash;
5574 hpp = elf_sym_hashes (abfd);
5575 hppend = hpp + extsymcount;
5576 sym_count = 0;
5577 for (; hpp < hppend; hpp++)
5578 {
5579 h = *hpp;
5580 if (h != NULL
5581 && h->root.type == bfd_link_hash_defined
fcb93ecf 5582 && !bed->is_function_type (h->type))
4ad4eba5
AM
5583 {
5584 *sym_hash = h;
5585 sym_hash++;
5586 sym_count++;
5587 }
5588 }
5589
3a3f4bf7 5590 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
4ad4eba5
AM
5591 elf_sort_symbol);
5592
5593 while (weaks != NULL)
5594 {
5595 struct elf_link_hash_entry *hlook;
5596 asection *slook;
5597 bfd_vma vlook;
ed54588d 5598 size_t i, j, idx = 0;
4ad4eba5
AM
5599
5600 hlook = weaks;
60d67dc8
AM
5601 weaks = hlook->u.alias;
5602 hlook->u.alias = NULL;
4ad4eba5 5603
e3e53eed
AM
5604 if (hlook->root.type != bfd_link_hash_defined
5605 && hlook->root.type != bfd_link_hash_defweak)
5606 continue;
5607
4ad4eba5
AM
5608 slook = hlook->root.u.def.section;
5609 vlook = hlook->root.u.def.value;
5610
4ad4eba5
AM
5611 i = 0;
5612 j = sym_count;
14160578 5613 while (i != j)
4ad4eba5
AM
5614 {
5615 bfd_signed_vma vdiff;
5616 idx = (i + j) / 2;
14160578 5617 h = sorted_sym_hash[idx];
4ad4eba5
AM
5618 vdiff = vlook - h->root.u.def.value;
5619 if (vdiff < 0)
5620 j = idx;
5621 else if (vdiff > 0)
5622 i = idx + 1;
5623 else
5624 {
d3435ae8 5625 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
5626 if (sdiff < 0)
5627 j = idx;
5628 else if (sdiff > 0)
5629 i = idx + 1;
5630 else
14160578 5631 break;
4ad4eba5
AM
5632 }
5633 }
5634
5635 /* We didn't find a value/section match. */
14160578 5636 if (i == j)
4ad4eba5
AM
5637 continue;
5638
14160578
AM
5639 /* With multiple aliases, or when the weak symbol is already
5640 strongly defined, we have multiple matching symbols and
5641 the binary search above may land on any of them. Step
5642 one past the matching symbol(s). */
5643 while (++idx != j)
5644 {
5645 h = sorted_sym_hash[idx];
5646 if (h->root.u.def.section != slook
5647 || h->root.u.def.value != vlook)
5648 break;
5649 }
5650
5651 /* Now look back over the aliases. Since we sorted by size
5652 as well as value and section, we'll choose the one with
5653 the largest size. */
5654 while (idx-- != i)
4ad4eba5 5655 {
14160578 5656 h = sorted_sym_hash[idx];
4ad4eba5
AM
5657
5658 /* Stop if value or section doesn't match. */
14160578
AM
5659 if (h->root.u.def.section != slook
5660 || h->root.u.def.value != vlook)
4ad4eba5
AM
5661 break;
5662 else if (h != hlook)
5663 {
60d67dc8
AM
5664 struct elf_link_hash_entry *t;
5665
5666 hlook->u.alias = h;
5667 hlook->is_weakalias = 1;
5668 t = h;
5669 if (t->u.alias != NULL)
5670 while (t->u.alias != h)
5671 t = t->u.alias;
5672 t->u.alias = hlook;
4ad4eba5
AM
5673
5674 /* If the weak definition is in the list of dynamic
5675 symbols, make sure the real definition is put
5676 there as well. */
5677 if (hlook->dynindx != -1 && h->dynindx == -1)
5678 {
c152c796 5679 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
5680 {
5681 err_free_sym_hash:
5682 free (sorted_sym_hash);
5683 goto error_return;
5684 }
4ad4eba5
AM
5685 }
5686
5687 /* If the real definition is in the list of dynamic
5688 symbols, make sure the weak definition is put
5689 there as well. If we don't do this, then the
5690 dynamic loader might not merge the entries for the
5691 real definition and the weak definition. */
5692 if (h->dynindx != -1 && hlook->dynindx == -1)
5693 {
c152c796 5694 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 5695 goto err_free_sym_hash;
4ad4eba5
AM
5696 }
5697 break;
5698 }
5699 }
5700 }
5701
5702 free (sorted_sym_hash);
5703 }
5704
33177bb1
AM
5705 if (bed->check_directives
5706 && !(*bed->check_directives) (abfd, info))
0a1b45a2 5707 return false;
85fbca6a 5708
4ad4eba5
AM
5709 /* If this is a non-traditional link, try to optimize the handling
5710 of the .stab/.stabstr sections. */
5711 if (! dynamic
5712 && ! info->traditional_format
2cc15b10 5713 && is_elf_hash_table (&htab->root)
4ad4eba5
AM
5714 && (info->strip != strip_all && info->strip != strip_debugger))
5715 {
5716 asection *stabstr;
5717
5718 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5719 if (stabstr != NULL)
5720 {
5721 bfd_size_type string_offset = 0;
5722 asection *stab;
5723
5724 for (stab = abfd->sections; stab; stab = stab->next)
08dedd66 5725 if (startswith (stab->name, ".stab")
4ad4eba5
AM
5726 && (!stab->name[5] ||
5727 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5728 && (stab->flags & SEC_MERGE) == 0
5729 && !bfd_is_abs_section (stab->output_section))
5730 {
5731 struct bfd_elf_section_data *secdata;
5732
5733 secdata = elf_section_data (stab);
66eb6687
AM
5734 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5735 stabstr, &secdata->sec_info,
4ad4eba5
AM
5736 &string_offset))
5737 goto error_return;
5738 if (secdata->sec_info)
dbaa2011 5739 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5740 }
5741 }
5742 }
5743
e310298c 5744 if (dynamic && add_needed)
4ad4eba5
AM
5745 {
5746 /* Add this bfd to the loaded list. */
5747 struct elf_link_loaded_list *n;
5748
ca4be51c 5749 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5750 if (n == NULL)
5751 goto error_return;
5752 n->abfd = abfd;
e310298c
AM
5753 n->next = htab->dyn_loaded;
5754 htab->dyn_loaded = n;
4ad4eba5 5755 }
e310298c
AM
5756 if (dynamic && !add_needed
5757 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
5758 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
4ad4eba5 5759
0a1b45a2 5760 return true;
4ad4eba5
AM
5761
5762 error_free_vers:
c9594989
AM
5763 free (old_tab);
5764 free (old_strtab);
5765 free (nondeflt_vers);
5766 free (extversym);
4ad4eba5 5767 error_free_sym:
c9594989 5768 free (isymbuf);
4ad4eba5 5769 error_return:
0a1b45a2 5770 return false;
4ad4eba5
AM
5771}
5772
8387904d
AM
5773/* Return the linker hash table entry of a symbol that might be
5774 satisfied by an archive symbol. Return -1 on error. */
5775
b585e899 5776struct bfd_link_hash_entry *
8387904d
AM
5777_bfd_elf_archive_symbol_lookup (bfd *abfd,
5778 struct bfd_link_info *info,
5779 const char *name)
5780{
b585e899 5781 struct bfd_link_hash_entry *h;
8387904d
AM
5782 char *p, *copy;
5783 size_t len, first;
5784
b585e899 5785 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
8387904d
AM
5786 if (h != NULL)
5787 return h;
5788
5789 /* If this is a default version (the name contains @@), look up the
5790 symbol again with only one `@' as well as without the version.
5791 The effect is that references to the symbol with and without the
5792 version will be matched by the default symbol in the archive. */
5793
5794 p = strchr (name, ELF_VER_CHR);
5795 if (p == NULL || p[1] != ELF_VER_CHR)
5796 return h;
5797
5798 /* First check with only one `@'. */
5799 len = strlen (name);
a50b1753 5800 copy = (char *) bfd_alloc (abfd, len);
8387904d 5801 if (copy == NULL)
b585e899 5802 return (struct bfd_link_hash_entry *) -1;
8387904d
AM
5803
5804 first = p - name + 1;
5805 memcpy (copy, name, first);
5806 memcpy (copy + first, name + first + 1, len - first);
5807
b585e899 5808 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
8387904d
AM
5809 if (h == NULL)
5810 {
5811 /* We also need to check references to the symbol without the
5812 version. */
5813 copy[first - 1] = '\0';
b585e899 5814 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
8387904d
AM
5815 }
5816
5817 bfd_release (abfd, copy);
5818 return h;
5819}
5820
0ad989f9 5821/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5822 don't use _bfd_generic_link_add_archive_symbols because we need to
5823 handle versioned symbols.
0ad989f9
L
5824
5825 Fortunately, ELF archive handling is simpler than that done by
5826 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5827 oddities. In ELF, if we find a symbol in the archive map, and the
5828 symbol is currently undefined, we know that we must pull in that
5829 object file.
5830
5831 Unfortunately, we do have to make multiple passes over the symbol
5832 table until nothing further is resolved. */
5833
0a1b45a2 5834static bool
4ad4eba5 5835elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5836{
5837 symindex c;
13e570f8 5838 unsigned char *included = NULL;
0ad989f9 5839 carsym *symdefs;
0a1b45a2 5840 bool loop;
986f0783 5841 size_t amt;
8387904d 5842 const struct elf_backend_data *bed;
b585e899 5843 struct bfd_link_hash_entry * (*archive_symbol_lookup)
8387904d 5844 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5845
5846 if (! bfd_has_map (abfd))
5847 {
5848 /* An empty archive is a special case. */
5849 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
0a1b45a2 5850 return true;
0ad989f9 5851 bfd_set_error (bfd_error_no_armap);
0a1b45a2 5852 return false;
0ad989f9
L
5853 }
5854
5855 /* Keep track of all symbols we know to be already defined, and all
5856 files we know to be already included. This is to speed up the
5857 second and subsequent passes. */
5858 c = bfd_ardata (abfd)->symdef_count;
5859 if (c == 0)
0a1b45a2 5860 return true;
986f0783 5861 amt = c * sizeof (*included);
13e570f8
AM
5862 included = (unsigned char *) bfd_zmalloc (amt);
5863 if (included == NULL)
0a1b45a2 5864 return false;
0ad989f9
L
5865
5866 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5867 bed = get_elf_backend_data (abfd);
5868 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5869
5870 do
5871 {
5872 file_ptr last;
5873 symindex i;
5874 carsym *symdef;
5875 carsym *symdefend;
5876
0a1b45a2 5877 loop = false;
0ad989f9
L
5878 last = -1;
5879
5880 symdef = symdefs;
5881 symdefend = symdef + c;
5882 for (i = 0; symdef < symdefend; symdef++, i++)
5883 {
b585e899 5884 struct bfd_link_hash_entry *h;
0ad989f9
L
5885 bfd *element;
5886 struct bfd_link_hash_entry *undefs_tail;
5887 symindex mark;
5888
13e570f8 5889 if (included[i])
0ad989f9
L
5890 continue;
5891 if (symdef->file_offset == last)
5892 {
0a1b45a2 5893 included[i] = true;
0ad989f9
L
5894 continue;
5895 }
5896
8387904d 5897 h = archive_symbol_lookup (abfd, info, symdef->name);
b585e899 5898 if (h == (struct bfd_link_hash_entry *) -1)
8387904d 5899 goto error_return;
0ad989f9
L
5900
5901 if (h == NULL)
5902 continue;
5903
b585e899 5904 if (h->type == bfd_link_hash_undefined)
75cfe082
AM
5905 {
5906 /* If the archive element has already been loaded then one
5907 of the symbols defined by that element might have been
5908 made undefined due to being in a discarded section. */
b585e899
AM
5909 if (is_elf_hash_table (info->hash)
5910 && ((struct elf_link_hash_entry *) h)->indx == -3)
75cfe082
AM
5911 continue;
5912 }
b585e899 5913 else if (h->type == bfd_link_hash_common)
0ad989f9
L
5914 {
5915 /* We currently have a common symbol. The archive map contains
5916 a reference to this symbol, so we may want to include it. We
5917 only want to include it however, if this archive element
5918 contains a definition of the symbol, not just another common
5919 declaration of it.
5920
5921 Unfortunately some archivers (including GNU ar) will put
5922 declarations of common symbols into their archive maps, as
5923 well as real definitions, so we cannot just go by the archive
5924 map alone. Instead we must read in the element's symbol
5925 table and check that to see what kind of symbol definition
5926 this is. */
5927 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5928 continue;
5929 }
75cfe082 5930 else
0ad989f9 5931 {
b585e899 5932 if (h->type != bfd_link_hash_undefweak)
13e570f8 5933 /* Symbol must be defined. Don't check it again. */
0a1b45a2 5934 included[i] = true;
0ad989f9
L
5935 continue;
5936 }
5937
5938 /* We need to include this archive member. */
5939 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5940 if (element == NULL)
5941 goto error_return;
5942
5943 if (! bfd_check_format (element, bfd_object))
5944 goto error_return;
5945
0ad989f9
L
5946 undefs_tail = info->hash->undefs_tail;
5947
0e144ba7
AM
5948 if (!(*info->callbacks
5949 ->add_archive_element) (info, element, symdef->name, &element))
b95a0a31 5950 continue;
0e144ba7 5951 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5952 goto error_return;
5953
5954 /* If there are any new undefined symbols, we need to make
5955 another pass through the archive in order to see whether
5956 they can be defined. FIXME: This isn't perfect, because
5957 common symbols wind up on undefs_tail and because an
5958 undefined symbol which is defined later on in this pass
5959 does not require another pass. This isn't a bug, but it
5960 does make the code less efficient than it could be. */
5961 if (undefs_tail != info->hash->undefs_tail)
0a1b45a2 5962 loop = true;
0ad989f9
L
5963
5964 /* Look backward to mark all symbols from this object file
5965 which we have already seen in this pass. */
5966 mark = i;
5967 do
5968 {
0a1b45a2 5969 included[mark] = true;
0ad989f9
L
5970 if (mark == 0)
5971 break;
5972 --mark;
5973 }
5974 while (symdefs[mark].file_offset == symdef->file_offset);
5975
5976 /* We mark subsequent symbols from this object file as we go
5977 on through the loop. */
5978 last = symdef->file_offset;
5979 }
5980 }
5981 while (loop);
5982
0ad989f9 5983 free (included);
0a1b45a2 5984 return true;
0ad989f9
L
5985
5986 error_return:
c9594989 5987 free (included);
0a1b45a2 5988 return false;
0ad989f9 5989}
4ad4eba5
AM
5990
5991/* Given an ELF BFD, add symbols to the global hash table as
5992 appropriate. */
5993
0a1b45a2 5994bool
4ad4eba5
AM
5995bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5996{
5997 switch (bfd_get_format (abfd))
5998 {
5999 case bfd_object:
6000 return elf_link_add_object_symbols (abfd, info);
6001 case bfd_archive:
6002 return elf_link_add_archive_symbols (abfd, info);
6003 default:
6004 bfd_set_error (bfd_error_wrong_format);
0a1b45a2 6005 return false;
4ad4eba5
AM
6006 }
6007}
5a580b3a 6008\f
14b1c01e
AM
6009struct hash_codes_info
6010{
6011 unsigned long *hashcodes;
0a1b45a2 6012 bool error;
14b1c01e 6013};
a0c8462f 6014
5a580b3a
AM
6015/* This function will be called though elf_link_hash_traverse to store
6016 all hash value of the exported symbols in an array. */
6017
0a1b45a2 6018static bool
5a580b3a
AM
6019elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
6020{
a50b1753 6021 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 6022 const char *name;
5a580b3a
AM
6023 unsigned long ha;
6024 char *alc = NULL;
6025
5a580b3a
AM
6026 /* Ignore indirect symbols. These are added by the versioning code. */
6027 if (h->dynindx == -1)
0a1b45a2 6028 return true;
5a580b3a
AM
6029
6030 name = h->root.root.string;
422f1182 6031 if (h->versioned >= versioned)
5a580b3a 6032 {
422f1182
L
6033 char *p = strchr (name, ELF_VER_CHR);
6034 if (p != NULL)
14b1c01e 6035 {
422f1182
L
6036 alc = (char *) bfd_malloc (p - name + 1);
6037 if (alc == NULL)
6038 {
0a1b45a2
AM
6039 inf->error = true;
6040 return false;
422f1182
L
6041 }
6042 memcpy (alc, name, p - name);
6043 alc[p - name] = '\0';
6044 name = alc;
14b1c01e 6045 }
5a580b3a
AM
6046 }
6047
6048 /* Compute the hash value. */
6049 ha = bfd_elf_hash (name);
6050
6051 /* Store the found hash value in the array given as the argument. */
14b1c01e 6052 *(inf->hashcodes)++ = ha;
5a580b3a
AM
6053
6054 /* And store it in the struct so that we can put it in the hash table
6055 later. */
f6e332e6 6056 h->u.elf_hash_value = ha;
5a580b3a 6057
c9594989 6058 free (alc);
0a1b45a2 6059 return true;
5a580b3a
AM
6060}
6061
fdc90cb4
JJ
6062struct collect_gnu_hash_codes
6063{
6064 bfd *output_bfd;
6065 const struct elf_backend_data *bed;
6066 unsigned long int nsyms;
6067 unsigned long int maskbits;
6068 unsigned long int *hashcodes;
6069 unsigned long int *hashval;
6070 unsigned long int *indx;
6071 unsigned long int *counts;
6072 bfd_vma *bitmask;
6073 bfd_byte *contents;
f16a9783 6074 bfd_size_type xlat;
fdc90cb4
JJ
6075 long int min_dynindx;
6076 unsigned long int bucketcount;
6077 unsigned long int symindx;
6078 long int local_indx;
6079 long int shift1, shift2;
6080 unsigned long int mask;
0a1b45a2 6081 bool error;
fdc90cb4
JJ
6082};
6083
6084/* This function will be called though elf_link_hash_traverse to store
6085 all hash value of the exported symbols in an array. */
6086
0a1b45a2 6087static bool
fdc90cb4
JJ
6088elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6089{
a50b1753 6090 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 6091 const char *name;
fdc90cb4
JJ
6092 unsigned long ha;
6093 char *alc = NULL;
6094
fdc90cb4
JJ
6095 /* Ignore indirect symbols. These are added by the versioning code. */
6096 if (h->dynindx == -1)
0a1b45a2 6097 return true;
fdc90cb4
JJ
6098
6099 /* Ignore also local symbols and undefined symbols. */
6100 if (! (*s->bed->elf_hash_symbol) (h))
0a1b45a2 6101 return true;
fdc90cb4
JJ
6102
6103 name = h->root.root.string;
422f1182 6104 if (h->versioned >= versioned)
fdc90cb4 6105 {
422f1182
L
6106 char *p = strchr (name, ELF_VER_CHR);
6107 if (p != NULL)
14b1c01e 6108 {
422f1182
L
6109 alc = (char *) bfd_malloc (p - name + 1);
6110 if (alc == NULL)
6111 {
0a1b45a2
AM
6112 s->error = true;
6113 return false;
422f1182
L
6114 }
6115 memcpy (alc, name, p - name);
6116 alc[p - name] = '\0';
6117 name = alc;
14b1c01e 6118 }
fdc90cb4
JJ
6119 }
6120
6121 /* Compute the hash value. */
6122 ha = bfd_elf_gnu_hash (name);
6123
6124 /* Store the found hash value in the array for compute_bucket_count,
6125 and also for .dynsym reordering purposes. */
6126 s->hashcodes[s->nsyms] = ha;
6127 s->hashval[h->dynindx] = ha;
6128 ++s->nsyms;
6129 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6130 s->min_dynindx = h->dynindx;
6131
c9594989 6132 free (alc);
0a1b45a2 6133 return true;
fdc90cb4
JJ
6134}
6135
6136/* This function will be called though elf_link_hash_traverse to do
f16a9783
MS
6137 final dynamic symbol renumbering in case of .gnu.hash.
6138 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6139 to the translation table. */
fdc90cb4 6140
0a1b45a2 6141static bool
f16a9783 6142elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
fdc90cb4 6143{
a50b1753 6144 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
6145 unsigned long int bucket;
6146 unsigned long int val;
6147
fdc90cb4
JJ
6148 /* Ignore indirect symbols. */
6149 if (h->dynindx == -1)
0a1b45a2 6150 return true;
fdc90cb4
JJ
6151
6152 /* Ignore also local symbols and undefined symbols. */
6153 if (! (*s->bed->elf_hash_symbol) (h))
6154 {
6155 if (h->dynindx >= s->min_dynindx)
f16a9783
MS
6156 {
6157 if (s->bed->record_xhash_symbol != NULL)
6158 {
6159 (*s->bed->record_xhash_symbol) (h, 0);
6160 s->local_indx++;
6161 }
6162 else
6163 h->dynindx = s->local_indx++;
6164 }
0a1b45a2 6165 return true;
fdc90cb4
JJ
6166 }
6167
6168 bucket = s->hashval[h->dynindx] % s->bucketcount;
6169 val = (s->hashval[h->dynindx] >> s->shift1)
6170 & ((s->maskbits >> s->shift1) - 1);
6171 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6172 s->bitmask[val]
6173 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6174 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6175 if (s->counts[bucket] == 1)
6176 /* Last element terminates the chain. */
6177 val |= 1;
6178 bfd_put_32 (s->output_bfd, val,
6179 s->contents + (s->indx[bucket] - s->symindx) * 4);
6180 --s->counts[bucket];
f16a9783
MS
6181 if (s->bed->record_xhash_symbol != NULL)
6182 {
6183 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6184
6185 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6186 }
6187 else
6188 h->dynindx = s->indx[bucket]++;
0a1b45a2 6189 return true;
fdc90cb4
JJ
6190}
6191
6192/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6193
0a1b45a2 6194bool
fdc90cb4
JJ
6195_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6196{
6197 return !(h->forced_local
6198 || h->root.type == bfd_link_hash_undefined
6199 || h->root.type == bfd_link_hash_undefweak
6200 || ((h->root.type == bfd_link_hash_defined
6201 || h->root.type == bfd_link_hash_defweak)
6202 && h->root.u.def.section->output_section == NULL));
6203}
6204
5a580b3a
AM
6205/* Array used to determine the number of hash table buckets to use
6206 based on the number of symbols there are. If there are fewer than
6207 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6208 fewer than 37 we use 17 buckets, and so forth. We never use more
6209 than 32771 buckets. */
6210
6211static const size_t elf_buckets[] =
6212{
6213 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6214 16411, 32771, 0
6215};
6216
6217/* Compute bucket count for hashing table. We do not use a static set
6218 of possible tables sizes anymore. Instead we determine for all
6219 possible reasonable sizes of the table the outcome (i.e., the
6220 number of collisions etc) and choose the best solution. The
6221 weighting functions are not too simple to allow the table to grow
6222 without bounds. Instead one of the weighting factors is the size.
6223 Therefore the result is always a good payoff between few collisions
6224 (= short chain lengths) and table size. */
6225static size_t
b20dd2ce 6226compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
6227 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6228 unsigned long int nsyms,
6229 int gnu_hash)
5a580b3a 6230{
5a580b3a 6231 size_t best_size = 0;
5a580b3a 6232 unsigned long int i;
5a580b3a 6233
5a580b3a
AM
6234 /* We have a problem here. The following code to optimize the table
6235 size requires an integer type with more the 32 bits. If
6236 BFD_HOST_U_64_BIT is set we know about such a type. */
6237#ifdef BFD_HOST_U_64_BIT
6238 if (info->optimize)
6239 {
5a580b3a
AM
6240 size_t minsize;
6241 size_t maxsize;
6242 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 6243 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 6244 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 6245 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 6246 unsigned long int *counts;
d40f3da9 6247 bfd_size_type amt;
0883b6e0 6248 unsigned int no_improvement_count = 0;
5a580b3a
AM
6249
6250 /* Possible optimization parameters: if we have NSYMS symbols we say
6251 that the hashing table must at least have NSYMS/4 and at most
6252 2*NSYMS buckets. */
6253 minsize = nsyms / 4;
6254 if (minsize == 0)
6255 minsize = 1;
6256 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
6257 if (gnu_hash)
6258 {
6259 if (minsize < 2)
6260 minsize = 2;
6261 if ((best_size & 31) == 0)
6262 ++best_size;
6263 }
5a580b3a
AM
6264
6265 /* Create array where we count the collisions in. We must use bfd_malloc
6266 since the size could be large. */
6267 amt = maxsize;
6268 amt *= sizeof (unsigned long int);
a50b1753 6269 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 6270 if (counts == NULL)
fdc90cb4 6271 return 0;
5a580b3a
AM
6272
6273 /* Compute the "optimal" size for the hash table. The criteria is a
6274 minimal chain length. The minor criteria is (of course) the size
6275 of the table. */
6276 for (i = minsize; i < maxsize; ++i)
6277 {
6278 /* Walk through the array of hashcodes and count the collisions. */
6279 BFD_HOST_U_64_BIT max;
6280 unsigned long int j;
6281 unsigned long int fact;
6282
fdc90cb4
JJ
6283 if (gnu_hash && (i & 31) == 0)
6284 continue;
6285
5a580b3a
AM
6286 memset (counts, '\0', i * sizeof (unsigned long int));
6287
6288 /* Determine how often each hash bucket is used. */
6289 for (j = 0; j < nsyms; ++j)
6290 ++counts[hashcodes[j] % i];
6291
6292 /* For the weight function we need some information about the
6293 pagesize on the target. This is information need not be 100%
6294 accurate. Since this information is not available (so far) we
6295 define it here to a reasonable default value. If it is crucial
6296 to have a better value some day simply define this value. */
6297# ifndef BFD_TARGET_PAGESIZE
6298# define BFD_TARGET_PAGESIZE (4096)
6299# endif
6300
fdc90cb4
JJ
6301 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6302 and the chains. */
6303 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
6304
6305# if 1
6306 /* Variant 1: optimize for short chains. We add the squares
6307 of all the chain lengths (which favors many small chain
6308 over a few long chains). */
6309 for (j = 0; j < i; ++j)
6310 max += counts[j] * counts[j];
6311
6312 /* This adds penalties for the overall size of the table. */
fdc90cb4 6313 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6314 max *= fact * fact;
6315# else
6316 /* Variant 2: Optimize a lot more for small table. Here we
6317 also add squares of the size but we also add penalties for
6318 empty slots (the +1 term). */
6319 for (j = 0; j < i; ++j)
6320 max += (1 + counts[j]) * (1 + counts[j]);
6321
6322 /* The overall size of the table is considered, but not as
6323 strong as in variant 1, where it is squared. */
fdc90cb4 6324 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6325 max *= fact;
6326# endif
6327
6328 /* Compare with current best results. */
6329 if (max < best_chlen)
6330 {
6331 best_chlen = max;
6332 best_size = i;
ca4be51c 6333 no_improvement_count = 0;
5a580b3a 6334 }
0883b6e0
NC
6335 /* PR 11843: Avoid futile long searches for the best bucket size
6336 when there are a large number of symbols. */
6337 else if (++no_improvement_count == 100)
6338 break;
5a580b3a
AM
6339 }
6340
6341 free (counts);
6342 }
6343 else
6344#endif /* defined (BFD_HOST_U_64_BIT) */
6345 {
6346 /* This is the fallback solution if no 64bit type is available or if we
6347 are not supposed to spend much time on optimizations. We select the
6348 bucket count using a fixed set of numbers. */
6349 for (i = 0; elf_buckets[i] != 0; i++)
6350 {
6351 best_size = elf_buckets[i];
fdc90cb4 6352 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
6353 break;
6354 }
fdc90cb4
JJ
6355 if (gnu_hash && best_size < 2)
6356 best_size = 2;
5a580b3a
AM
6357 }
6358
5a580b3a
AM
6359 return best_size;
6360}
6361
d0bf826b
AM
6362/* Size any SHT_GROUP section for ld -r. */
6363
0a1b45a2 6364bool
d0bf826b
AM
6365_bfd_elf_size_group_sections (struct bfd_link_info *info)
6366{
6367 bfd *ibfd;
57963c05 6368 asection *s;
d0bf826b 6369
c72f2fb2 6370 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b 6371 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
57963c05
AM
6372 && (s = ibfd->sections) != NULL
6373 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
d0bf826b 6374 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
0a1b45a2
AM
6375 return false;
6376 return true;
d0bf826b
AM
6377}
6378
04c3a755
NS
6379/* Set a default stack segment size. The value in INFO wins. If it
6380 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6381 undefined it is initialized. */
6382
0a1b45a2 6383bool
04c3a755
NS
6384bfd_elf_stack_segment_size (bfd *output_bfd,
6385 struct bfd_link_info *info,
6386 const char *legacy_symbol,
6387 bfd_vma default_size)
6388{
6389 struct elf_link_hash_entry *h = NULL;
6390
6391 /* Look for legacy symbol. */
6392 if (legacy_symbol)
6393 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
0a1b45a2 6394 false, false, false);
04c3a755
NS
6395 if (h && (h->root.type == bfd_link_hash_defined
6396 || h->root.type == bfd_link_hash_defweak)
6397 && h->def_regular
6398 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6399 {
6400 /* The symbol has no type if specified on the command line. */
6401 h->type = STT_OBJECT;
6402 if (info->stacksize)
695344c0 6403 /* xgettext:c-format */
871b3ab2 6404 _bfd_error_handler (_("%pB: stack size specified and %s set"),
4eca0228 6405 output_bfd, legacy_symbol);
04c3a755 6406 else if (h->root.u.def.section != bfd_abs_section_ptr)
695344c0 6407 /* xgettext:c-format */
871b3ab2 6408 _bfd_error_handler (_("%pB: %s not absolute"),
4eca0228 6409 output_bfd, legacy_symbol);
04c3a755
NS
6410 else
6411 info->stacksize = h->root.u.def.value;
6412 }
6413
6414 if (!info->stacksize)
6415 /* If the user didn't set a size, or explicitly inhibit the
6416 size, set it now. */
6417 info->stacksize = default_size;
6418
6419 /* Provide the legacy symbol, if it is referenced. */
6420 if (h && (h->root.type == bfd_link_hash_undefined
6421 || h->root.type == bfd_link_hash_undefweak))
6422 {
6423 struct bfd_link_hash_entry *bh = NULL;
6424
6425 if (!(_bfd_generic_link_add_one_symbol
6426 (info, output_bfd, legacy_symbol,
6427 BSF_GLOBAL, bfd_abs_section_ptr,
6428 info->stacksize >= 0 ? info->stacksize : 0,
0a1b45a2
AM
6429 NULL, false, get_elf_backend_data (output_bfd)->collect, &bh)))
6430 return false;
04c3a755
NS
6431
6432 h = (struct elf_link_hash_entry *) bh;
6433 h->def_regular = 1;
6434 h->type = STT_OBJECT;
6435 }
6436
0a1b45a2 6437 return true;
04c3a755
NS
6438}
6439
b531344c
MR
6440/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6441
6442struct elf_gc_sweep_symbol_info
6443{
6444 struct bfd_link_info *info;
6445 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
0a1b45a2 6446 bool);
b531344c
MR
6447};
6448
0a1b45a2 6449static bool
b531344c
MR
6450elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6451{
6452 if (!h->mark
6453 && (((h->root.type == bfd_link_hash_defined
6454 || h->root.type == bfd_link_hash_defweak)
6455 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6456 && h->root.u.def.section->gc_mark))
6457 || h->root.type == bfd_link_hash_undefined
6458 || h->root.type == bfd_link_hash_undefweak))
6459 {
6460 struct elf_gc_sweep_symbol_info *inf;
6461
6462 inf = (struct elf_gc_sweep_symbol_info *) data;
0a1b45a2 6463 (*inf->hide_symbol) (inf->info, h, true);
b531344c
MR
6464 h->def_regular = 0;
6465 h->ref_regular = 0;
6466 h->ref_regular_nonweak = 0;
6467 }
6468
0a1b45a2 6469 return true;
b531344c
MR
6470}
6471
5a580b3a
AM
6472/* Set up the sizes and contents of the ELF dynamic sections. This is
6473 called by the ELF linker emulation before_allocation routine. We
6474 must set the sizes of the sections before the linker sets the
6475 addresses of the various sections. */
6476
0a1b45a2 6477bool
5a580b3a
AM
6478bfd_elf_size_dynamic_sections (bfd *output_bfd,
6479 const char *soname,
6480 const char *rpath,
6481 const char *filter_shlib,
7ee314fa
AM
6482 const char *audit,
6483 const char *depaudit,
5a580b3a
AM
6484 const char * const *auxiliary_filters,
6485 struct bfd_link_info *info,
fd91d419 6486 asection **sinterpptr)
5a580b3a 6487{
5a580b3a
AM
6488 bfd *dynobj;
6489 const struct elf_backend_data *bed;
5a580b3a
AM
6490
6491 *sinterpptr = NULL;
6492
5a580b3a 6493 if (!is_elf_hash_table (info->hash))
0a1b45a2 6494 return true;
5a580b3a 6495
5a580b3a
AM
6496 dynobj = elf_hash_table (info)->dynobj;
6497
9a2a56cc 6498 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 6499 {
902e9fc7
MR
6500 struct bfd_elf_version_tree *verdefs;
6501 struct elf_info_failed asvinfo;
5a580b3a
AM
6502 struct bfd_elf_version_tree *t;
6503 struct bfd_elf_version_expr *d;
902e9fc7 6504 asection *s;
e6699019 6505 size_t soname_indx;
7ee314fa 6506
5a580b3a
AM
6507 /* If we are supposed to export all symbols into the dynamic symbol
6508 table (this is not the normal case), then do so. */
55255dae 6509 if (info->export_dynamic
0e1862bb 6510 || (bfd_link_executable (info) && info->dynamic))
5a580b3a 6511 {
3d13f3e9
AM
6512 struct elf_info_failed eif;
6513
6514 eif.info = info;
0a1b45a2 6515 eif.failed = false;
5a580b3a
AM
6516 elf_link_hash_traverse (elf_hash_table (info),
6517 _bfd_elf_export_symbol,
6518 &eif);
6519 if (eif.failed)
0a1b45a2 6520 return false;
5a580b3a
AM
6521 }
6522
e6699019
L
6523 if (soname != NULL)
6524 {
6525 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
0a1b45a2 6526 soname, true);
e6699019
L
6527 if (soname_indx == (size_t) -1
6528 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
0a1b45a2 6529 return false;
e6699019
L
6530 }
6531 else
6532 soname_indx = (size_t) -1;
6533
5a580b3a 6534 /* Make all global versions with definition. */
fd91d419 6535 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6536 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6537 if (!d->symver && d->literal)
5a580b3a
AM
6538 {
6539 const char *verstr, *name;
6540 size_t namelen, verlen, newlen;
93252b1c 6541 char *newname, *p, leading_char;
5a580b3a
AM
6542 struct elf_link_hash_entry *newh;
6543
93252b1c 6544 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 6545 name = d->pattern;
93252b1c 6546 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
6547 verstr = t->name;
6548 verlen = strlen (verstr);
6549 newlen = namelen + verlen + 3;
6550
a50b1753 6551 newname = (char *) bfd_malloc (newlen);
5a580b3a 6552 if (newname == NULL)
0a1b45a2 6553 return false;
93252b1c
MF
6554 newname[0] = leading_char;
6555 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
6556
6557 /* Check the hidden versioned definition. */
6558 p = newname + namelen;
6559 *p++ = ELF_VER_CHR;
6560 memcpy (p, verstr, verlen + 1);
6561 newh = elf_link_hash_lookup (elf_hash_table (info),
0a1b45a2
AM
6562 newname, false, false,
6563 false);
5a580b3a
AM
6564 if (newh == NULL
6565 || (newh->root.type != bfd_link_hash_defined
6566 && newh->root.type != bfd_link_hash_defweak))
6567 {
6568 /* Check the default versioned definition. */
6569 *p++ = ELF_VER_CHR;
6570 memcpy (p, verstr, verlen + 1);
6571 newh = elf_link_hash_lookup (elf_hash_table (info),
0a1b45a2
AM
6572 newname, false, false,
6573 false);
5a580b3a
AM
6574 }
6575 free (newname);
6576
6577 /* Mark this version if there is a definition and it is
6578 not defined in a shared object. */
6579 if (newh != NULL
f5385ebf 6580 && !newh->def_dynamic
5a580b3a
AM
6581 && (newh->root.type == bfd_link_hash_defined
6582 || newh->root.type == bfd_link_hash_defweak))
6583 d->symver = 1;
6584 }
6585
6586 /* Attach all the symbols to their version information. */
5a580b3a 6587 asvinfo.info = info;
0a1b45a2 6588 asvinfo.failed = false;
5a580b3a
AM
6589
6590 elf_link_hash_traverse (elf_hash_table (info),
6591 _bfd_elf_link_assign_sym_version,
6592 &asvinfo);
6593 if (asvinfo.failed)
0a1b45a2 6594 return false;
5a580b3a
AM
6595
6596 if (!info->allow_undefined_version)
6597 {
6598 /* Check if all global versions have a definition. */
0a1b45a2 6599 bool all_defined = true;
fd91d419 6600 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6601 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6602 if (d->literal && !d->symver && !d->script)
5a580b3a 6603 {
4eca0228 6604 _bfd_error_handler
5a580b3a
AM
6605 (_("%s: undefined version: %s"),
6606 d->pattern, t->name);
0a1b45a2 6607 all_defined = false;
5a580b3a
AM
6608 }
6609
6610 if (!all_defined)
6611 {
6612 bfd_set_error (bfd_error_bad_value);
0a1b45a2 6613 return false;
5a580b3a
AM
6614 }
6615 }
6616
902e9fc7
MR
6617 /* Set up the version definition section. */
6618 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6619 BFD_ASSERT (s != NULL);
5a580b3a 6620
902e9fc7
MR
6621 /* We may have created additional version definitions if we are
6622 just linking a regular application. */
6623 verdefs = info->version_info;
5a580b3a 6624
902e9fc7
MR
6625 /* Skip anonymous version tag. */
6626 if (verdefs != NULL && verdefs->vernum == 0)
6627 verdefs = verdefs->next;
5a580b3a 6628
902e9fc7
MR
6629 if (verdefs == NULL && !info->create_default_symver)
6630 s->flags |= SEC_EXCLUDE;
6631 else
5a580b3a 6632 {
902e9fc7
MR
6633 unsigned int cdefs;
6634 bfd_size_type size;
6635 bfd_byte *p;
6636 Elf_Internal_Verdef def;
6637 Elf_Internal_Verdaux defaux;
6638 struct bfd_link_hash_entry *bh;
6639 struct elf_link_hash_entry *h;
6640 const char *name;
5a580b3a 6641
902e9fc7
MR
6642 cdefs = 0;
6643 size = 0;
5a580b3a 6644
902e9fc7
MR
6645 /* Make space for the base version. */
6646 size += sizeof (Elf_External_Verdef);
6647 size += sizeof (Elf_External_Verdaux);
6648 ++cdefs;
6649
6650 /* Make space for the default version. */
6651 if (info->create_default_symver)
6652 {
6653 size += sizeof (Elf_External_Verdef);
6654 ++cdefs;
3e3b46e5
PB
6655 }
6656
5a580b3a
AM
6657 for (t = verdefs; t != NULL; t = t->next)
6658 {
6659 struct bfd_elf_version_deps *n;
6660
a6cc6b3b
RO
6661 /* Don't emit base version twice. */
6662 if (t->vernum == 0)
6663 continue;
6664
5a580b3a
AM
6665 size += sizeof (Elf_External_Verdef);
6666 size += sizeof (Elf_External_Verdaux);
6667 ++cdefs;
6668
6669 for (n = t->deps; n != NULL; n = n->next)
6670 size += sizeof (Elf_External_Verdaux);
6671 }
6672
eea6121a 6673 s->size = size;
a50b1753 6674 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6675 if (s->contents == NULL && s->size != 0)
0a1b45a2 6676 return false;
5a580b3a
AM
6677
6678 /* Fill in the version definition section. */
6679
6680 p = s->contents;
6681
6682 def.vd_version = VER_DEF_CURRENT;
6683 def.vd_flags = VER_FLG_BASE;
6684 def.vd_ndx = 1;
6685 def.vd_cnt = 1;
3e3b46e5
PB
6686 if (info->create_default_symver)
6687 {
6688 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6689 def.vd_next = sizeof (Elf_External_Verdef);
6690 }
6691 else
6692 {
6693 def.vd_aux = sizeof (Elf_External_Verdef);
6694 def.vd_next = (sizeof (Elf_External_Verdef)
6695 + sizeof (Elf_External_Verdaux));
6696 }
5a580b3a 6697
ef53be89 6698 if (soname_indx != (size_t) -1)
5a580b3a
AM
6699 {
6700 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6701 soname_indx);
6702 def.vd_hash = bfd_elf_hash (soname);
6703 defaux.vda_name = soname_indx;
3e3b46e5 6704 name = soname;
5a580b3a
AM
6705 }
6706 else
6707 {
ef53be89 6708 size_t indx;
5a580b3a 6709
765cf5f6 6710 name = lbasename (bfd_get_filename (output_bfd));
5a580b3a
AM
6711 def.vd_hash = bfd_elf_hash (name);
6712 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
0a1b45a2 6713 name, false);
ef53be89 6714 if (indx == (size_t) -1)
0a1b45a2 6715 return false;
5a580b3a
AM
6716 defaux.vda_name = indx;
6717 }
6718 defaux.vda_next = 0;
6719
6720 _bfd_elf_swap_verdef_out (output_bfd, &def,
6721 (Elf_External_Verdef *) p);
6722 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6723 if (info->create_default_symver)
6724 {
6725 /* Add a symbol representing this version. */
6726 bh = NULL;
6727 if (! (_bfd_generic_link_add_one_symbol
6728 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
0a1b45a2 6729 0, NULL, false,
3e3b46e5 6730 get_elf_backend_data (dynobj)->collect, &bh)))
0a1b45a2 6731 return false;
3e3b46e5
PB
6732 h = (struct elf_link_hash_entry *) bh;
6733 h->non_elf = 0;
6734 h->def_regular = 1;
6735 h->type = STT_OBJECT;
6736 h->verinfo.vertree = NULL;
6737
6738 if (! bfd_elf_link_record_dynamic_symbol (info, h))
0a1b45a2 6739 return false;
3e3b46e5
PB
6740
6741 /* Create a duplicate of the base version with the same
6742 aux block, but different flags. */
6743 def.vd_flags = 0;
6744 def.vd_ndx = 2;
6745 def.vd_aux = sizeof (Elf_External_Verdef);
6746 if (verdefs)
6747 def.vd_next = (sizeof (Elf_External_Verdef)
6748 + sizeof (Elf_External_Verdaux));
6749 else
6750 def.vd_next = 0;
6751 _bfd_elf_swap_verdef_out (output_bfd, &def,
6752 (Elf_External_Verdef *) p);
6753 p += sizeof (Elf_External_Verdef);
6754 }
5a580b3a
AM
6755 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6756 (Elf_External_Verdaux *) p);
6757 p += sizeof (Elf_External_Verdaux);
6758
6759 for (t = verdefs; t != NULL; t = t->next)
6760 {
6761 unsigned int cdeps;
6762 struct bfd_elf_version_deps *n;
5a580b3a 6763
a6cc6b3b
RO
6764 /* Don't emit the base version twice. */
6765 if (t->vernum == 0)
6766 continue;
6767
5a580b3a
AM
6768 cdeps = 0;
6769 for (n = t->deps; n != NULL; n = n->next)
6770 ++cdeps;
6771
6772 /* Add a symbol representing this version. */
6773 bh = NULL;
6774 if (! (_bfd_generic_link_add_one_symbol
6775 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
0a1b45a2 6776 0, NULL, false,
5a580b3a 6777 get_elf_backend_data (dynobj)->collect, &bh)))
0a1b45a2 6778 return false;
5a580b3a 6779 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6780 h->non_elf = 0;
6781 h->def_regular = 1;
5a580b3a
AM
6782 h->type = STT_OBJECT;
6783 h->verinfo.vertree = t;
6784
c152c796 6785 if (! bfd_elf_link_record_dynamic_symbol (info, h))
0a1b45a2 6786 return false;
5a580b3a
AM
6787
6788 def.vd_version = VER_DEF_CURRENT;
6789 def.vd_flags = 0;
6790 if (t->globals.list == NULL
6791 && t->locals.list == NULL
6792 && ! t->used)
6793 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6794 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6795 def.vd_cnt = cdeps + 1;
6796 def.vd_hash = bfd_elf_hash (t->name);
6797 def.vd_aux = sizeof (Elf_External_Verdef);
6798 def.vd_next = 0;
a6cc6b3b
RO
6799
6800 /* If a basever node is next, it *must* be the last node in
6801 the chain, otherwise Verdef construction breaks. */
6802 if (t->next != NULL && t->next->vernum == 0)
6803 BFD_ASSERT (t->next->next == NULL);
6804
6805 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6806 def.vd_next = (sizeof (Elf_External_Verdef)
6807 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6808
6809 _bfd_elf_swap_verdef_out (output_bfd, &def,
6810 (Elf_External_Verdef *) p);
6811 p += sizeof (Elf_External_Verdef);
6812
6813 defaux.vda_name = h->dynstr_index;
6814 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6815 h->dynstr_index);
6816 defaux.vda_next = 0;
6817 if (t->deps != NULL)
6818 defaux.vda_next = sizeof (Elf_External_Verdaux);
6819 t->name_indx = defaux.vda_name;
6820
6821 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6822 (Elf_External_Verdaux *) p);
6823 p += sizeof (Elf_External_Verdaux);
6824
6825 for (n = t->deps; n != NULL; n = n->next)
6826 {
6827 if (n->version_needed == NULL)
6828 {
6829 /* This can happen if there was an error in the
6830 version script. */
6831 defaux.vda_name = 0;
6832 }
6833 else
6834 {
6835 defaux.vda_name = n->version_needed->name_indx;
6836 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6837 defaux.vda_name);
6838 }
6839 if (n->next == NULL)
6840 defaux.vda_next = 0;
6841 else
6842 defaux.vda_next = sizeof (Elf_External_Verdaux);
6843
6844 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6845 (Elf_External_Verdaux *) p);
6846 p += sizeof (Elf_External_Verdaux);
6847 }
6848 }
6849
5a580b3a
AM
6850 elf_tdata (output_bfd)->cverdefs = cdefs;
6851 }
902e9fc7
MR
6852 }
6853
6854 bed = get_elf_backend_data (output_bfd);
6855
6856 if (info->gc_sections && bed->can_gc_sections)
6857 {
6858 struct elf_gc_sweep_symbol_info sweep_info;
902e9fc7
MR
6859
6860 /* Remove the symbols that were in the swept sections from the
3d13f3e9 6861 dynamic symbol table. */
902e9fc7
MR
6862 sweep_info.info = info;
6863 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6864 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6865 &sweep_info);
3d13f3e9
AM
6866 }
6867
6868 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6869 {
6870 asection *s;
6871 struct elf_find_verdep_info sinfo;
6872
6873 /* Work out the size of the version reference section. */
6874
6875 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6876 BFD_ASSERT (s != NULL);
902e9fc7 6877
3d13f3e9
AM
6878 sinfo.info = info;
6879 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6880 if (sinfo.vers == 0)
6881 sinfo.vers = 1;
0a1b45a2 6882 sinfo.failed = false;
3d13f3e9
AM
6883
6884 elf_link_hash_traverse (elf_hash_table (info),
6885 _bfd_elf_link_find_version_dependencies,
6886 &sinfo);
6887 if (sinfo.failed)
0a1b45a2 6888 return false;
3d13f3e9
AM
6889
6890 if (elf_tdata (output_bfd)->verref == NULL)
6891 s->flags |= SEC_EXCLUDE;
6892 else
6893 {
6894 Elf_Internal_Verneed *vn;
6895 unsigned int size;
6896 unsigned int crefs;
6897 bfd_byte *p;
6898
6899 /* Build the version dependency section. */
6900 size = 0;
6901 crefs = 0;
6902 for (vn = elf_tdata (output_bfd)->verref;
6903 vn != NULL;
6904 vn = vn->vn_nextref)
6905 {
6906 Elf_Internal_Vernaux *a;
6907
6908 size += sizeof (Elf_External_Verneed);
6909 ++crefs;
6910 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6911 size += sizeof (Elf_External_Vernaux);
6912 }
6913
6914 s->size = size;
6915 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6916 if (s->contents == NULL)
0a1b45a2 6917 return false;
3d13f3e9
AM
6918
6919 p = s->contents;
6920 for (vn = elf_tdata (output_bfd)->verref;
6921 vn != NULL;
6922 vn = vn->vn_nextref)
6923 {
6924 unsigned int caux;
6925 Elf_Internal_Vernaux *a;
6926 size_t indx;
6927
6928 caux = 0;
6929 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6930 ++caux;
6931
6932 vn->vn_version = VER_NEED_CURRENT;
6933 vn->vn_cnt = caux;
6934 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6935 elf_dt_name (vn->vn_bfd) != NULL
6936 ? elf_dt_name (vn->vn_bfd)
765cf5f6
AM
6937 : lbasename (bfd_get_filename
6938 (vn->vn_bfd)),
0a1b45a2 6939 false);
3d13f3e9 6940 if (indx == (size_t) -1)
0a1b45a2 6941 return false;
3d13f3e9
AM
6942 vn->vn_file = indx;
6943 vn->vn_aux = sizeof (Elf_External_Verneed);
6944 if (vn->vn_nextref == NULL)
6945 vn->vn_next = 0;
6946 else
6947 vn->vn_next = (sizeof (Elf_External_Verneed)
6948 + caux * sizeof (Elf_External_Vernaux));
6949
6950 _bfd_elf_swap_verneed_out (output_bfd, vn,
6951 (Elf_External_Verneed *) p);
6952 p += sizeof (Elf_External_Verneed);
6953
6954 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6955 {
6956 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6957 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
0a1b45a2 6958 a->vna_nodename, false);
3d13f3e9 6959 if (indx == (size_t) -1)
0a1b45a2 6960 return false;
3d13f3e9
AM
6961 a->vna_name = indx;
6962 if (a->vna_nextptr == NULL)
6963 a->vna_next = 0;
6964 else
6965 a->vna_next = sizeof (Elf_External_Vernaux);
6966
6967 _bfd_elf_swap_vernaux_out (output_bfd, a,
6968 (Elf_External_Vernaux *) p);
6969 p += sizeof (Elf_External_Vernaux);
6970 }
6971 }
6972
6973 elf_tdata (output_bfd)->cverrefs = crefs;
6974 }
902e9fc7
MR
6975 }
6976
6977 /* Any syms created from now on start with -1 in
6978 got.refcount/offset and plt.refcount/offset. */
6979 elf_hash_table (info)->init_got_refcount
6980 = elf_hash_table (info)->init_got_offset;
6981 elf_hash_table (info)->init_plt_refcount
6982 = elf_hash_table (info)->init_plt_offset;
6983
6984 if (bfd_link_relocatable (info)
6985 && !_bfd_elf_size_group_sections (info))
0a1b45a2 6986 return false;
902e9fc7
MR
6987
6988 /* The backend may have to create some sections regardless of whether
6989 we're dynamic or not. */
6990 if (bed->elf_backend_always_size_sections
6991 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
0a1b45a2 6992 return false;
902e9fc7
MR
6993
6994 /* Determine any GNU_STACK segment requirements, after the backend
6995 has had a chance to set a default segment size. */
6996 if (info->execstack)
6997 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6998 else if (info->noexecstack)
6999 elf_stack_flags (output_bfd) = PF_R | PF_W;
7000 else
7001 {
7002 bfd *inputobj;
7003 asection *notesec = NULL;
7004 int exec = 0;
7005
7006 for (inputobj = info->input_bfds;
7007 inputobj;
7008 inputobj = inputobj->link.next)
7009 {
7010 asection *s;
7011
7012 if (inputobj->flags
7013 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
7014 continue;
57963c05
AM
7015 s = inputobj->sections;
7016 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7017 continue;
7018
902e9fc7
MR
7019 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
7020 if (s)
7021 {
7022 if (s->flags & SEC_CODE)
7023 exec = PF_X;
7024 notesec = s;
7025 }
7026 else if (bed->default_execstack)
7027 exec = PF_X;
7028 }
7029 if (notesec || info->stacksize > 0)
7030 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
7031 if (notesec && exec && bfd_link_relocatable (info)
7032 && notesec->output_section != bfd_abs_section_ptr)
7033 notesec->output_section->flags |= SEC_CODE;
7034 }
7035
7036 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7037 {
7038 struct elf_info_failed eif;
7039 struct elf_link_hash_entry *h;
7040 asection *dynstr;
7041 asection *s;
7042
7043 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
7044 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
7045
902e9fc7
MR
7046 if (info->symbolic)
7047 {
7048 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
0a1b45a2 7049 return false;
902e9fc7
MR
7050 info->flags |= DF_SYMBOLIC;
7051 }
7052
7053 if (rpath != NULL)
7054 {
7055 size_t indx;
7056 bfd_vma tag;
7057
7058 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
0a1b45a2 7059 true);
902e9fc7 7060 if (indx == (size_t) -1)
0a1b45a2 7061 return false;
902e9fc7
MR
7062
7063 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
7064 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
0a1b45a2 7065 return false;
902e9fc7
MR
7066 }
7067
7068 if (filter_shlib != NULL)
7069 {
7070 size_t indx;
7071
7072 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
0a1b45a2 7073 filter_shlib, true);
902e9fc7
MR
7074 if (indx == (size_t) -1
7075 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
0a1b45a2 7076 return false;
902e9fc7
MR
7077 }
7078
7079 if (auxiliary_filters != NULL)
7080 {
7081 const char * const *p;
7082
7083 for (p = auxiliary_filters; *p != NULL; p++)
7084 {
7085 size_t indx;
7086
7087 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
0a1b45a2 7088 *p, true);
902e9fc7
MR
7089 if (indx == (size_t) -1
7090 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
0a1b45a2 7091 return false;
902e9fc7
MR
7092 }
7093 }
7094
7095 if (audit != NULL)
7096 {
7097 size_t indx;
7098
7099 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
0a1b45a2 7100 true);
902e9fc7
MR
7101 if (indx == (size_t) -1
7102 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
0a1b45a2 7103 return false;
902e9fc7
MR
7104 }
7105
7106 if (depaudit != NULL)
7107 {
7108 size_t indx;
7109
7110 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
0a1b45a2 7111 true);
902e9fc7
MR
7112 if (indx == (size_t) -1
7113 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
0a1b45a2 7114 return false;
902e9fc7
MR
7115 }
7116
7117 eif.info = info;
0a1b45a2 7118 eif.failed = false;
902e9fc7
MR
7119
7120 /* Find all symbols which were defined in a dynamic object and make
7121 the backend pick a reasonable value for them. */
7122 elf_link_hash_traverse (elf_hash_table (info),
7123 _bfd_elf_adjust_dynamic_symbol,
7124 &eif);
7125 if (eif.failed)
0a1b45a2 7126 return false;
902e9fc7
MR
7127
7128 /* Add some entries to the .dynamic section. We fill in some of the
7129 values later, in bfd_elf_final_link, but we must add the entries
7130 now so that we know the final size of the .dynamic section. */
7131
7132 /* If there are initialization and/or finalization functions to
7133 call then add the corresponding DT_INIT/DT_FINI entries. */
7134 h = (info->init_function
7135 ? elf_link_hash_lookup (elf_hash_table (info),
0a1b45a2
AM
7136 info->init_function, false,
7137 false, false)
902e9fc7
MR
7138 : NULL);
7139 if (h != NULL
7140 && (h->ref_regular
7141 || h->def_regular))
7142 {
7143 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
0a1b45a2 7144 return false;
902e9fc7
MR
7145 }
7146 h = (info->fini_function
7147 ? elf_link_hash_lookup (elf_hash_table (info),
0a1b45a2
AM
7148 info->fini_function, false,
7149 false, false)
902e9fc7
MR
7150 : NULL);
7151 if (h != NULL
7152 && (h->ref_regular
7153 || h->def_regular))
7154 {
7155 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
0a1b45a2 7156 return false;
902e9fc7
MR
7157 }
7158
7159 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7160 if (s != NULL && s->linker_has_input)
7161 {
7162 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7163 if (! bfd_link_executable (info))
7164 {
7165 bfd *sub;
7166 asection *o;
7167
57963c05
AM
7168 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7169 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7170 && (o = sub->sections) != NULL
7171 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
902e9fc7
MR
7172 for (o = sub->sections; o != NULL; o = o->next)
7173 if (elf_section_data (o)->this_hdr.sh_type
7174 == SHT_PREINIT_ARRAY)
7175 {
7176 _bfd_error_handler
871b3ab2 7177 (_("%pB: .preinit_array section is not allowed in DSO"),
902e9fc7
MR
7178 sub);
7179 break;
7180 }
7181
7182 bfd_set_error (bfd_error_nonrepresentable_section);
0a1b45a2 7183 return false;
902e9fc7
MR
7184 }
7185
7186 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7187 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
0a1b45a2 7188 return false;
902e9fc7
MR
7189 }
7190 s = bfd_get_section_by_name (output_bfd, ".init_array");
7191 if (s != NULL && s->linker_has_input)
7192 {
7193 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7194 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
0a1b45a2 7195 return false;
902e9fc7
MR
7196 }
7197 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7198 if (s != NULL && s->linker_has_input)
7199 {
7200 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7201 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
0a1b45a2 7202 return false;
902e9fc7
MR
7203 }
7204
7205 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7206 /* If .dynstr is excluded from the link, we don't want any of
7207 these tags. Strictly, we should be checking each section
7208 individually; This quick check covers for the case where
7209 someone does a /DISCARD/ : { *(*) }. */
7210 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7211 {
7212 bfd_size_type strsize;
7213
7214 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7215 if ((info->emit_hash
7216 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7217 || (info->emit_gnu_hash
f16a9783
MS
7218 && (bed->record_xhash_symbol == NULL
7219 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
902e9fc7
MR
7220 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7221 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7222 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7223 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6a0a0dd0
VDM
7224 bed->s->sizeof_sym)
7225 || (info->gnu_flags_1
7226 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
7227 info->gnu_flags_1)))
0a1b45a2 7228 return false;
902e9fc7
MR
7229 }
7230 }
7231
7232 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
0a1b45a2 7233 return false;
902e9fc7
MR
7234
7235 /* The backend must work out the sizes of all the other dynamic
7236 sections. */
7237 if (dynobj != NULL
7238 && bed->elf_backend_size_dynamic_sections != NULL
7239 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
0a1b45a2 7240 return false;
902e9fc7
MR
7241
7242 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7243 {
902e9fc7
MR
7244 if (elf_tdata (output_bfd)->cverdefs)
7245 {
7246 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7247
7248 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7249 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
0a1b45a2 7250 return false;
902e9fc7
MR
7251 }
7252
7253 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7254 {
7255 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
0a1b45a2 7256 return false;
902e9fc7
MR
7257 }
7258 else if (info->flags & DF_BIND_NOW)
7259 {
7260 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
0a1b45a2 7261 return false;
902e9fc7
MR
7262 }
7263
7264 if (info->flags_1)
7265 {
7266 if (bfd_link_executable (info))
7267 info->flags_1 &= ~ (DF_1_INITFIRST
7268 | DF_1_NODELETE
7269 | DF_1_NOOPEN);
7270 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
0a1b45a2 7271 return false;
902e9fc7
MR
7272 }
7273
7274 if (elf_tdata (output_bfd)->cverrefs)
7275 {
7276 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7277
7278 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7279 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
0a1b45a2 7280 return false;
902e9fc7 7281 }
5a580b3a 7282
8423293d
AM
7283 if ((elf_tdata (output_bfd)->cverrefs == 0
7284 && elf_tdata (output_bfd)->cverdefs == 0)
63f452a8 7285 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
8423293d 7286 {
902e9fc7
MR
7287 asection *s;
7288
3d4d4302 7289 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
7290 s->flags |= SEC_EXCLUDE;
7291 }
7292 }
0a1b45a2 7293 return true;
8423293d
AM
7294}
7295
74541ad4
AM
7296/* Find the first non-excluded output section. We'll use its
7297 section symbol for some emitted relocs. */
7298void
7299_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7300{
7301 asection *s;
f26a3287 7302 asection *found = NULL;
74541ad4
AM
7303
7304 for (s = output_bfd->sections; s != NULL; s = s->next)
7305 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
d00dd7dc 7306 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7307 {
f26a3287
AM
7308 found = s;
7309 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7310 break;
74541ad4 7311 }
f26a3287 7312 elf_hash_table (info)->text_index_section = found;
74541ad4
AM
7313}
7314
7315/* Find two non-excluded output sections, one for code, one for data.
7316 We'll use their section symbols for some emitted relocs. */
7317void
7318_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7319{
7320 asection *s;
f26a3287 7321 asection *found = NULL;
74541ad4 7322
266b05cf 7323 /* Data first, since setting text_index_section changes
7f923b7f 7324 _bfd_elf_omit_section_dynsym_default. */
74541ad4 7325 for (s = output_bfd->sections; s != NULL; s = s->next)
f26a3287
AM
7326 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7327 && !(s->flags & SEC_READONLY)
d00dd7dc 7328 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7329 {
f26a3287
AM
7330 found = s;
7331 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7332 break;
74541ad4 7333 }
f26a3287 7334 elf_hash_table (info)->data_index_section = found;
74541ad4
AM
7335
7336 for (s = output_bfd->sections; s != NULL; s = s->next)
f26a3287
AM
7337 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7338 && (s->flags & SEC_READONLY)
d00dd7dc 7339 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7340 {
f26a3287 7341 found = s;
74541ad4
AM
7342 break;
7343 }
f26a3287 7344 elf_hash_table (info)->text_index_section = found;
74541ad4
AM
7345}
7346
f16a9783
MS
7347#define GNU_HASH_SECTION_NAME(bed) \
7348 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7349
0a1b45a2 7350bool
8423293d
AM
7351bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7352{
74541ad4 7353 const struct elf_backend_data *bed;
23ec1e32 7354 unsigned long section_sym_count;
96d01d93 7355 bfd_size_type dynsymcount = 0;
74541ad4 7356
8423293d 7357 if (!is_elf_hash_table (info->hash))
0a1b45a2 7358 return true;
8423293d 7359
74541ad4
AM
7360 bed = get_elf_backend_data (output_bfd);
7361 (*bed->elf_backend_init_index_section) (output_bfd, info);
7362
23ec1e32
MR
7363 /* Assign dynsym indices. In a shared library we generate a section
7364 symbol for each output section, which come first. Next come all
7365 of the back-end allocated local dynamic syms, followed by the rest
7366 of the global symbols.
7367
7368 This is usually not needed for static binaries, however backends
7369 can request to always do it, e.g. the MIPS backend uses dynamic
7370 symbol counts to lay out GOT, which will be produced in the
7371 presence of GOT relocations even in static binaries (holding fixed
7372 data in that case, to satisfy those relocations). */
7373
7374 if (elf_hash_table (info)->dynamic_sections_created
7375 || bed->always_renumber_dynsyms)
7376 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7377 &section_sym_count);
7378
8423293d
AM
7379 if (elf_hash_table (info)->dynamic_sections_created)
7380 {
7381 bfd *dynobj;
8423293d 7382 asection *s;
8423293d
AM
7383 unsigned int dtagcount;
7384
7385 dynobj = elf_hash_table (info)->dynobj;
7386
5a580b3a 7387 /* Work out the size of the symbol version section. */
3d4d4302 7388 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 7389 BFD_ASSERT (s != NULL);
d5486c43 7390 if ((s->flags & SEC_EXCLUDE) == 0)
5a580b3a 7391 {
eea6121a 7392 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 7393 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a 7394 if (s->contents == NULL)
0a1b45a2 7395 return false;
5a580b3a
AM
7396
7397 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
0a1b45a2 7398 return false;
5a580b3a
AM
7399 }
7400
7401 /* Set the size of the .dynsym and .hash sections. We counted
7402 the number of dynamic symbols in elf_link_add_object_symbols.
7403 We will build the contents of .dynsym and .hash when we build
7404 the final symbol table, because until then we do not know the
7405 correct value to give the symbols. We built the .dynstr
7406 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 7407 s = elf_hash_table (info)->dynsym;
5a580b3a 7408 BFD_ASSERT (s != NULL);
eea6121a 7409 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a 7410
d5486c43
L
7411 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7412 if (s->contents == NULL)
0a1b45a2 7413 return false;
5a580b3a 7414
d5486c43
L
7415 /* The first entry in .dynsym is a dummy symbol. Clear all the
7416 section syms, in case we don't output them all. */
7417 ++section_sym_count;
7418 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a 7419
fdc90cb4
JJ
7420 elf_hash_table (info)->bucketcount = 0;
7421
5a580b3a
AM
7422 /* Compute the size of the hashing table. As a side effect this
7423 computes the hash values for all the names we export. */
fdc90cb4
JJ
7424 if (info->emit_hash)
7425 {
7426 unsigned long int *hashcodes;
14b1c01e 7427 struct hash_codes_info hashinf;
fdc90cb4
JJ
7428 bfd_size_type amt;
7429 unsigned long int nsyms;
7430 size_t bucketcount;
7431 size_t hash_entry_size;
7432
7433 /* Compute the hash values for all exported symbols. At the same
7434 time store the values in an array so that we could use them for
7435 optimizations. */
7436 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 7437 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4 7438 if (hashcodes == NULL)
0a1b45a2 7439 return false;
14b1c01e 7440 hashinf.hashcodes = hashcodes;
0a1b45a2 7441 hashinf.error = false;
5a580b3a 7442
fdc90cb4
JJ
7443 /* Put all hash values in HASHCODES. */
7444 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
7445 elf_collect_hash_codes, &hashinf);
7446 if (hashinf.error)
4dd07732
AM
7447 {
7448 free (hashcodes);
0a1b45a2 7449 return false;
4dd07732 7450 }
5a580b3a 7451
14b1c01e 7452 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
7453 bucketcount
7454 = compute_bucket_count (info, hashcodes, nsyms, 0);
7455 free (hashcodes);
7456
4b48e2f6 7457 if (bucketcount == 0 && nsyms > 0)
0a1b45a2 7458 return false;
5a580b3a 7459
fdc90cb4
JJ
7460 elf_hash_table (info)->bucketcount = bucketcount;
7461
3d4d4302 7462 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
7463 BFD_ASSERT (s != NULL);
7464 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7465 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 7466 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4 7467 if (s->contents == NULL)
0a1b45a2 7468 return false;
fdc90cb4
JJ
7469
7470 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7471 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7472 s->contents + hash_entry_size);
7473 }
7474
7475 if (info->emit_gnu_hash)
7476 {
7477 size_t i, cnt;
7478 unsigned char *contents;
7479 struct collect_gnu_hash_codes cinfo;
7480 bfd_size_type amt;
7481 size_t bucketcount;
7482
7483 memset (&cinfo, 0, sizeof (cinfo));
7484
7485 /* Compute the hash values for all exported symbols. At the same
7486 time store the values in an array so that we could use them for
7487 optimizations. */
7488 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 7489 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4 7490 if (cinfo.hashcodes == NULL)
0a1b45a2 7491 return false;
fdc90cb4
JJ
7492
7493 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7494 cinfo.min_dynindx = -1;
7495 cinfo.output_bfd = output_bfd;
7496 cinfo.bed = bed;
7497
7498 /* Put all hash values in HASHCODES. */
7499 elf_link_hash_traverse (elf_hash_table (info),
7500 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 7501 if (cinfo.error)
4dd07732
AM
7502 {
7503 free (cinfo.hashcodes);
0a1b45a2 7504 return false;
4dd07732 7505 }
fdc90cb4
JJ
7506
7507 bucketcount
7508 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7509
7510 if (bucketcount == 0)
7511 {
7512 free (cinfo.hashcodes);
0a1b45a2 7513 return false;
fdc90cb4
JJ
7514 }
7515
f16a9783 7516 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
fdc90cb4
JJ
7517 BFD_ASSERT (s != NULL);
7518
7519 if (cinfo.nsyms == 0)
7520 {
f16a9783 7521 /* Empty .gnu.hash or .MIPS.xhash section is special. */
fdc90cb4
JJ
7522 BFD_ASSERT (cinfo.min_dynindx == -1);
7523 free (cinfo.hashcodes);
7524 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 7525 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4 7526 if (contents == NULL)
0a1b45a2 7527 return false;
fdc90cb4
JJ
7528 s->contents = contents;
7529 /* 1 empty bucket. */
7530 bfd_put_32 (output_bfd, 1, contents);
7531 /* SYMIDX above the special symbol 0. */
7532 bfd_put_32 (output_bfd, 1, contents + 4);
7533 /* Just one word for bitmask. */
7534 bfd_put_32 (output_bfd, 1, contents + 8);
7535 /* Only hash fn bloom filter. */
7536 bfd_put_32 (output_bfd, 0, contents + 12);
7537 /* No hashes are valid - empty bitmask. */
7538 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7539 /* No hashes in the only bucket. */
7540 bfd_put_32 (output_bfd, 0,
7541 contents + 16 + bed->s->arch_size / 8);
7542 }
7543 else
7544 {
9e6619e2 7545 unsigned long int maskwords, maskbitslog2, x;
0b33793d 7546 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 7547
9e6619e2
AM
7548 x = cinfo.nsyms;
7549 maskbitslog2 = 1;
7550 while ((x >>= 1) != 0)
7551 ++maskbitslog2;
fdc90cb4
JJ
7552 if (maskbitslog2 < 3)
7553 maskbitslog2 = 5;
7554 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7555 maskbitslog2 = maskbitslog2 + 3;
7556 else
7557 maskbitslog2 = maskbitslog2 + 2;
7558 if (bed->s->arch_size == 64)
7559 {
7560 if (maskbitslog2 == 5)
7561 maskbitslog2 = 6;
7562 cinfo.shift1 = 6;
7563 }
7564 else
7565 cinfo.shift1 = 5;
7566 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 7567 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
7568 cinfo.maskbits = 1 << maskbitslog2;
7569 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7570 amt = bucketcount * sizeof (unsigned long int) * 2;
7571 amt += maskwords * sizeof (bfd_vma);
a50b1753 7572 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
7573 if (cinfo.bitmask == NULL)
7574 {
7575 free (cinfo.hashcodes);
0a1b45a2 7576 return false;
fdc90cb4
JJ
7577 }
7578
a50b1753 7579 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
7580 cinfo.indx = cinfo.counts + bucketcount;
7581 cinfo.symindx = dynsymcount - cinfo.nsyms;
7582 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7583
7584 /* Determine how often each hash bucket is used. */
7585 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7586 for (i = 0; i < cinfo.nsyms; ++i)
7587 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7588
7589 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7590 if (cinfo.counts[i] != 0)
7591 {
7592 cinfo.indx[i] = cnt;
7593 cnt += cinfo.counts[i];
7594 }
7595 BFD_ASSERT (cnt == dynsymcount);
7596 cinfo.bucketcount = bucketcount;
7597 cinfo.local_indx = cinfo.min_dynindx;
7598
7599 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7600 s->size += cinfo.maskbits / 8;
f16a9783
MS
7601 if (bed->record_xhash_symbol != NULL)
7602 s->size += cinfo.nsyms * 4;
a50b1753 7603 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7604 if (contents == NULL)
7605 {
7606 free (cinfo.bitmask);
7607 free (cinfo.hashcodes);
0a1b45a2 7608 return false;
fdc90cb4
JJ
7609 }
7610
7611 s->contents = contents;
7612 bfd_put_32 (output_bfd, bucketcount, contents);
7613 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7614 bfd_put_32 (output_bfd, maskwords, contents + 8);
7615 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7616 contents += 16 + cinfo.maskbits / 8;
7617
7618 for (i = 0; i < bucketcount; ++i)
7619 {
7620 if (cinfo.counts[i] == 0)
7621 bfd_put_32 (output_bfd, 0, contents);
7622 else
7623 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7624 contents += 4;
7625 }
7626
7627 cinfo.contents = contents;
7628
f16a9783
MS
7629 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7630 /* Renumber dynamic symbols, if populating .gnu.hash section.
7631 If using .MIPS.xhash, populate the translation table. */
fdc90cb4 7632 elf_link_hash_traverse (elf_hash_table (info),
f16a9783 7633 elf_gnu_hash_process_symidx, &cinfo);
fdc90cb4
JJ
7634
7635 contents = s->contents + 16;
7636 for (i = 0; i < maskwords; ++i)
7637 {
7638 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7639 contents);
7640 contents += bed->s->arch_size / 8;
7641 }
7642
7643 free (cinfo.bitmask);
7644 free (cinfo.hashcodes);
7645 }
7646 }
5a580b3a 7647
3d4d4302 7648 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
7649 BFD_ASSERT (s != NULL);
7650
4ad4eba5 7651 elf_finalize_dynstr (output_bfd, info);
5a580b3a 7652
eea6121a 7653 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
7654
7655 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7656 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
0a1b45a2 7657 return false;
5a580b3a
AM
7658 }
7659
0a1b45a2 7660 return true;
5a580b3a 7661}
4d269e42 7662\f
4d269e42
AM
7663/* Make sure sec_info_type is cleared if sec_info is cleared too. */
7664
7665static void
7666merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7667 asection *sec)
7668{
dbaa2011
AM
7669 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7670 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
7671}
7672
7673/* Finish SHF_MERGE section merging. */
7674
0a1b45a2 7675bool
630993ec 7676_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
7677{
7678 bfd *ibfd;
7679 asection *sec;
7680
7681 if (!is_elf_hash_table (info->hash))
0a1b45a2 7682 return false;
4d269e42 7683
c72f2fb2 7684 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
7685 if ((ibfd->flags & DYNAMIC) == 0
7686 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
7687 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7688 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
7689 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7690 if ((sec->flags & SEC_MERGE) != 0
7691 && !bfd_is_abs_section (sec->output_section))
7692 {
7693 struct bfd_elf_section_data *secdata;
7694
7695 secdata = elf_section_data (sec);
630993ec 7696 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
7697 &elf_hash_table (info)->merge_info,
7698 sec, &secdata->sec_info))
0a1b45a2 7699 return false;
4d269e42 7700 else if (secdata->sec_info)
dbaa2011 7701 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
7702 }
7703
7704 if (elf_hash_table (info)->merge_info != NULL)
630993ec 7705 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42 7706 merge_sections_remove_hook);
0a1b45a2 7707 return true;
4d269e42
AM
7708}
7709
7710/* Create an entry in an ELF linker hash table. */
7711
7712struct bfd_hash_entry *
7713_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7714 struct bfd_hash_table *table,
7715 const char *string)
7716{
7717 /* Allocate the structure if it has not already been allocated by a
7718 subclass. */
7719 if (entry == NULL)
7720 {
a50b1753 7721 entry = (struct bfd_hash_entry *)
ca4be51c 7722 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
7723 if (entry == NULL)
7724 return entry;
7725 }
7726
7727 /* Call the allocation method of the superclass. */
7728 entry = _bfd_link_hash_newfunc (entry, table, string);
7729 if (entry != NULL)
7730 {
7731 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7732 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7733
7734 /* Set local fields. */
7735 ret->indx = -1;
7736 ret->dynindx = -1;
7737 ret->got = htab->init_got_refcount;
7738 ret->plt = htab->init_plt_refcount;
7739 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7740 - offsetof (struct elf_link_hash_entry, size)));
7741 /* Assume that we have been called by a non-ELF symbol reader.
7742 This flag is then reset by the code which reads an ELF input
7743 file. This ensures that a symbol created by a non-ELF symbol
7744 reader will have the flag set correctly. */
7745 ret->non_elf = 1;
7746 }
7747
7748 return entry;
7749}
7750
7751/* Copy data from an indirect symbol to its direct symbol, hiding the
7752 old indirect symbol. Also used for copying flags to a weakdef. */
7753
7754void
7755_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7756 struct elf_link_hash_entry *dir,
7757 struct elf_link_hash_entry *ind)
7758{
7759 struct elf_link_hash_table *htab;
7760
ad172eaa
L
7761 if (ind->dyn_relocs != NULL)
7762 {
7763 if (dir->dyn_relocs != NULL)
7764 {
7765 struct elf_dyn_relocs **pp;
7766 struct elf_dyn_relocs *p;
7767
7768 /* Add reloc counts against the indirect sym to the direct sym
7769 list. Merge any entries against the same section. */
7770 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
7771 {
7772 struct elf_dyn_relocs *q;
7773
7774 for (q = dir->dyn_relocs; q != NULL; q = q->next)
7775 if (q->sec == p->sec)
7776 {
7777 q->pc_count += p->pc_count;
7778 q->count += p->count;
7779 *pp = p->next;
7780 break;
7781 }
7782 if (q == NULL)
7783 pp = &p->next;
7784 }
7785 *pp = dir->dyn_relocs;
7786 }
7787
7788 dir->dyn_relocs = ind->dyn_relocs;
7789 ind->dyn_relocs = NULL;
7790 }
7791
4d269e42 7792 /* Copy down any references that we may have already seen to the
e81830c5 7793 symbol which just became indirect. */
4d269e42 7794
422f1182 7795 if (dir->versioned != versioned_hidden)
e81830c5
AM
7796 dir->ref_dynamic |= ind->ref_dynamic;
7797 dir->ref_regular |= ind->ref_regular;
7798 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7799 dir->non_got_ref |= ind->non_got_ref;
7800 dir->needs_plt |= ind->needs_plt;
7801 dir->pointer_equality_needed |= ind->pointer_equality_needed;
4d269e42
AM
7802
7803 if (ind->root.type != bfd_link_hash_indirect)
7804 return;
7805
7806 /* Copy over the global and procedure linkage table refcount entries.
7807 These may have been already set up by a check_relocs routine. */
7808 htab = elf_hash_table (info);
7809 if (ind->got.refcount > htab->init_got_refcount.refcount)
7810 {
7811 if (dir->got.refcount < 0)
7812 dir->got.refcount = 0;
7813 dir->got.refcount += ind->got.refcount;
7814 ind->got.refcount = htab->init_got_refcount.refcount;
7815 }
7816
7817 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7818 {
7819 if (dir->plt.refcount < 0)
7820 dir->plt.refcount = 0;
7821 dir->plt.refcount += ind->plt.refcount;
7822 ind->plt.refcount = htab->init_plt_refcount.refcount;
7823 }
7824
7825 if (ind->dynindx != -1)
7826 {
7827 if (dir->dynindx != -1)
7828 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7829 dir->dynindx = ind->dynindx;
7830 dir->dynstr_index = ind->dynstr_index;
7831 ind->dynindx = -1;
7832 ind->dynstr_index = 0;
7833 }
7834}
7835
7836void
7837_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7838 struct elf_link_hash_entry *h,
0a1b45a2 7839 bool force_local)
4d269e42 7840{
3aa14d16
L
7841 /* STT_GNU_IFUNC symbol must go through PLT. */
7842 if (h->type != STT_GNU_IFUNC)
7843 {
7844 h->plt = elf_hash_table (info)->init_plt_offset;
7845 h->needs_plt = 0;
7846 }
4d269e42
AM
7847 if (force_local)
7848 {
7849 h->forced_local = 1;
7850 if (h->dynindx != -1)
7851 {
4d269e42
AM
7852 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7853 h->dynstr_index);
641338d8
AM
7854 h->dynindx = -1;
7855 h->dynstr_index = 0;
4d269e42
AM
7856 }
7857 }
7858}
7859
34a87bb0
L
7860/* Hide a symbol. */
7861
7862void
7863_bfd_elf_link_hide_symbol (bfd *output_bfd,
7864 struct bfd_link_info *info,
7865 struct bfd_link_hash_entry *h)
7866{
7867 if (is_elf_hash_table (info->hash))
7868 {
7869 const struct elf_backend_data *bed
7870 = get_elf_backend_data (output_bfd);
7871 struct elf_link_hash_entry *eh
7872 = (struct elf_link_hash_entry *) h;
0a1b45a2 7873 bed->elf_backend_hide_symbol (info, eh, true);
34a87bb0
L
7874 eh->def_dynamic = 0;
7875 eh->ref_dynamic = 0;
7876 eh->dynamic_def = 0;
7877 }
7878}
7879
7bf52ea2
AM
7880/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7881 caller. */
4d269e42 7882
0a1b45a2 7883bool
4d269e42
AM
7884_bfd_elf_link_hash_table_init
7885 (struct elf_link_hash_table *table,
7886 bfd *abfd,
7887 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7888 struct bfd_hash_table *,
7889 const char *),
4dfe6ac6
NC
7890 unsigned int entsize,
7891 enum elf_target_id target_id)
4d269e42 7892{
0a1b45a2 7893 bool ret;
4d269e42
AM
7894 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7895
4d269e42
AM
7896 table->init_got_refcount.refcount = can_refcount - 1;
7897 table->init_plt_refcount.refcount = can_refcount - 1;
7898 table->init_got_offset.offset = -(bfd_vma) 1;
7899 table->init_plt_offset.offset = -(bfd_vma) 1;
7900 /* The first dynamic symbol is a dummy. */
7901 table->dynsymcount = 1;
7902
7903 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7904
4d269e42 7905 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7906 table->hash_table_id = target_id;
90c14f0c 7907 table->target_os = get_elf_backend_data (abfd)->target_os;
4d269e42
AM
7908
7909 return ret;
7910}
7911
7912/* Create an ELF linker hash table. */
7913
7914struct bfd_link_hash_table *
7915_bfd_elf_link_hash_table_create (bfd *abfd)
7916{
7917 struct elf_link_hash_table *ret;
986f0783 7918 size_t amt = sizeof (struct elf_link_hash_table);
4d269e42 7919
7bf52ea2 7920 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7921 if (ret == NULL)
7922 return NULL;
7923
7924 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7925 sizeof (struct elf_link_hash_entry),
7926 GENERIC_ELF_DATA))
4d269e42
AM
7927 {
7928 free (ret);
7929 return NULL;
7930 }
d495ab0d 7931 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7932
7933 return &ret->root;
7934}
7935
9f7c3e5e
AM
7936/* Destroy an ELF linker hash table. */
7937
7938void
d495ab0d 7939_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7940{
d495ab0d
AM
7941 struct elf_link_hash_table *htab;
7942
7943 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7944 if (htab->dynstr != NULL)
7945 _bfd_elf_strtab_free (htab->dynstr);
7946 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7947 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7948}
7949
4d269e42
AM
7950/* This is a hook for the ELF emulation code in the generic linker to
7951 tell the backend linker what file name to use for the DT_NEEDED
7952 entry for a dynamic object. */
7953
7954void
7955bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7956{
7957 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7958 && bfd_get_format (abfd) == bfd_object)
7959 elf_dt_name (abfd) = name;
7960}
7961
7962int
7963bfd_elf_get_dyn_lib_class (bfd *abfd)
7964{
7965 int lib_class;
7966 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7967 && bfd_get_format (abfd) == bfd_object)
7968 lib_class = elf_dyn_lib_class (abfd);
7969 else
7970 lib_class = 0;
7971 return lib_class;
7972}
7973
7974void
7975bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7976{
7977 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7978 && bfd_get_format (abfd) == bfd_object)
7979 elf_dyn_lib_class (abfd) = lib_class;
7980}
7981
7982/* Get the list of DT_NEEDED entries for a link. This is a hook for
7983 the linker ELF emulation code. */
7984
7985struct bfd_link_needed_list *
7986bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7987 struct bfd_link_info *info)
7988{
7989 if (! is_elf_hash_table (info->hash))
7990 return NULL;
7991 return elf_hash_table (info)->needed;
7992}
7993
7994/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7995 hook for the linker ELF emulation code. */
7996
7997struct bfd_link_needed_list *
7998bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7999 struct bfd_link_info *info)
8000{
8001 if (! is_elf_hash_table (info->hash))
8002 return NULL;
8003 return elf_hash_table (info)->runpath;
8004}
8005
8006/* Get the name actually used for a dynamic object for a link. This
8007 is the SONAME entry if there is one. Otherwise, it is the string
8008 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
8009
8010const char *
8011bfd_elf_get_dt_soname (bfd *abfd)
8012{
8013 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8014 && bfd_get_format (abfd) == bfd_object)
8015 return elf_dt_name (abfd);
8016 return NULL;
8017}
8018
8019/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
8020 the ELF linker emulation code. */
8021
0a1b45a2 8022bool
4d269e42
AM
8023bfd_elf_get_bfd_needed_list (bfd *abfd,
8024 struct bfd_link_needed_list **pneeded)
8025{
8026 asection *s;
8027 bfd_byte *dynbuf = NULL;
cb33740c 8028 unsigned int elfsec;
4d269e42
AM
8029 unsigned long shlink;
8030 bfd_byte *extdyn, *extdynend;
8031 size_t extdynsize;
8032 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
8033
8034 *pneeded = NULL;
8035
8036 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
8037 || bfd_get_format (abfd) != bfd_object)
0a1b45a2 8038 return true;
4d269e42
AM
8039
8040 s = bfd_get_section_by_name (abfd, ".dynamic");
8041 if (s == NULL || s->size == 0)
0a1b45a2 8042 return true;
4d269e42
AM
8043
8044 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
8045 goto error_return;
8046
8047 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 8048 if (elfsec == SHN_BAD)
4d269e42
AM
8049 goto error_return;
8050
8051 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 8052
4d269e42
AM
8053 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
8054 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
8055
8056 extdyn = dynbuf;
8057 extdynend = extdyn + s->size;
8058 for (; extdyn < extdynend; extdyn += extdynsize)
8059 {
8060 Elf_Internal_Dyn dyn;
8061
8062 (*swap_dyn_in) (abfd, extdyn, &dyn);
8063
8064 if (dyn.d_tag == DT_NULL)
8065 break;
8066
8067 if (dyn.d_tag == DT_NEEDED)
8068 {
8069 const char *string;
8070 struct bfd_link_needed_list *l;
8071 unsigned int tagv = dyn.d_un.d_val;
986f0783 8072 size_t amt;
4d269e42
AM
8073
8074 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8075 if (string == NULL)
8076 goto error_return;
8077
8078 amt = sizeof *l;
a50b1753 8079 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
8080 if (l == NULL)
8081 goto error_return;
8082
8083 l->by = abfd;
8084 l->name = string;
8085 l->next = *pneeded;
8086 *pneeded = l;
8087 }
8088 }
8089
8090 free (dynbuf);
8091
0a1b45a2 8092 return true;
4d269e42
AM
8093
8094 error_return:
c9594989 8095 free (dynbuf);
0a1b45a2 8096 return false;
4d269e42
AM
8097}
8098
8099struct elf_symbuf_symbol
8100{
8101 unsigned long st_name; /* Symbol name, index in string tbl */
8102 unsigned char st_info; /* Type and binding attributes */
8103 unsigned char st_other; /* Visibilty, and target specific */
8104};
8105
8106struct elf_symbuf_head
8107{
8108 struct elf_symbuf_symbol *ssym;
ef53be89 8109 size_t count;
4d269e42
AM
8110 unsigned int st_shndx;
8111};
8112
8113struct elf_symbol
8114{
8115 union
8116 {
8117 Elf_Internal_Sym *isym;
8118 struct elf_symbuf_symbol *ssym;
dcea6a95 8119 void *p;
4d269e42
AM
8120 } u;
8121 const char *name;
8122};
8123
8124/* Sort references to symbols by ascending section number. */
8125
8126static int
8127elf_sort_elf_symbol (const void *arg1, const void *arg2)
8128{
8129 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8130 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8131
dcea6a95
AM
8132 if (s1->st_shndx != s2->st_shndx)
8133 return s1->st_shndx > s2->st_shndx ? 1 : -1;
8134 /* Final sort by the address of the sym in the symbuf ensures
8135 a stable sort. */
8136 if (s1 != s2)
8137 return s1 > s2 ? 1 : -1;
8138 return 0;
4d269e42
AM
8139}
8140
8141static int
8142elf_sym_name_compare (const void *arg1, const void *arg2)
8143{
8144 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8145 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
dcea6a95
AM
8146 int ret = strcmp (s1->name, s2->name);
8147 if (ret != 0)
8148 return ret;
8149 if (s1->u.p != s2->u.p)
8150 return s1->u.p > s2->u.p ? 1 : -1;
8151 return 0;
4d269e42
AM
8152}
8153
8154static struct elf_symbuf_head *
ef53be89 8155elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
4d269e42 8156{
14b1c01e 8157 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
8158 struct elf_symbuf_symbol *ssym;
8159 struct elf_symbuf_head *ssymbuf, *ssymhead;
446f7ed5 8160 size_t i, shndx_count, total_size, amt;
4d269e42 8161
446f7ed5
AM
8162 amt = symcount * sizeof (*indbuf);
8163 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
4d269e42
AM
8164 if (indbuf == NULL)
8165 return NULL;
8166
8167 for (ind = indbuf, i = 0; i < symcount; i++)
3818d4ab 8168 if (isymbuf[i].st_shndx != SHN_UNDEF)
4d269e42
AM
8169 *ind++ = &isymbuf[i];
8170 indbufend = ind;
8171
8172 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8173 elf_sort_elf_symbol);
8174
8175 shndx_count = 0;
8176 if (indbufend > indbuf)
8177 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8178 if (ind[0]->st_shndx != ind[1]->st_shndx)
8179 shndx_count++;
8180
3ae181ee
L
8181 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8182 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 8183 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
8184 if (ssymbuf == NULL)
8185 {
8186 free (indbuf);
8187 return NULL;
8188 }
8189
3ae181ee 8190 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
8191 ssymbuf->ssym = NULL;
8192 ssymbuf->count = shndx_count;
8193 ssymbuf->st_shndx = 0;
8194 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8195 {
8196 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8197 {
8198 ssymhead++;
8199 ssymhead->ssym = ssym;
8200 ssymhead->count = 0;
8201 ssymhead->st_shndx = (*ind)->st_shndx;
8202 }
8203 ssym->st_name = (*ind)->st_name;
8204 ssym->st_info = (*ind)->st_info;
8205 ssym->st_other = (*ind)->st_other;
8206 ssymhead->count++;
8207 }
ef53be89 8208 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
3ae181ee
L
8209 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
8210 == total_size));
4d269e42
AM
8211
8212 free (indbuf);
8213 return ssymbuf;
8214}
8215
8216/* Check if 2 sections define the same set of local and global
8217 symbols. */
8218
0a1b45a2 8219static bool
4d269e42
AM
8220bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8221 struct bfd_link_info *info)
8222{
8223 bfd *bfd1, *bfd2;
8224 const struct elf_backend_data *bed1, *bed2;
8225 Elf_Internal_Shdr *hdr1, *hdr2;
ef53be89 8226 size_t symcount1, symcount2;
4d269e42
AM
8227 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8228 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8229 Elf_Internal_Sym *isym, *isymend;
8230 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
3818d4ab 8231 size_t count1, count2, sec_count1, sec_count2, i;
cb33740c 8232 unsigned int shndx1, shndx2;
0a1b45a2
AM
8233 bool result;
8234 bool ignore_section_symbol_p;
4d269e42
AM
8235
8236 bfd1 = sec1->owner;
8237 bfd2 = sec2->owner;
8238
4d269e42
AM
8239 /* Both sections have to be in ELF. */
8240 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8241 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
0a1b45a2 8242 return false;
4d269e42
AM
8243
8244 if (elf_section_type (sec1) != elf_section_type (sec2))
0a1b45a2 8245 return false;
4d269e42 8246
4d269e42
AM
8247 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8248 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 8249 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
0a1b45a2 8250 return false;
4d269e42
AM
8251
8252 bed1 = get_elf_backend_data (bfd1);
8253 bed2 = get_elf_backend_data (bfd2);
8254 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8255 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8256 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8257 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8258
8259 if (symcount1 == 0 || symcount2 == 0)
0a1b45a2 8260 return false;
4d269e42 8261
0a1b45a2 8262 result = false;
4d269e42
AM
8263 isymbuf1 = NULL;
8264 isymbuf2 = NULL;
a50b1753
NC
8265 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8266 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42 8267
3818d4ab
L
8268 /* Ignore section symbols only when matching non-debugging sections
8269 or linkonce section with comdat section. */
8270 ignore_section_symbol_p
8271 = ((sec1->flags & SEC_DEBUGGING) == 0
8272 || ((elf_section_flags (sec1) & SHF_GROUP)
8273 != (elf_section_flags (sec2) & SHF_GROUP)));
8274
4d269e42
AM
8275 if (ssymbuf1 == NULL)
8276 {
8277 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8278 NULL, NULL, NULL);
8279 if (isymbuf1 == NULL)
8280 goto done;
8281
67411cbf 8282 if (info != NULL && !info->reduce_memory_overheads)
dcea6a95
AM
8283 {
8284 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8285 elf_tdata (bfd1)->symbuf = ssymbuf1;
8286 }
4d269e42
AM
8287 }
8288
8289 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8290 {
8291 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8292 NULL, NULL, NULL);
8293 if (isymbuf2 == NULL)
8294 goto done;
8295
67411cbf 8296 if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
dcea6a95
AM
8297 {
8298 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8299 elf_tdata (bfd2)->symbuf = ssymbuf2;
8300 }
4d269e42
AM
8301 }
8302
8303 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8304 {
8305 /* Optimized faster version. */
ef53be89 8306 size_t lo, hi, mid;
4d269e42
AM
8307 struct elf_symbol *symp;
8308 struct elf_symbuf_symbol *ssym, *ssymend;
8309
8310 lo = 0;
8311 hi = ssymbuf1->count;
8312 ssymbuf1++;
8313 count1 = 0;
3818d4ab 8314 sec_count1 = 0;
4d269e42
AM
8315 while (lo < hi)
8316 {
8317 mid = (lo + hi) / 2;
cb33740c 8318 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 8319 hi = mid;
cb33740c 8320 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
8321 lo = mid + 1;
8322 else
8323 {
8324 count1 = ssymbuf1[mid].count;
8325 ssymbuf1 += mid;
8326 break;
8327 }
8328 }
3818d4ab
L
8329 if (ignore_section_symbol_p)
8330 {
8331 for (i = 0; i < count1; i++)
8332 if (ELF_ST_TYPE (ssymbuf1->ssym[i].st_info) == STT_SECTION)
8333 sec_count1++;
8334 count1 -= sec_count1;
8335 }
4d269e42
AM
8336
8337 lo = 0;
8338 hi = ssymbuf2->count;
8339 ssymbuf2++;
8340 count2 = 0;
3818d4ab 8341 sec_count2 = 0;
4d269e42
AM
8342 while (lo < hi)
8343 {
8344 mid = (lo + hi) / 2;
cb33740c 8345 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 8346 hi = mid;
cb33740c 8347 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
8348 lo = mid + 1;
8349 else
8350 {
8351 count2 = ssymbuf2[mid].count;
8352 ssymbuf2 += mid;
8353 break;
8354 }
8355 }
3818d4ab
L
8356 if (ignore_section_symbol_p)
8357 {
8358 for (i = 0; i < count2; i++)
8359 if (ELF_ST_TYPE (ssymbuf2->ssym[i].st_info) == STT_SECTION)
8360 sec_count2++;
8361 count2 -= sec_count2;
8362 }
4d269e42
AM
8363
8364 if (count1 == 0 || count2 == 0 || count1 != count2)
8365 goto done;
8366
ca4be51c
AM
8367 symtable1
8368 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8369 symtable2
8370 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
8371 if (symtable1 == NULL || symtable2 == NULL)
8372 goto done;
8373
8374 symp = symtable1;
3818d4ab
L
8375 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1 + sec_count1;
8376 ssym < ssymend; ssym++)
8377 if (sec_count1 == 0
8378 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8379 {
8380 symp->u.ssym = ssym;
8381 symp->name = bfd_elf_string_from_elf_section (bfd1,
8382 hdr1->sh_link,
8383 ssym->st_name);
8384 symp++;
8385 }
4d269e42
AM
8386
8387 symp = symtable2;
3818d4ab
L
8388 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2 + sec_count2;
8389 ssym < ssymend; ssym++)
8390 if (sec_count2 == 0
8391 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8392 {
8393 symp->u.ssym = ssym;
8394 symp->name = bfd_elf_string_from_elf_section (bfd2,
8395 hdr2->sh_link,
8396 ssym->st_name);
8397 symp++;
8398 }
4d269e42
AM
8399
8400 /* Sort symbol by name. */
8401 qsort (symtable1, count1, sizeof (struct elf_symbol),
8402 elf_sym_name_compare);
8403 qsort (symtable2, count1, sizeof (struct elf_symbol),
8404 elf_sym_name_compare);
8405
8406 for (i = 0; i < count1; i++)
8407 /* Two symbols must have the same binding, type and name. */
8408 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8409 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8410 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8411 goto done;
8412
0a1b45a2 8413 result = true;
4d269e42
AM
8414 goto done;
8415 }
8416
a50b1753
NC
8417 symtable1 = (struct elf_symbol *)
8418 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8419 symtable2 = (struct elf_symbol *)
8420 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
8421 if (symtable1 == NULL || symtable2 == NULL)
8422 goto done;
8423
8424 /* Count definitions in the section. */
8425 count1 = 0;
8426 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
3818d4ab
L
8427 if (isym->st_shndx == shndx1
8428 && (!ignore_section_symbol_p
8429 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
4d269e42
AM
8430 symtable1[count1++].u.isym = isym;
8431
8432 count2 = 0;
8433 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
3818d4ab
L
8434 if (isym->st_shndx == shndx2
8435 && (!ignore_section_symbol_p
8436 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
4d269e42
AM
8437 symtable2[count2++].u.isym = isym;
8438
8439 if (count1 == 0 || count2 == 0 || count1 != count2)
8440 goto done;
8441
8442 for (i = 0; i < count1; i++)
8443 symtable1[i].name
8444 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8445 symtable1[i].u.isym->st_name);
8446
8447 for (i = 0; i < count2; i++)
8448 symtable2[i].name
8449 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8450 symtable2[i].u.isym->st_name);
8451
8452 /* Sort symbol by name. */
8453 qsort (symtable1, count1, sizeof (struct elf_symbol),
8454 elf_sym_name_compare);
8455 qsort (symtable2, count1, sizeof (struct elf_symbol),
8456 elf_sym_name_compare);
8457
8458 for (i = 0; i < count1; i++)
8459 /* Two symbols must have the same binding, type and name. */
8460 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8461 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8462 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8463 goto done;
8464
0a1b45a2 8465 result = true;
4d269e42 8466
dc1e8a47 8467 done:
c9594989
AM
8468 free (symtable1);
8469 free (symtable2);
8470 free (isymbuf1);
8471 free (isymbuf2);
4d269e42
AM
8472
8473 return result;
8474}
8475
8476/* Return TRUE if 2 section types are compatible. */
8477
0a1b45a2 8478bool
4d269e42
AM
8479_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8480 bfd *bbfd, const asection *bsec)
8481{
8482 if (asec == NULL
8483 || bsec == NULL
8484 || abfd->xvec->flavour != bfd_target_elf_flavour
8485 || bbfd->xvec->flavour != bfd_target_elf_flavour)
0a1b45a2 8486 return true;
4d269e42
AM
8487
8488 return elf_section_type (asec) == elf_section_type (bsec);
8489}
8490\f
c152c796
AM
8491/* Final phase of ELF linker. */
8492
8493/* A structure we use to avoid passing large numbers of arguments. */
8494
8495struct elf_final_link_info
8496{
8497 /* General link information. */
8498 struct bfd_link_info *info;
8499 /* Output BFD. */
8500 bfd *output_bfd;
8501 /* Symbol string table. */
ef10c3ac 8502 struct elf_strtab_hash *symstrtab;
c152c796
AM
8503 /* .hash section. */
8504 asection *hash_sec;
8505 /* symbol version section (.gnu.version). */
8506 asection *symver_sec;
8507 /* Buffer large enough to hold contents of any section. */
8508 bfd_byte *contents;
8509 /* Buffer large enough to hold external relocs of any section. */
8510 void *external_relocs;
8511 /* Buffer large enough to hold internal relocs of any section. */
8512 Elf_Internal_Rela *internal_relocs;
8513 /* Buffer large enough to hold external local symbols of any input
8514 BFD. */
8515 bfd_byte *external_syms;
8516 /* And a buffer for symbol section indices. */
8517 Elf_External_Sym_Shndx *locsym_shndx;
8518 /* Buffer large enough to hold internal local symbols of any input
8519 BFD. */
8520 Elf_Internal_Sym *internal_syms;
8521 /* Array large enough to hold a symbol index for each local symbol
8522 of any input BFD. */
8523 long *indices;
8524 /* Array large enough to hold a section pointer for each local
8525 symbol of any input BFD. */
8526 asection **sections;
ef10c3ac 8527 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 8528 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
8529 /* Number of STT_FILE syms seen. */
8530 size_t filesym_count;
496afd17
L
8531 /* Local symbol hash table. */
8532 struct bfd_hash_table local_hash_table;
c152c796
AM
8533};
8534
496afd17
L
8535struct local_hash_entry
8536{
8537 /* Base hash table entry structure. */
8538 struct bfd_hash_entry root;
8539 /* Size of the local symbol name. */
8540 size_t size;
8541 /* Number of the duplicated local symbol names. */
8542 long count;
8543};
8544
8545/* Create an entry in the local symbol hash table. */
8546
8547static struct bfd_hash_entry *
8548local_hash_newfunc (struct bfd_hash_entry *entry,
8549 struct bfd_hash_table *table,
8550 const char *string)
8551{
8552
8553 /* Allocate the structure if it has not already been allocated by a
8554 subclass. */
8555 if (entry == NULL)
8556 {
8557 entry = bfd_hash_allocate (table,
8558 sizeof (struct local_hash_entry));
8559 if (entry == NULL)
8560 return entry;
8561 }
8562
8563 /* Call the allocation method of the superclass. */
8564 entry = bfd_hash_newfunc (entry, table, string);
8565 if (entry != NULL)
8566 {
8567 ((struct local_hash_entry *) entry)->count = 0;
8568 ((struct local_hash_entry *) entry)->size = 0;
8569 }
8570
8571 return entry;
8572}
8573
c152c796
AM
8574/* This struct is used to pass information to elf_link_output_extsym. */
8575
8576struct elf_outext_info
8577{
0a1b45a2
AM
8578 bool failed;
8579 bool localsyms;
8580 bool file_sym_done;
8b127cbc 8581 struct elf_final_link_info *flinfo;
c152c796
AM
8582};
8583
d9352518
DB
8584
8585/* Support for evaluating a complex relocation.
8586
8587 Complex relocations are generalized, self-describing relocations. The
8588 implementation of them consists of two parts: complex symbols, and the
a0c8462f 8589 relocations themselves.
d9352518 8590
4b69ce9b 8591 The relocations use a reserved elf-wide relocation type code (R_RELC
d9352518
DB
8592 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8593 information (start bit, end bit, word width, etc) into the addend. This
8594 information is extracted from CGEN-generated operand tables within gas.
8595
4b69ce9b 8596 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
d9352518
DB
8597 internal) representing prefix-notation expressions, including but not
8598 limited to those sorts of expressions normally encoded as addends in the
8599 addend field. The symbol mangling format is:
8600
8601 <node> := <literal>
07d6d2b8
AM
8602 | <unary-operator> ':' <node>
8603 | <binary-operator> ':' <node> ':' <node>
d9352518
DB
8604 ;
8605
8606 <literal> := 's' <digits=N> ':' <N character symbol name>
07d6d2b8 8607 | 'S' <digits=N> ':' <N character section name>
d9352518
DB
8608 | '#' <hexdigits>
8609 ;
8610
8611 <binary-operator> := as in C
8612 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8613
8614static void
a0c8462f
AM
8615set_symbol_value (bfd *bfd_with_globals,
8616 Elf_Internal_Sym *isymbuf,
8617 size_t locsymcount,
8618 size_t symidx,
8619 bfd_vma val)
d9352518 8620{
8977835c
AM
8621 struct elf_link_hash_entry **sym_hashes;
8622 struct elf_link_hash_entry *h;
8623 size_t extsymoff = locsymcount;
d9352518 8624
8977835c 8625 if (symidx < locsymcount)
d9352518 8626 {
8977835c
AM
8627 Elf_Internal_Sym *sym;
8628
8629 sym = isymbuf + symidx;
8630 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8631 {
8632 /* It is a local symbol: move it to the
8633 "absolute" section and give it a value. */
8634 sym->st_shndx = SHN_ABS;
8635 sym->st_value = val;
8636 return;
8637 }
8638 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8639 extsymoff = 0;
d9352518 8640 }
8977835c
AM
8641
8642 /* It is a global symbol: set its link type
8643 to "defined" and give it a value. */
8644
8645 sym_hashes = elf_sym_hashes (bfd_with_globals);
8646 h = sym_hashes [symidx - extsymoff];
8647 while (h->root.type == bfd_link_hash_indirect
8648 || h->root.type == bfd_link_hash_warning)
8649 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8650 h->root.type = bfd_link_hash_defined;
8651 h->root.u.def.value = val;
8652 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
8653}
8654
0a1b45a2 8655static bool
a0c8462f
AM
8656resolve_symbol (const char *name,
8657 bfd *input_bfd,
8b127cbc 8658 struct elf_final_link_info *flinfo,
a0c8462f
AM
8659 bfd_vma *result,
8660 Elf_Internal_Sym *isymbuf,
8661 size_t locsymcount)
d9352518 8662{
a0c8462f
AM
8663 Elf_Internal_Sym *sym;
8664 struct bfd_link_hash_entry *global_entry;
8665 const char *candidate = NULL;
8666 Elf_Internal_Shdr *symtab_hdr;
8667 size_t i;
8668
d9352518
DB
8669 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8670
8671 for (i = 0; i < locsymcount; ++ i)
8672 {
8977835c 8673 sym = isymbuf + i;
d9352518
DB
8674
8675 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8676 continue;
8677
8678 candidate = bfd_elf_string_from_elf_section (input_bfd,
8679 symtab_hdr->sh_link,
8680 sym->st_name);
8681#ifdef DEBUG
0f02bbd9
AM
8682 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8683 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
8684#endif
8685 if (candidate && strcmp (candidate, name) == 0)
8686 {
8b127cbc 8687 asection *sec = flinfo->sections [i];
d9352518 8688
0f02bbd9
AM
8689 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8690 *result += sec->output_offset + sec->output_section->vma;
d9352518 8691#ifdef DEBUG
0f02bbd9
AM
8692 printf ("Found symbol with value %8.8lx\n",
8693 (unsigned long) *result);
d9352518 8694#endif
0a1b45a2 8695 return true;
d9352518
DB
8696 }
8697 }
8698
8699 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 8700 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
0a1b45a2 8701 false, false, true);
d9352518 8702 if (!global_entry)
0a1b45a2 8703 return false;
a0c8462f 8704
d9352518
DB
8705 if (global_entry->type == bfd_link_hash_defined
8706 || global_entry->type == bfd_link_hash_defweak)
8707 {
a0c8462f
AM
8708 *result = (global_entry->u.def.value
8709 + global_entry->u.def.section->output_section->vma
8710 + global_entry->u.def.section->output_offset);
d9352518 8711#ifdef DEBUG
0f02bbd9
AM
8712 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8713 global_entry->root.string, (unsigned long) *result);
d9352518 8714#endif
0a1b45a2 8715 return true;
a0c8462f 8716 }
d9352518 8717
0a1b45a2 8718 return false;
d9352518
DB
8719}
8720
37b01f6a
DG
8721/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8722 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8723 names like "foo.end" which is the end address of section "foo". */
07d6d2b8 8724
0a1b45a2 8725static bool
a0c8462f
AM
8726resolve_section (const char *name,
8727 asection *sections,
37b01f6a
DG
8728 bfd_vma *result,
8729 bfd * abfd)
d9352518 8730{
a0c8462f
AM
8731 asection *curr;
8732 unsigned int len;
d9352518 8733
a0c8462f 8734 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8735 if (strcmp (curr->name, name) == 0)
8736 {
8737 *result = curr->vma;
0a1b45a2 8738 return true;
d9352518
DB
8739 }
8740
8741 /* Hmm. still haven't found it. try pseudo-section names. */
37b01f6a 8742 /* FIXME: This could be coded more efficiently... */
a0c8462f 8743 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8744 {
8745 len = strlen (curr->name);
a0c8462f 8746 if (len > strlen (name))
d9352518
DB
8747 continue;
8748
8749 if (strncmp (curr->name, name, len) == 0)
8750 {
3f3328b8 8751 if (startswith (name + len, ".end"))
d9352518 8752 {
61826503 8753 *result = (curr->vma
bb294208 8754 + curr->size / bfd_octets_per_byte (abfd, curr));
0a1b45a2 8755 return true;
d9352518
DB
8756 }
8757
8758 /* Insert more pseudo-section names here, if you like. */
8759 }
8760 }
a0c8462f 8761
0a1b45a2 8762 return false;
d9352518
DB
8763}
8764
8765static void
a0c8462f 8766undefined_reference (const char *reftype, const char *name)
d9352518 8767{
695344c0 8768 /* xgettext:c-format */
a0c8462f
AM
8769 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8770 reftype, name);
4b69ce9b 8771 bfd_set_error (bfd_error_bad_value);
d9352518
DB
8772}
8773
0a1b45a2 8774static bool
a0c8462f
AM
8775eval_symbol (bfd_vma *result,
8776 const char **symp,
8777 bfd *input_bfd,
8b127cbc 8778 struct elf_final_link_info *flinfo,
a0c8462f
AM
8779 bfd_vma dot,
8780 Elf_Internal_Sym *isymbuf,
8781 size_t locsymcount,
8782 int signed_p)
d9352518 8783{
4b93929b
NC
8784 size_t len;
8785 size_t symlen;
a0c8462f
AM
8786 bfd_vma a;
8787 bfd_vma b;
4b93929b 8788 char symbuf[4096];
0f02bbd9 8789 const char *sym = *symp;
a0c8462f 8790 const char *symend;
0a1b45a2 8791 bool symbol_is_section = false;
d9352518
DB
8792
8793 len = strlen (sym);
8794 symend = sym + len;
8795
4b93929b 8796 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
8797 {
8798 bfd_set_error (bfd_error_invalid_operation);
0a1b45a2 8799 return false;
d9352518 8800 }
a0c8462f 8801
d9352518
DB
8802 switch (* sym)
8803 {
8804 case '.':
0f02bbd9
AM
8805 *result = dot;
8806 *symp = sym + 1;
0a1b45a2 8807 return true;
d9352518
DB
8808
8809 case '#':
0f02bbd9
AM
8810 ++sym;
8811 *result = strtoul (sym, (char **) symp, 16);
0a1b45a2 8812 return true;
d9352518
DB
8813
8814 case 'S':
0a1b45a2 8815 symbol_is_section = true;
1a0670f3 8816 /* Fall through. */
a0c8462f 8817 case 's':
0f02bbd9
AM
8818 ++sym;
8819 symlen = strtol (sym, (char **) symp, 10);
8820 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 8821
4b93929b 8822 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
8823 {
8824 bfd_set_error (bfd_error_invalid_operation);
0a1b45a2 8825 return false;
d9352518
DB
8826 }
8827
8828 memcpy (symbuf, sym, symlen);
a0c8462f 8829 symbuf[symlen] = '\0';
0f02bbd9 8830 *symp = sym + symlen;
a0c8462f
AM
8831
8832 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
8833 the symbol as a section, or vice-versa. so we're pretty liberal in our
8834 interpretation here; section means "try section first", not "must be a
8835 section", and likewise with symbol. */
8836
a0c8462f 8837 if (symbol_is_section)
d9352518 8838 {
37b01f6a 8839 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8b127cbc 8840 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8841 isymbuf, locsymcount))
d9352518
DB
8842 {
8843 undefined_reference ("section", symbuf);
0a1b45a2 8844 return false;
d9352518 8845 }
a0c8462f
AM
8846 }
8847 else
d9352518 8848 {
8b127cbc 8849 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8850 isymbuf, locsymcount)
8b127cbc 8851 && !resolve_section (symbuf, flinfo->output_bfd->sections,
37b01f6a 8852 result, input_bfd))
d9352518
DB
8853 {
8854 undefined_reference ("symbol", symbuf);
0a1b45a2 8855 return false;
d9352518
DB
8856 }
8857 }
8858
0a1b45a2 8859 return true;
a0c8462f 8860
d9352518
DB
8861 /* All that remains are operators. */
8862
8863#define UNARY_OP(op) \
3f3328b8 8864 if (startswith (sym, #op)) \
d9352518
DB
8865 { \
8866 sym += strlen (#op); \
a0c8462f
AM
8867 if (*sym == ':') \
8868 ++sym; \
0f02bbd9 8869 *symp = sym; \
8b127cbc 8870 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8871 isymbuf, locsymcount, signed_p)) \
0a1b45a2 8872 return false; \
a0c8462f 8873 if (signed_p) \
0f02bbd9 8874 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
8875 else \
8876 *result = op a; \
0a1b45a2 8877 return true; \
d9352518
DB
8878 }
8879
4b69ce9b 8880#define BINARY_OP_HEAD(op) \
3f3328b8 8881 if (startswith (sym, #op)) \
d9352518
DB
8882 { \
8883 sym += strlen (#op); \
a0c8462f
AM
8884 if (*sym == ':') \
8885 ++sym; \
0f02bbd9 8886 *symp = sym; \
8b127cbc 8887 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8888 isymbuf, locsymcount, signed_p)) \
0a1b45a2 8889 return false; \
0f02bbd9 8890 ++*symp; \
8b127cbc 8891 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 8892 isymbuf, locsymcount, signed_p)) \
0a1b45a2 8893 return false;
4b69ce9b 8894#define BINARY_OP_TAIL(op) \
a0c8462f 8895 if (signed_p) \
0f02bbd9 8896 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
8897 else \
8898 *result = a op b; \
0a1b45a2 8899 return true; \
d9352518 8900 }
4b69ce9b 8901#define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
d9352518
DB
8902
8903 default:
8904 UNARY_OP (0-);
4b69ce9b
AM
8905 BINARY_OP_HEAD (<<);
8906 if (b >= sizeof (a) * CHAR_BIT)
8907 {
8908 *result = 0;
0a1b45a2 8909 return true;
4b69ce9b
AM
8910 }
8911 signed_p = 0;
8912 BINARY_OP_TAIL (<<);
8913 BINARY_OP_HEAD (>>);
8914 if (b >= sizeof (a) * CHAR_BIT)
8915 {
8916 *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
0a1b45a2 8917 return true;
4b69ce9b
AM
8918 }
8919 BINARY_OP_TAIL (>>);
d9352518
DB
8920 BINARY_OP (==);
8921 BINARY_OP (!=);
8922 BINARY_OP (<=);
8923 BINARY_OP (>=);
8924 BINARY_OP (&&);
8925 BINARY_OP (||);
8926 UNARY_OP (~);
8927 UNARY_OP (!);
8928 BINARY_OP (*);
4b69ce9b
AM
8929 BINARY_OP_HEAD (/);
8930 if (b == 0)
8931 {
8932 _bfd_error_handler (_("division by zero"));
8933 bfd_set_error (bfd_error_bad_value);
0a1b45a2 8934 return false;
4b69ce9b
AM
8935 }
8936 BINARY_OP_TAIL (/);
8937 BINARY_OP_HEAD (%);
8938 if (b == 0)
8939 {
8940 _bfd_error_handler (_("division by zero"));
8941 bfd_set_error (bfd_error_bad_value);
0a1b45a2 8942 return false;
4b69ce9b
AM
8943 }
8944 BINARY_OP_TAIL (%);
d9352518
DB
8945 BINARY_OP (^);
8946 BINARY_OP (|);
8947 BINARY_OP (&);
8948 BINARY_OP (+);
8949 BINARY_OP (-);
8950 BINARY_OP (<);
8951 BINARY_OP (>);
8952#undef UNARY_OP
8953#undef BINARY_OP
8954 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8955 bfd_set_error (bfd_error_invalid_operation);
0a1b45a2 8956 return false;
d9352518
DB
8957 }
8958}
8959
d9352518 8960static void
a0c8462f
AM
8961put_value (bfd_vma size,
8962 unsigned long chunksz,
8963 bfd *input_bfd,
8964 bfd_vma x,
8965 bfd_byte *location)
d9352518
DB
8966{
8967 location += (size - chunksz);
8968
41cd1ad1 8969 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
8970 {
8971 switch (chunksz)
8972 {
d9352518
DB
8973 case 1:
8974 bfd_put_8 (input_bfd, x, location);
41cd1ad1 8975 x >>= 8;
d9352518
DB
8976 break;
8977 case 2:
8978 bfd_put_16 (input_bfd, x, location);
41cd1ad1 8979 x >>= 16;
d9352518
DB
8980 break;
8981 case 4:
8982 bfd_put_32 (input_bfd, x, location);
65164438
NC
8983 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8984 x >>= 16;
8985 x >>= 16;
d9352518 8986 break;
d9352518 8987#ifdef BFD64
41cd1ad1 8988 case 8:
d9352518 8989 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
8990 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8991 x >>= 32;
8992 x >>= 32;
8993 break;
d9352518 8994#endif
41cd1ad1
NC
8995 default:
8996 abort ();
d9352518
DB
8997 break;
8998 }
8999 }
9000}
9001
a0c8462f
AM
9002static bfd_vma
9003get_value (bfd_vma size,
9004 unsigned long chunksz,
9005 bfd *input_bfd,
9006 bfd_byte *location)
d9352518 9007{
9b239e0e 9008 int shift;
d9352518
DB
9009 bfd_vma x = 0;
9010
9b239e0e
NC
9011 /* Sanity checks. */
9012 BFD_ASSERT (chunksz <= sizeof (x)
9013 && size >= chunksz
9014 && chunksz != 0
9015 && (size % chunksz) == 0
9016 && input_bfd != NULL
9017 && location != NULL);
9018
9019 if (chunksz == sizeof (x))
9020 {
9021 BFD_ASSERT (size == chunksz);
9022
9023 /* Make sure that we do not perform an undefined shift operation.
9024 We know that size == chunksz so there will only be one iteration
9025 of the loop below. */
9026 shift = 0;
9027 }
9028 else
9029 shift = 8 * chunksz;
9030
a0c8462f 9031 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
9032 {
9033 switch (chunksz)
9034 {
d9352518 9035 case 1:
9b239e0e 9036 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
9037 break;
9038 case 2:
9b239e0e 9039 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
9040 break;
9041 case 4:
9b239e0e 9042 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 9043 break;
d9352518 9044#ifdef BFD64
9b239e0e
NC
9045 case 8:
9046 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 9047 break;
9b239e0e
NC
9048#endif
9049 default:
9050 abort ();
d9352518
DB
9051 }
9052 }
9053 return x;
9054}
9055
a0c8462f
AM
9056static void
9057decode_complex_addend (unsigned long *start, /* in bits */
9058 unsigned long *oplen, /* in bits */
9059 unsigned long *len, /* in bits */
9060 unsigned long *wordsz, /* in bytes */
9061 unsigned long *chunksz, /* in bytes */
9062 unsigned long *lsb0_p,
9063 unsigned long *signed_p,
9064 unsigned long *trunc_p,
9065 unsigned long encoded)
d9352518 9066{
07d6d2b8
AM
9067 * start = encoded & 0x3F;
9068 * len = (encoded >> 6) & 0x3F;
d9352518
DB
9069 * oplen = (encoded >> 12) & 0x3F;
9070 * wordsz = (encoded >> 18) & 0xF;
9071 * chunksz = (encoded >> 22) & 0xF;
9072 * lsb0_p = (encoded >> 27) & 1;
9073 * signed_p = (encoded >> 28) & 1;
9074 * trunc_p = (encoded >> 29) & 1;
9075}
9076
cdfeee4f 9077bfd_reloc_status_type
0f02bbd9 9078bfd_elf_perform_complex_relocation (bfd *input_bfd,
bb294208 9079 asection *input_section,
0f02bbd9
AM
9080 bfd_byte *contents,
9081 Elf_Internal_Rela *rel,
9082 bfd_vma relocation)
d9352518 9083{
0f02bbd9
AM
9084 bfd_vma shift, x, mask;
9085 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 9086 bfd_reloc_status_type r;
bb294208 9087 bfd_size_type octets;
d9352518
DB
9088
9089 /* Perform this reloc, since it is complex.
9090 (this is not to say that it necessarily refers to a complex
9091 symbol; merely that it is a self-describing CGEN based reloc.
9092 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 9093 word size, etc) encoded within it.). */
d9352518 9094
a0c8462f
AM
9095 decode_complex_addend (&start, &oplen, &len, &wordsz,
9096 &chunksz, &lsb0_p, &signed_p,
9097 &trunc_p, rel->r_addend);
d9352518
DB
9098
9099 mask = (((1L << (len - 1)) - 1) << 1) | 1;
9100
9101 if (lsb0_p)
9102 shift = (start + 1) - len;
9103 else
9104 shift = (8 * wordsz) - (start + len);
9105
bb294208
AM
9106 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
9107 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
d9352518
DB
9108
9109#ifdef DEBUG
9110 printf ("Doing complex reloc: "
9111 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9112 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9113 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9114 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
9115 oplen, (unsigned long) x, (unsigned long) mask,
9116 (unsigned long) relocation);
d9352518
DB
9117#endif
9118
cdfeee4f 9119 r = bfd_reloc_ok;
d9352518 9120 if (! trunc_p)
cdfeee4f
AM
9121 /* Now do an overflow check. */
9122 r = bfd_check_overflow ((signed_p
9123 ? complain_overflow_signed
9124 : complain_overflow_unsigned),
9125 len, 0, (8 * wordsz),
9126 relocation);
a0c8462f 9127
d9352518
DB
9128 /* Do the deed. */
9129 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9130
9131#ifdef DEBUG
9132 printf (" relocation: %8.8lx\n"
9133 " shifted mask: %8.8lx\n"
9134 " shifted/masked reloc: %8.8lx\n"
9135 " result: %8.8lx\n",
9ccb8af9
AM
9136 (unsigned long) relocation, (unsigned long) (mask << shift),
9137 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 9138#endif
bb294208 9139 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
cdfeee4f 9140 return r;
d9352518
DB
9141}
9142
0e287786
AM
9143/* Functions to read r_offset from external (target order) reloc
9144 entry. Faster than bfd_getl32 et al, because we let the compiler
9145 know the value is aligned. */
53df40a4 9146
0e287786
AM
9147static bfd_vma
9148ext32l_r_offset (const void *p)
53df40a4
AM
9149{
9150 union aligned32
9151 {
9152 uint32_t v;
9153 unsigned char c[4];
9154 };
9155 const union aligned32 *a
0e287786 9156 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
9157
9158 uint32_t aval = ( (uint32_t) a->c[0]
9159 | (uint32_t) a->c[1] << 8
9160 | (uint32_t) a->c[2] << 16
9161 | (uint32_t) a->c[3] << 24);
0e287786 9162 return aval;
53df40a4
AM
9163}
9164
0e287786
AM
9165static bfd_vma
9166ext32b_r_offset (const void *p)
53df40a4
AM
9167{
9168 union aligned32
9169 {
9170 uint32_t v;
9171 unsigned char c[4];
9172 };
9173 const union aligned32 *a
0e287786 9174 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
9175
9176 uint32_t aval = ( (uint32_t) a->c[0] << 24
9177 | (uint32_t) a->c[1] << 16
9178 | (uint32_t) a->c[2] << 8
9179 | (uint32_t) a->c[3]);
0e287786 9180 return aval;
53df40a4
AM
9181}
9182
9183#ifdef BFD_HOST_64_BIT
0e287786
AM
9184static bfd_vma
9185ext64l_r_offset (const void *p)
53df40a4
AM
9186{
9187 union aligned64
9188 {
9189 uint64_t v;
9190 unsigned char c[8];
9191 };
9192 const union aligned64 *a
0e287786 9193 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
9194
9195 uint64_t aval = ( (uint64_t) a->c[0]
9196 | (uint64_t) a->c[1] << 8
9197 | (uint64_t) a->c[2] << 16
9198 | (uint64_t) a->c[3] << 24
9199 | (uint64_t) a->c[4] << 32
9200 | (uint64_t) a->c[5] << 40
9201 | (uint64_t) a->c[6] << 48
9202 | (uint64_t) a->c[7] << 56);
0e287786 9203 return aval;
53df40a4
AM
9204}
9205
0e287786
AM
9206static bfd_vma
9207ext64b_r_offset (const void *p)
53df40a4
AM
9208{
9209 union aligned64
9210 {
9211 uint64_t v;
9212 unsigned char c[8];
9213 };
9214 const union aligned64 *a
0e287786 9215 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
9216
9217 uint64_t aval = ( (uint64_t) a->c[0] << 56
9218 | (uint64_t) a->c[1] << 48
9219 | (uint64_t) a->c[2] << 40
9220 | (uint64_t) a->c[3] << 32
9221 | (uint64_t) a->c[4] << 24
9222 | (uint64_t) a->c[5] << 16
9223 | (uint64_t) a->c[6] << 8
9224 | (uint64_t) a->c[7]);
0e287786 9225 return aval;
53df40a4
AM
9226}
9227#endif
9228
c152c796
AM
9229/* When performing a relocatable link, the input relocations are
9230 preserved. But, if they reference global symbols, the indices
d4730f92
BS
9231 referenced must be updated. Update all the relocations found in
9232 RELDATA. */
c152c796 9233
0a1b45a2 9234static bool
c152c796 9235elf_link_adjust_relocs (bfd *abfd,
9eaff861 9236 asection *sec,
28dbcedc 9237 struct bfd_elf_section_reloc_data *reldata,
0a1b45a2 9238 bool sort,
10bbbc1d 9239 struct bfd_link_info *info)
c152c796
AM
9240{
9241 unsigned int i;
9242 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9243 bfd_byte *erela;
9244 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9245 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9246 bfd_vma r_type_mask;
9247 int r_sym_shift;
d4730f92
BS
9248 unsigned int count = reldata->count;
9249 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 9250
d4730f92 9251 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
9252 {
9253 swap_in = bed->s->swap_reloc_in;
9254 swap_out = bed->s->swap_reloc_out;
9255 }
d4730f92 9256 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
9257 {
9258 swap_in = bed->s->swap_reloca_in;
9259 swap_out = bed->s->swap_reloca_out;
9260 }
9261 else
9262 abort ();
9263
9264 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9265 abort ();
9266
9267 if (bed->s->arch_size == 32)
9268 {
9269 r_type_mask = 0xff;
9270 r_sym_shift = 8;
9271 }
9272 else
9273 {
9274 r_type_mask = 0xffffffff;
9275 r_sym_shift = 32;
9276 }
9277
d4730f92
BS
9278 erela = reldata->hdr->contents;
9279 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
9280 {
9281 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9282 unsigned int j;
9283
9284 if (*rel_hash == NULL)
9285 continue;
9286
10bbbc1d
NC
9287 if ((*rel_hash)->indx == -2
9288 && info->gc_sections
9289 && ! info->gc_keep_exported)
9290 {
9291 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9793eb77 9292 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
10bbbc1d
NC
9293 abfd, sec,
9294 (*rel_hash)->root.root.string);
9793eb77 9295 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
d42c267e 9296 abfd, sec);
10bbbc1d 9297 bfd_set_error (bfd_error_invalid_operation);
0a1b45a2 9298 return false;
10bbbc1d 9299 }
c152c796
AM
9300 BFD_ASSERT ((*rel_hash)->indx >= 0);
9301
9302 (*swap_in) (abfd, erela, irela);
9303 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9304 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9305 | (irela[j].r_info & r_type_mask));
9306 (*swap_out) (abfd, irela, erela);
9307 }
53df40a4 9308
9eaff861
AO
9309 if (bed->elf_backend_update_relocs)
9310 (*bed->elf_backend_update_relocs) (sec, reldata);
9311
0e287786 9312 if (sort && count != 0)
53df40a4 9313 {
0e287786
AM
9314 bfd_vma (*ext_r_off) (const void *);
9315 bfd_vma r_off;
9316 size_t elt_size;
9317 bfd_byte *base, *end, *p, *loc;
bca6d0e3 9318 bfd_byte *buf = NULL;
28dbcedc
AM
9319
9320 if (bed->s->arch_size == 32)
9321 {
9322 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 9323 ext_r_off = ext32l_r_offset;
28dbcedc 9324 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 9325 ext_r_off = ext32b_r_offset;
28dbcedc
AM
9326 else
9327 abort ();
9328 }
53df40a4 9329 else
28dbcedc 9330 {
53df40a4 9331#ifdef BFD_HOST_64_BIT
28dbcedc 9332 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 9333 ext_r_off = ext64l_r_offset;
28dbcedc 9334 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 9335 ext_r_off = ext64b_r_offset;
28dbcedc 9336 else
53df40a4 9337#endif
28dbcedc
AM
9338 abort ();
9339 }
0e287786 9340
bca6d0e3
AM
9341 /* Must use a stable sort here. A modified insertion sort,
9342 since the relocs are mostly sorted already. */
0e287786
AM
9343 elt_size = reldata->hdr->sh_entsize;
9344 base = reldata->hdr->contents;
9345 end = base + count * elt_size;
bca6d0e3 9346 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
9347 abort ();
9348
9349 /* Ensure the first element is lowest. This acts as a sentinel,
9350 speeding the main loop below. */
9351 r_off = (*ext_r_off) (base);
9352 for (p = loc = base; (p += elt_size) < end; )
9353 {
9354 bfd_vma r_off2 = (*ext_r_off) (p);
9355 if (r_off > r_off2)
9356 {
9357 r_off = r_off2;
9358 loc = p;
9359 }
9360 }
9361 if (loc != base)
9362 {
9363 /* Don't just swap *base and *loc as that changes the order
9364 of the original base[0] and base[1] if they happen to
9365 have the same r_offset. */
bca6d0e3
AM
9366 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9367 memcpy (onebuf, loc, elt_size);
0e287786 9368 memmove (base + elt_size, base, loc - base);
bca6d0e3 9369 memcpy (base, onebuf, elt_size);
0e287786
AM
9370 }
9371
b29b8669 9372 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
9373 {
9374 /* base to p is sorted, *p is next to insert. */
9375 r_off = (*ext_r_off) (p);
9376 /* Search the sorted region for location to insert. */
9377 loc = p - elt_size;
9378 while (r_off < (*ext_r_off) (loc))
9379 loc -= elt_size;
9380 loc += elt_size;
9381 if (loc != p)
9382 {
bca6d0e3
AM
9383 /* Chances are there is a run of relocs to insert here,
9384 from one of more input files. Files are not always
9385 linked in order due to the way elf_link_input_bfd is
9386 called. See pr17666. */
9387 size_t sortlen = p - loc;
9388 bfd_vma r_off2 = (*ext_r_off) (loc);
9389 size_t runlen = elt_size;
9390 size_t buf_size = 96 * 1024;
9391 while (p + runlen < end
9392 && (sortlen <= buf_size
9393 || runlen + elt_size <= buf_size)
9394 && r_off2 > (*ext_r_off) (p + runlen))
9395 runlen += elt_size;
9396 if (buf == NULL)
9397 {
9398 buf = bfd_malloc (buf_size);
9399 if (buf == NULL)
0a1b45a2 9400 return false;
bca6d0e3
AM
9401 }
9402 if (runlen < sortlen)
9403 {
9404 memcpy (buf, p, runlen);
9405 memmove (loc + runlen, loc, sortlen);
9406 memcpy (loc, buf, runlen);
9407 }
9408 else
9409 {
9410 memcpy (buf, loc, sortlen);
9411 memmove (loc, p, runlen);
9412 memcpy (loc + runlen, buf, sortlen);
9413 }
b29b8669 9414 p += runlen - elt_size;
0e287786
AM
9415 }
9416 }
9417 /* Hashes are no longer valid. */
28dbcedc
AM
9418 free (reldata->hashes);
9419 reldata->hashes = NULL;
bca6d0e3 9420 free (buf);
53df40a4 9421 }
0a1b45a2 9422 return true;
c152c796
AM
9423}
9424
9425struct elf_link_sort_rela
9426{
9427 union {
9428 bfd_vma offset;
9429 bfd_vma sym_mask;
9430 } u;
9431 enum elf_reloc_type_class type;
9432 /* We use this as an array of size int_rels_per_ext_rel. */
9433 Elf_Internal_Rela rela[1];
9434};
9435
dcea6a95
AM
9436/* qsort stability here and for cmp2 is only an issue if multiple
9437 dynamic relocations are emitted at the same address. But targets
9438 that apply a series of dynamic relocations each operating on the
9439 result of the prior relocation can't use -z combreloc as
9440 implemented anyway. Such schemes tend to be broken by sorting on
9441 symbol index. That leaves dynamic NONE relocs as the only other
9442 case where ld might emit multiple relocs at the same address, and
9443 those are only emitted due to target bugs. */
9444
c152c796
AM
9445static int
9446elf_link_sort_cmp1 (const void *A, const void *B)
9447{
a50b1753
NC
9448 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9449 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
9450 int relativea, relativeb;
9451
9452 relativea = a->type == reloc_class_relative;
9453 relativeb = b->type == reloc_class_relative;
9454
9455 if (relativea < relativeb)
9456 return 1;
9457 if (relativea > relativeb)
9458 return -1;
9459 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9460 return -1;
9461 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9462 return 1;
9463 if (a->rela->r_offset < b->rela->r_offset)
9464 return -1;
9465 if (a->rela->r_offset > b->rela->r_offset)
9466 return 1;
9467 return 0;
9468}
9469
9470static int
9471elf_link_sort_cmp2 (const void *A, const void *B)
9472{
a50b1753
NC
9473 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9474 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 9475
7e612e98 9476 if (a->type < b->type)
c152c796 9477 return -1;
7e612e98 9478 if (a->type > b->type)
c152c796 9479 return 1;
7e612e98 9480 if (a->u.offset < b->u.offset)
c152c796 9481 return -1;
7e612e98 9482 if (a->u.offset > b->u.offset)
c152c796
AM
9483 return 1;
9484 if (a->rela->r_offset < b->rela->r_offset)
9485 return -1;
9486 if (a->rela->r_offset > b->rela->r_offset)
9487 return 1;
9488 return 0;
9489}
9490
9491static size_t
9492elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9493{
3410fea8 9494 asection *dynamic_relocs;
fc66a176
L
9495 asection *rela_dyn;
9496 asection *rel_dyn;
c152c796
AM
9497 bfd_size_type count, size;
9498 size_t i, ret, sort_elt, ext_size;
9499 bfd_byte *sort, *s_non_relative, *p;
9500 struct elf_link_sort_rela *sq;
9501 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9502 int i2e = bed->s->int_rels_per_ext_rel;
61826503 9503 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
c152c796
AM
9504 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9505 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9506 struct bfd_link_order *lo;
9507 bfd_vma r_sym_mask;
0a1b45a2 9508 bool use_rela;
c152c796 9509
3410fea8
NC
9510 /* Find a dynamic reloc section. */
9511 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9512 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9513 if (rela_dyn != NULL && rela_dyn->size > 0
9514 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 9515 {
0a1b45a2 9516 bool use_rela_initialised = false;
3410fea8
NC
9517
9518 /* This is just here to stop gcc from complaining.
c8e44c6d 9519 Its initialization checking code is not perfect. */
0a1b45a2 9520 use_rela = true;
3410fea8
NC
9521
9522 /* Both sections are present. Examine the sizes
9523 of the indirect sections to help us choose. */
9524 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9525 if (lo->type == bfd_indirect_link_order)
9526 {
9527 asection *o = lo->u.indirect.section;
9528
9529 if ((o->size % bed->s->sizeof_rela) == 0)
9530 {
9531 if ((o->size % bed->s->sizeof_rel) == 0)
9532 /* Section size is divisible by both rel and rela sizes.
9533 It is of no help to us. */
9534 ;
9535 else
9536 {
9537 /* Section size is only divisible by rela. */
535b785f 9538 if (use_rela_initialised && !use_rela)
3410fea8 9539 {
9793eb77 9540 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9541 "they are in more than one size"),
9542 abfd);
3410fea8
NC
9543 bfd_set_error (bfd_error_invalid_operation);
9544 return 0;
9545 }
9546 else
9547 {
0a1b45a2
AM
9548 use_rela = true;
9549 use_rela_initialised = true;
3410fea8
NC
9550 }
9551 }
9552 }
9553 else if ((o->size % bed->s->sizeof_rel) == 0)
9554 {
9555 /* Section size is only divisible by rel. */
535b785f 9556 if (use_rela_initialised && use_rela)
3410fea8 9557 {
9793eb77 9558 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9559 "they are in more than one size"),
9560 abfd);
3410fea8
NC
9561 bfd_set_error (bfd_error_invalid_operation);
9562 return 0;
9563 }
9564 else
9565 {
0a1b45a2
AM
9566 use_rela = false;
9567 use_rela_initialised = true;
3410fea8
NC
9568 }
9569 }
9570 else
9571 {
c8e44c6d
AM
9572 /* The section size is not divisible by either -
9573 something is wrong. */
9793eb77 9574 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9575 "they are of an unknown size"), abfd);
3410fea8
NC
9576 bfd_set_error (bfd_error_invalid_operation);
9577 return 0;
9578 }
9579 }
9580
9581 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9582 if (lo->type == bfd_indirect_link_order)
9583 {
9584 asection *o = lo->u.indirect.section;
9585
9586 if ((o->size % bed->s->sizeof_rela) == 0)
9587 {
9588 if ((o->size % bed->s->sizeof_rel) == 0)
9589 /* Section size is divisible by both rel and rela sizes.
9590 It is of no help to us. */
9591 ;
9592 else
9593 {
9594 /* Section size is only divisible by rela. */
535b785f 9595 if (use_rela_initialised && !use_rela)
3410fea8 9596 {
9793eb77 9597 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9598 "they are in more than one size"),
9599 abfd);
3410fea8
NC
9600 bfd_set_error (bfd_error_invalid_operation);
9601 return 0;
9602 }
9603 else
9604 {
0a1b45a2
AM
9605 use_rela = true;
9606 use_rela_initialised = true;
3410fea8
NC
9607 }
9608 }
9609 }
9610 else if ((o->size % bed->s->sizeof_rel) == 0)
9611 {
9612 /* Section size is only divisible by rel. */
535b785f 9613 if (use_rela_initialised && use_rela)
3410fea8 9614 {
9793eb77 9615 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9616 "they are in more than one size"),
9617 abfd);
3410fea8
NC
9618 bfd_set_error (bfd_error_invalid_operation);
9619 return 0;
9620 }
9621 else
9622 {
0a1b45a2
AM
9623 use_rela = false;
9624 use_rela_initialised = true;
3410fea8
NC
9625 }
9626 }
9627 else
9628 {
c8e44c6d
AM
9629 /* The section size is not divisible by either -
9630 something is wrong. */
9793eb77 9631 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9632 "they are of an unknown size"), abfd);
3410fea8
NC
9633 bfd_set_error (bfd_error_invalid_operation);
9634 return 0;
9635 }
9636 }
9637
9638 if (! use_rela_initialised)
9639 /* Make a guess. */
0a1b45a2 9640 use_rela = true;
c152c796 9641 }
fc66a176 9642 else if (rela_dyn != NULL && rela_dyn->size > 0)
0a1b45a2 9643 use_rela = true;
fc66a176 9644 else if (rel_dyn != NULL && rel_dyn->size > 0)
0a1b45a2 9645 use_rela = false;
c152c796 9646 else
fc66a176 9647 return 0;
3410fea8
NC
9648
9649 if (use_rela)
c152c796 9650 {
3410fea8 9651 dynamic_relocs = rela_dyn;
c152c796
AM
9652 ext_size = bed->s->sizeof_rela;
9653 swap_in = bed->s->swap_reloca_in;
9654 swap_out = bed->s->swap_reloca_out;
9655 }
3410fea8
NC
9656 else
9657 {
9658 dynamic_relocs = rel_dyn;
9659 ext_size = bed->s->sizeof_rel;
9660 swap_in = bed->s->swap_reloc_in;
9661 swap_out = bed->s->swap_reloc_out;
9662 }
c152c796
AM
9663
9664 size = 0;
3410fea8 9665 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 9666 if (lo->type == bfd_indirect_link_order)
3410fea8 9667 size += lo->u.indirect.section->size;
c152c796 9668
3410fea8 9669 if (size != dynamic_relocs->size)
c152c796
AM
9670 return 0;
9671
9672 sort_elt = (sizeof (struct elf_link_sort_rela)
9673 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
9674
9675 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
9676 if (count == 0)
9677 return 0;
a50b1753 9678 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 9679
c152c796
AM
9680 if (sort == NULL)
9681 {
9682 (*info->callbacks->warning)
9793eb77 9683 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
c152c796
AM
9684 return 0;
9685 }
9686
9687 if (bed->s->arch_size == 32)
9688 r_sym_mask = ~(bfd_vma) 0xff;
9689 else
9690 r_sym_mask = ~(bfd_vma) 0xffffffff;
9691
3410fea8 9692 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9693 if (lo->type == bfd_indirect_link_order)
9694 {
9695 bfd_byte *erel, *erelend;
9696 asection *o = lo->u.indirect.section;
9697
1da212d6
AM
9698 if (o->contents == NULL && o->size != 0)
9699 {
9700 /* This is a reloc section that is being handled as a normal
9701 section. See bfd_section_from_shdr. We can't combine
9702 relocs in this case. */
9703 free (sort);
9704 return 0;
9705 }
c152c796 9706 erel = o->contents;
eea6121a 9707 erelend = o->contents + o->size;
c8e44c6d 9708 p = sort + o->output_offset * opb / ext_size * sort_elt;
3410fea8 9709
c152c796
AM
9710 while (erel < erelend)
9711 {
9712 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 9713
c152c796 9714 (*swap_in) (abfd, erel, s->rela);
7e612e98 9715 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
9716 s->u.sym_mask = r_sym_mask;
9717 p += sort_elt;
9718 erel += ext_size;
9719 }
9720 }
9721
9722 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9723
9724 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9725 {
9726 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9727 if (s->type != reloc_class_relative)
9728 break;
9729 }
9730 ret = i;
9731 s_non_relative = p;
9732
9733 sq = (struct elf_link_sort_rela *) s_non_relative;
9734 for (; i < count; i++, p += sort_elt)
9735 {
9736 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9737 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9738 sq = sp;
9739 sp->u.offset = sq->rela->r_offset;
9740 }
9741
9742 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9743
c8e44c6d
AM
9744 struct elf_link_hash_table *htab = elf_hash_table (info);
9745 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9746 {
9747 /* We have plt relocs in .rela.dyn. */
9748 sq = (struct elf_link_sort_rela *) sort;
9749 for (i = 0; i < count; i++)
9750 if (sq[count - i - 1].type != reloc_class_plt)
9751 break;
9752 if (i != 0 && htab->srelplt->size == i * ext_size)
9753 {
9754 struct bfd_link_order **plo;
9755 /* Put srelplt link_order last. This is so the output_offset
9756 set in the next loop is correct for DT_JMPREL. */
9757 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9758 if ((*plo)->type == bfd_indirect_link_order
9759 && (*plo)->u.indirect.section == htab->srelplt)
9760 {
9761 lo = *plo;
9762 *plo = lo->next;
9763 }
9764 else
9765 plo = &(*plo)->next;
9766 *plo = lo;
9767 lo->next = NULL;
9768 dynamic_relocs->map_tail.link_order = lo;
9769 }
9770 }
9771
9772 p = sort;
3410fea8 9773 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9774 if (lo->type == bfd_indirect_link_order)
9775 {
9776 bfd_byte *erel, *erelend;
9777 asection *o = lo->u.indirect.section;
9778
9779 erel = o->contents;
eea6121a 9780 erelend = o->contents + o->size;
c8e44c6d 9781 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
c152c796
AM
9782 while (erel < erelend)
9783 {
9784 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9785 (*swap_out) (abfd, s->rela, erel);
9786 p += sort_elt;
9787 erel += ext_size;
9788 }
9789 }
9790
9791 free (sort);
3410fea8 9792 *psec = dynamic_relocs;
c152c796
AM
9793 return ret;
9794}
9795
ef10c3ac 9796/* Add a symbol to the output symbol string table. */
c152c796 9797
6e0b88f1 9798static int
37bb890f 9799elf_link_output_symstrtab (void *finf,
ef10c3ac
L
9800 const char *name,
9801 Elf_Internal_Sym *elfsym,
9802 asection *input_sec,
9803 struct elf_link_hash_entry *h)
c152c796 9804{
37bb890f 9805 struct elf_final_link_info *flinfo = finf;
6e0b88f1 9806 int (*output_symbol_hook)
c152c796
AM
9807 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9808 struct elf_link_hash_entry *);
ef10c3ac 9809 struct elf_link_hash_table *hash_table;
c152c796 9810 const struct elf_backend_data *bed;
ef10c3ac 9811 bfd_size_type strtabsize;
c152c796 9812
8539e4e8
AM
9813 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9814
8b127cbc 9815 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
9816 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9817 if (output_symbol_hook != NULL)
9818 {
8b127cbc 9819 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
9820 if (ret != 1)
9821 return ret;
c152c796
AM
9822 }
9823
06f44071
AM
9824 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
9825 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
9826 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
9827 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
9828
ef10c3ac
L
9829 if (name == NULL
9830 || *name == '\0'
9831 || (input_sec->flags & SEC_EXCLUDE))
9832 elfsym->st_name = (unsigned long) -1;
c152c796
AM
9833 else
9834 {
ef10c3ac
L
9835 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9836 to get the final offset for st_name. */
3f2e9699 9837 char *versioned_name = (char *) name;
496afd17
L
9838 if (h != NULL)
9839 {
9840 if (h->versioned == versioned && h->def_dynamic)
9841 {
9842 /* Keep only one '@' for versioned symbols defined in
9843 shared objects. */
9844 char *version = strrchr (name, ELF_VER_CHR);
9845 char *base_end = strchr (name, ELF_VER_CHR);
9846 if (version != base_end)
9847 {
9848 size_t base_len;
9849 size_t len = strlen (name);
9850 versioned_name = bfd_alloc (flinfo->output_bfd, len);
9851 if (versioned_name == NULL)
9852 return 0;
9853 base_len = base_end - name;
9854 memcpy (versioned_name, name, base_len);
9855 memcpy (versioned_name + base_len, version,
9856 len - base_len);
9857 }
9858 }
9859 }
9860 else if (flinfo->info->unique_symbol
9861 && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
3f2e9699 9862 {
496afd17 9863 struct local_hash_entry *lh;
4467df35
L
9864 size_t count_len;
9865 size_t base_len;
9866 char buf[30];
496afd17 9867 switch (ELF_ST_TYPE (elfsym->st_info))
3f2e9699 9868 {
496afd17
L
9869 case STT_FILE:
9870 case STT_SECTION:
9871 break;
9872 default:
9873 lh = (struct local_hash_entry *) bfd_hash_lookup
0a1b45a2 9874 (&flinfo->local_hash_table, name, true, false);
496afd17 9875 if (lh == NULL)
3f2e9699 9876 return 0;
4467df35
L
9877 /* Always append ".COUNT" to local symbols to avoid
9878 potential conflicts with local symbol "XXX.COUNT". */
9879 sprintf (buf, "%lx", lh->count);
9880 base_len = lh->size;
9881 if (!base_len)
496afd17 9882 {
4467df35
L
9883 base_len = strlen (name);
9884 lh->size = base_len;
496afd17 9885 }
4467df35
L
9886 count_len = strlen (buf);
9887 versioned_name = bfd_alloc (flinfo->output_bfd,
9888 base_len + count_len + 2);
9889 if (versioned_name == NULL)
9890 return 0;
9891 memcpy (versioned_name, name, base_len);
9892 versioned_name[base_len] = '.';
9893 memcpy (versioned_name + base_len + 1, buf,
9894 count_len + 1);
496afd17
L
9895 lh->count++;
9896 break;
3f2e9699
L
9897 }
9898 }
ef10c3ac
L
9899 elfsym->st_name
9900 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
0a1b45a2 9901 versioned_name, false);
c152c796 9902 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 9903 return 0;
c152c796
AM
9904 }
9905
ef10c3ac
L
9906 hash_table = elf_hash_table (flinfo->info);
9907 strtabsize = hash_table->strtabsize;
9908 if (strtabsize <= hash_table->strtabcount)
c152c796 9909 {
ef10c3ac
L
9910 strtabsize += strtabsize;
9911 hash_table->strtabsize = strtabsize;
9912 strtabsize *= sizeof (*hash_table->strtab);
9913 hash_table->strtab
9914 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9915 strtabsize);
9916 if (hash_table->strtab == NULL)
6e0b88f1 9917 return 0;
c152c796 9918 }
ef10c3ac
L
9919 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9920 hash_table->strtab[hash_table->strtabcount].dest_index
9921 = hash_table->strtabcount;
9922 hash_table->strtab[hash_table->strtabcount].destshndx_index
9923 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9924
ed48ec2e 9925 flinfo->output_bfd->symcount += 1;
ef10c3ac
L
9926 hash_table->strtabcount += 1;
9927
9928 return 1;
9929}
9930
9931/* Swap symbols out to the symbol table and flush the output symbols to
9932 the file. */
9933
0a1b45a2 9934static bool
ef10c3ac
L
9935elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9936{
9937 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
986f0783 9938 size_t amt;
ef53be89 9939 size_t i;
ef10c3ac
L
9940 const struct elf_backend_data *bed;
9941 bfd_byte *symbuf;
9942 Elf_Internal_Shdr *hdr;
9943 file_ptr pos;
0a1b45a2 9944 bool ret;
ef10c3ac
L
9945
9946 if (!hash_table->strtabcount)
0a1b45a2 9947 return true;
ef10c3ac
L
9948
9949 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9950
9951 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9952
ef10c3ac
L
9953 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9954 symbuf = (bfd_byte *) bfd_malloc (amt);
9955 if (symbuf == NULL)
0a1b45a2 9956 return false;
1b786873 9957
ef10c3ac 9958 if (flinfo->symshndxbuf)
c152c796 9959 {
ef53be89
AM
9960 amt = sizeof (Elf_External_Sym_Shndx);
9961 amt *= bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9962 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9963 if (flinfo->symshndxbuf == NULL)
c152c796 9964 {
ef10c3ac 9965 free (symbuf);
0a1b45a2 9966 return false;
c152c796 9967 }
c152c796
AM
9968 }
9969
3d16b64e 9970 /* Now swap out the symbols. */
ef10c3ac
L
9971 for (i = 0; i < hash_table->strtabcount; i++)
9972 {
9973 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9974 if (elfsym->sym.st_name == (unsigned long) -1)
9975 elfsym->sym.st_name = 0;
9976 else
9977 elfsym->sym.st_name
9978 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9979 elfsym->sym.st_name);
3d16b64e
NA
9980
9981 /* Inform the linker of the addition of this symbol. */
9982
9983 if (flinfo->info->callbacks->ctf_new_symbol)
9984 flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
9985 &elfsym->sym);
9986
ef10c3ac
L
9987 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9988 ((bfd_byte *) symbuf
9989 + (elfsym->dest_index
9990 * bed->s->sizeof_sym)),
9991 (flinfo->symshndxbuf
9992 + elfsym->destshndx_index));
9993 }
9994
9995 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9996 pos = hdr->sh_offset + hdr->sh_size;
9997 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9998 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9999 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
10000 {
10001 hdr->sh_size += amt;
0a1b45a2 10002 ret = true;
ef10c3ac
L
10003 }
10004 else
0a1b45a2 10005 ret = false;
c152c796 10006
ef10c3ac
L
10007 free (symbuf);
10008
10009 free (hash_table->strtab);
10010 hash_table->strtab = NULL;
10011
10012 return ret;
c152c796
AM
10013}
10014
c0d5a53d
L
10015/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
10016
0a1b45a2 10017static bool
c0d5a53d
L
10018check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
10019{
4fbb74a6
AM
10020 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
10021 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
10022 {
10023 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 10024 beyond 64k. */
4eca0228 10025 _bfd_error_handler
695344c0 10026 /* xgettext:c-format */
9793eb77 10027 (_("%pB: too many sections: %d (>= %d)"),
4fbb74a6 10028 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d 10029 bfd_set_error (bfd_error_nonrepresentable_section);
0a1b45a2 10030 return false;
c0d5a53d 10031 }
0a1b45a2 10032 return true;
c0d5a53d
L
10033}
10034
c152c796
AM
10035/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10036 allowing an unsatisfied unversioned symbol in the DSO to match a
10037 versioned symbol that would normally require an explicit version.
10038 We also handle the case that a DSO references a hidden symbol
10039 which may be satisfied by a versioned symbol in another DSO. */
10040
0a1b45a2 10041static bool
c152c796
AM
10042elf_link_check_versioned_symbol (struct bfd_link_info *info,
10043 const struct elf_backend_data *bed,
10044 struct elf_link_hash_entry *h)
10045{
10046 bfd *abfd;
10047 struct elf_link_loaded_list *loaded;
10048
10049 if (!is_elf_hash_table (info->hash))
0a1b45a2 10050 return false;
c152c796 10051
90c984fc
L
10052 /* Check indirect symbol. */
10053 while (h->root.type == bfd_link_hash_indirect)
10054 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10055
c152c796
AM
10056 switch (h->root.type)
10057 {
10058 default:
10059 abfd = NULL;
10060 break;
10061
10062 case bfd_link_hash_undefined:
10063 case bfd_link_hash_undefweak:
10064 abfd = h->root.u.undef.abfd;
f4ab0e2d
L
10065 if (abfd == NULL
10066 || (abfd->flags & DYNAMIC) == 0
e56f61be 10067 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
0a1b45a2 10068 return false;
c152c796
AM
10069 break;
10070
10071 case bfd_link_hash_defined:
10072 case bfd_link_hash_defweak:
10073 abfd = h->root.u.def.section->owner;
10074 break;
10075
10076 case bfd_link_hash_common:
10077 abfd = h->root.u.c.p->section->owner;
10078 break;
10079 }
10080 BFD_ASSERT (abfd != NULL);
10081
e310298c 10082 for (loaded = elf_hash_table (info)->dyn_loaded;
c152c796
AM
10083 loaded != NULL;
10084 loaded = loaded->next)
10085 {
10086 bfd *input;
10087 Elf_Internal_Shdr *hdr;
ef53be89
AM
10088 size_t symcount;
10089 size_t extsymcount;
10090 size_t extsymoff;
c152c796
AM
10091 Elf_Internal_Shdr *versymhdr;
10092 Elf_Internal_Sym *isym;
10093 Elf_Internal_Sym *isymend;
10094 Elf_Internal_Sym *isymbuf;
10095 Elf_External_Versym *ever;
10096 Elf_External_Versym *extversym;
10097
10098 input = loaded->abfd;
10099
10100 /* We check each DSO for a possible hidden versioned definition. */
10101 if (input == abfd
c152c796
AM
10102 || elf_dynversym (input) == 0)
10103 continue;
10104
10105 hdr = &elf_tdata (input)->dynsymtab_hdr;
10106
10107 symcount = hdr->sh_size / bed->s->sizeof_sym;
10108 if (elf_bad_symtab (input))
10109 {
10110 extsymcount = symcount;
10111 extsymoff = 0;
10112 }
10113 else
10114 {
10115 extsymcount = symcount - hdr->sh_info;
10116 extsymoff = hdr->sh_info;
10117 }
10118
10119 if (extsymcount == 0)
10120 continue;
10121
10122 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10123 NULL, NULL, NULL);
10124 if (isymbuf == NULL)
0a1b45a2 10125 return false;
c152c796
AM
10126
10127 /* Read in any version definitions. */
10128 versymhdr = &elf_tdata (input)->dynversym_hdr;
c152c796 10129 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
2bb3687b
AM
10130 || (extversym = (Elf_External_Versym *)
10131 _bfd_malloc_and_read (input, versymhdr->sh_size,
10132 versymhdr->sh_size)) == NULL)
c152c796 10133 {
c152c796 10134 free (isymbuf);
0a1b45a2 10135 return false;
c152c796
AM
10136 }
10137
10138 ever = extversym + extsymoff;
10139 isymend = isymbuf + extsymcount;
10140 for (isym = isymbuf; isym < isymend; isym++, ever++)
10141 {
10142 const char *name;
10143 Elf_Internal_Versym iver;
10144 unsigned short version_index;
10145
10146 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10147 || isym->st_shndx == SHN_UNDEF)
10148 continue;
10149
10150 name = bfd_elf_string_from_elf_section (input,
10151 hdr->sh_link,
10152 isym->st_name);
10153 if (strcmp (name, h->root.root.string) != 0)
10154 continue;
10155
10156 _bfd_elf_swap_versym_in (input, ever, &iver);
10157
d023c380
L
10158 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10159 && !(h->def_regular
10160 && h->forced_local))
c152c796
AM
10161 {
10162 /* If we have a non-hidden versioned sym, then it should
d023c380
L
10163 have provided a definition for the undefined sym unless
10164 it is defined in a non-shared object and forced local.
10165 */
c152c796
AM
10166 abort ();
10167 }
10168
10169 version_index = iver.vs_vers & VERSYM_VERSION;
10170 if (version_index == 1 || version_index == 2)
10171 {
10172 /* This is the base or first version. We can use it. */
10173 free (extversym);
10174 free (isymbuf);
0a1b45a2 10175 return true;
c152c796
AM
10176 }
10177 }
10178
10179 free (extversym);
10180 free (isymbuf);
10181 }
10182
0a1b45a2 10183 return false;
c152c796
AM
10184}
10185
b8871f35
L
10186/* Convert ELF common symbol TYPE. */
10187
10188static int
10189elf_link_convert_common_type (struct bfd_link_info *info, int type)
10190{
10191 /* Commom symbol can only appear in relocatable link. */
10192 if (!bfd_link_relocatable (info))
10193 abort ();
10194 switch (info->elf_stt_common)
10195 {
10196 case unchanged:
10197 break;
10198 case elf_stt_common:
10199 type = STT_COMMON;
10200 break;
10201 case no_elf_stt_common:
10202 type = STT_OBJECT;
10203 break;
10204 }
10205 return type;
10206}
10207
c152c796
AM
10208/* Add an external symbol to the symbol table. This is called from
10209 the hash table traversal routine. When generating a shared object,
10210 we go through the symbol table twice. The first time we output
10211 anything that might have been forced to local scope in a version
10212 script. The second time we output the symbols that are still
10213 global symbols. */
10214
0a1b45a2 10215static bool
7686d77d 10216elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 10217{
7686d77d 10218 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 10219 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 10220 struct elf_final_link_info *flinfo = eoinfo->flinfo;
0a1b45a2 10221 bool strip;
c152c796
AM
10222 Elf_Internal_Sym sym;
10223 asection *input_sec;
10224 const struct elf_backend_data *bed;
6e0b88f1
AM
10225 long indx;
10226 int ret;
b8871f35 10227 unsigned int type;
c152c796
AM
10228
10229 if (h->root.type == bfd_link_hash_warning)
10230 {
10231 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10232 if (h->root.type == bfd_link_hash_new)
0a1b45a2 10233 return true;
c152c796
AM
10234 }
10235
10236 /* Decide whether to output this symbol in this pass. */
10237 if (eoinfo->localsyms)
10238 {
4deb8f71 10239 if (!h->forced_local)
0a1b45a2 10240 return true;
c152c796
AM
10241 }
10242 else
10243 {
4deb8f71 10244 if (h->forced_local)
0a1b45a2 10245 return true;
c152c796
AM
10246 }
10247
8b127cbc 10248 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 10249
12ac1cf5 10250 if (h->root.type == bfd_link_hash_undefined)
c152c796 10251 {
12ac1cf5
NC
10252 /* If we have an undefined symbol reference here then it must have
10253 come from a shared library that is being linked in. (Undefined
98da7939
L
10254 references in regular files have already been handled unless
10255 they are in unreferenced sections which are removed by garbage
10256 collection). */
0a1b45a2 10257 bool ignore_undef = false;
12ac1cf5
NC
10258
10259 /* Some symbols may be special in that the fact that they're
10260 undefined can be safely ignored - let backend determine that. */
10261 if (bed->elf_backend_ignore_undef_symbol)
10262 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10263
10264 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 10265 if (!ignore_undef
c54f1524 10266 && h->ref_dynamic_nonweak
8b127cbc
AM
10267 && (!h->ref_regular || flinfo->info->gc_sections)
10268 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10269 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
95a51568
FS
10270 {
10271 flinfo->info->callbacks->undefined_symbol
10272 (flinfo->info, h->root.root.string,
10273 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10274 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10275 && !flinfo->info->warn_unresolved_syms);
10276 }
97196564
L
10277
10278 /* Strip a global symbol defined in a discarded section. */
10279 if (h->indx == -3)
0a1b45a2 10280 return true;
c152c796
AM
10281 }
10282
10283 /* We should also warn if a forced local symbol is referenced from
10284 shared libraries. */
0e1862bb 10285 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
10286 && h->forced_local
10287 && h->ref_dynamic
371a5866 10288 && h->def_regular
f5385ebf 10289 && !h->dynamic_def
ee659f1f 10290 && h->ref_dynamic_nonweak
8b127cbc 10291 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 10292 {
17d078c5
AM
10293 bfd *def_bfd;
10294 const char *msg;
90c984fc
L
10295 struct elf_link_hash_entry *hi = h;
10296
10297 /* Check indirect symbol. */
10298 while (hi->root.type == bfd_link_hash_indirect)
10299 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
10300
10301 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
695344c0 10302 /* xgettext:c-format */
871b3ab2 10303 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
17d078c5 10304 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
695344c0 10305 /* xgettext:c-format */
871b3ab2 10306 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
17d078c5 10307 else
695344c0 10308 /* xgettext:c-format */
871b3ab2 10309 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
8b127cbc 10310 def_bfd = flinfo->output_bfd;
90c984fc
L
10311 if (hi->root.u.def.section != bfd_abs_section_ptr)
10312 def_bfd = hi->root.u.def.section->owner;
c08bb8dd
AM
10313 _bfd_error_handler (msg, flinfo->output_bfd,
10314 h->root.root.string, def_bfd);
17d078c5 10315 bfd_set_error (bfd_error_bad_value);
0a1b45a2
AM
10316 eoinfo->failed = true;
10317 return false;
c152c796
AM
10318 }
10319
10320 /* We don't want to output symbols that have never been mentioned by
10321 a regular file, or that we have been told to strip. However, if
10322 h->indx is set to -2, the symbol is used by a reloc and we must
10323 output it. */
0a1b45a2 10324 strip = false;
c152c796 10325 if (h->indx == -2)
d983c8c5 10326 ;
f5385ebf 10327 else if ((h->def_dynamic
77cfaee6
AM
10328 || h->ref_dynamic
10329 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
10330 && !h->def_regular
10331 && !h->ref_regular)
0a1b45a2 10332 strip = true;
8b127cbc 10333 else if (flinfo->info->strip == strip_all)
0a1b45a2 10334 strip = true;
8b127cbc
AM
10335 else if (flinfo->info->strip == strip_some
10336 && bfd_hash_lookup (flinfo->info->keep_hash,
0a1b45a2
AM
10337 h->root.root.string, false, false) == NULL)
10338 strip = true;
d56d55e7
AM
10339 else if ((h->root.type == bfd_link_hash_defined
10340 || h->root.type == bfd_link_hash_defweak)
8b127cbc 10341 && ((flinfo->info->strip_discarded
dbaa2011 10342 && discarded_section (h->root.u.def.section))
ca4be51c
AM
10343 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10344 && h->root.u.def.section->owner != NULL
d56d55e7 10345 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
0a1b45a2 10346 strip = true;
9e2278f5
AM
10347 else if ((h->root.type == bfd_link_hash_undefined
10348 || h->root.type == bfd_link_hash_undefweak)
10349 && h->root.u.undef.abfd != NULL
10350 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
0a1b45a2 10351 strip = true;
c152c796 10352
b8871f35
L
10353 type = h->type;
10354
c152c796 10355 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
10356 nothing else to do. However, if it is a forced local symbol or
10357 an ifunc symbol we need to give the backend finish_dynamic_symbol
10358 function a chance to make it dynamic. */
c152c796
AM
10359 if (strip
10360 && h->dynindx == -1
b8871f35 10361 && type != STT_GNU_IFUNC
f5385ebf 10362 && !h->forced_local)
0a1b45a2 10363 return true;
c152c796
AM
10364
10365 sym.st_value = 0;
10366 sym.st_size = h->size;
10367 sym.st_other = h->other;
c152c796
AM
10368 switch (h->root.type)
10369 {
10370 default:
10371 case bfd_link_hash_new:
10372 case bfd_link_hash_warning:
10373 abort ();
0a1b45a2 10374 return false;
c152c796
AM
10375
10376 case bfd_link_hash_undefined:
10377 case bfd_link_hash_undefweak:
10378 input_sec = bfd_und_section_ptr;
10379 sym.st_shndx = SHN_UNDEF;
10380 break;
10381
10382 case bfd_link_hash_defined:
10383 case bfd_link_hash_defweak:
10384 {
10385 input_sec = h->root.u.def.section;
10386 if (input_sec->output_section != NULL)
10387 {
10388 sym.st_shndx =
8b127cbc 10389 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
10390 input_sec->output_section);
10391 if (sym.st_shndx == SHN_BAD)
10392 {
4eca0228 10393 _bfd_error_handler
695344c0 10394 /* xgettext:c-format */
871b3ab2 10395 (_("%pB: could not find output section %pA for input section %pA"),
8b127cbc 10396 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 10397 bfd_set_error (bfd_error_nonrepresentable_section);
0a1b45a2
AM
10398 eoinfo->failed = true;
10399 return false;
c152c796
AM
10400 }
10401
10402 /* ELF symbols in relocatable files are section relative,
10403 but in nonrelocatable files they are virtual
10404 addresses. */
10405 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 10406 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10407 {
10408 sym.st_value += input_sec->output_section->vma;
10409 if (h->type == STT_TLS)
10410 {
8b127cbc 10411 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
10412 if (tls_sec != NULL)
10413 sym.st_value -= tls_sec->vma;
c152c796
AM
10414 }
10415 }
10416 }
10417 else
10418 {
10419 BFD_ASSERT (input_sec->owner == NULL
10420 || (input_sec->owner->flags & DYNAMIC) != 0);
10421 sym.st_shndx = SHN_UNDEF;
10422 input_sec = bfd_und_section_ptr;
10423 }
10424 }
10425 break;
10426
10427 case bfd_link_hash_common:
10428 input_sec = h->root.u.c.p->section;
a4d8e49b 10429 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
10430 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10431 break;
10432
10433 case bfd_link_hash_indirect:
10434 /* These symbols are created by symbol versioning. They point
10435 to the decorated version of the name. For example, if the
10436 symbol foo@@GNU_1.2 is the default, which should be used when
10437 foo is used with no version, then we add an indirect symbol
10438 foo which points to foo@@GNU_1.2. We ignore these symbols,
10439 since the indirected symbol is already in the hash table. */
0a1b45a2 10440 return true;
c152c796
AM
10441 }
10442
b8871f35
L
10443 if (type == STT_COMMON || type == STT_OBJECT)
10444 switch (h->root.type)
10445 {
10446 case bfd_link_hash_common:
10447 type = elf_link_convert_common_type (flinfo->info, type);
10448 break;
10449 case bfd_link_hash_defined:
10450 case bfd_link_hash_defweak:
10451 if (bed->common_definition (&sym))
10452 type = elf_link_convert_common_type (flinfo->info, type);
10453 else
10454 type = STT_OBJECT;
10455 break;
10456 case bfd_link_hash_undefined:
10457 case bfd_link_hash_undefweak:
10458 break;
10459 default:
10460 abort ();
10461 }
10462
4deb8f71 10463 if (h->forced_local)
b8871f35
L
10464 {
10465 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10466 /* Turn off visibility on local symbol. */
10467 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10468 }
10469 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10470 else if (h->unique_global && h->def_regular)
10471 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10472 else if (h->root.type == bfd_link_hash_undefweak
10473 || h->root.type == bfd_link_hash_defweak)
10474 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10475 else
10476 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10477 sym.st_target_internal = h->target_internal;
10478
c152c796
AM
10479 /* Give the processor backend a chance to tweak the symbol value,
10480 and also to finish up anything that needs to be done for this
10481 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 10482 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 10483 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 10484 if ((h->type == STT_GNU_IFUNC
5f35ea9c 10485 && h->def_regular
0e1862bb 10486 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
10487 || ((h->dynindx != -1
10488 || h->forced_local)
0e1862bb 10489 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
10490 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10491 || h->root.type != bfd_link_hash_undefweak))
10492 || !h->forced_local)
8b127cbc 10493 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
10494 {
10495 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 10496 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796 10497 {
0a1b45a2
AM
10498 eoinfo->failed = true;
10499 return false;
c152c796
AM
10500 }
10501 }
10502
10503 /* If we are marking the symbol as undefined, and there are no
10504 non-weak references to this symbol from a regular object, then
10505 mark the symbol as weak undefined; if there are non-weak
10506 references, mark the symbol as strong. We can't do this earlier,
10507 because it might not be marked as undefined until the
10508 finish_dynamic_symbol routine gets through with it. */
10509 if (sym.st_shndx == SHN_UNDEF
f5385ebf 10510 && h->ref_regular
c152c796
AM
10511 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10512 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10513 {
10514 int bindtype;
b8871f35 10515 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
10516
10517 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10518 if (type == STT_GNU_IFUNC)
10519 type = STT_FUNC;
c152c796 10520
f5385ebf 10521 if (h->ref_regular_nonweak)
c152c796
AM
10522 bindtype = STB_GLOBAL;
10523 else
10524 bindtype = STB_WEAK;
2955ec4c 10525 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
10526 }
10527
bda987c2
CD
10528 /* If this is a symbol defined in a dynamic library, don't use the
10529 symbol size from the dynamic library. Relinking an executable
10530 against a new library may introduce gratuitous changes in the
10531 executable's symbols if we keep the size. */
10532 if (sym.st_shndx == SHN_UNDEF
10533 && !h->def_regular
10534 && h->def_dynamic)
10535 sym.st_size = 0;
10536
c152c796
AM
10537 /* If a non-weak symbol with non-default visibility is not defined
10538 locally, it is a fatal error. */
0e1862bb 10539 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
10540 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10541 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10542 && h->root.type == bfd_link_hash_undefined
f5385ebf 10543 && !h->def_regular)
c152c796 10544 {
17d078c5
AM
10545 const char *msg;
10546
10547 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
695344c0 10548 /* xgettext:c-format */
871b3ab2 10549 msg = _("%pB: protected symbol `%s' isn't defined");
17d078c5 10550 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
695344c0 10551 /* xgettext:c-format */
871b3ab2 10552 msg = _("%pB: internal symbol `%s' isn't defined");
17d078c5 10553 else
695344c0 10554 /* xgettext:c-format */
871b3ab2 10555 msg = _("%pB: hidden symbol `%s' isn't defined");
4eca0228 10556 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 10557 bfd_set_error (bfd_error_bad_value);
0a1b45a2
AM
10558 eoinfo->failed = true;
10559 return false;
c152c796
AM
10560 }
10561
10562 /* If this symbol should be put in the .dynsym section, then put it
10563 there now. We already know the symbol index. We also fill in
10564 the entry in the .hash section. */
1c2649ed
EB
10565 if (h->dynindx != -1
10566 && elf_hash_table (flinfo->info)->dynamic_sections_created
10567 && elf_hash_table (flinfo->info)->dynsym != NULL
10568 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
c152c796 10569 {
c152c796
AM
10570 bfd_byte *esym;
10571
90c984fc
L
10572 /* Since there is no version information in the dynamic string,
10573 if there is no version info in symbol version section, we will
1659f720 10574 have a run-time problem if not linking executable, referenced
4deb8f71 10575 by shared library, or not bound locally. */
1659f720 10576 if (h->verinfo.verdef == NULL
0e1862bb 10577 && (!bfd_link_executable (flinfo->info)
1659f720
L
10578 || h->ref_dynamic
10579 || !h->def_regular))
90c984fc
L
10580 {
10581 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10582
10583 if (p && p [1] != '\0')
10584 {
4eca0228 10585 _bfd_error_handler
695344c0 10586 /* xgettext:c-format */
9793eb77 10587 (_("%pB: no symbol version section for versioned symbol `%s'"),
90c984fc 10588 flinfo->output_bfd, h->root.root.string);
0a1b45a2
AM
10589 eoinfo->failed = true;
10590 return false;
90c984fc
L
10591 }
10592 }
10593
c152c796 10594 sym.st_name = h->dynstr_index;
cae1fbbb
L
10595 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10596 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 10597 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d 10598 {
0a1b45a2
AM
10599 eoinfo->failed = true;
10600 return false;
c0d5a53d 10601 }
3d16b64e
NA
10602
10603 /* Inform the linker of the addition of this symbol. */
10604
10605 if (flinfo->info->callbacks->ctf_new_dynsym)
10606 flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
10607
8b127cbc 10608 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 10609
8b127cbc 10610 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
10611 {
10612 size_t hash_entry_size;
10613 bfd_byte *bucketpos;
10614 bfd_vma chain;
41198d0c
L
10615 size_t bucketcount;
10616 size_t bucket;
10617
8b127cbc 10618 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 10619 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
10620
10621 hash_entry_size
8b127cbc
AM
10622 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10623 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 10624 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
10625 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10626 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10627 bucketpos);
10628 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10629 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
10630 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10631 }
c152c796 10632
8b127cbc 10633 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
10634 {
10635 Elf_Internal_Versym iversym;
10636 Elf_External_Versym *eversym;
10637
5fa370e4 10638 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
c152c796 10639 {
7b20f099
AM
10640 if (h->verinfo.verdef == NULL
10641 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10642 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
eb6e6af8 10643 iversym.vs_vers = 1;
c152c796
AM
10644 else
10645 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10646 }
10647 else
10648 {
10649 if (h->verinfo.vertree == NULL)
10650 iversym.vs_vers = 1;
10651 else
10652 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 10653 if (flinfo->info->create_default_symver)
3e3b46e5 10654 iversym.vs_vers++;
c152c796
AM
10655 }
10656
422f1182 10657 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 10658 defined locally. */
422f1182 10659 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
10660 iversym.vs_vers |= VERSYM_HIDDEN;
10661
8b127cbc 10662 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 10663 eversym += h->dynindx;
8b127cbc 10664 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
10665 }
10666 }
10667
d983c8c5
AM
10668 /* If the symbol is undefined, and we didn't output it to .dynsym,
10669 strip it from .symtab too. Obviously we can't do this for
10670 relocatable output or when needed for --emit-relocs. */
10671 else if (input_sec == bfd_und_section_ptr
10672 && h->indx != -2
66cae560
NC
10673 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10674 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
0e1862bb 10675 && !bfd_link_relocatable (flinfo->info))
0a1b45a2 10676 return true;
66cae560 10677
d983c8c5
AM
10678 /* Also strip others that we couldn't earlier due to dynamic symbol
10679 processing. */
10680 if (strip)
0a1b45a2 10681 return true;
d983c8c5 10682 if ((input_sec->flags & SEC_EXCLUDE) != 0)
0a1b45a2 10683 return true;
c152c796 10684
2ec55de3
AM
10685 /* Output a FILE symbol so that following locals are not associated
10686 with the wrong input file. We need one for forced local symbols
10687 if we've seen more than one FILE symbol or when we have exactly
10688 one FILE symbol but global symbols are present in a file other
10689 than the one with the FILE symbol. We also need one if linker
10690 defined symbols are present. In practice these conditions are
10691 always met, so just emit the FILE symbol unconditionally. */
10692 if (eoinfo->localsyms
10693 && !eoinfo->file_sym_done
10694 && eoinfo->flinfo->filesym_count != 0)
10695 {
10696 Elf_Internal_Sym fsym;
10697
10698 memset (&fsym, 0, sizeof (fsym));
10699 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10700 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
10701 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10702 bfd_und_section_ptr, NULL))
0a1b45a2 10703 return false;
2ec55de3 10704
0a1b45a2 10705 eoinfo->file_sym_done = true;
2ec55de3
AM
10706 }
10707
8b127cbc 10708 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
10709 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10710 input_sec, h);
6e0b88f1 10711 if (ret == 0)
c152c796 10712 {
0a1b45a2
AM
10713 eoinfo->failed = true;
10714 return false;
c152c796 10715 }
6e0b88f1
AM
10716 else if (ret == 1)
10717 h->indx = indx;
10718 else if (h->indx == -2)
10719 abort();
c152c796 10720
0a1b45a2 10721 return true;
c152c796
AM
10722}
10723
cdd3575c
AM
10724/* Return TRUE if special handling is done for relocs in SEC against
10725 symbols defined in discarded sections. */
10726
0a1b45a2 10727static bool
c152c796
AM
10728elf_section_ignore_discarded_relocs (asection *sec)
10729{
10730 const struct elf_backend_data *bed;
10731
cdd3575c
AM
10732 switch (sec->sec_info_type)
10733 {
dbaa2011
AM
10734 case SEC_INFO_TYPE_STABS:
10735 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 10736 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
0a1b45a2 10737 return true;
cdd3575c
AM
10738 default:
10739 break;
10740 }
c152c796
AM
10741
10742 bed = get_elf_backend_data (sec->owner);
10743 if (bed->elf_backend_ignore_discarded_relocs != NULL
10744 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
0a1b45a2 10745 return true;
c152c796 10746
0a1b45a2 10747 return false;
c152c796
AM
10748}
10749
9e66c942
AM
10750/* Return a mask saying how ld should treat relocations in SEC against
10751 symbols defined in discarded sections. If this function returns
10752 COMPLAIN set, ld will issue a warning message. If this function
10753 returns PRETEND set, and the discarded section was link-once and the
10754 same size as the kept link-once section, ld will pretend that the
10755 symbol was actually defined in the kept section. Otherwise ld will
10756 zero the reloc (at least that is the intent, but some cooperation by
10757 the target dependent code is needed, particularly for REL targets). */
10758
8a696751
AM
10759unsigned int
10760_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 10761{
9e66c942 10762 if (sec->flags & SEC_DEBUGGING)
69d54b1b 10763 return PRETEND;
cdd3575c
AM
10764
10765 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 10766 return 0;
cdd3575c
AM
10767
10768 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 10769 return 0;
cdd3575c 10770
9e66c942 10771 return COMPLAIN | PRETEND;
cdd3575c
AM
10772}
10773
3d7f7666
L
10774/* Find a match between a section and a member of a section group. */
10775
10776static asection *
c0f00686
L
10777match_group_member (asection *sec, asection *group,
10778 struct bfd_link_info *info)
3d7f7666
L
10779{
10780 asection *first = elf_next_in_group (group);
10781 asection *s = first;
10782
10783 while (s != NULL)
10784 {
c0f00686 10785 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
10786 return s;
10787
83180ade 10788 s = elf_next_in_group (s);
3d7f7666
L
10789 if (s == first)
10790 break;
10791 }
10792
10793 return NULL;
10794}
10795
01b3c8ab 10796/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
10797 to replace it. Return the replacement if it is OK. Otherwise return
10798 NULL. */
01b3c8ab
L
10799
10800asection *
c0f00686 10801_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
10802{
10803 asection *kept;
10804
10805 kept = sec->kept_section;
10806 if (kept != NULL)
10807 {
c2370991 10808 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 10809 kept = match_group_member (sec, kept, info);
58349d00
L
10810 if (kept != NULL)
10811 {
10812 if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10813 != (kept->rawsize != 0 ? kept->rawsize : kept->size))
10814 kept = NULL;
10815 else
10816 {
10817 /* Get the real kept section. */
10818 asection *next;
10819 for (next = kept->kept_section;
10820 next != NULL;
10821 next = next->kept_section)
10822 kept = next;
10823 }
10824 }
c2370991 10825 sec->kept_section = kept;
01b3c8ab
L
10826 }
10827 return kept;
10828}
10829
c152c796
AM
10830/* Link an input file into the linker output file. This function
10831 handles all the sections and relocations of the input file at once.
10832 This is so that we only have to read the local symbols once, and
10833 don't have to keep them in memory. */
10834
0a1b45a2 10835static bool
8b127cbc 10836elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 10837{
ece5ef60 10838 int (*relocate_section)
c152c796
AM
10839 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10840 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10841 bfd *output_bfd;
10842 Elf_Internal_Shdr *symtab_hdr;
10843 size_t locsymcount;
10844 size_t extsymoff;
10845 Elf_Internal_Sym *isymbuf;
10846 Elf_Internal_Sym *isym;
10847 Elf_Internal_Sym *isymend;
10848 long *pindex;
10849 asection **ppsection;
10850 asection *o;
10851 const struct elf_backend_data *bed;
c152c796 10852 struct elf_link_hash_entry **sym_hashes;
310fd250
L
10853 bfd_size_type address_size;
10854 bfd_vma r_type_mask;
10855 int r_sym_shift;
0a1b45a2 10856 bool have_file_sym = false;
c152c796 10857
8b127cbc 10858 output_bfd = flinfo->output_bfd;
c152c796
AM
10859 bed = get_elf_backend_data (output_bfd);
10860 relocate_section = bed->elf_backend_relocate_section;
10861
10862 /* If this is a dynamic object, we don't want to do anything here:
10863 we don't want the local symbols, and we don't want the section
10864 contents. */
10865 if ((input_bfd->flags & DYNAMIC) != 0)
0a1b45a2 10866 return true;
c152c796 10867
c152c796
AM
10868 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10869 if (elf_bad_symtab (input_bfd))
10870 {
10871 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10872 extsymoff = 0;
10873 }
10874 else
10875 {
10876 locsymcount = symtab_hdr->sh_info;
10877 extsymoff = symtab_hdr->sh_info;
10878 }
10879
99fabbc9
JL
10880 /* Enable GNU OSABI features in the output BFD that are used in the input
10881 BFD. */
10882 if (bed->elf_osabi == ELFOSABI_NONE
10883 || bed->elf_osabi == ELFOSABI_GNU
10884 || bed->elf_osabi == ELFOSABI_FREEBSD)
10885 elf_tdata (output_bfd)->has_gnu_osabi
04f89674
L
10886 |= (elf_tdata (input_bfd)->has_gnu_osabi
10887 & (bfd_link_relocatable (flinfo->info)
10888 ? -1 : ~elf_gnu_osabi_retain));
99fabbc9 10889
c152c796
AM
10890 /* Read the local symbols. */
10891 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10892 if (isymbuf == NULL && locsymcount != 0)
10893 {
10894 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
10895 flinfo->internal_syms,
10896 flinfo->external_syms,
10897 flinfo->locsym_shndx);
c152c796 10898 if (isymbuf == NULL)
0a1b45a2 10899 return false;
c152c796
AM
10900 }
10901
10902 /* Find local symbol sections and adjust values of symbols in
10903 SEC_MERGE sections. Write out those local symbols we know are
10904 going into the output file. */
10905 isymend = isymbuf + locsymcount;
8b127cbc 10906 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
10907 isym < isymend;
10908 isym++, pindex++, ppsection++)
10909 {
10910 asection *isec;
10911 const char *name;
10912 Elf_Internal_Sym osym;
6e0b88f1
AM
10913 long indx;
10914 int ret;
c152c796
AM
10915
10916 *pindex = -1;
10917
10918 if (elf_bad_symtab (input_bfd))
10919 {
10920 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10921 {
10922 *ppsection = NULL;
10923 continue;
10924 }
10925 }
10926
10927 if (isym->st_shndx == SHN_UNDEF)
10928 isec = bfd_und_section_ptr;
c152c796
AM
10929 else if (isym->st_shndx == SHN_ABS)
10930 isec = bfd_abs_section_ptr;
10931 else if (isym->st_shndx == SHN_COMMON)
10932 isec = bfd_com_section_ptr;
10933 else
10934 {
cb33740c
AM
10935 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10936 if (isec == NULL)
10937 {
10938 /* Don't attempt to output symbols with st_shnx in the
10939 reserved range other than SHN_ABS and SHN_COMMON. */
6835821b 10940 isec = bfd_und_section_ptr;
cb33740c 10941 }
dbaa2011 10942 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
10943 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10944 isym->st_value =
10945 _bfd_merged_section_offset (output_bfd, &isec,
10946 elf_section_data (isec)->sec_info,
10947 isym->st_value);
c152c796
AM
10948 }
10949
10950 *ppsection = isec;
10951
d983c8c5
AM
10952 /* Don't output the first, undefined, symbol. In fact, don't
10953 output any undefined local symbol. */
10954 if (isec == bfd_und_section_ptr)
c152c796
AM
10955 continue;
10956
10957 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10958 {
10959 /* We never output section symbols. Instead, we use the
10960 section symbol of the corresponding section in the output
10961 file. */
10962 continue;
10963 }
10964
10965 /* If we are stripping all symbols, we don't want to output this
10966 one. */
8b127cbc 10967 if (flinfo->info->strip == strip_all)
c152c796
AM
10968 continue;
10969
10970 /* If we are discarding all local symbols, we don't want to
10971 output this one. If we are generating a relocatable output
10972 file, then some of the local symbols may be required by
10973 relocs; we output them below as we discover that they are
10974 needed. */
8b127cbc 10975 if (flinfo->info->discard == discard_all)
c152c796
AM
10976 continue;
10977
10978 /* If this symbol is defined in a section which we are
f02571c5 10979 discarding, we don't need to keep it. */
abf874aa
CL
10980 if (isym->st_shndx != SHN_UNDEF
10981 && isym->st_shndx < SHN_LORESERVE
10982 && isec->output_section == NULL
10983 && flinfo->info->non_contiguous_regions
10984 && flinfo->info->non_contiguous_regions_warnings)
10985 {
10986 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
10987 "discards section `%s' from '%s'\n"),
765cf5f6 10988 isec->name, bfd_get_filename (isec->owner));
abf874aa
CL
10989 continue;
10990 }
10991
f02571c5 10992 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
10993 && isym->st_shndx < SHN_LORESERVE
10994 && bfd_section_removed_from_list (output_bfd,
10995 isec->output_section))
e75a280b
L
10996 continue;
10997
c152c796
AM
10998 /* Get the name of the symbol. */
10999 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
11000 isym->st_name);
11001 if (name == NULL)
0a1b45a2 11002 return false;
c152c796
AM
11003
11004 /* See if we are discarding symbols with this name. */
8b127cbc 11005 if ((flinfo->info->strip == strip_some
0a1b45a2 11006 && (bfd_hash_lookup (flinfo->info->keep_hash, name, false, false)
c152c796 11007 == NULL))
8b127cbc 11008 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
11009 && (isec->flags & SEC_MERGE)
11010 && !bfd_link_relocatable (flinfo->info))
8b127cbc 11011 || flinfo->info->discard == discard_l)
c152c796
AM
11012 && bfd_is_local_label_name (input_bfd, name)))
11013 continue;
11014
ffbc01cc
AM
11015 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
11016 {
ce875075
AM
11017 if (input_bfd->lto_output)
11018 /* -flto puts a temp file name here. This means builds
11019 are not reproducible. Discard the symbol. */
11020 continue;
0a1b45a2 11021 have_file_sym = true;
ffbc01cc
AM
11022 flinfo->filesym_count += 1;
11023 }
11024 if (!have_file_sym)
11025 {
11026 /* In the absence of debug info, bfd_find_nearest_line uses
11027 FILE symbols to determine the source file for local
11028 function symbols. Provide a FILE symbol here if input
11029 files lack such, so that their symbols won't be
11030 associated with a previous input file. It's not the
11031 source file, but the best we can do. */
5b4293ba 11032 const char *filename;
0a1b45a2 11033 have_file_sym = true;
ffbc01cc
AM
11034 flinfo->filesym_count += 1;
11035 memset (&osym, 0, sizeof (osym));
11036 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11037 osym.st_shndx = SHN_ABS;
5b4293ba
AM
11038 if (input_bfd->lto_output)
11039 filename = NULL;
11040 else
11041 filename = lbasename (bfd_get_filename (input_bfd));
11042 if (!elf_link_output_symstrtab (flinfo, filename, &osym,
11043 bfd_abs_section_ptr, NULL))
0a1b45a2 11044 return false;
ffbc01cc
AM
11045 }
11046
c152c796
AM
11047 osym = *isym;
11048
11049 /* Adjust the section index for the output file. */
11050 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11051 isec->output_section);
11052 if (osym.st_shndx == SHN_BAD)
0a1b45a2 11053 return false;
c152c796 11054
c152c796
AM
11055 /* ELF symbols in relocatable files are section relative, but
11056 in executable files they are virtual addresses. Note that
11057 this code assumes that all ELF sections have an associated
11058 BFD section with a reasonable value for output_offset; below
11059 we assume that they also have a reasonable value for
11060 output_section. Any special sections must be set up to meet
11061 these requirements. */
11062 osym.st_value += isec->output_offset;
0e1862bb 11063 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11064 {
11065 osym.st_value += isec->output_section->vma;
11066 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
11067 {
11068 /* STT_TLS symbols are relative to PT_TLS segment base. */
102def4d
AM
11069 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
11070 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
11071 else
11072 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
11073 STT_NOTYPE);
c152c796
AM
11074 }
11075 }
11076
6e0b88f1 11077 indx = bfd_get_symcount (output_bfd);
ef10c3ac 11078 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 11079 if (ret == 0)
0a1b45a2 11080 return false;
6e0b88f1
AM
11081 else if (ret == 1)
11082 *pindex = indx;
c152c796
AM
11083 }
11084
310fd250
L
11085 if (bed->s->arch_size == 32)
11086 {
11087 r_type_mask = 0xff;
11088 r_sym_shift = 8;
11089 address_size = 4;
11090 }
11091 else
11092 {
11093 r_type_mask = 0xffffffff;
11094 r_sym_shift = 32;
11095 address_size = 8;
11096 }
11097
c152c796
AM
11098 /* Relocate the contents of each section. */
11099 sym_hashes = elf_sym_hashes (input_bfd);
11100 for (o = input_bfd->sections; o != NULL; o = o->next)
11101 {
11102 bfd_byte *contents;
11103
11104 if (! o->linker_mark)
11105 {
11106 /* This section was omitted from the link. */
11107 continue;
11108 }
11109
7bdf4127 11110 if (!flinfo->info->resolve_section_groups
bcacc0f5
AM
11111 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
11112 {
11113 /* Deal with the group signature symbol. */
11114 struct bfd_elf_section_data *sec_data = elf_section_data (o);
11115 unsigned long symndx = sec_data->this_hdr.sh_info;
11116 asection *osec = o->output_section;
11117
7bdf4127 11118 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
bcacc0f5
AM
11119 if (symndx >= locsymcount
11120 || (elf_bad_symtab (input_bfd)
8b127cbc 11121 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
11122 {
11123 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
11124 while (h->root.type == bfd_link_hash_indirect
11125 || h->root.type == bfd_link_hash_warning)
11126 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11127 /* Arrange for symbol to be output. */
11128 h->indx = -2;
11129 elf_section_data (osec)->this_hdr.sh_info = -2;
11130 }
11131 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
11132 {
11133 /* We'll use the output section target_index. */
8b127cbc 11134 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
11135 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
11136 }
11137 else
11138 {
8b127cbc 11139 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
11140 {
11141 /* Otherwise output the local symbol now. */
11142 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 11143 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 11144 const char *name;
6e0b88f1
AM
11145 long indx;
11146 int ret;
bcacc0f5
AM
11147
11148 name = bfd_elf_string_from_elf_section (input_bfd,
11149 symtab_hdr->sh_link,
11150 sym.st_name);
11151 if (name == NULL)
0a1b45a2 11152 return false;
bcacc0f5
AM
11153
11154 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11155 sec);
11156 if (sym.st_shndx == SHN_BAD)
0a1b45a2 11157 return false;
bcacc0f5
AM
11158
11159 sym.st_value += o->output_offset;
11160
6e0b88f1 11161 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
11162 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11163 NULL);
6e0b88f1 11164 if (ret == 0)
0a1b45a2 11165 return false;
6e0b88f1 11166 else if (ret == 1)
8b127cbc 11167 flinfo->indices[symndx] = indx;
6e0b88f1
AM
11168 else
11169 abort ();
bcacc0f5
AM
11170 }
11171 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 11172 = flinfo->indices[symndx];
bcacc0f5
AM
11173 }
11174 }
11175
c152c796 11176 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 11177 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
11178 continue;
11179
11180 if ((o->flags & SEC_LINKER_CREATED) != 0)
11181 {
11182 /* Section was created by _bfd_elf_link_create_dynamic_sections
11183 or somesuch. */
11184 continue;
11185 }
11186
11187 /* Get the contents of the section. They have been cached by a
11188 relaxation routine. Note that o is a section in an input
11189 file, so the contents field will not have been set by any of
11190 the routines which work on output files. */
11191 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
11192 {
11193 contents = elf_section_data (o)->this_hdr.contents;
11194 if (bed->caches_rawsize
11195 && o->rawsize != 0
11196 && o->rawsize < o->size)
11197 {
11198 memcpy (flinfo->contents, contents, o->rawsize);
11199 contents = flinfo->contents;
11200 }
11201 }
c152c796
AM
11202 else
11203 {
8b127cbc 11204 contents = flinfo->contents;
4a114e3e 11205 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
0a1b45a2 11206 return false;
c152c796
AM
11207 }
11208
11209 if ((o->flags & SEC_RELOC) != 0)
11210 {
11211 Elf_Internal_Rela *internal_relocs;
0f02bbd9 11212 Elf_Internal_Rela *rel, *relend;
0f02bbd9 11213 int action_discarded;
ece5ef60 11214 int ret;
c152c796
AM
11215
11216 /* Get the swapped relocs. */
11217 internal_relocs
a8dde0a2
L
11218 = _bfd_elf_link_info_read_relocs (input_bfd, flinfo->info, o,
11219 flinfo->external_relocs,
11220 flinfo->internal_relocs,
11221 false);
c152c796
AM
11222 if (internal_relocs == NULL
11223 && o->reloc_count > 0)
0a1b45a2 11224 return false;
c152c796 11225
310fd250
L
11226 /* We need to reverse-copy input .ctors/.dtors sections if
11227 they are placed in .init_array/.finit_array for output. */
11228 if (o->size > address_size
3f3328b8 11229 && ((startswith (o->name, ".ctors")
310fd250
L
11230 && strcmp (o->output_section->name,
11231 ".init_array") == 0)
3f3328b8 11232 || (startswith (o->name, ".dtors")
310fd250
L
11233 && strcmp (o->output_section->name,
11234 ".fini_array") == 0))
11235 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 11236 {
056bafd4
MR
11237 if (o->size * bed->s->int_rels_per_ext_rel
11238 != o->reloc_count * address_size)
310fd250 11239 {
4eca0228 11240 _bfd_error_handler
695344c0 11241 /* xgettext:c-format */
871b3ab2 11242 (_("error: %pB: size of section %pA is not "
310fd250
L
11243 "multiple of address size"),
11244 input_bfd, o);
8c6716e5 11245 bfd_set_error (bfd_error_bad_value);
0a1b45a2 11246 return false;
310fd250
L
11247 }
11248 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
11249 }
11250
0f02bbd9 11251 action_discarded = -1;
c152c796 11252 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
11253 action_discarded = (*bed->action_discarded) (o);
11254
11255 /* Run through the relocs evaluating complex reloc symbols and
11256 looking for relocs against symbols from discarded sections
11257 or section symbols from removed link-once sections.
11258 Complain about relocs against discarded sections. Zero
11259 relocs against removed link-once sections. */
11260
11261 rel = internal_relocs;
056bafd4 11262 relend = rel + o->reloc_count;
0f02bbd9 11263 for ( ; rel < relend; rel++)
c152c796 11264 {
0f02bbd9
AM
11265 unsigned long r_symndx = rel->r_info >> r_sym_shift;
11266 unsigned int s_type;
11267 asection **ps, *sec;
11268 struct elf_link_hash_entry *h = NULL;
11269 const char *sym_name;
c152c796 11270
0f02bbd9
AM
11271 if (r_symndx == STN_UNDEF)
11272 continue;
c152c796 11273
0f02bbd9
AM
11274 if (r_symndx >= locsymcount
11275 || (elf_bad_symtab (input_bfd)
8b127cbc 11276 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
11277 {
11278 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 11279
0f02bbd9
AM
11280 /* Badly formatted input files can contain relocs that
11281 reference non-existant symbols. Check here so that
11282 we do not seg fault. */
11283 if (h == NULL)
c152c796 11284 {
4eca0228 11285 _bfd_error_handler
695344c0 11286 /* xgettext:c-format */
2dcf00ce 11287 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
0f02bbd9 11288 "that references a non-existent global symbol"),
2dcf00ce 11289 input_bfd, (uint64_t) rel->r_info, o);
0f02bbd9 11290 bfd_set_error (bfd_error_bad_value);
0a1b45a2 11291 return false;
0f02bbd9 11292 }
3b36f7e6 11293
0f02bbd9
AM
11294 while (h->root.type == bfd_link_hash_indirect
11295 || h->root.type == bfd_link_hash_warning)
11296 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 11297
0f02bbd9 11298 s_type = h->type;
cdd3575c 11299
9e2dec47 11300 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
11301 mark the symbol as undefined. Note that the
11302 linker may attach linker created dynamic sections
11303 to the plugin bfd. Symbols defined in linker
11304 created sections are not plugin symbols. */
bc4e12de 11305 if ((h->root.non_ir_ref_regular
4070765b 11306 || h->root.non_ir_ref_dynamic)
9e2dec47
L
11307 && (h->root.type == bfd_link_hash_defined
11308 || h->root.type == bfd_link_hash_defweak)
11309 && (h->root.u.def.section->flags
11310 & SEC_LINKER_CREATED) == 0
11311 && h->root.u.def.section->owner != NULL
11312 && (h->root.u.def.section->owner->flags
11313 & BFD_PLUGIN) != 0)
11314 {
11315 h->root.type = bfd_link_hash_undefined;
11316 h->root.u.undef.abfd = h->root.u.def.section->owner;
11317 }
11318
0f02bbd9
AM
11319 ps = NULL;
11320 if (h->root.type == bfd_link_hash_defined
11321 || h->root.type == bfd_link_hash_defweak)
11322 ps = &h->root.u.def.section;
11323
11324 sym_name = h->root.root.string;
11325 }
11326 else
11327 {
11328 Elf_Internal_Sym *sym = isymbuf + r_symndx;
11329
11330 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 11331 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
11332 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11333 sym, *ps);
11334 }
c152c796 11335
c301e700 11336 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 11337 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
11338 {
11339 bfd_vma val;
11340 bfd_vma dot = (rel->r_offset
11341 + o->output_offset + o->output_section->vma);
11342#ifdef DEBUG
11343 printf ("Encountered a complex symbol!");
11344 printf (" (input_bfd %s, section %s, reloc %ld\n",
765cf5f6 11345 bfd_get_filename (input_bfd), o->name,
9ccb8af9 11346 (long) (rel - internal_relocs));
0f02bbd9
AM
11347 printf (" symbol: idx %8.8lx, name %s\n",
11348 r_symndx, sym_name);
11349 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11350 (unsigned long) rel->r_info,
11351 (unsigned long) rel->r_offset);
11352#endif
8b127cbc 11353 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9 11354 isymbuf, locsymcount, s_type == STT_SRELC))
0a1b45a2 11355 return false;
0f02bbd9
AM
11356
11357 /* Symbol evaluated OK. Update to absolute value. */
11358 set_symbol_value (input_bfd, isymbuf, locsymcount,
11359 r_symndx, val);
11360 continue;
11361 }
11362
11363 if (action_discarded != -1 && ps != NULL)
11364 {
cdd3575c
AM
11365 /* Complain if the definition comes from a
11366 discarded section. */
dbaa2011 11367 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 11368 {
cf35638d 11369 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 11370 if (action_discarded & COMPLAIN)
8b127cbc 11371 (*flinfo->info->callbacks->einfo)
695344c0 11372 /* xgettext:c-format */
871b3ab2
AM
11373 (_("%X`%s' referenced in section `%pA' of %pB: "
11374 "defined in discarded section `%pA' of %pB\n"),
e1fffbe6 11375 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 11376
87e5235d 11377 /* Try to do the best we can to support buggy old
e0ae6d6f 11378 versions of gcc. Pretend that the symbol is
87e5235d
AM
11379 really defined in the kept linkonce section.
11380 FIXME: This is quite broken. Modifying the
11381 symbol here means we will be changing all later
e0ae6d6f 11382 uses of the symbol, not just in this section. */
0f02bbd9 11383 if (action_discarded & PRETEND)
87e5235d 11384 {
01b3c8ab
L
11385 asection *kept;
11386
c0f00686 11387 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 11388 flinfo->info);
01b3c8ab 11389 if (kept != NULL)
87e5235d
AM
11390 {
11391 *ps = kept;
11392 continue;
11393 }
11394 }
c152c796
AM
11395 }
11396 }
11397 }
11398
11399 /* Relocate the section by invoking a back end routine.
11400
11401 The back end routine is responsible for adjusting the
11402 section contents as necessary, and (if using Rela relocs
11403 and generating a relocatable output file) adjusting the
11404 reloc addend as necessary.
11405
11406 The back end routine does not have to worry about setting
11407 the reloc address or the reloc symbol index.
11408
11409 The back end routine is given a pointer to the swapped in
11410 internal symbols, and can access the hash table entries
11411 for the external symbols via elf_sym_hashes (input_bfd).
11412
11413 When generating relocatable output, the back end routine
11414 must handle STB_LOCAL/STT_SECTION symbols specially. The
11415 output symbol is going to be a section symbol
11416 corresponding to the output section, which will require
11417 the addend to be adjusted. */
11418
8b127cbc 11419 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
11420 input_bfd, o, contents,
11421 internal_relocs,
11422 isymbuf,
8b127cbc 11423 flinfo->sections);
ece5ef60 11424 if (!ret)
0a1b45a2 11425 return false;
c152c796 11426
ece5ef60 11427 if (ret == 2
0e1862bb 11428 || bfd_link_relocatable (flinfo->info)
8b127cbc 11429 || flinfo->info->emitrelocations)
c152c796
AM
11430 {
11431 Elf_Internal_Rela *irela;
d4730f92 11432 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
11433 bfd_vma last_offset;
11434 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
11435 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11436 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 11437 unsigned int next_erel;
0a1b45a2 11438 bool rela_normal;
d4730f92 11439 struct bfd_elf_section_data *esdi, *esdo;
c152c796 11440
d4730f92
BS
11441 esdi = elf_section_data (o);
11442 esdo = elf_section_data (o->output_section);
0a1b45a2 11443 rela_normal = false;
c152c796
AM
11444
11445 /* Adjust the reloc addresses and symbol indices. */
11446
11447 irela = internal_relocs;
056bafd4 11448 irelaend = irela + o->reloc_count;
d4730f92
BS
11449 rel_hash = esdo->rel.hashes + esdo->rel.count;
11450 /* We start processing the REL relocs, if any. When we reach
11451 IRELAMID in the loop, we switch to the RELA relocs. */
11452 irelamid = irela;
11453 if (esdi->rel.hdr != NULL)
11454 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11455 * bed->s->int_rels_per_ext_rel);
eac338cf 11456 rel_hash_list = rel_hash;
d4730f92 11457 rela_hash_list = NULL;
c152c796 11458 last_offset = o->output_offset;
0e1862bb 11459 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11460 last_offset += o->output_section->vma;
11461 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11462 {
11463 unsigned long r_symndx;
11464 asection *sec;
11465 Elf_Internal_Sym sym;
11466
11467 if (next_erel == bed->s->int_rels_per_ext_rel)
11468 {
11469 rel_hash++;
11470 next_erel = 0;
11471 }
11472
d4730f92
BS
11473 if (irela == irelamid)
11474 {
11475 rel_hash = esdo->rela.hashes + esdo->rela.count;
11476 rela_hash_list = rel_hash;
11477 rela_normal = bed->rela_normal;
11478 }
11479
c152c796 11480 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 11481 flinfo->info, o,
c152c796
AM
11482 irela->r_offset);
11483 if (irela->r_offset >= (bfd_vma) -2)
11484 {
11485 /* This is a reloc for a deleted entry or somesuch.
11486 Turn it into an R_*_NONE reloc, at the same
11487 offset as the last reloc. elf_eh_frame.c and
e460dd0d 11488 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
11489 being ordered. */
11490 irela->r_offset = last_offset;
11491 irela->r_info = 0;
11492 irela->r_addend = 0;
11493 continue;
11494 }
11495
11496 irela->r_offset += o->output_offset;
11497
11498 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 11499 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11500 irela->r_offset += o->output_section->vma;
11501
11502 last_offset = irela->r_offset;
11503
11504 r_symndx = irela->r_info >> r_sym_shift;
11505 if (r_symndx == STN_UNDEF)
11506 continue;
11507
11508 if (r_symndx >= locsymcount
11509 || (elf_bad_symtab (input_bfd)
8b127cbc 11510 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
11511 {
11512 struct elf_link_hash_entry *rh;
11513 unsigned long indx;
11514
11515 /* This is a reloc against a global symbol. We
11516 have not yet output all the local symbols, so
11517 we do not know the symbol index of any global
11518 symbol. We set the rel_hash entry for this
11519 reloc to point to the global hash table entry
11520 for this symbol. The symbol index is then
ee75fd95 11521 set at the end of bfd_elf_final_link. */
c152c796
AM
11522 indx = r_symndx - extsymoff;
11523 rh = elf_sym_hashes (input_bfd)[indx];
11524 while (rh->root.type == bfd_link_hash_indirect
11525 || rh->root.type == bfd_link_hash_warning)
11526 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11527
11528 /* Setting the index to -2 tells
11529 elf_link_output_extsym that this symbol is
11530 used by a reloc. */
11531 BFD_ASSERT (rh->indx < 0);
11532 rh->indx = -2;
c152c796
AM
11533 *rel_hash = rh;
11534
11535 continue;
11536 }
11537
11538 /* This is a reloc against a local symbol. */
11539
11540 *rel_hash = NULL;
11541 sym = isymbuf[r_symndx];
8b127cbc 11542 sec = flinfo->sections[r_symndx];
c152c796
AM
11543 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11544 {
11545 /* I suppose the backend ought to fill in the
11546 section of any STT_SECTION symbol against a
6a8d1586 11547 processor specific section. */
cf35638d 11548 r_symndx = STN_UNDEF;
6a8d1586
AM
11549 if (bfd_is_abs_section (sec))
11550 ;
c152c796
AM
11551 else if (sec == NULL || sec->owner == NULL)
11552 {
11553 bfd_set_error (bfd_error_bad_value);
0a1b45a2 11554 return false;
c152c796
AM
11555 }
11556 else
11557 {
6a8d1586
AM
11558 asection *osec = sec->output_section;
11559
11560 /* If we have discarded a section, the output
11561 section will be the absolute section. In
ab96bf03
AM
11562 case of discarded SEC_MERGE sections, use
11563 the kept section. relocate_section should
11564 have already handled discarded linkonce
11565 sections. */
6a8d1586
AM
11566 if (bfd_is_abs_section (osec)
11567 && sec->kept_section != NULL
11568 && sec->kept_section->output_section != NULL)
11569 {
11570 osec = sec->kept_section->output_section;
11571 irela->r_addend -= osec->vma;
11572 }
11573
11574 if (!bfd_is_abs_section (osec))
11575 {
11576 r_symndx = osec->target_index;
cf35638d 11577 if (r_symndx == STN_UNDEF)
74541ad4 11578 {
051d833a
AM
11579 irela->r_addend += osec->vma;
11580 osec = _bfd_nearby_section (output_bfd, osec,
11581 osec->vma);
11582 irela->r_addend -= osec->vma;
11583 r_symndx = osec->target_index;
74541ad4 11584 }
6a8d1586 11585 }
c152c796
AM
11586 }
11587
11588 /* Adjust the addend according to where the
11589 section winds up in the output section. */
11590 if (rela_normal)
11591 irela->r_addend += sec->output_offset;
11592 }
11593 else
11594 {
8b127cbc 11595 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
11596 {
11597 unsigned long shlink;
11598 const char *name;
11599 asection *osec;
6e0b88f1 11600 long indx;
c152c796 11601
8b127cbc 11602 if (flinfo->info->strip == strip_all)
c152c796
AM
11603 {
11604 /* You can't do ld -r -s. */
11605 bfd_set_error (bfd_error_invalid_operation);
0a1b45a2 11606 return false;
c152c796
AM
11607 }
11608
11609 /* This symbol was skipped earlier, but
11610 since it is needed by a reloc, we
11611 must output it now. */
11612 shlink = symtab_hdr->sh_link;
11613 name = (bfd_elf_string_from_elf_section
11614 (input_bfd, shlink, sym.st_name));
11615 if (name == NULL)
0a1b45a2 11616 return false;
c152c796
AM
11617
11618 osec = sec->output_section;
11619 sym.st_shndx =
11620 _bfd_elf_section_from_bfd_section (output_bfd,
11621 osec);
11622 if (sym.st_shndx == SHN_BAD)
0a1b45a2 11623 return false;
c152c796
AM
11624
11625 sym.st_value += sec->output_offset;
0e1862bb 11626 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11627 {
11628 sym.st_value += osec->vma;
11629 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11630 {
102def4d
AM
11631 struct elf_link_hash_table *htab
11632 = elf_hash_table (flinfo->info);
11633
c152c796
AM
11634 /* STT_TLS symbols are relative to PT_TLS
11635 segment base. */
102def4d
AM
11636 if (htab->tls_sec != NULL)
11637 sym.st_value -= htab->tls_sec->vma;
11638 else
11639 sym.st_info
11640 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11641 STT_NOTYPE);
c152c796
AM
11642 }
11643 }
11644
6e0b88f1 11645 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
11646 ret = elf_link_output_symstrtab (flinfo, name,
11647 &sym, sec,
11648 NULL);
6e0b88f1 11649 if (ret == 0)
0a1b45a2 11650 return false;
6e0b88f1 11651 else if (ret == 1)
8b127cbc 11652 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
11653 else
11654 abort ();
c152c796
AM
11655 }
11656
8b127cbc 11657 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
11658 }
11659
11660 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11661 | (irela->r_info & r_type_mask));
11662 }
11663
11664 /* Swap out the relocs. */
d4730f92
BS
11665 input_rel_hdr = esdi->rel.hdr;
11666 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 11667 {
d4730f92
BS
11668 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11669 input_rel_hdr,
11670 internal_relocs,
11671 rel_hash_list))
0a1b45a2 11672 return false;
c152c796
AM
11673 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11674 * bed->s->int_rels_per_ext_rel);
eac338cf 11675 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
11676 }
11677
11678 input_rela_hdr = esdi->rela.hdr;
11679 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11680 {
eac338cf 11681 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 11682 input_rela_hdr,
eac338cf 11683 internal_relocs,
d4730f92 11684 rela_hash_list))
0a1b45a2 11685 return false;
c152c796
AM
11686 }
11687 }
11688 }
11689
11690 /* Write out the modified section contents. */
11691 if (bed->elf_backend_write_section
8b127cbc 11692 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 11693 contents))
c152c796
AM
11694 {
11695 /* Section written out. */
11696 }
11697 else switch (o->sec_info_type)
11698 {
dbaa2011 11699 case SEC_INFO_TYPE_STABS:
c152c796
AM
11700 if (! (_bfd_write_section_stabs
11701 (output_bfd,
8b127cbc 11702 &elf_hash_table (flinfo->info)->stab_info,
c152c796 11703 o, &elf_section_data (o)->sec_info, contents)))
0a1b45a2 11704 return false;
c152c796 11705 break;
dbaa2011 11706 case SEC_INFO_TYPE_MERGE:
c152c796
AM
11707 if (! _bfd_write_merged_section (output_bfd, o,
11708 elf_section_data (o)->sec_info))
0a1b45a2 11709 return false;
c152c796 11710 break;
dbaa2011 11711 case SEC_INFO_TYPE_EH_FRAME:
c152c796 11712 {
8b127cbc 11713 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796 11714 o, contents))
0a1b45a2 11715 return false;
c152c796
AM
11716 }
11717 break;
2f0c68f2
CM
11718 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11719 {
11720 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11721 flinfo->info,
11722 o, contents))
0a1b45a2 11723 return false;
2f0c68f2
CM
11724 }
11725 break;
c152c796
AM
11726 default:
11727 {
310fd250
L
11728 if (! (o->flags & SEC_EXCLUDE))
11729 {
11730 file_ptr offset = (file_ptr) o->output_offset;
11731 bfd_size_type todo = o->size;
37b01f6a 11732
bb294208 11733 offset *= bfd_octets_per_byte (output_bfd, o);
37b01f6a 11734
310fd250
L
11735 if ((o->flags & SEC_ELF_REVERSE_COPY))
11736 {
11737 /* Reverse-copy input section to output. */
11738 do
11739 {
11740 todo -= address_size;
11741 if (! bfd_set_section_contents (output_bfd,
11742 o->output_section,
11743 contents + todo,
11744 offset,
11745 address_size))
0a1b45a2 11746 return false;
310fd250
L
11747 if (todo == 0)
11748 break;
11749 offset += address_size;
11750 }
11751 while (1);
11752 }
11753 else if (! bfd_set_section_contents (output_bfd,
11754 o->output_section,
11755 contents,
11756 offset, todo))
0a1b45a2 11757 return false;
310fd250 11758 }
c152c796
AM
11759 }
11760 break;
11761 }
11762 }
11763
0a1b45a2 11764 return true;
c152c796
AM
11765}
11766
11767/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 11768 requested by the linker, and does not come from any input file. This
c152c796
AM
11769 is used to build constructor and destructor tables when linking
11770 with -Ur. */
11771
0a1b45a2 11772static bool
c152c796
AM
11773elf_reloc_link_order (bfd *output_bfd,
11774 struct bfd_link_info *info,
11775 asection *output_section,
11776 struct bfd_link_order *link_order)
11777{
11778 reloc_howto_type *howto;
11779 long indx;
11780 bfd_vma offset;
11781 bfd_vma addend;
d4730f92 11782 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
11783 struct elf_link_hash_entry **rel_hash_ptr;
11784 Elf_Internal_Shdr *rel_hdr;
11785 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11786 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11787 bfd_byte *erel;
11788 unsigned int i;
d4730f92 11789 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
11790
11791 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11792 if (howto == NULL)
11793 {
11794 bfd_set_error (bfd_error_bad_value);
0a1b45a2 11795 return false;
c152c796
AM
11796 }
11797
11798 addend = link_order->u.reloc.p->addend;
11799
d4730f92
BS
11800 if (esdo->rel.hdr)
11801 reldata = &esdo->rel;
11802 else if (esdo->rela.hdr)
11803 reldata = &esdo->rela;
11804 else
11805 {
11806 reldata = NULL;
11807 BFD_ASSERT (0);
11808 }
11809
c152c796 11810 /* Figure out the symbol index. */
d4730f92 11811 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
11812 if (link_order->type == bfd_section_reloc_link_order)
11813 {
11814 indx = link_order->u.reloc.p->u.section->target_index;
11815 BFD_ASSERT (indx != 0);
11816 *rel_hash_ptr = NULL;
11817 }
11818 else
11819 {
11820 struct elf_link_hash_entry *h;
11821
11822 /* Treat a reloc against a defined symbol as though it were
11823 actually against the section. */
11824 h = ((struct elf_link_hash_entry *)
11825 bfd_wrapped_link_hash_lookup (output_bfd, info,
11826 link_order->u.reloc.p->u.name,
0a1b45a2 11827 false, false, true));
c152c796
AM
11828 if (h != NULL
11829 && (h->root.type == bfd_link_hash_defined
11830 || h->root.type == bfd_link_hash_defweak))
11831 {
11832 asection *section;
11833
11834 section = h->root.u.def.section;
11835 indx = section->output_section->target_index;
11836 *rel_hash_ptr = NULL;
11837 /* It seems that we ought to add the symbol value to the
11838 addend here, but in practice it has already been added
11839 because it was passed to constructor_callback. */
11840 addend += section->output_section->vma + section->output_offset;
11841 }
11842 else if (h != NULL)
11843 {
11844 /* Setting the index to -2 tells elf_link_output_extsym that
11845 this symbol is used by a reloc. */
11846 h->indx = -2;
11847 *rel_hash_ptr = h;
11848 indx = 0;
11849 }
11850 else
11851 {
1a72702b
AM
11852 (*info->callbacks->unattached_reloc)
11853 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
c152c796
AM
11854 indx = 0;
11855 }
11856 }
11857
11858 /* If this is an inplace reloc, we must write the addend into the
11859 object file. */
11860 if (howto->partial_inplace && addend != 0)
11861 {
11862 bfd_size_type size;
11863 bfd_reloc_status_type rstat;
11864 bfd_byte *buf;
0a1b45a2 11865 bool ok;
c152c796 11866 const char *sym_name;
bb294208 11867 bfd_size_type octets;
c152c796 11868
a50b1753
NC
11869 size = (bfd_size_type) bfd_get_reloc_size (howto);
11870 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 11871 if (buf == NULL && size != 0)
0a1b45a2 11872 return false;
c152c796
AM
11873 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11874 switch (rstat)
11875 {
11876 case bfd_reloc_ok:
11877 break;
11878
11879 default:
11880 case bfd_reloc_outofrange:
11881 abort ();
11882
11883 case bfd_reloc_overflow:
11884 if (link_order->type == bfd_section_reloc_link_order)
fd361982 11885 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
c152c796
AM
11886 else
11887 sym_name = link_order->u.reloc.p->u.name;
1a72702b
AM
11888 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11889 howto->name, addend, NULL, NULL,
11890 (bfd_vma) 0);
c152c796
AM
11891 break;
11892 }
37b01f6a 11893
bb294208
AM
11894 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
11895 output_section);
c152c796 11896 ok = bfd_set_section_contents (output_bfd, output_section, buf,
bb294208 11897 octets, size);
c152c796
AM
11898 free (buf);
11899 if (! ok)
0a1b45a2 11900 return false;
c152c796
AM
11901 }
11902
11903 /* The address of a reloc is relative to the section in a
11904 relocatable file, and is a virtual address in an executable
11905 file. */
11906 offset = link_order->offset;
0e1862bb 11907 if (! bfd_link_relocatable (info))
c152c796
AM
11908 offset += output_section->vma;
11909
11910 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11911 {
11912 irel[i].r_offset = offset;
11913 irel[i].r_info = 0;
11914 irel[i].r_addend = 0;
11915 }
11916 if (bed->s->arch_size == 32)
11917 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11918 else
11919 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11920
d4730f92 11921 rel_hdr = reldata->hdr;
c152c796
AM
11922 erel = rel_hdr->contents;
11923 if (rel_hdr->sh_type == SHT_REL)
11924 {
d4730f92 11925 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
11926 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11927 }
11928 else
11929 {
11930 irel[0].r_addend = addend;
d4730f92 11931 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
11932 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11933 }
11934
d4730f92 11935 ++reldata->count;
c152c796 11936
0a1b45a2 11937 return true;
c152c796
AM
11938}
11939
76359541
TP
11940/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11941 Returns TRUE upon success, FALSE otherwise. */
11942
0a1b45a2 11943static bool
76359541
TP
11944elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11945{
0a1b45a2 11946 bool ret = false;
76359541
TP
11947 bfd *implib_bfd;
11948 const struct elf_backend_data *bed;
11949 flagword flags;
11950 enum bfd_architecture arch;
11951 unsigned int mach;
11952 asymbol **sympp = NULL;
11953 long symsize;
11954 long symcount;
11955 long src_count;
11956 elf_symbol_type *osymbuf;
446f7ed5 11957 size_t amt;
76359541
TP
11958
11959 implib_bfd = info->out_implib_bfd;
11960 bed = get_elf_backend_data (abfd);
11961
11962 if (!bfd_set_format (implib_bfd, bfd_object))
0a1b45a2 11963 return false;
76359541 11964
046734ff 11965 /* Use flag from executable but make it a relocatable object. */
76359541
TP
11966 flags = bfd_get_file_flags (abfd);
11967 flags &= ~HAS_RELOC;
11968 if (!bfd_set_start_address (implib_bfd, 0)
046734ff 11969 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
0a1b45a2 11970 return false;
76359541
TP
11971
11972 /* Copy architecture of output file to import library file. */
11973 arch = bfd_get_arch (abfd);
11974 mach = bfd_get_mach (abfd);
11975 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11976 && (abfd->target_defaulted
11977 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
0a1b45a2 11978 return false;
76359541
TP
11979
11980 /* Get symbol table size. */
11981 symsize = bfd_get_symtab_upper_bound (abfd);
11982 if (symsize < 0)
0a1b45a2 11983 return false;
76359541
TP
11984
11985 /* Read in the symbol table. */
ec9bd0a2
AM
11986 sympp = (asymbol **) bfd_malloc (symsize);
11987 if (sympp == NULL)
0a1b45a2 11988 return false;
ec9bd0a2 11989
76359541
TP
11990 symcount = bfd_canonicalize_symtab (abfd, sympp);
11991 if (symcount < 0)
11992 goto free_sym_buf;
11993
11994 /* Allow the BFD backend to copy any private header data it
11995 understands from the output BFD to the import library BFD. */
11996 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11997 goto free_sym_buf;
11998
11999 /* Filter symbols to appear in the import library. */
12000 if (bed->elf_backend_filter_implib_symbols)
12001 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
12002 symcount);
12003 else
12004 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
12005 if (symcount == 0)
12006 {
5df1bc57 12007 bfd_set_error (bfd_error_no_symbols);
871b3ab2 12008 _bfd_error_handler (_("%pB: no symbol found for import library"),
4eca0228 12009 implib_bfd);
76359541
TP
12010 goto free_sym_buf;
12011 }
12012
12013
12014 /* Make symbols absolute. */
446f7ed5
AM
12015 amt = symcount * sizeof (*osymbuf);
12016 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
ec9bd0a2
AM
12017 if (osymbuf == NULL)
12018 goto free_sym_buf;
12019
76359541
TP
12020 for (src_count = 0; src_count < symcount; src_count++)
12021 {
12022 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
12023 sizeof (*osymbuf));
12024 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
12025 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
12026 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
12027 osymbuf[src_count].internal_elf_sym.st_value =
12028 osymbuf[src_count].symbol.value;
12029 sympp[src_count] = &osymbuf[src_count].symbol;
12030 }
12031
12032 bfd_set_symtab (implib_bfd, sympp, symcount);
12033
12034 /* Allow the BFD backend to copy any private data it understands
12035 from the output BFD to the import library BFD. This is done last
12036 to permit the routine to look at the filtered symbol table. */
12037 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
12038 goto free_sym_buf;
12039
12040 if (!bfd_close (implib_bfd))
12041 goto free_sym_buf;
12042
0a1b45a2 12043 ret = true;
76359541 12044
dc1e8a47 12045 free_sym_buf:
76359541
TP
12046 free (sympp);
12047 return ret;
12048}
12049
9f7c3e5e
AM
12050static void
12051elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
12052{
12053 asection *o;
12054
12055 if (flinfo->symstrtab != NULL)
ef10c3ac 12056 _bfd_elf_strtab_free (flinfo->symstrtab);
c9594989
AM
12057 free (flinfo->contents);
12058 free (flinfo->external_relocs);
12059 free (flinfo->internal_relocs);
12060 free (flinfo->external_syms);
12061 free (flinfo->locsym_shndx);
12062 free (flinfo->internal_syms);
12063 free (flinfo->indices);
12064 free (flinfo->sections);
12065 if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
9f7c3e5e
AM
12066 free (flinfo->symshndxbuf);
12067 for (o = obfd->sections; o != NULL; o = o->next)
12068 {
12069 struct bfd_elf_section_data *esdo = elf_section_data (o);
c9594989
AM
12070 free (esdo->rel.hashes);
12071 free (esdo->rela.hashes);
9f7c3e5e
AM
12072 }
12073}
0b52efa6 12074
c152c796
AM
12075/* Do the final step of an ELF link. */
12076
0a1b45a2 12077bool
c152c796
AM
12078bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
12079{
0a1b45a2
AM
12080 bool dynamic;
12081 bool emit_relocs;
c152c796 12082 bfd *dynobj;
8b127cbc 12083 struct elf_final_link_info flinfo;
91d6fa6a
NC
12084 asection *o;
12085 struct bfd_link_order *p;
12086 bfd *sub;
c152c796
AM
12087 bfd_size_type max_contents_size;
12088 bfd_size_type max_external_reloc_size;
12089 bfd_size_type max_internal_reloc_count;
12090 bfd_size_type max_sym_count;
12091 bfd_size_type max_sym_shndx_count;
c152c796
AM
12092 Elf_Internal_Sym elfsym;
12093 unsigned int i;
12094 Elf_Internal_Shdr *symtab_hdr;
12095 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
12096 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12097 struct elf_outext_info eoinfo;
0a1b45a2 12098 bool merged;
c152c796
AM
12099 size_t relativecount = 0;
12100 asection *reldyn = 0;
12101 bfd_size_type amt;
104d59d1
JM
12102 asection *attr_section = NULL;
12103 bfd_vma attr_size = 0;
12104 const char *std_attrs_section;
64f52338 12105 struct elf_link_hash_table *htab = elf_hash_table (info);
0a1b45a2
AM
12106 bool sections_removed;
12107 bool ret;
c152c796 12108
2cc15b10 12109 if (!is_elf_hash_table (&htab->root))
0a1b45a2 12110 return false;
c152c796 12111
0e1862bb 12112 if (bfd_link_pic (info))
c152c796
AM
12113 abfd->flags |= DYNAMIC;
12114
64f52338
AM
12115 dynamic = htab->dynamic_sections_created;
12116 dynobj = htab->dynobj;
c152c796 12117
0e1862bb 12118 emit_relocs = (bfd_link_relocatable (info)
a4676736 12119 || info->emitrelocations);
c152c796 12120
496afd17 12121 memset (&flinfo, 0, sizeof (flinfo));
8b127cbc
AM
12122 flinfo.info = info;
12123 flinfo.output_bfd = abfd;
ef10c3ac 12124 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 12125 if (flinfo.symstrtab == NULL)
0a1b45a2 12126 return false;
c152c796
AM
12127
12128 if (! dynamic)
12129 {
8b127cbc
AM
12130 flinfo.hash_sec = NULL;
12131 flinfo.symver_sec = NULL;
c152c796
AM
12132 }
12133 else
12134 {
3d4d4302 12135 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 12136 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 12137 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
12138 /* Note that it is OK if symver_sec is NULL. */
12139 }
12140
496afd17
L
12141 if (info->unique_symbol
12142 && !bfd_hash_table_init (&flinfo.local_hash_table,
12143 local_hash_newfunc,
12144 sizeof (struct local_hash_entry)))
0a1b45a2 12145 return false;
c152c796 12146
104d59d1
JM
12147 /* The object attributes have been merged. Remove the input
12148 sections from the link, and set the contents of the output
1ff6de03 12149 section. */
0a1b45a2 12150 sections_removed = false;
104d59d1
JM
12151 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
12152 for (o = abfd->sections; o != NULL; o = o->next)
12153 {
0a1b45a2 12154 bool remove_section = false;
b8a6ced7 12155
104d59d1
JM
12156 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
12157 || strcmp (o->name, ".gnu.attributes") == 0)
12158 {
12159 for (p = o->map_head.link_order; p != NULL; p = p->next)
12160 {
12161 asection *input_section;
12162
12163 if (p->type != bfd_indirect_link_order)
12164 continue;
12165 input_section = p->u.indirect.section;
12166 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12167 elf_link_input_bfd ignores this section. */
12168 input_section->flags &= ~SEC_HAS_CONTENTS;
12169 }
a0c8462f 12170
104d59d1 12171 attr_size = bfd_elf_obj_attr_size (abfd);
fd361982 12172 bfd_set_section_size (o, attr_size);
b8a6ced7
AM
12173 /* Skip this section later on. */
12174 o->map_head.link_order = NULL;
104d59d1 12175 if (attr_size)
b8a6ced7 12176 attr_section = o;
104d59d1 12177 else
0a1b45a2 12178 remove_section = true;
104d59d1 12179 }
6e5e9d58
AM
12180 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12181 {
12182 /* Remove empty group section from linker output. */
0a1b45a2 12183 remove_section = true;
b8a6ced7 12184 }
5270eddc 12185 if (remove_section)
b8a6ced7 12186 {
6e5e9d58
AM
12187 o->flags |= SEC_EXCLUDE;
12188 bfd_section_list_remove (abfd, o);
12189 abfd->section_count--;
0a1b45a2 12190 sections_removed = true;
6e5e9d58 12191 }
104d59d1 12192 }
4c6ee646
AM
12193 if (sections_removed)
12194 _bfd_fix_excluded_sec_syms (abfd, info);
104d59d1 12195
c152c796
AM
12196 /* Count up the number of relocations we will output for each output
12197 section, so that we know the sizes of the reloc sections. We
12198 also figure out some maximum sizes. */
12199 max_contents_size = 0;
12200 max_external_reloc_size = 0;
12201 max_internal_reloc_count = 0;
12202 max_sym_count = 0;
12203 max_sym_shndx_count = 0;
0a1b45a2 12204 merged = false;
c152c796
AM
12205 for (o = abfd->sections; o != NULL; o = o->next)
12206 {
12207 struct bfd_elf_section_data *esdo = elf_section_data (o);
12208 o->reloc_count = 0;
12209
8423293d 12210 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
12211 {
12212 unsigned int reloc_count = 0;
9eaff861 12213 unsigned int additional_reloc_count = 0;
c152c796 12214 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
12215
12216 if (p->type == bfd_section_reloc_link_order
12217 || p->type == bfd_symbol_reloc_link_order)
12218 reloc_count = 1;
12219 else if (p->type == bfd_indirect_link_order)
12220 {
12221 asection *sec;
12222
12223 sec = p->u.indirect.section;
c152c796
AM
12224
12225 /* Mark all sections which are to be included in the
12226 link. This will normally be every section. We need
12227 to do this so that we can identify any sections which
12228 the linker has decided to not include. */
0a1b45a2 12229 sec->linker_mark = true;
c152c796
AM
12230
12231 if (sec->flags & SEC_MERGE)
0a1b45a2 12232 merged = true;
c152c796 12233
eea6121a
AM
12234 if (sec->rawsize > max_contents_size)
12235 max_contents_size = sec->rawsize;
12236 if (sec->size > max_contents_size)
12237 max_contents_size = sec->size;
c152c796 12238
c152c796
AM
12239 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12240 && (sec->owner->flags & DYNAMIC) == 0)
12241 {
12242 size_t sym_count;
12243
a961cdd5
AM
12244 /* We are interested in just local symbols, not all
12245 symbols. */
c152c796
AM
12246 if (elf_bad_symtab (sec->owner))
12247 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12248 / bed->s->sizeof_sym);
12249 else
12250 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12251
12252 if (sym_count > max_sym_count)
12253 max_sym_count = sym_count;
12254
12255 if (sym_count > max_sym_shndx_count
6a40cf0c 12256 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
12257 max_sym_shndx_count = sym_count;
12258
a961cdd5
AM
12259 if (esdo->this_hdr.sh_type == SHT_REL
12260 || esdo->this_hdr.sh_type == SHT_RELA)
12261 /* Some backends use reloc_count in relocation sections
12262 to count particular types of relocs. Of course,
12263 reloc sections themselves can't have relocations. */
12264 ;
12265 else if (emit_relocs)
12266 {
12267 reloc_count = sec->reloc_count;
12268 if (bed->elf_backend_count_additional_relocs)
12269 {
12270 int c;
12271 c = (*bed->elf_backend_count_additional_relocs) (sec);
12272 additional_reloc_count += c;
12273 }
12274 }
12275 else if (bed->elf_backend_count_relocs)
12276 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12277
12278 esdi = elf_section_data (sec);
12279
c152c796
AM
12280 if ((sec->flags & SEC_RELOC) != 0)
12281 {
d4730f92 12282 size_t ext_size = 0;
c152c796 12283
d4730f92
BS
12284 if (esdi->rel.hdr != NULL)
12285 ext_size = esdi->rel.hdr->sh_size;
12286 if (esdi->rela.hdr != NULL)
12287 ext_size += esdi->rela.hdr->sh_size;
7326c758 12288
c152c796
AM
12289 if (ext_size > max_external_reloc_size)
12290 max_external_reloc_size = ext_size;
12291 if (sec->reloc_count > max_internal_reloc_count)
12292 max_internal_reloc_count = sec->reloc_count;
12293 }
12294 }
12295 }
12296
12297 if (reloc_count == 0)
12298 continue;
12299
9eaff861 12300 reloc_count += additional_reloc_count;
c152c796
AM
12301 o->reloc_count += reloc_count;
12302
0e1862bb 12303 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 12304 {
d4730f92 12305 if (esdi->rel.hdr)
9eaff861 12306 {
491d01d3 12307 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
9eaff861
AO
12308 esdo->rel.count += additional_reloc_count;
12309 }
d4730f92 12310 if (esdi->rela.hdr)
9eaff861 12311 {
491d01d3 12312 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
9eaff861
AO
12313 esdo->rela.count += additional_reloc_count;
12314 }
d4730f92
BS
12315 }
12316 else
12317 {
12318 if (o->use_rela_p)
12319 esdo->rela.count += reloc_count;
2c2b4ed4 12320 else
d4730f92 12321 esdo->rel.count += reloc_count;
c152c796 12322 }
c152c796
AM
12323 }
12324
9eaff861 12325 if (o->reloc_count > 0)
c152c796
AM
12326 o->flags |= SEC_RELOC;
12327 else
12328 {
12329 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12330 set it (this is probably a bug) and if it is set
12331 assign_section_numbers will create a reloc section. */
12332 o->flags &=~ SEC_RELOC;
12333 }
12334
12335 /* If the SEC_ALLOC flag is not set, force the section VMA to
12336 zero. This is done in elf_fake_sections as well, but forcing
12337 the VMA to 0 here will ensure that relocs against these
12338 sections are handled correctly. */
12339 if ((o->flags & SEC_ALLOC) == 0
12340 && ! o->user_set_vma)
12341 o->vma = 0;
12342 }
12343
0e1862bb 12344 if (! bfd_link_relocatable (info) && merged)
64f52338 12345 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
c152c796
AM
12346
12347 /* Figure out the file positions for everything but the symbol table
12348 and the relocs. We set symcount to force assign_section_numbers
12349 to create a symbol table. */
ed48ec2e 12350 abfd->symcount = info->strip != strip_all || emit_relocs;
c152c796
AM
12351 BFD_ASSERT (! abfd->output_has_begun);
12352 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12353 goto error_return;
12354
ee75fd95 12355 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
12356 for (o = abfd->sections; o != NULL; o = o->next)
12357 {
d4730f92 12358 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
12359 if ((o->flags & SEC_RELOC) != 0)
12360 {
d4730f92 12361 if (esdo->rel.hdr
9eaff861 12362 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
12363 goto error_return;
12364
d4730f92 12365 if (esdo->rela.hdr
9eaff861 12366 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
12367 goto error_return;
12368 }
12369
48db3297
AM
12370 /* _bfd_elf_compute_section_file_positions makes temporary use
12371 of target_index. Reset it. */
12372 o->target_index = 0;
12373
c152c796
AM
12374 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12375 to count upwards while actually outputting the relocations. */
d4730f92
BS
12376 esdo->rel.count = 0;
12377 esdo->rela.count = 0;
0ce398f1 12378
1ff6de03
NA
12379 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12380 && !bfd_section_is_ctf (o))
0ce398f1
L
12381 {
12382 /* Cache the section contents so that they can be compressed
12383 later. Use bfd_malloc since it will be freed by
12384 bfd_compress_section_contents. */
12385 unsigned char *contents = esdo->this_hdr.contents;
12386 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
12387 abort ();
12388 contents
12389 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12390 if (contents == NULL)
12391 goto error_return;
12392 esdo->this_hdr.contents = contents;
12393 }
c152c796
AM
12394 }
12395
1ff6de03
NA
12396 /* We have now assigned file positions for all the sections except .symtab,
12397 .strtab, and non-loaded reloc and compressed debugging sections. We start
12398 the .symtab section at the current file position, and write directly to it.
12399 We build the .strtab section in memory. */
ed48ec2e 12400 abfd->symcount = 0;
c152c796
AM
12401 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12402 /* sh_name is set in prep_headers. */
12403 symtab_hdr->sh_type = SHT_SYMTAB;
12404 /* sh_flags, sh_addr and sh_size all start off zero. */
12405 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12406 /* sh_link is set in assign_section_numbers. */
12407 /* sh_info is set below. */
12408 /* sh_offset is set just below. */
72de5009 12409 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 12410
ef10c3ac
L
12411 if (max_sym_count < 20)
12412 max_sym_count = 20;
64f52338 12413 htab->strtabsize = max_sym_count;
ef10c3ac 12414 amt = max_sym_count * sizeof (struct elf_sym_strtab);
64f52338
AM
12415 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12416 if (htab->strtab == NULL)
c152c796 12417 goto error_return;
ef10c3ac
L
12418 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12419 flinfo.symshndxbuf
12420 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12421 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 12422
8539e4e8 12423 if (info->strip != strip_all || emit_relocs)
c152c796 12424 {
8539e4e8
AM
12425 file_ptr off = elf_next_file_pos (abfd);
12426
0a1b45a2 12427 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true);
8539e4e8
AM
12428
12429 /* Note that at this point elf_next_file_pos (abfd) is
12430 incorrect. We do not yet know the size of the .symtab section.
12431 We correct next_file_pos below, after we do know the size. */
12432
12433 /* Start writing out the symbol table. The first symbol is always a
12434 dummy symbol. */
c152c796
AM
12435 elfsym.st_value = 0;
12436 elfsym.st_size = 0;
12437 elfsym.st_info = 0;
12438 elfsym.st_other = 0;
12439 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 12440 elfsym.st_target_internal = 0;
ef10c3ac
L
12441 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12442 bfd_und_section_ptr, NULL) != 1)
c152c796 12443 goto error_return;
c152c796 12444
d1bcae83
L
12445 /* Output a symbol for each section if asked or they are used for
12446 relocs. These symbols usually have no names. We store the
12447 index of each one in the index field of the section, so that
12448 we can find it again when outputting relocs. */
c77cb2a0 12449
d1bcae83 12450 if (bfd_keep_unused_section_symbols (abfd) || emit_relocs)
c152c796 12451 {
0a1b45a2 12452 bool name_local_sections
d1bcae83
L
12453 = (bed->elf_backend_name_local_section_symbols
12454 && bed->elf_backend_name_local_section_symbols (abfd));
12455 const char *name = NULL;
12456
12457 elfsym.st_size = 0;
12458 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12459 elfsym.st_other = 0;
12460 elfsym.st_value = 0;
12461 elfsym.st_target_internal = 0;
12462 for (i = 1; i < elf_numsections (abfd); i++)
f0b5bb34 12463 {
d1bcae83
L
12464 o = bfd_section_from_elf_index (abfd, i);
12465 if (o != NULL)
12466 {
12467 o->target_index = bfd_get_symcount (abfd);
12468 elfsym.st_shndx = i;
12469 if (!bfd_link_relocatable (info))
12470 elfsym.st_value = o->vma;
12471 if (name_local_sections)
12472 name = o->name;
12473 if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
12474 NULL) != 1)
12475 goto error_return;
12476 }
f0b5bb34 12477 }
c152c796
AM
12478 }
12479 }
12480
3f1b17bb
MR
12481 /* On some targets like Irix 5 the symbol split between local and global
12482 ones recorded in the sh_info field needs to be done between section
12483 and all other symbols. */
12484 if (bed->elf_backend_elfsym_local_is_section
12485 && bed->elf_backend_elfsym_local_is_section (abfd))
12486 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12487
c152c796
AM
12488 /* Allocate some memory to hold information read in from the input
12489 files. */
12490 if (max_contents_size != 0)
12491 {
8b127cbc
AM
12492 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12493 if (flinfo.contents == NULL)
c152c796
AM
12494 goto error_return;
12495 }
12496
12497 if (max_external_reloc_size != 0)
12498 {
8b127cbc
AM
12499 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12500 if (flinfo.external_relocs == NULL)
c152c796
AM
12501 goto error_return;
12502 }
12503
12504 if (max_internal_reloc_count != 0)
12505 {
056bafd4 12506 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
8b127cbc
AM
12507 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12508 if (flinfo.internal_relocs == NULL)
c152c796
AM
12509 goto error_return;
12510 }
12511
12512 if (max_sym_count != 0)
12513 {
12514 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
12515 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12516 if (flinfo.external_syms == NULL)
c152c796
AM
12517 goto error_return;
12518
12519 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
12520 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12521 if (flinfo.internal_syms == NULL)
c152c796
AM
12522 goto error_return;
12523
12524 amt = max_sym_count * sizeof (long);
8b127cbc
AM
12525 flinfo.indices = (long int *) bfd_malloc (amt);
12526 if (flinfo.indices == NULL)
c152c796
AM
12527 goto error_return;
12528
12529 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
12530 flinfo.sections = (asection **) bfd_malloc (amt);
12531 if (flinfo.sections == NULL)
c152c796
AM
12532 goto error_return;
12533 }
12534
12535 if (max_sym_shndx_count != 0)
12536 {
12537 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
12538 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12539 if (flinfo.locsym_shndx == NULL)
c152c796
AM
12540 goto error_return;
12541 }
12542
64f52338 12543 if (htab->tls_sec)
c152c796 12544 {
66631823 12545 bfd_vma base, end = 0; /* Both bytes. */
c152c796
AM
12546 asection *sec;
12547
64f52338 12548 for (sec = htab->tls_sec;
c152c796
AM
12549 sec && (sec->flags & SEC_THREAD_LOCAL);
12550 sec = sec->next)
12551 {
3a800eb9 12552 bfd_size_type size = sec->size;
66631823 12553 unsigned int opb = bfd_octets_per_byte (abfd, sec);
c152c796 12554
3a800eb9
AM
12555 if (size == 0
12556 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 12557 {
91d6fa6a
NC
12558 struct bfd_link_order *ord = sec->map_tail.link_order;
12559
12560 if (ord != NULL)
66631823 12561 size = ord->offset * opb + ord->size;
c152c796 12562 }
66631823 12563 end = sec->vma + size / opb;
c152c796 12564 }
64f52338 12565 base = htab->tls_sec->vma;
7dc98aea
RO
12566 /* Only align end of TLS section if static TLS doesn't have special
12567 alignment requirements. */
12568 if (bed->static_tls_alignment == 1)
64f52338
AM
12569 end = align_power (end, htab->tls_sec->alignment_power);
12570 htab->tls_size = end - base;
c152c796
AM
12571 }
12572
2f0c68f2 12573 if (!_bfd_elf_fixup_eh_frame_hdr (info))
0a1b45a2 12574 return false;
2f0c68f2 12575
c152c796
AM
12576 /* Since ELF permits relocations to be against local symbols, we
12577 must have the local symbols available when we do the relocations.
12578 Since we would rather only read the local symbols once, and we
12579 would rather not keep them in memory, we handle all the
12580 relocations for a single input file at the same time.
12581
12582 Unfortunately, there is no way to know the total number of local
12583 symbols until we have seen all of them, and the local symbol
12584 indices precede the global symbol indices. This means that when
12585 we are generating relocatable output, and we see a reloc against
12586 a global symbol, we can not know the symbol index until we have
12587 finished examining all the local symbols to see which ones we are
12588 going to output. To deal with this, we keep the relocations in
12589 memory, and don't output them until the end of the link. This is
12590 an unfortunate waste of memory, but I don't see a good way around
12591 it. Fortunately, it only happens when performing a relocatable
12592 link, which is not the common case. FIXME: If keep_memory is set
12593 we could write the relocs out and then read them again; I don't
12594 know how bad the memory loss will be. */
12595
c72f2fb2 12596 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
0a1b45a2 12597 sub->output_has_begun = false;
c152c796
AM
12598 for (o = abfd->sections; o != NULL; o = o->next)
12599 {
8423293d 12600 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
12601 {
12602 if (p->type == bfd_indirect_link_order
12603 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12604 == bfd_target_elf_flavour)
12605 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12606 {
12607 if (! sub->output_has_begun)
12608 {
8b127cbc 12609 if (! elf_link_input_bfd (&flinfo, sub))
c152c796 12610 goto error_return;
0a1b45a2 12611 sub->output_has_begun = true;
c152c796
AM
12612 }
12613 }
12614 else if (p->type == bfd_section_reloc_link_order
12615 || p->type == bfd_symbol_reloc_link_order)
12616 {
12617 if (! elf_reloc_link_order (abfd, info, o, p))
12618 goto error_return;
12619 }
12620 else
12621 {
12622 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
12623 {
12624 if (p->type == bfd_indirect_link_order
12625 && (bfd_get_flavour (sub)
12626 == bfd_target_elf_flavour)
12627 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12628 != bed->s->elfclass))
12629 {
12630 const char *iclass, *oclass;
12631
aebf9be7 12632 switch (bed->s->elfclass)
351f65ca 12633 {
aebf9be7
NC
12634 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12635 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12636 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12637 default: abort ();
351f65ca 12638 }
aebf9be7
NC
12639
12640 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 12641 {
aebf9be7
NC
12642 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12643 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12644 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12645 default: abort ();
351f65ca
L
12646 }
12647
12648 bfd_set_error (bfd_error_wrong_format);
4eca0228 12649 _bfd_error_handler
695344c0 12650 /* xgettext:c-format */
871b3ab2 12651 (_("%pB: file class %s incompatible with %s"),
351f65ca
L
12652 sub, iclass, oclass);
12653 }
12654
12655 goto error_return;
12656 }
c152c796
AM
12657 }
12658 }
12659 }
12660
c0f00686
L
12661 /* Free symbol buffer if needed. */
12662 if (!info->reduce_memory_overheads)
12663 {
c72f2fb2 12664 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c9594989 12665 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
c0f00686
L
12666 {
12667 free (elf_tdata (sub)->symbuf);
12668 elf_tdata (sub)->symbuf = NULL;
12669 }
12670 }
12671
0a1b45a2 12672 ret = true;
496afd17 12673
c152c796
AM
12674 /* Output any global symbols that got converted to local in a
12675 version script or due to symbol visibility. We do this in a
12676 separate step since ELF requires all local symbols to appear
12677 prior to any global symbols. FIXME: We should only do this if
12678 some global symbols were, in fact, converted to become local.
12679 FIXME: Will this work correctly with the Irix 5 linker? */
0a1b45a2 12680 eoinfo.failed = false;
8b127cbc 12681 eoinfo.flinfo = &flinfo;
0a1b45a2
AM
12682 eoinfo.localsyms = true;
12683 eoinfo.file_sym_done = false;
7686d77d 12684 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796 12685 if (eoinfo.failed)
496afd17 12686 {
0a1b45a2 12687 ret = false;
496afd17
L
12688 goto return_local_hash_table;
12689 }
c152c796 12690
4e617b1e
PB
12691 /* If backend needs to output some local symbols not present in the hash
12692 table, do it now. */
8539e4e8
AM
12693 if (bed->elf_backend_output_arch_local_syms
12694 && (info->strip != strip_all || emit_relocs))
4e617b1e 12695 {
4e617b1e 12696 if (! ((*bed->elf_backend_output_arch_local_syms)
37bb890f 12697 (abfd, info, &flinfo, elf_link_output_symstrtab)))
496afd17 12698 {
0a1b45a2 12699 ret = false;
496afd17
L
12700 goto return_local_hash_table;
12701 }
4e617b1e
PB
12702 }
12703
c152c796
AM
12704 /* That wrote out all the local symbols. Finish up the symbol table
12705 with the global symbols. Even if we want to strip everything we
12706 can, we still need to deal with those global symbols that got
12707 converted to local in a version script. */
12708
12709 /* The sh_info field records the index of the first non local symbol. */
3f1b17bb
MR
12710 if (!symtab_hdr->sh_info)
12711 symtab_hdr->sh_info = bfd_get_symcount (abfd);
c152c796
AM
12712
12713 if (dynamic
64f52338
AM
12714 && htab->dynsym != NULL
12715 && htab->dynsym->output_section != bfd_abs_section_ptr)
c152c796
AM
12716 {
12717 Elf_Internal_Sym sym;
64f52338 12718 bfd_byte *dynsym = htab->dynsym->contents;
90ac2420 12719
64f52338
AM
12720 o = htab->dynsym->output_section;
12721 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
c152c796
AM
12722
12723 /* Write out the section symbols for the output sections. */
0e1862bb 12724 if (bfd_link_pic (info)
64f52338 12725 || htab->is_relocatable_executable)
c152c796
AM
12726 {
12727 asection *s;
12728
12729 sym.st_size = 0;
12730 sym.st_name = 0;
12731 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12732 sym.st_other = 0;
35fc36a8 12733 sym.st_target_internal = 0;
c152c796
AM
12734
12735 for (s = abfd->sections; s != NULL; s = s->next)
12736 {
12737 int indx;
12738 bfd_byte *dest;
12739 long dynindx;
12740
c152c796 12741 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
12742 if (dynindx <= 0)
12743 continue;
12744 indx = elf_section_data (s)->this_idx;
c152c796
AM
12745 BFD_ASSERT (indx > 0);
12746 sym.st_shndx = indx;
c0d5a53d 12747 if (! check_dynsym (abfd, &sym))
496afd17 12748 {
0a1b45a2 12749 ret = false;
496afd17
L
12750 goto return_local_hash_table;
12751 }
c152c796
AM
12752 sym.st_value = s->vma;
12753 dest = dynsym + dynindx * bed->s->sizeof_sym;
3d16b64e
NA
12754
12755 /* Inform the linker of the addition of this symbol. */
12756
12757 if (info->callbacks->ctf_new_dynsym)
12758 info->callbacks->ctf_new_dynsym (dynindx, &sym);
12759
c152c796
AM
12760 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12761 }
c152c796
AM
12762 }
12763
12764 /* Write out the local dynsyms. */
64f52338 12765 if (htab->dynlocal)
c152c796
AM
12766 {
12767 struct elf_link_local_dynamic_entry *e;
64f52338 12768 for (e = htab->dynlocal; e ; e = e->next)
c152c796
AM
12769 {
12770 asection *s;
12771 bfd_byte *dest;
12772
935bd1e0 12773 /* Copy the internal symbol and turn off visibility.
c152c796
AM
12774 Note that we saved a word of storage and overwrote
12775 the original st_name with the dynstr_index. */
12776 sym = e->isym;
935bd1e0 12777 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
4d68fd75 12778 sym.st_shndx = SHN_UNDEF;
c152c796 12779
cb33740c
AM
12780 s = bfd_section_from_elf_index (e->input_bfd,
12781 e->isym.st_shndx);
4d68fd75
AM
12782 if (s != NULL
12783 && s->output_section != NULL
12784 && elf_section_data (s->output_section) != NULL)
c152c796 12785 {
c152c796
AM
12786 sym.st_shndx =
12787 elf_section_data (s->output_section)->this_idx;
c0d5a53d 12788 if (! check_dynsym (abfd, &sym))
496afd17 12789 {
0a1b45a2 12790 ret = false;
496afd17
L
12791 goto return_local_hash_table;
12792 }
c152c796
AM
12793 sym.st_value = (s->output_section->vma
12794 + s->output_offset
12795 + e->isym.st_value);
12796 }
12797
3d16b64e
NA
12798 /* Inform the linker of the addition of this symbol. */
12799
12800 if (info->callbacks->ctf_new_dynsym)
12801 info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
12802
c152c796
AM
12803 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12804 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12805 }
12806 }
c152c796
AM
12807 }
12808
12809 /* We get the global symbols from the hash table. */
0a1b45a2
AM
12810 eoinfo.failed = false;
12811 eoinfo.localsyms = false;
8b127cbc 12812 eoinfo.flinfo = &flinfo;
7686d77d 12813 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796 12814 if (eoinfo.failed)
496afd17 12815 {
0a1b45a2 12816 ret = false;
496afd17
L
12817 goto return_local_hash_table;
12818 }
c152c796
AM
12819
12820 /* If backend needs to output some symbols not present in the hash
12821 table, do it now. */
8539e4e8
AM
12822 if (bed->elf_backend_output_arch_syms
12823 && (info->strip != strip_all || emit_relocs))
c152c796 12824 {
c152c796 12825 if (! ((*bed->elf_backend_output_arch_syms)
37bb890f 12826 (abfd, info, &flinfo, elf_link_output_symstrtab)))
496afd17 12827 {
0a1b45a2 12828 ret = false;
496afd17
L
12829 goto return_local_hash_table;
12830 }
c152c796
AM
12831 }
12832
ef10c3ac
L
12833 /* Finalize the .strtab section. */
12834 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12835
12836 /* Swap out the .strtab section. */
12837 if (!elf_link_swap_symbols_out (&flinfo))
496afd17 12838 {
0a1b45a2 12839 ret = false;
496afd17
L
12840 goto return_local_hash_table;
12841 }
c152c796
AM
12842
12843 /* Now we know the size of the symtab section. */
c152c796
AM
12844 if (bfd_get_symcount (abfd) > 0)
12845 {
ee3b52e9
L
12846 /* Finish up and write out the symbol string table (.strtab)
12847 section. */
ad32986f 12848 Elf_Internal_Shdr *symstrtab_hdr = NULL;
8539e4e8
AM
12849 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12850
ad32986f 12851 if (elf_symtab_shndx_list (abfd))
8539e4e8 12852 {
ad32986f 12853 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8539e4e8 12854
ad32986f
NC
12855 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12856 {
12857 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12858 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12859 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12860 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12861 symtab_shndx_hdr->sh_size = amt;
8539e4e8 12862
ad32986f 12863 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
0a1b45a2 12864 off, true);
ad32986f
NC
12865
12866 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12867 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
496afd17 12868 {
0a1b45a2 12869 ret = false;
496afd17
L
12870 goto return_local_hash_table;
12871 }
ad32986f 12872 }
8539e4e8 12873 }
ee3b52e9
L
12874
12875 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12876 /* sh_name was set in prep_headers. */
12877 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 12878 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 12879 symstrtab_hdr->sh_addr = 0;
ef10c3ac 12880 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
12881 symstrtab_hdr->sh_entsize = 0;
12882 symstrtab_hdr->sh_link = 0;
12883 symstrtab_hdr->sh_info = 0;
12884 /* sh_offset is set just below. */
12885 symstrtab_hdr->sh_addralign = 1;
12886
12887 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
0a1b45a2 12888 off, true);
ee3b52e9
L
12889 elf_next_file_pos (abfd) = off;
12890
c152c796 12891 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 12892 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
496afd17 12893 {
0a1b45a2 12894 ret = false;
496afd17
L
12895 goto return_local_hash_table;
12896 }
c152c796
AM
12897 }
12898
76359541
TP
12899 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12900 {
871b3ab2 12901 _bfd_error_handler (_("%pB: failed to generate import library"),
4eca0228 12902 info->out_implib_bfd);
0a1b45a2 12903 ret = false;
496afd17 12904 goto return_local_hash_table;
76359541
TP
12905 }
12906
c152c796
AM
12907 /* Adjust the relocs to have the correct symbol indices. */
12908 for (o = abfd->sections; o != NULL; o = o->next)
12909 {
d4730f92 12910 struct bfd_elf_section_data *esdo = elf_section_data (o);
0a1b45a2 12911 bool sort;
10bbbc1d 12912
c152c796
AM
12913 if ((o->flags & SEC_RELOC) == 0)
12914 continue;
12915
28dbcedc 12916 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3 12917 if (esdo->rel.hdr != NULL
10bbbc1d 12918 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
496afd17 12919 {
0a1b45a2 12920 ret = false;
496afd17
L
12921 goto return_local_hash_table;
12922 }
bca6d0e3 12923 if (esdo->rela.hdr != NULL
10bbbc1d 12924 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
496afd17 12925 {
0a1b45a2 12926 ret = false;
496afd17
L
12927 goto return_local_hash_table;
12928 }
c152c796
AM
12929
12930 /* Set the reloc_count field to 0 to prevent write_relocs from
12931 trying to swap the relocs out itself. */
12932 o->reloc_count = 0;
12933 }
12934
12935 if (dynamic && info->combreloc && dynobj != NULL)
12936 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12937
12938 /* If we are linking against a dynamic object, or generating a
12939 shared library, finish up the dynamic linking information. */
12940 if (dynamic)
12941 {
12942 bfd_byte *dyncon, *dynconend;
12943
12944 /* Fix up .dynamic entries. */
3d4d4302 12945 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
12946 BFD_ASSERT (o != NULL);
12947
12948 dyncon = o->contents;
eea6121a 12949 dynconend = o->contents + o->size;
c152c796
AM
12950 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12951 {
12952 Elf_Internal_Dyn dyn;
12953 const char *name;
12954 unsigned int type;
64487780
AM
12955 bfd_size_type sh_size;
12956 bfd_vma sh_addr;
c152c796
AM
12957
12958 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12959
12960 switch (dyn.d_tag)
12961 {
12962 default:
12963 continue;
12964 case DT_NULL:
12965 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12966 {
12967 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12968 {
12969 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12970 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12971 default: continue;
12972 }
12973 dyn.d_un.d_val = relativecount;
12974 relativecount = 0;
12975 break;
12976 }
12977 continue;
12978
12979 case DT_INIT:
12980 name = info->init_function;
12981 goto get_sym;
12982 case DT_FINI:
12983 name = info->fini_function;
12984 get_sym:
12985 {
12986 struct elf_link_hash_entry *h;
12987
0a1b45a2 12988 h = elf_link_hash_lookup (htab, name, false, false, true);
c152c796
AM
12989 if (h != NULL
12990 && (h->root.type == bfd_link_hash_defined
12991 || h->root.type == bfd_link_hash_defweak))
12992 {
bef26483 12993 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
12994 o = h->root.u.def.section;
12995 if (o->output_section != NULL)
bef26483 12996 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
12997 + o->output_offset);
12998 else
12999 {
13000 /* The symbol is imported from another shared
13001 library and does not apply to this one. */
bef26483 13002 dyn.d_un.d_ptr = 0;
c152c796
AM
13003 }
13004 break;
13005 }
13006 }
13007 continue;
13008
13009 case DT_PREINIT_ARRAYSZ:
13010 name = ".preinit_array";
4ade44b7 13011 goto get_out_size;
c152c796
AM
13012 case DT_INIT_ARRAYSZ:
13013 name = ".init_array";
4ade44b7 13014 goto get_out_size;
c152c796
AM
13015 case DT_FINI_ARRAYSZ:
13016 name = ".fini_array";
4ade44b7 13017 get_out_size:
c152c796
AM
13018 o = bfd_get_section_by_name (abfd, name);
13019 if (o == NULL)
13020 {
4eca0228 13021 _bfd_error_handler
4ade44b7 13022 (_("could not find section %s"), name);
c152c796
AM
13023 goto error_return;
13024 }
eea6121a 13025 if (o->size == 0)
4eca0228 13026 _bfd_error_handler
c152c796 13027 (_("warning: %s section has zero size"), name);
eea6121a 13028 dyn.d_un.d_val = o->size;
c152c796
AM
13029 break;
13030
13031 case DT_PREINIT_ARRAY:
13032 name = ".preinit_array";
4ade44b7 13033 goto get_out_vma;
c152c796
AM
13034 case DT_INIT_ARRAY:
13035 name = ".init_array";
4ade44b7 13036 goto get_out_vma;
c152c796
AM
13037 case DT_FINI_ARRAY:
13038 name = ".fini_array";
4ade44b7
AM
13039 get_out_vma:
13040 o = bfd_get_section_by_name (abfd, name);
13041 goto do_vma;
c152c796
AM
13042
13043 case DT_HASH:
13044 name = ".hash";
13045 goto get_vma;
fdc90cb4
JJ
13046 case DT_GNU_HASH:
13047 name = ".gnu.hash";
13048 goto get_vma;
c152c796
AM
13049 case DT_STRTAB:
13050 name = ".dynstr";
13051 goto get_vma;
13052 case DT_SYMTAB:
13053 name = ".dynsym";
13054 goto get_vma;
13055 case DT_VERDEF:
13056 name = ".gnu.version_d";
13057 goto get_vma;
13058 case DT_VERNEED:
13059 name = ".gnu.version_r";
13060 goto get_vma;
13061 case DT_VERSYM:
13062 name = ".gnu.version";
13063 get_vma:
4ade44b7
AM
13064 o = bfd_get_linker_section (dynobj, name);
13065 do_vma:
b3293efa 13066 if (o == NULL || bfd_is_abs_section (o->output_section))
c152c796 13067 {
4eca0228 13068 _bfd_error_handler
4ade44b7 13069 (_("could not find section %s"), name);
c152c796
AM
13070 goto error_return;
13071 }
894891db
NC
13072 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13073 {
4eca0228 13074 _bfd_error_handler
894891db
NC
13075 (_("warning: section '%s' is being made into a note"), name);
13076 bfd_set_error (bfd_error_nonrepresentable_section);
13077 goto error_return;
13078 }
4ade44b7 13079 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
c152c796
AM
13080 break;
13081
13082 case DT_REL:
13083 case DT_RELA:
13084 case DT_RELSZ:
13085 case DT_RELASZ:
13086 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13087 type = SHT_REL;
13088 else
13089 type = SHT_RELA;
64487780
AM
13090 sh_size = 0;
13091 sh_addr = 0;
c152c796
AM
13092 for (i = 1; i < elf_numsections (abfd); i++)
13093 {
13094 Elf_Internal_Shdr *hdr;
13095
13096 hdr = elf_elfsections (abfd)[i];
13097 if (hdr->sh_type == type
13098 && (hdr->sh_flags & SHF_ALLOC) != 0)
13099 {
64487780
AM
13100 sh_size += hdr->sh_size;
13101 if (sh_addr == 0
13102 || sh_addr > hdr->sh_addr)
13103 sh_addr = hdr->sh_addr;
c152c796
AM
13104 }
13105 }
64487780 13106
64f52338
AM
13107 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
13108 {
66631823
CE
13109 unsigned int opb = bfd_octets_per_byte (abfd, o);
13110
64f52338
AM
13111 /* Don't count procedure linkage table relocs in the
13112 overall reloc count. */
64487780
AM
13113 sh_size -= htab->srelplt->size;
13114 if (sh_size == 0)
13115 /* If the size is zero, make the address zero too.
13116 This is to avoid a glibc bug. If the backend
13117 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13118 zero, then we'll put DT_RELA at the end of
13119 DT_JMPREL. glibc will interpret the end of
13120 DT_RELA matching the end of DT_JMPREL as the
13121 case where DT_RELA includes DT_JMPREL, and for
13122 LD_BIND_NOW will decide that processing DT_RELA
13123 will process the PLT relocs too. Net result:
13124 No PLT relocs applied. */
13125 sh_addr = 0;
13126
64f52338
AM
13127 /* If .rela.plt is the first .rela section, exclude
13128 it from DT_RELA. */
64487780 13129 else if (sh_addr == (htab->srelplt->output_section->vma
66631823 13130 + htab->srelplt->output_offset) * opb)
64487780 13131 sh_addr += htab->srelplt->size;
64f52338 13132 }
64487780
AM
13133
13134 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13135 dyn.d_un.d_val = sh_size;
13136 else
13137 dyn.d_un.d_ptr = sh_addr;
c152c796
AM
13138 break;
13139 }
13140 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13141 }
13142 }
13143
13144 /* If we have created any dynamic sections, then output them. */
13145 if (dynobj != NULL)
13146 {
13147 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
13148 goto error_return;
13149
943284cc 13150 /* Check for DT_TEXTREL (late, in case the backend removes it). */
a6dbf402 13151 if (bfd_link_textrel_check (info)
3d4d4302 13152 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
13153 {
13154 bfd_byte *dyncon, *dynconend;
13155
943284cc
DJ
13156 dyncon = o->contents;
13157 dynconend = o->contents + o->size;
13158 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13159 {
13160 Elf_Internal_Dyn dyn;
13161
13162 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13163
13164 if (dyn.d_tag == DT_TEXTREL)
13165 {
a6dbf402 13166 if (info->textrel_check == textrel_check_error)
c192a133 13167 info->callbacks->einfo
9793eb77 13168 (_("%P%X: read-only segment has dynamic relocations\n"));
a6dbf402
L
13169 else if (bfd_link_dll (info))
13170 info->callbacks->einfo
13171 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
83b1d8f4
L
13172 else if (bfd_link_pde (info))
13173 info->callbacks->einfo
13174 (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
c192a133
AM
13175 else
13176 info->callbacks->einfo
a6dbf402 13177 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
943284cc
DJ
13178 break;
13179 }
13180 }
13181 }
13182
c152c796
AM
13183 for (o = dynobj->sections; o != NULL; o = o->next)
13184 {
13185 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 13186 || o->size == 0
c152c796
AM
13187 || o->output_section == bfd_abs_section_ptr)
13188 continue;
13189 if ((o->flags & SEC_LINKER_CREATED) == 0)
13190 {
13191 /* At this point, we are only interested in sections
13192 created by _bfd_elf_link_create_dynamic_sections. */
13193 continue;
13194 }
64f52338 13195 if (htab->stab_info.stabstr == o)
3722b82f 13196 continue;
64f52338 13197 if (htab->eh_info.hdr_sec == o)
eea6121a 13198 continue;
3d4d4302 13199 if (strcmp (o->name, ".dynstr") != 0)
c152c796 13200 {
bb294208
AM
13201 bfd_size_type octets = ((file_ptr) o->output_offset
13202 * bfd_octets_per_byte (abfd, o));
13203 if (!bfd_set_section_contents (abfd, o->output_section,
13204 o->contents, octets, o->size))
c152c796
AM
13205 goto error_return;
13206 }
13207 else
13208 {
13209 /* The contents of the .dynstr section are actually in a
13210 stringtab. */
8539e4e8
AM
13211 file_ptr off;
13212
c152c796
AM
13213 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13214 if (bfd_seek (abfd, off, SEEK_SET) != 0
64f52338 13215 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
c152c796
AM
13216 goto error_return;
13217 }
13218 }
13219 }
13220
7bdf4127 13221 if (!info->resolve_section_groups)
c152c796 13222 {
0a1b45a2 13223 bool failed = false;
c152c796 13224
7bdf4127 13225 BFD_ASSERT (bfd_link_relocatable (info));
c152c796
AM
13226 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13227 if (failed)
13228 goto error_return;
13229 }
13230
13231 /* If we have optimized stabs strings, output them. */
64f52338 13232 if (htab->stab_info.stabstr != NULL)
c152c796 13233 {
64f52338 13234 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
c152c796
AM
13235 goto error_return;
13236 }
13237
9f7c3e5e
AM
13238 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13239 goto error_return;
c152c796 13240
1ff6de03
NA
13241 if (info->callbacks->emit_ctf)
13242 info->callbacks->emit_ctf ();
13243
9f7c3e5e 13244 elf_final_link_free (abfd, &flinfo);
c152c796 13245
104d59d1
JM
13246 if (attr_section)
13247 {
a50b1753 13248 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 13249 if (contents == NULL)
496afd17
L
13250 {
13251 /* Bail out and fail. */
0a1b45a2 13252 ret = false;
496afd17
L
13253 goto return_local_hash_table;
13254 }
104d59d1
JM
13255 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
13256 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
13257 free (contents);
13258 }
13259
496afd17
L
13260 return_local_hash_table:
13261 if (info->unique_symbol)
13262 bfd_hash_table_free (&flinfo.local_hash_table);
13263 return ret;
c152c796
AM
13264
13265 error_return:
9f7c3e5e 13266 elf_final_link_free (abfd, &flinfo);
0a1b45a2 13267 ret = false;
496afd17 13268 goto return_local_hash_table;
c152c796
AM
13269}
13270\f
5241d853
RS
13271/* Initialize COOKIE for input bfd ABFD. */
13272
0a1b45a2 13273static bool
5241d853
RS
13274init_reloc_cookie (struct elf_reloc_cookie *cookie,
13275 struct bfd_link_info *info, bfd *abfd)
13276{
13277 Elf_Internal_Shdr *symtab_hdr;
13278 const struct elf_backend_data *bed;
13279
13280 bed = get_elf_backend_data (abfd);
13281 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13282
13283 cookie->abfd = abfd;
13284 cookie->sym_hashes = elf_sym_hashes (abfd);
13285 cookie->bad_symtab = elf_bad_symtab (abfd);
13286 if (cookie->bad_symtab)
13287 {
13288 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13289 cookie->extsymoff = 0;
13290 }
13291 else
13292 {
13293 cookie->locsymcount = symtab_hdr->sh_info;
13294 cookie->extsymoff = symtab_hdr->sh_info;
13295 }
13296
13297 if (bed->s->arch_size == 32)
13298 cookie->r_sym_shift = 8;
13299 else
13300 cookie->r_sym_shift = 32;
13301
13302 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13303 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13304 {
13305 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13306 cookie->locsymcount, 0,
13307 NULL, NULL, NULL);
13308 if (cookie->locsyms == NULL)
13309 {
13310 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
0a1b45a2 13311 return false;
5241d853 13312 }
a8dde0a2
L
13313 if (_bfd_link_keep_memory (info) )
13314 {
13315 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13316 info->cache_size += (cookie->locsymcount
13317 * sizeof (Elf_External_Sym_Shndx));
13318 }
5241d853 13319 }
0a1b45a2 13320 return true;
5241d853
RS
13321}
13322
13323/* Free the memory allocated by init_reloc_cookie, if appropriate. */
13324
13325static void
13326fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13327{
13328 Elf_Internal_Shdr *symtab_hdr;
13329
13330 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
c9594989 13331 if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
5241d853
RS
13332 free (cookie->locsyms);
13333}
13334
13335/* Initialize the relocation information in COOKIE for input section SEC
13336 of input bfd ABFD. */
13337
0a1b45a2 13338static bool
5241d853
RS
13339init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13340 struct bfd_link_info *info, bfd *abfd,
13341 asection *sec)
13342{
5241d853
RS
13343 if (sec->reloc_count == 0)
13344 {
13345 cookie->rels = NULL;
13346 cookie->relend = NULL;
13347 }
13348 else
13349 {
a8dde0a2
L
13350 cookie->rels = _bfd_elf_link_info_read_relocs (abfd, info, sec,
13351 NULL, NULL,
13352 _bfd_link_keep_memory (info));
5241d853 13353 if (cookie->rels == NULL)
0a1b45a2 13354 return false;
5241d853 13355 cookie->rel = cookie->rels;
056bafd4 13356 cookie->relend = cookie->rels + sec->reloc_count;
5241d853
RS
13357 }
13358 cookie->rel = cookie->rels;
0a1b45a2 13359 return true;
5241d853
RS
13360}
13361
13362/* Free the memory allocated by init_reloc_cookie_rels,
13363 if appropriate. */
13364
13365static void
13366fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13367 asection *sec)
13368{
c9594989 13369 if (elf_section_data (sec)->relocs != cookie->rels)
5241d853
RS
13370 free (cookie->rels);
13371}
13372
13373/* Initialize the whole of COOKIE for input section SEC. */
13374
0a1b45a2 13375static bool
5241d853
RS
13376init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13377 struct bfd_link_info *info,
13378 asection *sec)
13379{
13380 if (!init_reloc_cookie (cookie, info, sec->owner))
13381 goto error1;
13382 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13383 goto error2;
0a1b45a2 13384 return true;
5241d853
RS
13385
13386 error2:
13387 fini_reloc_cookie (cookie, sec->owner);
13388 error1:
0a1b45a2 13389 return false;
5241d853
RS
13390}
13391
13392/* Free the memory allocated by init_reloc_cookie_for_section,
13393 if appropriate. */
13394
13395static void
13396fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13397 asection *sec)
13398{
13399 fini_reloc_cookie_rels (cookie, sec);
13400 fini_reloc_cookie (cookie, sec->owner);
13401}
13402\f
c152c796
AM
13403/* Garbage collect unused sections. */
13404
07adf181
AM
13405/* Default gc_mark_hook. */
13406
13407asection *
13408_bfd_elf_gc_mark_hook (asection *sec,
13409 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13410 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13411 struct elf_link_hash_entry *h,
13412 Elf_Internal_Sym *sym)
13413{
13414 if (h != NULL)
13415 {
13416 switch (h->root.type)
13417 {
13418 case bfd_link_hash_defined:
13419 case bfd_link_hash_defweak:
13420 return h->root.u.def.section;
13421
13422 case bfd_link_hash_common:
13423 return h->root.u.c.p->section;
13424
13425 default:
13426 break;
13427 }
13428 }
13429 else
13430 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13431
13432 return NULL;
13433}
13434
9e223787 13435/* Return the debug definition section. */
b7c871ed
L
13436
13437static asection *
13438elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13439 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13440 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13441 struct elf_link_hash_entry *h,
9e223787 13442 Elf_Internal_Sym *sym)
b7c871ed 13443{
9e223787
L
13444 if (h != NULL)
13445 {
13446 /* Return the global debug definition section. */
13447 if ((h->root.type == bfd_link_hash_defined
13448 || h->root.type == bfd_link_hash_defweak)
13449 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13450 return h->root.u.def.section;
13451 }
13452 else
13453 {
13454 /* Return the local debug definition section. */
13455 asection *isec = bfd_section_from_elf_index (sec->owner,
13456 sym->st_shndx);
13457 if ((isec->flags & SEC_DEBUGGING) != 0)
13458 return isec;
13459 }
b7c871ed
L
13460
13461 return NULL;
13462}
13463
5241d853
RS
13464/* COOKIE->rel describes a relocation against section SEC, which is
13465 a section we've decided to keep. Return the section that contains
13466 the relocation symbol, or NULL if no section contains it. */
13467
13468asection *
13469_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13470 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9 13471 struct elf_reloc_cookie *cookie,
0a1b45a2 13472 bool *start_stop)
5241d853
RS
13473{
13474 unsigned long r_symndx;
3024a17a 13475 struct elf_link_hash_entry *h, *hw;
5241d853
RS
13476
13477 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 13478 if (r_symndx == STN_UNDEF)
5241d853
RS
13479 return NULL;
13480
13481 if (r_symndx >= cookie->locsymcount
13482 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13483 {
0a1b45a2 13484 bool was_marked;
5789f845 13485
5241d853 13486 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
13487 if (h == NULL)
13488 {
871b3ab2 13489 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
263ddf68
L
13490 sec->owner);
13491 return NULL;
13492 }
5241d853
RS
13493 while (h->root.type == bfd_link_hash_indirect
13494 || h->root.type == bfd_link_hash_warning)
13495 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5789f845
AM
13496
13497 was_marked = h->mark;
1d5316ab 13498 h->mark = 1;
3024a17a
AM
13499 /* Keep all aliases of the symbol too. If an object symbol
13500 needs to be copied into .dynbss then all of its aliases
13501 should be present as dynamic symbols, not just the one used
13502 on the copy relocation. */
13503 hw = h;
13504 while (hw->is_weakalias)
13505 {
13506 hw = hw->u.alias;
13507 hw->mark = 1;
13508 }
1cce69b9 13509
5789f845 13510 if (!was_marked && h->start_stop && !h->root.ldscript_def)
1cce69b9 13511 {
8ee10e86
AM
13512 if (info->start_stop_gc)
13513 return NULL;
13514
7dba9362
AM
13515 /* To work around a glibc bug, mark XXX input sections
13516 when there is a reference to __start_XXX or __stop_XXX
13517 symbols. */
8ee10e86 13518 else if (start_stop != NULL)
1cce69b9 13519 {
cbd0eecf 13520 asection *s = h->u2.start_stop_section;
0a1b45a2 13521 *start_stop = true;
a6a4679f 13522 return s;
1cce69b9
AM
13523 }
13524 }
13525
5241d853
RS
13526 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13527 }
13528
13529 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13530 &cookie->locsyms[r_symndx]);
13531}
13532
13533/* COOKIE->rel describes a relocation against section SEC, which is
13534 a section we've decided to keep. Mark the section that contains
9d0a14d3 13535 the relocation symbol. */
5241d853 13536
0a1b45a2 13537bool
5241d853
RS
13538_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13539 asection *sec,
13540 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 13541 struct elf_reloc_cookie *cookie)
5241d853
RS
13542{
13543 asection *rsec;
0a1b45a2 13544 bool start_stop = false;
5241d853 13545
1cce69b9
AM
13546 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13547 while (rsec != NULL)
5241d853 13548 {
1cce69b9
AM
13549 if (!rsec->gc_mark)
13550 {
13551 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13552 || (rsec->owner->flags & DYNAMIC) != 0)
13553 rsec->gc_mark = 1;
13554 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
0a1b45a2 13555 return false;
1cce69b9
AM
13556 }
13557 if (!start_stop)
13558 break;
199af150 13559 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853 13560 }
0a1b45a2 13561 return true;
5241d853
RS
13562}
13563
07adf181
AM
13564/* The mark phase of garbage collection. For a given section, mark
13565 it and any sections in this section's group, and all the sections
13566 which define symbols to which it refers. */
13567
0a1b45a2 13568bool
ccfa59ea
AM
13569_bfd_elf_gc_mark (struct bfd_link_info *info,
13570 asection *sec,
6a5bb875 13571 elf_gc_mark_hook_fn gc_mark_hook)
c152c796 13572{
0a1b45a2 13573 bool ret;
9d0a14d3 13574 asection *group_sec, *eh_frame;
c152c796
AM
13575
13576 sec->gc_mark = 1;
13577
13578 /* Mark all the sections in the group. */
13579 group_sec = elf_section_data (sec)->next_in_group;
13580 if (group_sec && !group_sec->gc_mark)
ccfa59ea 13581 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
0a1b45a2 13582 return false;
c152c796
AM
13583
13584 /* Look through the section relocs. */
0a1b45a2 13585 ret = true;
9d0a14d3
RS
13586 eh_frame = elf_eh_frame_section (sec->owner);
13587 if ((sec->flags & SEC_RELOC) != 0
13588 && sec->reloc_count > 0
13589 && sec != eh_frame)
c152c796 13590 {
5241d853 13591 struct elf_reloc_cookie cookie;
c152c796 13592
5241d853 13593 if (!init_reloc_cookie_for_section (&cookie, info, sec))
0a1b45a2 13594 ret = false;
c152c796 13595 else
c152c796 13596 {
5241d853 13597 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 13598 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853 13599 {
0a1b45a2 13600 ret = false;
5241d853
RS
13601 break;
13602 }
13603 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
13604 }
13605 }
9d0a14d3
RS
13606
13607 if (ret && eh_frame && elf_fde_list (sec))
13608 {
13609 struct elf_reloc_cookie cookie;
13610
13611 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
0a1b45a2 13612 ret = false;
9d0a14d3
RS
13613 else
13614 {
13615 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13616 gc_mark_hook, &cookie))
0a1b45a2 13617 ret = false;
9d0a14d3
RS
13618 fini_reloc_cookie_for_section (&cookie, eh_frame);
13619 }
13620 }
13621
2f0c68f2
CM
13622 eh_frame = elf_section_eh_frame_entry (sec);
13623 if (ret && eh_frame && !eh_frame->gc_mark)
13624 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
0a1b45a2 13625 ret = false;
2f0c68f2 13626
c152c796
AM
13627 return ret;
13628}
13629
3c758495
TG
13630/* Scan and mark sections in a special or debug section group. */
13631
13632static void
13633_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13634{
13635 /* Point to first section of section group. */
13636 asection *ssec;
13637 /* Used to iterate the section group. */
13638 asection *msec;
13639
0a1b45a2
AM
13640 bool is_special_grp = true;
13641 bool is_debug_grp = true;
3c758495
TG
13642
13643 /* First scan to see if group contains any section other than debug
13644 and special section. */
13645 ssec = msec = elf_next_in_group (grp);
13646 do
13647 {
13648 if ((msec->flags & SEC_DEBUGGING) == 0)
0a1b45a2 13649 is_debug_grp = false;
3c758495
TG
13650
13651 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
0a1b45a2 13652 is_special_grp = false;
3c758495
TG
13653
13654 msec = elf_next_in_group (msec);
13655 }
13656 while (msec != ssec);
13657
13658 /* If this is a pure debug section group or pure special section group,
13659 keep all sections in this group. */
13660 if (is_debug_grp || is_special_grp)
13661 {
13662 do
13663 {
13664 msec->gc_mark = 1;
13665 msec = elf_next_in_group (msec);
13666 }
13667 while (msec != ssec);
13668 }
13669}
13670
7f6ab9f8
AM
13671/* Keep debug and special sections. */
13672
0a1b45a2 13673bool
7f6ab9f8 13674_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
b7d07216 13675 elf_gc_mark_hook_fn mark_hook)
7f6ab9f8
AM
13676{
13677 bfd *ibfd;
13678
c72f2fb2 13679 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
13680 {
13681 asection *isec;
0a1b45a2
AM
13682 bool some_kept;
13683 bool debug_frag_seen;
13684 bool has_kept_debug_info;
7f6ab9f8
AM
13685
13686 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13687 continue;
57963c05
AM
13688 isec = ibfd->sections;
13689 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13690 continue;
7f6ab9f8 13691
b40bf0a2
NC
13692 /* Ensure all linker created sections are kept,
13693 see if any other section is already marked,
13694 and note if we have any fragmented debug sections. */
0a1b45a2 13695 debug_frag_seen = some_kept = has_kept_debug_info = false;
7f6ab9f8
AM
13696 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13697 {
13698 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13699 isec->gc_mark = 1;
eb026f09
AM
13700 else if (isec->gc_mark
13701 && (isec->flags & SEC_ALLOC) != 0
13702 && elf_section_type (isec) != SHT_NOTE)
0a1b45a2 13703 some_kept = true;
b7d07216
L
13704 else
13705 {
13706 /* Since all sections, except for backend specific ones,
13707 have been garbage collected, call mark_hook on this
13708 section if any of its linked-to sections is marked. */
def97fb9
AM
13709 asection *linked_to_sec;
13710 for (linked_to_sec = elf_linked_to_section (isec);
13711 linked_to_sec != NULL && !linked_to_sec->linker_mark;
b7d07216 13712 linked_to_sec = elf_linked_to_section (linked_to_sec))
def97fb9
AM
13713 {
13714 if (linked_to_sec->gc_mark)
13715 {
13716 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
0a1b45a2 13717 return false;
def97fb9
AM
13718 break;
13719 }
13720 linked_to_sec->linker_mark = 1;
13721 }
13722 for (linked_to_sec = elf_linked_to_section (isec);
13723 linked_to_sec != NULL && linked_to_sec->linker_mark;
13724 linked_to_sec = elf_linked_to_section (linked_to_sec))
13725 linked_to_sec->linker_mark = 0;
b7d07216 13726 }
b40bf0a2 13727
535b785f 13728 if (!debug_frag_seen
b40bf0a2 13729 && (isec->flags & SEC_DEBUGGING)
08dedd66 13730 && startswith (isec->name, ".debug_line."))
0a1b45a2 13731 debug_frag_seen = true;
5242a0a0
L
13732 else if (strcmp (bfd_section_name (isec),
13733 "__patchable_function_entries") == 0
13734 && elf_linked_to_section (isec) == NULL)
13735 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
13736 "need linked-to section "
13737 "for --gc-sections\n"),
13738 isec->owner, isec);
7f6ab9f8
AM
13739 }
13740
eb026f09
AM
13741 /* If no non-note alloc section in this file will be kept, then
13742 we can toss out the debug and special sections. */
7f6ab9f8
AM
13743 if (!some_kept)
13744 continue;
13745
13746 /* Keep debug and special sections like .comment when they are
3c758495 13747 not part of a group. Also keep section groups that contain
b7d07216
L
13748 just debug sections or special sections. NB: Sections with
13749 linked-to section has been handled above. */
7f6ab9f8 13750 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
13751 {
13752 if ((isec->flags & SEC_GROUP) != 0)
13753 _bfd_elf_gc_mark_debug_special_section_group (isec);
13754 else if (((isec->flags & SEC_DEBUGGING) != 0
13755 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
b7d07216
L
13756 && elf_next_in_group (isec) == NULL
13757 && elf_linked_to_section (isec) == NULL)
3c758495 13758 isec->gc_mark = 1;
b7c871ed 13759 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
0a1b45a2 13760 has_kept_debug_info = true;
3c758495 13761 }
b40bf0a2 13762
b40bf0a2
NC
13763 /* Look for CODE sections which are going to be discarded,
13764 and find and discard any fragmented debug sections which
13765 are associated with that code section. */
b7c871ed
L
13766 if (debug_frag_seen)
13767 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13768 if ((isec->flags & SEC_CODE) != 0
13769 && isec->gc_mark == 0)
13770 {
13771 unsigned int ilen;
13772 asection *dsec;
b40bf0a2 13773
b7c871ed 13774 ilen = strlen (isec->name);
b40bf0a2 13775
b7c871ed 13776 /* Association is determined by the name of the debug
07d6d2b8 13777 section containing the name of the code section as
b7c871ed
L
13778 a suffix. For example .debug_line.text.foo is a
13779 debug section associated with .text.foo. */
13780 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13781 {
13782 unsigned int dlen;
b40bf0a2 13783
b7c871ed
L
13784 if (dsec->gc_mark == 0
13785 || (dsec->flags & SEC_DEBUGGING) == 0)
13786 continue;
b40bf0a2 13787
b7c871ed 13788 dlen = strlen (dsec->name);
b40bf0a2 13789
b7c871ed
L
13790 if (dlen > ilen
13791 && strncmp (dsec->name + (dlen - ilen),
13792 isec->name, ilen) == 0)
b40bf0a2 13793 dsec->gc_mark = 0;
b7c871ed 13794 }
b40bf0a2 13795 }
b7c871ed
L
13796
13797 /* Mark debug sections referenced by kept debug sections. */
13798 if (has_kept_debug_info)
13799 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13800 if (isec->gc_mark
13801 && (isec->flags & SEC_DEBUGGING) != 0)
13802 if (!_bfd_elf_gc_mark (info, isec,
13803 elf_gc_mark_debug_section))
0a1b45a2 13804 return false;
7f6ab9f8 13805 }
0a1b45a2 13806 return true;
7f6ab9f8
AM
13807}
13808
0a1b45a2 13809static bool
ccabcbe5 13810elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
13811{
13812 bfd *sub;
ccabcbe5 13813 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
c152c796 13814
c72f2fb2 13815 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13816 {
13817 asection *o;
13818
b19a8f85 13819 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 13820 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
b19a8f85 13821 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796 13822 continue;
57963c05
AM
13823 o = sub->sections;
13824 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13825 continue;
c152c796
AM
13826
13827 for (o = sub->sections; o != NULL; o = o->next)
13828 {
a33dafc3
L
13829 /* When any section in a section group is kept, we keep all
13830 sections in the section group. If the first member of
13831 the section group is excluded, we will also exclude the
13832 group section. */
13833 if (o->flags & SEC_GROUP)
13834 {
13835 asection *first = elf_next_in_group (o);
13836 o->gc_mark = first->gc_mark;
13837 }
c152c796 13838
1e7eae0d 13839 if (o->gc_mark)
c152c796
AM
13840 continue;
13841
13842 /* Skip sweeping sections already excluded. */
13843 if (o->flags & SEC_EXCLUDE)
13844 continue;
13845
13846 /* Since this is early in the link process, it is simple
13847 to remove a section from the output. */
13848 o->flags |= SEC_EXCLUDE;
13849
c55fe096 13850 if (info->print_gc_sections && o->size != 0)
695344c0 13851 /* xgettext:c-format */
9793eb77 13852 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
c08bb8dd 13853 o, sub);
c152c796
AM
13854 }
13855 }
13856
0a1b45a2 13857 return true;
c152c796
AM
13858}
13859
13860/* Propagate collected vtable information. This is called through
13861 elf_link_hash_traverse. */
13862
0a1b45a2 13863static bool
c152c796
AM
13864elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13865{
c152c796 13866 /* Those that are not vtables. */
cbd0eecf
L
13867 if (h->start_stop
13868 || h->u2.vtable == NULL
13869 || h->u2.vtable->parent == NULL)
0a1b45a2 13870 return true;
c152c796
AM
13871
13872 /* Those vtables that do not have parents, we cannot merge. */
cbd0eecf 13873 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
0a1b45a2 13874 return true;
c152c796
AM
13875
13876 /* If we've already been done, exit. */
cbd0eecf 13877 if (h->u2.vtable->used && h->u2.vtable->used[-1])
0a1b45a2 13878 return true;
c152c796
AM
13879
13880 /* Make sure the parent's table is up to date. */
cbd0eecf 13881 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
c152c796 13882
cbd0eecf 13883 if (h->u2.vtable->used == NULL)
c152c796
AM
13884 {
13885 /* None of this table's entries were referenced. Re-use the
13886 parent's table. */
cbd0eecf
L
13887 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13888 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
c152c796
AM
13889 }
13890 else
13891 {
13892 size_t n;
0a1b45a2 13893 bool *cu, *pu;
c152c796
AM
13894
13895 /* Or the parent's entries into ours. */
cbd0eecf 13896 cu = h->u2.vtable->used;
0a1b45a2 13897 cu[-1] = true;
cbd0eecf 13898 pu = h->u2.vtable->parent->u2.vtable->used;
c152c796
AM
13899 if (pu != NULL)
13900 {
13901 const struct elf_backend_data *bed;
13902 unsigned int log_file_align;
13903
13904 bed = get_elf_backend_data (h->root.u.def.section->owner);
13905 log_file_align = bed->s->log_file_align;
cbd0eecf 13906 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
c152c796
AM
13907 while (n--)
13908 {
13909 if (*pu)
0a1b45a2 13910 *cu = true;
c152c796
AM
13911 pu++;
13912 cu++;
13913 }
13914 }
13915 }
13916
0a1b45a2 13917 return true;
c152c796
AM
13918}
13919
a8dde0a2
L
13920struct link_info_ok
13921{
13922 struct bfd_link_info *info;
13923 bool ok;
13924};
13925
0a1b45a2 13926static bool
a8dde0a2
L
13927elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h,
13928 void *ptr)
c152c796
AM
13929{
13930 asection *sec;
13931 bfd_vma hstart, hend;
13932 Elf_Internal_Rela *relstart, *relend, *rel;
13933 const struct elf_backend_data *bed;
13934 unsigned int log_file_align;
a8dde0a2 13935 struct link_info_ok *info = (struct link_info_ok *) ptr;
c152c796 13936
c152c796
AM
13937 /* Take care of both those symbols that do not describe vtables as
13938 well as those that are not loaded. */
cbd0eecf
L
13939 if (h->start_stop
13940 || h->u2.vtable == NULL
13941 || h->u2.vtable->parent == NULL)
0a1b45a2 13942 return true;
c152c796
AM
13943
13944 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13945 || h->root.type == bfd_link_hash_defweak);
13946
13947 sec = h->root.u.def.section;
13948 hstart = h->root.u.def.value;
13949 hend = hstart + h->size;
13950
a8dde0a2
L
13951 relstart = _bfd_elf_link_info_read_relocs (sec->owner, info->info,
13952 sec, NULL, NULL, true);
c152c796 13953 if (!relstart)
a8dde0a2 13954 return info->ok = false;
c152c796
AM
13955 bed = get_elf_backend_data (sec->owner);
13956 log_file_align = bed->s->log_file_align;
13957
056bafd4 13958 relend = relstart + sec->reloc_count;
c152c796
AM
13959
13960 for (rel = relstart; rel < relend; ++rel)
13961 if (rel->r_offset >= hstart && rel->r_offset < hend)
13962 {
13963 /* If the entry is in use, do nothing. */
cbd0eecf
L
13964 if (h->u2.vtable->used
13965 && (rel->r_offset - hstart) < h->u2.vtable->size)
c152c796
AM
13966 {
13967 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
cbd0eecf 13968 if (h->u2.vtable->used[entry])
c152c796
AM
13969 continue;
13970 }
13971 /* Otherwise, kill it. */
13972 rel->r_offset = rel->r_info = rel->r_addend = 0;
13973 }
13974
0a1b45a2 13975 return true;
c152c796
AM
13976}
13977
87538722
AM
13978/* Mark sections containing dynamically referenced symbols. When
13979 building shared libraries, we must assume that any visible symbol is
13980 referenced. */
715df9b8 13981
0a1b45a2 13982bool
64d03ab5 13983bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 13984{
87538722 13985 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 13986 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 13987
715df9b8
EB
13988 if ((h->root.type == bfd_link_hash_defined
13989 || h->root.type == bfd_link_hash_defweak)
8ee10e86
AM
13990 && (!h->start_stop
13991 || h->root.ldscript_def
13992 || !info->start_stop_gc)
d664fd41 13993 && ((h->ref_dynamic && !h->forced_local)
c4621b33 13994 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 13995 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 13996 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 13997 && (!bfd_link_executable (info)
22185505 13998 || info->gc_keep_exported
b407645f
AM
13999 || info->export_dynamic
14000 || (h->dynamic
14001 && d != NULL
14002 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 14003 && (h->versioned >= versioned
54e8959c
L
14004 || !bfd_hide_sym_by_version (info->version_info,
14005 h->root.root.string)))))
715df9b8
EB
14006 h->root.u.def.section->flags |= SEC_KEEP;
14007
0a1b45a2 14008 return true;
715df9b8 14009}
3b36f7e6 14010
74f0fb50
AM
14011/* Keep all sections containing symbols undefined on the command-line,
14012 and the section containing the entry symbol. */
14013
14014void
14015_bfd_elf_gc_keep (struct bfd_link_info *info)
14016{
14017 struct bfd_sym_chain *sym;
14018
14019 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
14020 {
14021 struct elf_link_hash_entry *h;
14022
14023 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
0a1b45a2 14024 false, false, false);
74f0fb50
AM
14025
14026 if (h != NULL
14027 && (h->root.type == bfd_link_hash_defined
14028 || h->root.type == bfd_link_hash_defweak)
2f5541f3 14029 && !bfd_is_const_section (h->root.u.def.section))
74f0fb50
AM
14030 h->root.u.def.section->flags |= SEC_KEEP;
14031 }
14032}
14033
0a1b45a2 14034bool
2f0c68f2
CM
14035bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
14036 struct bfd_link_info *info)
14037{
14038 bfd *ibfd = info->input_bfds;
14039
14040 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14041 {
14042 asection *sec;
14043 struct elf_reloc_cookie cookie;
14044
14045 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14046 continue;
57963c05
AM
14047 sec = ibfd->sections;
14048 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14049 continue;
2f0c68f2
CM
14050
14051 if (!init_reloc_cookie (&cookie, info, ibfd))
0a1b45a2 14052 return false;
2f0c68f2
CM
14053
14054 for (sec = ibfd->sections; sec; sec = sec->next)
14055 {
08dedd66 14056 if (startswith (bfd_section_name (sec), ".eh_frame_entry")
2f0c68f2
CM
14057 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
14058 {
14059 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
14060 fini_reloc_cookie_rels (&cookie, sec);
14061 }
14062 }
14063 }
0a1b45a2 14064 return true;
2f0c68f2
CM
14065}
14066
c152c796
AM
14067/* Do mark and sweep of unused sections. */
14068
0a1b45a2 14069bool
c152c796
AM
14070bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
14071{
0a1b45a2 14072 bool ok = true;
c152c796 14073 bfd *sub;
6a5bb875 14074 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 14075 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 14076 struct elf_link_hash_table *htab;
a8dde0a2 14077 struct link_info_ok info_ok;
c152c796 14078
64d03ab5 14079 if (!bed->can_gc_sections
715df9b8 14080 || !is_elf_hash_table (info->hash))
c152c796 14081 {
9793eb77 14082 _bfd_error_handler(_("warning: gc-sections option ignored"));
0a1b45a2 14083 return true;
c152c796
AM
14084 }
14085
74f0fb50 14086 bed->gc_keep (info);
da44f4e5 14087 htab = elf_hash_table (info);
74f0fb50 14088
9d0a14d3
RS
14089 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14090 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
14091 for (sub = info->input_bfds;
14092 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14093 sub = sub->link.next)
9d0a14d3
RS
14094 {
14095 asection *sec;
14096 struct elf_reloc_cookie cookie;
14097
57963c05
AM
14098 sec = sub->sections;
14099 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14100 continue;
9d0a14d3 14101 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 14102 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
14103 {
14104 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
14105 if (elf_section_data (sec)->sec_info
14106 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
14107 elf_eh_frame_section (sub) = sec;
14108 fini_reloc_cookie_for_section (&cookie, sec);
199af150 14109 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
14110 }
14111 }
9d0a14d3 14112
c152c796 14113 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 14114 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796 14115 if (!ok)
0a1b45a2 14116 return false;
c152c796
AM
14117
14118 /* Kill the vtable relocations that were not used. */
a8dde0a2
L
14119 info_ok.info = info;
14120 info_ok.ok = true;
14121 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &info_ok);
14122 if (!info_ok.ok)
0a1b45a2 14123 return false;
c152c796 14124
715df9b8 14125 /* Mark dynamically referenced symbols. */
22185505 14126 if (htab->dynamic_sections_created || info->gc_keep_exported)
da44f4e5 14127 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 14128
715df9b8 14129 /* Grovel through relocs to find out who stays ... */
64d03ab5 14130 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 14131 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
14132 {
14133 asection *o;
14134
b19a8f85 14135 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 14136 || elf_object_id (sub) != elf_hash_table_id (htab)
b19a8f85 14137 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
14138 continue;
14139
57963c05
AM
14140 o = sub->sections;
14141 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14142 continue;
14143
7f6ab9f8
AM
14144 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14145 Also treat note sections as a root, if the section is not part
8b6f4cd3
L
14146 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14147 well as FINI_ARRAY sections for ld -r. */
c152c796 14148 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
14149 if (!o->gc_mark
14150 && (o->flags & SEC_EXCLUDE) == 0
24007750 14151 && ((o->flags & SEC_KEEP) != 0
8b6f4cd3
L
14152 || (bfd_link_relocatable (info)
14153 && ((elf_section_data (o)->this_hdr.sh_type
14154 == SHT_PREINIT_ARRAY)
14155 || (elf_section_data (o)->this_hdr.sh_type
14156 == SHT_INIT_ARRAY)
14157 || (elf_section_data (o)->this_hdr.sh_type
14158 == SHT_FINI_ARRAY)))
7f6ab9f8 14159 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
7026832e 14160 && elf_next_in_group (o) == NULL
99fabbc9
JL
14161 && elf_linked_to_section (o) == NULL)
14162 || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14163 && (elf_section_flags (o) & SHF_GNU_RETAIN))))
7f6ab9f8
AM
14164 {
14165 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
0a1b45a2 14166 return false;
7f6ab9f8 14167 }
c152c796
AM
14168 }
14169
6a5bb875 14170 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 14171 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 14172
c152c796 14173 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 14174 return elf_gc_sweep (abfd, info);
c152c796
AM
14175}
14176\f
14177/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14178
0a1b45a2 14179bool
c152c796
AM
14180bfd_elf_gc_record_vtinherit (bfd *abfd,
14181 asection *sec,
14182 struct elf_link_hash_entry *h,
14183 bfd_vma offset)
14184{
14185 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14186 struct elf_link_hash_entry **search, *child;
ef53be89 14187 size_t extsymcount;
c152c796
AM
14188 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14189
14190 /* The sh_info field of the symtab header tells us where the
14191 external symbols start. We don't care about the local symbols at
14192 this point. */
14193 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
14194 if (!elf_bad_symtab (abfd))
14195 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
14196
14197 sym_hashes = elf_sym_hashes (abfd);
14198 sym_hashes_end = sym_hashes + extsymcount;
14199
14200 /* Hunt down the child symbol, which is in this section at the same
14201 offset as the relocation. */
14202 for (search = sym_hashes; search != sym_hashes_end; ++search)
14203 {
14204 if ((child = *search) != NULL
14205 && (child->root.type == bfd_link_hash_defined
14206 || child->root.type == bfd_link_hash_defweak)
14207 && child->root.u.def.section == sec
14208 && child->root.u.def.value == offset)
14209 goto win;
14210 }
14211
695344c0 14212 /* xgettext:c-format */
9793eb77 14213 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
2dcf00ce 14214 abfd, sec, (uint64_t) offset);
c152c796 14215 bfd_set_error (bfd_error_invalid_operation);
0a1b45a2 14216 return false;
c152c796
AM
14217
14218 win:
cbd0eecf 14219 if (!child->u2.vtable)
f6e332e6 14220 {
cbd0eecf
L
14221 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
14222 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
14223 if (!child->u2.vtable)
0a1b45a2 14224 return false;
f6e332e6 14225 }
c152c796
AM
14226 if (!h)
14227 {
14228 /* This *should* only be the absolute section. It could potentially
14229 be that someone has defined a non-global vtable though, which
14230 would be bad. It isn't worth paging in the local symbols to be
14231 sure though; that case should simply be handled by the assembler. */
14232
cbd0eecf 14233 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
14234 }
14235 else
cbd0eecf 14236 child->u2.vtable->parent = h;
c152c796 14237
0a1b45a2 14238 return true;
c152c796
AM
14239}
14240
14241/* Called from check_relocs to record the existence of a VTENTRY reloc. */
14242
0a1b45a2 14243bool
a0ea3a14 14244bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
c152c796
AM
14245 struct elf_link_hash_entry *h,
14246 bfd_vma addend)
14247{
14248 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14249 unsigned int log_file_align = bed->s->log_file_align;
14250
a0ea3a14
L
14251 if (!h)
14252 {
14253 /* xgettext:c-format */
14254 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14255 abfd, sec);
14256 bfd_set_error (bfd_error_bad_value);
0a1b45a2 14257 return false;
a0ea3a14
L
14258 }
14259
cbd0eecf 14260 if (!h->u2.vtable)
f6e332e6 14261 {
cbd0eecf
L
14262 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14263 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14264 if (!h->u2.vtable)
0a1b45a2 14265 return false;
f6e332e6
AM
14266 }
14267
cbd0eecf 14268 if (addend >= h->u2.vtable->size)
c152c796
AM
14269 {
14270 size_t size, bytes, file_align;
0a1b45a2 14271 bool *ptr = h->u2.vtable->used;
c152c796
AM
14272
14273 /* While the symbol is undefined, we have to be prepared to handle
14274 a zero size. */
14275 file_align = 1 << log_file_align;
14276 if (h->root.type == bfd_link_hash_undefined)
14277 size = addend + file_align;
14278 else
14279 {
14280 size = h->size;
14281 if (addend >= size)
14282 {
14283 /* Oops! We've got a reference past the defined end of
14284 the table. This is probably a bug -- shall we warn? */
14285 size = addend + file_align;
14286 }
14287 }
14288 size = (size + file_align - 1) & -file_align;
14289
14290 /* Allocate one extra entry for use as a "done" flag for the
14291 consolidation pass. */
0a1b45a2 14292 bytes = ((size >> log_file_align) + 1) * sizeof (bool);
c152c796
AM
14293
14294 if (ptr)
14295 {
0a1b45a2 14296 ptr = (bool *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
14297
14298 if (ptr != NULL)
14299 {
14300 size_t oldbytes;
14301
cbd0eecf 14302 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
0a1b45a2 14303 * sizeof (bool));
c152c796
AM
14304 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14305 }
14306 }
14307 else
0a1b45a2 14308 ptr = (bool *) bfd_zmalloc (bytes);
c152c796
AM
14309
14310 if (ptr == NULL)
0a1b45a2 14311 return false;
c152c796
AM
14312
14313 /* And arrange for that done flag to be at index -1. */
cbd0eecf
L
14314 h->u2.vtable->used = ptr + 1;
14315 h->u2.vtable->size = size;
c152c796
AM
14316 }
14317
0a1b45a2 14318 h->u2.vtable->used[addend >> log_file_align] = true;
c152c796 14319
0a1b45a2 14320 return true;
c152c796
AM
14321}
14322
ae17ab41
CM
14323/* Map an ELF section header flag to its corresponding string. */
14324typedef struct
14325{
14326 char *flag_name;
14327 flagword flag_value;
14328} elf_flags_to_name_table;
14329
3f75e1d6 14330static const elf_flags_to_name_table elf_flags_to_names [] =
ae17ab41
CM
14331{
14332 { "SHF_WRITE", SHF_WRITE },
14333 { "SHF_ALLOC", SHF_ALLOC },
14334 { "SHF_EXECINSTR", SHF_EXECINSTR },
14335 { "SHF_MERGE", SHF_MERGE },
14336 { "SHF_STRINGS", SHF_STRINGS },
14337 { "SHF_INFO_LINK", SHF_INFO_LINK},
14338 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14339 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14340 { "SHF_GROUP", SHF_GROUP },
14341 { "SHF_TLS", SHF_TLS },
14342 { "SHF_MASKOS", SHF_MASKOS },
14343 { "SHF_EXCLUDE", SHF_EXCLUDE },
14344};
14345
b9c361e0 14346/* Returns TRUE if the section is to be included, otherwise FALSE. */
0a1b45a2 14347bool
ae17ab41 14348bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 14349 struct flag_info *flaginfo,
b9c361e0 14350 asection *section)
ae17ab41 14351{
8b127cbc 14352 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 14353
8b127cbc 14354 if (!flaginfo->flags_initialized)
ae17ab41 14355 {
8b127cbc
AM
14356 bfd *obfd = info->output_bfd;
14357 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14358 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
14359 int with_hex = 0;
14360 int without_hex = 0;
14361
8b127cbc 14362 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 14363 {
b9c361e0 14364 unsigned i;
8b127cbc 14365 flagword (*lookup) (char *);
ae17ab41 14366
8b127cbc
AM
14367 lookup = bed->elf_backend_lookup_section_flags_hook;
14368 if (lookup != NULL)
ae17ab41 14369 {
8b127cbc 14370 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
14371
14372 if (hexval != 0)
14373 {
14374 if (tf->with == with_flags)
14375 with_hex |= hexval;
14376 else if (tf->with == without_flags)
14377 without_hex |= hexval;
0a1b45a2 14378 tf->valid = true;
b9c361e0
JL
14379 continue;
14380 }
ae17ab41 14381 }
8b127cbc 14382 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 14383 {
8b127cbc 14384 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
14385 {
14386 if (tf->with == with_flags)
14387 with_hex |= elf_flags_to_names[i].flag_value;
14388 else if (tf->with == without_flags)
14389 without_hex |= elf_flags_to_names[i].flag_value;
0a1b45a2 14390 tf->valid = true;
b9c361e0
JL
14391 break;
14392 }
14393 }
8b127cbc 14394 if (!tf->valid)
b9c361e0 14395 {
68ffbac6 14396 info->callbacks->einfo
9793eb77 14397 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
0a1b45a2 14398 return false;
ae17ab41
CM
14399 }
14400 }
0a1b45a2 14401 flaginfo->flags_initialized = true;
8b127cbc
AM
14402 flaginfo->only_with_flags |= with_hex;
14403 flaginfo->not_with_flags |= without_hex;
ae17ab41 14404 }
ae17ab41 14405
8b127cbc 14406 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
0a1b45a2 14407 return false;
b9c361e0 14408
8b127cbc 14409 if ((flaginfo->not_with_flags & sh_flags) != 0)
0a1b45a2 14410 return false;
b9c361e0 14411
0a1b45a2 14412 return true;
ae17ab41
CM
14413}
14414
c152c796
AM
14415struct alloc_got_off_arg {
14416 bfd_vma gotoff;
10455f89 14417 struct bfd_link_info *info;
c152c796
AM
14418};
14419
14420/* We need a special top-level link routine to convert got reference counts
14421 to real got offsets. */
14422
0a1b45a2 14423static bool
c152c796
AM
14424elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14425{
a50b1753 14426 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
14427 bfd *obfd = gofarg->info->output_bfd;
14428 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 14429
c152c796
AM
14430 if (h->got.refcount > 0)
14431 {
14432 h->got.offset = gofarg->gotoff;
10455f89 14433 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
14434 }
14435 else
14436 h->got.offset = (bfd_vma) -1;
14437
0a1b45a2 14438 return true;
c152c796
AM
14439}
14440
14441/* And an accompanying bit to work out final got entry offsets once
14442 we're done. Should be called from final_link. */
14443
0a1b45a2 14444bool
c152c796
AM
14445bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14446 struct bfd_link_info *info)
14447{
14448 bfd *i;
14449 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14450 bfd_vma gotoff;
c152c796
AM
14451 struct alloc_got_off_arg gofarg;
14452
10455f89
HPN
14453 BFD_ASSERT (abfd == info->output_bfd);
14454
c152c796 14455 if (! is_elf_hash_table (info->hash))
0a1b45a2 14456 return false;
c152c796
AM
14457
14458 /* The GOT offset is relative to the .got section, but the GOT header is
14459 put into the .got.plt section, if the backend uses it. */
14460 if (bed->want_got_plt)
14461 gotoff = 0;
14462 else
14463 gotoff = bed->got_header_size;
14464
14465 /* Do the local .got entries first. */
c72f2fb2 14466 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
14467 {
14468 bfd_signed_vma *local_got;
ef53be89 14469 size_t j, locsymcount;
c152c796
AM
14470 Elf_Internal_Shdr *symtab_hdr;
14471
14472 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14473 continue;
14474
14475 local_got = elf_local_got_refcounts (i);
14476 if (!local_got)
14477 continue;
14478
14479 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14480 if (elf_bad_symtab (i))
14481 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14482 else
14483 locsymcount = symtab_hdr->sh_info;
14484
14485 for (j = 0; j < locsymcount; ++j)
14486 {
14487 if (local_got[j] > 0)
14488 {
14489 local_got[j] = gotoff;
10455f89 14490 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
14491 }
14492 else
14493 local_got[j] = (bfd_vma) -1;
14494 }
14495 }
14496
14497 /* Then the global .got entries. .plt refcounts are handled by
14498 adjust_dynamic_symbol */
14499 gofarg.gotoff = gotoff;
10455f89 14500 gofarg.info = info;
c152c796
AM
14501 elf_link_hash_traverse (elf_hash_table (info),
14502 elf_gc_allocate_got_offsets,
14503 &gofarg);
0a1b45a2 14504 return true;
c152c796
AM
14505}
14506
14507/* Many folk need no more in the way of final link than this, once
14508 got entry reference counting is enabled. */
14509
0a1b45a2 14510bool
c152c796
AM
14511bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14512{
14513 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
0a1b45a2 14514 return false;
c152c796
AM
14515
14516 /* Invoke the regular ELF backend linker to do all the work. */
14517 return bfd_elf_final_link (abfd, info);
14518}
14519
0a1b45a2 14520bool
c152c796
AM
14521bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14522{
a50b1753 14523 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
14524
14525 if (rcookie->bad_symtab)
14526 rcookie->rel = rcookie->rels;
14527
14528 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14529 {
14530 unsigned long r_symndx;
14531
14532 if (! rcookie->bad_symtab)
14533 if (rcookie->rel->r_offset > offset)
0a1b45a2 14534 return false;
c152c796
AM
14535 if (rcookie->rel->r_offset != offset)
14536 continue;
14537
14538 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 14539 if (r_symndx == STN_UNDEF)
0a1b45a2 14540 return true;
c152c796
AM
14541
14542 if (r_symndx >= rcookie->locsymcount
14543 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14544 {
14545 struct elf_link_hash_entry *h;
14546
14547 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14548
14549 while (h->root.type == bfd_link_hash_indirect
14550 || h->root.type == bfd_link_hash_warning)
14551 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14552
14553 if ((h->root.type == bfd_link_hash_defined
14554 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
14555 && (h->root.u.def.section->owner != rcookie->abfd
14556 || h->root.u.def.section->kept_section != NULL
14557 || discarded_section (h->root.u.def.section)))
0a1b45a2 14558 return true;
c152c796
AM
14559 }
14560 else
14561 {
14562 /* It's not a relocation against a global symbol,
14563 but it could be a relocation against a local
14564 symbol for a discarded section. */
14565 asection *isec;
14566 Elf_Internal_Sym *isym;
14567
14568 /* Need to: get the symbol; get the section. */
14569 isym = &rcookie->locsyms[r_symndx];
cb33740c 14570 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
14571 if (isec != NULL
14572 && (isec->kept_section != NULL
14573 || discarded_section (isec)))
0a1b45a2 14574 return true;
c152c796 14575 }
0a1b45a2 14576 return false;
c152c796 14577 }
0a1b45a2 14578 return false;
c152c796
AM
14579}
14580
14581/* Discard unneeded references to discarded sections.
75938853
AM
14582 Returns -1 on error, 1 if any section's size was changed, 0 if
14583 nothing changed. This function assumes that the relocations are in
14584 sorted order, which is true for all known assemblers. */
c152c796 14585
75938853 14586int
c152c796
AM
14587bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14588{
14589 struct elf_reloc_cookie cookie;
18cd5bce 14590 asection *o;
c152c796 14591 bfd *abfd;
75938853 14592 int changed = 0;
c152c796
AM
14593
14594 if (info->traditional_format
14595 || !is_elf_hash_table (info->hash))
75938853 14596 return 0;
c152c796 14597
18cd5bce
AM
14598 o = bfd_get_section_by_name (output_bfd, ".stab");
14599 if (o != NULL)
c152c796 14600 {
18cd5bce 14601 asection *i;
c152c796 14602
18cd5bce 14603 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 14604 {
18cd5bce
AM
14605 if (i->size == 0
14606 || i->reloc_count == 0
14607 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14608 continue;
c152c796 14609
18cd5bce
AM
14610 abfd = i->owner;
14611 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14612 continue;
c152c796 14613
18cd5bce 14614 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14615 return -1;
c152c796 14616
18cd5bce
AM
14617 if (_bfd_discard_section_stabs (abfd, i,
14618 elf_section_data (i)->sec_info,
5241d853
RS
14619 bfd_elf_reloc_symbol_deleted_p,
14620 &cookie))
75938853 14621 changed = 1;
18cd5bce
AM
14622
14623 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14624 }
18cd5bce
AM
14625 }
14626
2f0c68f2
CM
14627 o = NULL;
14628 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14629 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
14630 if (o != NULL)
14631 {
14632 asection *i;
d7153c4a 14633 int eh_changed = 0;
66631823 14634 unsigned int eh_alignment; /* Octets. */
c152c796 14635
18cd5bce 14636 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 14637 {
18cd5bce
AM
14638 if (i->size == 0)
14639 continue;
14640
14641 abfd = i->owner;
14642 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14643 continue;
14644
14645 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14646 return -1;
18cd5bce
AM
14647
14648 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14649 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
14650 bfd_elf_reloc_symbol_deleted_p,
14651 &cookie))
d7153c4a
AM
14652 {
14653 eh_changed = 1;
14654 if (i->size != i->rawsize)
14655 changed = 1;
14656 }
18cd5bce
AM
14657
14658 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14659 }
9866ffe2 14660
66631823
CE
14661 eh_alignment = ((1 << o->alignment_power)
14662 * bfd_octets_per_byte (output_bfd, o));
9866ffe2
AM
14663 /* Skip over zero terminator, and prevent empty sections from
14664 adding alignment padding at the end. */
14665 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14666 if (i->size == 0)
14667 i->flags |= SEC_EXCLUDE;
14668 else if (i->size > 4)
14669 break;
14670 /* The last non-empty eh_frame section doesn't need padding. */
14671 if (i != NULL)
14672 i = i->map_tail.s;
14673 /* Any prior sections must pad the last FDE out to the output
14674 section alignment. Otherwise we might have zero padding
14675 between sections, which would be seen as a terminator. */
14676 for (; i != NULL; i = i->map_tail.s)
14677 if (i->size == 4)
14678 /* All but the last zero terminator should have been removed. */
14679 BFD_FAIL ();
14680 else
14681 {
14682 bfd_size_type size
14683 = (i->size + eh_alignment - 1) & -eh_alignment;
14684 if (i->size != size)
af471f82 14685 {
9866ffe2
AM
14686 i->size = size;
14687 changed = 1;
14688 eh_changed = 1;
af471f82 14689 }
9866ffe2 14690 }
d7153c4a
AM
14691 if (eh_changed)
14692 elf_link_hash_traverse (elf_hash_table (info),
14693 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
18cd5bce 14694 }
c152c796 14695
18cd5bce
AM
14696 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14697 {
14698 const struct elf_backend_data *bed;
57963c05 14699 asection *s;
c152c796 14700
18cd5bce
AM
14701 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14702 continue;
57963c05
AM
14703 s = abfd->sections;
14704 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14705 continue;
18cd5bce
AM
14706
14707 bed = get_elf_backend_data (abfd);
14708
14709 if (bed->elf_backend_discard_info != NULL)
14710 {
14711 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 14712 return -1;
18cd5bce
AM
14713
14714 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 14715 changed = 1;
18cd5bce
AM
14716
14717 fini_reloc_cookie (&cookie, abfd);
14718 }
c152c796
AM
14719 }
14720
2f0c68f2
CM
14721 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14722 _bfd_elf_end_eh_frame_parsing (info);
14723
14724 if (info->eh_frame_hdr_type
0e1862bb 14725 && !bfd_link_relocatable (info)
c152c796 14726 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 14727 changed = 1;
c152c796 14728
75938853 14729 return changed;
c152c796 14730}
082b7297 14731
0a1b45a2 14732bool
0c511000 14733_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 14734 asection *sec,
c0f00686 14735 struct bfd_link_info *info)
082b7297
L
14736{
14737 flagword flags;
c77ec726 14738 const char *name, *key;
082b7297
L
14739 struct bfd_section_already_linked *l;
14740 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 14741
c77ec726 14742 if (sec->output_section == bfd_abs_section_ptr)
0a1b45a2 14743 return false;
0c511000 14744
c77ec726 14745 flags = sec->flags;
0c511000 14746
c77ec726
AM
14747 /* Return if it isn't a linkonce section. A comdat group section
14748 also has SEC_LINK_ONCE set. */
14749 if ((flags & SEC_LINK_ONCE) == 0)
0a1b45a2 14750 return false;
0c511000 14751
c77ec726
AM
14752 /* Don't put group member sections on our list of already linked
14753 sections. They are handled as a group via their group section. */
14754 if (elf_sec_group (sec) != NULL)
0a1b45a2 14755 return false;
0c511000 14756
c77ec726
AM
14757 /* For a SHT_GROUP section, use the group signature as the key. */
14758 name = sec->name;
14759 if ((flags & SEC_GROUP) != 0
14760 && elf_next_in_group (sec) != NULL
14761 && elf_group_name (elf_next_in_group (sec)) != NULL)
14762 key = elf_group_name (elf_next_in_group (sec));
14763 else
14764 {
14765 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
08dedd66 14766 if (startswith (name, ".gnu.linkonce.")
c77ec726
AM
14767 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14768 key++;
0c511000 14769 else
c77ec726
AM
14770 /* Must be a user linkonce section that doesn't follow gcc's
14771 naming convention. In this case we won't be matching
14772 single member groups. */
14773 key = name;
0c511000 14774 }
6d2cd210 14775
c77ec726 14776 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
14777
14778 for (l = already_linked_list->entry; l != NULL; l = l->next)
14779 {
c2370991 14780 /* We may have 2 different types of sections on the list: group
c77ec726
AM
14781 sections with a signature of <key> (<key> is some string),
14782 and linkonce sections named .gnu.linkonce.<type>.<key>.
14783 Match like sections. LTO plugin sections are an exception.
14784 They are always named .gnu.linkonce.t.<key> and match either
14785 type of section. */
14786 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14787 && ((flags & SEC_GROUP) != 0
14788 || strcmp (name, l->sec->name) == 0))
e8a83e93
MB
14789 || (l->sec->owner->flags & BFD_PLUGIN) != 0
14790 || (sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
14791 {
14792 /* The section has already been linked. See if we should
6d2cd210 14793 issue a warning. */
c77ec726 14794 if (!_bfd_handle_already_linked (sec, l, info))
0a1b45a2 14795 return false;
082b7297 14796
c77ec726 14797 if (flags & SEC_GROUP)
3d7f7666 14798 {
c77ec726
AM
14799 asection *first = elf_next_in_group (sec);
14800 asection *s = first;
3d7f7666 14801
c77ec726 14802 while (s != NULL)
3d7f7666 14803 {
c77ec726
AM
14804 s->output_section = bfd_abs_section_ptr;
14805 /* Record which group discards it. */
14806 s->kept_section = l->sec;
14807 s = elf_next_in_group (s);
14808 /* These lists are circular. */
14809 if (s == first)
14810 break;
3d7f7666
L
14811 }
14812 }
082b7297 14813
0a1b45a2 14814 return true;
082b7297
L
14815 }
14816 }
14817
c77ec726
AM
14818 /* A single member comdat group section may be discarded by a
14819 linkonce section and vice versa. */
14820 if ((flags & SEC_GROUP) != 0)
3d7f7666 14821 {
c77ec726 14822 asection *first = elf_next_in_group (sec);
c2370991 14823
c77ec726
AM
14824 if (first != NULL && elf_next_in_group (first) == first)
14825 /* Check this single member group against linkonce sections. */
14826 for (l = already_linked_list->entry; l != NULL; l = l->next)
14827 if ((l->sec->flags & SEC_GROUP) == 0
14828 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14829 {
14830 first->output_section = bfd_abs_section_ptr;
14831 first->kept_section = l->sec;
14832 sec->output_section = bfd_abs_section_ptr;
14833 break;
14834 }
14835 }
14836 else
14837 /* Check this linkonce section against single member groups. */
14838 for (l = already_linked_list->entry; l != NULL; l = l->next)
14839 if (l->sec->flags & SEC_GROUP)
6d2cd210 14840 {
c77ec726 14841 asection *first = elf_next_in_group (l->sec);
6d2cd210 14842
c77ec726
AM
14843 if (first != NULL
14844 && elf_next_in_group (first) == first
14845 && bfd_elf_match_symbols_in_sections (first, sec, info))
14846 {
14847 sec->output_section = bfd_abs_section_ptr;
14848 sec->kept_section = first;
14849 break;
14850 }
6d2cd210 14851 }
0c511000 14852
c77ec726
AM
14853 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14854 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14855 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14856 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14857 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14858 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14859 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14860 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14861 The reverse order cannot happen as there is never a bfd with only the
14862 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14863 matter as here were are looking only for cross-bfd sections. */
14864
08dedd66 14865 if ((flags & SEC_GROUP) == 0 && startswith (name, ".gnu.linkonce.r."))
c77ec726
AM
14866 for (l = already_linked_list->entry; l != NULL; l = l->next)
14867 if ((l->sec->flags & SEC_GROUP) == 0
08dedd66 14868 && startswith (l->sec->name, ".gnu.linkonce.t."))
c77ec726
AM
14869 {
14870 if (abfd != l->sec->owner)
14871 sec->output_section = bfd_abs_section_ptr;
14872 break;
14873 }
80c29487 14874
082b7297 14875 /* This is the first section with this name. Record it. */
c77ec726 14876 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 14877 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 14878 return sec->output_section == bfd_abs_section_ptr;
082b7297 14879}
81e1b023 14880
0a1b45a2 14881bool
a4d8e49b
L
14882_bfd_elf_common_definition (Elf_Internal_Sym *sym)
14883{
14884 return sym->st_shndx == SHN_COMMON;
14885}
14886
14887unsigned int
14888_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14889{
14890 return SHN_COMMON;
14891}
14892
14893asection *
14894_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14895{
14896 return bfd_com_section_ptr;
14897}
10455f89
HPN
14898
14899bfd_vma
14900_bfd_elf_default_got_elt_size (bfd *abfd,
14901 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14902 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14903 bfd *ibfd ATTRIBUTE_UNUSED,
14904 unsigned long symndx ATTRIBUTE_UNUSED)
14905{
14906 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14907 return bed->s->arch_size / 8;
14908}
83bac4b0
NC
14909
14910/* Routines to support the creation of dynamic relocs. */
14911
83bac4b0
NC
14912/* Returns the name of the dynamic reloc section associated with SEC. */
14913
14914static const char *
14915get_dynamic_reloc_section_name (bfd * abfd,
14916 asection * sec,
0a1b45a2 14917 bool is_rela)
83bac4b0 14918{
ddcf1fcf 14919 char *name;
fd361982 14920 const char *old_name = bfd_section_name (sec);
ddcf1fcf 14921 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 14922
ddcf1fcf 14923 if (old_name == NULL)
83bac4b0
NC
14924 return NULL;
14925
ddcf1fcf 14926 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 14927 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
14928
14929 return name;
14930}
14931
14932/* Returns the dynamic reloc section associated with SEC.
14933 If necessary compute the name of the dynamic reloc section based
14934 on SEC's name (looked up in ABFD's string table) and the setting
14935 of IS_RELA. */
14936
14937asection *
0a1b45a2
AM
14938_bfd_elf_get_dynamic_reloc_section (bfd *abfd,
14939 asection *sec,
14940 bool is_rela)
83bac4b0 14941{
0a1b45a2 14942 asection *reloc_sec = elf_section_data (sec)->sreloc;
83bac4b0
NC
14943
14944 if (reloc_sec == NULL)
14945 {
0a1b45a2 14946 const char *name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
83bac4b0
NC
14947
14948 if (name != NULL)
14949 {
3d4d4302 14950 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
14951
14952 if (reloc_sec != NULL)
14953 elf_section_data (sec)->sreloc = reloc_sec;
14954 }
14955 }
14956
14957 return reloc_sec;
14958}
14959
14960/* Returns the dynamic reloc section associated with SEC. If the
14961 section does not exist it is created and attached to the DYNOBJ
14962 bfd and stored in the SRELOC field of SEC's elf_section_data
14963 structure.
f8076f98 14964
83bac4b0
NC
14965 ALIGNMENT is the alignment for the newly created section and
14966 IS_RELA defines whether the name should be .rela.<SEC's name>
14967 or .rel.<SEC's name>. The section name is looked up in the
14968 string table associated with ABFD. */
14969
14970asection *
ca4be51c
AM
14971_bfd_elf_make_dynamic_reloc_section (asection *sec,
14972 bfd *dynobj,
14973 unsigned int alignment,
14974 bfd *abfd,
0a1b45a2 14975 bool is_rela)
83bac4b0
NC
14976{
14977 asection * reloc_sec = elf_section_data (sec)->sreloc;
14978
14979 if (reloc_sec == NULL)
14980 {
14981 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14982
14983 if (name == NULL)
14984 return NULL;
14985
3d4d4302 14986 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
14987
14988 if (reloc_sec == NULL)
14989 {
3d4d4302
AM
14990 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14991 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
14992 if ((sec->flags & SEC_ALLOC) != 0)
14993 flags |= SEC_ALLOC | SEC_LOAD;
14994
3d4d4302 14995 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
14996 if (reloc_sec != NULL)
14997 {
8877b5e5
AM
14998 /* _bfd_elf_get_sec_type_attr chooses a section type by
14999 name. Override as it may be wrong, eg. for a user
15000 section named "auto" we'll get ".relauto" which is
15001 seen to be a .rela section. */
15002 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
fd361982 15003 if (!bfd_set_section_alignment (reloc_sec, alignment))
83bac4b0
NC
15004 reloc_sec = NULL;
15005 }
15006 }
15007
15008 elf_section_data (sec)->sreloc = reloc_sec;
15009 }
15010
15011 return reloc_sec;
15012}
1338dd10 15013
bffebb6b
AM
15014/* Copy the ELF symbol type and other attributes for a linker script
15015 assignment from HSRC to HDEST. Generally this should be treated as
15016 if we found a strong non-dynamic definition for HDEST (except that
15017 ld ignores multiple definition errors). */
1338dd10 15018void
bffebb6b
AM
15019_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
15020 struct bfd_link_hash_entry *hdest,
15021 struct bfd_link_hash_entry *hsrc)
1338dd10 15022{
bffebb6b
AM
15023 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
15024 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
15025 Elf_Internal_Sym isym;
1338dd10
PB
15026
15027 ehdest->type = ehsrc->type;
35fc36a8 15028 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
15029
15030 isym.st_other = ehsrc->other;
0a1b45a2 15031 elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, true, false);
1338dd10 15032}
351f65ca
L
15033
15034/* Append a RELA relocation REL to section S in BFD. */
15035
15036void
15037elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15038{
15039 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15040 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
15041 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
15042 bed->s->swap_reloca_out (abfd, rel, loc);
15043}
15044
15045/* Append a REL relocation REL to section S in BFD. */
15046
15047void
15048elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15049{
15050 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15051 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
15052 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 15053 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 15054}
7dba9362
AM
15055
15056/* Define __start, __stop, .startof. or .sizeof. symbol. */
15057
15058struct bfd_link_hash_entry *
15059bfd_elf_define_start_stop (struct bfd_link_info *info,
15060 const char *symbol, asection *sec)
15061{
487b6440 15062 struct elf_link_hash_entry *h;
7dba9362 15063
487b6440 15064 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
0a1b45a2 15065 false, false, true);
e1b5d517 15066 /* NB: Common symbols will be turned into definition later. */
487b6440 15067 if (h != NULL
8ee10e86 15068 && !h->root.ldscript_def
487b6440
AM
15069 && (h->root.type == bfd_link_hash_undefined
15070 || h->root.type == bfd_link_hash_undefweak
e1b5d517
L
15071 || ((h->ref_regular || h->def_dynamic)
15072 && !h->def_regular
15073 && h->root.type != bfd_link_hash_common)))
7dba9362 15074 {
0a1b45a2 15075 bool was_dynamic = h->ref_dynamic || h->def_dynamic;
e1b5d517 15076 h->verinfo.verdef = NULL;
487b6440
AM
15077 h->root.type = bfd_link_hash_defined;
15078 h->root.u.def.section = sec;
15079 h->root.u.def.value = 0;
15080 h->def_regular = 1;
15081 h->def_dynamic = 0;
15082 h->start_stop = 1;
15083 h->u2.start_stop_section = sec;
15084 if (symbol[0] == '.')
15085 {
15086 /* .startof. and .sizeof. symbols are local. */
559192d8
AM
15087 const struct elf_backend_data *bed;
15088 bed = get_elf_backend_data (info->output_bfd);
0a1b45a2 15089 (*bed->elf_backend_hide_symbol) (info, h, true);
487b6440 15090 }
36b8fda5
AM
15091 else
15092 {
15093 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
cae64165
RM
15094 h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15095 | info->start_stop_visibility);
bf3077a6 15096 if (was_dynamic)
36b8fda5
AM
15097 bfd_elf_link_record_dynamic_symbol (info, h);
15098 }
487b6440 15099 return &h->root;
7dba9362 15100 }
487b6440 15101 return NULL;
7dba9362 15102}
5dbc8b37
L
15103
15104/* Find dynamic relocs for H that apply to read-only sections. */
15105
15106asection *
15107_bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15108{
15109 struct elf_dyn_relocs *p;
15110
15111 for (p = h->dyn_relocs; p != NULL; p = p->next)
15112 {
15113 asection *s = p->sec->output_section;
15114
15115 if (s != NULL && (s->flags & SEC_READONLY) != 0)
15116 return p->sec;
15117 }
15118 return NULL;
15119}
d49e5065
L
15120
15121/* Set DF_TEXTREL if we find any dynamic relocs that apply to
15122 read-only sections. */
15123
0a1b45a2 15124bool
d49e5065
L
15125_bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15126{
15127 asection *sec;
15128
15129 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 15130 return true;
d49e5065
L
15131
15132 sec = _bfd_elf_readonly_dynrelocs (h);
15133 if (sec != NULL)
15134 {
15135 struct bfd_link_info *info = (struct bfd_link_info *) inf;
15136
15137 info->flags |= DF_TEXTREL;
15138 /* xgettext:c-format */
15139 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15140 "in read-only section `%pA'\n"),
15141 sec->owner, h->root.root.string, sec);
15142
15143 if (bfd_link_textrel_check (info))
15144 /* xgettext:c-format */
15145 info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15146 "in read-only section `%pA'\n"),
15147 sec->owner, h->root.root.string, sec);
15148
15149 /* Not an error, just cut short the traversal. */
0a1b45a2 15150 return false;
d49e5065 15151 }
0a1b45a2 15152 return true;
d49e5065 15153}
3084d7a2
L
15154
15155/* Add dynamic tags. */
15156
0a1b45a2 15157bool
3084d7a2 15158_bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
0a1b45a2 15159 bool need_dynamic_reloc)
3084d7a2
L
15160{
15161 struct elf_link_hash_table *htab = elf_hash_table (info);
15162
15163 if (htab->dynamic_sections_created)
15164 {
15165 /* Add some entries to the .dynamic section. We fill in the
15166 values later, in finish_dynamic_sections, but we must add
15167 the entries now so that we get the correct size for the
15168 .dynamic section. The DT_DEBUG entry is filled in by the
15169 dynamic linker and used by the debugger. */
15170#define add_dynamic_entry(TAG, VAL) \
15171 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15172
15173 const struct elf_backend_data *bed
15174 = get_elf_backend_data (output_bfd);
15175
15176 if (bfd_link_executable (info))
15177 {
15178 if (!add_dynamic_entry (DT_DEBUG, 0))
0a1b45a2 15179 return false;
3084d7a2
L
15180 }
15181
15182 if (htab->dt_pltgot_required || htab->splt->size != 0)
15183 {
15184 /* DT_PLTGOT is used by prelink even if there is no PLT
15185 relocation. */
15186 if (!add_dynamic_entry (DT_PLTGOT, 0))
0a1b45a2 15187 return false;
3084d7a2
L
15188 }
15189
15190 if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15191 {
15192 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15193 || !add_dynamic_entry (DT_PLTREL,
15194 (bed->rela_plts_and_copies_p
15195 ? DT_RELA : DT_REL))
15196 || !add_dynamic_entry (DT_JMPREL, 0))
0a1b45a2 15197 return false;
3084d7a2
L
15198 }
15199
15200 if (htab->tlsdesc_plt
15201 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15202 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
0a1b45a2 15203 return false;
3084d7a2
L
15204
15205 if (need_dynamic_reloc)
15206 {
15207 if (bed->rela_plts_and_copies_p)
15208 {
15209 if (!add_dynamic_entry (DT_RELA, 0)
15210 || !add_dynamic_entry (DT_RELASZ, 0)
15211 || !add_dynamic_entry (DT_RELAENT,
15212 bed->s->sizeof_rela))
0a1b45a2 15213 return false;
3084d7a2
L
15214 }
15215 else
15216 {
15217 if (!add_dynamic_entry (DT_REL, 0)
15218 || !add_dynamic_entry (DT_RELSZ, 0)
15219 || !add_dynamic_entry (DT_RELENT,
15220 bed->s->sizeof_rel))
0a1b45a2 15221 return false;
3084d7a2
L
15222 }
15223
15224 /* If any dynamic relocs apply to a read-only section,
15225 then we need a DT_TEXTREL entry. */
15226 if ((info->flags & DF_TEXTREL) == 0)
15227 elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15228 info);
15229
15230 if ((info->flags & DF_TEXTREL) != 0)
15231 {
15232 if (htab->ifunc_resolvers)
15233 info->callbacks->einfo
15234 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15235 "may result in a segfault at runtime; recompile with %s\n"),
15236 bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15237
15238 if (!add_dynamic_entry (DT_TEXTREL, 0))
0a1b45a2 15239 return false;
3084d7a2
L
15240 }
15241 }
15242 }
15243#undef add_dynamic_entry
15244
0a1b45a2 15245 return true;
3084d7a2 15246}