]> 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.
b3adc24a 2 Copyright (C) 1995-2020 Free Software Foundation, Inc.
252b5132 3
8fdd7217 4 This file is part of BFD, the Binary File Descriptor library.
252b5132 5
8fdd7217
NC
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
cd123cb7 8 the Free Software Foundation; either version 3 of the License, or
8fdd7217 9 (at your option) any later version.
252b5132 10
8fdd7217
NC
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
252b5132 15
8fdd7217
NC
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
cd123cb7
NC
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
252b5132 20
252b5132 21#include "sysdep.h"
3db64b00 22#include "bfd.h"
252b5132
RH
23#include "bfdlink.h"
24#include "libbfd.h"
25#define ARCH_SIZE 0
26#include "elf-bfd.h"
4ad4eba5 27#include "safe-ctype.h"
ccf2f652 28#include "libiberty.h"
66eb6687 29#include "objalloc.h"
08ce1d72 30#if BFD_SUPPORTS_PLUGINS
7d0b9ebc 31#include "plugin-api.h"
7dc3990e
L
32#include "plugin.h"
33#endif
252b5132 34
4b69ce9b
AM
35#ifdef HAVE_LIMITS_H
36#include <limits.h>
37#endif
38#ifndef CHAR_BIT
39#define CHAR_BIT 8
40#endif
41
28caa186
AM
42/* This struct is used to pass information to routines called via
43 elf_link_hash_traverse which must return failure. */
44
45struct elf_info_failed
46{
47 struct bfd_link_info *info;
28caa186
AM
48 bfd_boolean failed;
49};
50
51/* This structure is used to pass information to
52 _bfd_elf_link_find_version_dependencies. */
53
54struct elf_find_verdep_info
55{
56 /* General link information. */
57 struct bfd_link_info *info;
58 /* The number of dependencies. */
59 unsigned int vers;
60 /* Whether we had a failure. */
61 bfd_boolean failed;
62};
63
64static bfd_boolean _bfd_elf_fix_symbol_flags
65 (struct elf_link_hash_entry *, struct elf_info_failed *);
66
2f0c68f2
CM
67asection *
68_bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
69 unsigned long r_symndx,
70 bfd_boolean discard)
71{
72 if (r_symndx >= cookie->locsymcount
73 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
74 {
75 struct elf_link_hash_entry *h;
76
77 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
78
79 while (h->root.type == bfd_link_hash_indirect
80 || h->root.type == bfd_link_hash_warning)
81 h = (struct elf_link_hash_entry *) h->root.u.i.link;
82
83 if ((h->root.type == bfd_link_hash_defined
84 || h->root.type == bfd_link_hash_defweak)
85 && discarded_section (h->root.u.def.section))
07d6d2b8 86 return h->root.u.def.section;
2f0c68f2
CM
87 else
88 return NULL;
89 }
90 else
91 {
92 /* It's not a relocation against a global symbol,
93 but it could be a relocation against a local
94 symbol for a discarded section. */
95 asection *isec;
96 Elf_Internal_Sym *isym;
97
98 /* Need to: get the symbol; get the section. */
99 isym = &cookie->locsyms[r_symndx];
100 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
101 if (isec != NULL
102 && discard ? discarded_section (isec) : 1)
103 return isec;
104 }
105 return NULL;
106}
107
d98685ac
AM
108/* Define a symbol in a dynamic linkage section. */
109
110struct elf_link_hash_entry *
111_bfd_elf_define_linkage_sym (bfd *abfd,
112 struct bfd_link_info *info,
113 asection *sec,
114 const char *name)
115{
116 struct elf_link_hash_entry *h;
117 struct bfd_link_hash_entry *bh;
ccabcbe5 118 const struct elf_backend_data *bed;
d98685ac
AM
119
120 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
121 if (h != NULL)
122 {
123 /* Zap symbol defined in an as-needed lib that wasn't linked.
124 This is a symptom of a larger problem: Absolute symbols
125 defined in shared libraries can't be overridden, because we
126 lose the link to the bfd which is via the symbol section. */
127 h->root.type = bfd_link_hash_new;
ad32986f 128 bh = &h->root;
d98685ac 129 }
ad32986f
NC
130 else
131 bh = NULL;
d98685ac 132
cf18fda4 133 bed = get_elf_backend_data (abfd);
d98685ac 134 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
cf18fda4 135 sec, 0, NULL, FALSE, bed->collect,
d98685ac
AM
136 &bh))
137 return NULL;
138 h = (struct elf_link_hash_entry *) bh;
ad32986f 139 BFD_ASSERT (h != NULL);
d98685ac 140 h->def_regular = 1;
e28df02b 141 h->non_elf = 0;
12b2843a 142 h->root.linker_def = 1;
d98685ac 143 h->type = STT_OBJECT;
00b7642b
AM
144 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
145 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
d98685ac 146
ccabcbe5 147 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
d98685ac
AM
148 return h;
149}
150
b34976b6 151bfd_boolean
268b6b39 152_bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
153{
154 flagword flags;
aad5d350 155 asection *s;
252b5132 156 struct elf_link_hash_entry *h;
9c5bfbb7 157 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 158 struct elf_link_hash_table *htab = elf_hash_table (info);
252b5132
RH
159
160 /* This function may be called more than once. */
ce558b89 161 if (htab->sgot != NULL)
b34976b6 162 return TRUE;
252b5132 163
e5a52504 164 flags = bed->dynamic_sec_flags;
252b5132 165
14b2f831
AM
166 s = bfd_make_section_anyway_with_flags (abfd,
167 (bed->rela_plts_and_copies_p
168 ? ".rela.got" : ".rel.got"),
169 (bed->dynamic_sec_flags
170 | SEC_READONLY));
6de2ae4a 171 if (s == NULL
fd361982 172 || !bfd_set_section_alignment (s, bed->s->log_file_align))
6de2ae4a
L
173 return FALSE;
174 htab->srelgot = s;
252b5132 175
14b2f831 176 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
64e77c6d 177 if (s == NULL
fd361982 178 || !bfd_set_section_alignment (s, bed->s->log_file_align))
64e77c6d
L
179 return FALSE;
180 htab->sgot = s;
181
252b5132
RH
182 if (bed->want_got_plt)
183 {
14b2f831 184 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
252b5132 185 if (s == NULL
fd361982 186 || !bfd_set_section_alignment (s, bed->s->log_file_align))
b34976b6 187 return FALSE;
6de2ae4a 188 htab->sgotplt = s;
252b5132
RH
189 }
190
64e77c6d
L
191 /* The first bit of the global offset table is the header. */
192 s->size += bed->got_header_size;
193
2517a57f
AM
194 if (bed->want_got_sym)
195 {
196 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
197 (or .got.plt) section. We don't do this in the linker script
198 because we don't want to define the symbol if we are not creating
199 a global offset table. */
6de2ae4a
L
200 h = _bfd_elf_define_linkage_sym (abfd, info, s,
201 "_GLOBAL_OFFSET_TABLE_");
2517a57f 202 elf_hash_table (info)->hgot = h;
d98685ac
AM
203 if (h == NULL)
204 return FALSE;
2517a57f 205 }
252b5132 206
b34976b6 207 return TRUE;
252b5132
RH
208}
209\f
7e9f0867
AM
210/* Create a strtab to hold the dynamic symbol names. */
211static bfd_boolean
212_bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
213{
214 struct elf_link_hash_table *hash_table;
215
216 hash_table = elf_hash_table (info);
217 if (hash_table->dynobj == NULL)
6cd255ca
L
218 {
219 /* We may not set dynobj, an input file holding linker created
220 dynamic sections to abfd, which may be a dynamic object with
221 its own dynamic sections. We need to find a normal input file
222 to hold linker created sections if possible. */
223 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
224 {
225 bfd *ibfd;
57963c05 226 asection *s;
6cd255ca 227 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
6645479e 228 if ((ibfd->flags
57963c05
AM
229 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
230 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
4de5434b 231 && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
57963c05
AM
232 && !((s = ibfd->sections) != NULL
233 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
6cd255ca
L
234 {
235 abfd = ibfd;
236 break;
237 }
238 }
239 hash_table->dynobj = abfd;
240 }
7e9f0867
AM
241
242 if (hash_table->dynstr == NULL)
243 {
244 hash_table->dynstr = _bfd_elf_strtab_init ();
245 if (hash_table->dynstr == NULL)
246 return FALSE;
247 }
248 return TRUE;
249}
250
45d6a902
AM
251/* Create some sections which will be filled in with dynamic linking
252 information. ABFD is an input file which requires dynamic sections
253 to be created. The dynamic sections take up virtual memory space
254 when the final executable is run, so we need to create them before
255 addresses are assigned to the output sections. We work out the
256 actual contents and size of these sections later. */
252b5132 257
b34976b6 258bfd_boolean
268b6b39 259_bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
252b5132 260{
45d6a902 261 flagword flags;
91d6fa6a 262 asection *s;
9c5bfbb7 263 const struct elf_backend_data *bed;
9637f6ef 264 struct elf_link_hash_entry *h;
252b5132 265
0eddce27 266 if (! is_elf_hash_table (info->hash))
45d6a902
AM
267 return FALSE;
268
269 if (elf_hash_table (info)->dynamic_sections_created)
270 return TRUE;
271
7e9f0867
AM
272 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
273 return FALSE;
45d6a902 274
7e9f0867 275 abfd = elf_hash_table (info)->dynobj;
e5a52504
MM
276 bed = get_elf_backend_data (abfd);
277
278 flags = bed->dynamic_sec_flags;
45d6a902
AM
279
280 /* A dynamically linked executable has a .interp section, but a
281 shared library does not. */
9b8b325a 282 if (bfd_link_executable (info) && !info->nointerp)
252b5132 283 {
14b2f831
AM
284 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
285 flags | SEC_READONLY);
3496cb2a 286 if (s == NULL)
45d6a902
AM
287 return FALSE;
288 }
bb0deeff 289
45d6a902
AM
290 /* Create sections to hold version informations. These are removed
291 if they are not needed. */
14b2f831
AM
292 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
293 flags | SEC_READONLY);
45d6a902 294 if (s == NULL
fd361982 295 || !bfd_set_section_alignment (s, bed->s->log_file_align))
45d6a902
AM
296 return FALSE;
297
14b2f831
AM
298 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
299 flags | SEC_READONLY);
45d6a902 300 if (s == NULL
fd361982 301 || !bfd_set_section_alignment (s, 1))
45d6a902
AM
302 return FALSE;
303
14b2f831
AM
304 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
305 flags | SEC_READONLY);
45d6a902 306 if (s == NULL
fd361982 307 || !bfd_set_section_alignment (s, bed->s->log_file_align))
45d6a902
AM
308 return FALSE;
309
14b2f831
AM
310 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
311 flags | SEC_READONLY);
45d6a902 312 if (s == NULL
fd361982 313 || !bfd_set_section_alignment (s, bed->s->log_file_align))
45d6a902 314 return FALSE;
cae1fbbb 315 elf_hash_table (info)->dynsym = s;
45d6a902 316
14b2f831
AM
317 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
318 flags | SEC_READONLY);
3496cb2a 319 if (s == NULL)
45d6a902
AM
320 return FALSE;
321
14b2f831 322 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
45d6a902 323 if (s == NULL
fd361982 324 || !bfd_set_section_alignment (s, bed->s->log_file_align))
45d6a902
AM
325 return FALSE;
326
327 /* The special symbol _DYNAMIC is always set to the start of the
77cfaee6
AM
328 .dynamic section. We could set _DYNAMIC in a linker script, but we
329 only want to define it if we are, in fact, creating a .dynamic
330 section. We don't want to define it if there is no .dynamic
331 section, since on some ELF platforms the start up code examines it
332 to decide how to initialize the process. */
9637f6ef
L
333 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
334 elf_hash_table (info)->hdynamic = h;
335 if (h == NULL)
45d6a902
AM
336 return FALSE;
337
fdc90cb4
JJ
338 if (info->emit_hash)
339 {
14b2f831
AM
340 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
341 flags | SEC_READONLY);
fdc90cb4 342 if (s == NULL
fd361982 343 || !bfd_set_section_alignment (s, bed->s->log_file_align))
fdc90cb4
JJ
344 return FALSE;
345 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
346 }
347
f16a9783 348 if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL)
fdc90cb4 349 {
14b2f831
AM
350 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
351 flags | SEC_READONLY);
fdc90cb4 352 if (s == NULL
fd361982 353 || !bfd_set_section_alignment (s, bed->s->log_file_align))
fdc90cb4
JJ
354 return FALSE;
355 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
356 4 32-bit words followed by variable count of 64-bit words, then
357 variable count of 32-bit words. */
358 if (bed->s->arch_size == 64)
359 elf_section_data (s)->this_hdr.sh_entsize = 0;
360 else
361 elf_section_data (s)->this_hdr.sh_entsize = 4;
362 }
45d6a902
AM
363
364 /* Let the backend create the rest of the sections. This lets the
365 backend set the right flags. The backend will normally create
366 the .got and .plt sections. */
894891db
NC
367 if (bed->elf_backend_create_dynamic_sections == NULL
368 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
45d6a902
AM
369 return FALSE;
370
371 elf_hash_table (info)->dynamic_sections_created = TRUE;
372
373 return TRUE;
374}
375
376/* Create dynamic sections when linking against a dynamic object. */
377
378bfd_boolean
268b6b39 379_bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
45d6a902
AM
380{
381 flagword flags, pltflags;
7325306f 382 struct elf_link_hash_entry *h;
45d6a902 383 asection *s;
9c5bfbb7 384 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6de2ae4a 385 struct elf_link_hash_table *htab = elf_hash_table (info);
45d6a902 386
252b5132
RH
387 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
388 .rel[a].bss sections. */
e5a52504 389 flags = bed->dynamic_sec_flags;
252b5132
RH
390
391 pltflags = flags;
252b5132 392 if (bed->plt_not_loaded)
6df4d94c
MM
393 /* We do not clear SEC_ALLOC here because we still want the OS to
394 allocate space for the section; it's just that there's nothing
395 to read in from the object file. */
5d1634d7 396 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
6df4d94c
MM
397 else
398 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
252b5132
RH
399 if (bed->plt_readonly)
400 pltflags |= SEC_READONLY;
401
14b2f831 402 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
252b5132 403 if (s == NULL
fd361982 404 || !bfd_set_section_alignment (s, bed->plt_alignment))
b34976b6 405 return FALSE;
6de2ae4a 406 htab->splt = s;
252b5132 407
d98685ac
AM
408 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
409 .plt section. */
7325306f
RS
410 if (bed->want_plt_sym)
411 {
412 h = _bfd_elf_define_linkage_sym (abfd, info, s,
413 "_PROCEDURE_LINKAGE_TABLE_");
414 elf_hash_table (info)->hplt = h;
415 if (h == NULL)
416 return FALSE;
417 }
252b5132 418
14b2f831
AM
419 s = bfd_make_section_anyway_with_flags (abfd,
420 (bed->rela_plts_and_copies_p
421 ? ".rela.plt" : ".rel.plt"),
422 flags | SEC_READONLY);
252b5132 423 if (s == NULL
fd361982 424 || !bfd_set_section_alignment (s, bed->s->log_file_align))
b34976b6 425 return FALSE;
6de2ae4a 426 htab->srelplt = s;
252b5132
RH
427
428 if (! _bfd_elf_create_got_section (abfd, info))
b34976b6 429 return FALSE;
252b5132 430
3018b441
RH
431 if (bed->want_dynbss)
432 {
433 /* The .dynbss section is a place to put symbols which are defined
434 by dynamic objects, are referenced by regular objects, and are
435 not functions. We must allocate space for them in the process
436 image and use a R_*_COPY reloc to tell the dynamic linker to
437 initialize them at run time. The linker script puts the .dynbss
438 section into the .bss section of the final image. */
14b2f831 439 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
afbf7e8e 440 SEC_ALLOC | SEC_LINKER_CREATED);
3496cb2a 441 if (s == NULL)
b34976b6 442 return FALSE;
9d19e4fd 443 htab->sdynbss = s;
252b5132 444
5474d94f
AM
445 if (bed->want_dynrelro)
446 {
447 /* Similarly, but for symbols that were originally in read-only
afbf7e8e
AM
448 sections. This section doesn't really need to have contents,
449 but make it like other .data.rel.ro sections. */
5474d94f 450 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
afbf7e8e 451 flags);
5474d94f
AM
452 if (s == NULL)
453 return FALSE;
454 htab->sdynrelro = s;
455 }
456
3018b441 457 /* The .rel[a].bss section holds copy relocs. This section is not
77cfaee6
AM
458 normally needed. We need to create it here, though, so that the
459 linker will map it to an output section. We can't just create it
460 only if we need it, because we will not know whether we need it
461 until we have seen all the input files, and the first time the
462 main linker code calls BFD after examining all the input files
463 (size_dynamic_sections) the input sections have already been
464 mapped to the output sections. If the section turns out not to
465 be needed, we can discard it later. We will never need this
466 section when generating a shared object, since they do not use
467 copy relocs. */
9d19e4fd 468 if (bfd_link_executable (info))
3018b441 469 {
14b2f831
AM
470 s = bfd_make_section_anyway_with_flags (abfd,
471 (bed->rela_plts_and_copies_p
472 ? ".rela.bss" : ".rel.bss"),
473 flags | SEC_READONLY);
3018b441 474 if (s == NULL
fd361982 475 || !bfd_set_section_alignment (s, bed->s->log_file_align))
b34976b6 476 return FALSE;
9d19e4fd 477 htab->srelbss = s;
5474d94f
AM
478
479 if (bed->want_dynrelro)
480 {
481 s = (bfd_make_section_anyway_with_flags
482 (abfd, (bed->rela_plts_and_copies_p
483 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
484 flags | SEC_READONLY));
485 if (s == NULL
fd361982 486 || !bfd_set_section_alignment (s, bed->s->log_file_align))
5474d94f
AM
487 return FALSE;
488 htab->sreldynrelro = s;
489 }
3018b441 490 }
252b5132
RH
491 }
492
b34976b6 493 return TRUE;
252b5132
RH
494}
495\f
252b5132
RH
496/* Record a new dynamic symbol. We record the dynamic symbols as we
497 read the input files, since we need to have a list of all of them
498 before we can determine the final sizes of the output sections.
499 Note that we may actually call this function even though we are not
500 going to output any dynamic symbols; in some cases we know that a
501 symbol should be in the dynamic symbol table, but only if there is
502 one. */
503
b34976b6 504bfd_boolean
c152c796
AM
505bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
506 struct elf_link_hash_entry *h)
252b5132
RH
507{
508 if (h->dynindx == -1)
509 {
2b0f7ef9 510 struct elf_strtab_hash *dynstr;
68b6ddd0 511 char *p;
252b5132 512 const char *name;
ef53be89 513 size_t indx;
252b5132 514
a896df97
AM
515 if (h->root.type == bfd_link_hash_defined
516 || h->root.type == bfd_link_hash_defweak)
517 {
518 /* An IR symbol should not be made dynamic. */
519 if (h->root.u.def.section != NULL
520 && h->root.u.def.section->owner != NULL
521 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)
522 return TRUE;
523 }
524
7a13edea
NC
525 /* XXX: The ABI draft says the linker must turn hidden and
526 internal symbols into STB_LOCAL symbols when producing the
527 DSO. However, if ld.so honors st_other in the dynamic table,
528 this would not be necessary. */
529 switch (ELF_ST_VISIBILITY (h->other))
530 {
531 case STV_INTERNAL:
532 case STV_HIDDEN:
9d6eee78
L
533 if (h->root.type != bfd_link_hash_undefined
534 && h->root.type != bfd_link_hash_undefweak)
38048eb9 535 {
f5385ebf 536 h->forced_local = 1;
67687978
PB
537 if (!elf_hash_table (info)->is_relocatable_executable)
538 return TRUE;
7a13edea 539 }
0444bdd4 540
7a13edea
NC
541 default:
542 break;
543 }
544
252b5132
RH
545 h->dynindx = elf_hash_table (info)->dynsymcount;
546 ++elf_hash_table (info)->dynsymcount;
547
548 dynstr = elf_hash_table (info)->dynstr;
549 if (dynstr == NULL)
550 {
551 /* Create a strtab to hold the dynamic symbol names. */
2b0f7ef9 552 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
252b5132 553 if (dynstr == NULL)
b34976b6 554 return FALSE;
252b5132
RH
555 }
556
557 /* We don't put any version information in the dynamic string
aad5d350 558 table. */
252b5132
RH
559 name = h->root.root.string;
560 p = strchr (name, ELF_VER_CHR);
68b6ddd0
AM
561 if (p != NULL)
562 /* We know that the p points into writable memory. In fact,
563 there are only a few symbols that have read-only names, being
564 those like _GLOBAL_OFFSET_TABLE_ that are created specially
565 by the backends. Most symbols will have names pointing into
566 an ELF string table read from a file, or to objalloc memory. */
567 *p = 0;
568
569 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
570
571 if (p != NULL)
572 *p = ELF_VER_CHR;
252b5132 573
ef53be89 574 if (indx == (size_t) -1)
b34976b6 575 return FALSE;
252b5132
RH
576 h->dynstr_index = indx;
577 }
578
b34976b6 579 return TRUE;
252b5132 580}
45d6a902 581\f
55255dae
L
582/* Mark a symbol dynamic. */
583
28caa186 584static void
55255dae 585bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
40b36307
L
586 struct elf_link_hash_entry *h,
587 Elf_Internal_Sym *sym)
55255dae 588{
40b36307 589 struct bfd_elf_dynamic_list *d = info->dynamic_list;
55255dae 590
40b36307 591 /* It may be called more than once on the same H. */
0e1862bb 592 if(h->dynamic || bfd_link_relocatable (info))
55255dae
L
593 return;
594
40b36307
L
595 if ((info->dynamic_data
596 && (h->type == STT_OBJECT
b8871f35 597 || h->type == STT_COMMON
40b36307 598 || (sym != NULL
b8871f35
L
599 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
600 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
a0c8462f 601 || (d != NULL
73ec947d 602 && h->non_elf
40b36307 603 && (*d->match) (&d->head, NULL, h->root.root.string)))
416c34d6
L
604 {
605 h->dynamic = 1;
606 /* NB: If a symbol is made dynamic by --dynamic-list, it has
607 non-IR reference. */
608 h->root.non_ir_ref_dynamic = 1;
609 }
55255dae
L
610}
611
45d6a902
AM
612/* Record an assignment to a symbol made by a linker script. We need
613 this in case some dynamic object refers to this symbol. */
614
615bfd_boolean
fe21a8fc
L
616bfd_elf_record_link_assignment (bfd *output_bfd,
617 struct bfd_link_info *info,
268b6b39 618 const char *name,
fe21a8fc
L
619 bfd_boolean provide,
620 bfd_boolean hidden)
45d6a902 621{
00cbee0a 622 struct elf_link_hash_entry *h, *hv;
4ea42fb7 623 struct elf_link_hash_table *htab;
00cbee0a 624 const struct elf_backend_data *bed;
45d6a902 625
0eddce27 626 if (!is_elf_hash_table (info->hash))
45d6a902
AM
627 return TRUE;
628
4ea42fb7
AM
629 htab = elf_hash_table (info);
630 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
45d6a902 631 if (h == NULL)
4ea42fb7 632 return provide;
45d6a902 633
8e2a4f11
AM
634 if (h->root.type == bfd_link_hash_warning)
635 h = (struct elf_link_hash_entry *) h->root.u.i.link;
636
0f550b3d
L
637 if (h->versioned == unknown)
638 {
639 /* Set versioned if symbol version is unknown. */
640 char *version = strrchr (name, ELF_VER_CHR);
641 if (version)
642 {
643 if (version > name && version[-1] != ELF_VER_CHR)
644 h->versioned = versioned_hidden;
645 else
646 h->versioned = versioned;
647 }
648 }
649
73ec947d
AM
650 /* Symbols defined in a linker script but not referenced anywhere
651 else will have non_elf set. */
652 if (h->non_elf)
653 {
654 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
655 h->non_elf = 0;
656 }
657
00cbee0a 658 switch (h->root.type)
77cfaee6 659 {
00cbee0a
L
660 case bfd_link_hash_defined:
661 case bfd_link_hash_defweak:
662 case bfd_link_hash_common:
663 break;
664 case bfd_link_hash_undefweak:
665 case bfd_link_hash_undefined:
666 /* Since we're defining the symbol, don't let it seem to have not
667 been defined. record_dynamic_symbol and size_dynamic_sections
668 may depend on this. */
4ea42fb7 669 h->root.type = bfd_link_hash_new;
77cfaee6
AM
670 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
671 bfd_link_repair_undef_list (&htab->root);
00cbee0a
L
672 break;
673 case bfd_link_hash_new:
00cbee0a
L
674 break;
675 case bfd_link_hash_indirect:
676 /* We had a versioned symbol in a dynamic library. We make the
a0c8462f 677 the versioned symbol point to this one. */
00cbee0a
L
678 bed = get_elf_backend_data (output_bfd);
679 hv = h;
680 while (hv->root.type == bfd_link_hash_indirect
681 || hv->root.type == bfd_link_hash_warning)
682 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
683 /* We don't need to update h->root.u since linker will set them
684 later. */
685 h->root.type = bfd_link_hash_undefined;
686 hv->root.type = bfd_link_hash_indirect;
687 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
688 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
689 break;
8e2a4f11
AM
690 default:
691 BFD_FAIL ();
c2596ca5 692 return FALSE;
55255dae 693 }
45d6a902
AM
694
695 /* If this symbol is being provided by the linker script, and it is
696 currently defined by a dynamic object, but not by a regular
697 object, then mark it as undefined so that the generic linker will
698 force the correct value. */
699 if (provide
f5385ebf
AM
700 && h->def_dynamic
701 && !h->def_regular)
45d6a902
AM
702 h->root.type = bfd_link_hash_undefined;
703
48e30f52
L
704 /* If this symbol is currently defined by a dynamic object, but not
705 by a regular object, then clear out any version information because
706 the symbol will not be associated with the dynamic object any
707 more. */
708 if (h->def_dynamic && !h->def_regular)
b531344c
MR
709 h->verinfo.verdef = NULL;
710
711 /* Make sure this symbol is not garbage collected. */
712 h->mark = 1;
45d6a902 713
f5385ebf 714 h->def_regular = 1;
45d6a902 715
eb8476a6 716 if (hidden)
fe21a8fc 717 {
91d6fa6a 718 bed = get_elf_backend_data (output_bfd);
b8297068
AM
719 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
720 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
fe21a8fc
L
721 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
722 }
723
6fa3860b
PB
724 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
725 and executables. */
0e1862bb 726 if (!bfd_link_relocatable (info)
6fa3860b
PB
727 && h->dynindx != -1
728 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
729 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
730 h->forced_local = 1;
731
f5385ebf
AM
732 if ((h->def_dynamic
733 || h->ref_dynamic
6b3b0ab8
L
734 || bfd_link_dll (info)
735 || elf_hash_table (info)->is_relocatable_executable)
34a87bb0 736 && !h->forced_local
45d6a902
AM
737 && h->dynindx == -1)
738 {
c152c796 739 if (! bfd_elf_link_record_dynamic_symbol (info, h))
45d6a902
AM
740 return FALSE;
741
742 /* If this is a weak defined symbol, and we know a corresponding
743 real symbol from the same dynamic object, make sure the real
744 symbol is also made into a dynamic symbol. */
60d67dc8 745 if (h->is_weakalias)
45d6a902 746 {
60d67dc8
AM
747 struct elf_link_hash_entry *def = weakdef (h);
748
749 if (def->dynindx == -1
750 && !bfd_elf_link_record_dynamic_symbol (info, def))
45d6a902
AM
751 return FALSE;
752 }
753 }
754
755 return TRUE;
756}
42751cf3 757
8c58d23b
AM
758/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
759 success, and 2 on a failure caused by attempting to record a symbol
760 in a discarded section, eg. a discarded link-once section symbol. */
761
762int
c152c796
AM
763bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
764 bfd *input_bfd,
765 long input_indx)
8c58d23b 766{
986f0783 767 size_t amt;
8c58d23b
AM
768 struct elf_link_local_dynamic_entry *entry;
769 struct elf_link_hash_table *eht;
770 struct elf_strtab_hash *dynstr;
ef53be89 771 size_t dynstr_index;
8c58d23b
AM
772 char *name;
773 Elf_External_Sym_Shndx eshndx;
774 char esym[sizeof (Elf64_External_Sym)];
775
0eddce27 776 if (! is_elf_hash_table (info->hash))
8c58d23b
AM
777 return 0;
778
779 /* See if the entry exists already. */
780 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
781 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
782 return 1;
783
784 amt = sizeof (*entry);
a50b1753 785 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
8c58d23b
AM
786 if (entry == NULL)
787 return 0;
788
789 /* Go find the symbol, so that we can find it's name. */
790 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
268b6b39 791 1, input_indx, &entry->isym, esym, &eshndx))
8c58d23b
AM
792 {
793 bfd_release (input_bfd, entry);
794 return 0;
795 }
796
797 if (entry->isym.st_shndx != SHN_UNDEF
4fbb74a6 798 && entry->isym.st_shndx < SHN_LORESERVE)
8c58d23b
AM
799 {
800 asection *s;
801
802 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
803 if (s == NULL || bfd_is_abs_section (s->output_section))
804 {
805 /* We can still bfd_release here as nothing has done another
806 bfd_alloc. We can't do this later in this function. */
807 bfd_release (input_bfd, entry);
808 return 2;
809 }
810 }
811
812 name = (bfd_elf_string_from_elf_section
813 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
814 entry->isym.st_name));
815
816 dynstr = elf_hash_table (info)->dynstr;
817 if (dynstr == NULL)
818 {
819 /* Create a strtab to hold the dynamic symbol names. */
820 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
821 if (dynstr == NULL)
822 return 0;
823 }
824
b34976b6 825 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
ef53be89 826 if (dynstr_index == (size_t) -1)
8c58d23b
AM
827 return 0;
828 entry->isym.st_name = dynstr_index;
829
830 eht = elf_hash_table (info);
831
832 entry->next = eht->dynlocal;
833 eht->dynlocal = entry;
834 entry->input_bfd = input_bfd;
835 entry->input_indx = input_indx;
836 eht->dynsymcount++;
837
838 /* Whatever binding the symbol had before, it's now local. */
839 entry->isym.st_info
840 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
841
842 /* The dynindx will be set at the end of size_dynamic_sections. */
843
844 return 1;
845}
846
30b30c21 847/* Return the dynindex of a local dynamic symbol. */
42751cf3 848
30b30c21 849long
268b6b39
AM
850_bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
851 bfd *input_bfd,
852 long input_indx)
30b30c21
RH
853{
854 struct elf_link_local_dynamic_entry *e;
855
856 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
857 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
858 return e->dynindx;
859 return -1;
860}
861
862/* This function is used to renumber the dynamic symbols, if some of
863 them are removed because they are marked as local. This is called
864 via elf_link_hash_traverse. */
865
b34976b6 866static bfd_boolean
268b6b39
AM
867elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
868 void *data)
42751cf3 869{
a50b1753 870 size_t *count = (size_t *) data;
30b30c21 871
6fa3860b
PB
872 if (h->forced_local)
873 return TRUE;
874
875 if (h->dynindx != -1)
876 h->dynindx = ++(*count);
877
878 return TRUE;
879}
880
881
882/* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
883 STB_LOCAL binding. */
884
885static bfd_boolean
886elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
887 void *data)
888{
a50b1753 889 size_t *count = (size_t *) data;
6fa3860b 890
6fa3860b
PB
891 if (!h->forced_local)
892 return TRUE;
893
42751cf3 894 if (h->dynindx != -1)
30b30c21
RH
895 h->dynindx = ++(*count);
896
b34976b6 897 return TRUE;
42751cf3 898}
30b30c21 899
aee6f5b4
AO
900/* Return true if the dynamic symbol for a given section should be
901 omitted when creating a shared library. */
902bfd_boolean
d00dd7dc
AM
903_bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
904 struct bfd_link_info *info,
905 asection *p)
aee6f5b4 906{
74541ad4 907 struct elf_link_hash_table *htab;
ca55926c 908 asection *ip;
74541ad4 909
aee6f5b4
AO
910 switch (elf_section_data (p)->this_hdr.sh_type)
911 {
912 case SHT_PROGBITS:
913 case SHT_NOBITS:
914 /* If sh_type is yet undecided, assume it could be
915 SHT_PROGBITS/SHT_NOBITS. */
916 case SHT_NULL:
74541ad4 917 htab = elf_hash_table (info);
74541ad4
AM
918 if (htab->text_index_section != NULL)
919 return p != htab->text_index_section && p != htab->data_index_section;
920
ca55926c 921 return (htab->dynobj != NULL
3d4d4302 922 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
ca55926c 923 && ip->output_section == p);
aee6f5b4
AO
924
925 /* There shouldn't be section relative relocations
926 against any other section. */
927 default:
928 return TRUE;
929 }
930}
931
d00dd7dc
AM
932bfd_boolean
933_bfd_elf_omit_section_dynsym_all
934 (bfd *output_bfd ATTRIBUTE_UNUSED,
935 struct bfd_link_info *info ATTRIBUTE_UNUSED,
936 asection *p ATTRIBUTE_UNUSED)
937{
938 return TRUE;
939}
940
062e2358 941/* Assign dynsym indices. In a shared library we generate a section
6fa3860b
PB
942 symbol for each output section, which come first. Next come symbols
943 which have been forced to local binding. Then all of the back-end
944 allocated local dynamic syms, followed by the rest of the global
63f452a8
AM
945 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
946 (This prevents the early call before elf_backend_init_index_section
947 and strip_excluded_output_sections setting dynindx for sections
948 that are stripped.) */
30b30c21 949
554220db
AM
950static unsigned long
951_bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
952 struct bfd_link_info *info,
953 unsigned long *section_sym_count)
30b30c21
RH
954{
955 unsigned long dynsymcount = 0;
63f452a8 956 bfd_boolean do_sec = section_sym_count != NULL;
30b30c21 957
0e1862bb
L
958 if (bfd_link_pic (info)
959 || elf_hash_table (info)->is_relocatable_executable)
30b30c21 960 {
aee6f5b4 961 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
30b30c21
RH
962 asection *p;
963 for (p = output_bfd->sections; p ; p = p->next)
8c37241b 964 if ((p->flags & SEC_EXCLUDE) == 0
aee6f5b4 965 && (p->flags & SEC_ALLOC) != 0
7f923b7f 966 && elf_hash_table (info)->dynamic_relocs
aee6f5b4 967 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
63f452a8
AM
968 {
969 ++dynsymcount;
970 if (do_sec)
971 elf_section_data (p)->dynindx = dynsymcount;
972 }
973 else if (do_sec)
74541ad4 974 elf_section_data (p)->dynindx = 0;
30b30c21 975 }
63f452a8
AM
976 if (do_sec)
977 *section_sym_count = dynsymcount;
30b30c21 978
6fa3860b
PB
979 elf_link_hash_traverse (elf_hash_table (info),
980 elf_link_renumber_local_hash_table_dynsyms,
981 &dynsymcount);
982
30b30c21
RH
983 if (elf_hash_table (info)->dynlocal)
984 {
985 struct elf_link_local_dynamic_entry *p;
986 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
987 p->dynindx = ++dynsymcount;
988 }
90ac2420 989 elf_hash_table (info)->local_dynsymcount = dynsymcount;
30b30c21
RH
990
991 elf_link_hash_traverse (elf_hash_table (info),
992 elf_link_renumber_hash_table_dynsyms,
993 &dynsymcount);
994
d5486c43
L
995 /* There is an unused NULL entry at the head of the table which we
996 must account for in our count even if the table is empty since it
997 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
998 .dynamic section. */
999 dynsymcount++;
30b30c21 1000
ccabcbe5
AM
1001 elf_hash_table (info)->dynsymcount = dynsymcount;
1002 return dynsymcount;
30b30c21 1003}
252b5132 1004
54ac0771
L
1005/* Merge st_other field. */
1006
1007static void
1008elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
b8417128 1009 const Elf_Internal_Sym *isym, asection *sec,
cd3416da 1010 bfd_boolean definition, bfd_boolean dynamic)
54ac0771
L
1011{
1012 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1013
1014 /* If st_other has a processor-specific meaning, specific
cd3416da 1015 code might be needed here. */
54ac0771
L
1016 if (bed->elf_backend_merge_symbol_attribute)
1017 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
1018 dynamic);
1019
cd3416da 1020 if (!dynamic)
54ac0771 1021 {
cd3416da
AM
1022 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
1023 unsigned hvis = ELF_ST_VISIBILITY (h->other);
54ac0771 1024
cd3416da
AM
1025 /* Keep the most constraining visibility. Leave the remainder
1026 of the st_other field to elf_backend_merge_symbol_attribute. */
1027 if (symvis - 1 < hvis - 1)
1028 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
54ac0771 1029 }
b8417128
AM
1030 else if (definition
1031 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
1032 && (sec->flags & SEC_READONLY) == 0)
6cabe1ea 1033 h->protected_def = 1;
54ac0771
L
1034}
1035
4f3fedcf
AM
1036/* This function is called when we want to merge a new symbol with an
1037 existing symbol. It handles the various cases which arise when we
1038 find a definition in a dynamic object, or when there is already a
1039 definition in a dynamic object. The new symbol is described by
1040 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1041 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1042 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1043 of an old common symbol. We set OVERRIDE if the old symbol is
1044 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1045 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1046 to change. By OK to change, we mean that we shouldn't warn if the
1047 type or size does change. */
45d6a902 1048
8a56bd02 1049static bfd_boolean
268b6b39
AM
1050_bfd_elf_merge_symbol (bfd *abfd,
1051 struct bfd_link_info *info,
1052 const char *name,
1053 Elf_Internal_Sym *sym,
1054 asection **psec,
1055 bfd_vma *pvalue,
4f3fedcf
AM
1056 struct elf_link_hash_entry **sym_hash,
1057 bfd **poldbfd,
37a9e49a 1058 bfd_boolean *pold_weak,
af44c138 1059 unsigned int *pold_alignment,
268b6b39 1060 bfd_boolean *skip,
7ba11550 1061 bfd **override,
268b6b39 1062 bfd_boolean *type_change_ok,
6e33951e
L
1063 bfd_boolean *size_change_ok,
1064 bfd_boolean *matched)
252b5132 1065{
7479dfd4 1066 asection *sec, *oldsec;
45d6a902 1067 struct elf_link_hash_entry *h;
90c984fc 1068 struct elf_link_hash_entry *hi;
45d6a902
AM
1069 struct elf_link_hash_entry *flip;
1070 int bind;
1071 bfd *oldbfd;
1072 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
0a36a439 1073 bfd_boolean newweak, oldweak, newfunc, oldfunc;
a4d8e49b 1074 const struct elf_backend_data *bed;
6e33951e 1075 char *new_version;
93f4de39 1076 bfd_boolean default_sym = *matched;
45d6a902
AM
1077
1078 *skip = FALSE;
7ba11550 1079 *override = NULL;
45d6a902
AM
1080
1081 sec = *psec;
1082 bind = ELF_ST_BIND (sym->st_info);
1083
1084 if (! bfd_is_und_section (sec))
1085 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1086 else
1087 h = ((struct elf_link_hash_entry *)
1088 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1089 if (h == NULL)
1090 return FALSE;
1091 *sym_hash = h;
252b5132 1092
88ba32a0
L
1093 bed = get_elf_backend_data (abfd);
1094
6e33951e 1095 /* NEW_VERSION is the symbol version of the new symbol. */
422f1182 1096 if (h->versioned != unversioned)
6e33951e 1097 {
422f1182
L
1098 /* Symbol version is unknown or versioned. */
1099 new_version = strrchr (name, ELF_VER_CHR);
1100 if (new_version)
1101 {
1102 if (h->versioned == unknown)
1103 {
1104 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1105 h->versioned = versioned_hidden;
1106 else
1107 h->versioned = versioned;
1108 }
1109 new_version += 1;
1110 if (new_version[0] == '\0')
1111 new_version = NULL;
1112 }
1113 else
1114 h->versioned = unversioned;
6e33951e 1115 }
422f1182
L
1116 else
1117 new_version = NULL;
6e33951e 1118
90c984fc
L
1119 /* For merging, we only care about real symbols. But we need to make
1120 sure that indirect symbol dynamic flags are updated. */
1121 hi = h;
45d6a902
AM
1122 while (h->root.type == bfd_link_hash_indirect
1123 || h->root.type == bfd_link_hash_warning)
1124 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1125
6e33951e
L
1126 if (!*matched)
1127 {
1128 if (hi == h || h->root.type == bfd_link_hash_new)
1129 *matched = TRUE;
1130 else
1131 {
ae7683d2 1132 /* OLD_HIDDEN is true if the existing symbol is only visible
6e33951e 1133 to the symbol with the same symbol version. NEW_HIDDEN is
ae7683d2 1134 true if the new symbol is only visible to the symbol with
6e33951e 1135 the same symbol version. */
422f1182
L
1136 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1137 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
6e33951e
L
1138 if (!old_hidden && !new_hidden)
1139 /* The new symbol matches the existing symbol if both
1140 aren't hidden. */
1141 *matched = TRUE;
1142 else
1143 {
1144 /* OLD_VERSION is the symbol version of the existing
1145 symbol. */
422f1182
L
1146 char *old_version;
1147
1148 if (h->versioned >= versioned)
1149 old_version = strrchr (h->root.root.string,
1150 ELF_VER_CHR) + 1;
1151 else
1152 old_version = NULL;
6e33951e
L
1153
1154 /* The new symbol matches the existing symbol if they
1155 have the same symbol version. */
1156 *matched = (old_version == new_version
1157 || (old_version != NULL
1158 && new_version != NULL
1159 && strcmp (old_version, new_version) == 0));
1160 }
1161 }
1162 }
1163
934bce08
AM
1164 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1165 existing symbol. */
1166
1167 oldbfd = NULL;
1168 oldsec = NULL;
1169 switch (h->root.type)
1170 {
1171 default:
1172 break;
1173
1174 case bfd_link_hash_undefined:
1175 case bfd_link_hash_undefweak:
1176 oldbfd = h->root.u.undef.abfd;
1177 break;
1178
1179 case bfd_link_hash_defined:
1180 case bfd_link_hash_defweak:
1181 oldbfd = h->root.u.def.section->owner;
1182 oldsec = h->root.u.def.section;
1183 break;
1184
1185 case bfd_link_hash_common:
1186 oldbfd = h->root.u.c.p->section->owner;
1187 oldsec = h->root.u.c.p->section;
1188 if (pold_alignment)
1189 *pold_alignment = h->root.u.c.p->alignment_power;
1190 break;
1191 }
1192 if (poldbfd && *poldbfd == NULL)
1193 *poldbfd = oldbfd;
1194
1195 /* Differentiate strong and weak symbols. */
1196 newweak = bind == STB_WEAK;
1197 oldweak = (h->root.type == bfd_link_hash_defweak
1198 || h->root.type == bfd_link_hash_undefweak);
1199 if (pold_weak)
1200 *pold_weak = oldweak;
1201
40b36307 1202 /* We have to check it for every instance since the first few may be
ee659f1f 1203 references and not all compilers emit symbol type for undefined
40b36307
L
1204 symbols. */
1205 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1206
ee659f1f
AM
1207 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1208 respectively, is from a dynamic object. */
1209
1210 newdyn = (abfd->flags & DYNAMIC) != 0;
1211
1212 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1213 syms and defined syms in dynamic libraries respectively.
1214 ref_dynamic on the other hand can be set for a symbol defined in
1215 a dynamic library, and def_dynamic may not be set; When the
1216 definition in a dynamic lib is overridden by a definition in the
1217 executable use of the symbol in the dynamic lib becomes a
1218 reference to the executable symbol. */
1219 if (newdyn)
1220 {
1221 if (bfd_is_und_section (sec))
1222 {
1223 if (bind != STB_WEAK)
1224 {
1225 h->ref_dynamic_nonweak = 1;
1226 hi->ref_dynamic_nonweak = 1;
1227 }
1228 }
1229 else
1230 {
6e33951e
L
1231 /* Update the existing symbol only if they match. */
1232 if (*matched)
1233 h->dynamic_def = 1;
ee659f1f
AM
1234 hi->dynamic_def = 1;
1235 }
1236 }
1237
45d6a902
AM
1238 /* If we just created the symbol, mark it as being an ELF symbol.
1239 Other than that, there is nothing to do--there is no merge issue
1240 with a newly defined symbol--so we just return. */
1241
1242 if (h->root.type == bfd_link_hash_new)
252b5132 1243 {
f5385ebf 1244 h->non_elf = 0;
45d6a902
AM
1245 return TRUE;
1246 }
252b5132 1247
45d6a902
AM
1248 /* In cases involving weak versioned symbols, we may wind up trying
1249 to merge a symbol with itself. Catch that here, to avoid the
1250 confusion that results if we try to override a symbol with
1251 itself. The additional tests catch cases like
1252 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1253 dynamic object, which we do want to handle here. */
1254 if (abfd == oldbfd
895fa45f 1255 && (newweak || oldweak)
45d6a902 1256 && ((abfd->flags & DYNAMIC) == 0
f5385ebf 1257 || !h->def_regular))
45d6a902
AM
1258 return TRUE;
1259
707bba77 1260 olddyn = FALSE;
45d6a902
AM
1261 if (oldbfd != NULL)
1262 olddyn = (oldbfd->flags & DYNAMIC) != 0;
707bba77 1263 else if (oldsec != NULL)
45d6a902 1264 {
707bba77 1265 /* This handles the special SHN_MIPS_{TEXT,DATA} section
45d6a902 1266 indices used by MIPS ELF. */
707bba77 1267 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
45d6a902 1268 }
252b5132 1269
1a3b5c34
AM
1270 /* Handle a case where plugin_notice won't be called and thus won't
1271 set the non_ir_ref flags on the first pass over symbols. */
1272 if (oldbfd != NULL
1273 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1274 && newdyn != olddyn)
1275 {
1276 h->root.non_ir_ref_dynamic = TRUE;
1277 hi->root.non_ir_ref_dynamic = TRUE;
1278 }
1279
45d6a902
AM
1280 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1281 respectively, appear to be a definition rather than reference. */
1282
707bba77 1283 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
45d6a902 1284
707bba77
AM
1285 olddef = (h->root.type != bfd_link_hash_undefined
1286 && h->root.type != bfd_link_hash_undefweak
202ac193 1287 && h->root.type != bfd_link_hash_common);
45d6a902 1288
0a36a439
L
1289 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1290 respectively, appear to be a function. */
1291
1292 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1293 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1294
1295 oldfunc = (h->type != STT_NOTYPE
1296 && bed->is_function_type (h->type));
1297
c5d37467 1298 if (!(newfunc && oldfunc)
5b677558
AM
1299 && ELF_ST_TYPE (sym->st_info) != h->type
1300 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1301 && h->type != STT_NOTYPE
c5d37467
AM
1302 && (newdef || bfd_is_com_section (sec))
1303 && (olddef || h->root.type == bfd_link_hash_common))
580a2b6e 1304 {
c5d37467
AM
1305 /* If creating a default indirect symbol ("foo" or "foo@") from
1306 a dynamic versioned definition ("foo@@") skip doing so if
1307 there is an existing regular definition with a different
1308 type. We don't want, for example, a "time" variable in the
1309 executable overriding a "time" function in a shared library. */
1310 if (newdyn
1311 && !olddyn)
1312 {
1313 *skip = TRUE;
1314 return TRUE;
1315 }
1316
1317 /* When adding a symbol from a regular object file after we have
1318 created indirect symbols, undo the indirection and any
1319 dynamic state. */
1320 if (hi != h
1321 && !newdyn
1322 && olddyn)
1323 {
1324 h = hi;
1325 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1326 h->forced_local = 0;
1327 h->ref_dynamic = 0;
1328 h->def_dynamic = 0;
1329 h->dynamic_def = 0;
1330 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1331 {
1332 h->root.type = bfd_link_hash_undefined;
1333 h->root.u.undef.abfd = abfd;
1334 }
1335 else
1336 {
1337 h->root.type = bfd_link_hash_new;
1338 h->root.u.undef.abfd = NULL;
1339 }
1340 return TRUE;
1341 }
580a2b6e
L
1342 }
1343
4c34aff8
AM
1344 /* Check TLS symbols. We don't check undefined symbols introduced
1345 by "ld -u" which have no type (and oldbfd NULL), and we don't
1346 check symbols from plugins because they also have no type. */
1347 if (oldbfd != NULL
1348 && (oldbfd->flags & BFD_PLUGIN) == 0
1349 && (abfd->flags & BFD_PLUGIN) == 0
1350 && ELF_ST_TYPE (sym->st_info) != h->type
1351 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
7479dfd4
L
1352 {
1353 bfd *ntbfd, *tbfd;
1354 bfd_boolean ntdef, tdef;
1355 asection *ntsec, *tsec;
1356
1357 if (h->type == STT_TLS)
1358 {
3b36f7e6 1359 ntbfd = abfd;
7479dfd4
L
1360 ntsec = sec;
1361 ntdef = newdef;
1362 tbfd = oldbfd;
1363 tsec = oldsec;
1364 tdef = olddef;
1365 }
1366 else
1367 {
1368 ntbfd = oldbfd;
1369 ntsec = oldsec;
1370 ntdef = olddef;
1371 tbfd = abfd;
1372 tsec = sec;
1373 tdef = newdef;
1374 }
1375
1376 if (tdef && ntdef)
4eca0228 1377 _bfd_error_handler
695344c0 1378 /* xgettext:c-format */
871b3ab2
AM
1379 (_("%s: TLS definition in %pB section %pA "
1380 "mismatches non-TLS definition in %pB section %pA"),
c08bb8dd 1381 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
7479dfd4 1382 else if (!tdef && !ntdef)
4eca0228 1383 _bfd_error_handler
695344c0 1384 /* xgettext:c-format */
871b3ab2
AM
1385 (_("%s: TLS reference in %pB "
1386 "mismatches non-TLS reference in %pB"),
c08bb8dd 1387 h->root.root.string, tbfd, ntbfd);
7479dfd4 1388 else if (tdef)
4eca0228 1389 _bfd_error_handler
695344c0 1390 /* xgettext:c-format */
871b3ab2
AM
1391 (_("%s: TLS definition in %pB section %pA "
1392 "mismatches non-TLS reference in %pB"),
c08bb8dd 1393 h->root.root.string, tbfd, tsec, ntbfd);
7479dfd4 1394 else
4eca0228 1395 _bfd_error_handler
695344c0 1396 /* xgettext:c-format */
871b3ab2
AM
1397 (_("%s: TLS reference in %pB "
1398 "mismatches non-TLS definition in %pB section %pA"),
c08bb8dd 1399 h->root.root.string, tbfd, ntbfd, ntsec);
7479dfd4
L
1400
1401 bfd_set_error (bfd_error_bad_value);
1402 return FALSE;
1403 }
1404
45d6a902
AM
1405 /* If the old symbol has non-default visibility, we ignore the new
1406 definition from a dynamic object. */
1407 if (newdyn
9c7a29a3 1408 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
45d6a902
AM
1409 && !bfd_is_und_section (sec))
1410 {
1411 *skip = TRUE;
1412 /* Make sure this symbol is dynamic. */
f5385ebf 1413 h->ref_dynamic = 1;
90c984fc 1414 hi->ref_dynamic = 1;
45d6a902
AM
1415 /* A protected symbol has external availability. Make sure it is
1416 recorded as dynamic.
1417
1418 FIXME: Should we check type and size for protected symbol? */
1419 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
c152c796 1420 return bfd_elf_link_record_dynamic_symbol (info, h);
45d6a902
AM
1421 else
1422 return TRUE;
1423 }
1424 else if (!newdyn
9c7a29a3 1425 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
f5385ebf 1426 && h->def_dynamic)
45d6a902
AM
1427 {
1428 /* If the new symbol with non-default visibility comes from a
1429 relocatable file and the old definition comes from a dynamic
1430 object, we remove the old definition. */
6c9b78e6 1431 if (hi->root.type == bfd_link_hash_indirect)
d2dee3b2
L
1432 {
1433 /* Handle the case where the old dynamic definition is
1434 default versioned. We need to copy the symbol info from
1435 the symbol with default version to the normal one if it
1436 was referenced before. */
1437 if (h->ref_regular)
1438 {
6c9b78e6 1439 hi->root.type = h->root.type;
d2dee3b2 1440 h->root.type = bfd_link_hash_indirect;
6c9b78e6 1441 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
aed81c4e 1442
6c9b78e6 1443 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
aed81c4e 1444 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
d2dee3b2 1445 {
aed81c4e
MR
1446 /* If the new symbol is hidden or internal, completely undo
1447 any dynamic link state. */
1448 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1449 h->forced_local = 0;
1450 h->ref_dynamic = 0;
d2dee3b2
L
1451 }
1452 else
aed81c4e
MR
1453 h->ref_dynamic = 1;
1454
1455 h->def_dynamic = 0;
aed81c4e
MR
1456 /* FIXME: Should we check type and size for protected symbol? */
1457 h->size = 0;
1458 h->type = 0;
1459
6c9b78e6 1460 h = hi;
d2dee3b2
L
1461 }
1462 else
6c9b78e6 1463 h = hi;
d2dee3b2 1464 }
1de1a317 1465
f5eda473
AM
1466 /* If the old symbol was undefined before, then it will still be
1467 on the undefs list. If the new symbol is undefined or
1468 common, we can't make it bfd_link_hash_new here, because new
1469 undefined or common symbols will be added to the undefs list
1470 by _bfd_generic_link_add_one_symbol. Symbols may not be
1471 added twice to the undefs list. Also, if the new symbol is
1472 undefweak then we don't want to lose the strong undef. */
1473 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1de1a317 1474 {
1de1a317 1475 h->root.type = bfd_link_hash_undefined;
1de1a317
L
1476 h->root.u.undef.abfd = abfd;
1477 }
1478 else
1479 {
1480 h->root.type = bfd_link_hash_new;
1481 h->root.u.undef.abfd = NULL;
1482 }
1483
f5eda473 1484 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
252b5132 1485 {
f5eda473
AM
1486 /* If the new symbol is hidden or internal, completely undo
1487 any dynamic link state. */
1488 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1489 h->forced_local = 0;
1490 h->ref_dynamic = 0;
45d6a902 1491 }
f5eda473
AM
1492 else
1493 h->ref_dynamic = 1;
1494 h->def_dynamic = 0;
45d6a902
AM
1495 /* FIXME: Should we check type and size for protected symbol? */
1496 h->size = 0;
1497 h->type = 0;
1498 return TRUE;
1499 }
14a793b2 1500
15b43f48
AM
1501 /* If a new weak symbol definition comes from a regular file and the
1502 old symbol comes from a dynamic library, we treat the new one as
1503 strong. Similarly, an old weak symbol definition from a regular
1504 file is treated as strong when the new symbol comes from a dynamic
1505 library. Further, an old weak symbol from a dynamic library is
1506 treated as strong if the new symbol is from a dynamic library.
1507 This reflects the way glibc's ld.so works.
1508
165f707a
AM
1509 Also allow a weak symbol to override a linker script symbol
1510 defined by an early pass over the script. This is done so the
1511 linker knows the symbol is defined in an object file, for the
1512 DEFINED script function.
1513
15b43f48
AM
1514 Do this before setting *type_change_ok or *size_change_ok so that
1515 we warn properly when dynamic library symbols are overridden. */
1516
165f707a 1517 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
0f8a2703 1518 newweak = FALSE;
15b43f48 1519 if (olddef && newdyn)
0f8a2703
AM
1520 oldweak = FALSE;
1521
d334575b 1522 /* Allow changes between different types of function symbol. */
0a36a439 1523 if (newfunc && oldfunc)
fcb93ecf
PB
1524 *type_change_ok = TRUE;
1525
79349b09
AM
1526 /* It's OK to change the type if either the existing symbol or the
1527 new symbol is weak. A type change is also OK if the old symbol
1528 is undefined and the new symbol is defined. */
252b5132 1529
79349b09
AM
1530 if (oldweak
1531 || newweak
1532 || (newdef
1533 && h->root.type == bfd_link_hash_undefined))
1534 *type_change_ok = TRUE;
1535
1536 /* It's OK to change the size if either the existing symbol or the
1537 new symbol is weak, or if the old symbol is undefined. */
1538
1539 if (*type_change_ok
1540 || h->root.type == bfd_link_hash_undefined)
1541 *size_change_ok = TRUE;
45d6a902 1542
45d6a902
AM
1543 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1544 symbol, respectively, appears to be a common symbol in a dynamic
1545 object. If a symbol appears in an uninitialized section, and is
1546 not weak, and is not a function, then it may be a common symbol
1547 which was resolved when the dynamic object was created. We want
1548 to treat such symbols specially, because they raise special
1549 considerations when setting the symbol size: if the symbol
1550 appears as a common symbol in a regular object, and the size in
1551 the regular object is larger, we must make sure that we use the
1552 larger size. This problematic case can always be avoided in C,
1553 but it must be handled correctly when using Fortran shared
1554 libraries.
1555
1556 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1557 likewise for OLDDYNCOMMON and OLDDEF.
1558
1559 Note that this test is just a heuristic, and that it is quite
1560 possible to have an uninitialized symbol in a shared object which
1561 is really a definition, rather than a common symbol. This could
1562 lead to some minor confusion when the symbol really is a common
1563 symbol in some regular object. However, I think it will be
1564 harmless. */
1565
1566 if (newdyn
1567 && newdef
79349b09 1568 && !newweak
45d6a902
AM
1569 && (sec->flags & SEC_ALLOC) != 0
1570 && (sec->flags & SEC_LOAD) == 0
1571 && sym->st_size > 0
0a36a439 1572 && !newfunc)
45d6a902
AM
1573 newdyncommon = TRUE;
1574 else
1575 newdyncommon = FALSE;
1576
1577 if (olddyn
1578 && olddef
1579 && h->root.type == bfd_link_hash_defined
f5385ebf 1580 && h->def_dynamic
45d6a902
AM
1581 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1582 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1583 && h->size > 0
0a36a439 1584 && !oldfunc)
45d6a902
AM
1585 olddyncommon = TRUE;
1586 else
1587 olddyncommon = FALSE;
1588
a4d8e49b
L
1589 /* We now know everything about the old and new symbols. We ask the
1590 backend to check if we can merge them. */
5d13b3b3
AM
1591 if (bed->merge_symbol != NULL)
1592 {
1593 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1594 return FALSE;
1595 sec = *psec;
1596 }
a4d8e49b 1597
a83ef4d1
L
1598 /* There are multiple definitions of a normal symbol. Skip the
1599 default symbol as well as definition from an IR object. */
93f4de39 1600 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
a83ef4d1
L
1601 && !default_sym && h->def_regular
1602 && !(oldbfd != NULL
1603 && (oldbfd->flags & BFD_PLUGIN) != 0
1604 && (abfd->flags & BFD_PLUGIN) == 0))
93f4de39
RL
1605 {
1606 /* Handle a multiple definition. */
1607 (*info->callbacks->multiple_definition) (info, &h->root,
1608 abfd, sec, *pvalue);
1609 *skip = TRUE;
1610 return TRUE;
1611 }
1612
45d6a902
AM
1613 /* If both the old and the new symbols look like common symbols in a
1614 dynamic object, set the size of the symbol to the larger of the
1615 two. */
1616
1617 if (olddyncommon
1618 && newdyncommon
1619 && sym->st_size != h->size)
1620 {
1621 /* Since we think we have two common symbols, issue a multiple
1622 common warning if desired. Note that we only warn if the
1623 size is different. If the size is the same, we simply let
1624 the old symbol override the new one as normally happens with
1625 symbols defined in dynamic objects. */
1626
1a72702b
AM
1627 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1628 bfd_link_hash_common, sym->st_size);
45d6a902
AM
1629 if (sym->st_size > h->size)
1630 h->size = sym->st_size;
252b5132 1631
45d6a902 1632 *size_change_ok = TRUE;
252b5132
RH
1633 }
1634
45d6a902
AM
1635 /* If we are looking at a dynamic object, and we have found a
1636 definition, we need to see if the symbol was already defined by
1637 some other object. If so, we want to use the existing
1638 definition, and we do not want to report a multiple symbol
1639 definition error; we do this by clobbering *PSEC to be
1640 bfd_und_section_ptr.
1641
1642 We treat a common symbol as a definition if the symbol in the
1643 shared library is a function, since common symbols always
1644 represent variables; this can cause confusion in principle, but
1645 any such confusion would seem to indicate an erroneous program or
1646 shared library. We also permit a common symbol in a regular
8170f769 1647 object to override a weak symbol in a shared object. */
45d6a902
AM
1648
1649 if (newdyn
1650 && newdef
77cfaee6 1651 && (olddef
45d6a902 1652 || (h->root.type == bfd_link_hash_common
8170f769 1653 && (newweak || newfunc))))
45d6a902 1654 {
7ba11550 1655 *override = abfd;
45d6a902
AM
1656 newdef = FALSE;
1657 newdyncommon = FALSE;
252b5132 1658
45d6a902
AM
1659 *psec = sec = bfd_und_section_ptr;
1660 *size_change_ok = TRUE;
252b5132 1661
45d6a902
AM
1662 /* If we get here when the old symbol is a common symbol, then
1663 we are explicitly letting it override a weak symbol or
1664 function in a dynamic object, and we don't want to warn about
1665 a type change. If the old symbol is a defined symbol, a type
1666 change warning may still be appropriate. */
252b5132 1667
45d6a902
AM
1668 if (h->root.type == bfd_link_hash_common)
1669 *type_change_ok = TRUE;
1670 }
1671
1672 /* Handle the special case of an old common symbol merging with a
1673 new symbol which looks like a common symbol in a shared object.
1674 We change *PSEC and *PVALUE to make the new symbol look like a
91134c82
L
1675 common symbol, and let _bfd_generic_link_add_one_symbol do the
1676 right thing. */
45d6a902
AM
1677
1678 if (newdyncommon
1679 && h->root.type == bfd_link_hash_common)
1680 {
7ba11550 1681 *override = oldbfd;
45d6a902
AM
1682 newdef = FALSE;
1683 newdyncommon = FALSE;
1684 *pvalue = sym->st_size;
a4d8e49b 1685 *psec = sec = bed->common_section (oldsec);
45d6a902
AM
1686 *size_change_ok = TRUE;
1687 }
1688
c5e2cead 1689 /* Skip weak definitions of symbols that are already defined. */
f41d945b 1690 if (newdef && olddef && newweak)
54ac0771 1691 {
35ed3f94 1692 /* Don't skip new non-IR weak syms. */
3a5dbfb2
AM
1693 if (!(oldbfd != NULL
1694 && (oldbfd->flags & BFD_PLUGIN) != 0
35ed3f94 1695 && (abfd->flags & BFD_PLUGIN) == 0))
57fa7b8c
AM
1696 {
1697 newdef = FALSE;
1698 *skip = TRUE;
1699 }
54ac0771
L
1700
1701 /* Merge st_other. If the symbol already has a dynamic index,
1702 but visibility says it should not be visible, turn it into a
1703 local symbol. */
b8417128 1704 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
54ac0771
L
1705 if (h->dynindx != -1)
1706 switch (ELF_ST_VISIBILITY (h->other))
1707 {
1708 case STV_INTERNAL:
1709 case STV_HIDDEN:
1710 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1711 break;
1712 }
1713 }
c5e2cead 1714
45d6a902
AM
1715 /* If the old symbol is from a dynamic object, and the new symbol is
1716 a definition which is not from a dynamic object, then the new
1717 symbol overrides the old symbol. Symbols from regular files
1718 always take precedence over symbols from dynamic objects, even if
1719 they are defined after the dynamic object in the link.
1720
1721 As above, we again permit a common symbol in a regular object to
1722 override a definition in a shared object if the shared object
0f8a2703 1723 symbol is a function or is weak. */
45d6a902
AM
1724
1725 flip = NULL;
77cfaee6 1726 if (!newdyn
45d6a902
AM
1727 && (newdef
1728 || (bfd_is_com_section (sec)
0a36a439 1729 && (oldweak || oldfunc)))
45d6a902
AM
1730 && olddyn
1731 && olddef
f5385ebf 1732 && h->def_dynamic)
45d6a902
AM
1733 {
1734 /* Change the hash table entry to undefined, and let
1735 _bfd_generic_link_add_one_symbol do the right thing with the
1736 new definition. */
1737
1738 h->root.type = bfd_link_hash_undefined;
1739 h->root.u.undef.abfd = h->root.u.def.section->owner;
1740 *size_change_ok = TRUE;
1741
1742 olddef = FALSE;
1743 olddyncommon = FALSE;
1744
1745 /* We again permit a type change when a common symbol may be
1746 overriding a function. */
1747
1748 if (bfd_is_com_section (sec))
0a36a439
L
1749 {
1750 if (oldfunc)
1751 {
1752 /* If a common symbol overrides a function, make sure
1753 that it isn't defined dynamically nor has type
1754 function. */
1755 h->def_dynamic = 0;
1756 h->type = STT_NOTYPE;
1757 }
1758 *type_change_ok = TRUE;
1759 }
45d6a902 1760
6c9b78e6
AM
1761 if (hi->root.type == bfd_link_hash_indirect)
1762 flip = hi;
45d6a902
AM
1763 else
1764 /* This union may have been set to be non-NULL when this symbol
1765 was seen in a dynamic object. We must force the union to be
1766 NULL, so that it is correct for a regular symbol. */
1767 h->verinfo.vertree = NULL;
1768 }
1769
1770 /* Handle the special case of a new common symbol merging with an
1771 old symbol that looks like it might be a common symbol defined in
1772 a shared object. Note that we have already handled the case in
1773 which a new common symbol should simply override the definition
1774 in the shared library. */
1775
1776 if (! newdyn
1777 && bfd_is_com_section (sec)
1778 && olddyncommon)
1779 {
1780 /* It would be best if we could set the hash table entry to a
1781 common symbol, but we don't know what to use for the section
1782 or the alignment. */
1a72702b
AM
1783 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1784 bfd_link_hash_common, sym->st_size);
45d6a902 1785
4cc11e76 1786 /* If the presumed common symbol in the dynamic object is
45d6a902
AM
1787 larger, pretend that the new symbol has its size. */
1788
1789 if (h->size > *pvalue)
1790 *pvalue = h->size;
1791
af44c138
L
1792 /* We need to remember the alignment required by the symbol
1793 in the dynamic object. */
1794 BFD_ASSERT (pold_alignment);
1795 *pold_alignment = h->root.u.def.section->alignment_power;
45d6a902
AM
1796
1797 olddef = FALSE;
1798 olddyncommon = FALSE;
1799
1800 h->root.type = bfd_link_hash_undefined;
1801 h->root.u.undef.abfd = h->root.u.def.section->owner;
1802
1803 *size_change_ok = TRUE;
1804 *type_change_ok = TRUE;
1805
6c9b78e6
AM
1806 if (hi->root.type == bfd_link_hash_indirect)
1807 flip = hi;
45d6a902
AM
1808 else
1809 h->verinfo.vertree = NULL;
1810 }
1811
1812 if (flip != NULL)
1813 {
1814 /* Handle the case where we had a versioned symbol in a dynamic
1815 library and now find a definition in a normal object. In this
1816 case, we make the versioned symbol point to the normal one. */
45d6a902 1817 flip->root.type = h->root.type;
00cbee0a 1818 flip->root.u.undef.abfd = h->root.u.undef.abfd;
45d6a902
AM
1819 h->root.type = bfd_link_hash_indirect;
1820 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
fcfa13d2 1821 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
f5385ebf 1822 if (h->def_dynamic)
45d6a902 1823 {
f5385ebf
AM
1824 h->def_dynamic = 0;
1825 flip->ref_dynamic = 1;
45d6a902
AM
1826 }
1827 }
1828
45d6a902
AM
1829 return TRUE;
1830}
1831
1832/* This function is called to create an indirect symbol from the
1833 default for the symbol with the default version if needed. The
4f3fedcf 1834 symbol is described by H, NAME, SYM, SEC, and VALUE. We
0f8a2703 1835 set DYNSYM if the new indirect symbol is dynamic. */
45d6a902 1836
28caa186 1837static bfd_boolean
268b6b39
AM
1838_bfd_elf_add_default_symbol (bfd *abfd,
1839 struct bfd_link_info *info,
1840 struct elf_link_hash_entry *h,
1841 const char *name,
1842 Elf_Internal_Sym *sym,
4f3fedcf
AM
1843 asection *sec,
1844 bfd_vma value,
1845 bfd **poldbfd,
e3c9d234 1846 bfd_boolean *dynsym)
45d6a902
AM
1847{
1848 bfd_boolean type_change_ok;
1849 bfd_boolean size_change_ok;
1850 bfd_boolean skip;
1851 char *shortname;
1852 struct elf_link_hash_entry *hi;
1853 struct bfd_link_hash_entry *bh;
9c5bfbb7 1854 const struct elf_backend_data *bed;
45d6a902
AM
1855 bfd_boolean collect;
1856 bfd_boolean dynamic;
7ba11550 1857 bfd *override;
45d6a902
AM
1858 char *p;
1859 size_t len, shortlen;
ffd65175 1860 asection *tmp_sec;
6e33951e 1861 bfd_boolean matched;
45d6a902 1862
422f1182
L
1863 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1864 return TRUE;
1865
45d6a902
AM
1866 /* If this symbol has a version, and it is the default version, we
1867 create an indirect symbol from the default name to the fully
1868 decorated name. This will cause external references which do not
1869 specify a version to be bound to this version of the symbol. */
1870 p = strchr (name, ELF_VER_CHR);
422f1182
L
1871 if (h->versioned == unknown)
1872 {
1873 if (p == NULL)
1874 {
1875 h->versioned = unversioned;
1876 return TRUE;
1877 }
1878 else
1879 {
1880 if (p[1] != ELF_VER_CHR)
1881 {
1882 h->versioned = versioned_hidden;
1883 return TRUE;
1884 }
1885 else
1886 h->versioned = versioned;
1887 }
1888 }
4373f8af
L
1889 else
1890 {
1891 /* PR ld/19073: We may see an unversioned definition after the
1892 default version. */
1893 if (p == NULL)
1894 return TRUE;
1895 }
45d6a902 1896
45d6a902
AM
1897 bed = get_elf_backend_data (abfd);
1898 collect = bed->collect;
1899 dynamic = (abfd->flags & DYNAMIC) != 0;
1900
1901 shortlen = p - name;
a50b1753 1902 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
45d6a902
AM
1903 if (shortname == NULL)
1904 return FALSE;
1905 memcpy (shortname, name, shortlen);
1906 shortname[shortlen] = '\0';
1907
1908 /* We are going to create a new symbol. Merge it with any existing
1909 symbol with this name. For the purposes of the merge, act as
1910 though we were defining the symbol we just defined, although we
1911 actually going to define an indirect symbol. */
1912 type_change_ok = FALSE;
1913 size_change_ok = FALSE;
6e33951e 1914 matched = TRUE;
ffd65175
AM
1915 tmp_sec = sec;
1916 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
4f3fedcf 1917 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 1918 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
1919 return FALSE;
1920
1921 if (skip)
1922 goto nondefault;
1923
5fa370e4 1924 if (hi->def_regular || ELF_COMMON_DEF_P (hi))
5b677558
AM
1925 {
1926 /* If the undecorated symbol will have a version added by a
1927 script different to H, then don't indirect to/from the
1928 undecorated symbol. This isn't ideal because we may not yet
1929 have seen symbol versions, if given by a script on the
1930 command line rather than via --version-script. */
1931 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1932 {
1933 bfd_boolean hide;
1934
1935 hi->verinfo.vertree
1936 = bfd_find_version_for_sym (info->version_info,
1937 hi->root.root.string, &hide);
1938 if (hi->verinfo.vertree != NULL && hide)
1939 {
1940 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1941 goto nondefault;
1942 }
1943 }
1944 if (hi->verinfo.vertree != NULL
1945 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1946 goto nondefault;
1947 }
1948
45d6a902
AM
1949 if (! override)
1950 {
c6e8a9a8 1951 /* Add the default symbol if not performing a relocatable link. */
0e1862bb 1952 if (! bfd_link_relocatable (info))
c6e8a9a8
L
1953 {
1954 bh = &hi->root;
fbcc8baf 1955 if (bh->type == bfd_link_hash_defined
6cc71b82 1956 && bh->u.def.section->owner != NULL
fbcc8baf
L
1957 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1958 {
1959 /* Mark the previous definition from IR object as
1960 undefined so that the generic linker will override
1961 it. */
1962 bh->type = bfd_link_hash_undefined;
1963 bh->u.undef.abfd = bh->u.def.section->owner;
1964 }
c6e8a9a8
L
1965 if (! (_bfd_generic_link_add_one_symbol
1966 (info, abfd, shortname, BSF_INDIRECT,
1967 bfd_ind_section_ptr,
1968 0, name, FALSE, collect, &bh)))
1969 return FALSE;
1970 hi = (struct elf_link_hash_entry *) bh;
1971 }
45d6a902
AM
1972 }
1973 else
1974 {
1975 /* In this case the symbol named SHORTNAME is overriding the
1976 indirect symbol we want to add. We were planning on making
1977 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1978 is the name without a version. NAME is the fully versioned
1979 name, and it is the default version.
1980
1981 Overriding means that we already saw a definition for the
1982 symbol SHORTNAME in a regular object, and it is overriding
1983 the symbol defined in the dynamic object.
1984
1985 When this happens, we actually want to change NAME, the
1986 symbol we just added, to refer to SHORTNAME. This will cause
1987 references to NAME in the shared object to become references
1988 to SHORTNAME in the regular object. This is what we expect
1989 when we override a function in a shared object: that the
1990 references in the shared object will be mapped to the
1991 definition in the regular object. */
1992
1993 while (hi->root.type == bfd_link_hash_indirect
1994 || hi->root.type == bfd_link_hash_warning)
1995 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1996
1997 h->root.type = bfd_link_hash_indirect;
1998 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
f5385ebf 1999 if (h->def_dynamic)
45d6a902 2000 {
f5385ebf
AM
2001 h->def_dynamic = 0;
2002 hi->ref_dynamic = 1;
2003 if (hi->ref_regular
2004 || hi->def_regular)
45d6a902 2005 {
c152c796 2006 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
45d6a902
AM
2007 return FALSE;
2008 }
2009 }
2010
2011 /* Now set HI to H, so that the following code will set the
2012 other fields correctly. */
2013 hi = h;
2014 }
2015
fab4a87f
L
2016 /* Check if HI is a warning symbol. */
2017 if (hi->root.type == bfd_link_hash_warning)
2018 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2019
45d6a902
AM
2020 /* If there is a duplicate definition somewhere, then HI may not
2021 point to an indirect symbol. We will have reported an error to
2022 the user in that case. */
2023
2024 if (hi->root.type == bfd_link_hash_indirect)
2025 {
2026 struct elf_link_hash_entry *ht;
2027
45d6a902 2028 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
fcfa13d2 2029 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
45d6a902 2030
68c88cd4
AM
2031 /* A reference to the SHORTNAME symbol from a dynamic library
2032 will be satisfied by the versioned symbol at runtime. In
2033 effect, we have a reference to the versioned symbol. */
2034 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2035 hi->dynamic_def |= ht->dynamic_def;
2036
45d6a902
AM
2037 /* See if the new flags lead us to realize that the symbol must
2038 be dynamic. */
2039 if (! *dynsym)
2040 {
2041 if (! dynamic)
2042 {
0e1862bb 2043 if (! bfd_link_executable (info)
90c984fc 2044 || hi->def_dynamic
f5385ebf 2045 || hi->ref_dynamic)
45d6a902
AM
2046 *dynsym = TRUE;
2047 }
2048 else
2049 {
f5385ebf 2050 if (hi->ref_regular)
45d6a902
AM
2051 *dynsym = TRUE;
2052 }
2053 }
2054 }
2055
2056 /* We also need to define an indirection from the nondefault version
2057 of the symbol. */
2058
dc1e8a47 2059 nondefault:
45d6a902 2060 len = strlen (name);
a50b1753 2061 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
2062 if (shortname == NULL)
2063 return FALSE;
2064 memcpy (shortname, name, shortlen);
2065 memcpy (shortname + shortlen, p + 1, len - shortlen);
2066
2067 /* Once again, merge with any existing symbol. */
2068 type_change_ok = FALSE;
2069 size_change_ok = FALSE;
ffd65175
AM
2070 tmp_sec = sec;
2071 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
115c6d5c 2072 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 2073 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
2074 return FALSE;
2075
2076 if (skip)
2077 return TRUE;
2078
2079 if (override)
2080 {
2081 /* Here SHORTNAME is a versioned name, so we don't expect to see
2082 the type of override we do in the case above unless it is
4cc11e76 2083 overridden by a versioned definition. */
45d6a902
AM
2084 if (hi->root.type != bfd_link_hash_defined
2085 && hi->root.type != bfd_link_hash_defweak)
4eca0228 2086 _bfd_error_handler
695344c0 2087 /* xgettext:c-format */
871b3ab2 2088 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
d003868e 2089 abfd, shortname);
45d6a902
AM
2090 }
2091 else
2092 {
2093 bh = &hi->root;
2094 if (! (_bfd_generic_link_add_one_symbol
2095 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 2096 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
2097 return FALSE;
2098 hi = (struct elf_link_hash_entry *) bh;
2099
2100 /* If there is a duplicate definition somewhere, then HI may not
2101 point to an indirect symbol. We will have reported an error
2102 to the user in that case. */
2103
2104 if (hi->root.type == bfd_link_hash_indirect)
2105 {
fcfa13d2 2106 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
68c88cd4
AM
2107 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2108 hi->dynamic_def |= h->dynamic_def;
45d6a902
AM
2109
2110 /* See if the new flags lead us to realize that the symbol
2111 must be dynamic. */
2112 if (! *dynsym)
2113 {
2114 if (! dynamic)
2115 {
0e1862bb 2116 if (! bfd_link_executable (info)
f5385ebf 2117 || hi->ref_dynamic)
45d6a902
AM
2118 *dynsym = TRUE;
2119 }
2120 else
2121 {
f5385ebf 2122 if (hi->ref_regular)
45d6a902
AM
2123 *dynsym = TRUE;
2124 }
2125 }
2126 }
2127 }
2128
2129 return TRUE;
2130}
2131\f
2132/* This routine is used to export all defined symbols into the dynamic
2133 symbol table. It is called via elf_link_hash_traverse. */
2134
28caa186 2135static bfd_boolean
268b6b39 2136_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2137{
a50b1753 2138 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
2139
2140 /* Ignore indirect symbols. These are added by the versioning code. */
2141 if (h->root.type == bfd_link_hash_indirect)
2142 return TRUE;
2143
7686d77d
AM
2144 /* Ignore this if we won't export it. */
2145 if (!eif->info->export_dynamic && !h->dynamic)
2146 return TRUE;
45d6a902
AM
2147
2148 if (h->dynindx == -1
fd91d419
L
2149 && (h->def_regular || h->ref_regular)
2150 && ! bfd_hide_sym_by_version (eif->info->version_info,
2151 h->root.root.string))
45d6a902 2152 {
fd91d419 2153 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 2154 {
fd91d419
L
2155 eif->failed = TRUE;
2156 return FALSE;
45d6a902
AM
2157 }
2158 }
2159
2160 return TRUE;
2161}
2162\f
2163/* Look through the symbols which are defined in other shared
2164 libraries and referenced here. Update the list of version
2165 dependencies. This will be put into the .gnu.version_r section.
2166 This function is called via elf_link_hash_traverse. */
2167
28caa186 2168static bfd_boolean
268b6b39
AM
2169_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2170 void *data)
45d6a902 2171{
a50b1753 2172 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
2173 Elf_Internal_Verneed *t;
2174 Elf_Internal_Vernaux *a;
986f0783 2175 size_t amt;
45d6a902 2176
45d6a902
AM
2177 /* We only care about symbols defined in shared objects with version
2178 information. */
f5385ebf
AM
2179 if (!h->def_dynamic
2180 || h->def_regular
45d6a902 2181 || h->dynindx == -1
7b20f099
AM
2182 || h->verinfo.verdef == NULL
2183 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2184 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
45d6a902
AM
2185 return TRUE;
2186
2187 /* See if we already know about this version. */
28caa186
AM
2188 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2189 t != NULL;
2190 t = t->vn_nextref)
45d6a902
AM
2191 {
2192 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2193 continue;
2194
2195 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2196 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2197 return TRUE;
2198
2199 break;
2200 }
2201
2202 /* This is a new version. Add it to tree we are building. */
2203
2204 if (t == NULL)
2205 {
2206 amt = sizeof *t;
a50b1753 2207 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
2208 if (t == NULL)
2209 {
2210 rinfo->failed = TRUE;
2211 return FALSE;
2212 }
2213
2214 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
2215 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2216 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
2217 }
2218
2219 amt = sizeof *a;
a50b1753 2220 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
2221 if (a == NULL)
2222 {
2223 rinfo->failed = TRUE;
2224 return FALSE;
2225 }
45d6a902
AM
2226
2227 /* Note that we are copying a string pointer here, and testing it
2228 above. If bfd_elf_string_from_elf_section is ever changed to
2229 discard the string data when low in memory, this will have to be
2230 fixed. */
2231 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2232
2233 a->vna_flags = h->verinfo.verdef->vd_flags;
2234 a->vna_nextptr = t->vn_auxptr;
2235
2236 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2237 ++rinfo->vers;
2238
2239 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2240
2241 t->vn_auxptr = a;
2242
2243 return TRUE;
2244}
2245
099bb8fb
L
2246/* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2247 hidden. Set *T_P to NULL if there is no match. */
2248
2249static bfd_boolean
2250_bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2251 struct elf_link_hash_entry *h,
2252 const char *version_p,
2253 struct bfd_elf_version_tree **t_p,
2254 bfd_boolean *hide)
2255{
2256 struct bfd_elf_version_tree *t;
2257
2258 /* Look for the version. If we find it, it is no longer weak. */
2259 for (t = info->version_info; t != NULL; t = t->next)
2260 {
2261 if (strcmp (t->name, version_p) == 0)
2262 {
2263 size_t len;
2264 char *alc;
2265 struct bfd_elf_version_expr *d;
2266
2267 len = version_p - h->root.root.string;
2268 alc = (char *) bfd_malloc (len);
2269 if (alc == NULL)
2270 return FALSE;
2271 memcpy (alc, h->root.root.string, len - 1);
2272 alc[len - 1] = '\0';
2273 if (alc[len - 2] == ELF_VER_CHR)
2274 alc[len - 2] = '\0';
2275
2276 h->verinfo.vertree = t;
2277 t->used = TRUE;
2278 d = NULL;
2279
2280 if (t->globals.list != NULL)
2281 d = (*t->match) (&t->globals, NULL, alc);
2282
2283 /* See if there is anything to force this symbol to
2284 local scope. */
2285 if (d == NULL && t->locals.list != NULL)
2286 {
2287 d = (*t->match) (&t->locals, NULL, alc);
2288 if (d != NULL
2289 && h->dynindx != -1
2290 && ! info->export_dynamic)
2291 *hide = TRUE;
2292 }
2293
2294 free (alc);
2295 break;
2296 }
2297 }
2298
2299 *t_p = t;
2300
2301 return TRUE;
2302}
2303
2304/* Return TRUE if the symbol H is hidden by version script. */
2305
2306bfd_boolean
2307_bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2308 struct elf_link_hash_entry *h)
2309{
2310 const char *p;
2311 bfd_boolean hide = FALSE;
2312 const struct elf_backend_data *bed
2313 = get_elf_backend_data (info->output_bfd);
2314
2315 /* Version script only hides symbols defined in regular objects. */
2316 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2317 return TRUE;
2318
2319 p = strchr (h->root.root.string, ELF_VER_CHR);
2320 if (p != NULL && h->verinfo.vertree == NULL)
2321 {
2322 struct bfd_elf_version_tree *t;
2323
2324 ++p;
2325 if (*p == ELF_VER_CHR)
2326 ++p;
2327
2328 if (*p != '\0'
2329 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2330 && hide)
2331 {
2332 if (hide)
2333 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2334 return TRUE;
2335 }
2336 }
2337
2338 /* If we don't have a version for this symbol, see if we can find
2339 something. */
2340 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2341 {
2342 h->verinfo.vertree
2343 = bfd_find_version_for_sym (info->version_info,
2344 h->root.root.string, &hide);
2345 if (h->verinfo.vertree != NULL && hide)
2346 {
2347 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2348 return TRUE;
2349 }
2350 }
2351
2352 return FALSE;
2353}
2354
45d6a902
AM
2355/* Figure out appropriate versions for all the symbols. We may not
2356 have the version number script until we have read all of the input
2357 files, so until that point we don't know which symbols should be
2358 local. This function is called via elf_link_hash_traverse. */
2359
28caa186 2360static bfd_boolean
268b6b39 2361_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 2362{
28caa186 2363 struct elf_info_failed *sinfo;
45d6a902 2364 struct bfd_link_info *info;
9c5bfbb7 2365 const struct elf_backend_data *bed;
45d6a902
AM
2366 struct elf_info_failed eif;
2367 char *p;
099bb8fb 2368 bfd_boolean hide;
45d6a902 2369
a50b1753 2370 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
2371 info = sinfo->info;
2372
45d6a902
AM
2373 /* Fix the symbol flags. */
2374 eif.failed = FALSE;
2375 eif.info = info;
2376 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2377 {
2378 if (eif.failed)
2379 sinfo->failed = TRUE;
2380 return FALSE;
2381 }
2382
0a640d71
L
2383 bed = get_elf_backend_data (info->output_bfd);
2384
45d6a902
AM
2385 /* We only need version numbers for symbols defined in regular
2386 objects. */
5fa370e4 2387 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
0a640d71
L
2388 {
2389 /* Hide symbols defined in discarded input sections. */
2390 if ((h->root.type == bfd_link_hash_defined
2391 || h->root.type == bfd_link_hash_defweak)
2392 && discarded_section (h->root.u.def.section))
2393 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2394 return TRUE;
2395 }
45d6a902 2396
099bb8fb 2397 hide = FALSE;
45d6a902
AM
2398 p = strchr (h->root.root.string, ELF_VER_CHR);
2399 if (p != NULL && h->verinfo.vertree == NULL)
2400 {
2401 struct bfd_elf_version_tree *t;
45d6a902 2402
45d6a902
AM
2403 ++p;
2404 if (*p == ELF_VER_CHR)
6e33951e 2405 ++p;
45d6a902
AM
2406
2407 /* If there is no version string, we can just return out. */
2408 if (*p == '\0')
6e33951e 2409 return TRUE;
45d6a902 2410
099bb8fb 2411 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
45d6a902 2412 {
099bb8fb
L
2413 sinfo->failed = TRUE;
2414 return FALSE;
45d6a902
AM
2415 }
2416
099bb8fb
L
2417 if (hide)
2418 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2419
45d6a902
AM
2420 /* If we are building an application, we need to create a
2421 version node for this version. */
0e1862bb 2422 if (t == NULL && bfd_link_executable (info))
45d6a902
AM
2423 {
2424 struct bfd_elf_version_tree **pp;
2425 int version_index;
2426
2427 /* If we aren't going to export this symbol, we don't need
2428 to worry about it. */
2429 if (h->dynindx == -1)
2430 return TRUE;
2431
ef53be89
AM
2432 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2433 sizeof *t);
45d6a902
AM
2434 if (t == NULL)
2435 {
2436 sinfo->failed = TRUE;
2437 return FALSE;
2438 }
2439
45d6a902 2440 t->name = p;
45d6a902
AM
2441 t->name_indx = (unsigned int) -1;
2442 t->used = TRUE;
2443
2444 version_index = 1;
2445 /* Don't count anonymous version tag. */
fd91d419
L
2446 if (sinfo->info->version_info != NULL
2447 && sinfo->info->version_info->vernum == 0)
45d6a902 2448 version_index = 0;
fd91d419
L
2449 for (pp = &sinfo->info->version_info;
2450 *pp != NULL;
2451 pp = &(*pp)->next)
45d6a902
AM
2452 ++version_index;
2453 t->vernum = version_index;
2454
2455 *pp = t;
2456
2457 h->verinfo.vertree = t;
2458 }
2459 else if (t == NULL)
2460 {
2461 /* We could not find the version for a symbol when
2462 generating a shared archive. Return an error. */
4eca0228 2463 _bfd_error_handler
695344c0 2464 /* xgettext:c-format */
871b3ab2 2465 (_("%pB: version node not found for symbol %s"),
28caa186 2466 info->output_bfd, h->root.root.string);
45d6a902
AM
2467 bfd_set_error (bfd_error_bad_value);
2468 sinfo->failed = TRUE;
2469 return FALSE;
2470 }
45d6a902
AM
2471 }
2472
2473 /* If we don't have a version for this symbol, see if we can find
2474 something. */
099bb8fb
L
2475 if (!hide
2476 && h->verinfo.vertree == NULL
2477 && sinfo->info->version_info != NULL)
45d6a902 2478 {
fd91d419
L
2479 h->verinfo.vertree
2480 = bfd_find_version_for_sym (sinfo->info->version_info,
2481 h->root.root.string, &hide);
1e8fa21e
AM
2482 if (h->verinfo.vertree != NULL && hide)
2483 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2484 }
2485
2486 return TRUE;
2487}
2488\f
45d6a902
AM
2489/* Read and swap the relocs from the section indicated by SHDR. This
2490 may be either a REL or a RELA section. The relocations are
2491 translated into RELA relocations and stored in INTERNAL_RELOCS,
2492 which should have already been allocated to contain enough space.
2493 The EXTERNAL_RELOCS are a buffer where the external form of the
2494 relocations should be stored.
2495
2496 Returns FALSE if something goes wrong. */
2497
2498static bfd_boolean
268b6b39 2499elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2500 asection *sec,
268b6b39
AM
2501 Elf_Internal_Shdr *shdr,
2502 void *external_relocs,
2503 Elf_Internal_Rela *internal_relocs)
45d6a902 2504{
9c5bfbb7 2505 const struct elf_backend_data *bed;
268b6b39 2506 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2507 const bfd_byte *erela;
2508 const bfd_byte *erelaend;
2509 Elf_Internal_Rela *irela;
243ef1e0
L
2510 Elf_Internal_Shdr *symtab_hdr;
2511 size_t nsyms;
45d6a902 2512
45d6a902
AM
2513 /* Position ourselves at the start of the section. */
2514 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2515 return FALSE;
2516
2517 /* Read the relocations. */
2518 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2519 return FALSE;
2520
243ef1e0 2521 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2522 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2523
45d6a902
AM
2524 bed = get_elf_backend_data (abfd);
2525
2526 /* Convert the external relocations to the internal format. */
2527 if (shdr->sh_entsize == bed->s->sizeof_rel)
2528 swap_in = bed->s->swap_reloc_in;
2529 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2530 swap_in = bed->s->swap_reloca_in;
2531 else
2532 {
2533 bfd_set_error (bfd_error_wrong_format);
2534 return FALSE;
2535 }
2536
a50b1753 2537 erela = (const bfd_byte *) external_relocs;
f55b1e32
AM
2538 /* Setting erelaend like this and comparing with <= handles case of
2539 a fuzzed object with sh_size not a multiple of sh_entsize. */
2540 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
45d6a902 2541 irela = internal_relocs;
f55b1e32 2542 while (erela <= erelaend)
45d6a902 2543 {
243ef1e0
L
2544 bfd_vma r_symndx;
2545
45d6a902 2546 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2547 r_symndx = ELF32_R_SYM (irela->r_info);
2548 if (bed->s->arch_size == 64)
2549 r_symndx >>= 24;
ce98a316
NC
2550 if (nsyms > 0)
2551 {
2552 if ((size_t) r_symndx >= nsyms)
2553 {
4eca0228 2554 _bfd_error_handler
695344c0 2555 /* xgettext:c-format */
2dcf00ce
AM
2556 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2557 " for offset %#" PRIx64 " in section `%pA'"),
2558 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2559 (uint64_t) irela->r_offset, sec);
ce98a316
NC
2560 bfd_set_error (bfd_error_bad_value);
2561 return FALSE;
2562 }
2563 }
cf35638d 2564 else if (r_symndx != STN_UNDEF)
243ef1e0 2565 {
4eca0228 2566 _bfd_error_handler
695344c0 2567 /* xgettext:c-format */
2dcf00ce
AM
2568 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2569 " for offset %#" PRIx64 " in section `%pA'"
ce98a316 2570 " when the object file has no symbol table"),
2dcf00ce
AM
2571 abfd, (uint64_t) r_symndx,
2572 (uint64_t) irela->r_offset, sec);
243ef1e0
L
2573 bfd_set_error (bfd_error_bad_value);
2574 return FALSE;
2575 }
45d6a902
AM
2576 irela += bed->s->int_rels_per_ext_rel;
2577 erela += shdr->sh_entsize;
2578 }
2579
2580 return TRUE;
2581}
2582
2583/* Read and swap the relocs for a section O. They may have been
2584 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2585 not NULL, they are used as buffers to read into. They are known to
2586 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2587 the return value is allocated using either malloc or bfd_alloc,
2588 according to the KEEP_MEMORY argument. If O has two relocation
2589 sections (both REL and RELA relocations), then the REL_HDR
2590 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2591 RELA_HDR relocations. */
45d6a902
AM
2592
2593Elf_Internal_Rela *
268b6b39
AM
2594_bfd_elf_link_read_relocs (bfd *abfd,
2595 asection *o,
2596 void *external_relocs,
2597 Elf_Internal_Rela *internal_relocs,
2598 bfd_boolean keep_memory)
45d6a902 2599{
268b6b39 2600 void *alloc1 = NULL;
45d6a902 2601 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2602 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2603 struct bfd_elf_section_data *esdo = elf_section_data (o);
2604 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2605
d4730f92
BS
2606 if (esdo->relocs != NULL)
2607 return esdo->relocs;
45d6a902
AM
2608
2609 if (o->reloc_count == 0)
2610 return NULL;
2611
45d6a902
AM
2612 if (internal_relocs == NULL)
2613 {
2614 bfd_size_type size;
2615
056bafd4 2616 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
45d6a902 2617 if (keep_memory)
a50b1753 2618 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2619 else
a50b1753 2620 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2621 if (internal_relocs == NULL)
2622 goto error_return;
2623 }
2624
2625 if (external_relocs == NULL)
2626 {
d4730f92
BS
2627 bfd_size_type size = 0;
2628
2629 if (esdo->rel.hdr)
2630 size += esdo->rel.hdr->sh_size;
2631 if (esdo->rela.hdr)
2632 size += esdo->rela.hdr->sh_size;
45d6a902 2633
268b6b39 2634 alloc1 = bfd_malloc (size);
45d6a902
AM
2635 if (alloc1 == NULL)
2636 goto error_return;
2637 external_relocs = alloc1;
2638 }
2639
d4730f92
BS
2640 internal_rela_relocs = internal_relocs;
2641 if (esdo->rel.hdr)
2642 {
2643 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2644 external_relocs,
2645 internal_relocs))
2646 goto error_return;
2647 external_relocs = (((bfd_byte *) external_relocs)
2648 + esdo->rel.hdr->sh_size);
2649 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2650 * bed->s->int_rels_per_ext_rel);
2651 }
2652
2653 if (esdo->rela.hdr
2654 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2655 external_relocs,
2656 internal_rela_relocs)))
45d6a902
AM
2657 goto error_return;
2658
2659 /* Cache the results for next time, if we can. */
2660 if (keep_memory)
d4730f92 2661 esdo->relocs = internal_relocs;
45d6a902 2662
c9594989 2663 free (alloc1);
45d6a902
AM
2664
2665 /* Don't free alloc2, since if it was allocated we are passing it
2666 back (under the name of internal_relocs). */
2667
2668 return internal_relocs;
2669
2670 error_return:
c9594989 2671 free (alloc1);
45d6a902 2672 if (alloc2 != NULL)
4dd07732
AM
2673 {
2674 if (keep_memory)
2675 bfd_release (abfd, alloc2);
2676 else
2677 free (alloc2);
2678 }
45d6a902
AM
2679 return NULL;
2680}
2681
2682/* Compute the size of, and allocate space for, REL_HDR which is the
2683 section header for a section containing relocations for O. */
2684
28caa186 2685static bfd_boolean
9eaff861
AO
2686_bfd_elf_link_size_reloc_section (bfd *abfd,
2687 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2688{
9eaff861 2689 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2690
2691 /* That allows us to calculate the size of the section. */
9eaff861 2692 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2693
2694 /* The contents field must last into write_object_contents, so we
2695 allocate it with bfd_alloc rather than malloc. Also since we
2696 cannot be sure that the contents will actually be filled in,
2697 we zero the allocated space. */
a50b1753 2698 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2699 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2700 return FALSE;
2701
d4730f92 2702 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2703 {
2704 struct elf_link_hash_entry **p;
2705
ca4be51c
AM
2706 p = ((struct elf_link_hash_entry **)
2707 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902
AM
2708 if (p == NULL)
2709 return FALSE;
2710
d4730f92 2711 reldata->hashes = p;
45d6a902
AM
2712 }
2713
2714 return TRUE;
2715}
2716
2717/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2718 originated from the section given by INPUT_REL_HDR) to the
2719 OUTPUT_BFD. */
2720
2721bfd_boolean
268b6b39
AM
2722_bfd_elf_link_output_relocs (bfd *output_bfd,
2723 asection *input_section,
2724 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2725 Elf_Internal_Rela *internal_relocs,
2726 struct elf_link_hash_entry **rel_hash
2727 ATTRIBUTE_UNUSED)
45d6a902
AM
2728{
2729 Elf_Internal_Rela *irela;
2730 Elf_Internal_Rela *irelaend;
2731 bfd_byte *erel;
d4730f92 2732 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2733 asection *output_section;
9c5bfbb7 2734 const struct elf_backend_data *bed;
268b6b39 2735 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2736 struct bfd_elf_section_data *esdo;
45d6a902
AM
2737
2738 output_section = input_section->output_section;
45d6a902 2739
d4730f92
BS
2740 bed = get_elf_backend_data (output_bfd);
2741 esdo = elf_section_data (output_section);
2742 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2743 {
d4730f92
BS
2744 output_reldata = &esdo->rel;
2745 swap_out = bed->s->swap_reloc_out;
45d6a902 2746 }
d4730f92
BS
2747 else if (esdo->rela.hdr
2748 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2749 {
d4730f92
BS
2750 output_reldata = &esdo->rela;
2751 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2752 }
2753 else
2754 {
4eca0228 2755 _bfd_error_handler
695344c0 2756 /* xgettext:c-format */
871b3ab2 2757 (_("%pB: relocation size mismatch in %pB section %pA"),
d003868e 2758 output_bfd, input_section->owner, input_section);
297d8443 2759 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2760 return FALSE;
2761 }
2762
d4730f92
BS
2763 erel = output_reldata->hdr->contents;
2764 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2765 irela = internal_relocs;
2766 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2767 * bed->s->int_rels_per_ext_rel);
2768 while (irela < irelaend)
2769 {
2770 (*swap_out) (output_bfd, irela, erel);
2771 irela += bed->s->int_rels_per_ext_rel;
2772 erel += input_rel_hdr->sh_entsize;
2773 }
2774
2775 /* Bump the counter, so that we know where to add the next set of
2776 relocations. */
d4730f92 2777 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2778
2779 return TRUE;
2780}
2781\f
508c3946
L
2782/* Make weak undefined symbols in PIE dynamic. */
2783
2784bfd_boolean
2785_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2786 struct elf_link_hash_entry *h)
2787{
0e1862bb 2788 if (bfd_link_pie (info)
508c3946
L
2789 && h->dynindx == -1
2790 && h->root.type == bfd_link_hash_undefweak)
2791 return bfd_elf_link_record_dynamic_symbol (info, h);
2792
2793 return TRUE;
2794}
2795
45d6a902
AM
2796/* Fix up the flags for a symbol. This handles various cases which
2797 can only be fixed after all the input files are seen. This is
2798 currently called by both adjust_dynamic_symbol and
2799 assign_sym_version, which is unnecessary but perhaps more robust in
2800 the face of future changes. */
2801
28caa186 2802static bfd_boolean
268b6b39
AM
2803_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2804 struct elf_info_failed *eif)
45d6a902 2805{
33774f08 2806 const struct elf_backend_data *bed;
508c3946 2807
45d6a902
AM
2808 /* If this symbol was mentioned in a non-ELF file, try to set
2809 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2810 permit a non-ELF file to correctly refer to a symbol defined in
2811 an ELF dynamic object. */
f5385ebf 2812 if (h->non_elf)
45d6a902
AM
2813 {
2814 while (h->root.type == bfd_link_hash_indirect)
2815 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2816
2817 if (h->root.type != bfd_link_hash_defined
2818 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2819 {
2820 h->ref_regular = 1;
2821 h->ref_regular_nonweak = 1;
2822 }
45d6a902
AM
2823 else
2824 {
2825 if (h->root.u.def.section->owner != NULL
2826 && (bfd_get_flavour (h->root.u.def.section->owner)
2827 == bfd_target_elf_flavour))
f5385ebf
AM
2828 {
2829 h->ref_regular = 1;
2830 h->ref_regular_nonweak = 1;
2831 }
45d6a902 2832 else
f5385ebf 2833 h->def_regular = 1;
45d6a902
AM
2834 }
2835
2836 if (h->dynindx == -1
f5385ebf
AM
2837 && (h->def_dynamic
2838 || h->ref_dynamic))
45d6a902 2839 {
c152c796 2840 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2841 {
2842 eif->failed = TRUE;
2843 return FALSE;
2844 }
2845 }
2846 }
2847 else
2848 {
f5385ebf 2849 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2850 was first seen in a non-ELF file. Fortunately, if the symbol
2851 was first seen in an ELF file, we're probably OK unless the
2852 symbol was defined in a non-ELF file. Catch that case here.
2853 FIXME: We're still in trouble if the symbol was first seen in
2854 a dynamic object, and then later in a non-ELF regular object. */
2855 if ((h->root.type == bfd_link_hash_defined
2856 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2857 && !h->def_regular
45d6a902
AM
2858 && (h->root.u.def.section->owner != NULL
2859 ? (bfd_get_flavour (h->root.u.def.section->owner)
2860 != bfd_target_elf_flavour)
2861 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2862 && !h->def_dynamic)))
2863 h->def_regular = 1;
45d6a902
AM
2864 }
2865
508c3946 2866 /* Backend specific symbol fixup. */
33774f08
AM
2867 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2868 if (bed->elf_backend_fixup_symbol
2869 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2870 return FALSE;
508c3946 2871
45d6a902
AM
2872 /* If this is a final link, and the symbol was defined as a common
2873 symbol in a regular object file, and there was no definition in
2874 any dynamic object, then the linker will have allocated space for
f5385ebf 2875 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2876 flag will not have been set. */
2877 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2878 && !h->def_regular
2879 && h->ref_regular
2880 && !h->def_dynamic
96f29d96 2881 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2882 h->def_regular = 1;
45d6a902 2883
af0bfb9c
AM
2884 /* Symbols defined in discarded sections shouldn't be dynamic. */
2885 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
2886 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2887
4deb8f71
L
2888 /* If a weak undefined symbol has non-default visibility, we also
2889 hide it from the dynamic linker. */
af0bfb9c
AM
2890 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2891 && h->root.type == bfd_link_hash_undefweak)
4deb8f71
L
2892 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2893
2894 /* A hidden versioned symbol in executable should be forced local if
2895 it is is locally defined, not referenced by shared library and not
2896 exported. */
2897 else if (bfd_link_executable (eif->info)
2898 && h->versioned == versioned_hidden
2899 && !eif->info->export_dynamic
2900 && !h->dynamic
2901 && !h->ref_dynamic
2902 && h->def_regular)
2903 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2904
45d6a902
AM
2905 /* If -Bsymbolic was used (which means to bind references to global
2906 symbols to the definition within the shared object), and this
2907 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2908 need a PLT entry. Likewise, if the symbol has non-default
2909 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2910 will force it local. */
4deb8f71
L
2911 else if (h->needs_plt
2912 && bfd_link_pic (eif->info)
2913 && is_elf_hash_table (eif->info->hash)
2914 && (SYMBOLIC_BIND (eif->info, h)
2915 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2916 && h->def_regular)
45d6a902 2917 {
45d6a902
AM
2918 bfd_boolean force_local;
2919
45d6a902
AM
2920 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2921 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2922 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2923 }
2924
45d6a902
AM
2925 /* If this is a weak defined symbol in a dynamic object, and we know
2926 the real definition in the dynamic object, copy interesting flags
2927 over to the real definition. */
60d67dc8 2928 if (h->is_weakalias)
45d6a902 2929 {
60d67dc8
AM
2930 struct elf_link_hash_entry *def = weakdef (h);
2931
45d6a902
AM
2932 /* If the real definition is defined by a regular object file,
2933 don't do anything special. See the longer description in
5b9d7a9a
AM
2934 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
2935 bfd_link_hash_defined as it was when put on the alias list
2936 then it must have originally been a versioned symbol (for
2937 which a non-versioned indirect symbol is created) and later
2938 a definition for the non-versioned symbol is found. In that
2939 case the indirection is flipped with the versioned symbol
2940 becoming an indirect pointing at the non-versioned symbol.
2941 Thus, not an alias any more. */
2942 if (def->def_regular
2943 || def->root.type != bfd_link_hash_defined)
60d67dc8
AM
2944 {
2945 h = def;
2946 while ((h = h->u.alias) != def)
2947 h->is_weakalias = 0;
2948 }
45d6a902 2949 else
a26587ba 2950 {
4e6b54a6
AM
2951 while (h->root.type == bfd_link_hash_indirect)
2952 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4e6b54a6
AM
2953 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2954 || h->root.type == bfd_link_hash_defweak);
60d67dc8 2955 BFD_ASSERT (def->def_dynamic);
60d67dc8 2956 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
a26587ba 2957 }
45d6a902
AM
2958 }
2959
2960 return TRUE;
2961}
2962
2963/* Make the backend pick a good value for a dynamic symbol. This is
2964 called via elf_link_hash_traverse, and also calls itself
2965 recursively. */
2966
28caa186 2967static bfd_boolean
268b6b39 2968_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2969{
a50b1753 2970 struct elf_info_failed *eif = (struct elf_info_failed *) data;
559192d8 2971 struct elf_link_hash_table *htab;
9c5bfbb7 2972 const struct elf_backend_data *bed;
45d6a902 2973
0eddce27 2974 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
2975 return FALSE;
2976
45d6a902
AM
2977 /* Ignore indirect symbols. These are added by the versioning code. */
2978 if (h->root.type == bfd_link_hash_indirect)
2979 return TRUE;
2980
2981 /* Fix the symbol flags. */
2982 if (! _bfd_elf_fix_symbol_flags (h, eif))
2983 return FALSE;
2984
559192d8
AM
2985 htab = elf_hash_table (eif->info);
2986 bed = get_elf_backend_data (htab->dynobj);
2987
954b63d4
AM
2988 if (h->root.type == bfd_link_hash_undefweak)
2989 {
2990 if (eif->info->dynamic_undefined_weak == 0)
559192d8 2991 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
954b63d4
AM
2992 else if (eif->info->dynamic_undefined_weak > 0
2993 && h->ref_regular
2994 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2995 && !bfd_hide_sym_by_version (eif->info->version_info,
2996 h->root.root.string))
2997 {
2998 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2999 {
3000 eif->failed = TRUE;
3001 return FALSE;
3002 }
3003 }
3004 }
3005
45d6a902
AM
3006 /* If this symbol does not require a PLT entry, and it is not
3007 defined by a dynamic object, or is not referenced by a regular
3008 object, ignore it. We do have to handle a weak defined symbol,
3009 even if no regular object refers to it, if we decided to add it
3010 to the dynamic symbol table. FIXME: Do we normally need to worry
3011 about symbols which are defined by one dynamic object and
3012 referenced by another one? */
f5385ebf 3013 if (!h->needs_plt
91e21fb7 3014 && h->type != STT_GNU_IFUNC
f5385ebf
AM
3015 && (h->def_regular
3016 || !h->def_dynamic
3017 || (!h->ref_regular
60d67dc8 3018 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
45d6a902 3019 {
a6aa5195 3020 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
3021 return TRUE;
3022 }
3023
3024 /* If we've already adjusted this symbol, don't do it again. This
3025 can happen via a recursive call. */
f5385ebf 3026 if (h->dynamic_adjusted)
45d6a902
AM
3027 return TRUE;
3028
3029 /* Don't look at this symbol again. Note that we must set this
3030 after checking the above conditions, because we may look at a
3031 symbol once, decide not to do anything, and then get called
3032 recursively later after REF_REGULAR is set below. */
f5385ebf 3033 h->dynamic_adjusted = 1;
45d6a902
AM
3034
3035 /* If this is a weak definition, and we know a real definition, and
3036 the real symbol is not itself defined by a regular object file,
3037 then get a good value for the real definition. We handle the
3038 real symbol first, for the convenience of the backend routine.
3039
3040 Note that there is a confusing case here. If the real definition
3041 is defined by a regular object file, we don't get the real symbol
3042 from the dynamic object, but we do get the weak symbol. If the
3043 processor backend uses a COPY reloc, then if some routine in the
3044 dynamic object changes the real symbol, we will not see that
3045 change in the corresponding weak symbol. This is the way other
3046 ELF linkers work as well, and seems to be a result of the shared
3047 library model.
3048
3049 I will clarify this issue. Most SVR4 shared libraries define the
3050 variable _timezone and define timezone as a weak synonym. The
3051 tzset call changes _timezone. If you write
3052 extern int timezone;
3053 int _timezone = 5;
3054 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3055 you might expect that, since timezone is a synonym for _timezone,
3056 the same number will print both times. However, if the processor
3057 backend uses a COPY reloc, then actually timezone will be copied
3058 into your process image, and, since you define _timezone
3059 yourself, _timezone will not. Thus timezone and _timezone will
3060 wind up at different memory locations. The tzset call will set
3061 _timezone, leaving timezone unchanged. */
3062
60d67dc8 3063 if (h->is_weakalias)
45d6a902 3064 {
60d67dc8
AM
3065 struct elf_link_hash_entry *def = weakdef (h);
3066
ec24dc88 3067 /* If we get to this point, there is an implicit reference to
60d67dc8
AM
3068 the alias by a regular object file via the weak symbol H. */
3069 def->ref_regular = 1;
45d6a902 3070
ec24dc88 3071 /* Ensure that the backend adjust_dynamic_symbol function sees
60d67dc8
AM
3072 the strong alias before H by recursively calling ourselves. */
3073 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
45d6a902
AM
3074 return FALSE;
3075 }
3076
3077 /* If a symbol has no type and no size and does not require a PLT
3078 entry, then we are probably about to do the wrong thing here: we
3079 are probably going to create a COPY reloc for an empty object.
3080 This case can arise when a shared object is built with assembly
3081 code, and the assembly code fails to set the symbol type. */
3082 if (h->size == 0
3083 && h->type == STT_NOTYPE
f5385ebf 3084 && !h->needs_plt)
4eca0228 3085 _bfd_error_handler
45d6a902
AM
3086 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3087 h->root.root.string);
3088
45d6a902
AM
3089 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3090 {
3091 eif->failed = TRUE;
3092 return FALSE;
3093 }
3094
3095 return TRUE;
3096}
3097
027297b7
L
3098/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3099 DYNBSS. */
3100
3101bfd_boolean
6cabe1ea
AM
3102_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3103 struct elf_link_hash_entry *h,
027297b7
L
3104 asection *dynbss)
3105{
91ac5911 3106 unsigned int power_of_two;
027297b7
L
3107 bfd_vma mask;
3108 asection *sec = h->root.u.def.section;
3109
de194d85 3110 /* The section alignment of the definition is the maximum alignment
91ac5911
L
3111 requirement of symbols defined in the section. Since we don't
3112 know the symbol alignment requirement, we start with the
3113 maximum alignment and check low bits of the symbol address
3114 for the minimum alignment. */
fd361982 3115 power_of_two = bfd_section_alignment (sec);
91ac5911
L
3116 mask = ((bfd_vma) 1 << power_of_two) - 1;
3117 while ((h->root.u.def.value & mask) != 0)
3118 {
3119 mask >>= 1;
3120 --power_of_two;
3121 }
027297b7 3122
fd361982 3123 if (power_of_two > bfd_section_alignment (dynbss))
027297b7
L
3124 {
3125 /* Adjust the section alignment if needed. */
fd361982 3126 if (!bfd_set_section_alignment (dynbss, power_of_two))
027297b7
L
3127 return FALSE;
3128 }
3129
91ac5911 3130 /* We make sure that the symbol will be aligned properly. */
027297b7
L
3131 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3132
3133 /* Define the symbol as being at this point in DYNBSS. */
3134 h->root.u.def.section = dynbss;
3135 h->root.u.def.value = dynbss->size;
3136
3137 /* Increment the size of DYNBSS to make room for the symbol. */
3138 dynbss->size += h->size;
3139
f7483970
L
3140 /* No error if extern_protected_data is true. */
3141 if (h->protected_def
889c2a67
L
3142 && (!info->extern_protected_data
3143 || (info->extern_protected_data < 0
3144 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05 3145 info->callbacks->einfo
c1c8c1ef 3146 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
d07a1b05 3147 h->root.root.string);
6cabe1ea 3148
027297b7
L
3149 return TRUE;
3150}
3151
45d6a902
AM
3152/* Adjust all external symbols pointing into SEC_MERGE sections
3153 to reflect the object merging within the sections. */
3154
28caa186 3155static bfd_boolean
268b6b39 3156_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
3157{
3158 asection *sec;
3159
45d6a902
AM
3160 if ((h->root.type == bfd_link_hash_defined
3161 || h->root.type == bfd_link_hash_defweak)
3162 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 3163 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 3164 {
a50b1753 3165 bfd *output_bfd = (bfd *) data;
45d6a902
AM
3166
3167 h->root.u.def.value =
3168 _bfd_merged_section_offset (output_bfd,
3169 &h->root.u.def.section,
3170 elf_section_data (sec)->sec_info,
753731ee 3171 h->root.u.def.value);
45d6a902
AM
3172 }
3173
3174 return TRUE;
3175}
986a241f
RH
3176
3177/* Returns false if the symbol referred to by H should be considered
3178 to resolve local to the current module, and true if it should be
3179 considered to bind dynamically. */
3180
3181bfd_boolean
268b6b39
AM
3182_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3183 struct bfd_link_info *info,
89a2ee5a 3184 bfd_boolean not_local_protected)
986a241f
RH
3185{
3186 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
3187 const struct elf_backend_data *bed;
3188 struct elf_link_hash_table *hash_table;
986a241f
RH
3189
3190 if (h == NULL)
3191 return FALSE;
3192
3193 while (h->root.type == bfd_link_hash_indirect
3194 || h->root.type == bfd_link_hash_warning)
3195 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3196
3197 /* If it was forced local, then clearly it's not dynamic. */
3198 if (h->dynindx == -1)
3199 return FALSE;
f5385ebf 3200 if (h->forced_local)
986a241f
RH
3201 return FALSE;
3202
3203 /* Identify the cases where name binding rules say that a
3204 visible symbol resolves locally. */
0e1862bb
L
3205 binding_stays_local_p = (bfd_link_executable (info)
3206 || SYMBOLIC_BIND (info, h));
986a241f
RH
3207
3208 switch (ELF_ST_VISIBILITY (h->other))
3209 {
3210 case STV_INTERNAL:
3211 case STV_HIDDEN:
3212 return FALSE;
3213
3214 case STV_PROTECTED:
fcb93ecf
PB
3215 hash_table = elf_hash_table (info);
3216 if (!is_elf_hash_table (hash_table))
3217 return FALSE;
3218
3219 bed = get_elf_backend_data (hash_table->dynobj);
3220
986a241f
RH
3221 /* Proper resolution for function pointer equality may require
3222 that these symbols perhaps be resolved dynamically, even though
3223 we should be resolving them to the current module. */
89a2ee5a 3224 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
3225 binding_stays_local_p = TRUE;
3226 break;
3227
3228 default:
986a241f
RH
3229 break;
3230 }
3231
aa37626c 3232 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 3233 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
3234 return TRUE;
3235
986a241f
RH
3236 /* Otherwise, the symbol is dynamic if binding rules don't tell
3237 us that it remains local. */
3238 return !binding_stays_local_p;
3239}
f6c52c13
AM
3240
3241/* Return true if the symbol referred to by H should be considered
3242 to resolve local to the current module, and false otherwise. Differs
3243 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 3244 undefined symbols. The two functions are virtually identical except
0fad2956
MR
3245 for the place where dynindx == -1 is tested. If that test is true,
3246 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3247 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3248 defined symbols.
89a2ee5a
AM
3249 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3250 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3251 treatment of undefined weak symbols. For those that do not make
3252 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
3253
3254bfd_boolean
268b6b39
AM
3255_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3256 struct bfd_link_info *info,
3257 bfd_boolean local_protected)
f6c52c13 3258{
fcb93ecf
PB
3259 const struct elf_backend_data *bed;
3260 struct elf_link_hash_table *hash_table;
3261
f6c52c13
AM
3262 /* If it's a local sym, of course we resolve locally. */
3263 if (h == NULL)
3264 return TRUE;
3265
d95edcac
L
3266 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3267 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3268 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3269 return TRUE;
3270
0fad2956
MR
3271 /* Forced local symbols resolve locally. */
3272 if (h->forced_local)
3273 return TRUE;
3274
7e2294f9
AO
3275 /* Common symbols that become definitions don't get the DEF_REGULAR
3276 flag set, so test it first, and don't bail out. */
3277 if (ELF_COMMON_DEF_P (h))
3278 /* Do nothing. */;
f6c52c13 3279 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
3280 resolve locally. The sym is either undefined or dynamic. */
3281 else if (!h->def_regular)
f6c52c13
AM
3282 return FALSE;
3283
0fad2956 3284 /* Non-dynamic symbols resolve locally. */
f6c52c13
AM
3285 if (h->dynindx == -1)
3286 return TRUE;
3287
3288 /* At this point, we know the symbol is defined and dynamic. In an
3289 executable it must resolve locally, likewise when building symbolic
3290 shared libraries. */
0e1862bb 3291 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
f6c52c13
AM
3292 return TRUE;
3293
3294 /* Now deal with defined dynamic symbols in shared libraries. Ones
3295 with default visibility might not resolve locally. */
3296 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3297 return FALSE;
3298
fcb93ecf
PB
3299 hash_table = elf_hash_table (info);
3300 if (!is_elf_hash_table (hash_table))
3301 return TRUE;
3302
3303 bed = get_elf_backend_data (hash_table->dynobj);
3304
f7483970
L
3305 /* If extern_protected_data is false, STV_PROTECTED non-function
3306 symbols are local. */
889c2a67
L
3307 if ((!info->extern_protected_data
3308 || (info->extern_protected_data < 0
3309 && !bed->extern_protected_data))
3310 && !bed->is_function_type (h->type))
1c16dfa5
L
3311 return TRUE;
3312
f6c52c13 3313 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
3314 symbols be treated as dynamic symbols. If the address of a
3315 function not defined in an executable is set to that function's
3316 plt entry in the executable, then the address of the function in
3317 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
3318 return local_protected;
3319}
e1918d23
AM
3320
3321/* Caches some TLS segment info, and ensures that the TLS segment vma is
3322 aligned. Returns the first TLS output section. */
3323
3324struct bfd_section *
3325_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3326{
3327 struct bfd_section *sec, *tls;
3328 unsigned int align = 0;
3329
3330 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3331 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3332 break;
3333 tls = sec;
3334
3335 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3336 if (sec->alignment_power > align)
3337 align = sec->alignment_power;
3338
3339 elf_hash_table (info)->tls_sec = tls;
3340
fdde2fb6
SH
3341 /* Ensure the alignment of the first section (usually .tdata) is the largest
3342 alignment, so that the tls segment starts aligned. */
e1918d23
AM
3343 if (tls != NULL)
3344 tls->alignment_power = align;
3345
3346 return tls;
3347}
0ad989f9
L
3348
3349/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3350static bfd_boolean
3351is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3352 Elf_Internal_Sym *sym)
3353{
a4d8e49b
L
3354 const struct elf_backend_data *bed;
3355
0ad989f9
L
3356 /* Local symbols do not count, but target specific ones might. */
3357 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3358 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3359 return FALSE;
3360
fcb93ecf 3361 bed = get_elf_backend_data (abfd);
0ad989f9 3362 /* Function symbols do not count. */
fcb93ecf 3363 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
3364 return FALSE;
3365
3366 /* If the section is undefined, then so is the symbol. */
3367 if (sym->st_shndx == SHN_UNDEF)
3368 return FALSE;
3369
3370 /* If the symbol is defined in the common section, then
3371 it is a common definition and so does not count. */
a4d8e49b 3372 if (bed->common_definition (sym))
0ad989f9
L
3373 return FALSE;
3374
3375 /* If the symbol is in a target specific section then we
3376 must rely upon the backend to tell us what it is. */
3377 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3378 /* FIXME - this function is not coded yet:
3379
3380 return _bfd_is_global_symbol_definition (abfd, sym);
3381
3382 Instead for now assume that the definition is not global,
3383 Even if this is wrong, at least the linker will behave
3384 in the same way that it used to do. */
3385 return FALSE;
3386
3387 return TRUE;
3388}
3389
3390/* Search the symbol table of the archive element of the archive ABFD
3391 whose archive map contains a mention of SYMDEF, and determine if
3392 the symbol is defined in this element. */
3393static bfd_boolean
3394elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3395{
3396 Elf_Internal_Shdr * hdr;
ef53be89
AM
3397 size_t symcount;
3398 size_t extsymcount;
3399 size_t extsymoff;
0ad989f9
L
3400 Elf_Internal_Sym *isymbuf;
3401 Elf_Internal_Sym *isym;
3402 Elf_Internal_Sym *isymend;
3403 bfd_boolean result;
3404
3405 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3406 if (abfd == NULL)
3407 return FALSE;
3408
3409 if (! bfd_check_format (abfd, bfd_object))
3410 return FALSE;
3411
7dc3990e
L
3412 /* Select the appropriate symbol table. If we don't know if the
3413 object file is an IR object, give linker LTO plugin a chance to
3414 get the correct symbol table. */
3415 if (abfd->plugin_format == bfd_plugin_yes
08ce1d72 3416#if BFD_SUPPORTS_PLUGINS
7dc3990e
L
3417 || (abfd->plugin_format == bfd_plugin_unknown
3418 && bfd_link_plugin_object_p (abfd))
3419#endif
3420 )
3421 {
3422 /* Use the IR symbol table if the object has been claimed by
3423 plugin. */
3424 abfd = abfd->plugin_dummy_bfd;
3425 hdr = &elf_tdata (abfd)->symtab_hdr;
3426 }
3427 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
0ad989f9
L
3428 hdr = &elf_tdata (abfd)->symtab_hdr;
3429 else
3430 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3431
3432 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3433
3434 /* The sh_info field of the symtab header tells us where the
3435 external symbols start. We don't care about the local symbols. */
3436 if (elf_bad_symtab (abfd))
3437 {
3438 extsymcount = symcount;
3439 extsymoff = 0;
3440 }
3441 else
3442 {
3443 extsymcount = symcount - hdr->sh_info;
3444 extsymoff = hdr->sh_info;
3445 }
3446
3447 if (extsymcount == 0)
3448 return FALSE;
3449
3450 /* Read in the symbol table. */
3451 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3452 NULL, NULL, NULL);
3453 if (isymbuf == NULL)
3454 return FALSE;
3455
3456 /* Scan the symbol table looking for SYMDEF. */
3457 result = FALSE;
3458 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3459 {
3460 const char *name;
3461
3462 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3463 isym->st_name);
3464 if (name == NULL)
3465 break;
3466
3467 if (strcmp (name, symdef->name) == 0)
3468 {
3469 result = is_global_data_symbol_definition (abfd, isym);
3470 break;
3471 }
3472 }
3473
3474 free (isymbuf);
3475
3476 return result;
3477}
3478\f
5a580b3a
AM
3479/* Add an entry to the .dynamic table. */
3480
3481bfd_boolean
3482_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3483 bfd_vma tag,
3484 bfd_vma val)
3485{
3486 struct elf_link_hash_table *hash_table;
3487 const struct elf_backend_data *bed;
3488 asection *s;
3489 bfd_size_type newsize;
3490 bfd_byte *newcontents;
3491 Elf_Internal_Dyn dyn;
3492
3493 hash_table = elf_hash_table (info);
3494 if (! is_elf_hash_table (hash_table))
3495 return FALSE;
3496
7f923b7f
AM
3497 if (tag == DT_RELA || tag == DT_REL)
3498 hash_table->dynamic_relocs = TRUE;
3499
5a580b3a 3500 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3501 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3502 BFD_ASSERT (s != NULL);
3503
eea6121a 3504 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3505 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3506 if (newcontents == NULL)
3507 return FALSE;
3508
3509 dyn.d_tag = tag;
3510 dyn.d_un.d_val = val;
eea6121a 3511 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3512
eea6121a 3513 s->size = newsize;
5a580b3a
AM
3514 s->contents = newcontents;
3515
3516 return TRUE;
3517}
3518
6f6fd151
L
3519/* Strip zero-sized dynamic sections. */
3520
3521bfd_boolean
3522_bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3523{
3524 struct elf_link_hash_table *hash_table;
3525 const struct elf_backend_data *bed;
3526 asection *s, *sdynamic, **pp;
3527 asection *rela_dyn, *rel_dyn;
3528 Elf_Internal_Dyn dyn;
3529 bfd_byte *extdyn, *next;
3530 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3531 bfd_boolean strip_zero_sized;
3532 bfd_boolean strip_zero_sized_plt;
3533
3534 if (bfd_link_relocatable (info))
3535 return TRUE;
3536
3537 hash_table = elf_hash_table (info);
3538 if (!is_elf_hash_table (hash_table))
3539 return FALSE;
3540
3541 if (!hash_table->dynobj)
3542 return TRUE;
3543
3544 sdynamic= bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3545 if (!sdynamic)
3546 return TRUE;
3547
3548 bed = get_elf_backend_data (hash_table->dynobj);
3549 swap_dyn_in = bed->s->swap_dyn_in;
3550
3551 strip_zero_sized = FALSE;
3552 strip_zero_sized_plt = FALSE;
3553
3554 /* Strip zero-sized dynamic sections. */
3555 rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3556 rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3557 for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3558 if (s->size == 0
3559 && (s == rela_dyn
3560 || s == rel_dyn
3561 || s == hash_table->srelplt->output_section
3562 || s == hash_table->splt->output_section))
3563 {
3564 *pp = s->next;
3565 info->output_bfd->section_count--;
3566 strip_zero_sized = TRUE;
3567 if (s == rela_dyn)
3568 s = rela_dyn;
3569 if (s == rel_dyn)
3570 s = rel_dyn;
3571 else if (s == hash_table->splt->output_section)
3572 {
3573 s = hash_table->splt;
3574 strip_zero_sized_plt = TRUE;
3575 }
3576 else
3577 s = hash_table->srelplt;
3578 s->flags |= SEC_EXCLUDE;
3579 s->output_section = bfd_abs_section_ptr;
3580 }
3581 else
3582 pp = &s->next;
3583
3584 if (strip_zero_sized_plt)
3585 for (extdyn = sdynamic->contents;
3586 extdyn < sdynamic->contents + sdynamic->size;
3587 extdyn = next)
3588 {
3589 next = extdyn + bed->s->sizeof_dyn;
3590 swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3591 switch (dyn.d_tag)
3592 {
3593 default:
3594 break;
3595 case DT_JMPREL:
3596 case DT_PLTRELSZ:
3597 case DT_PLTREL:
3598 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3599 the procedure linkage table (the .plt section) has been
3600 removed. */
3601 memmove (extdyn, next,
3602 sdynamic->size - (next - sdynamic->contents));
3603 next = extdyn;
3604 }
3605 }
3606
3607 if (strip_zero_sized)
3608 {
3609 /* Regenerate program headers. */
3610 elf_seg_map (info->output_bfd) = NULL;
3611 return _bfd_elf_map_sections_to_segments (info->output_bfd, info);
3612 }
3613
3614 return TRUE;
3615}
3616
e310298c 3617/* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
5a580b3a
AM
3618 1 if a DT_NEEDED tag already exists, and 0 on success. */
3619
e310298c
AM
3620int
3621bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
5a580b3a
AM
3622{
3623 struct elf_link_hash_table *hash_table;
ef53be89 3624 size_t strindex;
e310298c 3625 const char *soname;
5a580b3a 3626
7e9f0867
AM
3627 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3628 return -1;
3629
5a580b3a 3630 hash_table = elf_hash_table (info);
e310298c 3631 soname = elf_dt_name (abfd);
5a580b3a 3632 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
ef53be89 3633 if (strindex == (size_t) -1)
5a580b3a
AM
3634 return -1;
3635
02be4619 3636 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3637 {
3638 asection *sdyn;
3639 const struct elf_backend_data *bed;
3640 bfd_byte *extdyn;
3641
3642 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3643 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3644 if (sdyn != NULL)
3645 for (extdyn = sdyn->contents;
3646 extdyn < sdyn->contents + sdyn->size;
3647 extdyn += bed->s->sizeof_dyn)
3648 {
3649 Elf_Internal_Dyn dyn;
5a580b3a 3650
7e9f0867
AM
3651 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3652 if (dyn.d_tag == DT_NEEDED
3653 && dyn.d_un.d_val == strindex)
3654 {
3655 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3656 return 1;
3657 }
3658 }
5a580b3a
AM
3659 }
3660
e310298c
AM
3661 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3662 return -1;
7e9f0867 3663
e310298c
AM
3664 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3665 return -1;
5a580b3a
AM
3666
3667 return 0;
3668}
3669
7b15fa7a
AM
3670/* Return true if SONAME is on the needed list between NEEDED and STOP
3671 (or the end of list if STOP is NULL), and needed by a library that
3672 will be loaded. */
3673
010e5ae2 3674static bfd_boolean
7b15fa7a
AM
3675on_needed_list (const char *soname,
3676 struct bfd_link_needed_list *needed,
3677 struct bfd_link_needed_list *stop)
010e5ae2 3678{
7b15fa7a
AM
3679 struct bfd_link_needed_list *look;
3680 for (look = needed; look != stop; look = look->next)
3681 if (strcmp (soname, look->name) == 0
3682 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3683 /* If needed by a library that itself is not directly
3684 needed, recursively check whether that library is
3685 indirectly needed. Since we add DT_NEEDED entries to
3686 the end of the list, library dependencies appear after
3687 the library. Therefore search prior to the current
3688 LOOK, preventing possible infinite recursion. */
3689 || on_needed_list (elf_dt_name (look->by), needed, look)))
010e5ae2
AM
3690 return TRUE;
3691
3692 return FALSE;
3693}
3694
3a3f4bf7 3695/* Sort symbol by value, section, size, and type. */
4ad4eba5
AM
3696static int
3697elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3698{
3699 const struct elf_link_hash_entry *h1;
3700 const struct elf_link_hash_entry *h2;
10b7e05b 3701 bfd_signed_vma vdiff;
3a3f4bf7
AM
3702 int sdiff;
3703 const char *n1;
3704 const char *n2;
5a580b3a
AM
3705
3706 h1 = *(const struct elf_link_hash_entry **) arg1;
3707 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3708 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3709 if (vdiff != 0)
3710 return vdiff > 0 ? 1 : -1;
3a3f4bf7
AM
3711
3712 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3713 if (sdiff != 0)
3714 return sdiff;
3715
3716 /* Sort so that sized symbols are selected over zero size symbols. */
3717 vdiff = h1->size - h2->size;
3718 if (vdiff != 0)
3719 return vdiff > 0 ? 1 : -1;
3720
3721 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3722 if (h1->type != h2->type)
3723 return h1->type - h2->type;
3724
3725 /* If symbols are properly sized and typed, and multiple strong
3726 aliases are not defined in a shared library by the user we
3727 shouldn't get here. Unfortunately linker script symbols like
3728 __bss_start sometimes match a user symbol defined at the start of
3729 .bss without proper size and type. We'd like to preference the
3730 user symbol over reserved system symbols. Sort on leading
3731 underscores. */
3732 n1 = h1->root.root.string;
3733 n2 = h2->root.root.string;
3734 while (*n1 == *n2)
10b7e05b 3735 {
3a3f4bf7
AM
3736 if (*n1 == 0)
3737 break;
3738 ++n1;
3739 ++n2;
10b7e05b 3740 }
3a3f4bf7
AM
3741 if (*n1 == '_')
3742 return -1;
3743 if (*n2 == '_')
3744 return 1;
3745
3746 /* Final sort on name selects user symbols like '_u' over reserved
3747 system symbols like '_Z' and also will avoid qsort instability. */
3748 return *n1 - *n2;
5a580b3a 3749}
4ad4eba5 3750
5a580b3a
AM
3751/* This function is used to adjust offsets into .dynstr for
3752 dynamic symbols. This is called via elf_link_hash_traverse. */
3753
3754static bfd_boolean
3755elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3756{
a50b1753 3757 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3758
5a580b3a
AM
3759 if (h->dynindx != -1)
3760 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3761 return TRUE;
3762}
3763
3764/* Assign string offsets in .dynstr, update all structures referencing
3765 them. */
3766
4ad4eba5
AM
3767static bfd_boolean
3768elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3769{
3770 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3771 struct elf_link_local_dynamic_entry *entry;
3772 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3773 bfd *dynobj = hash_table->dynobj;
3774 asection *sdyn;
3775 bfd_size_type size;
3776 const struct elf_backend_data *bed;
3777 bfd_byte *extdyn;
3778
3779 _bfd_elf_strtab_finalize (dynstr);
3780 size = _bfd_elf_strtab_size (dynstr);
3781
3d16b64e
NA
3782 /* Allow the linker to examine the dynsymtab now it's fully populated. */
3783
3784 if (info->callbacks->examine_strtab)
3785 info->callbacks->examine_strtab (dynstr);
3786
5a580b3a 3787 bed = get_elf_backend_data (dynobj);
3d4d4302 3788 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3789 BFD_ASSERT (sdyn != NULL);
3790
3791 /* Update all .dynamic entries referencing .dynstr strings. */
3792 for (extdyn = sdyn->contents;
eea6121a 3793 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3794 extdyn += bed->s->sizeof_dyn)
3795 {
3796 Elf_Internal_Dyn dyn;
3797
3798 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3799 switch (dyn.d_tag)
3800 {
3801 case DT_STRSZ:
3802 dyn.d_un.d_val = size;
3803 break;
3804 case DT_NEEDED:
3805 case DT_SONAME:
3806 case DT_RPATH:
3807 case DT_RUNPATH:
3808 case DT_FILTER:
3809 case DT_AUXILIARY:
7ee314fa
AM
3810 case DT_AUDIT:
3811 case DT_DEPAUDIT:
5a580b3a
AM
3812 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3813 break;
3814 default:
3815 continue;
3816 }
3817 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3818 }
3819
3820 /* Now update local dynamic symbols. */
3821 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3822 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3823 entry->isym.st_name);
3824
3825 /* And the rest of dynamic symbols. */
3826 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3827
3828 /* Adjust version definitions. */
3829 if (elf_tdata (output_bfd)->cverdefs)
3830 {
3831 asection *s;
3832 bfd_byte *p;
ef53be89 3833 size_t i;
5a580b3a
AM
3834 Elf_Internal_Verdef def;
3835 Elf_Internal_Verdaux defaux;
3836
3d4d4302 3837 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3838 p = s->contents;
3839 do
3840 {
3841 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3842 &def);
3843 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3844 if (def.vd_aux != sizeof (Elf_External_Verdef))
3845 continue;
5a580b3a
AM
3846 for (i = 0; i < def.vd_cnt; ++i)
3847 {
3848 _bfd_elf_swap_verdaux_in (output_bfd,
3849 (Elf_External_Verdaux *) p, &defaux);
3850 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3851 defaux.vda_name);
3852 _bfd_elf_swap_verdaux_out (output_bfd,
3853 &defaux, (Elf_External_Verdaux *) p);
3854 p += sizeof (Elf_External_Verdaux);
3855 }
3856 }
3857 while (def.vd_next);
3858 }
3859
3860 /* Adjust version references. */
3861 if (elf_tdata (output_bfd)->verref)
3862 {
3863 asection *s;
3864 bfd_byte *p;
ef53be89 3865 size_t i;
5a580b3a
AM
3866 Elf_Internal_Verneed need;
3867 Elf_Internal_Vernaux needaux;
3868
3d4d4302 3869 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3870 p = s->contents;
3871 do
3872 {
3873 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3874 &need);
3875 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3876 _bfd_elf_swap_verneed_out (output_bfd, &need,
3877 (Elf_External_Verneed *) p);
3878 p += sizeof (Elf_External_Verneed);
3879 for (i = 0; i < need.vn_cnt; ++i)
3880 {
3881 _bfd_elf_swap_vernaux_in (output_bfd,
3882 (Elf_External_Vernaux *) p, &needaux);
3883 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3884 needaux.vna_name);
3885 _bfd_elf_swap_vernaux_out (output_bfd,
3886 &needaux,
3887 (Elf_External_Vernaux *) p);
3888 p += sizeof (Elf_External_Vernaux);
3889 }
3890 }
3891 while (need.vn_next);
3892 }
3893
3894 return TRUE;
3895}
3896\f
13285a1b
AM
3897/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3898 The default is to only match when the INPUT and OUTPUT are exactly
3899 the same target. */
3900
3901bfd_boolean
3902_bfd_elf_default_relocs_compatible (const bfd_target *input,
3903 const bfd_target *output)
3904{
3905 return input == output;
3906}
3907
3908/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3909 This version is used when different targets for the same architecture
3910 are virtually identical. */
3911
3912bfd_boolean
3913_bfd_elf_relocs_compatible (const bfd_target *input,
3914 const bfd_target *output)
3915{
3916 const struct elf_backend_data *obed, *ibed;
3917
3918 if (input == output)
3919 return TRUE;
3920
3921 ibed = xvec_get_elf_backend_data (input);
3922 obed = xvec_get_elf_backend_data (output);
3923
3924 if (ibed->arch != obed->arch)
3925 return FALSE;
3926
3927 /* If both backends are using this function, deem them compatible. */
3928 return ibed->relocs_compatible == obed->relocs_compatible;
3929}
3930
e5034e59
AM
3931/* Make a special call to the linker "notice" function to tell it that
3932 we are about to handle an as-needed lib, or have finished
1b786873 3933 processing the lib. */
e5034e59
AM
3934
3935bfd_boolean
3936_bfd_elf_notice_as_needed (bfd *ibfd,
3937 struct bfd_link_info *info,
3938 enum notice_asneeded_action act)
3939{
46135103 3940 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3941}
3942
d9689752
L
3943/* Check relocations an ELF object file. */
3944
3945bfd_boolean
3946_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3947{
3948 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3949 struct elf_link_hash_table *htab = elf_hash_table (info);
3950
3951 /* If this object is the same format as the output object, and it is
3952 not a shared library, then let the backend look through the
3953 relocs.
3954
3955 This is required to build global offset table entries and to
3956 arrange for dynamic relocs. It is not required for the
3957 particular common case of linking non PIC code, even when linking
3958 against shared libraries, but unfortunately there is no way of
3959 knowing whether an object file has been compiled PIC or not.
3960 Looking through the relocs is not particularly time consuming.
3961 The problem is that we must either (1) keep the relocs in memory,
3962 which causes the linker to require additional runtime memory or
3963 (2) read the relocs twice from the input file, which wastes time.
3964 This would be a good case for using mmap.
3965
3966 I have no idea how to handle linking PIC code into a file of a
3967 different format. It probably can't be done. */
3968 if ((abfd->flags & DYNAMIC) == 0
3969 && is_elf_hash_table (htab)
3970 && bed->check_relocs != NULL
3971 && elf_object_id (abfd) == elf_hash_table_id (htab)
3972 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3973 {
3974 asection *o;
3975
3976 for (o = abfd->sections; o != NULL; o = o->next)
3977 {
3978 Elf_Internal_Rela *internal_relocs;
3979 bfd_boolean ok;
3980
c4b126b8
L
3981 /* Don't check relocations in excluded sections. Don't do
3982 anything special with non-loaded, non-alloced sections.
3983 In particular, any relocs in such sections should not
3984 affect GOT and PLT reference counting (ie. we don't
3985 allow them to create GOT or PLT entries), there's no
3986 possibility or desire to optimize TLS relocs, and
3987 there's not much point in propagating relocs to shared
3988 libs that the dynamic linker won't relocate. */
3989 if ((o->flags & SEC_ALLOC) == 0
3990 || (o->flags & SEC_RELOC) == 0
5ce03cea 3991 || (o->flags & SEC_EXCLUDE) != 0
d9689752
L
3992 || o->reloc_count == 0
3993 || ((info->strip == strip_all || info->strip == strip_debugger)
3994 && (o->flags & SEC_DEBUGGING) != 0)
3995 || bfd_is_abs_section (o->output_section))
3996 continue;
3997
3998 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3999 info->keep_memory);
4000 if (internal_relocs == NULL)
4001 return FALSE;
4002
4003 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4004
4005 if (elf_section_data (o)->relocs != internal_relocs)
4006 free (internal_relocs);
4007
4008 if (! ok)
4009 return FALSE;
4010 }
4011 }
4012
4013 return TRUE;
4014}
4015
4ad4eba5
AM
4016/* Add symbols from an ELF object file to the linker hash table. */
4017
4018static bfd_boolean
4019elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4020{
a0c402a5 4021 Elf_Internal_Ehdr *ehdr;
4ad4eba5 4022 Elf_Internal_Shdr *hdr;
ef53be89
AM
4023 size_t symcount;
4024 size_t extsymcount;
4025 size_t extsymoff;
4ad4eba5
AM
4026 struct elf_link_hash_entry **sym_hash;
4027 bfd_boolean dynamic;
4028 Elf_External_Versym *extversym = NULL;
be22c732 4029 Elf_External_Versym *extversym_end = NULL;
4ad4eba5
AM
4030 Elf_External_Versym *ever;
4031 struct elf_link_hash_entry *weaks;
4032 struct elf_link_hash_entry **nondeflt_vers = NULL;
ef53be89 4033 size_t nondeflt_vers_cnt = 0;
4ad4eba5
AM
4034 Elf_Internal_Sym *isymbuf = NULL;
4035 Elf_Internal_Sym *isym;
4036 Elf_Internal_Sym *isymend;
4037 const struct elf_backend_data *bed;
4038 bfd_boolean add_needed;
66eb6687 4039 struct elf_link_hash_table *htab;
66eb6687 4040 void *alloc_mark = NULL;
4f87808c
AM
4041 struct bfd_hash_entry **old_table = NULL;
4042 unsigned int old_size = 0;
4043 unsigned int old_count = 0;
66eb6687 4044 void *old_tab = NULL;
66eb6687
AM
4045 void *old_ent;
4046 struct bfd_link_hash_entry *old_undefs = NULL;
4047 struct bfd_link_hash_entry *old_undefs_tail = NULL;
5b677558 4048 void *old_strtab = NULL;
66eb6687 4049 size_t tabsize = 0;
db6a5d5f 4050 asection *s;
29a9f53e 4051 bfd_boolean just_syms;
4ad4eba5 4052
66eb6687 4053 htab = elf_hash_table (info);
4ad4eba5 4054 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
4055
4056 if ((abfd->flags & DYNAMIC) == 0)
4057 dynamic = FALSE;
4058 else
4059 {
4060 dynamic = TRUE;
4061
4062 /* You can't use -r against a dynamic object. Also, there's no
4063 hope of using a dynamic object which does not exactly match
4064 the format of the output file. */
0e1862bb 4065 if (bfd_link_relocatable (info)
66eb6687 4066 || !is_elf_hash_table (htab)
f13a99db 4067 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 4068 {
0e1862bb 4069 if (bfd_link_relocatable (info))
9a0789ec
NC
4070 bfd_set_error (bfd_error_invalid_operation);
4071 else
4072 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
4073 goto error_return;
4074 }
4075 }
4076
a0c402a5
L
4077 ehdr = elf_elfheader (abfd);
4078 if (info->warn_alternate_em
4079 && bed->elf_machine_code != ehdr->e_machine
4080 && ((bed->elf_machine_alt1 != 0
4081 && ehdr->e_machine == bed->elf_machine_alt1)
4082 || (bed->elf_machine_alt2 != 0
4083 && ehdr->e_machine == bed->elf_machine_alt2)))
9793eb77 4084 _bfd_error_handler
695344c0 4085 /* xgettext:c-format */
9793eb77 4086 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
a0c402a5
L
4087 ehdr->e_machine, abfd, bed->elf_machine_code);
4088
4ad4eba5
AM
4089 /* As a GNU extension, any input sections which are named
4090 .gnu.warning.SYMBOL are treated as warning symbols for the given
4091 symbol. This differs from .gnu.warning sections, which generate
4092 warnings when they are included in an output file. */
dd98f8d2 4093 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 4094 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 4095 {
db6a5d5f 4096 const char *name;
4ad4eba5 4097
fd361982 4098 name = bfd_section_name (s);
db6a5d5f 4099 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 4100 {
db6a5d5f
AM
4101 char *msg;
4102 bfd_size_type sz;
4103
4104 name += sizeof ".gnu.warning." - 1;
4105
4106 /* If this is a shared object, then look up the symbol
4107 in the hash table. If it is there, and it is already
4108 been defined, then we will not be using the entry
4109 from this shared object, so we don't need to warn.
4110 FIXME: If we see the definition in a regular object
4111 later on, we will warn, but we shouldn't. The only
4112 fix is to keep track of what warnings we are supposed
4113 to emit, and then handle them all at the end of the
4114 link. */
4115 if (dynamic)
4ad4eba5 4116 {
db6a5d5f
AM
4117 struct elf_link_hash_entry *h;
4118
4119 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
4120
4121 /* FIXME: What about bfd_link_hash_common? */
4122 if (h != NULL
4123 && (h->root.type == bfd_link_hash_defined
4124 || h->root.type == bfd_link_hash_defweak))
4125 continue;
4126 }
4ad4eba5 4127
db6a5d5f
AM
4128 sz = s->size;
4129 msg = (char *) bfd_alloc (abfd, sz + 1);
4130 if (msg == NULL)
4131 goto error_return;
4ad4eba5 4132
db6a5d5f
AM
4133 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4134 goto error_return;
4ad4eba5 4135
db6a5d5f 4136 msg[sz] = '\0';
4ad4eba5 4137
db6a5d5f
AM
4138 if (! (_bfd_generic_link_add_one_symbol
4139 (info, abfd, name, BSF_WARNING, s, 0, msg,
4140 FALSE, bed->collect, NULL)))
4141 goto error_return;
4ad4eba5 4142
0e1862bb 4143 if (bfd_link_executable (info))
db6a5d5f
AM
4144 {
4145 /* Clobber the section size so that the warning does
4146 not get copied into the output file. */
4147 s->size = 0;
11d2f718 4148
db6a5d5f
AM
4149 /* Also set SEC_EXCLUDE, so that symbols defined in
4150 the warning section don't get copied to the output. */
4151 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
4152 }
4153 }
4154 }
4155
29a9f53e
L
4156 just_syms = ((s = abfd->sections) != NULL
4157 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4158
4ad4eba5
AM
4159 add_needed = TRUE;
4160 if (! dynamic)
4161 {
4162 /* If we are creating a shared library, create all the dynamic
4163 sections immediately. We need to attach them to something,
4164 so we attach them to this BFD, provided it is the right
bf89386a
L
4165 format and is not from ld --just-symbols. Always create the
4166 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
4167 are no input BFD's of the same format as the output, we can't
4168 make a shared library. */
4169 if (!just_syms
bf89386a 4170 && (bfd_link_pic (info)
9c1d7a08 4171 || (!bfd_link_relocatable (info)
3c5fce9b 4172 && info->nointerp
9c1d7a08 4173 && (info->export_dynamic || info->dynamic)))
66eb6687 4174 && is_elf_hash_table (htab)
f13a99db 4175 && info->output_bfd->xvec == abfd->xvec
66eb6687 4176 && !htab->dynamic_sections_created)
4ad4eba5
AM
4177 {
4178 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4179 goto error_return;
4180 }
4181 }
66eb6687 4182 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
4183 goto error_return;
4184 else
4185 {
4ad4eba5 4186 const char *soname = NULL;
7ee314fa 4187 char *audit = NULL;
4ad4eba5 4188 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
9acc85a6 4189 const Elf_Internal_Phdr *phdr;
e310298c 4190 struct elf_link_loaded_list *loaded_lib;
4ad4eba5
AM
4191
4192 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 4193 ld shouldn't allow it. */
29a9f53e 4194 if (just_syms)
92fd189d 4195 abort ();
4ad4eba5
AM
4196
4197 /* If this dynamic lib was specified on the command line with
4198 --as-needed in effect, then we don't want to add a DT_NEEDED
4199 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
4200 in by another lib's DT_NEEDED. When --no-add-needed is used
4201 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4202 any dynamic library in DT_NEEDED tags in the dynamic lib at
4203 all. */
4204 add_needed = (elf_dyn_lib_class (abfd)
4205 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4206 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
4207
4208 s = bfd_get_section_by_name (abfd, ".dynamic");
4209 if (s != NULL)
4210 {
4211 bfd_byte *dynbuf;
4212 bfd_byte *extdyn;
cb33740c 4213 unsigned int elfsec;
4ad4eba5
AM
4214 unsigned long shlink;
4215
eea6121a 4216 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194 4217 {
dc1e8a47 4218 error_free_dyn:
f8703194
L
4219 free (dynbuf);
4220 goto error_return;
4221 }
4ad4eba5
AM
4222
4223 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 4224 if (elfsec == SHN_BAD)
4ad4eba5
AM
4225 goto error_free_dyn;
4226 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4227
4228 for (extdyn = dynbuf;
9bff840e 4229 extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
4ad4eba5
AM
4230 extdyn += bed->s->sizeof_dyn)
4231 {
4232 Elf_Internal_Dyn dyn;
4233
4234 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4235 if (dyn.d_tag == DT_SONAME)
4236 {
4237 unsigned int tagv = dyn.d_un.d_val;
4238 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4239 if (soname == NULL)
4240 goto error_free_dyn;
4241 }
4242 if (dyn.d_tag == DT_NEEDED)
4243 {
4244 struct bfd_link_needed_list *n, **pn;
4245 char *fnm, *anm;
4246 unsigned int tagv = dyn.d_un.d_val;
986f0783 4247 size_t amt = sizeof (struct bfd_link_needed_list);
4ad4eba5 4248
a50b1753 4249 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4250 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4251 if (n == NULL || fnm == NULL)
4252 goto error_free_dyn;
4253 amt = strlen (fnm) + 1;
a50b1753 4254 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4255 if (anm == NULL)
4256 goto error_free_dyn;
4257 memcpy (anm, fnm, amt);
4258 n->name = anm;
4259 n->by = abfd;
4260 n->next = NULL;
66eb6687 4261 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4262 ;
4263 *pn = n;
4264 }
4265 if (dyn.d_tag == DT_RUNPATH)
4266 {
4267 struct bfd_link_needed_list *n, **pn;
4268 char *fnm, *anm;
4269 unsigned int tagv = dyn.d_un.d_val;
986f0783 4270 size_t amt = sizeof (struct bfd_link_needed_list);
4ad4eba5 4271
a50b1753 4272 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4273 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4274 if (n == NULL || fnm == NULL)
4275 goto error_free_dyn;
4276 amt = strlen (fnm) + 1;
a50b1753 4277 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4278 if (anm == NULL)
4279 goto error_free_dyn;
4280 memcpy (anm, fnm, amt);
4281 n->name = anm;
4282 n->by = abfd;
4283 n->next = NULL;
4284 for (pn = & runpath;
4285 *pn != NULL;
4286 pn = &(*pn)->next)
4287 ;
4288 *pn = n;
4289 }
4290 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4291 if (!runpath && dyn.d_tag == DT_RPATH)
4292 {
4293 struct bfd_link_needed_list *n, **pn;
4294 char *fnm, *anm;
4295 unsigned int tagv = dyn.d_un.d_val;
986f0783 4296 size_t amt = sizeof (struct bfd_link_needed_list);
4ad4eba5 4297
a50b1753 4298 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4299 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4300 if (n == NULL || fnm == NULL)
4301 goto error_free_dyn;
4302 amt = strlen (fnm) + 1;
a50b1753 4303 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 4304 if (anm == NULL)
f8703194 4305 goto error_free_dyn;
4ad4eba5
AM
4306 memcpy (anm, fnm, amt);
4307 n->name = anm;
4308 n->by = abfd;
4309 n->next = NULL;
4310 for (pn = & rpath;
4311 *pn != NULL;
4312 pn = &(*pn)->next)
4313 ;
4314 *pn = n;
4315 }
7ee314fa
AM
4316 if (dyn.d_tag == DT_AUDIT)
4317 {
4318 unsigned int tagv = dyn.d_un.d_val;
4319 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4320 }
4ad4eba5
AM
4321 }
4322
4323 free (dynbuf);
4324 }
4325
4326 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4327 frees all more recently bfd_alloc'd blocks as well. */
4328 if (runpath)
4329 rpath = runpath;
4330
4331 if (rpath)
4332 {
4333 struct bfd_link_needed_list **pn;
66eb6687 4334 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4335 ;
4336 *pn = rpath;
4337 }
4338
9acc85a6
AM
4339 /* If we have a PT_GNU_RELRO program header, mark as read-only
4340 all sections contained fully therein. This makes relro
4341 shared library sections appear as they will at run-time. */
4342 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
54025d58 4343 while (phdr-- > elf_tdata (abfd)->phdr)
9acc85a6
AM
4344 if (phdr->p_type == PT_GNU_RELRO)
4345 {
4346 for (s = abfd->sections; s != NULL; s = s->next)
502794d4
CE
4347 {
4348 unsigned int opb = bfd_octets_per_byte (abfd, s);
4349
4350 if ((s->flags & SEC_ALLOC) != 0
4351 && s->vma * opb >= phdr->p_vaddr
4352 && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4353 s->flags |= SEC_READONLY;
4354 }
9acc85a6
AM
4355 break;
4356 }
4357
4ad4eba5
AM
4358 /* We do not want to include any of the sections in a dynamic
4359 object in the output file. We hack by simply clobbering the
4360 list of sections in the BFD. This could be handled more
4361 cleanly by, say, a new section flag; the existing
4362 SEC_NEVER_LOAD flag is not the one we want, because that one
4363 still implies that the section takes up space in the output
4364 file. */
4365 bfd_section_list_clear (abfd);
4366
4ad4eba5
AM
4367 /* Find the name to use in a DT_NEEDED entry that refers to this
4368 object. If the object has a DT_SONAME entry, we use it.
4369 Otherwise, if the generic linker stuck something in
4370 elf_dt_name, we use that. Otherwise, we just use the file
4371 name. */
4372 if (soname == NULL || *soname == '\0')
4373 {
4374 soname = elf_dt_name (abfd);
4375 if (soname == NULL || *soname == '\0')
4376 soname = bfd_get_filename (abfd);
4377 }
4378
4379 /* Save the SONAME because sometimes the linker emulation code
4380 will need to know it. */
4381 elf_dt_name (abfd) = soname;
4382
4ad4eba5
AM
4383 /* If we have already included this dynamic object in the
4384 link, just ignore it. There is no reason to include a
4385 particular dynamic object more than once. */
e310298c
AM
4386 for (loaded_lib = htab->dyn_loaded;
4387 loaded_lib != NULL;
4388 loaded_lib = loaded_lib->next)
4389 {
4390 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4391 return TRUE;
4392 }
4393
4394 /* Create dynamic sections for backends that require that be done
4395 before setup_gnu_properties. */
4396 if (add_needed
4397 && !_bfd_elf_link_create_dynamic_sections (abfd, info))
4398 return FALSE;
7ee314fa
AM
4399
4400 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 4401 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
4402 }
4403
4404 /* If this is a dynamic object, we always link against the .dynsym
4405 symbol table, not the .symtab symbol table. The dynamic linker
4406 will only see the .dynsym symbol table, so there is no reason to
4407 look at .symtab for a dynamic object. */
4408
4409 if (! dynamic || elf_dynsymtab (abfd) == 0)
4410 hdr = &elf_tdata (abfd)->symtab_hdr;
4411 else
4412 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4413
4414 symcount = hdr->sh_size / bed->s->sizeof_sym;
4415
4416 /* The sh_info field of the symtab header tells us where the
4417 external symbols start. We don't care about the local symbols at
4418 this point. */
4419 if (elf_bad_symtab (abfd))
4420 {
4421 extsymcount = symcount;
4422 extsymoff = 0;
4423 }
4424 else
4425 {
4426 extsymcount = symcount - hdr->sh_info;
4427 extsymoff = hdr->sh_info;
4428 }
4429
f45794cb 4430 sym_hash = elf_sym_hashes (abfd);
012b2306 4431 if (extsymcount != 0)
4ad4eba5
AM
4432 {
4433 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4434 NULL, NULL, NULL);
4435 if (isymbuf == NULL)
4436 goto error_return;
4437
4ad4eba5 4438 if (sym_hash == NULL)
012b2306
AM
4439 {
4440 /* We store a pointer to the hash table entry for each
4441 external symbol. */
986f0783 4442 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
012b2306
AM
4443 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4444 if (sym_hash == NULL)
4445 goto error_free_sym;
4446 elf_sym_hashes (abfd) = sym_hash;
4447 }
4ad4eba5
AM
4448 }
4449
4450 if (dynamic)
4451 {
4452 /* Read in any version definitions. */
fc0e6df6
PB
4453 if (!_bfd_elf_slurp_version_tables (abfd,
4454 info->default_imported_symver))
4ad4eba5
AM
4455 goto error_free_sym;
4456
4457 /* Read in the symbol versions, but don't bother to convert them
4458 to internal format. */
4459 if (elf_dynversym (abfd) != 0)
4460 {
986f0783
AM
4461 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4462 bfd_size_type amt = versymhdr->sh_size;
4ad4eba5 4463
2bb3687b
AM
4464 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4465 goto error_free_sym;
4466 extversym = (Elf_External_Versym *)
4467 _bfd_malloc_and_read (abfd, amt, amt);
4ad4eba5
AM
4468 if (extversym == NULL)
4469 goto error_free_sym;
986f0783 4470 extversym_end = extversym + amt / sizeof (*extversym);
4ad4eba5
AM
4471 }
4472 }
4473
66eb6687
AM
4474 /* If we are loading an as-needed shared lib, save the symbol table
4475 state before we start adding symbols. If the lib turns out
4476 to be unneeded, restore the state. */
4477 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4478 {
4479 unsigned int i;
4480 size_t entsize;
4481
4482 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4483 {
4484 struct bfd_hash_entry *p;
2de92251 4485 struct elf_link_hash_entry *h;
66eb6687
AM
4486
4487 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
4488 {
4489 h = (struct elf_link_hash_entry *) p;
4490 entsize += htab->root.table.entsize;
4491 if (h->root.type == bfd_link_hash_warning)
7ba11550
AM
4492 {
4493 entsize += htab->root.table.entsize;
4494 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4495 }
4496 if (h->root.type == bfd_link_hash_common)
4497 entsize += sizeof (*h->root.u.c.p);
2de92251 4498 }
66eb6687
AM
4499 }
4500
4501 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 4502 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
4503 if (old_tab == NULL)
4504 goto error_free_vers;
4505
4506 /* Remember the current objalloc pointer, so that all mem for
4507 symbols added can later be reclaimed. */
4508 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4509 if (alloc_mark == NULL)
4510 goto error_free_vers;
4511
5061a885
AM
4512 /* Make a special call to the linker "notice" function to
4513 tell it that we are about to handle an as-needed lib. */
e5034e59 4514 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 4515 goto error_free_vers;
5061a885 4516
f45794cb
AM
4517 /* Clone the symbol table. Remember some pointers into the
4518 symbol table, and dynamic symbol count. */
4519 old_ent = (char *) old_tab + tabsize;
66eb6687 4520 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
4521 old_undefs = htab->root.undefs;
4522 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
4523 old_table = htab->root.table.table;
4524 old_size = htab->root.table.size;
4525 old_count = htab->root.table.count;
e310298c
AM
4526 old_strtab = NULL;
4527 if (htab->dynstr != NULL)
4528 {
4529 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4530 if (old_strtab == NULL)
4531 goto error_free_vers;
4532 }
66eb6687
AM
4533
4534 for (i = 0; i < htab->root.table.size; i++)
4535 {
4536 struct bfd_hash_entry *p;
2de92251 4537 struct elf_link_hash_entry *h;
66eb6687
AM
4538
4539 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4540 {
2de92251 4541 h = (struct elf_link_hash_entry *) p;
7ba11550
AM
4542 memcpy (old_ent, h, htab->root.table.entsize);
4543 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4544 if (h->root.type == bfd_link_hash_warning)
4545 {
7ba11550
AM
4546 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4547 memcpy (old_ent, h, htab->root.table.entsize);
2de92251
AM
4548 old_ent = (char *) old_ent + htab->root.table.entsize;
4549 }
7ba11550
AM
4550 if (h->root.type == bfd_link_hash_common)
4551 {
4552 memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
4553 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
4554 }
66eb6687
AM
4555 }
4556 }
4557 }
4ad4eba5 4558
66eb6687 4559 weaks = NULL;
be22c732
NC
4560 if (extversym == NULL)
4561 ever = NULL;
4562 else if (extversym + extsymoff < extversym_end)
4563 ever = extversym + extsymoff;
4564 else
4565 {
4566 /* xgettext:c-format */
4567 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4568 abfd, (long) extsymoff,
4569 (long) (extversym_end - extversym) / sizeof (* extversym));
4570 bfd_set_error (bfd_error_bad_value);
4571 goto error_free_vers;
4572 }
4573
b4c555cf
ML
4574 if (!bfd_link_relocatable (info)
4575 && abfd->lto_slim_object)
cc5277b1
ML
4576 {
4577 _bfd_error_handler
4578 (_("%pB: plugin needed to handle lto object"), abfd);
4579 }
4580
4ad4eba5
AM
4581 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4582 isym < isymend;
4583 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4584 {
4585 int bind;
4586 bfd_vma value;
af44c138 4587 asection *sec, *new_sec;
4ad4eba5
AM
4588 flagword flags;
4589 const char *name;
4590 struct elf_link_hash_entry *h;
90c984fc 4591 struct elf_link_hash_entry *hi;
4ad4eba5
AM
4592 bfd_boolean definition;
4593 bfd_boolean size_change_ok;
4594 bfd_boolean type_change_ok;
37a9e49a
L
4595 bfd_boolean new_weak;
4596 bfd_boolean old_weak;
7ba11550 4597 bfd *override;
a4d8e49b 4598 bfd_boolean common;
97196564 4599 bfd_boolean discarded;
4ad4eba5 4600 unsigned int old_alignment;
4538d1c7 4601 unsigned int shindex;
4ad4eba5 4602 bfd *old_bfd;
6e33951e 4603 bfd_boolean matched;
4ad4eba5 4604
7ba11550 4605 override = NULL;
4ad4eba5
AM
4606
4607 flags = BSF_NO_FLAGS;
4608 sec = NULL;
4609 value = isym->st_value;
a4d8e49b 4610 common = bed->common_definition (isym);
2980ccad
L
4611 if (common && info->inhibit_common_definition)
4612 {
4613 /* Treat common symbol as undefined for --no-define-common. */
4614 isym->st_shndx = SHN_UNDEF;
4615 common = FALSE;
4616 }
97196564 4617 discarded = FALSE;
4ad4eba5
AM
4618
4619 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 4620 switch (bind)
4ad4eba5 4621 {
3e7a7d11 4622 case STB_LOCAL:
4ad4eba5
AM
4623 /* This should be impossible, since ELF requires that all
4624 global symbols follow all local symbols, and that sh_info
4625 point to the first global symbol. Unfortunately, Irix 5
4626 screws this up. */
fe3fef62
AM
4627 if (elf_bad_symtab (abfd))
4628 continue;
4629
4630 /* If we aren't prepared to handle locals within the globals
4538d1c7
AM
4631 then we'll likely segfault on a NULL symbol hash if the
4632 symbol is ever referenced in relocations. */
4633 shindex = elf_elfheader (abfd)->e_shstrndx;
4634 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4635 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4636 " (>= sh_info of %lu)"),
4637 abfd, name, (long) (isym - isymbuf + extsymoff),
4638 (long) extsymoff);
4639
4640 /* Dynamic object relocations are not processed by ld, so
4641 ld won't run into the problem mentioned above. */
4642 if (dynamic)
4643 continue;
fe3fef62
AM
4644 bfd_set_error (bfd_error_bad_value);
4645 goto error_free_vers;
3e7a7d11
NC
4646
4647 case STB_GLOBAL:
a4d8e49b 4648 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4649 flags = BSF_GLOBAL;
3e7a7d11
NC
4650 break;
4651
4652 case STB_WEAK:
4653 flags = BSF_WEAK;
4654 break;
4655
4656 case STB_GNU_UNIQUE:
4657 flags = BSF_GNU_UNIQUE;
4658 break;
4659
4660 default:
4ad4eba5 4661 /* Leave it up to the processor backend. */
3e7a7d11 4662 break;
4ad4eba5
AM
4663 }
4664
4665 if (isym->st_shndx == SHN_UNDEF)
4666 sec = bfd_und_section_ptr;
cb33740c
AM
4667 else if (isym->st_shndx == SHN_ABS)
4668 sec = bfd_abs_section_ptr;
4669 else if (isym->st_shndx == SHN_COMMON)
4670 {
4671 sec = bfd_com_section_ptr;
4672 /* What ELF calls the size we call the value. What ELF
4673 calls the value we call the alignment. */
4674 value = isym->st_size;
4675 }
4676 else
4ad4eba5
AM
4677 {
4678 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4679 if (sec == NULL)
4680 sec = bfd_abs_section_ptr;
dbaa2011 4681 else if (discarded_section (sec))
529fcb95 4682 {
e5d08002
L
4683 /* Symbols from discarded section are undefined. We keep
4684 its visibility. */
529fcb95 4685 sec = bfd_und_section_ptr;
97196564 4686 discarded = TRUE;
529fcb95
PB
4687 isym->st_shndx = SHN_UNDEF;
4688 }
4ad4eba5
AM
4689 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4690 value -= sec->vma;
4691 }
4ad4eba5
AM
4692
4693 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4694 isym->st_name);
4695 if (name == NULL)
4696 goto error_free_vers;
4697
4698 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4699 && (abfd->flags & BFD_PLUGIN) != 0)
4700 {
4701 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4702
4703 if (xc == NULL)
4704 {
4705 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4706 | SEC_EXCLUDE);
4707 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4708 if (xc == NULL)
4709 goto error_free_vers;
4710 }
4711 sec = xc;
4712 }
4713 else if (isym->st_shndx == SHN_COMMON
4714 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4715 && !bfd_link_relocatable (info))
4ad4eba5
AM
4716 {
4717 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4718
4719 if (tcomm == NULL)
4720 {
02d00247
AM
4721 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4722 | SEC_LINKER_CREATED);
4723 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4724 if (tcomm == NULL)
4ad4eba5
AM
4725 goto error_free_vers;
4726 }
4727 sec = tcomm;
4728 }
66eb6687 4729 else if (bed->elf_add_symbol_hook)
4ad4eba5 4730 {
66eb6687
AM
4731 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4732 &sec, &value))
4ad4eba5
AM
4733 goto error_free_vers;
4734
4735 /* The hook function sets the name to NULL if this symbol
4736 should be skipped for some reason. */
4737 if (name == NULL)
4738 continue;
4739 }
4740
4741 /* Sanity check that all possibilities were handled. */
4742 if (sec == NULL)
4538d1c7 4743 abort ();
4ad4eba5 4744
191c0c42
AM
4745 /* Silently discard TLS symbols from --just-syms. There's
4746 no way to combine a static TLS block with a new TLS block
4747 for this executable. */
4748 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4749 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4750 continue;
4751
4ad4eba5
AM
4752 if (bfd_is_und_section (sec)
4753 || bfd_is_com_section (sec))
4754 definition = FALSE;
4755 else
4756 definition = TRUE;
4757
4758 size_change_ok = FALSE;
66eb6687 4759 type_change_ok = bed->type_change_ok;
37a9e49a 4760 old_weak = FALSE;
6e33951e 4761 matched = FALSE;
4ad4eba5
AM
4762 old_alignment = 0;
4763 old_bfd = NULL;
af44c138 4764 new_sec = sec;
4ad4eba5 4765
66eb6687 4766 if (is_elf_hash_table (htab))
4ad4eba5
AM
4767 {
4768 Elf_Internal_Versym iver;
4769 unsigned int vernum = 0;
4770 bfd_boolean skip;
4771
fc0e6df6 4772 if (ever == NULL)
4ad4eba5 4773 {
fc0e6df6
PB
4774 if (info->default_imported_symver)
4775 /* Use the default symbol version created earlier. */
4776 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4777 else
4778 iver.vs_vers = 0;
4779 }
be22c732
NC
4780 else if (ever >= extversym_end)
4781 {
4782 /* xgettext:c-format */
4783 _bfd_error_handler (_("%pB: not enough version information"),
4784 abfd);
4785 bfd_set_error (bfd_error_bad_value);
4786 goto error_free_vers;
4787 }
fc0e6df6
PB
4788 else
4789 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4790
4791 vernum = iver.vs_vers & VERSYM_VERSION;
4792
4793 /* If this is a hidden symbol, or if it is not version
4794 1, we append the version name to the symbol name.
cc86ff91
EB
4795 However, we do not modify a non-hidden absolute symbol
4796 if it is not a function, because it might be the version
4797 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4798 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4799 || (vernum > 1
4800 && (!bfd_is_abs_section (sec)
4801 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4802 {
4803 const char *verstr;
4804 size_t namelen, verlen, newlen;
4805 char *newname, *p;
4806
4807 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4808 {
fc0e6df6
PB
4809 if (vernum > elf_tdata (abfd)->cverdefs)
4810 verstr = NULL;
4811 else if (vernum > 1)
4812 verstr =
4813 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4814 else
4815 verstr = "";
4ad4eba5 4816
fc0e6df6 4817 if (verstr == NULL)
4ad4eba5 4818 {
4eca0228 4819 _bfd_error_handler
695344c0 4820 /* xgettext:c-format */
871b3ab2 4821 (_("%pB: %s: invalid version %u (max %d)"),
fc0e6df6
PB
4822 abfd, name, vernum,
4823 elf_tdata (abfd)->cverdefs);
4824 bfd_set_error (bfd_error_bad_value);
4825 goto error_free_vers;
4ad4eba5 4826 }
fc0e6df6
PB
4827 }
4828 else
4829 {
4830 /* We cannot simply test for the number of
4831 entries in the VERNEED section since the
4832 numbers for the needed versions do not start
4833 at 0. */
4834 Elf_Internal_Verneed *t;
4835
4836 verstr = NULL;
4837 for (t = elf_tdata (abfd)->verref;
4838 t != NULL;
4839 t = t->vn_nextref)
4ad4eba5 4840 {
fc0e6df6 4841 Elf_Internal_Vernaux *a;
4ad4eba5 4842
fc0e6df6
PB
4843 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4844 {
4845 if (a->vna_other == vernum)
4ad4eba5 4846 {
fc0e6df6
PB
4847 verstr = a->vna_nodename;
4848 break;
4ad4eba5 4849 }
4ad4eba5 4850 }
fc0e6df6
PB
4851 if (a != NULL)
4852 break;
4853 }
4854 if (verstr == NULL)
4855 {
4eca0228 4856 _bfd_error_handler
695344c0 4857 /* xgettext:c-format */
871b3ab2 4858 (_("%pB: %s: invalid needed version %d"),
fc0e6df6
PB
4859 abfd, name, vernum);
4860 bfd_set_error (bfd_error_bad_value);
4861 goto error_free_vers;
4ad4eba5 4862 }
4ad4eba5 4863 }
fc0e6df6
PB
4864
4865 namelen = strlen (name);
4866 verlen = strlen (verstr);
4867 newlen = namelen + verlen + 2;
4868 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4869 && isym->st_shndx != SHN_UNDEF)
4870 ++newlen;
4871
a50b1753 4872 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4873 if (newname == NULL)
4874 goto error_free_vers;
4875 memcpy (newname, name, namelen);
4876 p = newname + namelen;
4877 *p++ = ELF_VER_CHR;
4878 /* If this is a defined non-hidden version symbol,
4879 we add another @ to the name. This indicates the
4880 default version of the symbol. */
4881 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4882 && isym->st_shndx != SHN_UNDEF)
4883 *p++ = ELF_VER_CHR;
4884 memcpy (p, verstr, verlen + 1);
4885
4886 name = newname;
4ad4eba5
AM
4887 }
4888
cd3416da
AM
4889 /* If this symbol has default visibility and the user has
4890 requested we not re-export it, then mark it as hidden. */
a0d49154 4891 if (!bfd_is_und_section (sec)
cd3416da 4892 && !dynamic
ce875075 4893 && abfd->no_export
cd3416da
AM
4894 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4895 isym->st_other = (STV_HIDDEN
4896 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4897
4f3fedcf
AM
4898 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4899 sym_hash, &old_bfd, &old_weak,
4900 &old_alignment, &skip, &override,
6e33951e
L
4901 &type_change_ok, &size_change_ok,
4902 &matched))
4ad4eba5
AM
4903 goto error_free_vers;
4904
4905 if (skip)
4906 continue;
4907
6e33951e
L
4908 /* Override a definition only if the new symbol matches the
4909 existing one. */
4910 if (override && matched)
4ad4eba5
AM
4911 definition = FALSE;
4912
4913 h = *sym_hash;
4914 while (h->root.type == bfd_link_hash_indirect
4915 || h->root.type == bfd_link_hash_warning)
4916 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4917
4ad4eba5 4918 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4919 && vernum > 1
4920 && definition)
4921 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4922 }
4923
4924 if (! (_bfd_generic_link_add_one_symbol
7ba11550
AM
4925 (info, override ? override : abfd, name, flags, sec, value,
4926 NULL, FALSE, bed->collect,
4ad4eba5
AM
4927 (struct bfd_link_hash_entry **) sym_hash)))
4928 goto error_free_vers;
4929
4930 h = *sym_hash;
90c984fc
L
4931 /* We need to make sure that indirect symbol dynamic flags are
4932 updated. */
4933 hi = h;
4ad4eba5
AM
4934 while (h->root.type == bfd_link_hash_indirect
4935 || h->root.type == bfd_link_hash_warning)
4936 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4937
97196564
L
4938 /* Setting the index to -3 tells elf_link_output_extsym that
4939 this symbol is defined in a discarded section. */
4940 if (discarded)
4941 h->indx = -3;
4942
4ad4eba5
AM
4943 *sym_hash = h;
4944
37a9e49a 4945 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4946 if (dynamic
4947 && definition
37a9e49a 4948 && new_weak
fcb93ecf 4949 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4950 && is_elf_hash_table (htab)
60d67dc8 4951 && h->u.alias == NULL)
4ad4eba5
AM
4952 {
4953 /* Keep a list of all weak defined non function symbols from
60d67dc8
AM
4954 a dynamic object, using the alias field. Later in this
4955 function we will set the alias field to the correct
4ad4eba5
AM
4956 value. We only put non-function symbols from dynamic
4957 objects on this list, because that happens to be the only
4958 time we need to know the normal symbol corresponding to a
4959 weak symbol, and the information is time consuming to
60d67dc8 4960 figure out. If the alias field is not already NULL,
4ad4eba5
AM
4961 then this symbol was already defined by some previous
4962 dynamic object, and we will be using that previous
4963 definition anyhow. */
4964
60d67dc8 4965 h->u.alias = weaks;
4ad4eba5 4966 weaks = h;
4ad4eba5
AM
4967 }
4968
4969 /* Set the alignment of a common symbol. */
a4d8e49b 4970 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4971 && h->root.type == bfd_link_hash_common)
4972 {
4973 unsigned int align;
4974
a4d8e49b 4975 if (common)
af44c138
L
4976 align = bfd_log2 (isym->st_value);
4977 else
4978 {
4979 /* The new symbol is a common symbol in a shared object.
4980 We need to get the alignment from the section. */
4981 align = new_sec->alignment_power;
4982 }
595213d4 4983 if (align > old_alignment)
4ad4eba5
AM
4984 h->root.u.c.p->alignment_power = align;
4985 else
4986 h->root.u.c.p->alignment_power = old_alignment;
4987 }
4988
66eb6687 4989 if (is_elf_hash_table (htab))
4ad4eba5 4990 {
4f3fedcf
AM
4991 /* Set a flag in the hash table entry indicating the type of
4992 reference or definition we just found. A dynamic symbol
4993 is one which is referenced or defined by both a regular
4994 object and a shared object. */
4995 bfd_boolean dynsym = FALSE;
4996
b1a92c63
AM
4997 /* Plugin symbols aren't normal. Don't set def/ref flags. */
4998 if ((abfd->flags & BFD_PLUGIN) != 0)
4999 ;
5000 else if (!dynamic)
4f3fedcf
AM
5001 {
5002 if (! definition)
5003 {
5004 h->ref_regular = 1;
5005 if (bind != STB_WEAK)
5006 h->ref_regular_nonweak = 1;
5007 }
5008 else
5009 {
5010 h->def_regular = 1;
5011 if (h->def_dynamic)
5012 {
5013 h->def_dynamic = 0;
5014 h->ref_dynamic = 1;
5015 }
5016 }
4f3fedcf
AM
5017 }
5018 else
5019 {
5020 if (! definition)
5021 {
5022 h->ref_dynamic = 1;
5023 hi->ref_dynamic = 1;
5024 }
5025 else
5026 {
5027 h->def_dynamic = 1;
5028 hi->def_dynamic = 1;
5029 }
b1a92c63 5030 }
4f3fedcf 5031
b1a92c63
AM
5032 /* If an indirect symbol has been forced local, don't
5033 make the real symbol dynamic. */
5034 if (h != hi && hi->forced_local)
5035 ;
5036 else if (!dynamic)
5037 {
5038 if (bfd_link_dll (info)
5039 || h->def_dynamic
5040 || h->ref_dynamic)
5041 dynsym = TRUE;
5042 }
5043 else
5044 {
5045 if (h->def_regular
5046 || h->ref_regular
5047 || (h->is_weakalias
5048 && weakdef (h)->dynindx != -1))
4f3fedcf
AM
5049 dynsym = TRUE;
5050 }
5051
5052 /* Check to see if we need to add an indirect symbol for
5053 the default name. */
5054 if (definition
5055 || (!override && h->root.type == bfd_link_hash_common))
5056 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5057 sec, value, &old_bfd, &dynsym))
5058 goto error_free_vers;
4ad4eba5
AM
5059
5060 /* Check the alignment when a common symbol is involved. This
5061 can change when a common symbol is overridden by a normal
5062 definition or a common symbol is ignored due to the old
5063 normal definition. We need to make sure the maximum
5064 alignment is maintained. */
a4d8e49b 5065 if ((old_alignment || common)
4ad4eba5
AM
5066 && h->root.type != bfd_link_hash_common)
5067 {
5068 unsigned int common_align;
5069 unsigned int normal_align;
5070 unsigned int symbol_align;
5071 bfd *normal_bfd;
5072 bfd *common_bfd;
5073
3a81e825
AM
5074 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5075 || h->root.type == bfd_link_hash_defweak);
5076
4ad4eba5
AM
5077 symbol_align = ffs (h->root.u.def.value) - 1;
5078 if (h->root.u.def.section->owner != NULL
0616a280
AM
5079 && (h->root.u.def.section->owner->flags
5080 & (DYNAMIC | BFD_PLUGIN)) == 0)
4ad4eba5
AM
5081 {
5082 normal_align = h->root.u.def.section->alignment_power;
5083 if (normal_align > symbol_align)
5084 normal_align = symbol_align;
5085 }
5086 else
5087 normal_align = symbol_align;
5088
5089 if (old_alignment)
5090 {
5091 common_align = old_alignment;
5092 common_bfd = old_bfd;
5093 normal_bfd = abfd;
5094 }
5095 else
5096 {
5097 common_align = bfd_log2 (isym->st_value);
5098 common_bfd = abfd;
5099 normal_bfd = old_bfd;
5100 }
5101
5102 if (normal_align < common_align)
d07676f8
NC
5103 {
5104 /* PR binutils/2735 */
5105 if (normal_bfd == NULL)
4eca0228 5106 _bfd_error_handler
695344c0 5107 /* xgettext:c-format */
9793eb77 5108 (_("warning: alignment %u of common symbol `%s' in %pB is"
871b3ab2 5109 " greater than the alignment (%u) of its section %pA"),
c08bb8dd
AM
5110 1 << common_align, name, common_bfd,
5111 1 << normal_align, h->root.u.def.section);
d07676f8 5112 else
4eca0228 5113 _bfd_error_handler
695344c0 5114 /* xgettext:c-format */
9793eb77 5115 (_("warning: alignment %u of symbol `%s' in %pB"
871b3ab2 5116 " is smaller than %u in %pB"),
c08bb8dd
AM
5117 1 << normal_align, name, normal_bfd,
5118 1 << common_align, common_bfd);
d07676f8 5119 }
4ad4eba5
AM
5120 }
5121
83ad0046 5122 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
5123 if (isym->st_size != 0
5124 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
5125 && (definition || h->size == 0))
5126 {
83ad0046
L
5127 if (h->size != 0
5128 && h->size != isym->st_size
5129 && ! size_change_ok)
4eca0228 5130 _bfd_error_handler
695344c0 5131 /* xgettext:c-format */
9793eb77 5132 (_("warning: size of symbol `%s' changed"
2dcf00ce
AM
5133 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5134 name, (uint64_t) h->size, old_bfd,
5135 (uint64_t) isym->st_size, abfd);
4ad4eba5
AM
5136
5137 h->size = isym->st_size;
5138 }
5139
5140 /* If this is a common symbol, then we always want H->SIZE
5141 to be the size of the common symbol. The code just above
5142 won't fix the size if a common symbol becomes larger. We
5143 don't warn about a size change here, because that is
4f3fedcf 5144 covered by --warn-common. Allow changes between different
fcb93ecf 5145 function types. */
4ad4eba5
AM
5146 if (h->root.type == bfd_link_hash_common)
5147 h->size = h->root.u.c.size;
5148
5149 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
5150 && ((definition && !new_weak)
5151 || (old_weak && h->root.type == bfd_link_hash_common)
5152 || h->type == STT_NOTYPE))
4ad4eba5 5153 {
2955ec4c
L
5154 unsigned int type = ELF_ST_TYPE (isym->st_info);
5155
5156 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5157 symbol. */
5158 if (type == STT_GNU_IFUNC
5159 && (abfd->flags & DYNAMIC) != 0)
5160 type = STT_FUNC;
4ad4eba5 5161
2955ec4c
L
5162 if (h->type != type)
5163 {
5164 if (h->type != STT_NOTYPE && ! type_change_ok)
695344c0 5165 /* xgettext:c-format */
4eca0228 5166 _bfd_error_handler
9793eb77 5167 (_("warning: type of symbol `%s' changed"
871b3ab2 5168 " from %d to %d in %pB"),
c08bb8dd 5169 name, h->type, type, abfd);
2955ec4c
L
5170
5171 h->type = type;
5172 }
4ad4eba5
AM
5173 }
5174
54ac0771 5175 /* Merge st_other field. */
b8417128 5176 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4ad4eba5 5177
c3df8c14 5178 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
5179 if (definition
5180 && (sec->flags & SEC_DEBUGGING)
5181 && !bfd_link_relocatable (info))
c3df8c14
AM
5182 dynsym = FALSE;
5183
b1a92c63
AM
5184 /* Nor should we make plugin symbols dynamic. */
5185 if ((abfd->flags & BFD_PLUGIN) != 0)
5186 dynsym = FALSE;
5187
35fc36a8 5188 if (definition)
35399224
L
5189 {
5190 h->target_internal = isym->st_target_internal;
5191 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5192 }
35fc36a8 5193
4ad4eba5
AM
5194 if (definition && !dynamic)
5195 {
5196 char *p = strchr (name, ELF_VER_CHR);
5197 if (p != NULL && p[1] != ELF_VER_CHR)
5198 {
5199 /* Queue non-default versions so that .symver x, x@FOO
5200 aliases can be checked. */
66eb6687 5201 if (!nondeflt_vers)
4ad4eba5 5202 {
986f0783
AM
5203 size_t amt = ((isymend - isym + 1)
5204 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
5205 nondeflt_vers
5206 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
5207 if (!nondeflt_vers)
5208 goto error_free_vers;
4ad4eba5 5209 }
66eb6687 5210 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
5211 }
5212 }
5213
b1a92c63 5214 if (dynsym && h->dynindx == -1)
4ad4eba5 5215 {
c152c796 5216 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 5217 goto error_free_vers;
60d67dc8
AM
5218 if (h->is_weakalias
5219 && weakdef (h)->dynindx == -1)
4ad4eba5 5220 {
60d67dc8 5221 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
4ad4eba5
AM
5222 goto error_free_vers;
5223 }
5224 }
1f599d0e 5225 else if (h->dynindx != -1)
4ad4eba5
AM
5226 /* If the symbol already has a dynamic index, but
5227 visibility says it should not be visible, turn it into
5228 a local symbol. */
5229 switch (ELF_ST_VISIBILITY (h->other))
5230 {
5231 case STV_INTERNAL:
5232 case STV_HIDDEN:
5233 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5234 dynsym = FALSE;
5235 break;
5236 }
5237
5238 if (!add_needed
aef28989 5239 && matched
4ad4eba5 5240 && definition
010e5ae2 5241 && ((dynsym
a896df97 5242 && h->ref_regular_nonweak)
b1a92c63
AM
5243 || (old_bfd != NULL
5244 && (old_bfd->flags & BFD_PLUGIN) != 0
5245 && bind != STB_WEAK)
ffa9430d 5246 || (h->ref_dynamic_nonweak
010e5ae2 5247 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
7b15fa7a
AM
5248 && !on_needed_list (elf_dt_name (abfd),
5249 htab->needed, NULL))))
4ad4eba5 5250 {
4ad4eba5
AM
5251 const char *soname = elf_dt_name (abfd);
5252
16e4ecc0
AM
5253 info->callbacks->minfo ("%!", soname, old_bfd,
5254 h->root.root.string);
5255
4ad4eba5
AM
5256 /* A symbol from a library loaded via DT_NEEDED of some
5257 other library is referenced by a regular object.
e56f61be 5258 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
5259 --no-add-needed is used and the reference was not
5260 a weak one. */
4f3fedcf 5261 if (old_bfd != NULL
b918acf9 5262 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be 5263 {
4eca0228 5264 _bfd_error_handler
695344c0 5265 /* xgettext:c-format */
871b3ab2 5266 (_("%pB: undefined reference to symbol '%s'"),
4f3fedcf 5267 old_bfd, name);
ff5ac77b 5268 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
5269 goto error_free_vers;
5270 }
5271
a50b1753 5272 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 5273 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 5274
e310298c
AM
5275 /* Create dynamic sections for backends that require
5276 that be done before setup_gnu_properties. */
5277 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5278 return FALSE;
4ad4eba5 5279 add_needed = TRUE;
4ad4eba5
AM
5280 }
5281 }
5282 }
5283
a83ef4d1
L
5284 if (info->lto_plugin_active
5285 && !bfd_link_relocatable (info)
5286 && (abfd->flags & BFD_PLUGIN) == 0
5287 && !just_syms
5288 && extsymcount)
5289 {
5290 int r_sym_shift;
5291
5292 if (bed->s->arch_size == 32)
5293 r_sym_shift = 8;
5294 else
5295 r_sym_shift = 32;
5296
5297 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5298 referenced in regular objects so that linker plugin will get
5299 the correct symbol resolution. */
5300
5301 sym_hash = elf_sym_hashes (abfd);
5302 for (s = abfd->sections; s != NULL; s = s->next)
5303 {
5304 Elf_Internal_Rela *internal_relocs;
5305 Elf_Internal_Rela *rel, *relend;
5306
5307 /* Don't check relocations in excluded sections. */
5308 if ((s->flags & SEC_RELOC) == 0
5309 || s->reloc_count == 0
5310 || (s->flags & SEC_EXCLUDE) != 0
5311 || ((info->strip == strip_all
5312 || info->strip == strip_debugger)
5313 && (s->flags & SEC_DEBUGGING) != 0))
5314 continue;
5315
5316 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5317 NULL,
5318 info->keep_memory);
5319 if (internal_relocs == NULL)
5320 goto error_free_vers;
5321
5322 rel = internal_relocs;
5323 relend = rel + s->reloc_count;
5324 for ( ; rel < relend; rel++)
5325 {
5326 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5327 struct elf_link_hash_entry *h;
5328
5329 /* Skip local symbols. */
5330 if (r_symndx < extsymoff)
5331 continue;
5332
5333 h = sym_hash[r_symndx - extsymoff];
5334 if (h != NULL)
5335 h->root.non_ir_ref_regular = 1;
5336 }
5337
5338 if (elf_section_data (s)->relocs != internal_relocs)
5339 free (internal_relocs);
5340 }
5341 }
5342
c9594989
AM
5343 free (extversym);
5344 extversym = NULL;
5345 free (isymbuf);
5346 isymbuf = NULL;
66eb6687
AM
5347
5348 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5349 {
5350 unsigned int i;
5351
5352 /* Restore the symbol table. */
f45794cb
AM
5353 old_ent = (char *) old_tab + tabsize;
5354 memset (elf_sym_hashes (abfd), 0,
5355 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
5356 htab->root.table.table = old_table;
5357 htab->root.table.size = old_size;
5358 htab->root.table.count = old_count;
66eb6687 5359 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
5360 htab->root.undefs = old_undefs;
5361 htab->root.undefs_tail = old_undefs_tail;
e310298c
AM
5362 if (htab->dynstr != NULL)
5363 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5b677558
AM
5364 free (old_strtab);
5365 old_strtab = NULL;
66eb6687
AM
5366 for (i = 0; i < htab->root.table.size; i++)
5367 {
5368 struct bfd_hash_entry *p;
5369 struct elf_link_hash_entry *h;
4070765b 5370 unsigned int non_ir_ref_dynamic;
66eb6687
AM
5371
5372 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5373 {
4070765b 5374 /* Preserve non_ir_ref_dynamic so that this symbol
59fa66c5
L
5375 will be exported when the dynamic lib becomes needed
5376 in the second pass. */
7ba11550
AM
5377 h = (struct elf_link_hash_entry *) p;
5378 if (h->root.type == bfd_link_hash_warning)
5379 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4070765b 5380 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
7ba11550 5381
2de92251 5382 h = (struct elf_link_hash_entry *) p;
7ba11550
AM
5383 memcpy (h, old_ent, htab->root.table.entsize);
5384 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
5385 if (h->root.type == bfd_link_hash_warning)
5386 {
a4542f1b 5387 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7ba11550
AM
5388 memcpy (h, old_ent, htab->root.table.entsize);
5389 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251 5390 }
a4542f1b 5391 if (h->root.type == bfd_link_hash_common)
3e0882af 5392 {
7ba11550
AM
5393 memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5394 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
3e0882af 5395 }
4070765b 5396 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
66eb6687
AM
5397 }
5398 }
5399
5061a885
AM
5400 /* Make a special call to the linker "notice" function to
5401 tell it that symbols added for crefs may need to be removed. */
e5034e59 5402 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 5403 goto error_free_vers;
5061a885 5404
66eb6687
AM
5405 free (old_tab);
5406 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5407 alloc_mark);
c9594989 5408 free (nondeflt_vers);
66eb6687
AM
5409 return TRUE;
5410 }
2de92251 5411
66eb6687
AM
5412 if (old_tab != NULL)
5413 {
e5034e59 5414 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 5415 goto error_free_vers;
66eb6687
AM
5416 free (old_tab);
5417 old_tab = NULL;
5418 }
5419
c6e8a9a8
L
5420 /* Now that all the symbols from this input file are created, if
5421 not performing a relocatable link, handle .symver foo, foo@BAR
5422 such that any relocs against foo become foo@BAR. */
0e1862bb 5423 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5 5424 {
ef53be89 5425 size_t cnt, symidx;
4ad4eba5
AM
5426
5427 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5428 {
5429 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5430 char *shortname, *p;
986f0783 5431 size_t amt;
4ad4eba5
AM
5432
5433 p = strchr (h->root.root.string, ELF_VER_CHR);
5434 if (p == NULL
5435 || (h->root.type != bfd_link_hash_defined
5436 && h->root.type != bfd_link_hash_defweak))
5437 continue;
5438
5439 amt = p - h->root.root.string;
a50b1753 5440 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
5441 if (!shortname)
5442 goto error_free_vers;
4ad4eba5
AM
5443 memcpy (shortname, h->root.root.string, amt);
5444 shortname[amt] = '\0';
5445
5446 hi = (struct elf_link_hash_entry *)
66eb6687 5447 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
5448 FALSE, FALSE, FALSE);
5449 if (hi != NULL
5450 && hi->root.type == h->root.type
5451 && hi->root.u.def.value == h->root.u.def.value
5452 && hi->root.u.def.section == h->root.u.def.section)
5453 {
5454 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5455 hi->root.type = bfd_link_hash_indirect;
5456 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 5457 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
5458 sym_hash = elf_sym_hashes (abfd);
5459 if (sym_hash)
5460 for (symidx = 0; symidx < extsymcount; ++symidx)
5461 if (sym_hash[symidx] == hi)
5462 {
5463 sym_hash[symidx] = h;
5464 break;
5465 }
5466 }
5467 free (shortname);
5468 }
5469 free (nondeflt_vers);
5470 nondeflt_vers = NULL;
5471 }
5472
60d67dc8 5473 /* Now set the alias field correctly for all the weak defined
4ad4eba5
AM
5474 symbols we found. The only way to do this is to search all the
5475 symbols. Since we only need the information for non functions in
5476 dynamic objects, that's the only time we actually put anything on
5477 the list WEAKS. We need this information so that if a regular
5478 object refers to a symbol defined weakly in a dynamic object, the
5479 real symbol in the dynamic object is also put in the dynamic
5480 symbols; we also must arrange for both symbols to point to the
5481 same memory location. We could handle the general case of symbol
5482 aliasing, but a general symbol alias can only be generated in
5483 assembler code, handling it correctly would be very time
5484 consuming, and other ELF linkers don't handle general aliasing
5485 either. */
5486 if (weaks != NULL)
5487 {
5488 struct elf_link_hash_entry **hpp;
5489 struct elf_link_hash_entry **hppend;
5490 struct elf_link_hash_entry **sorted_sym_hash;
5491 struct elf_link_hash_entry *h;
986f0783 5492 size_t sym_count, amt;
4ad4eba5
AM
5493
5494 /* Since we have to search the whole symbol list for each weak
5495 defined symbol, search time for N weak defined symbols will be
5496 O(N^2). Binary search will cut it down to O(NlogN). */
986f0783 5497 amt = extsymcount * sizeof (*sorted_sym_hash);
3a3f4bf7 5498 sorted_sym_hash = bfd_malloc (amt);
4ad4eba5
AM
5499 if (sorted_sym_hash == NULL)
5500 goto error_return;
5501 sym_hash = sorted_sym_hash;
5502 hpp = elf_sym_hashes (abfd);
5503 hppend = hpp + extsymcount;
5504 sym_count = 0;
5505 for (; hpp < hppend; hpp++)
5506 {
5507 h = *hpp;
5508 if (h != NULL
5509 && h->root.type == bfd_link_hash_defined
fcb93ecf 5510 && !bed->is_function_type (h->type))
4ad4eba5
AM
5511 {
5512 *sym_hash = h;
5513 sym_hash++;
5514 sym_count++;
5515 }
5516 }
5517
3a3f4bf7 5518 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
4ad4eba5
AM
5519 elf_sort_symbol);
5520
5521 while (weaks != NULL)
5522 {
5523 struct elf_link_hash_entry *hlook;
5524 asection *slook;
5525 bfd_vma vlook;
ed54588d 5526 size_t i, j, idx = 0;
4ad4eba5
AM
5527
5528 hlook = weaks;
60d67dc8
AM
5529 weaks = hlook->u.alias;
5530 hlook->u.alias = NULL;
4ad4eba5 5531
e3e53eed
AM
5532 if (hlook->root.type != bfd_link_hash_defined
5533 && hlook->root.type != bfd_link_hash_defweak)
5534 continue;
5535
4ad4eba5
AM
5536 slook = hlook->root.u.def.section;
5537 vlook = hlook->root.u.def.value;
5538
4ad4eba5
AM
5539 i = 0;
5540 j = sym_count;
14160578 5541 while (i != j)
4ad4eba5
AM
5542 {
5543 bfd_signed_vma vdiff;
5544 idx = (i + j) / 2;
14160578 5545 h = sorted_sym_hash[idx];
4ad4eba5
AM
5546 vdiff = vlook - h->root.u.def.value;
5547 if (vdiff < 0)
5548 j = idx;
5549 else if (vdiff > 0)
5550 i = idx + 1;
5551 else
5552 {
d3435ae8 5553 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
5554 if (sdiff < 0)
5555 j = idx;
5556 else if (sdiff > 0)
5557 i = idx + 1;
5558 else
14160578 5559 break;
4ad4eba5
AM
5560 }
5561 }
5562
5563 /* We didn't find a value/section match. */
14160578 5564 if (i == j)
4ad4eba5
AM
5565 continue;
5566
14160578
AM
5567 /* With multiple aliases, or when the weak symbol is already
5568 strongly defined, we have multiple matching symbols and
5569 the binary search above may land on any of them. Step
5570 one past the matching symbol(s). */
5571 while (++idx != j)
5572 {
5573 h = sorted_sym_hash[idx];
5574 if (h->root.u.def.section != slook
5575 || h->root.u.def.value != vlook)
5576 break;
5577 }
5578
5579 /* Now look back over the aliases. Since we sorted by size
5580 as well as value and section, we'll choose the one with
5581 the largest size. */
5582 while (idx-- != i)
4ad4eba5 5583 {
14160578 5584 h = sorted_sym_hash[idx];
4ad4eba5
AM
5585
5586 /* Stop if value or section doesn't match. */
14160578
AM
5587 if (h->root.u.def.section != slook
5588 || h->root.u.def.value != vlook)
4ad4eba5
AM
5589 break;
5590 else if (h != hlook)
5591 {
60d67dc8
AM
5592 struct elf_link_hash_entry *t;
5593
5594 hlook->u.alias = h;
5595 hlook->is_weakalias = 1;
5596 t = h;
5597 if (t->u.alias != NULL)
5598 while (t->u.alias != h)
5599 t = t->u.alias;
5600 t->u.alias = hlook;
4ad4eba5
AM
5601
5602 /* If the weak definition is in the list of dynamic
5603 symbols, make sure the real definition is put
5604 there as well. */
5605 if (hlook->dynindx != -1 && h->dynindx == -1)
5606 {
c152c796 5607 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
5608 {
5609 err_free_sym_hash:
5610 free (sorted_sym_hash);
5611 goto error_return;
5612 }
4ad4eba5
AM
5613 }
5614
5615 /* If the real definition is in the list of dynamic
5616 symbols, make sure the weak definition is put
5617 there as well. If we don't do this, then the
5618 dynamic loader might not merge the entries for the
5619 real definition and the weak definition. */
5620 if (h->dynindx != -1 && hlook->dynindx == -1)
5621 {
c152c796 5622 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 5623 goto err_free_sym_hash;
4ad4eba5
AM
5624 }
5625 break;
5626 }
5627 }
5628 }
5629
5630 free (sorted_sym_hash);
5631 }
5632
33177bb1
AM
5633 if (bed->check_directives
5634 && !(*bed->check_directives) (abfd, info))
5635 return FALSE;
85fbca6a 5636
4ad4eba5
AM
5637 /* If this is a non-traditional link, try to optimize the handling
5638 of the .stab/.stabstr sections. */
5639 if (! dynamic
5640 && ! info->traditional_format
66eb6687 5641 && is_elf_hash_table (htab)
4ad4eba5
AM
5642 && (info->strip != strip_all && info->strip != strip_debugger))
5643 {
5644 asection *stabstr;
5645
5646 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5647 if (stabstr != NULL)
5648 {
5649 bfd_size_type string_offset = 0;
5650 asection *stab;
5651
5652 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 5653 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
5654 && (!stab->name[5] ||
5655 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5656 && (stab->flags & SEC_MERGE) == 0
5657 && !bfd_is_abs_section (stab->output_section))
5658 {
5659 struct bfd_elf_section_data *secdata;
5660
5661 secdata = elf_section_data (stab);
66eb6687
AM
5662 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5663 stabstr, &secdata->sec_info,
4ad4eba5
AM
5664 &string_offset))
5665 goto error_return;
5666 if (secdata->sec_info)
dbaa2011 5667 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5668 }
5669 }
5670 }
5671
e310298c 5672 if (dynamic && add_needed)
4ad4eba5
AM
5673 {
5674 /* Add this bfd to the loaded list. */
5675 struct elf_link_loaded_list *n;
5676
ca4be51c 5677 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5678 if (n == NULL)
5679 goto error_return;
5680 n->abfd = abfd;
e310298c
AM
5681 n->next = htab->dyn_loaded;
5682 htab->dyn_loaded = n;
4ad4eba5 5683 }
e310298c
AM
5684 if (dynamic && !add_needed
5685 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
5686 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
4ad4eba5
AM
5687
5688 return TRUE;
5689
5690 error_free_vers:
c9594989
AM
5691 free (old_tab);
5692 free (old_strtab);
5693 free (nondeflt_vers);
5694 free (extversym);
4ad4eba5 5695 error_free_sym:
c9594989 5696 free (isymbuf);
4ad4eba5
AM
5697 error_return:
5698 return FALSE;
5699}
5700
8387904d
AM
5701/* Return the linker hash table entry of a symbol that might be
5702 satisfied by an archive symbol. Return -1 on error. */
5703
5704struct elf_link_hash_entry *
5705_bfd_elf_archive_symbol_lookup (bfd *abfd,
5706 struct bfd_link_info *info,
5707 const char *name)
5708{
5709 struct elf_link_hash_entry *h;
5710 char *p, *copy;
5711 size_t len, first;
5712
2a41f396 5713 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5714 if (h != NULL)
5715 return h;
5716
5717 /* If this is a default version (the name contains @@), look up the
5718 symbol again with only one `@' as well as without the version.
5719 The effect is that references to the symbol with and without the
5720 version will be matched by the default symbol in the archive. */
5721
5722 p = strchr (name, ELF_VER_CHR);
5723 if (p == NULL || p[1] != ELF_VER_CHR)
5724 return h;
5725
5726 /* First check with only one `@'. */
5727 len = strlen (name);
a50b1753 5728 copy = (char *) bfd_alloc (abfd, len);
8387904d 5729 if (copy == NULL)
e99955cd 5730 return (struct elf_link_hash_entry *) -1;
8387904d
AM
5731
5732 first = p - name + 1;
5733 memcpy (copy, name, first);
5734 memcpy (copy + first, name + first + 1, len - first);
5735
2a41f396 5736 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5737 if (h == NULL)
5738 {
5739 /* We also need to check references to the symbol without the
5740 version. */
5741 copy[first - 1] = '\0';
5742 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5743 FALSE, FALSE, TRUE);
8387904d
AM
5744 }
5745
5746 bfd_release (abfd, copy);
5747 return h;
5748}
5749
0ad989f9 5750/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5751 don't use _bfd_generic_link_add_archive_symbols because we need to
5752 handle versioned symbols.
0ad989f9
L
5753
5754 Fortunately, ELF archive handling is simpler than that done by
5755 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5756 oddities. In ELF, if we find a symbol in the archive map, and the
5757 symbol is currently undefined, we know that we must pull in that
5758 object file.
5759
5760 Unfortunately, we do have to make multiple passes over the symbol
5761 table until nothing further is resolved. */
5762
4ad4eba5
AM
5763static bfd_boolean
5764elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5765{
5766 symindex c;
13e570f8 5767 unsigned char *included = NULL;
0ad989f9
L
5768 carsym *symdefs;
5769 bfd_boolean loop;
986f0783 5770 size_t amt;
8387904d
AM
5771 const struct elf_backend_data *bed;
5772 struct elf_link_hash_entry * (*archive_symbol_lookup)
5773 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5774
5775 if (! bfd_has_map (abfd))
5776 {
5777 /* An empty archive is a special case. */
5778 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5779 return TRUE;
5780 bfd_set_error (bfd_error_no_armap);
5781 return FALSE;
5782 }
5783
5784 /* Keep track of all symbols we know to be already defined, and all
5785 files we know to be already included. This is to speed up the
5786 second and subsequent passes. */
5787 c = bfd_ardata (abfd)->symdef_count;
5788 if (c == 0)
5789 return TRUE;
986f0783 5790 amt = c * sizeof (*included);
13e570f8
AM
5791 included = (unsigned char *) bfd_zmalloc (amt);
5792 if (included == NULL)
5793 return FALSE;
0ad989f9
L
5794
5795 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5796 bed = get_elf_backend_data (abfd);
5797 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5798
5799 do
5800 {
5801 file_ptr last;
5802 symindex i;
5803 carsym *symdef;
5804 carsym *symdefend;
5805
5806 loop = FALSE;
5807 last = -1;
5808
5809 symdef = symdefs;
5810 symdefend = symdef + c;
5811 for (i = 0; symdef < symdefend; symdef++, i++)
5812 {
5813 struct elf_link_hash_entry *h;
5814 bfd *element;
5815 struct bfd_link_hash_entry *undefs_tail;
5816 symindex mark;
5817
13e570f8 5818 if (included[i])
0ad989f9
L
5819 continue;
5820 if (symdef->file_offset == last)
5821 {
5822 included[i] = TRUE;
5823 continue;
5824 }
5825
8387904d 5826 h = archive_symbol_lookup (abfd, info, symdef->name);
e99955cd 5827 if (h == (struct elf_link_hash_entry *) -1)
8387904d 5828 goto error_return;
0ad989f9
L
5829
5830 if (h == NULL)
5831 continue;
5832
75cfe082
AM
5833 if (h->root.type == bfd_link_hash_undefined)
5834 {
5835 /* If the archive element has already been loaded then one
5836 of the symbols defined by that element might have been
5837 made undefined due to being in a discarded section. */
5838 if (h->indx == -3)
5839 continue;
5840 }
5841 else if (h->root.type == bfd_link_hash_common)
0ad989f9
L
5842 {
5843 /* We currently have a common symbol. The archive map contains
5844 a reference to this symbol, so we may want to include it. We
5845 only want to include it however, if this archive element
5846 contains a definition of the symbol, not just another common
5847 declaration of it.
5848
5849 Unfortunately some archivers (including GNU ar) will put
5850 declarations of common symbols into their archive maps, as
5851 well as real definitions, so we cannot just go by the archive
5852 map alone. Instead we must read in the element's symbol
5853 table and check that to see what kind of symbol definition
5854 this is. */
5855 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5856 continue;
5857 }
75cfe082 5858 else
0ad989f9
L
5859 {
5860 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5861 /* Symbol must be defined. Don't check it again. */
5862 included[i] = TRUE;
0ad989f9
L
5863 continue;
5864 }
5865
5866 /* We need to include this archive member. */
5867 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5868 if (element == NULL)
5869 goto error_return;
5870
5871 if (! bfd_check_format (element, bfd_object))
5872 goto error_return;
5873
0ad989f9
L
5874 undefs_tail = info->hash->undefs_tail;
5875
0e144ba7
AM
5876 if (!(*info->callbacks
5877 ->add_archive_element) (info, element, symdef->name, &element))
b95a0a31 5878 continue;
0e144ba7 5879 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5880 goto error_return;
5881
5882 /* If there are any new undefined symbols, we need to make
5883 another pass through the archive in order to see whether
5884 they can be defined. FIXME: This isn't perfect, because
5885 common symbols wind up on undefs_tail and because an
5886 undefined symbol which is defined later on in this pass
5887 does not require another pass. This isn't a bug, but it
5888 does make the code less efficient than it could be. */
5889 if (undefs_tail != info->hash->undefs_tail)
5890 loop = TRUE;
5891
5892 /* Look backward to mark all symbols from this object file
5893 which we have already seen in this pass. */
5894 mark = i;
5895 do
5896 {
5897 included[mark] = TRUE;
5898 if (mark == 0)
5899 break;
5900 --mark;
5901 }
5902 while (symdefs[mark].file_offset == symdef->file_offset);
5903
5904 /* We mark subsequent symbols from this object file as we go
5905 on through the loop. */
5906 last = symdef->file_offset;
5907 }
5908 }
5909 while (loop);
5910
0ad989f9 5911 free (included);
0ad989f9
L
5912 return TRUE;
5913
5914 error_return:
c9594989 5915 free (included);
0ad989f9
L
5916 return FALSE;
5917}
4ad4eba5
AM
5918
5919/* Given an ELF BFD, add symbols to the global hash table as
5920 appropriate. */
5921
5922bfd_boolean
5923bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5924{
5925 switch (bfd_get_format (abfd))
5926 {
5927 case bfd_object:
5928 return elf_link_add_object_symbols (abfd, info);
5929 case bfd_archive:
5930 return elf_link_add_archive_symbols (abfd, info);
5931 default:
5932 bfd_set_error (bfd_error_wrong_format);
5933 return FALSE;
5934 }
5935}
5a580b3a 5936\f
14b1c01e
AM
5937struct hash_codes_info
5938{
5939 unsigned long *hashcodes;
5940 bfd_boolean error;
5941};
a0c8462f 5942
5a580b3a
AM
5943/* This function will be called though elf_link_hash_traverse to store
5944 all hash value of the exported symbols in an array. */
5945
5946static bfd_boolean
5947elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5948{
a50b1753 5949 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5950 const char *name;
5a580b3a
AM
5951 unsigned long ha;
5952 char *alc = NULL;
5953
5a580b3a
AM
5954 /* Ignore indirect symbols. These are added by the versioning code. */
5955 if (h->dynindx == -1)
5956 return TRUE;
5957
5958 name = h->root.root.string;
422f1182 5959 if (h->versioned >= versioned)
5a580b3a 5960 {
422f1182
L
5961 char *p = strchr (name, ELF_VER_CHR);
5962 if (p != NULL)
14b1c01e 5963 {
422f1182
L
5964 alc = (char *) bfd_malloc (p - name + 1);
5965 if (alc == NULL)
5966 {
5967 inf->error = TRUE;
5968 return FALSE;
5969 }
5970 memcpy (alc, name, p - name);
5971 alc[p - name] = '\0';
5972 name = alc;
14b1c01e 5973 }
5a580b3a
AM
5974 }
5975
5976 /* Compute the hash value. */
5977 ha = bfd_elf_hash (name);
5978
5979 /* Store the found hash value in the array given as the argument. */
14b1c01e 5980 *(inf->hashcodes)++ = ha;
5a580b3a
AM
5981
5982 /* And store it in the struct so that we can put it in the hash table
5983 later. */
f6e332e6 5984 h->u.elf_hash_value = ha;
5a580b3a 5985
c9594989 5986 free (alc);
5a580b3a
AM
5987 return TRUE;
5988}
5989
fdc90cb4
JJ
5990struct collect_gnu_hash_codes
5991{
5992 bfd *output_bfd;
5993 const struct elf_backend_data *bed;
5994 unsigned long int nsyms;
5995 unsigned long int maskbits;
5996 unsigned long int *hashcodes;
5997 unsigned long int *hashval;
5998 unsigned long int *indx;
5999 unsigned long int *counts;
6000 bfd_vma *bitmask;
6001 bfd_byte *contents;
f16a9783 6002 bfd_size_type xlat;
fdc90cb4
JJ
6003 long int min_dynindx;
6004 unsigned long int bucketcount;
6005 unsigned long int symindx;
6006 long int local_indx;
6007 long int shift1, shift2;
6008 unsigned long int mask;
14b1c01e 6009 bfd_boolean error;
fdc90cb4
JJ
6010};
6011
6012/* This function will be called though elf_link_hash_traverse to store
6013 all hash value of the exported symbols in an array. */
6014
6015static bfd_boolean
6016elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6017{
a50b1753 6018 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 6019 const char *name;
fdc90cb4
JJ
6020 unsigned long ha;
6021 char *alc = NULL;
6022
fdc90cb4
JJ
6023 /* Ignore indirect symbols. These are added by the versioning code. */
6024 if (h->dynindx == -1)
6025 return TRUE;
6026
6027 /* Ignore also local symbols and undefined symbols. */
6028 if (! (*s->bed->elf_hash_symbol) (h))
6029 return TRUE;
6030
6031 name = h->root.root.string;
422f1182 6032 if (h->versioned >= versioned)
fdc90cb4 6033 {
422f1182
L
6034 char *p = strchr (name, ELF_VER_CHR);
6035 if (p != NULL)
14b1c01e 6036 {
422f1182
L
6037 alc = (char *) bfd_malloc (p - name + 1);
6038 if (alc == NULL)
6039 {
6040 s->error = TRUE;
6041 return FALSE;
6042 }
6043 memcpy (alc, name, p - name);
6044 alc[p - name] = '\0';
6045 name = alc;
14b1c01e 6046 }
fdc90cb4
JJ
6047 }
6048
6049 /* Compute the hash value. */
6050 ha = bfd_elf_gnu_hash (name);
6051
6052 /* Store the found hash value in the array for compute_bucket_count,
6053 and also for .dynsym reordering purposes. */
6054 s->hashcodes[s->nsyms] = ha;
6055 s->hashval[h->dynindx] = ha;
6056 ++s->nsyms;
6057 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6058 s->min_dynindx = h->dynindx;
6059
c9594989 6060 free (alc);
fdc90cb4
JJ
6061 return TRUE;
6062}
6063
6064/* This function will be called though elf_link_hash_traverse to do
f16a9783
MS
6065 final dynamic symbol renumbering in case of .gnu.hash.
6066 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6067 to the translation table. */
fdc90cb4
JJ
6068
6069static bfd_boolean
f16a9783 6070elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
fdc90cb4 6071{
a50b1753 6072 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
6073 unsigned long int bucket;
6074 unsigned long int val;
6075
fdc90cb4
JJ
6076 /* Ignore indirect symbols. */
6077 if (h->dynindx == -1)
6078 return TRUE;
6079
6080 /* Ignore also local symbols and undefined symbols. */
6081 if (! (*s->bed->elf_hash_symbol) (h))
6082 {
6083 if (h->dynindx >= s->min_dynindx)
f16a9783
MS
6084 {
6085 if (s->bed->record_xhash_symbol != NULL)
6086 {
6087 (*s->bed->record_xhash_symbol) (h, 0);
6088 s->local_indx++;
6089 }
6090 else
6091 h->dynindx = s->local_indx++;
6092 }
fdc90cb4
JJ
6093 return TRUE;
6094 }
6095
6096 bucket = s->hashval[h->dynindx] % s->bucketcount;
6097 val = (s->hashval[h->dynindx] >> s->shift1)
6098 & ((s->maskbits >> s->shift1) - 1);
6099 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6100 s->bitmask[val]
6101 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6102 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6103 if (s->counts[bucket] == 1)
6104 /* Last element terminates the chain. */
6105 val |= 1;
6106 bfd_put_32 (s->output_bfd, val,
6107 s->contents + (s->indx[bucket] - s->symindx) * 4);
6108 --s->counts[bucket];
f16a9783
MS
6109 if (s->bed->record_xhash_symbol != NULL)
6110 {
6111 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6112
6113 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6114 }
6115 else
6116 h->dynindx = s->indx[bucket]++;
fdc90cb4
JJ
6117 return TRUE;
6118}
6119
6120/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6121
6122bfd_boolean
6123_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6124{
6125 return !(h->forced_local
6126 || h->root.type == bfd_link_hash_undefined
6127 || h->root.type == bfd_link_hash_undefweak
6128 || ((h->root.type == bfd_link_hash_defined
6129 || h->root.type == bfd_link_hash_defweak)
6130 && h->root.u.def.section->output_section == NULL));
6131}
6132
5a580b3a
AM
6133/* Array used to determine the number of hash table buckets to use
6134 based on the number of symbols there are. If there are fewer than
6135 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6136 fewer than 37 we use 17 buckets, and so forth. We never use more
6137 than 32771 buckets. */
6138
6139static const size_t elf_buckets[] =
6140{
6141 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6142 16411, 32771, 0
6143};
6144
6145/* Compute bucket count for hashing table. We do not use a static set
6146 of possible tables sizes anymore. Instead we determine for all
6147 possible reasonable sizes of the table the outcome (i.e., the
6148 number of collisions etc) and choose the best solution. The
6149 weighting functions are not too simple to allow the table to grow
6150 without bounds. Instead one of the weighting factors is the size.
6151 Therefore the result is always a good payoff between few collisions
6152 (= short chain lengths) and table size. */
6153static size_t
b20dd2ce 6154compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
6155 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6156 unsigned long int nsyms,
6157 int gnu_hash)
5a580b3a 6158{
5a580b3a 6159 size_t best_size = 0;
5a580b3a 6160 unsigned long int i;
5a580b3a 6161
5a580b3a
AM
6162 /* We have a problem here. The following code to optimize the table
6163 size requires an integer type with more the 32 bits. If
6164 BFD_HOST_U_64_BIT is set we know about such a type. */
6165#ifdef BFD_HOST_U_64_BIT
6166 if (info->optimize)
6167 {
5a580b3a
AM
6168 size_t minsize;
6169 size_t maxsize;
6170 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 6171 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 6172 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 6173 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 6174 unsigned long int *counts;
d40f3da9 6175 bfd_size_type amt;
0883b6e0 6176 unsigned int no_improvement_count = 0;
5a580b3a
AM
6177
6178 /* Possible optimization parameters: if we have NSYMS symbols we say
6179 that the hashing table must at least have NSYMS/4 and at most
6180 2*NSYMS buckets. */
6181 minsize = nsyms / 4;
6182 if (minsize == 0)
6183 minsize = 1;
6184 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
6185 if (gnu_hash)
6186 {
6187 if (minsize < 2)
6188 minsize = 2;
6189 if ((best_size & 31) == 0)
6190 ++best_size;
6191 }
5a580b3a
AM
6192
6193 /* Create array where we count the collisions in. We must use bfd_malloc
6194 since the size could be large. */
6195 amt = maxsize;
6196 amt *= sizeof (unsigned long int);
a50b1753 6197 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 6198 if (counts == NULL)
fdc90cb4 6199 return 0;
5a580b3a
AM
6200
6201 /* Compute the "optimal" size for the hash table. The criteria is a
6202 minimal chain length. The minor criteria is (of course) the size
6203 of the table. */
6204 for (i = minsize; i < maxsize; ++i)
6205 {
6206 /* Walk through the array of hashcodes and count the collisions. */
6207 BFD_HOST_U_64_BIT max;
6208 unsigned long int j;
6209 unsigned long int fact;
6210
fdc90cb4
JJ
6211 if (gnu_hash && (i & 31) == 0)
6212 continue;
6213
5a580b3a
AM
6214 memset (counts, '\0', i * sizeof (unsigned long int));
6215
6216 /* Determine how often each hash bucket is used. */
6217 for (j = 0; j < nsyms; ++j)
6218 ++counts[hashcodes[j] % i];
6219
6220 /* For the weight function we need some information about the
6221 pagesize on the target. This is information need not be 100%
6222 accurate. Since this information is not available (so far) we
6223 define it here to a reasonable default value. If it is crucial
6224 to have a better value some day simply define this value. */
6225# ifndef BFD_TARGET_PAGESIZE
6226# define BFD_TARGET_PAGESIZE (4096)
6227# endif
6228
fdc90cb4
JJ
6229 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6230 and the chains. */
6231 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
6232
6233# if 1
6234 /* Variant 1: optimize for short chains. We add the squares
6235 of all the chain lengths (which favors many small chain
6236 over a few long chains). */
6237 for (j = 0; j < i; ++j)
6238 max += counts[j] * counts[j];
6239
6240 /* This adds penalties for the overall size of the table. */
fdc90cb4 6241 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6242 max *= fact * fact;
6243# else
6244 /* Variant 2: Optimize a lot more for small table. Here we
6245 also add squares of the size but we also add penalties for
6246 empty slots (the +1 term). */
6247 for (j = 0; j < i; ++j)
6248 max += (1 + counts[j]) * (1 + counts[j]);
6249
6250 /* The overall size of the table is considered, but not as
6251 strong as in variant 1, where it is squared. */
fdc90cb4 6252 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6253 max *= fact;
6254# endif
6255
6256 /* Compare with current best results. */
6257 if (max < best_chlen)
6258 {
6259 best_chlen = max;
6260 best_size = i;
ca4be51c 6261 no_improvement_count = 0;
5a580b3a 6262 }
0883b6e0
NC
6263 /* PR 11843: Avoid futile long searches for the best bucket size
6264 when there are a large number of symbols. */
6265 else if (++no_improvement_count == 100)
6266 break;
5a580b3a
AM
6267 }
6268
6269 free (counts);
6270 }
6271 else
6272#endif /* defined (BFD_HOST_U_64_BIT) */
6273 {
6274 /* This is the fallback solution if no 64bit type is available or if we
6275 are not supposed to spend much time on optimizations. We select the
6276 bucket count using a fixed set of numbers. */
6277 for (i = 0; elf_buckets[i] != 0; i++)
6278 {
6279 best_size = elf_buckets[i];
fdc90cb4 6280 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
6281 break;
6282 }
fdc90cb4
JJ
6283 if (gnu_hash && best_size < 2)
6284 best_size = 2;
5a580b3a
AM
6285 }
6286
5a580b3a
AM
6287 return best_size;
6288}
6289
d0bf826b
AM
6290/* Size any SHT_GROUP section for ld -r. */
6291
6292bfd_boolean
6293_bfd_elf_size_group_sections (struct bfd_link_info *info)
6294{
6295 bfd *ibfd;
57963c05 6296 asection *s;
d0bf826b 6297
c72f2fb2 6298 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b 6299 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
57963c05
AM
6300 && (s = ibfd->sections) != NULL
6301 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
d0bf826b
AM
6302 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6303 return FALSE;
6304 return TRUE;
6305}
6306
04c3a755
NS
6307/* Set a default stack segment size. The value in INFO wins. If it
6308 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6309 undefined it is initialized. */
6310
6311bfd_boolean
6312bfd_elf_stack_segment_size (bfd *output_bfd,
6313 struct bfd_link_info *info,
6314 const char *legacy_symbol,
6315 bfd_vma default_size)
6316{
6317 struct elf_link_hash_entry *h = NULL;
6318
6319 /* Look for legacy symbol. */
6320 if (legacy_symbol)
6321 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6322 FALSE, FALSE, FALSE);
6323 if (h && (h->root.type == bfd_link_hash_defined
6324 || h->root.type == bfd_link_hash_defweak)
6325 && h->def_regular
6326 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6327 {
6328 /* The symbol has no type if specified on the command line. */
6329 h->type = STT_OBJECT;
6330 if (info->stacksize)
695344c0 6331 /* xgettext:c-format */
871b3ab2 6332 _bfd_error_handler (_("%pB: stack size specified and %s set"),
4eca0228 6333 output_bfd, legacy_symbol);
04c3a755 6334 else if (h->root.u.def.section != bfd_abs_section_ptr)
695344c0 6335 /* xgettext:c-format */
871b3ab2 6336 _bfd_error_handler (_("%pB: %s not absolute"),
4eca0228 6337 output_bfd, legacy_symbol);
04c3a755
NS
6338 else
6339 info->stacksize = h->root.u.def.value;
6340 }
6341
6342 if (!info->stacksize)
6343 /* If the user didn't set a size, or explicitly inhibit the
6344 size, set it now. */
6345 info->stacksize = default_size;
6346
6347 /* Provide the legacy symbol, if it is referenced. */
6348 if (h && (h->root.type == bfd_link_hash_undefined
6349 || h->root.type == bfd_link_hash_undefweak))
6350 {
6351 struct bfd_link_hash_entry *bh = NULL;
6352
6353 if (!(_bfd_generic_link_add_one_symbol
6354 (info, output_bfd, legacy_symbol,
6355 BSF_GLOBAL, bfd_abs_section_ptr,
6356 info->stacksize >= 0 ? info->stacksize : 0,
6357 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6358 return FALSE;
6359
6360 h = (struct elf_link_hash_entry *) bh;
6361 h->def_regular = 1;
6362 h->type = STT_OBJECT;
6363 }
6364
6365 return TRUE;
6366}
6367
b531344c
MR
6368/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6369
6370struct elf_gc_sweep_symbol_info
6371{
6372 struct bfd_link_info *info;
6373 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6374 bfd_boolean);
6375};
6376
6377static bfd_boolean
6378elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6379{
6380 if (!h->mark
6381 && (((h->root.type == bfd_link_hash_defined
6382 || h->root.type == bfd_link_hash_defweak)
6383 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6384 && h->root.u.def.section->gc_mark))
6385 || h->root.type == bfd_link_hash_undefined
6386 || h->root.type == bfd_link_hash_undefweak))
6387 {
6388 struct elf_gc_sweep_symbol_info *inf;
6389
6390 inf = (struct elf_gc_sweep_symbol_info *) data;
6391 (*inf->hide_symbol) (inf->info, h, TRUE);
6392 h->def_regular = 0;
6393 h->ref_regular = 0;
6394 h->ref_regular_nonweak = 0;
6395 }
6396
6397 return TRUE;
6398}
6399
5a580b3a
AM
6400/* Set up the sizes and contents of the ELF dynamic sections. This is
6401 called by the ELF linker emulation before_allocation routine. We
6402 must set the sizes of the sections before the linker sets the
6403 addresses of the various sections. */
6404
6405bfd_boolean
6406bfd_elf_size_dynamic_sections (bfd *output_bfd,
6407 const char *soname,
6408 const char *rpath,
6409 const char *filter_shlib,
7ee314fa
AM
6410 const char *audit,
6411 const char *depaudit,
5a580b3a
AM
6412 const char * const *auxiliary_filters,
6413 struct bfd_link_info *info,
fd91d419 6414 asection **sinterpptr)
5a580b3a 6415{
5a580b3a
AM
6416 bfd *dynobj;
6417 const struct elf_backend_data *bed;
5a580b3a
AM
6418
6419 *sinterpptr = NULL;
6420
5a580b3a
AM
6421 if (!is_elf_hash_table (info->hash))
6422 return TRUE;
6423
5a580b3a
AM
6424 dynobj = elf_hash_table (info)->dynobj;
6425
9a2a56cc 6426 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 6427 {
902e9fc7
MR
6428 struct bfd_elf_version_tree *verdefs;
6429 struct elf_info_failed asvinfo;
5a580b3a
AM
6430 struct bfd_elf_version_tree *t;
6431 struct bfd_elf_version_expr *d;
902e9fc7 6432 asection *s;
e6699019 6433 size_t soname_indx;
7ee314fa 6434
5a580b3a
AM
6435 /* If we are supposed to export all symbols into the dynamic symbol
6436 table (this is not the normal case), then do so. */
55255dae 6437 if (info->export_dynamic
0e1862bb 6438 || (bfd_link_executable (info) && info->dynamic))
5a580b3a 6439 {
3d13f3e9
AM
6440 struct elf_info_failed eif;
6441
6442 eif.info = info;
6443 eif.failed = FALSE;
5a580b3a
AM
6444 elf_link_hash_traverse (elf_hash_table (info),
6445 _bfd_elf_export_symbol,
6446 &eif);
6447 if (eif.failed)
6448 return FALSE;
6449 }
6450
e6699019
L
6451 if (soname != NULL)
6452 {
6453 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6454 soname, TRUE);
6455 if (soname_indx == (size_t) -1
6456 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6457 return FALSE;
6458 }
6459 else
6460 soname_indx = (size_t) -1;
6461
5a580b3a 6462 /* Make all global versions with definition. */
fd91d419 6463 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6464 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6465 if (!d->symver && d->literal)
5a580b3a
AM
6466 {
6467 const char *verstr, *name;
6468 size_t namelen, verlen, newlen;
93252b1c 6469 char *newname, *p, leading_char;
5a580b3a
AM
6470 struct elf_link_hash_entry *newh;
6471
93252b1c 6472 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 6473 name = d->pattern;
93252b1c 6474 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
6475 verstr = t->name;
6476 verlen = strlen (verstr);
6477 newlen = namelen + verlen + 3;
6478
a50b1753 6479 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
6480 if (newname == NULL)
6481 return FALSE;
93252b1c
MF
6482 newname[0] = leading_char;
6483 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
6484
6485 /* Check the hidden versioned definition. */
6486 p = newname + namelen;
6487 *p++ = ELF_VER_CHR;
6488 memcpy (p, verstr, verlen + 1);
6489 newh = elf_link_hash_lookup (elf_hash_table (info),
6490 newname, FALSE, FALSE,
6491 FALSE);
6492 if (newh == NULL
6493 || (newh->root.type != bfd_link_hash_defined
6494 && newh->root.type != bfd_link_hash_defweak))
6495 {
6496 /* Check the default versioned definition. */
6497 *p++ = ELF_VER_CHR;
6498 memcpy (p, verstr, verlen + 1);
6499 newh = elf_link_hash_lookup (elf_hash_table (info),
6500 newname, FALSE, FALSE,
6501 FALSE);
6502 }
6503 free (newname);
6504
6505 /* Mark this version if there is a definition and it is
6506 not defined in a shared object. */
6507 if (newh != NULL
f5385ebf 6508 && !newh->def_dynamic
5a580b3a
AM
6509 && (newh->root.type == bfd_link_hash_defined
6510 || newh->root.type == bfd_link_hash_defweak))
6511 d->symver = 1;
6512 }
6513
6514 /* Attach all the symbols to their version information. */
5a580b3a 6515 asvinfo.info = info;
5a580b3a
AM
6516 asvinfo.failed = FALSE;
6517
6518 elf_link_hash_traverse (elf_hash_table (info),
6519 _bfd_elf_link_assign_sym_version,
6520 &asvinfo);
6521 if (asvinfo.failed)
6522 return FALSE;
6523
6524 if (!info->allow_undefined_version)
6525 {
6526 /* Check if all global versions have a definition. */
3d13f3e9 6527 bfd_boolean all_defined = TRUE;
fd91d419 6528 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6529 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6530 if (d->literal && !d->symver && !d->script)
5a580b3a 6531 {
4eca0228 6532 _bfd_error_handler
5a580b3a
AM
6533 (_("%s: undefined version: %s"),
6534 d->pattern, t->name);
6535 all_defined = FALSE;
6536 }
6537
6538 if (!all_defined)
6539 {
6540 bfd_set_error (bfd_error_bad_value);
6541 return FALSE;
6542 }
6543 }
6544
902e9fc7
MR
6545 /* Set up the version definition section. */
6546 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6547 BFD_ASSERT (s != NULL);
5a580b3a 6548
902e9fc7
MR
6549 /* We may have created additional version definitions if we are
6550 just linking a regular application. */
6551 verdefs = info->version_info;
5a580b3a 6552
902e9fc7
MR
6553 /* Skip anonymous version tag. */
6554 if (verdefs != NULL && verdefs->vernum == 0)
6555 verdefs = verdefs->next;
5a580b3a 6556
902e9fc7
MR
6557 if (verdefs == NULL && !info->create_default_symver)
6558 s->flags |= SEC_EXCLUDE;
6559 else
5a580b3a 6560 {
902e9fc7
MR
6561 unsigned int cdefs;
6562 bfd_size_type size;
6563 bfd_byte *p;
6564 Elf_Internal_Verdef def;
6565 Elf_Internal_Verdaux defaux;
6566 struct bfd_link_hash_entry *bh;
6567 struct elf_link_hash_entry *h;
6568 const char *name;
5a580b3a 6569
902e9fc7
MR
6570 cdefs = 0;
6571 size = 0;
5a580b3a 6572
902e9fc7
MR
6573 /* Make space for the base version. */
6574 size += sizeof (Elf_External_Verdef);
6575 size += sizeof (Elf_External_Verdaux);
6576 ++cdefs;
6577
6578 /* Make space for the default version. */
6579 if (info->create_default_symver)
6580 {
6581 size += sizeof (Elf_External_Verdef);
6582 ++cdefs;
3e3b46e5
PB
6583 }
6584
5a580b3a
AM
6585 for (t = verdefs; t != NULL; t = t->next)
6586 {
6587 struct bfd_elf_version_deps *n;
6588
a6cc6b3b
RO
6589 /* Don't emit base version twice. */
6590 if (t->vernum == 0)
6591 continue;
6592
5a580b3a
AM
6593 size += sizeof (Elf_External_Verdef);
6594 size += sizeof (Elf_External_Verdaux);
6595 ++cdefs;
6596
6597 for (n = t->deps; n != NULL; n = n->next)
6598 size += sizeof (Elf_External_Verdaux);
6599 }
6600
eea6121a 6601 s->size = size;
a50b1753 6602 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6603 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6604 return FALSE;
6605
6606 /* Fill in the version definition section. */
6607
6608 p = s->contents;
6609
6610 def.vd_version = VER_DEF_CURRENT;
6611 def.vd_flags = VER_FLG_BASE;
6612 def.vd_ndx = 1;
6613 def.vd_cnt = 1;
3e3b46e5
PB
6614 if (info->create_default_symver)
6615 {
6616 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6617 def.vd_next = sizeof (Elf_External_Verdef);
6618 }
6619 else
6620 {
6621 def.vd_aux = sizeof (Elf_External_Verdef);
6622 def.vd_next = (sizeof (Elf_External_Verdef)
6623 + sizeof (Elf_External_Verdaux));
6624 }
5a580b3a 6625
ef53be89 6626 if (soname_indx != (size_t) -1)
5a580b3a
AM
6627 {
6628 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6629 soname_indx);
6630 def.vd_hash = bfd_elf_hash (soname);
6631 defaux.vda_name = soname_indx;
3e3b46e5 6632 name = soname;
5a580b3a
AM
6633 }
6634 else
6635 {
ef53be89 6636 size_t indx;
5a580b3a 6637
765cf5f6 6638 name = lbasename (bfd_get_filename (output_bfd));
5a580b3a
AM
6639 def.vd_hash = bfd_elf_hash (name);
6640 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6641 name, FALSE);
ef53be89 6642 if (indx == (size_t) -1)
5a580b3a
AM
6643 return FALSE;
6644 defaux.vda_name = indx;
6645 }
6646 defaux.vda_next = 0;
6647
6648 _bfd_elf_swap_verdef_out (output_bfd, &def,
6649 (Elf_External_Verdef *) p);
6650 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6651 if (info->create_default_symver)
6652 {
6653 /* Add a symbol representing this version. */
6654 bh = NULL;
6655 if (! (_bfd_generic_link_add_one_symbol
6656 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6657 0, NULL, FALSE,
6658 get_elf_backend_data (dynobj)->collect, &bh)))
6659 return FALSE;
6660 h = (struct elf_link_hash_entry *) bh;
6661 h->non_elf = 0;
6662 h->def_regular = 1;
6663 h->type = STT_OBJECT;
6664 h->verinfo.vertree = NULL;
6665
6666 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6667 return FALSE;
6668
6669 /* Create a duplicate of the base version with the same
6670 aux block, but different flags. */
6671 def.vd_flags = 0;
6672 def.vd_ndx = 2;
6673 def.vd_aux = sizeof (Elf_External_Verdef);
6674 if (verdefs)
6675 def.vd_next = (sizeof (Elf_External_Verdef)
6676 + sizeof (Elf_External_Verdaux));
6677 else
6678 def.vd_next = 0;
6679 _bfd_elf_swap_verdef_out (output_bfd, &def,
6680 (Elf_External_Verdef *) p);
6681 p += sizeof (Elf_External_Verdef);
6682 }
5a580b3a
AM
6683 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6684 (Elf_External_Verdaux *) p);
6685 p += sizeof (Elf_External_Verdaux);
6686
6687 for (t = verdefs; t != NULL; t = t->next)
6688 {
6689 unsigned int cdeps;
6690 struct bfd_elf_version_deps *n;
5a580b3a 6691
a6cc6b3b
RO
6692 /* Don't emit the base version twice. */
6693 if (t->vernum == 0)
6694 continue;
6695
5a580b3a
AM
6696 cdeps = 0;
6697 for (n = t->deps; n != NULL; n = n->next)
6698 ++cdeps;
6699
6700 /* Add a symbol representing this version. */
6701 bh = NULL;
6702 if (! (_bfd_generic_link_add_one_symbol
6703 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6704 0, NULL, FALSE,
6705 get_elf_backend_data (dynobj)->collect, &bh)))
6706 return FALSE;
6707 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6708 h->non_elf = 0;
6709 h->def_regular = 1;
5a580b3a
AM
6710 h->type = STT_OBJECT;
6711 h->verinfo.vertree = t;
6712
c152c796 6713 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6714 return FALSE;
6715
6716 def.vd_version = VER_DEF_CURRENT;
6717 def.vd_flags = 0;
6718 if (t->globals.list == NULL
6719 && t->locals.list == NULL
6720 && ! t->used)
6721 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6722 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6723 def.vd_cnt = cdeps + 1;
6724 def.vd_hash = bfd_elf_hash (t->name);
6725 def.vd_aux = sizeof (Elf_External_Verdef);
6726 def.vd_next = 0;
a6cc6b3b
RO
6727
6728 /* If a basever node is next, it *must* be the last node in
6729 the chain, otherwise Verdef construction breaks. */
6730 if (t->next != NULL && t->next->vernum == 0)
6731 BFD_ASSERT (t->next->next == NULL);
6732
6733 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6734 def.vd_next = (sizeof (Elf_External_Verdef)
6735 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6736
6737 _bfd_elf_swap_verdef_out (output_bfd, &def,
6738 (Elf_External_Verdef *) p);
6739 p += sizeof (Elf_External_Verdef);
6740
6741 defaux.vda_name = h->dynstr_index;
6742 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6743 h->dynstr_index);
6744 defaux.vda_next = 0;
6745 if (t->deps != NULL)
6746 defaux.vda_next = sizeof (Elf_External_Verdaux);
6747 t->name_indx = defaux.vda_name;
6748
6749 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6750 (Elf_External_Verdaux *) p);
6751 p += sizeof (Elf_External_Verdaux);
6752
6753 for (n = t->deps; n != NULL; n = n->next)
6754 {
6755 if (n->version_needed == NULL)
6756 {
6757 /* This can happen if there was an error in the
6758 version script. */
6759 defaux.vda_name = 0;
6760 }
6761 else
6762 {
6763 defaux.vda_name = n->version_needed->name_indx;
6764 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6765 defaux.vda_name);
6766 }
6767 if (n->next == NULL)
6768 defaux.vda_next = 0;
6769 else
6770 defaux.vda_next = sizeof (Elf_External_Verdaux);
6771
6772 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6773 (Elf_External_Verdaux *) p);
6774 p += sizeof (Elf_External_Verdaux);
6775 }
6776 }
6777
5a580b3a
AM
6778 elf_tdata (output_bfd)->cverdefs = cdefs;
6779 }
902e9fc7
MR
6780 }
6781
6782 bed = get_elf_backend_data (output_bfd);
6783
6784 if (info->gc_sections && bed->can_gc_sections)
6785 {
6786 struct elf_gc_sweep_symbol_info sweep_info;
902e9fc7
MR
6787
6788 /* Remove the symbols that were in the swept sections from the
3d13f3e9 6789 dynamic symbol table. */
902e9fc7
MR
6790 sweep_info.info = info;
6791 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6792 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6793 &sweep_info);
3d13f3e9
AM
6794 }
6795
6796 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6797 {
6798 asection *s;
6799 struct elf_find_verdep_info sinfo;
6800
6801 /* Work out the size of the version reference section. */
6802
6803 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6804 BFD_ASSERT (s != NULL);
902e9fc7 6805
3d13f3e9
AM
6806 sinfo.info = info;
6807 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6808 if (sinfo.vers == 0)
6809 sinfo.vers = 1;
6810 sinfo.failed = FALSE;
6811
6812 elf_link_hash_traverse (elf_hash_table (info),
6813 _bfd_elf_link_find_version_dependencies,
6814 &sinfo);
6815 if (sinfo.failed)
6816 return FALSE;
6817
6818 if (elf_tdata (output_bfd)->verref == NULL)
6819 s->flags |= SEC_EXCLUDE;
6820 else
6821 {
6822 Elf_Internal_Verneed *vn;
6823 unsigned int size;
6824 unsigned int crefs;
6825 bfd_byte *p;
6826
6827 /* Build the version dependency section. */
6828 size = 0;
6829 crefs = 0;
6830 for (vn = elf_tdata (output_bfd)->verref;
6831 vn != NULL;
6832 vn = vn->vn_nextref)
6833 {
6834 Elf_Internal_Vernaux *a;
6835
6836 size += sizeof (Elf_External_Verneed);
6837 ++crefs;
6838 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6839 size += sizeof (Elf_External_Vernaux);
6840 }
6841
6842 s->size = size;
6843 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6844 if (s->contents == NULL)
6845 return FALSE;
6846
6847 p = s->contents;
6848 for (vn = elf_tdata (output_bfd)->verref;
6849 vn != NULL;
6850 vn = vn->vn_nextref)
6851 {
6852 unsigned int caux;
6853 Elf_Internal_Vernaux *a;
6854 size_t indx;
6855
6856 caux = 0;
6857 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6858 ++caux;
6859
6860 vn->vn_version = VER_NEED_CURRENT;
6861 vn->vn_cnt = caux;
6862 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6863 elf_dt_name (vn->vn_bfd) != NULL
6864 ? elf_dt_name (vn->vn_bfd)
765cf5f6
AM
6865 : lbasename (bfd_get_filename
6866 (vn->vn_bfd)),
3d13f3e9
AM
6867 FALSE);
6868 if (indx == (size_t) -1)
6869 return FALSE;
6870 vn->vn_file = indx;
6871 vn->vn_aux = sizeof (Elf_External_Verneed);
6872 if (vn->vn_nextref == NULL)
6873 vn->vn_next = 0;
6874 else
6875 vn->vn_next = (sizeof (Elf_External_Verneed)
6876 + caux * sizeof (Elf_External_Vernaux));
6877
6878 _bfd_elf_swap_verneed_out (output_bfd, vn,
6879 (Elf_External_Verneed *) p);
6880 p += sizeof (Elf_External_Verneed);
6881
6882 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6883 {
6884 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6885 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6886 a->vna_nodename, FALSE);
6887 if (indx == (size_t) -1)
6888 return FALSE;
6889 a->vna_name = indx;
6890 if (a->vna_nextptr == NULL)
6891 a->vna_next = 0;
6892 else
6893 a->vna_next = sizeof (Elf_External_Vernaux);
6894
6895 _bfd_elf_swap_vernaux_out (output_bfd, a,
6896 (Elf_External_Vernaux *) p);
6897 p += sizeof (Elf_External_Vernaux);
6898 }
6899 }
6900
6901 elf_tdata (output_bfd)->cverrefs = crefs;
6902 }
902e9fc7
MR
6903 }
6904
6905 /* Any syms created from now on start with -1 in
6906 got.refcount/offset and plt.refcount/offset. */
6907 elf_hash_table (info)->init_got_refcount
6908 = elf_hash_table (info)->init_got_offset;
6909 elf_hash_table (info)->init_plt_refcount
6910 = elf_hash_table (info)->init_plt_offset;
6911
6912 if (bfd_link_relocatable (info)
6913 && !_bfd_elf_size_group_sections (info))
6914 return FALSE;
6915
6916 /* The backend may have to create some sections regardless of whether
6917 we're dynamic or not. */
6918 if (bed->elf_backend_always_size_sections
6919 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6920 return FALSE;
6921
6922 /* Determine any GNU_STACK segment requirements, after the backend
6923 has had a chance to set a default segment size. */
6924 if (info->execstack)
6925 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6926 else if (info->noexecstack)
6927 elf_stack_flags (output_bfd) = PF_R | PF_W;
6928 else
6929 {
6930 bfd *inputobj;
6931 asection *notesec = NULL;
6932 int exec = 0;
6933
6934 for (inputobj = info->input_bfds;
6935 inputobj;
6936 inputobj = inputobj->link.next)
6937 {
6938 asection *s;
6939
6940 if (inputobj->flags
6941 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6942 continue;
57963c05
AM
6943 s = inputobj->sections;
6944 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6945 continue;
6946
902e9fc7
MR
6947 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6948 if (s)
6949 {
6950 if (s->flags & SEC_CODE)
6951 exec = PF_X;
6952 notesec = s;
6953 }
6954 else if (bed->default_execstack)
6955 exec = PF_X;
6956 }
6957 if (notesec || info->stacksize > 0)
6958 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6959 if (notesec && exec && bfd_link_relocatable (info)
6960 && notesec->output_section != bfd_abs_section_ptr)
6961 notesec->output_section->flags |= SEC_CODE;
6962 }
6963
6964 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6965 {
6966 struct elf_info_failed eif;
6967 struct elf_link_hash_entry *h;
6968 asection *dynstr;
6969 asection *s;
6970
6971 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6972 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6973
902e9fc7
MR
6974 if (info->symbolic)
6975 {
6976 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6977 return FALSE;
6978 info->flags |= DF_SYMBOLIC;
6979 }
6980
6981 if (rpath != NULL)
6982 {
6983 size_t indx;
6984 bfd_vma tag;
6985
6986 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6987 TRUE);
6988 if (indx == (size_t) -1)
6989 return FALSE;
6990
6991 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6992 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6993 return FALSE;
6994 }
6995
6996 if (filter_shlib != NULL)
6997 {
6998 size_t indx;
6999
7000 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7001 filter_shlib, TRUE);
7002 if (indx == (size_t) -1
7003 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
7004 return FALSE;
7005 }
7006
7007 if (auxiliary_filters != NULL)
7008 {
7009 const char * const *p;
7010
7011 for (p = auxiliary_filters; *p != NULL; p++)
7012 {
7013 size_t indx;
7014
7015 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7016 *p, TRUE);
7017 if (indx == (size_t) -1
7018 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7019 return FALSE;
7020 }
7021 }
7022
7023 if (audit != NULL)
7024 {
7025 size_t indx;
7026
7027 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7028 TRUE);
7029 if (indx == (size_t) -1
7030 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7031 return FALSE;
7032 }
7033
7034 if (depaudit != NULL)
7035 {
7036 size_t indx;
7037
7038 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7039 TRUE);
7040 if (indx == (size_t) -1
7041 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7042 return FALSE;
7043 }
7044
7045 eif.info = info;
7046 eif.failed = FALSE;
7047
7048 /* Find all symbols which were defined in a dynamic object and make
7049 the backend pick a reasonable value for them. */
7050 elf_link_hash_traverse (elf_hash_table (info),
7051 _bfd_elf_adjust_dynamic_symbol,
7052 &eif);
7053 if (eif.failed)
7054 return FALSE;
7055
7056 /* Add some entries to the .dynamic section. We fill in some of the
7057 values later, in bfd_elf_final_link, but we must add the entries
7058 now so that we know the final size of the .dynamic section. */
7059
7060 /* If there are initialization and/or finalization functions to
7061 call then add the corresponding DT_INIT/DT_FINI entries. */
7062 h = (info->init_function
7063 ? elf_link_hash_lookup (elf_hash_table (info),
7064 info->init_function, FALSE,
7065 FALSE, FALSE)
7066 : NULL);
7067 if (h != NULL
7068 && (h->ref_regular
7069 || h->def_regular))
7070 {
7071 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7072 return FALSE;
7073 }
7074 h = (info->fini_function
7075 ? elf_link_hash_lookup (elf_hash_table (info),
7076 info->fini_function, FALSE,
7077 FALSE, FALSE)
7078 : NULL);
7079 if (h != NULL
7080 && (h->ref_regular
7081 || h->def_regular))
7082 {
7083 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7084 return FALSE;
7085 }
7086
7087 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7088 if (s != NULL && s->linker_has_input)
7089 {
7090 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7091 if (! bfd_link_executable (info))
7092 {
7093 bfd *sub;
7094 asection *o;
7095
57963c05
AM
7096 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7097 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7098 && (o = sub->sections) != NULL
7099 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
902e9fc7
MR
7100 for (o = sub->sections; o != NULL; o = o->next)
7101 if (elf_section_data (o)->this_hdr.sh_type
7102 == SHT_PREINIT_ARRAY)
7103 {
7104 _bfd_error_handler
871b3ab2 7105 (_("%pB: .preinit_array section is not allowed in DSO"),
902e9fc7
MR
7106 sub);
7107 break;
7108 }
7109
7110 bfd_set_error (bfd_error_nonrepresentable_section);
7111 return FALSE;
7112 }
7113
7114 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7115 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7116 return FALSE;
7117 }
7118 s = bfd_get_section_by_name (output_bfd, ".init_array");
7119 if (s != NULL && s->linker_has_input)
7120 {
7121 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7122 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7123 return FALSE;
7124 }
7125 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7126 if (s != NULL && s->linker_has_input)
7127 {
7128 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7129 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7130 return FALSE;
7131 }
7132
7133 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7134 /* If .dynstr is excluded from the link, we don't want any of
7135 these tags. Strictly, we should be checking each section
7136 individually; This quick check covers for the case where
7137 someone does a /DISCARD/ : { *(*) }. */
7138 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7139 {
7140 bfd_size_type strsize;
7141
7142 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7143 if ((info->emit_hash
7144 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7145 || (info->emit_gnu_hash
f16a9783
MS
7146 && (bed->record_xhash_symbol == NULL
7147 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
902e9fc7
MR
7148 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7149 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7150 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7151 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7152 bed->s->sizeof_sym))
7153 return FALSE;
7154 }
7155 }
7156
7157 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7158 return FALSE;
7159
7160 /* The backend must work out the sizes of all the other dynamic
7161 sections. */
7162 if (dynobj != NULL
7163 && bed->elf_backend_size_dynamic_sections != NULL
7164 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7165 return FALSE;
7166
7167 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7168 {
902e9fc7
MR
7169 if (elf_tdata (output_bfd)->cverdefs)
7170 {
7171 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7172
7173 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7174 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7175 return FALSE;
7176 }
7177
7178 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7179 {
7180 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7181 return FALSE;
7182 }
7183 else if (info->flags & DF_BIND_NOW)
7184 {
7185 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7186 return FALSE;
7187 }
7188
7189 if (info->flags_1)
7190 {
7191 if (bfd_link_executable (info))
7192 info->flags_1 &= ~ (DF_1_INITFIRST
7193 | DF_1_NODELETE
7194 | DF_1_NOOPEN);
7195 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7196 return FALSE;
7197 }
7198
7199 if (elf_tdata (output_bfd)->cverrefs)
7200 {
7201 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7202
7203 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7204 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7205 return FALSE;
7206 }
5a580b3a 7207
8423293d
AM
7208 if ((elf_tdata (output_bfd)->cverrefs == 0
7209 && elf_tdata (output_bfd)->cverdefs == 0)
63f452a8 7210 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
8423293d 7211 {
902e9fc7
MR
7212 asection *s;
7213
3d4d4302 7214 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
7215 s->flags |= SEC_EXCLUDE;
7216 }
7217 }
7218 return TRUE;
7219}
7220
74541ad4
AM
7221/* Find the first non-excluded output section. We'll use its
7222 section symbol for some emitted relocs. */
7223void
7224_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7225{
7226 asection *s;
f26a3287 7227 asection *found = NULL;
74541ad4
AM
7228
7229 for (s = output_bfd->sections; s != NULL; s = s->next)
7230 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
d00dd7dc 7231 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7232 {
f26a3287
AM
7233 found = s;
7234 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7235 break;
74541ad4 7236 }
f26a3287 7237 elf_hash_table (info)->text_index_section = found;
74541ad4
AM
7238}
7239
7240/* Find two non-excluded output sections, one for code, one for data.
7241 We'll use their section symbols for some emitted relocs. */
7242void
7243_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7244{
7245 asection *s;
f26a3287 7246 asection *found = NULL;
74541ad4 7247
266b05cf 7248 /* Data first, since setting text_index_section changes
7f923b7f 7249 _bfd_elf_omit_section_dynsym_default. */
74541ad4 7250 for (s = output_bfd->sections; s != NULL; s = s->next)
f26a3287
AM
7251 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7252 && !(s->flags & SEC_READONLY)
d00dd7dc 7253 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7254 {
f26a3287
AM
7255 found = s;
7256 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7257 break;
74541ad4 7258 }
f26a3287 7259 elf_hash_table (info)->data_index_section = found;
74541ad4
AM
7260
7261 for (s = output_bfd->sections; s != NULL; s = s->next)
f26a3287
AM
7262 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7263 && (s->flags & SEC_READONLY)
d00dd7dc 7264 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7265 {
f26a3287 7266 found = s;
74541ad4
AM
7267 break;
7268 }
f26a3287 7269 elf_hash_table (info)->text_index_section = found;
74541ad4
AM
7270}
7271
f16a9783
MS
7272#define GNU_HASH_SECTION_NAME(bed) \
7273 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7274
8423293d
AM
7275bfd_boolean
7276bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7277{
74541ad4 7278 const struct elf_backend_data *bed;
23ec1e32 7279 unsigned long section_sym_count;
96d01d93 7280 bfd_size_type dynsymcount = 0;
74541ad4 7281
8423293d
AM
7282 if (!is_elf_hash_table (info->hash))
7283 return TRUE;
7284
74541ad4
AM
7285 bed = get_elf_backend_data (output_bfd);
7286 (*bed->elf_backend_init_index_section) (output_bfd, info);
7287
23ec1e32
MR
7288 /* Assign dynsym indices. In a shared library we generate a section
7289 symbol for each output section, which come first. Next come all
7290 of the back-end allocated local dynamic syms, followed by the rest
7291 of the global symbols.
7292
7293 This is usually not needed for static binaries, however backends
7294 can request to always do it, e.g. the MIPS backend uses dynamic
7295 symbol counts to lay out GOT, which will be produced in the
7296 presence of GOT relocations even in static binaries (holding fixed
7297 data in that case, to satisfy those relocations). */
7298
7299 if (elf_hash_table (info)->dynamic_sections_created
7300 || bed->always_renumber_dynsyms)
7301 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7302 &section_sym_count);
7303
8423293d
AM
7304 if (elf_hash_table (info)->dynamic_sections_created)
7305 {
7306 bfd *dynobj;
8423293d 7307 asection *s;
8423293d
AM
7308 unsigned int dtagcount;
7309
7310 dynobj = elf_hash_table (info)->dynobj;
7311
5a580b3a 7312 /* Work out the size of the symbol version section. */
3d4d4302 7313 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 7314 BFD_ASSERT (s != NULL);
d5486c43 7315 if ((s->flags & SEC_EXCLUDE) == 0)
5a580b3a 7316 {
eea6121a 7317 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 7318 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
7319 if (s->contents == NULL)
7320 return FALSE;
7321
7322 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7323 return FALSE;
7324 }
7325
7326 /* Set the size of the .dynsym and .hash sections. We counted
7327 the number of dynamic symbols in elf_link_add_object_symbols.
7328 We will build the contents of .dynsym and .hash when we build
7329 the final symbol table, because until then we do not know the
7330 correct value to give the symbols. We built the .dynstr
7331 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 7332 s = elf_hash_table (info)->dynsym;
5a580b3a 7333 BFD_ASSERT (s != NULL);
eea6121a 7334 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a 7335
d5486c43
L
7336 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7337 if (s->contents == NULL)
7338 return FALSE;
5a580b3a 7339
d5486c43
L
7340 /* The first entry in .dynsym is a dummy symbol. Clear all the
7341 section syms, in case we don't output them all. */
7342 ++section_sym_count;
7343 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a 7344
fdc90cb4
JJ
7345 elf_hash_table (info)->bucketcount = 0;
7346
5a580b3a
AM
7347 /* Compute the size of the hashing table. As a side effect this
7348 computes the hash values for all the names we export. */
fdc90cb4
JJ
7349 if (info->emit_hash)
7350 {
7351 unsigned long int *hashcodes;
14b1c01e 7352 struct hash_codes_info hashinf;
fdc90cb4
JJ
7353 bfd_size_type amt;
7354 unsigned long int nsyms;
7355 size_t bucketcount;
7356 size_t hash_entry_size;
7357
7358 /* Compute the hash values for all exported symbols. At the same
7359 time store the values in an array so that we could use them for
7360 optimizations. */
7361 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 7362 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
7363 if (hashcodes == NULL)
7364 return FALSE;
14b1c01e
AM
7365 hashinf.hashcodes = hashcodes;
7366 hashinf.error = FALSE;
5a580b3a 7367
fdc90cb4
JJ
7368 /* Put all hash values in HASHCODES. */
7369 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
7370 elf_collect_hash_codes, &hashinf);
7371 if (hashinf.error)
4dd07732
AM
7372 {
7373 free (hashcodes);
7374 return FALSE;
7375 }
5a580b3a 7376
14b1c01e 7377 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
7378 bucketcount
7379 = compute_bucket_count (info, hashcodes, nsyms, 0);
7380 free (hashcodes);
7381
4b48e2f6 7382 if (bucketcount == 0 && nsyms > 0)
fdc90cb4 7383 return FALSE;
5a580b3a 7384
fdc90cb4
JJ
7385 elf_hash_table (info)->bucketcount = bucketcount;
7386
3d4d4302 7387 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
7388 BFD_ASSERT (s != NULL);
7389 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7390 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 7391 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7392 if (s->contents == NULL)
7393 return FALSE;
7394
7395 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7396 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7397 s->contents + hash_entry_size);
7398 }
7399
7400 if (info->emit_gnu_hash)
7401 {
7402 size_t i, cnt;
7403 unsigned char *contents;
7404 struct collect_gnu_hash_codes cinfo;
7405 bfd_size_type amt;
7406 size_t bucketcount;
7407
7408 memset (&cinfo, 0, sizeof (cinfo));
7409
7410 /* Compute the hash values for all exported symbols. At the same
7411 time store the values in an array so that we could use them for
7412 optimizations. */
7413 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 7414 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
7415 if (cinfo.hashcodes == NULL)
7416 return FALSE;
7417
7418 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7419 cinfo.min_dynindx = -1;
7420 cinfo.output_bfd = output_bfd;
7421 cinfo.bed = bed;
7422
7423 /* Put all hash values in HASHCODES. */
7424 elf_link_hash_traverse (elf_hash_table (info),
7425 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 7426 if (cinfo.error)
4dd07732
AM
7427 {
7428 free (cinfo.hashcodes);
7429 return FALSE;
7430 }
fdc90cb4
JJ
7431
7432 bucketcount
7433 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7434
7435 if (bucketcount == 0)
7436 {
7437 free (cinfo.hashcodes);
7438 return FALSE;
7439 }
7440
f16a9783 7441 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
fdc90cb4
JJ
7442 BFD_ASSERT (s != NULL);
7443
7444 if (cinfo.nsyms == 0)
7445 {
f16a9783 7446 /* Empty .gnu.hash or .MIPS.xhash section is special. */
fdc90cb4
JJ
7447 BFD_ASSERT (cinfo.min_dynindx == -1);
7448 free (cinfo.hashcodes);
7449 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 7450 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7451 if (contents == NULL)
7452 return FALSE;
7453 s->contents = contents;
7454 /* 1 empty bucket. */
7455 bfd_put_32 (output_bfd, 1, contents);
7456 /* SYMIDX above the special symbol 0. */
7457 bfd_put_32 (output_bfd, 1, contents + 4);
7458 /* Just one word for bitmask. */
7459 bfd_put_32 (output_bfd, 1, contents + 8);
7460 /* Only hash fn bloom filter. */
7461 bfd_put_32 (output_bfd, 0, contents + 12);
7462 /* No hashes are valid - empty bitmask. */
7463 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7464 /* No hashes in the only bucket. */
7465 bfd_put_32 (output_bfd, 0,
7466 contents + 16 + bed->s->arch_size / 8);
7467 }
7468 else
7469 {
9e6619e2 7470 unsigned long int maskwords, maskbitslog2, x;
0b33793d 7471 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 7472
9e6619e2
AM
7473 x = cinfo.nsyms;
7474 maskbitslog2 = 1;
7475 while ((x >>= 1) != 0)
7476 ++maskbitslog2;
fdc90cb4
JJ
7477 if (maskbitslog2 < 3)
7478 maskbitslog2 = 5;
7479 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7480 maskbitslog2 = maskbitslog2 + 3;
7481 else
7482 maskbitslog2 = maskbitslog2 + 2;
7483 if (bed->s->arch_size == 64)
7484 {
7485 if (maskbitslog2 == 5)
7486 maskbitslog2 = 6;
7487 cinfo.shift1 = 6;
7488 }
7489 else
7490 cinfo.shift1 = 5;
7491 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 7492 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
7493 cinfo.maskbits = 1 << maskbitslog2;
7494 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7495 amt = bucketcount * sizeof (unsigned long int) * 2;
7496 amt += maskwords * sizeof (bfd_vma);
a50b1753 7497 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
7498 if (cinfo.bitmask == NULL)
7499 {
7500 free (cinfo.hashcodes);
7501 return FALSE;
7502 }
7503
a50b1753 7504 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
7505 cinfo.indx = cinfo.counts + bucketcount;
7506 cinfo.symindx = dynsymcount - cinfo.nsyms;
7507 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7508
7509 /* Determine how often each hash bucket is used. */
7510 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7511 for (i = 0; i < cinfo.nsyms; ++i)
7512 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7513
7514 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7515 if (cinfo.counts[i] != 0)
7516 {
7517 cinfo.indx[i] = cnt;
7518 cnt += cinfo.counts[i];
7519 }
7520 BFD_ASSERT (cnt == dynsymcount);
7521 cinfo.bucketcount = bucketcount;
7522 cinfo.local_indx = cinfo.min_dynindx;
7523
7524 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7525 s->size += cinfo.maskbits / 8;
f16a9783
MS
7526 if (bed->record_xhash_symbol != NULL)
7527 s->size += cinfo.nsyms * 4;
a50b1753 7528 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7529 if (contents == NULL)
7530 {
7531 free (cinfo.bitmask);
7532 free (cinfo.hashcodes);
7533 return FALSE;
7534 }
7535
7536 s->contents = contents;
7537 bfd_put_32 (output_bfd, bucketcount, contents);
7538 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7539 bfd_put_32 (output_bfd, maskwords, contents + 8);
7540 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7541 contents += 16 + cinfo.maskbits / 8;
7542
7543 for (i = 0; i < bucketcount; ++i)
7544 {
7545 if (cinfo.counts[i] == 0)
7546 bfd_put_32 (output_bfd, 0, contents);
7547 else
7548 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7549 contents += 4;
7550 }
7551
7552 cinfo.contents = contents;
7553
f16a9783
MS
7554 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7555 /* Renumber dynamic symbols, if populating .gnu.hash section.
7556 If using .MIPS.xhash, populate the translation table. */
fdc90cb4 7557 elf_link_hash_traverse (elf_hash_table (info),
f16a9783 7558 elf_gnu_hash_process_symidx, &cinfo);
fdc90cb4
JJ
7559
7560 contents = s->contents + 16;
7561 for (i = 0; i < maskwords; ++i)
7562 {
7563 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7564 contents);
7565 contents += bed->s->arch_size / 8;
7566 }
7567
7568 free (cinfo.bitmask);
7569 free (cinfo.hashcodes);
7570 }
7571 }
5a580b3a 7572
3d4d4302 7573 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
7574 BFD_ASSERT (s != NULL);
7575
4ad4eba5 7576 elf_finalize_dynstr (output_bfd, info);
5a580b3a 7577
eea6121a 7578 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
7579
7580 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7581 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7582 return FALSE;
7583 }
7584
7585 return TRUE;
7586}
4d269e42 7587\f
4d269e42
AM
7588/* Make sure sec_info_type is cleared if sec_info is cleared too. */
7589
7590static void
7591merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7592 asection *sec)
7593{
dbaa2011
AM
7594 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7595 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
7596}
7597
7598/* Finish SHF_MERGE section merging. */
7599
7600bfd_boolean
630993ec 7601_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
7602{
7603 bfd *ibfd;
7604 asection *sec;
7605
7606 if (!is_elf_hash_table (info->hash))
7607 return FALSE;
7608
c72f2fb2 7609 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
7610 if ((ibfd->flags & DYNAMIC) == 0
7611 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
7612 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7613 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
7614 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7615 if ((sec->flags & SEC_MERGE) != 0
7616 && !bfd_is_abs_section (sec->output_section))
7617 {
7618 struct bfd_elf_section_data *secdata;
7619
7620 secdata = elf_section_data (sec);
630993ec 7621 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
7622 &elf_hash_table (info)->merge_info,
7623 sec, &secdata->sec_info))
7624 return FALSE;
7625 else if (secdata->sec_info)
dbaa2011 7626 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
7627 }
7628
7629 if (elf_hash_table (info)->merge_info != NULL)
630993ec 7630 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
7631 merge_sections_remove_hook);
7632 return TRUE;
7633}
7634
7635/* Create an entry in an ELF linker hash table. */
7636
7637struct bfd_hash_entry *
7638_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7639 struct bfd_hash_table *table,
7640 const char *string)
7641{
7642 /* Allocate the structure if it has not already been allocated by a
7643 subclass. */
7644 if (entry == NULL)
7645 {
a50b1753 7646 entry = (struct bfd_hash_entry *)
ca4be51c 7647 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
7648 if (entry == NULL)
7649 return entry;
7650 }
7651
7652 /* Call the allocation method of the superclass. */
7653 entry = _bfd_link_hash_newfunc (entry, table, string);
7654 if (entry != NULL)
7655 {
7656 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7657 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7658
7659 /* Set local fields. */
7660 ret->indx = -1;
7661 ret->dynindx = -1;
7662 ret->got = htab->init_got_refcount;
7663 ret->plt = htab->init_plt_refcount;
7664 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7665 - offsetof (struct elf_link_hash_entry, size)));
7666 /* Assume that we have been called by a non-ELF symbol reader.
7667 This flag is then reset by the code which reads an ELF input
7668 file. This ensures that a symbol created by a non-ELF symbol
7669 reader will have the flag set correctly. */
7670 ret->non_elf = 1;
7671 }
7672
7673 return entry;
7674}
7675
7676/* Copy data from an indirect symbol to its direct symbol, hiding the
7677 old indirect symbol. Also used for copying flags to a weakdef. */
7678
7679void
7680_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7681 struct elf_link_hash_entry *dir,
7682 struct elf_link_hash_entry *ind)
7683{
7684 struct elf_link_hash_table *htab;
7685
ad172eaa
L
7686 if (ind->dyn_relocs != NULL)
7687 {
7688 if (dir->dyn_relocs != NULL)
7689 {
7690 struct elf_dyn_relocs **pp;
7691 struct elf_dyn_relocs *p;
7692
7693 /* Add reloc counts against the indirect sym to the direct sym
7694 list. Merge any entries against the same section. */
7695 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
7696 {
7697 struct elf_dyn_relocs *q;
7698
7699 for (q = dir->dyn_relocs; q != NULL; q = q->next)
7700 if (q->sec == p->sec)
7701 {
7702 q->pc_count += p->pc_count;
7703 q->count += p->count;
7704 *pp = p->next;
7705 break;
7706 }
7707 if (q == NULL)
7708 pp = &p->next;
7709 }
7710 *pp = dir->dyn_relocs;
7711 }
7712
7713 dir->dyn_relocs = ind->dyn_relocs;
7714 ind->dyn_relocs = NULL;
7715 }
7716
4d269e42 7717 /* Copy down any references that we may have already seen to the
e81830c5 7718 symbol which just became indirect. */
4d269e42 7719
422f1182 7720 if (dir->versioned != versioned_hidden)
e81830c5
AM
7721 dir->ref_dynamic |= ind->ref_dynamic;
7722 dir->ref_regular |= ind->ref_regular;
7723 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7724 dir->non_got_ref |= ind->non_got_ref;
7725 dir->needs_plt |= ind->needs_plt;
7726 dir->pointer_equality_needed |= ind->pointer_equality_needed;
4d269e42
AM
7727
7728 if (ind->root.type != bfd_link_hash_indirect)
7729 return;
7730
7731 /* Copy over the global and procedure linkage table refcount entries.
7732 These may have been already set up by a check_relocs routine. */
7733 htab = elf_hash_table (info);
7734 if (ind->got.refcount > htab->init_got_refcount.refcount)
7735 {
7736 if (dir->got.refcount < 0)
7737 dir->got.refcount = 0;
7738 dir->got.refcount += ind->got.refcount;
7739 ind->got.refcount = htab->init_got_refcount.refcount;
7740 }
7741
7742 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7743 {
7744 if (dir->plt.refcount < 0)
7745 dir->plt.refcount = 0;
7746 dir->plt.refcount += ind->plt.refcount;
7747 ind->plt.refcount = htab->init_plt_refcount.refcount;
7748 }
7749
7750 if (ind->dynindx != -1)
7751 {
7752 if (dir->dynindx != -1)
7753 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7754 dir->dynindx = ind->dynindx;
7755 dir->dynstr_index = ind->dynstr_index;
7756 ind->dynindx = -1;
7757 ind->dynstr_index = 0;
7758 }
7759}
7760
7761void
7762_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7763 struct elf_link_hash_entry *h,
7764 bfd_boolean force_local)
7765{
3aa14d16
L
7766 /* STT_GNU_IFUNC symbol must go through PLT. */
7767 if (h->type != STT_GNU_IFUNC)
7768 {
7769 h->plt = elf_hash_table (info)->init_plt_offset;
7770 h->needs_plt = 0;
7771 }
4d269e42
AM
7772 if (force_local)
7773 {
7774 h->forced_local = 1;
7775 if (h->dynindx != -1)
7776 {
4d269e42
AM
7777 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7778 h->dynstr_index);
641338d8
AM
7779 h->dynindx = -1;
7780 h->dynstr_index = 0;
4d269e42
AM
7781 }
7782 }
7783}
7784
34a87bb0
L
7785/* Hide a symbol. */
7786
7787void
7788_bfd_elf_link_hide_symbol (bfd *output_bfd,
7789 struct bfd_link_info *info,
7790 struct bfd_link_hash_entry *h)
7791{
7792 if (is_elf_hash_table (info->hash))
7793 {
7794 const struct elf_backend_data *bed
7795 = get_elf_backend_data (output_bfd);
7796 struct elf_link_hash_entry *eh
7797 = (struct elf_link_hash_entry *) h;
7798 bed->elf_backend_hide_symbol (info, eh, TRUE);
7799 eh->def_dynamic = 0;
7800 eh->ref_dynamic = 0;
7801 eh->dynamic_def = 0;
7802 }
7803}
7804
7bf52ea2
AM
7805/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7806 caller. */
4d269e42
AM
7807
7808bfd_boolean
7809_bfd_elf_link_hash_table_init
7810 (struct elf_link_hash_table *table,
7811 bfd *abfd,
7812 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7813 struct bfd_hash_table *,
7814 const char *),
4dfe6ac6
NC
7815 unsigned int entsize,
7816 enum elf_target_id target_id)
4d269e42
AM
7817{
7818 bfd_boolean ret;
7819 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7820
4d269e42
AM
7821 table->init_got_refcount.refcount = can_refcount - 1;
7822 table->init_plt_refcount.refcount = can_refcount - 1;
7823 table->init_got_offset.offset = -(bfd_vma) 1;
7824 table->init_plt_offset.offset = -(bfd_vma) 1;
7825 /* The first dynamic symbol is a dummy. */
7826 table->dynsymcount = 1;
7827
7828 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7829
4d269e42 7830 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7831 table->hash_table_id = target_id;
90c14f0c 7832 table->target_os = get_elf_backend_data (abfd)->target_os;
4d269e42
AM
7833
7834 return ret;
7835}
7836
7837/* Create an ELF linker hash table. */
7838
7839struct bfd_link_hash_table *
7840_bfd_elf_link_hash_table_create (bfd *abfd)
7841{
7842 struct elf_link_hash_table *ret;
986f0783 7843 size_t amt = sizeof (struct elf_link_hash_table);
4d269e42 7844
7bf52ea2 7845 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7846 if (ret == NULL)
7847 return NULL;
7848
7849 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7850 sizeof (struct elf_link_hash_entry),
7851 GENERIC_ELF_DATA))
4d269e42
AM
7852 {
7853 free (ret);
7854 return NULL;
7855 }
d495ab0d 7856 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7857
7858 return &ret->root;
7859}
7860
9f7c3e5e
AM
7861/* Destroy an ELF linker hash table. */
7862
7863void
d495ab0d 7864_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7865{
d495ab0d
AM
7866 struct elf_link_hash_table *htab;
7867
7868 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7869 if (htab->dynstr != NULL)
7870 _bfd_elf_strtab_free (htab->dynstr);
7871 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7872 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7873}
7874
4d269e42
AM
7875/* This is a hook for the ELF emulation code in the generic linker to
7876 tell the backend linker what file name to use for the DT_NEEDED
7877 entry for a dynamic object. */
7878
7879void
7880bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7881{
7882 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7883 && bfd_get_format (abfd) == bfd_object)
7884 elf_dt_name (abfd) = name;
7885}
7886
7887int
7888bfd_elf_get_dyn_lib_class (bfd *abfd)
7889{
7890 int lib_class;
7891 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7892 && bfd_get_format (abfd) == bfd_object)
7893 lib_class = elf_dyn_lib_class (abfd);
7894 else
7895 lib_class = 0;
7896 return lib_class;
7897}
7898
7899void
7900bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7901{
7902 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7903 && bfd_get_format (abfd) == bfd_object)
7904 elf_dyn_lib_class (abfd) = lib_class;
7905}
7906
7907/* Get the list of DT_NEEDED entries for a link. This is a hook for
7908 the linker ELF emulation code. */
7909
7910struct bfd_link_needed_list *
7911bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7912 struct bfd_link_info *info)
7913{
7914 if (! is_elf_hash_table (info->hash))
7915 return NULL;
7916 return elf_hash_table (info)->needed;
7917}
7918
7919/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7920 hook for the linker ELF emulation code. */
7921
7922struct bfd_link_needed_list *
7923bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7924 struct bfd_link_info *info)
7925{
7926 if (! is_elf_hash_table (info->hash))
7927 return NULL;
7928 return elf_hash_table (info)->runpath;
7929}
7930
7931/* Get the name actually used for a dynamic object for a link. This
7932 is the SONAME entry if there is one. Otherwise, it is the string
7933 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7934
7935const char *
7936bfd_elf_get_dt_soname (bfd *abfd)
7937{
7938 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7939 && bfd_get_format (abfd) == bfd_object)
7940 return elf_dt_name (abfd);
7941 return NULL;
7942}
7943
7944/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7945 the ELF linker emulation code. */
7946
7947bfd_boolean
7948bfd_elf_get_bfd_needed_list (bfd *abfd,
7949 struct bfd_link_needed_list **pneeded)
7950{
7951 asection *s;
7952 bfd_byte *dynbuf = NULL;
cb33740c 7953 unsigned int elfsec;
4d269e42
AM
7954 unsigned long shlink;
7955 bfd_byte *extdyn, *extdynend;
7956 size_t extdynsize;
7957 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7958
7959 *pneeded = NULL;
7960
7961 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7962 || bfd_get_format (abfd) != bfd_object)
7963 return TRUE;
7964
7965 s = bfd_get_section_by_name (abfd, ".dynamic");
7966 if (s == NULL || s->size == 0)
7967 return TRUE;
7968
7969 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7970 goto error_return;
7971
7972 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 7973 if (elfsec == SHN_BAD)
4d269e42
AM
7974 goto error_return;
7975
7976 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 7977
4d269e42
AM
7978 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7979 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7980
7981 extdyn = dynbuf;
7982 extdynend = extdyn + s->size;
7983 for (; extdyn < extdynend; extdyn += extdynsize)
7984 {
7985 Elf_Internal_Dyn dyn;
7986
7987 (*swap_dyn_in) (abfd, extdyn, &dyn);
7988
7989 if (dyn.d_tag == DT_NULL)
7990 break;
7991
7992 if (dyn.d_tag == DT_NEEDED)
7993 {
7994 const char *string;
7995 struct bfd_link_needed_list *l;
7996 unsigned int tagv = dyn.d_un.d_val;
986f0783 7997 size_t amt;
4d269e42
AM
7998
7999 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8000 if (string == NULL)
8001 goto error_return;
8002
8003 amt = sizeof *l;
a50b1753 8004 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
8005 if (l == NULL)
8006 goto error_return;
8007
8008 l->by = abfd;
8009 l->name = string;
8010 l->next = *pneeded;
8011 *pneeded = l;
8012 }
8013 }
8014
8015 free (dynbuf);
8016
8017 return TRUE;
8018
8019 error_return:
c9594989 8020 free (dynbuf);
4d269e42
AM
8021 return FALSE;
8022}
8023
8024struct elf_symbuf_symbol
8025{
8026 unsigned long st_name; /* Symbol name, index in string tbl */
8027 unsigned char st_info; /* Type and binding attributes */
8028 unsigned char st_other; /* Visibilty, and target specific */
8029};
8030
8031struct elf_symbuf_head
8032{
8033 struct elf_symbuf_symbol *ssym;
ef53be89 8034 size_t count;
4d269e42
AM
8035 unsigned int st_shndx;
8036};
8037
8038struct elf_symbol
8039{
8040 union
8041 {
8042 Elf_Internal_Sym *isym;
8043 struct elf_symbuf_symbol *ssym;
dcea6a95 8044 void *p;
4d269e42
AM
8045 } u;
8046 const char *name;
8047};
8048
8049/* Sort references to symbols by ascending section number. */
8050
8051static int
8052elf_sort_elf_symbol (const void *arg1, const void *arg2)
8053{
8054 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8055 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8056
dcea6a95
AM
8057 if (s1->st_shndx != s2->st_shndx)
8058 return s1->st_shndx > s2->st_shndx ? 1 : -1;
8059 /* Final sort by the address of the sym in the symbuf ensures
8060 a stable sort. */
8061 if (s1 != s2)
8062 return s1 > s2 ? 1 : -1;
8063 return 0;
4d269e42
AM
8064}
8065
8066static int
8067elf_sym_name_compare (const void *arg1, const void *arg2)
8068{
8069 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8070 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
dcea6a95
AM
8071 int ret = strcmp (s1->name, s2->name);
8072 if (ret != 0)
8073 return ret;
8074 if (s1->u.p != s2->u.p)
8075 return s1->u.p > s2->u.p ? 1 : -1;
8076 return 0;
4d269e42
AM
8077}
8078
8079static struct elf_symbuf_head *
ef53be89 8080elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
4d269e42 8081{
14b1c01e 8082 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
8083 struct elf_symbuf_symbol *ssym;
8084 struct elf_symbuf_head *ssymbuf, *ssymhead;
446f7ed5 8085 size_t i, shndx_count, total_size, amt;
4d269e42 8086
446f7ed5
AM
8087 amt = symcount * sizeof (*indbuf);
8088 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
4d269e42
AM
8089 if (indbuf == NULL)
8090 return NULL;
8091
8092 for (ind = indbuf, i = 0; i < symcount; i++)
8093 if (isymbuf[i].st_shndx != SHN_UNDEF)
8094 *ind++ = &isymbuf[i];
8095 indbufend = ind;
8096
8097 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8098 elf_sort_elf_symbol);
8099
8100 shndx_count = 0;
8101 if (indbufend > indbuf)
8102 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8103 if (ind[0]->st_shndx != ind[1]->st_shndx)
8104 shndx_count++;
8105
3ae181ee
L
8106 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8107 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 8108 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
8109 if (ssymbuf == NULL)
8110 {
8111 free (indbuf);
8112 return NULL;
8113 }
8114
3ae181ee 8115 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
8116 ssymbuf->ssym = NULL;
8117 ssymbuf->count = shndx_count;
8118 ssymbuf->st_shndx = 0;
8119 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8120 {
8121 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8122 {
8123 ssymhead++;
8124 ssymhead->ssym = ssym;
8125 ssymhead->count = 0;
8126 ssymhead->st_shndx = (*ind)->st_shndx;
8127 }
8128 ssym->st_name = (*ind)->st_name;
8129 ssym->st_info = (*ind)->st_info;
8130 ssym->st_other = (*ind)->st_other;
8131 ssymhead->count++;
8132 }
ef53be89 8133 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
3ae181ee
L
8134 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
8135 == total_size));
4d269e42
AM
8136
8137 free (indbuf);
8138 return ssymbuf;
8139}
8140
8141/* Check if 2 sections define the same set of local and global
8142 symbols. */
8143
8f317e31 8144static bfd_boolean
4d269e42
AM
8145bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8146 struct bfd_link_info *info)
8147{
8148 bfd *bfd1, *bfd2;
8149 const struct elf_backend_data *bed1, *bed2;
8150 Elf_Internal_Shdr *hdr1, *hdr2;
ef53be89 8151 size_t symcount1, symcount2;
4d269e42
AM
8152 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8153 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8154 Elf_Internal_Sym *isym, *isymend;
8155 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
ef53be89 8156 size_t count1, count2, i;
cb33740c 8157 unsigned int shndx1, shndx2;
4d269e42
AM
8158 bfd_boolean result;
8159
8160 bfd1 = sec1->owner;
8161 bfd2 = sec2->owner;
8162
4d269e42
AM
8163 /* Both sections have to be in ELF. */
8164 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8165 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8166 return FALSE;
8167
8168 if (elf_section_type (sec1) != elf_section_type (sec2))
8169 return FALSE;
8170
4d269e42
AM
8171 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8172 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 8173 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
8174 return FALSE;
8175
8176 bed1 = get_elf_backend_data (bfd1);
8177 bed2 = get_elf_backend_data (bfd2);
8178 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8179 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8180 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8181 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8182
8183 if (symcount1 == 0 || symcount2 == 0)
8184 return FALSE;
8185
8186 result = FALSE;
8187 isymbuf1 = NULL;
8188 isymbuf2 = NULL;
a50b1753
NC
8189 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8190 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
8191
8192 if (ssymbuf1 == NULL)
8193 {
8194 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8195 NULL, NULL, NULL);
8196 if (isymbuf1 == NULL)
8197 goto done;
8198
67411cbf 8199 if (info != NULL && !info->reduce_memory_overheads)
dcea6a95
AM
8200 {
8201 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8202 elf_tdata (bfd1)->symbuf = ssymbuf1;
8203 }
4d269e42
AM
8204 }
8205
8206 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8207 {
8208 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8209 NULL, NULL, NULL);
8210 if (isymbuf2 == NULL)
8211 goto done;
8212
67411cbf 8213 if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
dcea6a95
AM
8214 {
8215 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8216 elf_tdata (bfd2)->symbuf = ssymbuf2;
8217 }
4d269e42
AM
8218 }
8219
8220 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8221 {
8222 /* Optimized faster version. */
ef53be89 8223 size_t lo, hi, mid;
4d269e42
AM
8224 struct elf_symbol *symp;
8225 struct elf_symbuf_symbol *ssym, *ssymend;
8226
8227 lo = 0;
8228 hi = ssymbuf1->count;
8229 ssymbuf1++;
8230 count1 = 0;
8231 while (lo < hi)
8232 {
8233 mid = (lo + hi) / 2;
cb33740c 8234 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 8235 hi = mid;
cb33740c 8236 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
8237 lo = mid + 1;
8238 else
8239 {
8240 count1 = ssymbuf1[mid].count;
8241 ssymbuf1 += mid;
8242 break;
8243 }
8244 }
8245
8246 lo = 0;
8247 hi = ssymbuf2->count;
8248 ssymbuf2++;
8249 count2 = 0;
8250 while (lo < hi)
8251 {
8252 mid = (lo + hi) / 2;
cb33740c 8253 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 8254 hi = mid;
cb33740c 8255 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
8256 lo = mid + 1;
8257 else
8258 {
8259 count2 = ssymbuf2[mid].count;
8260 ssymbuf2 += mid;
8261 break;
8262 }
8263 }
8264
8265 if (count1 == 0 || count2 == 0 || count1 != count2)
8266 goto done;
8267
ca4be51c
AM
8268 symtable1
8269 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8270 symtable2
8271 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
8272 if (symtable1 == NULL || symtable2 == NULL)
8273 goto done;
8274
8275 symp = symtable1;
8276 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
8277 ssym < ssymend; ssym++, symp++)
8278 {
8279 symp->u.ssym = ssym;
8280 symp->name = bfd_elf_string_from_elf_section (bfd1,
8281 hdr1->sh_link,
8282 ssym->st_name);
8283 }
8284
8285 symp = symtable2;
8286 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8287 ssym < ssymend; ssym++, symp++)
8288 {
8289 symp->u.ssym = ssym;
8290 symp->name = bfd_elf_string_from_elf_section (bfd2,
8291 hdr2->sh_link,
8292 ssym->st_name);
8293 }
8294
8295 /* Sort symbol by name. */
8296 qsort (symtable1, count1, sizeof (struct elf_symbol),
8297 elf_sym_name_compare);
8298 qsort (symtable2, count1, sizeof (struct elf_symbol),
8299 elf_sym_name_compare);
8300
8301 for (i = 0; i < count1; i++)
8302 /* Two symbols must have the same binding, type and name. */
8303 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8304 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8305 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8306 goto done;
8307
8308 result = TRUE;
8309 goto done;
8310 }
8311
a50b1753
NC
8312 symtable1 = (struct elf_symbol *)
8313 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8314 symtable2 = (struct elf_symbol *)
8315 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
8316 if (symtable1 == NULL || symtable2 == NULL)
8317 goto done;
8318
8319 /* Count definitions in the section. */
8320 count1 = 0;
8321 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 8322 if (isym->st_shndx == shndx1)
4d269e42
AM
8323 symtable1[count1++].u.isym = isym;
8324
8325 count2 = 0;
8326 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 8327 if (isym->st_shndx == shndx2)
4d269e42
AM
8328 symtable2[count2++].u.isym = isym;
8329
8330 if (count1 == 0 || count2 == 0 || count1 != count2)
8331 goto done;
8332
8333 for (i = 0; i < count1; i++)
8334 symtable1[i].name
8335 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8336 symtable1[i].u.isym->st_name);
8337
8338 for (i = 0; i < count2; i++)
8339 symtable2[i].name
8340 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8341 symtable2[i].u.isym->st_name);
8342
8343 /* Sort symbol by name. */
8344 qsort (symtable1, count1, sizeof (struct elf_symbol),
8345 elf_sym_name_compare);
8346 qsort (symtable2, count1, sizeof (struct elf_symbol),
8347 elf_sym_name_compare);
8348
8349 for (i = 0; i < count1; i++)
8350 /* Two symbols must have the same binding, type and name. */
8351 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8352 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8353 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8354 goto done;
8355
8356 result = TRUE;
8357
dc1e8a47 8358 done:
c9594989
AM
8359 free (symtable1);
8360 free (symtable2);
8361 free (isymbuf1);
8362 free (isymbuf2);
4d269e42
AM
8363
8364 return result;
8365}
8366
8367/* Return TRUE if 2 section types are compatible. */
8368
8369bfd_boolean
8370_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8371 bfd *bbfd, const asection *bsec)
8372{
8373 if (asec == NULL
8374 || bsec == NULL
8375 || abfd->xvec->flavour != bfd_target_elf_flavour
8376 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8377 return TRUE;
8378
8379 return elf_section_type (asec) == elf_section_type (bsec);
8380}
8381\f
c152c796
AM
8382/* Final phase of ELF linker. */
8383
8384/* A structure we use to avoid passing large numbers of arguments. */
8385
8386struct elf_final_link_info
8387{
8388 /* General link information. */
8389 struct bfd_link_info *info;
8390 /* Output BFD. */
8391 bfd *output_bfd;
8392 /* Symbol string table. */
ef10c3ac 8393 struct elf_strtab_hash *symstrtab;
c152c796
AM
8394 /* .hash section. */
8395 asection *hash_sec;
8396 /* symbol version section (.gnu.version). */
8397 asection *symver_sec;
8398 /* Buffer large enough to hold contents of any section. */
8399 bfd_byte *contents;
8400 /* Buffer large enough to hold external relocs of any section. */
8401 void *external_relocs;
8402 /* Buffer large enough to hold internal relocs of any section. */
8403 Elf_Internal_Rela *internal_relocs;
8404 /* Buffer large enough to hold external local symbols of any input
8405 BFD. */
8406 bfd_byte *external_syms;
8407 /* And a buffer for symbol section indices. */
8408 Elf_External_Sym_Shndx *locsym_shndx;
8409 /* Buffer large enough to hold internal local symbols of any input
8410 BFD. */
8411 Elf_Internal_Sym *internal_syms;
8412 /* Array large enough to hold a symbol index for each local symbol
8413 of any input BFD. */
8414 long *indices;
8415 /* Array large enough to hold a section pointer for each local
8416 symbol of any input BFD. */
8417 asection **sections;
ef10c3ac 8418 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 8419 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
8420 /* Number of STT_FILE syms seen. */
8421 size_t filesym_count;
496afd17
L
8422 /* Local symbol hash table. */
8423 struct bfd_hash_table local_hash_table;
c152c796
AM
8424};
8425
496afd17
L
8426struct local_hash_entry
8427{
8428 /* Base hash table entry structure. */
8429 struct bfd_hash_entry root;
8430 /* Size of the local symbol name. */
8431 size_t size;
8432 /* Number of the duplicated local symbol names. */
8433 long count;
8434};
8435
8436/* Create an entry in the local symbol hash table. */
8437
8438static struct bfd_hash_entry *
8439local_hash_newfunc (struct bfd_hash_entry *entry,
8440 struct bfd_hash_table *table,
8441 const char *string)
8442{
8443
8444 /* Allocate the structure if it has not already been allocated by a
8445 subclass. */
8446 if (entry == NULL)
8447 {
8448 entry = bfd_hash_allocate (table,
8449 sizeof (struct local_hash_entry));
8450 if (entry == NULL)
8451 return entry;
8452 }
8453
8454 /* Call the allocation method of the superclass. */
8455 entry = bfd_hash_newfunc (entry, table, string);
8456 if (entry != NULL)
8457 {
8458 ((struct local_hash_entry *) entry)->count = 0;
8459 ((struct local_hash_entry *) entry)->size = 0;
8460 }
8461
8462 return entry;
8463}
8464
c152c796
AM
8465/* This struct is used to pass information to elf_link_output_extsym. */
8466
8467struct elf_outext_info
8468{
8469 bfd_boolean failed;
8470 bfd_boolean localsyms;
34a79995 8471 bfd_boolean file_sym_done;
8b127cbc 8472 struct elf_final_link_info *flinfo;
c152c796
AM
8473};
8474
d9352518
DB
8475
8476/* Support for evaluating a complex relocation.
8477
8478 Complex relocations are generalized, self-describing relocations. The
8479 implementation of them consists of two parts: complex symbols, and the
a0c8462f 8480 relocations themselves.
d9352518 8481
4b69ce9b 8482 The relocations use a reserved elf-wide relocation type code (R_RELC
d9352518
DB
8483 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8484 information (start bit, end bit, word width, etc) into the addend. This
8485 information is extracted from CGEN-generated operand tables within gas.
8486
4b69ce9b 8487 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
d9352518
DB
8488 internal) representing prefix-notation expressions, including but not
8489 limited to those sorts of expressions normally encoded as addends in the
8490 addend field. The symbol mangling format is:
8491
8492 <node> := <literal>
07d6d2b8
AM
8493 | <unary-operator> ':' <node>
8494 | <binary-operator> ':' <node> ':' <node>
d9352518
DB
8495 ;
8496
8497 <literal> := 's' <digits=N> ':' <N character symbol name>
07d6d2b8 8498 | 'S' <digits=N> ':' <N character section name>
d9352518
DB
8499 | '#' <hexdigits>
8500 ;
8501
8502 <binary-operator> := as in C
8503 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8504
8505static void
a0c8462f
AM
8506set_symbol_value (bfd *bfd_with_globals,
8507 Elf_Internal_Sym *isymbuf,
8508 size_t locsymcount,
8509 size_t symidx,
8510 bfd_vma val)
d9352518 8511{
8977835c
AM
8512 struct elf_link_hash_entry **sym_hashes;
8513 struct elf_link_hash_entry *h;
8514 size_t extsymoff = locsymcount;
d9352518 8515
8977835c 8516 if (symidx < locsymcount)
d9352518 8517 {
8977835c
AM
8518 Elf_Internal_Sym *sym;
8519
8520 sym = isymbuf + symidx;
8521 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8522 {
8523 /* It is a local symbol: move it to the
8524 "absolute" section and give it a value. */
8525 sym->st_shndx = SHN_ABS;
8526 sym->st_value = val;
8527 return;
8528 }
8529 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8530 extsymoff = 0;
d9352518 8531 }
8977835c
AM
8532
8533 /* It is a global symbol: set its link type
8534 to "defined" and give it a value. */
8535
8536 sym_hashes = elf_sym_hashes (bfd_with_globals);
8537 h = sym_hashes [symidx - extsymoff];
8538 while (h->root.type == bfd_link_hash_indirect
8539 || h->root.type == bfd_link_hash_warning)
8540 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8541 h->root.type = bfd_link_hash_defined;
8542 h->root.u.def.value = val;
8543 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
8544}
8545
a0c8462f
AM
8546static bfd_boolean
8547resolve_symbol (const char *name,
8548 bfd *input_bfd,
8b127cbc 8549 struct elf_final_link_info *flinfo,
a0c8462f
AM
8550 bfd_vma *result,
8551 Elf_Internal_Sym *isymbuf,
8552 size_t locsymcount)
d9352518 8553{
a0c8462f
AM
8554 Elf_Internal_Sym *sym;
8555 struct bfd_link_hash_entry *global_entry;
8556 const char *candidate = NULL;
8557 Elf_Internal_Shdr *symtab_hdr;
8558 size_t i;
8559
d9352518
DB
8560 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8561
8562 for (i = 0; i < locsymcount; ++ i)
8563 {
8977835c 8564 sym = isymbuf + i;
d9352518
DB
8565
8566 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8567 continue;
8568
8569 candidate = bfd_elf_string_from_elf_section (input_bfd,
8570 symtab_hdr->sh_link,
8571 sym->st_name);
8572#ifdef DEBUG
0f02bbd9
AM
8573 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8574 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
8575#endif
8576 if (candidate && strcmp (candidate, name) == 0)
8577 {
8b127cbc 8578 asection *sec = flinfo->sections [i];
d9352518 8579
0f02bbd9
AM
8580 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8581 *result += sec->output_offset + sec->output_section->vma;
d9352518 8582#ifdef DEBUG
0f02bbd9
AM
8583 printf ("Found symbol with value %8.8lx\n",
8584 (unsigned long) *result);
d9352518
DB
8585#endif
8586 return TRUE;
8587 }
8588 }
8589
8590 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 8591 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 8592 FALSE, FALSE, TRUE);
d9352518
DB
8593 if (!global_entry)
8594 return FALSE;
a0c8462f 8595
d9352518
DB
8596 if (global_entry->type == bfd_link_hash_defined
8597 || global_entry->type == bfd_link_hash_defweak)
8598 {
a0c8462f
AM
8599 *result = (global_entry->u.def.value
8600 + global_entry->u.def.section->output_section->vma
8601 + global_entry->u.def.section->output_offset);
d9352518 8602#ifdef DEBUG
0f02bbd9
AM
8603 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8604 global_entry->root.string, (unsigned long) *result);
d9352518
DB
8605#endif
8606 return TRUE;
a0c8462f 8607 }
d9352518 8608
d9352518
DB
8609 return FALSE;
8610}
8611
37b01f6a
DG
8612/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8613 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8614 names like "foo.end" which is the end address of section "foo". */
07d6d2b8 8615
d9352518 8616static bfd_boolean
a0c8462f
AM
8617resolve_section (const char *name,
8618 asection *sections,
37b01f6a
DG
8619 bfd_vma *result,
8620 bfd * abfd)
d9352518 8621{
a0c8462f
AM
8622 asection *curr;
8623 unsigned int len;
d9352518 8624
a0c8462f 8625 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8626 if (strcmp (curr->name, name) == 0)
8627 {
8628 *result = curr->vma;
8629 return TRUE;
8630 }
8631
8632 /* Hmm. still haven't found it. try pseudo-section names. */
37b01f6a 8633 /* FIXME: This could be coded more efficiently... */
a0c8462f 8634 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8635 {
8636 len = strlen (curr->name);
a0c8462f 8637 if (len > strlen (name))
d9352518
DB
8638 continue;
8639
8640 if (strncmp (curr->name, name, len) == 0)
8641 {
8642 if (strncmp (".end", name + len, 4) == 0)
8643 {
61826503 8644 *result = (curr->vma
bb294208 8645 + curr->size / bfd_octets_per_byte (abfd, curr));
d9352518
DB
8646 return TRUE;
8647 }
8648
8649 /* Insert more pseudo-section names here, if you like. */
8650 }
8651 }
a0c8462f 8652
d9352518
DB
8653 return FALSE;
8654}
8655
8656static void
a0c8462f 8657undefined_reference (const char *reftype, const char *name)
d9352518 8658{
695344c0 8659 /* xgettext:c-format */
a0c8462f
AM
8660 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8661 reftype, name);
4b69ce9b 8662 bfd_set_error (bfd_error_bad_value);
d9352518
DB
8663}
8664
8665static bfd_boolean
a0c8462f
AM
8666eval_symbol (bfd_vma *result,
8667 const char **symp,
8668 bfd *input_bfd,
8b127cbc 8669 struct elf_final_link_info *flinfo,
a0c8462f
AM
8670 bfd_vma dot,
8671 Elf_Internal_Sym *isymbuf,
8672 size_t locsymcount,
8673 int signed_p)
d9352518 8674{
4b93929b
NC
8675 size_t len;
8676 size_t symlen;
a0c8462f
AM
8677 bfd_vma a;
8678 bfd_vma b;
4b93929b 8679 char symbuf[4096];
0f02bbd9 8680 const char *sym = *symp;
a0c8462f
AM
8681 const char *symend;
8682 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
8683
8684 len = strlen (sym);
8685 symend = sym + len;
8686
4b93929b 8687 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
8688 {
8689 bfd_set_error (bfd_error_invalid_operation);
8690 return FALSE;
8691 }
a0c8462f 8692
d9352518
DB
8693 switch (* sym)
8694 {
8695 case '.':
0f02bbd9
AM
8696 *result = dot;
8697 *symp = sym + 1;
d9352518
DB
8698 return TRUE;
8699
8700 case '#':
0f02bbd9
AM
8701 ++sym;
8702 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
8703 return TRUE;
8704
8705 case 'S':
8706 symbol_is_section = TRUE;
1a0670f3 8707 /* Fall through. */
a0c8462f 8708 case 's':
0f02bbd9
AM
8709 ++sym;
8710 symlen = strtol (sym, (char **) symp, 10);
8711 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 8712
4b93929b 8713 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
8714 {
8715 bfd_set_error (bfd_error_invalid_operation);
8716 return FALSE;
8717 }
8718
8719 memcpy (symbuf, sym, symlen);
a0c8462f 8720 symbuf[symlen] = '\0';
0f02bbd9 8721 *symp = sym + symlen;
a0c8462f
AM
8722
8723 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
8724 the symbol as a section, or vice-versa. so we're pretty liberal in our
8725 interpretation here; section means "try section first", not "must be a
8726 section", and likewise with symbol. */
8727
a0c8462f 8728 if (symbol_is_section)
d9352518 8729 {
37b01f6a 8730 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8b127cbc 8731 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8732 isymbuf, locsymcount))
d9352518
DB
8733 {
8734 undefined_reference ("section", symbuf);
8735 return FALSE;
8736 }
a0c8462f
AM
8737 }
8738 else
d9352518 8739 {
8b127cbc 8740 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8741 isymbuf, locsymcount)
8b127cbc 8742 && !resolve_section (symbuf, flinfo->output_bfd->sections,
37b01f6a 8743 result, input_bfd))
d9352518
DB
8744 {
8745 undefined_reference ("symbol", symbuf);
8746 return FALSE;
8747 }
8748 }
8749
8750 return TRUE;
a0c8462f 8751
d9352518
DB
8752 /* All that remains are operators. */
8753
8754#define UNARY_OP(op) \
8755 if (strncmp (sym, #op, strlen (#op)) == 0) \
8756 { \
8757 sym += strlen (#op); \
a0c8462f
AM
8758 if (*sym == ':') \
8759 ++sym; \
0f02bbd9 8760 *symp = sym; \
8b127cbc 8761 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8762 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8763 return FALSE; \
8764 if (signed_p) \
0f02bbd9 8765 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
8766 else \
8767 *result = op a; \
d9352518
DB
8768 return TRUE; \
8769 }
8770
4b69ce9b 8771#define BINARY_OP_HEAD(op) \
d9352518
DB
8772 if (strncmp (sym, #op, strlen (#op)) == 0) \
8773 { \
8774 sym += strlen (#op); \
a0c8462f
AM
8775 if (*sym == ':') \
8776 ++sym; \
0f02bbd9 8777 *symp = sym; \
8b127cbc 8778 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8779 isymbuf, locsymcount, signed_p)) \
a0c8462f 8780 return FALSE; \
0f02bbd9 8781 ++*symp; \
8b127cbc 8782 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 8783 isymbuf, locsymcount, signed_p)) \
4b69ce9b
AM
8784 return FALSE;
8785#define BINARY_OP_TAIL(op) \
a0c8462f 8786 if (signed_p) \
0f02bbd9 8787 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
8788 else \
8789 *result = a op b; \
d9352518
DB
8790 return TRUE; \
8791 }
4b69ce9b 8792#define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
d9352518
DB
8793
8794 default:
8795 UNARY_OP (0-);
4b69ce9b
AM
8796 BINARY_OP_HEAD (<<);
8797 if (b >= sizeof (a) * CHAR_BIT)
8798 {
8799 *result = 0;
8800 return TRUE;
8801 }
8802 signed_p = 0;
8803 BINARY_OP_TAIL (<<);
8804 BINARY_OP_HEAD (>>);
8805 if (b >= sizeof (a) * CHAR_BIT)
8806 {
8807 *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
8808 return TRUE;
8809 }
8810 BINARY_OP_TAIL (>>);
d9352518
DB
8811 BINARY_OP (==);
8812 BINARY_OP (!=);
8813 BINARY_OP (<=);
8814 BINARY_OP (>=);
8815 BINARY_OP (&&);
8816 BINARY_OP (||);
8817 UNARY_OP (~);
8818 UNARY_OP (!);
8819 BINARY_OP (*);
4b69ce9b
AM
8820 BINARY_OP_HEAD (/);
8821 if (b == 0)
8822 {
8823 _bfd_error_handler (_("division by zero"));
8824 bfd_set_error (bfd_error_bad_value);
8825 return FALSE;
8826 }
8827 BINARY_OP_TAIL (/);
8828 BINARY_OP_HEAD (%);
8829 if (b == 0)
8830 {
8831 _bfd_error_handler (_("division by zero"));
8832 bfd_set_error (bfd_error_bad_value);
8833 return FALSE;
8834 }
8835 BINARY_OP_TAIL (%);
d9352518
DB
8836 BINARY_OP (^);
8837 BINARY_OP (|);
8838 BINARY_OP (&);
8839 BINARY_OP (+);
8840 BINARY_OP (-);
8841 BINARY_OP (<);
8842 BINARY_OP (>);
8843#undef UNARY_OP
8844#undef BINARY_OP
8845 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8846 bfd_set_error (bfd_error_invalid_operation);
8847 return FALSE;
8848 }
8849}
8850
d9352518 8851static void
a0c8462f
AM
8852put_value (bfd_vma size,
8853 unsigned long chunksz,
8854 bfd *input_bfd,
8855 bfd_vma x,
8856 bfd_byte *location)
d9352518
DB
8857{
8858 location += (size - chunksz);
8859
41cd1ad1 8860 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
8861 {
8862 switch (chunksz)
8863 {
d9352518
DB
8864 case 1:
8865 bfd_put_8 (input_bfd, x, location);
41cd1ad1 8866 x >>= 8;
d9352518
DB
8867 break;
8868 case 2:
8869 bfd_put_16 (input_bfd, x, location);
41cd1ad1 8870 x >>= 16;
d9352518
DB
8871 break;
8872 case 4:
8873 bfd_put_32 (input_bfd, x, location);
65164438
NC
8874 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8875 x >>= 16;
8876 x >>= 16;
d9352518 8877 break;
d9352518 8878#ifdef BFD64
41cd1ad1 8879 case 8:
d9352518 8880 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
8881 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8882 x >>= 32;
8883 x >>= 32;
8884 break;
d9352518 8885#endif
41cd1ad1
NC
8886 default:
8887 abort ();
d9352518
DB
8888 break;
8889 }
8890 }
8891}
8892
a0c8462f
AM
8893static bfd_vma
8894get_value (bfd_vma size,
8895 unsigned long chunksz,
8896 bfd *input_bfd,
8897 bfd_byte *location)
d9352518 8898{
9b239e0e 8899 int shift;
d9352518
DB
8900 bfd_vma x = 0;
8901
9b239e0e
NC
8902 /* Sanity checks. */
8903 BFD_ASSERT (chunksz <= sizeof (x)
8904 && size >= chunksz
8905 && chunksz != 0
8906 && (size % chunksz) == 0
8907 && input_bfd != NULL
8908 && location != NULL);
8909
8910 if (chunksz == sizeof (x))
8911 {
8912 BFD_ASSERT (size == chunksz);
8913
8914 /* Make sure that we do not perform an undefined shift operation.
8915 We know that size == chunksz so there will only be one iteration
8916 of the loop below. */
8917 shift = 0;
8918 }
8919 else
8920 shift = 8 * chunksz;
8921
a0c8462f 8922 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8923 {
8924 switch (chunksz)
8925 {
d9352518 8926 case 1:
9b239e0e 8927 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8928 break;
8929 case 2:
9b239e0e 8930 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8931 break;
8932 case 4:
9b239e0e 8933 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8934 break;
d9352518 8935#ifdef BFD64
9b239e0e
NC
8936 case 8:
8937 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8938 break;
9b239e0e
NC
8939#endif
8940 default:
8941 abort ();
d9352518
DB
8942 }
8943 }
8944 return x;
8945}
8946
a0c8462f
AM
8947static void
8948decode_complex_addend (unsigned long *start, /* in bits */
8949 unsigned long *oplen, /* in bits */
8950 unsigned long *len, /* in bits */
8951 unsigned long *wordsz, /* in bytes */
8952 unsigned long *chunksz, /* in bytes */
8953 unsigned long *lsb0_p,
8954 unsigned long *signed_p,
8955 unsigned long *trunc_p,
8956 unsigned long encoded)
d9352518 8957{
07d6d2b8
AM
8958 * start = encoded & 0x3F;
8959 * len = (encoded >> 6) & 0x3F;
d9352518
DB
8960 * oplen = (encoded >> 12) & 0x3F;
8961 * wordsz = (encoded >> 18) & 0xF;
8962 * chunksz = (encoded >> 22) & 0xF;
8963 * lsb0_p = (encoded >> 27) & 1;
8964 * signed_p = (encoded >> 28) & 1;
8965 * trunc_p = (encoded >> 29) & 1;
8966}
8967
cdfeee4f 8968bfd_reloc_status_type
0f02bbd9 8969bfd_elf_perform_complex_relocation (bfd *input_bfd,
bb294208 8970 asection *input_section,
0f02bbd9
AM
8971 bfd_byte *contents,
8972 Elf_Internal_Rela *rel,
8973 bfd_vma relocation)
d9352518 8974{
0f02bbd9
AM
8975 bfd_vma shift, x, mask;
8976 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 8977 bfd_reloc_status_type r;
bb294208 8978 bfd_size_type octets;
d9352518
DB
8979
8980 /* Perform this reloc, since it is complex.
8981 (this is not to say that it necessarily refers to a complex
8982 symbol; merely that it is a self-describing CGEN based reloc.
8983 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 8984 word size, etc) encoded within it.). */
d9352518 8985
a0c8462f
AM
8986 decode_complex_addend (&start, &oplen, &len, &wordsz,
8987 &chunksz, &lsb0_p, &signed_p,
8988 &trunc_p, rel->r_addend);
d9352518
DB
8989
8990 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8991
8992 if (lsb0_p)
8993 shift = (start + 1) - len;
8994 else
8995 shift = (8 * wordsz) - (start + len);
8996
bb294208
AM
8997 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
8998 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
d9352518
DB
8999
9000#ifdef DEBUG
9001 printf ("Doing complex reloc: "
9002 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9003 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9004 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9005 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
9006 oplen, (unsigned long) x, (unsigned long) mask,
9007 (unsigned long) relocation);
d9352518
DB
9008#endif
9009
cdfeee4f 9010 r = bfd_reloc_ok;
d9352518 9011 if (! trunc_p)
cdfeee4f
AM
9012 /* Now do an overflow check. */
9013 r = bfd_check_overflow ((signed_p
9014 ? complain_overflow_signed
9015 : complain_overflow_unsigned),
9016 len, 0, (8 * wordsz),
9017 relocation);
a0c8462f 9018
d9352518
DB
9019 /* Do the deed. */
9020 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9021
9022#ifdef DEBUG
9023 printf (" relocation: %8.8lx\n"
9024 " shifted mask: %8.8lx\n"
9025 " shifted/masked reloc: %8.8lx\n"
9026 " result: %8.8lx\n",
9ccb8af9
AM
9027 (unsigned long) relocation, (unsigned long) (mask << shift),
9028 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 9029#endif
bb294208 9030 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
cdfeee4f 9031 return r;
d9352518
DB
9032}
9033
0e287786
AM
9034/* Functions to read r_offset from external (target order) reloc
9035 entry. Faster than bfd_getl32 et al, because we let the compiler
9036 know the value is aligned. */
53df40a4 9037
0e287786
AM
9038static bfd_vma
9039ext32l_r_offset (const void *p)
53df40a4
AM
9040{
9041 union aligned32
9042 {
9043 uint32_t v;
9044 unsigned char c[4];
9045 };
9046 const union aligned32 *a
0e287786 9047 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
9048
9049 uint32_t aval = ( (uint32_t) a->c[0]
9050 | (uint32_t) a->c[1] << 8
9051 | (uint32_t) a->c[2] << 16
9052 | (uint32_t) a->c[3] << 24);
0e287786 9053 return aval;
53df40a4
AM
9054}
9055
0e287786
AM
9056static bfd_vma
9057ext32b_r_offset (const void *p)
53df40a4
AM
9058{
9059 union aligned32
9060 {
9061 uint32_t v;
9062 unsigned char c[4];
9063 };
9064 const union aligned32 *a
0e287786 9065 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
9066
9067 uint32_t aval = ( (uint32_t) a->c[0] << 24
9068 | (uint32_t) a->c[1] << 16
9069 | (uint32_t) a->c[2] << 8
9070 | (uint32_t) a->c[3]);
0e287786 9071 return aval;
53df40a4
AM
9072}
9073
9074#ifdef BFD_HOST_64_BIT
0e287786
AM
9075static bfd_vma
9076ext64l_r_offset (const void *p)
53df40a4
AM
9077{
9078 union aligned64
9079 {
9080 uint64_t v;
9081 unsigned char c[8];
9082 };
9083 const union aligned64 *a
0e287786 9084 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
9085
9086 uint64_t aval = ( (uint64_t) a->c[0]
9087 | (uint64_t) a->c[1] << 8
9088 | (uint64_t) a->c[2] << 16
9089 | (uint64_t) a->c[3] << 24
9090 | (uint64_t) a->c[4] << 32
9091 | (uint64_t) a->c[5] << 40
9092 | (uint64_t) a->c[6] << 48
9093 | (uint64_t) a->c[7] << 56);
0e287786 9094 return aval;
53df40a4
AM
9095}
9096
0e287786
AM
9097static bfd_vma
9098ext64b_r_offset (const void *p)
53df40a4
AM
9099{
9100 union aligned64
9101 {
9102 uint64_t v;
9103 unsigned char c[8];
9104 };
9105 const union aligned64 *a
0e287786 9106 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
9107
9108 uint64_t aval = ( (uint64_t) a->c[0] << 56
9109 | (uint64_t) a->c[1] << 48
9110 | (uint64_t) a->c[2] << 40
9111 | (uint64_t) a->c[3] << 32
9112 | (uint64_t) a->c[4] << 24
9113 | (uint64_t) a->c[5] << 16
9114 | (uint64_t) a->c[6] << 8
9115 | (uint64_t) a->c[7]);
0e287786 9116 return aval;
53df40a4
AM
9117}
9118#endif
9119
c152c796
AM
9120/* When performing a relocatable link, the input relocations are
9121 preserved. But, if they reference global symbols, the indices
d4730f92
BS
9122 referenced must be updated. Update all the relocations found in
9123 RELDATA. */
c152c796 9124
bca6d0e3 9125static bfd_boolean
c152c796 9126elf_link_adjust_relocs (bfd *abfd,
9eaff861 9127 asection *sec,
28dbcedc 9128 struct bfd_elf_section_reloc_data *reldata,
10bbbc1d
NC
9129 bfd_boolean sort,
9130 struct bfd_link_info *info)
c152c796
AM
9131{
9132 unsigned int i;
9133 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9134 bfd_byte *erela;
9135 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9136 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9137 bfd_vma r_type_mask;
9138 int r_sym_shift;
d4730f92
BS
9139 unsigned int count = reldata->count;
9140 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 9141
d4730f92 9142 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
9143 {
9144 swap_in = bed->s->swap_reloc_in;
9145 swap_out = bed->s->swap_reloc_out;
9146 }
d4730f92 9147 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
9148 {
9149 swap_in = bed->s->swap_reloca_in;
9150 swap_out = bed->s->swap_reloca_out;
9151 }
9152 else
9153 abort ();
9154
9155 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9156 abort ();
9157
9158 if (bed->s->arch_size == 32)
9159 {
9160 r_type_mask = 0xff;
9161 r_sym_shift = 8;
9162 }
9163 else
9164 {
9165 r_type_mask = 0xffffffff;
9166 r_sym_shift = 32;
9167 }
9168
d4730f92
BS
9169 erela = reldata->hdr->contents;
9170 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
9171 {
9172 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9173 unsigned int j;
9174
9175 if (*rel_hash == NULL)
9176 continue;
9177
10bbbc1d
NC
9178 if ((*rel_hash)->indx == -2
9179 && info->gc_sections
9180 && ! info->gc_keep_exported)
9181 {
9182 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9793eb77 9183 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
10bbbc1d
NC
9184 abfd, sec,
9185 (*rel_hash)->root.root.string);
9793eb77 9186 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
d42c267e 9187 abfd, sec);
10bbbc1d
NC
9188 bfd_set_error (bfd_error_invalid_operation);
9189 return FALSE;
9190 }
c152c796
AM
9191 BFD_ASSERT ((*rel_hash)->indx >= 0);
9192
9193 (*swap_in) (abfd, erela, irela);
9194 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9195 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9196 | (irela[j].r_info & r_type_mask));
9197 (*swap_out) (abfd, irela, erela);
9198 }
53df40a4 9199
9eaff861
AO
9200 if (bed->elf_backend_update_relocs)
9201 (*bed->elf_backend_update_relocs) (sec, reldata);
9202
0e287786 9203 if (sort && count != 0)
53df40a4 9204 {
0e287786
AM
9205 bfd_vma (*ext_r_off) (const void *);
9206 bfd_vma r_off;
9207 size_t elt_size;
9208 bfd_byte *base, *end, *p, *loc;
bca6d0e3 9209 bfd_byte *buf = NULL;
28dbcedc
AM
9210
9211 if (bed->s->arch_size == 32)
9212 {
9213 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 9214 ext_r_off = ext32l_r_offset;
28dbcedc 9215 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 9216 ext_r_off = ext32b_r_offset;
28dbcedc
AM
9217 else
9218 abort ();
9219 }
53df40a4 9220 else
28dbcedc 9221 {
53df40a4 9222#ifdef BFD_HOST_64_BIT
28dbcedc 9223 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 9224 ext_r_off = ext64l_r_offset;
28dbcedc 9225 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 9226 ext_r_off = ext64b_r_offset;
28dbcedc 9227 else
53df40a4 9228#endif
28dbcedc
AM
9229 abort ();
9230 }
0e287786 9231
bca6d0e3
AM
9232 /* Must use a stable sort here. A modified insertion sort,
9233 since the relocs are mostly sorted already. */
0e287786
AM
9234 elt_size = reldata->hdr->sh_entsize;
9235 base = reldata->hdr->contents;
9236 end = base + count * elt_size;
bca6d0e3 9237 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
9238 abort ();
9239
9240 /* Ensure the first element is lowest. This acts as a sentinel,
9241 speeding the main loop below. */
9242 r_off = (*ext_r_off) (base);
9243 for (p = loc = base; (p += elt_size) < end; )
9244 {
9245 bfd_vma r_off2 = (*ext_r_off) (p);
9246 if (r_off > r_off2)
9247 {
9248 r_off = r_off2;
9249 loc = p;
9250 }
9251 }
9252 if (loc != base)
9253 {
9254 /* Don't just swap *base and *loc as that changes the order
9255 of the original base[0] and base[1] if they happen to
9256 have the same r_offset. */
bca6d0e3
AM
9257 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9258 memcpy (onebuf, loc, elt_size);
0e287786 9259 memmove (base + elt_size, base, loc - base);
bca6d0e3 9260 memcpy (base, onebuf, elt_size);
0e287786
AM
9261 }
9262
b29b8669 9263 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
9264 {
9265 /* base to p is sorted, *p is next to insert. */
9266 r_off = (*ext_r_off) (p);
9267 /* Search the sorted region for location to insert. */
9268 loc = p - elt_size;
9269 while (r_off < (*ext_r_off) (loc))
9270 loc -= elt_size;
9271 loc += elt_size;
9272 if (loc != p)
9273 {
bca6d0e3
AM
9274 /* Chances are there is a run of relocs to insert here,
9275 from one of more input files. Files are not always
9276 linked in order due to the way elf_link_input_bfd is
9277 called. See pr17666. */
9278 size_t sortlen = p - loc;
9279 bfd_vma r_off2 = (*ext_r_off) (loc);
9280 size_t runlen = elt_size;
9281 size_t buf_size = 96 * 1024;
9282 while (p + runlen < end
9283 && (sortlen <= buf_size
9284 || runlen + elt_size <= buf_size)
9285 && r_off2 > (*ext_r_off) (p + runlen))
9286 runlen += elt_size;
9287 if (buf == NULL)
9288 {
9289 buf = bfd_malloc (buf_size);
9290 if (buf == NULL)
9291 return FALSE;
9292 }
9293 if (runlen < sortlen)
9294 {
9295 memcpy (buf, p, runlen);
9296 memmove (loc + runlen, loc, sortlen);
9297 memcpy (loc, buf, runlen);
9298 }
9299 else
9300 {
9301 memcpy (buf, loc, sortlen);
9302 memmove (loc, p, runlen);
9303 memcpy (loc + runlen, buf, sortlen);
9304 }
b29b8669 9305 p += runlen - elt_size;
0e287786
AM
9306 }
9307 }
9308 /* Hashes are no longer valid. */
28dbcedc
AM
9309 free (reldata->hashes);
9310 reldata->hashes = NULL;
bca6d0e3 9311 free (buf);
53df40a4 9312 }
bca6d0e3 9313 return TRUE;
c152c796
AM
9314}
9315
9316struct elf_link_sort_rela
9317{
9318 union {
9319 bfd_vma offset;
9320 bfd_vma sym_mask;
9321 } u;
9322 enum elf_reloc_type_class type;
9323 /* We use this as an array of size int_rels_per_ext_rel. */
9324 Elf_Internal_Rela rela[1];
9325};
9326
dcea6a95
AM
9327/* qsort stability here and for cmp2 is only an issue if multiple
9328 dynamic relocations are emitted at the same address. But targets
9329 that apply a series of dynamic relocations each operating on the
9330 result of the prior relocation can't use -z combreloc as
9331 implemented anyway. Such schemes tend to be broken by sorting on
9332 symbol index. That leaves dynamic NONE relocs as the only other
9333 case where ld might emit multiple relocs at the same address, and
9334 those are only emitted due to target bugs. */
9335
c152c796
AM
9336static int
9337elf_link_sort_cmp1 (const void *A, const void *B)
9338{
a50b1753
NC
9339 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9340 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
9341 int relativea, relativeb;
9342
9343 relativea = a->type == reloc_class_relative;
9344 relativeb = b->type == reloc_class_relative;
9345
9346 if (relativea < relativeb)
9347 return 1;
9348 if (relativea > relativeb)
9349 return -1;
9350 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9351 return -1;
9352 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9353 return 1;
9354 if (a->rela->r_offset < b->rela->r_offset)
9355 return -1;
9356 if (a->rela->r_offset > b->rela->r_offset)
9357 return 1;
9358 return 0;
9359}
9360
9361static int
9362elf_link_sort_cmp2 (const void *A, const void *B)
9363{
a50b1753
NC
9364 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9365 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 9366
7e612e98 9367 if (a->type < b->type)
c152c796 9368 return -1;
7e612e98 9369 if (a->type > b->type)
c152c796 9370 return 1;
7e612e98 9371 if (a->u.offset < b->u.offset)
c152c796 9372 return -1;
7e612e98 9373 if (a->u.offset > b->u.offset)
c152c796
AM
9374 return 1;
9375 if (a->rela->r_offset < b->rela->r_offset)
9376 return -1;
9377 if (a->rela->r_offset > b->rela->r_offset)
9378 return 1;
9379 return 0;
9380}
9381
9382static size_t
9383elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9384{
3410fea8 9385 asection *dynamic_relocs;
fc66a176
L
9386 asection *rela_dyn;
9387 asection *rel_dyn;
c152c796
AM
9388 bfd_size_type count, size;
9389 size_t i, ret, sort_elt, ext_size;
9390 bfd_byte *sort, *s_non_relative, *p;
9391 struct elf_link_sort_rela *sq;
9392 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9393 int i2e = bed->s->int_rels_per_ext_rel;
61826503 9394 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
c152c796
AM
9395 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9396 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9397 struct bfd_link_order *lo;
9398 bfd_vma r_sym_mask;
3410fea8 9399 bfd_boolean use_rela;
c152c796 9400
3410fea8
NC
9401 /* Find a dynamic reloc section. */
9402 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9403 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9404 if (rela_dyn != NULL && rela_dyn->size > 0
9405 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 9406 {
3410fea8
NC
9407 bfd_boolean use_rela_initialised = FALSE;
9408
9409 /* This is just here to stop gcc from complaining.
c8e44c6d 9410 Its initialization checking code is not perfect. */
3410fea8
NC
9411 use_rela = TRUE;
9412
9413 /* Both sections are present. Examine the sizes
9414 of the indirect sections to help us choose. */
9415 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9416 if (lo->type == bfd_indirect_link_order)
9417 {
9418 asection *o = lo->u.indirect.section;
9419
9420 if ((o->size % bed->s->sizeof_rela) == 0)
9421 {
9422 if ((o->size % bed->s->sizeof_rel) == 0)
9423 /* Section size is divisible by both rel and rela sizes.
9424 It is of no help to us. */
9425 ;
9426 else
9427 {
9428 /* Section size is only divisible by rela. */
535b785f 9429 if (use_rela_initialised && !use_rela)
3410fea8 9430 {
9793eb77 9431 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9432 "they are in more than one size"),
9433 abfd);
3410fea8
NC
9434 bfd_set_error (bfd_error_invalid_operation);
9435 return 0;
9436 }
9437 else
9438 {
9439 use_rela = TRUE;
9440 use_rela_initialised = TRUE;
9441 }
9442 }
9443 }
9444 else if ((o->size % bed->s->sizeof_rel) == 0)
9445 {
9446 /* Section size is only divisible by rel. */
535b785f 9447 if (use_rela_initialised && use_rela)
3410fea8 9448 {
9793eb77 9449 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9450 "they are in more than one size"),
9451 abfd);
3410fea8
NC
9452 bfd_set_error (bfd_error_invalid_operation);
9453 return 0;
9454 }
9455 else
9456 {
9457 use_rela = FALSE;
9458 use_rela_initialised = TRUE;
9459 }
9460 }
9461 else
9462 {
c8e44c6d
AM
9463 /* The section size is not divisible by either -
9464 something is wrong. */
9793eb77 9465 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9466 "they are of an unknown size"), abfd);
3410fea8
NC
9467 bfd_set_error (bfd_error_invalid_operation);
9468 return 0;
9469 }
9470 }
9471
9472 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9473 if (lo->type == bfd_indirect_link_order)
9474 {
9475 asection *o = lo->u.indirect.section;
9476
9477 if ((o->size % bed->s->sizeof_rela) == 0)
9478 {
9479 if ((o->size % bed->s->sizeof_rel) == 0)
9480 /* Section size is divisible by both rel and rela sizes.
9481 It is of no help to us. */
9482 ;
9483 else
9484 {
9485 /* Section size is only divisible by rela. */
535b785f 9486 if (use_rela_initialised && !use_rela)
3410fea8 9487 {
9793eb77 9488 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9489 "they are in more than one size"),
9490 abfd);
3410fea8
NC
9491 bfd_set_error (bfd_error_invalid_operation);
9492 return 0;
9493 }
9494 else
9495 {
9496 use_rela = TRUE;
9497 use_rela_initialised = TRUE;
9498 }
9499 }
9500 }
9501 else if ((o->size % bed->s->sizeof_rel) == 0)
9502 {
9503 /* Section size is only divisible by rel. */
535b785f 9504 if (use_rela_initialised && use_rela)
3410fea8 9505 {
9793eb77 9506 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9507 "they are in more than one size"),
9508 abfd);
3410fea8
NC
9509 bfd_set_error (bfd_error_invalid_operation);
9510 return 0;
9511 }
9512 else
9513 {
9514 use_rela = FALSE;
9515 use_rela_initialised = TRUE;
9516 }
9517 }
9518 else
9519 {
c8e44c6d
AM
9520 /* The section size is not divisible by either -
9521 something is wrong. */
9793eb77 9522 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9523 "they are of an unknown size"), abfd);
3410fea8
NC
9524 bfd_set_error (bfd_error_invalid_operation);
9525 return 0;
9526 }
9527 }
9528
9529 if (! use_rela_initialised)
9530 /* Make a guess. */
9531 use_rela = TRUE;
c152c796 9532 }
fc66a176
L
9533 else if (rela_dyn != NULL && rela_dyn->size > 0)
9534 use_rela = TRUE;
9535 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 9536 use_rela = FALSE;
c152c796 9537 else
fc66a176 9538 return 0;
3410fea8
NC
9539
9540 if (use_rela)
c152c796 9541 {
3410fea8 9542 dynamic_relocs = rela_dyn;
c152c796
AM
9543 ext_size = bed->s->sizeof_rela;
9544 swap_in = bed->s->swap_reloca_in;
9545 swap_out = bed->s->swap_reloca_out;
9546 }
3410fea8
NC
9547 else
9548 {
9549 dynamic_relocs = rel_dyn;
9550 ext_size = bed->s->sizeof_rel;
9551 swap_in = bed->s->swap_reloc_in;
9552 swap_out = bed->s->swap_reloc_out;
9553 }
c152c796
AM
9554
9555 size = 0;
3410fea8 9556 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 9557 if (lo->type == bfd_indirect_link_order)
3410fea8 9558 size += lo->u.indirect.section->size;
c152c796 9559
3410fea8 9560 if (size != dynamic_relocs->size)
c152c796
AM
9561 return 0;
9562
9563 sort_elt = (sizeof (struct elf_link_sort_rela)
9564 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
9565
9566 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
9567 if (count == 0)
9568 return 0;
a50b1753 9569 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 9570
c152c796
AM
9571 if (sort == NULL)
9572 {
9573 (*info->callbacks->warning)
9793eb77 9574 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
c152c796
AM
9575 return 0;
9576 }
9577
9578 if (bed->s->arch_size == 32)
9579 r_sym_mask = ~(bfd_vma) 0xff;
9580 else
9581 r_sym_mask = ~(bfd_vma) 0xffffffff;
9582
3410fea8 9583 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9584 if (lo->type == bfd_indirect_link_order)
9585 {
9586 bfd_byte *erel, *erelend;
9587 asection *o = lo->u.indirect.section;
9588
1da212d6
AM
9589 if (o->contents == NULL && o->size != 0)
9590 {
9591 /* This is a reloc section that is being handled as a normal
9592 section. See bfd_section_from_shdr. We can't combine
9593 relocs in this case. */
9594 free (sort);
9595 return 0;
9596 }
c152c796 9597 erel = o->contents;
eea6121a 9598 erelend = o->contents + o->size;
c8e44c6d 9599 p = sort + o->output_offset * opb / ext_size * sort_elt;
3410fea8 9600
c152c796
AM
9601 while (erel < erelend)
9602 {
9603 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 9604
c152c796 9605 (*swap_in) (abfd, erel, s->rela);
7e612e98 9606 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
9607 s->u.sym_mask = r_sym_mask;
9608 p += sort_elt;
9609 erel += ext_size;
9610 }
9611 }
9612
9613 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9614
9615 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9616 {
9617 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9618 if (s->type != reloc_class_relative)
9619 break;
9620 }
9621 ret = i;
9622 s_non_relative = p;
9623
9624 sq = (struct elf_link_sort_rela *) s_non_relative;
9625 for (; i < count; i++, p += sort_elt)
9626 {
9627 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9628 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9629 sq = sp;
9630 sp->u.offset = sq->rela->r_offset;
9631 }
9632
9633 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9634
c8e44c6d
AM
9635 struct elf_link_hash_table *htab = elf_hash_table (info);
9636 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9637 {
9638 /* We have plt relocs in .rela.dyn. */
9639 sq = (struct elf_link_sort_rela *) sort;
9640 for (i = 0; i < count; i++)
9641 if (sq[count - i - 1].type != reloc_class_plt)
9642 break;
9643 if (i != 0 && htab->srelplt->size == i * ext_size)
9644 {
9645 struct bfd_link_order **plo;
9646 /* Put srelplt link_order last. This is so the output_offset
9647 set in the next loop is correct for DT_JMPREL. */
9648 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9649 if ((*plo)->type == bfd_indirect_link_order
9650 && (*plo)->u.indirect.section == htab->srelplt)
9651 {
9652 lo = *plo;
9653 *plo = lo->next;
9654 }
9655 else
9656 plo = &(*plo)->next;
9657 *plo = lo;
9658 lo->next = NULL;
9659 dynamic_relocs->map_tail.link_order = lo;
9660 }
9661 }
9662
9663 p = sort;
3410fea8 9664 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9665 if (lo->type == bfd_indirect_link_order)
9666 {
9667 bfd_byte *erel, *erelend;
9668 asection *o = lo->u.indirect.section;
9669
9670 erel = o->contents;
eea6121a 9671 erelend = o->contents + o->size;
c8e44c6d 9672 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
c152c796
AM
9673 while (erel < erelend)
9674 {
9675 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9676 (*swap_out) (abfd, s->rela, erel);
9677 p += sort_elt;
9678 erel += ext_size;
9679 }
9680 }
9681
9682 free (sort);
3410fea8 9683 *psec = dynamic_relocs;
c152c796
AM
9684 return ret;
9685}
9686
ef10c3ac 9687/* Add a symbol to the output symbol string table. */
c152c796 9688
6e0b88f1 9689static int
ef10c3ac
L
9690elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9691 const char *name,
9692 Elf_Internal_Sym *elfsym,
9693 asection *input_sec,
9694 struct elf_link_hash_entry *h)
c152c796 9695{
6e0b88f1 9696 int (*output_symbol_hook)
c152c796
AM
9697 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9698 struct elf_link_hash_entry *);
ef10c3ac 9699 struct elf_link_hash_table *hash_table;
c152c796 9700 const struct elf_backend_data *bed;
ef10c3ac 9701 bfd_size_type strtabsize;
c152c796 9702
8539e4e8
AM
9703 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9704
8b127cbc 9705 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
9706 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9707 if (output_symbol_hook != NULL)
9708 {
8b127cbc 9709 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
9710 if (ret != 1)
9711 return ret;
c152c796
AM
9712 }
9713
06f44071
AM
9714 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
9715 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
9716 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
9717 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
9718
ef10c3ac
L
9719 if (name == NULL
9720 || *name == '\0'
9721 || (input_sec->flags & SEC_EXCLUDE))
9722 elfsym->st_name = (unsigned long) -1;
c152c796
AM
9723 else
9724 {
ef10c3ac
L
9725 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9726 to get the final offset for st_name. */
3f2e9699 9727 char *versioned_name = (char *) name;
496afd17
L
9728 if (h != NULL)
9729 {
9730 if (h->versioned == versioned && h->def_dynamic)
9731 {
9732 /* Keep only one '@' for versioned symbols defined in
9733 shared objects. */
9734 char *version = strrchr (name, ELF_VER_CHR);
9735 char *base_end = strchr (name, ELF_VER_CHR);
9736 if (version != base_end)
9737 {
9738 size_t base_len;
9739 size_t len = strlen (name);
9740 versioned_name = bfd_alloc (flinfo->output_bfd, len);
9741 if (versioned_name == NULL)
9742 return 0;
9743 base_len = base_end - name;
9744 memcpy (versioned_name, name, base_len);
9745 memcpy (versioned_name + base_len, version,
9746 len - base_len);
9747 }
9748 }
9749 }
9750 else if (flinfo->info->unique_symbol
9751 && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
3f2e9699 9752 {
496afd17
L
9753 struct local_hash_entry *lh;
9754 switch (ELF_ST_TYPE (elfsym->st_info))
3f2e9699 9755 {
496afd17
L
9756 case STT_FILE:
9757 case STT_SECTION:
9758 break;
9759 default:
9760 lh = (struct local_hash_entry *) bfd_hash_lookup
9761 (&flinfo->local_hash_table, name, TRUE, FALSE);
9762 if (lh == NULL)
3f2e9699 9763 return 0;
496afd17
L
9764 if (lh->count)
9765 {
9766 /* Append ".COUNT" to duplicated local symbols. */
9767 size_t count_len;
9768 size_t base_len = lh->size;
9769 char buf[30];
9770 sprintf (buf, "%lx", lh->count);
9771 if (!base_len)
9772 {
9773 base_len = strlen (name);
9774 lh->size = base_len;
9775 }
9776 count_len = strlen (buf);
9777 versioned_name = bfd_alloc (flinfo->output_bfd,
9778 base_len + count_len + 2);
9779 if (versioned_name == NULL)
9780 return 0;
9781 memcpy (versioned_name, name, base_len);
9782 versioned_name[base_len] = '.';
9783 memcpy (versioned_name + base_len + 1, buf,
9784 count_len + 1);
9785 }
9786 lh->count++;
9787 break;
3f2e9699
L
9788 }
9789 }
ef10c3ac
L
9790 elfsym->st_name
9791 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
3f2e9699 9792 versioned_name, FALSE);
c152c796 9793 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 9794 return 0;
c152c796
AM
9795 }
9796
ef10c3ac
L
9797 hash_table = elf_hash_table (flinfo->info);
9798 strtabsize = hash_table->strtabsize;
9799 if (strtabsize <= hash_table->strtabcount)
c152c796 9800 {
ef10c3ac
L
9801 strtabsize += strtabsize;
9802 hash_table->strtabsize = strtabsize;
9803 strtabsize *= sizeof (*hash_table->strtab);
9804 hash_table->strtab
9805 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9806 strtabsize);
9807 if (hash_table->strtab == NULL)
6e0b88f1 9808 return 0;
c152c796 9809 }
ef10c3ac
L
9810 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9811 hash_table->strtab[hash_table->strtabcount].dest_index
9812 = hash_table->strtabcount;
9813 hash_table->strtab[hash_table->strtabcount].destshndx_index
9814 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9815
ed48ec2e 9816 flinfo->output_bfd->symcount += 1;
ef10c3ac
L
9817 hash_table->strtabcount += 1;
9818
9819 return 1;
9820}
9821
9822/* Swap symbols out to the symbol table and flush the output symbols to
9823 the file. */
9824
9825static bfd_boolean
9826elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9827{
9828 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
986f0783 9829 size_t amt;
ef53be89 9830 size_t i;
ef10c3ac
L
9831 const struct elf_backend_data *bed;
9832 bfd_byte *symbuf;
9833 Elf_Internal_Shdr *hdr;
9834 file_ptr pos;
9835 bfd_boolean ret;
9836
9837 if (!hash_table->strtabcount)
9838 return TRUE;
9839
9840 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9841
9842 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9843
ef10c3ac
L
9844 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9845 symbuf = (bfd_byte *) bfd_malloc (amt);
9846 if (symbuf == NULL)
9847 return FALSE;
1b786873 9848
ef10c3ac 9849 if (flinfo->symshndxbuf)
c152c796 9850 {
ef53be89
AM
9851 amt = sizeof (Elf_External_Sym_Shndx);
9852 amt *= bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9853 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9854 if (flinfo->symshndxbuf == NULL)
c152c796 9855 {
ef10c3ac
L
9856 free (symbuf);
9857 return FALSE;
c152c796 9858 }
c152c796
AM
9859 }
9860
3d16b64e 9861 /* Now swap out the symbols. */
ef10c3ac
L
9862 for (i = 0; i < hash_table->strtabcount; i++)
9863 {
9864 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9865 if (elfsym->sym.st_name == (unsigned long) -1)
9866 elfsym->sym.st_name = 0;
9867 else
9868 elfsym->sym.st_name
9869 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9870 elfsym->sym.st_name);
3d16b64e
NA
9871
9872 /* Inform the linker of the addition of this symbol. */
9873
9874 if (flinfo->info->callbacks->ctf_new_symbol)
9875 flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
9876 &elfsym->sym);
9877
ef10c3ac
L
9878 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9879 ((bfd_byte *) symbuf
9880 + (elfsym->dest_index
9881 * bed->s->sizeof_sym)),
9882 (flinfo->symshndxbuf
9883 + elfsym->destshndx_index));
9884 }
9885
9886 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9887 pos = hdr->sh_offset + hdr->sh_size;
9888 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9889 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9890 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9891 {
9892 hdr->sh_size += amt;
9893 ret = TRUE;
9894 }
9895 else
9896 ret = FALSE;
c152c796 9897
ef10c3ac
L
9898 free (symbuf);
9899
9900 free (hash_table->strtab);
9901 hash_table->strtab = NULL;
9902
9903 return ret;
c152c796
AM
9904}
9905
c0d5a53d
L
9906/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9907
9908static bfd_boolean
9909check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9910{
4fbb74a6
AM
9911 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9912 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
9913 {
9914 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 9915 beyond 64k. */
4eca0228 9916 _bfd_error_handler
695344c0 9917 /* xgettext:c-format */
9793eb77 9918 (_("%pB: too many sections: %d (>= %d)"),
4fbb74a6 9919 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
9920 bfd_set_error (bfd_error_nonrepresentable_section);
9921 return FALSE;
9922 }
9923 return TRUE;
9924}
9925
c152c796
AM
9926/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9927 allowing an unsatisfied unversioned symbol in the DSO to match a
9928 versioned symbol that would normally require an explicit version.
9929 We also handle the case that a DSO references a hidden symbol
9930 which may be satisfied by a versioned symbol in another DSO. */
9931
9932static bfd_boolean
9933elf_link_check_versioned_symbol (struct bfd_link_info *info,
9934 const struct elf_backend_data *bed,
9935 struct elf_link_hash_entry *h)
9936{
9937 bfd *abfd;
9938 struct elf_link_loaded_list *loaded;
9939
9940 if (!is_elf_hash_table (info->hash))
9941 return FALSE;
9942
90c984fc
L
9943 /* Check indirect symbol. */
9944 while (h->root.type == bfd_link_hash_indirect)
9945 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9946
c152c796
AM
9947 switch (h->root.type)
9948 {
9949 default:
9950 abfd = NULL;
9951 break;
9952
9953 case bfd_link_hash_undefined:
9954 case bfd_link_hash_undefweak:
9955 abfd = h->root.u.undef.abfd;
f4ab0e2d
L
9956 if (abfd == NULL
9957 || (abfd->flags & DYNAMIC) == 0
e56f61be 9958 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
9959 return FALSE;
9960 break;
9961
9962 case bfd_link_hash_defined:
9963 case bfd_link_hash_defweak:
9964 abfd = h->root.u.def.section->owner;
9965 break;
9966
9967 case bfd_link_hash_common:
9968 abfd = h->root.u.c.p->section->owner;
9969 break;
9970 }
9971 BFD_ASSERT (abfd != NULL);
9972
e310298c 9973 for (loaded = elf_hash_table (info)->dyn_loaded;
c152c796
AM
9974 loaded != NULL;
9975 loaded = loaded->next)
9976 {
9977 bfd *input;
9978 Elf_Internal_Shdr *hdr;
ef53be89
AM
9979 size_t symcount;
9980 size_t extsymcount;
9981 size_t extsymoff;
c152c796
AM
9982 Elf_Internal_Shdr *versymhdr;
9983 Elf_Internal_Sym *isym;
9984 Elf_Internal_Sym *isymend;
9985 Elf_Internal_Sym *isymbuf;
9986 Elf_External_Versym *ever;
9987 Elf_External_Versym *extversym;
9988
9989 input = loaded->abfd;
9990
9991 /* We check each DSO for a possible hidden versioned definition. */
9992 if (input == abfd
c152c796
AM
9993 || elf_dynversym (input) == 0)
9994 continue;
9995
9996 hdr = &elf_tdata (input)->dynsymtab_hdr;
9997
9998 symcount = hdr->sh_size / bed->s->sizeof_sym;
9999 if (elf_bad_symtab (input))
10000 {
10001 extsymcount = symcount;
10002 extsymoff = 0;
10003 }
10004 else
10005 {
10006 extsymcount = symcount - hdr->sh_info;
10007 extsymoff = hdr->sh_info;
10008 }
10009
10010 if (extsymcount == 0)
10011 continue;
10012
10013 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10014 NULL, NULL, NULL);
10015 if (isymbuf == NULL)
10016 return FALSE;
10017
10018 /* Read in any version definitions. */
10019 versymhdr = &elf_tdata (input)->dynversym_hdr;
c152c796 10020 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
2bb3687b
AM
10021 || (extversym = (Elf_External_Versym *)
10022 _bfd_malloc_and_read (input, versymhdr->sh_size,
10023 versymhdr->sh_size)) == NULL)
c152c796 10024 {
c152c796
AM
10025 free (isymbuf);
10026 return FALSE;
10027 }
10028
10029 ever = extversym + extsymoff;
10030 isymend = isymbuf + extsymcount;
10031 for (isym = isymbuf; isym < isymend; isym++, ever++)
10032 {
10033 const char *name;
10034 Elf_Internal_Versym iver;
10035 unsigned short version_index;
10036
10037 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10038 || isym->st_shndx == SHN_UNDEF)
10039 continue;
10040
10041 name = bfd_elf_string_from_elf_section (input,
10042 hdr->sh_link,
10043 isym->st_name);
10044 if (strcmp (name, h->root.root.string) != 0)
10045 continue;
10046
10047 _bfd_elf_swap_versym_in (input, ever, &iver);
10048
d023c380
L
10049 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10050 && !(h->def_regular
10051 && h->forced_local))
c152c796
AM
10052 {
10053 /* If we have a non-hidden versioned sym, then it should
d023c380
L
10054 have provided a definition for the undefined sym unless
10055 it is defined in a non-shared object and forced local.
10056 */
c152c796
AM
10057 abort ();
10058 }
10059
10060 version_index = iver.vs_vers & VERSYM_VERSION;
10061 if (version_index == 1 || version_index == 2)
10062 {
10063 /* This is the base or first version. We can use it. */
10064 free (extversym);
10065 free (isymbuf);
10066 return TRUE;
10067 }
10068 }
10069
10070 free (extversym);
10071 free (isymbuf);
10072 }
10073
10074 return FALSE;
10075}
10076
b8871f35
L
10077/* Convert ELF common symbol TYPE. */
10078
10079static int
10080elf_link_convert_common_type (struct bfd_link_info *info, int type)
10081{
10082 /* Commom symbol can only appear in relocatable link. */
10083 if (!bfd_link_relocatable (info))
10084 abort ();
10085 switch (info->elf_stt_common)
10086 {
10087 case unchanged:
10088 break;
10089 case elf_stt_common:
10090 type = STT_COMMON;
10091 break;
10092 case no_elf_stt_common:
10093 type = STT_OBJECT;
10094 break;
10095 }
10096 return type;
10097}
10098
c152c796
AM
10099/* Add an external symbol to the symbol table. This is called from
10100 the hash table traversal routine. When generating a shared object,
10101 we go through the symbol table twice. The first time we output
10102 anything that might have been forced to local scope in a version
10103 script. The second time we output the symbols that are still
10104 global symbols. */
10105
10106static bfd_boolean
7686d77d 10107elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 10108{
7686d77d 10109 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 10110 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 10111 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
10112 bfd_boolean strip;
10113 Elf_Internal_Sym sym;
10114 asection *input_sec;
10115 const struct elf_backend_data *bed;
6e0b88f1
AM
10116 long indx;
10117 int ret;
b8871f35 10118 unsigned int type;
c152c796
AM
10119
10120 if (h->root.type == bfd_link_hash_warning)
10121 {
10122 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10123 if (h->root.type == bfd_link_hash_new)
10124 return TRUE;
10125 }
10126
10127 /* Decide whether to output this symbol in this pass. */
10128 if (eoinfo->localsyms)
10129 {
4deb8f71 10130 if (!h->forced_local)
c152c796
AM
10131 return TRUE;
10132 }
10133 else
10134 {
4deb8f71 10135 if (h->forced_local)
c152c796
AM
10136 return TRUE;
10137 }
10138
8b127cbc 10139 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 10140
12ac1cf5 10141 if (h->root.type == bfd_link_hash_undefined)
c152c796 10142 {
12ac1cf5
NC
10143 /* If we have an undefined symbol reference here then it must have
10144 come from a shared library that is being linked in. (Undefined
98da7939
L
10145 references in regular files have already been handled unless
10146 they are in unreferenced sections which are removed by garbage
10147 collection). */
12ac1cf5
NC
10148 bfd_boolean ignore_undef = FALSE;
10149
10150 /* Some symbols may be special in that the fact that they're
10151 undefined can be safely ignored - let backend determine that. */
10152 if (bed->elf_backend_ignore_undef_symbol)
10153 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10154
10155 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 10156 if (!ignore_undef
c54f1524 10157 && h->ref_dynamic_nonweak
8b127cbc
AM
10158 && (!h->ref_regular || flinfo->info->gc_sections)
10159 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10160 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
95a51568
FS
10161 {
10162 flinfo->info->callbacks->undefined_symbol
10163 (flinfo->info, h->root.root.string,
10164 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10165 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10166 && !flinfo->info->warn_unresolved_syms);
10167 }
97196564
L
10168
10169 /* Strip a global symbol defined in a discarded section. */
10170 if (h->indx == -3)
10171 return TRUE;
c152c796
AM
10172 }
10173
10174 /* We should also warn if a forced local symbol is referenced from
10175 shared libraries. */
0e1862bb 10176 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
10177 && h->forced_local
10178 && h->ref_dynamic
371a5866 10179 && h->def_regular
f5385ebf 10180 && !h->dynamic_def
ee659f1f 10181 && h->ref_dynamic_nonweak
8b127cbc 10182 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 10183 {
17d078c5
AM
10184 bfd *def_bfd;
10185 const char *msg;
90c984fc
L
10186 struct elf_link_hash_entry *hi = h;
10187
10188 /* Check indirect symbol. */
10189 while (hi->root.type == bfd_link_hash_indirect)
10190 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
10191
10192 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
695344c0 10193 /* xgettext:c-format */
871b3ab2 10194 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
17d078c5 10195 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
695344c0 10196 /* xgettext:c-format */
871b3ab2 10197 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
17d078c5 10198 else
695344c0 10199 /* xgettext:c-format */
871b3ab2 10200 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
8b127cbc 10201 def_bfd = flinfo->output_bfd;
90c984fc
L
10202 if (hi->root.u.def.section != bfd_abs_section_ptr)
10203 def_bfd = hi->root.u.def.section->owner;
c08bb8dd
AM
10204 _bfd_error_handler (msg, flinfo->output_bfd,
10205 h->root.root.string, def_bfd);
17d078c5 10206 bfd_set_error (bfd_error_bad_value);
c152c796
AM
10207 eoinfo->failed = TRUE;
10208 return FALSE;
10209 }
10210
10211 /* We don't want to output symbols that have never been mentioned by
10212 a regular file, or that we have been told to strip. However, if
10213 h->indx is set to -2, the symbol is used by a reloc and we must
10214 output it. */
d983c8c5 10215 strip = FALSE;
c152c796 10216 if (h->indx == -2)
d983c8c5 10217 ;
f5385ebf 10218 else if ((h->def_dynamic
77cfaee6
AM
10219 || h->ref_dynamic
10220 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
10221 && !h->def_regular
10222 && !h->ref_regular)
c152c796 10223 strip = TRUE;
8b127cbc 10224 else if (flinfo->info->strip == strip_all)
c152c796 10225 strip = TRUE;
8b127cbc
AM
10226 else if (flinfo->info->strip == strip_some
10227 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
10228 h->root.root.string, FALSE, FALSE) == NULL)
10229 strip = TRUE;
d56d55e7
AM
10230 else if ((h->root.type == bfd_link_hash_defined
10231 || h->root.type == bfd_link_hash_defweak)
8b127cbc 10232 && ((flinfo->info->strip_discarded
dbaa2011 10233 && discarded_section (h->root.u.def.section))
ca4be51c
AM
10234 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10235 && h->root.u.def.section->owner != NULL
d56d55e7 10236 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 10237 strip = TRUE;
9e2278f5
AM
10238 else if ((h->root.type == bfd_link_hash_undefined
10239 || h->root.type == bfd_link_hash_undefweak)
10240 && h->root.u.undef.abfd != NULL
10241 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10242 strip = TRUE;
c152c796 10243
b8871f35
L
10244 type = h->type;
10245
c152c796 10246 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
10247 nothing else to do. However, if it is a forced local symbol or
10248 an ifunc symbol we need to give the backend finish_dynamic_symbol
10249 function a chance to make it dynamic. */
c152c796
AM
10250 if (strip
10251 && h->dynindx == -1
b8871f35 10252 && type != STT_GNU_IFUNC
f5385ebf 10253 && !h->forced_local)
c152c796
AM
10254 return TRUE;
10255
10256 sym.st_value = 0;
10257 sym.st_size = h->size;
10258 sym.st_other = h->other;
c152c796
AM
10259 switch (h->root.type)
10260 {
10261 default:
10262 case bfd_link_hash_new:
10263 case bfd_link_hash_warning:
10264 abort ();
10265 return FALSE;
10266
10267 case bfd_link_hash_undefined:
10268 case bfd_link_hash_undefweak:
10269 input_sec = bfd_und_section_ptr;
10270 sym.st_shndx = SHN_UNDEF;
10271 break;
10272
10273 case bfd_link_hash_defined:
10274 case bfd_link_hash_defweak:
10275 {
10276 input_sec = h->root.u.def.section;
10277 if (input_sec->output_section != NULL)
10278 {
10279 sym.st_shndx =
8b127cbc 10280 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
10281 input_sec->output_section);
10282 if (sym.st_shndx == SHN_BAD)
10283 {
4eca0228 10284 _bfd_error_handler
695344c0 10285 /* xgettext:c-format */
871b3ab2 10286 (_("%pB: could not find output section %pA for input section %pA"),
8b127cbc 10287 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 10288 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
10289 eoinfo->failed = TRUE;
10290 return FALSE;
10291 }
10292
10293 /* ELF symbols in relocatable files are section relative,
10294 but in nonrelocatable files they are virtual
10295 addresses. */
10296 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 10297 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10298 {
10299 sym.st_value += input_sec->output_section->vma;
10300 if (h->type == STT_TLS)
10301 {
8b127cbc 10302 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
10303 if (tls_sec != NULL)
10304 sym.st_value -= tls_sec->vma;
c152c796
AM
10305 }
10306 }
10307 }
10308 else
10309 {
10310 BFD_ASSERT (input_sec->owner == NULL
10311 || (input_sec->owner->flags & DYNAMIC) != 0);
10312 sym.st_shndx = SHN_UNDEF;
10313 input_sec = bfd_und_section_ptr;
10314 }
10315 }
10316 break;
10317
10318 case bfd_link_hash_common:
10319 input_sec = h->root.u.c.p->section;
a4d8e49b 10320 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
10321 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10322 break;
10323
10324 case bfd_link_hash_indirect:
10325 /* These symbols are created by symbol versioning. They point
10326 to the decorated version of the name. For example, if the
10327 symbol foo@@GNU_1.2 is the default, which should be used when
10328 foo is used with no version, then we add an indirect symbol
10329 foo which points to foo@@GNU_1.2. We ignore these symbols,
10330 since the indirected symbol is already in the hash table. */
10331 return TRUE;
10332 }
10333
b8871f35
L
10334 if (type == STT_COMMON || type == STT_OBJECT)
10335 switch (h->root.type)
10336 {
10337 case bfd_link_hash_common:
10338 type = elf_link_convert_common_type (flinfo->info, type);
10339 break;
10340 case bfd_link_hash_defined:
10341 case bfd_link_hash_defweak:
10342 if (bed->common_definition (&sym))
10343 type = elf_link_convert_common_type (flinfo->info, type);
10344 else
10345 type = STT_OBJECT;
10346 break;
10347 case bfd_link_hash_undefined:
10348 case bfd_link_hash_undefweak:
10349 break;
10350 default:
10351 abort ();
10352 }
10353
4deb8f71 10354 if (h->forced_local)
b8871f35
L
10355 {
10356 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10357 /* Turn off visibility on local symbol. */
10358 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10359 }
10360 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10361 else if (h->unique_global && h->def_regular)
10362 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10363 else if (h->root.type == bfd_link_hash_undefweak
10364 || h->root.type == bfd_link_hash_defweak)
10365 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10366 else
10367 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10368 sym.st_target_internal = h->target_internal;
10369
c152c796
AM
10370 /* Give the processor backend a chance to tweak the symbol value,
10371 and also to finish up anything that needs to be done for this
10372 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 10373 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 10374 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 10375 if ((h->type == STT_GNU_IFUNC
5f35ea9c 10376 && h->def_regular
0e1862bb 10377 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
10378 || ((h->dynindx != -1
10379 || h->forced_local)
0e1862bb 10380 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
10381 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10382 || h->root.type != bfd_link_hash_undefweak))
10383 || !h->forced_local)
8b127cbc 10384 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
10385 {
10386 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 10387 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
10388 {
10389 eoinfo->failed = TRUE;
10390 return FALSE;
10391 }
10392 }
10393
10394 /* If we are marking the symbol as undefined, and there are no
10395 non-weak references to this symbol from a regular object, then
10396 mark the symbol as weak undefined; if there are non-weak
10397 references, mark the symbol as strong. We can't do this earlier,
10398 because it might not be marked as undefined until the
10399 finish_dynamic_symbol routine gets through with it. */
10400 if (sym.st_shndx == SHN_UNDEF
f5385ebf 10401 && h->ref_regular
c152c796
AM
10402 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10403 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10404 {
10405 int bindtype;
b8871f35 10406 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
10407
10408 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10409 if (type == STT_GNU_IFUNC)
10410 type = STT_FUNC;
c152c796 10411
f5385ebf 10412 if (h->ref_regular_nonweak)
c152c796
AM
10413 bindtype = STB_GLOBAL;
10414 else
10415 bindtype = STB_WEAK;
2955ec4c 10416 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
10417 }
10418
bda987c2
CD
10419 /* If this is a symbol defined in a dynamic library, don't use the
10420 symbol size from the dynamic library. Relinking an executable
10421 against a new library may introduce gratuitous changes in the
10422 executable's symbols if we keep the size. */
10423 if (sym.st_shndx == SHN_UNDEF
10424 && !h->def_regular
10425 && h->def_dynamic)
10426 sym.st_size = 0;
10427
c152c796
AM
10428 /* If a non-weak symbol with non-default visibility is not defined
10429 locally, it is a fatal error. */
0e1862bb 10430 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
10431 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10432 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10433 && h->root.type == bfd_link_hash_undefined
f5385ebf 10434 && !h->def_regular)
c152c796 10435 {
17d078c5
AM
10436 const char *msg;
10437
10438 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
695344c0 10439 /* xgettext:c-format */
871b3ab2 10440 msg = _("%pB: protected symbol `%s' isn't defined");
17d078c5 10441 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
695344c0 10442 /* xgettext:c-format */
871b3ab2 10443 msg = _("%pB: internal symbol `%s' isn't defined");
17d078c5 10444 else
695344c0 10445 /* xgettext:c-format */
871b3ab2 10446 msg = _("%pB: hidden symbol `%s' isn't defined");
4eca0228 10447 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 10448 bfd_set_error (bfd_error_bad_value);
c152c796
AM
10449 eoinfo->failed = TRUE;
10450 return FALSE;
10451 }
10452
10453 /* If this symbol should be put in the .dynsym section, then put it
10454 there now. We already know the symbol index. We also fill in
10455 the entry in the .hash section. */
1c2649ed
EB
10456 if (h->dynindx != -1
10457 && elf_hash_table (flinfo->info)->dynamic_sections_created
10458 && elf_hash_table (flinfo->info)->dynsym != NULL
10459 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
c152c796 10460 {
c152c796
AM
10461 bfd_byte *esym;
10462
90c984fc
L
10463 /* Since there is no version information in the dynamic string,
10464 if there is no version info in symbol version section, we will
1659f720 10465 have a run-time problem if not linking executable, referenced
4deb8f71 10466 by shared library, or not bound locally. */
1659f720 10467 if (h->verinfo.verdef == NULL
0e1862bb 10468 && (!bfd_link_executable (flinfo->info)
1659f720
L
10469 || h->ref_dynamic
10470 || !h->def_regular))
90c984fc
L
10471 {
10472 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10473
10474 if (p && p [1] != '\0')
10475 {
4eca0228 10476 _bfd_error_handler
695344c0 10477 /* xgettext:c-format */
9793eb77 10478 (_("%pB: no symbol version section for versioned symbol `%s'"),
90c984fc
L
10479 flinfo->output_bfd, h->root.root.string);
10480 eoinfo->failed = TRUE;
10481 return FALSE;
10482 }
10483 }
10484
c152c796 10485 sym.st_name = h->dynstr_index;
cae1fbbb
L
10486 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10487 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 10488 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
10489 {
10490 eoinfo->failed = TRUE;
10491 return FALSE;
10492 }
3d16b64e
NA
10493
10494 /* Inform the linker of the addition of this symbol. */
10495
10496 if (flinfo->info->callbacks->ctf_new_dynsym)
10497 flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
10498
8b127cbc 10499 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 10500
8b127cbc 10501 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
10502 {
10503 size_t hash_entry_size;
10504 bfd_byte *bucketpos;
10505 bfd_vma chain;
41198d0c
L
10506 size_t bucketcount;
10507 size_t bucket;
10508
8b127cbc 10509 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 10510 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
10511
10512 hash_entry_size
8b127cbc
AM
10513 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10514 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 10515 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
10516 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10517 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10518 bucketpos);
10519 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10520 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
10521 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10522 }
c152c796 10523
8b127cbc 10524 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
10525 {
10526 Elf_Internal_Versym iversym;
10527 Elf_External_Versym *eversym;
10528
5fa370e4 10529 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
c152c796 10530 {
7b20f099
AM
10531 if (h->verinfo.verdef == NULL
10532 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10533 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
10534 iversym.vs_vers = 0;
10535 else
10536 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10537 }
10538 else
10539 {
10540 if (h->verinfo.vertree == NULL)
10541 iversym.vs_vers = 1;
10542 else
10543 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 10544 if (flinfo->info->create_default_symver)
3e3b46e5 10545 iversym.vs_vers++;
c152c796
AM
10546 }
10547
422f1182 10548 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 10549 defined locally. */
422f1182 10550 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
10551 iversym.vs_vers |= VERSYM_HIDDEN;
10552
8b127cbc 10553 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 10554 eversym += h->dynindx;
8b127cbc 10555 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
10556 }
10557 }
10558
d983c8c5
AM
10559 /* If the symbol is undefined, and we didn't output it to .dynsym,
10560 strip it from .symtab too. Obviously we can't do this for
10561 relocatable output or when needed for --emit-relocs. */
10562 else if (input_sec == bfd_und_section_ptr
10563 && h->indx != -2
66cae560
NC
10564 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10565 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
0e1862bb 10566 && !bfd_link_relocatable (flinfo->info))
d983c8c5 10567 return TRUE;
66cae560 10568
d983c8c5
AM
10569 /* Also strip others that we couldn't earlier due to dynamic symbol
10570 processing. */
10571 if (strip)
10572 return TRUE;
10573 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
10574 return TRUE;
10575
2ec55de3
AM
10576 /* Output a FILE symbol so that following locals are not associated
10577 with the wrong input file. We need one for forced local symbols
10578 if we've seen more than one FILE symbol or when we have exactly
10579 one FILE symbol but global symbols are present in a file other
10580 than the one with the FILE symbol. We also need one if linker
10581 defined symbols are present. In practice these conditions are
10582 always met, so just emit the FILE symbol unconditionally. */
10583 if (eoinfo->localsyms
10584 && !eoinfo->file_sym_done
10585 && eoinfo->flinfo->filesym_count != 0)
10586 {
10587 Elf_Internal_Sym fsym;
10588
10589 memset (&fsym, 0, sizeof (fsym));
10590 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10591 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
10592 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10593 bfd_und_section_ptr, NULL))
2ec55de3
AM
10594 return FALSE;
10595
10596 eoinfo->file_sym_done = TRUE;
10597 }
10598
8b127cbc 10599 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
10600 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10601 input_sec, h);
6e0b88f1 10602 if (ret == 0)
c152c796
AM
10603 {
10604 eoinfo->failed = TRUE;
10605 return FALSE;
10606 }
6e0b88f1
AM
10607 else if (ret == 1)
10608 h->indx = indx;
10609 else if (h->indx == -2)
10610 abort();
c152c796
AM
10611
10612 return TRUE;
10613}
10614
cdd3575c
AM
10615/* Return TRUE if special handling is done for relocs in SEC against
10616 symbols defined in discarded sections. */
10617
c152c796
AM
10618static bfd_boolean
10619elf_section_ignore_discarded_relocs (asection *sec)
10620{
10621 const struct elf_backend_data *bed;
10622
cdd3575c
AM
10623 switch (sec->sec_info_type)
10624 {
dbaa2011
AM
10625 case SEC_INFO_TYPE_STABS:
10626 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 10627 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
10628 return TRUE;
10629 default:
10630 break;
10631 }
c152c796
AM
10632
10633 bed = get_elf_backend_data (sec->owner);
10634 if (bed->elf_backend_ignore_discarded_relocs != NULL
10635 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10636 return TRUE;
10637
10638 return FALSE;
10639}
10640
9e66c942
AM
10641/* Return a mask saying how ld should treat relocations in SEC against
10642 symbols defined in discarded sections. If this function returns
10643 COMPLAIN set, ld will issue a warning message. If this function
10644 returns PRETEND set, and the discarded section was link-once and the
10645 same size as the kept link-once section, ld will pretend that the
10646 symbol was actually defined in the kept section. Otherwise ld will
10647 zero the reloc (at least that is the intent, but some cooperation by
10648 the target dependent code is needed, particularly for REL targets). */
10649
8a696751
AM
10650unsigned int
10651_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 10652{
9e66c942 10653 if (sec->flags & SEC_DEBUGGING)
69d54b1b 10654 return PRETEND;
cdd3575c
AM
10655
10656 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 10657 return 0;
cdd3575c
AM
10658
10659 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 10660 return 0;
cdd3575c 10661
9e66c942 10662 return COMPLAIN | PRETEND;
cdd3575c
AM
10663}
10664
3d7f7666
L
10665/* Find a match between a section and a member of a section group. */
10666
10667static asection *
c0f00686
L
10668match_group_member (asection *sec, asection *group,
10669 struct bfd_link_info *info)
3d7f7666
L
10670{
10671 asection *first = elf_next_in_group (group);
10672 asection *s = first;
10673
10674 while (s != NULL)
10675 {
c0f00686 10676 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
10677 return s;
10678
83180ade 10679 s = elf_next_in_group (s);
3d7f7666
L
10680 if (s == first)
10681 break;
10682 }
10683
10684 return NULL;
10685}
10686
01b3c8ab 10687/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
10688 to replace it. Return the replacement if it is OK. Otherwise return
10689 NULL. */
01b3c8ab
L
10690
10691asection *
c0f00686 10692_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
10693{
10694 asection *kept;
10695
10696 kept = sec->kept_section;
10697 if (kept != NULL)
10698 {
c2370991 10699 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 10700 kept = match_group_member (sec, kept, info);
58349d00
L
10701 if (kept != NULL)
10702 {
10703 if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10704 != (kept->rawsize != 0 ? kept->rawsize : kept->size))
10705 kept = NULL;
10706 else
10707 {
10708 /* Get the real kept section. */
10709 asection *next;
10710 for (next = kept->kept_section;
10711 next != NULL;
10712 next = next->kept_section)
10713 kept = next;
10714 }
10715 }
c2370991 10716 sec->kept_section = kept;
01b3c8ab
L
10717 }
10718 return kept;
10719}
10720
c152c796
AM
10721/* Link an input file into the linker output file. This function
10722 handles all the sections and relocations of the input file at once.
10723 This is so that we only have to read the local symbols once, and
10724 don't have to keep them in memory. */
10725
10726static bfd_boolean
8b127cbc 10727elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 10728{
ece5ef60 10729 int (*relocate_section)
c152c796
AM
10730 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10731 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10732 bfd *output_bfd;
10733 Elf_Internal_Shdr *symtab_hdr;
10734 size_t locsymcount;
10735 size_t extsymoff;
10736 Elf_Internal_Sym *isymbuf;
10737 Elf_Internal_Sym *isym;
10738 Elf_Internal_Sym *isymend;
10739 long *pindex;
10740 asection **ppsection;
10741 asection *o;
10742 const struct elf_backend_data *bed;
c152c796 10743 struct elf_link_hash_entry **sym_hashes;
310fd250
L
10744 bfd_size_type address_size;
10745 bfd_vma r_type_mask;
10746 int r_sym_shift;
ffbc01cc 10747 bfd_boolean have_file_sym = FALSE;
c152c796 10748
8b127cbc 10749 output_bfd = flinfo->output_bfd;
c152c796
AM
10750 bed = get_elf_backend_data (output_bfd);
10751 relocate_section = bed->elf_backend_relocate_section;
10752
10753 /* If this is a dynamic object, we don't want to do anything here:
10754 we don't want the local symbols, and we don't want the section
10755 contents. */
10756 if ((input_bfd->flags & DYNAMIC) != 0)
10757 return TRUE;
10758
c152c796
AM
10759 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10760 if (elf_bad_symtab (input_bfd))
10761 {
10762 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10763 extsymoff = 0;
10764 }
10765 else
10766 {
10767 locsymcount = symtab_hdr->sh_info;
10768 extsymoff = symtab_hdr->sh_info;
10769 }
10770
99fabbc9
JL
10771 /* Enable GNU OSABI features in the output BFD that are used in the input
10772 BFD. */
10773 if (bed->elf_osabi == ELFOSABI_NONE
10774 || bed->elf_osabi == ELFOSABI_GNU
10775 || bed->elf_osabi == ELFOSABI_FREEBSD)
10776 elf_tdata (output_bfd)->has_gnu_osabi
10777 |= elf_tdata (input_bfd)->has_gnu_osabi;
10778
c152c796
AM
10779 /* Read the local symbols. */
10780 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10781 if (isymbuf == NULL && locsymcount != 0)
10782 {
10783 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
10784 flinfo->internal_syms,
10785 flinfo->external_syms,
10786 flinfo->locsym_shndx);
c152c796
AM
10787 if (isymbuf == NULL)
10788 return FALSE;
10789 }
10790
10791 /* Find local symbol sections and adjust values of symbols in
10792 SEC_MERGE sections. Write out those local symbols we know are
10793 going into the output file. */
10794 isymend = isymbuf + locsymcount;
8b127cbc 10795 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
10796 isym < isymend;
10797 isym++, pindex++, ppsection++)
10798 {
10799 asection *isec;
10800 const char *name;
10801 Elf_Internal_Sym osym;
6e0b88f1
AM
10802 long indx;
10803 int ret;
c152c796
AM
10804
10805 *pindex = -1;
10806
10807 if (elf_bad_symtab (input_bfd))
10808 {
10809 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10810 {
10811 *ppsection = NULL;
10812 continue;
10813 }
10814 }
10815
10816 if (isym->st_shndx == SHN_UNDEF)
10817 isec = bfd_und_section_ptr;
c152c796
AM
10818 else if (isym->st_shndx == SHN_ABS)
10819 isec = bfd_abs_section_ptr;
10820 else if (isym->st_shndx == SHN_COMMON)
10821 isec = bfd_com_section_ptr;
10822 else
10823 {
cb33740c
AM
10824 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10825 if (isec == NULL)
10826 {
10827 /* Don't attempt to output symbols with st_shnx in the
10828 reserved range other than SHN_ABS and SHN_COMMON. */
6835821b 10829 isec = bfd_und_section_ptr;
cb33740c 10830 }
dbaa2011 10831 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
10832 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10833 isym->st_value =
10834 _bfd_merged_section_offset (output_bfd, &isec,
10835 elf_section_data (isec)->sec_info,
10836 isym->st_value);
c152c796
AM
10837 }
10838
10839 *ppsection = isec;
10840
d983c8c5
AM
10841 /* Don't output the first, undefined, symbol. In fact, don't
10842 output any undefined local symbol. */
10843 if (isec == bfd_und_section_ptr)
c152c796
AM
10844 continue;
10845
10846 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10847 {
10848 /* We never output section symbols. Instead, we use the
10849 section symbol of the corresponding section in the output
10850 file. */
10851 continue;
10852 }
10853
10854 /* If we are stripping all symbols, we don't want to output this
10855 one. */
8b127cbc 10856 if (flinfo->info->strip == strip_all)
c152c796
AM
10857 continue;
10858
10859 /* If we are discarding all local symbols, we don't want to
10860 output this one. If we are generating a relocatable output
10861 file, then some of the local symbols may be required by
10862 relocs; we output them below as we discover that they are
10863 needed. */
8b127cbc 10864 if (flinfo->info->discard == discard_all)
c152c796
AM
10865 continue;
10866
10867 /* If this symbol is defined in a section which we are
f02571c5 10868 discarding, we don't need to keep it. */
abf874aa
CL
10869 if (isym->st_shndx != SHN_UNDEF
10870 && isym->st_shndx < SHN_LORESERVE
10871 && isec->output_section == NULL
10872 && flinfo->info->non_contiguous_regions
10873 && flinfo->info->non_contiguous_regions_warnings)
10874 {
10875 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
10876 "discards section `%s' from '%s'\n"),
765cf5f6 10877 isec->name, bfd_get_filename (isec->owner));
abf874aa
CL
10878 continue;
10879 }
10880
f02571c5 10881 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
10882 && isym->st_shndx < SHN_LORESERVE
10883 && bfd_section_removed_from_list (output_bfd,
10884 isec->output_section))
e75a280b
L
10885 continue;
10886
c152c796
AM
10887 /* Get the name of the symbol. */
10888 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10889 isym->st_name);
10890 if (name == NULL)
10891 return FALSE;
10892
10893 /* See if we are discarding symbols with this name. */
8b127cbc
AM
10894 if ((flinfo->info->strip == strip_some
10895 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 10896 == NULL))
8b127cbc 10897 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
10898 && (isec->flags & SEC_MERGE)
10899 && !bfd_link_relocatable (flinfo->info))
8b127cbc 10900 || flinfo->info->discard == discard_l)
c152c796
AM
10901 && bfd_is_local_label_name (input_bfd, name)))
10902 continue;
10903
ffbc01cc
AM
10904 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10905 {
ce875075
AM
10906 if (input_bfd->lto_output)
10907 /* -flto puts a temp file name here. This means builds
10908 are not reproducible. Discard the symbol. */
10909 continue;
ffbc01cc
AM
10910 have_file_sym = TRUE;
10911 flinfo->filesym_count += 1;
10912 }
10913 if (!have_file_sym)
10914 {
10915 /* In the absence of debug info, bfd_find_nearest_line uses
10916 FILE symbols to determine the source file for local
10917 function symbols. Provide a FILE symbol here if input
10918 files lack such, so that their symbols won't be
10919 associated with a previous input file. It's not the
10920 source file, but the best we can do. */
10921 have_file_sym = TRUE;
10922 flinfo->filesym_count += 1;
10923 memset (&osym, 0, sizeof (osym));
10924 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10925 osym.st_shndx = SHN_ABS;
ef10c3ac
L
10926 if (!elf_link_output_symstrtab (flinfo,
10927 (input_bfd->lto_output ? NULL
765cf5f6 10928 : bfd_get_filename (input_bfd)),
ef10c3ac
L
10929 &osym, bfd_abs_section_ptr,
10930 NULL))
ffbc01cc
AM
10931 return FALSE;
10932 }
10933
c152c796
AM
10934 osym = *isym;
10935
10936 /* Adjust the section index for the output file. */
10937 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10938 isec->output_section);
10939 if (osym.st_shndx == SHN_BAD)
10940 return FALSE;
10941
c152c796
AM
10942 /* ELF symbols in relocatable files are section relative, but
10943 in executable files they are virtual addresses. Note that
10944 this code assumes that all ELF sections have an associated
10945 BFD section with a reasonable value for output_offset; below
10946 we assume that they also have a reasonable value for
10947 output_section. Any special sections must be set up to meet
10948 these requirements. */
10949 osym.st_value += isec->output_offset;
0e1862bb 10950 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10951 {
10952 osym.st_value += isec->output_section->vma;
10953 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10954 {
10955 /* STT_TLS symbols are relative to PT_TLS segment base. */
102def4d
AM
10956 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
10957 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10958 else
10959 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
10960 STT_NOTYPE);
c152c796
AM
10961 }
10962 }
10963
6e0b88f1 10964 indx = bfd_get_symcount (output_bfd);
ef10c3ac 10965 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 10966 if (ret == 0)
c152c796 10967 return FALSE;
6e0b88f1
AM
10968 else if (ret == 1)
10969 *pindex = indx;
c152c796
AM
10970 }
10971
310fd250
L
10972 if (bed->s->arch_size == 32)
10973 {
10974 r_type_mask = 0xff;
10975 r_sym_shift = 8;
10976 address_size = 4;
10977 }
10978 else
10979 {
10980 r_type_mask = 0xffffffff;
10981 r_sym_shift = 32;
10982 address_size = 8;
10983 }
10984
c152c796
AM
10985 /* Relocate the contents of each section. */
10986 sym_hashes = elf_sym_hashes (input_bfd);
10987 for (o = input_bfd->sections; o != NULL; o = o->next)
10988 {
10989 bfd_byte *contents;
10990
10991 if (! o->linker_mark)
10992 {
10993 /* This section was omitted from the link. */
10994 continue;
10995 }
10996
7bdf4127 10997 if (!flinfo->info->resolve_section_groups
bcacc0f5
AM
10998 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10999 {
11000 /* Deal with the group signature symbol. */
11001 struct bfd_elf_section_data *sec_data = elf_section_data (o);
11002 unsigned long symndx = sec_data->this_hdr.sh_info;
11003 asection *osec = o->output_section;
11004
7bdf4127 11005 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
bcacc0f5
AM
11006 if (symndx >= locsymcount
11007 || (elf_bad_symtab (input_bfd)
8b127cbc 11008 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
11009 {
11010 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
11011 while (h->root.type == bfd_link_hash_indirect
11012 || h->root.type == bfd_link_hash_warning)
11013 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11014 /* Arrange for symbol to be output. */
11015 h->indx = -2;
11016 elf_section_data (osec)->this_hdr.sh_info = -2;
11017 }
11018 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
11019 {
11020 /* We'll use the output section target_index. */
8b127cbc 11021 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
11022 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
11023 }
11024 else
11025 {
8b127cbc 11026 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
11027 {
11028 /* Otherwise output the local symbol now. */
11029 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 11030 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 11031 const char *name;
6e0b88f1
AM
11032 long indx;
11033 int ret;
bcacc0f5
AM
11034
11035 name = bfd_elf_string_from_elf_section (input_bfd,
11036 symtab_hdr->sh_link,
11037 sym.st_name);
11038 if (name == NULL)
11039 return FALSE;
11040
11041 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11042 sec);
11043 if (sym.st_shndx == SHN_BAD)
11044 return FALSE;
11045
11046 sym.st_value += o->output_offset;
11047
6e0b88f1 11048 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
11049 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11050 NULL);
6e0b88f1 11051 if (ret == 0)
bcacc0f5 11052 return FALSE;
6e0b88f1 11053 else if (ret == 1)
8b127cbc 11054 flinfo->indices[symndx] = indx;
6e0b88f1
AM
11055 else
11056 abort ();
bcacc0f5
AM
11057 }
11058 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 11059 = flinfo->indices[symndx];
bcacc0f5
AM
11060 }
11061 }
11062
c152c796 11063 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 11064 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
11065 continue;
11066
11067 if ((o->flags & SEC_LINKER_CREATED) != 0)
11068 {
11069 /* Section was created by _bfd_elf_link_create_dynamic_sections
11070 or somesuch. */
11071 continue;
11072 }
11073
11074 /* Get the contents of the section. They have been cached by a
11075 relaxation routine. Note that o is a section in an input
11076 file, so the contents field will not have been set by any of
11077 the routines which work on output files. */
11078 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
11079 {
11080 contents = elf_section_data (o)->this_hdr.contents;
11081 if (bed->caches_rawsize
11082 && o->rawsize != 0
11083 && o->rawsize < o->size)
11084 {
11085 memcpy (flinfo->contents, contents, o->rawsize);
11086 contents = flinfo->contents;
11087 }
11088 }
c152c796
AM
11089 else
11090 {
8b127cbc 11091 contents = flinfo->contents;
4a114e3e 11092 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
11093 return FALSE;
11094 }
11095
11096 if ((o->flags & SEC_RELOC) != 0)
11097 {
11098 Elf_Internal_Rela *internal_relocs;
0f02bbd9 11099 Elf_Internal_Rela *rel, *relend;
0f02bbd9 11100 int action_discarded;
ece5ef60 11101 int ret;
c152c796
AM
11102
11103 /* Get the swapped relocs. */
11104 internal_relocs
8b127cbc
AM
11105 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
11106 flinfo->internal_relocs, FALSE);
c152c796
AM
11107 if (internal_relocs == NULL
11108 && o->reloc_count > 0)
11109 return FALSE;
11110
310fd250
L
11111 /* We need to reverse-copy input .ctors/.dtors sections if
11112 they are placed in .init_array/.finit_array for output. */
11113 if (o->size > address_size
11114 && ((strncmp (o->name, ".ctors", 6) == 0
11115 && strcmp (o->output_section->name,
11116 ".init_array") == 0)
11117 || (strncmp (o->name, ".dtors", 6) == 0
11118 && strcmp (o->output_section->name,
11119 ".fini_array") == 0))
11120 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 11121 {
056bafd4
MR
11122 if (o->size * bed->s->int_rels_per_ext_rel
11123 != o->reloc_count * address_size)
310fd250 11124 {
4eca0228 11125 _bfd_error_handler
695344c0 11126 /* xgettext:c-format */
871b3ab2 11127 (_("error: %pB: size of section %pA is not "
310fd250
L
11128 "multiple of address size"),
11129 input_bfd, o);
8c6716e5 11130 bfd_set_error (bfd_error_bad_value);
310fd250
L
11131 return FALSE;
11132 }
11133 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
11134 }
11135
0f02bbd9 11136 action_discarded = -1;
c152c796 11137 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
11138 action_discarded = (*bed->action_discarded) (o);
11139
11140 /* Run through the relocs evaluating complex reloc symbols and
11141 looking for relocs against symbols from discarded sections
11142 or section symbols from removed link-once sections.
11143 Complain about relocs against discarded sections. Zero
11144 relocs against removed link-once sections. */
11145
11146 rel = internal_relocs;
056bafd4 11147 relend = rel + o->reloc_count;
0f02bbd9 11148 for ( ; rel < relend; rel++)
c152c796 11149 {
0f02bbd9
AM
11150 unsigned long r_symndx = rel->r_info >> r_sym_shift;
11151 unsigned int s_type;
11152 asection **ps, *sec;
11153 struct elf_link_hash_entry *h = NULL;
11154 const char *sym_name;
c152c796 11155
0f02bbd9
AM
11156 if (r_symndx == STN_UNDEF)
11157 continue;
c152c796 11158
0f02bbd9
AM
11159 if (r_symndx >= locsymcount
11160 || (elf_bad_symtab (input_bfd)
8b127cbc 11161 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
11162 {
11163 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 11164
0f02bbd9
AM
11165 /* Badly formatted input files can contain relocs that
11166 reference non-existant symbols. Check here so that
11167 we do not seg fault. */
11168 if (h == NULL)
c152c796 11169 {
4eca0228 11170 _bfd_error_handler
695344c0 11171 /* xgettext:c-format */
2dcf00ce 11172 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
0f02bbd9 11173 "that references a non-existent global symbol"),
2dcf00ce 11174 input_bfd, (uint64_t) rel->r_info, o);
0f02bbd9
AM
11175 bfd_set_error (bfd_error_bad_value);
11176 return FALSE;
11177 }
3b36f7e6 11178
0f02bbd9
AM
11179 while (h->root.type == bfd_link_hash_indirect
11180 || h->root.type == bfd_link_hash_warning)
11181 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 11182
0f02bbd9 11183 s_type = h->type;
cdd3575c 11184
9e2dec47 11185 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
11186 mark the symbol as undefined. Note that the
11187 linker may attach linker created dynamic sections
11188 to the plugin bfd. Symbols defined in linker
11189 created sections are not plugin symbols. */
bc4e12de 11190 if ((h->root.non_ir_ref_regular
4070765b 11191 || h->root.non_ir_ref_dynamic)
9e2dec47
L
11192 && (h->root.type == bfd_link_hash_defined
11193 || h->root.type == bfd_link_hash_defweak)
11194 && (h->root.u.def.section->flags
11195 & SEC_LINKER_CREATED) == 0
11196 && h->root.u.def.section->owner != NULL
11197 && (h->root.u.def.section->owner->flags
11198 & BFD_PLUGIN) != 0)
11199 {
11200 h->root.type = bfd_link_hash_undefined;
11201 h->root.u.undef.abfd = h->root.u.def.section->owner;
11202 }
11203
0f02bbd9
AM
11204 ps = NULL;
11205 if (h->root.type == bfd_link_hash_defined
11206 || h->root.type == bfd_link_hash_defweak)
11207 ps = &h->root.u.def.section;
11208
11209 sym_name = h->root.root.string;
11210 }
11211 else
11212 {
11213 Elf_Internal_Sym *sym = isymbuf + r_symndx;
11214
11215 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 11216 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
11217 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11218 sym, *ps);
11219 }
c152c796 11220
c301e700 11221 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 11222 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
11223 {
11224 bfd_vma val;
11225 bfd_vma dot = (rel->r_offset
11226 + o->output_offset + o->output_section->vma);
11227#ifdef DEBUG
11228 printf ("Encountered a complex symbol!");
11229 printf (" (input_bfd %s, section %s, reloc %ld\n",
765cf5f6 11230 bfd_get_filename (input_bfd), o->name,
9ccb8af9 11231 (long) (rel - internal_relocs));
0f02bbd9
AM
11232 printf (" symbol: idx %8.8lx, name %s\n",
11233 r_symndx, sym_name);
11234 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11235 (unsigned long) rel->r_info,
11236 (unsigned long) rel->r_offset);
11237#endif
8b127cbc 11238 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
11239 isymbuf, locsymcount, s_type == STT_SRELC))
11240 return FALSE;
11241
11242 /* Symbol evaluated OK. Update to absolute value. */
11243 set_symbol_value (input_bfd, isymbuf, locsymcount,
11244 r_symndx, val);
11245 continue;
11246 }
11247
11248 if (action_discarded != -1 && ps != NULL)
11249 {
cdd3575c
AM
11250 /* Complain if the definition comes from a
11251 discarded section. */
dbaa2011 11252 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 11253 {
cf35638d 11254 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 11255 if (action_discarded & COMPLAIN)
8b127cbc 11256 (*flinfo->info->callbacks->einfo)
695344c0 11257 /* xgettext:c-format */
871b3ab2
AM
11258 (_("%X`%s' referenced in section `%pA' of %pB: "
11259 "defined in discarded section `%pA' of %pB\n"),
e1fffbe6 11260 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 11261
87e5235d 11262 /* Try to do the best we can to support buggy old
e0ae6d6f 11263 versions of gcc. Pretend that the symbol is
87e5235d
AM
11264 really defined in the kept linkonce section.
11265 FIXME: This is quite broken. Modifying the
11266 symbol here means we will be changing all later
e0ae6d6f 11267 uses of the symbol, not just in this section. */
0f02bbd9 11268 if (action_discarded & PRETEND)
87e5235d 11269 {
01b3c8ab
L
11270 asection *kept;
11271
c0f00686 11272 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 11273 flinfo->info);
01b3c8ab 11274 if (kept != NULL)
87e5235d
AM
11275 {
11276 *ps = kept;
11277 continue;
11278 }
11279 }
c152c796
AM
11280 }
11281 }
11282 }
11283
11284 /* Relocate the section by invoking a back end routine.
11285
11286 The back end routine is responsible for adjusting the
11287 section contents as necessary, and (if using Rela relocs
11288 and generating a relocatable output file) adjusting the
11289 reloc addend as necessary.
11290
11291 The back end routine does not have to worry about setting
11292 the reloc address or the reloc symbol index.
11293
11294 The back end routine is given a pointer to the swapped in
11295 internal symbols, and can access the hash table entries
11296 for the external symbols via elf_sym_hashes (input_bfd).
11297
11298 When generating relocatable output, the back end routine
11299 must handle STB_LOCAL/STT_SECTION symbols specially. The
11300 output symbol is going to be a section symbol
11301 corresponding to the output section, which will require
11302 the addend to be adjusted. */
11303
8b127cbc 11304 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
11305 input_bfd, o, contents,
11306 internal_relocs,
11307 isymbuf,
8b127cbc 11308 flinfo->sections);
ece5ef60 11309 if (!ret)
c152c796
AM
11310 return FALSE;
11311
ece5ef60 11312 if (ret == 2
0e1862bb 11313 || bfd_link_relocatable (flinfo->info)
8b127cbc 11314 || flinfo->info->emitrelocations)
c152c796
AM
11315 {
11316 Elf_Internal_Rela *irela;
d4730f92 11317 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
11318 bfd_vma last_offset;
11319 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
11320 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11321 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 11322 unsigned int next_erel;
c152c796 11323 bfd_boolean rela_normal;
d4730f92 11324 struct bfd_elf_section_data *esdi, *esdo;
c152c796 11325
d4730f92
BS
11326 esdi = elf_section_data (o);
11327 esdo = elf_section_data (o->output_section);
11328 rela_normal = FALSE;
c152c796
AM
11329
11330 /* Adjust the reloc addresses and symbol indices. */
11331
11332 irela = internal_relocs;
056bafd4 11333 irelaend = irela + o->reloc_count;
d4730f92
BS
11334 rel_hash = esdo->rel.hashes + esdo->rel.count;
11335 /* We start processing the REL relocs, if any. When we reach
11336 IRELAMID in the loop, we switch to the RELA relocs. */
11337 irelamid = irela;
11338 if (esdi->rel.hdr != NULL)
11339 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11340 * bed->s->int_rels_per_ext_rel);
eac338cf 11341 rel_hash_list = rel_hash;
d4730f92 11342 rela_hash_list = NULL;
c152c796 11343 last_offset = o->output_offset;
0e1862bb 11344 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11345 last_offset += o->output_section->vma;
11346 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11347 {
11348 unsigned long r_symndx;
11349 asection *sec;
11350 Elf_Internal_Sym sym;
11351
11352 if (next_erel == bed->s->int_rels_per_ext_rel)
11353 {
11354 rel_hash++;
11355 next_erel = 0;
11356 }
11357
d4730f92
BS
11358 if (irela == irelamid)
11359 {
11360 rel_hash = esdo->rela.hashes + esdo->rela.count;
11361 rela_hash_list = rel_hash;
11362 rela_normal = bed->rela_normal;
11363 }
11364
c152c796 11365 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 11366 flinfo->info, o,
c152c796
AM
11367 irela->r_offset);
11368 if (irela->r_offset >= (bfd_vma) -2)
11369 {
11370 /* This is a reloc for a deleted entry or somesuch.
11371 Turn it into an R_*_NONE reloc, at the same
11372 offset as the last reloc. elf_eh_frame.c and
e460dd0d 11373 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
11374 being ordered. */
11375 irela->r_offset = last_offset;
11376 irela->r_info = 0;
11377 irela->r_addend = 0;
11378 continue;
11379 }
11380
11381 irela->r_offset += o->output_offset;
11382
11383 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 11384 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11385 irela->r_offset += o->output_section->vma;
11386
11387 last_offset = irela->r_offset;
11388
11389 r_symndx = irela->r_info >> r_sym_shift;
11390 if (r_symndx == STN_UNDEF)
11391 continue;
11392
11393 if (r_symndx >= locsymcount
11394 || (elf_bad_symtab (input_bfd)
8b127cbc 11395 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
11396 {
11397 struct elf_link_hash_entry *rh;
11398 unsigned long indx;
11399
11400 /* This is a reloc against a global symbol. We
11401 have not yet output all the local symbols, so
11402 we do not know the symbol index of any global
11403 symbol. We set the rel_hash entry for this
11404 reloc to point to the global hash table entry
11405 for this symbol. The symbol index is then
ee75fd95 11406 set at the end of bfd_elf_final_link. */
c152c796
AM
11407 indx = r_symndx - extsymoff;
11408 rh = elf_sym_hashes (input_bfd)[indx];
11409 while (rh->root.type == bfd_link_hash_indirect
11410 || rh->root.type == bfd_link_hash_warning)
11411 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11412
11413 /* Setting the index to -2 tells
11414 elf_link_output_extsym that this symbol is
11415 used by a reloc. */
11416 BFD_ASSERT (rh->indx < 0);
11417 rh->indx = -2;
c152c796
AM
11418 *rel_hash = rh;
11419
11420 continue;
11421 }
11422
11423 /* This is a reloc against a local symbol. */
11424
11425 *rel_hash = NULL;
11426 sym = isymbuf[r_symndx];
8b127cbc 11427 sec = flinfo->sections[r_symndx];
c152c796
AM
11428 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11429 {
11430 /* I suppose the backend ought to fill in the
11431 section of any STT_SECTION symbol against a
6a8d1586 11432 processor specific section. */
cf35638d 11433 r_symndx = STN_UNDEF;
6a8d1586
AM
11434 if (bfd_is_abs_section (sec))
11435 ;
c152c796
AM
11436 else if (sec == NULL || sec->owner == NULL)
11437 {
11438 bfd_set_error (bfd_error_bad_value);
11439 return FALSE;
11440 }
11441 else
11442 {
6a8d1586
AM
11443 asection *osec = sec->output_section;
11444
11445 /* If we have discarded a section, the output
11446 section will be the absolute section. In
ab96bf03
AM
11447 case of discarded SEC_MERGE sections, use
11448 the kept section. relocate_section should
11449 have already handled discarded linkonce
11450 sections. */
6a8d1586
AM
11451 if (bfd_is_abs_section (osec)
11452 && sec->kept_section != NULL
11453 && sec->kept_section->output_section != NULL)
11454 {
11455 osec = sec->kept_section->output_section;
11456 irela->r_addend -= osec->vma;
11457 }
11458
11459 if (!bfd_is_abs_section (osec))
11460 {
11461 r_symndx = osec->target_index;
cf35638d 11462 if (r_symndx == STN_UNDEF)
74541ad4 11463 {
051d833a
AM
11464 irela->r_addend += osec->vma;
11465 osec = _bfd_nearby_section (output_bfd, osec,
11466 osec->vma);
11467 irela->r_addend -= osec->vma;
11468 r_symndx = osec->target_index;
74541ad4 11469 }
6a8d1586 11470 }
c152c796
AM
11471 }
11472
11473 /* Adjust the addend according to where the
11474 section winds up in the output section. */
11475 if (rela_normal)
11476 irela->r_addend += sec->output_offset;
11477 }
11478 else
11479 {
8b127cbc 11480 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
11481 {
11482 unsigned long shlink;
11483 const char *name;
11484 asection *osec;
6e0b88f1 11485 long indx;
c152c796 11486
8b127cbc 11487 if (flinfo->info->strip == strip_all)
c152c796
AM
11488 {
11489 /* You can't do ld -r -s. */
11490 bfd_set_error (bfd_error_invalid_operation);
11491 return FALSE;
11492 }
11493
11494 /* This symbol was skipped earlier, but
11495 since it is needed by a reloc, we
11496 must output it now. */
11497 shlink = symtab_hdr->sh_link;
11498 name = (bfd_elf_string_from_elf_section
11499 (input_bfd, shlink, sym.st_name));
11500 if (name == NULL)
11501 return FALSE;
11502
11503 osec = sec->output_section;
11504 sym.st_shndx =
11505 _bfd_elf_section_from_bfd_section (output_bfd,
11506 osec);
11507 if (sym.st_shndx == SHN_BAD)
11508 return FALSE;
11509
11510 sym.st_value += sec->output_offset;
0e1862bb 11511 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11512 {
11513 sym.st_value += osec->vma;
11514 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11515 {
102def4d
AM
11516 struct elf_link_hash_table *htab
11517 = elf_hash_table (flinfo->info);
11518
c152c796
AM
11519 /* STT_TLS symbols are relative to PT_TLS
11520 segment base. */
102def4d
AM
11521 if (htab->tls_sec != NULL)
11522 sym.st_value -= htab->tls_sec->vma;
11523 else
11524 sym.st_info
11525 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11526 STT_NOTYPE);
c152c796
AM
11527 }
11528 }
11529
6e0b88f1 11530 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
11531 ret = elf_link_output_symstrtab (flinfo, name,
11532 &sym, sec,
11533 NULL);
6e0b88f1 11534 if (ret == 0)
c152c796 11535 return FALSE;
6e0b88f1 11536 else if (ret == 1)
8b127cbc 11537 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
11538 else
11539 abort ();
c152c796
AM
11540 }
11541
8b127cbc 11542 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
11543 }
11544
11545 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11546 | (irela->r_info & r_type_mask));
11547 }
11548
11549 /* Swap out the relocs. */
d4730f92
BS
11550 input_rel_hdr = esdi->rel.hdr;
11551 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 11552 {
d4730f92
BS
11553 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11554 input_rel_hdr,
11555 internal_relocs,
11556 rel_hash_list))
11557 return FALSE;
c152c796
AM
11558 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11559 * bed->s->int_rels_per_ext_rel);
eac338cf 11560 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
11561 }
11562
11563 input_rela_hdr = esdi->rela.hdr;
11564 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11565 {
eac338cf 11566 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 11567 input_rela_hdr,
eac338cf 11568 internal_relocs,
d4730f92 11569 rela_hash_list))
c152c796
AM
11570 return FALSE;
11571 }
11572 }
11573 }
11574
11575 /* Write out the modified section contents. */
11576 if (bed->elf_backend_write_section
8b127cbc 11577 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 11578 contents))
c152c796
AM
11579 {
11580 /* Section written out. */
11581 }
11582 else switch (o->sec_info_type)
11583 {
dbaa2011 11584 case SEC_INFO_TYPE_STABS:
c152c796
AM
11585 if (! (_bfd_write_section_stabs
11586 (output_bfd,
8b127cbc 11587 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
11588 o, &elf_section_data (o)->sec_info, contents)))
11589 return FALSE;
11590 break;
dbaa2011 11591 case SEC_INFO_TYPE_MERGE:
c152c796
AM
11592 if (! _bfd_write_merged_section (output_bfd, o,
11593 elf_section_data (o)->sec_info))
11594 return FALSE;
11595 break;
dbaa2011 11596 case SEC_INFO_TYPE_EH_FRAME:
c152c796 11597 {
8b127cbc 11598 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
11599 o, contents))
11600 return FALSE;
11601 }
11602 break;
2f0c68f2
CM
11603 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11604 {
11605 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11606 flinfo->info,
11607 o, contents))
11608 return FALSE;
11609 }
11610 break;
c152c796
AM
11611 default:
11612 {
310fd250
L
11613 if (! (o->flags & SEC_EXCLUDE))
11614 {
11615 file_ptr offset = (file_ptr) o->output_offset;
11616 bfd_size_type todo = o->size;
37b01f6a 11617
bb294208 11618 offset *= bfd_octets_per_byte (output_bfd, o);
37b01f6a 11619
310fd250
L
11620 if ((o->flags & SEC_ELF_REVERSE_COPY))
11621 {
11622 /* Reverse-copy input section to output. */
11623 do
11624 {
11625 todo -= address_size;
11626 if (! bfd_set_section_contents (output_bfd,
11627 o->output_section,
11628 contents + todo,
11629 offset,
11630 address_size))
11631 return FALSE;
11632 if (todo == 0)
11633 break;
11634 offset += address_size;
11635 }
11636 while (1);
11637 }
11638 else if (! bfd_set_section_contents (output_bfd,
11639 o->output_section,
11640 contents,
11641 offset, todo))
11642 return FALSE;
11643 }
c152c796
AM
11644 }
11645 break;
11646 }
11647 }
11648
11649 return TRUE;
11650}
11651
11652/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 11653 requested by the linker, and does not come from any input file. This
c152c796
AM
11654 is used to build constructor and destructor tables when linking
11655 with -Ur. */
11656
11657static bfd_boolean
11658elf_reloc_link_order (bfd *output_bfd,
11659 struct bfd_link_info *info,
11660 asection *output_section,
11661 struct bfd_link_order *link_order)
11662{
11663 reloc_howto_type *howto;
11664 long indx;
11665 bfd_vma offset;
11666 bfd_vma addend;
d4730f92 11667 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
11668 struct elf_link_hash_entry **rel_hash_ptr;
11669 Elf_Internal_Shdr *rel_hdr;
11670 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11671 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11672 bfd_byte *erel;
11673 unsigned int i;
d4730f92 11674 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
11675
11676 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11677 if (howto == NULL)
11678 {
11679 bfd_set_error (bfd_error_bad_value);
11680 return FALSE;
11681 }
11682
11683 addend = link_order->u.reloc.p->addend;
11684
d4730f92
BS
11685 if (esdo->rel.hdr)
11686 reldata = &esdo->rel;
11687 else if (esdo->rela.hdr)
11688 reldata = &esdo->rela;
11689 else
11690 {
11691 reldata = NULL;
11692 BFD_ASSERT (0);
11693 }
11694
c152c796 11695 /* Figure out the symbol index. */
d4730f92 11696 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
11697 if (link_order->type == bfd_section_reloc_link_order)
11698 {
11699 indx = link_order->u.reloc.p->u.section->target_index;
11700 BFD_ASSERT (indx != 0);
11701 *rel_hash_ptr = NULL;
11702 }
11703 else
11704 {
11705 struct elf_link_hash_entry *h;
11706
11707 /* Treat a reloc against a defined symbol as though it were
11708 actually against the section. */
11709 h = ((struct elf_link_hash_entry *)
11710 bfd_wrapped_link_hash_lookup (output_bfd, info,
11711 link_order->u.reloc.p->u.name,
11712 FALSE, FALSE, TRUE));
11713 if (h != NULL
11714 && (h->root.type == bfd_link_hash_defined
11715 || h->root.type == bfd_link_hash_defweak))
11716 {
11717 asection *section;
11718
11719 section = h->root.u.def.section;
11720 indx = section->output_section->target_index;
11721 *rel_hash_ptr = NULL;
11722 /* It seems that we ought to add the symbol value to the
11723 addend here, but in practice it has already been added
11724 because it was passed to constructor_callback. */
11725 addend += section->output_section->vma + section->output_offset;
11726 }
11727 else if (h != NULL)
11728 {
11729 /* Setting the index to -2 tells elf_link_output_extsym that
11730 this symbol is used by a reloc. */
11731 h->indx = -2;
11732 *rel_hash_ptr = h;
11733 indx = 0;
11734 }
11735 else
11736 {
1a72702b
AM
11737 (*info->callbacks->unattached_reloc)
11738 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
c152c796
AM
11739 indx = 0;
11740 }
11741 }
11742
11743 /* If this is an inplace reloc, we must write the addend into the
11744 object file. */
11745 if (howto->partial_inplace && addend != 0)
11746 {
11747 bfd_size_type size;
11748 bfd_reloc_status_type rstat;
11749 bfd_byte *buf;
11750 bfd_boolean ok;
11751 const char *sym_name;
bb294208 11752 bfd_size_type octets;
c152c796 11753
a50b1753
NC
11754 size = (bfd_size_type) bfd_get_reloc_size (howto);
11755 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 11756 if (buf == NULL && size != 0)
c152c796
AM
11757 return FALSE;
11758 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11759 switch (rstat)
11760 {
11761 case bfd_reloc_ok:
11762 break;
11763
11764 default:
11765 case bfd_reloc_outofrange:
11766 abort ();
11767
11768 case bfd_reloc_overflow:
11769 if (link_order->type == bfd_section_reloc_link_order)
fd361982 11770 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
c152c796
AM
11771 else
11772 sym_name = link_order->u.reloc.p->u.name;
1a72702b
AM
11773 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11774 howto->name, addend, NULL, NULL,
11775 (bfd_vma) 0);
c152c796
AM
11776 break;
11777 }
37b01f6a 11778
bb294208
AM
11779 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
11780 output_section);
c152c796 11781 ok = bfd_set_section_contents (output_bfd, output_section, buf,
bb294208 11782 octets, size);
c152c796
AM
11783 free (buf);
11784 if (! ok)
11785 return FALSE;
11786 }
11787
11788 /* The address of a reloc is relative to the section in a
11789 relocatable file, and is a virtual address in an executable
11790 file. */
11791 offset = link_order->offset;
0e1862bb 11792 if (! bfd_link_relocatable (info))
c152c796
AM
11793 offset += output_section->vma;
11794
11795 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11796 {
11797 irel[i].r_offset = offset;
11798 irel[i].r_info = 0;
11799 irel[i].r_addend = 0;
11800 }
11801 if (bed->s->arch_size == 32)
11802 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11803 else
11804 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11805
d4730f92 11806 rel_hdr = reldata->hdr;
c152c796
AM
11807 erel = rel_hdr->contents;
11808 if (rel_hdr->sh_type == SHT_REL)
11809 {
d4730f92 11810 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
11811 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11812 }
11813 else
11814 {
11815 irel[0].r_addend = addend;
d4730f92 11816 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
11817 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11818 }
11819
d4730f92 11820 ++reldata->count;
c152c796
AM
11821
11822 return TRUE;
11823}
11824
0b52efa6 11825
0b52efa6
PB
11826/* Compare two sections based on the locations of the sections they are
11827 linked to. Used by elf_fixup_link_order. */
11828
11829static int
8c1c5e5d 11830compare_link_order (const void *a, const void *b)
0b52efa6 11831{
8c1c5e5d
AM
11832 const struct bfd_link_order *alo = *(const struct bfd_link_order **) a;
11833 const struct bfd_link_order *blo = *(const struct bfd_link_order **) b;
11834 asection *asec = elf_linked_to_section (alo->u.indirect.section);
11835 asection *bsec = elf_linked_to_section (blo->u.indirect.section);
11836 bfd_vma apos = asec->output_section->lma + asec->output_offset;
11837 bfd_vma bpos = bsec->output_section->lma + bsec->output_offset;
0b52efa6 11838
0b52efa6
PB
11839 if (apos < bpos)
11840 return -1;
8c1c5e5d
AM
11841 if (apos > bpos)
11842 return 1;
11843
11844 /* The only way we should get matching LMAs is when the first of two
11845 sections has zero size. */
11846 if (asec->size < bsec->size)
11847 return -1;
11848 if (asec->size > bsec->size)
11849 return 1;
11850
11851 /* If they are both zero size then they almost certainly have the same
11852 VMA and thus are not ordered with respect to each other. Test VMA
11853 anyway, and fall back to id to make the result reproducible across
11854 qsort implementations. */
11855 apos = asec->output_section->vma + asec->output_offset;
11856 bpos = bsec->output_section->vma + bsec->output_offset;
11857 if (apos < bpos)
11858 return -1;
11859 if (apos > bpos)
11860 return 1;
11861
11862 return asec->id - bsec->id;
0b52efa6
PB
11863}
11864
11865
11866/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11867 order as their linked sections. Returns false if this could not be done
11868 because an output section includes both ordered and unordered
11869 sections. Ideally we'd do this in the linker proper. */
11870
11871static bfd_boolean
11872elf_fixup_link_order (bfd *abfd, asection *o)
11873{
8c1c5e5d
AM
11874 size_t seen_linkorder;
11875 size_t seen_other;
11876 size_t n;
0b52efa6
PB
11877 struct bfd_link_order *p;
11878 bfd *sub;
0b52efa6 11879 struct bfd_link_order **sections;
66631823
CE
11880 asection *other_sec, *linkorder_sec;
11881 bfd_vma offset; /* Octets. */
3b36f7e6 11882
d33cdfe3
L
11883 other_sec = NULL;
11884 linkorder_sec = NULL;
0b52efa6
PB
11885 seen_other = 0;
11886 seen_linkorder = 0;
8423293d 11887 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 11888 {
d33cdfe3 11889 if (p->type == bfd_indirect_link_order)
0b52efa6 11890 {
66631823 11891 asection *s = p->u.indirect.section;
d33cdfe3 11892 sub = s->owner;
847d5183
AM
11893 if ((s->flags & SEC_LINKER_CREATED) == 0
11894 && bfd_get_flavour (sub) == bfd_target_elf_flavour
8c1c5e5d
AM
11895 && elf_section_data (s) != NULL
11896 && elf_linked_to_section (s) != NULL)
d33cdfe3
L
11897 {
11898 seen_linkorder++;
11899 linkorder_sec = s;
11900 }
0b52efa6 11901 else
d33cdfe3
L
11902 {
11903 seen_other++;
11904 other_sec = s;
11905 }
0b52efa6
PB
11906 }
11907 else
11908 seen_other++;
d33cdfe3
L
11909
11910 if (seen_other && seen_linkorder)
11911 {
11912 if (other_sec && linkorder_sec)
4eca0228 11913 _bfd_error_handler
695344c0 11914 /* xgettext:c-format */
871b3ab2
AM
11915 (_("%pA has both ordered [`%pA' in %pB] "
11916 "and unordered [`%pA' in %pB] sections"),
63a5468a
AM
11917 o, linkorder_sec, linkorder_sec->owner,
11918 other_sec, other_sec->owner);
d33cdfe3 11919 else
4eca0228 11920 _bfd_error_handler
871b3ab2 11921 (_("%pA has both ordered and unordered sections"), o);
d33cdfe3
L
11922 bfd_set_error (bfd_error_bad_value);
11923 return FALSE;
11924 }
0b52efa6
PB
11925 }
11926
11927 if (!seen_linkorder)
11928 return TRUE;
11929
8c1c5e5d 11930 sections = bfd_malloc (seen_linkorder * sizeof (*sections));
14b1c01e
AM
11931 if (sections == NULL)
11932 return FALSE;
3b36f7e6 11933
8c1c5e5d 11934 seen_linkorder = 0;
8423293d 11935 for (p = o->map_head.link_order; p != NULL; p = p->next)
8c1c5e5d
AM
11936 sections[seen_linkorder++] = p;
11937
0b52efa6 11938 /* Sort the input sections in the order of their linked section. */
8c1c5e5d 11939 qsort (sections, seen_linkorder, sizeof (*sections), compare_link_order);
0b52efa6
PB
11940
11941 /* Change the offsets of the sections. */
11942 offset = 0;
11943 for (n = 0; n < seen_linkorder; n++)
11944 {
8c1c5e5d 11945 bfd_vma mask;
66631823
CE
11946 asection *s = sections[n]->u.indirect.section;
11947 unsigned int opb = bfd_octets_per_byte (abfd, s);
11948
11949 mask = ~(bfd_vma) 0 << s->alignment_power * opb;
8c1c5e5d 11950 offset = (offset + ~mask) & mask;
66631823 11951 sections[n]->offset = s->output_offset = offset / opb;
0b52efa6
PB
11952 offset += sections[n]->size;
11953 }
11954
4dd07732 11955 free (sections);
0b52efa6
PB
11956 return TRUE;
11957}
11958
76359541
TP
11959/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11960 Returns TRUE upon success, FALSE otherwise. */
11961
11962static bfd_boolean
11963elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11964{
11965 bfd_boolean ret = FALSE;
11966 bfd *implib_bfd;
11967 const struct elf_backend_data *bed;
11968 flagword flags;
11969 enum bfd_architecture arch;
11970 unsigned int mach;
11971 asymbol **sympp = NULL;
11972 long symsize;
11973 long symcount;
11974 long src_count;
11975 elf_symbol_type *osymbuf;
446f7ed5 11976 size_t amt;
76359541
TP
11977
11978 implib_bfd = info->out_implib_bfd;
11979 bed = get_elf_backend_data (abfd);
11980
11981 if (!bfd_set_format (implib_bfd, bfd_object))
11982 return FALSE;
11983
046734ff 11984 /* Use flag from executable but make it a relocatable object. */
76359541
TP
11985 flags = bfd_get_file_flags (abfd);
11986 flags &= ~HAS_RELOC;
11987 if (!bfd_set_start_address (implib_bfd, 0)
046734ff 11988 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
76359541
TP
11989 return FALSE;
11990
11991 /* Copy architecture of output file to import library file. */
11992 arch = bfd_get_arch (abfd);
11993 mach = bfd_get_mach (abfd);
11994 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11995 && (abfd->target_defaulted
11996 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11997 return FALSE;
11998
11999 /* Get symbol table size. */
12000 symsize = bfd_get_symtab_upper_bound (abfd);
12001 if (symsize < 0)
12002 return FALSE;
12003
12004 /* Read in the symbol table. */
ec9bd0a2
AM
12005 sympp = (asymbol **) bfd_malloc (symsize);
12006 if (sympp == NULL)
12007 return FALSE;
12008
76359541
TP
12009 symcount = bfd_canonicalize_symtab (abfd, sympp);
12010 if (symcount < 0)
12011 goto free_sym_buf;
12012
12013 /* Allow the BFD backend to copy any private header data it
12014 understands from the output BFD to the import library BFD. */
12015 if (! bfd_copy_private_header_data (abfd, implib_bfd))
12016 goto free_sym_buf;
12017
12018 /* Filter symbols to appear in the import library. */
12019 if (bed->elf_backend_filter_implib_symbols)
12020 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
12021 symcount);
12022 else
12023 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
12024 if (symcount == 0)
12025 {
5df1bc57 12026 bfd_set_error (bfd_error_no_symbols);
871b3ab2 12027 _bfd_error_handler (_("%pB: no symbol found for import library"),
4eca0228 12028 implib_bfd);
76359541
TP
12029 goto free_sym_buf;
12030 }
12031
12032
12033 /* Make symbols absolute. */
446f7ed5
AM
12034 amt = symcount * sizeof (*osymbuf);
12035 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
ec9bd0a2
AM
12036 if (osymbuf == NULL)
12037 goto free_sym_buf;
12038
76359541
TP
12039 for (src_count = 0; src_count < symcount; src_count++)
12040 {
12041 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
12042 sizeof (*osymbuf));
12043 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
12044 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
12045 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
12046 osymbuf[src_count].internal_elf_sym.st_value =
12047 osymbuf[src_count].symbol.value;
12048 sympp[src_count] = &osymbuf[src_count].symbol;
12049 }
12050
12051 bfd_set_symtab (implib_bfd, sympp, symcount);
12052
12053 /* Allow the BFD backend to copy any private data it understands
12054 from the output BFD to the import library BFD. This is done last
12055 to permit the routine to look at the filtered symbol table. */
12056 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
12057 goto free_sym_buf;
12058
12059 if (!bfd_close (implib_bfd))
12060 goto free_sym_buf;
12061
12062 ret = TRUE;
12063
dc1e8a47 12064 free_sym_buf:
76359541
TP
12065 free (sympp);
12066 return ret;
12067}
12068
9f7c3e5e
AM
12069static void
12070elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
12071{
12072 asection *o;
12073
12074 if (flinfo->symstrtab != NULL)
ef10c3ac 12075 _bfd_elf_strtab_free (flinfo->symstrtab);
c9594989
AM
12076 free (flinfo->contents);
12077 free (flinfo->external_relocs);
12078 free (flinfo->internal_relocs);
12079 free (flinfo->external_syms);
12080 free (flinfo->locsym_shndx);
12081 free (flinfo->internal_syms);
12082 free (flinfo->indices);
12083 free (flinfo->sections);
12084 if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
9f7c3e5e
AM
12085 free (flinfo->symshndxbuf);
12086 for (o = obfd->sections; o != NULL; o = o->next)
12087 {
12088 struct bfd_elf_section_data *esdo = elf_section_data (o);
c9594989
AM
12089 free (esdo->rel.hashes);
12090 free (esdo->rela.hashes);
9f7c3e5e
AM
12091 }
12092}
0b52efa6 12093
c152c796
AM
12094/* Do the final step of an ELF link. */
12095
12096bfd_boolean
12097bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
12098{
12099 bfd_boolean dynamic;
12100 bfd_boolean emit_relocs;
12101 bfd *dynobj;
8b127cbc 12102 struct elf_final_link_info flinfo;
91d6fa6a
NC
12103 asection *o;
12104 struct bfd_link_order *p;
12105 bfd *sub;
c152c796
AM
12106 bfd_size_type max_contents_size;
12107 bfd_size_type max_external_reloc_size;
12108 bfd_size_type max_internal_reloc_count;
12109 bfd_size_type max_sym_count;
12110 bfd_size_type max_sym_shndx_count;
c152c796
AM
12111 Elf_Internal_Sym elfsym;
12112 unsigned int i;
12113 Elf_Internal_Shdr *symtab_hdr;
12114 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
12115 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12116 struct elf_outext_info eoinfo;
12117 bfd_boolean merged;
12118 size_t relativecount = 0;
12119 asection *reldyn = 0;
12120 bfd_size_type amt;
104d59d1
JM
12121 asection *attr_section = NULL;
12122 bfd_vma attr_size = 0;
12123 const char *std_attrs_section;
64f52338 12124 struct elf_link_hash_table *htab = elf_hash_table (info);
4c6ee646 12125 bfd_boolean sections_removed;
496afd17 12126 bfd_boolean ret;
c152c796 12127
64f52338 12128 if (!is_elf_hash_table (htab))
c152c796
AM
12129 return FALSE;
12130
0e1862bb 12131 if (bfd_link_pic (info))
c152c796
AM
12132 abfd->flags |= DYNAMIC;
12133
64f52338
AM
12134 dynamic = htab->dynamic_sections_created;
12135 dynobj = htab->dynobj;
c152c796 12136
0e1862bb 12137 emit_relocs = (bfd_link_relocatable (info)
a4676736 12138 || info->emitrelocations);
c152c796 12139
496afd17 12140 memset (&flinfo, 0, sizeof (flinfo));
8b127cbc
AM
12141 flinfo.info = info;
12142 flinfo.output_bfd = abfd;
ef10c3ac 12143 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 12144 if (flinfo.symstrtab == NULL)
c152c796
AM
12145 return FALSE;
12146
12147 if (! dynamic)
12148 {
8b127cbc
AM
12149 flinfo.hash_sec = NULL;
12150 flinfo.symver_sec = NULL;
c152c796
AM
12151 }
12152 else
12153 {
3d4d4302 12154 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 12155 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 12156 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
12157 /* Note that it is OK if symver_sec is NULL. */
12158 }
12159
496afd17
L
12160 if (info->unique_symbol
12161 && !bfd_hash_table_init (&flinfo.local_hash_table,
12162 local_hash_newfunc,
12163 sizeof (struct local_hash_entry)))
12164 return FALSE;
c152c796 12165
104d59d1
JM
12166 /* The object attributes have been merged. Remove the input
12167 sections from the link, and set the contents of the output
1ff6de03 12168 section. */
4c6ee646 12169 sections_removed = FALSE;
104d59d1
JM
12170 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
12171 for (o = abfd->sections; o != NULL; o = o->next)
12172 {
5270eddc 12173 bfd_boolean remove_section = FALSE;
b8a6ced7 12174
104d59d1
JM
12175 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
12176 || strcmp (o->name, ".gnu.attributes") == 0)
12177 {
12178 for (p = o->map_head.link_order; p != NULL; p = p->next)
12179 {
12180 asection *input_section;
12181
12182 if (p->type != bfd_indirect_link_order)
12183 continue;
12184 input_section = p->u.indirect.section;
12185 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12186 elf_link_input_bfd ignores this section. */
12187 input_section->flags &= ~SEC_HAS_CONTENTS;
12188 }
a0c8462f 12189
104d59d1 12190 attr_size = bfd_elf_obj_attr_size (abfd);
fd361982 12191 bfd_set_section_size (o, attr_size);
b8a6ced7
AM
12192 /* Skip this section later on. */
12193 o->map_head.link_order = NULL;
104d59d1 12194 if (attr_size)
b8a6ced7 12195 attr_section = o;
104d59d1 12196 else
5270eddc 12197 remove_section = TRUE;
104d59d1 12198 }
6e5e9d58
AM
12199 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12200 {
12201 /* Remove empty group section from linker output. */
5270eddc 12202 remove_section = TRUE;
b8a6ced7 12203 }
5270eddc 12204 if (remove_section)
b8a6ced7 12205 {
6e5e9d58
AM
12206 o->flags |= SEC_EXCLUDE;
12207 bfd_section_list_remove (abfd, o);
12208 abfd->section_count--;
4c6ee646 12209 sections_removed = TRUE;
6e5e9d58 12210 }
104d59d1 12211 }
4c6ee646
AM
12212 if (sections_removed)
12213 _bfd_fix_excluded_sec_syms (abfd, info);
104d59d1 12214
c152c796
AM
12215 /* Count up the number of relocations we will output for each output
12216 section, so that we know the sizes of the reloc sections. We
12217 also figure out some maximum sizes. */
12218 max_contents_size = 0;
12219 max_external_reloc_size = 0;
12220 max_internal_reloc_count = 0;
12221 max_sym_count = 0;
12222 max_sym_shndx_count = 0;
12223 merged = FALSE;
12224 for (o = abfd->sections; o != NULL; o = o->next)
12225 {
12226 struct bfd_elf_section_data *esdo = elf_section_data (o);
12227 o->reloc_count = 0;
12228
8423293d 12229 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
12230 {
12231 unsigned int reloc_count = 0;
9eaff861 12232 unsigned int additional_reloc_count = 0;
c152c796 12233 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
12234
12235 if (p->type == bfd_section_reloc_link_order
12236 || p->type == bfd_symbol_reloc_link_order)
12237 reloc_count = 1;
12238 else if (p->type == bfd_indirect_link_order)
12239 {
12240 asection *sec;
12241
12242 sec = p->u.indirect.section;
c152c796
AM
12243
12244 /* Mark all sections which are to be included in the
12245 link. This will normally be every section. We need
12246 to do this so that we can identify any sections which
12247 the linker has decided to not include. */
12248 sec->linker_mark = TRUE;
12249
12250 if (sec->flags & SEC_MERGE)
12251 merged = TRUE;
12252
eea6121a
AM
12253 if (sec->rawsize > max_contents_size)
12254 max_contents_size = sec->rawsize;
12255 if (sec->size > max_contents_size)
12256 max_contents_size = sec->size;
c152c796 12257
c152c796
AM
12258 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12259 && (sec->owner->flags & DYNAMIC) == 0)
12260 {
12261 size_t sym_count;
12262
a961cdd5
AM
12263 /* We are interested in just local symbols, not all
12264 symbols. */
c152c796
AM
12265 if (elf_bad_symtab (sec->owner))
12266 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12267 / bed->s->sizeof_sym);
12268 else
12269 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12270
12271 if (sym_count > max_sym_count)
12272 max_sym_count = sym_count;
12273
12274 if (sym_count > max_sym_shndx_count
6a40cf0c 12275 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
12276 max_sym_shndx_count = sym_count;
12277
a961cdd5
AM
12278 if (esdo->this_hdr.sh_type == SHT_REL
12279 || esdo->this_hdr.sh_type == SHT_RELA)
12280 /* Some backends use reloc_count in relocation sections
12281 to count particular types of relocs. Of course,
12282 reloc sections themselves can't have relocations. */
12283 ;
12284 else if (emit_relocs)
12285 {
12286 reloc_count = sec->reloc_count;
12287 if (bed->elf_backend_count_additional_relocs)
12288 {
12289 int c;
12290 c = (*bed->elf_backend_count_additional_relocs) (sec);
12291 additional_reloc_count += c;
12292 }
12293 }
12294 else if (bed->elf_backend_count_relocs)
12295 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12296
12297 esdi = elf_section_data (sec);
12298
c152c796
AM
12299 if ((sec->flags & SEC_RELOC) != 0)
12300 {
d4730f92 12301 size_t ext_size = 0;
c152c796 12302
d4730f92
BS
12303 if (esdi->rel.hdr != NULL)
12304 ext_size = esdi->rel.hdr->sh_size;
12305 if (esdi->rela.hdr != NULL)
12306 ext_size += esdi->rela.hdr->sh_size;
7326c758 12307
c152c796
AM
12308 if (ext_size > max_external_reloc_size)
12309 max_external_reloc_size = ext_size;
12310 if (sec->reloc_count > max_internal_reloc_count)
12311 max_internal_reloc_count = sec->reloc_count;
12312 }
12313 }
12314 }
12315
12316 if (reloc_count == 0)
12317 continue;
12318
9eaff861 12319 reloc_count += additional_reloc_count;
c152c796
AM
12320 o->reloc_count += reloc_count;
12321
0e1862bb 12322 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 12323 {
d4730f92 12324 if (esdi->rel.hdr)
9eaff861 12325 {
491d01d3 12326 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
9eaff861
AO
12327 esdo->rel.count += additional_reloc_count;
12328 }
d4730f92 12329 if (esdi->rela.hdr)
9eaff861 12330 {
491d01d3 12331 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
9eaff861
AO
12332 esdo->rela.count += additional_reloc_count;
12333 }
d4730f92
BS
12334 }
12335 else
12336 {
12337 if (o->use_rela_p)
12338 esdo->rela.count += reloc_count;
2c2b4ed4 12339 else
d4730f92 12340 esdo->rel.count += reloc_count;
c152c796 12341 }
c152c796
AM
12342 }
12343
9eaff861 12344 if (o->reloc_count > 0)
c152c796
AM
12345 o->flags |= SEC_RELOC;
12346 else
12347 {
12348 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12349 set it (this is probably a bug) and if it is set
12350 assign_section_numbers will create a reloc section. */
12351 o->flags &=~ SEC_RELOC;
12352 }
12353
12354 /* If the SEC_ALLOC flag is not set, force the section VMA to
12355 zero. This is done in elf_fake_sections as well, but forcing
12356 the VMA to 0 here will ensure that relocs against these
12357 sections are handled correctly. */
12358 if ((o->flags & SEC_ALLOC) == 0
12359 && ! o->user_set_vma)
12360 o->vma = 0;
12361 }
12362
0e1862bb 12363 if (! bfd_link_relocatable (info) && merged)
64f52338 12364 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
c152c796
AM
12365
12366 /* Figure out the file positions for everything but the symbol table
12367 and the relocs. We set symcount to force assign_section_numbers
12368 to create a symbol table. */
ed48ec2e 12369 abfd->symcount = info->strip != strip_all || emit_relocs;
c152c796
AM
12370 BFD_ASSERT (! abfd->output_has_begun);
12371 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12372 goto error_return;
12373
ee75fd95 12374 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
12375 for (o = abfd->sections; o != NULL; o = o->next)
12376 {
d4730f92 12377 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
12378 if ((o->flags & SEC_RELOC) != 0)
12379 {
d4730f92 12380 if (esdo->rel.hdr
9eaff861 12381 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
12382 goto error_return;
12383
d4730f92 12384 if (esdo->rela.hdr
9eaff861 12385 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
12386 goto error_return;
12387 }
12388
48db3297
AM
12389 /* _bfd_elf_compute_section_file_positions makes temporary use
12390 of target_index. Reset it. */
12391 o->target_index = 0;
12392
c152c796
AM
12393 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12394 to count upwards while actually outputting the relocations. */
d4730f92
BS
12395 esdo->rel.count = 0;
12396 esdo->rela.count = 0;
0ce398f1 12397
1ff6de03
NA
12398 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12399 && !bfd_section_is_ctf (o))
0ce398f1
L
12400 {
12401 /* Cache the section contents so that they can be compressed
12402 later. Use bfd_malloc since it will be freed by
12403 bfd_compress_section_contents. */
12404 unsigned char *contents = esdo->this_hdr.contents;
12405 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
12406 abort ();
12407 contents
12408 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12409 if (contents == NULL)
12410 goto error_return;
12411 esdo->this_hdr.contents = contents;
12412 }
c152c796
AM
12413 }
12414
1ff6de03
NA
12415 /* We have now assigned file positions for all the sections except .symtab,
12416 .strtab, and non-loaded reloc and compressed debugging sections. We start
12417 the .symtab section at the current file position, and write directly to it.
12418 We build the .strtab section in memory. */
ed48ec2e 12419 abfd->symcount = 0;
c152c796
AM
12420 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12421 /* sh_name is set in prep_headers. */
12422 symtab_hdr->sh_type = SHT_SYMTAB;
12423 /* sh_flags, sh_addr and sh_size all start off zero. */
12424 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12425 /* sh_link is set in assign_section_numbers. */
12426 /* sh_info is set below. */
12427 /* sh_offset is set just below. */
72de5009 12428 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 12429
ef10c3ac
L
12430 if (max_sym_count < 20)
12431 max_sym_count = 20;
64f52338 12432 htab->strtabsize = max_sym_count;
ef10c3ac 12433 amt = max_sym_count * sizeof (struct elf_sym_strtab);
64f52338
AM
12434 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12435 if (htab->strtab == NULL)
c152c796 12436 goto error_return;
ef10c3ac
L
12437 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12438 flinfo.symshndxbuf
12439 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12440 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 12441
8539e4e8 12442 if (info->strip != strip_all || emit_relocs)
c152c796 12443 {
c77cb2a0
MR
12444 bfd_boolean name_local_sections;
12445 const char *name;
12446
8539e4e8
AM
12447 file_ptr off = elf_next_file_pos (abfd);
12448
12449 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
12450
12451 /* Note that at this point elf_next_file_pos (abfd) is
12452 incorrect. We do not yet know the size of the .symtab section.
12453 We correct next_file_pos below, after we do know the size. */
12454
12455 /* Start writing out the symbol table. The first symbol is always a
12456 dummy symbol. */
c152c796
AM
12457 elfsym.st_value = 0;
12458 elfsym.st_size = 0;
12459 elfsym.st_info = 0;
12460 elfsym.st_other = 0;
12461 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 12462 elfsym.st_target_internal = 0;
ef10c3ac
L
12463 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12464 bfd_und_section_ptr, NULL) != 1)
c152c796 12465 goto error_return;
c152c796 12466
8539e4e8
AM
12467 /* Output a symbol for each section. We output these even if we are
12468 discarding local symbols, since they are used for relocs. These
c77cb2a0
MR
12469 symbols usually have no names. We store the index of each one in
12470 the index field of the section, so that we can find it again when
8539e4e8
AM
12471 outputting relocs. */
12472
c77cb2a0
MR
12473 name_local_sections
12474 = (bed->elf_backend_name_local_section_symbols
12475 && bed->elf_backend_name_local_section_symbols (abfd));
12476
12477 name = NULL;
c152c796
AM
12478 elfsym.st_size = 0;
12479 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12480 elfsym.st_other = 0;
f0b5bb34 12481 elfsym.st_value = 0;
35fc36a8 12482 elfsym.st_target_internal = 0;
c152c796
AM
12483 for (i = 1; i < elf_numsections (abfd); i++)
12484 {
12485 o = bfd_section_from_elf_index (abfd, i);
12486 if (o != NULL)
f0b5bb34
AM
12487 {
12488 o->target_index = bfd_get_symcount (abfd);
12489 elfsym.st_shndx = i;
0e1862bb 12490 if (!bfd_link_relocatable (info))
f0b5bb34 12491 elfsym.st_value = o->vma;
c77cb2a0
MR
12492 if (name_local_sections)
12493 name = o->name;
12494 if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
ef10c3ac 12495 NULL) != 1)
f0b5bb34
AM
12496 goto error_return;
12497 }
c152c796
AM
12498 }
12499 }
12500
3f1b17bb
MR
12501 /* On some targets like Irix 5 the symbol split between local and global
12502 ones recorded in the sh_info field needs to be done between section
12503 and all other symbols. */
12504 if (bed->elf_backend_elfsym_local_is_section
12505 && bed->elf_backend_elfsym_local_is_section (abfd))
12506 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12507
c152c796
AM
12508 /* Allocate some memory to hold information read in from the input
12509 files. */
12510 if (max_contents_size != 0)
12511 {
8b127cbc
AM
12512 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12513 if (flinfo.contents == NULL)
c152c796
AM
12514 goto error_return;
12515 }
12516
12517 if (max_external_reloc_size != 0)
12518 {
8b127cbc
AM
12519 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12520 if (flinfo.external_relocs == NULL)
c152c796
AM
12521 goto error_return;
12522 }
12523
12524 if (max_internal_reloc_count != 0)
12525 {
056bafd4 12526 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
8b127cbc
AM
12527 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12528 if (flinfo.internal_relocs == NULL)
c152c796
AM
12529 goto error_return;
12530 }
12531
12532 if (max_sym_count != 0)
12533 {
12534 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
12535 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12536 if (flinfo.external_syms == NULL)
c152c796
AM
12537 goto error_return;
12538
12539 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
12540 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12541 if (flinfo.internal_syms == NULL)
c152c796
AM
12542 goto error_return;
12543
12544 amt = max_sym_count * sizeof (long);
8b127cbc
AM
12545 flinfo.indices = (long int *) bfd_malloc (amt);
12546 if (flinfo.indices == NULL)
c152c796
AM
12547 goto error_return;
12548
12549 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
12550 flinfo.sections = (asection **) bfd_malloc (amt);
12551 if (flinfo.sections == NULL)
c152c796
AM
12552 goto error_return;
12553 }
12554
12555 if (max_sym_shndx_count != 0)
12556 {
12557 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
12558 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12559 if (flinfo.locsym_shndx == NULL)
c152c796
AM
12560 goto error_return;
12561 }
12562
64f52338 12563 if (htab->tls_sec)
c152c796 12564 {
66631823 12565 bfd_vma base, end = 0; /* Both bytes. */
c152c796
AM
12566 asection *sec;
12567
64f52338 12568 for (sec = htab->tls_sec;
c152c796
AM
12569 sec && (sec->flags & SEC_THREAD_LOCAL);
12570 sec = sec->next)
12571 {
3a800eb9 12572 bfd_size_type size = sec->size;
66631823 12573 unsigned int opb = bfd_octets_per_byte (abfd, sec);
c152c796 12574
3a800eb9
AM
12575 if (size == 0
12576 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 12577 {
91d6fa6a
NC
12578 struct bfd_link_order *ord = sec->map_tail.link_order;
12579
12580 if (ord != NULL)
66631823 12581 size = ord->offset * opb + ord->size;
c152c796 12582 }
66631823 12583 end = sec->vma + size / opb;
c152c796 12584 }
64f52338 12585 base = htab->tls_sec->vma;
7dc98aea
RO
12586 /* Only align end of TLS section if static TLS doesn't have special
12587 alignment requirements. */
12588 if (bed->static_tls_alignment == 1)
64f52338
AM
12589 end = align_power (end, htab->tls_sec->alignment_power);
12590 htab->tls_size = end - base;
c152c796
AM
12591 }
12592
0b52efa6
PB
12593 /* Reorder SHF_LINK_ORDER sections. */
12594 for (o = abfd->sections; o != NULL; o = o->next)
12595 {
12596 if (!elf_fixup_link_order (abfd, o))
12597 return FALSE;
12598 }
12599
2f0c68f2
CM
12600 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12601 return FALSE;
12602
c152c796
AM
12603 /* Since ELF permits relocations to be against local symbols, we
12604 must have the local symbols available when we do the relocations.
12605 Since we would rather only read the local symbols once, and we
12606 would rather not keep them in memory, we handle all the
12607 relocations for a single input file at the same time.
12608
12609 Unfortunately, there is no way to know the total number of local
12610 symbols until we have seen all of them, and the local symbol
12611 indices precede the global symbol indices. This means that when
12612 we are generating relocatable output, and we see a reloc against
12613 a global symbol, we can not know the symbol index until we have
12614 finished examining all the local symbols to see which ones we are
12615 going to output. To deal with this, we keep the relocations in
12616 memory, and don't output them until the end of the link. This is
12617 an unfortunate waste of memory, but I don't see a good way around
12618 it. Fortunately, it only happens when performing a relocatable
12619 link, which is not the common case. FIXME: If keep_memory is set
12620 we could write the relocs out and then read them again; I don't
12621 know how bad the memory loss will be. */
12622
c72f2fb2 12623 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12624 sub->output_has_begun = FALSE;
12625 for (o = abfd->sections; o != NULL; o = o->next)
12626 {
8423293d 12627 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
12628 {
12629 if (p->type == bfd_indirect_link_order
12630 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12631 == bfd_target_elf_flavour)
12632 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12633 {
12634 if (! sub->output_has_begun)
12635 {
8b127cbc 12636 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
12637 goto error_return;
12638 sub->output_has_begun = TRUE;
12639 }
12640 }
12641 else if (p->type == bfd_section_reloc_link_order
12642 || p->type == bfd_symbol_reloc_link_order)
12643 {
12644 if (! elf_reloc_link_order (abfd, info, o, p))
12645 goto error_return;
12646 }
12647 else
12648 {
12649 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
12650 {
12651 if (p->type == bfd_indirect_link_order
12652 && (bfd_get_flavour (sub)
12653 == bfd_target_elf_flavour)
12654 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12655 != bed->s->elfclass))
12656 {
12657 const char *iclass, *oclass;
12658
aebf9be7 12659 switch (bed->s->elfclass)
351f65ca 12660 {
aebf9be7
NC
12661 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12662 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12663 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12664 default: abort ();
351f65ca 12665 }
aebf9be7
NC
12666
12667 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 12668 {
aebf9be7
NC
12669 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12670 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12671 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12672 default: abort ();
351f65ca
L
12673 }
12674
12675 bfd_set_error (bfd_error_wrong_format);
4eca0228 12676 _bfd_error_handler
695344c0 12677 /* xgettext:c-format */
871b3ab2 12678 (_("%pB: file class %s incompatible with %s"),
351f65ca
L
12679 sub, iclass, oclass);
12680 }
12681
12682 goto error_return;
12683 }
c152c796
AM
12684 }
12685 }
12686 }
12687
c0f00686
L
12688 /* Free symbol buffer if needed. */
12689 if (!info->reduce_memory_overheads)
12690 {
c72f2fb2 12691 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c9594989 12692 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
c0f00686
L
12693 {
12694 free (elf_tdata (sub)->symbuf);
12695 elf_tdata (sub)->symbuf = NULL;
12696 }
12697 }
12698
496afd17
L
12699 ret = TRUE;
12700
c152c796
AM
12701 /* Output any global symbols that got converted to local in a
12702 version script or due to symbol visibility. We do this in a
12703 separate step since ELF requires all local symbols to appear
12704 prior to any global symbols. FIXME: We should only do this if
12705 some global symbols were, in fact, converted to become local.
12706 FIXME: Will this work correctly with the Irix 5 linker? */
12707 eoinfo.failed = FALSE;
8b127cbc 12708 eoinfo.flinfo = &flinfo;
c152c796 12709 eoinfo.localsyms = TRUE;
34a79995 12710 eoinfo.file_sym_done = FALSE;
7686d77d 12711 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796 12712 if (eoinfo.failed)
496afd17
L
12713 {
12714 ret = FALSE;
12715 goto return_local_hash_table;
12716 }
c152c796 12717
4e617b1e
PB
12718 /* If backend needs to output some local symbols not present in the hash
12719 table, do it now. */
8539e4e8
AM
12720 if (bed->elf_backend_output_arch_local_syms
12721 && (info->strip != strip_all || emit_relocs))
4e617b1e 12722 {
6e0b88f1 12723 typedef int (*out_sym_func)
4e617b1e
PB
12724 (void *, const char *, Elf_Internal_Sym *, asection *,
12725 struct elf_link_hash_entry *);
12726
12727 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
12728 (abfd, info, &flinfo,
12729 (out_sym_func) elf_link_output_symstrtab)))
496afd17
L
12730 {
12731 ret = FALSE;
12732 goto return_local_hash_table;
12733 }
4e617b1e
PB
12734 }
12735
c152c796
AM
12736 /* That wrote out all the local symbols. Finish up the symbol table
12737 with the global symbols. Even if we want to strip everything we
12738 can, we still need to deal with those global symbols that got
12739 converted to local in a version script. */
12740
12741 /* The sh_info field records the index of the first non local symbol. */
3f1b17bb
MR
12742 if (!symtab_hdr->sh_info)
12743 symtab_hdr->sh_info = bfd_get_symcount (abfd);
c152c796
AM
12744
12745 if (dynamic
64f52338
AM
12746 && htab->dynsym != NULL
12747 && htab->dynsym->output_section != bfd_abs_section_ptr)
c152c796
AM
12748 {
12749 Elf_Internal_Sym sym;
64f52338 12750 bfd_byte *dynsym = htab->dynsym->contents;
90ac2420 12751
64f52338
AM
12752 o = htab->dynsym->output_section;
12753 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
c152c796
AM
12754
12755 /* Write out the section symbols for the output sections. */
0e1862bb 12756 if (bfd_link_pic (info)
64f52338 12757 || htab->is_relocatable_executable)
c152c796
AM
12758 {
12759 asection *s;
12760
12761 sym.st_size = 0;
12762 sym.st_name = 0;
12763 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12764 sym.st_other = 0;
35fc36a8 12765 sym.st_target_internal = 0;
c152c796
AM
12766
12767 for (s = abfd->sections; s != NULL; s = s->next)
12768 {
12769 int indx;
12770 bfd_byte *dest;
12771 long dynindx;
12772
c152c796 12773 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
12774 if (dynindx <= 0)
12775 continue;
12776 indx = elf_section_data (s)->this_idx;
c152c796
AM
12777 BFD_ASSERT (indx > 0);
12778 sym.st_shndx = indx;
c0d5a53d 12779 if (! check_dynsym (abfd, &sym))
496afd17
L
12780 {
12781 ret = FALSE;
12782 goto return_local_hash_table;
12783 }
c152c796
AM
12784 sym.st_value = s->vma;
12785 dest = dynsym + dynindx * bed->s->sizeof_sym;
3d16b64e
NA
12786
12787 /* Inform the linker of the addition of this symbol. */
12788
12789 if (info->callbacks->ctf_new_dynsym)
12790 info->callbacks->ctf_new_dynsym (dynindx, &sym);
12791
c152c796
AM
12792 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12793 }
c152c796
AM
12794 }
12795
12796 /* Write out the local dynsyms. */
64f52338 12797 if (htab->dynlocal)
c152c796
AM
12798 {
12799 struct elf_link_local_dynamic_entry *e;
64f52338 12800 for (e = htab->dynlocal; e ; e = e->next)
c152c796
AM
12801 {
12802 asection *s;
12803 bfd_byte *dest;
12804
935bd1e0 12805 /* Copy the internal symbol and turn off visibility.
c152c796
AM
12806 Note that we saved a word of storage and overwrote
12807 the original st_name with the dynstr_index. */
12808 sym = e->isym;
935bd1e0 12809 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
4d68fd75 12810 sym.st_shndx = SHN_UNDEF;
c152c796 12811
cb33740c
AM
12812 s = bfd_section_from_elf_index (e->input_bfd,
12813 e->isym.st_shndx);
4d68fd75
AM
12814 if (s != NULL
12815 && s->output_section != NULL
12816 && elf_section_data (s->output_section) != NULL)
c152c796 12817 {
c152c796
AM
12818 sym.st_shndx =
12819 elf_section_data (s->output_section)->this_idx;
c0d5a53d 12820 if (! check_dynsym (abfd, &sym))
496afd17
L
12821 {
12822 ret = FALSE;
12823 goto return_local_hash_table;
12824 }
c152c796
AM
12825 sym.st_value = (s->output_section->vma
12826 + s->output_offset
12827 + e->isym.st_value);
12828 }
12829
3d16b64e
NA
12830 /* Inform the linker of the addition of this symbol. */
12831
12832 if (info->callbacks->ctf_new_dynsym)
12833 info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
12834
c152c796
AM
12835 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12836 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12837 }
12838 }
c152c796
AM
12839 }
12840
12841 /* We get the global symbols from the hash table. */
12842 eoinfo.failed = FALSE;
12843 eoinfo.localsyms = FALSE;
8b127cbc 12844 eoinfo.flinfo = &flinfo;
7686d77d 12845 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796 12846 if (eoinfo.failed)
496afd17
L
12847 {
12848 ret = FALSE;
12849 goto return_local_hash_table;
12850 }
c152c796
AM
12851
12852 /* If backend needs to output some symbols not present in the hash
12853 table, do it now. */
8539e4e8
AM
12854 if (bed->elf_backend_output_arch_syms
12855 && (info->strip != strip_all || emit_relocs))
c152c796 12856 {
6e0b88f1 12857 typedef int (*out_sym_func)
c152c796
AM
12858 (void *, const char *, Elf_Internal_Sym *, asection *,
12859 struct elf_link_hash_entry *);
12860
12861 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
12862 (abfd, info, &flinfo,
12863 (out_sym_func) elf_link_output_symstrtab)))
496afd17
L
12864 {
12865 ret = FALSE;
12866 goto return_local_hash_table;
12867 }
c152c796
AM
12868 }
12869
ef10c3ac
L
12870 /* Finalize the .strtab section. */
12871 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12872
12873 /* Swap out the .strtab section. */
12874 if (!elf_link_swap_symbols_out (&flinfo))
496afd17
L
12875 {
12876 ret = FALSE;
12877 goto return_local_hash_table;
12878 }
c152c796
AM
12879
12880 /* Now we know the size of the symtab section. */
c152c796
AM
12881 if (bfd_get_symcount (abfd) > 0)
12882 {
ee3b52e9
L
12883 /* Finish up and write out the symbol string table (.strtab)
12884 section. */
ad32986f 12885 Elf_Internal_Shdr *symstrtab_hdr = NULL;
8539e4e8
AM
12886 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12887
ad32986f 12888 if (elf_symtab_shndx_list (abfd))
8539e4e8 12889 {
ad32986f 12890 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8539e4e8 12891
ad32986f
NC
12892 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12893 {
12894 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12895 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12896 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12897 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12898 symtab_shndx_hdr->sh_size = amt;
8539e4e8 12899
ad32986f
NC
12900 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12901 off, TRUE);
12902
12903 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12904 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
496afd17
L
12905 {
12906 ret = FALSE;
12907 goto return_local_hash_table;
12908 }
ad32986f 12909 }
8539e4e8 12910 }
ee3b52e9
L
12911
12912 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12913 /* sh_name was set in prep_headers. */
12914 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 12915 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 12916 symstrtab_hdr->sh_addr = 0;
ef10c3ac 12917 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
12918 symstrtab_hdr->sh_entsize = 0;
12919 symstrtab_hdr->sh_link = 0;
12920 symstrtab_hdr->sh_info = 0;
12921 /* sh_offset is set just below. */
12922 symstrtab_hdr->sh_addralign = 1;
12923
12924 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12925 off, TRUE);
12926 elf_next_file_pos (abfd) = off;
12927
c152c796 12928 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 12929 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
496afd17
L
12930 {
12931 ret = FALSE;
12932 goto return_local_hash_table;
12933 }
c152c796
AM
12934 }
12935
76359541
TP
12936 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12937 {
871b3ab2 12938 _bfd_error_handler (_("%pB: failed to generate import library"),
4eca0228 12939 info->out_implib_bfd);
496afd17
L
12940 ret = FALSE;
12941 goto return_local_hash_table;
76359541
TP
12942 }
12943
c152c796
AM
12944 /* Adjust the relocs to have the correct symbol indices. */
12945 for (o = abfd->sections; o != NULL; o = o->next)
12946 {
d4730f92 12947 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 12948 bfd_boolean sort;
10bbbc1d 12949
c152c796
AM
12950 if ((o->flags & SEC_RELOC) == 0)
12951 continue;
12952
28dbcedc 12953 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3 12954 if (esdo->rel.hdr != NULL
10bbbc1d 12955 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
496afd17
L
12956 {
12957 ret = FALSE;
12958 goto return_local_hash_table;
12959 }
bca6d0e3 12960 if (esdo->rela.hdr != NULL
10bbbc1d 12961 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
496afd17
L
12962 {
12963 ret = FALSE;
12964 goto return_local_hash_table;
12965 }
c152c796
AM
12966
12967 /* Set the reloc_count field to 0 to prevent write_relocs from
12968 trying to swap the relocs out itself. */
12969 o->reloc_count = 0;
12970 }
12971
12972 if (dynamic && info->combreloc && dynobj != NULL)
12973 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12974
12975 /* If we are linking against a dynamic object, or generating a
12976 shared library, finish up the dynamic linking information. */
12977 if (dynamic)
12978 {
12979 bfd_byte *dyncon, *dynconend;
12980
12981 /* Fix up .dynamic entries. */
3d4d4302 12982 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
12983 BFD_ASSERT (o != NULL);
12984
12985 dyncon = o->contents;
eea6121a 12986 dynconend = o->contents + o->size;
c152c796
AM
12987 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12988 {
12989 Elf_Internal_Dyn dyn;
12990 const char *name;
12991 unsigned int type;
64487780
AM
12992 bfd_size_type sh_size;
12993 bfd_vma sh_addr;
c152c796
AM
12994
12995 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12996
12997 switch (dyn.d_tag)
12998 {
12999 default:
13000 continue;
13001 case DT_NULL:
13002 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
13003 {
13004 switch (elf_section_data (reldyn)->this_hdr.sh_type)
13005 {
13006 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
13007 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
13008 default: continue;
13009 }
13010 dyn.d_un.d_val = relativecount;
13011 relativecount = 0;
13012 break;
13013 }
13014 continue;
13015
13016 case DT_INIT:
13017 name = info->init_function;
13018 goto get_sym;
13019 case DT_FINI:
13020 name = info->fini_function;
13021 get_sym:
13022 {
13023 struct elf_link_hash_entry *h;
13024
64f52338 13025 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
c152c796
AM
13026 if (h != NULL
13027 && (h->root.type == bfd_link_hash_defined
13028 || h->root.type == bfd_link_hash_defweak))
13029 {
bef26483 13030 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
13031 o = h->root.u.def.section;
13032 if (o->output_section != NULL)
bef26483 13033 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
13034 + o->output_offset);
13035 else
13036 {
13037 /* The symbol is imported from another shared
13038 library and does not apply to this one. */
bef26483 13039 dyn.d_un.d_ptr = 0;
c152c796
AM
13040 }
13041 break;
13042 }
13043 }
13044 continue;
13045
13046 case DT_PREINIT_ARRAYSZ:
13047 name = ".preinit_array";
4ade44b7 13048 goto get_out_size;
c152c796
AM
13049 case DT_INIT_ARRAYSZ:
13050 name = ".init_array";
4ade44b7 13051 goto get_out_size;
c152c796
AM
13052 case DT_FINI_ARRAYSZ:
13053 name = ".fini_array";
4ade44b7 13054 get_out_size:
c152c796
AM
13055 o = bfd_get_section_by_name (abfd, name);
13056 if (o == NULL)
13057 {
4eca0228 13058 _bfd_error_handler
4ade44b7 13059 (_("could not find section %s"), name);
c152c796
AM
13060 goto error_return;
13061 }
eea6121a 13062 if (o->size == 0)
4eca0228 13063 _bfd_error_handler
c152c796 13064 (_("warning: %s section has zero size"), name);
eea6121a 13065 dyn.d_un.d_val = o->size;
c152c796
AM
13066 break;
13067
13068 case DT_PREINIT_ARRAY:
13069 name = ".preinit_array";
4ade44b7 13070 goto get_out_vma;
c152c796
AM
13071 case DT_INIT_ARRAY:
13072 name = ".init_array";
4ade44b7 13073 goto get_out_vma;
c152c796
AM
13074 case DT_FINI_ARRAY:
13075 name = ".fini_array";
4ade44b7
AM
13076 get_out_vma:
13077 o = bfd_get_section_by_name (abfd, name);
13078 goto do_vma;
c152c796
AM
13079
13080 case DT_HASH:
13081 name = ".hash";
13082 goto get_vma;
fdc90cb4
JJ
13083 case DT_GNU_HASH:
13084 name = ".gnu.hash";
13085 goto get_vma;
c152c796
AM
13086 case DT_STRTAB:
13087 name = ".dynstr";
13088 goto get_vma;
13089 case DT_SYMTAB:
13090 name = ".dynsym";
13091 goto get_vma;
13092 case DT_VERDEF:
13093 name = ".gnu.version_d";
13094 goto get_vma;
13095 case DT_VERNEED:
13096 name = ".gnu.version_r";
13097 goto get_vma;
13098 case DT_VERSYM:
13099 name = ".gnu.version";
13100 get_vma:
4ade44b7
AM
13101 o = bfd_get_linker_section (dynobj, name);
13102 do_vma:
b3293efa 13103 if (o == NULL || bfd_is_abs_section (o->output_section))
c152c796 13104 {
4eca0228 13105 _bfd_error_handler
4ade44b7 13106 (_("could not find section %s"), name);
c152c796
AM
13107 goto error_return;
13108 }
894891db
NC
13109 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13110 {
4eca0228 13111 _bfd_error_handler
894891db
NC
13112 (_("warning: section '%s' is being made into a note"), name);
13113 bfd_set_error (bfd_error_nonrepresentable_section);
13114 goto error_return;
13115 }
4ade44b7 13116 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
c152c796
AM
13117 break;
13118
13119 case DT_REL:
13120 case DT_RELA:
13121 case DT_RELSZ:
13122 case DT_RELASZ:
13123 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13124 type = SHT_REL;
13125 else
13126 type = SHT_RELA;
64487780
AM
13127 sh_size = 0;
13128 sh_addr = 0;
c152c796
AM
13129 for (i = 1; i < elf_numsections (abfd); i++)
13130 {
13131 Elf_Internal_Shdr *hdr;
13132
13133 hdr = elf_elfsections (abfd)[i];
13134 if (hdr->sh_type == type
13135 && (hdr->sh_flags & SHF_ALLOC) != 0)
13136 {
64487780
AM
13137 sh_size += hdr->sh_size;
13138 if (sh_addr == 0
13139 || sh_addr > hdr->sh_addr)
13140 sh_addr = hdr->sh_addr;
c152c796
AM
13141 }
13142 }
64487780 13143
64f52338
AM
13144 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
13145 {
66631823
CE
13146 unsigned int opb = bfd_octets_per_byte (abfd, o);
13147
64f52338
AM
13148 /* Don't count procedure linkage table relocs in the
13149 overall reloc count. */
64487780
AM
13150 sh_size -= htab->srelplt->size;
13151 if (sh_size == 0)
13152 /* If the size is zero, make the address zero too.
13153 This is to avoid a glibc bug. If the backend
13154 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13155 zero, then we'll put DT_RELA at the end of
13156 DT_JMPREL. glibc will interpret the end of
13157 DT_RELA matching the end of DT_JMPREL as the
13158 case where DT_RELA includes DT_JMPREL, and for
13159 LD_BIND_NOW will decide that processing DT_RELA
13160 will process the PLT relocs too. Net result:
13161 No PLT relocs applied. */
13162 sh_addr = 0;
13163
64f52338
AM
13164 /* If .rela.plt is the first .rela section, exclude
13165 it from DT_RELA. */
64487780 13166 else if (sh_addr == (htab->srelplt->output_section->vma
66631823 13167 + htab->srelplt->output_offset) * opb)
64487780 13168 sh_addr += htab->srelplt->size;
64f52338 13169 }
64487780
AM
13170
13171 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13172 dyn.d_un.d_val = sh_size;
13173 else
13174 dyn.d_un.d_ptr = sh_addr;
c152c796
AM
13175 break;
13176 }
13177 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13178 }
13179 }
13180
13181 /* If we have created any dynamic sections, then output them. */
13182 if (dynobj != NULL)
13183 {
13184 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
13185 goto error_return;
13186
943284cc 13187 /* Check for DT_TEXTREL (late, in case the backend removes it). */
a6dbf402 13188 if (bfd_link_textrel_check (info)
3d4d4302 13189 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
13190 {
13191 bfd_byte *dyncon, *dynconend;
13192
943284cc
DJ
13193 dyncon = o->contents;
13194 dynconend = o->contents + o->size;
13195 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13196 {
13197 Elf_Internal_Dyn dyn;
13198
13199 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13200
13201 if (dyn.d_tag == DT_TEXTREL)
13202 {
a6dbf402 13203 if (info->textrel_check == textrel_check_error)
c192a133 13204 info->callbacks->einfo
9793eb77 13205 (_("%P%X: read-only segment has dynamic relocations\n"));
a6dbf402
L
13206 else if (bfd_link_dll (info))
13207 info->callbacks->einfo
13208 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
c192a133
AM
13209 else
13210 info->callbacks->einfo
a6dbf402 13211 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
943284cc
DJ
13212 break;
13213 }
13214 }
13215 }
13216
c152c796
AM
13217 for (o = dynobj->sections; o != NULL; o = o->next)
13218 {
13219 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 13220 || o->size == 0
c152c796
AM
13221 || o->output_section == bfd_abs_section_ptr)
13222 continue;
13223 if ((o->flags & SEC_LINKER_CREATED) == 0)
13224 {
13225 /* At this point, we are only interested in sections
13226 created by _bfd_elf_link_create_dynamic_sections. */
13227 continue;
13228 }
64f52338 13229 if (htab->stab_info.stabstr == o)
3722b82f 13230 continue;
64f52338 13231 if (htab->eh_info.hdr_sec == o)
eea6121a 13232 continue;
3d4d4302 13233 if (strcmp (o->name, ".dynstr") != 0)
c152c796 13234 {
bb294208
AM
13235 bfd_size_type octets = ((file_ptr) o->output_offset
13236 * bfd_octets_per_byte (abfd, o));
13237 if (!bfd_set_section_contents (abfd, o->output_section,
13238 o->contents, octets, o->size))
c152c796
AM
13239 goto error_return;
13240 }
13241 else
13242 {
13243 /* The contents of the .dynstr section are actually in a
13244 stringtab. */
8539e4e8
AM
13245 file_ptr off;
13246
c152c796
AM
13247 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13248 if (bfd_seek (abfd, off, SEEK_SET) != 0
64f52338 13249 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
c152c796
AM
13250 goto error_return;
13251 }
13252 }
13253 }
13254
7bdf4127 13255 if (!info->resolve_section_groups)
c152c796
AM
13256 {
13257 bfd_boolean failed = FALSE;
13258
7bdf4127 13259 BFD_ASSERT (bfd_link_relocatable (info));
c152c796
AM
13260 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13261 if (failed)
13262 goto error_return;
13263 }
13264
13265 /* If we have optimized stabs strings, output them. */
64f52338 13266 if (htab->stab_info.stabstr != NULL)
c152c796 13267 {
64f52338 13268 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
c152c796
AM
13269 goto error_return;
13270 }
13271
9f7c3e5e
AM
13272 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13273 goto error_return;
c152c796 13274
1ff6de03
NA
13275 if (info->callbacks->emit_ctf)
13276 info->callbacks->emit_ctf ();
13277
9f7c3e5e 13278 elf_final_link_free (abfd, &flinfo);
c152c796 13279
104d59d1
JM
13280 if (attr_section)
13281 {
a50b1753 13282 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 13283 if (contents == NULL)
496afd17
L
13284 {
13285 /* Bail out and fail. */
13286 ret = FALSE;
13287 goto return_local_hash_table;
13288 }
104d59d1
JM
13289 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
13290 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
13291 free (contents);
13292 }
13293
496afd17
L
13294 return_local_hash_table:
13295 if (info->unique_symbol)
13296 bfd_hash_table_free (&flinfo.local_hash_table);
13297 return ret;
c152c796
AM
13298
13299 error_return:
9f7c3e5e 13300 elf_final_link_free (abfd, &flinfo);
496afd17
L
13301 ret = FALSE;
13302 goto return_local_hash_table;
c152c796
AM
13303}
13304\f
5241d853
RS
13305/* Initialize COOKIE for input bfd ABFD. */
13306
13307static bfd_boolean
13308init_reloc_cookie (struct elf_reloc_cookie *cookie,
13309 struct bfd_link_info *info, bfd *abfd)
13310{
13311 Elf_Internal_Shdr *symtab_hdr;
13312 const struct elf_backend_data *bed;
13313
13314 bed = get_elf_backend_data (abfd);
13315 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13316
13317 cookie->abfd = abfd;
13318 cookie->sym_hashes = elf_sym_hashes (abfd);
13319 cookie->bad_symtab = elf_bad_symtab (abfd);
13320 if (cookie->bad_symtab)
13321 {
13322 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13323 cookie->extsymoff = 0;
13324 }
13325 else
13326 {
13327 cookie->locsymcount = symtab_hdr->sh_info;
13328 cookie->extsymoff = symtab_hdr->sh_info;
13329 }
13330
13331 if (bed->s->arch_size == 32)
13332 cookie->r_sym_shift = 8;
13333 else
13334 cookie->r_sym_shift = 32;
13335
13336 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13337 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13338 {
13339 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13340 cookie->locsymcount, 0,
13341 NULL, NULL, NULL);
13342 if (cookie->locsyms == NULL)
13343 {
13344 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
13345 return FALSE;
13346 }
13347 if (info->keep_memory)
13348 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13349 }
13350 return TRUE;
13351}
13352
13353/* Free the memory allocated by init_reloc_cookie, if appropriate. */
13354
13355static void
13356fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13357{
13358 Elf_Internal_Shdr *symtab_hdr;
13359
13360 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
c9594989 13361 if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
5241d853
RS
13362 free (cookie->locsyms);
13363}
13364
13365/* Initialize the relocation information in COOKIE for input section SEC
13366 of input bfd ABFD. */
13367
13368static bfd_boolean
13369init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13370 struct bfd_link_info *info, bfd *abfd,
13371 asection *sec)
13372{
5241d853
RS
13373 if (sec->reloc_count == 0)
13374 {
13375 cookie->rels = NULL;
13376 cookie->relend = NULL;
13377 }
13378 else
13379 {
5241d853
RS
13380 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
13381 info->keep_memory);
13382 if (cookie->rels == NULL)
13383 return FALSE;
13384 cookie->rel = cookie->rels;
056bafd4 13385 cookie->relend = cookie->rels + sec->reloc_count;
5241d853
RS
13386 }
13387 cookie->rel = cookie->rels;
13388 return TRUE;
13389}
13390
13391/* Free the memory allocated by init_reloc_cookie_rels,
13392 if appropriate. */
13393
13394static void
13395fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13396 asection *sec)
13397{
c9594989 13398 if (elf_section_data (sec)->relocs != cookie->rels)
5241d853
RS
13399 free (cookie->rels);
13400}
13401
13402/* Initialize the whole of COOKIE for input section SEC. */
13403
13404static bfd_boolean
13405init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13406 struct bfd_link_info *info,
13407 asection *sec)
13408{
13409 if (!init_reloc_cookie (cookie, info, sec->owner))
13410 goto error1;
13411 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13412 goto error2;
13413 return TRUE;
13414
13415 error2:
13416 fini_reloc_cookie (cookie, sec->owner);
13417 error1:
13418 return FALSE;
13419}
13420
13421/* Free the memory allocated by init_reloc_cookie_for_section,
13422 if appropriate. */
13423
13424static void
13425fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13426 asection *sec)
13427{
13428 fini_reloc_cookie_rels (cookie, sec);
13429 fini_reloc_cookie (cookie, sec->owner);
13430}
13431\f
c152c796
AM
13432/* Garbage collect unused sections. */
13433
07adf181
AM
13434/* Default gc_mark_hook. */
13435
13436asection *
13437_bfd_elf_gc_mark_hook (asection *sec,
13438 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13439 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13440 struct elf_link_hash_entry *h,
13441 Elf_Internal_Sym *sym)
13442{
13443 if (h != NULL)
13444 {
13445 switch (h->root.type)
13446 {
13447 case bfd_link_hash_defined:
13448 case bfd_link_hash_defweak:
13449 return h->root.u.def.section;
13450
13451 case bfd_link_hash_common:
13452 return h->root.u.c.p->section;
13453
13454 default:
13455 break;
13456 }
13457 }
13458 else
13459 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13460
13461 return NULL;
13462}
13463
9e223787 13464/* Return the debug definition section. */
b7c871ed
L
13465
13466static asection *
13467elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13468 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13469 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13470 struct elf_link_hash_entry *h,
9e223787 13471 Elf_Internal_Sym *sym)
b7c871ed 13472{
9e223787
L
13473 if (h != NULL)
13474 {
13475 /* Return the global debug definition section. */
13476 if ((h->root.type == bfd_link_hash_defined
13477 || h->root.type == bfd_link_hash_defweak)
13478 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13479 return h->root.u.def.section;
13480 }
13481 else
13482 {
13483 /* Return the local debug definition section. */
13484 asection *isec = bfd_section_from_elf_index (sec->owner,
13485 sym->st_shndx);
13486 if ((isec->flags & SEC_DEBUGGING) != 0)
13487 return isec;
13488 }
b7c871ed
L
13489
13490 return NULL;
13491}
13492
5241d853
RS
13493/* COOKIE->rel describes a relocation against section SEC, which is
13494 a section we've decided to keep. Return the section that contains
13495 the relocation symbol, or NULL if no section contains it. */
13496
13497asection *
13498_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13499 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
13500 struct elf_reloc_cookie *cookie,
13501 bfd_boolean *start_stop)
5241d853
RS
13502{
13503 unsigned long r_symndx;
3024a17a 13504 struct elf_link_hash_entry *h, *hw;
5241d853
RS
13505
13506 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 13507 if (r_symndx == STN_UNDEF)
5241d853
RS
13508 return NULL;
13509
13510 if (r_symndx >= cookie->locsymcount
13511 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13512 {
13513 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
13514 if (h == NULL)
13515 {
871b3ab2 13516 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
263ddf68
L
13517 sec->owner);
13518 return NULL;
13519 }
5241d853
RS
13520 while (h->root.type == bfd_link_hash_indirect
13521 || h->root.type == bfd_link_hash_warning)
13522 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 13523 h->mark = 1;
3024a17a
AM
13524 /* Keep all aliases of the symbol too. If an object symbol
13525 needs to be copied into .dynbss then all of its aliases
13526 should be present as dynamic symbols, not just the one used
13527 on the copy relocation. */
13528 hw = h;
13529 while (hw->is_weakalias)
13530 {
13531 hw = hw->u.alias;
13532 hw->mark = 1;
13533 }
1cce69b9 13534
a6a4679f 13535 if (start_stop != NULL)
1cce69b9 13536 {
7dba9362
AM
13537 /* To work around a glibc bug, mark XXX input sections
13538 when there is a reference to __start_XXX or __stop_XXX
13539 symbols. */
cbd0eecf 13540 if (h->start_stop)
1cce69b9 13541 {
cbd0eecf 13542 asection *s = h->u2.start_stop_section;
a6a4679f
AM
13543 *start_stop = !s->gc_mark;
13544 return s;
1cce69b9
AM
13545 }
13546 }
13547
5241d853
RS
13548 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13549 }
13550
13551 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13552 &cookie->locsyms[r_symndx]);
13553}
13554
13555/* COOKIE->rel describes a relocation against section SEC, which is
13556 a section we've decided to keep. Mark the section that contains
9d0a14d3 13557 the relocation symbol. */
5241d853
RS
13558
13559bfd_boolean
13560_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13561 asection *sec,
13562 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 13563 struct elf_reloc_cookie *cookie)
5241d853
RS
13564{
13565 asection *rsec;
1cce69b9 13566 bfd_boolean start_stop = FALSE;
5241d853 13567
1cce69b9
AM
13568 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13569 while (rsec != NULL)
5241d853 13570 {
1cce69b9
AM
13571 if (!rsec->gc_mark)
13572 {
13573 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13574 || (rsec->owner->flags & DYNAMIC) != 0)
13575 rsec->gc_mark = 1;
13576 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13577 return FALSE;
13578 }
13579 if (!start_stop)
13580 break;
199af150 13581 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
13582 }
13583 return TRUE;
13584}
13585
07adf181
AM
13586/* The mark phase of garbage collection. For a given section, mark
13587 it and any sections in this section's group, and all the sections
13588 which define symbols to which it refers. */
13589
ccfa59ea
AM
13590bfd_boolean
13591_bfd_elf_gc_mark (struct bfd_link_info *info,
13592 asection *sec,
6a5bb875 13593 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
13594{
13595 bfd_boolean ret;
9d0a14d3 13596 asection *group_sec, *eh_frame;
c152c796
AM
13597
13598 sec->gc_mark = 1;
13599
13600 /* Mark all the sections in the group. */
13601 group_sec = elf_section_data (sec)->next_in_group;
13602 if (group_sec && !group_sec->gc_mark)
ccfa59ea 13603 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
13604 return FALSE;
13605
13606 /* Look through the section relocs. */
13607 ret = TRUE;
9d0a14d3
RS
13608 eh_frame = elf_eh_frame_section (sec->owner);
13609 if ((sec->flags & SEC_RELOC) != 0
13610 && sec->reloc_count > 0
13611 && sec != eh_frame)
c152c796 13612 {
5241d853 13613 struct elf_reloc_cookie cookie;
c152c796 13614
5241d853
RS
13615 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13616 ret = FALSE;
c152c796 13617 else
c152c796 13618 {
5241d853 13619 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 13620 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
13621 {
13622 ret = FALSE;
13623 break;
13624 }
13625 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
13626 }
13627 }
9d0a14d3
RS
13628
13629 if (ret && eh_frame && elf_fde_list (sec))
13630 {
13631 struct elf_reloc_cookie cookie;
13632
13633 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13634 ret = FALSE;
13635 else
13636 {
13637 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13638 gc_mark_hook, &cookie))
13639 ret = FALSE;
13640 fini_reloc_cookie_for_section (&cookie, eh_frame);
13641 }
13642 }
13643
2f0c68f2
CM
13644 eh_frame = elf_section_eh_frame_entry (sec);
13645 if (ret && eh_frame && !eh_frame->gc_mark)
13646 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13647 ret = FALSE;
13648
c152c796
AM
13649 return ret;
13650}
13651
3c758495
TG
13652/* Scan and mark sections in a special or debug section group. */
13653
13654static void
13655_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13656{
13657 /* Point to first section of section group. */
13658 asection *ssec;
13659 /* Used to iterate the section group. */
13660 asection *msec;
13661
13662 bfd_boolean is_special_grp = TRUE;
13663 bfd_boolean is_debug_grp = TRUE;
13664
13665 /* First scan to see if group contains any section other than debug
13666 and special section. */
13667 ssec = msec = elf_next_in_group (grp);
13668 do
13669 {
13670 if ((msec->flags & SEC_DEBUGGING) == 0)
13671 is_debug_grp = FALSE;
13672
13673 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13674 is_special_grp = FALSE;
13675
13676 msec = elf_next_in_group (msec);
13677 }
13678 while (msec != ssec);
13679
13680 /* If this is a pure debug section group or pure special section group,
13681 keep all sections in this group. */
13682 if (is_debug_grp || is_special_grp)
13683 {
13684 do
13685 {
13686 msec->gc_mark = 1;
13687 msec = elf_next_in_group (msec);
13688 }
13689 while (msec != ssec);
13690 }
13691}
13692
7f6ab9f8
AM
13693/* Keep debug and special sections. */
13694
13695bfd_boolean
13696_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
b7d07216 13697 elf_gc_mark_hook_fn mark_hook)
7f6ab9f8
AM
13698{
13699 bfd *ibfd;
13700
c72f2fb2 13701 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
13702 {
13703 asection *isec;
13704 bfd_boolean some_kept;
b40bf0a2 13705 bfd_boolean debug_frag_seen;
b7c871ed 13706 bfd_boolean has_kept_debug_info;
7f6ab9f8
AM
13707
13708 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13709 continue;
57963c05
AM
13710 isec = ibfd->sections;
13711 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13712 continue;
7f6ab9f8 13713
b40bf0a2
NC
13714 /* Ensure all linker created sections are kept,
13715 see if any other section is already marked,
13716 and note if we have any fragmented debug sections. */
b7c871ed 13717 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
7f6ab9f8
AM
13718 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13719 {
13720 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13721 isec->gc_mark = 1;
eb026f09
AM
13722 else if (isec->gc_mark
13723 && (isec->flags & SEC_ALLOC) != 0
13724 && elf_section_type (isec) != SHT_NOTE)
7f6ab9f8 13725 some_kept = TRUE;
b7d07216
L
13726 else
13727 {
13728 /* Since all sections, except for backend specific ones,
13729 have been garbage collected, call mark_hook on this
13730 section if any of its linked-to sections is marked. */
13731 asection *linked_to_sec = elf_linked_to_section (isec);
13732 for (; linked_to_sec != NULL;
13733 linked_to_sec = elf_linked_to_section (linked_to_sec))
13734 if (linked_to_sec->gc_mark)
13735 {
13736 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
13737 return FALSE;
13738 break;
13739 }
13740 }
b40bf0a2 13741
535b785f 13742 if (!debug_frag_seen
b40bf0a2
NC
13743 && (isec->flags & SEC_DEBUGGING)
13744 && CONST_STRNEQ (isec->name, ".debug_line."))
13745 debug_frag_seen = TRUE;
5242a0a0
L
13746 else if (strcmp (bfd_section_name (isec),
13747 "__patchable_function_entries") == 0
13748 && elf_linked_to_section (isec) == NULL)
13749 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
13750 "need linked-to section "
13751 "for --gc-sections\n"),
13752 isec->owner, isec);
7f6ab9f8
AM
13753 }
13754
eb026f09
AM
13755 /* If no non-note alloc section in this file will be kept, then
13756 we can toss out the debug and special sections. */
7f6ab9f8
AM
13757 if (!some_kept)
13758 continue;
13759
13760 /* Keep debug and special sections like .comment when they are
3c758495 13761 not part of a group. Also keep section groups that contain
b7d07216
L
13762 just debug sections or special sections. NB: Sections with
13763 linked-to section has been handled above. */
7f6ab9f8 13764 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
13765 {
13766 if ((isec->flags & SEC_GROUP) != 0)
13767 _bfd_elf_gc_mark_debug_special_section_group (isec);
13768 else if (((isec->flags & SEC_DEBUGGING) != 0
13769 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
b7d07216
L
13770 && elf_next_in_group (isec) == NULL
13771 && elf_linked_to_section (isec) == NULL)
3c758495 13772 isec->gc_mark = 1;
b7c871ed
L
13773 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13774 has_kept_debug_info = TRUE;
3c758495 13775 }
b40bf0a2 13776
b40bf0a2
NC
13777 /* Look for CODE sections which are going to be discarded,
13778 and find and discard any fragmented debug sections which
13779 are associated with that code section. */
b7c871ed
L
13780 if (debug_frag_seen)
13781 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13782 if ((isec->flags & SEC_CODE) != 0
13783 && isec->gc_mark == 0)
13784 {
13785 unsigned int ilen;
13786 asection *dsec;
b40bf0a2 13787
b7c871ed 13788 ilen = strlen (isec->name);
b40bf0a2 13789
b7c871ed 13790 /* Association is determined by the name of the debug
07d6d2b8 13791 section containing the name of the code section as
b7c871ed
L
13792 a suffix. For example .debug_line.text.foo is a
13793 debug section associated with .text.foo. */
13794 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13795 {
13796 unsigned int dlen;
b40bf0a2 13797
b7c871ed
L
13798 if (dsec->gc_mark == 0
13799 || (dsec->flags & SEC_DEBUGGING) == 0)
13800 continue;
b40bf0a2 13801
b7c871ed 13802 dlen = strlen (dsec->name);
b40bf0a2 13803
b7c871ed
L
13804 if (dlen > ilen
13805 && strncmp (dsec->name + (dlen - ilen),
13806 isec->name, ilen) == 0)
b40bf0a2 13807 dsec->gc_mark = 0;
b7c871ed 13808 }
b40bf0a2 13809 }
b7c871ed
L
13810
13811 /* Mark debug sections referenced by kept debug sections. */
13812 if (has_kept_debug_info)
13813 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13814 if (isec->gc_mark
13815 && (isec->flags & SEC_DEBUGGING) != 0)
13816 if (!_bfd_elf_gc_mark (info, isec,
13817 elf_gc_mark_debug_section))
13818 return FALSE;
7f6ab9f8
AM
13819 }
13820 return TRUE;
13821}
13822
c152c796 13823static bfd_boolean
ccabcbe5 13824elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
13825{
13826 bfd *sub;
ccabcbe5 13827 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
c152c796 13828
c72f2fb2 13829 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13830 {
13831 asection *o;
13832
b19a8f85 13833 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 13834 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
b19a8f85 13835 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796 13836 continue;
57963c05
AM
13837 o = sub->sections;
13838 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13839 continue;
c152c796
AM
13840
13841 for (o = sub->sections; o != NULL; o = o->next)
13842 {
a33dafc3
L
13843 /* When any section in a section group is kept, we keep all
13844 sections in the section group. If the first member of
13845 the section group is excluded, we will also exclude the
13846 group section. */
13847 if (o->flags & SEC_GROUP)
13848 {
13849 asection *first = elf_next_in_group (o);
13850 o->gc_mark = first->gc_mark;
13851 }
c152c796 13852
1e7eae0d 13853 if (o->gc_mark)
c152c796
AM
13854 continue;
13855
13856 /* Skip sweeping sections already excluded. */
13857 if (o->flags & SEC_EXCLUDE)
13858 continue;
13859
13860 /* Since this is early in the link process, it is simple
13861 to remove a section from the output. */
13862 o->flags |= SEC_EXCLUDE;
13863
c55fe096 13864 if (info->print_gc_sections && o->size != 0)
695344c0 13865 /* xgettext:c-format */
9793eb77 13866 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
c08bb8dd 13867 o, sub);
c152c796
AM
13868 }
13869 }
13870
c152c796
AM
13871 return TRUE;
13872}
13873
13874/* Propagate collected vtable information. This is called through
13875 elf_link_hash_traverse. */
13876
13877static bfd_boolean
13878elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13879{
c152c796 13880 /* Those that are not vtables. */
cbd0eecf
L
13881 if (h->start_stop
13882 || h->u2.vtable == NULL
13883 || h->u2.vtable->parent == NULL)
c152c796
AM
13884 return TRUE;
13885
13886 /* Those vtables that do not have parents, we cannot merge. */
cbd0eecf 13887 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
13888 return TRUE;
13889
13890 /* If we've already been done, exit. */
cbd0eecf 13891 if (h->u2.vtable->used && h->u2.vtable->used[-1])
c152c796
AM
13892 return TRUE;
13893
13894 /* Make sure the parent's table is up to date. */
cbd0eecf 13895 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
c152c796 13896
cbd0eecf 13897 if (h->u2.vtable->used == NULL)
c152c796
AM
13898 {
13899 /* None of this table's entries were referenced. Re-use the
13900 parent's table. */
cbd0eecf
L
13901 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13902 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
c152c796
AM
13903 }
13904 else
13905 {
13906 size_t n;
13907 bfd_boolean *cu, *pu;
13908
13909 /* Or the parent's entries into ours. */
cbd0eecf 13910 cu = h->u2.vtable->used;
c152c796 13911 cu[-1] = TRUE;
cbd0eecf 13912 pu = h->u2.vtable->parent->u2.vtable->used;
c152c796
AM
13913 if (pu != NULL)
13914 {
13915 const struct elf_backend_data *bed;
13916 unsigned int log_file_align;
13917
13918 bed = get_elf_backend_data (h->root.u.def.section->owner);
13919 log_file_align = bed->s->log_file_align;
cbd0eecf 13920 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
c152c796
AM
13921 while (n--)
13922 {
13923 if (*pu)
13924 *cu = TRUE;
13925 pu++;
13926 cu++;
13927 }
13928 }
13929 }
13930
13931 return TRUE;
13932}
13933
13934static bfd_boolean
13935elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13936{
13937 asection *sec;
13938 bfd_vma hstart, hend;
13939 Elf_Internal_Rela *relstart, *relend, *rel;
13940 const struct elf_backend_data *bed;
13941 unsigned int log_file_align;
13942
c152c796
AM
13943 /* Take care of both those symbols that do not describe vtables as
13944 well as those that are not loaded. */
cbd0eecf
L
13945 if (h->start_stop
13946 || h->u2.vtable == NULL
13947 || h->u2.vtable->parent == NULL)
c152c796
AM
13948 return TRUE;
13949
13950 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13951 || h->root.type == bfd_link_hash_defweak);
13952
13953 sec = h->root.u.def.section;
13954 hstart = h->root.u.def.value;
13955 hend = hstart + h->size;
13956
13957 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13958 if (!relstart)
13959 return *(bfd_boolean *) okp = FALSE;
13960 bed = get_elf_backend_data (sec->owner);
13961 log_file_align = bed->s->log_file_align;
13962
056bafd4 13963 relend = relstart + sec->reloc_count;
c152c796
AM
13964
13965 for (rel = relstart; rel < relend; ++rel)
13966 if (rel->r_offset >= hstart && rel->r_offset < hend)
13967 {
13968 /* If the entry is in use, do nothing. */
cbd0eecf
L
13969 if (h->u2.vtable->used
13970 && (rel->r_offset - hstart) < h->u2.vtable->size)
c152c796
AM
13971 {
13972 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
cbd0eecf 13973 if (h->u2.vtable->used[entry])
c152c796
AM
13974 continue;
13975 }
13976 /* Otherwise, kill it. */
13977 rel->r_offset = rel->r_info = rel->r_addend = 0;
13978 }
13979
13980 return TRUE;
13981}
13982
87538722
AM
13983/* Mark sections containing dynamically referenced symbols. When
13984 building shared libraries, we must assume that any visible symbol is
13985 referenced. */
715df9b8 13986
64d03ab5
AM
13987bfd_boolean
13988bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 13989{
87538722 13990 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 13991 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 13992
715df9b8
EB
13993 if ((h->root.type == bfd_link_hash_defined
13994 || h->root.type == bfd_link_hash_defweak)
d664fd41 13995 && ((h->ref_dynamic && !h->forced_local)
c4621b33 13996 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 13997 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 13998 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 13999 && (!bfd_link_executable (info)
22185505 14000 || info->gc_keep_exported
b407645f
AM
14001 || info->export_dynamic
14002 || (h->dynamic
14003 && d != NULL
14004 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 14005 && (h->versioned >= versioned
54e8959c
L
14006 || !bfd_hide_sym_by_version (info->version_info,
14007 h->root.root.string)))))
715df9b8
EB
14008 h->root.u.def.section->flags |= SEC_KEEP;
14009
14010 return TRUE;
14011}
3b36f7e6 14012
74f0fb50
AM
14013/* Keep all sections containing symbols undefined on the command-line,
14014 and the section containing the entry symbol. */
14015
14016void
14017_bfd_elf_gc_keep (struct bfd_link_info *info)
14018{
14019 struct bfd_sym_chain *sym;
14020
14021 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
14022 {
14023 struct elf_link_hash_entry *h;
14024
14025 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
14026 FALSE, FALSE, FALSE);
14027
14028 if (h != NULL
14029 && (h->root.type == bfd_link_hash_defined
14030 || h->root.type == bfd_link_hash_defweak)
2f5541f3 14031 && !bfd_is_const_section (h->root.u.def.section))
74f0fb50
AM
14032 h->root.u.def.section->flags |= SEC_KEEP;
14033 }
14034}
14035
2f0c68f2
CM
14036bfd_boolean
14037bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
14038 struct bfd_link_info *info)
14039{
14040 bfd *ibfd = info->input_bfds;
14041
14042 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14043 {
14044 asection *sec;
14045 struct elf_reloc_cookie cookie;
14046
14047 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14048 continue;
57963c05
AM
14049 sec = ibfd->sections;
14050 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14051 continue;
2f0c68f2
CM
14052
14053 if (!init_reloc_cookie (&cookie, info, ibfd))
14054 return FALSE;
14055
14056 for (sec = ibfd->sections; sec; sec = sec->next)
14057 {
fd361982 14058 if (CONST_STRNEQ (bfd_section_name (sec), ".eh_frame_entry")
2f0c68f2
CM
14059 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
14060 {
14061 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
14062 fini_reloc_cookie_rels (&cookie, sec);
14063 }
14064 }
14065 }
14066 return TRUE;
14067}
14068
c152c796
AM
14069/* Do mark and sweep of unused sections. */
14070
14071bfd_boolean
14072bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
14073{
14074 bfd_boolean ok = TRUE;
14075 bfd *sub;
6a5bb875 14076 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 14077 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 14078 struct elf_link_hash_table *htab;
c152c796 14079
64d03ab5 14080 if (!bed->can_gc_sections
715df9b8 14081 || !is_elf_hash_table (info->hash))
c152c796 14082 {
9793eb77 14083 _bfd_error_handler(_("warning: gc-sections option ignored"));
c152c796
AM
14084 return TRUE;
14085 }
14086
74f0fb50 14087 bed->gc_keep (info);
da44f4e5 14088 htab = elf_hash_table (info);
74f0fb50 14089
9d0a14d3
RS
14090 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14091 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
14092 for (sub = info->input_bfds;
14093 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14094 sub = sub->link.next)
9d0a14d3
RS
14095 {
14096 asection *sec;
14097 struct elf_reloc_cookie cookie;
14098
57963c05
AM
14099 sec = sub->sections;
14100 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14101 continue;
9d0a14d3 14102 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 14103 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
14104 {
14105 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
14106 if (elf_section_data (sec)->sec_info
14107 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
14108 elf_eh_frame_section (sub) = sec;
14109 fini_reloc_cookie_for_section (&cookie, sec);
199af150 14110 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
14111 }
14112 }
9d0a14d3 14113
c152c796 14114 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 14115 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
14116 if (!ok)
14117 return FALSE;
14118
14119 /* Kill the vtable relocations that were not used. */
da44f4e5 14120 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
14121 if (!ok)
14122 return FALSE;
14123
715df9b8 14124 /* Mark dynamically referenced symbols. */
22185505 14125 if (htab->dynamic_sections_created || info->gc_keep_exported)
da44f4e5 14126 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 14127
715df9b8 14128 /* Grovel through relocs to find out who stays ... */
64d03ab5 14129 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 14130 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
14131 {
14132 asection *o;
14133
b19a8f85 14134 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 14135 || elf_object_id (sub) != elf_hash_table_id (htab)
b19a8f85 14136 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
14137 continue;
14138
57963c05
AM
14139 o = sub->sections;
14140 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14141 continue;
14142
7f6ab9f8
AM
14143 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14144 Also treat note sections as a root, if the section is not part
8b6f4cd3
L
14145 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14146 well as FINI_ARRAY sections for ld -r. */
c152c796 14147 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
14148 if (!o->gc_mark
14149 && (o->flags & SEC_EXCLUDE) == 0
24007750 14150 && ((o->flags & SEC_KEEP) != 0
8b6f4cd3
L
14151 || (bfd_link_relocatable (info)
14152 && ((elf_section_data (o)->this_hdr.sh_type
14153 == SHT_PREINIT_ARRAY)
14154 || (elf_section_data (o)->this_hdr.sh_type
14155 == SHT_INIT_ARRAY)
14156 || (elf_section_data (o)->this_hdr.sh_type
14157 == SHT_FINI_ARRAY)))
7f6ab9f8 14158 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
7026832e 14159 && elf_next_in_group (o) == NULL
99fabbc9
JL
14160 && elf_linked_to_section (o) == NULL)
14161 || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14162 && (elf_section_flags (o) & SHF_GNU_RETAIN))))
7f6ab9f8
AM
14163 {
14164 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
14165 return FALSE;
14166 }
c152c796
AM
14167 }
14168
6a5bb875 14169 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 14170 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 14171
c152c796 14172 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 14173 return elf_gc_sweep (abfd, info);
c152c796
AM
14174}
14175\f
14176/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14177
14178bfd_boolean
14179bfd_elf_gc_record_vtinherit (bfd *abfd,
14180 asection *sec,
14181 struct elf_link_hash_entry *h,
14182 bfd_vma offset)
14183{
14184 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14185 struct elf_link_hash_entry **search, *child;
ef53be89 14186 size_t extsymcount;
c152c796
AM
14187 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14188
14189 /* The sh_info field of the symtab header tells us where the
14190 external symbols start. We don't care about the local symbols at
14191 this point. */
14192 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
14193 if (!elf_bad_symtab (abfd))
14194 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
14195
14196 sym_hashes = elf_sym_hashes (abfd);
14197 sym_hashes_end = sym_hashes + extsymcount;
14198
14199 /* Hunt down the child symbol, which is in this section at the same
14200 offset as the relocation. */
14201 for (search = sym_hashes; search != sym_hashes_end; ++search)
14202 {
14203 if ((child = *search) != NULL
14204 && (child->root.type == bfd_link_hash_defined
14205 || child->root.type == bfd_link_hash_defweak)
14206 && child->root.u.def.section == sec
14207 && child->root.u.def.value == offset)
14208 goto win;
14209 }
14210
695344c0 14211 /* xgettext:c-format */
9793eb77 14212 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
2dcf00ce 14213 abfd, sec, (uint64_t) offset);
c152c796
AM
14214 bfd_set_error (bfd_error_invalid_operation);
14215 return FALSE;
14216
14217 win:
cbd0eecf 14218 if (!child->u2.vtable)
f6e332e6 14219 {
cbd0eecf
L
14220 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
14221 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
14222 if (!child->u2.vtable)
f6e332e6
AM
14223 return FALSE;
14224 }
c152c796
AM
14225 if (!h)
14226 {
14227 /* This *should* only be the absolute section. It could potentially
14228 be that someone has defined a non-global vtable though, which
14229 would be bad. It isn't worth paging in the local symbols to be
14230 sure though; that case should simply be handled by the assembler. */
14231
cbd0eecf 14232 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
14233 }
14234 else
cbd0eecf 14235 child->u2.vtable->parent = h;
c152c796
AM
14236
14237 return TRUE;
14238}
14239
14240/* Called from check_relocs to record the existence of a VTENTRY reloc. */
14241
14242bfd_boolean
a0ea3a14 14243bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
c152c796
AM
14244 struct elf_link_hash_entry *h,
14245 bfd_vma addend)
14246{
14247 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14248 unsigned int log_file_align = bed->s->log_file_align;
14249
a0ea3a14
L
14250 if (!h)
14251 {
14252 /* xgettext:c-format */
14253 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14254 abfd, sec);
14255 bfd_set_error (bfd_error_bad_value);
14256 return FALSE;
14257 }
14258
cbd0eecf 14259 if (!h->u2.vtable)
f6e332e6 14260 {
cbd0eecf
L
14261 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14262 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14263 if (!h->u2.vtable)
f6e332e6
AM
14264 return FALSE;
14265 }
14266
cbd0eecf 14267 if (addend >= h->u2.vtable->size)
c152c796
AM
14268 {
14269 size_t size, bytes, file_align;
cbd0eecf 14270 bfd_boolean *ptr = h->u2.vtable->used;
c152c796
AM
14271
14272 /* While the symbol is undefined, we have to be prepared to handle
14273 a zero size. */
14274 file_align = 1 << log_file_align;
14275 if (h->root.type == bfd_link_hash_undefined)
14276 size = addend + file_align;
14277 else
14278 {
14279 size = h->size;
14280 if (addend >= size)
14281 {
14282 /* Oops! We've got a reference past the defined end of
14283 the table. This is probably a bug -- shall we warn? */
14284 size = addend + file_align;
14285 }
14286 }
14287 size = (size + file_align - 1) & -file_align;
14288
14289 /* Allocate one extra entry for use as a "done" flag for the
14290 consolidation pass. */
14291 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
14292
14293 if (ptr)
14294 {
a50b1753 14295 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
14296
14297 if (ptr != NULL)
14298 {
14299 size_t oldbytes;
14300
cbd0eecf 14301 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
c152c796
AM
14302 * sizeof (bfd_boolean));
14303 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14304 }
14305 }
14306 else
a50b1753 14307 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
14308
14309 if (ptr == NULL)
14310 return FALSE;
14311
14312 /* And arrange for that done flag to be at index -1. */
cbd0eecf
L
14313 h->u2.vtable->used = ptr + 1;
14314 h->u2.vtable->size = size;
c152c796
AM
14315 }
14316
cbd0eecf 14317 h->u2.vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
14318
14319 return TRUE;
14320}
14321
ae17ab41
CM
14322/* Map an ELF section header flag to its corresponding string. */
14323typedef struct
14324{
14325 char *flag_name;
14326 flagword flag_value;
14327} elf_flags_to_name_table;
14328
14329static elf_flags_to_name_table elf_flags_to_names [] =
14330{
14331 { "SHF_WRITE", SHF_WRITE },
14332 { "SHF_ALLOC", SHF_ALLOC },
14333 { "SHF_EXECINSTR", SHF_EXECINSTR },
14334 { "SHF_MERGE", SHF_MERGE },
14335 { "SHF_STRINGS", SHF_STRINGS },
14336 { "SHF_INFO_LINK", SHF_INFO_LINK},
14337 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14338 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14339 { "SHF_GROUP", SHF_GROUP },
14340 { "SHF_TLS", SHF_TLS },
14341 { "SHF_MASKOS", SHF_MASKOS },
14342 { "SHF_EXCLUDE", SHF_EXCLUDE },
14343};
14344
b9c361e0
JL
14345/* Returns TRUE if the section is to be included, otherwise FALSE. */
14346bfd_boolean
ae17ab41 14347bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 14348 struct flag_info *flaginfo,
b9c361e0 14349 asection *section)
ae17ab41 14350{
8b127cbc 14351 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 14352
8b127cbc 14353 if (!flaginfo->flags_initialized)
ae17ab41 14354 {
8b127cbc
AM
14355 bfd *obfd = info->output_bfd;
14356 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14357 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
14358 int with_hex = 0;
14359 int without_hex = 0;
14360
8b127cbc 14361 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 14362 {
b9c361e0 14363 unsigned i;
8b127cbc 14364 flagword (*lookup) (char *);
ae17ab41 14365
8b127cbc
AM
14366 lookup = bed->elf_backend_lookup_section_flags_hook;
14367 if (lookup != NULL)
ae17ab41 14368 {
8b127cbc 14369 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
14370
14371 if (hexval != 0)
14372 {
14373 if (tf->with == with_flags)
14374 with_hex |= hexval;
14375 else if (tf->with == without_flags)
14376 without_hex |= hexval;
14377 tf->valid = TRUE;
14378 continue;
14379 }
ae17ab41 14380 }
8b127cbc 14381 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 14382 {
8b127cbc 14383 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
14384 {
14385 if (tf->with == with_flags)
14386 with_hex |= elf_flags_to_names[i].flag_value;
14387 else if (tf->with == without_flags)
14388 without_hex |= elf_flags_to_names[i].flag_value;
14389 tf->valid = TRUE;
14390 break;
14391 }
14392 }
8b127cbc 14393 if (!tf->valid)
b9c361e0 14394 {
68ffbac6 14395 info->callbacks->einfo
9793eb77 14396 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 14397 return FALSE;
ae17ab41
CM
14398 }
14399 }
8b127cbc
AM
14400 flaginfo->flags_initialized = TRUE;
14401 flaginfo->only_with_flags |= with_hex;
14402 flaginfo->not_with_flags |= without_hex;
ae17ab41 14403 }
ae17ab41 14404
8b127cbc 14405 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
14406 return FALSE;
14407
8b127cbc 14408 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
14409 return FALSE;
14410
14411 return TRUE;
ae17ab41
CM
14412}
14413
c152c796
AM
14414struct alloc_got_off_arg {
14415 bfd_vma gotoff;
10455f89 14416 struct bfd_link_info *info;
c152c796
AM
14417};
14418
14419/* We need a special top-level link routine to convert got reference counts
14420 to real got offsets. */
14421
14422static bfd_boolean
14423elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14424{
a50b1753 14425 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
14426 bfd *obfd = gofarg->info->output_bfd;
14427 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 14428
c152c796
AM
14429 if (h->got.refcount > 0)
14430 {
14431 h->got.offset = gofarg->gotoff;
10455f89 14432 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
14433 }
14434 else
14435 h->got.offset = (bfd_vma) -1;
14436
14437 return TRUE;
14438}
14439
14440/* And an accompanying bit to work out final got entry offsets once
14441 we're done. Should be called from final_link. */
14442
14443bfd_boolean
14444bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14445 struct bfd_link_info *info)
14446{
14447 bfd *i;
14448 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14449 bfd_vma gotoff;
c152c796
AM
14450 struct alloc_got_off_arg gofarg;
14451
10455f89
HPN
14452 BFD_ASSERT (abfd == info->output_bfd);
14453
c152c796
AM
14454 if (! is_elf_hash_table (info->hash))
14455 return FALSE;
14456
14457 /* The GOT offset is relative to the .got section, but the GOT header is
14458 put into the .got.plt section, if the backend uses it. */
14459 if (bed->want_got_plt)
14460 gotoff = 0;
14461 else
14462 gotoff = bed->got_header_size;
14463
14464 /* Do the local .got entries first. */
c72f2fb2 14465 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
14466 {
14467 bfd_signed_vma *local_got;
ef53be89 14468 size_t j, locsymcount;
c152c796
AM
14469 Elf_Internal_Shdr *symtab_hdr;
14470
14471 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14472 continue;
14473
14474 local_got = elf_local_got_refcounts (i);
14475 if (!local_got)
14476 continue;
14477
14478 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14479 if (elf_bad_symtab (i))
14480 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14481 else
14482 locsymcount = symtab_hdr->sh_info;
14483
14484 for (j = 0; j < locsymcount; ++j)
14485 {
14486 if (local_got[j] > 0)
14487 {
14488 local_got[j] = gotoff;
10455f89 14489 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
14490 }
14491 else
14492 local_got[j] = (bfd_vma) -1;
14493 }
14494 }
14495
14496 /* Then the global .got entries. .plt refcounts are handled by
14497 adjust_dynamic_symbol */
14498 gofarg.gotoff = gotoff;
10455f89 14499 gofarg.info = info;
c152c796
AM
14500 elf_link_hash_traverse (elf_hash_table (info),
14501 elf_gc_allocate_got_offsets,
14502 &gofarg);
14503 return TRUE;
14504}
14505
14506/* Many folk need no more in the way of final link than this, once
14507 got entry reference counting is enabled. */
14508
14509bfd_boolean
14510bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14511{
14512 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14513 return FALSE;
14514
14515 /* Invoke the regular ELF backend linker to do all the work. */
14516 return bfd_elf_final_link (abfd, info);
14517}
14518
14519bfd_boolean
14520bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14521{
a50b1753 14522 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
14523
14524 if (rcookie->bad_symtab)
14525 rcookie->rel = rcookie->rels;
14526
14527 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14528 {
14529 unsigned long r_symndx;
14530
14531 if (! rcookie->bad_symtab)
14532 if (rcookie->rel->r_offset > offset)
14533 return FALSE;
14534 if (rcookie->rel->r_offset != offset)
14535 continue;
14536
14537 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 14538 if (r_symndx == STN_UNDEF)
c152c796
AM
14539 return TRUE;
14540
14541 if (r_symndx >= rcookie->locsymcount
14542 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14543 {
14544 struct elf_link_hash_entry *h;
14545
14546 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14547
14548 while (h->root.type == bfd_link_hash_indirect
14549 || h->root.type == bfd_link_hash_warning)
14550 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14551
14552 if ((h->root.type == bfd_link_hash_defined
14553 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
14554 && (h->root.u.def.section->owner != rcookie->abfd
14555 || h->root.u.def.section->kept_section != NULL
14556 || discarded_section (h->root.u.def.section)))
c152c796 14557 return TRUE;
c152c796
AM
14558 }
14559 else
14560 {
14561 /* It's not a relocation against a global symbol,
14562 but it could be a relocation against a local
14563 symbol for a discarded section. */
14564 asection *isec;
14565 Elf_Internal_Sym *isym;
14566
14567 /* Need to: get the symbol; get the section. */
14568 isym = &rcookie->locsyms[r_symndx];
cb33740c 14569 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
14570 if (isec != NULL
14571 && (isec->kept_section != NULL
14572 || discarded_section (isec)))
cb33740c 14573 return TRUE;
c152c796
AM
14574 }
14575 return FALSE;
14576 }
14577 return FALSE;
14578}
14579
14580/* Discard unneeded references to discarded sections.
75938853
AM
14581 Returns -1 on error, 1 if any section's size was changed, 0 if
14582 nothing changed. This function assumes that the relocations are in
14583 sorted order, which is true for all known assemblers. */
c152c796 14584
75938853 14585int
c152c796
AM
14586bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14587{
14588 struct elf_reloc_cookie cookie;
18cd5bce 14589 asection *o;
c152c796 14590 bfd *abfd;
75938853 14591 int changed = 0;
c152c796
AM
14592
14593 if (info->traditional_format
14594 || !is_elf_hash_table (info->hash))
75938853 14595 return 0;
c152c796 14596
18cd5bce
AM
14597 o = bfd_get_section_by_name (output_bfd, ".stab");
14598 if (o != NULL)
c152c796 14599 {
18cd5bce 14600 asection *i;
c152c796 14601
18cd5bce 14602 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 14603 {
18cd5bce
AM
14604 if (i->size == 0
14605 || i->reloc_count == 0
14606 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14607 continue;
c152c796 14608
18cd5bce
AM
14609 abfd = i->owner;
14610 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14611 continue;
c152c796 14612
18cd5bce 14613 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14614 return -1;
c152c796 14615
18cd5bce
AM
14616 if (_bfd_discard_section_stabs (abfd, i,
14617 elf_section_data (i)->sec_info,
5241d853
RS
14618 bfd_elf_reloc_symbol_deleted_p,
14619 &cookie))
75938853 14620 changed = 1;
18cd5bce
AM
14621
14622 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14623 }
18cd5bce
AM
14624 }
14625
2f0c68f2
CM
14626 o = NULL;
14627 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14628 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
14629 if (o != NULL)
14630 {
14631 asection *i;
d7153c4a 14632 int eh_changed = 0;
66631823 14633 unsigned int eh_alignment; /* Octets. */
c152c796 14634
18cd5bce 14635 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 14636 {
18cd5bce
AM
14637 if (i->size == 0)
14638 continue;
14639
14640 abfd = i->owner;
14641 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14642 continue;
14643
14644 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14645 return -1;
18cd5bce
AM
14646
14647 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14648 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
14649 bfd_elf_reloc_symbol_deleted_p,
14650 &cookie))
d7153c4a
AM
14651 {
14652 eh_changed = 1;
14653 if (i->size != i->rawsize)
14654 changed = 1;
14655 }
18cd5bce
AM
14656
14657 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14658 }
9866ffe2 14659
66631823
CE
14660 eh_alignment = ((1 << o->alignment_power)
14661 * bfd_octets_per_byte (output_bfd, o));
9866ffe2
AM
14662 /* Skip over zero terminator, and prevent empty sections from
14663 adding alignment padding at the end. */
14664 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14665 if (i->size == 0)
14666 i->flags |= SEC_EXCLUDE;
14667 else if (i->size > 4)
14668 break;
14669 /* The last non-empty eh_frame section doesn't need padding. */
14670 if (i != NULL)
14671 i = i->map_tail.s;
14672 /* Any prior sections must pad the last FDE out to the output
14673 section alignment. Otherwise we might have zero padding
14674 between sections, which would be seen as a terminator. */
14675 for (; i != NULL; i = i->map_tail.s)
14676 if (i->size == 4)
14677 /* All but the last zero terminator should have been removed. */
14678 BFD_FAIL ();
14679 else
14680 {
14681 bfd_size_type size
14682 = (i->size + eh_alignment - 1) & -eh_alignment;
14683 if (i->size != size)
af471f82 14684 {
9866ffe2
AM
14685 i->size = size;
14686 changed = 1;
14687 eh_changed = 1;
af471f82 14688 }
9866ffe2 14689 }
d7153c4a
AM
14690 if (eh_changed)
14691 elf_link_hash_traverse (elf_hash_table (info),
14692 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
18cd5bce 14693 }
c152c796 14694
18cd5bce
AM
14695 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14696 {
14697 const struct elf_backend_data *bed;
57963c05 14698 asection *s;
c152c796 14699
18cd5bce
AM
14700 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14701 continue;
57963c05
AM
14702 s = abfd->sections;
14703 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14704 continue;
18cd5bce
AM
14705
14706 bed = get_elf_backend_data (abfd);
14707
14708 if (bed->elf_backend_discard_info != NULL)
14709 {
14710 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 14711 return -1;
18cd5bce
AM
14712
14713 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 14714 changed = 1;
18cd5bce
AM
14715
14716 fini_reloc_cookie (&cookie, abfd);
14717 }
c152c796
AM
14718 }
14719
2f0c68f2
CM
14720 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14721 _bfd_elf_end_eh_frame_parsing (info);
14722
14723 if (info->eh_frame_hdr_type
0e1862bb 14724 && !bfd_link_relocatable (info)
c152c796 14725 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 14726 changed = 1;
c152c796 14727
75938853 14728 return changed;
c152c796 14729}
082b7297 14730
43e1669b 14731bfd_boolean
0c511000 14732_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 14733 asection *sec,
c0f00686 14734 struct bfd_link_info *info)
082b7297
L
14735{
14736 flagword flags;
c77ec726 14737 const char *name, *key;
082b7297
L
14738 struct bfd_section_already_linked *l;
14739 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 14740
c77ec726
AM
14741 if (sec->output_section == bfd_abs_section_ptr)
14742 return FALSE;
0c511000 14743
c77ec726 14744 flags = sec->flags;
0c511000 14745
c77ec726
AM
14746 /* Return if it isn't a linkonce section. A comdat group section
14747 also has SEC_LINK_ONCE set. */
14748 if ((flags & SEC_LINK_ONCE) == 0)
14749 return FALSE;
0c511000 14750
c77ec726
AM
14751 /* Don't put group member sections on our list of already linked
14752 sections. They are handled as a group via their group section. */
14753 if (elf_sec_group (sec) != NULL)
14754 return FALSE;
0c511000 14755
c77ec726
AM
14756 /* For a SHT_GROUP section, use the group signature as the key. */
14757 name = sec->name;
14758 if ((flags & SEC_GROUP) != 0
14759 && elf_next_in_group (sec) != NULL
14760 && elf_group_name (elf_next_in_group (sec)) != NULL)
14761 key = elf_group_name (elf_next_in_group (sec));
14762 else
14763 {
14764 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 14765 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
14766 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14767 key++;
0c511000 14768 else
c77ec726
AM
14769 /* Must be a user linkonce section that doesn't follow gcc's
14770 naming convention. In this case we won't be matching
14771 single member groups. */
14772 key = name;
0c511000 14773 }
6d2cd210 14774
c77ec726 14775 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
14776
14777 for (l = already_linked_list->entry; l != NULL; l = l->next)
14778 {
c2370991 14779 /* We may have 2 different types of sections on the list: group
c77ec726
AM
14780 sections with a signature of <key> (<key> is some string),
14781 and linkonce sections named .gnu.linkonce.<type>.<key>.
14782 Match like sections. LTO plugin sections are an exception.
14783 They are always named .gnu.linkonce.t.<key> and match either
14784 type of section. */
14785 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14786 && ((flags & SEC_GROUP) != 0
14787 || strcmp (name, l->sec->name) == 0))
e8a83e93
MB
14788 || (l->sec->owner->flags & BFD_PLUGIN) != 0
14789 || (sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
14790 {
14791 /* The section has already been linked. See if we should
6d2cd210 14792 issue a warning. */
c77ec726
AM
14793 if (!_bfd_handle_already_linked (sec, l, info))
14794 return FALSE;
082b7297 14795
c77ec726 14796 if (flags & SEC_GROUP)
3d7f7666 14797 {
c77ec726
AM
14798 asection *first = elf_next_in_group (sec);
14799 asection *s = first;
3d7f7666 14800
c77ec726 14801 while (s != NULL)
3d7f7666 14802 {
c77ec726
AM
14803 s->output_section = bfd_abs_section_ptr;
14804 /* Record which group discards it. */
14805 s->kept_section = l->sec;
14806 s = elf_next_in_group (s);
14807 /* These lists are circular. */
14808 if (s == first)
14809 break;
3d7f7666
L
14810 }
14811 }
082b7297 14812
43e1669b 14813 return TRUE;
082b7297
L
14814 }
14815 }
14816
c77ec726
AM
14817 /* A single member comdat group section may be discarded by a
14818 linkonce section and vice versa. */
14819 if ((flags & SEC_GROUP) != 0)
3d7f7666 14820 {
c77ec726 14821 asection *first = elf_next_in_group (sec);
c2370991 14822
c77ec726
AM
14823 if (first != NULL && elf_next_in_group (first) == first)
14824 /* Check this single member group against linkonce sections. */
14825 for (l = already_linked_list->entry; l != NULL; l = l->next)
14826 if ((l->sec->flags & SEC_GROUP) == 0
14827 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14828 {
14829 first->output_section = bfd_abs_section_ptr;
14830 first->kept_section = l->sec;
14831 sec->output_section = bfd_abs_section_ptr;
14832 break;
14833 }
14834 }
14835 else
14836 /* Check this linkonce section against single member groups. */
14837 for (l = already_linked_list->entry; l != NULL; l = l->next)
14838 if (l->sec->flags & SEC_GROUP)
6d2cd210 14839 {
c77ec726 14840 asection *first = elf_next_in_group (l->sec);
6d2cd210 14841
c77ec726
AM
14842 if (first != NULL
14843 && elf_next_in_group (first) == first
14844 && bfd_elf_match_symbols_in_sections (first, sec, info))
14845 {
14846 sec->output_section = bfd_abs_section_ptr;
14847 sec->kept_section = first;
14848 break;
14849 }
6d2cd210 14850 }
0c511000 14851
c77ec726
AM
14852 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14853 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14854 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14855 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14856 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14857 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14858 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14859 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14860 The reverse order cannot happen as there is never a bfd with only the
14861 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14862 matter as here were are looking only for cross-bfd sections. */
14863
14864 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14865 for (l = already_linked_list->entry; l != NULL; l = l->next)
14866 if ((l->sec->flags & SEC_GROUP) == 0
14867 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14868 {
14869 if (abfd != l->sec->owner)
14870 sec->output_section = bfd_abs_section_ptr;
14871 break;
14872 }
80c29487 14873
082b7297 14874 /* This is the first section with this name. Record it. */
c77ec726 14875 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 14876 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 14877 return sec->output_section == bfd_abs_section_ptr;
082b7297 14878}
81e1b023 14879
a4d8e49b
L
14880bfd_boolean
14881_bfd_elf_common_definition (Elf_Internal_Sym *sym)
14882{
14883 return sym->st_shndx == SHN_COMMON;
14884}
14885
14886unsigned int
14887_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14888{
14889 return SHN_COMMON;
14890}
14891
14892asection *
14893_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14894{
14895 return bfd_com_section_ptr;
14896}
10455f89
HPN
14897
14898bfd_vma
14899_bfd_elf_default_got_elt_size (bfd *abfd,
14900 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14901 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14902 bfd *ibfd ATTRIBUTE_UNUSED,
14903 unsigned long symndx ATTRIBUTE_UNUSED)
14904{
14905 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14906 return bed->s->arch_size / 8;
14907}
83bac4b0
NC
14908
14909/* Routines to support the creation of dynamic relocs. */
14910
83bac4b0
NC
14911/* Returns the name of the dynamic reloc section associated with SEC. */
14912
14913static const char *
14914get_dynamic_reloc_section_name (bfd * abfd,
14915 asection * sec,
14916 bfd_boolean is_rela)
14917{
ddcf1fcf 14918 char *name;
fd361982 14919 const char *old_name = bfd_section_name (sec);
ddcf1fcf 14920 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 14921
ddcf1fcf 14922 if (old_name == NULL)
83bac4b0
NC
14923 return NULL;
14924
ddcf1fcf 14925 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 14926 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
14927
14928 return name;
14929}
14930
14931/* Returns the dynamic reloc section associated with SEC.
14932 If necessary compute the name of the dynamic reloc section based
14933 on SEC's name (looked up in ABFD's string table) and the setting
14934 of IS_RELA. */
14935
14936asection *
14937_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14938 asection * sec,
14939 bfd_boolean is_rela)
14940{
14941 asection * reloc_sec = elf_section_data (sec)->sreloc;
14942
14943 if (reloc_sec == NULL)
14944 {
14945 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14946
14947 if (name != NULL)
14948 {
3d4d4302 14949 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
14950
14951 if (reloc_sec != NULL)
14952 elf_section_data (sec)->sreloc = reloc_sec;
14953 }
14954 }
14955
14956 return reloc_sec;
14957}
14958
14959/* Returns the dynamic reloc section associated with SEC. If the
14960 section does not exist it is created and attached to the DYNOBJ
14961 bfd and stored in the SRELOC field of SEC's elf_section_data
14962 structure.
f8076f98 14963
83bac4b0
NC
14964 ALIGNMENT is the alignment for the newly created section and
14965 IS_RELA defines whether the name should be .rela.<SEC's name>
14966 or .rel.<SEC's name>. The section name is looked up in the
14967 string table associated with ABFD. */
14968
14969asection *
ca4be51c
AM
14970_bfd_elf_make_dynamic_reloc_section (asection *sec,
14971 bfd *dynobj,
14972 unsigned int alignment,
14973 bfd *abfd,
14974 bfd_boolean is_rela)
83bac4b0
NC
14975{
14976 asection * reloc_sec = elf_section_data (sec)->sreloc;
14977
14978 if (reloc_sec == NULL)
14979 {
14980 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14981
14982 if (name == NULL)
14983 return NULL;
14984
3d4d4302 14985 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
14986
14987 if (reloc_sec == NULL)
14988 {
3d4d4302
AM
14989 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14990 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
14991 if ((sec->flags & SEC_ALLOC) != 0)
14992 flags |= SEC_ALLOC | SEC_LOAD;
14993
3d4d4302 14994 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
14995 if (reloc_sec != NULL)
14996 {
8877b5e5
AM
14997 /* _bfd_elf_get_sec_type_attr chooses a section type by
14998 name. Override as it may be wrong, eg. for a user
14999 section named "auto" we'll get ".relauto" which is
15000 seen to be a .rela section. */
15001 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
fd361982 15002 if (!bfd_set_section_alignment (reloc_sec, alignment))
83bac4b0
NC
15003 reloc_sec = NULL;
15004 }
15005 }
15006
15007 elf_section_data (sec)->sreloc = reloc_sec;
15008 }
15009
15010 return reloc_sec;
15011}
1338dd10 15012
bffebb6b
AM
15013/* Copy the ELF symbol type and other attributes for a linker script
15014 assignment from HSRC to HDEST. Generally this should be treated as
15015 if we found a strong non-dynamic definition for HDEST (except that
15016 ld ignores multiple definition errors). */
1338dd10 15017void
bffebb6b
AM
15018_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
15019 struct bfd_link_hash_entry *hdest,
15020 struct bfd_link_hash_entry *hsrc)
1338dd10 15021{
bffebb6b
AM
15022 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
15023 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
15024 Elf_Internal_Sym isym;
1338dd10
PB
15025
15026 ehdest->type = ehsrc->type;
35fc36a8 15027 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
15028
15029 isym.st_other = ehsrc->other;
b8417128 15030 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
1338dd10 15031}
351f65ca
L
15032
15033/* Append a RELA relocation REL to section S in BFD. */
15034
15035void
15036elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15037{
15038 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15039 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
15040 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
15041 bed->s->swap_reloca_out (abfd, rel, loc);
15042}
15043
15044/* Append a REL relocation REL to section S in BFD. */
15045
15046void
15047elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15048{
15049 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15050 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
15051 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 15052 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 15053}
7dba9362
AM
15054
15055/* Define __start, __stop, .startof. or .sizeof. symbol. */
15056
15057struct bfd_link_hash_entry *
15058bfd_elf_define_start_stop (struct bfd_link_info *info,
15059 const char *symbol, asection *sec)
15060{
487b6440 15061 struct elf_link_hash_entry *h;
7dba9362 15062
487b6440
AM
15063 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
15064 FALSE, FALSE, TRUE);
e1b5d517 15065 /* NB: Common symbols will be turned into definition later. */
487b6440
AM
15066 if (h != NULL
15067 && (h->root.type == bfd_link_hash_undefined
15068 || h->root.type == bfd_link_hash_undefweak
e1b5d517
L
15069 || ((h->ref_regular || h->def_dynamic)
15070 && !h->def_regular
15071 && h->root.type != bfd_link_hash_common)))
7dba9362 15072 {
bf3077a6 15073 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
e1b5d517 15074 h->verinfo.verdef = NULL;
487b6440
AM
15075 h->root.type = bfd_link_hash_defined;
15076 h->root.u.def.section = sec;
15077 h->root.u.def.value = 0;
15078 h->def_regular = 1;
15079 h->def_dynamic = 0;
15080 h->start_stop = 1;
15081 h->u2.start_stop_section = sec;
15082 if (symbol[0] == '.')
15083 {
15084 /* .startof. and .sizeof. symbols are local. */
559192d8
AM
15085 const struct elf_backend_data *bed;
15086 bed = get_elf_backend_data (info->output_bfd);
15087 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
487b6440 15088 }
36b8fda5
AM
15089 else
15090 {
15091 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
cae64165
RM
15092 h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15093 | info->start_stop_visibility);
bf3077a6 15094 if (was_dynamic)
36b8fda5
AM
15095 bfd_elf_link_record_dynamic_symbol (info, h);
15096 }
487b6440 15097 return &h->root;
7dba9362 15098 }
487b6440 15099 return NULL;
7dba9362 15100}
5dbc8b37
L
15101
15102/* Find dynamic relocs for H that apply to read-only sections. */
15103
15104asection *
15105_bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15106{
15107 struct elf_dyn_relocs *p;
15108
15109 for (p = h->dyn_relocs; p != NULL; p = p->next)
15110 {
15111 asection *s = p->sec->output_section;
15112
15113 if (s != NULL && (s->flags & SEC_READONLY) != 0)
15114 return p->sec;
15115 }
15116 return NULL;
15117}
d49e5065
L
15118
15119/* Set DF_TEXTREL if we find any dynamic relocs that apply to
15120 read-only sections. */
15121
15122bfd_boolean
15123_bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15124{
15125 asection *sec;
15126
15127 if (h->root.type == bfd_link_hash_indirect)
15128 return TRUE;
15129
15130 sec = _bfd_elf_readonly_dynrelocs (h);
15131 if (sec != NULL)
15132 {
15133 struct bfd_link_info *info = (struct bfd_link_info *) inf;
15134
15135 info->flags |= DF_TEXTREL;
15136 /* xgettext:c-format */
15137 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15138 "in read-only section `%pA'\n"),
15139 sec->owner, h->root.root.string, sec);
15140
15141 if (bfd_link_textrel_check (info))
15142 /* xgettext:c-format */
15143 info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15144 "in read-only section `%pA'\n"),
15145 sec->owner, h->root.root.string, sec);
15146
15147 /* Not an error, just cut short the traversal. */
15148 return FALSE;
15149 }
15150 return TRUE;
15151}
3084d7a2
L
15152
15153/* Add dynamic tags. */
15154
15155bfd_boolean
15156_bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
15157 bfd_boolean need_dynamic_reloc)
15158{
15159 struct elf_link_hash_table *htab = elf_hash_table (info);
15160
15161 if (htab->dynamic_sections_created)
15162 {
15163 /* Add some entries to the .dynamic section. We fill in the
15164 values later, in finish_dynamic_sections, but we must add
15165 the entries now so that we get the correct size for the
15166 .dynamic section. The DT_DEBUG entry is filled in by the
15167 dynamic linker and used by the debugger. */
15168#define add_dynamic_entry(TAG, VAL) \
15169 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15170
15171 const struct elf_backend_data *bed
15172 = get_elf_backend_data (output_bfd);
15173
15174 if (bfd_link_executable (info))
15175 {
15176 if (!add_dynamic_entry (DT_DEBUG, 0))
15177 return FALSE;
15178 }
15179
15180 if (htab->dt_pltgot_required || htab->splt->size != 0)
15181 {
15182 /* DT_PLTGOT is used by prelink even if there is no PLT
15183 relocation. */
15184 if (!add_dynamic_entry (DT_PLTGOT, 0))
15185 return FALSE;
15186 }
15187
15188 if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15189 {
15190 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15191 || !add_dynamic_entry (DT_PLTREL,
15192 (bed->rela_plts_and_copies_p
15193 ? DT_RELA : DT_REL))
15194 || !add_dynamic_entry (DT_JMPREL, 0))
15195 return FALSE;
15196 }
15197
15198 if (htab->tlsdesc_plt
15199 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15200 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
15201 return FALSE;
15202
15203 if (need_dynamic_reloc)
15204 {
15205 if (bed->rela_plts_and_copies_p)
15206 {
15207 if (!add_dynamic_entry (DT_RELA, 0)
15208 || !add_dynamic_entry (DT_RELASZ, 0)
15209 || !add_dynamic_entry (DT_RELAENT,
15210 bed->s->sizeof_rela))
15211 return FALSE;
15212 }
15213 else
15214 {
15215 if (!add_dynamic_entry (DT_REL, 0)
15216 || !add_dynamic_entry (DT_RELSZ, 0)
15217 || !add_dynamic_entry (DT_RELENT,
15218 bed->s->sizeof_rel))
15219 return FALSE;
15220 }
15221
15222 /* If any dynamic relocs apply to a read-only section,
15223 then we need a DT_TEXTREL entry. */
15224 if ((info->flags & DF_TEXTREL) == 0)
15225 elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15226 info);
15227
15228 if ((info->flags & DF_TEXTREL) != 0)
15229 {
15230 if (htab->ifunc_resolvers)
15231 info->callbacks->einfo
15232 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15233 "may result in a segfault at runtime; recompile with %s\n"),
15234 bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15235
15236 if (!add_dynamic_entry (DT_TEXTREL, 0))
15237 return FALSE;
15238 }
15239 }
15240 }
15241#undef add_dynamic_entry
15242
15243 return TRUE;
15244}