]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elflink.c
Automatic date update in version.in
[thirdparty/binutils-gdb.git] / bfd / elflink.c
CommitLineData
252b5132 1/* ELF linking support for BFD.
e8e7cf2a 2 Copyright (C) 1995-2025 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
0a1b45a2 49static bool _bfd_elf_fix_symbol_flags
28caa186
AM
50 (struct elf_link_hash_entry *, struct elf_info_failed *);
51
d41b7648
L
52/* Return false if linker should avoid caching relocation information
53 and symbol tables of input files in memory. */
54
55static bool
56_bfd_elf_link_keep_memory (struct bfd_link_info *info)
57{
58#ifdef USE_MMAP
59 /* Don't cache symbol nor relocation tables if they are mapped in.
60 NB: Since the --no-keep-memory linker option causes:
61
62 https://sourceware.org/bugzilla/show_bug.cgi?id=31458
63
64 this is opt-in by each backend. */
65 const struct elf_backend_data *bed
66 = get_elf_backend_data (info->output_bfd);
ec8f5671 67 if (bed != NULL && bed->use_mmap)
d41b7648
L
68 return false;
69#endif
70 bfd *abfd;
71 bfd_size_type size;
72
73 if (!info->keep_memory)
74 return false;
75
76 if (info->max_cache_size == (bfd_size_type) -1)
77 return true;
78
79 abfd = info->input_bfds;
80 size = info->cache_size;
81 do
82 {
83 if (size >= info->max_cache_size)
84 {
85 /* Over the limit. Reduce the memory usage. */
86 info->keep_memory = false;
87 return false;
88 }
89 if (!abfd)
90 break;
91 size += abfd->alloc_size;
92 abfd = abfd->link.next;
93 }
94 while (1);
95
96 return true;
97}
98
931494c9
NC
99static struct elf_link_hash_entry *
100get_link_hash_entry (struct elf_link_hash_entry ** sym_hashes,
101 unsigned int symndx,
102 unsigned int ext_sym_start)
18cc11a2 103{
931494c9
NC
104 if (sym_hashes == NULL
105 /* Guard against corrupt input. See PR 32636 for an example. */
106 || symndx < ext_sym_start)
18cc11a2
NC
107 return NULL;
108
931494c9 109 struct elf_link_hash_entry *h = sym_hashes[symndx - ext_sym_start];
18cc11a2
NC
110
111 /* The hash might be empty. See PR 32641 for an example of this. */
112 if (h == NULL)
113 return NULL;
114
115 while (h->root.type == bfd_link_hash_indirect
116 || h->root.type == bfd_link_hash_warning)
117 h = (struct elf_link_hash_entry *) h->root.u.i.link;
118
119 return h;
120}
121
931494c9
NC
122struct elf_link_hash_entry *
123_bfd_elf_get_link_hash_entry (struct elf_link_hash_entry ** sym_hashes,
124 unsigned int symndx,
125 Elf_Internal_Shdr * symtab_hdr)
2f0c68f2 126{
931494c9
NC
127 if (symtab_hdr == NULL)
128 return NULL;
2f0c68f2 129
931494c9
NC
130 return get_link_hash_entry (sym_hashes, symndx, symtab_hdr->sh_info);
131}
18cc11a2 132
931494c9
NC
133static struct elf_link_hash_entry *
134get_ext_sym_hash_from_cookie (struct elf_reloc_cookie *cookie, unsigned long r_symndx)
135{
136 if (cookie == NULL || cookie->sym_hashes == NULL)
137 return NULL;
138
139 if (r_symndx >= cookie->locsymcount
140 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
141 return get_link_hash_entry (cookie->sym_hashes, r_symndx, cookie->extsymoff);
f9978def 142
931494c9 143 return NULL;
f9978def 144}
2f0c68f2 145
f9978def
NC
146asection *
147_bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
148 unsigned long r_symndx,
149 bool discard)
150{
151 struct elf_link_hash_entry *h;
152
931494c9 153 h = get_ext_sym_hash_from_cookie (cookie, r_symndx);
f9978def
NC
154
155 if (h != NULL)
156 {
2f0c68f2
CM
157 if ((h->root.type == bfd_link_hash_defined
158 || h->root.type == bfd_link_hash_defweak)
159 && discarded_section (h->root.u.def.section))
07d6d2b8 160 return h->root.u.def.section;
2f0c68f2
CM
161 else
162 return NULL;
163 }
2f0c68f2 164
f9978def
NC
165 /* It's not a relocation against a global symbol,
166 but it could be a relocation against a local
167 symbol for a discarded section. */
168 asection *isec;
169 Elf_Internal_Sym *isym;
170
171 /* Need to: get the symbol; get the section. */
172 isym = &cookie->locsyms[r_symndx];
173 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
174 if (isec != NULL
175 && discard ? discarded_section (isec) : 1)
176 return isec;
177
2f0c68f2
CM
178 return NULL;
179}
180
d98685ac
AM
181/* Define a symbol in a dynamic linkage section. */
182
183struct elf_link_hash_entry *
184_bfd_elf_define_linkage_sym (bfd *abfd,
185 struct bfd_link_info *info,
186 asection *sec,
187 const char *name)
188{
189 struct elf_link_hash_entry *h;
190 struct bfd_link_hash_entry *bh;
ccabcbe5 191 const struct elf_backend_data *bed;
d98685ac 192
0a1b45a2 193 h = elf_link_hash_lookup (elf_hash_table (info), name, false, false, false);
d98685ac
AM
194 if (h != NULL)
195 {
196 /* Zap symbol defined in an as-needed lib that wasn't linked.
197 This is a symptom of a larger problem: Absolute symbols
198 defined in shared libraries can't be overridden, because we
199 lose the link to the bfd which is via the symbol section. */
200 h->root.type = bfd_link_hash_new;
ad32986f 201 bh = &h->root;
d98685ac 202 }
ad32986f
NC
203 else
204 bh = NULL;
d98685ac 205
cf18fda4 206 bed = get_elf_backend_data (abfd);
d98685ac 207 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
0a1b45a2 208 sec, 0, NULL, false, bed->collect,
d98685ac
AM
209 &bh))
210 return NULL;
211 h = (struct elf_link_hash_entry *) bh;
ad32986f 212 BFD_ASSERT (h != NULL);
d98685ac 213 h->def_regular = 1;
e28df02b 214 h->non_elf = 0;
12b2843a 215 h->root.linker_def = 1;
d98685ac 216 h->type = STT_OBJECT;
00b7642b
AM
217 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
218 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
d98685ac 219
0a1b45a2 220 (*bed->elf_backend_hide_symbol) (info, h, true);
d98685ac
AM
221 return h;
222}
223
0a1b45a2 224bool
268b6b39 225_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
226{
227 flagword flags;
aad5d350 228 asection *s;
252b5132 229 struct elf_link_hash_entry *h;
9c5bfbb7 230 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 231 struct elf_link_hash_table *htab = elf_hash_table (info);
252b5132
RH
232
233 /* This function may be called more than once. */
ce558b89 234 if (htab->sgot != NULL)
0a1b45a2 235 return true;
252b5132 236
e5a52504 237 flags = bed->dynamic_sec_flags;
252b5132 238
14b2f831
AM
239 s = bfd_make_section_anyway_with_flags (abfd,
240 (bed->rela_plts_and_copies_p
241 ? ".rela.got" : ".rel.got"),
13bc088d 242 flags | SEC_READONLY);
6de2ae4a 243 if (s == NULL
fd361982 244 || !bfd_set_section_alignment (s, bed->s->log_file_align))
0a1b45a2 245 return false;
6de2ae4a 246 htab->srelgot = s;
252b5132 247
14b2f831 248 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
64e77c6d 249 if (s == NULL
fd361982 250 || !bfd_set_section_alignment (s, bed->s->log_file_align))
0a1b45a2 251 return false;
64e77c6d
L
252 htab->sgot = s;
253
252b5132
RH
254 if (bed->want_got_plt)
255 {
14b2f831 256 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
252b5132 257 if (s == NULL
fd361982 258 || !bfd_set_section_alignment (s, bed->s->log_file_align))
0a1b45a2 259 return false;
6de2ae4a 260 htab->sgotplt = s;
252b5132
RH
261 }
262
64e77c6d
L
263 /* The first bit of the global offset table is the header. */
264 s->size += bed->got_header_size;
265
2517a57f
AM
266 if (bed->want_got_sym)
267 {
268 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
269 (or .got.plt) section. We don't do this in the linker script
270 because we don't want to define the symbol if we are not creating
271 a global offset table. */
6de2ae4a
L
272 h = _bfd_elf_define_linkage_sym (abfd, info, s,
273 "_GLOBAL_OFFSET_TABLE_");
2517a57f 274 elf_hash_table (info)->hgot = h;
d98685ac 275 if (h == NULL)
0a1b45a2 276 return false;
2517a57f 277 }
252b5132 278
0a1b45a2 279 return true;
252b5132
RH
280}
281\f
7e9f0867 282/* Create a strtab to hold the dynamic symbol names. */
0a1b45a2 283static bool
7e9f0867
AM
284_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
285{
286 struct elf_link_hash_table *hash_table;
287
288 hash_table = elf_hash_table (info);
289 if (hash_table->dynobj == NULL)
6cd255ca
L
290 {
291 /* We may not set dynobj, an input file holding linker created
292 dynamic sections to abfd, which may be a dynamic object with
293 its own dynamic sections. We need to find a normal input file
294 to hold linker created sections if possible. */
295 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
296 {
297 bfd *ibfd;
57963c05 298 asection *s;
6cd255ca 299 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
6645479e 300 if ((ibfd->flags
57963c05
AM
301 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
302 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
4de5434b 303 && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
57963c05
AM
304 && !((s = ibfd->sections) != NULL
305 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
6cd255ca
L
306 {
307 abfd = ibfd;
308 break;
309 }
310 }
311 hash_table->dynobj = abfd;
312 }
7e9f0867
AM
313
314 if (hash_table->dynstr == NULL)
315 {
316 hash_table->dynstr = _bfd_elf_strtab_init ();
317 if (hash_table->dynstr == NULL)
0a1b45a2 318 return false;
7e9f0867 319 }
0a1b45a2 320 return true;
7e9f0867
AM
321}
322
45d6a902
AM
323/* Create some sections which will be filled in with dynamic linking
324 information. ABFD is an input file which requires dynamic sections
325 to be created. The dynamic sections take up virtual memory space
326 when the final executable is run, so we need to create them before
327 addresses are assigned to the output sections. We work out the
328 actual contents and size of these sections later. */
252b5132 329
0a1b45a2 330bool
268b6b39 331_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
252b5132 332{
45d6a902 333 flagword flags;
91d6fa6a 334 asection *s;
9c5bfbb7 335 const struct elf_backend_data *bed;
9637f6ef 336 struct elf_link_hash_entry *h;
252b5132 337
0eddce27 338 if (! is_elf_hash_table (info->hash))
0a1b45a2 339 return false;
45d6a902
AM
340
341 if (elf_hash_table (info)->dynamic_sections_created)
0a1b45a2 342 return true;
45d6a902 343
7e9f0867 344 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
0a1b45a2 345 return false;
45d6a902 346
7e9f0867 347 abfd = elf_hash_table (info)->dynobj;
e5a52504
MM
348 bed = get_elf_backend_data (abfd);
349
350 flags = bed->dynamic_sec_flags;
45d6a902
AM
351
352 /* A dynamically linked executable has a .interp section, but a
353 shared library does not. */
9b8b325a 354 if (bfd_link_executable (info) && !info->nointerp)
252b5132 355 {
14b2f831
AM
356 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
357 flags | SEC_READONLY);
3496cb2a 358 if (s == NULL)
0a1b45a2 359 return false;
45d6a902 360 }
bb0deeff 361
45d6a902
AM
362 /* Create sections to hold version informations. These are removed
363 if they are not needed. */
14b2f831
AM
364 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
365 flags | SEC_READONLY);
45d6a902 366 if (s == NULL
fd361982 367 || !bfd_set_section_alignment (s, bed->s->log_file_align))
0a1b45a2 368 return false;
45d6a902 369
14b2f831
AM
370 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
371 flags | SEC_READONLY);
45d6a902 372 if (s == NULL
fd361982 373 || !bfd_set_section_alignment (s, 1))
0a1b45a2 374 return false;
45d6a902 375
14b2f831
AM
376 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
377 flags | SEC_READONLY);
45d6a902 378 if (s == NULL
fd361982 379 || !bfd_set_section_alignment (s, bed->s->log_file_align))
0a1b45a2 380 return false;
45d6a902 381
14b2f831
AM
382 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
383 flags | SEC_READONLY);
45d6a902 384 if (s == NULL
fd361982 385 || !bfd_set_section_alignment (s, bed->s->log_file_align))
0a1b45a2 386 return false;
cae1fbbb 387 elf_hash_table (info)->dynsym = s;
45d6a902 388
14b2f831
AM
389 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
390 flags | SEC_READONLY);
3496cb2a 391 if (s == NULL)
0a1b45a2 392 return false;
45d6a902 393
14b2f831 394 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
45d6a902 395 if (s == NULL
fd361982 396 || !bfd_set_section_alignment (s, bed->s->log_file_align))
0a1b45a2 397 return false;
c3460201 398 elf_hash_table (info)->dynamic = s;
45d6a902
AM
399
400 /* The special symbol _DYNAMIC is always set to the start of the
77cfaee6
AM
401 .dynamic section. We could set _DYNAMIC in a linker script, but we
402 only want to define it if we are, in fact, creating a .dynamic
403 section. We don't want to define it if there is no .dynamic
404 section, since on some ELF platforms the start up code examines it
405 to decide how to initialize the process. */
9637f6ef
L
406 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
407 elf_hash_table (info)->hdynamic = h;
408 if (h == NULL)
0a1b45a2 409 return false;
45d6a902 410
fdc90cb4
JJ
411 if (info->emit_hash)
412 {
14b2f831
AM
413 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
414 flags | SEC_READONLY);
fdc90cb4 415 if (s == NULL
fd361982 416 || !bfd_set_section_alignment (s, bed->s->log_file_align))
0a1b45a2 417 return false;
fdc90cb4
JJ
418 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
419 }
420
f16a9783 421 if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL)
fdc90cb4 422 {
14b2f831
AM
423 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
424 flags | SEC_READONLY);
fdc90cb4 425 if (s == NULL
fd361982 426 || !bfd_set_section_alignment (s, bed->s->log_file_align))
0a1b45a2 427 return false;
fdc90cb4
JJ
428 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
429 4 32-bit words followed by variable count of 64-bit words, then
430 variable count of 32-bit words. */
431 if (bed->s->arch_size == 64)
432 elf_section_data (s)->this_hdr.sh_entsize = 0;
433 else
434 elf_section_data (s)->this_hdr.sh_entsize = 4;
435 }
45d6a902 436
6a91be86
L
437 if (info->enable_dt_relr)
438 {
439 s = bfd_make_section_anyway_with_flags (abfd, ".relr.dyn",
13bc088d 440 flags | SEC_READONLY);
6a91be86
L
441 if (s == NULL
442 || !bfd_set_section_alignment (s, bed->s->log_file_align))
443 return false;
444 elf_hash_table (info)->srelrdyn = s;
445 }
446
45d6a902
AM
447 /* Let the backend create the rest of the sections. This lets the
448 backend set the right flags. The backend will normally create
449 the .got and .plt sections. */
894891db
NC
450 if (bed->elf_backend_create_dynamic_sections == NULL
451 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
0a1b45a2 452 return false;
45d6a902 453
0a1b45a2 454 elf_hash_table (info)->dynamic_sections_created = true;
45d6a902 455
0a1b45a2 456 return true;
45d6a902
AM
457}
458
459/* Create dynamic sections when linking against a dynamic object. */
460
0a1b45a2 461bool
268b6b39 462_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
45d6a902
AM
463{
464 flagword flags, pltflags;
7325306f 465 struct elf_link_hash_entry *h;
45d6a902 466 asection *s;
9c5bfbb7 467 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 468 struct elf_link_hash_table *htab = elf_hash_table (info);
45d6a902 469
252b5132
RH
470 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
471 .rel[a].bss sections. */
e5a52504 472 flags = bed->dynamic_sec_flags;
252b5132
RH
473
474 pltflags = flags;
252b5132 475 if (bed->plt_not_loaded)
6df4d94c
MM
476 /* We do not clear SEC_ALLOC here because we still want the OS to
477 allocate space for the section; it's just that there's nothing
478 to read in from the object file. */
5d1634d7 479 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
6df4d94c
MM
480 else
481 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
252b5132
RH
482 if (bed->plt_readonly)
483 pltflags |= SEC_READONLY;
484
14b2f831 485 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
252b5132 486 if (s == NULL
fd361982 487 || !bfd_set_section_alignment (s, bed->plt_alignment))
0a1b45a2 488 return false;
6de2ae4a 489 htab->splt = s;
252b5132 490
d98685ac
AM
491 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
492 .plt section. */
7325306f
RS
493 if (bed->want_plt_sym)
494 {
495 h = _bfd_elf_define_linkage_sym (abfd, info, s,
496 "_PROCEDURE_LINKAGE_TABLE_");
497 elf_hash_table (info)->hplt = h;
498 if (h == NULL)
0a1b45a2 499 return false;
7325306f 500 }
252b5132 501
14b2f831
AM
502 s = bfd_make_section_anyway_with_flags (abfd,
503 (bed->rela_plts_and_copies_p
504 ? ".rela.plt" : ".rel.plt"),
505 flags | SEC_READONLY);
252b5132 506 if (s == NULL
fd361982 507 || !bfd_set_section_alignment (s, bed->s->log_file_align))
0a1b45a2 508 return false;
6de2ae4a 509 htab->srelplt = s;
252b5132
RH
510
511 if (! _bfd_elf_create_got_section (abfd, info))
0a1b45a2 512 return false;
252b5132 513
3018b441
RH
514 if (bed->want_dynbss)
515 {
516 /* The .dynbss section is a place to put symbols which are defined
517 by dynamic objects, are referenced by regular objects, and are
518 not functions. We must allocate space for them in the process
519 image and use a R_*_COPY reloc to tell the dynamic linker to
520 initialize them at run time. The linker script puts the .dynbss
521 section into the .bss section of the final image. */
14b2f831 522 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
afbf7e8e 523 SEC_ALLOC | SEC_LINKER_CREATED);
3496cb2a 524 if (s == NULL)
0a1b45a2 525 return false;
9d19e4fd 526 htab->sdynbss = s;
252b5132 527
5474d94f
AM
528 if (bed->want_dynrelro)
529 {
530 /* Similarly, but for symbols that were originally in read-only
afbf7e8e
AM
531 sections. This section doesn't really need to have contents,
532 but make it like other .data.rel.ro sections. */
5474d94f 533 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
afbf7e8e 534 flags);
5474d94f 535 if (s == NULL)
0a1b45a2 536 return false;
5474d94f
AM
537 htab->sdynrelro = s;
538 }
539
3018b441 540 /* The .rel[a].bss section holds copy relocs. This section is not
77cfaee6
AM
541 normally needed. We need to create it here, though, so that the
542 linker will map it to an output section. We can't just create it
543 only if we need it, because we will not know whether we need it
544 until we have seen all the input files, and the first time the
545 main linker code calls BFD after examining all the input files
546 (size_dynamic_sections) the input sections have already been
547 mapped to the output sections. If the section turns out not to
548 be needed, we can discard it later. We will never need this
549 section when generating a shared object, since they do not use
550 copy relocs. */
9d19e4fd 551 if (bfd_link_executable (info))
3018b441 552 {
14b2f831
AM
553 s = bfd_make_section_anyway_with_flags (abfd,
554 (bed->rela_plts_and_copies_p
555 ? ".rela.bss" : ".rel.bss"),
556 flags | SEC_READONLY);
3018b441 557 if (s == NULL
fd361982 558 || !bfd_set_section_alignment (s, bed->s->log_file_align))
0a1b45a2 559 return false;
9d19e4fd 560 htab->srelbss = s;
5474d94f
AM
561
562 if (bed->want_dynrelro)
563 {
564 s = (bfd_make_section_anyway_with_flags
565 (abfd, (bed->rela_plts_and_copies_p
566 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
567 flags | SEC_READONLY));
568 if (s == NULL
fd361982 569 || !bfd_set_section_alignment (s, bed->s->log_file_align))
0a1b45a2 570 return false;
5474d94f
AM
571 htab->sreldynrelro = s;
572 }
3018b441 573 }
252b5132
RH
574 }
575
0a1b45a2 576 return true;
252b5132
RH
577}
578\f
252b5132
RH
579/* Record a new dynamic symbol. We record the dynamic symbols as we
580 read the input files, since we need to have a list of all of them
581 before we can determine the final sizes of the output sections.
582 Note that we may actually call this function even though we are not
583 going to output any dynamic symbols; in some cases we know that a
584 symbol should be in the dynamic symbol table, but only if there is
585 one. */
586
0a1b45a2 587bool
c152c796
AM
588bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
589 struct elf_link_hash_entry *h)
252b5132
RH
590{
591 if (h->dynindx == -1)
592 {
2b0f7ef9 593 struct elf_strtab_hash *dynstr;
68b6ddd0 594 char *p;
252b5132 595 const char *name;
ef53be89 596 size_t indx;
252b5132 597
a896df97
AM
598 if (h->root.type == bfd_link_hash_defined
599 || h->root.type == bfd_link_hash_defweak)
600 {
601 /* An IR symbol should not be made dynamic. */
602 if (h->root.u.def.section != NULL
603 && h->root.u.def.section->owner != NULL
604 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)
0a1b45a2 605 return true;
a896df97
AM
606 }
607
7a13edea
NC
608 /* XXX: The ABI draft says the linker must turn hidden and
609 internal symbols into STB_LOCAL symbols when producing the
610 DSO. However, if ld.so honors st_other in the dynamic table,
611 this would not be necessary. */
612 switch (ELF_ST_VISIBILITY (h->other))
613 {
614 case STV_INTERNAL:
615 case STV_HIDDEN:
9d6eee78
L
616 if (h->root.type != bfd_link_hash_undefined
617 && h->root.type != bfd_link_hash_undefweak)
38048eb9 618 {
f5385ebf 619 h->forced_local = 1;
9c697157 620 return true;
7a13edea 621 }
0444bdd4 622
7a13edea
NC
623 default:
624 break;
625 }
626
252b5132
RH
627 h->dynindx = elf_hash_table (info)->dynsymcount;
628 ++elf_hash_table (info)->dynsymcount;
629
630 dynstr = elf_hash_table (info)->dynstr;
631 if (dynstr == NULL)
632 {
633 /* Create a strtab to hold the dynamic symbol names. */
2b0f7ef9 634 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
252b5132 635 if (dynstr == NULL)
0a1b45a2 636 return false;
252b5132
RH
637 }
638
9ba56ace
L
639 char *unversioned_name = NULL;
640
252b5132 641 /* We don't put any version information in the dynamic string
aad5d350 642 table. */
252b5132
RH
643 name = h->root.root.string;
644 p = strchr (name, ELF_VER_CHR);
68b6ddd0 645 if (p != NULL)
9ba56ace
L
646 {
647 unversioned_name = bfd_malloc (p - name + 1);
648 memcpy (unversioned_name, name, p - name);
649 unversioned_name[p - name] = 0;
650 name = unversioned_name;
651 }
68b6ddd0
AM
652
653 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
654
655 if (p != NULL)
9ba56ace 656 free (unversioned_name);
252b5132 657
ef53be89 658 if (indx == (size_t) -1)
0a1b45a2 659 return false;
252b5132
RH
660 h->dynstr_index = indx;
661 }
662
0a1b45a2 663 return true;
252b5132 664}
45d6a902 665\f
55255dae
L
666/* Mark a symbol dynamic. */
667
28caa186 668static void
55255dae 669bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
40b36307
L
670 struct elf_link_hash_entry *h,
671 Elf_Internal_Sym *sym)
55255dae 672{
40b36307 673 struct bfd_elf_dynamic_list *d = info->dynamic_list;
55255dae 674
40b36307 675 /* It may be called more than once on the same H. */
0e1862bb 676 if(h->dynamic || bfd_link_relocatable (info))
55255dae
L
677 return;
678
40b36307
L
679 if ((info->dynamic_data
680 && (h->type == STT_OBJECT
b8871f35 681 || h->type == STT_COMMON
40b36307 682 || (sym != NULL
b8871f35
L
683 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
684 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
a0c8462f 685 || (d != NULL
73ec947d 686 && h->non_elf
40b36307 687 && (*d->match) (&d->head, NULL, h->root.root.string)))
416c34d6
L
688 {
689 h->dynamic = 1;
690 /* NB: If a symbol is made dynamic by --dynamic-list, it has
691 non-IR reference. */
692 h->root.non_ir_ref_dynamic = 1;
693 }
55255dae
L
694}
695
45d6a902
AM
696/* Record an assignment to a symbol made by a linker script. We need
697 this in case some dynamic object refers to this symbol. */
698
0a1b45a2 699bool
fe21a8fc
L
700bfd_elf_record_link_assignment (bfd *output_bfd,
701 struct bfd_link_info *info,
268b6b39 702 const char *name,
0a1b45a2
AM
703 bool provide,
704 bool hidden)
45d6a902 705{
00cbee0a 706 struct elf_link_hash_entry *h, *hv;
4ea42fb7 707 struct elf_link_hash_table *htab;
00cbee0a 708 const struct elf_backend_data *bed;
45d6a902 709
0eddce27 710 if (!is_elf_hash_table (info->hash))
0a1b45a2 711 return true;
45d6a902 712
4ea42fb7 713 htab = elf_hash_table (info);
0a1b45a2 714 h = elf_link_hash_lookup (htab, name, !provide, true, false);
45d6a902 715 if (h == NULL)
4ea42fb7 716 return provide;
45d6a902 717
8e2a4f11
AM
718 if (h->root.type == bfd_link_hash_warning)
719 h = (struct elf_link_hash_entry *) h->root.u.i.link;
720
0f550b3d
L
721 if (h->versioned == unknown)
722 {
723 /* Set versioned if symbol version is unknown. */
724 char *version = strrchr (name, ELF_VER_CHR);
725 if (version)
726 {
727 if (version > name && version[-1] != ELF_VER_CHR)
728 h->versioned = versioned_hidden;
729 else
730 h->versioned = versioned;
731 }
732 }
733
73ec947d
AM
734 /* Symbols defined in a linker script but not referenced anywhere
735 else will have non_elf set. */
736 if (h->non_elf)
737 {
738 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
739 h->non_elf = 0;
740 }
741
00cbee0a 742 switch (h->root.type)
77cfaee6 743 {
00cbee0a
L
744 case bfd_link_hash_defined:
745 case bfd_link_hash_defweak:
746 case bfd_link_hash_common:
747 break;
748 case bfd_link_hash_undefweak:
749 case bfd_link_hash_undefined:
750 /* Since we're defining the symbol, don't let it seem to have not
751 been defined. record_dynamic_symbol and size_dynamic_sections
752 may depend on this. */
4ea42fb7 753 h->root.type = bfd_link_hash_new;
77cfaee6
AM
754 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
755 bfd_link_repair_undef_list (&htab->root);
00cbee0a
L
756 break;
757 case bfd_link_hash_new:
00cbee0a
L
758 break;
759 case bfd_link_hash_indirect:
760 /* We had a versioned symbol in a dynamic library. We make the
a0c8462f 761 the versioned symbol point to this one. */
00cbee0a
L
762 bed = get_elf_backend_data (output_bfd);
763 hv = h;
764 while (hv->root.type == bfd_link_hash_indirect
765 || hv->root.type == bfd_link_hash_warning)
766 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
767 /* We don't need to update h->root.u since linker will set them
768 later. */
769 h->root.type = bfd_link_hash_undefined;
770 hv->root.type = bfd_link_hash_indirect;
771 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
772 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
773 break;
8e2a4f11
AM
774 default:
775 BFD_FAIL ();
0a1b45a2 776 return false;
55255dae 777 }
45d6a902
AM
778
779 /* If this symbol is being provided by the linker script, and it is
780 currently defined by a dynamic object, but not by a regular
781 object, then mark it as undefined so that the generic linker will
782 force the correct value. */
783 if (provide
f5385ebf
AM
784 && h->def_dynamic
785 && !h->def_regular)
45d6a902
AM
786 h->root.type = bfd_link_hash_undefined;
787
48e30f52
L
788 /* If this symbol is currently defined by a dynamic object, but not
789 by a regular object, then clear out any version information because
790 the symbol will not be associated with the dynamic object any
791 more. */
792 if (h->def_dynamic && !h->def_regular)
b531344c
MR
793 h->verinfo.verdef = NULL;
794
795 /* Make sure this symbol is not garbage collected. */
796 h->mark = 1;
45d6a902 797
f5385ebf 798 h->def_regular = 1;
45d6a902 799
eb8476a6 800 if (hidden)
fe21a8fc 801 {
91d6fa6a 802 bed = get_elf_backend_data (output_bfd);
b8297068
AM
803 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
804 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
0a1b45a2 805 (*bed->elf_backend_hide_symbol) (info, h, true);
fe21a8fc
L
806 }
807
6fa3860b
PB
808 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
809 and executables. */
0e1862bb 810 if (!bfd_link_relocatable (info)
6fa3860b
PB
811 && h->dynindx != -1
812 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
813 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
814 h->forced_local = 1;
815
f5385ebf
AM
816 if ((h->def_dynamic
817 || h->ref_dynamic
9c697157 818 || bfd_link_dll (info))
34a87bb0 819 && !h->forced_local
45d6a902
AM
820 && h->dynindx == -1)
821 {
c152c796 822 if (! bfd_elf_link_record_dynamic_symbol (info, h))
0a1b45a2 823 return false;
45d6a902
AM
824
825 /* If this is a weak defined symbol, and we know a corresponding
826 real symbol from the same dynamic object, make sure the real
827 symbol is also made into a dynamic symbol. */
60d67dc8 828 if (h->is_weakalias)
45d6a902 829 {
60d67dc8
AM
830 struct elf_link_hash_entry *def = weakdef (h);
831
832 if (def->dynindx == -1
833 && !bfd_elf_link_record_dynamic_symbol (info, def))
0a1b45a2 834 return false;
45d6a902
AM
835 }
836 }
837
0a1b45a2 838 return true;
45d6a902 839}
42751cf3 840
8c58d23b
AM
841/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
842 success, and 2 on a failure caused by attempting to record a symbol
843 in a discarded section, eg. a discarded link-once section symbol. */
844
845int
c152c796
AM
846bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
847 bfd *input_bfd,
848 long input_indx)
8c58d23b 849{
986f0783 850 size_t amt;
8c58d23b
AM
851 struct elf_link_local_dynamic_entry *entry;
852 struct elf_link_hash_table *eht;
853 struct elf_strtab_hash *dynstr;
ef53be89 854 size_t dynstr_index;
8c58d23b
AM
855 char *name;
856 Elf_External_Sym_Shndx eshndx;
857 char esym[sizeof (Elf64_External_Sym)];
858
0eddce27 859 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
860 return 0;
861
862 /* See if the entry exists already. */
863 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
864 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
865 return 1;
866
867 amt = sizeof (*entry);
a50b1753 868 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
8c58d23b
AM
869 if (entry == NULL)
870 return 0;
871
872 /* Go find the symbol, so that we can find it's name. */
873 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 874 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
875 {
876 bfd_release (input_bfd, entry);
877 return 0;
878 }
879
880 if (entry->isym.st_shndx != SHN_UNDEF
4fbb74a6 881 && entry->isym.st_shndx < SHN_LORESERVE)
8c58d23b
AM
882 {
883 asection *s;
884
885 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
886 if (s == NULL || bfd_is_abs_section (s->output_section))
887 {
888 /* We can still bfd_release here as nothing has done another
889 bfd_alloc. We can't do this later in this function. */
890 bfd_release (input_bfd, entry);
891 return 2;
892 }
893 }
894
895 name = (bfd_elf_string_from_elf_section
896 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
897 entry->isym.st_name));
898
899 dynstr = elf_hash_table (info)->dynstr;
900 if (dynstr == NULL)
901 {
902 /* Create a strtab to hold the dynamic symbol names. */
903 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
904 if (dynstr == NULL)
905 return 0;
906 }
907
0a1b45a2 908 dynstr_index = _bfd_elf_strtab_add (dynstr, name, false);
ef53be89 909 if (dynstr_index == (size_t) -1)
8c58d23b
AM
910 return 0;
911 entry->isym.st_name = dynstr_index;
912
913 eht = elf_hash_table (info);
914
915 entry->next = eht->dynlocal;
916 eht->dynlocal = entry;
917 entry->input_bfd = input_bfd;
918 entry->input_indx = input_indx;
919 eht->dynsymcount++;
920
921 /* Whatever binding the symbol had before, it's now local. */
922 entry->isym.st_info
923 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
924
925 /* The dynindx will be set at the end of size_dynamic_sections. */
926
927 return 1;
928}
929
30b30c21 930/* Return the dynindex of a local dynamic symbol. */
42751cf3 931
30b30c21 932long
268b6b39
AM
933_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
934 bfd *input_bfd,
935 long input_indx)
30b30c21
RH
936{
937 struct elf_link_local_dynamic_entry *e;
938
939 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
940 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
941 return e->dynindx;
942 return -1;
943}
944
945/* This function is used to renumber the dynamic symbols, if some of
946 them are removed because they are marked as local. This is called
947 via elf_link_hash_traverse. */
948
0a1b45a2 949static bool
268b6b39
AM
950elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
951 void *data)
42751cf3 952{
a50b1753 953 size_t *count = (size_t *) data;
30b30c21 954
6fa3860b 955 if (h->forced_local)
0a1b45a2 956 return true;
6fa3860b
PB
957
958 if (h->dynindx != -1)
959 h->dynindx = ++(*count);
960
0a1b45a2 961 return true;
6fa3860b
PB
962}
963
964
965/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
966 STB_LOCAL binding. */
967
0a1b45a2 968static bool
6fa3860b
PB
969elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
970 void *data)
971{
a50b1753 972 size_t *count = (size_t *) data;
6fa3860b 973
6fa3860b 974 if (!h->forced_local)
0a1b45a2 975 return true;
6fa3860b 976
42751cf3 977 if (h->dynindx != -1)
30b30c21
RH
978 h->dynindx = ++(*count);
979
0a1b45a2 980 return true;
42751cf3 981}
30b30c21 982
aee6f5b4
AO
983/* Return true if the dynamic symbol for a given section should be
984 omitted when creating a shared library. */
0a1b45a2 985bool
d00dd7dc
AM
986_bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
987 struct bfd_link_info *info,
988 asection *p)
aee6f5b4 989{
74541ad4 990 struct elf_link_hash_table *htab;
ca55926c 991 asection *ip;
74541ad4 992
aee6f5b4
AO
993 switch (elf_section_data (p)->this_hdr.sh_type)
994 {
995 case SHT_PROGBITS:
996 case SHT_NOBITS:
997 /* If sh_type is yet undecided, assume it could be
998 SHT_PROGBITS/SHT_NOBITS. */
999 case SHT_NULL:
74541ad4 1000 htab = elf_hash_table (info);
74541ad4
AM
1001 if (htab->text_index_section != NULL)
1002 return p != htab->text_index_section && p != htab->data_index_section;
1003
ca55926c 1004 return (htab->dynobj != NULL
3d4d4302 1005 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
ca55926c 1006 && ip->output_section == p);
aee6f5b4
AO
1007
1008 /* There shouldn't be section relative relocations
1009 against any other section. */
1010 default:
0a1b45a2 1011 return true;
aee6f5b4
AO
1012 }
1013}
1014
0a1b45a2 1015bool
d00dd7dc
AM
1016_bfd_elf_omit_section_dynsym_all
1017 (bfd *output_bfd ATTRIBUTE_UNUSED,
1018 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1019 asection *p ATTRIBUTE_UNUSED)
1020{
0a1b45a2 1021 return true;
d00dd7dc
AM
1022}
1023
062e2358 1024/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
1025 symbol for each output section, which come first. Next come symbols
1026 which have been forced to local binding. Then all of the back-end
1027 allocated local dynamic syms, followed by the rest of the global
63f452a8
AM
1028 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
1029 (This prevents the early call before elf_backend_init_index_section
1030 and strip_excluded_output_sections setting dynindx for sections
1031 that are stripped.) */
30b30c21 1032
554220db
AM
1033static unsigned long
1034_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
1035 struct bfd_link_info *info,
1036 unsigned long *section_sym_count)
30b30c21
RH
1037{
1038 unsigned long dynsymcount = 0;
0a1b45a2 1039 bool do_sec = section_sym_count != NULL;
30b30c21 1040
0e1862bb
L
1041 if (bfd_link_pic (info)
1042 || elf_hash_table (info)->is_relocatable_executable)
30b30c21 1043 {
aee6f5b4 1044 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
1045 asection *p;
1046 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 1047 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4 1048 && (p->flags & SEC_ALLOC) != 0
7f923b7f 1049 && elf_hash_table (info)->dynamic_relocs
aee6f5b4 1050 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
63f452a8
AM
1051 {
1052 ++dynsymcount;
1053 if (do_sec)
1054 elf_section_data (p)->dynindx = dynsymcount;
1055 }
1056 else if (do_sec)
74541ad4 1057 elf_section_data (p)->dynindx = 0;
30b30c21 1058 }
63f452a8
AM
1059 if (do_sec)
1060 *section_sym_count = dynsymcount;
30b30c21 1061
6fa3860b
PB
1062 elf_link_hash_traverse (elf_hash_table (info),
1063 elf_link_renumber_local_hash_table_dynsyms,
1064 &dynsymcount);
1065
30b30c21
RH
1066 if (elf_hash_table (info)->dynlocal)
1067 {
1068 struct elf_link_local_dynamic_entry *p;
1069 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
1070 p->dynindx = ++dynsymcount;
1071 }
90ac2420 1072 elf_hash_table (info)->local_dynsymcount = dynsymcount;
30b30c21
RH
1073
1074 elf_link_hash_traverse (elf_hash_table (info),
1075 elf_link_renumber_hash_table_dynsyms,
1076 &dynsymcount);
1077
d5486c43
L
1078 /* There is an unused NULL entry at the head of the table which we
1079 must account for in our count even if the table is empty since it
1080 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
1081 .dynamic section. */
1082 dynsymcount++;
30b30c21 1083
ccabcbe5
AM
1084 elf_hash_table (info)->dynsymcount = dynsymcount;
1085 return dynsymcount;
30b30c21 1086}
252b5132 1087
54ac0771
L
1088/* Merge st_other field. */
1089
1090static void
1091elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
5160d0f3 1092 unsigned int st_other, asection *sec,
0a1b45a2 1093 bool definition, bool dynamic)
54ac0771
L
1094{
1095 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1096
1097 /* If st_other has a processor-specific meaning, specific
cd3416da 1098 code might be needed here. */
54ac0771 1099 if (bed->elf_backend_merge_symbol_attribute)
5160d0f3 1100 (*bed->elf_backend_merge_symbol_attribute) (h, st_other, definition,
54ac0771
L
1101 dynamic);
1102
cd3416da 1103 if (!dynamic)
54ac0771 1104 {
5160d0f3 1105 unsigned symvis = ELF_ST_VISIBILITY (st_other);
cd3416da 1106 unsigned hvis = ELF_ST_VISIBILITY (h->other);
54ac0771 1107
cd3416da
AM
1108 /* Keep the most constraining visibility. Leave the remainder
1109 of the st_other field to elf_backend_merge_symbol_attribute. */
1110 if (symvis - 1 < hvis - 1)
1111 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
54ac0771 1112 }
b8417128 1113 else if (definition
5160d0f3 1114 && ELF_ST_VISIBILITY (st_other) != STV_DEFAULT
b8417128 1115 && (sec->flags & SEC_READONLY) == 0)
6cabe1ea 1116 h->protected_def = 1;
54ac0771
L
1117}
1118
4f3fedcf
AM
1119/* This function is called when we want to merge a new symbol with an
1120 existing symbol. It handles the various cases which arise when we
1121 find a definition in a dynamic object, or when there is already a
1122 definition in a dynamic object. The new symbol is described by
1123 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1124 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1125 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1126 of an old common symbol. We set OVERRIDE if the old symbol is
1127 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1128 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1129 to change. By OK to change, we mean that we shouldn't warn if the
1130 type or size does change. */
45d6a902 1131
0a1b45a2 1132static bool
268b6b39
AM
1133_bfd_elf_merge_symbol (bfd *abfd,
1134 struct bfd_link_info *info,
1135 const char *name,
1136 Elf_Internal_Sym *sym,
1137 asection **psec,
1138 bfd_vma *pvalue,
4f3fedcf
AM
1139 struct elf_link_hash_entry **sym_hash,
1140 bfd **poldbfd,
0a1b45a2 1141 bool *pold_weak,
af44c138 1142 unsigned int *pold_alignment,
0a1b45a2 1143 bool *skip,
7ba11550 1144 bfd **override,
0a1b45a2
AM
1145 bool *type_change_ok,
1146 bool *size_change_ok,
1147 bool *matched)
252b5132 1148{
7479dfd4 1149 asection *sec, *oldsec;
45d6a902 1150 struct elf_link_hash_entry *h;
90c984fc 1151 struct elf_link_hash_entry *hi;
45d6a902
AM
1152 struct elf_link_hash_entry *flip;
1153 int bind;
1154 bfd *oldbfd;
0a1b45a2
AM
1155 bool newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1156 bool newweak, oldweak, newfunc, oldfunc;
a4d8e49b 1157 const struct elf_backend_data *bed;
6e33951e 1158 char *new_version;
0a1b45a2 1159 bool default_sym = *matched;
320fdefe 1160 struct elf_link_hash_table *htab;
45d6a902 1161
0a1b45a2 1162 *skip = false;
7ba11550 1163 *override = NULL;
45d6a902
AM
1164
1165 sec = *psec;
1166 bind = ELF_ST_BIND (sym->st_info);
1167
1168 if (! bfd_is_und_section (sec))
0a1b45a2 1169 h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
45d6a902
AM
1170 else
1171 h = ((struct elf_link_hash_entry *)
0a1b45a2 1172 bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
45d6a902 1173 if (h == NULL)
0a1b45a2 1174 return false;
45d6a902 1175 *sym_hash = h;
252b5132 1176
88ba32a0
L
1177 bed = get_elf_backend_data (abfd);
1178
6e33951e 1179 /* NEW_VERSION is the symbol version of the new symbol. */
422f1182 1180 if (h->versioned != unversioned)
6e33951e 1181 {
422f1182
L
1182 /* Symbol version is unknown or versioned. */
1183 new_version = strrchr (name, ELF_VER_CHR);
1184 if (new_version)
1185 {
1186 if (h->versioned == unknown)
1187 {
1188 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1189 h->versioned = versioned_hidden;
1190 else
1191 h->versioned = versioned;
1192 }
1193 new_version += 1;
1194 if (new_version[0] == '\0')
1195 new_version = NULL;
1196 }
1197 else
1198 h->versioned = unversioned;
6e33951e 1199 }
422f1182
L
1200 else
1201 new_version = NULL;
6e33951e 1202
90c984fc
L
1203 /* For merging, we only care about real symbols. But we need to make
1204 sure that indirect symbol dynamic flags are updated. */
1205 hi = h;
45d6a902
AM
1206 while (h->root.type == bfd_link_hash_indirect
1207 || h->root.type == bfd_link_hash_warning)
1208 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1209
6e33951e
L
1210 if (!*matched)
1211 {
1212 if (hi == h || h->root.type == bfd_link_hash_new)
0a1b45a2 1213 *matched = true;
6e33951e
L
1214 else
1215 {
ae7683d2 1216 /* OLD_HIDDEN is true if the existing symbol is only visible
6e33951e 1217 to the symbol with the same symbol version. NEW_HIDDEN is
ae7683d2 1218 true if the new symbol is only visible to the symbol with
6e33951e 1219 the same symbol version. */
0a1b45a2
AM
1220 bool old_hidden = h->versioned == versioned_hidden;
1221 bool new_hidden = hi->versioned == versioned_hidden;
6e33951e
L
1222 if (!old_hidden && !new_hidden)
1223 /* The new symbol matches the existing symbol if both
1224 aren't hidden. */
0a1b45a2 1225 *matched = true;
6e33951e
L
1226 else
1227 {
1228 /* OLD_VERSION is the symbol version of the existing
1229 symbol. */
422f1182
L
1230 char *old_version;
1231
1232 if (h->versioned >= versioned)
1233 old_version = strrchr (h->root.root.string,
1234 ELF_VER_CHR) + 1;
1235 else
1236 old_version = NULL;
6e33951e
L
1237
1238 /* The new symbol matches the existing symbol if they
1239 have the same symbol version. */
1240 *matched = (old_version == new_version
1241 || (old_version != NULL
1242 && new_version != NULL
1243 && strcmp (old_version, new_version) == 0));
1244 }
1245 }
1246 }
1247
934bce08
AM
1248 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1249 existing symbol. */
1250
1251 oldbfd = NULL;
1252 oldsec = NULL;
1253 switch (h->root.type)
1254 {
1255 default:
1256 break;
1257
1258 case bfd_link_hash_undefined:
1259 case bfd_link_hash_undefweak:
1260 oldbfd = h->root.u.undef.abfd;
1261 break;
1262
1263 case bfd_link_hash_defined:
1264 case bfd_link_hash_defweak:
1265 oldbfd = h->root.u.def.section->owner;
1266 oldsec = h->root.u.def.section;
1267 break;
1268
1269 case bfd_link_hash_common:
1270 oldbfd = h->root.u.c.p->section->owner;
1271 oldsec = h->root.u.c.p->section;
1272 if (pold_alignment)
1273 *pold_alignment = h->root.u.c.p->alignment_power;
1274 break;
1275 }
1276 if (poldbfd && *poldbfd == NULL)
1277 *poldbfd = oldbfd;
1278
1279 /* Differentiate strong and weak symbols. */
1280 newweak = bind == STB_WEAK;
1281 oldweak = (h->root.type == bfd_link_hash_defweak
1282 || h->root.type == bfd_link_hash_undefweak);
1283 if (pold_weak)
1284 *pold_weak = oldweak;
1285
40b36307 1286 /* We have to check it for every instance since the first few may be
ee659f1f 1287 references and not all compilers emit symbol type for undefined
40b36307
L
1288 symbols. */
1289 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1290
320fdefe
L
1291 htab = elf_hash_table (info);
1292
ee659f1f
AM
1293 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1294 respectively, is from a dynamic object. */
1295
1296 newdyn = (abfd->flags & DYNAMIC) != 0;
1297
1298 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1299 syms and defined syms in dynamic libraries respectively.
1300 ref_dynamic on the other hand can be set for a symbol defined in
1301 a dynamic library, and def_dynamic may not be set; When the
1302 definition in a dynamic lib is overridden by a definition in the
1303 executable use of the symbol in the dynamic lib becomes a
1304 reference to the executable symbol. */
1305 if (newdyn)
1306 {
1307 if (bfd_is_und_section (sec))
1308 {
1309 if (bind != STB_WEAK)
1310 {
1311 h->ref_dynamic_nonweak = 1;
1312 hi->ref_dynamic_nonweak = 1;
1313 }
1314 }
1315 else
1316 {
6e33951e
L
1317 /* Update the existing symbol only if they match. */
1318 if (*matched)
1319 h->dynamic_def = 1;
ee659f1f
AM
1320 hi->dynamic_def = 1;
1321 }
1322 }
1323
45d6a902
AM
1324 /* If we just created the symbol, mark it as being an ELF symbol.
1325 Other than that, there is nothing to do--there is no merge issue
1326 with a newly defined symbol--so we just return. */
1327
1328 if (h->root.type == bfd_link_hash_new)
252b5132 1329 {
f5385ebf 1330 h->non_elf = 0;
0a1b45a2 1331 return true;
45d6a902 1332 }
252b5132 1333
45d6a902
AM
1334 /* In cases involving weak versioned symbols, we may wind up trying
1335 to merge a symbol with itself. Catch that here, to avoid the
1336 confusion that results if we try to override a symbol with
1337 itself. The additional tests catch cases like
1338 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1339 dynamic object, which we do want to handle here. */
1340 if (abfd == oldbfd
895fa45f 1341 && (newweak || oldweak)
45d6a902 1342 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1343 || !h->def_regular))
0a1b45a2 1344 return true;
45d6a902 1345
0a1b45a2 1346 olddyn = false;
45d6a902
AM
1347 if (oldbfd != NULL)
1348 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1349 else if (oldsec != NULL)
45d6a902 1350 {
707bba77 1351 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1352 indices used by MIPS ELF. */
707bba77 1353 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1354 }
252b5132 1355
320fdefe
L
1356 /* Set non_ir_ref_dynamic only when not handling DT_NEEDED entries. */
1357 if (!htab->handling_dt_needed
1358 && oldbfd != NULL
7de7786b 1359 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN))
1a3b5c34 1360 {
7de7786b
L
1361 if (newdyn != olddyn)
1362 {
1363 /* Handle a case where plugin_notice won't be called and thus
1364 won't set the non_ir_ref flags on the first pass over
1365 symbols. */
1366 h->root.non_ir_ref_dynamic = true;
1367 hi->root.non_ir_ref_dynamic = true;
1368 }
20ea3acc
L
1369 else if ((oldbfd->flags & BFD_PLUGIN) != 0
1370 && hi->root.type == bfd_link_hash_indirect)
7de7786b
L
1371 {
1372 /* Change indirect symbol from IR to undefined. */
1373 hi->root.type = bfd_link_hash_undefined;
1374 hi->root.u.undef.abfd = oldbfd;
1375 }
1a3b5c34
AM
1376 }
1377
45d6a902
AM
1378 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1379 respectively, appear to be a definition rather than reference. */
1380
707bba77 1381 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1382
707bba77
AM
1383 olddef = (h->root.type != bfd_link_hash_undefined
1384 && h->root.type != bfd_link_hash_undefweak
202ac193 1385 && h->root.type != bfd_link_hash_common);
45d6a902 1386
0a36a439
L
1387 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1388 respectively, appear to be a function. */
1389
1390 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1391 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1392
1393 oldfunc = (h->type != STT_NOTYPE
1394 && bed->is_function_type (h->type));
1395
c5d37467 1396 if (!(newfunc && oldfunc)
5b677558
AM
1397 && ELF_ST_TYPE (sym->st_info) != h->type
1398 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1399 && h->type != STT_NOTYPE
c5d37467
AM
1400 && (newdef || bfd_is_com_section (sec))
1401 && (olddef || h->root.type == bfd_link_hash_common))
580a2b6e 1402 {
c5d37467
AM
1403 /* If creating a default indirect symbol ("foo" or "foo@") from
1404 a dynamic versioned definition ("foo@@") skip doing so if
1405 there is an existing regular definition with a different
1406 type. We don't want, for example, a "time" variable in the
1407 executable overriding a "time" function in a shared library. */
1408 if (newdyn
1409 && !olddyn)
1410 {
0a1b45a2
AM
1411 *skip = true;
1412 return true;
c5d37467
AM
1413 }
1414
1415 /* When adding a symbol from a regular object file after we have
1416 created indirect symbols, undo the indirection and any
1417 dynamic state. */
1418 if (hi != h
1419 && !newdyn
1420 && olddyn)
1421 {
1422 h = hi;
0a1b45a2 1423 (*bed->elf_backend_hide_symbol) (info, h, true);
c5d37467
AM
1424 h->forced_local = 0;
1425 h->ref_dynamic = 0;
1426 h->def_dynamic = 0;
1427 h->dynamic_def = 0;
1428 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1429 {
1430 h->root.type = bfd_link_hash_undefined;
1431 h->root.u.undef.abfd = abfd;
1432 }
1433 else
1434 {
1435 h->root.type = bfd_link_hash_new;
1436 h->root.u.undef.abfd = NULL;
1437 }
0a1b45a2 1438 return true;
c5d37467 1439 }
580a2b6e
L
1440 }
1441
4c34aff8
AM
1442 /* Check TLS symbols. We don't check undefined symbols introduced
1443 by "ld -u" which have no type (and oldbfd NULL), and we don't
1444 check symbols from plugins because they also have no type. */
1445 if (oldbfd != NULL
1446 && (oldbfd->flags & BFD_PLUGIN) == 0
1447 && (abfd->flags & BFD_PLUGIN) == 0
1448 && ELF_ST_TYPE (sym->st_info) != h->type
1449 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1450 {
1451 bfd *ntbfd, *tbfd;
0a1b45a2 1452 bool ntdef, tdef;
7479dfd4
L
1453 asection *ntsec, *tsec;
1454
1455 if (h->type == STT_TLS)
1456 {
3b36f7e6 1457 ntbfd = abfd;
7479dfd4
L
1458 ntsec = sec;
1459 ntdef = newdef;
1460 tbfd = oldbfd;
1461 tsec = oldsec;
1462 tdef = olddef;
1463 }
1464 else
1465 {
1466 ntbfd = oldbfd;
1467 ntsec = oldsec;
1468 ntdef = olddef;
1469 tbfd = abfd;
1470 tsec = sec;
1471 tdef = newdef;
1472 }
1473
1474 if (tdef && ntdef)
4eca0228 1475 _bfd_error_handler
695344c0 1476 /* xgettext:c-format */
871b3ab2
AM
1477 (_("%s: TLS definition in %pB section %pA "
1478 "mismatches non-TLS definition in %pB section %pA"),
c08bb8dd 1479 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
7479dfd4 1480 else if (!tdef && !ntdef)
4eca0228 1481 _bfd_error_handler
695344c0 1482 /* xgettext:c-format */
871b3ab2
AM
1483 (_("%s: TLS reference in %pB "
1484 "mismatches non-TLS reference in %pB"),
c08bb8dd 1485 h->root.root.string, tbfd, ntbfd);
7479dfd4 1486 else if (tdef)
4eca0228 1487 _bfd_error_handler
695344c0 1488 /* xgettext:c-format */
871b3ab2
AM
1489 (_("%s: TLS definition in %pB section %pA "
1490 "mismatches non-TLS reference in %pB"),
c08bb8dd 1491 h->root.root.string, tbfd, tsec, ntbfd);
7479dfd4 1492 else
4eca0228 1493 _bfd_error_handler
695344c0 1494 /* xgettext:c-format */
871b3ab2
AM
1495 (_("%s: TLS reference in %pB "
1496 "mismatches non-TLS definition in %pB section %pA"),
c08bb8dd 1497 h->root.root.string, tbfd, ntbfd, ntsec);
7479dfd4
L
1498
1499 bfd_set_error (bfd_error_bad_value);
0a1b45a2 1500 return false;
7479dfd4
L
1501 }
1502
45d6a902
AM
1503 /* If the old symbol has non-default visibility, we ignore the new
1504 definition from a dynamic object. */
1505 if (newdyn
9c7a29a3 1506 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1507 && !bfd_is_und_section (sec))
1508 {
0a1b45a2 1509 *skip = true;
45d6a902 1510 /* Make sure this symbol is dynamic. */
f5385ebf 1511 h->ref_dynamic = 1;
90c984fc 1512 hi->ref_dynamic = 1;
45d6a902
AM
1513 /* A protected symbol has external availability. Make sure it is
1514 recorded as dynamic.
1515
1516 FIXME: Should we check type and size for protected symbol? */
1517 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1518 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902 1519 else
0a1b45a2 1520 return true;
45d6a902
AM
1521 }
1522 else if (!newdyn
9c7a29a3 1523 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1524 && h->def_dynamic)
45d6a902
AM
1525 {
1526 /* If the new symbol with non-default visibility comes from a
1527 relocatable file and the old definition comes from a dynamic
1528 object, we remove the old definition. */
6c9b78e6 1529 if (hi->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1530 {
1531 /* Handle the case where the old dynamic definition is
1532 default versioned. We need to copy the symbol info from
1533 the symbol with default version to the normal one if it
1534 was referenced before. */
1535 if (h->ref_regular)
1536 {
6c9b78e6 1537 hi->root.type = h->root.type;
d2dee3b2 1538 h->root.type = bfd_link_hash_indirect;
6c9b78e6 1539 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
aed81c4e 1540
6c9b78e6 1541 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
aed81c4e 1542 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
d2dee3b2 1543 {
aed81c4e
MR
1544 /* If the new symbol is hidden or internal, completely undo
1545 any dynamic link state. */
0a1b45a2 1546 (*bed->elf_backend_hide_symbol) (info, h, true);
aed81c4e
MR
1547 h->forced_local = 0;
1548 h->ref_dynamic = 0;
d2dee3b2
L
1549 }
1550 else
aed81c4e
MR
1551 h->ref_dynamic = 1;
1552
1553 h->def_dynamic = 0;
aed81c4e
MR
1554 /* FIXME: Should we check type and size for protected symbol? */
1555 h->size = 0;
1556 h->type = 0;
1557
6c9b78e6 1558 h = hi;
d2dee3b2
L
1559 }
1560 else
6c9b78e6 1561 h = hi;
d2dee3b2 1562 }
1de1a317 1563
f5eda473
AM
1564 /* If the old symbol was undefined before, then it will still be
1565 on the undefs list. If the new symbol is undefined or
1566 common, we can't make it bfd_link_hash_new here, because new
1567 undefined or common symbols will be added to the undefs list
1568 by _bfd_generic_link_add_one_symbol. Symbols may not be
1569 added twice to the undefs list. Also, if the new symbol is
1570 undefweak then we don't want to lose the strong undef. */
1571 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317 1572 {
1de1a317 1573 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1574 h->root.u.undef.abfd = abfd;
1575 }
1576 else
1577 {
1578 h->root.type = bfd_link_hash_new;
1579 h->root.u.undef.abfd = NULL;
1580 }
1581
f5eda473 1582 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
252b5132 1583 {
f5eda473
AM
1584 /* If the new symbol is hidden or internal, completely undo
1585 any dynamic link state. */
0a1b45a2 1586 (*bed->elf_backend_hide_symbol) (info, h, true);
f5eda473
AM
1587 h->forced_local = 0;
1588 h->ref_dynamic = 0;
45d6a902 1589 }
f5eda473
AM
1590 else
1591 h->ref_dynamic = 1;
1592 h->def_dynamic = 0;
45d6a902
AM
1593 /* FIXME: Should we check type and size for protected symbol? */
1594 h->size = 0;
1595 h->type = 0;
0a1b45a2 1596 return true;
45d6a902 1597 }
14a793b2 1598
15b43f48
AM
1599 /* If a new weak symbol definition comes from a regular file and the
1600 old symbol comes from a dynamic library, we treat the new one as
1601 strong. Similarly, an old weak symbol definition from a regular
1602 file is treated as strong when the new symbol comes from a dynamic
1603 library. Further, an old weak symbol from a dynamic library is
1604 treated as strong if the new symbol is from a dynamic library.
1605 This reflects the way glibc's ld.so works.
1606
165f707a
AM
1607 Also allow a weak symbol to override a linker script symbol
1608 defined by an early pass over the script. This is done so the
1609 linker knows the symbol is defined in an object file, for the
1610 DEFINED script function.
1611
15b43f48
AM
1612 Do this before setting *type_change_ok or *size_change_ok so that
1613 we warn properly when dynamic library symbols are overridden. */
1614
165f707a 1615 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
0a1b45a2 1616 newweak = false;
15b43f48 1617 if (olddef && newdyn)
0a1b45a2 1618 oldweak = false;
0f8a2703 1619
d334575b 1620 /* Allow changes between different types of function symbol. */
0a36a439 1621 if (newfunc && oldfunc)
0a1b45a2 1622 *type_change_ok = true;
fcb93ecf 1623
79349b09
AM
1624 /* It's OK to change the type if either the existing symbol or the
1625 new symbol is weak. A type change is also OK if the old symbol
1626 is undefined and the new symbol is defined. */
252b5132 1627
79349b09
AM
1628 if (oldweak
1629 || newweak
1630 || (newdef
1631 && h->root.type == bfd_link_hash_undefined))
0a1b45a2 1632 *type_change_ok = true;
79349b09
AM
1633
1634 /* It's OK to change the size if either the existing symbol or the
1635 new symbol is weak, or if the old symbol is undefined. */
1636
1637 if (*type_change_ok
1638 || h->root.type == bfd_link_hash_undefined)
0a1b45a2 1639 *size_change_ok = true;
45d6a902 1640
45d6a902
AM
1641 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1642 symbol, respectively, appears to be a common symbol in a dynamic
1643 object. If a symbol appears in an uninitialized section, and is
1644 not weak, and is not a function, then it may be a common symbol
1645 which was resolved when the dynamic object was created. We want
1646 to treat such symbols specially, because they raise special
1647 considerations when setting the symbol size: if the symbol
1648 appears as a common symbol in a regular object, and the size in
1649 the regular object is larger, we must make sure that we use the
1650 larger size. This problematic case can always be avoided in C,
1651 but it must be handled correctly when using Fortran shared
1652 libraries.
1653
1654 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1655 likewise for OLDDYNCOMMON and OLDDEF.
1656
1657 Note that this test is just a heuristic, and that it is quite
1658 possible to have an uninitialized symbol in a shared object which
1659 is really a definition, rather than a common symbol. This could
1660 lead to some minor confusion when the symbol really is a common
1661 symbol in some regular object. However, I think it will be
1662 harmless. */
1663
1664 if (newdyn
1665 && newdef
79349b09 1666 && !newweak
45d6a902
AM
1667 && (sec->flags & SEC_ALLOC) != 0
1668 && (sec->flags & SEC_LOAD) == 0
1669 && sym->st_size > 0
0a36a439 1670 && !newfunc)
0a1b45a2 1671 newdyncommon = true;
45d6a902 1672 else
0a1b45a2 1673 newdyncommon = false;
45d6a902
AM
1674
1675 if (olddyn
1676 && olddef
1677 && h->root.type == bfd_link_hash_defined
f5385ebf 1678 && h->def_dynamic
45d6a902
AM
1679 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1680 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1681 && h->size > 0
0a36a439 1682 && !oldfunc)
0a1b45a2 1683 olddyncommon = true;
45d6a902 1684 else
0a1b45a2 1685 olddyncommon = false;
45d6a902 1686
a4d8e49b
L
1687 /* We now know everything about the old and new symbols. We ask the
1688 backend to check if we can merge them. */
5d13b3b3
AM
1689 if (bed->merge_symbol != NULL)
1690 {
1691 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
0a1b45a2 1692 return false;
5d13b3b3
AM
1693 sec = *psec;
1694 }
a4d8e49b 1695
a83ef4d1
L
1696 /* There are multiple definitions of a normal symbol. Skip the
1697 default symbol as well as definition from an IR object. */
93f4de39 1698 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
a83ef4d1
L
1699 && !default_sym && h->def_regular
1700 && !(oldbfd != NULL
1701 && (oldbfd->flags & BFD_PLUGIN) != 0
1702 && (abfd->flags & BFD_PLUGIN) == 0))
93f4de39
RL
1703 {
1704 /* Handle a multiple definition. */
1705 (*info->callbacks->multiple_definition) (info, &h->root,
1706 abfd, sec, *pvalue);
0a1b45a2
AM
1707 *skip = true;
1708 return true;
93f4de39
RL
1709 }
1710
45d6a902
AM
1711 /* If both the old and the new symbols look like common symbols in a
1712 dynamic object, set the size of the symbol to the larger of the
1713 two. */
1714
1715 if (olddyncommon
1716 && newdyncommon
1717 && sym->st_size != h->size)
1718 {
1719 /* Since we think we have two common symbols, issue a multiple
1720 common warning if desired. Note that we only warn if the
1721 size is different. If the size is the same, we simply let
1722 the old symbol override the new one as normally happens with
1723 symbols defined in dynamic objects. */
1724
1a72702b
AM
1725 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1726 bfd_link_hash_common, sym->st_size);
45d6a902
AM
1727 if (sym->st_size > h->size)
1728 h->size = sym->st_size;
252b5132 1729
0a1b45a2 1730 *size_change_ok = true;
252b5132
RH
1731 }
1732
45d6a902
AM
1733 /* If we are looking at a dynamic object, and we have found a
1734 definition, we need to see if the symbol was already defined by
1735 some other object. If so, we want to use the existing
1736 definition, and we do not want to report a multiple symbol
1737 definition error; we do this by clobbering *PSEC to be
1738 bfd_und_section_ptr.
1739
1740 We treat a common symbol as a definition if the symbol in the
1741 shared library is a function, since common symbols always
1742 represent variables; this can cause confusion in principle, but
1743 any such confusion would seem to indicate an erroneous program or
1744 shared library. We also permit a common symbol in a regular
8170f769 1745 object to override a weak symbol in a shared object. */
45d6a902
AM
1746
1747 if (newdyn
1748 && newdef
77cfaee6 1749 && (olddef
45d6a902 1750 || (h->root.type == bfd_link_hash_common
8170f769 1751 && (newweak || newfunc))))
45d6a902 1752 {
7ba11550 1753 *override = abfd;
0a1b45a2
AM
1754 newdef = false;
1755 newdyncommon = false;
252b5132 1756
45d6a902 1757 *psec = sec = bfd_und_section_ptr;
0a1b45a2 1758 *size_change_ok = true;
252b5132 1759
45d6a902
AM
1760 /* If we get here when the old symbol is a common symbol, then
1761 we are explicitly letting it override a weak symbol or
1762 function in a dynamic object, and we don't want to warn about
1763 a type change. If the old symbol is a defined symbol, a type
1764 change warning may still be appropriate. */
252b5132 1765
45d6a902 1766 if (h->root.type == bfd_link_hash_common)
0a1b45a2 1767 *type_change_ok = true;
45d6a902
AM
1768 }
1769
1770 /* Handle the special case of an old common symbol merging with a
1771 new symbol which looks like a common symbol in a shared object.
1772 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1773 common symbol, and let _bfd_generic_link_add_one_symbol do the
1774 right thing. */
45d6a902
AM
1775
1776 if (newdyncommon
1777 && h->root.type == bfd_link_hash_common)
1778 {
7ba11550 1779 *override = oldbfd;
0a1b45a2
AM
1780 newdef = false;
1781 newdyncommon = false;
45d6a902 1782 *pvalue = sym->st_size;
a4d8e49b 1783 *psec = sec = bed->common_section (oldsec);
0a1b45a2 1784 *size_change_ok = true;
45d6a902
AM
1785 }
1786
c5e2cead 1787 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1788 if (newdef && olddef && newweak)
54ac0771 1789 {
35ed3f94 1790 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1791 if (!(oldbfd != NULL
1792 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94 1793 && (abfd->flags & BFD_PLUGIN) == 0))
57fa7b8c 1794 {
0a1b45a2
AM
1795 newdef = false;
1796 *skip = true;
57fa7b8c 1797 }
54ac0771
L
1798
1799 /* Merge st_other. If the symbol already has a dynamic index,
1800 but visibility says it should not be visible, turn it into a
1801 local symbol. */
5160d0f3 1802 elf_merge_st_other (abfd, h, sym->st_other, sec, newdef, newdyn);
54ac0771
L
1803 if (h->dynindx != -1)
1804 switch (ELF_ST_VISIBILITY (h->other))
1805 {
1806 case STV_INTERNAL:
1807 case STV_HIDDEN:
0a1b45a2 1808 (*bed->elf_backend_hide_symbol) (info, h, true);
54ac0771
L
1809 break;
1810 }
1811 }
c5e2cead 1812
45d6a902
AM
1813 /* If the old symbol is from a dynamic object, and the new symbol is
1814 a definition which is not from a dynamic object, then the new
1815 symbol overrides the old symbol. Symbols from regular files
1816 always take precedence over symbols from dynamic objects, even if
1817 they are defined after the dynamic object in the link.
1818
1819 As above, we again permit a common symbol in a regular object to
1820 override a definition in a shared object if the shared object
0f8a2703 1821 symbol is a function or is weak. */
45d6a902
AM
1822
1823 flip = NULL;
77cfaee6 1824 if (!newdyn
45d6a902
AM
1825 && (newdef
1826 || (bfd_is_com_section (sec)
0a36a439 1827 && (oldweak || oldfunc)))
45d6a902
AM
1828 && olddyn
1829 && olddef
f5385ebf 1830 && h->def_dynamic)
45d6a902
AM
1831 {
1832 /* Change the hash table entry to undefined, and let
1833 _bfd_generic_link_add_one_symbol do the right thing with the
1834 new definition. */
1835
1836 h->root.type = bfd_link_hash_undefined;
1837 h->root.u.undef.abfd = h->root.u.def.section->owner;
0a1b45a2 1838 *size_change_ok = true;
45d6a902 1839
0a1b45a2
AM
1840 olddef = false;
1841 olddyncommon = false;
45d6a902
AM
1842
1843 /* We again permit a type change when a common symbol may be
1844 overriding a function. */
1845
1846 if (bfd_is_com_section (sec))
0a36a439
L
1847 {
1848 if (oldfunc)
1849 {
1850 /* If a common symbol overrides a function, make sure
1851 that it isn't defined dynamically nor has type
1852 function. */
1853 h->def_dynamic = 0;
1854 h->type = STT_NOTYPE;
1855 }
0a1b45a2 1856 *type_change_ok = true;
0a36a439 1857 }
45d6a902 1858
6c9b78e6
AM
1859 if (hi->root.type == bfd_link_hash_indirect)
1860 flip = hi;
45d6a902
AM
1861 else
1862 /* This union may have been set to be non-NULL when this symbol
1863 was seen in a dynamic object. We must force the union to be
1864 NULL, so that it is correct for a regular symbol. */
1865 h->verinfo.vertree = NULL;
1866 }
1867
1868 /* Handle the special case of a new common symbol merging with an
1869 old symbol that looks like it might be a common symbol defined in
1870 a shared object. Note that we have already handled the case in
1871 which a new common symbol should simply override the definition
1872 in the shared library. */
1873
1874 if (! newdyn
1875 && bfd_is_com_section (sec)
1876 && olddyncommon)
1877 {
1878 /* It would be best if we could set the hash table entry to a
1879 common symbol, but we don't know what to use for the section
1880 or the alignment. */
1a72702b
AM
1881 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1882 bfd_link_hash_common, sym->st_size);
45d6a902 1883
4cc11e76 1884 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1885 larger, pretend that the new symbol has its size. */
1886
1887 if (h->size > *pvalue)
1888 *pvalue = h->size;
1889
af44c138
L
1890 /* We need to remember the alignment required by the symbol
1891 in the dynamic object. */
1892 BFD_ASSERT (pold_alignment);
1893 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902 1894
0a1b45a2
AM
1895 olddef = false;
1896 olddyncommon = false;
45d6a902
AM
1897
1898 h->root.type = bfd_link_hash_undefined;
1899 h->root.u.undef.abfd = h->root.u.def.section->owner;
1900
0a1b45a2
AM
1901 *size_change_ok = true;
1902 *type_change_ok = true;
45d6a902 1903
6c9b78e6
AM
1904 if (hi->root.type == bfd_link_hash_indirect)
1905 flip = hi;
45d6a902
AM
1906 else
1907 h->verinfo.vertree = NULL;
1908 }
1909
1910 if (flip != NULL)
1911 {
1912 /* Handle the case where we had a versioned symbol in a dynamic
1913 library and now find a definition in a normal object. In this
1914 case, we make the versioned symbol point to the normal one. */
45d6a902 1915 flip->root.type = h->root.type;
00cbee0a 1916 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1917 h->root.type = bfd_link_hash_indirect;
1918 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1919 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1920 if (h->def_dynamic)
45d6a902 1921 {
f5385ebf
AM
1922 h->def_dynamic = 0;
1923 flip->ref_dynamic = 1;
45d6a902
AM
1924 }
1925 }
1926
0a1b45a2 1927 return true;
45d6a902
AM
1928}
1929
1930/* This function is called to create an indirect symbol from the
1931 default for the symbol with the default version if needed. The
4f3fedcf 1932 symbol is described by H, NAME, SYM, SEC, and VALUE. We
0f8a2703 1933 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1934
0a1b45a2 1935static bool
268b6b39
AM
1936_bfd_elf_add_default_symbol (bfd *abfd,
1937 struct bfd_link_info *info,
1938 struct elf_link_hash_entry *h,
1939 const char *name,
1940 Elf_Internal_Sym *sym,
4f3fedcf
AM
1941 asection *sec,
1942 bfd_vma value,
1943 bfd **poldbfd,
0a1b45a2 1944 bool *dynsym)
45d6a902 1945{
0a1b45a2
AM
1946 bool type_change_ok;
1947 bool size_change_ok;
1948 bool skip;
45d6a902
AM
1949 char *shortname;
1950 struct elf_link_hash_entry *hi;
1951 struct bfd_link_hash_entry *bh;
9c5bfbb7 1952 const struct elf_backend_data *bed;
0a1b45a2
AM
1953 bool collect;
1954 bool dynamic;
f01fb44c 1955 bfd *override;
45d6a902
AM
1956 char *p;
1957 size_t len, shortlen;
ffd65175 1958 asection *tmp_sec;
0a1b45a2 1959 bool matched;
45d6a902 1960
422f1182 1961 if (h->versioned == unversioned || h->versioned == versioned_hidden)
0a1b45a2 1962 return true;
422f1182 1963
45d6a902
AM
1964 /* If this symbol has a version, and it is the default version, we
1965 create an indirect symbol from the default name to the fully
1966 decorated name. This will cause external references which do not
1967 specify a version to be bound to this version of the symbol. */
1968 p = strchr (name, ELF_VER_CHR);
422f1182
L
1969 if (h->versioned == unknown)
1970 {
1971 if (p == NULL)
1972 {
1973 h->versioned = unversioned;
0a1b45a2 1974 return true;
422f1182
L
1975 }
1976 else
1977 {
1978 if (p[1] != ELF_VER_CHR)
1979 {
1980 h->versioned = versioned_hidden;
0a1b45a2 1981 return true;
422f1182
L
1982 }
1983 else
1984 h->versioned = versioned;
1985 }
1986 }
4373f8af
L
1987 else
1988 {
1989 /* PR ld/19073: We may see an unversioned definition after the
1990 default version. */
1991 if (p == NULL)
0a1b45a2 1992 return true;
4373f8af 1993 }
45d6a902 1994
45d6a902
AM
1995 bed = get_elf_backend_data (abfd);
1996 collect = bed->collect;
1997 dynamic = (abfd->flags & DYNAMIC) != 0;
1998
1999 shortlen = p - name;
a50b1753 2000 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902 2001 if (shortname == NULL)
0a1b45a2 2002 return false;
45d6a902
AM
2003 memcpy (shortname, name, shortlen);
2004 shortname[shortlen] = '\0';
2005
2006 /* We are going to create a new symbol. Merge it with any existing
2007 symbol with this name. For the purposes of the merge, act as
2008 though we were defining the symbol we just defined, although we
2009 actually going to define an indirect symbol. */
0a1b45a2
AM
2010 type_change_ok = false;
2011 size_change_ok = false;
2012 matched = true;
ffd65175
AM
2013 tmp_sec = sec;
2014 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
f01fb44c 2015 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 2016 &type_change_ok, &size_change_ok, &matched))
0a1b45a2 2017 return false;
45d6a902
AM
2018
2019 if (skip)
2020 goto nondefault;
2021
5fa370e4 2022 if (hi->def_regular || ELF_COMMON_DEF_P (hi))
5b677558
AM
2023 {
2024 /* If the undecorated symbol will have a version added by a
2025 script different to H, then don't indirect to/from the
2026 undecorated symbol. This isn't ideal because we may not yet
2027 have seen symbol versions, if given by a script on the
2028 command line rather than via --version-script. */
2029 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
2030 {
0a1b45a2 2031 bool hide;
5b677558
AM
2032
2033 hi->verinfo.vertree
2034 = bfd_find_version_for_sym (info->version_info,
2035 hi->root.root.string, &hide);
2036 if (hi->verinfo.vertree != NULL && hide)
2037 {
0a1b45a2 2038 (*bed->elf_backend_hide_symbol) (info, hi, true);
5b677558
AM
2039 goto nondefault;
2040 }
2041 }
2042 if (hi->verinfo.vertree != NULL
2043 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
f01fb44c 2044 goto nondefault;
5b677558
AM
2045 }
2046
f01fb44c 2047 if (! override)
45d6a902 2048 {
c6e8a9a8 2049 /* Add the default symbol if not performing a relocatable link. */
0e1862bb 2050 if (! bfd_link_relocatable (info))
c6e8a9a8
L
2051 {
2052 bh = &hi->root;
fbcc8baf 2053 if (bh->type == bfd_link_hash_defined
6cc71b82 2054 && bh->u.def.section->owner != NULL
fbcc8baf
L
2055 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
2056 {
2057 /* Mark the previous definition from IR object as
2058 undefined so that the generic linker will override
2059 it. */
2060 bh->type = bfd_link_hash_undefined;
2061 bh->u.undef.abfd = bh->u.def.section->owner;
2062 }
c6e8a9a8
L
2063 if (! (_bfd_generic_link_add_one_symbol
2064 (info, abfd, shortname, BSF_INDIRECT,
2065 bfd_ind_section_ptr,
0a1b45a2
AM
2066 0, name, false, collect, &bh)))
2067 return false;
c6e8a9a8
L
2068 hi = (struct elf_link_hash_entry *) bh;
2069 }
45d6a902
AM
2070 }
2071 else
2072 {
2073 /* In this case the symbol named SHORTNAME is overriding the
2074 indirect symbol we want to add. We were planning on making
2075 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
2076 is the name without a version. NAME is the fully versioned
2077 name, and it is the default version.
2078
2079 Overriding means that we already saw a definition for the
2080 symbol SHORTNAME in a regular object, and it is overriding
2081 the symbol defined in the dynamic object.
2082
2083 When this happens, we actually want to change NAME, the
2084 symbol we just added, to refer to SHORTNAME. This will cause
2085 references to NAME in the shared object to become references
2086 to SHORTNAME in the regular object. This is what we expect
2087 when we override a function in a shared object: that the
2088 references in the shared object will be mapped to the
2089 definition in the regular object. */
2090
2091 while (hi->root.type == bfd_link_hash_indirect
2092 || hi->root.type == bfd_link_hash_warning)
2093 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2094
2095 h->root.type = bfd_link_hash_indirect;
2096 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 2097 if (h->def_dynamic)
45d6a902 2098 {
f5385ebf
AM
2099 h->def_dynamic = 0;
2100 hi->ref_dynamic = 1;
2101 if (hi->ref_regular
2102 || hi->def_regular)
45d6a902 2103 {
c152c796 2104 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
0a1b45a2 2105 return false;
45d6a902
AM
2106 }
2107 }
2108
2109 /* Now set HI to H, so that the following code will set the
2110 other fields correctly. */
2111 hi = h;
2112 }
2113
fab4a87f
L
2114 /* Check if HI is a warning symbol. */
2115 if (hi->root.type == bfd_link_hash_warning)
2116 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2117
45d6a902
AM
2118 /* If there is a duplicate definition somewhere, then HI may not
2119 point to an indirect symbol. We will have reported an error to
2120 the user in that case. */
2121
2122 if (hi->root.type == bfd_link_hash_indirect)
2123 {
2124 struct elf_link_hash_entry *ht;
2125
45d6a902 2126 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 2127 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902 2128
5160d0f3
AM
2129 /* If we first saw a reference to SHORTNAME with non-default
2130 visibility, merge that visibility to the @@VER symbol. */
0a1b45a2 2131 elf_merge_st_other (abfd, ht, hi->other, sec, true, dynamic);
5160d0f3 2132
68c88cd4
AM
2133 /* A reference to the SHORTNAME symbol from a dynamic library
2134 will be satisfied by the versioned symbol at runtime. In
2135 effect, we have a reference to the versioned symbol. */
2136 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2137 hi->dynamic_def |= ht->dynamic_def;
2138
45d6a902
AM
2139 /* See if the new flags lead us to realize that the symbol must
2140 be dynamic. */
2141 if (! *dynsym)
2142 {
2143 if (! dynamic)
2144 {
0e1862bb 2145 if (! bfd_link_executable (info)
90c984fc 2146 || hi->def_dynamic
f5385ebf 2147 || hi->ref_dynamic)
0a1b45a2 2148 *dynsym = true;
45d6a902
AM
2149 }
2150 else
2151 {
f5385ebf 2152 if (hi->ref_regular)
0a1b45a2 2153 *dynsym = true;
45d6a902
AM
2154 }
2155 }
2156 }
2157
2158 /* We also need to define an indirection from the nondefault version
2159 of the symbol. */
2160
dc1e8a47 2161 nondefault:
45d6a902 2162 len = strlen (name);
a50b1753 2163 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902 2164 if (shortname == NULL)
0a1b45a2 2165 return false;
45d6a902
AM
2166 memcpy (shortname, name, shortlen);
2167 memcpy (shortname + shortlen, p + 1, len - shortlen);
2168
2169 /* Once again, merge with any existing symbol. */
0a1b45a2
AM
2170 type_change_ok = false;
2171 size_change_ok = false;
ffd65175
AM
2172 tmp_sec = sec;
2173 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
f01fb44c 2174 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 2175 &type_change_ok, &size_change_ok, &matched))
0a1b45a2 2176 return false;
45d6a902
AM
2177
2178 if (skip)
726d7d1e
AM
2179 {
2180 if (!dynamic
2181 && h->root.type == bfd_link_hash_defweak
2182 && hi->root.type == bfd_link_hash_defined)
2183 {
2184 /* We are handling a weak sym@@ver and attempting to define
2185 a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2186 new weak sym@ver because there is already a strong sym@ver.
2187 However, sym@ver and sym@@ver are really the same symbol.
2188 The existing strong sym@ver ought to override sym@@ver. */
2189 h->root.type = bfd_link_hash_defined;
2190 h->root.u.def.section = hi->root.u.def.section;
2191 h->root.u.def.value = hi->root.u.def.value;
2192 hi->root.type = bfd_link_hash_indirect;
2193 hi->root.u.i.link = &h->root;
2194 }
2195 else
0a1b45a2 2196 return true;
726d7d1e 2197 }
f01fb44c 2198 else if (override)
45d6a902
AM
2199 {
2200 /* Here SHORTNAME is a versioned name, so we don't expect to see
2201 the type of override we do in the case above unless it is
4cc11e76 2202 overridden by a versioned definition. */
45d6a902
AM
2203 if (hi->root.type != bfd_link_hash_defined
2204 && hi->root.type != bfd_link_hash_defweak)
4eca0228 2205 _bfd_error_handler
695344c0 2206 /* xgettext:c-format */
871b3ab2 2207 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
d003868e 2208 abfd, shortname);
0a1b45a2 2209 return true;
45d6a902
AM
2210 }
2211 else
2212 {
2213 bh = &hi->root;
2214 if (! (_bfd_generic_link_add_one_symbol
2215 (info, abfd, shortname, BSF_INDIRECT,
0a1b45a2
AM
2216 bfd_ind_section_ptr, 0, name, false, collect, &bh)))
2217 return false;
45d6a902 2218 hi = (struct elf_link_hash_entry *) bh;
726d7d1e 2219 }
45d6a902 2220
726d7d1e
AM
2221 /* If there is a duplicate definition somewhere, then HI may not
2222 point to an indirect symbol. We will have reported an error
2223 to the user in that case. */
2224 if (hi->root.type == bfd_link_hash_indirect)
2225 {
2226 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2227 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2228 hi->dynamic_def |= h->dynamic_def;
45d6a902 2229
726d7d1e
AM
2230 /* If we first saw a reference to @VER symbol with
2231 non-default visibility, merge that visibility to the
2232 @@VER symbol. */
0a1b45a2 2233 elf_merge_st_other (abfd, h, hi->other, sec, true, dynamic);
726d7d1e
AM
2234
2235 /* See if the new flags lead us to realize that the symbol
2236 must be dynamic. */
2237 if (! *dynsym)
45d6a902 2238 {
726d7d1e 2239 if (! dynamic)
45d6a902 2240 {
726d7d1e
AM
2241 if (! bfd_link_executable (info)
2242 || hi->ref_dynamic)
0a1b45a2 2243 *dynsym = true;
726d7d1e
AM
2244 }
2245 else
2246 {
2247 if (hi->ref_regular)
0a1b45a2 2248 *dynsym = true;
45d6a902
AM
2249 }
2250 }
2251 }
2252
0a1b45a2 2253 return true;
45d6a902
AM
2254}
2255\f
2256/* This routine is used to export all defined symbols into the dynamic
2257 symbol table. It is called via elf_link_hash_traverse. */
2258
0a1b45a2 2259static bool
268b6b39 2260_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2261{
a50b1753 2262 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
2263
2264 /* Ignore indirect symbols. These are added by the versioning code. */
2265 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 2266 return true;
45d6a902 2267
7686d77d
AM
2268 /* Ignore this if we won't export it. */
2269 if (!eif->info->export_dynamic && !h->dynamic)
0a1b45a2 2270 return true;
45d6a902
AM
2271
2272 if (h->dynindx == -1
fd91d419
L
2273 && (h->def_regular || h->ref_regular)
2274 && ! bfd_hide_sym_by_version (eif->info->version_info,
2275 h->root.root.string))
45d6a902 2276 {
fd91d419 2277 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 2278 {
0a1b45a2
AM
2279 eif->failed = true;
2280 return false;
45d6a902
AM
2281 }
2282 }
2283
0a1b45a2 2284 return true;
45d6a902
AM
2285}
2286\f
91673042
L
2287/* Return the glibc version reference if VERSION_DEP is added to the
2288 list of glibc version dependencies successfully. VERSION_DEP will
7757f66f
L
2289 be put into the .gnu.version_r section. GLIBC_MINOR_BASE is the
2290 pointer to the glibc minor base version. */
91673042
L
2291
2292static Elf_Internal_Verneed *
2293elf_link_add_glibc_verneed (struct elf_find_verdep_info *rinfo,
2294 Elf_Internal_Verneed *glibc_verref,
7757f66f
L
2295 const char *version_dep,
2296 int *glibc_minor_base)
72aa8173 2297{
72aa8173
L
2298 Elf_Internal_Verneed *t;
2299 Elf_Internal_Vernaux *a;
2300 size_t amt;
7757f66f 2301 int minor_version = -1;
72aa8173 2302
91673042 2303 if (glibc_verref != NULL)
72aa8173 2304 {
91673042 2305 t = glibc_verref;
72aa8173
L
2306
2307 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2308 {
91673042
L
2309 /* Return if VERSION_DEP dependency has been added. */
2310 if (a->vna_nodename == version_dep
2311 || strcmp (a->vna_nodename, version_dep) == 0)
2312 return t;
72aa8173 2313 }
72aa8173 2314 }
91673042
L
2315 else
2316 {
91673042
L
2317 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2318 t != NULL;
2319 t = t->vn_nextref)
2320 {
2321 const char *soname = bfd_elf_get_dt_soname (t->vn_bfd);
2322 if (soname != NULL && startswith (soname, "libc.so."))
2323 break;
2324 }
72aa8173 2325
91673042 2326 /* Skip the shared library if it isn't libc.so. */
72aa8173 2327 if (t == NULL)
91673042
L
2328 return t;
2329
91673042 2330 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
72aa8173 2331 {
91673042
L
2332 /* Return if VERSION_DEP dependency has been added. */
2333 if (a->vna_nodename == version_dep
2334 || strcmp (a->vna_nodename, version_dep) == 0)
2335 return t;
2336
2337 /* Check if libc.so provides GLIBC_2.XX version. */
7757f66f
L
2338 if (startswith (a->vna_nodename, "GLIBC_2."))
2339 {
2340 minor_version = strtol (a->vna_nodename + 8, NULL, 10);
2341 if (minor_version < *glibc_minor_base)
2342 *glibc_minor_base = minor_version;
2343 }
72aa8173
L
2344 }
2345
91673042 2346 /* Skip if it isn't linked against glibc. */
7757f66f
L
2347 if (minor_version < 0)
2348 return NULL;
2349 }
2350
2351 /* Skip if 2.GLIBC_MINOR_BASE includes VERSION_DEP. */
2352 if (startswith (version_dep, "GLIBC_2."))
2353 {
2354 minor_version = strtol (version_dep + 8, NULL, 10);
2355 if (minor_version <= *glibc_minor_base)
91673042 2356 return NULL;
72aa8173
L
2357 }
2358
2359 amt = sizeof *a;
2360 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2361 if (a == NULL)
2362 {
2363 rinfo->failed = true;
91673042 2364 return NULL;
72aa8173
L
2365 }
2366
91673042 2367 a->vna_nodename = version_dep;
72aa8173
L
2368 a->vna_flags = 0;
2369 a->vna_nextptr = t->vn_auxptr;
2370 a->vna_other = rinfo->vers + 1;
2371 ++rinfo->vers;
2372
2373 t->vn_auxptr = a;
2374
91673042
L
2375 return t;
2376}
2377
2378/* Add VERSION_DEP to the list of version dependencies when linked
2379 against glibc. */
2380
2381void
2382_bfd_elf_link_add_glibc_version_dependency
2383 (struct elf_find_verdep_info *rinfo,
2384 const char *version_dep[])
2385{
2386 Elf_Internal_Verneed *t = NULL;
7757f66f 2387 int glibc_minor_base = INT_MAX;
91673042
L
2388
2389 do
2390 {
7757f66f
L
2391 t = elf_link_add_glibc_verneed (rinfo, t, *version_dep,
2392 &glibc_minor_base);
91673042
L
2393 /* Return if there is no glibc version reference. */
2394 if (t == NULL)
2395 return;
2396 version_dep++;
2397 }
2398 while (*version_dep != NULL);
2399}
2400
2401/* Add GLIBC_ABI_DT_RELR to the list of version dependencies when
2402 linked against glibc. */
2403
2404void
2405_bfd_elf_link_add_dt_relr_dependency (struct elf_find_verdep_info *rinfo)
2406{
2407 if (rinfo->info->enable_dt_relr)
2408 {
2409 const char *version[] =
2410 {
2411 "GLIBC_ABI_DT_RELR",
2412 NULL
2413 };
2414 _bfd_elf_link_add_glibc_version_dependency (rinfo, version);
2415 }
72aa8173
L
2416}
2417
45d6a902
AM
2418/* Look through the symbols which are defined in other shared
2419 libraries and referenced here. Update the list of version
2420 dependencies. This will be put into the .gnu.version_r section.
2421 This function is called via elf_link_hash_traverse. */
2422
0a1b45a2 2423static bool
268b6b39
AM
2424_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2425 void *data)
45d6a902 2426{
a50b1753 2427 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
2428 Elf_Internal_Verneed *t;
2429 Elf_Internal_Vernaux *a;
986f0783 2430 size_t amt;
45d6a902 2431
45d6a902
AM
2432 /* We only care about symbols defined in shared objects with version
2433 information. */
f5385ebf
AM
2434 if (!h->def_dynamic
2435 || h->def_regular
45d6a902 2436 || h->dynindx == -1
7b20f099
AM
2437 || h->verinfo.verdef == NULL
2438 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2439 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
0a1b45a2 2440 return true;
45d6a902
AM
2441
2442 /* See if we already know about this version. */
28caa186
AM
2443 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2444 t != NULL;
2445 t = t->vn_nextref)
45d6a902
AM
2446 {
2447 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2448 continue;
2449
2450 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2451 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
0a1b45a2 2452 return true;
45d6a902
AM
2453
2454 break;
2455 }
2456
2457 /* This is a new version. Add it to tree we are building. */
2458
2459 if (t == NULL)
2460 {
2461 amt = sizeof *t;
a50b1753 2462 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
2463 if (t == NULL)
2464 {
0a1b45a2
AM
2465 rinfo->failed = true;
2466 return false;
45d6a902
AM
2467 }
2468
2469 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
2470 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2471 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
2472 }
2473
2474 amt = sizeof *a;
a50b1753 2475 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
2476 if (a == NULL)
2477 {
0a1b45a2
AM
2478 rinfo->failed = true;
2479 return false;
14b1c01e 2480 }
45d6a902
AM
2481
2482 /* Note that we are copying a string pointer here, and testing it
2483 above. If bfd_elf_string_from_elf_section is ever changed to
2484 discard the string data when low in memory, this will have to be
2485 fixed. */
2486 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2487
2488 a->vna_flags = h->verinfo.verdef->vd_flags;
2489 a->vna_nextptr = t->vn_auxptr;
2490
2491 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2492 ++rinfo->vers;
2493
2494 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2495
2496 t->vn_auxptr = a;
2497
0a1b45a2 2498 return true;
45d6a902
AM
2499}
2500
099bb8fb
L
2501/* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2502 hidden. Set *T_P to NULL if there is no match. */
2503
0a1b45a2 2504static bool
099bb8fb
L
2505_bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2506 struct elf_link_hash_entry *h,
2507 const char *version_p,
2508 struct bfd_elf_version_tree **t_p,
0a1b45a2 2509 bool *hide)
099bb8fb
L
2510{
2511 struct bfd_elf_version_tree *t;
2512
2513 /* Look for the version. If we find it, it is no longer weak. */
2514 for (t = info->version_info; t != NULL; t = t->next)
2515 {
2516 if (strcmp (t->name, version_p) == 0)
2517 {
2518 size_t len;
2519 char *alc;
2520 struct bfd_elf_version_expr *d;
2521
2522 len = version_p - h->root.root.string;
2523 alc = (char *) bfd_malloc (len);
2524 if (alc == NULL)
0a1b45a2 2525 return false;
099bb8fb
L
2526 memcpy (alc, h->root.root.string, len - 1);
2527 alc[len - 1] = '\0';
2528 if (alc[len - 2] == ELF_VER_CHR)
2529 alc[len - 2] = '\0';
2530
2531 h->verinfo.vertree = t;
0a1b45a2 2532 t->used = true;
099bb8fb
L
2533 d = NULL;
2534
2535 if (t->globals.list != NULL)
2536 d = (*t->match) (&t->globals, NULL, alc);
2537
2538 /* See if there is anything to force this symbol to
2539 local scope. */
2540 if (d == NULL && t->locals.list != NULL)
2541 {
2542 d = (*t->match) (&t->locals, NULL, alc);
2543 if (d != NULL
2544 && h->dynindx != -1
2545 && ! info->export_dynamic)
0a1b45a2 2546 *hide = true;
099bb8fb
L
2547 }
2548
2549 free (alc);
2550 break;
2551 }
2552 }
2553
2554 *t_p = t;
2555
0a1b45a2 2556 return true;
099bb8fb
L
2557}
2558
2559/* Return TRUE if the symbol H is hidden by version script. */
2560
0a1b45a2 2561bool
099bb8fb
L
2562_bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2563 struct elf_link_hash_entry *h)
2564{
2565 const char *p;
0a1b45a2 2566 bool hide = false;
099bb8fb
L
2567 const struct elf_backend_data *bed
2568 = get_elf_backend_data (info->output_bfd);
2569
2570 /* Version script only hides symbols defined in regular objects. */
2571 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
0a1b45a2 2572 return true;
099bb8fb
L
2573
2574 p = strchr (h->root.root.string, ELF_VER_CHR);
2575 if (p != NULL && h->verinfo.vertree == NULL)
2576 {
2577 struct bfd_elf_version_tree *t;
2578
2579 ++p;
2580 if (*p == ELF_VER_CHR)
2581 ++p;
2582
2583 if (*p != '\0'
2584 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2585 && hide)
2586 {
2587 if (hide)
0a1b45a2
AM
2588 (*bed->elf_backend_hide_symbol) (info, h, true);
2589 return true;
099bb8fb
L
2590 }
2591 }
2592
2593 /* If we don't have a version for this symbol, see if we can find
2594 something. */
2595 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2596 {
2597 h->verinfo.vertree
2598 = bfd_find_version_for_sym (info->version_info,
2599 h->root.root.string, &hide);
2600 if (h->verinfo.vertree != NULL && hide)
2601 {
0a1b45a2
AM
2602 (*bed->elf_backend_hide_symbol) (info, h, true);
2603 return true;
099bb8fb
L
2604 }
2605 }
2606
0a1b45a2 2607 return false;
099bb8fb
L
2608}
2609
45d6a902
AM
2610/* Figure out appropriate versions for all the symbols. We may not
2611 have the version number script until we have read all of the input
2612 files, so until that point we don't know which symbols should be
2613 local. This function is called via elf_link_hash_traverse. */
2614
0a1b45a2 2615static bool
268b6b39 2616_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 2617{
28caa186 2618 struct elf_info_failed *sinfo;
45d6a902 2619 struct bfd_link_info *info;
9c5bfbb7 2620 const struct elf_backend_data *bed;
45d6a902
AM
2621 struct elf_info_failed eif;
2622 char *p;
0a1b45a2 2623 bool hide;
45d6a902 2624
a50b1753 2625 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
2626 info = sinfo->info;
2627
45d6a902 2628 /* Fix the symbol flags. */
0a1b45a2 2629 eif.failed = false;
45d6a902
AM
2630 eif.info = info;
2631 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2632 {
2633 if (eif.failed)
0a1b45a2
AM
2634 sinfo->failed = true;
2635 return false;
45d6a902
AM
2636 }
2637
0a640d71
L
2638 bed = get_elf_backend_data (info->output_bfd);
2639
45d6a902
AM
2640 /* We only need version numbers for symbols defined in regular
2641 objects. */
5fa370e4 2642 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
0a640d71
L
2643 {
2644 /* Hide symbols defined in discarded input sections. */
2645 if ((h->root.type == bfd_link_hash_defined
2646 || h->root.type == bfd_link_hash_defweak)
2647 && discarded_section (h->root.u.def.section))
0a1b45a2
AM
2648 (*bed->elf_backend_hide_symbol) (info, h, true);
2649 return true;
0a640d71 2650 }
45d6a902 2651
0a1b45a2 2652 hide = false;
45d6a902
AM
2653 p = strchr (h->root.root.string, ELF_VER_CHR);
2654 if (p != NULL && h->verinfo.vertree == NULL)
2655 {
2656 struct bfd_elf_version_tree *t;
45d6a902 2657
45d6a902
AM
2658 ++p;
2659 if (*p == ELF_VER_CHR)
6e33951e 2660 ++p;
45d6a902
AM
2661
2662 /* If there is no version string, we can just return out. */
2663 if (*p == '\0')
0a1b45a2 2664 return true;
45d6a902 2665
099bb8fb 2666 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
45d6a902 2667 {
0a1b45a2
AM
2668 sinfo->failed = true;
2669 return false;
45d6a902
AM
2670 }
2671
099bb8fb 2672 if (hide)
0a1b45a2 2673 (*bed->elf_backend_hide_symbol) (info, h, true);
099bb8fb 2674
45d6a902
AM
2675 /* If we are building an application, we need to create a
2676 version node for this version. */
0e1862bb 2677 if (t == NULL && bfd_link_executable (info))
45d6a902
AM
2678 {
2679 struct bfd_elf_version_tree **pp;
2680 int version_index;
2681
2682 /* If we aren't going to export this symbol, we don't need
2683 to worry about it. */
2684 if (h->dynindx == -1)
0a1b45a2 2685 return true;
45d6a902 2686
ef53be89
AM
2687 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2688 sizeof *t);
45d6a902
AM
2689 if (t == NULL)
2690 {
0a1b45a2
AM
2691 sinfo->failed = true;
2692 return false;
45d6a902
AM
2693 }
2694
45d6a902 2695 t->name = p;
45d6a902 2696 t->name_indx = (unsigned int) -1;
0a1b45a2 2697 t->used = true;
45d6a902
AM
2698
2699 version_index = 1;
2700 /* Don't count anonymous version tag. */
fd91d419
L
2701 if (sinfo->info->version_info != NULL
2702 && sinfo->info->version_info->vernum == 0)
45d6a902 2703 version_index = 0;
fd91d419
L
2704 for (pp = &sinfo->info->version_info;
2705 *pp != NULL;
2706 pp = &(*pp)->next)
45d6a902
AM
2707 ++version_index;
2708 t->vernum = version_index;
2709
2710 *pp = t;
2711
2712 h->verinfo.vertree = t;
2713 }
2714 else if (t == NULL)
2715 {
2716 /* We could not find the version for a symbol when
2717 generating a shared archive. Return an error. */
4eca0228 2718 _bfd_error_handler
695344c0 2719 /* xgettext:c-format */
871b3ab2 2720 (_("%pB: version node not found for symbol %s"),
28caa186 2721 info->output_bfd, h->root.root.string);
45d6a902 2722 bfd_set_error (bfd_error_bad_value);
0a1b45a2
AM
2723 sinfo->failed = true;
2724 return false;
45d6a902 2725 }
45d6a902
AM
2726 }
2727
2728 /* If we don't have a version for this symbol, see if we can find
2729 something. */
099bb8fb
L
2730 if (!hide
2731 && h->verinfo.vertree == NULL
2732 && sinfo->info->version_info != NULL)
45d6a902 2733 {
fd91d419
L
2734 h->verinfo.vertree
2735 = bfd_find_version_for_sym (sinfo->info->version_info,
2736 h->root.root.string, &hide);
1e8fa21e 2737 if (h->verinfo.vertree != NULL && hide)
0a1b45a2 2738 (*bed->elf_backend_hide_symbol) (info, h, true);
45d6a902
AM
2739 }
2740
0a1b45a2 2741 return true;
45d6a902
AM
2742}
2743\f
45d6a902
AM
2744/* Read and swap the relocs from the section indicated by SHDR. This
2745 may be either a REL or a RELA section. The relocations are
2746 translated into RELA relocations and stored in INTERNAL_RELOCS,
2747 which should have already been allocated to contain enough space.
c6291d74
L
2748 The *EXTERNAL_RELOCS_P are a buffer where the external form of the
2749 relocations should be stored. If *EXTERNAL_RELOCS_ADDR is NULL,
2750 *EXTERNAL_RELOCS_ADDR and *EXTERNAL_RELOCS_SIZE returns the mmap
2751 memory address and size. Otherwise, *EXTERNAL_RELOCS_ADDR is
2752 unchanged and *EXTERNAL_RELOCS_SIZE returns 0.
45d6a902
AM
2753
2754 Returns FALSE if something goes wrong. */
2755
0a1b45a2 2756static bool
268b6b39 2757elf_link_read_relocs_from_section (bfd *abfd,
33c58f48 2758 const asection *sec,
268b6b39 2759 Elf_Internal_Shdr *shdr,
c6291d74
L
2760 void **external_relocs_addr,
2761 size_t *external_relocs_size,
268b6b39 2762 Elf_Internal_Rela *internal_relocs)
45d6a902 2763{
9c5bfbb7 2764 const struct elf_backend_data *bed;
268b6b39 2765 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2766 const bfd_byte *erela;
2767 const bfd_byte *erelaend;
2768 Elf_Internal_Rela *irela;
243ef1e0
L
2769 Elf_Internal_Shdr *symtab_hdr;
2770 size_t nsyms;
c6291d74 2771 void *external_relocs = *external_relocs_addr;
45d6a902 2772
45d6a902
AM
2773 /* Position ourselves at the start of the section. */
2774 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
0a1b45a2 2775 return false;
45d6a902
AM
2776
2777 /* Read the relocations. */
c6291d74
L
2778 *external_relocs_size = shdr->sh_size;
2779 if (!_bfd_mmap_read_temporary (&external_relocs,
2780 external_relocs_size,
2781 external_relocs_addr, abfd, true))
0a1b45a2 2782 return false;
45d6a902 2783
243ef1e0 2784 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2785 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2786
45d6a902
AM
2787 bed = get_elf_backend_data (abfd);
2788
2789 /* Convert the external relocations to the internal format. */
2790 if (shdr->sh_entsize == bed->s->sizeof_rel)
2791 swap_in = bed->s->swap_reloc_in;
2792 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2793 swap_in = bed->s->swap_reloca_in;
2794 else
2795 {
2796 bfd_set_error (bfd_error_wrong_format);
0a1b45a2 2797 return false;
45d6a902
AM
2798 }
2799
a50b1753 2800 erela = (const bfd_byte *) external_relocs;
f55b1e32
AM
2801 /* Setting erelaend like this and comparing with <= handles case of
2802 a fuzzed object with sh_size not a multiple of sh_entsize. */
2803 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
45d6a902 2804 irela = internal_relocs;
f55b1e32 2805 while (erela <= erelaend)
45d6a902 2806 {
243ef1e0
L
2807 bfd_vma r_symndx;
2808
45d6a902 2809 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2810 r_symndx = ELF32_R_SYM (irela->r_info);
2811 if (bed->s->arch_size == 64)
2812 r_symndx >>= 24;
ce98a316
NC
2813 if (nsyms > 0)
2814 {
2815 if ((size_t) r_symndx >= nsyms)
2816 {
4eca0228 2817 _bfd_error_handler
695344c0 2818 /* xgettext:c-format */
2dcf00ce
AM
2819 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2820 " for offset %#" PRIx64 " in section `%pA'"),
2821 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2822 (uint64_t) irela->r_offset, sec);
ce98a316 2823 bfd_set_error (bfd_error_bad_value);
0a1b45a2 2824 return false;
ce98a316
NC
2825 }
2826 }
cf35638d 2827 else if (r_symndx != STN_UNDEF)
243ef1e0 2828 {
4eca0228 2829 _bfd_error_handler
695344c0 2830 /* xgettext:c-format */
2dcf00ce
AM
2831 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2832 " for offset %#" PRIx64 " in section `%pA'"
ce98a316 2833 " when the object file has no symbol table"),
2dcf00ce
AM
2834 abfd, (uint64_t) r_symndx,
2835 (uint64_t) irela->r_offset, sec);
243ef1e0 2836 bfd_set_error (bfd_error_bad_value);
0a1b45a2 2837 return false;
243ef1e0 2838 }
45d6a902
AM
2839 irela += bed->s->int_rels_per_ext_rel;
2840 erela += shdr->sh_entsize;
2841 }
2842
0a1b45a2 2843 return true;
45d6a902
AM
2844}
2845
2846/* Read and swap the relocs for a section O. They may have been
2847 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2848 not NULL, they are used as buffers to read into. They are known to
2849 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2850 the return value is allocated using either malloc or bfd_alloc,
2851 according to the KEEP_MEMORY argument. If O has two relocation
2852 sections (both REL and RELA relocations), then the REL_HDR
2853 relocations will appear first in INTERNAL_RELOCS, followed by the
a8dde0a2
L
2854 RELA_HDR relocations. If INFO isn't NULL and KEEP_MEMORY is true,
2855 update cache_size. */
45d6a902
AM
2856
2857Elf_Internal_Rela *
a8dde0a2
L
2858_bfd_elf_link_info_read_relocs (bfd *abfd,
2859 struct bfd_link_info *info,
33c58f48 2860 const asection *o,
a8dde0a2
L
2861 void *external_relocs,
2862 Elf_Internal_Rela *internal_relocs,
2863 bool keep_memory)
45d6a902 2864{
268b6b39 2865 void *alloc1 = NULL;
c6291d74 2866 size_t alloc1_size;
45d6a902 2867 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2868 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2869 struct bfd_elf_section_data *esdo = elf_section_data (o);
2870 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2871
d4730f92
BS
2872 if (esdo->relocs != NULL)
2873 return esdo->relocs;
45d6a902
AM
2874
2875 if (o->reloc_count == 0)
2876 return NULL;
2877
45d6a902
AM
2878 if (internal_relocs == NULL)
2879 {
2880 bfd_size_type size;
2881
056bafd4 2882 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
3a8864b3
AM
2883 if (keep_memory && info)
2884 info->cache_size += size;
2885 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902 2886 if (internal_relocs == NULL)
9fd82d91 2887 return NULL;
45d6a902
AM
2888 }
2889
c6291d74 2890 alloc1 = external_relocs;
d4730f92
BS
2891 internal_rela_relocs = internal_relocs;
2892 if (esdo->rel.hdr)
2893 {
2894 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
c6291d74 2895 &alloc1, &alloc1_size,
d4730f92
BS
2896 internal_relocs))
2897 goto error_return;
2898 external_relocs = (((bfd_byte *) external_relocs)
2899 + esdo->rel.hdr->sh_size);
2900 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2901 * bed->s->int_rels_per_ext_rel);
2902 }
2903
2904 if (esdo->rela.hdr
2905 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
c6291d74 2906 &alloc1, &alloc1_size,
d4730f92 2907 internal_rela_relocs)))
45d6a902
AM
2908 goto error_return;
2909
2910 /* Cache the results for next time, if we can. */
2911 if (keep_memory)
d4730f92 2912 esdo->relocs = internal_relocs;
45d6a902 2913
76eab8f4 2914 _bfd_munmap_temporary (alloc1, alloc1_size);
45d6a902
AM
2915
2916 /* Don't free alloc2, since if it was allocated we are passing it
2917 back (under the name of internal_relocs). */
2918
2919 return internal_relocs;
2920
2921 error_return:
76eab8f4 2922 _bfd_munmap_temporary (alloc1, alloc1_size);
3a8864b3 2923 free (alloc2);
45d6a902
AM
2924 return NULL;
2925}
2926
a8dde0a2
L
2927/* This is similar to _bfd_elf_link_info_read_relocs, except for that
2928 NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
2929 struct bfd_link_info. */
2930
2931Elf_Internal_Rela *
2932_bfd_elf_link_read_relocs (bfd *abfd,
33c58f48 2933 const asection *o,
a8dde0a2
L
2934 void *external_relocs,
2935 Elf_Internal_Rela *internal_relocs,
2936 bool keep_memory)
2937{
2938 return _bfd_elf_link_info_read_relocs (abfd, NULL, o, external_relocs,
2939 internal_relocs, keep_memory);
2940
2941}
2942
45d6a902
AM
2943/* Compute the size of, and allocate space for, REL_HDR which is the
2944 section header for a section containing relocations for O. */
2945
0a1b45a2 2946static bool
9eaff861
AO
2947_bfd_elf_link_size_reloc_section (bfd *abfd,
2948 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2949{
9eaff861 2950 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2951
2952 /* That allows us to calculate the size of the section. */
9eaff861 2953 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2954
2955 /* The contents field must last into write_object_contents, so we
2956 allocate it with bfd_alloc rather than malloc. Also since we
2957 cannot be sure that the contents will actually be filled in,
2958 we zero the allocated space. */
a50b1753 2959 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902 2960 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
0a1b45a2 2961 return false;
45d6a902 2962
d4730f92 2963 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2964 {
2965 struct elf_link_hash_entry **p;
2966
ca4be51c
AM
2967 p = ((struct elf_link_hash_entry **)
2968 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902 2969 if (p == NULL)
0a1b45a2 2970 return false;
45d6a902 2971
d4730f92 2972 reldata->hashes = p;
45d6a902
AM
2973 }
2974
0a1b45a2 2975 return true;
45d6a902
AM
2976}
2977
2978/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2979 originated from the section given by INPUT_REL_HDR) to the
2980 OUTPUT_BFD. */
2981
0a1b45a2 2982bool
268b6b39
AM
2983_bfd_elf_link_output_relocs (bfd *output_bfd,
2984 asection *input_section,
2985 Elf_Internal_Shdr *input_rel_hdr,
eac338cf 2986 Elf_Internal_Rela *internal_relocs,
eebad48e 2987 struct elf_link_hash_entry **rel_hash)
45d6a902
AM
2988{
2989 Elf_Internal_Rela *irela;
2990 Elf_Internal_Rela *irelaend;
2991 bfd_byte *erel;
d4730f92 2992 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2993 asection *output_section;
9c5bfbb7 2994 const struct elf_backend_data *bed;
268b6b39 2995 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2996 struct bfd_elf_section_data *esdo;
45d6a902
AM
2997
2998 output_section = input_section->output_section;
45d6a902 2999
d4730f92
BS
3000 bed = get_elf_backend_data (output_bfd);
3001 esdo = elf_section_data (output_section);
3002 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 3003 {
d4730f92
BS
3004 output_reldata = &esdo->rel;
3005 swap_out = bed->s->swap_reloc_out;
45d6a902 3006 }
d4730f92
BS
3007 else if (esdo->rela.hdr
3008 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 3009 {
d4730f92
BS
3010 output_reldata = &esdo->rela;
3011 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
3012 }
3013 else
3014 {
4eca0228 3015 _bfd_error_handler
695344c0 3016 /* xgettext:c-format */
871b3ab2 3017 (_("%pB: relocation size mismatch in %pB section %pA"),
d003868e 3018 output_bfd, input_section->owner, input_section);
297d8443 3019 bfd_set_error (bfd_error_wrong_format);
0a1b45a2 3020 return false;
45d6a902
AM
3021 }
3022
d4730f92
BS
3023 erel = output_reldata->hdr->contents;
3024 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
3025 irela = internal_relocs;
3026 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
3027 * bed->s->int_rels_per_ext_rel);
3028 while (irela < irelaend)
3029 {
9cf3c87e 3030 if (rel_hash && *rel_hash)
eebad48e 3031 (*rel_hash)->has_reloc = 1;
45d6a902
AM
3032 (*swap_out) (output_bfd, irela, erel);
3033 irela += bed->s->int_rels_per_ext_rel;
3034 erel += input_rel_hdr->sh_entsize;
9cf3c87e
AM
3035 if (rel_hash)
3036 rel_hash++;
45d6a902
AM
3037 }
3038
3039 /* Bump the counter, so that we know where to add the next set of
3040 relocations. */
d4730f92 3041 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902 3042
0a1b45a2 3043 return true;
45d6a902
AM
3044}
3045\f
508c3946
L
3046/* Make weak undefined symbols in PIE dynamic. */
3047
0a1b45a2 3048bool
508c3946
L
3049_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
3050 struct elf_link_hash_entry *h)
3051{
0e1862bb 3052 if (bfd_link_pie (info)
508c3946
L
3053 && h->dynindx == -1
3054 && h->root.type == bfd_link_hash_undefweak)
3055 return bfd_elf_link_record_dynamic_symbol (info, h);
3056
0a1b45a2 3057 return true;
508c3946
L
3058}
3059
45d6a902
AM
3060/* Fix up the flags for a symbol. This handles various cases which
3061 can only be fixed after all the input files are seen. This is
3062 currently called by both adjust_dynamic_symbol and
3063 assign_sym_version, which is unnecessary but perhaps more robust in
3064 the face of future changes. */
3065
0a1b45a2 3066static bool
268b6b39
AM
3067_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
3068 struct elf_info_failed *eif)
45d6a902 3069{
33774f08 3070 const struct elf_backend_data *bed;
508c3946 3071
45d6a902
AM
3072 /* If this symbol was mentioned in a non-ELF file, try to set
3073 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
3074 permit a non-ELF file to correctly refer to a symbol defined in
3075 an ELF dynamic object. */
f5385ebf 3076 if (h->non_elf)
45d6a902
AM
3077 {
3078 while (h->root.type == bfd_link_hash_indirect)
3079 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3080
3081 if (h->root.type != bfd_link_hash_defined
3082 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
3083 {
3084 h->ref_regular = 1;
3085 h->ref_regular_nonweak = 1;
3086 }
45d6a902
AM
3087 else
3088 {
3089 if (h->root.u.def.section->owner != NULL
3090 && (bfd_get_flavour (h->root.u.def.section->owner)
3091 == bfd_target_elf_flavour))
f5385ebf
AM
3092 {
3093 h->ref_regular = 1;
3094 h->ref_regular_nonweak = 1;
3095 }
45d6a902 3096 else
f5385ebf 3097 h->def_regular = 1;
45d6a902
AM
3098 }
3099
3100 if (h->dynindx == -1
f5385ebf
AM
3101 && (h->def_dynamic
3102 || h->ref_dynamic))
45d6a902 3103 {
c152c796 3104 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 3105 {
0a1b45a2
AM
3106 eif->failed = true;
3107 return false;
45d6a902
AM
3108 }
3109 }
3110 }
3111 else
3112 {
f5385ebf 3113 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
3114 was first seen in a non-ELF file. Fortunately, if the symbol
3115 was first seen in an ELF file, we're probably OK unless the
3116 symbol was defined in a non-ELF file. Catch that case here.
3117 FIXME: We're still in trouble if the symbol was first seen in
3118 a dynamic object, and then later in a non-ELF regular object. */
3119 if ((h->root.type == bfd_link_hash_defined
3120 || h->root.type == bfd_link_hash_defweak)
f5385ebf 3121 && !h->def_regular
45d6a902
AM
3122 && (h->root.u.def.section->owner != NULL
3123 ? (bfd_get_flavour (h->root.u.def.section->owner)
3124 != bfd_target_elf_flavour)
3125 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
3126 && !h->def_dynamic)))
3127 h->def_regular = 1;
45d6a902
AM
3128 }
3129
508c3946 3130 /* Backend specific symbol fixup. */
33774f08
AM
3131 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
3132 if (bed->elf_backend_fixup_symbol
3133 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
0a1b45a2 3134 return false;
508c3946 3135
45d6a902
AM
3136 /* If this is a final link, and the symbol was defined as a common
3137 symbol in a regular object file, and there was no definition in
3138 any dynamic object, then the linker will have allocated space for
f5385ebf 3139 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
3140 flag will not have been set. */
3141 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
3142 && !h->def_regular
3143 && h->ref_regular
3144 && !h->def_dynamic
96f29d96 3145 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 3146 h->def_regular = 1;
45d6a902 3147
af0bfb9c
AM
3148 /* Symbols defined in discarded sections shouldn't be dynamic. */
3149 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
0a1b45a2 3150 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
af0bfb9c 3151
4deb8f71
L
3152 /* If a weak undefined symbol has non-default visibility, we also
3153 hide it from the dynamic linker. */
af0bfb9c
AM
3154 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3155 && h->root.type == bfd_link_hash_undefweak)
0a1b45a2 3156 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
4deb8f71
L
3157
3158 /* A hidden versioned symbol in executable should be forced local if
3159 it is is locally defined, not referenced by shared library and not
3160 exported. */
3161 else if (bfd_link_executable (eif->info)
3162 && h->versioned == versioned_hidden
3163 && !eif->info->export_dynamic
3164 && !h->dynamic
3165 && !h->ref_dynamic
3166 && h->def_regular)
0a1b45a2 3167 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
4deb8f71 3168
45d6a902
AM
3169 /* If -Bsymbolic was used (which means to bind references to global
3170 symbols to the definition within the shared object), and this
3171 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
3172 need a PLT entry. Likewise, if the symbol has non-default
3173 visibility. If the symbol has hidden or internal visibility, we
c1be741f 3174 will force it local. */
4deb8f71
L
3175 else if (h->needs_plt
3176 && bfd_link_pic (eif->info)
3177 && is_elf_hash_table (eif->info->hash)
3178 && (SYMBOLIC_BIND (eif->info, h)
3179 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3180 && h->def_regular)
45d6a902 3181 {
0a1b45a2 3182 bool force_local;
45d6a902 3183
45d6a902
AM
3184 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
3185 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
3186 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
3187 }
3188
45d6a902
AM
3189 /* If this is a weak defined symbol in a dynamic object, and we know
3190 the real definition in the dynamic object, copy interesting flags
3191 over to the real definition. */
60d67dc8 3192 if (h->is_weakalias)
45d6a902 3193 {
60d67dc8
AM
3194 struct elf_link_hash_entry *def = weakdef (h);
3195
45d6a902
AM
3196 /* If the real definition is defined by a regular object file,
3197 don't do anything special. See the longer description in
5b9d7a9a
AM
3198 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
3199 bfd_link_hash_defined as it was when put on the alias list
3200 then it must have originally been a versioned symbol (for
3201 which a non-versioned indirect symbol is created) and later
3202 a definition for the non-versioned symbol is found. In that
3203 case the indirection is flipped with the versioned symbol
3204 becoming an indirect pointing at the non-versioned symbol.
3205 Thus, not an alias any more. */
3206 if (def->def_regular
3207 || def->root.type != bfd_link_hash_defined)
60d67dc8
AM
3208 {
3209 h = def;
3210 while ((h = h->u.alias) != def)
3211 h->is_weakalias = 0;
3212 }
45d6a902 3213 else
a26587ba 3214 {
4e6b54a6
AM
3215 while (h->root.type == bfd_link_hash_indirect)
3216 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4e6b54a6
AM
3217 BFD_ASSERT (h->root.type == bfd_link_hash_defined
3218 || h->root.type == bfd_link_hash_defweak);
60d67dc8 3219 BFD_ASSERT (def->def_dynamic);
60d67dc8 3220 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
a26587ba 3221 }
45d6a902
AM
3222 }
3223
0a1b45a2 3224 return true;
45d6a902
AM
3225}
3226
3227/* Make the backend pick a good value for a dynamic symbol. This is
3228 called via elf_link_hash_traverse, and also calls itself
3229 recursively. */
3230
0a1b45a2 3231static bool
268b6b39 3232_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 3233{
a50b1753 3234 struct elf_info_failed *eif = (struct elf_info_failed *) data;
559192d8 3235 struct elf_link_hash_table *htab;
9c5bfbb7 3236 const struct elf_backend_data *bed;
45d6a902 3237
0eddce27 3238 if (! is_elf_hash_table (eif->info->hash))
0a1b45a2 3239 return false;
45d6a902 3240
45d6a902
AM
3241 /* Ignore indirect symbols. These are added by the versioning code. */
3242 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 3243 return true;
45d6a902
AM
3244
3245 /* Fix the symbol flags. */
3246 if (! _bfd_elf_fix_symbol_flags (h, eif))
0a1b45a2 3247 return false;
45d6a902 3248
559192d8
AM
3249 htab = elf_hash_table (eif->info);
3250 bed = get_elf_backend_data (htab->dynobj);
3251
954b63d4
AM
3252 if (h->root.type == bfd_link_hash_undefweak)
3253 {
3254 if (eif->info->dynamic_undefined_weak == 0)
0a1b45a2 3255 (*bed->elf_backend_hide_symbol) (eif->info, h, true);
954b63d4
AM
3256 else if (eif->info->dynamic_undefined_weak > 0
3257 && h->ref_regular
3258 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3259 && !bfd_hide_sym_by_version (eif->info->version_info,
3260 h->root.root.string))
3261 {
3262 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
3263 {
0a1b45a2
AM
3264 eif->failed = true;
3265 return false;
954b63d4
AM
3266 }
3267 }
3268 }
3269
45d6a902
AM
3270 /* If this symbol does not require a PLT entry, and it is not
3271 defined by a dynamic object, or is not referenced by a regular
3272 object, ignore it. We do have to handle a weak defined symbol,
3273 even if no regular object refers to it, if we decided to add it
3274 to the dynamic symbol table. FIXME: Do we normally need to worry
3275 about symbols which are defined by one dynamic object and
3276 referenced by another one? */
f5385ebf 3277 if (!h->needs_plt
91e21fb7 3278 && h->type != STT_GNU_IFUNC
f5385ebf
AM
3279 && (h->def_regular
3280 || !h->def_dynamic
3281 || (!h->ref_regular
60d67dc8 3282 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
45d6a902 3283 {
a6aa5195 3284 h->plt = elf_hash_table (eif->info)->init_plt_offset;
0a1b45a2 3285 return true;
45d6a902
AM
3286 }
3287
3288 /* If we've already adjusted this symbol, don't do it again. This
3289 can happen via a recursive call. */
f5385ebf 3290 if (h->dynamic_adjusted)
0a1b45a2 3291 return true;
45d6a902
AM
3292
3293 /* Don't look at this symbol again. Note that we must set this
3294 after checking the above conditions, because we may look at a
3295 symbol once, decide not to do anything, and then get called
3296 recursively later after REF_REGULAR is set below. */
f5385ebf 3297 h->dynamic_adjusted = 1;
45d6a902
AM
3298
3299 /* If this is a weak definition, and we know a real definition, and
3300 the real symbol is not itself defined by a regular object file,
3301 then get a good value for the real definition. We handle the
3302 real symbol first, for the convenience of the backend routine.
3303
3304 Note that there is a confusing case here. If the real definition
3305 is defined by a regular object file, we don't get the real symbol
3306 from the dynamic object, but we do get the weak symbol. If the
3307 processor backend uses a COPY reloc, then if some routine in the
3308 dynamic object changes the real symbol, we will not see that
3309 change in the corresponding weak symbol. This is the way other
3310 ELF linkers work as well, and seems to be a result of the shared
3311 library model.
3312
3313 I will clarify this issue. Most SVR4 shared libraries define the
3314 variable _timezone and define timezone as a weak synonym. The
3315 tzset call changes _timezone. If you write
3316 extern int timezone;
3317 int _timezone = 5;
3318 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3319 you might expect that, since timezone is a synonym for _timezone,
3320 the same number will print both times. However, if the processor
3321 backend uses a COPY reloc, then actually timezone will be copied
3322 into your process image, and, since you define _timezone
3323 yourself, _timezone will not. Thus timezone and _timezone will
3324 wind up at different memory locations. The tzset call will set
3325 _timezone, leaving timezone unchanged. */
3326
60d67dc8 3327 if (h->is_weakalias)
45d6a902 3328 {
60d67dc8
AM
3329 struct elf_link_hash_entry *def = weakdef (h);
3330
ec24dc88 3331 /* If we get to this point, there is an implicit reference to
60d67dc8
AM
3332 the alias by a regular object file via the weak symbol H. */
3333 def->ref_regular = 1;
45d6a902 3334
ec24dc88 3335 /* Ensure that the backend adjust_dynamic_symbol function sees
60d67dc8
AM
3336 the strong alias before H by recursively calling ourselves. */
3337 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
0a1b45a2 3338 return false;
45d6a902
AM
3339 }
3340
3341 /* If a symbol has no type and no size and does not require a PLT
3342 entry, then we are probably about to do the wrong thing here: we
3343 are probably going to create a COPY reloc for an empty object.
3344 This case can arise when a shared object is built with assembly
3345 code, and the assembly code fails to set the symbol type. */
3346 if (h->size == 0
3347 && h->type == STT_NOTYPE
f5385ebf 3348 && !h->needs_plt)
4eca0228 3349 _bfd_error_handler
45d6a902
AM
3350 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3351 h->root.root.string);
3352
45d6a902
AM
3353 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3354 {
0a1b45a2
AM
3355 eif->failed = true;
3356 return false;
45d6a902
AM
3357 }
3358
0a1b45a2 3359 return true;
45d6a902
AM
3360}
3361
027297b7
L
3362/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3363 DYNBSS. */
3364
0a1b45a2 3365bool
6cabe1ea
AM
3366_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3367 struct elf_link_hash_entry *h,
027297b7
L
3368 asection *dynbss)
3369{
91ac5911 3370 unsigned int power_of_two;
027297b7
L
3371 bfd_vma mask;
3372 asection *sec = h->root.u.def.section;
3373
de194d85 3374 /* The section alignment of the definition is the maximum alignment
91ac5911
L
3375 requirement of symbols defined in the section. Since we don't
3376 know the symbol alignment requirement, we start with the
3377 maximum alignment and check low bits of the symbol address
3378 for the minimum alignment. */
fd361982 3379 power_of_two = bfd_section_alignment (sec);
91ac5911
L
3380 mask = ((bfd_vma) 1 << power_of_two) - 1;
3381 while ((h->root.u.def.value & mask) != 0)
3382 {
3383 mask >>= 1;
3384 --power_of_two;
3385 }
027297b7 3386
230a788e
AM
3387 /* Adjust the section alignment if needed. */
3388 if (!bfd_link_align_section (dynbss, power_of_two))
3389 return false;
027297b7 3390
91ac5911 3391 /* We make sure that the symbol will be aligned properly. */
027297b7
L
3392 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3393
3394 /* Define the symbol as being at this point in DYNBSS. */
3395 h->root.u.def.section = dynbss;
3396 h->root.u.def.value = dynbss->size;
3397
3398 /* Increment the size of DYNBSS to make room for the symbol. */
3399 dynbss->size += h->size;
3400
f7483970
L
3401 /* No error if extern_protected_data is true. */
3402 if (h->protected_def
889c2a67
L
3403 && (!info->extern_protected_data
3404 || (info->extern_protected_data < 0
3405 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05 3406 info->callbacks->einfo
c1c8c1ef 3407 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
d07a1b05 3408 h->root.root.string);
6cabe1ea 3409
0a1b45a2 3410 return true;
027297b7
L
3411}
3412
45d6a902
AM
3413/* Adjust all external symbols pointing into SEC_MERGE sections
3414 to reflect the object merging within the sections. */
3415
0a1b45a2 3416static bool
268b6b39 3417_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
3418{
3419 asection *sec;
3420
45d6a902
AM
3421 if ((h->root.type == bfd_link_hash_defined
3422 || h->root.type == bfd_link_hash_defweak)
3423 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 3424 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 3425 {
a50b1753 3426 bfd *output_bfd = (bfd *) data;
45d6a902
AM
3427
3428 h->root.u.def.value =
3429 _bfd_merged_section_offset (output_bfd,
3430 &h->root.u.def.section,
3431 elf_section_data (sec)->sec_info,
753731ee 3432 h->root.u.def.value);
45d6a902
AM
3433 }
3434
0a1b45a2 3435 return true;
45d6a902 3436}
986a241f
RH
3437
3438/* Returns false if the symbol referred to by H should be considered
3439 to resolve local to the current module, and true if it should be
3440 considered to bind dynamically. */
3441
0a1b45a2 3442bool
268b6b39
AM
3443_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3444 struct bfd_link_info *info,
0a1b45a2 3445 bool not_local_protected)
986a241f 3446{
0a1b45a2 3447 bool binding_stays_local_p;
fcb93ecf
PB
3448 const struct elf_backend_data *bed;
3449 struct elf_link_hash_table *hash_table;
986a241f
RH
3450
3451 if (h == NULL)
0a1b45a2 3452 return false;
986a241f
RH
3453
3454 while (h->root.type == bfd_link_hash_indirect
3455 || h->root.type == bfd_link_hash_warning)
3456 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3457
3458 /* If it was forced local, then clearly it's not dynamic. */
3459 if (h->dynindx == -1)
0a1b45a2 3460 return false;
f5385ebf 3461 if (h->forced_local)
0a1b45a2 3462 return false;
986a241f
RH
3463
3464 /* Identify the cases where name binding rules say that a
3465 visible symbol resolves locally. */
0e1862bb
L
3466 binding_stays_local_p = (bfd_link_executable (info)
3467 || SYMBOLIC_BIND (info, h));
986a241f
RH
3468
3469 switch (ELF_ST_VISIBILITY (h->other))
3470 {
3471 case STV_INTERNAL:
3472 case STV_HIDDEN:
0a1b45a2 3473 return false;
986a241f
RH
3474
3475 case STV_PROTECTED:
fcb93ecf 3476 hash_table = elf_hash_table (info);
2cc15b10 3477 if (!is_elf_hash_table (&hash_table->root))
0a1b45a2 3478 return false;
fcb93ecf
PB
3479
3480 bed = get_elf_backend_data (hash_table->dynobj);
3481
986a241f
RH
3482 /* Proper resolution for function pointer equality may require
3483 that these symbols perhaps be resolved dynamically, even though
3484 we should be resolving them to the current module. */
89a2ee5a 3485 if (!not_local_protected || !bed->is_function_type (h->type))
0a1b45a2 3486 binding_stays_local_p = true;
986a241f
RH
3487 break;
3488
3489 default:
986a241f
RH
3490 break;
3491 }
3492
aa37626c 3493 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 3494 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
0a1b45a2 3495 return true;
aa37626c 3496
986a241f
RH
3497 /* Otherwise, the symbol is dynamic if binding rules don't tell
3498 us that it remains local. */
3499 return !binding_stays_local_p;
3500}
f6c52c13
AM
3501
3502/* Return true if the symbol referred to by H should be considered
3503 to resolve local to the current module, and false otherwise. Differs
3504 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 3505 undefined symbols. The two functions are virtually identical except
0fad2956
MR
3506 for the place where dynindx == -1 is tested. If that test is true,
3507 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3508 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3509 defined symbols.
89a2ee5a
AM
3510 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3511 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3512 treatment of undefined weak symbols. For those that do not make
3513 undefined weak symbols dynamic, both functions may return false. */
f6c52c13 3514
0a1b45a2 3515bool
268b6b39
AM
3516_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3517 struct bfd_link_info *info,
0a1b45a2 3518 bool local_protected)
f6c52c13 3519{
fcb93ecf
PB
3520 const struct elf_backend_data *bed;
3521 struct elf_link_hash_table *hash_table;
3522
f6c52c13
AM
3523 /* If it's a local sym, of course we resolve locally. */
3524 if (h == NULL)
0a1b45a2 3525 return true;
f6c52c13 3526
d95edcac
L
3527 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3528 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3529 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
0a1b45a2 3530 return true;
d95edcac 3531
0fad2956
MR
3532 /* Forced local symbols resolve locally. */
3533 if (h->forced_local)
0a1b45a2 3534 return true;
0fad2956 3535
7e2294f9
AO
3536 /* Common symbols that become definitions don't get the DEF_REGULAR
3537 flag set, so test it first, and don't bail out. */
3538 if (ELF_COMMON_DEF_P (h))
3539 /* Do nothing. */;
f6c52c13 3540 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
3541 resolve locally. The sym is either undefined or dynamic. */
3542 else if (!h->def_regular)
0a1b45a2 3543 return false;
f6c52c13 3544
0fad2956 3545 /* Non-dynamic symbols resolve locally. */
f6c52c13 3546 if (h->dynindx == -1)
0a1b45a2 3547 return true;
f6c52c13
AM
3548
3549 /* At this point, we know the symbol is defined and dynamic. In an
3550 executable it must resolve locally, likewise when building symbolic
3551 shared libraries. */
0e1862bb 3552 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
0a1b45a2 3553 return true;
f6c52c13
AM
3554
3555 /* Now deal with defined dynamic symbols in shared libraries. Ones
3556 with default visibility might not resolve locally. */
3557 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
0a1b45a2 3558 return false;
f6c52c13 3559
fcb93ecf 3560 hash_table = elf_hash_table (info);
2cc15b10 3561 if (!is_elf_hash_table (&hash_table->root))
0a1b45a2 3562 return true;
fcb93ecf 3563
6f365fda
L
3564 /* STV_PROTECTED symbols with indirect external access are local. */
3565 if (info->indirect_extern_access > 0)
3566 return true;
3567
fcb93ecf
PB
3568 bed = get_elf_backend_data (hash_table->dynobj);
3569
f7483970
L
3570 /* If extern_protected_data is false, STV_PROTECTED non-function
3571 symbols are local. */
889c2a67
L
3572 if ((!info->extern_protected_data
3573 || (info->extern_protected_data < 0
3574 && !bed->extern_protected_data))
3575 && !bed->is_function_type (h->type))
0a1b45a2 3576 return true;
1c16dfa5 3577
f6c52c13 3578 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
3579 symbols be treated as dynamic symbols. If the address of a
3580 function not defined in an executable is set to that function's
3581 plt entry in the executable, then the address of the function in
3582 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
3583 return local_protected;
3584}
e1918d23
AM
3585
3586/* Caches some TLS segment info, and ensures that the TLS segment vma is
3587 aligned. Returns the first TLS output section. */
3588
3589struct bfd_section *
3590_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3591{
3592 struct bfd_section *sec, *tls;
3593 unsigned int align = 0;
3594
3595 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3596 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3597 break;
3598 tls = sec;
3599
3600 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3601 if (sec->alignment_power > align)
3602 align = sec->alignment_power;
3603
3604 elf_hash_table (info)->tls_sec = tls;
3605
fdde2fb6
SH
3606 /* Ensure the alignment of the first section (usually .tdata) is the largest
3607 alignment, so that the tls segment starts aligned. */
e1918d23 3608 if (tls != NULL)
10444351 3609 (void) bfd_link_align_section (tls, align);
e1918d23
AM
3610
3611 return tls;
3612}
0ad989f9
L
3613
3614/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
0a1b45a2 3615static bool
0ad989f9
L
3616is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3617 Elf_Internal_Sym *sym)
3618{
a4d8e49b
L
3619 const struct elf_backend_data *bed;
3620
0ad989f9
L
3621 /* Local symbols do not count, but target specific ones might. */
3622 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3623 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
0a1b45a2 3624 return false;
0ad989f9 3625
fcb93ecf 3626 bed = get_elf_backend_data (abfd);
0ad989f9 3627 /* Function symbols do not count. */
fcb93ecf 3628 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0a1b45a2 3629 return false;
0ad989f9
L
3630
3631 /* If the section is undefined, then so is the symbol. */
3632 if (sym->st_shndx == SHN_UNDEF)
0a1b45a2 3633 return false;
0ad989f9
L
3634
3635 /* If the symbol is defined in the common section, then
3636 it is a common definition and so does not count. */
a4d8e49b 3637 if (bed->common_definition (sym))
0a1b45a2 3638 return false;
0ad989f9
L
3639
3640 /* If the symbol is in a target specific section then we
3641 must rely upon the backend to tell us what it is. */
3642 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3643 /* FIXME - this function is not coded yet:
3644
3645 return _bfd_is_global_symbol_definition (abfd, sym);
3646
3647 Instead for now assume that the definition is not global,
3648 Even if this is wrong, at least the linker will behave
3649 in the same way that it used to do. */
0a1b45a2 3650 return false;
0ad989f9 3651
0a1b45a2 3652 return true;
0ad989f9
L
3653}
3654
3655/* Search the symbol table of the archive element of the archive ABFD
3656 whose archive map contains a mention of SYMDEF, and determine if
3657 the symbol is defined in this element. */
0a1b45a2 3658static bool
0ad989f9
L
3659elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3660{
3661 Elf_Internal_Shdr * hdr;
ef53be89
AM
3662 size_t symcount;
3663 size_t extsymcount;
3664 size_t extsymoff;
0ad989f9
L
3665 Elf_Internal_Sym *isymbuf;
3666 Elf_Internal_Sym *isym;
3667 Elf_Internal_Sym *isymend;
0a1b45a2 3668 bool result;
0ad989f9 3669
6395a102 3670 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset, NULL);
0ad989f9 3671 if (abfd == NULL)
0a1b45a2 3672 return false;
0ad989f9
L
3673
3674 if (! bfd_check_format (abfd, bfd_object))
0a1b45a2 3675 return false;
0ad989f9 3676
7dc3990e
L
3677 /* Select the appropriate symbol table. If we don't know if the
3678 object file is an IR object, give linker LTO plugin a chance to
3679 get the correct symbol table. */
3680 if (abfd->plugin_format == bfd_plugin_yes
a6f8fe0a 3681 || abfd->plugin_format == bfd_plugin_yes_unused
08ce1d72 3682#if BFD_SUPPORTS_PLUGINS
7dc3990e
L
3683 || (abfd->plugin_format == bfd_plugin_unknown
3684 && bfd_link_plugin_object_p (abfd))
3685#endif
3686 )
3687 {
3688 /* Use the IR symbol table if the object has been claimed by
3689 plugin. */
3690 abfd = abfd->plugin_dummy_bfd;
3691 hdr = &elf_tdata (abfd)->symtab_hdr;
3692 }
0ad989f9 3693 else
bac5753c
L
3694 {
3695 if (elf_use_dt_symtab_p (abfd))
3696 {
3697 bfd_set_error (bfd_error_wrong_format);
3698 return false;
3699 }
3700
3701 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3702 hdr = &elf_tdata (abfd)->symtab_hdr;
3703 else
3704 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3705 }
0ad989f9
L
3706
3707 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3708
3709 /* The sh_info field of the symtab header tells us where the
3710 external symbols start. We don't care about the local symbols. */
3711 if (elf_bad_symtab (abfd))
3712 {
3713 extsymcount = symcount;
3714 extsymoff = 0;
3715 }
3716 else
3717 {
3718 extsymcount = symcount - hdr->sh_info;
3719 extsymoff = hdr->sh_info;
3720 }
3721
3722 if (extsymcount == 0)
0a1b45a2 3723 return false;
0ad989f9
L
3724
3725 /* Read in the symbol table. */
3726 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3727 NULL, NULL, NULL);
3728 if (isymbuf == NULL)
0a1b45a2 3729 return false;
0ad989f9
L
3730
3731 /* Scan the symbol table looking for SYMDEF. */
0a1b45a2 3732 result = false;
0ad989f9
L
3733 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3734 {
3735 const char *name;
3736
3737 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3738 isym->st_name);
3739 if (name == NULL)
3740 break;
3741
3742 if (strcmp (name, symdef->name) == 0)
3743 {
3744 result = is_global_data_symbol_definition (abfd, isym);
3745 break;
3746 }
3747 }
3748
3749 free (isymbuf);
3750
3751 return result;
3752}
3753\f
5a580b3a
AM
3754/* Add an entry to the .dynamic table. */
3755
0a1b45a2 3756bool
5a580b3a
AM
3757_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3758 bfd_vma tag,
3759 bfd_vma val)
3760{
3761 struct elf_link_hash_table *hash_table;
3762 const struct elf_backend_data *bed;
3763 asection *s;
3764 bfd_size_type newsize;
3765 bfd_byte *newcontents;
3766 Elf_Internal_Dyn dyn;
3767
3768 hash_table = elf_hash_table (info);
2cc15b10 3769 if (! is_elf_hash_table (&hash_table->root))
0a1b45a2 3770 return false;
5a580b3a 3771
7f923b7f 3772 if (tag == DT_RELA || tag == DT_REL)
0a1b45a2 3773 hash_table->dynamic_relocs = true;
7f923b7f 3774
5a580b3a 3775 bed = get_elf_backend_data (hash_table->dynobj);
c3460201 3776 s = hash_table->dynamic;
5a580b3a
AM
3777 BFD_ASSERT (s != NULL);
3778
eea6121a 3779 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3780 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a 3781 if (newcontents == NULL)
0a1b45a2 3782 return false;
5a580b3a
AM
3783
3784 dyn.d_tag = tag;
3785 dyn.d_un.d_val = val;
eea6121a 3786 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3787
eea6121a 3788 s->size = newsize;
5a580b3a
AM
3789 s->contents = newcontents;
3790
0a1b45a2 3791 return true;
5a580b3a
AM
3792}
3793
6f6fd151
L
3794/* Strip zero-sized dynamic sections. */
3795
0a1b45a2 3796bool
6f6fd151
L
3797_bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3798{
3799 struct elf_link_hash_table *hash_table;
3800 const struct elf_backend_data *bed;
3801 asection *s, *sdynamic, **pp;
3802 asection *rela_dyn, *rel_dyn;
3803 Elf_Internal_Dyn dyn;
3804 bfd_byte *extdyn, *next;
3805 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
0a1b45a2
AM
3806 bool strip_zero_sized;
3807 bool strip_zero_sized_plt;
6f6fd151
L
3808
3809 if (bfd_link_relocatable (info))
0a1b45a2 3810 return true;
6f6fd151
L
3811
3812 hash_table = elf_hash_table (info);
2cc15b10 3813 if (!is_elf_hash_table (&hash_table->root))
0a1b45a2 3814 return false;
6f6fd151
L
3815
3816 if (!hash_table->dynobj)
0a1b45a2 3817 return true;
6f6fd151 3818
c3460201 3819 sdynamic= hash_table->dynamic;
6f6fd151 3820 if (!sdynamic)
0a1b45a2 3821 return true;
6f6fd151
L
3822
3823 bed = get_elf_backend_data (hash_table->dynobj);
3824 swap_dyn_in = bed->s->swap_dyn_in;
3825
0a1b45a2
AM
3826 strip_zero_sized = false;
3827 strip_zero_sized_plt = false;
6f6fd151
L
3828
3829 /* Strip zero-sized dynamic sections. */
3830 rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3831 rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3832 for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3833 if (s->size == 0
3834 && (s == rela_dyn
3835 || s == rel_dyn
3836 || s == hash_table->srelplt->output_section
3837 || s == hash_table->splt->output_section))
3838 {
3839 *pp = s->next;
3840 info->output_bfd->section_count--;
0a1b45a2 3841 strip_zero_sized = true;
6f6fd151
L
3842 if (s == rela_dyn)
3843 s = rela_dyn;
3844 if (s == rel_dyn)
3845 s = rel_dyn;
3846 else if (s == hash_table->splt->output_section)
3847 {
3848 s = hash_table->splt;
0a1b45a2 3849 strip_zero_sized_plt = true;
6f6fd151
L
3850 }
3851 else
3852 s = hash_table->srelplt;
3853 s->flags |= SEC_EXCLUDE;
3854 s->output_section = bfd_abs_section_ptr;
3855 }
3856 else
3857 pp = &s->next;
3858
36f61bf2 3859 if (strip_zero_sized_plt && sdynamic->size != 0)
6f6fd151
L
3860 for (extdyn = sdynamic->contents;
3861 extdyn < sdynamic->contents + sdynamic->size;
3862 extdyn = next)
3863 {
3864 next = extdyn + bed->s->sizeof_dyn;
3865 swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3866 switch (dyn.d_tag)
3867 {
3868 default:
3869 break;
3870 case DT_JMPREL:
3871 case DT_PLTRELSZ:
3872 case DT_PLTREL:
3873 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3874 the procedure linkage table (the .plt section) has been
3875 removed. */
3876 memmove (extdyn, next,
3877 sdynamic->size - (next - sdynamic->contents));
3878 next = extdyn;
3879 }
3880 }
3881
3882 if (strip_zero_sized)
3883 {
3884 /* Regenerate program headers. */
3885 elf_seg_map (info->output_bfd) = NULL;
e2cbf4df
L
3886 return _bfd_elf_map_sections_to_segments (info->output_bfd, info,
3887 NULL);
6f6fd151
L
3888 }
3889
0a1b45a2 3890 return true;
6f6fd151
L
3891}
3892
e310298c 3893/* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
5a580b3a
AM
3894 1 if a DT_NEEDED tag already exists, and 0 on success. */
3895
e310298c
AM
3896int
3897bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
5a580b3a
AM
3898{
3899 struct elf_link_hash_table *hash_table;
ef53be89 3900 size_t strindex;
e310298c 3901 const char *soname;
5a580b3a 3902
7e9f0867
AM
3903 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3904 return -1;
3905
5a580b3a 3906 hash_table = elf_hash_table (info);
e310298c 3907 soname = elf_dt_name (abfd);
0a1b45a2 3908 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, false);
ef53be89 3909 if (strindex == (size_t) -1)
5a580b3a
AM
3910 return -1;
3911
02be4619 3912 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3913 {
3914 asection *sdyn;
3915 const struct elf_backend_data *bed;
3916 bfd_byte *extdyn;
3917
3918 bed = get_elf_backend_data (hash_table->dynobj);
c3460201 3919 sdyn = hash_table->dynamic;
36f61bf2 3920 if (sdyn != NULL && sdyn->size != 0)
7e9f0867
AM
3921 for (extdyn = sdyn->contents;
3922 extdyn < sdyn->contents + sdyn->size;
3923 extdyn += bed->s->sizeof_dyn)
3924 {
3925 Elf_Internal_Dyn dyn;
5a580b3a 3926
7e9f0867
AM
3927 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3928 if (dyn.d_tag == DT_NEEDED
3929 && dyn.d_un.d_val == strindex)
3930 {
3931 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3932 return 1;
3933 }
3934 }
5a580b3a
AM
3935 }
3936
e310298c
AM
3937 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3938 return -1;
7e9f0867 3939
e310298c
AM
3940 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3941 return -1;
5a580b3a
AM
3942
3943 return 0;
3944}
3945
7b15fa7a
AM
3946/* Return true if SONAME is on the needed list between NEEDED and STOP
3947 (or the end of list if STOP is NULL), and needed by a library that
3948 will be loaded. */
3949
0a1b45a2 3950static bool
7b15fa7a
AM
3951on_needed_list (const char *soname,
3952 struct bfd_link_needed_list *needed,
3953 struct bfd_link_needed_list *stop)
010e5ae2 3954{
7b15fa7a
AM
3955 struct bfd_link_needed_list *look;
3956 for (look = needed; look != stop; look = look->next)
3957 if (strcmp (soname, look->name) == 0
3958 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3959 /* If needed by a library that itself is not directly
3960 needed, recursively check whether that library is
3961 indirectly needed. Since we add DT_NEEDED entries to
3962 the end of the list, library dependencies appear after
3963 the library. Therefore search prior to the current
3964 LOOK, preventing possible infinite recursion. */
3965 || on_needed_list (elf_dt_name (look->by), needed, look)))
0a1b45a2 3966 return true;
010e5ae2 3967
0a1b45a2 3968 return false;
010e5ae2
AM
3969}
3970
3a3f4bf7 3971/* Sort symbol by value, section, size, and type. */
4ad4eba5
AM
3972static int
3973elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3974{
3975 const struct elf_link_hash_entry *h1;
3976 const struct elf_link_hash_entry *h2;
10b7e05b 3977 bfd_signed_vma vdiff;
3a3f4bf7
AM
3978 int sdiff;
3979 const char *n1;
3980 const char *n2;
5a580b3a
AM
3981
3982 h1 = *(const struct elf_link_hash_entry **) arg1;
3983 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3984 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3985 if (vdiff != 0)
3986 return vdiff > 0 ? 1 : -1;
3a3f4bf7
AM
3987
3988 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3989 if (sdiff != 0)
3990 return sdiff;
3991
3992 /* Sort so that sized symbols are selected over zero size symbols. */
3993 vdiff = h1->size - h2->size;
3994 if (vdiff != 0)
3995 return vdiff > 0 ? 1 : -1;
3996
3997 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3998 if (h1->type != h2->type)
3999 return h1->type - h2->type;
4000
4001 /* If symbols are properly sized and typed, and multiple strong
4002 aliases are not defined in a shared library by the user we
4003 shouldn't get here. Unfortunately linker script symbols like
4004 __bss_start sometimes match a user symbol defined at the start of
4005 .bss without proper size and type. We'd like to preference the
4006 user symbol over reserved system symbols. Sort on leading
4007 underscores. */
4008 n1 = h1->root.root.string;
4009 n2 = h2->root.root.string;
4010 while (*n1 == *n2)
10b7e05b 4011 {
3a3f4bf7
AM
4012 if (*n1 == 0)
4013 break;
4014 ++n1;
4015 ++n2;
10b7e05b 4016 }
3a3f4bf7
AM
4017 if (*n1 == '_')
4018 return -1;
4019 if (*n2 == '_')
4020 return 1;
4021
4022 /* Final sort on name selects user symbols like '_u' over reserved
4023 system symbols like '_Z' and also will avoid qsort instability. */
4024 return *n1 - *n2;
5a580b3a 4025}
4ad4eba5 4026
5a580b3a
AM
4027/* This function is used to adjust offsets into .dynstr for
4028 dynamic symbols. This is called via elf_link_hash_traverse. */
4029
0a1b45a2 4030static bool
5a580b3a
AM
4031elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
4032{
a50b1753 4033 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 4034
5a580b3a
AM
4035 if (h->dynindx != -1)
4036 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
0a1b45a2 4037 return true;
5a580b3a
AM
4038}
4039
4040/* Assign string offsets in .dynstr, update all structures referencing
4041 them. */
4042
0a1b45a2 4043static bool
4ad4eba5 4044elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
4045{
4046 struct elf_link_hash_table *hash_table = elf_hash_table (info);
4047 struct elf_link_local_dynamic_entry *entry;
4048 struct elf_strtab_hash *dynstr = hash_table->dynstr;
4049 bfd *dynobj = hash_table->dynobj;
4050 asection *sdyn;
4051 bfd_size_type size;
4052 const struct elf_backend_data *bed;
4053 bfd_byte *extdyn;
4054
4055 _bfd_elf_strtab_finalize (dynstr);
4056 size = _bfd_elf_strtab_size (dynstr);
4057
3d16b64e
NA
4058 /* Allow the linker to examine the dynsymtab now it's fully populated. */
4059
4060 if (info->callbacks->examine_strtab)
4061 info->callbacks->examine_strtab (dynstr);
4062
5a580b3a 4063 bed = get_elf_backend_data (dynobj);
c3460201 4064 sdyn = hash_table->dynamic;
5a580b3a
AM
4065 BFD_ASSERT (sdyn != NULL);
4066
4067 /* Update all .dynamic entries referencing .dynstr strings. */
4068 for (extdyn = sdyn->contents;
36f61bf2 4069 extdyn < PTR_ADD (sdyn->contents, sdyn->size);
5a580b3a
AM
4070 extdyn += bed->s->sizeof_dyn)
4071 {
4072 Elf_Internal_Dyn dyn;
4073
4074 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
4075 switch (dyn.d_tag)
4076 {
4077 case DT_STRSZ:
4078 dyn.d_un.d_val = size;
4079 break;
4080 case DT_NEEDED:
4081 case DT_SONAME:
4082 case DT_RPATH:
4083 case DT_RUNPATH:
4084 case DT_FILTER:
4085 case DT_AUXILIARY:
7ee314fa
AM
4086 case DT_AUDIT:
4087 case DT_DEPAUDIT:
5a580b3a
AM
4088 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
4089 break;
4090 default:
4091 continue;
4092 }
4093 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
4094 }
4095
4096 /* Now update local dynamic symbols. */
4097 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
4098 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
4099 entry->isym.st_name);
4100
4101 /* And the rest of dynamic symbols. */
4102 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
4103
4104 /* Adjust version definitions. */
4105 if (elf_tdata (output_bfd)->cverdefs)
4106 {
4107 asection *s;
4108 bfd_byte *p;
ef53be89 4109 size_t i;
5a580b3a
AM
4110 Elf_Internal_Verdef def;
4111 Elf_Internal_Verdaux defaux;
4112
3d4d4302 4113 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
4114 p = s->contents;
4115 do
4116 {
4117 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
4118 &def);
4119 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
4120 if (def.vd_aux != sizeof (Elf_External_Verdef))
4121 continue;
5a580b3a
AM
4122 for (i = 0; i < def.vd_cnt; ++i)
4123 {
4124 _bfd_elf_swap_verdaux_in (output_bfd,
4125 (Elf_External_Verdaux *) p, &defaux);
4126 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
4127 defaux.vda_name);
4128 _bfd_elf_swap_verdaux_out (output_bfd,
4129 &defaux, (Elf_External_Verdaux *) p);
4130 p += sizeof (Elf_External_Verdaux);
4131 }
4132 }
4133 while (def.vd_next);
4134 }
4135
4136 /* Adjust version references. */
4137 if (elf_tdata (output_bfd)->verref)
4138 {
4139 asection *s;
4140 bfd_byte *p;
ef53be89 4141 size_t i;
5a580b3a
AM
4142 Elf_Internal_Verneed need;
4143 Elf_Internal_Vernaux needaux;
4144
3d4d4302 4145 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
4146 p = s->contents;
4147 do
4148 {
4149 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
4150 &need);
4151 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
4152 _bfd_elf_swap_verneed_out (output_bfd, &need,
4153 (Elf_External_Verneed *) p);
4154 p += sizeof (Elf_External_Verneed);
4155 for (i = 0; i < need.vn_cnt; ++i)
4156 {
4157 _bfd_elf_swap_vernaux_in (output_bfd,
4158 (Elf_External_Vernaux *) p, &needaux);
4159 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
4160 needaux.vna_name);
4161 _bfd_elf_swap_vernaux_out (output_bfd,
4162 &needaux,
4163 (Elf_External_Vernaux *) p);
4164 p += sizeof (Elf_External_Vernaux);
4165 }
4166 }
4167 while (need.vn_next);
4168 }
4169
0a1b45a2 4170 return true;
5a580b3a
AM
4171}
4172\f
13285a1b
AM
4173/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4174 The default is to only match when the INPUT and OUTPUT are exactly
4175 the same target. */
4176
0a1b45a2 4177bool
13285a1b
AM
4178_bfd_elf_default_relocs_compatible (const bfd_target *input,
4179 const bfd_target *output)
4180{
4181 return input == output;
4182}
4183
4184/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
4185 This version is used when different targets for the same architecture
4186 are virtually identical. */
4187
0a1b45a2 4188bool
13285a1b
AM
4189_bfd_elf_relocs_compatible (const bfd_target *input,
4190 const bfd_target *output)
4191{
4192 const struct elf_backend_data *obed, *ibed;
4193
4194 if (input == output)
0a1b45a2 4195 return true;
13285a1b
AM
4196
4197 ibed = xvec_get_elf_backend_data (input);
4198 obed = xvec_get_elf_backend_data (output);
4199
4200 if (ibed->arch != obed->arch)
0a1b45a2 4201 return false;
13285a1b
AM
4202
4203 /* If both backends are using this function, deem them compatible. */
4204 return ibed->relocs_compatible == obed->relocs_compatible;
4205}
4206
e5034e59
AM
4207/* Make a special call to the linker "notice" function to tell it that
4208 we are about to handle an as-needed lib, or have finished
1b786873 4209 processing the lib. */
e5034e59 4210
0a1b45a2 4211bool
e5034e59
AM
4212_bfd_elf_notice_as_needed (bfd *ibfd,
4213 struct bfd_link_info *info,
4214 enum notice_asneeded_action act)
4215{
46135103 4216 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
4217}
4218
3747999c 4219/* Call ACTION on each relocation in an ELF object file. */
d9689752 4220
0a1b45a2 4221bool
3747999c
L
4222_bfd_elf_link_iterate_on_relocs
4223 (bfd *abfd, struct bfd_link_info *info,
4224 bool (*action) (bfd *, struct bfd_link_info *, asection *,
4225 const Elf_Internal_Rela *))
d9689752
L
4226{
4227 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4228 struct elf_link_hash_table *htab = elf_hash_table (info);
4229
4230 /* If this object is the same format as the output object, and it is
4231 not a shared library, then let the backend look through the
4232 relocs.
4233
4234 This is required to build global offset table entries and to
4235 arrange for dynamic relocs. It is not required for the
4236 particular common case of linking non PIC code, even when linking
4237 against shared libraries, but unfortunately there is no way of
4238 knowing whether an object file has been compiled PIC or not.
4239 Looking through the relocs is not particularly time consuming.
4240 The problem is that we must either (1) keep the relocs in memory,
4241 which causes the linker to require additional runtime memory or
4242 (2) read the relocs twice from the input file, which wastes time.
4243 This would be a good case for using mmap.
4244
4245 I have no idea how to handle linking PIC code into a file of a
4246 different format. It probably can't be done. */
4247 if ((abfd->flags & DYNAMIC) == 0
2cc15b10 4248 && is_elf_hash_table (&htab->root)
d9689752
L
4249 && elf_object_id (abfd) == elf_hash_table_id (htab)
4250 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4251 {
4252 asection *o;
4253
4254 for (o = abfd->sections; o != NULL; o = o->next)
4255 {
4256 Elf_Internal_Rela *internal_relocs;
0a1b45a2 4257 bool ok;
d9689752 4258
c4b126b8
L
4259 /* Don't check relocations in excluded sections. Don't do
4260 anything special with non-loaded, non-alloced sections.
4261 In particular, any relocs in such sections should not
4262 affect GOT and PLT reference counting (ie. we don't
4263 allow them to create GOT or PLT entries), there's no
4264 possibility or desire to optimize TLS relocs, and
4265 there's not much point in propagating relocs to shared
4266 libs that the dynamic linker won't relocate. */
4267 if ((o->flags & SEC_ALLOC) == 0
4268 || (o->flags & SEC_RELOC) == 0
5ce03cea 4269 || (o->flags & SEC_EXCLUDE) != 0
d9689752
L
4270 || o->reloc_count == 0
4271 || ((info->strip == strip_all || info->strip == strip_debugger)
4272 && (o->flags & SEC_DEBUGGING) != 0)
4273 || bfd_is_abs_section (o->output_section))
4274 continue;
4275
d41b7648
L
4276 internal_relocs = _bfd_elf_link_info_read_relocs
4277 (abfd, info, o, NULL, NULL,
4278 _bfd_elf_link_keep_memory (info));
d9689752 4279 if (internal_relocs == NULL)
0a1b45a2 4280 return false;
d9689752 4281
3747999c 4282 ok = action (abfd, info, o, internal_relocs);
d9689752
L
4283
4284 if (elf_section_data (o)->relocs != internal_relocs)
4285 free (internal_relocs);
4286
4287 if (! ok)
0a1b45a2 4288 return false;
d9689752
L
4289 }
4290 }
4291
0a1b45a2 4292 return true;
d9689752
L
4293}
4294
3747999c
L
4295/* Check relocations in an ELF object file. This is called after
4296 all input files have been opened. */
4297
4298bool
4299_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
4300{
4301 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4302 if (bed->check_relocs != NULL)
4303 return _bfd_elf_link_iterate_on_relocs (abfd, info,
4304 bed->check_relocs);
4305 return true;
4306}
4307
816fd3dc
L
4308/* An entry in the first definition hash table. */
4309
4310struct elf_link_first_hash_entry
4311{
4312 struct bfd_hash_entry root;
4313 /* The object of the first definition. */
4314 bfd *abfd;
4315};
4316
4317/* The function to create a new entry in the first definition hash
4318 table. */
4319
4320static struct bfd_hash_entry *
4321elf_link_first_hash_newfunc (struct bfd_hash_entry *entry,
4322 struct bfd_hash_table *table,
4323 const char *string)
4324{
4325 struct elf_link_first_hash_entry *ret =
4326 (struct elf_link_first_hash_entry *) entry;
4327
4328 /* Allocate the structure if it has not already been allocated by a
4329 subclass. */
4330 if (ret == NULL)
4331 ret = (struct elf_link_first_hash_entry *)
4332 bfd_hash_allocate (table,
4333 sizeof (struct elf_link_first_hash_entry));
4334 if (ret == NULL)
4335 return NULL;
4336
4337 /* Call the allocation method of the superclass. */
4338 ret = ((struct elf_link_first_hash_entry *)
4339 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table,
4340 string));
4341 if (ret != NULL)
4342 ret->abfd = NULL;
4343
4344 return (struct bfd_hash_entry *) ret;
4345}
4346
c0419c02
L
4347/* Add the symbol NAME from ABFD to first hash. */
4348
4349static void
4350elf_link_add_to_first_hash (bfd *abfd, struct bfd_link_info *info,
e7e05a9d 4351 const char *name, bool copy)
c0419c02
L
4352{
4353 struct elf_link_hash_table *htab = elf_hash_table (info);
4354 /* Skip if there is no first hash. */
4355 if (htab->first_hash == NULL)
4356 return;
4357
816fd3dc
L
4358 struct elf_link_first_hash_entry *e
4359 = ((struct elf_link_first_hash_entry *)
e7e05a9d 4360 bfd_hash_lookup (htab->first_hash, name, true, copy));
c0419c02 4361 if (e == NULL)
d2616191
AM
4362 info->callbacks->fatal
4363 (_("%P: %pB: failed to add %s to first hash\n"), abfd, name);
c0419c02 4364
816fd3dc
L
4365 if (e->abfd == NULL)
4366 /* Store ABFD in abfd. */
4367 e->abfd = abfd;
c0419c02
L
4368}
4369
4ad4eba5
AM
4370/* Add symbols from an ELF object file to the linker hash table. */
4371
0a1b45a2 4372static bool
4ad4eba5
AM
4373elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4374{
a0c402a5 4375 Elf_Internal_Ehdr *ehdr;
4ad4eba5 4376 Elf_Internal_Shdr *hdr;
ef53be89
AM
4377 size_t symcount;
4378 size_t extsymcount;
4379 size_t extsymoff;
4ad4eba5 4380 struct elf_link_hash_entry **sym_hash;
0a1b45a2 4381 bool dynamic;
4ad4eba5 4382 Elf_External_Versym *extversym = NULL;
be22c732 4383 Elf_External_Versym *extversym_end = NULL;
4ad4eba5
AM
4384 Elf_External_Versym *ever;
4385 struct elf_link_hash_entry *weaks;
4386 struct elf_link_hash_entry **nondeflt_vers = NULL;
ef53be89 4387 size_t nondeflt_vers_cnt = 0;
4ad4eba5
AM
4388 Elf_Internal_Sym *isymbuf = NULL;
4389 Elf_Internal_Sym *isym;
4390 Elf_Internal_Sym *isymend;
4391 const struct elf_backend_data *bed;
0a1b45a2 4392 bool add_needed;
66eb6687 4393 struct elf_link_hash_table *htab;
66eb6687 4394 void *alloc_mark = NULL;
4f87808c
AM
4395 struct bfd_hash_entry **old_table = NULL;
4396 unsigned int old_size = 0;
4397 unsigned int old_count = 0;
66eb6687 4398 void *old_tab = NULL;
66eb6687
AM
4399 void *old_ent;
4400 struct bfd_link_hash_entry *old_undefs = NULL;
4401 struct bfd_link_hash_entry *old_undefs_tail = NULL;
5b677558 4402 void *old_strtab = NULL;
66eb6687 4403 size_t tabsize = 0;
db6a5d5f 4404 asection *s;
0a1b45a2 4405 bool just_syms;
4ad4eba5 4406
66eb6687 4407 htab = elf_hash_table (info);
4ad4eba5 4408 bed = get_elf_backend_data (abfd);
4ad4eba5 4409
46675b6b
L
4410 if (elf_use_dt_symtab_p (abfd))
4411 {
4412 bfd_set_error (bfd_error_wrong_format);
4413 return false;
4414 }
4415
4ad4eba5 4416 if ((abfd->flags & DYNAMIC) == 0)
c0419c02
L
4417 {
4418 dynamic = false;
4419 if ((abfd->flags & BFD_PLUGIN) != 0
4420 && is_elf_hash_table (&htab->root)
4421 && htab->first_hash == NULL)
4422 {
4423 /* Initialize first_hash for an IR input. */
816fd3dc 4424 htab->first_hash = (struct bfd_hash_table *)
c411ee98
L
4425 bfd_malloc (sizeof (struct bfd_hash_table));
4426 if (htab->first_hash == NULL
4427 || !bfd_hash_table_init
4428 (htab->first_hash, elf_link_first_hash_newfunc,
4429 sizeof (struct elf_link_first_hash_entry)))
d2616191
AM
4430 info->callbacks->fatal
4431 (_("%P: first_hash failed to create: %E\n"));
c0419c02
L
4432 }
4433 }
4ad4eba5
AM
4434 else
4435 {
0a1b45a2 4436 dynamic = true;
4ad4eba5
AM
4437
4438 /* You can't use -r against a dynamic object. Also, there's no
4439 hope of using a dynamic object which does not exactly match
4440 the format of the output file. */
0e1862bb 4441 if (bfd_link_relocatable (info)
2cc15b10 4442 || !is_elf_hash_table (&htab->root)
f13a99db 4443 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 4444 {
0e1862bb 4445 if (bfd_link_relocatable (info))
9a0789ec
NC
4446 bfd_set_error (bfd_error_invalid_operation);
4447 else
4448 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
4449 goto error_return;
4450 }
4451 }
4452
a0c402a5
L
4453 ehdr = elf_elfheader (abfd);
4454 if (info->warn_alternate_em
4455 && bed->elf_machine_code != ehdr->e_machine
4456 && ((bed->elf_machine_alt1 != 0
4457 && ehdr->e_machine == bed->elf_machine_alt1)
4458 || (bed->elf_machine_alt2 != 0
4459 && ehdr->e_machine == bed->elf_machine_alt2)))
9793eb77 4460 _bfd_error_handler
695344c0 4461 /* xgettext:c-format */
9793eb77 4462 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
a0c402a5
L
4463 ehdr->e_machine, abfd, bed->elf_machine_code);
4464
4ad4eba5
AM
4465 /* As a GNU extension, any input sections which are named
4466 .gnu.warning.SYMBOL are treated as warning symbols for the given
4467 symbol. This differs from .gnu.warning sections, which generate
4468 warnings when they are included in an output file. */
dd98f8d2 4469 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 4470 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 4471 {
db6a5d5f 4472 const char *name;
4ad4eba5 4473
fd361982 4474 name = bfd_section_name (s);
08dedd66 4475 if (startswith (name, ".gnu.warning."))
4ad4eba5 4476 {
db6a5d5f
AM
4477 char *msg;
4478 bfd_size_type sz;
4479
4480 name += sizeof ".gnu.warning." - 1;
4481
4482 /* If this is a shared object, then look up the symbol
4483 in the hash table. If it is there, and it is already
4484 been defined, then we will not be using the entry
4485 from this shared object, so we don't need to warn.
4486 FIXME: If we see the definition in a regular object
4487 later on, we will warn, but we shouldn't. The only
4488 fix is to keep track of what warnings we are supposed
4489 to emit, and then handle them all at the end of the
4490 link. */
4491 if (dynamic)
4ad4eba5 4492 {
db6a5d5f
AM
4493 struct elf_link_hash_entry *h;
4494
0a1b45a2 4495 h = elf_link_hash_lookup (htab, name, false, false, true);
db6a5d5f
AM
4496
4497 /* FIXME: What about bfd_link_hash_common? */
4498 if (h != NULL
4499 && (h->root.type == bfd_link_hash_defined
4500 || h->root.type == bfd_link_hash_defweak))
4501 continue;
4502 }
4ad4eba5 4503
db6a5d5f
AM
4504 sz = s->size;
4505 msg = (char *) bfd_alloc (abfd, sz + 1);
4506 if (msg == NULL)
4507 goto error_return;
4ad4eba5 4508
db6a5d5f
AM
4509 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4510 goto error_return;
4ad4eba5 4511
db6a5d5f 4512 msg[sz] = '\0';
4ad4eba5 4513
db6a5d5f
AM
4514 if (! (_bfd_generic_link_add_one_symbol
4515 (info, abfd, name, BSF_WARNING, s, 0, msg,
0a1b45a2 4516 false, bed->collect, NULL)))
db6a5d5f 4517 goto error_return;
4ad4eba5 4518
0e1862bb 4519 if (bfd_link_executable (info))
db6a5d5f
AM
4520 {
4521 /* Clobber the section size so that the warning does
4522 not get copied into the output file. */
4523 s->size = 0;
11d2f718 4524
db6a5d5f
AM
4525 /* Also set SEC_EXCLUDE, so that symbols defined in
4526 the warning section don't get copied to the output. */
4527 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
4528 }
4529 }
4530 }
4531
29a9f53e
L
4532 just_syms = ((s = abfd->sections) != NULL
4533 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4534
0a1b45a2 4535 add_needed = true;
4ad4eba5
AM
4536 if (! dynamic)
4537 {
4538 /* If we are creating a shared library, create all the dynamic
4539 sections immediately. We need to attach them to something,
4540 so we attach them to this BFD, provided it is the right
bf89386a
L
4541 format and is not from ld --just-symbols. Always create the
4542 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
4543 are no input BFD's of the same format as the output, we can't
4544 make a shared library. */
4545 if (!just_syms
bf89386a 4546 && (bfd_link_pic (info)
9c1d7a08 4547 || (!bfd_link_relocatable (info)
3c5fce9b 4548 && info->nointerp
9c1d7a08 4549 && (info->export_dynamic || info->dynamic)))
2cc15b10 4550 && is_elf_hash_table (&htab->root)
f13a99db 4551 && info->output_bfd->xvec == abfd->xvec
66eb6687 4552 && !htab->dynamic_sections_created)
4ad4eba5
AM
4553 {
4554 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4555 goto error_return;
4556 }
4557 }
2cc15b10 4558 else if (!is_elf_hash_table (&htab->root))
4ad4eba5
AM
4559 goto error_return;
4560 else
4561 {
4ad4eba5 4562 const char *soname = NULL;
7ee314fa 4563 char *audit = NULL;
4ad4eba5 4564 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
9acc85a6 4565 const Elf_Internal_Phdr *phdr;
e310298c 4566 struct elf_link_loaded_list *loaded_lib;
4ad4eba5
AM
4567
4568 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 4569 ld shouldn't allow it. */
29a9f53e 4570 if (just_syms)
92fd189d 4571 abort ();
4ad4eba5
AM
4572
4573 /* If this dynamic lib was specified on the command line with
4574 --as-needed in effect, then we don't want to add a DT_NEEDED
4575 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
4576 in by another lib's DT_NEEDED. When --no-add-needed is used
4577 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4578 any dynamic library in DT_NEEDED tags in the dynamic lib at
4579 all. */
4580 add_needed = (elf_dyn_lib_class (abfd)
4581 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4582 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
4583
4584 s = bfd_get_section_by_name (abfd, ".dynamic");
81ff113f 4585 if (s != NULL && s->size != 0 && (s->flags & SEC_HAS_CONTENTS) != 0)
4ad4eba5
AM
4586 {
4587 bfd_byte *dynbuf;
4588 bfd_byte *extdyn;
cb33740c 4589 unsigned int elfsec;
4ad4eba5
AM
4590 unsigned long shlink;
4591
584b30e4 4592 if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
f8703194 4593 {
dc1e8a47 4594 error_free_dyn:
584b30e4 4595 _bfd_elf_munmap_section_contents (s, dynbuf);
f8703194
L
4596 goto error_return;
4597 }
4ad4eba5
AM
4598
4599 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 4600 if (elfsec == SHN_BAD)
4ad4eba5
AM
4601 goto error_free_dyn;
4602 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4603
4604 for (extdyn = dynbuf;
37c59664 4605 (size_t) (dynbuf + s->size - extdyn) >= bed->s->sizeof_dyn;
4ad4eba5
AM
4606 extdyn += bed->s->sizeof_dyn)
4607 {
4608 Elf_Internal_Dyn dyn;
4609
4610 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4611 if (dyn.d_tag == DT_SONAME)
4612 {
4613 unsigned int tagv = dyn.d_un.d_val;
4614 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4615 if (soname == NULL)
4616 goto error_free_dyn;
4617 }
4618 if (dyn.d_tag == DT_NEEDED)
4619 {
4620 struct bfd_link_needed_list *n, **pn;
4621 char *fnm, *anm;
4622 unsigned int tagv = dyn.d_un.d_val;
986f0783 4623 size_t amt = sizeof (struct bfd_link_needed_list);
4ad4eba5 4624
a50b1753 4625 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4626 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4627 if (n == NULL || fnm == NULL)
4628 goto error_free_dyn;
4629 amt = strlen (fnm) + 1;
a50b1753 4630 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4631 if (anm == NULL)
4632 goto error_free_dyn;
4633 memcpy (anm, fnm, amt);
4634 n->name = anm;
4635 n->by = abfd;
4636 n->next = NULL;
66eb6687 4637 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4638 ;
4639 *pn = n;
4640 }
4641 if (dyn.d_tag == DT_RUNPATH)
4642 {
4643 struct bfd_link_needed_list *n, **pn;
4644 char *fnm, *anm;
4645 unsigned int tagv = dyn.d_un.d_val;
986f0783 4646 size_t amt = sizeof (struct bfd_link_needed_list);
4ad4eba5 4647
a50b1753 4648 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4649 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4650 if (n == NULL || fnm == NULL)
4651 goto error_free_dyn;
4652 amt = strlen (fnm) + 1;
a50b1753 4653 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4654 if (anm == NULL)
4655 goto error_free_dyn;
4656 memcpy (anm, fnm, amt);
4657 n->name = anm;
4658 n->by = abfd;
4659 n->next = NULL;
4660 for (pn = & runpath;
4661 *pn != NULL;
4662 pn = &(*pn)->next)
4663 ;
4664 *pn = n;
4665 }
4666 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4667 if (!runpath && dyn.d_tag == DT_RPATH)
4668 {
4669 struct bfd_link_needed_list *n, **pn;
4670 char *fnm, *anm;
4671 unsigned int tagv = dyn.d_un.d_val;
986f0783 4672 size_t amt = sizeof (struct bfd_link_needed_list);
4ad4eba5 4673
a50b1753 4674 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4675 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4676 if (n == NULL || fnm == NULL)
4677 goto error_free_dyn;
4678 amt = strlen (fnm) + 1;
a50b1753 4679 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 4680 if (anm == NULL)
f8703194 4681 goto error_free_dyn;
4ad4eba5
AM
4682 memcpy (anm, fnm, amt);
4683 n->name = anm;
4684 n->by = abfd;
4685 n->next = NULL;
4686 for (pn = & rpath;
4687 *pn != NULL;
4688 pn = &(*pn)->next)
4689 ;
4690 *pn = n;
4691 }
7ee314fa
AM
4692 if (dyn.d_tag == DT_AUDIT)
4693 {
4694 unsigned int tagv = dyn.d_un.d_val;
4695 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4696 }
f64b9b13
AM
4697 if (dyn.d_tag == DT_FLAGS_1)
4698 elf_tdata (abfd)->is_pie = (dyn.d_un.d_val & DF_1_PIE) != 0;
4ad4eba5
AM
4699 }
4700
584b30e4 4701 _bfd_elf_munmap_section_contents (s, dynbuf);
4ad4eba5
AM
4702 }
4703
4704 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4705 frees all more recently bfd_alloc'd blocks as well. */
4706 if (runpath)
4707 rpath = runpath;
4708
4709 if (rpath)
4710 {
4711 struct bfd_link_needed_list **pn;
66eb6687 4712 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4713 ;
4714 *pn = rpath;
4715 }
4716
9acc85a6
AM
4717 /* If we have a PT_GNU_RELRO program header, mark as read-only
4718 all sections contained fully therein. This makes relro
4719 shared library sections appear as they will at run-time. */
4720 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
54025d58 4721 while (phdr-- > elf_tdata (abfd)->phdr)
9acc85a6
AM
4722 if (phdr->p_type == PT_GNU_RELRO)
4723 {
4724 for (s = abfd->sections; s != NULL; s = s->next)
502794d4
CE
4725 {
4726 unsigned int opb = bfd_octets_per_byte (abfd, s);
4727
4728 if ((s->flags & SEC_ALLOC) != 0
4729 && s->vma * opb >= phdr->p_vaddr
4730 && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4731 s->flags |= SEC_READONLY;
4732 }
9acc85a6
AM
4733 break;
4734 }
4735
4ad4eba5
AM
4736 /* We do not want to include any of the sections in a dynamic
4737 object in the output file. We hack by simply clobbering the
4738 list of sections in the BFD. This could be handled more
4739 cleanly by, say, a new section flag; the existing
4740 SEC_NEVER_LOAD flag is not the one we want, because that one
4741 still implies that the section takes up space in the output
4742 file. */
4743 bfd_section_list_clear (abfd);
4744
4ad4eba5
AM
4745 /* Find the name to use in a DT_NEEDED entry that refers to this
4746 object. If the object has a DT_SONAME entry, we use it.
4747 Otherwise, if the generic linker stuck something in
4748 elf_dt_name, we use that. Otherwise, we just use the file
4749 name. */
4750 if (soname == NULL || *soname == '\0')
4751 {
4752 soname = elf_dt_name (abfd);
4753 if (soname == NULL || *soname == '\0')
4754 soname = bfd_get_filename (abfd);
4755 }
4756
4757 /* Save the SONAME because sometimes the linker emulation code
4758 will need to know it. */
4759 elf_dt_name (abfd) = soname;
4760
4ad4eba5
AM
4761 /* If we have already included this dynamic object in the
4762 link, just ignore it. There is no reason to include a
4763 particular dynamic object more than once. */
e310298c
AM
4764 for (loaded_lib = htab->dyn_loaded;
4765 loaded_lib != NULL;
4766 loaded_lib = loaded_lib->next)
4767 {
4768 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
0a1b45a2 4769 return true;
e310298c
AM
4770 }
4771
4772 /* Create dynamic sections for backends that require that be done
4773 before setup_gnu_properties. */
4774 if (add_needed
4775 && !_bfd_elf_link_create_dynamic_sections (abfd, info))
0a1b45a2 4776 return false;
7ee314fa
AM
4777
4778 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 4779 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
4780 }
4781
4782 /* If this is a dynamic object, we always link against the .dynsym
4783 symbol table, not the .symtab symbol table. The dynamic linker
4784 will only see the .dynsym symbol table, so there is no reason to
4785 look at .symtab for a dynamic object. */
4786
4787 if (! dynamic || elf_dynsymtab (abfd) == 0)
4788 hdr = &elf_tdata (abfd)->symtab_hdr;
4789 else
4790 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4791
4792 symcount = hdr->sh_size / bed->s->sizeof_sym;
4793
4794 /* The sh_info field of the symtab header tells us where the
4795 external symbols start. We don't care about the local symbols at
4796 this point. */
4797 if (elf_bad_symtab (abfd))
4798 {
4799 extsymcount = symcount;
4800 extsymoff = 0;
4801 }
4802 else
4803 {
4804 extsymcount = symcount - hdr->sh_info;
4805 extsymoff = hdr->sh_info;
4806 }
4807
f45794cb 4808 sym_hash = elf_sym_hashes (abfd);
012b2306 4809 if (extsymcount != 0)
4ad4eba5
AM
4810 {
4811 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4812 NULL, NULL, NULL);
4813 if (isymbuf == NULL)
4814 goto error_return;
4815
4ad4eba5 4816 if (sym_hash == NULL)
012b2306
AM
4817 {
4818 /* We store a pointer to the hash table entry for each
4819 external symbol. */
986f0783 4820 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
012b2306
AM
4821 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4822 if (sym_hash == NULL)
4823 goto error_free_sym;
4824 elf_sym_hashes (abfd) = sym_hash;
4825 }
4ad4eba5
AM
4826 }
4827
4828 if (dynamic)
4829 {
4830 /* Read in any version definitions. */
fc0e6df6
PB
4831 if (!_bfd_elf_slurp_version_tables (abfd,
4832 info->default_imported_symver))
4ad4eba5
AM
4833 goto error_free_sym;
4834
4835 /* Read in the symbol versions, but don't bother to convert them
4836 to internal format. */
4837 if (elf_dynversym (abfd) != 0)
4838 {
986f0783
AM
4839 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4840 bfd_size_type amt = versymhdr->sh_size;
4ad4eba5 4841
2bb3687b
AM
4842 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4843 goto error_free_sym;
4844 extversym = (Elf_External_Versym *)
4845 _bfd_malloc_and_read (abfd, amt, amt);
4ad4eba5
AM
4846 if (extversym == NULL)
4847 goto error_free_sym;
986f0783 4848 extversym_end = extversym + amt / sizeof (*extversym);
4ad4eba5
AM
4849 }
4850 }
4851
66eb6687
AM
4852 /* If we are loading an as-needed shared lib, save the symbol table
4853 state before we start adding symbols. If the lib turns out
4854 to be unneeded, restore the state. */
4855 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4856 {
4857 unsigned int i;
4858 size_t entsize;
4859
4860 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4861 {
4862 struct bfd_hash_entry *p;
2de92251 4863 struct elf_link_hash_entry *h;
66eb6687
AM
4864
4865 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
4866 {
4867 h = (struct elf_link_hash_entry *) p;
4868 entsize += htab->root.table.entsize;
4869 if (h->root.type == bfd_link_hash_warning)
7ba11550
AM
4870 {
4871 entsize += htab->root.table.entsize;
4872 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4873 }
4874 if (h->root.type == bfd_link_hash_common)
4875 entsize += sizeof (*h->root.u.c.p);
2de92251 4876 }
66eb6687
AM
4877 }
4878
4879 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 4880 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
4881 if (old_tab == NULL)
4882 goto error_free_vers;
4883
4884 /* Remember the current objalloc pointer, so that all mem for
4885 symbols added can later be reclaimed. */
4886 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4887 if (alloc_mark == NULL)
4888 goto error_free_vers;
4889
5061a885
AM
4890 /* Make a special call to the linker "notice" function to
4891 tell it that we are about to handle an as-needed lib. */
e5034e59 4892 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 4893 goto error_free_vers;
5061a885 4894
f45794cb
AM
4895 /* Clone the symbol table. Remember some pointers into the
4896 symbol table, and dynamic symbol count. */
4897 old_ent = (char *) old_tab + tabsize;
66eb6687 4898 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
4899 old_undefs = htab->root.undefs;
4900 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
4901 old_table = htab->root.table.table;
4902 old_size = htab->root.table.size;
4903 old_count = htab->root.table.count;
e310298c
AM
4904 old_strtab = NULL;
4905 if (htab->dynstr != NULL)
4906 {
4907 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4908 if (old_strtab == NULL)
4909 goto error_free_vers;
4910 }
66eb6687
AM
4911
4912 for (i = 0; i < htab->root.table.size; i++)
4913 {
4914 struct bfd_hash_entry *p;
2de92251 4915 struct elf_link_hash_entry *h;
66eb6687
AM
4916
4917 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4918 {
2de92251 4919 h = (struct elf_link_hash_entry *) p;
7ba11550
AM
4920 memcpy (old_ent, h, htab->root.table.entsize);
4921 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4922 if (h->root.type == bfd_link_hash_warning)
4923 {
7ba11550
AM
4924 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4925 memcpy (old_ent, h, htab->root.table.entsize);
2de92251
AM
4926 old_ent = (char *) old_ent + htab->root.table.entsize;
4927 }
7ba11550
AM
4928 if (h->root.type == bfd_link_hash_common)
4929 {
4930 memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
4931 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
4932 }
66eb6687
AM
4933 }
4934 }
4935 }
4ad4eba5 4936
66eb6687 4937 weaks = NULL;
be22c732
NC
4938 if (extversym == NULL)
4939 ever = NULL;
4940 else if (extversym + extsymoff < extversym_end)
4941 ever = extversym + extsymoff;
4942 else
4943 {
4944 /* xgettext:c-format */
4945 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4946 abfd, (long) extsymoff,
4947 (long) (extversym_end - extversym) / sizeof (* extversym));
4948 bfd_set_error (bfd_error_bad_value);
4949 goto error_free_vers;
4950 }
4951
b4c555cf 4952 if (!bfd_link_relocatable (info)
bb9a951f 4953 && bfd_get_lto_type (abfd) == lto_slim_ir_object)
cc5277b1
ML
4954 {
4955 _bfd_error_handler
4956 (_("%pB: plugin needed to handle lto object"), abfd);
4957 }
4958
36f61bf2 4959 for (isym = isymbuf, isymend = PTR_ADD (isymbuf, extsymcount);
4ad4eba5
AM
4960 isym < isymend;
4961 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4962 {
4963 int bind;
4964 bfd_vma value;
af44c138 4965 asection *sec, *new_sec;
4ad4eba5
AM
4966 flagword flags;
4967 const char *name;
2707d55e 4968 const char *defvername;
e7e05a9d 4969 bool must_copy_name = false;
4ad4eba5 4970 struct elf_link_hash_entry *h;
90c984fc 4971 struct elf_link_hash_entry *hi;
0a1b45a2
AM
4972 bool definition;
4973 bool size_change_ok;
4974 bool type_change_ok;
4975 bool new_weak;
4976 bool old_weak;
7ba11550 4977 bfd *override;
0a1b45a2
AM
4978 bool common;
4979 bool discarded;
4ad4eba5 4980 unsigned int old_alignment;
4538d1c7 4981 unsigned int shindex;
4ad4eba5 4982 bfd *old_bfd;
0a1b45a2 4983 bool matched;
4ad4eba5 4984
7ba11550 4985 override = NULL;
4ad4eba5
AM
4986
4987 flags = BSF_NO_FLAGS;
4988 sec = NULL;
4989 value = isym->st_value;
a4d8e49b 4990 common = bed->common_definition (isym);
2980ccad
L
4991 if (common && info->inhibit_common_definition)
4992 {
4993 /* Treat common symbol as undefined for --no-define-common. */
4994 isym->st_shndx = SHN_UNDEF;
0a1b45a2 4995 common = false;
2980ccad 4996 }
0a1b45a2 4997 discarded = false;
4ad4eba5
AM
4998
4999 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 5000 switch (bind)
4ad4eba5 5001 {
3e7a7d11 5002 case STB_LOCAL:
4ad4eba5
AM
5003 /* This should be impossible, since ELF requires that all
5004 global symbols follow all local symbols, and that sh_info
5005 point to the first global symbol. Unfortunately, Irix 5
5006 screws this up. */
fe3fef62
AM
5007 if (elf_bad_symtab (abfd))
5008 continue;
5009
5010 /* If we aren't prepared to handle locals within the globals
4538d1c7
AM
5011 then we'll likely segfault on a NULL symbol hash if the
5012 symbol is ever referenced in relocations. */
5013 shindex = elf_elfheader (abfd)->e_shstrndx;
5014 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
5015 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
5016 " (>= sh_info of %lu)"),
5017 abfd, name, (long) (isym - isymbuf + extsymoff),
5018 (long) extsymoff);
5019
5020 /* Dynamic object relocations are not processed by ld, so
5021 ld won't run into the problem mentioned above. */
5022 if (dynamic)
5023 continue;
fe3fef62
AM
5024 bfd_set_error (bfd_error_bad_value);
5025 goto error_free_vers;
3e7a7d11
NC
5026
5027 case STB_GLOBAL:
a4d8e49b 5028 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 5029 flags = BSF_GLOBAL;
3e7a7d11
NC
5030 break;
5031
5032 case STB_WEAK:
5033 flags = BSF_WEAK;
5034 break;
5035
5036 case STB_GNU_UNIQUE:
5037 flags = BSF_GNU_UNIQUE;
5038 break;
5039
5040 default:
4ad4eba5 5041 /* Leave it up to the processor backend. */
3e7a7d11 5042 break;
4ad4eba5
AM
5043 }
5044
5045 if (isym->st_shndx == SHN_UNDEF)
5046 sec = bfd_und_section_ptr;
cb33740c
AM
5047 else if (isym->st_shndx == SHN_ABS)
5048 sec = bfd_abs_section_ptr;
5049 else if (isym->st_shndx == SHN_COMMON)
5050 {
5051 sec = bfd_com_section_ptr;
5052 /* What ELF calls the size we call the value. What ELF
5053 calls the value we call the alignment. */
5054 value = isym->st_size;
5055 }
5056 else
4ad4eba5
AM
5057 {
5058 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
5059 if (sec == NULL)
5060 sec = bfd_abs_section_ptr;
dbaa2011 5061 else if (discarded_section (sec))
529fcb95 5062 {
e5d08002
L
5063 /* Symbols from discarded section are undefined. We keep
5064 its visibility. */
529fcb95 5065 sec = bfd_und_section_ptr;
0a1b45a2 5066 discarded = true;
529fcb95
PB
5067 isym->st_shndx = SHN_UNDEF;
5068 }
4ad4eba5
AM
5069 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
5070 value -= sec->vma;
5071 }
4ad4eba5
AM
5072
5073 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5074 isym->st_name);
5075 if (name == NULL)
5076 goto error_free_vers;
5077
5078 if (isym->st_shndx == SHN_COMMON
02d00247
AM
5079 && (abfd->flags & BFD_PLUGIN) != 0)
5080 {
5081 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
5082
5083 if (xc == NULL)
5084 {
5085 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
5086 | SEC_EXCLUDE);
5087 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
5088 if (xc == NULL)
5089 goto error_free_vers;
5090 }
5091 sec = xc;
5092 }
5093 else if (isym->st_shndx == SHN_COMMON
5094 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 5095 && !bfd_link_relocatable (info))
4ad4eba5
AM
5096 {
5097 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
5098
5099 if (tcomm == NULL)
5100 {
02d00247
AM
5101 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
5102 | SEC_LINKER_CREATED);
5103 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 5104 if (tcomm == NULL)
4ad4eba5
AM
5105 goto error_free_vers;
5106 }
5107 sec = tcomm;
5108 }
66eb6687 5109 else if (bed->elf_add_symbol_hook)
4ad4eba5 5110 {
66eb6687
AM
5111 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
5112 &sec, &value))
4ad4eba5
AM
5113 goto error_free_vers;
5114
5115 /* The hook function sets the name to NULL if this symbol
5116 should be skipped for some reason. */
5117 if (name == NULL)
5118 continue;
5119 }
5120
5121 /* Sanity check that all possibilities were handled. */
5122 if (sec == NULL)
4538d1c7 5123 abort ();
4ad4eba5 5124
191c0c42
AM
5125 /* Silently discard TLS symbols from --just-syms. There's
5126 no way to combine a static TLS block with a new TLS block
5127 for this executable. */
5128 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
5129 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
5130 continue;
5131
4ad4eba5
AM
5132 if (bfd_is_und_section (sec)
5133 || bfd_is_com_section (sec))
0a1b45a2 5134 definition = false;
4ad4eba5 5135 else
0a1b45a2 5136 definition = true;
4ad4eba5 5137
0a1b45a2 5138 size_change_ok = false;
66eb6687 5139 type_change_ok = bed->type_change_ok;
0a1b45a2
AM
5140 old_weak = false;
5141 matched = false;
4ad4eba5
AM
5142 old_alignment = 0;
5143 old_bfd = NULL;
af44c138 5144 new_sec = sec;
2707d55e 5145 defvername = NULL;
4ad4eba5 5146
2cc15b10 5147 if (is_elf_hash_table (&htab->root))
4ad4eba5
AM
5148 {
5149 Elf_Internal_Versym iver;
5150 unsigned int vernum = 0;
0a1b45a2 5151 bool skip;
4ad4eba5 5152
fc0e6df6 5153 if (ever == NULL)
4ad4eba5 5154 {
fc0e6df6
PB
5155 if (info->default_imported_symver)
5156 /* Use the default symbol version created earlier. */
5157 iver.vs_vers = elf_tdata (abfd)->cverdefs;
5158 else
5159 iver.vs_vers = 0;
5160 }
be22c732
NC
5161 else if (ever >= extversym_end)
5162 {
5163 /* xgettext:c-format */
5164 _bfd_error_handler (_("%pB: not enough version information"),
5165 abfd);
5166 bfd_set_error (bfd_error_bad_value);
5167 goto error_free_vers;
5168 }
fc0e6df6
PB
5169 else
5170 _bfd_elf_swap_versym_in (abfd, ever, &iver);
5171
5172 vernum = iver.vs_vers & VERSYM_VERSION;
5173
5174 /* If this is a hidden symbol, or if it is not version
5175 1, we append the version name to the symbol name.
cc86ff91
EB
5176 However, we do not modify a non-hidden absolute symbol
5177 if it is not a function, because it might be the version
5178 symbol itself. FIXME: What if it isn't? */
fc0e6df6 5179 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
5180 || (vernum > 1
5181 && (!bfd_is_abs_section (sec)
5182 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
5183 {
5184 const char *verstr;
5185 size_t namelen, verlen, newlen;
5186 char *newname, *p;
5187
5188 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 5189 {
fc0e6df6
PB
5190 if (vernum > elf_tdata (abfd)->cverdefs)
5191 verstr = NULL;
5192 else if (vernum > 1)
5193 verstr =
5194 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
5195 else
5196 verstr = "";
4ad4eba5 5197
fc0e6df6 5198 if (verstr == NULL)
4ad4eba5 5199 {
4eca0228 5200 _bfd_error_handler
695344c0 5201 /* xgettext:c-format */
871b3ab2 5202 (_("%pB: %s: invalid version %u (max %d)"),
fc0e6df6
PB
5203 abfd, name, vernum,
5204 elf_tdata (abfd)->cverdefs);
5205 bfd_set_error (bfd_error_bad_value);
5206 goto error_free_vers;
4ad4eba5 5207 }
fc0e6df6
PB
5208 }
5209 else
5210 {
5211 /* We cannot simply test for the number of
5212 entries in the VERNEED section since the
5213 numbers for the needed versions do not start
5214 at 0. */
5215 Elf_Internal_Verneed *t;
5216
5217 verstr = NULL;
5218 for (t = elf_tdata (abfd)->verref;
5219 t != NULL;
5220 t = t->vn_nextref)
4ad4eba5 5221 {
fc0e6df6 5222 Elf_Internal_Vernaux *a;
4ad4eba5 5223
fc0e6df6
PB
5224 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5225 {
5226 if (a->vna_other == vernum)
4ad4eba5 5227 {
fc0e6df6
PB
5228 verstr = a->vna_nodename;
5229 break;
4ad4eba5 5230 }
4ad4eba5 5231 }
fc0e6df6
PB
5232 if (a != NULL)
5233 break;
5234 }
5235 if (verstr == NULL)
5236 {
4eca0228 5237 _bfd_error_handler
695344c0 5238 /* xgettext:c-format */
871b3ab2 5239 (_("%pB: %s: invalid needed version %d"),
fc0e6df6
PB
5240 abfd, name, vernum);
5241 bfd_set_error (bfd_error_bad_value);
5242 goto error_free_vers;
4ad4eba5 5243 }
4ad4eba5 5244 }
fc0e6df6
PB
5245
5246 namelen = strlen (name);
5247 verlen = strlen (verstr);
5248 newlen = namelen + verlen + 2;
5249 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5250 && isym->st_shndx != SHN_UNDEF)
5251 ++newlen;
5252
a50b1753 5253 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
5254 if (newname == NULL)
5255 goto error_free_vers;
5256 memcpy (newname, name, namelen);
5257 p = newname + namelen;
5258 *p++ = ELF_VER_CHR;
5259 /* If this is a defined non-hidden version symbol,
5260 we add another @ to the name. This indicates the
5261 default version of the symbol. */
5262 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
5263 && isym->st_shndx != SHN_UNDEF)
2707d55e 5264 *p++ = ELF_VER_CHR, defvername = name;
fc0e6df6
PB
5265 memcpy (p, verstr, verlen + 1);
5266
5267 name = newname;
e7e05a9d
AM
5268 /* Since bfd_hash_alloc is used for "name", the string
5269 must be copied if added to first_hash. The string
5270 memory can be freed when an --as-needed library is
5271 not needed. */
5272 must_copy_name = true;
4ad4eba5
AM
5273 }
5274
cd3416da
AM
5275 /* If this symbol has default visibility and the user has
5276 requested we not re-export it, then mark it as hidden. */
a0d49154 5277 if (!bfd_is_und_section (sec)
cd3416da 5278 && !dynamic
ce875075 5279 && abfd->no_export
cd3416da
AM
5280 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
5281 isym->st_other = (STV_HIDDEN
5282 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
5283
4f3fedcf
AM
5284 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
5285 sym_hash, &old_bfd, &old_weak,
5286 &old_alignment, &skip, &override,
6e33951e
L
5287 &type_change_ok, &size_change_ok,
5288 &matched))
4ad4eba5
AM
5289 goto error_free_vers;
5290
5291 if (skip)
5292 continue;
5293
4ad4eba5
AM
5294 h = *sym_hash;
5295 while (h->root.type == bfd_link_hash_indirect
5296 || h->root.type == bfd_link_hash_warning)
5297 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5298
c0419c02
L
5299 /* Override a definition only if the new symbol matches the
5300 existing one. */
5301 if (override && matched)
5302 {
5303 definition = false;
5304 if (htab->first_hash != NULL
5305 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5306 && h->root.non_ir_ref_regular)
5307 {
5308 /* When reloading --as-needed shared objects for new
5309 symbols added from IR inputs, if this shared object
5310 has the first definition, use it. */
816fd3dc
L
5311 struct elf_link_first_hash_entry *e
5312 = ((struct elf_link_first_hash_entry *)
5313 bfd_hash_lookup (htab->first_hash, name, false,
5314 false));
5315 if (e != NULL && e->abfd == abfd)
c0419c02
L
5316 definition = true;
5317 }
5318 }
5319
e4675a58
L
5320 if (h->versioned != unversioned
5321 && elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
5322 && vernum > 1
5323 && definition)
5324 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
5325 }
5326
5327 if (! (_bfd_generic_link_add_one_symbol
7ba11550 5328 (info, override ? override : abfd, name, flags, sec, value,
0a1b45a2 5329 NULL, false, bed->collect,
4ad4eba5
AM
5330 (struct bfd_link_hash_entry **) sym_hash)))
5331 goto error_free_vers;
5332
5333 h = *sym_hash;
90c984fc
L
5334 /* We need to make sure that indirect symbol dynamic flags are
5335 updated. */
5336 hi = h;
4ad4eba5
AM
5337 while (h->root.type == bfd_link_hash_indirect
5338 || h->root.type == bfd_link_hash_warning)
5339 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 5340
d1cbe007
AM
5341 *sym_hash = h;
5342
97196564
L
5343 /* Setting the index to -3 tells elf_link_output_extsym that
5344 this symbol is defined in a discarded section. */
2cc15b10 5345 if (discarded && is_elf_hash_table (&htab->root))
97196564
L
5346 h->indx = -3;
5347
37a9e49a 5348 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
5349 if (dynamic
5350 && definition
37a9e49a 5351 && new_weak
fcb93ecf 5352 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
2cc15b10 5353 && is_elf_hash_table (&htab->root)
60d67dc8 5354 && h->u.alias == NULL)
4ad4eba5
AM
5355 {
5356 /* Keep a list of all weak defined non function symbols from
60d67dc8
AM
5357 a dynamic object, using the alias field. Later in this
5358 function we will set the alias field to the correct
4ad4eba5
AM
5359 value. We only put non-function symbols from dynamic
5360 objects on this list, because that happens to be the only
5361 time we need to know the normal symbol corresponding to a
5362 weak symbol, and the information is time consuming to
60d67dc8 5363 figure out. If the alias field is not already NULL,
4ad4eba5
AM
5364 then this symbol was already defined by some previous
5365 dynamic object, and we will be using that previous
5366 definition anyhow. */
5367
60d67dc8 5368 h->u.alias = weaks;
4ad4eba5 5369 weaks = h;
4ad4eba5
AM
5370 }
5371
5372 /* Set the alignment of a common symbol. */
a4d8e49b 5373 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
5374 && h->root.type == bfd_link_hash_common)
5375 {
5376 unsigned int align;
5377
a4d8e49b 5378 if (common)
af44c138
L
5379 align = bfd_log2 (isym->st_value);
5380 else
5381 {
5382 /* The new symbol is a common symbol in a shared object.
5383 We need to get the alignment from the section. */
5384 align = new_sec->alignment_power;
5385 }
595213d4 5386 if (align > old_alignment)
4ad4eba5
AM
5387 h->root.u.c.p->alignment_power = align;
5388 else
5389 h->root.u.c.p->alignment_power = old_alignment;
5390 }
5391
2cc15b10 5392 if (is_elf_hash_table (&htab->root))
4ad4eba5 5393 {
4f3fedcf
AM
5394 /* Set a flag in the hash table entry indicating the type of
5395 reference or definition we just found. A dynamic symbol
5396 is one which is referenced or defined by both a regular
5397 object and a shared object. */
0a1b45a2 5398 bool dynsym = false;
4f3fedcf 5399
b1a92c63
AM
5400 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5401 if ((abfd->flags & BFD_PLUGIN) != 0)
bbaddd4b
AM
5402 {
5403 /* Except for this flag to track nonweak references. */
5404 if (!definition
5405 && bind != STB_WEAK)
5406 h->ref_ir_nonweak = 1;
5407 }
b1a92c63 5408 else if (!dynamic)
4f3fedcf
AM
5409 {
5410 if (! definition)
5411 {
5412 h->ref_regular = 1;
5413 if (bind != STB_WEAK)
5414 h->ref_regular_nonweak = 1;
5415 }
5416 else
5417 {
5418 h->def_regular = 1;
5419 if (h->def_dynamic)
5420 {
5421 h->def_dynamic = 0;
5422 h->ref_dynamic = 1;
5423 }
5424 }
4f3fedcf
AM
5425 }
5426 else
5427 {
5428 if (! definition)
5429 {
5430 h->ref_dynamic = 1;
5431 hi->ref_dynamic = 1;
5432 }
5433 else
5434 {
5435 h->def_dynamic = 1;
5436 hi->def_dynamic = 1;
5437 }
b1a92c63 5438 }
4f3fedcf 5439
b1a92c63
AM
5440 /* If an indirect symbol has been forced local, don't
5441 make the real symbol dynamic. */
5442 if (h != hi && hi->forced_local)
5443 ;
5444 else if (!dynamic)
5445 {
5446 if (bfd_link_dll (info)
5447 || h->def_dynamic
5448 || h->ref_dynamic)
0a1b45a2 5449 dynsym = true;
b1a92c63
AM
5450 }
5451 else
5452 {
5453 if (h->def_regular
5454 || h->ref_regular
5455 || (h->is_weakalias
5456 && weakdef (h)->dynindx != -1))
0a1b45a2 5457 dynsym = true;
4f3fedcf
AM
5458 }
5459
5460 /* Check to see if we need to add an indirect symbol for
5461 the default name. */
726d7d1e
AM
5462 if ((definition
5463 || (!override && h->root.type == bfd_link_hash_common))
5464 && !(hi != h
5465 && hi->versioned == versioned_hidden))
4f3fedcf 5466 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
f01fb44c 5467 sec, value, &old_bfd, &dynsym))
4f3fedcf 5468 goto error_free_vers;
4ad4eba5
AM
5469
5470 /* Check the alignment when a common symbol is involved. This
5471 can change when a common symbol is overridden by a normal
5472 definition or a common symbol is ignored due to the old
5473 normal definition. We need to make sure the maximum
5474 alignment is maintained. */
a4d8e49b 5475 if ((old_alignment || common)
4ad4eba5
AM
5476 && h->root.type != bfd_link_hash_common)
5477 {
5478 unsigned int common_align;
5479 unsigned int normal_align;
5480 unsigned int symbol_align;
5481 bfd *normal_bfd;
5482 bfd *common_bfd;
5483
3a81e825
AM
5484 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5485 || h->root.type == bfd_link_hash_defweak);
5486
4ad4eba5
AM
5487 symbol_align = ffs (h->root.u.def.value) - 1;
5488 if (h->root.u.def.section->owner != NULL
0616a280
AM
5489 && (h->root.u.def.section->owner->flags
5490 & (DYNAMIC | BFD_PLUGIN)) == 0)
4ad4eba5
AM
5491 {
5492 normal_align = h->root.u.def.section->alignment_power;
5493 if (normal_align > symbol_align)
5494 normal_align = symbol_align;
5495 }
5496 else
5497 normal_align = symbol_align;
5498
5499 if (old_alignment)
5500 {
5501 common_align = old_alignment;
5502 common_bfd = old_bfd;
5503 normal_bfd = abfd;
5504 }
5505 else
5506 {
5507 common_align = bfd_log2 (isym->st_value);
5508 common_bfd = abfd;
5509 normal_bfd = old_bfd;
5510 }
5511
5512 if (normal_align < common_align)
d07676f8
NC
5513 {
5514 /* PR binutils/2735 */
5515 if (normal_bfd == NULL)
4eca0228 5516 _bfd_error_handler
695344c0 5517 /* xgettext:c-format */
9793eb77 5518 (_("warning: alignment %u of common symbol `%s' in %pB is"
871b3ab2 5519 " greater than the alignment (%u) of its section %pA"),
c08bb8dd
AM
5520 1 << common_align, name, common_bfd,
5521 1 << normal_align, h->root.u.def.section);
d07676f8 5522 else
4eca0228 5523 _bfd_error_handler
695344c0 5524 /* xgettext:c-format */
064ad3ea
NC
5525 (_("warning: alignment %u of normal symbol `%s' in %pB"
5526 " is smaller than %u used by the common definition in %pB"),
c08bb8dd
AM
5527 1 << normal_align, name, normal_bfd,
5528 1 << common_align, common_bfd);
064ad3ea
NC
5529
5530 /* PR 30499: make sure that users understand that this warning is serious. */
5531 _bfd_error_handler
5532 (_("warning: NOTE: alignment discrepancies can cause real problems. Investigation is advised."));
d07676f8 5533 }
4ad4eba5
AM
5534 }
5535
83ad0046 5536 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
5537 if (isym->st_size != 0
5538 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
5539 && (definition || h->size == 0))
5540 {
83ad0046
L
5541 if (h->size != 0
5542 && h->size != isym->st_size
5543 && ! size_change_ok)
064ad3ea
NC
5544 {
5545 _bfd_error_handler
5546 /* xgettext:c-format */
5547 (_("warning: size of symbol `%s' changed"
5548 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5549 name, (uint64_t) h->size, old_bfd,
5550 (uint64_t) isym->st_size, abfd);
5551
5552 /* PR 30499: make sure that users understand that this warning is serious. */
5553 _bfd_error_handler
5554 (_("warning: NOTE: size discrepancies can cause real problems. Investigation is advised."));
5555 }
4ad4eba5
AM
5556
5557 h->size = isym->st_size;
5558 }
5559
5560 /* If this is a common symbol, then we always want H->SIZE
5561 to be the size of the common symbol. The code just above
5562 won't fix the size if a common symbol becomes larger. We
5563 don't warn about a size change here, because that is
4f3fedcf 5564 covered by --warn-common. Allow changes between different
fcb93ecf 5565 function types. */
4ad4eba5
AM
5566 if (h->root.type == bfd_link_hash_common)
5567 h->size = h->root.u.c.size;
5568
5569 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
5570 && ((definition && !new_weak)
5571 || (old_weak && h->root.type == bfd_link_hash_common)
5572 || h->type == STT_NOTYPE))
4ad4eba5 5573 {
2955ec4c
L
5574 unsigned int type = ELF_ST_TYPE (isym->st_info);
5575
5576 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5577 symbol. */
5578 if (type == STT_GNU_IFUNC
5579 && (abfd->flags & DYNAMIC) != 0)
5580 type = STT_FUNC;
4ad4eba5 5581
2955ec4c
L
5582 if (h->type != type)
5583 {
5584 if (h->type != STT_NOTYPE && ! type_change_ok)
695344c0 5585 /* xgettext:c-format */
4eca0228 5586 _bfd_error_handler
9793eb77 5587 (_("warning: type of symbol `%s' changed"
871b3ab2 5588 " from %d to %d in %pB"),
c08bb8dd 5589 name, h->type, type, abfd);
2955ec4c
L
5590
5591 h->type = type;
5592 }
4ad4eba5
AM
5593 }
5594
54ac0771 5595 /* Merge st_other field. */
5160d0f3
AM
5596 elf_merge_st_other (abfd, h, isym->st_other, sec,
5597 definition, dynamic);
4ad4eba5 5598
c3df8c14 5599 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
5600 if (definition
5601 && (sec->flags & SEC_DEBUGGING)
5602 && !bfd_link_relocatable (info))
0a1b45a2 5603 dynsym = false;
c3df8c14 5604
b1a92c63
AM
5605 /* Nor should we make plugin symbols dynamic. */
5606 if ((abfd->flags & BFD_PLUGIN) != 0)
0a1b45a2 5607 dynsym = false;
b1a92c63 5608
35fc36a8 5609 if (definition)
35399224
L
5610 {
5611 h->target_internal = isym->st_target_internal;
5612 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5613 }
35fc36a8 5614
79cfb928
L
5615 /* Don't add indirect symbols for .symver x, x@FOO aliases
5616 in IR. Since all data or text symbols in IR have the
5617 same type, value and section, we can't tell if a symbol
5618 is an alias of another symbol by their types, values and
5619 sections. */
5620 if (definition
5621 && !dynamic
5622 && (abfd->flags & BFD_PLUGIN) == 0)
4ad4eba5
AM
5623 {
5624 char *p = strchr (name, ELF_VER_CHR);
5625 if (p != NULL && p[1] != ELF_VER_CHR)
5626 {
5627 /* Queue non-default versions so that .symver x, x@FOO
5628 aliases can be checked. */
66eb6687 5629 if (!nondeflt_vers)
4ad4eba5 5630 {
986f0783
AM
5631 size_t amt = ((isymend - isym + 1)
5632 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
5633 nondeflt_vers
5634 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
5635 if (!nondeflt_vers)
5636 goto error_free_vers;
4ad4eba5 5637 }
66eb6687 5638 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
5639 }
5640 }
5641
b1a92c63 5642 if (dynsym && h->dynindx == -1)
4ad4eba5 5643 {
c152c796 5644 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 5645 goto error_free_vers;
60d67dc8
AM
5646 if (h->is_weakalias
5647 && weakdef (h)->dynindx == -1)
4ad4eba5 5648 {
60d67dc8 5649 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
4ad4eba5
AM
5650 goto error_free_vers;
5651 }
5652 }
1f599d0e 5653 else if (h->dynindx != -1)
4ad4eba5
AM
5654 /* If the symbol already has a dynamic index, but
5655 visibility says it should not be visible, turn it into
5656 a local symbol. */
5657 switch (ELF_ST_VISIBILITY (h->other))
5658 {
5659 case STV_INTERNAL:
5660 case STV_HIDDEN:
0a1b45a2
AM
5661 (*bed->elf_backend_hide_symbol) (info, h, true);
5662 dynsym = false;
4ad4eba5
AM
5663 break;
5664 }
5665
5666 if (!add_needed
aef28989 5667 && matched
4ad4eba5 5668 && definition
c0419c02
L
5669 && h->root.type != bfd_link_hash_indirect)
5670 {
5671 if ((dynsym
a896df97 5672 && h->ref_regular_nonweak)
b1a92c63
AM
5673 || (old_bfd != NULL
5674 && (old_bfd->flags & BFD_PLUGIN) != 0
bbaddd4b
AM
5675 && h->ref_ir_nonweak
5676 && !info->lto_all_symbols_read)
ffa9430d 5677 || (h->ref_dynamic_nonweak
010e5ae2 5678 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
7b15fa7a 5679 && !on_needed_list (elf_dt_name (abfd),
c0419c02 5680 htab->needed, NULL)))
e56f61be 5681 {
c0419c02
L
5682 const char *soname = elf_dt_name (abfd);
5683
5684 info->callbacks->minfo ("%!", soname, old_bfd,
5685 h->root.root.string);
5686
5687 /* A symbol from a library loaded via DT_NEEDED of some
5688 other library is referenced by a regular object.
5689 Add a DT_NEEDED entry for it. Issue an error if
5690 --no-add-needed is used and the reference was not
5691 a weak one. */
5692 if (old_bfd != NULL
5693 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5694 {
5695 _bfd_error_handler
5696 /* xgettext:c-format */
5697 (_("%pB: undefined reference to symbol '%s'"),
5698 old_bfd, name);
5699 bfd_set_error (bfd_error_missing_dso);
5700 goto error_free_vers;
5701 }
e56f61be 5702
c0419c02
L
5703 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5704 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 5705
c0419c02
L
5706 /* Create dynamic sections for backends that require
5707 that be done before setup_gnu_properties. */
5708 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
8c0361c7 5709 goto error_free_vers;
c0419c02
L
5710 add_needed = true;
5711 }
5712 else if (dynamic
5713 && h->root.u.def.section->owner == abfd)
2707d55e
MM
5714 {
5715 /* Add this symbol to first hash if this shared
5716 object has the first definition. */
5717 elf_link_add_to_first_hash (abfd, info, name, must_copy_name);
5718 /* And if it was the default symbol version definition,
5719 also add the short name. */
5720 if (defvername)
5721 elf_link_add_to_first_hash (abfd, info, defvername, false);
5722 }
4ad4eba5
AM
5723 }
5724 }
5725 }
5726
a83ef4d1
L
5727 if (info->lto_plugin_active
5728 && !bfd_link_relocatable (info)
5729 && (abfd->flags & BFD_PLUGIN) == 0
5730 && !just_syms
ec8f5671
AM
5731 && extsymcount != 0
5732 && is_elf_hash_table (&htab->root))
a83ef4d1
L
5733 {
5734 int r_sym_shift;
5735
5736 if (bed->s->arch_size == 32)
5737 r_sym_shift = 8;
5738 else
5739 r_sym_shift = 32;
5740
5741 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5742 referenced in regular objects so that linker plugin will get
5743 the correct symbol resolution. */
5744
5745 sym_hash = elf_sym_hashes (abfd);
5746 for (s = abfd->sections; s != NULL; s = s->next)
5747 {
5748 Elf_Internal_Rela *internal_relocs;
5749 Elf_Internal_Rela *rel, *relend;
5750
5751 /* Don't check relocations in excluded sections. */
5752 if ((s->flags & SEC_RELOC) == 0
5753 || s->reloc_count == 0
5754 || (s->flags & SEC_EXCLUDE) != 0
549bc65f 5755 || (s->flags & SEC_DEBUGGING) != 0)
a83ef4d1
L
5756 continue;
5757
d41b7648
L
5758 internal_relocs = _bfd_elf_link_info_read_relocs
5759 (abfd, info, s, NULL, NULL,
5760 _bfd_elf_link_keep_memory (info));
a83ef4d1
L
5761 if (internal_relocs == NULL)
5762 goto error_free_vers;
5763
5764 rel = internal_relocs;
5765 relend = rel + s->reloc_count;
5766 for ( ; rel < relend; rel++)
5767 {
5768 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5769 struct elf_link_hash_entry *h;
5770
5771 /* Skip local symbols. */
5772 if (r_symndx < extsymoff)
5773 continue;
5774
5775 h = sym_hash[r_symndx - extsymoff];
5776 if (h != NULL)
5777 h->root.non_ir_ref_regular = 1;
5778 }
5779
5780 if (elf_section_data (s)->relocs != internal_relocs)
5781 free (internal_relocs);
5782 }
5783 }
5784
c9594989
AM
5785 free (extversym);
5786 extversym = NULL;
5787 free (isymbuf);
5788 isymbuf = NULL;
66eb6687
AM
5789
5790 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5791 {
5792 unsigned int i;
5793
5794 /* Restore the symbol table. */
f45794cb
AM
5795 old_ent = (char *) old_tab + tabsize;
5796 memset (elf_sym_hashes (abfd), 0,
5797 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
5798 htab->root.table.table = old_table;
5799 htab->root.table.size = old_size;
5800 htab->root.table.count = old_count;
66eb6687 5801 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
5802 htab->root.undefs = old_undefs;
5803 htab->root.undefs_tail = old_undefs_tail;
e310298c
AM
5804 if (htab->dynstr != NULL)
5805 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5b677558
AM
5806 free (old_strtab);
5807 old_strtab = NULL;
66eb6687
AM
5808 for (i = 0; i < htab->root.table.size; i++)
5809 {
5810 struct bfd_hash_entry *p;
5811 struct elf_link_hash_entry *h;
4070765b 5812 unsigned int non_ir_ref_dynamic;
66eb6687
AM
5813
5814 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5815 {
4070765b 5816 /* Preserve non_ir_ref_dynamic so that this symbol
59fa66c5
L
5817 will be exported when the dynamic lib becomes needed
5818 in the second pass. */
7ba11550
AM
5819 h = (struct elf_link_hash_entry *) p;
5820 if (h->root.type == bfd_link_hash_warning)
5821 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4070765b 5822 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
7ba11550 5823
2de92251 5824 h = (struct elf_link_hash_entry *) p;
7ba11550
AM
5825 memcpy (h, old_ent, htab->root.table.entsize);
5826 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
5827 if (h->root.type == bfd_link_hash_warning)
5828 {
a4542f1b 5829 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7ba11550
AM
5830 memcpy (h, old_ent, htab->root.table.entsize);
5831 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251 5832 }
a4542f1b 5833 if (h->root.type == bfd_link_hash_common)
3e0882af 5834 {
7ba11550
AM
5835 memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5836 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
3e0882af 5837 }
4070765b 5838 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
66eb6687
AM
5839 }
5840 }
5841
5061a885
AM
5842 /* Make a special call to the linker "notice" function to
5843 tell it that symbols added for crefs may need to be removed. */
e5034e59 5844 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 5845 goto error_free_vers;
5061a885 5846
66eb6687
AM
5847 free (old_tab);
5848 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5849 alloc_mark);
c9594989 5850 free (nondeflt_vers);
0a1b45a2 5851 return true;
66eb6687 5852 }
2de92251 5853
c785932b
AM
5854 free (old_strtab);
5855 old_strtab = NULL;
66eb6687
AM
5856 if (old_tab != NULL)
5857 {
e5034e59 5858 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 5859 goto error_free_vers;
66eb6687
AM
5860 free (old_tab);
5861 old_tab = NULL;
5862 }
5863
c6e8a9a8
L
5864 /* Now that all the symbols from this input file are created, if
5865 not performing a relocatable link, handle .symver foo, foo@BAR
5866 such that any relocs against foo become foo@BAR. */
0e1862bb 5867 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5 5868 {
ef53be89 5869 size_t cnt, symidx;
4ad4eba5
AM
5870
5871 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5872 {
5873 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5874 char *shortname, *p;
986f0783 5875 size_t amt;
4ad4eba5
AM
5876
5877 p = strchr (h->root.root.string, ELF_VER_CHR);
5878 if (p == NULL
5879 || (h->root.type != bfd_link_hash_defined
5880 && h->root.type != bfd_link_hash_defweak))
5881 continue;
5882
5883 amt = p - h->root.root.string;
a50b1753 5884 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
5885 if (!shortname)
5886 goto error_free_vers;
4ad4eba5
AM
5887 memcpy (shortname, h->root.root.string, amt);
5888 shortname[amt] = '\0';
5889
5890 hi = (struct elf_link_hash_entry *)
66eb6687 5891 bfd_link_hash_lookup (&htab->root, shortname,
0a1b45a2 5892 false, false, false);
4ad4eba5
AM
5893 if (hi != NULL
5894 && hi->root.type == h->root.type
5895 && hi->root.u.def.value == h->root.u.def.value
5896 && hi->root.u.def.section == h->root.u.def.section)
5897 {
0a1b45a2 5898 (*bed->elf_backend_hide_symbol) (info, hi, true);
4ad4eba5
AM
5899 hi->root.type = bfd_link_hash_indirect;
5900 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 5901 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
5902 sym_hash = elf_sym_hashes (abfd);
5903 if (sym_hash)
5904 for (symidx = 0; symidx < extsymcount; ++symidx)
5905 if (sym_hash[symidx] == hi)
5906 {
5907 sym_hash[symidx] = h;
5908 break;
5909 }
5910 }
5911 free (shortname);
5912 }
4ad4eba5 5913 }
8c0361c7
AM
5914 free (nondeflt_vers);
5915 nondeflt_vers = NULL;
4ad4eba5 5916
60d67dc8 5917 /* Now set the alias field correctly for all the weak defined
4ad4eba5
AM
5918 symbols we found. The only way to do this is to search all the
5919 symbols. Since we only need the information for non functions in
5920 dynamic objects, that's the only time we actually put anything on
5921 the list WEAKS. We need this information so that if a regular
5922 object refers to a symbol defined weakly in a dynamic object, the
5923 real symbol in the dynamic object is also put in the dynamic
5924 symbols; we also must arrange for both symbols to point to the
5925 same memory location. We could handle the general case of symbol
5926 aliasing, but a general symbol alias can only be generated in
5927 assembler code, handling it correctly would be very time
5928 consuming, and other ELF linkers don't handle general aliasing
5929 either. */
5930 if (weaks != NULL)
5931 {
5932 struct elf_link_hash_entry **hpp;
5933 struct elf_link_hash_entry **hppend;
5934 struct elf_link_hash_entry **sorted_sym_hash;
5935 struct elf_link_hash_entry *h;
986f0783 5936 size_t sym_count, amt;
4ad4eba5
AM
5937
5938 /* Since we have to search the whole symbol list for each weak
5939 defined symbol, search time for N weak defined symbols will be
5940 O(N^2). Binary search will cut it down to O(NlogN). */
986f0783 5941 amt = extsymcount * sizeof (*sorted_sym_hash);
3a3f4bf7 5942 sorted_sym_hash = bfd_malloc (amt);
4ad4eba5
AM
5943 if (sorted_sym_hash == NULL)
5944 goto error_return;
5945 sym_hash = sorted_sym_hash;
5946 hpp = elf_sym_hashes (abfd);
5947 hppend = hpp + extsymcount;
5948 sym_count = 0;
5949 for (; hpp < hppend; hpp++)
5950 {
5951 h = *hpp;
5952 if (h != NULL
5953 && h->root.type == bfd_link_hash_defined
fcb93ecf 5954 && !bed->is_function_type (h->type))
4ad4eba5
AM
5955 {
5956 *sym_hash = h;
5957 sym_hash++;
5958 sym_count++;
5959 }
5960 }
5961
3a3f4bf7 5962 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
4ad4eba5
AM
5963 elf_sort_symbol);
5964
5965 while (weaks != NULL)
5966 {
5967 struct elf_link_hash_entry *hlook;
5968 asection *slook;
5969 bfd_vma vlook;
ed54588d 5970 size_t i, j, idx = 0;
4ad4eba5
AM
5971
5972 hlook = weaks;
60d67dc8
AM
5973 weaks = hlook->u.alias;
5974 hlook->u.alias = NULL;
4ad4eba5 5975
e3e53eed
AM
5976 if (hlook->root.type != bfd_link_hash_defined
5977 && hlook->root.type != bfd_link_hash_defweak)
5978 continue;
5979
4ad4eba5
AM
5980 slook = hlook->root.u.def.section;
5981 vlook = hlook->root.u.def.value;
5982
4ad4eba5
AM
5983 i = 0;
5984 j = sym_count;
14160578 5985 while (i != j)
4ad4eba5
AM
5986 {
5987 bfd_signed_vma vdiff;
5988 idx = (i + j) / 2;
14160578 5989 h = sorted_sym_hash[idx];
4ad4eba5
AM
5990 vdiff = vlook - h->root.u.def.value;
5991 if (vdiff < 0)
5992 j = idx;
5993 else if (vdiff > 0)
5994 i = idx + 1;
5995 else
5996 {
d3435ae8 5997 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
5998 if (sdiff < 0)
5999 j = idx;
6000 else if (sdiff > 0)
6001 i = idx + 1;
6002 else
14160578 6003 break;
4ad4eba5
AM
6004 }
6005 }
6006
6007 /* We didn't find a value/section match. */
14160578 6008 if (i == j)
4ad4eba5
AM
6009 continue;
6010
14160578
AM
6011 /* With multiple aliases, or when the weak symbol is already
6012 strongly defined, we have multiple matching symbols and
6013 the binary search above may land on any of them. Step
6014 one past the matching symbol(s). */
6015 while (++idx != j)
6016 {
6017 h = sorted_sym_hash[idx];
6018 if (h->root.u.def.section != slook
6019 || h->root.u.def.value != vlook)
6020 break;
6021 }
6022
6023 /* Now look back over the aliases. Since we sorted by size
6024 as well as value and section, we'll choose the one with
6025 the largest size. */
6026 while (idx-- != i)
4ad4eba5 6027 {
14160578 6028 h = sorted_sym_hash[idx];
4ad4eba5
AM
6029
6030 /* Stop if value or section doesn't match. */
14160578
AM
6031 if (h->root.u.def.section != slook
6032 || h->root.u.def.value != vlook)
4ad4eba5
AM
6033 break;
6034 else if (h != hlook)
6035 {
60d67dc8
AM
6036 struct elf_link_hash_entry *t;
6037
6038 hlook->u.alias = h;
6039 hlook->is_weakalias = 1;
6040 t = h;
6041 if (t->u.alias != NULL)
6042 while (t->u.alias != h)
6043 t = t->u.alias;
6044 t->u.alias = hlook;
4ad4eba5
AM
6045
6046 /* If the weak definition is in the list of dynamic
6047 symbols, make sure the real definition is put
6048 there as well. */
6049 if (hlook->dynindx != -1 && h->dynindx == -1)
6050 {
c152c796 6051 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
6052 {
6053 err_free_sym_hash:
6054 free (sorted_sym_hash);
6055 goto error_return;
6056 }
4ad4eba5
AM
6057 }
6058
6059 /* If the real definition is in the list of dynamic
6060 symbols, make sure the weak definition is put
6061 there as well. If we don't do this, then the
6062 dynamic loader might not merge the entries for the
6063 real definition and the weak definition. */
6064 if (h->dynindx != -1 && hlook->dynindx == -1)
6065 {
c152c796 6066 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 6067 goto err_free_sym_hash;
4ad4eba5
AM
6068 }
6069 break;
6070 }
6071 }
6072 }
6073
6074 free (sorted_sym_hash);
6075 }
6076
33177bb1
AM
6077 if (bed->check_directives
6078 && !(*bed->check_directives) (abfd, info))
c785932b 6079 goto error_return;
85fbca6a 6080
4ad4eba5
AM
6081 /* If this is a non-traditional link, try to optimize the handling
6082 of the .stab/.stabstr sections. */
6083 if (! dynamic
6084 && ! info->traditional_format
2cc15b10 6085 && is_elf_hash_table (&htab->root)
4ad4eba5
AM
6086 && (info->strip != strip_all && info->strip != strip_debugger))
6087 {
6088 asection *stabstr;
6089
6090 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
6091 if (stabstr != NULL)
6092 {
6093 bfd_size_type string_offset = 0;
6094 asection *stab;
6095
6096 for (stab = abfd->sections; stab; stab = stab->next)
08dedd66 6097 if (startswith (stab->name, ".stab")
4ad4eba5
AM
6098 && (!stab->name[5] ||
6099 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
6100 && (stab->flags & SEC_MERGE) == 0
6101 && !bfd_is_abs_section (stab->output_section))
6102 {
6103 struct bfd_elf_section_data *secdata;
6104
6105 secdata = elf_section_data (stab);
66eb6687
AM
6106 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
6107 stabstr, &secdata->sec_info,
4ad4eba5
AM
6108 &string_offset))
6109 goto error_return;
6110 if (secdata->sec_info)
dbaa2011 6111 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
6112 }
6113 }
6114 }
6115
e310298c 6116 if (dynamic && add_needed)
4ad4eba5
AM
6117 {
6118 /* Add this bfd to the loaded list. */
6119 struct elf_link_loaded_list *n;
6120
ca4be51c 6121 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
6122 if (n == NULL)
6123 goto error_return;
6124 n->abfd = abfd;
e310298c
AM
6125 n->next = htab->dyn_loaded;
6126 htab->dyn_loaded = n;
4ad4eba5 6127 }
e310298c
AM
6128 if (dynamic && !add_needed
6129 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
6130 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
4ad4eba5 6131
0a1b45a2 6132 return true;
4ad4eba5
AM
6133
6134 error_free_vers:
c9594989
AM
6135 free (old_tab);
6136 free (old_strtab);
6137 free (nondeflt_vers);
6138 free (extversym);
4ad4eba5 6139 error_free_sym:
c9594989 6140 free (isymbuf);
4ad4eba5 6141 error_return:
0a1b45a2 6142 return false;
4ad4eba5
AM
6143}
6144
8387904d
AM
6145/* Return the linker hash table entry of a symbol that might be
6146 satisfied by an archive symbol. Return -1 on error. */
6147
b585e899 6148struct bfd_link_hash_entry *
8387904d
AM
6149_bfd_elf_archive_symbol_lookup (bfd *abfd,
6150 struct bfd_link_info *info,
6151 const char *name)
6152{
b585e899 6153 struct bfd_link_hash_entry *h;
8387904d
AM
6154 char *p, *copy;
6155 size_t len, first;
6156
b585e899 6157 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
8387904d
AM
6158 if (h != NULL)
6159 return h;
6160
6161 /* If this is a default version (the name contains @@), look up the
6162 symbol again with only one `@' as well as without the version.
6163 The effect is that references to the symbol with and without the
6164 version will be matched by the default symbol in the archive. */
6165
6166 p = strchr (name, ELF_VER_CHR);
6167 if (p == NULL || p[1] != ELF_VER_CHR)
c0419c02
L
6168 {
6169 /* Add this symbol to first hash if this archive has the first
6170 definition. */
de9dc65b
AM
6171 if (is_elf_hash_table (info->hash))
6172 elf_link_add_to_first_hash (abfd, info, name, false);
c0419c02
L
6173 return h;
6174 }
8387904d
AM
6175
6176 /* First check with only one `@'. */
6177 len = strlen (name);
a50b1753 6178 copy = (char *) bfd_alloc (abfd, len);
8387904d 6179 if (copy == NULL)
b585e899 6180 return (struct bfd_link_hash_entry *) -1;
8387904d
AM
6181
6182 first = p - name + 1;
6183 memcpy (copy, name, first);
6184 memcpy (copy + first, name + first + 1, len - first);
6185
b585e899 6186 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
8387904d
AM
6187 if (h == NULL)
6188 {
6189 /* We also need to check references to the symbol without the
6190 version. */
6191 copy[first - 1] = '\0';
b585e899 6192 h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
8387904d
AM
6193 }
6194
6195 bfd_release (abfd, copy);
6196 return h;
6197}
6198
0ad989f9 6199/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
6200 don't use _bfd_generic_link_add_archive_symbols because we need to
6201 handle versioned symbols.
0ad989f9
L
6202
6203 Fortunately, ELF archive handling is simpler than that done by
6204 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
6205 oddities. In ELF, if we find a symbol in the archive map, and the
6206 symbol is currently undefined, we know that we must pull in that
6207 object file.
6208
6209 Unfortunately, we do have to make multiple passes over the symbol
6210 table until nothing further is resolved. */
6211
0a1b45a2 6212static bool
4ad4eba5 6213elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
6214{
6215 symindex c;
13e570f8 6216 unsigned char *included = NULL;
0ad989f9 6217 carsym *symdefs;
0a1b45a2 6218 bool loop;
986f0783 6219 size_t amt;
8387904d 6220 const struct elf_backend_data *bed;
b585e899 6221 struct bfd_link_hash_entry * (*archive_symbol_lookup)
8387904d 6222 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
6223
6224 if (! bfd_has_map (abfd))
6225 {
6226 /* An empty archive is a special case. */
6227 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
0a1b45a2 6228 return true;
0ad989f9 6229 bfd_set_error (bfd_error_no_armap);
0a1b45a2 6230 return false;
0ad989f9
L
6231 }
6232
6233 /* Keep track of all symbols we know to be already defined, and all
6234 files we know to be already included. This is to speed up the
6235 second and subsequent passes. */
6236 c = bfd_ardata (abfd)->symdef_count;
6237 if (c == 0)
0a1b45a2 6238 return true;
986f0783 6239 amt = c * sizeof (*included);
13e570f8
AM
6240 included = (unsigned char *) bfd_zmalloc (amt);
6241 if (included == NULL)
0a1b45a2 6242 return false;
0ad989f9
L
6243
6244 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
6245 bed = get_elf_backend_data (abfd);
6246 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
6247
6248 do
6249 {
6250 file_ptr last;
6251 symindex i;
6252 carsym *symdef;
6253 carsym *symdefend;
6254
0a1b45a2 6255 loop = false;
0ad989f9
L
6256 last = -1;
6257
6258 symdef = symdefs;
6259 symdefend = symdef + c;
6260 for (i = 0; symdef < symdefend; symdef++, i++)
6261 {
b585e899 6262 struct bfd_link_hash_entry *h;
0ad989f9
L
6263 bfd *element;
6264 struct bfd_link_hash_entry *undefs_tail;
6265 symindex mark;
6266
13e570f8 6267 if (included[i])
0ad989f9
L
6268 continue;
6269 if (symdef->file_offset == last)
6270 {
0a1b45a2 6271 included[i] = true;
0ad989f9
L
6272 continue;
6273 }
6274
8387904d 6275 h = archive_symbol_lookup (abfd, info, symdef->name);
b585e899 6276 if (h == (struct bfd_link_hash_entry *) -1)
8387904d 6277 goto error_return;
0ad989f9
L
6278
6279 if (h == NULL)
6280 continue;
6281
b585e899 6282 if (h->type == bfd_link_hash_undefined)
75cfe082 6283 {
2707d55e
MM
6284 if (is_elf_hash_table (info->hash))
6285 {
6286 /* If the archive element has already been loaded then one
6287 of the symbols defined by that element might have been
6288 made undefined due to being in a discarded section. */
6289 if (((struct elf_link_hash_entry *) h)->indx == -3)
6290 continue;
6291
6292 /* In the pre-LTO-plugin pass we must not mistakenly
2cec9142
L
6293 include this archive member if an earlier shared
6294 library defined this symbol. */
2707d55e
MM
6295 struct elf_link_hash_table *htab = elf_hash_table (info);
6296 if (htab->first_hash)
6297 {
6298 struct elf_link_first_hash_entry *e
6299 = ((struct elf_link_first_hash_entry *)
6300 bfd_hash_lookup (htab->first_hash, symdef->name,
6301 false, false));
2cec9142
L
6302 if (e
6303 && (e->abfd->flags & DYNAMIC) != 0
6304 && e->abfd != abfd)
2707d55e
MM
6305 continue;
6306 }
6307 }
75cfe082 6308 }
b585e899 6309 else if (h->type == bfd_link_hash_common)
0ad989f9
L
6310 {
6311 /* We currently have a common symbol. The archive map contains
6312 a reference to this symbol, so we may want to include it. We
6313 only want to include it however, if this archive element
6314 contains a definition of the symbol, not just another common
6315 declaration of it.
6316
6317 Unfortunately some archivers (including GNU ar) will put
6318 declarations of common symbols into their archive maps, as
6319 well as real definitions, so we cannot just go by the archive
6320 map alone. Instead we must read in the element's symbol
6321 table and check that to see what kind of symbol definition
6322 this is. */
6323 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
6324 continue;
6325 }
75cfe082 6326 else
0ad989f9 6327 {
b585e899 6328 if (h->type != bfd_link_hash_undefweak)
13e570f8 6329 /* Symbol must be defined. Don't check it again. */
0a1b45a2 6330 included[i] = true;
c0419c02 6331
de9dc65b
AM
6332 if (!is_elf_hash_table (info->hash))
6333 continue;
c2fbf578
L
6334 struct elf_link_hash_entry *eh
6335 = (struct elf_link_hash_entry *) h;
6336 /* Ignore the archive if the symbol isn't referenced by a
6337 regular object or isn't defined in a shared object. */
6338 if (!eh->ref_regular || !eh->def_dynamic)
c0419c02
L
6339 continue;
6340 /* Ignore the dynamic definition if symbol is first
6341 defined in this archive. */
6342 struct elf_link_hash_table *htab = elf_hash_table (info);
6343 if (htab->first_hash == NULL)
6344 continue;
816fd3dc
L
6345 struct elf_link_first_hash_entry *e
6346 = ((struct elf_link_first_hash_entry *)
6347 bfd_hash_lookup (htab->first_hash, symdef->name,
6348 false, false));
6349 if (e == NULL || e->abfd != abfd)
c0419c02 6350 continue;
0ad989f9
L
6351 }
6352
6353 /* We need to include this archive member. */
6395a102
L
6354 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset,
6355 info);
0ad989f9
L
6356 if (element == NULL)
6357 goto error_return;
6358
6359 if (! bfd_check_format (element, bfd_object))
6360 goto error_return;
6361
0ad989f9
L
6362 undefs_tail = info->hash->undefs_tail;
6363
0e144ba7
AM
6364 if (!(*info->callbacks
6365 ->add_archive_element) (info, element, symdef->name, &element))
b95a0a31 6366 continue;
0e144ba7 6367 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
6368 goto error_return;
6369
6370 /* If there are any new undefined symbols, we need to make
6371 another pass through the archive in order to see whether
6372 they can be defined. FIXME: This isn't perfect, because
6373 common symbols wind up on undefs_tail and because an
6374 undefined symbol which is defined later on in this pass
6375 does not require another pass. This isn't a bug, but it
6376 does make the code less efficient than it could be. */
6377 if (undefs_tail != info->hash->undefs_tail)
0a1b45a2 6378 loop = true;
0ad989f9
L
6379
6380 /* Look backward to mark all symbols from this object file
6381 which we have already seen in this pass. */
6382 mark = i;
6383 do
6384 {
0a1b45a2 6385 included[mark] = true;
0ad989f9
L
6386 if (mark == 0)
6387 break;
6388 --mark;
6389 }
6390 while (symdefs[mark].file_offset == symdef->file_offset);
6391
6392 /* We mark subsequent symbols from this object file as we go
6393 on through the loop. */
6394 last = symdef->file_offset;
6395 }
6396 }
6397 while (loop);
6398
0ad989f9 6399 free (included);
0a1b45a2 6400 return true;
0ad989f9
L
6401
6402 error_return:
c9594989 6403 free (included);
0a1b45a2 6404 return false;
0ad989f9 6405}
4ad4eba5
AM
6406
6407/* Given an ELF BFD, add symbols to the global hash table as
6408 appropriate. */
6409
0a1b45a2 6410bool
4ad4eba5
AM
6411bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
6412{
6413 switch (bfd_get_format (abfd))
6414 {
6415 case bfd_object:
6416 return elf_link_add_object_symbols (abfd, info);
6417 case bfd_archive:
6418 return elf_link_add_archive_symbols (abfd, info);
6419 default:
6420 bfd_set_error (bfd_error_wrong_format);
0a1b45a2 6421 return false;
4ad4eba5
AM
6422 }
6423}
5a580b3a 6424\f
14b1c01e
AM
6425struct hash_codes_info
6426{
6427 unsigned long *hashcodes;
0a1b45a2 6428 bool error;
14b1c01e 6429};
a0c8462f 6430
5a580b3a
AM
6431/* This function will be called though elf_link_hash_traverse to store
6432 all hash value of the exported symbols in an array. */
6433
0a1b45a2 6434static bool
5a580b3a
AM
6435elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
6436{
a50b1753 6437 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 6438 const char *name;
5a580b3a
AM
6439 unsigned long ha;
6440 char *alc = NULL;
6441
5a580b3a
AM
6442 /* Ignore indirect symbols. These are added by the versioning code. */
6443 if (h->dynindx == -1)
0a1b45a2 6444 return true;
5a580b3a
AM
6445
6446 name = h->root.root.string;
422f1182 6447 if (h->versioned >= versioned)
5a580b3a 6448 {
422f1182
L
6449 char *p = strchr (name, ELF_VER_CHR);
6450 if (p != NULL)
14b1c01e 6451 {
422f1182
L
6452 alc = (char *) bfd_malloc (p - name + 1);
6453 if (alc == NULL)
6454 {
0a1b45a2
AM
6455 inf->error = true;
6456 return false;
422f1182
L
6457 }
6458 memcpy (alc, name, p - name);
6459 alc[p - name] = '\0';
6460 name = alc;
14b1c01e 6461 }
5a580b3a
AM
6462 }
6463
6464 /* Compute the hash value. */
6465 ha = bfd_elf_hash (name);
6466
6467 /* Store the found hash value in the array given as the argument. */
14b1c01e 6468 *(inf->hashcodes)++ = ha;
5a580b3a
AM
6469
6470 /* And store it in the struct so that we can put it in the hash table
6471 later. */
f6e332e6 6472 h->u.elf_hash_value = ha;
5a580b3a 6473
c9594989 6474 free (alc);
0a1b45a2 6475 return true;
5a580b3a
AM
6476}
6477
fdc90cb4
JJ
6478struct collect_gnu_hash_codes
6479{
6480 bfd *output_bfd;
6481 const struct elf_backend_data *bed;
6482 unsigned long int nsyms;
6483 unsigned long int maskbits;
6484 unsigned long int *hashcodes;
6485 unsigned long int *hashval;
6486 unsigned long int *indx;
6487 unsigned long int *counts;
6488 bfd_vma *bitmask;
6489 bfd_byte *contents;
f16a9783 6490 bfd_size_type xlat;
fdc90cb4
JJ
6491 long int min_dynindx;
6492 unsigned long int bucketcount;
6493 unsigned long int symindx;
6494 long int local_indx;
6495 long int shift1, shift2;
6496 unsigned long int mask;
0a1b45a2 6497 bool error;
fdc90cb4
JJ
6498};
6499
6500/* This function will be called though elf_link_hash_traverse to store
6501 all hash value of the exported symbols in an array. */
6502
0a1b45a2 6503static bool
fdc90cb4
JJ
6504elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6505{
a50b1753 6506 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 6507 const char *name;
fdc90cb4
JJ
6508 unsigned long ha;
6509 char *alc = NULL;
6510
fdc90cb4
JJ
6511 /* Ignore indirect symbols. These are added by the versioning code. */
6512 if (h->dynindx == -1)
0a1b45a2 6513 return true;
fdc90cb4
JJ
6514
6515 /* Ignore also local symbols and undefined symbols. */
6516 if (! (*s->bed->elf_hash_symbol) (h))
0a1b45a2 6517 return true;
fdc90cb4
JJ
6518
6519 name = h->root.root.string;
422f1182 6520 if (h->versioned >= versioned)
fdc90cb4 6521 {
422f1182
L
6522 char *p = strchr (name, ELF_VER_CHR);
6523 if (p != NULL)
14b1c01e 6524 {
422f1182
L
6525 alc = (char *) bfd_malloc (p - name + 1);
6526 if (alc == NULL)
6527 {
0a1b45a2
AM
6528 s->error = true;
6529 return false;
422f1182
L
6530 }
6531 memcpy (alc, name, p - name);
6532 alc[p - name] = '\0';
6533 name = alc;
14b1c01e 6534 }
fdc90cb4
JJ
6535 }
6536
6537 /* Compute the hash value. */
6538 ha = bfd_elf_gnu_hash (name);
6539
6540 /* Store the found hash value in the array for compute_bucket_count,
6541 and also for .dynsym reordering purposes. */
6542 s->hashcodes[s->nsyms] = ha;
6543 s->hashval[h->dynindx] = ha;
6544 ++s->nsyms;
6545 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6546 s->min_dynindx = h->dynindx;
6547
c9594989 6548 free (alc);
0a1b45a2 6549 return true;
fdc90cb4
JJ
6550}
6551
6552/* This function will be called though elf_link_hash_traverse to do
f16a9783
MS
6553 final dynamic symbol renumbering in case of .gnu.hash.
6554 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6555 to the translation table. */
fdc90cb4 6556
0a1b45a2 6557static bool
f16a9783 6558elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
fdc90cb4 6559{
a50b1753 6560 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
6561 unsigned long int bucket;
6562 unsigned long int val;
6563
fdc90cb4
JJ
6564 /* Ignore indirect symbols. */
6565 if (h->dynindx == -1)
0a1b45a2 6566 return true;
fdc90cb4
JJ
6567
6568 /* Ignore also local symbols and undefined symbols. */
6569 if (! (*s->bed->elf_hash_symbol) (h))
6570 {
6571 if (h->dynindx >= s->min_dynindx)
f16a9783
MS
6572 {
6573 if (s->bed->record_xhash_symbol != NULL)
6574 {
6575 (*s->bed->record_xhash_symbol) (h, 0);
6576 s->local_indx++;
6577 }
6578 else
6579 h->dynindx = s->local_indx++;
6580 }
0a1b45a2 6581 return true;
fdc90cb4
JJ
6582 }
6583
6584 bucket = s->hashval[h->dynindx] % s->bucketcount;
6585 val = (s->hashval[h->dynindx] >> s->shift1)
6586 & ((s->maskbits >> s->shift1) - 1);
6587 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6588 s->bitmask[val]
6589 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6590 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6591 if (s->counts[bucket] == 1)
6592 /* Last element terminates the chain. */
6593 val |= 1;
6594 bfd_put_32 (s->output_bfd, val,
6595 s->contents + (s->indx[bucket] - s->symindx) * 4);
6596 --s->counts[bucket];
f16a9783
MS
6597 if (s->bed->record_xhash_symbol != NULL)
6598 {
6599 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6600
6601 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6602 }
6603 else
6604 h->dynindx = s->indx[bucket]++;
0a1b45a2 6605 return true;
fdc90cb4
JJ
6606}
6607
6608/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6609
0a1b45a2 6610bool
fdc90cb4
JJ
6611_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6612{
6613 return !(h->forced_local
6614 || h->root.type == bfd_link_hash_undefined
6615 || h->root.type == bfd_link_hash_undefweak
6616 || ((h->root.type == bfd_link_hash_defined
6617 || h->root.type == bfd_link_hash_defweak)
6618 && h->root.u.def.section->output_section == NULL));
6619}
6620
5a580b3a
AM
6621/* Array used to determine the number of hash table buckets to use
6622 based on the number of symbols there are. If there are fewer than
6623 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6624 fewer than 37 we use 17 buckets, and so forth. We never use more
6625 than 32771 buckets. */
6626
6627static const size_t elf_buckets[] =
6628{
6629 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6630 16411, 32771, 0
6631};
6632
6633/* Compute bucket count for hashing table. We do not use a static set
6634 of possible tables sizes anymore. Instead we determine for all
6635 possible reasonable sizes of the table the outcome (i.e., the
6636 number of collisions etc) and choose the best solution. The
6637 weighting functions are not too simple to allow the table to grow
6638 without bounds. Instead one of the weighting factors is the size.
6639 Therefore the result is always a good payoff between few collisions
6640 (= short chain lengths) and table size. */
6641static size_t
b20dd2ce 6642compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
6643 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6644 unsigned long int nsyms,
6645 int gnu_hash)
5a580b3a 6646{
5a580b3a 6647 size_t best_size = 0;
5a580b3a 6648 unsigned long int i;
5a580b3a 6649
5a580b3a
AM
6650 if (info->optimize)
6651 {
5a580b3a
AM
6652 size_t minsize;
6653 size_t maxsize;
0e3c1eeb 6654 uint64_t best_chlen = ~((uint64_t) 0);
5a580b3a 6655 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 6656 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 6657 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 6658 unsigned long int *counts;
d40f3da9 6659 bfd_size_type amt;
0883b6e0 6660 unsigned int no_improvement_count = 0;
5a580b3a
AM
6661
6662 /* Possible optimization parameters: if we have NSYMS symbols we say
6663 that the hashing table must at least have NSYMS/4 and at most
6664 2*NSYMS buckets. */
6665 minsize = nsyms / 4;
6666 if (minsize == 0)
6667 minsize = 1;
6668 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
6669 if (gnu_hash)
6670 {
6671 if (minsize < 2)
6672 minsize = 2;
6673 if ((best_size & 31) == 0)
6674 ++best_size;
6675 }
5a580b3a
AM
6676
6677 /* Create array where we count the collisions in. We must use bfd_malloc
6678 since the size could be large. */
6679 amt = maxsize;
6680 amt *= sizeof (unsigned long int);
a50b1753 6681 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 6682 if (counts == NULL)
fdc90cb4 6683 return 0;
5a580b3a
AM
6684
6685 /* Compute the "optimal" size for the hash table. The criteria is a
6686 minimal chain length. The minor criteria is (of course) the size
6687 of the table. */
6688 for (i = minsize; i < maxsize; ++i)
6689 {
6690 /* Walk through the array of hashcodes and count the collisions. */
0e3c1eeb 6691 uint64_t max;
5a580b3a
AM
6692 unsigned long int j;
6693 unsigned long int fact;
6694
fdc90cb4
JJ
6695 if (gnu_hash && (i & 31) == 0)
6696 continue;
6697
5a580b3a
AM
6698 memset (counts, '\0', i * sizeof (unsigned long int));
6699
6700 /* Determine how often each hash bucket is used. */
6701 for (j = 0; j < nsyms; ++j)
6702 ++counts[hashcodes[j] % i];
6703
6704 /* For the weight function we need some information about the
6705 pagesize on the target. This is information need not be 100%
6706 accurate. Since this information is not available (so far) we
6707 define it here to a reasonable default value. If it is crucial
6708 to have a better value some day simply define this value. */
6709# ifndef BFD_TARGET_PAGESIZE
6710# define BFD_TARGET_PAGESIZE (4096)
6711# endif
6712
fdc90cb4
JJ
6713 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6714 and the chains. */
6715 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
6716
6717# if 1
6718 /* Variant 1: optimize for short chains. We add the squares
6719 of all the chain lengths (which favors many small chain
6720 over a few long chains). */
6721 for (j = 0; j < i; ++j)
6722 max += counts[j] * counts[j];
6723
6724 /* This adds penalties for the overall size of the table. */
fdc90cb4 6725 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6726 max *= fact * fact;
6727# else
6728 /* Variant 2: Optimize a lot more for small table. Here we
6729 also add squares of the size but we also add penalties for
6730 empty slots (the +1 term). */
6731 for (j = 0; j < i; ++j)
6732 max += (1 + counts[j]) * (1 + counts[j]);
6733
6734 /* The overall size of the table is considered, but not as
6735 strong as in variant 1, where it is squared. */
fdc90cb4 6736 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6737 max *= fact;
6738# endif
6739
6740 /* Compare with current best results. */
6741 if (max < best_chlen)
6742 {
6743 best_chlen = max;
6744 best_size = i;
ca4be51c 6745 no_improvement_count = 0;
5a580b3a 6746 }
0883b6e0
NC
6747 /* PR 11843: Avoid futile long searches for the best bucket size
6748 when there are a large number of symbols. */
6749 else if (++no_improvement_count == 100)
6750 break;
5a580b3a
AM
6751 }
6752
6753 free (counts);
6754 }
6755 else
5a580b3a 6756 {
5a580b3a
AM
6757 for (i = 0; elf_buckets[i] != 0; i++)
6758 {
6759 best_size = elf_buckets[i];
fdc90cb4 6760 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
6761 break;
6762 }
fdc90cb4
JJ
6763 if (gnu_hash && best_size < 2)
6764 best_size = 2;
5a580b3a
AM
6765 }
6766
5a580b3a
AM
6767 return best_size;
6768}
6769
d0bf826b
AM
6770/* Size any SHT_GROUP section for ld -r. */
6771
0a1b45a2 6772bool
d0bf826b
AM
6773_bfd_elf_size_group_sections (struct bfd_link_info *info)
6774{
6775 bfd *ibfd;
57963c05 6776 asection *s;
d0bf826b 6777
c72f2fb2 6778 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b 6779 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
57963c05
AM
6780 && (s = ibfd->sections) != NULL
6781 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
d0bf826b 6782 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
0a1b45a2
AM
6783 return false;
6784 return true;
d0bf826b
AM
6785}
6786
04c3a755
NS
6787/* Set a default stack segment size. The value in INFO wins. If it
6788 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6789 undefined it is initialized. */
6790
0a1b45a2 6791bool
04c3a755
NS
6792bfd_elf_stack_segment_size (bfd *output_bfd,
6793 struct bfd_link_info *info,
6794 const char *legacy_symbol,
6795 bfd_vma default_size)
6796{
6797 struct elf_link_hash_entry *h = NULL;
6798
6799 /* Look for legacy symbol. */
6800 if (legacy_symbol)
6801 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
0a1b45a2 6802 false, false, false);
04c3a755
NS
6803 if (h && (h->root.type == bfd_link_hash_defined
6804 || h->root.type == bfd_link_hash_defweak)
6805 && h->def_regular
6806 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6807 {
6808 /* The symbol has no type if specified on the command line. */
6809 h->type = STT_OBJECT;
6810 if (info->stacksize)
695344c0 6811 /* xgettext:c-format */
871b3ab2 6812 _bfd_error_handler (_("%pB: stack size specified and %s set"),
4eca0228 6813 output_bfd, legacy_symbol);
04c3a755 6814 else if (h->root.u.def.section != bfd_abs_section_ptr)
695344c0 6815 /* xgettext:c-format */
871b3ab2 6816 _bfd_error_handler (_("%pB: %s not absolute"),
4eca0228 6817 output_bfd, legacy_symbol);
04c3a755
NS
6818 else
6819 info->stacksize = h->root.u.def.value;
6820 }
6821
6822 if (!info->stacksize)
6823 /* If the user didn't set a size, or explicitly inhibit the
6824 size, set it now. */
6825 info->stacksize = default_size;
6826
6827 /* Provide the legacy symbol, if it is referenced. */
6828 if (h && (h->root.type == bfd_link_hash_undefined
6829 || h->root.type == bfd_link_hash_undefweak))
6830 {
6831 struct bfd_link_hash_entry *bh = NULL;
6832
6833 if (!(_bfd_generic_link_add_one_symbol
6834 (info, output_bfd, legacy_symbol,
6835 BSF_GLOBAL, bfd_abs_section_ptr,
6836 info->stacksize >= 0 ? info->stacksize : 0,
0a1b45a2
AM
6837 NULL, false, get_elf_backend_data (output_bfd)->collect, &bh)))
6838 return false;
04c3a755
NS
6839
6840 h = (struct elf_link_hash_entry *) bh;
6841 h->def_regular = 1;
6842 h->type = STT_OBJECT;
6843 }
6844
0a1b45a2 6845 return true;
04c3a755
NS
6846}
6847
b531344c
MR
6848/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6849
6850struct elf_gc_sweep_symbol_info
6851{
6852 struct bfd_link_info *info;
6853 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
0a1b45a2 6854 bool);
b531344c
MR
6855};
6856
0a1b45a2 6857static bool
b531344c
MR
6858elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6859{
6860 if (!h->mark
6861 && (((h->root.type == bfd_link_hash_defined
6862 || h->root.type == bfd_link_hash_defweak)
6863 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6864 && h->root.u.def.section->gc_mark))
6865 || h->root.type == bfd_link_hash_undefined
6866 || h->root.type == bfd_link_hash_undefweak))
6867 {
6868 struct elf_gc_sweep_symbol_info *inf;
6869
6870 inf = (struct elf_gc_sweep_symbol_info *) data;
0a1b45a2 6871 (*inf->hide_symbol) (inf->info, h, true);
b531344c
MR
6872 h->def_regular = 0;
6873 h->ref_regular = 0;
6874 h->ref_regular_nonweak = 0;
6875 }
6876
0a1b45a2 6877 return true;
b531344c
MR
6878}
6879
5a580b3a
AM
6880/* Set up the sizes and contents of the ELF dynamic sections. This is
6881 called by the ELF linker emulation before_allocation routine. We
6882 must set the sizes of the sections before the linker sets the
6883 addresses of the various sections. */
6884
0a1b45a2 6885bool
5a580b3a
AM
6886bfd_elf_size_dynamic_sections (bfd *output_bfd,
6887 const char *soname,
6888 const char *rpath,
6889 const char *filter_shlib,
7ee314fa
AM
6890 const char *audit,
6891 const char *depaudit,
5a580b3a
AM
6892 const char * const *auxiliary_filters,
6893 struct bfd_link_info *info,
fd91d419 6894 asection **sinterpptr)
5a580b3a 6895{
5a580b3a
AM
6896 bfd *dynobj;
6897 const struct elf_backend_data *bed;
5a580b3a
AM
6898
6899 *sinterpptr = NULL;
6900
5a580b3a 6901 if (!is_elf_hash_table (info->hash))
0a1b45a2 6902 return true;
5a580b3a 6903
6540edd5
AM
6904 /* Any syms created from now on start with -1 in
6905 got.refcount/offset and plt.refcount/offset. */
6906 elf_hash_table (info)->init_got_refcount
6907 = elf_hash_table (info)->init_got_offset;
6908 elf_hash_table (info)->init_plt_refcount
6909 = elf_hash_table (info)->init_plt_offset;
6910
6911 bed = get_elf_backend_data (output_bfd);
6912
6913 /* The backend may have to create some sections regardless of whether
6914 we're dynamic or not. */
af969b14
AM
6915 if (bed->elf_backend_early_size_sections
6916 && !bed->elf_backend_early_size_sections (output_bfd, info))
6540edd5
AM
6917 return false;
6918
5a580b3a
AM
6919 dynobj = elf_hash_table (info)->dynobj;
6920
9a2a56cc 6921 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 6922 {
902e9fc7
MR
6923 struct bfd_elf_version_tree *verdefs;
6924 struct elf_info_failed asvinfo;
5a580b3a
AM
6925 struct bfd_elf_version_tree *t;
6926 struct bfd_elf_version_expr *d;
902e9fc7 6927 asection *s;
e6699019 6928 size_t soname_indx;
7ee314fa 6929
5a580b3a
AM
6930 /* If we are supposed to export all symbols into the dynamic symbol
6931 table (this is not the normal case), then do so. */
55255dae 6932 if (info->export_dynamic
0e1862bb 6933 || (bfd_link_executable (info) && info->dynamic))
5a580b3a 6934 {
3d13f3e9
AM
6935 struct elf_info_failed eif;
6936
6937 eif.info = info;
0a1b45a2 6938 eif.failed = false;
5a580b3a
AM
6939 elf_link_hash_traverse (elf_hash_table (info),
6940 _bfd_elf_export_symbol,
6941 &eif);
6942 if (eif.failed)
0a1b45a2 6943 return false;
5a580b3a
AM
6944 }
6945
e6699019
L
6946 if (soname != NULL)
6947 {
6948 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
0a1b45a2 6949 soname, true);
e6699019
L
6950 if (soname_indx == (size_t) -1
6951 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
0a1b45a2 6952 return false;
e6699019
L
6953 }
6954 else
6955 soname_indx = (size_t) -1;
6956
5a580b3a 6957 /* Make all global versions with definition. */
fd91d419 6958 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6959 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6960 if (!d->symver && d->literal)
5a580b3a
AM
6961 {
6962 const char *verstr, *name;
6963 size_t namelen, verlen, newlen;
93252b1c 6964 char *newname, *p, leading_char;
5a580b3a
AM
6965 struct elf_link_hash_entry *newh;
6966
93252b1c 6967 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 6968 name = d->pattern;
93252b1c 6969 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
6970 verstr = t->name;
6971 verlen = strlen (verstr);
6972 newlen = namelen + verlen + 3;
6973
a50b1753 6974 newname = (char *) bfd_malloc (newlen);
5a580b3a 6975 if (newname == NULL)
0a1b45a2 6976 return false;
93252b1c
MF
6977 newname[0] = leading_char;
6978 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
6979
6980 /* Check the hidden versioned definition. */
6981 p = newname + namelen;
6982 *p++ = ELF_VER_CHR;
6983 memcpy (p, verstr, verlen + 1);
6984 newh = elf_link_hash_lookup (elf_hash_table (info),
0a1b45a2
AM
6985 newname, false, false,
6986 false);
5a580b3a
AM
6987 if (newh == NULL
6988 || (newh->root.type != bfd_link_hash_defined
6989 && newh->root.type != bfd_link_hash_defweak))
6990 {
6991 /* Check the default versioned definition. */
6992 *p++ = ELF_VER_CHR;
6993 memcpy (p, verstr, verlen + 1);
6994 newh = elf_link_hash_lookup (elf_hash_table (info),
0a1b45a2
AM
6995 newname, false, false,
6996 false);
5a580b3a
AM
6997 }
6998 free (newname);
6999
7000 /* Mark this version if there is a definition and it is
7001 not defined in a shared object. */
7002 if (newh != NULL
f5385ebf 7003 && !newh->def_dynamic
5a580b3a
AM
7004 && (newh->root.type == bfd_link_hash_defined
7005 || newh->root.type == bfd_link_hash_defweak))
7006 d->symver = 1;
7007 }
7008
7009 /* Attach all the symbols to their version information. */
5a580b3a 7010 asvinfo.info = info;
0a1b45a2 7011 asvinfo.failed = false;
5a580b3a
AM
7012
7013 elf_link_hash_traverse (elf_hash_table (info),
7014 _bfd_elf_link_assign_sym_version,
7015 &asvinfo);
7016 if (asvinfo.failed)
0a1b45a2 7017 return false;
5a580b3a
AM
7018
7019 if (!info->allow_undefined_version)
7020 {
7021 /* Check if all global versions have a definition. */
0a1b45a2 7022 bool all_defined = true;
fd91d419 7023 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 7024 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 7025 if (d->literal && !d->symver && !d->script)
5a580b3a 7026 {
4eca0228 7027 _bfd_error_handler
5a580b3a
AM
7028 (_("%s: undefined version: %s"),
7029 d->pattern, t->name);
0a1b45a2 7030 all_defined = false;
5a580b3a
AM
7031 }
7032
7033 if (!all_defined)
7034 {
7035 bfd_set_error (bfd_error_bad_value);
0a1b45a2 7036 return false;
5a580b3a
AM
7037 }
7038 }
7039
902e9fc7
MR
7040 /* Set up the version definition section. */
7041 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
7042 BFD_ASSERT (s != NULL);
5a580b3a 7043
902e9fc7
MR
7044 /* We may have created additional version definitions if we are
7045 just linking a regular application. */
7046 verdefs = info->version_info;
5a580b3a 7047
902e9fc7
MR
7048 /* Skip anonymous version tag. */
7049 if (verdefs != NULL && verdefs->vernum == 0)
7050 verdefs = verdefs->next;
5a580b3a 7051
902e9fc7
MR
7052 if (verdefs == NULL && !info->create_default_symver)
7053 s->flags |= SEC_EXCLUDE;
7054 else
5a580b3a 7055 {
902e9fc7
MR
7056 unsigned int cdefs;
7057 bfd_size_type size;
7058 bfd_byte *p;
7059 Elf_Internal_Verdef def;
7060 Elf_Internal_Verdaux defaux;
7061 struct bfd_link_hash_entry *bh;
7062 struct elf_link_hash_entry *h;
7063 const char *name;
5a580b3a 7064
902e9fc7
MR
7065 cdefs = 0;
7066 size = 0;
5a580b3a 7067
902e9fc7
MR
7068 /* Make space for the base version. */
7069 size += sizeof (Elf_External_Verdef);
7070 size += sizeof (Elf_External_Verdaux);
7071 ++cdefs;
7072
7073 /* Make space for the default version. */
7074 if (info->create_default_symver)
7075 {
7076 size += sizeof (Elf_External_Verdef);
7077 ++cdefs;
3e3b46e5
PB
7078 }
7079
5a580b3a
AM
7080 for (t = verdefs; t != NULL; t = t->next)
7081 {
7082 struct bfd_elf_version_deps *n;
7083
a6cc6b3b
RO
7084 /* Don't emit base version twice. */
7085 if (t->vernum == 0)
7086 continue;
7087
5a580b3a
AM
7088 size += sizeof (Elf_External_Verdef);
7089 size += sizeof (Elf_External_Verdaux);
7090 ++cdefs;
7091
7092 for (n = t->deps; n != NULL; n = n->next)
7093 size += sizeof (Elf_External_Verdaux);
7094 }
7095
eea6121a 7096 s->size = size;
a50b1753 7097 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 7098 if (s->contents == NULL && s->size != 0)
0a1b45a2 7099 return false;
cb6326b5 7100 s->alloced = 1;
5a580b3a
AM
7101
7102 /* Fill in the version definition section. */
7103
7104 p = s->contents;
7105
7106 def.vd_version = VER_DEF_CURRENT;
7107 def.vd_flags = VER_FLG_BASE;
7108 def.vd_ndx = 1;
7109 def.vd_cnt = 1;
3e3b46e5
PB
7110 if (info->create_default_symver)
7111 {
7112 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
7113 def.vd_next = sizeof (Elf_External_Verdef);
7114 }
7115 else
7116 {
7117 def.vd_aux = sizeof (Elf_External_Verdef);
7118 def.vd_next = (sizeof (Elf_External_Verdef)
7119 + sizeof (Elf_External_Verdaux));
7120 }
5a580b3a 7121
ef53be89 7122 if (soname_indx != (size_t) -1)
5a580b3a
AM
7123 {
7124 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7125 soname_indx);
7126 def.vd_hash = bfd_elf_hash (soname);
7127 defaux.vda_name = soname_indx;
3e3b46e5 7128 name = soname;
5a580b3a
AM
7129 }
7130 else
7131 {
ef53be89 7132 size_t indx;
5a580b3a 7133
765cf5f6 7134 name = lbasename (bfd_get_filename (output_bfd));
5a580b3a
AM
7135 def.vd_hash = bfd_elf_hash (name);
7136 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
0a1b45a2 7137 name, false);
ef53be89 7138 if (indx == (size_t) -1)
0a1b45a2 7139 return false;
5a580b3a
AM
7140 defaux.vda_name = indx;
7141 }
7142 defaux.vda_next = 0;
7143
7144 _bfd_elf_swap_verdef_out (output_bfd, &def,
7145 (Elf_External_Verdef *) p);
7146 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
7147 if (info->create_default_symver)
7148 {
7149 /* Add a symbol representing this version. */
7150 bh = NULL;
7151 if (! (_bfd_generic_link_add_one_symbol
7152 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
0a1b45a2 7153 0, NULL, false,
3e3b46e5 7154 get_elf_backend_data (dynobj)->collect, &bh)))
0a1b45a2 7155 return false;
3e3b46e5
PB
7156 h = (struct elf_link_hash_entry *) bh;
7157 h->non_elf = 0;
7158 h->def_regular = 1;
7159 h->type = STT_OBJECT;
7160 h->verinfo.vertree = NULL;
7161
7162 if (! bfd_elf_link_record_dynamic_symbol (info, h))
0a1b45a2 7163 return false;
3e3b46e5
PB
7164
7165 /* Create a duplicate of the base version with the same
7166 aux block, but different flags. */
7167 def.vd_flags = 0;
7168 def.vd_ndx = 2;
7169 def.vd_aux = sizeof (Elf_External_Verdef);
7170 if (verdefs)
7171 def.vd_next = (sizeof (Elf_External_Verdef)
7172 + sizeof (Elf_External_Verdaux));
7173 else
7174 def.vd_next = 0;
7175 _bfd_elf_swap_verdef_out (output_bfd, &def,
7176 (Elf_External_Verdef *) p);
7177 p += sizeof (Elf_External_Verdef);
7178 }
5a580b3a
AM
7179 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
7180 (Elf_External_Verdaux *) p);
7181 p += sizeof (Elf_External_Verdaux);
7182
7183 for (t = verdefs; t != NULL; t = t->next)
7184 {
7185 unsigned int cdeps;
7186 struct bfd_elf_version_deps *n;
5a580b3a 7187
a6cc6b3b
RO
7188 /* Don't emit the base version twice. */
7189 if (t->vernum == 0)
7190 continue;
7191
5a580b3a
AM
7192 cdeps = 0;
7193 for (n = t->deps; n != NULL; n = n->next)
7194 ++cdeps;
7195
7196 /* Add a symbol representing this version. */
7197 bh = NULL;
7198 if (! (_bfd_generic_link_add_one_symbol
7199 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
0a1b45a2 7200 0, NULL, false,
5a580b3a 7201 get_elf_backend_data (dynobj)->collect, &bh)))
0a1b45a2 7202 return false;
5a580b3a 7203 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
7204 h->non_elf = 0;
7205 h->def_regular = 1;
5a580b3a
AM
7206 h->type = STT_OBJECT;
7207 h->verinfo.vertree = t;
7208
c152c796 7209 if (! bfd_elf_link_record_dynamic_symbol (info, h))
0a1b45a2 7210 return false;
5a580b3a
AM
7211
7212 def.vd_version = VER_DEF_CURRENT;
7213 def.vd_flags = 0;
7214 if (t->globals.list == NULL
7215 && t->locals.list == NULL
7216 && ! t->used)
7217 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 7218 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
7219 def.vd_cnt = cdeps + 1;
7220 def.vd_hash = bfd_elf_hash (t->name);
7221 def.vd_aux = sizeof (Elf_External_Verdef);
7222 def.vd_next = 0;
a6cc6b3b
RO
7223
7224 /* If a basever node is next, it *must* be the last node in
7225 the chain, otherwise Verdef construction breaks. */
7226 if (t->next != NULL && t->next->vernum == 0)
7227 BFD_ASSERT (t->next->next == NULL);
7228
7229 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
7230 def.vd_next = (sizeof (Elf_External_Verdef)
7231 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
7232
7233 _bfd_elf_swap_verdef_out (output_bfd, &def,
7234 (Elf_External_Verdef *) p);
7235 p += sizeof (Elf_External_Verdef);
7236
7237 defaux.vda_name = h->dynstr_index;
7238 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7239 h->dynstr_index);
7240 defaux.vda_next = 0;
7241 if (t->deps != NULL)
7242 defaux.vda_next = sizeof (Elf_External_Verdaux);
7243 t->name_indx = defaux.vda_name;
7244
7245 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
7246 (Elf_External_Verdaux *) p);
7247 p += sizeof (Elf_External_Verdaux);
7248
7249 for (n = t->deps; n != NULL; n = n->next)
7250 {
7251 if (n->version_needed == NULL)
7252 {
7253 /* This can happen if there was an error in the
7254 version script. */
7255 defaux.vda_name = 0;
7256 }
7257 else
7258 {
7259 defaux.vda_name = n->version_needed->name_indx;
7260 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
7261 defaux.vda_name);
7262 }
7263 if (n->next == NULL)
7264 defaux.vda_next = 0;
7265 else
7266 defaux.vda_next = sizeof (Elf_External_Verdaux);
7267
7268 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
7269 (Elf_External_Verdaux *) p);
7270 p += sizeof (Elf_External_Verdaux);
7271 }
7272 }
7273
5a580b3a
AM
7274 elf_tdata (output_bfd)->cverdefs = cdefs;
7275 }
902e9fc7
MR
7276 }
7277
902e9fc7
MR
7278 if (info->gc_sections && bed->can_gc_sections)
7279 {
7280 struct elf_gc_sweep_symbol_info sweep_info;
902e9fc7
MR
7281
7282 /* Remove the symbols that were in the swept sections from the
3d13f3e9 7283 dynamic symbol table. */
902e9fc7
MR
7284 sweep_info.info = info;
7285 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
7286 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
7287 &sweep_info);
3d13f3e9
AM
7288 }
7289
7290 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7291 {
7292 asection *s;
7293 struct elf_find_verdep_info sinfo;
7294
7295 /* Work out the size of the version reference section. */
7296
7297 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
7298 BFD_ASSERT (s != NULL);
902e9fc7 7299
3d13f3e9
AM
7300 sinfo.info = info;
7301 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
7302 if (sinfo.vers == 0)
7303 sinfo.vers = 1;
0a1b45a2 7304 sinfo.failed = false;
3d13f3e9
AM
7305
7306 elf_link_hash_traverse (elf_hash_table (info),
7307 _bfd_elf_link_find_version_dependencies,
7308 &sinfo);
7309 if (sinfo.failed)
0a1b45a2 7310 return false;
3d13f3e9 7311
91673042
L
7312 bed->elf_backend_add_glibc_version_dependency (&sinfo);
7313 if (sinfo.failed)
7314 return false;
72aa8173 7315
3d13f3e9
AM
7316 if (elf_tdata (output_bfd)->verref == NULL)
7317 s->flags |= SEC_EXCLUDE;
7318 else
7319 {
7320 Elf_Internal_Verneed *vn;
7321 unsigned int size;
7322 unsigned int crefs;
7323 bfd_byte *p;
7324
7325 /* Build the version dependency section. */
7326 size = 0;
7327 crefs = 0;
7328 for (vn = elf_tdata (output_bfd)->verref;
7329 vn != NULL;
7330 vn = vn->vn_nextref)
7331 {
7332 Elf_Internal_Vernaux *a;
7333
7334 size += sizeof (Elf_External_Verneed);
7335 ++crefs;
7336 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7337 size += sizeof (Elf_External_Vernaux);
7338 }
7339
7340 s->size = size;
7341 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7342 if (s->contents == NULL)
0a1b45a2 7343 return false;
cb6326b5 7344 s->alloced = 1;
3d13f3e9
AM
7345
7346 p = s->contents;
7347 for (vn = elf_tdata (output_bfd)->verref;
7348 vn != NULL;
7349 vn = vn->vn_nextref)
7350 {
7351 unsigned int caux;
7352 Elf_Internal_Vernaux *a;
7353 size_t indx;
7354
7355 caux = 0;
7356 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7357 ++caux;
7358
7359 vn->vn_version = VER_NEED_CURRENT;
7360 vn->vn_cnt = caux;
7361 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7362 elf_dt_name (vn->vn_bfd) != NULL
7363 ? elf_dt_name (vn->vn_bfd)
765cf5f6
AM
7364 : lbasename (bfd_get_filename
7365 (vn->vn_bfd)),
0a1b45a2 7366 false);
3d13f3e9 7367 if (indx == (size_t) -1)
0a1b45a2 7368 return false;
3d13f3e9
AM
7369 vn->vn_file = indx;
7370 vn->vn_aux = sizeof (Elf_External_Verneed);
7371 if (vn->vn_nextref == NULL)
7372 vn->vn_next = 0;
7373 else
7374 vn->vn_next = (sizeof (Elf_External_Verneed)
7375 + caux * sizeof (Elf_External_Vernaux));
7376
7377 _bfd_elf_swap_verneed_out (output_bfd, vn,
7378 (Elf_External_Verneed *) p);
7379 p += sizeof (Elf_External_Verneed);
7380
7381 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
7382 {
7383 a->vna_hash = bfd_elf_hash (a->vna_nodename);
7384 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
0a1b45a2 7385 a->vna_nodename, false);
3d13f3e9 7386 if (indx == (size_t) -1)
0a1b45a2 7387 return false;
3d13f3e9
AM
7388 a->vna_name = indx;
7389 if (a->vna_nextptr == NULL)
7390 a->vna_next = 0;
7391 else
7392 a->vna_next = sizeof (Elf_External_Vernaux);
7393
7394 _bfd_elf_swap_vernaux_out (output_bfd, a,
7395 (Elf_External_Vernaux *) p);
7396 p += sizeof (Elf_External_Vernaux);
7397 }
7398 }
7399
7400 elf_tdata (output_bfd)->cverrefs = crefs;
7401 }
902e9fc7
MR
7402 }
7403
902e9fc7
MR
7404 if (bfd_link_relocatable (info)
7405 && !_bfd_elf_size_group_sections (info))
0a1b45a2 7406 return false;
902e9fc7 7407
902e9fc7
MR
7408 /* Determine any GNU_STACK segment requirements, after the backend
7409 has had a chance to set a default segment size. */
7410 if (info->execstack)
65daf5be
NC
7411 {
7412 /* If the user has explicitly requested warnings, then generate one even
7413 though the choice is the result of another command line option. */
7414 if (info->warn_execstack == 1)
e922d1ea
NC
7415 {
7416 if (info->error_execstack)
7417 {
7418 _bfd_error_handler
7419 (_("\
7420error: creating an executable stack because of -z execstack command line option"));
7421 return false;
7422 }
7423
7424 _bfd_error_handler
7425 (_("\
65daf5be 7426warning: enabling an executable stack because of -z execstack command line option"));
e922d1ea
NC
7427 }
7428
65daf5be
NC
7429 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
7430 }
902e9fc7
MR
7431 else if (info->noexecstack)
7432 elf_stack_flags (output_bfd) = PF_R | PF_W;
7433 else
7434 {
7435 bfd *inputobj;
7436 asection *notesec = NULL;
65daf5be
NC
7437 bfd *noteobj = NULL;
7438 bfd *emptyobj = NULL;
902e9fc7
MR
7439 int exec = 0;
7440
7441 for (inputobj = info->input_bfds;
7442 inputobj;
7443 inputobj = inputobj->link.next)
7444 {
7445 asection *s;
7446
7447 if (inputobj->flags
7448 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
7449 continue;
57963c05
AM
7450 s = inputobj->sections;
7451 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7452 continue;
7453
902e9fc7
MR
7454 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
7455 if (s)
7456 {
902e9fc7 7457 notesec = s;
65daf5be
NC
7458 if (s->flags & SEC_CODE)
7459 {
7460 noteobj = inputobj;
7461 exec = PF_X;
7462 /* There is no point in scanning the remaining bfds. */
7463 break;
7464 }
902e9fc7 7465 }
ba951afb 7466 else if (bed->default_execstack && info->default_execstack)
65daf5be
NC
7467 {
7468 exec = PF_X;
7469 emptyobj = inputobj;
7470 }
902e9fc7 7471 }
65daf5be 7472
902e9fc7 7473 if (notesec || info->stacksize > 0)
65daf5be
NC
7474 {
7475 if (exec)
7476 {
bd7d326d 7477 if (info->warn_execstack != 0)
65daf5be
NC
7478 {
7479 /* PR 29072: Because an executable stack is a serious
7480 security risk, make sure that the user knows that it is
7481 being enabled despite the fact that it was not requested
7482 on the command line. */
7483 if (noteobj)
e922d1ea
NC
7484 {
7485 if (info->error_execstack)
7486 {
7487 _bfd_error_handler (_("\
7488error: %s: is triggering the generation of an executable stack (because it has an executable .note.GNU-stack section)"),
7489 bfd_get_filename (noteobj));
7490 return false;
7491 }
7492
7493 _bfd_error_handler (_("\
65daf5be
NC
7494warning: %s: requires executable stack (because the .note.GNU-stack section is executable)"),
7495 bfd_get_filename (noteobj));
e922d1ea 7496 }
65daf5be 7497 else if (emptyobj)
0d38576a 7498 {
e922d1ea
NC
7499 if (info->error_execstack)
7500 {
7501 _bfd_error_handler (_("\
7502error: %s: is triggering the generation of an executable stack because it does not have a .note.GNU-stack section"),
7503 bfd_get_filename (emptyobj));
7504 return false;
7505 }
7506
0d38576a 7507 _bfd_error_handler (_("\
65daf5be 7508warning: %s: missing .note.GNU-stack section implies executable stack"),
0d38576a
NC
7509 bfd_get_filename (emptyobj));
7510 _bfd_error_handler (_("\
7511NOTE: This behaviour is deprecated and will be removed in a future version of the linker"));
7512 }
65daf5be
NC
7513 }
7514 }
7515 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
7516 }
7517
902e9fc7
MR
7518 if (notesec && exec && bfd_link_relocatable (info)
7519 && notesec->output_section != bfd_abs_section_ptr)
7520 notesec->output_section->flags |= SEC_CODE;
7521 }
7522
7523 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7524 {
7525 struct elf_info_failed eif;
7526 struct elf_link_hash_entry *h;
7527 asection *dynstr;
7528 asection *s;
7529
7530 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
7531 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
7532
902e9fc7
MR
7533 if (info->symbolic)
7534 {
7535 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
0a1b45a2 7536 return false;
902e9fc7
MR
7537 info->flags |= DF_SYMBOLIC;
7538 }
7539
7540 if (rpath != NULL)
7541 {
7542 size_t indx;
7543 bfd_vma tag;
7544
7545 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
0a1b45a2 7546 true);
902e9fc7 7547 if (indx == (size_t) -1)
0a1b45a2 7548 return false;
902e9fc7
MR
7549
7550 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
7551 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
0a1b45a2 7552 return false;
902e9fc7
MR
7553 }
7554
7555 if (filter_shlib != NULL)
7556 {
7557 size_t indx;
7558
7559 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
0a1b45a2 7560 filter_shlib, true);
902e9fc7
MR
7561 if (indx == (size_t) -1
7562 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
0a1b45a2 7563 return false;
902e9fc7
MR
7564 }
7565
7566 if (auxiliary_filters != NULL)
7567 {
7568 const char * const *p;
7569
7570 for (p = auxiliary_filters; *p != NULL; p++)
7571 {
7572 size_t indx;
7573
7574 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
0a1b45a2 7575 *p, true);
902e9fc7
MR
7576 if (indx == (size_t) -1
7577 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
0a1b45a2 7578 return false;
902e9fc7
MR
7579 }
7580 }
7581
7582 if (audit != NULL)
7583 {
7584 size_t indx;
7585
7586 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
0a1b45a2 7587 true);
902e9fc7
MR
7588 if (indx == (size_t) -1
7589 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
0a1b45a2 7590 return false;
902e9fc7
MR
7591 }
7592
7593 if (depaudit != NULL)
7594 {
7595 size_t indx;
7596
7597 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
0a1b45a2 7598 true);
902e9fc7
MR
7599 if (indx == (size_t) -1
7600 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
0a1b45a2 7601 return false;
902e9fc7
MR
7602 }
7603
7604 eif.info = info;
0a1b45a2 7605 eif.failed = false;
902e9fc7
MR
7606
7607 /* Find all symbols which were defined in a dynamic object and make
7608 the backend pick a reasonable value for them. */
7609 elf_link_hash_traverse (elf_hash_table (info),
7610 _bfd_elf_adjust_dynamic_symbol,
7611 &eif);
7612 if (eif.failed)
0a1b45a2 7613 return false;
902e9fc7
MR
7614
7615 /* Add some entries to the .dynamic section. We fill in some of the
7616 values later, in bfd_elf_final_link, but we must add the entries
7617 now so that we know the final size of the .dynamic section. */
7618
7619 /* If there are initialization and/or finalization functions to
7620 call then add the corresponding DT_INIT/DT_FINI entries. */
7621 h = (info->init_function
7622 ? elf_link_hash_lookup (elf_hash_table (info),
0a1b45a2
AM
7623 info->init_function, false,
7624 false, false)
902e9fc7
MR
7625 : NULL);
7626 if (h != NULL
7627 && (h->ref_regular
7628 || h->def_regular))
7629 {
7630 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
0a1b45a2 7631 return false;
902e9fc7
MR
7632 }
7633 h = (info->fini_function
7634 ? elf_link_hash_lookup (elf_hash_table (info),
0a1b45a2
AM
7635 info->fini_function, false,
7636 false, false)
902e9fc7
MR
7637 : NULL);
7638 if (h != NULL
7639 && (h->ref_regular
7640 || h->def_regular))
7641 {
7642 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
0a1b45a2 7643 return false;
902e9fc7
MR
7644 }
7645
7646 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7647 if (s != NULL && s->linker_has_input)
7648 {
7649 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7650 if (! bfd_link_executable (info))
7651 {
7652 bfd *sub;
7653 asection *o;
7654
57963c05
AM
7655 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7656 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7657 && (o = sub->sections) != NULL
7658 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
902e9fc7
MR
7659 for (o = sub->sections; o != NULL; o = o->next)
7660 if (elf_section_data (o)->this_hdr.sh_type
7661 == SHT_PREINIT_ARRAY)
7662 {
7663 _bfd_error_handler
871b3ab2 7664 (_("%pB: .preinit_array section is not allowed in DSO"),
902e9fc7
MR
7665 sub);
7666 break;
7667 }
7668
7669 bfd_set_error (bfd_error_nonrepresentable_section);
0a1b45a2 7670 return false;
902e9fc7
MR
7671 }
7672
7673 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7674 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
0a1b45a2 7675 return false;
902e9fc7
MR
7676 }
7677 s = bfd_get_section_by_name (output_bfd, ".init_array");
7678 if (s != NULL && s->linker_has_input)
7679 {
7680 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7681 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
0a1b45a2 7682 return false;
902e9fc7
MR
7683 }
7684 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7685 if (s != NULL && s->linker_has_input)
7686 {
7687 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7688 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
0a1b45a2 7689 return false;
902e9fc7
MR
7690 }
7691
7692 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7693 /* If .dynstr is excluded from the link, we don't want any of
7694 these tags. Strictly, we should be checking each section
7695 individually; This quick check covers for the case where
7696 someone does a /DISCARD/ : { *(*) }. */
7697 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7698 {
7699 bfd_size_type strsize;
7700
7701 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7702 if ((info->emit_hash
7703 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7704 || (info->emit_gnu_hash
f16a9783
MS
7705 && (bed->record_xhash_symbol == NULL
7706 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
902e9fc7
MR
7707 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7708 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7709 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7710 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6a0a0dd0
VDM
7711 bed->s->sizeof_sym)
7712 || (info->gnu_flags_1
7713 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
7714 info->gnu_flags_1)))
0a1b45a2 7715 return false;
902e9fc7
MR
7716 }
7717 }
7718
7719 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
0a1b45a2 7720 return false;
902e9fc7
MR
7721
7722 /* The backend must work out the sizes of all the other dynamic
7723 sections. */
af969b14
AM
7724 if (bed->elf_backend_late_size_sections != NULL
7725 && !bed->elf_backend_late_size_sections (output_bfd, info))
0a1b45a2 7726 return false;
902e9fc7
MR
7727
7728 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7729 {
902e9fc7
MR
7730 if (elf_tdata (output_bfd)->cverdefs)
7731 {
7732 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7733
7734 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7735 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
0a1b45a2 7736 return false;
902e9fc7
MR
7737 }
7738
7739 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7740 {
7741 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
0a1b45a2 7742 return false;
902e9fc7
MR
7743 }
7744 else if (info->flags & DF_BIND_NOW)
7745 {
7746 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
0a1b45a2 7747 return false;
902e9fc7
MR
7748 }
7749
7750 if (info->flags_1)
7751 {
7752 if (bfd_link_executable (info))
7753 info->flags_1 &= ~ (DF_1_INITFIRST
7754 | DF_1_NODELETE
7755 | DF_1_NOOPEN);
7756 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
0a1b45a2 7757 return false;
902e9fc7
MR
7758 }
7759
7760 if (elf_tdata (output_bfd)->cverrefs)
7761 {
7762 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7763
7764 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7765 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
0a1b45a2 7766 return false;
902e9fc7 7767 }
5a580b3a 7768
8423293d
AM
7769 if ((elf_tdata (output_bfd)->cverrefs == 0
7770 && elf_tdata (output_bfd)->cverdefs == 0)
63f452a8 7771 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
8423293d 7772 {
902e9fc7
MR
7773 asection *s;
7774
3d4d4302 7775 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
7776 s->flags |= SEC_EXCLUDE;
7777 }
7778 }
0a1b45a2 7779 return true;
8423293d
AM
7780}
7781
74541ad4
AM
7782/* Find the first non-excluded output section. We'll use its
7783 section symbol for some emitted relocs. */
7784void
7785_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7786{
7787 asection *s;
f26a3287 7788 asection *found = NULL;
74541ad4
AM
7789
7790 for (s = output_bfd->sections; s != NULL; s = s->next)
7791 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
d00dd7dc 7792 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7793 {
f26a3287
AM
7794 found = s;
7795 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7796 break;
74541ad4 7797 }
f26a3287 7798 elf_hash_table (info)->text_index_section = found;
74541ad4
AM
7799}
7800
7801/* Find two non-excluded output sections, one for code, one for data.
7802 We'll use their section symbols for some emitted relocs. */
7803void
7804_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7805{
7806 asection *s;
f26a3287 7807 asection *found = NULL;
74541ad4 7808
266b05cf 7809 /* Data first, since setting text_index_section changes
7f923b7f 7810 _bfd_elf_omit_section_dynsym_default. */
74541ad4 7811 for (s = output_bfd->sections; s != NULL; s = s->next)
f26a3287
AM
7812 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7813 && !(s->flags & SEC_READONLY)
d00dd7dc 7814 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7815 {
f26a3287
AM
7816 found = s;
7817 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7818 break;
74541ad4 7819 }
f26a3287 7820 elf_hash_table (info)->data_index_section = found;
74541ad4
AM
7821
7822 for (s = output_bfd->sections; s != NULL; s = s->next)
f26a3287
AM
7823 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7824 && (s->flags & SEC_READONLY)
d00dd7dc 7825 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7826 {
f26a3287 7827 found = s;
74541ad4
AM
7828 break;
7829 }
f26a3287 7830 elf_hash_table (info)->text_index_section = found;
74541ad4
AM
7831}
7832
f16a9783
MS
7833#define GNU_HASH_SECTION_NAME(bed) \
7834 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7835
0a1b45a2 7836bool
8423293d
AM
7837bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7838{
74541ad4 7839 const struct elf_backend_data *bed;
23ec1e32 7840 unsigned long section_sym_count;
96d01d93 7841 bfd_size_type dynsymcount = 0;
74541ad4 7842
8423293d 7843 if (!is_elf_hash_table (info->hash))
0a1b45a2 7844 return true;
8423293d 7845
74541ad4
AM
7846 bed = get_elf_backend_data (output_bfd);
7847 (*bed->elf_backend_init_index_section) (output_bfd, info);
7848
23ec1e32
MR
7849 /* Assign dynsym indices. In a shared library we generate a section
7850 symbol for each output section, which come first. Next come all
7851 of the back-end allocated local dynamic syms, followed by the rest
7852 of the global symbols.
7853
7854 This is usually not needed for static binaries, however backends
7855 can request to always do it, e.g. the MIPS backend uses dynamic
7856 symbol counts to lay out GOT, which will be produced in the
7857 presence of GOT relocations even in static binaries (holding fixed
7858 data in that case, to satisfy those relocations). */
7859
7860 if (elf_hash_table (info)->dynamic_sections_created
7861 || bed->always_renumber_dynsyms)
7862 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7863 &section_sym_count);
7864
8423293d
AM
7865 if (elf_hash_table (info)->dynamic_sections_created)
7866 {
7867 bfd *dynobj;
8423293d 7868 asection *s;
8423293d
AM
7869 unsigned int dtagcount;
7870
7871 dynobj = elf_hash_table (info)->dynobj;
7872
5a580b3a 7873 /* Work out the size of the symbol version section. */
3d4d4302 7874 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 7875 BFD_ASSERT (s != NULL);
d5486c43 7876 if ((s->flags & SEC_EXCLUDE) == 0)
5a580b3a 7877 {
eea6121a 7878 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 7879 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a 7880 if (s->contents == NULL)
0a1b45a2 7881 return false;
cb6326b5 7882 s->alloced = 1;
5a580b3a
AM
7883
7884 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
0a1b45a2 7885 return false;
5a580b3a
AM
7886 }
7887
7888 /* Set the size of the .dynsym and .hash sections. We counted
7889 the number of dynamic symbols in elf_link_add_object_symbols.
7890 We will build the contents of .dynsym and .hash when we build
7891 the final symbol table, because until then we do not know the
7892 correct value to give the symbols. We built the .dynstr
7893 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 7894 s = elf_hash_table (info)->dynsym;
5a580b3a 7895 BFD_ASSERT (s != NULL);
eea6121a 7896 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a 7897
d5486c43
L
7898 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7899 if (s->contents == NULL)
0a1b45a2 7900 return false;
cb6326b5 7901 s->alloced = 1;
5a580b3a 7902
d5486c43
L
7903 /* The first entry in .dynsym is a dummy symbol. Clear all the
7904 section syms, in case we don't output them all. */
7905 ++section_sym_count;
7906 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a 7907
fdc90cb4
JJ
7908 elf_hash_table (info)->bucketcount = 0;
7909
5a580b3a
AM
7910 /* Compute the size of the hashing table. As a side effect this
7911 computes the hash values for all the names we export. */
fdc90cb4
JJ
7912 if (info->emit_hash)
7913 {
7914 unsigned long int *hashcodes;
14b1c01e 7915 struct hash_codes_info hashinf;
fdc90cb4
JJ
7916 bfd_size_type amt;
7917 unsigned long int nsyms;
7918 size_t bucketcount;
7919 size_t hash_entry_size;
7920
7921 /* Compute the hash values for all exported symbols. At the same
7922 time store the values in an array so that we could use them for
7923 optimizations. */
7924 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 7925 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4 7926 if (hashcodes == NULL)
0a1b45a2 7927 return false;
14b1c01e 7928 hashinf.hashcodes = hashcodes;
0a1b45a2 7929 hashinf.error = false;
5a580b3a 7930
fdc90cb4
JJ
7931 /* Put all hash values in HASHCODES. */
7932 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
7933 elf_collect_hash_codes, &hashinf);
7934 if (hashinf.error)
4dd07732
AM
7935 {
7936 free (hashcodes);
0a1b45a2 7937 return false;
4dd07732 7938 }
5a580b3a 7939
14b1c01e 7940 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
7941 bucketcount
7942 = compute_bucket_count (info, hashcodes, nsyms, 0);
7943 free (hashcodes);
7944
4b48e2f6 7945 if (bucketcount == 0 && nsyms > 0)
0a1b45a2 7946 return false;
5a580b3a 7947
fdc90cb4
JJ
7948 elf_hash_table (info)->bucketcount = bucketcount;
7949
3d4d4302 7950 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
7951 BFD_ASSERT (s != NULL);
7952 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7953 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 7954 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4 7955 if (s->contents == NULL)
0a1b45a2 7956 return false;
cb6326b5 7957 s->alloced = 1;
fdc90cb4
JJ
7958
7959 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7960 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7961 s->contents + hash_entry_size);
7962 }
7963
7964 if (info->emit_gnu_hash)
7965 {
7966 size_t i, cnt;
7967 unsigned char *contents;
7968 struct collect_gnu_hash_codes cinfo;
7969 bfd_size_type amt;
7970 size_t bucketcount;
7971
7972 memset (&cinfo, 0, sizeof (cinfo));
7973
7974 /* Compute the hash values for all exported symbols. At the same
7975 time store the values in an array so that we could use them for
7976 optimizations. */
7977 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 7978 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4 7979 if (cinfo.hashcodes == NULL)
0a1b45a2 7980 return false;
fdc90cb4
JJ
7981
7982 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7983 cinfo.min_dynindx = -1;
7984 cinfo.output_bfd = output_bfd;
7985 cinfo.bed = bed;
7986
7987 /* Put all hash values in HASHCODES. */
7988 elf_link_hash_traverse (elf_hash_table (info),
7989 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 7990 if (cinfo.error)
4dd07732
AM
7991 {
7992 free (cinfo.hashcodes);
0a1b45a2 7993 return false;
4dd07732 7994 }
fdc90cb4
JJ
7995
7996 bucketcount
7997 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7998
7999 if (bucketcount == 0)
8000 {
8001 free (cinfo.hashcodes);
0a1b45a2 8002 return false;
fdc90cb4
JJ
8003 }
8004
f16a9783 8005 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
fdc90cb4
JJ
8006 BFD_ASSERT (s != NULL);
8007
8008 if (cinfo.nsyms == 0)
8009 {
f16a9783 8010 /* Empty .gnu.hash or .MIPS.xhash section is special. */
fdc90cb4
JJ
8011 BFD_ASSERT (cinfo.min_dynindx == -1);
8012 free (cinfo.hashcodes);
8013 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 8014 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4 8015 if (contents == NULL)
0a1b45a2 8016 return false;
fdc90cb4 8017 s->contents = contents;
cb6326b5 8018 s->alloced = 1;
fdc90cb4
JJ
8019 /* 1 empty bucket. */
8020 bfd_put_32 (output_bfd, 1, contents);
8021 /* SYMIDX above the special symbol 0. */
8022 bfd_put_32 (output_bfd, 1, contents + 4);
8023 /* Just one word for bitmask. */
8024 bfd_put_32 (output_bfd, 1, contents + 8);
8025 /* Only hash fn bloom filter. */
8026 bfd_put_32 (output_bfd, 0, contents + 12);
8027 /* No hashes are valid - empty bitmask. */
8028 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
8029 /* No hashes in the only bucket. */
8030 bfd_put_32 (output_bfd, 0,
8031 contents + 16 + bed->s->arch_size / 8);
8032 }
8033 else
8034 {
9e6619e2 8035 unsigned long int maskwords, maskbitslog2, x;
0b33793d 8036 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 8037
9e6619e2
AM
8038 x = cinfo.nsyms;
8039 maskbitslog2 = 1;
8040 while ((x >>= 1) != 0)
8041 ++maskbitslog2;
fdc90cb4
JJ
8042 if (maskbitslog2 < 3)
8043 maskbitslog2 = 5;
8044 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
8045 maskbitslog2 = maskbitslog2 + 3;
8046 else
8047 maskbitslog2 = maskbitslog2 + 2;
8048 if (bed->s->arch_size == 64)
8049 {
8050 if (maskbitslog2 == 5)
8051 maskbitslog2 = 6;
8052 cinfo.shift1 = 6;
8053 }
8054 else
8055 cinfo.shift1 = 5;
8056 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 8057 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
8058 cinfo.maskbits = 1 << maskbitslog2;
8059 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
8060 amt = bucketcount * sizeof (unsigned long int) * 2;
8061 amt += maskwords * sizeof (bfd_vma);
a50b1753 8062 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
8063 if (cinfo.bitmask == NULL)
8064 {
8065 free (cinfo.hashcodes);
0a1b45a2 8066 return false;
fdc90cb4
JJ
8067 }
8068
a50b1753 8069 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
8070 cinfo.indx = cinfo.counts + bucketcount;
8071 cinfo.symindx = dynsymcount - cinfo.nsyms;
8072 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
8073
8074 /* Determine how often each hash bucket is used. */
8075 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
8076 for (i = 0; i < cinfo.nsyms; ++i)
8077 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
8078
8079 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
8080 if (cinfo.counts[i] != 0)
8081 {
8082 cinfo.indx[i] = cnt;
8083 cnt += cinfo.counts[i];
8084 }
8085 BFD_ASSERT (cnt == dynsymcount);
8086 cinfo.bucketcount = bucketcount;
8087 cinfo.local_indx = cinfo.min_dynindx;
8088
8089 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
8090 s->size += cinfo.maskbits / 8;
f16a9783
MS
8091 if (bed->record_xhash_symbol != NULL)
8092 s->size += cinfo.nsyms * 4;
a50b1753 8093 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
8094 if (contents == NULL)
8095 {
8096 free (cinfo.bitmask);
8097 free (cinfo.hashcodes);
0a1b45a2 8098 return false;
fdc90cb4
JJ
8099 }
8100
8101 s->contents = contents;
cb6326b5 8102 s->alloced = 1;
fdc90cb4
JJ
8103 bfd_put_32 (output_bfd, bucketcount, contents);
8104 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
8105 bfd_put_32 (output_bfd, maskwords, contents + 8);
8106 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
8107 contents += 16 + cinfo.maskbits / 8;
8108
8109 for (i = 0; i < bucketcount; ++i)
8110 {
8111 if (cinfo.counts[i] == 0)
8112 bfd_put_32 (output_bfd, 0, contents);
8113 else
8114 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
8115 contents += 4;
8116 }
8117
8118 cinfo.contents = contents;
8119
f16a9783
MS
8120 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
8121 /* Renumber dynamic symbols, if populating .gnu.hash section.
8122 If using .MIPS.xhash, populate the translation table. */
fdc90cb4 8123 elf_link_hash_traverse (elf_hash_table (info),
f16a9783 8124 elf_gnu_hash_process_symidx, &cinfo);
fdc90cb4
JJ
8125
8126 contents = s->contents + 16;
8127 for (i = 0; i < maskwords; ++i)
8128 {
8129 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
8130 contents);
8131 contents += bed->s->arch_size / 8;
8132 }
8133
8134 free (cinfo.bitmask);
8135 free (cinfo.hashcodes);
8136 }
8137 }
5a580b3a 8138
3d4d4302 8139 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
8140 BFD_ASSERT (s != NULL);
8141
4ad4eba5 8142 elf_finalize_dynstr (output_bfd, info);
5a580b3a 8143
eea6121a 8144 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
8145
8146 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
8147 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
0a1b45a2 8148 return false;
5a580b3a
AM
8149 }
8150
0a1b45a2 8151 return true;
5a580b3a 8152}
4d269e42 8153\f
4d269e42
AM
8154/* Make sure sec_info_type is cleared if sec_info is cleared too. */
8155
8156static void
8157merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
8158 asection *sec)
8159{
dbaa2011
AM
8160 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
8161 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
8162}
8163
8164/* Finish SHF_MERGE section merging. */
8165
0a1b45a2 8166bool
630993ec 8167_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
8168{
8169 bfd *ibfd;
8170 asection *sec;
8171
32dc3672
AM
8172 if (ENABLE_CHECKING && !is_elf_hash_table (info->hash))
8173 abort ();
4d269e42 8174
c72f2fb2 8175 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
8176 if ((ibfd->flags & DYNAMIC) == 0
8177 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
8178 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
8179 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
8180 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8181 if ((sec->flags & SEC_MERGE) != 0
8182 && !bfd_is_abs_section (sec->output_section))
8183 {
8184 struct bfd_elf_section_data *secdata;
8185
8186 secdata = elf_section_data (sec);
630993ec 8187 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
8188 &elf_hash_table (info)->merge_info,
8189 sec, &secdata->sec_info))
0a1b45a2 8190 return false;
4d269e42 8191 else if (secdata->sec_info)
dbaa2011 8192 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
8193 }
8194
8195 if (elf_hash_table (info)->merge_info != NULL)
32dc3672
AM
8196 return _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
8197 merge_sections_remove_hook);
0a1b45a2 8198 return true;
4d269e42
AM
8199}
8200
8201/* Create an entry in an ELF linker hash table. */
8202
8203struct bfd_hash_entry *
8204_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
8205 struct bfd_hash_table *table,
8206 const char *string)
8207{
8208 /* Allocate the structure if it has not already been allocated by a
8209 subclass. */
8210 if (entry == NULL)
8211 {
a50b1753 8212 entry = (struct bfd_hash_entry *)
ca4be51c 8213 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
8214 if (entry == NULL)
8215 return entry;
8216 }
8217
8218 /* Call the allocation method of the superclass. */
8219 entry = _bfd_link_hash_newfunc (entry, table, string);
8220 if (entry != NULL)
8221 {
8222 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
8223 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
8224
8225 /* Set local fields. */
8226 ret->indx = -1;
8227 ret->dynindx = -1;
8228 ret->got = htab->init_got_refcount;
8229 ret->plt = htab->init_plt_refcount;
8230 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
8231 - offsetof (struct elf_link_hash_entry, size)));
8232 /* Assume that we have been called by a non-ELF symbol reader.
8233 This flag is then reset by the code which reads an ELF input
8234 file. This ensures that a symbol created by a non-ELF symbol
8235 reader will have the flag set correctly. */
8236 ret->non_elf = 1;
8237 }
8238
8239 return entry;
8240}
8241
8242/* Copy data from an indirect symbol to its direct symbol, hiding the
8243 old indirect symbol. Also used for copying flags to a weakdef. */
8244
8245void
8246_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
8247 struct elf_link_hash_entry *dir,
8248 struct elf_link_hash_entry *ind)
8249{
8250 struct elf_link_hash_table *htab;
8251
ad172eaa
L
8252 if (ind->dyn_relocs != NULL)
8253 {
8254 if (dir->dyn_relocs != NULL)
8255 {
8256 struct elf_dyn_relocs **pp;
8257 struct elf_dyn_relocs *p;
8258
8259 /* Add reloc counts against the indirect sym to the direct sym
8260 list. Merge any entries against the same section. */
8261 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
8262 {
8263 struct elf_dyn_relocs *q;
8264
8265 for (q = dir->dyn_relocs; q != NULL; q = q->next)
8266 if (q->sec == p->sec)
8267 {
8268 q->pc_count += p->pc_count;
8269 q->count += p->count;
8270 *pp = p->next;
8271 break;
8272 }
8273 if (q == NULL)
8274 pp = &p->next;
8275 }
8276 *pp = dir->dyn_relocs;
8277 }
8278
8279 dir->dyn_relocs = ind->dyn_relocs;
8280 ind->dyn_relocs = NULL;
8281 }
8282
4d269e42 8283 /* Copy down any references that we may have already seen to the
e81830c5 8284 symbol which just became indirect. */
4d269e42 8285
422f1182 8286 if (dir->versioned != versioned_hidden)
e81830c5
AM
8287 dir->ref_dynamic |= ind->ref_dynamic;
8288 dir->ref_regular |= ind->ref_regular;
8289 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
8290 dir->non_got_ref |= ind->non_got_ref;
8291 dir->needs_plt |= ind->needs_plt;
8292 dir->pointer_equality_needed |= ind->pointer_equality_needed;
4d269e42
AM
8293
8294 if (ind->root.type != bfd_link_hash_indirect)
8295 return;
8296
8297 /* Copy over the global and procedure linkage table refcount entries.
8298 These may have been already set up by a check_relocs routine. */
8299 htab = elf_hash_table (info);
8300 if (ind->got.refcount > htab->init_got_refcount.refcount)
8301 {
8302 if (dir->got.refcount < 0)
8303 dir->got.refcount = 0;
8304 dir->got.refcount += ind->got.refcount;
8305 ind->got.refcount = htab->init_got_refcount.refcount;
8306 }
8307
8308 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
8309 {
8310 if (dir->plt.refcount < 0)
8311 dir->plt.refcount = 0;
8312 dir->plt.refcount += ind->plt.refcount;
8313 ind->plt.refcount = htab->init_plt_refcount.refcount;
8314 }
8315
8316 if (ind->dynindx != -1)
8317 {
8318 if (dir->dynindx != -1)
8319 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
8320 dir->dynindx = ind->dynindx;
8321 dir->dynstr_index = ind->dynstr_index;
8322 ind->dynindx = -1;
8323 ind->dynstr_index = 0;
8324 }
8325}
8326
8327void
8328_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
8329 struct elf_link_hash_entry *h,
0a1b45a2 8330 bool force_local)
4d269e42 8331{
3aa14d16
L
8332 /* STT_GNU_IFUNC symbol must go through PLT. */
8333 if (h->type != STT_GNU_IFUNC)
8334 {
8335 h->plt = elf_hash_table (info)->init_plt_offset;
8336 h->needs_plt = 0;
8337 }
4d269e42
AM
8338 if (force_local)
8339 {
8340 h->forced_local = 1;
8341 if (h->dynindx != -1)
8342 {
4d269e42
AM
8343 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8344 h->dynstr_index);
641338d8
AM
8345 h->dynindx = -1;
8346 h->dynstr_index = 0;
4d269e42
AM
8347 }
8348 }
8349}
8350
34a87bb0
L
8351/* Hide a symbol. */
8352
8353void
8354_bfd_elf_link_hide_symbol (bfd *output_bfd,
8355 struct bfd_link_info *info,
8356 struct bfd_link_hash_entry *h)
8357{
8358 if (is_elf_hash_table (info->hash))
8359 {
8360 const struct elf_backend_data *bed
8361 = get_elf_backend_data (output_bfd);
8362 struct elf_link_hash_entry *eh
8363 = (struct elf_link_hash_entry *) h;
0a1b45a2 8364 bed->elf_backend_hide_symbol (info, eh, true);
34a87bb0
L
8365 eh->def_dynamic = 0;
8366 eh->ref_dynamic = 0;
8367 eh->dynamic_def = 0;
8368 }
8369}
8370
7bf52ea2
AM
8371/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
8372 caller. */
4d269e42 8373
0a1b45a2 8374bool
4d269e42
AM
8375_bfd_elf_link_hash_table_init
8376 (struct elf_link_hash_table *table,
8377 bfd *abfd,
8378 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
8379 struct bfd_hash_table *,
8380 const char *),
f92b077e 8381 unsigned int entsize)
4d269e42 8382{
0a1b45a2 8383 bool ret;
f92b077e
AM
8384 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8385 int can_refcount = bed->can_refcount;
4d269e42 8386
4d269e42
AM
8387 table->init_got_refcount.refcount = can_refcount - 1;
8388 table->init_plt_refcount.refcount = can_refcount - 1;
8389 table->init_got_offset.offset = -(bfd_vma) 1;
8390 table->init_plt_offset.offset = -(bfd_vma) 1;
8391 /* The first dynamic symbol is a dummy. */
8392 table->dynsymcount = 1;
8393
8394 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 8395
4d269e42 8396 table->root.type = bfd_link_elf_hash_table;
f92b077e
AM
8397 table->hash_table_id = bed->target_id;
8398 table->target_os = bed->target_os;
8c0361c7 8399 table->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
8400
8401 return ret;
8402}
8403
8404/* Create an ELF linker hash table. */
8405
8406struct bfd_link_hash_table *
8407_bfd_elf_link_hash_table_create (bfd *abfd)
8408{
8409 struct elf_link_hash_table *ret;
986f0783 8410 size_t amt = sizeof (struct elf_link_hash_table);
4d269e42 8411
7bf52ea2 8412 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
8413 if (ret == NULL)
8414 return NULL;
8415
8416 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
f92b077e 8417 sizeof (struct elf_link_hash_entry)))
4d269e42
AM
8418 {
8419 free (ret);
8420 return NULL;
8421 }
8422
8423 return &ret->root;
8424}
8425
9f7c3e5e
AM
8426/* Destroy an ELF linker hash table. */
8427
8428void
d495ab0d 8429_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 8430{
d495ab0d
AM
8431 struct elf_link_hash_table *htab;
8432
8433 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
8434 if (htab->dynstr != NULL)
8435 _bfd_elf_strtab_free (htab->dynstr);
8436 _bfd_merge_sections_free (htab->merge_info);
c3460201
L
8437 /* NB: htab->dynamic->contents is always allocated by bfd_realloc. */
8438 if (htab->dynamic != NULL)
cb6326b5
AM
8439 {
8440 free (htab->dynamic->contents);
8441 htab->dynamic->contents = NULL;
8442 }
c0419c02
L
8443 if (htab->first_hash != NULL)
8444 {
816fd3dc 8445 bfd_hash_table_free (htab->first_hash);
c0419c02
L
8446 free (htab->first_hash);
8447 }
ee8f3b6c
AM
8448 if (htab->eh_info.frame_hdr_is_compact)
8449 free (htab->eh_info.u.compact.entries);
8450 else
8451 free (htab->eh_info.u.dwarf.array);
816fd3dc 8452 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
8453}
8454
4d269e42
AM
8455/* This is a hook for the ELF emulation code in the generic linker to
8456 tell the backend linker what file name to use for the DT_NEEDED
8457 entry for a dynamic object. */
8458
8459void
8460bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
8461{
8462 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8463 && bfd_get_format (abfd) == bfd_object)
8464 elf_dt_name (abfd) = name;
8465}
8466
8467int
8468bfd_elf_get_dyn_lib_class (bfd *abfd)
8469{
8470 int lib_class;
8471 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8472 && bfd_get_format (abfd) == bfd_object)
8473 lib_class = elf_dyn_lib_class (abfd);
8474 else
8475 lib_class = 0;
8476 return lib_class;
8477}
8478
8479void
8480bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
8481{
8482 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8483 && bfd_get_format (abfd) == bfd_object)
8484 elf_dyn_lib_class (abfd) = lib_class;
8485}
8486
8487/* Get the list of DT_NEEDED entries for a link. This is a hook for
8488 the linker ELF emulation code. */
8489
8490struct bfd_link_needed_list *
8491bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
8492 struct bfd_link_info *info)
8493{
8494 if (! is_elf_hash_table (info->hash))
8495 return NULL;
8496 return elf_hash_table (info)->needed;
8497}
8498
8499/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
8500 hook for the linker ELF emulation code. */
8501
8502struct bfd_link_needed_list *
8503bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
8504 struct bfd_link_info *info)
8505{
8506 if (! is_elf_hash_table (info->hash))
8507 return NULL;
8508 return elf_hash_table (info)->runpath;
8509}
8510
8511/* Get the name actually used for a dynamic object for a link. This
8512 is the SONAME entry if there is one. Otherwise, it is the string
8513 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
8514
8515const char *
8516bfd_elf_get_dt_soname (bfd *abfd)
8517{
8518 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
8519 && bfd_get_format (abfd) == bfd_object)
8520 return elf_dt_name (abfd);
8521 return NULL;
8522}
8523
8524/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
8525 the ELF linker emulation code. */
8526
0a1b45a2 8527bool
4d269e42
AM
8528bfd_elf_get_bfd_needed_list (bfd *abfd,
8529 struct bfd_link_needed_list **pneeded)
8530{
8531 asection *s;
8532 bfd_byte *dynbuf = NULL;
cb33740c 8533 unsigned int elfsec;
4d269e42
AM
8534 unsigned long shlink;
8535 bfd_byte *extdyn, *extdynend;
8536 size_t extdynsize;
8537 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
8538
8539 *pneeded = NULL;
8540
8541 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
8542 || bfd_get_format (abfd) != bfd_object)
0a1b45a2 8543 return true;
4d269e42
AM
8544
8545 s = bfd_get_section_by_name (abfd, ".dynamic");
81ff113f 8546 if (s == NULL || s->size == 0 || (s->flags & SEC_HAS_CONTENTS) == 0)
0a1b45a2 8547 return true;
4d269e42 8548
584b30e4 8549 if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
4d269e42
AM
8550 goto error_return;
8551
8552 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 8553 if (elfsec == SHN_BAD)
4d269e42
AM
8554 goto error_return;
8555
8556 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 8557
4d269e42
AM
8558 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
8559 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
8560
37c59664
AM
8561 for (extdyn = dynbuf, extdynend = dynbuf + s->size;
8562 (size_t) (extdynend - extdyn) >= extdynsize;
8563 extdyn += extdynsize)
4d269e42
AM
8564 {
8565 Elf_Internal_Dyn dyn;
8566
8567 (*swap_dyn_in) (abfd, extdyn, &dyn);
8568
8569 if (dyn.d_tag == DT_NULL)
8570 break;
8571
8572 if (dyn.d_tag == DT_NEEDED)
8573 {
8574 const char *string;
8575 struct bfd_link_needed_list *l;
8576 unsigned int tagv = dyn.d_un.d_val;
986f0783 8577 size_t amt;
4d269e42
AM
8578
8579 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8580 if (string == NULL)
8581 goto error_return;
8582
8583 amt = sizeof *l;
a50b1753 8584 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
8585 if (l == NULL)
8586 goto error_return;
8587
8588 l->by = abfd;
8589 l->name = string;
8590 l->next = *pneeded;
8591 *pneeded = l;
8592 }
8593 }
8594
584b30e4 8595 _bfd_elf_munmap_section_contents (s, dynbuf);
4d269e42 8596
0a1b45a2 8597 return true;
4d269e42
AM
8598
8599 error_return:
584b30e4 8600 _bfd_elf_munmap_section_contents (s, dynbuf);
0a1b45a2 8601 return false;
4d269e42
AM
8602}
8603
8604struct elf_symbuf_symbol
8605{
8606 unsigned long st_name; /* Symbol name, index in string tbl */
8607 unsigned char st_info; /* Type and binding attributes */
8608 unsigned char st_other; /* Visibilty, and target specific */
8609};
8610
8611struct elf_symbuf_head
8612{
8613 struct elf_symbuf_symbol *ssym;
ef53be89 8614 size_t count;
4d269e42
AM
8615 unsigned int st_shndx;
8616};
8617
8618struct elf_symbol
8619{
8620 union
8621 {
8622 Elf_Internal_Sym *isym;
8623 struct elf_symbuf_symbol *ssym;
dcea6a95 8624 void *p;
4d269e42
AM
8625 } u;
8626 const char *name;
8627};
8628
8629/* Sort references to symbols by ascending section number. */
8630
8631static int
8632elf_sort_elf_symbol (const void *arg1, const void *arg2)
8633{
8634 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8635 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8636
dcea6a95
AM
8637 if (s1->st_shndx != s2->st_shndx)
8638 return s1->st_shndx > s2->st_shndx ? 1 : -1;
8639 /* Final sort by the address of the sym in the symbuf ensures
8640 a stable sort. */
8641 if (s1 != s2)
8642 return s1 > s2 ? 1 : -1;
8643 return 0;
4d269e42
AM
8644}
8645
8646static int
8647elf_sym_name_compare (const void *arg1, const void *arg2)
8648{
8649 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8650 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
dcea6a95
AM
8651 int ret = strcmp (s1->name, s2->name);
8652 if (ret != 0)
8653 return ret;
8654 if (s1->u.p != s2->u.p)
8655 return s1->u.p > s2->u.p ? 1 : -1;
8656 return 0;
4d269e42
AM
8657}
8658
8659static struct elf_symbuf_head *
ef53be89 8660elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
4d269e42 8661{
14b1c01e 8662 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
8663 struct elf_symbuf_symbol *ssym;
8664 struct elf_symbuf_head *ssymbuf, *ssymhead;
446f7ed5 8665 size_t i, shndx_count, total_size, amt;
4d269e42 8666
446f7ed5
AM
8667 amt = symcount * sizeof (*indbuf);
8668 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
4d269e42
AM
8669 if (indbuf == NULL)
8670 return NULL;
8671
8672 for (ind = indbuf, i = 0; i < symcount; i++)
3818d4ab 8673 if (isymbuf[i].st_shndx != SHN_UNDEF)
4d269e42
AM
8674 *ind++ = &isymbuf[i];
8675 indbufend = ind;
8676
8677 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8678 elf_sort_elf_symbol);
8679
8680 shndx_count = 0;
8681 if (indbufend > indbuf)
8682 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8683 if (ind[0]->st_shndx != ind[1]->st_shndx)
8684 shndx_count++;
8685
3ae181ee
L
8686 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8687 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 8688 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
8689 if (ssymbuf == NULL)
8690 {
8691 free (indbuf);
8692 return NULL;
8693 }
8694
3ae181ee 8695 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
8696 ssymbuf->ssym = NULL;
8697 ssymbuf->count = shndx_count;
8698 ssymbuf->st_shndx = 0;
8699 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8700 {
8701 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8702 {
8703 ssymhead++;
8704 ssymhead->ssym = ssym;
8705 ssymhead->count = 0;
8706 ssymhead->st_shndx = (*ind)->st_shndx;
8707 }
8708 ssym->st_name = (*ind)->st_name;
8709 ssym->st_info = (*ind)->st_info;
8710 ssym->st_other = (*ind)->st_other;
8711 ssymhead->count++;
8712 }
ef53be89 8713 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
60159858 8714 && (uintptr_t) ssym - (uintptr_t) ssymbuf == total_size);
4d269e42
AM
8715
8716 free (indbuf);
8717 return ssymbuf;
8718}
8719
8720/* Check if 2 sections define the same set of local and global
8721 symbols. */
8722
0a1b45a2 8723static bool
4d269e42
AM
8724bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8725 struct bfd_link_info *info)
8726{
8727 bfd *bfd1, *bfd2;
8728 const struct elf_backend_data *bed1, *bed2;
8729 Elf_Internal_Shdr *hdr1, *hdr2;
ef53be89 8730 size_t symcount1, symcount2;
4d269e42
AM
8731 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8732 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8733 Elf_Internal_Sym *isym, *isymend;
8734 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
3818d4ab 8735 size_t count1, count2, sec_count1, sec_count2, i;
cb33740c 8736 unsigned int shndx1, shndx2;
0a1b45a2
AM
8737 bool result;
8738 bool ignore_section_symbol_p;
4d269e42
AM
8739
8740 bfd1 = sec1->owner;
8741 bfd2 = sec2->owner;
8742
4d269e42
AM
8743 /* Both sections have to be in ELF. */
8744 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8745 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
0a1b45a2 8746 return false;
4d269e42
AM
8747
8748 if (elf_section_type (sec1) != elf_section_type (sec2))
0a1b45a2 8749 return false;
4d269e42 8750
4d269e42
AM
8751 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8752 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 8753 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
0a1b45a2 8754 return false;
4d269e42
AM
8755
8756 bed1 = get_elf_backend_data (bfd1);
8757 bed2 = get_elf_backend_data (bfd2);
8758 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8759 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8760 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8761 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8762
8763 if (symcount1 == 0 || symcount2 == 0)
0a1b45a2 8764 return false;
4d269e42 8765
0a1b45a2 8766 result = false;
4d269e42
AM
8767 isymbuf1 = NULL;
8768 isymbuf2 = NULL;
a50b1753
NC
8769 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8770 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42 8771
3818d4ab
L
8772 /* Ignore section symbols only when matching non-debugging sections
8773 or linkonce section with comdat section. */
8774 ignore_section_symbol_p
8775 = ((sec1->flags & SEC_DEBUGGING) == 0
8776 || ((elf_section_flags (sec1) & SHF_GROUP)
8777 != (elf_section_flags (sec2) & SHF_GROUP)));
8778
4d269e42
AM
8779 if (ssymbuf1 == NULL)
8780 {
8781 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8782 NULL, NULL, NULL);
8783 if (isymbuf1 == NULL)
8784 goto done;
8785
67411cbf 8786 if (info != NULL && !info->reduce_memory_overheads)
dcea6a95
AM
8787 {
8788 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8789 elf_tdata (bfd1)->symbuf = ssymbuf1;
8790 }
4d269e42
AM
8791 }
8792
8793 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8794 {
8795 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8796 NULL, NULL, NULL);
8797 if (isymbuf2 == NULL)
8798 goto done;
8799
67411cbf 8800 if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
dcea6a95
AM
8801 {
8802 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8803 elf_tdata (bfd2)->symbuf = ssymbuf2;
8804 }
4d269e42
AM
8805 }
8806
8807 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8808 {
8809 /* Optimized faster version. */
ef53be89 8810 size_t lo, hi, mid;
4d269e42
AM
8811 struct elf_symbol *symp;
8812 struct elf_symbuf_symbol *ssym, *ssymend;
8813
8814 lo = 0;
8815 hi = ssymbuf1->count;
8816 ssymbuf1++;
8817 count1 = 0;
3818d4ab 8818 sec_count1 = 0;
4d269e42
AM
8819 while (lo < hi)
8820 {
8821 mid = (lo + hi) / 2;
cb33740c 8822 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 8823 hi = mid;
cb33740c 8824 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
8825 lo = mid + 1;
8826 else
8827 {
8828 count1 = ssymbuf1[mid].count;
8829 ssymbuf1 += mid;
8830 break;
8831 }
8832 }
3818d4ab
L
8833 if (ignore_section_symbol_p)
8834 {
8835 for (i = 0; i < count1; i++)
8836 if (ELF_ST_TYPE (ssymbuf1->ssym[i].st_info) == STT_SECTION)
8837 sec_count1++;
8838 count1 -= sec_count1;
8839 }
4d269e42
AM
8840
8841 lo = 0;
8842 hi = ssymbuf2->count;
8843 ssymbuf2++;
8844 count2 = 0;
3818d4ab 8845 sec_count2 = 0;
4d269e42
AM
8846 while (lo < hi)
8847 {
8848 mid = (lo + hi) / 2;
cb33740c 8849 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 8850 hi = mid;
cb33740c 8851 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
8852 lo = mid + 1;
8853 else
8854 {
8855 count2 = ssymbuf2[mid].count;
8856 ssymbuf2 += mid;
8857 break;
8858 }
8859 }
3818d4ab
L
8860 if (ignore_section_symbol_p)
8861 {
8862 for (i = 0; i < count2; i++)
8863 if (ELF_ST_TYPE (ssymbuf2->ssym[i].st_info) == STT_SECTION)
8864 sec_count2++;
8865 count2 -= sec_count2;
8866 }
4d269e42
AM
8867
8868 if (count1 == 0 || count2 == 0 || count1 != count2)
8869 goto done;
8870
ca4be51c
AM
8871 symtable1
8872 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8873 symtable2
8874 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
8875 if (symtable1 == NULL || symtable2 == NULL)
8876 goto done;
8877
8878 symp = symtable1;
3818d4ab
L
8879 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1 + sec_count1;
8880 ssym < ssymend; ssym++)
8881 if (sec_count1 == 0
8882 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8883 {
8884 symp->u.ssym = ssym;
8885 symp->name = bfd_elf_string_from_elf_section (bfd1,
8886 hdr1->sh_link,
8887 ssym->st_name);
124deb31
AM
8888 if (symp->name == NULL)
8889 goto done;
3818d4ab
L
8890 symp++;
8891 }
4d269e42
AM
8892
8893 symp = symtable2;
3818d4ab
L
8894 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2 + sec_count2;
8895 ssym < ssymend; ssym++)
8896 if (sec_count2 == 0
8897 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
8898 {
8899 symp->u.ssym = ssym;
8900 symp->name = bfd_elf_string_from_elf_section (bfd2,
8901 hdr2->sh_link,
8902 ssym->st_name);
124deb31
AM
8903 if (symp->name == NULL)
8904 goto done;
3818d4ab
L
8905 symp++;
8906 }
4d269e42
AM
8907
8908 /* Sort symbol by name. */
8909 qsort (symtable1, count1, sizeof (struct elf_symbol),
8910 elf_sym_name_compare);
8911 qsort (symtable2, count1, sizeof (struct elf_symbol),
8912 elf_sym_name_compare);
8913
8914 for (i = 0; i < count1; i++)
8915 /* Two symbols must have the same binding, type and name. */
8916 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8917 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8918 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8919 goto done;
8920
0a1b45a2 8921 result = true;
4d269e42
AM
8922 goto done;
8923 }
8924
a50b1753
NC
8925 symtable1 = (struct elf_symbol *)
8926 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8927 symtable2 = (struct elf_symbol *)
8928 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
8929 if (symtable1 == NULL || symtable2 == NULL)
8930 goto done;
8931
8932 /* Count definitions in the section. */
8933 count1 = 0;
8934 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
3818d4ab
L
8935 if (isym->st_shndx == shndx1
8936 && (!ignore_section_symbol_p
8937 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
4d269e42
AM
8938 symtable1[count1++].u.isym = isym;
8939
8940 count2 = 0;
8941 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
3818d4ab
L
8942 if (isym->st_shndx == shndx2
8943 && (!ignore_section_symbol_p
8944 || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
4d269e42
AM
8945 symtable2[count2++].u.isym = isym;
8946
8947 if (count1 == 0 || count2 == 0 || count1 != count2)
8948 goto done;
8949
8950 for (i = 0; i < count1; i++)
124deb31
AM
8951 {
8952 symtable1[i].name
8953 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8954 symtable1[i].u.isym->st_name);
8955 if (symtable1[i].name == NULL)
8956 goto done;
8957 }
4d269e42
AM
8958
8959 for (i = 0; i < count2; i++)
124deb31
AM
8960 {
8961 symtable2[i].name
8962 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8963 symtable2[i].u.isym->st_name);
8964 if (symtable2[i].name == NULL)
8965 goto done;
8966 }
4d269e42
AM
8967
8968 /* Sort symbol by name. */
8969 qsort (symtable1, count1, sizeof (struct elf_symbol),
8970 elf_sym_name_compare);
8971 qsort (symtable2, count1, sizeof (struct elf_symbol),
8972 elf_sym_name_compare);
8973
8974 for (i = 0; i < count1; i++)
8975 /* Two symbols must have the same binding, type and name. */
8976 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8977 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8978 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8979 goto done;
8980
0a1b45a2 8981 result = true;
4d269e42 8982
dc1e8a47 8983 done:
c9594989
AM
8984 free (symtable1);
8985 free (symtable2);
8986 free (isymbuf1);
8987 free (isymbuf2);
4d269e42
AM
8988
8989 return result;
8990}
8991
8992/* Return TRUE if 2 section types are compatible. */
8993
0a1b45a2 8994bool
4d269e42
AM
8995_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8996 bfd *bbfd, const asection *bsec)
8997{
8998 if (asec == NULL
8999 || bsec == NULL
9000 || abfd->xvec->flavour != bfd_target_elf_flavour
9001 || bbfd->xvec->flavour != bfd_target_elf_flavour)
0a1b45a2 9002 return true;
4d269e42
AM
9003
9004 return elf_section_type (asec) == elf_section_type (bsec);
9005}
9006\f
c152c796
AM
9007/* Final phase of ELF linker. */
9008
9009/* A structure we use to avoid passing large numbers of arguments. */
9010
9011struct elf_final_link_info
9012{
9013 /* General link information. */
9014 struct bfd_link_info *info;
9015 /* Output BFD. */
9016 bfd *output_bfd;
9017 /* Symbol string table. */
ef10c3ac 9018 struct elf_strtab_hash *symstrtab;
c152c796
AM
9019 /* .hash section. */
9020 asection *hash_sec;
9021 /* symbol version section (.gnu.version). */
9022 asection *symver_sec;
9023 /* Buffer large enough to hold contents of any section. */
9024 bfd_byte *contents;
9025 /* Buffer large enough to hold external relocs of any section. */
9026 void *external_relocs;
9027 /* Buffer large enough to hold internal relocs of any section. */
9028 Elf_Internal_Rela *internal_relocs;
9029 /* Buffer large enough to hold external local symbols of any input
9030 BFD. */
9031 bfd_byte *external_syms;
9032 /* And a buffer for symbol section indices. */
9033 Elf_External_Sym_Shndx *locsym_shndx;
9034 /* Buffer large enough to hold internal local symbols of any input
9035 BFD. */
9036 Elf_Internal_Sym *internal_syms;
9037 /* Array large enough to hold a symbol index for each local symbol
9038 of any input BFD. */
9039 long *indices;
9040 /* Array large enough to hold a section pointer for each local
9041 symbol of any input BFD. */
9042 asection **sections;
ef10c3ac 9043 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 9044 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
9045 /* Number of STT_FILE syms seen. */
9046 size_t filesym_count;
496afd17
L
9047 /* Local symbol hash table. */
9048 struct bfd_hash_table local_hash_table;
c152c796
AM
9049};
9050
496afd17
L
9051struct local_hash_entry
9052{
9053 /* Base hash table entry structure. */
9054 struct bfd_hash_entry root;
9055 /* Size of the local symbol name. */
9056 size_t size;
9057 /* Number of the duplicated local symbol names. */
9058 long count;
9059};
9060
9061/* Create an entry in the local symbol hash table. */
9062
9063static struct bfd_hash_entry *
9064local_hash_newfunc (struct bfd_hash_entry *entry,
9065 struct bfd_hash_table *table,
9066 const char *string)
9067{
9068
9069 /* Allocate the structure if it has not already been allocated by a
9070 subclass. */
9071 if (entry == NULL)
9072 {
9073 entry = bfd_hash_allocate (table,
9074 sizeof (struct local_hash_entry));
9075 if (entry == NULL)
9076 return entry;
9077 }
9078
9079 /* Call the allocation method of the superclass. */
9080 entry = bfd_hash_newfunc (entry, table, string);
9081 if (entry != NULL)
9082 {
9083 ((struct local_hash_entry *) entry)->count = 0;
9084 ((struct local_hash_entry *) entry)->size = 0;
9085 }
9086
9087 return entry;
9088}
9089
c152c796
AM
9090/* This struct is used to pass information to elf_link_output_extsym. */
9091
9092struct elf_outext_info
9093{
0a1b45a2
AM
9094 bool failed;
9095 bool localsyms;
9096 bool file_sym_done;
8b127cbc 9097 struct elf_final_link_info *flinfo;
c152c796
AM
9098};
9099
d9352518
DB
9100
9101/* Support for evaluating a complex relocation.
9102
9103 Complex relocations are generalized, self-describing relocations. The
9104 implementation of them consists of two parts: complex symbols, and the
a0c8462f 9105 relocations themselves.
d9352518 9106
4b69ce9b 9107 The relocations use a reserved elf-wide relocation type code (R_RELC
d9352518
DB
9108 external / BFD_RELOC_RELC internal) and an encoding of relocation field
9109 information (start bit, end bit, word width, etc) into the addend. This
9110 information is extracted from CGEN-generated operand tables within gas.
9111
4b69ce9b 9112 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
d9352518
DB
9113 internal) representing prefix-notation expressions, including but not
9114 limited to those sorts of expressions normally encoded as addends in the
9115 addend field. The symbol mangling format is:
9116
9117 <node> := <literal>
07d6d2b8
AM
9118 | <unary-operator> ':' <node>
9119 | <binary-operator> ':' <node> ':' <node>
d9352518
DB
9120 ;
9121
9122 <literal> := 's' <digits=N> ':' <N character symbol name>
07d6d2b8 9123 | 'S' <digits=N> ':' <N character section name>
d9352518
DB
9124 | '#' <hexdigits>
9125 ;
9126
9127 <binary-operator> := as in C
9128 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
9129
9130static void
a0c8462f
AM
9131set_symbol_value (bfd *bfd_with_globals,
9132 Elf_Internal_Sym *isymbuf,
9133 size_t locsymcount,
9134 size_t symidx,
9135 bfd_vma val)
d9352518 9136{
8977835c
AM
9137 struct elf_link_hash_entry *h;
9138 size_t extsymoff = locsymcount;
d9352518 9139
8977835c 9140 if (symidx < locsymcount)
d9352518 9141 {
8977835c
AM
9142 Elf_Internal_Sym *sym;
9143
9144 sym = isymbuf + symidx;
9145 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
9146 {
9147 /* It is a local symbol: move it to the
9148 "absolute" section and give it a value. */
9149 sym->st_shndx = SHN_ABS;
9150 sym->st_value = val;
9151 return;
9152 }
9153 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
9154 extsymoff = 0;
d9352518 9155 }
8977835c
AM
9156
9157 /* It is a global symbol: set its link type
9158 to "defined" and give it a value. */
931494c9
NC
9159 h = get_link_hash_entry (elf_sym_hashes (bfd_with_globals), symidx, extsymoff);
9160 if (h == NULL)
9161 {
9162 /* FIXMEL What should we do ? */
9163 return;
9164 }
8977835c
AM
9165 h->root.type = bfd_link_hash_defined;
9166 h->root.u.def.value = val;
9167 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
9168}
9169
0a1b45a2 9170static bool
a0c8462f
AM
9171resolve_symbol (const char *name,
9172 bfd *input_bfd,
8b127cbc 9173 struct elf_final_link_info *flinfo,
a0c8462f
AM
9174 bfd_vma *result,
9175 Elf_Internal_Sym *isymbuf,
9176 size_t locsymcount)
d9352518 9177{
a0c8462f
AM
9178 Elf_Internal_Sym *sym;
9179 struct bfd_link_hash_entry *global_entry;
9180 const char *candidate = NULL;
9181 Elf_Internal_Shdr *symtab_hdr;
9182 size_t i;
9183
d9352518
DB
9184 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
9185
9186 for (i = 0; i < locsymcount; ++ i)
9187 {
8977835c 9188 sym = isymbuf + i;
d9352518
DB
9189
9190 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
9191 continue;
9192
9193 candidate = bfd_elf_string_from_elf_section (input_bfd,
9194 symtab_hdr->sh_link,
9195 sym->st_name);
9196#ifdef DEBUG
0f02bbd9
AM
9197 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
9198 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
9199#endif
9200 if (candidate && strcmp (candidate, name) == 0)
9201 {
8b127cbc 9202 asection *sec = flinfo->sections [i];
d9352518 9203
0f02bbd9
AM
9204 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
9205 *result += sec->output_offset + sec->output_section->vma;
d9352518 9206#ifdef DEBUG
0f02bbd9
AM
9207 printf ("Found symbol with value %8.8lx\n",
9208 (unsigned long) *result);
d9352518 9209#endif
0a1b45a2 9210 return true;
d9352518
DB
9211 }
9212 }
9213
9214 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 9215 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
0a1b45a2 9216 false, false, true);
d9352518 9217 if (!global_entry)
0a1b45a2 9218 return false;
a0c8462f 9219
d9352518
DB
9220 if (global_entry->type == bfd_link_hash_defined
9221 || global_entry->type == bfd_link_hash_defweak)
9222 {
a0c8462f
AM
9223 *result = (global_entry->u.def.value
9224 + global_entry->u.def.section->output_section->vma
9225 + global_entry->u.def.section->output_offset);
d9352518 9226#ifdef DEBUG
0f02bbd9
AM
9227 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
9228 global_entry->root.string, (unsigned long) *result);
d9352518 9229#endif
0a1b45a2 9230 return true;
a0c8462f 9231 }
d9352518 9232
0a1b45a2 9233 return false;
d9352518
DB
9234}
9235
37b01f6a
DG
9236/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
9237 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
9238 names like "foo.end" which is the end address of section "foo". */
07d6d2b8 9239
0a1b45a2 9240static bool
a0c8462f
AM
9241resolve_section (const char *name,
9242 asection *sections,
37b01f6a
DG
9243 bfd_vma *result,
9244 bfd * abfd)
d9352518 9245{
a0c8462f
AM
9246 asection *curr;
9247 unsigned int len;
d9352518 9248
a0c8462f 9249 for (curr = sections; curr; curr = curr->next)
d9352518
DB
9250 if (strcmp (curr->name, name) == 0)
9251 {
9252 *result = curr->vma;
0a1b45a2 9253 return true;
d9352518
DB
9254 }
9255
9256 /* Hmm. still haven't found it. try pseudo-section names. */
37b01f6a 9257 /* FIXME: This could be coded more efficiently... */
a0c8462f 9258 for (curr = sections; curr; curr = curr->next)
d9352518
DB
9259 {
9260 len = strlen (curr->name);
a0c8462f 9261 if (len > strlen (name))
d9352518
DB
9262 continue;
9263
9264 if (strncmp (curr->name, name, len) == 0)
9265 {
3f3328b8 9266 if (startswith (name + len, ".end"))
d9352518 9267 {
61826503 9268 *result = (curr->vma
bb294208 9269 + curr->size / bfd_octets_per_byte (abfd, curr));
0a1b45a2 9270 return true;
d9352518
DB
9271 }
9272
9273 /* Insert more pseudo-section names here, if you like. */
9274 }
9275 }
a0c8462f 9276
0a1b45a2 9277 return false;
d9352518
DB
9278}
9279
9280static void
a0c8462f 9281undefined_reference (const char *reftype, const char *name)
d9352518 9282{
695344c0 9283 /* xgettext:c-format */
a0c8462f
AM
9284 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
9285 reftype, name);
4b69ce9b 9286 bfd_set_error (bfd_error_bad_value);
d9352518
DB
9287}
9288
0a1b45a2 9289static bool
a0c8462f
AM
9290eval_symbol (bfd_vma *result,
9291 const char **symp,
9292 bfd *input_bfd,
8b127cbc 9293 struct elf_final_link_info *flinfo,
a0c8462f
AM
9294 bfd_vma dot,
9295 Elf_Internal_Sym *isymbuf,
9296 size_t locsymcount,
9297 int signed_p)
d9352518 9298{
4b93929b
NC
9299 size_t len;
9300 size_t symlen;
a0c8462f
AM
9301 bfd_vma a;
9302 bfd_vma b;
4b93929b 9303 char symbuf[4096];
0f02bbd9 9304 const char *sym = *symp;
a0c8462f 9305 const char *symend;
0a1b45a2 9306 bool symbol_is_section = false;
d9352518
DB
9307
9308 len = strlen (sym);
9309 symend = sym + len;
9310
4b93929b 9311 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
9312 {
9313 bfd_set_error (bfd_error_invalid_operation);
0a1b45a2 9314 return false;
d9352518 9315 }
a0c8462f 9316
d9352518
DB
9317 switch (* sym)
9318 {
9319 case '.':
0f02bbd9
AM
9320 *result = dot;
9321 *symp = sym + 1;
0a1b45a2 9322 return true;
d9352518
DB
9323
9324 case '#':
0f02bbd9
AM
9325 ++sym;
9326 *result = strtoul (sym, (char **) symp, 16);
0a1b45a2 9327 return true;
d9352518
DB
9328
9329 case 'S':
0a1b45a2 9330 symbol_is_section = true;
1a0670f3 9331 /* Fall through. */
a0c8462f 9332 case 's':
0f02bbd9
AM
9333 ++sym;
9334 symlen = strtol (sym, (char **) symp, 10);
9335 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 9336
4b93929b 9337 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
9338 {
9339 bfd_set_error (bfd_error_invalid_operation);
0a1b45a2 9340 return false;
d9352518
DB
9341 }
9342
9343 memcpy (symbuf, sym, symlen);
a0c8462f 9344 symbuf[symlen] = '\0';
0f02bbd9 9345 *symp = sym + symlen;
a0c8462f
AM
9346
9347 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
9348 the symbol as a section, or vice-versa. so we're pretty liberal in our
9349 interpretation here; section means "try section first", not "must be a
9350 section", and likewise with symbol. */
9351
a0c8462f 9352 if (symbol_is_section)
d9352518 9353 {
37b01f6a 9354 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8b127cbc 9355 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 9356 isymbuf, locsymcount))
d9352518
DB
9357 {
9358 undefined_reference ("section", symbuf);
0a1b45a2 9359 return false;
d9352518 9360 }
a0c8462f
AM
9361 }
9362 else
d9352518 9363 {
8b127cbc 9364 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 9365 isymbuf, locsymcount)
8b127cbc 9366 && !resolve_section (symbuf, flinfo->output_bfd->sections,
37b01f6a 9367 result, input_bfd))
d9352518
DB
9368 {
9369 undefined_reference ("symbol", symbuf);
0a1b45a2 9370 return false;
d9352518
DB
9371 }
9372 }
9373
0a1b45a2 9374 return true;
a0c8462f 9375
d9352518
DB
9376 /* All that remains are operators. */
9377
9378#define UNARY_OP(op) \
3f3328b8 9379 if (startswith (sym, #op)) \
d9352518
DB
9380 { \
9381 sym += strlen (#op); \
a0c8462f
AM
9382 if (*sym == ':') \
9383 ++sym; \
0f02bbd9 9384 *symp = sym; \
8b127cbc 9385 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 9386 isymbuf, locsymcount, signed_p)) \
0a1b45a2 9387 return false; \
a0c8462f 9388 if (signed_p) \
0f02bbd9 9389 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
9390 else \
9391 *result = op a; \
0a1b45a2 9392 return true; \
d9352518
DB
9393 }
9394
4b69ce9b 9395#define BINARY_OP_HEAD(op) \
3f3328b8 9396 if (startswith (sym, #op)) \
d9352518
DB
9397 { \
9398 sym += strlen (#op); \
a0c8462f
AM
9399 if (*sym == ':') \
9400 ++sym; \
0f02bbd9 9401 *symp = sym; \
8b127cbc 9402 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 9403 isymbuf, locsymcount, signed_p)) \
0a1b45a2 9404 return false; \
0f02bbd9 9405 ++*symp; \
8b127cbc 9406 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 9407 isymbuf, locsymcount, signed_p)) \
0a1b45a2 9408 return false;
4b69ce9b 9409#define BINARY_OP_TAIL(op) \
a0c8462f 9410 if (signed_p) \
0f02bbd9 9411 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
9412 else \
9413 *result = a op b; \
0a1b45a2 9414 return true; \
d9352518 9415 }
4b69ce9b 9416#define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
d9352518
DB
9417
9418 default:
9419 UNARY_OP (0-);
4b69ce9b
AM
9420 BINARY_OP_HEAD (<<);
9421 if (b >= sizeof (a) * CHAR_BIT)
9422 {
9423 *result = 0;
0a1b45a2 9424 return true;
4b69ce9b
AM
9425 }
9426 signed_p = 0;
9427 BINARY_OP_TAIL (<<);
9428 BINARY_OP_HEAD (>>);
9429 if (b >= sizeof (a) * CHAR_BIT)
9430 {
9431 *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
0a1b45a2 9432 return true;
4b69ce9b
AM
9433 }
9434 BINARY_OP_TAIL (>>);
d9352518
DB
9435 BINARY_OP (==);
9436 BINARY_OP (!=);
9437 BINARY_OP (<=);
9438 BINARY_OP (>=);
9439 BINARY_OP (&&);
9440 BINARY_OP (||);
9441 UNARY_OP (~);
9442 UNARY_OP (!);
9443 BINARY_OP (*);
4b69ce9b
AM
9444 BINARY_OP_HEAD (/);
9445 if (b == 0)
9446 {
9447 _bfd_error_handler (_("division by zero"));
9448 bfd_set_error (bfd_error_bad_value);
0a1b45a2 9449 return false;
4b69ce9b
AM
9450 }
9451 BINARY_OP_TAIL (/);
9452 BINARY_OP_HEAD (%);
9453 if (b == 0)
9454 {
9455 _bfd_error_handler (_("division by zero"));
9456 bfd_set_error (bfd_error_bad_value);
0a1b45a2 9457 return false;
4b69ce9b
AM
9458 }
9459 BINARY_OP_TAIL (%);
d9352518
DB
9460 BINARY_OP (^);
9461 BINARY_OP (|);
9462 BINARY_OP (&);
9463 BINARY_OP (+);
9464 BINARY_OP (-);
9465 BINARY_OP (<);
9466 BINARY_OP (>);
9467#undef UNARY_OP
9468#undef BINARY_OP
9469 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
9470 bfd_set_error (bfd_error_invalid_operation);
0a1b45a2 9471 return false;
d9352518
DB
9472 }
9473}
9474
d9352518 9475static void
a0c8462f
AM
9476put_value (bfd_vma size,
9477 unsigned long chunksz,
9478 bfd *input_bfd,
9479 bfd_vma x,
9480 bfd_byte *location)
d9352518
DB
9481{
9482 location += (size - chunksz);
9483
41cd1ad1 9484 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
9485 {
9486 switch (chunksz)
9487 {
d9352518
DB
9488 case 1:
9489 bfd_put_8 (input_bfd, x, location);
41cd1ad1 9490 x >>= 8;
d9352518
DB
9491 break;
9492 case 2:
9493 bfd_put_16 (input_bfd, x, location);
41cd1ad1 9494 x >>= 16;
d9352518
DB
9495 break;
9496 case 4:
9497 bfd_put_32 (input_bfd, x, location);
65164438
NC
9498 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
9499 x >>= 16;
9500 x >>= 16;
d9352518 9501 break;
d9352518 9502#ifdef BFD64
41cd1ad1 9503 case 8:
d9352518 9504 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
9505 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
9506 x >>= 32;
9507 x >>= 32;
9508 break;
d9352518 9509#endif
41cd1ad1
NC
9510 default:
9511 abort ();
d9352518
DB
9512 break;
9513 }
9514 }
9515}
9516
a0c8462f
AM
9517static bfd_vma
9518get_value (bfd_vma size,
9519 unsigned long chunksz,
9520 bfd *input_bfd,
9521 bfd_byte *location)
d9352518 9522{
9b239e0e 9523 int shift;
d9352518
DB
9524 bfd_vma x = 0;
9525
9b239e0e
NC
9526 /* Sanity checks. */
9527 BFD_ASSERT (chunksz <= sizeof (x)
9528 && size >= chunksz
9529 && chunksz != 0
9530 && (size % chunksz) == 0
9531 && input_bfd != NULL
9532 && location != NULL);
9533
9534 if (chunksz == sizeof (x))
9535 {
9536 BFD_ASSERT (size == chunksz);
9537
9538 /* Make sure that we do not perform an undefined shift operation.
9539 We know that size == chunksz so there will only be one iteration
9540 of the loop below. */
9541 shift = 0;
9542 }
9543 else
9544 shift = 8 * chunksz;
9545
a0c8462f 9546 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
9547 {
9548 switch (chunksz)
9549 {
d9352518 9550 case 1:
9b239e0e 9551 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
9552 break;
9553 case 2:
9b239e0e 9554 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
9555 break;
9556 case 4:
9b239e0e 9557 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 9558 break;
d9352518 9559#ifdef BFD64
9b239e0e
NC
9560 case 8:
9561 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 9562 break;
9b239e0e
NC
9563#endif
9564 default:
9565 abort ();
d9352518
DB
9566 }
9567 }
9568 return x;
9569}
9570
a0c8462f
AM
9571static void
9572decode_complex_addend (unsigned long *start, /* in bits */
9573 unsigned long *oplen, /* in bits */
9574 unsigned long *len, /* in bits */
9575 unsigned long *wordsz, /* in bytes */
9576 unsigned long *chunksz, /* in bytes */
9577 unsigned long *lsb0_p,
9578 unsigned long *signed_p,
9579 unsigned long *trunc_p,
9580 unsigned long encoded)
d9352518 9581{
07d6d2b8
AM
9582 * start = encoded & 0x3F;
9583 * len = (encoded >> 6) & 0x3F;
d9352518
DB
9584 * oplen = (encoded >> 12) & 0x3F;
9585 * wordsz = (encoded >> 18) & 0xF;
9586 * chunksz = (encoded >> 22) & 0xF;
9587 * lsb0_p = (encoded >> 27) & 1;
9588 * signed_p = (encoded >> 28) & 1;
9589 * trunc_p = (encoded >> 29) & 1;
9590}
9591
cdfeee4f 9592bfd_reloc_status_type
0f02bbd9 9593bfd_elf_perform_complex_relocation (bfd *input_bfd,
bb294208 9594 asection *input_section,
0f02bbd9
AM
9595 bfd_byte *contents,
9596 Elf_Internal_Rela *rel,
9597 bfd_vma relocation)
d9352518 9598{
0f02bbd9
AM
9599 bfd_vma shift, x, mask;
9600 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 9601 bfd_reloc_status_type r;
bb294208 9602 bfd_size_type octets;
d9352518
DB
9603
9604 /* Perform this reloc, since it is complex.
9605 (this is not to say that it necessarily refers to a complex
9606 symbol; merely that it is a self-describing CGEN based reloc.
9607 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 9608 word size, etc) encoded within it.). */
d9352518 9609
a0c8462f
AM
9610 decode_complex_addend (&start, &oplen, &len, &wordsz,
9611 &chunksz, &lsb0_p, &signed_p,
9612 &trunc_p, rel->r_addend);
d9352518
DB
9613
9614 mask = (((1L << (len - 1)) - 1) << 1) | 1;
9615
9616 if (lsb0_p)
9617 shift = (start + 1) - len;
9618 else
9619 shift = (8 * wordsz) - (start + len);
9620
bb294208
AM
9621 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
9622 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
d9352518
DB
9623
9624#ifdef DEBUG
9625 printf ("Doing complex reloc: "
9626 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9627 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9628 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9629 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
9630 oplen, (unsigned long) x, (unsigned long) mask,
9631 (unsigned long) relocation);
d9352518
DB
9632#endif
9633
cdfeee4f 9634 r = bfd_reloc_ok;
d9352518 9635 if (! trunc_p)
cdfeee4f
AM
9636 /* Now do an overflow check. */
9637 r = bfd_check_overflow ((signed_p
9638 ? complain_overflow_signed
9639 : complain_overflow_unsigned),
9640 len, 0, (8 * wordsz),
9641 relocation);
a0c8462f 9642
d9352518
DB
9643 /* Do the deed. */
9644 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9645
9646#ifdef DEBUG
9647 printf (" relocation: %8.8lx\n"
9648 " shifted mask: %8.8lx\n"
9649 " shifted/masked reloc: %8.8lx\n"
9650 " result: %8.8lx\n",
9ccb8af9
AM
9651 (unsigned long) relocation, (unsigned long) (mask << shift),
9652 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 9653#endif
bb294208 9654 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
cdfeee4f 9655 return r;
d9352518
DB
9656}
9657
0e287786
AM
9658/* Functions to read r_offset from external (target order) reloc
9659 entry. Faster than bfd_getl32 et al, because we let the compiler
9660 know the value is aligned. */
53df40a4 9661
0e287786
AM
9662static bfd_vma
9663ext32l_r_offset (const void *p)
53df40a4
AM
9664{
9665 union aligned32
9666 {
9667 uint32_t v;
9668 unsigned char c[4];
9669 };
9670 const union aligned32 *a
0e287786 9671 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
9672
9673 uint32_t aval = ( (uint32_t) a->c[0]
9674 | (uint32_t) a->c[1] << 8
9675 | (uint32_t) a->c[2] << 16
9676 | (uint32_t) a->c[3] << 24);
0e287786 9677 return aval;
53df40a4
AM
9678}
9679
0e287786
AM
9680static bfd_vma
9681ext32b_r_offset (const void *p)
53df40a4
AM
9682{
9683 union aligned32
9684 {
9685 uint32_t v;
9686 unsigned char c[4];
9687 };
9688 const union aligned32 *a
0e287786 9689 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
9690
9691 uint32_t aval = ( (uint32_t) a->c[0] << 24
9692 | (uint32_t) a->c[1] << 16
9693 | (uint32_t) a->c[2] << 8
9694 | (uint32_t) a->c[3]);
0e287786 9695 return aval;
53df40a4
AM
9696}
9697
0e287786
AM
9698static bfd_vma
9699ext64l_r_offset (const void *p)
53df40a4
AM
9700{
9701 union aligned64
9702 {
9703 uint64_t v;
9704 unsigned char c[8];
9705 };
9706 const union aligned64 *a
0e287786 9707 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
9708
9709 uint64_t aval = ( (uint64_t) a->c[0]
9710 | (uint64_t) a->c[1] << 8
9711 | (uint64_t) a->c[2] << 16
9712 | (uint64_t) a->c[3] << 24
9713 | (uint64_t) a->c[4] << 32
9714 | (uint64_t) a->c[5] << 40
9715 | (uint64_t) a->c[6] << 48
9716 | (uint64_t) a->c[7] << 56);
0e287786 9717 return aval;
53df40a4
AM
9718}
9719
0e287786
AM
9720static bfd_vma
9721ext64b_r_offset (const void *p)
53df40a4
AM
9722{
9723 union aligned64
9724 {
9725 uint64_t v;
9726 unsigned char c[8];
9727 };
9728 const union aligned64 *a
0e287786 9729 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
9730
9731 uint64_t aval = ( (uint64_t) a->c[0] << 56
9732 | (uint64_t) a->c[1] << 48
9733 | (uint64_t) a->c[2] << 40
9734 | (uint64_t) a->c[3] << 32
9735 | (uint64_t) a->c[4] << 24
9736 | (uint64_t) a->c[5] << 16
9737 | (uint64_t) a->c[6] << 8
9738 | (uint64_t) a->c[7]);
0e287786 9739 return aval;
53df40a4 9740}
53df40a4 9741
c152c796
AM
9742/* When performing a relocatable link, the input relocations are
9743 preserved. But, if they reference global symbols, the indices
d4730f92
BS
9744 referenced must be updated. Update all the relocations found in
9745 RELDATA. */
c152c796 9746
0a1b45a2 9747static bool
c152c796 9748elf_link_adjust_relocs (bfd *abfd,
9eaff861 9749 asection *sec,
28dbcedc 9750 struct bfd_elf_section_reloc_data *reldata,
0a1b45a2 9751 bool sort,
10bbbc1d 9752 struct bfd_link_info *info)
c152c796
AM
9753{
9754 unsigned int i;
9755 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9756 bfd_byte *erela;
9757 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9758 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9759 bfd_vma r_type_mask;
9760 int r_sym_shift;
d4730f92
BS
9761 unsigned int count = reldata->count;
9762 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 9763
d4730f92 9764 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
9765 {
9766 swap_in = bed->s->swap_reloc_in;
9767 swap_out = bed->s->swap_reloc_out;
9768 }
d4730f92 9769 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
9770 {
9771 swap_in = bed->s->swap_reloca_in;
9772 swap_out = bed->s->swap_reloca_out;
9773 }
9774 else
9775 abort ();
9776
9777 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9778 abort ();
9779
9780 if (bed->s->arch_size == 32)
9781 {
9782 r_type_mask = 0xff;
9783 r_sym_shift = 8;
9784 }
9785 else
9786 {
9787 r_type_mask = 0xffffffff;
9788 r_sym_shift = 32;
9789 }
9790
d4730f92
BS
9791 erela = reldata->hdr->contents;
9792 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
9793 {
9794 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9795 unsigned int j;
9796
9797 if (*rel_hash == NULL)
9798 continue;
9799
10bbbc1d
NC
9800 if ((*rel_hash)->indx == -2
9801 && info->gc_sections
9802 && ! info->gc_keep_exported)
9803 {
9804 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9793eb77 9805 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
10bbbc1d
NC
9806 abfd, sec,
9807 (*rel_hash)->root.root.string);
9793eb77 9808 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
d42c267e 9809 abfd, sec);
10bbbc1d 9810 bfd_set_error (bfd_error_invalid_operation);
0a1b45a2 9811 return false;
10bbbc1d 9812 }
c152c796
AM
9813 BFD_ASSERT ((*rel_hash)->indx >= 0);
9814
9815 (*swap_in) (abfd, erela, irela);
9816 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9817 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9818 | (irela[j].r_info & r_type_mask));
9819 (*swap_out) (abfd, irela, erela);
9820 }
53df40a4 9821
9eaff861
AO
9822 if (bed->elf_backend_update_relocs)
9823 (*bed->elf_backend_update_relocs) (sec, reldata);
9824
0e287786 9825 if (sort && count != 0)
53df40a4 9826 {
0e287786
AM
9827 bfd_vma (*ext_r_off) (const void *);
9828 bfd_vma r_off;
9829 size_t elt_size;
9830 bfd_byte *base, *end, *p, *loc;
bca6d0e3 9831 bfd_byte *buf = NULL;
28dbcedc
AM
9832
9833 if (bed->s->arch_size == 32)
9834 {
9835 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 9836 ext_r_off = ext32l_r_offset;
28dbcedc 9837 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 9838 ext_r_off = ext32b_r_offset;
28dbcedc
AM
9839 else
9840 abort ();
9841 }
53df40a4 9842 else
28dbcedc 9843 {
28dbcedc 9844 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 9845 ext_r_off = ext64l_r_offset;
28dbcedc 9846 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 9847 ext_r_off = ext64b_r_offset;
28dbcedc 9848 else
28dbcedc
AM
9849 abort ();
9850 }
0e287786 9851
bca6d0e3
AM
9852 /* Must use a stable sort here. A modified insertion sort,
9853 since the relocs are mostly sorted already. */
0e287786
AM
9854 elt_size = reldata->hdr->sh_entsize;
9855 base = reldata->hdr->contents;
9856 end = base + count * elt_size;
bca6d0e3 9857 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
9858 abort ();
9859
9860 /* Ensure the first element is lowest. This acts as a sentinel,
9861 speeding the main loop below. */
9862 r_off = (*ext_r_off) (base);
9863 for (p = loc = base; (p += elt_size) < end; )
9864 {
9865 bfd_vma r_off2 = (*ext_r_off) (p);
9866 if (r_off > r_off2)
9867 {
9868 r_off = r_off2;
9869 loc = p;
9870 }
9871 }
9872 if (loc != base)
9873 {
9874 /* Don't just swap *base and *loc as that changes the order
9875 of the original base[0] and base[1] if they happen to
9876 have the same r_offset. */
bca6d0e3
AM
9877 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9878 memcpy (onebuf, loc, elt_size);
0e287786 9879 memmove (base + elt_size, base, loc - base);
bca6d0e3 9880 memcpy (base, onebuf, elt_size);
0e287786
AM
9881 }
9882
b29b8669 9883 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
9884 {
9885 /* base to p is sorted, *p is next to insert. */
9886 r_off = (*ext_r_off) (p);
9887 /* Search the sorted region for location to insert. */
9888 loc = p - elt_size;
9889 while (r_off < (*ext_r_off) (loc))
9890 loc -= elt_size;
9891 loc += elt_size;
9892 if (loc != p)
9893 {
bca6d0e3
AM
9894 /* Chances are there is a run of relocs to insert here,
9895 from one of more input files. Files are not always
9896 linked in order due to the way elf_link_input_bfd is
9897 called. See pr17666. */
9898 size_t sortlen = p - loc;
9899 bfd_vma r_off2 = (*ext_r_off) (loc);
9900 size_t runlen = elt_size;
fba1ac87
TK
9901 bfd_vma r_off_runend = r_off;
9902 bfd_vma r_off_runend_next;
bca6d0e3
AM
9903 size_t buf_size = 96 * 1024;
9904 while (p + runlen < end
9905 && (sortlen <= buf_size
9906 || runlen + elt_size <= buf_size)
fba1ac87
TK
9907 /* run must not break the ordering of base..loc+1 */
9908 && r_off2 > (r_off_runend_next = (*ext_r_off) (p + runlen))
9909 /* run must be already sorted */
9910 && r_off_runend_next >= r_off_runend)
9911 {
9912 runlen += elt_size;
9913 r_off_runend = r_off_runend_next;
9914 }
bca6d0e3
AM
9915 if (buf == NULL)
9916 {
9917 buf = bfd_malloc (buf_size);
9918 if (buf == NULL)
0a1b45a2 9919 return false;
bca6d0e3
AM
9920 }
9921 if (runlen < sortlen)
9922 {
9923 memcpy (buf, p, runlen);
9924 memmove (loc + runlen, loc, sortlen);
9925 memcpy (loc, buf, runlen);
9926 }
9927 else
9928 {
9929 memcpy (buf, loc, sortlen);
9930 memmove (loc, p, runlen);
9931 memcpy (loc + runlen, buf, sortlen);
9932 }
b29b8669 9933 p += runlen - elt_size;
0e287786
AM
9934 }
9935 }
9936 /* Hashes are no longer valid. */
28dbcedc
AM
9937 free (reldata->hashes);
9938 reldata->hashes = NULL;
bca6d0e3 9939 free (buf);
53df40a4 9940 }
0a1b45a2 9941 return true;
c152c796
AM
9942}
9943
9944struct elf_link_sort_rela
9945{
9946 union {
9947 bfd_vma offset;
9948 bfd_vma sym_mask;
9949 } u;
9950 enum elf_reloc_type_class type;
9951 /* We use this as an array of size int_rels_per_ext_rel. */
9952 Elf_Internal_Rela rela[1];
9953};
9954
dcea6a95
AM
9955/* qsort stability here and for cmp2 is only an issue if multiple
9956 dynamic relocations are emitted at the same address. But targets
9957 that apply a series of dynamic relocations each operating on the
9958 result of the prior relocation can't use -z combreloc as
9959 implemented anyway. Such schemes tend to be broken by sorting on
9960 symbol index. That leaves dynamic NONE relocs as the only other
9961 case where ld might emit multiple relocs at the same address, and
9962 those are only emitted due to target bugs. */
9963
c152c796
AM
9964static int
9965elf_link_sort_cmp1 (const void *A, const void *B)
9966{
a50b1753
NC
9967 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9968 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
9969 int relativea, relativeb;
9970
9971 relativea = a->type == reloc_class_relative;
9972 relativeb = b->type == reloc_class_relative;
9973
9974 if (relativea < relativeb)
9975 return 1;
9976 if (relativea > relativeb)
9977 return -1;
9978 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9979 return -1;
9980 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9981 return 1;
9982 if (a->rela->r_offset < b->rela->r_offset)
9983 return -1;
9984 if (a->rela->r_offset > b->rela->r_offset)
9985 return 1;
9986 return 0;
9987}
9988
9989static int
9990elf_link_sort_cmp2 (const void *A, const void *B)
9991{
a50b1753
NC
9992 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9993 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 9994
7e612e98 9995 if (a->type < b->type)
c152c796 9996 return -1;
7e612e98 9997 if (a->type > b->type)
c152c796 9998 return 1;
7e612e98 9999 if (a->u.offset < b->u.offset)
c152c796 10000 return -1;
7e612e98 10001 if (a->u.offset > b->u.offset)
c152c796
AM
10002 return 1;
10003 if (a->rela->r_offset < b->rela->r_offset)
10004 return -1;
10005 if (a->rela->r_offset > b->rela->r_offset)
10006 return 1;
10007 return 0;
10008}
10009
10010static size_t
10011elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
10012{
3410fea8 10013 asection *dynamic_relocs;
fc66a176
L
10014 asection *rela_dyn;
10015 asection *rel_dyn;
c152c796
AM
10016 bfd_size_type count, size;
10017 size_t i, ret, sort_elt, ext_size;
10018 bfd_byte *sort, *s_non_relative, *p;
10019 struct elf_link_sort_rela *sq;
10020 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10021 int i2e = bed->s->int_rels_per_ext_rel;
61826503 10022 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
c152c796
AM
10023 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
10024 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
10025 struct bfd_link_order *lo;
10026 bfd_vma r_sym_mask;
0a1b45a2 10027 bool use_rela;
c152c796 10028
3410fea8
NC
10029 /* Find a dynamic reloc section. */
10030 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
10031 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
10032 if (rela_dyn != NULL && rela_dyn->size > 0
10033 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 10034 {
0a1b45a2 10035 bool use_rela_initialised = false;
3410fea8
NC
10036
10037 /* This is just here to stop gcc from complaining.
c8e44c6d 10038 Its initialization checking code is not perfect. */
0a1b45a2 10039 use_rela = true;
3410fea8
NC
10040
10041 /* Both sections are present. Examine the sizes
10042 of the indirect sections to help us choose. */
10043 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
10044 if (lo->type == bfd_indirect_link_order)
10045 {
10046 asection *o = lo->u.indirect.section;
10047
10048 if ((o->size % bed->s->sizeof_rela) == 0)
10049 {
10050 if ((o->size % bed->s->sizeof_rel) == 0)
10051 /* Section size is divisible by both rel and rela sizes.
10052 It is of no help to us. */
10053 ;
10054 else
10055 {
10056 /* Section size is only divisible by rela. */
535b785f 10057 if (use_rela_initialised && !use_rela)
3410fea8 10058 {
9793eb77 10059 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
10060 "they are in more than one size"),
10061 abfd);
3410fea8
NC
10062 bfd_set_error (bfd_error_invalid_operation);
10063 return 0;
10064 }
10065 else
10066 {
0a1b45a2
AM
10067 use_rela = true;
10068 use_rela_initialised = true;
3410fea8
NC
10069 }
10070 }
10071 }
10072 else if ((o->size % bed->s->sizeof_rel) == 0)
10073 {
10074 /* Section size is only divisible by rel. */
535b785f 10075 if (use_rela_initialised && use_rela)
3410fea8 10076 {
9793eb77 10077 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
10078 "they are in more than one size"),
10079 abfd);
3410fea8
NC
10080 bfd_set_error (bfd_error_invalid_operation);
10081 return 0;
10082 }
10083 else
10084 {
0a1b45a2
AM
10085 use_rela = false;
10086 use_rela_initialised = true;
3410fea8
NC
10087 }
10088 }
10089 else
10090 {
c8e44c6d
AM
10091 /* The section size is not divisible by either -
10092 something is wrong. */
9793eb77 10093 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 10094 "they are of an unknown size"), abfd);
3410fea8
NC
10095 bfd_set_error (bfd_error_invalid_operation);
10096 return 0;
10097 }
10098 }
10099
10100 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
10101 if (lo->type == bfd_indirect_link_order)
10102 {
10103 asection *o = lo->u.indirect.section;
10104
10105 if ((o->size % bed->s->sizeof_rela) == 0)
10106 {
10107 if ((o->size % bed->s->sizeof_rel) == 0)
10108 /* Section size is divisible by both rel and rela sizes.
10109 It is of no help to us. */
10110 ;
10111 else
10112 {
10113 /* Section size is only divisible by rela. */
535b785f 10114 if (use_rela_initialised && !use_rela)
3410fea8 10115 {
9793eb77 10116 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
10117 "they are in more than one size"),
10118 abfd);
3410fea8
NC
10119 bfd_set_error (bfd_error_invalid_operation);
10120 return 0;
10121 }
10122 else
10123 {
0a1b45a2
AM
10124 use_rela = true;
10125 use_rela_initialised = true;
3410fea8
NC
10126 }
10127 }
10128 }
10129 else if ((o->size % bed->s->sizeof_rel) == 0)
10130 {
10131 /* Section size is only divisible by rel. */
535b785f 10132 if (use_rela_initialised && use_rela)
3410fea8 10133 {
9793eb77 10134 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
10135 "they are in more than one size"),
10136 abfd);
3410fea8
NC
10137 bfd_set_error (bfd_error_invalid_operation);
10138 return 0;
10139 }
10140 else
10141 {
0a1b45a2
AM
10142 use_rela = false;
10143 use_rela_initialised = true;
3410fea8
NC
10144 }
10145 }
10146 else
10147 {
c8e44c6d
AM
10148 /* The section size is not divisible by either -
10149 something is wrong. */
9793eb77 10150 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 10151 "they are of an unknown size"), abfd);
3410fea8
NC
10152 bfd_set_error (bfd_error_invalid_operation);
10153 return 0;
10154 }
10155 }
10156
10157 if (! use_rela_initialised)
10158 /* Make a guess. */
0a1b45a2 10159 use_rela = true;
c152c796 10160 }
fc66a176 10161 else if (rela_dyn != NULL && rela_dyn->size > 0)
0a1b45a2 10162 use_rela = true;
fc66a176 10163 else if (rel_dyn != NULL && rel_dyn->size > 0)
0a1b45a2 10164 use_rela = false;
c152c796 10165 else
fc66a176 10166 return 0;
3410fea8
NC
10167
10168 if (use_rela)
c152c796 10169 {
3410fea8 10170 dynamic_relocs = rela_dyn;
c152c796
AM
10171 ext_size = bed->s->sizeof_rela;
10172 swap_in = bed->s->swap_reloca_in;
10173 swap_out = bed->s->swap_reloca_out;
10174 }
3410fea8
NC
10175 else
10176 {
10177 dynamic_relocs = rel_dyn;
10178 ext_size = bed->s->sizeof_rel;
10179 swap_in = bed->s->swap_reloc_in;
10180 swap_out = bed->s->swap_reloc_out;
10181 }
c152c796
AM
10182
10183 size = 0;
3410fea8 10184 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 10185 if (lo->type == bfd_indirect_link_order)
3410fea8 10186 size += lo->u.indirect.section->size;
c152c796 10187
3410fea8 10188 if (size != dynamic_relocs->size)
c152c796
AM
10189 return 0;
10190
10191 sort_elt = (sizeof (struct elf_link_sort_rela)
10192 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
10193
10194 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
10195 if (count == 0)
10196 return 0;
a50b1753 10197 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 10198
c152c796
AM
10199 if (sort == NULL)
10200 {
10201 (*info->callbacks->warning)
9793eb77 10202 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
c152c796
AM
10203 return 0;
10204 }
10205
10206 if (bed->s->arch_size == 32)
10207 r_sym_mask = ~(bfd_vma) 0xff;
10208 else
10209 r_sym_mask = ~(bfd_vma) 0xffffffff;
10210
3410fea8 10211 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
10212 if (lo->type == bfd_indirect_link_order)
10213 {
10214 bfd_byte *erel, *erelend;
10215 asection *o = lo->u.indirect.section;
10216
1da212d6
AM
10217 if (o->contents == NULL && o->size != 0)
10218 {
10219 /* This is a reloc section that is being handled as a normal
10220 section. See bfd_section_from_shdr. We can't combine
10221 relocs in this case. */
10222 free (sort);
10223 return 0;
10224 }
c152c796 10225 erel = o->contents;
eea6121a 10226 erelend = o->contents + o->size;
c8e44c6d 10227 p = sort + o->output_offset * opb / ext_size * sort_elt;
3410fea8 10228
c152c796
AM
10229 while (erel < erelend)
10230 {
10231 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 10232
c152c796 10233 (*swap_in) (abfd, erel, s->rela);
7e612e98 10234 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
10235 s->u.sym_mask = r_sym_mask;
10236 p += sort_elt;
10237 erel += ext_size;
10238 }
10239 }
10240
10241 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
10242
10243 for (i = 0, p = sort; i < count; i++, p += sort_elt)
10244 {
10245 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10246 if (s->type != reloc_class_relative)
10247 break;
10248 }
10249 ret = i;
10250 s_non_relative = p;
10251
10252 sq = (struct elf_link_sort_rela *) s_non_relative;
10253 for (; i < count; i++, p += sort_elt)
10254 {
10255 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
10256 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
10257 sq = sp;
10258 sp->u.offset = sq->rela->r_offset;
10259 }
10260
10261 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
10262
c8e44c6d
AM
10263 struct elf_link_hash_table *htab = elf_hash_table (info);
10264 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
10265 {
10266 /* We have plt relocs in .rela.dyn. */
10267 sq = (struct elf_link_sort_rela *) sort;
10268 for (i = 0; i < count; i++)
10269 if (sq[count - i - 1].type != reloc_class_plt)
10270 break;
10271 if (i != 0 && htab->srelplt->size == i * ext_size)
10272 {
10273 struct bfd_link_order **plo;
10274 /* Put srelplt link_order last. This is so the output_offset
10275 set in the next loop is correct for DT_JMPREL. */
10276 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
10277 if ((*plo)->type == bfd_indirect_link_order
10278 && (*plo)->u.indirect.section == htab->srelplt)
10279 {
10280 lo = *plo;
10281 *plo = lo->next;
10282 }
10283 else
10284 plo = &(*plo)->next;
10285 *plo = lo;
10286 lo->next = NULL;
10287 dynamic_relocs->map_tail.link_order = lo;
10288 }
10289 }
10290
10291 p = sort;
3410fea8 10292 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
10293 if (lo->type == bfd_indirect_link_order)
10294 {
10295 bfd_byte *erel, *erelend;
10296 asection *o = lo->u.indirect.section;
10297
10298 erel = o->contents;
eea6121a 10299 erelend = o->contents + o->size;
c8e44c6d 10300 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
c152c796
AM
10301 while (erel < erelend)
10302 {
10303 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
10304 (*swap_out) (abfd, s->rela, erel);
10305 p += sort_elt;
10306 erel += ext_size;
10307 }
10308 }
10309
10310 free (sort);
3410fea8 10311 *psec = dynamic_relocs;
c152c796
AM
10312 return ret;
10313}
10314
ef10c3ac 10315/* Add a symbol to the output symbol string table. */
c152c796 10316
6e0b88f1 10317static int
37bb890f 10318elf_link_output_symstrtab (void *finf,
ef10c3ac
L
10319 const char *name,
10320 Elf_Internal_Sym *elfsym,
10321 asection *input_sec,
10322 struct elf_link_hash_entry *h)
c152c796 10323{
37bb890f 10324 struct elf_final_link_info *flinfo = finf;
6e0b88f1 10325 int (*output_symbol_hook)
c152c796
AM
10326 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
10327 struct elf_link_hash_entry *);
ef10c3ac 10328 struct elf_link_hash_table *hash_table;
c152c796 10329 const struct elf_backend_data *bed;
ef10c3ac 10330 bfd_size_type strtabsize;
c152c796 10331
8539e4e8
AM
10332 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10333
8b127cbc 10334 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
10335 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
10336 if (output_symbol_hook != NULL)
10337 {
8b127cbc 10338 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
10339 if (ret != 1)
10340 return ret;
c152c796
AM
10341 }
10342
06f44071
AM
10343 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
10344 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
10345 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
10346 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
10347
e9a20a41 10348 if (name == NULL || *name == '\0')
ef10c3ac 10349 elfsym->st_name = (unsigned long) -1;
c152c796
AM
10350 else
10351 {
ef10c3ac
L
10352 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
10353 to get the final offset for st_name. */
3f2e9699 10354 char *versioned_name = (char *) name;
496afd17
L
10355 if (h != NULL)
10356 {
10357 if (h->versioned == versioned && h->def_dynamic)
10358 {
10359 /* Keep only one '@' for versioned symbols defined in
10360 shared objects. */
10361 char *version = strrchr (name, ELF_VER_CHR);
10362 char *base_end = strchr (name, ELF_VER_CHR);
10363 if (version != base_end)
10364 {
10365 size_t base_len;
10366 size_t len = strlen (name);
10367 versioned_name = bfd_alloc (flinfo->output_bfd, len);
10368 if (versioned_name == NULL)
10369 return 0;
10370 base_len = base_end - name;
10371 memcpy (versioned_name, name, base_len);
10372 memcpy (versioned_name + base_len, version,
10373 len - base_len);
10374 }
10375 }
10376 }
10377 else if (flinfo->info->unique_symbol
10378 && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
3f2e9699 10379 {
496afd17 10380 struct local_hash_entry *lh;
4467df35
L
10381 size_t count_len;
10382 size_t base_len;
10383 char buf[30];
496afd17 10384 switch (ELF_ST_TYPE (elfsym->st_info))
3f2e9699 10385 {
496afd17
L
10386 case STT_FILE:
10387 case STT_SECTION:
10388 break;
10389 default:
10390 lh = (struct local_hash_entry *) bfd_hash_lookup
0a1b45a2 10391 (&flinfo->local_hash_table, name, true, false);
496afd17 10392 if (lh == NULL)
3f2e9699 10393 return 0;
4467df35
L
10394 /* Always append ".COUNT" to local symbols to avoid
10395 potential conflicts with local symbol "XXX.COUNT". */
10396 sprintf (buf, "%lx", lh->count);
10397 base_len = lh->size;
10398 if (!base_len)
496afd17 10399 {
4467df35
L
10400 base_len = strlen (name);
10401 lh->size = base_len;
496afd17 10402 }
4467df35
L
10403 count_len = strlen (buf);
10404 versioned_name = bfd_alloc (flinfo->output_bfd,
10405 base_len + count_len + 2);
10406 if (versioned_name == NULL)
10407 return 0;
10408 memcpy (versioned_name, name, base_len);
10409 versioned_name[base_len] = '.';
10410 memcpy (versioned_name + base_len + 1, buf,
10411 count_len + 1);
496afd17
L
10412 lh->count++;
10413 break;
3f2e9699
L
10414 }
10415 }
ef10c3ac
L
10416 elfsym->st_name
10417 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
0a1b45a2 10418 versioned_name, false);
c152c796 10419 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 10420 return 0;
c152c796
AM
10421 }
10422
ef10c3ac
L
10423 hash_table = elf_hash_table (flinfo->info);
10424 strtabsize = hash_table->strtabsize;
b03b65e2 10425 if (strtabsize <= flinfo->output_bfd->symcount)
c152c796 10426 {
ef10c3ac
L
10427 strtabsize += strtabsize;
10428 hash_table->strtabsize = strtabsize;
10429 strtabsize *= sizeof (*hash_table->strtab);
10430 hash_table->strtab
10431 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
10432 strtabsize);
10433 if (hash_table->strtab == NULL)
6e0b88f1 10434 return 0;
c152c796 10435 }
b03b65e2
AM
10436 hash_table->strtab[flinfo->output_bfd->symcount].sym = *elfsym;
10437 hash_table->strtab[flinfo->output_bfd->symcount].dest_index
10438 = flinfo->output_bfd->symcount;
ed48ec2e 10439 flinfo->output_bfd->symcount += 1;
ef10c3ac
L
10440
10441 return 1;
10442}
10443
10444/* Swap symbols out to the symbol table and flush the output symbols to
10445 the file. */
10446
0a1b45a2 10447static bool
ef10c3ac
L
10448elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
10449{
10450 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
986f0783 10451 size_t amt;
ef53be89 10452 size_t i;
ef10c3ac
L
10453 const struct elf_backend_data *bed;
10454 bfd_byte *symbuf;
10455 Elf_Internal_Shdr *hdr;
10456 file_ptr pos;
0a1b45a2 10457 bool ret;
ef10c3ac 10458
b03b65e2 10459 if (flinfo->output_bfd->symcount == 0)
0a1b45a2 10460 return true;
ef10c3ac
L
10461
10462 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
10463
10464 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 10465
b03b65e2 10466 amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
ef10c3ac
L
10467 symbuf = (bfd_byte *) bfd_malloc (amt);
10468 if (symbuf == NULL)
0a1b45a2 10469 return false;
1b786873 10470
ef10c3ac 10471 if (flinfo->symshndxbuf)
c152c796 10472 {
ef53be89
AM
10473 amt = sizeof (Elf_External_Sym_Shndx);
10474 amt *= bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
10475 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
10476 if (flinfo->symshndxbuf == NULL)
c152c796 10477 {
ef10c3ac 10478 free (symbuf);
0a1b45a2 10479 return false;
c152c796 10480 }
c152c796
AM
10481 }
10482
3d16b64e 10483 /* Now swap out the symbols. */
b03b65e2 10484 for (i = 0; i < flinfo->output_bfd->symcount; i++)
ef10c3ac
L
10485 {
10486 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
10487 if (elfsym->sym.st_name == (unsigned long) -1)
10488 elfsym->sym.st_name = 0;
10489 else
10490 elfsym->sym.st_name
10491 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
10492 elfsym->sym.st_name);
3d16b64e
NA
10493
10494 /* Inform the linker of the addition of this symbol. */
10495
10496 if (flinfo->info->callbacks->ctf_new_symbol)
10497 flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
10498 &elfsym->sym);
10499
ef10c3ac
L
10500 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
10501 ((bfd_byte *) symbuf
10502 + (elfsym->dest_index
10503 * bed->s->sizeof_sym)),
b03b65e2
AM
10504 NPTR_ADD (flinfo->symshndxbuf,
10505 elfsym->dest_index));
ef10c3ac
L
10506 }
10507
10508 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
10509 pos = hdr->sh_offset + hdr->sh_size;
b03b65e2 10510 amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
ef10c3ac 10511 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
226f9f4f 10512 && bfd_write (symbuf, amt, flinfo->output_bfd) == amt)
ef10c3ac
L
10513 {
10514 hdr->sh_size += amt;
0a1b45a2 10515 ret = true;
ef10c3ac
L
10516 }
10517 else
0a1b45a2 10518 ret = false;
c152c796 10519
ef10c3ac 10520 free (symbuf);
ef10c3ac 10521 return ret;
c152c796
AM
10522}
10523
c0d5a53d
L
10524/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
10525
0a1b45a2 10526static bool
c0d5a53d
L
10527check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
10528{
4fbb74a6
AM
10529 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
10530 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
10531 {
10532 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 10533 beyond 64k. */
4eca0228 10534 _bfd_error_handler
695344c0 10535 /* xgettext:c-format */
9793eb77 10536 (_("%pB: too many sections: %d (>= %d)"),
4fbb74a6 10537 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d 10538 bfd_set_error (bfd_error_nonrepresentable_section);
0a1b45a2 10539 return false;
c0d5a53d 10540 }
0a1b45a2 10541 return true;
c0d5a53d
L
10542}
10543
c152c796
AM
10544/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
10545 allowing an unsatisfied unversioned symbol in the DSO to match a
10546 versioned symbol that would normally require an explicit version.
10547 We also handle the case that a DSO references a hidden symbol
10548 which may be satisfied by a versioned symbol in another DSO. */
10549
0a1b45a2 10550static bool
c152c796
AM
10551elf_link_check_versioned_symbol (struct bfd_link_info *info,
10552 const struct elf_backend_data *bed,
10553 struct elf_link_hash_entry *h)
10554{
10555 bfd *abfd;
10556 struct elf_link_loaded_list *loaded;
10557
10558 if (!is_elf_hash_table (info->hash))
0a1b45a2 10559 return false;
c152c796 10560
90c984fc
L
10561 /* Check indirect symbol. */
10562 while (h->root.type == bfd_link_hash_indirect)
10563 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10564
c152c796
AM
10565 switch (h->root.type)
10566 {
10567 default:
10568 abfd = NULL;
10569 break;
10570
10571 case bfd_link_hash_undefined:
10572 case bfd_link_hash_undefweak:
10573 abfd = h->root.u.undef.abfd;
f4ab0e2d
L
10574 if (abfd == NULL
10575 || (abfd->flags & DYNAMIC) == 0
e56f61be 10576 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
0a1b45a2 10577 return false;
c152c796
AM
10578 break;
10579
10580 case bfd_link_hash_defined:
10581 case bfd_link_hash_defweak:
10582 abfd = h->root.u.def.section->owner;
10583 break;
10584
10585 case bfd_link_hash_common:
10586 abfd = h->root.u.c.p->section->owner;
10587 break;
10588 }
10589 BFD_ASSERT (abfd != NULL);
10590
e310298c 10591 for (loaded = elf_hash_table (info)->dyn_loaded;
c152c796
AM
10592 loaded != NULL;
10593 loaded = loaded->next)
10594 {
10595 bfd *input;
10596 Elf_Internal_Shdr *hdr;
ef53be89
AM
10597 size_t symcount;
10598 size_t extsymcount;
10599 size_t extsymoff;
c152c796
AM
10600 Elf_Internal_Shdr *versymhdr;
10601 Elf_Internal_Sym *isym;
10602 Elf_Internal_Sym *isymend;
10603 Elf_Internal_Sym *isymbuf;
10604 Elf_External_Versym *ever;
10605 Elf_External_Versym *extversym;
10606
10607 input = loaded->abfd;
10608
10609 /* We check each DSO for a possible hidden versioned definition. */
10610 if (input == abfd
c152c796
AM
10611 || elf_dynversym (input) == 0)
10612 continue;
10613
10614 hdr = &elf_tdata (input)->dynsymtab_hdr;
10615
10616 symcount = hdr->sh_size / bed->s->sizeof_sym;
10617 if (elf_bad_symtab (input))
10618 {
10619 extsymcount = symcount;
10620 extsymoff = 0;
10621 }
10622 else
10623 {
10624 extsymcount = symcount - hdr->sh_info;
10625 extsymoff = hdr->sh_info;
10626 }
10627
10628 if (extsymcount == 0)
10629 continue;
10630
10631 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10632 NULL, NULL, NULL);
10633 if (isymbuf == NULL)
0a1b45a2 10634 return false;
c152c796
AM
10635
10636 /* Read in any version definitions. */
10637 versymhdr = &elf_tdata (input)->dynversym_hdr;
c152c796 10638 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
2bb3687b
AM
10639 || (extversym = (Elf_External_Versym *)
10640 _bfd_malloc_and_read (input, versymhdr->sh_size,
10641 versymhdr->sh_size)) == NULL)
c152c796 10642 {
c152c796 10643 free (isymbuf);
0a1b45a2 10644 return false;
c152c796
AM
10645 }
10646
10647 ever = extversym + extsymoff;
10648 isymend = isymbuf + extsymcount;
10649 for (isym = isymbuf; isym < isymend; isym++, ever++)
10650 {
10651 const char *name;
10652 Elf_Internal_Versym iver;
10653 unsigned short version_index;
10654
10655 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10656 || isym->st_shndx == SHN_UNDEF)
10657 continue;
10658
10659 name = bfd_elf_string_from_elf_section (input,
10660 hdr->sh_link,
10661 isym->st_name);
10662 if (strcmp (name, h->root.root.string) != 0)
10663 continue;
10664
10665 _bfd_elf_swap_versym_in (input, ever, &iver);
10666
d023c380
L
10667 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10668 && !(h->def_regular
10669 && h->forced_local))
c152c796
AM
10670 {
10671 /* If we have a non-hidden versioned sym, then it should
d023c380
L
10672 have provided a definition for the undefined sym unless
10673 it is defined in a non-shared object and forced local.
10674 */
c152c796
AM
10675 abort ();
10676 }
10677
10678 version_index = iver.vs_vers & VERSYM_VERSION;
10679 if (version_index == 1 || version_index == 2)
10680 {
10681 /* This is the base or first version. We can use it. */
10682 free (extversym);
10683 free (isymbuf);
0a1b45a2 10684 return true;
c152c796
AM
10685 }
10686 }
10687
10688 free (extversym);
10689 free (isymbuf);
10690 }
10691
0a1b45a2 10692 return false;
c152c796
AM
10693}
10694
b8871f35
L
10695/* Convert ELF common symbol TYPE. */
10696
10697static int
10698elf_link_convert_common_type (struct bfd_link_info *info, int type)
10699{
10700 /* Commom symbol can only appear in relocatable link. */
10701 if (!bfd_link_relocatable (info))
10702 abort ();
10703 switch (info->elf_stt_common)
10704 {
10705 case unchanged:
10706 break;
10707 case elf_stt_common:
10708 type = STT_COMMON;
10709 break;
10710 case no_elf_stt_common:
10711 type = STT_OBJECT;
10712 break;
10713 }
10714 return type;
10715}
10716
c152c796
AM
10717/* Add an external symbol to the symbol table. This is called from
10718 the hash table traversal routine. When generating a shared object,
10719 we go through the symbol table twice. The first time we output
10720 anything that might have been forced to local scope in a version
10721 script. The second time we output the symbols that are still
10722 global symbols. */
10723
0a1b45a2 10724static bool
7686d77d 10725elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 10726{
7686d77d 10727 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 10728 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 10729 struct elf_final_link_info *flinfo = eoinfo->flinfo;
0a1b45a2 10730 bool strip;
c152c796
AM
10731 Elf_Internal_Sym sym;
10732 asection *input_sec;
10733 const struct elf_backend_data *bed;
6e0b88f1
AM
10734 long indx;
10735 int ret;
b8871f35 10736 unsigned int type;
c152c796
AM
10737
10738 if (h->root.type == bfd_link_hash_warning)
10739 {
10740 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10741 if (h->root.type == bfd_link_hash_new)
0a1b45a2 10742 return true;
c152c796
AM
10743 }
10744
10745 /* Decide whether to output this symbol in this pass. */
10746 if (eoinfo->localsyms)
10747 {
4deb8f71 10748 if (!h->forced_local)
0a1b45a2 10749 return true;
c152c796
AM
10750 }
10751 else
10752 {
4deb8f71 10753 if (h->forced_local)
0a1b45a2 10754 return true;
c152c796
AM
10755 }
10756
8b127cbc 10757 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 10758
12ac1cf5 10759 if (h->root.type == bfd_link_hash_undefined)
c152c796 10760 {
12ac1cf5
NC
10761 /* If we have an undefined symbol reference here then it must have
10762 come from a shared library that is being linked in. (Undefined
98da7939
L
10763 references in regular files have already been handled unless
10764 they are in unreferenced sections which are removed by garbage
10765 collection). */
0a1b45a2 10766 bool ignore_undef = false;
12ac1cf5
NC
10767
10768 /* Some symbols may be special in that the fact that they're
10769 undefined can be safely ignored - let backend determine that. */
10770 if (bed->elf_backend_ignore_undef_symbol)
10771 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10772
10773 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 10774 if (!ignore_undef
c54f1524 10775 && h->ref_dynamic_nonweak
8b127cbc
AM
10776 && (!h->ref_regular || flinfo->info->gc_sections)
10777 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10778 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
95a51568
FS
10779 {
10780 flinfo->info->callbacks->undefined_symbol
10781 (flinfo->info, h->root.root.string,
10782 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10783 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10784 && !flinfo->info->warn_unresolved_syms);
10785 }
97196564
L
10786
10787 /* Strip a global symbol defined in a discarded section. */
10788 if (h->indx == -3)
0a1b45a2 10789 return true;
c152c796
AM
10790 }
10791
10792 /* We should also warn if a forced local symbol is referenced from
10793 shared libraries. */
0e1862bb 10794 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
10795 && h->forced_local
10796 && h->ref_dynamic
371a5866 10797 && h->def_regular
f5385ebf 10798 && !h->dynamic_def
ee659f1f 10799 && h->ref_dynamic_nonweak
8b127cbc 10800 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 10801 {
17d078c5
AM
10802 bfd *def_bfd;
10803 const char *msg;
90c984fc
L
10804 struct elf_link_hash_entry *hi = h;
10805
10806 /* Check indirect symbol. */
10807 while (hi->root.type == bfd_link_hash_indirect)
10808 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
10809
10810 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
695344c0 10811 /* xgettext:c-format */
871b3ab2 10812 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
17d078c5 10813 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
695344c0 10814 /* xgettext:c-format */
871b3ab2 10815 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
17d078c5 10816 else
695344c0 10817 /* xgettext:c-format */
871b3ab2 10818 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
8b127cbc 10819 def_bfd = flinfo->output_bfd;
90c984fc
L
10820 if (hi->root.u.def.section != bfd_abs_section_ptr)
10821 def_bfd = hi->root.u.def.section->owner;
c08bb8dd
AM
10822 _bfd_error_handler (msg, flinfo->output_bfd,
10823 h->root.root.string, def_bfd);
17d078c5 10824 bfd_set_error (bfd_error_bad_value);
0a1b45a2
AM
10825 eoinfo->failed = true;
10826 return false;
c152c796
AM
10827 }
10828
10829 /* We don't want to output symbols that have never been mentioned by
10830 a regular file, or that we have been told to strip. However, if
10831 h->indx is set to -2, the symbol is used by a reloc and we must
10832 output it. */
0a1b45a2 10833 strip = false;
c152c796 10834 if (h->indx == -2)
d983c8c5 10835 ;
f5385ebf 10836 else if ((h->def_dynamic
77cfaee6
AM
10837 || h->ref_dynamic
10838 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
10839 && !h->def_regular
10840 && !h->ref_regular)
0a1b45a2 10841 strip = true;
8b127cbc 10842 else if (flinfo->info->strip == strip_all)
0a1b45a2 10843 strip = true;
8b127cbc
AM
10844 else if (flinfo->info->strip == strip_some
10845 && bfd_hash_lookup (flinfo->info->keep_hash,
0a1b45a2
AM
10846 h->root.root.string, false, false) == NULL)
10847 strip = true;
d56d55e7
AM
10848 else if ((h->root.type == bfd_link_hash_defined
10849 || h->root.type == bfd_link_hash_defweak)
8b127cbc 10850 && ((flinfo->info->strip_discarded
dbaa2011 10851 && discarded_section (h->root.u.def.section))
ca4be51c
AM
10852 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10853 && h->root.u.def.section->owner != NULL
d56d55e7 10854 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
0a1b45a2 10855 strip = true;
9e2278f5
AM
10856 else if ((h->root.type == bfd_link_hash_undefined
10857 || h->root.type == bfd_link_hash_undefweak)
10858 && h->root.u.undef.abfd != NULL
10859 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
0a1b45a2 10860 strip = true;
c152c796 10861
eebad48e
L
10862 /* Remember if this symbol should be stripped. */
10863 bool should_strip = strip;
10864
10865 /* Strip undefined weak symbols link if they don't have relocation. */
10866 if (!strip)
10867 strip = !h->has_reloc && h->root.type == bfd_link_hash_undefweak;
10868
b8871f35
L
10869 type = h->type;
10870
c152c796 10871 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
10872 nothing else to do. However, if it is a forced local symbol or
10873 an ifunc symbol we need to give the backend finish_dynamic_symbol
10874 function a chance to make it dynamic. */
c152c796
AM
10875 if (strip
10876 && h->dynindx == -1
b8871f35 10877 && type != STT_GNU_IFUNC
f5385ebf 10878 && !h->forced_local)
0a1b45a2 10879 return true;
c152c796
AM
10880
10881 sym.st_value = 0;
10882 sym.st_size = h->size;
10883 sym.st_other = h->other;
c152c796
AM
10884 switch (h->root.type)
10885 {
10886 default:
10887 case bfd_link_hash_new:
10888 case bfd_link_hash_warning:
10889 abort ();
0a1b45a2 10890 return false;
c152c796
AM
10891
10892 case bfd_link_hash_undefined:
10893 case bfd_link_hash_undefweak:
10894 input_sec = bfd_und_section_ptr;
10895 sym.st_shndx = SHN_UNDEF;
10896 break;
10897
10898 case bfd_link_hash_defined:
10899 case bfd_link_hash_defweak:
10900 {
10901 input_sec = h->root.u.def.section;
10902 if (input_sec->output_section != NULL)
10903 {
10904 sym.st_shndx =
8b127cbc 10905 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
10906 input_sec->output_section);
10907 if (sym.st_shndx == SHN_BAD)
10908 {
4eca0228 10909 _bfd_error_handler
695344c0 10910 /* xgettext:c-format */
871b3ab2 10911 (_("%pB: could not find output section %pA for input section %pA"),
8b127cbc 10912 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 10913 bfd_set_error (bfd_error_nonrepresentable_section);
0a1b45a2
AM
10914 eoinfo->failed = true;
10915 return false;
c152c796
AM
10916 }
10917
10918 /* ELF symbols in relocatable files are section relative,
10919 but in nonrelocatable files they are virtual
10920 addresses. */
10921 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 10922 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10923 {
10924 sym.st_value += input_sec->output_section->vma;
10925 if (h->type == STT_TLS)
10926 {
8b127cbc 10927 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
10928 if (tls_sec != NULL)
10929 sym.st_value -= tls_sec->vma;
c152c796
AM
10930 }
10931 }
10932 }
10933 else
10934 {
10935 BFD_ASSERT (input_sec->owner == NULL
10936 || (input_sec->owner->flags & DYNAMIC) != 0);
10937 sym.st_shndx = SHN_UNDEF;
10938 input_sec = bfd_und_section_ptr;
10939 }
10940 }
10941 break;
10942
10943 case bfd_link_hash_common:
10944 input_sec = h->root.u.c.p->section;
a4d8e49b 10945 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
10946 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10947 break;
10948
10949 case bfd_link_hash_indirect:
10950 /* These symbols are created by symbol versioning. They point
10951 to the decorated version of the name. For example, if the
10952 symbol foo@@GNU_1.2 is the default, which should be used when
10953 foo is used with no version, then we add an indirect symbol
10954 foo which points to foo@@GNU_1.2. We ignore these symbols,
10955 since the indirected symbol is already in the hash table. */
0a1b45a2 10956 return true;
c152c796
AM
10957 }
10958
b8871f35
L
10959 if (type == STT_COMMON || type == STT_OBJECT)
10960 switch (h->root.type)
10961 {
10962 case bfd_link_hash_common:
10963 type = elf_link_convert_common_type (flinfo->info, type);
10964 break;
10965 case bfd_link_hash_defined:
10966 case bfd_link_hash_defweak:
10967 if (bed->common_definition (&sym))
10968 type = elf_link_convert_common_type (flinfo->info, type);
10969 else
10970 type = STT_OBJECT;
10971 break;
10972 case bfd_link_hash_undefined:
10973 case bfd_link_hash_undefweak:
10974 break;
10975 default:
10976 abort ();
10977 }
10978
4deb8f71 10979 if (h->forced_local)
b8871f35
L
10980 {
10981 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10982 /* Turn off visibility on local symbol. */
10983 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10984 }
10985 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10986 else if (h->unique_global && h->def_regular)
10987 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10988 else if (h->root.type == bfd_link_hash_undefweak
10989 || h->root.type == bfd_link_hash_defweak)
10990 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10991 else
10992 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10993 sym.st_target_internal = h->target_internal;
10994
c152c796
AM
10995 /* Give the processor backend a chance to tweak the symbol value,
10996 and also to finish up anything that needs to be done for this
10997 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 10998 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 10999 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 11000 if ((h->type == STT_GNU_IFUNC
5f35ea9c 11001 && h->def_regular
0e1862bb 11002 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
11003 || ((h->dynindx != -1
11004 || h->forced_local)
0e1862bb 11005 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
11006 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
11007 || h->root.type != bfd_link_hash_undefweak))
11008 || !h->forced_local)
8b127cbc 11009 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
11010 {
11011 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 11012 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796 11013 {
0a1b45a2
AM
11014 eoinfo->failed = true;
11015 return false;
c152c796 11016 }
eebad48e
L
11017 /* If a symbol is in the dynamic symbol table and isn't a
11018 should-strip symbol, also keep it in the symbol table. */
11019 if (!should_strip)
11020 strip = false;
c152c796
AM
11021 }
11022
11023 /* If we are marking the symbol as undefined, and there are no
11024 non-weak references to this symbol from a regular object, then
11025 mark the symbol as weak undefined; if there are non-weak
11026 references, mark the symbol as strong. We can't do this earlier,
11027 because it might not be marked as undefined until the
11028 finish_dynamic_symbol routine gets through with it. */
11029 if (sym.st_shndx == SHN_UNDEF
f5385ebf 11030 && h->ref_regular
c152c796
AM
11031 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
11032 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
11033 {
11034 int bindtype;
b8871f35 11035 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
11036
11037 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
11038 if (type == STT_GNU_IFUNC)
11039 type = STT_FUNC;
c152c796 11040
f5385ebf 11041 if (h->ref_regular_nonweak)
c152c796
AM
11042 bindtype = STB_GLOBAL;
11043 else
11044 bindtype = STB_WEAK;
2955ec4c 11045 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
11046 }
11047
bda987c2
CD
11048 /* If this is a symbol defined in a dynamic library, don't use the
11049 symbol size from the dynamic library. Relinking an executable
11050 against a new library may introduce gratuitous changes in the
11051 executable's symbols if we keep the size. */
11052 if (sym.st_shndx == SHN_UNDEF
11053 && !h->def_regular
11054 && h->def_dynamic)
11055 sym.st_size = 0;
11056
c152c796
AM
11057 /* If a non-weak symbol with non-default visibility is not defined
11058 locally, it is a fatal error. */
0e1862bb 11059 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
11060 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
11061 && ELF_ST_BIND (sym.st_info) != STB_WEAK
11062 && h->root.type == bfd_link_hash_undefined
f5385ebf 11063 && !h->def_regular)
c152c796 11064 {
17d078c5
AM
11065 const char *msg;
11066
11067 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
695344c0 11068 /* xgettext:c-format */
871b3ab2 11069 msg = _("%pB: protected symbol `%s' isn't defined");
17d078c5 11070 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
695344c0 11071 /* xgettext:c-format */
871b3ab2 11072 msg = _("%pB: internal symbol `%s' isn't defined");
17d078c5 11073 else
695344c0 11074 /* xgettext:c-format */
871b3ab2 11075 msg = _("%pB: hidden symbol `%s' isn't defined");
4eca0228 11076 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 11077 bfd_set_error (bfd_error_bad_value);
0a1b45a2
AM
11078 eoinfo->failed = true;
11079 return false;
c152c796
AM
11080 }
11081
11082 /* If this symbol should be put in the .dynsym section, then put it
11083 there now. We already know the symbol index. We also fill in
11084 the entry in the .hash section. */
1c2649ed
EB
11085 if (h->dynindx != -1
11086 && elf_hash_table (flinfo->info)->dynamic_sections_created
11087 && elf_hash_table (flinfo->info)->dynsym != NULL
11088 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
c152c796 11089 {
c152c796
AM
11090 bfd_byte *esym;
11091
90c984fc
L
11092 /* Since there is no version information in the dynamic string,
11093 if there is no version info in symbol version section, we will
1659f720 11094 have a run-time problem if not linking executable, referenced
4deb8f71 11095 by shared library, or not bound locally. */
1659f720 11096 if (h->verinfo.verdef == NULL
0e1862bb 11097 && (!bfd_link_executable (flinfo->info)
1659f720
L
11098 || h->ref_dynamic
11099 || !h->def_regular))
90c984fc
L
11100 {
11101 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
11102
11103 if (p && p [1] != '\0')
11104 {
4eca0228 11105 _bfd_error_handler
695344c0 11106 /* xgettext:c-format */
9793eb77 11107 (_("%pB: no symbol version section for versioned symbol `%s'"),
90c984fc 11108 flinfo->output_bfd, h->root.root.string);
0a1b45a2
AM
11109 eoinfo->failed = true;
11110 return false;
90c984fc
L
11111 }
11112 }
11113
c152c796 11114 sym.st_name = h->dynstr_index;
cae1fbbb
L
11115 esym = (elf_hash_table (flinfo->info)->dynsym->contents
11116 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 11117 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d 11118 {
0a1b45a2
AM
11119 eoinfo->failed = true;
11120 return false;
c0d5a53d 11121 }
3d16b64e
NA
11122
11123 /* Inform the linker of the addition of this symbol. */
11124
11125 if (flinfo->info->callbacks->ctf_new_dynsym)
11126 flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
11127
8b127cbc 11128 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 11129
8b127cbc 11130 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
11131 {
11132 size_t hash_entry_size;
11133 bfd_byte *bucketpos;
11134 bfd_vma chain;
41198d0c
L
11135 size_t bucketcount;
11136 size_t bucket;
11137
8b127cbc 11138 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 11139 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
11140
11141 hash_entry_size
8b127cbc
AM
11142 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
11143 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 11144 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
11145 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
11146 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
11147 bucketpos);
11148 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
11149 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
11150 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
11151 }
c152c796 11152
8b127cbc 11153 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
11154 {
11155 Elf_Internal_Versym iversym;
11156 Elf_External_Versym *eversym;
11157
5fa370e4 11158 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
c152c796 11159 {
7b20f099
AM
11160 if (h->verinfo.verdef == NULL
11161 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
11162 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
eb6e6af8 11163 iversym.vs_vers = 1;
c152c796
AM
11164 else
11165 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
11166 }
11167 else
11168 {
11169 if (h->verinfo.vertree == NULL)
11170 iversym.vs_vers = 1;
11171 else
11172 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 11173 if (flinfo->info->create_default_symver)
3e3b46e5 11174 iversym.vs_vers++;
c152c796
AM
11175 }
11176
422f1182 11177 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 11178 defined locally. */
422f1182 11179 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
11180 iversym.vs_vers |= VERSYM_HIDDEN;
11181
8b127cbc 11182 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 11183 eversym += h->dynindx;
8b127cbc 11184 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
11185 }
11186 }
11187
d983c8c5
AM
11188 /* If the symbol is undefined, and we didn't output it to .dynsym,
11189 strip it from .symtab too. Obviously we can't do this for
11190 relocatable output or when needed for --emit-relocs. */
11191 else if (input_sec == bfd_und_section_ptr
11192 && h->indx != -2
66cae560
NC
11193 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
11194 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
0e1862bb 11195 && !bfd_link_relocatable (flinfo->info))
0a1b45a2 11196 return true;
66cae560 11197
d983c8c5
AM
11198 /* Also strip others that we couldn't earlier due to dynamic symbol
11199 processing. */
11200 if (strip)
0a1b45a2 11201 return true;
d983c8c5 11202 if ((input_sec->flags & SEC_EXCLUDE) != 0)
0a1b45a2 11203 return true;
c152c796 11204
2ec55de3
AM
11205 /* Output a FILE symbol so that following locals are not associated
11206 with the wrong input file. We need one for forced local symbols
11207 if we've seen more than one FILE symbol or when we have exactly
11208 one FILE symbol but global symbols are present in a file other
11209 than the one with the FILE symbol. We also need one if linker
11210 defined symbols are present. In practice these conditions are
11211 always met, so just emit the FILE symbol unconditionally. */
11212 if (eoinfo->localsyms
11213 && !eoinfo->file_sym_done
11214 && eoinfo->flinfo->filesym_count != 0)
11215 {
11216 Elf_Internal_Sym fsym;
11217
11218 memset (&fsym, 0, sizeof (fsym));
11219 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11220 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
11221 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
11222 bfd_und_section_ptr, NULL))
0a1b45a2 11223 return false;
2ec55de3 11224
0a1b45a2 11225 eoinfo->file_sym_done = true;
2ec55de3
AM
11226 }
11227
8b127cbc 11228 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
11229 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
11230 input_sec, h);
6e0b88f1 11231 if (ret == 0)
c152c796 11232 {
0a1b45a2
AM
11233 eoinfo->failed = true;
11234 return false;
c152c796 11235 }
6e0b88f1
AM
11236 else if (ret == 1)
11237 h->indx = indx;
11238 else if (h->indx == -2)
11239 abort();
c152c796 11240
0a1b45a2 11241 return true;
c152c796
AM
11242}
11243
cdd3575c
AM
11244/* Return TRUE if special handling is done for relocs in SEC against
11245 symbols defined in discarded sections. */
11246
0a1b45a2 11247static bool
c152c796
AM
11248elf_section_ignore_discarded_relocs (asection *sec)
11249{
11250 const struct elf_backend_data *bed;
11251
cdd3575c
AM
11252 switch (sec->sec_info_type)
11253 {
dbaa2011
AM
11254 case SEC_INFO_TYPE_STABS:
11255 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 11256 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cf0e0a0b 11257 case SEC_INFO_TYPE_SFRAME:
0a1b45a2 11258 return true;
cdd3575c
AM
11259 default:
11260 break;
11261 }
c152c796
AM
11262
11263 bed = get_elf_backend_data (sec->owner);
11264 if (bed->elf_backend_ignore_discarded_relocs != NULL
11265 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
0a1b45a2 11266 return true;
c152c796 11267
0a1b45a2 11268 return false;
c152c796
AM
11269}
11270
9e66c942
AM
11271/* Return a mask saying how ld should treat relocations in SEC against
11272 symbols defined in discarded sections. If this function returns
11273 COMPLAIN set, ld will issue a warning message. If this function
11274 returns PRETEND set, and the discarded section was link-once and the
11275 same size as the kept link-once section, ld will pretend that the
11276 symbol was actually defined in the kept section. Otherwise ld will
11277 zero the reloc (at least that is the intent, but some cooperation by
11278 the target dependent code is needed, particularly for REL targets). */
11279
8a696751
AM
11280unsigned int
11281_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 11282{
853ba678
J
11283 const struct elf_backend_data *bed;
11284 bed = get_elf_backend_data (sec->owner);
11285
9e66c942 11286 if (sec->flags & SEC_DEBUGGING)
69d54b1b 11287 return PRETEND;
cdd3575c
AM
11288
11289 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 11290 return 0;
cdd3575c 11291
853ba678
J
11292 if (bed->elf_backend_can_make_multiple_eh_frame
11293 && strncmp (sec->name, ".eh_frame.", 10) == 0)
11294 return 0;
11295
cf0e0a0b
IB
11296 if (strcmp (".sframe", sec->name) == 0)
11297 return 0;
11298
cdd3575c 11299 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 11300 return 0;
cdd3575c 11301
9e66c942 11302 return COMPLAIN | PRETEND;
cdd3575c
AM
11303}
11304
3d7f7666
L
11305/* Find a match between a section and a member of a section group. */
11306
11307static asection *
c0f00686
L
11308match_group_member (asection *sec, asection *group,
11309 struct bfd_link_info *info)
3d7f7666
L
11310{
11311 asection *first = elf_next_in_group (group);
11312 asection *s = first;
11313
11314 while (s != NULL)
11315 {
c0f00686 11316 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
11317 return s;
11318
83180ade 11319 s = elf_next_in_group (s);
3d7f7666
L
11320 if (s == first)
11321 break;
11322 }
11323
11324 return NULL;
11325}
11326
01b3c8ab 11327/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
11328 to replace it. Return the replacement if it is OK. Otherwise return
11329 NULL. */
01b3c8ab
L
11330
11331asection *
c0f00686 11332_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
11333{
11334 asection *kept;
11335
11336 kept = sec->kept_section;
11337 if (kept != NULL)
11338 {
c2370991 11339 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 11340 kept = match_group_member (sec, kept, info);
58349d00
L
11341 if (kept != NULL)
11342 {
11343 if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
11344 != (kept->rawsize != 0 ? kept->rawsize : kept->size))
11345 kept = NULL;
11346 else
11347 {
11348 /* Get the real kept section. */
11349 asection *next;
11350 for (next = kept->kept_section;
11351 next != NULL;
11352 next = next->kept_section)
11353 kept = next;
11354 }
11355 }
c2370991 11356 sec->kept_section = kept;
01b3c8ab
L
11357 }
11358 return kept;
11359}
11360
c152c796
AM
11361/* Link an input file into the linker output file. This function
11362 handles all the sections and relocations of the input file at once.
11363 This is so that we only have to read the local symbols once, and
11364 don't have to keep them in memory. */
11365
0a1b45a2 11366static bool
8b127cbc 11367elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 11368{
ece5ef60 11369 int (*relocate_section)
c152c796
AM
11370 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
11371 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
11372 bfd *output_bfd;
11373 Elf_Internal_Shdr *symtab_hdr;
11374 size_t locsymcount;
11375 size_t extsymoff;
11376 Elf_Internal_Sym *isymbuf;
11377 Elf_Internal_Sym *isym;
11378 Elf_Internal_Sym *isymend;
11379 long *pindex;
11380 asection **ppsection;
11381 asection *o;
11382 const struct elf_backend_data *bed;
c152c796 11383 struct elf_link_hash_entry **sym_hashes;
310fd250
L
11384 bfd_size_type address_size;
11385 bfd_vma r_type_mask;
11386 int r_sym_shift;
0a1b45a2 11387 bool have_file_sym = false;
c152c796 11388
8b127cbc 11389 output_bfd = flinfo->output_bfd;
c152c796
AM
11390 bed = get_elf_backend_data (output_bfd);
11391 relocate_section = bed->elf_backend_relocate_section;
11392
11393 /* If this is a dynamic object, we don't want to do anything here:
11394 we don't want the local symbols, and we don't want the section
11395 contents. */
11396 if ((input_bfd->flags & DYNAMIC) != 0)
0a1b45a2 11397 return true;
c152c796 11398
c152c796
AM
11399 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
11400 if (elf_bad_symtab (input_bfd))
11401 {
11402 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11403 extsymoff = 0;
11404 }
11405 else
11406 {
11407 locsymcount = symtab_hdr->sh_info;
11408 extsymoff = symtab_hdr->sh_info;
11409 }
11410
99fabbc9
JL
11411 /* Enable GNU OSABI features in the output BFD that are used in the input
11412 BFD. */
11413 if (bed->elf_osabi == ELFOSABI_NONE
11414 || bed->elf_osabi == ELFOSABI_GNU
11415 || bed->elf_osabi == ELFOSABI_FREEBSD)
11416 elf_tdata (output_bfd)->has_gnu_osabi
04f89674
L
11417 |= (elf_tdata (input_bfd)->has_gnu_osabi
11418 & (bfd_link_relocatable (flinfo->info)
11419 ? -1 : ~elf_gnu_osabi_retain));
99fabbc9 11420
c152c796
AM
11421 /* Read the local symbols. */
11422 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
11423 if (isymbuf == NULL && locsymcount != 0)
11424 {
11425 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
11426 flinfo->internal_syms,
11427 flinfo->external_syms,
11428 flinfo->locsym_shndx);
c152c796 11429 if (isymbuf == NULL)
0a1b45a2 11430 return false;
c152c796
AM
11431 }
11432
11433 /* Find local symbol sections and adjust values of symbols in
11434 SEC_MERGE sections. Write out those local symbols we know are
11435 going into the output file. */
36f61bf2 11436 isymend = PTR_ADD (isymbuf, locsymcount);
8b127cbc 11437 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
11438 isym < isymend;
11439 isym++, pindex++, ppsection++)
11440 {
11441 asection *isec;
11442 const char *name;
11443 Elf_Internal_Sym osym;
6e0b88f1
AM
11444 long indx;
11445 int ret;
c152c796
AM
11446
11447 *pindex = -1;
11448
11449 if (elf_bad_symtab (input_bfd))
11450 {
11451 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
11452 {
11453 *ppsection = NULL;
11454 continue;
11455 }
11456 }
11457
11458 if (isym->st_shndx == SHN_UNDEF)
11459 isec = bfd_und_section_ptr;
c152c796
AM
11460 else if (isym->st_shndx == SHN_ABS)
11461 isec = bfd_abs_section_ptr;
11462 else if (isym->st_shndx == SHN_COMMON)
11463 isec = bfd_com_section_ptr;
11464 else
11465 {
cb33740c
AM
11466 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
11467 if (isec == NULL)
11468 {
11469 /* Don't attempt to output symbols with st_shnx in the
11470 reserved range other than SHN_ABS and SHN_COMMON. */
6835821b 11471 isec = bfd_und_section_ptr;
cb33740c 11472 }
dbaa2011 11473 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
11474 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
11475 isym->st_value =
11476 _bfd_merged_section_offset (output_bfd, &isec,
11477 elf_section_data (isec)->sec_info,
11478 isym->st_value);
c152c796
AM
11479 }
11480
11481 *ppsection = isec;
11482
d983c8c5
AM
11483 /* Don't output the first, undefined, symbol. In fact, don't
11484 output any undefined local symbol. */
11485 if (isec == bfd_und_section_ptr)
c152c796
AM
11486 continue;
11487
11488 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
11489 {
11490 /* We never output section symbols. Instead, we use the
11491 section symbol of the corresponding section in the output
11492 file. */
11493 continue;
11494 }
11495
11496 /* If we are stripping all symbols, we don't want to output this
11497 one. */
8b127cbc 11498 if (flinfo->info->strip == strip_all)
c152c796
AM
11499 continue;
11500
11501 /* If we are discarding all local symbols, we don't want to
11502 output this one. If we are generating a relocatable output
11503 file, then some of the local symbols may be required by
11504 relocs; we output them below as we discover that they are
11505 needed. */
8b127cbc 11506 if (flinfo->info->discard == discard_all)
c152c796
AM
11507 continue;
11508
11509 /* If this symbol is defined in a section which we are
f02571c5 11510 discarding, we don't need to keep it. */
c63d4862
AM
11511 if (isym->st_shndx < SHN_LORESERVE
11512 && (isec->output_section == NULL
11513 || bfd_section_removed_from_list (output_bfd,
11514 isec->output_section)))
e75a280b
L
11515 continue;
11516
c152c796
AM
11517 /* Get the name of the symbol. */
11518 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
11519 isym->st_name);
11520 if (name == NULL)
0a1b45a2 11521 return false;
c152c796
AM
11522
11523 /* See if we are discarding symbols with this name. */
8b127cbc 11524 if ((flinfo->info->strip == strip_some
0a1b45a2 11525 && (bfd_hash_lookup (flinfo->info->keep_hash, name, false, false)
c152c796 11526 == NULL))
8b127cbc 11527 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
11528 && (isec->flags & SEC_MERGE)
11529 && !bfd_link_relocatable (flinfo->info))
8b127cbc 11530 || flinfo->info->discard == discard_l)
c152c796
AM
11531 && bfd_is_local_label_name (input_bfd, name)))
11532 continue;
11533
ffbc01cc
AM
11534 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
11535 {
ce875075
AM
11536 if (input_bfd->lto_output)
11537 /* -flto puts a temp file name here. This means builds
11538 are not reproducible. Discard the symbol. */
11539 continue;
0a1b45a2 11540 have_file_sym = true;
ffbc01cc
AM
11541 flinfo->filesym_count += 1;
11542 }
11543 if (!have_file_sym)
11544 {
11545 /* In the absence of debug info, bfd_find_nearest_line uses
11546 FILE symbols to determine the source file for local
11547 function symbols. Provide a FILE symbol here if input
11548 files lack such, so that their symbols won't be
11549 associated with a previous input file. It's not the
11550 source file, but the best we can do. */
5b4293ba 11551 const char *filename;
0a1b45a2 11552 have_file_sym = true;
ffbc01cc
AM
11553 flinfo->filesym_count += 1;
11554 memset (&osym, 0, sizeof (osym));
11555 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
11556 osym.st_shndx = SHN_ABS;
5b4293ba
AM
11557 if (input_bfd->lto_output)
11558 filename = NULL;
11559 else
11560 filename = lbasename (bfd_get_filename (input_bfd));
11561 if (!elf_link_output_symstrtab (flinfo, filename, &osym,
11562 bfd_abs_section_ptr, NULL))
0a1b45a2 11563 return false;
ffbc01cc
AM
11564 }
11565
c152c796
AM
11566 osym = *isym;
11567
11568 /* Adjust the section index for the output file. */
11569 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11570 isec->output_section);
11571 if (osym.st_shndx == SHN_BAD)
0a1b45a2 11572 return false;
c152c796 11573
c152c796
AM
11574 /* ELF symbols in relocatable files are section relative, but
11575 in executable files they are virtual addresses. Note that
11576 this code assumes that all ELF sections have an associated
11577 BFD section with a reasonable value for output_offset; below
11578 we assume that they also have a reasonable value for
11579 output_section. Any special sections must be set up to meet
11580 these requirements. */
11581 osym.st_value += isec->output_offset;
0e1862bb 11582 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11583 {
11584 osym.st_value += isec->output_section->vma;
11585 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
11586 {
11587 /* STT_TLS symbols are relative to PT_TLS segment base. */
102def4d
AM
11588 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
11589 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
11590 else
11591 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
11592 STT_NOTYPE);
c152c796
AM
11593 }
11594 }
11595
6e0b88f1 11596 indx = bfd_get_symcount (output_bfd);
ef10c3ac 11597 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 11598 if (ret == 0)
0a1b45a2 11599 return false;
6e0b88f1
AM
11600 else if (ret == 1)
11601 *pindex = indx;
c152c796
AM
11602 }
11603
310fd250
L
11604 if (bed->s->arch_size == 32)
11605 {
11606 r_type_mask = 0xff;
11607 r_sym_shift = 8;
11608 address_size = 4;
11609 }
11610 else
11611 {
11612 r_type_mask = 0xffffffff;
11613 r_sym_shift = 32;
11614 address_size = 8;
11615 }
11616
c152c796
AM
11617 /* Relocate the contents of each section. */
11618 sym_hashes = elf_sym_hashes (input_bfd);
11619 for (o = input_bfd->sections; o != NULL; o = o->next)
11620 {
11621 bfd_byte *contents;
11622
11623 if (! o->linker_mark)
11624 {
11625 /* This section was omitted from the link. */
11626 continue;
11627 }
11628
7bdf4127 11629 if (!flinfo->info->resolve_section_groups
bcacc0f5
AM
11630 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
11631 {
11632 /* Deal with the group signature symbol. */
11633 struct bfd_elf_section_data *sec_data = elf_section_data (o);
11634 unsigned long symndx = sec_data->this_hdr.sh_info;
11635 asection *osec = o->output_section;
11636
7bdf4127 11637 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
bcacc0f5
AM
11638 if (symndx >= locsymcount
11639 || (elf_bad_symtab (input_bfd)
8b127cbc 11640 && flinfo->sections[symndx] == NULL))
bcacc0f5 11641 {
931494c9
NC
11642 struct elf_link_hash_entry *h;
11643
11644 h = get_link_hash_entry (sym_hashes, symndx, extsymoff);
11645 if (h == NULL)
11646 {
11647 _bfd_error_handler
11648 /* xgettext:c-format */
11649 (_("error: %pB: unable to create group section symbol"),
11650 input_bfd);
11651 bfd_set_error (bfd_error_bad_value);
11652 return false;
11653 }
11654
bcacc0f5
AM
11655 /* Arrange for symbol to be output. */
11656 h->indx = -2;
11657 elf_section_data (osec)->this_hdr.sh_info = -2;
11658 }
11659 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
11660 {
11661 /* We'll use the output section target_index. */
8b127cbc 11662 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
11663 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
11664 }
11665 else
11666 {
8b127cbc 11667 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
11668 {
11669 /* Otherwise output the local symbol now. */
11670 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 11671 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 11672 const char *name;
6e0b88f1
AM
11673 long indx;
11674 int ret;
bcacc0f5
AM
11675
11676 name = bfd_elf_string_from_elf_section (input_bfd,
11677 symtab_hdr->sh_link,
11678 sym.st_name);
11679 if (name == NULL)
0a1b45a2 11680 return false;
bcacc0f5
AM
11681
11682 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11683 sec);
11684 if (sym.st_shndx == SHN_BAD)
0a1b45a2 11685 return false;
bcacc0f5
AM
11686
11687 sym.st_value += o->output_offset;
11688
6e0b88f1 11689 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
11690 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11691 NULL);
6e0b88f1 11692 if (ret == 0)
0a1b45a2 11693 return false;
6e0b88f1 11694 else if (ret == 1)
8b127cbc 11695 flinfo->indices[symndx] = indx;
6e0b88f1
AM
11696 else
11697 abort ();
bcacc0f5
AM
11698 }
11699 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 11700 = flinfo->indices[symndx];
bcacc0f5
AM
11701 }
11702 }
11703
c152c796 11704 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 11705 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
11706 continue;
11707
11708 if ((o->flags & SEC_LINKER_CREATED) != 0)
11709 {
11710 /* Section was created by _bfd_elf_link_create_dynamic_sections
11711 or somesuch. */
11712 continue;
11713 }
11714
11715 /* Get the contents of the section. They have been cached by a
11716 relaxation routine. Note that o is a section in an input
11717 file, so the contents field will not have been set by any of
11718 the routines which work on output files. */
11719 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
11720 {
11721 contents = elf_section_data (o)->this_hdr.contents;
11722 if (bed->caches_rawsize
11723 && o->rawsize != 0
11724 && o->rawsize < o->size)
11725 {
11726 memcpy (flinfo->contents, contents, o->rawsize);
11727 contents = flinfo->contents;
11728 }
11729 }
1a528d3e
MM
11730 else if (!(o->flags & SEC_RELOC)
11731 && !bed->elf_backend_write_section
11732 && o->sec_info_type == SEC_INFO_TYPE_MERGE)
11733 /* A MERGE section that has no relocations doesn't need the
11734 contents anymore, they have been recorded earlier. Except
11735 if the backend has special provisions for writing sections. */
11736 contents = NULL;
c152c796
AM
11737 else
11738 {
8b127cbc 11739 contents = flinfo->contents;
a9505c74
L
11740 if (! _bfd_elf_link_mmap_section_contents (input_bfd, o,
11741 &contents))
0a1b45a2 11742 return false;
c152c796
AM
11743 }
11744
11745 if ((o->flags & SEC_RELOC) != 0)
11746 {
11747 Elf_Internal_Rela *internal_relocs;
0f02bbd9 11748 Elf_Internal_Rela *rel, *relend;
0f02bbd9 11749 int action_discarded;
ece5ef60 11750 int ret;
c152c796
AM
11751
11752 /* Get the swapped relocs. */
11753 internal_relocs
a8dde0a2
L
11754 = _bfd_elf_link_info_read_relocs (input_bfd, flinfo->info, o,
11755 flinfo->external_relocs,
11756 flinfo->internal_relocs,
11757 false);
c152c796
AM
11758 if (internal_relocs == NULL
11759 && o->reloc_count > 0)
0a1b45a2 11760 return false;
c152c796 11761
0f02bbd9 11762 action_discarded = -1;
c152c796 11763 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
11764 action_discarded = (*bed->action_discarded) (o);
11765
11766 /* Run through the relocs evaluating complex reloc symbols and
11767 looking for relocs against symbols from discarded sections
11768 or section symbols from removed link-once sections.
11769 Complain about relocs against discarded sections. Zero
11770 relocs against removed link-once sections. */
11771
11772 rel = internal_relocs;
056bafd4 11773 relend = rel + o->reloc_count;
0f02bbd9 11774 for ( ; rel < relend; rel++)
c152c796 11775 {
0f02bbd9
AM
11776 unsigned long r_symndx = rel->r_info >> r_sym_shift;
11777 unsigned int s_type;
11778 asection **ps, *sec;
11779 struct elf_link_hash_entry *h = NULL;
11780 const char *sym_name;
c152c796 11781
0f02bbd9
AM
11782 if (r_symndx == STN_UNDEF)
11783 continue;
c152c796 11784
0f02bbd9
AM
11785 if (r_symndx >= locsymcount
11786 || (elf_bad_symtab (input_bfd)
8b127cbc 11787 && flinfo->sections[r_symndx] == NULL))
0f02bbd9 11788 {
931494c9 11789 h = get_link_hash_entry (sym_hashes, r_symndx, extsymoff);
ee75fd95 11790
0f02bbd9
AM
11791 /* Badly formatted input files can contain relocs that
11792 reference non-existant symbols. Check here so that
11793 we do not seg fault. */
11794 if (h == NULL)
c152c796 11795 {
4eca0228 11796 _bfd_error_handler
695344c0 11797 /* xgettext:c-format */
931494c9 11798 (_("error: %pB contains a reloc (%#" PRIx64 ") for section '%pA' "
0f02bbd9 11799 "that references a non-existent global symbol"),
2dcf00ce 11800 input_bfd, (uint64_t) rel->r_info, o);
0f02bbd9 11801 bfd_set_error (bfd_error_bad_value);
0a1b45a2 11802 return false;
0f02bbd9 11803 }
3b36f7e6 11804
0f02bbd9 11805 s_type = h->type;
cdd3575c 11806
9e2dec47 11807 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
11808 mark the symbol as undefined. Note that the
11809 linker may attach linker created dynamic sections
11810 to the plugin bfd. Symbols defined in linker
11811 created sections are not plugin symbols. */
bc4e12de 11812 if ((h->root.non_ir_ref_regular
4070765b 11813 || h->root.non_ir_ref_dynamic)
9e2dec47
L
11814 && (h->root.type == bfd_link_hash_defined
11815 || h->root.type == bfd_link_hash_defweak)
11816 && (h->root.u.def.section->flags
11817 & SEC_LINKER_CREATED) == 0
11818 && h->root.u.def.section->owner != NULL
11819 && (h->root.u.def.section->owner->flags
11820 & BFD_PLUGIN) != 0)
11821 {
11822 h->root.type = bfd_link_hash_undefined;
11823 h->root.u.undef.abfd = h->root.u.def.section->owner;
11824 }
11825
0f02bbd9
AM
11826 ps = NULL;
11827 if (h->root.type == bfd_link_hash_defined
11828 || h->root.type == bfd_link_hash_defweak)
11829 ps = &h->root.u.def.section;
11830
11831 sym_name = h->root.root.string;
11832 }
11833 else
11834 {
11835 Elf_Internal_Sym *sym = isymbuf + r_symndx;
11836
11837 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 11838 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
11839 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11840 sym, *ps);
11841 }
c152c796 11842
c301e700 11843 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 11844 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
11845 {
11846 bfd_vma val;
11847 bfd_vma dot = (rel->r_offset
11848 + o->output_offset + o->output_section->vma);
11849#ifdef DEBUG
11850 printf ("Encountered a complex symbol!");
11851 printf (" (input_bfd %s, section %s, reloc %ld\n",
765cf5f6 11852 bfd_get_filename (input_bfd), o->name,
9ccb8af9 11853 (long) (rel - internal_relocs));
0f02bbd9
AM
11854 printf (" symbol: idx %8.8lx, name %s\n",
11855 r_symndx, sym_name);
11856 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11857 (unsigned long) rel->r_info,
11858 (unsigned long) rel->r_offset);
11859#endif
8b127cbc 11860 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9 11861 isymbuf, locsymcount, s_type == STT_SRELC))
0a1b45a2 11862 return false;
0f02bbd9
AM
11863
11864 /* Symbol evaluated OK. Update to absolute value. */
11865 set_symbol_value (input_bfd, isymbuf, locsymcount,
11866 r_symndx, val);
11867 continue;
11868 }
11869
11870 if (action_discarded != -1 && ps != NULL)
11871 {
cdd3575c
AM
11872 /* Complain if the definition comes from a
11873 discarded section. */
dbaa2011 11874 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 11875 {
cf35638d 11876 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 11877 if (action_discarded & COMPLAIN)
8b127cbc 11878 (*flinfo->info->callbacks->einfo)
695344c0 11879 /* xgettext:c-format */
871b3ab2
AM
11880 (_("%X`%s' referenced in section `%pA' of %pB: "
11881 "defined in discarded section `%pA' of %pB\n"),
e1fffbe6 11882 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 11883
87e5235d 11884 /* Try to do the best we can to support buggy old
e0ae6d6f 11885 versions of gcc. Pretend that the symbol is
87e5235d
AM
11886 really defined in the kept linkonce section.
11887 FIXME: This is quite broken. Modifying the
11888 symbol here means we will be changing all later
e0ae6d6f 11889 uses of the symbol, not just in this section. */
0f02bbd9 11890 if (action_discarded & PRETEND)
87e5235d 11891 {
01b3c8ab
L
11892 asection *kept;
11893
c0f00686 11894 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 11895 flinfo->info);
01b3c8ab 11896 if (kept != NULL)
87e5235d
AM
11897 {
11898 *ps = kept;
11899 continue;
11900 }
11901 }
c152c796
AM
11902 }
11903 }
11904 }
11905
11906 /* Relocate the section by invoking a back end routine.
11907
11908 The back end routine is responsible for adjusting the
11909 section contents as necessary, and (if using Rela relocs
11910 and generating a relocatable output file) adjusting the
11911 reloc addend as necessary.
11912
11913 The back end routine does not have to worry about setting
11914 the reloc address or the reloc symbol index.
11915
11916 The back end routine is given a pointer to the swapped in
11917 internal symbols, and can access the hash table entries
11918 for the external symbols via elf_sym_hashes (input_bfd).
11919
11920 When generating relocatable output, the back end routine
11921 must handle STB_LOCAL/STT_SECTION symbols specially. The
11922 output symbol is going to be a section symbol
11923 corresponding to the output section, which will require
11924 the addend to be adjusted. */
11925
8b127cbc 11926 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
11927 input_bfd, o, contents,
11928 internal_relocs,
11929 isymbuf,
8b127cbc 11930 flinfo->sections);
ece5ef60 11931 if (!ret)
0a1b45a2 11932 return false;
c152c796 11933
ece5ef60 11934 if (ret == 2
0e1862bb 11935 || bfd_link_relocatable (flinfo->info)
8b127cbc 11936 || flinfo->info->emitrelocations)
c152c796
AM
11937 {
11938 Elf_Internal_Rela *irela;
d4730f92 11939 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
11940 bfd_vma last_offset;
11941 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
11942 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11943 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 11944 unsigned int next_erel;
0a1b45a2 11945 bool rela_normal;
d4730f92 11946 struct bfd_elf_section_data *esdi, *esdo;
c152c796 11947
d4730f92
BS
11948 esdi = elf_section_data (o);
11949 esdo = elf_section_data (o->output_section);
0a1b45a2 11950 rela_normal = false;
c152c796
AM
11951
11952 /* Adjust the reloc addresses and symbol indices. */
11953
11954 irela = internal_relocs;
056bafd4 11955 irelaend = irela + o->reloc_count;
36f61bf2 11956 rel_hash = PTR_ADD (esdo->rel.hashes, esdo->rel.count);
d4730f92
BS
11957 /* We start processing the REL relocs, if any. When we reach
11958 IRELAMID in the loop, we switch to the RELA relocs. */
11959 irelamid = irela;
11960 if (esdi->rel.hdr != NULL)
11961 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11962 * bed->s->int_rels_per_ext_rel);
eac338cf 11963 rel_hash_list = rel_hash;
d4730f92 11964 rela_hash_list = NULL;
c152c796 11965 last_offset = o->output_offset;
0e1862bb 11966 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11967 last_offset += o->output_section->vma;
11968 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11969 {
11970 unsigned long r_symndx;
11971 asection *sec;
11972 Elf_Internal_Sym sym;
11973
11974 if (next_erel == bed->s->int_rels_per_ext_rel)
11975 {
11976 rel_hash++;
11977 next_erel = 0;
11978 }
11979
d4730f92
BS
11980 if (irela == irelamid)
11981 {
36f61bf2 11982 rel_hash = PTR_ADD (esdo->rela.hashes, esdo->rela.count);
d4730f92 11983 rela_hash_list = rel_hash;
5f4fa40e 11984 rela_normal = bed->rela_normal;
d4730f92
BS
11985 }
11986
c152c796 11987 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 11988 flinfo->info, o,
c152c796
AM
11989 irela->r_offset);
11990 if (irela->r_offset >= (bfd_vma) -2)
11991 {
11992 /* This is a reloc for a deleted entry or somesuch.
11993 Turn it into an R_*_NONE reloc, at the same
11994 offset as the last reloc. elf_eh_frame.c and
e460dd0d 11995 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
11996 being ordered. */
11997 irela->r_offset = last_offset;
11998 irela->r_info = 0;
11999 irela->r_addend = 0;
12000 continue;
12001 }
12002
12003 irela->r_offset += o->output_offset;
12004
12005 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 12006 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
12007 irela->r_offset += o->output_section->vma;
12008
12009 last_offset = irela->r_offset;
12010
12011 r_symndx = irela->r_info >> r_sym_shift;
12012 if (r_symndx == STN_UNDEF)
12013 continue;
12014
12015 if (r_symndx >= locsymcount
12016 || (elf_bad_symtab (input_bfd)
8b127cbc 12017 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
12018 {
12019 struct elf_link_hash_entry *rh;
c152c796
AM
12020
12021 /* This is a reloc against a global symbol. We
12022 have not yet output all the local symbols, so
12023 we do not know the symbol index of any global
12024 symbol. We set the rel_hash entry for this
12025 reloc to point to the global hash table entry
12026 for this symbol. The symbol index is then
ee75fd95 12027 set at the end of bfd_elf_final_link. */
931494c9
NC
12028 rh = get_link_hash_entry (elf_sym_hashes (input_bfd),
12029 r_symndx, extsymoff);
12030 if (rh == NULL)
12031 {
12032 /* FIXME: Generate an error ? */
12033 continue;
12034 }
12035
12036 /* Setting the index to -2 tells elf_link_output_extsym
12037 that this symbol is used by a reloc. */
c152c796
AM
12038 BFD_ASSERT (rh->indx < 0);
12039 rh->indx = -2;
c152c796
AM
12040 *rel_hash = rh;
12041
12042 continue;
12043 }
12044
12045 /* This is a reloc against a local symbol. */
12046
12047 *rel_hash = NULL;
12048 sym = isymbuf[r_symndx];
8b127cbc 12049 sec = flinfo->sections[r_symndx];
c152c796
AM
12050 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
12051 {
12052 /* I suppose the backend ought to fill in the
12053 section of any STT_SECTION symbol against a
6a8d1586 12054 processor specific section. */
cf35638d 12055 r_symndx = STN_UNDEF;
6a8d1586
AM
12056 if (bfd_is_abs_section (sec))
12057 ;
c152c796
AM
12058 else if (sec == NULL || sec->owner == NULL)
12059 {
12060 bfd_set_error (bfd_error_bad_value);
0a1b45a2 12061 return false;
c152c796
AM
12062 }
12063 else
12064 {
6a8d1586
AM
12065 asection *osec = sec->output_section;
12066
12067 /* If we have discarded a section, the output
12068 section will be the absolute section. In
ab96bf03
AM
12069 case of discarded SEC_MERGE sections, use
12070 the kept section. relocate_section should
12071 have already handled discarded linkonce
12072 sections. */
6a8d1586
AM
12073 if (bfd_is_abs_section (osec)
12074 && sec->kept_section != NULL
12075 && sec->kept_section->output_section != NULL)
12076 {
12077 osec = sec->kept_section->output_section;
12078 irela->r_addend -= osec->vma;
12079 }
12080
12081 if (!bfd_is_abs_section (osec))
12082 {
12083 r_symndx = osec->target_index;
cf35638d 12084 if (r_symndx == STN_UNDEF)
74541ad4 12085 {
051d833a
AM
12086 irela->r_addend += osec->vma;
12087 osec = _bfd_nearby_section (output_bfd, osec,
12088 osec->vma);
12089 irela->r_addend -= osec->vma;
12090 r_symndx = osec->target_index;
74541ad4 12091 }
6a8d1586 12092 }
c152c796
AM
12093 }
12094
12095 /* Adjust the addend according to where the
12096 section winds up in the output section. */
12097 if (rela_normal)
12098 irela->r_addend += sec->output_offset;
12099 }
12100 else
12101 {
8b127cbc 12102 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
12103 {
12104 unsigned long shlink;
12105 const char *name;
12106 asection *osec;
6e0b88f1 12107 long indx;
c152c796 12108
8b127cbc 12109 if (flinfo->info->strip == strip_all)
c152c796
AM
12110 {
12111 /* You can't do ld -r -s. */
12112 bfd_set_error (bfd_error_invalid_operation);
0a1b45a2 12113 return false;
c152c796
AM
12114 }
12115
12116 /* This symbol was skipped earlier, but
12117 since it is needed by a reloc, we
12118 must output it now. */
12119 shlink = symtab_hdr->sh_link;
12120 name = (bfd_elf_string_from_elf_section
12121 (input_bfd, shlink, sym.st_name));
12122 if (name == NULL)
0a1b45a2 12123 return false;
c152c796
AM
12124
12125 osec = sec->output_section;
12126 sym.st_shndx =
12127 _bfd_elf_section_from_bfd_section (output_bfd,
12128 osec);
12129 if (sym.st_shndx == SHN_BAD)
0a1b45a2 12130 return false;
c152c796
AM
12131
12132 sym.st_value += sec->output_offset;
0e1862bb 12133 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
12134 {
12135 sym.st_value += osec->vma;
12136 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
12137 {
102def4d
AM
12138 struct elf_link_hash_table *htab
12139 = elf_hash_table (flinfo->info);
12140
c152c796
AM
12141 /* STT_TLS symbols are relative to PT_TLS
12142 segment base. */
102def4d
AM
12143 if (htab->tls_sec != NULL)
12144 sym.st_value -= htab->tls_sec->vma;
12145 else
12146 sym.st_info
12147 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
12148 STT_NOTYPE);
c152c796
AM
12149 }
12150 }
12151
6e0b88f1 12152 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
12153 ret = elf_link_output_symstrtab (flinfo, name,
12154 &sym, sec,
12155 NULL);
6e0b88f1 12156 if (ret == 0)
0a1b45a2 12157 return false;
6e0b88f1 12158 else if (ret == 1)
8b127cbc 12159 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
12160 else
12161 abort ();
c152c796
AM
12162 }
12163
8b127cbc 12164 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
12165 }
12166
12167 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
12168 | (irela->r_info & r_type_mask));
12169 }
12170
12171 /* Swap out the relocs. */
d4730f92
BS
12172 input_rel_hdr = esdi->rel.hdr;
12173 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 12174 {
d4730f92
BS
12175 if (!bed->elf_backend_emit_relocs (output_bfd, o,
12176 input_rel_hdr,
12177 internal_relocs,
12178 rel_hash_list))
0a1b45a2 12179 return false;
c152c796
AM
12180 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
12181 * bed->s->int_rels_per_ext_rel);
eac338cf 12182 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
12183 }
12184
12185 input_rela_hdr = esdi->rela.hdr;
12186 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
12187 {
eac338cf 12188 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 12189 input_rela_hdr,
eac338cf 12190 internal_relocs,
d4730f92 12191 rela_hash_list))
0a1b45a2 12192 return false;
c152c796
AM
12193 }
12194 }
12195 }
12196
12197 /* Write out the modified section contents. */
12198 if (bed->elf_backend_write_section
8b127cbc 12199 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 12200 contents))
c152c796
AM
12201 {
12202 /* Section written out. */
12203 }
12204 else switch (o->sec_info_type)
12205 {
dbaa2011 12206 case SEC_INFO_TYPE_STABS:
c152c796
AM
12207 if (! (_bfd_write_section_stabs
12208 (output_bfd,
8b127cbc 12209 &elf_hash_table (flinfo->info)->stab_info,
c152c796 12210 o, &elf_section_data (o)->sec_info, contents)))
0a1b45a2 12211 return false;
c152c796 12212 break;
dbaa2011 12213 case SEC_INFO_TYPE_MERGE:
c152c796
AM
12214 if (! _bfd_write_merged_section (output_bfd, o,
12215 elf_section_data (o)->sec_info))
0a1b45a2 12216 return false;
c152c796 12217 break;
dbaa2011 12218 case SEC_INFO_TYPE_EH_FRAME:
c152c796 12219 {
8b127cbc 12220 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796 12221 o, contents))
0a1b45a2 12222 return false;
c152c796
AM
12223 }
12224 break;
2f0c68f2
CM
12225 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
12226 {
12227 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
12228 flinfo->info,
12229 o, contents))
0a1b45a2 12230 return false;
2f0c68f2
CM
12231 }
12232 break;
cf0e0a0b
IB
12233 case SEC_INFO_TYPE_SFRAME:
12234 {
3ba8e337
IB
12235 /* Merge SFrame section into the SFrame encoder context of the
12236 output_bfd's section. The final .sframe output section will
12237 be written out later. */
cf0e0a0b
IB
12238 if (!_bfd_elf_merge_section_sframe (output_bfd, flinfo->info,
12239 o, contents))
12240 return false;
12241 }
12242 break;
c152c796
AM
12243 default:
12244 {
310fd250
L
12245 if (! (o->flags & SEC_EXCLUDE))
12246 {
12247 file_ptr offset = (file_ptr) o->output_offset;
12248 bfd_size_type todo = o->size;
37b01f6a 12249
bb294208 12250 offset *= bfd_octets_per_byte (output_bfd, o);
37b01f6a 12251
b02db378
AM
12252 if ((o->flags & SEC_ELF_REVERSE_COPY)
12253 && o->size > address_size)
310fd250
L
12254 {
12255 /* Reverse-copy input section to output. */
b02db378 12256
0ae12467
AM
12257 if ((o->size & (address_size - 1)) != 0
12258 || (o->reloc_count != 0
12259 && (o->size * bed->s->int_rels_per_ext_rel
12260 != o->reloc_count * address_size)))
b02db378
AM
12261 {
12262 _bfd_error_handler
12263 /* xgettext:c-format */
12264 (_("error: %pB: size of section %pA is not "
12265 "multiple of address size"),
12266 input_bfd, o);
12267 bfd_set_error (bfd_error_bad_value);
12268 return false;
12269 }
12270
310fd250
L
12271 do
12272 {
12273 todo -= address_size;
12274 if (! bfd_set_section_contents (output_bfd,
12275 o->output_section,
12276 contents + todo,
12277 offset,
12278 address_size))
0a1b45a2 12279 return false;
310fd250
L
12280 if (todo == 0)
12281 break;
12282 offset += address_size;
12283 }
12284 while (1);
12285 }
12286 else if (! bfd_set_section_contents (output_bfd,
12287 o->output_section,
12288 contents,
12289 offset, todo))
0a1b45a2 12290 return false;
310fd250 12291 }
c152c796
AM
12292 }
12293 break;
12294 }
a9505c74
L
12295
12296 /* Munmap the section contents for each input section. */
12297 _bfd_elf_link_munmap_section_contents (o);
c152c796
AM
12298 }
12299
0a1b45a2 12300 return true;
c152c796
AM
12301}
12302
12303/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 12304 requested by the linker, and does not come from any input file. This
c152c796
AM
12305 is used to build constructor and destructor tables when linking
12306 with -Ur. */
12307
0a1b45a2 12308static bool
c152c796
AM
12309elf_reloc_link_order (bfd *output_bfd,
12310 struct bfd_link_info *info,
12311 asection *output_section,
12312 struct bfd_link_order *link_order)
12313{
12314 reloc_howto_type *howto;
12315 long indx;
12316 bfd_vma offset;
12317 bfd_vma addend;
d4730f92 12318 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
12319 struct elf_link_hash_entry **rel_hash_ptr;
12320 Elf_Internal_Shdr *rel_hdr;
12321 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
12322 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
12323 bfd_byte *erel;
12324 unsigned int i;
d4730f92 12325 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
12326
12327 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
12328 if (howto == NULL)
12329 {
12330 bfd_set_error (bfd_error_bad_value);
0a1b45a2 12331 return false;
c152c796
AM
12332 }
12333
12334 addend = link_order->u.reloc.p->addend;
12335
d4730f92
BS
12336 if (esdo->rel.hdr)
12337 reldata = &esdo->rel;
12338 else if (esdo->rela.hdr)
12339 reldata = &esdo->rela;
12340 else
12341 {
12342 reldata = NULL;
12343 BFD_ASSERT (0);
12344 }
12345
c152c796 12346 /* Figure out the symbol index. */
d4730f92 12347 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
12348 if (link_order->type == bfd_section_reloc_link_order)
12349 {
12350 indx = link_order->u.reloc.p->u.section->target_index;
12351 BFD_ASSERT (indx != 0);
12352 *rel_hash_ptr = NULL;
12353 }
12354 else
12355 {
12356 struct elf_link_hash_entry *h;
12357
12358 /* Treat a reloc against a defined symbol as though it were
12359 actually against the section. */
12360 h = ((struct elf_link_hash_entry *)
12361 bfd_wrapped_link_hash_lookup (output_bfd, info,
12362 link_order->u.reloc.p->u.name,
0a1b45a2 12363 false, false, true));
c152c796
AM
12364 if (h != NULL
12365 && (h->root.type == bfd_link_hash_defined
12366 || h->root.type == bfd_link_hash_defweak))
12367 {
12368 asection *section;
12369
12370 section = h->root.u.def.section;
12371 indx = section->output_section->target_index;
12372 *rel_hash_ptr = NULL;
12373 /* It seems that we ought to add the symbol value to the
12374 addend here, but in practice it has already been added
12375 because it was passed to constructor_callback. */
12376 addend += section->output_section->vma + section->output_offset;
12377 }
12378 else if (h != NULL)
12379 {
12380 /* Setting the index to -2 tells elf_link_output_extsym that
12381 this symbol is used by a reloc. */
12382 h->indx = -2;
12383 *rel_hash_ptr = h;
12384 indx = 0;
12385 }
12386 else
12387 {
1a72702b
AM
12388 (*info->callbacks->unattached_reloc)
12389 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
c152c796
AM
12390 indx = 0;
12391 }
12392 }
12393
12394 /* If this is an inplace reloc, we must write the addend into the
12395 object file. */
12396 if (howto->partial_inplace && addend != 0)
12397 {
12398 bfd_size_type size;
12399 bfd_reloc_status_type rstat;
12400 bfd_byte *buf;
0a1b45a2 12401 bool ok;
c152c796 12402 const char *sym_name;
bb294208 12403 bfd_size_type octets;
c152c796 12404
a50b1753
NC
12405 size = (bfd_size_type) bfd_get_reloc_size (howto);
12406 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 12407 if (buf == NULL && size != 0)
0a1b45a2 12408 return false;
c152c796
AM
12409 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
12410 switch (rstat)
12411 {
12412 case bfd_reloc_ok:
12413 break;
12414
12415 default:
12416 case bfd_reloc_outofrange:
12417 abort ();
12418
12419 case bfd_reloc_overflow:
12420 if (link_order->type == bfd_section_reloc_link_order)
fd361982 12421 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
c152c796
AM
12422 else
12423 sym_name = link_order->u.reloc.p->u.name;
1a72702b
AM
12424 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
12425 howto->name, addend, NULL, NULL,
12426 (bfd_vma) 0);
c152c796
AM
12427 break;
12428 }
37b01f6a 12429
bb294208
AM
12430 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
12431 output_section);
c152c796 12432 ok = bfd_set_section_contents (output_bfd, output_section, buf,
bb294208 12433 octets, size);
c152c796
AM
12434 free (buf);
12435 if (! ok)
0a1b45a2 12436 return false;
c152c796
AM
12437 }
12438
12439 /* The address of a reloc is relative to the section in a
12440 relocatable file, and is a virtual address in an executable
12441 file. */
12442 offset = link_order->offset;
0e1862bb 12443 if (! bfd_link_relocatable (info))
c152c796
AM
12444 offset += output_section->vma;
12445
12446 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
12447 {
12448 irel[i].r_offset = offset;
12449 irel[i].r_info = 0;
12450 irel[i].r_addend = 0;
12451 }
12452 if (bed->s->arch_size == 32)
12453 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
12454 else
12455 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
12456
d4730f92 12457 rel_hdr = reldata->hdr;
c152c796
AM
12458 erel = rel_hdr->contents;
12459 if (rel_hdr->sh_type == SHT_REL)
12460 {
d4730f92 12461 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
12462 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
12463 }
12464 else
12465 {
12466 irel[0].r_addend = addend;
d4730f92 12467 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
12468 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
12469 }
12470
d4730f92 12471 ++reldata->count;
c152c796 12472
0a1b45a2 12473 return true;
c152c796
AM
12474}
12475
76359541
TP
12476/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
12477 Returns TRUE upon success, FALSE otherwise. */
12478
0a1b45a2 12479static bool
76359541
TP
12480elf_output_implib (bfd *abfd, struct bfd_link_info *info)
12481{
0a1b45a2 12482 bool ret = false;
76359541
TP
12483 bfd *implib_bfd;
12484 const struct elf_backend_data *bed;
12485 flagword flags;
12486 enum bfd_architecture arch;
12487 unsigned int mach;
12488 asymbol **sympp = NULL;
12489 long symsize;
12490 long symcount;
12491 long src_count;
12492 elf_symbol_type *osymbuf;
446f7ed5 12493 size_t amt;
76359541
TP
12494
12495 implib_bfd = info->out_implib_bfd;
12496 bed = get_elf_backend_data (abfd);
12497
12498 if (!bfd_set_format (implib_bfd, bfd_object))
0a1b45a2 12499 return false;
76359541 12500
046734ff 12501 /* Use flag from executable but make it a relocatable object. */
76359541
TP
12502 flags = bfd_get_file_flags (abfd);
12503 flags &= ~HAS_RELOC;
12504 if (!bfd_set_start_address (implib_bfd, 0)
046734ff 12505 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
0a1b45a2 12506 return false;
76359541
TP
12507
12508 /* Copy architecture of output file to import library file. */
12509 arch = bfd_get_arch (abfd);
12510 mach = bfd_get_mach (abfd);
12511 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
12512 && (abfd->target_defaulted
12513 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
0a1b45a2 12514 return false;
76359541
TP
12515
12516 /* Get symbol table size. */
12517 symsize = bfd_get_symtab_upper_bound (abfd);
12518 if (symsize < 0)
0a1b45a2 12519 return false;
76359541
TP
12520
12521 /* Read in the symbol table. */
ec9bd0a2
AM
12522 sympp = (asymbol **) bfd_malloc (symsize);
12523 if (sympp == NULL)
0a1b45a2 12524 return false;
ec9bd0a2 12525
76359541
TP
12526 symcount = bfd_canonicalize_symtab (abfd, sympp);
12527 if (symcount < 0)
12528 goto free_sym_buf;
12529
12530 /* Allow the BFD backend to copy any private header data it
12531 understands from the output BFD to the import library BFD. */
12532 if (! bfd_copy_private_header_data (abfd, implib_bfd))
12533 goto free_sym_buf;
12534
12535 /* Filter symbols to appear in the import library. */
12536 if (bed->elf_backend_filter_implib_symbols)
12537 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
12538 symcount);
12539 else
12540 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
12541 if (symcount == 0)
12542 {
5df1bc57 12543 bfd_set_error (bfd_error_no_symbols);
871b3ab2 12544 _bfd_error_handler (_("%pB: no symbol found for import library"),
4eca0228 12545 implib_bfd);
76359541
TP
12546 goto free_sym_buf;
12547 }
12548
12549
12550 /* Make symbols absolute. */
446f7ed5
AM
12551 amt = symcount * sizeof (*osymbuf);
12552 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
ec9bd0a2
AM
12553 if (osymbuf == NULL)
12554 goto free_sym_buf;
12555
76359541
TP
12556 for (src_count = 0; src_count < symcount; src_count++)
12557 {
12558 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
12559 sizeof (*osymbuf));
12560 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
12561 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
12562 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
12563 osymbuf[src_count].internal_elf_sym.st_value =
12564 osymbuf[src_count].symbol.value;
12565 sympp[src_count] = &osymbuf[src_count].symbol;
12566 }
12567
12568 bfd_set_symtab (implib_bfd, sympp, symcount);
12569
12570 /* Allow the BFD backend to copy any private data it understands
12571 from the output BFD to the import library BFD. This is done last
12572 to permit the routine to look at the filtered symbol table. */
12573 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
12574 goto free_sym_buf;
12575
12576 if (!bfd_close (implib_bfd))
12577 goto free_sym_buf;
12578
0a1b45a2 12579 ret = true;
76359541 12580
dc1e8a47 12581 free_sym_buf:
76359541
TP
12582 free (sympp);
12583 return ret;
12584}
12585
9f7c3e5e
AM
12586static void
12587elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
12588{
12589 asection *o;
12590
12591 if (flinfo->symstrtab != NULL)
ef10c3ac 12592 _bfd_elf_strtab_free (flinfo->symstrtab);
c9594989
AM
12593 free (flinfo->contents);
12594 free (flinfo->external_relocs);
12595 free (flinfo->internal_relocs);
12596 free (flinfo->external_syms);
12597 free (flinfo->locsym_shndx);
12598 free (flinfo->internal_syms);
12599 free (flinfo->indices);
12600 free (flinfo->sections);
12601 if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
9f7c3e5e
AM
12602 free (flinfo->symshndxbuf);
12603 for (o = obfd->sections; o != NULL; o = o->next)
12604 {
12605 struct bfd_elf_section_data *esdo = elf_section_data (o);
c9594989
AM
12606 free (esdo->rel.hashes);
12607 free (esdo->rela.hashes);
9f7c3e5e
AM
12608 }
12609}
0b52efa6 12610
c152c796
AM
12611/* Do the final step of an ELF link. */
12612
0a1b45a2 12613bool
c152c796
AM
12614bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
12615{
0a1b45a2
AM
12616 bool dynamic;
12617 bool emit_relocs;
c152c796 12618 bfd *dynobj;
8b127cbc 12619 struct elf_final_link_info flinfo;
91d6fa6a
NC
12620 asection *o;
12621 struct bfd_link_order *p;
12622 bfd *sub;
c152c796
AM
12623 bfd_size_type max_contents_size;
12624 bfd_size_type max_external_reloc_size;
12625 bfd_size_type max_internal_reloc_count;
12626 bfd_size_type max_sym_count;
12627 bfd_size_type max_sym_shndx_count;
c152c796
AM
12628 Elf_Internal_Sym elfsym;
12629 unsigned int i;
12630 Elf_Internal_Shdr *symtab_hdr;
12631 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
12632 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12633 struct elf_outext_info eoinfo;
0a1b45a2 12634 bool merged;
9c3d7d9b
AM
12635 size_t relativecount;
12636 size_t relr_entsize;
c152c796
AM
12637 asection *reldyn = 0;
12638 bfd_size_type amt;
64f52338 12639 struct elf_link_hash_table *htab = elf_hash_table (info);
0a1b45a2 12640 bool sections_removed;
c152c796 12641
2cc15b10 12642 if (!is_elf_hash_table (&htab->root))
0a1b45a2 12643 return false;
c152c796 12644
0e1862bb 12645 if (bfd_link_pic (info))
c152c796
AM
12646 abfd->flags |= DYNAMIC;
12647
64f52338
AM
12648 dynamic = htab->dynamic_sections_created;
12649 dynobj = htab->dynobj;
c152c796 12650
0e1862bb 12651 emit_relocs = (bfd_link_relocatable (info)
a4676736 12652 || info->emitrelocations);
c152c796 12653
496afd17 12654 memset (&flinfo, 0, sizeof (flinfo));
8b127cbc
AM
12655 flinfo.info = info;
12656 flinfo.output_bfd = abfd;
ef10c3ac 12657 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 12658 if (flinfo.symstrtab == NULL)
0a1b45a2 12659 return false;
c152c796
AM
12660
12661 if (! dynamic)
12662 {
8b127cbc
AM
12663 flinfo.hash_sec = NULL;
12664 flinfo.symver_sec = NULL;
c152c796
AM
12665 }
12666 else
12667 {
3d4d4302 12668 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 12669 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 12670 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
12671 /* Note that it is OK if symver_sec is NULL. */
12672 }
12673
496afd17
L
12674 if (info->unique_symbol
12675 && !bfd_hash_table_init (&flinfo.local_hash_table,
12676 local_hash_newfunc,
12677 sizeof (struct local_hash_entry)))
0a1b45a2 12678 return false;
c152c796 12679
104d59d1
JM
12680 /* The object attributes have been merged. Remove the input
12681 sections from the link, and set the contents of the output
1ff6de03 12682 section. */
0a1b45a2 12683 sections_removed = false;
011e5014 12684 const char *obj_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
104d59d1
JM
12685 for (o = abfd->sections; o != NULL; o = o->next)
12686 {
0a1b45a2 12687 bool remove_section = false;
b8a6ced7 12688
011e5014 12689 if ((obj_attrs_section && strcmp (o->name, obj_attrs_section) == 0)
104d59d1
JM
12690 || strcmp (o->name, ".gnu.attributes") == 0)
12691 {
12692 for (p = o->map_head.link_order; p != NULL; p = p->next)
12693 {
12694 asection *input_section;
12695
12696 if (p->type != bfd_indirect_link_order)
12697 continue;
12698 input_section = p->u.indirect.section;
12699 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12700 elf_link_input_bfd ignores this section. */
12701 input_section->flags &= ~SEC_HAS_CONTENTS;
12702 }
a0c8462f 12703
b8a6ced7
AM
12704 /* Skip this section later on. */
12705 o->map_head.link_order = NULL;
011e5014
ML
12706
12707 bfd_vma attr_size = bfd_elf_obj_attr_size (abfd);
12708 /* Once ELF headers have been written, the size of a section is
12709 frozen. We need to set the size of the attribute section before
12710 _bfd_elf_compute_section_file_positions. */
12711 bfd_set_section_size (o, attr_size);
12712 if (attr_size > 0)
12713 elf_obj_build_attributes (abfd) = o;
104d59d1 12714 else
0a1b45a2 12715 remove_section = true;
104d59d1 12716 }
6e5e9d58
AM
12717 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12718 {
12719 /* Remove empty group section from linker output. */
0a1b45a2 12720 remove_section = true;
b8a6ced7 12721 }
5270eddc 12722 if (remove_section)
b8a6ced7 12723 {
6e5e9d58
AM
12724 o->flags |= SEC_EXCLUDE;
12725 bfd_section_list_remove (abfd, o);
12726 abfd->section_count--;
0a1b45a2 12727 sections_removed = true;
6e5e9d58 12728 }
104d59d1 12729 }
4c6ee646
AM
12730 if (sections_removed)
12731 _bfd_fix_excluded_sec_syms (abfd, info);
104d59d1 12732
c152c796
AM
12733 /* Count up the number of relocations we will output for each output
12734 section, so that we know the sizes of the reloc sections. We
12735 also figure out some maximum sizes. */
c6291d74 12736#ifdef USE_MMAP
b43b3528
AM
12737 if (bed->use_mmap)
12738 {
12739 /* Mmap is used only if section size >= the minimum mmap section
12740 size. The initial max_contents_size value covers all sections
12741 smaller than the minimum mmap section size. It may be increased
12742 for compressed or linker created sections or sections whose
12743 rawsize != size. max_external_reloc_size covers all relocation
12744 sections smaller than the minimum mmap section size. */
12745 max_contents_size = _bfd_minimum_mmap_size;
12746 max_external_reloc_size = _bfd_minimum_mmap_size;
12747 }
12748 else
c6291d74 12749#endif
b43b3528
AM
12750 {
12751 max_contents_size = 0;
12752 max_external_reloc_size = 0;
12753 }
c152c796
AM
12754 max_internal_reloc_count = 0;
12755 max_sym_count = 0;
12756 max_sym_shndx_count = 0;
0a1b45a2 12757 merged = false;
c152c796
AM
12758 for (o = abfd->sections; o != NULL; o = o->next)
12759 {
12760 struct bfd_elf_section_data *esdo = elf_section_data (o);
12761 o->reloc_count = 0;
12762
8423293d 12763 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
12764 {
12765 unsigned int reloc_count = 0;
9eaff861 12766 unsigned int additional_reloc_count = 0;
c152c796 12767 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
12768
12769 if (p->type == bfd_section_reloc_link_order
12770 || p->type == bfd_symbol_reloc_link_order)
12771 reloc_count = 1;
12772 else if (p->type == bfd_indirect_link_order)
12773 {
12774 asection *sec;
12775
12776 sec = p->u.indirect.section;
c152c796
AM
12777
12778 /* Mark all sections which are to be included in the
12779 link. This will normally be every section. We need
12780 to do this so that we can identify any sections which
12781 the linker has decided to not include. */
0a1b45a2 12782 sec->linker_mark = true;
c152c796
AM
12783
12784 if (sec->flags & SEC_MERGE)
0a1b45a2 12785 merged = true;
c152c796 12786
a9505c74
L
12787#ifdef USE_MMAP
12788 /* Mmap is used only on non-compressed, non-linker created
12789 sections whose rawsize == size. */
b43b3528
AM
12790 if (!bed->use_mmap
12791 || sec->compress_status != COMPRESS_SECTION_NONE
12792 || (sec->flags & SEC_LINKER_CREATED) != 0
12793 || sec->rawsize != sec->size)
a9505c74
L
12794#endif
12795 {
12796 if (sec->rawsize > max_contents_size)
12797 max_contents_size = sec->rawsize;
12798 if (sec->size > max_contents_size)
12799 max_contents_size = sec->size;
12800 }
c152c796 12801
c152c796
AM
12802 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12803 && (sec->owner->flags & DYNAMIC) == 0)
12804 {
12805 size_t sym_count;
12806
a961cdd5
AM
12807 /* We are interested in just local symbols, not all
12808 symbols. */
c152c796
AM
12809 if (elf_bad_symtab (sec->owner))
12810 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12811 / bed->s->sizeof_sym);
12812 else
12813 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12814
12815 if (sym_count > max_sym_count)
12816 max_sym_count = sym_count;
12817
12818 if (sym_count > max_sym_shndx_count
6a40cf0c 12819 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
12820 max_sym_shndx_count = sym_count;
12821
35c5dcc6
AM
12822 esdi = elf_section_data (sec);
12823
12824 if (esdi->this_hdr.sh_type == SHT_REL
12825 || esdi->this_hdr.sh_type == SHT_RELA)
a961cdd5
AM
12826 /* Some backends use reloc_count in relocation sections
12827 to count particular types of relocs. Of course,
12828 reloc sections themselves can't have relocations. */
12829 ;
12830 else if (emit_relocs)
12831 {
12832 reloc_count = sec->reloc_count;
12833 if (bed->elf_backend_count_additional_relocs)
12834 {
12835 int c;
12836 c = (*bed->elf_backend_count_additional_relocs) (sec);
12837 additional_reloc_count += c;
12838 }
12839 }
12840 else if (bed->elf_backend_count_relocs)
12841 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12842
c152c796
AM
12843 if ((sec->flags & SEC_RELOC) != 0)
12844 {
b43b3528
AM
12845#ifdef USE_MMAP
12846 if (!bed->use_mmap)
12847#endif
12848 {
12849 size_t ext_size = 0;
c152c796 12850
b43b3528
AM
12851 if (esdi->rel.hdr != NULL)
12852 ext_size = esdi->rel.hdr->sh_size;
12853 if (esdi->rela.hdr != NULL)
12854 ext_size += esdi->rela.hdr->sh_size;
7326c758 12855
b43b3528
AM
12856 if (ext_size > max_external_reloc_size)
12857 max_external_reloc_size = ext_size;
12858 }
c152c796
AM
12859 if (sec->reloc_count > max_internal_reloc_count)
12860 max_internal_reloc_count = sec->reloc_count;
12861 }
12862 }
12863 }
12864
12865 if (reloc_count == 0)
12866 continue;
12867
9eaff861 12868 reloc_count += additional_reloc_count;
c152c796
AM
12869 o->reloc_count += reloc_count;
12870
0e1862bb 12871 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 12872 {
d4730f92 12873 if (esdi->rel.hdr)
9eaff861 12874 {
491d01d3 12875 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
9eaff861
AO
12876 esdo->rel.count += additional_reloc_count;
12877 }
d4730f92 12878 if (esdi->rela.hdr)
9eaff861 12879 {
491d01d3 12880 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
9eaff861
AO
12881 esdo->rela.count += additional_reloc_count;
12882 }
d4730f92
BS
12883 }
12884 else
12885 {
12886 if (o->use_rela_p)
12887 esdo->rela.count += reloc_count;
2c2b4ed4 12888 else
d4730f92 12889 esdo->rel.count += reloc_count;
c152c796 12890 }
c152c796
AM
12891 }
12892
9eaff861 12893 if (o->reloc_count > 0)
c152c796
AM
12894 o->flags |= SEC_RELOC;
12895 else
12896 {
12897 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12898 set it (this is probably a bug) and if it is set
12899 assign_section_numbers will create a reloc section. */
12900 o->flags &=~ SEC_RELOC;
12901 }
12902
12903 /* If the SEC_ALLOC flag is not set, force the section VMA to
12904 zero. This is done in elf_fake_sections as well, but forcing
12905 the VMA to 0 here will ensure that relocs against these
12906 sections are handled correctly. */
12907 if ((o->flags & SEC_ALLOC) == 0
12908 && ! o->user_set_vma)
12909 o->vma = 0;
12910 }
12911
0e1862bb 12912 if (! bfd_link_relocatable (info) && merged)
64f52338 12913 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
c152c796
AM
12914
12915 /* Figure out the file positions for everything but the symbol table
12916 and the relocs. We set symcount to force assign_section_numbers
12917 to create a symbol table. */
ed48ec2e 12918 abfd->symcount = info->strip != strip_all || emit_relocs;
c152c796
AM
12919 BFD_ASSERT (! abfd->output_has_begun);
12920 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12921 goto error_return;
12922
ee75fd95 12923 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
12924 for (o = abfd->sections; o != NULL; o = o->next)
12925 {
d4730f92 12926 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
12927 if ((o->flags & SEC_RELOC) != 0)
12928 {
d4730f92 12929 if (esdo->rel.hdr
9eaff861 12930 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
12931 goto error_return;
12932
d4730f92 12933 if (esdo->rela.hdr
9eaff861 12934 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
12935 goto error_return;
12936 }
12937
48db3297
AM
12938 /* _bfd_elf_compute_section_file_positions makes temporary use
12939 of target_index. Reset it. */
12940 o->target_index = 0;
12941
c152c796
AM
12942 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12943 to count upwards while actually outputting the relocations. */
d4730f92
BS
12944 esdo->rel.count = 0;
12945 esdo->rela.count = 0;
0ce398f1 12946
1ff6de03
NA
12947 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12948 && !bfd_section_is_ctf (o))
0ce398f1
L
12949 {
12950 /* Cache the section contents so that they can be compressed
12951 later. Use bfd_malloc since it will be freed by
12952 bfd_compress_section_contents. */
12953 unsigned char *contents = esdo->this_hdr.contents;
da16cc96 12954 if (contents != NULL)
0ce398f1
L
12955 abort ();
12956 contents
12957 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12958 if (contents == NULL)
12959 goto error_return;
12960 esdo->this_hdr.contents = contents;
12961 }
c152c796
AM
12962 }
12963
1ff6de03
NA
12964 /* We have now assigned file positions for all the sections except .symtab,
12965 .strtab, and non-loaded reloc and compressed debugging sections. We start
12966 the .symtab section at the current file position, and write directly to it.
12967 We build the .strtab section in memory. */
ed48ec2e 12968 abfd->symcount = 0;
c152c796
AM
12969 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12970 /* sh_name is set in prep_headers. */
12971 symtab_hdr->sh_type = SHT_SYMTAB;
12972 /* sh_flags, sh_addr and sh_size all start off zero. */
12973 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12974 /* sh_link is set in assign_section_numbers. */
12975 /* sh_info is set below. */
12976 /* sh_offset is set just below. */
72de5009 12977 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 12978
ef10c3ac
L
12979 if (max_sym_count < 20)
12980 max_sym_count = 20;
64f52338 12981 htab->strtabsize = max_sym_count;
ef10c3ac 12982 amt = max_sym_count * sizeof (struct elf_sym_strtab);
64f52338
AM
12983 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12984 if (htab->strtab == NULL)
c152c796 12985 goto error_return;
ef10c3ac
L
12986 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12987 flinfo.symshndxbuf
12988 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12989 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 12990
8539e4e8 12991 if (info->strip != strip_all || emit_relocs)
c152c796 12992 {
8539e4e8
AM
12993 file_ptr off = elf_next_file_pos (abfd);
12994
1f1b5e50 12995 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true, 0);
8539e4e8
AM
12996
12997 /* Note that at this point elf_next_file_pos (abfd) is
12998 incorrect. We do not yet know the size of the .symtab section.
12999 We correct next_file_pos below, after we do know the size. */
13000
13001 /* Start writing out the symbol table. The first symbol is always a
13002 dummy symbol. */
c152c796
AM
13003 elfsym.st_value = 0;
13004 elfsym.st_size = 0;
13005 elfsym.st_info = 0;
13006 elfsym.st_other = 0;
13007 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 13008 elfsym.st_target_internal = 0;
ef10c3ac
L
13009 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
13010 bfd_und_section_ptr, NULL) != 1)
c152c796 13011 goto error_return;
c152c796 13012
d1bcae83
L
13013 /* Output a symbol for each section if asked or they are used for
13014 relocs. These symbols usually have no names. We store the
13015 index of each one in the index field of the section, so that
13016 we can find it again when outputting relocs. */
c77cb2a0 13017
d1bcae83 13018 if (bfd_keep_unused_section_symbols (abfd) || emit_relocs)
c152c796 13019 {
0a1b45a2 13020 bool name_local_sections
d1bcae83
L
13021 = (bed->elf_backend_name_local_section_symbols
13022 && bed->elf_backend_name_local_section_symbols (abfd));
13023 const char *name = NULL;
13024
13025 elfsym.st_size = 0;
13026 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
13027 elfsym.st_other = 0;
13028 elfsym.st_value = 0;
13029 elfsym.st_target_internal = 0;
13030 for (i = 1; i < elf_numsections (abfd); i++)
f0b5bb34 13031 {
d1bcae83
L
13032 o = bfd_section_from_elf_index (abfd, i);
13033 if (o != NULL)
13034 {
13035 o->target_index = bfd_get_symcount (abfd);
13036 elfsym.st_shndx = i;
13037 if (!bfd_link_relocatable (info))
13038 elfsym.st_value = o->vma;
13039 if (name_local_sections)
13040 name = o->name;
13041 if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
13042 NULL) != 1)
13043 goto error_return;
13044 }
f0b5bb34 13045 }
c152c796
AM
13046 }
13047 }
13048
3f1b17bb
MR
13049 /* On some targets like Irix 5 the symbol split between local and global
13050 ones recorded in the sh_info field needs to be done between section
13051 and all other symbols. */
13052 if (bed->elf_backend_elfsym_local_is_section
13053 && bed->elf_backend_elfsym_local_is_section (abfd))
13054 symtab_hdr->sh_info = bfd_get_symcount (abfd);
13055
c152c796
AM
13056 /* Allocate some memory to hold information read in from the input
13057 files. */
13058 if (max_contents_size != 0)
13059 {
8b127cbc
AM
13060 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
13061 if (flinfo.contents == NULL)
c152c796
AM
13062 goto error_return;
13063 }
13064
13065 if (max_external_reloc_size != 0)
13066 {
8b127cbc
AM
13067 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
13068 if (flinfo.external_relocs == NULL)
c152c796
AM
13069 goto error_return;
13070 }
13071
13072 if (max_internal_reloc_count != 0)
13073 {
056bafd4 13074 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
8b127cbc
AM
13075 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
13076 if (flinfo.internal_relocs == NULL)
c152c796
AM
13077 goto error_return;
13078 }
13079
13080 if (max_sym_count != 0)
13081 {
13082 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
13083 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
13084 if (flinfo.external_syms == NULL)
c152c796
AM
13085 goto error_return;
13086
13087 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
13088 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
13089 if (flinfo.internal_syms == NULL)
c152c796
AM
13090 goto error_return;
13091
13092 amt = max_sym_count * sizeof (long);
8b127cbc
AM
13093 flinfo.indices = (long int *) bfd_malloc (amt);
13094 if (flinfo.indices == NULL)
c152c796
AM
13095 goto error_return;
13096
13097 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
13098 flinfo.sections = (asection **) bfd_malloc (amt);
13099 if (flinfo.sections == NULL)
c152c796
AM
13100 goto error_return;
13101 }
13102
13103 if (max_sym_shndx_count != 0)
13104 {
13105 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
13106 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
13107 if (flinfo.locsym_shndx == NULL)
c152c796
AM
13108 goto error_return;
13109 }
13110
64f52338 13111 if (htab->tls_sec)
c152c796 13112 {
66631823 13113 bfd_vma base, end = 0; /* Both bytes. */
c152c796
AM
13114 asection *sec;
13115
64f52338 13116 for (sec = htab->tls_sec;
c152c796
AM
13117 sec && (sec->flags & SEC_THREAD_LOCAL);
13118 sec = sec->next)
13119 {
3a800eb9 13120 bfd_size_type size = sec->size;
66631823 13121 unsigned int opb = bfd_octets_per_byte (abfd, sec);
c152c796 13122
3a800eb9
AM
13123 if (size == 0
13124 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 13125 {
91d6fa6a
NC
13126 struct bfd_link_order *ord = sec->map_tail.link_order;
13127
13128 if (ord != NULL)
66631823 13129 size = ord->offset * opb + ord->size;
c152c796 13130 }
66631823 13131 end = sec->vma + size / opb;
c152c796 13132 }
64f52338 13133 base = htab->tls_sec->vma;
7dc98aea
RO
13134 /* Only align end of TLS section if static TLS doesn't have special
13135 alignment requirements. */
13136 if (bed->static_tls_alignment == 1)
64f52338
AM
13137 end = align_power (end, htab->tls_sec->alignment_power);
13138 htab->tls_size = end - base;
c152c796
AM
13139 }
13140
2f0c68f2 13141 if (!_bfd_elf_fixup_eh_frame_hdr (info))
0a1b45a2 13142 return false;
2f0c68f2 13143
23cc1de5
L
13144 /* Finish relative relocations here after regular symbol processing
13145 is finished if DT_RELR is enabled. */
13146 if (info->enable_dt_relr
13147 && bed->finish_relative_relocs
13148 && !bed->finish_relative_relocs (info))
d2616191
AM
13149 info->callbacks->fatal
13150 (_("%P: %pB: failed to finish relative relocations\n"), abfd);
23cc1de5 13151
c152c796
AM
13152 /* Since ELF permits relocations to be against local symbols, we
13153 must have the local symbols available when we do the relocations.
13154 Since we would rather only read the local symbols once, and we
13155 would rather not keep them in memory, we handle all the
13156 relocations for a single input file at the same time.
13157
13158 Unfortunately, there is no way to know the total number of local
13159 symbols until we have seen all of them, and the local symbol
13160 indices precede the global symbol indices. This means that when
13161 we are generating relocatable output, and we see a reloc against
13162 a global symbol, we can not know the symbol index until we have
13163 finished examining all the local symbols to see which ones we are
13164 going to output. To deal with this, we keep the relocations in
13165 memory, and don't output them until the end of the link. This is
13166 an unfortunate waste of memory, but I don't see a good way around
13167 it. Fortunately, it only happens when performing a relocatable
13168 link, which is not the common case. FIXME: If keep_memory is set
13169 we could write the relocs out and then read them again; I don't
13170 know how bad the memory loss will be. */
13171
c72f2fb2 13172 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
0a1b45a2 13173 sub->output_has_begun = false;
c152c796
AM
13174 for (o = abfd->sections; o != NULL; o = o->next)
13175 {
8423293d 13176 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
13177 {
13178 if (p->type == bfd_indirect_link_order
13179 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
13180 == bfd_target_elf_flavour)
13181 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
13182 {
13183 if (! sub->output_has_begun)
13184 {
8b127cbc 13185 if (! elf_link_input_bfd (&flinfo, sub))
c152c796 13186 goto error_return;
0a1b45a2 13187 sub->output_has_begun = true;
c152c796
AM
13188 }
13189 }
13190 else if (p->type == bfd_section_reloc_link_order
13191 || p->type == bfd_symbol_reloc_link_order)
13192 {
13193 if (! elf_reloc_link_order (abfd, info, o, p))
13194 goto error_return;
13195 }
13196 else
13197 {
13198 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
13199 {
13200 if (p->type == bfd_indirect_link_order
13201 && (bfd_get_flavour (sub)
13202 == bfd_target_elf_flavour)
13203 && (elf_elfheader (sub)->e_ident[EI_CLASS]
13204 != bed->s->elfclass))
13205 {
13206 const char *iclass, *oclass;
13207
aebf9be7 13208 switch (bed->s->elfclass)
351f65ca 13209 {
aebf9be7
NC
13210 case ELFCLASS64: oclass = "ELFCLASS64"; break;
13211 case ELFCLASS32: oclass = "ELFCLASS32"; break;
13212 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
13213 default: abort ();
351f65ca 13214 }
aebf9be7
NC
13215
13216 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 13217 {
aebf9be7
NC
13218 case ELFCLASS64: iclass = "ELFCLASS64"; break;
13219 case ELFCLASS32: iclass = "ELFCLASS32"; break;
13220 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
13221 default: abort ();
351f65ca
L
13222 }
13223
13224 bfd_set_error (bfd_error_wrong_format);
4eca0228 13225 _bfd_error_handler
695344c0 13226 /* xgettext:c-format */
871b3ab2 13227 (_("%pB: file class %s incompatible with %s"),
351f65ca
L
13228 sub, iclass, oclass);
13229 }
13230
13231 goto error_return;
13232 }
c152c796
AM
13233 }
13234 }
13235 }
13236
c0f00686
L
13237 /* Free symbol buffer if needed. */
13238 if (!info->reduce_memory_overheads)
13239 {
c72f2fb2 13240 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c9594989 13241 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
c0f00686
L
13242 {
13243 free (elf_tdata (sub)->symbuf);
13244 elf_tdata (sub)->symbuf = NULL;
13245 }
13246 }
13247
c152c796
AM
13248 /* Output any global symbols that got converted to local in a
13249 version script or due to symbol visibility. We do this in a
13250 separate step since ELF requires all local symbols to appear
13251 prior to any global symbols. FIXME: We should only do this if
13252 some global symbols were, in fact, converted to become local.
13253 FIXME: Will this work correctly with the Irix 5 linker? */
0a1b45a2 13254 eoinfo.failed = false;
8b127cbc 13255 eoinfo.flinfo = &flinfo;
0a1b45a2
AM
13256 eoinfo.localsyms = true;
13257 eoinfo.file_sym_done = false;
7686d77d 13258 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796 13259 if (eoinfo.failed)
8c0361c7 13260 goto error_return;
c152c796 13261
4e617b1e
PB
13262 /* If backend needs to output some local symbols not present in the hash
13263 table, do it now. */
25d17459 13264 if (bed->elf_backend_output_arch_local_syms)
4e617b1e 13265 {
4e617b1e 13266 if (! ((*bed->elf_backend_output_arch_local_syms)
37bb890f 13267 (abfd, info, &flinfo, elf_link_output_symstrtab)))
8c0361c7 13268 goto error_return;
4e617b1e
PB
13269 }
13270
c152c796
AM
13271 /* That wrote out all the local symbols. Finish up the symbol table
13272 with the global symbols. Even if we want to strip everything we
13273 can, we still need to deal with those global symbols that got
13274 converted to local in a version script. */
13275
13276 /* The sh_info field records the index of the first non local symbol. */
3f1b17bb
MR
13277 if (!symtab_hdr->sh_info)
13278 symtab_hdr->sh_info = bfd_get_symcount (abfd);
c152c796
AM
13279
13280 if (dynamic
64f52338
AM
13281 && htab->dynsym != NULL
13282 && htab->dynsym->output_section != bfd_abs_section_ptr)
c152c796
AM
13283 {
13284 Elf_Internal_Sym sym;
64f52338 13285 bfd_byte *dynsym = htab->dynsym->contents;
90ac2420 13286
64f52338
AM
13287 o = htab->dynsym->output_section;
13288 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
c152c796
AM
13289
13290 /* Write out the section symbols for the output sections. */
0e1862bb 13291 if (bfd_link_pic (info)
64f52338 13292 || htab->is_relocatable_executable)
c152c796
AM
13293 {
13294 asection *s;
13295
13296 sym.st_size = 0;
13297 sym.st_name = 0;
13298 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
13299 sym.st_other = 0;
35fc36a8 13300 sym.st_target_internal = 0;
c152c796
AM
13301
13302 for (s = abfd->sections; s != NULL; s = s->next)
13303 {
13304 int indx;
13305 bfd_byte *dest;
13306 long dynindx;
13307
c152c796 13308 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
13309 if (dynindx <= 0)
13310 continue;
13311 indx = elf_section_data (s)->this_idx;
c152c796
AM
13312 BFD_ASSERT (indx > 0);
13313 sym.st_shndx = indx;
c0d5a53d 13314 if (! check_dynsym (abfd, &sym))
8c0361c7 13315 goto error_return;
c152c796
AM
13316 sym.st_value = s->vma;
13317 dest = dynsym + dynindx * bed->s->sizeof_sym;
3d16b64e
NA
13318
13319 /* Inform the linker of the addition of this symbol. */
13320
13321 if (info->callbacks->ctf_new_dynsym)
13322 info->callbacks->ctf_new_dynsym (dynindx, &sym);
13323
c152c796
AM
13324 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
13325 }
c152c796
AM
13326 }
13327
13328 /* Write out the local dynsyms. */
64f52338 13329 if (htab->dynlocal)
c152c796
AM
13330 {
13331 struct elf_link_local_dynamic_entry *e;
64f52338 13332 for (e = htab->dynlocal; e ; e = e->next)
c152c796
AM
13333 {
13334 asection *s;
13335 bfd_byte *dest;
13336
935bd1e0 13337 /* Copy the internal symbol and turn off visibility.
c152c796
AM
13338 Note that we saved a word of storage and overwrote
13339 the original st_name with the dynstr_index. */
13340 sym = e->isym;
935bd1e0 13341 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
4d68fd75 13342 sym.st_shndx = SHN_UNDEF;
c152c796 13343
cb33740c
AM
13344 s = bfd_section_from_elf_index (e->input_bfd,
13345 e->isym.st_shndx);
4d68fd75
AM
13346 if (s != NULL
13347 && s->output_section != NULL
13348 && elf_section_data (s->output_section) != NULL)
c152c796 13349 {
c152c796
AM
13350 sym.st_shndx =
13351 elf_section_data (s->output_section)->this_idx;
c0d5a53d 13352 if (! check_dynsym (abfd, &sym))
8c0361c7 13353 goto error_return;
c152c796
AM
13354 sym.st_value = (s->output_section->vma
13355 + s->output_offset
13356 + e->isym.st_value);
13357 }
13358
3d16b64e
NA
13359 /* Inform the linker of the addition of this symbol. */
13360
13361 if (info->callbacks->ctf_new_dynsym)
13362 info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
13363
c152c796
AM
13364 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
13365 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
13366 }
13367 }
c152c796
AM
13368 }
13369
13370 /* We get the global symbols from the hash table. */
0a1b45a2
AM
13371 eoinfo.failed = false;
13372 eoinfo.localsyms = false;
8b127cbc 13373 eoinfo.flinfo = &flinfo;
7686d77d 13374 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796 13375 if (eoinfo.failed)
8c0361c7 13376 goto error_return;
c152c796
AM
13377
13378 /* If backend needs to output some symbols not present in the hash
13379 table, do it now. */
8539e4e8
AM
13380 if (bed->elf_backend_output_arch_syms
13381 && (info->strip != strip_all || emit_relocs))
c152c796 13382 {
c152c796 13383 if (! ((*bed->elf_backend_output_arch_syms)
37bb890f 13384 (abfd, info, &flinfo, elf_link_output_symstrtab)))
8c0361c7 13385 goto error_return;
c152c796
AM
13386 }
13387
ef10c3ac
L
13388 /* Finalize the .strtab section. */
13389 _bfd_elf_strtab_finalize (flinfo.symstrtab);
13390
13391 /* Swap out the .strtab section. */
13392 if (!elf_link_swap_symbols_out (&flinfo))
8c0361c7
AM
13393 goto error_return;
13394 free (htab->strtab);
13395 htab->strtab = NULL;
c152c796
AM
13396
13397 /* Now we know the size of the symtab section. */
c152c796
AM
13398 if (bfd_get_symcount (abfd) > 0)
13399 {
ee3b52e9
L
13400 /* Finish up and write out the symbol string table (.strtab)
13401 section. */
ad32986f 13402 Elf_Internal_Shdr *symstrtab_hdr = NULL;
8539e4e8
AM
13403 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
13404
ad32986f 13405 if (elf_symtab_shndx_list (abfd))
8539e4e8 13406 {
ad32986f 13407 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8539e4e8 13408
ad32986f
NC
13409 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
13410 {
13411 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
13412 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
13413 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
13414 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
13415 symtab_shndx_hdr->sh_size = amt;
8539e4e8 13416
ad32986f 13417 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
1f1b5e50 13418 off, true, 0);
ad32986f
NC
13419
13420 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
226f9f4f 13421 || (bfd_write (flinfo.symshndxbuf, amt, abfd) != amt))
8c0361c7 13422 goto error_return;
ad32986f 13423 }
8539e4e8 13424 }
ee3b52e9
L
13425
13426 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
13427 /* sh_name was set in prep_headers. */
13428 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 13429 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 13430 symstrtab_hdr->sh_addr = 0;
ef10c3ac 13431 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
13432 symstrtab_hdr->sh_entsize = 0;
13433 symstrtab_hdr->sh_link = 0;
13434 symstrtab_hdr->sh_info = 0;
13435 /* sh_offset is set just below. */
13436 symstrtab_hdr->sh_addralign = 1;
13437
13438 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
1f1b5e50 13439 off, true, 0);
ee3b52e9
L
13440 elf_next_file_pos (abfd) = off;
13441
c152c796 13442 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 13443 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
8c0361c7 13444 goto error_return;
c152c796
AM
13445 }
13446
76359541
TP
13447 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
13448 {
871b3ab2 13449 _bfd_error_handler (_("%pB: failed to generate import library"),
4eca0228 13450 info->out_implib_bfd);
8c0361c7 13451 goto error_return;
76359541
TP
13452 }
13453
c152c796
AM
13454 /* Adjust the relocs to have the correct symbol indices. */
13455 for (o = abfd->sections; o != NULL; o = o->next)
13456 {
d4730f92 13457 struct bfd_elf_section_data *esdo = elf_section_data (o);
0a1b45a2 13458 bool sort;
10bbbc1d 13459
c152c796
AM
13460 if ((o->flags & SEC_RELOC) == 0)
13461 continue;
13462
28dbcedc 13463 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3 13464 if (esdo->rel.hdr != NULL
10bbbc1d 13465 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
8c0361c7 13466 goto error_return;
bca6d0e3 13467 if (esdo->rela.hdr != NULL
10bbbc1d 13468 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
8c0361c7 13469 goto error_return;
c152c796
AM
13470
13471 /* Set the reloc_count field to 0 to prevent write_relocs from
13472 trying to swap the relocs out itself. */
13473 o->reloc_count = 0;
13474 }
13475
9c3d7d9b 13476 relativecount = 0;
c152c796
AM
13477 if (dynamic && info->combreloc && dynobj != NULL)
13478 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
13479
9c3d7d9b
AM
13480 relr_entsize = 0;
13481 if (htab->srelrdyn != NULL
13482 && htab->srelrdyn->output_section != NULL
13483 && htab->srelrdyn->size != 0)
13484 {
13485 asection *s = htab->srelrdyn->output_section;
13486 relr_entsize = elf_section_data (s)->this_hdr.sh_entsize;
13487 if (relr_entsize == 0)
13488 {
13489 relr_entsize = bed->s->arch_size / 8;
13490 elf_section_data (s)->this_hdr.sh_entsize = relr_entsize;
13491 }
13492 }
13493
c152c796
AM
13494 /* If we are linking against a dynamic object, or generating a
13495 shared library, finish up the dynamic linking information. */
13496 if (dynamic)
13497 {
13498 bfd_byte *dyncon, *dynconend;
13499
13500 /* Fix up .dynamic entries. */
c3460201 13501 o = htab->dynamic;
c152c796
AM
13502 BFD_ASSERT (o != NULL);
13503
13504 dyncon = o->contents;
36f61bf2 13505 dynconend = PTR_ADD (o->contents, o->size);
c152c796
AM
13506 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13507 {
13508 Elf_Internal_Dyn dyn;
13509 const char *name;
13510 unsigned int type;
64487780
AM
13511 bfd_size_type sh_size;
13512 bfd_vma sh_addr;
c152c796
AM
13513
13514 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13515
13516 switch (dyn.d_tag)
13517 {
13518 default:
13519 continue;
13520 case DT_NULL:
9c3d7d9b 13521 if (relativecount != 0)
c152c796
AM
13522 {
13523 switch (elf_section_data (reldyn)->this_hdr.sh_type)
13524 {
13525 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
13526 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
c152c796 13527 }
9c3d7d9b
AM
13528 if (dyn.d_tag != DT_NULL
13529 && dynconend - dyncon >= bed->s->sizeof_dyn)
13530 {
13531 dyn.d_un.d_val = relativecount;
13532 relativecount = 0;
13533 break;
13534 }
c152c796 13535 relativecount = 0;
9c3d7d9b
AM
13536 }
13537 if (relr_entsize != 0)
13538 {
13539 if (dynconend - dyncon >= 3 * bed->s->sizeof_dyn)
13540 {
13541 asection *s = htab->srelrdyn;
13542 dyn.d_tag = DT_RELR;
13543 dyn.d_un.d_ptr
13544 = s->output_section->vma + s->output_offset;
13545 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13546 dyncon += bed->s->sizeof_dyn;
13547
13548 dyn.d_tag = DT_RELRSZ;
13549 dyn.d_un.d_val = s->size;
13550 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13551 dyncon += bed->s->sizeof_dyn;
13552
13553 dyn.d_tag = DT_RELRENT;
13554 dyn.d_un.d_val = relr_entsize;
13555 relr_entsize = 0;
13556 break;
13557 }
13558 relr_entsize = 0;
c152c796
AM
13559 }
13560 continue;
13561
13562 case DT_INIT:
13563 name = info->init_function;
13564 goto get_sym;
13565 case DT_FINI:
13566 name = info->fini_function;
13567 get_sym:
13568 {
13569 struct elf_link_hash_entry *h;
13570
0a1b45a2 13571 h = elf_link_hash_lookup (htab, name, false, false, true);
c152c796
AM
13572 if (h != NULL
13573 && (h->root.type == bfd_link_hash_defined
13574 || h->root.type == bfd_link_hash_defweak))
13575 {
bef26483 13576 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
13577 o = h->root.u.def.section;
13578 if (o->output_section != NULL)
bef26483 13579 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
13580 + o->output_offset);
13581 else
13582 {
13583 /* The symbol is imported from another shared
13584 library and does not apply to this one. */
bef26483 13585 dyn.d_un.d_ptr = 0;
c152c796
AM
13586 }
13587 break;
13588 }
13589 }
13590 continue;
13591
13592 case DT_PREINIT_ARRAYSZ:
13593 name = ".preinit_array";
4ade44b7 13594 goto get_out_size;
c152c796
AM
13595 case DT_INIT_ARRAYSZ:
13596 name = ".init_array";
4ade44b7 13597 goto get_out_size;
c152c796
AM
13598 case DT_FINI_ARRAYSZ:
13599 name = ".fini_array";
4ade44b7 13600 get_out_size:
c152c796
AM
13601 o = bfd_get_section_by_name (abfd, name);
13602 if (o == NULL)
13603 {
4eca0228 13604 _bfd_error_handler
4ade44b7 13605 (_("could not find section %s"), name);
c152c796
AM
13606 goto error_return;
13607 }
eea6121a 13608 if (o->size == 0)
4eca0228 13609 _bfd_error_handler
c152c796 13610 (_("warning: %s section has zero size"), name);
eea6121a 13611 dyn.d_un.d_val = o->size;
c152c796
AM
13612 break;
13613
13614 case DT_PREINIT_ARRAY:
13615 name = ".preinit_array";
4ade44b7 13616 goto get_out_vma;
c152c796
AM
13617 case DT_INIT_ARRAY:
13618 name = ".init_array";
4ade44b7 13619 goto get_out_vma;
c152c796
AM
13620 case DT_FINI_ARRAY:
13621 name = ".fini_array";
4ade44b7
AM
13622 get_out_vma:
13623 o = bfd_get_section_by_name (abfd, name);
13624 goto do_vma;
c152c796
AM
13625
13626 case DT_HASH:
13627 name = ".hash";
13628 goto get_vma;
fdc90cb4
JJ
13629 case DT_GNU_HASH:
13630 name = ".gnu.hash";
13631 goto get_vma;
c152c796
AM
13632 case DT_STRTAB:
13633 name = ".dynstr";
13634 goto get_vma;
13635 case DT_SYMTAB:
13636 name = ".dynsym";
13637 goto get_vma;
13638 case DT_VERDEF:
13639 name = ".gnu.version_d";
13640 goto get_vma;
13641 case DT_VERNEED:
13642 name = ".gnu.version_r";
13643 goto get_vma;
13644 case DT_VERSYM:
13645 name = ".gnu.version";
13646 get_vma:
4ade44b7
AM
13647 o = bfd_get_linker_section (dynobj, name);
13648 do_vma:
b3293efa 13649 if (o == NULL || bfd_is_abs_section (o->output_section))
c152c796 13650 {
4eca0228 13651 _bfd_error_handler
4ade44b7 13652 (_("could not find section %s"), name);
c152c796
AM
13653 goto error_return;
13654 }
894891db
NC
13655 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13656 {
4eca0228 13657 _bfd_error_handler
894891db
NC
13658 (_("warning: section '%s' is being made into a note"), name);
13659 bfd_set_error (bfd_error_nonrepresentable_section);
13660 goto error_return;
13661 }
4ade44b7 13662 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
c152c796
AM
13663 break;
13664
13665 case DT_REL:
13666 case DT_RELA:
13667 case DT_RELSZ:
13668 case DT_RELASZ:
13669 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13670 type = SHT_REL;
13671 else
13672 type = SHT_RELA;
64487780
AM
13673 sh_size = 0;
13674 sh_addr = 0;
c152c796
AM
13675 for (i = 1; i < elf_numsections (abfd); i++)
13676 {
13677 Elf_Internal_Shdr *hdr;
13678
13679 hdr = elf_elfsections (abfd)[i];
13680 if (hdr->sh_type == type
13681 && (hdr->sh_flags & SHF_ALLOC) != 0)
13682 {
64487780
AM
13683 sh_size += hdr->sh_size;
13684 if (sh_addr == 0
13685 || sh_addr > hdr->sh_addr)
13686 sh_addr = hdr->sh_addr;
c152c796
AM
13687 }
13688 }
64487780 13689
64f52338
AM
13690 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
13691 {
66631823
CE
13692 unsigned int opb = bfd_octets_per_byte (abfd, o);
13693
64f52338
AM
13694 /* Don't count procedure linkage table relocs in the
13695 overall reloc count. */
64487780
AM
13696 sh_size -= htab->srelplt->size;
13697 if (sh_size == 0)
13698 /* If the size is zero, make the address zero too.
13699 This is to avoid a glibc bug. If the backend
13700 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13701 zero, then we'll put DT_RELA at the end of
13702 DT_JMPREL. glibc will interpret the end of
13703 DT_RELA matching the end of DT_JMPREL as the
13704 case where DT_RELA includes DT_JMPREL, and for
13705 LD_BIND_NOW will decide that processing DT_RELA
13706 will process the PLT relocs too. Net result:
13707 No PLT relocs applied. */
13708 sh_addr = 0;
13709
64f52338
AM
13710 /* If .rela.plt is the first .rela section, exclude
13711 it from DT_RELA. */
64487780 13712 else if (sh_addr == (htab->srelplt->output_section->vma
66631823 13713 + htab->srelplt->output_offset) * opb)
64487780 13714 sh_addr += htab->srelplt->size;
64f52338 13715 }
64487780
AM
13716
13717 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13718 dyn.d_un.d_val = sh_size;
13719 else
13720 dyn.d_un.d_ptr = sh_addr;
c152c796
AM
13721 break;
13722 }
13723 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13724 }
13725 }
13726
13727 /* If we have created any dynamic sections, then output them. */
13728 if (dynobj != NULL)
13729 {
13730 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
13731 goto error_return;
13732
943284cc 13733 /* Check for DT_TEXTREL (late, in case the backend removes it). */
a6dbf402 13734 if (bfd_link_textrel_check (info)
c3460201 13735 && (o = htab->dynamic) != NULL
36f61bf2 13736 && o->size != 0)
943284cc
DJ
13737 {
13738 bfd_byte *dyncon, *dynconend;
13739
943284cc
DJ
13740 dyncon = o->contents;
13741 dynconend = o->contents + o->size;
13742 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13743 {
13744 Elf_Internal_Dyn dyn;
13745
13746 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13747
13748 if (dyn.d_tag == DT_TEXTREL)
13749 {
a6dbf402 13750 if (info->textrel_check == textrel_check_error)
c192a133 13751 info->callbacks->einfo
9793eb77 13752 (_("%P%X: read-only segment has dynamic relocations\n"));
a6dbf402
L
13753 else if (bfd_link_dll (info))
13754 info->callbacks->einfo
13755 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
83b1d8f4
L
13756 else if (bfd_link_pde (info))
13757 info->callbacks->einfo
13758 (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
c192a133
AM
13759 else
13760 info->callbacks->einfo
a6dbf402 13761 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
943284cc
DJ
13762 break;
13763 }
13764 }
13765 }
13766
c152c796
AM
13767 for (o = dynobj->sections; o != NULL; o = o->next)
13768 {
13769 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 13770 || o->size == 0
c152c796
AM
13771 || o->output_section == bfd_abs_section_ptr)
13772 continue;
13773 if ((o->flags & SEC_LINKER_CREATED) == 0)
13774 {
13775 /* At this point, we are only interested in sections
13776 created by _bfd_elf_link_create_dynamic_sections. */
13777 continue;
13778 }
64f52338 13779 if (htab->stab_info.stabstr == o)
3722b82f 13780 continue;
64f52338 13781 if (htab->eh_info.hdr_sec == o)
eea6121a 13782 continue;
3d4d4302 13783 if (strcmp (o->name, ".dynstr") != 0)
c152c796 13784 {
bb294208
AM
13785 bfd_size_type octets = ((file_ptr) o->output_offset
13786 * bfd_octets_per_byte (abfd, o));
13787 if (!bfd_set_section_contents (abfd, o->output_section,
13788 o->contents, octets, o->size))
c152c796
AM
13789 goto error_return;
13790 }
13791 else
13792 {
13793 /* The contents of the .dynstr section are actually in a
13794 stringtab. */
8539e4e8
AM
13795 file_ptr off;
13796
c152c796
AM
13797 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13798 if (bfd_seek (abfd, off, SEEK_SET) != 0
64f52338 13799 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
c152c796
AM
13800 goto error_return;
13801 }
13802 }
13803 }
13804
7bdf4127 13805 if (!info->resolve_section_groups)
c152c796 13806 {
0a1b45a2 13807 bool failed = false;
c152c796 13808
7bdf4127 13809 BFD_ASSERT (bfd_link_relocatable (info));
c152c796
AM
13810 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13811 if (failed)
13812 goto error_return;
13813 }
13814
13815 /* If we have optimized stabs strings, output them. */
64f52338 13816 if (htab->stab_info.stabstr != NULL)
c152c796 13817 {
64f52338 13818 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
c152c796
AM
13819 goto error_return;
13820 }
13821
9f7c3e5e
AM
13822 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13823 goto error_return;
c152c796 13824
cf0e0a0b
IB
13825 if (! _bfd_elf_write_section_sframe (abfd, info))
13826 goto error_return;
13827
011e5014
ML
13828 if (! _bfd_elf_write_section_build_attributes (abfd, info))
13829 goto error_ret2;
13830
1ff6de03
NA
13831 if (info->callbacks->emit_ctf)
13832 info->callbacks->emit_ctf ();
13833
9f7c3e5e 13834 elf_final_link_free (abfd, &flinfo);
c152c796 13835
496afd17
L
13836 if (info->unique_symbol)
13837 bfd_hash_table_free (&flinfo.local_hash_table);
8c0361c7 13838 return true;
c152c796
AM
13839
13840 error_return:
8c0361c7
AM
13841 free (htab->strtab);
13842 htab->strtab = NULL;
9f7c3e5e 13843 elf_final_link_free (abfd, &flinfo);
8c0361c7
AM
13844 error_ret2:
13845 if (info->unique_symbol)
13846 bfd_hash_table_free (&flinfo.local_hash_table);
13847 return false;
c152c796
AM
13848}
13849\f
5241d853
RS
13850/* Initialize COOKIE for input bfd ABFD. */
13851
0a1b45a2 13852static bool
5241d853 13853init_reloc_cookie (struct elf_reloc_cookie *cookie,
3428c771
L
13854 struct bfd_link_info *info, bfd *abfd,
13855 bool keep_memory)
5241d853
RS
13856{
13857 Elf_Internal_Shdr *symtab_hdr;
13858 const struct elf_backend_data *bed;
13859
13860 bed = get_elf_backend_data (abfd);
13861 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13862
13863 cookie->abfd = abfd;
13864 cookie->sym_hashes = elf_sym_hashes (abfd);
13865 cookie->bad_symtab = elf_bad_symtab (abfd);
13866 if (cookie->bad_symtab)
13867 {
13868 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13869 cookie->extsymoff = 0;
13870 }
13871 else
13872 {
13873 cookie->locsymcount = symtab_hdr->sh_info;
13874 cookie->extsymoff = symtab_hdr->sh_info;
13875 }
13876
13877 if (bed->s->arch_size == 32)
13878 cookie->r_sym_shift = 8;
13879 else
13880 cookie->r_sym_shift = 32;
13881
13882 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13883 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13884 {
13885 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13886 cookie->locsymcount, 0,
13887 NULL, NULL, NULL);
13888 if (cookie->locsyms == NULL)
13889 {
13890 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
0a1b45a2 13891 return false;
5241d853 13892 }
3428c771 13893 if (keep_memory || _bfd_elf_link_keep_memory (info))
a8dde0a2
L
13894 {
13895 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13896 info->cache_size += (cookie->locsymcount
049467c7 13897 * sizeof (Elf_Internal_Sym));
a8dde0a2 13898 }
5241d853 13899 }
0a1b45a2 13900 return true;
5241d853
RS
13901}
13902
13903/* Free the memory allocated by init_reloc_cookie, if appropriate. */
13904
13905static void
13906fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13907{
13908 Elf_Internal_Shdr *symtab_hdr;
13909
13910 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
c9594989 13911 if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
5241d853
RS
13912 free (cookie->locsyms);
13913}
13914
13915/* Initialize the relocation information in COOKIE for input section SEC
13916 of input bfd ABFD. */
13917
0a1b45a2 13918static bool
5241d853
RS
13919init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13920 struct bfd_link_info *info, bfd *abfd,
3428c771 13921 asection *sec, bool keep_memory)
5241d853 13922{
5241d853
RS
13923 if (sec->reloc_count == 0)
13924 {
13925 cookie->rels = NULL;
13926 cookie->relend = NULL;
13927 }
13928 else
13929 {
d41b7648
L
13930 cookie->rels = _bfd_elf_link_info_read_relocs
13931 (abfd, info, sec, NULL, NULL,
3428c771 13932 keep_memory || _bfd_elf_link_keep_memory (info));
5241d853 13933 if (cookie->rels == NULL)
0a1b45a2 13934 return false;
5241d853 13935 cookie->rel = cookie->rels;
056bafd4 13936 cookie->relend = cookie->rels + sec->reloc_count;
5241d853
RS
13937 }
13938 cookie->rel = cookie->rels;
0a1b45a2 13939 return true;
5241d853
RS
13940}
13941
13942/* Free the memory allocated by init_reloc_cookie_rels,
13943 if appropriate. */
13944
13945static void
13946fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13947 asection *sec)
13948{
c9594989 13949 if (elf_section_data (sec)->relocs != cookie->rels)
5241d853
RS
13950 free (cookie->rels);
13951}
13952
13953/* Initialize the whole of COOKIE for input section SEC. */
13954
0a1b45a2 13955static bool
5241d853
RS
13956init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13957 struct bfd_link_info *info,
3428c771 13958 asection *sec, bool keep_memory)
5241d853 13959{
3428c771 13960 if (!init_reloc_cookie (cookie, info, sec->owner, keep_memory))
5241d853 13961 goto error1;
3428c771
L
13962 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec,
13963 keep_memory))
5241d853 13964 goto error2;
0a1b45a2 13965 return true;
5241d853
RS
13966
13967 error2:
13968 fini_reloc_cookie (cookie, sec->owner);
13969 error1:
0a1b45a2 13970 return false;
5241d853
RS
13971}
13972
13973/* Free the memory allocated by init_reloc_cookie_for_section,
13974 if appropriate. */
13975
13976static void
13977fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13978 asection *sec)
13979{
13980 fini_reloc_cookie_rels (cookie, sec);
13981 fini_reloc_cookie (cookie, sec->owner);
13982}
13983\f
c152c796
AM
13984/* Garbage collect unused sections. */
13985
07adf181
AM
13986/* Default gc_mark_hook. */
13987
13988asection *
13989_bfd_elf_gc_mark_hook (asection *sec,
13990 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13991 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13992 struct elf_link_hash_entry *h,
13993 Elf_Internal_Sym *sym)
13994{
931494c9
NC
13995 if (h == NULL)
13996 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13997
13998 switch (h->root.type)
07adf181 13999 {
931494c9
NC
14000 case bfd_link_hash_defined:
14001 case bfd_link_hash_defweak:
14002 return h->root.u.def.section;
07adf181 14003
931494c9
NC
14004 case bfd_link_hash_common:
14005 return h->root.u.c.p->section;
07adf181 14006
931494c9
NC
14007 default:
14008 return NULL;
07adf181 14009 }
07adf181
AM
14010}
14011
9e223787 14012/* Return the debug definition section. */
b7c871ed
L
14013
14014static asection *
14015elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
14016 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14017 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
14018 struct elf_link_hash_entry *h,
9e223787 14019 Elf_Internal_Sym *sym)
b7c871ed 14020{
9e223787
L
14021 if (h != NULL)
14022 {
14023 /* Return the global debug definition section. */
14024 if ((h->root.type == bfd_link_hash_defined
14025 || h->root.type == bfd_link_hash_defweak)
14026 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
14027 return h->root.u.def.section;
14028 }
14029 else
14030 {
14031 /* Return the local debug definition section. */
14032 asection *isec = bfd_section_from_elf_index (sec->owner,
14033 sym->st_shndx);
e7c3fa04 14034 if (isec != NULL && (isec->flags & SEC_DEBUGGING) != 0)
9e223787
L
14035 return isec;
14036 }
b7c871ed
L
14037
14038 return NULL;
14039}
14040
5241d853
RS
14041/* COOKIE->rel describes a relocation against section SEC, which is
14042 a section we've decided to keep. Return the section that contains
14043 the relocation symbol, or NULL if no section contains it. */
14044
14045asection *
14046_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
14047 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9 14048 struct elf_reloc_cookie *cookie,
0a1b45a2 14049 bool *start_stop)
5241d853
RS
14050{
14051 unsigned long r_symndx;
3024a17a 14052 struct elf_link_hash_entry *h, *hw;
5241d853
RS
14053
14054 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 14055 if (r_symndx == STN_UNDEF)
5241d853
RS
14056 return NULL;
14057
931494c9
NC
14058 h = get_ext_sym_hash_from_cookie (cookie, r_symndx);
14059 if (h == NULL)
5241d853 14060 {
879d24de 14061 /* A corrupt input file can lead to a situation where the index
931494c9
NC
14062 does not reference either a local or an external symbol. */
14063 if (r_symndx >= cookie->locsymcount)
14064 return NULL;
5789f845 14065
931494c9
NC
14066 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
14067 &cookie->locsyms[r_symndx]);
14068 }
1cce69b9 14069
931494c9 14070 bool was_marked = h->mark;
8ee10e86 14071
931494c9
NC
14072 h->mark = 1;
14073 /* Keep all aliases of the symbol too. If an object symbol
14074 needs to be copied into .dynbss then all of its aliases
14075 should be present as dynamic symbols, not just the one used
14076 on the copy relocation. */
14077 hw = h;
14078 while (hw->is_weakalias)
14079 {
14080 hw = hw->u.alias;
14081 hw->mark = 1;
14082 }
1cce69b9 14083
931494c9
NC
14084 if (!was_marked && h->start_stop && !h->root.ldscript_def)
14085 {
14086 if (info->start_stop_gc)
14087 return NULL;
14088
14089 /* To work around a glibc bug, mark XXX input sections
14090 when there is a reference to __start_XXX or __stop_XXX
14091 symbols. */
14092 else if (start_stop != NULL)
14093 {
14094 asection *s = h->u2.start_stop_section;
14095 *start_stop = true;
14096 return s;
14097 }
5241d853
RS
14098 }
14099
931494c9 14100 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
5241d853
RS
14101}
14102
14103/* COOKIE->rel describes a relocation against section SEC, which is
14104 a section we've decided to keep. Mark the section that contains
9d0a14d3 14105 the relocation symbol. */
5241d853 14106
0a1b45a2 14107bool
5241d853
RS
14108_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
14109 asection *sec,
14110 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 14111 struct elf_reloc_cookie *cookie)
5241d853
RS
14112{
14113 asection *rsec;
0a1b45a2 14114 bool start_stop = false;
5241d853 14115
1cce69b9
AM
14116 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
14117 while (rsec != NULL)
5241d853 14118 {
1cce69b9
AM
14119 if (!rsec->gc_mark)
14120 {
14121 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
14122 || (rsec->owner->flags & DYNAMIC) != 0)
14123 rsec->gc_mark = 1;
14124 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
0a1b45a2 14125 return false;
1cce69b9
AM
14126 }
14127 if (!start_stop)
14128 break;
199af150 14129 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853 14130 }
0a1b45a2 14131 return true;
5241d853
RS
14132}
14133
07adf181
AM
14134/* The mark phase of garbage collection. For a given section, mark
14135 it and any sections in this section's group, and all the sections
14136 which define symbols to which it refers. */
14137
0a1b45a2 14138bool
ccfa59ea
AM
14139_bfd_elf_gc_mark (struct bfd_link_info *info,
14140 asection *sec,
6a5bb875 14141 elf_gc_mark_hook_fn gc_mark_hook)
c152c796 14142{
0a1b45a2 14143 bool ret;
9d0a14d3 14144 asection *group_sec, *eh_frame;
c152c796
AM
14145
14146 sec->gc_mark = 1;
14147
14148 /* Mark all the sections in the group. */
14149 group_sec = elf_section_data (sec)->next_in_group;
14150 if (group_sec && !group_sec->gc_mark)
ccfa59ea 14151 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
0a1b45a2 14152 return false;
c152c796
AM
14153
14154 /* Look through the section relocs. */
0a1b45a2 14155 ret = true;
9d0a14d3
RS
14156 eh_frame = elf_eh_frame_section (sec->owner);
14157 if ((sec->flags & SEC_RELOC) != 0
14158 && sec->reloc_count > 0
14159 && sec != eh_frame)
c152c796 14160 {
5241d853 14161 struct elf_reloc_cookie cookie;
c152c796 14162
3428c771 14163 if (!init_reloc_cookie_for_section (&cookie, info, sec, false))
0a1b45a2 14164 ret = false;
c152c796 14165 else
c152c796 14166 {
5241d853 14167 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 14168 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853 14169 {
0a1b45a2 14170 ret = false;
5241d853
RS
14171 break;
14172 }
14173 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
14174 }
14175 }
9d0a14d3
RS
14176
14177 if (ret && eh_frame && elf_fde_list (sec))
14178 {
14179 struct elf_reloc_cookie cookie;
14180
3428c771
L
14181 /* NB: When --no-keep-memory is used, the symbol table and
14182 relocation info for eh_frame are freed after they are retrieved
14183 for each text section in the input object. If an input object
14184 has many text sections, the same data is retrieved and freed
14185 many times which can take a very long time. Always keep the
14186 symbol table and relocation info for eh_frame to avoid it. */
14187 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame,
14188 true))
0a1b45a2 14189 ret = false;
9d0a14d3
RS
14190 else
14191 {
14192 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
14193 gc_mark_hook, &cookie))
0a1b45a2 14194 ret = false;
9d0a14d3
RS
14195 fini_reloc_cookie_for_section (&cookie, eh_frame);
14196 }
14197 }
14198
2f0c68f2
CM
14199 eh_frame = elf_section_eh_frame_entry (sec);
14200 if (ret && eh_frame && !eh_frame->gc_mark)
14201 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
0a1b45a2 14202 ret = false;
2f0c68f2 14203
c152c796
AM
14204 return ret;
14205}
14206
3c758495
TG
14207/* Scan and mark sections in a special or debug section group. */
14208
14209static void
14210_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
14211{
14212 /* Point to first section of section group. */
14213 asection *ssec;
14214 /* Used to iterate the section group. */
14215 asection *msec;
14216
0a1b45a2
AM
14217 bool is_special_grp = true;
14218 bool is_debug_grp = true;
3c758495
TG
14219
14220 /* First scan to see if group contains any section other than debug
14221 and special section. */
14222 ssec = msec = elf_next_in_group (grp);
14223 do
14224 {
14225 if ((msec->flags & SEC_DEBUGGING) == 0)
0a1b45a2 14226 is_debug_grp = false;
3c758495
TG
14227
14228 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
0a1b45a2 14229 is_special_grp = false;
3c758495
TG
14230
14231 msec = elf_next_in_group (msec);
14232 }
14233 while (msec != ssec);
14234
14235 /* If this is a pure debug section group or pure special section group,
14236 keep all sections in this group. */
14237 if (is_debug_grp || is_special_grp)
14238 {
14239 do
14240 {
14241 msec->gc_mark = 1;
14242 msec = elf_next_in_group (msec);
14243 }
14244 while (msec != ssec);
14245 }
14246}
14247
7f6ab9f8
AM
14248/* Keep debug and special sections. */
14249
0a1b45a2 14250bool
7f6ab9f8 14251_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
b7d07216 14252 elf_gc_mark_hook_fn mark_hook)
7f6ab9f8
AM
14253{
14254 bfd *ibfd;
14255
c72f2fb2 14256 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
14257 {
14258 asection *isec;
0a1b45a2
AM
14259 bool some_kept;
14260 bool debug_frag_seen;
14261 bool has_kept_debug_info;
7f6ab9f8
AM
14262
14263 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14264 continue;
57963c05
AM
14265 isec = ibfd->sections;
14266 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14267 continue;
7f6ab9f8 14268
b40bf0a2
NC
14269 /* Ensure all linker created sections are kept,
14270 see if any other section is already marked,
14271 and note if we have any fragmented debug sections. */
0a1b45a2 14272 debug_frag_seen = some_kept = has_kept_debug_info = false;
7f6ab9f8
AM
14273 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14274 {
14275 if ((isec->flags & SEC_LINKER_CREATED) != 0)
14276 isec->gc_mark = 1;
eb026f09
AM
14277 else if (isec->gc_mark
14278 && (isec->flags & SEC_ALLOC) != 0
14279 && elf_section_type (isec) != SHT_NOTE)
0a1b45a2 14280 some_kept = true;
b7d07216
L
14281 else
14282 {
14283 /* Since all sections, except for backend specific ones,
14284 have been garbage collected, call mark_hook on this
14285 section if any of its linked-to sections is marked. */
def97fb9
AM
14286 asection *linked_to_sec;
14287 for (linked_to_sec = elf_linked_to_section (isec);
14288 linked_to_sec != NULL && !linked_to_sec->linker_mark;
b7d07216 14289 linked_to_sec = elf_linked_to_section (linked_to_sec))
def97fb9
AM
14290 {
14291 if (linked_to_sec->gc_mark)
14292 {
14293 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
0a1b45a2 14294 return false;
def97fb9
AM
14295 break;
14296 }
14297 linked_to_sec->linker_mark = 1;
14298 }
14299 for (linked_to_sec = elf_linked_to_section (isec);
14300 linked_to_sec != NULL && linked_to_sec->linker_mark;
14301 linked_to_sec = elf_linked_to_section (linked_to_sec))
14302 linked_to_sec->linker_mark = 0;
b7d07216 14303 }
b40bf0a2 14304
535b785f 14305 if (!debug_frag_seen
b40bf0a2 14306 && (isec->flags & SEC_DEBUGGING)
08dedd66 14307 && startswith (isec->name, ".debug_line."))
0a1b45a2 14308 debug_frag_seen = true;
5242a0a0
L
14309 else if (strcmp (bfd_section_name (isec),
14310 "__patchable_function_entries") == 0
14311 && elf_linked_to_section (isec) == NULL)
d2616191 14312 info->callbacks->fatal (_("%P: %pB(%pA): error: "
5242a0a0
L
14313 "need linked-to section "
14314 "for --gc-sections\n"),
14315 isec->owner, isec);
7f6ab9f8
AM
14316 }
14317
eb026f09
AM
14318 /* If no non-note alloc section in this file will be kept, then
14319 we can toss out the debug and special sections. */
7f6ab9f8
AM
14320 if (!some_kept)
14321 continue;
14322
14323 /* Keep debug and special sections like .comment when they are
3c758495 14324 not part of a group. Also keep section groups that contain
b7d07216
L
14325 just debug sections or special sections. NB: Sections with
14326 linked-to section has been handled above. */
7f6ab9f8 14327 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
14328 {
14329 if ((isec->flags & SEC_GROUP) != 0)
14330 _bfd_elf_gc_mark_debug_special_section_group (isec);
14331 else if (((isec->flags & SEC_DEBUGGING) != 0
14332 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
b7d07216
L
14333 && elf_next_in_group (isec) == NULL
14334 && elf_linked_to_section (isec) == NULL)
3c758495 14335 isec->gc_mark = 1;
b7c871ed 14336 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
0a1b45a2 14337 has_kept_debug_info = true;
3c758495 14338 }
b40bf0a2 14339
b40bf0a2
NC
14340 /* Look for CODE sections which are going to be discarded,
14341 and find and discard any fragmented debug sections which
14342 are associated with that code section. */
b7c871ed
L
14343 if (debug_frag_seen)
14344 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14345 if ((isec->flags & SEC_CODE) != 0
14346 && isec->gc_mark == 0)
14347 {
14348 unsigned int ilen;
14349 asection *dsec;
b40bf0a2 14350
b7c871ed 14351 ilen = strlen (isec->name);
b40bf0a2 14352
b7c871ed 14353 /* Association is determined by the name of the debug
07d6d2b8 14354 section containing the name of the code section as
b7c871ed
L
14355 a suffix. For example .debug_line.text.foo is a
14356 debug section associated with .text.foo. */
14357 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
14358 {
14359 unsigned int dlen;
b40bf0a2 14360
b7c871ed
L
14361 if (dsec->gc_mark == 0
14362 || (dsec->flags & SEC_DEBUGGING) == 0)
14363 continue;
b40bf0a2 14364
b7c871ed 14365 dlen = strlen (dsec->name);
b40bf0a2 14366
b7c871ed
L
14367 if (dlen > ilen
14368 && strncmp (dsec->name + (dlen - ilen),
14369 isec->name, ilen) == 0)
b40bf0a2 14370 dsec->gc_mark = 0;
b7c871ed 14371 }
b40bf0a2 14372 }
b7c871ed
L
14373
14374 /* Mark debug sections referenced by kept debug sections. */
14375 if (has_kept_debug_info)
14376 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
14377 if (isec->gc_mark
14378 && (isec->flags & SEC_DEBUGGING) != 0)
14379 if (!_bfd_elf_gc_mark (info, isec,
14380 elf_gc_mark_debug_section))
0a1b45a2 14381 return false;
7f6ab9f8 14382 }
0a1b45a2 14383 return true;
7f6ab9f8
AM
14384}
14385
0a1b45a2 14386static bool
ccabcbe5 14387elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
14388{
14389 bfd *sub;
ccabcbe5 14390 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
c152c796 14391
c72f2fb2 14392 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
14393 {
14394 asection *o;
14395
b19a8f85 14396 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 14397 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
b19a8f85 14398 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796 14399 continue;
57963c05
AM
14400 o = sub->sections;
14401 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14402 continue;
c152c796
AM
14403
14404 for (o = sub->sections; o != NULL; o = o->next)
14405 {
a33dafc3
L
14406 /* When any section in a section group is kept, we keep all
14407 sections in the section group. If the first member of
14408 the section group is excluded, we will also exclude the
14409 group section. */
14410 if (o->flags & SEC_GROUP)
14411 {
14412 asection *first = elf_next_in_group (o);
d1458933
AM
14413 if (first != NULL)
14414 o->gc_mark = first->gc_mark;
a33dafc3 14415 }
c152c796 14416
1e7eae0d 14417 if (o->gc_mark)
c152c796
AM
14418 continue;
14419
14420 /* Skip sweeping sections already excluded. */
14421 if (o->flags & SEC_EXCLUDE)
14422 continue;
14423
14424 /* Since this is early in the link process, it is simple
14425 to remove a section from the output. */
14426 o->flags |= SEC_EXCLUDE;
14427
c55fe096 14428 if (info->print_gc_sections && o->size != 0)
695344c0 14429 /* xgettext:c-format */
9793eb77 14430 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
c08bb8dd 14431 o, sub);
c152c796
AM
14432 }
14433 }
14434
0a1b45a2 14435 return true;
c152c796
AM
14436}
14437
14438/* Propagate collected vtable information. This is called through
14439 elf_link_hash_traverse. */
14440
0a1b45a2 14441static bool
c152c796
AM
14442elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
14443{
c152c796 14444 /* Those that are not vtables. */
cbd0eecf
L
14445 if (h->start_stop
14446 || h->u2.vtable == NULL
14447 || h->u2.vtable->parent == NULL)
0a1b45a2 14448 return true;
c152c796
AM
14449
14450 /* Those vtables that do not have parents, we cannot merge. */
cbd0eecf 14451 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
0a1b45a2 14452 return true;
c152c796
AM
14453
14454 /* If we've already been done, exit. */
cbd0eecf 14455 if (h->u2.vtable->used && h->u2.vtable->used[-1])
0a1b45a2 14456 return true;
c152c796
AM
14457
14458 /* Make sure the parent's table is up to date. */
cbd0eecf 14459 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
c152c796 14460
cbd0eecf 14461 if (h->u2.vtable->used == NULL)
c152c796
AM
14462 {
14463 /* None of this table's entries were referenced. Re-use the
14464 parent's table. */
cbd0eecf
L
14465 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
14466 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
c152c796
AM
14467 }
14468 else
14469 {
14470 size_t n;
0a1b45a2 14471 bool *cu, *pu;
c152c796
AM
14472
14473 /* Or the parent's entries into ours. */
cbd0eecf 14474 cu = h->u2.vtable->used;
0a1b45a2 14475 cu[-1] = true;
cbd0eecf 14476 pu = h->u2.vtable->parent->u2.vtable->used;
c152c796
AM
14477 if (pu != NULL)
14478 {
14479 const struct elf_backend_data *bed;
14480 unsigned int log_file_align;
14481
14482 bed = get_elf_backend_data (h->root.u.def.section->owner);
14483 log_file_align = bed->s->log_file_align;
cbd0eecf 14484 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
c152c796
AM
14485 while (n--)
14486 {
14487 if (*pu)
0a1b45a2 14488 *cu = true;
c152c796
AM
14489 pu++;
14490 cu++;
14491 }
14492 }
14493 }
14494
0a1b45a2 14495 return true;
c152c796
AM
14496}
14497
a8dde0a2
L
14498struct link_info_ok
14499{
14500 struct bfd_link_info *info;
14501 bool ok;
14502};
14503
0a1b45a2 14504static bool
a8dde0a2
L
14505elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h,
14506 void *ptr)
c152c796
AM
14507{
14508 asection *sec;
14509 bfd_vma hstart, hend;
14510 Elf_Internal_Rela *relstart, *relend, *rel;
14511 const struct elf_backend_data *bed;
14512 unsigned int log_file_align;
a8dde0a2 14513 struct link_info_ok *info = (struct link_info_ok *) ptr;
c152c796 14514
c152c796
AM
14515 /* Take care of both those symbols that do not describe vtables as
14516 well as those that are not loaded. */
cbd0eecf
L
14517 if (h->start_stop
14518 || h->u2.vtable == NULL
14519 || h->u2.vtable->parent == NULL)
0a1b45a2 14520 return true;
c152c796
AM
14521
14522 BFD_ASSERT (h->root.type == bfd_link_hash_defined
14523 || h->root.type == bfd_link_hash_defweak);
14524
14525 sec = h->root.u.def.section;
14526 hstart = h->root.u.def.value;
14527 hend = hstart + h->size;
14528
a8dde0a2
L
14529 relstart = _bfd_elf_link_info_read_relocs (sec->owner, info->info,
14530 sec, NULL, NULL, true);
c152c796 14531 if (!relstart)
a8dde0a2 14532 return info->ok = false;
c152c796
AM
14533 bed = get_elf_backend_data (sec->owner);
14534 log_file_align = bed->s->log_file_align;
14535
056bafd4 14536 relend = relstart + sec->reloc_count;
c152c796
AM
14537
14538 for (rel = relstart; rel < relend; ++rel)
14539 if (rel->r_offset >= hstart && rel->r_offset < hend)
14540 {
14541 /* If the entry is in use, do nothing. */
cbd0eecf
L
14542 if (h->u2.vtable->used
14543 && (rel->r_offset - hstart) < h->u2.vtable->size)
c152c796
AM
14544 {
14545 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
cbd0eecf 14546 if (h->u2.vtable->used[entry])
c152c796
AM
14547 continue;
14548 }
14549 /* Otherwise, kill it. */
14550 rel->r_offset = rel->r_info = rel->r_addend = 0;
14551 }
14552
0a1b45a2 14553 return true;
c152c796
AM
14554}
14555
87538722
AM
14556/* Mark sections containing dynamically referenced symbols. When
14557 building shared libraries, we must assume that any visible symbol is
14558 referenced. */
715df9b8 14559
0a1b45a2 14560bool
64d03ab5 14561bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 14562{
87538722 14563 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 14564 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 14565
715df9b8
EB
14566 if ((h->root.type == bfd_link_hash_defined
14567 || h->root.type == bfd_link_hash_defweak)
8ee10e86
AM
14568 && (!h->start_stop
14569 || h->root.ldscript_def
14570 || !info->start_stop_gc)
d664fd41 14571 && ((h->ref_dynamic && !h->forced_local)
c4621b33 14572 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 14573 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 14574 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 14575 && (!bfd_link_executable (info)
22185505 14576 || info->gc_keep_exported
b407645f
AM
14577 || info->export_dynamic
14578 || (h->dynamic
14579 && d != NULL
14580 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 14581 && (h->versioned >= versioned
54e8959c
L
14582 || !bfd_hide_sym_by_version (info->version_info,
14583 h->root.root.string)))))
715df9b8
EB
14584 h->root.u.def.section->flags |= SEC_KEEP;
14585
0a1b45a2 14586 return true;
715df9b8 14587}
3b36f7e6 14588
74f0fb50
AM
14589/* Keep all sections containing symbols undefined on the command-line,
14590 and the section containing the entry symbol. */
14591
14592void
14593_bfd_elf_gc_keep (struct bfd_link_info *info)
14594{
14595 struct bfd_sym_chain *sym;
14596
14597 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
14598 {
14599 struct elf_link_hash_entry *h;
14600
14601 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
0a1b45a2 14602 false, false, false);
74f0fb50
AM
14603
14604 if (h != NULL
14605 && (h->root.type == bfd_link_hash_defined
14606 || h->root.type == bfd_link_hash_defweak)
2f5541f3 14607 && !bfd_is_const_section (h->root.u.def.section))
74f0fb50
AM
14608 h->root.u.def.section->flags |= SEC_KEEP;
14609 }
14610}
14611
0a1b45a2 14612bool
2f0c68f2
CM
14613bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
14614 struct bfd_link_info *info)
14615{
14616 bfd *ibfd = info->input_bfds;
14617
14618 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14619 {
14620 asection *sec;
14621 struct elf_reloc_cookie cookie;
14622
14623 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14624 continue;
57963c05
AM
14625 sec = ibfd->sections;
14626 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14627 continue;
2f0c68f2 14628
3428c771 14629 if (!init_reloc_cookie (&cookie, info, ibfd, false))
0a1b45a2 14630 return false;
2f0c68f2
CM
14631
14632 for (sec = ibfd->sections; sec; sec = sec->next)
14633 {
08dedd66 14634 if (startswith (bfd_section_name (sec), ".eh_frame_entry")
3428c771
L
14635 && init_reloc_cookie_rels (&cookie, info, ibfd, sec,
14636 false))
2f0c68f2
CM
14637 {
14638 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
14639 fini_reloc_cookie_rels (&cookie, sec);
14640 }
14641 }
14642 }
0a1b45a2 14643 return true;
2f0c68f2
CM
14644}
14645
c152c796
AM
14646/* Do mark and sweep of unused sections. */
14647
0a1b45a2 14648bool
c152c796
AM
14649bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
14650{
0a1b45a2 14651 bool ok = true;
c152c796 14652 bfd *sub;
6a5bb875 14653 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 14654 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 14655 struct elf_link_hash_table *htab;
a8dde0a2 14656 struct link_info_ok info_ok;
c152c796 14657
64d03ab5 14658 if (!bed->can_gc_sections
715df9b8 14659 || !is_elf_hash_table (info->hash))
c152c796 14660 {
9793eb77 14661 _bfd_error_handler(_("warning: gc-sections option ignored"));
0a1b45a2 14662 return true;
c152c796
AM
14663 }
14664
74f0fb50 14665 bed->gc_keep (info);
da44f4e5 14666 htab = elf_hash_table (info);
74f0fb50 14667
9d0a14d3
RS
14668 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14669 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
14670 for (sub = info->input_bfds;
14671 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14672 sub = sub->link.next)
9d0a14d3
RS
14673 {
14674 asection *sec;
14675 struct elf_reloc_cookie cookie;
14676
57963c05
AM
14677 sec = sub->sections;
14678 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14679 continue;
9d0a14d3 14680 sec = bfd_get_section_by_name (sub, ".eh_frame");
3428c771
L
14681 while (sec && init_reloc_cookie_for_section (&cookie, info, sec,
14682 false))
9d0a14d3
RS
14683 {
14684 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
14685 if (elf_section_data (sec)->sec_info
14686 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
14687 elf_eh_frame_section (sub) = sec;
14688 fini_reloc_cookie_for_section (&cookie, sec);
199af150 14689 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
14690 }
14691 }
9d0a14d3 14692
c152c796 14693 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 14694 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796 14695 if (!ok)
0a1b45a2 14696 return false;
c152c796
AM
14697
14698 /* Kill the vtable relocations that were not used. */
a8dde0a2
L
14699 info_ok.info = info;
14700 info_ok.ok = true;
14701 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &info_ok);
14702 if (!info_ok.ok)
0a1b45a2 14703 return false;
c152c796 14704
715df9b8 14705 /* Mark dynamically referenced symbols. */
22185505 14706 if (htab->dynamic_sections_created || info->gc_keep_exported)
da44f4e5 14707 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 14708
715df9b8 14709 /* Grovel through relocs to find out who stays ... */
64d03ab5 14710 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 14711 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
14712 {
14713 asection *o;
14714
b19a8f85 14715 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 14716 || elf_object_id (sub) != elf_hash_table_id (htab)
b19a8f85 14717 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
14718 continue;
14719
57963c05
AM
14720 o = sub->sections;
14721 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14722 continue;
14723
7f6ab9f8
AM
14724 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14725 Also treat note sections as a root, if the section is not part
8b6f4cd3
L
14726 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14727 well as FINI_ARRAY sections for ld -r. */
c152c796 14728 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
14729 if (!o->gc_mark
14730 && (o->flags & SEC_EXCLUDE) == 0
24007750 14731 && ((o->flags & SEC_KEEP) != 0
8b6f4cd3
L
14732 || (bfd_link_relocatable (info)
14733 && ((elf_section_data (o)->this_hdr.sh_type
14734 == SHT_PREINIT_ARRAY)
14735 || (elf_section_data (o)->this_hdr.sh_type
14736 == SHT_INIT_ARRAY)
14737 || (elf_section_data (o)->this_hdr.sh_type
14738 == SHT_FINI_ARRAY)))
7f6ab9f8 14739 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
7026832e 14740 && elf_next_in_group (o) == NULL
99fabbc9
JL
14741 && elf_linked_to_section (o) == NULL)
14742 || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14743 && (elf_section_flags (o) & SHF_GNU_RETAIN))))
7f6ab9f8
AM
14744 {
14745 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
0a1b45a2 14746 return false;
7f6ab9f8 14747 }
c152c796
AM
14748 }
14749
6a5bb875 14750 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 14751 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 14752
c152c796 14753 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 14754 return elf_gc_sweep (abfd, info);
c152c796
AM
14755}
14756\f
14757/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14758
0a1b45a2 14759bool
c152c796
AM
14760bfd_elf_gc_record_vtinherit (bfd *abfd,
14761 asection *sec,
14762 struct elf_link_hash_entry *h,
14763 bfd_vma offset)
14764{
14765 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14766 struct elf_link_hash_entry **search, *child;
ef53be89 14767 size_t extsymcount;
c152c796
AM
14768 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14769
14770 /* The sh_info field of the symtab header tells us where the
14771 external symbols start. We don't care about the local symbols at
14772 this point. */
14773 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
14774 if (!elf_bad_symtab (abfd))
14775 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
14776
14777 sym_hashes = elf_sym_hashes (abfd);
36f61bf2 14778 sym_hashes_end = PTR_ADD (sym_hashes, extsymcount);
c152c796
AM
14779
14780 /* Hunt down the child symbol, which is in this section at the same
14781 offset as the relocation. */
14782 for (search = sym_hashes; search != sym_hashes_end; ++search)
14783 {
14784 if ((child = *search) != NULL
14785 && (child->root.type == bfd_link_hash_defined
14786 || child->root.type == bfd_link_hash_defweak)
14787 && child->root.u.def.section == sec
14788 && child->root.u.def.value == offset)
14789 goto win;
14790 }
14791
695344c0 14792 /* xgettext:c-format */
9793eb77 14793 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
2dcf00ce 14794 abfd, sec, (uint64_t) offset);
c152c796 14795 bfd_set_error (bfd_error_invalid_operation);
0a1b45a2 14796 return false;
c152c796
AM
14797
14798 win:
cbd0eecf 14799 if (!child->u2.vtable)
f6e332e6 14800 {
cbd0eecf
L
14801 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
14802 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
14803 if (!child->u2.vtable)
0a1b45a2 14804 return false;
f6e332e6 14805 }
c152c796
AM
14806 if (!h)
14807 {
14808 /* This *should* only be the absolute section. It could potentially
14809 be that someone has defined a non-global vtable though, which
14810 would be bad. It isn't worth paging in the local symbols to be
14811 sure though; that case should simply be handled by the assembler. */
14812
cbd0eecf 14813 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
14814 }
14815 else
cbd0eecf 14816 child->u2.vtable->parent = h;
c152c796 14817
0a1b45a2 14818 return true;
c152c796
AM
14819}
14820
14821/* Called from check_relocs to record the existence of a VTENTRY reloc. */
14822
0a1b45a2 14823bool
a0ea3a14 14824bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
c152c796
AM
14825 struct elf_link_hash_entry *h,
14826 bfd_vma addend)
14827{
14828 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14829 unsigned int log_file_align = bed->s->log_file_align;
14830
a0ea3a14
L
14831 if (!h)
14832 {
14833 /* xgettext:c-format */
14834 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14835 abfd, sec);
14836 bfd_set_error (bfd_error_bad_value);
0a1b45a2 14837 return false;
a0ea3a14
L
14838 }
14839
cbd0eecf 14840 if (!h->u2.vtable)
f6e332e6 14841 {
cbd0eecf
L
14842 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14843 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14844 if (!h->u2.vtable)
0a1b45a2 14845 return false;
f6e332e6
AM
14846 }
14847
cbd0eecf 14848 if (addend >= h->u2.vtable->size)
c152c796
AM
14849 {
14850 size_t size, bytes, file_align;
0a1b45a2 14851 bool *ptr = h->u2.vtable->used;
c152c796
AM
14852
14853 /* While the symbol is undefined, we have to be prepared to handle
14854 a zero size. */
14855 file_align = 1 << log_file_align;
14856 if (h->root.type == bfd_link_hash_undefined)
14857 size = addend + file_align;
14858 else
14859 {
14860 size = h->size;
14861 if (addend >= size)
14862 {
14863 /* Oops! We've got a reference past the defined end of
14864 the table. This is probably a bug -- shall we warn? */
14865 size = addend + file_align;
14866 }
14867 }
14868 size = (size + file_align - 1) & -file_align;
14869
14870 /* Allocate one extra entry for use as a "done" flag for the
14871 consolidation pass. */
0a1b45a2 14872 bytes = ((size >> log_file_align) + 1) * sizeof (bool);
c152c796
AM
14873
14874 if (ptr)
14875 {
0a1b45a2 14876 ptr = (bool *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
14877
14878 if (ptr != NULL)
14879 {
14880 size_t oldbytes;
14881
cbd0eecf 14882 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
0a1b45a2 14883 * sizeof (bool));
c152c796
AM
14884 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14885 }
14886 }
14887 else
0a1b45a2 14888 ptr = (bool *) bfd_zmalloc (bytes);
c152c796
AM
14889
14890 if (ptr == NULL)
0a1b45a2 14891 return false;
c152c796
AM
14892
14893 /* And arrange for that done flag to be at index -1. */
cbd0eecf
L
14894 h->u2.vtable->used = ptr + 1;
14895 h->u2.vtable->size = size;
c152c796
AM
14896 }
14897
0a1b45a2 14898 h->u2.vtable->used[addend >> log_file_align] = true;
c152c796 14899
0a1b45a2 14900 return true;
c152c796
AM
14901}
14902
ae17ab41
CM
14903/* Map an ELF section header flag to its corresponding string. */
14904typedef struct
14905{
14906 char *flag_name;
14907 flagword flag_value;
14908} elf_flags_to_name_table;
14909
3f75e1d6 14910static const elf_flags_to_name_table elf_flags_to_names [] =
ae17ab41
CM
14911{
14912 { "SHF_WRITE", SHF_WRITE },
14913 { "SHF_ALLOC", SHF_ALLOC },
14914 { "SHF_EXECINSTR", SHF_EXECINSTR },
14915 { "SHF_MERGE", SHF_MERGE },
14916 { "SHF_STRINGS", SHF_STRINGS },
14917 { "SHF_INFO_LINK", SHF_INFO_LINK},
14918 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14919 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14920 { "SHF_GROUP", SHF_GROUP },
14921 { "SHF_TLS", SHF_TLS },
14922 { "SHF_MASKOS", SHF_MASKOS },
14923 { "SHF_EXCLUDE", SHF_EXCLUDE },
14924};
14925
b9c361e0 14926/* Returns TRUE if the section is to be included, otherwise FALSE. */
0a1b45a2 14927bool
ae17ab41 14928bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 14929 struct flag_info *flaginfo,
b9c361e0 14930 asection *section)
ae17ab41 14931{
8b127cbc 14932 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 14933
8b127cbc 14934 if (!flaginfo->flags_initialized)
ae17ab41 14935 {
8b127cbc
AM
14936 bfd *obfd = info->output_bfd;
14937 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14938 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
14939 int with_hex = 0;
14940 int without_hex = 0;
14941
8b127cbc 14942 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 14943 {
b9c361e0 14944 unsigned i;
8b127cbc 14945 flagword (*lookup) (char *);
ae17ab41 14946
8b127cbc
AM
14947 lookup = bed->elf_backend_lookup_section_flags_hook;
14948 if (lookup != NULL)
ae17ab41 14949 {
8b127cbc 14950 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
14951
14952 if (hexval != 0)
14953 {
14954 if (tf->with == with_flags)
14955 with_hex |= hexval;
14956 else if (tf->with == without_flags)
14957 without_hex |= hexval;
0a1b45a2 14958 tf->valid = true;
b9c361e0
JL
14959 continue;
14960 }
ae17ab41 14961 }
8b127cbc 14962 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 14963 {
8b127cbc 14964 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
14965 {
14966 if (tf->with == with_flags)
14967 with_hex |= elf_flags_to_names[i].flag_value;
14968 else if (tf->with == without_flags)
14969 without_hex |= elf_flags_to_names[i].flag_value;
0a1b45a2 14970 tf->valid = true;
b9c361e0
JL
14971 break;
14972 }
14973 }
8b127cbc 14974 if (!tf->valid)
b9c361e0 14975 {
68ffbac6 14976 info->callbacks->einfo
9793eb77 14977 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
0a1b45a2 14978 return false;
ae17ab41
CM
14979 }
14980 }
0a1b45a2 14981 flaginfo->flags_initialized = true;
8b127cbc
AM
14982 flaginfo->only_with_flags |= with_hex;
14983 flaginfo->not_with_flags |= without_hex;
ae17ab41 14984 }
ae17ab41 14985
8b127cbc 14986 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
0a1b45a2 14987 return false;
b9c361e0 14988
8b127cbc 14989 if ((flaginfo->not_with_flags & sh_flags) != 0)
0a1b45a2 14990 return false;
b9c361e0 14991
0a1b45a2 14992 return true;
ae17ab41
CM
14993}
14994
c152c796
AM
14995struct alloc_got_off_arg {
14996 bfd_vma gotoff;
10455f89 14997 struct bfd_link_info *info;
c152c796
AM
14998};
14999
15000/* We need a special top-level link routine to convert got reference counts
15001 to real got offsets. */
15002
0a1b45a2 15003static bool
c152c796
AM
15004elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
15005{
a50b1753 15006 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
15007 bfd *obfd = gofarg->info->output_bfd;
15008 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 15009
c152c796
AM
15010 if (h->got.refcount > 0)
15011 {
15012 h->got.offset = gofarg->gotoff;
10455f89 15013 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
15014 }
15015 else
15016 h->got.offset = (bfd_vma) -1;
15017
0a1b45a2 15018 return true;
c152c796
AM
15019}
15020
15021/* And an accompanying bit to work out final got entry offsets once
15022 we're done. Should be called from final_link. */
15023
0a1b45a2 15024bool
c152c796
AM
15025bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
15026 struct bfd_link_info *info)
15027{
15028 bfd *i;
15029 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15030 bfd_vma gotoff;
c152c796
AM
15031 struct alloc_got_off_arg gofarg;
15032
10455f89
HPN
15033 BFD_ASSERT (abfd == info->output_bfd);
15034
c152c796 15035 if (! is_elf_hash_table (info->hash))
0a1b45a2 15036 return false;
c152c796
AM
15037
15038 /* The GOT offset is relative to the .got section, but the GOT header is
15039 put into the .got.plt section, if the backend uses it. */
15040 if (bed->want_got_plt)
15041 gotoff = 0;
15042 else
15043 gotoff = bed->got_header_size;
15044
15045 /* Do the local .got entries first. */
c72f2fb2 15046 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
15047 {
15048 bfd_signed_vma *local_got;
ef53be89 15049 size_t j, locsymcount;
c152c796
AM
15050 Elf_Internal_Shdr *symtab_hdr;
15051
15052 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
15053 continue;
15054
15055 local_got = elf_local_got_refcounts (i);
15056 if (!local_got)
15057 continue;
15058
15059 symtab_hdr = &elf_tdata (i)->symtab_hdr;
15060 if (elf_bad_symtab (i))
15061 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
15062 else
15063 locsymcount = symtab_hdr->sh_info;
15064
15065 for (j = 0; j < locsymcount; ++j)
15066 {
15067 if (local_got[j] > 0)
15068 {
15069 local_got[j] = gotoff;
10455f89 15070 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
15071 }
15072 else
15073 local_got[j] = (bfd_vma) -1;
15074 }
15075 }
15076
15077 /* Then the global .got entries. .plt refcounts are handled by
15078 adjust_dynamic_symbol */
15079 gofarg.gotoff = gotoff;
10455f89 15080 gofarg.info = info;
c152c796
AM
15081 elf_link_hash_traverse (elf_hash_table (info),
15082 elf_gc_allocate_got_offsets,
15083 &gofarg);
0a1b45a2 15084 return true;
c152c796
AM
15085}
15086
15087/* Many folk need no more in the way of final link than this, once
15088 got entry reference counting is enabled. */
15089
0a1b45a2 15090bool
c152c796
AM
15091bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
15092{
15093 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
0a1b45a2 15094 return false;
c152c796
AM
15095
15096 /* Invoke the regular ELF backend linker to do all the work. */
15097 return bfd_elf_final_link (abfd, info);
15098}
15099
0a1b45a2 15100bool
c152c796
AM
15101bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
15102{
a50b1753 15103 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
15104
15105 if (rcookie->bad_symtab)
15106 rcookie->rel = rcookie->rels;
15107
15108 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
15109 {
15110 unsigned long r_symndx;
15111
15112 if (! rcookie->bad_symtab)
15113 if (rcookie->rel->r_offset > offset)
0a1b45a2 15114 return false;
c152c796
AM
15115 if (rcookie->rel->r_offset != offset)
15116 continue;
15117
15118 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 15119 if (r_symndx == STN_UNDEF)
0a1b45a2 15120 return true;
c152c796 15121
f9978def 15122 struct elf_link_hash_entry *h;
c152c796 15123
931494c9 15124 h = get_ext_sym_hash_from_cookie (rcookie, r_symndx);
f9978def
NC
15125
15126 if (h != NULL)
15127 {
c152c796
AM
15128 if ((h->root.type == bfd_link_hash_defined
15129 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
15130 && (h->root.u.def.section->owner != rcookie->abfd
15131 || h->root.u.def.section->kept_section != NULL
15132 || discarded_section (h->root.u.def.section)))
0a1b45a2 15133 return true;
c152c796
AM
15134 }
15135 else
15136 {
b4258590
NC
15137 if (r_symndx >= rcookie->locsymcount)
15138 /* This can happen with corrupt input. */
15139 return false;
15140
c152c796
AM
15141 /* It's not a relocation against a global symbol,
15142 but it could be a relocation against a local
15143 symbol for a discarded section. */
15144 asection *isec;
15145 Elf_Internal_Sym *isym;
15146
15147 /* Need to: get the symbol; get the section. */
15148 isym = &rcookie->locsyms[r_symndx];
cb33740c 15149 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
15150 if (isec != NULL
15151 && (isec->kept_section != NULL
15152 || discarded_section (isec)))
0a1b45a2 15153 return true;
c152c796 15154 }
f9978def 15155
0a1b45a2 15156 return false;
c152c796 15157 }
0a1b45a2 15158 return false;
c152c796
AM
15159}
15160
15161/* Discard unneeded references to discarded sections.
75938853
AM
15162 Returns -1 on error, 1 if any section's size was changed, 0 if
15163 nothing changed. This function assumes that the relocations are in
15164 sorted order, which is true for all known assemblers. */
c152c796 15165
75938853 15166int
c152c796
AM
15167bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
15168{
15169 struct elf_reloc_cookie cookie;
18cd5bce 15170 asection *o;
c152c796 15171 bfd *abfd;
75938853 15172 int changed = 0;
c152c796
AM
15173
15174 if (info->traditional_format
15175 || !is_elf_hash_table (info->hash))
75938853 15176 return 0;
c152c796 15177
18cd5bce
AM
15178 o = bfd_get_section_by_name (output_bfd, ".stab");
15179 if (o != NULL)
c152c796 15180 {
18cd5bce 15181 asection *i;
c152c796 15182
18cd5bce 15183 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 15184 {
18cd5bce
AM
15185 if (i->size == 0
15186 || i->reloc_count == 0
15187 || i->sec_info_type != SEC_INFO_TYPE_STABS)
15188 continue;
c152c796 15189
18cd5bce
AM
15190 abfd = i->owner;
15191 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15192 continue;
c152c796 15193
3428c771 15194 if (!init_reloc_cookie_for_section (&cookie, info, i, false))
75938853 15195 return -1;
c152c796 15196
18cd5bce
AM
15197 if (_bfd_discard_section_stabs (abfd, i,
15198 elf_section_data (i)->sec_info,
5241d853
RS
15199 bfd_elf_reloc_symbol_deleted_p,
15200 &cookie))
75938853 15201 changed = 1;
18cd5bce
AM
15202
15203 fini_reloc_cookie_for_section (&cookie, i);
c152c796 15204 }
18cd5bce
AM
15205 }
15206
2f0c68f2
CM
15207 o = NULL;
15208 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
15209 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
15210 if (o != NULL)
15211 {
15212 asection *i;
d7153c4a 15213 int eh_changed = 0;
66631823 15214 unsigned int eh_alignment; /* Octets. */
c152c796 15215
18cd5bce 15216 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 15217 {
18cd5bce
AM
15218 if (i->size == 0)
15219 continue;
15220
15221 abfd = i->owner;
15222 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15223 continue;
15224
3428c771 15225 if (!init_reloc_cookie_for_section (&cookie, info, i, false))
75938853 15226 return -1;
18cd5bce
AM
15227
15228 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
15229 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
15230 bfd_elf_reloc_symbol_deleted_p,
15231 &cookie))
d7153c4a
AM
15232 {
15233 eh_changed = 1;
15234 if (i->size != i->rawsize)
15235 changed = 1;
15236 }
18cd5bce
AM
15237
15238 fini_reloc_cookie_for_section (&cookie, i);
c152c796 15239 }
9866ffe2 15240
66631823
CE
15241 eh_alignment = ((1 << o->alignment_power)
15242 * bfd_octets_per_byte (output_bfd, o));
9866ffe2
AM
15243 /* Skip over zero terminator, and prevent empty sections from
15244 adding alignment padding at the end. */
15245 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
15246 if (i->size == 0)
15247 i->flags |= SEC_EXCLUDE;
15248 else if (i->size > 4)
15249 break;
15250 /* The last non-empty eh_frame section doesn't need padding. */
15251 if (i != NULL)
15252 i = i->map_tail.s;
15253 /* Any prior sections must pad the last FDE out to the output
15254 section alignment. Otherwise we might have zero padding
15255 between sections, which would be seen as a terminator. */
15256 for (; i != NULL; i = i->map_tail.s)
15257 if (i->size == 4)
15258 /* All but the last zero terminator should have been removed. */
15259 BFD_FAIL ();
15260 else
15261 {
15262 bfd_size_type size
15263 = (i->size + eh_alignment - 1) & -eh_alignment;
15264 if (i->size != size)
af471f82 15265 {
9866ffe2
AM
15266 i->size = size;
15267 changed = 1;
15268 eh_changed = 1;
af471f82 15269 }
9866ffe2 15270 }
d7153c4a
AM
15271 if (eh_changed)
15272 elf_link_hash_traverse (elf_hash_table (info),
15273 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
18cd5bce 15274 }
c152c796 15275
cf0e0a0b
IB
15276 o = bfd_get_section_by_name (output_bfd, ".sframe");
15277 if (o != NULL)
15278 {
15279 asection *i;
15280
15281 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
15282 {
15283 if (i->size == 0)
15284 continue;
15285
15286 abfd = i->owner;
15287 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15288 continue;
15289
3428c771 15290 if (!init_reloc_cookie_for_section (&cookie, info, i, false))
cf0e0a0b
IB
15291 return -1;
15292
15293 if (_bfd_elf_parse_sframe (abfd, info, i, &cookie))
15294 {
15295 if (_bfd_elf_discard_section_sframe (i,
15296 bfd_elf_reloc_symbol_deleted_p,
15297 &cookie))
15298 {
15299 if (i->size != i->rawsize)
15300 changed = 1;
15301 }
15302 }
15303 fini_reloc_cookie_for_section (&cookie, i);
15304 }
15305 /* Update the reference to the output .sframe section. Used to
15306 determine later if PT_GNU_SFRAME segment is to be generated. */
15307 if (!_bfd_elf_set_section_sframe (output_bfd, info))
15308 return -1;
15309 }
15310
18cd5bce
AM
15311 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
15312 {
15313 const struct elf_backend_data *bed;
57963c05 15314 asection *s;
c152c796 15315
18cd5bce
AM
15316 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
15317 continue;
57963c05
AM
15318 s = abfd->sections;
15319 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
15320 continue;
18cd5bce
AM
15321
15322 bed = get_elf_backend_data (abfd);
15323
15324 if (bed->elf_backend_discard_info != NULL)
15325 {
3428c771 15326 if (!init_reloc_cookie (&cookie, info, abfd, false))
75938853 15327 return -1;
18cd5bce
AM
15328
15329 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 15330 changed = 1;
18cd5bce
AM
15331
15332 fini_reloc_cookie (&cookie, abfd);
15333 }
c152c796
AM
15334 }
15335
2f0c68f2
CM
15336 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
15337 _bfd_elf_end_eh_frame_parsing (info);
15338
701fe09b 15339 if (_bfd_elf_discard_section_eh_frame_hdr (info))
75938853 15340 changed = 1;
c152c796 15341
75938853 15342 return changed;
c152c796 15343}
082b7297 15344
0a1b45a2 15345bool
0c511000 15346_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 15347 asection *sec,
c0f00686 15348 struct bfd_link_info *info)
082b7297
L
15349{
15350 flagword flags;
c77ec726 15351 const char *name, *key;
082b7297
L
15352 struct bfd_section_already_linked *l;
15353 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 15354
c77ec726 15355 if (sec->output_section == bfd_abs_section_ptr)
0a1b45a2 15356 return false;
0c511000 15357
c77ec726 15358 flags = sec->flags;
0c511000 15359
c77ec726
AM
15360 /* Return if it isn't a linkonce section. A comdat group section
15361 also has SEC_LINK_ONCE set. */
15362 if ((flags & SEC_LINK_ONCE) == 0)
0a1b45a2 15363 return false;
0c511000 15364
c77ec726
AM
15365 /* Don't put group member sections on our list of already linked
15366 sections. They are handled as a group via their group section. */
15367 if (elf_sec_group (sec) != NULL)
0a1b45a2 15368 return false;
0c511000 15369
c77ec726
AM
15370 /* For a SHT_GROUP section, use the group signature as the key. */
15371 name = sec->name;
15372 if ((flags & SEC_GROUP) != 0
15373 && elf_next_in_group (sec) != NULL
15374 && elf_group_name (elf_next_in_group (sec)) != NULL)
15375 key = elf_group_name (elf_next_in_group (sec));
15376 else
15377 {
15378 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
08dedd66 15379 if (startswith (name, ".gnu.linkonce.")
c77ec726
AM
15380 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
15381 key++;
0c511000 15382 else
c77ec726
AM
15383 /* Must be a user linkonce section that doesn't follow gcc's
15384 naming convention. In this case we won't be matching
15385 single member groups. */
15386 key = name;
0c511000 15387 }
6d2cd210 15388
c77ec726 15389 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
15390
15391 for (l = already_linked_list->entry; l != NULL; l = l->next)
15392 {
c2370991 15393 /* We may have 2 different types of sections on the list: group
c77ec726
AM
15394 sections with a signature of <key> (<key> is some string),
15395 and linkonce sections named .gnu.linkonce.<type>.<key>.
15396 Match like sections. LTO plugin sections are an exception.
15397 They are always named .gnu.linkonce.t.<key> and match either
15398 type of section. */
15399 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
15400 && ((flags & SEC_GROUP) != 0
15401 || strcmp (name, l->sec->name) == 0))
e8a83e93
MB
15402 || (l->sec->owner->flags & BFD_PLUGIN) != 0
15403 || (sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
15404 {
15405 /* The section has already been linked. See if we should
6d2cd210 15406 issue a warning. */
c77ec726 15407 if (!_bfd_handle_already_linked (sec, l, info))
0a1b45a2 15408 return false;
082b7297 15409
c77ec726 15410 if (flags & SEC_GROUP)
3d7f7666 15411 {
c77ec726
AM
15412 asection *first = elf_next_in_group (sec);
15413 asection *s = first;
3d7f7666 15414
c77ec726 15415 while (s != NULL)
3d7f7666 15416 {
c77ec726
AM
15417 s->output_section = bfd_abs_section_ptr;
15418 /* Record which group discards it. */
15419 s->kept_section = l->sec;
15420 s = elf_next_in_group (s);
15421 /* These lists are circular. */
15422 if (s == first)
15423 break;
3d7f7666
L
15424 }
15425 }
082b7297 15426
0a1b45a2 15427 return true;
082b7297
L
15428 }
15429 }
15430
c77ec726
AM
15431 /* A single member comdat group section may be discarded by a
15432 linkonce section and vice versa. */
15433 if ((flags & SEC_GROUP) != 0)
3d7f7666 15434 {
c77ec726 15435 asection *first = elf_next_in_group (sec);
c2370991 15436
c77ec726
AM
15437 if (first != NULL && elf_next_in_group (first) == first)
15438 /* Check this single member group against linkonce sections. */
15439 for (l = already_linked_list->entry; l != NULL; l = l->next)
15440 if ((l->sec->flags & SEC_GROUP) == 0
15441 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
15442 {
15443 first->output_section = bfd_abs_section_ptr;
15444 first->kept_section = l->sec;
15445 sec->output_section = bfd_abs_section_ptr;
15446 break;
15447 }
15448 }
15449 else
15450 /* Check this linkonce section against single member groups. */
15451 for (l = already_linked_list->entry; l != NULL; l = l->next)
15452 if (l->sec->flags & SEC_GROUP)
6d2cd210 15453 {
c77ec726 15454 asection *first = elf_next_in_group (l->sec);
6d2cd210 15455
c77ec726
AM
15456 if (first != NULL
15457 && elf_next_in_group (first) == first
15458 && bfd_elf_match_symbols_in_sections (first, sec, info))
15459 {
15460 sec->output_section = bfd_abs_section_ptr;
15461 sec->kept_section = first;
15462 break;
15463 }
6d2cd210 15464 }
0c511000 15465
c77ec726
AM
15466 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
15467 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
15468 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
15469 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
15470 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
15471 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
15472 `.gnu.linkonce.t.F' section from a different bfd not requiring any
15473 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
15474 The reverse order cannot happen as there is never a bfd with only the
15475 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
15476 matter as here were are looking only for cross-bfd sections. */
15477
08dedd66 15478 if ((flags & SEC_GROUP) == 0 && startswith (name, ".gnu.linkonce.r."))
c77ec726
AM
15479 for (l = already_linked_list->entry; l != NULL; l = l->next)
15480 if ((l->sec->flags & SEC_GROUP) == 0
08dedd66 15481 && startswith (l->sec->name, ".gnu.linkonce.t."))
c77ec726
AM
15482 {
15483 if (abfd != l->sec->owner)
15484 sec->output_section = bfd_abs_section_ptr;
15485 break;
15486 }
80c29487 15487
082b7297 15488 /* This is the first section with this name. Record it. */
c77ec726 15489 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
d2616191 15490 info->callbacks->fatal (_("%P: already_linked_table: %E\n"));
c77ec726 15491 return sec->output_section == bfd_abs_section_ptr;
082b7297 15492}
81e1b023 15493
0a1b45a2 15494bool
a4d8e49b
L
15495_bfd_elf_common_definition (Elf_Internal_Sym *sym)
15496{
15497 return sym->st_shndx == SHN_COMMON;
15498}
15499
15500unsigned int
15501_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
15502{
15503 return SHN_COMMON;
15504}
15505
15506asection *
15507_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
15508{
15509 return bfd_com_section_ptr;
15510}
10455f89
HPN
15511
15512bfd_vma
15513_bfd_elf_default_got_elt_size (bfd *abfd,
15514 struct bfd_link_info *info ATTRIBUTE_UNUSED,
15515 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
15516 bfd *ibfd ATTRIBUTE_UNUSED,
15517 unsigned long symndx ATTRIBUTE_UNUSED)
15518{
15519 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15520 return bed->s->arch_size / 8;
15521}
83bac4b0
NC
15522
15523/* Routines to support the creation of dynamic relocs. */
15524
83bac4b0
NC
15525/* Returns the name of the dynamic reloc section associated with SEC. */
15526
15527static const char *
15528get_dynamic_reloc_section_name (bfd * abfd,
15529 asection * sec,
0a1b45a2 15530 bool is_rela)
83bac4b0 15531{
ddcf1fcf 15532 char *name;
fd361982 15533 const char *old_name = bfd_section_name (sec);
ddcf1fcf 15534 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 15535
ddcf1fcf 15536 if (old_name == NULL)
83bac4b0
NC
15537 return NULL;
15538
ddcf1fcf 15539 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 15540 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
15541
15542 return name;
15543}
15544
15545/* Returns the dynamic reloc section associated with SEC.
15546 If necessary compute the name of the dynamic reloc section based
15547 on SEC's name (looked up in ABFD's string table) and the setting
15548 of IS_RELA. */
15549
15550asection *
0a1b45a2
AM
15551_bfd_elf_get_dynamic_reloc_section (bfd *abfd,
15552 asection *sec,
15553 bool is_rela)
83bac4b0 15554{
0a1b45a2 15555 asection *reloc_sec = elf_section_data (sec)->sreloc;
83bac4b0
NC
15556
15557 if (reloc_sec == NULL)
15558 {
0a1b45a2 15559 const char *name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
83bac4b0
NC
15560
15561 if (name != NULL)
15562 {
3d4d4302 15563 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
15564
15565 if (reloc_sec != NULL)
15566 elf_section_data (sec)->sreloc = reloc_sec;
15567 }
15568 }
15569
15570 return reloc_sec;
15571}
15572
15573/* Returns the dynamic reloc section associated with SEC. If the
15574 section does not exist it is created and attached to the DYNOBJ
15575 bfd and stored in the SRELOC field of SEC's elf_section_data
15576 structure.
f8076f98 15577
83bac4b0
NC
15578 ALIGNMENT is the alignment for the newly created section and
15579 IS_RELA defines whether the name should be .rela.<SEC's name>
15580 or .rel.<SEC's name>. The section name is looked up in the
15581 string table associated with ABFD. */
15582
15583asection *
ca4be51c
AM
15584_bfd_elf_make_dynamic_reloc_section (asection *sec,
15585 bfd *dynobj,
15586 unsigned int alignment,
15587 bfd *abfd,
0a1b45a2 15588 bool is_rela)
83bac4b0
NC
15589{
15590 asection * reloc_sec = elf_section_data (sec)->sreloc;
15591
15592 if (reloc_sec == NULL)
15593 {
15594 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15595
15596 if (name == NULL)
15597 return NULL;
15598
3d4d4302 15599 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
15600
15601 if (reloc_sec == NULL)
15602 {
3d4d4302
AM
15603 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
15604 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
15605 if ((sec->flags & SEC_ALLOC) != 0)
15606 flags |= SEC_ALLOC | SEC_LOAD;
15607
3d4d4302 15608 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
15609 if (reloc_sec != NULL)
15610 {
8877b5e5
AM
15611 /* _bfd_elf_get_sec_type_attr chooses a section type by
15612 name. Override as it may be wrong, eg. for a user
15613 section named "auto" we'll get ".relauto" which is
15614 seen to be a .rela section. */
15615 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
fd361982 15616 if (!bfd_set_section_alignment (reloc_sec, alignment))
83bac4b0
NC
15617 reloc_sec = NULL;
15618 }
15619 }
15620
15621 elf_section_data (sec)->sreloc = reloc_sec;
15622 }
15623
15624 return reloc_sec;
15625}
1338dd10 15626
bffebb6b
AM
15627/* Copy the ELF symbol type and other attributes for a linker script
15628 assignment from HSRC to HDEST. Generally this should be treated as
15629 if we found a strong non-dynamic definition for HDEST (except that
15630 ld ignores multiple definition errors). */
1338dd10 15631void
bffebb6b
AM
15632_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
15633 struct bfd_link_hash_entry *hdest,
15634 struct bfd_link_hash_entry *hsrc)
1338dd10 15635{
bffebb6b
AM
15636 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
15637 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
15638 Elf_Internal_Sym isym;
1338dd10
PB
15639
15640 ehdest->type = ehsrc->type;
35fc36a8 15641 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
15642
15643 isym.st_other = ehsrc->other;
0a1b45a2 15644 elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, true, false);
1338dd10 15645}
351f65ca
L
15646
15647/* Append a RELA relocation REL to section S in BFD. */
15648
15649void
15650elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15651{
15652 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15653 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
15654 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
15655 bed->s->swap_reloca_out (abfd, rel, loc);
15656}
15657
15658/* Append a REL relocation REL to section S in BFD. */
15659
15660void
15661elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15662{
15663 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15664 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
15665 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 15666 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 15667}
7dba9362
AM
15668
15669/* Define __start, __stop, .startof. or .sizeof. symbol. */
15670
15671struct bfd_link_hash_entry *
15672bfd_elf_define_start_stop (struct bfd_link_info *info,
15673 const char *symbol, asection *sec)
15674{
487b6440 15675 struct elf_link_hash_entry *h;
7dba9362 15676
487b6440 15677 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
0a1b45a2 15678 false, false, true);
e1b5d517 15679 /* NB: Common symbols will be turned into definition later. */
487b6440 15680 if (h != NULL
8ee10e86 15681 && !h->root.ldscript_def
487b6440
AM
15682 && (h->root.type == bfd_link_hash_undefined
15683 || h->root.type == bfd_link_hash_undefweak
e1b5d517
L
15684 || ((h->ref_regular || h->def_dynamic)
15685 && !h->def_regular
15686 && h->root.type != bfd_link_hash_common)))
7dba9362 15687 {
0a1b45a2 15688 bool was_dynamic = h->ref_dynamic || h->def_dynamic;
e1b5d517 15689 h->verinfo.verdef = NULL;
487b6440
AM
15690 h->root.type = bfd_link_hash_defined;
15691 h->root.u.def.section = sec;
15692 h->root.u.def.value = 0;
15693 h->def_regular = 1;
15694 h->def_dynamic = 0;
15695 h->start_stop = 1;
15696 h->u2.start_stop_section = sec;
15697 if (symbol[0] == '.')
15698 {
15699 /* .startof. and .sizeof. symbols are local. */
559192d8
AM
15700 const struct elf_backend_data *bed;
15701 bed = get_elf_backend_data (info->output_bfd);
0a1b45a2 15702 (*bed->elf_backend_hide_symbol) (info, h, true);
487b6440 15703 }
36b8fda5
AM
15704 else
15705 {
15706 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
cae64165
RM
15707 h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15708 | info->start_stop_visibility);
bf3077a6 15709 if (was_dynamic)
36b8fda5
AM
15710 bfd_elf_link_record_dynamic_symbol (info, h);
15711 }
487b6440 15712 return &h->root;
7dba9362 15713 }
487b6440 15714 return NULL;
7dba9362 15715}
5dbc8b37
L
15716
15717/* Find dynamic relocs for H that apply to read-only sections. */
15718
15719asection *
15720_bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15721{
15722 struct elf_dyn_relocs *p;
15723
15724 for (p = h->dyn_relocs; p != NULL; p = p->next)
15725 {
15726 asection *s = p->sec->output_section;
15727
15728 if (s != NULL && (s->flags & SEC_READONLY) != 0)
15729 return p->sec;
15730 }
15731 return NULL;
15732}
d49e5065
L
15733
15734/* Set DF_TEXTREL if we find any dynamic relocs that apply to
15735 read-only sections. */
15736
0a1b45a2 15737bool
d49e5065
L
15738_bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15739{
15740 asection *sec;
15741
15742 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 15743 return true;
d49e5065
L
15744
15745 sec = _bfd_elf_readonly_dynrelocs (h);
15746 if (sec != NULL)
15747 {
15748 struct bfd_link_info *info = (struct bfd_link_info *) inf;
15749
15750 info->flags |= DF_TEXTREL;
15751 /* xgettext:c-format */
15752 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15753 "in read-only section `%pA'\n"),
15754 sec->owner, h->root.root.string, sec);
15755
15756 if (bfd_link_textrel_check (info))
15757 /* xgettext:c-format */
15758 info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15759 "in read-only section `%pA'\n"),
15760 sec->owner, h->root.root.string, sec);
15761
15762 /* Not an error, just cut short the traversal. */
0a1b45a2 15763 return false;
d49e5065 15764 }
0a1b45a2 15765 return true;
d49e5065 15766}
3084d7a2
L
15767
15768/* Add dynamic tags. */
15769
0a1b45a2 15770bool
3084d7a2 15771_bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
0a1b45a2 15772 bool need_dynamic_reloc)
3084d7a2
L
15773{
15774 struct elf_link_hash_table *htab = elf_hash_table (info);
15775
15776 if (htab->dynamic_sections_created)
15777 {
15778 /* Add some entries to the .dynamic section. We fill in the
15779 values later, in finish_dynamic_sections, but we must add
15780 the entries now so that we get the correct size for the
15781 .dynamic section. The DT_DEBUG entry is filled in by the
15782 dynamic linker and used by the debugger. */
15783#define add_dynamic_entry(TAG, VAL) \
15784 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15785
15786 const struct elf_backend_data *bed
15787 = get_elf_backend_data (output_bfd);
15788
15789 if (bfd_link_executable (info))
15790 {
15791 if (!add_dynamic_entry (DT_DEBUG, 0))
0a1b45a2 15792 return false;
3084d7a2
L
15793 }
15794
15795 if (htab->dt_pltgot_required || htab->splt->size != 0)
15796 {
15797 /* DT_PLTGOT is used by prelink even if there is no PLT
15798 relocation. */
15799 if (!add_dynamic_entry (DT_PLTGOT, 0))
0a1b45a2 15800 return false;
3084d7a2
L
15801 }
15802
15803 if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15804 {
15805 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15806 || !add_dynamic_entry (DT_PLTREL,
15807 (bed->rela_plts_and_copies_p
15808 ? DT_RELA : DT_REL))
15809 || !add_dynamic_entry (DT_JMPREL, 0))
0a1b45a2 15810 return false;
3084d7a2
L
15811 }
15812
15813 if (htab->tlsdesc_plt
15814 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15815 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
0a1b45a2 15816 return false;
3084d7a2
L
15817
15818 if (need_dynamic_reloc)
15819 {
15820 if (bed->rela_plts_and_copies_p)
15821 {
15822 if (!add_dynamic_entry (DT_RELA, 0)
15823 || !add_dynamic_entry (DT_RELASZ, 0)
15824 || !add_dynamic_entry (DT_RELAENT,
15825 bed->s->sizeof_rela))
0a1b45a2 15826 return false;
3084d7a2
L
15827 }
15828 else
15829 {
15830 if (!add_dynamic_entry (DT_REL, 0)
15831 || !add_dynamic_entry (DT_RELSZ, 0)
15832 || !add_dynamic_entry (DT_RELENT,
15833 bed->s->sizeof_rel))
0a1b45a2 15834 return false;
3084d7a2
L
15835 }
15836
15837 /* If any dynamic relocs apply to a read-only section,
15838 then we need a DT_TEXTREL entry. */
15839 if ((info->flags & DF_TEXTREL) == 0)
15840 elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15841 info);
15842
15843 if ((info->flags & DF_TEXTREL) != 0)
15844 {
15845 if (htab->ifunc_resolvers)
15846 info->callbacks->einfo
15847 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15848 "may result in a segfault at runtime; recompile with %s\n"),
15849 bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15850
15851 if (!add_dynamic_entry (DT_TEXTREL, 0))
0a1b45a2 15852 return false;
3084d7a2
L
15853 }
15854 }
15855 }
15856#undef add_dynamic_entry
15857
0a1b45a2 15858 return true;
3084d7a2 15859}