Define a new DT_GNU_FLAGS_1 dynamic section for ld, readelf et al
[deliverable/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,
5160d0f3 1009 unsigned int st_other, 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 1016 if (bed->elf_backend_merge_symbol_attribute)
5160d0f3 1017 (*bed->elf_backend_merge_symbol_attribute) (h, st_other, definition,
54ac0771
L
1018 dynamic);
1019
cd3416da 1020 if (!dynamic)
54ac0771 1021 {
5160d0f3 1022 unsigned symvis = ELF_ST_VISIBILITY (st_other);
cd3416da 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 1030 else if (definition
5160d0f3 1031 && ELF_ST_VISIBILITY (st_other) != STV_DEFAULT
b8417128 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. */
5160d0f3 1704 elf_merge_st_other (abfd, h, sym->st_other, 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
5160d0f3
AM
2031 /* If we first saw a reference to SHORTNAME with non-default
2032 visibility, merge that visibility to the @@VER symbol. */
2033 elf_merge_st_other (abfd, ht, hi->other, sec, TRUE, dynamic);
2034
68c88cd4
AM
2035 /* A reference to the SHORTNAME symbol from a dynamic library
2036 will be satisfied by the versioned symbol at runtime. In
2037 effect, we have a reference to the versioned symbol. */
2038 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2039 hi->dynamic_def |= ht->dynamic_def;
2040
45d6a902
AM
2041 /* See if the new flags lead us to realize that the symbol must
2042 be dynamic. */
2043 if (! *dynsym)
2044 {
2045 if (! dynamic)
2046 {
0e1862bb 2047 if (! bfd_link_executable (info)
90c984fc 2048 || hi->def_dynamic
f5385ebf 2049 || hi->ref_dynamic)
45d6a902
AM
2050 *dynsym = TRUE;
2051 }
2052 else
2053 {
f5385ebf 2054 if (hi->ref_regular)
45d6a902
AM
2055 *dynsym = TRUE;
2056 }
2057 }
2058 }
2059
2060 /* We also need to define an indirection from the nondefault version
2061 of the symbol. */
2062
dc1e8a47 2063 nondefault:
45d6a902 2064 len = strlen (name);
a50b1753 2065 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
45d6a902
AM
2066 if (shortname == NULL)
2067 return FALSE;
2068 memcpy (shortname, name, shortlen);
2069 memcpy (shortname + shortlen, p + 1, len - shortlen);
2070
2071 /* Once again, merge with any existing symbol. */
2072 type_change_ok = FALSE;
2073 size_change_ok = FALSE;
ffd65175
AM
2074 tmp_sec = sec;
2075 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
115c6d5c 2076 &hi, poldbfd, NULL, NULL, &skip, &override,
6e33951e 2077 &type_change_ok, &size_change_ok, &matched))
45d6a902
AM
2078 return FALSE;
2079
2080 if (skip)
726d7d1e
AM
2081 {
2082 if (!dynamic
2083 && h->root.type == bfd_link_hash_defweak
2084 && hi->root.type == bfd_link_hash_defined)
2085 {
2086 /* We are handling a weak sym@@ver and attempting to define
2087 a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
2088 new weak sym@ver because there is already a strong sym@ver.
2089 However, sym@ver and sym@@ver are really the same symbol.
2090 The existing strong sym@ver ought to override sym@@ver. */
2091 h->root.type = bfd_link_hash_defined;
2092 h->root.u.def.section = hi->root.u.def.section;
2093 h->root.u.def.value = hi->root.u.def.value;
2094 hi->root.type = bfd_link_hash_indirect;
2095 hi->root.u.i.link = &h->root;
2096 }
2097 else
2098 return TRUE;
2099 }
2100 else if (override)
45d6a902
AM
2101 {
2102 /* Here SHORTNAME is a versioned name, so we don't expect to see
2103 the type of override we do in the case above unless it is
4cc11e76 2104 overridden by a versioned definition. */
45d6a902
AM
2105 if (hi->root.type != bfd_link_hash_defined
2106 && hi->root.type != bfd_link_hash_defweak)
4eca0228 2107 _bfd_error_handler
695344c0 2108 /* xgettext:c-format */
871b3ab2 2109 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
d003868e 2110 abfd, shortname);
726d7d1e 2111 return TRUE;
45d6a902
AM
2112 }
2113 else
2114 {
2115 bh = &hi->root;
2116 if (! (_bfd_generic_link_add_one_symbol
2117 (info, abfd, shortname, BSF_INDIRECT,
268b6b39 2118 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
45d6a902
AM
2119 return FALSE;
2120 hi = (struct elf_link_hash_entry *) bh;
726d7d1e 2121 }
45d6a902 2122
726d7d1e
AM
2123 /* If there is a duplicate definition somewhere, then HI may not
2124 point to an indirect symbol. We will have reported an error
2125 to the user in that case. */
2126 if (hi->root.type == bfd_link_hash_indirect)
2127 {
2128 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2129 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2130 hi->dynamic_def |= h->dynamic_def;
45d6a902 2131
726d7d1e
AM
2132 /* If we first saw a reference to @VER symbol with
2133 non-default visibility, merge that visibility to the
2134 @@VER symbol. */
2135 elf_merge_st_other (abfd, h, hi->other, sec, TRUE, dynamic);
2136
2137 /* See if the new flags lead us to realize that the symbol
2138 must be dynamic. */
2139 if (! *dynsym)
45d6a902 2140 {
726d7d1e 2141 if (! dynamic)
45d6a902 2142 {
726d7d1e
AM
2143 if (! bfd_link_executable (info)
2144 || hi->ref_dynamic)
2145 *dynsym = TRUE;
2146 }
2147 else
2148 {
2149 if (hi->ref_regular)
2150 *dynsym = TRUE;
45d6a902
AM
2151 }
2152 }
2153 }
2154
2155 return TRUE;
2156}
2157\f
2158/* This routine is used to export all defined symbols into the dynamic
2159 symbol table. It is called via elf_link_hash_traverse. */
2160
28caa186 2161static bfd_boolean
268b6b39 2162_bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2163{
a50b1753 2164 struct elf_info_failed *eif = (struct elf_info_failed *) data;
45d6a902
AM
2165
2166 /* Ignore indirect symbols. These are added by the versioning code. */
2167 if (h->root.type == bfd_link_hash_indirect)
2168 return TRUE;
2169
7686d77d
AM
2170 /* Ignore this if we won't export it. */
2171 if (!eif->info->export_dynamic && !h->dynamic)
2172 return TRUE;
45d6a902
AM
2173
2174 if (h->dynindx == -1
fd91d419
L
2175 && (h->def_regular || h->ref_regular)
2176 && ! bfd_hide_sym_by_version (eif->info->version_info,
2177 h->root.root.string))
45d6a902 2178 {
fd91d419 2179 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902 2180 {
fd91d419
L
2181 eif->failed = TRUE;
2182 return FALSE;
45d6a902
AM
2183 }
2184 }
2185
2186 return TRUE;
2187}
2188\f
2189/* Look through the symbols which are defined in other shared
2190 libraries and referenced here. Update the list of version
2191 dependencies. This will be put into the .gnu.version_r section.
2192 This function is called via elf_link_hash_traverse. */
2193
28caa186 2194static bfd_boolean
268b6b39
AM
2195_bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2196 void *data)
45d6a902 2197{
a50b1753 2198 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
45d6a902
AM
2199 Elf_Internal_Verneed *t;
2200 Elf_Internal_Vernaux *a;
986f0783 2201 size_t amt;
45d6a902 2202
45d6a902
AM
2203 /* We only care about symbols defined in shared objects with version
2204 information. */
f5385ebf
AM
2205 if (!h->def_dynamic
2206 || h->def_regular
45d6a902 2207 || h->dynindx == -1
7b20f099
AM
2208 || h->verinfo.verdef == NULL
2209 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2210 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
45d6a902
AM
2211 return TRUE;
2212
2213 /* See if we already know about this version. */
28caa186
AM
2214 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2215 t != NULL;
2216 t = t->vn_nextref)
45d6a902
AM
2217 {
2218 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2219 continue;
2220
2221 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2222 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2223 return TRUE;
2224
2225 break;
2226 }
2227
2228 /* This is a new version. Add it to tree we are building. */
2229
2230 if (t == NULL)
2231 {
2232 amt = sizeof *t;
a50b1753 2233 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
45d6a902
AM
2234 if (t == NULL)
2235 {
2236 rinfo->failed = TRUE;
2237 return FALSE;
2238 }
2239
2240 t->vn_bfd = h->verinfo.verdef->vd_bfd;
28caa186
AM
2241 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2242 elf_tdata (rinfo->info->output_bfd)->verref = t;
45d6a902
AM
2243 }
2244
2245 amt = sizeof *a;
a50b1753 2246 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
14b1c01e
AM
2247 if (a == NULL)
2248 {
2249 rinfo->failed = TRUE;
2250 return FALSE;
2251 }
45d6a902
AM
2252
2253 /* Note that we are copying a string pointer here, and testing it
2254 above. If bfd_elf_string_from_elf_section is ever changed to
2255 discard the string data when low in memory, this will have to be
2256 fixed. */
2257 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2258
2259 a->vna_flags = h->verinfo.verdef->vd_flags;
2260 a->vna_nextptr = t->vn_auxptr;
2261
2262 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2263 ++rinfo->vers;
2264
2265 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2266
2267 t->vn_auxptr = a;
2268
2269 return TRUE;
2270}
2271
099bb8fb
L
2272/* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2273 hidden. Set *T_P to NULL if there is no match. */
2274
2275static bfd_boolean
2276_bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2277 struct elf_link_hash_entry *h,
2278 const char *version_p,
2279 struct bfd_elf_version_tree **t_p,
2280 bfd_boolean *hide)
2281{
2282 struct bfd_elf_version_tree *t;
2283
2284 /* Look for the version. If we find it, it is no longer weak. */
2285 for (t = info->version_info; t != NULL; t = t->next)
2286 {
2287 if (strcmp (t->name, version_p) == 0)
2288 {
2289 size_t len;
2290 char *alc;
2291 struct bfd_elf_version_expr *d;
2292
2293 len = version_p - h->root.root.string;
2294 alc = (char *) bfd_malloc (len);
2295 if (alc == NULL)
2296 return FALSE;
2297 memcpy (alc, h->root.root.string, len - 1);
2298 alc[len - 1] = '\0';
2299 if (alc[len - 2] == ELF_VER_CHR)
2300 alc[len - 2] = '\0';
2301
2302 h->verinfo.vertree = t;
2303 t->used = TRUE;
2304 d = NULL;
2305
2306 if (t->globals.list != NULL)
2307 d = (*t->match) (&t->globals, NULL, alc);
2308
2309 /* See if there is anything to force this symbol to
2310 local scope. */
2311 if (d == NULL && t->locals.list != NULL)
2312 {
2313 d = (*t->match) (&t->locals, NULL, alc);
2314 if (d != NULL
2315 && h->dynindx != -1
2316 && ! info->export_dynamic)
2317 *hide = TRUE;
2318 }
2319
2320 free (alc);
2321 break;
2322 }
2323 }
2324
2325 *t_p = t;
2326
2327 return TRUE;
2328}
2329
2330/* Return TRUE if the symbol H is hidden by version script. */
2331
2332bfd_boolean
2333_bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2334 struct elf_link_hash_entry *h)
2335{
2336 const char *p;
2337 bfd_boolean hide = FALSE;
2338 const struct elf_backend_data *bed
2339 = get_elf_backend_data (info->output_bfd);
2340
2341 /* Version script only hides symbols defined in regular objects. */
2342 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2343 return TRUE;
2344
2345 p = strchr (h->root.root.string, ELF_VER_CHR);
2346 if (p != NULL && h->verinfo.vertree == NULL)
2347 {
2348 struct bfd_elf_version_tree *t;
2349
2350 ++p;
2351 if (*p == ELF_VER_CHR)
2352 ++p;
2353
2354 if (*p != '\0'
2355 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2356 && hide)
2357 {
2358 if (hide)
2359 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2360 return TRUE;
2361 }
2362 }
2363
2364 /* If we don't have a version for this symbol, see if we can find
2365 something. */
2366 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2367 {
2368 h->verinfo.vertree
2369 = bfd_find_version_for_sym (info->version_info,
2370 h->root.root.string, &hide);
2371 if (h->verinfo.vertree != NULL && hide)
2372 {
2373 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2374 return TRUE;
2375 }
2376 }
2377
2378 return FALSE;
2379}
2380
45d6a902
AM
2381/* Figure out appropriate versions for all the symbols. We may not
2382 have the version number script until we have read all of the input
2383 files, so until that point we don't know which symbols should be
2384 local. This function is called via elf_link_hash_traverse. */
2385
28caa186 2386static bfd_boolean
268b6b39 2387_bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
45d6a902 2388{
28caa186 2389 struct elf_info_failed *sinfo;
45d6a902 2390 struct bfd_link_info *info;
9c5bfbb7 2391 const struct elf_backend_data *bed;
45d6a902
AM
2392 struct elf_info_failed eif;
2393 char *p;
099bb8fb 2394 bfd_boolean hide;
45d6a902 2395
a50b1753 2396 sinfo = (struct elf_info_failed *) data;
45d6a902
AM
2397 info = sinfo->info;
2398
45d6a902
AM
2399 /* Fix the symbol flags. */
2400 eif.failed = FALSE;
2401 eif.info = info;
2402 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2403 {
2404 if (eif.failed)
2405 sinfo->failed = TRUE;
2406 return FALSE;
2407 }
2408
0a640d71
L
2409 bed = get_elf_backend_data (info->output_bfd);
2410
45d6a902
AM
2411 /* We only need version numbers for symbols defined in regular
2412 objects. */
5fa370e4 2413 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
0a640d71
L
2414 {
2415 /* Hide symbols defined in discarded input sections. */
2416 if ((h->root.type == bfd_link_hash_defined
2417 || h->root.type == bfd_link_hash_defweak)
2418 && discarded_section (h->root.u.def.section))
2419 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2420 return TRUE;
2421 }
45d6a902 2422
099bb8fb 2423 hide = FALSE;
45d6a902
AM
2424 p = strchr (h->root.root.string, ELF_VER_CHR);
2425 if (p != NULL && h->verinfo.vertree == NULL)
2426 {
2427 struct bfd_elf_version_tree *t;
45d6a902 2428
45d6a902
AM
2429 ++p;
2430 if (*p == ELF_VER_CHR)
6e33951e 2431 ++p;
45d6a902
AM
2432
2433 /* If there is no version string, we can just return out. */
2434 if (*p == '\0')
6e33951e 2435 return TRUE;
45d6a902 2436
099bb8fb 2437 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
45d6a902 2438 {
099bb8fb
L
2439 sinfo->failed = TRUE;
2440 return FALSE;
45d6a902
AM
2441 }
2442
099bb8fb
L
2443 if (hide)
2444 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2445
45d6a902
AM
2446 /* If we are building an application, we need to create a
2447 version node for this version. */
0e1862bb 2448 if (t == NULL && bfd_link_executable (info))
45d6a902
AM
2449 {
2450 struct bfd_elf_version_tree **pp;
2451 int version_index;
2452
2453 /* If we aren't going to export this symbol, we don't need
2454 to worry about it. */
2455 if (h->dynindx == -1)
2456 return TRUE;
2457
ef53be89
AM
2458 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2459 sizeof *t);
45d6a902
AM
2460 if (t == NULL)
2461 {
2462 sinfo->failed = TRUE;
2463 return FALSE;
2464 }
2465
45d6a902 2466 t->name = p;
45d6a902
AM
2467 t->name_indx = (unsigned int) -1;
2468 t->used = TRUE;
2469
2470 version_index = 1;
2471 /* Don't count anonymous version tag. */
fd91d419
L
2472 if (sinfo->info->version_info != NULL
2473 && sinfo->info->version_info->vernum == 0)
45d6a902 2474 version_index = 0;
fd91d419
L
2475 for (pp = &sinfo->info->version_info;
2476 *pp != NULL;
2477 pp = &(*pp)->next)
45d6a902
AM
2478 ++version_index;
2479 t->vernum = version_index;
2480
2481 *pp = t;
2482
2483 h->verinfo.vertree = t;
2484 }
2485 else if (t == NULL)
2486 {
2487 /* We could not find the version for a symbol when
2488 generating a shared archive. Return an error. */
4eca0228 2489 _bfd_error_handler
695344c0 2490 /* xgettext:c-format */
871b3ab2 2491 (_("%pB: version node not found for symbol %s"),
28caa186 2492 info->output_bfd, h->root.root.string);
45d6a902
AM
2493 bfd_set_error (bfd_error_bad_value);
2494 sinfo->failed = TRUE;
2495 return FALSE;
2496 }
45d6a902
AM
2497 }
2498
2499 /* If we don't have a version for this symbol, see if we can find
2500 something. */
099bb8fb
L
2501 if (!hide
2502 && h->verinfo.vertree == NULL
2503 && sinfo->info->version_info != NULL)
45d6a902 2504 {
fd91d419
L
2505 h->verinfo.vertree
2506 = bfd_find_version_for_sym (sinfo->info->version_info,
2507 h->root.root.string, &hide);
1e8fa21e
AM
2508 if (h->verinfo.vertree != NULL && hide)
2509 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
45d6a902
AM
2510 }
2511
2512 return TRUE;
2513}
2514\f
45d6a902
AM
2515/* Read and swap the relocs from the section indicated by SHDR. This
2516 may be either a REL or a RELA section. The relocations are
2517 translated into RELA relocations and stored in INTERNAL_RELOCS,
2518 which should have already been allocated to contain enough space.
2519 The EXTERNAL_RELOCS are a buffer where the external form of the
2520 relocations should be stored.
2521
2522 Returns FALSE if something goes wrong. */
2523
2524static bfd_boolean
268b6b39 2525elf_link_read_relocs_from_section (bfd *abfd,
243ef1e0 2526 asection *sec,
268b6b39
AM
2527 Elf_Internal_Shdr *shdr,
2528 void *external_relocs,
2529 Elf_Internal_Rela *internal_relocs)
45d6a902 2530{
9c5bfbb7 2531 const struct elf_backend_data *bed;
268b6b39 2532 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
45d6a902
AM
2533 const bfd_byte *erela;
2534 const bfd_byte *erelaend;
2535 Elf_Internal_Rela *irela;
243ef1e0
L
2536 Elf_Internal_Shdr *symtab_hdr;
2537 size_t nsyms;
45d6a902 2538
45d6a902
AM
2539 /* Position ourselves at the start of the section. */
2540 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2541 return FALSE;
2542
2543 /* Read the relocations. */
2544 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2545 return FALSE;
2546
243ef1e0 2547 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
ce98a316 2548 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
243ef1e0 2549
45d6a902
AM
2550 bed = get_elf_backend_data (abfd);
2551
2552 /* Convert the external relocations to the internal format. */
2553 if (shdr->sh_entsize == bed->s->sizeof_rel)
2554 swap_in = bed->s->swap_reloc_in;
2555 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2556 swap_in = bed->s->swap_reloca_in;
2557 else
2558 {
2559 bfd_set_error (bfd_error_wrong_format);
2560 return FALSE;
2561 }
2562
a50b1753 2563 erela = (const bfd_byte *) external_relocs;
f55b1e32
AM
2564 /* Setting erelaend like this and comparing with <= handles case of
2565 a fuzzed object with sh_size not a multiple of sh_entsize. */
2566 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
45d6a902 2567 irela = internal_relocs;
f55b1e32 2568 while (erela <= erelaend)
45d6a902 2569 {
243ef1e0
L
2570 bfd_vma r_symndx;
2571
45d6a902 2572 (*swap_in) (abfd, erela, irela);
243ef1e0
L
2573 r_symndx = ELF32_R_SYM (irela->r_info);
2574 if (bed->s->arch_size == 64)
2575 r_symndx >>= 24;
ce98a316
NC
2576 if (nsyms > 0)
2577 {
2578 if ((size_t) r_symndx >= nsyms)
2579 {
4eca0228 2580 _bfd_error_handler
695344c0 2581 /* xgettext:c-format */
2dcf00ce
AM
2582 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2583 " for offset %#" PRIx64 " in section `%pA'"),
2584 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2585 (uint64_t) irela->r_offset, sec);
ce98a316
NC
2586 bfd_set_error (bfd_error_bad_value);
2587 return FALSE;
2588 }
2589 }
cf35638d 2590 else if (r_symndx != STN_UNDEF)
243ef1e0 2591 {
4eca0228 2592 _bfd_error_handler
695344c0 2593 /* xgettext:c-format */
2dcf00ce
AM
2594 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2595 " for offset %#" PRIx64 " in section `%pA'"
ce98a316 2596 " when the object file has no symbol table"),
2dcf00ce
AM
2597 abfd, (uint64_t) r_symndx,
2598 (uint64_t) irela->r_offset, sec);
243ef1e0
L
2599 bfd_set_error (bfd_error_bad_value);
2600 return FALSE;
2601 }
45d6a902
AM
2602 irela += bed->s->int_rels_per_ext_rel;
2603 erela += shdr->sh_entsize;
2604 }
2605
2606 return TRUE;
2607}
2608
2609/* Read and swap the relocs for a section O. They may have been
2610 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2611 not NULL, they are used as buffers to read into. They are known to
2612 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2613 the return value is allocated using either malloc or bfd_alloc,
2614 according to the KEEP_MEMORY argument. If O has two relocation
2615 sections (both REL and RELA relocations), then the REL_HDR
2616 relocations will appear first in INTERNAL_RELOCS, followed by the
d4730f92 2617 RELA_HDR relocations. */
45d6a902
AM
2618
2619Elf_Internal_Rela *
268b6b39
AM
2620_bfd_elf_link_read_relocs (bfd *abfd,
2621 asection *o,
2622 void *external_relocs,
2623 Elf_Internal_Rela *internal_relocs,
2624 bfd_boolean keep_memory)
45d6a902 2625{
268b6b39 2626 void *alloc1 = NULL;
45d6a902 2627 Elf_Internal_Rela *alloc2 = NULL;
9c5bfbb7 2628 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92
BS
2629 struct bfd_elf_section_data *esdo = elf_section_data (o);
2630 Elf_Internal_Rela *internal_rela_relocs;
45d6a902 2631
d4730f92
BS
2632 if (esdo->relocs != NULL)
2633 return esdo->relocs;
45d6a902
AM
2634
2635 if (o->reloc_count == 0)
2636 return NULL;
2637
45d6a902
AM
2638 if (internal_relocs == NULL)
2639 {
2640 bfd_size_type size;
2641
056bafd4 2642 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
45d6a902 2643 if (keep_memory)
a50b1753 2644 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
45d6a902 2645 else
a50b1753 2646 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
45d6a902
AM
2647 if (internal_relocs == NULL)
2648 goto error_return;
2649 }
2650
2651 if (external_relocs == NULL)
2652 {
d4730f92
BS
2653 bfd_size_type size = 0;
2654
2655 if (esdo->rel.hdr)
2656 size += esdo->rel.hdr->sh_size;
2657 if (esdo->rela.hdr)
2658 size += esdo->rela.hdr->sh_size;
45d6a902 2659
268b6b39 2660 alloc1 = bfd_malloc (size);
45d6a902
AM
2661 if (alloc1 == NULL)
2662 goto error_return;
2663 external_relocs = alloc1;
2664 }
2665
d4730f92
BS
2666 internal_rela_relocs = internal_relocs;
2667 if (esdo->rel.hdr)
2668 {
2669 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2670 external_relocs,
2671 internal_relocs))
2672 goto error_return;
2673 external_relocs = (((bfd_byte *) external_relocs)
2674 + esdo->rel.hdr->sh_size);
2675 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2676 * bed->s->int_rels_per_ext_rel);
2677 }
2678
2679 if (esdo->rela.hdr
2680 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2681 external_relocs,
2682 internal_rela_relocs)))
45d6a902
AM
2683 goto error_return;
2684
2685 /* Cache the results for next time, if we can. */
2686 if (keep_memory)
d4730f92 2687 esdo->relocs = internal_relocs;
45d6a902 2688
c9594989 2689 free (alloc1);
45d6a902
AM
2690
2691 /* Don't free alloc2, since if it was allocated we are passing it
2692 back (under the name of internal_relocs). */
2693
2694 return internal_relocs;
2695
2696 error_return:
c9594989 2697 free (alloc1);
45d6a902 2698 if (alloc2 != NULL)
4dd07732
AM
2699 {
2700 if (keep_memory)
2701 bfd_release (abfd, alloc2);
2702 else
2703 free (alloc2);
2704 }
45d6a902
AM
2705 return NULL;
2706}
2707
2708/* Compute the size of, and allocate space for, REL_HDR which is the
2709 section header for a section containing relocations for O. */
2710
28caa186 2711static bfd_boolean
9eaff861
AO
2712_bfd_elf_link_size_reloc_section (bfd *abfd,
2713 struct bfd_elf_section_reloc_data *reldata)
45d6a902 2714{
9eaff861 2715 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
45d6a902
AM
2716
2717 /* That allows us to calculate the size of the section. */
9eaff861 2718 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
45d6a902
AM
2719
2720 /* The contents field must last into write_object_contents, so we
2721 allocate it with bfd_alloc rather than malloc. Also since we
2722 cannot be sure that the contents will actually be filled in,
2723 we zero the allocated space. */
a50b1753 2724 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
45d6a902
AM
2725 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2726 return FALSE;
2727
d4730f92 2728 if (reldata->hashes == NULL && reldata->count)
45d6a902
AM
2729 {
2730 struct elf_link_hash_entry **p;
2731
ca4be51c
AM
2732 p = ((struct elf_link_hash_entry **)
2733 bfd_zmalloc (reldata->count * sizeof (*p)));
45d6a902
AM
2734 if (p == NULL)
2735 return FALSE;
2736
d4730f92 2737 reldata->hashes = p;
45d6a902
AM
2738 }
2739
2740 return TRUE;
2741}
2742
2743/* Copy the relocations indicated by the INTERNAL_RELOCS (which
2744 originated from the section given by INPUT_REL_HDR) to the
2745 OUTPUT_BFD. */
2746
2747bfd_boolean
268b6b39
AM
2748_bfd_elf_link_output_relocs (bfd *output_bfd,
2749 asection *input_section,
2750 Elf_Internal_Shdr *input_rel_hdr,
eac338cf
PB
2751 Elf_Internal_Rela *internal_relocs,
2752 struct elf_link_hash_entry **rel_hash
2753 ATTRIBUTE_UNUSED)
45d6a902
AM
2754{
2755 Elf_Internal_Rela *irela;
2756 Elf_Internal_Rela *irelaend;
2757 bfd_byte *erel;
d4730f92 2758 struct bfd_elf_section_reloc_data *output_reldata;
45d6a902 2759 asection *output_section;
9c5bfbb7 2760 const struct elf_backend_data *bed;
268b6b39 2761 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
d4730f92 2762 struct bfd_elf_section_data *esdo;
45d6a902
AM
2763
2764 output_section = input_section->output_section;
45d6a902 2765
d4730f92
BS
2766 bed = get_elf_backend_data (output_bfd);
2767 esdo = elf_section_data (output_section);
2768 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2769 {
d4730f92
BS
2770 output_reldata = &esdo->rel;
2771 swap_out = bed->s->swap_reloc_out;
45d6a902 2772 }
d4730f92
BS
2773 else if (esdo->rela.hdr
2774 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
45d6a902 2775 {
d4730f92
BS
2776 output_reldata = &esdo->rela;
2777 swap_out = bed->s->swap_reloca_out;
45d6a902
AM
2778 }
2779 else
2780 {
4eca0228 2781 _bfd_error_handler
695344c0 2782 /* xgettext:c-format */
871b3ab2 2783 (_("%pB: relocation size mismatch in %pB section %pA"),
d003868e 2784 output_bfd, input_section->owner, input_section);
297d8443 2785 bfd_set_error (bfd_error_wrong_format);
45d6a902
AM
2786 return FALSE;
2787 }
2788
d4730f92
BS
2789 erel = output_reldata->hdr->contents;
2790 erel += output_reldata->count * input_rel_hdr->sh_entsize;
45d6a902
AM
2791 irela = internal_relocs;
2792 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2793 * bed->s->int_rels_per_ext_rel);
2794 while (irela < irelaend)
2795 {
2796 (*swap_out) (output_bfd, irela, erel);
2797 irela += bed->s->int_rels_per_ext_rel;
2798 erel += input_rel_hdr->sh_entsize;
2799 }
2800
2801 /* Bump the counter, so that we know where to add the next set of
2802 relocations. */
d4730f92 2803 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
45d6a902
AM
2804
2805 return TRUE;
2806}
2807\f
508c3946
L
2808/* Make weak undefined symbols in PIE dynamic. */
2809
2810bfd_boolean
2811_bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2812 struct elf_link_hash_entry *h)
2813{
0e1862bb 2814 if (bfd_link_pie (info)
508c3946
L
2815 && h->dynindx == -1
2816 && h->root.type == bfd_link_hash_undefweak)
2817 return bfd_elf_link_record_dynamic_symbol (info, h);
2818
2819 return TRUE;
2820}
2821
45d6a902
AM
2822/* Fix up the flags for a symbol. This handles various cases which
2823 can only be fixed after all the input files are seen. This is
2824 currently called by both adjust_dynamic_symbol and
2825 assign_sym_version, which is unnecessary but perhaps more robust in
2826 the face of future changes. */
2827
28caa186 2828static bfd_boolean
268b6b39
AM
2829_bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2830 struct elf_info_failed *eif)
45d6a902 2831{
33774f08 2832 const struct elf_backend_data *bed;
508c3946 2833
45d6a902
AM
2834 /* If this symbol was mentioned in a non-ELF file, try to set
2835 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2836 permit a non-ELF file to correctly refer to a symbol defined in
2837 an ELF dynamic object. */
f5385ebf 2838 if (h->non_elf)
45d6a902
AM
2839 {
2840 while (h->root.type == bfd_link_hash_indirect)
2841 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2842
2843 if (h->root.type != bfd_link_hash_defined
2844 && h->root.type != bfd_link_hash_defweak)
f5385ebf
AM
2845 {
2846 h->ref_regular = 1;
2847 h->ref_regular_nonweak = 1;
2848 }
45d6a902
AM
2849 else
2850 {
2851 if (h->root.u.def.section->owner != NULL
2852 && (bfd_get_flavour (h->root.u.def.section->owner)
2853 == bfd_target_elf_flavour))
f5385ebf
AM
2854 {
2855 h->ref_regular = 1;
2856 h->ref_regular_nonweak = 1;
2857 }
45d6a902 2858 else
f5385ebf 2859 h->def_regular = 1;
45d6a902
AM
2860 }
2861
2862 if (h->dynindx == -1
f5385ebf
AM
2863 && (h->def_dynamic
2864 || h->ref_dynamic))
45d6a902 2865 {
c152c796 2866 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
45d6a902
AM
2867 {
2868 eif->failed = TRUE;
2869 return FALSE;
2870 }
2871 }
2872 }
2873 else
2874 {
f5385ebf 2875 /* Unfortunately, NON_ELF is only correct if the symbol
45d6a902
AM
2876 was first seen in a non-ELF file. Fortunately, if the symbol
2877 was first seen in an ELF file, we're probably OK unless the
2878 symbol was defined in a non-ELF file. Catch that case here.
2879 FIXME: We're still in trouble if the symbol was first seen in
2880 a dynamic object, and then later in a non-ELF regular object. */
2881 if ((h->root.type == bfd_link_hash_defined
2882 || h->root.type == bfd_link_hash_defweak)
f5385ebf 2883 && !h->def_regular
45d6a902
AM
2884 && (h->root.u.def.section->owner != NULL
2885 ? (bfd_get_flavour (h->root.u.def.section->owner)
2886 != bfd_target_elf_flavour)
2887 : (bfd_is_abs_section (h->root.u.def.section)
f5385ebf
AM
2888 && !h->def_dynamic)))
2889 h->def_regular = 1;
45d6a902
AM
2890 }
2891
508c3946 2892 /* Backend specific symbol fixup. */
33774f08
AM
2893 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2894 if (bed->elf_backend_fixup_symbol
2895 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2896 return FALSE;
508c3946 2897
45d6a902
AM
2898 /* If this is a final link, and the symbol was defined as a common
2899 symbol in a regular object file, and there was no definition in
2900 any dynamic object, then the linker will have allocated space for
f5385ebf 2901 the symbol in a common section but the DEF_REGULAR
45d6a902
AM
2902 flag will not have been set. */
2903 if (h->root.type == bfd_link_hash_defined
f5385ebf
AM
2904 && !h->def_regular
2905 && h->ref_regular
2906 && !h->def_dynamic
96f29d96 2907 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
f5385ebf 2908 h->def_regular = 1;
45d6a902 2909
af0bfb9c
AM
2910 /* Symbols defined in discarded sections shouldn't be dynamic. */
2911 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
2912 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2913
4deb8f71
L
2914 /* If a weak undefined symbol has non-default visibility, we also
2915 hide it from the dynamic linker. */
af0bfb9c
AM
2916 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2917 && h->root.type == bfd_link_hash_undefweak)
4deb8f71
L
2918 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2919
2920 /* A hidden versioned symbol in executable should be forced local if
2921 it is is locally defined, not referenced by shared library and not
2922 exported. */
2923 else if (bfd_link_executable (eif->info)
2924 && h->versioned == versioned_hidden
2925 && !eif->info->export_dynamic
2926 && !h->dynamic
2927 && !h->ref_dynamic
2928 && h->def_regular)
2929 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2930
45d6a902
AM
2931 /* If -Bsymbolic was used (which means to bind references to global
2932 symbols to the definition within the shared object), and this
2933 symbol was defined in a regular object, then it actually doesn't
9c7a29a3
AM
2934 need a PLT entry. Likewise, if the symbol has non-default
2935 visibility. If the symbol has hidden or internal visibility, we
c1be741f 2936 will force it local. */
4deb8f71
L
2937 else if (h->needs_plt
2938 && bfd_link_pic (eif->info)
2939 && is_elf_hash_table (eif->info->hash)
2940 && (SYMBOLIC_BIND (eif->info, h)
2941 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2942 && h->def_regular)
45d6a902 2943 {
45d6a902
AM
2944 bfd_boolean force_local;
2945
45d6a902
AM
2946 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2947 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2948 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2949 }
2950
45d6a902
AM
2951 /* If this is a weak defined symbol in a dynamic object, and we know
2952 the real definition in the dynamic object, copy interesting flags
2953 over to the real definition. */
60d67dc8 2954 if (h->is_weakalias)
45d6a902 2955 {
60d67dc8
AM
2956 struct elf_link_hash_entry *def = weakdef (h);
2957
45d6a902
AM
2958 /* If the real definition is defined by a regular object file,
2959 don't do anything special. See the longer description in
5b9d7a9a
AM
2960 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
2961 bfd_link_hash_defined as it was when put on the alias list
2962 then it must have originally been a versioned symbol (for
2963 which a non-versioned indirect symbol is created) and later
2964 a definition for the non-versioned symbol is found. In that
2965 case the indirection is flipped with the versioned symbol
2966 becoming an indirect pointing at the non-versioned symbol.
2967 Thus, not an alias any more. */
2968 if (def->def_regular
2969 || def->root.type != bfd_link_hash_defined)
60d67dc8
AM
2970 {
2971 h = def;
2972 while ((h = h->u.alias) != def)
2973 h->is_weakalias = 0;
2974 }
45d6a902 2975 else
a26587ba 2976 {
4e6b54a6
AM
2977 while (h->root.type == bfd_link_hash_indirect)
2978 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4e6b54a6
AM
2979 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2980 || h->root.type == bfd_link_hash_defweak);
60d67dc8 2981 BFD_ASSERT (def->def_dynamic);
60d67dc8 2982 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
a26587ba 2983 }
45d6a902
AM
2984 }
2985
2986 return TRUE;
2987}
2988
2989/* Make the backend pick a good value for a dynamic symbol. This is
2990 called via elf_link_hash_traverse, and also calls itself
2991 recursively. */
2992
28caa186 2993static bfd_boolean
268b6b39 2994_bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
45d6a902 2995{
a50b1753 2996 struct elf_info_failed *eif = (struct elf_info_failed *) data;
559192d8 2997 struct elf_link_hash_table *htab;
9c5bfbb7 2998 const struct elf_backend_data *bed;
45d6a902 2999
0eddce27 3000 if (! is_elf_hash_table (eif->info->hash))
45d6a902
AM
3001 return FALSE;
3002
45d6a902
AM
3003 /* Ignore indirect symbols. These are added by the versioning code. */
3004 if (h->root.type == bfd_link_hash_indirect)
3005 return TRUE;
3006
3007 /* Fix the symbol flags. */
3008 if (! _bfd_elf_fix_symbol_flags (h, eif))
3009 return FALSE;
3010
559192d8
AM
3011 htab = elf_hash_table (eif->info);
3012 bed = get_elf_backend_data (htab->dynobj);
3013
954b63d4
AM
3014 if (h->root.type == bfd_link_hash_undefweak)
3015 {
3016 if (eif->info->dynamic_undefined_weak == 0)
559192d8 3017 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
954b63d4
AM
3018 else if (eif->info->dynamic_undefined_weak > 0
3019 && h->ref_regular
3020 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3021 && !bfd_hide_sym_by_version (eif->info->version_info,
3022 h->root.root.string))
3023 {
3024 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
3025 {
3026 eif->failed = TRUE;
3027 return FALSE;
3028 }
3029 }
3030 }
3031
45d6a902
AM
3032 /* If this symbol does not require a PLT entry, and it is not
3033 defined by a dynamic object, or is not referenced by a regular
3034 object, ignore it. We do have to handle a weak defined symbol,
3035 even if no regular object refers to it, if we decided to add it
3036 to the dynamic symbol table. FIXME: Do we normally need to worry
3037 about symbols which are defined by one dynamic object and
3038 referenced by another one? */
f5385ebf 3039 if (!h->needs_plt
91e21fb7 3040 && h->type != STT_GNU_IFUNC
f5385ebf
AM
3041 && (h->def_regular
3042 || !h->def_dynamic
3043 || (!h->ref_regular
60d67dc8 3044 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
45d6a902 3045 {
a6aa5195 3046 h->plt = elf_hash_table (eif->info)->init_plt_offset;
45d6a902
AM
3047 return TRUE;
3048 }
3049
3050 /* If we've already adjusted this symbol, don't do it again. This
3051 can happen via a recursive call. */
f5385ebf 3052 if (h->dynamic_adjusted)
45d6a902
AM
3053 return TRUE;
3054
3055 /* Don't look at this symbol again. Note that we must set this
3056 after checking the above conditions, because we may look at a
3057 symbol once, decide not to do anything, and then get called
3058 recursively later after REF_REGULAR is set below. */
f5385ebf 3059 h->dynamic_adjusted = 1;
45d6a902
AM
3060
3061 /* If this is a weak definition, and we know a real definition, and
3062 the real symbol is not itself defined by a regular object file,
3063 then get a good value for the real definition. We handle the
3064 real symbol first, for the convenience of the backend routine.
3065
3066 Note that there is a confusing case here. If the real definition
3067 is defined by a regular object file, we don't get the real symbol
3068 from the dynamic object, but we do get the weak symbol. If the
3069 processor backend uses a COPY reloc, then if some routine in the
3070 dynamic object changes the real symbol, we will not see that
3071 change in the corresponding weak symbol. This is the way other
3072 ELF linkers work as well, and seems to be a result of the shared
3073 library model.
3074
3075 I will clarify this issue. Most SVR4 shared libraries define the
3076 variable _timezone and define timezone as a weak synonym. The
3077 tzset call changes _timezone. If you write
3078 extern int timezone;
3079 int _timezone = 5;
3080 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3081 you might expect that, since timezone is a synonym for _timezone,
3082 the same number will print both times. However, if the processor
3083 backend uses a COPY reloc, then actually timezone will be copied
3084 into your process image, and, since you define _timezone
3085 yourself, _timezone will not. Thus timezone and _timezone will
3086 wind up at different memory locations. The tzset call will set
3087 _timezone, leaving timezone unchanged. */
3088
60d67dc8 3089 if (h->is_weakalias)
45d6a902 3090 {
60d67dc8
AM
3091 struct elf_link_hash_entry *def = weakdef (h);
3092
ec24dc88 3093 /* If we get to this point, there is an implicit reference to
60d67dc8
AM
3094 the alias by a regular object file via the weak symbol H. */
3095 def->ref_regular = 1;
45d6a902 3096
ec24dc88 3097 /* Ensure that the backend adjust_dynamic_symbol function sees
60d67dc8
AM
3098 the strong alias before H by recursively calling ourselves. */
3099 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
45d6a902
AM
3100 return FALSE;
3101 }
3102
3103 /* If a symbol has no type and no size and does not require a PLT
3104 entry, then we are probably about to do the wrong thing here: we
3105 are probably going to create a COPY reloc for an empty object.
3106 This case can arise when a shared object is built with assembly
3107 code, and the assembly code fails to set the symbol type. */
3108 if (h->size == 0
3109 && h->type == STT_NOTYPE
f5385ebf 3110 && !h->needs_plt)
4eca0228 3111 _bfd_error_handler
45d6a902
AM
3112 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3113 h->root.root.string);
3114
45d6a902
AM
3115 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3116 {
3117 eif->failed = TRUE;
3118 return FALSE;
3119 }
3120
3121 return TRUE;
3122}
3123
027297b7
L
3124/* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3125 DYNBSS. */
3126
3127bfd_boolean
6cabe1ea
AM
3128_bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3129 struct elf_link_hash_entry *h,
027297b7
L
3130 asection *dynbss)
3131{
91ac5911 3132 unsigned int power_of_two;
027297b7
L
3133 bfd_vma mask;
3134 asection *sec = h->root.u.def.section;
3135
de194d85 3136 /* The section alignment of the definition is the maximum alignment
91ac5911
L
3137 requirement of symbols defined in the section. Since we don't
3138 know the symbol alignment requirement, we start with the
3139 maximum alignment and check low bits of the symbol address
3140 for the minimum alignment. */
fd361982 3141 power_of_two = bfd_section_alignment (sec);
91ac5911
L
3142 mask = ((bfd_vma) 1 << power_of_two) - 1;
3143 while ((h->root.u.def.value & mask) != 0)
3144 {
3145 mask >>= 1;
3146 --power_of_two;
3147 }
027297b7 3148
fd361982 3149 if (power_of_two > bfd_section_alignment (dynbss))
027297b7
L
3150 {
3151 /* Adjust the section alignment if needed. */
fd361982 3152 if (!bfd_set_section_alignment (dynbss, power_of_two))
027297b7
L
3153 return FALSE;
3154 }
3155
91ac5911 3156 /* We make sure that the symbol will be aligned properly. */
027297b7
L
3157 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3158
3159 /* Define the symbol as being at this point in DYNBSS. */
3160 h->root.u.def.section = dynbss;
3161 h->root.u.def.value = dynbss->size;
3162
3163 /* Increment the size of DYNBSS to make room for the symbol. */
3164 dynbss->size += h->size;
3165
f7483970
L
3166 /* No error if extern_protected_data is true. */
3167 if (h->protected_def
889c2a67
L
3168 && (!info->extern_protected_data
3169 || (info->extern_protected_data < 0
3170 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
d07a1b05 3171 info->callbacks->einfo
c1c8c1ef 3172 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
d07a1b05 3173 h->root.root.string);
6cabe1ea 3174
027297b7
L
3175 return TRUE;
3176}
3177
45d6a902
AM
3178/* Adjust all external symbols pointing into SEC_MERGE sections
3179 to reflect the object merging within the sections. */
3180
28caa186 3181static bfd_boolean
268b6b39 3182_bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
45d6a902
AM
3183{
3184 asection *sec;
3185
45d6a902
AM
3186 if ((h->root.type == bfd_link_hash_defined
3187 || h->root.type == bfd_link_hash_defweak)
3188 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
dbaa2011 3189 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
45d6a902 3190 {
a50b1753 3191 bfd *output_bfd = (bfd *) data;
45d6a902
AM
3192
3193 h->root.u.def.value =
3194 _bfd_merged_section_offset (output_bfd,
3195 &h->root.u.def.section,
3196 elf_section_data (sec)->sec_info,
753731ee 3197 h->root.u.def.value);
45d6a902
AM
3198 }
3199
3200 return TRUE;
3201}
986a241f
RH
3202
3203/* Returns false if the symbol referred to by H should be considered
3204 to resolve local to the current module, and true if it should be
3205 considered to bind dynamically. */
3206
3207bfd_boolean
268b6b39
AM
3208_bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3209 struct bfd_link_info *info,
89a2ee5a 3210 bfd_boolean not_local_protected)
986a241f
RH
3211{
3212 bfd_boolean binding_stays_local_p;
fcb93ecf
PB
3213 const struct elf_backend_data *bed;
3214 struct elf_link_hash_table *hash_table;
986a241f
RH
3215
3216 if (h == NULL)
3217 return FALSE;
3218
3219 while (h->root.type == bfd_link_hash_indirect
3220 || h->root.type == bfd_link_hash_warning)
3221 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3222
3223 /* If it was forced local, then clearly it's not dynamic. */
3224 if (h->dynindx == -1)
3225 return FALSE;
f5385ebf 3226 if (h->forced_local)
986a241f
RH
3227 return FALSE;
3228
3229 /* Identify the cases where name binding rules say that a
3230 visible symbol resolves locally. */
0e1862bb
L
3231 binding_stays_local_p = (bfd_link_executable (info)
3232 || SYMBOLIC_BIND (info, h));
986a241f
RH
3233
3234 switch (ELF_ST_VISIBILITY (h->other))
3235 {
3236 case STV_INTERNAL:
3237 case STV_HIDDEN:
3238 return FALSE;
3239
3240 case STV_PROTECTED:
fcb93ecf
PB
3241 hash_table = elf_hash_table (info);
3242 if (!is_elf_hash_table (hash_table))
3243 return FALSE;
3244
3245 bed = get_elf_backend_data (hash_table->dynobj);
3246
986a241f
RH
3247 /* Proper resolution for function pointer equality may require
3248 that these symbols perhaps be resolved dynamically, even though
3249 we should be resolving them to the current module. */
89a2ee5a 3250 if (!not_local_protected || !bed->is_function_type (h->type))
986a241f
RH
3251 binding_stays_local_p = TRUE;
3252 break;
3253
3254 default:
986a241f
RH
3255 break;
3256 }
3257
aa37626c 3258 /* If it isn't defined locally, then clearly it's dynamic. */
89a2ee5a 3259 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
aa37626c
L
3260 return TRUE;
3261
986a241f
RH
3262 /* Otherwise, the symbol is dynamic if binding rules don't tell
3263 us that it remains local. */
3264 return !binding_stays_local_p;
3265}
f6c52c13
AM
3266
3267/* Return true if the symbol referred to by H should be considered
3268 to resolve local to the current module, and false otherwise. Differs
3269 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2e76e85a 3270 undefined symbols. The two functions are virtually identical except
0fad2956
MR
3271 for the place where dynindx == -1 is tested. If that test is true,
3272 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3273 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3274 defined symbols.
89a2ee5a
AM
3275 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3276 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3277 treatment of undefined weak symbols. For those that do not make
3278 undefined weak symbols dynamic, both functions may return false. */
f6c52c13
AM
3279
3280bfd_boolean
268b6b39
AM
3281_bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3282 struct bfd_link_info *info,
3283 bfd_boolean local_protected)
f6c52c13 3284{
fcb93ecf
PB
3285 const struct elf_backend_data *bed;
3286 struct elf_link_hash_table *hash_table;
3287
f6c52c13
AM
3288 /* If it's a local sym, of course we resolve locally. */
3289 if (h == NULL)
3290 return TRUE;
3291
d95edcac
L
3292 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3293 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3294 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3295 return TRUE;
3296
0fad2956
MR
3297 /* Forced local symbols resolve locally. */
3298 if (h->forced_local)
3299 return TRUE;
3300
7e2294f9
AO
3301 /* Common symbols that become definitions don't get the DEF_REGULAR
3302 flag set, so test it first, and don't bail out. */
3303 if (ELF_COMMON_DEF_P (h))
3304 /* Do nothing. */;
f6c52c13 3305 /* If we don't have a definition in a regular file, then we can't
49ff44d6
L
3306 resolve locally. The sym is either undefined or dynamic. */
3307 else if (!h->def_regular)
f6c52c13
AM
3308 return FALSE;
3309
0fad2956 3310 /* Non-dynamic symbols resolve locally. */
f6c52c13
AM
3311 if (h->dynindx == -1)
3312 return TRUE;
3313
3314 /* At this point, we know the symbol is defined and dynamic. In an
3315 executable it must resolve locally, likewise when building symbolic
3316 shared libraries. */
0e1862bb 3317 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
f6c52c13
AM
3318 return TRUE;
3319
3320 /* Now deal with defined dynamic symbols in shared libraries. Ones
3321 with default visibility might not resolve locally. */
3322 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3323 return FALSE;
3324
fcb93ecf
PB
3325 hash_table = elf_hash_table (info);
3326 if (!is_elf_hash_table (hash_table))
3327 return TRUE;
3328
3329 bed = get_elf_backend_data (hash_table->dynobj);
3330
f7483970
L
3331 /* If extern_protected_data is false, STV_PROTECTED non-function
3332 symbols are local. */
889c2a67
L
3333 if ((!info->extern_protected_data
3334 || (info->extern_protected_data < 0
3335 && !bed->extern_protected_data))
3336 && !bed->is_function_type (h->type))
1c16dfa5
L
3337 return TRUE;
3338
f6c52c13 3339 /* Function pointer equality tests may require that STV_PROTECTED
2676a7d9
AM
3340 symbols be treated as dynamic symbols. If the address of a
3341 function not defined in an executable is set to that function's
3342 plt entry in the executable, then the address of the function in
3343 a shared library must also be the plt entry in the executable. */
f6c52c13
AM
3344 return local_protected;
3345}
e1918d23
AM
3346
3347/* Caches some TLS segment info, and ensures that the TLS segment vma is
3348 aligned. Returns the first TLS output section. */
3349
3350struct bfd_section *
3351_bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3352{
3353 struct bfd_section *sec, *tls;
3354 unsigned int align = 0;
3355
3356 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3357 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3358 break;
3359 tls = sec;
3360
3361 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3362 if (sec->alignment_power > align)
3363 align = sec->alignment_power;
3364
3365 elf_hash_table (info)->tls_sec = tls;
3366
fdde2fb6
SH
3367 /* Ensure the alignment of the first section (usually .tdata) is the largest
3368 alignment, so that the tls segment starts aligned. */
e1918d23
AM
3369 if (tls != NULL)
3370 tls->alignment_power = align;
3371
3372 return tls;
3373}
0ad989f9
L
3374
3375/* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3376static bfd_boolean
3377is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3378 Elf_Internal_Sym *sym)
3379{
a4d8e49b
L
3380 const struct elf_backend_data *bed;
3381
0ad989f9
L
3382 /* Local symbols do not count, but target specific ones might. */
3383 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3384 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3385 return FALSE;
3386
fcb93ecf 3387 bed = get_elf_backend_data (abfd);
0ad989f9 3388 /* Function symbols do not count. */
fcb93ecf 3389 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
0ad989f9
L
3390 return FALSE;
3391
3392 /* If the section is undefined, then so is the symbol. */
3393 if (sym->st_shndx == SHN_UNDEF)
3394 return FALSE;
3395
3396 /* If the symbol is defined in the common section, then
3397 it is a common definition and so does not count. */
a4d8e49b 3398 if (bed->common_definition (sym))
0ad989f9
L
3399 return FALSE;
3400
3401 /* If the symbol is in a target specific section then we
3402 must rely upon the backend to tell us what it is. */
3403 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3404 /* FIXME - this function is not coded yet:
3405
3406 return _bfd_is_global_symbol_definition (abfd, sym);
3407
3408 Instead for now assume that the definition is not global,
3409 Even if this is wrong, at least the linker will behave
3410 in the same way that it used to do. */
3411 return FALSE;
3412
3413 return TRUE;
3414}
3415
3416/* Search the symbol table of the archive element of the archive ABFD
3417 whose archive map contains a mention of SYMDEF, and determine if
3418 the symbol is defined in this element. */
3419static bfd_boolean
3420elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3421{
3422 Elf_Internal_Shdr * hdr;
ef53be89
AM
3423 size_t symcount;
3424 size_t extsymcount;
3425 size_t extsymoff;
0ad989f9
L
3426 Elf_Internal_Sym *isymbuf;
3427 Elf_Internal_Sym *isym;
3428 Elf_Internal_Sym *isymend;
3429 bfd_boolean result;
3430
3431 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3432 if (abfd == NULL)
3433 return FALSE;
3434
3435 if (! bfd_check_format (abfd, bfd_object))
3436 return FALSE;
3437
7dc3990e
L
3438 /* Select the appropriate symbol table. If we don't know if the
3439 object file is an IR object, give linker LTO plugin a chance to
3440 get the correct symbol table. */
3441 if (abfd->plugin_format == bfd_plugin_yes
08ce1d72 3442#if BFD_SUPPORTS_PLUGINS
7dc3990e
L
3443 || (abfd->plugin_format == bfd_plugin_unknown
3444 && bfd_link_plugin_object_p (abfd))
3445#endif
3446 )
3447 {
3448 /* Use the IR symbol table if the object has been claimed by
3449 plugin. */
3450 abfd = abfd->plugin_dummy_bfd;
3451 hdr = &elf_tdata (abfd)->symtab_hdr;
3452 }
3453 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
0ad989f9
L
3454 hdr = &elf_tdata (abfd)->symtab_hdr;
3455 else
3456 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3457
3458 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3459
3460 /* The sh_info field of the symtab header tells us where the
3461 external symbols start. We don't care about the local symbols. */
3462 if (elf_bad_symtab (abfd))
3463 {
3464 extsymcount = symcount;
3465 extsymoff = 0;
3466 }
3467 else
3468 {
3469 extsymcount = symcount - hdr->sh_info;
3470 extsymoff = hdr->sh_info;
3471 }
3472
3473 if (extsymcount == 0)
3474 return FALSE;
3475
3476 /* Read in the symbol table. */
3477 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3478 NULL, NULL, NULL);
3479 if (isymbuf == NULL)
3480 return FALSE;
3481
3482 /* Scan the symbol table looking for SYMDEF. */
3483 result = FALSE;
3484 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3485 {
3486 const char *name;
3487
3488 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3489 isym->st_name);
3490 if (name == NULL)
3491 break;
3492
3493 if (strcmp (name, symdef->name) == 0)
3494 {
3495 result = is_global_data_symbol_definition (abfd, isym);
3496 break;
3497 }
3498 }
3499
3500 free (isymbuf);
3501
3502 return result;
3503}
3504\f
5a580b3a
AM
3505/* Add an entry to the .dynamic table. */
3506
3507bfd_boolean
3508_bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3509 bfd_vma tag,
3510 bfd_vma val)
3511{
3512 struct elf_link_hash_table *hash_table;
3513 const struct elf_backend_data *bed;
3514 asection *s;
3515 bfd_size_type newsize;
3516 bfd_byte *newcontents;
3517 Elf_Internal_Dyn dyn;
3518
3519 hash_table = elf_hash_table (info);
3520 if (! is_elf_hash_table (hash_table))
3521 return FALSE;
3522
7f923b7f
AM
3523 if (tag == DT_RELA || tag == DT_REL)
3524 hash_table->dynamic_relocs = TRUE;
3525
5a580b3a 3526 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3527 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
5a580b3a
AM
3528 BFD_ASSERT (s != NULL);
3529
eea6121a 3530 newsize = s->size + bed->s->sizeof_dyn;
a50b1753 3531 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
5a580b3a
AM
3532 if (newcontents == NULL)
3533 return FALSE;
3534
3535 dyn.d_tag = tag;
3536 dyn.d_un.d_val = val;
eea6121a 3537 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
5a580b3a 3538
eea6121a 3539 s->size = newsize;
5a580b3a
AM
3540 s->contents = newcontents;
3541
3542 return TRUE;
3543}
3544
6f6fd151
L
3545/* Strip zero-sized dynamic sections. */
3546
3547bfd_boolean
3548_bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
3549{
3550 struct elf_link_hash_table *hash_table;
3551 const struct elf_backend_data *bed;
3552 asection *s, *sdynamic, **pp;
3553 asection *rela_dyn, *rel_dyn;
3554 Elf_Internal_Dyn dyn;
3555 bfd_byte *extdyn, *next;
3556 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3557 bfd_boolean strip_zero_sized;
3558 bfd_boolean strip_zero_sized_plt;
3559
3560 if (bfd_link_relocatable (info))
3561 return TRUE;
3562
3563 hash_table = elf_hash_table (info);
3564 if (!is_elf_hash_table (hash_table))
3565 return FALSE;
3566
3567 if (!hash_table->dynobj)
3568 return TRUE;
3569
3570 sdynamic= bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3571 if (!sdynamic)
3572 return TRUE;
3573
3574 bed = get_elf_backend_data (hash_table->dynobj);
3575 swap_dyn_in = bed->s->swap_dyn_in;
3576
3577 strip_zero_sized = FALSE;
3578 strip_zero_sized_plt = FALSE;
3579
3580 /* Strip zero-sized dynamic sections. */
3581 rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
3582 rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
3583 for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
3584 if (s->size == 0
3585 && (s == rela_dyn
3586 || s == rel_dyn
3587 || s == hash_table->srelplt->output_section
3588 || s == hash_table->splt->output_section))
3589 {
3590 *pp = s->next;
3591 info->output_bfd->section_count--;
3592 strip_zero_sized = TRUE;
3593 if (s == rela_dyn)
3594 s = rela_dyn;
3595 if (s == rel_dyn)
3596 s = rel_dyn;
3597 else if (s == hash_table->splt->output_section)
3598 {
3599 s = hash_table->splt;
3600 strip_zero_sized_plt = TRUE;
3601 }
3602 else
3603 s = hash_table->srelplt;
3604 s->flags |= SEC_EXCLUDE;
3605 s->output_section = bfd_abs_section_ptr;
3606 }
3607 else
3608 pp = &s->next;
3609
3610 if (strip_zero_sized_plt)
3611 for (extdyn = sdynamic->contents;
3612 extdyn < sdynamic->contents + sdynamic->size;
3613 extdyn = next)
3614 {
3615 next = extdyn + bed->s->sizeof_dyn;
3616 swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3617 switch (dyn.d_tag)
3618 {
3619 default:
3620 break;
3621 case DT_JMPREL:
3622 case DT_PLTRELSZ:
3623 case DT_PLTREL:
3624 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
3625 the procedure linkage table (the .plt section) has been
3626 removed. */
3627 memmove (extdyn, next,
3628 sdynamic->size - (next - sdynamic->contents));
3629 next = extdyn;
3630 }
3631 }
3632
3633 if (strip_zero_sized)
3634 {
3635 /* Regenerate program headers. */
3636 elf_seg_map (info->output_bfd) = NULL;
3637 return _bfd_elf_map_sections_to_segments (info->output_bfd, info);
3638 }
3639
3640 return TRUE;
3641}
3642
e310298c 3643/* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
5a580b3a
AM
3644 1 if a DT_NEEDED tag already exists, and 0 on success. */
3645
e310298c
AM
3646int
3647bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
5a580b3a
AM
3648{
3649 struct elf_link_hash_table *hash_table;
ef53be89 3650 size_t strindex;
e310298c 3651 const char *soname;
5a580b3a 3652
7e9f0867
AM
3653 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3654 return -1;
3655
5a580b3a 3656 hash_table = elf_hash_table (info);
e310298c 3657 soname = elf_dt_name (abfd);
5a580b3a 3658 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
ef53be89 3659 if (strindex == (size_t) -1)
5a580b3a
AM
3660 return -1;
3661
02be4619 3662 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
5a580b3a
AM
3663 {
3664 asection *sdyn;
3665 const struct elf_backend_data *bed;
3666 bfd_byte *extdyn;
3667
3668 bed = get_elf_backend_data (hash_table->dynobj);
3d4d4302 3669 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
7e9f0867
AM
3670 if (sdyn != NULL)
3671 for (extdyn = sdyn->contents;
3672 extdyn < sdyn->contents + sdyn->size;
3673 extdyn += bed->s->sizeof_dyn)
3674 {
3675 Elf_Internal_Dyn dyn;
5a580b3a 3676
7e9f0867
AM
3677 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3678 if (dyn.d_tag == DT_NEEDED
3679 && dyn.d_un.d_val == strindex)
3680 {
3681 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3682 return 1;
3683 }
3684 }
5a580b3a
AM
3685 }
3686
e310298c
AM
3687 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3688 return -1;
7e9f0867 3689
e310298c
AM
3690 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3691 return -1;
5a580b3a
AM
3692
3693 return 0;
3694}
3695
7b15fa7a
AM
3696/* Return true if SONAME is on the needed list between NEEDED and STOP
3697 (or the end of list if STOP is NULL), and needed by a library that
3698 will be loaded. */
3699
010e5ae2 3700static bfd_boolean
7b15fa7a
AM
3701on_needed_list (const char *soname,
3702 struct bfd_link_needed_list *needed,
3703 struct bfd_link_needed_list *stop)
010e5ae2 3704{
7b15fa7a
AM
3705 struct bfd_link_needed_list *look;
3706 for (look = needed; look != stop; look = look->next)
3707 if (strcmp (soname, look->name) == 0
3708 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3709 /* If needed by a library that itself is not directly
3710 needed, recursively check whether that library is
3711 indirectly needed. Since we add DT_NEEDED entries to
3712 the end of the list, library dependencies appear after
3713 the library. Therefore search prior to the current
3714 LOOK, preventing possible infinite recursion. */
3715 || on_needed_list (elf_dt_name (look->by), needed, look)))
010e5ae2
AM
3716 return TRUE;
3717
3718 return FALSE;
3719}
3720
3a3f4bf7 3721/* Sort symbol by value, section, size, and type. */
4ad4eba5
AM
3722static int
3723elf_sort_symbol (const void *arg1, const void *arg2)
5a580b3a
AM
3724{
3725 const struct elf_link_hash_entry *h1;
3726 const struct elf_link_hash_entry *h2;
10b7e05b 3727 bfd_signed_vma vdiff;
3a3f4bf7
AM
3728 int sdiff;
3729 const char *n1;
3730 const char *n2;
5a580b3a
AM
3731
3732 h1 = *(const struct elf_link_hash_entry **) arg1;
3733 h2 = *(const struct elf_link_hash_entry **) arg2;
10b7e05b
NC
3734 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3735 if (vdiff != 0)
3736 return vdiff > 0 ? 1 : -1;
3a3f4bf7
AM
3737
3738 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3739 if (sdiff != 0)
3740 return sdiff;
3741
3742 /* Sort so that sized symbols are selected over zero size symbols. */
3743 vdiff = h1->size - h2->size;
3744 if (vdiff != 0)
3745 return vdiff > 0 ? 1 : -1;
3746
3747 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
3748 if (h1->type != h2->type)
3749 return h1->type - h2->type;
3750
3751 /* If symbols are properly sized and typed, and multiple strong
3752 aliases are not defined in a shared library by the user we
3753 shouldn't get here. Unfortunately linker script symbols like
3754 __bss_start sometimes match a user symbol defined at the start of
3755 .bss without proper size and type. We'd like to preference the
3756 user symbol over reserved system symbols. Sort on leading
3757 underscores. */
3758 n1 = h1->root.root.string;
3759 n2 = h2->root.root.string;
3760 while (*n1 == *n2)
10b7e05b 3761 {
3a3f4bf7
AM
3762 if (*n1 == 0)
3763 break;
3764 ++n1;
3765 ++n2;
10b7e05b 3766 }
3a3f4bf7
AM
3767 if (*n1 == '_')
3768 return -1;
3769 if (*n2 == '_')
3770 return 1;
3771
3772 /* Final sort on name selects user symbols like '_u' over reserved
3773 system symbols like '_Z' and also will avoid qsort instability. */
3774 return *n1 - *n2;
5a580b3a 3775}
4ad4eba5 3776
5a580b3a
AM
3777/* This function is used to adjust offsets into .dynstr for
3778 dynamic symbols. This is called via elf_link_hash_traverse. */
3779
3780static bfd_boolean
3781elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3782{
a50b1753 3783 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
5a580b3a 3784
5a580b3a
AM
3785 if (h->dynindx != -1)
3786 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3787 return TRUE;
3788}
3789
3790/* Assign string offsets in .dynstr, update all structures referencing
3791 them. */
3792
4ad4eba5
AM
3793static bfd_boolean
3794elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5a580b3a
AM
3795{
3796 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3797 struct elf_link_local_dynamic_entry *entry;
3798 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3799 bfd *dynobj = hash_table->dynobj;
3800 asection *sdyn;
3801 bfd_size_type size;
3802 const struct elf_backend_data *bed;
3803 bfd_byte *extdyn;
3804
3805 _bfd_elf_strtab_finalize (dynstr);
3806 size = _bfd_elf_strtab_size (dynstr);
3807
3d16b64e
NA
3808 /* Allow the linker to examine the dynsymtab now it's fully populated. */
3809
3810 if (info->callbacks->examine_strtab)
3811 info->callbacks->examine_strtab (dynstr);
3812
5a580b3a 3813 bed = get_elf_backend_data (dynobj);
3d4d4302 3814 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5a580b3a
AM
3815 BFD_ASSERT (sdyn != NULL);
3816
3817 /* Update all .dynamic entries referencing .dynstr strings. */
3818 for (extdyn = sdyn->contents;
eea6121a 3819 extdyn < sdyn->contents + sdyn->size;
5a580b3a
AM
3820 extdyn += bed->s->sizeof_dyn)
3821 {
3822 Elf_Internal_Dyn dyn;
3823
3824 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3825 switch (dyn.d_tag)
3826 {
3827 case DT_STRSZ:
3828 dyn.d_un.d_val = size;
3829 break;
3830 case DT_NEEDED:
3831 case DT_SONAME:
3832 case DT_RPATH:
3833 case DT_RUNPATH:
3834 case DT_FILTER:
3835 case DT_AUXILIARY:
7ee314fa
AM
3836 case DT_AUDIT:
3837 case DT_DEPAUDIT:
5a580b3a
AM
3838 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3839 break;
3840 default:
3841 continue;
3842 }
3843 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3844 }
3845
3846 /* Now update local dynamic symbols. */
3847 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3848 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3849 entry->isym.st_name);
3850
3851 /* And the rest of dynamic symbols. */
3852 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3853
3854 /* Adjust version definitions. */
3855 if (elf_tdata (output_bfd)->cverdefs)
3856 {
3857 asection *s;
3858 bfd_byte *p;
ef53be89 3859 size_t i;
5a580b3a
AM
3860 Elf_Internal_Verdef def;
3861 Elf_Internal_Verdaux defaux;
3862
3d4d4302 3863 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5a580b3a
AM
3864 p = s->contents;
3865 do
3866 {
3867 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3868 &def);
3869 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
3870 if (def.vd_aux != sizeof (Elf_External_Verdef))
3871 continue;
5a580b3a
AM
3872 for (i = 0; i < def.vd_cnt; ++i)
3873 {
3874 _bfd_elf_swap_verdaux_in (output_bfd,
3875 (Elf_External_Verdaux *) p, &defaux);
3876 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3877 defaux.vda_name);
3878 _bfd_elf_swap_verdaux_out (output_bfd,
3879 &defaux, (Elf_External_Verdaux *) p);
3880 p += sizeof (Elf_External_Verdaux);
3881 }
3882 }
3883 while (def.vd_next);
3884 }
3885
3886 /* Adjust version references. */
3887 if (elf_tdata (output_bfd)->verref)
3888 {
3889 asection *s;
3890 bfd_byte *p;
ef53be89 3891 size_t i;
5a580b3a
AM
3892 Elf_Internal_Verneed need;
3893 Elf_Internal_Vernaux needaux;
3894
3d4d4302 3895 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
5a580b3a
AM
3896 p = s->contents;
3897 do
3898 {
3899 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3900 &need);
3901 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3902 _bfd_elf_swap_verneed_out (output_bfd, &need,
3903 (Elf_External_Verneed *) p);
3904 p += sizeof (Elf_External_Verneed);
3905 for (i = 0; i < need.vn_cnt; ++i)
3906 {
3907 _bfd_elf_swap_vernaux_in (output_bfd,
3908 (Elf_External_Vernaux *) p, &needaux);
3909 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3910 needaux.vna_name);
3911 _bfd_elf_swap_vernaux_out (output_bfd,
3912 &needaux,
3913 (Elf_External_Vernaux *) p);
3914 p += sizeof (Elf_External_Vernaux);
3915 }
3916 }
3917 while (need.vn_next);
3918 }
3919
3920 return TRUE;
3921}
3922\f
13285a1b
AM
3923/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3924 The default is to only match when the INPUT and OUTPUT are exactly
3925 the same target. */
3926
3927bfd_boolean
3928_bfd_elf_default_relocs_compatible (const bfd_target *input,
3929 const bfd_target *output)
3930{
3931 return input == output;
3932}
3933
3934/* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3935 This version is used when different targets for the same architecture
3936 are virtually identical. */
3937
3938bfd_boolean
3939_bfd_elf_relocs_compatible (const bfd_target *input,
3940 const bfd_target *output)
3941{
3942 const struct elf_backend_data *obed, *ibed;
3943
3944 if (input == output)
3945 return TRUE;
3946
3947 ibed = xvec_get_elf_backend_data (input);
3948 obed = xvec_get_elf_backend_data (output);
3949
3950 if (ibed->arch != obed->arch)
3951 return FALSE;
3952
3953 /* If both backends are using this function, deem them compatible. */
3954 return ibed->relocs_compatible == obed->relocs_compatible;
3955}
3956
e5034e59
AM
3957/* Make a special call to the linker "notice" function to tell it that
3958 we are about to handle an as-needed lib, or have finished
1b786873 3959 processing the lib. */
e5034e59
AM
3960
3961bfd_boolean
3962_bfd_elf_notice_as_needed (bfd *ibfd,
3963 struct bfd_link_info *info,
3964 enum notice_asneeded_action act)
3965{
46135103 3966 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
e5034e59
AM
3967}
3968
d9689752
L
3969/* Check relocations an ELF object file. */
3970
3971bfd_boolean
3972_bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3973{
3974 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3975 struct elf_link_hash_table *htab = elf_hash_table (info);
3976
3977 /* If this object is the same format as the output object, and it is
3978 not a shared library, then let the backend look through the
3979 relocs.
3980
3981 This is required to build global offset table entries and to
3982 arrange for dynamic relocs. It is not required for the
3983 particular common case of linking non PIC code, even when linking
3984 against shared libraries, but unfortunately there is no way of
3985 knowing whether an object file has been compiled PIC or not.
3986 Looking through the relocs is not particularly time consuming.
3987 The problem is that we must either (1) keep the relocs in memory,
3988 which causes the linker to require additional runtime memory or
3989 (2) read the relocs twice from the input file, which wastes time.
3990 This would be a good case for using mmap.
3991
3992 I have no idea how to handle linking PIC code into a file of a
3993 different format. It probably can't be done. */
3994 if ((abfd->flags & DYNAMIC) == 0
3995 && is_elf_hash_table (htab)
3996 && bed->check_relocs != NULL
3997 && elf_object_id (abfd) == elf_hash_table_id (htab)
3998 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3999 {
4000 asection *o;
4001
4002 for (o = abfd->sections; o != NULL; o = o->next)
4003 {
4004 Elf_Internal_Rela *internal_relocs;
4005 bfd_boolean ok;
4006
c4b126b8
L
4007 /* Don't check relocations in excluded sections. Don't do
4008 anything special with non-loaded, non-alloced sections.
4009 In particular, any relocs in such sections should not
4010 affect GOT and PLT reference counting (ie. we don't
4011 allow them to create GOT or PLT entries), there's no
4012 possibility or desire to optimize TLS relocs, and
4013 there's not much point in propagating relocs to shared
4014 libs that the dynamic linker won't relocate. */
4015 if ((o->flags & SEC_ALLOC) == 0
4016 || (o->flags & SEC_RELOC) == 0
5ce03cea 4017 || (o->flags & SEC_EXCLUDE) != 0
d9689752
L
4018 || o->reloc_count == 0
4019 || ((info->strip == strip_all || info->strip == strip_debugger)
4020 && (o->flags & SEC_DEBUGGING) != 0)
4021 || bfd_is_abs_section (o->output_section))
4022 continue;
4023
4024 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4025 info->keep_memory);
4026 if (internal_relocs == NULL)
4027 return FALSE;
4028
4029 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4030
4031 if (elf_section_data (o)->relocs != internal_relocs)
4032 free (internal_relocs);
4033
4034 if (! ok)
4035 return FALSE;
4036 }
4037 }
4038
4039 return TRUE;
4040}
4041
4ad4eba5
AM
4042/* Add symbols from an ELF object file to the linker hash table. */
4043
4044static bfd_boolean
4045elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
4046{
a0c402a5 4047 Elf_Internal_Ehdr *ehdr;
4ad4eba5 4048 Elf_Internal_Shdr *hdr;
ef53be89
AM
4049 size_t symcount;
4050 size_t extsymcount;
4051 size_t extsymoff;
4ad4eba5
AM
4052 struct elf_link_hash_entry **sym_hash;
4053 bfd_boolean dynamic;
4054 Elf_External_Versym *extversym = NULL;
be22c732 4055 Elf_External_Versym *extversym_end = NULL;
4ad4eba5
AM
4056 Elf_External_Versym *ever;
4057 struct elf_link_hash_entry *weaks;
4058 struct elf_link_hash_entry **nondeflt_vers = NULL;
ef53be89 4059 size_t nondeflt_vers_cnt = 0;
4ad4eba5
AM
4060 Elf_Internal_Sym *isymbuf = NULL;
4061 Elf_Internal_Sym *isym;
4062 Elf_Internal_Sym *isymend;
4063 const struct elf_backend_data *bed;
4064 bfd_boolean add_needed;
66eb6687 4065 struct elf_link_hash_table *htab;
66eb6687 4066 void *alloc_mark = NULL;
4f87808c
AM
4067 struct bfd_hash_entry **old_table = NULL;
4068 unsigned int old_size = 0;
4069 unsigned int old_count = 0;
66eb6687 4070 void *old_tab = NULL;
66eb6687
AM
4071 void *old_ent;
4072 struct bfd_link_hash_entry *old_undefs = NULL;
4073 struct bfd_link_hash_entry *old_undefs_tail = NULL;
5b677558 4074 void *old_strtab = NULL;
66eb6687 4075 size_t tabsize = 0;
db6a5d5f 4076 asection *s;
29a9f53e 4077 bfd_boolean just_syms;
4ad4eba5 4078
66eb6687 4079 htab = elf_hash_table (info);
4ad4eba5 4080 bed = get_elf_backend_data (abfd);
4ad4eba5
AM
4081
4082 if ((abfd->flags & DYNAMIC) == 0)
4083 dynamic = FALSE;
4084 else
4085 {
4086 dynamic = TRUE;
4087
4088 /* You can't use -r against a dynamic object. Also, there's no
4089 hope of using a dynamic object which does not exactly match
4090 the format of the output file. */
0e1862bb 4091 if (bfd_link_relocatable (info)
66eb6687 4092 || !is_elf_hash_table (htab)
f13a99db 4093 || info->output_bfd->xvec != abfd->xvec)
4ad4eba5 4094 {
0e1862bb 4095 if (bfd_link_relocatable (info))
9a0789ec
NC
4096 bfd_set_error (bfd_error_invalid_operation);
4097 else
4098 bfd_set_error (bfd_error_wrong_format);
4ad4eba5
AM
4099 goto error_return;
4100 }
4101 }
4102
a0c402a5
L
4103 ehdr = elf_elfheader (abfd);
4104 if (info->warn_alternate_em
4105 && bed->elf_machine_code != ehdr->e_machine
4106 && ((bed->elf_machine_alt1 != 0
4107 && ehdr->e_machine == bed->elf_machine_alt1)
4108 || (bed->elf_machine_alt2 != 0
4109 && ehdr->e_machine == bed->elf_machine_alt2)))
9793eb77 4110 _bfd_error_handler
695344c0 4111 /* xgettext:c-format */
9793eb77 4112 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
a0c402a5
L
4113 ehdr->e_machine, abfd, bed->elf_machine_code);
4114
4ad4eba5
AM
4115 /* As a GNU extension, any input sections which are named
4116 .gnu.warning.SYMBOL are treated as warning symbols for the given
4117 symbol. This differs from .gnu.warning sections, which generate
4118 warnings when they are included in an output file. */
dd98f8d2 4119 /* PR 12761: Also generate this warning when building shared libraries. */
db6a5d5f 4120 for (s = abfd->sections; s != NULL; s = s->next)
4ad4eba5 4121 {
db6a5d5f 4122 const char *name;
4ad4eba5 4123
fd361982 4124 name = bfd_section_name (s);
db6a5d5f 4125 if (CONST_STRNEQ (name, ".gnu.warning."))
4ad4eba5 4126 {
db6a5d5f
AM
4127 char *msg;
4128 bfd_size_type sz;
4129
4130 name += sizeof ".gnu.warning." - 1;
4131
4132 /* If this is a shared object, then look up the symbol
4133 in the hash table. If it is there, and it is already
4134 been defined, then we will not be using the entry
4135 from this shared object, so we don't need to warn.
4136 FIXME: If we see the definition in a regular object
4137 later on, we will warn, but we shouldn't. The only
4138 fix is to keep track of what warnings we are supposed
4139 to emit, and then handle them all at the end of the
4140 link. */
4141 if (dynamic)
4ad4eba5 4142 {
db6a5d5f
AM
4143 struct elf_link_hash_entry *h;
4144
4145 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
4146
4147 /* FIXME: What about bfd_link_hash_common? */
4148 if (h != NULL
4149 && (h->root.type == bfd_link_hash_defined
4150 || h->root.type == bfd_link_hash_defweak))
4151 continue;
4152 }
4ad4eba5 4153
db6a5d5f
AM
4154 sz = s->size;
4155 msg = (char *) bfd_alloc (abfd, sz + 1);
4156 if (msg == NULL)
4157 goto error_return;
4ad4eba5 4158
db6a5d5f
AM
4159 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
4160 goto error_return;
4ad4eba5 4161
db6a5d5f 4162 msg[sz] = '\0';
4ad4eba5 4163
db6a5d5f
AM
4164 if (! (_bfd_generic_link_add_one_symbol
4165 (info, abfd, name, BSF_WARNING, s, 0, msg,
4166 FALSE, bed->collect, NULL)))
4167 goto error_return;
4ad4eba5 4168
0e1862bb 4169 if (bfd_link_executable (info))
db6a5d5f
AM
4170 {
4171 /* Clobber the section size so that the warning does
4172 not get copied into the output file. */
4173 s->size = 0;
11d2f718 4174
db6a5d5f
AM
4175 /* Also set SEC_EXCLUDE, so that symbols defined in
4176 the warning section don't get copied to the output. */
4177 s->flags |= SEC_EXCLUDE;
4ad4eba5
AM
4178 }
4179 }
4180 }
4181
29a9f53e
L
4182 just_syms = ((s = abfd->sections) != NULL
4183 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4184
4ad4eba5
AM
4185 add_needed = TRUE;
4186 if (! dynamic)
4187 {
4188 /* If we are creating a shared library, create all the dynamic
4189 sections immediately. We need to attach them to something,
4190 so we attach them to this BFD, provided it is the right
bf89386a
L
4191 format and is not from ld --just-symbols. Always create the
4192 dynamic sections for -E/--dynamic-list. FIXME: If there
29a9f53e
L
4193 are no input BFD's of the same format as the output, we can't
4194 make a shared library. */
4195 if (!just_syms
bf89386a 4196 && (bfd_link_pic (info)
9c1d7a08 4197 || (!bfd_link_relocatable (info)
3c5fce9b 4198 && info->nointerp
9c1d7a08 4199 && (info->export_dynamic || info->dynamic)))
66eb6687 4200 && is_elf_hash_table (htab)
f13a99db 4201 && info->output_bfd->xvec == abfd->xvec
66eb6687 4202 && !htab->dynamic_sections_created)
4ad4eba5
AM
4203 {
4204 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4205 goto error_return;
4206 }
4207 }
66eb6687 4208 else if (!is_elf_hash_table (htab))
4ad4eba5
AM
4209 goto error_return;
4210 else
4211 {
4ad4eba5 4212 const char *soname = NULL;
7ee314fa 4213 char *audit = NULL;
4ad4eba5 4214 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
9acc85a6 4215 const Elf_Internal_Phdr *phdr;
e310298c 4216 struct elf_link_loaded_list *loaded_lib;
4ad4eba5
AM
4217
4218 /* ld --just-symbols and dynamic objects don't mix very well.
92fd189d 4219 ld shouldn't allow it. */
29a9f53e 4220 if (just_syms)
92fd189d 4221 abort ();
4ad4eba5
AM
4222
4223 /* If this dynamic lib was specified on the command line with
4224 --as-needed in effect, then we don't want to add a DT_NEEDED
4225 tag unless the lib is actually used. Similary for libs brought
e56f61be
L
4226 in by another lib's DT_NEEDED. When --no-add-needed is used
4227 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4228 any dynamic library in DT_NEEDED tags in the dynamic lib at
4229 all. */
4230 add_needed = (elf_dyn_lib_class (abfd)
4231 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4232 | DYN_NO_NEEDED)) == 0;
4ad4eba5
AM
4233
4234 s = bfd_get_section_by_name (abfd, ".dynamic");
4235 if (s != NULL)
4236 {
4237 bfd_byte *dynbuf;
4238 bfd_byte *extdyn;
cb33740c 4239 unsigned int elfsec;
4ad4eba5
AM
4240 unsigned long shlink;
4241
eea6121a 4242 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
f8703194 4243 {
dc1e8a47 4244 error_free_dyn:
f8703194
L
4245 free (dynbuf);
4246 goto error_return;
4247 }
4ad4eba5
AM
4248
4249 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 4250 if (elfsec == SHN_BAD)
4ad4eba5
AM
4251 goto error_free_dyn;
4252 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4253
4254 for (extdyn = dynbuf;
9bff840e 4255 extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
4ad4eba5
AM
4256 extdyn += bed->s->sizeof_dyn)
4257 {
4258 Elf_Internal_Dyn dyn;
4259
4260 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4261 if (dyn.d_tag == DT_SONAME)
4262 {
4263 unsigned int tagv = dyn.d_un.d_val;
4264 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4265 if (soname == NULL)
4266 goto error_free_dyn;
4267 }
4268 if (dyn.d_tag == DT_NEEDED)
4269 {
4270 struct bfd_link_needed_list *n, **pn;
4271 char *fnm, *anm;
4272 unsigned int tagv = dyn.d_un.d_val;
986f0783 4273 size_t amt = sizeof (struct bfd_link_needed_list);
4ad4eba5 4274
a50b1753 4275 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4276 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4277 if (n == NULL || fnm == NULL)
4278 goto error_free_dyn;
4279 amt = strlen (fnm) + 1;
a50b1753 4280 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4281 if (anm == NULL)
4282 goto error_free_dyn;
4283 memcpy (anm, fnm, amt);
4284 n->name = anm;
4285 n->by = abfd;
4286 n->next = NULL;
66eb6687 4287 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4288 ;
4289 *pn = n;
4290 }
4291 if (dyn.d_tag == DT_RUNPATH)
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
AM
4304 if (anm == NULL)
4305 goto error_free_dyn;
4306 memcpy (anm, fnm, amt);
4307 n->name = anm;
4308 n->by = abfd;
4309 n->next = NULL;
4310 for (pn = & runpath;
4311 *pn != NULL;
4312 pn = &(*pn)->next)
4313 ;
4314 *pn = n;
4315 }
4316 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4317 if (!runpath && dyn.d_tag == DT_RPATH)
4318 {
4319 struct bfd_link_needed_list *n, **pn;
4320 char *fnm, *anm;
4321 unsigned int tagv = dyn.d_un.d_val;
986f0783 4322 size_t amt = sizeof (struct bfd_link_needed_list);
4ad4eba5 4323
a50b1753 4324 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4ad4eba5
AM
4325 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4326 if (n == NULL || fnm == NULL)
4327 goto error_free_dyn;
4328 amt = strlen (fnm) + 1;
a50b1753 4329 anm = (char *) bfd_alloc (abfd, amt);
4ad4eba5 4330 if (anm == NULL)
f8703194 4331 goto error_free_dyn;
4ad4eba5
AM
4332 memcpy (anm, fnm, amt);
4333 n->name = anm;
4334 n->by = abfd;
4335 n->next = NULL;
4336 for (pn = & rpath;
4337 *pn != NULL;
4338 pn = &(*pn)->next)
4339 ;
4340 *pn = n;
4341 }
7ee314fa
AM
4342 if (dyn.d_tag == DT_AUDIT)
4343 {
4344 unsigned int tagv = dyn.d_un.d_val;
4345 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4346 }
4ad4eba5
AM
4347 }
4348
4349 free (dynbuf);
4350 }
4351
4352 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4353 frees all more recently bfd_alloc'd blocks as well. */
4354 if (runpath)
4355 rpath = runpath;
4356
4357 if (rpath)
4358 {
4359 struct bfd_link_needed_list **pn;
66eb6687 4360 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4ad4eba5
AM
4361 ;
4362 *pn = rpath;
4363 }
4364
9acc85a6
AM
4365 /* If we have a PT_GNU_RELRO program header, mark as read-only
4366 all sections contained fully therein. This makes relro
4367 shared library sections appear as they will at run-time. */
4368 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
54025d58 4369 while (phdr-- > elf_tdata (abfd)->phdr)
9acc85a6
AM
4370 if (phdr->p_type == PT_GNU_RELRO)
4371 {
4372 for (s = abfd->sections; s != NULL; s = s->next)
502794d4
CE
4373 {
4374 unsigned int opb = bfd_octets_per_byte (abfd, s);
4375
4376 if ((s->flags & SEC_ALLOC) != 0
4377 && s->vma * opb >= phdr->p_vaddr
4378 && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
4379 s->flags |= SEC_READONLY;
4380 }
9acc85a6
AM
4381 break;
4382 }
4383
4ad4eba5
AM
4384 /* We do not want to include any of the sections in a dynamic
4385 object in the output file. We hack by simply clobbering the
4386 list of sections in the BFD. This could be handled more
4387 cleanly by, say, a new section flag; the existing
4388 SEC_NEVER_LOAD flag is not the one we want, because that one
4389 still implies that the section takes up space in the output
4390 file. */
4391 bfd_section_list_clear (abfd);
4392
4ad4eba5
AM
4393 /* Find the name to use in a DT_NEEDED entry that refers to this
4394 object. If the object has a DT_SONAME entry, we use it.
4395 Otherwise, if the generic linker stuck something in
4396 elf_dt_name, we use that. Otherwise, we just use the file
4397 name. */
4398 if (soname == NULL || *soname == '\0')
4399 {
4400 soname = elf_dt_name (abfd);
4401 if (soname == NULL || *soname == '\0')
4402 soname = bfd_get_filename (abfd);
4403 }
4404
4405 /* Save the SONAME because sometimes the linker emulation code
4406 will need to know it. */
4407 elf_dt_name (abfd) = soname;
4408
4ad4eba5
AM
4409 /* If we have already included this dynamic object in the
4410 link, just ignore it. There is no reason to include a
4411 particular dynamic object more than once. */
e310298c
AM
4412 for (loaded_lib = htab->dyn_loaded;
4413 loaded_lib != NULL;
4414 loaded_lib = loaded_lib->next)
4415 {
4416 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
4417 return TRUE;
4418 }
4419
4420 /* Create dynamic sections for backends that require that be done
4421 before setup_gnu_properties. */
4422 if (add_needed
4423 && !_bfd_elf_link_create_dynamic_sections (abfd, info))
4424 return FALSE;
7ee314fa
AM
4425
4426 /* Save the DT_AUDIT entry for the linker emulation code. */
68ffbac6 4427 elf_dt_audit (abfd) = audit;
4ad4eba5
AM
4428 }
4429
4430 /* If this is a dynamic object, we always link against the .dynsym
4431 symbol table, not the .symtab symbol table. The dynamic linker
4432 will only see the .dynsym symbol table, so there is no reason to
4433 look at .symtab for a dynamic object. */
4434
4435 if (! dynamic || elf_dynsymtab (abfd) == 0)
4436 hdr = &elf_tdata (abfd)->symtab_hdr;
4437 else
4438 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4439
4440 symcount = hdr->sh_size / bed->s->sizeof_sym;
4441
4442 /* The sh_info field of the symtab header tells us where the
4443 external symbols start. We don't care about the local symbols at
4444 this point. */
4445 if (elf_bad_symtab (abfd))
4446 {
4447 extsymcount = symcount;
4448 extsymoff = 0;
4449 }
4450 else
4451 {
4452 extsymcount = symcount - hdr->sh_info;
4453 extsymoff = hdr->sh_info;
4454 }
4455
f45794cb 4456 sym_hash = elf_sym_hashes (abfd);
012b2306 4457 if (extsymcount != 0)
4ad4eba5
AM
4458 {
4459 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4460 NULL, NULL, NULL);
4461 if (isymbuf == NULL)
4462 goto error_return;
4463
4ad4eba5 4464 if (sym_hash == NULL)
012b2306
AM
4465 {
4466 /* We store a pointer to the hash table entry for each
4467 external symbol. */
986f0783 4468 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
012b2306
AM
4469 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4470 if (sym_hash == NULL)
4471 goto error_free_sym;
4472 elf_sym_hashes (abfd) = sym_hash;
4473 }
4ad4eba5
AM
4474 }
4475
4476 if (dynamic)
4477 {
4478 /* Read in any version definitions. */
fc0e6df6
PB
4479 if (!_bfd_elf_slurp_version_tables (abfd,
4480 info->default_imported_symver))
4ad4eba5
AM
4481 goto error_free_sym;
4482
4483 /* Read in the symbol versions, but don't bother to convert them
4484 to internal format. */
4485 if (elf_dynversym (abfd) != 0)
4486 {
986f0783
AM
4487 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4488 bfd_size_type amt = versymhdr->sh_size;
4ad4eba5 4489
2bb3687b
AM
4490 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
4491 goto error_free_sym;
4492 extversym = (Elf_External_Versym *)
4493 _bfd_malloc_and_read (abfd, amt, amt);
4ad4eba5
AM
4494 if (extversym == NULL)
4495 goto error_free_sym;
986f0783 4496 extversym_end = extversym + amt / sizeof (*extversym);
4ad4eba5
AM
4497 }
4498 }
4499
66eb6687
AM
4500 /* If we are loading an as-needed shared lib, save the symbol table
4501 state before we start adding symbols. If the lib turns out
4502 to be unneeded, restore the state. */
4503 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4504 {
4505 unsigned int i;
4506 size_t entsize;
4507
4508 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4509 {
4510 struct bfd_hash_entry *p;
2de92251 4511 struct elf_link_hash_entry *h;
66eb6687
AM
4512
4513 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
2de92251
AM
4514 {
4515 h = (struct elf_link_hash_entry *) p;
4516 entsize += htab->root.table.entsize;
4517 if (h->root.type == bfd_link_hash_warning)
7ba11550
AM
4518 {
4519 entsize += htab->root.table.entsize;
4520 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4521 }
4522 if (h->root.type == bfd_link_hash_common)
4523 entsize += sizeof (*h->root.u.c.p);
2de92251 4524 }
66eb6687
AM
4525 }
4526
4527 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
f45794cb 4528 old_tab = bfd_malloc (tabsize + entsize);
66eb6687
AM
4529 if (old_tab == NULL)
4530 goto error_free_vers;
4531
4532 /* Remember the current objalloc pointer, so that all mem for
4533 symbols added can later be reclaimed. */
4534 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4535 if (alloc_mark == NULL)
4536 goto error_free_vers;
4537
5061a885
AM
4538 /* Make a special call to the linker "notice" function to
4539 tell it that we are about to handle an as-needed lib. */
e5034e59 4540 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
9af2a943 4541 goto error_free_vers;
5061a885 4542
f45794cb
AM
4543 /* Clone the symbol table. Remember some pointers into the
4544 symbol table, and dynamic symbol count. */
4545 old_ent = (char *) old_tab + tabsize;
66eb6687 4546 memcpy (old_tab, htab->root.table.table, tabsize);
66eb6687
AM
4547 old_undefs = htab->root.undefs;
4548 old_undefs_tail = htab->root.undefs_tail;
4f87808c
AM
4549 old_table = htab->root.table.table;
4550 old_size = htab->root.table.size;
4551 old_count = htab->root.table.count;
e310298c
AM
4552 old_strtab = NULL;
4553 if (htab->dynstr != NULL)
4554 {
4555 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4556 if (old_strtab == NULL)
4557 goto error_free_vers;
4558 }
66eb6687
AM
4559
4560 for (i = 0; i < htab->root.table.size; i++)
4561 {
4562 struct bfd_hash_entry *p;
2de92251 4563 struct elf_link_hash_entry *h;
66eb6687
AM
4564
4565 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4566 {
2de92251 4567 h = (struct elf_link_hash_entry *) p;
7ba11550
AM
4568 memcpy (old_ent, h, htab->root.table.entsize);
4569 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
4570 if (h->root.type == bfd_link_hash_warning)
4571 {
7ba11550
AM
4572 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4573 memcpy (old_ent, h, htab->root.table.entsize);
2de92251
AM
4574 old_ent = (char *) old_ent + htab->root.table.entsize;
4575 }
7ba11550
AM
4576 if (h->root.type == bfd_link_hash_common)
4577 {
4578 memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
4579 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
4580 }
66eb6687
AM
4581 }
4582 }
4583 }
4ad4eba5 4584
66eb6687 4585 weaks = NULL;
be22c732
NC
4586 if (extversym == NULL)
4587 ever = NULL;
4588 else if (extversym + extsymoff < extversym_end)
4589 ever = extversym + extsymoff;
4590 else
4591 {
4592 /* xgettext:c-format */
4593 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4594 abfd, (long) extsymoff,
4595 (long) (extversym_end - extversym) / sizeof (* extversym));
4596 bfd_set_error (bfd_error_bad_value);
4597 goto error_free_vers;
4598 }
4599
b4c555cf
ML
4600 if (!bfd_link_relocatable (info)
4601 && abfd->lto_slim_object)
cc5277b1
ML
4602 {
4603 _bfd_error_handler
4604 (_("%pB: plugin needed to handle lto object"), abfd);
4605 }
4606
4ad4eba5
AM
4607 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4608 isym < isymend;
4609 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4610 {
4611 int bind;
4612 bfd_vma value;
af44c138 4613 asection *sec, *new_sec;
4ad4eba5
AM
4614 flagword flags;
4615 const char *name;
4616 struct elf_link_hash_entry *h;
90c984fc 4617 struct elf_link_hash_entry *hi;
4ad4eba5
AM
4618 bfd_boolean definition;
4619 bfd_boolean size_change_ok;
4620 bfd_boolean type_change_ok;
37a9e49a
L
4621 bfd_boolean new_weak;
4622 bfd_boolean old_weak;
7ba11550 4623 bfd *override;
a4d8e49b 4624 bfd_boolean common;
97196564 4625 bfd_boolean discarded;
4ad4eba5 4626 unsigned int old_alignment;
4538d1c7 4627 unsigned int shindex;
4ad4eba5 4628 bfd *old_bfd;
6e33951e 4629 bfd_boolean matched;
4ad4eba5 4630
7ba11550 4631 override = NULL;
4ad4eba5
AM
4632
4633 flags = BSF_NO_FLAGS;
4634 sec = NULL;
4635 value = isym->st_value;
a4d8e49b 4636 common = bed->common_definition (isym);
2980ccad
L
4637 if (common && info->inhibit_common_definition)
4638 {
4639 /* Treat common symbol as undefined for --no-define-common. */
4640 isym->st_shndx = SHN_UNDEF;
4641 common = FALSE;
4642 }
97196564 4643 discarded = FALSE;
4ad4eba5
AM
4644
4645 bind = ELF_ST_BIND (isym->st_info);
3e7a7d11 4646 switch (bind)
4ad4eba5 4647 {
3e7a7d11 4648 case STB_LOCAL:
4ad4eba5
AM
4649 /* This should be impossible, since ELF requires that all
4650 global symbols follow all local symbols, and that sh_info
4651 point to the first global symbol. Unfortunately, Irix 5
4652 screws this up. */
fe3fef62
AM
4653 if (elf_bad_symtab (abfd))
4654 continue;
4655
4656 /* If we aren't prepared to handle locals within the globals
4538d1c7
AM
4657 then we'll likely segfault on a NULL symbol hash if the
4658 symbol is ever referenced in relocations. */
4659 shindex = elf_elfheader (abfd)->e_shstrndx;
4660 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4661 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4662 " (>= sh_info of %lu)"),
4663 abfd, name, (long) (isym - isymbuf + extsymoff),
4664 (long) extsymoff);
4665
4666 /* Dynamic object relocations are not processed by ld, so
4667 ld won't run into the problem mentioned above. */
4668 if (dynamic)
4669 continue;
fe3fef62
AM
4670 bfd_set_error (bfd_error_bad_value);
4671 goto error_free_vers;
3e7a7d11
NC
4672
4673 case STB_GLOBAL:
a4d8e49b 4674 if (isym->st_shndx != SHN_UNDEF && !common)
4ad4eba5 4675 flags = BSF_GLOBAL;
3e7a7d11
NC
4676 break;
4677
4678 case STB_WEAK:
4679 flags = BSF_WEAK;
4680 break;
4681
4682 case STB_GNU_UNIQUE:
4683 flags = BSF_GNU_UNIQUE;
4684 break;
4685
4686 default:
4ad4eba5 4687 /* Leave it up to the processor backend. */
3e7a7d11 4688 break;
4ad4eba5
AM
4689 }
4690
4691 if (isym->st_shndx == SHN_UNDEF)
4692 sec = bfd_und_section_ptr;
cb33740c
AM
4693 else if (isym->st_shndx == SHN_ABS)
4694 sec = bfd_abs_section_ptr;
4695 else if (isym->st_shndx == SHN_COMMON)
4696 {
4697 sec = bfd_com_section_ptr;
4698 /* What ELF calls the size we call the value. What ELF
4699 calls the value we call the alignment. */
4700 value = isym->st_size;
4701 }
4702 else
4ad4eba5
AM
4703 {
4704 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4705 if (sec == NULL)
4706 sec = bfd_abs_section_ptr;
dbaa2011 4707 else if (discarded_section (sec))
529fcb95 4708 {
e5d08002
L
4709 /* Symbols from discarded section are undefined. We keep
4710 its visibility. */
529fcb95 4711 sec = bfd_und_section_ptr;
97196564 4712 discarded = TRUE;
529fcb95
PB
4713 isym->st_shndx = SHN_UNDEF;
4714 }
4ad4eba5
AM
4715 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4716 value -= sec->vma;
4717 }
4ad4eba5
AM
4718
4719 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4720 isym->st_name);
4721 if (name == NULL)
4722 goto error_free_vers;
4723
4724 if (isym->st_shndx == SHN_COMMON
02d00247
AM
4725 && (abfd->flags & BFD_PLUGIN) != 0)
4726 {
4727 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4728
4729 if (xc == NULL)
4730 {
4731 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4732 | SEC_EXCLUDE);
4733 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4734 if (xc == NULL)
4735 goto error_free_vers;
4736 }
4737 sec = xc;
4738 }
4739 else if (isym->st_shndx == SHN_COMMON
4740 && ELF_ST_TYPE (isym->st_info) == STT_TLS
0e1862bb 4741 && !bfd_link_relocatable (info))
4ad4eba5
AM
4742 {
4743 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4744
4745 if (tcomm == NULL)
4746 {
02d00247
AM
4747 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4748 | SEC_LINKER_CREATED);
4749 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3496cb2a 4750 if (tcomm == NULL)
4ad4eba5
AM
4751 goto error_free_vers;
4752 }
4753 sec = tcomm;
4754 }
66eb6687 4755 else if (bed->elf_add_symbol_hook)
4ad4eba5 4756 {
66eb6687
AM
4757 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4758 &sec, &value))
4ad4eba5
AM
4759 goto error_free_vers;
4760
4761 /* The hook function sets the name to NULL if this symbol
4762 should be skipped for some reason. */
4763 if (name == NULL)
4764 continue;
4765 }
4766
4767 /* Sanity check that all possibilities were handled. */
4768 if (sec == NULL)
4538d1c7 4769 abort ();
4ad4eba5 4770
191c0c42
AM
4771 /* Silently discard TLS symbols from --just-syms. There's
4772 no way to combine a static TLS block with a new TLS block
4773 for this executable. */
4774 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4775 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4776 continue;
4777
4ad4eba5
AM
4778 if (bfd_is_und_section (sec)
4779 || bfd_is_com_section (sec))
4780 definition = FALSE;
4781 else
4782 definition = TRUE;
4783
4784 size_change_ok = FALSE;
66eb6687 4785 type_change_ok = bed->type_change_ok;
37a9e49a 4786 old_weak = FALSE;
6e33951e 4787 matched = FALSE;
4ad4eba5
AM
4788 old_alignment = 0;
4789 old_bfd = NULL;
af44c138 4790 new_sec = sec;
4ad4eba5 4791
66eb6687 4792 if (is_elf_hash_table (htab))
4ad4eba5
AM
4793 {
4794 Elf_Internal_Versym iver;
4795 unsigned int vernum = 0;
4796 bfd_boolean skip;
4797
fc0e6df6 4798 if (ever == NULL)
4ad4eba5 4799 {
fc0e6df6
PB
4800 if (info->default_imported_symver)
4801 /* Use the default symbol version created earlier. */
4802 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4803 else
4804 iver.vs_vers = 0;
4805 }
be22c732
NC
4806 else if (ever >= extversym_end)
4807 {
4808 /* xgettext:c-format */
4809 _bfd_error_handler (_("%pB: not enough version information"),
4810 abfd);
4811 bfd_set_error (bfd_error_bad_value);
4812 goto error_free_vers;
4813 }
fc0e6df6
PB
4814 else
4815 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4816
4817 vernum = iver.vs_vers & VERSYM_VERSION;
4818
4819 /* If this is a hidden symbol, or if it is not version
4820 1, we append the version name to the symbol name.
cc86ff91
EB
4821 However, we do not modify a non-hidden absolute symbol
4822 if it is not a function, because it might be the version
4823 symbol itself. FIXME: What if it isn't? */
fc0e6df6 4824 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
fcb93ecf
PB
4825 || (vernum > 1
4826 && (!bfd_is_abs_section (sec)
4827 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
fc0e6df6
PB
4828 {
4829 const char *verstr;
4830 size_t namelen, verlen, newlen;
4831 char *newname, *p;
4832
4833 if (isym->st_shndx != SHN_UNDEF)
4ad4eba5 4834 {
fc0e6df6
PB
4835 if (vernum > elf_tdata (abfd)->cverdefs)
4836 verstr = NULL;
4837 else if (vernum > 1)
4838 verstr =
4839 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4840 else
4841 verstr = "";
4ad4eba5 4842
fc0e6df6 4843 if (verstr == NULL)
4ad4eba5 4844 {
4eca0228 4845 _bfd_error_handler
695344c0 4846 /* xgettext:c-format */
871b3ab2 4847 (_("%pB: %s: invalid version %u (max %d)"),
fc0e6df6
PB
4848 abfd, name, vernum,
4849 elf_tdata (abfd)->cverdefs);
4850 bfd_set_error (bfd_error_bad_value);
4851 goto error_free_vers;
4ad4eba5 4852 }
fc0e6df6
PB
4853 }
4854 else
4855 {
4856 /* We cannot simply test for the number of
4857 entries in the VERNEED section since the
4858 numbers for the needed versions do not start
4859 at 0. */
4860 Elf_Internal_Verneed *t;
4861
4862 verstr = NULL;
4863 for (t = elf_tdata (abfd)->verref;
4864 t != NULL;
4865 t = t->vn_nextref)
4ad4eba5 4866 {
fc0e6df6 4867 Elf_Internal_Vernaux *a;
4ad4eba5 4868
fc0e6df6
PB
4869 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4870 {
4871 if (a->vna_other == vernum)
4ad4eba5 4872 {
fc0e6df6
PB
4873 verstr = a->vna_nodename;
4874 break;
4ad4eba5 4875 }
4ad4eba5 4876 }
fc0e6df6
PB
4877 if (a != NULL)
4878 break;
4879 }
4880 if (verstr == NULL)
4881 {
4eca0228 4882 _bfd_error_handler
695344c0 4883 /* xgettext:c-format */
871b3ab2 4884 (_("%pB: %s: invalid needed version %d"),
fc0e6df6
PB
4885 abfd, name, vernum);
4886 bfd_set_error (bfd_error_bad_value);
4887 goto error_free_vers;
4ad4eba5 4888 }
4ad4eba5 4889 }
fc0e6df6
PB
4890
4891 namelen = strlen (name);
4892 verlen = strlen (verstr);
4893 newlen = namelen + verlen + 2;
4894 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4895 && isym->st_shndx != SHN_UNDEF)
4896 ++newlen;
4897
a50b1753 4898 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
fc0e6df6
PB
4899 if (newname == NULL)
4900 goto error_free_vers;
4901 memcpy (newname, name, namelen);
4902 p = newname + namelen;
4903 *p++ = ELF_VER_CHR;
4904 /* If this is a defined non-hidden version symbol,
4905 we add another @ to the name. This indicates the
4906 default version of the symbol. */
4907 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4908 && isym->st_shndx != SHN_UNDEF)
4909 *p++ = ELF_VER_CHR;
4910 memcpy (p, verstr, verlen + 1);
4911
4912 name = newname;
4ad4eba5
AM
4913 }
4914
cd3416da
AM
4915 /* If this symbol has default visibility and the user has
4916 requested we not re-export it, then mark it as hidden. */
a0d49154 4917 if (!bfd_is_und_section (sec)
cd3416da 4918 && !dynamic
ce875075 4919 && abfd->no_export
cd3416da
AM
4920 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4921 isym->st_other = (STV_HIDDEN
4922 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4923
4f3fedcf
AM
4924 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4925 sym_hash, &old_bfd, &old_weak,
4926 &old_alignment, &skip, &override,
6e33951e
L
4927 &type_change_ok, &size_change_ok,
4928 &matched))
4ad4eba5
AM
4929 goto error_free_vers;
4930
4931 if (skip)
4932 continue;
4933
6e33951e
L
4934 /* Override a definition only if the new symbol matches the
4935 existing one. */
4936 if (override && matched)
4ad4eba5
AM
4937 definition = FALSE;
4938
4939 h = *sym_hash;
4940 while (h->root.type == bfd_link_hash_indirect
4941 || h->root.type == bfd_link_hash_warning)
4942 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4943
4ad4eba5 4944 if (elf_tdata (abfd)->verdef != NULL
4ad4eba5
AM
4945 && vernum > 1
4946 && definition)
4947 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4948 }
4949
4950 if (! (_bfd_generic_link_add_one_symbol
7ba11550
AM
4951 (info, override ? override : abfd, name, flags, sec, value,
4952 NULL, FALSE, bed->collect,
4ad4eba5
AM
4953 (struct bfd_link_hash_entry **) sym_hash)))
4954 goto error_free_vers;
4955
4956 h = *sym_hash;
90c984fc
L
4957 /* We need to make sure that indirect symbol dynamic flags are
4958 updated. */
4959 hi = h;
4ad4eba5
AM
4960 while (h->root.type == bfd_link_hash_indirect
4961 || h->root.type == bfd_link_hash_warning)
4962 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3e7a7d11 4963
97196564
L
4964 /* Setting the index to -3 tells elf_link_output_extsym that
4965 this symbol is defined in a discarded section. */
4966 if (discarded)
4967 h->indx = -3;
4968
4ad4eba5
AM
4969 *sym_hash = h;
4970
37a9e49a 4971 new_weak = (flags & BSF_WEAK) != 0;
4ad4eba5
AM
4972 if (dynamic
4973 && definition
37a9e49a 4974 && new_weak
fcb93ecf 4975 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
66eb6687 4976 && is_elf_hash_table (htab)
60d67dc8 4977 && h->u.alias == NULL)
4ad4eba5
AM
4978 {
4979 /* Keep a list of all weak defined non function symbols from
60d67dc8
AM
4980 a dynamic object, using the alias field. Later in this
4981 function we will set the alias field to the correct
4ad4eba5
AM
4982 value. We only put non-function symbols from dynamic
4983 objects on this list, because that happens to be the only
4984 time we need to know the normal symbol corresponding to a
4985 weak symbol, and the information is time consuming to
60d67dc8 4986 figure out. If the alias field is not already NULL,
4ad4eba5
AM
4987 then this symbol was already defined by some previous
4988 dynamic object, and we will be using that previous
4989 definition anyhow. */
4990
60d67dc8 4991 h->u.alias = weaks;
4ad4eba5 4992 weaks = h;
4ad4eba5
AM
4993 }
4994
4995 /* Set the alignment of a common symbol. */
a4d8e49b 4996 if ((common || bfd_is_com_section (sec))
4ad4eba5
AM
4997 && h->root.type == bfd_link_hash_common)
4998 {
4999 unsigned int align;
5000
a4d8e49b 5001 if (common)
af44c138
L
5002 align = bfd_log2 (isym->st_value);
5003 else
5004 {
5005 /* The new symbol is a common symbol in a shared object.
5006 We need to get the alignment from the section. */
5007 align = new_sec->alignment_power;
5008 }
595213d4 5009 if (align > old_alignment)
4ad4eba5
AM
5010 h->root.u.c.p->alignment_power = align;
5011 else
5012 h->root.u.c.p->alignment_power = old_alignment;
5013 }
5014
66eb6687 5015 if (is_elf_hash_table (htab))
4ad4eba5 5016 {
4f3fedcf
AM
5017 /* Set a flag in the hash table entry indicating the type of
5018 reference or definition we just found. A dynamic symbol
5019 is one which is referenced or defined by both a regular
5020 object and a shared object. */
5021 bfd_boolean dynsym = FALSE;
5022
b1a92c63
AM
5023 /* Plugin symbols aren't normal. Don't set def/ref flags. */
5024 if ((abfd->flags & BFD_PLUGIN) != 0)
5025 ;
5026 else if (!dynamic)
4f3fedcf
AM
5027 {
5028 if (! definition)
5029 {
5030 h->ref_regular = 1;
5031 if (bind != STB_WEAK)
5032 h->ref_regular_nonweak = 1;
5033 }
5034 else
5035 {
5036 h->def_regular = 1;
5037 if (h->def_dynamic)
5038 {
5039 h->def_dynamic = 0;
5040 h->ref_dynamic = 1;
5041 }
5042 }
4f3fedcf
AM
5043 }
5044 else
5045 {
5046 if (! definition)
5047 {
5048 h->ref_dynamic = 1;
5049 hi->ref_dynamic = 1;
5050 }
5051 else
5052 {
5053 h->def_dynamic = 1;
5054 hi->def_dynamic = 1;
5055 }
b1a92c63 5056 }
4f3fedcf 5057
b1a92c63
AM
5058 /* If an indirect symbol has been forced local, don't
5059 make the real symbol dynamic. */
5060 if (h != hi && hi->forced_local)
5061 ;
5062 else if (!dynamic)
5063 {
5064 if (bfd_link_dll (info)
5065 || h->def_dynamic
5066 || h->ref_dynamic)
5067 dynsym = TRUE;
5068 }
5069 else
5070 {
5071 if (h->def_regular
5072 || h->ref_regular
5073 || (h->is_weakalias
5074 && weakdef (h)->dynindx != -1))
4f3fedcf
AM
5075 dynsym = TRUE;
5076 }
5077
5078 /* Check to see if we need to add an indirect symbol for
5079 the default name. */
726d7d1e
AM
5080 if ((definition
5081 || (!override && h->root.type == bfd_link_hash_common))
5082 && !(hi != h
5083 && hi->versioned == versioned_hidden))
4f3fedcf
AM
5084 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
5085 sec, value, &old_bfd, &dynsym))
5086 goto error_free_vers;
4ad4eba5
AM
5087
5088 /* Check the alignment when a common symbol is involved. This
5089 can change when a common symbol is overridden by a normal
5090 definition or a common symbol is ignored due to the old
5091 normal definition. We need to make sure the maximum
5092 alignment is maintained. */
a4d8e49b 5093 if ((old_alignment || common)
4ad4eba5
AM
5094 && h->root.type != bfd_link_hash_common)
5095 {
5096 unsigned int common_align;
5097 unsigned int normal_align;
5098 unsigned int symbol_align;
5099 bfd *normal_bfd;
5100 bfd *common_bfd;
5101
3a81e825
AM
5102 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5103 || h->root.type == bfd_link_hash_defweak);
5104
4ad4eba5
AM
5105 symbol_align = ffs (h->root.u.def.value) - 1;
5106 if (h->root.u.def.section->owner != NULL
0616a280
AM
5107 && (h->root.u.def.section->owner->flags
5108 & (DYNAMIC | BFD_PLUGIN)) == 0)
4ad4eba5
AM
5109 {
5110 normal_align = h->root.u.def.section->alignment_power;
5111 if (normal_align > symbol_align)
5112 normal_align = symbol_align;
5113 }
5114 else
5115 normal_align = symbol_align;
5116
5117 if (old_alignment)
5118 {
5119 common_align = old_alignment;
5120 common_bfd = old_bfd;
5121 normal_bfd = abfd;
5122 }
5123 else
5124 {
5125 common_align = bfd_log2 (isym->st_value);
5126 common_bfd = abfd;
5127 normal_bfd = old_bfd;
5128 }
5129
5130 if (normal_align < common_align)
d07676f8
NC
5131 {
5132 /* PR binutils/2735 */
5133 if (normal_bfd == NULL)
4eca0228 5134 _bfd_error_handler
695344c0 5135 /* xgettext:c-format */
9793eb77 5136 (_("warning: alignment %u of common symbol `%s' in %pB is"
871b3ab2 5137 " greater than the alignment (%u) of its section %pA"),
c08bb8dd
AM
5138 1 << common_align, name, common_bfd,
5139 1 << normal_align, h->root.u.def.section);
d07676f8 5140 else
4eca0228 5141 _bfd_error_handler
695344c0 5142 /* xgettext:c-format */
9793eb77 5143 (_("warning: alignment %u of symbol `%s' in %pB"
871b3ab2 5144 " is smaller than %u in %pB"),
c08bb8dd
AM
5145 1 << normal_align, name, normal_bfd,
5146 1 << common_align, common_bfd);
d07676f8 5147 }
4ad4eba5
AM
5148 }
5149
83ad0046 5150 /* Remember the symbol size if it isn't undefined. */
3a81e825
AM
5151 if (isym->st_size != 0
5152 && isym->st_shndx != SHN_UNDEF
4ad4eba5
AM
5153 && (definition || h->size == 0))
5154 {
83ad0046
L
5155 if (h->size != 0
5156 && h->size != isym->st_size
5157 && ! size_change_ok)
4eca0228 5158 _bfd_error_handler
695344c0 5159 /* xgettext:c-format */
9793eb77 5160 (_("warning: size of symbol `%s' changed"
2dcf00ce
AM
5161 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
5162 name, (uint64_t) h->size, old_bfd,
5163 (uint64_t) isym->st_size, abfd);
4ad4eba5
AM
5164
5165 h->size = isym->st_size;
5166 }
5167
5168 /* If this is a common symbol, then we always want H->SIZE
5169 to be the size of the common symbol. The code just above
5170 won't fix the size if a common symbol becomes larger. We
5171 don't warn about a size change here, because that is
4f3fedcf 5172 covered by --warn-common. Allow changes between different
fcb93ecf 5173 function types. */
4ad4eba5
AM
5174 if (h->root.type == bfd_link_hash_common)
5175 h->size = h->root.u.c.size;
5176
5177 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
37a9e49a
L
5178 && ((definition && !new_weak)
5179 || (old_weak && h->root.type == bfd_link_hash_common)
5180 || h->type == STT_NOTYPE))
4ad4eba5 5181 {
2955ec4c
L
5182 unsigned int type = ELF_ST_TYPE (isym->st_info);
5183
5184 /* Turn an IFUNC symbol from a DSO into a normal FUNC
5185 symbol. */
5186 if (type == STT_GNU_IFUNC
5187 && (abfd->flags & DYNAMIC) != 0)
5188 type = STT_FUNC;
4ad4eba5 5189
2955ec4c
L
5190 if (h->type != type)
5191 {
5192 if (h->type != STT_NOTYPE && ! type_change_ok)
695344c0 5193 /* xgettext:c-format */
4eca0228 5194 _bfd_error_handler
9793eb77 5195 (_("warning: type of symbol `%s' changed"
871b3ab2 5196 " from %d to %d in %pB"),
c08bb8dd 5197 name, h->type, type, abfd);
2955ec4c
L
5198
5199 h->type = type;
5200 }
4ad4eba5
AM
5201 }
5202
54ac0771 5203 /* Merge st_other field. */
5160d0f3
AM
5204 elf_merge_st_other (abfd, h, isym->st_other, sec,
5205 definition, dynamic);
4ad4eba5 5206
c3df8c14 5207 /* We don't want to make debug symbol dynamic. */
0e1862bb
L
5208 if (definition
5209 && (sec->flags & SEC_DEBUGGING)
5210 && !bfd_link_relocatable (info))
c3df8c14
AM
5211 dynsym = FALSE;
5212
b1a92c63
AM
5213 /* Nor should we make plugin symbols dynamic. */
5214 if ((abfd->flags & BFD_PLUGIN) != 0)
5215 dynsym = FALSE;
5216
35fc36a8 5217 if (definition)
35399224
L
5218 {
5219 h->target_internal = isym->st_target_internal;
5220 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5221 }
35fc36a8 5222
4ad4eba5
AM
5223 if (definition && !dynamic)
5224 {
5225 char *p = strchr (name, ELF_VER_CHR);
5226 if (p != NULL && p[1] != ELF_VER_CHR)
5227 {
5228 /* Queue non-default versions so that .symver x, x@FOO
5229 aliases can be checked. */
66eb6687 5230 if (!nondeflt_vers)
4ad4eba5 5231 {
986f0783
AM
5232 size_t amt = ((isymend - isym + 1)
5233 * sizeof (struct elf_link_hash_entry *));
ca4be51c
AM
5234 nondeflt_vers
5235 = (struct elf_link_hash_entry **) bfd_malloc (amt);
14b1c01e
AM
5236 if (!nondeflt_vers)
5237 goto error_free_vers;
4ad4eba5 5238 }
66eb6687 5239 nondeflt_vers[nondeflt_vers_cnt++] = h;
4ad4eba5
AM
5240 }
5241 }
5242
b1a92c63 5243 if (dynsym && h->dynindx == -1)
4ad4eba5 5244 {
c152c796 5245 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4ad4eba5 5246 goto error_free_vers;
60d67dc8
AM
5247 if (h->is_weakalias
5248 && weakdef (h)->dynindx == -1)
4ad4eba5 5249 {
60d67dc8 5250 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
4ad4eba5
AM
5251 goto error_free_vers;
5252 }
5253 }
1f599d0e 5254 else if (h->dynindx != -1)
4ad4eba5
AM
5255 /* If the symbol already has a dynamic index, but
5256 visibility says it should not be visible, turn it into
5257 a local symbol. */
5258 switch (ELF_ST_VISIBILITY (h->other))
5259 {
5260 case STV_INTERNAL:
5261 case STV_HIDDEN:
5262 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5263 dynsym = FALSE;
5264 break;
5265 }
5266
5267 if (!add_needed
aef28989 5268 && matched
4ad4eba5 5269 && definition
010e5ae2 5270 && ((dynsym
a896df97 5271 && h->ref_regular_nonweak)
b1a92c63
AM
5272 || (old_bfd != NULL
5273 && (old_bfd->flags & BFD_PLUGIN) != 0
5274 && bind != STB_WEAK)
ffa9430d 5275 || (h->ref_dynamic_nonweak
010e5ae2 5276 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
7b15fa7a
AM
5277 && !on_needed_list (elf_dt_name (abfd),
5278 htab->needed, NULL))))
4ad4eba5 5279 {
4ad4eba5
AM
5280 const char *soname = elf_dt_name (abfd);
5281
16e4ecc0
AM
5282 info->callbacks->minfo ("%!", soname, old_bfd,
5283 h->root.root.string);
5284
4ad4eba5
AM
5285 /* A symbol from a library loaded via DT_NEEDED of some
5286 other library is referenced by a regular object.
e56f61be 5287 Add a DT_NEEDED entry for it. Issue an error if
b918acf9
NC
5288 --no-add-needed is used and the reference was not
5289 a weak one. */
4f3fedcf 5290 if (old_bfd != NULL
b918acf9 5291 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
e56f61be 5292 {
4eca0228 5293 _bfd_error_handler
695344c0 5294 /* xgettext:c-format */
871b3ab2 5295 (_("%pB: undefined reference to symbol '%s'"),
4f3fedcf 5296 old_bfd, name);
ff5ac77b 5297 bfd_set_error (bfd_error_missing_dso);
e56f61be
L
5298 goto error_free_vers;
5299 }
5300
a50b1753 5301 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
ca4be51c 5302 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
a5db907e 5303
e310298c
AM
5304 /* Create dynamic sections for backends that require
5305 that be done before setup_gnu_properties. */
5306 if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
5307 return FALSE;
4ad4eba5 5308 add_needed = TRUE;
4ad4eba5
AM
5309 }
5310 }
5311 }
5312
a83ef4d1
L
5313 if (info->lto_plugin_active
5314 && !bfd_link_relocatable (info)
5315 && (abfd->flags & BFD_PLUGIN) == 0
5316 && !just_syms
5317 && extsymcount)
5318 {
5319 int r_sym_shift;
5320
5321 if (bed->s->arch_size == 32)
5322 r_sym_shift = 8;
5323 else
5324 r_sym_shift = 32;
5325
5326 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5327 referenced in regular objects so that linker plugin will get
5328 the correct symbol resolution. */
5329
5330 sym_hash = elf_sym_hashes (abfd);
5331 for (s = abfd->sections; s != NULL; s = s->next)
5332 {
5333 Elf_Internal_Rela *internal_relocs;
5334 Elf_Internal_Rela *rel, *relend;
5335
5336 /* Don't check relocations in excluded sections. */
5337 if ((s->flags & SEC_RELOC) == 0
5338 || s->reloc_count == 0
5339 || (s->flags & SEC_EXCLUDE) != 0
5340 || ((info->strip == strip_all
5341 || info->strip == strip_debugger)
5342 && (s->flags & SEC_DEBUGGING) != 0))
5343 continue;
5344
5345 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5346 NULL,
5347 info->keep_memory);
5348 if (internal_relocs == NULL)
5349 goto error_free_vers;
5350
5351 rel = internal_relocs;
5352 relend = rel + s->reloc_count;
5353 for ( ; rel < relend; rel++)
5354 {
5355 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5356 struct elf_link_hash_entry *h;
5357
5358 /* Skip local symbols. */
5359 if (r_symndx < extsymoff)
5360 continue;
5361
5362 h = sym_hash[r_symndx - extsymoff];
5363 if (h != NULL)
5364 h->root.non_ir_ref_regular = 1;
5365 }
5366
5367 if (elf_section_data (s)->relocs != internal_relocs)
5368 free (internal_relocs);
5369 }
5370 }
5371
c9594989
AM
5372 free (extversym);
5373 extversym = NULL;
5374 free (isymbuf);
5375 isymbuf = NULL;
66eb6687
AM
5376
5377 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5378 {
5379 unsigned int i;
5380
5381 /* Restore the symbol table. */
f45794cb
AM
5382 old_ent = (char *) old_tab + tabsize;
5383 memset (elf_sym_hashes (abfd), 0,
5384 extsymcount * sizeof (struct elf_link_hash_entry *));
4f87808c
AM
5385 htab->root.table.table = old_table;
5386 htab->root.table.size = old_size;
5387 htab->root.table.count = old_count;
66eb6687 5388 memcpy (htab->root.table.table, old_tab, tabsize);
66eb6687
AM
5389 htab->root.undefs = old_undefs;
5390 htab->root.undefs_tail = old_undefs_tail;
e310298c
AM
5391 if (htab->dynstr != NULL)
5392 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5b677558
AM
5393 free (old_strtab);
5394 old_strtab = NULL;
66eb6687
AM
5395 for (i = 0; i < htab->root.table.size; i++)
5396 {
5397 struct bfd_hash_entry *p;
5398 struct elf_link_hash_entry *h;
4070765b 5399 unsigned int non_ir_ref_dynamic;
66eb6687
AM
5400
5401 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5402 {
4070765b 5403 /* Preserve non_ir_ref_dynamic so that this symbol
59fa66c5
L
5404 will be exported when the dynamic lib becomes needed
5405 in the second pass. */
7ba11550
AM
5406 h = (struct elf_link_hash_entry *) p;
5407 if (h->root.type == bfd_link_hash_warning)
5408 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4070765b 5409 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
7ba11550 5410
2de92251 5411 h = (struct elf_link_hash_entry *) p;
7ba11550
AM
5412 memcpy (h, old_ent, htab->root.table.entsize);
5413 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251
AM
5414 if (h->root.type == bfd_link_hash_warning)
5415 {
a4542f1b 5416 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7ba11550
AM
5417 memcpy (h, old_ent, htab->root.table.entsize);
5418 old_ent = (char *) old_ent + htab->root.table.entsize;
2de92251 5419 }
a4542f1b 5420 if (h->root.type == bfd_link_hash_common)
3e0882af 5421 {
7ba11550
AM
5422 memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
5423 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
3e0882af 5424 }
4070765b 5425 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
66eb6687
AM
5426 }
5427 }
5428
5061a885
AM
5429 /* Make a special call to the linker "notice" function to
5430 tell it that symbols added for crefs may need to be removed. */
e5034e59 5431 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
9af2a943 5432 goto error_free_vers;
5061a885 5433
66eb6687
AM
5434 free (old_tab);
5435 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5436 alloc_mark);
c9594989 5437 free (nondeflt_vers);
66eb6687
AM
5438 return TRUE;
5439 }
2de92251 5440
66eb6687
AM
5441 if (old_tab != NULL)
5442 {
e5034e59 5443 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
9af2a943 5444 goto error_free_vers;
66eb6687
AM
5445 free (old_tab);
5446 old_tab = NULL;
5447 }
5448
c6e8a9a8
L
5449 /* Now that all the symbols from this input file are created, if
5450 not performing a relocatable link, handle .symver foo, foo@BAR
5451 such that any relocs against foo become foo@BAR. */
0e1862bb 5452 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4ad4eba5 5453 {
ef53be89 5454 size_t cnt, symidx;
4ad4eba5
AM
5455
5456 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5457 {
5458 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5459 char *shortname, *p;
986f0783 5460 size_t amt;
4ad4eba5
AM
5461
5462 p = strchr (h->root.root.string, ELF_VER_CHR);
5463 if (p == NULL
5464 || (h->root.type != bfd_link_hash_defined
5465 && h->root.type != bfd_link_hash_defweak))
5466 continue;
5467
5468 amt = p - h->root.root.string;
a50b1753 5469 shortname = (char *) bfd_malloc (amt + 1);
14b1c01e
AM
5470 if (!shortname)
5471 goto error_free_vers;
4ad4eba5
AM
5472 memcpy (shortname, h->root.root.string, amt);
5473 shortname[amt] = '\0';
5474
5475 hi = (struct elf_link_hash_entry *)
66eb6687 5476 bfd_link_hash_lookup (&htab->root, shortname,
4ad4eba5
AM
5477 FALSE, FALSE, FALSE);
5478 if (hi != NULL
5479 && hi->root.type == h->root.type
5480 && hi->root.u.def.value == h->root.u.def.value
5481 && hi->root.u.def.section == h->root.u.def.section)
5482 {
5483 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5484 hi->root.type = bfd_link_hash_indirect;
5485 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
fcfa13d2 5486 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4ad4eba5
AM
5487 sym_hash = elf_sym_hashes (abfd);
5488 if (sym_hash)
5489 for (symidx = 0; symidx < extsymcount; ++symidx)
5490 if (sym_hash[symidx] == hi)
5491 {
5492 sym_hash[symidx] = h;
5493 break;
5494 }
5495 }
5496 free (shortname);
5497 }
5498 free (nondeflt_vers);
5499 nondeflt_vers = NULL;
5500 }
5501
60d67dc8 5502 /* Now set the alias field correctly for all the weak defined
4ad4eba5
AM
5503 symbols we found. The only way to do this is to search all the
5504 symbols. Since we only need the information for non functions in
5505 dynamic objects, that's the only time we actually put anything on
5506 the list WEAKS. We need this information so that if a regular
5507 object refers to a symbol defined weakly in a dynamic object, the
5508 real symbol in the dynamic object is also put in the dynamic
5509 symbols; we also must arrange for both symbols to point to the
5510 same memory location. We could handle the general case of symbol
5511 aliasing, but a general symbol alias can only be generated in
5512 assembler code, handling it correctly would be very time
5513 consuming, and other ELF linkers don't handle general aliasing
5514 either. */
5515 if (weaks != NULL)
5516 {
5517 struct elf_link_hash_entry **hpp;
5518 struct elf_link_hash_entry **hppend;
5519 struct elf_link_hash_entry **sorted_sym_hash;
5520 struct elf_link_hash_entry *h;
986f0783 5521 size_t sym_count, amt;
4ad4eba5
AM
5522
5523 /* Since we have to search the whole symbol list for each weak
5524 defined symbol, search time for N weak defined symbols will be
5525 O(N^2). Binary search will cut it down to O(NlogN). */
986f0783 5526 amt = extsymcount * sizeof (*sorted_sym_hash);
3a3f4bf7 5527 sorted_sym_hash = bfd_malloc (amt);
4ad4eba5
AM
5528 if (sorted_sym_hash == NULL)
5529 goto error_return;
5530 sym_hash = sorted_sym_hash;
5531 hpp = elf_sym_hashes (abfd);
5532 hppend = hpp + extsymcount;
5533 sym_count = 0;
5534 for (; hpp < hppend; hpp++)
5535 {
5536 h = *hpp;
5537 if (h != NULL
5538 && h->root.type == bfd_link_hash_defined
fcb93ecf 5539 && !bed->is_function_type (h->type))
4ad4eba5
AM
5540 {
5541 *sym_hash = h;
5542 sym_hash++;
5543 sym_count++;
5544 }
5545 }
5546
3a3f4bf7 5547 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
4ad4eba5
AM
5548 elf_sort_symbol);
5549
5550 while (weaks != NULL)
5551 {
5552 struct elf_link_hash_entry *hlook;
5553 asection *slook;
5554 bfd_vma vlook;
ed54588d 5555 size_t i, j, idx = 0;
4ad4eba5
AM
5556
5557 hlook = weaks;
60d67dc8
AM
5558 weaks = hlook->u.alias;
5559 hlook->u.alias = NULL;
4ad4eba5 5560
e3e53eed
AM
5561 if (hlook->root.type != bfd_link_hash_defined
5562 && hlook->root.type != bfd_link_hash_defweak)
5563 continue;
5564
4ad4eba5
AM
5565 slook = hlook->root.u.def.section;
5566 vlook = hlook->root.u.def.value;
5567
4ad4eba5
AM
5568 i = 0;
5569 j = sym_count;
14160578 5570 while (i != j)
4ad4eba5
AM
5571 {
5572 bfd_signed_vma vdiff;
5573 idx = (i + j) / 2;
14160578 5574 h = sorted_sym_hash[idx];
4ad4eba5
AM
5575 vdiff = vlook - h->root.u.def.value;
5576 if (vdiff < 0)
5577 j = idx;
5578 else if (vdiff > 0)
5579 i = idx + 1;
5580 else
5581 {
d3435ae8 5582 int sdiff = slook->id - h->root.u.def.section->id;
4ad4eba5
AM
5583 if (sdiff < 0)
5584 j = idx;
5585 else if (sdiff > 0)
5586 i = idx + 1;
5587 else
14160578 5588 break;
4ad4eba5
AM
5589 }
5590 }
5591
5592 /* We didn't find a value/section match. */
14160578 5593 if (i == j)
4ad4eba5
AM
5594 continue;
5595
14160578
AM
5596 /* With multiple aliases, or when the weak symbol is already
5597 strongly defined, we have multiple matching symbols and
5598 the binary search above may land on any of them. Step
5599 one past the matching symbol(s). */
5600 while (++idx != j)
5601 {
5602 h = sorted_sym_hash[idx];
5603 if (h->root.u.def.section != slook
5604 || h->root.u.def.value != vlook)
5605 break;
5606 }
5607
5608 /* Now look back over the aliases. Since we sorted by size
5609 as well as value and section, we'll choose the one with
5610 the largest size. */
5611 while (idx-- != i)
4ad4eba5 5612 {
14160578 5613 h = sorted_sym_hash[idx];
4ad4eba5
AM
5614
5615 /* Stop if value or section doesn't match. */
14160578
AM
5616 if (h->root.u.def.section != slook
5617 || h->root.u.def.value != vlook)
4ad4eba5
AM
5618 break;
5619 else if (h != hlook)
5620 {
60d67dc8
AM
5621 struct elf_link_hash_entry *t;
5622
5623 hlook->u.alias = h;
5624 hlook->is_weakalias = 1;
5625 t = h;
5626 if (t->u.alias != NULL)
5627 while (t->u.alias != h)
5628 t = t->u.alias;
5629 t->u.alias = hlook;
4ad4eba5
AM
5630
5631 /* If the weak definition is in the list of dynamic
5632 symbols, make sure the real definition is put
5633 there as well. */
5634 if (hlook->dynindx != -1 && h->dynindx == -1)
5635 {
c152c796 5636 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4dd07732
AM
5637 {
5638 err_free_sym_hash:
5639 free (sorted_sym_hash);
5640 goto error_return;
5641 }
4ad4eba5
AM
5642 }
5643
5644 /* If the real definition is in the list of dynamic
5645 symbols, make sure the weak definition is put
5646 there as well. If we don't do this, then the
5647 dynamic loader might not merge the entries for the
5648 real definition and the weak definition. */
5649 if (h->dynindx != -1 && hlook->dynindx == -1)
5650 {
c152c796 5651 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4dd07732 5652 goto err_free_sym_hash;
4ad4eba5
AM
5653 }
5654 break;
5655 }
5656 }
5657 }
5658
5659 free (sorted_sym_hash);
5660 }
5661
33177bb1
AM
5662 if (bed->check_directives
5663 && !(*bed->check_directives) (abfd, info))
5664 return FALSE;
85fbca6a 5665
4ad4eba5
AM
5666 /* If this is a non-traditional link, try to optimize the handling
5667 of the .stab/.stabstr sections. */
5668 if (! dynamic
5669 && ! info->traditional_format
66eb6687 5670 && is_elf_hash_table (htab)
4ad4eba5
AM
5671 && (info->strip != strip_all && info->strip != strip_debugger))
5672 {
5673 asection *stabstr;
5674
5675 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5676 if (stabstr != NULL)
5677 {
5678 bfd_size_type string_offset = 0;
5679 asection *stab;
5680
5681 for (stab = abfd->sections; stab; stab = stab->next)
0112cd26 5682 if (CONST_STRNEQ (stab->name, ".stab")
4ad4eba5
AM
5683 && (!stab->name[5] ||
5684 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5685 && (stab->flags & SEC_MERGE) == 0
5686 && !bfd_is_abs_section (stab->output_section))
5687 {
5688 struct bfd_elf_section_data *secdata;
5689
5690 secdata = elf_section_data (stab);
66eb6687
AM
5691 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5692 stabstr, &secdata->sec_info,
4ad4eba5
AM
5693 &string_offset))
5694 goto error_return;
5695 if (secdata->sec_info)
dbaa2011 5696 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4ad4eba5
AM
5697 }
5698 }
5699 }
5700
e310298c 5701 if (dynamic && add_needed)
4ad4eba5
AM
5702 {
5703 /* Add this bfd to the loaded list. */
5704 struct elf_link_loaded_list *n;
5705
ca4be51c 5706 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4ad4eba5
AM
5707 if (n == NULL)
5708 goto error_return;
5709 n->abfd = abfd;
e310298c
AM
5710 n->next = htab->dyn_loaded;
5711 htab->dyn_loaded = n;
4ad4eba5 5712 }
e310298c
AM
5713 if (dynamic && !add_needed
5714 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
5715 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
4ad4eba5
AM
5716
5717 return TRUE;
5718
5719 error_free_vers:
c9594989
AM
5720 free (old_tab);
5721 free (old_strtab);
5722 free (nondeflt_vers);
5723 free (extversym);
4ad4eba5 5724 error_free_sym:
c9594989 5725 free (isymbuf);
4ad4eba5
AM
5726 error_return:
5727 return FALSE;
5728}
5729
8387904d
AM
5730/* Return the linker hash table entry of a symbol that might be
5731 satisfied by an archive symbol. Return -1 on error. */
5732
5733struct elf_link_hash_entry *
5734_bfd_elf_archive_symbol_lookup (bfd *abfd,
5735 struct bfd_link_info *info,
5736 const char *name)
5737{
5738 struct elf_link_hash_entry *h;
5739 char *p, *copy;
5740 size_t len, first;
5741
2a41f396 5742 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
8387904d
AM
5743 if (h != NULL)
5744 return h;
5745
5746 /* If this is a default version (the name contains @@), look up the
5747 symbol again with only one `@' as well as without the version.
5748 The effect is that references to the symbol with and without the
5749 version will be matched by the default symbol in the archive. */
5750
5751 p = strchr (name, ELF_VER_CHR);
5752 if (p == NULL || p[1] != ELF_VER_CHR)
5753 return h;
5754
5755 /* First check with only one `@'. */
5756 len = strlen (name);
a50b1753 5757 copy = (char *) bfd_alloc (abfd, len);
8387904d 5758 if (copy == NULL)
e99955cd 5759 return (struct elf_link_hash_entry *) -1;
8387904d
AM
5760
5761 first = p - name + 1;
5762 memcpy (copy, name, first);
5763 memcpy (copy + first, name + first + 1, len - first);
5764
2a41f396 5765 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
8387904d
AM
5766 if (h == NULL)
5767 {
5768 /* We also need to check references to the symbol without the
5769 version. */
5770 copy[first - 1] = '\0';
5771 h = elf_link_hash_lookup (elf_hash_table (info), copy,
2a41f396 5772 FALSE, FALSE, TRUE);
8387904d
AM
5773 }
5774
5775 bfd_release (abfd, copy);
5776 return h;
5777}
5778
0ad989f9 5779/* Add symbols from an ELF archive file to the linker hash table. We
13e570f8
AM
5780 don't use _bfd_generic_link_add_archive_symbols because we need to
5781 handle versioned symbols.
0ad989f9
L
5782
5783 Fortunately, ELF archive handling is simpler than that done by
5784 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5785 oddities. In ELF, if we find a symbol in the archive map, and the
5786 symbol is currently undefined, we know that we must pull in that
5787 object file.
5788
5789 Unfortunately, we do have to make multiple passes over the symbol
5790 table until nothing further is resolved. */
5791
4ad4eba5
AM
5792static bfd_boolean
5793elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
0ad989f9
L
5794{
5795 symindex c;
13e570f8 5796 unsigned char *included = NULL;
0ad989f9
L
5797 carsym *symdefs;
5798 bfd_boolean loop;
986f0783 5799 size_t amt;
8387904d
AM
5800 const struct elf_backend_data *bed;
5801 struct elf_link_hash_entry * (*archive_symbol_lookup)
5802 (bfd *, struct bfd_link_info *, const char *);
0ad989f9
L
5803
5804 if (! bfd_has_map (abfd))
5805 {
5806 /* An empty archive is a special case. */
5807 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5808 return TRUE;
5809 bfd_set_error (bfd_error_no_armap);
5810 return FALSE;
5811 }
5812
5813 /* Keep track of all symbols we know to be already defined, and all
5814 files we know to be already included. This is to speed up the
5815 second and subsequent passes. */
5816 c = bfd_ardata (abfd)->symdef_count;
5817 if (c == 0)
5818 return TRUE;
986f0783 5819 amt = c * sizeof (*included);
13e570f8
AM
5820 included = (unsigned char *) bfd_zmalloc (amt);
5821 if (included == NULL)
5822 return FALSE;
0ad989f9
L
5823
5824 symdefs = bfd_ardata (abfd)->symdefs;
8387904d
AM
5825 bed = get_elf_backend_data (abfd);
5826 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
0ad989f9
L
5827
5828 do
5829 {
5830 file_ptr last;
5831 symindex i;
5832 carsym *symdef;
5833 carsym *symdefend;
5834
5835 loop = FALSE;
5836 last = -1;
5837
5838 symdef = symdefs;
5839 symdefend = symdef + c;
5840 for (i = 0; symdef < symdefend; symdef++, i++)
5841 {
5842 struct elf_link_hash_entry *h;
5843 bfd *element;
5844 struct bfd_link_hash_entry *undefs_tail;
5845 symindex mark;
5846
13e570f8 5847 if (included[i])
0ad989f9
L
5848 continue;
5849 if (symdef->file_offset == last)
5850 {
5851 included[i] = TRUE;
5852 continue;
5853 }
5854
8387904d 5855 h = archive_symbol_lookup (abfd, info, symdef->name);
e99955cd 5856 if (h == (struct elf_link_hash_entry *) -1)
8387904d 5857 goto error_return;
0ad989f9
L
5858
5859 if (h == NULL)
5860 continue;
5861
75cfe082
AM
5862 if (h->root.type == bfd_link_hash_undefined)
5863 {
5864 /* If the archive element has already been loaded then one
5865 of the symbols defined by that element might have been
5866 made undefined due to being in a discarded section. */
5867 if (h->indx == -3)
5868 continue;
5869 }
5870 else if (h->root.type == bfd_link_hash_common)
0ad989f9
L
5871 {
5872 /* We currently have a common symbol. The archive map contains
5873 a reference to this symbol, so we may want to include it. We
5874 only want to include it however, if this archive element
5875 contains a definition of the symbol, not just another common
5876 declaration of it.
5877
5878 Unfortunately some archivers (including GNU ar) will put
5879 declarations of common symbols into their archive maps, as
5880 well as real definitions, so we cannot just go by the archive
5881 map alone. Instead we must read in the element's symbol
5882 table and check that to see what kind of symbol definition
5883 this is. */
5884 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5885 continue;
5886 }
75cfe082 5887 else
0ad989f9
L
5888 {
5889 if (h->root.type != bfd_link_hash_undefweak)
13e570f8
AM
5890 /* Symbol must be defined. Don't check it again. */
5891 included[i] = TRUE;
0ad989f9
L
5892 continue;
5893 }
5894
5895 /* We need to include this archive member. */
5896 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5897 if (element == NULL)
5898 goto error_return;
5899
5900 if (! bfd_check_format (element, bfd_object))
5901 goto error_return;
5902
0ad989f9
L
5903 undefs_tail = info->hash->undefs_tail;
5904
0e144ba7
AM
5905 if (!(*info->callbacks
5906 ->add_archive_element) (info, element, symdef->name, &element))
b95a0a31 5907 continue;
0e144ba7 5908 if (!bfd_link_add_symbols (element, info))
0ad989f9
L
5909 goto error_return;
5910
5911 /* If there are any new undefined symbols, we need to make
5912 another pass through the archive in order to see whether
5913 they can be defined. FIXME: This isn't perfect, because
5914 common symbols wind up on undefs_tail and because an
5915 undefined symbol which is defined later on in this pass
5916 does not require another pass. This isn't a bug, but it
5917 does make the code less efficient than it could be. */
5918 if (undefs_tail != info->hash->undefs_tail)
5919 loop = TRUE;
5920
5921 /* Look backward to mark all symbols from this object file
5922 which we have already seen in this pass. */
5923 mark = i;
5924 do
5925 {
5926 included[mark] = TRUE;
5927 if (mark == 0)
5928 break;
5929 --mark;
5930 }
5931 while (symdefs[mark].file_offset == symdef->file_offset);
5932
5933 /* We mark subsequent symbols from this object file as we go
5934 on through the loop. */
5935 last = symdef->file_offset;
5936 }
5937 }
5938 while (loop);
5939
0ad989f9 5940 free (included);
0ad989f9
L
5941 return TRUE;
5942
5943 error_return:
c9594989 5944 free (included);
0ad989f9
L
5945 return FALSE;
5946}
4ad4eba5
AM
5947
5948/* Given an ELF BFD, add symbols to the global hash table as
5949 appropriate. */
5950
5951bfd_boolean
5952bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5953{
5954 switch (bfd_get_format (abfd))
5955 {
5956 case bfd_object:
5957 return elf_link_add_object_symbols (abfd, info);
5958 case bfd_archive:
5959 return elf_link_add_archive_symbols (abfd, info);
5960 default:
5961 bfd_set_error (bfd_error_wrong_format);
5962 return FALSE;
5963 }
5964}
5a580b3a 5965\f
14b1c01e
AM
5966struct hash_codes_info
5967{
5968 unsigned long *hashcodes;
5969 bfd_boolean error;
5970};
a0c8462f 5971
5a580b3a
AM
5972/* This function will be called though elf_link_hash_traverse to store
5973 all hash value of the exported symbols in an array. */
5974
5975static bfd_boolean
5976elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5977{
a50b1753 5978 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5a580b3a 5979 const char *name;
5a580b3a
AM
5980 unsigned long ha;
5981 char *alc = NULL;
5982
5a580b3a
AM
5983 /* Ignore indirect symbols. These are added by the versioning code. */
5984 if (h->dynindx == -1)
5985 return TRUE;
5986
5987 name = h->root.root.string;
422f1182 5988 if (h->versioned >= versioned)
5a580b3a 5989 {
422f1182
L
5990 char *p = strchr (name, ELF_VER_CHR);
5991 if (p != NULL)
14b1c01e 5992 {
422f1182
L
5993 alc = (char *) bfd_malloc (p - name + 1);
5994 if (alc == NULL)
5995 {
5996 inf->error = TRUE;
5997 return FALSE;
5998 }
5999 memcpy (alc, name, p - name);
6000 alc[p - name] = '\0';
6001 name = alc;
14b1c01e 6002 }
5a580b3a
AM
6003 }
6004
6005 /* Compute the hash value. */
6006 ha = bfd_elf_hash (name);
6007
6008 /* Store the found hash value in the array given as the argument. */
14b1c01e 6009 *(inf->hashcodes)++ = ha;
5a580b3a
AM
6010
6011 /* And store it in the struct so that we can put it in the hash table
6012 later. */
f6e332e6 6013 h->u.elf_hash_value = ha;
5a580b3a 6014
c9594989 6015 free (alc);
5a580b3a
AM
6016 return TRUE;
6017}
6018
fdc90cb4
JJ
6019struct collect_gnu_hash_codes
6020{
6021 bfd *output_bfd;
6022 const struct elf_backend_data *bed;
6023 unsigned long int nsyms;
6024 unsigned long int maskbits;
6025 unsigned long int *hashcodes;
6026 unsigned long int *hashval;
6027 unsigned long int *indx;
6028 unsigned long int *counts;
6029 bfd_vma *bitmask;
6030 bfd_byte *contents;
f16a9783 6031 bfd_size_type xlat;
fdc90cb4
JJ
6032 long int min_dynindx;
6033 unsigned long int bucketcount;
6034 unsigned long int symindx;
6035 long int local_indx;
6036 long int shift1, shift2;
6037 unsigned long int mask;
14b1c01e 6038 bfd_boolean error;
fdc90cb4
JJ
6039};
6040
6041/* This function will be called though elf_link_hash_traverse to store
6042 all hash value of the exported symbols in an array. */
6043
6044static bfd_boolean
6045elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
6046{
a50b1753 6047 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4 6048 const char *name;
fdc90cb4
JJ
6049 unsigned long ha;
6050 char *alc = NULL;
6051
fdc90cb4
JJ
6052 /* Ignore indirect symbols. These are added by the versioning code. */
6053 if (h->dynindx == -1)
6054 return TRUE;
6055
6056 /* Ignore also local symbols and undefined symbols. */
6057 if (! (*s->bed->elf_hash_symbol) (h))
6058 return TRUE;
6059
6060 name = h->root.root.string;
422f1182 6061 if (h->versioned >= versioned)
fdc90cb4 6062 {
422f1182
L
6063 char *p = strchr (name, ELF_VER_CHR);
6064 if (p != NULL)
14b1c01e 6065 {
422f1182
L
6066 alc = (char *) bfd_malloc (p - name + 1);
6067 if (alc == NULL)
6068 {
6069 s->error = TRUE;
6070 return FALSE;
6071 }
6072 memcpy (alc, name, p - name);
6073 alc[p - name] = '\0';
6074 name = alc;
14b1c01e 6075 }
fdc90cb4
JJ
6076 }
6077
6078 /* Compute the hash value. */
6079 ha = bfd_elf_gnu_hash (name);
6080
6081 /* Store the found hash value in the array for compute_bucket_count,
6082 and also for .dynsym reordering purposes. */
6083 s->hashcodes[s->nsyms] = ha;
6084 s->hashval[h->dynindx] = ha;
6085 ++s->nsyms;
6086 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
6087 s->min_dynindx = h->dynindx;
6088
c9594989 6089 free (alc);
fdc90cb4
JJ
6090 return TRUE;
6091}
6092
6093/* This function will be called though elf_link_hash_traverse to do
f16a9783
MS
6094 final dynamic symbol renumbering in case of .gnu.hash.
6095 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
6096 to the translation table. */
fdc90cb4
JJ
6097
6098static bfd_boolean
f16a9783 6099elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
fdc90cb4 6100{
a50b1753 6101 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
fdc90cb4
JJ
6102 unsigned long int bucket;
6103 unsigned long int val;
6104
fdc90cb4
JJ
6105 /* Ignore indirect symbols. */
6106 if (h->dynindx == -1)
6107 return TRUE;
6108
6109 /* Ignore also local symbols and undefined symbols. */
6110 if (! (*s->bed->elf_hash_symbol) (h))
6111 {
6112 if (h->dynindx >= s->min_dynindx)
f16a9783
MS
6113 {
6114 if (s->bed->record_xhash_symbol != NULL)
6115 {
6116 (*s->bed->record_xhash_symbol) (h, 0);
6117 s->local_indx++;
6118 }
6119 else
6120 h->dynindx = s->local_indx++;
6121 }
fdc90cb4
JJ
6122 return TRUE;
6123 }
6124
6125 bucket = s->hashval[h->dynindx] % s->bucketcount;
6126 val = (s->hashval[h->dynindx] >> s->shift1)
6127 & ((s->maskbits >> s->shift1) - 1);
6128 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
6129 s->bitmask[val]
6130 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
6131 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
6132 if (s->counts[bucket] == 1)
6133 /* Last element terminates the chain. */
6134 val |= 1;
6135 bfd_put_32 (s->output_bfd, val,
6136 s->contents + (s->indx[bucket] - s->symindx) * 4);
6137 --s->counts[bucket];
f16a9783
MS
6138 if (s->bed->record_xhash_symbol != NULL)
6139 {
6140 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
6141
6142 (*s->bed->record_xhash_symbol) (h, xlat_loc);
6143 }
6144 else
6145 h->dynindx = s->indx[bucket]++;
fdc90cb4
JJ
6146 return TRUE;
6147}
6148
6149/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6150
6151bfd_boolean
6152_bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
6153{
6154 return !(h->forced_local
6155 || h->root.type == bfd_link_hash_undefined
6156 || h->root.type == bfd_link_hash_undefweak
6157 || ((h->root.type == bfd_link_hash_defined
6158 || h->root.type == bfd_link_hash_defweak)
6159 && h->root.u.def.section->output_section == NULL));
6160}
6161
5a580b3a
AM
6162/* Array used to determine the number of hash table buckets to use
6163 based on the number of symbols there are. If there are fewer than
6164 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
6165 fewer than 37 we use 17 buckets, and so forth. We never use more
6166 than 32771 buckets. */
6167
6168static const size_t elf_buckets[] =
6169{
6170 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
6171 16411, 32771, 0
6172};
6173
6174/* Compute bucket count for hashing table. We do not use a static set
6175 of possible tables sizes anymore. Instead we determine for all
6176 possible reasonable sizes of the table the outcome (i.e., the
6177 number of collisions etc) and choose the best solution. The
6178 weighting functions are not too simple to allow the table to grow
6179 without bounds. Instead one of the weighting factors is the size.
6180 Therefore the result is always a good payoff between few collisions
6181 (= short chain lengths) and table size. */
6182static size_t
b20dd2ce 6183compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
d40f3da9
AM
6184 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6185 unsigned long int nsyms,
6186 int gnu_hash)
5a580b3a 6187{
5a580b3a 6188 size_t best_size = 0;
5a580b3a 6189 unsigned long int i;
5a580b3a 6190
5a580b3a
AM
6191 /* We have a problem here. The following code to optimize the table
6192 size requires an integer type with more the 32 bits. If
6193 BFD_HOST_U_64_BIT is set we know about such a type. */
6194#ifdef BFD_HOST_U_64_BIT
6195 if (info->optimize)
6196 {
5a580b3a
AM
6197 size_t minsize;
6198 size_t maxsize;
6199 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5a580b3a 6200 bfd *dynobj = elf_hash_table (info)->dynobj;
d40f3da9 6201 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5a580b3a 6202 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
fdc90cb4 6203 unsigned long int *counts;
d40f3da9 6204 bfd_size_type amt;
0883b6e0 6205 unsigned int no_improvement_count = 0;
5a580b3a
AM
6206
6207 /* Possible optimization parameters: if we have NSYMS symbols we say
6208 that the hashing table must at least have NSYMS/4 and at most
6209 2*NSYMS buckets. */
6210 minsize = nsyms / 4;
6211 if (minsize == 0)
6212 minsize = 1;
6213 best_size = maxsize = nsyms * 2;
fdc90cb4
JJ
6214 if (gnu_hash)
6215 {
6216 if (minsize < 2)
6217 minsize = 2;
6218 if ((best_size & 31) == 0)
6219 ++best_size;
6220 }
5a580b3a
AM
6221
6222 /* Create array where we count the collisions in. We must use bfd_malloc
6223 since the size could be large. */
6224 amt = maxsize;
6225 amt *= sizeof (unsigned long int);
a50b1753 6226 counts = (unsigned long int *) bfd_malloc (amt);
5a580b3a 6227 if (counts == NULL)
fdc90cb4 6228 return 0;
5a580b3a
AM
6229
6230 /* Compute the "optimal" size for the hash table. The criteria is a
6231 minimal chain length. The minor criteria is (of course) the size
6232 of the table. */
6233 for (i = minsize; i < maxsize; ++i)
6234 {
6235 /* Walk through the array of hashcodes and count the collisions. */
6236 BFD_HOST_U_64_BIT max;
6237 unsigned long int j;
6238 unsigned long int fact;
6239
fdc90cb4
JJ
6240 if (gnu_hash && (i & 31) == 0)
6241 continue;
6242
5a580b3a
AM
6243 memset (counts, '\0', i * sizeof (unsigned long int));
6244
6245 /* Determine how often each hash bucket is used. */
6246 for (j = 0; j < nsyms; ++j)
6247 ++counts[hashcodes[j] % i];
6248
6249 /* For the weight function we need some information about the
6250 pagesize on the target. This is information need not be 100%
6251 accurate. Since this information is not available (so far) we
6252 define it here to a reasonable default value. If it is crucial
6253 to have a better value some day simply define this value. */
6254# ifndef BFD_TARGET_PAGESIZE
6255# define BFD_TARGET_PAGESIZE (4096)
6256# endif
6257
fdc90cb4
JJ
6258 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6259 and the chains. */
6260 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5a580b3a
AM
6261
6262# if 1
6263 /* Variant 1: optimize for short chains. We add the squares
6264 of all the chain lengths (which favors many small chain
6265 over a few long chains). */
6266 for (j = 0; j < i; ++j)
6267 max += counts[j] * counts[j];
6268
6269 /* This adds penalties for the overall size of the table. */
fdc90cb4 6270 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6271 max *= fact * fact;
6272# else
6273 /* Variant 2: Optimize a lot more for small table. Here we
6274 also add squares of the size but we also add penalties for
6275 empty slots (the +1 term). */
6276 for (j = 0; j < i; ++j)
6277 max += (1 + counts[j]) * (1 + counts[j]);
6278
6279 /* The overall size of the table is considered, but not as
6280 strong as in variant 1, where it is squared. */
fdc90cb4 6281 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5a580b3a
AM
6282 max *= fact;
6283# endif
6284
6285 /* Compare with current best results. */
6286 if (max < best_chlen)
6287 {
6288 best_chlen = max;
6289 best_size = i;
ca4be51c 6290 no_improvement_count = 0;
5a580b3a 6291 }
0883b6e0
NC
6292 /* PR 11843: Avoid futile long searches for the best bucket size
6293 when there are a large number of symbols. */
6294 else if (++no_improvement_count == 100)
6295 break;
5a580b3a
AM
6296 }
6297
6298 free (counts);
6299 }
6300 else
6301#endif /* defined (BFD_HOST_U_64_BIT) */
6302 {
6303 /* This is the fallback solution if no 64bit type is available or if we
6304 are not supposed to spend much time on optimizations. We select the
6305 bucket count using a fixed set of numbers. */
6306 for (i = 0; elf_buckets[i] != 0; i++)
6307 {
6308 best_size = elf_buckets[i];
fdc90cb4 6309 if (nsyms < elf_buckets[i + 1])
5a580b3a
AM
6310 break;
6311 }
fdc90cb4
JJ
6312 if (gnu_hash && best_size < 2)
6313 best_size = 2;
5a580b3a
AM
6314 }
6315
5a580b3a
AM
6316 return best_size;
6317}
6318
d0bf826b
AM
6319/* Size any SHT_GROUP section for ld -r. */
6320
6321bfd_boolean
6322_bfd_elf_size_group_sections (struct bfd_link_info *info)
6323{
6324 bfd *ibfd;
57963c05 6325 asection *s;
d0bf826b 6326
c72f2fb2 6327 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
d0bf826b 6328 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
57963c05
AM
6329 && (s = ibfd->sections) != NULL
6330 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
d0bf826b
AM
6331 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6332 return FALSE;
6333 return TRUE;
6334}
6335
04c3a755
NS
6336/* Set a default stack segment size. The value in INFO wins. If it
6337 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6338 undefined it is initialized. */
6339
6340bfd_boolean
6341bfd_elf_stack_segment_size (bfd *output_bfd,
6342 struct bfd_link_info *info,
6343 const char *legacy_symbol,
6344 bfd_vma default_size)
6345{
6346 struct elf_link_hash_entry *h = NULL;
6347
6348 /* Look for legacy symbol. */
6349 if (legacy_symbol)
6350 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6351 FALSE, FALSE, FALSE);
6352 if (h && (h->root.type == bfd_link_hash_defined
6353 || h->root.type == bfd_link_hash_defweak)
6354 && h->def_regular
6355 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6356 {
6357 /* The symbol has no type if specified on the command line. */
6358 h->type = STT_OBJECT;
6359 if (info->stacksize)
695344c0 6360 /* xgettext:c-format */
871b3ab2 6361 _bfd_error_handler (_("%pB: stack size specified and %s set"),
4eca0228 6362 output_bfd, legacy_symbol);
04c3a755 6363 else if (h->root.u.def.section != bfd_abs_section_ptr)
695344c0 6364 /* xgettext:c-format */
871b3ab2 6365 _bfd_error_handler (_("%pB: %s not absolute"),
4eca0228 6366 output_bfd, legacy_symbol);
04c3a755
NS
6367 else
6368 info->stacksize = h->root.u.def.value;
6369 }
6370
6371 if (!info->stacksize)
6372 /* If the user didn't set a size, or explicitly inhibit the
6373 size, set it now. */
6374 info->stacksize = default_size;
6375
6376 /* Provide the legacy symbol, if it is referenced. */
6377 if (h && (h->root.type == bfd_link_hash_undefined
6378 || h->root.type == bfd_link_hash_undefweak))
6379 {
6380 struct bfd_link_hash_entry *bh = NULL;
6381
6382 if (!(_bfd_generic_link_add_one_symbol
6383 (info, output_bfd, legacy_symbol,
6384 BSF_GLOBAL, bfd_abs_section_ptr,
6385 info->stacksize >= 0 ? info->stacksize : 0,
6386 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6387 return FALSE;
6388
6389 h = (struct elf_link_hash_entry *) bh;
6390 h->def_regular = 1;
6391 h->type = STT_OBJECT;
6392 }
6393
6394 return TRUE;
6395}
6396
b531344c
MR
6397/* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6398
6399struct elf_gc_sweep_symbol_info
6400{
6401 struct bfd_link_info *info;
6402 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6403 bfd_boolean);
6404};
6405
6406static bfd_boolean
6407elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6408{
6409 if (!h->mark
6410 && (((h->root.type == bfd_link_hash_defined
6411 || h->root.type == bfd_link_hash_defweak)
6412 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6413 && h->root.u.def.section->gc_mark))
6414 || h->root.type == bfd_link_hash_undefined
6415 || h->root.type == bfd_link_hash_undefweak))
6416 {
6417 struct elf_gc_sweep_symbol_info *inf;
6418
6419 inf = (struct elf_gc_sweep_symbol_info *) data;
6420 (*inf->hide_symbol) (inf->info, h, TRUE);
6421 h->def_regular = 0;
6422 h->ref_regular = 0;
6423 h->ref_regular_nonweak = 0;
6424 }
6425
6426 return TRUE;
6427}
6428
5a580b3a
AM
6429/* Set up the sizes and contents of the ELF dynamic sections. This is
6430 called by the ELF linker emulation before_allocation routine. We
6431 must set the sizes of the sections before the linker sets the
6432 addresses of the various sections. */
6433
6434bfd_boolean
6435bfd_elf_size_dynamic_sections (bfd *output_bfd,
6436 const char *soname,
6437 const char *rpath,
6438 const char *filter_shlib,
7ee314fa
AM
6439 const char *audit,
6440 const char *depaudit,
5a580b3a
AM
6441 const char * const *auxiliary_filters,
6442 struct bfd_link_info *info,
fd91d419 6443 asection **sinterpptr)
5a580b3a 6444{
5a580b3a
AM
6445 bfd *dynobj;
6446 const struct elf_backend_data *bed;
5a580b3a
AM
6447
6448 *sinterpptr = NULL;
6449
5a580b3a
AM
6450 if (!is_elf_hash_table (info->hash))
6451 return TRUE;
6452
5a580b3a
AM
6453 dynobj = elf_hash_table (info)->dynobj;
6454
9a2a56cc 6455 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5a580b3a 6456 {
902e9fc7
MR
6457 struct bfd_elf_version_tree *verdefs;
6458 struct elf_info_failed asvinfo;
5a580b3a
AM
6459 struct bfd_elf_version_tree *t;
6460 struct bfd_elf_version_expr *d;
902e9fc7 6461 asection *s;
e6699019 6462 size_t soname_indx;
7ee314fa 6463
5a580b3a
AM
6464 /* If we are supposed to export all symbols into the dynamic symbol
6465 table (this is not the normal case), then do so. */
55255dae 6466 if (info->export_dynamic
0e1862bb 6467 || (bfd_link_executable (info) && info->dynamic))
5a580b3a 6468 {
3d13f3e9
AM
6469 struct elf_info_failed eif;
6470
6471 eif.info = info;
6472 eif.failed = FALSE;
5a580b3a
AM
6473 elf_link_hash_traverse (elf_hash_table (info),
6474 _bfd_elf_export_symbol,
6475 &eif);
6476 if (eif.failed)
6477 return FALSE;
6478 }
6479
e6699019
L
6480 if (soname != NULL)
6481 {
6482 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6483 soname, TRUE);
6484 if (soname_indx == (size_t) -1
6485 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6486 return FALSE;
6487 }
6488 else
6489 soname_indx = (size_t) -1;
6490
5a580b3a 6491 /* Make all global versions with definition. */
fd91d419 6492 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6493 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6494 if (!d->symver && d->literal)
5a580b3a
AM
6495 {
6496 const char *verstr, *name;
6497 size_t namelen, verlen, newlen;
93252b1c 6498 char *newname, *p, leading_char;
5a580b3a
AM
6499 struct elf_link_hash_entry *newh;
6500
93252b1c 6501 leading_char = bfd_get_symbol_leading_char (output_bfd);
ae5a3597 6502 name = d->pattern;
93252b1c 6503 namelen = strlen (name) + (leading_char != '\0');
5a580b3a
AM
6504 verstr = t->name;
6505 verlen = strlen (verstr);
6506 newlen = namelen + verlen + 3;
6507
a50b1753 6508 newname = (char *) bfd_malloc (newlen);
5a580b3a
AM
6509 if (newname == NULL)
6510 return FALSE;
93252b1c
MF
6511 newname[0] = leading_char;
6512 memcpy (newname + (leading_char != '\0'), name, namelen);
5a580b3a
AM
6513
6514 /* Check the hidden versioned definition. */
6515 p = newname + namelen;
6516 *p++ = ELF_VER_CHR;
6517 memcpy (p, verstr, verlen + 1);
6518 newh = elf_link_hash_lookup (elf_hash_table (info),
6519 newname, FALSE, FALSE,
6520 FALSE);
6521 if (newh == NULL
6522 || (newh->root.type != bfd_link_hash_defined
6523 && newh->root.type != bfd_link_hash_defweak))
6524 {
6525 /* Check the default versioned definition. */
6526 *p++ = ELF_VER_CHR;
6527 memcpy (p, verstr, verlen + 1);
6528 newh = elf_link_hash_lookup (elf_hash_table (info),
6529 newname, FALSE, FALSE,
6530 FALSE);
6531 }
6532 free (newname);
6533
6534 /* Mark this version if there is a definition and it is
6535 not defined in a shared object. */
6536 if (newh != NULL
f5385ebf 6537 && !newh->def_dynamic
5a580b3a
AM
6538 && (newh->root.type == bfd_link_hash_defined
6539 || newh->root.type == bfd_link_hash_defweak))
6540 d->symver = 1;
6541 }
6542
6543 /* Attach all the symbols to their version information. */
5a580b3a 6544 asvinfo.info = info;
5a580b3a
AM
6545 asvinfo.failed = FALSE;
6546
6547 elf_link_hash_traverse (elf_hash_table (info),
6548 _bfd_elf_link_assign_sym_version,
6549 &asvinfo);
6550 if (asvinfo.failed)
6551 return FALSE;
6552
6553 if (!info->allow_undefined_version)
6554 {
6555 /* Check if all global versions have a definition. */
3d13f3e9 6556 bfd_boolean all_defined = TRUE;
fd91d419 6557 for (t = info->version_info; t != NULL; t = t->next)
5a580b3a 6558 for (d = t->globals.list; d != NULL; d = d->next)
ae5a3597 6559 if (d->literal && !d->symver && !d->script)
5a580b3a 6560 {
4eca0228 6561 _bfd_error_handler
5a580b3a
AM
6562 (_("%s: undefined version: %s"),
6563 d->pattern, t->name);
6564 all_defined = FALSE;
6565 }
6566
6567 if (!all_defined)
6568 {
6569 bfd_set_error (bfd_error_bad_value);
6570 return FALSE;
6571 }
6572 }
6573
902e9fc7
MR
6574 /* Set up the version definition section. */
6575 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6576 BFD_ASSERT (s != NULL);
5a580b3a 6577
902e9fc7
MR
6578 /* We may have created additional version definitions if we are
6579 just linking a regular application. */
6580 verdefs = info->version_info;
5a580b3a 6581
902e9fc7
MR
6582 /* Skip anonymous version tag. */
6583 if (verdefs != NULL && verdefs->vernum == 0)
6584 verdefs = verdefs->next;
5a580b3a 6585
902e9fc7
MR
6586 if (verdefs == NULL && !info->create_default_symver)
6587 s->flags |= SEC_EXCLUDE;
6588 else
5a580b3a 6589 {
902e9fc7
MR
6590 unsigned int cdefs;
6591 bfd_size_type size;
6592 bfd_byte *p;
6593 Elf_Internal_Verdef def;
6594 Elf_Internal_Verdaux defaux;
6595 struct bfd_link_hash_entry *bh;
6596 struct elf_link_hash_entry *h;
6597 const char *name;
5a580b3a 6598
902e9fc7
MR
6599 cdefs = 0;
6600 size = 0;
5a580b3a 6601
902e9fc7
MR
6602 /* Make space for the base version. */
6603 size += sizeof (Elf_External_Verdef);
6604 size += sizeof (Elf_External_Verdaux);
6605 ++cdefs;
6606
6607 /* Make space for the default version. */
6608 if (info->create_default_symver)
6609 {
6610 size += sizeof (Elf_External_Verdef);
6611 ++cdefs;
3e3b46e5
PB
6612 }
6613
5a580b3a
AM
6614 for (t = verdefs; t != NULL; t = t->next)
6615 {
6616 struct bfd_elf_version_deps *n;
6617
a6cc6b3b
RO
6618 /* Don't emit base version twice. */
6619 if (t->vernum == 0)
6620 continue;
6621
5a580b3a
AM
6622 size += sizeof (Elf_External_Verdef);
6623 size += sizeof (Elf_External_Verdaux);
6624 ++cdefs;
6625
6626 for (n = t->deps; n != NULL; n = n->next)
6627 size += sizeof (Elf_External_Verdaux);
6628 }
6629
eea6121a 6630 s->size = size;
a50b1753 6631 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
eea6121a 6632 if (s->contents == NULL && s->size != 0)
5a580b3a
AM
6633 return FALSE;
6634
6635 /* Fill in the version definition section. */
6636
6637 p = s->contents;
6638
6639 def.vd_version = VER_DEF_CURRENT;
6640 def.vd_flags = VER_FLG_BASE;
6641 def.vd_ndx = 1;
6642 def.vd_cnt = 1;
3e3b46e5
PB
6643 if (info->create_default_symver)
6644 {
6645 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6646 def.vd_next = sizeof (Elf_External_Verdef);
6647 }
6648 else
6649 {
6650 def.vd_aux = sizeof (Elf_External_Verdef);
6651 def.vd_next = (sizeof (Elf_External_Verdef)
6652 + sizeof (Elf_External_Verdaux));
6653 }
5a580b3a 6654
ef53be89 6655 if (soname_indx != (size_t) -1)
5a580b3a
AM
6656 {
6657 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6658 soname_indx);
6659 def.vd_hash = bfd_elf_hash (soname);
6660 defaux.vda_name = soname_indx;
3e3b46e5 6661 name = soname;
5a580b3a
AM
6662 }
6663 else
6664 {
ef53be89 6665 size_t indx;
5a580b3a 6666
765cf5f6 6667 name = lbasename (bfd_get_filename (output_bfd));
5a580b3a
AM
6668 def.vd_hash = bfd_elf_hash (name);
6669 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6670 name, FALSE);
ef53be89 6671 if (indx == (size_t) -1)
5a580b3a
AM
6672 return FALSE;
6673 defaux.vda_name = indx;
6674 }
6675 defaux.vda_next = 0;
6676
6677 _bfd_elf_swap_verdef_out (output_bfd, &def,
6678 (Elf_External_Verdef *) p);
6679 p += sizeof (Elf_External_Verdef);
3e3b46e5
PB
6680 if (info->create_default_symver)
6681 {
6682 /* Add a symbol representing this version. */
6683 bh = NULL;
6684 if (! (_bfd_generic_link_add_one_symbol
6685 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6686 0, NULL, FALSE,
6687 get_elf_backend_data (dynobj)->collect, &bh)))
6688 return FALSE;
6689 h = (struct elf_link_hash_entry *) bh;
6690 h->non_elf = 0;
6691 h->def_regular = 1;
6692 h->type = STT_OBJECT;
6693 h->verinfo.vertree = NULL;
6694
6695 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6696 return FALSE;
6697
6698 /* Create a duplicate of the base version with the same
6699 aux block, but different flags. */
6700 def.vd_flags = 0;
6701 def.vd_ndx = 2;
6702 def.vd_aux = sizeof (Elf_External_Verdef);
6703 if (verdefs)
6704 def.vd_next = (sizeof (Elf_External_Verdef)
6705 + sizeof (Elf_External_Verdaux));
6706 else
6707 def.vd_next = 0;
6708 _bfd_elf_swap_verdef_out (output_bfd, &def,
6709 (Elf_External_Verdef *) p);
6710 p += sizeof (Elf_External_Verdef);
6711 }
5a580b3a
AM
6712 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6713 (Elf_External_Verdaux *) p);
6714 p += sizeof (Elf_External_Verdaux);
6715
6716 for (t = verdefs; t != NULL; t = t->next)
6717 {
6718 unsigned int cdeps;
6719 struct bfd_elf_version_deps *n;
5a580b3a 6720
a6cc6b3b
RO
6721 /* Don't emit the base version twice. */
6722 if (t->vernum == 0)
6723 continue;
6724
5a580b3a
AM
6725 cdeps = 0;
6726 for (n = t->deps; n != NULL; n = n->next)
6727 ++cdeps;
6728
6729 /* Add a symbol representing this version. */
6730 bh = NULL;
6731 if (! (_bfd_generic_link_add_one_symbol
6732 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6733 0, NULL, FALSE,
6734 get_elf_backend_data (dynobj)->collect, &bh)))
6735 return FALSE;
6736 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6737 h->non_elf = 0;
6738 h->def_regular = 1;
5a580b3a
AM
6739 h->type = STT_OBJECT;
6740 h->verinfo.vertree = t;
6741
c152c796 6742 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5a580b3a
AM
6743 return FALSE;
6744
6745 def.vd_version = VER_DEF_CURRENT;
6746 def.vd_flags = 0;
6747 if (t->globals.list == NULL
6748 && t->locals.list == NULL
6749 && ! t->used)
6750 def.vd_flags |= VER_FLG_WEAK;
3e3b46e5 6751 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5a580b3a
AM
6752 def.vd_cnt = cdeps + 1;
6753 def.vd_hash = bfd_elf_hash (t->name);
6754 def.vd_aux = sizeof (Elf_External_Verdef);
6755 def.vd_next = 0;
a6cc6b3b
RO
6756
6757 /* If a basever node is next, it *must* be the last node in
6758 the chain, otherwise Verdef construction breaks. */
6759 if (t->next != NULL && t->next->vernum == 0)
6760 BFD_ASSERT (t->next->next == NULL);
6761
6762 if (t->next != NULL && t->next->vernum != 0)
5a580b3a
AM
6763 def.vd_next = (sizeof (Elf_External_Verdef)
6764 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6765
6766 _bfd_elf_swap_verdef_out (output_bfd, &def,
6767 (Elf_External_Verdef *) p);
6768 p += sizeof (Elf_External_Verdef);
6769
6770 defaux.vda_name = h->dynstr_index;
6771 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6772 h->dynstr_index);
6773 defaux.vda_next = 0;
6774 if (t->deps != NULL)
6775 defaux.vda_next = sizeof (Elf_External_Verdaux);
6776 t->name_indx = defaux.vda_name;
6777
6778 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6779 (Elf_External_Verdaux *) p);
6780 p += sizeof (Elf_External_Verdaux);
6781
6782 for (n = t->deps; n != NULL; n = n->next)
6783 {
6784 if (n->version_needed == NULL)
6785 {
6786 /* This can happen if there was an error in the
6787 version script. */
6788 defaux.vda_name = 0;
6789 }
6790 else
6791 {
6792 defaux.vda_name = n->version_needed->name_indx;
6793 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6794 defaux.vda_name);
6795 }
6796 if (n->next == NULL)
6797 defaux.vda_next = 0;
6798 else
6799 defaux.vda_next = sizeof (Elf_External_Verdaux);
6800
6801 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6802 (Elf_External_Verdaux *) p);
6803 p += sizeof (Elf_External_Verdaux);
6804 }
6805 }
6806
5a580b3a
AM
6807 elf_tdata (output_bfd)->cverdefs = cdefs;
6808 }
902e9fc7
MR
6809 }
6810
6811 bed = get_elf_backend_data (output_bfd);
6812
6813 if (info->gc_sections && bed->can_gc_sections)
6814 {
6815 struct elf_gc_sweep_symbol_info sweep_info;
902e9fc7
MR
6816
6817 /* Remove the symbols that were in the swept sections from the
3d13f3e9 6818 dynamic symbol table. */
902e9fc7
MR
6819 sweep_info.info = info;
6820 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6821 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6822 &sweep_info);
3d13f3e9
AM
6823 }
6824
6825 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6826 {
6827 asection *s;
6828 struct elf_find_verdep_info sinfo;
6829
6830 /* Work out the size of the version reference section. */
6831
6832 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6833 BFD_ASSERT (s != NULL);
902e9fc7 6834
3d13f3e9
AM
6835 sinfo.info = info;
6836 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6837 if (sinfo.vers == 0)
6838 sinfo.vers = 1;
6839 sinfo.failed = FALSE;
6840
6841 elf_link_hash_traverse (elf_hash_table (info),
6842 _bfd_elf_link_find_version_dependencies,
6843 &sinfo);
6844 if (sinfo.failed)
6845 return FALSE;
6846
6847 if (elf_tdata (output_bfd)->verref == NULL)
6848 s->flags |= SEC_EXCLUDE;
6849 else
6850 {
6851 Elf_Internal_Verneed *vn;
6852 unsigned int size;
6853 unsigned int crefs;
6854 bfd_byte *p;
6855
6856 /* Build the version dependency section. */
6857 size = 0;
6858 crefs = 0;
6859 for (vn = elf_tdata (output_bfd)->verref;
6860 vn != NULL;
6861 vn = vn->vn_nextref)
6862 {
6863 Elf_Internal_Vernaux *a;
6864
6865 size += sizeof (Elf_External_Verneed);
6866 ++crefs;
6867 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6868 size += sizeof (Elf_External_Vernaux);
6869 }
6870
6871 s->size = size;
6872 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6873 if (s->contents == NULL)
6874 return FALSE;
6875
6876 p = s->contents;
6877 for (vn = elf_tdata (output_bfd)->verref;
6878 vn != NULL;
6879 vn = vn->vn_nextref)
6880 {
6881 unsigned int caux;
6882 Elf_Internal_Vernaux *a;
6883 size_t indx;
6884
6885 caux = 0;
6886 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6887 ++caux;
6888
6889 vn->vn_version = VER_NEED_CURRENT;
6890 vn->vn_cnt = caux;
6891 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6892 elf_dt_name (vn->vn_bfd) != NULL
6893 ? elf_dt_name (vn->vn_bfd)
765cf5f6
AM
6894 : lbasename (bfd_get_filename
6895 (vn->vn_bfd)),
3d13f3e9
AM
6896 FALSE);
6897 if (indx == (size_t) -1)
6898 return FALSE;
6899 vn->vn_file = indx;
6900 vn->vn_aux = sizeof (Elf_External_Verneed);
6901 if (vn->vn_nextref == NULL)
6902 vn->vn_next = 0;
6903 else
6904 vn->vn_next = (sizeof (Elf_External_Verneed)
6905 + caux * sizeof (Elf_External_Vernaux));
6906
6907 _bfd_elf_swap_verneed_out (output_bfd, vn,
6908 (Elf_External_Verneed *) p);
6909 p += sizeof (Elf_External_Verneed);
6910
6911 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6912 {
6913 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6914 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6915 a->vna_nodename, FALSE);
6916 if (indx == (size_t) -1)
6917 return FALSE;
6918 a->vna_name = indx;
6919 if (a->vna_nextptr == NULL)
6920 a->vna_next = 0;
6921 else
6922 a->vna_next = sizeof (Elf_External_Vernaux);
6923
6924 _bfd_elf_swap_vernaux_out (output_bfd, a,
6925 (Elf_External_Vernaux *) p);
6926 p += sizeof (Elf_External_Vernaux);
6927 }
6928 }
6929
6930 elf_tdata (output_bfd)->cverrefs = crefs;
6931 }
902e9fc7
MR
6932 }
6933
6934 /* Any syms created from now on start with -1 in
6935 got.refcount/offset and plt.refcount/offset. */
6936 elf_hash_table (info)->init_got_refcount
6937 = elf_hash_table (info)->init_got_offset;
6938 elf_hash_table (info)->init_plt_refcount
6939 = elf_hash_table (info)->init_plt_offset;
6940
6941 if (bfd_link_relocatable (info)
6942 && !_bfd_elf_size_group_sections (info))
6943 return FALSE;
6944
6945 /* The backend may have to create some sections regardless of whether
6946 we're dynamic or not. */
6947 if (bed->elf_backend_always_size_sections
6948 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6949 return FALSE;
6950
6951 /* Determine any GNU_STACK segment requirements, after the backend
6952 has had a chance to set a default segment size. */
6953 if (info->execstack)
6954 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6955 else if (info->noexecstack)
6956 elf_stack_flags (output_bfd) = PF_R | PF_W;
6957 else
6958 {
6959 bfd *inputobj;
6960 asection *notesec = NULL;
6961 int exec = 0;
6962
6963 for (inputobj = info->input_bfds;
6964 inputobj;
6965 inputobj = inputobj->link.next)
6966 {
6967 asection *s;
6968
6969 if (inputobj->flags
6970 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6971 continue;
57963c05
AM
6972 s = inputobj->sections;
6973 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6974 continue;
6975
902e9fc7
MR
6976 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6977 if (s)
6978 {
6979 if (s->flags & SEC_CODE)
6980 exec = PF_X;
6981 notesec = s;
6982 }
6983 else if (bed->default_execstack)
6984 exec = PF_X;
6985 }
6986 if (notesec || info->stacksize > 0)
6987 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6988 if (notesec && exec && bfd_link_relocatable (info)
6989 && notesec->output_section != bfd_abs_section_ptr)
6990 notesec->output_section->flags |= SEC_CODE;
6991 }
6992
6993 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6994 {
6995 struct elf_info_failed eif;
6996 struct elf_link_hash_entry *h;
6997 asection *dynstr;
6998 asection *s;
6999
7000 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
7001 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
7002
902e9fc7
MR
7003 if (info->symbolic)
7004 {
7005 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
7006 return FALSE;
7007 info->flags |= DF_SYMBOLIC;
7008 }
7009
7010 if (rpath != NULL)
7011 {
7012 size_t indx;
7013 bfd_vma tag;
7014
7015 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
7016 TRUE);
7017 if (indx == (size_t) -1)
7018 return FALSE;
7019
7020 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
7021 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
7022 return FALSE;
7023 }
7024
7025 if (filter_shlib != NULL)
7026 {
7027 size_t indx;
7028
7029 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7030 filter_shlib, TRUE);
7031 if (indx == (size_t) -1
7032 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
7033 return FALSE;
7034 }
7035
7036 if (auxiliary_filters != NULL)
7037 {
7038 const char * const *p;
7039
7040 for (p = auxiliary_filters; *p != NULL; p++)
7041 {
7042 size_t indx;
7043
7044 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
7045 *p, TRUE);
7046 if (indx == (size_t) -1
7047 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
7048 return FALSE;
7049 }
7050 }
7051
7052 if (audit != NULL)
7053 {
7054 size_t indx;
7055
7056 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
7057 TRUE);
7058 if (indx == (size_t) -1
7059 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
7060 return FALSE;
7061 }
7062
7063 if (depaudit != NULL)
7064 {
7065 size_t indx;
7066
7067 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
7068 TRUE);
7069 if (indx == (size_t) -1
7070 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
7071 return FALSE;
7072 }
7073
7074 eif.info = info;
7075 eif.failed = FALSE;
7076
7077 /* Find all symbols which were defined in a dynamic object and make
7078 the backend pick a reasonable value for them. */
7079 elf_link_hash_traverse (elf_hash_table (info),
7080 _bfd_elf_adjust_dynamic_symbol,
7081 &eif);
7082 if (eif.failed)
7083 return FALSE;
7084
7085 /* Add some entries to the .dynamic section. We fill in some of the
7086 values later, in bfd_elf_final_link, but we must add the entries
7087 now so that we know the final size of the .dynamic section. */
7088
7089 /* If there are initialization and/or finalization functions to
7090 call then add the corresponding DT_INIT/DT_FINI entries. */
7091 h = (info->init_function
7092 ? elf_link_hash_lookup (elf_hash_table (info),
7093 info->init_function, FALSE,
7094 FALSE, FALSE)
7095 : NULL);
7096 if (h != NULL
7097 && (h->ref_regular
7098 || h->def_regular))
7099 {
7100 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
7101 return FALSE;
7102 }
7103 h = (info->fini_function
7104 ? elf_link_hash_lookup (elf_hash_table (info),
7105 info->fini_function, FALSE,
7106 FALSE, FALSE)
7107 : NULL);
7108 if (h != NULL
7109 && (h->ref_regular
7110 || h->def_regular))
7111 {
7112 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
7113 return FALSE;
7114 }
7115
7116 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
7117 if (s != NULL && s->linker_has_input)
7118 {
7119 /* DT_PREINIT_ARRAY is not allowed in shared library. */
7120 if (! bfd_link_executable (info))
7121 {
7122 bfd *sub;
7123 asection *o;
7124
57963c05
AM
7125 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7126 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7127 && (o = sub->sections) != NULL
7128 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
902e9fc7
MR
7129 for (o = sub->sections; o != NULL; o = o->next)
7130 if (elf_section_data (o)->this_hdr.sh_type
7131 == SHT_PREINIT_ARRAY)
7132 {
7133 _bfd_error_handler
871b3ab2 7134 (_("%pB: .preinit_array section is not allowed in DSO"),
902e9fc7
MR
7135 sub);
7136 break;
7137 }
7138
7139 bfd_set_error (bfd_error_nonrepresentable_section);
7140 return FALSE;
7141 }
7142
7143 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
7144 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
7145 return FALSE;
7146 }
7147 s = bfd_get_section_by_name (output_bfd, ".init_array");
7148 if (s != NULL && s->linker_has_input)
7149 {
7150 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
7151 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
7152 return FALSE;
7153 }
7154 s = bfd_get_section_by_name (output_bfd, ".fini_array");
7155 if (s != NULL && s->linker_has_input)
7156 {
7157 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
7158 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
7159 return FALSE;
7160 }
7161
7162 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
7163 /* If .dynstr is excluded from the link, we don't want any of
7164 these tags. Strictly, we should be checking each section
7165 individually; This quick check covers for the case where
7166 someone does a /DISCARD/ : { *(*) }. */
7167 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
7168 {
7169 bfd_size_type strsize;
7170
7171 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7172 if ((info->emit_hash
7173 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
7174 || (info->emit_gnu_hash
f16a9783
MS
7175 && (bed->record_xhash_symbol == NULL
7176 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
902e9fc7
MR
7177 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7178 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7179 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7180 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7181 bed->s->sizeof_sym))
7182 return FALSE;
7183 }
7184 }
7185
7186 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7187 return FALSE;
7188
7189 /* The backend must work out the sizes of all the other dynamic
7190 sections. */
7191 if (dynobj != NULL
7192 && bed->elf_backend_size_dynamic_sections != NULL
7193 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7194 return FALSE;
7195
7196 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7197 {
902e9fc7
MR
7198 if (elf_tdata (output_bfd)->cverdefs)
7199 {
7200 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7201
7202 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7203 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7204 return FALSE;
7205 }
7206
7207 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7208 {
7209 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7210 return FALSE;
7211 }
7212 else if (info->flags & DF_BIND_NOW)
7213 {
7214 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7215 return FALSE;
7216 }
7217
7218 if (info->flags_1)
7219 {
7220 if (bfd_link_executable (info))
7221 info->flags_1 &= ~ (DF_1_INITFIRST
7222 | DF_1_NODELETE
7223 | DF_1_NOOPEN);
7224 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7225 return FALSE;
7226 }
7227
7228 if (elf_tdata (output_bfd)->cverrefs)
7229 {
7230 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7231
7232 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7233 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7234 return FALSE;
7235 }
5a580b3a 7236
8423293d
AM
7237 if ((elf_tdata (output_bfd)->cverrefs == 0
7238 && elf_tdata (output_bfd)->cverdefs == 0)
63f452a8 7239 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
8423293d 7240 {
902e9fc7
MR
7241 asection *s;
7242
3d4d4302 7243 s = bfd_get_linker_section (dynobj, ".gnu.version");
8423293d
AM
7244 s->flags |= SEC_EXCLUDE;
7245 }
7246 }
7247 return TRUE;
7248}
7249
74541ad4
AM
7250/* Find the first non-excluded output section. We'll use its
7251 section symbol for some emitted relocs. */
7252void
7253_bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7254{
7255 asection *s;
f26a3287 7256 asection *found = NULL;
74541ad4
AM
7257
7258 for (s = output_bfd->sections; s != NULL; s = s->next)
7259 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
d00dd7dc 7260 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7261 {
f26a3287
AM
7262 found = s;
7263 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7264 break;
74541ad4 7265 }
f26a3287 7266 elf_hash_table (info)->text_index_section = found;
74541ad4
AM
7267}
7268
7269/* Find two non-excluded output sections, one for code, one for data.
7270 We'll use their section symbols for some emitted relocs. */
7271void
7272_bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7273{
7274 asection *s;
f26a3287 7275 asection *found = NULL;
74541ad4 7276
266b05cf 7277 /* Data first, since setting text_index_section changes
7f923b7f 7278 _bfd_elf_omit_section_dynsym_default. */
74541ad4 7279 for (s = output_bfd->sections; s != NULL; s = s->next)
f26a3287
AM
7280 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7281 && !(s->flags & SEC_READONLY)
d00dd7dc 7282 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7283 {
f26a3287
AM
7284 found = s;
7285 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7286 break;
74541ad4 7287 }
f26a3287 7288 elf_hash_table (info)->data_index_section = found;
74541ad4
AM
7289
7290 for (s = output_bfd->sections; s != NULL; s = s->next)
f26a3287
AM
7291 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7292 && (s->flags & SEC_READONLY)
d00dd7dc 7293 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
74541ad4 7294 {
f26a3287 7295 found = s;
74541ad4
AM
7296 break;
7297 }
f26a3287 7298 elf_hash_table (info)->text_index_section = found;
74541ad4
AM
7299}
7300
f16a9783
MS
7301#define GNU_HASH_SECTION_NAME(bed) \
7302 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7303
8423293d
AM
7304bfd_boolean
7305bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7306{
74541ad4 7307 const struct elf_backend_data *bed;
23ec1e32 7308 unsigned long section_sym_count;
96d01d93 7309 bfd_size_type dynsymcount = 0;
74541ad4 7310
8423293d
AM
7311 if (!is_elf_hash_table (info->hash))
7312 return TRUE;
7313
74541ad4
AM
7314 bed = get_elf_backend_data (output_bfd);
7315 (*bed->elf_backend_init_index_section) (output_bfd, info);
7316
23ec1e32
MR
7317 /* Assign dynsym indices. In a shared library we generate a section
7318 symbol for each output section, which come first. Next come all
7319 of the back-end allocated local dynamic syms, followed by the rest
7320 of the global symbols.
7321
7322 This is usually not needed for static binaries, however backends
7323 can request to always do it, e.g. the MIPS backend uses dynamic
7324 symbol counts to lay out GOT, which will be produced in the
7325 presence of GOT relocations even in static binaries (holding fixed
7326 data in that case, to satisfy those relocations). */
7327
7328 if (elf_hash_table (info)->dynamic_sections_created
7329 || bed->always_renumber_dynsyms)
7330 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7331 &section_sym_count);
7332
8423293d
AM
7333 if (elf_hash_table (info)->dynamic_sections_created)
7334 {
7335 bfd *dynobj;
8423293d 7336 asection *s;
8423293d
AM
7337 unsigned int dtagcount;
7338
7339 dynobj = elf_hash_table (info)->dynobj;
7340
5a580b3a 7341 /* Work out the size of the symbol version section. */
3d4d4302 7342 s = bfd_get_linker_section (dynobj, ".gnu.version");
5a580b3a 7343 BFD_ASSERT (s != NULL);
d5486c43 7344 if ((s->flags & SEC_EXCLUDE) == 0)
5a580b3a 7345 {
eea6121a 7346 s->size = dynsymcount * sizeof (Elf_External_Versym);
a50b1753 7347 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
5a580b3a
AM
7348 if (s->contents == NULL)
7349 return FALSE;
7350
7351 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7352 return FALSE;
7353 }
7354
7355 /* Set the size of the .dynsym and .hash sections. We counted
7356 the number of dynamic symbols in elf_link_add_object_symbols.
7357 We will build the contents of .dynsym and .hash when we build
7358 the final symbol table, because until then we do not know the
7359 correct value to give the symbols. We built the .dynstr
7360 section as we went along in elf_link_add_object_symbols. */
cae1fbbb 7361 s = elf_hash_table (info)->dynsym;
5a580b3a 7362 BFD_ASSERT (s != NULL);
eea6121a 7363 s->size = dynsymcount * bed->s->sizeof_sym;
5a580b3a 7364
d5486c43
L
7365 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7366 if (s->contents == NULL)
7367 return FALSE;
5a580b3a 7368
d5486c43
L
7369 /* The first entry in .dynsym is a dummy symbol. Clear all the
7370 section syms, in case we don't output them all. */
7371 ++section_sym_count;
7372 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5a580b3a 7373
fdc90cb4
JJ
7374 elf_hash_table (info)->bucketcount = 0;
7375
5a580b3a
AM
7376 /* Compute the size of the hashing table. As a side effect this
7377 computes the hash values for all the names we export. */
fdc90cb4
JJ
7378 if (info->emit_hash)
7379 {
7380 unsigned long int *hashcodes;
14b1c01e 7381 struct hash_codes_info hashinf;
fdc90cb4
JJ
7382 bfd_size_type amt;
7383 unsigned long int nsyms;
7384 size_t bucketcount;
7385 size_t hash_entry_size;
7386
7387 /* Compute the hash values for all exported symbols. At the same
7388 time store the values in an array so that we could use them for
7389 optimizations. */
7390 amt = dynsymcount * sizeof (unsigned long int);
a50b1753 7391 hashcodes = (unsigned long int *) bfd_malloc (amt);
fdc90cb4
JJ
7392 if (hashcodes == NULL)
7393 return FALSE;
14b1c01e
AM
7394 hashinf.hashcodes = hashcodes;
7395 hashinf.error = FALSE;
5a580b3a 7396
fdc90cb4
JJ
7397 /* Put all hash values in HASHCODES. */
7398 elf_link_hash_traverse (elf_hash_table (info),
14b1c01e
AM
7399 elf_collect_hash_codes, &hashinf);
7400 if (hashinf.error)
4dd07732
AM
7401 {
7402 free (hashcodes);
7403 return FALSE;
7404 }
5a580b3a 7405
14b1c01e 7406 nsyms = hashinf.hashcodes - hashcodes;
fdc90cb4
JJ
7407 bucketcount
7408 = compute_bucket_count (info, hashcodes, nsyms, 0);
7409 free (hashcodes);
7410
4b48e2f6 7411 if (bucketcount == 0 && nsyms > 0)
fdc90cb4 7412 return FALSE;
5a580b3a 7413
fdc90cb4
JJ
7414 elf_hash_table (info)->bucketcount = bucketcount;
7415
3d4d4302 7416 s = bfd_get_linker_section (dynobj, ".hash");
fdc90cb4
JJ
7417 BFD_ASSERT (s != NULL);
7418 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7419 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
a50b1753 7420 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7421 if (s->contents == NULL)
7422 return FALSE;
7423
7424 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7425 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7426 s->contents + hash_entry_size);
7427 }
7428
7429 if (info->emit_gnu_hash)
7430 {
7431 size_t i, cnt;
7432 unsigned char *contents;
7433 struct collect_gnu_hash_codes cinfo;
7434 bfd_size_type amt;
7435 size_t bucketcount;
7436
7437 memset (&cinfo, 0, sizeof (cinfo));
7438
7439 /* Compute the hash values for all exported symbols. At the same
7440 time store the values in an array so that we could use them for
7441 optimizations. */
7442 amt = dynsymcount * 2 * sizeof (unsigned long int);
a50b1753 7443 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
fdc90cb4
JJ
7444 if (cinfo.hashcodes == NULL)
7445 return FALSE;
7446
7447 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7448 cinfo.min_dynindx = -1;
7449 cinfo.output_bfd = output_bfd;
7450 cinfo.bed = bed;
7451
7452 /* Put all hash values in HASHCODES. */
7453 elf_link_hash_traverse (elf_hash_table (info),
7454 elf_collect_gnu_hash_codes, &cinfo);
14b1c01e 7455 if (cinfo.error)
4dd07732
AM
7456 {
7457 free (cinfo.hashcodes);
7458 return FALSE;
7459 }
fdc90cb4
JJ
7460
7461 bucketcount
7462 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7463
7464 if (bucketcount == 0)
7465 {
7466 free (cinfo.hashcodes);
7467 return FALSE;
7468 }
7469
f16a9783 7470 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
fdc90cb4
JJ
7471 BFD_ASSERT (s != NULL);
7472
7473 if (cinfo.nsyms == 0)
7474 {
f16a9783 7475 /* Empty .gnu.hash or .MIPS.xhash section is special. */
fdc90cb4
JJ
7476 BFD_ASSERT (cinfo.min_dynindx == -1);
7477 free (cinfo.hashcodes);
7478 s->size = 5 * 4 + bed->s->arch_size / 8;
a50b1753 7479 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7480 if (contents == NULL)
7481 return FALSE;
7482 s->contents = contents;
7483 /* 1 empty bucket. */
7484 bfd_put_32 (output_bfd, 1, contents);
7485 /* SYMIDX above the special symbol 0. */
7486 bfd_put_32 (output_bfd, 1, contents + 4);
7487 /* Just one word for bitmask. */
7488 bfd_put_32 (output_bfd, 1, contents + 8);
7489 /* Only hash fn bloom filter. */
7490 bfd_put_32 (output_bfd, 0, contents + 12);
7491 /* No hashes are valid - empty bitmask. */
7492 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7493 /* No hashes in the only bucket. */
7494 bfd_put_32 (output_bfd, 0,
7495 contents + 16 + bed->s->arch_size / 8);
7496 }
7497 else
7498 {
9e6619e2 7499 unsigned long int maskwords, maskbitslog2, x;
0b33793d 7500 BFD_ASSERT (cinfo.min_dynindx != -1);
fdc90cb4 7501
9e6619e2
AM
7502 x = cinfo.nsyms;
7503 maskbitslog2 = 1;
7504 while ((x >>= 1) != 0)
7505 ++maskbitslog2;
fdc90cb4
JJ
7506 if (maskbitslog2 < 3)
7507 maskbitslog2 = 5;
7508 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7509 maskbitslog2 = maskbitslog2 + 3;
7510 else
7511 maskbitslog2 = maskbitslog2 + 2;
7512 if (bed->s->arch_size == 64)
7513 {
7514 if (maskbitslog2 == 5)
7515 maskbitslog2 = 6;
7516 cinfo.shift1 = 6;
7517 }
7518 else
7519 cinfo.shift1 = 5;
7520 cinfo.mask = (1 << cinfo.shift1) - 1;
2ccdbfcc 7521 cinfo.shift2 = maskbitslog2;
fdc90cb4
JJ
7522 cinfo.maskbits = 1 << maskbitslog2;
7523 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7524 amt = bucketcount * sizeof (unsigned long int) * 2;
7525 amt += maskwords * sizeof (bfd_vma);
a50b1753 7526 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
fdc90cb4
JJ
7527 if (cinfo.bitmask == NULL)
7528 {
7529 free (cinfo.hashcodes);
7530 return FALSE;
7531 }
7532
a50b1753 7533 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
fdc90cb4
JJ
7534 cinfo.indx = cinfo.counts + bucketcount;
7535 cinfo.symindx = dynsymcount - cinfo.nsyms;
7536 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7537
7538 /* Determine how often each hash bucket is used. */
7539 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7540 for (i = 0; i < cinfo.nsyms; ++i)
7541 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7542
7543 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7544 if (cinfo.counts[i] != 0)
7545 {
7546 cinfo.indx[i] = cnt;
7547 cnt += cinfo.counts[i];
7548 }
7549 BFD_ASSERT (cnt == dynsymcount);
7550 cinfo.bucketcount = bucketcount;
7551 cinfo.local_indx = cinfo.min_dynindx;
7552
7553 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7554 s->size += cinfo.maskbits / 8;
f16a9783
MS
7555 if (bed->record_xhash_symbol != NULL)
7556 s->size += cinfo.nsyms * 4;
a50b1753 7557 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
fdc90cb4
JJ
7558 if (contents == NULL)
7559 {
7560 free (cinfo.bitmask);
7561 free (cinfo.hashcodes);
7562 return FALSE;
7563 }
7564
7565 s->contents = contents;
7566 bfd_put_32 (output_bfd, bucketcount, contents);
7567 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7568 bfd_put_32 (output_bfd, maskwords, contents + 8);
7569 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7570 contents += 16 + cinfo.maskbits / 8;
7571
7572 for (i = 0; i < bucketcount; ++i)
7573 {
7574 if (cinfo.counts[i] == 0)
7575 bfd_put_32 (output_bfd, 0, contents);
7576 else
7577 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7578 contents += 4;
7579 }
7580
7581 cinfo.contents = contents;
7582
f16a9783
MS
7583 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7584 /* Renumber dynamic symbols, if populating .gnu.hash section.
7585 If using .MIPS.xhash, populate the translation table. */
fdc90cb4 7586 elf_link_hash_traverse (elf_hash_table (info),
f16a9783 7587 elf_gnu_hash_process_symidx, &cinfo);
fdc90cb4
JJ
7588
7589 contents = s->contents + 16;
7590 for (i = 0; i < maskwords; ++i)
7591 {
7592 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7593 contents);
7594 contents += bed->s->arch_size / 8;
7595 }
7596
7597 free (cinfo.bitmask);
7598 free (cinfo.hashcodes);
7599 }
7600 }
5a580b3a 7601
3d4d4302 7602 s = bfd_get_linker_section (dynobj, ".dynstr");
5a580b3a
AM
7603 BFD_ASSERT (s != NULL);
7604
4ad4eba5 7605 elf_finalize_dynstr (output_bfd, info);
5a580b3a 7606
eea6121a 7607 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5a580b3a
AM
7608
7609 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7610 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7611 return FALSE;
7612 }
7613
7614 return TRUE;
7615}
4d269e42 7616\f
4d269e42
AM
7617/* Make sure sec_info_type is cleared if sec_info is cleared too. */
7618
7619static void
7620merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7621 asection *sec)
7622{
dbaa2011
AM
7623 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7624 sec->sec_info_type = SEC_INFO_TYPE_NONE;
4d269e42
AM
7625}
7626
7627/* Finish SHF_MERGE section merging. */
7628
7629bfd_boolean
630993ec 7630_bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
4d269e42
AM
7631{
7632 bfd *ibfd;
7633 asection *sec;
7634
7635 if (!is_elf_hash_table (info->hash))
7636 return FALSE;
7637
c72f2fb2 7638 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
630993ec
AM
7639 if ((ibfd->flags & DYNAMIC) == 0
7640 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
017e6bce
AM
7641 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7642 == get_elf_backend_data (obfd)->s->elfclass))
4d269e42
AM
7643 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7644 if ((sec->flags & SEC_MERGE) != 0
7645 && !bfd_is_abs_section (sec->output_section))
7646 {
7647 struct bfd_elf_section_data *secdata;
7648
7649 secdata = elf_section_data (sec);
630993ec 7650 if (! _bfd_add_merge_section (obfd,
4d269e42
AM
7651 &elf_hash_table (info)->merge_info,
7652 sec, &secdata->sec_info))
7653 return FALSE;
7654 else if (secdata->sec_info)
dbaa2011 7655 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
4d269e42
AM
7656 }
7657
7658 if (elf_hash_table (info)->merge_info != NULL)
630993ec 7659 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
4d269e42
AM
7660 merge_sections_remove_hook);
7661 return TRUE;
7662}
7663
7664/* Create an entry in an ELF linker hash table. */
7665
7666struct bfd_hash_entry *
7667_bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7668 struct bfd_hash_table *table,
7669 const char *string)
7670{
7671 /* Allocate the structure if it has not already been allocated by a
7672 subclass. */
7673 if (entry == NULL)
7674 {
a50b1753 7675 entry = (struct bfd_hash_entry *)
ca4be51c 7676 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
4d269e42
AM
7677 if (entry == NULL)
7678 return entry;
7679 }
7680
7681 /* Call the allocation method of the superclass. */
7682 entry = _bfd_link_hash_newfunc (entry, table, string);
7683 if (entry != NULL)
7684 {
7685 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7686 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7687
7688 /* Set local fields. */
7689 ret->indx = -1;
7690 ret->dynindx = -1;
7691 ret->got = htab->init_got_refcount;
7692 ret->plt = htab->init_plt_refcount;
7693 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7694 - offsetof (struct elf_link_hash_entry, size)));
7695 /* Assume that we have been called by a non-ELF symbol reader.
7696 This flag is then reset by the code which reads an ELF input
7697 file. This ensures that a symbol created by a non-ELF symbol
7698 reader will have the flag set correctly. */
7699 ret->non_elf = 1;
7700 }
7701
7702 return entry;
7703}
7704
7705/* Copy data from an indirect symbol to its direct symbol, hiding the
7706 old indirect symbol. Also used for copying flags to a weakdef. */
7707
7708void
7709_bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7710 struct elf_link_hash_entry *dir,
7711 struct elf_link_hash_entry *ind)
7712{
7713 struct elf_link_hash_table *htab;
7714
ad172eaa
L
7715 if (ind->dyn_relocs != NULL)
7716 {
7717 if (dir->dyn_relocs != NULL)
7718 {
7719 struct elf_dyn_relocs **pp;
7720 struct elf_dyn_relocs *p;
7721
7722 /* Add reloc counts against the indirect sym to the direct sym
7723 list. Merge any entries against the same section. */
7724 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
7725 {
7726 struct elf_dyn_relocs *q;
7727
7728 for (q = dir->dyn_relocs; q != NULL; q = q->next)
7729 if (q->sec == p->sec)
7730 {
7731 q->pc_count += p->pc_count;
7732 q->count += p->count;
7733 *pp = p->next;
7734 break;
7735 }
7736 if (q == NULL)
7737 pp = &p->next;
7738 }
7739 *pp = dir->dyn_relocs;
7740 }
7741
7742 dir->dyn_relocs = ind->dyn_relocs;
7743 ind->dyn_relocs = NULL;
7744 }
7745
4d269e42 7746 /* Copy down any references that we may have already seen to the
e81830c5 7747 symbol which just became indirect. */
4d269e42 7748
422f1182 7749 if (dir->versioned != versioned_hidden)
e81830c5
AM
7750 dir->ref_dynamic |= ind->ref_dynamic;
7751 dir->ref_regular |= ind->ref_regular;
7752 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7753 dir->non_got_ref |= ind->non_got_ref;
7754 dir->needs_plt |= ind->needs_plt;
7755 dir->pointer_equality_needed |= ind->pointer_equality_needed;
4d269e42
AM
7756
7757 if (ind->root.type != bfd_link_hash_indirect)
7758 return;
7759
7760 /* Copy over the global and procedure linkage table refcount entries.
7761 These may have been already set up by a check_relocs routine. */
7762 htab = elf_hash_table (info);
7763 if (ind->got.refcount > htab->init_got_refcount.refcount)
7764 {
7765 if (dir->got.refcount < 0)
7766 dir->got.refcount = 0;
7767 dir->got.refcount += ind->got.refcount;
7768 ind->got.refcount = htab->init_got_refcount.refcount;
7769 }
7770
7771 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7772 {
7773 if (dir->plt.refcount < 0)
7774 dir->plt.refcount = 0;
7775 dir->plt.refcount += ind->plt.refcount;
7776 ind->plt.refcount = htab->init_plt_refcount.refcount;
7777 }
7778
7779 if (ind->dynindx != -1)
7780 {
7781 if (dir->dynindx != -1)
7782 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7783 dir->dynindx = ind->dynindx;
7784 dir->dynstr_index = ind->dynstr_index;
7785 ind->dynindx = -1;
7786 ind->dynstr_index = 0;
7787 }
7788}
7789
7790void
7791_bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7792 struct elf_link_hash_entry *h,
7793 bfd_boolean force_local)
7794{
3aa14d16
L
7795 /* STT_GNU_IFUNC symbol must go through PLT. */
7796 if (h->type != STT_GNU_IFUNC)
7797 {
7798 h->plt = elf_hash_table (info)->init_plt_offset;
7799 h->needs_plt = 0;
7800 }
4d269e42
AM
7801 if (force_local)
7802 {
7803 h->forced_local = 1;
7804 if (h->dynindx != -1)
7805 {
4d269e42
AM
7806 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7807 h->dynstr_index);
641338d8
AM
7808 h->dynindx = -1;
7809 h->dynstr_index = 0;
4d269e42
AM
7810 }
7811 }
7812}
7813
34a87bb0
L
7814/* Hide a symbol. */
7815
7816void
7817_bfd_elf_link_hide_symbol (bfd *output_bfd,
7818 struct bfd_link_info *info,
7819 struct bfd_link_hash_entry *h)
7820{
7821 if (is_elf_hash_table (info->hash))
7822 {
7823 const struct elf_backend_data *bed
7824 = get_elf_backend_data (output_bfd);
7825 struct elf_link_hash_entry *eh
7826 = (struct elf_link_hash_entry *) h;
7827 bed->elf_backend_hide_symbol (info, eh, TRUE);
7828 eh->def_dynamic = 0;
7829 eh->ref_dynamic = 0;
7830 eh->dynamic_def = 0;
7831 }
7832}
7833
7bf52ea2
AM
7834/* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7835 caller. */
4d269e42
AM
7836
7837bfd_boolean
7838_bfd_elf_link_hash_table_init
7839 (struct elf_link_hash_table *table,
7840 bfd *abfd,
7841 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7842 struct bfd_hash_table *,
7843 const char *),
4dfe6ac6
NC
7844 unsigned int entsize,
7845 enum elf_target_id target_id)
4d269e42
AM
7846{
7847 bfd_boolean ret;
7848 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7849
4d269e42
AM
7850 table->init_got_refcount.refcount = can_refcount - 1;
7851 table->init_plt_refcount.refcount = can_refcount - 1;
7852 table->init_got_offset.offset = -(bfd_vma) 1;
7853 table->init_plt_offset.offset = -(bfd_vma) 1;
7854 /* The first dynamic symbol is a dummy. */
7855 table->dynsymcount = 1;
7856
7857 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
4dfe6ac6 7858
4d269e42 7859 table->root.type = bfd_link_elf_hash_table;
4dfe6ac6 7860 table->hash_table_id = target_id;
90c14f0c 7861 table->target_os = get_elf_backend_data (abfd)->target_os;
4d269e42
AM
7862
7863 return ret;
7864}
7865
7866/* Create an ELF linker hash table. */
7867
7868struct bfd_link_hash_table *
7869_bfd_elf_link_hash_table_create (bfd *abfd)
7870{
7871 struct elf_link_hash_table *ret;
986f0783 7872 size_t amt = sizeof (struct elf_link_hash_table);
4d269e42 7873
7bf52ea2 7874 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
4d269e42
AM
7875 if (ret == NULL)
7876 return NULL;
7877
7878 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
7879 sizeof (struct elf_link_hash_entry),
7880 GENERIC_ELF_DATA))
4d269e42
AM
7881 {
7882 free (ret);
7883 return NULL;
7884 }
d495ab0d 7885 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
4d269e42
AM
7886
7887 return &ret->root;
7888}
7889
9f7c3e5e
AM
7890/* Destroy an ELF linker hash table. */
7891
7892void
d495ab0d 7893_bfd_elf_link_hash_table_free (bfd *obfd)
9f7c3e5e 7894{
d495ab0d
AM
7895 struct elf_link_hash_table *htab;
7896
7897 htab = (struct elf_link_hash_table *) obfd->link.hash;
9f7c3e5e
AM
7898 if (htab->dynstr != NULL)
7899 _bfd_elf_strtab_free (htab->dynstr);
7900 _bfd_merge_sections_free (htab->merge_info);
d495ab0d 7901 _bfd_generic_link_hash_table_free (obfd);
9f7c3e5e
AM
7902}
7903
4d269e42
AM
7904/* This is a hook for the ELF emulation code in the generic linker to
7905 tell the backend linker what file name to use for the DT_NEEDED
7906 entry for a dynamic object. */
7907
7908void
7909bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7910{
7911 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7912 && bfd_get_format (abfd) == bfd_object)
7913 elf_dt_name (abfd) = name;
7914}
7915
7916int
7917bfd_elf_get_dyn_lib_class (bfd *abfd)
7918{
7919 int lib_class;
7920 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7921 && bfd_get_format (abfd) == bfd_object)
7922 lib_class = elf_dyn_lib_class (abfd);
7923 else
7924 lib_class = 0;
7925 return lib_class;
7926}
7927
7928void
7929bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7930{
7931 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7932 && bfd_get_format (abfd) == bfd_object)
7933 elf_dyn_lib_class (abfd) = lib_class;
7934}
7935
7936/* Get the list of DT_NEEDED entries for a link. This is a hook for
7937 the linker ELF emulation code. */
7938
7939struct bfd_link_needed_list *
7940bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7941 struct bfd_link_info *info)
7942{
7943 if (! is_elf_hash_table (info->hash))
7944 return NULL;
7945 return elf_hash_table (info)->needed;
7946}
7947
7948/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7949 hook for the linker ELF emulation code. */
7950
7951struct bfd_link_needed_list *
7952bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7953 struct bfd_link_info *info)
7954{
7955 if (! is_elf_hash_table (info->hash))
7956 return NULL;
7957 return elf_hash_table (info)->runpath;
7958}
7959
7960/* Get the name actually used for a dynamic object for a link. This
7961 is the SONAME entry if there is one. Otherwise, it is the string
7962 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7963
7964const char *
7965bfd_elf_get_dt_soname (bfd *abfd)
7966{
7967 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7968 && bfd_get_format (abfd) == bfd_object)
7969 return elf_dt_name (abfd);
7970 return NULL;
7971}
7972
7973/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7974 the ELF linker emulation code. */
7975
7976bfd_boolean
7977bfd_elf_get_bfd_needed_list (bfd *abfd,
7978 struct bfd_link_needed_list **pneeded)
7979{
7980 asection *s;
7981 bfd_byte *dynbuf = NULL;
cb33740c 7982 unsigned int elfsec;
4d269e42
AM
7983 unsigned long shlink;
7984 bfd_byte *extdyn, *extdynend;
7985 size_t extdynsize;
7986 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7987
7988 *pneeded = NULL;
7989
7990 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7991 || bfd_get_format (abfd) != bfd_object)
7992 return TRUE;
7993
7994 s = bfd_get_section_by_name (abfd, ".dynamic");
7995 if (s == NULL || s->size == 0)
7996 return TRUE;
7997
7998 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7999 goto error_return;
8000
8001 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 8002 if (elfsec == SHN_BAD)
4d269e42
AM
8003 goto error_return;
8004
8005 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
c152c796 8006
4d269e42
AM
8007 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
8008 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
8009
8010 extdyn = dynbuf;
8011 extdynend = extdyn + s->size;
8012 for (; extdyn < extdynend; extdyn += extdynsize)
8013 {
8014 Elf_Internal_Dyn dyn;
8015
8016 (*swap_dyn_in) (abfd, extdyn, &dyn);
8017
8018 if (dyn.d_tag == DT_NULL)
8019 break;
8020
8021 if (dyn.d_tag == DT_NEEDED)
8022 {
8023 const char *string;
8024 struct bfd_link_needed_list *l;
8025 unsigned int tagv = dyn.d_un.d_val;
986f0783 8026 size_t amt;
4d269e42
AM
8027
8028 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
8029 if (string == NULL)
8030 goto error_return;
8031
8032 amt = sizeof *l;
a50b1753 8033 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4d269e42
AM
8034 if (l == NULL)
8035 goto error_return;
8036
8037 l->by = abfd;
8038 l->name = string;
8039 l->next = *pneeded;
8040 *pneeded = l;
8041 }
8042 }
8043
8044 free (dynbuf);
8045
8046 return TRUE;
8047
8048 error_return:
c9594989 8049 free (dynbuf);
4d269e42
AM
8050 return FALSE;
8051}
8052
8053struct elf_symbuf_symbol
8054{
8055 unsigned long st_name; /* Symbol name, index in string tbl */
8056 unsigned char st_info; /* Type and binding attributes */
8057 unsigned char st_other; /* Visibilty, and target specific */
8058};
8059
8060struct elf_symbuf_head
8061{
8062 struct elf_symbuf_symbol *ssym;
ef53be89 8063 size_t count;
4d269e42
AM
8064 unsigned int st_shndx;
8065};
8066
8067struct elf_symbol
8068{
8069 union
8070 {
8071 Elf_Internal_Sym *isym;
8072 struct elf_symbuf_symbol *ssym;
dcea6a95 8073 void *p;
4d269e42
AM
8074 } u;
8075 const char *name;
8076};
8077
8078/* Sort references to symbols by ascending section number. */
8079
8080static int
8081elf_sort_elf_symbol (const void *arg1, const void *arg2)
8082{
8083 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
8084 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
8085
dcea6a95
AM
8086 if (s1->st_shndx != s2->st_shndx)
8087 return s1->st_shndx > s2->st_shndx ? 1 : -1;
8088 /* Final sort by the address of the sym in the symbuf ensures
8089 a stable sort. */
8090 if (s1 != s2)
8091 return s1 > s2 ? 1 : -1;
8092 return 0;
4d269e42
AM
8093}
8094
8095static int
8096elf_sym_name_compare (const void *arg1, const void *arg2)
8097{
8098 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8099 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
dcea6a95
AM
8100 int ret = strcmp (s1->name, s2->name);
8101 if (ret != 0)
8102 return ret;
8103 if (s1->u.p != s2->u.p)
8104 return s1->u.p > s2->u.p ? 1 : -1;
8105 return 0;
4d269e42
AM
8106}
8107
8108static struct elf_symbuf_head *
ef53be89 8109elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
4d269e42 8110{
14b1c01e 8111 Elf_Internal_Sym **ind, **indbufend, **indbuf;
4d269e42
AM
8112 struct elf_symbuf_symbol *ssym;
8113 struct elf_symbuf_head *ssymbuf, *ssymhead;
446f7ed5 8114 size_t i, shndx_count, total_size, amt;
4d269e42 8115
446f7ed5
AM
8116 amt = symcount * sizeof (*indbuf);
8117 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
4d269e42
AM
8118 if (indbuf == NULL)
8119 return NULL;
8120
8121 for (ind = indbuf, i = 0; i < symcount; i++)
8122 if (isymbuf[i].st_shndx != SHN_UNDEF)
8123 *ind++ = &isymbuf[i];
8124 indbufend = ind;
8125
8126 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
8127 elf_sort_elf_symbol);
8128
8129 shndx_count = 0;
8130 if (indbufend > indbuf)
8131 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
8132 if (ind[0]->st_shndx != ind[1]->st_shndx)
8133 shndx_count++;
8134
3ae181ee
L
8135 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
8136 + (indbufend - indbuf) * sizeof (*ssym));
a50b1753 8137 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
4d269e42
AM
8138 if (ssymbuf == NULL)
8139 {
8140 free (indbuf);
8141 return NULL;
8142 }
8143
3ae181ee 8144 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
4d269e42
AM
8145 ssymbuf->ssym = NULL;
8146 ssymbuf->count = shndx_count;
8147 ssymbuf->st_shndx = 0;
8148 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
8149 {
8150 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
8151 {
8152 ssymhead++;
8153 ssymhead->ssym = ssym;
8154 ssymhead->count = 0;
8155 ssymhead->st_shndx = (*ind)->st_shndx;
8156 }
8157 ssym->st_name = (*ind)->st_name;
8158 ssym->st_info = (*ind)->st_info;
8159 ssym->st_other = (*ind)->st_other;
8160 ssymhead->count++;
8161 }
ef53be89 8162 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
3ae181ee
L
8163 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
8164 == total_size));
4d269e42
AM
8165
8166 free (indbuf);
8167 return ssymbuf;
8168}
8169
8170/* Check if 2 sections define the same set of local and global
8171 symbols. */
8172
8f317e31 8173static bfd_boolean
4d269e42
AM
8174bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
8175 struct bfd_link_info *info)
8176{
8177 bfd *bfd1, *bfd2;
8178 const struct elf_backend_data *bed1, *bed2;
8179 Elf_Internal_Shdr *hdr1, *hdr2;
ef53be89 8180 size_t symcount1, symcount2;
4d269e42
AM
8181 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8182 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
8183 Elf_Internal_Sym *isym, *isymend;
8184 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
ef53be89 8185 size_t count1, count2, i;
cb33740c 8186 unsigned int shndx1, shndx2;
4d269e42
AM
8187 bfd_boolean result;
8188
8189 bfd1 = sec1->owner;
8190 bfd2 = sec2->owner;
8191
4d269e42
AM
8192 /* Both sections have to be in ELF. */
8193 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8194 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8195 return FALSE;
8196
8197 if (elf_section_type (sec1) != elf_section_type (sec2))
8198 return FALSE;
8199
4d269e42
AM
8200 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8201 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
cb33740c 8202 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
4d269e42
AM
8203 return FALSE;
8204
8205 bed1 = get_elf_backend_data (bfd1);
8206 bed2 = get_elf_backend_data (bfd2);
8207 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8208 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8209 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8210 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8211
8212 if (symcount1 == 0 || symcount2 == 0)
8213 return FALSE;
8214
8215 result = FALSE;
8216 isymbuf1 = NULL;
8217 isymbuf2 = NULL;
a50b1753
NC
8218 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8219 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
4d269e42
AM
8220
8221 if (ssymbuf1 == NULL)
8222 {
8223 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8224 NULL, NULL, NULL);
8225 if (isymbuf1 == NULL)
8226 goto done;
8227
67411cbf 8228 if (info != NULL && !info->reduce_memory_overheads)
dcea6a95
AM
8229 {
8230 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8231 elf_tdata (bfd1)->symbuf = ssymbuf1;
8232 }
4d269e42
AM
8233 }
8234
8235 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8236 {
8237 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8238 NULL, NULL, NULL);
8239 if (isymbuf2 == NULL)
8240 goto done;
8241
67411cbf 8242 if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
dcea6a95
AM
8243 {
8244 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8245 elf_tdata (bfd2)->symbuf = ssymbuf2;
8246 }
4d269e42
AM
8247 }
8248
8249 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8250 {
8251 /* Optimized faster version. */
ef53be89 8252 size_t lo, hi, mid;
4d269e42
AM
8253 struct elf_symbol *symp;
8254 struct elf_symbuf_symbol *ssym, *ssymend;
8255
8256 lo = 0;
8257 hi = ssymbuf1->count;
8258 ssymbuf1++;
8259 count1 = 0;
8260 while (lo < hi)
8261 {
8262 mid = (lo + hi) / 2;
cb33740c 8263 if (shndx1 < ssymbuf1[mid].st_shndx)
4d269e42 8264 hi = mid;
cb33740c 8265 else if (shndx1 > ssymbuf1[mid].st_shndx)
4d269e42
AM
8266 lo = mid + 1;
8267 else
8268 {
8269 count1 = ssymbuf1[mid].count;
8270 ssymbuf1 += mid;
8271 break;
8272 }
8273 }
8274
8275 lo = 0;
8276 hi = ssymbuf2->count;
8277 ssymbuf2++;
8278 count2 = 0;
8279 while (lo < hi)
8280 {
8281 mid = (lo + hi) / 2;
cb33740c 8282 if (shndx2 < ssymbuf2[mid].st_shndx)
4d269e42 8283 hi = mid;
cb33740c 8284 else if (shndx2 > ssymbuf2[mid].st_shndx)
4d269e42
AM
8285 lo = mid + 1;
8286 else
8287 {
8288 count2 = ssymbuf2[mid].count;
8289 ssymbuf2 += mid;
8290 break;
8291 }
8292 }
8293
8294 if (count1 == 0 || count2 == 0 || count1 != count2)
8295 goto done;
8296
ca4be51c
AM
8297 symtable1
8298 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8299 symtable2
8300 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
4d269e42
AM
8301 if (symtable1 == NULL || symtable2 == NULL)
8302 goto done;
8303
8304 symp = symtable1;
8305 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
8306 ssym < ssymend; ssym++, symp++)
8307 {
8308 symp->u.ssym = ssym;
8309 symp->name = bfd_elf_string_from_elf_section (bfd1,
8310 hdr1->sh_link,
8311 ssym->st_name);
8312 }
8313
8314 symp = symtable2;
8315 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8316 ssym < ssymend; ssym++, symp++)
8317 {
8318 symp->u.ssym = ssym;
8319 symp->name = bfd_elf_string_from_elf_section (bfd2,
8320 hdr2->sh_link,
8321 ssym->st_name);
8322 }
8323
8324 /* Sort symbol by name. */
8325 qsort (symtable1, count1, sizeof (struct elf_symbol),
8326 elf_sym_name_compare);
8327 qsort (symtable2, count1, sizeof (struct elf_symbol),
8328 elf_sym_name_compare);
8329
8330 for (i = 0; i < count1; i++)
8331 /* Two symbols must have the same binding, type and name. */
8332 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8333 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8334 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8335 goto done;
8336
8337 result = TRUE;
8338 goto done;
8339 }
8340
a50b1753
NC
8341 symtable1 = (struct elf_symbol *)
8342 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8343 symtable2 = (struct elf_symbol *)
8344 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
4d269e42
AM
8345 if (symtable1 == NULL || symtable2 == NULL)
8346 goto done;
8347
8348 /* Count definitions in the section. */
8349 count1 = 0;
8350 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
cb33740c 8351 if (isym->st_shndx == shndx1)
4d269e42
AM
8352 symtable1[count1++].u.isym = isym;
8353
8354 count2 = 0;
8355 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
cb33740c 8356 if (isym->st_shndx == shndx2)
4d269e42
AM
8357 symtable2[count2++].u.isym = isym;
8358
8359 if (count1 == 0 || count2 == 0 || count1 != count2)
8360 goto done;
8361
8362 for (i = 0; i < count1; i++)
8363 symtable1[i].name
8364 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8365 symtable1[i].u.isym->st_name);
8366
8367 for (i = 0; i < count2; i++)
8368 symtable2[i].name
8369 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8370 symtable2[i].u.isym->st_name);
8371
8372 /* Sort symbol by name. */
8373 qsort (symtable1, count1, sizeof (struct elf_symbol),
8374 elf_sym_name_compare);
8375 qsort (symtable2, count1, sizeof (struct elf_symbol),
8376 elf_sym_name_compare);
8377
8378 for (i = 0; i < count1; i++)
8379 /* Two symbols must have the same binding, type and name. */
8380 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8381 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8382 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8383 goto done;
8384
8385 result = TRUE;
8386
dc1e8a47 8387 done:
c9594989
AM
8388 free (symtable1);
8389 free (symtable2);
8390 free (isymbuf1);
8391 free (isymbuf2);
4d269e42
AM
8392
8393 return result;
8394}
8395
8396/* Return TRUE if 2 section types are compatible. */
8397
8398bfd_boolean
8399_bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8400 bfd *bbfd, const asection *bsec)
8401{
8402 if (asec == NULL
8403 || bsec == NULL
8404 || abfd->xvec->flavour != bfd_target_elf_flavour
8405 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8406 return TRUE;
8407
8408 return elf_section_type (asec) == elf_section_type (bsec);
8409}
8410\f
c152c796
AM
8411/* Final phase of ELF linker. */
8412
8413/* A structure we use to avoid passing large numbers of arguments. */
8414
8415struct elf_final_link_info
8416{
8417 /* General link information. */
8418 struct bfd_link_info *info;
8419 /* Output BFD. */
8420 bfd *output_bfd;
8421 /* Symbol string table. */
ef10c3ac 8422 struct elf_strtab_hash *symstrtab;
c152c796
AM
8423 /* .hash section. */
8424 asection *hash_sec;
8425 /* symbol version section (.gnu.version). */
8426 asection *symver_sec;
8427 /* Buffer large enough to hold contents of any section. */
8428 bfd_byte *contents;
8429 /* Buffer large enough to hold external relocs of any section. */
8430 void *external_relocs;
8431 /* Buffer large enough to hold internal relocs of any section. */
8432 Elf_Internal_Rela *internal_relocs;
8433 /* Buffer large enough to hold external local symbols of any input
8434 BFD. */
8435 bfd_byte *external_syms;
8436 /* And a buffer for symbol section indices. */
8437 Elf_External_Sym_Shndx *locsym_shndx;
8438 /* Buffer large enough to hold internal local symbols of any input
8439 BFD. */
8440 Elf_Internal_Sym *internal_syms;
8441 /* Array large enough to hold a symbol index for each local symbol
8442 of any input BFD. */
8443 long *indices;
8444 /* Array large enough to hold a section pointer for each local
8445 symbol of any input BFD. */
8446 asection **sections;
ef10c3ac 8447 /* Buffer for SHT_SYMTAB_SHNDX section. */
c152c796 8448 Elf_External_Sym_Shndx *symshndxbuf;
ffbc01cc
AM
8449 /* Number of STT_FILE syms seen. */
8450 size_t filesym_count;
496afd17
L
8451 /* Local symbol hash table. */
8452 struct bfd_hash_table local_hash_table;
c152c796
AM
8453};
8454
496afd17
L
8455struct local_hash_entry
8456{
8457 /* Base hash table entry structure. */
8458 struct bfd_hash_entry root;
8459 /* Size of the local symbol name. */
8460 size_t size;
8461 /* Number of the duplicated local symbol names. */
8462 long count;
8463};
8464
8465/* Create an entry in the local symbol hash table. */
8466
8467static struct bfd_hash_entry *
8468local_hash_newfunc (struct bfd_hash_entry *entry,
8469 struct bfd_hash_table *table,
8470 const char *string)
8471{
8472
8473 /* Allocate the structure if it has not already been allocated by a
8474 subclass. */
8475 if (entry == NULL)
8476 {
8477 entry = bfd_hash_allocate (table,
8478 sizeof (struct local_hash_entry));
8479 if (entry == NULL)
8480 return entry;
8481 }
8482
8483 /* Call the allocation method of the superclass. */
8484 entry = bfd_hash_newfunc (entry, table, string);
8485 if (entry != NULL)
8486 {
8487 ((struct local_hash_entry *) entry)->count = 0;
8488 ((struct local_hash_entry *) entry)->size = 0;
8489 }
8490
8491 return entry;
8492}
8493
c152c796
AM
8494/* This struct is used to pass information to elf_link_output_extsym. */
8495
8496struct elf_outext_info
8497{
8498 bfd_boolean failed;
8499 bfd_boolean localsyms;
34a79995 8500 bfd_boolean file_sym_done;
8b127cbc 8501 struct elf_final_link_info *flinfo;
c152c796
AM
8502};
8503
d9352518
DB
8504
8505/* Support for evaluating a complex relocation.
8506
8507 Complex relocations are generalized, self-describing relocations. The
8508 implementation of them consists of two parts: complex symbols, and the
a0c8462f 8509 relocations themselves.
d9352518 8510
4b69ce9b 8511 The relocations use a reserved elf-wide relocation type code (R_RELC
d9352518
DB
8512 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8513 information (start bit, end bit, word width, etc) into the addend. This
8514 information is extracted from CGEN-generated operand tables within gas.
8515
4b69ce9b 8516 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
d9352518
DB
8517 internal) representing prefix-notation expressions, including but not
8518 limited to those sorts of expressions normally encoded as addends in the
8519 addend field. The symbol mangling format is:
8520
8521 <node> := <literal>
07d6d2b8
AM
8522 | <unary-operator> ':' <node>
8523 | <binary-operator> ':' <node> ':' <node>
d9352518
DB
8524 ;
8525
8526 <literal> := 's' <digits=N> ':' <N character symbol name>
07d6d2b8 8527 | 'S' <digits=N> ':' <N character section name>
d9352518
DB
8528 | '#' <hexdigits>
8529 ;
8530
8531 <binary-operator> := as in C
8532 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8533
8534static void
a0c8462f
AM
8535set_symbol_value (bfd *bfd_with_globals,
8536 Elf_Internal_Sym *isymbuf,
8537 size_t locsymcount,
8538 size_t symidx,
8539 bfd_vma val)
d9352518 8540{
8977835c
AM
8541 struct elf_link_hash_entry **sym_hashes;
8542 struct elf_link_hash_entry *h;
8543 size_t extsymoff = locsymcount;
d9352518 8544
8977835c 8545 if (symidx < locsymcount)
d9352518 8546 {
8977835c
AM
8547 Elf_Internal_Sym *sym;
8548
8549 sym = isymbuf + symidx;
8550 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8551 {
8552 /* It is a local symbol: move it to the
8553 "absolute" section and give it a value. */
8554 sym->st_shndx = SHN_ABS;
8555 sym->st_value = val;
8556 return;
8557 }
8558 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8559 extsymoff = 0;
d9352518 8560 }
8977835c
AM
8561
8562 /* It is a global symbol: set its link type
8563 to "defined" and give it a value. */
8564
8565 sym_hashes = elf_sym_hashes (bfd_with_globals);
8566 h = sym_hashes [symidx - extsymoff];
8567 while (h->root.type == bfd_link_hash_indirect
8568 || h->root.type == bfd_link_hash_warning)
8569 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8570 h->root.type = bfd_link_hash_defined;
8571 h->root.u.def.value = val;
8572 h->root.u.def.section = bfd_abs_section_ptr;
d9352518
DB
8573}
8574
a0c8462f
AM
8575static bfd_boolean
8576resolve_symbol (const char *name,
8577 bfd *input_bfd,
8b127cbc 8578 struct elf_final_link_info *flinfo,
a0c8462f
AM
8579 bfd_vma *result,
8580 Elf_Internal_Sym *isymbuf,
8581 size_t locsymcount)
d9352518 8582{
a0c8462f
AM
8583 Elf_Internal_Sym *sym;
8584 struct bfd_link_hash_entry *global_entry;
8585 const char *candidate = NULL;
8586 Elf_Internal_Shdr *symtab_hdr;
8587 size_t i;
8588
d9352518
DB
8589 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8590
8591 for (i = 0; i < locsymcount; ++ i)
8592 {
8977835c 8593 sym = isymbuf + i;
d9352518
DB
8594
8595 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8596 continue;
8597
8598 candidate = bfd_elf_string_from_elf_section (input_bfd,
8599 symtab_hdr->sh_link,
8600 sym->st_name);
8601#ifdef DEBUG
0f02bbd9
AM
8602 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8603 name, candidate, (unsigned long) sym->st_value);
d9352518
DB
8604#endif
8605 if (candidate && strcmp (candidate, name) == 0)
8606 {
8b127cbc 8607 asection *sec = flinfo->sections [i];
d9352518 8608
0f02bbd9
AM
8609 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8610 *result += sec->output_offset + sec->output_section->vma;
d9352518 8611#ifdef DEBUG
0f02bbd9
AM
8612 printf ("Found symbol with value %8.8lx\n",
8613 (unsigned long) *result);
d9352518
DB
8614#endif
8615 return TRUE;
8616 }
8617 }
8618
8619 /* Hmm, haven't found it yet. perhaps it is a global. */
8b127cbc 8620 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
a0c8462f 8621 FALSE, FALSE, TRUE);
d9352518
DB
8622 if (!global_entry)
8623 return FALSE;
a0c8462f 8624
d9352518
DB
8625 if (global_entry->type == bfd_link_hash_defined
8626 || global_entry->type == bfd_link_hash_defweak)
8627 {
a0c8462f
AM
8628 *result = (global_entry->u.def.value
8629 + global_entry->u.def.section->output_section->vma
8630 + global_entry->u.def.section->output_offset);
d9352518 8631#ifdef DEBUG
0f02bbd9
AM
8632 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8633 global_entry->root.string, (unsigned long) *result);
d9352518
DB
8634#endif
8635 return TRUE;
a0c8462f 8636 }
d9352518 8637
d9352518
DB
8638 return FALSE;
8639}
8640
37b01f6a
DG
8641/* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8642 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8643 names like "foo.end" which is the end address of section "foo". */
07d6d2b8 8644
d9352518 8645static bfd_boolean
a0c8462f
AM
8646resolve_section (const char *name,
8647 asection *sections,
37b01f6a
DG
8648 bfd_vma *result,
8649 bfd * abfd)
d9352518 8650{
a0c8462f
AM
8651 asection *curr;
8652 unsigned int len;
d9352518 8653
a0c8462f 8654 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8655 if (strcmp (curr->name, name) == 0)
8656 {
8657 *result = curr->vma;
8658 return TRUE;
8659 }
8660
8661 /* Hmm. still haven't found it. try pseudo-section names. */
37b01f6a 8662 /* FIXME: This could be coded more efficiently... */
a0c8462f 8663 for (curr = sections; curr; curr = curr->next)
d9352518
DB
8664 {
8665 len = strlen (curr->name);
a0c8462f 8666 if (len > strlen (name))
d9352518
DB
8667 continue;
8668
8669 if (strncmp (curr->name, name, len) == 0)
8670 {
8671 if (strncmp (".end", name + len, 4) == 0)
8672 {
61826503 8673 *result = (curr->vma
bb294208 8674 + curr->size / bfd_octets_per_byte (abfd, curr));
d9352518
DB
8675 return TRUE;
8676 }
8677
8678 /* Insert more pseudo-section names here, if you like. */
8679 }
8680 }
a0c8462f 8681
d9352518
DB
8682 return FALSE;
8683}
8684
8685static void
a0c8462f 8686undefined_reference (const char *reftype, const char *name)
d9352518 8687{
695344c0 8688 /* xgettext:c-format */
a0c8462f
AM
8689 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8690 reftype, name);
4b69ce9b 8691 bfd_set_error (bfd_error_bad_value);
d9352518
DB
8692}
8693
8694static bfd_boolean
a0c8462f
AM
8695eval_symbol (bfd_vma *result,
8696 const char **symp,
8697 bfd *input_bfd,
8b127cbc 8698 struct elf_final_link_info *flinfo,
a0c8462f
AM
8699 bfd_vma dot,
8700 Elf_Internal_Sym *isymbuf,
8701 size_t locsymcount,
8702 int signed_p)
d9352518 8703{
4b93929b
NC
8704 size_t len;
8705 size_t symlen;
a0c8462f
AM
8706 bfd_vma a;
8707 bfd_vma b;
4b93929b 8708 char symbuf[4096];
0f02bbd9 8709 const char *sym = *symp;
a0c8462f
AM
8710 const char *symend;
8711 bfd_boolean symbol_is_section = FALSE;
d9352518
DB
8712
8713 len = strlen (sym);
8714 symend = sym + len;
8715
4b93929b 8716 if (len < 1 || len > sizeof (symbuf))
d9352518
DB
8717 {
8718 bfd_set_error (bfd_error_invalid_operation);
8719 return FALSE;
8720 }
a0c8462f 8721
d9352518
DB
8722 switch (* sym)
8723 {
8724 case '.':
0f02bbd9
AM
8725 *result = dot;
8726 *symp = sym + 1;
d9352518
DB
8727 return TRUE;
8728
8729 case '#':
0f02bbd9
AM
8730 ++sym;
8731 *result = strtoul (sym, (char **) symp, 16);
d9352518
DB
8732 return TRUE;
8733
8734 case 'S':
8735 symbol_is_section = TRUE;
1a0670f3 8736 /* Fall through. */
a0c8462f 8737 case 's':
0f02bbd9
AM
8738 ++sym;
8739 symlen = strtol (sym, (char **) symp, 10);
8740 sym = *symp + 1; /* Skip the trailing ':'. */
d9352518 8741
4b93929b 8742 if (symend < sym || symlen + 1 > sizeof (symbuf))
d9352518
DB
8743 {
8744 bfd_set_error (bfd_error_invalid_operation);
8745 return FALSE;
8746 }
8747
8748 memcpy (symbuf, sym, symlen);
a0c8462f 8749 symbuf[symlen] = '\0';
0f02bbd9 8750 *symp = sym + symlen;
a0c8462f
AM
8751
8752 /* Is it always possible, with complex symbols, that gas "mis-guessed"
d9352518
DB
8753 the symbol as a section, or vice-versa. so we're pretty liberal in our
8754 interpretation here; section means "try section first", not "must be a
8755 section", and likewise with symbol. */
8756
a0c8462f 8757 if (symbol_is_section)
d9352518 8758 {
37b01f6a 8759 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8b127cbc 8760 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8761 isymbuf, locsymcount))
d9352518
DB
8762 {
8763 undefined_reference ("section", symbuf);
8764 return FALSE;
8765 }
a0c8462f
AM
8766 }
8767 else
d9352518 8768 {
8b127cbc 8769 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8977835c 8770 isymbuf, locsymcount)
8b127cbc 8771 && !resolve_section (symbuf, flinfo->output_bfd->sections,
37b01f6a 8772 result, input_bfd))
d9352518
DB
8773 {
8774 undefined_reference ("symbol", symbuf);
8775 return FALSE;
8776 }
8777 }
8778
8779 return TRUE;
a0c8462f 8780
d9352518
DB
8781 /* All that remains are operators. */
8782
8783#define UNARY_OP(op) \
8784 if (strncmp (sym, #op, strlen (#op)) == 0) \
8785 { \
8786 sym += strlen (#op); \
a0c8462f
AM
8787 if (*sym == ':') \
8788 ++sym; \
0f02bbd9 8789 *symp = sym; \
8b127cbc 8790 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8791 isymbuf, locsymcount, signed_p)) \
a0c8462f
AM
8792 return FALSE; \
8793 if (signed_p) \
0f02bbd9 8794 *result = op ((bfd_signed_vma) a); \
a0c8462f
AM
8795 else \
8796 *result = op a; \
d9352518
DB
8797 return TRUE; \
8798 }
8799
4b69ce9b 8800#define BINARY_OP_HEAD(op) \
d9352518
DB
8801 if (strncmp (sym, #op, strlen (#op)) == 0) \
8802 { \
8803 sym += strlen (#op); \
a0c8462f
AM
8804 if (*sym == ':') \
8805 ++sym; \
0f02bbd9 8806 *symp = sym; \
8b127cbc 8807 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
0f02bbd9 8808 isymbuf, locsymcount, signed_p)) \
a0c8462f 8809 return FALSE; \
0f02bbd9 8810 ++*symp; \
8b127cbc 8811 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
0f02bbd9 8812 isymbuf, locsymcount, signed_p)) \
4b69ce9b
AM
8813 return FALSE;
8814#define BINARY_OP_TAIL(op) \
a0c8462f 8815 if (signed_p) \
0f02bbd9 8816 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
a0c8462f
AM
8817 else \
8818 *result = a op b; \
d9352518
DB
8819 return TRUE; \
8820 }
4b69ce9b 8821#define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
d9352518
DB
8822
8823 default:
8824 UNARY_OP (0-);
4b69ce9b
AM
8825 BINARY_OP_HEAD (<<);
8826 if (b >= sizeof (a) * CHAR_BIT)
8827 {
8828 *result = 0;
8829 return TRUE;
8830 }
8831 signed_p = 0;
8832 BINARY_OP_TAIL (<<);
8833 BINARY_OP_HEAD (>>);
8834 if (b >= sizeof (a) * CHAR_BIT)
8835 {
8836 *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
8837 return TRUE;
8838 }
8839 BINARY_OP_TAIL (>>);
d9352518
DB
8840 BINARY_OP (==);
8841 BINARY_OP (!=);
8842 BINARY_OP (<=);
8843 BINARY_OP (>=);
8844 BINARY_OP (&&);
8845 BINARY_OP (||);
8846 UNARY_OP (~);
8847 UNARY_OP (!);
8848 BINARY_OP (*);
4b69ce9b
AM
8849 BINARY_OP_HEAD (/);
8850 if (b == 0)
8851 {
8852 _bfd_error_handler (_("division by zero"));
8853 bfd_set_error (bfd_error_bad_value);
8854 return FALSE;
8855 }
8856 BINARY_OP_TAIL (/);
8857 BINARY_OP_HEAD (%);
8858 if (b == 0)
8859 {
8860 _bfd_error_handler (_("division by zero"));
8861 bfd_set_error (bfd_error_bad_value);
8862 return FALSE;
8863 }
8864 BINARY_OP_TAIL (%);
d9352518
DB
8865 BINARY_OP (^);
8866 BINARY_OP (|);
8867 BINARY_OP (&);
8868 BINARY_OP (+);
8869 BINARY_OP (-);
8870 BINARY_OP (<);
8871 BINARY_OP (>);
8872#undef UNARY_OP
8873#undef BINARY_OP
8874 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8875 bfd_set_error (bfd_error_invalid_operation);
8876 return FALSE;
8877 }
8878}
8879
d9352518 8880static void
a0c8462f
AM
8881put_value (bfd_vma size,
8882 unsigned long chunksz,
8883 bfd *input_bfd,
8884 bfd_vma x,
8885 bfd_byte *location)
d9352518
DB
8886{
8887 location += (size - chunksz);
8888
41cd1ad1 8889 for (; size; size -= chunksz, location -= chunksz)
d9352518
DB
8890 {
8891 switch (chunksz)
8892 {
d9352518
DB
8893 case 1:
8894 bfd_put_8 (input_bfd, x, location);
41cd1ad1 8895 x >>= 8;
d9352518
DB
8896 break;
8897 case 2:
8898 bfd_put_16 (input_bfd, x, location);
41cd1ad1 8899 x >>= 16;
d9352518
DB
8900 break;
8901 case 4:
8902 bfd_put_32 (input_bfd, x, location);
65164438
NC
8903 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8904 x >>= 16;
8905 x >>= 16;
d9352518 8906 break;
d9352518 8907#ifdef BFD64
41cd1ad1 8908 case 8:
d9352518 8909 bfd_put_64 (input_bfd, x, location);
41cd1ad1
NC
8910 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8911 x >>= 32;
8912 x >>= 32;
8913 break;
d9352518 8914#endif
41cd1ad1
NC
8915 default:
8916 abort ();
d9352518
DB
8917 break;
8918 }
8919 }
8920}
8921
a0c8462f
AM
8922static bfd_vma
8923get_value (bfd_vma size,
8924 unsigned long chunksz,
8925 bfd *input_bfd,
8926 bfd_byte *location)
d9352518 8927{
9b239e0e 8928 int shift;
d9352518
DB
8929 bfd_vma x = 0;
8930
9b239e0e
NC
8931 /* Sanity checks. */
8932 BFD_ASSERT (chunksz <= sizeof (x)
8933 && size >= chunksz
8934 && chunksz != 0
8935 && (size % chunksz) == 0
8936 && input_bfd != NULL
8937 && location != NULL);
8938
8939 if (chunksz == sizeof (x))
8940 {
8941 BFD_ASSERT (size == chunksz);
8942
8943 /* Make sure that we do not perform an undefined shift operation.
8944 We know that size == chunksz so there will only be one iteration
8945 of the loop below. */
8946 shift = 0;
8947 }
8948 else
8949 shift = 8 * chunksz;
8950
a0c8462f 8951 for (; size; size -= chunksz, location += chunksz)
d9352518
DB
8952 {
8953 switch (chunksz)
8954 {
d9352518 8955 case 1:
9b239e0e 8956 x = (x << shift) | bfd_get_8 (input_bfd, location);
d9352518
DB
8957 break;
8958 case 2:
9b239e0e 8959 x = (x << shift) | bfd_get_16 (input_bfd, location);
d9352518
DB
8960 break;
8961 case 4:
9b239e0e 8962 x = (x << shift) | bfd_get_32 (input_bfd, location);
d9352518 8963 break;
d9352518 8964#ifdef BFD64
9b239e0e
NC
8965 case 8:
8966 x = (x << shift) | bfd_get_64 (input_bfd, location);
d9352518 8967 break;
9b239e0e
NC
8968#endif
8969 default:
8970 abort ();
d9352518
DB
8971 }
8972 }
8973 return x;
8974}
8975
a0c8462f
AM
8976static void
8977decode_complex_addend (unsigned long *start, /* in bits */
8978 unsigned long *oplen, /* in bits */
8979 unsigned long *len, /* in bits */
8980 unsigned long *wordsz, /* in bytes */
8981 unsigned long *chunksz, /* in bytes */
8982 unsigned long *lsb0_p,
8983 unsigned long *signed_p,
8984 unsigned long *trunc_p,
8985 unsigned long encoded)
d9352518 8986{
07d6d2b8
AM
8987 * start = encoded & 0x3F;
8988 * len = (encoded >> 6) & 0x3F;
d9352518
DB
8989 * oplen = (encoded >> 12) & 0x3F;
8990 * wordsz = (encoded >> 18) & 0xF;
8991 * chunksz = (encoded >> 22) & 0xF;
8992 * lsb0_p = (encoded >> 27) & 1;
8993 * signed_p = (encoded >> 28) & 1;
8994 * trunc_p = (encoded >> 29) & 1;
8995}
8996
cdfeee4f 8997bfd_reloc_status_type
0f02bbd9 8998bfd_elf_perform_complex_relocation (bfd *input_bfd,
bb294208 8999 asection *input_section,
0f02bbd9
AM
9000 bfd_byte *contents,
9001 Elf_Internal_Rela *rel,
9002 bfd_vma relocation)
d9352518 9003{
0f02bbd9
AM
9004 bfd_vma shift, x, mask;
9005 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
cdfeee4f 9006 bfd_reloc_status_type r;
bb294208 9007 bfd_size_type octets;
d9352518
DB
9008
9009 /* Perform this reloc, since it is complex.
9010 (this is not to say that it necessarily refers to a complex
9011 symbol; merely that it is a self-describing CGEN based reloc.
9012 i.e. the addend has the complete reloc information (bit start, end,
a0c8462f 9013 word size, etc) encoded within it.). */
d9352518 9014
a0c8462f
AM
9015 decode_complex_addend (&start, &oplen, &len, &wordsz,
9016 &chunksz, &lsb0_p, &signed_p,
9017 &trunc_p, rel->r_addend);
d9352518
DB
9018
9019 mask = (((1L << (len - 1)) - 1) << 1) | 1;
9020
9021 if (lsb0_p)
9022 shift = (start + 1) - len;
9023 else
9024 shift = (8 * wordsz) - (start + len);
9025
bb294208
AM
9026 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
9027 x = get_value (wordsz, chunksz, input_bfd, contents + octets);
d9352518
DB
9028
9029#ifdef DEBUG
9030 printf ("Doing complex reloc: "
9031 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
9032 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
9033 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
9034 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
9ccb8af9
AM
9035 oplen, (unsigned long) x, (unsigned long) mask,
9036 (unsigned long) relocation);
d9352518
DB
9037#endif
9038
cdfeee4f 9039 r = bfd_reloc_ok;
d9352518 9040 if (! trunc_p)
cdfeee4f
AM
9041 /* Now do an overflow check. */
9042 r = bfd_check_overflow ((signed_p
9043 ? complain_overflow_signed
9044 : complain_overflow_unsigned),
9045 len, 0, (8 * wordsz),
9046 relocation);
a0c8462f 9047
d9352518
DB
9048 /* Do the deed. */
9049 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
9050
9051#ifdef DEBUG
9052 printf (" relocation: %8.8lx\n"
9053 " shifted mask: %8.8lx\n"
9054 " shifted/masked reloc: %8.8lx\n"
9055 " result: %8.8lx\n",
9ccb8af9
AM
9056 (unsigned long) relocation, (unsigned long) (mask << shift),
9057 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
d9352518 9058#endif
bb294208 9059 put_value (wordsz, chunksz, input_bfd, x, contents + octets);
cdfeee4f 9060 return r;
d9352518
DB
9061}
9062
0e287786
AM
9063/* Functions to read r_offset from external (target order) reloc
9064 entry. Faster than bfd_getl32 et al, because we let the compiler
9065 know the value is aligned. */
53df40a4 9066
0e287786
AM
9067static bfd_vma
9068ext32l_r_offset (const void *p)
53df40a4
AM
9069{
9070 union aligned32
9071 {
9072 uint32_t v;
9073 unsigned char c[4];
9074 };
9075 const union aligned32 *a
0e287786 9076 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
9077
9078 uint32_t aval = ( (uint32_t) a->c[0]
9079 | (uint32_t) a->c[1] << 8
9080 | (uint32_t) a->c[2] << 16
9081 | (uint32_t) a->c[3] << 24);
0e287786 9082 return aval;
53df40a4
AM
9083}
9084
0e287786
AM
9085static bfd_vma
9086ext32b_r_offset (const void *p)
53df40a4
AM
9087{
9088 union aligned32
9089 {
9090 uint32_t v;
9091 unsigned char c[4];
9092 };
9093 const union aligned32 *a
0e287786 9094 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
53df40a4
AM
9095
9096 uint32_t aval = ( (uint32_t) a->c[0] << 24
9097 | (uint32_t) a->c[1] << 16
9098 | (uint32_t) a->c[2] << 8
9099 | (uint32_t) a->c[3]);
0e287786 9100 return aval;
53df40a4
AM
9101}
9102
9103#ifdef BFD_HOST_64_BIT
0e287786
AM
9104static bfd_vma
9105ext64l_r_offset (const void *p)
53df40a4
AM
9106{
9107 union aligned64
9108 {
9109 uint64_t v;
9110 unsigned char c[8];
9111 };
9112 const union aligned64 *a
0e287786 9113 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
9114
9115 uint64_t aval = ( (uint64_t) a->c[0]
9116 | (uint64_t) a->c[1] << 8
9117 | (uint64_t) a->c[2] << 16
9118 | (uint64_t) a->c[3] << 24
9119 | (uint64_t) a->c[4] << 32
9120 | (uint64_t) a->c[5] << 40
9121 | (uint64_t) a->c[6] << 48
9122 | (uint64_t) a->c[7] << 56);
0e287786 9123 return aval;
53df40a4
AM
9124}
9125
0e287786
AM
9126static bfd_vma
9127ext64b_r_offset (const void *p)
53df40a4
AM
9128{
9129 union aligned64
9130 {
9131 uint64_t v;
9132 unsigned char c[8];
9133 };
9134 const union aligned64 *a
0e287786 9135 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
53df40a4
AM
9136
9137 uint64_t aval = ( (uint64_t) a->c[0] << 56
9138 | (uint64_t) a->c[1] << 48
9139 | (uint64_t) a->c[2] << 40
9140 | (uint64_t) a->c[3] << 32
9141 | (uint64_t) a->c[4] << 24
9142 | (uint64_t) a->c[5] << 16
9143 | (uint64_t) a->c[6] << 8
9144 | (uint64_t) a->c[7]);
0e287786 9145 return aval;
53df40a4
AM
9146}
9147#endif
9148
c152c796
AM
9149/* When performing a relocatable link, the input relocations are
9150 preserved. But, if they reference global symbols, the indices
d4730f92
BS
9151 referenced must be updated. Update all the relocations found in
9152 RELDATA. */
c152c796 9153
bca6d0e3 9154static bfd_boolean
c152c796 9155elf_link_adjust_relocs (bfd *abfd,
9eaff861 9156 asection *sec,
28dbcedc 9157 struct bfd_elf_section_reloc_data *reldata,
10bbbc1d
NC
9158 bfd_boolean sort,
9159 struct bfd_link_info *info)
c152c796
AM
9160{
9161 unsigned int i;
9162 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9163 bfd_byte *erela;
9164 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9165 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9166 bfd_vma r_type_mask;
9167 int r_sym_shift;
d4730f92
BS
9168 unsigned int count = reldata->count;
9169 struct elf_link_hash_entry **rel_hash = reldata->hashes;
c152c796 9170
d4730f92 9171 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
c152c796
AM
9172 {
9173 swap_in = bed->s->swap_reloc_in;
9174 swap_out = bed->s->swap_reloc_out;
9175 }
d4730f92 9176 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
c152c796
AM
9177 {
9178 swap_in = bed->s->swap_reloca_in;
9179 swap_out = bed->s->swap_reloca_out;
9180 }
9181 else
9182 abort ();
9183
9184 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
9185 abort ();
9186
9187 if (bed->s->arch_size == 32)
9188 {
9189 r_type_mask = 0xff;
9190 r_sym_shift = 8;
9191 }
9192 else
9193 {
9194 r_type_mask = 0xffffffff;
9195 r_sym_shift = 32;
9196 }
9197
d4730f92
BS
9198 erela = reldata->hdr->contents;
9199 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
c152c796
AM
9200 {
9201 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
9202 unsigned int j;
9203
9204 if (*rel_hash == NULL)
9205 continue;
9206
10bbbc1d
NC
9207 if ((*rel_hash)->indx == -2
9208 && info->gc_sections
9209 && ! info->gc_keep_exported)
9210 {
9211 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
9793eb77 9212 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
10bbbc1d
NC
9213 abfd, sec,
9214 (*rel_hash)->root.root.string);
9793eb77 9215 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
d42c267e 9216 abfd, sec);
10bbbc1d
NC
9217 bfd_set_error (bfd_error_invalid_operation);
9218 return FALSE;
9219 }
c152c796
AM
9220 BFD_ASSERT ((*rel_hash)->indx >= 0);
9221
9222 (*swap_in) (abfd, erela, irela);
9223 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
9224 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
9225 | (irela[j].r_info & r_type_mask));
9226 (*swap_out) (abfd, irela, erela);
9227 }
53df40a4 9228
9eaff861
AO
9229 if (bed->elf_backend_update_relocs)
9230 (*bed->elf_backend_update_relocs) (sec, reldata);
9231
0e287786 9232 if (sort && count != 0)
53df40a4 9233 {
0e287786
AM
9234 bfd_vma (*ext_r_off) (const void *);
9235 bfd_vma r_off;
9236 size_t elt_size;
9237 bfd_byte *base, *end, *p, *loc;
bca6d0e3 9238 bfd_byte *buf = NULL;
28dbcedc
AM
9239
9240 if (bed->s->arch_size == 32)
9241 {
9242 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 9243 ext_r_off = ext32l_r_offset;
28dbcedc 9244 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 9245 ext_r_off = ext32b_r_offset;
28dbcedc
AM
9246 else
9247 abort ();
9248 }
53df40a4 9249 else
28dbcedc 9250 {
53df40a4 9251#ifdef BFD_HOST_64_BIT
28dbcedc 9252 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
0e287786 9253 ext_r_off = ext64l_r_offset;
28dbcedc 9254 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
0e287786 9255 ext_r_off = ext64b_r_offset;
28dbcedc 9256 else
53df40a4 9257#endif
28dbcedc
AM
9258 abort ();
9259 }
0e287786 9260
bca6d0e3
AM
9261 /* Must use a stable sort here. A modified insertion sort,
9262 since the relocs are mostly sorted already. */
0e287786
AM
9263 elt_size = reldata->hdr->sh_entsize;
9264 base = reldata->hdr->contents;
9265 end = base + count * elt_size;
bca6d0e3 9266 if (elt_size > sizeof (Elf64_External_Rela))
0e287786
AM
9267 abort ();
9268
9269 /* Ensure the first element is lowest. This acts as a sentinel,
9270 speeding the main loop below. */
9271 r_off = (*ext_r_off) (base);
9272 for (p = loc = base; (p += elt_size) < end; )
9273 {
9274 bfd_vma r_off2 = (*ext_r_off) (p);
9275 if (r_off > r_off2)
9276 {
9277 r_off = r_off2;
9278 loc = p;
9279 }
9280 }
9281 if (loc != base)
9282 {
9283 /* Don't just swap *base and *loc as that changes the order
9284 of the original base[0] and base[1] if they happen to
9285 have the same r_offset. */
bca6d0e3
AM
9286 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9287 memcpy (onebuf, loc, elt_size);
0e287786 9288 memmove (base + elt_size, base, loc - base);
bca6d0e3 9289 memcpy (base, onebuf, elt_size);
0e287786
AM
9290 }
9291
b29b8669 9292 for (p = base + elt_size; (p += elt_size) < end; )
0e287786
AM
9293 {
9294 /* base to p is sorted, *p is next to insert. */
9295 r_off = (*ext_r_off) (p);
9296 /* Search the sorted region for location to insert. */
9297 loc = p - elt_size;
9298 while (r_off < (*ext_r_off) (loc))
9299 loc -= elt_size;
9300 loc += elt_size;
9301 if (loc != p)
9302 {
bca6d0e3
AM
9303 /* Chances are there is a run of relocs to insert here,
9304 from one of more input files. Files are not always
9305 linked in order due to the way elf_link_input_bfd is
9306 called. See pr17666. */
9307 size_t sortlen = p - loc;
9308 bfd_vma r_off2 = (*ext_r_off) (loc);
9309 size_t runlen = elt_size;
9310 size_t buf_size = 96 * 1024;
9311 while (p + runlen < end
9312 && (sortlen <= buf_size
9313 || runlen + elt_size <= buf_size)
9314 && r_off2 > (*ext_r_off) (p + runlen))
9315 runlen += elt_size;
9316 if (buf == NULL)
9317 {
9318 buf = bfd_malloc (buf_size);
9319 if (buf == NULL)
9320 return FALSE;
9321 }
9322 if (runlen < sortlen)
9323 {
9324 memcpy (buf, p, runlen);
9325 memmove (loc + runlen, loc, sortlen);
9326 memcpy (loc, buf, runlen);
9327 }
9328 else
9329 {
9330 memcpy (buf, loc, sortlen);
9331 memmove (loc, p, runlen);
9332 memcpy (loc + runlen, buf, sortlen);
9333 }
b29b8669 9334 p += runlen - elt_size;
0e287786
AM
9335 }
9336 }
9337 /* Hashes are no longer valid. */
28dbcedc
AM
9338 free (reldata->hashes);
9339 reldata->hashes = NULL;
bca6d0e3 9340 free (buf);
53df40a4 9341 }
bca6d0e3 9342 return TRUE;
c152c796
AM
9343}
9344
9345struct elf_link_sort_rela
9346{
9347 union {
9348 bfd_vma offset;
9349 bfd_vma sym_mask;
9350 } u;
9351 enum elf_reloc_type_class type;
9352 /* We use this as an array of size int_rels_per_ext_rel. */
9353 Elf_Internal_Rela rela[1];
9354};
9355
dcea6a95
AM
9356/* qsort stability here and for cmp2 is only an issue if multiple
9357 dynamic relocations are emitted at the same address. But targets
9358 that apply a series of dynamic relocations each operating on the
9359 result of the prior relocation can't use -z combreloc as
9360 implemented anyway. Such schemes tend to be broken by sorting on
9361 symbol index. That leaves dynamic NONE relocs as the only other
9362 case where ld might emit multiple relocs at the same address, and
9363 those are only emitted due to target bugs. */
9364
c152c796
AM
9365static int
9366elf_link_sort_cmp1 (const void *A, const void *B)
9367{
a50b1753
NC
9368 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9369 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796
AM
9370 int relativea, relativeb;
9371
9372 relativea = a->type == reloc_class_relative;
9373 relativeb = b->type == reloc_class_relative;
9374
9375 if (relativea < relativeb)
9376 return 1;
9377 if (relativea > relativeb)
9378 return -1;
9379 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9380 return -1;
9381 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9382 return 1;
9383 if (a->rela->r_offset < b->rela->r_offset)
9384 return -1;
9385 if (a->rela->r_offset > b->rela->r_offset)
9386 return 1;
9387 return 0;
9388}
9389
9390static int
9391elf_link_sort_cmp2 (const void *A, const void *B)
9392{
a50b1753
NC
9393 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9394 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
c152c796 9395
7e612e98 9396 if (a->type < b->type)
c152c796 9397 return -1;
7e612e98 9398 if (a->type > b->type)
c152c796 9399 return 1;
7e612e98 9400 if (a->u.offset < b->u.offset)
c152c796 9401 return -1;
7e612e98 9402 if (a->u.offset > b->u.offset)
c152c796
AM
9403 return 1;
9404 if (a->rela->r_offset < b->rela->r_offset)
9405 return -1;
9406 if (a->rela->r_offset > b->rela->r_offset)
9407 return 1;
9408 return 0;
9409}
9410
9411static size_t
9412elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9413{
3410fea8 9414 asection *dynamic_relocs;
fc66a176
L
9415 asection *rela_dyn;
9416 asection *rel_dyn;
c152c796
AM
9417 bfd_size_type count, size;
9418 size_t i, ret, sort_elt, ext_size;
9419 bfd_byte *sort, *s_non_relative, *p;
9420 struct elf_link_sort_rela *sq;
9421 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9422 int i2e = bed->s->int_rels_per_ext_rel;
61826503 9423 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
c152c796
AM
9424 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9425 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9426 struct bfd_link_order *lo;
9427 bfd_vma r_sym_mask;
3410fea8 9428 bfd_boolean use_rela;
c152c796 9429
3410fea8
NC
9430 /* Find a dynamic reloc section. */
9431 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9432 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9433 if (rela_dyn != NULL && rela_dyn->size > 0
9434 && rel_dyn != NULL && rel_dyn->size > 0)
c152c796 9435 {
3410fea8
NC
9436 bfd_boolean use_rela_initialised = FALSE;
9437
9438 /* This is just here to stop gcc from complaining.
c8e44c6d 9439 Its initialization checking code is not perfect. */
3410fea8
NC
9440 use_rela = TRUE;
9441
9442 /* Both sections are present. Examine the sizes
9443 of the indirect sections to help us choose. */
9444 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9445 if (lo->type == bfd_indirect_link_order)
9446 {
9447 asection *o = lo->u.indirect.section;
9448
9449 if ((o->size % bed->s->sizeof_rela) == 0)
9450 {
9451 if ((o->size % bed->s->sizeof_rel) == 0)
9452 /* Section size is divisible by both rel and rela sizes.
9453 It is of no help to us. */
9454 ;
9455 else
9456 {
9457 /* Section size is only divisible by rela. */
535b785f 9458 if (use_rela_initialised && !use_rela)
3410fea8 9459 {
9793eb77 9460 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9461 "they are in more than one size"),
9462 abfd);
3410fea8
NC
9463 bfd_set_error (bfd_error_invalid_operation);
9464 return 0;
9465 }
9466 else
9467 {
9468 use_rela = TRUE;
9469 use_rela_initialised = TRUE;
9470 }
9471 }
9472 }
9473 else if ((o->size % bed->s->sizeof_rel) == 0)
9474 {
9475 /* Section size is only divisible by rel. */
535b785f 9476 if (use_rela_initialised && use_rela)
3410fea8 9477 {
9793eb77 9478 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9479 "they are in more than one size"),
9480 abfd);
3410fea8
NC
9481 bfd_set_error (bfd_error_invalid_operation);
9482 return 0;
9483 }
9484 else
9485 {
9486 use_rela = FALSE;
9487 use_rela_initialised = TRUE;
9488 }
9489 }
9490 else
9491 {
c8e44c6d
AM
9492 /* The section size is not divisible by either -
9493 something is wrong. */
9793eb77 9494 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9495 "they are of an unknown size"), abfd);
3410fea8
NC
9496 bfd_set_error (bfd_error_invalid_operation);
9497 return 0;
9498 }
9499 }
9500
9501 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9502 if (lo->type == bfd_indirect_link_order)
9503 {
9504 asection *o = lo->u.indirect.section;
9505
9506 if ((o->size % bed->s->sizeof_rela) == 0)
9507 {
9508 if ((o->size % bed->s->sizeof_rel) == 0)
9509 /* Section size is divisible by both rel and rela sizes.
9510 It is of no help to us. */
9511 ;
9512 else
9513 {
9514 /* Section size is only divisible by rela. */
535b785f 9515 if (use_rela_initialised && !use_rela)
3410fea8 9516 {
9793eb77 9517 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9518 "they are in more than one size"),
9519 abfd);
3410fea8
NC
9520 bfd_set_error (bfd_error_invalid_operation);
9521 return 0;
9522 }
9523 else
9524 {
9525 use_rela = TRUE;
9526 use_rela_initialised = TRUE;
9527 }
9528 }
9529 }
9530 else if ((o->size % bed->s->sizeof_rel) == 0)
9531 {
9532 /* Section size is only divisible by rel. */
535b785f 9533 if (use_rela_initialised && use_rela)
3410fea8 9534 {
9793eb77 9535 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d
AM
9536 "they are in more than one size"),
9537 abfd);
3410fea8
NC
9538 bfd_set_error (bfd_error_invalid_operation);
9539 return 0;
9540 }
9541 else
9542 {
9543 use_rela = FALSE;
9544 use_rela_initialised = TRUE;
9545 }
9546 }
9547 else
9548 {
c8e44c6d
AM
9549 /* The section size is not divisible by either -
9550 something is wrong. */
9793eb77 9551 _bfd_error_handler (_("%pB: unable to sort relocs - "
c8e44c6d 9552 "they are of an unknown size"), abfd);
3410fea8
NC
9553 bfd_set_error (bfd_error_invalid_operation);
9554 return 0;
9555 }
9556 }
9557
9558 if (! use_rela_initialised)
9559 /* Make a guess. */
9560 use_rela = TRUE;
c152c796 9561 }
fc66a176
L
9562 else if (rela_dyn != NULL && rela_dyn->size > 0)
9563 use_rela = TRUE;
9564 else if (rel_dyn != NULL && rel_dyn->size > 0)
3410fea8 9565 use_rela = FALSE;
c152c796 9566 else
fc66a176 9567 return 0;
3410fea8
NC
9568
9569 if (use_rela)
c152c796 9570 {
3410fea8 9571 dynamic_relocs = rela_dyn;
c152c796
AM
9572 ext_size = bed->s->sizeof_rela;
9573 swap_in = bed->s->swap_reloca_in;
9574 swap_out = bed->s->swap_reloca_out;
9575 }
3410fea8
NC
9576 else
9577 {
9578 dynamic_relocs = rel_dyn;
9579 ext_size = bed->s->sizeof_rel;
9580 swap_in = bed->s->swap_reloc_in;
9581 swap_out = bed->s->swap_reloc_out;
9582 }
c152c796
AM
9583
9584 size = 0;
3410fea8 9585 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796 9586 if (lo->type == bfd_indirect_link_order)
3410fea8 9587 size += lo->u.indirect.section->size;
c152c796 9588
3410fea8 9589 if (size != dynamic_relocs->size)
c152c796
AM
9590 return 0;
9591
9592 sort_elt = (sizeof (struct elf_link_sort_rela)
9593 + (i2e - 1) * sizeof (Elf_Internal_Rela));
3410fea8
NC
9594
9595 count = dynamic_relocs->size / ext_size;
5e486aa1
NC
9596 if (count == 0)
9597 return 0;
a50b1753 9598 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
3410fea8 9599
c152c796
AM
9600 if (sort == NULL)
9601 {
9602 (*info->callbacks->warning)
9793eb77 9603 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
c152c796
AM
9604 return 0;
9605 }
9606
9607 if (bed->s->arch_size == 32)
9608 r_sym_mask = ~(bfd_vma) 0xff;
9609 else
9610 r_sym_mask = ~(bfd_vma) 0xffffffff;
9611
3410fea8 9612 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9613 if (lo->type == bfd_indirect_link_order)
9614 {
9615 bfd_byte *erel, *erelend;
9616 asection *o = lo->u.indirect.section;
9617
1da212d6
AM
9618 if (o->contents == NULL && o->size != 0)
9619 {
9620 /* This is a reloc section that is being handled as a normal
9621 section. See bfd_section_from_shdr. We can't combine
9622 relocs in this case. */
9623 free (sort);
9624 return 0;
9625 }
c152c796 9626 erel = o->contents;
eea6121a 9627 erelend = o->contents + o->size;
c8e44c6d 9628 p = sort + o->output_offset * opb / ext_size * sort_elt;
3410fea8 9629
c152c796
AM
9630 while (erel < erelend)
9631 {
9632 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
3410fea8 9633
c152c796 9634 (*swap_in) (abfd, erel, s->rela);
7e612e98 9635 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
c152c796
AM
9636 s->u.sym_mask = r_sym_mask;
9637 p += sort_elt;
9638 erel += ext_size;
9639 }
9640 }
9641
9642 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9643
9644 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9645 {
9646 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9647 if (s->type != reloc_class_relative)
9648 break;
9649 }
9650 ret = i;
9651 s_non_relative = p;
9652
9653 sq = (struct elf_link_sort_rela *) s_non_relative;
9654 for (; i < count; i++, p += sort_elt)
9655 {
9656 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9657 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9658 sq = sp;
9659 sp->u.offset = sq->rela->r_offset;
9660 }
9661
9662 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9663
c8e44c6d
AM
9664 struct elf_link_hash_table *htab = elf_hash_table (info);
9665 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9666 {
9667 /* We have plt relocs in .rela.dyn. */
9668 sq = (struct elf_link_sort_rela *) sort;
9669 for (i = 0; i < count; i++)
9670 if (sq[count - i - 1].type != reloc_class_plt)
9671 break;
9672 if (i != 0 && htab->srelplt->size == i * ext_size)
9673 {
9674 struct bfd_link_order **plo;
9675 /* Put srelplt link_order last. This is so the output_offset
9676 set in the next loop is correct for DT_JMPREL. */
9677 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9678 if ((*plo)->type == bfd_indirect_link_order
9679 && (*plo)->u.indirect.section == htab->srelplt)
9680 {
9681 lo = *plo;
9682 *plo = lo->next;
9683 }
9684 else
9685 plo = &(*plo)->next;
9686 *plo = lo;
9687 lo->next = NULL;
9688 dynamic_relocs->map_tail.link_order = lo;
9689 }
9690 }
9691
9692 p = sort;
3410fea8 9693 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
c152c796
AM
9694 if (lo->type == bfd_indirect_link_order)
9695 {
9696 bfd_byte *erel, *erelend;
9697 asection *o = lo->u.indirect.section;
9698
9699 erel = o->contents;
eea6121a 9700 erelend = o->contents + o->size;
c8e44c6d 9701 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
c152c796
AM
9702 while (erel < erelend)
9703 {
9704 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9705 (*swap_out) (abfd, s->rela, erel);
9706 p += sort_elt;
9707 erel += ext_size;
9708 }
9709 }
9710
9711 free (sort);
3410fea8 9712 *psec = dynamic_relocs;
c152c796
AM
9713 return ret;
9714}
9715
ef10c3ac 9716/* Add a symbol to the output symbol string table. */
c152c796 9717
6e0b88f1 9718static int
ef10c3ac
L
9719elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9720 const char *name,
9721 Elf_Internal_Sym *elfsym,
9722 asection *input_sec,
9723 struct elf_link_hash_entry *h)
c152c796 9724{
6e0b88f1 9725 int (*output_symbol_hook)
c152c796
AM
9726 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9727 struct elf_link_hash_entry *);
ef10c3ac 9728 struct elf_link_hash_table *hash_table;
c152c796 9729 const struct elf_backend_data *bed;
ef10c3ac 9730 bfd_size_type strtabsize;
c152c796 9731
8539e4e8
AM
9732 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9733
8b127cbc 9734 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796
AM
9735 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9736 if (output_symbol_hook != NULL)
9737 {
8b127cbc 9738 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
6e0b88f1
AM
9739 if (ret != 1)
9740 return ret;
c152c796
AM
9741 }
9742
06f44071
AM
9743 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
9744 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
9745 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
9746 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
9747
ef10c3ac
L
9748 if (name == NULL
9749 || *name == '\0'
9750 || (input_sec->flags & SEC_EXCLUDE))
9751 elfsym->st_name = (unsigned long) -1;
c152c796
AM
9752 else
9753 {
ef10c3ac
L
9754 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9755 to get the final offset for st_name. */
3f2e9699 9756 char *versioned_name = (char *) name;
496afd17
L
9757 if (h != NULL)
9758 {
9759 if (h->versioned == versioned && h->def_dynamic)
9760 {
9761 /* Keep only one '@' for versioned symbols defined in
9762 shared objects. */
9763 char *version = strrchr (name, ELF_VER_CHR);
9764 char *base_end = strchr (name, ELF_VER_CHR);
9765 if (version != base_end)
9766 {
9767 size_t base_len;
9768 size_t len = strlen (name);
9769 versioned_name = bfd_alloc (flinfo->output_bfd, len);
9770 if (versioned_name == NULL)
9771 return 0;
9772 base_len = base_end - name;
9773 memcpy (versioned_name, name, base_len);
9774 memcpy (versioned_name + base_len, version,
9775 len - base_len);
9776 }
9777 }
9778 }
9779 else if (flinfo->info->unique_symbol
9780 && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
3f2e9699 9781 {
496afd17
L
9782 struct local_hash_entry *lh;
9783 switch (ELF_ST_TYPE (elfsym->st_info))
3f2e9699 9784 {
496afd17
L
9785 case STT_FILE:
9786 case STT_SECTION:
9787 break;
9788 default:
9789 lh = (struct local_hash_entry *) bfd_hash_lookup
9790 (&flinfo->local_hash_table, name, TRUE, FALSE);
9791 if (lh == NULL)
3f2e9699 9792 return 0;
496afd17
L
9793 if (lh->count)
9794 {
9795 /* Append ".COUNT" to duplicated local symbols. */
9796 size_t count_len;
9797 size_t base_len = lh->size;
9798 char buf[30];
9799 sprintf (buf, "%lx", lh->count);
9800 if (!base_len)
9801 {
9802 base_len = strlen (name);
9803 lh->size = base_len;
9804 }
9805 count_len = strlen (buf);
9806 versioned_name = bfd_alloc (flinfo->output_bfd,
9807 base_len + count_len + 2);
9808 if (versioned_name == NULL)
9809 return 0;
9810 memcpy (versioned_name, name, base_len);
9811 versioned_name[base_len] = '.';
9812 memcpy (versioned_name + base_len + 1, buf,
9813 count_len + 1);
9814 }
9815 lh->count++;
9816 break;
3f2e9699
L
9817 }
9818 }
ef10c3ac
L
9819 elfsym->st_name
9820 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
3f2e9699 9821 versioned_name, FALSE);
c152c796 9822 if (elfsym->st_name == (unsigned long) -1)
6e0b88f1 9823 return 0;
c152c796
AM
9824 }
9825
ef10c3ac
L
9826 hash_table = elf_hash_table (flinfo->info);
9827 strtabsize = hash_table->strtabsize;
9828 if (strtabsize <= hash_table->strtabcount)
c152c796 9829 {
ef10c3ac
L
9830 strtabsize += strtabsize;
9831 hash_table->strtabsize = strtabsize;
9832 strtabsize *= sizeof (*hash_table->strtab);
9833 hash_table->strtab
9834 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9835 strtabsize);
9836 if (hash_table->strtab == NULL)
6e0b88f1 9837 return 0;
c152c796 9838 }
ef10c3ac
L
9839 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9840 hash_table->strtab[hash_table->strtabcount].dest_index
9841 = hash_table->strtabcount;
9842 hash_table->strtab[hash_table->strtabcount].destshndx_index
9843 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9844
ed48ec2e 9845 flinfo->output_bfd->symcount += 1;
ef10c3ac
L
9846 hash_table->strtabcount += 1;
9847
9848 return 1;
9849}
9850
9851/* Swap symbols out to the symbol table and flush the output symbols to
9852 the file. */
9853
9854static bfd_boolean
9855elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9856{
9857 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
986f0783 9858 size_t amt;
ef53be89 9859 size_t i;
ef10c3ac
L
9860 const struct elf_backend_data *bed;
9861 bfd_byte *symbuf;
9862 Elf_Internal_Shdr *hdr;
9863 file_ptr pos;
9864 bfd_boolean ret;
9865
9866 if (!hash_table->strtabcount)
9867 return TRUE;
9868
9869 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9870
9871 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 9872
ef10c3ac
L
9873 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9874 symbuf = (bfd_byte *) bfd_malloc (amt);
9875 if (symbuf == NULL)
9876 return FALSE;
1b786873 9877
ef10c3ac 9878 if (flinfo->symshndxbuf)
c152c796 9879 {
ef53be89
AM
9880 amt = sizeof (Elf_External_Sym_Shndx);
9881 amt *= bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
9882 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9883 if (flinfo->symshndxbuf == NULL)
c152c796 9884 {
ef10c3ac
L
9885 free (symbuf);
9886 return FALSE;
c152c796 9887 }
c152c796
AM
9888 }
9889
3d16b64e 9890 /* Now swap out the symbols. */
ef10c3ac
L
9891 for (i = 0; i < hash_table->strtabcount; i++)
9892 {
9893 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9894 if (elfsym->sym.st_name == (unsigned long) -1)
9895 elfsym->sym.st_name = 0;
9896 else
9897 elfsym->sym.st_name
9898 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9899 elfsym->sym.st_name);
3d16b64e
NA
9900
9901 /* Inform the linker of the addition of this symbol. */
9902
9903 if (flinfo->info->callbacks->ctf_new_symbol)
9904 flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
9905 &elfsym->sym);
9906
ef10c3ac
L
9907 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9908 ((bfd_byte *) symbuf
9909 + (elfsym->dest_index
9910 * bed->s->sizeof_sym)),
9911 (flinfo->symshndxbuf
9912 + elfsym->destshndx_index));
9913 }
9914
9915 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9916 pos = hdr->sh_offset + hdr->sh_size;
9917 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9918 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9919 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9920 {
9921 hdr->sh_size += amt;
9922 ret = TRUE;
9923 }
9924 else
9925 ret = FALSE;
c152c796 9926
ef10c3ac
L
9927 free (symbuf);
9928
9929 free (hash_table->strtab);
9930 hash_table->strtab = NULL;
9931
9932 return ret;
c152c796
AM
9933}
9934
c0d5a53d
L
9935/* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9936
9937static bfd_boolean
9938check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9939{
4fbb74a6
AM
9940 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9941 && sym->st_shndx < SHN_LORESERVE)
c0d5a53d
L
9942 {
9943 /* The gABI doesn't support dynamic symbols in output sections
a0c8462f 9944 beyond 64k. */
4eca0228 9945 _bfd_error_handler
695344c0 9946 /* xgettext:c-format */
9793eb77 9947 (_("%pB: too many sections: %d (>= %d)"),
4fbb74a6 9948 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
c0d5a53d
L
9949 bfd_set_error (bfd_error_nonrepresentable_section);
9950 return FALSE;
9951 }
9952 return TRUE;
9953}
9954
c152c796
AM
9955/* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9956 allowing an unsatisfied unversioned symbol in the DSO to match a
9957 versioned symbol that would normally require an explicit version.
9958 We also handle the case that a DSO references a hidden symbol
9959 which may be satisfied by a versioned symbol in another DSO. */
9960
9961static bfd_boolean
9962elf_link_check_versioned_symbol (struct bfd_link_info *info,
9963 const struct elf_backend_data *bed,
9964 struct elf_link_hash_entry *h)
9965{
9966 bfd *abfd;
9967 struct elf_link_loaded_list *loaded;
9968
9969 if (!is_elf_hash_table (info->hash))
9970 return FALSE;
9971
90c984fc
L
9972 /* Check indirect symbol. */
9973 while (h->root.type == bfd_link_hash_indirect)
9974 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9975
c152c796
AM
9976 switch (h->root.type)
9977 {
9978 default:
9979 abfd = NULL;
9980 break;
9981
9982 case bfd_link_hash_undefined:
9983 case bfd_link_hash_undefweak:
9984 abfd = h->root.u.undef.abfd;
f4ab0e2d
L
9985 if (abfd == NULL
9986 || (abfd->flags & DYNAMIC) == 0
e56f61be 9987 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
c152c796
AM
9988 return FALSE;
9989 break;
9990
9991 case bfd_link_hash_defined:
9992 case bfd_link_hash_defweak:
9993 abfd = h->root.u.def.section->owner;
9994 break;
9995
9996 case bfd_link_hash_common:
9997 abfd = h->root.u.c.p->section->owner;
9998 break;
9999 }
10000 BFD_ASSERT (abfd != NULL);
10001
e310298c 10002 for (loaded = elf_hash_table (info)->dyn_loaded;
c152c796
AM
10003 loaded != NULL;
10004 loaded = loaded->next)
10005 {
10006 bfd *input;
10007 Elf_Internal_Shdr *hdr;
ef53be89
AM
10008 size_t symcount;
10009 size_t extsymcount;
10010 size_t extsymoff;
c152c796
AM
10011 Elf_Internal_Shdr *versymhdr;
10012 Elf_Internal_Sym *isym;
10013 Elf_Internal_Sym *isymend;
10014 Elf_Internal_Sym *isymbuf;
10015 Elf_External_Versym *ever;
10016 Elf_External_Versym *extversym;
10017
10018 input = loaded->abfd;
10019
10020 /* We check each DSO for a possible hidden versioned definition. */
10021 if (input == abfd
c152c796
AM
10022 || elf_dynversym (input) == 0)
10023 continue;
10024
10025 hdr = &elf_tdata (input)->dynsymtab_hdr;
10026
10027 symcount = hdr->sh_size / bed->s->sizeof_sym;
10028 if (elf_bad_symtab (input))
10029 {
10030 extsymcount = symcount;
10031 extsymoff = 0;
10032 }
10033 else
10034 {
10035 extsymcount = symcount - hdr->sh_info;
10036 extsymoff = hdr->sh_info;
10037 }
10038
10039 if (extsymcount == 0)
10040 continue;
10041
10042 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
10043 NULL, NULL, NULL);
10044 if (isymbuf == NULL)
10045 return FALSE;
10046
10047 /* Read in any version definitions. */
10048 versymhdr = &elf_tdata (input)->dynversym_hdr;
c152c796 10049 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
2bb3687b
AM
10050 || (extversym = (Elf_External_Versym *)
10051 _bfd_malloc_and_read (input, versymhdr->sh_size,
10052 versymhdr->sh_size)) == NULL)
c152c796 10053 {
c152c796
AM
10054 free (isymbuf);
10055 return FALSE;
10056 }
10057
10058 ever = extversym + extsymoff;
10059 isymend = isymbuf + extsymcount;
10060 for (isym = isymbuf; isym < isymend; isym++, ever++)
10061 {
10062 const char *name;
10063 Elf_Internal_Versym iver;
10064 unsigned short version_index;
10065
10066 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
10067 || isym->st_shndx == SHN_UNDEF)
10068 continue;
10069
10070 name = bfd_elf_string_from_elf_section (input,
10071 hdr->sh_link,
10072 isym->st_name);
10073 if (strcmp (name, h->root.root.string) != 0)
10074 continue;
10075
10076 _bfd_elf_swap_versym_in (input, ever, &iver);
10077
d023c380
L
10078 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
10079 && !(h->def_regular
10080 && h->forced_local))
c152c796
AM
10081 {
10082 /* If we have a non-hidden versioned sym, then it should
d023c380
L
10083 have provided a definition for the undefined sym unless
10084 it is defined in a non-shared object and forced local.
10085 */
c152c796
AM
10086 abort ();
10087 }
10088
10089 version_index = iver.vs_vers & VERSYM_VERSION;
10090 if (version_index == 1 || version_index == 2)
10091 {
10092 /* This is the base or first version. We can use it. */
10093 free (extversym);
10094 free (isymbuf);
10095 return TRUE;
10096 }
10097 }
10098
10099 free (extversym);
10100 free (isymbuf);
10101 }
10102
10103 return FALSE;
10104}
10105
b8871f35
L
10106/* Convert ELF common symbol TYPE. */
10107
10108static int
10109elf_link_convert_common_type (struct bfd_link_info *info, int type)
10110{
10111 /* Commom symbol can only appear in relocatable link. */
10112 if (!bfd_link_relocatable (info))
10113 abort ();
10114 switch (info->elf_stt_common)
10115 {
10116 case unchanged:
10117 break;
10118 case elf_stt_common:
10119 type = STT_COMMON;
10120 break;
10121 case no_elf_stt_common:
10122 type = STT_OBJECT;
10123 break;
10124 }
10125 return type;
10126}
10127
c152c796
AM
10128/* Add an external symbol to the symbol table. This is called from
10129 the hash table traversal routine. When generating a shared object,
10130 we go through the symbol table twice. The first time we output
10131 anything that might have been forced to local scope in a version
10132 script. The second time we output the symbols that are still
10133 global symbols. */
10134
10135static bfd_boolean
7686d77d 10136elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
c152c796 10137{
7686d77d 10138 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
a50b1753 10139 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8b127cbc 10140 struct elf_final_link_info *flinfo = eoinfo->flinfo;
c152c796
AM
10141 bfd_boolean strip;
10142 Elf_Internal_Sym sym;
10143 asection *input_sec;
10144 const struct elf_backend_data *bed;
6e0b88f1
AM
10145 long indx;
10146 int ret;
b8871f35 10147 unsigned int type;
c152c796
AM
10148
10149 if (h->root.type == bfd_link_hash_warning)
10150 {
10151 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10152 if (h->root.type == bfd_link_hash_new)
10153 return TRUE;
10154 }
10155
10156 /* Decide whether to output this symbol in this pass. */
10157 if (eoinfo->localsyms)
10158 {
4deb8f71 10159 if (!h->forced_local)
c152c796
AM
10160 return TRUE;
10161 }
10162 else
10163 {
4deb8f71 10164 if (h->forced_local)
c152c796
AM
10165 return TRUE;
10166 }
10167
8b127cbc 10168 bed = get_elf_backend_data (flinfo->output_bfd);
c152c796 10169
12ac1cf5 10170 if (h->root.type == bfd_link_hash_undefined)
c152c796 10171 {
12ac1cf5
NC
10172 /* If we have an undefined symbol reference here then it must have
10173 come from a shared library that is being linked in. (Undefined
98da7939
L
10174 references in regular files have already been handled unless
10175 they are in unreferenced sections which are removed by garbage
10176 collection). */
12ac1cf5
NC
10177 bfd_boolean ignore_undef = FALSE;
10178
10179 /* Some symbols may be special in that the fact that they're
10180 undefined can be safely ignored - let backend determine that. */
10181 if (bed->elf_backend_ignore_undef_symbol)
10182 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
10183
10184 /* If we are reporting errors for this situation then do so now. */
89a2ee5a 10185 if (!ignore_undef
c54f1524 10186 && h->ref_dynamic_nonweak
8b127cbc
AM
10187 && (!h->ref_regular || flinfo->info->gc_sections)
10188 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
10189 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
95a51568
FS
10190 {
10191 flinfo->info->callbacks->undefined_symbol
10192 (flinfo->info, h->root.root.string,
10193 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
10194 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
10195 && !flinfo->info->warn_unresolved_syms);
10196 }
97196564
L
10197
10198 /* Strip a global symbol defined in a discarded section. */
10199 if (h->indx == -3)
10200 return TRUE;
c152c796
AM
10201 }
10202
10203 /* We should also warn if a forced local symbol is referenced from
10204 shared libraries. */
0e1862bb 10205 if (bfd_link_executable (flinfo->info)
f5385ebf
AM
10206 && h->forced_local
10207 && h->ref_dynamic
371a5866 10208 && h->def_regular
f5385ebf 10209 && !h->dynamic_def
ee659f1f 10210 && h->ref_dynamic_nonweak
8b127cbc 10211 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
c152c796 10212 {
17d078c5
AM
10213 bfd *def_bfd;
10214 const char *msg;
90c984fc
L
10215 struct elf_link_hash_entry *hi = h;
10216
10217 /* Check indirect symbol. */
10218 while (hi->root.type == bfd_link_hash_indirect)
10219 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
17d078c5
AM
10220
10221 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
695344c0 10222 /* xgettext:c-format */
871b3ab2 10223 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
17d078c5 10224 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
695344c0 10225 /* xgettext:c-format */
871b3ab2 10226 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
17d078c5 10227 else
695344c0 10228 /* xgettext:c-format */
871b3ab2 10229 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
8b127cbc 10230 def_bfd = flinfo->output_bfd;
90c984fc
L
10231 if (hi->root.u.def.section != bfd_abs_section_ptr)
10232 def_bfd = hi->root.u.def.section->owner;
c08bb8dd
AM
10233 _bfd_error_handler (msg, flinfo->output_bfd,
10234 h->root.root.string, def_bfd);
17d078c5 10235 bfd_set_error (bfd_error_bad_value);
c152c796
AM
10236 eoinfo->failed = TRUE;
10237 return FALSE;
10238 }
10239
10240 /* We don't want to output symbols that have never been mentioned by
10241 a regular file, or that we have been told to strip. However, if
10242 h->indx is set to -2, the symbol is used by a reloc and we must
10243 output it. */
d983c8c5 10244 strip = FALSE;
c152c796 10245 if (h->indx == -2)
d983c8c5 10246 ;
f5385ebf 10247 else if ((h->def_dynamic
77cfaee6
AM
10248 || h->ref_dynamic
10249 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
10250 && !h->def_regular
10251 && !h->ref_regular)
c152c796 10252 strip = TRUE;
8b127cbc 10253 else if (flinfo->info->strip == strip_all)
c152c796 10254 strip = TRUE;
8b127cbc
AM
10255 else if (flinfo->info->strip == strip_some
10256 && bfd_hash_lookup (flinfo->info->keep_hash,
c152c796
AM
10257 h->root.root.string, FALSE, FALSE) == NULL)
10258 strip = TRUE;
d56d55e7
AM
10259 else if ((h->root.type == bfd_link_hash_defined
10260 || h->root.type == bfd_link_hash_defweak)
8b127cbc 10261 && ((flinfo->info->strip_discarded
dbaa2011 10262 && discarded_section (h->root.u.def.section))
ca4be51c
AM
10263 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
10264 && h->root.u.def.section->owner != NULL
d56d55e7 10265 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
c152c796 10266 strip = TRUE;
9e2278f5
AM
10267 else if ((h->root.type == bfd_link_hash_undefined
10268 || h->root.type == bfd_link_hash_undefweak)
10269 && h->root.u.undef.abfd != NULL
10270 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
10271 strip = TRUE;
c152c796 10272
b8871f35
L
10273 type = h->type;
10274
c152c796 10275 /* If we're stripping it, and it's not a dynamic symbol, there's
d983c8c5
AM
10276 nothing else to do. However, if it is a forced local symbol or
10277 an ifunc symbol we need to give the backend finish_dynamic_symbol
10278 function a chance to make it dynamic. */
c152c796
AM
10279 if (strip
10280 && h->dynindx == -1
b8871f35 10281 && type != STT_GNU_IFUNC
f5385ebf 10282 && !h->forced_local)
c152c796
AM
10283 return TRUE;
10284
10285 sym.st_value = 0;
10286 sym.st_size = h->size;
10287 sym.st_other = h->other;
c152c796
AM
10288 switch (h->root.type)
10289 {
10290 default:
10291 case bfd_link_hash_new:
10292 case bfd_link_hash_warning:
10293 abort ();
10294 return FALSE;
10295
10296 case bfd_link_hash_undefined:
10297 case bfd_link_hash_undefweak:
10298 input_sec = bfd_und_section_ptr;
10299 sym.st_shndx = SHN_UNDEF;
10300 break;
10301
10302 case bfd_link_hash_defined:
10303 case bfd_link_hash_defweak:
10304 {
10305 input_sec = h->root.u.def.section;
10306 if (input_sec->output_section != NULL)
10307 {
10308 sym.st_shndx =
8b127cbc 10309 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
c152c796
AM
10310 input_sec->output_section);
10311 if (sym.st_shndx == SHN_BAD)
10312 {
4eca0228 10313 _bfd_error_handler
695344c0 10314 /* xgettext:c-format */
871b3ab2 10315 (_("%pB: could not find output section %pA for input section %pA"),
8b127cbc 10316 flinfo->output_bfd, input_sec->output_section, input_sec);
17d078c5 10317 bfd_set_error (bfd_error_nonrepresentable_section);
c152c796
AM
10318 eoinfo->failed = TRUE;
10319 return FALSE;
10320 }
10321
10322 /* ELF symbols in relocatable files are section relative,
10323 but in nonrelocatable files they are virtual
10324 addresses. */
10325 sym.st_value = h->root.u.def.value + input_sec->output_offset;
0e1862bb 10326 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10327 {
10328 sym.st_value += input_sec->output_section->vma;
10329 if (h->type == STT_TLS)
10330 {
8b127cbc 10331 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
430a16a5
NC
10332 if (tls_sec != NULL)
10333 sym.st_value -= tls_sec->vma;
c152c796
AM
10334 }
10335 }
10336 }
10337 else
10338 {
10339 BFD_ASSERT (input_sec->owner == NULL
10340 || (input_sec->owner->flags & DYNAMIC) != 0);
10341 sym.st_shndx = SHN_UNDEF;
10342 input_sec = bfd_und_section_ptr;
10343 }
10344 }
10345 break;
10346
10347 case bfd_link_hash_common:
10348 input_sec = h->root.u.c.p->section;
a4d8e49b 10349 sym.st_shndx = bed->common_section_index (input_sec);
c152c796
AM
10350 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10351 break;
10352
10353 case bfd_link_hash_indirect:
10354 /* These symbols are created by symbol versioning. They point
10355 to the decorated version of the name. For example, if the
10356 symbol foo@@GNU_1.2 is the default, which should be used when
10357 foo is used with no version, then we add an indirect symbol
10358 foo which points to foo@@GNU_1.2. We ignore these symbols,
10359 since the indirected symbol is already in the hash table. */
10360 return TRUE;
10361 }
10362
b8871f35
L
10363 if (type == STT_COMMON || type == STT_OBJECT)
10364 switch (h->root.type)
10365 {
10366 case bfd_link_hash_common:
10367 type = elf_link_convert_common_type (flinfo->info, type);
10368 break;
10369 case bfd_link_hash_defined:
10370 case bfd_link_hash_defweak:
10371 if (bed->common_definition (&sym))
10372 type = elf_link_convert_common_type (flinfo->info, type);
10373 else
10374 type = STT_OBJECT;
10375 break;
10376 case bfd_link_hash_undefined:
10377 case bfd_link_hash_undefweak:
10378 break;
10379 default:
10380 abort ();
10381 }
10382
4deb8f71 10383 if (h->forced_local)
b8871f35
L
10384 {
10385 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10386 /* Turn off visibility on local symbol. */
10387 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10388 }
10389 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10390 else if (h->unique_global && h->def_regular)
10391 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10392 else if (h->root.type == bfd_link_hash_undefweak
10393 || h->root.type == bfd_link_hash_defweak)
10394 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10395 else
10396 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10397 sym.st_target_internal = h->target_internal;
10398
c152c796
AM
10399 /* Give the processor backend a chance to tweak the symbol value,
10400 and also to finish up anything that needs to be done for this
10401 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
3aa14d16 10402 forced local syms when non-shared is due to a historical quirk.
5f35ea9c 10403 STT_GNU_IFUNC symbol must go through PLT. */
3aa14d16 10404 if ((h->type == STT_GNU_IFUNC
5f35ea9c 10405 && h->def_regular
0e1862bb 10406 && !bfd_link_relocatable (flinfo->info))
3aa14d16
L
10407 || ((h->dynindx != -1
10408 || h->forced_local)
0e1862bb 10409 && ((bfd_link_pic (flinfo->info)
3aa14d16
L
10410 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10411 || h->root.type != bfd_link_hash_undefweak))
10412 || !h->forced_local)
8b127cbc 10413 && elf_hash_table (flinfo->info)->dynamic_sections_created))
c152c796
AM
10414 {
10415 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8b127cbc 10416 (flinfo->output_bfd, flinfo->info, h, &sym)))
c152c796
AM
10417 {
10418 eoinfo->failed = TRUE;
10419 return FALSE;
10420 }
10421 }
10422
10423 /* If we are marking the symbol as undefined, and there are no
10424 non-weak references to this symbol from a regular object, then
10425 mark the symbol as weak undefined; if there are non-weak
10426 references, mark the symbol as strong. We can't do this earlier,
10427 because it might not be marked as undefined until the
10428 finish_dynamic_symbol routine gets through with it. */
10429 if (sym.st_shndx == SHN_UNDEF
f5385ebf 10430 && h->ref_regular
c152c796
AM
10431 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10432 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10433 {
10434 int bindtype;
b8871f35 10435 type = ELF_ST_TYPE (sym.st_info);
2955ec4c
L
10436
10437 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10438 if (type == STT_GNU_IFUNC)
10439 type = STT_FUNC;
c152c796 10440
f5385ebf 10441 if (h->ref_regular_nonweak)
c152c796
AM
10442 bindtype = STB_GLOBAL;
10443 else
10444 bindtype = STB_WEAK;
2955ec4c 10445 sym.st_info = ELF_ST_INFO (bindtype, type);
c152c796
AM
10446 }
10447
bda987c2
CD
10448 /* If this is a symbol defined in a dynamic library, don't use the
10449 symbol size from the dynamic library. Relinking an executable
10450 against a new library may introduce gratuitous changes in the
10451 executable's symbols if we keep the size. */
10452 if (sym.st_shndx == SHN_UNDEF
10453 && !h->def_regular
10454 && h->def_dynamic)
10455 sym.st_size = 0;
10456
c152c796
AM
10457 /* If a non-weak symbol with non-default visibility is not defined
10458 locally, it is a fatal error. */
0e1862bb 10459 if (!bfd_link_relocatable (flinfo->info)
c152c796
AM
10460 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10461 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10462 && h->root.type == bfd_link_hash_undefined
f5385ebf 10463 && !h->def_regular)
c152c796 10464 {
17d078c5
AM
10465 const char *msg;
10466
10467 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
695344c0 10468 /* xgettext:c-format */
871b3ab2 10469 msg = _("%pB: protected symbol `%s' isn't defined");
17d078c5 10470 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
695344c0 10471 /* xgettext:c-format */
871b3ab2 10472 msg = _("%pB: internal symbol `%s' isn't defined");
17d078c5 10473 else
695344c0 10474 /* xgettext:c-format */
871b3ab2 10475 msg = _("%pB: hidden symbol `%s' isn't defined");
4eca0228 10476 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
17d078c5 10477 bfd_set_error (bfd_error_bad_value);
c152c796
AM
10478 eoinfo->failed = TRUE;
10479 return FALSE;
10480 }
10481
10482 /* If this symbol should be put in the .dynsym section, then put it
10483 there now. We already know the symbol index. We also fill in
10484 the entry in the .hash section. */
1c2649ed
EB
10485 if (h->dynindx != -1
10486 && elf_hash_table (flinfo->info)->dynamic_sections_created
10487 && elf_hash_table (flinfo->info)->dynsym != NULL
10488 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
c152c796 10489 {
c152c796
AM
10490 bfd_byte *esym;
10491
90c984fc
L
10492 /* Since there is no version information in the dynamic string,
10493 if there is no version info in symbol version section, we will
1659f720 10494 have a run-time problem if not linking executable, referenced
4deb8f71 10495 by shared library, or not bound locally. */
1659f720 10496 if (h->verinfo.verdef == NULL
0e1862bb 10497 && (!bfd_link_executable (flinfo->info)
1659f720
L
10498 || h->ref_dynamic
10499 || !h->def_regular))
90c984fc
L
10500 {
10501 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10502
10503 if (p && p [1] != '\0')
10504 {
4eca0228 10505 _bfd_error_handler
695344c0 10506 /* xgettext:c-format */
9793eb77 10507 (_("%pB: no symbol version section for versioned symbol `%s'"),
90c984fc
L
10508 flinfo->output_bfd, h->root.root.string);
10509 eoinfo->failed = TRUE;
10510 return FALSE;
10511 }
10512 }
10513
c152c796 10514 sym.st_name = h->dynstr_index;
cae1fbbb
L
10515 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10516 + h->dynindx * bed->s->sizeof_sym);
8b127cbc 10517 if (!check_dynsym (flinfo->output_bfd, &sym))
c0d5a53d
L
10518 {
10519 eoinfo->failed = TRUE;
10520 return FALSE;
10521 }
3d16b64e
NA
10522
10523 /* Inform the linker of the addition of this symbol. */
10524
10525 if (flinfo->info->callbacks->ctf_new_dynsym)
10526 flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
10527
8b127cbc 10528 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
c152c796 10529
8b127cbc 10530 if (flinfo->hash_sec != NULL)
fdc90cb4
JJ
10531 {
10532 size_t hash_entry_size;
10533 bfd_byte *bucketpos;
10534 bfd_vma chain;
41198d0c
L
10535 size_t bucketcount;
10536 size_t bucket;
10537
8b127cbc 10538 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
41198d0c 10539 bucket = h->u.elf_hash_value % bucketcount;
fdc90cb4
JJ
10540
10541 hash_entry_size
8b127cbc
AM
10542 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10543 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4 10544 + (bucket + 2) * hash_entry_size);
8b127cbc
AM
10545 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10546 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10547 bucketpos);
10548 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10549 ((bfd_byte *) flinfo->hash_sec->contents
fdc90cb4
JJ
10550 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10551 }
c152c796 10552
8b127cbc 10553 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
c152c796
AM
10554 {
10555 Elf_Internal_Versym iversym;
10556 Elf_External_Versym *eversym;
10557
5fa370e4 10558 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
c152c796 10559 {
7b20f099
AM
10560 if (h->verinfo.verdef == NULL
10561 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10562 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
c152c796
AM
10563 iversym.vs_vers = 0;
10564 else
10565 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10566 }
10567 else
10568 {
10569 if (h->verinfo.vertree == NULL)
10570 iversym.vs_vers = 1;
10571 else
10572 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8b127cbc 10573 if (flinfo->info->create_default_symver)
3e3b46e5 10574 iversym.vs_vers++;
c152c796
AM
10575 }
10576
422f1182 10577 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
6e33951e 10578 defined locally. */
422f1182 10579 if (h->versioned == versioned_hidden && h->def_regular)
c152c796
AM
10580 iversym.vs_vers |= VERSYM_HIDDEN;
10581
8b127cbc 10582 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
c152c796 10583 eversym += h->dynindx;
8b127cbc 10584 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
c152c796
AM
10585 }
10586 }
10587
d983c8c5
AM
10588 /* If the symbol is undefined, and we didn't output it to .dynsym,
10589 strip it from .symtab too. Obviously we can't do this for
10590 relocatable output or when needed for --emit-relocs. */
10591 else if (input_sec == bfd_und_section_ptr
10592 && h->indx != -2
66cae560
NC
10593 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10594 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
0e1862bb 10595 && !bfd_link_relocatable (flinfo->info))
d983c8c5 10596 return TRUE;
66cae560 10597
d983c8c5
AM
10598 /* Also strip others that we couldn't earlier due to dynamic symbol
10599 processing. */
10600 if (strip)
10601 return TRUE;
10602 if ((input_sec->flags & SEC_EXCLUDE) != 0)
c152c796
AM
10603 return TRUE;
10604
2ec55de3
AM
10605 /* Output a FILE symbol so that following locals are not associated
10606 with the wrong input file. We need one for forced local symbols
10607 if we've seen more than one FILE symbol or when we have exactly
10608 one FILE symbol but global symbols are present in a file other
10609 than the one with the FILE symbol. We also need one if linker
10610 defined symbols are present. In practice these conditions are
10611 always met, so just emit the FILE symbol unconditionally. */
10612 if (eoinfo->localsyms
10613 && !eoinfo->file_sym_done
10614 && eoinfo->flinfo->filesym_count != 0)
10615 {
10616 Elf_Internal_Sym fsym;
10617
10618 memset (&fsym, 0, sizeof (fsym));
10619 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10620 fsym.st_shndx = SHN_ABS;
ef10c3ac
L
10621 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10622 bfd_und_section_ptr, NULL))
2ec55de3
AM
10623 return FALSE;
10624
10625 eoinfo->file_sym_done = TRUE;
10626 }
10627
8b127cbc 10628 indx = bfd_get_symcount (flinfo->output_bfd);
ef10c3ac
L
10629 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10630 input_sec, h);
6e0b88f1 10631 if (ret == 0)
c152c796
AM
10632 {
10633 eoinfo->failed = TRUE;
10634 return FALSE;
10635 }
6e0b88f1
AM
10636 else if (ret == 1)
10637 h->indx = indx;
10638 else if (h->indx == -2)
10639 abort();
c152c796
AM
10640
10641 return TRUE;
10642}
10643
cdd3575c
AM
10644/* Return TRUE if special handling is done for relocs in SEC against
10645 symbols defined in discarded sections. */
10646
c152c796
AM
10647static bfd_boolean
10648elf_section_ignore_discarded_relocs (asection *sec)
10649{
10650 const struct elf_backend_data *bed;
10651
cdd3575c
AM
10652 switch (sec->sec_info_type)
10653 {
dbaa2011
AM
10654 case SEC_INFO_TYPE_STABS:
10655 case SEC_INFO_TYPE_EH_FRAME:
2f0c68f2 10656 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
cdd3575c
AM
10657 return TRUE;
10658 default:
10659 break;
10660 }
c152c796
AM
10661
10662 bed = get_elf_backend_data (sec->owner);
10663 if (bed->elf_backend_ignore_discarded_relocs != NULL
10664 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10665 return TRUE;
10666
10667 return FALSE;
10668}
10669
9e66c942
AM
10670/* Return a mask saying how ld should treat relocations in SEC against
10671 symbols defined in discarded sections. If this function returns
10672 COMPLAIN set, ld will issue a warning message. If this function
10673 returns PRETEND set, and the discarded section was link-once and the
10674 same size as the kept link-once section, ld will pretend that the
10675 symbol was actually defined in the kept section. Otherwise ld will
10676 zero the reloc (at least that is the intent, but some cooperation by
10677 the target dependent code is needed, particularly for REL targets). */
10678
8a696751
AM
10679unsigned int
10680_bfd_elf_default_action_discarded (asection *sec)
cdd3575c 10681{
9e66c942 10682 if (sec->flags & SEC_DEBUGGING)
69d54b1b 10683 return PRETEND;
cdd3575c
AM
10684
10685 if (strcmp (".eh_frame", sec->name) == 0)
9e66c942 10686 return 0;
cdd3575c
AM
10687
10688 if (strcmp (".gcc_except_table", sec->name) == 0)
9e66c942 10689 return 0;
cdd3575c 10690
9e66c942 10691 return COMPLAIN | PRETEND;
cdd3575c
AM
10692}
10693
3d7f7666
L
10694/* Find a match between a section and a member of a section group. */
10695
10696static asection *
c0f00686
L
10697match_group_member (asection *sec, asection *group,
10698 struct bfd_link_info *info)
3d7f7666
L
10699{
10700 asection *first = elf_next_in_group (group);
10701 asection *s = first;
10702
10703 while (s != NULL)
10704 {
c0f00686 10705 if (bfd_elf_match_symbols_in_sections (s, sec, info))
3d7f7666
L
10706 return s;
10707
83180ade 10708 s = elf_next_in_group (s);
3d7f7666
L
10709 if (s == first)
10710 break;
10711 }
10712
10713 return NULL;
10714}
10715
01b3c8ab 10716/* Check if the kept section of a discarded section SEC can be used
c2370991
AM
10717 to replace it. Return the replacement if it is OK. Otherwise return
10718 NULL. */
01b3c8ab
L
10719
10720asection *
c0f00686 10721_bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
01b3c8ab
L
10722{
10723 asection *kept;
10724
10725 kept = sec->kept_section;
10726 if (kept != NULL)
10727 {
c2370991 10728 if ((kept->flags & SEC_GROUP) != 0)
c0f00686 10729 kept = match_group_member (sec, kept, info);
58349d00
L
10730 if (kept != NULL)
10731 {
10732 if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10733 != (kept->rawsize != 0 ? kept->rawsize : kept->size))
10734 kept = NULL;
10735 else
10736 {
10737 /* Get the real kept section. */
10738 asection *next;
10739 for (next = kept->kept_section;
10740 next != NULL;
10741 next = next->kept_section)
10742 kept = next;
10743 }
10744 }
c2370991 10745 sec->kept_section = kept;
01b3c8ab
L
10746 }
10747 return kept;
10748}
10749
c152c796
AM
10750/* Link an input file into the linker output file. This function
10751 handles all the sections and relocations of the input file at once.
10752 This is so that we only have to read the local symbols once, and
10753 don't have to keep them in memory. */
10754
10755static bfd_boolean
8b127cbc 10756elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
c152c796 10757{
ece5ef60 10758 int (*relocate_section)
c152c796
AM
10759 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10760 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10761 bfd *output_bfd;
10762 Elf_Internal_Shdr *symtab_hdr;
10763 size_t locsymcount;
10764 size_t extsymoff;
10765 Elf_Internal_Sym *isymbuf;
10766 Elf_Internal_Sym *isym;
10767 Elf_Internal_Sym *isymend;
10768 long *pindex;
10769 asection **ppsection;
10770 asection *o;
10771 const struct elf_backend_data *bed;
c152c796 10772 struct elf_link_hash_entry **sym_hashes;
310fd250
L
10773 bfd_size_type address_size;
10774 bfd_vma r_type_mask;
10775 int r_sym_shift;
ffbc01cc 10776 bfd_boolean have_file_sym = FALSE;
c152c796 10777
8b127cbc 10778 output_bfd = flinfo->output_bfd;
c152c796
AM
10779 bed = get_elf_backend_data (output_bfd);
10780 relocate_section = bed->elf_backend_relocate_section;
10781
10782 /* If this is a dynamic object, we don't want to do anything here:
10783 we don't want the local symbols, and we don't want the section
10784 contents. */
10785 if ((input_bfd->flags & DYNAMIC) != 0)
10786 return TRUE;
10787
c152c796
AM
10788 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10789 if (elf_bad_symtab (input_bfd))
10790 {
10791 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10792 extsymoff = 0;
10793 }
10794 else
10795 {
10796 locsymcount = symtab_hdr->sh_info;
10797 extsymoff = symtab_hdr->sh_info;
10798 }
10799
99fabbc9
JL
10800 /* Enable GNU OSABI features in the output BFD that are used in the input
10801 BFD. */
10802 if (bed->elf_osabi == ELFOSABI_NONE
10803 || bed->elf_osabi == ELFOSABI_GNU
10804 || bed->elf_osabi == ELFOSABI_FREEBSD)
10805 elf_tdata (output_bfd)->has_gnu_osabi
10806 |= elf_tdata (input_bfd)->has_gnu_osabi;
10807
c152c796
AM
10808 /* Read the local symbols. */
10809 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10810 if (isymbuf == NULL && locsymcount != 0)
10811 {
10812 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
8b127cbc
AM
10813 flinfo->internal_syms,
10814 flinfo->external_syms,
10815 flinfo->locsym_shndx);
c152c796
AM
10816 if (isymbuf == NULL)
10817 return FALSE;
10818 }
10819
10820 /* Find local symbol sections and adjust values of symbols in
10821 SEC_MERGE sections. Write out those local symbols we know are
10822 going into the output file. */
10823 isymend = isymbuf + locsymcount;
8b127cbc 10824 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
c152c796
AM
10825 isym < isymend;
10826 isym++, pindex++, ppsection++)
10827 {
10828 asection *isec;
10829 const char *name;
10830 Elf_Internal_Sym osym;
6e0b88f1
AM
10831 long indx;
10832 int ret;
c152c796
AM
10833
10834 *pindex = -1;
10835
10836 if (elf_bad_symtab (input_bfd))
10837 {
10838 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10839 {
10840 *ppsection = NULL;
10841 continue;
10842 }
10843 }
10844
10845 if (isym->st_shndx == SHN_UNDEF)
10846 isec = bfd_und_section_ptr;
c152c796
AM
10847 else if (isym->st_shndx == SHN_ABS)
10848 isec = bfd_abs_section_ptr;
10849 else if (isym->st_shndx == SHN_COMMON)
10850 isec = bfd_com_section_ptr;
10851 else
10852 {
cb33740c
AM
10853 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10854 if (isec == NULL)
10855 {
10856 /* Don't attempt to output symbols with st_shnx in the
10857 reserved range other than SHN_ABS and SHN_COMMON. */
6835821b 10858 isec = bfd_und_section_ptr;
cb33740c 10859 }
dbaa2011 10860 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
cb33740c
AM
10861 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10862 isym->st_value =
10863 _bfd_merged_section_offset (output_bfd, &isec,
10864 elf_section_data (isec)->sec_info,
10865 isym->st_value);
c152c796
AM
10866 }
10867
10868 *ppsection = isec;
10869
d983c8c5
AM
10870 /* Don't output the first, undefined, symbol. In fact, don't
10871 output any undefined local symbol. */
10872 if (isec == bfd_und_section_ptr)
c152c796
AM
10873 continue;
10874
10875 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10876 {
10877 /* We never output section symbols. Instead, we use the
10878 section symbol of the corresponding section in the output
10879 file. */
10880 continue;
10881 }
10882
10883 /* If we are stripping all symbols, we don't want to output this
10884 one. */
8b127cbc 10885 if (flinfo->info->strip == strip_all)
c152c796
AM
10886 continue;
10887
10888 /* If we are discarding all local symbols, we don't want to
10889 output this one. If we are generating a relocatable output
10890 file, then some of the local symbols may be required by
10891 relocs; we output them below as we discover that they are
10892 needed. */
8b127cbc 10893 if (flinfo->info->discard == discard_all)
c152c796
AM
10894 continue;
10895
10896 /* If this symbol is defined in a section which we are
f02571c5 10897 discarding, we don't need to keep it. */
abf874aa
CL
10898 if (isym->st_shndx != SHN_UNDEF
10899 && isym->st_shndx < SHN_LORESERVE
10900 && isec->output_section == NULL
10901 && flinfo->info->non_contiguous_regions
10902 && flinfo->info->non_contiguous_regions_warnings)
10903 {
10904 _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
10905 "discards section `%s' from '%s'\n"),
765cf5f6 10906 isec->name, bfd_get_filename (isec->owner));
abf874aa
CL
10907 continue;
10908 }
10909
f02571c5 10910 if (isym->st_shndx != SHN_UNDEF
4fbb74a6
AM
10911 && isym->st_shndx < SHN_LORESERVE
10912 && bfd_section_removed_from_list (output_bfd,
10913 isec->output_section))
e75a280b
L
10914 continue;
10915
c152c796
AM
10916 /* Get the name of the symbol. */
10917 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10918 isym->st_name);
10919 if (name == NULL)
10920 return FALSE;
10921
10922 /* See if we are discarding symbols with this name. */
8b127cbc
AM
10923 if ((flinfo->info->strip == strip_some
10924 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
c152c796 10925 == NULL))
8b127cbc 10926 || (((flinfo->info->discard == discard_sec_merge
0e1862bb
L
10927 && (isec->flags & SEC_MERGE)
10928 && !bfd_link_relocatable (flinfo->info))
8b127cbc 10929 || flinfo->info->discard == discard_l)
c152c796
AM
10930 && bfd_is_local_label_name (input_bfd, name)))
10931 continue;
10932
ffbc01cc
AM
10933 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10934 {
ce875075
AM
10935 if (input_bfd->lto_output)
10936 /* -flto puts a temp file name here. This means builds
10937 are not reproducible. Discard the symbol. */
10938 continue;
ffbc01cc
AM
10939 have_file_sym = TRUE;
10940 flinfo->filesym_count += 1;
10941 }
10942 if (!have_file_sym)
10943 {
10944 /* In the absence of debug info, bfd_find_nearest_line uses
10945 FILE symbols to determine the source file for local
10946 function symbols. Provide a FILE symbol here if input
10947 files lack such, so that their symbols won't be
10948 associated with a previous input file. It's not the
10949 source file, but the best we can do. */
10950 have_file_sym = TRUE;
10951 flinfo->filesym_count += 1;
10952 memset (&osym, 0, sizeof (osym));
10953 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10954 osym.st_shndx = SHN_ABS;
ef10c3ac
L
10955 if (!elf_link_output_symstrtab (flinfo,
10956 (input_bfd->lto_output ? NULL
765cf5f6 10957 : bfd_get_filename (input_bfd)),
ef10c3ac
L
10958 &osym, bfd_abs_section_ptr,
10959 NULL))
ffbc01cc
AM
10960 return FALSE;
10961 }
10962
c152c796
AM
10963 osym = *isym;
10964
10965 /* Adjust the section index for the output file. */
10966 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10967 isec->output_section);
10968 if (osym.st_shndx == SHN_BAD)
10969 return FALSE;
10970
c152c796
AM
10971 /* ELF symbols in relocatable files are section relative, but
10972 in executable files they are virtual addresses. Note that
10973 this code assumes that all ELF sections have an associated
10974 BFD section with a reasonable value for output_offset; below
10975 we assume that they also have a reasonable value for
10976 output_section. Any special sections must be set up to meet
10977 these requirements. */
10978 osym.st_value += isec->output_offset;
0e1862bb 10979 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
10980 {
10981 osym.st_value += isec->output_section->vma;
10982 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10983 {
10984 /* STT_TLS symbols are relative to PT_TLS segment base. */
102def4d
AM
10985 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
10986 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10987 else
10988 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
10989 STT_NOTYPE);
c152c796
AM
10990 }
10991 }
10992
6e0b88f1 10993 indx = bfd_get_symcount (output_bfd);
ef10c3ac 10994 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
6e0b88f1 10995 if (ret == 0)
c152c796 10996 return FALSE;
6e0b88f1
AM
10997 else if (ret == 1)
10998 *pindex = indx;
c152c796
AM
10999 }
11000
310fd250
L
11001 if (bed->s->arch_size == 32)
11002 {
11003 r_type_mask = 0xff;
11004 r_sym_shift = 8;
11005 address_size = 4;
11006 }
11007 else
11008 {
11009 r_type_mask = 0xffffffff;
11010 r_sym_shift = 32;
11011 address_size = 8;
11012 }
11013
c152c796
AM
11014 /* Relocate the contents of each section. */
11015 sym_hashes = elf_sym_hashes (input_bfd);
11016 for (o = input_bfd->sections; o != NULL; o = o->next)
11017 {
11018 bfd_byte *contents;
11019
11020 if (! o->linker_mark)
11021 {
11022 /* This section was omitted from the link. */
11023 continue;
11024 }
11025
7bdf4127 11026 if (!flinfo->info->resolve_section_groups
bcacc0f5
AM
11027 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
11028 {
11029 /* Deal with the group signature symbol. */
11030 struct bfd_elf_section_data *sec_data = elf_section_data (o);
11031 unsigned long symndx = sec_data->this_hdr.sh_info;
11032 asection *osec = o->output_section;
11033
7bdf4127 11034 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
bcacc0f5
AM
11035 if (symndx >= locsymcount
11036 || (elf_bad_symtab (input_bfd)
8b127cbc 11037 && flinfo->sections[symndx] == NULL))
bcacc0f5
AM
11038 {
11039 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
11040 while (h->root.type == bfd_link_hash_indirect
11041 || h->root.type == bfd_link_hash_warning)
11042 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11043 /* Arrange for symbol to be output. */
11044 h->indx = -2;
11045 elf_section_data (osec)->this_hdr.sh_info = -2;
11046 }
11047 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
11048 {
11049 /* We'll use the output section target_index. */
8b127cbc 11050 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5
AM
11051 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
11052 }
11053 else
11054 {
8b127cbc 11055 if (flinfo->indices[symndx] == -1)
bcacc0f5
AM
11056 {
11057 /* Otherwise output the local symbol now. */
11058 Elf_Internal_Sym sym = isymbuf[symndx];
8b127cbc 11059 asection *sec = flinfo->sections[symndx]->output_section;
bcacc0f5 11060 const char *name;
6e0b88f1
AM
11061 long indx;
11062 int ret;
bcacc0f5
AM
11063
11064 name = bfd_elf_string_from_elf_section (input_bfd,
11065 symtab_hdr->sh_link,
11066 sym.st_name);
11067 if (name == NULL)
11068 return FALSE;
11069
11070 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
11071 sec);
11072 if (sym.st_shndx == SHN_BAD)
11073 return FALSE;
11074
11075 sym.st_value += o->output_offset;
11076
6e0b88f1 11077 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
11078 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
11079 NULL);
6e0b88f1 11080 if (ret == 0)
bcacc0f5 11081 return FALSE;
6e0b88f1 11082 else if (ret == 1)
8b127cbc 11083 flinfo->indices[symndx] = indx;
6e0b88f1
AM
11084 else
11085 abort ();
bcacc0f5
AM
11086 }
11087 elf_section_data (osec)->this_hdr.sh_info
8b127cbc 11088 = flinfo->indices[symndx];
bcacc0f5
AM
11089 }
11090 }
11091
c152c796 11092 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 11093 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
c152c796
AM
11094 continue;
11095
11096 if ((o->flags & SEC_LINKER_CREATED) != 0)
11097 {
11098 /* Section was created by _bfd_elf_link_create_dynamic_sections
11099 or somesuch. */
11100 continue;
11101 }
11102
11103 /* Get the contents of the section. They have been cached by a
11104 relaxation routine. Note that o is a section in an input
11105 file, so the contents field will not have been set by any of
11106 the routines which work on output files. */
11107 if (elf_section_data (o)->this_hdr.contents != NULL)
53291d1f
AM
11108 {
11109 contents = elf_section_data (o)->this_hdr.contents;
11110 if (bed->caches_rawsize
11111 && o->rawsize != 0
11112 && o->rawsize < o->size)
11113 {
11114 memcpy (flinfo->contents, contents, o->rawsize);
11115 contents = flinfo->contents;
11116 }
11117 }
c152c796
AM
11118 else
11119 {
8b127cbc 11120 contents = flinfo->contents;
4a114e3e 11121 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
c152c796
AM
11122 return FALSE;
11123 }
11124
11125 if ((o->flags & SEC_RELOC) != 0)
11126 {
11127 Elf_Internal_Rela *internal_relocs;
0f02bbd9 11128 Elf_Internal_Rela *rel, *relend;
0f02bbd9 11129 int action_discarded;
ece5ef60 11130 int ret;
c152c796
AM
11131
11132 /* Get the swapped relocs. */
11133 internal_relocs
8b127cbc
AM
11134 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
11135 flinfo->internal_relocs, FALSE);
c152c796
AM
11136 if (internal_relocs == NULL
11137 && o->reloc_count > 0)
11138 return FALSE;
11139
310fd250
L
11140 /* We need to reverse-copy input .ctors/.dtors sections if
11141 they are placed in .init_array/.finit_array for output. */
11142 if (o->size > address_size
11143 && ((strncmp (o->name, ".ctors", 6) == 0
11144 && strcmp (o->output_section->name,
11145 ".init_array") == 0)
11146 || (strncmp (o->name, ".dtors", 6) == 0
11147 && strcmp (o->output_section->name,
11148 ".fini_array") == 0))
11149 && (o->name[6] == 0 || o->name[6] == '.'))
c152c796 11150 {
056bafd4
MR
11151 if (o->size * bed->s->int_rels_per_ext_rel
11152 != o->reloc_count * address_size)
310fd250 11153 {
4eca0228 11154 _bfd_error_handler
695344c0 11155 /* xgettext:c-format */
871b3ab2 11156 (_("error: %pB: size of section %pA is not "
310fd250
L
11157 "multiple of address size"),
11158 input_bfd, o);
8c6716e5 11159 bfd_set_error (bfd_error_bad_value);
310fd250
L
11160 return FALSE;
11161 }
11162 o->flags |= SEC_ELF_REVERSE_COPY;
c152c796
AM
11163 }
11164
0f02bbd9 11165 action_discarded = -1;
c152c796 11166 if (!elf_section_ignore_discarded_relocs (o))
0f02bbd9
AM
11167 action_discarded = (*bed->action_discarded) (o);
11168
11169 /* Run through the relocs evaluating complex reloc symbols and
11170 looking for relocs against symbols from discarded sections
11171 or section symbols from removed link-once sections.
11172 Complain about relocs against discarded sections. Zero
11173 relocs against removed link-once sections. */
11174
11175 rel = internal_relocs;
056bafd4 11176 relend = rel + o->reloc_count;
0f02bbd9 11177 for ( ; rel < relend; rel++)
c152c796 11178 {
0f02bbd9
AM
11179 unsigned long r_symndx = rel->r_info >> r_sym_shift;
11180 unsigned int s_type;
11181 asection **ps, *sec;
11182 struct elf_link_hash_entry *h = NULL;
11183 const char *sym_name;
c152c796 11184
0f02bbd9
AM
11185 if (r_symndx == STN_UNDEF)
11186 continue;
c152c796 11187
0f02bbd9
AM
11188 if (r_symndx >= locsymcount
11189 || (elf_bad_symtab (input_bfd)
8b127cbc 11190 && flinfo->sections[r_symndx] == NULL))
0f02bbd9
AM
11191 {
11192 h = sym_hashes[r_symndx - extsymoff];
ee75fd95 11193
0f02bbd9
AM
11194 /* Badly formatted input files can contain relocs that
11195 reference non-existant symbols. Check here so that
11196 we do not seg fault. */
11197 if (h == NULL)
c152c796 11198 {
4eca0228 11199 _bfd_error_handler
695344c0 11200 /* xgettext:c-format */
2dcf00ce 11201 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
0f02bbd9 11202 "that references a non-existent global symbol"),
2dcf00ce 11203 input_bfd, (uint64_t) rel->r_info, o);
0f02bbd9
AM
11204 bfd_set_error (bfd_error_bad_value);
11205 return FALSE;
11206 }
3b36f7e6 11207
0f02bbd9
AM
11208 while (h->root.type == bfd_link_hash_indirect
11209 || h->root.type == bfd_link_hash_warning)
11210 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c152c796 11211
0f02bbd9 11212 s_type = h->type;
cdd3575c 11213
9e2dec47 11214 /* If a plugin symbol is referenced from a non-IR file,
ca4be51c
AM
11215 mark the symbol as undefined. Note that the
11216 linker may attach linker created dynamic sections
11217 to the plugin bfd. Symbols defined in linker
11218 created sections are not plugin symbols. */
bc4e12de 11219 if ((h->root.non_ir_ref_regular
4070765b 11220 || h->root.non_ir_ref_dynamic)
9e2dec47
L
11221 && (h->root.type == bfd_link_hash_defined
11222 || h->root.type == bfd_link_hash_defweak)
11223 && (h->root.u.def.section->flags
11224 & SEC_LINKER_CREATED) == 0
11225 && h->root.u.def.section->owner != NULL
11226 && (h->root.u.def.section->owner->flags
11227 & BFD_PLUGIN) != 0)
11228 {
11229 h->root.type = bfd_link_hash_undefined;
11230 h->root.u.undef.abfd = h->root.u.def.section->owner;
11231 }
11232
0f02bbd9
AM
11233 ps = NULL;
11234 if (h->root.type == bfd_link_hash_defined
11235 || h->root.type == bfd_link_hash_defweak)
11236 ps = &h->root.u.def.section;
11237
11238 sym_name = h->root.root.string;
11239 }
11240 else
11241 {
11242 Elf_Internal_Sym *sym = isymbuf + r_symndx;
11243
11244 s_type = ELF_ST_TYPE (sym->st_info);
8b127cbc 11245 ps = &flinfo->sections[r_symndx];
0f02bbd9
AM
11246 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
11247 sym, *ps);
11248 }
c152c796 11249
c301e700 11250 if ((s_type == STT_RELC || s_type == STT_SRELC)
0e1862bb 11251 && !bfd_link_relocatable (flinfo->info))
0f02bbd9
AM
11252 {
11253 bfd_vma val;
11254 bfd_vma dot = (rel->r_offset
11255 + o->output_offset + o->output_section->vma);
11256#ifdef DEBUG
11257 printf ("Encountered a complex symbol!");
11258 printf (" (input_bfd %s, section %s, reloc %ld\n",
765cf5f6 11259 bfd_get_filename (input_bfd), o->name,
9ccb8af9 11260 (long) (rel - internal_relocs));
0f02bbd9
AM
11261 printf (" symbol: idx %8.8lx, name %s\n",
11262 r_symndx, sym_name);
11263 printf (" reloc : info %8.8lx, addr %8.8lx\n",
11264 (unsigned long) rel->r_info,
11265 (unsigned long) rel->r_offset);
11266#endif
8b127cbc 11267 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
0f02bbd9
AM
11268 isymbuf, locsymcount, s_type == STT_SRELC))
11269 return FALSE;
11270
11271 /* Symbol evaluated OK. Update to absolute value. */
11272 set_symbol_value (input_bfd, isymbuf, locsymcount,
11273 r_symndx, val);
11274 continue;
11275 }
11276
11277 if (action_discarded != -1 && ps != NULL)
11278 {
cdd3575c
AM
11279 /* Complain if the definition comes from a
11280 discarded section. */
dbaa2011 11281 if ((sec = *ps) != NULL && discarded_section (sec))
cdd3575c 11282 {
cf35638d 11283 BFD_ASSERT (r_symndx != STN_UNDEF);
0f02bbd9 11284 if (action_discarded & COMPLAIN)
8b127cbc 11285 (*flinfo->info->callbacks->einfo)
695344c0 11286 /* xgettext:c-format */
871b3ab2
AM
11287 (_("%X`%s' referenced in section `%pA' of %pB: "
11288 "defined in discarded section `%pA' of %pB\n"),
e1fffbe6 11289 sym_name, o, input_bfd, sec, sec->owner);
cdd3575c 11290
87e5235d 11291 /* Try to do the best we can to support buggy old
e0ae6d6f 11292 versions of gcc. Pretend that the symbol is
87e5235d
AM
11293 really defined in the kept linkonce section.
11294 FIXME: This is quite broken. Modifying the
11295 symbol here means we will be changing all later
e0ae6d6f 11296 uses of the symbol, not just in this section. */
0f02bbd9 11297 if (action_discarded & PRETEND)
87e5235d 11298 {
01b3c8ab
L
11299 asection *kept;
11300
c0f00686 11301 kept = _bfd_elf_check_kept_section (sec,
8b127cbc 11302 flinfo->info);
01b3c8ab 11303 if (kept != NULL)
87e5235d
AM
11304 {
11305 *ps = kept;
11306 continue;
11307 }
11308 }
c152c796
AM
11309 }
11310 }
11311 }
11312
11313 /* Relocate the section by invoking a back end routine.
11314
11315 The back end routine is responsible for adjusting the
11316 section contents as necessary, and (if using Rela relocs
11317 and generating a relocatable output file) adjusting the
11318 reloc addend as necessary.
11319
11320 The back end routine does not have to worry about setting
11321 the reloc address or the reloc symbol index.
11322
11323 The back end routine is given a pointer to the swapped in
11324 internal symbols, and can access the hash table entries
11325 for the external symbols via elf_sym_hashes (input_bfd).
11326
11327 When generating relocatable output, the back end routine
11328 must handle STB_LOCAL/STT_SECTION symbols specially. The
11329 output symbol is going to be a section symbol
11330 corresponding to the output section, which will require
11331 the addend to be adjusted. */
11332
8b127cbc 11333 ret = (*relocate_section) (output_bfd, flinfo->info,
c152c796
AM
11334 input_bfd, o, contents,
11335 internal_relocs,
11336 isymbuf,
8b127cbc 11337 flinfo->sections);
ece5ef60 11338 if (!ret)
c152c796
AM
11339 return FALSE;
11340
ece5ef60 11341 if (ret == 2
0e1862bb 11342 || bfd_link_relocatable (flinfo->info)
8b127cbc 11343 || flinfo->info->emitrelocations)
c152c796
AM
11344 {
11345 Elf_Internal_Rela *irela;
d4730f92 11346 Elf_Internal_Rela *irelaend, *irelamid;
c152c796
AM
11347 bfd_vma last_offset;
11348 struct elf_link_hash_entry **rel_hash;
d4730f92
BS
11349 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
11350 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
c152c796 11351 unsigned int next_erel;
c152c796 11352 bfd_boolean rela_normal;
d4730f92 11353 struct bfd_elf_section_data *esdi, *esdo;
c152c796 11354
d4730f92
BS
11355 esdi = elf_section_data (o);
11356 esdo = elf_section_data (o->output_section);
11357 rela_normal = FALSE;
c152c796
AM
11358
11359 /* Adjust the reloc addresses and symbol indices. */
11360
11361 irela = internal_relocs;
056bafd4 11362 irelaend = irela + o->reloc_count;
d4730f92
BS
11363 rel_hash = esdo->rel.hashes + esdo->rel.count;
11364 /* We start processing the REL relocs, if any. When we reach
11365 IRELAMID in the loop, we switch to the RELA relocs. */
11366 irelamid = irela;
11367 if (esdi->rel.hdr != NULL)
11368 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
11369 * bed->s->int_rels_per_ext_rel);
eac338cf 11370 rel_hash_list = rel_hash;
d4730f92 11371 rela_hash_list = NULL;
c152c796 11372 last_offset = o->output_offset;
0e1862bb 11373 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11374 last_offset += o->output_section->vma;
11375 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11376 {
11377 unsigned long r_symndx;
11378 asection *sec;
11379 Elf_Internal_Sym sym;
11380
11381 if (next_erel == bed->s->int_rels_per_ext_rel)
11382 {
11383 rel_hash++;
11384 next_erel = 0;
11385 }
11386
d4730f92
BS
11387 if (irela == irelamid)
11388 {
11389 rel_hash = esdo->rela.hashes + esdo->rela.count;
11390 rela_hash_list = rel_hash;
11391 rela_normal = bed->rela_normal;
11392 }
11393
c152c796 11394 irela->r_offset = _bfd_elf_section_offset (output_bfd,
8b127cbc 11395 flinfo->info, o,
c152c796
AM
11396 irela->r_offset);
11397 if (irela->r_offset >= (bfd_vma) -2)
11398 {
11399 /* This is a reloc for a deleted entry or somesuch.
11400 Turn it into an R_*_NONE reloc, at the same
11401 offset as the last reloc. elf_eh_frame.c and
e460dd0d 11402 bfd_elf_discard_info rely on reloc offsets
c152c796
AM
11403 being ordered. */
11404 irela->r_offset = last_offset;
11405 irela->r_info = 0;
11406 irela->r_addend = 0;
11407 continue;
11408 }
11409
11410 irela->r_offset += o->output_offset;
11411
11412 /* Relocs in an executable have to be virtual addresses. */
0e1862bb 11413 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11414 irela->r_offset += o->output_section->vma;
11415
11416 last_offset = irela->r_offset;
11417
11418 r_symndx = irela->r_info >> r_sym_shift;
11419 if (r_symndx == STN_UNDEF)
11420 continue;
11421
11422 if (r_symndx >= locsymcount
11423 || (elf_bad_symtab (input_bfd)
8b127cbc 11424 && flinfo->sections[r_symndx] == NULL))
c152c796
AM
11425 {
11426 struct elf_link_hash_entry *rh;
11427 unsigned long indx;
11428
11429 /* This is a reloc against a global symbol. We
11430 have not yet output all the local symbols, so
11431 we do not know the symbol index of any global
11432 symbol. We set the rel_hash entry for this
11433 reloc to point to the global hash table entry
11434 for this symbol. The symbol index is then
ee75fd95 11435 set at the end of bfd_elf_final_link. */
c152c796
AM
11436 indx = r_symndx - extsymoff;
11437 rh = elf_sym_hashes (input_bfd)[indx];
11438 while (rh->root.type == bfd_link_hash_indirect
11439 || rh->root.type == bfd_link_hash_warning)
11440 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11441
11442 /* Setting the index to -2 tells
11443 elf_link_output_extsym that this symbol is
11444 used by a reloc. */
11445 BFD_ASSERT (rh->indx < 0);
11446 rh->indx = -2;
c152c796
AM
11447 *rel_hash = rh;
11448
11449 continue;
11450 }
11451
11452 /* This is a reloc against a local symbol. */
11453
11454 *rel_hash = NULL;
11455 sym = isymbuf[r_symndx];
8b127cbc 11456 sec = flinfo->sections[r_symndx];
c152c796
AM
11457 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11458 {
11459 /* I suppose the backend ought to fill in the
11460 section of any STT_SECTION symbol against a
6a8d1586 11461 processor specific section. */
cf35638d 11462 r_symndx = STN_UNDEF;
6a8d1586
AM
11463 if (bfd_is_abs_section (sec))
11464 ;
c152c796
AM
11465 else if (sec == NULL || sec->owner == NULL)
11466 {
11467 bfd_set_error (bfd_error_bad_value);
11468 return FALSE;
11469 }
11470 else
11471 {
6a8d1586
AM
11472 asection *osec = sec->output_section;
11473
11474 /* If we have discarded a section, the output
11475 section will be the absolute section. In
ab96bf03
AM
11476 case of discarded SEC_MERGE sections, use
11477 the kept section. relocate_section should
11478 have already handled discarded linkonce
11479 sections. */
6a8d1586
AM
11480 if (bfd_is_abs_section (osec)
11481 && sec->kept_section != NULL
11482 && sec->kept_section->output_section != NULL)
11483 {
11484 osec = sec->kept_section->output_section;
11485 irela->r_addend -= osec->vma;
11486 }
11487
11488 if (!bfd_is_abs_section (osec))
11489 {
11490 r_symndx = osec->target_index;
cf35638d 11491 if (r_symndx == STN_UNDEF)
74541ad4 11492 {
051d833a
AM
11493 irela->r_addend += osec->vma;
11494 osec = _bfd_nearby_section (output_bfd, osec,
11495 osec->vma);
11496 irela->r_addend -= osec->vma;
11497 r_symndx = osec->target_index;
74541ad4 11498 }
6a8d1586 11499 }
c152c796
AM
11500 }
11501
11502 /* Adjust the addend according to where the
11503 section winds up in the output section. */
11504 if (rela_normal)
11505 irela->r_addend += sec->output_offset;
11506 }
11507 else
11508 {
8b127cbc 11509 if (flinfo->indices[r_symndx] == -1)
c152c796
AM
11510 {
11511 unsigned long shlink;
11512 const char *name;
11513 asection *osec;
6e0b88f1 11514 long indx;
c152c796 11515
8b127cbc 11516 if (flinfo->info->strip == strip_all)
c152c796
AM
11517 {
11518 /* You can't do ld -r -s. */
11519 bfd_set_error (bfd_error_invalid_operation);
11520 return FALSE;
11521 }
11522
11523 /* This symbol was skipped earlier, but
11524 since it is needed by a reloc, we
11525 must output it now. */
11526 shlink = symtab_hdr->sh_link;
11527 name = (bfd_elf_string_from_elf_section
11528 (input_bfd, shlink, sym.st_name));
11529 if (name == NULL)
11530 return FALSE;
11531
11532 osec = sec->output_section;
11533 sym.st_shndx =
11534 _bfd_elf_section_from_bfd_section (output_bfd,
11535 osec);
11536 if (sym.st_shndx == SHN_BAD)
11537 return FALSE;
11538
11539 sym.st_value += sec->output_offset;
0e1862bb 11540 if (!bfd_link_relocatable (flinfo->info))
c152c796
AM
11541 {
11542 sym.st_value += osec->vma;
11543 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11544 {
102def4d
AM
11545 struct elf_link_hash_table *htab
11546 = elf_hash_table (flinfo->info);
11547
c152c796
AM
11548 /* STT_TLS symbols are relative to PT_TLS
11549 segment base. */
102def4d
AM
11550 if (htab->tls_sec != NULL)
11551 sym.st_value -= htab->tls_sec->vma;
11552 else
11553 sym.st_info
11554 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11555 STT_NOTYPE);
c152c796
AM
11556 }
11557 }
11558
6e0b88f1 11559 indx = bfd_get_symcount (output_bfd);
ef10c3ac
L
11560 ret = elf_link_output_symstrtab (flinfo, name,
11561 &sym, sec,
11562 NULL);
6e0b88f1 11563 if (ret == 0)
c152c796 11564 return FALSE;
6e0b88f1 11565 else if (ret == 1)
8b127cbc 11566 flinfo->indices[r_symndx] = indx;
6e0b88f1
AM
11567 else
11568 abort ();
c152c796
AM
11569 }
11570
8b127cbc 11571 r_symndx = flinfo->indices[r_symndx];
c152c796
AM
11572 }
11573
11574 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11575 | (irela->r_info & r_type_mask));
11576 }
11577
11578 /* Swap out the relocs. */
d4730f92
BS
11579 input_rel_hdr = esdi->rel.hdr;
11580 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
c152c796 11581 {
d4730f92
BS
11582 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11583 input_rel_hdr,
11584 internal_relocs,
11585 rel_hash_list))
11586 return FALSE;
c152c796
AM
11587 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11588 * bed->s->int_rels_per_ext_rel);
eac338cf 11589 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
d4730f92
BS
11590 }
11591
11592 input_rela_hdr = esdi->rela.hdr;
11593 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11594 {
eac338cf 11595 if (!bed->elf_backend_emit_relocs (output_bfd, o,
d4730f92 11596 input_rela_hdr,
eac338cf 11597 internal_relocs,
d4730f92 11598 rela_hash_list))
c152c796
AM
11599 return FALSE;
11600 }
11601 }
11602 }
11603
11604 /* Write out the modified section contents. */
11605 if (bed->elf_backend_write_section
8b127cbc 11606 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
c7b8f16e 11607 contents))
c152c796
AM
11608 {
11609 /* Section written out. */
11610 }
11611 else switch (o->sec_info_type)
11612 {
dbaa2011 11613 case SEC_INFO_TYPE_STABS:
c152c796
AM
11614 if (! (_bfd_write_section_stabs
11615 (output_bfd,
8b127cbc 11616 &elf_hash_table (flinfo->info)->stab_info,
c152c796
AM
11617 o, &elf_section_data (o)->sec_info, contents)))
11618 return FALSE;
11619 break;
dbaa2011 11620 case SEC_INFO_TYPE_MERGE:
c152c796
AM
11621 if (! _bfd_write_merged_section (output_bfd, o,
11622 elf_section_data (o)->sec_info))
11623 return FALSE;
11624 break;
dbaa2011 11625 case SEC_INFO_TYPE_EH_FRAME:
c152c796 11626 {
8b127cbc 11627 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
c152c796
AM
11628 o, contents))
11629 return FALSE;
11630 }
11631 break;
2f0c68f2
CM
11632 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11633 {
11634 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11635 flinfo->info,
11636 o, contents))
11637 return FALSE;
11638 }
11639 break;
c152c796
AM
11640 default:
11641 {
310fd250
L
11642 if (! (o->flags & SEC_EXCLUDE))
11643 {
11644 file_ptr offset = (file_ptr) o->output_offset;
11645 bfd_size_type todo = o->size;
37b01f6a 11646
bb294208 11647 offset *= bfd_octets_per_byte (output_bfd, o);
37b01f6a 11648
310fd250
L
11649 if ((o->flags & SEC_ELF_REVERSE_COPY))
11650 {
11651 /* Reverse-copy input section to output. */
11652 do
11653 {
11654 todo -= address_size;
11655 if (! bfd_set_section_contents (output_bfd,
11656 o->output_section,
11657 contents + todo,
11658 offset,
11659 address_size))
11660 return FALSE;
11661 if (todo == 0)
11662 break;
11663 offset += address_size;
11664 }
11665 while (1);
11666 }
11667 else if (! bfd_set_section_contents (output_bfd,
11668 o->output_section,
11669 contents,
11670 offset, todo))
11671 return FALSE;
11672 }
c152c796
AM
11673 }
11674 break;
11675 }
11676 }
11677
11678 return TRUE;
11679}
11680
11681/* Generate a reloc when linking an ELF file. This is a reloc
3a800eb9 11682 requested by the linker, and does not come from any input file. This
c152c796
AM
11683 is used to build constructor and destructor tables when linking
11684 with -Ur. */
11685
11686static bfd_boolean
11687elf_reloc_link_order (bfd *output_bfd,
11688 struct bfd_link_info *info,
11689 asection *output_section,
11690 struct bfd_link_order *link_order)
11691{
11692 reloc_howto_type *howto;
11693 long indx;
11694 bfd_vma offset;
11695 bfd_vma addend;
d4730f92 11696 struct bfd_elf_section_reloc_data *reldata;
c152c796
AM
11697 struct elf_link_hash_entry **rel_hash_ptr;
11698 Elf_Internal_Shdr *rel_hdr;
11699 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11700 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11701 bfd_byte *erel;
11702 unsigned int i;
d4730f92 11703 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
c152c796
AM
11704
11705 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11706 if (howto == NULL)
11707 {
11708 bfd_set_error (bfd_error_bad_value);
11709 return FALSE;
11710 }
11711
11712 addend = link_order->u.reloc.p->addend;
11713
d4730f92
BS
11714 if (esdo->rel.hdr)
11715 reldata = &esdo->rel;
11716 else if (esdo->rela.hdr)
11717 reldata = &esdo->rela;
11718 else
11719 {
11720 reldata = NULL;
11721 BFD_ASSERT (0);
11722 }
11723
c152c796 11724 /* Figure out the symbol index. */
d4730f92 11725 rel_hash_ptr = reldata->hashes + reldata->count;
c152c796
AM
11726 if (link_order->type == bfd_section_reloc_link_order)
11727 {
11728 indx = link_order->u.reloc.p->u.section->target_index;
11729 BFD_ASSERT (indx != 0);
11730 *rel_hash_ptr = NULL;
11731 }
11732 else
11733 {
11734 struct elf_link_hash_entry *h;
11735
11736 /* Treat a reloc against a defined symbol as though it were
11737 actually against the section. */
11738 h = ((struct elf_link_hash_entry *)
11739 bfd_wrapped_link_hash_lookup (output_bfd, info,
11740 link_order->u.reloc.p->u.name,
11741 FALSE, FALSE, TRUE));
11742 if (h != NULL
11743 && (h->root.type == bfd_link_hash_defined
11744 || h->root.type == bfd_link_hash_defweak))
11745 {
11746 asection *section;
11747
11748 section = h->root.u.def.section;
11749 indx = section->output_section->target_index;
11750 *rel_hash_ptr = NULL;
11751 /* It seems that we ought to add the symbol value to the
11752 addend here, but in practice it has already been added
11753 because it was passed to constructor_callback. */
11754 addend += section->output_section->vma + section->output_offset;
11755 }
11756 else if (h != NULL)
11757 {
11758 /* Setting the index to -2 tells elf_link_output_extsym that
11759 this symbol is used by a reloc. */
11760 h->indx = -2;
11761 *rel_hash_ptr = h;
11762 indx = 0;
11763 }
11764 else
11765 {
1a72702b
AM
11766 (*info->callbacks->unattached_reloc)
11767 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
c152c796
AM
11768 indx = 0;
11769 }
11770 }
11771
11772 /* If this is an inplace reloc, we must write the addend into the
11773 object file. */
11774 if (howto->partial_inplace && addend != 0)
11775 {
11776 bfd_size_type size;
11777 bfd_reloc_status_type rstat;
11778 bfd_byte *buf;
11779 bfd_boolean ok;
11780 const char *sym_name;
bb294208 11781 bfd_size_type octets;
c152c796 11782
a50b1753
NC
11783 size = (bfd_size_type) bfd_get_reloc_size (howto);
11784 buf = (bfd_byte *) bfd_zmalloc (size);
6346d5ca 11785 if (buf == NULL && size != 0)
c152c796
AM
11786 return FALSE;
11787 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11788 switch (rstat)
11789 {
11790 case bfd_reloc_ok:
11791 break;
11792
11793 default:
11794 case bfd_reloc_outofrange:
11795 abort ();
11796
11797 case bfd_reloc_overflow:
11798 if (link_order->type == bfd_section_reloc_link_order)
fd361982 11799 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
c152c796
AM
11800 else
11801 sym_name = link_order->u.reloc.p->u.name;
1a72702b
AM
11802 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11803 howto->name, addend, NULL, NULL,
11804 (bfd_vma) 0);
c152c796
AM
11805 break;
11806 }
37b01f6a 11807
bb294208
AM
11808 octets = link_order->offset * bfd_octets_per_byte (output_bfd,
11809 output_section);
c152c796 11810 ok = bfd_set_section_contents (output_bfd, output_section, buf,
bb294208 11811 octets, size);
c152c796
AM
11812 free (buf);
11813 if (! ok)
11814 return FALSE;
11815 }
11816
11817 /* The address of a reloc is relative to the section in a
11818 relocatable file, and is a virtual address in an executable
11819 file. */
11820 offset = link_order->offset;
0e1862bb 11821 if (! bfd_link_relocatable (info))
c152c796
AM
11822 offset += output_section->vma;
11823
11824 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11825 {
11826 irel[i].r_offset = offset;
11827 irel[i].r_info = 0;
11828 irel[i].r_addend = 0;
11829 }
11830 if (bed->s->arch_size == 32)
11831 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11832 else
11833 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11834
d4730f92 11835 rel_hdr = reldata->hdr;
c152c796
AM
11836 erel = rel_hdr->contents;
11837 if (rel_hdr->sh_type == SHT_REL)
11838 {
d4730f92 11839 erel += reldata->count * bed->s->sizeof_rel;
c152c796
AM
11840 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11841 }
11842 else
11843 {
11844 irel[0].r_addend = addend;
d4730f92 11845 erel += reldata->count * bed->s->sizeof_rela;
c152c796
AM
11846 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11847 }
11848
d4730f92 11849 ++reldata->count;
c152c796
AM
11850
11851 return TRUE;
11852}
11853
0b52efa6 11854
0b52efa6
PB
11855/* Compare two sections based on the locations of the sections they are
11856 linked to. Used by elf_fixup_link_order. */
11857
11858static int
8c1c5e5d 11859compare_link_order (const void *a, const void *b)
0b52efa6 11860{
8c1c5e5d
AM
11861 const struct bfd_link_order *alo = *(const struct bfd_link_order **) a;
11862 const struct bfd_link_order *blo = *(const struct bfd_link_order **) b;
11863 asection *asec = elf_linked_to_section (alo->u.indirect.section);
11864 asection *bsec = elf_linked_to_section (blo->u.indirect.section);
11865 bfd_vma apos = asec->output_section->lma + asec->output_offset;
11866 bfd_vma bpos = bsec->output_section->lma + bsec->output_offset;
0b52efa6 11867
0b52efa6
PB
11868 if (apos < bpos)
11869 return -1;
8c1c5e5d
AM
11870 if (apos > bpos)
11871 return 1;
11872
11873 /* The only way we should get matching LMAs is when the first of two
11874 sections has zero size. */
11875 if (asec->size < bsec->size)
11876 return -1;
11877 if (asec->size > bsec->size)
11878 return 1;
11879
11880 /* If they are both zero size then they almost certainly have the same
11881 VMA and thus are not ordered with respect to each other. Test VMA
11882 anyway, and fall back to id to make the result reproducible across
11883 qsort implementations. */
11884 apos = asec->output_section->vma + asec->output_offset;
11885 bpos = bsec->output_section->vma + bsec->output_offset;
11886 if (apos < bpos)
11887 return -1;
11888 if (apos > bpos)
11889 return 1;
11890
11891 return asec->id - bsec->id;
0b52efa6
PB
11892}
11893
11894
11895/* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11896 order as their linked sections. Returns false if this could not be done
11897 because an output section includes both ordered and unordered
11898 sections. Ideally we'd do this in the linker proper. */
11899
11900static bfd_boolean
11901elf_fixup_link_order (bfd *abfd, asection *o)
11902{
8c1c5e5d
AM
11903 size_t seen_linkorder;
11904 size_t seen_other;
11905 size_t n;
0b52efa6
PB
11906 struct bfd_link_order *p;
11907 bfd *sub;
0b52efa6 11908 struct bfd_link_order **sections;
66631823
CE
11909 asection *other_sec, *linkorder_sec;
11910 bfd_vma offset; /* Octets. */
3b36f7e6 11911
d33cdfe3
L
11912 other_sec = NULL;
11913 linkorder_sec = NULL;
0b52efa6
PB
11914 seen_other = 0;
11915 seen_linkorder = 0;
8423293d 11916 for (p = o->map_head.link_order; p != NULL; p = p->next)
0b52efa6 11917 {
d33cdfe3 11918 if (p->type == bfd_indirect_link_order)
0b52efa6 11919 {
66631823 11920 asection *s = p->u.indirect.section;
d33cdfe3 11921 sub = s->owner;
847d5183
AM
11922 if ((s->flags & SEC_LINKER_CREATED) == 0
11923 && bfd_get_flavour (sub) == bfd_target_elf_flavour
8c1c5e5d
AM
11924 && elf_section_data (s) != NULL
11925 && elf_linked_to_section (s) != NULL)
d33cdfe3
L
11926 {
11927 seen_linkorder++;
11928 linkorder_sec = s;
11929 }
0b52efa6 11930 else
d33cdfe3
L
11931 {
11932 seen_other++;
11933 other_sec = s;
11934 }
0b52efa6
PB
11935 }
11936 else
11937 seen_other++;
d33cdfe3
L
11938
11939 if (seen_other && seen_linkorder)
11940 {
11941 if (other_sec && linkorder_sec)
4eca0228 11942 _bfd_error_handler
695344c0 11943 /* xgettext:c-format */
871b3ab2
AM
11944 (_("%pA has both ordered [`%pA' in %pB] "
11945 "and unordered [`%pA' in %pB] sections"),
63a5468a
AM
11946 o, linkorder_sec, linkorder_sec->owner,
11947 other_sec, other_sec->owner);
d33cdfe3 11948 else
4eca0228 11949 _bfd_error_handler
871b3ab2 11950 (_("%pA has both ordered and unordered sections"), o);
d33cdfe3
L
11951 bfd_set_error (bfd_error_bad_value);
11952 return FALSE;
11953 }
0b52efa6
PB
11954 }
11955
11956 if (!seen_linkorder)
11957 return TRUE;
11958
8c1c5e5d 11959 sections = bfd_malloc (seen_linkorder * sizeof (*sections));
14b1c01e
AM
11960 if (sections == NULL)
11961 return FALSE;
3b36f7e6 11962
8c1c5e5d 11963 seen_linkorder = 0;
8423293d 11964 for (p = o->map_head.link_order; p != NULL; p = p->next)
8c1c5e5d
AM
11965 sections[seen_linkorder++] = p;
11966
0b52efa6 11967 /* Sort the input sections in the order of their linked section. */
8c1c5e5d 11968 qsort (sections, seen_linkorder, sizeof (*sections), compare_link_order);
0b52efa6
PB
11969
11970 /* Change the offsets of the sections. */
11971 offset = 0;
11972 for (n = 0; n < seen_linkorder; n++)
11973 {
8c1c5e5d 11974 bfd_vma mask;
66631823
CE
11975 asection *s = sections[n]->u.indirect.section;
11976 unsigned int opb = bfd_octets_per_byte (abfd, s);
11977
11978 mask = ~(bfd_vma) 0 << s->alignment_power * opb;
8c1c5e5d 11979 offset = (offset + ~mask) & mask;
66631823 11980 sections[n]->offset = s->output_offset = offset / opb;
0b52efa6
PB
11981 offset += sections[n]->size;
11982 }
11983
4dd07732 11984 free (sections);
0b52efa6
PB
11985 return TRUE;
11986}
11987
76359541
TP
11988/* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11989 Returns TRUE upon success, FALSE otherwise. */
11990
11991static bfd_boolean
11992elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11993{
11994 bfd_boolean ret = FALSE;
11995 bfd *implib_bfd;
11996 const struct elf_backend_data *bed;
11997 flagword flags;
11998 enum bfd_architecture arch;
11999 unsigned int mach;
12000 asymbol **sympp = NULL;
12001 long symsize;
12002 long symcount;
12003 long src_count;
12004 elf_symbol_type *osymbuf;
446f7ed5 12005 size_t amt;
76359541
TP
12006
12007 implib_bfd = info->out_implib_bfd;
12008 bed = get_elf_backend_data (abfd);
12009
12010 if (!bfd_set_format (implib_bfd, bfd_object))
12011 return FALSE;
12012
046734ff 12013 /* Use flag from executable but make it a relocatable object. */
76359541
TP
12014 flags = bfd_get_file_flags (abfd);
12015 flags &= ~HAS_RELOC;
12016 if (!bfd_set_start_address (implib_bfd, 0)
046734ff 12017 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
76359541
TP
12018 return FALSE;
12019
12020 /* Copy architecture of output file to import library file. */
12021 arch = bfd_get_arch (abfd);
12022 mach = bfd_get_mach (abfd);
12023 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
12024 && (abfd->target_defaulted
12025 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
12026 return FALSE;
12027
12028 /* Get symbol table size. */
12029 symsize = bfd_get_symtab_upper_bound (abfd);
12030 if (symsize < 0)
12031 return FALSE;
12032
12033 /* Read in the symbol table. */
ec9bd0a2
AM
12034 sympp = (asymbol **) bfd_malloc (symsize);
12035 if (sympp == NULL)
12036 return FALSE;
12037
76359541
TP
12038 symcount = bfd_canonicalize_symtab (abfd, sympp);
12039 if (symcount < 0)
12040 goto free_sym_buf;
12041
12042 /* Allow the BFD backend to copy any private header data it
12043 understands from the output BFD to the import library BFD. */
12044 if (! bfd_copy_private_header_data (abfd, implib_bfd))
12045 goto free_sym_buf;
12046
12047 /* Filter symbols to appear in the import library. */
12048 if (bed->elf_backend_filter_implib_symbols)
12049 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
12050 symcount);
12051 else
12052 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
12053 if (symcount == 0)
12054 {
5df1bc57 12055 bfd_set_error (bfd_error_no_symbols);
871b3ab2 12056 _bfd_error_handler (_("%pB: no symbol found for import library"),
4eca0228 12057 implib_bfd);
76359541
TP
12058 goto free_sym_buf;
12059 }
12060
12061
12062 /* Make symbols absolute. */
446f7ed5
AM
12063 amt = symcount * sizeof (*osymbuf);
12064 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
ec9bd0a2
AM
12065 if (osymbuf == NULL)
12066 goto free_sym_buf;
12067
76359541
TP
12068 for (src_count = 0; src_count < symcount; src_count++)
12069 {
12070 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
12071 sizeof (*osymbuf));
12072 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
12073 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
12074 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
12075 osymbuf[src_count].internal_elf_sym.st_value =
12076 osymbuf[src_count].symbol.value;
12077 sympp[src_count] = &osymbuf[src_count].symbol;
12078 }
12079
12080 bfd_set_symtab (implib_bfd, sympp, symcount);
12081
12082 /* Allow the BFD backend to copy any private data it understands
12083 from the output BFD to the import library BFD. This is done last
12084 to permit the routine to look at the filtered symbol table. */
12085 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
12086 goto free_sym_buf;
12087
12088 if (!bfd_close (implib_bfd))
12089 goto free_sym_buf;
12090
12091 ret = TRUE;
12092
dc1e8a47 12093 free_sym_buf:
76359541
TP
12094 free (sympp);
12095 return ret;
12096}
12097
9f7c3e5e
AM
12098static void
12099elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
12100{
12101 asection *o;
12102
12103 if (flinfo->symstrtab != NULL)
ef10c3ac 12104 _bfd_elf_strtab_free (flinfo->symstrtab);
c9594989
AM
12105 free (flinfo->contents);
12106 free (flinfo->external_relocs);
12107 free (flinfo->internal_relocs);
12108 free (flinfo->external_syms);
12109 free (flinfo->locsym_shndx);
12110 free (flinfo->internal_syms);
12111 free (flinfo->indices);
12112 free (flinfo->sections);
12113 if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
9f7c3e5e
AM
12114 free (flinfo->symshndxbuf);
12115 for (o = obfd->sections; o != NULL; o = o->next)
12116 {
12117 struct bfd_elf_section_data *esdo = elf_section_data (o);
c9594989
AM
12118 free (esdo->rel.hashes);
12119 free (esdo->rela.hashes);
9f7c3e5e
AM
12120 }
12121}
0b52efa6 12122
c152c796
AM
12123/* Do the final step of an ELF link. */
12124
12125bfd_boolean
12126bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
12127{
12128 bfd_boolean dynamic;
12129 bfd_boolean emit_relocs;
12130 bfd *dynobj;
8b127cbc 12131 struct elf_final_link_info flinfo;
91d6fa6a
NC
12132 asection *o;
12133 struct bfd_link_order *p;
12134 bfd *sub;
c152c796
AM
12135 bfd_size_type max_contents_size;
12136 bfd_size_type max_external_reloc_size;
12137 bfd_size_type max_internal_reloc_count;
12138 bfd_size_type max_sym_count;
12139 bfd_size_type max_sym_shndx_count;
c152c796
AM
12140 Elf_Internal_Sym elfsym;
12141 unsigned int i;
12142 Elf_Internal_Shdr *symtab_hdr;
12143 Elf_Internal_Shdr *symtab_shndx_hdr;
c152c796
AM
12144 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12145 struct elf_outext_info eoinfo;
12146 bfd_boolean merged;
12147 size_t relativecount = 0;
12148 asection *reldyn = 0;
12149 bfd_size_type amt;
104d59d1
JM
12150 asection *attr_section = NULL;
12151 bfd_vma attr_size = 0;
12152 const char *std_attrs_section;
64f52338 12153 struct elf_link_hash_table *htab = elf_hash_table (info);
4c6ee646 12154 bfd_boolean sections_removed;
496afd17 12155 bfd_boolean ret;
c152c796 12156
64f52338 12157 if (!is_elf_hash_table (htab))
c152c796
AM
12158 return FALSE;
12159
0e1862bb 12160 if (bfd_link_pic (info))
c152c796
AM
12161 abfd->flags |= DYNAMIC;
12162
64f52338
AM
12163 dynamic = htab->dynamic_sections_created;
12164 dynobj = htab->dynobj;
c152c796 12165
0e1862bb 12166 emit_relocs = (bfd_link_relocatable (info)
a4676736 12167 || info->emitrelocations);
c152c796 12168
496afd17 12169 memset (&flinfo, 0, sizeof (flinfo));
8b127cbc
AM
12170 flinfo.info = info;
12171 flinfo.output_bfd = abfd;
ef10c3ac 12172 flinfo.symstrtab = _bfd_elf_strtab_init ();
8b127cbc 12173 if (flinfo.symstrtab == NULL)
c152c796
AM
12174 return FALSE;
12175
12176 if (! dynamic)
12177 {
8b127cbc
AM
12178 flinfo.hash_sec = NULL;
12179 flinfo.symver_sec = NULL;
c152c796
AM
12180 }
12181 else
12182 {
3d4d4302 12183 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
202e2356 12184 /* Note that dynsym_sec can be NULL (on VMS). */
3d4d4302 12185 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
c152c796
AM
12186 /* Note that it is OK if symver_sec is NULL. */
12187 }
12188
496afd17
L
12189 if (info->unique_symbol
12190 && !bfd_hash_table_init (&flinfo.local_hash_table,
12191 local_hash_newfunc,
12192 sizeof (struct local_hash_entry)))
12193 return FALSE;
c152c796 12194
104d59d1
JM
12195 /* The object attributes have been merged. Remove the input
12196 sections from the link, and set the contents of the output
1ff6de03 12197 section. */
4c6ee646 12198 sections_removed = FALSE;
104d59d1
JM
12199 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
12200 for (o = abfd->sections; o != NULL; o = o->next)
12201 {
5270eddc 12202 bfd_boolean remove_section = FALSE;
b8a6ced7 12203
104d59d1
JM
12204 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
12205 || strcmp (o->name, ".gnu.attributes") == 0)
12206 {
12207 for (p = o->map_head.link_order; p != NULL; p = p->next)
12208 {
12209 asection *input_section;
12210
12211 if (p->type != bfd_indirect_link_order)
12212 continue;
12213 input_section = p->u.indirect.section;
12214 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12215 elf_link_input_bfd ignores this section. */
12216 input_section->flags &= ~SEC_HAS_CONTENTS;
12217 }
a0c8462f 12218
104d59d1 12219 attr_size = bfd_elf_obj_attr_size (abfd);
fd361982 12220 bfd_set_section_size (o, attr_size);
b8a6ced7
AM
12221 /* Skip this section later on. */
12222 o->map_head.link_order = NULL;
104d59d1 12223 if (attr_size)
b8a6ced7 12224 attr_section = o;
104d59d1 12225 else
5270eddc 12226 remove_section = TRUE;
104d59d1 12227 }
6e5e9d58
AM
12228 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
12229 {
12230 /* Remove empty group section from linker output. */
5270eddc 12231 remove_section = TRUE;
b8a6ced7 12232 }
5270eddc 12233 if (remove_section)
b8a6ced7 12234 {
6e5e9d58
AM
12235 o->flags |= SEC_EXCLUDE;
12236 bfd_section_list_remove (abfd, o);
12237 abfd->section_count--;
4c6ee646 12238 sections_removed = TRUE;
6e5e9d58 12239 }
104d59d1 12240 }
4c6ee646
AM
12241 if (sections_removed)
12242 _bfd_fix_excluded_sec_syms (abfd, info);
104d59d1 12243
c152c796
AM
12244 /* Count up the number of relocations we will output for each output
12245 section, so that we know the sizes of the reloc sections. We
12246 also figure out some maximum sizes. */
12247 max_contents_size = 0;
12248 max_external_reloc_size = 0;
12249 max_internal_reloc_count = 0;
12250 max_sym_count = 0;
12251 max_sym_shndx_count = 0;
12252 merged = FALSE;
12253 for (o = abfd->sections; o != NULL; o = o->next)
12254 {
12255 struct bfd_elf_section_data *esdo = elf_section_data (o);
12256 o->reloc_count = 0;
12257
8423293d 12258 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
12259 {
12260 unsigned int reloc_count = 0;
9eaff861 12261 unsigned int additional_reloc_count = 0;
c152c796 12262 struct bfd_elf_section_data *esdi = NULL;
c152c796
AM
12263
12264 if (p->type == bfd_section_reloc_link_order
12265 || p->type == bfd_symbol_reloc_link_order)
12266 reloc_count = 1;
12267 else if (p->type == bfd_indirect_link_order)
12268 {
12269 asection *sec;
12270
12271 sec = p->u.indirect.section;
c152c796
AM
12272
12273 /* Mark all sections which are to be included in the
12274 link. This will normally be every section. We need
12275 to do this so that we can identify any sections which
12276 the linker has decided to not include. */
12277 sec->linker_mark = TRUE;
12278
12279 if (sec->flags & SEC_MERGE)
12280 merged = TRUE;
12281
eea6121a
AM
12282 if (sec->rawsize > max_contents_size)
12283 max_contents_size = sec->rawsize;
12284 if (sec->size > max_contents_size)
12285 max_contents_size = sec->size;
c152c796 12286
c152c796
AM
12287 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
12288 && (sec->owner->flags & DYNAMIC) == 0)
12289 {
12290 size_t sym_count;
12291
a961cdd5
AM
12292 /* We are interested in just local symbols, not all
12293 symbols. */
c152c796
AM
12294 if (elf_bad_symtab (sec->owner))
12295 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
12296 / bed->s->sizeof_sym);
12297 else
12298 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
12299
12300 if (sym_count > max_sym_count)
12301 max_sym_count = sym_count;
12302
12303 if (sym_count > max_sym_shndx_count
6a40cf0c 12304 && elf_symtab_shndx_list (sec->owner) != NULL)
c152c796
AM
12305 max_sym_shndx_count = sym_count;
12306
a961cdd5
AM
12307 if (esdo->this_hdr.sh_type == SHT_REL
12308 || esdo->this_hdr.sh_type == SHT_RELA)
12309 /* Some backends use reloc_count in relocation sections
12310 to count particular types of relocs. Of course,
12311 reloc sections themselves can't have relocations. */
12312 ;
12313 else if (emit_relocs)
12314 {
12315 reloc_count = sec->reloc_count;
12316 if (bed->elf_backend_count_additional_relocs)
12317 {
12318 int c;
12319 c = (*bed->elf_backend_count_additional_relocs) (sec);
12320 additional_reloc_count += c;
12321 }
12322 }
12323 else if (bed->elf_backend_count_relocs)
12324 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
12325
12326 esdi = elf_section_data (sec);
12327
c152c796
AM
12328 if ((sec->flags & SEC_RELOC) != 0)
12329 {
d4730f92 12330 size_t ext_size = 0;
c152c796 12331
d4730f92
BS
12332 if (esdi->rel.hdr != NULL)
12333 ext_size = esdi->rel.hdr->sh_size;
12334 if (esdi->rela.hdr != NULL)
12335 ext_size += esdi->rela.hdr->sh_size;
7326c758 12336
c152c796
AM
12337 if (ext_size > max_external_reloc_size)
12338 max_external_reloc_size = ext_size;
12339 if (sec->reloc_count > max_internal_reloc_count)
12340 max_internal_reloc_count = sec->reloc_count;
12341 }
12342 }
12343 }
12344
12345 if (reloc_count == 0)
12346 continue;
12347
9eaff861 12348 reloc_count += additional_reloc_count;
c152c796
AM
12349 o->reloc_count += reloc_count;
12350
0e1862bb 12351 if (p->type == bfd_indirect_link_order && emit_relocs)
c152c796 12352 {
d4730f92 12353 if (esdi->rel.hdr)
9eaff861 12354 {
491d01d3 12355 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
9eaff861
AO
12356 esdo->rel.count += additional_reloc_count;
12357 }
d4730f92 12358 if (esdi->rela.hdr)
9eaff861 12359 {
491d01d3 12360 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
9eaff861
AO
12361 esdo->rela.count += additional_reloc_count;
12362 }
d4730f92
BS
12363 }
12364 else
12365 {
12366 if (o->use_rela_p)
12367 esdo->rela.count += reloc_count;
2c2b4ed4 12368 else
d4730f92 12369 esdo->rel.count += reloc_count;
c152c796 12370 }
c152c796
AM
12371 }
12372
9eaff861 12373 if (o->reloc_count > 0)
c152c796
AM
12374 o->flags |= SEC_RELOC;
12375 else
12376 {
12377 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12378 set it (this is probably a bug) and if it is set
12379 assign_section_numbers will create a reloc section. */
12380 o->flags &=~ SEC_RELOC;
12381 }
12382
12383 /* If the SEC_ALLOC flag is not set, force the section VMA to
12384 zero. This is done in elf_fake_sections as well, but forcing
12385 the VMA to 0 here will ensure that relocs against these
12386 sections are handled correctly. */
12387 if ((o->flags & SEC_ALLOC) == 0
12388 && ! o->user_set_vma)
12389 o->vma = 0;
12390 }
12391
0e1862bb 12392 if (! bfd_link_relocatable (info) && merged)
64f52338 12393 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
c152c796
AM
12394
12395 /* Figure out the file positions for everything but the symbol table
12396 and the relocs. We set symcount to force assign_section_numbers
12397 to create a symbol table. */
ed48ec2e 12398 abfd->symcount = info->strip != strip_all || emit_relocs;
c152c796
AM
12399 BFD_ASSERT (! abfd->output_has_begun);
12400 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12401 goto error_return;
12402
ee75fd95 12403 /* Set sizes, and assign file positions for reloc sections. */
c152c796
AM
12404 for (o = abfd->sections; o != NULL; o = o->next)
12405 {
d4730f92 12406 struct bfd_elf_section_data *esdo = elf_section_data (o);
c152c796
AM
12407 if ((o->flags & SEC_RELOC) != 0)
12408 {
d4730f92 12409 if (esdo->rel.hdr
9eaff861 12410 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
c152c796
AM
12411 goto error_return;
12412
d4730f92 12413 if (esdo->rela.hdr
9eaff861 12414 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
c152c796
AM
12415 goto error_return;
12416 }
12417
48db3297
AM
12418 /* _bfd_elf_compute_section_file_positions makes temporary use
12419 of target_index. Reset it. */
12420 o->target_index = 0;
12421
c152c796
AM
12422 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12423 to count upwards while actually outputting the relocations. */
d4730f92
BS
12424 esdo->rel.count = 0;
12425 esdo->rela.count = 0;
0ce398f1 12426
1ff6de03
NA
12427 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12428 && !bfd_section_is_ctf (o))
0ce398f1
L
12429 {
12430 /* Cache the section contents so that they can be compressed
12431 later. Use bfd_malloc since it will be freed by
12432 bfd_compress_section_contents. */
12433 unsigned char *contents = esdo->this_hdr.contents;
12434 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
12435 abort ();
12436 contents
12437 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12438 if (contents == NULL)
12439 goto error_return;
12440 esdo->this_hdr.contents = contents;
12441 }
c152c796
AM
12442 }
12443
1ff6de03
NA
12444 /* We have now assigned file positions for all the sections except .symtab,
12445 .strtab, and non-loaded reloc and compressed debugging sections. We start
12446 the .symtab section at the current file position, and write directly to it.
12447 We build the .strtab section in memory. */
ed48ec2e 12448 abfd->symcount = 0;
c152c796
AM
12449 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12450 /* sh_name is set in prep_headers. */
12451 symtab_hdr->sh_type = SHT_SYMTAB;
12452 /* sh_flags, sh_addr and sh_size all start off zero. */
12453 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12454 /* sh_link is set in assign_section_numbers. */
12455 /* sh_info is set below. */
12456 /* sh_offset is set just below. */
72de5009 12457 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
c152c796 12458
ef10c3ac
L
12459 if (max_sym_count < 20)
12460 max_sym_count = 20;
64f52338 12461 htab->strtabsize = max_sym_count;
ef10c3ac 12462 amt = max_sym_count * sizeof (struct elf_sym_strtab);
64f52338
AM
12463 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12464 if (htab->strtab == NULL)
c152c796 12465 goto error_return;
ef10c3ac
L
12466 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12467 flinfo.symshndxbuf
12468 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12469 ? (Elf_External_Sym_Shndx *) -1 : NULL);
c152c796 12470
8539e4e8 12471 if (info->strip != strip_all || emit_relocs)
c152c796 12472 {
c77cb2a0
MR
12473 bfd_boolean name_local_sections;
12474 const char *name;
12475
8539e4e8
AM
12476 file_ptr off = elf_next_file_pos (abfd);
12477
12478 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
12479
12480 /* Note that at this point elf_next_file_pos (abfd) is
12481 incorrect. We do not yet know the size of the .symtab section.
12482 We correct next_file_pos below, after we do know the size. */
12483
12484 /* Start writing out the symbol table. The first symbol is always a
12485 dummy symbol. */
c152c796
AM
12486 elfsym.st_value = 0;
12487 elfsym.st_size = 0;
12488 elfsym.st_info = 0;
12489 elfsym.st_other = 0;
12490 elfsym.st_shndx = SHN_UNDEF;
35fc36a8 12491 elfsym.st_target_internal = 0;
ef10c3ac
L
12492 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12493 bfd_und_section_ptr, NULL) != 1)
c152c796 12494 goto error_return;
c152c796 12495
8539e4e8
AM
12496 /* Output a symbol for each section. We output these even if we are
12497 discarding local symbols, since they are used for relocs. These
c77cb2a0
MR
12498 symbols usually have no names. We store the index of each one in
12499 the index field of the section, so that we can find it again when
8539e4e8
AM
12500 outputting relocs. */
12501
c77cb2a0
MR
12502 name_local_sections
12503 = (bed->elf_backend_name_local_section_symbols
12504 && bed->elf_backend_name_local_section_symbols (abfd));
12505
12506 name = NULL;
c152c796
AM
12507 elfsym.st_size = 0;
12508 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12509 elfsym.st_other = 0;
f0b5bb34 12510 elfsym.st_value = 0;
35fc36a8 12511 elfsym.st_target_internal = 0;
c152c796
AM
12512 for (i = 1; i < elf_numsections (abfd); i++)
12513 {
12514 o = bfd_section_from_elf_index (abfd, i);
12515 if (o != NULL)
f0b5bb34
AM
12516 {
12517 o->target_index = bfd_get_symcount (abfd);
12518 elfsym.st_shndx = i;
0e1862bb 12519 if (!bfd_link_relocatable (info))
f0b5bb34 12520 elfsym.st_value = o->vma;
c77cb2a0
MR
12521 if (name_local_sections)
12522 name = o->name;
12523 if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
ef10c3ac 12524 NULL) != 1)
f0b5bb34
AM
12525 goto error_return;
12526 }
c152c796
AM
12527 }
12528 }
12529
3f1b17bb
MR
12530 /* On some targets like Irix 5 the symbol split between local and global
12531 ones recorded in the sh_info field needs to be done between section
12532 and all other symbols. */
12533 if (bed->elf_backend_elfsym_local_is_section
12534 && bed->elf_backend_elfsym_local_is_section (abfd))
12535 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12536
c152c796
AM
12537 /* Allocate some memory to hold information read in from the input
12538 files. */
12539 if (max_contents_size != 0)
12540 {
8b127cbc
AM
12541 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12542 if (flinfo.contents == NULL)
c152c796
AM
12543 goto error_return;
12544 }
12545
12546 if (max_external_reloc_size != 0)
12547 {
8b127cbc
AM
12548 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12549 if (flinfo.external_relocs == NULL)
c152c796
AM
12550 goto error_return;
12551 }
12552
12553 if (max_internal_reloc_count != 0)
12554 {
056bafd4 12555 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
8b127cbc
AM
12556 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12557 if (flinfo.internal_relocs == NULL)
c152c796
AM
12558 goto error_return;
12559 }
12560
12561 if (max_sym_count != 0)
12562 {
12563 amt = max_sym_count * bed->s->sizeof_sym;
8b127cbc
AM
12564 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12565 if (flinfo.external_syms == NULL)
c152c796
AM
12566 goto error_return;
12567
12568 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8b127cbc
AM
12569 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12570 if (flinfo.internal_syms == NULL)
c152c796
AM
12571 goto error_return;
12572
12573 amt = max_sym_count * sizeof (long);
8b127cbc
AM
12574 flinfo.indices = (long int *) bfd_malloc (amt);
12575 if (flinfo.indices == NULL)
c152c796
AM
12576 goto error_return;
12577
12578 amt = max_sym_count * sizeof (asection *);
8b127cbc
AM
12579 flinfo.sections = (asection **) bfd_malloc (amt);
12580 if (flinfo.sections == NULL)
c152c796
AM
12581 goto error_return;
12582 }
12583
12584 if (max_sym_shndx_count != 0)
12585 {
12586 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8b127cbc
AM
12587 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12588 if (flinfo.locsym_shndx == NULL)
c152c796
AM
12589 goto error_return;
12590 }
12591
64f52338 12592 if (htab->tls_sec)
c152c796 12593 {
66631823 12594 bfd_vma base, end = 0; /* Both bytes. */
c152c796
AM
12595 asection *sec;
12596
64f52338 12597 for (sec = htab->tls_sec;
c152c796
AM
12598 sec && (sec->flags & SEC_THREAD_LOCAL);
12599 sec = sec->next)
12600 {
3a800eb9 12601 bfd_size_type size = sec->size;
66631823 12602 unsigned int opb = bfd_octets_per_byte (abfd, sec);
c152c796 12603
3a800eb9
AM
12604 if (size == 0
12605 && (sec->flags & SEC_HAS_CONTENTS) == 0)
c152c796 12606 {
91d6fa6a
NC
12607 struct bfd_link_order *ord = sec->map_tail.link_order;
12608
12609 if (ord != NULL)
66631823 12610 size = ord->offset * opb + ord->size;
c152c796 12611 }
66631823 12612 end = sec->vma + size / opb;
c152c796 12613 }
64f52338 12614 base = htab->tls_sec->vma;
7dc98aea
RO
12615 /* Only align end of TLS section if static TLS doesn't have special
12616 alignment requirements. */
12617 if (bed->static_tls_alignment == 1)
64f52338
AM
12618 end = align_power (end, htab->tls_sec->alignment_power);
12619 htab->tls_size = end - base;
c152c796
AM
12620 }
12621
0b52efa6
PB
12622 /* Reorder SHF_LINK_ORDER sections. */
12623 for (o = abfd->sections; o != NULL; o = o->next)
12624 {
12625 if (!elf_fixup_link_order (abfd, o))
12626 return FALSE;
12627 }
12628
2f0c68f2
CM
12629 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12630 return FALSE;
12631
c152c796
AM
12632 /* Since ELF permits relocations to be against local symbols, we
12633 must have the local symbols available when we do the relocations.
12634 Since we would rather only read the local symbols once, and we
12635 would rather not keep them in memory, we handle all the
12636 relocations for a single input file at the same time.
12637
12638 Unfortunately, there is no way to know the total number of local
12639 symbols until we have seen all of them, and the local symbol
12640 indices precede the global symbol indices. This means that when
12641 we are generating relocatable output, and we see a reloc against
12642 a global symbol, we can not know the symbol index until we have
12643 finished examining all the local symbols to see which ones we are
12644 going to output. To deal with this, we keep the relocations in
12645 memory, and don't output them until the end of the link. This is
12646 an unfortunate waste of memory, but I don't see a good way around
12647 it. Fortunately, it only happens when performing a relocatable
12648 link, which is not the common case. FIXME: If keep_memory is set
12649 we could write the relocs out and then read them again; I don't
12650 know how bad the memory loss will be. */
12651
c72f2fb2 12652 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
12653 sub->output_has_begun = FALSE;
12654 for (o = abfd->sections; o != NULL; o = o->next)
12655 {
8423293d 12656 for (p = o->map_head.link_order; p != NULL; p = p->next)
c152c796
AM
12657 {
12658 if (p->type == bfd_indirect_link_order
12659 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12660 == bfd_target_elf_flavour)
12661 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12662 {
12663 if (! sub->output_has_begun)
12664 {
8b127cbc 12665 if (! elf_link_input_bfd (&flinfo, sub))
c152c796
AM
12666 goto error_return;
12667 sub->output_has_begun = TRUE;
12668 }
12669 }
12670 else if (p->type == bfd_section_reloc_link_order
12671 || p->type == bfd_symbol_reloc_link_order)
12672 {
12673 if (! elf_reloc_link_order (abfd, info, o, p))
12674 goto error_return;
12675 }
12676 else
12677 {
12678 if (! _bfd_default_link_order (abfd, info, o, p))
351f65ca
L
12679 {
12680 if (p->type == bfd_indirect_link_order
12681 && (bfd_get_flavour (sub)
12682 == bfd_target_elf_flavour)
12683 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12684 != bed->s->elfclass))
12685 {
12686 const char *iclass, *oclass;
12687
aebf9be7 12688 switch (bed->s->elfclass)
351f65ca 12689 {
aebf9be7
NC
12690 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12691 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12692 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12693 default: abort ();
351f65ca 12694 }
aebf9be7
NC
12695
12696 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
351f65ca 12697 {
aebf9be7
NC
12698 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12699 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12700 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12701 default: abort ();
351f65ca
L
12702 }
12703
12704 bfd_set_error (bfd_error_wrong_format);
4eca0228 12705 _bfd_error_handler
695344c0 12706 /* xgettext:c-format */
871b3ab2 12707 (_("%pB: file class %s incompatible with %s"),
351f65ca
L
12708 sub, iclass, oclass);
12709 }
12710
12711 goto error_return;
12712 }
c152c796
AM
12713 }
12714 }
12715 }
12716
c0f00686
L
12717 /* Free symbol buffer if needed. */
12718 if (!info->reduce_memory_overheads)
12719 {
c72f2fb2 12720 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c9594989 12721 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
c0f00686
L
12722 {
12723 free (elf_tdata (sub)->symbuf);
12724 elf_tdata (sub)->symbuf = NULL;
12725 }
12726 }
12727
496afd17
L
12728 ret = TRUE;
12729
c152c796
AM
12730 /* Output any global symbols that got converted to local in a
12731 version script or due to symbol visibility. We do this in a
12732 separate step since ELF requires all local symbols to appear
12733 prior to any global symbols. FIXME: We should only do this if
12734 some global symbols were, in fact, converted to become local.
12735 FIXME: Will this work correctly with the Irix 5 linker? */
12736 eoinfo.failed = FALSE;
8b127cbc 12737 eoinfo.flinfo = &flinfo;
c152c796 12738 eoinfo.localsyms = TRUE;
34a79995 12739 eoinfo.file_sym_done = FALSE;
7686d77d 12740 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796 12741 if (eoinfo.failed)
496afd17
L
12742 {
12743 ret = FALSE;
12744 goto return_local_hash_table;
12745 }
c152c796 12746
4e617b1e
PB
12747 /* If backend needs to output some local symbols not present in the hash
12748 table, do it now. */
8539e4e8
AM
12749 if (bed->elf_backend_output_arch_local_syms
12750 && (info->strip != strip_all || emit_relocs))
4e617b1e 12751 {
6e0b88f1 12752 typedef int (*out_sym_func)
4e617b1e
PB
12753 (void *, const char *, Elf_Internal_Sym *, asection *,
12754 struct elf_link_hash_entry *);
12755
12756 if (! ((*bed->elf_backend_output_arch_local_syms)
ef10c3ac
L
12757 (abfd, info, &flinfo,
12758 (out_sym_func) elf_link_output_symstrtab)))
496afd17
L
12759 {
12760 ret = FALSE;
12761 goto return_local_hash_table;
12762 }
4e617b1e
PB
12763 }
12764
c152c796
AM
12765 /* That wrote out all the local symbols. Finish up the symbol table
12766 with the global symbols. Even if we want to strip everything we
12767 can, we still need to deal with those global symbols that got
12768 converted to local in a version script. */
12769
12770 /* The sh_info field records the index of the first non local symbol. */
3f1b17bb
MR
12771 if (!symtab_hdr->sh_info)
12772 symtab_hdr->sh_info = bfd_get_symcount (abfd);
c152c796
AM
12773
12774 if (dynamic
64f52338
AM
12775 && htab->dynsym != NULL
12776 && htab->dynsym->output_section != bfd_abs_section_ptr)
c152c796
AM
12777 {
12778 Elf_Internal_Sym sym;
64f52338 12779 bfd_byte *dynsym = htab->dynsym->contents;
90ac2420 12780
64f52338
AM
12781 o = htab->dynsym->output_section;
12782 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
c152c796
AM
12783
12784 /* Write out the section symbols for the output sections. */
0e1862bb 12785 if (bfd_link_pic (info)
64f52338 12786 || htab->is_relocatable_executable)
c152c796
AM
12787 {
12788 asection *s;
12789
12790 sym.st_size = 0;
12791 sym.st_name = 0;
12792 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12793 sym.st_other = 0;
35fc36a8 12794 sym.st_target_internal = 0;
c152c796
AM
12795
12796 for (s = abfd->sections; s != NULL; s = s->next)
12797 {
12798 int indx;
12799 bfd_byte *dest;
12800 long dynindx;
12801
c152c796 12802 dynindx = elf_section_data (s)->dynindx;
8c37241b
JJ
12803 if (dynindx <= 0)
12804 continue;
12805 indx = elf_section_data (s)->this_idx;
c152c796
AM
12806 BFD_ASSERT (indx > 0);
12807 sym.st_shndx = indx;
c0d5a53d 12808 if (! check_dynsym (abfd, &sym))
496afd17
L
12809 {
12810 ret = FALSE;
12811 goto return_local_hash_table;
12812 }
c152c796
AM
12813 sym.st_value = s->vma;
12814 dest = dynsym + dynindx * bed->s->sizeof_sym;
3d16b64e
NA
12815
12816 /* Inform the linker of the addition of this symbol. */
12817
12818 if (info->callbacks->ctf_new_dynsym)
12819 info->callbacks->ctf_new_dynsym (dynindx, &sym);
12820
c152c796
AM
12821 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12822 }
c152c796
AM
12823 }
12824
12825 /* Write out the local dynsyms. */
64f52338 12826 if (htab->dynlocal)
c152c796
AM
12827 {
12828 struct elf_link_local_dynamic_entry *e;
64f52338 12829 for (e = htab->dynlocal; e ; e = e->next)
c152c796
AM
12830 {
12831 asection *s;
12832 bfd_byte *dest;
12833
935bd1e0 12834 /* Copy the internal symbol and turn off visibility.
c152c796
AM
12835 Note that we saved a word of storage and overwrote
12836 the original st_name with the dynstr_index. */
12837 sym = e->isym;
935bd1e0 12838 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
4d68fd75 12839 sym.st_shndx = SHN_UNDEF;
c152c796 12840
cb33740c
AM
12841 s = bfd_section_from_elf_index (e->input_bfd,
12842 e->isym.st_shndx);
4d68fd75
AM
12843 if (s != NULL
12844 && s->output_section != NULL
12845 && elf_section_data (s->output_section) != NULL)
c152c796 12846 {
c152c796
AM
12847 sym.st_shndx =
12848 elf_section_data (s->output_section)->this_idx;
c0d5a53d 12849 if (! check_dynsym (abfd, &sym))
496afd17
L
12850 {
12851 ret = FALSE;
12852 goto return_local_hash_table;
12853 }
c152c796
AM
12854 sym.st_value = (s->output_section->vma
12855 + s->output_offset
12856 + e->isym.st_value);
12857 }
12858
3d16b64e
NA
12859 /* Inform the linker of the addition of this symbol. */
12860
12861 if (info->callbacks->ctf_new_dynsym)
12862 info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
12863
c152c796
AM
12864 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12865 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12866 }
12867 }
c152c796
AM
12868 }
12869
12870 /* We get the global symbols from the hash table. */
12871 eoinfo.failed = FALSE;
12872 eoinfo.localsyms = FALSE;
8b127cbc 12873 eoinfo.flinfo = &flinfo;
7686d77d 12874 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
c152c796 12875 if (eoinfo.failed)
496afd17
L
12876 {
12877 ret = FALSE;
12878 goto return_local_hash_table;
12879 }
c152c796
AM
12880
12881 /* If backend needs to output some symbols not present in the hash
12882 table, do it now. */
8539e4e8
AM
12883 if (bed->elf_backend_output_arch_syms
12884 && (info->strip != strip_all || emit_relocs))
c152c796 12885 {
6e0b88f1 12886 typedef int (*out_sym_func)
c152c796
AM
12887 (void *, const char *, Elf_Internal_Sym *, asection *,
12888 struct elf_link_hash_entry *);
12889
12890 if (! ((*bed->elf_backend_output_arch_syms)
ef10c3ac
L
12891 (abfd, info, &flinfo,
12892 (out_sym_func) elf_link_output_symstrtab)))
496afd17
L
12893 {
12894 ret = FALSE;
12895 goto return_local_hash_table;
12896 }
c152c796
AM
12897 }
12898
ef10c3ac
L
12899 /* Finalize the .strtab section. */
12900 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12901
12902 /* Swap out the .strtab section. */
12903 if (!elf_link_swap_symbols_out (&flinfo))
496afd17
L
12904 {
12905 ret = FALSE;
12906 goto return_local_hash_table;
12907 }
c152c796
AM
12908
12909 /* Now we know the size of the symtab section. */
c152c796
AM
12910 if (bfd_get_symcount (abfd) > 0)
12911 {
ee3b52e9
L
12912 /* Finish up and write out the symbol string table (.strtab)
12913 section. */
ad32986f 12914 Elf_Internal_Shdr *symstrtab_hdr = NULL;
8539e4e8
AM
12915 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12916
ad32986f 12917 if (elf_symtab_shndx_list (abfd))
8539e4e8 12918 {
ad32986f 12919 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8539e4e8 12920
ad32986f
NC
12921 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12922 {
12923 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12924 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12925 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12926 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12927 symtab_shndx_hdr->sh_size = amt;
8539e4e8 12928
ad32986f
NC
12929 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12930 off, TRUE);
12931
12932 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12933 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
496afd17
L
12934 {
12935 ret = FALSE;
12936 goto return_local_hash_table;
12937 }
ad32986f 12938 }
8539e4e8 12939 }
ee3b52e9
L
12940
12941 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12942 /* sh_name was set in prep_headers. */
12943 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 12944 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
ee3b52e9 12945 symstrtab_hdr->sh_addr = 0;
ef10c3ac 12946 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
ee3b52e9
L
12947 symstrtab_hdr->sh_entsize = 0;
12948 symstrtab_hdr->sh_link = 0;
12949 symstrtab_hdr->sh_info = 0;
12950 /* sh_offset is set just below. */
12951 symstrtab_hdr->sh_addralign = 1;
12952
12953 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12954 off, TRUE);
12955 elf_next_file_pos (abfd) = off;
12956
c152c796 12957 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 12958 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
496afd17
L
12959 {
12960 ret = FALSE;
12961 goto return_local_hash_table;
12962 }
c152c796
AM
12963 }
12964
76359541
TP
12965 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12966 {
871b3ab2 12967 _bfd_error_handler (_("%pB: failed to generate import library"),
4eca0228 12968 info->out_implib_bfd);
496afd17
L
12969 ret = FALSE;
12970 goto return_local_hash_table;
76359541
TP
12971 }
12972
c152c796
AM
12973 /* Adjust the relocs to have the correct symbol indices. */
12974 for (o = abfd->sections; o != NULL; o = o->next)
12975 {
d4730f92 12976 struct bfd_elf_section_data *esdo = elf_section_data (o);
28dbcedc 12977 bfd_boolean sort;
10bbbc1d 12978
c152c796
AM
12979 if ((o->flags & SEC_RELOC) == 0)
12980 continue;
12981
28dbcedc 12982 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
bca6d0e3 12983 if (esdo->rel.hdr != NULL
10bbbc1d 12984 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
496afd17
L
12985 {
12986 ret = FALSE;
12987 goto return_local_hash_table;
12988 }
bca6d0e3 12989 if (esdo->rela.hdr != NULL
10bbbc1d 12990 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
496afd17
L
12991 {
12992 ret = FALSE;
12993 goto return_local_hash_table;
12994 }
c152c796
AM
12995
12996 /* Set the reloc_count field to 0 to prevent write_relocs from
12997 trying to swap the relocs out itself. */
12998 o->reloc_count = 0;
12999 }
13000
13001 if (dynamic && info->combreloc && dynobj != NULL)
13002 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
13003
13004 /* If we are linking against a dynamic object, or generating a
13005 shared library, finish up the dynamic linking information. */
13006 if (dynamic)
13007 {
13008 bfd_byte *dyncon, *dynconend;
13009
13010 /* Fix up .dynamic entries. */
3d4d4302 13011 o = bfd_get_linker_section (dynobj, ".dynamic");
c152c796
AM
13012 BFD_ASSERT (o != NULL);
13013
13014 dyncon = o->contents;
eea6121a 13015 dynconend = o->contents + o->size;
c152c796
AM
13016 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13017 {
13018 Elf_Internal_Dyn dyn;
13019 const char *name;
13020 unsigned int type;
64487780
AM
13021 bfd_size_type sh_size;
13022 bfd_vma sh_addr;
c152c796
AM
13023
13024 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13025
13026 switch (dyn.d_tag)
13027 {
13028 default:
13029 continue;
13030 case DT_NULL:
13031 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
13032 {
13033 switch (elf_section_data (reldyn)->this_hdr.sh_type)
13034 {
13035 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
13036 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
13037 default: continue;
13038 }
13039 dyn.d_un.d_val = relativecount;
13040 relativecount = 0;
13041 break;
13042 }
13043 continue;
13044
13045 case DT_INIT:
13046 name = info->init_function;
13047 goto get_sym;
13048 case DT_FINI:
13049 name = info->fini_function;
13050 get_sym:
13051 {
13052 struct elf_link_hash_entry *h;
13053
64f52338 13054 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
c152c796
AM
13055 if (h != NULL
13056 && (h->root.type == bfd_link_hash_defined
13057 || h->root.type == bfd_link_hash_defweak))
13058 {
bef26483 13059 dyn.d_un.d_ptr = h->root.u.def.value;
c152c796
AM
13060 o = h->root.u.def.section;
13061 if (o->output_section != NULL)
bef26483 13062 dyn.d_un.d_ptr += (o->output_section->vma
c152c796
AM
13063 + o->output_offset);
13064 else
13065 {
13066 /* The symbol is imported from another shared
13067 library and does not apply to this one. */
bef26483 13068 dyn.d_un.d_ptr = 0;
c152c796
AM
13069 }
13070 break;
13071 }
13072 }
13073 continue;
13074
13075 case DT_PREINIT_ARRAYSZ:
13076 name = ".preinit_array";
4ade44b7 13077 goto get_out_size;
c152c796
AM
13078 case DT_INIT_ARRAYSZ:
13079 name = ".init_array";
4ade44b7 13080 goto get_out_size;
c152c796
AM
13081 case DT_FINI_ARRAYSZ:
13082 name = ".fini_array";
4ade44b7 13083 get_out_size:
c152c796
AM
13084 o = bfd_get_section_by_name (abfd, name);
13085 if (o == NULL)
13086 {
4eca0228 13087 _bfd_error_handler
4ade44b7 13088 (_("could not find section %s"), name);
c152c796
AM
13089 goto error_return;
13090 }
eea6121a 13091 if (o->size == 0)
4eca0228 13092 _bfd_error_handler
c152c796 13093 (_("warning: %s section has zero size"), name);
eea6121a 13094 dyn.d_un.d_val = o->size;
c152c796
AM
13095 break;
13096
13097 case DT_PREINIT_ARRAY:
13098 name = ".preinit_array";
4ade44b7 13099 goto get_out_vma;
c152c796
AM
13100 case DT_INIT_ARRAY:
13101 name = ".init_array";
4ade44b7 13102 goto get_out_vma;
c152c796
AM
13103 case DT_FINI_ARRAY:
13104 name = ".fini_array";
4ade44b7
AM
13105 get_out_vma:
13106 o = bfd_get_section_by_name (abfd, name);
13107 goto do_vma;
c152c796
AM
13108
13109 case DT_HASH:
13110 name = ".hash";
13111 goto get_vma;
fdc90cb4
JJ
13112 case DT_GNU_HASH:
13113 name = ".gnu.hash";
13114 goto get_vma;
c152c796
AM
13115 case DT_STRTAB:
13116 name = ".dynstr";
13117 goto get_vma;
13118 case DT_SYMTAB:
13119 name = ".dynsym";
13120 goto get_vma;
13121 case DT_VERDEF:
13122 name = ".gnu.version_d";
13123 goto get_vma;
13124 case DT_VERNEED:
13125 name = ".gnu.version_r";
13126 goto get_vma;
13127 case DT_VERSYM:
13128 name = ".gnu.version";
13129 get_vma:
4ade44b7
AM
13130 o = bfd_get_linker_section (dynobj, name);
13131 do_vma:
b3293efa 13132 if (o == NULL || bfd_is_abs_section (o->output_section))
c152c796 13133 {
4eca0228 13134 _bfd_error_handler
4ade44b7 13135 (_("could not find section %s"), name);
c152c796
AM
13136 goto error_return;
13137 }
894891db
NC
13138 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
13139 {
4eca0228 13140 _bfd_error_handler
894891db
NC
13141 (_("warning: section '%s' is being made into a note"), name);
13142 bfd_set_error (bfd_error_nonrepresentable_section);
13143 goto error_return;
13144 }
4ade44b7 13145 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
c152c796
AM
13146 break;
13147
13148 case DT_REL:
13149 case DT_RELA:
13150 case DT_RELSZ:
13151 case DT_RELASZ:
13152 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
13153 type = SHT_REL;
13154 else
13155 type = SHT_RELA;
64487780
AM
13156 sh_size = 0;
13157 sh_addr = 0;
c152c796
AM
13158 for (i = 1; i < elf_numsections (abfd); i++)
13159 {
13160 Elf_Internal_Shdr *hdr;
13161
13162 hdr = elf_elfsections (abfd)[i];
13163 if (hdr->sh_type == type
13164 && (hdr->sh_flags & SHF_ALLOC) != 0)
13165 {
64487780
AM
13166 sh_size += hdr->sh_size;
13167 if (sh_addr == 0
13168 || sh_addr > hdr->sh_addr)
13169 sh_addr = hdr->sh_addr;
c152c796
AM
13170 }
13171 }
64487780 13172
64f52338
AM
13173 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
13174 {
66631823
CE
13175 unsigned int opb = bfd_octets_per_byte (abfd, o);
13176
64f52338
AM
13177 /* Don't count procedure linkage table relocs in the
13178 overall reloc count. */
64487780
AM
13179 sh_size -= htab->srelplt->size;
13180 if (sh_size == 0)
13181 /* If the size is zero, make the address zero too.
13182 This is to avoid a glibc bug. If the backend
13183 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
13184 zero, then we'll put DT_RELA at the end of
13185 DT_JMPREL. glibc will interpret the end of
13186 DT_RELA matching the end of DT_JMPREL as the
13187 case where DT_RELA includes DT_JMPREL, and for
13188 LD_BIND_NOW will decide that processing DT_RELA
13189 will process the PLT relocs too. Net result:
13190 No PLT relocs applied. */
13191 sh_addr = 0;
13192
64f52338
AM
13193 /* If .rela.plt is the first .rela section, exclude
13194 it from DT_RELA. */
64487780 13195 else if (sh_addr == (htab->srelplt->output_section->vma
66631823 13196 + htab->srelplt->output_offset) * opb)
64487780 13197 sh_addr += htab->srelplt->size;
64f52338 13198 }
64487780
AM
13199
13200 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
13201 dyn.d_un.d_val = sh_size;
13202 else
13203 dyn.d_un.d_ptr = sh_addr;
c152c796
AM
13204 break;
13205 }
13206 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
13207 }
13208 }
13209
13210 /* If we have created any dynamic sections, then output them. */
13211 if (dynobj != NULL)
13212 {
13213 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
13214 goto error_return;
13215
943284cc 13216 /* Check for DT_TEXTREL (late, in case the backend removes it). */
a6dbf402 13217 if (bfd_link_textrel_check (info)
3d4d4302 13218 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
943284cc
DJ
13219 {
13220 bfd_byte *dyncon, *dynconend;
13221
943284cc
DJ
13222 dyncon = o->contents;
13223 dynconend = o->contents + o->size;
13224 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
13225 {
13226 Elf_Internal_Dyn dyn;
13227
13228 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
13229
13230 if (dyn.d_tag == DT_TEXTREL)
13231 {
a6dbf402 13232 if (info->textrel_check == textrel_check_error)
c192a133 13233 info->callbacks->einfo
9793eb77 13234 (_("%P%X: read-only segment has dynamic relocations\n"));
a6dbf402
L
13235 else if (bfd_link_dll (info))
13236 info->callbacks->einfo
13237 (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
c192a133
AM
13238 else
13239 info->callbacks->einfo
a6dbf402 13240 (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
943284cc
DJ
13241 break;
13242 }
13243 }
13244 }
13245
c152c796
AM
13246 for (o = dynobj->sections; o != NULL; o = o->next)
13247 {
13248 if ((o->flags & SEC_HAS_CONTENTS) == 0
eea6121a 13249 || o->size == 0
c152c796
AM
13250 || o->output_section == bfd_abs_section_ptr)
13251 continue;
13252 if ((o->flags & SEC_LINKER_CREATED) == 0)
13253 {
13254 /* At this point, we are only interested in sections
13255 created by _bfd_elf_link_create_dynamic_sections. */
13256 continue;
13257 }
64f52338 13258 if (htab->stab_info.stabstr == o)
3722b82f 13259 continue;
64f52338 13260 if (htab->eh_info.hdr_sec == o)
eea6121a 13261 continue;
3d4d4302 13262 if (strcmp (o->name, ".dynstr") != 0)
c152c796 13263 {
bb294208
AM
13264 bfd_size_type octets = ((file_ptr) o->output_offset
13265 * bfd_octets_per_byte (abfd, o));
13266 if (!bfd_set_section_contents (abfd, o->output_section,
13267 o->contents, octets, o->size))
c152c796
AM
13268 goto error_return;
13269 }
13270 else
13271 {
13272 /* The contents of the .dynstr section are actually in a
13273 stringtab. */
8539e4e8
AM
13274 file_ptr off;
13275
c152c796
AM
13276 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
13277 if (bfd_seek (abfd, off, SEEK_SET) != 0
64f52338 13278 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
c152c796
AM
13279 goto error_return;
13280 }
13281 }
13282 }
13283
7bdf4127 13284 if (!info->resolve_section_groups)
c152c796
AM
13285 {
13286 bfd_boolean failed = FALSE;
13287
7bdf4127 13288 BFD_ASSERT (bfd_link_relocatable (info));
c152c796
AM
13289 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
13290 if (failed)
13291 goto error_return;
13292 }
13293
13294 /* If we have optimized stabs strings, output them. */
64f52338 13295 if (htab->stab_info.stabstr != NULL)
c152c796 13296 {
64f52338 13297 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
c152c796
AM
13298 goto error_return;
13299 }
13300
9f7c3e5e
AM
13301 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
13302 goto error_return;
c152c796 13303
1ff6de03
NA
13304 if (info->callbacks->emit_ctf)
13305 info->callbacks->emit_ctf ();
13306
9f7c3e5e 13307 elf_final_link_free (abfd, &flinfo);
c152c796 13308
104d59d1
JM
13309 if (attr_section)
13310 {
a50b1753 13311 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
104d59d1 13312 if (contents == NULL)
496afd17
L
13313 {
13314 /* Bail out and fail. */
13315 ret = FALSE;
13316 goto return_local_hash_table;
13317 }
104d59d1
JM
13318 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
13319 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
13320 free (contents);
13321 }
13322
496afd17
L
13323 return_local_hash_table:
13324 if (info->unique_symbol)
13325 bfd_hash_table_free (&flinfo.local_hash_table);
13326 return ret;
c152c796
AM
13327
13328 error_return:
9f7c3e5e 13329 elf_final_link_free (abfd, &flinfo);
496afd17
L
13330 ret = FALSE;
13331 goto return_local_hash_table;
c152c796
AM
13332}
13333\f
5241d853
RS
13334/* Initialize COOKIE for input bfd ABFD. */
13335
13336static bfd_boolean
13337init_reloc_cookie (struct elf_reloc_cookie *cookie,
13338 struct bfd_link_info *info, bfd *abfd)
13339{
13340 Elf_Internal_Shdr *symtab_hdr;
13341 const struct elf_backend_data *bed;
13342
13343 bed = get_elf_backend_data (abfd);
13344 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13345
13346 cookie->abfd = abfd;
13347 cookie->sym_hashes = elf_sym_hashes (abfd);
13348 cookie->bad_symtab = elf_bad_symtab (abfd);
13349 if (cookie->bad_symtab)
13350 {
13351 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13352 cookie->extsymoff = 0;
13353 }
13354 else
13355 {
13356 cookie->locsymcount = symtab_hdr->sh_info;
13357 cookie->extsymoff = symtab_hdr->sh_info;
13358 }
13359
13360 if (bed->s->arch_size == 32)
13361 cookie->r_sym_shift = 8;
13362 else
13363 cookie->r_sym_shift = 32;
13364
13365 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
13366 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
13367 {
13368 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13369 cookie->locsymcount, 0,
13370 NULL, NULL, NULL);
13371 if (cookie->locsyms == NULL)
13372 {
13373 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
13374 return FALSE;
13375 }
13376 if (info->keep_memory)
13377 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
13378 }
13379 return TRUE;
13380}
13381
13382/* Free the memory allocated by init_reloc_cookie, if appropriate. */
13383
13384static void
13385fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
13386{
13387 Elf_Internal_Shdr *symtab_hdr;
13388
13389 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
c9594989 13390 if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
5241d853
RS
13391 free (cookie->locsyms);
13392}
13393
13394/* Initialize the relocation information in COOKIE for input section SEC
13395 of input bfd ABFD. */
13396
13397static bfd_boolean
13398init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13399 struct bfd_link_info *info, bfd *abfd,
13400 asection *sec)
13401{
5241d853
RS
13402 if (sec->reloc_count == 0)
13403 {
13404 cookie->rels = NULL;
13405 cookie->relend = NULL;
13406 }
13407 else
13408 {
5241d853
RS
13409 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
13410 info->keep_memory);
13411 if (cookie->rels == NULL)
13412 return FALSE;
13413 cookie->rel = cookie->rels;
056bafd4 13414 cookie->relend = cookie->rels + sec->reloc_count;
5241d853
RS
13415 }
13416 cookie->rel = cookie->rels;
13417 return TRUE;
13418}
13419
13420/* Free the memory allocated by init_reloc_cookie_rels,
13421 if appropriate. */
13422
13423static void
13424fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
13425 asection *sec)
13426{
c9594989 13427 if (elf_section_data (sec)->relocs != cookie->rels)
5241d853
RS
13428 free (cookie->rels);
13429}
13430
13431/* Initialize the whole of COOKIE for input section SEC. */
13432
13433static bfd_boolean
13434init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13435 struct bfd_link_info *info,
13436 asection *sec)
13437{
13438 if (!init_reloc_cookie (cookie, info, sec->owner))
13439 goto error1;
13440 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
13441 goto error2;
13442 return TRUE;
13443
13444 error2:
13445 fini_reloc_cookie (cookie, sec->owner);
13446 error1:
13447 return FALSE;
13448}
13449
13450/* Free the memory allocated by init_reloc_cookie_for_section,
13451 if appropriate. */
13452
13453static void
13454fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13455 asection *sec)
13456{
13457 fini_reloc_cookie_rels (cookie, sec);
13458 fini_reloc_cookie (cookie, sec->owner);
13459}
13460\f
c152c796
AM
13461/* Garbage collect unused sections. */
13462
07adf181
AM
13463/* Default gc_mark_hook. */
13464
13465asection *
13466_bfd_elf_gc_mark_hook (asection *sec,
13467 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13468 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13469 struct elf_link_hash_entry *h,
13470 Elf_Internal_Sym *sym)
13471{
13472 if (h != NULL)
13473 {
13474 switch (h->root.type)
13475 {
13476 case bfd_link_hash_defined:
13477 case bfd_link_hash_defweak:
13478 return h->root.u.def.section;
13479
13480 case bfd_link_hash_common:
13481 return h->root.u.c.p->section;
13482
13483 default:
13484 break;
13485 }
13486 }
13487 else
13488 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13489
13490 return NULL;
13491}
13492
9e223787 13493/* Return the debug definition section. */
b7c871ed
L
13494
13495static asection *
13496elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13497 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13498 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13499 struct elf_link_hash_entry *h,
9e223787 13500 Elf_Internal_Sym *sym)
b7c871ed 13501{
9e223787
L
13502 if (h != NULL)
13503 {
13504 /* Return the global debug definition section. */
13505 if ((h->root.type == bfd_link_hash_defined
13506 || h->root.type == bfd_link_hash_defweak)
13507 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13508 return h->root.u.def.section;
13509 }
13510 else
13511 {
13512 /* Return the local debug definition section. */
13513 asection *isec = bfd_section_from_elf_index (sec->owner,
13514 sym->st_shndx);
13515 if ((isec->flags & SEC_DEBUGGING) != 0)
13516 return isec;
13517 }
b7c871ed
L
13518
13519 return NULL;
13520}
13521
5241d853
RS
13522/* COOKIE->rel describes a relocation against section SEC, which is
13523 a section we've decided to keep. Return the section that contains
13524 the relocation symbol, or NULL if no section contains it. */
13525
13526asection *
13527_bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13528 elf_gc_mark_hook_fn gc_mark_hook,
1cce69b9
AM
13529 struct elf_reloc_cookie *cookie,
13530 bfd_boolean *start_stop)
5241d853
RS
13531{
13532 unsigned long r_symndx;
3024a17a 13533 struct elf_link_hash_entry *h, *hw;
5241d853
RS
13534
13535 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
cf35638d 13536 if (r_symndx == STN_UNDEF)
5241d853
RS
13537 return NULL;
13538
13539 if (r_symndx >= cookie->locsymcount
13540 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13541 {
13542 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
263ddf68
L
13543 if (h == NULL)
13544 {
871b3ab2 13545 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
263ddf68
L
13546 sec->owner);
13547 return NULL;
13548 }
5241d853
RS
13549 while (h->root.type == bfd_link_hash_indirect
13550 || h->root.type == bfd_link_hash_warning)
13551 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1d5316ab 13552 h->mark = 1;
3024a17a
AM
13553 /* Keep all aliases of the symbol too. If an object symbol
13554 needs to be copied into .dynbss then all of its aliases
13555 should be present as dynamic symbols, not just the one used
13556 on the copy relocation. */
13557 hw = h;
13558 while (hw->is_weakalias)
13559 {
13560 hw = hw->u.alias;
13561 hw->mark = 1;
13562 }
1cce69b9 13563
a6a4679f 13564 if (start_stop != NULL)
1cce69b9 13565 {
7dba9362
AM
13566 /* To work around a glibc bug, mark XXX input sections
13567 when there is a reference to __start_XXX or __stop_XXX
13568 symbols. */
cbd0eecf 13569 if (h->start_stop)
1cce69b9 13570 {
cbd0eecf 13571 asection *s = h->u2.start_stop_section;
a6a4679f
AM
13572 *start_stop = !s->gc_mark;
13573 return s;
1cce69b9
AM
13574 }
13575 }
13576
5241d853
RS
13577 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13578 }
13579
13580 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13581 &cookie->locsyms[r_symndx]);
13582}
13583
13584/* COOKIE->rel describes a relocation against section SEC, which is
13585 a section we've decided to keep. Mark the section that contains
9d0a14d3 13586 the relocation symbol. */
5241d853
RS
13587
13588bfd_boolean
13589_bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13590 asection *sec,
13591 elf_gc_mark_hook_fn gc_mark_hook,
9d0a14d3 13592 struct elf_reloc_cookie *cookie)
5241d853
RS
13593{
13594 asection *rsec;
1cce69b9 13595 bfd_boolean start_stop = FALSE;
5241d853 13596
1cce69b9
AM
13597 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13598 while (rsec != NULL)
5241d853 13599 {
1cce69b9
AM
13600 if (!rsec->gc_mark)
13601 {
13602 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13603 || (rsec->owner->flags & DYNAMIC) != 0)
13604 rsec->gc_mark = 1;
13605 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13606 return FALSE;
13607 }
13608 if (!start_stop)
13609 break;
199af150 13610 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
5241d853
RS
13611 }
13612 return TRUE;
13613}
13614
07adf181
AM
13615/* The mark phase of garbage collection. For a given section, mark
13616 it and any sections in this section's group, and all the sections
13617 which define symbols to which it refers. */
13618
ccfa59ea
AM
13619bfd_boolean
13620_bfd_elf_gc_mark (struct bfd_link_info *info,
13621 asection *sec,
6a5bb875 13622 elf_gc_mark_hook_fn gc_mark_hook)
c152c796
AM
13623{
13624 bfd_boolean ret;
9d0a14d3 13625 asection *group_sec, *eh_frame;
c152c796
AM
13626
13627 sec->gc_mark = 1;
13628
13629 /* Mark all the sections in the group. */
13630 group_sec = elf_section_data (sec)->next_in_group;
13631 if (group_sec && !group_sec->gc_mark)
ccfa59ea 13632 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
c152c796
AM
13633 return FALSE;
13634
13635 /* Look through the section relocs. */
13636 ret = TRUE;
9d0a14d3
RS
13637 eh_frame = elf_eh_frame_section (sec->owner);
13638 if ((sec->flags & SEC_RELOC) != 0
13639 && sec->reloc_count > 0
13640 && sec != eh_frame)
c152c796 13641 {
5241d853 13642 struct elf_reloc_cookie cookie;
c152c796 13643
5241d853
RS
13644 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13645 ret = FALSE;
c152c796 13646 else
c152c796 13647 {
5241d853 13648 for (; cookie.rel < cookie.relend; cookie.rel++)
9d0a14d3 13649 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
5241d853
RS
13650 {
13651 ret = FALSE;
13652 break;
13653 }
13654 fini_reloc_cookie_for_section (&cookie, sec);
c152c796
AM
13655 }
13656 }
9d0a14d3
RS
13657
13658 if (ret && eh_frame && elf_fde_list (sec))
13659 {
13660 struct elf_reloc_cookie cookie;
13661
13662 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13663 ret = FALSE;
13664 else
13665 {
13666 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13667 gc_mark_hook, &cookie))
13668 ret = FALSE;
13669 fini_reloc_cookie_for_section (&cookie, eh_frame);
13670 }
13671 }
13672
2f0c68f2
CM
13673 eh_frame = elf_section_eh_frame_entry (sec);
13674 if (ret && eh_frame && !eh_frame->gc_mark)
13675 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13676 ret = FALSE;
13677
c152c796
AM
13678 return ret;
13679}
13680
3c758495
TG
13681/* Scan and mark sections in a special or debug section group. */
13682
13683static void
13684_bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13685{
13686 /* Point to first section of section group. */
13687 asection *ssec;
13688 /* Used to iterate the section group. */
13689 asection *msec;
13690
13691 bfd_boolean is_special_grp = TRUE;
13692 bfd_boolean is_debug_grp = TRUE;
13693
13694 /* First scan to see if group contains any section other than debug
13695 and special section. */
13696 ssec = msec = elf_next_in_group (grp);
13697 do
13698 {
13699 if ((msec->flags & SEC_DEBUGGING) == 0)
13700 is_debug_grp = FALSE;
13701
13702 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13703 is_special_grp = FALSE;
13704
13705 msec = elf_next_in_group (msec);
13706 }
13707 while (msec != ssec);
13708
13709 /* If this is a pure debug section group or pure special section group,
13710 keep all sections in this group. */
13711 if (is_debug_grp || is_special_grp)
13712 {
13713 do
13714 {
13715 msec->gc_mark = 1;
13716 msec = elf_next_in_group (msec);
13717 }
13718 while (msec != ssec);
13719 }
13720}
13721
7f6ab9f8
AM
13722/* Keep debug and special sections. */
13723
13724bfd_boolean
13725_bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
b7d07216 13726 elf_gc_mark_hook_fn mark_hook)
7f6ab9f8
AM
13727{
13728 bfd *ibfd;
13729
c72f2fb2 13730 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7f6ab9f8
AM
13731 {
13732 asection *isec;
13733 bfd_boolean some_kept;
b40bf0a2 13734 bfd_boolean debug_frag_seen;
b7c871ed 13735 bfd_boolean has_kept_debug_info;
7f6ab9f8
AM
13736
13737 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13738 continue;
57963c05
AM
13739 isec = ibfd->sections;
13740 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13741 continue;
7f6ab9f8 13742
b40bf0a2
NC
13743 /* Ensure all linker created sections are kept,
13744 see if any other section is already marked,
13745 and note if we have any fragmented debug sections. */
b7c871ed 13746 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
7f6ab9f8
AM
13747 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13748 {
13749 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13750 isec->gc_mark = 1;
eb026f09
AM
13751 else if (isec->gc_mark
13752 && (isec->flags & SEC_ALLOC) != 0
13753 && elf_section_type (isec) != SHT_NOTE)
7f6ab9f8 13754 some_kept = TRUE;
b7d07216
L
13755 else
13756 {
13757 /* Since all sections, except for backend specific ones,
13758 have been garbage collected, call mark_hook on this
13759 section if any of its linked-to sections is marked. */
13760 asection *linked_to_sec = elf_linked_to_section (isec);
13761 for (; linked_to_sec != NULL;
13762 linked_to_sec = elf_linked_to_section (linked_to_sec))
13763 if (linked_to_sec->gc_mark)
13764 {
13765 if (!_bfd_elf_gc_mark (info, isec, mark_hook))
13766 return FALSE;
13767 break;
13768 }
13769 }
b40bf0a2 13770
535b785f 13771 if (!debug_frag_seen
b40bf0a2
NC
13772 && (isec->flags & SEC_DEBUGGING)
13773 && CONST_STRNEQ (isec->name, ".debug_line."))
13774 debug_frag_seen = TRUE;
5242a0a0
L
13775 else if (strcmp (bfd_section_name (isec),
13776 "__patchable_function_entries") == 0
13777 && elf_linked_to_section (isec) == NULL)
13778 info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
13779 "need linked-to section "
13780 "for --gc-sections\n"),
13781 isec->owner, isec);
7f6ab9f8
AM
13782 }
13783
eb026f09
AM
13784 /* If no non-note alloc section in this file will be kept, then
13785 we can toss out the debug and special sections. */
7f6ab9f8
AM
13786 if (!some_kept)
13787 continue;
13788
13789 /* Keep debug and special sections like .comment when they are
3c758495 13790 not part of a group. Also keep section groups that contain
b7d07216
L
13791 just debug sections or special sections. NB: Sections with
13792 linked-to section has been handled above. */
7f6ab9f8 13793 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3c758495
TG
13794 {
13795 if ((isec->flags & SEC_GROUP) != 0)
13796 _bfd_elf_gc_mark_debug_special_section_group (isec);
13797 else if (((isec->flags & SEC_DEBUGGING) != 0
13798 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
b7d07216
L
13799 && elf_next_in_group (isec) == NULL
13800 && elf_linked_to_section (isec) == NULL)
3c758495 13801 isec->gc_mark = 1;
b7c871ed
L
13802 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13803 has_kept_debug_info = TRUE;
3c758495 13804 }
b40bf0a2 13805
b40bf0a2
NC
13806 /* Look for CODE sections which are going to be discarded,
13807 and find and discard any fragmented debug sections which
13808 are associated with that code section. */
b7c871ed
L
13809 if (debug_frag_seen)
13810 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13811 if ((isec->flags & SEC_CODE) != 0
13812 && isec->gc_mark == 0)
13813 {
13814 unsigned int ilen;
13815 asection *dsec;
b40bf0a2 13816
b7c871ed 13817 ilen = strlen (isec->name);
b40bf0a2 13818
b7c871ed 13819 /* Association is determined by the name of the debug
07d6d2b8 13820 section containing the name of the code section as
b7c871ed
L
13821 a suffix. For example .debug_line.text.foo is a
13822 debug section associated with .text.foo. */
13823 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13824 {
13825 unsigned int dlen;
b40bf0a2 13826
b7c871ed
L
13827 if (dsec->gc_mark == 0
13828 || (dsec->flags & SEC_DEBUGGING) == 0)
13829 continue;
b40bf0a2 13830
b7c871ed 13831 dlen = strlen (dsec->name);
b40bf0a2 13832
b7c871ed
L
13833 if (dlen > ilen
13834 && strncmp (dsec->name + (dlen - ilen),
13835 isec->name, ilen) == 0)
b40bf0a2 13836 dsec->gc_mark = 0;
b7c871ed 13837 }
b40bf0a2 13838 }
b7c871ed
L
13839
13840 /* Mark debug sections referenced by kept debug sections. */
13841 if (has_kept_debug_info)
13842 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13843 if (isec->gc_mark
13844 && (isec->flags & SEC_DEBUGGING) != 0)
13845 if (!_bfd_elf_gc_mark (info, isec,
13846 elf_gc_mark_debug_section))
13847 return FALSE;
7f6ab9f8
AM
13848 }
13849 return TRUE;
13850}
13851
c152c796 13852static bfd_boolean
ccabcbe5 13853elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
c152c796
AM
13854{
13855 bfd *sub;
ccabcbe5 13856 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
c152c796 13857
c72f2fb2 13858 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
13859 {
13860 asection *o;
13861
b19a8f85 13862 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 13863 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
b19a8f85 13864 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796 13865 continue;
57963c05
AM
13866 o = sub->sections;
13867 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13868 continue;
c152c796
AM
13869
13870 for (o = sub->sections; o != NULL; o = o->next)
13871 {
a33dafc3
L
13872 /* When any section in a section group is kept, we keep all
13873 sections in the section group. If the first member of
13874 the section group is excluded, we will also exclude the
13875 group section. */
13876 if (o->flags & SEC_GROUP)
13877 {
13878 asection *first = elf_next_in_group (o);
13879 o->gc_mark = first->gc_mark;
13880 }
c152c796 13881
1e7eae0d 13882 if (o->gc_mark)
c152c796
AM
13883 continue;
13884
13885 /* Skip sweeping sections already excluded. */
13886 if (o->flags & SEC_EXCLUDE)
13887 continue;
13888
13889 /* Since this is early in the link process, it is simple
13890 to remove a section from the output. */
13891 o->flags |= SEC_EXCLUDE;
13892
c55fe096 13893 if (info->print_gc_sections && o->size != 0)
695344c0 13894 /* xgettext:c-format */
9793eb77 13895 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
c08bb8dd 13896 o, sub);
c152c796
AM
13897 }
13898 }
13899
c152c796
AM
13900 return TRUE;
13901}
13902
13903/* Propagate collected vtable information. This is called through
13904 elf_link_hash_traverse. */
13905
13906static bfd_boolean
13907elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13908{
c152c796 13909 /* Those that are not vtables. */
cbd0eecf
L
13910 if (h->start_stop
13911 || h->u2.vtable == NULL
13912 || h->u2.vtable->parent == NULL)
c152c796
AM
13913 return TRUE;
13914
13915 /* Those vtables that do not have parents, we cannot merge. */
cbd0eecf 13916 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
c152c796
AM
13917 return TRUE;
13918
13919 /* If we've already been done, exit. */
cbd0eecf 13920 if (h->u2.vtable->used && h->u2.vtable->used[-1])
c152c796
AM
13921 return TRUE;
13922
13923 /* Make sure the parent's table is up to date. */
cbd0eecf 13924 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
c152c796 13925
cbd0eecf 13926 if (h->u2.vtable->used == NULL)
c152c796
AM
13927 {
13928 /* None of this table's entries were referenced. Re-use the
13929 parent's table. */
cbd0eecf
L
13930 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13931 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
c152c796
AM
13932 }
13933 else
13934 {
13935 size_t n;
13936 bfd_boolean *cu, *pu;
13937
13938 /* Or the parent's entries into ours. */
cbd0eecf 13939 cu = h->u2.vtable->used;
c152c796 13940 cu[-1] = TRUE;
cbd0eecf 13941 pu = h->u2.vtable->parent->u2.vtable->used;
c152c796
AM
13942 if (pu != NULL)
13943 {
13944 const struct elf_backend_data *bed;
13945 unsigned int log_file_align;
13946
13947 bed = get_elf_backend_data (h->root.u.def.section->owner);
13948 log_file_align = bed->s->log_file_align;
cbd0eecf 13949 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
c152c796
AM
13950 while (n--)
13951 {
13952 if (*pu)
13953 *cu = TRUE;
13954 pu++;
13955 cu++;
13956 }
13957 }
13958 }
13959
13960 return TRUE;
13961}
13962
13963static bfd_boolean
13964elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13965{
13966 asection *sec;
13967 bfd_vma hstart, hend;
13968 Elf_Internal_Rela *relstart, *relend, *rel;
13969 const struct elf_backend_data *bed;
13970 unsigned int log_file_align;
13971
c152c796
AM
13972 /* Take care of both those symbols that do not describe vtables as
13973 well as those that are not loaded. */
cbd0eecf
L
13974 if (h->start_stop
13975 || h->u2.vtable == NULL
13976 || h->u2.vtable->parent == NULL)
c152c796
AM
13977 return TRUE;
13978
13979 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13980 || h->root.type == bfd_link_hash_defweak);
13981
13982 sec = h->root.u.def.section;
13983 hstart = h->root.u.def.value;
13984 hend = hstart + h->size;
13985
13986 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13987 if (!relstart)
13988 return *(bfd_boolean *) okp = FALSE;
13989 bed = get_elf_backend_data (sec->owner);
13990 log_file_align = bed->s->log_file_align;
13991
056bafd4 13992 relend = relstart + sec->reloc_count;
c152c796
AM
13993
13994 for (rel = relstart; rel < relend; ++rel)
13995 if (rel->r_offset >= hstart && rel->r_offset < hend)
13996 {
13997 /* If the entry is in use, do nothing. */
cbd0eecf
L
13998 if (h->u2.vtable->used
13999 && (rel->r_offset - hstart) < h->u2.vtable->size)
c152c796
AM
14000 {
14001 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
cbd0eecf 14002 if (h->u2.vtable->used[entry])
c152c796
AM
14003 continue;
14004 }
14005 /* Otherwise, kill it. */
14006 rel->r_offset = rel->r_info = rel->r_addend = 0;
14007 }
14008
14009 return TRUE;
14010}
14011
87538722
AM
14012/* Mark sections containing dynamically referenced symbols. When
14013 building shared libraries, we must assume that any visible symbol is
14014 referenced. */
715df9b8 14015
64d03ab5
AM
14016bfd_boolean
14017bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
715df9b8 14018{
87538722 14019 struct bfd_link_info *info = (struct bfd_link_info *) inf;
d6f6f455 14020 struct bfd_elf_dynamic_list *d = info->dynamic_list;
87538722 14021
715df9b8
EB
14022 if ((h->root.type == bfd_link_hash_defined
14023 || h->root.type == bfd_link_hash_defweak)
d664fd41 14024 && ((h->ref_dynamic && !h->forced_local)
c4621b33 14025 || ((h->def_regular || ELF_COMMON_DEF_P (h))
87538722 14026 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
fd91d419 14027 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
0e1862bb 14028 && (!bfd_link_executable (info)
22185505 14029 || info->gc_keep_exported
b407645f
AM
14030 || info->export_dynamic
14031 || (h->dynamic
14032 && d != NULL
14033 && (*d->match) (&d->head, NULL, h->root.root.string)))
422f1182 14034 && (h->versioned >= versioned
54e8959c
L
14035 || !bfd_hide_sym_by_version (info->version_info,
14036 h->root.root.string)))))
715df9b8
EB
14037 h->root.u.def.section->flags |= SEC_KEEP;
14038
14039 return TRUE;
14040}
3b36f7e6 14041
74f0fb50
AM
14042/* Keep all sections containing symbols undefined on the command-line,
14043 and the section containing the entry symbol. */
14044
14045void
14046_bfd_elf_gc_keep (struct bfd_link_info *info)
14047{
14048 struct bfd_sym_chain *sym;
14049
14050 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
14051 {
14052 struct elf_link_hash_entry *h;
14053
14054 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
14055 FALSE, FALSE, FALSE);
14056
14057 if (h != NULL
14058 && (h->root.type == bfd_link_hash_defined
14059 || h->root.type == bfd_link_hash_defweak)
2f5541f3 14060 && !bfd_is_const_section (h->root.u.def.section))
74f0fb50
AM
14061 h->root.u.def.section->flags |= SEC_KEEP;
14062 }
14063}
14064
2f0c68f2
CM
14065bfd_boolean
14066bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
14067 struct bfd_link_info *info)
14068{
14069 bfd *ibfd = info->input_bfds;
14070
14071 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14072 {
14073 asection *sec;
14074 struct elf_reloc_cookie cookie;
14075
14076 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
14077 continue;
57963c05
AM
14078 sec = ibfd->sections;
14079 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14080 continue;
2f0c68f2
CM
14081
14082 if (!init_reloc_cookie (&cookie, info, ibfd))
14083 return FALSE;
14084
14085 for (sec = ibfd->sections; sec; sec = sec->next)
14086 {
fd361982 14087 if (CONST_STRNEQ (bfd_section_name (sec), ".eh_frame_entry")
2f0c68f2
CM
14088 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
14089 {
14090 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
14091 fini_reloc_cookie_rels (&cookie, sec);
14092 }
14093 }
14094 }
14095 return TRUE;
14096}
14097
c152c796
AM
14098/* Do mark and sweep of unused sections. */
14099
14100bfd_boolean
14101bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
14102{
14103 bfd_boolean ok = TRUE;
14104 bfd *sub;
6a5bb875 14105 elf_gc_mark_hook_fn gc_mark_hook;
64d03ab5 14106 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
da44f4e5 14107 struct elf_link_hash_table *htab;
c152c796 14108
64d03ab5 14109 if (!bed->can_gc_sections
715df9b8 14110 || !is_elf_hash_table (info->hash))
c152c796 14111 {
9793eb77 14112 _bfd_error_handler(_("warning: gc-sections option ignored"));
c152c796
AM
14113 return TRUE;
14114 }
14115
74f0fb50 14116 bed->gc_keep (info);
da44f4e5 14117 htab = elf_hash_table (info);
74f0fb50 14118
9d0a14d3
RS
14119 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
14120 at the .eh_frame section if we can mark the FDEs individually. */
2f0c68f2
CM
14121 for (sub = info->input_bfds;
14122 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
14123 sub = sub->link.next)
9d0a14d3
RS
14124 {
14125 asection *sec;
14126 struct elf_reloc_cookie cookie;
14127
57963c05
AM
14128 sec = sub->sections;
14129 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14130 continue;
9d0a14d3 14131 sec = bfd_get_section_by_name (sub, ".eh_frame");
9a2a56cc 14132 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
9d0a14d3
RS
14133 {
14134 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
9a2a56cc
AM
14135 if (elf_section_data (sec)->sec_info
14136 && (sec->flags & SEC_LINKER_CREATED) == 0)
9d0a14d3
RS
14137 elf_eh_frame_section (sub) = sec;
14138 fini_reloc_cookie_for_section (&cookie, sec);
199af150 14139 sec = bfd_get_next_section_by_name (NULL, sec);
9d0a14d3
RS
14140 }
14141 }
9d0a14d3 14142
c152c796 14143 /* Apply transitive closure to the vtable entry usage info. */
da44f4e5 14144 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
c152c796
AM
14145 if (!ok)
14146 return FALSE;
14147
14148 /* Kill the vtable relocations that were not used. */
da44f4e5 14149 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
c152c796
AM
14150 if (!ok)
14151 return FALSE;
14152
715df9b8 14153 /* Mark dynamically referenced symbols. */
22185505 14154 if (htab->dynamic_sections_created || info->gc_keep_exported)
da44f4e5 14155 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
c152c796 14156
715df9b8 14157 /* Grovel through relocs to find out who stays ... */
64d03ab5 14158 gc_mark_hook = bed->gc_mark_hook;
c72f2fb2 14159 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
c152c796
AM
14160 {
14161 asection *o;
14162
b19a8f85 14163 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
81742b83 14164 || elf_object_id (sub) != elf_hash_table_id (htab)
b19a8f85 14165 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
c152c796
AM
14166 continue;
14167
57963c05
AM
14168 o = sub->sections;
14169 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14170 continue;
14171
7f6ab9f8
AM
14172 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
14173 Also treat note sections as a root, if the section is not part
8b6f4cd3
L
14174 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
14175 well as FINI_ARRAY sections for ld -r. */
c152c796 14176 for (o = sub->sections; o != NULL; o = o->next)
7f6ab9f8
AM
14177 if (!o->gc_mark
14178 && (o->flags & SEC_EXCLUDE) == 0
24007750 14179 && ((o->flags & SEC_KEEP) != 0
8b6f4cd3
L
14180 || (bfd_link_relocatable (info)
14181 && ((elf_section_data (o)->this_hdr.sh_type
14182 == SHT_PREINIT_ARRAY)
14183 || (elf_section_data (o)->this_hdr.sh_type
14184 == SHT_INIT_ARRAY)
14185 || (elf_section_data (o)->this_hdr.sh_type
14186 == SHT_FINI_ARRAY)))
7f6ab9f8 14187 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
7026832e 14188 && elf_next_in_group (o) == NULL
99fabbc9
JL
14189 && elf_linked_to_section (o) == NULL)
14190 || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
14191 && (elf_section_flags (o) & SHF_GNU_RETAIN))))
7f6ab9f8
AM
14192 {
14193 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
14194 return FALSE;
14195 }
c152c796
AM
14196 }
14197
6a5bb875 14198 /* Allow the backend to mark additional target specific sections. */
7f6ab9f8 14199 bed->gc_mark_extra_sections (info, gc_mark_hook);
6a5bb875 14200
c152c796 14201 /* ... and mark SEC_EXCLUDE for those that go. */
ccabcbe5 14202 return elf_gc_sweep (abfd, info);
c152c796
AM
14203}
14204\f
14205/* Called from check_relocs to record the existence of a VTINHERIT reloc. */
14206
14207bfd_boolean
14208bfd_elf_gc_record_vtinherit (bfd *abfd,
14209 asection *sec,
14210 struct elf_link_hash_entry *h,
14211 bfd_vma offset)
14212{
14213 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
14214 struct elf_link_hash_entry **search, *child;
ef53be89 14215 size_t extsymcount;
c152c796
AM
14216 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14217
14218 /* The sh_info field of the symtab header tells us where the
14219 external symbols start. We don't care about the local symbols at
14220 this point. */
14221 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
14222 if (!elf_bad_symtab (abfd))
14223 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
14224
14225 sym_hashes = elf_sym_hashes (abfd);
14226 sym_hashes_end = sym_hashes + extsymcount;
14227
14228 /* Hunt down the child symbol, which is in this section at the same
14229 offset as the relocation. */
14230 for (search = sym_hashes; search != sym_hashes_end; ++search)
14231 {
14232 if ((child = *search) != NULL
14233 && (child->root.type == bfd_link_hash_defined
14234 || child->root.type == bfd_link_hash_defweak)
14235 && child->root.u.def.section == sec
14236 && child->root.u.def.value == offset)
14237 goto win;
14238 }
14239
695344c0 14240 /* xgettext:c-format */
9793eb77 14241 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
2dcf00ce 14242 abfd, sec, (uint64_t) offset);
c152c796
AM
14243 bfd_set_error (bfd_error_invalid_operation);
14244 return FALSE;
14245
14246 win:
cbd0eecf 14247 if (!child->u2.vtable)
f6e332e6 14248 {
cbd0eecf
L
14249 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
14250 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
14251 if (!child->u2.vtable)
f6e332e6
AM
14252 return FALSE;
14253 }
c152c796
AM
14254 if (!h)
14255 {
14256 /* This *should* only be the absolute section. It could potentially
14257 be that someone has defined a non-global vtable though, which
14258 would be bad. It isn't worth paging in the local symbols to be
14259 sure though; that case should simply be handled by the assembler. */
14260
cbd0eecf 14261 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
c152c796
AM
14262 }
14263 else
cbd0eecf 14264 child->u2.vtable->parent = h;
c152c796
AM
14265
14266 return TRUE;
14267}
14268
14269/* Called from check_relocs to record the existence of a VTENTRY reloc. */
14270
14271bfd_boolean
a0ea3a14 14272bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
c152c796
AM
14273 struct elf_link_hash_entry *h,
14274 bfd_vma addend)
14275{
14276 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14277 unsigned int log_file_align = bed->s->log_file_align;
14278
a0ea3a14
L
14279 if (!h)
14280 {
14281 /* xgettext:c-format */
14282 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
14283 abfd, sec);
14284 bfd_set_error (bfd_error_bad_value);
14285 return FALSE;
14286 }
14287
cbd0eecf 14288 if (!h->u2.vtable)
f6e332e6 14289 {
cbd0eecf
L
14290 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
14291 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
14292 if (!h->u2.vtable)
f6e332e6
AM
14293 return FALSE;
14294 }
14295
cbd0eecf 14296 if (addend >= h->u2.vtable->size)
c152c796
AM
14297 {
14298 size_t size, bytes, file_align;
cbd0eecf 14299 bfd_boolean *ptr = h->u2.vtable->used;
c152c796
AM
14300
14301 /* While the symbol is undefined, we have to be prepared to handle
14302 a zero size. */
14303 file_align = 1 << log_file_align;
14304 if (h->root.type == bfd_link_hash_undefined)
14305 size = addend + file_align;
14306 else
14307 {
14308 size = h->size;
14309 if (addend >= size)
14310 {
14311 /* Oops! We've got a reference past the defined end of
14312 the table. This is probably a bug -- shall we warn? */
14313 size = addend + file_align;
14314 }
14315 }
14316 size = (size + file_align - 1) & -file_align;
14317
14318 /* Allocate one extra entry for use as a "done" flag for the
14319 consolidation pass. */
14320 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
14321
14322 if (ptr)
14323 {
a50b1753 14324 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
c152c796
AM
14325
14326 if (ptr != NULL)
14327 {
14328 size_t oldbytes;
14329
cbd0eecf 14330 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
c152c796
AM
14331 * sizeof (bfd_boolean));
14332 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
14333 }
14334 }
14335 else
a50b1753 14336 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
c152c796
AM
14337
14338 if (ptr == NULL)
14339 return FALSE;
14340
14341 /* And arrange for that done flag to be at index -1. */
cbd0eecf
L
14342 h->u2.vtable->used = ptr + 1;
14343 h->u2.vtable->size = size;
c152c796
AM
14344 }
14345
cbd0eecf 14346 h->u2.vtable->used[addend >> log_file_align] = TRUE;
c152c796
AM
14347
14348 return TRUE;
14349}
14350
ae17ab41
CM
14351/* Map an ELF section header flag to its corresponding string. */
14352typedef struct
14353{
14354 char *flag_name;
14355 flagword flag_value;
14356} elf_flags_to_name_table;
14357
14358static elf_flags_to_name_table elf_flags_to_names [] =
14359{
14360 { "SHF_WRITE", SHF_WRITE },
14361 { "SHF_ALLOC", SHF_ALLOC },
14362 { "SHF_EXECINSTR", SHF_EXECINSTR },
14363 { "SHF_MERGE", SHF_MERGE },
14364 { "SHF_STRINGS", SHF_STRINGS },
14365 { "SHF_INFO_LINK", SHF_INFO_LINK},
14366 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
14367 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
14368 { "SHF_GROUP", SHF_GROUP },
14369 { "SHF_TLS", SHF_TLS },
14370 { "SHF_MASKOS", SHF_MASKOS },
14371 { "SHF_EXCLUDE", SHF_EXCLUDE },
14372};
14373
b9c361e0
JL
14374/* Returns TRUE if the section is to be included, otherwise FALSE. */
14375bfd_boolean
ae17ab41 14376bfd_elf_lookup_section_flags (struct bfd_link_info *info,
8b127cbc 14377 struct flag_info *flaginfo,
b9c361e0 14378 asection *section)
ae17ab41 14379{
8b127cbc 14380 const bfd_vma sh_flags = elf_section_flags (section);
ae17ab41 14381
8b127cbc 14382 if (!flaginfo->flags_initialized)
ae17ab41 14383 {
8b127cbc
AM
14384 bfd *obfd = info->output_bfd;
14385 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
14386 struct flag_info_list *tf = flaginfo->flag_list;
b9c361e0
JL
14387 int with_hex = 0;
14388 int without_hex = 0;
14389
8b127cbc 14390 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
ae17ab41 14391 {
b9c361e0 14392 unsigned i;
8b127cbc 14393 flagword (*lookup) (char *);
ae17ab41 14394
8b127cbc
AM
14395 lookup = bed->elf_backend_lookup_section_flags_hook;
14396 if (lookup != NULL)
ae17ab41 14397 {
8b127cbc 14398 flagword hexval = (*lookup) ((char *) tf->name);
b9c361e0
JL
14399
14400 if (hexval != 0)
14401 {
14402 if (tf->with == with_flags)
14403 with_hex |= hexval;
14404 else if (tf->with == without_flags)
14405 without_hex |= hexval;
14406 tf->valid = TRUE;
14407 continue;
14408 }
ae17ab41 14409 }
8b127cbc 14410 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
ae17ab41 14411 {
8b127cbc 14412 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
b9c361e0
JL
14413 {
14414 if (tf->with == with_flags)
14415 with_hex |= elf_flags_to_names[i].flag_value;
14416 else if (tf->with == without_flags)
14417 without_hex |= elf_flags_to_names[i].flag_value;
14418 tf->valid = TRUE;
14419 break;
14420 }
14421 }
8b127cbc 14422 if (!tf->valid)
b9c361e0 14423 {
68ffbac6 14424 info->callbacks->einfo
9793eb77 14425 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
b9c361e0 14426 return FALSE;
ae17ab41
CM
14427 }
14428 }
8b127cbc
AM
14429 flaginfo->flags_initialized = TRUE;
14430 flaginfo->only_with_flags |= with_hex;
14431 flaginfo->not_with_flags |= without_hex;
ae17ab41 14432 }
ae17ab41 14433
8b127cbc 14434 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
b9c361e0
JL
14435 return FALSE;
14436
8b127cbc 14437 if ((flaginfo->not_with_flags & sh_flags) != 0)
b9c361e0
JL
14438 return FALSE;
14439
14440 return TRUE;
ae17ab41
CM
14441}
14442
c152c796
AM
14443struct alloc_got_off_arg {
14444 bfd_vma gotoff;
10455f89 14445 struct bfd_link_info *info;
c152c796
AM
14446};
14447
14448/* We need a special top-level link routine to convert got reference counts
14449 to real got offsets. */
14450
14451static bfd_boolean
14452elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
14453{
a50b1753 14454 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
10455f89
HPN
14455 bfd *obfd = gofarg->info->output_bfd;
14456 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
c152c796 14457
c152c796
AM
14458 if (h->got.refcount > 0)
14459 {
14460 h->got.offset = gofarg->gotoff;
10455f89 14461 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
c152c796
AM
14462 }
14463 else
14464 h->got.offset = (bfd_vma) -1;
14465
14466 return TRUE;
14467}
14468
14469/* And an accompanying bit to work out final got entry offsets once
14470 we're done. Should be called from final_link. */
14471
14472bfd_boolean
14473bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
14474 struct bfd_link_info *info)
14475{
14476 bfd *i;
14477 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14478 bfd_vma gotoff;
c152c796
AM
14479 struct alloc_got_off_arg gofarg;
14480
10455f89
HPN
14481 BFD_ASSERT (abfd == info->output_bfd);
14482
c152c796
AM
14483 if (! is_elf_hash_table (info->hash))
14484 return FALSE;
14485
14486 /* The GOT offset is relative to the .got section, but the GOT header is
14487 put into the .got.plt section, if the backend uses it. */
14488 if (bed->want_got_plt)
14489 gotoff = 0;
14490 else
14491 gotoff = bed->got_header_size;
14492
14493 /* Do the local .got entries first. */
c72f2fb2 14494 for (i = info->input_bfds; i; i = i->link.next)
c152c796
AM
14495 {
14496 bfd_signed_vma *local_got;
ef53be89 14497 size_t j, locsymcount;
c152c796
AM
14498 Elf_Internal_Shdr *symtab_hdr;
14499
14500 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14501 continue;
14502
14503 local_got = elf_local_got_refcounts (i);
14504 if (!local_got)
14505 continue;
14506
14507 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14508 if (elf_bad_symtab (i))
14509 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14510 else
14511 locsymcount = symtab_hdr->sh_info;
14512
14513 for (j = 0; j < locsymcount; ++j)
14514 {
14515 if (local_got[j] > 0)
14516 {
14517 local_got[j] = gotoff;
10455f89 14518 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
c152c796
AM
14519 }
14520 else
14521 local_got[j] = (bfd_vma) -1;
14522 }
14523 }
14524
14525 /* Then the global .got entries. .plt refcounts are handled by
14526 adjust_dynamic_symbol */
14527 gofarg.gotoff = gotoff;
10455f89 14528 gofarg.info = info;
c152c796
AM
14529 elf_link_hash_traverse (elf_hash_table (info),
14530 elf_gc_allocate_got_offsets,
14531 &gofarg);
14532 return TRUE;
14533}
14534
14535/* Many folk need no more in the way of final link than this, once
14536 got entry reference counting is enabled. */
14537
14538bfd_boolean
14539bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14540{
14541 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14542 return FALSE;
14543
14544 /* Invoke the regular ELF backend linker to do all the work. */
14545 return bfd_elf_final_link (abfd, info);
14546}
14547
14548bfd_boolean
14549bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14550{
a50b1753 14551 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
c152c796
AM
14552
14553 if (rcookie->bad_symtab)
14554 rcookie->rel = rcookie->rels;
14555
14556 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14557 {
14558 unsigned long r_symndx;
14559
14560 if (! rcookie->bad_symtab)
14561 if (rcookie->rel->r_offset > offset)
14562 return FALSE;
14563 if (rcookie->rel->r_offset != offset)
14564 continue;
14565
14566 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
2c2fa401 14567 if (r_symndx == STN_UNDEF)
c152c796
AM
14568 return TRUE;
14569
14570 if (r_symndx >= rcookie->locsymcount
14571 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14572 {
14573 struct elf_link_hash_entry *h;
14574
14575 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14576
14577 while (h->root.type == bfd_link_hash_indirect
14578 || h->root.type == bfd_link_hash_warning)
14579 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14580
14581 if ((h->root.type == bfd_link_hash_defined
14582 || h->root.type == bfd_link_hash_defweak)
5b69e357
AM
14583 && (h->root.u.def.section->owner != rcookie->abfd
14584 || h->root.u.def.section->kept_section != NULL
14585 || discarded_section (h->root.u.def.section)))
c152c796 14586 return TRUE;
c152c796
AM
14587 }
14588 else
14589 {
14590 /* It's not a relocation against a global symbol,
14591 but it could be a relocation against a local
14592 symbol for a discarded section. */
14593 asection *isec;
14594 Elf_Internal_Sym *isym;
14595
14596 /* Need to: get the symbol; get the section. */
14597 isym = &rcookie->locsyms[r_symndx];
cb33740c 14598 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
5b69e357
AM
14599 if (isec != NULL
14600 && (isec->kept_section != NULL
14601 || discarded_section (isec)))
cb33740c 14602 return TRUE;
c152c796
AM
14603 }
14604 return FALSE;
14605 }
14606 return FALSE;
14607}
14608
14609/* Discard unneeded references to discarded sections.
75938853
AM
14610 Returns -1 on error, 1 if any section's size was changed, 0 if
14611 nothing changed. This function assumes that the relocations are in
14612 sorted order, which is true for all known assemblers. */
c152c796 14613
75938853 14614int
c152c796
AM
14615bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14616{
14617 struct elf_reloc_cookie cookie;
18cd5bce 14618 asection *o;
c152c796 14619 bfd *abfd;
75938853 14620 int changed = 0;
c152c796
AM
14621
14622 if (info->traditional_format
14623 || !is_elf_hash_table (info->hash))
75938853 14624 return 0;
c152c796 14625
18cd5bce
AM
14626 o = bfd_get_section_by_name (output_bfd, ".stab");
14627 if (o != NULL)
c152c796 14628 {
18cd5bce 14629 asection *i;
c152c796 14630
18cd5bce 14631 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
8da3dbc5 14632 {
18cd5bce
AM
14633 if (i->size == 0
14634 || i->reloc_count == 0
14635 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14636 continue;
c152c796 14637
18cd5bce
AM
14638 abfd = i->owner;
14639 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14640 continue;
c152c796 14641
18cd5bce 14642 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14643 return -1;
c152c796 14644
18cd5bce
AM
14645 if (_bfd_discard_section_stabs (abfd, i,
14646 elf_section_data (i)->sec_info,
5241d853
RS
14647 bfd_elf_reloc_symbol_deleted_p,
14648 &cookie))
75938853 14649 changed = 1;
18cd5bce
AM
14650
14651 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14652 }
18cd5bce
AM
14653 }
14654
2f0c68f2
CM
14655 o = NULL;
14656 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14657 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
18cd5bce
AM
14658 if (o != NULL)
14659 {
14660 asection *i;
d7153c4a 14661 int eh_changed = 0;
66631823 14662 unsigned int eh_alignment; /* Octets. */
c152c796 14663
18cd5bce 14664 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
c152c796 14665 {
18cd5bce
AM
14666 if (i->size == 0)
14667 continue;
14668
14669 abfd = i->owner;
14670 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14671 continue;
14672
14673 if (!init_reloc_cookie_for_section (&cookie, info, i))
75938853 14674 return -1;
18cd5bce
AM
14675
14676 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14677 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
c152c796
AM
14678 bfd_elf_reloc_symbol_deleted_p,
14679 &cookie))
d7153c4a
AM
14680 {
14681 eh_changed = 1;
14682 if (i->size != i->rawsize)
14683 changed = 1;
14684 }
18cd5bce
AM
14685
14686 fini_reloc_cookie_for_section (&cookie, i);
c152c796 14687 }
9866ffe2 14688
66631823
CE
14689 eh_alignment = ((1 << o->alignment_power)
14690 * bfd_octets_per_byte (output_bfd, o));
9866ffe2
AM
14691 /* Skip over zero terminator, and prevent empty sections from
14692 adding alignment padding at the end. */
14693 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14694 if (i->size == 0)
14695 i->flags |= SEC_EXCLUDE;
14696 else if (i->size > 4)
14697 break;
14698 /* The last non-empty eh_frame section doesn't need padding. */
14699 if (i != NULL)
14700 i = i->map_tail.s;
14701 /* Any prior sections must pad the last FDE out to the output
14702 section alignment. Otherwise we might have zero padding
14703 between sections, which would be seen as a terminator. */
14704 for (; i != NULL; i = i->map_tail.s)
14705 if (i->size == 4)
14706 /* All but the last zero terminator should have been removed. */
14707 BFD_FAIL ();
14708 else
14709 {
14710 bfd_size_type size
14711 = (i->size + eh_alignment - 1) & -eh_alignment;
14712 if (i->size != size)
af471f82 14713 {
9866ffe2
AM
14714 i->size = size;
14715 changed = 1;
14716 eh_changed = 1;
af471f82 14717 }
9866ffe2 14718 }
d7153c4a
AM
14719 if (eh_changed)
14720 elf_link_hash_traverse (elf_hash_table (info),
14721 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
18cd5bce 14722 }
c152c796 14723
18cd5bce
AM
14724 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14725 {
14726 const struct elf_backend_data *bed;
57963c05 14727 asection *s;
c152c796 14728
18cd5bce
AM
14729 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14730 continue;
57963c05
AM
14731 s = abfd->sections;
14732 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14733 continue;
18cd5bce
AM
14734
14735 bed = get_elf_backend_data (abfd);
14736
14737 if (bed->elf_backend_discard_info != NULL)
14738 {
14739 if (!init_reloc_cookie (&cookie, info, abfd))
75938853 14740 return -1;
18cd5bce
AM
14741
14742 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
75938853 14743 changed = 1;
18cd5bce
AM
14744
14745 fini_reloc_cookie (&cookie, abfd);
14746 }
c152c796
AM
14747 }
14748
2f0c68f2
CM
14749 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14750 _bfd_elf_end_eh_frame_parsing (info);
14751
14752 if (info->eh_frame_hdr_type
0e1862bb 14753 && !bfd_link_relocatable (info)
c152c796 14754 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
75938853 14755 changed = 1;
c152c796 14756
75938853 14757 return changed;
c152c796 14758}
082b7297 14759
43e1669b 14760bfd_boolean
0c511000 14761_bfd_elf_section_already_linked (bfd *abfd,
c77ec726 14762 asection *sec,
c0f00686 14763 struct bfd_link_info *info)
082b7297
L
14764{
14765 flagword flags;
c77ec726 14766 const char *name, *key;
082b7297
L
14767 struct bfd_section_already_linked *l;
14768 struct bfd_section_already_linked_hash_entry *already_linked_list;
0c511000 14769
c77ec726
AM
14770 if (sec->output_section == bfd_abs_section_ptr)
14771 return FALSE;
0c511000 14772
c77ec726 14773 flags = sec->flags;
0c511000 14774
c77ec726
AM
14775 /* Return if it isn't a linkonce section. A comdat group section
14776 also has SEC_LINK_ONCE set. */
14777 if ((flags & SEC_LINK_ONCE) == 0)
14778 return FALSE;
0c511000 14779
c77ec726
AM
14780 /* Don't put group member sections on our list of already linked
14781 sections. They are handled as a group via their group section. */
14782 if (elf_sec_group (sec) != NULL)
14783 return FALSE;
0c511000 14784
c77ec726
AM
14785 /* For a SHT_GROUP section, use the group signature as the key. */
14786 name = sec->name;
14787 if ((flags & SEC_GROUP) != 0
14788 && elf_next_in_group (sec) != NULL
14789 && elf_group_name (elf_next_in_group (sec)) != NULL)
14790 key = elf_group_name (elf_next_in_group (sec));
14791 else
14792 {
14793 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
0c511000 14794 if (CONST_STRNEQ (name, ".gnu.linkonce.")
c77ec726
AM
14795 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14796 key++;
0c511000 14797 else
c77ec726
AM
14798 /* Must be a user linkonce section that doesn't follow gcc's
14799 naming convention. In this case we won't be matching
14800 single member groups. */
14801 key = name;
0c511000 14802 }
6d2cd210 14803
c77ec726 14804 already_linked_list = bfd_section_already_linked_table_lookup (key);
082b7297
L
14805
14806 for (l = already_linked_list->entry; l != NULL; l = l->next)
14807 {
c2370991 14808 /* We may have 2 different types of sections on the list: group
c77ec726
AM
14809 sections with a signature of <key> (<key> is some string),
14810 and linkonce sections named .gnu.linkonce.<type>.<key>.
14811 Match like sections. LTO plugin sections are an exception.
14812 They are always named .gnu.linkonce.t.<key> and match either
14813 type of section. */
14814 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14815 && ((flags & SEC_GROUP) != 0
14816 || strcmp (name, l->sec->name) == 0))
e8a83e93
MB
14817 || (l->sec->owner->flags & BFD_PLUGIN) != 0
14818 || (sec->owner->flags & BFD_PLUGIN) != 0)
082b7297
L
14819 {
14820 /* The section has already been linked. See if we should
6d2cd210 14821 issue a warning. */
c77ec726
AM
14822 if (!_bfd_handle_already_linked (sec, l, info))
14823 return FALSE;
082b7297 14824
c77ec726 14825 if (flags & SEC_GROUP)
3d7f7666 14826 {
c77ec726
AM
14827 asection *first = elf_next_in_group (sec);
14828 asection *s = first;
3d7f7666 14829
c77ec726 14830 while (s != NULL)
3d7f7666 14831 {
c77ec726
AM
14832 s->output_section = bfd_abs_section_ptr;
14833 /* Record which group discards it. */
14834 s->kept_section = l->sec;
14835 s = elf_next_in_group (s);
14836 /* These lists are circular. */
14837 if (s == first)
14838 break;
3d7f7666
L
14839 }
14840 }
082b7297 14841
43e1669b 14842 return TRUE;
082b7297
L
14843 }
14844 }
14845
c77ec726
AM
14846 /* A single member comdat group section may be discarded by a
14847 linkonce section and vice versa. */
14848 if ((flags & SEC_GROUP) != 0)
3d7f7666 14849 {
c77ec726 14850 asection *first = elf_next_in_group (sec);
c2370991 14851
c77ec726
AM
14852 if (first != NULL && elf_next_in_group (first) == first)
14853 /* Check this single member group against linkonce sections. */
14854 for (l = already_linked_list->entry; l != NULL; l = l->next)
14855 if ((l->sec->flags & SEC_GROUP) == 0
14856 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14857 {
14858 first->output_section = bfd_abs_section_ptr;
14859 first->kept_section = l->sec;
14860 sec->output_section = bfd_abs_section_ptr;
14861 break;
14862 }
14863 }
14864 else
14865 /* Check this linkonce section against single member groups. */
14866 for (l = already_linked_list->entry; l != NULL; l = l->next)
14867 if (l->sec->flags & SEC_GROUP)
6d2cd210 14868 {
c77ec726 14869 asection *first = elf_next_in_group (l->sec);
6d2cd210 14870
c77ec726
AM
14871 if (first != NULL
14872 && elf_next_in_group (first) == first
14873 && bfd_elf_match_symbols_in_sections (first, sec, info))
14874 {
14875 sec->output_section = bfd_abs_section_ptr;
14876 sec->kept_section = first;
14877 break;
14878 }
6d2cd210 14879 }
0c511000 14880
c77ec726
AM
14881 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14882 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14883 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14884 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14885 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14886 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14887 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14888 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14889 The reverse order cannot happen as there is never a bfd with only the
14890 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14891 matter as here were are looking only for cross-bfd sections. */
14892
14893 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14894 for (l = already_linked_list->entry; l != NULL; l = l->next)
14895 if ((l->sec->flags & SEC_GROUP) == 0
14896 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14897 {
14898 if (abfd != l->sec->owner)
14899 sec->output_section = bfd_abs_section_ptr;
14900 break;
14901 }
80c29487 14902
082b7297 14903 /* This is the first section with this name. Record it. */
c77ec726 14904 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
bb6198d2 14905 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
c77ec726 14906 return sec->output_section == bfd_abs_section_ptr;
082b7297 14907}
81e1b023 14908
a4d8e49b
L
14909bfd_boolean
14910_bfd_elf_common_definition (Elf_Internal_Sym *sym)
14911{
14912 return sym->st_shndx == SHN_COMMON;
14913}
14914
14915unsigned int
14916_bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14917{
14918 return SHN_COMMON;
14919}
14920
14921asection *
14922_bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14923{
14924 return bfd_com_section_ptr;
14925}
10455f89
HPN
14926
14927bfd_vma
14928_bfd_elf_default_got_elt_size (bfd *abfd,
14929 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14930 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14931 bfd *ibfd ATTRIBUTE_UNUSED,
14932 unsigned long symndx ATTRIBUTE_UNUSED)
14933{
14934 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14935 return bed->s->arch_size / 8;
14936}
83bac4b0
NC
14937
14938/* Routines to support the creation of dynamic relocs. */
14939
83bac4b0
NC
14940/* Returns the name of the dynamic reloc section associated with SEC. */
14941
14942static const char *
14943get_dynamic_reloc_section_name (bfd * abfd,
14944 asection * sec,
14945 bfd_boolean is_rela)
14946{
ddcf1fcf 14947 char *name;
fd361982 14948 const char *old_name = bfd_section_name (sec);
ddcf1fcf 14949 const char *prefix = is_rela ? ".rela" : ".rel";
83bac4b0 14950
ddcf1fcf 14951 if (old_name == NULL)
83bac4b0
NC
14952 return NULL;
14953
ddcf1fcf 14954 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
68ffbac6 14955 sprintf (name, "%s%s", prefix, old_name);
83bac4b0
NC
14956
14957 return name;
14958}
14959
14960/* Returns the dynamic reloc section associated with SEC.
14961 If necessary compute the name of the dynamic reloc section based
14962 on SEC's name (looked up in ABFD's string table) and the setting
14963 of IS_RELA. */
14964
14965asection *
14966_bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14967 asection * sec,
14968 bfd_boolean is_rela)
14969{
14970 asection * reloc_sec = elf_section_data (sec)->sreloc;
14971
14972 if (reloc_sec == NULL)
14973 {
14974 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14975
14976 if (name != NULL)
14977 {
3d4d4302 14978 reloc_sec = bfd_get_linker_section (abfd, name);
83bac4b0
NC
14979
14980 if (reloc_sec != NULL)
14981 elf_section_data (sec)->sreloc = reloc_sec;
14982 }
14983 }
14984
14985 return reloc_sec;
14986}
14987
14988/* Returns the dynamic reloc section associated with SEC. If the
14989 section does not exist it is created and attached to the DYNOBJ
14990 bfd and stored in the SRELOC field of SEC's elf_section_data
14991 structure.
f8076f98 14992
83bac4b0
NC
14993 ALIGNMENT is the alignment for the newly created section and
14994 IS_RELA defines whether the name should be .rela.<SEC's name>
14995 or .rel.<SEC's name>. The section name is looked up in the
14996 string table associated with ABFD. */
14997
14998asection *
ca4be51c
AM
14999_bfd_elf_make_dynamic_reloc_section (asection *sec,
15000 bfd *dynobj,
15001 unsigned int alignment,
15002 bfd *abfd,
15003 bfd_boolean is_rela)
83bac4b0
NC
15004{
15005 asection * reloc_sec = elf_section_data (sec)->sreloc;
15006
15007 if (reloc_sec == NULL)
15008 {
15009 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
15010
15011 if (name == NULL)
15012 return NULL;
15013
3d4d4302 15014 reloc_sec = bfd_get_linker_section (dynobj, name);
83bac4b0
NC
15015
15016 if (reloc_sec == NULL)
15017 {
3d4d4302
AM
15018 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
15019 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
83bac4b0
NC
15020 if ((sec->flags & SEC_ALLOC) != 0)
15021 flags |= SEC_ALLOC | SEC_LOAD;
15022
3d4d4302 15023 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
83bac4b0
NC
15024 if (reloc_sec != NULL)
15025 {
8877b5e5
AM
15026 /* _bfd_elf_get_sec_type_attr chooses a section type by
15027 name. Override as it may be wrong, eg. for a user
15028 section named "auto" we'll get ".relauto" which is
15029 seen to be a .rela section. */
15030 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
fd361982 15031 if (!bfd_set_section_alignment (reloc_sec, alignment))
83bac4b0
NC
15032 reloc_sec = NULL;
15033 }
15034 }
15035
15036 elf_section_data (sec)->sreloc = reloc_sec;
15037 }
15038
15039 return reloc_sec;
15040}
1338dd10 15041
bffebb6b
AM
15042/* Copy the ELF symbol type and other attributes for a linker script
15043 assignment from HSRC to HDEST. Generally this should be treated as
15044 if we found a strong non-dynamic definition for HDEST (except that
15045 ld ignores multiple definition errors). */
1338dd10 15046void
bffebb6b
AM
15047_bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
15048 struct bfd_link_hash_entry *hdest,
15049 struct bfd_link_hash_entry *hsrc)
1338dd10 15050{
bffebb6b
AM
15051 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
15052 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
15053 Elf_Internal_Sym isym;
1338dd10
PB
15054
15055 ehdest->type = ehsrc->type;
35fc36a8 15056 ehdest->target_internal = ehsrc->target_internal;
bffebb6b
AM
15057
15058 isym.st_other = ehsrc->other;
5160d0f3 15059 elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, TRUE, FALSE);
1338dd10 15060}
351f65ca
L
15061
15062/* Append a RELA relocation REL to section S in BFD. */
15063
15064void
15065elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15066{
15067 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15068 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
15069 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
15070 bed->s->swap_reloca_out (abfd, rel, loc);
15071}
15072
15073/* Append a REL relocation REL to section S in BFD. */
15074
15075void
15076elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
15077{
15078 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15079 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
15080 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
59d6ffb2 15081 bed->s->swap_reloc_out (abfd, rel, loc);
351f65ca 15082}
7dba9362
AM
15083
15084/* Define __start, __stop, .startof. or .sizeof. symbol. */
15085
15086struct bfd_link_hash_entry *
15087bfd_elf_define_start_stop (struct bfd_link_info *info,
15088 const char *symbol, asection *sec)
15089{
487b6440 15090 struct elf_link_hash_entry *h;
7dba9362 15091
487b6440
AM
15092 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
15093 FALSE, FALSE, TRUE);
e1b5d517 15094 /* NB: Common symbols will be turned into definition later. */
487b6440
AM
15095 if (h != NULL
15096 && (h->root.type == bfd_link_hash_undefined
15097 || h->root.type == bfd_link_hash_undefweak
e1b5d517
L
15098 || ((h->ref_regular || h->def_dynamic)
15099 && !h->def_regular
15100 && h->root.type != bfd_link_hash_common)))
7dba9362 15101 {
bf3077a6 15102 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
e1b5d517 15103 h->verinfo.verdef = NULL;
487b6440
AM
15104 h->root.type = bfd_link_hash_defined;
15105 h->root.u.def.section = sec;
15106 h->root.u.def.value = 0;
15107 h->def_regular = 1;
15108 h->def_dynamic = 0;
15109 h->start_stop = 1;
15110 h->u2.start_stop_section = sec;
15111 if (symbol[0] == '.')
15112 {
15113 /* .startof. and .sizeof. symbols are local. */
559192d8
AM
15114 const struct elf_backend_data *bed;
15115 bed = get_elf_backend_data (info->output_bfd);
15116 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
487b6440 15117 }
36b8fda5
AM
15118 else
15119 {
15120 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
cae64165
RM
15121 h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
15122 | info->start_stop_visibility);
bf3077a6 15123 if (was_dynamic)
36b8fda5
AM
15124 bfd_elf_link_record_dynamic_symbol (info, h);
15125 }
487b6440 15126 return &h->root;
7dba9362 15127 }
487b6440 15128 return NULL;
7dba9362 15129}
5dbc8b37
L
15130
15131/* Find dynamic relocs for H that apply to read-only sections. */
15132
15133asection *
15134_bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
15135{
15136 struct elf_dyn_relocs *p;
15137
15138 for (p = h->dyn_relocs; p != NULL; p = p->next)
15139 {
15140 asection *s = p->sec->output_section;
15141
15142 if (s != NULL && (s->flags & SEC_READONLY) != 0)
15143 return p->sec;
15144 }
15145 return NULL;
15146}
d49e5065
L
15147
15148/* Set DF_TEXTREL if we find any dynamic relocs that apply to
15149 read-only sections. */
15150
15151bfd_boolean
15152_bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
15153{
15154 asection *sec;
15155
15156 if (h->root.type == bfd_link_hash_indirect)
15157 return TRUE;
15158
15159 sec = _bfd_elf_readonly_dynrelocs (h);
15160 if (sec != NULL)
15161 {
15162 struct bfd_link_info *info = (struct bfd_link_info *) inf;
15163
15164 info->flags |= DF_TEXTREL;
15165 /* xgettext:c-format */
15166 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
15167 "in read-only section `%pA'\n"),
15168 sec->owner, h->root.root.string, sec);
15169
15170 if (bfd_link_textrel_check (info))
15171 /* xgettext:c-format */
15172 info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
15173 "in read-only section `%pA'\n"),
15174 sec->owner, h->root.root.string, sec);
15175
15176 /* Not an error, just cut short the traversal. */
15177 return FALSE;
15178 }
15179 return TRUE;
15180}
3084d7a2
L
15181
15182/* Add dynamic tags. */
15183
15184bfd_boolean
15185_bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
15186 bfd_boolean need_dynamic_reloc)
15187{
15188 struct elf_link_hash_table *htab = elf_hash_table (info);
15189
15190 if (htab->dynamic_sections_created)
15191 {
15192 /* Add some entries to the .dynamic section. We fill in the
15193 values later, in finish_dynamic_sections, but we must add
15194 the entries now so that we get the correct size for the
15195 .dynamic section. The DT_DEBUG entry is filled in by the
15196 dynamic linker and used by the debugger. */
15197#define add_dynamic_entry(TAG, VAL) \
15198 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
15199
15200 const struct elf_backend_data *bed
15201 = get_elf_backend_data (output_bfd);
15202
15203 if (bfd_link_executable (info))
15204 {
15205 if (!add_dynamic_entry (DT_DEBUG, 0))
15206 return FALSE;
15207 }
15208
15209 if (htab->dt_pltgot_required || htab->splt->size != 0)
15210 {
15211 /* DT_PLTGOT is used by prelink even if there is no PLT
15212 relocation. */
15213 if (!add_dynamic_entry (DT_PLTGOT, 0))
15214 return FALSE;
15215 }
15216
15217 if (htab->dt_jmprel_required || htab->srelplt->size != 0)
15218 {
15219 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
15220 || !add_dynamic_entry (DT_PLTREL,
15221 (bed->rela_plts_and_copies_p
15222 ? DT_RELA : DT_REL))
15223 || !add_dynamic_entry (DT_JMPREL, 0))
15224 return FALSE;
15225 }
15226
15227 if (htab->tlsdesc_plt
15228 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
15229 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
15230 return FALSE;
15231
15232 if (need_dynamic_reloc)
15233 {
15234 if (bed->rela_plts_and_copies_p)
15235 {
15236 if (!add_dynamic_entry (DT_RELA, 0)
15237 || !add_dynamic_entry (DT_RELASZ, 0)
15238 || !add_dynamic_entry (DT_RELAENT,
15239 bed->s->sizeof_rela))
15240 return FALSE;
15241 }
15242 else
15243 {
15244 if (!add_dynamic_entry (DT_REL, 0)
15245 || !add_dynamic_entry (DT_RELSZ, 0)
15246 || !add_dynamic_entry (DT_RELENT,
15247 bed->s->sizeof_rel))
15248 return FALSE;
15249 }
15250
15251 /* If any dynamic relocs apply to a read-only section,
15252 then we need a DT_TEXTREL entry. */
15253 if ((info->flags & DF_TEXTREL) == 0)
15254 elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
15255 info);
15256
15257 if ((info->flags & DF_TEXTREL) != 0)
15258 {
15259 if (htab->ifunc_resolvers)
15260 info->callbacks->einfo
15261 (_("%P: warning: GNU indirect functions with DT_TEXTREL "
15262 "may result in a segfault at runtime; recompile with %s\n"),
15263 bfd_link_dll (info) ? "-fPIC" : "-fPIE");
15264
15265 if (!add_dynamic_entry (DT_TEXTREL, 0))
15266 return FALSE;
15267 }
15268 }
15269 }
15270#undef add_dynamic_entry
15271
15272 return TRUE;
15273}
This page took 2.724349 seconds and 4 git commands to generate.